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