2 * Copyright 1989 - 1993, Julianne Frances Haugh
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of Julianne F. Haugh nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY JULIE HAUGH AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL JULIE HAUGH OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 RCSID (PKG_VER "$Id: faillog.c,v 1.23 2005/05/25 19:31:51 kloczek Exp $")
34 #include <sys/types.h>
40 #include "prototypes.h"
43 static FILE *fail; /* failure file stream */
44 static uid_t user; /* one single user, specified on command line */
45 static int days; /* number of days to consider for print command */
46 static time_t seconds; /* that number of days in seconds */
49 aflg = 0, /* set if all users are to be printed always */
50 uflg = 0, /* set if user is a valid user id */
51 tflg = 0; /* print is restricted to most recent days */
53 static struct stat statbuf; /* fstat buffer for file size */
55 #define NOW (time((time_t *) 0))
57 static void usage (void)
59 fprintf (stderr, _("Usage: faillog [options]\n"
62 " -a, --all display faillog records for all users\n"
63 " -h, --help display this help message and exit\n"
64 " -l, --lock-time SEC after failed login lock accout to SEC seconds\n"
65 " -m, --maximum MAX set maximum failed login counters to MAX\n"
66 " -r, --reset reset the counters of login failures\n"
67 " -t, --time DAYS display faillog records more recent than DAYS\n"
68 " -u, --user LOGIN display faillog record or maintains failure counters\n"
69 " and limits (if used with -r, -m or -l options) only\n"
70 " for user with LOGIN\n"));
74 static void print_one (const struct faillog *fl, uid_t uid)
88 ("Login Failures Maximum Latest On\n"));
91 pwent = getpwuid (uid);
93 tm = localtime (&fl->fail_time);
95 strftime (ptime, sizeof (ptime), "%D %H:%M:%S %z", tm);
99 printf ("%-9s %5d %5d ",
100 pwent->pw_name, fl->fail_cnt, fl->fail_max);
102 printf ("%s %s", cp, fl->fail_line);
103 if (fl->fail_locktime) {
104 if (fl->fail_time + fl->fail_locktime > now
106 printf (_(" [%lds left]"),
108 fl->fail_locktime - now);
110 printf (_(" [%lds lock]"),
118 static int reset_one (uid_t uid)
121 struct faillog faillog;
123 offset = uid * sizeof faillog;
124 if (fstat (fileno (fail), &statbuf)) {
125 perror (FAILLOG_FILE);
128 if (offset >= statbuf.st_size)
131 if (fseeko (fail, offset, SEEK_SET) != 0) {
132 perror (FAILLOG_FILE);
135 if (fread ((char *) &faillog, sizeof faillog, 1, fail) != 1) {
137 perror (FAILLOG_FILE);
141 if (faillog.fail_cnt == 0)
142 return 1; /* don't fill in no holes ... */
144 faillog.fail_cnt = 0;
146 if (fseeko (fail, offset, SEEK_SET) == 0
147 && fwrite ((char *) &faillog, sizeof faillog, 1, fail) == 1) {
151 perror (FAILLOG_FILE);
156 static void reset (void)
163 for (uid = 0; reset_one (uid); uid++);
166 static void print (void)
170 struct faillog faillog;
173 offset = user * sizeof faillog;
174 if (fstat (fileno (fail), &statbuf)) {
175 perror (FAILLOG_FILE);
178 if (offset >= statbuf.st_size)
181 fseeko (fail, (off_t) user * sizeof faillog, SEEK_SET);
182 if (fread ((char *) &faillog, sizeof faillog, 1, fail) == 1)
183 print_one (&faillog, user);
185 perror (FAILLOG_FILE);
188 fread ((char *) &faillog, sizeof faillog, 1,
191 if (aflg == 0 && faillog.fail_cnt == 0)
194 if (aflg == 0 && tflg &&
195 NOW - faillog.fail_time > seconds)
198 if (aflg && faillog.fail_time == 0)
201 print_one (&faillog, uid);
206 static void setmax_one (uid_t uid, int max)
209 struct faillog faillog;
211 offset = uid * sizeof faillog;
213 if (fseeko (fail, offset, SEEK_SET) != 0) {
214 perror (FAILLOG_FILE);
217 if (fread ((char *) &faillog, sizeof faillog, 1, fail) != 1) {
219 perror (FAILLOG_FILE);
220 memzero (&faillog, sizeof faillog);
222 faillog.fail_max = max;
224 if (fseeko (fail, offset, SEEK_SET) == 0
225 && fwrite ((char *) &faillog, sizeof faillog, 1, fail) == 1)
228 perror (FAILLOG_FILE);
231 static void setmax (int max)
233 struct passwd *pwent;
236 setmax_one (user, max);
239 while ((pwent = getpwent ()))
240 setmax_one (pwent->pw_uid, max);
244 static void set_locktime_one (uid_t uid, long locktime)
247 struct faillog faillog;
249 offset = uid * sizeof faillog;
251 if (fseeko (fail, offset, SEEK_SET) != 0) {
252 perror (FAILLOG_FILE);
255 if (fread ((char *) &faillog, sizeof faillog, 1, fail) != 1) {
257 perror (FAILLOG_FILE);
258 memzero (&faillog, sizeof faillog);
260 faillog.fail_locktime = locktime;
262 if (fseeko (fail, offset, SEEK_SET) == 0
263 && fwrite ((char *) &faillog, sizeof faillog, 1, fail) == 1)
266 perror (FAILLOG_FILE);
270 * XXX - this needs to be written properly some day, right now it is
271 * a quick cut-and-paste hack from the above two functions. --marekm
273 static void set_locktime (long locktime)
275 struct passwd *pwent;
278 set_locktime_one (user, locktime);
281 while ((pwent = getpwent ()))
282 set_locktime_one (pwent->pw_uid, locktime);
286 int main (int argc, char **argv)
289 struct passwd *pwent;
291 setlocale (LC_ALL, "");
292 bindtextdomain (PACKAGE, LOCALEDIR);
293 textdomain (PACKAGE);
295 /* try to open for read/write, if that fails - read only */
296 fail = fopen (FAILLOG_FILE, "r+");
298 fail = fopen (FAILLOG_FILE, "r");
300 perror (FAILLOG_FILE);
305 int option_index = 0;
307 static struct option long_options[] = {
308 {"all", no_argument, NULL, 'a'},
309 {"help", no_argument, NULL, 'h'},
310 {"lock-secs", no_argument, NULL, 'l'},
311 {"maximum", no_argument, NULL, 'm'},
312 {"reset", no_argument, NULL, 'r'},
313 {"time", no_argument, NULL, 't'},
314 {"user", no_argument, NULL, 'u'},
315 {NULL, 0, NULL, '\0'}
319 getopt_long (argc, argv, "ahl:m:rt:u:",
320 long_options, &option_index)) != -1) {
331 set_locktime ((long) atoi (optarg));
335 setmax (atoi (optarg));
343 days = atoi (optarg);
344 seconds = days * DAY;
351 pwent = getpwnam (optarg);
354 _("Unknown User: %s\n"),
359 user = pwent->pw_uid;
367 /* no flags implies -a -p (= print information for all users) */
368 if (!(anyflag || aflg || tflg || uflg))
370 /* (-a or -t days or -u user) and no other flags implies -p
371 (= print information for selected users) */
372 if (!anyflag && (aflg || tflg || uflg))