getty.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020
  1. /* vi: set sw=4 ts=4: */
  2. /* agetty.c - another getty program for Linux. By W. Z. Venema 1989
  3. Ported to Linux by Peter Orbaek <poe@daimi.aau.dk>
  4. This program is freely distributable. The entire man-page used to
  5. be here. Now read the real man-page agetty.8 instead.
  6. -f option added by Eric Rasmussen <ear@usfirst.org> - 12/28/95
  7. 1999-02-22 Arkadiusz Mi¶kiewicz <misiek@misiek.eu.org>
  8. - added Native Language Support
  9. 1999-05-05 Thorsten Kranzkowski <dl8bcu@gmx.net>
  10. - enable hardware flow control before displaying /etc/issue
  11. */
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <unistd.h>
  15. #include <string.h>
  16. #include <sys/ioctl.h>
  17. #include <errno.h>
  18. #include <sys/stat.h>
  19. #include <sys/signal.h>
  20. #include <fcntl.h>
  21. #include <stdarg.h>
  22. #include <ctype.h>
  23. #include <utmp.h>
  24. #include <getopt.h>
  25. #include <termios.h>
  26. #include "busybox.h"
  27. #define _PATH_LOGIN "/bin/login"
  28. /* If USE_SYSLOG is undefined all diagnostics go directly to /dev/console. */
  29. #ifdef CONFIG_SYSLOGD
  30. #include <sys/param.h>
  31. #define USE_SYSLOG
  32. #include <syslog.h>
  33. #endif
  34. /*
  35. * Some heuristics to find out what environment we are in: if it is not
  36. * System V, assume it is SunOS 4.
  37. */
  38. #ifdef LOGIN_PROCESS /* defined in System V utmp.h */
  39. #define SYSV_STYLE /* select System V style getty */
  40. #ifdef CONFIG_FEATURE_U_W_TMP
  41. extern void updwtmp(const char *filename, const struct utmp *ut);
  42. #endif
  43. #endif /* LOGIN_PROCESS */
  44. /*
  45. * Things you may want to modify.
  46. *
  47. * If ISSUE is not defined, agetty will never display the contents of the
  48. * /etc/issue file. You will not want to spit out large "issue" files at the
  49. * wrong baud rate. Relevant for System V only.
  50. *
  51. * You may disagree with the default line-editing etc. characters defined
  52. * below. Note, however, that DEL cannot be used for interrupt generation
  53. * and for line editing at the same time.
  54. */
  55. #ifdef SYSV_STYLE
  56. #define ISSUE "/etc/issue" /* displayed before the login prompt */
  57. #include <sys/utsname.h>
  58. #include <time.h>
  59. #endif
  60. /* Some shorthands for control characters. */
  61. #define CTL(x) (x ^ 0100) /* Assumes ASCII dialect */
  62. #define CR CTL('M') /* carriage return */
  63. #define NL CTL('J') /* line feed */
  64. #define BS CTL('H') /* back space */
  65. #define DEL CTL('?') /* delete */
  66. /* Defaults for line-editing etc. characters; you may want to change this. */
  67. #define DEF_ERASE DEL /* default erase character */
  68. #define DEF_INTR CTL('C') /* default interrupt character */
  69. #define DEF_QUIT CTL('\\') /* default quit char */
  70. #define DEF_KILL CTL('U') /* default kill char */
  71. #define DEF_EOF CTL('D') /* default EOF char */
  72. #define DEF_EOL '\n'
  73. #define DEF_SWITCH 0 /* default switch char */
  74. /*
  75. * SunOS 4.1.1 termio is broken. We must use the termios stuff instead,
  76. * because the termio -> termios translation does not clear the termios
  77. * CIBAUD bits. Therefore, the tty driver would sometimes report that input
  78. * baud rate != output baud rate. I did not notice that problem with SunOS
  79. * 4.1. We will use termios where available, and termio otherwise.
  80. */
  81. /* linux 0.12 termio is broken too, if we use it c_cc[VERASE] isn't set
  82. properly, but all is well if we use termios?! */
  83. #ifdef TCGETS
  84. #undef TCGETA
  85. #undef TCSETA
  86. #undef TCSETAW
  87. #define termio termios
  88. #define TCGETA TCGETS
  89. #define TCSETA TCSETS
  90. #define TCSETAW TCSETSW
  91. #endif
  92. /*
  93. * This program tries to not use the standard-i/o library. This keeps the
  94. * executable small on systems that do not have shared libraries (System V
  95. * Release <3).
  96. */
  97. #ifndef BUFSIZ
  98. #define BUFSIZ 1024
  99. #endif
  100. /*
  101. * When multiple baud rates are specified on the command line, the first one
  102. * we will try is the first one specified.
  103. */
  104. #define FIRST_SPEED 0
  105. /* Storage for command-line options. */
  106. #define MAX_SPEED 10 /* max. nr. of baud rates */
  107. struct options {
  108. int flags; /* toggle switches, see below */
  109. int timeout; /* time-out period */
  110. char *login; /* login program */
  111. char *tty; /* name of tty */
  112. char *initstring; /* modem init string */
  113. char *issue; /* alternative issue file */
  114. int numspeed; /* number of baud rates to try */
  115. int speeds[MAX_SPEED]; /* baud rates to be tried */
  116. };
  117. #define F_PARSE (1<<0) /* process modem status messages */
  118. #define F_ISSUE (1<<1) /* display /etc/issue */
  119. #define F_RTSCTS (1<<2) /* enable RTS/CTS flow control */
  120. #define F_LOCAL (1<<3) /* force local */
  121. #define F_INITSTRING (1<<4) /* initstring is set */
  122. #define F_WAITCRLF (1<<5) /* wait for CR or LF */
  123. #define F_CUSTISSUE (1<<6) /* give alternative issue file */
  124. #define F_NOPROMPT (1<<7) /* don't ask for login name! */
  125. /* Storage for things detected while the login name was read. */
  126. static struct chardata {
  127. int erase; /* erase character */
  128. int kill; /* kill character */
  129. int eol; /* end-of-line character */
  130. int parity; /* what parity did we see */
  131. int capslock; /* upper case without lower case */
  132. };
  133. /* Initial values for the above. */
  134. static struct chardata init_chardata = {
  135. DEF_ERASE, /* default erase character */
  136. DEF_KILL, /* default kill character */
  137. 13, /* default eol char */
  138. 0, /* space parity */
  139. 0, /* no capslock */
  140. };
  141. #if 0
  142. struct Speedtab {
  143. long speed;
  144. int code;
  145. };
  146. static struct Speedtab speedtab[] = {
  147. {50, B50},
  148. {75, B75},
  149. {110, B110},
  150. {134, B134},
  151. {150, B150},
  152. {200, B200},
  153. {300, B300},
  154. {600, B600},
  155. {1200, B1200},
  156. {1800, B1800},
  157. {2400, B2400},
  158. {4800, B4800},
  159. {9600, B9600},
  160. #ifdef B19200
  161. {19200, B19200},
  162. #endif
  163. #ifdef B38400
  164. {38400, B38400},
  165. #endif
  166. #ifdef EXTA
  167. {19200, EXTA},
  168. #endif
  169. #ifdef EXTB
  170. {38400, EXTB},
  171. #endif
  172. #ifdef B57600
  173. {57600, B57600},
  174. #endif
  175. #ifdef B115200
  176. {115200, B115200},
  177. #endif
  178. #ifdef B230400
  179. {230400, B230400},
  180. #endif
  181. {0, 0},
  182. };
  183. #endif
  184. static void parse_args(int argc, char **argv, struct options *op);
  185. static void parse_speeds(struct options *op, char *arg);
  186. static void open_tty(char *tty, struct termio *tp, int local);
  187. static void termio_init(struct termio *tp, int speed, struct options *op);
  188. static void auto_baud(struct termio *tp);
  189. static void do_prompt(struct options *op, struct termio *tp);
  190. static void next_speed(struct termio *tp, struct options *op);
  191. static char *get_logname(struct options *op, struct chardata *cp,
  192. struct termio *tp);
  193. static void termio_final(struct options *op, struct termio *tp,
  194. struct chardata *cp);
  195. static int caps_lock(const char *s);
  196. static int bcode(char *s);
  197. static void error(const char *fmt, ...) __attribute__ ((noreturn));
  198. #ifdef CONFIG_FEATURE_U_W_TMP
  199. static void update_utmp(char *line);
  200. #endif
  201. /* The following is used for understandable diagnostics. */
  202. /* Fake hostname for ut_host specified on command line. */
  203. static char *fakehost = NULL;
  204. /* ... */
  205. #ifdef DEBUGGING
  206. #define debug(s) fprintf(dbf,s); fflush(dbf)
  207. #define DEBUGTERM "/dev/ttyp0"
  208. FILE *dbf;
  209. #else
  210. #define debug(s) /* nothing */
  211. #endif
  212. int getty_main(int argc, char **argv)
  213. {
  214. char *logname = NULL; /* login name, given to /bin/login */
  215. struct chardata chardata; /* set by get_logname() */
  216. struct termio termio; /* terminal mode bits */
  217. static struct options options = {
  218. F_ISSUE, /* show /etc/issue (SYSV_STYLE) */
  219. 0, /* no timeout */
  220. _PATH_LOGIN, /* default login program */
  221. "tty1", /* default tty line */
  222. "", /* modem init string */
  223. ISSUE, /* default issue file */
  224. 0, /* no baud rates known yet */
  225. };
  226. #ifdef DEBUGGING
  227. dbf = bb_xfopen(DEBUGTERM, "w");
  228. {
  229. int i;
  230. for (i = 1; i < argc; i++) {
  231. debug(argv[i]);
  232. debug("\n");
  233. }
  234. }
  235. #endif
  236. /* Parse command-line arguments. */
  237. parse_args(argc, argv, &options);
  238. #ifdef __linux__
  239. setsid();
  240. #endif
  241. /* Update the utmp file. */
  242. #ifdef SYSV_STYLE
  243. #ifdef CONFIG_FEATURE_U_W_TMP
  244. update_utmp(options.tty);
  245. #endif
  246. #endif
  247. debug("calling open_tty\n");
  248. /* Open the tty as standard { input, output, error }. */
  249. open_tty(options.tty, &termio, options.flags & F_LOCAL);
  250. #ifdef __linux__
  251. {
  252. int iv;
  253. iv = getpid();
  254. ioctl(0, TIOCSPGRP, &iv);
  255. }
  256. #endif
  257. /* Initialize the termio settings (raw mode, eight-bit, blocking i/o). */
  258. debug("calling termio_init\n");
  259. termio_init(&termio, options.speeds[FIRST_SPEED], &options);
  260. /* write the modem init string and DON'T flush the buffers */
  261. if (options.flags & F_INITSTRING) {
  262. debug("writing init string\n");
  263. write(1, options.initstring, strlen(options.initstring));
  264. }
  265. if (!(options.flags & F_LOCAL)) {
  266. /* go to blocking write mode unless -L is specified */
  267. fcntl(1, F_SETFL, fcntl(1, F_GETFL, 0) & ~O_NONBLOCK);
  268. }
  269. /* Optionally detect the baud rate from the modem status message. */
  270. debug("before autobaud\n");
  271. if (options.flags & F_PARSE)
  272. auto_baud(&termio);
  273. /* Set the optional timer. */
  274. if (options.timeout)
  275. (void) alarm((unsigned) options.timeout);
  276. /* optionally wait for CR or LF before writing /etc/issue */
  277. if (options.flags & F_WAITCRLF) {
  278. char ch;
  279. debug("waiting for cr-lf\n");
  280. while (read(0, &ch, 1) == 1) {
  281. ch &= 0x7f; /* strip "parity bit" */
  282. #ifdef DEBUGGING
  283. fprintf(dbf, "read %c\n", ch);
  284. #endif
  285. if (ch == '\n' || ch == '\r')
  286. break;
  287. }
  288. }
  289. chardata = init_chardata;
  290. if (!(options.flags & F_NOPROMPT)) {
  291. /* Read the login name. */
  292. debug("reading login name\n");
  293. /* while ((logname = get_logname(&options, &chardata, &termio)) == 0) */
  294. while ((logname = get_logname(&options, &chardata, &termio)) ==
  295. NULL) next_speed(&termio, &options);
  296. }
  297. /* Disable timer. */
  298. if (options.timeout)
  299. (void) alarm(0);
  300. /* Finalize the termio settings. */
  301. termio_final(&options, &termio, &chardata);
  302. /* Now the newline character should be properly written. */
  303. (void) write(1, "\n", 1);
  304. /* Let the login program take care of password validation. */
  305. (void) execl(options.login, options.login, "--", logname, (char *) 0);
  306. error("%s: can't exec %s: %m", options.tty, options.login);
  307. }
  308. /* parse-args - parse command-line arguments */
  309. static void parse_args(int argc, char **argv, struct options *op)
  310. {
  311. extern char *optarg; /* getopt */
  312. extern int optind; /* getopt */
  313. int c;
  314. while (isascii(c = getopt(argc, argv, "I:LH:f:hil:mt:wn"))) {
  315. switch (c) {
  316. case 'I':
  317. if (!(op->initstring = strdup(optarg)))
  318. error(bb_msg_memory_exhausted);
  319. {
  320. const char *p;
  321. char *q;
  322. /* copy optarg into op->initstring decoding \ddd
  323. octal codes into chars */
  324. q = op->initstring;
  325. p = optarg;
  326. while (*p) {
  327. if (*p == '\\') {
  328. p++;
  329. *q++ = bb_process_escape_sequence(&p);
  330. } else {
  331. *q++ = *p++;
  332. }
  333. }
  334. *q = '\0';
  335. }
  336. op->flags |= F_INITSTRING;
  337. break;
  338. case 'L': /* force local */
  339. op->flags |= F_LOCAL;
  340. break;
  341. case 'H': /* fake login host */
  342. fakehost = optarg;
  343. break;
  344. case 'f': /* custom issue file */
  345. op->flags |= F_CUSTISSUE;
  346. op->issue = optarg;
  347. break;
  348. case 'h': /* enable h/w flow control */
  349. op->flags |= F_RTSCTS;
  350. break;
  351. case 'i': /* do not show /etc/issue */
  352. op->flags &= ~F_ISSUE;
  353. break;
  354. case 'l':
  355. op->login = optarg; /* non-default login program */
  356. break;
  357. case 'm': /* parse modem status message */
  358. op->flags |= F_PARSE;
  359. break;
  360. case 'n':
  361. op->flags |= F_NOPROMPT;
  362. break;
  363. case 't': /* time out */
  364. if ((op->timeout = atoi(optarg)) <= 0)
  365. error("bad timeout value: %s", optarg);
  366. break;
  367. case 'w':
  368. op->flags |= F_WAITCRLF;
  369. break;
  370. default:
  371. bb_show_usage();
  372. }
  373. }
  374. debug("after getopt loop\n");
  375. if (argc < optind + 2) /* check parameter count */
  376. bb_show_usage();
  377. /* we loosen up a bit and accept both "baudrate tty" and "tty baudrate" */
  378. if ('0' <= argv[optind][0] && argv[optind][0] <= '9') {
  379. /* a number first, assume it's a speed (BSD style) */
  380. parse_speeds(op, argv[optind++]); /* baud rate(s) */
  381. op->tty = argv[optind]; /* tty name */
  382. } else {
  383. op->tty = argv[optind++]; /* tty name */
  384. parse_speeds(op, argv[optind]); /* baud rate(s) */
  385. }
  386. optind++;
  387. if (argc > optind && argv[optind])
  388. setenv("TERM", argv[optind], 1);
  389. debug("exiting parseargs\n");
  390. }
  391. /* parse_speeds - parse alternate baud rates */
  392. static void parse_speeds(struct options *op, char *arg)
  393. {
  394. char *cp;
  395. debug("entered parse_speeds\n");
  396. for (cp = strtok(arg, ","); cp != 0; cp = strtok((char *) 0, ",")) {
  397. if ((op->speeds[op->numspeed++] = bcode(cp)) <= 0)
  398. error("bad speed: %s", cp);
  399. if (op->numspeed > MAX_SPEED)
  400. error("too many alternate speeds");
  401. }
  402. debug("exiting parsespeeds\n");
  403. }
  404. #ifdef SYSV_STYLE
  405. #ifdef CONFIG_FEATURE_U_W_TMP
  406. /* update_utmp - update our utmp entry */
  407. static void update_utmp(char *line)
  408. {
  409. struct utmp ut;
  410. struct utmp *utp;
  411. time_t t;
  412. int mypid = getpid();
  413. #if ! (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 1))
  414. struct flock lock;
  415. #endif
  416. /*
  417. * The utmp file holds miscellaneous information about things started by
  418. * /sbin/init and other system-related events. Our purpose is to update
  419. * the utmp entry for the current process, in particular the process type
  420. * and the tty line we are listening to. Return successfully only if the
  421. * utmp file can be opened for update, and if we are able to find our
  422. * entry in the utmp file.
  423. */
  424. if (access(_PATH_UTMP, R_OK|W_OK) == -1) {
  425. close(creat(_PATH_UTMP, 0664));
  426. }
  427. utmpname(_PATH_UTMP);
  428. setutent();
  429. while ((utp = getutent())
  430. && !(utp->ut_type == INIT_PROCESS && utp->ut_pid == mypid)) /* nothing */
  431. ;
  432. if (utp) {
  433. memcpy(&ut, utp, sizeof(ut));
  434. } else {
  435. /* some inits don't initialize utmp... */
  436. memset(&ut, 0, sizeof(ut));
  437. strncpy(ut.ut_id, line + 3, sizeof(ut.ut_id));
  438. }
  439. /*endutent(); */
  440. strncpy(ut.ut_user, "LOGIN", sizeof(ut.ut_user));
  441. strncpy(ut.ut_line, line, sizeof(ut.ut_line));
  442. if (fakehost)
  443. strncpy(ut.ut_host, fakehost, sizeof(ut.ut_host));
  444. time(&t);
  445. ut.ut_time = t;
  446. ut.ut_type = LOGIN_PROCESS;
  447. ut.ut_pid = mypid;
  448. pututline(&ut);
  449. endutent();
  450. {
  451. if (access(_PATH_WTMP, R_OK|W_OK) == -1) {
  452. close(creat(_PATH_WTMP, 0664));
  453. }
  454. updwtmp(_PATH_WTMP, &ut);
  455. }
  456. }
  457. #endif /* CONFIG_FEATURE_U_W_TMP */
  458. #endif /* SYSV_STYLE */
  459. /* open_tty - set up tty as standard { input, output, error } */
  460. static void open_tty(char *tty, struct termio *tp, int local)
  461. {
  462. /* Get rid of the present standard { output, error} if any. */
  463. (void) close(1);
  464. (void) close(2);
  465. errno = 0; /* ignore above errors */
  466. /* Set up new standard input, unless we are given an already opened port. */
  467. if (strcmp(tty, "-")) {
  468. struct stat st;
  469. /* Sanity checks... */
  470. if (chdir("/dev"))
  471. error("/dev: chdir() failed: %m");
  472. if (stat(tty, &st) < 0)
  473. error("/dev/%s: %m", tty);
  474. if ((st.st_mode & S_IFMT) != S_IFCHR)
  475. error("/dev/%s: not a character device", tty);
  476. /* Open the tty as standard input. */
  477. (void) close(0);
  478. errno = 0; /* ignore close(2) errors */
  479. debug("open(2)\n");
  480. if (open(tty, O_RDWR | O_NONBLOCK, 0) != 0)
  481. error("/dev/%s: cannot open as standard input: %m", tty);
  482. } else {
  483. /*
  484. * Standard input should already be connected to an open port. Make
  485. * sure it is open for read/write.
  486. */
  487. if ((fcntl(0, F_GETFL, 0) & O_RDWR) != O_RDWR)
  488. error("%s: not open for read/write", tty);
  489. }
  490. /* Set up standard output and standard error file descriptors. */
  491. debug("duping\n");
  492. if (dup(0) != 1 || dup(0) != 2) /* set up stdout and stderr */
  493. error("%s: dup problem: %m", tty); /* we have a problem */
  494. /*
  495. * The following ioctl will fail if stdin is not a tty, but also when
  496. * there is noise on the modem control lines. In the latter case, the
  497. * common course of action is (1) fix your cables (2) give the modem more
  498. * time to properly reset after hanging up. SunOS users can achieve (2)
  499. * by patching the SunOS kernel variable "zsadtrlow" to a larger value;
  500. * 5 seconds seems to be a good value.
  501. */
  502. if (ioctl(0, TCGETA, tp) < 0)
  503. error("%s: ioctl: %m", tty);
  504. /*
  505. * It seems to be a terminal. Set proper protections and ownership. Mode
  506. * 0622 is suitable for SYSV <4 because /bin/login does not change
  507. * protections. SunOS 4 login will change the protections to 0620 (write
  508. * access for group tty) after the login has succeeded.
  509. */
  510. #ifdef DEBIAN
  511. {
  512. /* tty to root.dialout 660 */
  513. struct group *gr;
  514. int id;
  515. id = (gr = getgrnam("dialout")) ? gr->gr_gid : 0;
  516. chown(tty, 0, id);
  517. chmod(tty, 0660);
  518. /* vcs,vcsa to root.sys 600 */
  519. if (!strncmp(tty, "tty", 3) && isdigit(tty[3])) {
  520. char *vcs, *vcsa;
  521. if (!(vcs = strdup(tty)))
  522. error("Can't malloc for vcs");
  523. if (!(vcsa = malloc(strlen(tty) + 2)))
  524. error("Can't malloc for vcsa");
  525. strcpy(vcs, "vcs");
  526. strcpy(vcs + 3, tty + 3);
  527. strcpy(vcsa, "vcsa");
  528. strcpy(vcsa + 4, tty + 3);
  529. id = (gr = getgrnam("sys")) ? gr->gr_gid : 0;
  530. chown(vcs, 0, id);
  531. chmod(vcs, 0600);
  532. chown(vcsa, 0, id);
  533. chmod(vcs, 0600);
  534. free(vcs);
  535. free(vcsa);
  536. }
  537. }
  538. #else
  539. (void) chown(tty, 0, 0); /* root, sys */
  540. (void) chmod(tty, 0622); /* crw--w--w- */
  541. errno = 0; /* ignore above errors */
  542. #endif
  543. }
  544. /* termio_init - initialize termio settings */
  545. static void termio_init(struct termio *tp, int speed, struct options *op)
  546. {
  547. /*
  548. * Initial termio settings: 8-bit characters, raw-mode, blocking i/o.
  549. * Special characters are set after we have read the login name; all
  550. * reads will be done in raw mode anyway. Errors will be dealt with
  551. * lateron.
  552. */
  553. #ifdef __linux__
  554. /* flush input and output queues, important for modems! */
  555. (void) ioctl(0, TCFLSH, TCIOFLUSH);
  556. #endif
  557. tp->c_cflag = CS8 | HUPCL | CREAD | speed;
  558. if (op->flags & F_LOCAL) {
  559. tp->c_cflag |= CLOCAL;
  560. }
  561. tp->c_iflag = tp->c_lflag = tp->c_oflag = tp->c_line = 0;
  562. tp->c_cc[VMIN] = 1;
  563. tp->c_cc[VTIME] = 0;
  564. /* Optionally enable hardware flow control */
  565. #ifdef CRTSCTS
  566. if (op->flags & F_RTSCTS)
  567. tp->c_cflag |= CRTSCTS;
  568. #endif
  569. (void) ioctl(0, TCSETA, tp);
  570. /* go to blocking input even in local mode */
  571. fcntl(0, F_SETFL, fcntl(0, F_GETFL, 0) & ~O_NONBLOCK);
  572. debug("term_io 2\n");
  573. }
  574. /* auto_baud - extract baud rate from modem status message */
  575. static void auto_baud(struct termio *tp)
  576. {
  577. int speed;
  578. int vmin;
  579. unsigned iflag;
  580. char buf[BUFSIZ];
  581. char *bp;
  582. int nread;
  583. /*
  584. * This works only if the modem produces its status code AFTER raising
  585. * the DCD line, and if the computer is fast enough to set the proper
  586. * baud rate before the message has gone by. We expect a message of the
  587. * following format:
  588. *
  589. * <junk><number><junk>
  590. *
  591. * The number is interpreted as the baud rate of the incoming call. If the
  592. * modem does not tell us the baud rate within one second, we will keep
  593. * using the current baud rate. It is advisable to enable BREAK
  594. * processing (comma-separated list of baud rates) if the processing of
  595. * modem status messages is enabled.
  596. */
  597. /*
  598. * Use 7-bit characters, don't block if input queue is empty. Errors will
  599. * be dealt with lateron.
  600. */
  601. iflag = tp->c_iflag;
  602. tp->c_iflag |= ISTRIP; /* enable 8th-bit stripping */
  603. vmin = tp->c_cc[VMIN];
  604. tp->c_cc[VMIN] = 0; /* don't block if queue empty */
  605. (void) ioctl(0, TCSETA, tp);
  606. /*
  607. * Wait for a while, then read everything the modem has said so far and
  608. * try to extract the speed of the dial-in call.
  609. */
  610. (void) sleep(1);
  611. if ((nread = read(0, buf, sizeof(buf) - 1)) > 0) {
  612. buf[nread] = '\0';
  613. for (bp = buf; bp < buf + nread; bp++) {
  614. if (isascii(*bp) && isdigit(*bp)) {
  615. if ((speed = bcode(bp))) {
  616. tp->c_cflag &= ~CBAUD;
  617. tp->c_cflag |= speed;
  618. }
  619. break;
  620. }
  621. }
  622. }
  623. /* Restore terminal settings. Errors will be dealt with lateron. */
  624. tp->c_iflag = iflag;
  625. tp->c_cc[VMIN] = vmin;
  626. (void) ioctl(0, TCSETA, tp);
  627. }
  628. /* do_prompt - show login prompt, optionally preceded by /etc/issue contents */
  629. static void do_prompt(struct options *op, struct termio *tp)
  630. {
  631. #ifdef ISSUE /* optional: show /etc/issue */
  632. print_login_issue(op->issue, op->tty);
  633. #endif
  634. print_login_prompt();
  635. }
  636. /* next_speed - select next baud rate */
  637. static void next_speed(struct termio *tp, struct options *op)
  638. {
  639. static int baud_index = FIRST_SPEED; /* current speed index */
  640. baud_index = (baud_index + 1) % op->numspeed;
  641. tp->c_cflag &= ~CBAUD;
  642. tp->c_cflag |= op->speeds[baud_index];
  643. (void) ioctl(0, TCSETA, tp);
  644. }
  645. /* get_logname - get user name, establish parity, speed, erase, kill, eol */
  646. /* return NULL on failure, logname on success */
  647. static char *get_logname(struct options *op, struct chardata *cp, struct termio *tp)
  648. {
  649. static char logname[BUFSIZ];
  650. char *bp;
  651. char c; /* input character, full eight bits */
  652. char ascval; /* low 7 bits of input character */
  653. int bits; /* # of "1" bits per character */
  654. int mask; /* mask with 1 bit up */
  655. static char *erase[] = { /* backspace-space-backspace */
  656. "\010\040\010", /* space parity */
  657. "\010\040\010", /* odd parity */
  658. "\210\240\210", /* even parity */
  659. "\210\240\210", /* no parity */
  660. };
  661. /* Initialize kill, erase, parity etc. (also after switching speeds). */
  662. *cp = init_chardata;
  663. /* Flush pending input (esp. after parsing or switching the baud rate). */
  664. (void) sleep(1);
  665. (void) ioctl(0, TCFLSH, TCIFLUSH);
  666. /* Prompt for and read a login name. */
  667. for (*logname = 0; *logname == 0; /* void */ ) {
  668. /* Write issue file and prompt, with "parity" bit == 0. */
  669. do_prompt(op, tp);
  670. /* Read name, watch for break, parity, erase, kill, end-of-line. */
  671. for (bp = logname, cp->eol = 0; cp->eol == 0; /* void */ ) {
  672. /* Do not report trivial EINTR/EIO errors. */
  673. if (read(0, &c, 1) < 1) {
  674. if (errno == EINTR || errno == EIO)
  675. exit(0);
  676. error("%s: read: %m", op->tty);
  677. }
  678. /* Do BREAK handling elsewhere. */
  679. if ((c == 0) && op->numspeed > 1)
  680. /* return (0); */
  681. return NULL;
  682. /* Do parity bit handling. */
  683. if (c != (ascval = (c & 0177))) { /* "parity" bit on ? */
  684. for (bits = 1, mask = 1; mask & 0177; mask <<= 1)
  685. if (mask & ascval)
  686. bits++; /* count "1" bits */
  687. cp->parity |= ((bits & 1) ? 1 : 2);
  688. }
  689. /* Do erase, kill and end-of-line processing. */
  690. switch (ascval) {
  691. case CR:
  692. case NL:
  693. *bp = 0; /* terminate logname */
  694. cp->eol = ascval; /* set end-of-line char */
  695. break;
  696. case BS:
  697. case DEL:
  698. case '#':
  699. cp->erase = ascval; /* set erase character */
  700. if (bp > logname) {
  701. (void) write(1, erase[cp->parity], 3);
  702. bp--;
  703. }
  704. break;
  705. case CTL('U'):
  706. case '@':
  707. cp->kill = ascval; /* set kill character */
  708. while (bp > logname) {
  709. (void) write(1, erase[cp->parity], 3);
  710. bp--;
  711. }
  712. break;
  713. case CTL('D'):
  714. exit(0);
  715. default:
  716. if (!isascii(ascval) || !isprint(ascval)) {
  717. /* ignore garbage characters */ ;
  718. } else if (bp - logname >= sizeof(logname) - 1) {
  719. error("%s: input overrun", op->tty);
  720. } else {
  721. (void) write(1, &c, 1); /* echo the character */
  722. *bp++ = ascval; /* and store it */
  723. }
  724. break;
  725. }
  726. }
  727. }
  728. /* Handle names with upper case and no lower case. */
  729. if ((cp->capslock = caps_lock(logname))) {
  730. for (bp = logname; *bp; bp++)
  731. if (isupper(*bp))
  732. *bp = tolower(*bp); /* map name to lower case */
  733. }
  734. return (logname);
  735. }
  736. /* termio_final - set the final tty mode bits */
  737. static void termio_final(struct options *op, struct termio *tp, struct chardata *cp)
  738. {
  739. /* General terminal-independent stuff. */
  740. tp->c_iflag |= IXON | IXOFF; /* 2-way flow control */
  741. tp->c_lflag |= ICANON | ISIG | ECHO | ECHOE | ECHOK | ECHOKE;
  742. /* no longer| ECHOCTL | ECHOPRT */
  743. tp->c_oflag |= OPOST;
  744. /* tp->c_cflag = 0; */
  745. tp->c_cc[VINTR] = DEF_INTR; /* default interrupt */
  746. tp->c_cc[VQUIT] = DEF_QUIT; /* default quit */
  747. tp->c_cc[VEOF] = DEF_EOF; /* default EOF character */
  748. tp->c_cc[VEOL] = DEF_EOL;
  749. tp->c_cc[VSWTC] = DEF_SWITCH; /* default switch character */
  750. /* Account for special characters seen in input. */
  751. if (cp->eol == CR) {
  752. tp->c_iflag |= ICRNL; /* map CR in input to NL */
  753. tp->c_oflag |= ONLCR; /* map NL in output to CR-NL */
  754. }
  755. tp->c_cc[VERASE] = cp->erase; /* set erase character */
  756. tp->c_cc[VKILL] = cp->kill; /* set kill character */
  757. /* Account for the presence or absence of parity bits in input. */
  758. switch (cp->parity) {
  759. case 0: /* space (always 0) parity */
  760. break;
  761. case 1: /* odd parity */
  762. tp->c_cflag |= PARODD;
  763. /* FALLTHROUGH */
  764. case 2: /* even parity */
  765. tp->c_cflag |= PARENB;
  766. tp->c_iflag |= INPCK | ISTRIP;
  767. /* FALLTHROUGH */
  768. case (1 | 2): /* no parity bit */
  769. tp->c_cflag &= ~CSIZE;
  770. tp->c_cflag |= CS7;
  771. break;
  772. }
  773. /* Account for upper case without lower case. */
  774. if (cp->capslock) {
  775. tp->c_iflag |= IUCLC;
  776. tp->c_lflag |= XCASE;
  777. tp->c_oflag |= OLCUC;
  778. }
  779. /* Optionally enable hardware flow control */
  780. #ifdef CRTSCTS
  781. if (op->flags & F_RTSCTS)
  782. tp->c_cflag |= CRTSCTS;
  783. #endif
  784. /* Finally, make the new settings effective */
  785. if (ioctl(0, TCSETA, tp) < 0)
  786. error("%s: ioctl: TCSETA: %m", op->tty);
  787. }
  788. /* caps_lock - string contains upper case without lower case */
  789. /* returns 1 if true, 0 if false */
  790. static int caps_lock(const char *s)
  791. {
  792. int capslock;
  793. for (capslock = 0; *s; s++) {
  794. if (islower(*s))
  795. return (0);
  796. if (capslock == 0)
  797. capslock = isupper(*s);
  798. }
  799. return (capslock);
  800. }
  801. /* bcode - convert speed string to speed code; return 0 on failure */
  802. static int bcode(char *s)
  803. {
  804. int r;
  805. unsigned long value;
  806. if (safe_strtoul(s, &value)) {
  807. return -1;
  808. }
  809. if ((r = bb_value_to_baud(value)) > 0) {
  810. return r;
  811. }
  812. return 0;
  813. }
  814. /* error - report errors to console or syslog; only understands %s and %m */
  815. #define str2cpy(b,s1,s2) strcat(strcpy(b,s1),s2)
  816. /*
  817. * output error messages
  818. */
  819. static void error(const char *fmt, ...)
  820. {
  821. va_list va_alist;
  822. char buf[256], *bp;
  823. #ifndef USE_SYSLOG
  824. int fd;
  825. #endif
  826. #ifdef USE_SYSLOG
  827. buf[0] = '\0';
  828. bp = buf;
  829. #else
  830. strncpy(buf, bb_applet_name, 256);
  831. strncat(buf, ": ", 256);
  832. buf[255] = 0;
  833. bp = buf + strlen(buf);
  834. #endif
  835. va_start(va_alist, fmt);
  836. vsnprintf(bp, 256 - strlen(buf), fmt, va_alist);
  837. buf[255] = 0;
  838. va_end(va_alist);
  839. #ifdef USE_SYSLOG
  840. openlog(bb_applet_name, 0, LOG_AUTH);
  841. syslog(LOG_ERR, "%s", buf);
  842. closelog();
  843. #else
  844. strncat(bp, "\r\n", 256 - strlen(buf));
  845. buf[255] = 0;
  846. if ((fd = open("/dev/console", 1)) >= 0) {
  847. write(fd, buf, strlen(buf));
  848. close(fd);
  849. }
  850. #endif
  851. (void) sleep((unsigned) 10); /* be kind to init(8) */
  852. exit(1);
  853. }