2007-10-07 17:14:02 +05:30
|
|
|
/*
|
2008-04-27 06:10:09 +05:30
|
|
|
* Copyright (c) 1989 - 1994, Julianne Frances Haugh
|
|
|
|
* Copyright (c) 1996 - 1998, Marek Michałkiewicz
|
|
|
|
* Copyright (c) 2002 - 2005, Tomasz Kłoczko
|
2008-05-26 05:01:10 +05:30
|
|
|
* Copyright (c) 2008 , Nicolas François
|
2007-10-07 17:14:02 +05:30
|
|
|
* 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.
|
2008-04-27 06:10:09 +05:30
|
|
|
* 3. The name of the copyright holders or contributors may not be used to
|
|
|
|
* endorse or promote products derived from this software without
|
|
|
|
* specific prior written permission.
|
2007-10-07 17:14:02 +05:30
|
|
|
*
|
2008-04-27 06:10:09 +05:30
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
* HOLDERS OR CONTRIBUTORS BE LIABLE 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.
|
2007-10-07 17:14:02 +05:30
|
|
|
*/
|
|
|
|
|
|
|
|
#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:14:02 +05:30
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include "defines.h"
|
|
|
|
#include "faillog.h"
|
|
|
|
#include "getdef.h"
|
|
|
|
#include "failure.h"
|
|
|
|
#define YEAR (365L*DAY)
|
|
|
|
/*
|
|
|
|
* failure - make failure entry
|
|
|
|
*
|
|
|
|
* failure() creates a new (struct faillog) entry or updates an
|
|
|
|
* existing one with the current failed login information.
|
|
|
|
*/
|
2007-10-07 17:15:23 +05:30
|
|
|
void failure (uid_t uid, const char *tty, struct faillog *fl)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Don't do anything if failure logging isn't set up.
|
|
|
|
*/
|
2008-05-26 04:14:44 +05:30
|
|
|
/* TODO: check if the file exists */
|
2007-10-07 17:15:23 +05:30
|
|
|
if ((fd = open (FAILLOG_FILE, O_RDWR)) < 0)
|
2007-10-07 17:14:02 +05:30
|
|
|
return;
|
|
|
|
|
|
|
|
/*
|
2007-10-07 17:16:25 +05:30
|
|
|
* The file is indexed by UID value meaning that shared UID's
|
2007-10-07 17:14:02 +05:30
|
|
|
* share failure log records. That's OK since they really
|
|
|
|
* share just about everything else ...
|
|
|
|
*/
|
|
|
|
|
2007-10-07 17:15:23 +05:30
|
|
|
lseek (fd, (off_t) (sizeof *fl) * uid, SEEK_SET);
|
|
|
|
if (read (fd, (char *) fl, sizeof *fl) != sizeof *fl)
|
|
|
|
memzero (fl, sizeof *fl);
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
/*
|
|
|
|
* Update the record. We increment the failure count to log the
|
|
|
|
* latest failure. The only concern here is overflow, and we'll
|
|
|
|
* check for that. The line name and time of day are both
|
|
|
|
* updated as well.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (fl->fail_cnt + 1 > 0)
|
|
|
|
fl->fail_cnt++;
|
|
|
|
|
2007-10-07 17:15:23 +05:30
|
|
|
strncpy (fl->fail_line, tty, sizeof fl->fail_line);
|
|
|
|
time (&fl->fail_time);
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
/*
|
|
|
|
* Seek back to the correct position in the file and write the
|
|
|
|
* record out. Ideally we should lock the file in case the same
|
|
|
|
* account is being logged simultaneously. But the risk doesn't
|
|
|
|
* seem that great.
|
|
|
|
*/
|
|
|
|
|
2007-10-07 17:15:23 +05:30
|
|
|
lseek (fd, (off_t) (sizeof *fl) * uid, SEEK_SET);
|
2008-05-26 04:14:44 +05:30
|
|
|
/* TODO: check failures */
|
2007-10-07 17:15:23 +05:30
|
|
|
write (fd, (char *) fl, sizeof *fl);
|
2008-05-26 04:14:44 +05:30
|
|
|
/* TODO: log failures */
|
2007-10-07 17:15:23 +05:30
|
|
|
close (fd);
|
2008-05-26 04:14:44 +05:30
|
|
|
/* TODO: log failures */
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
|
2008-05-26 04:14:44 +05:30
|
|
|
static bool too_many_failures (const struct faillog *fl)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
|
|
|
time_t now;
|
|
|
|
|
|
|
|
if (fl->fail_max == 0 || fl->fail_cnt < fl->fail_max)
|
2008-05-26 04:14:44 +05:30
|
|
|
return false;
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
if (fl->fail_locktime == 0)
|
2008-05-26 04:14:44 +05:30
|
|
|
return true; /* locked until reset manually */
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2007-10-07 17:15:23 +05:30
|
|
|
time (&now);
|
|
|
|
if (fl->fail_time + fl->fail_locktime < now)
|
2008-05-26 04:14:44 +05:30
|
|
|
return false; /* enough time since last failure */
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2008-05-26 04:14:44 +05:30
|
|
|
return true;
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* failcheck - check for failures > allowable
|
|
|
|
*
|
|
|
|
* failcheck() is called AFTER the password has been validated. If the
|
|
|
|
* account has been "attacked" with too many login failures, failcheck()
|
2008-05-26 04:14:44 +05:30
|
|
|
* returns 0 to indicate that the login should be denied even though
|
2007-10-07 17:14:02 +05:30
|
|
|
* the password is valid.
|
2008-05-26 04:14:44 +05:30
|
|
|
*
|
|
|
|
* failed indicates if the login failed AFTER the password has been
|
|
|
|
* validated.
|
2007-10-07 17:14:02 +05:30
|
|
|
*/
|
|
|
|
|
2008-05-26 04:14:44 +05:30
|
|
|
int failcheck (uid_t uid, struct faillog *fl, bool failed)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
2007-10-07 17:15:23 +05:30
|
|
|
int fd;
|
|
|
|
struct faillog fail;
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
/*
|
|
|
|
* Suppress the check if the log file isn't there.
|
|
|
|
*/
|
|
|
|
|
2008-05-26 04:14:44 +05:30
|
|
|
/* TODO: check if the file exists */
|
|
|
|
fd = open (FAILLOG_FILE, O_RDWR);
|
|
|
|
if (fd < 0) {
|
2007-10-07 17:14:02 +05:30
|
|
|
return 1;
|
2008-05-26 04:14:44 +05:30
|
|
|
}
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
/*
|
|
|
|
* Get the record from the file and determine if the user has
|
|
|
|
* exceeded the failure limit. If "max" is zero, any number
|
|
|
|
* of failures are permitted. Only when "max" is non-zero and
|
|
|
|
* "cnt" is greater than or equal to "max" is the account
|
|
|
|
* considered to be locked.
|
|
|
|
*
|
|
|
|
* If read fails, there is no record for this user yet (the
|
|
|
|
* file is initially zero length and extended by writes), so
|
|
|
|
* no need to reset the count.
|
|
|
|
*/
|
|
|
|
|
|
|
|
lseek (fd, (off_t) (sizeof *fl) * uid, SEEK_SET);
|
2007-10-07 17:15:23 +05:30
|
|
|
if (read (fd, (char *) fl, sizeof *fl) != sizeof *fl) {
|
|
|
|
close (fd);
|
2007-10-07 17:14:02 +05:30
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2007-10-07 17:15:23 +05:30
|
|
|
if (too_many_failures (fl)) {
|
|
|
|
close (fd);
|
2007-10-07 17:14:02 +05:30
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The record is updated if this is not a failure. The count will
|
|
|
|
* be reset to zero, but the rest of the information will be left
|
|
|
|
* in the record in case someone wants to see where the failed
|
|
|
|
* login originated.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (!failed) {
|
|
|
|
fail = *fl;
|
|
|
|
fail.fail_cnt = 0;
|
|
|
|
|
|
|
|
lseek (fd, (off_t) sizeof fail * uid, SEEK_SET);
|
|
|
|
write (fd, (char *) &fail, sizeof fail);
|
|
|
|
}
|
|
|
|
close (fd);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* failprint - print line of failure information
|
|
|
|
*
|
|
|
|
* failprint takes a (struct faillog) entry and formats it into a
|
|
|
|
* message which is displayed at login time.
|
|
|
|
*/
|
|
|
|
|
2007-10-07 17:15:23 +05:30
|
|
|
void failprint (const struct faillog *fail)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
2007-10-07 17:15:23 +05:30
|
|
|
struct tm *tp;
|
|
|
|
|
2007-10-07 17:14:02 +05:30
|
|
|
#if HAVE_STRFTIME
|
2007-10-07 17:15:23 +05:30
|
|
|
char lasttimeb[256];
|
|
|
|
char *lasttime = lasttimeb;
|
2007-10-07 17:14:02 +05:30
|
|
|
#else
|
2007-10-07 17:15:23 +05:30
|
|
|
char *lasttime;
|
2007-10-07 17:14:02 +05:30
|
|
|
#endif
|
|
|
|
time_t NOW;
|
|
|
|
|
|
|
|
if (fail->fail_cnt == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
tp = localtime (&(fail->fail_time));
|
2007-10-07 17:15:23 +05:30
|
|
|
time (&NOW);
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
#if HAVE_STRFTIME
|
|
|
|
/*
|
2007-10-07 17:17:01 +05:30
|
|
|
* Print all information we have.
|
2007-10-07 17:14:02 +05:30
|
|
|
*/
|
2007-10-07 17:17:01 +05:30
|
|
|
strftime (lasttimeb, sizeof lasttimeb, "%c", tp);
|
2007-10-07 17:14:02 +05:30
|
|
|
#else
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Do the same thing, but don't use strftime since it
|
|
|
|
* probably doesn't exist on this system
|
|
|
|
*/
|
|
|
|
lasttime = asctime (tp);
|
|
|
|
lasttime[24] = '\0';
|
|
|
|
|
|
|
|
if (NOW - fail->fail_time < YEAR)
|
|
|
|
lasttime[19] = '\0';
|
|
|
|
if (NOW - fail->fail_time < DAY)
|
|
|
|
lasttime = lasttime + 11;
|
|
|
|
|
|
|
|
if (*lasttime == ' ')
|
|
|
|
lasttime++;
|
|
|
|
#endif
|
2007-10-07 17:16:25 +05:30
|
|
|
printf (ngettext ("%d failure since last login.\n"
|
|
|
|
"Last was %s on %s.\n",
|
|
|
|
"%d failures since last login.\n"
|
|
|
|
"Last was %s on %s.\n",
|
|
|
|
fail->fail_cnt),
|
|
|
|
fail->fail_cnt, lasttime, fail->fail_line);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* failtmp - update the cummulative failure log
|
|
|
|
*
|
|
|
|
* failtmp updates the (struct utmp) formatted failure log which
|
|
|
|
* maintains a record of all login failures.
|
|
|
|
*/
|
|
|
|
|
2007-10-07 17:16:25 +05:30
|
|
|
void failtmp (
|
|
|
|
#ifdef HAVE_UTMPX_H
|
|
|
|
const struct utmpx *failent
|
|
|
|
#else
|
|
|
|
const struct utmp *failent
|
|
|
|
#endif
|
|
|
|
)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
|
|
|
char *ftmp;
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get the name of the failure file. If no file has been defined
|
|
|
|
* in login.defs, don't do this.
|
|
|
|
*/
|
|
|
|
|
2008-05-26 04:14:44 +05:30
|
|
|
ftmp = getdef_str ("FTMP_FILE");
|
|
|
|
if (NULL == ftmp) {
|
2007-10-07 17:14:02 +05:30
|
|
|
return;
|
2008-05-26 04:14:44 +05:30
|
|
|
}
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
/*
|
|
|
|
* Open the file for append. It must already exist for this
|
|
|
|
* feature to be used.
|
|
|
|
*/
|
|
|
|
|
2008-05-26 04:14:44 +05:30
|
|
|
fd = open (ftmp, O_WRONLY | O_APPEND);
|
|
|
|
if (-1 == fd) {
|
2007-10-07 17:14:02 +05:30
|
|
|
return;
|
2008-05-26 04:14:44 +05:30
|
|
|
}
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
/*
|
|
|
|
* Output the new failure record and close the log file.
|
|
|
|
*/
|
|
|
|
|
2007-10-07 17:15:23 +05:30
|
|
|
write (fd, (const char *) failent, sizeof *failent);
|
|
|
|
close (fd);
|
2008-05-26 04:14:44 +05:30
|
|
|
/* TODO: check if the file could be closed */
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
2008-05-26 04:14:44 +05:30
|
|
|
|