capture.c 21.1 KB
Newer Older
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
/* 
 * File:	capture.c
 * Description: 
 * Author:	Leigh Stoller
 * 		Computer Science Dept.
 * 		University of Utah
 * Date:	27-Jul-92
 *
 * (c) Copyright 1992, 2000, University of Utah, all rights reserved.
 */

/*
 * Testbed note:  This code has developed over the last several
 * years in RCS.  This is an import of the current version of
 * capture from the /usr/src/utah RCS repository into the testbed,
 * with some new hacks to port it to Linux.
 *
 * - dga, 10/10/2000
 */

/*
 * A LITTLE hack to record output from a tty device to a file, and still
 * have it available to tip using a pty/tty pair.
 */
	
#include <sys/param.h>

#include <stdio.h>
#include <ctype.h>
#include <strings.h>
#include <syslog.h>
#ifdef HPBSD
#include <sgtty.h>
#else
#include <termios.h>
#endif
#include <errno.h>

#include <sys/param.h>
#include <sys/file.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <signal.h>
#include <sys/ioctl.h>
#include <sys/termios.h>
47
48
49
50
51
52
#ifdef USESOCKETS
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#endif
53
54
55
56
57

#define geterr(e)	strerror(e)

void quit(int);
void reinit(int);
58
void newrun(int);
59
void terminate(int);
60
61
62
63
64
65
void cleanup(void);
void capture();
void usage();

#ifdef __linux__
#define _POSIX_VDISABLE '\0'
66
67
68
69
70
#define revoke(tty)	(0)
#endif

#ifdef HPBSD
#define TWOPROCESS	/* historic? */
71
72
73
74
75
76
77
78
79
80
81
82
83
#endif

/*
 *  Configurable things.
 */
#define DEVPATH		"/dev"
#ifdef HPBSD
#define LOGPATH		"/usr/adm/tiplogs"
#else
#define LOGPATH		"/var/log/tiplogs"
#endif
#define PIDNAME		"%s/%s.pid"
#define LOGNAME		"%s/%s.log"
84
#define RUNNAME		"%s/%s.run"
85
#define TTYNAME		"%s/tip/%s"
86
#define PTYNAME		"%s/tip/%s-pty"
87
#define ACLNAME		"%s/tip/%s.acl"
88
#define DEVNAME		"%s/%s"
89
#define BUFSIZE		4096
90
#define DROP_THRESH	(32*1024)
91
92
93
94

char 	*Progname;
char 	*Pidname;
char	*Logname;
95
char	*Runname;
96
char	*Ttyname;
97
98
99
char	*Ptyname;
char	*Devname;
char	*Machine;
100
101
int	logfd, runfd, devfd, ptyfd;
int	hwflow = 0, speed = B9600, debug = 0, runfile = 0;
102
103
104
105
106
#ifdef  USESOCKETS
int	sockfd, tipactive, portnum;
struct sockaddr_in tipclient;
int	ACLbits[3];
#endif
107
108
109
110
111
112
113
114
115
116

