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