-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutility.c
More file actions
643 lines (544 loc) · 10.1 KB
/
utility.c
File metadata and controls
643 lines (544 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
#define PRINTOPTIONS
#include <stdarg.h>
#include <sys/utsname.h>
#include <sys/time.h>
#include "telnetd.h"
struct buflist {
struct buflist *next;
char *buf;
size_t len;
};
static struct buflist head = { next: &head, buf: 0, len: 0 };
static struct buflist *tail = &head;
static size_t skip;
static int trailing;
static size_t listlen;
static int doclear;
static struct buflist *urg;
/*
* ttloop
*
* A small subroutine to flush the network output buffer, get some data
* from the network, and pass it through the telnet state machine. We
* also flush the pty input buffer (by dropping its data) if it becomes
* too full.
*/
void
ttloop(void)
{
netflush();
ncc = read(net, netibuf, sizeof(netibuf));
if (ncc < 0) {
syslog(LOG_INFO, "ttloop: read: %m\n");
exit(1);
} else if (ncc == 0) {
syslog(LOG_INFO, "ttloop: peer died: EOF\n");
exit(1);
}
netip = netibuf;
telrcv(); /* state machine */
if (ncc > 0) {
pfrontp = pbackp = ptyobuf;
telrcv();
}
} /* end of ttloop */
/*
* Check a descriptor to see if out of band data exists on it.
*/
int stilloob(int s) /* socket number */
{
static struct timeval timeout = { 0, 0 };
fd_set excepts;
int value;
do {
FD_ZERO(&excepts);
FD_SET(s, &excepts);
value = select(s+1, (fd_set *)0, (fd_set *)0, &excepts, &timeout);
} while ((value == -1) && (errno == EINTR));
if (value < 0) {
fatalperror(pty, "select");
}
if (FD_ISSET(s, &excepts)) {
return 1;
} else {
return 0;
}
}
void ptyflush(void)
{
int n;
if ((n = pfrontp - pbackp) > 0) {
n = write(pty, pbackp, n);
}
if (n < 0) {
if (errno == EWOULDBLOCK || errno == EINTR)
return;
cleanup(0);
}
pbackp += n;
if (pbackp == pfrontp)
pbackp = pfrontp = ptyobuf;
}
/*
* nextitem()
*
* Return the address of the next "item" in the TELNET data
* stream. This will be the address of the next character if
* the current address is a user data character, or it will
* be the address of the character following the TELNET command
* if the current address is a TELNET IAC ("I Am a Command")
* character.
*/
static
const char *
nextitem(
const char *current, const char *end,
const char *next, const char *nextend
) {
if (*current++ != IAC) {
while (current < end && *current++ != IAC)
;
goto out;
}
if (current >= end) {
current = next;
if (!current) {
return 0;
}
end = nextend;
next = 0;
}
switch ((unsigned char)*current++) {
case DO:
case DONT:
case WILL:
case WONT:
current++;
break;
case SB: /* loop forever looking for the SE */
for (;;) {
int iac;
while (iac = 0, current < end) {
if (*current++ == IAC) {
if (current >= end) {
iac = 1;
break;
}
iac:
if (*current++ == SE) {
goto out;
}
}
}
current = next;
if (!current) {
return 0;
}
end = nextend;
next = 0;
if (iac) {
goto iac;
}
}
}
out:
return (next ? next + (current - end) : current);
} /* end of nextitem */
/*
* netclear()
*
* We are about to do a TELNET SYNCH operation. Clear
* the path to the network.
*
* Things are a bit tricky since we may have sent the first
* byte or so of a previous TELNET command into the network.
* So, we have to scan the network buffer from the beginning
* until we are up to where we want to be.
*
* A side effect of what we do, just to keep things
* simple, is to clear the urgent data pointer. The principal
* caller should be setting the urgent data pointer AFTER calling
* us in any case.
*/
void netclear(void)
{
doclear++;
netflush();
doclear--;
} /* end of netclear */
static void
netwritebuf(void)
{
struct iovec *vector;
struct iovec *v;
struct buflist *lp;
ssize_t n;
size_t len;
int ltrailing = trailing;
if (!listlen)
return;
vector = malloc(listlen * sizeof(struct iovec));
if (!vector) {
return;
}
len = listlen - (doclear & ltrailing);
v = vector;
lp = head.next;
while (lp != &head) {
if (lp == urg) {
len = v - vector;
if (!len) {
n = send(net, lp->buf, 1, MSG_OOB);
if (n > 0) {
urg = 0;
}
goto epi;
}
break;
}
v->iov_base = lp->buf;
v->iov_len = lp->len;
v++;
lp = lp->next;
}
vector->iov_base = (char *)vector->iov_base + skip;
vector->iov_len -= skip;
n = writev(net, vector, len);
epi:
free(vector);
if (n < 0) {
if (errno != EWOULDBLOCK && errno != EINTR)
cleanup(0);
return;
}
len = n + skip;
lp = head.next;
while (lp->len <= len) {
if (lp == tail && ltrailing) {
break;
}
len -= lp->len;
head.next = lp->next;
listlen--;
free(lp->buf);
free(lp);
lp = head.next;
if (lp == &head) {
tail = &head;
break;
}
}
skip = len;
}
/*
* netflush
* Send as much data as possible to the network,
* handling requests for urgent data.
*/
void
netflush(void)
{
if (fflush(netfile)) {
/* out of memory? */
cleanup(0);
}
netwritebuf();
}
/*
* miscellaneous functions doing a variety of little jobs follow ...
*/
void
fatal(int f, const char *msg)
{
char buf[BUFSIZ];
snprintf(buf, sizeof(buf), "yoursh: %s.\r\n", msg);
(void)write(f, buf, (int)strlen(buf));
sleep(1); /*XXX*/
exit(1);
}
void
fatalperror(int f, const char *msg)
{
char buf[BUFSIZ];
snprintf(buf, sizeof(buf), "%s: %s\r\n", msg, strerror(errno));
fatal(f, buf);
}
char *editedhost;
struct utsname kerninfo;
void
edithost(const char *pat, const char *host)
{
char *res;
uname(&kerninfo);
if (!pat)
pat = "";
res = realloc(editedhost, strlen(pat) + strlen(host) + 1);
if (!res) {
if (editedhost) {
free(editedhost);
editedhost = 0;
}
fprintf(stderr, "edithost: Out of memory\n");
return;
}
editedhost = res;
while (*pat) {
switch (*pat) {
case '#':
if (*host)
host++;
break;
case '@':
if (*host)
*res++ = *host++;
break;
default:
*res++ = *pat;
break;
}
pat++;
}
if (*host)
(void) strcpy(res, host);
else
*res = '\0';
}
static char *putlocation;
static
void
putstr(const char *s)
{
while (*s) putchr(*s++);
}
void putchr(int cc)
{
*putlocation++ = cc;
}
static char fmtstr[] = { "%H:%M on %A, %d %B %Y" };
void putf(const char *cp, char *where)
{
char *slash;
time_t t;
char db[100];
if (where)
putlocation = where;
while (*cp) {
if (*cp != '%') {
putchr(*cp++);
continue;
}
switch (*++cp) {
case 't':
slash = strrchr(line, '/');
if (slash == NULL)
putstr(line);
else
putstr(slash+1);
break;
case 'h':
if (editedhost) {
putstr(editedhost);
}
break;
case 'd':
(void)time(&t);
(void)strftime(db, sizeof(db), fmtstr, localtime(&t));
putstr(db);
break;
case '%':
putchr('%');
break;
case 'D':
{
char buff[128];
if (getdomainname(buff,sizeof(buff)) < 0
|| buff[0] == '\0'
|| strcmp(buff, "(none)") == 0)
break;
putstr(buff);
}
break;
case 'i':
{
char buff[3];
FILE *fp;
int p, c;
if ((fp = fopen(ISSUE_FILE, "r")) == NULL)
break;
p = '\n';
while ((c = fgetc(fp)) != EOF) {
if (p == '\n' && c == '#') {
do {
c = fgetc(fp);
} while (c != EOF && c != '\n');
continue;
} else if (c == '%') {
buff[0] = c;
c = fgetc(fp);
if (c == EOF) break;
buff[1] = c;
buff[2] = '\0';
putf(buff, NULL);
} else {
if (c == '\n') putchr('\r');
putchr(c);
p = c;
}
};
(void) fclose(fp);
}
return; /* ignore remainder of the banner string */
/*NOTREACHED*/
case 's':
putstr(kerninfo.sysname);
break;
case 'm':
putstr(kerninfo.machine);
break;
case 'r':
putstr(kerninfo.release);
break;
case 'v':
#ifdef __linux__
putstr(kerninfo.version);
#else
puts(kerninfo.version);
#endif
break;
}
cp++;
}
}
static struct buflist *
addbuf(const char *buf, size_t len)
{
struct buflist *bufl;
bufl = malloc(sizeof(struct buflist));
if (!bufl) {
return 0;
}
bufl->next = tail->next;
bufl->buf = malloc(len);
if (!bufl->buf) {
free(bufl);
return 0;
}
bufl->len = len;
tail = tail->next = bufl;
listlen++;
memcpy(bufl->buf, buf, len);
return bufl;
}
static ssize_t
netwrite(void *cookie, const char *buf, size_t len)
{
size_t ret;
const char *const end = buf + len;
int ltrailing = trailing;
int ldoclear = doclear;
#define wewant(p) ((*p&0xff) == IAC) && \
((*(p+1)&0xff) != EC) && ((*(p+1)&0xff) != EL)
ret = 0;
if (ltrailing) {
const char *p;
size_t l;
size_t m = tail->len;
p = nextitem(tail->buf, tail->buf + tail->len, buf, end);
ltrailing = !p;
if (ltrailing) {
p = end;
}
l = p - buf;
tail->len += l;
tail->buf = realloc(tail->buf, tail->len);
if (!tail->buf) {
return -1;
}
memcpy(tail->buf + m, buf, l);
buf += l;
len -= l;
ret += l;
trailing = ltrailing;
}
if (ldoclear) {
struct buflist *lpprev;
skip = 0;
lpprev = &head;
for (;;) {
struct buflist *lp;
lp = lpprev->next;
if (lp == &head) {
tail = lpprev;
break;
}
if (lp == tail && ltrailing) {
break;
}
if (!wewant(lp->buf)) {
lpprev->next = lp->next;
listlen--;
free(lp->buf);
free(lp);
} else {
lpprev = lp;
}
}
}
while (len) {
const char *p;
size_t l;
p = nextitem(buf, end, NULL, NULL);
ltrailing = !p;
if (ltrailing) {
p = end;
} else if (ldoclear) {
if (!wewant(buf)) {
l = p - buf;
goto cont;
}
}
l = p - buf;
if (!addbuf(buf, l)) {
return ret ? ret : -1;
}
trailing = ltrailing;
cont:
buf += l;
len -= l;
ret += l;
}
netwritebuf();
return ret;
}
void
netopen() {
/*
typedef struct {
cookie_read_function_t *read;
cookie_write_function_t *write;
cookie_seek_function_t *seek;
cookie_close_function_t *close;
} cookie_io_functions_t;
*/
static const cookie_io_functions_t funcs = {
//0, netwrite, 0, 0
read: 0, write: netwrite, seek: 0, close: 0
};
netfile = fopencookie(0, "w", funcs);
}
extern int not42;
void
sendurg(const char *buf, size_t len) {
if (!not42) {
fwrite(buf, 1, len, netfile);
return;
}
urg = addbuf(buf, len);
}
size_t
netbuflen(int flush) {
if (flush) {
netflush();
}
return listlen != 1 ? listlen : tail->len - skip;
}