]> granicus.if.org Git - shadow/blob - libmisc/failure.c
Updated copyright dates.
[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         if ((fd = open (FAILLOG_FILE, O_RDWR)) < 0)
59                 return;
60
61         /*
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 ...
65          */
66
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);
70
71         /*
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
75          * updated as well.
76          */
77
78         if (fl->fail_cnt + 1 > 0)
79                 fl->fail_cnt++;
80
81         strncpy (fl->fail_line, tty, sizeof fl->fail_line);
82         time (&fl->fail_time);
83
84         /*
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
88          * seem that great.
89          */
90
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 */
95         close (fd);
96         /* TODO: log failures */
97 }
98
99 static bool too_many_failures (const struct faillog *fl)
100 {
101         time_t now;
102
103         if (fl->fail_max == 0 || fl->fail_cnt < fl->fail_max)
104                 return false;
105
106         if (fl->fail_locktime == 0)
107                 return true;    /* locked until reset manually */
108
109         time (&now);
110         if (fl->fail_time + fl->fail_locktime < now)
111                 return false;   /* enough time since last failure */
112
113         return true;
114 }
115
116 /*
117  * failcheck - check for failures > allowable
118  *
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.
123  *
124  *      failed indicates if the login failed AFTER the password has been
125  *             validated.
126  */
127
128 int failcheck (uid_t uid, struct faillog *fl, bool failed)
129 {
130         int fd;
131         struct faillog fail;
132
133         /*
134          * Suppress the check if the log file isn't there.
135          */
136
137         /* TODO: check if the file exists */
138         fd = open (FAILLOG_FILE, O_RDWR);
139         if (fd < 0) {
140                 return 1;
141         }
142
143         /*
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.
149          *
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.
153          */
154
155         lseek (fd, (off_t) (sizeof *fl) * uid, SEEK_SET);
156         if (read (fd, (char *) fl, sizeof *fl) != sizeof *fl) {
157                 close (fd);
158                 return 1;
159         }
160
161         if (too_many_failures (fl)) {
162                 close (fd);
163                 return 0;
164         }
165
166         /*
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
170          * login originated.
171          */
172
173         if (!failed) {
174                 fail = *fl;
175                 fail.fail_cnt = 0;
176
177                 lseek (fd, (off_t) sizeof fail * uid, SEEK_SET);
178                 write (fd, (char *) &fail, sizeof fail);
179         }
180         close (fd);
181         return 1;
182 }
183
184 /*
185  * failprint - print line of failure information
186  *
187  *      failprint takes a (struct faillog) entry and formats it into a
188  *      message which is displayed at login time.
189  */
190
191 void failprint (const struct faillog *fail)
192 {
193         struct tm *tp;
194
195 #if HAVE_STRFTIME
196         char lasttimeb[256];
197         char *lasttime = lasttimeb;
198 #else
199         char *lasttime;
200 #endif
201         time_t NOW;
202
203         if (fail->fail_cnt == 0)
204                 return;
205
206         tp = localtime (&(fail->fail_time));
207         time (&NOW);
208
209 #if HAVE_STRFTIME
210         /*
211          * Print all information we have.
212          */
213         strftime (lasttimeb, sizeof lasttimeb, "%c", tp);
214 #else
215
216         /*
217          * Do the same thing, but don't use strftime since it
218          * probably doesn't exist on this system
219          */
220         lasttime = asctime (tp);
221         lasttime[24] = '\0';
222
223         if (NOW - fail->fail_time < YEAR)
224                 lasttime[19] = '\0';
225         if (NOW - fail->fail_time < DAY)
226                 lasttime = lasttime + 11;
227
228         if (*lasttime == ' ')
229                 lasttime++;
230 #endif
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",
235                           fail->fail_cnt),
236                 fail->fail_cnt, lasttime, fail->fail_line);
237 }
238
239 /*
240  * failtmp - update the cummulative failure log
241  *
242  *      failtmp updates the (struct utmp) formatted failure log which
243  *      maintains a record of all login failures.
244  */
245
246 void failtmp (
247 #ifdef HAVE_UTMPX_H
248                      const struct utmpx *failent
249 #else
250                      const struct utmp *failent
251 #endif
252     )
253 {
254         char *ftmp;
255         int fd;
256
257         /*
258          * Get the name of the failure file.  If no file has been defined
259          * in login.defs, don't do this.
260          */
261
262         ftmp = getdef_str ("FTMP_FILE");
263         if (NULL == ftmp) {
264                 return;
265         }
266
267         /*
268          * Open the file for append.  It must already exist for this
269          * feature to be used.
270          */
271
272         fd = open (ftmp, O_WRONLY | O_APPEND);
273         if (-1 == fd) {
274                 return;
275         }
276
277         /*
278          * Output the new failure record and close the log file.
279          */
280
281         write (fd, (const char *) failent, sizeof *failent);
282         close (fd);
283         /* TODO: check if the file could be closed */
284 }
285