2 * Copyright (c) 2004-2008, 2010-2011 Todd C. Miller <Todd.Miller@courtesan.com>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #if defined(HAVE_SETRESUID) || defined(HAVE_SETREUID) || defined(HAVE_SETEUID)
21 #include <sys/types.h>
22 #include <sys/param.h>
26 #include <sys/socket.h>
35 #endif /* STDC_HEADERS */
38 #endif /* HAVE_STRING_H */
41 #endif /* HAVE_STRINGS_H */
44 #endif /* HAVE_UNISTD_H */
51 #if TIME_WITH_SYS_TIME
58 switch_user(uid_t euid, gid_t egid, int ngroups, GETGROUPS_T *groups)
62 /* When restoring root, change euid first; otherwise change it last. */
63 if (euid == ROOT_UID) {
64 if (seteuid(ROOT_UID) != 0)
65 error(1, "seteuid(ROOT_UID)");
68 if (setgroups(ngroups, groups) != 0)
69 error(1, "setgroups");
71 if (setegid(egid) != 0)
72 error(1, "setegid(%d)", (int)egid);
73 if (euid != ROOT_UID) {
74 if (seteuid(euid) != 0)
75 error(1, "seteuid(%d)", (int)euid);
82 * Wrapper to allow users to edit privileged files with their own uid.
85 sudo_edit(struct command_details *command_details)
87 struct command_details editor_details;
88 ssize_t nread, nwritten;
90 char *cp, *suff, **nargv, **ap, **files = NULL;
92 int rc, i, j, ac, ofd, tfd, nargc, rval, tmplen;
93 int editor_argc = 0, nfiles = 0;
95 struct timeval tv, tv1, tv2;
104 * Set real, effective and saved uids to root.
105 * We will change the euid as needed below.
107 if (setuid(ROOT_UID) != 0) {
108 warning("unable to change to uid to root (%u)", ROOT_UID);
113 * Find our temporary directory, one of /var/tmp, /usr/tmp, or /tmp
115 if (stat(_PATH_VARTMP, &sb) == 0 && S_ISDIR(sb.st_mode))
116 tmpdir = _PATH_VARTMP;
118 else if (stat(_PATH_USRTMP, &sb) == 0 && S_ISDIR(sb.st_mode))
119 tmpdir = _PATH_USRTMP;
123 tmplen = strlen(tmpdir);
124 while (tmplen > 0 && tmpdir[tmplen - 1] == '/')
128 * The user's editor must be separated from the files to be
129 * edited by a "--" option.
131 for (ap = command_details->argv; *ap != NULL; ap++) {
134 else if (strcmp(*ap, "--") == 0)
140 warningx("plugin error: missing file list for sudoedit");
145 * For each file specified by the user, make a temporary version
146 * and copy the contents of the original to it.
148 tf = emalloc2(nfiles, sizeof(*tf));
149 zero_bytes(tf, nfiles * sizeof(*tf));
150 for (i = 0, j = 0; i < nfiles; i++) {
152 switch_user(command_details->euid, command_details->egid,
153 command_details->ngroups, command_details->groups);
154 if ((ofd = open(files[i], O_RDONLY, 0644)) != -1 || errno == ENOENT) {
156 zero_bytes(&sb, sizeof(sb)); /* new file */
160 rc = fstat(ofd, &sb);
162 rc = stat(tf[j].ofile, &sb);
166 switch_user(ROOT_UID, user_details.egid,
167 user_details.ngroups, user_details.groups);
168 if (rc || (ofd != -1 && !S_ISREG(sb.st_mode))) {
170 warning("%s", files[i]);
172 warningx("%s: not a regular file", files[i]);
177 tf[j].ofile = files[i];
178 tf[j].osize = sb.st_size;
179 mtim_get(&sb, &tf[j].omtim);
180 if ((cp = strrchr(tf[j].ofile, '/')) != NULL)
184 suff = strrchr(cp, '.');
186 easprintf(&tf[j].tfile, "%.*s/%.*sXXXXXXXX%s", tmplen, tmpdir,
187 (int)(size_t)(suff - cp), cp, suff);
189 easprintf(&tf[j].tfile, "%.*s/%s.XXXXXXXX", tmplen, tmpdir, cp);
191 if (seteuid(user_details.uid) != 0)
192 error(1, "seteuid(%d)", (int)user_details.uid);
193 tfd = mkstemps(tf[j].tfile, suff ? strlen(suff) : 0);
194 if (seteuid(ROOT_UID) != 0)
195 error(1, "seteuid(ROOT_UID)");
201 while ((nread = read(ofd, buf, sizeof(buf))) != 0) {
202 if ((nwritten = write(tfd, buf, nread)) != nread) {
204 warning("%s", tf[j].tfile);
206 warningx("%s: short write", tf[j].tfile);
213 * We always update the stashed mtime because the time
214 * resolution of the filesystem the temporary file is on may
215 * not match that of the filesystem where the file to be edited
216 * resides. It is OK if touch() fails since we only use the info
217 * to determine whether or not a file has been modified.
219 (void) touch(tfd, NULL, &tf[j].omtim);
221 rc = fstat(tfd, &sb);
223 rc = stat(tf[j].tfile, &sb);
226 mtim_get(&sb, &tf[j].omtim);
230 if ((nfiles = j) == 0)
231 return 1; /* no files readable, you lose */
234 * Allocate space for the new argument vector and fill it in.
235 * We concatenate the editor with its args and the file list
236 * to create a new argv.
238 nargc = editor_argc + nfiles;
239 nargv = (char **) emalloc2(nargc + 1, sizeof(char *));
240 for (ac = 0; ac < editor_argc; ac++)
241 nargv[ac] = command_details->argv[ac];
242 for (i = 0; i < nfiles && ac < nargc; )
243 nargv[ac++] = tf[i++].tfile;
247 * Run the editor with the invoking user's creds,
248 * keeping track of the time spent in the editor.
250 gettimeofday(&tv1, NULL);
251 memcpy(&editor_details, command_details, sizeof(editor_details));
252 editor_details.uid = user_details.uid;
253 editor_details.euid = user_details.uid;
254 editor_details.gid = user_details.gid;
255 editor_details.egid = user_details.gid;
256 editor_details.ngroups = user_details.ngroups;
257 editor_details.groups = user_details.groups;
258 editor_details.argv = nargv;
259 rval = run_command(&editor_details);
260 gettimeofday(&tv2, NULL);
262 /* Copy contents of temp files to real ones */
263 for (i = 0; i < nfiles; i++) {
265 if (seteuid(user_details.uid) != 0)
266 error(1, "seteuid(%d)", (int)user_details.uid);
267 if ((tfd = open(tf[i].tfile, O_RDONLY, 0644)) != -1) {
269 rc = fstat(tfd, &sb);
271 rc = stat(tf[i].tfile, &sb);
274 if (seteuid(ROOT_UID) != 0)
275 error(1, "seteuid(ROOT_UID)");
276 if (rc || !S_ISREG(sb.st_mode)) {
278 warning("%s", tf[i].tfile);
280 warningx("%s: not a regular file", tf[i].tfile);
281 warningx("%s left unmodified", tf[i].ofile);
287 if (tf[i].osize == sb.st_size && timevalcmp(&tf[i].omtim, &tv, ==)) {
289 * If mtime and size match but the user spent no measurable
290 * time in the editor we can't tell if the file was changed.
292 timevalsub(&tv1, &tv2);
293 if (timevalisset(&tv2)) {
294 warningx("%s unchanged", tf[i].ofile);
300 switch_user(command_details->euid, command_details->egid,
301 command_details->ngroups, command_details->groups);
302 ofd = open(tf[i].ofile, O_WRONLY|O_TRUNC|O_CREAT, 0644);
303 switch_user(ROOT_UID, user_details.egid,
304 user_details.ngroups, user_details.groups);
306 warning("unable to write to %s", tf[i].ofile);
307 warningx("contents of edit session left in %s", tf[i].tfile);
311 while ((nread = read(tfd, buf, sizeof(buf))) > 0) {
312 if ((nwritten = write(ofd, buf, nread)) != nread) {
314 warning("%s", tf[i].ofile);
316 warningx("%s: short write", tf[i].ofile);
321 /* success, got EOF */
323 } else if (nread < 0) {
324 warning("unable to read temporary file");
325 warningx("contents of edit session left in %s", tf[i].tfile);
327 warning("unable to write to %s", tf[i].ofile);
328 warningx("contents of edit session left in %s", tf[i].tfile);
335 /* Clean up temp files and return. */
336 for (i = 0; i < nfiles; i++) {
337 if (tf[i].tfile != NULL)
343 #else /* HAVE_SETRESUID || HAVE_SETREUID || HAVE_SETEUID */
346 * Must have the ability to change the effective uid to use sudoedit.
349 sudo_edit(struct command_details *command_details)
354 #endif /* HAVE_SETRESUID || HAVE_SETREUID || HAVE_SETEUID */