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.
41 #include <sys/types.h>
45 #include "exitcodes.h"
47 #include "prototypes.h"
51 static FILE *fail; /* failure file stream */
52 static time_t seconds; /* that number of days in seconds */
53 static unsigned long umin; /* if uflg and has_umin, only display users with uid >= umin */
54 static bool has_umin = false;
55 static unsigned long umax; /* if uflg and has_umax, only display users with uid <= umax */
56 static bool has_umax = false;
57 static bool errors = false;
59 static bool aflg = false; /* set if all users are to be printed always */
60 static bool uflg = false; /* set if user is a valid user id */
61 static bool tflg = false; /* print is restricted to most recent days */
62 static bool lflg = false; /* set the locktime */
63 static bool mflg = false; /* set maximum failed login counters */
64 static bool rflg = false; /* reset the counters of login failures */
66 static struct stat statbuf; /* fstat buffer for file size */
68 #define NOW (time((time_t *) 0))
70 static void usage (void)
72 fputs (_("Usage: faillog [options]\n"
75 " -a, --all display faillog records for all users\n"
76 " -h, --help display this help message and exit\n"
77 " -l, --lock-time SEC after failed login lock account to SEC seconds\n"
78 " -m, --maximum MAX set maximum failed login counters to MAX\n"
79 " -r, --reset reset the counters of login failures\n"
80 " -t, --time DAYS display faillog records more recent than DAYS\n"
81 " -u, --user LOGIN display faillog record or maintains failure\n"
82 " counters and limits (if used with -r, -m or -l\n"
83 " options) only for user with LOGIN\n"
88 static void print_one (/*@null@*/const struct passwd *pw, bool force)
90 static bool once = false;
105 offset = pw->pw_uid * sizeof (fl);
106 if (offset <= (statbuf.st_size - sizeof (fl))) {
107 /* fseeko errors are not really relevant for us. */
108 int err = fseeko (fail, offset, SEEK_SET);
110 /* faillog is a sparse file. Even if no entries were
111 * entered for this user, which should be able to get the
112 * empty entry in this case.
114 if (fread ((char *) &fl, sizeof (fl), 1, fail) != 1) {
116 _("faillog: Failed to get the entry for UID %lu\n"),
117 (unsigned long int)pw->pw_uid);
121 /* Outsize of the faillog file.
122 * Behave as if there were a missing entry (same behavior
123 * as if we were reading an non existing entry in the
124 * sparse faillog file).
126 memzero (&fl, sizeof (fl));
129 /* Nothing to report */
130 if (!force && (0 == fl.fail_time)) {
136 /* Filter out entries that do not match with the -t option */
137 if (tflg && ((now - fl.fail_time) > seconds)) {
141 /* Print the header only once */
143 puts (_("Login Failures Maximum Latest On\n"));
147 tm = localtime (&fl.fail_time);
149 strftime (ptime, sizeof (ptime), "%D %H:%M:%S %z", tm);
152 printf ("%-9s %5d %5d ",
153 pw->pw_name, fl.fail_cnt, fl.fail_max);
154 /* FIXME: cp is not defined ifndef HAVE_STRFTIME */
155 printf ("%s %s", cp, fl.fail_line);
156 if (0 != fl.fail_locktime) {
157 if ( ((fl.fail_time + fl.fail_locktime) > now)
158 && (0 != fl.fail_cnt)) {
159 printf (_(" [%lus left]"),
160 (unsigned long) fl.fail_time + fl.fail_locktime - now);
162 printf (_(" [%lds lock]"),
169 static void print (void)
171 if (uflg && has_umin && has_umax && (umin==umax)) {
172 print_one (getpwuid ((uid_t)umin), true);
174 /* We only print records for existing users.
175 * Loop based on the user database instead of reading the
176 * whole file. We will have to query the database anyway
177 * so except for very small ranges and large user
178 * database, this should not be a performance issue.
180 struct passwd *pwent;
183 while ( (pwent = getpwent ()) != NULL ) {
185 && ( (has_umin && (pwent->pw_uid < (uid_t)umin))
186 || (has_umax && (pwent->pw_uid > (uid_t)umax)))) {
189 print_one (pwent, aflg);
196 * reset_one - Reset the fail count for one user
198 * This returns a boolean indicating if an error occurred.
200 static bool reset_one (uid_t uid)
205 offset = uid * sizeof (fl);
206 if (offset <= (statbuf.st_size - sizeof (fl))) {
207 /* fseeko errors are not really relevant for us. */
208 int err = fseeko (fail, offset, SEEK_SET);
210 /* faillog is a sparse file. Even if no entries were
211 * entered for this user, which should be able to get the
212 * empty entry in this case.
214 if (fread ((char *) &fl, sizeof (fl), 1, fail) != 1) {
216 _("faillog: Failed to get the entry for UID %lu\n"),
217 (unsigned long int)uid);
221 /* Outsize of the faillog file.
222 * Behave as if there were a missing entry (same behavior
223 * as if we were reading an non existing entry in the
224 * sparse faillog file).
226 memzero (&fl, sizeof (fl));
229 if (0 == fl.fail_cnt) {
230 /* If the count is already null, do not write in the file.
231 * This avoids writing 0 when no entries were present for
239 if ( (fseeko (fail, offset, SEEK_SET) == 0)
240 && (fwrite ((char *) &fl, sizeof (fl), 1, fail) == 1)) {
241 (void) fflush (fail);
246 _("faillog: Failed to reset fail count for UID %lu\n"),
247 (unsigned long int)uid);
251 static void reset (void)
253 if (uflg && has_umin && has_umax && (umin==umax)) {
254 if (reset_one ((uid_t)umin)) {
258 /* Reset all entries in the specified range.
259 * Non existing entries will not be touched.
260 * Entries for non existing users are also reset.
263 uid_t uidmax = statbuf.st_size / sizeof (struct faillog);
265 /* Make sure we stay in the umin-umax range if specified */
269 if (has_umax && (uid_t)umax < uidmax) {
270 uidmax = (uid_t)umax;
273 while (uid < uidmax) {
274 if (reset_one (uid)) {
283 * setmax_one - Set the maximum failed login counter for one user
285 * This returns a boolean indicating if an error occurred.
287 static bool setmax_one (uid_t uid, int max)
292 offset = (off_t) uid * sizeof (fl);
293 if (offset <= (statbuf.st_size - sizeof (fl))) {
294 /* fseeko errors are not really relevant for us. */
295 int err = fseeko (fail, offset, SEEK_SET);
297 /* faillog is a sparse file. Even if no entries were
298 * entered for this user, which should be able to get the
299 * empty entry in this case.
301 if (fread ((char *) &fl, sizeof (fl), 1, fail) != 1) {
303 _("faillog: Failed to get the entry for UID %lu\n"),
304 (unsigned long int)uid);
308 /* Outsize of the faillog file.
309 * Behave as if there were a missing entry (same behavior
310 * as if we were reading an non existing entry in the
311 * sparse faillog file).
313 memzero (&fl, sizeof (fl));
316 if (max == fl.fail_max) {
317 /* If the max is already set to the right value, do not
319 * This avoids writing 0 when no entries were present for
320 * the user and the max argument is 0.
327 if ( (fseeko (fail, offset, SEEK_SET) == 0)
328 && (fwrite ((char *) &fl, sizeof (fl), 1, fail) == 1)) {
329 (void) fflush (fail);
334 _("faillog: Failed to set max for UID %lu\n"),
335 (unsigned long int)uid);
339 static void setmax (int max)
341 if (uflg && has_umin && has_umax && (umin==umax)) {
342 if (setmax_one ((uid_t)umin, max)) {
346 /* Set max for all entries in the specified range.
347 * If max is unchanged for an entry, the entry is not touched.
348 * If max is null, and no entries exist for this user, no
349 * entries will be created.
350 * Entries for non existing user are also taken into
351 * account (in order to define policy for future users).
354 uid_t uidmax = statbuf.st_size / sizeof (struct faillog);
356 /* Make sure we stay in the umin-umax range if specified */
360 if (has_umax && (uid_t)umax < uidmax) {
361 uidmax = (uid_t)umax;
364 while (uid < uidmax) {
365 if (setmax_one (uid, max)) {
374 * set_locktime_one - Set the locktime for one user
376 * This returns a boolean indicating if an error occurred.
378 static bool set_locktime_one (uid_t uid, long locktime)
383 offset = (off_t) uid * sizeof (fl);
384 if (offset <= (statbuf.st_size - sizeof (fl))) {
385 /* fseeko errors are not really relevant for us. */
386 int err = fseeko (fail, offset, SEEK_SET);
388 /* faillog is a sparse file. Even if no entries were
389 * entered for this user, which should be able to get the
390 * empty entry in this case.
392 if (fread ((char *) &fl, sizeof (fl), 1, fail) != 1) {
394 _("faillog: Failed to get the entry for UID %lu\n"),
395 (unsigned long int)uid);
399 /* Outsize of the faillog file.
400 * Behave as if there were a missing entry (same behavior
401 * as if we were reading an non existing entry in the
402 * sparse faillog file).
404 memzero (&fl, sizeof (fl));
407 if (locktime == fl.fail_locktime) {
408 /* If the max is already set to the right value, do not
410 * This avoids writing 0 when no entries were present for
411 * the user and the max argument is 0.
416 fl.fail_locktime = locktime;
418 if ( (fseeko (fail, offset, SEEK_SET) == 0)
419 && (fwrite ((char *) &fl, sizeof (fl), 1, fail) == 1)) {
420 (void) fflush (fail);
425 _("faillog: Failed to set locktime for UID %lu\n"),
426 (unsigned long int)uid);
430 static void set_locktime (long locktime)
432 if (uflg && has_umin && has_umax && (umin==umax)) {
433 if (set_locktime_one ((uid_t)umin, locktime)) {
437 /* Set locktime for all entries in the specified range.
438 * If locktime is unchanged for an entry, the entry is not touched.
439 * If locktime is null, and no entries exist for this user, no
440 * entries will be created.
441 * Entries for non existing user are also taken into
442 * account (in order to define policy for future users).
445 uid_t uidmax = statbuf.st_size / sizeof (struct faillog);
447 /* Make sure we stay in the umin-umax range if specified */
451 if (has_umax && (uid_t)umax < uidmax) {
452 uidmax = (uid_t)umax;
455 while (uid < uidmax) {
456 if (set_locktime_one (uid, locktime)) {
464 int main (int argc, char **argv)
470 (void) setlocale (LC_ALL, "");
471 (void) bindtextdomain (PACKAGE, LOCALEDIR);
472 (void) textdomain (PACKAGE);
475 int option_index = 0;
477 static struct option long_options[] = {
478 {"all", no_argument, NULL, 'a'},
479 {"help", no_argument, NULL, 'h'},
480 {"lock-secs", required_argument, NULL, 'l'},
481 {"maximum", required_argument, NULL, 'm'},
482 {"reset", no_argument, NULL, 'r'},
483 {"time", required_argument, NULL, 't'},
484 {"user", required_argument, NULL, 'u'},
485 {NULL, 0, NULL, '\0'}
487 while ((c = getopt_long (argc, argv, "ahl:m:rt:u:",
488 long_options, &option_index)) != -1) {
497 if (getlong (optarg, &fail_locktime) == 0) {
499 _("%s: invalid numeric argument '%s'\n"),
506 if (getlong (optarg, &fail_max) == 0) {
508 _("%s: invalid numeric argument '%s'\n"),
518 if (getlong (optarg, &days) == 0) {
520 _("%s: invalid numeric argument '%s'\n"),
524 seconds = (time_t) days * DAY;
533 * - a numerical login ID
534 * - a range (-x, x-, x-y)
536 struct passwd *pwent;
539 /* local, no need for xgetpwnam */
540 pwent = getpwnam (optarg);
542 umin = (unsigned long) pwent->pw_uid;
547 if (getrange (optarg,
549 &umax, &has_umax) == 0) {
551 _("lastlog: Unknown user or range: %s\n"),
568 if (tflg && (lflg || mflg || rflg)) {
572 /* Open the faillog database */
573 if (lflg || mflg || rflg) {
574 fail = fopen (FAILLOG_FILE, "r+");
576 fail = fopen (FAILLOG_FILE, "r");
580 _("faillog: Cannot open %s: %s\n"),
581 FAILLOG_FILE, strerror (errno));
585 /* Get the size of the faillog */
586 if (fstat (fileno (fail), &statbuf) != 0) {
588 _("faillog: Cannot get the size of %s: %s\n"),
589 FAILLOG_FILE, strerror (errno));
594 set_locktime (fail_locktime);
605 if (!(lflg || mflg || rflg)) {
611 exit (errors ? EXIT_FAILURE : EXIT_SUCCESS);