login.c revision 1.87 1 /* $NetBSD: login.c,v 1.87 2006/03/06 22:59:27 jnemeth 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. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 #ifndef lint
34 __COPYRIGHT(
35 "@(#) Copyright (c) 1980, 1987, 1988, 1991, 1993, 1994\n\
36 The Regents of the University of California. All rights reserved.\n");
37 #endif /* not lint */
38
39 #ifndef lint
40 #if 0
41 static char sccsid[] = "@(#)login.c 8.4 (Berkeley) 4/2/94";
42 #endif
43 __RCSID("$NetBSD: login.c,v 1.87 2006/03/06 22:59:27 jnemeth Exp $");
44 #endif /* not lint */
45
46 /*
47 * login [ name ]
48 * login -h hostname (for telnetd, etc.)
49 * login -f name (for pre-authenticated login: datakit, xterm, etc.)
50 */
51
52 #include <sys/param.h>
53 #include <sys/stat.h>
54 #include <sys/time.h>
55 #include <sys/resource.h>
56 #include <sys/file.h>
57 #include <sys/wait.h>
58 #include <sys/socket.h>
59
60 #include <err.h>
61 #include <errno.h>
62 #include <grp.h>
63 #include <pwd.h>
64 #include <setjmp.h>
65 #include <signal.h>
66 #include <stdio.h>
67 #include <stdlib.h>
68 #include <string.h>
69 #include <syslog.h>
70 #include <time.h>
71 #include <ttyent.h>
72 #include <tzfile.h>
73 #include <unistd.h>
74 #ifdef SUPPORT_UTMP
75 #include <utmp.h>
76 #endif
77 #ifdef SUPPORT_UTMPX
78 #include <utmpx.h>
79 #endif
80 #include <util.h>
81 #ifdef SKEY
82 #include <skey.h>
83 #endif
84 #ifdef KERBEROS5
85 #include <krb5/krb5.h>
86 #include <com_err.h>
87 #endif
88 #ifdef LOGIN_CAP
89 #include <login_cap.h>
90 #endif
91 #include <vis.h>
92
93 #include "pathnames.h"
94
95 #ifdef KERBEROS5
96 int login_krb5_get_tickets = 1;
97 int login_krb4_get_tickets = 0;
98 int login_krb5_forwardable_tgt = 0;
99 int login_krb5_retain_ccache = 0;
100 #endif
101
102 void badlogin(char *);
103 void checknologin(char *);
104 #ifdef SUPPORT_UTMP
105 static void doutmp(void);
106 static void dolastlog(int);
107 #endif
108 #ifdef SUPPORT_UTMPX
109 static void doutmpx(void);
110 static void dolastlogx(int);
111 #endif
112 static void update_db(int);
113 void getloginname(void);
114 void motd(char *);
115 int rootterm(char *);
116 void sigint(int);
117 void sleepexit(int);
118 const char *stypeof(const char *);
119 void timedout(int);
120 #if defined(KERBEROS)
121 int klogin(struct passwd *, char *, char *, char *);
122 void kdestroy(void);
123 #endif
124 #ifdef KERBEROS5
125 int k5login(struct passwd *, char *, char *, char *);
126 void k5destroy(void);
127 int k5_read_creds(char*);
128 int k5_write_creds(void);
129 #endif
130 #if defined(KERBEROS) || defined(KERBEROS5)
131 void dofork(void);
132 #endif
133 void decode_ss(const char *);
134 void usage(void);
135
136 #define TTYGRPNAME "tty" /* name of group to own ttys */
137
138 #define DEFAULT_BACKOFF 3
139 #define DEFAULT_RETRIES 10
140
141 /*
142 * This bounds the time given to login. Not a define so it can
143 * be patched on machines where it's too small.
144 */
145 u_int timeout = 300;
146
147 #if defined(KERBEROS) || defined(KERBEROS5)
148 int notickets = 1;
149 char *instance;
150 int has_ccache = 0;
151 #endif
152 #ifdef KERBEROS
153 extern char *krbtkfile_env;
154 extern int krb_configured;
155 #endif
156 #ifdef KERBEROS5
157 extern krb5_context kcontext;
158 extern int have_forward;
159 extern char *krb5tkfile_env;
160 extern int krb5_configured;
161 #endif
162
163 #if defined(KERBEROS) && defined(KERBEROS5)
164 #define KERBEROS_CONFIGURED (krb_configured || krb5_configured)
165 #elif defined(KERBEROS)
166 #define KERBEROS_CONFIGURED krb_configured
167 #elif defined(KERBEROS5)
168 #define KERBEROS_CONFIGURED krb5_configured
169 #endif
170
171 struct passwd *pwd;
172 int failures, have_ss;
173 char term[64], *envinit[1], *hostname, *username, *tty, *nested;
174 struct timeval now;
175 struct sockaddr_storage ss;
176
177 extern const char copyrightstr[];
178
179 int
180 main(int argc, char *argv[])
181 {
182 extern char **environ;
183 struct group *gr;
184 struct stat st;
185 int ask, ch, cnt, fflag, hflag, pflag, sflag, quietlog, rootlogin, rval;
186 int Fflag;
187 uid_t uid, saved_uid;
188 gid_t saved_gid, saved_gids[NGROUPS_MAX];
189 int nsaved_gids;
190 char *domain, *p, *ttyn, *pwprompt;
191 const char *salt;
192 char tbuf[MAXPATHLEN + 2], tname[sizeof(_PATH_TTY) + 10];
193 char localhost[MAXHOSTNAMELEN + 1];
194 int need_chpass, require_chpass;
195 int login_retries = DEFAULT_RETRIES,
196 login_backoff = DEFAULT_BACKOFF;
197 time_t pw_warntime = _PASSWORD_WARNDAYS * SECSPERDAY;
198 #ifdef KERBEROS5
199 krb5_error_code kerror;
200 #endif
201 #if defined(KERBEROS) || defined(KERBEROS5)
202 int got_tickets = 0;
203 #endif
204 #ifdef LOGIN_CAP
205 char *shell = NULL;
206 login_cap_t *lc = NULL;
207 #endif
208
209 tbuf[0] = '\0';
210 rval = 0;
211 pwprompt = NULL;
212 nested = NULL;
213 need_chpass = require_chpass = 0;
214
215 (void)signal(SIGALRM, timedout);
216 (void)alarm(timeout);
217 (void)signal(SIGQUIT, SIG_IGN);
218 (void)signal(SIGINT, SIG_IGN);
219 (void)setpriority(PRIO_PROCESS, 0, 0);
220
221 openlog("login", 0, LOG_AUTH);
222
223 /*
224 * -p is used by getty to tell login not to destroy the environment
225 * -f is used to skip a second login authentication
226 * -h is used by other servers to pass the name of the remote host to
227 * login so that it may be placed in utmp/utmpx and wtmp/wtmpx
228 * -a in addition to -h, a server may supply -a to pass the actual
229 * server address.
230 * -s is used to force use of S/Key or equivalent.
231 */
232 domain = NULL;
233 if (gethostname(localhost, sizeof(localhost)) < 0)
234 syslog(LOG_ERR, "couldn't get local hostname: %m");
235 else
236 domain = strchr(localhost, '.');
237 localhost[sizeof(localhost) - 1] = '\0';
238
239 Fflag = fflag = hflag = pflag = sflag = 0;
240 have_ss = 0;
241 #ifdef KERBEROS5
242 have_forward = 0;
243 #endif
244 uid = getuid();
245 while ((ch = getopt(argc, argv, "a:Ffh:ps")) != -1)
246 switch (ch) {
247 case 'a':
248 if (uid)
249 errx(1, "-a option: %s", strerror(EPERM));
250 decode_ss(optarg);
251 #ifdef notdef
252 (void)sockaddr_snprintf(optarg,
253 sizeof(struct sockaddr_storage), "%a", (void *)&ss);
254 #endif
255 break;
256 case 'F':
257 Fflag = 1;
258 /* FALLTHROUGH */
259 case 'f':
260 fflag = 1;
261 break;
262 case 'h':
263 if (uid)
264 errx(1, "-h option: %s", strerror(EPERM));
265 hflag = 1;
266 #ifdef notdef
267 if (domain && (p = strchr(optarg, '.')) != NULL &&
268 strcasecmp(p, domain) == 0)
269 *p = '\0';
270 #endif
271 hostname = optarg;
272 break;
273 case 'p':
274 pflag = 1;
275 break;
276 case 's':
277 sflag = 1;
278 break;
279 default:
280 case '?':
281 usage();
282 break;
283 }
284
285 setproctitle(NULL);
286 argc -= optind;
287 argv += optind;
288
289 if (*argv) {
290 username = *argv;
291 ask = 0;
292 } else
293 ask = 1;
294
295 #ifdef F_CLOSEM
296 (void)fcntl(3, F_CLOSEM, 0);
297 #else
298 for (cnt = getdtablesize(); cnt > 2; cnt--)
299 (void)close(cnt);
300 #endif
301
302 ttyn = ttyname(STDIN_FILENO);
303 if (ttyn == NULL || *ttyn == '\0') {
304 (void)snprintf(tname, sizeof(tname), "%s??", _PATH_TTY);
305 ttyn = tname;
306 }
307 if ((tty = strstr(ttyn, "/pts/")) != NULL)
308 ++tty;
309 else if ((tty = strrchr(ttyn, '/')) != NULL)
310 ++tty;
311 else
312 tty = ttyn;
313
314 if (issetugid()) {
315 nested = strdup(user_from_uid(getuid(), 0));
316 if (nested == NULL) {
317 syslog(LOG_ERR, "strdup: %m");
318 sleepexit(1);
319 }
320 }
321
322 #ifdef LOGIN_CAP
323 /* Get "login-retries" and "login-backoff" from default class */
324 if ((lc = login_getclass(NULL)) != NULL) {
325 login_retries = (int)login_getcapnum(lc, "login-retries",
326 DEFAULT_RETRIES, DEFAULT_RETRIES);
327 login_backoff = (int)login_getcapnum(lc, "login-backoff",
328 DEFAULT_BACKOFF, DEFAULT_BACKOFF);
329 login_close(lc);
330 lc = NULL;
331 }
332 #endif
333
334 #ifdef KERBEROS5
335 kerror = krb5_init_context(&kcontext);
336 if (kerror) {
337 /*
338 * If Kerberos is not configured, that is, we are
339 * not using Kerberos, do not log the error message.
340 * However, if Kerberos is configured, and the
341 * context init fails for some other reason, we need
342 * to issue a no tickets warning to the user when the
343 * login succeeds.
344 */
345 if (kerror != ENXIO) { /* XXX NetBSD-local Heimdal hack */
346 syslog(LOG_NOTICE,
347 "%s when initializing Kerberos context",
348 error_message(kerror));
349 krb5_configured = 1;
350 }
351 login_krb5_get_tickets = 0;
352 }
353 #endif /* KERBEROS5 */
354
355 for (cnt = 0;; ask = 1) {
356 #if defined(KERBEROS)
357 kdestroy();
358 #endif
359 #if defined(KERBEROS5)
360 if (login_krb5_get_tickets)
361 k5destroy();
362 #endif
363 if (ask) {
364 fflag = 0;
365 getloginname();
366 }
367 rootlogin = 0;
368 #ifdef KERBEROS
369 if ((instance = strchr(username, '.')) != NULL)
370 *instance++ = '\0';
371 else
372 instance = "";
373 #endif
374 #ifdef KERBEROS5
375 if ((instance = strchr(username, '/')) != NULL)
376 *instance++ = '\0';
377 else
378 instance = "";
379 #endif
380 if (strlen(username) > MAXLOGNAME)
381 username[MAXLOGNAME] = '\0';
382
383 /*
384 * Note if trying multiple user names; log failures for
385 * previous user name, but don't bother logging one failure
386 * for nonexistent name (mistyped username).
387 */
388 if (failures && strcmp(tbuf, username)) {
389 if (failures > (pwd ? 0 : 1))
390 badlogin(tbuf);
391 failures = 0;
392 }
393 (void)strlcpy(tbuf, username, sizeof(tbuf));
394
395 if ((pwd = getpwnam(username)) != NULL)
396 salt = pwd->pw_passwd;
397 else
398 salt = "xx";
399
400 #ifdef LOGIN_CAP
401 /*
402 * Establish the class now, before we might goto
403 * within the next block. pwd can be NULL since it
404 * falls back to the "default" class if it is.
405 */
406 lc = login_getclass(pwd ? pwd->pw_class : NULL);
407 #endif
408 /*
409 * if we have a valid account name, and it doesn't have a
410 * password, or the -f option was specified and the caller
411 * is root or the caller isn't changing their uid, don't
412 * authenticate.
413 */
414 if (pwd) {
415 if (pwd->pw_uid == 0)
416 rootlogin = 1;
417
418 if (fflag && (uid == 0 || uid == pwd->pw_uid)) {
419 /* already authenticated */
420 #ifdef KERBEROS5
421 if (login_krb5_get_tickets && Fflag)
422 k5_read_creds(username);
423 #endif
424 break;
425 } else if (pwd->pw_passwd[0] == '\0') {
426 /* pretend password okay */
427 rval = 0;
428 goto ttycheck;
429 }
430 }
431
432 fflag = 0;
433
434 (void)setpriority(PRIO_PROCESS, 0, -4);
435
436 #ifdef SKEY
437 if (skey_haskey(username) == 0) {
438 static char skprompt[80];
439 const char *skinfo = skey_keyinfo(username);
440
441 (void)snprintf(skprompt, sizeof(skprompt),
442 "Password [ %s ]:",
443 skinfo ? skinfo : "error getting challenge");
444 pwprompt = skprompt;
445 } else
446 #endif
447 pwprompt = "Password:";
448
449 p = getpass(pwprompt);
450
451 if (pwd == NULL) {
452 rval = 1;
453 goto skip;
454 }
455 #ifdef KERBEROS
456 if (
457 #ifdef KERBEROS5
458 /* allow a user to get both krb4 and krb5 tickets, if
459 * desired. If krb5 is compiled in, the default action
460 * is to ignore krb4 and get krb5 tickets, but the user
461 * can override this in the krb5.conf. */
462 login_krb4_get_tickets &&
463 #endif
464 klogin(pwd, instance, localhost, p) == 0) {
465 rval = 0;
466 got_tickets = 1;
467 }
468 #endif
469 #ifdef KERBEROS5
470 if (login_krb5_get_tickets &&
471 k5login(pwd, instance, localhost, p) == 0) {
472 rval = 0;
473 got_tickets = 1;
474 }
475 #endif
476 #if defined(KERBEROS) || defined(KERBEROS5)
477 if (got_tickets)
478 goto skip;
479 #endif
480 #ifdef SKEY
481 if (skey_haskey(username) == 0 &&
482 skey_passcheck(username, p) != -1) {
483 rval = 0;
484 goto skip;
485 }
486 #endif
487 if (!sflag && *pwd->pw_passwd != '\0' &&
488 !strcmp(crypt(p, pwd->pw_passwd), pwd->pw_passwd)) {
489 rval = 0;
490 require_chpass = 1;
491 goto skip;
492 }
493 rval = 1;
494
495 skip:
496 memset(p, 0, strlen(p));
497
498 (void)setpriority(PRIO_PROCESS, 0, 0);
499
500 ttycheck:
501 /*
502 * If trying to log in as root without Kerberos,
503 * but with insecure terminal, refuse the login attempt.
504 */
505 if (pwd && !rval && rootlogin && !rootterm(tty)) {
506 (void)printf("Login incorrect or refused on this "
507 "terminal.\n");
508 if (hostname)
509 syslog(LOG_NOTICE,
510 "LOGIN %s REFUSED FROM %s ON TTY %s",
511 pwd->pw_name, hostname, tty);
512 else
513 syslog(LOG_NOTICE,
514 "LOGIN %s REFUSED ON TTY %s",
515 pwd->pw_name, tty);
516 continue;
517 }
518
519 if (pwd && !rval)
520 break;
521
522 (void)printf("Login incorrect or refused on this "
523 "terminal.\n");
524 failures++;
525 cnt++;
526 /* we allow 10 tries, but after 3 we start backing off */
527 if (cnt > login_backoff) {
528 if (cnt >= login_retries) {
529 badlogin(username);
530 sleepexit(1);
531 }
532 sleep((u_int)((cnt - 3) * 5));
533 }
534 }
535
536 /* committed to login -- turn off timeout */
537 (void)alarm((u_int)0);
538
539 endpwent();
540
541 /* if user not super-user, check for disabled logins */
542 #ifdef LOGIN_CAP
543 if (!login_getcapbool(lc, "ignorenologin", rootlogin))
544 checknologin(login_getcapstr(lc, "nologin", NULL, NULL));
545 #else
546 if (!rootlogin)
547 checknologin(NULL);
548 #endif
549
550 #ifdef LOGIN_CAP
551 quietlog = login_getcapbool(lc, "hushlogin", 0);
552 #else
553 quietlog = 0;
554 #endif
555 /* Temporarily give up special privileges so we can change */
556 /* into NFS-mounted homes that are exported for non-root */
557 /* access and have mode 7x0 */
558 saved_uid = geteuid();
559 saved_gid = getegid();
560 nsaved_gids = getgroups(NGROUPS_MAX, saved_gids);
561
562 (void)setegid(pwd->pw_gid);
563 initgroups(username, pwd->pw_gid);
564 (void)seteuid(pwd->pw_uid);
565
566 if (chdir(pwd->pw_dir) < 0) {
567 #ifdef LOGIN_CAP
568 if (login_getcapbool(lc, "requirehome", 0)) {
569 (void)printf("Home directory %s required\n",
570 pwd->pw_dir);
571 sleepexit(1);
572 }
573 #endif
574 (void)printf("No home directory %s!\n", pwd->pw_dir);
575 if (chdir("/"))
576 exit(0);
577 pwd->pw_dir = "/";
578 (void)printf("Logging in with home = \"/\".\n");
579 }
580
581 if (!quietlog)
582 quietlog = access(_PATH_HUSHLOGIN, F_OK) == 0;
583
584 /* regain special privileges */
585 (void)seteuid(saved_uid);
586 setgroups(nsaved_gids, saved_gids);
587 (void)setegid(saved_gid);
588
589 #ifdef LOGIN_CAP
590 pw_warntime = login_getcaptime(lc, "password-warn",
591 _PASSWORD_WARNDAYS * SECSPERDAY,
592 _PASSWORD_WARNDAYS * SECSPERDAY);
593 #endif
594
595 (void)gettimeofday(&now, (struct timezone *)NULL);
596 if (pwd->pw_expire) {
597 if (now.tv_sec >= pwd->pw_expire) {
598 (void)printf("Sorry -- your account has expired.\n");
599 sleepexit(1);
600 } else if (pwd->pw_expire - now.tv_sec < pw_warntime &&
601 !quietlog)
602 (void)printf("Warning: your account expires on %s",
603 ctime(&pwd->pw_expire));
604 }
605 if (pwd->pw_change) {
606 if (pwd->pw_change == _PASSWORD_CHGNOW)
607 need_chpass = 1;
608 else if (now.tv_sec >= pwd->pw_change) {
609 (void)printf("Sorry -- your password has expired.\n");
610 sleepexit(1);
611 } else if (pwd->pw_change - now.tv_sec < pw_warntime &&
612 !quietlog)
613 (void)printf("Warning: your password expires on %s",
614 ctime(&pwd->pw_change));
615
616 }
617 /* Nothing else left to fail -- really log in. */
618 update_db(quietlog);
619
620 (void)chown(ttyn, pwd->pw_uid,
621 (gr = getgrnam(TTYGRPNAME)) ? gr->gr_gid : pwd->pw_gid);
622
623 if (ttyaction(ttyn, "login", pwd->pw_name))
624 (void)printf("Warning: ttyaction failed.\n");
625
626 #if defined(KERBEROS) || defined(KERBEROS5)
627 /* Fork so that we can call kdestroy */
628 if (
629 #ifdef KERBEROS5
630 ! login_krb5_retain_ccache &&
631 #endif
632 has_ccache)
633 dofork();
634 #endif
635
636 /* Destroy environment unless user has requested its preservation. */
637 if (!pflag)
638 environ = envinit;
639
640 #ifdef LOGIN_CAP
641 if (nested == NULL && setusercontext(lc, pwd, pwd->pw_uid,
642 LOGIN_SETLOGIN) != 0) {
643 syslog(LOG_ERR, "setusercontext failed");
644 exit(1);
645 }
646 if (setusercontext(lc, pwd, pwd->pw_uid,
647 (LOGIN_SETALL & ~(LOGIN_SETPATH|LOGIN_SETLOGIN))) != 0) {
648 syslog(LOG_ERR, "setusercontext failed");
649 exit(1);
650 }
651 #else
652 (void)setgid(pwd->pw_gid);
653
654 initgroups(username, pwd->pw_gid);
655
656 if (nested == NULL && setlogin(pwd->pw_name) < 0)
657 syslog(LOG_ERR, "setlogin() failure: %m");
658
659 /* Discard permissions last so can't get killed and drop core. */
660 if (rootlogin)
661 (void)setuid(0);
662 else
663 (void)setuid(pwd->pw_uid);
664 #endif
665
666 if (*pwd->pw_shell == '\0')
667 pwd->pw_shell = _PATH_BSHELL;
668 #ifdef LOGIN_CAP
669 if ((shell = login_getcapstr(lc, "shell", NULL, NULL)) != NULL) {
670 if ((shell = strdup(shell)) == NULL) {
671 syslog(LOG_ERR, "Cannot alloc mem");
672 sleepexit(1);
673 }
674 pwd->pw_shell = shell;
675 }
676 #endif
677
678 (void)setenv("HOME", pwd->pw_dir, 1);
679 (void)setenv("SHELL", pwd->pw_shell, 1);
680 if (term[0] == '\0') {
681 char *tt = (char *)stypeof(tty);
682 #ifdef LOGIN_CAP
683 if (tt == NULL)
684 tt = login_getcapstr(lc, "term", NULL, NULL);
685 #endif
686 /* unknown term -> "su" */
687 (void)strlcpy(term, tt != NULL ? tt : "su", sizeof(term));
688 }
689 (void)setenv("TERM", term, 0);
690 (void)setenv("LOGNAME", pwd->pw_name, 1);
691 (void)setenv("USER", pwd->pw_name, 1);
692
693 #ifdef LOGIN_CAP
694 setusercontext(lc, pwd, pwd->pw_uid, LOGIN_SETPATH);
695 #else
696 (void)setenv("PATH", _PATH_DEFPATH, 0);
697 #endif
698
699 #ifdef KERBEROS
700 if (krbtkfile_env)
701 (void)setenv("KRBTKFILE", krbtkfile_env, 1);
702 #endif
703 #ifdef KERBEROS5
704 if (krb5tkfile_env)
705 (void)setenv("KRB5CCNAME", krb5tkfile_env, 1);
706 #endif
707
708 if (tty[sizeof("tty")-1] == 'd')
709 syslog(LOG_INFO, "DIALUP %s, %s", tty, pwd->pw_name);
710
711 /* If fflag is on, assume caller/authenticator has logged root login. */
712 if (rootlogin && fflag == 0) {
713 if (hostname)
714 syslog(LOG_NOTICE, "ROOT LOGIN (%s) ON %s FROM %s",
715 username, tty, hostname);
716 else
717 syslog(LOG_NOTICE, "ROOT LOGIN (%s) ON %s",
718 username, tty);
719 }
720
721 #if defined(KERBEROS) || defined(KERBEROS5)
722 if (KERBEROS_CONFIGURED && !quietlog && notickets == 1)
723 (void)printf("Warning: no Kerberos tickets issued.\n");
724 #endif
725
726 if (!quietlog) {
727 char *fname;
728 #ifdef LOGIN_CAP
729 fname = login_getcapstr(lc, "copyright", NULL, NULL);
730 if (fname != NULL && access(fname, F_OK) == 0)
731 motd(fname);
732 else
733 #endif
734 (void)printf("%s", copyrightstr);
735
736 #ifdef LOGIN_CAP
737 fname = login_getcapstr(lc, "welcome", NULL, NULL);
738 if (fname == NULL || access(fname, F_OK) != 0)
739 #endif
740 fname = _PATH_MOTDFILE;
741 motd(fname);
742
743 (void)snprintf(tbuf,
744 sizeof(tbuf), "%s/%s", _PATH_MAILDIR, pwd->pw_name);
745 if (stat(tbuf, &st) == 0 && st.st_size != 0)
746 (void)printf("You have %smail.\n",
747 (st.st_mtime > st.st_atime) ? "new " : "");
748 }
749
750 #ifdef LOGIN_CAP
751 login_close(lc);
752 #endif
753
754 (void)signal(SIGALRM, SIG_DFL);
755 (void)signal(SIGQUIT, SIG_DFL);
756 (void)signal(SIGINT, SIG_DFL);
757 (void)signal(SIGTSTP, SIG_IGN);
758
759 tbuf[0] = '-';
760 (void)strlcpy(tbuf + 1, (p = strrchr(pwd->pw_shell, '/')) ?
761 p + 1 : pwd->pw_shell, sizeof(tbuf) - 1);
762
763 /* Wait to change password until we're unprivileged */
764 if (need_chpass) {
765 if (!require_chpass)
766 (void)printf(
767 "Warning: your password has expired. Please change it as soon as possible.\n");
768 else {
769 int status;
770
771 (void)printf(
772 "Your password has expired. Please choose a new one.\n");
773 switch (fork()) {
774 case -1:
775 warn("fork");
776 sleepexit(1);
777 case 0:
778 execl(_PATH_BINPASSWD, "passwd", 0);
779 _exit(1);
780 default:
781 if (wait(&status) == -1 ||
782 WEXITSTATUS(status))
783 sleepexit(1);
784 }
785 }
786 }
787
788 #ifdef KERBEROS5
789 if (login_krb5_get_tickets)
790 k5_write_creds();
791 #endif
792 execlp(pwd->pw_shell, tbuf, 0);
793 err(1, "%s", pwd->pw_shell);
794 }
795
796 #if defined(KERBEROS) || defined(KERBEROS5)
797 #define NBUFSIZ (MAXLOGNAME + 1 + 5) /* .root suffix */
798 #else
799 #define NBUFSIZ (MAXLOGNAME + 1)
800 #endif
801
802 #if defined(KERBEROS) || defined(KERBEROS5)
803 /*
804 * This routine handles cleanup stuff, and the like.
805 * It exists only in the child process.
806 */
807 #include <sys/wait.h>
808 void
809 dofork(void)
810 {
811 int child;
812
813 if (!(child = fork()))
814 return; /* Child process */
815
816 /*
817 * Setup stuff? This would be things we could do in parallel
818 * with login
819 */
820 (void)chdir("/"); /* Let's not keep the fs busy... */
821
822 /* If we're the parent, watch the child until it dies */
823 while (wait(0) != child)
824 ;
825
826 /* Cleanup stuff */
827 /* Run kdestroy to destroy tickets */
828 #ifdef KERBEROS
829 kdestroy();
830 #endif
831 #ifdef KERBEROS5
832 if (login_krb5_get_tickets)
833 k5destroy();
834 #endif
835
836 /* Leave */
837 exit(0);
838 }
839 #endif
840
841 void
842 getloginname(void)
843 {
844 int ch;
845 char *p;
846 static char nbuf[NBUFSIZ];
847
848 for (;;) {
849 (void)printf("login: ");
850 for (p = nbuf; (ch = getchar()) != '\n'; ) {
851 if (ch == EOF) {
852 badlogin(username);
853 exit(0);
854 }
855 if (p < nbuf + (NBUFSIZ - 1))
856 *p++ = ch;
857 }
858 if (p > nbuf) {
859 if (nbuf[0] == '-')
860 (void)fprintf(stderr,
861 "login names may not start with '-'.\n");
862 else {
863 *p = '\0';
864 username = nbuf;
865 break;
866 }
867 }
868 }
869 }
870
871 int
872 rootterm(char *ttyn)
873 {
874 struct ttyent *t;
875
876 return ((t = getttynam(ttyn)) && t->ty_status & TTY_SECURE);
877 }
878
879 jmp_buf motdinterrupt;
880
881 void
882 motd(char *fname)
883 {
884 int fd, nchars;
885 sig_t oldint;
886 char tbuf[8192];
887
888 if ((fd = open(fname ? fname : _PATH_MOTDFILE, O_RDONLY, 0)) < 0)
889 return;
890 oldint = signal(SIGINT, sigint);
891 if (setjmp(motdinterrupt) == 0)
892 while ((nchars = read(fd, tbuf, sizeof(tbuf))) > 0)
893 (void)write(fileno(stdout), tbuf, nchars);
894 (void)signal(SIGINT, oldint);
895 (void)close(fd);
896 }
897
898 /* ARGSUSED */
899 void
900 sigint(int signo)
901 {
902
903 longjmp(motdinterrupt, 1);
904 }
905
906 /* ARGSUSED */
907 void
908 timedout(int signo)
909 {
910
911 (void)fprintf(stderr, "Login timed out after %d seconds\n", timeout);
912 exit(0);
913 }
914
915 void
916 checknologin(char *fname)
917 {
918 int fd, nchars;
919 char tbuf[8192];
920
921 if ((fd = open(fname ? fname : _PATH_NOLOGIN, O_RDONLY, 0)) >= 0) {
922 while ((nchars = read(fd, tbuf, sizeof(tbuf))) > 0)
923 (void)write(fileno(stdout), tbuf, nchars);
924 sleepexit(0);
925 }
926 }
927
928 static void
929 update_db(int quietlog)
930 {
931 if (nested != NULL) {
932 if (hostname != NULL)
933 syslog(LOG_NOTICE, "%s to %s on tty %s from %s",
934 nested, pwd->pw_name, tty, hostname);
935 else
936 syslog(LOG_NOTICE, "%s to %s on tty %s", nested,
937 pwd->pw_name, tty);
938
939 return;
940 }
941 if (hostname != NULL && have_ss == 0) {
942 socklen_t len = sizeof(ss);
943 have_ss = getpeername(STDIN_FILENO, (struct sockaddr *)&ss,
944 &len) != -1;
945 }
946 (void)gettimeofday(&now, NULL);
947 #ifdef SUPPORT_UTMPX
948 doutmpx();
949 dolastlogx(quietlog);
950 quietlog = 1;
951 #endif
952 #ifdef SUPPORT_UTMP
953 doutmp();
954 dolastlog(quietlog);
955 #endif
956 }
957
958 #ifdef SUPPORT_UTMPX
959 static void
960 doutmpx(void)
961 {
962 struct utmpx utmpx;
963 char *t;
964
965 memset((void *)&utmpx, 0, sizeof(utmpx));
966 utmpx.ut_tv = now;
967 (void)strncpy(utmpx.ut_name, username, sizeof(utmpx.ut_name));
968 if (hostname) {
969 (void)strncpy(utmpx.ut_host, hostname, sizeof(utmpx.ut_host));
970 utmpx.ut_ss = ss;
971 }
972 (void)strncpy(utmpx.ut_line, tty, sizeof(utmpx.ut_line));
973 utmpx.ut_type = USER_PROCESS;
974 utmpx.ut_pid = getpid();
975 t = tty + strlen(tty);
976 if (t - tty >= sizeof(utmpx.ut_id)) {
977 (void)strncpy(utmpx.ut_id, t - sizeof(utmpx.ut_id),
978 sizeof(utmpx.ut_id));
979 } else {
980 (void)strncpy(utmpx.ut_id, tty, sizeof(utmpx.ut_id));
981 }
982 if (pututxline(&utmpx) == NULL)
983 syslog(LOG_NOTICE, "Cannot update utmpx: %m");
984 endutxent();
985 if (updwtmpx(_PATH_WTMPX, &utmpx) != 0)
986 syslog(LOG_NOTICE, "Cannot update wtmpx: %m");
987 }
988
989 static void
990 dolastlogx(int quiet)
991 {
992 struct lastlogx ll;
993 if (!quiet && getlastlogx(_PATH_LASTLOGX, pwd->pw_uid, &ll) != NULL) {
994 time_t t = (time_t)ll.ll_tv.tv_sec;
995 (void)printf("Last login: %.24s ", ctime(&t));
996 if (*ll.ll_host != '\0')
997 (void)printf("from %.*s ",
998 (int)sizeof(ll.ll_host),
999 ll.ll_host);
1000 (void)printf("on %.*s\n",
1001 (int)sizeof(ll.ll_line),
1002 ll.ll_line);
1003 }
1004 ll.ll_tv = now;
1005 (void)strncpy(ll.ll_line, tty, sizeof(ll.ll_line));
1006 if (hostname)
1007 (void)strncpy(ll.ll_host, hostname, sizeof(ll.ll_host));
1008 else
1009 (void)memset(ll.ll_host, '\0', sizeof(ll.ll_host));
1010 if (have_ss)
1011 ll.ll_ss = ss;
1012 else
1013 (void)memset(&ll.ll_ss, 0, sizeof(ll.ll_ss));
1014 if (updlastlogx(_PATH_LASTLOGX, pwd->pw_uid, &ll) != 0)
1015 syslog(LOG_NOTICE, "Cannot update lastlogx: %m");
1016 }
1017 #endif
1018
1019 #ifdef SUPPORT_UTMP
1020 static void
1021 doutmp(void)
1022 {
1023 struct utmp utmp;
1024
1025 (void)memset((void *)&utmp, 0, sizeof(utmp));
1026 utmp.ut_time = now.tv_sec;
1027 (void)strncpy(utmp.ut_name, username, sizeof(utmp.ut_name));
1028 if (hostname)
1029 (void)strncpy(utmp.ut_host, hostname, sizeof(utmp.ut_host));
1030 (void)strncpy(utmp.ut_line, tty, sizeof(utmp.ut_line));
1031 login(&utmp);
1032 }
1033
1034 static void
1035 dolastlog(int quiet)
1036 {
1037 struct lastlog ll;
1038 int fd;
1039
1040 if ((fd = open(_PATH_LASTLOG, O_RDWR, 0)) >= 0) {
1041 (void)lseek(fd, (off_t)(pwd->pw_uid * sizeof(ll)), SEEK_SET);
1042 if (!quiet) {
1043 if (read(fd, (char *)&ll, sizeof(ll)) == sizeof(ll) &&
1044 ll.ll_time != 0) {
1045 (void)printf("Last login: %.24s ",
1046 ctime(&ll.ll_time));
1047 if (*ll.ll_host != '\0')
1048 (void)printf("from %.*s ",
1049 (int)sizeof(ll.ll_host),
1050 ll.ll_host);
1051 (void)printf("on %.*s\n",
1052 (int)sizeof(ll.ll_line), ll.ll_line);
1053 }
1054 (void)lseek(fd, (off_t)(pwd->pw_uid * sizeof(ll)),
1055 SEEK_SET);
1056 }
1057 memset((void *)&ll, 0, sizeof(ll));
1058 ll.ll_time = now.tv_sec;
1059 (void)strncpy(ll.ll_line, tty, sizeof(ll.ll_line));
1060 if (hostname)
1061 (void)strncpy(ll.ll_host, hostname, sizeof(ll.ll_host));
1062 (void)write(fd, (char *)&ll, sizeof(ll));
1063 (void)close(fd);
1064 }
1065 }
1066 #endif
1067
1068 void
1069 badlogin(char *name)
1070 {
1071
1072 if (failures == 0)
1073 return;
1074 if (hostname) {
1075 syslog(LOG_NOTICE, "%d LOGIN FAILURE%s FROM %s",
1076 failures, failures > 1 ? "S" : "", hostname);
1077 syslog(LOG_AUTHPRIV|LOG_NOTICE,
1078 "%d LOGIN FAILURE%s FROM %s, %s",
1079 failures, failures > 1 ? "S" : "", hostname, name);
1080 } else {
1081 syslog(LOG_NOTICE, "%d LOGIN FAILURE%s ON %s",
1082 failures, failures > 1 ? "S" : "", tty);
1083 syslog(LOG_AUTHPRIV|LOG_NOTICE,
1084 "%d LOGIN FAILURE%s ON %s, %s",
1085 failures, failures > 1 ? "S" : "", tty, name);
1086 }
1087 }
1088
1089 const char *
1090 stypeof(const char *ttyid)
1091 {
1092 struct ttyent *t;
1093
1094 return (ttyid && (t = getttynam(ttyid)) ? t->ty_type : NULL);
1095 }
1096
1097 void
1098 sleepexit(int eval)
1099 {
1100
1101 (void)sleep(5);
1102 exit(eval);
1103 }
1104
1105 void
1106 decode_ss(const char *arg)
1107 {
1108 struct sockaddr_storage *ssp;
1109 size_t len = strlen(arg);
1110
1111 if (len > sizeof(*ssp) * 4 + 1 || len < sizeof(*ssp))
1112 errx(1, "Bad argument");
1113
1114 if ((ssp = malloc(len)) == NULL)
1115 err(1, NULL);
1116
1117 if (strunvis((char *)ssp, arg) != sizeof(*ssp))
1118 errx(1, "Decoding error");
1119
1120 (void)memcpy(&ss, ssp, sizeof(ss));
1121 have_ss = 1;
1122 }
1123
1124 void
1125 usage(void)
1126 {
1127 (void)fprintf(stderr,
1128 "Usage: %s [-Ffps] [-a address] [-h hostname] [username]\n",
1129 getprogname());
1130 exit(1);
1131 }
1132