Codebase list conserver / HEAD conserver / client.c
HEAD

Tree @HEAD (Download .tar.gz)

client.c @HEADraw · history · blame

  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
/*
 *  Copyright conserver.com, 2000
 *
 *  Maintainer/Enhancer: Bryan Stansell (bryan@conserver.com)
 *
 *  Copyright GNAC, Inc., 1998
 */

/*
 * Copyright 1992 Purdue Research Foundation, West Lafayette, Indiana
 * 47907.  All rights reserved.
 *
 * Written by Kevin S Braunsdorf, ksb@cc.purdue.edu, purdue!ksb
 *
 * This software is not subject to any license of the American Telephone
 * and Telegraph Company or the Regents of the University of California.
 *
 * Permission is granted to anyone to use this software for any purpose on
 * any computer system, and to alter it and redistribute it freely, subject
 * to the following restrictions:
 *
 * 1. Neither the authors nor Purdue University are responsible for any
 *    consequences of the use of this software.
 *
 * 2. The origin of this software must not be misrepresented, either by
 *    explicit claim or by omission.  Credit to the authors and Purdue
 *    University must appear in documentation and sources.
 *
 * 3. Altered versions must be plainly marked as such, and must not be
 *    misrepresented as being the original software.
 *
 * 4. This notice may not be removed or altered.
 */

#include <compat.h>

#include <cutil.h>
#include <consent.h>
#include <access.h>
#include <client.h>
#include <group.h>
#include <readcfg.h>

#if USE_IPV6
# include <sys/socket.h>
# include <netdb.h>
#endif /* USE_IPV6 */

#if defined(USE_LIBWRAP)
# include <syslog.h>
# include <tcpd.h>
int allow_severity = LOG_INFO;
int deny_severity = LOG_WARNING;
#endif


/* find the next guy who wants to write on the console			(ksb)
 */
void
FindWrite(CONSENT *pCE)
{
    CONSCLIENT *pCLfound = (CONSCLIENT *)0;
    CONSCLIENT *pCL;

    /* make the first guy (last on the list) to have the `want write' bit set
     * the writer (tell him of the promotion, too)  we could look for the most
     * recent or some such... I guess it doesn't matter that much.
     */
    if (pCE->pCLwr != (CONSCLIENT *)0 || pCE->fronly)
	return;

    for (pCL = pCE->pCLon; (CONSCLIENT *)0 != pCL; pCL = pCL->pCLnext) {
	if (pCL->fwantwr && !pCL->fro)
	    pCLfound = pCL;
    }

    if (pCLfound != (CONSCLIENT *)0) {
	pCLfound->fwantwr = 0;
	pCLfound->fwr = 1;
	if (pCE->nolog) {
	    FileWrite(pCLfound->fd, FLAGFALSE,
		      "\r\n[attached (nologging)]\r\n", -1);
	} else {
	    FileWrite(pCLfound->fd, FLAGFALSE, "\r\n[attached]\r\n", -1);
	}
	TagLogfileAct(pCE, "%s attached", pCLfound->acid->string);
	pCE->pCLwr = pCLfound;
    }
}

void
BumpClient(CONSENT *pCE, char *message)
{
    if ((CONSCLIENT *)0 == pCE->pCLwr)
	return;

    if ((char *)0 != message)
	FileWrite(pCE->pCLwr->fd, FLAGFALSE, message, -1);
    pCE->pCLwr->fwantwr = 0;
    pCE->pCLwr->fwr = 0;
    pCE->pCLwr = (CONSCLIENT *)0;
}

/* replay last 'back' lines of the log file upon connect to console	(ksb)
 *
 * NB: we know the console might be spewing when the replay happens,
 * we want to just output what is in the log file and get out,
 * so we don't drop chars...
 */
#define REPLAYBUFFER 4096

