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