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