int
main(argc, argv)
	int argc;
	char **argv;
{
	char strbuf[MAXPATHLEN], *newstr();
	int flags, op, i;
	extern int optind;
	extern char *optarg;
117
118
119
#ifdef  USESOCKETS
	struct sockaddr_in name;
#endif
120
121
122

	Progname = (Progname = rindex(argv[0], '/')) ? ++Progname : *argv;

123
	while ((op = getopt(argc, argv, "rds:H")) != EOF)
124
125
126
127
128
129
		switch (op) {

		case 'H':
			++hwflow;
			break;

130
131
132
133
134
135
136
137
		case 'd':
			debug++;
			break;

		case 'r':
			runfile++;
			break;

138
139
140
141
142
143
144
145
146
147
148
149
150
		case 's':
			if ((i = atoi(optarg)) == 0 ||
			    (speed = val2speed(i)) == 0)
				usage();
			break;
		}

	argc -= optind;
	argv += optind;

	if (argc != 2)
		usage();

151
152
153
154
155
#ifndef HPBSD
	if (!debug)
		(void)daemon(0, 0);
#endif

156
157
158
159
160
161
	Machine = argv[0];

	(void) sprintf(strbuf, PIDNAME, LOGPATH, argv[0]);
	Pidname = newstr(strbuf);
	(void) sprintf(strbuf, LOGNAME, LOGPATH, argv[0]);
	Logname = newstr(strbuf);
162
163
	(void) sprintf(strbuf, RUNNAME, LOGPATH, argv[0]);
	Runname = newstr(strbuf);
164
165
	(void) sprintf(strbuf, TTYNAME, DEVPATH, argv[0]);
	Ttyname = newstr(strbuf);
166
167
168
169
170
171
172
173
174
175
176
	(void) sprintf(strbuf, PTYNAME, DEVPATH, argv[0]);
	Ptyname = newstr(strbuf);
	(void) sprintf(strbuf, DEVNAME, DEVPATH, argv[1]);
	Devname = newstr(strbuf);

	openlog(Progname, LOG_PID, LOG_USER);
	dolog(LOG_NOTICE, "starting");

	signal(SIGINT, quit);
	signal(SIGTERM, quit);
	signal(SIGHUP, reinit);
177
178
	if (runfile)
		signal(SIGUSR1, newrun);
179
	signal(SIGUSR2, terminate);
180
181

	/*
182
	 * Open up run/log file, console tty, and controlling pty.
183
184
	 */
	if ((logfd = open(Logname, O_WRONLY|O_CREAT|O_APPEND, 0666)) < 0)
185
		die("%s: open: %s", Logname, geterr(errno));
186
	if (chmod(Logname, 0640) < 0)
187
		die("%s: chmod: %s", Logname, geterr(errno));
188
189
190

	if (runfile) {
		if ((runfd = open(Runname,O_WRONLY|O_CREAT|O_APPEND,0666)) < 0)
191
			die("%s: open: %s", Runname, geterr(errno));
192
		if (chmod(Runname, 0640) < 0)
193
			die("%s: chmod: %s", Runname, geterr(errno));
194
	}
195
196
197
198
199
200
201
202
203
204
205
206
#ifdef  USESOCKETS
	/*
	 * Create and bind our socket.
	 */
	sockfd = socket(AF_INET, SOCK_STREAM, 0);
	if (sockfd < 0)
		die("socket(): opening stream socket: %s", geterr(errno));

	i = 1;
	if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR,
		       (char *)&i, sizeof(i)) < 0)
		die("setsockopt(): SO_REUSEADDR: %s", geterr(errno));
207
	
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
	/* Create wildcard name. */
	name.sin_family = AF_INET;
	name.sin_addr.s_addr = INADDR_ANY;
	name.sin_port = 0;
	if (bind(sockfd, (struct sockaddr *) &name, sizeof(name)))
		die("bind(): binding stream socket: %s", geterr(errno));

	/* Find assigned port value and print it out. */
	i = sizeof(name);
	if (getsockname(sockfd, (struct sockaddr *)&name, &i))
		die("getsockname(): %s", geterr(errno));
	portnum = ntohs(name.sin_port);

	if (listen(sockfd, 1) < 0)
		die("listen(): listening on socket: %s", geterr(errno));

	genaclfile();

	dolog(LOG_NOTICE, "listening on TCP port %d", portnum);
#else
228
	if ((ptyfd = open(Ptyname, O_RDWR, 0666)) < 0)
229
		die("%s: open: %s", Ptyname, geterr(errno));
230
#endif
231
	if ((devfd = open(Devname, O_RDWR|O_NONBLOCK, 0666)) < 0)
232
		die("%s: open: %s", Devname, geterr(errno));
233
234

	if (ioctl(devfd, TIOCEXCL, 0) < 0)
235
		warn("TIOCEXCL %s: %s", Devname, geterr(errno));
236
237
238
239
240
241
242
243
244
245

	writepid();
	rawmode(speed);

	capture();

	cleanup();
	exit(0);
}

246
247
248
#ifdef TWOPROCESS
int	pid;

249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
void
capture()
{
	flags = FNDELAY;
	(void) fcntl(ptyfd, F_SETFL, &flags);

	if (pid = fork())
		in();
	else
		out();
}

