2 * Copyright (c) 2004-2008, 2010-2013 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 #include <sys/types.h>
23 #include <sys/socket.h>
32 #endif /* STDC_HEADERS */
35 #endif /* HAVE_STRING_H */
38 #endif /* HAVE_STRINGS_H */
41 #endif /* HAVE_UNISTD_H */
48 #if TIME_WITH_SYS_TIME
54 #if defined(HAVE_SETRESUID) || defined(HAVE_SETREUID) || defined(HAVE_SETEUID)
57 switch_user(uid_t euid, gid_t egid, int ngroups, GETGROUPS_T *groups)
60 debug_decl(switch_user, SUDO_DEBUG_EDIT)
62 /* When restoring root, change euid first; otherwise change it last. */
63 if (euid == ROOT_UID) {
64 if (seteuid(ROOT_UID) != 0)
65 fatal("seteuid(ROOT_UID)");
67 if (setegid(egid) != 0)
68 fatal("setegid(%d)", (int)egid);
70 if (sudo_setgroups(ngroups, groups) != 0)
73 if (euid != ROOT_UID) {
74 if (seteuid(euid) != 0)
75 fatal("seteuid(%d)", (int)euid);
83 * Wrapper to allow users to edit privileged files with their own uid.
86 sudo_edit(struct command_details *command_details)
88 struct command_details editor_details;
89 ssize_t nread, nwritten;
91 char *cp, *suff, **nargv, **ap, **files = NULL;
93 int rc, i, j, ac, ofd, tfd, nargc, rval, tmplen;
94 int editor_argc = 0, nfiles = 0;
96 struct timeval tv, tv1, tv2;
100 struct timeval omtim;
103 debug_decl(sudo_edit, SUDO_DEBUG_EDIT)
106 * Set real, effective and saved uids to root.
107 * We will change the euid as needed below.
109 if (setuid(ROOT_UID) != 0) {
110 warning(_("unable to change uid to root (%u)"), ROOT_UID);
115 * Find our temporary directory, one of /var/tmp, /usr/tmp, or /tmp
117 if (stat(_PATH_VARTMP, &sb) == 0 && S_ISDIR(sb.st_mode))
118 tmpdir = _PATH_VARTMP;
120 else if (stat(_PATH_USRTMP, &sb) == 0 && S_ISDIR(sb.st_mode))
121 tmpdir = _PATH_USRTMP;
125 tmplen = strlen(tmpdir);
126 while (tmplen > 0 && tmpdir[tmplen - 1] == '/')
130 * The user's editor must be separated from the files to be
131 * edited by a "--" option.
133 for (ap = command_details->argv; *ap != NULL; ap++) {
136 else if (strcmp(*ap, "--") == 0)
142 warningx(_("plugin error: missing file list for sudoedit"));
147 * For each file specified by the user, make a temporary version
148 * and copy the contents of the original to it.
150 tf = emalloc2(nfiles, sizeof(*tf));
151 zero_bytes(tf, nfiles * sizeof(*tf));
152 for (i = 0, j = 0; i < nfiles; i++) {
154 switch_user(command_details->euid, command_details->egid,
155 command_details->ngroups, command_details->groups);
156 if ((ofd = open(files[i], O_RDONLY, 0644)) != -1 || errno == ENOENT) {
158 zero_bytes(&sb, sizeof(sb)); /* new file */
161 rc = fstat(ofd, &sb);
164 switch_user(ROOT_UID, user_details.egid,
165 user_details.ngroups, user_details.groups);
166 if (rc || (ofd != -1 && !S_ISREG(sb.st_mode))) {
168 warning("%s", files[i]);
170 warningx(_("%s: not a regular file"), files[i]);
175 tf[j].ofile = files[i];
176 tf[j].osize = sb.st_size;
177 mtim_get(&sb, &tf[j].omtim);
178 if ((cp = strrchr(tf[j].ofile, '/')) != NULL)
182 suff = strrchr(cp, '.');
184 easprintf(&tf[j].tfile, "%.*s/%.*sXXXXXXXX%s", tmplen, tmpdir,
185 (int)(size_t)(suff - cp), cp, suff);
187 easprintf(&tf[j].tfile, "%.*s/%s.XXXXXXXX", tmplen, tmpdir, cp);
189 if (seteuid(user_details.uid) != 0)
190 fatal("seteuid(%d)", (int)user_details.uid);
191 tfd = mkstemps(tf[j].tfile, suff ? strlen(suff) : 0);
192 if (seteuid(ROOT_UID) != 0)
193 fatal("seteuid(ROOT_UID)");
199 while ((nread = read(ofd, buf, sizeof(buf))) != 0) {
200 if ((nwritten = write(tfd, buf, nread)) != nread) {
202 warning("%s", tf[j].tfile);
204 warningx(_("%s: short write"), tf[j].tfile);
211 * We always update the stashed mtime because the time
212 * resolution of the filesystem the temporary file is on may
213 * not match that of the filesystem where the file to be edited
214 * resides. It is OK if touch() fails since we only use the info
215 * to determine whether or not a file has been modified.
217 (void) touch(tfd, NULL, &tf[j].omtim);
218 rc = fstat(tfd, &sb);
220 mtim_get(&sb, &tf[j].omtim);
224 if ((nfiles = j) == 0)
225 goto cleanup; /* no files readable, you lose */
228 * Allocate space for the new argument vector and fill it in.
229 * We concatenate the editor with its args and the file list
230 * to create a new argv.
232 nargc = editor_argc + nfiles;
233 nargv = (char **) emalloc2(nargc + 1, sizeof(char *));
234 for (ac = 0; ac < editor_argc; ac++)
235 nargv[ac] = command_details->argv[ac];
236 for (i = 0; i < nfiles && ac < nargc; )
237 nargv[ac++] = tf[i++].tfile;
241 * Run the editor with the invoking user's creds,
242 * keeping track of the time spent in the editor.
244 gettimeofday(&tv1, NULL);
245 memcpy(&editor_details, command_details, sizeof(editor_details));
246 editor_details.uid = user_details.uid;
247 editor_details.euid = user_details.uid;
248 editor_details.gid = user_details.gid;
249 editor_details.egid = user_details.gid;
250 editor_details.ngroups = user_details.ngroups;
251 editor_details.groups = user_details.groups;
252 editor_details.argv = nargv;
253 rval = run_command(&editor_details);
254 gettimeofday(&tv2, NULL);
256 /* Copy contents of temp files to real ones */
257 for (i = 0; i < nfiles; i++) {
259 if (seteuid(user_details.uid) != 0)
260 fatal("seteuid(%d)", (int)user_details.uid);
261 if ((tfd = open(tf[i].tfile, O_RDONLY, 0644)) != -1) {
262 rc = fstat(tfd, &sb);
264 if (seteuid(ROOT_UID) != 0)
265 fatal("seteuid(ROOT_UID)");
266 if (rc || !S_ISREG(sb.st_mode)) {
268 warning("%s", tf[i].tfile);
270 warningx(_("%s: not a regular file"), tf[i].tfile);
271 warningx(_("%s left unmodified"), tf[i].ofile);
277 if (tf[i].osize == sb.st_size && timevalcmp(&tf[i].omtim, &tv, ==)) {
279 * If mtime and size match but the user spent no measurable
280 * time in the editor we can't tell if the file was changed.
282 timevalsub(&tv1, &tv2);
283 if (timevalisset(&tv2)) {
284 warningx(_("%s unchanged"), tf[i].ofile);
290 switch_user(command_details->euid, command_details->egid,
291 command_details->ngroups, command_details->groups);
292 ofd = open(tf[i].ofile, O_WRONLY|O_TRUNC|O_CREAT, 0644);
293 switch_user(ROOT_UID, user_details.egid,
294 user_details.ngroups, user_details.groups);
296 warning(_("unable to write to %s"), tf[i].ofile);
297 warningx(_("contents of edit session left in %s"), tf[i].tfile);
301 while ((nread = read(tfd, buf, sizeof(buf))) > 0) {
302 if ((nwritten = write(ofd, buf, nread)) != nread) {
304 warning("%s", tf[i].ofile);
306 warningx(_("%s: short write"), tf[i].ofile);
311 /* success, got EOF */
313 } else if (nread < 0) {
314 warning(_("unable to read temporary file"));
315 warningx(_("contents of edit session left in %s"), tf[i].tfile);
317 warning(_("unable to write to %s"), tf[i].ofile);
318 warningx(_("contents of edit session left in %s"), tf[i].tfile);
322 debug_return_int(rval);
325 /* Clean up temp files and return. */
327 for (i = 0; i < nfiles; i++) {
328 if (tf[i].tfile != NULL)
335 #else /* HAVE_SETRESUID || HAVE_SETREUID || HAVE_SETEUID */
338 * Must have the ability to change the effective uid to use sudoedit.
341 sudo_edit(struct command_details *command_details)
343 debug_decl(sudo_edit, SUDO_DEBUG_EDIT)
347 #endif /* HAVE_SETRESUID || HAVE_SETREUID || HAVE_SETEUID */