1 --- mingetty-0.9.4/mingetty.c.autologin Sun Nov 26 10:54:13 2000
2 +++ mingetty-0.9.4/mingetty.c Sun Nov 26 10:28:29 2000
7 +/* Autologin stuff. Currently Linux-specific, since there's no
8 + standard way of getting the system boot time. The kernel.h and the
9 + sysinfo prototype are used to get the system uptime under Linux. */
10 +#include <linux/kernel.h>
11 +int sysinfo(struct sysinfo *info);
13 +/* AUTO_LAST points to a timestamp file used to limit autologins to
14 + one per system boot. */
15 +#define AUTO_LAST "/var/log/autologin"
17 +/* AUTO_TTY is the tty on which autologins will be accepted. If set
18 + to an empty string, autologins will be accepted on any tty. */
19 +#define AUTO_TTY "tty1"
21 #include <sys/param.h>
25 static int noclear = 0;
26 /* Print the whole string of gethostname() instead of just until the next "." */
27 static int longhostname = 0;
29 +/* If supplied, attempt an automatic login with this username. */
30 +static char *autologin_name = NULL;
33 * output error messages
39 + * autologin_ok -- returns 1 if it's okay to auto-login when:
40 + * this login is from /dev/tty1; and
41 + * there was a login name passed with the --autologin option; and
42 + * the autologin_name contains only "nice" characters; and
43 + * this is the first autologin attempt since the last boot;
44 + * return 0 otherwise.
46 +static int autologin_ok(void)
50 + struct sysinfo info;
53 + /* Autologins are restricted to AUTO_TTY if non-empty. */
54 + if (AUTO_TTY[0] && strcmp(tty, AUTO_TTY))
57 + /* An all-alphanumeric autologin name must be supplied. */
58 + if (autologin_name == NULL || autologin_name[0] == '\0')
60 + for (cp = autologin_name; (c = *cp); cp++)
61 + if (!isalnum(c) && c != '_')
64 + /* Get the uptime in info.uptime, and the last autologin time
65 + in sbuf.st_mtime. */
67 + stat_err = stat(AUTO_LAST, &sbuf);
69 + /* If a stat error other than "no such file" occurs, I don't
70 + know what went wrong, so I'll proceed with caution by
71 + denying the autologin request. */
72 + if (stat_err && errno != ENOENT)
75 + /* If there's been an autologin granted since the last boot,
76 + deny this and any subsequent attempts. Note that this test
77 + is skipped if the AUTO_LAST file doesn't exist. */
78 + if (!stat_err && time(NULL) - info.uptime < sbuf.st_mtime)
81 + /* Create the AUTO_LAST file. The mtime of this file provides
82 + a persistent record of the last time that an autologin
83 + request was granted. Deny the autologin request if either
84 + the file open or file close fails. */
85 + if ((fd=open(AUTO_LAST, O_WRONLY | O_CREAT | O_TRUNC, 0644)) < 0)
90 + /* All tests are okay, so grant the autologin request. */
94 static void usage (void)
96 error ("usage: '%s tty' with e.g. tty=tty1", progname);
98 static struct option const long_options[] = {
99 { "noclear", no_argument, &noclear, 1},
100 { "long-hostname", no_argument, &longhostname, 1},
101 + { "autologin", required_argument, NULL, 'a'},
110 + autologin_name = optarg;
116 /* flush input and output queues, important for modems */
117 ioctl (0, TCFLSH, 2);
119 - while ((logname = get_logname ()) == 0);
121 - execl (_PATH_LOGIN, _PATH_LOGIN, "--", logname, NULL);
122 + if (autologin_ok()) {
123 + execl (_PATH_LOGIN, _PATH_LOGIN, "-f", autologin_name, NULL);
125 + while ((logname = get_logname ()) == 0) ;
126 + execl (_PATH_LOGIN, _PATH_LOGIN, "--", logname, NULL);
128 error ("%s: can't exec " _PATH_LOGIN ": %s", tty, sys_errlist[errno]);