void
Replay(CONSENT *pCE, CONSFILE *fdOut, unsigned short back)
{
    CONSFILE *fdLog = (CONSFILE *)0;
    STRING *line = (STRING *)0;
    off_t file_pos;
    off_t buf_pos;
    char *buf = (char *)0;
    char *bp = (char *)0;
    int ch;
    struct stat stLog;
    int ln;
    int was_mark = 0;
#if HAVE_DMALLOC && DMALLOC_MARK_REPLAY
    unsigned long dmallocMarkReplay = 0;
#endif

    if (pCE != (CONSENT *)0 && pCE->logfile != (char *)0)
	fdLog = FileOpen(pCE->logfile, O_RDONLY, 0644);

    if (fdLog == (CONSFILE *)0) {
	FileWrite(fdOut, FLAGFALSE, "[no log file on this console]\r\n",
		  -1);
	return;
    }
#if HAVE_DMALLOC && DMALLOC_MARK_REPLAY
    dmallocMarkReplay = dmalloc_mark();
#endif

    /* find the size of the file
     */
    if (0 != FileStat(fdLog, &stLog))
	goto common_exit;

    file_pos = stLog.st_size - 1;	/* point at last byte */
    buf_pos = file_pos + 1;

    if ((char *)0 == (buf = malloc(REPLAYBUFFER)))
	OutOfMem();
    bp = buf + 1;		/* just give it something - it resets below */

    line = AllocString();

    /* loop as long as there is data in the file or we have not found
     * the requested number of lines
     */
    ln = -1;
    for (; file_pos >= 0; file_pos--, bp--) {
	if (file_pos < buf_pos) {
	    int r;

	    /* read one buffer worth of data a buffer boundary
	     *
	     * the first read will probably not get a full buffer but
	     * the rest (as we work our way back in the file) should be
	     */
	    buf_pos = (file_pos / REPLAYBUFFER) * REPLAYBUFFER;
	    if (FileSeek(fdLog, buf_pos, SEEK_SET) < 0) {
		goto common_exit;
	    }
	    if ((r = FileRead(fdLog, buf, REPLAYBUFFER)) < 0) {
		goto common_exit;
	    }
	    bp = buf + r - 1;
	}

	/* process the next character
	 */
	if ((ch = *bp) == '\n') {
	    if (ln >= 0) {
		int i;
		int u;
		int is_mark = 0;

		/* reverse the text to put it in forward order
		 */
		u = line->used - 1;
		for (i = 0; i < u / 2; i++) {
		    int temp;

		    temp = line->string[i];
		    line->string[i] = line->string[u - i - 1];
		    line->string[u - i - 1] = temp;
		}

		/* see if this line is a MARK
		 */
		if (line->used > 0 && line->string[0] == '[') {
		    char dummy[4];
		    int j;
		    i = sscanf(line->string + 1,
			       "-- MARK -- %3c %3c %d %d:%d:%d %d]\r\n",
			       dummy, dummy, &j, &j, &j, &j, &j);
		    is_mark = (i == 7);
		}

		/* process this line
		 */
		if (is_mark && was_mark) {
		    /* this is a mark and the previous line is also
		     * a mark, so reduce the line count 'cause it'll
		     * go up by one and we're joining them on output.
		     */
		    ln--;
		}
		was_mark = is_mark;
	    }

	    /* advance to the next line and break if we have enough
	     */
	    ln++;
	    BuildString((char *)0, line);
	    if (ln >= back) {
		break;
	    }
	}

	/* if we have a character but no lines yet, the last text in the
	 * file does not end with a newline, so start the first line anyway
	 */
	if (ln < 0) {
	    ln = 0;
	}
	BuildStringChar(ch, line);

	/* if we've processed "a lot" of data for a line, then bail
	 * why?  there must be some very long non-newline terminated
	 * strings and if we just keep going back, we could spew lots
	 * of data and chew up lots of memory
	 */
	if (line->used > MAXREPLAYLINELEN) {
	    break;
	}
    }

    /* move forward.  either we hit the beginning of the file and we
     * move to the first byte, or we hit a \n and we move past it
     */
    file_pos++;

    /* Now output the lines, starting from where we stopped */
    if (FileSeek(fdLog, file_pos, SEEK_SET) >= 0) {
	int eof = 0;
	int i = 0;
	int r = 0;
	STRING *mark_beg = (STRING *)0;
	STRING *mark_end = (STRING *)0;

	mark_beg = AllocString();
	mark_end = AllocString();

	ln = 0;			/* number of lines output */
	BuildString((char *)0, line);

	while (ln < back && !eof) {
	    if (r <= 0) {
		if ((r = FileRead(fdLog, buf, REPLAYBUFFER)) < 0)
		    eof = 1;
		i = 0;
	    }

	    if (!eof)
		BuildStringChar(buf[i], line);

	    if (buf[i] == '\n' || eof) {
		int is_mark = 0;
		if (line->used > 0 && line->string[0] == '[') {
		    char dummy[4];
		    int j;
		    int i;
		    i = sscanf(line->string + 1,
			       "-- MARK -- %3c %3c %d %d:%d:%d %d]\r\n",
			       dummy, dummy, &j, &j, &j, &j, &j);
		    is_mark = (i == 7);
		}
		if (is_mark) {
		    if (mark_beg->used > 1) {
			BuildString((char *)0, mark_end);
			BuildString(line->string, mark_end);
		    } else
			BuildString(line->string, mark_beg);
		} else {
		    if (mark_beg->used > 1) {
			if (mark_end->used > 1) {
			    char *s;

			    /* output the start of the range, stopping at the ']' */
			    s = strrchr(mark_beg->string, ']');
			    if ((char *)0 != s)
				*s = '\000';
			    FileWrite(fdOut, FLAGTRUE, mark_beg->string,
				      -1);
			    FileWrite(fdOut, FLAGTRUE, " .. ", 4);

			    /* build the end string by removing the leading "[-- MARK -- "
			     * and replacing "]\r\n" on the end with " -- MARK --]\r\n"
			     */
			    s = strrchr(mark_end->string, ']');
			    if ((char *)0 != s)
				*s = '\000';
			    FileWrite(fdOut, FLAGTRUE,
				      mark_end->string +
				      sizeof("[-- MARK -- ") - 1, -1);
			    FileWrite(fdOut, FLAGFALSE, " -- MARK --]\r\n",
				      -1);
			} else {
			    FileWrite(fdOut, FLAGFALSE, mark_beg->string,
				      mark_beg->used - 1);
			}
			BuildString((char *)0, mark_beg);
			BuildString((char *)0, mark_end);
			ln++;
			if (ln >= back)
			    break;
		    }
		    FileWrite(fdOut, FLAGFALSE, line->string,
			      line->used - 1);
		    ln++;
		}
		BuildString((char *)0, line);
	    }

	    /* move the counters */
	    i++;
	    r--;
	}
	DestroyString(mark_end);
	DestroyString(mark_beg);
    }

  common_exit:

    if (line != (STRING *)0)
	DestroyString(line);
    if (buf != (char *)0)
	free(buf);
    if (fdLog != (CONSFILE *)0)
	FileClose(&fdLog);

#if HAVE_DMALLOC && DMALLOC_MARK_REPLAY
    CONDDEBUG((1, "Replay(): dmalloc / MarkReplay"));
    dmalloc_log_changed(dmallocMarkReplay, 1, 0, 1);
#endif
}


