Commit | Line | Data |
---|---|---|
9bcf1061 | 1 | /*- |
ede75793 KB |
2 | * Copyright (c) 1980, 1987, 1988 The Regents of the University of California. |
3 | * All rights reserved. | |
4 | * | |
9bcf1061 | 5 | * %sccs.include.redist.c% |
bcf1365c DF |
6 | */ |
7 | ||
8 | #ifndef lint | |
9 | char copyright[] = | |
ede75793 | 10 | "@(#) Copyright (c) 1980, 1987, 1988 The Regents of the University of California.\n\ |
bcf1365c | 11 | All rights reserved.\n"; |
ede75793 | 12 | #endif /* not lint */ |
bcf1365c | 13 | |
22d4760e | 14 | #ifndef lint |
e83bda0c | 15 | static char sccsid[] = "@(#)login.c 5.61 (Berkeley) %G%"; |
ede75793 | 16 | #endif /* not lint */ |
22d4760e | 17 | |
88a01c09 BJ |
18 | /* |
19 | * login [ name ] | |
d0faf133 | 20 | * login -r hostname (for rlogind) |
5dbe2745 MK |
21 | * login -h hostname (for telnetd, etc.) |
22 | * login -f name (for pre-authenticated login: datakit, xterm, etc.) | |
88a01c09 BJ |
23 | */ |
24 | ||
7a625b73 | 25 | #include <sys/param.h> |
3b8dd95e SL |
26 | #include <sys/stat.h> |
27 | #include <sys/time.h> | |
28 | #include <sys/resource.h> | |
9479aa87 | 29 | #include <sys/file.h> |
2d4a74b8 | 30 | #include <sgtty.h> |
3b8dd95e | 31 | |
88a01c09 BJ |
32 | #include <utmp.h> |
33 | #include <signal.h> | |
22d4760e | 34 | #include <errno.h> |
9479aa87 BJ |
35 | #include <ttyent.h> |
36 | #include <syslog.h> | |
c89291e2 | 37 | #include <grp.h> |
80f91e3f | 38 | #include <pwd.h> |
ede75793 | 39 | #include <setjmp.h> |
80f91e3f | 40 | #include <stdio.h> |
38dde0cd | 41 | #include <string.h> |
0e08a2b3 | 42 | #include <tzfile.h> |
fb6e7f7e | 43 | #include "pathnames.h" |
c89291e2 | 44 | |
80f91e3f | 45 | #define TTYGRPNAME "tty" /* name of group to own ttys */ |
88a01c09 | 46 | |
3b8dd95e | 47 | /* |
80f91e3f KB |
48 | * This bounds the time given to login. Not a define so it can |
49 | * be patched on machines where it's too small. | |
3b8dd95e | 50 | */ |
a9b031fe | 51 | int timeout = 300; |
9bcf1061 KB |
52 | #ifdef KERBEROS |
53 | int notickets = 1; | |
54 | #endif | |
86eb6c9e | 55 | |
659be7fb MK |
56 | struct passwd *pwd; |
57 | int failures; | |
657e5a3c | 58 | char term[64], *envinit[1], *hostname, *username, *tty; |
88a01c09 | 59 | |
659be7fb MK |
60 | struct sgttyb sgttyb; |
61 | struct tchars tc = { | |
714accc5 SL |
62 | CINTR, CQUIT, CSTART, CSTOP, CEOT, CBRK |
63 | }; | |
659be7fb | 64 | struct ltchars ltc = { |
9bcf1061 | 65 | CSUSP, CDSUSP, CRPRNT, CDISCARD, CWERASE, CLNEXT |
841d84b0 BJ |
66 | }; |
67 | ||
c91c86eb KB |
68 | char *months[] = |
69 | { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", | |
70 | "Sep", "Oct", "Nov", "Dec" }; | |
71 | ||
88a01c09 | 72 | main(argc, argv) |
80f91e3f KB |
73 | int argc; |
74 | char **argv; | |
88a01c09 | 75 | { |
9bcf1061 | 76 | extern int optind; |
80f91e3f | 77 | extern char *optarg, **environ; |
c91c86eb KB |
78 | struct timeval tp; |
79 | struct tm *ttp; | |
83cc7253 | 80 | struct timeval tp; |
80f91e3f KB |
81 | struct group *gr; |
82 | register int ch; | |
659be7fb | 83 | register char *p; |
d0faf133 | 84 | int ask, fflag, hflag, pflag, rflag, cnt; |
cb400a97 | 85 | int quietlog, ioctlval, rval; |
9bcf1061 | 86 | char *domain, *salt, *ttyn; |
a339b173 | 87 | char tbuf[MAXPATHLEN + 2], tname[sizeof(_PATH_TTY) + 10]; |
e2a7a526 | 88 | char localhost[MAXHOSTNAMELEN]; |
c91c86eb | 89 | char *ctime(), *ttyname(), *stypeof(), *crypt(), *getpass(); |
80f91e3f KB |
90 | time_t time(); |
91 | off_t lseek(); | |
9bcf1061 | 92 | void timedout(); |
80f91e3f KB |
93 | |
94 | (void)signal(SIGALRM, timedout); | |
95 | (void)alarm((u_int)timeout); | |
96 | (void)signal(SIGQUIT, SIG_IGN); | |
97 | (void)signal(SIGINT, SIG_IGN); | |
98 | (void)setpriority(PRIO_PROCESS, 0, 0); | |
80f91e3f | 99 | |
9e76b9f1 KB |
100 | openlog("login", LOG_ODELAY, LOG_AUTH); |
101 | ||
3b8dd95e | 102 | /* |
d3737d51 | 103 | * -p is used by getty to tell login not to destroy the environment |
d0faf133 | 104 | * -r is used by rlogind to cause the autologin protocol; |
5dbe2745 | 105 | * -f is used to skip a second login authentication |
ee1eff74 MK |
106 | * -h is used by other servers to pass the name of the remote |
107 | * host to login so that it may be placed in utmp and wtmp | |
3b8dd95e | 108 | */ |
e2a7a526 KF |
109 | domain = NULL; |
110 | if (gethostname(localhost, sizeof(localhost)) < 0) | |
111 | syslog(LOG_ERR, "couldn't get local hostname: %m"); | |
112 | else | |
113 | domain = index(localhost, '.'); | |
80f91e3f | 114 | |
d0faf133 | 115 | fflag = hflag = pflag = rflag = 0; |
2d4a74b8 | 116 | uid = getuid(); |
d0faf133 | 117 | while ((ch = getopt(argc, argv, "fh:pr:")) != EOF) |
ede75793 | 118 | switch (ch) { |
80f91e3f | 119 | case 'f': |
d0faf133 KB |
120 | if (rflag) { |
121 | fprintf(stderr, | |
122 | "login: only one of -r and -f allowed.\n"); | |
123 | exit(1); | |
124 | } | |
80f91e3f | 125 | fflag = 1; |
80f91e3f KB |
126 | break; |
127 | case 'h': | |
2d4a74b8 | 128 | if (uid) { |
0e08a2b3 | 129 | (void)fprintf(stderr, |
80f91e3f | 130 | "login: -h for super-user only.\n"); |
b6f7cade | 131 | exit(1); |
5dbe2745 | 132 | } |
d0faf133 KB |
133 | if (rflag) { |
134 | fprintf(stderr, | |
135 | "login: only one of -r and -h allowed.\n"); | |
136 | exit(1); | |
137 | } | |
80f91e3f KB |
138 | hflag = 1; |
139 | if (domain && (p = index(optarg, '.')) && | |
abc4056e | 140 | strcasecmp(p, domain) == 0) |
80f91e3f KB |
141 | *p = 0; |
142 | hostname = optarg; | |
143 | break; | |
144 | case 'p': | |
d3737d51 | 145 | pflag = 1; |
80f91e3f | 146 | break; |
d0faf133 KB |
147 | case 'r': |
148 | if (hflag || fflag) { | |
149 | fprintf(stderr, | |
150 | "login: -f and -h not allowed with -r.\n"); | |
151 | exit(1); | |
152 | } | |
153 | if (getuid()) { | |
154 | fprintf(stderr, | |
155 | "login: -r for super-user only.\n"); | |
156 | exit(1); | |
157 | } | |
158 | /* "-r hostname" must be last args */ | |
159 | if (optind != argc) { | |
160 | fprintf(stderr, "Syntax error.\n"); | |
161 | exit(1); | |
162 | } | |
163 | rflag = 1; | |
164 | passwd_req = (doremotelogin(optarg) == -1); | |
165 | if (domain && (p = index(optarg, '.')) && | |
166 | !strcmp(p, domain)) | |
167 | *p = '\0'; | |
168 | hostname = optarg; | |
169 | break; | |
80f91e3f KB |
170 | case '?': |
171 | default: | |
2d4a74b8 KF |
172 | if (!uid) |
173 | syslog(LOG_ERR, "invalid flag %c", ch); | |
0e08a2b3 KB |
174 | (void)fprintf(stderr, |
175 | "usage: login [-fp] [username]\n"); | |
80f91e3f | 176 | exit(1); |
d3737d51 | 177 | } |
80f91e3f KB |
178 | argc -= optind; |
179 | argv += optind; | |
f0c0c252 | 180 | if (*argv) { |
80f91e3f | 181 | username = *argv; |
9bcf1061 KB |
182 | if (strlen(username) > UT_NAMESIZE) |
183 | username[UT_NAMESIZE] = '\0'; | |
659be7fb MK |
184 | ask = 0; |
185 | } else | |
f0c0c252 | 186 | ask = 1; |
d0faf133 KB |
187 | if (rflag) |
188 | ask = 0; | |
80f91e3f KB |
189 | |
190 | ioctlval = 0; | |
191 | (void)ioctl(0, TIOCLSET, &ioctlval); | |
192 | (void)ioctl(0, TIOCNXCL, 0); | |
ede75793 KB |
193 | (void)fcntl(0, F_SETFL, ioctlval); |
194 | (void)ioctl(0, TIOCGETP, &sgttyb); | |
d0faf133 KB |
195 | |
196 | /* | |
197 | * If talking to an rlogin process, propagate the terminal type and | |
198 | * baud rate across the network. | |
199 | */ | |
200 | if (rflag) | |
201 | doremoteterm(&sgttyb); | |
ede75793 KB |
202 | sgttyb.sg_erase = CERASE; |
203 | sgttyb.sg_kill = CKILL; | |
80f91e3f KB |
204 | (void)ioctl(0, TIOCSLTC, <c); |
205 | (void)ioctl(0, TIOCSETC, &tc); | |
ede75793 | 206 | (void)ioctl(0, TIOCSETP, &sgttyb); |
80f91e3f KB |
207 | |
208 | for (cnt = getdtablesize(); cnt > 2; cnt--) | |
209 | close(cnt); | |
210 | ||
88a01c09 | 211 | ttyn = ttyname(0); |
a339b173 KB |
212 | if (ttyn == NULL || *ttyn == '\0') { |
213 | (void)sprintf(tname, "%s??", _PATH_TTY); | |
214 | ttyn = tname; | |
215 | } | |
80f91e3f KB |
216 | if (tty = rindex(ttyn, '/')) |
217 | ++tty; | |
9479aa87 | 218 | else |
80f91e3f KB |
219 | tty = ttyn; |
220 | ||
f0c0c252 | 221 | for (cnt = 0;; ask = 1) { |
7e845b92 | 222 | ioctlval = TTYDISC; |
80f91e3f KB |
223 | (void)ioctl(0, TIOCSETD, &ioctlval); |
224 | ||
f0c0c252 | 225 | if (ask) { |
ede75793 | 226 | fflag = 0; |
80f91e3f | 227 | getloginname(); |
ede75793 | 228 | } |
659be7fb | 229 | /* |
2d4a74b8 KF |
230 | * Note if trying multiple user names; log failures for |
231 | * previous user name, but don't bother logging one failure | |
659be7fb MK |
232 | * for nonexistent name (mistyped username). |
233 | */ | |
234 | if (failures && strcmp(tbuf, username)) { | |
235 | if (failures > (pwd ? 0 : 1)) | |
f0c0c252 | 236 | badlogin(tbuf); |
659be7fb | 237 | failures = 0; |
f0c0c252 | 238 | } |
659be7fb | 239 | (void)strcpy(tbuf, username); |
320a5e84 | 240 | |
ede75793 KB |
241 | if (pwd = getpwnam(username)) |
242 | salt = pwd->pw_passwd; | |
320a5e84 KB |
243 | else |
244 | salt = "xx"; | |
80f91e3f | 245 | |
3b8dd95e | 246 | /* |
cb400a97 KB |
247 | * if we have a valid account name, and it doesn't have a |
248 | * password, or the -f option was specified and the caller | |
249 | * is root or the caller isn't changing their uid, don't | |
250 | * authenticate. | |
3b8dd95e | 251 | */ |
cb400a97 KB |
252 | if (pwd && (*pwd->pw_passwd == '\0' || |
253 | fflag && (uid == 0 || uid == pwd->pw_uid))) | |
80f91e3f | 254 | break; |
e83bda0c | 255 | fflag = 0; |
80f91e3f | 256 | |
2d4a74b8 KF |
257 | /* |
258 | * If trying to log in as root, but with insecure terminal, | |
259 | * refuse the login attempt. | |
260 | */ | |
320a5e84 | 261 | if (pwd && pwd->pw_uid == 0 && !rootterm(tty)) { |
2d4a74b8 KF |
262 | (void)fprintf(stderr, |
263 | "%s login refused on this terminal.\n", | |
264 | pwd->pw_name); | |
265 | if (hostname) | |
266 | syslog(LOG_NOTICE, | |
267 | "LOGIN %s REFUSED FROM %s ON TTY %s", | |
268 | pwd->pw_name, hostname, tty); | |
269 | else | |
270 | syslog(LOG_NOTICE, | |
271 | "LOGIN %s REFUSED ON TTY %s", | |
272 | pwd->pw_name, tty); | |
273 | continue; | |
274 | } | |
275 | ||
9bcf1061 | 276 | (void)setpriority(PRIO_PROCESS, 0, -4); |
320a5e84 | 277 | |
9bcf1061 | 278 | p = getpass("Password:"); |
9d6a5a69 | 279 | |
320a5e84 | 280 | if (pwd) { |
9bcf1061 | 281 | bzero(p, strlen(p)); |
320a5e84 KB |
282 | |
283 | (void)setpriority(PRIO_PROCESS, 0, 0); | |
284 | ||
285 | if (pwd && !rval) | |
80f91e3f KB |
286 | break; |
287 | ||
320a5e84 | 288 | (void)printf("Login incorrect\n"); |
659be7fb | 289 | failures++; |
f0c0c252 KB |
290 | /* we allow 10 tries, but after 3 we start backing off */ |
291 | if (++cnt > 3) { | |
292 | if (cnt >= 10) { | |
293 | badlogin(username); | |
294 | (void)ioctl(0, TIOCHPCL, (struct sgttyb *)NULL); | |
295 | sleepexit(1); | |
296 | } | |
297 | sleep((u_int)((cnt - 3) * 5)); | |
f570e1ff | 298 | } |
80f91e3f | 299 | } |
88a01c09 | 300 | |
80f91e3f KB |
301 | /* committed to login -- turn off timeout */ |
302 | (void)alarm((u_int)0); | |
303 | ||
a339b173 KB |
304 | /* paranoia... */ |
305 | endpwent(); | |
306 | ||
c374ff7b KB |
307 | /* if user not super-user, check for disabled logins */ |
308 | if (pwd->pw_uid) | |
309 | checknologin(); | |
310 | ||
ede75793 | 311 | if (chdir(pwd->pw_dir) < 0) { |
0e08a2b3 | 312 | (void)printf("No directory %s!\n", pwd->pw_dir); |
ede75793 KB |
313 | if (chdir("/")) |
314 | exit(0); | |
315 | pwd->pw_dir = "/"; | |
0e08a2b3 | 316 | (void)printf("Logging in with home = \"/\".\n"); |
ede75793 | 317 | } |
83cc7253 KB |
318 | #define TWOWEEKS (14*24*60*60) |
319 | if (pwd->pw_change || pwd->pw_expire) | |
320 | (void)gettimeofday(&tp, (struct timezone *)NULL); | |
321 | if (pwd->pw_change) | |
322 | if (tp.tv_sec >= pwd->pw_change) { | |
323 | printf("Sorry -- your password has expired.\n"); | |
324 | sleepexit(1); | |
325 | } | |
326 | else if (tp.tv_sec - pwd->pw_change < TWOWEEKS) { | |
327 | ttp = localtime(&pwd->pw_change); | |
328 | printf("Warning: your password expires on %s %d, 19%d\n", | |
329 | months[ttp->tm_mon], ttp->tm_mday, ttp->tm_year); | |
330 | } | |
331 | if (pwd->pw_expire) | |
332 | if (tp.tv_sec >= pwd->pw_expire) { | |
333 | printf("Sorry -- your account has expired.\n"); | |
334 | sleepexit(1); | |
335 | } | |
336 | else if (tp.tv_sec - pwd->pw_expire < TWOWEEKS) { | |
337 | ttp = localtime(&pwd->pw_expire); | |
338 | printf("Warning: your account expires on %s %d, 19%d\n", | |
339 | months[ttp->tm_mon], ttp->tm_mday, ttp->tm_year); | |
340 | } | |
341 | ||
c91c86eb KB |
342 | if (pwd->pw_change || pwd->pw_expire) |
343 | (void)gettimeofday(&tp, (struct timezone *)NULL); | |
344 | if (pwd->pw_change) | |
345 | if (tp.tv_sec >= pwd->pw_change) { | |
0e08a2b3 | 346 | (void)printf("Sorry -- your password has expired.\n"); |
c91c86eb KB |
347 | sleepexit(1); |
348 | } | |
50112e48 | 349 | else if (pwd->pw_change - tp.tv_sec < |
44d9892d KB |
350 | 2 * DAYSPERWEEK * SECSPERDAY && !quietlog) |
351 | (void)printf("Warning: your password expires on %s", | |
352 | ctime(&pwd->pw_expire)); | |
c91c86eb KB |
353 | if (pwd->pw_expire) |
354 | if (tp.tv_sec >= pwd->pw_expire) { | |
0e08a2b3 | 355 | (void)printf("Sorry -- your account has expired.\n"); |
c91c86eb KB |
356 | sleepexit(1); |
357 | } | |
50112e48 | 358 | else if (pwd->pw_expire - tp.tv_sec < |
44d9892d KB |
359 | 2 * DAYSPERWEEK * SECSPERDAY && !quietlog) |
360 | (void)printf("Warning: your account expires on %s", | |
361 | ctime(&pwd->pw_expire)); | |
c91c86eb | 362 | |
ede75793 | 363 | /* nothing else left to fail -- really log in */ |
80f91e3f KB |
364 | { |
365 | struct utmp utmp; | |
366 | ||
9bcf1061 | 367 | bzero((void *)&utmp, sizeof(utmp)); |
80f91e3f KB |
368 | (void)time(&utmp.ut_time); |
369 | strncpy(utmp.ut_name, username, sizeof(utmp.ut_name)); | |
a829b33b KB |
370 | if (hostname) |
371 | strncpy(utmp.ut_host, hostname, sizeof(utmp.ut_host)); | |
80f91e3f KB |
372 | strncpy(utmp.ut_line, tty, sizeof(utmp.ut_line)); |
373 | login(&utmp); | |
f570e1ff | 374 | } |
80f91e3f | 375 | |
f0c0c252 | 376 | dolastlog(quietlog); |
80f91e3f | 377 | |
d0faf133 | 378 | if (!hflag && !rflag) { /* XXX */ |
80f91e3f KB |
379 | static struct winsize win = { 0, 0, 0, 0 }; |
380 | ||
381 | (void)ioctl(0, TIOCSWINSZ, &win); | |
382 | } | |
383 | ||
384 | (void)chown(ttyn, pwd->pw_uid, | |
385 | (gr = getgrnam(TTYGRPNAME)) ? gr->gr_gid : pwd->pw_gid); | |
386 | (void)chmod(ttyn, 0620); | |
387 | (void)setgid(pwd->pw_gid); | |
388 | ||
389 | initgroups(username, pwd->pw_gid); | |
390 | ||
ede75793 | 391 | if (*pwd->pw_shell == '\0') |
fb6e7f7e | 392 | pwd->pw_shell = _PATH_BSHELL; |
ede75793 | 393 | |
80f91e3f | 394 | /* destroy environment unless user has requested preservation */ |
d3737d51 SL |
395 | if (!pflag) |
396 | environ = envinit; | |
ede75793 KB |
397 | (void)setenv("HOME", pwd->pw_dir, 1); |
398 | (void)setenv("SHELL", pwd->pw_shell, 1); | |
d3737d51 | 399 | if (term[0] == '\0') |
659be7fb | 400 | strncpy(term, stypeof(tty), sizeof(term)); |
ede75793 KB |
401 | (void)setenv("TERM", term, 0); |
402 | (void)setenv("USER", pwd->pw_name, 1); | |
c502712b | 403 | (void)setenv("PATH", _PATH_DEFPATH, 0); |
d3737d51 | 404 | |
9479aa87 | 405 | if (tty[sizeof("tty")-1] == 'd') |
d3737d51 | 406 | syslog(LOG_INFO, "DIALUP %s, %s", tty, pwd->pw_name); |
e83bda0c MK |
407 | /* if fflag is on, assume caller/authenticator has logged root login */ |
408 | if (pwd->pw_uid == 0 && fflag == 0) | |
80f91e3f | 409 | if (hostname) |
659be7fb | 410 | syslog(LOG_NOTICE, "ROOT LOGIN ON %s FROM %s", |
f0c0c252 | 411 | tty, hostname); |
df9d9536 | 412 | else |
659be7fb | 413 | syslog(LOG_NOTICE, "ROOT LOGIN ON %s", tty); |
80f91e3f | 414 | |
9bcf1061 KB |
415 | #ifdef KERBEROS |
416 | if (!quietlog && notickets == 1) | |
417 | (void)printf("Warning: no Kerberos tickets issued.\n"); | |
418 | #endif | |
419 | ||
4f8d3876 | 420 | if (!quietlog) { |
6821e9c5 | 421 | struct stat st; |
d3737d51 | 422 | |
80f91e3f | 423 | motd(); |
fb6e7f7e | 424 | (void)sprintf(tbuf, "%s/%s", _PATH_MAILDIR, pwd->pw_name); |
80f91e3f | 425 | if (stat(tbuf, &st) == 0 && st.st_size != 0) |
0e08a2b3 | 426 | (void)printf("You have %smail.\n", |
80f91e3f | 427 | (st.st_mtime > st.st_atime) ? "new " : ""); |
f570e1ff | 428 | } |
80f91e3f KB |
429 | |
430 | (void)signal(SIGALRM, SIG_DFL); | |
431 | (void)signal(SIGQUIT, SIG_DFL); | |
432 | (void)signal(SIGINT, SIG_DFL); | |
433 | (void)signal(SIGTSTP, SIG_IGN); | |
434 | ||
435 | tbuf[0] = '-'; | |
436 | strcpy(tbuf + 1, (p = rindex(pwd->pw_shell, '/')) ? | |
437 | p + 1 : pwd->pw_shell); | |
0e08a2b3 | 438 | |
3d28f6dd KS |
439 | if (setlogin(pwd->pw_name) < 0) |
440 | syslog(LOG_ERR, "setlogin() failure: %m"); | |
a339b173 | 441 | |
0e08a2b3 KB |
442 | /* discard permissions last so can't get killed and drop core */ |
443 | (void)setuid(pwd->pw_uid); | |
444 | ||
80f91e3f | 445 | execlp(pwd->pw_shell, tbuf, 0); |
0e08a2b3 | 446 | (void)fprintf(stderr, "login: no shell: %s.\n", strerror(errno)); |
88a01c09 BJ |
447 | exit(0); |
448 | } | |
449 | ||
80f91e3f | 450 | getloginname() |
3b8dd95e | 451 | { |
80f91e3f KB |
452 | register int ch; |
453 | register char *p; | |
454 | static char nbuf[UT_NAMESIZE + 1]; | |
3b8dd95e | 455 | |
80f91e3f | 456 | for (;;) { |
0e08a2b3 | 457 | (void)printf("login: "); |
ede75793 | 458 | for (p = nbuf; (ch = getchar()) != '\n'; ) { |
f0c0c252 KB |
459 | if (ch == EOF) { |
460 | badlogin(username); | |
3b8dd95e | 461 | exit(0); |
f0c0c252 | 462 | } |
ede75793 | 463 | if (p < nbuf + UT_NAMESIZE) |
80f91e3f | 464 | *p++ = ch; |
3b8dd95e | 465 | } |
80f91e3f KB |
466 | if (p > nbuf) |
467 | if (nbuf[0] == '-') | |
0e08a2b3 | 468 | (void)fprintf(stderr, |
80f91e3f KB |
469 | "login names may not start with '-'.\n"); |
470 | else { | |
471 | *p = '\0'; | |
472 | username = nbuf; | |
ede75793 | 473 | break; |
80f91e3f | 474 | } |
3b8dd95e | 475 | } |
3b8dd95e SL |
476 | } |
477 | ||
9bcf1061 | 478 | void |
3b8dd95e SL |
479 | timedout() |
480 | { | |
0e08a2b3 | 481 | (void)fprintf(stderr, "Login timed out after %d seconds\n", timeout); |
3b8dd95e SL |
482 | exit(0); |
483 | } | |
484 | ||
659be7fb MK |
485 | rootterm(ttyn) |
486 | char *ttyn; | |
88a01c09 | 487 | { |
80f91e3f | 488 | struct ttyent *t; |
1886582e | 489 | |
659be7fb | 490 | return((t = getttynam(ttyn)) && t->ty_status&TTY_SECURE); |
88a01c09 BJ |
491 | } |
492 | ||
ede75793 KB |
493 | jmp_buf motdinterrupt; |
494 | ||
80f91e3f | 495 | motd() |
f570e1ff | 496 | { |
ede75793 | 497 | register int fd, nchars; |
b32e4ad7 KF |
498 | sig_t oldint; |
499 | int sigint(); | |
ede75793 | 500 | char tbuf[8192]; |
80f91e3f | 501 | |
fb6e7f7e | 502 | if ((fd = open(_PATH_MOTDFILE, O_RDONLY, 0)) < 0) |
80f91e3f KB |
503 | return; |
504 | oldint = signal(SIGINT, sigint); | |
ede75793 KB |
505 | if (setjmp(motdinterrupt) == 0) |
506 | while ((nchars = read(fd, tbuf, sizeof(tbuf))) > 0) | |
507 | (void)write(fileno(stdout), tbuf, nchars); | |
80f91e3f | 508 | (void)signal(SIGINT, oldint); |
ede75793 | 509 | (void)close(fd); |
80f91e3f | 510 | } |
9479aa87 | 511 | |
80f91e3f KB |
512 | sigint() |
513 | { | |
ede75793 | 514 | longjmp(motdinterrupt, 1); |
80f91e3f KB |
515 | } |
516 | ||
517 | checknologin() | |
518 | { | |
519 | register int fd, nchars; | |
ede75793 | 520 | char tbuf[8192]; |
80f91e3f | 521 | |
fb6e7f7e | 522 | if ((fd = open(_PATH_NOLOGIN, O_RDONLY, 0)) >= 0) { |
80f91e3f KB |
523 | while ((nchars = read(fd, tbuf, sizeof(tbuf))) > 0) |
524 | (void)write(fileno(stdout), tbuf, nchars); | |
525 | sleepexit(0); | |
f570e1ff | 526 | } |
f570e1ff BJ |
527 | } |
528 | ||
f0c0c252 | 529 | dolastlog(quiet) |
80f91e3f | 530 | int quiet; |
88a01c09 | 531 | { |
80f91e3f KB |
532 | struct lastlog ll; |
533 | int fd; | |
c91c86eb | 534 | char *ctime(); |
83cc7253 | 535 | char *ctime(); |
80f91e3f | 536 | |
fb6e7f7e | 537 | if ((fd = open(_PATH_LASTLOG, O_RDWR, 0)) >= 0) { |
ede75793 | 538 | (void)lseek(fd, (off_t)pwd->pw_uid * sizeof(ll), L_SET); |
80f91e3f KB |
539 | if (!quiet) { |
540 | if (read(fd, (char *)&ll, sizeof(ll)) == sizeof(ll) && | |
541 | ll.ll_time != 0) { | |
0e08a2b3 | 542 | (void)printf("Last login: %.*s ", |
80f91e3f KB |
543 | 24-5, (char *)ctime(&ll.ll_time)); |
544 | if (*ll.ll_host != '\0') | |
0e08a2b3 | 545 | (void)printf("from %.*s\n", |
80f91e3f KB |
546 | sizeof(ll.ll_host), ll.ll_host); |
547 | else | |
0e08a2b3 | 548 | (void)printf("on %.*s\n", |
80f91e3f KB |
549 | sizeof(ll.ll_line), ll.ll_line); |
550 | } | |
ede75793 | 551 | (void)lseek(fd, (off_t)pwd->pw_uid * sizeof(ll), L_SET); |
80f91e3f | 552 | } |
9bcf1061 | 553 | bzero((void *)&ll, sizeof(ll)); |
80f91e3f KB |
554 | (void)time(&ll.ll_time); |
555 | strncpy(ll.ll_line, tty, sizeof(ll.ll_line)); | |
a829b33b KB |
556 | if (hostname) |
557 | strncpy(ll.ll_host, hostname, sizeof(ll.ll_host)); | |
80f91e3f KB |
558 | (void)write(fd, (char *)&ll, sizeof(ll)); |
559 | (void)close(fd); | |
88a01c09 | 560 | } |
88a01c09 BJ |
561 | } |
562 | ||
f0c0c252 KB |
563 | badlogin(name) |
564 | char *name; | |
565 | { | |
659be7fb | 566 | if (failures == 0) |
f0c0c252 KB |
567 | return; |
568 | if (hostname) | |
659be7fb MK |
569 | syslog(LOG_NOTICE, "%d LOGIN FAILURE%s FROM %s, %s", |
570 | failures, failures > 1 ? "S" : "", hostname, name); | |
f0c0c252 | 571 | else |
659be7fb MK |
572 | syslog(LOG_NOTICE, "%d LOGIN FAILURE%s ON %s, %s", |
573 | failures, failures > 1 ? "S" : "", tty, name); | |
f0c0c252 KB |
574 | } |
575 | ||
f570e1ff | 576 | #undef UNKNOWN |
80f91e3f | 577 | #define UNKNOWN "su" |
88a01c09 BJ |
578 | |
579 | char * | |
659be7fb MK |
580 | stypeof(ttyid) |
581 | char *ttyid; | |
88a01c09 | 582 | { |
80f91e3f | 583 | struct ttyent *t; |
88a01c09 | 584 | |
659be7fb | 585 | return(ttyid && (t = getttynam(ttyid)) ? t->ty_type : UNKNOWN); |
88a01c09 | 586 | } |
86eb6c9e | 587 | |
80f91e3f KB |
588 | sleepexit(eval) |
589 | int eval; | |
c89291e2 | 590 | { |
80f91e3f KB |
591 | sleep((u_int)5); |
592 | exit(eval); | |
c89291e2 | 593 | } |
d0faf133 KB |
594 | |
595 | doremotelogin(host) | |
596 | char *host; | |
597 | { | |
598 | static char lusername[UT_NAMESIZE+1]; | |
599 | char rusername[UT_NAMESIZE+1]; | |
600 | ||
601 | getstr(rusername, sizeof(rusername), "remuser"); | |
602 | getstr(lusername, sizeof(lusername), "locuser"); | |
603 | getstr(term, sizeof(term), "Terminal type"); | |
604 | username = lusername; | |
605 | pwd = getpwnam(username); | |
606 | if (pwd == NULL) | |
607 | return(-1); | |
608 | return(ruserok(host, (pwd->pw_uid == 0), rusername, username)); | |
609 | } | |
610 | ||
611 | char *speeds[] = { | |
612 | "0", "50", "75", "110", "134", "150", "200", "300", "600", | |
613 | "1200", "1800", "2400", "4800", "9600", "19200", "38400", | |
614 | }; | |
615 | #define NSPEEDS (sizeof(speeds) / sizeof(speeds[0])) | |
616 | ||
617 | doremoteterm(tp) | |
618 | struct sgttyb *tp; | |
619 | { | |
620 | register char *cp = index(term, '/'), **cpp; | |
621 | char *speed; | |
622 | ||
623 | if (cp) { | |
624 | *cp++ = '\0'; | |
625 | speed = cp; | |
626 | cp = index(speed, '/'); | |
627 | if (cp) | |
628 | *cp++ = '\0'; | |
629 | for (cpp = speeds; cpp < &speeds[NSPEEDS]; cpp++) | |
630 | if (strcmp(*cpp, speed) == 0) { | |
631 | tp->sg_ispeed = tp->sg_ospeed = cpp-speeds; | |
632 | break; | |
633 | } | |
634 | } | |
635 | tp->sg_flags = ECHO|CRMOD|ANYP|XTABS; | |
636 | } |