xref: /freebsd/contrib/libarchive/libarchive/archive_read_support_filter_program.c (revision 185becb1e1bd2657c156f78aeb52edac05ba5fb5)
1 /*-
2  * Copyright (c) 2007 Joerg Sonnenberger
3  * Copyright (c) 2012 Michihiro NAKAJIMA
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #include "archive_platform.h"
28 
29 #ifdef HAVE_SYS_WAIT_H
30 #  include <sys/wait.h>
31 #endif
32 #ifdef HAVE_ERRNO_H
33 #  include <errno.h>
34 #endif
35 #ifdef HAVE_FCNTL_H
36 #  include <fcntl.h>
37 #endif
38 #ifdef HAVE_LIMITS_H
39 #  include <limits.h>
40 #endif
41 #ifdef HAVE_SIGNAL_H
42 #  include <signal.h>
43 #endif
44 #ifdef HAVE_STDLIB_H
45 #  include <stdlib.h>
46 #endif
47 #ifdef HAVE_STRING_H
48 #  include <string.h>
49 #endif
50 #ifdef HAVE_UNISTD_H
51 #  include <unistd.h>
52 #endif
53 
54 #include "archive.h"
55 #include "archive_private.h"
56 #include "archive_string.h"
57 #include "archive_read_private.h"
58 #include "filter_fork.h"
59 
60 
61 #if ARCHIVE_VERSION_NUMBER < 4000000
62 /* Deprecated; remove in libarchive 4.0 */
63 int
archive_read_support_compression_program(struct archive * a,const char * cmd)64 archive_read_support_compression_program(struct archive *a, const char *cmd)
65 {
66 	return archive_read_support_filter_program(a, cmd);
67 }
68 
69 int
archive_read_support_compression_program_signature(struct archive * a,const char * cmd,const void * signature,size_t signature_len)70 archive_read_support_compression_program_signature(struct archive *a,
71     const char *cmd, const void *signature, size_t signature_len)
72 {
73 	return archive_read_support_filter_program_signature(a,
74 	    cmd, signature, signature_len);
75 }
76 #endif
77 
78 int
archive_read_support_filter_program(struct archive * a,const char * cmd)79 archive_read_support_filter_program(struct archive *a, const char *cmd)
80 {
81 	return (archive_read_support_filter_program_signature(a, cmd, NULL, 0));
82 }
83 
84 /*
85  * The bidder object stores the command and the signature to watch for.
86  * The 'inhibit' entry here is used to ensure that unchecked filters never
87  * bid twice in the same pipeline.
88  */
89 struct program_bidder {
90 	struct archive_string description;
91 	char *cmd;
92 	void *signature;
93 	size_t signature_len;
94 	int inhibit;
95 };
96 
97 static int	program_bidder_bid(struct archive_read_filter_bidder *,
98 		    struct archive_read_filter *upstream);
99 static int	program_bidder_init(struct archive_read_filter *);
100 static void	program_bidder_free(struct archive_read_filter_bidder *);
101 
102 /*
103  * The actual filter needs to track input and output data.
104  */
105 struct program {
106 	struct archive_string description;
107 #if defined(_WIN32) && !defined(__CYGWIN__)
108 	HANDLE		 child;
109 #else
110 	pid_t		 child;
111 #endif
112 	int		 exit_status;
113 	pid_t		 waitpid_return;
114 	int		 child_stdin, child_stdout;
115 
116 	char		*out_buf;
117 	size_t		 out_buf_len;
118 };
119 
120 static ssize_t	program_filter_read(struct archive_read_filter *,
121 		    const void **);
122 static int	program_filter_close(struct archive_read_filter *);
123 static void	free_state(struct program_bidder *);
124 
125 static const struct archive_read_filter_bidder_vtable
126 program_bidder_vtable = {
127 	.bid = program_bidder_bid,
128 	.init = program_bidder_init,
129 	.free = program_bidder_free,
130 };
131 
132 int
archive_read_support_filter_program_signature(struct archive * _a,const char * cmd,const void * signature,size_t signature_len)133 archive_read_support_filter_program_signature(struct archive *_a,
134     const char *cmd, const void *signature, size_t signature_len)
135 {
136 	struct archive_read *a = (struct archive_read *)_a;
137 	struct program_bidder *state;
138 
139 	/*
140 	 * Allocate our private state.
141 	 */
142 	state = calloc(1, sizeof (*state));
143 	if (state == NULL)
144 		goto memerr;
145 	state->cmd = strdup(cmd);
146 	if (state->cmd == NULL)
147 		goto memerr;
148 	archive_strcpy(&state->description, "Program: ");
149 	archive_strcat(&state->description, cmd);
150 
151 	if (signature != NULL && signature_len > 0) {
152 		state->signature_len = signature_len;
153 		state->signature = malloc(signature_len);
154 		if (state->signature == NULL)
155 			goto memerr;
156 		memcpy(state->signature, signature, signature_len);
157 	}
158 
159 	if (__archive_read_register_bidder(a, state, NULL,
160 				&program_bidder_vtable) != ARCHIVE_OK) {
161 		free_state(state);
162 		return (ARCHIVE_FATAL);
163 	}
164 	return (ARCHIVE_OK);
165 
166 memerr:
167 	free_state(state);
168 	archive_set_error(_a, ENOMEM, "Can't allocate memory");
169 	return (ARCHIVE_FATAL);
170 }
171 
172 static void
program_bidder_free(struct archive_read_filter_bidder * b)173 program_bidder_free(struct archive_read_filter_bidder *b)
174 {
175 	struct program_bidder *state = (struct program_bidder *)b->data;
176 
177 	free_state(state);
178 }
179 
180 static void
free_state(struct program_bidder * state)181 free_state(struct program_bidder *state)
182 {
183 
184 	if (state) {
185 		archive_string_free(&state->description);
186 		free(state->cmd);
187 		free(state->signature);
188 		free(state);
189 	}
190 }
191 
192 /*
193  * If we do have a signature, bid only if that matches.
194  *
195  * If there's no signature, we bid INT_MAX the first time
196  * we're called, then never bid again.
197  */
198 static int
program_bidder_bid(struct archive_read_filter_bidder * b,struct archive_read_filter * f)199 program_bidder_bid(struct archive_read_filter_bidder *b,
200     struct archive_read_filter *f)
201 {
202 	struct program_bidder *state = b->data;
203 	const char *p;
204 
205 	/* If we have a signature, use that to match. */
206 	if (state->signature_len > 0) {
207 		p = __archive_read_filter_ahead(f,
208 		    state->signature_len, NULL);
209 		if (p == NULL)
210 			return (0);
211 		/* No match, so don't bid. */
212 		if (memcmp(p, state->signature, state->signature_len) != 0)
213 			return (0);
214 		return ((int)state->signature_len * 8);
215 	}
216 
217 	/* Otherwise, bid once and then never bid again. */
218 	if (state->inhibit)
219 		return (0);
220 	state->inhibit = 1;
221 	return (INT_MAX);
222 }
223 
224 /*
225  * Shut down the child, return ARCHIVE_OK if it exited normally.
226  *
227  * Note that the return value is sticky; if we're called again,
228  * we won't reap the child again, but we will return the same status
229  * (including error message if the child came to a bad end).
230  */
231 static int
child_stop(struct archive_read_filter * f,struct program * program)232 child_stop(struct archive_read_filter *f, struct program *program)
233 {
234 	/* Close our side of the I/O with the child. */
235 	if (program->child_stdin != -1) {
236 		close(program->child_stdin);
237 		program->child_stdin = -1;
238 	}
239 	if (program->child_stdout != -1) {
240 		close(program->child_stdout);
241 		program->child_stdout = -1;
242 	}
243 
244 	if (program->child != 0) {
245 		/* Reap the child. */
246 		do {
247 			program->waitpid_return
248 			    = waitpid(program->child, &program->exit_status, 0);
249 		} while (program->waitpid_return == -1 && errno == EINTR);
250 		program->child = 0;
251 	}
252 
253 	if (program->waitpid_return < 0) {
254 		/* waitpid() failed?  This is ugly. */
255 		archive_set_error(&f->archive->archive, ARCHIVE_ERRNO_MISC,
256 		    "Error closing child process");
257 		return (ARCHIVE_WARN);
258 	}
259 
260 #if !defined(_WIN32) || defined(__CYGWIN__)
261 	if (WIFSIGNALED(program->exit_status)) {
262 #ifdef SIGPIPE
263 		/* If the child died because we stopped reading before
264 		 * it was done, that's okay.  Some archive formats
265 		 * have padding at the end that we routinely ignore. */
266 		/* The alternative to this would be to add a step
267 		 * before close(child_stdout) above to read from the
268 		 * child until the child has no more to write. */
269 		if (WTERMSIG(program->exit_status) == SIGPIPE)
270 			return (ARCHIVE_OK);
271 #endif
272 		archive_set_error(&f->archive->archive, ARCHIVE_ERRNO_MISC,
273 		    "Child process exited with signal %d",
274 		    WTERMSIG(program->exit_status));
275 		return (ARCHIVE_WARN);
276 	}
277 #endif /* !_WIN32 || __CYGWIN__ */
278 
279 	if (WIFEXITED(program->exit_status)) {
280 		if (WEXITSTATUS(program->exit_status) == 0)
281 			return (ARCHIVE_OK);
282 
283 		archive_set_error(&f->archive->archive,
284 		    ARCHIVE_ERRNO_MISC,
285 		    "Child process exited with status %d",
286 		    WEXITSTATUS(program->exit_status));
287 		return (ARCHIVE_WARN);
288 	}
289 
290 	return (ARCHIVE_WARN);
291 }
292 
293 /*
294  * Use select() to decide whether the child is ready for read or write.
295  */
296 static ssize_t
child_read(struct archive_read_filter * f,char * buf,size_t buf_len)297 child_read(struct archive_read_filter *f, char *buf, size_t buf_len)
298 {
299 	struct program *program = f->data;
300 	ssize_t ret, requested, avail;
301 	const char *p;
302 #if defined(_WIN32) && !defined(__CYGWIN__)
303 	HANDLE handle = (HANDLE)_get_osfhandle(program->child_stdout);
304 #endif
305 
306 	requested = buf_len > SSIZE_MAX ? SSIZE_MAX : buf_len;
307 
308 	for (;;) {
309 		do {
310 #if defined(_WIN32) && !defined(__CYGWIN__)
311 			/* Avoid infinity wait.
312 			 * Note: If there is no data in the pipe, ReadFile()
313 			 * called in read() never returns and so we won't
314 			 * write remaining encoded data to the pipe.
315 			 * Note: This way may cause performance problem.
316 			 * we are looking forward to great code to resolve
317 			 * this.  */
318 			DWORD pipe_avail = -1;
319 			int cnt = 2;
320 
321 			while (PeekNamedPipe(handle, NULL, 0, NULL,
322 			    &pipe_avail, NULL) != 0 && pipe_avail == 0 &&
323 			    cnt--)
324 				Sleep(5);
325 			if (pipe_avail == 0) {
326 				ret = -1;
327 				errno = EAGAIN;
328 				break;
329 			}
330 #endif
331 			ret = read(program->child_stdout, buf, requested);
332 		} while (ret == -1 && errno == EINTR);
333 
334 		if (ret > 0)
335 			return (ret);
336 		if (ret == 0 || (ret == -1 && errno == EPIPE))
337 			/* Child has closed its output; reap the child
338 			 * and return the status. */
339 			return (child_stop(f, program));
340 		if (ret == -1 && errno != EAGAIN)
341 			return (-1);
342 
343 		if (program->child_stdin == -1) {
344 			/* Block until child has some I/O ready. */
345 			__archive_check_child(program->child_stdin,
346 			    program->child_stdout);
347 			continue;
348 		}
349 
350 		/* Get some more data from upstream. */
351 		p = __archive_read_filter_ahead(f->upstream, 1, &avail);
352 		if (p == NULL) {
353 			close(program->child_stdin);
354 			program->child_stdin = -1;
355 			fcntl(program->child_stdout, F_SETFL, 0);
356 			if (avail < 0)
357 				return (avail);
358 			continue;
359 		}
360 
361 		do {
362 			ret = write(program->child_stdin, p, avail);
363 		} while (ret == -1 && errno == EINTR);
364 
365 		if (ret > 0) {
366 			/* Consume whatever we managed to write. */
367 			__archive_read_filter_consume(f->upstream, ret);
368 		} else if (ret == -1 && errno == EAGAIN) {
369 			/* Block until child has some I/O ready. */
370 			__archive_check_child(program->child_stdin,
371 			    program->child_stdout);
372 		} else {
373 			/* Write failed. */
374 			close(program->child_stdin);
375 			program->child_stdin = -1;
376 			fcntl(program->child_stdout, F_SETFL, 0);
377 			/* If it was a bad error, we're done; otherwise
378 			 * it was EPIPE or EOF, and we can still read
379 			 * from the child. */
380 			if (ret == -1 && errno != EPIPE)
381 				return (-1);
382 		}
383 	}
384 }
385 
386 static const struct archive_read_filter_vtable
387 program_reader_vtable = {
388 	.read = program_filter_read,
389 	.close = program_filter_close,
390 };
391 
392 int
__archive_read_program(struct archive_read_filter * f,const char * cmd)393 __archive_read_program(struct archive_read_filter *f, const char *cmd)
394 {
395 	struct program *program;
396 	static const size_t out_buf_len = 65536;
397 	char *out_buf;
398 	const char *prefix = "Program: ";
399 	int ret;
400 	size_t l;
401 
402 	l = strlen(prefix) + strlen(cmd) + 1;
403 	program = calloc(1, sizeof(*program));
404 	out_buf = malloc(out_buf_len);
405 	if (program == NULL || out_buf == NULL ||
406 	    archive_string_ensure(&program->description, l) == NULL) {
407 		archive_set_error(&f->archive->archive, ENOMEM,
408 		    "Can't allocate input data");
409 		if (program != NULL) {
410 			archive_string_free(&program->description);
411 			free(program);
412 		}
413 		free(out_buf);
414 		return (ARCHIVE_FATAL);
415 	}
416 	archive_strcpy(&program->description, prefix);
417 	archive_strcat(&program->description, cmd);
418 
419 	f->code = ARCHIVE_FILTER_PROGRAM;
420 	f->name = program->description.s;
421 
422 	program->out_buf = out_buf;
423 	program->out_buf_len = out_buf_len;
424 
425 	ret = __archive_create_child(cmd, &program->child_stdin,
426 	    &program->child_stdout, &program->child);
427 	if (ret != ARCHIVE_OK) {
428 		free(program->out_buf);
429 		archive_string_free(&program->description);
430 		free(program);
431 		archive_set_error(&f->archive->archive, EINVAL,
432 		    "Can't initialize filter; unable to run program \"%s\"",
433 		    cmd);
434 		return (ARCHIVE_FATAL);
435 	}
436 
437 	f->data = program;
438 	f->vtable = &program_reader_vtable;
439 
440 	/* XXX Check that we can read at least one byte? */
441 	return (ARCHIVE_OK);
442 }
443 
444 static int
program_bidder_init(struct archive_read_filter * f)445 program_bidder_init(struct archive_read_filter *f)
446 {
447 	struct program_bidder   *bidder_state;
448 	int r;
449 
450 	bidder_state = (struct program_bidder *)f->bidder->data;
451 	r = __archive_read_program(f, bidder_state->cmd);
452 	f->name = bidder_state->description.s;
453 	return (r);
454 }
455 
456 static ssize_t
program_filter_read(struct archive_read_filter * f,const void ** buff)457 program_filter_read(struct archive_read_filter *f, const void **buff)
458 {
459 	struct program *program = f->data;
460 	ssize_t bytes;
461 	size_t total;
462 	char *p;
463 
464 	total = 0;
465 	p = program->out_buf;
466 	while (program->child_stdout != -1 && total < program->out_buf_len) {
467 		bytes = child_read(f, p, program->out_buf_len - total);
468 		if (bytes < 0)
469 			/* No recovery is possible if we can no longer
470 			 * read from the child. */
471 			return (ARCHIVE_FATAL);
472 		if (bytes == 0)
473 			/* We got EOF from the child. */
474 			break;
475 		total += bytes;
476 		p += bytes;
477 	}
478 
479 	*buff = program->out_buf;
480 	return (total);
481 }
482 
483 static int
program_filter_close(struct archive_read_filter * f)484 program_filter_close(struct archive_read_filter *f)
485 {
486 	struct program *program = f->data;
487 	int e;
488 
489 	e = child_stop(f, program);
490 
491 	/* Release our private data. */
492 	free(program->out_buf);
493 	archive_string_free(&program->description);
494 	free(program);
495 
496 	return (e);
497 }
498