]> git.ipfire.org Git - thirdparty/util-linux.git/blob - term-utils/agetty.c
agetty: add support for /etc/issue.d
[thirdparty/util-linux.git] / term-utils / agetty.c
1 /*
2 * Alternate Getty (agetty) 'agetty' is a versatile, portable, easy to use
3 * replacement for getty on SunOS 4.1.x or the SAC ttymon/ttyadm/sacadm/pmadm
4 * suite on Solaris and other SVR4 systems. 'agetty' was written by Wietse
5 * Venema, enhanced by John DiMarco, and further enhanced by Dennis Cronin.
6 *
7 * Ported to Linux by Peter Orbaek <poe@daimi.aau.dk>
8 * Adopt the mingetty features for a better support
9 * of virtual consoles by Werner Fink <werner@suse.de>
10 *
11 * This program is freely distributable.
12 */
13
14 #include <stdio.h>
15 #include <unistd.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <termios.h>
19 #include <signal.h>
20 #include <errno.h>
21 #include <sys/ioctl.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <sys/wait.h>
25 #include <fcntl.h>
26 #include <stdarg.h>
27 #include <ctype.h>
28 #include <utmpx.h>
29 #include <getopt.h>
30 #include <time.h>
31 #include <sys/socket.h>
32 #include <langinfo.h>
33 #include <grp.h>
34 #include <arpa/inet.h>
35 #include <netdb.h>
36 #include <ifaddrs.h>
37 #include <net/if.h>
38 #include <sys/utsname.h>
39
40 #include "strutils.h"
41 #include "all-io.h"
42 #include "nls.h"
43 #include "pathnames.h"
44 #include "c.h"
45 #include "widechar.h"
46 #include "ttyutils.h"
47 #include "color-names.h"
48 #include "env.h"
49
50 #ifdef USE_PLYMOUTH_SUPPORT
51 # include "plymouth-ctrl.h"
52 #endif
53
54 #ifdef HAVE_SYS_PARAM_H
55 # include <sys/param.h>
56 #endif
57
58 #if defined(__FreeBSD_kernel__)
59 # include <pty.h>
60 # ifdef HAVE_UTMP_H
61 # include <utmp.h>
62 # endif
63 # ifdef HAVE_LIBUTIL_H
64 # include <libutil.h>
65 # endif
66 #endif
67
68 #ifdef __linux__
69 # include <sys/kd.h>
70 # define USE_SYSLOG
71 # ifndef DEFAULT_VCTERM
72 # define DEFAULT_VCTERM "linux"
73 # endif
74 # if defined (__s390__) || defined (__s390x__)
75 # define DEFAULT_TTYS0 "dumb"
76 # define DEFAULT_TTY32 "ibm327x"
77 # define DEFAULT_TTYS1 "vt220"
78 # endif
79 # ifndef DEFAULT_STERM
80 # define DEFAULT_STERM "vt102"
81 # endif
82 #elif defined(__GNU__)
83 # define USE_SYSLOG
84 # ifndef DEFAULT_VCTERM
85 # define DEFAULT_VCTERM "hurd"
86 # endif
87 # ifndef DEFAULT_STERM
88 # define DEFAULT_STERM "vt102"
89 # endif
90 #else
91 # ifndef DEFAULT_VCTERM
92 # define DEFAULT_VCTERM "vt100"
93 # endif
94 # ifndef DEFAULT_STERM
95 # define DEFAULT_STERM "vt100"
96 # endif
97 #endif
98
99 #ifdef __FreeBSD_kernel__
100 #define USE_SYSLOG
101 #endif
102
103 /* If USE_SYSLOG is undefined all diagnostics go to /dev/console. */
104 #ifdef USE_SYSLOG
105 # include <syslog.h>
106 #endif
107
108 /*
109 * Some heuristics to find out what environment we are in: if it is not
110 * System V, assume it is SunOS 4. The LOGIN_PROCESS is defined in System V
111 * utmp.h, which will select System V style getty.
112 */
113 #ifdef LOGIN_PROCESS
114 # define SYSV_STYLE
115 #endif
116
117 /*
118 * Things you may want to modify.
119 *
120 * If ISSUE_SUPPORT is not defined, agetty will never display the contents of
121 * the /etc/issue file. You will not want to spit out large "issue" files at
122 * the wrong baud rate. Relevant for System V only.
123 *
124 * You may disagree with the default line-editing etc. characters defined
125 * below. Note, however, that DEL cannot be used for interrupt generation
126 * and for line editing at the same time.
127 */
128
129 /* Displayed before the login prompt. */
130 #ifdef SYSV_STYLE
131 # define ISSUE_SUPPORT
132 # if defined(HAVE_SCANDIRAT) && defined(HAVE_OPENAT)
133 # include <dirent.h>
134 # include "fileutils.h"
135 # define ISSUEDIR_SUPPORT
136 # define ISSUEDIR_EXT ".issue"
137 # define ISSUEDIR_EXTSIZ (sizeof(ISSUEDIR_EXT) - 1)
138 # endif
139 #endif
140
141 /* Login prompt. */
142 #define LOGIN "login: "
143 #define LOGIN_ARGV_MAX 16 /* Numbers of args for login */
144
145 /*
146 * agetty --reload
147 */
148 #ifdef AGETTY_RELOAD
149 # include <sys/inotify.h>
150 # include <linux/netlink.h>
151 # include <linux/rtnetlink.h>
152 # define AGETTY_RELOAD_FILENAME "/run/agetty.reload" /* trigger file */
153 # define AGETTY_RELOAD_FDNONE -2 /* uninitialized fd */
154 static int inotify_fd = AGETTY_RELOAD_FDNONE;
155 static int netlink_fd = AGETTY_RELOAD_FDNONE;
156 #endif
157
158 /*
159 * When multiple baud rates are specified on the command line, the first one
160 * we will try is the first one specified.
161 */
162 #define FIRST_SPEED 0
163
164 /* Storage for command-line options. */
165 #define MAX_SPEED 10 /* max. nr. of baud rates */
166
167 struct options {
168 int flags; /* toggle switches, see below */
169 unsigned int timeout; /* time-out period */
170 char *autolog; /* login the user automatically */
171 char *chdir; /* Chdir before the login */
172 char *chroot; /* Chroot before the login */
173 char *login; /* login program */
174 char *logopt; /* options for login program */
175 char *tty; /* name of tty */
176 char *vcline; /* line of virtual console */
177 char *term; /* terminal type */
178 char *initstring; /* modem init string */
179 char *issue; /* alternative issue file or directory */
180 char *erasechars; /* string with erase chars */
181 char *killchars; /* string with kill chars */
182 char *osrelease; /* /etc/os-release data */
183 unsigned int delay; /* Sleep seconds before prompt */
184 int nice; /* Run login with this priority */
185 int numspeed; /* number of baud rates to try */
186 int clocal; /* CLOCAL_MODE_* */
187 int kbmode; /* Keyboard mode if virtual console */
188 speed_t speeds[MAX_SPEED]; /* baud rates to be tried */
189 };
190
191 enum {
192 CLOCAL_MODE_AUTO = 0,
193 CLOCAL_MODE_ALWAYS,
194 CLOCAL_MODE_NEVER
195 };
196
197 #define F_PARSE (1<<0) /* process modem status messages */
198 #define F_ISSUE (1<<1) /* display /etc/issue or /etc/issue.d */
199 #define F_RTSCTS (1<<2) /* enable RTS/CTS flow control */
200
201 #define F_INITSTRING (1<<4) /* initstring is set */
202 #define F_WAITCRLF (1<<5) /* wait for CR or LF */
203
204 #define F_NOPROMPT (1<<7) /* do not ask for login name! */
205 #define F_LCUC (1<<8) /* support for *LCUC stty modes */
206 #define F_KEEPSPEED (1<<9) /* follow baud rate from kernel */
207 #define F_KEEPCFLAGS (1<<10) /* reuse c_cflags setup from kernel */
208 #define F_EIGHTBITS (1<<11) /* Assume 8bit-clean tty */
209 #define F_VCONSOLE (1<<12) /* This is a virtual console */
210 #define F_HANGUP (1<<13) /* Do call vhangup(2) */
211 #define F_UTF8 (1<<14) /* We can do UTF8 */
212 #define F_LOGINPAUSE (1<<15) /* Wait for any key before dropping login prompt */
213 #define F_NOCLEAR (1<<16) /* Do not clear the screen before prompting */
214 #define F_NONL (1<<17) /* No newline before issue */
215 #define F_NOHOSTNAME (1<<18) /* Do not show the hostname */
216 #define F_LONGHNAME (1<<19) /* Show Full qualified hostname */
217 #define F_NOHINTS (1<<20) /* Don't print hints */
218 #define F_REMOTE (1<<21) /* Add '-h fakehost' to login(1) command line */
219
220 #define serial_tty_option(opt, flag) \
221 (((opt)->flags & (F_VCONSOLE|(flag))) == (flag))
222
223 struct Speedtab {
224 long speed;
225 speed_t code;
226 };
227
228 static const struct Speedtab speedtab[] = {
229 {50, B50},
230 {75, B75},
231 {110, B110},
232 {134, B134},
233 {150, B150},
234 {200, B200},
235 {300, B300},
236 {600, B600},
237 {1200, B1200},
238 {1800, B1800},
239 {2400, B2400},
240 {4800, B4800},
241 {9600, B9600},
242 #ifdef B19200
243 {19200, B19200},
244 #elif defined(EXTA)
245 {19200, EXTA},
246 #endif
247 #ifdef B38400
248 {38400, B38400},
249 #elif defined(EXTB)
250 {38400, EXTB},
251 #endif
252 #ifdef B57600
253 {57600, B57600},
254 #endif
255 #ifdef B115200
256 {115200, B115200},
257 #endif
258 #ifdef B230400
259 {230400, B230400},
260 #endif
261 #ifdef B460800
262 {460800, B460800},
263 #endif
264 #ifdef B500000
265 {500000, B500000},
266 #endif
267 #ifdef B576000
268 {576000, B576000},
269 #endif
270 #ifdef B921600
271 {921600, B921600},
272 #endif
273 #ifdef B1000000
274 {1000000, B1000000},
275 #endif
276 #ifdef B1152000
277 {1152000, B1152000},
278 #endif
279 #ifdef B1500000
280 {1500000, B1500000},
281 #endif
282 #ifdef B2000000
283 {2000000, B2000000},
284 #endif
285 #ifdef B2500000
286 {2500000, B2500000},
287 #endif
288 #ifdef B3000000
289 {3000000, B3000000},
290 #endif
291 #ifdef B3500000
292 {3500000, B3500000},
293 #endif
294 #ifdef B4000000
295 {4000000, B4000000},
296 #endif
297 {0, 0},
298 };
299
300 static void init_special_char(char* arg, struct options *op);
301 static void parse_args(int argc, char **argv, struct options *op);
302 static void parse_speeds(struct options *op, char *arg);
303 static void update_utmp(struct options *op);
304 static void open_tty(char *tty, struct termios *tp, struct options *op);
305 static void termio_init(struct options *op, struct termios *tp);
306 static void reset_vc (const struct options *op, struct termios *tp);
307 static void auto_baud(struct termios *tp);
308 static void list_speeds(void);
309 static void output_special_char (unsigned char c, struct options *op,
310 struct termios *tp, FILE *fp);
311 static void do_prompt(struct options *op, struct termios *tp);
312 static void next_speed(struct options *op, struct termios *tp);
313 static char *get_logname(struct options *op,
314 struct termios *tp, struct chardata *cp);
315 static void termio_final(struct options *op,
316 struct termios *tp, struct chardata *cp);
317 static int caps_lock(char *s);
318 static speed_t bcode(char *s);
319 static void usage(void) __attribute__((__noreturn__));
320 static void log_err(const char *, ...) __attribute__((__noreturn__))
321 __attribute__((__format__(printf, 1, 2)));
322 static void log_warn (const char *, ...)
323 __attribute__((__format__(printf, 1, 2)));
324 static ssize_t append(char *dest, size_t len, const char *sep, const char *src);
325 static void check_username (const char* nm);
326 static void login_options_to_argv(char *argv[], int *argc, char *str, char *username);
327 static void reload_agettys(void);
328 static void print_issue_file(struct options *op, struct termios *tp);
329
330 /* Fake hostname for ut_host specified on command line. */
331 static char *fakehost;
332
333 #ifdef DEBUGGING
334 # include "closestream.h"
335 # ifndef DEBUG_OUTPUT
336 # define DEBUG_OUTPUT "/dev/tty10"
337 # endif
338 # define debug(s) do { fprintf(dbf,s); fflush(dbf); } while (0)
339 FILE *dbf;
340 #else
341 # define debug(s) do { ; } while (0)
342 #endif
343
344 int main(int argc, char **argv)
345 {
346 char *username = NULL; /* login name, given to /bin/login */
347 struct chardata chardata; /* will be set by get_logname() */
348 struct termios termios; /* terminal mode bits */
349 struct options options = {
350 .flags = F_ISSUE, /* show /etc/issue (SYSV_STYLE) */
351 .login = _PATH_LOGIN, /* default login program */
352 .tty = "tty1" /* default tty line */
353 };
354 char *login_argv[LOGIN_ARGV_MAX + 1];
355 int login_argc = 0;
356 struct sigaction sa, sa_hup, sa_quit, sa_int;
357 sigset_t set;
358
359 setlocale(LC_ALL, "");
360 bindtextdomain(PACKAGE, LOCALEDIR);
361 textdomain(PACKAGE);
362
363 /* In case vhangup(2) has to called */
364 sa.sa_handler = SIG_IGN;
365 sa.sa_flags = SA_RESTART;
366 sigemptyset (&sa.sa_mask);
367 sigaction(SIGHUP, &sa, &sa_hup);
368 sigaction(SIGQUIT, &sa, &sa_quit);
369 sigaction(SIGINT, &sa, &sa_int);
370
371 #ifdef DEBUGGING
372 dbf = fopen(DEBUG_OUTPUT, "w");
373 for (int i = 1; i < argc; i++) {
374 if (i > 1)
375 debug(" ");
376 debug(argv[i]);
377 }
378 debug("\n");
379 #endif /* DEBUGGING */
380
381 /* Parse command-line arguments. */
382 parse_args(argc, argv, &options);
383
384 login_argv[login_argc++] = options.login; /* set login program name */
385
386 /* Update the utmp file. */
387 #ifdef SYSV_STYLE
388 update_utmp(&options);
389 #endif
390 if (options.delay)
391 sleep(options.delay);
392
393 debug("calling open_tty\n");
394
395 /* Open the tty as standard { input, output, error }. */
396 open_tty(options.tty, &termios, &options);
397
398 /* Unmask SIGHUP if inherited */
399 sigemptyset(&set);
400 sigaddset(&set, SIGHUP);
401 sigprocmask(SIG_UNBLOCK, &set, NULL);
402 sigaction(SIGHUP, &sa_hup, NULL);
403
404 tcsetpgrp(STDIN_FILENO, getpid());
405
406 /* Default is to follow the current line speed and then default to 9600 */
407 if ((options.flags & F_VCONSOLE) == 0 && options.numspeed == 0) {
408 options.speeds[options.numspeed++] = bcode("9600");
409 options.flags |= F_KEEPSPEED;
410 }
411
412 /* Initialize the termios settings (raw mode, eight-bit, blocking i/o). */
413 debug("calling termio_init\n");
414 termio_init(&options, &termios);
415
416 /* Write the modem init string and DO NOT flush the buffers. */
417 if (serial_tty_option(&options, F_INITSTRING) &&
418 options.initstring && *options.initstring != '\0') {
419 debug("writing init string\n");
420 write_all(STDOUT_FILENO, options.initstring,
421 strlen(options.initstring));
422 }
423
424 if (options.flags & F_VCONSOLE || options.clocal != CLOCAL_MODE_ALWAYS)
425 /* Go to blocking mode unless -L is specified, this change
426 * affects stdout, stdin and stderr as all the file descriptors
427 * are created by dup(). */
428 fcntl(STDOUT_FILENO, F_SETFL,
429 fcntl(STDOUT_FILENO, F_GETFL, 0) & ~O_NONBLOCK);
430
431 /* Optionally detect the baud rate from the modem status message. */
432 debug("before autobaud\n");
433 if (serial_tty_option(&options, F_PARSE))
434 auto_baud(&termios);
435
436 /* Set the optional timer. */
437 if (options.timeout)
438 alarm(options.timeout);
439
440 /* Optionally wait for CR or LF before writing /etc/issue */
441 if (serial_tty_option(&options, F_WAITCRLF)) {
442 char ch;
443
444 debug("waiting for cr-lf\n");
445 while (read(STDIN_FILENO, &ch, 1) == 1) {
446 /* Strip "parity bit". */
447 ch &= 0x7f;
448 #ifdef DEBUGGING
449 fprintf(dbf, "read %c\n", ch);
450 #endif
451 if (ch == '\n' || ch == '\r')
452 break;
453 }
454 }
455
456 INIT_CHARDATA(&chardata);
457
458 if (options.autolog) {
459 debug("doing auto login\n");
460 username = options.autolog;
461 }
462
463 if (options.flags & F_NOPROMPT) { /* --skip-login */
464 print_issue_file(&options, &termios);
465 } else { /* regular (auto)login */
466 if (options.autolog) {
467 /* Autologin prompt */
468 do_prompt(&options, &termios);
469 printf(_("%s%s (automatic login)\n"), LOGIN, options.autolog);
470 } else {
471 /* Read the login name. */
472 debug("reading login name\n");
473 while ((username =
474 get_logname(&options, &termios, &chardata)) == NULL)
475 if ((options.flags & F_VCONSOLE) == 0 && options.numspeed)
476 next_speed(&options, &termios);
477 }
478 }
479
480 /* Disable timer. */
481 if (options.timeout)
482 alarm(0);
483
484 if ((options.flags & F_VCONSOLE) == 0) {
485 /* Finalize the termios settings. */
486 termio_final(&options, &termios, &chardata);
487
488 /* Now the newline character should be properly written. */
489 write_all(STDOUT_FILENO, "\r\n", 2);
490 }
491
492 sigaction(SIGQUIT, &sa_quit, NULL);
493 sigaction(SIGINT, &sa_int, NULL);
494
495 if (username)
496 check_username(username);
497
498 if (options.logopt) {
499 /*
500 * The --login-options completely overwrites the default
501 * way how agetty composes login(1) command line.
502 */
503 login_options_to_argv(login_argv, &login_argc,
504 options.logopt, username);
505 } else {
506 if (options.flags & F_REMOTE) {
507 if (fakehost) {
508 login_argv[login_argc++] = "-h";
509 login_argv[login_argc++] = fakehost;
510 } else if (options.flags & F_NOHOSTNAME)
511 login_argv[login_argc++] = "-H";
512 }
513 if (username) {
514 if (options.autolog)
515 login_argv[login_argc++] = "-f";
516 else
517 login_argv[login_argc++] = "--";
518 login_argv[login_argc++] = username;
519 }
520 }
521
522 login_argv[login_argc] = NULL; /* last login argv */
523
524 if (options.chroot && chroot(options.chroot) < 0)
525 log_err(_("%s: can't change root directory %s: %m"),
526 options.tty, options.chroot);
527 if (options.chdir && chdir(options.chdir) < 0)
528 log_err(_("%s: can't change working directory %s: %m"),
529 options.tty, options.chdir);
530 if (options.nice && nice(options.nice) < 0)
531 log_warn(_("%s: can't change process priority: %m"),
532 options.tty);
533
534 free(options.osrelease);
535 #ifdef DEBUGGING
536 if (close_stream(dbf) != 0)
537 log_err("write failed: %s", DEBUG_OUTPUT);
538 #endif
539
540 /* Let the login program take care of password validation. */
541 execv(options.login, login_argv);
542 log_err(_("%s: can't exec %s: %m"), options.tty, login_argv[0]);
543 }
544
545 /*
546 * Returns : @str if \u not found
547 * : @username if @str equal to "\u"
548 * : newly allocated string if \u mixed with something other
549 */
550 static char *replace_u(char *str, char *username)
551 {
552 char *entry = NULL, *p = str;
553 size_t usz = username ? strlen(username) : 0;
554
555 while (*p) {
556 size_t sz;
557 char *tp, *old = entry;
558
559 if (memcmp(p, "\\u", 2)) {
560 p++;
561 continue; /* no \u */
562 }
563 sz = strlen(str);
564
565 if (p == str && sz == 2) {
566 /* 'str' contains only '\u' */
567 free(old);
568 return username;
569 }
570
571 tp = entry = malloc(sz + usz);
572 if (!tp)
573 log_err(_("failed to allocate memory: %m"));
574
575 if (p != str) {
576 /* copy chars before \u */
577 memcpy(tp, str, p - str);
578 tp += p - str;
579 }
580 if (usz) {
581 /* copy username */
582 memcpy(tp, username, usz);
583 tp += usz;
584 }
585 if (*(p + 2))
586 /* copy chars after \u + \0 */
587 memcpy(tp, p + 2, sz - (p - str) - 1);
588 else
589 *tp = '\0';
590
591 p = tp;
592 str = entry;
593 free(old);
594 }
595
596 return entry ? entry : str;
597 }
598
599 static void login_options_to_argv(char *argv[], int *argc,
600 char *str, char *username)
601 {
602 char *p;
603 int i = *argc;
604
605 while (str && isspace(*str))
606 str++;
607 p = str;
608
609 while (p && *p && i < LOGIN_ARGV_MAX) {
610 if (isspace(*p)) {
611 *p = '\0';
612 while (isspace(*++p))
613 ;
614 if (*p) {
615 argv[i++] = replace_u(str, username);
616 str = p;
617 }
618 } else
619 p++;
620 }
621 if (str && *str && i < LOGIN_ARGV_MAX)
622 argv[i++] = replace_u(str, username);
623 *argc = i;
624 }
625
626 static void output_version(void)
627 {
628 static const char *features[] = {
629 #ifdef DEBUGGING
630 "debug",
631 #endif
632 #ifdef CRTSCTS
633 "flow control",
634 #endif
635 #ifdef KDGKBLED
636 "hints",
637 #endif
638 #ifdef ISSUE_SUPPORT
639 "issue",
640 #endif
641 #ifdef ISSUEDIR_SUPPORT
642 "issue.d",
643 #endif
644 #ifdef KDGKBMODE
645 "keyboard mode",
646 #endif
647 #ifdef USE_PLYMOUTH_SUPPORT
648 "plymouth",
649 #endif
650 #ifdef AGETTY_RELOAD
651 "reload",
652 #endif
653 #ifdef USE_SYSLOG
654 "syslog",
655 #endif
656 #ifdef HAVE_WIDECHAR
657 "widechar",
658 #endif
659 NULL
660 };
661 unsigned int i;
662
663 printf( _("%s from %s"), program_invocation_short_name, PACKAGE_STRING);
664 fputs(" (", stdout);
665 for (i = 0; features[i]; i++) {
666 if (0 < i)
667 fputs(", ", stdout);
668 printf("%s", features[i]);
669 }
670 fputs(")\n", stdout);
671 }
672
673 #define is_speed(str) (strlen((str)) == strspn((str), "0123456789,"))
674
675 /* Parse command-line arguments. */
676 static void parse_args(int argc, char **argv, struct options *op)
677 {
678 int c;
679
680 enum {
681 VERSION_OPTION = CHAR_MAX + 1,
682 NOHINTS_OPTION,
683 NOHOSTNAME_OPTION,
684 LONGHOSTNAME_OPTION,
685 HELP_OPTION,
686 ERASE_CHARS_OPTION,
687 KILL_CHARS_OPTION,
688 RELOAD_OPTION,
689 LIST_SPEEDS_OPTION,
690 };
691 const struct option longopts[] = {
692 { "8bits", no_argument, NULL, '8' },
693 { "autologin", required_argument, NULL, 'a' },
694 { "noreset", no_argument, NULL, 'c' },
695 { "chdir", required_argument, NULL, 'C' },
696 { "delay", required_argument, NULL, 'd' },
697 { "remote", no_argument, NULL, 'E' },
698 { "issue-file", required_argument, NULL, 'f' },
699 { "flow-control", no_argument, NULL, 'h' },
700 { "host", required_argument, NULL, 'H' },
701 { "noissue", no_argument, NULL, 'i' },
702 { "init-string", required_argument, NULL, 'I' },
703 { "noclear", no_argument, NULL, 'J' },
704 { "login-program", required_argument, NULL, 'l' },
705 { "local-line", optional_argument, NULL, 'L' },
706 { "extract-baud", no_argument, NULL, 'm' },
707 { "list-speeds", no_argument, NULL, LIST_SPEEDS_OPTION },
708 { "skip-login", no_argument, NULL, 'n' },
709 { "nonewline", no_argument, NULL, 'N' },
710 { "login-options", required_argument, NULL, 'o' },
711 { "login-pause", no_argument, NULL, 'p' },
712 { "nice", required_argument, NULL, 'P' },
713 { "chroot", required_argument, NULL, 'r' },
714 { "hangup", no_argument, NULL, 'R' },
715 { "keep-baud", no_argument, NULL, 's' },
716 { "timeout", required_argument, NULL, 't' },
717 { "detect-case", no_argument, NULL, 'U' },
718 { "wait-cr", no_argument, NULL, 'w' },
719 { "nohints", no_argument, NULL, NOHINTS_OPTION },
720 { "nohostname", no_argument, NULL, NOHOSTNAME_OPTION },
721 { "long-hostname", no_argument, NULL, LONGHOSTNAME_OPTION },
722 { "reload", no_argument, NULL, RELOAD_OPTION },
723 { "version", no_argument, NULL, VERSION_OPTION },
724 { "help", no_argument, NULL, HELP_OPTION },
725 { "erase-chars", required_argument, NULL, ERASE_CHARS_OPTION },
726 { "kill-chars", required_argument, NULL, KILL_CHARS_OPTION },
727 { NULL, 0, NULL, 0 }
728 };
729
730 while ((c = getopt_long(argc, argv,
731 "8a:cC:d:Ef:hH:iI:Jl:L::mnNo:pP:r:Rst:Uw", longopts,
732 NULL)) != -1) {
733 switch (c) {
734 case '8':
735 op->flags |= F_EIGHTBITS;
736 break;
737 case 'a':
738 op->autolog = optarg;
739 break;
740 case 'c':
741 op->flags |= F_KEEPCFLAGS;
742 break;
743 case 'C':
744 op->chdir = optarg;
745 break;
746 case 'd':
747 op->delay = strtou32_or_err(optarg, _("invalid delay argument"));
748 break;
749 case 'E':
750 op->flags |= F_REMOTE;
751 break;
752 case 'f':
753 op->issue = optarg;
754 break;
755 case 'h':
756 op->flags |= F_RTSCTS;
757 break;
758 case 'H':
759 fakehost = optarg;
760 break;
761 case 'i':
762 op->flags &= ~F_ISSUE;
763 break;
764 case 'I':
765 init_special_char(optarg, op);
766 op->flags |= F_INITSTRING;
767 break;
768 case 'J':
769 op->flags |= F_NOCLEAR;
770 break;
771 case 'l':
772 op->login = optarg;
773 break;
774 case 'L':
775 /* -L and -L=always have the same meaning */
776 op->clocal = CLOCAL_MODE_ALWAYS;
777 if (optarg) {
778 if (strcmp(optarg, "=always") == 0)
779 op->clocal = CLOCAL_MODE_ALWAYS;
780 else if (strcmp(optarg, "=never") == 0)
781 op->clocal = CLOCAL_MODE_NEVER;
782 else if (strcmp(optarg, "=auto") == 0)
783 op->clocal = CLOCAL_MODE_AUTO;
784 else
785 log_err(_("invalid argument of --local-line"));
786 }
787 break;
788 case 'm':
789 op->flags |= F_PARSE;
790 break;
791 case 'n':
792 op->flags |= F_NOPROMPT;
793 break;
794 case 'N':
795 op->flags |= F_NONL;
796 break;
797 case 'o':
798 op->logopt = optarg;
799 break;
800 case 'p':
801 op->flags |= F_LOGINPAUSE;
802 break;
803 case 'P':
804 op->nice = strtos32_or_err(optarg, _("invalid nice argument"));
805 break;
806 case 'r':
807 op->chroot = optarg;
808 break;
809 case 'R':
810 op->flags |= F_HANGUP;
811 break;
812 case 's':
813 op->flags |= F_KEEPSPEED;
814 break;
815 case 't':
816 op->timeout = strtou32_or_err(optarg, _("invalid timeout argument"));
817 break;
818 case 'U':
819 op->flags |= F_LCUC;
820 break;
821 case 'w':
822 op->flags |= F_WAITCRLF;
823 break;
824 case NOHINTS_OPTION:
825 op->flags |= F_NOHINTS;
826 break;
827 case NOHOSTNAME_OPTION:
828 op->flags |= F_NOHOSTNAME;
829 break;
830 case LONGHOSTNAME_OPTION:
831 op->flags |= F_LONGHNAME;
832 break;
833 case ERASE_CHARS_OPTION:
834 op->erasechars = optarg;
835 break;
836 case KILL_CHARS_OPTION:
837 op->killchars = optarg;
838 break;
839 case RELOAD_OPTION:
840 reload_agettys();
841 exit(EXIT_SUCCESS);
842 case LIST_SPEEDS_OPTION:
843 list_speeds();
844 exit(EXIT_SUCCESS);
845 case VERSION_OPTION:
846 output_version();
847 exit(EXIT_SUCCESS);
848 case HELP_OPTION:
849 usage();
850 default:
851 errtryhelp(EXIT_FAILURE);
852 }
853 }
854
855 debug("after getopt loop\n");
856
857 if (argc < optind + 1) {
858 log_warn(_("not enough arguments"));
859 errx(EXIT_FAILURE, _("not enough arguments"));
860 }
861
862 /* Accept "tty", "baudrate tty", and "tty baudrate". */
863 if (is_speed(argv[optind])) {
864 /* Assume BSD style speed. */
865 parse_speeds(op, argv[optind++]);
866 if (argc < optind + 1) {
867 log_warn(_("not enough arguments"));
868 errx(EXIT_FAILURE, _("not enough arguments"));
869 }
870 op->tty = argv[optind++];
871 } else {
872 op->tty = argv[optind++];
873 if (argc > optind) {
874 char *v = argv[optind];
875 if (is_speed(v)) {
876 parse_speeds(op, v);
877 optind++;
878 }
879 }
880 }
881
882 /* On virtual console remember the line which is used for */
883 if (strncmp(op->tty, "tty", 3) == 0 &&
884 strspn(op->tty + 3, "0123456789") == strlen(op->tty+3))
885 op->vcline = op->tty+3;
886
887 if (argc > optind && argv[optind])
888 op->term = argv[optind];
889
890 debug("exiting parseargs\n");
891 }
892
893 /* Parse alternate baud rates. */
894 static void parse_speeds(struct options *op, char *arg)
895 {
896 char *cp;
897 char *str = strdup(arg);
898
899 if (!str)
900 log_err(_("failed to allocate memory: %m"));
901
902 debug("entered parse_speeds:\n");
903 for (cp = strtok(str, ","); cp != NULL; cp = strtok((char *)0, ",")) {
904 if ((op->speeds[op->numspeed++] = bcode(cp)) <= 0)
905 log_err(_("bad speed: %s"), cp);
906 if (op->numspeed >= MAX_SPEED)
907 log_err(_("too many alternate speeds"));
908 }
909 debug("exiting parsespeeds\n");
910 free(str);
911 }
912
913 #ifdef SYSV_STYLE
914
915 /* Update our utmp entry. */
916 static void update_utmp(struct options *op)
917 {
918 struct utmpx ut;
919 time_t t;
920 pid_t pid = getpid();
921 pid_t sid = getsid(0);
922 char *vcline = op->vcline;
923 char *line = op->tty;
924 struct utmpx *utp;
925
926 /*
927 * The utmp file holds miscellaneous information about things started by
928 * /sbin/init and other system-related events. Our purpose is to update
929 * the utmp entry for the current process, in particular the process type
930 * and the tty line we are listening to. Return successfully only if the
931 * utmp file can be opened for update, and if we are able to find our
932 * entry in the utmp file.
933 */
934 utmpxname(_PATH_UTMP);
935 setutxent();
936
937 /*
938 * Find my pid in utmp.
939 *
940 * FIXME: Earlier (when was that?) code here tested only utp->ut_type !=
941 * INIT_PROCESS, so maybe the >= here should be >.
942 *
943 * FIXME: The present code is taken from login.c, so if this is changed,
944 * maybe login has to be changed as well (is this true?).
945 */
946 while ((utp = getutxent()))
947 if (utp->ut_pid == pid
948 && utp->ut_type >= INIT_PROCESS
949 && utp->ut_type <= DEAD_PROCESS)
950 break;
951
952 if (utp) {
953 memcpy(&ut, utp, sizeof(ut));
954 } else {
955 /* Some inits do not initialize utmp. */
956 memset(&ut, 0, sizeof(ut));
957 if (vcline && *vcline)
958 /* Standard virtual console devices */
959 strncpy(ut.ut_id, vcline, sizeof(ut.ut_id));
960 else {
961 size_t len = strlen(line);
962 char * ptr;
963 if (len >= sizeof(ut.ut_id))
964 ptr = line + len - sizeof(ut.ut_id);
965 else
966 ptr = line;
967 strncpy(ut.ut_id, ptr, sizeof(ut.ut_id));
968 }
969 }
970
971 strncpy(ut.ut_user, "LOGIN", sizeof(ut.ut_user));
972 strncpy(ut.ut_line, line, sizeof(ut.ut_line));
973 if (fakehost)
974 strncpy(ut.ut_host, fakehost, sizeof(ut.ut_host));
975 time(&t);
976 ut.ut_tv.tv_sec = t;
977 ut.ut_type = LOGIN_PROCESS;
978 ut.ut_pid = pid;
979 ut.ut_session = sid;
980
981 pututxline(&ut);
982 endutxent();
983
984 updwtmpx(_PATH_WTMP, &ut);
985 }
986
987 #endif /* SYSV_STYLE */
988
989 /* Set up tty as stdin, stdout & stderr. */
990 static void open_tty(char *tty, struct termios *tp, struct options *op)
991 {
992 const pid_t pid = getpid();
993 int closed = 0;
994 #ifndef KDGKBMODE
995 int serial;
996 #endif
997
998 /* Set up new standard input, unless we are given an already opened port. */
999
1000 if (strcmp(tty, "-") != 0) {
1001 char buf[PATH_MAX+1];
1002 struct group *gr = NULL;
1003 struct stat st;
1004 int fd, len;
1005 pid_t tid;
1006 gid_t gid = 0;
1007
1008 /* Use tty group if available */
1009 if ((gr = getgrnam("tty")))
1010 gid = gr->gr_gid;
1011
1012 len = snprintf(buf, sizeof(buf), "/dev/%s", tty);
1013 if (len < 0 || (size_t)len >= sizeof(buf))
1014 log_err(_("/dev/%s: cannot open as standard input: %m"), tty);
1015
1016 /* Open the tty as standard input. */
1017 if ((fd = open(buf, O_RDWR|O_NOCTTY|O_NONBLOCK, 0)) < 0)
1018 log_err(_("/dev/%s: cannot open as standard input: %m"), tty);
1019
1020 /*
1021 * There is always a race between this reset and the call to
1022 * vhangup() that s.o. can use to get access to your tty.
1023 * Linux login(1) will change tty permissions. Use root owner and group
1024 * with permission -rw------- for the period between getty and login.
1025 */
1026 if (fchown(fd, 0, gid) || fchmod(fd, (gid ? 0620 : 0600))) {
1027 if (errno == EROFS)
1028 log_warn("%s: %m", buf);
1029 else
1030 log_err("%s: %m", buf);
1031 }
1032
1033 /* Sanity checks... */
1034 if (fstat(fd, &st) < 0)
1035 log_err("%s: %m", buf);
1036 if ((st.st_mode & S_IFMT) != S_IFCHR)
1037 log_err(_("/dev/%s: not a character device"), tty);
1038 if (!isatty(fd))
1039 log_err(_("/dev/%s: not a tty"), tty);
1040
1041 if (((tid = tcgetsid(fd)) < 0) || (pid != tid)) {
1042 if (ioctl(fd, TIOCSCTTY, 1) == -1)
1043 log_warn(_("/dev/%s: cannot get controlling tty: %m"), tty);
1044 }
1045
1046 close(STDIN_FILENO);
1047 errno = 0;
1048
1049 if (op->flags & F_HANGUP) {
1050
1051 if (ioctl(fd, TIOCNOTTY))
1052 debug("TIOCNOTTY ioctl failed\n");
1053
1054 /*
1055 * Let's close all file descriptors before vhangup
1056 * https://lkml.org/lkml/2012/6/5/145
1057 */
1058 close(fd);
1059 close(STDOUT_FILENO);
1060 close(STDERR_FILENO);
1061 errno = 0;
1062 closed = 1;
1063
1064 if (vhangup())
1065 log_err(_("/dev/%s: vhangup() failed: %m"), tty);
1066 } else
1067 close(fd);
1068
1069 debug("open(2)\n");
1070 if (open(buf, O_RDWR|O_NOCTTY|O_NONBLOCK, 0) != 0)
1071 log_err(_("/dev/%s: cannot open as standard input: %m"), tty);
1072
1073 if (((tid = tcgetsid(STDIN_FILENO)) < 0) || (pid != tid)) {
1074 if (ioctl(STDIN_FILENO, TIOCSCTTY, 1) == -1)
1075 log_warn(_("/dev/%s: cannot get controlling tty: %m"), tty);
1076 }
1077
1078 } else {
1079
1080 /*
1081 * Standard input should already be connected to an open port. Make
1082 * sure it is open for read/write.
1083 */
1084
1085 if ((fcntl(STDIN_FILENO, F_GETFL, 0) & O_RDWR) != O_RDWR)
1086 log_err(_("%s: not open for read/write"), tty);
1087
1088 }
1089
1090 if (tcsetpgrp(STDIN_FILENO, pid))
1091 log_warn(_("/dev/%s: cannot set process group: %m"), tty);
1092
1093 /* Get rid of the present outputs. */
1094 if (!closed) {
1095 close(STDOUT_FILENO);
1096 close(STDERR_FILENO);
1097 errno = 0;
1098 }
1099
1100 /* Set up standard output and standard error file descriptors. */
1101 debug("duping\n");
1102
1103 /* set up stdout and stderr */
1104 if (dup(STDIN_FILENO) != 1 || dup(STDIN_FILENO) != 2)
1105 log_err(_("%s: dup problem: %m"), tty);
1106
1107 /* make stdio unbuffered for slow modem lines */
1108 setvbuf(stdout, NULL, _IONBF, 0);
1109
1110 /*
1111 * The following ioctl will fail if stdin is not a tty, but also when
1112 * there is noise on the modem control lines. In the latter case, the
1113 * common course of action is (1) fix your cables (2) give the modem
1114 * more time to properly reset after hanging up.
1115 *
1116 * SunOS users can achieve (2) by patching the SunOS kernel variable
1117 * "zsadtrlow" to a larger value; 5 seconds seems to be a good value.
1118 * http://www.sunmanagers.org/archives/1993/0574.html
1119 */
1120 memset(tp, 0, sizeof(struct termios));
1121 if (tcgetattr(STDIN_FILENO, tp) < 0)
1122 log_err(_("%s: failed to get terminal attributes: %m"), tty);
1123
1124 #if defined (__s390__) || defined (__s390x__)
1125 if (!op->term) {
1126 /*
1127 * Special terminal on first serial line on a S/390(x) which
1128 * is due legacy reasons a block terminal of type 3270 or
1129 * higher. Whereas the second serial line on a S/390(x) is
1130 * a real character terminal which is compatible with VT220.
1131 */
1132 if (strcmp(op->tty, "ttyS0") == 0) /* linux/drivers/s390/char/con3215.c */
1133 op->term = DEFAULT_TTYS0;
1134 else if (strncmp(op->tty, "3270/tty", 8) == 0) /* linux/drivers/s390/char/con3270.c */
1135 op->term = DEFAULT_TTY32;
1136 else if (strcmp(op->tty, "ttyS1") == 0) /* linux/drivers/s390/char/sclp_vt220.c */
1137 op->term = DEFAULT_TTYS1;
1138 }
1139 #endif
1140
1141 #if defined(__FreeBSD_kernel__)
1142 login_tty (0);
1143 #endif
1144
1145 /*
1146 * Detect if this is a virtual console or serial/modem line.
1147 * In case of a virtual console the ioctl KDGKBMODE succeeds
1148 * whereas on other lines it will fails.
1149 */
1150 #ifdef KDGKBMODE
1151 if (ioctl(STDIN_FILENO, KDGKBMODE, &op->kbmode) == 0)
1152 #else
1153 if (ioctl(STDIN_FILENO, TIOCMGET, &serial) < 0 && (errno == EINVAL))
1154 #endif
1155 {
1156 op->flags |= F_VCONSOLE;
1157 if (!op->term)
1158 op->term = DEFAULT_VCTERM;
1159 } else {
1160 #ifdef K_RAW
1161 op->kbmode = K_RAW;
1162 #endif
1163 if (!op->term)
1164 op->term = DEFAULT_STERM;
1165 }
1166
1167 if (setenv("TERM", op->term, 1) != 0)
1168 log_err(_("failed to set the %s environment variable"), "TERM");
1169 }
1170
1171 /* Initialize termios settings. */
1172 static void termio_clear(int fd)
1173 {
1174 /*
1175 * Do not write a full reset (ESC c) because this destroys
1176 * the unicode mode again if the terminal was in unicode
1177 * mode. Also it clears the CONSOLE_MAGIC features which
1178 * are required for some languages/console-fonts.
1179 * Just put the cursor to the home position (ESC [ H),
1180 * erase everything below the cursor (ESC [ J), and set the
1181 * scrolling region to the full window (ESC [ r)
1182 */
1183 write_all(fd, "\033[r\033[H\033[J", 9);
1184 }
1185
1186 /* Initialize termios settings. */
1187 static void termio_init(struct options *op, struct termios *tp)
1188 {
1189 speed_t ispeed, ospeed;
1190 struct winsize ws;
1191 #ifdef USE_PLYMOUTH_SUPPORT
1192 struct termios lock;
1193 int i = (plymouth_command(MAGIC_PING) == 0) ? PLYMOUTH_TERMIOS_FLAGS_DELAY : 0;
1194 if (i)
1195 plymouth_command(MAGIC_QUIT);
1196 while (i-- > 0) {
1197 /*
1198 * Even with TTYReset=no it seems with systemd or plymouth
1199 * the termios flags become changed from under the first
1200 * agetty on a serial system console as the flags are locked.
1201 */
1202 memset(&lock, 0, sizeof(struct termios));
1203 if (ioctl(STDIN_FILENO, TIOCGLCKTRMIOS, &lock) < 0)
1204 break;
1205 if (!lock.c_iflag && !lock.c_oflag && !lock.c_cflag && !lock.c_lflag)
1206 break;
1207 debug("termios locked\n");
1208 sleep(1);
1209 }
1210 memset(&lock, 0, sizeof(struct termios));
1211 ioctl(STDIN_FILENO, TIOCSLCKTRMIOS, &lock);
1212 #endif
1213
1214 if (op->flags & F_VCONSOLE) {
1215 #if defined(IUTF8) && defined(KDGKBMODE)
1216 switch(op->kbmode) {
1217 case K_UNICODE:
1218 setlocale(LC_CTYPE, "C.UTF-8");
1219 op->flags |= F_UTF8;
1220 break;
1221 case K_RAW:
1222 case K_MEDIUMRAW:
1223 case K_XLATE:
1224 default:
1225 setlocale(LC_CTYPE, "POSIX");
1226 op->flags &= ~F_UTF8;
1227 break;
1228 }
1229 #else
1230 setlocale(LC_CTYPE, "POSIX");
1231 op->flags &= ~F_UTF8;
1232 #endif
1233 reset_vc(op, tp);
1234
1235 if ((tp->c_cflag & (CS8|PARODD|PARENB)) == CS8)
1236 op->flags |= F_EIGHTBITS;
1237
1238 if ((op->flags & F_NOCLEAR) == 0)
1239 termio_clear(STDOUT_FILENO);
1240 return;
1241 }
1242
1243 /*
1244 * Serial line
1245 */
1246
1247 if (op->flags & F_KEEPSPEED || !op->numspeed) {
1248 /* Save the original setting. */
1249 ispeed = cfgetispeed(tp);
1250 ospeed = cfgetospeed(tp);
1251
1252 if (!ispeed) ispeed = TTYDEF_SPEED;
1253 if (!ospeed) ospeed = TTYDEF_SPEED;
1254
1255 } else {
1256 ospeed = ispeed = op->speeds[FIRST_SPEED];
1257 }
1258
1259 /*
1260 * Initial termios settings: 8-bit characters, raw-mode, blocking i/o.
1261 * Special characters are set after we have read the login name; all
1262 * reads will be done in raw mode anyway. Errors will be dealt with
1263 * later on.
1264 */
1265
1266 #ifdef IUTF8
1267 tp->c_iflag = tp->c_iflag & IUTF8;
1268 if (tp->c_iflag & IUTF8)
1269 op->flags |= F_UTF8;
1270 #else
1271 tp->c_iflag = 0;
1272 #endif
1273 tp->c_lflag = 0;
1274 tp->c_oflag &= OPOST | ONLCR;
1275
1276 if ((op->flags & F_KEEPCFLAGS) == 0)
1277 tp->c_cflag = CS8 | HUPCL | CREAD | (tp->c_cflag & CLOCAL);
1278
1279 /*
1280 * Note that the speed is stored in the c_cflag termios field, so we have
1281 * set the speed always when the cflag is reset.
1282 */
1283 cfsetispeed(tp, ispeed);
1284 cfsetospeed(tp, ospeed);
1285
1286 /* The default is to follow setting from kernel, but it's possible
1287 * to explicitly remove/add CLOCAL flag by -L[=<mode>]*/
1288 switch (op->clocal) {
1289 case CLOCAL_MODE_ALWAYS:
1290 tp->c_cflag |= CLOCAL; /* -L or -L=always */
1291 break;
1292 case CLOCAL_MODE_NEVER:
1293 tp->c_cflag &= ~CLOCAL; /* -L=never */
1294 break;
1295 case CLOCAL_MODE_AUTO: /* -L=auto */
1296 break;
1297 }
1298
1299 #ifdef HAVE_STRUCT_TERMIOS_C_LINE
1300 tp->c_line = 0;
1301 #endif
1302 tp->c_cc[VMIN] = 1;
1303 tp->c_cc[VTIME] = 0;
1304
1305 /* Check for terminal size and if not found set default */
1306 if (ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) == 0) {
1307 if (ws.ws_row == 0)
1308 ws.ws_row = 24;
1309 if (ws.ws_col == 0)
1310 ws.ws_col = 80;
1311 if (ioctl(STDIN_FILENO, TIOCSWINSZ, &ws))
1312 debug("TIOCSWINSZ ioctl failed\n");
1313 }
1314
1315 /* Optionally enable hardware flow control. */
1316 #ifdef CRTSCTS
1317 if (op->flags & F_RTSCTS)
1318 tp->c_cflag |= CRTSCTS;
1319 #endif
1320 /* Flush input and output queues, important for modems! */
1321 tcflush(STDIN_FILENO, TCIOFLUSH);
1322
1323 if (tcsetattr(STDIN_FILENO, TCSANOW, tp))
1324 log_warn(_("setting terminal attributes failed: %m"));
1325
1326 /* Go to blocking input even in local mode. */
1327 fcntl(STDIN_FILENO, F_SETFL,
1328 fcntl(STDIN_FILENO, F_GETFL, 0) & ~O_NONBLOCK);
1329
1330 debug("term_io 2\n");
1331 }
1332
1333 /* Reset virtual console on stdin to its defaults */
1334 static void reset_vc(const struct options *op, struct termios *tp)
1335 {
1336 int fl = 0;
1337
1338 fl |= (op->flags & F_KEEPCFLAGS) == 0 ? 0 : UL_TTY_KEEPCFLAGS;
1339 fl |= (op->flags & F_UTF8) == 0 ? 0 : UL_TTY_UTF8;
1340
1341 reset_virtual_console(tp, fl);
1342
1343 if (tcsetattr(STDIN_FILENO, TCSADRAIN, tp))
1344 log_warn(_("setting terminal attributes failed: %m"));
1345
1346 /* Go to blocking input even in local mode. */
1347 fcntl(STDIN_FILENO, F_SETFL,
1348 fcntl(STDIN_FILENO, F_GETFL, 0) & ~O_NONBLOCK);
1349 }
1350
1351 /* Extract baud rate from modem status message. */
1352 static void auto_baud(struct termios *tp)
1353 {
1354 speed_t speed;
1355 int vmin;
1356 unsigned iflag;
1357 char buf[BUFSIZ];
1358 char *bp;
1359 int nread;
1360
1361 /*
1362 * This works only if the modem produces its status code AFTER raising
1363 * the DCD line, and if the computer is fast enough to set the proper
1364 * baud rate before the message has gone by. We expect a message of the
1365 * following format:
1366 *
1367 * <junk><number><junk>
1368 *
1369 * The number is interpreted as the baud rate of the incoming call. If the
1370 * modem does not tell us the baud rate within one second, we will keep
1371 * using the current baud rate. It is advisable to enable BREAK
1372 * processing (comma-separated list of baud rates) if the processing of
1373 * modem status messages is enabled.
1374 */
1375
1376 /*
1377 * Use 7-bit characters, don't block if input queue is empty. Errors will
1378 * be dealt with later on.
1379 */
1380 iflag = tp->c_iflag;
1381 /* Enable 8th-bit stripping. */
1382 tp->c_iflag |= ISTRIP;
1383 vmin = tp->c_cc[VMIN];
1384 /* Do not block when queue is empty. */
1385 tp->c_cc[VMIN] = 0;
1386 tcsetattr(STDIN_FILENO, TCSANOW, tp);
1387
1388 /*
1389 * Wait for a while, then read everything the modem has said so far and
1390 * try to extract the speed of the dial-in call.
1391 */
1392 sleep(1);
1393 if ((nread = read(STDIN_FILENO, buf, sizeof(buf) - 1)) > 0) {
1394 buf[nread] = '\0';
1395 for (bp = buf; bp < buf + nread; bp++)
1396 if (isascii(*bp) && isdigit(*bp)) {
1397 if ((speed = bcode(bp))) {
1398 cfsetispeed(tp, speed);
1399 cfsetospeed(tp, speed);
1400 }
1401 break;
1402 }
1403 }
1404
1405 /* Restore terminal settings. Errors will be dealt with later on. */
1406 tp->c_iflag = iflag;
1407 tp->c_cc[VMIN] = vmin;
1408 tcsetattr(STDIN_FILENO, TCSANOW, tp);
1409 }
1410
1411 static char *xgethostname(void)
1412 {
1413 char *name;
1414 size_t sz = get_hostname_max() + 1;
1415
1416 name = malloc(sizeof(char) * sz);
1417 if (!name)
1418 log_err(_("failed to allocate memory: %m"));
1419
1420 if (gethostname(name, sz) != 0) {
1421 free(name);
1422 return NULL;
1423 }
1424 name[sz - 1] = '\0';
1425 return name;
1426 }
1427
1428 static char *xgetdomainname(void)
1429 {
1430 #ifdef HAVE_GETDOMAINNAME
1431 char *name;
1432 const size_t sz = get_hostname_max() + 1;
1433
1434 name = malloc(sizeof(char) * sz);
1435 if (!name)
1436 log_err(_("failed to allocate memory: %m"));
1437
1438 if (getdomainname(name, sz) != 0) {
1439 free(name);
1440 return NULL;
1441 }
1442 name[sz - 1] = '\0';
1443 return name;
1444 #else
1445 return NULL;
1446 #endif
1447 }
1448
1449
1450 static char *read_os_release(struct options *op, const char *varname)
1451 {
1452 int fd = -1;
1453 struct stat st;
1454 size_t varsz = strlen(varname);
1455 char *p, *buf = NULL, *ret = NULL;
1456
1457 /* read the file only once */
1458 if (!op->osrelease) {
1459 fd = open(_PATH_OS_RELEASE_ETC, O_RDONLY);
1460 if (fd == -1) {
1461 fd = open(_PATH_OS_RELEASE_USR, O_RDONLY);
1462 if (fd == -1) {
1463 log_warn(_("cannot open os-release file"));
1464 return NULL;
1465 }
1466 }
1467
1468 if (fstat(fd, &st) < 0 || st.st_size > 4 * 1024 * 1024)
1469 goto done;
1470
1471 op->osrelease = malloc(st.st_size + 1);
1472 if (!op->osrelease)
1473 log_err(_("failed to allocate memory: %m"));
1474 if (read_all(fd, op->osrelease, st.st_size) != (ssize_t) st.st_size) {
1475 free(op->osrelease);
1476 op->osrelease = NULL;
1477 goto done;
1478 }
1479 op->osrelease[st.st_size] = 0;
1480 }
1481 buf = strdup(op->osrelease);
1482 if (!buf)
1483 log_err(_("failed to allocate memory: %m"));
1484 p = buf;
1485
1486 for (;;) {
1487 char *eol, *eon;
1488
1489 p += strspn(p, "\n\r");
1490 p += strspn(p, " \t\n\r");
1491 if (!*p)
1492 break;
1493 if (strspn(p, "#;\n") != 0) {
1494 p += strcspn(p, "\n\r");
1495 continue;
1496 }
1497 if (strncmp(p, varname, varsz) != 0) {
1498 p += strcspn(p, "\n\r");
1499 continue;
1500 }
1501 p += varsz;
1502 p += strspn(p, " \t\n\r");
1503
1504 if (*p != '=')
1505 continue;
1506
1507 p += strspn(p, " \t\n\r=\"");
1508 eol = p + strcspn(p, "\n\r");
1509 *eol = '\0';
1510 eon = eol-1;
1511 while (eon > p) {
1512 if (*eon == '\t' || *eon == ' ') {
1513 eon--;
1514 continue;
1515 }
1516 if (*eon == '"') {
1517 *eon = '\0';
1518 break;
1519 }
1520 break;
1521 }
1522 free(ret);
1523 ret = strdup(p);
1524 if (!ret)
1525 log_err(_("failed to allocate memory: %m"));
1526 p = eol + 1;
1527 }
1528 done:
1529 free(buf);
1530 if (fd >= 0)
1531 close(fd);
1532 return ret;
1533 }
1534
1535 #ifdef AGETTY_RELOAD
1536 static void open_netlink(void)
1537 {
1538 struct sockaddr_nl addr = { 0, };
1539 int sock;
1540
1541 if (netlink_fd != AGETTY_RELOAD_FDNONE)
1542 return;
1543
1544 sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
1545 if (sock >= 0) {
1546 addr.nl_family = AF_NETLINK;
1547 addr.nl_pid = getpid();
1548 addr.nl_groups = RTMGRP_IPV4_IFADDR | RTMGRP_IPV6_IFADDR;
1549 if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0)
1550 close(sock);
1551 else
1552 netlink_fd = sock;
1553 }
1554 }
1555
1556 static int process_netlink_msg(int *changed)
1557 {
1558 char buf[4096];
1559 struct sockaddr_nl snl;
1560 struct nlmsghdr *h;
1561 int rc;
1562
1563 struct iovec iov = {
1564 .iov_base = buf,
1565 .iov_len = sizeof(buf)
1566 };
1567 struct msghdr msg = {
1568 .msg_name = &snl,
1569 .msg_namelen = sizeof(snl),
1570 .msg_iov = &iov,
1571 .msg_iovlen = 1,
1572 .msg_control = NULL,
1573 .msg_controllen = 0,
1574 .msg_flags = 0
1575 };
1576
1577 rc = recvmsg(netlink_fd, &msg, MSG_DONTWAIT);
1578 if (rc < 0) {
1579 if (errno == EWOULDBLOCK || errno == EAGAIN)
1580 return 0;
1581
1582 /* Failure, just stop listening for changes */
1583 close(netlink_fd);
1584 netlink_fd = AGETTY_RELOAD_FDNONE;
1585 return 0;
1586 }
1587
1588 for (h = (struct nlmsghdr *)buf; NLMSG_OK(h, (unsigned int)rc); h = NLMSG_NEXT(h, rc)) {
1589 if (h->nlmsg_type == NLMSG_DONE ||
1590 h->nlmsg_type == NLMSG_ERROR) {
1591 close(netlink_fd);
1592 netlink_fd = AGETTY_RELOAD_FDNONE;
1593 return 0;
1594 }
1595
1596 *changed = 1;
1597 break;
1598 }
1599
1600 return 1;
1601 }
1602
1603 static int process_netlink(void)
1604 {
1605 int changed = 0;
1606 while (process_netlink_msg(&changed));
1607 return changed;
1608 }
1609
1610 static int wait_for_term_input(int fd)
1611 {
1612 char buffer[sizeof(struct inotify_event) + NAME_MAX + 1];
1613 fd_set rfds;
1614
1615 if (inotify_fd == AGETTY_RELOAD_FDNONE) {
1616 /* make sure the reload trigger file exists */
1617 int reload_fd = open(AGETTY_RELOAD_FILENAME,
1618 O_CREAT|O_CLOEXEC|O_RDONLY,
1619 S_IRUSR|S_IWUSR);
1620
1621 /* initialize reload trigger inotify stuff */
1622 if (reload_fd >= 0) {
1623 inotify_fd = inotify_init1(IN_NONBLOCK | IN_CLOEXEC);
1624 if (inotify_fd > 0)
1625 inotify_add_watch(inotify_fd, AGETTY_RELOAD_FILENAME,
1626 IN_ATTRIB | IN_MODIFY);
1627
1628 close(reload_fd);
1629 } else
1630 log_warn(_("failed to create reload file: %s: %m"),
1631 AGETTY_RELOAD_FILENAME);
1632 }
1633
1634 while (1) {
1635 int nfds = fd;
1636
1637 FD_ZERO(&rfds);
1638 FD_SET(fd, &rfds);
1639
1640 if (inotify_fd >= 0) {
1641 FD_SET(inotify_fd, &rfds);
1642 nfds = max(nfds, inotify_fd);
1643 }
1644 if (netlink_fd >= 0) {
1645 FD_SET(netlink_fd, &rfds);
1646 nfds = max(nfds, netlink_fd);
1647 }
1648
1649 /* If waiting fails, just fall through, presumably reading input will fail */
1650 if (select(nfds + 1, &rfds, NULL, NULL, NULL) < 0)
1651 return 1;
1652
1653 if (FD_ISSET(fd, &rfds)) {
1654 return 1;
1655
1656 } else if (netlink_fd >= 0 && FD_ISSET(netlink_fd, &rfds)) {
1657 if (!process_netlink())
1658 continue;
1659
1660 /* Just drain the inotify buffer */
1661 } else if (inotify_fd >= 0 && FD_ISSET(inotify_fd, &rfds)) {
1662 while (read(inotify_fd, buffer, sizeof (buffer)) > 0);
1663 }
1664
1665 return 0;
1666 }
1667 }
1668 #endif /* AGETTY_RELOAD */
1669
1670 #ifdef ISSUEDIR_SUPPORT
1671 static int issuedir_filter(const struct dirent *d)
1672 {
1673 size_t namesz;
1674
1675 #ifdef _DIRENT_HAVE_D_TYPE
1676 if (d->d_type != DT_UNKNOWN && d->d_type != DT_REG &&
1677 d->d_type != DT_LNK)
1678 return 0;
1679 #endif
1680 if (*d->d_name == '.')
1681 return 0;
1682
1683 namesz = strlen(d->d_name);
1684 if (!namesz || namesz < ISSUEDIR_EXTSIZ + 1 ||
1685 strcmp(d->d_name + (namesz - ISSUEDIR_EXTSIZ), ISSUEDIR_EXT))
1686 return 0;
1687
1688 /* Accept this */
1689 return 1;
1690 }
1691
1692 static FILE *issuedir_next_file(int dd, struct dirent **namelist, int nfiles, int *n)
1693 {
1694 while (*n < nfiles) {
1695 struct dirent *d = namelist[*n];
1696 struct stat st;
1697 FILE *f;
1698
1699 (*n)++;
1700
1701 if (fstatat(dd, d->d_name, &st, 0) ||
1702 !S_ISREG(st.st_mode))
1703 continue;
1704
1705 f = fopen_at(dd, d->d_name, O_RDONLY|O_CLOEXEC, "r" UL_CLOEXECSTR);
1706 if (f)
1707 return f;
1708 }
1709 return NULL;
1710 }
1711
1712 #endif /* ISSUEDIR_SUPPORT */
1713
1714 #ifndef ISSUE_SUPPORT
1715 static void print_issue_file(struct options *op, struct termios *tp __attribute__((__unused__)))
1716 {
1717 if ((op->flags & F_NONL) == 0) {
1718 /* Issue not in use, start with a new line. */
1719 write_all(STDOUT_FILENO, "\r\n", 2);
1720 }
1721 }
1722 #else /* ISSUE_SUPPORT */
1723
1724 static void print_issue_file(struct options *op, struct termios *tp)
1725 {
1726 const char *filename, *dirname = NULL;
1727 FILE *f = NULL;
1728 #ifdef ISSUEDIR_SUPPORT
1729 int dd = -1, nfiles = 0, i;
1730 struct dirent **namelist = NULL;
1731 #endif
1732 if ((op->flags & F_NONL) == 0) {
1733 /* Issue not in use, start with a new line. */
1734 write_all(STDOUT_FILENO, "\r\n", 2);
1735 }
1736
1737 if (!(op->flags & F_ISSUE))
1738 return;
1739
1740 /*
1741 * The custom issue file or directory specified by: agetty -f <path>.
1742 * Note that nothing is printed if the file/dir does not exist.
1743 */
1744 filename = op->issue;
1745 if (filename) {
1746 struct stat st;
1747
1748 if (stat(filename, &st) < 0)
1749 return;
1750 if (S_ISDIR(st.st_mode)) {
1751 dirname = filename;
1752 filename = NULL;
1753 }
1754 } else {
1755 /* The default /etc/issue and optional /etc/issue.d directory
1756 * as extension to the file. The /etc/issue.d directory is
1757 * ignored if there is no /etc/issue file. The file may be
1758 * empty or symlink.
1759 */
1760 if (access(_PATH_ISSUE, F_OK|R_OK) != 0)
1761 return;
1762 filename = _PATH_ISSUE;
1763 dirname = _PATH_ISSUEDIR;
1764 }
1765
1766 #ifdef ISSUEDIR_SUPPORT
1767 if (dirname) {
1768 dd = open(dirname, O_RDONLY|O_CLOEXEC|O_DIRECTORY);
1769 if (dd >= 0)
1770 nfiles = scandirat(dd, ".", &namelist, issuedir_filter, versionsort);
1771 if (nfiles <= 0)
1772 dirname = NULL;
1773 }
1774 i = 0;
1775 #endif
1776 if (filename)
1777 f = fopen(filename, "r");
1778
1779 if (f || dirname) {
1780 int c, oflag = tp->c_oflag; /* Save current setting. */
1781
1782 if ((op->flags & F_VCONSOLE) == 0) {
1783 /* Map new line in output to carriage return & new line. */
1784 tp->c_oflag |= (ONLCR | OPOST);
1785 tcsetattr(STDIN_FILENO, TCSADRAIN, tp);
1786 }
1787
1788 do {
1789 #ifdef ISSUEDIR_SUPPORT
1790 if (!f && i < nfiles)
1791 f = issuedir_next_file(dd, namelist, nfiles, &i);
1792 #endif
1793 if (!f)
1794 break;
1795 while ((c = getc(f)) != EOF) {
1796 if (c == '\\')
1797 output_special_char(getc(f), op, tp, f);
1798 else
1799 putchar(c);
1800 }
1801 fclose(f);
1802 f = NULL;
1803 } while (dirname);
1804
1805 fflush(stdout);
1806
1807 if ((op->flags & F_VCONSOLE) == 0) {
1808 /* Restore settings. */
1809 tp->c_oflag = oflag;
1810 /* Wait till output is gone. */
1811 tcsetattr(STDIN_FILENO, TCSADRAIN, tp);
1812 }
1813 }
1814
1815 #ifdef ISSUEDIR_SUPPORT
1816 for (i = 0; i < nfiles; i++)
1817 free(namelist[i]);
1818 free(namelist);
1819 if (dd >= 0)
1820 close(dd);
1821 #endif
1822 }
1823 #endif /* ISSUE_SUPPORT */
1824
1825 /* Show login prompt, optionally preceded by /etc/issue contents. */
1826 static void do_prompt(struct options *op, struct termios *tp)
1827 {
1828 #ifdef AGETTY_RELOAD
1829 again:
1830 #endif
1831 print_issue_file(op, tp);
1832
1833 if (op->flags & F_LOGINPAUSE) {
1834 puts(_("[press ENTER to login]"));
1835 #ifdef AGETTY_RELOAD
1836 if (!wait_for_term_input(STDIN_FILENO)) {
1837 /* reload issue */
1838 if (op->flags & F_VCONSOLE)
1839 termio_clear(STDOUT_FILENO);
1840 goto again;
1841 }
1842 #endif
1843 getc(stdin);
1844 }
1845 #ifdef KDGKBLED
1846 if (!(op->flags & F_NOHINTS) && !op->autolog &&
1847 (op->flags & F_VCONSOLE)) {
1848 int kb = 0;
1849
1850 if (ioctl(STDIN_FILENO, KDGKBLED, &kb) == 0) {
1851 char hint[256] = { '\0' };
1852 int nl = 0;
1853
1854 if (access(_PATH_NUMLOCK_ON, F_OK) == 0)
1855 nl = 1;
1856
1857 if (nl && (kb & 0x02) == 0)
1858 append(hint, sizeof(hint), NULL, _("Num Lock off"));
1859
1860 else if (nl == 0 && (kb & 2) && (kb & 0x20) == 0)
1861 append(hint, sizeof(hint), NULL, _("Num Lock on"));
1862
1863 if ((kb & 0x04) && (kb & 0x40) == 0)
1864 append(hint, sizeof(hint), ", ", _("Caps Lock on"));
1865
1866 if ((kb & 0x01) && (kb & 0x10) == 0)
1867 append(hint, sizeof(hint), ", ", _("Scroll Lock on"));
1868
1869 if (*hint)
1870 printf(_("Hint: %s\n\n"), hint);
1871 }
1872 }
1873 #endif /* KDGKBLED */
1874 if ((op->flags & F_NOHOSTNAME) == 0) {
1875 char *hn = xgethostname();
1876
1877 if (hn) {
1878 char *dot = strchr(hn, '.');
1879 char *cn = hn;
1880 struct addrinfo *res = NULL;
1881
1882 if ((op->flags & F_LONGHNAME) == 0) {
1883 if (dot)
1884 *dot = '\0';
1885
1886 } else if (dot == NULL) {
1887 struct addrinfo hints;
1888
1889 memset(&hints, 0, sizeof(hints));
1890 hints.ai_flags = AI_CANONNAME;
1891
1892 if (!getaddrinfo(hn, NULL, &hints, &res)
1893 && res && res->ai_canonname)
1894 cn = res->ai_canonname;
1895 }
1896
1897 write_all(STDOUT_FILENO, cn, strlen(cn));
1898 write_all(STDOUT_FILENO, " ", 1);
1899
1900 if (res)
1901 freeaddrinfo(res);
1902 free(hn);
1903 }
1904 }
1905 if (!op->autolog) {
1906 /* Always show login prompt. */
1907 write_all(STDOUT_FILENO, LOGIN, sizeof(LOGIN) - 1);
1908 }
1909 }
1910
1911 /* Select next baud rate. */
1912 static void next_speed(struct options *op, struct termios *tp)
1913 {
1914 static int baud_index = -1;
1915
1916 if (baud_index == -1)
1917 /*
1918 * If the F_KEEPSPEED flags is set then the FIRST_SPEED is not
1919 * tested yet (see termio_init()).
1920 */
1921 baud_index =
1922 (op->flags & F_KEEPSPEED) ? FIRST_SPEED : 1 % op->numspeed;
1923 else
1924 baud_index = (baud_index + 1) % op->numspeed;
1925
1926 cfsetispeed(tp, op->speeds[baud_index]);
1927 cfsetospeed(tp, op->speeds[baud_index]);
1928 tcsetattr(STDIN_FILENO, TCSANOW, tp);
1929 }
1930
1931 /* Get user name, establish parity, speed, erase, kill & eol. */
1932 static char *get_logname(struct options *op, struct termios *tp, struct chardata *cp)
1933 {
1934 static char logname[BUFSIZ];
1935 char *bp;
1936 char c; /* input character, full eight bits */
1937 char ascval; /* low 7 bits of input character */
1938 int eightbit;
1939 static char *erase[] = { /* backspace-space-backspace */
1940 "\010\040\010", /* space parity */
1941 "\010\040\010", /* odd parity */
1942 "\210\240\210", /* even parity */
1943 "\210\240\210", /* no parity */
1944 };
1945
1946 /* Initialize kill, erase, parity etc. (also after switching speeds). */
1947 INIT_CHARDATA(cp);
1948
1949 /*
1950 * Flush pending input (especially important after parsing or switching
1951 * the baud rate).
1952 */
1953 if ((op->flags & F_VCONSOLE) == 0)
1954 sleep(1);
1955 tcflush(STDIN_FILENO, TCIFLUSH);
1956
1957 eightbit = (op->flags & F_EIGHTBITS);
1958 bp = logname;
1959 *bp = '\0';
1960
1961 while (*logname == '\0') {
1962 /* Write issue file and prompt */
1963 do_prompt(op, tp);
1964
1965 #ifdef AGETTY_RELOAD
1966 if (!wait_for_term_input(STDIN_FILENO)) {
1967 /* refresh prompt -- discard input data, clear terminal
1968 * and call do_prompt() again
1969 */
1970 if ((op->flags & F_VCONSOLE) == 0)
1971 sleep(1);
1972 tcflush(STDIN_FILENO, TCIFLUSH);
1973 if (op->flags & F_VCONSOLE)
1974 termio_clear(STDOUT_FILENO);
1975 bp = logname;
1976 *bp = '\0';
1977 continue;
1978 }
1979 #endif
1980 cp->eol = '\0';
1981
1982 /* Read name, watch for break and end-of-line. */
1983 while (cp->eol == '\0') {
1984
1985 char key;
1986
1987 debug("read from FD\n");
1988 if (read(STDIN_FILENO, &c, 1) < 1) {
1989 debug("read failed\n");
1990
1991 /* The terminal could be open with O_NONBLOCK when
1992 * -L (force CLOCAL) is specified... */
1993 if (errno == EINTR || errno == EAGAIN) {
1994 xusleep(250000);
1995 continue;
1996 }
1997 switch (errno) {
1998 case 0:
1999 case EIO:
2000 case ESRCH:
2001 case EINVAL:
2002 case ENOENT:
2003 break;
2004 default:
2005 log_err(_("%s: read: %m"), op->tty);
2006 }
2007 }
2008
2009 /* Do parity bit handling. */
2010 if (eightbit)
2011 ascval = c;
2012 else if (c != (ascval = (c & 0177))) {
2013 uint32_t bits; /* # of "1" bits per character */
2014 uint32_t mask; /* mask with 1 bit up */
2015 for (bits = 1, mask = 1; mask & 0177; mask <<= 1) {
2016 if (mask & ascval)
2017 bits++;
2018 }
2019 cp->parity |= ((bits & 1) ? 1 : 2);
2020 }
2021
2022 if (op->killchars && strchr(op->killchars, ascval))
2023 key = CTL('U');
2024 else if (op->erasechars && strchr(op->erasechars, ascval))
2025 key = DEL;
2026 else
2027 key = ascval;
2028
2029 /* Do erase, kill and end-of-line processing. */
2030 switch (key) {
2031 case 0:
2032 *bp = 0;
2033 if (op->numspeed > 1)
2034 return NULL;
2035 break;
2036 case CR:
2037 case NL:
2038 *bp = 0; /* terminate logname */
2039 cp->eol = ascval; /* set end-of-line char */
2040 break;
2041 case BS:
2042 case DEL:
2043 cp->erase = ascval; /* set erase character */
2044 if (bp > logname) {
2045 if ((tp->c_lflag & ECHO) == 0)
2046 write_all(1, erase[cp->parity], 3);
2047 bp--;
2048 }
2049 break;
2050 case CTL('U'):
2051 cp->kill = ascval; /* set kill character */
2052 while (bp > logname) {
2053 if ((tp->c_lflag & ECHO) == 0)
2054 write_all(1, erase[cp->parity], 3);
2055 bp--;
2056 }
2057 break;
2058 case CTL('D'):
2059 exit(EXIT_SUCCESS);
2060 default:
2061 if (!isascii(ascval) || !isprint(ascval))
2062 break;
2063 if ((size_t)(bp - logname) >= sizeof(logname) - 1)
2064 log_err(_("%s: input overrun"), op->tty);
2065 if ((tp->c_lflag & ECHO) == 0)
2066 write_all(1, &c, 1); /* echo the character */
2067 *bp++ = ascval; /* and store it */
2068 break;
2069 }
2070 }
2071 }
2072
2073 #ifdef HAVE_WIDECHAR
2074 if ((op->flags & (F_EIGHTBITS|F_UTF8)) == (F_EIGHTBITS|F_UTF8)) {
2075 /* Check out UTF-8 multibyte characters */
2076 ssize_t len;
2077 wchar_t *wcs, *wcp;
2078
2079 len = mbstowcs((wchar_t *)0, logname, 0);
2080 if (len < 0)
2081 log_err(_("%s: invalid character conversion for login name"), op->tty);
2082
2083 wcs = malloc((len + 1) * sizeof(wchar_t));
2084 if (!wcs)
2085 log_err(_("failed to allocate memory: %m"));
2086
2087 len = mbstowcs(wcs, logname, len + 1);
2088 if (len < 0)
2089 log_err(_("%s: invalid character conversion for login name"), op->tty);
2090
2091 wcp = wcs;
2092 while (*wcp) {
2093 const wint_t wc = *wcp++;
2094 if (!iswprint(wc))
2095 log_err(_("%s: invalid character 0x%x in login name"), op->tty, wc);
2096 }
2097 free(wcs);
2098 } else
2099 #endif
2100 if ((op->flags & F_LCUC) && (cp->capslock = caps_lock(logname))) {
2101
2102 /* Handle names with upper case and no lower case. */
2103 for (bp = logname; *bp; bp++)
2104 if (isupper(*bp))
2105 *bp = tolower(*bp); /* map name to lower case */
2106 }
2107
2108 return logname;
2109 }
2110
2111 /* Set the final tty mode bits. */
2112 static void termio_final(struct options *op, struct termios *tp, struct chardata *cp)
2113 {
2114 /* General terminal-independent stuff. */
2115
2116 /* 2-way flow control */
2117 tp->c_iflag |= IXON | IXOFF;
2118 tp->c_lflag |= ICANON | ISIG | ECHO | ECHOE | ECHOK | ECHOKE;
2119 /* no longer| ECHOCTL | ECHOPRT */
2120 tp->c_oflag |= OPOST;
2121 /* tp->c_cflag = 0; */
2122 tp->c_cc[VINTR] = DEF_INTR;
2123 tp->c_cc[VQUIT] = DEF_QUIT;
2124 tp->c_cc[VEOF] = DEF_EOF;
2125 tp->c_cc[VEOL] = DEF_EOL;
2126 #ifdef __linux__
2127 tp->c_cc[VSWTC] = DEF_SWITCH;
2128 #elif defined(VSWTCH)
2129 tp->c_cc[VSWTCH] = DEF_SWITCH;
2130 #endif /* __linux__ */
2131
2132 /* Account for special characters seen in input. */
2133 if (cp->eol == CR) {
2134 tp->c_iflag |= ICRNL;
2135 tp->c_oflag |= ONLCR;
2136 }
2137 tp->c_cc[VERASE] = cp->erase;
2138 tp->c_cc[VKILL] = cp->kill;
2139
2140 /* Account for the presence or absence of parity bits in input. */
2141 switch (cp->parity) {
2142 case 0:
2143 /* space (always 0) parity */
2144 break;
2145 case 1:
2146 /* odd parity */
2147 tp->c_cflag |= PARODD;
2148 /* fallthrough */
2149 case 2:
2150 /* even parity */
2151 tp->c_cflag |= PARENB;
2152 tp->c_iflag |= INPCK | ISTRIP;
2153 /* fallthrough */
2154 case (1 | 2):
2155 /* no parity bit */
2156 tp->c_cflag &= ~CSIZE;
2157 tp->c_cflag |= CS7;
2158 break;
2159 }
2160 /* Account for upper case without lower case. */
2161 if (cp->capslock) {
2162 #ifdef IUCLC
2163 tp->c_iflag |= IUCLC;
2164 #endif
2165 #ifdef XCASE
2166 tp->c_lflag |= XCASE;
2167 #endif
2168 #ifdef OLCUC
2169 tp->c_oflag |= OLCUC;
2170 #endif
2171 }
2172 /* Optionally enable hardware flow control. */
2173 #ifdef CRTSCTS
2174 if (op->flags & F_RTSCTS)
2175 tp->c_cflag |= CRTSCTS;
2176 #endif
2177
2178 /* Finally, make the new settings effective. */
2179 if (tcsetattr(STDIN_FILENO, TCSANOW, tp) < 0)
2180 log_err(_("%s: failed to set terminal attributes: %m"), op->tty);
2181 }
2182
2183 /*
2184 * String contains upper case without lower case.
2185 * http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=52940
2186 * http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=156242
2187 */
2188 static int caps_lock(char *s)
2189 {
2190 int capslock;
2191
2192 for (capslock = 0; *s; s++) {
2193 if (islower(*s))
2194 return EXIT_SUCCESS;
2195 if (capslock == 0)
2196 capslock = isupper(*s);
2197 }
2198 return capslock;
2199 }
2200
2201 /* Convert speed string to speed code; return 0 on failure. */
2202 static speed_t bcode(char *s)
2203 {
2204 const struct Speedtab *sp;
2205 long speed = atol(s);
2206
2207 for (sp = speedtab; sp->speed; sp++)
2208 if (sp->speed == speed)
2209 return sp->code;
2210 return 0;
2211 }
2212
2213 static void __attribute__((__noreturn__)) usage(void)
2214 {
2215 FILE *out = stdout;
2216
2217 fputs(USAGE_HEADER, out);
2218 fprintf(out, _(" %1$s [options] <line> [<baud_rate>,...] [<termtype>]\n"
2219 " %1$s [options] <baud_rate>,... <line> [<termtype>]\n"), program_invocation_short_name);
2220
2221 fputs(USAGE_SEPARATOR, out);
2222 fputs(_("Open a terminal and set its mode.\n"), out);
2223
2224 fputs(USAGE_OPTIONS, out);
2225 fputs(_(" -8, --8bits assume 8-bit tty\n"), out);
2226 fputs(_(" -a, --autologin <user> login the specified user automatically\n"), out);
2227 fputs(_(" -c, --noreset do not reset control mode\n"), out);
2228 fputs(_(" -E, --remote use -r <hostname> for login(1)\n"), out);
2229 fputs(_(" -f, --issue-file <file> display issue file\n"), out);
2230 fputs(_(" -h, --flow-control enable hardware flow control\n"), out);
2231 fputs(_(" -H, --host <hostname> specify login host\n"), out);
2232 fputs(_(" -i, --noissue do not display issue file\n"), out);
2233 fputs(_(" -I, --init-string <string> set init string\n"), out);
2234 fputs(_(" -J --noclear do not clear the screen before prompt\n"), out);
2235 fputs(_(" -l, --login-program <file> specify login program\n"), out);
2236 fputs(_(" -L, --local-line[=<mode>] control the local line flag\n"), out);
2237 fputs(_(" -m, --extract-baud extract baud rate during connect\n"), out);
2238 fputs(_(" -n, --skip-login do not prompt for login\n"), out);
2239 fputs(_(" -N --nonewline do not print a newline before issue\n"), out);
2240 fputs(_(" -o, --login-options <opts> options that are passed to login\n"), out);
2241 fputs(_(" -p, --login-pause wait for any key before the login\n"), out);
2242 fputs(_(" -r, --chroot <dir> change root to the directory\n"), out);
2243 fputs(_(" -R, --hangup do virtually hangup on the tty\n"), out);
2244 fputs(_(" -s, --keep-baud try to keep baud rate after break\n"), out);
2245 fputs(_(" -t, --timeout <number> login process timeout\n"), out);
2246 fputs(_(" -U, --detect-case detect uppercase terminal\n"), out);
2247 fputs(_(" -w, --wait-cr wait carriage-return\n"), out);
2248 fputs(_(" --nohints do not print hints\n"), out);
2249 fputs(_(" --nohostname no hostname at all will be shown\n"), out);
2250 fputs(_(" --long-hostname show full qualified hostname\n"), out);
2251 fputs(_(" --erase-chars <string> additional backspace chars\n"), out);
2252 fputs(_(" --kill-chars <string> additional kill chars\n"), out);
2253 fputs(_(" --chdir <directory> chdir before the login\n"), out);
2254 fputs(_(" --delay <number> sleep seconds before prompt\n"), out);
2255 fputs(_(" --nice <number> run login with this priority\n"), out);
2256 fputs(_(" --reload reload prompts on running agetty instances\n"), out);
2257 fputs(_(" --list-speeds display supported baud rates\n"), out);
2258 printf( " --help %s\n", USAGE_OPTSTR_HELP);
2259 printf( " --version %s\n", USAGE_OPTSTR_VERSION);
2260 printf(USAGE_MAN_TAIL("agetty(8)"));
2261
2262 exit(EXIT_SUCCESS);
2263 }
2264
2265 static void list_speeds(void)
2266 {
2267 const struct Speedtab *sp;
2268
2269 for (sp = speedtab; sp->speed; sp++)
2270 printf("%10ld\n", sp->speed);
2271 }
2272
2273 /*
2274 * Helper function reports errors to console or syslog.
2275 * Will be used by log_err() and log_warn() therefore
2276 * it takes a format as well as va_list.
2277 */
2278 #define str2cpy(b,s1,s2) strcat(strcpy(b,s1),s2)
2279
2280 static void dolog(int priority, const char *fmt, va_list ap)
2281 {
2282 #ifndef USE_SYSLOG
2283 int fd;
2284 #endif
2285 char buf[BUFSIZ];
2286 char *bp;
2287
2288 /*
2289 * If the diagnostic is reported via syslog(3), the process name is
2290 * automatically prepended to the message. If we write directly to
2291 * /dev/console, we must prepend the process name ourselves.
2292 */
2293 #ifdef USE_SYSLOG
2294 buf[0] = '\0';
2295 bp = buf;
2296 #else
2297 str2cpy(buf, program_invocation_short_name, ": ");
2298 bp = buf + strlen(buf);
2299 #endif /* USE_SYSLOG */
2300 vsnprintf(bp, sizeof(buf)-strlen(buf), fmt, ap);
2301
2302 /*
2303 * Write the diagnostic directly to /dev/console if we do not use the
2304 * syslog(3) facility.
2305 */
2306 #ifdef USE_SYSLOG
2307 openlog(program_invocation_short_name, LOG_PID, LOG_AUTHPRIV);
2308 syslog(priority, "%s", buf);
2309 closelog();
2310 #else
2311 /* Terminate with CR-LF since the console mode is unknown. */
2312 strcat(bp, "\r\n");
2313 if ((fd = open("/dev/console", 1)) >= 0) {
2314 write_all(fd, buf, strlen(buf));
2315 close(fd);
2316 }
2317 #endif /* USE_SYSLOG */
2318 }
2319
2320 static void log_err(const char *fmt, ...)
2321 {
2322 va_list ap;
2323
2324 va_start(ap, fmt);
2325 dolog(LOG_ERR, fmt, ap);
2326 va_end(ap);
2327
2328 /* Be kind to init(8). */
2329 sleep(10);
2330 exit(EXIT_FAILURE);
2331 }
2332
2333 static void log_warn(const char *fmt, ...)
2334 {
2335 va_list ap;
2336
2337 va_start(ap, fmt);
2338 dolog(LOG_WARNING, fmt, ap);
2339 va_end(ap);
2340 }
2341
2342 static void print_addr(sa_family_t family, void *addr)
2343 {
2344 char buff[INET6_ADDRSTRLEN + 1];
2345
2346 inet_ntop(family, addr, buff, sizeof(buff));
2347 printf("%s", buff);
2348 }
2349
2350 /*
2351 * Prints IP for the specified interface (@iface), if the interface is not
2352 * specified then prints the "best" one (UP, RUNNING, non-LOOPBACK). If not
2353 * found the "best" interface then prints at least host IP.
2354 */
2355 static void output_iface_ip(struct ifaddrs *addrs,
2356 const char *iface,
2357 sa_family_t family)
2358 {
2359 struct ifaddrs *p;
2360 struct addrinfo hints, *info = NULL;
2361 char *host = NULL;
2362 void *addr = NULL;
2363
2364 if (!addrs)
2365 return;
2366
2367 for (p = addrs; p; p = p->ifa_next) {
2368
2369 if (!p->ifa_name ||
2370 !p->ifa_addr ||
2371 p->ifa_addr->sa_family != family)
2372 continue;
2373
2374 if (iface) {
2375 /* Filter out by interface name */
2376 if (strcmp(p->ifa_name, iface) != 0)
2377 continue;
2378 } else {
2379 /* Select the "best" interface */
2380 if ((p->ifa_flags & IFF_LOOPBACK) ||
2381 !(p->ifa_flags & IFF_UP) ||
2382 !(p->ifa_flags & IFF_RUNNING))
2383 continue;
2384 }
2385
2386 addr = NULL;
2387 switch (p->ifa_addr->sa_family) {
2388 case AF_INET:
2389 addr = &((struct sockaddr_in *) p->ifa_addr)->sin_addr;
2390 break;
2391 case AF_INET6:
2392 addr = &((struct sockaddr_in6 *) p->ifa_addr)->sin6_addr;
2393 break;
2394 }
2395
2396 if (addr) {
2397 print_addr(family, addr);
2398 return;
2399 }
2400 }
2401
2402 if (iface)
2403 return;
2404
2405 /* Hmm.. not found the best interface, print host IP at least */
2406 memset(&hints, 0, sizeof(hints));
2407 hints.ai_family = family;
2408 if (family == AF_INET6)
2409 hints.ai_flags = AI_V4MAPPED;
2410
2411 host = xgethostname();
2412 if (host && getaddrinfo(host, NULL, &hints, &info) == 0 && info) {
2413 switch (info->ai_family) {
2414 case AF_INET:
2415 addr = &((struct sockaddr_in *) info->ai_addr)->sin_addr;
2416 break;
2417 case AF_INET6:
2418 addr = &((struct sockaddr_in6 *) info->ai_addr)->sin6_addr;
2419 break;
2420 }
2421 if (addr)
2422 print_addr(family, addr);
2423
2424 freeaddrinfo(info);
2425 }
2426 free(host);
2427 }
2428
2429 /*
2430 * parses \x{argument}, if not argument specified then returns NULL, the @fd
2431 * has to point to one char after the sequence (it means '{').
2432 */
2433 static char *get_escape_argument(FILE *fd, char *buf, size_t bufsz)
2434 {
2435 size_t i = 0;
2436 int c = fgetc(fd);
2437
2438 if (c == EOF || (unsigned char) c != '{') {
2439 ungetc(c, fd);
2440 return NULL;
2441 }
2442
2443 do {
2444 c = fgetc(fd);
2445 if (c == EOF)
2446 return NULL;
2447 if ((unsigned char) c != '}' && i < bufsz - 1)
2448 buf[i++] = (unsigned char) c;
2449
2450 } while ((unsigned char) c != '}');
2451
2452 buf[i] = '\0';
2453 return buf;
2454 }
2455
2456 static void output_special_char(unsigned char c, struct options *op,
2457 struct termios *tp, FILE *fp)
2458 {
2459 struct utsname uts;
2460
2461 switch (c) {
2462 case 'e':
2463 {
2464 char escname[UL_COLORNAME_MAXSZ];
2465
2466 if (get_escape_argument(fp, escname, sizeof(escname))) {
2467 const char *esc = color_sequence_from_colorname(escname);
2468 if (esc)
2469 fputs(esc, stdout);
2470 } else
2471 fputs("\033", stdout);
2472 break;
2473 }
2474 case 's':
2475 uname(&uts);
2476 printf("%s", uts.sysname);
2477 break;
2478 case 'n':
2479 uname(&uts);
2480 printf("%s", uts.nodename);
2481 break;
2482 case 'r':
2483 uname(&uts);
2484 printf("%s", uts.release);
2485 break;
2486 case 'v':
2487 uname(&uts);
2488 printf("%s", uts.version);
2489 break;
2490 case 'm':
2491 uname(&uts);
2492 printf("%s", uts.machine);
2493 break;
2494 case 'o':
2495 {
2496 char *dom = xgetdomainname();
2497
2498 fputs(dom ? dom : "unknown_domain", stdout);
2499 free(dom);
2500 break;
2501 }
2502 case 'O':
2503 {
2504 char *dom = NULL;
2505 char *host = xgethostname();
2506 struct addrinfo hints, *info = NULL;
2507
2508 memset(&hints, 0, sizeof(hints));
2509 hints.ai_flags = AI_CANONNAME;
2510
2511 if (host && getaddrinfo(host, NULL, &hints, &info) == 0 && info) {
2512 char *canon;
2513
2514 if (info->ai_canonname &&
2515 (canon = strchr(info->ai_canonname, '.')))
2516 dom = canon + 1;
2517 }
2518 fputs(dom ? dom : "unknown_domain", stdout);
2519 if (info)
2520 freeaddrinfo(info);
2521 free(host);
2522 break;
2523 }
2524 case 'd':
2525 case 't':
2526 {
2527 time_t now;
2528 struct tm *tm;
2529
2530 time(&now);
2531 tm = localtime(&now);
2532
2533 if (!tm)
2534 break;
2535
2536 if (c == 'd') /* ISO 8601 */
2537 printf("%s %s %d %d",
2538 nl_langinfo(ABDAY_1 + tm->tm_wday),
2539 nl_langinfo(ABMON_1 + tm->tm_mon),
2540 tm->tm_mday,
2541 tm->tm_year < 70 ? tm->tm_year + 2000 :
2542 tm->tm_year + 1900);
2543 else
2544 printf("%02d:%02d:%02d",
2545 tm->tm_hour, tm->tm_min, tm->tm_sec);
2546 break;
2547 }
2548 case 'l':
2549 printf ("%s", op->tty);
2550 break;
2551 case 'b':
2552 {
2553 const speed_t speed = cfgetispeed(tp);
2554 int i;
2555
2556 for (i = 0; speedtab[i].speed; i++) {
2557 if (speedtab[i].code == speed) {
2558 printf("%ld", speedtab[i].speed);
2559 break;
2560 }
2561 }
2562 break;
2563 }
2564 case 'S':
2565 {
2566 char *var = NULL, varname[64];
2567
2568 /* \S{varname} */
2569 if (get_escape_argument(fp, varname, sizeof(varname))) {
2570 var = read_os_release(op, varname);
2571 if (var) {
2572 if (strcmp(varname, "ANSI_COLOR") == 0)
2573 printf("\033[%sm", var);
2574 else
2575 fputs(var, stdout);
2576 }
2577 /* \S */
2578 } else if ((var = read_os_release(op, "PRETTY_NAME"))) {
2579 fputs(var, stdout);
2580
2581 /* \S and PRETTY_NAME not found */
2582 } else {
2583 uname(&uts);
2584 fputs(uts.sysname, stdout);
2585 }
2586
2587 free(var);
2588
2589 break;
2590 }
2591 case 'u':
2592 case 'U':
2593 {
2594 int users = 0;
2595 struct utmpx *ut;
2596 setutxent();
2597 while ((ut = getutxent()))
2598 if (ut->ut_type == USER_PROCESS)
2599 users++;
2600 endutxent();
2601 if (c == 'U')
2602 printf(P_("%d user", "%d users", users), users);
2603 else
2604 printf ("%d ", users);
2605 break;
2606 }
2607 case '4':
2608 case '6':
2609 {
2610 sa_family_t family = c == '4' ? AF_INET : AF_INET6;
2611 struct ifaddrs *addrs = NULL;
2612 char iface[128];
2613
2614 #ifdef AGETTY_RELOAD
2615 open_netlink();
2616 #endif
2617
2618 if (getifaddrs(&addrs))
2619 break;
2620
2621 if (get_escape_argument(fp, iface, sizeof(iface)))
2622 output_iface_ip(addrs, iface, family);
2623 else
2624 output_iface_ip(addrs, NULL, family);
2625
2626 freeifaddrs(addrs);
2627 break;
2628 }
2629 default:
2630 putchar(c);
2631 break;
2632 }
2633 }
2634
2635 static void init_special_char(char* arg, struct options *op)
2636 {
2637 char ch, *p, *q;
2638 int i;
2639
2640 op->initstring = malloc(strlen(arg) + 1);
2641 if (!op->initstring)
2642 log_err(_("failed to allocate memory: %m"));
2643
2644 /*
2645 * Copy optarg into op->initstring decoding \ddd octal
2646 * codes into chars.
2647 */
2648 q = op->initstring;
2649 p = arg;
2650 while (*p) {
2651 /* The \\ is converted to \ */
2652 if (*p == '\\') {
2653 p++;
2654 if (*p == '\\') {
2655 ch = '\\';
2656 p++;
2657 } else {
2658 /* Handle \000 - \177. */
2659 ch = 0;
2660 for (i = 1; i <= 3; i++) {
2661 if (*p >= '0' && *p <= '7') {
2662 ch <<= 3;
2663 ch += *p - '0';
2664 p++;
2665 } else {
2666 break;
2667 }
2668 }
2669 }
2670 *q++ = ch;
2671 } else
2672 *q++ = *p++;
2673 }
2674 *q = '\0';
2675 }
2676
2677 /*
2678 * Appends @str to @dest and if @dest is not empty then use @sep as a
2679 * separator. The maximal final length of the @dest is @len.
2680 *
2681 * Returns the final @dest length or -1 in case of error.
2682 */
2683 static ssize_t append(char *dest, size_t len, const char *sep, const char *src)
2684 {
2685 size_t dsz = 0, ssz = 0, sz;
2686 char *p;
2687
2688 if (!dest || !len || !src)
2689 return -1;
2690
2691 if (*dest)
2692 dsz = strlen(dest);
2693 if (dsz && sep)
2694 ssz = strlen(sep);
2695 sz = strlen(src);
2696
2697 if (dsz + ssz + sz + 1 > len)
2698 return -1;
2699
2700 p = dest + dsz;
2701 if (ssz) {
2702 memcpy(p, sep, ssz);
2703 p += ssz;
2704 }
2705 memcpy(p, src, sz);
2706 *(p + sz) = '\0';
2707
2708 return dsz + ssz + sz;
2709 }
2710
2711 /*
2712 * Do not allow the user to pass an option as a user name
2713 * To be more safe: Use `--' to make sure the rest is
2714 * interpreted as non-options by the program, if it supports it.
2715 */
2716 static void check_username(const char* nm)
2717 {
2718 const char *p = nm;
2719 if (!nm)
2720 goto err;
2721 if (strlen(nm) > 42)
2722 goto err;
2723 while (isspace(*p))
2724 p++;
2725 if (*p == '-')
2726 goto err;
2727 return;
2728 err:
2729 errno = EPERM;
2730 log_err(_("checkname failed: %m"));
2731 }
2732
2733 static void reload_agettys(void)
2734 {
2735 #ifdef AGETTY_RELOAD
2736 int fd = open(AGETTY_RELOAD_FILENAME, O_CREAT|O_CLOEXEC|O_WRONLY,
2737 S_IRUSR|S_IWUSR);
2738 if (fd < 0)
2739 err(EXIT_FAILURE, _("cannot open %s"), AGETTY_RELOAD_FILENAME);
2740
2741 if (futimens(fd, NULL) < 0 || close(fd) < 0)
2742 err(EXIT_FAILURE, _("cannot touch file %s"),
2743 AGETTY_RELOAD_FILENAME);
2744 #else
2745 /* very unusual */
2746 errx(EXIT_FAILURE, _("--reload is unsupported on your system"));
2747 #endif
2748 }