Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.git] / usr.bin / login / login.c
1 /*-
2  * Copyright (c) 1980, 1987, 1988, 1991, 1993, 1994
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  * @(#)login.c  8.4 (Berkeley) 4/2/94
34  * $FreeBSD: src/usr.bin/login/login.c,v 1.51.2.15 2003/04/29 14:10:41 des Exp $
35  * $DragonFly: src/usr.bin/login/login.c,v 1.2 2003/06/17 04:29:28 dillon Exp $
36  */
37
38 #if 0
39 static char copyright[] =
40 "@(#) Copyright (c) 1980, 1987, 1988, 1991, 1993, 1994\n\
41         The Regents of the University of California.  All rights reserved.\n";
42 #endif
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/copyright.h>
51 #include <sys/param.h>
52 #include <sys/stat.h>
53 #include <sys/socket.h>
54 #include <sys/time.h>
55 #include <sys/resource.h>
56 #include <sys/file.h>
57 #include <netinet/in.h>
58 #include <arpa/inet.h>
59
60 #include <err.h>
61 #include <errno.h>
62 #include <grp.h>
63 #include <libutil.h>
64 #include <login_cap.h>
65 #include <netdb.h>
66 #include <pwd.h>
67 #include <setjmp.h>
68 #include <signal.h>
69 #include <stdio.h>
70 #include <stdlib.h>
71 #include <string.h>
72 #include <syslog.h>
73 #include <ttyent.h>
74 #include <unistd.h>
75 #include <utmp.h>
76
77 #ifdef USE_PAM
78 #include <security/pam_appl.h>
79 #include <security/pam_misc.h>
80 #include <sys/wait.h>
81 #endif /* USE_PAM */
82
83 #include "pathnames.h"
84
85 /* wrapper for KAME-special getnameinfo() */
86 #ifndef NI_WITHSCOPEID
87 #define NI_WITHSCOPEID  0
88 #endif
89
90 void     badlogin __P((char *));
91 void     checknologin __P((void));
92 void     dolastlog __P((int));
93 void     getloginname __P((void));
94 void     motd __P((char *));
95 int      rootterm __P((char *));
96 void     sigint __P((int));
97 void     sleepexit __P((int));
98 void     refused __P((char *,char *,int));
99 char    *stypeof __P((char *));
100 void     timedout __P((int));
101 int      login_access __P((char *, char *));
102 void     login_fbtab __P((char *, uid_t, gid_t));
103
104 #ifdef USE_PAM
105 static int auth_pam __P((void));
106 static int export_pam_environment __P((void));
107 static int ok_to_export __P((const char *));
108
109 static pam_handle_t *pamh = NULL;
110 static char **environ_pam;
111
112 #define PAM_END { \
113         if ((e = pam_setcred(pamh, PAM_DELETE_CRED)) != PAM_SUCCESS) \
114                 syslog(LOG_ERR, "pam_setcred: %s", pam_strerror(pamh, e)); \
115         if ((e = pam_close_session(pamh,0)) != PAM_SUCCESS) \
116                 syslog(LOG_ERR, "pam_close_session: %s", pam_strerror(pamh, e)); \
117         if ((e = pam_end(pamh, e)) != PAM_SUCCESS) \
118                 syslog(LOG_ERR, "pam_end: %s", pam_strerror(pamh, e)); \
119 }
120 #endif /* USE_PAM */
121 static int auth_traditional __P((void));
122 extern void login __P((struct utmp *));
123 static void usage __P((void));
124
125 #define TTYGRPNAME      "tty"           /* name of group to own ttys */
126 #define DEFAULT_BACKOFF 3
127 #define DEFAULT_RETRIES 10
128 #define DEFAULT_PROMPT          "login: "
129 #define DEFAULT_PASSWD_PROMPT   "Password:"
130
131 /*
132  * This bounds the time given to login.  Not a define so it can
133  * be patched on machines where it's too small.
134  */
135 u_int   timeout = 300;
136
137 /* Buffer for signal handling of timeout */
138 jmp_buf timeout_buf;
139
140 struct  passwd *pwd;
141 int     failures;
142 char    *term, *envinit[1], *hostname, *passwd_prompt, *prompt, *tty, *username;
143 char    full_hostname[MAXHOSTNAMELEN];
144
145 int
146 main(argc, argv)
147         int argc;
148         char *argv[];
149 {
150         extern char **environ;
151         struct group *gr;
152         struct stat st;
153         struct timeval tp;
154         struct utmp utmp;
155         int rootok, retries, backoff;
156         int ask, ch, cnt, fflag, hflag, pflag, quietlog, rootlogin, rval;
157         int changepass;
158         time_t warntime;
159         uid_t uid, euid;
160         gid_t egid;
161         char *p, *ttyn;
162         char tbuf[MAXPATHLEN + 2];
163         char tname[sizeof(_PATH_TTY) + 10];
164         char *shell = NULL;
165         login_cap_t *lc = NULL;
166 #ifdef USE_PAM
167         pid_t pid;
168         int e;
169 #endif /* USE_PAM */
170
171         (void)signal(SIGQUIT, SIG_IGN);
172         (void)signal(SIGINT, SIG_IGN);
173         (void)signal(SIGHUP, SIG_IGN);
174         if (setjmp(timeout_buf)) {
175                 if (failures)
176                         badlogin(tbuf);
177                 (void)fprintf(stderr, "Login timed out after %d seconds\n",
178                     timeout);
179                 exit(0);
180         }
181         (void)signal(SIGALRM, timedout);
182         (void)alarm(timeout);
183         (void)setpriority(PRIO_PROCESS, 0, 0);
184
185         openlog("login", LOG_ODELAY, LOG_AUTH);
186
187         /*
188          * -p is used by getty to tell login not to destroy the environment
189          * -f is used to skip a second login authentication
190          * -h is used by other servers to pass the name of the remote
191          *    host to login so that it may be placed in utmp and wtmp
192          */
193         *full_hostname = '\0';
194         term = NULL;
195
196         fflag = hflag = pflag = 0;
197         uid = getuid();
198         euid = geteuid();
199         egid = getegid();
200         while ((ch = getopt(argc, argv, "fh:p")) != -1)
201                 switch (ch) {
202                 case 'f':
203                         fflag = 1;
204                         break;
205                 case 'h':
206                         if (uid)
207                                 errx(1, "-h option: %s", strerror(EPERM));
208                         hflag = 1;
209                         if (strlcpy(full_hostname, optarg,
210                             sizeof(full_hostname)) >= sizeof(full_hostname))
211                                 errx(1, "-h option: %s: exceeds maximum "
212                                     "hostname size", optarg);
213
214                         trimdomain(optarg, UT_HOSTSIZE);
215
216                         if (strlen(optarg) > UT_HOSTSIZE) {
217                                 struct addrinfo hints, *res;
218                                 int ga_err;
219                                 
220                                 memset(&hints, 0, sizeof(hints));
221                                 hints.ai_family = AF_UNSPEC;
222                                 ga_err = getaddrinfo(optarg, NULL, &hints,
223                                     &res);
224                                 if (ga_err == 0) {
225                                         char hostbuf[MAXHOSTNAMELEN];
226
227                                         getnameinfo(res->ai_addr,
228                                             res->ai_addrlen,
229                                             hostbuf,
230                                             sizeof(hostbuf), NULL, 0,
231                                             NI_NUMERICHOST|
232                                             NI_WITHSCOPEID);
233                                         optarg = strdup(hostbuf);
234                                         if (optarg == NULL) {
235                                                 syslog(LOG_NOTICE,
236                                                     "strdup(): %m");
237                                                 sleepexit(1);
238                                         }
239                                 } else
240                                         optarg = "invalid hostname";
241                                 if (res != NULL)
242                                         freeaddrinfo(res);
243                         }
244                         hostname = optarg;
245                         break;
246                 case 'p':
247                         pflag = 1;
248                         break;
249                 case '?':
250                 default:
251                         if (!uid)
252                                 syslog(LOG_ERR, "invalid flag %c", ch);
253                         usage();
254                 }
255         argc -= optind;
256         argv += optind;
257
258         if (*argv) {
259                 username = *argv;
260                 ask = 0;
261         } else
262                 ask = 1;
263
264         setproctitle("-%s", getprogname());
265
266         for (cnt = getdtablesize(); cnt > 2; cnt--)
267                 (void)close(cnt);
268
269         ttyn = ttyname(STDIN_FILENO);
270         if (ttyn == NULL || *ttyn == '\0') {
271                 (void)snprintf(tname, sizeof(tname), "%s??", _PATH_TTY);
272                 ttyn = tname;
273         }
274         if ((tty = strrchr(ttyn, '/')) != NULL)
275                 ++tty;
276         else
277                 tty = ttyn;
278
279         /*
280          * Get "login-retries" & "login-backoff" from default class
281          */
282         lc = login_getclass(NULL);
283         prompt = login_getcapstr(lc, "login_prompt",
284             DEFAULT_PROMPT, DEFAULT_PROMPT);
285         passwd_prompt = login_getcapstr(lc, "passwd_prompt",
286             DEFAULT_PASSWD_PROMPT, DEFAULT_PASSWD_PROMPT);
287         retries = login_getcapnum(lc, "login-retries", DEFAULT_RETRIES,
288             DEFAULT_RETRIES);
289         backoff = login_getcapnum(lc, "login-backoff", DEFAULT_BACKOFF,
290             DEFAULT_BACKOFF);
291         login_close(lc);
292         lc = NULL;
293
294         for (cnt = 0;; ask = 1) {
295                 if (ask) {
296                         fflag = 0;
297                         getloginname();
298                 }
299                 rootlogin = 0;
300                 rootok = rootterm(tty); /* Default (auth may change) */
301
302                 if (strlen(username) > UT_NAMESIZE)
303                         username[UT_NAMESIZE] = '\0';
304
305                 /*
306                  * Note if trying multiple user names; log failures for
307                  * previous user name, but don't bother logging one failure
308                  * for nonexistent name (mistyped username).
309                  */
310                 if (failures && strcmp(tbuf, username)) {
311                         if (failures > (pwd ? 0 : 1))
312                                 badlogin(tbuf);
313                 }
314                 (void)strlcpy(tbuf, username, sizeof(tbuf));
315
316                 pwd = getpwnam(username);
317
318                 /*
319                  * if we have a valid account name, and it doesn't have a
320                  * password, or the -f option was specified and the caller
321                  * is root or the caller isn't changing their uid, don't
322                  * authenticate.
323                  */
324                 if (pwd != NULL) {
325                         if (pwd->pw_uid == 0)
326                                 rootlogin = 1;
327
328                         if (fflag && (uid == (uid_t)0 ||
329                             uid == (uid_t)pwd->pw_uid)) {
330                                 /* already authenticated */
331                                 break;
332                         } else if (pwd->pw_passwd[0] == '\0') {
333                                 if (!rootlogin || rootok) {
334                                         /* pretend password okay */
335                                         rval = 0;
336                                         goto ttycheck;
337                                 }
338                         }
339                 }
340
341                 fflag = 0;
342
343                 (void)setpriority(PRIO_PROCESS, 0, -4);
344
345 #ifdef USE_PAM
346                 /*
347                  * Try to authenticate using PAM.  If a PAM system error
348                  * occurs, perhaps because of a botched configuration,
349                  * then fall back to using traditional Unix authentication.
350                  */
351                 if ((rval = auth_pam()) == -1)
352 #endif /* USE_PAM */
353                         rval = auth_traditional();
354
355                 (void)setpriority(PRIO_PROCESS, 0, 0);
356
357 #ifdef USE_PAM
358                 /*
359                  * PAM authentication may have changed "pwd" to the
360                  * entry for the template user.  Check again to see if
361                  * this is a root login after all.
362                  */
363                 if (pwd != NULL && pwd->pw_uid == 0)
364                         rootlogin = 1;
365 #endif /* USE_PAM */
366
367         ttycheck:
368                 /*
369                  * If trying to log in as root without Kerberos,
370                  * but with insecure terminal, refuse the login attempt.
371                  */
372                 if (pwd && !rval) {
373                         if (rootlogin && !rootok)
374                                 refused(NULL, "NOROOT", 0);
375                         else    /* valid password & authenticated */
376                                 break;
377                 }
378
379                 (void)printf("Login incorrect\n");
380                 failures++;
381
382                 /*
383                  * we allow up to 'retry' (10) tries,
384                  * but after 'backoff' (3) we start backing off
385                  */
386                 if (++cnt > backoff) {
387                         if (cnt >= retries) {
388                                 badlogin(username);
389                                 sleepexit(1);
390                         }
391                         sleep((u_int)((cnt - backoff) * 5));
392                 }
393         }
394
395         /* committed to login -- turn off timeout */
396         (void)alarm((u_int)0);
397         (void)signal(SIGHUP, SIG_DFL);
398
399         endpwent();
400
401         /*
402          * Establish the login class.
403          */
404         lc = login_getpwclass(pwd);
405
406         /* if user not super-user, check for disabled logins */
407         if (!rootlogin)
408                 auth_checknologin(lc);
409
410         quietlog = login_getcapbool(lc, "hushlogin", 0);
411         /* Switching needed for NFS with root access disabled */
412         (void)setegid(pwd->pw_gid);
413         (void)seteuid(rootlogin ? 0 : pwd->pw_uid);
414         if (!*pwd->pw_dir || chdir(pwd->pw_dir) < 0) {
415                 if (login_getcapbool(lc, "requirehome", 0))
416                         refused("Home directory not available", "HOMEDIR", 1);
417                 if (chdir("/") < 0) 
418                         refused("Cannot find root directory", "ROOTDIR", 1);
419                 if (!quietlog || *pwd->pw_dir)
420                         printf("No home directory.\nLogging in with home = \"/\".\n");
421                 pwd->pw_dir = "/";
422         }
423         (void)seteuid(euid);
424         (void)setegid(egid);
425         if (!quietlog)
426                 quietlog = access(_PATH_HUSHLOGIN, F_OK) == 0;
427
428         if (pwd->pw_change || pwd->pw_expire)
429                 (void)gettimeofday(&tp, (struct timezone *)NULL);
430
431 #define DEFAULT_WARN  (2L * 7L * 86400L)  /* Two weeks */
432
433         warntime = login_getcaptime(lc, "warnexpire", DEFAULT_WARN,
434             DEFAULT_WARN);
435
436         if (pwd->pw_expire) {
437                 if (tp.tv_sec >= pwd->pw_expire) {
438                         refused("Sorry -- your account has expired", "EXPIRED",
439                             1);
440                 } else if (pwd->pw_expire - tp.tv_sec < warntime && !quietlog)
441                         (void)printf("Warning: your account expires on %s",
442                             ctime(&pwd->pw_expire));
443         }
444
445         warntime = login_getcaptime(lc, "warnpassword", DEFAULT_WARN,
446             DEFAULT_WARN);
447
448         changepass = 0;
449         if (pwd->pw_change) {
450                 if (tp.tv_sec >= pwd->pw_change) {
451                         (void)printf("Sorry -- your password has expired.\n");
452                         changepass = 1;
453                         syslog(LOG_INFO, "%s Password expired - forcing change",
454                             pwd->pw_name);
455                 } else if (pwd->pw_change - tp.tv_sec < warntime && !quietlog)
456                         (void)printf("Warning: your password expires on %s",
457                             ctime(&pwd->pw_change));
458         }
459
460         if (lc != NULL) {
461                 if (hostname) {
462                         struct addrinfo hints, *res;
463                         int ga_err;
464
465                         memset(&hints, 0, sizeof(hints));
466                         hints.ai_family = AF_UNSPEC;
467                         ga_err = getaddrinfo(full_hostname, NULL, &hints,
468                                              &res);
469                         if (ga_err == 0) {
470                                 char hostbuf[MAXHOSTNAMELEN];
471
472                                 getnameinfo(res->ai_addr, res->ai_addrlen,
473                                     hostbuf, sizeof(hostbuf), NULL, 0,
474                                     NI_NUMERICHOST|NI_WITHSCOPEID);
475                                 if ((optarg = strdup(hostbuf)) == NULL) {
476                                         syslog(LOG_NOTICE, "strdup(): %m");
477                                         sleepexit(1);
478                                 }
479                         } else
480                                 optarg = NULL;
481                         if (res != NULL)
482                                 freeaddrinfo(res);
483                         if (!auth_hostok(lc, full_hostname, optarg))
484                                 refused("Permission denied", "HOST", 1);
485                 }
486
487                 if (!auth_ttyok(lc, tty))
488                         refused("Permission denied", "TTY", 1);
489
490                 if (!auth_timeok(lc, time(NULL)))
491                         refused("Logins not available right now", "TIME", 1);
492         }
493         shell = login_getcapstr(lc, "shell", pwd->pw_shell, pwd->pw_shell);
494         if (*pwd->pw_shell == '\0')
495                 pwd->pw_shell = _PATH_BSHELL;
496         if (*shell == '\0')   /* Not overridden */
497                 shell = pwd->pw_shell;
498         if ((shell = strdup(shell)) == NULL) {
499                 syslog(LOG_NOTICE, "strdup(): %m");
500                 sleepexit(1);
501         }
502
503 #ifdef LOGIN_ACCESS
504         if (login_access(pwd->pw_name, hostname ? full_hostname : tty) == 0)
505                 refused("Permission denied", "ACCESS", 1);
506 #endif /* LOGIN_ACCESS */
507
508         /* Nothing else left to fail -- really log in. */
509         memset((void *)&utmp, 0, sizeof(utmp));
510         (void)time(&utmp.ut_time);
511         (void)strncpy(utmp.ut_name, username, sizeof(utmp.ut_name));
512         if (hostname)
513                 (void)strncpy(utmp.ut_host, hostname, sizeof(utmp.ut_host));
514         (void)strncpy(utmp.ut_line, tty, sizeof(utmp.ut_line));
515         login(&utmp);
516
517         dolastlog(quietlog);
518
519         /*
520          * Set device protections, depending on what terminal the
521          * user is logged in. This feature is used on Suns to give
522          * console users better privacy.
523          */
524         login_fbtab(tty, pwd->pw_uid, pwd->pw_gid);
525
526         /*
527          * Clear flags of the tty.  None should be set, and when the
528          * user sets them otherwise, this can cause the chown to fail.
529          * Since it isn't clear that flags are useful on character
530          * devices, we just clear them.
531          */
532         if (ttyn != tname && chflags(ttyn, 0) && errno != EOPNOTSUPP)
533                 syslog(LOG_ERR, "chflags(%s): %m", ttyn);
534         if (ttyn != tname && chown(ttyn, pwd->pw_uid,
535             (gr = getgrnam(TTYGRPNAME)) ? gr->gr_gid : pwd->pw_gid))
536                 syslog(LOG_ERR, "chmod(%s): %m", ttyn);
537
538
539         /*
540          * Preserve TERM if it happens to be already set.
541          */
542         if ((term = getenv("TERM")) != NULL) {
543                 if ((term = strdup(term)) == NULL) {
544                         syslog(LOG_NOTICE,
545                             "strdup(): %m");
546                         sleepexit(1);
547                 }
548         }
549
550         /*
551          * Exclude cons/vt/ptys only, assume dialup otherwise
552          * TODO: Make dialup tty determination a library call
553          * for consistency (finger etc.)
554          */
555         if (hostname==NULL && isdialuptty(tty))
556                 syslog(LOG_INFO, "DIALUP %s, %s", tty, pwd->pw_name);
557
558 #ifdef LOGALL
559         /*
560          * Syslog each successful login, so we don't have to watch hundreds
561          * of wtmp or lastlogin files.
562          */
563         if (hostname)
564                 syslog(LOG_INFO, "login from %s on %s as %s",
565                        full_hostname, tty, pwd->pw_name);
566         else
567                 syslog(LOG_INFO, "login on %s as %s",
568                        tty, pwd->pw_name);
569 #endif
570
571         /*
572          * If fflag is on, assume caller/authenticator has logged root login.
573          */
574         if (rootlogin && fflag == 0)
575         {
576                 if (hostname)
577                         syslog(LOG_NOTICE, "ROOT LOGIN (%s) ON %s FROM %s",
578                             username, tty, full_hostname);
579                 else
580                         syslog(LOG_NOTICE, "ROOT LOGIN (%s) ON %s",
581                             username, tty);
582         }
583
584         /*
585          * Destroy environment unless user has requested its preservation.
586          * We need to do this before setusercontext() because that may
587          * set or reset some environment variables.
588          */
589         if (!pflag)
590                 environ = envinit;
591
592         /*
593          * PAM modules might add supplementary groups during pam_setcred().
594          */
595         if (setusercontext(lc, pwd, pwd->pw_uid, LOGIN_SETGROUP) != 0) {
596                 syslog(LOG_ERR, "setusercontext() failed - exiting");
597                 exit(1);
598         }
599
600 #ifdef USE_PAM
601         if (pamh) {
602                 if ((e = pam_open_session(pamh, 0)) != PAM_SUCCESS) {
603                         syslog(LOG_ERR, "pam_open_session: %s",
604                             pam_strerror(pamh, e));
605                 } else if ((e = pam_setcred(pamh, PAM_ESTABLISH_CRED))
606                     != PAM_SUCCESS) {
607                         syslog(LOG_ERR, "pam_setcred: %s",
608                             pam_strerror(pamh, e));
609                 }
610
611                 /*
612                  * Add any environmental variables that the
613                  * PAM modules may have set.
614                  * Call *after* opening session!
615                  */
616                 if (pamh) {
617                   environ_pam = pam_getenvlist(pamh);
618                   if (environ_pam)
619                         export_pam_environment();
620                 }
621
622                 /*
623                  * We must fork() before setuid() because we need to call
624                  * pam_close_session() as root.
625                  */
626                 pid = fork();
627                 if (pid < 0) {
628                         err(1, "fork");
629                         PAM_END;
630                         exit(0);
631                 } else if (pid) {
632                         /* parent - wait for child to finish, then cleanup
633                            session */
634                         setproctitle("-%s [pam]", getprogname());
635                         wait(NULL);
636                         PAM_END;
637                         exit(0);
638                 } else {
639                         if ((e = pam_end(pamh, PAM_DATA_SILENT)) != PAM_SUCCESS)
640                                 syslog(LOG_ERR, "pam_end: %s",
641                                     pam_strerror(pamh, e));
642                 }
643         }
644 #endif /* USE_PAM */
645
646         /*
647          * We don't need to be root anymore, so
648          * set the user and session context
649          */
650         if (setlogin(username) != 0) {
651                 syslog(LOG_ERR, "setlogin(%s): %m - exiting", username);
652                 exit(1);
653         }
654         if (setusercontext(lc, pwd, pwd->pw_uid,
655             LOGIN_SETALL & ~(LOGIN_SETLOGIN|LOGIN_SETGROUP)) != 0) {
656                 syslog(LOG_ERR, "setusercontext() failed - exiting");
657                 exit(1);
658         }
659
660         (void)setenv("SHELL", pwd->pw_shell, 1);
661         (void)setenv("HOME", pwd->pw_dir, 1);
662         if (term != NULL && *term != '\0')
663                 (void)setenv("TERM", term, 1);          /* Preset overrides */
664         else {
665                 (void)setenv("TERM", stypeof(tty), 0);  /* Fallback doesn't */
666         }
667         (void)setenv("LOGNAME", username, 1);
668         (void)setenv("USER", username, 1);
669         (void)setenv("PATH", rootlogin ? _PATH_STDPATH : _PATH_DEFPATH, 0);
670
671         if (!quietlog) {
672                 char    *cw;
673
674                 cw = login_getcapstr(lc, "copyright", NULL, NULL);
675                 if (cw != NULL && access(cw, F_OK) == 0)
676                         motd(cw);
677                 else
678                     (void)printf("%s\n\t%s %s\n",
679         "Copyright (c) 1980, 1983, 1986, 1988, 1990, 1991, 1993, 1994",
680         "The Regents of the University of California. ",
681         "All rights reserved.");
682
683                 (void)printf("\n");
684
685                 cw = login_getcapstr(lc, "welcome", NULL, NULL);
686                 if (cw == NULL || access(cw, F_OK) != 0)
687                         cw = _PATH_MOTDFILE;
688                 motd(cw);
689
690                 cw = getenv("MAIL");    /* $MAIL may have been set by class */
691                 if (cw != NULL)
692                         strlcpy(tbuf, cw, sizeof(tbuf));
693                 else
694                         snprintf(tbuf, sizeof(tbuf), "%s/%s", _PATH_MAILDIR,
695                             pwd->pw_name);
696                 if (stat(tbuf, &st) == 0 && st.st_size != 0)
697                         (void)printf("You have %smail.\n",
698                             (st.st_mtime > st.st_atime) ? "new " : "");
699         }
700
701         login_close(lc);
702
703         (void)signal(SIGALRM, SIG_DFL);
704         (void)signal(SIGQUIT, SIG_DFL);
705         (void)signal(SIGINT, SIG_DFL);
706         (void)signal(SIGTSTP, SIG_IGN);
707
708         if (changepass) {
709                 if (system(_PATH_CHPASS) != 0)
710                         sleepexit(1);
711         }
712
713         /*
714          * Login shells have a leading '-' in front of argv[0]
715          */
716         if (snprintf(tbuf, sizeof(tbuf), "-%s",
717             (p = strrchr(pwd->pw_shell, '/')) ? p + 1 : pwd->pw_shell) >=
718             sizeof(tbuf)) {
719                 syslog(LOG_ERR, "user: %s: shell exceeds maximum pathname size",
720                     username);
721                 errx(1, "shell exceeds maximum pathname size");
722         }
723
724         execlp(shell, tbuf, (char *)0);
725         err(1, "%s", shell);
726 }
727
728 static int
729 auth_traditional()
730 {
731         int rval;
732         char *p;
733         char *ep;
734         char *salt;
735
736         rval = 1;
737         salt = pwd != NULL ? pwd->pw_passwd : "xx";
738
739         p = getpass(passwd_prompt);
740         ep = crypt(p, salt);
741
742         if (pwd) {
743                 if (!p[0] && pwd->pw_passwd[0])
744                         ep = ":";
745                 if (strcmp(ep, pwd->pw_passwd) == 0)
746                         rval = 0;
747         }
748
749         /* clear entered password */
750         memset(p, 0, strlen(p));
751         return rval;
752 }
753
754 #ifdef USE_PAM
755 /*
756  * Attempt to authenticate the user using PAM.  Returns 0 if the user is
757  * authenticated, or 1 if not authenticated.  If some sort of PAM system
758  * error occurs (e.g., the "/etc/pam.conf" file is missing) then this
759  * function returns -1.  This can be used as an indication that we should
760  * fall back to a different authentication mechanism.
761  */
762 static int
763 auth_pam()
764 {
765         const char *tmpl_user;
766         const void *item;
767         int rval;
768         int e;
769         static struct pam_conv conv = { misc_conv, NULL };
770
771         if ((e = pam_start("login", username, &conv, &pamh)) != PAM_SUCCESS) {
772                 syslog(LOG_ERR, "pam_start: %s", pam_strerror(pamh, e));
773                 return -1;
774         }
775         if ((e = pam_set_item(pamh, PAM_TTY, tty)) != PAM_SUCCESS) {
776                 syslog(LOG_ERR, "pam_set_item(PAM_TTY): %s",
777                     pam_strerror(pamh, e));
778                 return -1;
779         }
780         if (hostname != NULL &&
781             (e = pam_set_item(pamh, PAM_RHOST, full_hostname)) != PAM_SUCCESS) {
782                 syslog(LOG_ERR, "pam_set_item(PAM_RHOST): %s",
783                     pam_strerror(pamh, e));
784                 return -1;
785         }
786         e = pam_authenticate(pamh, 0);
787         switch (e) {
788
789         case PAM_SUCCESS:
790                 /*
791                  * With PAM we support the concept of a "template"
792                  * user.  The user enters a login name which is
793                  * authenticated by PAM, usually via a remote service
794                  * such as RADIUS or TACACS+.  If authentication
795                  * succeeds, a different but related "template" name
796                  * is used for setting the credentials, shell, and
797                  * home directory.  The name the user enters need only
798                  * exist on the remote authentication server, but the
799                  * template name must be present in the local password
800                  * database.
801                  *
802                  * This is supported by two various mechanisms in the
803                  * individual modules.  However, from the application's
804                  * point of view, the template user is always passed
805                  * back as a changed value of the PAM_USER item.
806                  */
807                 if ((e = pam_get_item(pamh, PAM_USER, &item)) ==
808                     PAM_SUCCESS) {
809                         tmpl_user = (const char *) item;
810                         if (strcmp(username, tmpl_user) != 0)
811                                 pwd = getpwnam(tmpl_user);
812                 } else
813                         syslog(LOG_ERR, "Couldn't get PAM_USER: %s",
814                             pam_strerror(pamh, e));
815                 rval = 0;
816                 break;
817
818         case PAM_AUTH_ERR:
819         case PAM_USER_UNKNOWN:
820         case PAM_MAXTRIES:
821                 rval = 1;
822                 break;
823
824         default:
825                 syslog(LOG_ERR, "pam_authenticate: %s", pam_strerror(pamh, e));
826                 rval = -1;
827                 break;
828         }
829
830         if (rval == 0) {
831                 e = pam_acct_mgmt(pamh, 0);
832                 if (e == PAM_NEW_AUTHTOK_REQD) {
833                         e = pam_chauthtok(pamh, PAM_CHANGE_EXPIRED_AUTHTOK);
834                         if (e != PAM_SUCCESS) {
835                                 syslog(LOG_ERR, "pam_chauthtok: %s",
836                                     pam_strerror(pamh, e));
837                                 rval = 1;
838                         }
839                 } else if (e != PAM_SUCCESS) {
840                         rval = 1;
841                 }
842         }
843
844         if (rval != 0) {
845                 if ((e = pam_end(pamh, e)) != PAM_SUCCESS) {
846                         syslog(LOG_ERR, "pam_end: %s", pam_strerror(pamh, e));
847                 }
848                 pamh = NULL;
849         }
850         return rval;
851 }
852
853 static int
854 export_pam_environment()
855 {
856         char    **pp;
857
858         for (pp = environ_pam; *pp != NULL; pp++) {
859                 if (ok_to_export(*pp))
860                         (void) putenv(*pp);
861                 free(*pp);
862         }
863         return PAM_SUCCESS;
864 }
865
866 /*
867  * Sanity checks on PAM environmental variables:
868  * - Make sure there is an '=' in the string.
869  * - Make sure the string doesn't run on too long.
870  * - Do not export certain variables.  This list was taken from the
871  *   Solaris pam_putenv(3) man page.
872  */
873 static int
874 ok_to_export(s)
875         const char *s;
876 {
877         static const char *noexport[] = {
878                 "SHELL", "HOME", "LOGNAME", "MAIL", "CDPATH",
879                 "IFS", "PATH", NULL
880         };
881         const char **pp;
882         size_t n;
883
884         if (strlen(s) > 1024 || strchr(s, '=') == NULL)
885                 return 0;
886         if (strncmp(s, "LD_", 3) == 0)
887                 return 0;
888         for (pp = noexport; *pp != NULL; pp++) {
889                 n = strlen(*pp);
890                 if (s[n] == '=' && strncmp(s, *pp, n) == 0)
891                         return 0;
892         }
893         return 1;
894 }
895 #endif /* USE_PAM */
896
897 static void
898 usage()
899 {
900
901         (void)fprintf(stderr, "usage: login [-fp] [-h hostname] [username]\n");
902         exit(1);
903 }
904
905 /*
906  * Allow for authentication style and/or kerberos instance
907  */
908
909 #define NBUFSIZ         UT_NAMESIZE + 64
910
911 void
912 getloginname()
913 {
914         int ch;
915         char *p;
916         static char nbuf[NBUFSIZ];
917
918         for (;;) {
919                 (void)printf("%s", prompt);
920                 for (p = nbuf; (ch = getchar()) != '\n'; ) {
921                         if (ch == EOF) {
922                                 badlogin(username);
923                                 exit(0);
924                         }
925                         if (p < nbuf + (NBUFSIZ - 1))
926                                 *p++ = ch;
927                 }
928                 if (p > nbuf) {
929                         if (nbuf[0] == '-')
930                                 (void)fprintf(stderr,
931                                     "login names may not start with '-'.\n");
932                         else {
933                                 *p = '\0';
934                                 username = nbuf;
935                                 break;
936                         }
937                 }
938         }
939 }
940
941 int
942 rootterm(ttyn)
943         char *ttyn;
944 {
945         struct ttyent *t;
946
947         return ((t = getttynam(ttyn)) && t->ty_status & TTY_SECURE);
948 }
949
950 volatile int motdinterrupt;
951
952 void
953 sigint(signo)
954         int signo __unused;
955 {
956         motdinterrupt = 1;
957 }
958
959 void
960 motd(motdfile)
961         char *motdfile;
962 {
963         int fd, nchars;
964         sig_t oldint;
965         char tbuf[256];
966
967         if ((fd = open(motdfile, O_RDONLY, 0)) < 0)
968                 return;
969         motdinterrupt = 0;
970         oldint = signal(SIGINT, sigint);
971         while ((nchars = read(fd, tbuf, sizeof(tbuf))) > 0 && !motdinterrupt)
972                 (void)write(fileno(stdout), tbuf, nchars);
973         (void)signal(SIGINT, oldint);
974         (void)close(fd);
975 }
976
977 /* ARGSUSED */
978 void
979 timedout(signo)
980         int signo;
981 {
982
983         longjmp(timeout_buf, signo);
984 }
985
986
987 void
988 dolastlog(quiet)
989         int quiet;
990 {
991         struct lastlog ll;
992         int fd;
993
994         if ((fd = open(_PATH_LASTLOG, O_RDWR, 0)) >= 0) {
995                 (void)lseek(fd, (off_t)pwd->pw_uid * sizeof(ll), L_SET);
996                 if (!quiet) {
997                         if (read(fd, (char *)&ll, sizeof(ll)) == sizeof(ll) &&
998                             ll.ll_time != 0) {
999                                 (void)printf("Last login: %.*s ",
1000                                     24-5, (char *)ctime(&ll.ll_time));
1001                                 if (*ll.ll_host != '\0')
1002                                         (void)printf("from %.*s\n",
1003                                             (int)sizeof(ll.ll_host),
1004                                             ll.ll_host);
1005                                 else
1006                                         (void)printf("on %.*s\n",
1007                                             (int)sizeof(ll.ll_line),
1008                                             ll.ll_line);
1009                         }
1010                         (void)lseek(fd, (off_t)pwd->pw_uid * sizeof(ll), L_SET);
1011                 }
1012                 memset((void *)&ll, 0, sizeof(ll));
1013                 (void)time(&ll.ll_time);
1014                 (void)strncpy(ll.ll_line, tty, sizeof(ll.ll_line));
1015                 if (hostname)
1016                         (void)strncpy(ll.ll_host, hostname, sizeof(ll.ll_host));
1017                 (void)write(fd, (char *)&ll, sizeof(ll));
1018                 (void)close(fd);
1019         } else {
1020                 syslog(LOG_ERR, "cannot open %s: %m", _PATH_LASTLOG);
1021         }
1022 }
1023
1024 void
1025 badlogin(name)
1026         char *name;
1027 {
1028
1029         if (failures == 0)
1030                 return;
1031         if (hostname) {
1032                 syslog(LOG_NOTICE, "%d LOGIN FAILURE%s FROM %s",
1033                     failures, failures > 1 ? "S" : "", full_hostname);
1034                 syslog(LOG_AUTHPRIV|LOG_NOTICE,
1035                     "%d LOGIN FAILURE%s FROM %s, %s",
1036                     failures, failures > 1 ? "S" : "", full_hostname, name);
1037         } else {
1038                 syslog(LOG_NOTICE, "%d LOGIN FAILURE%s ON %s",
1039                     failures, failures > 1 ? "S" : "", tty);
1040                 syslog(LOG_AUTHPRIV|LOG_NOTICE,
1041                     "%d LOGIN FAILURE%s ON %s, %s",
1042                     failures, failures > 1 ? "S" : "", tty, name);
1043         }
1044         failures = 0;
1045 }
1046
1047 #undef  UNKNOWN
1048 #define UNKNOWN "su"
1049
1050 char *
1051 stypeof(ttyid)
1052         char *ttyid;
1053 {
1054         struct ttyent *t;
1055
1056         if (ttyid != NULL && *ttyid != '\0') {
1057                 t = getttynam(ttyid);
1058                 if (t != NULL && t->ty_type != NULL)
1059                         return (t->ty_type);
1060         }
1061         return (UNKNOWN);
1062 }
1063
1064 void
1065 refused(msg, rtype, lout)
1066         char *msg;
1067         char *rtype;
1068         int lout;
1069 {
1070
1071         if (msg != NULL)
1072             printf("%s.\n", msg);
1073         if (hostname)
1074                 syslog(LOG_NOTICE, "LOGIN %s REFUSED (%s) FROM %s ON TTY %s",
1075                     pwd->pw_name, rtype, full_hostname, tty);
1076         else
1077                 syslog(LOG_NOTICE, "LOGIN %s REFUSED (%s) ON TTY %s",
1078                     pwd->pw_name, rtype, tty);
1079         if (lout)
1080                 sleepexit(1);
1081 }
1082
1083 void
1084 sleepexit(eval)
1085         int eval;
1086 {
1087
1088         (void)sleep(5);
1089         exit(eval);
1090 }