Annotation of src/usr.bin/login/failedlogin.c, Revision 1.13
1.13 ! millert 1: /* $OpenBSD: failedlogin.c,v 1.12 2002/07/02 01:15:08 deraadt Exp $ */
1.1 millert 2:
3: /*
4: * Copyright (c) 1996 Todd C. Miller <Todd.Miller@courtesan.com>
5: *
1.13 ! millert 6: * Permission to use, copy, modify, and distribute this software for any
! 7: * purpose with or without fee is hereby granted, provided that the above
! 8: * copyright notice and this permission notice appear in all copies.
1.1 millert 9: *
1.13 ! millert 10: * THE SOFTWARE IS PROVIDED "AS IS" AND TODD C. MILLER DISCLAIMS ALL
! 11: * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
! 12: * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL TODD C. MILLER BE LIABLE
! 13: * FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
! 14: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
! 15: * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
! 16: * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1.1 millert 17: */
18:
1.12 deraadt 19: #ifndef lint
1.13 ! millert 20: static char rcsid[] = "$OpenBSD: failedlogin.c,v 1.12 2002/07/02 01:15:08 deraadt Exp $";
1.12 deraadt 21: #endif /* not lint */
1.1 millert 22:
23: /*
24: * failedlogin.c
25: * Log to failedlogin file and read from it, reporting the number of
26: * failed logins since the last good login and when/from where
27: * the last failed login was.
28: */
29:
30: #include <sys/param.h>
31: #include <sys/stat.h>
32: #include <sys/time.h>
33:
1.7 millert 34: #include <fcntl.h>
1.1 millert 35: #include <stdio.h>
36: #include <string.h>
37: #include <unistd.h>
38: #include <utmp.h>
39:
40: #include "pathnames.h"
41:
42: struct badlogin {
43: char bl_line[UT_LINESIZE]; /* tty used */
1.2 millert 44: char bl_name[UT_NAMESIZE]; /* remote username */
1.1 millert 45: char bl_host[UT_HOSTSIZE]; /* remote host */
1.2 millert 46: time_t bl_time; /* time of the login attempt */
47: size_t count; /* number of bad logins */
1.1 millert 48: };
1.10 pvalchev 49:
1.11 millert 50: void log_failedlogin(uid_t, char *, char *, char *);
51: int check_failedlogin(uid_t);
1.1 millert 52:
53: /*
54: * Log a bad login to the failedlogin file.
55: */
56: void
1.12 deraadt 57: log_failedlogin(uid_t uid, char *host, char *name, char *tty)
1.1 millert 58: {
59: struct badlogin failedlogin;
60: int fd;
61:
62: /* Add O_CREAT if you want to create failedlogin if it doesn't exist */
63: if ((fd = open(_PATH_FAILEDLOGIN, O_RDWR, S_IREAD|S_IWRITE)) >= 0) {
1.5 millert 64: (void)lseek(fd, (off_t)uid * sizeof(failedlogin), SEEK_SET);
1.1 millert 65:
66: /* Read in last bad login so can get the count */
67: if (read(fd, (char *)&failedlogin, sizeof(failedlogin)) !=
68: sizeof(failedlogin) || failedlogin.bl_time == 0)
69: memset((void *)&failedlogin, 0, sizeof(failedlogin));
70:
1.5 millert 71: (void)lseek(fd, (off_t)uid * sizeof(failedlogin), SEEK_SET);
1.1 millert 72: /* Increment count of bad logins */
73: ++failedlogin.count;
74: (void)time(&failedlogin.bl_time);
75: strncpy(failedlogin.bl_line, tty, sizeof(failedlogin.bl_line));
76: if (host)
77: strncpy(failedlogin.bl_host, host, sizeof(failedlogin.bl_host));
78: else
79: *failedlogin.bl_host = '\0'; /* NULL host field */
1.2 millert 80: if (name)
81: strncpy(failedlogin.bl_name, name, sizeof(failedlogin.bl_name));
82: else
83: *failedlogin.bl_name = '\0'; /* NULL name field */
1.1 millert 84: (void)write(fd, (char *)&failedlogin, sizeof(failedlogin));
85: (void)close(fd);
86: }
87: }
88:
89: /*
90: * Check the failedlogin file and report about the number of unsuccessful
91: * logins and info about the last one in lastlogin style.
92: * NOTE: zeros the count field since this is assumed to be called after the
93: * user has been validated.
94: */
95: int
1.12 deraadt 96: check_failedlogin(uid_t uid)
1.1 millert 97: {
98: struct badlogin failedlogin;
1.12 deraadt 99: int fd, was_bad = 0;
1.1 millert 100:
101: (void)memset((void *)&failedlogin, 0, sizeof(failedlogin));
102:
103: if ((fd = open(_PATH_FAILEDLOGIN, O_RDWR, 0)) >= 0) {
1.5 millert 104: (void)lseek(fd, (off_t)uid * sizeof(failedlogin), SEEK_SET);
1.1 millert 105: if (read(fd, (char *)&failedlogin, sizeof(failedlogin)) ==
106: sizeof(failedlogin) && failedlogin.count > 0 ) {
107: /* There was a bad login */
108: was_bad = 1;
109: if (failedlogin.count > 1)
1.9 millert 110: (void)printf("There have been %lu unsuccessful "
111: "login attempts to your account.\n",
112: (u_long)failedlogin.count);
1.6 pjanzen 113: (void)printf("Last unsuccessful login: %.*s", 24-5,
1.1 millert 114: (char *)ctime(&failedlogin.bl_time));
115: (void)printf(" on %.*s",
116: (int)sizeof(failedlogin.bl_line),
1.12 deraadt 117: failedlogin.bl_line);
1.3 art 118: if (*failedlogin.bl_host != '\0') {
1.2 millert 119: if (*failedlogin.bl_name != '\0')
120: (void)printf(" from %.*s@%.*s",
121: (int)sizeof(failedlogin.bl_name),
122: failedlogin.bl_name,
123: (int)sizeof(failedlogin.bl_host),
124: failedlogin.bl_host);
125: else
126: (void)printf(" from %.*s",
127: (int)sizeof(failedlogin.bl_host),
128: failedlogin.bl_host);
1.3 art 129: }
1.1 millert 130: (void)putchar('\n');
131:
132: /* Reset since this is a good login and write record */
133: failedlogin.count = 0;
1.5 millert 134: (void)lseek(fd, (off_t)uid * sizeof(failedlogin),
135: SEEK_SET);
136: (void)write(fd, (char *)&failedlogin,
137: sizeof(failedlogin));
1.1 millert 138: }
139: (void)close(fd);
140: }
141: return(was_bad);
142: }