xref: /freebsd/libexec/getty/subr.c (revision dbd5678d)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1983, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #ifndef lint
33 #if 0
34 static char sccsid[] = "@(#)from: subr.c	8.1 (Berkeley) 6/4/93";
35 #endif
36 static const char rcsid[] =
37     "$FreeBSD$";
38 #endif /* not lint */
39 
40 /*
41  * Melbourne getty.
42  */
43 #include <sys/ioctl.h>
44 #include <sys/param.h>
45 #include <sys/time.h>
46 
47 #include <poll.h>
48 #include <regex.h>
49 #include <stdlib.h>
50 #include <string.h>
51 #include <syslog.h>
52 #include <termios.h>
53 #include <unistd.h>
54 
55 #include "gettytab.h"
56 #include "pathnames.h"
57 #include "extern.h"
58 
59 /*
60  * Get a table entry.
61  */
62 void
63 gettable(const char *name)
64 {
65 	char *buf = NULL;
66 	struct gettystrs *sp;
67 	struct gettynums *np;
68 	struct gettyflags *fp;
69 	long n;
70 	int l;
71 	char *p;
72 	static char path_gettytab[PATH_MAX];
73 	char *dba[2];
74 
75 	static int firsttime = 1;
76 
77 	strlcpy(path_gettytab, _PATH_GETTYTAB, sizeof(path_gettytab));
78 	dba[0] = path_gettytab;
79 	dba[1] = NULL;
80 
81 	if (firsttime) {
82 		/*
83 		 * we need to strdup() anything in the strings array
84 		 * initially in order to simplify things later
85 		 */
86 		for (sp = gettystrs; sp->field; sp++)
87 			if (sp->value != NULL) {
88 				/* handle these ones more carefully */
89 				if (sp >= &gettystrs[4] && sp <= &gettystrs[6])
90 					l = 2;
91 				else
92 					l = strlen(sp->value) + 1;
93 				if ((p = malloc(l)) != NULL)
94 					strlcpy(p, sp->value, l);
95 				/*
96 				 * replace, even if NULL, else we'll
97 				 * have problems with free()ing static mem
98 				 */
99 				sp->value = p;
100 			}
101 		firsttime = 0;
102 	}
103 
104 	switch (cgetent(&buf, dba, name)) {
105 	case 1:
106 		syslog(LOG_ERR, "getty: couldn't resolve 'tc=' in gettytab '%s'", name);
107 		return;
108 	case 0:
109 		break;
110 	case -1:
111 		syslog(LOG_ERR, "getty: unknown gettytab entry '%s'", name);
112 		return;
113 	case -2:
114 		syslog(LOG_ERR, "getty: retrieving gettytab entry '%s': %m", name);
115 		return;
116 	case -3:
117 		syslog(LOG_ERR, "getty: recursive 'tc=' reference gettytab entry '%s'", name);
118 		return;
119 	default:
120 		syslog(LOG_ERR, "getty: unexpected cgetent() error for entry '%s'", name);
121 		return;
122 	}
123 
124 	for (sp = gettystrs; sp->field; sp++) {
125 		if ((l = cgetstr(buf, sp->field, &p)) >= 0) {
126 			if (sp->value) {
127 				/* prefer existing value */
128 				if (strcmp(p, sp->value) != 0)
129 					free(sp->value);
130 				else {
131 					free(p);
132 					p = sp->value;
133 				}
134 			}
135 			sp->value = p;
136 		} else if (l == -1) {
137 			free(sp->value);
138 			sp->value = NULL;
139 		}
140 	}
141 
142 	for (np = gettynums; np->field; np++) {
143 		if (cgetnum(buf, np->field, &n) == -1)
144 			np->set = 0;
145 		else {
146 			np->set = 1;
147 			np->value = n;
148 		}
149 	}
150 
151 	for (fp = gettyflags; fp->field; fp++) {
152 		if (cgetcap(buf, fp->field, ':') == NULL)
153 			fp->set = 0;
154 		else {
155 			fp->set = 1;
156 			fp->value = 1 ^ fp->invrt;
157 		}
158 	}
159 	free(buf);
160 }
161 
162 void
163 gendefaults(void)
164 {
165 	struct gettystrs *sp;
166 	struct gettynums *np;
167 	struct gettyflags *fp;
168 
169 	for (sp = gettystrs; sp->field; sp++)
170 		if (sp->value)
171 			sp->defalt = strdup(sp->value);
172 	for (np = gettynums; np->field; np++)
173 		if (np->set)
174 			np->defalt = np->value;
175 	for (fp = gettyflags; fp->field; fp++)
176 		if (fp->set)
177 			fp->defalt = fp->value;
178 		else
179 			fp->defalt = fp->invrt;
180 }
181 
182 void
183 setdefaults(void)
184 {
185 	struct gettystrs *sp;
186 	struct gettynums *np;
187 	struct gettyflags *fp;
188 
189 	for (sp = gettystrs; sp->field; sp++)
190 		if (!sp->value)
191 			sp->value = !sp->defalt ?
192 			    sp->defalt : strdup(sp->defalt);
193 	for (np = gettynums; np->field; np++)
194 		if (!np->set)
195 			np->value = np->defalt;
196 	for (fp = gettyflags; fp->field; fp++)
197 		if (!fp->set)
198 			fp->value = fp->defalt;
199 }
200 
201 static char **
202 charnames[] = {
203 	&ER, &KL, &IN, &QU, &XN, &XF, &ET, &BK,
204 	&SU, &DS, &RP, &FL, &WE, &LN, 0
205 };
206 
207 #define CV(a) (char *)(&tmode.c_cc[a])
208 
209 static char *
210 charvars[] = {
211 	CV(VERASE), CV(VKILL), CV(VINTR),
212 	CV(VQUIT), CV(VSTART), CV(VSTOP),
213 	CV(VEOF), CV(VEOL), CV(VSUSP),
214 	CV(VDSUSP), CV(VREPRINT), CV(VDISCARD),
215 	CV(VWERASE), CV(VLNEXT), 0
216 };
217 
218 void
219 setchars(void)
220 {
221 	int i;
222 	const char *p;
223 
224 	for (i = 0; charnames[i]; i++) {
225 		p = *charnames[i];
226 		if (p && *p)
227 			*charvars[i] = *p;
228 		else
229 			*charvars[i] = _POSIX_VDISABLE;
230 	}
231 }
232 
233 /* Macros to clear/set/test flags. */
234 #define	SET(t, f)	(t) |= (f)
235 #define	CLR(t, f)	(t) &= ~(f)
236 #define	ISSET(t, f)	((t) & (f))
237 
238 void
239 set_flags(int n)
240 {
241 	tcflag_t iflag, oflag, cflag, lflag;
242 
243 
244 	switch (n) {
245 	case 0:
246 		if (C0set && I0set && L0set && O0set) {
247 			tmode.c_cflag = C0;
248 			tmode.c_iflag = I0;
249 			tmode.c_lflag = L0;
250 			tmode.c_oflag = O0;
251 			return;
252 		}
253 		break;
254 	case 1:
255 		if (C1set && I1set && L1set && O1set) {
256 			tmode.c_cflag = C1;
257 			tmode.c_iflag = I1;
258 			tmode.c_lflag = L1;
259 			tmode.c_oflag = O1;
260 			return;
261 		}
262 		break;
263 	default:
264 		if (C2set && I2set && L2set && O2set) {
265 			tmode.c_cflag = C2;
266 			tmode.c_iflag = I2;
267 			tmode.c_lflag = L2;
268 			tmode.c_oflag = O2;
269 			return;
270 		}
271 		break;
272 	}
273 
274 	iflag = omode.c_iflag;
275 	oflag = omode.c_oflag;
276 	cflag = omode.c_cflag;
277 	lflag = omode.c_lflag;
278 
279 	if (NP) {
280 		CLR(cflag, CSIZE|PARENB);
281 		SET(cflag, CS8);
282 		CLR(iflag, ISTRIP|INPCK|IGNPAR);
283 	} else if (AP || EP || OP) {
284 		CLR(cflag, CSIZE);
285 		SET(cflag, CS7|PARENB);
286 		SET(iflag, ISTRIP);
287 		if (OP && !EP) {
288 			SET(iflag, INPCK|IGNPAR);
289 			SET(cflag, PARODD);
290 			if (AP)
291 				CLR(iflag, INPCK);
292 		} else if (EP && !OP) {
293 			SET(iflag, INPCK|IGNPAR);
294 			CLR(cflag, PARODD);
295 			if (AP)
296 				CLR(iflag, INPCK);
297 		} else if (AP || (EP && OP)) {
298 			CLR(iflag, INPCK|IGNPAR);
299 			CLR(cflag, PARODD);
300 		}
301 	} /* else, leave as is */
302 
303 #if 0
304 	if (UC)
305 		f |= LCASE;
306 #endif
307 
308 	if (HC)
309 		SET(cflag, HUPCL);
310 	else
311 		CLR(cflag, HUPCL);
312 
313 	if (MB)
314 		SET(cflag, MDMBUF);
315 	else
316 		CLR(cflag, MDMBUF);
317 
318 	if (HW)
319 		SET(cflag, CRTSCTS);
320 	else
321 		CLR(cflag, CRTSCTS);
322 
323 	if (NL) {
324 		SET(iflag, ICRNL);
325 		SET(oflag, ONLCR|OPOST);
326 	} else {
327 		CLR(iflag, ICRNL);
328 		CLR(oflag, ONLCR);
329 	}
330 
331 	if (!HT)
332 		SET(oflag, OXTABS|OPOST);
333 	else
334 		CLR(oflag, OXTABS);
335 
336 #ifdef XXX_DELAY
337 	SET(f, delaybits());
338 #endif
339 
340 	if (n == 1) {		/* read mode flags */
341 		if (RW) {
342 			iflag = 0;
343 			CLR(oflag, OPOST);
344 			CLR(cflag, CSIZE|PARENB);
345 			SET(cflag, CS8);
346 			lflag = 0;
347 		} else {
348 			CLR(lflag, ICANON);
349 		}
350 		goto out;
351 	}
352 
353 	if (n == 0)
354 		goto out;
355 
356 #if 0
357 	if (CB)
358 		SET(f, CRTBS);
359 #endif
360 
361 	if (CE)
362 		SET(lflag, ECHOE);
363 	else
364 		CLR(lflag, ECHOE);
365 
366 	if (CK)
367 		SET(lflag, ECHOKE);
368 	else
369 		CLR(lflag, ECHOKE);
370 
371 	if (PE)
372 		SET(lflag, ECHOPRT);
373 	else
374 		CLR(lflag, ECHOPRT);
375 
376 	if (EC)
377 		SET(lflag, ECHO);
378 	else
379 		CLR(lflag, ECHO);
380 
381 	if (XC)
382 		SET(lflag, ECHOCTL);
383 	else
384 		CLR(lflag, ECHOCTL);
385 
386 	if (DX)
387 		SET(lflag, IXANY);
388 	else
389 		CLR(lflag, IXANY);
390 
391 out:
392 	tmode.c_iflag = iflag;
393 	tmode.c_oflag = oflag;
394 	tmode.c_cflag = cflag;
395 	tmode.c_lflag = lflag;
396 }
397 
398 
399 #ifdef XXX_DELAY
400 struct delayval {
401 	unsigned	delay;		/* delay in ms */
402 	int		bits;
403 };
404 
405 /*
406  * below are random guesses, I can't be bothered checking
407  */
408 
409 struct delayval	crdelay[] = {
410 	{ 1,		CR1 },
411 	{ 2,		CR2 },
412 	{ 3,		CR3 },
413 	{ 83,		CR1 },
414 	{ 166,		CR2 },
415 	{ 0,		CR3 },
416 };
417 
418 struct delayval nldelay[] = {
419 	{ 1,		NL1 },		/* special, calculated */
420 	{ 2,		NL2 },
421 	{ 3,		NL3 },
422 	{ 100,		NL2 },
423 	{ 0,		NL3 },
424 };
425 
426 struct delayval	bsdelay[] = {
427 	{ 1,		BS1 },
428 	{ 0,		0 },
429 };
430 
431 struct delayval	ffdelay[] = {
432 	{ 1,		FF1 },
433 	{ 1750,		FF1 },
434 	{ 0,		FF1 },
435 };
436 
437 struct delayval	tbdelay[] = {
438 	{ 1,		TAB1 },
439 	{ 2,		TAB2 },
440 	{ 3,		XTABS },	/* this is expand tabs */
441 	{ 100,		TAB1 },
442 	{ 0,		TAB2 },
443 };
444 
445 int
446 delaybits(void)
447 {
448 	int f;
449 
450 	f  = adelay(CD, crdelay);
451 	f |= adelay(ND, nldelay);
452 	f |= adelay(FD, ffdelay);
453 	f |= adelay(TD, tbdelay);
454 	f |= adelay(BD, bsdelay);
455 	return (f);
456 }
457 
458 int
459 adelay(int ms, struct delayval *dp)
460 {
461 	if (ms == 0)
462 		return (0);
463 	while (dp->delay && ms > dp->delay)
464 		dp++;
465 	return (dp->bits);
466 }
467 #endif
468 
469 char	editedhost[MAXHOSTNAMELEN];
470 
471 void
472 edithost(const char *pattern)
473 {
474 	regex_t regex;
475 	regmatch_t *match;
476 	int found;
477 
478 	if (pattern == NULL || *pattern == '\0')
479 		goto copyasis;
480 	if (regcomp(&regex, pattern, REG_EXTENDED) != 0)
481 		goto copyasis;
482 
483 	match = calloc(regex.re_nsub + 1, sizeof(*match));
484 	if (match == NULL) {
485 		regfree(&regex);
486 		goto copyasis;
487 	}
488 
489 	found = !regexec(&regex, HN, regex.re_nsub + 1, match, 0);
490 	if (found) {
491 		size_t subex, totalsize;
492 
493 		/*
494 		 * We found a match.  If there were no parenthesized
495 		 * subexpressions in the pattern, use entire matched
496 		 * string as ``editedhost''; otherwise use the first
497 		 * matched subexpression.
498 		 */
499 		subex = !!regex.re_nsub;
500 		totalsize = match[subex].rm_eo - match[subex].rm_so + 1;
501 		strlcpy(editedhost, HN + match[subex].rm_so, totalsize >
502 		    sizeof(editedhost) ? sizeof(editedhost) : totalsize);
503 	}
504 	free(match);
505 	regfree(&regex);
506 	if (found)
507 		return;
508 	/*
509 	 * In case of any errors, or if the pattern did not match, pass
510 	 * the original hostname as is.
511 	 */
512 copyasis:
513 	strlcpy(editedhost, HN, sizeof(editedhost));
514 }
515 
516 static struct speedtab {
517 	int	speed;
518 	int	uxname;
519 } speedtab[] = {
520 	{ 50,	B50 },
521 	{ 75,	B75 },
522 	{ 110,	B110 },
523 	{ 134,	B134 },
524 	{ 150,	B150 },
525 	{ 200,	B200 },
526 	{ 300,	B300 },
527 	{ 600,	B600 },
528 	{ 1200,	B1200 },
529 	{ 1800,	B1800 },
530 	{ 2400,	B2400 },
531 	{ 4800,	B4800 },
532 	{ 9600,	B9600 },
533 	{ 19200, EXTA },
534 	{ 19,	EXTA },		/* for people who say 19.2K */
535 	{ 38400, EXTB },
536 	{ 38,	EXTB },
537 	{ 7200,	EXTB },		/* alternative */
538 	{ 57600, B57600 },
539 	{ 115200, B115200 },
540 	{ 230400, B230400 },
541 	{ 0, 0 }
542 };
543 
544 int
545 speed(int val)
546 {
547 	struct speedtab *sp;
548 
549 	if (val <= B230400)
550 		return (val);
551 
552 	for (sp = speedtab; sp->speed; sp++)
553 		if (sp->speed == val)
554 			return (sp->uxname);
555 
556 	return (B300);		/* default in impossible cases */
557 }
558 
559 void
560 makeenv(char *env[])
561 {
562 	static char termbuf[128] = "TERM=";
563 	char *p, *q;
564 	char **ep;
565 
566 	ep = env;
567 	if (TT && *TT) {
568 		strlcat(termbuf, TT, sizeof(termbuf));
569 		*ep++ = termbuf;
570 	}
571 	if ((p = EV)) {
572 		q = p;
573 		while ((q = strchr(q, ','))) {
574 			*q++ = '\0';
575 			*ep++ = p;
576 			p = q;
577 		}
578 		if (*p)
579 			*ep++ = p;
580 	}
581 	*ep = (char *)0;
582 }
583 
584 /*
585  * This speed select mechanism is written for the Develcon DATASWITCH.
586  * The Develcon sends a string of the form "B{speed}\n" at a predefined
587  * baud rate. This string indicates the user's actual speed.
588  * The routine below returns the terminal type mapped from derived speed.
589  */
590 static struct	portselect {
591 	const char	*ps_baud;
592 	const char	*ps_type;
593 } portspeeds[] = {
594 	{ "B110",	"std.110" },
595 	{ "B134",	"std.134" },
596 	{ "B150",	"std.150" },
597 	{ "B300",	"std.300" },
598 	{ "B600",	"std.600" },
599 	{ "B1200",	"std.1200" },
600 	{ "B2400",	"std.2400" },
601 	{ "B4800",	"std.4800" },
602 	{ "B9600",	"std.9600" },
603 	{ "B19200",	"std.19200" },
604 	{ NULL, NULL }
605 };
606 
607 const char *
608 portselector(void)
609 {
610 	char c, baud[20];
611 	const char *type = "default";
612 	struct portselect *ps;
613 	size_t len;
614 
615 	alarm(5*60);
616 	for (len = 0; len < sizeof (baud) - 1; len++) {
617 		if (read(STDIN_FILENO, &c, 1) <= 0)
618 			break;
619 		c &= 0177;
620 		if (c == '\n' || c == '\r')
621 			break;
622 		if (c == 'B')
623 			len = 0;	/* in case of leading garbage */
624 		baud[len] = c;
625 	}
626 	baud[len] = '\0';
627 	for (ps = portspeeds; ps->ps_baud; ps++)
628 		if (strcmp(ps->ps_baud, baud) == 0) {
629 			type = ps->ps_type;
630 			break;
631 		}
632 	sleep(2);	/* wait for connection to complete */
633 	return (type);
634 }
635 
636 /*
637  * This auto-baud speed select mechanism is written for the Micom 600
638  * portselector. Selection is done by looking at how the character '\r'
639  * is garbled at the different speeds.
640  */
641 const char *
642 autobaud(void)
643 {
644 	struct pollfd set[1];
645 	struct timespec timeout;
646 	char c;
647 	const char *type = "9600-baud";
648 
649 	(void)tcflush(0, TCIOFLUSH);
650 	set[0].fd = STDIN_FILENO;
651 	set[0].events = POLLIN;
652 	if (poll(set, 1, 5000) <= 0)
653 		return (type);
654 	if (read(STDIN_FILENO, &c, sizeof(char)) != sizeof(char))
655 		return (type);
656 	timeout.tv_sec = 0;
657 	timeout.tv_nsec = 20000;
658 	(void)nanosleep(&timeout, NULL);
659 	(void)tcflush(0, TCIOFLUSH);
660 	switch (c & 0377) {
661 
662 	case 0200:		/* 300-baud */
663 		type = "300-baud";
664 		break;
665 
666 	case 0346:		/* 1200-baud */
667 		type = "1200-baud";
668 		break;
669 
670 	case  015:		/* 2400-baud */
671 	case 0215:
672 		type = "2400-baud";
673 		break;
674 
675 	default:		/* 4800-baud */
676 		type = "4800-baud";
677 		break;
678 
679 	case 0377:		/* 9600-baud */
680 		type = "9600-baud";
681 		break;
682 	}
683 	return (type);
684 }
685