X-Git-Url: https://git.gag.com/?a=blobdiff_plain;f=sudo_edit.c;h=f8666d9555b80a0d075d622b9c4325e682d649ec;hb=bae76b8ef69fd40419f9031b57b3e7c21e407843;hp=f765e8a0a7d7473fce5a3913d314c4d984f40697;hpb=26b91e48d9297ef94e92d8501f12b1516096a940;p=debian%2Fsudo diff --git a/sudo_edit.c b/sudo_edit.c index f765e8a..f8666d9 100644 --- a/sudo_edit.c +++ b/sudo_edit.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2004 Todd C. Miller + * Copyright (c) 2004-2008, 2010 Todd C. Miller * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -14,7 +14,9 @@ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ -#include "config.h" +#include + +#if defined(HAVE_SETRESUID) || defined(HAVE_SETREUID) || defined(HAVE_SETEUID) #include #include @@ -33,57 +35,59 @@ #endif /* STDC_HEADERS */ #ifdef HAVE_STRING_H # include -#else -# ifdef HAVE_STRINGS_H -# include -# endif #endif /* HAVE_STRING_H */ +#ifdef HAVE_STRINGS_H +# include +#endif /* HAVE_STRINGS_H */ #ifdef HAVE_UNISTD_H # include #endif /* HAVE_UNISTD_H */ -#ifdef HAVE_ERR_H -# include -#else -# include "emul/err.h" -#endif /* HAVE_ERR_H */ #include +#include #include #include #include #include -#include +#if TIME_WITH_SYS_TIME +# include +#endif #include "sudo.h" -#ifndef lint -static const char rcsid[] = "$Sudo: sudo_edit.c,v 1.16 2004/09/15 16:16:20 millert Exp $"; -#endif /* lint */ +static char *find_editor __P((int *argc_out, char ***argv_out)); -extern sigaction_t saved_sa_int, saved_sa_quit, saved_sa_tstp, saved_sa_chld; +extern char **NewArgv; /* XXX */ /* * Wrapper to allow users to edit privileged files with their own uid. */ -int sudo_edit(argc, argv) +int +sudo_edit(argc, argv, envp) int argc; - char **argv; + char *argv[]; + char *envp[]; { ssize_t nread, nwritten; - pid_t kidpid, pid; const char *tmpdir; - char **nargv, **ap, *editor, *cp; + char *cp, *suff, **nargv, *editor, **files; + char **editor_argv = NULL; char buf[BUFSIZ]; - int i, ac, ofd, tfd, nargc, rval, tmplen; - sigaction_t sa; + int rc, i, j, ac, ofd, tfd, nargc, rval, nfiles, tmplen; + int editor_argc = 0; struct stat sb; - struct timespec ts1, ts2; + struct timeval tv, tv1, tv2; struct tempfile { char *tfile; char *ofile; - struct timespec omtim; + struct timeval omtim; off_t osize; } *tf; + /* Determine user's editor. */ + editor = find_editor(&editor_argc, &editor_argv); + if (editor == NULL) + return 1; + /* * Find our temporary directory, one of /var/tmp, /usr/tmp, or /tmp */ @@ -102,226 +106,163 @@ int sudo_edit(argc, argv) /* * For each file specified by the user, make a temporary version * and copy the contents of the original to it. - * XXX - It would be nice to lock the original files but that means - * keeping an extra fd open for each file. */ - tf = emalloc2(argc - 1, sizeof(*tf)); - memset(tf, 0, (argc - 1) * sizeof(*tf)); - for (i = 0, ap = argv + 1; i < argc - 1 && *ap != NULL; i++, ap++) { + files = argv + 1; + nfiles = argc - 1; + tf = emalloc2(nfiles, sizeof(*tf)); + zero_bytes(tf, nfiles * sizeof(*tf)); + for (i = 0, j = 0; i < nfiles; i++) { + rc = -1; set_perms(PERM_RUNAS); - ofd = open(*ap, O_RDONLY, 0644); - if (ofd != -1) { + if ((ofd = open(files[i], O_RDONLY, 0644)) != -1 || errno == ENOENT) { + if (ofd == -1) { + zero_bytes(&sb, sizeof(sb)); /* new file */ + rc = 0; + } else { #ifdef HAVE_FSTAT - if (fstat(ofd, &sb) != 0) { + rc = fstat(ofd, &sb); #else - if (stat(tf[i].ofile, &sb) != 0) { + rc = stat(tf[j].ofile, &sb); #endif - close(ofd); /* XXX - could reset errno */ - ofd = -1; } } set_perms(PERM_ROOT); - if (ofd == -1) { - if (errno != ENOENT) { - warn("%s", *ap); - argc--; - i--; - continue; - } - memset(&sb, 0, sizeof(sb)); - } else if (!S_ISREG(sb.st_mode)) { - warnx("%s: not a regular file", *ap); - close(ofd); - argc--; - i--; + if (rc || (ofd != -1 && !S_ISREG(sb.st_mode))) { + if (rc) + warning("%s", files[i]); + else + warningx("%s: not a regular file", files[i]); + if (ofd != -1) + close(ofd); continue; } - tf[i].ofile = *ap; - tf[i].omtim.tv_sec = mtim_getsec(sb); - tf[i].omtim.tv_nsec = mtim_getnsec(sb); - tf[i].osize = sb.st_size; - if ((cp = strrchr(tf[i].ofile, '/')) != NULL) + tf[j].ofile = files[i]; + tf[j].osize = sb.st_size; + mtim_get(&sb, &tf[j].omtim); + if ((cp = strrchr(tf[j].ofile, '/')) != NULL) cp++; else - cp = tf[i].ofile; - easprintf(&tf[i].tfile, "%.*s/%s.XXXXXXXX", tmplen, tmpdir, cp); + cp = tf[j].ofile; + suff = strrchr(cp, '.'); + if (suff != NULL) { + easprintf(&tf[j].tfile, "%.*s/%.*sXXXXXXXX%s", tmplen, tmpdir, (int)(size_t)(suff - cp), cp, suff); + } else { + easprintf(&tf[j].tfile, "%.*s/%s.XXXXXXXX", tmplen, tmpdir, cp); + } set_perms(PERM_USER); - tfd = mkstemp(tf[i].tfile); + tfd = mkstemps(tf[j].tfile, suff ? strlen(suff) : 0); set_perms(PERM_ROOT); if (tfd == -1) { - warn("mkstemp"); + warning("mkstemps"); goto cleanup; } if (ofd != -1) { while ((nread = read(ofd, buf, sizeof(buf))) != 0) { if ((nwritten = write(tfd, buf, nread)) != nread) { if (nwritten == -1) - warn("%s", tf[i].tfile); + warning("%s", tf[j].tfile); else - warnx("%s: short write", tf[i].tfile); + warningx("%s: short write", tf[j].tfile); goto cleanup; } } close(ofd); } -#ifdef HAVE_FSTAT /* - * If we are unable to set the mtime on the temp file to the value - * of the original file just make the stashed mtime match the temp - * file's mtime. It is better than nothing and we only use the info + * 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 + * resides. It is OK if touch() fails since we only use the info * to determine whether or not a file has been modified. */ - if (touch(tfd, NULL, &tf[i].omtim) == -1) { - if (fstat(tfd, &sb) == 0) { - tf[i].omtim.tv_sec = mtim_getsec(sb); - tf[i].omtim.tv_nsec = mtim_getnsec(sb); - } - /* XXX - else error? */ - } + (void) touch(tfd, NULL, &tf[j].omtim); +#ifdef HAVE_FSTAT + rc = fstat(tfd, &sb); +#else + rc = stat(tf[j].tfile, &sb); #endif + if (!rc) + mtim_get(&sb, &tf[j].omtim); close(tfd); + j++; } - if (argc == 1) - return(1); /* no files readable, you lose */ - - /* - * Determine which editor to use. We don't bother restricting this - * based on def_env_editor or def_editor since the editor runs with - * the uid of the invoking user, not the runas (privileged) user. - */ - if (((editor = getenv("VISUAL")) != NULL && *editor != '\0') || - ((editor = getenv("EDITOR")) != NULL && *editor != '\0')) { - editor = estrdup(editor); - } else { - editor = estrdup(def_editor); - if ((cp = strchr(editor, ':')) != NULL) - *cp = '\0'; /* def_editor could be a path */ - } + if ((nfiles = j) == 0) + return 1; /* no files readable, you lose */ /* * Allocate space for the new argument vector and fill it in. - * The EDITOR and VISUAL environment variables may contain command - * line args so look for those and alloc space for them too. + * We concatenate the editor with its args and the file list + * to create a new argv. + * We allocate an extra slot to be used if execve() fails. */ - nargc = argc; - for (cp = editor + 1; *cp != '\0'; cp++) { - if (isblank((unsigned char)cp[0]) && !isblank((unsigned char)cp[-1])) - nargc++; - } - nargv = (char **) emalloc2(nargc + 1, sizeof(char *)); - ac = 0; - for ((cp = strtok(editor, " \t")); cp != NULL; (cp = strtok(NULL, " \t"))) - nargv[ac++] = cp; - for (i = 0; i < argc - 1 && ac < nargc; ) + nargc = editor_argc + nfiles; + nargv = (char **) emalloc2(1 + nargc + 1, sizeof(char *)); + nargv++; + for (ac = 0; ac < editor_argc; ac++) + nargv[ac] = editor_argv[ac]; + for (i = 0; i < nfiles && ac < nargc; ) nargv[ac++] = tf[i++].tfile; nargv[ac] = NULL; - /* We wait for our own children and can be suspended. */ - sigemptyset(&sa.sa_mask); - sa.sa_flags = SA_RESTART; - sa.sa_handler = SIG_DFL; - (void) sigaction(SIGCHLD, &sa, NULL); - (void) sigaction(SIGTSTP, &saved_sa_tstp, NULL); - /* - * Fork and exec the editor with the invoking user's creds, + * Run the editor with the invoking user's creds, * keeping track of the time spent in the editor. */ - gettime(&ts1); - kidpid = fork(); - if (kidpid == -1) { - warn("fork"); - goto cleanup; - } else if (kidpid == 0) { - /* child */ - (void) sigaction(SIGINT, &saved_sa_int, NULL); - (void) sigaction(SIGQUIT, &saved_sa_quit, NULL); - (void) sigaction(SIGCHLD, &saved_sa_chld, NULL); - set_perms(PERM_FULL_USER); - execvp(nargv[0], nargv); - warn("unable to execute %s", nargv[0]); - _exit(127); - } + gettime(&tv1); + rval = run_command(editor, nargv, envp, user_uid, TRUE); + gettime(&tv2); - /* - * Wait for status from the child. Most modern kernels - * will not let an unprivileged child process send a - * signal to its privileged parent to we have to request - * status when the child is stopped and then send the - * same signal to our own pid. - */ - do { -#ifdef sudo_waitpid - pid = sudo_waitpid(kidpid, &i, WUNTRACED); + /* Copy contents of temp files to real ones */ + for (i = 0; i < nfiles; i++) { + rc = -1; + set_perms(PERM_USER); + if ((tfd = open(tf[i].tfile, O_RDONLY, 0644)) != -1) { +#ifdef HAVE_FSTAT + rc = fstat(tfd, &sb); #else - pid = wait(&i); + rc = stat(tf[i].tfile, &sb); #endif - if (pid == kidpid) { - if (WIFSTOPPED(i)) - kill(getpid(), WSTOPSIG(i)); - else - break; } - } while (pid != -1 || errno == EINTR); - gettime(&ts2); - if (pid == -1 || !WIFEXITED(i)) - rval = 1; - else - rval = WEXITSTATUS(i); - - /* Copy contents of temp files to real ones */ - for (i = 0; i < argc - 1; i++) { - set_perms(PERM_USER); - tfd = open(tf[i].tfile, O_RDONLY, 0644); set_perms(PERM_ROOT); - if (tfd < 0) { - warn("unable to read %s", tf[i].tfile); - warnx("%s left unmodified", tf[i].ofile); + if (rc || !S_ISREG(sb.st_mode)) { + if (rc) + warning("%s", tf[i].tfile); + else + warningx("%s: not a regular file", tf[i].tfile); + warningx("%s left unmodified", tf[i].ofile); + if (tfd != -1) + close(tfd); continue; } -#ifdef HAVE_FSTAT - if (fstat(tfd, &sb) == 0) { - if (!S_ISREG(sb.st_mode)) { - warnx("%s: not a regular file", tf[i].tfile); - warnx("%s left unmodified", tf[i].ofile); + mtim_get(&sb, &tv); + if (tf[i].osize == sb.st_size && 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. + */ + timevalsub(&tv1, &tv2); + if (timevalisset(&tv2)) { + warningx("%s unchanged", tf[i].ofile); + unlink(tf[i].tfile); + close(tfd); continue; } - if (tf[i].osize == sb.st_size && - tf[i].omtim.tv_sec == mtim_getsec(sb) && - tf[i].omtim.tv_nsec == mtim_getnsec(sb)) { - /* - * If mtime and size match but the user spent no measurable - * time in the editor we can't tell if the file was changed. - */ -#ifdef HAVE_TIMESPECSUB2 - timespecsub(&ts1, &ts2); -#else - timespecsub(&ts1, &ts2, &ts2); -#endif - if (timespecisset(&ts2)) { - warnx("%s unchanged", tf[i].ofile); - unlink(tf[i].tfile); - close(tfd); - continue; - } - } } -#endif set_perms(PERM_RUNAS); ofd = open(tf[i].ofile, O_WRONLY|O_TRUNC|O_CREAT, 0644); set_perms(PERM_ROOT); if (ofd == -1) { - warn("unable to write to %s", tf[i].ofile); - warnx("contents of edit session left in %s", tf[i].tfile); + warning("unable to write to %s", tf[i].ofile); + warningx("contents of edit session left in %s", tf[i].tfile); close(tfd); continue; } while ((nread = read(tfd, buf, sizeof(buf))) > 0) { if ((nwritten = write(ofd, buf, nread)) != nread) { if (nwritten == -1) - warn("%s", tf[i].ofile); + warning("%s", tf[i].ofile); else - warnx("%s: short write", tf[i].ofile); + warningx("%s: short write", tf[i].ofile); break; } } @@ -329,21 +270,127 @@ int sudo_edit(argc, argv) /* success, got EOF */ unlink(tf[i].tfile); } else if (nread < 0) { - warn("unable to read temporary file"); - warnx("contents of edit session left in %s", tf[i].tfile); + warning("unable to read temporary file"); + warningx("contents of edit session left in %s", tf[i].tfile); } else { - warn("unable to write to %s", tf[i].ofile); - warnx("contents of edit session left in %s", tf[i].tfile); + warning("unable to write to %s", tf[i].ofile); + warningx("contents of edit session left in %s", tf[i].tfile); } close(ofd); } - return(rval); + return rval; cleanup: /* Clean up temp files and return. */ - for (i = 0; i < argc - 1; i++) { + for (i = 0; i < nfiles; i++) { if (tf[i].tfile != NULL) unlink(tf[i].tfile); } - return(1); + return 1; +} + +static char * +resolve_editor(editor, argc_out, argv_out) + char *editor; + int *argc_out; + char ***argv_out; +{ + char *cp, **nargv, *editor_path = NULL; + int ac, nargc, wasblank; + + editor = estrdup(editor); /* becomes part of argv_out */ + + /* + * Split editor into an argument vector; editor is reused (do not free). + * The EDITOR and VISUAL environment variables may contain command + * line args so look for those and alloc space for them too. + */ + nargc = 1; + for (wasblank = FALSE, cp = editor; *cp != '\0'; cp++) { + if (isblank((unsigned char) *cp)) + wasblank = TRUE; + else if (wasblank) { + wasblank = FALSE; + nargc++; + } + } + /* If we can't find the editor in the user's PATH, give up. */ + cp = strtok(editor, " \t"); + if (cp == NULL || + find_path(cp, &editor_path, NULL, getenv("PATH"), 0) != FOUND) { + efree(editor); + return NULL; + } + nargv = (char **) emalloc2(nargc + 1, sizeof(char *)); + for (ac = 0; cp != NULL && ac < nargc; ac++) { + nargv[ac] = cp; + cp = strtok(NULL, " \t"); + } + nargv[ac] = NULL; + + *argc_out = nargc; + *argv_out = nargv; + return editor_path; +} + +/* + * Determine which editor to use. We don't need to worry about restricting + * this to a "safe" editor since it runs with the uid of the invoking user, + * not the runas (privileged) user. + * Fills in argv_out with an argument vector suitable for execve() that + * includes the editor with the specified files. + */ +static char * +find_editor(argc_out, argv_out) + int *argc_out; + char ***argv_out; +{ + char *cp, *editor, *editor_path = NULL, **ev, *ev0[4]; + + /* + * If any of SUDO_EDITOR, VISUAL or EDITOR are set, choose the first one. + */ + ev0[0] = "SUDO_EDITOR"; + ev0[1] = "VISUAL"; + ev0[2] = "EDITOR"; + ev0[3] = NULL; + for (ev = ev0; *ev != NULL; ev++) { + if ((editor = getenv(*ev)) != NULL && *editor != '\0') { + editor_path = resolve_editor(editor, argc_out, argv_out); + if (editor_path != NULL) + break; + } + } + if (editor_path == NULL) { + /* def_editor could be a path, split it up */ + editor = estrdup(def_editor); + cp = strtok(editor, ":"); + while (cp != NULL && editor_path == NULL) { + editor_path = resolve_editor(cp, argc_out, argv_out); + cp = strtok(NULL, ":"); + } + if (editor_path) + efree(editor); + } + if (!editor_path) { + audit_failure(NewArgv, "%s: command not found", editor); + warningx("%s: command not found", editor); + } + return editor_path; } + +#else /* HAVE_SETRESUID || HAVE_SETREUID || HAVE_SETEUID */ + +/* + * Must have the ability to change the effective uid to use sudoedit. + */ +int +sudo_edit(argc, argv, envp) + int argc; + char *argv[]; + char *envp[]; +{ + return 1; +} + +#endif /* HAVE_SETRESUID || HAVE_SETREUID || HAVE_SETEUID */