Add support for utmps as found in HP-UX.

This commit is contained in:
Todd C. Miller
2018-11-18 07:45:43 -07:00
parent 716aa6e4ab
commit ecd9688818
5 changed files with 319 additions and 152 deletions

View File

@@ -34,11 +34,13 @@
#endif /* HAVE_STRINGS_H */
#include <unistd.h>
#include <time.h>
#ifdef HAVE_UTMPX_H
#if defined(HAVE_UTMPS_H)
# include <utmps.h>
#elif defined(HAVE_UTMPX_H)
# include <utmpx.h>
#else
# include <utmp.h>
#endif /* HAVE_UTMPX_H */
#endif /* HAVE_UTMPS_H */
#ifdef HAVE_GETTTYENT
# include <ttyent.h>
#endif
@@ -49,34 +51,46 @@
#include "sudo_exec.h"
/*
* Simplify handling of utmp vs. utmpx
* Simplify handling of different utmp types.
*/
#if !defined(HAVE_GETUTXID) && defined(HAVE_GETUTID)
# define getutxline(u) getutline(u)
# define pututxline(u) pututline(u)
# define setutxent() setutent()
# define endutxent() endutent()
#endif /* !HAVE_GETUTXID && HAVE_GETUTID */
#if defined(HAVE_GETUTSID)
# define sudo_getutline(u) GETUTSLINE(u)
# define sudo_pututline(u) PUTUTSLINE(u)
# define sudo_setutent() SETUTSENT()
# define sudo_endutent() ENDUTSENT()
#elif defined(HAVE_GETUTXID)
# define sudo_getutline(u) getutxline(u)
# define sudo_pututline(u) pututxline(u)
# define sudo_setutent() setutxent()
# define sudo_endutent() endutxent()
#elif defined(HAVE_GETUTID)
# define sudo_getutline(u) getutline(u)
# define sudo_pututline(u) pututline(u)
# define sudo_setutent() setutent()
# define sudo_endutent() endutent()
#endif
#ifdef HAVE_GETUTXID
#if defined(HAVE_GETUTSID)
typedef struct utmps sudo_utmp_t;
#elif defined(HAVE_GETUTXID)
typedef struct utmpx sudo_utmp_t;
#else
typedef struct utmp sudo_utmp_t;
/* Older systems have ut_name, not us_user */
/* Older systems have ut_name, not ut_user */
# if !defined(HAVE_STRUCT_UTMP_UT_USER) && !defined(ut_user)
# define ut_user ut_name
# endif
#endif
/* HP-UX has __e_termination and __e_exit, others lack the __ */
#if defined(HAVE_STRUCT_UTMPX_UT_EXIT_E_TERMINATION) || defined(HAVE_STRUCT_UTMP_UT_EXIT_E_TERMINATION)
/* HP-UX has __e_termination and __e_exit, others may lack the __ */
#if defined(HAVE_STRUCT_UTMP_UT_EXIT_E_TERMINATION)
# undef __e_termination
# define __e_termination e_termination
# undef __e_exit
# define __e_exit e_exit
#endif
#if defined(HAVE_GETUTXID) || defined(HAVE_GETUTID)
#if defined(HAVE_GETUTSID) || defined(HAVE_GETUTXID) || defined(HAVE_GETUTID)
/*
* Create ut_id from the new ut_line and the old ut_id.
*/
@@ -108,7 +122,7 @@ utmp_setid(sudo_utmp_t *old, sudo_utmp_t *new)
debug_return;
}
#endif /* HAVE_GETUTXID || HAVE_GETUTID */
#endif /* HAVE_GETUTSID || HAVE_GETUTXID || HAVE_GETUTID */
/*
* Store time in utmp structure.
@@ -120,7 +134,7 @@ utmp_settime(sudo_utmp_t *ut)
debug_decl(utmp_settime, SUDO_DEBUG_UTMP)
if (gettimeofday(&tv, NULL) == 0) {
#if defined(HAVE_STRUCT_UTMP_UT_TV) || defined(HAVE_STRUCT_UTMPX_UT_TV)
#if defined(HAVE_STRUCT_UTMP_UT_TV)
ut->ut_tv.tv_sec = tv.tv_sec;
ut->ut_tv.tv_usec = tv.tv_usec;
#else
@@ -152,14 +166,14 @@ utmp_fill(const char *line, const char *user, sudo_utmp_t *ut_old,
if (user != NULL)
strncpy(ut_new->ut_user, user, sizeof(ut_new->ut_user));
strncpy(ut_new->ut_line, line, sizeof(ut_new->ut_line));
#if defined(HAVE_STRUCT_UTMPX_UT_ID) || defined(HAVE_STRUCT_UTMP_UT_ID)
#if defined(HAVE_STRUCT_UTMP_UT_ID)
utmp_setid(ut_old, ut_new);
#endif
#if defined(HAVE_STRUCT_UTMPX_UT_PID) || defined(HAVE_STRUCT_UTMP_UT_PID)
#if defined(HAVE_STRUCT_UTMP_UT_PID)
ut_new->ut_pid = getpid();
#endif
utmp_settime(ut_new);
#if defined(HAVE_STRUCT_UTMPX_UT_TYPE) || defined(HAVE_STRUCT_UTMP_UT_TYPE)
#if defined(HAVE_STRUCT_UTMP_UT_TYPE)
ut_new->ut_type = USER_PROCESS;
#endif
debug_return;
@@ -169,11 +183,11 @@ utmp_fill(const char *line, const char *user, sudo_utmp_t *ut_old,
* There are two basic utmp file types:
*
* POSIX: sequential access with new entries appended to the end.
* Manipulated via {get,put}utent()/{get,put}getutxent().
* Manipulated via {get,put}[sx]?utent()
*
* Legacy: sparse file indexed by ttyslot() * sizeof(struct utmp)
*/
#if defined(HAVE_GETUTXID) || defined(HAVE_GETUTID)
#if defined(HAVE_GETUTSID) || defined(HAVE_GETUTXID) || defined(HAVE_GETUTID)
bool
utmp_login(const char *from_line, const char *to_line, int ttyfd,
const char *user)
@@ -185,7 +199,7 @@ utmp_login(const char *from_line, const char *to_line, int ttyfd,
/* Strip off /dev/ prefix from line as needed. */
if (strncmp(to_line, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
to_line += sizeof(_PATH_DEV) - 1;
setutxent();
sudo_setutent();
if (from_line != NULL) {
if (strncmp(from_line, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
from_line += sizeof(_PATH_DEV) - 1;
@@ -193,12 +207,12 @@ utmp_login(const char *from_line, const char *to_line, int ttyfd,
/* Lookup old line. */
memset(&utbuf, 0, sizeof(utbuf));
strncpy(utbuf.ut_line, from_line, sizeof(utbuf.ut_line));
ut_old = getutxline(&utbuf);
ut_old = sudo_getutline(&utbuf);
}
utmp_fill(to_line, user, ut_old, &utbuf);
if (pututxline(&utbuf) != NULL)
if (sudo_pututline(&utbuf) != NULL)
ret = true;
endutxent();
sudo_endutent();
debug_return_bool(ret);
}
@@ -216,23 +230,23 @@ utmp_logout(const char *line, int status)
memset(&utbuf, 0, sizeof(utbuf));
strncpy(utbuf.ut_line, line, sizeof(utbuf.ut_line));
if ((ut = getutxline(&utbuf)) != NULL) {
if ((ut = sudo_getutline(&utbuf)) != NULL) {
memset(ut->ut_user, 0, sizeof(ut->ut_user));
# if defined(HAVE_STRUCT_UTMPX_UT_TYPE) || defined(HAVE_STRUCT_UTMP_UT_TYPE)
# if defined(HAVE_STRUCT_UTMP_UT_TYPE)
ut->ut_type = DEAD_PROCESS;
# endif
# if defined(HAVE_STRUCT_UTMPX_UT_EXIT) || defined(HAVE_STRUCT_UTMP_UT_EXIT)
# if defined(HAVE_STRUCT_UTMP_UT_EXIT)
ut->ut_exit.__e_termination = WIFSIGNALED(status) ? WTERMSIG(status) : 0;
ut->ut_exit.__e_exit = WIFEXITED(status) ? WEXITSTATUS(status) : 0;
# endif
utmp_settime(ut);
if (pututxline(ut) != NULL)
if (sudo_pututline(ut) != NULL)
ret = true;
}
debug_return_bool(ret);
}
#else /* !HAVE_GETUTXID && !HAVE_GETUTID */
#else /* !HAVE_GETUTSID && !HAVE_GETUTXID && !HAVE_GETUTID */
/*
* Find the slot for the specified line (tty name and file descriptor).
@@ -320,11 +334,11 @@ utmp_login(const char *from_line, const char *to_line, int ttyfd,
}
}
utmp_fill(to_line, user, ut_old, &utbuf);
#ifdef HAVE_FSEEKO
# ifdef HAVE_FSEEKO
if (fseeko(fp, slot * (off_t)sizeof(utbuf), SEEK_SET) == 0) {
#else
# else
if (fseek(fp, slot * (long)sizeof(utbuf), SEEK_SET) == 0) {
#endif
# endif
if (fwrite(&utbuf, sizeof(utbuf), 1, fp) == 1)
ret = true;
}
@@ -357,11 +371,11 @@ utmp_logout(const char *line, int status)
# endif
utmp_settime(&utbuf);
/* Back up and overwrite record. */
#ifdef HAVE_FSEEKO
# ifdef HAVE_FSEEKO
if (fseeko(fp, (off_t)0 - (off_t)sizeof(utbuf), SEEK_CUR) == 0) {
#else
# else
if (fseek(fp, 0L - (long)sizeof(utbuf), SEEK_CUR) == 0) {
#endif
# endif
if (fwrite(&utbuf, sizeof(utbuf), 1, fp) == 1)
ret = true;
}
@@ -372,4 +386,4 @@ utmp_logout(const char *line, int status)
debug_return_bool(ret);
}
#endif /* HAVE_GETUTXID || HAVE_GETUTID */
#endif /* HAVE_GETUTSID || HAVE_GETUTXID || HAVE_GETUTID */