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 if ((fd = open (FAILLOG_FILE, O_RDWR)) < 0)
62 * The file is indexed by UID value meaning that shared UID's
63 * share failure log records. That's OK since they really
64 * share just about everything else ...
67 lseek (fd, (off_t) (sizeof *fl) * uid, SEEK_SET);
68 if (read (fd, (char *) fl, sizeof *fl) != sizeof *fl)
69 memzero (fl, sizeof *fl);
72 * Update the record. We increment the failure count to log the
73 * latest failure. The only concern here is overflow, and we'll
74 * check for that. The line name and time of day are both
78 if (fl->fail_cnt + 1 > 0)
81 strncpy (fl->fail_line, tty, sizeof fl->fail_line);
82 time (&fl->fail_time);
85 * Seek back to the correct position in the file and write the
86 * record out. Ideally we should lock the file in case the same
87 * account is being logged simultaneously. But the risk doesn't
91 lseek (fd, (off_t) (sizeof *fl) * uid, SEEK_SET);
92 /* TODO: check failures */
93 write (fd, (char *) fl, sizeof *fl);
94 /* TODO: log failures */
96 /* TODO: log failures */
99 static bool too_many_failures (const struct faillog *fl)
103 if (fl->fail_max == 0 || fl->fail_cnt < fl->fail_max)
106 if (fl->fail_locktime == 0)
107 return true; /* locked until reset manually */
110 if (fl->fail_time + fl->fail_locktime < now)
111 return false; /* enough time since last failure */
117 * failcheck - check for failures > allowable
119 * failcheck() is called AFTER the password has been validated. If the
120 * account has been "attacked" with too many login failures, failcheck()
121 * returns 0 to indicate that the login should be denied even though
122 * the password is valid.
124 * failed indicates if the login failed AFTER the password has been
128 int failcheck (uid_t uid, struct faillog *fl, bool failed)
134 * Suppress the check if the log file isn't there.
137 /* TODO: check if the file exists */
138 fd = open (FAILLOG_FILE, O_RDWR);
144 * Get the record from the file and determine if the user has
145 * exceeded the failure limit. If "max" is zero, any number
146 * of failures are permitted. Only when "max" is non-zero and
147 * "cnt" is greater than or equal to "max" is the account
148 * considered to be locked.
150 * If read fails, there is no record for this user yet (the
151 * file is initially zero length and extended by writes), so
152 * no need to reset the count.
155 lseek (fd, (off_t) (sizeof *fl) * uid, SEEK_SET);
156 if (read (fd, (char *) fl, sizeof *fl) != sizeof *fl) {
161 if (too_many_failures (fl)) {
167 * The record is updated if this is not a failure. The count will
168 * be reset to zero, but the rest of the information will be left
169 * in the record in case someone wants to see where the failed
177 lseek (fd, (off_t) sizeof fail * uid, SEEK_SET);
178 write (fd, (char *) &fail, sizeof fail);
185 * failprint - print line of failure information
187 * failprint takes a (struct faillog) entry and formats it into a
188 * message which is displayed at login time.
191 void failprint (const struct faillog *fail)
197 char *lasttime = lasttimeb;
203 if (fail->fail_cnt == 0)
206 tp = localtime (&(fail->fail_time));
211 * Print all information we have.
213 strftime (lasttimeb, sizeof lasttimeb, "%c", tp);
217 * Do the same thing, but don't use strftime since it
218 * probably doesn't exist on this system
220 lasttime = asctime (tp);
223 if (NOW - fail->fail_time < YEAR)
225 if (NOW - fail->fail_time < DAY)
226 lasttime = lasttime + 11;
228 if (*lasttime == ' ')
231 printf (ngettext ("%d failure since last login.\n"
232 "Last was %s on %s.\n",
233 "%d failures since last login.\n"
234 "Last was %s on %s.\n",
236 fail->fail_cnt, lasttime, fail->fail_line);
240 * failtmp - update the cummulative failure log
242 * failtmp updates the (struct utmp) formatted failure log which
243 * maintains a record of all login failures.
248 const struct utmpx *failent
250 const struct utmp *failent
258 * Get the name of the failure file. If no file has been defined
259 * in login.defs, don't do this.
262 ftmp = getdef_str ("FTMP_FILE");
268 * Open the file for append. It must already exist for this
269 * feature to be used.
272 fd = open (ftmp, O_WRONLY | O_APPEND);
278 * Output the new failure record and close the log file.
281 write (fd, (const char *) failent, sizeof *failent);
283 /* TODO: check if the file could be closed */