2004-01-21 22:25:10 +00:00
|
|
|
/*
|
2014-04-22 16:02:28 -06:00
|
|
|
* Copyright (c) 2004-2008, 2010-2014 Todd C. Miller <Todd.Miller@courtesan.com>
|
2004-01-21 22:25:10 +00:00
|
|
|
*
|
2004-02-13 21:36:43 +00:00
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
2004-01-21 22:25:10 +00:00
|
|
|
*
|
2004-02-13 21:36:43 +00:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
2004-01-21 22:25:10 +00:00
|
|
|
*/
|
|
|
|
|
2004-11-19 18:39:14 +00:00
|
|
|
#include <config.h>
|
2004-01-21 22:25:10 +00:00
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
2004-09-07 19:57:00 +00:00
|
|
|
#include <sys/time.h>
|
2004-01-21 22:25:10 +00:00
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#ifdef STDC_HEADERS
|
|
|
|
# include <stdlib.h>
|
|
|
|
# include <stddef.h>
|
|
|
|
#else
|
|
|
|
# ifdef HAVE_STDLIB_H
|
|
|
|
# include <stdlib.h>
|
|
|
|
# endif
|
|
|
|
#endif /* STDC_HEADERS */
|
|
|
|
#ifdef HAVE_STRING_H
|
|
|
|
# include <string.h>
|
|
|
|
#endif /* HAVE_STRING_H */
|
2010-06-29 13:08:05 -04:00
|
|
|
#ifdef HAVE_STRINGS_H
|
|
|
|
# include <strings.h>
|
|
|
|
#endif /* HAVE_STRINGS_H */
|
2004-01-21 22:25:10 +00:00
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
# include <unistd.h>
|
|
|
|
#endif /* HAVE_UNISTD_H */
|
|
|
|
#include <ctype.h>
|
2010-05-13 14:09:21 -04:00
|
|
|
#include <grp.h>
|
2004-01-21 22:25:10 +00:00
|
|
|
#include <pwd.h>
|
2004-01-22 02:57:01 +00:00
|
|
|
#include <signal.h>
|
2004-01-21 22:25:10 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
2013-11-17 16:15:36 -07:00
|
|
|
#ifdef TIME_WITH_SYS_TIME
|
2004-12-03 18:52:28 +00:00
|
|
|
# include <time.h>
|
|
|
|
#endif
|
2014-08-21 15:28:36 -06:00
|
|
|
#ifdef HAVE_SELINUX
|
|
|
|
# include <selinux/selinux.h>
|
|
|
|
#endif
|
2004-01-21 22:25:10 +00:00
|
|
|
|
|
|
|
#include "sudo.h"
|
2014-08-21 15:28:36 -06:00
|
|
|
#include "sudo_exec.h"
|
2004-01-21 22:25:10 +00:00
|
|
|
|
2012-03-12 13:52:51 -04:00
|
|
|
#if defined(HAVE_SETRESUID) || defined(HAVE_SETREUID) || defined(HAVE_SETEUID)
|
|
|
|
|
2014-08-21 15:28:35 -06:00
|
|
|
/*
|
|
|
|
* Editor temporary file name along with original name, mtime and size.
|
|
|
|
*/
|
|
|
|
struct tempfile {
|
|
|
|
char *tfile;
|
|
|
|
char *ofile;
|
|
|
|
struct timeval omtim;
|
|
|
|
off_t osize;
|
|
|
|
};
|
|
|
|
|
|
|
|
static char edit_tmpdir[MAX(sizeof(_PATH_VARTMP), sizeof(_PATH_TMP))];
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Find our temporary directory, one of /var/tmp, /usr/tmp, or /tmp
|
|
|
|
* Returns true on success, else false;
|
|
|
|
*/
|
|
|
|
static bool
|
|
|
|
set_tmpdir(void)
|
|
|
|
{
|
|
|
|
const char *tdir;
|
|
|
|
struct stat sb;
|
|
|
|
size_t len;
|
|
|
|
debug_decl(set_tmpdir, SUDO_DEBUG_EDIT)
|
|
|
|
|
|
|
|
if (stat(_PATH_VARTMP, &sb) == 0 && S_ISDIR(sb.st_mode)) {
|
|
|
|
tdir = _PATH_VARTMP;
|
|
|
|
}
|
|
|
|
#ifdef _PATH_USRTMP
|
|
|
|
else if (stat(_PATH_USRTMP, &sb) == 0 && S_ISDIR(sb.st_mode)) {
|
|
|
|
tdir = _PATH_USRTMP;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
else {
|
|
|
|
tdir = _PATH_TMP;
|
|
|
|
}
|
|
|
|
len = strlcpy(edit_tmpdir, tdir, sizeof(edit_tmpdir));
|
|
|
|
if (len >= sizeof(edit_tmpdir)) {
|
|
|
|
errno = ENAMETOOLONG;
|
|
|
|
sudo_warn("%s", tdir);
|
|
|
|
debug_return_bool(false);
|
|
|
|
}
|
|
|
|
while (len > 0 && edit_tmpdir[--len] == '/')
|
|
|
|
edit_tmpdir[len] = '\0';
|
|
|
|
debug_return_bool(true);
|
|
|
|
}
|
|
|
|
|
2010-05-13 14:09:21 -04:00
|
|
|
static void
|
|
|
|
switch_user(uid_t euid, gid_t egid, int ngroups, GETGROUPS_T *groups)
|
|
|
|
{
|
|
|
|
int serrno = errno;
|
2011-10-22 14:40:21 -04:00
|
|
|
debug_decl(switch_user, SUDO_DEBUG_EDIT)
|
2010-05-13 14:09:21 -04:00
|
|
|
|
2014-08-21 15:42:35 -06:00
|
|
|
sudo_debug_printf(SUDO_DEBUG_INFO|SUDO_DEBUG_LINENO,
|
|
|
|
"set uid:gid to %u:%u(%u)", euid, egid, ngroups ? groups[0] : egid);
|
|
|
|
|
2010-05-13 14:09:21 -04:00
|
|
|
/* When restoring root, change euid first; otherwise change it last. */
|
|
|
|
if (euid == ROOT_UID) {
|
|
|
|
if (seteuid(ROOT_UID) != 0)
|
2014-06-27 09:30:52 -06:00
|
|
|
sudo_fatal("seteuid(ROOT_UID)");
|
2010-05-13 14:09:21 -04:00
|
|
|
}
|
2011-07-20 16:54:12 -04:00
|
|
|
if (setegid(egid) != 0)
|
2014-06-27 09:30:52 -06:00
|
|
|
sudo_fatal("setegid(%d)", (int)egid);
|
2010-05-13 14:09:21 -04:00
|
|
|
if (ngroups != -1) {
|
2011-07-20 16:54:12 -04:00
|
|
|
if (sudo_setgroups(ngroups, groups) != 0)
|
2014-06-27 09:30:52 -06:00
|
|
|
sudo_fatal("setgroups");
|
2010-05-13 14:09:21 -04:00
|
|
|
}
|
|
|
|
if (euid != ROOT_UID) {
|
|
|
|
if (seteuid(euid) != 0)
|
2014-06-27 09:30:52 -06:00
|
|
|
sudo_fatal("seteuid(%d)", (int)euid);
|
2010-05-13 14:09:21 -04:00
|
|
|
}
|
|
|
|
errno = serrno;
|
2011-10-22 14:40:21 -04:00
|
|
|
|
|
|
|
debug_return;
|
2010-05-13 14:09:21 -04:00
|
|
|
}
|
2008-09-14 20:07:49 +00:00
|
|
|
|
2004-01-21 22:25:10 +00:00
|
|
|
/*
|
2014-08-21 15:28:35 -06:00
|
|
|
* Construct a temporary file name for file and return an
|
|
|
|
* open file descriptor. The temporary file name is stored
|
|
|
|
* in tfile which the caller is responsible for freeing.
|
2004-01-21 22:25:10 +00:00
|
|
|
*/
|
2014-08-21 15:28:35 -06:00
|
|
|
static int
|
|
|
|
sudo_edit_mktemp(const char *ofile, char **tfile)
|
2004-01-21 22:25:10 +00:00
|
|
|
{
|
2014-08-21 15:28:35 -06:00
|
|
|
const char *cp, *suff;
|
2014-08-21 15:42:35 -06:00
|
|
|
int tfd;
|
2014-08-21 15:28:35 -06:00
|
|
|
debug_decl(sudo_edit_mktemp, SUDO_DEBUG_EDIT)
|
2010-05-13 14:09:21 -04:00
|
|
|
|
2014-08-21 15:28:35 -06:00
|
|
|
if ((cp = strrchr(ofile, '/')) != NULL)
|
|
|
|
cp++;
|
2004-01-21 22:25:10 +00:00
|
|
|
else
|
2014-08-21 15:28:35 -06:00
|
|
|
cp = ofile;
|
|
|
|
suff = strrchr(cp, '.');
|
|
|
|
if (suff != NULL) {
|
|
|
|
sudo_easprintf(tfile, "%s/%.*sXXXXXXXX%s", edit_tmpdir,
|
|
|
|
(int)(size_t)(suff - cp), cp, suff);
|
|
|
|
} else {
|
|
|
|
sudo_easprintf(tfile, "%s/%s.XXXXXXXX", edit_tmpdir, cp);
|
2010-05-17 10:25:27 -04:00
|
|
|
}
|
2014-08-21 15:42:35 -06:00
|
|
|
tfd = mkstemps(*tfile, suff ? strlen(suff) : 0);
|
|
|
|
sudo_debug_printf(SUDO_DEBUG_INFO|SUDO_DEBUG_LINENO,
|
|
|
|
"%s -> %s, fd %d", ofile, *tfile, tfd);
|
|
|
|
debug_return_int(tfd);
|
2014-08-21 15:28:35 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create temporary copies of files[] and store the temporary path name
|
|
|
|
* along with the original name, size and mtime in tf.
|
|
|
|
* Returns the number of files copied (which may be less than nfiles)
|
|
|
|
* or -1 if a fatal error occurred.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
sudo_edit_create_tfiles(struct command_details *command_details,
|
|
|
|
struct tempfile *tf, char * const files[], int nfiles)
|
|
|
|
{
|
|
|
|
int i, j, tfd, ofd, rc;
|
|
|
|
char buf[BUFSIZ];
|
|
|
|
ssize_t nwritten, nread;
|
|
|
|
struct timeval times[2];
|
|
|
|
struct stat sb;
|
|
|
|
debug_decl(sudo_edit_create_tfiles, SUDO_DEBUG_EDIT)
|
2010-05-13 17:11:31 -04:00
|
|
|
|
2004-01-21 22:25:10 +00:00
|
|
|
/*
|
2004-09-06 16:11:42 +00:00
|
|
|
* For each file specified by the user, make a temporary version
|
2004-09-07 18:06:33 +00:00
|
|
|
* and copy the contents of the original to it.
|
2004-01-21 22:25:10 +00:00
|
|
|
*/
|
2010-05-13 14:09:21 -04:00
|
|
|
for (i = 0, j = 0; i < nfiles; i++) {
|
2010-05-13 17:11:31 -04:00
|
|
|
rc = -1;
|
2010-05-13 14:09:21 -04:00
|
|
|
switch_user(command_details->euid, command_details->egid,
|
|
|
|
command_details->ngroups, command_details->groups);
|
|
|
|
if ((ofd = open(files[i], O_RDONLY, 0644)) != -1 || errno == ENOENT) {
|
2004-09-16 16:58:03 +00:00
|
|
|
if (ofd == -1) {
|
2013-08-03 08:30:06 -06:00
|
|
|
memset(&sb, 0, sizeof(sb)); /* new file */
|
2010-05-13 17:11:31 -04:00
|
|
|
rc = 0;
|
2004-09-16 16:58:03 +00:00
|
|
|
} else {
|
2010-05-13 17:11:31 -04:00
|
|
|
rc = fstat(ofd, &sb);
|
2004-01-21 22:25:10 +00:00
|
|
|
}
|
|
|
|
}
|
2010-05-13 14:09:21 -04:00
|
|
|
switch_user(ROOT_UID, user_details.egid,
|
|
|
|
user_details.ngroups, user_details.groups);
|
2010-05-13 17:11:31 -04:00
|
|
|
if (rc || (ofd != -1 && !S_ISREG(sb.st_mode))) {
|
|
|
|
if (rc)
|
2014-06-27 09:30:52 -06:00
|
|
|
sudo_warn("%s", files[i]);
|
2004-09-16 16:58:03 +00:00
|
|
|
else
|
2014-06-27 09:30:52 -06:00
|
|
|
sudo_warnx(U_("%s: not a regular file"), files[i]);
|
2004-09-16 16:58:03 +00:00
|
|
|
if (ofd != -1)
|
|
|
|
close(ofd);
|
2004-09-15 16:16:20 +00:00
|
|
|
continue;
|
2004-01-21 22:25:10 +00:00
|
|
|
}
|
2010-05-13 14:09:21 -04:00
|
|
|
tf[j].ofile = files[i];
|
|
|
|
tf[j].osize = sb.st_size;
|
|
|
|
mtim_get(&sb, &tf[j].omtim);
|
2014-08-21 15:42:35 -06:00
|
|
|
sudo_debug_printf(SUDO_DEBUG_INFO|SUDO_DEBUG_LINENO,
|
|
|
|
"seteuid(%u)", user_details.uid);
|
2010-05-13 14:09:21 -04:00
|
|
|
if (seteuid(user_details.uid) != 0)
|
2014-06-27 09:30:52 -06:00
|
|
|
sudo_fatal("seteuid(%d)", (int)user_details.uid);
|
2014-08-21 15:28:35 -06:00
|
|
|
tfd = sudo_edit_mktemp(tf[j].ofile, &tf[j].tfile);
|
2014-08-21 15:42:35 -06:00
|
|
|
sudo_debug_printf(SUDO_DEBUG_INFO|SUDO_DEBUG_LINENO,
|
|
|
|
"seteuid(%u)", ROOT_UID);
|
2010-05-13 14:09:21 -04:00
|
|
|
if (seteuid(ROOT_UID) != 0)
|
2014-06-27 09:30:52 -06:00
|
|
|
sudo_fatal("seteuid(ROOT_UID)");
|
2004-09-08 18:38:06 +00:00
|
|
|
if (tfd == -1) {
|
2014-06-27 09:30:52 -06:00
|
|
|
sudo_warn("mkstemps");
|
2014-08-21 15:28:35 -06:00
|
|
|
debug_return_int(-1);
|
2004-01-21 22:25:10 +00:00
|
|
|
}
|
|
|
|
if (ofd != -1) {
|
|
|
|
while ((nread = read(ofd, buf, sizeof(buf))) != 0) {
|
2004-09-08 18:38:06 +00:00
|
|
|
if ((nwritten = write(tfd, buf, nread)) != nread) {
|
2004-01-21 22:25:10 +00:00
|
|
|
if (nwritten == -1)
|
2014-06-27 09:30:52 -06:00
|
|
|
sudo_warn("%s", tf[j].tfile);
|
2004-01-21 22:25:10 +00:00
|
|
|
else
|
2014-06-27 09:30:52 -06:00
|
|
|
sudo_warnx(U_("%s: short write"), tf[j].tfile);
|
2014-08-21 15:28:35 -06:00
|
|
|
debug_return_int(-1);
|
2004-01-21 22:25:10 +00:00
|
|
|
}
|
|
|
|
}
|
2004-09-06 16:11:42 +00:00
|
|
|
close(ofd);
|
2004-01-21 22:25:10 +00:00
|
|
|
}
|
2004-09-07 18:06:33 +00:00
|
|
|
/*
|
2009-09-30 13:50:58 +00:00
|
|
|
* We always update the stashed mtime because the time
|
|
|
|
* resolution of the filesystem the temporary file is on may
|
|
|
|
* not match that of the filesystem where the file to be edited
|
2014-06-26 15:51:15 -06:00
|
|
|
* resides. It is OK if futimes() fails since we only use the
|
|
|
|
* info to determine whether or not a file has been modified.
|
2004-09-07 18:06:33 +00:00
|
|
|
*/
|
2014-06-26 15:51:15 -06:00
|
|
|
times[0].tv_sec = times[1].tv_sec = tf[j].omtim.tv_sec;
|
|
|
|
times[0].tv_usec = times[1].tv_usec = tf[j].omtim.tv_usec;
|
2014-06-27 11:47:16 -06:00
|
|
|
#ifdef HAVE_FUTIMES
|
2014-06-26 15:51:15 -06:00
|
|
|
(void) futimes(tfd, times);
|
2014-06-27 11:47:16 -06:00
|
|
|
#else
|
|
|
|
(void) utimes(tf[j].tfile, times);
|
|
|
|
#endif
|
2010-05-13 17:11:31 -04:00
|
|
|
rc = fstat(tfd, &sb);
|
|
|
|
if (!rc)
|
2010-05-13 14:09:21 -04:00
|
|
|
mtim_get(&sb, &tf[j].omtim);
|
2004-09-08 18:38:06 +00:00
|
|
|
close(tfd);
|
2010-05-13 14:09:21 -04:00
|
|
|
j++;
|
2004-01-21 22:25:10 +00:00
|
|
|
}
|
2014-08-21 15:28:35 -06:00
|
|
|
debug_return_int(j);
|
|
|
|
}
|
2004-01-21 22:25:10 +00:00
|
|
|
|
2014-08-21 15:28:35 -06:00
|
|
|
/*
|
|
|
|
* Copy the temporary files specified in tf to the originals.
|
|
|
|
* Returns the number of copy errors or 0 if completely successful.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
sudo_edit_copy_tfiles(struct command_details *command_details,
|
|
|
|
struct tempfile *tf, int nfiles, struct timeval *times)
|
|
|
|
{
|
|
|
|
int i, tfd, ofd, rc, errors = 0;
|
|
|
|
char buf[BUFSIZ];
|
|
|
|
ssize_t nwritten, nread;
|
|
|
|
struct timeval tv;
|
|
|
|
struct stat sb;
|
|
|
|
debug_decl(sudo_edit_copy_tfiles, SUDO_DEBUG_EDIT)
|
2014-07-26 06:06:18 -06:00
|
|
|
|
|
|
|
/* Copy contents of temp files to real ones. */
|
2010-05-13 14:09:21 -04:00
|
|
|
for (i = 0; i < nfiles; i++) {
|
2010-05-13 17:11:31 -04:00
|
|
|
rc = -1;
|
2014-08-21 15:42:35 -06:00
|
|
|
sudo_debug_printf(SUDO_DEBUG_INFO|SUDO_DEBUG_LINENO,
|
|
|
|
"seteuid(%u)", user_details.uid);
|
2010-05-13 14:09:21 -04:00
|
|
|
if (seteuid(user_details.uid) != 0)
|
2014-06-27 09:30:52 -06:00
|
|
|
sudo_fatal("seteuid(%d)", (int)user_details.uid);
|
2004-09-16 16:58:03 +00:00
|
|
|
if ((tfd = open(tf[i].tfile, O_RDONLY, 0644)) != -1) {
|
2010-05-13 17:11:31 -04:00
|
|
|
rc = fstat(tfd, &sb);
|
2004-09-16 16:58:03 +00:00
|
|
|
}
|
2014-08-21 15:42:35 -06:00
|
|
|
sudo_debug_printf(SUDO_DEBUG_INFO|SUDO_DEBUG_LINENO,
|
|
|
|
"seteuid(%u)", ROOT_UID);
|
2010-05-13 14:09:21 -04:00
|
|
|
if (seteuid(ROOT_UID) != 0)
|
2014-06-27 09:30:52 -06:00
|
|
|
sudo_fatal("seteuid(ROOT_UID)");
|
2010-05-13 17:11:31 -04:00
|
|
|
if (rc || !S_ISREG(sb.st_mode)) {
|
|
|
|
if (rc)
|
2014-06-27 09:30:52 -06:00
|
|
|
sudo_warn("%s", tf[i].tfile);
|
2004-09-16 16:58:03 +00:00
|
|
|
else
|
2014-06-27 09:30:52 -06:00
|
|
|
sudo_warnx(U_("%s: not a regular file"), tf[i].tfile);
|
|
|
|
sudo_warnx(U_("%s left unmodified"), tf[i].ofile);
|
2004-09-16 16:58:03 +00:00
|
|
|
if (tfd != -1)
|
|
|
|
close(tfd);
|
2014-08-21 15:28:35 -06:00
|
|
|
errors++;
|
2004-01-21 22:25:10 +00:00
|
|
|
continue;
|
|
|
|
}
|
2010-05-13 14:09:21 -04:00
|
|
|
mtim_get(&sb, &tv);
|
2014-01-30 15:51:59 -07:00
|
|
|
if (tf[i].osize == sb.st_size && sudo_timevalcmp(&tf[i].omtim, &tv, ==)) {
|
2004-09-16 16:58:03 +00:00
|
|
|
/*
|
|
|
|
* If mtime and size match but the user spent no measurable
|
|
|
|
* time in the editor we can't tell if the file was changed.
|
|
|
|
*/
|
2014-06-26 15:51:15 -06:00
|
|
|
if (sudo_timevalcmp(×[0], ×[1], !=)) {
|
2014-06-27 09:30:52 -06:00
|
|
|
sudo_warnx(U_("%s unchanged"), tf[i].ofile);
|
2004-09-16 16:58:03 +00:00
|
|
|
unlink(tf[i].tfile);
|
|
|
|
close(tfd);
|
2014-08-21 15:28:35 -06:00
|
|
|
errors++;
|
2004-09-15 16:16:20 +00:00
|
|
|
continue;
|
|
|
|
}
|
2004-01-21 22:25:10 +00:00
|
|
|
}
|
2010-05-13 14:09:21 -04:00
|
|
|
switch_user(command_details->euid, command_details->egid,
|
|
|
|
command_details->ngroups, command_details->groups);
|
2004-01-21 22:25:10 +00:00
|
|
|
ofd = open(tf[i].ofile, O_WRONLY|O_TRUNC|O_CREAT, 0644);
|
2010-05-13 14:09:21 -04:00
|
|
|
switch_user(ROOT_UID, user_details.egid,
|
|
|
|
user_details.ngroups, user_details.groups);
|
2004-01-21 22:25:10 +00:00
|
|
|
if (ofd == -1) {
|
2014-06-27 09:30:52 -06:00
|
|
|
sudo_warn(U_("unable to write to %s"), tf[i].ofile);
|
|
|
|
sudo_warnx(U_("contents of edit session left in %s"), tf[i].tfile);
|
2004-09-08 18:38:06 +00:00
|
|
|
close(tfd);
|
2014-08-21 15:28:35 -06:00
|
|
|
errors++;
|
2004-01-21 22:25:10 +00:00
|
|
|
continue;
|
|
|
|
}
|
2004-09-08 18:38:06 +00:00
|
|
|
while ((nread = read(tfd, buf, sizeof(buf))) > 0) {
|
2004-01-21 22:25:10 +00:00
|
|
|
if ((nwritten = write(ofd, buf, nread)) != nread) {
|
|
|
|
if (nwritten == -1)
|
2014-06-27 09:30:52 -06:00
|
|
|
sudo_warn("%s", tf[i].ofile);
|
2004-01-21 22:25:10 +00:00
|
|
|
else
|
2014-06-27 09:30:52 -06:00
|
|
|
sudo_warnx(U_("%s: short write"), tf[i].ofile);
|
2004-01-21 22:25:10 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2004-09-07 18:06:33 +00:00
|
|
|
if (nread == 0) {
|
|
|
|
/* success, got EOF */
|
2004-01-21 22:25:10 +00:00
|
|
|
unlink(tf[i].tfile);
|
2004-09-07 18:06:33 +00:00
|
|
|
} else if (nread < 0) {
|
2014-06-27 09:30:52 -06:00
|
|
|
sudo_warn(U_("unable to read temporary file"));
|
|
|
|
sudo_warnx(U_("contents of edit session left in %s"), tf[i].tfile);
|
2004-09-07 18:06:33 +00:00
|
|
|
} else {
|
2014-06-27 09:30:52 -06:00
|
|
|
sudo_warn(U_("unable to write to %s"), tf[i].ofile);
|
|
|
|
sudo_warnx(U_("contents of edit session left in %s"), tf[i].tfile);
|
2004-08-17 19:11:47 +00:00
|
|
|
}
|
2004-01-21 22:25:10 +00:00
|
|
|
close(ofd);
|
|
|
|
}
|
2014-08-21 15:28:35 -06:00
|
|
|
debug_return_int(errors);
|
|
|
|
}
|
|
|
|
|
2014-08-21 15:28:36 -06:00
|
|
|
#ifdef HAVE_SELINUX
|
|
|
|
static int
|
|
|
|
selinux_edit_create_tfiles(struct command_details *command_details,
|
|
|
|
struct tempfile *tf, char * const files[], int nfiles)
|
|
|
|
{
|
|
|
|
char **sesh_args, **sesh_ap;
|
|
|
|
int i, rc, sesh_nargs;
|
|
|
|
struct stat sb;
|
|
|
|
struct command_details saved_command_details;
|
|
|
|
debug_decl(selinux_edit_create_tfiles, SUDO_DEBUG_EDIT);
|
|
|
|
|
|
|
|
/* Prepare selinux stuff (setexeccon) */
|
|
|
|
if (selinux_setup(command_details->selinux_role,
|
|
|
|
command_details->selinux_type, NULL, -1) != 0)
|
|
|
|
debug_return_int(-1);
|
|
|
|
|
|
|
|
if (nfiles < 1)
|
|
|
|
debug_return_int(0);
|
|
|
|
|
|
|
|
/* Construct common args for sesh */
|
|
|
|
memcpy(&saved_command_details, command_details, sizeof(struct command_details));
|
|
|
|
command_details->command = _PATH_SUDO_SESH;
|
|
|
|
command_details->flags |= CD_SUDOEDIT_COPY;
|
|
|
|
|
|
|
|
sesh_nargs = 3 + (nfiles * 2) + 1;
|
|
|
|
sesh_args = sesh_ap = sudo_emallocarray(sesh_nargs, sizeof(char *));
|
|
|
|
*sesh_ap++ = "sesh";
|
|
|
|
*sesh_ap++ = "-e";
|
|
|
|
*sesh_ap++ = "0";
|
|
|
|
|
|
|
|
for (i = 0; i < nfiles; i++) {
|
|
|
|
char *tfile, *ofile = files[i];
|
|
|
|
int tfd;
|
|
|
|
*sesh_ap++ = ofile;
|
|
|
|
tf[i].ofile = ofile;
|
|
|
|
if (stat(ofile, &sb) == -1)
|
|
|
|
memset(&sb, 0, sizeof(sb)); /* new file */
|
|
|
|
tf[i].osize = sb.st_size;
|
|
|
|
mtim_get(&sb, &tf[i].omtim);
|
|
|
|
/*
|
|
|
|
* The temp file must be created by the sesh helper,
|
|
|
|
* which uses O_EXCL | O_NOFOLLOW to make this safe.
|
|
|
|
*/
|
|
|
|
tfd = sudo_edit_mktemp(ofile, &tfile);
|
|
|
|
if (tfd == -1) {
|
|
|
|
sudo_warn("mkstemps");
|
|
|
|
sudo_efree(tfile);
|
|
|
|
sudo_efree(sesh_args);
|
|
|
|
debug_return_int(-1);
|
|
|
|
}
|
|
|
|
/* Helper will re-create temp file with proper security context. */
|
|
|
|
close(tfd);
|
|
|
|
unlink(tfile);
|
|
|
|
*sesh_ap++ = tfile;
|
|
|
|
tf[i].tfile = tfile;
|
|
|
|
}
|
|
|
|
*sesh_ap = NULL;
|
|
|
|
|
|
|
|
/* Run sesh -e 0 <o1> <t1> ... <on> <tn> */
|
|
|
|
command_details->argv = sesh_args;
|
|
|
|
rc = run_command(command_details);
|
|
|
|
switch (rc) {
|
|
|
|
case SESH_SUCCESS:
|
|
|
|
break;
|
|
|
|
case SESH_ERR_BAD_PATHS:
|
|
|
|
sudo_fatalx(_("sesh: internal error: odd number of paths"));
|
|
|
|
case SESH_ERR_NO_FILES:
|
|
|
|
sudo_fatalx(_("sesh: unable to create temporary files"));
|
|
|
|
default:
|
|
|
|
sudo_fatalx(_("sesh: unknown error %d"), rc);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Restore saved command_details. */
|
|
|
|
command_details->command = saved_command_details.command;
|
|
|
|
command_details->flags = saved_command_details.flags;
|
|
|
|
command_details->argv = saved_command_details.argv;
|
|
|
|
|
|
|
|
/* Chown to user's UID so they can edit the temporary files. */
|
|
|
|
for (i = 0; i < nfiles; i++) {
|
|
|
|
if (chown(tf[i].tfile, user_details.uid, user_details.gid) != 0) {
|
|
|
|
sudo_warn("unable to chown(%s) to %d:%d for editing",
|
|
|
|
tf[i].tfile, user_details.uid, user_details.gid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Contents of tf will be freed by caller. */
|
|
|
|
sudo_efree(sesh_args);
|
|
|
|
|
|
|
|
return (nfiles);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
selinux_edit_copy_tfiles(struct command_details *command_details,
|
|
|
|
struct tempfile *tf, int nfiles, struct timeval *times)
|
|
|
|
{
|
|
|
|
char **sesh_args, **sesh_ap;
|
|
|
|
int i, rc, sesh_nargs, rval = 1;
|
|
|
|
struct command_details saved_command_details;
|
|
|
|
struct timeval tv;
|
|
|
|
struct stat sb;
|
|
|
|
debug_decl(selinux_edit_copy_tfiles, SUDO_DEBUG_EDIT)
|
|
|
|
|
|
|
|
/* Prepare selinux stuff (setexeccon) */
|
|
|
|
if (selinux_setup(command_details->selinux_role,
|
|
|
|
command_details->selinux_type, NULL, -1) != 0)
|
|
|
|
debug_return_int(1);
|
|
|
|
|
|
|
|
if (nfiles < 1)
|
|
|
|
debug_return_int(0);
|
|
|
|
|
|
|
|
/* Construct common args for sesh */
|
|
|
|
memcpy(&saved_command_details, command_details, sizeof(struct command_details));
|
|
|
|
command_details->command = _PATH_SUDO_SESH;
|
|
|
|
command_details->flags |= CD_SUDOEDIT_COPY;
|
|
|
|
|
|
|
|
sesh_nargs = 3 + (nfiles * 2) + 1;
|
|
|
|
sesh_args = sesh_ap = sudo_emallocarray(sesh_nargs, sizeof(char *));
|
|
|
|
*sesh_ap++ = "sesh";
|
|
|
|
*sesh_ap++ = "-e";
|
|
|
|
*sesh_ap++ = "1";
|
|
|
|
|
|
|
|
/* Construct args for sesh -e 1 */
|
|
|
|
for (i = 0; i < nfiles; i++) {
|
|
|
|
if (stat(tf[i].tfile, &sb) == 0) {
|
|
|
|
mtim_get(&sb, &tv);
|
|
|
|
if (tf[i].osize == sb.st_size && sudo_timevalcmp(&tf[i].omtim, &tv, ==)) {
|
|
|
|
/*
|
|
|
|
* If mtime and size match but the user spent no measurable
|
|
|
|
* time in the editor we can't tell if the file was changed.
|
|
|
|
*/
|
|
|
|
if (sudo_timevalcmp(×[0], ×[1], !=)) {
|
|
|
|
sudo_warnx(U_("%s unchanged"), tf[i].ofile);
|
|
|
|
unlink(tf[i].tfile);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*sesh_ap++ = tf[i].tfile;
|
|
|
|
*sesh_ap++ = tf[i].ofile;
|
|
|
|
if (chown(tf[i].tfile, command_details->uid, command_details->gid) != 0) {
|
|
|
|
sudo_warn("unable to chown(%s) back to %d:%d", tf[i].tfile,
|
|
|
|
command_details->uid, command_details->gid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*sesh_ap = NULL;
|
|
|
|
|
|
|
|
if (sesh_ap - sesh_args > 3) {
|
|
|
|
/* Run sesh -e 1 <t1> <o1> ... <tn> <on> */
|
|
|
|
command_details->argv = sesh_args;
|
|
|
|
rc = run_command(command_details);
|
|
|
|
switch (rc) {
|
|
|
|
case SESH_SUCCESS:
|
|
|
|
rval = 0;
|
|
|
|
break;
|
|
|
|
case SESH_ERR_NO_FILES:
|
|
|
|
sudo_warnx(_("unable to copy temporary files back to their original location"));
|
|
|
|
sudo_warnx(U_("contents of edit session left in %s"), edit_tmpdir);
|
|
|
|
break;
|
|
|
|
case SESH_ERR_SOME_FILES:
|
|
|
|
sudo_warnx(_("unable to copy some of the temporary files back to their original location"));
|
|
|
|
sudo_warnx(U_("contents of edit session left in %s"), edit_tmpdir);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
sudo_warnx(_("sesh: unknown error %d"), rc);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Restore saved command_details. */
|
|
|
|
command_details->command = saved_command_details.command;
|
|
|
|
command_details->flags = saved_command_details.flags;
|
|
|
|
command_details->argv = saved_command_details.argv;
|
|
|
|
|
|
|
|
debug_return_int(rval);
|
|
|
|
}
|
|
|
|
#endif /* HAVE_SELINUX */
|
|
|
|
|
2014-08-21 15:28:35 -06:00
|
|
|
/*
|
|
|
|
* Wrapper to allow users to edit privileged files with their own uid.
|
2014-08-21 15:28:36 -06:00
|
|
|
* Returns 0 on success and 1 on failure.
|
2014-08-21 15:28:35 -06:00
|
|
|
*/
|
|
|
|
int
|
|
|
|
sudo_edit(struct command_details *command_details)
|
|
|
|
{
|
|
|
|
struct command_details saved_command_details;
|
|
|
|
char **nargv = NULL, **ap, **files = NULL;
|
2014-08-21 15:28:36 -06:00
|
|
|
int errors, i, ac, nargc, rval;
|
2014-08-21 15:28:35 -06:00
|
|
|
int editor_argc = 0, nfiles = 0;
|
|
|
|
struct timeval times[2];
|
|
|
|
struct tempfile *tf = NULL;
|
|
|
|
debug_decl(sudo_edit, SUDO_DEBUG_EDIT)
|
|
|
|
|
|
|
|
if (!set_tmpdir())
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set real, effective and saved uids to root.
|
|
|
|
* We will change the euid as needed below.
|
|
|
|
*/
|
2014-08-21 15:42:35 -06:00
|
|
|
sudo_debug_printf(SUDO_DEBUG_INFO|SUDO_DEBUG_LINENO,
|
|
|
|
"setuid(%u)", ROOT_UID);
|
2014-08-21 15:28:35 -06:00
|
|
|
if (setuid(ROOT_UID) != 0) {
|
|
|
|
sudo_warn(U_("unable to change uid to root (%u)"), ROOT_UID);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The user's editor must be separated from the files to be
|
|
|
|
* edited by a "--" option.
|
|
|
|
*/
|
|
|
|
for (ap = command_details->argv; *ap != NULL; ap++) {
|
|
|
|
if (files)
|
|
|
|
nfiles++;
|
|
|
|
else if (strcmp(*ap, "--") == 0)
|
|
|
|
files = ap + 1;
|
|
|
|
else
|
|
|
|
editor_argc++;
|
|
|
|
}
|
|
|
|
if (nfiles == 0) {
|
|
|
|
sudo_warnx(U_("plugin error: missing file list for sudoedit"));
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy editor files to temporaries. */
|
|
|
|
tf = sudo_ecalloc(nfiles, sizeof(*tf));
|
2014-08-21 15:28:36 -06:00
|
|
|
#ifdef HAVE_SELINUX
|
|
|
|
if (ISSET(command_details->flags, CD_RBAC_ENABLED))
|
|
|
|
nfiles = selinux_edit_create_tfiles(command_details, tf, files, nfiles);
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
nfiles = sudo_edit_create_tfiles(command_details, tf, files, nfiles);
|
2014-08-21 15:28:35 -06:00
|
|
|
if (nfiles <= 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Allocate space for the new argument vector and fill it in.
|
|
|
|
* We concatenate the editor with its args and the file list
|
|
|
|
* to create a new argv.
|
|
|
|
*/
|
|
|
|
nargc = editor_argc + nfiles;
|
|
|
|
nargv = sudo_emallocarray(nargc + 1, sizeof(char *));
|
|
|
|
for (ac = 0; ac < editor_argc; ac++)
|
|
|
|
nargv[ac] = command_details->argv[ac];
|
|
|
|
for (i = 0; i < nfiles && ac < nargc; )
|
|
|
|
nargv[ac++] = tf[i++].tfile;
|
|
|
|
nargv[ac] = NULL;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Run the editor with the invoking user's creds,
|
|
|
|
* keeping track of the time spent in the editor.
|
|
|
|
*/
|
|
|
|
gettimeofday(×[0], NULL);
|
|
|
|
memcpy(&saved_command_details, command_details, sizeof(struct command_details));
|
|
|
|
command_details->uid = user_details.uid;
|
|
|
|
command_details->euid = user_details.uid;
|
|
|
|
command_details->gid = user_details.gid;
|
|
|
|
command_details->egid = user_details.gid;
|
|
|
|
command_details->ngroups = user_details.ngroups;
|
|
|
|
command_details->groups = user_details.groups;
|
|
|
|
command_details->argv = nargv;
|
|
|
|
rval = run_command(command_details);
|
|
|
|
gettimeofday(×[1], NULL);
|
|
|
|
|
|
|
|
/* Restore saved command_details. */
|
|
|
|
command_details->uid = saved_command_details.uid;
|
|
|
|
command_details->euid = saved_command_details.uid;
|
|
|
|
command_details->gid = saved_command_details.gid;
|
|
|
|
command_details->egid = saved_command_details.gid;
|
|
|
|
command_details->ngroups = saved_command_details.ngroups;
|
|
|
|
command_details->groups = saved_command_details.groups;
|
|
|
|
command_details->argv = saved_command_details.argv;
|
|
|
|
|
|
|
|
/* Copy contents of temp files to real ones. */
|
2014-08-21 15:28:36 -06:00
|
|
|
#ifdef HAVE_SELINUX
|
|
|
|
if (ISSET(command_details->flags, CD_RBAC_ENABLED))
|
|
|
|
errors = selinux_edit_copy_tfiles(command_details, tf, nfiles, times);
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
errors = sudo_edit_copy_tfiles(command_details, tf, nfiles, times);
|
2014-08-21 15:28:35 -06:00
|
|
|
|
2014-07-30 09:46:48 -06:00
|
|
|
sudo_efree(tf);
|
|
|
|
sudo_efree(nargv);
|
2014-08-21 15:28:36 -06:00
|
|
|
debug_return_int(errors ? 1 : rval);
|
2004-01-21 22:25:10 +00:00
|
|
|
|
|
|
|
cleanup:
|
|
|
|
/* Clean up temp files and return. */
|
2011-12-20 13:50:48 -05:00
|
|
|
if (tf != NULL) {
|
|
|
|
for (i = 0; i < nfiles; i++) {
|
|
|
|
if (tf[i].tfile != NULL)
|
|
|
|
unlink(tf[i].tfile);
|
|
|
|
}
|
2004-01-21 22:25:10 +00:00
|
|
|
}
|
2014-07-30 09:46:48 -06:00
|
|
|
sudo_efree(tf);
|
|
|
|
sudo_efree(nargv);
|
2011-10-22 14:40:21 -04:00
|
|
|
debug_return_int(1);
|
2010-05-14 16:35:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#else /* HAVE_SETRESUID || HAVE_SETREUID || HAVE_SETEUID */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Must have the ability to change the effective uid to use sudoedit.
|
|
|
|
*/
|
|
|
|
int
|
2011-03-10 14:24:10 -05:00
|
|
|
sudo_edit(struct command_details *command_details)
|
2010-05-14 16:35:03 -04:00
|
|
|
{
|
2011-10-22 14:40:21 -04:00
|
|
|
debug_decl(sudo_edit, SUDO_DEBUG_EDIT)
|
|
|
|
debug_return_int(1);
|
2004-01-21 22:25:10 +00:00
|
|
|
}
|
2010-05-14 16:35:03 -04:00
|
|
|
|
|
|
|
#endif /* HAVE_SETRESUID || HAVE_SETREUID || HAVE_SETEUID */
|