]> granicus.if.org Git - shadow/blob - libmisc/failure.c
* libmisc/getlong.c: Reset errno before calling strtol().
[shadow] / libmisc / failure.c
1 /*
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
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
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.
19  *
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.
31  */
32
33 #include <config.h>
34
35 #ident "$Id$"
36
37 #include <fcntl.h>
38 #include <stdio.h>
39 #include "defines.h"
40 #include "faillog.h"
41 #include "getdef.h"
42 #include "failure.h"
43 #define YEAR    (365L*DAY)
44 /*
45  * failure - make failure entry
46  *
47  *      failure() creates a new (struct faillog) entry or updates an
48  *      existing one with the current failed login information.
49  */
50 void failure (uid_t uid, const char *tty, struct faillog *fl)
51 {
52         int fd;
53
54         /*
55          * Don't do anything if failure logging isn't set up.
56          */
57         /* TODO: check if the file exists */
58         fd = open (FAILLOG_FILE, O_RDWR);
59         if (fd < 0) {
60                 return;
61         }
62
63         /*
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 ...
67          */
68
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);
73         }
74
75         /*
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
79          * updated as well.
80          */
81
82         if (fl->fail_cnt + 1 > 0) {
83                 fl->fail_cnt++;
84         }
85
86         strncpy (fl->fail_line, tty, sizeof fl->fail_line);
87         (void) time (&fl->fail_time);
88
89         /*
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
93          * seem that great.
94          */
95
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 */
100         close (fd);
101         /* TODO: log failures */
102 }
103
104 static bool too_many_failures (const struct faillog *fl)
105 {
106         time_t now;
107
108         if ((0 == fl->fail_max) || (fl->fail_cnt < fl->fail_max)) {
109                 return false;
110         }
111
112         if (0 == fl->fail_locktime) {
113                 return true;    /* locked until reset manually */
114         }
115
116         (void) time (&now);
117         if ((fl->fail_time + fl->fail_locktime) < now) {
118                 return false;   /* enough time since last failure */
119         }
120
121         return true;
122 }
123
124 /*
125  * failcheck - check for failures > allowable
126  *
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.
131  *
132  *      failed indicates if the login failed AFTER the password has been
133  *             validated.
134  */
135
136 int failcheck (uid_t uid, struct faillog *fl, bool failed)
137 {
138         int fd;
139         struct faillog fail;
140
141         /*
142          * Suppress the check if the log file isn't there.
143          */
144
145         /* TODO: check if the file exists */
146         fd = open (FAILLOG_FILE, O_RDWR);
147         if (fd < 0) {
148                 return 1;
149         }
150
151         /*
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.
157          *
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.
161          */
162
163         lseek (fd, (off_t) (sizeof *fl) * uid, SEEK_SET);
164         if (read (fd, (char *) fl, sizeof *fl) != (ssize_t) sizeof *fl) {
165                 close (fd);
166                 return 1;
167         }
168
169         if (too_many_failures (fl)) {
170                 close (fd);
171                 return 0;
172         }
173
174         /*
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
178          * login originated.
179          */
180
181         if (!failed) {
182                 fail = *fl;
183                 fail.fail_cnt = 0;
184
185                 lseek (fd, (off_t) sizeof fail * uid, SEEK_SET);
186                 write (fd, (char *) &fail, sizeof fail);
187         }
188         close (fd);
189         return 1;
190 }
191
192 /*
193  * failprint - print line of failure information
194  *
195  *      failprint takes a (struct faillog) entry and formats it into a
196  *      message which is displayed at login time.
197  */
198
199 void failprint (const struct faillog *fail)
200 {
201         struct tm *tp;
202
203 #if HAVE_STRFTIME
204         char lasttimeb[256];
205         char *lasttime = lasttimeb;
206 #else
207         char *lasttime;
208 #endif
209         time_t NOW;
210
211         if (0 == fail->fail_cnt) {
212                 return;
213         }
214
215         tp = localtime (&(fail->fail_time));
216         (void) time (&NOW);
217
218 #if HAVE_STRFTIME
219         /*
220          * Print all information we have.
221          */
222         strftime (lasttimeb, sizeof lasttimeb, "%c", tp);
223 #else
224
225         /*
226          * Do the same thing, but don't use strftime since it
227          * probably doesn't exist on this system
228          */
229         lasttime = asctime (tp);
230         lasttime[24] = '\0';
231
232         if ((NOW - fail->fail_time) < YEAR) {
233                 lasttime[19] = '\0';
234         }
235         if ((NOW - fail->fail_time) < DAY) {
236                 lasttime = lasttime + 11;
237         }
238
239         if (' ' == *lasttime) {
240                 lasttime++;
241         }
242 #endif
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",
247                           fail->fail_cnt),
248                 fail->fail_cnt, lasttime, fail->fail_line);
249 }
250
251 /*
252  * failtmp - update the cummulative failure log
253  *
254  *      failtmp updates the (struct utmp) formatted failure log which
255  *      maintains a record of all login failures.
256  */
257
258 void failtmp (
259 #ifdef HAVE_UTMPX_H
260                      const struct utmpx *failent
261 #else
262                      const struct utmp *failent
263 #endif
264     )
265 {
266         char *ftmp;
267         int fd;
268
269         /*
270          * Get the name of the failure file.  If no file has been defined
271          * in login.defs, don't do this.
272          */
273
274         ftmp = getdef_str ("FTMP_FILE");
275         if (NULL == ftmp) {
276                 return;
277         }
278
279         /*
280          * Open the file for append.  It must already exist for this
281          * feature to be used.
282          */
283
284         fd = open (ftmp, O_WRONLY | O_APPEND);
285         if (-1 == fd) {
286                 return;
287         }
288
289         /*
290          * Output the new failure record and close the log file.
291          */
292
293         write (fd, (const char *) failent, sizeof *failent);
294         close (fd);
295         /* TODO: check if the file could be closed */
296 }
297