1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
|
/*
* Copyright (c) 1996, 1998-2001 Todd C. Miller <Todd.Miller@courtesan.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* 4. Products derived from this software may not be called "Sudo" nor
* may "Sudo" appear in their names without specific prior written
* permission from the author.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* Lock the sudoers file for safe editing (ala vipw) and check for parse errors.
*/
#include "config.h"
#include <sys/types.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/file.h>
#include <sys/wait.h>
#include <stdio.h>
#ifdef STDC_HEADERS
# include <stdlib.h>
# include <stddef.h>
#else
# ifdef HAVE_STDLIB_H
# include <stdlib.h>
# endif
#endif /* STDC_HEADERS */
#ifdef HAVE_STRING_H
# include <string.h>
#else
# ifdef HAVE_STRINGS_H
# include <strings.h>
# endif
#endif /* HAVE_STRING_H */
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif /* HAVE_UNISTD_H */
#include <ctype.h>
#include <pwd.h>
#include <time.h>
#include <signal.h>
#include <errno.h>
#include <fcntl.h>
#include "sudo.h"
#include "version.h"
#ifndef lint
static const char rcsid[] = "$Sudo: visudo.c,v 1.145 2001/12/14 19:52:48 millert Exp $";
#endif /* lint */
/*
* Function prototypes
*/
static void usage __P((void));
static char whatnow __P((void));
static RETSIGTYPE Exit __P((int));
static void setup_signals __P((void));
static int run_command __P((char *, char **));
static int check_syntax __P((int));
int command_matches __P((char *, char *, char *, char *));
int addr_matches __P((char *));
int hostname_matches __P((char *, char *, char *));
int netgr_matches __P((char *, char *, char *, char *));
int usergr_matches __P((char *, char *));
void init_parser __P((void));
void yyrestart __P((FILE *));
/*
* External globals exported by the parser
*/
extern FILE *yyin, *yyout;
extern int errorlineno;
extern int pedantic;
extern int quiet;
/* For getopt(3) */
extern char *optarg;
extern int optind;
/*
* Globals
*/
char **Argv;
char *sudoers = _PATH_SUDOERS;
char *stmp = _PATH_SUDOERS_TMP;
struct sudo_user sudo_user;
int parse_error = FALSE;
int
main(argc, argv)
int argc;
char **argv;
{
char buf[MAXPATHLEN*2]; /* buffer used for copying files */
char *Editor; /* editor to use */
char *UserEditor; /* editor user wants to use */
char *EditorPath; /* colon-separated list of editors */
char *av[4]; /* argument vector for run_command */
int checkonly; /* only check existing file? */
int sudoers_fd; /* sudoers file descriptor */
int stmp_fd; /* stmp file descriptor */
int n; /* length parameter */
int ch; /* getopt char */
time_t now; /* time now */
struct stat stmp_sb, sudoers_sb; /* to check for changes */
/* Warn about aliases that are used before being defined. */
pedantic = 1;
/*
* Parse command line options
*/
Argv = argv;
/*
* Arg handling.
*/
checkonly = 0;
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 */
break;
case 'f':
sudoers = optarg; /* sudoers file path */
easprintf(&stmp, "%s.tmp", optarg);
break;
case 's':
pedantic++; /* strict mode */
break;
case 'q':
quiet++; /* quiet mode */
break;
default:
usage();
}
}
argc -= optind;
argv += optind;
if (argc)
usage();
/* 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();
if (checkonly)
exit(check_syntax(quiet));
/*
* Open sudoers, lock it and stat it.
* 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();
/* 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);
}
(void) close(stmp_fd);
(void) touch(stmp, sudoers_sb.st_mtime);
/* Parse sudoers to pull in editor and env_editor conf values. */
if ((yyin = fopen(stmp, "r"))) {
yyout = stdout;
init_parser();
yyparse();
parse_error = FALSE;
yyrestart(yyin);
fclose(yyin);
}
} else
(void) close(stmp_fd);
/*
* Check EDITOR and VISUAL environment variables to see which editor
* the user wants to use (we may not end up using it though).
* If the path is not fully-qualified, make it so and check that
* the specified executable actually exists.
*/
if ((UserEditor = getenv("EDITOR")) == NULL || *UserEditor == '\0')
UserEditor = getenv("VISUAL");
if (UserEditor && *UserEditor == '\0')
UserEditor = NULL;
else if (UserEditor) {
if (find_path(UserEditor, &Editor, getenv("PATH")) == FOUND) {
UserEditor = Editor;
} 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. */
UserEditor = NULL;
}
}
}
/*
* See if we can use the user's choice of editors either because
* we allow any $EDITOR or because $EDITOR is in the allowable list.
*/
Editor = NULL;
if (def_flag(I_ENV_EDITOR) && UserEditor)
Editor = UserEditor;
else if (UserEditor) {
struct stat editor_sb;
struct stat user_editor_sb;
char *base, *userbase;
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));
Editor = strtok(EditorPath, ":");
do {
/*
* Both Editor and UserEditor should be fully qualified but
* check anyway...
*/
if ((base = strrchr(Editor, '/')) == NULL)
continue;
if ((userbase = strrchr(UserEditor, '/')) == NULL) {
Editor = NULL;
break;
}
base++, userbase++;
/*
* We compare the basenames first and then use stat to match
* for sure.
*/
if (strcmp(base, userbase) == 0) {
if (stat(Editor, &editor_sb) == 0 && S_ISREG(editor_sb.st_mode)
&& (editor_sb.st_mode & 0000111) &&
editor_sb.st_dev == user_editor_sb.st_dev &&
editor_sb.st_ino == user_editor_sb.st_ino)
break;
}
} while ((Editor = strtok(NULL, ":")));
free(EditorPath);
}
/*
* Can't use $EDITOR, try each element of I_EDITOR until we
* find one that exists, is regular, and is executable.
*/
if (Editor == NULL || *Editor == '\0') {
EditorPath = estrdup(def_str(I_EDITOR));
Editor = strtok(EditorPath, ":");
do {
if (sudo_goodpath(Editor))
break;
} while ((Editor = strtok(NULL, ":")));
/* 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);
}
}
/*
* Edit the temp file and parse it (for sanity checking)
*/
do {
char linestr[64];
/* Build up argument vector for the command */
if ((av[0] = strrchr(Editor, '/')) != NULL)
av[0]++;
else
av[0] = Editor;
n = 1;
if (parse_error == TRUE) {
(void) snprintf(linestr, sizeof(linestr), "+%d", errorlineno);
av[n++] = linestr;
}
av[n++] = stmp;
av[n++] = NULL;
/*
* Do the edit:
* We cannot check the editor's exit value against 0 since
* XPG4 specifies that vi's exit value is a function of the
* number of errors during editing (?!?!).
*/
now = time(NULL);
if (run_command(Editor, av) != -1) {
/*
* 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);
}
/*
* Passed sanity checks so reopen stmp file and check
* for parse errors.
*/
yyout = stdout;
if (parse_error)
yyin = freopen(stmp, "r", yyin);
else
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);
}
/* Clean slate for each parse */
user_runas = NULL;
init_defaults();
init_parser();
/* Parse the sudoers file */
if (yyparse() && parse_error != TRUE) {
(void) fprintf(stderr,
"%s: Failed to parse temporary file (%s), unknown error.\n",
Argv[0], stmp);
parse_error = TRUE;
}
} else {
(void) fprintf(stderr,
"%s: Editor (%s) failed, %s unchanged.\n", Argv[0],
Editor, sudoers);
Exit(-1);
}
/*
* Got an error, prompt the user for what to do now
*/
if (parse_error == TRUE) {
switch (whatnow()) {
case 'Q' : parse_error = FALSE; /* ignore parse error */
break;
case 'x' : if (sudoers_sb.st_size == 0)
unlink(sudoers);
Exit(0);
break;
}
yyrestart(yyin); /* reset lexer */
}
} while (parse_error == TRUE);
/*
* If the user didn't change the temp file, just unlink it.
*/
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);
}
/*
* Change mode and ownership of temp file so when
* 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);
}
/*
* Now that we have a sane stmp file (parses ok) it needs to be
* rename(2)'d to sudoers. If the rename(2) fails we try using
* mv(1) in case stmp and sudoers are on different filesystems.
*/
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)
av[0]++;
else
av[0] = _PATH_MV;
av[1] = stmp;
av[2] = sudoers;
av[3] = NULL;
/* 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);
}
}
exit(0);
}
/*
* Dummy *_matches routines.
* These exist to allow us to use the same parser as sudo(8).
*/
int
command_matches(cmnd, cmnd_args, path, sudoers_args)
char *cmnd;
char *cmnd_args;
char *path;
char *sudoers_args;
{
return(TRUE);
}
int
addr_matches(n)
char *n;
{
return(TRUE);
}
int
hostname_matches(s, l, p)
char *s, *l, *p;
{
return(TRUE);
}
int
usergr_matches(g, u)
char *g, *u;
{
return(TRUE);
}
int
netgr_matches(n, h, sh, u)
char *n, *h, *sh, *u;
{
return(TRUE);
}
void
set_fqdn()
{
return;
}
int
user_is_exempt()
{
return(TRUE);
}
void
init_envtables()
{
return;
}
/*
* Assuming a parse error occurred, prompt the user for what they want
* to do now. Returns the first letter of their choice.
*/
static char
whatnow()
{
int choice, c;
for (;;) {
(void) fputs("What now? ", stdout);
choice = getchar();
for (c = choice; c != '\n' && c != EOF;)
c = getchar();
switch (choice) {
case EOF:
choice = 'x';
/* FALLTHROUGH */
case 'e':
case 'x':
case 'Q':
return(choice);
default:
(void) puts("Options are:");
(void) puts(" (e)dit sudoers file again");
(void) puts(" e(x)it without saving changes to sudoers file");
(void) puts(" (Q)uit and save changes to sudoers file (DANGER!)\n");
}
}
}
/*
* Install signal handlers for visudo.
*/
static void
setup_signals()
{
sigaction_t sa;
/*
* Setup signal handlers to cleanup nicely.
*/
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_RESTART;
sa.sa_handler = Exit;
(void) sigaction(SIGTERM, &sa, NULL);
(void) sigaction(SIGHUP, &sa, NULL);
(void) sigaction(SIGINT, &sa, NULL);
(void) sigaction(SIGQUIT, &sa, NULL);
}
static int
run_command(path, argv)
char *path;
char **argv;
{
int status;
pid_t pid;
sigset_t set, oset;
(void) sigemptyset(&set);
(void) sigaddset(&set, SIGCHLD);
(void) sigprocmask(SIG_BLOCK, &set, &oset);
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 */
}
#ifdef sudo_waitpid
pid = sudo_waitpid(pid, &status, 0);
#else
pid = wait(&status);
#endif
(void) sigprocmask(SIG_SETMASK, &oset, NULL);
/* XXX - should use WEXITSTATUS() */
return(pid == -1 ? -1 : (status >> 8));
}
static int
check_syntax(quiet)
int quiet;
{
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){
if (parse_error)
(void) printf("parse error in %s near line %d\n", sudoers,
errorlineno);
else
(void) printf("%s file parsed OK\n", sudoers);
}
return(parse_error == TRUE);
}
/*
* Unlink the sudoers temp file (if it exists) and exit.
* Used in place of a normal exit() and as a signal handler.
* A positive parameter indicates we were called as a signal handler.
*/
static RETSIGTYPE
Exit(sig)
int sig;
{
char *emsg = " exiting due to signal.\n";
(void) unlink(stmp);
if (sig > 0) {
write(STDERR_FILENO, Argv[0], strlen(Argv[0]));
write(STDERR_FILENO, emsg, sizeof(emsg) - 1);
_exit(-sig);
}
exit(-sig);
}
static void
usage()
{
(void) fprintf(stderr, "usage: %s [-c] [-f sudoers] [-q] [-s] [-V]\n",
Argv[0]);
exit(1);
}
|