Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sudo-project
GitHub Repository: sudo-project/sudo
Path: blob/main/plugins/sudoers/editor.c
1532 views
1
/*
2
* SPDX-License-Identifier: ISC
3
*
4
* Copyright (c) 2010-2015, 2020-2022 Todd C. Miller <[email protected]>
5
*
6
* Permission to use, copy, modify, and distribute this software for any
7
* purpose with or without fee is hereby granted, provided that the above
8
* copyright notice and this permission notice appear in all copies.
9
*
10
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17
*/
18
19
#include <config.h>
20
21
#include <sys/stat.h>
22
#include <stdio.h>
23
#include <stdlib.h>
24
#include <string.h>
25
#include <unistd.h>
26
#include <errno.h>
27
28
#include <sudoers.h>
29
30
/*
31
* Non-destructive word-split that handles single and double quotes and
32
* escaped white space. Quotes are only recognized at the start of a word.
33
* They are treated as normal characters inside a word.
34
*/
35
static const char *
36
wordsplit(const char *str, const char *endstr, const char **last)
37
{
38
const char *cp;
39
debug_decl(wordsplit, SUDOERS_DEBUG_UTIL);
40
41
/* If no str specified, use last ptr (if any). */
42
if (str == NULL) {
43
str = *last;
44
/* Consume end quote if present. */
45
if (*str == '"' || *str == '\'')
46
str++;
47
}
48
49
/* Skip leading white space characters. */
50
while (str < endstr && (*str == ' ' || *str == '\t'))
51
str++;
52
53
/* Empty string? */
54
if (str >= endstr) {
55
*last = endstr;
56
debug_return_ptr(NULL);
57
}
58
59
/* If word is quoted, skip to end quote and return. */
60
if (*str == '"' || *str == '\'') {
61
const char *endquote;
62
for (cp = str + 1; cp < endstr; cp = endquote + 1) {
63
endquote = memchr(cp, *str, (size_t)(endstr - cp));
64
if (endquote == NULL)
65
break;
66
/* ignore escaped quotes */
67
if (endquote[-1] != '\\') {
68
*last = endquote;
69
debug_return_const_ptr(str + 1);
70
}
71
}
72
}
73
74
/* Scan str until we encounter white space. */
75
for (cp = str; cp < endstr; cp++) {
76
if (*cp == '\\' && cp + 1 < endstr) {
77
/* quoted char, do not interpret */
78
cp++;
79
continue;
80
}
81
if (*cp == ' ' || *cp == '\t') {
82
/* end of word */
83
break;
84
}
85
}
86
*last = cp;
87
debug_return_const_ptr(str);
88
}
89
90
/* Copy len chars from string, collapsing chars escaped with a backslash. */
91
static char *
92
copy_arg(const char *src, size_t len)
93
{
94
const char *src_end = src + len;
95
char *copy, *dst;
96
debug_decl(copy_arg, SUDOERS_DEBUG_UTIL);
97
98
if ((copy = malloc(len + 1)) != NULL) {
99
sudoers_gc_add(GC_PTR, copy);
100
for (dst = copy; src < src_end; ) {
101
if (*src == '\\' && src + 1 < src_end)
102
src++;
103
*dst++ = *src++;
104
}
105
*dst = '\0';
106
}
107
108
debug_return_ptr(copy);
109
}
110
111
/*
112
* Search for the specified editor in the user's PATH, checking
113
* the result against allowlist if non-NULL. An argument vector
114
* suitable for execve() is allocated and stored in argv_out.
115
* If nfiles is non-zero, files[] is added to the end of argv_out.
116
*
117
* Returns the path to be executed on success, else NULL.
118
* The caller is responsible for freeing the returned editor path
119
* as well as the argument vector.
120
*/
121
static char *
122
resolve_editor(const char *ed, size_t edlen, int nfiles, char * const *files,
123
int *argc_out, char ***argv_out, char * const *allowlist)
124
{
125
char **nargv = NULL, *editor = NULL, *editor_path = NULL;
126
const char *tmp, *cp, *ep = NULL;
127
const char *edend = ed + edlen;
128
struct stat user_editor_sb;
129
int nargc = 0;
130
debug_decl(resolve_editor, SUDOERS_DEBUG_UTIL);
131
132
/*
133
* Split editor into an argument vector, including files to edit.
134
* The EDITOR and VISUAL environment variables may contain command
135
* line args so look for those and alloc space for them too.
136
*/
137
cp = wordsplit(ed, edend, &ep);
138
if (cp == NULL)
139
debug_return_str(NULL);
140
editor = copy_arg(cp, (size_t)(ep - cp));
141
if (editor == NULL)
142
goto oom;
143
144
/* If we can't find the editor in the user's PATH, give up. */
145
if (find_path(editor, &editor_path, &user_editor_sb, getenv("PATH"), NULL,
146
false, allowlist) != FOUND) {
147
errno = ENOENT;
148
goto bad;
149
}
150
151
/* Count rest of arguments and allocate editor argv. */
152
for (nargc = 1, tmp = ep; wordsplit(NULL, edend, &tmp) != NULL; )
153
nargc++;
154
if (nfiles != 0)
155
nargc += nfiles + 1;
156
nargv = reallocarray(NULL, (size_t)nargc + 1, sizeof(char *));
157
if (nargv == NULL)
158
goto oom;
159
sudoers_gc_add(GC_PTR, nargv);
160
161
/* Fill in editor argv (assumes files[] is NULL-terminated). */
162
nargv[0] = editor;
163
editor = NULL;
164
for (nargc = 1; (cp = wordsplit(NULL, edend, &ep)) != NULL; nargc++) {
165
/* Copy string, collapsing chars escaped with a backslash. */
166
nargv[nargc] = copy_arg(cp, (size_t)(ep - cp));
167
if (nargv[nargc] == NULL)
168
goto oom;
169
170
/*
171
* We use "--" to separate the editor and arguments from the files
172
* to edit. The editor arguments themselves may not contain "--".
173
*/
174
if (strcmp(nargv[nargc], "--") == 0) {
175
sudo_warnx(U_("ignoring editor: %.*s"), (int)edlen, ed);
176
sudo_warnx("%s", U_("editor arguments may not contain \"--\""));
177
errno = EINVAL;
178
goto bad;
179
}
180
}
181
if (nfiles != 0) {
182
nargv[nargc++] = (char *)"--";
183
do
184
nargv[nargc++] = *files++;
185
while (--nfiles > 0);
186
}
187
nargv[nargc] = NULL;
188
189
*argc_out = nargc;
190
*argv_out = nargv;
191
debug_return_str(editor_path);
192
oom:
193
sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
194
bad:
195
sudoers_gc_remove(GC_PTR, editor);
196
free(editor);
197
free(editor_path);
198
if (nargv != NULL) {
199
while (nargc > 0) {
200
sudoers_gc_remove(GC_PTR, nargv[--nargc]);
201
free(nargv[nargc]);
202
}
203
sudoers_gc_remove(GC_PTR, nargv);
204
free(nargv);
205
}
206
debug_return_str(NULL);
207
}
208
209
/*
210
* Determine which editor to use based on the SUDO_EDITOR, VISUAL and
211
* EDITOR environment variables as well as the editor path in sudoers.
212
*
213
* Returns the path to be executed on success, else NULL.
214
* The caller is responsible for freeing the returned editor path
215
* as well as the argument vector.
216
*/
217
char *
218
find_editor(int nfiles, char * const *files, int *argc_out, char ***argv_out,
219
char * const *allowlist, const char **env_editor)
220
{
221
char *editor_path = NULL;
222
const char *ev[3];
223
size_t i;
224
debug_decl(find_editor, SUDOERS_DEBUG_UTIL);
225
226
/*
227
* If any of SUDO_EDITOR, VISUAL or EDITOR are set, choose the first one.
228
*/
229
*env_editor = NULL;
230
ev[0] = "SUDO_EDITOR";
231
ev[1] = "VISUAL";
232
ev[2] = "EDITOR";
233
for (i = 0; i < nitems(ev); i++) {
234
char *editor = getenv(ev[i]);
235
236
if (editor != NULL && *editor != '\0') {
237
*env_editor = editor;
238
editor_path = resolve_editor(editor, strlen(editor),
239
nfiles, files, argc_out, argv_out, allowlist);
240
if (editor_path != NULL)
241
break;
242
if (errno != ENOENT)
243
debug_return_str(NULL);
244
}
245
}
246
247
/*
248
* If SUDO_EDITOR, VISUAL and EDITOR were either not set or not
249
* allowed (based on the values of def_editor and def_env_editor),
250
* choose the first one in def_editor that exists.
251
*/
252
if (editor_path == NULL) {
253
const char *def_editor_end = def_editor + strlen(def_editor);
254
const char *cp, *ep;
255
256
/* def_editor could be a path, split it up, avoiding strtok() */
257
for (cp = sudo_strsplit(def_editor, def_editor_end, ":", &ep);
258
cp != NULL; cp = sudo_strsplit(NULL, def_editor_end, ":", &ep)) {
259
editor_path = resolve_editor(cp, (size_t)(ep - cp), nfiles,
260
files, argc_out, argv_out, allowlist);
261
if (editor_path != NULL)
262
break;
263
if (errno != ENOENT)
264
debug_return_str(NULL);
265
}
266
}
267
268
/* Caller is responsible for freeing editor_path, not g/c'd. */
269
debug_return_str(editor_path);
270
}
271
272