Skip to content
This repository has been archived by the owner on Feb 28, 2024. It is now read-only.

Commit

Permalink
Merge pull request #107 from pprindeville/fix-logwtmp
Browse files Browse the repository at this point in the history
Retire logwtmp() in favor of POSIX pututxline()
  • Loading branch information
pprindeville authored Jan 5, 2018
2 parents 4c0616f + 3337b95 commit 81e942c
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 8 deletions.
4 changes: 3 additions & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ AC_CHECK_LIB(crypto, RAND_pseudo_bytes,
[AC_DEFINE([HAVE_RAND_PSEUDO_BYTES], [1], [Define to 1 if you have the `RAND_pseudo_bytes' function.])])
AC_CHECK_LIB(crypto, RAND_bytes,
[AC_DEFINE([HAVE_RAND_BYTES], [1], [Define to 1 if you have the `RAND_bytes' function.])])
AC_CHECK_LIB(util, logwtmp)
AC_CHECK_LIB(c, pututxline,
[AC_DEFINE([HAVE_PUTUTXLINE], [1], [Define to 1 if you have the `pututxline' function.])],
[AC_CHECK_LIB(util, logwtmp)])

case "$host" in
sparc-* | sparc64-*)
Expand Down
52 changes: 45 additions & 7 deletions tacc.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#include <string.h>
#include <syslog.h>
#include <errno.h>
#include <utmp.h>
#include <sys/file.h>
#include <sys/types.h>
#include <sys/wait.h>
Expand All @@ -34,6 +33,12 @@
#include "config.h"
#endif

#if defined(HAVE_PUTUTXLINE)
#include <utmpx.h>
#elif defined(HAVE_LOGWTMP)
#include <utmp.h>
#endif

#include "tacplus.h"
#include "libtac.h"

Expand Down Expand Up @@ -78,6 +83,13 @@ typedef unsigned char flag;
flag quiet = 0;
char *user = NULL; /* global, because of signal handler */

#if defined(HAVE_PUTUTXLINE)
struct utmpx utmpx;
#endif

/* take the length of a string constant without the NUL */
#define C_STRLEN(str) (sizeof("" str) - 1)

/* command line options */
static struct option long_options[] =
{
Expand Down Expand Up @@ -176,8 +188,7 @@ int main(int argc, char **argv) {
break;
case 'L':
// tac_login is a global variable initialized in libtac
bzero(tac_login, sizeof(tac_login));
strncpy(tac_login, optarg, sizeof(tac_login) - 1);
xstrcpy(tac_login, optarg, sizeof(tac_login));
break;
case 'p':
pass = optarg;
Expand Down Expand Up @@ -354,10 +365,28 @@ int main(int argc, char **argv) {
}

/* log in local utmp */
#ifdef HAVE_LOGWTMP
if (log_wtmp)
if (log_wtmp) {
#if defined(HAVE_PUTUTXLINE)
struct timeval tv;

gettimeofday(&tv, NULL);

memset(&utmpx, 0, sizeof(utmpx));
utmpx.ut_type = USER_PROCESS;
utmpx.ut_pid = getpid();
xstrcpy(utmpx.ut_line, tty, sizeof(utmpx.ut_line));
strncpy(utmpx.ut_id, tty + C_STRLEN("tty"), sizeof(utmpx.ut_id));
xstrcpy(utmpx.ut_host, "dialup", sizeof(utmpx.ut_host));
utmpx.ut_tv.tv_sec = tv.tv_sec;
utmpx.ut_tv.tv_usec = tv.tv_usec;
xstrcpy(utmpx.ut_user, user, sizeof(utmpx.ut_user));
/* ut_addr unused ... */
setutxent();
pututxline(&utmpx);
#elif defined(HAVE_LOGWTMP)
logwtmp(tty, user, "dialup");
#endif
}

if (command != NULL) {
int ret;
Expand Down Expand Up @@ -436,10 +465,19 @@ int main(int argc, char **argv) {
}

/* logout from utmp */
#ifdef HAVE_LOGWTMP
if (log_wtmp)
if (log_wtmp) {
#if defined(HAVE_PUTUTXLINE)
utmpx.ut_type = DEAD_PROCESS;
memset(utmpx.ut_line, 0, sizeof(utmpx.ut_line));
memset(utmpx.ut_user, 0, sizeof(utmpx.ut_user));
memset(utmpx.ut_host, 0, sizeof(utmpx.ut_host));
utmpx.ut_tv.tv_sec = utmpx.ut_tv.tv_usec = 0;
setutxent();
pututxline(&utmpx);
#elif defined(HAVE_LOGWTMP)
logwtmp(tty, "", "");
#endif
}

exit(EXIT_OK);
}
Expand Down

0 comments on commit 81e942c

Please sign in to comment.