/*
 * Loop reading from the console device, writing data to log file and
 * to the pty for tip to pick up.
 */
in()
{
267
	char buf[BUFSIZE];
268
269
270
	int cc, omask;
	
	while (1) {
271
		if ((cc = read(devfd, buf, BUFSIZE)) < 0) {
272
273
274
			if ((errno == EWOULDBLOCK) || (errno == EINTR))
				continue;
			else
275
				die("%s: read: %s", Devname, geterr(errno));
276
		}
277
278
		omask = sigblock(sigmask(SIGHUP)|sigmask(SIGTERM)|
				 sigmask(SIGUSR1)|sigmask(SIGUSR2));
279
280

		if (write(logfd, buf, cc) < 0)
281
			die("%s: write: %s", Logname, geterr(errno));
282

283
284
		if (runfile) {
			if (write(runfd, buf, cc) < 0)
285
				die("%s: write: %s", Runname, geterr(errno));
286
287
		}

288
289
		if (write(ptyfd, buf, cc) < 0) {
			if ((errno != EIO) && (errno != EWOULDBLOCK))
290
				die("%s: write: %s", Ptyname, geterr(errno));
291
292
293
294
295
296
297
298
299
300
		}
		(void) sigsetmask(omask);
	}
}

/*
 * Loop reading input from pty (tip), and send off to the console device.
 */
