login_pam.c revision 1.22 1 /* $NetBSD: login_pam.c,v 1.22 2012/04/23 09:27:36 martin 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("@(#) Copyright (c) 1980, 1987, 1988, 1991, 1993, 1994\
35 The Regents of the University of California. All rights reserved.");
36 #endif /* not lint */
37
38 #ifndef lint
39 #if 0
40 static char sccsid[] = "@(#)login.c 8.4 (Berkeley) 4/2/94";
41 #endif
42 __RCSID("$NetBSD: login_pam.c,v 1.22 2012/04/23 09:27:36 martin Exp $");
43 #endif /* not lint */
44
45 /*
46 * login [ name ]
47 * login -h hostname (for telnetd, etc.)
48 * login -f name (for pre-authenticated login: datakit, xterm, etc.)
49 */
50
51 #include <sys/param.h>
52 #include <sys/stat.h>
53 #include <sys/time.h>
54 #include <sys/resource.h>
55 #include <sys/file.h>
56 #include <sys/wait.h>
57 #include <sys/socket.h>
58
59 #include <err.h>
60 #include <errno.h>
61 #include <grp.h>
62 #include <pwd.h>
63 #include <setjmp.h>
64 #include <signal.h>
65 #include <stdio.h>
66 #include <stdlib.h>
67 #include <string.h>
68 #include <syslog.h>
69 #include <time.h>
70 #include <ttyent.h>
71 #include <tzfile.h>
72 #include <unistd.h>
73 #include <util.h>
74 #include <login_cap.h>
75 #include <vis.h>
76
77 #include <security/pam_appl.h>
78 #include <security/openpam.h>
79
80 #include "pathnames.h"
81 #include "common.h"
82
83 #if 0
84 static int rootterm(char *);
85 #endif
86 static void usage(void) __attribute__((__noreturn__));
87
88 static struct pam_conv pamc = { openpam_ttyconv, NULL };
89
90 #define TTYGRPNAME "tty" /* name of group to own ttys */
91
92 #define DEFAULT_BACKOFF 3
93 #define DEFAULT_RETRIES 10
94
95 static struct passwd pwres;
96 static char pwbuf[1024];
97 static struct group grs, *grp;
98 static char grbuf[1024];
99 extern char **environ;
100
101 int
102 main(int argc, char *argv[])
103 {
104 struct stat st;
105 int ask, ch, cnt, fflag, pflag, quietlog, rootlogin;
106 int auth_passed;
107 uid_t uid, saved_uid;
108 gid_t saved_gid, saved_gids[NGROUPS_MAX];
109 int nsaved_gids;
110 char *domain, *p, *ttyn, *pwprompt;
111 char tbuf[MAXPATHLEN + 2], tname[sizeof(_PATH_TTY) + 10];
112 char localhost[MAXHOSTNAMELEN + 1];
113 int need_chpass, require_chpass;
114 int login_retries = DEFAULT_RETRIES,
115 login_backoff = DEFAULT_BACKOFF;
116 char *shell = NULL;
117 login_cap_t *lc = NULL;
118 pam_handle_t *pamh = NULL;
119 int pam_err;
120 sig_t oint, oabrt, oquit, oalrm;
121 const void *newuser;
122 int pam_silent = PAM_SILENT;
123 pid_t xpid, pid;
124 int status;
125 char *saved_term;
126 char **pamenv;
127
128 tbuf[0] = '\0';
129 pwprompt = NULL;
130 nested = NULL;
131 need_chpass = require_chpass = 0;
132
133 oabrt = signal(SIGABRT, SIG_IGN);
134 oalrm = signal(SIGALRM, timedout);
135 oint = signal(SIGINT, SIG_IGN);
136 oquit = signal(SIGQUIT, SIG_IGN);
137
138 (void)alarm(timeout);
139 (void)setpriority(PRIO_PROCESS, 0, 0);
140
141 openlog("login", 0, LOG_AUTH);
142
143 /*
144 * -p is used by getty to tell login not to destroy the environment
145 * -f is used to skip a second login authentication
146 * -h is used by other servers to pass the name of the remote host to
147 * login so that it may be placed in utmp/utmpx and wtmp/wtmpx
148 * -a in addition to -h, a server my supply -a to pass the actual
149 * server address.
150 */
151 domain = NULL;
152 if (gethostname(localhost, sizeof(localhost)) < 0)
153 syslog(LOG_ERR, "couldn't get local hostname: %m");
154 else
155 domain = strchr(localhost, '.');
156 localhost[sizeof(localhost) - 1] = '\0';
157
158 fflag = pflag = 0;
159 have_ss = 0;
160 uid = getuid();
161 while ((ch = getopt(argc, argv, "a:fh:p")) != -1)
162 switch (ch) {
163 case 'a':
164 if (uid) {
165 errno = EPERM;
166 err(EXIT_FAILURE, "-a option");
167 }
168 decode_ss(optarg);
169 break;
170 case 'f':
171 fflag = 1;
172 break;
173 case 'h':
174 if (uid) {
175 errno = EPERM;
176 err(EXIT_FAILURE, "-h option");
177 }
178 if (domain && (p = strchr(optarg, '.')) != NULL &&
179 strcasecmp(p, domain) == 0)
180 *p = '\0';
181 hostname = optarg;
182 break;
183 case 'p':
184 pflag = 1;
185 break;
186 default:
187 case '?':
188 usage();
189 break;
190 }
191
192 setproctitle(NULL);
193 argc -= optind;
194 argv += optind;
195
196 if (*argv) {
197 username = trimloginname(*argv);
198 ask = 0;
199 } else
200 ask = 1;
201
202 #ifdef F_CLOSEM
203 (void)fcntl(3, F_CLOSEM, 0);
204 #else
205 for (cnt = getdtablesize(); cnt > 2; cnt--)
206 (void)close(cnt);
207 #endif
208
209 ttyn = ttyname(STDIN_FILENO);
210 if (ttyn == NULL || *ttyn == '\0') {
211 (void)snprintf(tname, sizeof(tname), "%s??", _PATH_TTY);
212 ttyn = tname;
213 }
214 if ((tty = strstr(ttyn, "/pts/")) != NULL)
215 ++tty;
216 else if ((tty = strrchr(ttyn, '/')) != NULL)
217 ++tty;
218 else
219 tty = ttyn;
220
221 if (issetugid()) {
222 nested = strdup(user_from_uid(getuid(), 0));
223 if (nested == NULL) {
224 syslog(LOG_ERR, "strdup: %m");
225 sleepexit(EXIT_FAILURE);
226 }
227 }
228
229 /* Get "login-retries" and "login-backoff" from default class */
230 if ((lc = login_getclass(NULL)) != NULL) {
231 login_retries = (int)login_getcapnum(lc, "login-retries",
232 DEFAULT_RETRIES, DEFAULT_RETRIES);
233 login_backoff = (int)login_getcapnum(lc, "login-backoff",
234 DEFAULT_BACKOFF, DEFAULT_BACKOFF);
235 login_close(lc);
236 lc = NULL;
237 }
238
239
240 for (cnt = 0;; ask = 1) {
241 if (ask) {
242 fflag = 0;
243 username = trimloginname(getloginname());
244 }
245 rootlogin = 0;
246 auth_passed = 0;
247
248 /*
249 * Note if trying multiple user names; log failures for
250 * previous user name, but don't bother logging one failure
251 * for nonexistent name (mistyped username).
252 */
253 if (failures && strcmp(tbuf, username)) {
254 if (failures > (pwd ? 0 : 1))
255 badlogin(tbuf);
256 failures = 0;
257 }
258
259 #define PAM_END(msg) do { \
260 syslog(LOG_ERR, "%s: %s", msg, pam_strerror(pamh, pam_err)); \
261 warnx("%s: %s", msg, pam_strerror(pamh, pam_err)); \
262 pam_end(pamh, pam_err); \
263 sleepexit(EXIT_FAILURE); \
264 } while (/*CONSTCOND*/0)
265
266 pam_err = pam_start("login", username, &pamc, &pamh);
267 if (pam_err != PAM_SUCCESS) {
268 if (pamh != NULL)
269 PAM_END("pam_start");
270 /* Things went really bad... */
271 syslog(LOG_ERR, "pam_start failed: %s",
272 pam_strerror(pamh, pam_err));
273 errx(EXIT_FAILURE, "pam_start failed");
274 }
275
276 #define PAM_SET_ITEM(item, var) do { \
277 pam_err = pam_set_item(pamh, (item), (var)); \
278 if (pam_err != PAM_SUCCESS) \
279 PAM_END("pam_set_item(" # item ")"); \
280 } while (/*CONSTCOND*/0)
281
282 /*
283 * Fill hostname tty, and nested user
284 */
285 PAM_SET_ITEM(PAM_RHOST, hostname);
286 PAM_SET_ITEM(PAM_TTY, tty);
287 if (nested)
288 PAM_SET_ITEM(PAM_NUSER, nested);
289 if (have_ss)
290 PAM_SET_ITEM(PAM_SOCKADDR, &ss);
291
292 /*
293 * Don't check for errors, because we don't want to give
294 * out any information.
295 */
296 pwd = NULL;
297 (void)getpwnam_r(username, &pwres, pwbuf, sizeof(pwbuf), &pwd);
298
299 /*
300 * Establish the class now, before we might goto
301 * within the next block. pwd can be NULL since it
302 * falls back to the "default" class if it is.
303 */
304 lc = login_getclass(pwd ? pwd->pw_class : NULL);
305
306 /*
307 * if we have a valid account name, and it doesn't have a
308 * password, or the -f option was specified and the caller
309 * is root or the caller isn't changing their uid, don't
310 * authenticate.
311 */
312 if (pwd) {
313 if (pwd->pw_uid == 0)
314 rootlogin = 1;
315
316 if (fflag && (uid == 0 || uid == pwd->pw_uid)) {
317 /* already authenticated */
318 auth_passed = 1;
319 goto skip_auth;
320 }
321 }
322
323 (void)setpriority(PRIO_PROCESS, 0, -4);
324
325 switch(pam_err = pam_authenticate(pamh, pam_silent)) {
326 case PAM_SUCCESS:
327 /*
328 * PAM can change the user, refresh
329 * username, pwd, and lc.
330 */
331 pam_err = pam_get_item(pamh, PAM_USER, &newuser);
332 if (pam_err != PAM_SUCCESS)
333 PAM_END("pam_get_item(PAM_USER)");
334
335 username = newuser;
336 /*
337 * Don't check for errors, because we don't want to give
338 * out any information.
339 */
340 pwd = NULL;
341 (void)getpwnam_r(username, &pwres, pwbuf, sizeof(pwbuf),
342 &pwd);
343 lc = login_getpwclass(pwd);
344 auth_passed = 1;
345
346 switch (pam_err = pam_acct_mgmt(pamh, pam_silent)) {
347 case PAM_SUCCESS:
348 break;
349
350 case PAM_NEW_AUTHTOK_REQD:
351 pam_err = pam_chauthtok(pamh,
352 pam_silent|PAM_CHANGE_EXPIRED_AUTHTOK);
353
354 if (pam_err != PAM_SUCCESS)
355 PAM_END("pam_chauthtok");
356 break;
357
358 case PAM_AUTH_ERR:
359 case PAM_USER_UNKNOWN:
360 case PAM_MAXTRIES:
361 auth_passed = 0;
362 break;
363
364 default:
365 PAM_END("pam_acct_mgmt");
366 break;
367 }
368 break;
369
370 case PAM_AUTH_ERR:
371 case PAM_USER_UNKNOWN:
372 case PAM_MAXTRIES:
373 auth_passed = 0;
374 break;
375
376 default:
377 PAM_END("pam_authenticate");
378 break;
379 }
380
381 (void)setpriority(PRIO_PROCESS, 0, 0);
382
383 skip_auth:
384 /*
385 * If the user exists and authentication passed,
386 * get out of the loop and login the user.
387 */
388 if (pwd && auth_passed)
389 break;
390
391 (void)printf("Login incorrect or refused on this terminal.\n");
392 failures++;
393 cnt++;
394 /*
395 * We allow login_retries tries, but after login_backoff
396 * we start backing off. These default to 10 and 3
397 * respectively.
398 */
399 if (cnt > login_backoff) {
400 if (cnt >= login_retries) {
401 badlogin(username);
402 pam_end(pamh, PAM_SUCCESS);
403 sleepexit(EXIT_FAILURE);
404 }
405 sleep((u_int)((cnt - login_backoff) * 5));
406 }
407 }
408
409 /* committed to login -- turn off timeout */
410 (void)alarm((u_int)0);
411
412 endpwent();
413
414 quietlog = login_getcapbool(lc, "hushlogin", 0);
415
416 /*
417 * Temporarily give up special privileges so we can change
418 * into NFS-mounted homes that are exported for non-root
419 * access and have mode 7x0
420 */
421 saved_uid = geteuid();
422 saved_gid = getegid();
423 nsaved_gids = getgroups(NGROUPS_MAX, saved_gids);
424
425 (void)setegid(pwd->pw_gid);
426 initgroups(username, pwd->pw_gid);
427 (void)seteuid(pwd->pw_uid);
428
429 if (chdir(pwd->pw_dir) != 0) {
430 if (login_getcapbool(lc, "requirehome", 0)) {
431 (void)printf("Home directory %s required\n",
432 pwd->pw_dir);
433 pam_end(pamh, PAM_SUCCESS);
434 exit(EXIT_FAILURE);
435 }
436
437 (void)printf("No home directory %s!\n", pwd->pw_dir);
438 if (chdir("/") == -1) {
439 pam_end(pamh, PAM_SUCCESS);
440 exit(EXIT_FAILURE);
441 }
442 pwd->pw_dir = __UNCONST("/");
443 (void)printf("Logging in with home = \"/\".\n");
444 }
445
446 if (!quietlog) {
447 quietlog = access(_PATH_HUSHLOGIN, F_OK) == 0;
448 pam_silent = quietlog ? PAM_SILENT : 0;
449 }
450
451 /* regain special privileges */
452 setegid(saved_gid);
453 setgroups(nsaved_gids, saved_gids);
454 seteuid(saved_uid);
455
456 (void)getgrnam_r(TTYGRPNAME, &grs, grbuf, sizeof(grbuf), &grp);
457 (void)chown(ttyn, pwd->pw_uid,
458 (grp != NULL) ? grp->gr_gid : pwd->pw_gid);
459
460 if (ttyaction(ttyn, "login", pwd->pw_name))
461 (void)printf("Warning: ttyaction failed.\n");
462
463 /* Nothing else left to fail -- really log in. */
464 update_db(quietlog, rootlogin, fflag);
465
466 if (nested == NULL && setusercontext(lc, pwd, pwd->pw_uid,
467 LOGIN_SETLOGIN) != 0) {
468 syslog(LOG_ERR, "setusercontext failed");
469 pam_end(pamh, PAM_SUCCESS);
470 exit(EXIT_FAILURE);
471 }
472
473 if (tty[sizeof("tty")-1] == 'd')
474 syslog(LOG_INFO, "DIALUP %s, %s", tty, pwd->pw_name);
475
476
477 /*
478 * Establish groups
479 */
480 if (setusercontext(lc, pwd, pwd->pw_uid, LOGIN_SETGROUP) != 0) {
481 syslog(LOG_ERR, "setusercontext failed");
482 pam_end(pamh, PAM_SUCCESS);
483 exit(EXIT_FAILURE);
484 }
485
486 pam_err = pam_setcred(pamh, pam_silent|PAM_ESTABLISH_CRED);
487 if (pam_err != PAM_SUCCESS)
488 PAM_END("pam_setcred");
489
490 pam_err = pam_open_session(pamh, pam_silent);
491 if (pam_err != PAM_SUCCESS)
492 PAM_END("pam_open_session");
493
494 /*
495 * Fork because we need to call pam_closesession as root.
496 * Make sure signals cannot kill the parent.
497 * This has been handled in the begining of main.
498 */
499
500 switch(pid = fork()) {
501 case -1:
502 pam_err = pam_close_session(pamh, 0);
503 if (pam_err != PAM_SUCCESS) {
504 syslog(LOG_ERR, "pam_close_session: %s",
505 pam_strerror(pamh, pam_err));
506 warnx("pam_close_session: %s",
507 pam_strerror(pamh, pam_err));
508 }
509 syslog(LOG_ERR, "fork failed: %m");
510 warn("fork failed");
511 pam_end(pamh, pam_err);
512 exit(EXIT_FAILURE);
513 break;
514
515 case 0: /* Child */
516 break;
517
518 default:
519 /*
520 * Parent: wait for the child to terminate
521 * and call pam_close_session.
522 */
523 if ((xpid = waitpid(pid, &status, 0)) != pid) {
524 pam_err = pam_close_session(pamh, 0);
525 if (pam_err != PAM_SUCCESS) {
526 syslog(LOG_ERR,
527 "pam_close_session: %s",
528 pam_strerror(pamh, pam_err));
529 warnx("pam_close_session: %s",
530 pam_strerror(pamh, pam_err));
531 }
532 pam_end(pamh, pam_err);
533 if (xpid != -1)
534 warnx("wrong PID: %d != %d", pid, xpid);
535 else
536 warn("wait for pid %d failed", pid);
537 exit(EXIT_FAILURE);
538 }
539
540 (void)signal(SIGABRT, oabrt);
541 (void)signal(SIGALRM, oalrm);
542 (void)signal(SIGINT, oint);
543 (void)signal(SIGQUIT, oquit);
544 if ((pam_err = pam_close_session(pamh, 0)) != PAM_SUCCESS) {
545 syslog(LOG_ERR, "pam_close_session: %s",
546 pam_strerror(pamh, pam_err));
547 warnx("pam_close_session: %s",
548 pam_strerror(pamh, pam_err));
549 }
550 pam_end(pamh, PAM_SUCCESS);
551 exit(EXIT_SUCCESS);
552 break;
553 }
554
555 /*
556 * The child: starting here, we don't have to care about
557 * handling PAM issues if we exit, the parent will do the
558 * job when we exit.
559 *
560 * Destroy environment unless user has requested its preservation.
561 * Try to preserve TERM anyway.
562 */
563 saved_term = getenv("TERM");
564 if (!pflag) {
565 environ = envinit;
566 if (saved_term)
567 setenv("TERM", saved_term, 0);
568 }
569
570 if (*pwd->pw_shell == '\0')
571 pwd->pw_shell = __UNCONST(_PATH_BSHELL);
572
573 shell = login_getcapstr(lc, "shell", pwd->pw_shell, pwd->pw_shell);
574 if (*shell == '\0')
575 shell = pwd->pw_shell;
576
577 if ((pwd->pw_shell = strdup(shell)) == NULL) {
578 syslog(LOG_ERR, "Cannot alloc mem");
579 exit(EXIT_FAILURE);
580 }
581
582 (void)setenv("HOME", pwd->pw_dir, 1);
583 (void)setenv("SHELL", pwd->pw_shell, 1);
584 if (term[0] == '\0') {
585 const char *tt = stypeof(tty);
586
587 if (tt == NULL)
588 tt = login_getcapstr(lc, "term", NULL, NULL);
589
590 /* unknown term -> "su" */
591 (void)strlcpy(term, tt != NULL ? tt : "su", sizeof(term));
592 }
593 (void)setenv("TERM", term, 0);
594 (void)setenv("LOGNAME", pwd->pw_name, 1);
595 (void)setenv("USER", pwd->pw_name, 1);
596
597 /*
598 * Add PAM environement
599 */
600 if ((pamenv = pam_getenvlist(pamh)) != NULL) {
601 char **envitem;
602
603 for (envitem = pamenv; *envitem; envitem++) {
604 putenv(*envitem);
605 free(*envitem);
606 }
607
608 free(pamenv);
609 }
610
611 /* This drops root privs */
612 if (setusercontext(lc, pwd, pwd->pw_uid,
613 (LOGIN_SETALL & ~LOGIN_SETLOGIN)) != 0) {
614 syslog(LOG_ERR, "setusercontext failed");
615 exit(EXIT_FAILURE);
616 }
617
618 if (!quietlog) {
619 const char *fname;
620
621 fname = login_getcapstr(lc, "copyright", NULL, NULL);
622 if (fname != NULL && access(fname, F_OK) == 0)
623 motd(fname);
624 else
625 (void)printf("%s", copyrightstr);
626
627 fname = login_getcapstr(lc, "welcome", NULL, NULL);
628 if (fname == NULL || access(fname, F_OK) != 0)
629 fname = _PATH_MOTDFILE;
630 motd(fname);
631
632 (void)snprintf(tbuf,
633 sizeof(tbuf), "%s/%s", _PATH_MAILDIR, pwd->pw_name);
634 if (stat(tbuf, &st) == 0 && st.st_size != 0)
635 (void)printf("You have %smail.\n",
636 (st.st_mtime > st.st_atime) ? "new " : "");
637 }
638
639 login_close(lc);
640
641
642 tbuf[0] = '-';
643 (void)strlcpy(tbuf + 1, (p = strrchr(pwd->pw_shell, '/')) ?
644 p + 1 : pwd->pw_shell, sizeof(tbuf) - 1);
645
646 (void)signal(SIGABRT, oabrt);
647 (void)signal(SIGALRM, oalrm);
648 (void)signal(SIGINT, oint);
649 (void)signal(SIGQUIT, oquit);
650 (void)signal(SIGTSTP, SIG_IGN);
651
652 execlp(pwd->pw_shell, tbuf, NULL);
653 err(EXIT_FAILURE, "%s", pwd->pw_shell);
654 }
655
656 static void
657 usage(void)
658 {
659 (void)fprintf(stderr,
660 "Usage: %s [-fp] [-a address] [-h hostname] [username]\n",
661 getprogname());
662 exit(EXIT_FAILURE);
663 }
664
665 #if 0
666 static int
667 rootterm(char *ttyn)
668 {
669 struct ttyent *t;
670
671 return ((t = getttynam(ttyn)) && t->ty_status & TTY_SECURE);
672 }
673 #endif
674