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