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: src/lib/libarchive/archive_read_support_compression_program.c,v 1.6 2008/12/06 06:45:15 kientzle Exp $");
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 int
archive_read_support_compression_program(struct archive * a,const char * cmd)59 archive_read_support_compression_program(struct archive *a, const char *cmd)
60 {
61 	return (archive_read_support_compression_program_signature(a, cmd, NULL, 0));
62 }
63 
64 
65 /* This capability is only available on POSIX systems. */
66 #if (!defined(HAVE_PIPE) || !defined(HAVE_FCNTL) || \
67     !(defined(HAVE_FORK) || defined(HAVE_VFORK))) && (!defined(_WIN32) || defined(__CYGWIN__))
68 
69 /*
70  * On non-Posix systems, allow the program to build, but choke if
71  * this function is actually invoked.
72  */
73 int
archive_read_support_compression_program_signature(struct archive * _a,const char * cmd,void * signature,size_t signature_len)74 archive_read_support_compression_program_signature(struct archive *_a,
75     const char *cmd, void *signature, size_t signature_len)
76 {
77 	(void)_a; /* UNUSED */
78 	(void)cmd; /* UNUSED */
79 	(void)signature; /* UNUSED */
80 	(void)signature_len; /* UNUSED */
81 
82 	archive_set_error(_a, -1,
83 	    "External compression programs not supported on this platform");
84 	return (ARCHIVE_FATAL);
85 }
86 
87 int
__archive_read_program(struct archive_read_filter * self,const char * cmd)88 __archive_read_program(struct archive_read_filter *self, const char *cmd)
89 {
90 	(void)self; /* UNUSED */
91 	(void)cmd; /* UNUSED */
92 
93 	archive_set_error(&self->archive->archive, -1,
94 	    "External compression programs not supported on this platform");
95 	return (ARCHIVE_FATAL);
96 }
97 
98 #else
99 
100 #include "filter_fork.h"
101 
102 /*
103  * The bidder object stores the command and the signature to watch for.
104  * The 'inhibit' entry here is used to ensure that unchecked filters never
105  * bid twice in the same pipeline.
106  */
107 struct program_bidder {
108 	char *cmd;
109 	void *signature;
110 	size_t signature_len;
111 	int inhibit;
112 };
113 
114 static int	program_bidder_bid(struct archive_read_filter_bidder *,
115 		    struct archive_read_filter *upstream);
116 static int	program_bidder_init(struct archive_read_filter *);
117 static int	program_bidder_free(struct archive_read_filter_bidder *);
118 
119 /*
120  * The actual filter needs to track input and output data.
121  */
122 struct program_filter {
123 	char		*description;
124 	pid_t		 child;
125 	int		 exit_status;
126 	int		 waitpid_return;
127 	int		 child_stdin, child_stdout;
128 
129 	char		*out_buf;
130 	size_t		 out_buf_len;
131 };
132 
133 static ssize_t	program_filter_read(struct archive_read_filter *,
134 		    const void **);
135 static int	program_filter_close(struct archive_read_filter *);
136 
137 int
archive_read_support_compression_program_signature(struct archive * _a,const char * cmd,const void * signature,size_t signature_len)138 archive_read_support_compression_program_signature(struct archive *_a,
139     const char *cmd, const void *signature, size_t signature_len)
140 {
141 	struct archive_read *a = (struct archive_read *)_a;
142 	struct archive_read_filter_bidder *bidder;
143 	struct program_bidder *state;
144 
145 	/*
146 	 * Get a bidder object from the read core.
147 	 */
148 	bidder = __archive_read_get_bidder(a);
149 	if (bidder == NULL)
150 		return (ARCHIVE_FATAL);
151 
152 	/*
153 	 * Allocate our private state.
154 	 */
155 	state = (struct program_bidder *)calloc(sizeof (*state), 1);
156 	if (state == NULL)
157 		goto memerr;
158 	state->cmd = strdup(cmd);
159 	if (state->cmd == NULL)
160 		goto memerr;
161 	if (signature != NULL && signature_len > 0) {
162 		state->signature_len = signature_len;
163 		state->signature = malloc(signature_len);
164 		memcpy(state->signature, signature, signature_len);
165 	}
166 
167 	/*
168 	 * Fill in the bidder object.
169 	 */
170 	bidder->data = state;
171 	bidder->bid = program_bidder_bid;
172 	bidder->init = program_bidder_init;
173 	bidder->options = NULL;
174 	bidder->free = program_bidder_free;
175 	return (ARCHIVE_OK);
176 
177 memerr:
178 	free(state);
179 	archive_set_error(_a, ENOMEM, "Can't allocate memory");
180 	return (ARCHIVE_FATAL);
181 }
182 
183 static int
program_bidder_free(struct archive_read_filter_bidder * self)184 program_bidder_free(struct archive_read_filter_bidder *self)
185 {
186 	struct program_bidder *state = (struct program_bidder *)self->data;
187 	free(state->cmd);
188 	free(state->signature);
189 	free(self->data);
190 	return (ARCHIVE_OK);
191 }
192 
193 /*
194  * If we do have a signature, bid only if that matches.
195  *
196  * If there's no signature, we bid INT_MAX the first time
197  * we're called, then never bid again.
198  */
199 static int
program_bidder_bid(struct archive_read_filter_bidder * self,struct archive_read_filter * upstream)200 program_bidder_bid(struct archive_read_filter_bidder *self,
201     struct archive_read_filter *upstream)
202 {
203 	struct program_bidder *state = self->data;
204 	const char *p;
205 
206 	/* If we have a signature, use that to match. */
207 	if (state->signature_len > 0) {
208 		p = __archive_read_filter_ahead(upstream,
209 		    state->signature_len, NULL);
210 		if (p == NULL)
211 			return (0);
212 		/* No match, so don't bid. */
213 		if (memcmp(p, state->signature, state->signature_len) != 0)
214 			return (0);
215 		return (state->signature_len * 8);
216 	}
217 
218 	/* Otherwise, bid once and then never bid again. */
219 	if (state->inhibit)
220 		return (0);
221 	state->inhibit = 1;
222 	return (INT_MAX);
223 }
224 
225 /*
226  * Shut down the child, return ARCHIVE_OK if it exited normally.
227  *
228  * Note that the return value is sticky; if we're called again,
229  * we won't reap the child again, but we will return the same status
230  * (including error message if the child came to a bad end).
231  */
232 static int
child_stop(struct archive_read_filter * self,struct program_filter * state)233 child_stop(struct archive_read_filter *self, struct program_filter *state)
234 {
235 	/* Close our side of the I/O with the child. */
236 	if (state->child_stdin != -1) {
237 		close(state->child_stdin);
238 		state->child_stdin = -1;
239 	}
240 	if (state->child_stdout != -1) {
241 		close(state->child_stdout);
242 		state->child_stdout = -1;
243 	}
244 
245 	if (state->child != 0) {
246 		/* Reap the child. */
247 		do {
248 			state->waitpid_return
249 			    = waitpid(state->child, &state->exit_status, 0);
250 		} while (state->waitpid_return == -1 && errno == EINTR);
251 		state->child = 0;
252 	}
253 
254 	if (state->waitpid_return < 0) {
255 		/* waitpid() failed?  This is ugly. */
256 		archive_set_error(&self->archive->archive, ARCHIVE_ERRNO_MISC,
257 		    "Child process exited badly");
258 		return (ARCHIVE_WARN);
259 	}
260 
261 	if (WIFSIGNALED(state->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(state->exit_status) == SIGPIPE)
270 			return (ARCHIVE_OK);
271 #endif
272 		archive_set_error(&self->archive->archive, ARCHIVE_ERRNO_MISC,
273 		    "Child process exited with signal %d",
274 		    WTERMSIG(state->exit_status));
275 		return (ARCHIVE_WARN);
276 	}
277 
278 	if (WIFEXITED(state->exit_status)) {
279 		if (WEXITSTATUS(state->exit_status) == 0)
280 			return (ARCHIVE_OK);
281 
282 		archive_set_error(&self->archive->archive,
283 		    ARCHIVE_ERRNO_MISC,
284 		    "Child process exited with status %d",
285 		    WEXITSTATUS(state->exit_status));
286 		return (ARCHIVE_WARN);
287 	}
288 
289 	return (ARCHIVE_WARN);
290 }
291 
292 /*
293  * Use select() to decide whether the child is ready for read or write.
294  */
295 static ssize_t
child_read(struct archive_read_filter * self,char * buf,size_t buf_len)296 child_read(struct archive_read_filter *self, char *buf, size_t buf_len)
297 {
298 	struct program_filter *state = self->data;
299 	ssize_t ret, requested, avail;
300 	const char *p;
301 
302 	requested = buf_len > SSIZE_MAX ? SSIZE_MAX : buf_len;
303 
304 	for (;;) {
305 		do {
306 			ret = read(state->child_stdout, buf, requested);
307 		} while (ret == -1 && errno == EINTR);
308 
309 		if (ret > 0)
310 			return (ret);
311 		if (ret == 0 || (ret == -1 && errno == EPIPE))
312 			/* Child has closed its output; reap the child
313 			 * and return the status. */
314 			return (child_stop(self, state));
315 		if (ret == -1 && errno != EAGAIN)
316 			return (-1);
317 
318 		if (state->child_stdin == -1) {
319 			/* Block until child has some I/O ready. */
320 			__archive_check_child(state->child_stdin,
321 			    state->child_stdout);
322 			continue;
323 		}
324 
325 		/* Get some more data from upstream. */
326 		p = __archive_read_filter_ahead(self->upstream, 1, &avail);
327 		if (p == NULL) {
328 			close(state->child_stdin);
329 			state->child_stdin = -1;
330 			fcntl(state->child_stdout, F_SETFL, 0);
331 			if (avail < 0)
332 				return (avail);
333 			continue;
334 		}
335 
336 		do {
337 			ret = write(state->child_stdin, p, avail);
338 		} while (ret == -1 && errno == EINTR);
339 
340 		if (ret > 0) {
341 			/* Consume whatever we managed to write. */
342 			__archive_read_filter_consume(self->upstream, ret);
343 		} else if (ret == -1 && errno == EAGAIN) {
344 			/* Block until child has some I/O ready. */
345 			__archive_check_child(state->child_stdin,
346 			    state->child_stdout);
347 		} else {
348 			/* Write failed. */
349 			close(state->child_stdin);
350 			state->child_stdin = -1;
351 			fcntl(state->child_stdout, F_SETFL, 0);
352 			/* If it was a bad error, we're done; otherwise
353 			 * it was EPIPE or EOF, and we can still read
354 			 * from the child. */
355 			if (ret == -1 && errno != EPIPE)
356 				return (-1);
357 		}
358 	}
359 }
360 
361 int
__archive_read_program(struct archive_read_filter * self,const char * cmd)362 __archive_read_program(struct archive_read_filter *self, const char *cmd)
363 {
364 	struct program_filter	*state;
365 	static const size_t out_buf_len = 65536;
366 	char *out_buf;
367 	char *description;
368 	const char *prefix = "Program: ";
369 
370 	state = (struct program_filter *)calloc(1, sizeof(*state));
371 	out_buf = (char *)malloc(out_buf_len);
372 	description = (char *)malloc(strlen(prefix) + strlen(cmd) + 1);
373 	if (state == NULL || out_buf == NULL || description == NULL) {
374 		archive_set_error(&self->archive->archive, ENOMEM,
375 		    "Can't allocate input data");
376 		free(state);
377 		free(out_buf);
378 		free(description);
379 		return (ARCHIVE_FATAL);
380 	}
381 
382 	self->code = ARCHIVE_COMPRESSION_PROGRAM;
383 	state->description = description;
384 	strcpy(state->description, prefix);
385 	strcat(state->description, cmd);
386 	self->name = state->description;
387 
388 	state->out_buf = out_buf;
389 	state->out_buf_len = out_buf_len;
390 
391 	if ((state->child = __archive_create_child(cmd,
392 		 &state->child_stdin, &state->child_stdout)) == -1) {
393 		free(state->out_buf);
394 		free(state);
395 		archive_set_error(&self->archive->archive, EINVAL,
396 		    "Can't initialise filter");
397 		return (ARCHIVE_FATAL);
398 	}
399 
400 	self->data = state;
401 	self->read = program_filter_read;
402 	self->skip = NULL;
403 	self->close = program_filter_close;
404 
405 	/* XXX Check that we can read at least one byte? */
406 	return (ARCHIVE_OK);
407 }
408 
409 static int
program_bidder_init(struct archive_read_filter * self)410 program_bidder_init(struct archive_read_filter *self)
411 {
412 	struct program_bidder   *bidder_state;
413 
414 	bidder_state = (struct program_bidder *)self->bidder->data;
415 	return (__archive_read_program(self, bidder_state->cmd));
416 }
417 
418 static ssize_t
program_filter_read(struct archive_read_filter * self,const void ** buff)419 program_filter_read(struct archive_read_filter *self, const void **buff)
420 {
421 	struct program_filter *state;
422 	ssize_t bytes;
423 	size_t total;
424 	char *p;
425 
426 	state = (struct program_filter *)self->data;
427 
428 	total = 0;
429 	p = state->out_buf;
430 	while (state->child_stdout != -1 && total < state->out_buf_len) {
431 		bytes = child_read(self, p, state->out_buf_len - total);
432 		if (bytes < 0)
433 			/* No recovery is possible if we can no longer
434 			 * read from the child. */
435 			return (ARCHIVE_FATAL);
436 		if (bytes == 0)
437 			/* We got EOF from the child. */
438 			break;
439 		total += bytes;
440 		p += bytes;
441 	}
442 
443 	*buff = state->out_buf;
444 	return (total);
445 }
446 
447 static int
program_filter_close(struct archive_read_filter * self)448 program_filter_close(struct archive_read_filter *self)
449 {
450 	struct program_filter	*state;
451 	int e;
452 
453 	state = (struct program_filter *)self->data;
454 	e = child_stop(self, state);
455 
456 	/* Release our private data. */
457 	free(state->out_buf);
458 	free(state->description);
459 	free(state);
460 
461 	return (e);
462 }
463 
464 #endif /* !defined(HAVE_PIPE) || !defined(HAVE_VFORK) || !defined(HAVE_FCNTL) */
465