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)");
67 if (setegid(egid) != 0)
68 error(1, "setegid(%d)", (int)egid);
70 if (sudo_setgroups(ngroups, groups) != 0)
71 error(1, "setgroups");
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 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 */
159 rc = fstat(ofd, &sb);
162 switch_user(ROOT_UID, user_details.egid,
163 user_details.ngroups, user_details.groups);
164 if (rc || (ofd != -1 && !S_ISREG(sb.st_mode))) {
166 warning("%s", files[i]);
168 warningx(_("%s: not a regular file"), files[i]);
173 tf[j].ofile = files[i];
174 tf[j].osize = sb.st_size;
175 mtim_get(&sb, &tf[j].omtim);
176 if ((cp = strrchr(tf[j].ofile, '/')) != NULL)
180 suff = strrchr(cp, '.');
182 easprintf(&tf[j].tfile, "%.*s/%.*sXXXXXXXX%s", tmplen, tmpdir,
183 (int)(size_t)(suff - cp), cp, suff);
185 easprintf(&tf[j].tfile, "%.*s/%s.XXXXXXXX", tmplen, tmpdir, cp);
187 if (seteuid(user_details.uid) != 0)
188 error(1, "seteuid(%d)", (int)user_details.uid);
189 tfd = mkstemps(tf[j].tfile, suff ? strlen(suff) : 0);
190 if (seteuid(ROOT_UID) != 0)
191 error(1, "seteuid(ROOT_UID)");
197 while ((nread = read(ofd, buf, sizeof(buf))) != 0) {
198 if ((nwritten = write(tfd, buf, nread)) != nread) {
200 warning("%s", tf[j].tfile);
202 warningx(_("%s: short write"), tf[j].tfile);
209 * We always update the stashed mtime because the time
210 * resolution of the filesystem the temporary file is on may
211 * not match that of the filesystem where the file to be edited
212 * resides. It is OK if touch() fails since we only use the info
213 * to determine whether or not a file has been modified.
215 (void) touch(tfd, NULL, &tf[j].omtim);
216 rc = fstat(tfd, &sb);
218 mtim_get(&sb, &tf[j].omtim);
222 if ((nfiles = j) == 0)
223 return 1; /* no files readable, you lose */
226 * Allocate space for the new argument vector and fill it in.
227 * We concatenate the editor with its args and the file list
228 * to create a new argv.
230 nargc = editor_argc + nfiles;
231 nargv = (char **) emalloc2(nargc + 1, sizeof(char *));
232 for (ac = 0; ac < editor_argc; ac++)
233 nargv[ac] = command_details->argv[ac];
234 for (i = 0; i < nfiles && ac < nargc; )
235 nargv[ac++] = tf[i++].tfile;
239 * Run the editor with the invoking user's creds,
240 * keeping track of the time spent in the editor.
242 gettimeofday(&tv1, NULL);
243 memcpy(&editor_details, command_details, sizeof(editor_details));
244 editor_details.uid = user_details.uid;
245 editor_details.euid = user_details.uid;
246 editor_details.gid = user_details.gid;
247 editor_details.egid = user_details.gid;
248 editor_details.ngroups = user_details.ngroups;
249 editor_details.groups = user_details.groups;
250 editor_details.argv = nargv;
251 rval = run_command(&editor_details);
252 gettimeofday(&tv2, NULL);
254 /* Copy contents of temp files to real ones */
255 for (i = 0; i < nfiles; i++) {
257 if (seteuid(user_details.uid) != 0)
258 error(1, "seteuid(%d)", (int)user_details.uid);
259 if ((tfd = open(tf[i].tfile, O_RDONLY, 0644)) != -1) {
260 rc = fstat(tfd, &sb);
262 if (seteuid(ROOT_UID) != 0)
263 error(1, "seteuid(ROOT_UID)");
264 if (rc || !S_ISREG(sb.st_mode)) {
266 warning("%s", tf[i].tfile);
268 warningx(_("%s: not a regular file"), tf[i].tfile);
269 warningx(_("%s left unmodified"), tf[i].ofile);
275 if (tf[i].osize == sb.st_size && timevalcmp(&tf[i].omtim, &tv, ==)) {
277 * If mtime and size match but the user spent no measurable
278 * time in the editor we can't tell if the file was changed.
280 timevalsub(&tv1, &tv2);
281 if (timevalisset(&tv2)) {
282 warningx(_("%s unchanged"), tf[i].ofile);
288 switch_user(command_details->euid, command_details->egid,
289 command_details->ngroups, command_details->groups);
290 ofd = open(tf[i].ofile, O_WRONLY|O_TRUNC|O_CREAT, 0644);
291 switch_user(ROOT_UID, user_details.egid,
292 user_details.ngroups, user_details.groups);
294 warning(_("unable to write to %s"), tf[i].ofile);
295 warningx(_("contents of edit session left in %s"), tf[i].tfile);
299 while ((nread = read(tfd, buf, sizeof(buf))) > 0) {
300 if ((nwritten = write(ofd, buf, nread)) != nread) {
302 warning("%s", tf[i].ofile);
304 warningx(_("%s: short write"), tf[i].ofile);
309 /* success, got EOF */
311 } else if (nread < 0) {
312 warning(_("unable to read temporary file"));
313 warningx(_("contents of edit session left in %s"), tf[i].tfile);
315 warning(_("unable to write to %s"), tf[i].ofile);
316 warningx(_("contents of edit session left in %s"), tf[i].tfile);
323 /* Clean up temp files and return. */
324 for (i = 0; i < nfiles; i++) {
325 if (tf[i].tfile != NULL)
331 #else /* HAVE_SETRESUID || HAVE_SETREUID || HAVE_SETEUID */
334 * Must have the ability to change the effective uid to use sudoedit.
337 sudo_edit(struct command_details *command_details)
342 #endif /* HAVE_SETRESUID || HAVE_SETREUID || HAVE_SETEUID */