xref: /freebsd/contrib/libarchive/libarchive/archive_read_support_filter_program.c (revision 401026e4825a05abba6f945cf1b74b3328876fa2)
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 	char *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_filter {
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 
149 	if (signature != NULL && signature_len > 0) {
150 		state->signature_len = signature_len;
151 		state->signature = malloc(signature_len);
152 		memcpy(state->signature, signature, signature_len);
153 	}
154 
155 	if (__archive_read_register_bidder(a, state, NULL,
156 				&program_bidder_vtable) != ARCHIVE_OK) {
157 		free_state(state);
158 		return (ARCHIVE_FATAL);
159 	}
160 	return (ARCHIVE_OK);
161 
162 memerr:
163 	free_state(state);
164 	archive_set_error(_a, ENOMEM, "Can't allocate memory");
165 	return (ARCHIVE_FATAL);
166 }
167 
168 static void
program_bidder_free(struct archive_read_filter_bidder * self)169 program_bidder_free(struct archive_read_filter_bidder *self)
170 {
171 	struct program_bidder *state = (struct program_bidder *)self->data;
172 
173 	free_state(state);
174 }
175 
176 static void
free_state(struct program_bidder * state)177 free_state(struct program_bidder *state)
178 {
179 
180 	if (state) {
181 		free(state->cmd);
182 		free(state->signature);
183 		free(state);
184 	}
185 }
186 
187 /*
188  * If we do have a signature, bid only if that matches.
189  *
190  * If there's no signature, we bid INT_MAX the first time
191  * we're called, then never bid again.
192  */
193 static int
program_bidder_bid(struct archive_read_filter_bidder * self,struct archive_read_filter * upstream)194 program_bidder_bid(struct archive_read_filter_bidder *self,
195     struct archive_read_filter *upstream)
196 {
197 	struct program_bidder *state = self->data;
198 	const char *p;
199 
200 	/* If we have a signature, use that to match. */
201 	if (state->signature_len > 0) {
202 		p = __archive_read_filter_ahead(upstream,
203 		    state->signature_len, NULL);
204 		if (p == NULL)
205 			return (0);
206 		/* No match, so don't bid. */
207 		if (memcmp(p, state->signature, state->signature_len) != 0)
208 			return (0);
209 		return ((int)state->signature_len * 8);
210 	}
211 
212 	/* Otherwise, bid once and then never bid again. */
213 	if (state->inhibit)
214 		return (0);
215 	state->inhibit = 1;
216 	return (INT_MAX);
217 }
218 
219 /*
220  * Shut down the child, return ARCHIVE_OK if it exited normally.
221  *
222  * Note that the return value is sticky; if we're called again,
223  * we won't reap the child again, but we will return the same status
224  * (including error message if the child came to a bad end).
225  */
226 static int
child_stop(struct archive_read_filter * self,struct program_filter * state)227 child_stop(struct archive_read_filter *self, struct program_filter *state)
228 {
229 	/* Close our side of the I/O with the child. */
230 	if (state->child_stdin != -1) {
231 		close(state->child_stdin);
232 		state->child_stdin = -1;
233 	}
234 	if (state->child_stdout != -1) {
235 		close(state->child_stdout);
236 		state->child_stdout = -1;
237 	}
238 
239 	if (state->child != 0) {
240 		/* Reap the child. */
241 		do {
242 			state->waitpid_return
243 			    = waitpid(state->child, &state->exit_status, 0);
244 		} while (state->waitpid_return == -1 && errno == EINTR);
245 		state->child = 0;
246 	}
247 
248 	if (state->waitpid_return < 0) {
249 		/* waitpid() failed?  This is ugly. */
250 		archive_set_error(&self->archive->archive, ARCHIVE_ERRNO_MISC,
251 		    "Error closing child process");
252 		return (ARCHIVE_WARN);
253 	}
254 
255 #if !defined(_WIN32) || defined(__CYGWIN__)
256 	if (WIFSIGNALED(state->exit_status)) {
257 #ifdef SIGPIPE
258 		/* If the child died because we stopped reading before
259 		 * it was done, that's okay.  Some archive formats
260 		 * have padding at the end that we routinely ignore. */
261 		/* The alternative to this would be to add a step
262 		 * before close(child_stdout) above to read from the
263 		 * child until the child has no more to write. */
264 		if (WTERMSIG(state->exit_status) == SIGPIPE)
265 			return (ARCHIVE_OK);
266 #endif
267 		archive_set_error(&self->archive->archive, ARCHIVE_ERRNO_MISC,
268 		    "Child process exited with signal %d",
269 		    WTERMSIG(state->exit_status));
270 		return (ARCHIVE_WARN);
271 	}
272 #endif /* !_WIN32 || __CYGWIN__ */
273 
274 	if (WIFEXITED(state->exit_status)) {
275 		if (WEXITSTATUS(state->exit_status) == 0)
276 			return (ARCHIVE_OK);
277 
278 		archive_set_error(&self->archive->archive,
279 		    ARCHIVE_ERRNO_MISC,
280 		    "Child process exited with status %d",
281 		    WEXITSTATUS(state->exit_status));
282 		return (ARCHIVE_WARN);
283 	}
284 
285 	return (ARCHIVE_WARN);
286 }
287 
288 /*
289  * Use select() to decide whether the child is ready for read or write.
290  */
291 static ssize_t
child_read(struct archive_read_filter * self,char * buf,size_t buf_len)292 child_read(struct archive_read_filter *self, char *buf, size_t buf_len)
293 {
294 	struct program_filter *state = self->data;
295 	ssize_t ret, requested, avail;
296 	const char *p;
297 #if defined(_WIN32) && !defined(__CYGWIN__)
298 	HANDLE handle = (HANDLE)_get_osfhandle(state->child_stdout);
299 #endif
300 
301 	requested = buf_len > SSIZE_MAX ? SSIZE_MAX : buf_len;
302 
303 	for (;;) {
304 		do {
305 #if defined(_WIN32) && !defined(__CYGWIN__)
306 			/* Avoid infinity wait.
307 			 * Note: If there is no data in the pipe, ReadFile()
308 			 * called in read() never returns and so we won't
309 			 * write remaining encoded data to the pipe.
310 			 * Note: This way may cause performance problem.
311 			 * we are looking forward to great code to resolve
312 			 * this.  */
313 			DWORD pipe_avail = -1;
314 			int cnt = 2;
315 
316 			while (PeekNamedPipe(handle, NULL, 0, NULL,
317 			    &pipe_avail, NULL) != 0 && pipe_avail == 0 &&
318 			    cnt--)
319 				Sleep(5);
320 			if (pipe_avail == 0) {
321 				ret = -1;
322 				errno = EAGAIN;
323 				break;
324 			}
325 #endif
326 			ret = read(state->child_stdout, buf, requested);
327 		} while (ret == -1 && errno == EINTR);
328 
329 		if (ret > 0)
330 			return (ret);
331 		if (ret == 0 || (ret == -1 && errno == EPIPE))
332 			/* Child has closed its output; reap the child
333 			 * and return the status. */
334 			return (child_stop(self, state));
335 		if (ret == -1 && errno != EAGAIN)
336 			return (-1);
337 
338 		if (state->child_stdin == -1) {
339 			/* Block until child has some I/O ready. */
340 			__archive_check_child(state->child_stdin,
341 			    state->child_stdout);
342 			continue;
343 		}
344 
345 		/* Get some more data from upstream. */
346 		p = __archive_read_filter_ahead(self->upstream, 1, &avail);
347 		if (p == NULL) {
348 			close(state->child_stdin);
349 			state->child_stdin = -1;
350 			fcntl(state->child_stdout, F_SETFL, 0);
351 			if (avail < 0)
352 				return (avail);
353 			continue;
354 		}
355 
356 		do {
357 			ret = write(state->child_stdin, p, avail);
358 		} while (ret == -1 && errno == EINTR);
359 
360 		if (ret > 0) {
361 			/* Consume whatever we managed to write. */
362 			__archive_read_filter_consume(self->upstream, ret);
363 		} else if (ret == -1 && errno == EAGAIN) {
364 			/* Block until child has some I/O ready. */
365 			__archive_check_child(state->child_stdin,
366 			    state->child_stdout);
367 		} else {
368 			/* Write failed. */
369 			close(state->child_stdin);
370 			state->child_stdin = -1;
371 			fcntl(state->child_stdout, F_SETFL, 0);
372 			/* If it was a bad error, we're done; otherwise
373 			 * it was EPIPE or EOF, and we can still read
374 			 * from the child. */
375 			if (ret == -1 && errno != EPIPE)
376 				return (-1);
377 		}
378 	}
379 }
380 
381 static const struct archive_read_filter_vtable
382 program_reader_vtable = {
383 	.read = program_filter_read,
384 	.close = program_filter_close,
385 };
386 
387 int
__archive_read_program(struct archive_read_filter * self,const char * cmd)388 __archive_read_program(struct archive_read_filter *self, const char *cmd)
389 {
390 	struct program_filter	*state;
391 	static const size_t out_buf_len = 65536;
392 	char *out_buf;
393 	const char *prefix = "Program: ";
394 	int ret;
395 	size_t l;
396 
397 	l = strlen(prefix) + strlen(cmd) + 1;
398 	state = calloc(1, sizeof(*state));
399 	out_buf = malloc(out_buf_len);
400 	if (state == NULL || out_buf == NULL ||
401 	    archive_string_ensure(&state->description, l) == NULL) {
402 		archive_set_error(&self->archive->archive, ENOMEM,
403 		    "Can't allocate input data");
404 		if (state != NULL) {
405 			archive_string_free(&state->description);
406 			free(state);
407 		}
408 		free(out_buf);
409 		return (ARCHIVE_FATAL);
410 	}
411 	archive_strcpy(&state->description, prefix);
412 	archive_strcat(&state->description, cmd);
413 
414 	self->code = ARCHIVE_FILTER_PROGRAM;
415 	self->name = state->description.s;
416 
417 	state->out_buf = out_buf;
418 	state->out_buf_len = out_buf_len;
419 
420 	ret = __archive_create_child(cmd, &state->child_stdin,
421 	    &state->child_stdout, &state->child);
422 	if (ret != ARCHIVE_OK) {
423 		free(state->out_buf);
424 		archive_string_free(&state->description);
425 		free(state);
426 		archive_set_error(&self->archive->archive, EINVAL,
427 		    "Can't initialize filter; unable to run program \"%s\"",
428 		    cmd);
429 		return (ARCHIVE_FATAL);
430 	}
431 
432 	self->data = state;
433 	self->vtable = &program_reader_vtable;
434 
435 	/* XXX Check that we can read at least one byte? */
436 	return (ARCHIVE_OK);
437 }
438 
439 static int
program_bidder_init(struct archive_read_filter * self)440 program_bidder_init(struct archive_read_filter *self)
441 {
442 	struct program_bidder   *bidder_state;
443 
444 	bidder_state = (struct program_bidder *)self->bidder->data;
445 	return (__archive_read_program(self, bidder_state->cmd));
446 }
447 
448 static ssize_t
program_filter_read(struct archive_read_filter * self,const void ** buff)449 program_filter_read(struct archive_read_filter *self, const void **buff)
450 {
451 	struct program_filter *state;
452 	ssize_t bytes;
453 	size_t total;
454 	char *p;
455 
456 	state = (struct program_filter *)self->data;
457 
458 	total = 0;
459 	p = state->out_buf;
460 	while (state->child_stdout != -1 && total < state->out_buf_len) {
461 		bytes = child_read(self, p, state->out_buf_len - total);
462 		if (bytes < 0)
463 			/* No recovery is possible if we can no longer
464 			 * read from the child. */
465 			return (ARCHIVE_FATAL);
466 		if (bytes == 0)
467 			/* We got EOF from the child. */
468 			break;
469 		total += bytes;
470 		p += bytes;
471 	}
472 
473 	*buff = state->out_buf;
474 	return (total);
475 }
476 
477 static int
program_filter_close(struct archive_read_filter * self)478 program_filter_close(struct archive_read_filter *self)
479 {
480 	struct program_filter	*state;
481 	int e;
482 
483 	state = (struct program_filter *)self->data;
484 	e = child_stop(self, state);
485 
486 	/* Release our private data. */
487 	free(state->out_buf);
488 	archive_string_free(&state->description);
489 	free(state);
490 
491 	return (e);
492 }
493