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