out()
{
301
	char buf[BUFSIZE];
302
303
304
305
306
307
308
	int cc, omask;
	struct timeval timeout;

	timeout.tv_sec  = 0;
	timeout.tv_usec = 100000;
	
	while (1) {
309
		omask = sigblock(SIGUSR2);
310
		if ((cc = read(ptyfd, buf, BUFSIZE)) < 0) {
311
			(void) sigsetmask(omask);
312
313
314
315
316
317
			if ((errno == EIO) || (errno == EWOULDBLOCK) ||
			    (errno == EINTR)) {
				select(0, 0, 0, 0, &timeout);
				continue;
			}
			else
318
				die("%s: read: %s", Ptyname, geterr(errno));
319
		}
320
321
322
323
		(void) sigsetmask(omask);

		omask = sigblock(sigmask(SIGHUP)|sigmask(SIGTERM)|
				 sigmask(SIGUSR1)|sigmask(SIGUSR2));
324
325

		if (write(devfd, buf, cc) < 0)
326
			die("%s: write: %s", Devname, geterr(errno));
327
328
329
330
331
		
		(void) sigsetmask(omask);
	}
}
#else
332
333
static fd_set	sfds;
static int	fdcount;
334
335
336
void
capture()
{
337
338
	fd_set fds;
	int i, cc, lcc, omask;
339
	char buf[BUFSIZE];
340
	struct timeval timeout;
341
342
343
344
#ifdef LOG_DROPS
	int drop_topty_chars = 0;
	int drop_todev_chars = 0;
#endif
345
346

	timeout.tv_sec  = 0;
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
	timeout.tv_usec = 10000;	/* ~115 chars at 115.2 kbaud */

	/*
	 * XXX for now we make both directions non-blocking.  This is a
	 * quick hack to achieve the goal that capture never block
	 * uninterruptably for long periods of time (use threads).
	 * This has the unfortunate side-effect that we may drop chars
	 * from the perspective of the user (use threads).  A more exotic
	 * solution would be to poll the readiness of output (use threads)
	 * as well as input and not read from one end unless we can write
	 * the other (use threads).
	 *
	 * I keep thinking (use threads) that there is a better way to do
	 * this (use threads).  Hmm...
	 */
Mike Hibler's avatar
Mike Hibler committed
362
	if (fcntl(devfd, F_SETFL, O_NONBLOCK) < 0)
363
		die("%s: fcntl(O_NONBLOCK): %s", Devname, geterr(errno));
364
#ifndef USESOCKETS
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
	/*
	 * It gets better!
	 * In FreeBSD 4.0 and beyond, the fcntl fails because the slave
	 * side is not open even though the only effect of this call is
	 * to set the file description's FNONBLOCK flag (i.e. the pty and
	 * tty code do nothing additional).  Turns out that we could use
	 * ioctl instead of fcntl to set the flag.  The call will still
	 * fail, but the flag will be left set.  Rather than rely on that
	 * dubious behavior, I temporarily open the slave, do the fcntl
	 * and close the slave again.
	 */
#ifdef __FreeBSD__
	if ((n = open(Ttyname, O_RDONLY, 0666)) < 0)
		die("%s: open: %s", Ttyname, geterr(errno));
#endif
	if (fcntl(ptyfd, F_SETFL, O_NONBLOCK) < 0)
		die("%s: fcntl(O_NONBLOCK): %s", Ptyname, geterr(errno));
#ifdef __FreeBSD__
	close(n);
#endif
385
#endif /* USESOCKETS */
386
387
388

	FD_ZERO(&sfds);
	FD_SET(devfd, &sfds);
389
390
391
392
393
394
395
396
	fdcount = devfd;
#ifdef  USESOCKETS
	if (devfd < sockfd)
		fdcount = sockfd;
	FD_SET(sockfd, &sfds);
#else
	if (devfd < ptyfd)
		fdcount = ptyfd;
397
	FD_SET(ptyfd, &sfds);
398
399
400
401
#endif	/* USESOCKETS */

	fdcount++;

402
	for (;;) {
403
404
405
406
407
408
409
410
#ifdef LOG_DROPS
		if (drop_topty_chars >= DROP_THRESH) {
			warn("%d dev -> pty chars dropped", drop_topty_chars);
			drop_topty_chars = 0;
		}
		if (drop_todev_chars >= DROP_THRESH) {
			warn("%d pty -> dev chars dropped", drop_todev_chars);
			drop_todev_chars = 0;
411
		}
412
#endif
413
		fds = sfds;
414
		i = select(fdcount, &fds, NULL, NULL, NULL);
415
416
417
418
419
		if (i < 0) {
			if (errno == EINTR) {
				warn("input select interrupted, continuing");
				continue;
			}
420
			die("%s: select: %s", Devname, geterr(errno));
421
		}
422
		if (i == 0) {
423
			warn("No fds ready!");
424
425
426
			sleep(1);
			continue;
		}
427
428
429
430
431
432
#ifdef	USESOCKETS
		if (FD_ISSET(sockfd, &fds)) {
			if (clientconnect())
				continue;
		}
#endif	/* USESOCKETS */
433
434
435
436
		if (FD_ISSET(devfd, &fds)) {
			errno = 0;
			cc = read(devfd, buf, sizeof(buf));
			if (cc < 0)
437
				die("%s: read: %s", Devname, geterr(errno));
438
			if (cc == 0)
439
				die("%s: read: EOF", Devname);
440
441
			errno = 0;

442
443
			omask = sigblock(sigmask(SIGHUP)|sigmask(SIGTERM)|
					 sigmask(SIGUSR1)|sigmask(SIGUSR2));
444
445
446
447
#ifdef	USESOCKETS
			if (!tipactive)
				goto dropped;
#endif
448
449
450
			for (lcc = 0; lcc < cc; lcc += i) {
				i = write(ptyfd, &buf[lcc], cc-lcc);
				if (i < 0) {
451
452
453
454
455
456
457
					/*
					 * Either tip is blocked (^S) or
					 * not running (the latter should
					 * return EIO but doesn't due to a
					 * pty bug).  Note that we have
					 * dropped some chars.
					 */
458
					if (errno == EIO || errno == EAGAIN) {
459
460
#ifdef LOG_DROPS
						drop_topty_chars += (cc-lcc);
461
462
463
#endif
						goto dropped;
					}
464
					die("%s: write: %s",
465
466
					    Ptyname, geterr(errno));
				}
467
468
469
470
				if (i == 0) {
#ifdef	USESOCKETS
					goto disconnected;
#else
471
					die("%s: write: zero-length", Ptyname);
472
473
#endif
				}
474
475
476
477
			}
dropped:
			i = write(logfd, buf, cc);
			if (i < 0)
478
				die("%s: write: %s", Logname, geterr(errno));
479
			if (i != cc)
480
				die("%s: write: incomplete", Logname);
481
482
483
			if (runfile) {
				i = write(runfd, buf, cc);
				if (i < 0)
484
					die("%s: write: %s",
485
486
					    Runname, geterr(errno));
				if (i != cc)
487
					die("%s: write: incomplete", Runname);
488
			}
489
490
491
492
			(void) sigsetmask(omask);

		}
		if (FD_ISSET(ptyfd, &fds)) {
493
			omask = sigblock(sigmask(SIGUSR2));
494
495
			errno = 0;
			cc = read(ptyfd, buf, sizeof(buf), 0);
496
			(void) sigsetmask(omask);
497
498
499
500
			if (cc < 0) {
				/* XXX commonly observed */
				if (errno == EIO || errno == EAGAIN)
					continue;
501
				die("%s: read: %s", Ptyname, geterr(errno));
502
503
			}
			if (cc == 0) {
504
505
506
507
508
509
510
511
512
513
514
515
#ifdef	USESOCKETS
			disconnected:
				/*
				 * Other end disconnected.
				 */
				dolog(LOG_INFO, "%s disconnecting",
				      inet_ntoa(tipclient.sin_addr));
				FD_CLR(ptyfd, &sfds);
				close(ptyfd);
				tipactive = 0;
				continue;
#else				
516
517
				select(0, 0, 0, 0, &timeout);
				continue;
518
#endif
519
520
521
			}
			errno = 0;

522
523
			omask = sigblock(sigmask(SIGHUP)|sigmask(SIGTERM)|
					 sigmask(SIGUSR1));
524
525
526
			for (lcc = 0; lcc < cc; lcc += i) {
				i = write(devfd, &buf[lcc], cc-lcc);
				if (i < 0) {
527
528
529
530
					/*
					 * Device backed up (or FUBARed)
					 * Note that we dropped some chars.
					 */
531
					if (errno == EAGAIN) {
532
533
534
#ifdef LOG_DROPS
						drop_todev_chars += (cc-lcc);
#endif
535
536
						goto dropped2;
					}
537
					die("%s: write: %s",
538
539
540
					    Devname, geterr(errno));
				}
				if (i == 0)
541
					die("%s: write: zero-length", Devname);
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
			}
dropped2:
			(void) sigsetmask(omask);

		}
	}
}
#endif

