2 * Copyright (c) 1989 - 1994, Julianne Frances Haugh
3 * Copyright (c) 1996 - 1998, Marek Michałkiewicz
4 * Copyright (c) 2002 - 2005, Tomasz Kłoczko
5 * Copyright (c) 2008 , Nicolas François
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the copyright holders or contributors may not be used to
17 * endorse or promote products derived from this software without
18 * specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43 #define YEAR (365L*DAY)
45 * failure - make failure entry
47 * failure() creates a new (struct faillog) entry or updates an
48 * existing one with the current failed login information.
50 void failure (uid_t uid, const char *tty, struct faillog *fl)
55 * Don't do anything if failure logging isn't set up.
57 /* TODO: check if the file exists */
58 fd = open (FAILLOG_FILE, O_RDWR);
64 * The file is indexed by UID value meaning that shared UID's
65 * share failure log records. That's OK since they really
66 * share just about everything else ...
69 lseek (fd, (off_t) (sizeof *fl) * uid, SEEK_SET);
70 /* TODO: check failures */
71 if (read (fd, (char *) fl, sizeof *fl) != (ssize_t) sizeof *fl) {
72 memzero (fl, sizeof *fl);
76 * Update the record. We increment the failure count to log the
77 * latest failure. The only concern here is overflow, and we'll
78 * check for that. The line name and time of day are both
82 if (fl->fail_cnt + 1 > 0) {
86 strncpy (fl->fail_line, tty, sizeof fl->fail_line);
87 (void) time (&fl->fail_time);
90 * Seek back to the correct position in the file and write the
91 * record out. Ideally we should lock the file in case the same
92 * account is being logged simultaneously. But the risk doesn't
96 lseek (fd, (off_t) (sizeof *fl) * uid, SEEK_SET);
97 /* TODO: check failures */
98 write (fd, (char *) fl, sizeof *fl);
99 /* TODO: log failures */
101 /* TODO: log failures */
104 static bool too_many_failures (const struct faillog *fl)
108 if ((0 == fl->fail_max) || (fl->fail_cnt < fl->fail_max)) {
112 if (0 == fl->fail_locktime) {
113 return true; /* locked until reset manually */
117 if ((fl->fail_time + fl->fail_locktime) < now) {
118 return false; /* enough time since last failure */
125 * failcheck - check for failures > allowable
127 * failcheck() is called AFTER the password has been validated. If the
128 * account has been "attacked" with too many login failures, failcheck()
129 * returns 0 to indicate that the login should be denied even though
130 * the password is valid.
132 * failed indicates if the login failed AFTER the password has been
136 int failcheck (uid_t uid, struct faillog *fl, bool failed)
142 * Suppress the check if the log file isn't there.
145 /* TODO: check if the file exists */
146 fd = open (FAILLOG_FILE, O_RDWR);
152 * Get the record from the file and determine if the user has
153 * exceeded the failure limit. If "max" is zero, any number
154 * of failures are permitted. Only when "max" is non-zero and
155 * "cnt" is greater than or equal to "max" is the account
156 * considered to be locked.
158 * If read fails, there is no record for this user yet (the
159 * file is initially zero length and extended by writes), so
160 * no need to reset the count.
163 lseek (fd, (off_t) (sizeof *fl) * uid, SEEK_SET);
164 if (read (fd, (char *) fl, sizeof *fl) != (ssize_t) sizeof *fl) {
169 if (too_many_failures (fl)) {
175 * The record is updated if this is not a failure. The count will
176 * be reset to zero, but the rest of the information will be left
177 * in the record in case someone wants to see where the failed
185 lseek (fd, (off_t) sizeof fail * uid, SEEK_SET);
186 write (fd, (char *) &fail, sizeof fail);
193 * failprint - print line of failure information
195 * failprint takes a (struct faillog) entry and formats it into a
196 * message which is displayed at login time.
199 void failprint (const struct faillog *fail)
205 char *lasttime = lasttimeb;
211 if (0 == fail->fail_cnt) {
215 tp = localtime (&(fail->fail_time));
220 * Print all information we have.
222 strftime (lasttimeb, sizeof lasttimeb, "%c", tp);
226 * Do the same thing, but don't use strftime since it
227 * probably doesn't exist on this system
229 lasttime = asctime (tp);
232 if ((NOW - fail->fail_time) < YEAR) {
235 if ((NOW - fail->fail_time) < DAY) {
236 lasttime = lasttime + 11;
239 if (' ' == *lasttime) {
243 printf (ngettext ("%d failure since last login.\n"
244 "Last was %s on %s.\n",
245 "%d failures since last login.\n"
246 "Last was %s on %s.\n",
248 fail->fail_cnt, lasttime, fail->fail_line);
252 * failtmp - update the cummulative failure log
254 * failtmp updates the (struct utmp) formatted failure log which
255 * maintains a record of all login failures.
260 const struct utmpx *failent
262 const struct utmp *failent
270 * Get the name of the failure file. If no file has been defined
271 * in login.defs, don't do this.
274 ftmp = getdef_str ("FTMP_FILE");
280 * Open the file for append. It must already exist for this
281 * feature to be used.
284 fd = open (ftmp, O_WRONLY | O_APPEND);
290 * Output the new failure record and close the log file.
293 write (fd, (const char *) failent, sizeof *failent);
295 /* TODO: check if the file could be closed */