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