1 /* 2 * Copyright (c) 1980 Regents of the University of California. 3 * All rights reserved. The Berkeley software License Agreement 4 * specifies the terms and conditions for redistribution. 5 */ 6 7 #ifndef lint 8 char copyright[] = 9 "@(#) Copyright (c) 1980 Regents of the University of California.\n\ 10 All rights reserved.\n"; 11 #endif not lint 12 13 #ifndef lint 14 static char sccsid[] = "@(#)main.c 5.10 (Berkeley) 06/28/90"; 15 #endif not lint 16 17 /* 18 * getty -- adapt to terminal speed on dialup, and call login 19 * 20 * Melbourne getty, June 83, kre. 21 */ 22 23 #define USE_OLD_TTY 24 #include <sys/param.h> 25 #include <sys/signal.h> 26 #include <sys/file.h> 27 #include <sgtty.h> 28 #include <ctype.h> 29 #include <setjmp.h> 30 #include <syslog.h> 31 #include <ctype.h> 32 #include "gettytab.h" 33 #include "pathnames.h" 34 35 extern char **environ; 36 37 struct sgttyb tmode = { 38 0, 0, CERASE, CKILL, 0 39 }; 40 struct tchars tc = { 41 CINTR, CQUIT, CSTART, 42 CSTOP, CEOF, CBRK, 43 }; 44 struct ltchars ltc = { 45 CSUSP, CDSUSP, CRPRNT, 46 CFLUSH, CWERASE, CLNEXT 47 }; 48 49 int crmod; 50 int upper; 51 int lower; 52 int digit; 53 54 char hostname[MAXHOSTNAMELEN]; 55 char name[16]; 56 char dev[] = _PATH_DEV; 57 char ttyn[32]; 58 char *portselector(); 59 char *ttyname(); 60 61 #define OBUFSIZ 128 62 #define TABBUFSIZ 512 63 64 char defent[TABBUFSIZ]; 65 char defstrs[TABBUFSIZ]; 66 char tabent[TABBUFSIZ]; 67 char tabstrs[TABBUFSIZ]; 68 69 char *env[128]; 70 71 char partab[] = { 72 0001,0201,0201,0001,0201,0001,0001,0201, 73 0202,0004,0003,0205,0005,0206,0201,0001, 74 0201,0001,0001,0201,0001,0201,0201,0001, 75 0001,0201,0201,0001,0201,0001,0001,0201, 76 0200,0000,0000,0200,0000,0200,0200,0000, 77 0000,0200,0200,0000,0200,0000,0000,0200, 78 0000,0200,0200,0000,0200,0000,0000,0200, 79 0200,0000,0000,0200,0000,0200,0200,0000, 80 0200,0000,0000,0200,0000,0200,0200,0000, 81 0000,0200,0200,0000,0200,0000,0000,0200, 82 0000,0200,0200,0000,0200,0000,0000,0200, 83 0200,0000,0000,0200,0000,0200,0200,0000, 84 0000,0200,0200,0000,0200,0000,0000,0200, 85 0200,0000,0000,0200,0000,0200,0200,0000, 86 0200,0000,0000,0200,0000,0200,0200,0000, 87 0000,0200,0200,0000,0200,0000,0000,0201 88 }; 89 90 #define ERASE tmode.sg_erase 91 #define KILL tmode.sg_kill 92 #define EOT tc.t_eofc 93 94 jmp_buf timeout; 95 96 dingdong() 97 { 98 99 alarm(0); 100 signal(SIGALRM, SIG_DFL); 101 longjmp(timeout, 1); 102 } 103 104 jmp_buf intrupt; 105 106 interrupt() 107 { 108 109 signal(SIGINT, interrupt); 110 longjmp(intrupt, 1); 111 } 112 113 main(argc, argv) 114 char *argv[]; 115 { 116 char *tname; 117 long allflags; 118 int repcnt = 0; 119 120 signal(SIGINT, SIG_IGN); 121 /* 122 signal(SIGQUIT, SIG_DFL); 123 */ 124 openlog("getty", LOG_ODELAY|LOG_CONS, LOG_AUTH); 125 gethostname(hostname, sizeof(hostname)); 126 if (hostname[0] == '\0') 127 strcpy(hostname, "Amnesiac"); 128 /* 129 * The following is a work around for vhangup interactions 130 * which cause great problems getting window systems started. 131 * If the tty line is "-", we do the old style getty presuming 132 * that the file descriptors are already set up for us. 133 * J. Gettys - MIT Project Athena. 134 */ 135 if (argc <= 2 || strcmp(argv[2], "-") == 0) 136 strcpy(ttyn, ttyname(0)); 137 else { 138 int i; 139 140 strcpy(ttyn, dev); 141 strncat(ttyn, argv[2], sizeof(ttyn)-sizeof(dev)); 142 if (strcmp(argv[0], "+") != 0) { 143 chown(ttyn, 0, 0); 144 chmod(ttyn, 0600); 145 revoke(ttyn); 146 /* 147 * Delay the open so DTR stays down long enough to be detected. 148 */ 149 sleep(2); 150 while ((i = open(ttyn, O_RDWR)) == -1) { 151 if (repcnt % 10 == 0) { 152 syslog(LOG_ERR, "%s: %m", ttyn); 153 closelog(); 154 } 155 repcnt++; 156 sleep(60); 157 } 158 login_tty(i); 159 } 160 } 161 162 gettable("default", defent, defstrs); 163 gendefaults(); 164 tname = "default"; 165 if (argc > 1) 166 tname = argv[1]; 167 for (;;) { 168 int ldisp = OTTYDISC; 169 int off = 0; 170 171 gettable(tname, tabent, tabstrs); 172 if (OPset || EPset || APset) 173 APset++, OPset++, EPset++; 174 setdefaults(); 175 ioctl(0, TIOCFLUSH, 0); /* clear out the crap */ 176 ioctl(0, FIONBIO, &off); /* turn off non-blocking mode */ 177 ioctl(0, FIOASYNC, &off); /* ditto for asynchronous mode */ 178 if (IS) 179 tmode.sg_ispeed = speed(IS); 180 else if (SP) 181 tmode.sg_ispeed = speed(SP); 182 if (OS) 183 tmode.sg_ospeed = speed(OS); 184 else if (SP) 185 tmode.sg_ospeed = speed(SP); 186 tmode.sg_flags = setflags(0); 187 ioctl(0, TIOCSETP, &tmode); 188 setchars(); 189 ioctl(0, TIOCSETC, &tc); 190 ioctl(0, TIOCSETD, &ldisp); 191 if (HC) 192 ioctl(0, TIOCHPCL, 0); 193 if (AB) { 194 extern char *autobaud(); 195 196 tname = autobaud(); 197 continue; 198 } 199 if (PS) { 200 tname = portselector(); 201 continue; 202 } 203 if (CL && *CL) 204 putpad(CL); 205 edithost(HE); 206 if (IM && *IM) 207 putf(IM); 208 if (setjmp(timeout)) { 209 tmode.sg_ispeed = tmode.sg_ospeed = 0; 210 ioctl(0, TIOCSETP, &tmode); 211 exit(1); 212 } 213 if (TO) { 214 signal(SIGALRM, dingdong); 215 alarm(TO); 216 } 217 if (getname()) { 218 register int i; 219 220 oflush(); 221 alarm(0); 222 signal(SIGALRM, SIG_DFL); 223 if (name[0] == '-') { 224 puts("user names may not start with '-'."); 225 continue; 226 } 227 if (!(upper || lower || digit)) 228 continue; 229 allflags = setflags(2); 230 tmode.sg_flags = allflags & 0xffff; 231 allflags >>= 16; 232 if (crmod || NL) 233 tmode.sg_flags |= CRMOD; 234 if (upper || UC) 235 tmode.sg_flags |= LCASE; 236 if (lower || LC) 237 tmode.sg_flags &= ~LCASE; 238 ioctl(0, TIOCSETP, &tmode); 239 ioctl(0, TIOCSLTC, <c); 240 ioctl(0, TIOCLSET, &allflags); 241 signal(SIGINT, SIG_DFL); 242 for (i = 0; environ[i] != (char *)0; i++) 243 env[i] = environ[i]; 244 makeenv(&env[i]); 245 execle(LO, "login", "-p", name, (char *) 0, env); 246 syslog(LOG_ERR, "%s: %m", LO); 247 exit(1); 248 } 249 alarm(0); 250 signal(SIGALRM, SIG_DFL); 251 signal(SIGINT, SIG_IGN); 252 if (NX && *NX) 253 tname = NX; 254 } 255 } 256 257 getname() 258 { 259 register char *np; 260 register c; 261 char cs; 262 263 /* 264 * Interrupt may happen if we use CBREAK mode 265 */ 266 if (setjmp(intrupt)) { 267 signal(SIGINT, SIG_IGN); 268 return (0); 269 } 270 signal(SIGINT, interrupt); 271 tmode.sg_flags = setflags(0); 272 ioctl(0, TIOCSETP, &tmode); 273 tmode.sg_flags = setflags(1); 274 prompt(); 275 if (PF > 0) { 276 oflush(); 277 sleep(PF); 278 PF = 0; 279 } 280 ioctl(0, TIOCSETP, &tmode); 281 crmod = 0; 282 upper = 0; 283 lower = 0; 284 digit = 0; 285 np = name; 286 for (;;) { 287 oflush(); 288 if (read(0, &cs, 1) <= 0) 289 exit(0); 290 if ((c = cs&0177) == 0) 291 return (0); 292 if (c == EOT) 293 exit(1); 294 if (c == '\r' || c == '\n' || np >= &name[sizeof name]) { 295 putf("\r\n"); 296 break; 297 } 298 if (islower(c)) 299 lower++; 300 else if (isupper(c)) 301 upper++; 302 else if (c == ERASE || c == '#' || c == '\b') { 303 if (np > name) { 304 np--; 305 if (tmode.sg_ospeed >= B1200) 306 puts("\b \b"); 307 else 308 putchr(cs); 309 } 310 continue; 311 } else if (c == KILL || c == '@') { 312 putchr(cs); 313 putchr('\r'); 314 if (tmode.sg_ospeed < B1200) 315 putchr('\n'); 316 /* this is the way they do it down under ... */ 317 else if (np > name) 318 puts(" \r"); 319 prompt(); 320 np = name; 321 continue; 322 } else if (isdigit(c)) 323 digit++; 324 if (IG && (c <= ' ' || c > 0176)) 325 continue; 326 *np++ = c; 327 putchr(cs); 328 } 329 signal(SIGINT, SIG_IGN); 330 *np = 0; 331 if (c == '\r') 332 crmod++; 333 if (upper && !lower && !LC || UC) 334 for (np = name; *np; np++) 335 if (isupper(*np)) 336 *np = tolower(*np); 337 return (1); 338 } 339 340 static 341 short tmspc10[] = { 342 0, 2000, 1333, 909, 743, 666, 500, 333, 166, 83, 55, 41, 20, 10, 5, 15 343 }; 344 345 putpad(s) 346 register char *s; 347 { 348 register pad = 0; 349 register mspc10; 350 351 if (isdigit(*s)) { 352 while (isdigit(*s)) { 353 pad *= 10; 354 pad += *s++ - '0'; 355 } 356 pad *= 10; 357 if (*s == '.' && isdigit(s[1])) { 358 pad += s[1] - '0'; 359 s += 2; 360 } 361 } 362 363 puts(s); 364 /* 365 * If no delay needed, or output speed is 366 * not comprehensible, then don't try to delay. 367 */ 368 if (pad == 0) 369 return; 370 if (tmode.sg_ospeed <= 0 || 371 tmode.sg_ospeed >= (sizeof tmspc10 / sizeof tmspc10[0])) 372 return; 373 374 /* 375 * Round up by a half a character frame, 376 * and then do the delay. 377 * Too bad there are no user program accessible programmed delays. 378 * Transmitting pad characters slows many 379 * terminals down and also loads the system. 380 */ 381 mspc10 = tmspc10[tmode.sg_ospeed]; 382 pad += mspc10 / 2; 383 for (pad /= mspc10; pad > 0; pad--) 384 putchr(*PC); 385 } 386 387 puts(s) 388 register char *s; 389 { 390 391 while (*s) 392 putchr(*s++); 393 } 394 395 char outbuf[OBUFSIZ]; 396 int obufcnt = 0; 397 398 putchr(cc) 399 { 400 char c; 401 402 c = cc; 403 c |= partab[c&0177] & 0200; 404 if (OP) 405 c ^= 0200; 406 if (!UB) { 407 outbuf[obufcnt++] = c; 408 if (obufcnt >= OBUFSIZ) 409 oflush(); 410 } else 411 write(1, &c, 1); 412 } 413 414 oflush() 415 { 416 if (obufcnt) 417 write(1, outbuf, obufcnt); 418 obufcnt = 0; 419 } 420 421 prompt() 422 { 423 424 putf(LM); 425 if (CO) 426 putchr('\n'); 427 } 428 429 putf(cp) 430 register char *cp; 431 { 432 char *slash; 433 char datebuffer[60]; 434 extern char editedhost[]; 435 extern char *rindex(); 436 437 while (*cp) { 438 if (*cp != '%') { 439 putchr(*cp++); 440 continue; 441 } 442 switch (*++cp) { 443 444 case 't': 445 slash = rindex(ttyn, '/'); 446 if (slash == (char *) 0) 447 puts(ttyn); 448 else 449 puts(&slash[1]); 450 break; 451 452 case 'h': 453 puts(editedhost); 454 break; 455 456 case 'd': 457 get_date(datebuffer); 458 puts(datebuffer); 459 break; 460 461 case '%': 462 putchr('%'); 463 break; 464 } 465 cp++; 466 } 467 } 468