2
0
mirror of https://github.com/sudo-project/sudo.git synced 2025-08-22 18:08:23 +00:00
sudo/check.c

308 lines
7.4 KiB
C
Raw Normal View History

/*
1994-03-12 18:55:39 +00:00
* CU sudo version 1.3.1 (based on Root Group sudo version 1.1)
*
* This software comes with no waranty whatsoever, use at your own risk.
*
* Please send bugs, changes, problems to sudo-bugs.cs.colorado.edu
*
*/
1993-06-11 22:03:45 +00:00
/*
* sudo version 1.1 allows users to execute commands as root
* Copyright (C) 1991 The Root Group, Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 1, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*******************************************************************
*
* check.c
*
* check_user() only returns if the user's timestamp file
* is current or if they enter a correct password.
*
* Jeff Nieusma Thu Mar 21 22:39:07 MST 1991
*/
#ifndef lint
static char rcsid[] = "$Id$";
#endif /* lint */
#include "config.h"
1993-06-11 22:03:45 +00:00
#include <stdio.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif /* HAVE_UNISTD_H */
#ifdef HAVE_STRING_H
1993-06-11 22:03:45 +00:00
#include <string.h>
#endif /* HAVE_STRING_H */
#ifdef HAVE_STRINGS_H
1993-06-11 22:03:45 +00:00
#include <strings.h>
#endif /* HAVE_STRINGS_H */
1993-06-11 22:03:45 +00:00
#include <fcntl.h>
#include <sys/param.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/file.h>
#include <pwd.h>
#include "sudo.h"
1993-11-28 08:00:06 +00:00
#include "insults.h"
#ifdef __svr4__
#include <shadow.h>
#endif /* __svr4__ */
1993-06-11 22:03:45 +00:00
1994-05-29 22:35:53 +00:00
1993-06-11 22:03:45 +00:00
1994-05-28 19:13:27 +00:00
/*
* Prototypes for local functions
*/
static int check_timestamp __P((void));
static void check_passwd __P((void));
static void update_timestamp __P((void));
static void reminder __P((void));
1993-06-11 22:03:45 +00:00
1994-05-28 19:13:27 +00:00
/*
* Globals
*/
static int timedir_is_good;
static char *timestampfile_p;
1993-06-11 22:03:45 +00:00
/********************************************************************
*
* check_user()
*
* This function only returns if the user can successfully
* verify who s/he is.
*/
void check_user()
{
1993-10-04 19:10:33 +00:00
register int rtn;
1994-02-28 01:55:10 +00:00
mode_t oldmask;
1993-06-11 22:03:45 +00:00
1994-02-28 01:55:10 +00:00
oldmask = umask(077); /* make sure the timestamp files are private */
1993-06-11 22:03:45 +00:00
1993-10-04 19:10:33 +00:00
rtn = check_timestamp();
if (rtn && uid) /* if timestamp is not current... */
check_passwd();
1993-06-11 22:03:45 +00:00
1993-10-04 19:10:33 +00:00
update_timestamp();
1994-02-28 01:55:10 +00:00
(void) umask(oldmask); /* want a real umask to exec() the command */
1993-06-11 22:03:45 +00:00
}
/********************************************************************
*
* check_timestamp()
*
* this function checks the timestamp file. If it is within
* TIMEOUT minutes, no password will be required
*/
static int check_timestamp()
{
1993-10-04 19:10:33 +00:00
static char timestampfile[MAXPATHLEN + 1];
register char *p;
struct stat statbuf;
register int timestamp_is_old = -1;
time_t now;
(void) sprintf(timestampfile, "%s/%s", _PATH_SUDO_TIMEDIR, user);
1993-10-04 19:10:33 +00:00
timestampfile_p = timestampfile;
timedir_is_good = 1; /* now there's an assumption for ya... */
1994-02-02 06:30:34 +00:00
/* become root */
be_root();
1993-10-04 19:10:33 +00:00
/*
* walk through the path one directory at a time
*/
for (p = timestampfile + 1; p = strchr(p, '/'); *p++ = '/') {
1993-10-04 19:10:33 +00:00
*p = '\0';
if (stat(timestampfile, &statbuf) < 0) {
if (strcmp(timestampfile, _PATH_SUDO_TIMEDIR))
1993-10-04 19:10:33 +00:00
(void) fprintf(stderr, "Cannot stat() %s\n", timestampfile);
timedir_is_good = 0;
*p = '/';
break;
1993-06-11 22:03:45 +00:00
}
}
1993-10-04 19:10:33 +00:00
/*
* if all the directories are stat()able
*/
if (timedir_is_good) {
if (stat(timestampfile, &statbuf)) { /* does the file exist? */
if (uid)
reminder(); /* if not, do the reminder */
timestamp_is_old = 1; /* and return (1) */
} else { /* otherwise, check the time */
now = time((time_t *) NULL);
if (now - statbuf.st_mtime < 60 * TIMEOUT)
timestamp_is_old = 0; /* if file is recent, return(0) */
else
timestamp_is_old = 1; /* else make 'em enter password */
1993-06-11 22:03:45 +00:00
}
}
1993-10-04 19:10:33 +00:00
/*
* there was a problem stat()ing a directory
*/
1993-06-11 22:03:45 +00:00
else {
1993-10-04 19:10:33 +00:00
timestamp_is_old = 1; /* user has to enter password */
if (mkdir(_PATH_SUDO_TIMEDIR, 0700)) { /* make the TIMEDIR directory */
1993-10-04 19:10:33 +00:00
perror("check_timestamp: mkdir");
timedir_is_good = 0;
} else {
timedir_is_good = 1;/* _PATH_SUDO_TIMEDIR now exists */
1993-10-04 19:10:33 +00:00
reminder();
1993-06-11 22:03:45 +00:00
}
}
1994-02-02 06:30:34 +00:00
/* relinquish root */
be_user();
1993-10-04 19:10:33 +00:00
return (timestamp_is_old);
1993-06-11 22:03:45 +00:00
}
/********************************************************************
*
* update_timestamp()
*
* This function changes the timestamp to now
*/
static void update_timestamp()
{
1993-10-04 19:10:33 +00:00
register int fd;
1993-06-11 22:03:45 +00:00
1994-02-02 06:30:34 +00:00
/* become root */
be_root();
1993-10-04 19:10:33 +00:00
if (timedir_is_good) {
unlink(timestampfile_p);
if ((fd = open(timestampfile_p, O_WRONLY | O_CREAT | O_TRUNC, 0600)) < 0)
perror("update_timestamp: open");
close(fd);
1993-06-11 22:03:45 +00:00
}
1994-02-02 06:30:34 +00:00
/* relinquish root */
be_user();
1993-06-11 22:03:45 +00:00
}
/********************************************************************
*
* check_passwd()
*
* This function grabs the user's password and checks with
* the password in /etc/passwd
*/
static void check_passwd()
{
1993-10-04 19:10:33 +00:00
struct passwd *pw_ent;
#ifdef __svr4__
struct spwd *spw_ent;
#endif /* __svr4__ */
1993-10-04 19:10:33 +00:00
char *encrypted; /* this comes from /etc/passwd */
char *pass; /* this is what gets entered */
register int counter = TRIES_FOR_PASSWORD;
/* some os's need to be root to get at shadow password */
be_root();
pw_ent = getpwuid(uid);
be_user();
if (pw_ent == NULL) {
1993-10-04 19:10:33 +00:00
(void) sprintf(user, "%u", uid);
log_error(GLOBAL_NO_PW_ENT);
inform_user(GLOBAL_NO_PW_ENT);
exit(1);
1993-06-11 22:03:45 +00:00
}
#ifdef __svr4__
1994-02-02 06:30:34 +00:00
be_root();
spw_ent = getspnam(pw_ent->pw_name);
be_user();
if (spw_ent == NULL) {
(void) sprintf(user, "%u", uid);
log_error(GLOBAL_NO_PW_ENT);
inform_user(GLOBAL_NO_PW_ENT);
exit(1);
}
encrypted = spw_ent -> sp_pwdp;
#else
1993-10-04 19:10:33 +00:00
encrypted = pw_ent -> pw_passwd;
#endif /* __svr4__ */
1993-10-04 19:10:33 +00:00
/*
* you get TRIES_FOR_PASSWORD times to guess your password
*/
while (counter > 0) {
1994-06-06 00:02:34 +00:00
pass = tgetpass("Password:", PASSWORD_TIMEOUT);
if (!pass || *pass == '\0')
1993-10-04 19:10:33 +00:00
exit(0);
1994-05-29 22:35:53 +00:00
if (!strcmp(encrypted, (char *) crypt(pass, encrypted)))
1993-10-04 19:10:33 +00:00
return; /* if the passwd is correct return() */
--counter; /* otherwise, try again */
1993-11-28 08:00:06 +00:00
#ifdef USE_INSULTS
(void) fprintf(stderr, "%s\n", INSULT);
#else
1993-10-04 19:10:33 +00:00
(void) fprintf(stderr, "%s\n", INCORRECT_PASSWORD);
1993-11-28 08:00:06 +00:00
#endif /* USE_INSULTS */
1993-06-11 22:03:45 +00:00
}
1993-10-04 19:10:33 +00:00
log_error(PASSWORD_NOT_CORRECT);
inform_user(PASSWORD_NOT_CORRECT);
1993-06-11 22:03:45 +00:00
1993-10-04 19:10:33 +00:00
exit(1);
1993-06-11 22:03:45 +00:00
}
/********************************************************************
*
* reminder()
*
* this function just prints the the reminder message
*/
static void reminder()
{
#ifdef SHORT_MESSAGE
1993-10-04 19:10:33 +00:00
(void) fprintf(stderr, "\n%s\n%s\n\n%s\n%s\n\n",
1993-06-11 22:03:45 +00:00
#else
1994-02-02 03:21:06 +00:00
(void) fprintf(stderr, "\n%s\n%s\n%s\n%s\n\n%s\n%s\n\n%s\n%s\n\n",
1994-03-12 18:55:39 +00:00
" CU sudo version 1.3.1, based on Root Group sudo version 1.1",
1993-10-28 13:36:13 +00:00
" sudo version 1.1, Copyright (C) 1991 The Root Group, Inc.",
1993-10-04 19:10:33 +00:00
" sudo comes with ABSOLUTELY NO WARRANTY. This is free software,",
" and you are welcome to redistribute it under certain conditions.",
1993-06-11 22:03:45 +00:00
#endif
1993-10-04 19:10:33 +00:00
"We trust you have received the usual lecture from the local Systems",
"Administrator. It usually boils down to these two things:",
" #1) Respect the privacy of others.",
" #2) Think before you type."
);
1993-10-28 13:36:13 +00:00
(void)fflush(stderr);
1993-06-11 22:03:45 +00:00
}