/*
 * SIGHUP means we want to close the old log file (because it has probably
 * been moved) and start a new version of it.
 */
void
reinit(int sig)
{
	/*
	 * We know that the any pending write to the log file completed
	 * because we blocked SIGHUP during the write.
	 */
	close(logfd);
	
	if ((logfd = open(Logname, O_WRONLY|O_CREAT|O_APPEND, 0666)) < 0)
565
		die("%s: open: %s", Logname, geterr(errno));
566
	if (chmod(Logname, 0640) < 0)
567
		die("%s: chmod: %s", Logname, geterr(errno));
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
	
	dolog(LOG_NOTICE, "new log started");

	if (runfile)
		newrun(sig);
}

/*
 * SIGUSR1 means we want to close the old run file (because it has probably
 * been moved) and start a new version of it.
 */
void
newrun(int sig)
{
	/*
	 * We know that the any pending write to the log file completed
	 * because we blocked SIGUSR1 during the write.
	 */
	close(runfd);
587

588
	if ((runfd = open(Runname, O_WRONLY|O_CREAT|O_APPEND, 0666)) < 0)
589
		die("%s: open: %s", Runname, geterr(errno));
590
	if (chmod(Runname, 0640) < 0)
591
		die("%s: chmod: %s", Runname, geterr(errno));
592
593
594
	
	dolog(LOG_NOTICE, "new run started");
}
595

596
597
/*
 * SIGUSR2 means we want to revoke the other side of the pty to close the
598
599
600
 * tip down gracefully.  We flush all input/output pending on the pty,
 * do a revoke on the tty and then close and reopen the pty just to make
 * sure everyone is gone.
601
602
 */
