version 1.1, 2012/02/21 16:23:02
|
version 1.1.1.5, 2014/06/15 16:12:55
|
Line 1
|
Line 1
|
/* |
/* |
* Copyright (c) 2011 Todd C. Miller <Todd.Miller@courtesan.com> | * Copyright (c) 2011-2013 Todd C. Miller <Todd.Miller@courtesan.com> |
* |
* |
* Permission to use, copy, modify, and distribute this software for any |
* Permission to use, copy, modify, and distribute this software for any |
* purpose with or without fee is hereby granted, provided that the above |
* purpose with or without fee is hereby granted, provided that the above |
Line 17
|
Line 17
|
#include <config.h> |
#include <config.h> |
|
|
#include <sys/types.h> |
#include <sys/types.h> |
#include <sys/param.h> |
|
#include <sys/time.h> |
#include <sys/time.h> |
#include <sys/wait.h> |
#include <sys/wait.h> |
#include <stdio.h> |
#include <stdio.h> |
Line 41
|
Line 40
|
#ifdef HAVE_UNISTD_H |
#ifdef HAVE_UNISTD_H |
# include <unistd.h> |
# include <unistd.h> |
#endif /* HAVE_UNISTD_H */ |
#endif /* HAVE_UNISTD_H */ |
#if TIME_WITH_SYS_TIME | #ifdef TIME_WITH_SYS_TIME |
# include <time.h> |
# include <time.h> |
#endif |
#endif |
#ifdef HAVE_UTMPX_H |
#ifdef HAVE_UTMPX_H |
Line 53
|
Line 52
|
# include <ttyent.h> |
# include <ttyent.h> |
#endif |
#endif |
#include <fcntl.h> |
#include <fcntl.h> |
|
#include <signal.h> |
|
|
#include "sudo.h" |
#include "sudo.h" |
#include "sudo_exec.h" |
#include "sudo_exec.h" |
Line 63
|
Line 63
|
#if !defined(HAVE_GETUTXID) && defined(HAVE_GETUTID) |
#if !defined(HAVE_GETUTXID) && defined(HAVE_GETUTID) |
# define getutxline(u) getutline(u) |
# define getutxline(u) getutline(u) |
# define pututxline(u) pututline(u) |
# define pututxline(u) pututline(u) |
# define setutxent setutent(u) | # define setutxent() setutent() |
# define endutxent endutent(u) | # define endutxent() endutent() |
#endif /* !HAVE_GETUTXID && HAVE_GETUTID */ |
#endif /* !HAVE_GETUTXID && HAVE_GETUTID */ |
|
|
#ifdef HAVE_GETUTXID |
#ifdef HAVE_GETUTXID |
Line 94 utmp_setid(sudo_utmp_t *old, sudo_utmp_t *new)
|
Line 94 utmp_setid(sudo_utmp_t *old, sudo_utmp_t *new)
|
{ |
{ |
const char *line = new->ut_line; |
const char *line = new->ut_line; |
size_t idlen; |
size_t idlen; |
|
debug_decl(utmp_setid, SUDO_DEBUG_UTMP) |
|
|
/* Skip over "tty" in the id if old entry did too. */ |
/* Skip over "tty" in the id if old entry did too. */ |
if (old != NULL) { |
if (old != NULL) { |
Line 111 utmp_setid(sudo_utmp_t *old, sudo_utmp_t *new)
|
Line 112 utmp_setid(sudo_utmp_t *old, sudo_utmp_t *new)
|
idlen = sizeof(new->ut_id); |
idlen = sizeof(new->ut_id); |
} |
} |
strncpy(new->ut_id, line, idlen); |
strncpy(new->ut_id, line, idlen); |
|
|
|
debug_return; |
} |
} |
#endif /* HAVE_GETUTXID || HAVE_GETUTID */ |
#endif /* HAVE_GETUTXID || HAVE_GETUTID */ |
|
|
Line 121 static void
|
Line 124 static void
|
utmp_settime(sudo_utmp_t *ut) |
utmp_settime(sudo_utmp_t *ut) |
{ |
{ |
struct timeval tv; |
struct timeval tv; |
|
debug_decl(utmp_settime, SUDO_DEBUG_UTMP) |
|
|
gettimeofday(&tv, NULL); |
gettimeofday(&tv, NULL); |
|
|
Line 130 utmp_settime(sudo_utmp_t *ut)
|
Line 134 utmp_settime(sudo_utmp_t *ut)
|
#else |
#else |
ut->ut_time = tv.tv_sec; |
ut->ut_time = tv.tv_sec; |
#endif |
#endif |
|
|
|
debug_return; |
} |
} |
|
|
/* |
/* |
Line 139 static void
|
Line 145 static void
|
utmp_fill(const char *line, const char *user, sudo_utmp_t *ut_old, |
utmp_fill(const char *line, const char *user, sudo_utmp_t *ut_old, |
sudo_utmp_t *ut_new) |
sudo_utmp_t *ut_new) |
{ |
{ |
|
debug_decl(utmp_file, SUDO_DEBUG_UTMP) |
|
|
if (ut_old == NULL) { |
if (ut_old == NULL) { |
memset(ut_new, 0, sizeof(*ut_new)); |
memset(ut_new, 0, sizeof(*ut_new)); |
if (user == NULL) { |
if (user == NULL) { |
Line 161 utmp_fill(const char *line, const char *user, sudo_utm
|
Line 169 utmp_fill(const char *line, const char *user, sudo_utm
|
#if defined(HAVE_STRUCT_UTMPX_UT_TYPE) || defined(HAVE_STRUCT_UTMP_UT_TYPE) |
#if defined(HAVE_STRUCT_UTMPX_UT_TYPE) || defined(HAVE_STRUCT_UTMP_UT_TYPE) |
ut_new->ut_type = USER_PROCESS; |
ut_new->ut_type = USER_PROCESS; |
#endif |
#endif |
|
debug_return; |
} |
} |
|
|
/* |
/* |
Line 172 utmp_fill(const char *line, const char *user, sudo_utm
|
Line 181 utmp_fill(const char *line, const char *user, sudo_utm
|
* Legacy: sparse file indexed by ttyslot() * sizeof(struct utmp) |
* Legacy: sparse file indexed by ttyslot() * sizeof(struct utmp) |
*/ |
*/ |
#if defined(HAVE_GETUTXID) || defined(HAVE_GETUTID) |
#if defined(HAVE_GETUTXID) || defined(HAVE_GETUTID) |
int | bool |
utmp_login(const char *from_line, const char *to_line, int ttyfd, |
utmp_login(const char *from_line, const char *to_line, int ttyfd, |
const char *user) |
const char *user) |
{ |
{ |
sudo_utmp_t utbuf, *ut_old = NULL; |
sudo_utmp_t utbuf, *ut_old = NULL; |
int rval = FALSE; | bool rval = false; |
| debug_decl(utmp_login, SUDO_DEBUG_UTMP) |
|
|
/* Strip off /dev/ prefix from line as needed. */ |
/* Strip off /dev/ prefix from line as needed. */ |
if (strncmp(to_line, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0) |
if (strncmp(to_line, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0) |
Line 194 utmp_login(const char *from_line, const char *to_line,
|
Line 204 utmp_login(const char *from_line, const char *to_line,
|
} |
} |
utmp_fill(to_line, user, ut_old, &utbuf); |
utmp_fill(to_line, user, ut_old, &utbuf); |
if (pututxline(&utbuf) != NULL) |
if (pututxline(&utbuf) != NULL) |
rval = TRUE; | rval = true; |
endutxent(); |
endutxent(); |
|
|
return rval; | debug_return_bool(rval); |
} |
} |
|
|
int | bool |
utmp_logout(const char *line, int status) |
utmp_logout(const char *line, int status) |
{ |
{ |
int rval = FALSE; | bool rval = false; |
sudo_utmp_t *ut, utbuf; |
sudo_utmp_t *ut, utbuf; |
|
debug_decl(utmp_logout, SUDO_DEBUG_UTMP) |
|
|
/* Strip off /dev/ prefix from line as needed. */ |
/* Strip off /dev/ prefix from line as needed. */ |
if (strncmp(line, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0) |
if (strncmp(line, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0) |
Line 223 utmp_logout(const char *line, int status)
|
Line 234 utmp_logout(const char *line, int status)
|
# endif |
# endif |
utmp_settime(ut); |
utmp_settime(ut); |
if (pututxline(ut) != NULL) |
if (pututxline(ut) != NULL) |
rval = TRUE; | rval = true; |
} |
} |
return rval; | debug_return_bool(rval); |
} |
} |
|
|
#else /* !HAVE_GETUTXID && !HAVE_GETUTID */ |
#else /* !HAVE_GETUTXID && !HAVE_GETUTID */ |
Line 241 utmp_slot(const char *line, int ttyfd)
|
Line 252 utmp_slot(const char *line, int ttyfd)
|
{ |
{ |
int slot = 1; |
int slot = 1; |
struct ttyent *tty; |
struct ttyent *tty; |
|
debug_decl(utmp_slot, SUDO_DEBUG_UTMP) |
|
|
setttyent(); |
setttyent(); |
while ((tty = getttyent()) != NULL) { |
while ((tty = getttyent()) != NULL) { |
Line 249 utmp_slot(const char *line, int ttyfd)
|
Line 261 utmp_slot(const char *line, int ttyfd)
|
slot++; |
slot++; |
} |
} |
endttyent(); |
endttyent(); |
return tty ? slot : 0; | debug_return_int(tty ? slot : 0); |
} |
} |
# else |
# else |
static int |
static int |
utmp_slot(const char *line, int ttyfd) |
utmp_slot(const char *line, int ttyfd) |
{ |
{ |
int sfd, slot; |
int sfd, slot; |
|
debug_decl(utmp_slot, SUDO_DEBUG_UTMP) |
|
|
/* |
/* |
* Temporarily point stdin to the tty since ttyslot() |
* Temporarily point stdin to the tty since ttyslot() |
* doesn't take an argument. |
* doesn't take an argument. |
*/ |
*/ |
if ((sfd = dup(STDIN_FILENO)) == -1) |
if ((sfd = dup(STDIN_FILENO)) == -1) |
error(1, _("unable to save stdin")); | fatal(U_("unable to save stdin")); |
if (dup2(ttyfd, STDIN_FILENO) == -1) |
if (dup2(ttyfd, STDIN_FILENO) == -1) |
error(1, _("unable to dup2 stdin")); | fatal(U_("unable to dup2 stdin")); |
slot = ttyslot(); |
slot = ttyslot(); |
if (dup2(sfd, STDIN_FILENO) == -1) |
if (dup2(sfd, STDIN_FILENO) == -1) |
error(1, _("unable to restore stdin")); | fatal(U_("unable to restore stdin")); |
close(sfd); |
close(sfd); |
|
|
return slot; | debug_return_int(slot); |
} |
} |
# endif /* HAVE_GETTTYENT */ |
# endif /* HAVE_GETTTYENT */ |
|
|
int | bool |
utmp_login(const char *from_line, const char *to_line, int ttyfd, |
utmp_login(const char *from_line, const char *to_line, int ttyfd, |
const char *user) |
const char *user) |
{ |
{ |
sudo_utmp_t utbuf, *ut_old = NULL; |
sudo_utmp_t utbuf, *ut_old = NULL; |
int slot, rval = FALSE; | bool rval = false; |
| int slot; |
FILE *fp; |
FILE *fp; |
|
debug_decl(utmp_login, SUDO_DEBUG_UTMP) |
|
|
/* Strip off /dev/ prefix from line as needed. */ |
/* Strip off /dev/ prefix from line as needed. */ |
if (strncmp(to_line, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0) |
if (strncmp(to_line, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0) |
Line 312 utmp_login(const char *from_line, const char *to_line,
|
Line 327 utmp_login(const char *from_line, const char *to_line,
|
} |
} |
} |
} |
utmp_fill(to_line, user, ut_old, &utbuf); |
utmp_fill(to_line, user, ut_old, &utbuf); |
|
#ifdef HAVE_FSEEKO |
|
if (fseeko(fp, slot * (off_t)sizeof(utbuf), SEEK_SET) == 0) { |
|
#else |
if (fseek(fp, slot * (long)sizeof(utbuf), SEEK_SET) == 0) { |
if (fseek(fp, slot * (long)sizeof(utbuf), SEEK_SET) == 0) { |
|
#endif |
if (fwrite(&utbuf, sizeof(utbuf), 1, fp) == 1) |
if (fwrite(&utbuf, sizeof(utbuf), 1, fp) == 1) |
rval = TRUE; | rval = true; |
} |
} |
fclose(fp); |
fclose(fp); |
|
|
done: |
done: |
return rval; | debug_return_bool(rval); |
} |
} |
|
|
int | bool |
utmp_logout(const char *line, int status) |
utmp_logout(const char *line, int status) |
{ |
{ |
sudo_utmp_t utbuf; |
sudo_utmp_t utbuf; |
int rval = FALSE; | bool rval = false; |
FILE *fp; |
FILE *fp; |
|
debug_decl(utmp_logout, SUDO_DEBUG_UTMP) |
|
|
if ((fp = fopen(_PATH_UTMP, "r+")) == NULL) |
if ((fp = fopen(_PATH_UTMP, "r+")) == NULL) |
return rval; | debug_return_int(rval); |
|
|
/* Strip off /dev/ prefix from line as needed. */ |
/* Strip off /dev/ prefix from line as needed. */ |
if (strncmp(line, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0) |
if (strncmp(line, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0) |
Line 344 utmp_logout(const char *line, int status)
|
Line 364 utmp_logout(const char *line, int status)
|
# endif |
# endif |
utmp_settime(&utbuf); |
utmp_settime(&utbuf); |
/* Back up and overwrite record. */ |
/* Back up and overwrite record. */ |
|
#ifdef HAVE_FSEEKO |
|
if (fseeko(fp, (off_t)0 - (off_t)sizeof(utbuf), SEEK_CUR) == 0) { |
|
#else |
if (fseek(fp, 0L - (long)sizeof(utbuf), SEEK_CUR) == 0) { |
if (fseek(fp, 0L - (long)sizeof(utbuf), SEEK_CUR) == 0) { |
|
#endif |
if (fwrite(&utbuf, sizeof(utbuf), 1, fp) == 1) |
if (fwrite(&utbuf, sizeof(utbuf), 1, fp) == 1) |
rval = TRUE; | rval = true; |
} |
} |
break; |
break; |
} |
} |
} |
} |
fclose(fp); |
fclose(fp); |
|
|
return rval; | debug_return_bool(rval); |
} |
} |
#endif /* HAVE_GETUTXID || HAVE_GETUTID */ |
#endif /* HAVE_GETUTXID || HAVE_GETUTID */ |