2 * Copyright (c) 1989 - 1993, Julianne Frances Haugh
3 * Copyright (c) 1996 - 2000, Marek Michałkiewicz
4 * Copyright (c) 2002 - 2006, Tomasz Kłoczko
5 * Copyright (c) 2007 - 2009, 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.
35 #ident "$Id: faillog.c 3015 2009-06-05 22:16:56Z nekral-guest $"
41 #include <sys/types.h>
46 #include "prototypes.h"
48 #include "exitcodes.h"
53 static FILE *fail; /* failure file stream */
54 static time_t seconds; /* that number of days in seconds */
55 static unsigned long umin; /* if uflg and has_umin, only display users with uid >= umin */
56 static bool has_umin = false;
57 static unsigned long umax; /* if uflg and has_umax, only display users with uid <= umax */
58 static bool has_umax = false;
59 static bool errors = false;
61 static bool aflg = false; /* set if all users are to be printed always */
62 static bool uflg = false; /* set if user is a valid user id */
63 static bool tflg = false; /* print is restricted to most recent days */
64 static bool lflg = false; /* set the locktime */
65 static bool mflg = false; /* set maximum failed login counters */
66 static bool rflg = false; /* reset the counters of login failures */
68 static struct stat statbuf; /* fstat buffer for file size */
70 #define NOW (time((time_t *) 0))
72 static void usage (void)
74 (void) fprintf (stderr,
75 _("Usage: %s [options]\n"
79 (void) fputs (_(" -a, --all display faillog records for all users\n"), stderr);
80 (void) fputs (_(" -h, --help display this help message and exit\n"), stderr);
81 (void) fputs (_(" -l, --lock-time SEC after failed login lock account for SEC seconds\n"), stderr);
82 (void) fputs (_(" -m, --maximum MAX set maximum failed login counters to MAX\n"), stderr);
83 (void) fputs (_(" -r, --reset reset the counters of login failures\n"), stderr);
84 (void) fputs (_(" -t, --time DAYS display faillog records more recent than DAYS\n"), stderr);
85 (void) fputs (_(" -u, --user LOGIN/RANGE display faillog record or maintains failure\n"
86 " counters and limits (if used with -r, -m,\n"
87 " or -l) only for the specified LOGIN(s)\n"), stderr);
88 (void) fputs ("\n", stderr);
92 static void print_one (/*@null@*/const struct passwd *pw, bool force)
94 static bool once = false;
109 offset = pw->pw_uid * sizeof (fl);
110 if (offset <= (statbuf.st_size - sizeof (fl))) {
111 /* fseeko errors are not really relevant for us. */
112 int err = fseeko (fail, offset, SEEK_SET);
114 /* faillog is a sparse file. Even if no entries were
115 * entered for this user, which should be able to get the
116 * empty entry in this case.
118 if (fread ((char *) &fl, sizeof (fl), 1, fail) != 1) {
120 _("faillog: Failed to get the entry for UID %lu\n"),
121 (unsigned long int)pw->pw_uid);
125 /* Outsize of the faillog file.
126 * Behave as if there were a missing entry (same behavior
127 * as if we were reading an non existing entry in the
128 * sparse faillog file).
130 memzero (&fl, sizeof (fl));
133 /* Nothing to report */
134 if (!force && (0 == fl.fail_time)) {
140 /* Filter out entries that do not match with the -t option */
141 if (tflg && ((now - fl.fail_time) > seconds)) {
145 /* Print the header only once */
147 puts (_("Login Failures Maximum Latest On\n"));
151 tm = localtime (&fl.fail_time);
153 strftime (ptime, sizeof (ptime), "%D %H:%M:%S %z", tm);
156 printf ("%-9s %5d %5d ",
157 pw->pw_name, fl.fail_cnt, fl.fail_max);
158 /* FIXME: cp is not defined ifndef HAVE_STRFTIME */
159 printf ("%s %s", cp, fl.fail_line);
160 if (0 != fl.fail_locktime) {
161 if ( ((fl.fail_time + fl.fail_locktime) > now)
162 && (0 != fl.fail_cnt)) {
163 printf (_(" [%lus left]"),
164 (unsigned long) fl.fail_time + fl.fail_locktime - now);
166 printf (_(" [%lds lock]"),
173 static void print (void)
175 if (uflg && has_umin && has_umax && (umin==umax)) {
176 print_one (getpwuid ((uid_t)umin), true);
178 /* We only print records for existing users.
179 * Loop based on the user database instead of reading the
180 * whole file. We will have to query the database anyway
181 * so except for very small ranges and large user
182 * database, this should not be a performance issue.
184 struct passwd *pwent;
187 while ( (pwent = getpwent ()) != NULL ) {
189 && ( (has_umin && (pwent->pw_uid < (uid_t)umin))
190 || (has_umax && (pwent->pw_uid > (uid_t)umax)))) {
193 print_one (pwent, aflg);
200 * reset_one - Reset the fail count for one user
202 * This returns a boolean indicating if an error occurred.
204 static bool reset_one (uid_t uid)
209 offset = uid * sizeof (fl);
210 if (offset <= (statbuf.st_size - sizeof (fl))) {
211 /* fseeko errors are not really relevant for us. */
212 int err = fseeko (fail, offset, SEEK_SET);
214 /* faillog is a sparse file. Even if no entries were
215 * entered for this user, which should be able to get the
216 * empty entry in this case.
218 if (fread ((char *) &fl, sizeof (fl), 1, fail) != 1) {
220 _("faillog: Failed to get the entry for UID %lu\n"),
221 (unsigned long int)uid);
225 /* Outsize of the faillog file.
226 * Behave as if there were a missing entry (same behavior
227 * as if we were reading an non existing entry in the
228 * sparse faillog file).
230 memzero (&fl, sizeof (fl));
233 if (0 == fl.fail_cnt) {
234 /* If the count is already null, do not write in the file.
235 * This avoids writing 0 when no entries were present for
243 if ( (fseeko (fail, offset, SEEK_SET) == 0)
244 && (fwrite ((char *) &fl, sizeof (fl), 1, fail) == 1)) {
245 (void) fflush (fail);
250 _("faillog: Failed to reset fail count for UID %lu\n"),
251 (unsigned long int)uid);
255 static void reset (void)
257 if (uflg && has_umin && has_umax && (umin==umax)) {
258 if (reset_one ((uid_t)umin)) {
262 /* Reset all entries in the specified range.
263 * Non existing entries will not be touched.
264 * Entries for non existing users are also reset.
267 uid_t uidmax = statbuf.st_size / sizeof (struct faillog);
269 /* Make sure we stay in the umin-umax range if specified */
273 if (has_umax && (uid_t)umax < uidmax) {
274 uidmax = (uid_t)umax;
277 while (uid < uidmax) {
278 if (reset_one (uid)) {
287 * setmax_one - Set the maximum failed login counter for one user
289 * This returns a boolean indicating if an error occurred.
291 static bool setmax_one (uid_t uid, int max)
296 offset = (off_t) uid * sizeof (fl);
297 if (offset <= (statbuf.st_size - sizeof (fl))) {
298 /* fseeko errors are not really relevant for us. */
299 int err = fseeko (fail, offset, SEEK_SET);
301 /* faillog is a sparse file. Even if no entries were
302 * entered for this user, which should be able to get the
303 * empty entry in this case.
305 if (fread ((char *) &fl, sizeof (fl), 1, fail) != 1) {
307 _("faillog: Failed to get the entry for UID %lu\n"),
308 (unsigned long int)uid);
312 /* Outsize of the faillog file.
313 * Behave as if there were a missing entry (same behavior
314 * as if we were reading an non existing entry in the
315 * sparse faillog file).
317 memzero (&fl, sizeof (fl));
320 if (max == fl.fail_max) {
321 /* If the max is already set to the right value, do not
323 * This avoids writing 0 when no entries were present for
324 * the user and the max argument is 0.
331 if ( (fseeko (fail, offset, SEEK_SET) == 0)
332 && (fwrite ((char *) &fl, sizeof (fl), 1, fail) == 1)) {
333 (void) fflush (fail);
338 _("faillog: Failed to set max for UID %lu\n"),
339 (unsigned long int)uid);
343 static void setmax (int max)
345 if (uflg && has_umin && has_umax && (umin==umax)) {
346 if (setmax_one ((uid_t)umin, max)) {
350 /* Set max for all entries in the specified range.
351 * If max is unchanged for an entry, the entry is not touched.
352 * If max is null, and no entries exist for this user, no
353 * entries will be created.
354 * Entries for non existing user are also taken into
355 * account (in order to define policy for future users).
358 uid_t uidmax = statbuf.st_size / sizeof (struct faillog);
360 /* Make sure we stay in the umin-umax range if specified */
364 if (has_umax && (uid_t)umax < uidmax) {
365 uidmax = (uid_t)umax;
368 while (uid < uidmax) {
369 if (setmax_one (uid, max)) {
378 * set_locktime_one - Set the locktime for one user
380 * This returns a boolean indicating if an error occurred.
382 static bool set_locktime_one (uid_t uid, long locktime)
387 offset = (off_t) uid * sizeof (fl);
388 if (offset <= (statbuf.st_size - sizeof (fl))) {
389 /* fseeko errors are not really relevant for us. */
390 int err = fseeko (fail, offset, SEEK_SET);
392 /* faillog is a sparse file. Even if no entries were
393 * entered for this user, which should be able to get the
394 * empty entry in this case.
396 if (fread ((char *) &fl, sizeof (fl), 1, fail) != 1) {
398 _("faillog: Failed to get the entry for UID %lu\n"),
399 (unsigned long int)uid);
403 /* Outsize of the faillog file.
404 * Behave as if there were a missing entry (same behavior
405 * as if we were reading an non existing entry in the
406 * sparse faillog file).
408 memzero (&fl, sizeof (fl));
411 if (locktime == fl.fail_locktime) {
412 /* If the max is already set to the right value, do not
414 * This avoids writing 0 when no entries were present for
415 * the user and the max argument is 0.
420 fl.fail_locktime = locktime;
422 if ( (fseeko (fail, offset, SEEK_SET) == 0)
423 && (fwrite ((char *) &fl, sizeof (fl), 1, fail) == 1)) {
424 (void) fflush (fail);
429 _("faillog: Failed to set locktime for UID %lu\n"),
430 (unsigned long int)uid);
434 static void set_locktime (long locktime)
436 if (uflg && has_umin && has_umax && (umin==umax)) {
437 if (set_locktime_one ((uid_t)umin, locktime)) {
441 /* Set locktime for all entries in the specified range.
442 * If locktime is unchanged for an entry, the entry is not touched.
443 * If locktime is null, and no entries exist for this user, no
444 * entries will be created.
445 * Entries for non existing user are also taken into
446 * account (in order to define policy for future users).
449 uid_t uidmax = statbuf.st_size / sizeof (struct faillog);
451 /* Make sure we stay in the umin-umax range if specified */
455 if (has_umax && (uid_t)umax < uidmax) {
456 uidmax = (uid_t)umax;
459 while (uid < uidmax) {
460 if (set_locktime_one (uid, locktime)) {
468 int main (int argc, char **argv)
474 (void) setlocale (LC_ALL, "");
475 (void) bindtextdomain (PACKAGE, LOCALEDIR);
476 (void) textdomain (PACKAGE);
479 int option_index = 0;
481 static struct option long_options[] = {
482 {"all", no_argument, NULL, 'a'},
483 {"help", no_argument, NULL, 'h'},
484 {"lock-secs", required_argument, NULL, 'l'},
485 {"maximum", required_argument, NULL, 'm'},
486 {"reset", no_argument, NULL, 'r'},
487 {"time", required_argument, NULL, 't'},
488 {"user", required_argument, NULL, 'u'},
489 {NULL, 0, NULL, '\0'}
491 while ((c = getopt_long (argc, argv, "ahl:m:rt:u:",
492 long_options, &option_index)) != -1) {
501 if (getlong (optarg, &fail_locktime) == 0) {
503 _("%s: invalid numeric argument '%s'\n"),
510 if (getlong (optarg, &fail_max) == 0) {
512 _("%s: invalid numeric argument '%s'\n"),
522 if (getlong (optarg, &days) == 0) {
524 _("%s: invalid numeric argument '%s'\n"),
528 seconds = (time_t) days * DAY;
537 * - a numerical login ID
538 * - a range (-x, x-, x-y)
540 struct passwd *pwent;
543 /* local, no need for xgetpwnam */
544 pwent = getpwnam (optarg);
546 umin = (unsigned long) pwent->pw_uid;
551 if (getrange (optarg,
553 &umax, &has_umax) == 0) {
555 _("lastlog: Unknown user or range: %s\n"),
572 if (tflg && (lflg || mflg || rflg)) {
576 /* Open the faillog database */
577 if (lflg || mflg || rflg) {
578 fail = fopen (FAILLOG_FILE, "r+");
580 fail = fopen (FAILLOG_FILE, "r");
584 _("faillog: Cannot open %s: %s\n"),
585 FAILLOG_FILE, strerror (errno));
589 /* Get the size of the faillog */
590 if (fstat (fileno (fail), &statbuf) != 0) {
592 _("faillog: Cannot get the size of %s: %s\n"),
593 FAILLOG_FILE, strerror (errno));
598 set_locktime (fail_locktime);
609 if (!(lflg || mflg || rflg)) {
615 exit (errors ? E_NOPERM : E_SUCCESS);