void
603
terminate(int sig)
604
{
605
606
607
608
609
610
611
612
613
614
615
616
617
618
#ifdef	USESOCKETS
	genaclfile();

	if (!tipactive)
		return;

	shutdown(ptyfd, SHUT_RDWR);
	close(ptyfd);
	FD_CLR(ptyfd, &sfds);
	ptyfd = 0;
	tipactive = 0;

	dolog(LOG_INFO, "%s shutdown", inet_ntoa(tipclient.sin_addr));
#else
619
620
621
622
623
624
	int ofd = ptyfd;
	
	/*
	 * We know that the any pending access to the pty completed
	 * because we blocked SIGUSR2 during the operation.
	 */
625
626
627
628
629
630
631
632
#ifdef HPBSD
	int zero = 0;
	ioctl(ptyfd, TIOCFLUSH, &zero);
#else
	tcflush(ptyfd, TCIOFLUSH);
#endif
	if (revoke(Ttyname) < 0)
		dolog(LOG_WARNING, "could not revoke access to tty");
633
634
635
	close(ptyfd);
	
	if ((ptyfd = open(Ptyname, O_RDWR, 0666)) < 0)
636
		die("%s: open: %s", Ptyname, geterr(errno));
637
638
639
640
641
642
643
644

	/* XXX so we don't have to recompute the select mask */
	if (ptyfd != ofd) {
		dup2(ptyfd, ofd);
		close(ptyfd);
		ptyfd = ofd;
	}

645
646
647
648
649
#ifdef __FreeBSD__
	/* see explanation in capture() above */
	if ((ofd = open(Ttyname, O_RDONLY, 0666)) < 0)
		die("%s: open: %s", Ttyname, geterr(errno));
#endif
650
	if (fcntl(ptyfd, F_SETFL, O_NONBLOCK) < 0)
651
652
653
654
		die("%s: fcntl(O_NONBLOCK): %s", Ptyname, geterr(errno));
#ifdef __FreeBSD__
	close(ofd);
#endif
655
656
	
	dolog(LOG_NOTICE, "pty reset");
657
#endif	/* USESOCKETS */
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
}

/*
 *  Display proper usage / error message and exit.
 */
void
usage()
{
	fprintf(stderr, "usage: %s machine tty\n", Progname);
	exit(1);
}

warn(format, arg0, arg1, arg2, arg3)
	char *format, *arg0, *arg1, *arg2, *arg3;
{
673
	char msgbuf[BUFSIZE];
674

675
	snprintf(msgbuf, BUFSIZE, format, arg0, arg1, arg2, arg3);
676
	dolog(LOG_WARNING, msgbuf);
677
678
679
680
681
}

die(format, arg0, arg1, arg2, arg3)
	char *format, *arg0, *arg1, *arg2, *arg3;
{
682
	char msgbuf[BUFSIZE];
683

684
	snprintf(msgbuf, BUFSIZE, format, arg0, arg1, arg2, arg3);
685
686
687
688
	dolog(LOG_ERR, msgbuf);
	quit(0);
}

689
690
691
dolog(level, format, arg0, arg1, arg2, arg3)
	int level;
	char *format, *arg0, *arg1, *arg2, *arg3;
692
{
693
	char msgbuf[BUFSIZE];
694

695
696
697
698
699
700
	snprintf(msgbuf, BUFSIZE, format, arg0, arg1, arg2, arg3);
	if (debug) {
		fprintf(stderr, "%s - %s\n", Machine, msgbuf);
		fflush(stderr);
	}
	syslog(level, "%s - %s\n", Machine, msgbuf);
701
702
703
704
705
706
707
708
709
710
711
712
}

void
quit(int sig)
{
	cleanup();
	exit(1);
}

void
cleanup()
{
713
714
	dolog(LOG_NOTICE, "exiting");
#ifdef TWOPROCESS
715
716
	if (pid)
		(void) kill(pid, SIGTERM);
717
#endif
718
719
720
721
722
723
724
725
726
727
	(void) unlink(Pidname);
}

