1 /*-
2 * Copyright (c) 1980, 1987, 1988, 1991 The Regents of the University
3 * of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34 #ifndef lint
35 char copyright[] =
36 "@(#) Copyright (c) 1980, 1987, 1988, 1991 The Regents of the University of California.\n\
37 All rights reserved.\n";
38 #endif /* not lint */
39
40 #ifndef lint
41 static char sccsid[] = "@(#)login.c 5.73 (Berkeley) 6/29/91";
42 #endif /* not lint */
43
44 /*
45 * login [ name ]
46 * login -h hostname (for telnetd, etc.)
47 * login -f name (for pre-authenticated login: datakit, xterm, etc.)
48 */
49
50 #include <sys/param.h>
51 #include <sys/stat.h>
52 #include <sys/time.h>
53 #include <sys/resource.h>
54 #include <sys/file.h>
55
56 #include <utmp.h>
57 #include <signal.h>
58 #include <errno.h>
59 #include <ttyent.h>
60 #include <syslog.h>
61 #include <grp.h>
62 #include <pwd.h>
63 #include <setjmp.h>
64 #include <stdio.h>
65 #include <string.h>
66 #include <tzfile.h>
67 #include "pathnames.h"
68
69 #define TTYGRPNAME "tty" /* name of group to own ttys */
70
71 /*
72 * This bounds the time given to login. Not a define so it can
73 * be patched on machines where it's too small.
74 */
75 int timeout = 300;
76 int rootlogin;
77 #ifdef KERBEROS
78 int notickets = 1;
79 char *instance;
80 char *krbtkfile_env;
81 int authok;
82 #endif
83
84 struct passwd *pwd;
85 int failures;
86 char term[64], *envinit[1], *hostname, *username, *tty;
87
main(argc,argv)88 main(argc, argv)
89 int argc;
90 char **argv;
91 {
92 extern int optind;
93 extern char *optarg, **environ;
94 struct timeval tp;
95 struct group *gr;
96 register int ch;
97 register char *p;
98 int ask, fflag, hflag, pflag, cnt, uid;
99 int quietlog, rval;
100 char *domain, *salt, *ttyn;
101 char tbuf[MAXPATHLEN + 2], tname[sizeof(_PATH_TTY) + 10];
102 char localhost[MAXHOSTNAMELEN];
103 char *ctime(), *ttyname(), *stypeof(), *crypt(), *getpass();
104 time_t time();
105 void timedout();
106
107 (void)signal(SIGALRM, timedout);
108 (void)alarm((u_int)timeout);
109 (void)signal(SIGQUIT, SIG_IGN);
110 (void)signal(SIGINT, SIG_IGN);
111 (void)setpriority(PRIO_PROCESS, 0, 0);
112
113 openlog("login", LOG_ODELAY, LOG_AUTH);
114
115 /*
116 * -p is used by getty to tell login not to destroy the environment
117 * -f is used to skip a second login authentication
118 * -h is used by other servers to pass the name of the remote
119 * host to login so that it may be placed in utmp and wtmp
120 */
121 domain = NULL;
122 if (gethostname(localhost, sizeof(localhost)) < 0)
123 syslog(LOG_ERR, "couldn't get local hostname: %m");
124 else
125 domain = index(localhost, '.');
126
127 fflag = hflag = pflag = 0;
128 uid = getuid();
129 while ((ch = getopt(argc, argv, "fh:p")) != EOF)
130 switch (ch) {
131 case 'f':
132 fflag = 1;
133 break;
134 case 'h':
135 if (uid) {
136 (void)fprintf(stderr,
137 "login: -h option: %s\n", strerror(EPERM));
138 exit(1);
139 }
140 hflag = 1;
141 if (domain && (p = index(optarg, '.')) &&
142 strcasecmp(p, domain) == 0)
143 *p = 0;
144 hostname = optarg;
145 break;
146 case 'p':
147 pflag = 1;
148 break;
149 case '?':
150 default:
151 if (!uid)
152 syslog(LOG_ERR, "invalid flag %c", ch);
153 (void)fprintf(stderr,
154 "usage: login [-fp] [-h hostname] [username]\n");
155 exit(1);
156 }
157 argc -= optind;
158 argv += optind;
159 if (*argv) {
160 username = *argv;
161 ask = 0;
162 } else
163 ask = 1;
164
165 for (cnt = getdtablesize(); cnt > 2; cnt--)
166 close(cnt);
167
168 ttyn = ttyname(0);
169 if (ttyn == NULL || *ttyn == '\0') {
170 (void)sprintf(tname, "%s??", _PATH_TTY);
171 ttyn = tname;
172 }
173 if (tty = rindex(ttyn, '/'))
174 ++tty;
175 else
176 tty = ttyn;
177
178 for (cnt = 0;; ask = 1) {
179 if (ask) {
180 fflag = 0;
181 getloginname();
182 }
183 #ifdef KERBEROS
184 if ((instance = index(username, '.')) != NULL) {
185 if (strncmp(instance, ".root", 5) == 0)
186 rootlogin++;
187 *instance++ = '\0';
188 } else {
189 rootlogin = 0;
190 instance = "";
191 }
192 #else
193 rootlogin = 0;
194 #endif
195 if (strlen(username) > UT_NAMESIZE)
196 username[UT_NAMESIZE] = '\0';
197
198 /*
199 * Note if trying multiple user names; log failures for
200 * previous user name, but don't bother logging one failure
201 * for nonexistent name (mistyped username).
202 */
203 if (failures && strcmp(tbuf, username)) {
204 if (failures > (pwd ? 0 : 1))
205 badlogin(tbuf);
206 failures = 0;
207 }
208 (void)strcpy(tbuf, username);
209
210 if (pwd = getpwnam(username))
211 salt = pwd->pw_passwd;
212 else
213 salt = "xx";
214
215 /*
216 * if we have a valid account name, and it doesn't have a
217 * password, or the -f option was specified and the caller
218 * is root or the caller isn't changing their uid, don't
219 * authenticate.
220 */
221 if (pwd && (*pwd->pw_passwd == '\0' ||
222 fflag && (uid == 0 || uid == pwd->pw_uid)))
223 break;
224 fflag = 0;
225 if (pwd && pwd->pw_uid == 0)
226 rootlogin = 1;
227
228 (void)setpriority(PRIO_PROCESS, 0, -4);
229
230 p = getpass("Password:");
231
232 if (pwd) {
233 #ifdef KERBEROS
234 rval = klogin(pwd, instance, localhost, p);
235 if (rval == 0)
236 authok = 1;
237 else if (rval == 1) {
238 if (pwd->pw_uid != 0)
239 rootlogin = 0;
240 rval = strcmp(crypt(p, salt), pwd->pw_passwd);
241 }
242 #else
243 if (pwd->pw_uid != 0)
244 rootlogin = 0;
245 #ifdef DES
246 rval = strcmp(crypt(p, salt), pwd->pw_passwd);
247 #else
248 rval = strcmp(p, pwd->pw_passwd);
249 #endif
250 #endif
251 }
252 bzero(p, strlen(p));
253
254 (void)setpriority(PRIO_PROCESS, 0, 0);
255
256 /*
257 * If trying to log in as root without Kerberos,
258 * but with insecure terminal, refuse the login attempt.
259 */
260 #ifdef KERBEROS
261 if (authok == 0)
262 #endif
263 if (pwd && rootlogin && !rootterm(tty)) {
264 (void)fprintf(stderr,
265 "%s login refused on this terminal.\n",
266 pwd->pw_name);
267 if (hostname)
268 syslog(LOG_NOTICE,
269 "LOGIN %s REFUSED FROM %s ON TTY %s",
270 pwd->pw_name, hostname, tty);
271 else
272 syslog(LOG_NOTICE,
273 "LOGIN %s REFUSED ON TTY %s",
274 pwd->pw_name, tty);
275 continue;
276 }
277
278 if (pwd && !rval)
279 break;
280
281 (void)printf("Login incorrect\n");
282 failures++;
283 /* we allow 10 tries, but after 3 we start backing off */
284 if (++cnt > 3) {
285 if (cnt >= 10) {
286 badlogin(username);
287 sleepexit(1);
288 }
289 sleep((u_int)((cnt - 3) * 5));
290 }
291 }
292
293 /* committed to login -- turn off timeout */
294 (void)alarm((u_int)0);
295
296 endpwent();
297
298 /* if user not super-user, check for disabled logins */
299 if (!rootlogin)
300 checknologin();
301
302 if (chdir(pwd->pw_dir) < 0) {
303 (void)printf("No home directory %s!\n", pwd->pw_dir);
304 if (chdir("/"))
305 exit(0);
306 pwd->pw_dir = "/";
307 (void)printf("Logging in with home = \"/\".\n");
308 }
309
310 quietlog = access(_PATH_HUSHLOGIN, F_OK) == 0;
311
312 if (pwd->pw_change || pwd->pw_expire)
313 (void)gettimeofday(&tp, (struct timezone *)NULL);
314 if (pwd->pw_change)
315 if (tp.tv_sec >= pwd->pw_change) {
316 (void)printf("Sorry -- your password has expired.\n");
317 sleepexit(1);
318 } else if (pwd->pw_change - tp.tv_sec <
319 2 * DAYSPERWEEK * SECSPERDAY && !quietlog)
320 (void)printf("Warning: your password expires on %s",
321 ctime(&pwd->pw_change));
322 if (pwd->pw_expire)
323 if (tp.tv_sec >= pwd->pw_expire) {
324 (void)printf("Sorry -- your account has expired.\n");
325 sleepexit(1);
326 } else if (pwd->pw_expire - tp.tv_sec <
327 2 * DAYSPERWEEK * SECSPERDAY && !quietlog)
328 (void)printf("Warning: your account expires on %s",
329 ctime(&pwd->pw_expire));
330
331 /* nothing else left to fail -- really log in */
332 {
333 struct utmp utmp;
334
335 bzero((void *)&utmp, sizeof(utmp));
336 (void)time(&utmp.ut_time);
337 strncpy(utmp.ut_name, username, sizeof(utmp.ut_name));
338 if (hostname)
339 strncpy(utmp.ut_host, hostname, sizeof(utmp.ut_host));
340 strncpy(utmp.ut_line, tty, sizeof(utmp.ut_line));
341 login(&utmp);
342 }
343
344 dolastlog(quietlog);
345
346 (void)chown(ttyn, pwd->pw_uid,
347 (gr = getgrnam(TTYGRPNAME)) ? gr->gr_gid : pwd->pw_gid);
348 (void)setgid(pwd->pw_gid);
349
350 initgroups(username, pwd->pw_gid);
351
352 if (*pwd->pw_shell == '\0')
353 pwd->pw_shell = _PATH_BSHELL;
354
355 /* destroy environment unless user has requested preservation */
356 if (!pflag)
357 environ = envinit;
358 (void)setenv("HOME", pwd->pw_dir, 1);
359 (void)setenv("SHELL", pwd->pw_shell, 1);
360 if (term[0] == '\0')
361 strncpy(term, stypeof(tty), sizeof(term));
362 (void)setenv("TERM", term, 0);
363 (void)setenv("LOGNAME", pwd->pw_name, 1);
364 (void)setenv("USER", pwd->pw_name, 1);
365 (void)setenv("PATH", _PATH_DEFPATH, 0);
366 #ifdef KERBEROS
367 if (krbtkfile_env)
368 (void)setenv("KRBTKFILE", krbtkfile_env, 1);
369 #endif
370
371 if (tty[sizeof("tty")-1] == 'd')
372 syslog(LOG_INFO, "DIALUP %s, %s", tty, pwd->pw_name);
373 /* if fflag is on, assume caller/authenticator has logged root login */
374 if (rootlogin && fflag == 0)
375 if (hostname)
376 syslog(LOG_NOTICE, "ROOT LOGIN (%s) ON %s FROM %s",
377 username, tty, hostname);
378 else
379 syslog(LOG_NOTICE, "ROOT LOGIN (%s) ON %s", username, tty);
380
381 #ifdef KERBEROS
382 if (!quietlog && notickets == 1)
383 (void)printf("Warning: no Kerberos tickets issued.\n");
384 #endif
385
386 if (!quietlog) {
387 struct stat st;
388
389 printf("%s%s",
390 "386BSD Release 1.0 by William and Lynne Jolitz.\n",
391 "Copyright (c) 1989-1994 William F. Jolitz. All Rights Reserved.\n");
392
393 motd();
394 (void)sprintf(tbuf, "%s/%s", _PATH_MAILDIR, pwd->pw_name);
395 if (stat(tbuf, &st) == 0 && st.st_size != 0)
396 (void)printf("You have %smail.\n",
397 (st.st_mtime > st.st_atime) ? "new " : "");
398 }
399
400 (void)signal(SIGALRM, SIG_DFL);
401 (void)signal(SIGQUIT, SIG_DFL);
402 (void)signal(SIGINT, SIG_DFL);
403 (void)signal(SIGTSTP, SIG_IGN);
404
405 tbuf[0] = '-';
406 strcpy(tbuf + 1, (p = rindex(pwd->pw_shell, '/')) ?
407 p + 1 : pwd->pw_shell);
408
409 if (setlogin(pwd->pw_name) < 0)
410 syslog(LOG_ERR, "setlogin() failure: %m");
411
412 /* discard permissions last so can't get killed and drop core */
413 if (rootlogin)
414 (void) setuid(0);
415 else
416 (void) setuid(pwd->pw_uid);
417
418 execlp(pwd->pw_shell, tbuf, 0);
419 (void)fprintf(stderr, "%s: %s\n", pwd->pw_shell, strerror(errno));
420 exit(1);
421 }
422
423 #ifdef KERBEROS
424 #define NBUFSIZ (UT_NAMESIZE + 1 + 5) /* .root suffix */
425 #else
426 #define NBUFSIZ (UT_NAMESIZE + 1)
427 #endif
428
getloginname()429 getloginname()
430 {
431 register int ch;
432 register char *p;
433 static char nbuf[NBUFSIZ];
434
435 for (;;) {
436 (void)printf("login: ");
437 for (p = nbuf; (ch = getchar()) != '\n'; ) {
438 if (ch == EOF) {
439 badlogin(username);
440 exit(0);
441 }
442 if (p < nbuf + (NBUFSIZ - 1))
443 *p++ = ch;
444 }
445 if (p > nbuf)
446 if (nbuf[0] == '-')
447 (void)fprintf(stderr,
448 "login names may not start with '-'.\n");
449 else {
450 *p = '\0';
451 username = nbuf;
452 break;
453 }
454 }
455 }
456
457 void
timedout()458 timedout()
459 {
460 (void)fprintf(stderr, "Login timed out after %d seconds\n", timeout);
461 exit(0);
462 }
463
rootterm(ttyn)464 rootterm(ttyn)
465 char *ttyn;
466 {
467 struct ttyent *t;
468
469 return((t = getttynam(ttyn)) && t->ty_status&TTY_SECURE);
470 }
471
472 jmp_buf motdinterrupt;
473
motd()474 motd()
475 {
476 register int fd, nchars;
477 sig_t oldint;
478 void sigint();
479 char tbuf[8192];
480
481 if ((fd = open(_PATH_MOTDFILE, O_RDONLY, 0)) < 0)
482 return;
483 oldint = signal(SIGINT, sigint);
484 if (setjmp(motdinterrupt) == 0)
485 while ((nchars = read(fd, tbuf, sizeof(tbuf))) > 0)
486 (void)write(fileno(stdout), tbuf, nchars);
487 (void)signal(SIGINT, oldint);
488 (void)close(fd);
489 }
490
491 void
sigint()492 sigint()
493 {
494 longjmp(motdinterrupt, 1);
495 }
496
checknologin()497 checknologin()
498 {
499 register int fd, nchars;
500 char tbuf[8192];
501
502 if ((fd = open(_PATH_NOLOGIN, O_RDONLY, 0)) >= 0) {
503 while ((nchars = read(fd, tbuf, sizeof(tbuf))) > 0)
504 (void)write(fileno(stdout), tbuf, nchars);
505 sleepexit(0);
506 }
507 }
508
dolastlog(quiet)509 dolastlog(quiet)
510 int quiet;
511 {
512 struct lastlog ll;
513 int fd;
514 char *ctime();
515
516 if ((fd = open(_PATH_LASTLOG, O_RDWR, 0)) >= 0) {
517 (void)lseek(fd, (off_t)pwd->pw_uid * sizeof(ll), L_SET);
518 if (!quiet) {
519 if (read(fd, (char *)&ll, sizeof(ll)) == sizeof(ll) &&
520 ll.ll_time != 0) {
521 (void)printf("Last login: %.*s ",
522 24-5, (char *)ctime(&ll.ll_time));
523 if (*ll.ll_host != '\0')
524 (void)printf("from %.*s\n",
525 sizeof(ll.ll_host), ll.ll_host);
526 else
527 (void)printf("on %.*s\n",
528 sizeof(ll.ll_line), ll.ll_line);
529 }
530 (void)lseek(fd, (off_t)pwd->pw_uid * sizeof(ll), L_SET);
531 }
532 bzero((void *)&ll, sizeof(ll));
533 (void)time(&ll.ll_time);
534 strncpy(ll.ll_line, tty, sizeof(ll.ll_line));
535 if (hostname)
536 strncpy(ll.ll_host, hostname, sizeof(ll.ll_host));
537 (void)write(fd, (char *)&ll, sizeof(ll));
538 (void)close(fd);
539 }
540 }
541
badlogin(name)542 badlogin(name)
543 char *name;
544 {
545 if (failures == 0)
546 return;
547 if (hostname) {
548 syslog(LOG_NOTICE, "%d LOGIN FAILURE%s FROM %s",
549 failures, failures > 1 ? "S" : "", hostname);
550 syslog(LOG_AUTHPRIV|LOG_NOTICE,
551 "%d LOGIN FAILURE%s FROM %s, %s",
552 failures, failures > 1 ? "S" : "", hostname, name);
553 } else {
554 syslog(LOG_NOTICE, "%d LOGIN FAILURE%s ON %s",
555 failures, failures > 1 ? "S" : "", tty);
556 syslog(LOG_AUTHPRIV|LOG_NOTICE,
557 "%d LOGIN FAILURE%s ON %s, %s",
558 failures, failures > 1 ? "S" : "", tty, name);
559 }
560 }
561
562 #undef UNKNOWN
563 #define UNKNOWN "su"
564
565 char *
stypeof(ttyid)566 stypeof(ttyid)
567 char *ttyid;
568 {
569 struct ttyent *t;
570
571 return(ttyid && (t = getttynam(ttyid)) ? t->ty_type : UNKNOWN);
572 }
573
sleepexit(eval)574 sleepexit(eval)
575 int eval;
576 {
577 sleep((u_int)5);
578 exit(eval);
579 }
580