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