/* these bit tell us which parts of the Truth to tell the client	(ksb)
 */
#define WHEN_SPY	0x01
#define WHEN_ATTACH	0x02
#define WHEN_EXPERT	0x04	/* ZZZ no way to set his yet    */
#define WHEN_ALWAYS	0x40
#define IS_LIMITED	0x100

#define HALFLINE	40

typedef struct HLnode {
    int iwhen;
    char *actext;
} HELP;

static HELP aHLTable[] = {
    {WHEN_ALWAYS, ".       disconnect"},
    {WHEN_ALWAYS | IS_LIMITED, ";       move to another console"},
    {WHEN_ALWAYS, "a       attach read/write"},
    {WHEN_ALWAYS, "b       send broadcast message"},
    {WHEN_ATTACH, "c       toggle flow control"},
    {WHEN_ATTACH, "d       down a console"},
    {WHEN_ALWAYS, "e       change escape sequence"},
    {WHEN_ALWAYS, "f       force attach read/write"},
    {WHEN_ALWAYS, "g       group info"},
    {WHEN_ALWAYS, "i       information dump"},
    {WHEN_ATTACH, "L       toggle logging on/off"},
    {WHEN_ATTACH, "l?      break sequence list"},
    {WHEN_ATTACH, "l0      send break per config file"},
    {WHEN_ATTACH, "l1-9a-z send specific break sequence"},
    {WHEN_ALWAYS, "m       display message of the day"},
    {WHEN_ALWAYS, "n       write a note to the logfile"},
    {WHEN_ALWAYS, "o       (re)open the tty and log file"},
    {WHEN_ALWAYS, "p       playback the last %hu lines"},
    {WHEN_ALWAYS, "P       set number of playback lines"},
    {WHEN_ALWAYS, "r       replay the last %hu lines"},
    {WHEN_ALWAYS, "R       set number of replay lines"},
    {WHEN_ATTACH, "s       spy mode (read only)"},
    {WHEN_ALWAYS, "u       show host status"},
    {WHEN_ALWAYS, "v       show version info"},
    {WHEN_ALWAYS, "w       who is on this console"},
    {WHEN_ALWAYS, "x       show console baud info"},
    {WHEN_ALWAYS | IS_LIMITED, "z       suspend the connection"},
    {WHEN_ATTACH, "!       invoke task"},
    {WHEN_ATTACH | IS_LIMITED, "|       attach local command"},
    {WHEN_ALWAYS, "?       print this message"},
    {WHEN_ALWAYS, "<cr>    ignore/abort command"},
    {WHEN_ALWAYS, "^R      replay the last line"},
    {WHEN_ATTACH, "\\ooo    send character by octal code"},
};

