1 /*- 2 * Copyright (c) 1980 The Regents of the University of California. 3 * All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 */ 7 8 #ifndef lint 9 char copyright[] = 10 "@(#) Copyright (c) 1980 The Regents of the University of California.\n\ 11 All rights reserved.\n"; 12 #endif /* not lint */ 13 14 #ifndef lint 15 static char sccsid[] = "@(#)main.c 5.18 (Berkeley) 03/29/92"; 16 #endif /* not lint */ 17 18 #define USE_OLD_TTY 19 20 #include <sys/param.h> 21 #include <sys/stat.h> 22 #include <signal.h> 23 #include <fcntl.h> 24 #include <sgtty.h> 25 #include <time.h> 26 #include <ctype.h> 27 #include <setjmp.h> 28 #include <syslog.h> 29 #include <unistd.h> 30 #include <ctype.h> 31 #include <stdlib.h> 32 #include <string.h> 33 #include "gettytab.h" 34 #include "pathnames.h" 35 36 struct sgttyb tmode = { 37 0, 0, CERASE, CKILL, 0 38 }; 39 struct tchars tc = { 40 CINTR, CQUIT, CSTART, 41 CSTOP, CEOF, CBRK, 42 }; 43 struct ltchars ltc = { 44 CSUSP, CDSUSP, CRPRNT, 45 CFLUSH, CWERASE, CLNEXT 46 }; 47 48 int crmod, digit, lower, upper; 49 50 char hostname[MAXHOSTNAMELEN]; 51 char name[16]; 52 char dev[] = _PATH_DEV; 53 char ttyn[32]; 54 char *portselector(); 55 char *ttyname(); 56 57 #define OBUFSIZ 128 58 #define TABBUFSIZ 512 59 60 char defent[TABBUFSIZ]; 61 char defstrs[TABBUFSIZ]; 62 char tabent[TABBUFSIZ]; 63 char tabstrs[TABBUFSIZ]; 64 65 char *env[128]; 66 67 char partab[] = { 68 0001,0201,0201,0001,0201,0001,0001,0201, 69 0202,0004,0003,0205,0005,0206,0201,0001, 70 0201,0001,0001,0201,0001,0201,0201,0001, 71 0001,0201,0201,0001,0201,0001,0001,0201, 72 0200,0000,0000,0200,0000,0200,0200,0000, 73 0000,0200,0200,0000,0200,0000,0000,0200, 74 0000,0200,0200,0000,0200,0000,0000,0200, 75 0200,0000,0000,0200,0000,0200,0200,0000, 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 0000,0200,0200,0000,0200,0000,0000,0200, 81 0200,0000,0000,0200,0000,0200,0200,0000, 82 0200,0000,0000,0200,0000,0200,0200,0000, 83 0000,0200,0200,0000,0200,0000,0000,0201 84 }; 85 86 #define ERASE tmode.sg_erase 87 #define KILL tmode.sg_kill 88 #define EOT tc.t_eofc 89 90 jmp_buf timeout; 91 92 static void 93 dingdong() 94 { 95 96 alarm(0); 97 signal(SIGALRM, SIG_DFL); 98 longjmp(timeout, 1); 99 } 100 101 jmp_buf intrupt; 102 103 static void 104 interrupt() 105 { 106 107 signal(SIGINT, interrupt); 108 longjmp(intrupt, 1); 109 } 110 111 main(argc, argv) 112 int argc; 113 char **argv; 114 { 115 extern char **environ; 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 off; 169 170 gettable(tname, tabent, tabstrs); 171 if (OPset || EPset || APset) 172 APset++, OPset++, EPset++; 173 setdefaults(); 174 off = 0; 175 ioctl(0, TIOCFLUSH, &off); /* clear out the crap */ 176 ioctl(0, FIONBIO, &off); /* turn off non-blocking mode */ 177 ioctl(0, FIOASYNC, &off); /* ditto for async 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 if (HC) 191 ioctl(0, TIOCHPCL, 0); 192 if (AB) { 193 extern char *autobaud(); 194 195 tname = autobaud(); 196 continue; 197 } 198 if (PS) { 199 tname = portselector(); 200 continue; 201 } 202 if (CL && *CL) 203 putpad(CL); 204 edithost(HE); 205 if (IM && *IM) 206 putf(IM); 207 if (setjmp(timeout)) { 208 tmode.sg_ispeed = tmode.sg_ospeed = 0; 209 ioctl(0, TIOCSETP, &tmode); 210 exit(1); 211 } 212 if (TO) { 213 signal(SIGALRM, dingdong); 214 alarm(TO); 215 } 216 if (getname()) { 217 register int i; 218 219 oflush(); 220 alarm(0); 221 signal(SIGALRM, SIG_DFL); 222 if (name[0] == '-') { 223 puts("user names may not start with '-'."); 224 continue; 225 } 226 if (!(upper || lower || digit)) 227 continue; 228 allflags = setflags(2); 229 tmode.sg_flags = allflags & 0xffff; 230 allflags >>= 16; 231 if (crmod || NL) 232 tmode.sg_flags |= CRMOD; 233 if (upper || UC) 234 tmode.sg_flags |= LCASE; 235 if (lower || LC) 236 tmode.sg_flags &= ~LCASE; 237 ioctl(0, TIOCSETP, &tmode); 238 ioctl(0, TIOCSLTC, <c); 239 ioctl(0, TIOCLSET, &allflags); 240 signal(SIGINT, SIG_DFL); 241 for (i = 0; environ[i] != (char *)0; i++) 242 env[i] = environ[i]; 243 makeenv(&env[i]); 244 245 /* 246 * this is what login was doing anyway. 247 * soon we rewrite getty completely. 248 */ 249 set_ttydefaults(0); 250 execle(LO, "login", "-p", name, (char *) 0, env); 251 syslog(LOG_ERR, "%s: %m", LO); 252 exit(1); 253 } 254 alarm(0); 255 signal(SIGALRM, SIG_DFL); 256 signal(SIGINT, SIG_IGN); 257 if (NX && *NX) 258 tname = NX; 259 } 260 } 261 262 getname() 263 { 264 register int c; 265 register char *np; 266 char cs; 267 268 /* 269 * Interrupt may happen if we use CBREAK mode 270 */ 271 if (setjmp(intrupt)) { 272 signal(SIGINT, SIG_IGN); 273 return (0); 274 } 275 signal(SIGINT, interrupt); 276 tmode.sg_flags = setflags(0); 277 ioctl(0, TIOCSETP, &tmode); 278 tmode.sg_flags = setflags(1); 279 prompt(); 280 if (PF > 0) { 281 oflush(); 282 sleep(PF); 283 PF = 0; 284 } 285 ioctl(0, TIOCSETP, &tmode); 286 crmod = digit = lower = upper = 0; 287 np = name; 288 for (;;) { 289 oflush(); 290 if (read(STDIN_FILENO, &cs, 1) <= 0) 291 exit(0); 292 if ((c = cs&0177) == 0) 293 return (0); 294 if (c == EOT) 295 exit(1); 296 if (c == '\r' || c == '\n' || np >= &name[sizeof name]) { 297 putf("\r\n"); 298 break; 299 } 300 if (islower(c)) 301 lower = 1; 302 else if (isupper(c)) 303 upper = 1; 304 else if (c == ERASE || c == '#' || c == '\b') { 305 if (np > name) { 306 np--; 307 if (tmode.sg_ospeed >= B1200) 308 puts("\b \b"); 309 else 310 putchr(cs); 311 } 312 continue; 313 } else if (c == KILL || c == '@') { 314 putchr(cs); 315 putchr('\r'); 316 if (tmode.sg_ospeed < B1200) 317 putchr('\n'); 318 /* this is the way they do it down under ... */ 319 else if (np > name) 320 puts(" \r"); 321 prompt(); 322 np = name; 323 continue; 324 } else if (isdigit(c)) 325 digit++; 326 if (IG && (c <= ' ' || c > 0176)) 327 continue; 328 *np++ = c; 329 putchr(cs); 330 } 331 signal(SIGINT, SIG_IGN); 332 *np = 0; 333 if (c == '\r') 334 crmod = 1; 335 if (upper && !lower && !LC || UC) 336 for (np = name; *np; np++) 337 if (isupper(*np)) 338 *np = tolower(*np); 339 return (1); 340 } 341 342 static 343 short tmspc10[] = { 344 0, 2000, 1333, 909, 743, 666, 500, 333, 166, 83, 55, 41, 20, 10, 5, 15 345 }; 346 347 putpad(s) 348 register char *s; 349 { 350 register pad = 0; 351 register mspc10; 352 353 if (isdigit(*s)) { 354 while (isdigit(*s)) { 355 pad *= 10; 356 pad += *s++ - '0'; 357 } 358 pad *= 10; 359 if (*s == '.' && isdigit(s[1])) { 360 pad += s[1] - '0'; 361 s += 2; 362 } 363 } 364 365 puts(s); 366 /* 367 * If no delay needed, or output speed is 368 * not comprehensible, then don't try to delay. 369 */ 370 if (pad == 0) 371 return; 372 if (tmode.sg_ospeed <= 0 || 373 tmode.sg_ospeed >= (sizeof tmspc10 / sizeof tmspc10[0])) 374 return; 375 376 /* 377 * Round up by a half a character frame, and then do the delay. 378 * Too bad there are no user program accessible programmed delays. 379 * Transmitting pad characters slows many terminals down and also 380 * loads the system. 381 */ 382 mspc10 = tmspc10[tmode.sg_ospeed]; 383 pad += mspc10 / 2; 384 for (pad /= mspc10; pad > 0; pad--) 385 putchr(*PC); 386 } 387 388 puts(s) 389 register char *s; 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 if (!NP) { 404 c |= partab[c&0177] & 0200; 405 if (OP) 406 c ^= 0200; 407 } 408 if (!UB) { 409 outbuf[obufcnt++] = c; 410 if (obufcnt >= OBUFSIZ) 411 oflush(); 412 } else 413 write(STDOUT_FILENO, &c, 1); 414 } 415 416 oflush() 417 { 418 if (obufcnt) 419 write(STDOUT_FILENO, outbuf, obufcnt); 420 obufcnt = 0; 421 } 422 423 prompt() 424 { 425 426 putf(LM); 427 if (CO) 428 putchr('\n'); 429 } 430 431 putf(cp) 432 register char *cp; 433 { 434 extern char editedhost[]; 435 time_t t; 436 char *slash, db[100]; 437 438 while (*cp) { 439 if (*cp != '%') { 440 putchr(*cp++); 441 continue; 442 } 443 switch (*++cp) { 444 445 case 't': 446 slash = rindex(ttyn, '/'); 447 if (slash == (char *) 0) 448 puts(ttyn); 449 else 450 puts(&slash[1]); 451 break; 452 453 case 'h': 454 puts(editedhost); 455 break; 456 457 case 'd': { 458 static char fmt[] = "%l:% %P on %A, %d %B %Y"; 459 460 fmt[4] = 'M'; /* I *hate* SCCS... */ 461 (void)time(&t); 462 (void)strftime(db, sizeof(db), fmt, localtime(&t)); 463 puts(db); 464 break; 465 } 466 467 case '%': 468 putchr('%'); 469 break; 470 } 471 cp++; 472 } 473 } 474