Remove utmpx stub from libulog.

I'm not increasing the shlib major version for this, because not a
single application outside the base system should have used these
functions in such a short timespan.

Rewrite ulog_login(3) and ulog_logout(3) to build on top of the utmpx
implementation in libc.
This commit is contained in:
Ed Schouten 2010-01-13 18:53:06 +00:00
parent c36be85f01
commit 691ac623d7
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=202215
15 changed files with 52 additions and 1029 deletions

View file

@ -14,6 +14,14 @@
# The file is partitioned: OLD_FILES first, then OLD_LIBS and OLD_DIRS last.
#
# 20100113: remove utmp.h, replace it by utmpx.h
OLD_FILES+=usr/share/man/man3/ulog_endutxent.3.gz
OLD_FILES+=usr/share/man/man3/ulog_getutxent.3.gz
OLD_FILES+=usr/share/man/man3/ulog_getutxline.3.gz
OLD_FILES+=usr/share/man/man3/ulog_getutxuser.3.gz
OLD_FILES+=usr/share/man/man3/ulog_pututxline.3.gz
OLD_FILES+=usr/share/man/man3/ulog_setutxent.3.gz
OLD_FILES+=usr/share/man/man3/ulog_setutxfile.3.gz
# 20100105: new userland semaphore implementation
OLD_FILES+=usr/include/sys/semaphore.h
# 20100103: ntptrace(8) removed

View file

@ -7,25 +7,21 @@ SHLIBDIR?=/lib
LIB= ulog
SHLIB_MAJOR= 0
INCS= ulog.h utempter.h
SRCS= ulog.h ulog_getutxent.c ulog_internal.h ulog_login.c \
ulog_login_pseudo.c ulog_pututxline.c ulog_util.c utempter.c
SRCS= ulog.h ulog_login.c ulog_login_pseudo.c utempter.c
MAN= ulog_getutxent.3 ulog_login.3 ulog_setutxfile.3 \
utempter_add_record.3
MLINKS+=ulog_getutxent.3 ulog_endutxent.3 \
ulog_getutxent.3 ulog_getutxline.3 \
ulog_getutxent.3 ulog_pututxline.3 \
ulog_getutxent.3 ulog_setutxent.3 \
ulog_login.3 ulog_login_pseudo.3 \
MAN= ulog_login.3 utempter_add_record.3
MLINKS+=ulog_login.3 ulog_login_pseudo.3 \
ulog_login.3 ulog_logout.3 \
ulog_login.3 ulog_logout_pseudo.3 \
ulog_setutxfile.3 ulog_getutxuser.3 \
utempter_add_record.3 utempter_remove_added_record.3 \
utempter_add_record.3 utempter_remove_record.3 \
utempter_add_record.3 addToUtmp.3 \
utempter_remove_added_record.3 removeFromUtmp.3 \
utempter_remove_record.3 removeLineFromUtmp.3
DPADD= ${LIBMD}
LDADD= -lmd
VERSION_DEF= ${.CURDIR}/../libc/Versions.def
SYMBOL_MAPS= ${.CURDIR}/Symbol.map

View file

