/src/httrack/src/htsusercmd.c
Line | Count | Source |
1 | | /* ------------------------------------------------------------ */ |
2 | | /* |
3 | | HTTrack Website Copier, Offline Browser for Windows and Unix |
4 | | Copyright (C) 1998 Xavier Roche and other contributors |
5 | | |
6 | | SPDX-License-Identifier: GPL-3.0-or-later |
7 | | |
8 | | This program is free software: you can redistribute it and/or modify |
9 | | it under the terms of the GNU General Public License as published by |
10 | | the Free Software Foundation, either version 3 of the License, or |
11 | | (at your option) any later version. |
12 | | |
13 | | This program is distributed in the hope that it will be useful, |
14 | | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | | GNU General Public License for more details. |
17 | | |
18 | | You should have received a copy of the GNU General Public License |
19 | | along with this program. If not, see <http://www.gnu.org/licenses/>. |
20 | | |
21 | | Ethical use: we kindly ask that you NOT use this software to harvest email |
22 | | addresses or to collect any other private information about people. Doing so |
23 | | would dishonor our work and waste the many hours we have spent on it. |
24 | | |
25 | | Please visit our Website: http://www.httrack.com |
26 | | */ |
27 | | |
28 | | /* ------------------------------------------------------------ */ |
29 | | /* File: -V user command runner */ |
30 | | /* Author: Xavier Roche */ |
31 | | /* ------------------------------------------------------------ */ |
32 | | |
33 | | #include "htscore.h" |
34 | | |
35 | | /* after htscore.h, which is what pulls in config.h and so HAVE_PATHS_H */ |
36 | | #ifndef _WIN32 |
37 | | #include <sys/wait.h> |
38 | | #ifdef HAVE_PATHS_H |
39 | | #include <paths.h> |
40 | | #endif |
41 | | /* bionic has no /bin/sh, so take the system's own answer where it gives one. */ |
42 | | #ifndef _PATH_BSHELL |
43 | | #define _PATH_BSHELL "/bin/sh" |
44 | | #endif |
45 | | #endif |
46 | | |
47 | | /* Shell quoting in force at the point of the template where "$0" appears. */ |
48 | | typedef enum { |
49 | | SHELL_CTX_PLAIN, /* not inside quotes */ |
50 | | SHELL_CTX_SINGLE, /* inside '...' */ |
51 | | SHELL_CTX_DOUBLE, /* inside "..." */ |
52 | | SHELL_CTX_OPAQUE /* a region whose text the shell reads again, so quotes are |
53 | | ordinary characters in it */ |
54 | | } shell_ctx_t; |
55 | | |
56 | | /* |
57 | | * A command substitution -- $( ) or `` -- starts a fresh shell, so the quoting |
58 | | * outside it says nothing about the quoting inside: in |
59 | | * printf "[%s]" "$(printf %s '$0')" |
60 | | * the $0 sits inside '...', not the "..." that encloses the substitution. The |
61 | | * scanner keeps a stack: opening one saves the quoting it interrupts and |
62 | | * restarts at PLAIN, closing it restores what was saved. |
63 | | */ |
64 | | typedef struct { |
65 | | shell_ctx_t ctx; /* quoting the region interrupted */ |
66 | | char closer; /* ')', ']', '}', or '`' for a backquote */ |
67 | | unsigned int open; /* openers not yet matched, including this one */ |
68 | | } shell_frame_t; |
69 | | |
70 | | /* A template nested deeper than this is refused rather than mis-tracked. */ |
71 | 0 | #define SHELL_CTX_MAX_DEPTH 16 |
72 | | |
73 | | /* The opener that matches a region's closer. */ |
74 | 0 | static char usercommand_opener(char closer) { |
75 | 0 | switch (closer) { |
76 | 0 | case ')': |
77 | 0 | return '('; |
78 | 0 | case ']': |
79 | 0 | return '['; |
80 | 0 | case '}': |
81 | 0 | return '{'; |
82 | 0 | } |
83 | 0 | return '\0'; |
84 | 0 | } |
85 | | |
86 | | /* Open a region ending at "closer"; HTS_FALSE when the stack is full. */ |
87 | | static hts_boolean usercommand_push(shell_frame_t *stack, size_t *depth, |
88 | | shell_ctx_t ctx, char closer, |
89 | 0 | unsigned int open) { |
90 | 0 | if (*depth == SHELL_CTX_MAX_DEPTH) |
91 | 0 | return HTS_FALSE; |
92 | 0 | stack[*depth].ctx = ctx; |
93 | 0 | stack[*depth].closer = closer; |
94 | 0 | stack[*depth].open = open; |
95 | 0 | (*depth)++; |
96 | 0 | return HTS_TRUE; |
97 | 0 | } |
98 | | |
99 | | /* Appends src at *pos in dest and keeps it NUL-terminated. |
100 | | Returns HTS_FALSE and parks *pos at dest.size when it does not fit. */ |
101 | 0 | static hts_boolean usercommand_append(hts_buf dest, size_t *pos, hts_span src) { |
102 | 0 | if (*pos >= dest.size || src.len >= dest.size - *pos) { |
103 | 0 | *pos = dest.size; |
104 | 0 | return HTS_FALSE; |
105 | 0 | } |
106 | 0 | memcpy(dest.p + *pos, src.p, src.len); |
107 | 0 | *pos += src.len; |
108 | 0 | dest.p[*pos] = '\0'; |
109 | 0 | return HTS_TRUE; |
110 | 0 | } |
111 | | |
112 | 0 | static hts_boolean usercommand_append_char(hts_buf dest, size_t *pos, char c) { |
113 | 0 | return usercommand_append(dest, pos, hts_span_at(&c, 1)); |
114 | 0 | } |
115 | | |
116 | | #ifndef _WIN32 |
117 | | /* |
118 | | * The spelling of the shell's first positional parameter that reads as one |
119 | | * literal word in "ctx", or NULL where no spelling does. The save name comes |
120 | | * from the crawled URL and htsname.c leaves ; ` $ & ' ( ) and spaces in it, |
121 | | * so the name is handed to the shell as $1 and never written into the command. |
122 | | */ |
123 | 0 | static const char *usercommand_param(shell_ctx_t ctx) { |
124 | 0 | switch (ctx) { |
125 | 0 | case SHELL_CTX_PLAIN: |
126 | 0 | return "\"${1}\""; |
127 | 0 | case SHELL_CTX_DOUBLE: |
128 | | /* already between quotes; braces so a digit after $0 is not read as $11 */ |
129 | 0 | return "${1}"; |
130 | 0 | case SHELL_CTX_SINGLE: |
131 | | /* a parameter is literal inside '...', so close the quote and reopen it */ |
132 | 0 | return "'\"${1}\"'"; |
133 | 0 | case SHELL_CTX_OPAQUE: |
134 | | /* bash evaluates an array subscript in arithmetic, so $1 holding |
135 | | x[$(cmd)] runs cmd even though it arrived as a parameter */ |
136 | 0 | return NULL; |
137 | 0 | } |
138 | 0 | return NULL; |
139 | 0 | } |
140 | | #else |
141 | | /* |
142 | | * Append "value" quoted for the context it lands in, so cmd.exe reads it as |
143 | | * one literal word. Windows keeps this route because "cmd /c" has no |
144 | | * positional parameters to hand the name to. |
145 | | */ |
146 | | static hts_boolean usercommand_append_quoted(hts_buf dest, size_t *pos, |
147 | | const char *value, |
148 | | shell_ctx_t ctx) { |
149 | | size_t i; |
150 | | |
151 | | /* cmd.exe has no single-quote syntax and no escape inside a quoted string, |
152 | | but & | < > ( ) ^ are inert between double quotes. A " would end that |
153 | | protection and cannot be escaped portably, so it is replaced. */ |
154 | | if (ctx != SHELL_CTX_DOUBLE) { |
155 | | if (!usercommand_append_char(dest, pos, '\"')) |
156 | | return HTS_FALSE; |
157 | | } |
158 | | for (i = 0; value[i] != '\0'; i++) { |
159 | | if (!usercommand_append_char(dest, pos, value[i] == '\"' ? '_' : value[i])) |
160 | | return HTS_FALSE; |
161 | | } |
162 | | if (ctx != SHELL_CTX_DOUBLE) { |
163 | | if (!usercommand_append_char(dest, pos, '\"')) |
164 | | return HTS_FALSE; |
165 | | } |
166 | | return HTS_TRUE; |
167 | | } |
168 | | #endif |
169 | | |
170 | | /* Name the cause of a refusal, so the user is told what to change. */ |
171 | 0 | static int usercommand_refuse(const char **why, const char *reason) { |
172 | 0 | if (why != NULL) |
173 | 0 | *why = reason; |
174 | 0 | return USERCOMMAND_EXPAND_REFUSED; |
175 | 0 | } |
176 | | |
177 | | /* |
178 | | * Rewrite the -V template "cmd" into "dest". On POSIX "$0" becomes the |
179 | | * shell's first positional parameter, which usercommand_exe() sets to the save |
180 | | * name. On Windows it becomes "file" quoted for cmd.exe. Returns |
181 | | * USERCOMMAND_EXPAND_OK, _TOOLONG, or _REFUSED with "*why" set. Except on OK, |
182 | | * "dest" holds a prefix that must not be run. |
183 | | */ |
184 | | int usercommand_expand(hts_buf dest, const char *cmd, const char *file, |
185 | 0 | const char **why) { |
186 | 0 | static const char too_deep[] = "the template nests substitutions deeper than " |
187 | 0 | "the scanner tracks"; |
188 | 0 | shell_frame_t stack[SHELL_CTX_MAX_DEPTH]; |
189 | 0 | size_t depth = 0; |
190 | 0 | shell_ctx_t ctx = SHELL_CTX_PLAIN; |
191 | 0 | hts_boolean escaped = HTS_FALSE; |
192 | 0 | size_t pos = 0; |
193 | 0 | size_t i; |
194 | |
|
195 | 0 | #ifndef _WIN32 |
196 | 0 | (void) file; /* the name travels as a parameter, not in the command text */ |
197 | 0 | #endif |
198 | 0 | if (dest.size == 0) |
199 | 0 | return USERCOMMAND_EXPAND_TOOLONG; |
200 | 0 | dest.p[0] = '\0'; |
201 | 0 | for (i = 0; cmd[i] != '\0'; i++) { |
202 | 0 | const char c = cmd[i]; |
203 | |
|
204 | 0 | if ((c == '$') && (cmd[i + 1] == '0')) { |
205 | | /* $0 is data only in a plain word, "..." and '...'. Elsewhere bash may |
206 | | re-expand or evaluate it, so every region the scanner opens refuses. */ |
207 | 0 | if (depth != 0) |
208 | 0 | return usercommand_refuse(why, "$0 must be a plain word, or inside " |
209 | 0 | "\"...\" or '...'; it is inside a " |
210 | 0 | "substitution, an expansion, " |
211 | 0 | "arithmetic, a subscript or [[ ]], " |
212 | 0 | "where the shell reads it again"); |
213 | 0 | #ifndef _WIN32 |
214 | | /* Drop a backslash an outer shell left, or it eats the parameter's first |
215 | | character. This keeps the documented -V "rm \$0" working. */ |
216 | 0 | if (escaped) { |
217 | | /* the flag is set only by a backslash this loop just emitted */ |
218 | 0 | assertf(pos > 0 && dest.p[pos - 1] == '\\'); |
219 | 0 | dest.p[--pos] = '\0'; |
220 | 0 | } |
221 | 0 | { |
222 | 0 | const char *const param = usercommand_param(ctx); |
223 | |
|
224 | 0 | if (param == NULL || |
225 | 0 | !usercommand_append(dest, &pos, hts_span_of(param))) |
226 | 0 | return USERCOMMAND_EXPAND_TOOLONG; |
227 | 0 | } |
228 | | #else |
229 | | if (!usercommand_append_quoted(dest, &pos, file, ctx)) |
230 | | return USERCOMMAND_EXPAND_TOOLONG; |
231 | | #endif |
232 | | /* the backslash applied to this $0 and is spent; leaving it set would |
233 | | suppress the next character's quote transition, and on the next $0 |
234 | | would delete the closing quote just emitted */ |
235 | 0 | escaped = HTS_FALSE; |
236 | 0 | i++; |
237 | 0 | continue; |
238 | 0 | } |
239 | | |
240 | | /* A backslash only suppresses the quote transition of the next character; |
241 | | it is still copied through verbatim. */ |
242 | 0 | if (!escaped && ctx != SHELL_CTX_SINGLE && c == '\\') { |
243 | 0 | escaped = HTS_TRUE; |
244 | 0 | } else { |
245 | 0 | if (!escaped && ctx != SHELL_CTX_SINGLE && c == '`') { |
246 | | /* A backquote closes the substitution it opened, or opens one. */ |
247 | 0 | if (depth != 0 && stack[depth - 1].closer == '`' && |
248 | 0 | ctx == SHELL_CTX_PLAIN) { |
249 | 0 | ctx = stack[--depth].ctx; |
250 | 0 | } else { |
251 | 0 | if (!usercommand_push(stack, &depth, ctx, '`', 0)) |
252 | 0 | return usercommand_refuse(why, too_deep); |
253 | 0 | ctx = SHELL_CTX_PLAIN; |
254 | 0 | } |
255 | 0 | } else if (!escaped && ctx != SHELL_CTX_SINGLE && c == '$' && |
256 | 0 | (cmd[i + 1] == '(' || cmd[i + 1] == '[' || |
257 | 0 | cmd[i + 1] == '{')) { |
258 | | /* $( ) and `...` start a fresh shell, so quoting is tracked inside |
259 | | them; $(( )), $[ ] and ${ } are read again as text by this one. */ |
260 | 0 | const char opened = cmd[i + 1]; |
261 | |
|
262 | 0 | if (!usercommand_push(stack, &depth, ctx, |
263 | 0 | opened == '(' ? ')' : (opened == '[' ? ']' : '}'), |
264 | 0 | 1)) |
265 | 0 | return usercommand_refuse(why, too_deep); |
266 | 0 | ctx = (opened == '{' || (opened == '(' && cmd[i + 2] != '(')) |
267 | 0 | ? SHELL_CTX_PLAIN |
268 | 0 | : SHELL_CTX_OPAQUE; |
269 | | /* copy the "$", then the bracket, which for arithmetic is the first of |
270 | | two and the second is counted below, so it takes both to close */ |
271 | 0 | if (!usercommand_append_char(dest, &pos, c)) |
272 | 0 | return USERCOMMAND_EXPAND_TOOLONG; |
273 | 0 | i++; |
274 | 0 | if (!usercommand_append_char(dest, &pos, cmd[i])) |
275 | 0 | return USERCOMMAND_EXPAND_TOOLONG; |
276 | 0 | continue; |
277 | 0 | } else if (!escaped && c == '[') { |
278 | | /* Bash evaluates a subscript (${a[$0]}, a[$0]=1) as arithmetic. A |
279 | | subscript sticks to a word, so "[ -f $0 ]" starts none. Inside '...' |
280 | | the emitted '"${1}"' splits the quote, so it is caught there too. */ |
281 | 0 | if (i != 0 && strchr(" \t\n;&|", cmd[i - 1]) == NULL) { |
282 | 0 | if (!usercommand_push(stack, &depth, ctx, ']', 1)) |
283 | 0 | return usercommand_refuse(why, too_deep); |
284 | 0 | ctx = SHELL_CTX_OPAQUE; |
285 | 0 | } else if (cmd[i + 1] == '[') { |
286 | | /* [[ ]] is not a subscript, but bash re-reads its operands: -eq |
287 | | evaluates arithmetic and =~ takes a regex. Its own region. */ |
288 | 0 | if (!usercommand_push(stack, &depth, ctx, ']', 2)) |
289 | 0 | return usercommand_refuse(why, too_deep); |
290 | 0 | if (!usercommand_append_char(dest, &pos, c)) |
291 | 0 | return USERCOMMAND_EXPAND_TOOLONG; |
292 | 0 | i++; |
293 | 0 | if (!usercommand_append_char(dest, &pos, cmd[i])) |
294 | 0 | return USERCOMMAND_EXPAND_TOOLONG; |
295 | 0 | continue; |
296 | 0 | } |
297 | 0 | } else if (!escaped && ctx == SHELL_CTX_PLAIN && c == '(' && |
298 | 0 | cmd[i + 1] == '(') { |
299 | | /* A bare (( )) is an arithmetic command, which covers "((n=$0))", the |
300 | | arithmetic for loop, and the "$( ((...)) )" that the lookahead |
301 | | above reads as an ordinary subshell. */ |
302 | 0 | if (!usercommand_push(stack, &depth, ctx, ')', 2)) |
303 | 0 | return usercommand_refuse(why, too_deep); |
304 | 0 | ctx = SHELL_CTX_OPAQUE; |
305 | 0 | if (!usercommand_append_char(dest, &pos, c)) |
306 | 0 | return USERCOMMAND_EXPAND_TOOLONG; |
307 | 0 | i++; |
308 | 0 | if (!usercommand_append_char(dest, &pos, cmd[i])) |
309 | 0 | return USERCOMMAND_EXPAND_TOOLONG; |
310 | 0 | continue; |
311 | 0 | } else if (!escaped && depth != 0 && stack[depth - 1].closer != '`' && |
312 | 0 | (ctx == SHELL_CTX_PLAIN || ctx == SHELL_CTX_OPAQUE) && |
313 | 0 | (c == stack[depth - 1].closer || |
314 | 0 | c == usercommand_opener(stack[depth - 1].closer))) { |
315 | 0 | if (c == stack[depth - 1].closer) { |
316 | 0 | if (--stack[depth - 1].open == 0) |
317 | 0 | ctx = stack[--depth].ctx; |
318 | 0 | } else { |
319 | 0 | stack[depth - 1].open++; |
320 | 0 | } |
321 | 0 | } else if (!escaped) { |
322 | 0 | switch (ctx) { |
323 | 0 | case SHELL_CTX_PLAIN: |
324 | 0 | if (c == '\'') |
325 | 0 | ctx = SHELL_CTX_SINGLE; |
326 | 0 | else if (c == '\"') |
327 | 0 | ctx = SHELL_CTX_DOUBLE; |
328 | 0 | break; |
329 | 0 | case SHELL_CTX_SINGLE: |
330 | 0 | if (c == '\'') |
331 | 0 | ctx = SHELL_CTX_PLAIN; |
332 | 0 | break; |
333 | 0 | case SHELL_CTX_DOUBLE: |
334 | 0 | if (c == '\"') |
335 | 0 | ctx = SHELL_CTX_PLAIN; |
336 | 0 | break; |
337 | 0 | case SHELL_CTX_OPAQUE: |
338 | | /* a quote here is an ordinary character, so it opens nothing */ |
339 | 0 | break; |
340 | 0 | } |
341 | 0 | } |
342 | 0 | escaped = HTS_FALSE; |
343 | 0 | } |
344 | | |
345 | 0 | if (!usercommand_append_char(dest, &pos, c)) |
346 | 0 | return USERCOMMAND_EXPAND_TOOLONG; |
347 | 0 | } |
348 | | |
349 | 0 | return USERCOMMAND_EXPAND_OK; |
350 | 0 | } |
351 | | |
352 | | #ifndef _WIN32 |
353 | | /* execv() does not write through its argv, but its prototype cannot say so. */ |
354 | 0 | static char *usercommand_unconst(const char *s) { |
355 | 0 | return (char *) (uintptr_t) s; |
356 | 0 | } |
357 | | |
358 | | /* |
359 | | * Fill "argv" with the vector usercommand_exe() execs and return how many |
360 | | * entries precede its terminating NULL. "file" becomes the shell's $1, which |
361 | | * is what usercommand_expand() rewrote each "$0" of the template to. |
362 | | */ |
363 | | size_t usercommand_argv(char *argv[USERCOMMAND_ARGV_MAX], const char *command, |
364 | 0 | const char *file) { |
365 | 0 | size_t n = 0; |
366 | |
|
367 | 0 | argv[n++] = usercommand_unconst("sh"); /* the shell's own argv[0] */ |
368 | 0 | argv[n++] = usercommand_unconst("-c"); |
369 | 0 | argv[n++] = usercommand_unconst(command); |
370 | 0 | argv[n++] = usercommand_unconst("sh"); /* $0 within the command */ |
371 | 0 | argv[n++] = usercommand_unconst(file); /* $1 */ |
372 | 0 | argv[n] = NULL; |
373 | 0 | return n; |
374 | 0 | } |
375 | | |
376 | | /* |
377 | | * Run "command" with "sh -c", handing "file" to it as $1. Returns the shell's |
378 | | * exit status, or -1 when it could not be run. Not system(): that blocks |
379 | | * SIGINT and SIGQUIT in the calling thread and waits on any child, and |
380 | | * httrack runs this from its workers. |
381 | | */ |
382 | | static int usercommand_spawn(httrackp *opt, const char *command, |
383 | 0 | const char *file) { |
384 | 0 | int status = 0; |
385 | 0 | const pid_t pid = fork(); |
386 | |
|
387 | 0 | if (pid == -1) { |
388 | 0 | hts_log_print(opt, LOG_ERROR | LOG_ERRNO, |
389 | 0 | "User command not run, can not fork: %.64s", command); |
390 | 0 | return -1; |
391 | 0 | } |
392 | 0 | if (pid == 0) { |
393 | 0 | char *argv[USERCOMMAND_ARGV_MAX]; |
394 | |
|
395 | 0 | (void) usercommand_argv(argv, command, file); |
396 | 0 | execv(_PATH_BSHELL, argv); |
397 | 0 | _exit(127); |
398 | 0 | } |
399 | 0 | while (waitpid(pid, &status, 0) == -1) { |
400 | | /* an embedder that set SIGCHLD to SIG_IGN reaped the child itself */ |
401 | 0 | if (errno != EINTR) |
402 | 0 | return -1; |
403 | 0 | } |
404 | 0 | if (WIFEXITED(status)) |
405 | 0 | return WEXITSTATUS(status); |
406 | 0 | return WIFSIGNALED(status) ? 128 + WTERMSIG(status) : -1; |
407 | 0 | } |
408 | | #endif |
409 | | |
410 | | /* Returns the shell's exit status, or -1 when the command was not run. */ |
411 | 0 | int usercommand_exe(httrackp *opt, const char *cmd, const char *file) { |
412 | 0 | char BIGSTK temp[8192]; |
413 | 0 | const char *why = "$0 cannot be quoted safely here"; |
414 | | |
415 | | /* Truncating a shell command is how a "rm '$0'" becomes a "rm". */ |
416 | 0 | switch (usercommand_expand(HTS_BUF(temp), cmd, file, &why)) { |
417 | 0 | case USERCOMMAND_EXPAND_OK: |
418 | 0 | break; |
419 | 0 | case USERCOMMAND_EXPAND_TOOLONG: |
420 | | /* on the console too: a silently skipped -V looks like a working one */ |
421 | 0 | printf("User command not run, expansion exceeds %d bytes: %.64s\n", |
422 | 0 | (int) sizeof(temp), cmd); |
423 | 0 | hts_log_print(opt, LOG_ERROR, |
424 | 0 | "User command not run, expansion exceeds %d bytes: %.64s", |
425 | 0 | (int) sizeof(temp), cmd); |
426 | 0 | return -1; |
427 | 0 | default: |
428 | 0 | printf("User command not run, %s: %.64s\n", why, cmd); |
429 | 0 | hts_log_print(opt, LOG_ERROR, "User command not run, %s: %.64s", why, cmd); |
430 | 0 | return -1; |
431 | 0 | } |
432 | | |
433 | | #ifdef _WIN32 |
434 | | return system(temp); |
435 | | #else |
436 | 0 | return usercommand_spawn(opt, temp, file); |
437 | 0 | #endif |
438 | 0 | } |