=================================================================== RCS file: /cvsrepo/anoncvs/cvs/src/usr.bin/sudo/Attic/visudo.c,v retrieving revision 1.13 retrieving revision 1.14 diff -c -r1.13 -r1.14 *** src/usr.bin/sudo/Attic/visudo.c 2003/03/15 21:23:54 1.13 --- src/usr.bin/sudo/Attic/visudo.c 2003/04/03 19:15:34 1.14 *************** *** 36,41 **** --- 36,43 ---- * Lock the sudoers file for safe editing (ala vipw) and check for parse errors. */ + #define _SUDO_MAIN + #include "config.h" #include *************** *** 62,67 **** --- 64,74 ---- #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 *************** *** 73,79 **** #include "version.h" #ifndef lint ! static const char rcsid[] = "$Sudo: visudo.c,v 1.149 2003/03/15 20:31:02 millert Exp $"; #endif /* lint */ /* --- 80,86 ---- #include "version.h" #ifndef lint ! static const char rcsid[] = "$Sudo: visudo.c,v 1.150 2003/04/02 18:25:19 millert Exp $"; #endif /* lint */ /* *************** *** 135,144 **** /* Warn about aliases that are used before being defined. */ pedantic = 1; ! /* ! * Parse command line options ! */ ! Argv = argv; /* * Arg handling. --- 142,148 ---- /* Warn about aliases that are used before being defined. */ pedantic = 1; ! Argv = argv; /* for warn/err */ /* * Arg handling. *************** *** 147,153 **** while ((ch = getopt(argc, argv, "Vcf:sq")) != -1) { switch (ch) { case 'V': ! (void) printf("visudo version %s\n", version); exit(0); case 'c': checkonly++; /* check mode */ --- 151,157 ---- while ((ch = getopt(argc, argv, "Vcf:sq")) != -1) { switch (ch) { case 'V': ! (void) printf("%s version %s\n", getprogname(), version); exit(0); case 'c': checkonly++; /* check mode */ *************** *** 173,183 **** /* Mock up a fake sudo_user struct. */ user_host = user_shost = user_cmnd = ""; ! if ((sudo_user.pw = getpwuid(getuid())) == NULL) { ! (void) fprintf(stderr, "%s: Can't find you in the passwd database.\n", ! Argv[0]); ! exit(1); ! } /* Setup defaults data structures. */ init_defaults(); --- 177,184 ---- /* Mock up a fake sudo_user struct. */ user_host = user_shost = user_cmnd = ""; ! if ((sudo_user.pw = getpwuid(getuid())) == NULL) ! errx(1, "you don't exist in the passwd database"); /* Setup defaults data structures. */ init_defaults(); *************** *** 190,223 **** * sudoers_fd must remain open throughout in order to hold the lock. */ sudoers_fd = open(sudoers, O_RDWR | O_CREAT, SUDOERS_MODE); ! if (sudoers_fd == -1) { ! (void) fprintf(stderr, "%s: %s: %s\n", Argv[0], sudoers, ! strerror(errno)); ! exit(1); ! } ! if (!lock_file(sudoers_fd, SUDO_TLOCK)) { ! (void) fprintf(stderr, "%s: sudoers file busy, try again later.\n", ! Argv[0]); ! exit(1); ! } #ifdef HAVE_FSTAT ! if (fstat(sudoers_fd, &sudoers_sb) == -1) { #else ! if (stat(sudoers, &sudoers_sb) == -1) { #endif ! (void) fprintf(stderr, "%s: can't stat %s: %s\n", ! Argv[0], sudoers, strerror(errno)); ! exit(1); ! } /* * Open sudoers temp file. */ stmp_fd = open(stmp, O_WRONLY | O_CREAT | O_TRUNC, 0600); ! if (stmp_fd < 0) { ! (void) fprintf(stderr, "%s: %s: %s\n", Argv[0], stmp, strerror(errno)); ! exit(1); ! } /* Install signal handlers to clean up stmp if we are killed. */ setup_signals(); --- 191,213 ---- * sudoers_fd must remain open throughout in order to hold the lock. */ sudoers_fd = open(sudoers, O_RDWR | O_CREAT, SUDOERS_MODE); ! if (sudoers_fd == -1) ! err(1, "%s", sudoers); ! if (!lock_file(sudoers_fd, SUDO_TLOCK)) ! errx(1, "sudoers file busy, try again later"); #ifdef HAVE_FSTAT ! if (fstat(sudoers_fd, &sudoers_sb) == -1) #else ! if (stat(sudoers, &sudoers_sb) == -1) #endif ! err(1, "can't stat %s", sudoers); /* * Open sudoers temp file. */ stmp_fd = open(stmp, O_WRONLY | O_CREAT | O_TRUNC, 0600); ! if (stmp_fd < 0) ! err(1, "%s", stmp); /* Install signal handlers to clean up stmp if we are killed. */ setup_signals(); *************** *** 225,235 **** /* Copy sudoers -> stmp and reset the mtime */ if (sudoers_sb.st_size) { while ((n = read(sudoers_fd, buf, sizeof(buf))) > 0) ! if (write(stmp_fd, buf, n) != n) { ! (void) fprintf(stderr, "%s: Write failed: %s\n", Argv[0], ! strerror(errno)); ! Exit(-1); ! } /* Add missing newline at EOF if needed. */ if (n > 0 && buf[n - 1] != '\n') { --- 215,222 ---- /* Copy sudoers -> stmp and reset the mtime */ if (sudoers_sb.st_size) { while ((n = read(sudoers_fd, buf, sizeof(buf))) > 0) ! if (write(stmp_fd, buf, n) != n) ! err(1, "write error"); /* Add missing newline at EOF if needed. */ if (n > 0 && buf[n - 1] != '\n') { *************** *** 270,278 **** } else { if (def_flag(I_ENV_EDITOR)) { /* If we are honoring $EDITOR this is a fatal error. */ ! (void) fprintf(stderr, ! "%s: specified editor (%s) doesn't exist!\n", ! Argv[0], UserEditor); Exit(-1); } else { /* Otherwise, just ignore $EDITOR. */ --- 257,263 ---- } else { if (def_flag(I_ENV_EDITOR)) { /* If we are honoring $EDITOR this is a fatal error. */ ! warnx("specified editor (%s) doesn't exist!", UserEditor); Exit(-1); } else { /* Otherwise, just ignore $EDITOR. */ *************** *** 295,302 **** if (stat(UserEditor, &user_editor_sb) != 0) { /* Should never happen since we already checked above. */ ! (void) fprintf(stderr, "%s: unable to stat editor (%s): %s\n", ! Argv[0], UserEditor, strerror(errno)); Exit(-1); } EditorPath = estrdup(def_str(I_EDITOR)); --- 280,286 ---- if (stat(UserEditor, &user_editor_sb) != 0) { /* Should never happen since we already checked above. */ ! warn("unable to stat editor (%s)", UserEditor); Exit(-1); } EditorPath = estrdup(def_str(I_EDITOR)); *************** *** 344,351 **** /* Bleah, none of the editors existed! */ if (Editor == NULL || *Editor == '\0') { ! (void) fprintf(stderr, "%s: no editor found (editor path = %s)\n", ! Argv[0], def_str(I_EDITOR)); Exit(-1); } } --- 328,334 ---- /* Bleah, none of the editors existed! */ if (Editor == NULL || *Editor == '\0') { ! warnx("no editor found (editor path = %s)", def_str(I_EDITOR)); Exit(-1); } } *************** *** 381,395 **** * Sanity checks. */ if (stat(stmp, &stmp_sb) < 0) { ! (void) fprintf(stderr, ! "%s: Can't stat temporary file (%s), %s unchanged.\n", ! Argv[0], stmp, sudoers); Exit(-1); } if (stmp_sb.st_size == 0) { ! (void) fprintf(stderr, ! "%s: Zero length temporary file (%s), %s unchanged.\n", ! Argv[0], stmp, sudoers); Exit(-1); } --- 364,376 ---- * Sanity checks. */ if (stat(stmp, &stmp_sb) < 0) { ! warnx("cannot stat temporary file (%s), %s unchanged", ! stmp, sudoers); Exit(-1); } if (stmp_sb.st_size == 0) { ! warnx("zero length temporary file (%s), %s unchanged", ! stmp, sudoers); Exit(-1); } *************** *** 400,408 **** yyout = stdout; yyin = fopen(stmp, "r+"); if (yyin == NULL) { ! (void) fprintf(stderr, ! "%s: Can't re-open temporary file (%s), %s unchanged.\n", ! Argv[0], stmp, sudoers); Exit(-1); } --- 381,388 ---- yyout = stdout; yyin = fopen(stmp, "r+"); if (yyin == NULL) { ! warnx("can't re-open temporary file (%s), %s unchanged.", ! stmp, sudoers); Exit(-1); } *************** *** 419,434 **** /* Parse the sudoers temp file */ yyrestart(yyin); if (yyparse() && parse_error != TRUE) { ! (void) fprintf(stderr, ! "%s: Failed to parse temporary file (%s), unknown error.\n", ! Argv[0], stmp); parse_error = TRUE; } fclose(yyin); } else { ! (void) fprintf(stderr, ! "%s: Editor (%s) failed, %s unchanged.\n", Argv[0], ! Editor, sudoers); Exit(-1); } --- 399,411 ---- /* Parse the sudoers temp file */ yyrestart(yyin); if (yyparse() && parse_error != TRUE) { ! warnx("unabled to parse temporary file (%s), unknown error", ! stmp); parse_error = TRUE; } fclose(yyin); } else { ! warnx("editor (%s) failed, %s unchanged", Editor, sudoers); Exit(-1); } *************** *** 452,458 **** */ if (sudoers_sb.st_mtime != now && sudoers_sb.st_mtime == stmp_sb.st_mtime && sudoers_sb.st_size == stmp_sb.st_size) { ! (void) fprintf(stderr, "%s: sudoers file unchanged.\n", Argv[0]); Exit(0); } --- 429,435 ---- */ if (sudoers_sb.st_mtime != now && sudoers_sb.st_mtime == stmp_sb.st_mtime && sudoers_sb.st_size == stmp_sb.st_size) { ! warnx("sudoers file unchanged"); Exit(0); } *************** *** 461,475 **** * we move it to sudoers things are kosher. */ if (chown(stmp, SUDOERS_UID, SUDOERS_GID)) { ! (void) fprintf(stderr, ! "%s: Unable to set (uid, gid) of %s to (%d, %d): %s\n", ! Argv[0], stmp, SUDOERS_UID, SUDOERS_GID, strerror(errno)); Exit(-1); } if (chmod(stmp, SUDOERS_MODE)) { ! (void) fprintf(stderr, ! "%s: Unable to change mode of %s to %o: %s\n", ! Argv[0], stmp, SUDOERS_MODE, strerror(errno)); Exit(-1); } --- 438,449 ---- * we move it to sudoers things are kosher. */ if (chown(stmp, SUDOERS_UID, SUDOERS_GID)) { ! warn("unable to set (uid, gid) of %s to (%d, %d)", ! stmp, SUDOERS_UID, SUDOERS_GID); Exit(-1); } if (chmod(stmp, SUDOERS_MODE)) { ! warn("unable to change mode of %s to 0%o", stmp, SUDOERS_MODE); Exit(-1); } *************** *** 480,488 **** */ if (rename(stmp, sudoers)) { if (errno == EXDEV) { ! (void) fprintf(stderr, ! "%s: %s and %s not on the same filesystem, using mv to rename.\n", ! Argv[0], stmp, sudoers); /* Build up argument vector for the command */ if ((av[0] = strrchr(_PATH_MV, '/')) != NULL) --- 454,461 ---- */ if (rename(stmp, sudoers)) { if (errno == EXDEV) { ! warnx("%s and %s not on the same filesystem, using mv to rename", ! stmp, sudoers); /* Build up argument vector for the command */ if ((av[0] = strrchr(_PATH_MV, '/')) != NULL) *************** *** 495,508 **** /* And run it... */ if (run_command(_PATH_MV, av)) { ! (void) fprintf(stderr, ! "%s: Command failed: '%s %s %s', %s unchanged.\n", ! Argv[0], _PATH_MV, stmp, sudoers, sudoers); Exit(-1); } } else { ! (void) fprintf(stderr, "%s: Error renaming %s, %s unchanged: %s\n", ! Argv[0], stmp, sudoers, strerror(errno)); Exit(-1); } } --- 468,479 ---- /* And run it... */ if (run_command(_PATH_MV, av)) { ! warnx("command failed: '%s %s %s', %s unchanged", ! _PATH_MV, stmp, sudoers, sudoers); Exit(-1); } } else { ! warn("error renaming %s, %s unchanged", stmp, sudoers); Exit(-1); } } *************** *** 637,651 **** switch (pid = fork()) { case -1: ! (void) fprintf(stderr, ! "%s: unable to run %s: %s\n", Argv[0], path, strerror(errno)); Exit(-1); break; /* NOTREACHED */ case 0: (void) sigprocmask(SIG_SETMASK, &oset, NULL); execv(path, argv); ! (void) fprintf(stderr, ! "%s: unable to run %s: %s\n", Argv[0], path, strerror(errno)); _exit(127); break; /* NOTREACHED */ } --- 608,620 ---- switch (pid = fork()) { case -1: ! warn("unable to run %s", path); Exit(-1); break; /* NOTREACHED */ case 0: (void) sigprocmask(SIG_SETMASK, &oset, NULL); execv(path, argv); ! warn("unable to run %s", path); _exit(127); break; /* NOTREACHED */ } *************** *** 669,685 **** if ((yyin = fopen(sudoers, "r")) == NULL) { if (!quiet) ! (void) fprintf(stderr, "%s: unable to open %s: %s\n", Argv[0], ! sudoers, strerror(errno)); exit(1); } yyout = stdout; init_parser(); if (yyparse() && parse_error != TRUE) { if (!quiet) ! (void) fprintf(stderr, ! "%s: failed to parse %s file, unknown error.\n", ! Argv[0], sudoers); parse_error = TRUE; } if (!quiet){ --- 638,651 ---- if ((yyin = fopen(sudoers, "r")) == NULL) { if (!quiet) ! warn("unable to open %s", sudoers); exit(1); } yyout = stdout; init_parser(); if (yyparse() && parse_error != TRUE) { if (!quiet) ! warnx("failed to parse %s file, unknown error", sudoers); parse_error = TRUE; } if (!quiet){ *************** *** 707,713 **** (void) unlink(stmp); if (sig > 0) { ! write(STDERR_FILENO, Argv[0], strlen(Argv[0])); write(STDERR_FILENO, emsg, sizeof(emsg) - 1); _exit(-sig); } --- 673,679 ---- (void) unlink(stmp); if (sig > 0) { ! write(STDERR_FILENO, getprogname(), strlen(getprogname())); write(STDERR_FILENO, emsg, sizeof(emsg) - 1); _exit(-sig); } *************** *** 718,723 **** usage() { (void) fprintf(stderr, "usage: %s [-c] [-f sudoers] [-q] [-s] [-V]\n", ! Argv[0]); exit(1); } --- 684,689 ---- usage() { (void) fprintf(stderr, "usage: %s [-c] [-f sudoers] [-q] [-s] [-V]\n", ! getprogname()); exit(1); }