login_pam.c revision 1.2 1 /* $NetBSD: login_pam.c,v 1.2 2005/02/01 17:57:16 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.2 2005/02/01 17:57:16 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 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 if (have_ss)
303 PAM_SET_ITEM(PAM_SOCKADDR, &ss);
304
305 pwd = getpwnam(username);
306
307 /*
308 * Establish the class now, before we might goto
309 * within the next block. pwd can be NULL since it
310 * falls back to the "default" class if it is.
311 */
312 lc = login_getclass(pwd ? pwd->pw_class : NULL);
313
314 /*
315 * if we have a valid account name, and it doesn't have a
316 * password, or the -f option was specified and the caller
317 * is root or the caller isn't changing their uid, don't
318 * authenticate.
319 */
320 if (pwd) {
321 if (pwd->pw_uid == 0)
322 rootlogin = 1;
323
324 if (fflag && (uid == 0 || uid == pwd->pw_uid)) {
325 /* already authenticated */
326 auth_passed = 1;
327 goto skip_auth;
328 }
329 }
330
331 (void)setpriority(PRIO_PROCESS, 0, -4);
332
333 switch(pam_err = pam_authenticate(pamh, pam_silent)) {
334 case PAM_SUCCESS:
335 /*
336 * PAM can change the user, refresh
337 * username, pwd, and lc.
338 */
339 pam_err = pam_get_item(pamh, PAM_USER, &newuser);
340 if (pam_err != PAM_SUCCESS)
341 PAM_END("pam_get_item(PAM_USER)");
342
343 username = (char *)newuser;
344 pwd = getpwnam(username);
345 lc = login_getpwclass(pwd);
346 auth_passed = 1;
347
348 switch (pam_err = pam_acct_mgmt(pamh, pam_silent)) {
349 case PAM_SUCCESS:
350 break;
351
352 case PAM_NEW_AUTHTOK_REQD:
353 pam_err = pam_chauthtok(pamh,
354 pam_silent|PAM_CHANGE_EXPIRED_AUTHTOK);
355
356 if (pam_err != PAM_SUCCESS)
357 PAM_END("pam_chauthtok");
358 break;
359
360 default:
361 PAM_END("pam_acct_mgmt");
362 break;
363 }
364 break;
365
366 case PAM_AUTH_ERR:
367 case PAM_USER_UNKNOWN:
368 case PAM_MAXTRIES:
369 auth_passed = 0;
370 break;
371
372 default:
373 PAM_END("pam_authenticate");
374 break;
375 }
376
377 (void)setpriority(PRIO_PROCESS, 0, 0);
378
379 skip_auth:
380 /*
381 * If the user exists and authentication passed,
382 * get out of the loop and login the user.
383 */
384 if (pwd && auth_passed)
385 break;
386
387 (void)printf("Login incorrect\n");
388 failures++;
389 cnt++;
390 /* we allow 10 tries, but after 3 we start backing off */
391 if (cnt > login_backoff) {
392 if (cnt >= login_retries) {
393 badlogin(username);
394 pam_end(pamh, PAM_SUCCESS);
395 sleepexit(1);
396 }
397 sleep((u_int)((cnt - 3) * 5));
398 }
399 }
400
401 /* committed to login -- turn off timeout */
402 (void)alarm((u_int)0);
403
404 endpwent();
405
406 quietlog = login_getcapbool(lc, "hushlogin", 0);
407
408 /*
409 * Temporarily give up special privileges so we can change
410 * into NFS-mounted homes that are exported for non-root
411 * access and have mode 7x0
412 */
413 saved_uid = geteuid();
414 saved_gid = getegid();
415 nsaved_gids = getgroups(NGROUPS_MAX, saved_gids);
416
417 (void)setegid(pwd->pw_gid);
418 initgroups(username, pwd->pw_gid);
419 (void)seteuid(pwd->pw_uid);
420
421 if (chdir(pwd->pw_dir) != 0) {
422 if (login_getcapbool(lc, "requirehome", 0)) {
423 (void)printf("Home directory %s required\n",
424 pwd->pw_dir);
425 pam_end(pamh, PAM_SUCCESS);
426 exit(1);
427 }
428
429 (void)printf("No home directory %s!\n", pwd->pw_dir);
430 if (chdir("/")) {
431 pam_end(pamh, PAM_SUCCESS);
432 exit(0);
433 }
434 pwd->pw_dir = "/";
435 (void)printf("Logging in with home = \"/\".\n");
436 }
437
438 if (!quietlog) {
439 quietlog = access(_PATH_HUSHLOGIN, F_OK) == 0;
440 pam_silent = 0;
441 }
442
443 /* regain special privileges */
444 setegid(saved_gid);
445 setgroups(nsaved_gids, saved_gids);
446 seteuid(saved_uid);
447
448 /* Nothing else left to fail -- really log in. */
449 update_db(quietlog);
450
451 if (nested == NULL && setusercontext(lc, pwd, pwd->pw_uid,
452 LOGIN_SETLOGIN) != 0) {
453 syslog(LOG_ERR, "setusercontext failed");
454 pam_end(pamh, PAM_SUCCESS);
455 exit(1);
456 }
457
458 if (tty[sizeof("tty")-1] == 'd')
459 syslog(LOG_INFO, "DIALUP %s, %s", tty, pwd->pw_name);
460
461 /* If fflag is on, assume caller/authenticator has logged root login. */
462 if (rootlogin && fflag == 0) {
463 if (hostname)
464 syslog(LOG_NOTICE, "ROOT LOGIN (%s) ON %s FROM %s",
465 username, tty, hostname);
466 else
467 syslog(LOG_NOTICE, "ROOT LOGIN (%s) ON %s",
468 username, tty);
469 }
470
471 /*
472 * Establish groups
473 */
474 if (setusercontext(lc, pwd, pwd->pw_uid, LOGIN_SETGROUP) != 0) {
475 syslog(LOG_ERR, "setusercontext failed");
476 pam_end(pamh, PAM_SUCCESS);
477 exit(1);
478 }
479
480 pam_err = pam_setcred(pamh, pam_silent|PAM_ESTABLISH_CRED);
481 if (pam_err != PAM_SUCCESS)
482 PAM_END("pam_setcred");
483
484 pam_err = pam_open_session(pamh, pam_silent);
485 if (pam_err != PAM_SUCCESS)
486 PAM_END("pam_open_session");
487
488 /*
489 * Fork because we need to call pam_closesession as root.
490 * Make sure signals cannot kill the parent.
491 * This is copied from crontab(8), which has to
492 * cope with a similar situation.
493 */
494 oint = signal(SIGINT, SIG_IGN);
495 oabrt = signal(SIGABRT, SIG_IGN);
496
497 switch(pid = fork()) {
498 case -1:
499 pam_err = pam_close_session(pamh, 0);
500 if (pam_err != PAM_SUCCESS) {
501 syslog(LOG_ERR, "pam_close_session: %s",
502 pam_strerror(pamh, pam_err));
503 warnx("pam_close_session: %s",
504 pam_strerror(pamh, pam_err));
505 }
506 syslog(LOG_ERR, "fork failed: %m");
507 warn("fork failed");
508 pam_end(pamh, pam_err);
509 exit(1);
510 break;
511
512 case 0: /* Child */
513 break;
514
515 default:
516 /*
517 * Parent: wait for the child to terminate
518 * and call pam_close_session.
519 */
520 if ((xpid = wait(&status)) != pid) {
521 pam_err = pam_close_session(pamh, 0);
522 if (pam_err != PAM_SUCCESS) {
523 syslog(LOG_ERR,
524 "pam_close_session: %s",
525 pam_strerror(pamh, pam_err));
526 warnx("pam_close_session: %s",
527 pam_strerror(pamh, pam_err));
528 }
529 pam_end(pamh, pam_err);
530 warnx("wrong PID: %d != %d", pid, xpid);
531 exit(1);
532 }
533
534 (void)signal(SIGINT, oint);
535 (void)signal(SIGABRT, oabrt);
536 if ((pam_err = pam_close_session(pamh, 0)) != PAM_SUCCESS) {
537 syslog(LOG_ERR, "pam_close_session: %s",
538 pam_strerror(pamh, pam_err));
539 warnx("pam_close_session: %s",
540 pam_strerror(pamh, pam_err));
541 }
542 pam_end(pamh, PAM_SUCCESS);
543 exit(0);
544 break;
545 }
546
547 /*
548 * The child: starting here, we don't have to care about
549 * handling PAM issues if we exit, the parent will do the
550 * job when we exit.
551 *
552 * Destroy environment unless user has requested its preservation.
553 * Try to preserve TERM anyway.
554 */
555 saved_term = getenv("TERM");
556 if (!pflag) {
557 environ = envinit;
558 if (saved_term)
559 setenv("TERM", saved_term, 0);
560 }
561
562 if (*pwd->pw_shell == '\0')
563 pwd->pw_shell = _PATH_BSHELL;
564
565 shell = login_getcapstr(lc, "shell", pwd->pw_shell, pwd->pw_shell);
566 if (*shell == '\0')
567 shell = pwd->pw_shell;
568
569 if ((pwd->pw_shell = strdup(shell)) == NULL) {
570 syslog(LOG_ERR, "Cannot alloc mem");
571 exit(1);
572 }
573
574 (void)setenv("HOME", pwd->pw_dir, 1);
575 (void)setenv("SHELL", pwd->pw_shell, 1);
576 if (term[0] == '\0') {
577 char *tt = (char *)stypeof(tty);
578
579 if (tt == NULL)
580 tt = login_getcapstr(lc, "term", NULL, NULL);
581
582 /* unknown term -> "su" */
583 (void)strlcpy(term, tt != NULL ? tt : "su", sizeof(term));
584 }
585 (void)setenv("TERM", term, 0);
586 (void)setenv("LOGNAME", pwd->pw_name, 1);
587 (void)setenv("USER", pwd->pw_name, 1);
588
589 /*
590 * Add PAM environement
591 */
592 if ((pamenv = pam_getenvlist(pamh)) != NULL) {
593 char **envitem;
594
595 for (envitem = pamenv; *envitem; envitem++) {
596 putenv(*envitem);
597 free(*envitem);
598 }
599
600 free(pamenv);
601 }
602
603 /* This drops root privs */
604 if (setusercontext(lc, pwd, pwd->pw_uid,
605 (LOGIN_SETALL & ~LOGIN_SETLOGIN)) != 0) {
606 syslog(LOG_ERR, "setusercontext failed");
607 exit(1);
608 }
609
610 if (!quietlog) {
611 char *fname;
612
613 fname = login_getcapstr(lc, "copyright", NULL, NULL);
614 if (fname != NULL && access(fname, F_OK) == 0)
615 motd(fname);
616 else
617 (void)printf("%s", copyrightstr);
618
619 fname = login_getcapstr(lc, "welcome", NULL, NULL);
620 if (fname == NULL || access(fname, F_OK) != 0)
621 fname = _PATH_MOTDFILE;
622 motd(fname);
623
624 (void)snprintf(tbuf,
625 sizeof(tbuf), "%s/%s", _PATH_MAILDIR, pwd->pw_name);
626 if (stat(tbuf, &st) == 0 && st.st_size != 0)
627 (void)printf("You have %smail.\n",
628 (st.st_mtime > st.st_atime) ? "new " : "");
629 }
630
631 login_close(lc);
632
633 (void)signal(SIGALRM, SIG_DFL);
634 (void)signal(SIGQUIT, SIG_DFL);
635 (void)signal(SIGINT, SIG_DFL);
636 (void)signal(SIGTSTP, SIG_IGN);
637
638 tbuf[0] = '-';
639 (void)strlcpy(tbuf + 1, (p = strrchr(pwd->pw_shell, '/')) ?
640 p + 1 : pwd->pw_shell, sizeof(tbuf) - 1);
641
642 execlp(pwd->pw_shell, tbuf, 0);
643 err(1, "%s", pwd->pw_shell);
644 }
645
646 #define NBUFSIZ (MAXLOGNAME + 1)
647
648
649 void
650 getloginname(void)
651 {
652 int ch;
653 char *p;
654 static char nbuf[NBUFSIZ];
655
656 for (;;) {
657 (void)printf("login: ");
658 for (p = nbuf; (ch = getchar()) != '\n'; ) {
659 if (ch == EOF) {
660 badlogin(username);
661 exit(0);
662 }
663 if (p < nbuf + (NBUFSIZ - 1))
664 *p++ = ch;
665 }
666 if (p > nbuf) {
667 if (nbuf[0] == '-')
668 (void)fprintf(stderr,
669 "login names may not start with '-'.\n");
670 else {
671 *p = '\0';
672 username = nbuf;
673 break;
674 }
675 }
676 }
677 }
678
679 int
680 rootterm(char *ttyn)
681 {
682 struct ttyent *t;
683
684 return ((t = getttynam(ttyn)) && t->ty_status & TTY_SECURE);
685 }
686
687 jmp_buf motdinterrupt;
688
689 void
690 motd(char *fname)
691 {
692 int fd, nchars;
693 sig_t oldint;
694 char tbuf[8192];
695
696 if ((fd = open(fname ? fname : _PATH_MOTDFILE, O_RDONLY, 0)) < 0)
697 return;
698 oldint = signal(SIGINT, sigint);
699 if (setjmp(motdinterrupt) == 0)
700 while ((nchars = read(fd, tbuf, sizeof(tbuf))) > 0)
701 (void)write(fileno(stdout), tbuf, nchars);
702 (void)signal(SIGINT, oldint);
703 (void)close(fd);
704 }
705
706 /* ARGSUSED */
707 void
708 sigint(int signo)
709 {
710
711 longjmp(motdinterrupt, 1);
712 }
713
714 /* ARGSUSED */
715 void
716 timedout(int signo)
717 {
718
719 (void)fprintf(stderr, "Login timed out after %d seconds\n", timeout);
720 exit(0);
721 }
722
723 static void
724 update_db(int quietlog)
725 {
726 if (nested != NULL) {
727 if (hostname != NULL)
728 syslog(LOG_NOTICE, "%s to %s on tty %s from %s",
729 nested, pwd->pw_name, tty, hostname);
730 else
731 syslog(LOG_NOTICE, "%s to %s on tty %s", nested,
732 pwd->pw_name, tty);
733
734 return;
735 }
736 if (hostname != NULL && have_ss == 0) {
737 socklen_t len = sizeof(ss);
738 (void)getpeername(STDIN_FILENO, (struct sockaddr *)&ss, &len);
739 }
740 (void)gettimeofday(&now, NULL);
741 }
742
743 void
744 badlogin(char *name)
745 {
746
747 if (failures == 0)
748 return;
749 if (hostname) {
750 syslog(LOG_NOTICE, "%d LOGIN FAILURE%s FROM %s",
751 failures, failures > 1 ? "S" : "", hostname);
752 syslog(LOG_AUTHPRIV|LOG_NOTICE,
753 "%d LOGIN FAILURE%s FROM %s, %s",
754 failures, failures > 1 ? "S" : "", hostname, name);
755 } else {
756 syslog(LOG_NOTICE, "%d LOGIN FAILURE%s ON %s",
757 failures, failures > 1 ? "S" : "", tty);
758 syslog(LOG_AUTHPRIV|LOG_NOTICE,
759 "%d LOGIN FAILURE%s ON %s, %s",
760 failures, failures > 1 ? "S" : "", tty, name);
761 }
762 }
763
764 const char *
765 stypeof(const char *ttyid)
766 {
767 struct ttyent *t;
768
769 return (ttyid && (t = getttynam(ttyid)) ? t->ty_type : NULL);
770 }
771
772 void
773 sleepexit(int eval)
774 {
775
776 (void)sleep(5);
777 exit(eval);
778 }
779
780 void
781 decode_ss(const char *arg)
782 {
783 struct sockaddr_storage *ssp;
784 size_t len = strlen(arg);
785
786 if (len > sizeof(*ssp) * 4 + 1 || len < sizeof(*ssp))
787 errx(1, "Bad argument");
788
789 if ((ssp = malloc(len)) == NULL)
790 err(1, NULL);
791
792 if (strunvis((char *)ssp, arg) != sizeof(*ssp))
793 errx(1, "Decoding error");
794
795 (void)memcpy(&ss, ssp, sizeof(ss));
796 have_ss = 1;
797 }
798
799 void
800 usage(void)
801 {
802 (void)fprintf(stderr,
803 "Usage: %s [-Ffps] [-a address] [-h hostname] [username]\n",
804 getprogname());
805 exit(1);
806 }
807