char *
newstr(str)
	char *str;
{
	char *malloc();
	register char *np;

728
729
	if ((np = malloc((unsigned) strlen(str) + 1)) == NULL)
		die("malloc: out of memory");
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759

	return(strcpy(np, str));
}

#if 0
char *
geterr(errindx)
int errindx;
{
	extern int sys_nerr;
	extern char *sys_errlist[];
	char syserr[25];

	if (errindx < sys_nerr)
		return(sys_errlist[errindx]);

	(void) sprintf(syserr, "unknown error (%d)", errindx);
	return(syserr);
}
#endif

/*
 * Open up PID file and write our pid into it.
 */
writepid()
{
	int fd;
	char buf[8];
	
	if ((fd = open(Pidname, O_WRONLY|O_CREAT|O_TRUNC, 0666)) < 0)
760
		die("%s: open: %s", Pidname, geterr(errno));
761
762

	if (chmod(Pidname, 0644) < 0)
763
		die("%s: chmod: %s", Pidname, geterr(errno));
764
765
766
767
	
	(void) sprintf(buf, "%d\n", getpid());
	
	if (write(fd, buf, strlen(buf)) < 0)
768
		die("%s: write: %s", Pidname, geterr(errno));
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
	
	(void) close(fd);
}

/*
 * Put the console line into raw mode.
 */
rawmode(speed)
int speed;
{
#ifdef HPBSD
	struct sgttyb sgbuf;
	int bits;
	
	if (ioctl(devfd, TIOCGETP, (char *)&sgbuf) < 0)
784
		die("%s: ioctl(TIOCGETP): %s", Device, geterr(errno));
785
786
787
788
	sgbuf.sg_ispeed = sgbuf.sg_ospeed = speed;
	sgbuf.sg_flags = RAW|TANDEM;
	bits = LDECCTQ;
	if (ioctl(devfd, TIOCSETP, (char *)&sgbuf) < 0)
789
		die("%s: ioctl(TIOCSETP): %s", Device, geterr(errno));
790
	if (ioctl(devfd, TIOCLBIS, (char *)&bits) < 0)
791
		die("%s: ioctl(TIOCLBIS): %s", Device, geterr(errno));
792
793
794
795
#else
	struct termios t;

	if (tcgetattr(devfd, &t) < 0)
796
		die("%s: tcgetattr: %s", Devname, geterr(errno));
797
798
799
800
801
802
803
804
805
806
807
808
	(void) cfsetispeed(&t, speed);
	(void) cfsetospeed(&t, speed);
	cfmakeraw(&t);
	t.c_cflag |= CLOCAL;
	if (hwflow)
#ifdef __linux__
	        t.c_cflag |= CRTSCTS;
#else
		t.c_cflag |= CCTS_OFLOW | CRTS_IFLOW;
#endif
	t.c_cc[VSTART] = t.c_cc[VSTOP] = _POSIX_VDISABLE;
	if (tcsetattr(devfd, TCSAFLUSH, &t) < 0)
809
		die("%s: tcsetattr: %s", Devname, geterr(errno));
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
#endif
}

/*
 * From kgdbtunnel
 */
static struct speeds {
	int speed;		/* symbolic speed */
	int val;		/* numeric value */
} speeds[] = {
#ifdef B50
	{ B50,	50 },
#endif
#ifdef B75
	{ B75,	75 },
#endif
#ifdef B110
	{ B110,	110 },
#endif
#ifdef B134
	{ B134,	134 },
#endif
#ifdef B150
	{ B150,	150 },
#endif
#ifdef B200
	{ B200,	200 },
#endif
#ifdef B300
	{ B300,	300 },
#endif
#ifdef B600
	{ B600,	600 },
#endif
#ifdef B1200
	{ B1200, 1200 },
#endif
#ifdef B1800
	{ B1800, 1800 },
#endif
#ifdef B2400
	{ B2400, 2400 },
#endif
#ifdef B4800
	{ B4800, 4800 },
#endif
#ifdef B7200
	{ B7200, 7200 },
#endif
#ifdef B9600
	{ B9600, 9600 },
#endif
#ifdef B14400
	{ B14400, 14400 },
#endif
#ifdef B19200
	{ B19200, 19200 },
#endif
#ifdef B38400
	{ B38400, 38400 },
#endif
#ifdef B28800
	{ B28800, 28800 },
#endif
#ifdef B57600
	{ B57600, 57600 },
#endif
#ifdef B76800
	{ B76800, 76800 },
#endif
#ifdef B115200
	{ B115200, 115200 },
#endif
#ifdef B230400
	{ B230400, 230400 },
#endif
};
#define NSPEEDS (sizeof(speeds) / sizeof(speeds[0]))

