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