reformatted with indent + by hand

This commit is contained in:
Todd C. Miller
1993-10-04 19:23:33 +00:00
parent e75a6d3b54
commit f7abfbf818

122
visudo.c
View File

@@ -22,13 +22,14 @@
* 3959 Arbol CT (303) 447-8093 * 3959 Arbol CT (303) 447-8093
* Boulder, CO 80301-1752 * Boulder, CO 80301-1752
* *
******************************************************************************** **************************************************************************
* visudo.c, sudo project * visudo.c, sudo project
* David R. Hieb * David R. Hieb
* March 18, 1991 * March 18, 1991
* *
* edit, lock and parse the sudoers file in a fashion similiar to /etc/vipw. * edit, lock and parse the sudoers file in a fashion similiar to /etc/vipw.
*******************************************************************************/ */
#include <sys/types.h> #include <sys/types.h>
#include <sys/param.h> #include <sys/param.h>
#include <sys/stat.h> #include <sys/stat.h>
@@ -50,11 +51,12 @@ FILE *sudoers_tmp_fp, *sudoers_fp;
void Exit() void Exit()
{ {
fclose(sudoers_tmp_fp); (void) fclose(sudoers_tmp_fp);
unlink(sudoers_tmp_file); (void) unlink(sudoers_tmp_file);
exit(1); exit(1);
} }
main(argc, argv) main(argc, argv)
int argc; int argc;
char **argv; char **argv;
@@ -62,117 +64,133 @@ char **argv;
int fd; int fd;
struct stat sbuf; struct stat sbuf;
/* handle the signals */ /*
signal(SIGILL, Exit); * handle the signals
signal(SIGTRAP, Exit); */
signal(SIGBUS, Exit); (void) signal(SIGILL, Exit);
signal(SIGSEGV, Exit); (void) signal(SIGTRAP, Exit);
signal(SIGTERM, Exit); (void) signal(SIGBUS, Exit);
(void) signal(SIGSEGV, Exit);
(void) signal(SIGTERM, Exit);
signal(SIGHUP, SIG_IGN); (void) signal(SIGHUP, SIG_IGN);
signal(SIGINT, SIG_IGN); (void) signal(SIGINT, SIG_IGN);
signal(SIGQUIT, SIG_IGN); (void) signal(SIGQUIT, SIG_IGN);
setbuf(stderr, NULL); setbuf(stderr, NULL);
/* we only want root to be able to read/write the sudoers_tmp_file */ /*
* we only want root to be able to read/write the sudoers_tmp_file
*/
umask(077); umask(077);
/* open the sudoers file read only */ /*
* open the sudoers file read only
*/
if ((sudoers_fp = fopen(sudoers, "r")) == NULL) { if ((sudoers_fp = fopen(sudoers, "r")) == NULL) {
fprintf(stderr, "%s: ", *argv); (void) fprintf(stderr, "%s: ", *argv);
perror(sudoers); perror(sudoers);
Exit(); Exit();
} }
/* open the temporary sudoers file with the correct flags */ /*
* open the temporary sudoers file with the correct flags
*/
if ((fd = open(sudoers_tmp_file, O_WRONLY | O_CREAT | O_EXCL, 0600)) < 0) { if ((fd = open(sudoers_tmp_file, O_WRONLY | O_CREAT | O_EXCL, 0600)) < 0) {
if (errno == EEXIST) { if (errno == EEXIST) {
fprintf(stderr, "%s: sudoers file busy\n", *argv); (void) fprintf(stderr, "%s: sudoers file busy\n", *argv);
exit(1); exit(1);
} }
fprintf(stderr, "%s: ", *argv); (void) fprintf(stderr, "%s: ", *argv);
perror(sudoers_tmp_file); perror(sudoers_tmp_file);
exit(1); exit(1);
} }
/* get a STREAM file pointer to the temporary sudoers file */ /*
* get a STREAM file pointer to the temporary sudoers file
*/
if ((sudoers_tmp_fp = fdopen(fd, "w")) == NULL) { if ((sudoers_tmp_fp = fdopen(fd, "w")) == NULL) {
fprintf(stderr, "%s: ", *argv); (void) fprintf(stderr, "%s: ", *argv);
perror(sudoers_tmp_file); perror(sudoers_tmp_file);
Exit(); Exit();
} }
/* transfer the contents of the sudoers file to the temporary sudoers file */ /*
* transfer the contents of the sudoers file to the temporary sudoers file
*/
while (fgets(buffer, sizeof(buffer) - 1, sudoers_fp) != NULL) { while (fgets(buffer, sizeof(buffer) - 1, sudoers_fp) != NULL) {
fputs(buffer, sudoers_tmp_fp); fputs(buffer, sudoers_tmp_fp);
} }
fclose(sudoers_fp); (void) fclose(sudoers_fp);
fclose(sudoers_tmp_fp); (void) fclose(sudoers_tmp_fp);
do { do {
/* build strings in buffer to be executed by system() */ /*
sprintf(buffer, "%s +%d %s", EDITOR, err_line_no, sudoers_tmp_file); * build strings in buffer to be executed by system()
*/
(void) sprintf(buffer, "%s +%d %s", EDITOR, err_line_no,
sudoers_tmp_file);
/* edit the file */ /* edit the file */
if (system(buffer) == 0) { if (system(buffer) == 0) {
/* can't stat file */ /* can't stat file */
if (stat(sudoers_tmp_file, &sbuf) < 0) { if (stat(sudoers_tmp_file, &sbuf) < 0) {
fprintf(stderr, "%s: can't stat temporary file, %s unchanged\n", (void) fprintf(stderr, "%s: can't stat temporary file, %s unchanged\n",
sudoers, *argv);
Exit();
}
/* file has size == 0 */
if (sbuf.st_size == 0) {
fprintf(stderr, "%s: bad temporary file, %s unchanged\n",
sudoers, *argv);
Exit();
}
/* re-open the sudoers file for parsing */
if ((sudoers_tmp_fp = fopen(sudoers_tmp_file, "r")) == NULL) {
fprintf(stderr, "%s: can't re-open temporary file, %s unchanged\n",
sudoers, *argv); sudoers, *argv);
Exit(); Exit();
} }
/* file has size == 0 */
if (sbuf.st_size == 0) {
(void) fprintf(stderr, "%s: bad temporary file, %s unchanged\n",
sudoers, *argv);
Exit();
}
/* re-open the sudoers file for parsing */
if ((sudoers_tmp_fp = fopen(sudoers_tmp_file, "r")) == NULL) {
(void) fprintf(stderr, "%s: can't re-open temporary file, %s unchanged\n",
sudoers, *argv);
Exit();
}
yyin = sudoers_tmp_fp; yyin = sudoers_tmp_fp;
yyout = stdout; yyout = stdout;
/* parse the file */ /* parse the file */
if (yyparse()) { if (yyparse()) {
fprintf(stderr, "yyparse() failed\n"); (void) fprintf(stderr, "yyparse() failed\n");
Exit(); Exit();
} }
/* /*
* the first time we get an error, set status to yylineno which * the first time we get an error, set status to yylineno which
* will be the line number after the line with the error. * will be the line number after the line with the error. then,
* then, if we have gotten an error, set err_line_no to the * if we have gotten an error, set err_line_no to the correct
* correct line so that when we edit the file err_line_no will * line so that when we edit the file err_line_no will be
* be correct. at this time we also reset status and yylineno * correct. at this time we also reset status and yylineno to
* to their default values so that the next time yyparse() is * their default values so that the next time yyparse() is
* called, they will be initialized correctly. * called, they will be initialized correctly.
*/ */
err_line_no = (status == 0) ? 0 : status - 1; err_line_no = (status == 0) ? 0 : status - 1;
status = 0; status = 0;
yylineno = 1; yylineno = 1;
fclose(sudoers_tmp_fp); (void) fclose(sudoers_tmp_fp);
} }
} while (err_line_no); } while (err_line_no);
/* once the temporary sudoers file is gramatically correct, we can /*
* once the temporary sudoers file is gramatically correct, we can
* rename it to the real sudoers file. * rename it to the real sudoers file.
*/ */
if (rename(sudoers_tmp_file, sudoers) != 0) { if (rename(sudoers_tmp_file, sudoers) != 0) {
fprintf(stderr, "%s: ", *argv), perror("rename"); (void) fprintf(stderr, "%s: ", *argv);
} perror("rename");
else { } else {
if (chmod(sudoers, 0400) != 0) { if (chmod(sudoers, 0400) != 0)
perror("chmod: failed"); perror("chmod: failed");
}
exit(0); exit(0);
} }
} }