Deleted Added
sdiff udiff text old ( 155312 ) new ( 157215 )
full compact
1/*-
2 * Copyright (c) 1980, 1987, 1988, 1991, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 2002 Networks Associates Technologies, Inc.
5 * All rights reserved.
6 *
7 * Portions of this software were developed for the FreeBSD Project by
8 * ThinkSec AS and NAI Labs, the Security Research Division of Network
9 * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035
10 * ("CBOSS"), as part of the DARPA CHATS research program.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the University of
23 * California, Berkeley and its contributors.
24 * 4. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * SUCH DAMAGE.
39 */
40
41#if 0
42#ifndef lint
43static char sccsid[] = "@(#)login.c 8.4 (Berkeley) 4/2/94";
44#endif
45#endif
46
47#include <sys/cdefs.h>
48__FBSDID("$FreeBSD: head/usr.bin/login/login.c 155312 2006-02-04 20:20:02Z wsalamon $");
49
50/*
51 * login [ name ]
52 * login -h hostname (for telnetd, etc.)
53 * login -f name (for pre-authenticated login: datakit, xterm, etc.)
54 */
55
56#include <sys/copyright.h>
57#include <sys/param.h>
58#include <sys/file.h>
59#include <sys/stat.h>
60#include <sys/time.h>
61#include <sys/resource.h>
62#include <sys/wait.h>
63
64#include <err.h>
65#include <errno.h>
66#include <grp.h>
67#include <libutil.h>
68#include <login_cap.h>
69#include <pwd.h>
70#include <setjmp.h>
71#include <signal.h>
72#include <stdio.h>
73#include <stdlib.h>
74#include <string.h>
75#include <syslog.h>
76#include <ttyent.h>
77#include <unistd.h>
78
79#include <security/pam_appl.h>
80#include <security/openpam.h>
81
82#include "login.h"
83#include "pathnames.h"
84
85static int auth_pam(void);
86static void bail(int, int);
87static int export(const char *);
88static void export_pam_environment(void);
89static int motd(const char *);
90static void badlogin(char *);
91static char *getloginname(void);
92static void pam_syslog(const char *);
93static void pam_cleanup(void);
94static void refused(const char *, const char *, int);
95static const char *stypeof(char *);
96static void sigint(int);
97static void timedout(int);
98static void usage(void);
99
100#define TTYGRPNAME "tty" /* group to own ttys */
101#define DEFAULT_BACKOFF 3
102#define DEFAULT_RETRIES 10
103#define DEFAULT_PROMPT "login: "
104#define DEFAULT_PASSWD_PROMPT "Password:"
105#define TERM_UNKNOWN "su"
106#define DEFAULT_WARN (2L * 7L * 86400L) /* Two weeks */
107#define NO_SLEEP_EXIT 0
108#define SLEEP_EXIT 5
109
110/*
111 * This bounds the time given to login. Not a define so it can
112 * be patched on machines where it's too small.
113 */
114static u_int timeout = 300;
115
116/* Buffer for signal handling of timeout */
117static jmp_buf timeout_buf;
118
119struct passwd *pwd;
120static int failures;
121
122static char *envinit[1]; /* empty environment list */
123
124/*
125 * Command line flags and arguments
126 */
127static int fflag; /* -f: do not perform authentication */
128static int hflag; /* -h: login from remote host */
129static char *hostname; /* hostname from command line */
130static int pflag; /* -p: preserve environment */
131
132/*
133 * User name
134 */
135static char *username; /* user name */
136static char *olduser; /* previous user name */
137
138/*
139 * Prompts
140 */
141static char default_prompt[] = DEFAULT_PROMPT;
142static const char *prompt;
143static char default_passwd_prompt[] = DEFAULT_PASSWD_PROMPT;
144static const char *passwd_prompt;
145
146static char *tty;
147
148/*
149 * PAM data
150 */
151static pam_handle_t *pamh = NULL;
152static struct pam_conv pamc = { openpam_ttyconv, NULL };
153static int pam_err;
154static int pam_silent = PAM_SILENT;
155static int pam_cred_established;
156static int pam_session_established;
157
158int
159main(int argc, char *argv[])
160{
161 struct group *gr;
162 struct stat st;
163 int retries, backoff;
164 int ask, ch, cnt, quietlog, rootlogin, rval;
165 uid_t uid, euid;
166 gid_t egid;
167 char *term;
168 char *p, *ttyn;
169 char tname[sizeof(_PATH_TTY) + 10];
170 char *arg0;
171 const char *tp;
172 const char *shell = NULL;
173 login_cap_t *lc = NULL;
174 login_cap_t *lc_user = NULL;
175 pid_t pid;
176 char auditsuccess = 1;
177
178 (void)signal(SIGQUIT, SIG_IGN);
179 (void)signal(SIGINT, SIG_IGN);
180 (void)signal(SIGHUP, SIG_IGN);
181 if (setjmp(timeout_buf)) {
182 if (failures)
183 badlogin(username);
184 (void)fprintf(stderr, "Login timed out after %d seconds\n",
185 timeout);
186 bail(NO_SLEEP_EXIT, 0);
187 }
188 (void)signal(SIGALRM, timedout);
189 (void)alarm(timeout);
190 (void)setpriority(PRIO_PROCESS, 0, 0);
191
192 openlog("login", LOG_ODELAY, LOG_AUTH);
193
194 uid = getuid();
195 euid = geteuid();
196 egid = getegid();
197
198 while ((ch = getopt(argc, argv, "fh:p")) != -1)
199 switch (ch) {
200 case 'f':
201 fflag = 1;
202 break;
203 case 'h':
204 if (uid != 0)
205 errx(1, "-h option: %s", strerror(EPERM));
206 if (strlen(optarg) >= MAXHOSTNAMELEN)
207 errx(1, "-h option: %s: exceeds maximum "
208 "hostname size", optarg);
209 hflag = 1;
210 hostname = optarg;
211 break;
212 case 'p':
213 pflag = 1;
214 break;
215 case '?':
216 default:
217 if (uid == 0)
218 syslog(LOG_ERR, "invalid flag %c", ch);
219 usage();
220 }
221 argc -= optind;
222 argv += optind;
223
224 if (argc > 0) {
225 username = strdup(*argv);
226 if (username == NULL)
227 err(1, "strdup()");
228 ask = 0;
229 } else {
230 ask = 1;
231 }
232
233 setproctitle("-%s", getprogname());
234
235 for (cnt = getdtablesize(); cnt > 2; cnt--)
236 (void)close(cnt);
237
238 /*
239 * Get current TTY
240 */
241 ttyn = ttyname(STDIN_FILENO);
242 if (ttyn == NULL || *ttyn == '\0') {
243 (void)snprintf(tname, sizeof(tname), "%s??", _PATH_TTY);
244 ttyn = tname;
245 }
246 if ((tty = strrchr(ttyn, '/')) != NULL)
247 ++tty;
248 else
249 tty = ttyn;
250
251 /*
252 * Get "login-retries" & "login-backoff" from default class
253 */
254 lc = login_getclass(NULL);
255 prompt = login_getcapstr(lc, "login_prompt",
256 default_prompt, default_prompt);
257 passwd_prompt = login_getcapstr(lc, "passwd_prompt",
258 default_passwd_prompt, default_passwd_prompt);
259 retries = login_getcapnum(lc, "login-retries",
260 DEFAULT_RETRIES, DEFAULT_RETRIES);
261 backoff = login_getcapnum(lc, "login-backoff",
262 DEFAULT_BACKOFF, DEFAULT_BACKOFF);
263 login_close(lc);
264 lc = NULL;
265
266 /*
267 * Try to authenticate the user until we succeed or time out.
268 */
269 for (cnt = 0;; ask = 1) {
270 if (ask) {
271 fflag = 0;
272 if (olduser != NULL)
273 free(olduser);
274 olduser = username;
275 username = getloginname();
276 }
277 rootlogin = 0;
278
279 /*
280 * Note if trying multiple user names; log failures for
281 * previous user name, but don't bother logging one failure
282 * for nonexistent name (mistyped username).
283 */
284 if (failures && strcmp(olduser, username) != 0) {
285 if (failures > (pwd ? 0 : 1))
286 badlogin(olduser);
287 }
288
289 /*
290 * Load the PAM policy and set some variables
291 */
292 pam_err = pam_start("login", username, &pamc, &pamh);
293 if (pam_err != PAM_SUCCESS) {
294 pam_syslog("pam_start()");
295 au_login_fail("PAM Error", 1);
296 bail(NO_SLEEP_EXIT, 1);
297 }
298 pam_err = pam_set_item(pamh, PAM_TTY, tty);
299 if (pam_err != PAM_SUCCESS) {
300 pam_syslog("pam_set_item(PAM_TTY)");
301 au_login_fail("PAM Error", 1);
302 bail(NO_SLEEP_EXIT, 1);
303 }
304 pam_err = pam_set_item(pamh, PAM_RHOST, hostname);
305 if (pam_err != PAM_SUCCESS) {
306 pam_syslog("pam_set_item(PAM_RHOST)");
307 au_login_fail("PAM Error", 1);
308 bail(NO_SLEEP_EXIT, 1);
309 }
310
311 pwd = getpwnam(username);
312 if (pwd != NULL && pwd->pw_uid == 0)
313 rootlogin = 1;
314
315 /*
316 * If the -f option was specified and the caller is
317 * root or the caller isn't changing their uid, don't
318 * authenticate.
319 */
320 if (pwd != NULL && fflag &&
321 (uid == (uid_t)0 || uid == (uid_t)pwd->pw_uid)) {
322 /* already authenticated */
323 rval = 0;
324 auditsuccess = 0; /* opened a terminal window only */
325 } else {
326 fflag = 0;
327 (void)setpriority(PRIO_PROCESS, 0, -4);
328 rval = auth_pam();
329 (void)setpriority(PRIO_PROCESS, 0, 0);
330 }
331
332 if (pwd && rval == 0)
333 break;
334
335 pam_cleanup();
336
337 /*
338 * We are not exiting here, but this corresponds to a failed
339 * login event, so set exitstatus to 1.
340 */
341 au_login_fail("Login incorrect", 1);
342
343 (void)printf("Login incorrect\n");
344 failures++;
345
346 /*
347 * Allow up to 'retry' (10) attempts, but start
348 * backing off after 'backoff' (3) attempts.
349 */
350 if (++cnt > backoff) {
351 if (cnt >= retries) {
352 badlogin(username);
353 bail(SLEEP_EXIT, 1);
354 }
355 sleep((u_int)((cnt - backoff) * 5));
356 }
357 }
358
359 /* committed to login -- turn off timeout */
360 (void)alarm((u_int)0);
361 (void)signal(SIGHUP, SIG_DFL);
362
363 endpwent();
364
365 /* Audit successful login. */
366 if (auditsuccess)
367 au_login_success();
368
369 /*
370 * Establish the login class.
371 */
372 lc = login_getpwclass(pwd);
373 lc_user = login_getuserclass(pwd);
374
375 if (!(quietlog = login_getcapbool(lc_user, "hushlogin", 0)))
376 quietlog = login_getcapbool(lc, "hushlogin", 0);
377
378 /*
379 * Switching needed for NFS with root access disabled.
380 *
381 * XXX: This change fails to modify the additional groups for the
382 * process, and as such, may restrict rights normally granted
383 * through those groups.
384 */
385 (void)setegid(pwd->pw_gid);
386 (void)seteuid(rootlogin ? 0 : pwd->pw_uid);
387 if (!*pwd->pw_dir || chdir(pwd->pw_dir) < 0) {
388 if (login_getcapbool(lc, "requirehome", 0))
389 refused("Home directory not available", "HOMEDIR", 1);
390 if (chdir("/") < 0)
391 refused("Cannot find root directory", "ROOTDIR", 1);
392 if (!quietlog || *pwd->pw_dir)
393 printf("No home directory.\nLogging in with home = \"/\".\n");
394 pwd->pw_dir = strdup("/");
395 if (pwd->pw_dir == NULL) {
396 syslog(LOG_NOTICE, "strdup(): %m");
397 bail(SLEEP_EXIT, 1);
398 }
399 }
400 (void)seteuid(euid);
401 (void)setegid(egid);
402 if (!quietlog) {
403 quietlog = access(_PATH_HUSHLOGIN, F_OK) == 0;
404 if (!quietlog)
405 pam_silent = 0;
406 }
407
408 shell = login_getcapstr(lc, "shell", pwd->pw_shell, pwd->pw_shell);
409 if (*pwd->pw_shell == '\0')
410 pwd->pw_shell = strdup(_PATH_BSHELL);
411 if (pwd->pw_shell == NULL) {
412 syslog(LOG_NOTICE, "strdup(): %m");
413 bail(SLEEP_EXIT, 1);
414 }
415 if (*shell == '\0') /* Not overridden */
416 shell = pwd->pw_shell;
417 if ((shell = strdup(shell)) == NULL) {
418 syslog(LOG_NOTICE, "strdup(): %m");
419 bail(SLEEP_EXIT, 1);
420 }
421
422 /*
423 * Set device protections, depending on what terminal the
424 * user is logged in. This feature is used on Suns to give
425 * console users better privacy.
426 */
427 login_fbtab(tty, pwd->pw_uid, pwd->pw_gid);
428
429 /*
430 * Clear flags of the tty. None should be set, and when the
431 * user sets them otherwise, this can cause the chown to fail.
432 * Since it isn't clear that flags are useful on character
433 * devices, we just clear them.
434 *
435 * We don't log in the case of EOPNOTSUPP because dev might be
436 * on NFS, which doesn't support chflags.
437 *
438 * We don't log in the EROFS because that means that /dev is on
439 * a read only file system and we assume that the permissions there
440 * are sane.
441 */
442 if (ttyn != tname && chflags(ttyn, 0))
443 if (errno != EOPNOTSUPP && errno != EROFS)
444 syslog(LOG_ERR, "chflags(%s): %m", ttyn);
445 if (ttyn != tname && chown(ttyn, pwd->pw_uid,
446 (gr = getgrnam(TTYGRPNAME)) ? gr->gr_gid : pwd->pw_gid))
447 if (errno != EROFS)
448 syslog(LOG_ERR, "chown(%s): %m", ttyn);
449
450 /*
451 * Exclude cons/vt/ptys only, assume dialup otherwise
452 * TODO: Make dialup tty determination a library call
453 * for consistency (finger etc.)
454 */
455 if (hflag && isdialuptty(tty))
456 syslog(LOG_INFO, "DIALUP %s, %s", tty, pwd->pw_name);
457
458#ifdef LOGALL
459 /*
460 * Syslog each successful login, so we don't have to watch
461 * hundreds of wtmp or lastlogin files.
462 */
463 if (hflag)
464 syslog(LOG_INFO, "login from %s on %s as %s",
465 hostname, tty, pwd->pw_name);
466 else
467 syslog(LOG_INFO, "login on %s as %s",
468 tty, pwd->pw_name);
469#endif
470
471 /*
472 * If fflag is on, assume caller/authenticator has logged root
473 * login.
474 */
475 if (rootlogin && fflag == 0) {
476 if (hflag)
477 syslog(LOG_NOTICE, "ROOT LOGIN (%s) ON %s FROM %s",
478 username, tty, hostname);
479 else
480 syslog(LOG_NOTICE, "ROOT LOGIN (%s) ON %s",
481 username, tty);
482 }
483
484 /*
485 * Destroy environment unless user has requested its
486 * preservation - but preserve TERM in all cases
487 */
488 term = getenv("TERM");
489 if (!pflag)
490 environ = envinit;
491 if (term != NULL)
492 setenv("TERM", term, 0);
493
494 /*
495 * PAM modules might add supplementary groups during pam_setcred().
496 */
497 if (setusercontext(lc, pwd, pwd->pw_uid, LOGIN_SETGROUP) != 0) {
498 syslog(LOG_ERR, "setusercontext() failed - exiting");
499 bail(NO_SLEEP_EXIT, 1);
500 }
501
502 pam_err = pam_setcred(pamh, pam_silent|PAM_ESTABLISH_CRED);
503 if (pam_err != PAM_SUCCESS) {
504 pam_syslog("pam_setcred()");
505 bail(NO_SLEEP_EXIT, 1);
506 }
507 pam_cred_established = 1;
508
509 pam_err = pam_open_session(pamh, pam_silent);
510 if (pam_err != PAM_SUCCESS) {
511 pam_syslog("pam_open_session()");
512 bail(NO_SLEEP_EXIT, 1);
513 }
514 pam_session_established = 1;
515
516 /*
517 * We must fork() before setuid() because we need to call
518 * pam_close_session() as root.
519 */
520 pid = fork();
521 if (pid < 0) {
522 err(1, "fork");
523 } else if (pid != 0) {
524 /*
525 * Parent: wait for child to finish, then clean up
526 * session.
527 */
528 int status;
529 setproctitle("-%s [pam]", getprogname());
530 waitpid(pid, &status, 0);
531 bail(NO_SLEEP_EXIT, 0);
532 }
533
534 /*
535 * NOTICE: We are now in the child process!
536 */
537
538 /*
539 * Add any environment variables the PAM modules may have set.
540 */
541 export_pam_environment();
542
543 /*
544 * We're done with PAM now; our parent will deal with the rest.
545 */
546 pam_end(pamh, 0);
547 pamh = NULL;
548
549 /*
550 * We don't need to be root anymore, so set the login name and
551 * the UID.
552 */
553 if (setlogin(username) != 0) {
554 syslog(LOG_ERR, "setlogin(%s): %m - exiting", username);
555 bail(NO_SLEEP_EXIT, 1);
556 }
557 if (setusercontext(lc, pwd, pwd->pw_uid,
558 LOGIN_SETALL & ~(LOGIN_SETLOGIN|LOGIN_SETGROUP)) != 0) {
559 syslog(LOG_ERR, "setusercontext() failed - exiting");
560 exit(1);
561 }
562
563 (void)setenv("SHELL", pwd->pw_shell, 1);
564 (void)setenv("HOME", pwd->pw_dir, 1);
565 /* Overwrite "term" from login.conf(5) for any known TERM */
566 if (term == NULL && (tp = stypeof(tty)) != NULL)
567 (void)setenv("TERM", tp, 1);
568 else
569 (void)setenv("TERM", TERM_UNKNOWN, 0);
570 (void)setenv("LOGNAME", username, 1);
571 (void)setenv("USER", username, 1);
572 (void)setenv("PATH", rootlogin ? _PATH_STDPATH : _PATH_DEFPATH, 0);
573
574 if (!quietlog) {
575 const char *cw;
576
577 cw = login_getcapstr(lc, "copyright", NULL, NULL);
578 if (cw == NULL || motd(cw) == -1)
579 (void)printf("%s", copyright);
580
581 (void)printf("\n");
582
583 cw = login_getcapstr(lc, "welcome", NULL, NULL);
584 if (cw != NULL && access(cw, F_OK) == 0)
585 motd(cw);
586 else
587 motd(_PATH_MOTDFILE);
588
589 if (login_getcapbool(lc_user, "nocheckmail", 0) == 0 &&
590 login_getcapbool(lc, "nocheckmail", 0) == 0) {
591 char *cx;
592
593 /* $MAIL may have been set by class. */
594 cx = getenv("MAIL");
595 if (cx == NULL) {
596 asprintf(&cx, "%s/%s",
597 _PATH_MAILDIR, pwd->pw_name);
598 }
599 if (cx && stat(cx, &st) == 0 && st.st_size != 0)
600 (void)printf("You have %smail.\n",
601 (st.st_mtime > st.st_atime) ? "new " : "");
602 if (getenv("MAIL") == NULL)
603 free(cx);
604 }
605 }
606
607 login_close(lc_user);
608 login_close(lc);
609
610 (void)signal(SIGALRM, SIG_DFL);
611 (void)signal(SIGQUIT, SIG_DFL);
612 (void)signal(SIGINT, SIG_DFL);
613 (void)signal(SIGTSTP, SIG_IGN);
614
615 /*
616 * Login shells have a leading '-' in front of argv[0]
617 */
618 p = strrchr(pwd->pw_shell, '/');
619 if (asprintf(&arg0, "-%s", p ? p + 1 : pwd->pw_shell) >= MAXPATHLEN) {
620 syslog(LOG_ERR, "user: %s: shell exceeds maximum pathname size",
621 username);
622 errx(1, "shell exceeds maximum pathname size");
623 } else if (arg0 == NULL) {
624 err(1, "asprintf()");
625 }
626
627 execlp(shell, arg0, (char *)0);
628 err(1, "%s", shell);
629
630 /*
631 * That's it, folks!
632 */
633}
634
635/*
636 * Attempt to authenticate the user using PAM. Returns 0 if the user is
637 * authenticated, or 1 if not authenticated. If some sort of PAM system
638 * error occurs (e.g., the "/etc/pam.conf" file is missing) then this
639 * function returns -1. This can be used as an indication that we should
640 * fall back to a different authentication mechanism.
641 */
642static int
643auth_pam(void)
644{
645 const char *tmpl_user;
646 const void *item;
647 int rval;
648
649 pam_err = pam_authenticate(pamh, pam_silent);
650 switch (pam_err) {
651
652 case PAM_SUCCESS:
653 /*
654 * With PAM we support the concept of a "template"
655 * user. The user enters a login name which is
656 * authenticated by PAM, usually via a remote service
657 * such as RADIUS or TACACS+. If authentication
658 * succeeds, a different but related "template" name
659 * is used for setting the credentials, shell, and
660 * home directory. The name the user enters need only
661 * exist on the remote authentication server, but the
662 * template name must be present in the local password
663 * database.
664 *
665 * This is supported by two various mechanisms in the
666 * individual modules. However, from the application's
667 * point of view, the template user is always passed
668 * back as a changed value of the PAM_USER item.
669 */
670 pam_err = pam_get_item(pamh, PAM_USER, &item);
671 if (pam_err == PAM_SUCCESS) {
672 tmpl_user = (const char *)item;
673 if (strcmp(username, tmpl_user) != 0)
674 pwd = getpwnam(tmpl_user);
675 } else {
676 pam_syslog("pam_get_item(PAM_USER)");
677 }
678 rval = 0;
679 break;
680
681 case PAM_AUTH_ERR:
682 case PAM_USER_UNKNOWN:
683 case PAM_MAXTRIES:
684 rval = 1;
685 break;
686
687 default:
688 pam_syslog("pam_authenticate()");
689 rval = -1;
690 break;
691 }
692
693 if (rval == 0) {
694 pam_err = pam_acct_mgmt(pamh, pam_silent);
695 switch (pam_err) {
696 case PAM_SUCCESS:
697 break;
698 case PAM_NEW_AUTHTOK_REQD:
699 pam_err = pam_chauthtok(pamh,
700 pam_silent|PAM_CHANGE_EXPIRED_AUTHTOK);
701 if (pam_err != PAM_SUCCESS) {
702 pam_syslog("pam_chauthtok()");
703 rval = 1;
704 }
705 break;
706 default:
707 pam_syslog("pam_acct_mgmt()");
708 rval = 1;
709 break;
710 }
711 }
712
713 if (rval != 0) {
714 pam_end(pamh, pam_err);
715 pamh = NULL;
716 }
717 return (rval);
718}
719
720/*
721 * Export any environment variables PAM modules may have set
722 */
723static void
724export_pam_environment()
725{
726 char **pam_env;
727 char **pp;
728
729 pam_env = pam_getenvlist(pamh);
730 if (pam_env != NULL) {
731 for (pp = pam_env; *pp != NULL; pp++) {
732 (void)export(*pp);
733 free(*pp);
734 }
735 }
736}
737
738/*
739 * Perform sanity checks on an environment variable:
740 * - Make sure there is an '=' in the string.
741 * - Make sure the string doesn't run on too long.
742 * - Do not export certain variables. This list was taken from the
743 * Solaris pam_putenv(3) man page.
744 * Then export it.
745 */
746static int
747export(const char *s)
748{
749 static const char *noexport[] = {
750 "SHELL", "HOME", "LOGNAME", "MAIL", "CDPATH",
751 "IFS", "PATH", NULL
752 };
753 const char **pp;
754 size_t n;
755
756 if (strlen(s) > 1024 || strchr(s, '=') == NULL)
757 return (0);
758 if (strncmp(s, "LD_", 3) == 0)
759 return (0);
760 for (pp = noexport; *pp != NULL; pp++) {
761 n = strlen(*pp);
762 if (s[n] == '=' && strncmp(s, *pp, n) == 0)
763 return (0);
764 }
765 (void)putenv(s);
766 return (1);
767}
768
769static void
770usage()
771{
772
773 (void)fprintf(stderr, "usage: login [-fp] [-h hostname] [username]\n");
774 exit(1);
775}
776
777/*
778 * Prompt user and read login name from stdin.
779 */
780static char *
781getloginname()
782{
783 char *nbuf, *p;
784 int ch;
785
786 nbuf = malloc(MAXLOGNAME);
787 if (nbuf == NULL)
788 err(1, "malloc()");
789 do {
790 (void)printf("%s", prompt);
791 for (p = nbuf; (ch = getchar()) != '\n'; ) {
792 if (ch == EOF) {
793 badlogin(username);
794 bail(NO_SLEEP_EXIT, 0);
795 }
796 if (p < nbuf + MAXLOGNAME - 1)
797 *p++ = ch;
798 }
799 } while (p == nbuf);
800
801 *p = '\0';
802 if (nbuf[0] == '-') {
803 pam_silent = 0;
804 memmove(nbuf, nbuf + 1, strlen(nbuf));
805 } else {
806 pam_silent = PAM_SILENT;
807 }
808 return nbuf;
809}
810
811/*
812 * SIGINT handler for motd().
813 */
814static volatile int motdinterrupt;
815static void
816sigint(int signo __unused)
817{
818 motdinterrupt = 1;
819}
820
821/*
822 * Display the contents of a file (such as /etc/motd).
823 */
824static int
825motd(const char *motdfile)
826{
827 sig_t oldint;
828 FILE *f;
829 int ch;
830
831 if ((f = fopen(motdfile, "r")) == NULL)
832 return (-1);
833 motdinterrupt = 0;
834 oldint = signal(SIGINT, sigint);
835 while ((ch = fgetc(f)) != EOF && !motdinterrupt)
836 putchar(ch);
837 signal(SIGINT, oldint);
838 if (ch != EOF || ferror(f)) {
839 fclose(f);
840 return (-1);
841 }
842 fclose(f);
843 return (0);
844}
845
846/*
847 * SIGALRM handler, to enforce login prompt timeout.
848 *
849 * XXX This can potentially confuse the hell out of PAM. We should
850 * XXX instead implement a conversation function that returns
851 * XXX PAM_CONV_ERR when interrupted by a signal, and have the signal
852 * XXX handler just set a flag.
853 */
854static void
855timedout(int signo __unused)
856{
857
858 longjmp(timeout_buf, signo);
859}
860
861static void
862badlogin(char *name)
863{
864
865 if (failures == 0)
866 return;
867 if (hflag) {
868 syslog(LOG_NOTICE, "%d LOGIN FAILURE%s FROM %s",
869 failures, failures > 1 ? "S" : "", hostname);
870 syslog(LOG_AUTHPRIV|LOG_NOTICE,
871 "%d LOGIN FAILURE%s FROM %s, %s",
872 failures, failures > 1 ? "S" : "", hostname, name);
873 } else {
874 syslog(LOG_NOTICE, "%d LOGIN FAILURE%s ON %s",
875 failures, failures > 1 ? "S" : "", tty);
876 syslog(LOG_AUTHPRIV|LOG_NOTICE,
877 "%d LOGIN FAILURE%s ON %s, %s",
878 failures, failures > 1 ? "S" : "", tty, name);
879 }
880 failures = 0;
881}
882
883const char *
884stypeof(char *ttyid)
885{
886 struct ttyent *t;
887
888 if (ttyid != NULL && *ttyid != '\0') {
889 t = getttynam(ttyid);
890 if (t != NULL && t->ty_type != NULL)
891 return (t->ty_type);
892 }
893 return (NULL);
894}
895
896static void
897refused(const char *msg, const char *rtype, int lout)
898{
899
900 if (msg != NULL)
901 printf("%s.\n", msg);
902 if (hflag)
903 syslog(LOG_NOTICE, "LOGIN %s REFUSED (%s) FROM %s ON TTY %s",
904 pwd->pw_name, rtype, hostname, tty);
905 else
906 syslog(LOG_NOTICE, "LOGIN %s REFUSED (%s) ON TTY %s",
907 pwd->pw_name, rtype, tty);
908 if (lout)
909 bail(SLEEP_EXIT, 1);
910}
911
912/*
913 * Log a PAM error
914 */
915static void
916pam_syslog(const char *msg)
917{
918 syslog(LOG_ERR, "%s: %s", msg, pam_strerror(pamh, pam_err));
919}
920
921/*
922 * Shut down PAM
923 */
924static void
925pam_cleanup()
926{
927
928 if (pamh != NULL) {
929 if (pam_session_established) {
930 pam_err = pam_close_session(pamh, 0);
931 if (pam_err != PAM_SUCCESS)
932 pam_syslog("pam_close_session()");
933 }
934 pam_session_established = 0;
935 if (pam_cred_established) {
936 pam_err = pam_setcred(pamh, pam_silent|PAM_DELETE_CRED);
937 if (pam_err != PAM_SUCCESS)
938 pam_syslog("pam_setcred()");
939 }
940 pam_cred_established = 0;
941 pam_end(pamh, pam_err);
942 pamh = NULL;
943 }
944}
945
946/*
947 * Exit, optionally after sleeping a few seconds
948 */
949void
950bail(int sec, int eval)
951{
952
953 pam_cleanup();
954 audit_logout();
955 (void)sleep(sec);
956 exit(eval);
957}