Skip to content

Commit 2547c50

Browse files
committed
edit_sudoers: Return false on failure instead of using sudo_fatal()
Callers of edit_sudoers() now check the return value.
1 parent 74626d8 commit 2547c50

File tree

1 file changed

+45
-22
lines changed

1 file changed

+45
-22
lines changed

plugins/sudoers/visudo.c

Lines changed: 45 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,8 @@ main(int argc, char *argv[])
304304
while ((ch = getchar()) != EOF && ch != '\r' && ch != '\n')
305305
continue;
306306
}
307-
edit_sudoers(sp, editor, editor_argc, editor_argv, -1);
307+
if (!edit_sudoers(sp, editor, editor_argc, editor_argv, -1))
308+
exitcode = 1;
308309
}
309310

310311
/*
@@ -323,6 +324,9 @@ main(int argc, char *argv[])
323324
exitcode = 1;
324325
}
325326
}
327+
} else {
328+
/* Remove temporary files. */
329+
visudo_cleanup();
326330
}
327331
free(editor);
328332

@@ -485,18 +489,24 @@ edit_sudoers(struct sudoersfile *sp, char *editor, int editor_argc,
485489
bool ret = false; /* return value */
486490
debug_decl(edit_sudoers, SUDOERS_DEBUG_UTIL);
487491

488-
if (fstat(sp->fd, &sb) == -1)
489-
sudo_fatal(U_("unable to stat %s"), sp->opath);
492+
if (fstat(sp->fd, &sb) == -1) {
493+
sudo_warn(U_("unable to stat %s"), sp->opath);
494+
goto done;
495+
}
490496
orig_size = sb.st_size;
491497
mtim_get(&sb, orig_mtim);
492498

493499
/* Create the temp file if needed and set timestamp. */
494500
if (sp->tpath == NULL) {
495-
if (asprintf(&sp->tpath, "%s.tmp", sp->dpath) == -1)
496-
sudo_fatalx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
501+
if (asprintf(&sp->tpath, "%s.tmp", sp->dpath) == -1) {
502+
sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
503+
goto done;
504+
}
497505
tfd = open(sp->tpath, O_WRONLY|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR);
498-
if (tfd < 0)
499-
sudo_fatal("%s", sp->tpath);
506+
if (tfd < 0) {
507+
sudo_warn("%s", sp->tpath);
508+
goto done;
509+
}
500510

501511
/* Copy sp->opath -> sp->tpath and reset the mtime. */
502512
if (orig_size != 0) {
@@ -505,16 +515,20 @@ edit_sudoers(struct sudoersfile *sp, char *editor, int editor_argc,
505515

506516
(void) lseek(sp->fd, (off_t)0, SEEK_SET);
507517
while ((nread = read(sp->fd, buf, sizeof(buf))) > 0) {
508-
if (write(tfd, buf, (size_t)nread) != nread)
509-
sudo_fatal("%s", U_("write error"));
518+
if (write(tfd, buf, (size_t)nread) != nread) {
519+
sudo_warn("%s", U_("write error"));
520+
goto done;
521+
}
510522
lastch = buf[nread - 1];
511523
}
512524

513525
/* Add missing newline at EOF if needed. */
514526
if (lastch != '\n') {
515527
lastch = '\n';
516-
if (write(tfd, &lastch, 1) != 1)
517-
sudo_fatal("%s", U_("write error"));
528+
if (write(tfd, &lastch, 1) != 1) {
529+
sudo_warn("%s", U_("write error"));
530+
goto done;
531+
}
518532
}
519533
}
520534
(void) close(tfd);
@@ -642,6 +656,7 @@ reparse_sudoers(struct sudoers_context *ctx, char *editor, int editor_argc,
642656
{
643657
struct sudoersfile *sp, *last;
644658
FILE *fp;
659+
bool ret = false;
645660
int ch, oldlocale;
646661
debug_decl(reparse_sudoers, SUDOERS_DEBUG_UTIL);
647662

@@ -652,13 +667,17 @@ reparse_sudoers(struct sudoers_context *ctx, char *editor, int editor_argc,
652667
while ((sp = TAILQ_FIRST(&sudoerslist)) != NULL) {
653668
last = TAILQ_LAST(&sudoerslist, sudoersfile_list);
654669
fp = fopen(sp->tpath, "r+");
655-
if (fp == NULL)
656-
sudo_fatalx(U_("unable to re-open temporary file (%s), %s unchanged."),
670+
if (fp == NULL) {
671+
sudo_warnx(U_("unable to re-open temporary file (%s), %s unchanged."),
657672
sp->tpath, sp->opath);
673+
goto done;
674+
}
658675

659676
/* Clean slate for each parse */
660-
if (!init_defaults())
661-
sudo_fatalx("%s", U_("unable to initialize sudoers default values"));
677+
if (!init_defaults()) {
678+
sudo_warnx("%s", U_("unable to initialize sudoers default values"));
679+
goto done;
680+
}
662681
init_parser(ctx, sp->opath);
663682
sp->errorline = -1;
664683

@@ -687,15 +706,16 @@ reparse_sudoers(struct sudoers_context *ctx, char *editor, int editor_argc,
687706
parse_error = false; /* ignore parse error */
688707
break;
689708
case 'x':
690-
visudo_cleanup(); /* discard changes */
691-
debug_return_bool(false);
709+
goto done; /* discard changes */
692710
case 'e':
693711
default:
694712
/* Edit file with the parse error */
695713
TAILQ_FOREACH(sp, &sudoerslist, entries) {
696714
if (errors == 0 || sp->errorline > 0) {
697-
edit_sudoers(sp, editor, editor_argc, editor_argv,
698-
sp->errorline);
715+
if (!edit_sudoers(sp, editor, editor_argc, editor_argv,
716+
sp->errorline)) {
717+
goto done;
718+
}
699719
}
700720
}
701721
break;
@@ -708,8 +728,9 @@ reparse_sudoers(struct sudoers_context *ctx, char *editor, int editor_argc,
708728
do {
709729
printf(_("press return to edit %s: "), sp->opath);
710730
while ((ch = getchar()) != EOF && ch != '\r' && ch != '\n')
711-
continue;
712-
edit_sudoers(sp, editor, editor_argc, editor_argv, -1);
731+
continue;
732+
if (!edit_sudoers(sp, editor, editor_argc, editor_argv, -1))
733+
goto done;
713734
if (sp->modified)
714735
modified = true;
715736
} while ((sp = TAILQ_NEXT(sp, entries)) != NULL);
@@ -723,8 +744,10 @@ reparse_sudoers(struct sudoers_context *ctx, char *editor, int editor_argc,
723744
if (!parse_error)
724745
break;
725746
}
747+
ret = true;
726748

727-
debug_return_bool(true);
749+
done:
750+
debug_return_bool(ret);
728751
}
729752

730753
/*

0 commit comments

Comments
 (0)