2007-10-07 17:14:02 +05:30
|
|
|
/*
|
|
|
|
* Copyright 1989 - 1993, Julianne Frances Haugh
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. Neither the name of Julianne F. Haugh nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY JULIE HAUGH AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
2007-10-07 17:14:59 +05:30
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL JULIE HAUGH OR CONTRIBUTORS BE LIABLE
|
2007-10-07 17:14:02 +05:30
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
2007-11-11 05:16:11 +05:30
|
|
|
#ident "$Id$"
|
2007-10-07 17:17:01 +05:30
|
|
|
|
2007-10-07 17:16:52 +05:30
|
|
|
#include <getopt.h>
|
2007-10-07 17:14:02 +05:30
|
|
|
#include <pwd.h>
|
2007-10-07 17:16:52 +05:30
|
|
|
#include <stdio.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
2007-10-07 17:14:02 +05:30
|
|
|
#include <time.h>
|
|
|
|
#include "defines.h"
|
2007-10-07 17:16:52 +05:30
|
|
|
#include "exitcodes.h"
|
2007-10-07 17:14:02 +05:30
|
|
|
#include "faillog.h"
|
2007-10-07 17:16:52 +05:30
|
|
|
#include "prototypes.h"
|
2007-10-07 17:17:01 +05:30
|
|
|
/*
|
|
|
|
* Global variables
|
|
|
|
*/
|
2007-10-07 17:14:59 +05:30
|
|
|
static FILE *fail; /* failure file stream */
|
|
|
|
static uid_t user; /* one single user, specified on command line */
|
|
|
|
static int days; /* number of days to consider for print command */
|
|
|
|
static time_t seconds; /* that number of days in seconds */
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
static int
|
2007-10-07 17:14:59 +05:30
|
|
|
aflg = 0, /* set if all users are to be printed always */
|
2007-10-07 17:15:58 +05:30
|
|
|
uflg = 0, /* set if user is a valid user id */
|
|
|
|
tflg = 0; /* print is restricted to most recent days */
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
static struct stat statbuf; /* fstat buffer for file size */
|
|
|
|
|
|
|
|
#define NOW (time((time_t *) 0))
|
|
|
|
|
2007-10-07 17:14:59 +05:30
|
|
|
static void usage (void)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
2007-10-07 17:15:58 +05:30
|
|
|
fprintf (stderr, _("Usage: faillog [options]\n"
|
|
|
|
"\n"
|
|
|
|
"Options:\n"
|
|
|
|
" -a, --all display faillog records for all users\n"
|
|
|
|
" -h, --help display this help message and exit\n"
|
|
|
|
" -l, --lock-time SEC after failed login lock accout to SEC seconds\n"
|
2007-10-07 17:16:07 +05:30
|
|
|
" -m, --maximum MAX set maximum failed login counters to MAX\n"
|
2007-10-07 17:15:58 +05:30
|
|
|
" -r, --reset reset the counters of login failures\n"
|
2007-10-07 17:16:07 +05:30
|
|
|
" -t, --time DAYS display faillog records more recent than DAYS\n"
|
2007-10-07 17:15:58 +05:30
|
|
|
" -u, --user LOGIN display faillog record or maintains failure counters\n"
|
|
|
|
" and limits (if used with -r, -m or -l options) only\n"
|
2007-10-07 17:17:45 +05:30
|
|
|
" for user with LOGIN\n"
|
|
|
|
"\n"));
|
2007-10-07 17:16:52 +05:30
|
|
|
exit (E_USAGE);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
|
2007-10-07 17:14:59 +05:30
|
|
|
static void print_one (const struct faillog *fl, uid_t uid)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
2007-10-07 17:14:59 +05:30
|
|
|
static int once;
|
|
|
|
char *cp;
|
|
|
|
struct tm *tm;
|
2007-10-07 17:14:02 +05:30
|
|
|
time_t now;
|
2007-10-07 17:14:59 +05:30
|
|
|
struct passwd *pwent;
|
|
|
|
|
2007-10-07 17:14:02 +05:30
|
|
|
#ifdef HAVE_STRFTIME
|
|
|
|
char ptime[80];
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (!once) {
|
2007-10-07 17:16:25 +05:30
|
|
|
printf (_
|
|
|
|
("Login Failures Maximum Latest On\n"));
|
2007-10-07 17:14:02 +05:30
|
|
|
once++;
|
|
|
|
}
|
2007-10-07 17:14:59 +05:30
|
|
|
pwent = getpwuid (uid);
|
|
|
|
time (&now);
|
|
|
|
tm = localtime (&fl->fail_time);
|
2007-10-07 17:14:02 +05:30
|
|
|
#ifdef HAVE_STRFTIME
|
2007-10-07 17:16:07 +05:30
|
|
|
strftime (ptime, sizeof (ptime), "%D %H:%M:%S %z", tm);
|
2007-10-07 17:14:02 +05:30
|
|
|
cp = ptime;
|
|
|
|
#endif
|
|
|
|
if (pwent) {
|
2007-10-07 17:16:07 +05:30
|
|
|
printf ("%-9s %5d %5d ",
|
2007-10-07 17:14:02 +05:30
|
|
|
pwent->pw_name, fl->fail_cnt, fl->fail_max);
|
|
|
|
if (fl->fail_time) {
|
2007-10-07 17:16:07 +05:30
|
|
|
printf ("%s %s", cp, fl->fail_line);
|
2007-10-07 17:14:59 +05:30
|
|
|
if (fl->fail_locktime) {
|
|
|
|
if (fl->fail_time + fl->fail_locktime > now
|
|
|
|
&& fl->fail_cnt)
|
|
|
|
printf (_(" [%lds left]"),
|
|
|
|
fl->fail_time +
|
|
|
|
fl->fail_locktime - now);
|
|
|
|
else
|
|
|
|
printf (_(" [%lds lock]"),
|
|
|
|
fl->fail_locktime);
|
|
|
|
}
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
2007-10-07 17:14:59 +05:30
|
|
|
putchar ('\n');
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-10-07 17:14:59 +05:30
|
|
|
static int reset_one (uid_t uid)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
2007-10-07 17:14:59 +05:30
|
|
|
off_t offset;
|
|
|
|
struct faillog faillog;
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
offset = uid * sizeof faillog;
|
2007-10-07 17:14:59 +05:30
|
|
|
if (fstat (fileno (fail), &statbuf)) {
|
|
|
|
perror (FAILLOG_FILE);
|
2007-10-07 17:14:02 +05:30
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (offset >= statbuf.st_size)
|
|
|
|
return 0;
|
|
|
|
|
2007-10-07 17:15:58 +05:30
|
|
|
if (fseeko (fail, offset, SEEK_SET) != 0) {
|
2007-10-07 17:14:59 +05:30
|
|
|
perror (FAILLOG_FILE);
|
2007-10-07 17:14:02 +05:30
|
|
|
return 0;
|
|
|
|
}
|
2007-10-07 17:14:59 +05:30
|
|
|
if (fread ((char *) &faillog, sizeof faillog, 1, fail) != 1) {
|
|
|
|
if (!feof (fail))
|
|
|
|
perror (FAILLOG_FILE);
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (faillog.fail_cnt == 0)
|
|
|
|
return 1; /* don't fill in no holes ... */
|
|
|
|
|
|
|
|
faillog.fail_cnt = 0;
|
|
|
|
|
2007-10-07 17:15:58 +05:30
|
|
|
if (fseeko (fail, offset, SEEK_SET) == 0
|
2007-10-07 17:14:59 +05:30
|
|
|
&& fwrite ((char *) &faillog, sizeof faillog, 1, fail) == 1) {
|
|
|
|
fflush (fail);
|
2007-10-07 17:14:02 +05:30
|
|
|
return 1;
|
|
|
|
} else {
|
2007-10-07 17:14:59 +05:30
|
|
|
perror (FAILLOG_FILE);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-10-07 17:15:58 +05:30
|
|
|
static void reset (void)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
2007-10-07 17:15:58 +05:30
|
|
|
uid_t uid;
|
|
|
|
|
|
|
|
if (uflg)
|
|
|
|
reset_one (user);
|
|
|
|
else
|
|
|
|
for (uid = 0; reset_one (uid); uid++);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void print (void)
|
|
|
|
{
|
|
|
|
uid_t uid;
|
|
|
|
off_t offset;
|
|
|
|
struct faillog faillog;
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
if (uflg) {
|
2007-10-07 17:15:58 +05:30
|
|
|
offset = user * sizeof faillog;
|
|
|
|
if (fstat (fileno (fail), &statbuf)) {
|
|
|
|
perror (FAILLOG_FILE);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (offset >= statbuf.st_size)
|
|
|
|
return;
|
|
|
|
|
|
|
|
fseeko (fail, (off_t) user * sizeof faillog, SEEK_SET);
|
2007-10-07 17:16:07 +05:30
|
|
|
if (fread ((char *) &faillog, sizeof faillog, 1, fail) == 1)
|
2007-10-07 17:15:58 +05:30
|
|
|
print_one (&faillog, user);
|
|
|
|
else
|
|
|
|
perror (FAILLOG_FILE);
|
2007-10-07 17:14:02 +05:30
|
|
|
} else {
|
2007-10-07 17:15:58 +05:30
|
|
|
for (uid = 0;
|
|
|
|
fread ((char *) &faillog, sizeof faillog, 1,
|
|
|
|
fail) == 1; uid++) {
|
|
|
|
|
|
|
|
if (aflg == 0 && faillog.fail_cnt == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (aflg == 0 && tflg &&
|
|
|
|
NOW - faillog.fail_time > seconds)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (aflg && faillog.fail_time == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
print_one (&faillog, uid);
|
|
|
|
}
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-10-07 17:14:59 +05:30
|
|
|
static void setmax_one (uid_t uid, int max)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
2007-10-07 17:14:59 +05:30
|
|
|
off_t offset;
|
|
|
|
struct faillog faillog;
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
offset = uid * sizeof faillog;
|
|
|
|
|
2007-10-07 17:15:58 +05:30
|
|
|
if (fseeko (fail, offset, SEEK_SET) != 0) {
|
2007-10-07 17:14:59 +05:30
|
|
|
perror (FAILLOG_FILE);
|
2007-10-07 17:14:02 +05:30
|
|
|
return;
|
|
|
|
}
|
2007-10-07 17:14:59 +05:30
|
|
|
if (fread ((char *) &faillog, sizeof faillog, 1, fail) != 1) {
|
|
|
|
if (!feof (fail))
|
|
|
|
perror (FAILLOG_FILE);
|
|
|
|
memzero (&faillog, sizeof faillog);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
faillog.fail_max = max;
|
|
|
|
|
2007-10-07 17:15:58 +05:30
|
|
|
if (fseeko (fail, offset, SEEK_SET) == 0
|
2007-10-07 17:14:59 +05:30
|
|
|
&& fwrite ((char *) &faillog, sizeof faillog, 1, fail) == 1)
|
|
|
|
fflush (fail);
|
2007-10-07 17:14:02 +05:30
|
|
|
else
|
2007-10-07 17:14:59 +05:30
|
|
|
perror (FAILLOG_FILE);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
|
2007-10-07 17:15:58 +05:30
|
|
|
static void setmax (int max)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
2007-10-07 17:14:59 +05:30
|
|
|
struct passwd *pwent;
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
if (uflg) {
|
2007-10-07 17:15:58 +05:30
|
|
|
setmax_one (user, max);
|
2007-10-07 17:14:02 +05:30
|
|
|
} else {
|
2007-10-07 17:14:59 +05:30
|
|
|
setpwent ();
|
|
|
|
while ((pwent = getpwent ()))
|
2007-10-07 17:15:58 +05:30
|
|
|
setmax_one (pwent->pw_uid, max);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-10-07 17:14:59 +05:30
|
|
|
static void set_locktime_one (uid_t uid, long locktime)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
2007-10-07 17:14:59 +05:30
|
|
|
off_t offset;
|
|
|
|
struct faillog faillog;
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
offset = uid * sizeof faillog;
|
|
|
|
|
2007-10-07 17:15:58 +05:30
|
|
|
if (fseeko (fail, offset, SEEK_SET) != 0) {
|
2007-10-07 17:14:59 +05:30
|
|
|
perror (FAILLOG_FILE);
|
2007-10-07 17:14:02 +05:30
|
|
|
return;
|
|
|
|
}
|
2007-10-07 17:14:59 +05:30
|
|
|
if (fread ((char *) &faillog, sizeof faillog, 1, fail) != 1) {
|
|
|
|
if (!feof (fail))
|
|
|
|
perror (FAILLOG_FILE);
|
|
|
|
memzero (&faillog, sizeof faillog);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
faillog.fail_locktime = locktime;
|
|
|
|
|
2007-10-07 17:15:58 +05:30
|
|
|
if (fseeko (fail, offset, SEEK_SET) == 0
|
2007-10-07 17:14:59 +05:30
|
|
|
&& fwrite ((char *) &faillog, sizeof faillog, 1, fail) == 1)
|
|
|
|
fflush (fail);
|
2007-10-07 17:14:02 +05:30
|
|
|
else
|
2007-10-07 17:14:59 +05:30
|
|
|
perror (FAILLOG_FILE);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
2007-10-07 17:15:58 +05:30
|
|
|
|
|
|
|
/*
|
|
|
|
* XXX - this needs to be written properly some day, right now it is
|
|
|
|
* a quick cut-and-paste hack from the above two functions. --marekm
|
|
|
|
*/
|
|
|
|
static void set_locktime (long locktime)
|
|
|
|
{
|
|
|
|
struct passwd *pwent;
|
|
|
|
|
|
|
|
if (uflg) {
|
|
|
|
set_locktime_one (user, locktime);
|
|
|
|
} else {
|
|
|
|
setpwent ();
|
|
|
|
while ((pwent = getpwent ()))
|
|
|
|
set_locktime_one (pwent->pw_uid, locktime);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int main (int argc, char **argv)
|
|
|
|
{
|
|
|
|
int anyflag = 0;
|
|
|
|
struct passwd *pwent;
|
|
|
|
|
|
|
|
setlocale (LC_ALL, "");
|
|
|
|
bindtextdomain (PACKAGE, LOCALEDIR);
|
|
|
|
textdomain (PACKAGE);
|
|
|
|
|
|
|
|
/* try to open for read/write, if that fails - read only */
|
|
|
|
fail = fopen (FAILLOG_FILE, "r+");
|
|
|
|
if (!fail)
|
|
|
|
fail = fopen (FAILLOG_FILE, "r");
|
|
|
|
if (!fail) {
|
|
|
|
perror (FAILLOG_FILE);
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
int option_index = 0;
|
|
|
|
int c;
|
|
|
|
static struct option long_options[] = {
|
2007-10-07 17:16:07 +05:30
|
|
|
{"all", no_argument, NULL, 'a'},
|
2007-10-07 17:15:58 +05:30
|
|
|
{"help", no_argument, NULL, 'h'},
|
2007-10-07 17:17:45 +05:30
|
|
|
{"lock-secs", required_argument, NULL, 'l'},
|
|
|
|
{"maximum", required_argument, NULL, 'm'},
|
2007-10-07 17:15:58 +05:30
|
|
|
{"reset", no_argument, NULL, 'r'},
|
2007-10-07 17:17:45 +05:30
|
|
|
{"time", required_argument, NULL, 't'},
|
|
|
|
{"user", required_argument, NULL, 'u'},
|
2007-10-07 17:15:58 +05:30
|
|
|
{NULL, 0, NULL, '\0'}
|
|
|
|
};
|
|
|
|
|
|
|
|
while ((c =
|
|
|
|
getopt_long (argc, argv, "ahl:m:rt:u:",
|
|
|
|
long_options, &option_index)) != -1) {
|
2007-10-07 17:16:07 +05:30
|
|
|
switch (c) {
|
2007-10-07 17:15:58 +05:30
|
|
|
case 'a':
|
|
|
|
aflg++;
|
|
|
|
if (uflg)
|
|
|
|
usage ();
|
|
|
|
break;
|
2007-10-07 17:16:07 +05:30
|
|
|
case 'h':
|
|
|
|
usage ();
|
|
|
|
break;
|
2007-10-07 17:15:58 +05:30
|
|
|
case 'l':
|
|
|
|
set_locktime ((long) atoi (optarg));
|
|
|
|
anyflag++;
|
|
|
|
break;
|
|
|
|
case 'm':
|
|
|
|
setmax (atoi (optarg));
|
|
|
|
anyflag++;
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
reset ();
|
|
|
|
anyflag++;
|
|
|
|
break;
|
2007-10-07 17:16:07 +05:30
|
|
|
case 't':
|
|
|
|
days = atoi (optarg);
|
|
|
|
seconds = days * DAY;
|
|
|
|
tflg++;
|
|
|
|
break;
|
2007-10-07 17:15:58 +05:30
|
|
|
case 'u':
|
|
|
|
if (aflg)
|
|
|
|
usage ();
|
|
|
|
|
|
|
|
pwent = getpwnam (optarg);
|
|
|
|
if (!pwent) {
|
2007-10-07 17:16:07 +05:30
|
|
|
fprintf (stderr,
|
|
|
|
_("Unknown User: %s\n"),
|
2007-10-07 17:15:58 +05:30
|
|
|
optarg);
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
uflg++;
|
|
|
|
user = pwent->pw_uid;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* no flags implies -a -p (= print information for all users) */
|
|
|
|
if (!(anyflag || aflg || tflg || uflg))
|
|
|
|
aflg++;
|
|
|
|
/* (-a or -t days or -u user) and no other flags implies -p
|
|
|
|
(= print information for selected users) */
|
|
|
|
if (!anyflag && (aflg || tflg || uflg))
|
|
|
|
print ();
|
|
|
|
fclose (fail);
|
2007-10-07 17:16:52 +05:30
|
|
|
|
|
|
|
exit (E_SUCCESS);
|
2007-10-07 17:15:58 +05:30
|
|
|
}
|