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
|
2010-08-22 18:34:54 +05:30
|
|
|
* Copyright (c) 2008 - 2010, 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>
|
2008-06-15 05:08:43 +05:30
|
|
|
#include <unistd.h>
|
2007-10-07 17:14:02 +05:30
|
|
|
#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;
|
2008-06-15 05:08:43 +05:30
|
|
|
off_t offset_uid = (off_t) (sizeof *fl) * uid;
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
/*
|
|
|
|
* Don't do anything if failure logging isn't set up.
|
|
|
|
*/
|
2008-06-15 05:08:43 +05:30
|
|
|
|
|
|
|
if (access (FAILLOG_FILE, F_OK) != 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-06-14 00:01:13 +05:30
|
|
|
fd = open (FAILLOG_FILE, O_RDWR);
|
|
|
|
if (fd < 0) {
|
2008-06-15 05:08:43 +05:30
|
|
|
SYSLOG ((LOG_WARN,
|
|
|
|
"Can't write faillog entry for UID %lu in %s.",
|
|
|
|
(unsigned long) uid, FAILLOG_FILE));
|
2007-10-07 17:14:02 +05:30
|
|
|
return;
|
2008-06-14 00:01:13 +05:30
|
|
|
}
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
/*
|
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 ...
|
|
|
|
*/
|
|
|
|
|
2008-06-15 05:08:43 +05:30
|
|
|
if ( (lseek (fd, offset_uid, SEEK_SET) != offset_uid)
|
|
|
|
|| (read (fd, (char *) fl, sizeof *fl) != (ssize_t) sizeof *fl)) {
|
|
|
|
/* This is not necessarily a failure. The file is
|
|
|
|
* initially zero length.
|
|
|
|
*
|
|
|
|
* If lseek() or read() failed for any other reason, this
|
|
|
|
* might reset the counter. But the new failure will be
|
|
|
|
* logged.
|
|
|
|
*/
|
2007-10-07 17:15:23 +05:30
|
|
|
memzero (fl, sizeof *fl);
|
2008-06-14 00:01:13 +05:30
|
|
|
}
|
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.
|
|
|
|
*/
|
|
|
|
|
2008-06-14 00:01:13 +05:30
|
|
|
if (fl->fail_cnt + 1 > 0) {
|
2007-10-07 17:14:02 +05:30
|
|
|
fl->fail_cnt++;
|
2008-06-14 00:01:13 +05:30
|
|
|
}
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2007-10-07 17:15:23 +05:30
|
|
|
strncpy (fl->fail_line, tty, sizeof fl->fail_line);
|
2008-06-14 00:01:13 +05:30
|
|
|
(void) 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.
|
|
|
|
*/
|
|
|
|
|
2008-06-15 05:08:43 +05:30
|
|
|
if ( (lseek (fd, offset_uid, SEEK_SET) != offset_uid)
|
|
|
|
|| (write (fd, (char *) fl, sizeof *fl) != (ssize_t) sizeof *fl)
|
|
|
|
|| (close (fd) != 0)) {
|
|
|
|
SYSLOG ((LOG_WARN,
|
|
|
|
"Can't write faillog entry for UID %lu in %s.",
|
|
|
|
(unsigned long) uid, FAILLOG_FILE));
|
2008-06-16 00:46:34 +05:30
|
|
|
(void) close (fd);
|
2008-06-15 05:08:43 +05:30
|
|
|
}
|
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;
|
|
|
|
|
2008-06-14 00:01:13 +05:30
|
|
|
if ((0 == fl->fail_max) || (fl->fail_cnt < fl->fail_max)) {
|
2008-05-26 04:14:44 +05:30
|
|
|
return false;
|
2008-06-14 00:01:13 +05:30
|
|
|
}
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2008-06-14 00:01:13 +05:30
|
|
|
if (0 == fl->fail_locktime) {
|
2008-05-26 04:14:44 +05:30
|
|
|
return true; /* locked until reset manually */
|
2008-06-14 00:01:13 +05:30
|
|
|
}
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2008-06-14 00:01:13 +05:30
|
|
|
(void) 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 */
|
2008-06-14 00:01:13 +05:30
|
|
|
}
|
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;
|
2008-06-15 05:08:43 +05:30
|
|
|
off_t offset_uid = (off_t) (sizeof *fl) * uid;
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
/*
|
|
|
|
* Suppress the check if the log file isn't there.
|
|
|
|
*/
|
|
|
|
|
2008-06-15 05:08:43 +05:30
|
|
|
if (access (FAILLOG_FILE, F_OK) != 0) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
fd = open (FAILLOG_FILE, failed?O_RDONLY:O_RDWR);
|
2008-05-26 04:14:44 +05:30
|
|
|
if (fd < 0) {
|
2008-06-15 05:08:43 +05:30
|
|
|
SYSLOG ((LOG_WARN,
|
|
|
|
"Can't open the faillog file (%s) to check UID %lu. "
|
|
|
|
"User access authorized.",
|
|
|
|
FAILLOG_FILE, (unsigned long) uid));
|
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.
|
|
|
|
*/
|
|
|
|
|
2008-06-15 05:08:43 +05:30
|
|
|
if ( (lseek (fd, offset_uid, SEEK_SET) != offset_uid)
|
|
|
|
|| (read (fd, (char *) fl, sizeof *fl) != (ssize_t) sizeof *fl)) {
|
|
|
|
(void) 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)) {
|
2008-06-15 05:08:43 +05:30
|
|
|
(void) 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;
|
|
|
|
|
2008-06-15 05:08:43 +05:30
|
|
|
if ( (lseek (fd, offset_uid, SEEK_SET) != offset_uid)
|
|
|
|
|| (write (fd, (const void *) &fail, sizeof fail) != (ssize_t) sizeof fail)
|
|
|
|
|| (close (fd) != 0)) {
|
|
|
|
SYSLOG ((LOG_WARN,
|
|
|
|
"Can't reset faillog entry for UID %lu in %s.",
|
|
|
|
(unsigned long) uid, FAILLOG_FILE));
|
2008-06-16 00:46:34 +05:30
|
|
|
(void) close (fd);
|
2008-06-15 05:08:43 +05:30
|
|
|
}
|
|
|
|
} else {
|
|
|
|
(void) close (fd);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
2008-06-15 05:08:43 +05:30
|
|
|
|
2007-10-07 17:14:02 +05:30
|
|
|
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;
|
|
|
|
|
2008-06-14 00:01:13 +05:30
|
|
|
if (0 == fail->fail_cnt) {
|
2007-10-07 17:14:02 +05:30
|
|
|
return;
|
2008-06-14 00:01:13 +05:30
|
|
|
}
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
tp = localtime (&(fail->fail_time));
|
2008-06-14 00:01:13 +05:30
|
|
|
(void) 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
|
|
|
*/
|
2008-06-15 05:31:46 +05:30
|
|
|
(void) 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';
|
|
|
|
|
2008-06-14 00:01:13 +05:30
|
|
|
if ((NOW - fail->fail_time) < YEAR) {
|
2007-10-07 17:14:02 +05:30
|
|
|
lasttime[19] = '\0';
|
2008-06-14 00:01:13 +05:30
|
|
|
}
|
|
|
|
if ((NOW - fail->fail_time) < DAY) {
|
2007-10-07 17:14:02 +05:30
|
|
|
lasttime = lasttime + 11;
|
2008-06-14 00:01:13 +05:30
|
|
|
}
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2008-06-14 00:01:13 +05:30
|
|
|
if (' ' == *lasttime) {
|
2007-10-07 17:14:02 +05:30
|
|
|
lasttime++;
|
2008-06-14 00:01:13 +05:30
|
|
|
}
|
2007-10-07 17:14:02 +05:30
|
|
|
#endif
|
2008-06-15 05:31:46 +05:30
|
|
|
(void) 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",
|
|
|
|
(unsigned long) fail->fail_cnt),
|
|
|
|
fail->fail_cnt, lasttime, fail->fail_line);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2009-04-22 03:32:37 +05:30
|
|
|
* failtmp - update the cumulative failure log
|
2007-10-07 17:14:02 +05:30
|
|
|
*
|
|
|
|
* failtmp updates the (struct utmp) formatted failure log which
|
|
|
|
* maintains a record of all login failures.
|
|
|
|
*/
|
|
|
|
|
2009-04-20 17:07:41 +05:30
|
|
|
void failtmp (const char *username,
|
2009-04-29 00:44:50 +05:30
|
|
|
#ifdef USE_UTMPX
|
2007-10-07 17:16:25 +05:30
|
|
|
const struct utmpx *failent
|
2009-04-29 00:44:50 +05:30
|
|
|
#else /* !USE_UTMPX */
|
2007-10-07 17:16:25 +05:30
|
|
|
const struct utmp *failent
|
2009-04-29 00:44:50 +05:30
|
|
|
#endif /* !USE_UTMPX */
|
2007-10-07 17:16:25 +05:30
|
|
|
)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
* libmisc/console.c, libmisc/motd.c, libmisc/setupenv.c,
libmisc/sulog.c, libmisc/hushed.c, libmisc/failure.c,
libmisc/loginprompt.c, libmisc/ttytype.c,
libmisc/pam_pass_non_interractive.c, src/userdel.c, src/login.c,
lib/commonio.c, lib/commonio.h: Fix some const issues.
* libmisc/motd.c: Avoid multi-statements lines.
* libmisc/motd.c: Support long MOTD_FILE.
* libmisc/list.c, lib/prototypes.h: Revert previous change.
dup_list and is_on_list are used with members as defined for the
group structure, and thus even if the list is not modified, the
list elements cannot be constant strings.
* libmisc/system.c: Avoid C++ comments.
* src/vipw.c: WITH_TCB cannot be tested inside a gettextized
string. Split the Usage string.
* lib/commonio.h: Re-indent.
2010-08-21 21:02:53 +05:30
|
|
|
const char *ftmp;
|
2007-10-07 17:14:02 +05:30
|
|
|
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-06-15 05:08:43 +05:30
|
|
|
if (access (ftmp, F_OK) != 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-05-26 04:14:44 +05:30
|
|
|
fd = open (ftmp, O_WRONLY | O_APPEND);
|
|
|
|
if (-1 == fd) {
|
2008-06-15 05:08:43 +05:30
|
|
|
SYSLOG ((LOG_WARN,
|
2008-06-15 05:31:46 +05:30
|
|
|
"Can't append failure of user %s to %s.",
|
2009-04-20 17:07:41 +05:30
|
|
|
username, 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
|
|
|
|
|
|
|
/*
|
2008-06-15 05:08:43 +05:30
|
|
|
* Append the new failure record and close the log file.
|
2007-10-07 17:14:02 +05:30
|
|
|
*/
|
|
|
|
|
2008-06-15 05:08:43 +05:30
|
|
|
if ( (write (fd, (const void *) failent, sizeof *failent) != (ssize_t) sizeof *failent)
|
|
|
|
|| (close (fd) != 0)) {
|
|
|
|
SYSLOG ((LOG_WARN,
|
2008-06-15 05:31:46 +05:30
|
|
|
"Can't append failure of user %s to %s.",
|
2009-04-20 17:07:41 +05:30
|
|
|
username, ftmp));
|
2008-06-16 00:46:34 +05:30
|
|
|
(void) close (fd);
|
2008-06-15 05:08:43 +05:30
|
|
|
}
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
2008-05-26 04:14:44 +05:30
|
|
|
|