1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23#define FOR_login
24#include "toys.h"
25
26GLOBALS(
27 char *h, *f;
28
29 int login_timeout, login_fail_timeout;
30)
31
32static void login_timeout_handler(int sig __attribute__((unused)))
33{
34 printf("\nLogin timed out after %d seconds.\n", TT.login_timeout);
35 xexit();
36}
37
38void login_main(void)
39{
40 int hh = FLAG(h), count, tty = tty_fd();
41 char *username, *pass = 0, *ss;
42 struct passwd *pwd = 0;
43
44
45 if (tty == -1) error_exit("no tty");
46
47 openlog("login", LOG_PID | LOG_CONS, LOG_AUTH);
48 xsignal(SIGALRM, login_timeout_handler);
49
50 if (TT.f) username = TT.f;
51 else username = *toys.optargs;
52 for (count = 0; count < 3; count++) {
53 alarm(TT.login_timeout = 60);
54 tcflush(0, TCIFLUSH);
55
56 if (!username) {
57 if (gethostname(toybuf, sizeof(toybuf)-1)) *toybuf = 0;
58 printf("%s%slogin: ", *toybuf ? toybuf : "", *toybuf ? " " : "");
59 fflush(stdout);
60
61 if(!fgets(toybuf, sizeof(toybuf)-1, stdin)) xexit();
62
63
64 for (ss = toybuf; *ss; ss++) if (*ss<=' ' || *ss==':') break;
65 *ss = 0;
66 if (!*(username = toybuf)) {
67 username = 0;
68 continue;
69 }
70 }
71
72
73 if ((pwd = getpwnam(username))) {
74
75 if (TT.f || !*pwd->pw_passwd) break;
76
77
78 if (*(pass = pwd->pw_passwd) == 'x') {
79 struct spwd *spwd = getspnam (username);
80
81 if (spwd) pass = spwd->sp_pwdp;
82 }
83 } else if (TT.f) error_exit("bad -f '%s'", TT.f);
84
85
86 if (!read_password(toybuf, sizeof(toybuf), "Password: ")) {
87 int x = pass && (ss = crypt(toybuf, pass)) && !strcmp(pass, ss);
88
89
90 memset(toybuf, 0, sizeof(toybuf));
91 if (x) break;
92 }
93
94 syslog(LOG_WARNING, "invalid password for '%s' on %s %s%s", pwd->pw_name,
95 ttyname(tty), hh ? "from " : "", hh ? TT.h : "");
96
97 sleep(3);
98 puts("Login incorrect");
99
100 username = 0;
101 pwd = 0;
102 }
103
104 alarm(0);
105 if (!pwd) error_exit("max retries (3)");
106
107
108
109 if (pwd->pw_uid && !access("/etc/nologin", R_OK)) {
110 ss = readfile("/etc/nologin", toybuf, sizeof(toybuf));
111 puts ((ss && *ss) ? ss : "nologin");
112 free(ss);
113 toys.exitval = 1;
114
115 return;
116 }
117
118 if (fchown(tty, pwd->pw_uid, pwd->pw_gid) || fchmod(tty, 0600))
119 printf("can't claim tty");
120 xsetuser(pwd);
121 reset_env(pwd, !FLAG(p));
122
123
124 if ((ss = readfile("/etc/motd", 0, 0))) puts(ss);
125
126 syslog(LOG_INFO, "%s logged in on %s %s %s", pwd->pw_name,
127 ttyname(tty), hh ? "from" : "", hh ? TT.h : "");
128
129
130 execl(pwd->pw_shell, xmprintf("-%s", pwd->pw_shell), (char *)0);
131 perror_exit("exec shell '%s'", pwd->pw_shell);
132}
133