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