summaryrefslogtreecommitdiff
path: root/visudo.c
blob: 6e1f251c4e4de2cfd188f69adce98598e986affa (plain)
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
/*
 * CU sudo version 1.3.1 (based on Root Group sudo version 1.1)
 *
 * This software comes with no waranty whatsoever, use at your own risk.
 *
 * Please send bugs, changes, problems to sudo-bugs.cs.colorado.edu
 *
 */

/*
 *  sudo version 1.1 allows users to execute commands as root
 *  Copyright (C) 1991  The Root Group, Inc.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 1, or (at your option)
 *  any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 **************************************************************************
 * visudo.c, sudo project
 * David R. Hieb
 * March 18, 1991
 *
 * edit, lock and parse the sudoers file in a fashion similiar to /etc/vipw.
 */

#ifndef lint
static char rcsid[] = "$Id$";
#endif /* lint */

#include "config.h"

#include <sys/types.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/file.h>
#include <stdio.h>
#ifdef STDC_HEADERS
#include <stdlib.h>
#endif /* STDC_HEADERS */
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif /* HAVE_UNISTD_H */
#include <errno.h>
#include <signal.h>

#include "sudo.h"

#ifndef STDC_HEADERS
extern char *getenv();
#endif /* !STDC_HEADERS */

extern FILE *yyin, *yyout;
extern int errno, yylineno;

char buffer[BUFSIZ];
char *sudoers = _PATH_SUDO_SUDOERS;
int status = 0, err_line_no = 0;
char *sudoers_tmp_file = _PATH_SUDO_STMP;
FILE *sudoers_tmp_fp=NULL, *sudoers_fp=NULL;

RETSIGTYPE Exit()
{
    if (sudoers_tmp_fp)
	(void) fclose(sudoers_tmp_fp);

    (void) unlink(sudoers_tmp_file);
    exit(1);
}


main(argc, argv)
    int argc;
    char **argv;
{
    int fd;
    struct stat sbuf;
#ifdef ENV_EDITOR
    char * Editor;
#endif /* ENV_EDITOR */

    /*
     * handle the signals
     */
    (void) signal(SIGILL, Exit);
    (void) signal(SIGTRAP, Exit);
    (void) signal(SIGBUS, Exit);
    (void) signal(SIGSEGV, Exit);
    (void) signal(SIGTERM, Exit);

    (void) signal(SIGHUP, SIG_IGN);
    (void) signal(SIGINT, SIG_IGN);
    (void) signal(SIGQUIT, SIG_IGN);

    setbuf(stderr, NULL);

    /*
     * we only want root to be able to read/write the sudoers_tmp_file
     */
    umask(077);

#ifdef ENV_EDITOR
    /*
     * set up the Editor variable correctly
     */
    if ( (Editor = getenv("EDITOR")) == NULL)
	if ( (Editor = getenv("VISUAL")) == NULL )
	    Editor = EDITOR;
#endif /* ENV_EDITOR */

    /*
     * open the sudoers file read only
     */
    if ((sudoers_fp = fopen(sudoers, "r")) == NULL) {
	(void) fprintf(stderr, "%s: ", *argv);
	perror(sudoers);
	Exit();
    }

    /*
     * open the temporary sudoers file with the correct flags
     */
    if ((fd = open(sudoers_tmp_file, O_WRONLY | O_CREAT | O_EXCL, 0600)) < 0) {
	if (errno == EEXIST) {
	    (void) fprintf(stderr, "%s: sudoers file busy\n", *argv);
	    exit(1);
	}
	(void) fprintf(stderr, "%s: ", *argv);
	perror(sudoers_tmp_file);
	exit(1);
    }

    /*
     * get a STREAM file pointer to the temporary sudoers file
     */
    if ((sudoers_tmp_fp = fdopen(fd, "w")) == NULL) {
	(void) fprintf(stderr, "%s: ", *argv);
	perror(sudoers_tmp_file);
	Exit();
    }

    /*
     * transfer the contents of the sudoers file to the temporary sudoers file
     */
    while (fgets(buffer, sizeof(buffer) - 1, sudoers_fp) != NULL) {
	fputs(buffer, sudoers_tmp_fp);
    }

    (void) fclose(sudoers_fp);
    (void) fclose(sudoers_tmp_fp);

    do {
	/*
	 * build strings in buffer to be executed by system()
	 */
#ifdef ENV_EDITOR
	(void) sprintf(buffer, "%s +%d %s", Editor, err_line_no,
	    sudoers_tmp_file);
#else
	(void) sprintf(buffer, "%s +%d %s", EDITOR, err_line_no,
	    sudoers_tmp_file);
#endif /* ENV_EDITOR */

	/* edit the file */
	if (system(buffer) == 0) {

	    /* can't stat file */
	    if (stat(sudoers_tmp_file, &sbuf) < 0) {
		(void) fprintf(stderr, "%s: can't stat temporary file, %s unchanged\n",
			sudoers, *argv);
		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;
	    yyout = stdout;

	    /* parse the file */
	    if (yyparse()) {
		(void) fprintf(stderr, "yyparse() failed\n");
		Exit();
	    }

	    /*
	     * the first time we get an error, set status to yylineno which
	     * will be the line number after the line with the error. then,
	     * if we have gotten an error, set err_line_no to the correct
	     * line so that when we edit the file err_line_no will be
	     * correct. at this time we also reset status and yylineno to
	     * their default values so that the next time yyparse() is
	     * called, they will be initialized correctly. 
	     */
	    err_line_no = (status == 0) ? 0 : status - 1;
	    status = 0;
	    yylineno = 1;

	    (void) fclose(sudoers_tmp_fp);
	}
    } while (err_line_no);

    /*
     * once the temporary sudoers file is gramatically correct, we can 
     * rename it to the real sudoers file.
     */
    if (rename(sudoers_tmp_file, sudoers) != 0) {
	(void) fprintf(stderr, "%s: ", *argv);
	perror("rename");
    } else {
	if (chmod(sudoers, 0400) != 0)
	    perror("chmod: failed");
	exit(0);
    }
}