Annotation of src/usr.bin/login/failedlogin.c, Revision 1.11
1.11 ! millert 1: /* $OpenBSD: failedlogin.c,v 1.10 2001/07/06 18:12:05 pvalchev Exp $ */
1.1 millert 2:
3: /*
4: * Copyright (c) 1996 Todd C. Miller <Todd.Miller@courtesan.com>
5: * 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.
1.4 millert 15: * 3. The name of the author may not be used to endorse or promote products
1.1 millert 16: * derived from this software without specific prior written permission.
17: *
18: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
19: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
20: * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21: * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26: * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28: */
29:
30: #ifndef lint
1.11 ! millert 31: static char rcsid[] = "$OpenBSD: failedlogin.c,v 1.10 2001/07/06 18:12:05 pvalchev Exp $";
1.1 millert 32: #endif /* not lint */
33:
34: /*
35: * failedlogin.c
36: * Log to failedlogin file and read from it, reporting the number of
37: * failed logins since the last good login and when/from where
38: * the last failed login was.
39: */
40:
41: #include <sys/param.h>
42: #include <sys/stat.h>
43: #include <sys/time.h>
44:
1.7 millert 45: #include <fcntl.h>
1.1 millert 46: #include <stdio.h>
47: #include <string.h>
48: #include <unistd.h>
49: #include <utmp.h>
50:
51: #include "pathnames.h"
52:
53: struct badlogin {
54: char bl_line[UT_LINESIZE]; /* tty used */
1.2 millert 55: char bl_name[UT_NAMESIZE]; /* remote username */
1.1 millert 56: char bl_host[UT_HOSTSIZE]; /* remote host */
1.2 millert 57: time_t bl_time; /* time of the login attempt */
58: size_t count; /* number of bad logins */
1.1 millert 59: };
1.10 pvalchev 60:
1.11 ! millert 61: void log_failedlogin(uid_t, char *, char *, char *);
! 62: int check_failedlogin(uid_t);
1.1 millert 63:
64: /*
65: * Log a bad login to the failedlogin file.
66: */
67: void
1.2 millert 68: log_failedlogin(uid, host, name, tty)
1.1 millert 69: uid_t uid;
1.2 millert 70: char *host, *name, *tty;
1.1 millert 71: {
72: struct badlogin failedlogin;
73: int fd;
74:
75: /* Add O_CREAT if you want to create failedlogin if it doesn't exist */
76: if ((fd = open(_PATH_FAILEDLOGIN, O_RDWR, S_IREAD|S_IWRITE)) >= 0) {
1.5 millert 77: (void)lseek(fd, (off_t)uid * sizeof(failedlogin), SEEK_SET);
1.1 millert 78:
79: /* Read in last bad login so can get the count */
80: if (read(fd, (char *)&failedlogin, sizeof(failedlogin)) !=
81: sizeof(failedlogin) || failedlogin.bl_time == 0)
82: memset((void *)&failedlogin, 0, sizeof(failedlogin));
83:
1.5 millert 84: (void)lseek(fd, (off_t)uid * sizeof(failedlogin), SEEK_SET);
1.1 millert 85: /* Increment count of bad logins */
86: ++failedlogin.count;
87: (void)time(&failedlogin.bl_time);
88: strncpy(failedlogin.bl_line, tty, sizeof(failedlogin.bl_line));
89: if (host)
90: strncpy(failedlogin.bl_host, host, sizeof(failedlogin.bl_host));
91: else
92: *failedlogin.bl_host = '\0'; /* NULL host field */
1.2 millert 93: if (name)
94: strncpy(failedlogin.bl_name, name, sizeof(failedlogin.bl_name));
95: else
96: *failedlogin.bl_name = '\0'; /* NULL name field */
1.1 millert 97: (void)write(fd, (char *)&failedlogin, sizeof(failedlogin));
98: (void)close(fd);
99: }
100: }
101:
102: /*
103: * Check the failedlogin file and report about the number of unsuccessful
104: * logins and info about the last one in lastlogin style.
105: * NOTE: zeros the count field since this is assumed to be called after the
106: * user has been validated.
107: */
108: int
109: check_failedlogin(uid)
110: uid_t uid;
111: {
112: int fd;
113: struct badlogin failedlogin;
114: int was_bad = 0;
115:
116: (void)memset((void *)&failedlogin, 0, sizeof(failedlogin));
117:
118: if ((fd = open(_PATH_FAILEDLOGIN, O_RDWR, 0)) >= 0) {
1.5 millert 119: (void)lseek(fd, (off_t)uid * sizeof(failedlogin), SEEK_SET);
1.1 millert 120: if (read(fd, (char *)&failedlogin, sizeof(failedlogin)) ==
121: sizeof(failedlogin) && failedlogin.count > 0 ) {
122: /* There was a bad login */
123: was_bad = 1;
124: if (failedlogin.count > 1)
1.9 millert 125: (void)printf("There have been %lu unsuccessful "
126: "login attempts to your account.\n",
127: (u_long)failedlogin.count);
1.6 pjanzen 128: (void)printf("Last unsuccessful login: %.*s", 24-5,
1.1 millert 129: (char *)ctime(&failedlogin.bl_time));
130: (void)printf(" on %.*s",
131: (int)sizeof(failedlogin.bl_line),
132: failedlogin.bl_line);
1.3 art 133: if (*failedlogin.bl_host != '\0') {
1.2 millert 134: if (*failedlogin.bl_name != '\0')
135: (void)printf(" from %.*s@%.*s",
136: (int)sizeof(failedlogin.bl_name),
137: failedlogin.bl_name,
138: (int)sizeof(failedlogin.bl_host),
139: failedlogin.bl_host);
140: else
141: (void)printf(" from %.*s",
142: (int)sizeof(failedlogin.bl_host),
143: failedlogin.bl_host);
1.3 art 144: }
1.1 millert 145: (void)putchar('\n');
146:
147: /* Reset since this is a good login and write record */
148: failedlogin.count = 0;
1.5 millert 149: (void)lseek(fd, (off_t)uid * sizeof(failedlogin),
150: SEEK_SET);
151: (void)write(fd, (char *)&failedlogin,
152: sizeof(failedlogin));
1.1 millert 153: }
154: (void)close(fd);
155: }
156: return(was_bad);
157: }