@ -3,17 +3,10 @@
*/
FBSD_1.2 {
ulog_endutxent;
ulog_getutxent;
ulog_getutxline;
ulog_getutxuser;
ulog_login;
ulog_login_pseudo;
ulog_logout;
ulog_logout_pseudo;
ulog_pututxline;
ulog_setutxent;
ulog_setutxfile;
addToUtmp;
removeFromUtmp;

View file

@ -30,79 +30,12 @@
#define _ULOG_H_
#include <sys/cdefs.h>
#include <sys/_timeval.h>
#include <sys/_types.h>
#ifndef _PID_T_DECLARED
typedef __pid_t pid_t;
#define _PID_T_DECLARED
#endif
/*
* libulog.
*
* This library is provided as a migratory tool towards <utmpx.h>. We
* cannot yet implement <utmpx.h>, because our on-disk file format lacks
* various fields. <utmpx.h> also has some shortcomings. Ideally we
* want to allow logging of user login records generated by unprivileged
* processes as well, provided that they hold a file descriptor to a
* pseudo-terminal master device.
*
* This library (or at least parts of it) will hopefully deprecate over
* time, when we provide the <utmpx.h> API.
*/
struct ulog_utmpx {
char ut_user[32];
char ut_id[8]; /* XXX: unsupported. */
char ut_line[32];
char ut_host[256];
pid_t ut_pid; /* XXX: unsupported. */
short ut_type;
#define EMPTY 0
#define BOOT_TIME 1
#define OLD_TIME 2
#define NEW_TIME 3
#define USER_PROCESS 4
#define INIT_PROCESS 5 /* XXX: unsupported. */
#define LOGIN_PROCESS 6 /* XXX: unsupported. */
#define DEAD_PROCESS 7
#define SHUTDOWN_TIME 8
struct timeval ut_tv;
};
__BEGIN_DECLS
/* POSIX routines. */
void ulog_endutxent(void);
struct ulog_utmpx *ulog_getutxent(void);
#if 0
struct ulog_utmpx *ulog_getutxid(const struct ulog_utmpx *);
#endif
struct ulog_utmpx *ulog_getutxline(const struct ulog_utmpx *);
struct ulog_utmpx *ulog_pututxline(const struct ulog_utmpx *);
void ulog_setutxent(void);
/* Extensions. */
struct ulog_utmpx *ulog_getutxuser(const char *);
int ulog_setutxfile(int, const char *);
#define UTXI_TTY 0
#define UTXI_TIME 1
#define UTXI_USER 2
/* Login/logout utility functions. */
void ulog_login(const char *, const char *, const char *);
void ulog_login_pseudo(int, const char *);
void ulog_logout(const char *);
void ulog_logout_pseudo(int);
__END_DECLS
#ifdef _ULOG_POSIX_NAMES
#define utmpx ulog_utmpx
#define endutxent ulog_endutxent
#define getutxent ulog_getutxent
#define getutxline ulog_getutxline
#define pututxline ulog_pututxline
#define setutxent ulog_setutxent
#endif /* _ULOG_POSIX_NAMES */
#endif /* !_ULOG_H_ */

View file

@ -1,183 +0,0 @@
.\" Copyright (c) 2009 Ed Schouten <ed@FreeBSD.org>
.\" 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.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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.
.\"
.\" $FreeBSD$
.\"
.Dd December 5, 2009
.Os
.Dt ULOG_GETUTXENT 3
.Sh NAME
.Nm ulog_getutxent ,
.Nm ulog_getutxline ,
.Nm ulog_pututxline ,
.Nm ulog_setutxent ,
.Nm ulog_endutxent
.Nd read user login records
.Sh LIBRARY
.Lb libulog
.Sh SYNOPSIS
.In ulog.h
.Ft struct ulog_utmpx *
.Fn ulog_getutxent "void"
.Ft struct ulog_utmpx *
.Fn ulog_getutxline "const struct ulog_utmpx *line"
.Ft struct ulog_utmpx *
.Fn ulog_pututxline "const struct ulog_utmpx *utmpx"
.Ft void
.Fn ulog_setutxent "void"
.Ft void
.Fn ulog_endutxent "void"
.Sh DESCRIPTION
The
.Fn ulog_getutxent
function returns a pointer to an object, with the following structure,
containing stored information of an active user login session.
.Bd -literal
struct ulog_utmpx {
char ut_user[]; /* Username. */
char ut_id[]; /* Private data. */
char ut_line[]; /* TTY device. */
char ut_host[]; /* Remote hostname. */
pid_t ut_pid; /* Process identifier. */
short ut_type; /* Type of entry. */
struct timeval ut_tv; /* Timestamp. */
};
.Ed
.Pp
The fields are as follows:
.Bl -tag -width ut_user
.It Fa ut_user
The username of the logged in user.
.It Fa ut_id
Private data that can be used to later identify the record.
This implementation is not capable of storing this value on disk.
.It Fa ut_line
The pathname of the TTY device, without the leading
.Pa /dev/
directory.
.It Fa ut_host
An optional hostname of a remote system, if the login session is
provided through a networked login service.
.It Fa ut_pid
Process identifier of the session leader of the login session.
This implementation is not capable of storing this value on disk.
.It Fa ut_type
The
.Fa ut_type
field contains the type of the message, which may have one of the
following values:
.Bl -tag -width SHUTDOWN_TIME
.It Dv EMPTY
No valid user accounting information.
.It Dv BOOT_TIME
Identifies time of system boot.
.It Dv OLD_TIME
Identifies time when system clock changed.
.It Dv NEW_TIME
Identifies time after system clock changed.
.It Dv USER_PROCESS
Identifies a process.
.It Dv INIT_PROCESS
Identifies a process spawned by the init process.
.It Dv LOGIN_PROCESS
Identifies the session leader of a logged-in user.
.It Dv DEAD_PROCESS
Identifies a session leader who has exited.
.It Dv SHUTDOWN_TIME
Identifies time when system was shut down.
.El
.It Fa ut_tv
Timestamp indicating when the entry was last modified.
.El
.Pp
This implementation guarantees all strings returned in the structure to
be null terminated.
.Pp
The
.Fn ulog_getutxent
function reads the next entry from the utmp file, opening the file if
necessary.
The
.Fn ulog_getutxline
function reads entries from the utmp file, until finding an entry which
shares the same
.Fa ut_line
as the structure
.Fa line .
The
.Fn ulog_setutxent
opens the file, closing it first if already opened.
The
.Fn ulog_endutxent
function closes any open files.
.Pp
The
.Fn ulog_getutxent
and
.Fn ulog_getutxline
functions read from the beginning of the file until and EOF is
encountered.
.Pp
The
.Fn ulog_pututxline
function writes a new entry to the file.
.Sh RETURN VALUES
The
.Fn ulog_getutxent
and
.Fn ulog_getutxline
functions return a null pointer on EOF or error.
.Sh SEE ALSO
.Xr ulog_login 3 ,
.Xr ulog_setutxfile 3 ,
.Xr utmp 5
.Sh STANDARDS
This interface is similar to
.In utmpx.h
described in
.St -p1003.1-2008 ,
but incompatible.
The underlying file format does not allow a correctly behaving
implementation of the standardized interface.
.Pp
This programming interface has been designed to ease the migration
towards
.In utmpx.h .
If
.Dv _ULOG_POSIX_NAMES
is set before inclusion of
.In ulog.h ,
it is also possible to use the
.Vt utmpx
structure and the
.Fn getutxent ,
.Fn getutxline ,
.Fn pututxline ,
.Fn setutxent
and
.Fn endutxent
functions.
.Sh HISTORY
These functions appeared in
.Fx 9.0 .

View file

@ -1,317 +0,0 @@
/*-
* Copyright (c) 2009 Ed Schouten <ed@FreeBSD.org>
* 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.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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.
*/
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include <sys/param.h>
#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <timeconv.h>
#include "ulog_internal.h"
static FILE *ufile;
static int ufileindex = -1;
void
ulog_endutxent(void)
{
if (ufile != NULL)
fclose(ufile);
ufile = NULL;
}
/*
* Conversion from on-disk formats to generic ulog_utmpx structure.
*/
static void
ulog_futmp_to_utmpx(const struct futmp *ut, struct ulog_utmpx *utx)
{
memset(utx, 0, sizeof *utx);
#define COPY_STRING(field) do { \
strncpy(utx->ut_ ## field, ut->ut_ ## field, \
MIN(sizeof utx->ut_ ## field - 1, sizeof ut->ut_ ## field));\
} while (0)
#define MATCH(field, value) (strncmp(ut->ut_ ## field, (value), \
sizeof(ut->ut_ ## field)) == 0)
if (MATCH(user, "reboot") && MATCH(line, "~"))
utx->ut_type = BOOT_TIME;
else if (MATCH(user, "date") && MATCH(line, "|"))
utx->ut_type = OLD_TIME;
else if (MATCH(user, "date") && MATCH(line, "{"))
utx->ut_type = NEW_TIME;
else if (MATCH(user, "shutdown") && MATCH(line, "~"))
utx->ut_type = SHUTDOWN_TIME;
else if (MATCH(user, "") && MATCH(host, "") && !MATCH(line, "")) {
utx->ut_type = DEAD_PROCESS;
/* XXX: ut_id and ut_pid missing. ut_line not needed. */
COPY_STRING(line);
} else if (!MATCH(user, "") && !MATCH(line, "") && ut->ut_time != 0) {
utx->ut_type = USER_PROCESS;
/* XXX: ut_id and ut_pid missing. */
COPY_STRING(user);
COPY_STRING(line);
COPY_STRING(host);
} else {
/* Only set ut_type for EMPTY. */
utx->ut_type = EMPTY;
return;
}
#undef COPY_STRING
#undef MATCH
utx->ut_tv.tv_sec = _time32_to_time(ut->ut_time);
utx->ut_tv.tv_usec = 0;
}
static void
ulog_flastlog_to_utmpx(const struct flastlog *ll, struct ulog_utmpx *utx)
{
memset(utx, 0, sizeof *utx);
#define COPY_STRING(field) do { \
strncpy(utx->ut_ ## field, ll->ll_ ## field, \
MIN(sizeof utx->ut_ ## field - 1, sizeof ll->ll_ ## field));\
} while (0)
#define MATCH(field, value) (strncmp(ll->ll_ ## field, (value), \
sizeof(ll->ll_ ## field)) == 0)
if (!MATCH(line, "") && ll->ll_time != 0) {
utx->ut_type = USER_PROCESS;
/* XXX: ut_id and ut_pid missing. */
COPY_STRING(line);
COPY_STRING(host);
} else {
/* Only set ut_type for EMPTY. */
utx->ut_type = EMPTY;
return;
}
#undef COPY_STRING
#undef MATCH
utx->ut_tv.tv_sec = _time32_to_time(ll->ll_time);
utx->ut_tv.tv_usec = 0;
}
/*
* File I/O.
*/
static inline off_t
ulog_tell(void)
{
if (ufileindex == UTXI_USER)
return (ftello(ufile) / sizeof(struct flastlog));
else
return (ftello(ufile) / sizeof(struct futmp));
}
static struct ulog_utmpx *
ulog_read(off_t off, int whence, int resolve_user)
{
static struct ulog_utmpx utx;
if (ufile == NULL)
ulog_setutxent();
if (ufile == NULL)
return (NULL);
/* Only allow seeking to move forward. */
if (whence == SEEK_SET && ulog_tell() > off)
return (NULL);
if (ufileindex == UTXI_USER) {
struct flastlog ll;
struct passwd *pw = NULL;
uid_t uid;
if (fseeko(ufile, off * sizeof ll, whence) != 0)
return (NULL);
uid = ulog_tell();
if (fread(&ll, sizeof ll, 1, ufile) != 1)
return (NULL);
ulog_flastlog_to_utmpx(&ll, &utx);
if (utx.ut_type == USER_PROCESS && resolve_user)
pw = getpwuid(uid);
if (pw != NULL)
strlcpy(utx.ut_user, pw->pw_name, sizeof utx.ut_user);
else
sprintf(utx.ut_user, "%u", (unsigned int)uid);
} else {
struct futmp ut;
if (fseeko(ufile, off * sizeof(struct futmp), whence) != 0)
return (NULL);
if (fread(&ut, sizeof ut, 1, ufile) != 1)
return (NULL);
ulog_futmp_to_utmpx(&ut, &utx);
}
return (&utx);
}
/*
* getutxent().
*
* Read the next entry from the file.
*/
struct ulog_utmpx *
ulog_getutxent(void)
{
return ulog_read(0, SEEK_CUR, 1);
}
/*
* ulog_getutxline().
*
* Read entries from the file, until reaching an entry which matches the
* provided TTY device name. We can optimize the case for utmp files,
* because they are indexed by TTY device name.
*/
struct ulog_utmpx *
ulog_getutxline(const struct ulog_utmpx *line)
{
struct ulog_utmpx *utx;
if (ufile == NULL)
ulog_setutxent();
if (ufile == NULL)
return (NULL);
if (ufileindex == UTXI_TTY) {
unsigned int slot;
slot = ulog_ttyslot(line->ut_line);
if (slot == 0)
return (NULL);
utx = ulog_read(slot, SEEK_SET, 1);
if (utx->ut_type == USER_PROCESS &&
strcmp(utx->ut_line, line->ut_line) == 0)
return (utx);
return (NULL);
} else {
for (;;) {
utx = ulog_read(0, SEEK_CUR, 1);
if (utx == NULL)
return (NULL);
if (utx->ut_type == USER_PROCESS &&
strcmp(utx->ut_line, line->ut_line) == 0)
return (utx);
}
}
}
/*
* ulog_getutxuser().
*
* Read entries from the file, until reaching an entry which matches the
* provided username. We can optimize the case for lastlog files,
* because they are indexed by user ID.
*/
struct ulog_utmpx *
ulog_getutxuser(const char *user)
{
struct ulog_utmpx *utx;
if (ufileindex == UTXI_USER) {
struct passwd *pw;
pw = getpwnam(user);
if (pw == NULL)
return (NULL);
utx = ulog_read(pw->pw_uid, SEEK_SET, 0);
if (utx != NULL)
strlcpy(utx->ut_user, user, sizeof utx->ut_user);
return (utx);
} else {
for (;;) {
utx = ulog_read(0, SEEK_CUR, 1);
if (utx == NULL)
return (NULL);
if (utx->ut_type == USER_PROCESS &&
strcmp(utx->ut_user, user) == 0)
return (utx);
}
}
}
/*
* ulog_setutxfile().
*
* Switch to a different record file. When no filename is provided, the
* system default is opened.
*/
int
ulog_setutxfile(int uidx, const char *file)
{
/* Supply default files. */
switch (uidx) {
case UTXI_TTY:
if (file == NULL)
file = _PATH_UTMP;
break;
case UTXI_TIME:
if (file == NULL)
file = _PATH_WTMP;
break;
case UTXI_USER:
if (file == NULL)
file = _PATH_LASTLOG;
break;
default:
return (-1);
}
if (ufile != NULL)
fclose(ufile);
ufile = fopen(file, "r");
ufileindex = uidx;
if (ufile == NULL)
return (-1);
return (0);
}
/*
* ulog_endutxfile().
*
* Close any opened files.
*/
void
ulog_setutxent(void)
{
ulog_setutxfile(UTXI_TTY, NULL);
}

View file

@ -1,60 +0,0 @@
/*-
* Copyright (c) 2009 Ed Schouten <ed@FreeBSD.org>
* 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.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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.
*
* $FreeBSD$
*/
#ifndef _ULOG_INTERNAL_H_
#define _ULOG_INTERNAL_H_
#include <stdint.h>
#include "ulog.h"
unsigned int ulog_ttyslot(const char *);
/*
* On-disk format.
*/
#define _PATH_UTMP "/var/run/utmp"
#define _PATH_WTMP "/var/log/wtmp"
struct futmp {
char ut_line[8];
char ut_user[16];
char ut_host[16];
int32_t ut_time;
};
#define _PATH_LASTLOG "/var/log/lastlog"
struct flastlog {
int32_t ll_time;
char ll_line[8];
char ll_host[16];
};
#endif /* !_ULOG_INTERNAL_H_ */

View file

@ -90,13 +90,10 @@ and
.Fn ulog_logout_pseudo
functions spawn a privileged process to perform the actual logging.
.Sh SEE ALSO
.Xr getuid 3 ,
.Xr login 3 ,
.Xr logout 3 ,
.Xr posix_openpt 2 ,
.Xr getuid 3 ,
.Xr ptsname 3 ,
.Xr ulog_getutxent 3 ,
.Xr utmp 5
.Xr pututxline 3
.Sh HISTORY
These functions appeared in
.Fx 9.0 .

View file

@ -27,49 +27,58 @@
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include <sys/param.h>
#include <sys/time.h>
#include <paths.h>
#include <sha.h>
#include <string.h>
#include <unistd.h>
#include <utmpx.h>
#include "ulog.h"
#include "ulog_internal.h"
void
ulog_login(const char *line, const char *user, const char *host)
static void
ulog_fill(struct utmpx *utx, const char *line)
{
struct ulog_utmpx utx;
SHA_CTX c;
char id[SHA_DIGEST_LENGTH];
/* Remove /dev/ component. */
if (strncmp(line, _PATH_DEV, sizeof _PATH_DEV - 1) == 0)
line += sizeof _PATH_DEV - 1;
memset(&utx, 0, sizeof utx);
memset(utx, 0, sizeof *utx);
/* XXX: ut_id, ut_pid missing. */
utx->ut_pid = getpid();
gettimeofday(&utx->ut_tv, NULL);
strncpy(utx->ut_line, line, sizeof utx->ut_line);
SHA1_Init(&c);
SHA1_Update(&c, "libulog", 7);
SHA1_Update(&c, utx->ut_line, sizeof utx->ut_line);
SHA_Final(id, &c);
memcpy(utx->ut_id, id, MIN(sizeof utx->ut_id, sizeof id));
}
void
ulog_login(const char *line, const char *user, const char *host)
{
struct utmpx utx;
ulog_fill(&utx, line);
utx.ut_type = USER_PROCESS;
strncpy(utx.ut_line, line, sizeof utx.ut_line);
strncpy(utx.ut_user, user, sizeof utx.ut_user);
if (host != NULL)
strncpy(utx.ut_host, host, sizeof utx.ut_host);
gettimeofday(&utx.ut_tv, NULL);
ulog_pututxline(&utx);
pututxline(&utx);
}
void
ulog_logout(const char *line)
{
struct ulog_utmpx utx;
struct utmpx utx;
/* Remove /dev/ component. */
if (strncmp(line, _PATH_DEV, sizeof _PATH_DEV - 1) == 0)
line += sizeof _PATH_DEV - 1;
memset(&utx, 0, sizeof utx);
/* XXX: ut_id, ut_pid missing. ut_line not needed */
ulog_fill(&utx, line);
utx.ut_type = DEAD_PROCESS;
strncpy(utx.ut_line, line, sizeof utx.ut_line);
gettimeofday(&utx.ut_tv, NULL);
ulog_pututxline(&utx);
pututxline(&utx);
}

View file

@ -34,8 +34,7 @@ __FBSDID("$FreeBSD$");
#include <stdlib.h>
#include <sysexits.h>
#include <unistd.h>
#include "ulog_internal.h"
#include "ulog.h"
#define _PATH_ULOG_HELPER "/usr/libexec/ulog-helper"

View file

@ -1,208 +0,0 @@
/*-
* Copyright (c) 2009 Ed Schouten <ed@FreeBSD.org>
* 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.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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.
*/
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include <sys/param.h>
#include <fcntl.h>
#include <pwd.h>
#include <string.h>
#include <timeconv.h>
#include <unistd.h>
#include "ulog_internal.h"
static void
ulog_utmpx_to_futmp(const struct ulog_utmpx *utx, struct futmp *ut)
{
memset(ut, 0, sizeof *ut);
#define COPY_STRING(field) do { \
strncpy(ut->ut_ ## field, utx->ut_ ## field, \
MIN(sizeof ut->ut_ ## field, sizeof utx->ut_ ## field)); \
} while (0)
switch (utx->ut_type) {
case BOOT_TIME:
strcpy(ut->ut_user, "reboot");
ut->ut_line[0] = '~';
break;
case OLD_TIME:
strcpy(ut->ut_user, "date");
ut->ut_line[0] = '|';
break;
case NEW_TIME:
strcpy(ut->ut_user, "date");
ut->ut_line[0] = '{';
break;
case USER_PROCESS:
COPY_STRING(user);
COPY_STRING(line);
COPY_STRING(host);
break;
case DEAD_PROCESS:
COPY_STRING(line);
break;
case SHUTDOWN_TIME:
strcpy(ut->ut_user, "shutdown");
ut->ut_line[0] = '~';
break;
}
#undef COPY_STRING
ut->ut_time = _time_to_time32(utx->ut_tv.tv_sec);
}
static void
ulog_utmpx_to_flastlog(const struct ulog_utmpx *utx, struct flastlog *ll)
{
memset(ll, 0, sizeof *ll);
#define COPY_STRING(field) do { \
strncpy(ll->ll_ ## field, utx->ut_ ## field, \
MIN(sizeof ll->ll_ ## field, sizeof utx->ut_ ## field)); \
} while (0)
switch (utx->ut_type) {
case USER_PROCESS:
COPY_STRING(line);
COPY_STRING(host);
break;
}
#undef COPY_STRING
ll->ll_time = _time_to_time32(utx->ut_tv.tv_sec);
}
static void
ulog_write_utmp_fast(const struct futmp *ut)
{
unsigned int idx;
char line[sizeof ut->ut_line + 1];
int fd;
if ((fd = open(_PATH_UTMP, O_WRONLY|O_CREAT, 0644)) < 0)
return;
strlcpy(line, ut->ut_line, sizeof line);
idx = ulog_ttyslot(line);
if (idx > 0) {
lseek(fd, (off_t)(idx * sizeof *ut), SEEK_SET);
write(fd, ut, sizeof *ut);
}
close(fd);
}
static int
ulog_write_utmp_slow(const struct futmp *ut)
{
struct futmp utf;
int fd, found;
if ((fd = open(_PATH_UTMP, O_RDWR, 0)) < 0)
return (0);
found = 0;
while (read(fd, &utf, sizeof utf) == sizeof utf) {
if (utf.ut_user[0] == '\0' ||
strncmp(utf.ut_line, ut->ut_line, sizeof utf.ut_line) != 0)
continue;
lseek(fd, -(off_t)sizeof utf, SEEK_CUR);
write(fd, ut, sizeof *ut);
found = 1;
}
close(fd);
return (found);
}
static void
ulog_write_wtmp(const struct futmp *ut)
{
int fd;
if ((fd = open(_PATH_WTMP, O_WRONLY|O_APPEND, 0)) < 0)
return;
write(fd, ut, sizeof *ut);
close(fd);
}
static void
ulog_write_lastlog(const struct flastlog *ll, const char *user)
{
struct passwd *pw;
int fd;
if ((fd = open(_PATH_LASTLOG, O_WRONLY, 0)) < 0)
return;
pw = getpwnam(user);
if (pw != NULL) {
lseek(fd, (off_t)(pw->pw_uid * sizeof *ll), SEEK_SET);
write(fd, ll, sizeof *ll);
}
close(fd);
}
struct ulog_utmpx *
ulog_pututxline(const struct ulog_utmpx *utmpx)
{
static struct ulog_utmpx utx;
struct futmp ut;
struct flastlog ll;
char user[sizeof utmpx->ut_user + 1];
switch (utmpx->ut_type) {
case BOOT_TIME:
case OLD_TIME:
case NEW_TIME:
case SHUTDOWN_TIME:
ulog_utmpx_to_futmp(utmpx, &ut);
/* Only log to wtmp. */
ulog_write_wtmp(&ut);
break;
case USER_PROCESS:
ulog_utmpx_to_futmp(utmpx, &ut);
ulog_utmpx_to_flastlog(utmpx, &ll);
/* Log to utmp, wtmp and lastlog. */
ulog_write_utmp_fast(&ut);
ulog_write_wtmp(&ut);
strlcpy(user, utmpx->ut_user, sizeof user);
ulog_write_lastlog(&ll, user);
break;
case DEAD_PROCESS:
ulog_utmpx_to_futmp(utmpx, &ut);
/* Only log to wtmp if logged in utmp. */
if (ulog_write_utmp_slow(&ut))
ulog_write_wtmp(&ut);
break;
default:
return (NULL);
}
/* XXX: Can't we just return utmpx itself? */
memcpy(&utx, utmpx, sizeof utx);
utx.ut_user[sizeof utx.ut_user - 1] = '\0';
utx.ut_line[sizeof utx.ut_line - 1] = '\0';
utx.ut_host[sizeof utx.ut_host - 1] = '\0';
return (&utx);
}

View file

@ -1,94 +0,0 @@
.\" Copyright (c) 2009 Ed Schouten <ed@FreeBSD.org>
.\" 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.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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.
.\"
.\" $FreeBSD$
.\"
.Dd December 5, 2009
.Os
.Dt ULOG_SETUTXFILE 3
.Sh NAME
.Nm ulog_setutxfile ,
.Nm ulog_getutxuser
.Nd additional user login records management
.Sh LIBRARY
.Lb libulog
.Sh SYNOPSIS
.In ulog.h
.Ft int
.Fn ulog_setutxfile "int index" "const char *file"
.Ft struct ulog_utmpx *
.Fn ulog_getutxuser "const char *user"
.Sh DESCRIPTION
The
.Fn ulog_setutxfile
and
.Fn ulog_getutxuser
functions are extensions to the standard
.In ulog.h
interface.
.Pp
The
.Fn ulog_setutxfile
function is similar to
.Fn ulog_setutxent ,
but is capable of returning an error code and also gives access to other
login record files by using one of the following values for
.Fa index :
.Bl -tag -width UTXI_TIME
.It Dv UTXI_TTY
Open the default
.Nm utmp
file, which is indexed by TTY device.
.It Dv UTXI_TIME
Open the
.Nm wtmp
file, which is indexed by time.
.It Dv UTXI_USER
Open the
.Nm lastlog
file, which is indexed by user ID.
.El
.Pp
The
.Fa file
argument determines the file to be opened.
If left null, an implementation-defined default file is opened.
.Pp
The
.Fn ulog_getutxuser
searches the currently opened file until an entry is found whose
.Fa ut_user
is equal to the
.Fa user
argument.
.Sh RETURN VALUES
If succesful,
.Fn ulog_setutxfile
returns 0.
It returns -1 on failure.
.Sh SEE ALSO
.Xr ulog_getutxent 3
.Sh HISTORY
These functions appeared in
.Fx 9.0 .

View file

@ -1,49 +0,0 @@
/*-
* Copyright (c) 2009 Ed Schouten <ed@FreeBSD.org>
* 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.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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.
*/
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include <string.h>
#include <ttyent.h>
#include "ulog_internal.h"
unsigned int
ulog_ttyslot(const char *name)
{
struct ttyent *ty;
unsigned int slot;
setttyent();
for (slot = 1; (ty = getttyent()) != NULL; ++slot)
if (strcmp(ty->ty_name, name) == 0) {
endttyent();
return (slot);
}
endttyent();
return (0);
}

View file

@ -27,7 +27,7 @@
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include "ulog_internal.h"
#include "ulog.h"
#include "utempter.h"
static int last_fd = -1;

View file

@ -98,9 +98,8 @@ and
.Fn utempter_remove_record
always return a value of 0.
.Sh SEE ALSO
.Xr ulog_login_pseudo 3 ,
.Xr ulog_setutxfile 3 ,
.Xr utmp 5
.Xr pututxline 3 ,
.Xr ulog_login_pseudo 3
.Sh HISTORY
These functions appeared in
.Fx 9.0 .