#include "config.h"
#include <sys/types.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/file.h>
#include <stdio.h>
#ifdef STDC_HEADERS
# include <stdlib.h>
# include <stddef.h>
#else
# ifdef HAVE_STDLIB_H
# include <stdlib.h>
# endif
#endif
#ifdef HAVE_STRING_H
# include <string.h>
#else
# ifdef HAVE_STRINGS_H
# include <strings.h>
# endif
#endif
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <time.h>
#include <pwd.h>
#include <grp.h>
#include "sudo.h"
#ifndef lint
static const char rcsid[] = "$Sudo: check.c,v 1.203 2002/04/25 15:30:12 millert Exp $";
#endif
#define TS_CURRENT 0
#define TS_OLD 1
#define TS_MISSING 2
#define TS_NOFILE 3
#define TS_ERROR 4
static void build_timestamp __P((char **, char **));
static int timestamp_status __P((char *, char *, char *, int));
static char *expand_prompt __P((char *, char *, char *));
static void lecture __P((void));
static void update_timestamp __P((char *, char *));
void
check_user()
{
char *timestampdir = NULL;
char *timestampfile = NULL;
char *prompt;
int status;
if (user_uid == 0 || user_is_exempt())
return;
build_timestamp(×tampdir, ×tampfile);
status = timestamp_status(timestampdir, timestampfile, user_name, TRUE);
if (status != TS_CURRENT) {
if (status == TS_MISSING || status == TS_ERROR)
lecture();
prompt = expand_prompt(user_prompt ? user_prompt : def_str(I_PASSPROMPT),
user_name, user_shost);
verify_user(auth_pw, prompt);
}
if (status != TS_ERROR)
update_timestamp(timestampdir, timestampfile);
free(timestampdir);
if (timestampfile)
free(timestampfile);
}
static void
lecture()
{
if (def_flag(I_LECTURE)) {
(void) fputs("\n\
We trust you have received the usual lecture from the local System\n\
Administrator. It usually boils down to these two things:\n\
\n\
#1) Respect the privacy of others.\n\
#2) Think before you type.\n\n",
stderr);
}
}
static void
update_timestamp(timestampdir, timestampfile)
char *timestampdir;
char *timestampfile;
{
if (touch(timestampfile ? timestampfile : timestampdir, time(NULL)) == -1) {
if (timestampfile) {
int fd = open(timestampfile, O_WRONLY|O_CREAT|O_TRUNC, 0600);
if (fd == -1)
log_error(NO_EXIT|USE_ERRNO, "Can't open %s", timestampfile);
else
close(fd);
} else {
if (mkdir(timestampdir, 0700) == -1)
log_error(NO_EXIT|USE_ERRNO, "Can't mkdir %s", timestampdir);
}
}
}
static char *
expand_prompt(old_prompt, user, host)
char *old_prompt;
char *user;
char *host;
{
size_t len;
int subst;
char *p, *np, *new_prompt, lastchar;
subst = 0;
for (p = old_prompt, len = strlen(old_prompt), lastchar = '\0'; *p; p++) {
if (lastchar == '%') {
if (*p == 'h') {
len += strlen(user_shost) - 2;
subst = 1;
} else if (*p == 'u') {
len += strlen(user_name) - 2;
subst = 1;
}
}
if (lastchar == '%' && *p == '%') {
lastchar = '\0';
len--;
} else
lastchar = *p;
}
if (subst) {
new_prompt = (char *) emalloc(len + 1);
for (p = old_prompt, np = new_prompt, lastchar = '\0'; *p; p++) {
if (lastchar == '%' && (*p == 'h' || *p == 'u' || *p == '%')) {
if (*p == 'h') {
np--;
strcpy(np, user_shost);
np += strlen(user_shost);
} else if (*p == 'u') {
np--;
strcpy(np, user_name);
np += strlen(user_name);
}
} else
*np++ = *p;
if (lastchar == '%' && *p == '%')
lastchar = '\0';
else
lastchar = *p;
}
*np = '\0';
} else
new_prompt = old_prompt;
return(new_prompt);
}
int
user_is_exempt()
{
struct group *grp;
char **gr_mem;
if (!def_str(I_EXEMPT_GROUP))
return(FALSE);
if (!(grp = getgrnam(def_str(I_EXEMPT_GROUP))))
return(FALSE);
if (user_gid == grp->gr_gid)
return(TRUE);
for (gr_mem = grp->gr_mem; *gr_mem; gr_mem++) {
if (strcmp(user_name, *gr_mem) == 0)
return(TRUE);
}
return(FALSE);
}
static void
build_timestamp(timestampdir, timestampfile)
char **timestampdir;
char **timestampfile;
{
char *dirparent;
int len;
dirparent = def_str(I_TIMESTAMPDIR);
len = easprintf(timestampdir, "%s/%s", dirparent, user_name);
if (len >= MAXPATHLEN)
log_error(0, "timestamp path too long: %s", timestampdir);
if (def_flag(I_TTY_TICKETS)) {
char *p;
if ((p = strrchr(user_tty, '/')))
p++;
else
p = user_tty;
if (def_flag(I_TARGETPW))
len = easprintf(timestampfile, "%s/%s/%s:%s", dirparent, user_name,
p, *user_runas);
else
len = easprintf(timestampfile, "%s/%s/%s", dirparent, user_name, p);
if (len >= MAXPATHLEN)
log_error(0, "timestamp path too long: %s", timestampfile);
} else if (def_flag(I_TARGETPW)) {
len = easprintf(timestampfile, "%s/%s/%s", dirparent, user_name,
*user_runas);
if (len >= MAXPATHLEN)
log_error(0, "timestamp path too long: %s", timestampfile);
} else
*timestampfile = NULL;
}
static int
timestamp_status(timestampdir, timestampfile, user, make_dirs)
char *timestampdir;
char *timestampfile;
char *user;
int make_dirs;
{
struct stat sb;
time_t now;
char *dirparent = def_str(I_TIMESTAMPDIR);
int status = TS_ERROR;
if (lstat(dirparent, &sb) == 0) {
if (!S_ISDIR(sb.st_mode))
log_error(NO_EXIT, "%s exists but is not a directory (0%o)",
dirparent, sb.st_mode);
else if (sb.st_uid != 0)
log_error(NO_EXIT, "%s owned by uid %ld, should be owned by root",
dirparent, (long) sb.st_uid);
else if ((sb.st_mode & 0000022))
log_error(NO_EXIT,
"%s writable by non-owner (0%o), should be mode 0700",
dirparent, sb.st_mode);
else {
if ((sb.st_mode & 0000777) != 0700)
(void) chmod(dirparent, 0700);
status = TS_MISSING;
}
} else if (errno != ENOENT) {
log_error(NO_EXIT|USE_ERRNO, "can't stat %s", dirparent);
} else {
if (make_dirs) {
if (mkdir(dirparent, S_IRWXU))
log_error(NO_EXIT|USE_ERRNO, "can't mkdir %s",
dirparent);
else
status = TS_MISSING;
}
}
if (status == TS_ERROR)
return(status);
status = TS_ERROR;
if (lstat(timestampdir, &sb) == 0) {
if (!S_ISDIR(sb.st_mode)) {
if (S_ISREG(sb.st_mode)) {
if (unlink(timestampdir) == 0)
status = TS_MISSING;
} else
log_error(NO_EXIT, "%s exists but is not a directory (0%o)",
timestampdir, sb.st_mode);
} else if (sb.st_uid != 0)
log_error(NO_EXIT, "%s owned by uid %ld, should be owned by root",
timestampdir, (long) sb.st_uid);
else if ((sb.st_mode & 0000022))
log_error(NO_EXIT,
"%s writable by non-owner (0%o), should be mode 0700",
timestampdir, sb.st_mode);
else {
if ((sb.st_mode & 0000777) != 0700)
(void) chmod(timestampdir, 0700);
status = TS_OLD;
}
} else if (errno != ENOENT) {
log_error(NO_EXIT|USE_ERRNO, "can't stat %s", timestampdir);
} else
status = TS_MISSING;
if (status == TS_MISSING && timestampfile && make_dirs) {
if (mkdir(timestampdir, S_IRWXU) == -1) {
status = TS_ERROR;
log_error(NO_EXIT|USE_ERRNO, "can't mkdir %s", timestampdir);
}
}
if (timestampfile && status != TS_ERROR) {
if (status != TS_MISSING)
status = TS_NOFILE;
if (lstat(timestampfile, &sb) == 0) {
if (!S_ISREG(sb.st_mode)) {
status = TS_ERROR;
log_error(NO_EXIT, "%s exists but is not a regular file (0%o)",
timestampfile, sb.st_mode);
} else {
if (sb.st_uid != 0) {
log_error(NO_EXIT,
"%s owned by uid %ld, should be owned by root",
timestampfile, (long) sb.st_uid);
(void) unlink(timestampfile);
} else if ((sb.st_mode & 0000022)) {
log_error(NO_EXIT,
"%s writable by non-owner (0%o), should be mode 0600",
timestampfile, sb.st_mode);
(void) unlink(timestampfile);
} else {
if ((sb.st_mode & 0000777) != 0600)
(void) chmod(timestampfile, 0600);
status = TS_OLD;
}
}
} else if (errno != ENOENT) {
log_error(NO_EXIT|USE_ERRNO, "can't stat %s", timestampfile);
status = TS_ERROR;
}
}
if (status == TS_OLD) {
if (def_ival(I_TIMESTAMP_TIMEOUT) < 0 && sb.st_mtime != 0)
status = TS_CURRENT;
else {
now = time(NULL);
if (def_ival(I_TIMESTAMP_TIMEOUT) &&
now - sb.st_mtime < 60 * def_ival(I_TIMESTAMP_TIMEOUT)) {
if (sb.st_mtime > now + 60 * def_ival(I_TIMESTAMP_TIMEOUT) * 2) {
log_error(NO_EXIT,
"timestamp too far in the future: %20.20s",
4 + ctime(&sb.st_mtime));
if (timestampfile)
(void) unlink(timestampfile);
else
(void) rmdir(timestampdir);
status = TS_MISSING;
} else
status = TS_CURRENT;
}
}
}
return(status);
}
void
remove_timestamp(remove)
int remove;
{
char *timestampdir;
char *timestampfile;
char *ts;
int status;
build_timestamp(×tampdir, ×tampfile);
status = timestamp_status(timestampdir, timestampfile, user_name, FALSE);
if (status == TS_OLD || status == TS_CURRENT) {
ts = timestampfile ? timestampfile : timestampdir;
if (remove) {
if (timestampfile)
status = unlink(timestampfile);
else
status = rmdir(timestampdir);
if (status == -1 && errno != ENOENT) {
log_error(NO_EXIT, "can't remove %s (%s), will reset to epoch",
ts, strerror(errno));
remove = FALSE;
}
}
if (!remove && touch(ts, 0) == -1) {
(void) fprintf(stderr, "%s: can't reset %s to epoch: %s\n",
Argv[0], ts, strerror(errno));
}
}
free(timestampdir);
if (timestampfile)
free(timestampfile);
}