int
val2speed(val)
	register int val;
{
	register int n;
	register struct speeds *sp;

	for (sp = speeds, n = NSPEEDS; n > 0; ++sp, --n)
		if (val == sp->val)
			return (sp->speed);

	return (0);
}
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998

#ifdef USESOCKETS
int
clientconnect()
{
	int	length = sizeof(tipclient);
	int	newfd;
	char	bits[sizeof(ACLbits)];

	newfd = accept(sockfd, (struct sockaddr *)&tipclient, &length);
	if (newfd < 0)
		die("accept(): accepting new client: %s", geterr(errno));

	/*
	 * Is there a better way to do this? I suppose we
	 * could shut the main socket down, and recreate
	 * it later when the client disconnects, but that
	 * sounds horribly brutish!
	 */
	if (tipactive) {
		close(newfd);
		dolog(LOG_NOTICE, "%s connecting, but tip is active",
		      inet_ntoa(tipclient.sin_addr));
		return 1;
	}
	ptyfd = newfd;

	/*
	 * Read the first part to verify the ACLbits. We must get the proper
	 * bits or this is not a valid tip connection.
	 */
	if (read(ptyfd, bits, sizeof(bits)) != sizeof(bits)) {
		close(ptyfd);
		dolog(LOG_NOTICE, "%s connecting, error reading aclbits",
		      inet_ntoa(tipclient.sin_addr));
		return 1;
	}
	if (bcmp(bits, ACLbits, sizeof(ACLbits))) {
		close(ptyfd);
		dolog(LOG_NOTICE, "%s connecting, aclbits do not match",
		      inet_ntoa(tipclient.sin_addr));
		return 1;
	}
	
	/*
	 * See Mike comments (use threads) above.
	 */
	if (fcntl(ptyfd, F_SETFL, O_NONBLOCK) < 0)
		die("fcntl(O_NONBLOCK): %s", geterr(errno));

	FD_SET(ptyfd, &sfds);
	if (ptyfd >= fdcount) {
		fdcount = ptyfd;
		fdcount++;
	}
	tipactive = 1;

	dolog(LOG_INFO, "%s connecting", inet_ntoa(tipclient.sin_addr));
	return 0;
}

/*
 * Simple access permission system for now.
 * Write out an acl file, which has the port number and "key". If
 * the user can read this file, then he knows what port to connect on
 * and what magic string to send. We eventually need to extend this to a
 * tipserver of some kind, that talks to the database and does the
 * permission checks. 
 */
genaclfile()
{
	char	aclname[BUFSIZ], buf[BUFSIZ];
	int	fd;

	if ((fd = open("/dev/urandom", O_RDONLY, 0666)) < 0)
		die("/dev/random: open: %s", geterr(errno));

	if (read(fd, ACLbits, sizeof(ACLbits)) != sizeof(ACLbits)) 
		die("/dev/random: read: %s", geterr(errno));
	close(fd);

	(void) sprintf(aclname, ACLNAME, DEVPATH, Machine);

	if ((fd = open(aclname, O_WRONLY|O_CREAT|O_TRUNC, 0666)) < 0)
		die("%s: open: %s", aclname, geterr(errno));

	(void) sprintf(buf, "%d\n0x%x 0x%x 0x%x\n", portnum,
		       ACLbits[0], ACLbits[1], ACLbits[2]);
	
	if (write(fd, buf, strlen(buf)) != strlen(buf))
		die("%s: write: %s", aclname, geterr(errno));
	close(fd);

	if (chmod(aclname, 0640) < 0)
		die("%s: chmod: %s", aclname, geterr(errno));
}
#endif