/* list the commands we know for the user				(ksb)
 */
void
HelpUser(CONSCLIENT *pCL)
{
    int i, j, iCmp;
    static char
      acH1[] = "help]\r\n", acH2[] = "help spy mode]\r\n", acEoln[] =
	"\r\n";
    static STRING *acLine = (STRING *)0;

    if (acLine == (STRING *)0)
	acLine = AllocString();

    iCmp = WHEN_ALWAYS | WHEN_SPY;
    if (pCL->fwr) {
	FileWrite(pCL->fd, FLAGTRUE, acH1, sizeof(acH1) - 1);
	iCmp |= WHEN_ATTACH;
    } else {
	FileWrite(pCL->fd, FLAGTRUE, acH2, sizeof(acH2) - 1);
    }

    BuildString((char *)0, acLine);
    for (i = 0; i < sizeof(aHLTable) / sizeof(HELP); ++i) {
	char *text;

	if (aHLTable[i].iwhen & IS_LIMITED &&
	    ConsentUserOk(pLUList, pCL->username->string) == 1)
	    continue;

	if (0 == (aHLTable[i].iwhen & iCmp))
	    continue;

	text = aHLTable[i].actext;
	if (text[0] == 'p') {
	    BuildTmpString((char *)0);
	    text = BuildTmpStringPrint(text, pCL->playback);
	} else if (text[0] == 'r') {
	    BuildTmpString((char *)0);
	    text = BuildTmpStringPrint(text, pCL->replay);
	}

	if (acLine->used != 0) {	/* second part of line */
	    if (strlen(text) < HALFLINE) {
		for (j = acLine->used; j <= HALFLINE; ++j) {
		    BuildStringChar(' ', acLine);
		}
		BuildString(text, acLine);
		BuildString(acEoln, acLine);
		FileWrite(pCL->fd, FLAGTRUE, acLine->string,
			  acLine->used - 1);
		BuildString((char *)0, acLine);
		continue;
	    } else {
		BuildString(acEoln, acLine);
		FileWrite(pCL->fd, FLAGTRUE, acLine->string,
			  acLine->used - 1);
		BuildString((char *)0, acLine);
	    }
	}
	if (acLine->used == 0) {	/* at new line */
	    BuildStringChar(' ', acLine);
	    BuildString(text, acLine);
	    if (acLine->used > HALFLINE) {
		BuildString(acEoln, acLine);
		FileWrite(pCL->fd, FLAGTRUE, acLine->string,
			  acLine->used - 1);
		BuildString((char *)0, acLine);
	    }
	}
    }
    if (acLine->used != 0) {
	BuildString(acEoln, acLine);
	FileWrite(pCL->fd, FLAGTRUE, acLine->string, acLine->used - 1);
    }
    FileWrite(pCL->fd, FLAGFALSE, (char *)0, 0);
}

