login_pam.c revision 1.3 1 /* $NetBSD: login_pam.c,v 1.3 2005/02/28 16:11:36 christos 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_pam.c,v 1.3 2005/02/28 16:11:36 christos 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 #include <util.h>
75 #include <login_cap.h>
76 #include <vis.h>
77
78 #include <security/pam_appl.h>
79 #include <security/openpam.h>
80
81 #include "pathnames.h"
82
83 void badlogin (char *);
84 static void update_db (int);
85 void getloginname (void);
86 int main (int, char *[]);
87 void motd (char *);
88 int rootterm (char *);
89 void sigint (int);
90 void sleepexit (int);
91 const char *stypeof (const char *);
92 void timedout (int);
93 void decode_ss (const char *);
94 void usage (void);
95
96 static struct pam_conv pamc = { openpam_ttyconv, NULL };
97
98 #define TTYGRPNAME "tty" /* name of group to own ttys */
99
100 #define DEFAULT_BACKOFF 3
101 #define DEFAULT_RETRIES 10
102
103 /*
104 * This bounds the time given to login. Not a define so it can
105 * be patched on machines where it's too small.
106 */
107 u_int timeout = 300;
108
109 struct passwd *pwd;
110 struct group *gr;
111 int failures, have_ss;
112 char term[64], *envinit[1], *hostname, *username, *tty, *nested;
113 struct timeval now;
114 struct sockaddr_storage ss;
115
116 extern const char copyrightstr[];
117
118 int
119 main(int argc, char *argv[])
120 {
121 extern char **environ;
122 struct stat st;
123 int ask, ch, cnt, fflag, hflag, pflag, sflag, quietlog, rootlogin;
124 int auth_passed;
125 int Fflag;
126 uid_t uid, saved_uid;
127 gid_t saved_gid, saved_gids[NGROUPS_MAX];
128 int nsaved_gids;
129 char *domain, *p, *ttyn, *pwprompt;
130 char tbuf[MAXPATHLEN + 2], tname[sizeof(_PATH_TTY) + 10];
131 char localhost[MAXHOSTNAMELEN + 1];
132 int need_chpass, require_chpass;
133 int login_retries = DEFAULT_RETRIES,
134 login_backoff = DEFAULT_BACKOFF;
135 char *shell = NULL;
136 login_cap_t *lc = NULL;
137 pam_handle_t *pamh = NULL;
138 int pam_err;
139 void *oint;
140 void *oabrt;
141 const void *newuser;
142 int pam_silent = PAM_SILENT;
143 pid_t xpid, pid;
144 int status;
145 char *saved_term;
146 char **pamenv;
147
148 tbuf[0] = '\0';
149 pwprompt = NULL;
150 nested = NULL;
151 need_chpass = require_chpass = 0;
152
153 (void)signal(SIGALRM, timedout);
154 (void)alarm(timeout);
155 (void)signal(SIGQUIT, SIG_IGN);
156 (void)signal(SIGINT, SIG_IGN);
157 (void)setpriority(PRIO_PROCESS, 0, 0);
158
159 openlog("login", 0, LOG_AUTH);
160
161 /*
162 * -p is used by getty to tell login not to destroy the environment
163 * -f is used to skip a second login authentication
164 * -h is used by other servers to pass the name of the remote host to
165 * login so that it may be placed in utmp/utmpx and wtmp/wtmpx
166 * -a in addition to -h, a server my supply -a to pass the actual
167 * server address.
168 * -s is used to force use of S/Key or equivalent.
169 */
170 domain = NULL;
171 if (gethostname(localhost, sizeof(localhost)) < 0)
172 syslog(LOG_ERR, "couldn't get local hostname: %m");
173 else
174 domain = strchr(localhost, '.');
175 localhost[sizeof(localhost) - 1] = '\0';
176
177 Fflag = fflag = hflag = pflag = sflag = 0;
178 have_ss = 0;
179 uid = getuid();
180 while ((ch = getopt(argc, argv, "a:Ffh:ps")) != -1)
181 switch (ch) {
182 case 'a':
183 if (uid)
184 errx(1, "-a option: %s", strerror(EPERM));
185 decode_ss(optarg);
186 #ifdef notdef
187 (void)sockaddr_snprintf(optarg,
188 sizeof(struct sockaddr_storage), "%a", (void *)&ss);
189 #endif
190 break;
191 case 'F':
192 Fflag = 1;
193 /* FALLTHROUGH */
194 case 'f':
195 fflag = 1;
196 break;
197 case 'h':
198 if (uid)
199 errx(1, "-h option: %s", strerror(EPERM));
200 hflag = 1;
201 if (domain && (p = strchr(optarg, '.')) != NULL &&
202 strcasecmp(p, domain) == 0)
203 *p = '\0';
204 hostname = optarg;
205 break;
206 case 'p':
207 pflag = 1;
208 break;
209 case 's':
210 sflag = 1;
211 break;
212 default:
213 case '?':
214 usage();
215 break;
216 }
217
218 setproctitle(NULL);
219 argc -= optind;
220 argv += optind;
221
222 if (*argv) {
223 username = *argv;
224 ask = 0;
225 } else
226 ask = 1;
227
228 #ifdef F_CLOSEM
229 (void)fcntl(3, F_CLOSEM, 0);
230 #else
231 for (cnt = getdtablesize(); cnt > 2; cnt--)
232 (void)close(cnt);
233 #endif
234
235 ttyn = ttyname(STDIN_FILENO);
236 if (ttyn == NULL || *ttyn == '\0') {
237 (void)snprintf(tname, sizeof(tname), "%s??", _PATH_TTY);
238 ttyn = tname;
239 }
240 if ((tty = strstr(ttyn, "/pts/")) != NULL)
241 ++tty;
242 else if ((tty = strrchr(ttyn, '/')) != NULL)
243 ++tty;
244 else
245 tty = ttyn;
246
247 if (issetugid()) {
248 nested = strdup(user_from_uid(getuid(), 0));
249 if (nested == NULL) {
250 syslog(LOG_ERR, "strdup: %m");
251 sleepexit(1);
252 }
253 }
254
255 /* Get "login-retries" and "login-backoff" from default class */
256 if ((lc = login_getclass(NULL)) != NULL) {
257 login_retries = (int)login_getcapnum(lc, "login-retries",
258 DEFAULT_RETRIES, DEFAULT_RETRIES);
259 login_backoff = (int)login_getcapnum(lc, "login-backoff",
260 DEFAULT_BACKOFF, DEFAULT_BACKOFF);
261 login_close(lc);
262 lc = NULL;
263 }
264
265
266 for (cnt = 0;; ask = 1) {
267 if (ask) {
268 fflag = 0;
269 getloginname();
270 }
271 rootlogin = 0;
272 auth_passed = 0;
273 if (strlen(username) > MAXLOGNAME)
274 username[MAXLOGNAME] = '\0';
275
276 /*
277 * Note if trying multiple user names; log failures for
278 * previous user name, but don't bother logging one failure
279 * for nonexistent name (mistyped username).
280 */
281 if (failures && strcmp(tbuf, username)) {
282 if (failures > (pwd ? 0 : 1))
283 badlogin(tbuf);
284 failures = 0;
285 }
286
287 #define PAM_END(msg) do { \
288 syslog(LOG_ERR, "%s: %s", msg, pam_strerror(pamh, pam_err)); \
289 warnx("%s: %s", msg, pam_strerror(pamh, pam_err)); \
290 pam_end(pamh, pam_err); \
291 sleepexit(1); \
292 } while (/*CONSTCOND*/0)
293
294 pam_err = pam_start("login", username, &pamc, &pamh);
295 if (pam_err != PAM_SUCCESS) {
296 if (pamh != NULL)
297 PAM_END("pam_start");
298 /* Things went really bad... */
299 syslog(LOG_ERR, "pam_start failed: %s",
300 pam_strerror(pamh, pam_err));
301 errx(1, "pam_start failed");
302 }
303
304 #define PAM_SET_ITEM(item, var) do { \
305 pam_err = pam_set_item(pamh, (item), (var)); \
306 if (pam_err != PAM_SUCCESS) \
307 PAM_END("pam_set_item(" # item ")"); \
308 } while (/*CONSTCOND*/0)
309
310 /*
311 * Fill hostname and tty
312 */
313 PAM_SET_ITEM(PAM_RHOST, hostname);
314 PAM_SET_ITEM(PAM_TTY, tty);
315 if (have_ss)
316 PAM_SET_ITEM(PAM_SOCKADDR, &ss);
317
318 pwd = getpwnam(username);
319
320 /*
321 * Establish the class now, before we might goto
322 * within the next block. pwd can be NULL since it
323 * falls back to the "default" class if it is.
324 */
325 lc = login_getclass(pwd ? pwd->pw_class : NULL);
326
327 /*
328 * if we have a valid account name, and it doesn't have a
329 * password, or the -f option was specified and the caller
330 * is root or the caller isn't changing their uid, don't
331 * authenticate.
332 */
333 if (pwd) {
334 if (pwd->pw_uid == 0)
335 rootlogin = 1;
336
337 if (fflag && (uid == 0 || uid == pwd->pw_uid)) {
338 /* already authenticated */
339 auth_passed = 1;
340 goto skip_auth;
341 }
342 }
343
344 (void)setpriority(PRIO_PROCESS, 0, -4);
345
346 switch(pam_err = pam_authenticate(pamh, pam_silent)) {
347 case PAM_SUCCESS:
348 /*
349 * PAM can change the user, refresh
350 * username, pwd, and lc.
351 */
352 pam_err = pam_get_item(pamh, PAM_USER, &newuser);
353 if (pam_err != PAM_SUCCESS)
354 PAM_END("pam_get_item(PAM_USER)");
355
356 username = (char *)newuser;
357 pwd = getpwnam(username);
358 lc = login_getpwclass(pwd);
359 auth_passed = 1;
360
361 switch (pam_err = pam_acct_mgmt(pamh, pam_silent)) {
362 case PAM_SUCCESS:
363 break;
364
365 case PAM_NEW_AUTHTOK_REQD:
366 pam_err = pam_chauthtok(pamh,
367 pam_silent|PAM_CHANGE_EXPIRED_AUTHTOK);
368
369 if (pam_err != PAM_SUCCESS)
370 PAM_END("pam_chauthtok");
371 break;
372
373 default:
374 PAM_END("pam_acct_mgmt");
375 break;
376 }
377 break;
378
379 case PAM_AUTH_ERR:
380 case PAM_USER_UNKNOWN:
381 case PAM_MAXTRIES:
382 auth_passed = 0;
383 break;
384
385 default:
386 PAM_END("pam_authenticate");
387 break;
388 }
389
390 (void)setpriority(PRIO_PROCESS, 0, 0);
391
392 skip_auth:
393 /*
394 * If the user exists and authentication passed,
395 * get out of the loop and login the user.
396 */
397 if (pwd && auth_passed)
398 break;
399
400 (void)printf("Login incorrect\n");
401 failures++;
402 cnt++;
403 /* we allow 10 tries, but after 3 we start backing off */
404 if (cnt > login_backoff) {
405 if (cnt >= login_retries) {
406 badlogin(username);
407 pam_end(pamh, PAM_SUCCESS);
408 sleepexit(1);
409 }
410 sleep((u_int)((cnt - 3) * 5));
411 }
412 }
413
414 /* committed to login -- turn off timeout */
415 (void)alarm((u_int)0);
416
417 endpwent();
418
419 quietlog = login_getcapbool(lc, "hushlogin", 0);
420
421 /*
422 * Temporarily give up special privileges so we can change
423 * into NFS-mounted homes that are exported for non-root
424 * access and have mode 7x0
425 */
426 saved_uid = geteuid();
427 saved_gid = getegid();
428 nsaved_gids = getgroups(NGROUPS_MAX, saved_gids);
429
430 (void)setegid(pwd->pw_gid);
431 initgroups(username, pwd->pw_gid);
432 (void)seteuid(pwd->pw_uid);
433
434 if (chdir(pwd->pw_dir) != 0) {
435 if (login_getcapbool(lc, "requirehome", 0)) {
436 (void)printf("Home directory %s required\n",
437 pwd->pw_dir);
438 pam_end(pamh, PAM_SUCCESS);
439 exit(1);
440 }
441
442 (void)printf("No home directory %s!\n", pwd->pw_dir);
443 if (chdir("/")) {
444 pam_end(pamh, PAM_SUCCESS);
445 exit(0);
446 }
447 pwd->pw_dir = "/";
448 (void)printf("Logging in with home = \"/\".\n");
449 }
450
451 if (!quietlog) {
452 quietlog = access(_PATH_HUSHLOGIN, F_OK) == 0;
453 pam_silent = 0;
454 }
455
456 /* regain special privileges */
457 setegid(saved_gid);
458 setgroups(nsaved_gids, saved_gids);
459 seteuid(saved_uid);
460
461 (void)chown(ttyn, pwd->pw_uid,
462 (gr = getgrnam(TTYGRPNAME)) ? gr->gr_gid : pwd->pw_gid);
463
464 /* Nothing else left to fail -- really log in. */
465 update_db(quietlog);
466
467 if (nested == NULL && setusercontext(lc, pwd, pwd->pw_uid,
468 LOGIN_SETLOGIN) != 0) {
469 syslog(LOG_ERR, "setusercontext failed");
470 pam_end(pamh, PAM_SUCCESS);
471 exit(1);
472 }
473
474 if (tty[sizeof("tty")-1] == 'd')
475 syslog(LOG_INFO, "DIALUP %s, %s", tty, pwd->pw_name);
476
477 /* If fflag is on, assume caller/authenticator has logged root login. */
478 if (rootlogin && fflag == 0) {
479 if (hostname)
480 syslog(LOG_NOTICE, "ROOT LOGIN (%s) ON %s FROM %s",
481 username, tty, hostname);
482 else
483 syslog(LOG_NOTICE, "ROOT LOGIN (%s) ON %s",
484 username, tty);
485 }
486
487 /*
488 * Establish groups
489 */
490 if (setusercontext(lc, pwd, pwd->pw_uid, LOGIN_SETGROUP) != 0) {
491 syslog(LOG_ERR, "setusercontext failed");
492 pam_end(pamh, PAM_SUCCESS);
493 exit(1);
494 }
495
496 pam_err = pam_setcred(pamh, pam_silent|PAM_ESTABLISH_CRED);
497 if (pam_err != PAM_SUCCESS)
498 PAM_END("pam_setcred");
499
500 pam_err = pam_open_session(pamh, pam_silent);
501 if (pam_err != PAM_SUCCESS)
502 PAM_END("pam_open_session");
503
504 /*
505 * Fork because we need to call pam_closesession as root.
506 * Make sure signals cannot kill the parent.
507 * This is copied from crontab(8), which has to
508 * cope with a similar situation.
509 */
510 oint = signal(SIGINT, SIG_IGN);
511 oabrt = signal(SIGABRT, SIG_IGN);
512
513 switch(pid = fork()) {
514 case -1:
515 pam_err = pam_close_session(pamh, 0);
516 if (pam_err != PAM_SUCCESS) {
517 syslog(LOG_ERR, "pam_close_session: %s",
518 pam_strerror(pamh, pam_err));
519 warnx("pam_close_session: %s",
520 pam_strerror(pamh, pam_err));
521 }
522 syslog(LOG_ERR, "fork failed: %m");
523 warn("fork failed");
524 pam_end(pamh, pam_err);
525 exit(1);
526 break;
527
528 case 0: /* Child */
529 break;
530
531 default:
532 /*
533 * Parent: wait for the child to terminate
534 * and call pam_close_session.
535 */
536 if ((xpid = waitpid(pid, &status, 0)) != pid) {
537 pam_err = pam_close_session(pamh, 0);
538 if (pam_err != PAM_SUCCESS) {
539 syslog(LOG_ERR,
540 "pam_close_session: %s",
541 pam_strerror(pamh, pam_err));
542 warnx("pam_close_session: %s",
543 pam_strerror(pamh, pam_err));
544 }
545 pam_end(pamh, pam_err);
546 if (xpid != -1)
547 warnx("wrong PID: %d != %d", pid, xpid);
548 else
549 warn("wait for pid %d failed", pid);
550 exit(1);
551 }
552
553 (void)signal(SIGINT, oint);
554 (void)signal(SIGABRT, oabrt);
555 if ((pam_err = pam_close_session(pamh, 0)) != PAM_SUCCESS) {
556 syslog(LOG_ERR, "pam_close_session: %s",
557 pam_strerror(pamh, pam_err));
558 warnx("pam_close_session: %s",
559 pam_strerror(pamh, pam_err));
560 }
561 pam_end(pamh, PAM_SUCCESS);
562 exit(0);
563 break;
564 }
565
566 /*
567 * The child: starting here, we don't have to care about
568 * handling PAM issues if we exit, the parent will do the
569 * job when we exit.
570 *
571 * Destroy environment unless user has requested its preservation.
572 * Try to preserve TERM anyway.
573 */
574 saved_term = getenv("TERM");
575 if (!pflag) {
576 environ = envinit;
577 if (saved_term)
578 setenv("TERM", saved_term, 0);
579 }
580
581 if (*pwd->pw_shell == '\0')
582 pwd->pw_shell = _PATH_BSHELL;
583
584 shell = login_getcapstr(lc, "shell", pwd->pw_shell, pwd->pw_shell);
585 if (*shell == '\0')
586 shell = pwd->pw_shell;
587
588 if ((pwd->pw_shell = strdup(shell)) == NULL) {
589 syslog(LOG_ERR, "Cannot alloc mem");
590 exit(1);
591 }
592
593 (void)setenv("HOME", pwd->pw_dir, 1);
594 (void)setenv("SHELL", pwd->pw_shell, 1);
595 if (term[0] == '\0') {
596 char *tt = (char *)stypeof(tty);
597
598 if (tt == NULL)
599 tt = login_getcapstr(lc, "term", NULL, NULL);
600
601 /* unknown term -> "su" */
602 (void)strlcpy(term, tt != NULL ? tt : "su", sizeof(term));
603 }
604 (void)setenv("TERM", term, 0);
605 (void)setenv("LOGNAME", pwd->pw_name, 1);
606 (void)setenv("USER", pwd->pw_name, 1);
607
608 /*
609 * Add PAM environement
610 */
611 if ((pamenv = pam_getenvlist(pamh)) != NULL) {
612 char **envitem;
613
614 for (envitem = pamenv; *envitem; envitem++) {
615 putenv(*envitem);
616 free(*envitem);
617 }
618
619 free(pamenv);
620 }
621
622 /* This drops root privs */
623 if (setusercontext(lc, pwd, pwd->pw_uid,
624 (LOGIN_SETALL & ~LOGIN_SETLOGIN)) != 0) {
625 syslog(LOG_ERR, "setusercontext failed");
626 exit(1);
627 }
628
629 if (!quietlog) {
630 char *fname;
631
632 fname = login_getcapstr(lc, "copyright", NULL, NULL);
633 if (fname != NULL && access(fname, F_OK) == 0)
634 motd(fname);
635 else
636 (void)printf("%s", copyrightstr);
637
638 fname = login_getcapstr(lc, "welcome", NULL, NULL);
639 if (fname == NULL || access(fname, F_OK) != 0)
640 fname = _PATH_MOTDFILE;
641 motd(fname);
642
643 (void)snprintf(tbuf,
644 sizeof(tbuf), "%s/%s", _PATH_MAILDIR, pwd->pw_name);
645 if (stat(tbuf, &st) == 0 && st.st_size != 0)
646 (void)printf("You have %smail.\n",
647 (st.st_mtime > st.st_atime) ? "new " : "");
648 }
649
650 login_close(lc);
651
652 (void)signal(SIGALRM, SIG_DFL);
653 (void)signal(SIGQUIT, SIG_DFL);
654 (void)signal(SIGINT, SIG_DFL);
655 (void)signal(SIGTSTP, SIG_IGN);
656
657 tbuf[0] = '-';
658 (void)strlcpy(tbuf + 1, (p = strrchr(pwd->pw_shell, '/')) ?
659 p + 1 : pwd->pw_shell, sizeof(tbuf) - 1);
660
661 execlp(pwd->pw_shell, tbuf, 0);
662 err(1, "%s", pwd->pw_shell);
663 }
664
665 #define NBUFSIZ (MAXLOGNAME + 1)
666
667
668 void
669 getloginname(void)
670 {
671 int ch;
672 char *p;
673 static char nbuf[NBUFSIZ];
674
675 for (;;) {
676 (void)printf("login: ");
677 for (p = nbuf; (ch = getchar()) != '\n'; ) {
678 if (ch == EOF) {
679 badlogin(username);
680 exit(0);
681 }
682 if (p < nbuf + (NBUFSIZ - 1))
683 *p++ = ch;
684 }
685 if (p > nbuf) {
686 if (nbuf[0] == '-')
687 (void)fprintf(stderr,
688 "login names may not start with '-'.\n");
689 else {
690 *p = '\0';
691 username = nbuf;
692 break;
693 }
694 }
695 }
696 }
697
698 int
699 rootterm(char *ttyn)
700 {
701 struct ttyent *t;
702
703 return ((t = getttynam(ttyn)) && t->ty_status & TTY_SECURE);
704 }
705
706 jmp_buf motdinterrupt;
707
708 void
709 motd(char *fname)
710 {
711 int fd, nchars;
712 sig_t oldint;
713 char tbuf[8192];
714
715 if ((fd = open(fname ? fname : _PATH_MOTDFILE, O_RDONLY, 0)) < 0)
716 return;
717 oldint = signal(SIGINT, sigint);
718 if (setjmp(motdinterrupt) == 0)
719 while ((nchars = read(fd, tbuf, sizeof(tbuf))) > 0)
720 (void)write(fileno(stdout), tbuf, nchars);
721 (void)signal(SIGINT, oldint);
722 (void)close(fd);
723 }
724
725 /* ARGSUSED */
726 void
727 sigint(int signo)
728 {
729
730 longjmp(motdinterrupt, 1);
731 }
732
733 /* ARGSUSED */
734 void
735 timedout(int signo)
736 {
737
738 (void)fprintf(stderr, "Login timed out after %d seconds\n", timeout);
739 exit(0);
740 }
741
742 static void
743 update_db(int quietlog)
744 {
745 if (nested != NULL) {
746 if (hostname != NULL)
747 syslog(LOG_NOTICE, "%s to %s on tty %s from %s",
748 nested, pwd->pw_name, tty, hostname);
749 else
750 syslog(LOG_NOTICE, "%s to %s on tty %s", nested,
751 pwd->pw_name, tty);
752
753 return;
754 }
755 if (hostname != NULL && have_ss == 0) {
756 socklen_t len = sizeof(ss);
757 (void)getpeername(STDIN_FILENO, (struct sockaddr *)&ss, &len);
758 }
759 (void)gettimeofday(&now, NULL);
760 }
761
762 void
763 badlogin(char *name)
764 {
765
766 if (failures == 0)
767 return;
768 if (hostname) {
769 syslog(LOG_NOTICE, "%d LOGIN FAILURE%s FROM %s",
770 failures, failures > 1 ? "S" : "", hostname);
771 syslog(LOG_AUTHPRIV|LOG_NOTICE,
772 "%d LOGIN FAILURE%s FROM %s, %s",
773 failures, failures > 1 ? "S" : "", hostname, name);
774 } else {
775 syslog(LOG_NOTICE, "%d LOGIN FAILURE%s ON %s",
776 failures, failures > 1 ? "S" : "", tty);
777 syslog(LOG_AUTHPRIV|LOG_NOTICE,
778 "%d LOGIN FAILURE%s ON %s, %s",
779 failures, failures > 1 ? "S" : "", tty, name);
780 }
781 }
782
783 const char *
784 stypeof(const char *ttyid)
785 {
786 struct ttyent *t;
787
788 return (ttyid && (t = getttynam(ttyid)) ? t->ty_type : NULL);
789 }
790
791 void
792 sleepexit(int eval)
793 {
794
795 (void)sleep(5);
796 exit(eval);
797 }
798
799 void
800 decode_ss(const char *arg)
801 {
802 struct sockaddr_storage *ssp;
803 size_t len = strlen(arg);
804
805 if (len > sizeof(*ssp) * 4 + 1 || len < sizeof(*ssp))
806 errx(1, "Bad argument");
807
808 if ((ssp = malloc(len)) == NULL)
809 err(1, NULL);
810
811 if (strunvis((char *)ssp, arg) != sizeof(*ssp))
812 errx(1, "Decoding error");
813
814 (void)memcpy(&ss, ssp, sizeof(ss));
815 have_ss = 1;
816 }
817
818 void
819 usage(void)
820 {
821 (void)fprintf(stderr,
822 "Usage: %s [-Ffps] [-a address] [-h hostname] [username]\n",
823 getprogname());
824 exit(1);
825 }
826