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