int
ClientAccessOk(CONSCLIENT *pCL)
{
    char *peername = (char *)0;
    int retval = 1;

#if USE_IPV6 || !USE_UNIX_DOMAIN_SOCKETS
    socklen_t so;
    int cfd;
# if USE_IPV6
    int error;
    char addr[NI_MAXHOST];
# endif
    SOCKADDR_STYPE in_port;
    int getpeer = -1;

    cfd = FileFDNum(pCL->fd);
    pCL->caccess = 'r';
# if defined(USE_LIBWRAP)
    {
	struct request_info request;
	CONDDEBUG((1, "ClientAccessOk(): doing tcpwrappers check"));
	request_init(&request, RQ_DAEMON, progname, RQ_FILE, cfd, 0);
	fromhost(&request);
	if (!hosts_access(&request)) {
	    FileWrite(pCL->fd, FLAGFALSE,
		      "access from your host refused\r\n", -1);
	    retval = 0;
	    goto setpeer;
	}
    }
# endif

    so = sizeof(in_port);
    if (-1 ==
	(getpeer = getpeername(cfd, (struct sockaddr *)&in_port, &so))) {
	FileWrite(pCL->fd, FLAGFALSE, "getpeername failed\r\n", -1);
	retval = 0;
	goto setpeer;
    }
    pCL->caccess = AccType(
# if USE_IPV6
			      &in_port,
# else
			      &in_port.sin_addr,
# endif
			      &peername);
    if (pCL->caccess == 'r') {
	FileWrite(pCL->fd, FLAGFALSE, "access from your host refused\r\n",
		  -1);
	retval = 0;
    }
  setpeer:
#else
    struct in_addr addr;

# if HAVE_INET_ATON
    inet_aton("127.0.0.1", &addr);
# else
    addr.s_addr = inet_addr("127.0.0.1");
# endif
    pCL->caccess = AccType(&addr, &peername);
    if (pCL->caccess == 'r') {
	FileWrite(pCL->fd, FLAGFALSE, "access from your host refused\r\n",
		  -1);
	retval = 0;
    }
#endif

    if (pCL->peername != (STRING *)0) {
	BuildString((char *)0, pCL->peername);
	if (peername != (char *)0)
	    BuildString(peername, pCL->peername);
#if USE_IPV6
	else if (getpeer != -1) {
	    error =
		getnameinfo((struct sockaddr *)&in_port, so, addr,
			    sizeof(addr), NULL, 0, NI_NUMERICHOST);
	    if (error) {
		FileWrite(pCL->fd, FLAGFALSE, "getnameinfo failed\r\n",
			  -1);
		Error("ClientAccessOk(): gatenameinfo: %s",
		      gai_strerror(error));
		retval = 0;
	    }

	    BuildString(addr, pCL->peername);
	} else
	    BuildString("<unknown>", pCL->peername);
#elif USE_UNIX_DOMAIN_SOCKETS
	else
	    BuildString("127.0.0.1", pCL->peername);
#else
	else if (getpeer != -1)
	    BuildString(inet_ntoa(in_port.sin_addr), pCL->peername);
	else
	    BuildString("<unknown>", pCL->peername);
#endif
    }
    if (peername != (char *)0)
	free(peername);
    return retval;
}