2000-06-03 16:57:40 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2000 Andre Lucas. All rights reserved.
|
2000-06-04 19:07:49 +02:00
|
|
|
* Portions copyright (c) 1998 Todd C. Miller
|
|
|
|
* Portions copyright (c) 1996 Jason Downs
|
|
|
|
* Portions copyright (c) 1996 Theo de Raadt
|
2000-06-03 16:57:40 +02:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.
|
|
|
|
*/
|
|
|
|
|
2005-02-02 13:30:24 +01:00
|
|
|
/*
|
|
|
|
* The btmp logging code is derived from login.c from util-linux and is under
|
|
|
|
* the the following license:
|
|
|
|
*
|
|
|
|
* Copyright (c) 1980, 1987, 1988 The Regents of the University of California.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms are permitted
|
|
|
|
* provided that the above copyright notice and this paragraph are
|
|
|
|
* duplicated in all such forms and that any documentation,
|
|
|
|
* advertising materials, and other materials related to such
|
|
|
|
* distribution and use acknowledge that the software was developed
|
|
|
|
* by the University of California, Berkeley. The name of the
|
|
|
|
* University may not be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
|
|
|
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2001-02-05 13:42:17 +01:00
|
|
|
/**
|
2000-06-03 16:57:40 +02:00
|
|
|
** loginrec.c: platform-independent login recording and lastlog retrieval
|
|
|
|
**/
|
|
|
|
|
2000-06-04 19:07:49 +02:00
|
|
|
/*
|
2004-09-12 07:18:55 +02:00
|
|
|
* The new login code explained
|
|
|
|
* ============================
|
|
|
|
*
|
|
|
|
* This code attempts to provide a common interface to login recording
|
|
|
|
* (utmp and friends) and last login time retrieval.
|
|
|
|
*
|
|
|
|
* Its primary means of achieving this is to use 'struct logininfo', a
|
|
|
|
* union of all the useful fields in the various different types of
|
|
|
|
* system login record structures one finds on UNIX variants.
|
|
|
|
*
|
|
|
|
* We depend on autoconf to define which recording methods are to be
|
|
|
|
* used, and which fields are contained in the relevant data structures
|
|
|
|
* on the local system. Many C preprocessor symbols affect which code
|
|
|
|
* gets compiled here.
|
|
|
|
*
|
|
|
|
* The code is designed to make it easy to modify a particular
|
|
|
|
* recording method, without affecting other methods nor requiring so
|
|
|
|
* many nested conditional compilation blocks as were commonplace in
|
|
|
|
* the old code.
|
|
|
|
*
|
|
|
|
* For login recording, we try to use the local system's libraries as
|
|
|
|
* these are clearly most likely to work correctly. For utmp systems
|
|
|
|
* this usually means login() and logout() or setutent() etc., probably
|
|
|
|
* in libutil, along with logwtmp() etc. On these systems, we fall back
|
|
|
|
* to writing the files directly if we have to, though this method
|
|
|
|
* requires very thorough testing so we do not corrupt local auditing
|
|
|
|
* information. These files and their access methods are very system
|
|
|
|
* specific indeed.
|
|
|
|
*
|
|
|
|
* For utmpx systems, the corresponding library functions are
|
|
|
|
* setutxent() etc. To the author's knowledge, all utmpx systems have
|
|
|
|
* these library functions and so no direct write is attempted. If such
|
|
|
|
* a system exists and needs support, direct analogues of the [uw]tmp
|
|
|
|
* code should suffice.
|
|
|
|
*
|
|
|
|
* Retrieving the time of last login ('lastlog') is in some ways even
|
|
|
|
* more problemmatic than login recording. Some systems provide a
|
|
|
|
* simple table of all users which we seek based on uid and retrieve a
|
|
|
|
* relatively standard structure. Others record the same information in
|
|
|
|
* a directory with a separate file, and others don't record the
|
|
|
|
* information separately at all. For systems in the latter category,
|
|
|
|
* we look backwards in the wtmp or wtmpx file for the last login entry
|
|
|
|
* for our user. Naturally this is slower and on busy systems could
|
|
|
|
* incur a significant performance penalty.
|
|
|
|
*
|
|
|
|
* Calling the new code
|
|
|
|
* --------------------
|
|
|
|
*
|
|
|
|
* In OpenSSH all login recording and retrieval is performed in
|
|
|
|
* login.c. Here you'll find working examples. Also, in the logintest.c
|
|
|
|
* program there are more examples.
|
|
|
|
*
|
|
|
|
* Internal handler calling method
|
|
|
|
* -------------------------------
|
|
|
|
*
|
|
|
|
* When a call is made to login_login() or login_logout(), both
|
|
|
|
* routines set a struct logininfo flag defining which action (log in,
|
|
|
|
* or log out) is to be taken. They both then call login_write(), which
|
|
|
|
* calls whichever of the many structure-specific handlers autoconf
|
|
|
|
* selects for the local system.
|
|
|
|
*
|
|
|
|
* The handlers themselves handle system data structure specifics. Both
|
|
|
|
* struct utmp and struct utmpx have utility functions (see
|
|
|
|
* construct_utmp*()) to try to make it simpler to add extra systems
|
|
|
|
* that introduce new features to either structure.
|
|
|
|
*
|
|
|
|
* While it may seem terribly wasteful to replicate so much similar
|
|
|
|
* code for each method, experience has shown that maintaining code to
|
|
|
|
* write both struct utmp and utmpx in one function, whilst maintaining
|
|
|
|
* support for all systems whether they have library support or not, is
|
|
|
|
* a difficult and time-consuming task.
|
|
|
|
*
|
|
|
|
* Lastlog support proceeds similarly. Functions login_get_lastlog()
|
|
|
|
* (and its OpenSSH-tuned friend login_get_lastlog_time()) call
|
|
|
|
* getlast_entry(), which tries one of three methods to find the last
|
|
|
|
* login time. It uses local system lastlog support if it can,
|
|
|
|
* otherwise it tries wtmp or wtmpx before giving up and returning 0,
|
|
|
|
* meaning "tilt".
|
|
|
|
*
|
|
|
|
* Maintenance
|
|
|
|
* -----------
|
|
|
|
*
|
|
|
|
* In many cases it's possible to tweak autoconf to select the correct
|
|
|
|
* methods for a particular platform, either by improving the detection
|
|
|
|
* code (best), or by presetting DISABLE_<method> or CONF_<method>_FILE
|
|
|
|
* symbols for the platform.
|
|
|
|
*
|
|
|
|
* Use logintest to check which symbols are defined before modifying
|
|
|
|
* configure.ac and loginrec.c. (You have to build logintest yourself
|
|
|
|
* with 'make logintest' as it's not built by default.)
|
|
|
|
*
|
|
|
|
* Otherwise, patches to the specific method(s) are very helpful!
|
|
|
|
*/
|
2000-06-03 16:57:40 +02:00
|
|
|
|
|
|
|
#include "includes.h"
|
|
|
|
|
2006-03-15 04:01:11 +01:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
2006-07-10 12:35:38 +02:00
|
|
|
#include <sys/socket.h>
|
2015-12-15 03:59:12 +01:00
|
|
|
#ifdef HAVE_SYS_TIME_H
|
|
|
|
# include <sys/time.h>
|
|
|
|
#endif
|
2006-07-10 12:35:38 +02:00
|
|
|
|
|
|
|
#include <netinet/in.h>
|
2006-03-15 04:01:11 +01:00
|
|
|
|
2019-09-06 06:53:27 +02:00
|
|
|
#include <stdlib.h>
|
2006-07-12 14:40:50 +02:00
|
|
|
#include <errno.h>
|
2006-07-10 13:33:04 +02:00
|
|
|
#include <fcntl.h>
|
2006-09-07 14:57:53 +02:00
|
|
|
#ifdef HAVE_PATHS_H
|
|
|
|
# include <paths.h>
|
|
|
|
#endif
|
2006-07-10 12:53:08 +02:00
|
|
|
#include <pwd.h>
|
2006-09-01 07:38:36 +02:00
|
|
|
#include <stdarg.h>
|
2019-07-23 14:25:44 +02:00
|
|
|
#include <stdio.h>
|
2006-07-24 06:51:00 +02:00
|
|
|
#include <string.h>
|
2007-04-29 04:10:57 +02:00
|
|
|
#include <time.h>
|
2006-07-24 06:51:00 +02:00
|
|
|
#include <unistd.h>
|
2006-07-10 12:53:08 +02:00
|
|
|
|
2000-06-03 16:57:40 +02:00
|
|
|
#include "xmalloc.h"
|
2018-07-11 20:53:29 +02:00
|
|
|
#include "sshkey.h"
|
2006-08-05 04:39:39 +02:00
|
|
|
#include "hostfile.h"
|
|
|
|
#include "ssh.h"
|
2000-06-03 16:57:40 +02:00
|
|
|
#include "loginrec.h"
|
2001-01-22 06:34:40 +01:00
|
|
|
#include "log.h"
|
|
|
|
#include "atomicio.h"
|
2005-02-02 13:30:24 +01:00
|
|
|
#include "packet.h"
|
|
|
|
#include "canohost.h"
|
2005-02-02 14:20:53 +01:00
|
|
|
#include "auth.h"
|
2018-07-10 11:39:52 +02:00
|
|
|
#include "sshbuf.h"
|
|
|
|
#include "ssherr.h"
|
2000-06-03 16:57:40 +02:00
|
|
|
|
2000-11-10 04:28:31 +01:00
|
|
|
#ifdef HAVE_UTIL_H
|
2004-09-12 07:18:55 +02:00
|
|
|
# include <util.h>
|
2000-11-10 04:28:31 +01:00
|
|
|
#endif
|
2000-06-03 16:57:40 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
** prototypes for helper functions in this file
|
|
|
|
**/
|
|
|
|
|
|
|
|
#if HAVE_UTMP_H
|
|
|
|
void set_utmp_time(struct logininfo *li, struct utmp *ut);
|
|
|
|
void construct_utmp(struct logininfo *li, struct utmp *ut);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_UTMPX_H
|
|
|
|
void set_utmpx_time(struct logininfo *li, struct utmpx *ut);
|
|
|
|
void construct_utmpx(struct logininfo *li, struct utmpx *ut);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
int utmp_write_entry(struct logininfo *li);
|
|
|
|
int utmpx_write_entry(struct logininfo *li);
|
|
|
|
int wtmp_write_entry(struct logininfo *li);
|
|
|
|
int wtmpx_write_entry(struct logininfo *li);
|
|
|
|
int lastlog_write_entry(struct logininfo *li);
|
|
|
|
int syslogin_write_entry(struct logininfo *li);
|
|
|
|
|
|
|
|
int getlast_entry(struct logininfo *li);
|
|
|
|
int lastlog_get_entry(struct logininfo *li);
|
2010-04-09 10:13:27 +02:00
|
|
|
int utmpx_get_entry(struct logininfo *li);
|
2000-06-03 16:57:40 +02:00
|
|
|
int wtmp_get_entry(struct logininfo *li);
|
|
|
|
int wtmpx_get_entry(struct logininfo *li);
|
|
|
|
|
2018-07-10 11:39:52 +02:00
|
|
|
extern struct sshbuf *loginmsg;
|
2005-02-15 11:45:57 +01:00
|
|
|
|
2000-06-19 10:20:03 +02:00
|
|
|
/* pick the shortest string */
|
2004-09-12 07:18:55 +02:00
|
|
|
#define MIN_SIZEOF(s1,s2) (sizeof(s1) < sizeof(s2) ? sizeof(s1) : sizeof(s2))
|
2000-06-19 10:20:03 +02:00
|
|
|
|
2000-06-03 16:57:40 +02:00
|
|
|
/**
|
|
|
|
** platform-independent login functions
|
|
|
|
**/
|
|
|
|
|
2004-09-12 07:18:55 +02:00
|
|
|
/*
|
|
|
|
* login_login(struct logininfo *) - Record a login
|
2001-02-05 13:42:17 +01:00
|
|
|
*
|
2000-06-19 10:20:03 +02:00
|
|
|
* Call with a pointer to a struct logininfo initialised with
|
|
|
|
* login_init_entry() or login_alloc_entry()
|
|
|
|
*
|
|
|
|
* Returns:
|
|
|
|
* >0 if successful
|
|
|
|
* 0 on failure (will use OpenSSH's logging facilities for diagnostics)
|
|
|
|
*/
|
2000-06-04 19:07:49 +02:00
|
|
|
int
|
2004-09-12 07:18:55 +02:00
|
|
|
login_login(struct logininfo *li)
|
2000-06-04 19:07:49 +02:00
|
|
|
{
|
|
|
|
li->type = LTYPE_LOGIN;
|
2004-09-12 07:18:55 +02:00
|
|
|
return (login_write(li));
|
2000-06-04 19:07:49 +02:00
|
|
|
}
|
2000-06-03 16:57:40 +02:00
|
|
|
|
|
|
|
|
2004-09-12 07:18:55 +02:00
|
|
|
/*
|
|
|
|
* login_logout(struct logininfo *) - Record a logout
|
2000-06-19 10:20:03 +02:00
|
|
|
*
|
|
|
|
* Call as with login_login()
|
|
|
|
*
|
|
|
|
* Returns:
|
|
|
|
* >0 if successful
|
|
|
|
* 0 on failure (will use OpenSSH's logging facilities for diagnostics)
|
|
|
|
*/
|
2000-06-04 19:07:49 +02:00
|
|
|
int
|
|
|
|
login_logout(struct logininfo *li)
|
|
|
|
{
|
|
|
|
li->type = LTYPE_LOGOUT;
|
2004-09-12 07:18:55 +02:00
|
|
|
return (login_write(li));
|
2000-06-03 16:57:40 +02:00
|
|
|
}
|
|
|
|
|
2004-09-12 07:18:55 +02:00
|
|
|
/*
|
|
|
|
* login_get_lastlog_time(int) - Retrieve the last login time
|
2000-06-19 10:20:03 +02:00
|
|
|
*
|
|
|
|
* Retrieve the last login time for the given uid. Will try to use the
|
|
|
|
* system lastlog facilities if they are available, but will fall back
|
|
|
|
* to looking in wtmp/wtmpx if necessary
|
|
|
|
*
|
|
|
|
* Returns:
|
|
|
|
* 0 on failure, or if user has never logged in
|
|
|
|
* Time in seconds from the epoch if successful
|
|
|
|
*
|
|
|
|
* Useful preprocessor symbols:
|
|
|
|
* DISABLE_LASTLOG: If set, *never* even try to retrieve lastlog
|
|
|
|
* info
|
|
|
|
* USE_LASTLOG: If set, indicates the presence of system lastlog
|
|
|
|
* facilities. If this and DISABLE_LASTLOG are not set,
|
|
|
|
* try to retrieve lastlog information from wtmp/wtmpx.
|
|
|
|
*/
|
2000-06-04 19:07:49 +02:00
|
|
|
unsigned int
|
2010-11-05 00:52:37 +01:00
|
|
|
login_get_lastlog_time(const uid_t uid)
|
2000-06-04 19:07:49 +02:00
|
|
|
{
|
|
|
|
struct logininfo li;
|
2000-06-03 16:57:40 +02:00
|
|
|
|
2000-06-19 10:20:03 +02:00
|
|
|
if (login_get_lastlog(&li, uid))
|
2004-09-12 07:18:55 +02:00
|
|
|
return (li.tv_sec);
|
2000-06-19 10:20:03 +02:00
|
|
|
else
|
2004-09-12 07:18:55 +02:00
|
|
|
return (0);
|
2000-06-04 19:07:49 +02:00
|
|
|
}
|
2000-06-03 16:57:40 +02:00
|
|
|
|
2004-09-12 07:18:55 +02:00
|
|
|
/*
|
|
|
|
* login_get_lastlog(struct logininfo *, int) - Retrieve a lastlog entry
|
2000-06-19 10:20:03 +02:00
|
|
|
*
|
|
|
|
* Retrieve a logininfo structure populated (only partially) with
|
|
|
|
* information from the system lastlog data, or from wtmp/wtmpx if no
|
|
|
|
* system lastlog information exists.
|
|
|
|
*
|
|
|
|
* Note this routine must be given a pre-allocated logininfo.
|
|
|
|
*
|
|
|
|
* Returns:
|
|
|
|
* >0: A pointer to your struct logininfo if successful
|
|
|
|
* 0 on failure (will use OpenSSH's logging facilities for diagnostics)
|
|
|
|
*/
|
2000-06-04 19:07:49 +02:00
|
|
|
struct logininfo *
|
2010-11-05 00:52:37 +01:00
|
|
|
login_get_lastlog(struct logininfo *li, const uid_t uid)
|
2000-06-04 19:07:49 +02:00
|
|
|
{
|
2000-06-19 10:20:03 +02:00
|
|
|
struct passwd *pw;
|
|
|
|
|
2000-08-15 02:01:22 +02:00
|
|
|
memset(li, '\0', sizeof(*li));
|
2000-06-03 16:57:40 +02:00
|
|
|
li->uid = uid;
|
2000-06-19 10:20:03 +02:00
|
|
|
|
2001-02-05 13:42:17 +01:00
|
|
|
/*
|
2000-06-27 16:50:50 +02:00
|
|
|
* If we don't have a 'real' lastlog, we need the username to
|
2000-06-19 10:20:03 +02:00
|
|
|
* reliably search wtmp(x) for the last login (see
|
2001-02-05 13:42:17 +01:00
|
|
|
* wtmp_get_entry().)
|
2000-06-27 16:50:50 +02:00
|
|
|
*/
|
2000-06-19 10:20:03 +02:00
|
|
|
pw = getpwuid(uid);
|
2000-06-27 03:18:27 +02:00
|
|
|
if (pw == NULL)
|
2010-11-05 00:52:37 +01:00
|
|
|
fatal("%s: Cannot find account for uid %ld", __func__,
|
|
|
|
(long)uid);
|
2001-02-05 13:42:17 +01:00
|
|
|
|
2013-12-29 07:40:18 +01:00
|
|
|
if (strlcpy(li->username, pw->pw_name, sizeof(li->username)) >=
|
|
|
|
sizeof(li->username)) {
|
|
|
|
error("%s: username too long (%lu > max %lu)", __func__,
|
2014-01-17 02:23:23 +01:00
|
|
|
(unsigned long)strlen(pw->pw_name),
|
|
|
|
(unsigned long)sizeof(li->username) - 1);
|
2013-12-29 07:40:18 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
2000-06-27 03:18:27 +02:00
|
|
|
|
2000-06-04 19:07:49 +02:00
|
|
|
if (getlast_entry(li))
|
2004-09-12 07:18:55 +02:00
|
|
|
return (li);
|
2000-06-04 19:07:49 +02:00
|
|
|
else
|
2004-09-12 07:18:55 +02:00
|
|
|
return (NULL);
|
2000-06-03 16:57:40 +02:00
|
|
|
}
|
|
|
|
|
2004-09-12 07:18:55 +02:00
|
|
|
/*
|
|
|
|
* login_alloc_entry(int, char*, char*, char*) - Allocate and initialise
|
2001-02-05 13:42:17 +01:00
|
|
|
* a logininfo structure
|
|
|
|
*
|
2000-06-19 10:20:03 +02:00
|
|
|
* This function creates a new struct logininfo, a data structure
|
|
|
|
* meant to carry the information required to portably record login info.
|
|
|
|
*
|
|
|
|
* Returns a pointer to a newly created struct logininfo. If memory
|
|
|
|
* allocation fails, the program halts.
|
|
|
|
*/
|
2000-06-04 19:07:49 +02:00
|
|
|
struct
|
2010-11-05 00:52:37 +01:00
|
|
|
logininfo *login_alloc_entry(pid_t pid, const char *username,
|
2004-09-12 07:18:55 +02:00
|
|
|
const char *hostname, const char *line)
|
2000-06-04 19:07:49 +02:00
|
|
|
{
|
|
|
|
struct logininfo *newli;
|
2000-06-03 16:57:40 +02:00
|
|
|
|
2004-09-12 07:18:55 +02:00
|
|
|
newli = xmalloc(sizeof(*newli));
|
|
|
|
login_init_entry(newli, pid, username, hostname, line);
|
|
|
|
return (newli);
|
2000-06-03 16:57:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-06-19 10:20:03 +02:00
|
|
|
/* login_free_entry(struct logininfo *) - free struct memory */
|
2000-06-03 16:57:40 +02:00
|
|
|
void
|
2000-06-04 19:07:49 +02:00
|
|
|
login_free_entry(struct logininfo *li)
|
|
|
|
{
|
2013-06-02 00:07:31 +02:00
|
|
|
free(li);
|
2000-06-03 16:57:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-06-19 10:20:03 +02:00
|
|
|
/* login_init_entry(struct logininfo *, int, char*, char*, char*)
|
|
|
|
* - initialise a struct logininfo
|
2001-02-05 13:42:17 +01:00
|
|
|
*
|
2000-06-19 10:20:03 +02:00
|
|
|
* Populates a new struct logininfo, a data structure meant to carry
|
|
|
|
* the information required to portably record login info.
|
|
|
|
*
|
|
|
|
* Returns: 1
|
|
|
|
*/
|
2000-06-04 19:07:49 +02:00
|
|
|
int
|
2010-11-05 00:52:37 +01:00
|
|
|
login_init_entry(struct logininfo *li, pid_t pid, const char *username,
|
2004-09-12 07:18:55 +02:00
|
|
|
const char *hostname, const char *line)
|
2000-06-04 19:07:49 +02:00
|
|
|
{
|
2000-06-27 01:40:06 +02:00
|
|
|
struct passwd *pw;
|
2001-02-05 13:42:17 +01:00
|
|
|
|
2000-08-15 02:01:22 +02:00
|
|
|
memset(li, 0, sizeof(*li));
|
2001-02-05 13:42:17 +01:00
|
|
|
|
2000-06-04 19:07:49 +02:00
|
|
|
li->pid = pid;
|
2000-06-27 01:40:06 +02:00
|
|
|
|
2000-06-04 19:07:49 +02:00
|
|
|
/* set the line information */
|
|
|
|
if (line)
|
|
|
|
line_fullname(li->line, line, sizeof(li->line));
|
2000-06-03 16:57:40 +02:00
|
|
|
|
2000-06-27 01:40:06 +02:00
|
|
|
if (username) {
|
2000-06-04 19:07:49 +02:00
|
|
|
strlcpy(li->username, username, sizeof(li->username));
|
2000-06-27 01:40:06 +02:00
|
|
|
pw = getpwnam(li->username);
|
2004-09-12 07:18:55 +02:00
|
|
|
if (pw == NULL) {
|
2005-07-17 09:04:47 +02:00
|
|
|
fatal("%s: Cannot find user \"%s\"", __func__,
|
2004-09-12 07:18:55 +02:00
|
|
|
li->username);
|
|
|
|
}
|
2000-06-27 01:40:06 +02:00
|
|
|
li->uid = pw->pw_uid;
|
|
|
|
}
|
2000-06-27 03:18:27 +02:00
|
|
|
|
2000-06-04 19:07:49 +02:00
|
|
|
if (hostname)
|
|
|
|
strlcpy(li->hostname, hostname, sizeof(li->hostname));
|
2000-06-27 01:40:06 +02:00
|
|
|
|
2004-09-12 07:18:55 +02:00
|
|
|
return (1);
|
2000-06-03 16:57:40 +02:00
|
|
|
}
|
|
|
|
|
2005-07-17 09:04:47 +02:00
|
|
|
/*
|
2004-09-12 07:18:55 +02:00
|
|
|
* login_set_current_time(struct logininfo *) - set the current time
|
2000-06-19 10:20:03 +02:00
|
|
|
*
|
|
|
|
* Set the current time in a logininfo structure. This function is
|
|
|
|
* meant to eliminate the need to deal with system dependencies for
|
|
|
|
* time handling.
|
|
|
|
*/
|
2000-06-03 16:57:40 +02:00
|
|
|
void
|
2000-06-04 19:07:49 +02:00
|
|
|
login_set_current_time(struct logininfo *li)
|
|
|
|
{
|
2000-06-03 16:57:40 +02:00
|
|
|
struct timeval tv;
|
|
|
|
|
|
|
|
gettimeofday(&tv, NULL);
|
2001-02-05 13:42:17 +01:00
|
|
|
|
2000-06-27 01:40:06 +02:00
|
|
|
li->tv_sec = tv.tv_sec;
|
|
|
|
li->tv_usec = tv.tv_usec;
|
2000-06-03 16:57:40 +02:00
|
|
|
}
|
|
|
|
|
2000-06-04 19:07:49 +02:00
|
|
|
/* copy a sockaddr_* into our logininfo */
|
2000-06-03 16:57:40 +02:00
|
|
|
void
|
2000-06-04 19:07:49 +02:00
|
|
|
login_set_addr(struct logininfo *li, const struct sockaddr *sa,
|
2004-09-12 07:18:55 +02:00
|
|
|
const unsigned int sa_size)
|
2000-06-04 19:07:49 +02:00
|
|
|
{
|
|
|
|
unsigned int bufsize = sa_size;
|
|
|
|
|
|
|
|
/* make sure we don't overrun our union */
|
|
|
|
if (sizeof(li->hostaddr) < sa_size)
|
|
|
|
bufsize = sizeof(li->hostaddr);
|
|
|
|
|
2004-09-12 07:18:55 +02:00
|
|
|
memcpy(&li->hostaddr.sa, sa, bufsize);
|
2000-06-03 16:57:40 +02:00
|
|
|
}
|
|
|
|
|
2000-06-04 19:07:49 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
** login_write: Call low-level recording functions based on autoconf
|
|
|
|
** results
|
|
|
|
**/
|
2000-06-03 16:57:40 +02:00
|
|
|
int
|
2004-09-12 07:18:55 +02:00
|
|
|
login_write(struct logininfo *li)
|
2000-06-04 19:07:49 +02:00
|
|
|
{
|
2000-09-05 07:13:06 +02:00
|
|
|
#ifndef HAVE_CYGWIN
|
2004-09-12 07:18:55 +02:00
|
|
|
if (geteuid() != 0) {
|
|
|
|
logit("Attempt to write login records by non-root user (aborting)");
|
|
|
|
return (1);
|
2000-06-03 16:57:40 +02:00
|
|
|
}
|
2000-09-05 07:13:06 +02:00
|
|
|
#endif
|
2000-06-27 03:18:27 +02:00
|
|
|
|
2000-06-03 16:57:40 +02:00
|
|
|
/* set the timestamp */
|
|
|
|
login_set_current_time(li);
|
|
|
|
#ifdef USE_LOGIN
|
|
|
|
syslogin_write_entry(li);
|
|
|
|
#endif
|
|
|
|
#ifdef USE_LASTLOG
|
2004-09-12 07:18:55 +02:00
|
|
|
if (li->type == LTYPE_LOGIN)
|
2000-06-03 16:57:40 +02:00
|
|
|
lastlog_write_entry(li);
|
|
|
|
#endif
|
|
|
|
#ifdef USE_UTMP
|
|
|
|
utmp_write_entry(li);
|
|
|
|
#endif
|
|
|
|
#ifdef USE_WTMP
|
|
|
|
wtmp_write_entry(li);
|
|
|
|
#endif
|
|
|
|
#ifdef USE_UTMPX
|
|
|
|
utmpx_write_entry(li);
|
|
|
|
#endif
|
|
|
|
#ifdef USE_WTMPX
|
|
|
|
wtmpx_write_entry(li);
|
2004-08-14 16:09:11 +02:00
|
|
|
#endif
|
|
|
|
#ifdef CUSTOM_SYS_AUTH_RECORD_LOGIN
|
2005-07-17 09:04:47 +02:00
|
|
|
if (li->type == LTYPE_LOGIN &&
|
2005-07-17 09:26:43 +02:00
|
|
|
!sys_auth_record_login(li->username,li->hostname,li->line,
|
2018-12-13 05:12:45 +01:00
|
|
|
loginmsg))
|
2004-08-14 16:09:11 +02:00
|
|
|
logit("Writing login record failed for %s", li->username);
|
2005-02-02 14:20:53 +01:00
|
|
|
#endif
|
2005-02-08 11:52:47 +01:00
|
|
|
#ifdef SSH_AUDIT_EVENTS
|
2005-02-02 14:20:53 +01:00
|
|
|
if (li->type == LTYPE_LOGIN)
|
2011-01-17 11:15:27 +01:00
|
|
|
audit_session_open(li);
|
2005-02-02 14:20:53 +01:00
|
|
|
else if (li->type == LTYPE_LOGOUT)
|
2011-01-17 11:15:27 +01:00
|
|
|
audit_session_close(li);
|
2000-06-03 16:57:40 +02:00
|
|
|
#endif
|
2004-09-12 07:18:55 +02:00
|
|
|
return (0);
|
2000-06-03 16:57:40 +02:00
|
|
|
}
|
|
|
|
|
2001-05-08 22:33:05 +02:00
|
|
|
#ifdef LOGIN_NEEDS_UTMPX
|
|
|
|
int
|
|
|
|
login_utmp_only(struct logininfo *li)
|
|
|
|
{
|
2003-11-21 13:48:55 +01:00
|
|
|
li->type = LTYPE_LOGIN;
|
2001-10-26 17:56:55 +02:00
|
|
|
login_set_current_time(li);
|
2001-05-08 22:33:05 +02:00
|
|
|
# ifdef USE_UTMP
|
|
|
|
utmp_write_entry(li);
|
|
|
|
# endif
|
|
|
|
# ifdef USE_WTMP
|
|
|
|
wtmp_write_entry(li);
|
|
|
|
# endif
|
|
|
|
# ifdef USE_UTMPX
|
|
|
|
utmpx_write_entry(li);
|
|
|
|
# endif
|
|
|
|
# ifdef USE_WTMPX
|
|
|
|
wtmpx_write_entry(li);
|
|
|
|
# endif
|
2004-09-12 07:18:55 +02:00
|
|
|
return (0);
|
2001-05-08 22:33:05 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2000-06-04 19:07:49 +02:00
|
|
|
/**
|
|
|
|
** getlast_entry: Call low-level functions to retrieve the last login
|
|
|
|
** time.
|
|
|
|
**/
|
2000-06-03 16:57:40 +02:00
|
|
|
|
2000-06-04 19:07:49 +02:00
|
|
|
/* take the uid in li and return the last login time */
|
2000-06-03 16:57:40 +02:00
|
|
|
int
|
2000-06-04 19:07:49 +02:00
|
|
|
getlast_entry(struct logininfo *li)
|
|
|
|
{
|
|
|
|
#ifdef USE_LASTLOG
|
2000-06-27 16:50:50 +02:00
|
|
|
return(lastlog_get_entry(li));
|
2000-06-27 03:18:27 +02:00
|
|
|
#else /* !USE_LASTLOG */
|
2010-04-09 10:13:27 +02:00
|
|
|
#if defined(USE_UTMPX) && defined(HAVE_SETUTXDB) && \
|
|
|
|
defined(UTXDB_LASTLOGIN) && defined(HAVE_GETUTXUSER)
|
|
|
|
return (utmpx_get_entry(li));
|
|
|
|
#endif
|
2000-06-03 16:57:40 +02:00
|
|
|
|
2004-09-12 07:18:55 +02:00
|
|
|
#if defined(DISABLE_LASTLOG)
|
2001-02-05 13:42:17 +01:00
|
|
|
/* On some systems we shouldn't even try to obtain last login
|
2000-06-13 00:21:44 +02:00
|
|
|
* time, e.g. AIX */
|
2004-09-12 07:18:55 +02:00
|
|
|
return (0);
|
|
|
|
# elif defined(USE_WTMP) && \
|
|
|
|
(defined(HAVE_TIME_IN_UTMP) || defined(HAVE_TV_IN_UTMP))
|
2000-06-04 19:07:49 +02:00
|
|
|
/* retrieve last login time from utmp */
|
2000-06-27 03:18:27 +02:00
|
|
|
return (wtmp_get_entry(li));
|
2004-09-12 07:18:55 +02:00
|
|
|
# elif defined(USE_WTMPX) && \
|
|
|
|
(defined(HAVE_TIME_IN_UTMPX) || defined(HAVE_TV_IN_UTMPX))
|
2000-06-04 19:07:49 +02:00
|
|
|
/* If wtmp isn't available, try wtmpx */
|
2000-06-27 03:18:27 +02:00
|
|
|
return (wtmpx_get_entry(li));
|
2004-09-12 07:18:55 +02:00
|
|
|
# else
|
2000-06-04 19:07:49 +02:00
|
|
|
/* Give up: No means of retrieving last login time */
|
2004-09-12 07:18:55 +02:00
|
|
|
return (0);
|
2001-02-05 13:42:17 +01:00
|
|
|
# endif /* DISABLE_LASTLOG */
|
2000-06-27 03:18:27 +02:00
|
|
|
#endif /* USE_LASTLOG */
|
2000-06-04 19:07:49 +02:00
|
|
|
}
|
2000-06-03 16:57:40 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2000-06-04 19:07:49 +02:00
|
|
|
* 'line' string utility functions
|
|
|
|
*
|
|
|
|
* These functions process the 'line' string into one of three forms:
|
|
|
|
*
|
2000-06-03 16:57:40 +02:00
|
|
|
* 1. The full filename (including '/dev')
|
|
|
|
* 2. The stripped name (excluding '/dev')
|
2000-06-04 19:07:49 +02:00
|
|
|
* 3. The abbreviated name (e.g. /dev/ttyp00 -> yp00
|
|
|
|
* /dev/pts/1 -> ts/1 )
|
2000-06-03 16:57:40 +02:00
|
|
|
*
|
|
|
|
* Form 3 is used on some systems to identify a .tmp.? entry when
|
|
|
|
* attempting to remove it. Typically both addition and removal is
|
2000-06-04 19:07:49 +02:00
|
|
|
* performed by one application - say, sshd - so as long as the choice
|
|
|
|
* uniquely identifies a terminal it's ok.
|
2000-06-03 16:57:40 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
2004-09-12 07:18:55 +02:00
|
|
|
/*
|
|
|
|
* line_fullname(): add the leading '/dev/' if it doesn't exist make
|
|
|
|
* sure dst has enough space, if not just copy src (ugh)
|
|
|
|
*/
|
2000-06-03 16:57:40 +02:00
|
|
|
char *
|
2005-06-19 02:19:43 +02:00
|
|
|
line_fullname(char *dst, const char *src, u_int dstsize)
|
2000-06-04 19:07:49 +02:00
|
|
|
{
|
2000-06-03 16:57:40 +02:00
|
|
|
memset(dst, '\0', dstsize);
|
2004-09-12 07:18:55 +02:00
|
|
|
if ((strncmp(src, "/dev/", 5) == 0) || (dstsize < (strlen(src) + 5)))
|
2000-06-03 16:57:40 +02:00
|
|
|
strlcpy(dst, src, dstsize);
|
2004-09-12 07:18:55 +02:00
|
|
|
else {
|
2000-06-13 13:23:17 +02:00
|
|
|
strlcpy(dst, "/dev/", dstsize);
|
2000-06-03 16:57:40 +02:00
|
|
|
strlcat(dst, src, dstsize);
|
|
|
|
}
|
2004-09-12 07:18:55 +02:00
|
|
|
return (dst);
|
2000-06-03 16:57:40 +02:00
|
|
|
}
|
|
|
|
|
2000-06-04 19:07:49 +02:00
|
|
|
/* line_stripname(): strip the leading '/dev' if it exists, return dst */
|
2000-06-03 16:57:40 +02:00
|
|
|
char *
|
2000-06-04 19:07:49 +02:00
|
|
|
line_stripname(char *dst, const char *src, int dstsize)
|
|
|
|
{
|
2000-06-03 16:57:40 +02:00
|
|
|
memset(dst, '\0', dstsize);
|
|
|
|
if (strncmp(src, "/dev/", 5) == 0)
|
2000-09-30 12:34:44 +02:00
|
|
|
strlcpy(dst, src + 5, dstsize);
|
2000-06-03 16:57:40 +02:00
|
|
|
else
|
|
|
|
strlcpy(dst, src, dstsize);
|
2004-09-12 07:18:55 +02:00
|
|
|
return (dst);
|
2000-06-04 19:07:49 +02:00
|
|
|
}
|
|
|
|
|
2005-07-17 09:04:47 +02:00
|
|
|
/*
|
2004-09-12 07:18:55 +02:00
|
|
|
* line_abbrevname(): Return the abbreviated (usually four-character)
|
2000-06-04 19:07:49 +02:00
|
|
|
* form of the line (Just use the last <dstsize> characters of the
|
|
|
|
* full name.)
|
|
|
|
*
|
|
|
|
* NOTE: use strncpy because we do NOT necessarily want zero
|
2004-09-12 07:18:55 +02:00
|
|
|
* termination
|
|
|
|
*/
|
2000-06-03 16:57:40 +02:00
|
|
|
char *
|
2001-02-05 13:42:17 +01:00
|
|
|
line_abbrevname(char *dst, const char *src, int dstsize)
|
2000-06-27 03:18:27 +02:00
|
|
|
{
|
|
|
|
size_t len;
|
2001-02-05 13:42:17 +01:00
|
|
|
|
2000-06-03 16:57:40 +02:00
|
|
|
memset(dst, '\0', dstsize);
|
2001-02-05 13:42:17 +01:00
|
|
|
|
2000-07-01 05:17:42 +02:00
|
|
|
/* Always skip prefix if present */
|
|
|
|
if (strncmp(src, "/dev/", 5) == 0)
|
|
|
|
src += 5;
|
2001-02-05 13:42:17 +01:00
|
|
|
|
2002-04-23 15:09:19 +02:00
|
|
|
#ifdef WITH_ABBREV_NO_TTY
|
|
|
|
if (strncmp(src, "tty", 3) == 0)
|
|
|
|
src += 3;
|
|
|
|
#endif
|
|
|
|
|
2000-06-27 03:18:27 +02:00
|
|
|
len = strlen(src);
|
|
|
|
|
2000-07-01 05:17:42 +02:00
|
|
|
if (len > 0) {
|
|
|
|
if (((int)len - dstsize) > 0)
|
|
|
|
src += ((int)len - dstsize);
|
|
|
|
|
|
|
|
/* note: _don't_ change this to strlcpy */
|
2001-02-05 13:42:17 +01:00
|
|
|
strncpy(dst, src, (size_t)dstsize);
|
2000-06-27 03:18:27 +02:00
|
|
|
}
|
2001-02-05 13:42:17 +01:00
|
|
|
|
2004-09-12 07:18:55 +02:00
|
|
|
return (dst);
|
2000-06-03 16:57:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
** utmp utility functions
|
2000-06-04 19:07:49 +02:00
|
|
|
**
|
|
|
|
** These functions manipulate struct utmp, taking system differences
|
|
|
|
** into account.
|
2000-06-03 16:57:40 +02:00
|
|
|
**/
|
|
|
|
|
|
|
|
#if defined(USE_UTMP) || defined (USE_WTMP) || defined (USE_LOGIN)
|
|
|
|
|
|
|
|
/* build the utmp structure */
|
|
|
|
void
|
2000-06-04 19:07:49 +02:00
|
|
|
set_utmp_time(struct logininfo *li, struct utmp *ut)
|
|
|
|
{
|
2004-09-12 07:18:55 +02:00
|
|
|
# if defined(HAVE_TV_IN_UTMP)
|
2000-06-03 16:57:40 +02:00
|
|
|
ut->ut_tv.tv_sec = li->tv_sec;
|
|
|
|
ut->ut_tv.tv_usec = li->tv_usec;
|
2004-09-12 07:18:55 +02:00
|
|
|
# elif defined(HAVE_TIME_IN_UTMP)
|
2000-06-03 16:57:40 +02:00
|
|
|
ut->ut_time = li->tv_sec;
|
2000-06-27 03:18:27 +02:00
|
|
|
# endif
|
2000-06-03 16:57:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
construct_utmp(struct logininfo *li,
|
2000-06-04 19:07:49 +02:00
|
|
|
struct utmp *ut)
|
|
|
|
{
|
2003-01-03 04:42:27 +01:00
|
|
|
# ifdef HAVE_ADDR_V6_IN_UTMP
|
|
|
|
struct sockaddr_in6 *sa6;
|
2004-09-12 07:18:55 +02:00
|
|
|
# endif
|
|
|
|
|
2000-08-15 02:01:22 +02:00
|
|
|
memset(ut, '\0', sizeof(*ut));
|
2000-06-19 10:20:03 +02:00
|
|
|
|
|
|
|
/* First fill out fields used for both logins and logouts */
|
|
|
|
|
2000-06-27 03:18:27 +02:00
|
|
|
# ifdef HAVE_ID_IN_UTMP
|
2000-06-03 16:57:40 +02:00
|
|
|
line_abbrevname(ut->ut_id, li->line, sizeof(ut->ut_id));
|
2000-06-27 03:18:27 +02:00
|
|
|
# endif
|
2000-06-03 16:57:40 +02:00
|
|
|
|
2000-06-27 03:18:27 +02:00
|
|
|
# ifdef HAVE_TYPE_IN_UTMP
|
2000-06-19 10:20:03 +02:00
|
|
|
/* This is done here to keep utmp constants out of struct logininfo */
|
2000-06-03 16:57:40 +02:00
|
|
|
switch (li->type) {
|
|
|
|
case LTYPE_LOGIN:
|
|
|
|
ut->ut_type = USER_PROCESS;
|
|
|
|
break;
|
|
|
|
case LTYPE_LOGOUT:
|
|
|
|
ut->ut_type = DEAD_PROCESS;
|
|
|
|
break;
|
|
|
|
}
|
2000-06-27 03:18:27 +02:00
|
|
|
# endif
|
2000-06-19 10:20:03 +02:00
|
|
|
set_utmp_time(li, ut);
|
2000-06-03 16:57:40 +02:00
|
|
|
|
2000-06-19 10:20:03 +02:00
|
|
|
line_stripname(ut->ut_line, li->line, sizeof(ut->ut_line));
|
2000-06-27 03:18:27 +02:00
|
|
|
|
|
|
|
# ifdef HAVE_PID_IN_UTMP
|
2000-06-03 16:57:40 +02:00
|
|
|
ut->ut_pid = li->pid;
|
2000-06-27 03:18:27 +02:00
|
|
|
# endif
|
2000-06-19 10:20:03 +02:00
|
|
|
|
|
|
|
/* If we're logging out, leave all other fields blank */
|
|
|
|
if (li->type == LTYPE_LOGOUT)
|
2004-09-12 07:18:55 +02:00
|
|
|
return;
|
2000-06-19 10:20:03 +02:00
|
|
|
|
2000-06-27 03:18:27 +02:00
|
|
|
/*
|
|
|
|
* These fields are only used when logging in, and are blank
|
2001-02-05 13:42:17 +01:00
|
|
|
* for logouts.
|
2000-06-27 03:18:27 +02:00
|
|
|
*/
|
2000-06-19 10:20:03 +02:00
|
|
|
|
|
|
|
/* Use strncpy because we don't necessarily want null termination */
|
2004-09-12 07:18:55 +02:00
|
|
|
strncpy(ut->ut_name, li->username,
|
|
|
|
MIN_SIZEOF(ut->ut_name, li->username));
|
2000-06-27 03:18:27 +02:00
|
|
|
# ifdef HAVE_HOST_IN_UTMP
|
2004-09-12 07:18:55 +02:00
|
|
|
strncpy(ut->ut_host, li->hostname,
|
|
|
|
MIN_SIZEOF(ut->ut_host, li->hostname));
|
2000-06-27 03:18:27 +02:00
|
|
|
# endif
|
|
|
|
# ifdef HAVE_ADDR_IN_UTMP
|
2000-06-04 19:07:49 +02:00
|
|
|
/* this is just a 32-bit IP address */
|
|
|
|
if (li->hostaddr.sa.sa_family == AF_INET)
|
|
|
|
ut->ut_addr = li->hostaddr.sa_in.sin_addr.s_addr;
|
2001-02-05 13:42:17 +01:00
|
|
|
# endif
|
2003-01-03 04:42:27 +01:00
|
|
|
# ifdef HAVE_ADDR_V6_IN_UTMP
|
|
|
|
/* this is just a 128-bit IPv6 address */
|
|
|
|
if (li->hostaddr.sa.sa_family == AF_INET6) {
|
|
|
|
sa6 = ((struct sockaddr_in6 *)&li->hostaddr.sa);
|
|
|
|
memcpy(ut->ut_addr_v6, sa6->sin6_addr.s6_addr, 16);
|
|
|
|
if (IN6_IS_ADDR_V4MAPPED(&sa6->sin6_addr)) {
|
|
|
|
ut->ut_addr_v6[0] = ut->ut_addr_v6[3];
|
|
|
|
ut->ut_addr_v6[1] = 0;
|
|
|
|
ut->ut_addr_v6[2] = 0;
|
|
|
|
ut->ut_addr_v6[3] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
# endif
|
2000-06-04 19:07:49 +02:00
|
|
|
}
|
2000-06-27 03:18:27 +02:00
|
|
|
#endif /* USE_UTMP || USE_WTMP || USE_LOGIN */
|
2000-06-04 19:07:49 +02:00
|
|
|
|
2000-06-03 16:57:40 +02:00
|
|
|
/**
|
|
|
|
** utmpx utility functions
|
2000-06-04 19:07:49 +02:00
|
|
|
**
|
|
|
|
** These functions manipulate struct utmpx, accounting for system
|
|
|
|
** variations.
|
2000-06-03 16:57:40 +02:00
|
|
|
**/
|
|
|
|
|
|
|
|
#if defined(USE_UTMPX) || defined (USE_WTMPX)
|
|
|
|
/* build the utmpx structure */
|
|
|
|
void
|
2000-06-04 19:07:49 +02:00
|
|
|
set_utmpx_time(struct logininfo *li, struct utmpx *utx)
|
|
|
|
{
|
2004-09-12 07:18:55 +02:00
|
|
|
# if defined(HAVE_TV_IN_UTMPX)
|
2000-06-03 16:57:40 +02:00
|
|
|
utx->ut_tv.tv_sec = li->tv_sec;
|
|
|
|
utx->ut_tv.tv_usec = li->tv_usec;
|
2004-09-12 07:18:55 +02:00
|
|
|
# elif defined(HAVE_TIME_IN_UTMPX)
|
2000-06-03 16:57:40 +02:00
|
|
|
utx->ut_time = li->tv_sec;
|
2004-09-12 07:18:55 +02:00
|
|
|
# endif
|
2000-06-03 16:57:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2000-06-04 19:07:49 +02:00
|
|
|
construct_utmpx(struct logininfo *li, struct utmpx *utx)
|
|
|
|
{
|
2003-01-03 04:42:27 +01:00
|
|
|
# ifdef HAVE_ADDR_V6_IN_UTMP
|
|
|
|
struct sockaddr_in6 *sa6;
|
|
|
|
# endif
|
2000-08-15 02:01:22 +02:00
|
|
|
memset(utx, '\0', sizeof(*utx));
|
2004-09-12 07:18:55 +02:00
|
|
|
|
2000-07-01 05:17:42 +02:00
|
|
|
# ifdef HAVE_ID_IN_UTMPX
|
2000-06-03 16:57:40 +02:00
|
|
|
line_abbrevname(utx->ut_id, li->line, sizeof(utx->ut_id));
|
2000-07-01 05:17:42 +02:00
|
|
|
# endif
|
2000-06-03 16:57:40 +02:00
|
|
|
|
|
|
|
/* this is done here to keep utmp constants out of loginrec.h */
|
|
|
|
switch (li->type) {
|
|
|
|
case LTYPE_LOGIN:
|
|
|
|
utx->ut_type = USER_PROCESS;
|
|
|
|
break;
|
|
|
|
case LTYPE_LOGOUT:
|
|
|
|
utx->ut_type = DEAD_PROCESS;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
line_stripname(utx->ut_line, li->line, sizeof(utx->ut_line));
|
|
|
|
set_utmpx_time(li, utx);
|
2000-06-19 10:20:03 +02:00
|
|
|
utx->ut_pid = li->pid;
|
2004-09-12 07:18:55 +02:00
|
|
|
|
2002-02-25 02:56:46 +01:00
|
|
|
/* strncpy(): Don't necessarily want null termination */
|
2010-01-09 08:18:04 +01:00
|
|
|
strncpy(utx->ut_user, li->username,
|
|
|
|
MIN_SIZEOF(utx->ut_user, li->username));
|
2000-06-19 10:20:03 +02:00
|
|
|
|
|
|
|
if (li->type == LTYPE_LOGOUT)
|
|
|
|
return;
|
|
|
|
|
2000-06-27 03:18:27 +02:00
|
|
|
/*
|
|
|
|
* These fields are only used when logging in, and are blank
|
2001-02-05 13:42:17 +01:00
|
|
|
* for logouts.
|
2000-06-27 03:18:27 +02:00
|
|
|
*/
|
2000-06-19 10:20:03 +02:00
|
|
|
|
2000-06-27 03:18:27 +02:00
|
|
|
# ifdef HAVE_HOST_IN_UTMPX
|
2004-09-12 07:18:55 +02:00
|
|
|
strncpy(utx->ut_host, li->hostname,
|
|
|
|
MIN_SIZEOF(utx->ut_host, li->hostname));
|
2000-06-27 03:18:27 +02:00
|
|
|
# endif
|
|
|
|
# ifdef HAVE_ADDR_IN_UTMPX
|
2000-09-23 04:57:27 +02:00
|
|
|
/* this is just a 32-bit IP address */
|
|
|
|
if (li->hostaddr.sa.sa_family == AF_INET)
|
|
|
|
utx->ut_addr = li->hostaddr.sa_in.sin_addr.s_addr;
|
2000-06-27 03:18:27 +02:00
|
|
|
# endif
|
2003-01-03 04:42:27 +01:00
|
|
|
# ifdef HAVE_ADDR_V6_IN_UTMP
|
|
|
|
/* this is just a 128-bit IPv6 address */
|
|
|
|
if (li->hostaddr.sa.sa_family == AF_INET6) {
|
|
|
|
sa6 = ((struct sockaddr_in6 *)&li->hostaddr.sa);
|
2014-12-23 05:26:13 +01:00
|
|
|
memcpy(utx->ut_addr_v6, sa6->sin6_addr.s6_addr, 16);
|
2003-01-03 04:42:27 +01:00
|
|
|
if (IN6_IS_ADDR_V4MAPPED(&sa6->sin6_addr)) {
|
2014-12-23 05:26:13 +01:00
|
|
|
utx->ut_addr_v6[0] = utx->ut_addr_v6[3];
|
|
|
|
utx->ut_addr_v6[1] = 0;
|
|
|
|
utx->ut_addr_v6[2] = 0;
|
|
|
|
utx->ut_addr_v6[3] = 0;
|
2003-01-03 04:42:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
# endif
|
2000-06-27 03:18:27 +02:00
|
|
|
# ifdef HAVE_SYSLEN_IN_UTMPX
|
2000-06-19 10:20:03 +02:00
|
|
|
/* ut_syslen is the length of the utx_host string */
|
|
|
|
utx->ut_syslen = MIN(strlen(li->hostname), sizeof(utx->ut_host));
|
2000-06-27 03:18:27 +02:00
|
|
|
# endif
|
2000-06-04 19:07:49 +02:00
|
|
|
}
|
2000-06-27 03:18:27 +02:00
|
|
|
#endif /* USE_UTMPX || USE_WTMPX */
|
2000-06-03 16:57:40 +02:00
|
|
|
|
|
|
|
/**
|
2000-06-04 19:07:49 +02:00
|
|
|
** Low-level utmp functions
|
2000-06-03 16:57:40 +02:00
|
|
|
**/
|
|
|
|
|
|
|
|
/* FIXME: (ATL) utmp_write_direct needs testing */
|
|
|
|
#ifdef USE_UTMP
|
|
|
|
|
|
|
|
/* if we can, use pututline() etc. */
|
2000-06-27 03:18:27 +02:00
|
|
|
# if !defined(DISABLE_PUTUTLINE) && defined(HAVE_SETUTENT) && \
|
|
|
|
defined(HAVE_PUTUTLINE)
|
2000-06-03 16:57:40 +02:00
|
|
|
# define UTMP_USE_LIBRARY
|
2000-06-27 03:18:27 +02:00
|
|
|
# endif
|
2000-06-03 16:57:40 +02:00
|
|
|
|
|
|
|
|
|
|
|
/* write a utmp entry with the system's help (pututline() and pals) */
|
2000-06-27 03:18:27 +02:00
|
|
|
# ifdef UTMP_USE_LIBRARY
|
2000-06-03 16:57:40 +02:00
|
|
|
static int
|
2000-06-04 19:07:49 +02:00
|
|
|
utmp_write_library(struct logininfo *li, struct utmp *ut)
|
|
|
|
{
|
2000-06-03 16:57:40 +02:00
|
|
|
setutent();
|
|
|
|
pututline(ut);
|
2000-06-27 03:18:27 +02:00
|
|
|
# ifdef HAVE_ENDUTENT
|
2000-06-03 16:57:40 +02:00
|
|
|
endutent();
|
2000-06-27 03:18:27 +02:00
|
|
|
# endif
|
2004-09-12 07:18:55 +02:00
|
|
|
return (1);
|
2000-06-04 19:07:49 +02:00
|
|
|
}
|
2000-06-27 03:18:27 +02:00
|
|
|
# else /* UTMP_USE_LIBRARY */
|
2000-06-03 16:57:40 +02:00
|
|
|
|
2005-07-17 09:04:47 +02:00
|
|
|
/*
|
2004-09-12 07:18:55 +02:00
|
|
|
* Write a utmp entry direct to the file
|
|
|
|
* This is a slightly modification of code in OpenBSD's login.c
|
|
|
|
*/
|
2000-06-03 16:57:40 +02:00
|
|
|
static int
|
2000-06-04 19:07:49 +02:00
|
|
|
utmp_write_direct(struct logininfo *li, struct utmp *ut)
|
|
|
|
{
|
2000-06-03 16:57:40 +02:00
|
|
|
struct utmp old_ut;
|
|
|
|
register int fd;
|
|
|
|
int tty;
|
|
|
|
|
2000-06-19 10:20:03 +02:00
|
|
|
/* FIXME: (ATL) ttyslot() needs local implementation */
|
2000-08-15 02:01:22 +02:00
|
|
|
|
2000-08-29 05:30:37 +02:00
|
|
|
#if defined(HAVE_GETTTYENT)
|
2004-09-12 07:18:55 +02:00
|
|
|
struct ttyent *ty;
|
2000-08-15 02:01:22 +02:00
|
|
|
|
|
|
|
tty=0;
|
|
|
|
setttyent();
|
2004-09-12 07:18:55 +02:00
|
|
|
while (NULL != (ty = getttyent())) {
|
2000-08-15 02:01:22 +02:00
|
|
|
tty++;
|
|
|
|
if (!strncmp(ty->ty_name, ut->ut_line, sizeof(ut->ut_line)))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
endttyent();
|
|
|
|
|
2004-09-12 07:18:55 +02:00
|
|
|
if (NULL == ty) {
|
2004-08-15 11:12:52 +02:00
|
|
|
logit("%s: tty not found", __func__);
|
|
|
|
return (0);
|
2000-08-15 02:01:22 +02:00
|
|
|
}
|
|
|
|
#else /* FIXME */
|
|
|
|
|
2000-06-03 16:57:40 +02:00
|
|
|
tty = ttyslot(); /* seems only to work for /dev/ttyp? style names */
|
|
|
|
|
2000-08-29 05:30:37 +02:00
|
|
|
#endif /* HAVE_GETTTYENT */
|
2000-08-15 02:01:22 +02:00
|
|
|
|
2000-06-03 16:57:40 +02:00
|
|
|
if (tty > 0 && (fd = open(UTMP_FILE, O_RDWR|O_CREAT, 0644)) >= 0) {
|
2004-08-15 11:12:52 +02:00
|
|
|
off_t pos, ret;
|
|
|
|
|
|
|
|
pos = (off_t)tty * sizeof(struct utmp);
|
|
|
|
if ((ret = lseek(fd, pos, SEEK_SET)) == -1) {
|
2004-08-23 13:53:28 +02:00
|
|
|
logit("%s: lseek: %s", __func__, strerror(errno));
|
2011-01-02 11:43:59 +01:00
|
|
|
close(fd);
|
2004-08-15 11:12:52 +02:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
if (ret != pos) {
|
2005-07-17 09:04:47 +02:00
|
|
|
logit("%s: Couldn't seek to tty %d slot in %s",
|
2004-08-23 13:53:28 +02:00
|
|
|
__func__, tty, UTMP_FILE);
|
2011-01-02 11:43:59 +01:00
|
|
|
close(fd);
|
2004-08-15 11:12:52 +02:00
|
|
|
return (0);
|
|
|
|
}
|
2000-06-03 16:57:40 +02:00
|
|
|
/*
|
|
|
|
* Prevent luser from zero'ing out ut_host.
|
|
|
|
* If the new ut_line is empty but the old one is not
|
2000-07-11 04:15:54 +02:00
|
|
|
* and ut_line and ut_name match, preserve the old ut_line.
|
2000-06-03 16:57:40 +02:00
|
|
|
*/
|
2001-02-05 13:42:17 +01:00
|
|
|
if (atomicio(read, fd, &old_ut, sizeof(old_ut)) == sizeof(old_ut) &&
|
2004-09-12 07:18:55 +02:00
|
|
|
(ut->ut_host[0] == '\0') && (old_ut.ut_host[0] != '\0') &&
|
|
|
|
(strncmp(old_ut.ut_line, ut->ut_line, sizeof(ut->ut_line)) == 0) &&
|
|
|
|
(strncmp(old_ut.ut_name, ut->ut_name, sizeof(ut->ut_name)) == 0))
|
|
|
|
memcpy(ut->ut_host, old_ut.ut_host, sizeof(ut->ut_host));
|
2001-02-05 13:42:17 +01:00
|
|
|
|
2004-08-15 11:12:52 +02:00
|
|
|
if ((ret = lseek(fd, pos, SEEK_SET)) == -1) {
|
2004-08-23 13:53:28 +02:00
|
|
|
logit("%s: lseek: %s", __func__, strerror(errno));
|
2011-01-02 11:43:59 +01:00
|
|
|
close(fd);
|
2004-08-15 11:12:52 +02:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
if (ret != pos) {
|
2004-08-23 13:53:28 +02:00
|
|
|
logit("%s: Couldn't seek to tty %d slot in %s",
|
2004-08-15 11:12:52 +02:00
|
|
|
__func__, tty, UTMP_FILE);
|
2011-01-02 11:43:59 +01:00
|
|
|
close(fd);
|
2004-08-15 11:12:52 +02:00
|
|
|
return (0);
|
|
|
|
}
|
2004-09-12 07:18:55 +02:00
|
|
|
if (atomicio(vwrite, fd, ut, sizeof(*ut)) != sizeof(*ut)) {
|
2004-08-15 11:12:52 +02:00
|
|
|
logit("%s: error writing %s: %s", __func__,
|
2000-06-19 10:20:03 +02:00
|
|
|
UTMP_FILE, strerror(errno));
|
2011-01-02 11:43:59 +01:00
|
|
|
close(fd);
|
|
|
|
return (0);
|
2004-09-12 07:18:55 +02:00
|
|
|
}
|
2001-02-05 13:42:17 +01:00
|
|
|
|
2004-09-12 07:18:55 +02:00
|
|
|
close(fd);
|
|
|
|
return (1);
|
2000-06-27 16:50:50 +02:00
|
|
|
} else {
|
2004-09-12 07:18:55 +02:00
|
|
|
return (0);
|
2000-06-27 16:50:50 +02:00
|
|
|
}
|
2000-06-04 19:07:49 +02:00
|
|
|
}
|
2000-06-27 03:18:27 +02:00
|
|
|
# endif /* UTMP_USE_LIBRARY */
|
2000-06-03 16:57:40 +02:00
|
|
|
|
|
|
|
static int
|
2000-06-04 19:07:49 +02:00
|
|
|
utmp_perform_login(struct logininfo *li)
|
|
|
|
{
|
2000-06-03 16:57:40 +02:00
|
|
|
struct utmp ut;
|
|
|
|
|
|
|
|
construct_utmp(li, &ut);
|
2000-06-27 03:18:27 +02:00
|
|
|
# ifdef UTMP_USE_LIBRARY
|
2000-06-03 16:57:40 +02:00
|
|
|
if (!utmp_write_library(li, &ut)) {
|
2004-09-12 07:25:17 +02:00
|
|
|
logit("%s: utmp_write_library() failed", __func__);
|
2004-09-12 07:18:55 +02:00
|
|
|
return (0);
|
2000-06-03 16:57:40 +02:00
|
|
|
}
|
2000-06-27 03:18:27 +02:00
|
|
|
# else
|
2000-06-03 16:57:40 +02:00
|
|
|
if (!utmp_write_direct(li, &ut)) {
|
2004-09-12 07:25:17 +02:00
|
|
|
logit("%s: utmp_write_direct() failed", __func__);
|
2004-09-12 07:18:55 +02:00
|
|
|
return (0);
|
2000-06-03 16:57:40 +02:00
|
|
|
}
|
2000-06-27 03:18:27 +02:00
|
|
|
# endif
|
2004-09-12 07:18:55 +02:00
|
|
|
return (1);
|
2000-06-04 19:07:49 +02:00
|
|
|
}
|
2000-06-03 16:57:40 +02:00
|
|
|
|
|
|
|
|
|
|
|
static int
|
2000-06-04 19:07:49 +02:00
|
|
|
utmp_perform_logout(struct logininfo *li)
|
|
|
|
{
|
2000-06-03 16:57:40 +02:00
|
|
|
struct utmp ut;
|
|
|
|
|
2000-06-19 10:20:03 +02:00
|
|
|
construct_utmp(li, &ut);
|
2000-06-27 03:18:27 +02:00
|
|
|
# ifdef UTMP_USE_LIBRARY
|
2000-06-19 10:20:03 +02:00
|
|
|
if (!utmp_write_library(li, &ut)) {
|
2004-09-12 07:25:17 +02:00
|
|
|
logit("%s: utmp_write_library() failed", __func__);
|
2004-09-12 07:18:55 +02:00
|
|
|
return (0);
|
2000-06-19 10:20:03 +02:00
|
|
|
}
|
2000-06-27 03:18:27 +02:00
|
|
|
# else
|
2000-06-19 10:20:03 +02:00
|
|
|
if (!utmp_write_direct(li, &ut)) {
|
2004-09-12 07:25:17 +02:00
|
|
|
logit("%s: utmp_write_direct() failed", __func__);
|
2004-09-12 07:18:55 +02:00
|
|
|
return (0);
|
2000-06-19 10:20:03 +02:00
|
|
|
}
|
2000-06-27 03:18:27 +02:00
|
|
|
# endif
|
2004-09-12 07:18:55 +02:00
|
|
|
return (1);
|
2000-06-04 19:07:49 +02:00
|
|
|
}
|
2000-06-03 16:57:40 +02:00
|
|
|
|
|
|
|
|
|
|
|
int
|
2000-06-04 19:07:49 +02:00
|
|
|
utmp_write_entry(struct logininfo *li)
|
|
|
|
{
|
2000-06-03 16:57:40 +02:00
|
|
|
switch(li->type) {
|
|
|
|
case LTYPE_LOGIN:
|
2004-09-12 07:18:55 +02:00
|
|
|
return (utmp_perform_login(li));
|
2000-06-03 16:57:40 +02:00
|
|
|
|
|
|
|
case LTYPE_LOGOUT:
|
2004-09-12 07:18:55 +02:00
|
|
|
return (utmp_perform_logout(li));
|
2000-06-03 16:57:40 +02:00
|
|
|
|
|
|
|
default:
|
2004-09-12 07:25:17 +02:00
|
|
|
logit("%s: invalid type field", __func__);
|
2004-09-12 07:18:55 +02:00
|
|
|
return (0);
|
2000-06-03 16:57:40 +02:00
|
|
|
}
|
2000-06-04 19:07:49 +02:00
|
|
|
}
|
2000-06-27 03:18:27 +02:00
|
|
|
#endif /* USE_UTMP */
|
2000-06-03 16:57:40 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
2000-06-04 19:07:49 +02:00
|
|
|
** Low-level utmpx functions
|
2000-06-03 16:57:40 +02:00
|
|
|
**/
|
|
|
|
|
|
|
|
/* not much point if we don't want utmpx entries */
|
|
|
|
#ifdef USE_UTMPX
|
|
|
|
|
|
|
|
/* if we have the wherewithall, use pututxline etc. */
|
2000-06-27 03:18:27 +02:00
|
|
|
# if !defined(DISABLE_PUTUTXLINE) && defined(HAVE_SETUTXENT) && \
|
|
|
|
defined(HAVE_PUTUTXLINE)
|
2000-06-03 16:57:40 +02:00
|
|
|
# define UTMPX_USE_LIBRARY
|
2000-06-27 03:18:27 +02:00
|
|
|
# endif
|
2000-06-03 16:57:40 +02:00
|
|
|
|
|
|
|
|
|
|
|
/* write a utmpx entry with the system's help (pututxline() and pals) */
|
2000-06-27 03:18:27 +02:00
|
|
|
# ifdef UTMPX_USE_LIBRARY
|
2000-06-03 16:57:40 +02:00
|
|
|
static int
|
2000-06-04 19:07:49 +02:00
|
|
|
utmpx_write_library(struct logininfo *li, struct utmpx *utx)
|
|
|
|
{
|
2000-06-03 16:57:40 +02:00
|
|
|
setutxent();
|
|
|
|
pututxline(utx);
|
|
|
|
|
2000-06-27 03:18:27 +02:00
|
|
|
# ifdef HAVE_ENDUTXENT
|
2000-06-03 16:57:40 +02:00
|
|
|
endutxent();
|
2000-06-27 03:18:27 +02:00
|
|
|
# endif
|
2004-09-12 07:18:55 +02:00
|
|
|
return (1);
|
2000-06-04 19:07:49 +02:00
|
|
|
}
|
2000-06-03 16:57:40 +02:00
|
|
|
|
2000-06-27 03:18:27 +02:00
|
|
|
# else /* UTMPX_USE_LIBRARY */
|
2000-06-03 16:57:40 +02:00
|
|
|
|
|
|
|
/* write a utmp entry direct to the file */
|
|
|
|
static int
|
2000-06-04 19:07:49 +02:00
|
|
|
utmpx_write_direct(struct logininfo *li, struct utmpx *utx)
|
2001-02-05 13:42:17 +01:00
|
|
|
{
|
2004-09-12 07:25:17 +02:00
|
|
|
logit("%s: not implemented!", __func__);
|
2004-09-12 07:18:55 +02:00
|
|
|
return (0);
|
2000-06-04 19:07:49 +02:00
|
|
|
}
|
2000-06-27 03:18:27 +02:00
|
|
|
# endif /* UTMPX_USE_LIBRARY */
|
2000-06-03 16:57:40 +02:00
|
|
|
|
|
|
|
static int
|
2000-06-04 19:07:49 +02:00
|
|
|
utmpx_perform_login(struct logininfo *li)
|
|
|
|
{
|
2000-06-03 16:57:40 +02:00
|
|
|
struct utmpx utx;
|
|
|
|
|
|
|
|
construct_utmpx(li, &utx);
|
2000-06-27 03:18:27 +02:00
|
|
|
# ifdef UTMPX_USE_LIBRARY
|
2000-06-03 16:57:40 +02:00
|
|
|
if (!utmpx_write_library(li, &utx)) {
|
2004-09-12 07:25:17 +02:00
|
|
|
logit("%s: utmp_write_library() failed", __func__);
|
2004-09-12 07:18:55 +02:00
|
|
|
return (0);
|
2000-06-03 16:57:40 +02:00
|
|
|
}
|
2000-06-27 03:18:27 +02:00
|
|
|
# else
|
2000-06-03 16:57:40 +02:00
|
|
|
if (!utmpx_write_direct(li, &ut)) {
|
2004-09-12 07:25:17 +02:00
|
|
|
logit("%s: utmp_write_direct() failed", __func__);
|
2004-09-12 07:18:55 +02:00
|
|
|
return (0);
|
2000-06-03 16:57:40 +02:00
|
|
|
}
|
2000-06-27 03:18:27 +02:00
|
|
|
# endif
|
2004-09-12 07:18:55 +02:00
|
|
|
return (1);
|
2000-06-04 19:07:49 +02:00
|
|
|
}
|
2000-06-03 16:57:40 +02:00
|
|
|
|
|
|
|
|
|
|
|
static int
|
2000-06-04 19:07:49 +02:00
|
|
|
utmpx_perform_logout(struct logininfo *li)
|
|
|
|
{
|
2000-06-03 16:57:40 +02:00
|
|
|
struct utmpx utx;
|
|
|
|
|
2002-02-25 02:56:46 +01:00
|
|
|
construct_utmpx(li, &utx);
|
2000-06-27 03:18:27 +02:00
|
|
|
# ifdef HAVE_ID_IN_UTMPX
|
2000-06-03 16:57:40 +02:00
|
|
|
line_abbrevname(utx.ut_id, li->line, sizeof(utx.ut_id));
|
2000-06-27 03:18:27 +02:00
|
|
|
# endif
|
|
|
|
# ifdef HAVE_TYPE_IN_UTMPX
|
2000-06-03 16:57:40 +02:00
|
|
|
utx.ut_type = DEAD_PROCESS;
|
2000-06-27 03:18:27 +02:00
|
|
|
# endif
|
2000-06-03 16:57:40 +02:00
|
|
|
|
2000-06-27 03:18:27 +02:00
|
|
|
# ifdef UTMPX_USE_LIBRARY
|
2000-06-03 16:57:40 +02:00
|
|
|
utmpx_write_library(li, &utx);
|
2000-06-27 03:18:27 +02:00
|
|
|
# else
|
2000-06-03 16:57:40 +02:00
|
|
|
utmpx_write_direct(li, &utx);
|
2000-06-27 03:18:27 +02:00
|
|
|
# endif
|
2004-09-12 07:18:55 +02:00
|
|
|
return (1);
|
2000-06-04 19:07:49 +02:00
|
|
|
}
|
2000-06-03 16:57:40 +02:00
|
|
|
|
|
|
|
int
|
2000-06-04 19:07:49 +02:00
|
|
|
utmpx_write_entry(struct logininfo *li)
|
|
|
|
{
|
2000-06-03 16:57:40 +02:00
|
|
|
switch(li->type) {
|
|
|
|
case LTYPE_LOGIN:
|
2004-09-12 07:18:55 +02:00
|
|
|
return (utmpx_perform_login(li));
|
2000-06-03 16:57:40 +02:00
|
|
|
case LTYPE_LOGOUT:
|
2004-09-12 07:18:55 +02:00
|
|
|
return (utmpx_perform_logout(li));
|
2000-06-03 16:57:40 +02:00
|
|
|
default:
|
2004-09-12 07:25:17 +02:00
|
|
|
logit("%s: invalid type field", __func__);
|
2004-09-12 07:18:55 +02:00
|
|
|
return (0);
|
2000-06-03 16:57:40 +02:00
|
|
|
}
|
2000-06-04 19:07:49 +02:00
|
|
|
}
|
2000-06-27 03:18:27 +02:00
|
|
|
#endif /* USE_UTMPX */
|
2000-06-03 16:57:40 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
2000-06-04 19:07:49 +02:00
|
|
|
** Low-level wtmp functions
|
2000-06-03 16:57:40 +02:00
|
|
|
**/
|
|
|
|
|
2001-02-05 13:42:17 +01:00
|
|
|
#ifdef USE_WTMP
|
2000-06-03 16:57:40 +02:00
|
|
|
|
2005-07-17 09:04:47 +02:00
|
|
|
/*
|
2004-09-12 07:18:55 +02:00
|
|
|
* Write a wtmp entry direct to the end of the file
|
|
|
|
* This is a slight modification of code in OpenBSD's logwtmp.c
|
|
|
|
*/
|
2000-06-03 16:57:40 +02:00
|
|
|
static int
|
2000-06-04 19:07:49 +02:00
|
|
|
wtmp_write(struct logininfo *li, struct utmp *ut)
|
|
|
|
{
|
2000-06-03 16:57:40 +02:00
|
|
|
struct stat buf;
|
|
|
|
int fd, ret = 1;
|
|
|
|
|
|
|
|
if ((fd = open(WTMP_FILE, O_WRONLY|O_APPEND, 0)) < 0) {
|
2004-09-12 07:25:17 +02:00
|
|
|
logit("%s: problem writing %s: %s", __func__,
|
2000-06-03 16:57:40 +02:00
|
|
|
WTMP_FILE, strerror(errno));
|
2004-09-12 07:18:55 +02:00
|
|
|
return (0);
|
2000-06-03 16:57:40 +02:00
|
|
|
}
|
2001-02-05 13:42:17 +01:00
|
|
|
if (fstat(fd, &buf) == 0)
|
2003-07-06 07:20:46 +02:00
|
|
|
if (atomicio(vwrite, fd, ut, sizeof(*ut)) != sizeof(*ut)) {
|
2000-06-03 16:57:40 +02:00
|
|
|
ftruncate(fd, buf.st_size);
|
2004-09-12 07:25:17 +02:00
|
|
|
logit("%s: problem writing %s: %s", __func__,
|
2000-06-03 16:57:40 +02:00
|
|
|
WTMP_FILE, strerror(errno));
|
|
|
|
ret = 0;
|
|
|
|
}
|
2004-09-12 07:18:55 +02:00
|
|
|
close(fd);
|
|
|
|
return (ret);
|
2000-06-04 19:07:49 +02:00
|
|
|
}
|
2000-06-03 16:57:40 +02:00
|
|
|
|
|
|
|
static int
|
2000-06-27 03:18:27 +02:00
|
|
|
wtmp_perform_login(struct logininfo *li)
|
|
|
|
{
|
2000-06-03 16:57:40 +02:00
|
|
|
struct utmp ut;
|
|
|
|
|
|
|
|
construct_utmp(li, &ut);
|
2004-09-12 07:18:55 +02:00
|
|
|
return (wtmp_write(li, &ut));
|
2000-06-04 19:07:49 +02:00
|
|
|
}
|
2000-06-03 16:57:40 +02:00
|
|
|
|
|
|
|
|
|
|
|
static int
|
2000-06-04 19:07:49 +02:00
|
|
|
wtmp_perform_logout(struct logininfo *li)
|
|
|
|
{
|
2000-06-03 16:57:40 +02:00
|
|
|
struct utmp ut;
|
|
|
|
|
|
|
|
construct_utmp(li, &ut);
|
2004-09-12 07:18:55 +02:00
|
|
|
return (wtmp_write(li, &ut));
|
2000-06-04 19:07:49 +02:00
|
|
|
}
|
2000-06-03 16:57:40 +02:00
|
|
|
|
|
|
|
|
|
|
|
int
|
2000-06-04 19:07:49 +02:00
|
|
|
wtmp_write_entry(struct logininfo *li)
|
|
|
|
{
|
2000-06-03 16:57:40 +02:00
|
|
|
switch(li->type) {
|
|
|
|
case LTYPE_LOGIN:
|
2004-09-12 07:18:55 +02:00
|
|
|
return (wtmp_perform_login(li));
|
2000-06-03 16:57:40 +02:00
|
|
|
case LTYPE_LOGOUT:
|
2004-09-12 07:18:55 +02:00
|
|
|
return (wtmp_perform_logout(li));
|
2000-06-03 16:57:40 +02:00
|
|
|
default:
|
2004-09-12 07:25:17 +02:00
|
|
|
logit("%s: invalid type field", __func__);
|
2004-09-12 07:18:55 +02:00
|
|
|
return (0);
|
2000-06-03 16:57:40 +02:00
|
|
|
}
|
2000-06-04 19:07:49 +02:00
|
|
|
}
|
2000-06-03 16:57:40 +02:00
|
|
|
|
|
|
|
|
2005-07-17 09:04:47 +02:00
|
|
|
/*
|
2004-09-12 07:18:55 +02:00
|
|
|
* Notes on fetching login data from wtmp/wtmpx
|
2001-02-05 13:42:17 +01:00
|
|
|
*
|
2000-06-19 10:20:03 +02:00
|
|
|
* Logouts are usually recorded with (amongst other things) a blank
|
|
|
|
* username on a given tty line. However, some systems (HP-UX is one)
|
|
|
|
* leave all fields set, but change the ut_type field to DEAD_PROCESS.
|
|
|
|
*
|
|
|
|
* Since we're only looking for logins here, we know that the username
|
|
|
|
* must be set correctly. On systems that leave it in, we check for
|
|
|
|
* ut_type==USER_PROCESS (indicating a login.)
|
|
|
|
*
|
|
|
|
* Portability: Some systems may set something other than USER_PROCESS
|
|
|
|
* to indicate a login process. I don't know of any as I write. Also,
|
|
|
|
* it's possible that some systems may both leave the username in
|
|
|
|
* place and not have ut_type.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* return true if this wtmp entry indicates a login */
|
|
|
|
static int
|
|
|
|
wtmp_islogin(struct logininfo *li, struct utmp *ut)
|
|
|
|
{
|
2001-02-05 13:42:17 +01:00
|
|
|
if (strncmp(li->username, ut->ut_name,
|
2004-09-12 07:18:55 +02:00
|
|
|
MIN_SIZEOF(li->username, ut->ut_name)) == 0) {
|
2000-06-27 03:18:27 +02:00
|
|
|
# ifdef HAVE_TYPE_IN_UTMP
|
2000-06-19 10:20:03 +02:00
|
|
|
if (ut->ut_type & USER_PROCESS)
|
2004-09-12 07:18:55 +02:00
|
|
|
return (1);
|
2000-06-27 03:18:27 +02:00
|
|
|
# else
|
2004-09-12 07:18:55 +02:00
|
|
|
return (1);
|
2000-06-27 03:18:27 +02:00
|
|
|
# endif
|
2000-06-19 10:20:03 +02:00
|
|
|
}
|
2004-09-12 07:18:55 +02:00
|
|
|
return (0);
|
2000-06-19 10:20:03 +02:00
|
|
|
}
|
|
|
|
|
2000-06-03 16:57:40 +02:00
|
|
|
int
|
2000-06-04 19:07:49 +02:00
|
|
|
wtmp_get_entry(struct logininfo *li)
|
|
|
|
{
|
2000-06-03 16:57:40 +02:00
|
|
|
struct stat st;
|
|
|
|
struct utmp ut;
|
2004-09-12 07:18:55 +02:00
|
|
|
int fd, found = 0;
|
2000-06-19 10:20:03 +02:00
|
|
|
|
|
|
|
/* Clear the time entries in our logininfo */
|
|
|
|
li->tv_sec = li->tv_usec = 0;
|
2000-06-03 16:57:40 +02:00
|
|
|
|
|
|
|
if ((fd = open(WTMP_FILE, O_RDONLY)) < 0) {
|
2005-07-17 09:04:47 +02:00
|
|
|
logit("%s: problem opening %s: %s", __func__,
|
2000-06-03 16:57:40 +02:00
|
|
|
WTMP_FILE, strerror(errno));
|
2004-09-12 07:18:55 +02:00
|
|
|
return (0);
|
2000-06-03 16:57:40 +02:00
|
|
|
}
|
2001-02-05 13:42:17 +01:00
|
|
|
if (fstat(fd, &st) != 0) {
|
2005-07-17 09:04:47 +02:00
|
|
|
logit("%s: couldn't stat %s: %s", __func__,
|
2000-06-03 16:57:40 +02:00
|
|
|
WTMP_FILE, strerror(errno));
|
|
|
|
close(fd);
|
2004-09-12 07:18:55 +02:00
|
|
|
return (0);
|
2000-06-03 16:57:40 +02:00
|
|
|
}
|
|
|
|
|
2000-06-19 10:20:03 +02:00
|
|
|
/* Seek to the start of the last struct utmp */
|
2001-10-02 02:29:00 +02:00
|
|
|
if (lseek(fd, -(off_t)sizeof(struct utmp), SEEK_END) == -1) {
|
2000-06-19 10:20:03 +02:00
|
|
|
/* Looks like we've got a fresh wtmp file */
|
|
|
|
close(fd);
|
2004-09-12 07:18:55 +02:00
|
|
|
return (0);
|
2000-06-19 10:20:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
while (!found) {
|
2000-06-27 16:50:50 +02:00
|
|
|
if (atomicio(read, fd, &ut, sizeof(ut)) != sizeof(ut)) {
|
2005-07-17 09:04:47 +02:00
|
|
|
logit("%s: read of %s failed: %s", __func__,
|
2000-06-03 16:57:40 +02:00
|
|
|
WTMP_FILE, strerror(errno));
|
|
|
|
close (fd);
|
2004-09-12 07:18:55 +02:00
|
|
|
return (0);
|
2000-06-03 16:57:40 +02:00
|
|
|
}
|
2011-01-02 11:43:59 +01:00
|
|
|
if (wtmp_islogin(li, &ut) ) {
|
2000-06-19 10:20:03 +02:00
|
|
|
found = 1;
|
2004-09-12 07:18:55 +02:00
|
|
|
/*
|
|
|
|
* We've already checked for a time in struct
|
|
|
|
* utmp, in login_getlast()
|
|
|
|
*/
|
2000-06-27 03:18:27 +02:00
|
|
|
# ifdef HAVE_TIME_IN_UTMP
|
2000-06-03 16:57:40 +02:00
|
|
|
li->tv_sec = ut.ut_time;
|
2000-06-27 03:18:27 +02:00
|
|
|
# else
|
2000-06-03 16:57:40 +02:00
|
|
|
# if HAVE_TV_IN_UTMP
|
|
|
|
li->tv_sec = ut.ut_tv.tv_sec;
|
|
|
|
# endif
|
2000-06-27 03:18:27 +02:00
|
|
|
# endif
|
2000-06-19 10:20:03 +02:00
|
|
|
line_fullname(li->line, ut.ut_line,
|
2004-09-12 07:18:55 +02:00
|
|
|
MIN_SIZEOF(li->line, ut.ut_line));
|
2000-06-27 03:18:27 +02:00
|
|
|
# ifdef HAVE_HOST_IN_UTMP
|
2000-06-19 10:20:03 +02:00
|
|
|
strlcpy(li->hostname, ut.ut_host,
|
2004-09-12 07:18:55 +02:00
|
|
|
MIN_SIZEOF(li->hostname, ut.ut_host));
|
2000-06-27 03:18:27 +02:00
|
|
|
# endif
|
2000-06-19 10:20:03 +02:00
|
|
|
continue;
|
2000-06-03 16:57:40 +02:00
|
|
|
}
|
2000-06-19 10:20:03 +02:00
|
|
|
/* Seek back 2 x struct utmp */
|
2001-10-02 02:29:00 +02:00
|
|
|
if (lseek(fd, -(off_t)(2 * sizeof(struct utmp)), SEEK_CUR) == -1) {
|
2000-06-19 10:20:03 +02:00
|
|
|
/* We've found the start of the file, so quit */
|
2004-09-12 07:18:55 +02:00
|
|
|
close(fd);
|
|
|
|
return (0);
|
2000-06-03 16:57:40 +02:00
|
|
|
}
|
2000-06-19 10:20:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* We found an entry. Tidy up and return */
|
|
|
|
close(fd);
|
2004-09-12 07:18:55 +02:00
|
|
|
return (1);
|
2000-06-04 19:07:49 +02:00
|
|
|
}
|
2000-06-27 03:18:27 +02:00
|
|
|
# endif /* USE_WTMP */
|
2000-06-03 16:57:40 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
2000-06-04 19:07:49 +02:00
|
|
|
** Low-level wtmpx functions
|
2000-06-03 16:57:40 +02:00
|
|
|
**/
|
|
|
|
|
|
|
|
#ifdef USE_WTMPX
|
2004-09-12 07:18:55 +02:00
|
|
|
/*
|
|
|
|
* Write a wtmpx entry direct to the end of the file
|
|
|
|
* This is a slight modification of code in OpenBSD's logwtmp.c
|
|
|
|
*/
|
2000-06-03 16:57:40 +02:00
|
|
|
static int
|
2000-06-04 19:07:49 +02:00
|
|
|
wtmpx_write(struct logininfo *li, struct utmpx *utx)
|
|
|
|
{
|
2004-02-10 06:49:35 +01:00
|
|
|
#ifndef HAVE_UPDWTMPX
|
2000-06-03 16:57:40 +02:00
|
|
|
struct stat buf;
|
|
|
|
int fd, ret = 1;
|
|
|
|
|
|
|
|
if ((fd = open(WTMPX_FILE, O_WRONLY|O_APPEND, 0)) < 0) {
|
2005-07-17 09:04:47 +02:00
|
|
|
logit("%s: problem opening %s: %s", __func__,
|
2000-06-03 16:57:40 +02:00
|
|
|
WTMPX_FILE, strerror(errno));
|
2004-09-12 07:18:55 +02:00
|
|
|
return (0);
|
2000-06-03 16:57:40 +02:00
|
|
|
}
|
|
|
|
|
2001-02-05 13:42:17 +01:00
|
|
|
if (fstat(fd, &buf) == 0)
|
2003-07-06 07:20:46 +02:00
|
|
|
if (atomicio(vwrite, fd, utx, sizeof(*utx)) != sizeof(*utx)) {
|
2000-06-03 16:57:40 +02:00
|
|
|
ftruncate(fd, buf.st_size);
|
2004-09-12 07:25:17 +02:00
|
|
|
logit("%s: problem writing %s: %s", __func__,
|
2000-06-03 16:57:40 +02:00
|
|
|
WTMPX_FILE, strerror(errno));
|
|
|
|
ret = 0;
|
|
|
|
}
|
2004-09-12 07:18:55 +02:00
|
|
|
close(fd);
|
2000-06-03 16:57:40 +02:00
|
|
|
|
2004-09-12 07:18:55 +02:00
|
|
|
return (ret);
|
2004-02-10 06:49:35 +01:00
|
|
|
#else
|
|
|
|
updwtmpx(WTMPX_FILE, utx);
|
2004-09-12 07:18:55 +02:00
|
|
|
return (1);
|
2004-02-10 06:49:35 +01:00
|
|
|
#endif
|
2000-06-04 19:07:49 +02:00
|
|
|
}
|
2000-06-03 16:57:40 +02:00
|
|
|
|
|
|
|
|
|
|
|
static int
|
2000-06-04 19:07:49 +02:00
|
|
|
wtmpx_perform_login(struct logininfo *li)
|
|
|
|
{
|
2000-06-03 16:57:40 +02:00
|
|
|
struct utmpx utx;
|
|
|
|
|
|
|
|
construct_utmpx(li, &utx);
|
2004-09-12 07:18:55 +02:00
|
|
|
return (wtmpx_write(li, &utx));
|
2000-06-04 19:07:49 +02:00
|
|
|
}
|
2000-06-03 16:57:40 +02:00
|
|
|
|
|
|
|
|
|
|
|
static int
|
2000-06-04 19:07:49 +02:00
|
|
|
wtmpx_perform_logout(struct logininfo *li)
|
|
|
|
{
|
2000-06-03 16:57:40 +02:00
|
|
|
struct utmpx utx;
|
|
|
|
|
|
|
|
construct_utmpx(li, &utx);
|
2004-09-12 07:18:55 +02:00
|
|
|
return (wtmpx_write(li, &utx));
|
2000-06-04 19:07:49 +02:00
|
|
|
}
|
2000-06-03 16:57:40 +02:00
|
|
|
|
|
|
|
|
|
|
|
int
|
2000-06-04 19:07:49 +02:00
|
|
|
wtmpx_write_entry(struct logininfo *li)
|
|
|
|
{
|
2000-06-03 16:57:40 +02:00
|
|
|
switch(li->type) {
|
|
|
|
case LTYPE_LOGIN:
|
2004-09-12 07:18:55 +02:00
|
|
|
return (wtmpx_perform_login(li));
|
2000-06-03 16:57:40 +02:00
|
|
|
case LTYPE_LOGOUT:
|
2004-09-12 07:18:55 +02:00
|
|
|
return (wtmpx_perform_logout(li));
|
2000-06-03 16:57:40 +02:00
|
|
|
default:
|
2004-09-12 07:25:17 +02:00
|
|
|
logit("%s: invalid type field", __func__);
|
2004-09-12 07:18:55 +02:00
|
|
|
return (0);
|
2000-06-03 16:57:40 +02:00
|
|
|
}
|
2000-06-04 19:07:49 +02:00
|
|
|
}
|
2000-06-03 16:57:40 +02:00
|
|
|
|
2000-06-19 10:20:03 +02:00
|
|
|
/* Please see the notes above wtmp_islogin() for information about the
|
|
|
|
next two functions */
|
|
|
|
|
|
|
|
/* Return true if this wtmpx entry indicates a login */
|
|
|
|
static int
|
|
|
|
wtmpx_islogin(struct logininfo *li, struct utmpx *utx)
|
|
|
|
{
|
2010-01-09 08:18:04 +01:00
|
|
|
if (strncmp(li->username, utx->ut_user,
|
|
|
|
MIN_SIZEOF(li->username, utx->ut_user)) == 0 ) {
|
2000-06-27 03:18:27 +02:00
|
|
|
# ifdef HAVE_TYPE_IN_UTMPX
|
2000-06-19 10:20:03 +02:00
|
|
|
if (utx->ut_type == USER_PROCESS)
|
2004-09-12 07:18:55 +02:00
|
|
|
return (1);
|
2000-06-27 03:18:27 +02:00
|
|
|
# else
|
2004-09-12 07:18:55 +02:00
|
|
|
return (1);
|
2000-06-27 03:18:27 +02:00
|
|
|
# endif
|
2000-06-19 10:20:03 +02:00
|
|
|
}
|
2004-09-12 07:18:55 +02:00
|
|
|
return (0);
|
2000-06-19 10:20:03 +02:00
|
|
|
}
|
|
|
|
|
2000-06-03 16:57:40 +02:00
|
|
|
|
|
|
|
int
|
2000-06-04 19:07:49 +02:00
|
|
|
wtmpx_get_entry(struct logininfo *li)
|
|
|
|
{
|
2000-06-03 16:57:40 +02:00
|
|
|
struct stat st;
|
|
|
|
struct utmpx utx;
|
2000-06-19 10:20:03 +02:00
|
|
|
int fd, found=0;
|
|
|
|
|
|
|
|
/* Clear the time entries */
|
|
|
|
li->tv_sec = li->tv_usec = 0;
|
2000-06-03 16:57:40 +02:00
|
|
|
|
|
|
|
if ((fd = open(WTMPX_FILE, O_RDONLY)) < 0) {
|
2005-07-17 09:04:47 +02:00
|
|
|
logit("%s: problem opening %s: %s", __func__,
|
2000-06-03 16:57:40 +02:00
|
|
|
WTMPX_FILE, strerror(errno));
|
2004-09-12 07:18:55 +02:00
|
|
|
return (0);
|
2000-06-03 16:57:40 +02:00
|
|
|
}
|
2001-02-05 13:42:17 +01:00
|
|
|
if (fstat(fd, &st) != 0) {
|
2005-07-17 09:04:47 +02:00
|
|
|
logit("%s: couldn't stat %s: %s", __func__,
|
2002-07-15 00:33:20 +02:00
|
|
|
WTMPX_FILE, strerror(errno));
|
2000-06-03 16:57:40 +02:00
|
|
|
close(fd);
|
2004-09-12 07:18:55 +02:00
|
|
|
return (0);
|
2000-06-03 16:57:40 +02:00
|
|
|
}
|
2001-02-05 13:42:17 +01:00
|
|
|
|
2000-06-19 10:20:03 +02:00
|
|
|
/* Seek to the start of the last struct utmpx */
|
2001-10-02 02:29:00 +02:00
|
|
|
if (lseek(fd, -(off_t)sizeof(struct utmpx), SEEK_END) == -1 ) {
|
2000-06-19 10:20:03 +02:00
|
|
|
/* probably a newly rotated wtmpx file */
|
|
|
|
close(fd);
|
2004-09-12 07:18:55 +02:00
|
|
|
return (0);
|
2000-06-19 10:20:03 +02:00
|
|
|
}
|
2000-06-03 16:57:40 +02:00
|
|
|
|
2000-06-19 10:20:03 +02:00
|
|
|
while (!found) {
|
2000-06-27 16:50:50 +02:00
|
|
|
if (atomicio(read, fd, &utx, sizeof(utx)) != sizeof(utx)) {
|
2005-07-17 09:04:47 +02:00
|
|
|
logit("%s: read of %s failed: %s", __func__,
|
2000-06-03 16:57:40 +02:00
|
|
|
WTMPX_FILE, strerror(errno));
|
|
|
|
close (fd);
|
2004-09-12 07:18:55 +02:00
|
|
|
return (0);
|
2000-06-03 16:57:40 +02:00
|
|
|
}
|
2004-09-12 07:18:55 +02:00
|
|
|
/*
|
2005-07-17 09:04:47 +02:00
|
|
|
* Logouts are recorded as a blank username on a particular
|
2004-09-12 07:18:55 +02:00
|
|
|
* line. So, we just need to find the username in struct utmpx
|
|
|
|
*/
|
|
|
|
if (wtmpx_islogin(li, &utx)) {
|
2002-07-15 00:50:51 +02:00
|
|
|
found = 1;
|
2004-09-12 07:18:55 +02:00
|
|
|
# if defined(HAVE_TV_IN_UTMPX)
|
2000-06-03 16:57:40 +02:00
|
|
|
li->tv_sec = utx.ut_tv.tv_sec;
|
2004-09-12 07:18:55 +02:00
|
|
|
# elif defined(HAVE_TIME_IN_UTMPX)
|
2000-06-03 16:57:40 +02:00
|
|
|
li->tv_sec = utx.ut_time;
|
2000-06-27 03:18:27 +02:00
|
|
|
# endif
|
2000-06-13 13:23:17 +02:00
|
|
|
line_fullname(li->line, utx.ut_line, sizeof(li->line));
|
2004-09-12 07:18:55 +02:00
|
|
|
# if defined(HAVE_HOST_IN_UTMPX)
|
2000-06-19 10:20:03 +02:00
|
|
|
strlcpy(li->hostname, utx.ut_host,
|
2004-09-12 07:18:55 +02:00
|
|
|
MIN_SIZEOF(li->hostname, utx.ut_host));
|
2000-06-27 03:18:27 +02:00
|
|
|
# endif
|
2000-06-19 10:20:03 +02:00
|
|
|
continue;
|
2000-06-03 16:57:40 +02:00
|
|
|
}
|
2001-10-02 02:29:00 +02:00
|
|
|
if (lseek(fd, -(off_t)(2 * sizeof(struct utmpx)), SEEK_CUR) == -1) {
|
2004-09-12 07:18:55 +02:00
|
|
|
close(fd);
|
|
|
|
return (0);
|
2000-06-03 16:57:40 +02:00
|
|
|
}
|
2000-06-19 10:20:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
close(fd);
|
2004-09-12 07:18:55 +02:00
|
|
|
return (1);
|
2000-06-04 19:07:49 +02:00
|
|
|
}
|
2000-06-07 13:32:13 +02:00
|
|
|
#endif /* USE_WTMPX */
|
2000-06-03 16:57:40 +02:00
|
|
|
|
|
|
|
/**
|
2000-06-04 19:07:49 +02:00
|
|
|
** Low-level libutil login() functions
|
2000-06-03 16:57:40 +02:00
|
|
|
**/
|
|
|
|
|
|
|
|
#ifdef USE_LOGIN
|
|
|
|
static int
|
2000-06-04 19:07:49 +02:00
|
|
|
syslogin_perform_login(struct logininfo *li)
|
|
|
|
{
|
2000-06-03 16:57:40 +02:00
|
|
|
struct utmp *ut;
|
|
|
|
|
2004-09-12 07:26:00 +02:00
|
|
|
ut = xmalloc(sizeof(*ut));
|
2000-06-03 16:57:40 +02:00
|
|
|
construct_utmp(li, ut);
|
|
|
|
login(ut);
|
2003-03-10 01:23:06 +01:00
|
|
|
free(ut);
|
2000-06-03 16:57:40 +02:00
|
|
|
|
2004-09-12 07:18:55 +02:00
|
|
|
return (1);
|
2000-06-04 19:07:49 +02:00
|
|
|
}
|
2000-06-03 16:57:40 +02:00
|
|
|
|
2000-06-04 19:07:49 +02:00
|
|
|
static int
|
|
|
|
syslogin_perform_logout(struct logininfo *li)
|
|
|
|
{
|
2000-06-27 03:18:27 +02:00
|
|
|
# ifdef HAVE_LOGOUT
|
2004-04-08 02:57:05 +02:00
|
|
|
char line[UT_LINESIZE];
|
2001-02-05 13:42:17 +01:00
|
|
|
|
2000-06-03 16:57:40 +02:00
|
|
|
(void)line_stripname(line, li->line, sizeof(line));
|
|
|
|
|
2004-09-12 07:18:55 +02:00
|
|
|
if (!logout(line))
|
2004-09-12 07:25:17 +02:00
|
|
|
logit("%s: logout() returned an error", __func__);
|
2000-06-27 03:18:27 +02:00
|
|
|
# ifdef HAVE_LOGWTMP
|
2004-09-12 07:18:55 +02:00
|
|
|
else
|
2000-06-03 16:57:40 +02:00
|
|
|
logwtmp(line, "", "");
|
2000-06-27 03:18:27 +02:00
|
|
|
# endif
|
2000-06-19 10:20:03 +02:00
|
|
|
/* FIXME: (ATL - if the need arises) What to do if we have
|
|
|
|
* login, but no logout? what if logout but no logwtmp? All
|
|
|
|
* routines are in libutil so they should all be there,
|
|
|
|
* but... */
|
2000-06-27 03:18:27 +02:00
|
|
|
# endif
|
2004-09-12 07:18:55 +02:00
|
|
|
return (1);
|
2000-06-04 19:07:49 +02:00
|
|
|
}
|
2000-06-03 16:57:40 +02:00
|
|
|
|
|
|
|
int
|
2000-06-04 19:07:49 +02:00
|
|
|
syslogin_write_entry(struct logininfo *li)
|
|
|
|
{
|
2000-06-03 16:57:40 +02:00
|
|
|
switch (li->type) {
|
|
|
|
case LTYPE_LOGIN:
|
2004-09-12 07:18:55 +02:00
|
|
|
return (syslogin_perform_login(li));
|
2000-06-03 16:57:40 +02:00
|
|
|
case LTYPE_LOGOUT:
|
2004-09-12 07:18:55 +02:00
|
|
|
return (syslogin_perform_logout(li));
|
2000-06-03 16:57:40 +02:00
|
|
|
default:
|
2004-09-12 07:25:17 +02:00
|
|
|
logit("%s: Invalid type field", __func__);
|
2004-09-12 07:18:55 +02:00
|
|
|
return (0);
|
2000-06-03 16:57:40 +02:00
|
|
|
}
|
2000-06-04 19:07:49 +02:00
|
|
|
}
|
2000-06-07 13:32:13 +02:00
|
|
|
#endif /* USE_LOGIN */
|
2000-06-03 16:57:40 +02:00
|
|
|
|
|
|
|
/* end of file log-syslogin.c */
|
|
|
|
|
|
|
|
/**
|
2000-06-04 19:07:49 +02:00
|
|
|
** Low-level lastlog functions
|
2000-06-03 16:57:40 +02:00
|
|
|
**/
|
|
|
|
|
|
|
|
#ifdef USE_LASTLOG
|
|
|
|
|
2009-02-12 03:12:21 +01:00
|
|
|
#if !defined(LASTLOG_WRITE_PUTUTXLINE) || !defined(HAVE_GETLASTLOGXBYNAME)
|
|
|
|
/* open the file (using filemode) and seek to the login entry */
|
2000-06-03 16:57:40 +02:00
|
|
|
static int
|
2009-02-12 03:12:21 +01:00
|
|
|
lastlog_openseek(struct logininfo *li, int *fd, int filemode)
|
2000-06-04 19:07:49 +02:00
|
|
|
{
|
2009-02-12 03:12:21 +01:00
|
|
|
off_t offset;
|
|
|
|
char lastlog_file[1024];
|
2000-06-03 16:57:40 +02:00
|
|
|
struct stat st;
|
|
|
|
|
2000-06-27 03:18:27 +02:00
|
|
|
if (stat(LASTLOG_FILE, &st) != 0) {
|
2004-09-12 07:25:17 +02:00
|
|
|
logit("%s: Couldn't stat %s: %s", __func__,
|
2004-09-12 07:18:55 +02:00
|
|
|
LASTLOG_FILE, strerror(errno));
|
|
|
|
return (0);
|
2000-06-03 16:57:40 +02:00
|
|
|
}
|
2009-02-12 03:12:21 +01:00
|
|
|
if (S_ISDIR(st.st_mode)) {
|
2004-09-12 07:18:55 +02:00
|
|
|
snprintf(lastlog_file, sizeof(lastlog_file), "%s/%s",
|
|
|
|
LASTLOG_FILE, li->username);
|
2009-02-12 03:12:21 +01:00
|
|
|
} else if (S_ISREG(st.st_mode)) {
|
|
|
|
strlcpy(lastlog_file, LASTLOG_FILE, sizeof(lastlog_file));
|
|
|
|
} else {
|
2004-09-12 07:25:17 +02:00
|
|
|
logit("%s: %.100s is not a file or directory!", __func__,
|
2004-09-12 07:18:55 +02:00
|
|
|
LASTLOG_FILE);
|
|
|
|
return (0);
|
2000-06-27 03:18:27 +02:00
|
|
|
}
|
2000-06-03 16:57:40 +02:00
|
|
|
|
2003-04-09 13:12:52 +02:00
|
|
|
*fd = open(lastlog_file, filemode, 0600);
|
2004-09-12 07:18:55 +02:00
|
|
|
if (*fd < 0) {
|
2004-09-12 07:25:17 +02:00
|
|
|
debug("%s: Couldn't open %s: %s", __func__,
|
2000-06-03 16:57:40 +02:00
|
|
|
lastlog_file, strerror(errno));
|
2004-09-12 07:18:55 +02:00
|
|
|
return (0);
|
2000-06-03 16:57:40 +02:00
|
|
|
}
|
2001-02-05 13:42:17 +01:00
|
|
|
|
2009-02-12 03:12:21 +01:00
|
|
|
if (S_ISREG(st.st_mode)) {
|
2000-08-15 02:21:17 +02:00
|
|
|
/* find this uid's offset in the lastlog file */
|
2010-11-05 00:52:37 +01:00
|
|
|
offset = (off_t) ((u_long)li->uid * sizeof(struct lastlog));
|
2000-06-03 16:57:40 +02:00
|
|
|
|
2004-09-12 07:18:55 +02:00
|
|
|
if (lseek(*fd, offset, SEEK_SET) != offset) {
|
2004-09-12 07:25:17 +02:00
|
|
|
logit("%s: %s->lseek(): %s", __func__,
|
|
|
|
lastlog_file, strerror(errno));
|
2011-01-02 11:43:59 +01:00
|
|
|
close(*fd);
|
2004-09-12 07:18:55 +02:00
|
|
|
return (0);
|
2000-08-15 02:21:17 +02:00
|
|
|
}
|
2000-06-03 16:57:40 +02:00
|
|
|
}
|
2001-02-05 13:42:17 +01:00
|
|
|
|
2004-09-12 07:18:55 +02:00
|
|
|
return (1);
|
2000-06-04 19:07:49 +02:00
|
|
|
}
|
2009-02-12 03:12:21 +01:00
|
|
|
#endif /* !LASTLOG_WRITE_PUTUTXLINE || !HAVE_GETLASTLOGXBYNAME */
|
2000-06-03 16:57:40 +02:00
|
|
|
|
2009-02-12 03:12:21 +01:00
|
|
|
#ifdef LASTLOG_WRITE_PUTUTXLINE
|
|
|
|
int
|
|
|
|
lastlog_write_entry(struct logininfo *li)
|
2000-06-04 19:07:49 +02:00
|
|
|
{
|
2009-02-12 03:12:21 +01:00
|
|
|
switch(li->type) {
|
|
|
|
case LTYPE_LOGIN:
|
|
|
|
return 1; /* lastlog written by pututxline */
|
|
|
|
default:
|
|
|
|
logit("lastlog_write_entry: Invalid type field");
|
|
|
|
return 0;
|
2000-06-27 03:18:27 +02:00
|
|
|
}
|
2000-06-04 19:07:49 +02:00
|
|
|
}
|
2009-02-12 03:12:21 +01:00
|
|
|
#else /* LASTLOG_WRITE_PUTUTXLINE */
|
2000-06-03 16:57:40 +02:00
|
|
|
int
|
2000-06-04 19:07:49 +02:00
|
|
|
lastlog_write_entry(struct logininfo *li)
|
|
|
|
{
|
2009-02-12 03:12:21 +01:00
|
|
|
struct lastlog last;
|
|
|
|
int fd;
|
|
|
|
|
2000-06-03 16:57:40 +02:00
|
|
|
switch(li->type) {
|
|
|
|
case LTYPE_LOGIN:
|
2009-02-12 03:12:21 +01:00
|
|
|
/* create our struct lastlog */
|
|
|
|
memset(&last, '\0', sizeof(last));
|
|
|
|
line_stripname(last.ll_line, li->line, sizeof(last.ll_line));
|
|
|
|
strlcpy(last.ll_host, li->hostname,
|
|
|
|
MIN_SIZEOF(last.ll_host, li->hostname));
|
|
|
|
last.ll_time = li->tv_sec;
|
|
|
|
|
|
|
|
if (!lastlog_openseek(li, &fd, O_RDWR|O_CREAT))
|
|
|
|
return (0);
|
|
|
|
|
|
|
|
/* write the entry */
|
|
|
|
if (atomicio(vwrite, fd, &last, sizeof(last)) != sizeof(last)) {
|
|
|
|
close(fd);
|
|
|
|
logit("%s: Error writing to %s: %s", __func__,
|
|
|
|
LASTLOG_FILE, strerror(errno));
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
return (1);
|
2000-06-03 16:57:40 +02:00
|
|
|
default:
|
2004-09-12 07:25:17 +02:00
|
|
|
logit("%s: Invalid type field", __func__);
|
2004-09-12 07:18:55 +02:00
|
|
|
return (0);
|
2000-06-03 16:57:40 +02:00
|
|
|
}
|
2000-06-04 19:07:49 +02:00
|
|
|
}
|
2009-02-12 03:12:21 +01:00
|
|
|
#endif /* LASTLOG_WRITE_PUTUTXLINE */
|
2000-06-03 16:57:40 +02:00
|
|
|
|
2009-02-12 03:12:21 +01:00
|
|
|
#ifdef HAVE_GETLASTLOGXBYNAME
|
|
|
|
int
|
|
|
|
lastlog_get_entry(struct logininfo *li)
|
2000-06-04 19:07:49 +02:00
|
|
|
{
|
2009-02-12 03:12:21 +01:00
|
|
|
struct lastlogx l, *ll;
|
2000-06-03 16:57:40 +02:00
|
|
|
|
2009-02-12 03:12:21 +01:00
|
|
|
if ((ll = getlastlogxbyname(li->username, &l)) == NULL) {
|
|
|
|
memset(&l, '\0', sizeof(l));
|
|
|
|
ll = &l;
|
|
|
|
}
|
|
|
|
line_fullname(li->line, ll->ll_line, sizeof(li->line));
|
|
|
|
strlcpy(li->hostname, ll->ll_host,
|
|
|
|
MIN_SIZEOF(li->hostname, ll->ll_host));
|
|
|
|
li->tv_sec = ll->ll_tv.tv_sec;
|
|
|
|
li->tv_usec = ll->ll_tv.tv_usec;
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
#else /* HAVE_GETLASTLOGXBYNAME */
|
2000-06-03 16:57:40 +02:00
|
|
|
int
|
2000-06-04 19:07:49 +02:00
|
|
|
lastlog_get_entry(struct logininfo *li)
|
|
|
|
{
|
2000-06-03 16:57:40 +02:00
|
|
|
struct lastlog last;
|
2003-01-07 06:46:58 +01:00
|
|
|
int fd, ret;
|
2000-06-03 16:57:40 +02:00
|
|
|
|
2001-10-22 08:49:22 +02:00
|
|
|
if (!lastlog_openseek(li, &fd, O_RDONLY))
|
2003-01-07 06:46:58 +01:00
|
|
|
return (0);
|
2001-10-22 08:49:22 +02:00
|
|
|
|
2003-01-07 06:46:58 +01:00
|
|
|
ret = atomicio(read, fd, &last, sizeof(last));
|
2001-10-22 08:49:22 +02:00
|
|
|
close(fd);
|
|
|
|
|
2003-01-07 06:46:58 +01:00
|
|
|
switch (ret) {
|
|
|
|
case 0:
|
|
|
|
memset(&last, '\0', sizeof(last));
|
|
|
|
/* FALLTHRU */
|
|
|
|
case sizeof(last):
|
2009-02-12 03:12:21 +01:00
|
|
|
line_fullname(li->line, last.ll_line, sizeof(li->line));
|
|
|
|
strlcpy(li->hostname, last.ll_host,
|
|
|
|
MIN_SIZEOF(li->hostname, last.ll_host));
|
|
|
|
li->tv_sec = last.ll_time;
|
2003-01-07 06:46:58 +01:00
|
|
|
return (1);
|
|
|
|
case -1:
|
2003-11-21 13:48:55 +01:00
|
|
|
error("%s: Error reading from %s: %s", __func__,
|
2003-01-07 06:46:58 +01:00
|
|
|
LASTLOG_FILE, strerror(errno));
|
|
|
|
return (0);
|
|
|
|
default:
|
|
|
|
error("%s: Error reading from %s: Expecting %d, got %d",
|
2005-11-22 09:55:13 +01:00
|
|
|
__func__, LASTLOG_FILE, (int)sizeof(last), ret);
|
2003-01-07 06:46:58 +01:00
|
|
|
return (0);
|
|
|
|
}
|
2001-10-22 08:49:22 +02:00
|
|
|
|
2003-01-07 06:46:58 +01:00
|
|
|
/* NOTREACHED */
|
|
|
|
return (0);
|
2000-06-04 19:07:49 +02:00
|
|
|
}
|
2009-02-12 03:12:21 +01:00
|
|
|
#endif /* HAVE_GETLASTLOGXBYNAME */
|
2000-06-07 13:32:13 +02:00
|
|
|
#endif /* USE_LASTLOG */
|
2005-02-02 13:30:24 +01:00
|
|
|
|
2010-04-09 10:13:27 +02:00
|
|
|
#if defined(USE_UTMPX) && defined(HAVE_SETUTXDB) && \
|
|
|
|
defined(UTXDB_LASTLOGIN) && defined(HAVE_GETUTXUSER)
|
|
|
|
int
|
|
|
|
utmpx_get_entry(struct logininfo *li)
|
|
|
|
{
|
|
|
|
struct utmpx *utx;
|
|
|
|
|
|
|
|
if (setutxdb(UTXDB_LASTLOGIN, NULL) != 0)
|
|
|
|
return (0);
|
|
|
|
utx = getutxuser(li->username);
|
|
|
|
if (utx == NULL) {
|
|
|
|
endutxent();
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
line_fullname(li->line, utx->ut_line,
|
|
|
|
MIN_SIZEOF(li->line, utx->ut_line));
|
|
|
|
strlcpy(li->hostname, utx->ut_host,
|
|
|
|
MIN_SIZEOF(li->hostname, utx->ut_host));
|
|
|
|
li->tv_sec = utx->ut_tv.tv_sec;
|
|
|
|
li->tv_usec = utx->ut_tv.tv_usec;
|
|
|
|
endutxent();
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
#endif /* USE_UTMPX && HAVE_SETUTXDB && UTXDB_LASTLOGIN && HAVE_GETUTXUSER */
|
|
|
|
|
2005-02-02 13:30:24 +01:00
|
|
|
#ifdef USE_BTMP
|
|
|
|
/*
|
|
|
|
* Logs failed login attempts in _PATH_BTMP if that exists.
|
|
|
|
* The most common login failure is to give password instead of username.
|
|
|
|
* So the _PATH_BTMP file checked for the correct permission, so that
|
|
|
|
* only root can read it.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
2019-01-19 23:58:45 +01:00
|
|
|
record_failed_login(struct ssh *ssh, const char *username, const char *hostname,
|
2005-02-02 13:30:24 +01:00
|
|
|
const char *ttyn)
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
struct utmp ut;
|
|
|
|
struct sockaddr_storage from;
|
2005-11-22 09:55:13 +01:00
|
|
|
socklen_t fromlen = sizeof(from);
|
2005-02-02 13:30:24 +01:00
|
|
|
struct sockaddr_in *a4;
|
|
|
|
struct sockaddr_in6 *a6;
|
|
|
|
time_t t;
|
|
|
|
struct stat fst;
|
|
|
|
|
|
|
|
if (geteuid() != 0)
|
|
|
|
return;
|
|
|
|
if ((fd = open(_PATH_BTMP, O_WRONLY | O_APPEND)) < 0) {
|
|
|
|
debug("Unable to open the btmp file %s: %s", _PATH_BTMP,
|
|
|
|
strerror(errno));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (fstat(fd, &fst) < 0) {
|
|
|
|
logit("%s: fstat of %s failed: %s", __func__, _PATH_BTMP,
|
|
|
|
strerror(errno));
|
|
|
|
goto out;
|
|
|
|
}
|
2010-11-24 00:36:15 +01:00
|
|
|
if((fst.st_mode & (S_IXGRP | S_IRWXO)) || (fst.st_uid != 0)){
|
2005-02-02 13:30:24 +01:00
|
|
|
logit("Excess permission or bad ownership on file %s",
|
|
|
|
_PATH_BTMP);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(&ut, 0, sizeof(ut));
|
|
|
|
/* strncpy because we don't necessarily want nul termination */
|
|
|
|
strncpy(ut.ut_user, username, sizeof(ut.ut_user));
|
|
|
|
strlcpy(ut.ut_line, "ssh:notty", sizeof(ut.ut_line));
|
|
|
|
|
|
|
|
time(&t);
|
|
|
|
ut.ut_time = t; /* ut_time is not always a time_t */
|
|
|
|
ut.ut_type = LOGIN_PROCESS;
|
|
|
|
ut.ut_pid = getpid();
|
|
|
|
|
|
|
|
/* strncpy because we don't necessarily want nul termination */
|
|
|
|
strncpy(ut.ut_host, hostname, sizeof(ut.ut_host));
|
|
|
|
|
2019-01-19 23:58:45 +01:00
|
|
|
if (ssh_packet_connection_is_on_socket(ssh) &&
|
|
|
|
getpeername(ssh_packet_get_connection_in(ssh),
|
2005-02-02 13:30:24 +01:00
|
|
|
(struct sockaddr *)&from, &fromlen) == 0) {
|
|
|
|
ipv64_normalise_mapped(&from, &fromlen);
|
|
|
|
if (from.ss_family == AF_INET) {
|
|
|
|
a4 = (struct sockaddr_in *)&from;
|
|
|
|
memcpy(&ut.ut_addr, &(a4->sin_addr),
|
|
|
|
MIN_SIZEOF(ut.ut_addr, a4->sin_addr));
|
|
|
|
}
|
|
|
|
#ifdef HAVE_ADDR_V6_IN_UTMP
|
|
|
|
if (from.ss_family == AF_INET6) {
|
|
|
|
a6 = (struct sockaddr_in6 *)&from;
|
|
|
|
memcpy(&ut.ut_addr_v6, &(a6->sin6_addr),
|
|
|
|
MIN_SIZEOF(ut.ut_addr_v6, a6->sin6_addr));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
if (atomicio(vwrite, fd, &ut, sizeof(ut)) != sizeof(ut))
|
|
|
|
error("Failed to write to %s: %s", _PATH_BTMP,
|
|
|
|
strerror(errno));
|
|
|
|
|
|
|
|
out:
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
#endif /* USE_BTMP */
|