Home | History | Annotate | Line # | Download | only in ftpd
popen.c revision 1.13.2.1
      1  1.13.2.1        he /*	$NetBSD: popen.c,v 1.13.2.1 2001/04/01 16:08:21 he Exp $	*/
      2       1.5       cgd 
      3       1.1       cgd /*
      4       1.3   deraadt  * Copyright (c) 1988, 1993, 1994
      5       1.3   deraadt  *	The Regents of the University of California.  All rights reserved.
      6       1.1       cgd  *
      7       1.1       cgd  * This code is derived from software written by Ken Arnold and
      8       1.1       cgd  * published in UNIX Review, Vol. 6, No. 8.
      9       1.1       cgd  *
     10       1.1       cgd  * Redistribution and use in source and binary forms, with or without
     11       1.1       cgd  * modification, are permitted provided that the following conditions
     12       1.1       cgd  * are met:
     13       1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     14       1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     15       1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     17       1.1       cgd  *    documentation and/or other materials provided with the distribution.
     18       1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     19       1.1       cgd  *    must display the following acknowledgement:
     20       1.1       cgd  *	This product includes software developed by the University of
     21       1.1       cgd  *	California, Berkeley and its contributors.
     22       1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     23       1.1       cgd  *    may be used to endorse or promote products derived from this software
     24       1.1       cgd  *    without specific prior written permission.
     25       1.1       cgd  *
     26       1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27       1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28       1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29       1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30       1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31       1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32       1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33       1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34       1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35       1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36       1.1       cgd  * SUCH DAMAGE.
     37       1.1       cgd  *
     38       1.1       cgd  */
     39       1.1       cgd 
     40       1.8  christos #include <sys/cdefs.h>
     41       1.1       cgd #ifndef lint
     42       1.5       cgd #if 0
     43       1.3   deraadt static char sccsid[] = "@(#)popen.c	8.3 (Berkeley) 4/6/94";
     44       1.5       cgd #else
     45  1.13.2.1        he __RCSID("$NetBSD: popen.c,v 1.13.2.1 2001/04/01 16:08:21 he Exp $");
     46       1.5       cgd #endif
     47       1.1       cgd #endif /* not lint */
     48       1.1       cgd 
     49       1.1       cgd #include <sys/types.h>
     50       1.1       cgd #include <sys/wait.h>
     51       1.3   deraadt 
     52       1.3   deraadt #include <errno.h>
     53       1.3   deraadt #include <glob.h>
     54       1.1       cgd #include <signal.h>
     55       1.1       cgd #include <stdio.h>
     56       1.1       cgd #include <stdlib.h>
     57       1.1       cgd #include <string.h>
     58       1.3   deraadt #include <unistd.h>
     59      1.13  explorer 
     60      1.13  explorer #ifdef KERBEROS5
     61      1.13  explorer #include <krb5.h>
     62      1.13  explorer #endif
     63       1.3   deraadt 
     64       1.3   deraadt #include "extern.h"
     65       1.1       cgd 
     66       1.8  christos #define INCR	100
     67       1.1       cgd /*
     68       1.9     lukem  * Special version of popen which avoids call to shell.  This ensures no-one
     69       1.1       cgd  * may create a pipe to a hidden program as a side effect of a list or dir
     70       1.1       cgd  * command.
     71       1.9     lukem  * If stderrfd != -1, then send stderr of a read command there,
     72       1.9     lukem  * otherwise close stderr.
     73       1.1       cgd  */
     74       1.1       cgd static int *pids;
     75       1.1       cgd static int fds;
     76       1.1       cgd 
     77       1.1       cgd FILE *
     78       1.9     lukem ftpd_popen(program, type, stderrfd)
     79       1.1       cgd 	char *program, *type;
     80       1.9     lukem 	int stderrfd;
     81       1.1       cgd {
     82       1.3   deraadt 	char *cp;
     83       1.8  christos 	FILE *iop = NULL;
     84       1.1       cgd 	int argc, gargc, pdes[2], pid;
     85       1.8  christos 	char **pop, **np;
     86       1.8  christos 	char **argv = NULL, **gargv = NULL;
     87       1.8  christos 	size_t nargc = 100, ngargc = 100;
     88       1.8  christos #ifdef __GNUC__
     89       1.8  christos 	(void) &iop;
     90       1.8  christos 	(void) &gargc;
     91       1.8  christos 	(void) &gargv;
     92       1.8  christos 	(void) &argv;
     93       1.8  christos #endif
     94       1.1       cgd 
     95       1.6     lukem 	if ((*type != 'r' && *type != 'w') || type[1])
     96       1.3   deraadt 		return (NULL);
     97       1.1       cgd 
     98       1.1       cgd 	if (!pids) {
     99       1.1       cgd 		if ((fds = getdtablesize()) <= 0)
    100       1.3   deraadt 			return (NULL);
    101       1.1       cgd 		if ((pids = (int *)malloc((u_int)(fds * sizeof(int)))) == NULL)
    102       1.3   deraadt 			return (NULL);
    103       1.3   deraadt 		memset(pids, 0, fds * sizeof(int));
    104       1.1       cgd 	}
    105       1.1       cgd 	if (pipe(pdes) < 0)
    106       1.3   deraadt 		return (NULL);
    107       1.1       cgd 
    108       1.8  christos 	if ((argv = malloc(nargc * sizeof(char *))) == NULL)
    109       1.8  christos 		return NULL;
    110       1.8  christos 
    111       1.8  christos #define CHECKMORE(c, v, n) \
    112       1.8  christos 	if (c >= n + 2) { \
    113       1.8  christos 		n += INCR; \
    114       1.8  christos 		if ((np = realloc(v, n * sizeof(char *))) == NULL) \
    115       1.8  christos 			goto pfree; \
    116       1.8  christos 		else \
    117       1.8  christos 			v = np; \
    118       1.8  christos 	}
    119       1.8  christos 
    120       1.1       cgd 	/* break up string into pieces */
    121       1.8  christos 	for (argc = 0, cp = program;; cp = NULL) {
    122       1.8  christos 		CHECKMORE(argc, argv, nargc)
    123       1.1       cgd 		if (!(argv[argc++] = strtok(cp, " \t\n")))
    124       1.1       cgd 			break;
    125       1.8  christos 	}
    126       1.8  christos 
    127       1.8  christos 	if ((gargv = malloc(ngargc * sizeof(char *))) == NULL)
    128       1.8  christos 		goto pfree;
    129       1.1       cgd 
    130       1.1       cgd 	/* glob each piece */
    131       1.1       cgd 	gargv[0] = argv[0];
    132       1.1       cgd 	for (gargc = argc = 1; argv[argc]; argc++) {
    133       1.3   deraadt 		glob_t gl;
    134  1.13.2.1        he 		int flags = GLOB_BRACE|GLOB_NOCHECK|GLOB_TILDE|GLOB_LIMIT;
    135       1.3   deraadt 
    136       1.3   deraadt 		memset(&gl, 0, sizeof(gl));
    137       1.8  christos 		if (glob(argv[argc], flags, NULL, &gl)) {
    138       1.8  christos 			CHECKMORE(gargc, gargv, ngargc)
    139       1.8  christos 			if ((gargv[gargc++] = strdup(argv[argc])) == NULL)
    140       1.8  christos 				goto pfree;
    141       1.8  christos 		}
    142       1.3   deraadt 		else
    143       1.8  christos 			for (pop = gl.gl_pathv; *pop; pop++) {
    144       1.8  christos 				CHECKMORE(gargc, gargv, ngargc)
    145       1.8  christos 				if ((gargv[gargc++] = strdup(*pop)) == NULL)
    146       1.8  christos 					goto pfree;
    147       1.8  christos 			}
    148       1.3   deraadt 		globfree(&gl);
    149       1.1       cgd 	}
    150       1.1       cgd 	gargv[gargc] = NULL;
    151       1.1       cgd 
    152       1.1       cgd 	switch(pid = vfork()) {
    153       1.1       cgd 	case -1:			/* error */
    154       1.1       cgd 		(void)close(pdes[0]);
    155       1.1       cgd 		(void)close(pdes[1]);
    156       1.1       cgd 		goto pfree;
    157       1.1       cgd 		/* NOTREACHED */
    158       1.1       cgd 	case 0:				/* child */
    159       1.1       cgd 		if (*type == 'r') {
    160       1.3   deraadt 			if (pdes[1] != STDOUT_FILENO) {
    161       1.3   deraadt 				dup2(pdes[1], STDOUT_FILENO);
    162       1.1       cgd 				(void)close(pdes[1]);
    163       1.1       cgd 			}
    164       1.9     lukem 			if (stderrfd == -1)
    165       1.7     lukem 				(void)close(STDERR_FILENO);
    166       1.9     lukem 			else
    167       1.9     lukem 				dup2(stderrfd, STDERR_FILENO);
    168       1.1       cgd 			(void)close(pdes[0]);
    169       1.1       cgd 		} else {
    170       1.3   deraadt 			if (pdes[0] != STDIN_FILENO) {
    171       1.3   deraadt 				dup2(pdes[0], STDIN_FILENO);
    172       1.1       cgd 				(void)close(pdes[0]);
    173       1.1       cgd 			}
    174       1.1       cgd 			(void)close(pdes[1]);
    175       1.1       cgd 		}
    176       1.1       cgd 		execv(gargv[0], gargv);
    177       1.1       cgd 		_exit(1);
    178       1.1       cgd 	}
    179       1.1       cgd 	/* parent; assume fdopen can't fail...  */
    180       1.1       cgd 	if (*type == 'r') {
    181       1.1       cgd 		iop = fdopen(pdes[0], type);
    182       1.1       cgd 		(void)close(pdes[1]);
    183       1.1       cgd 	} else {
    184       1.1       cgd 		iop = fdopen(pdes[1], type);
    185       1.1       cgd 		(void)close(pdes[0]);
    186       1.1       cgd 	}
    187       1.1       cgd 	pids[fileno(iop)] = pid;
    188       1.1       cgd 
    189       1.8  christos pfree:	if (gargv) {
    190       1.8  christos 		for (argc = 1; argc < gargc; argc++)
    191       1.8  christos 			free(gargv[argc]);
    192       1.8  christos 		free(gargv);
    193       1.8  christos 	}
    194       1.8  christos 	if (argv)
    195       1.8  christos 		free(argv);
    196       1.3   deraadt 
    197       1.3   deraadt 	return (iop);
    198       1.1       cgd }
    199       1.1       cgd 
    200       1.3   deraadt int
    201       1.1       cgd ftpd_pclose(iop)
    202       1.1       cgd 	FILE *iop;
    203       1.1       cgd {
    204       1.6     lukem 	int fdes, status;
    205       1.3   deraadt 	pid_t pid;
    206       1.4   mycroft 	sigset_t sigset, osigset;
    207       1.1       cgd 
    208       1.1       cgd 	/*
    209       1.1       cgd 	 * pclose returns -1 if stream is not associated with a
    210       1.1       cgd 	 * `popened' command, or, if already `pclosed'.
    211       1.1       cgd 	 */
    212       1.1       cgd 	if (pids == 0 || pids[fdes = fileno(iop)] == 0)
    213       1.3   deraadt 		return (-1);
    214       1.1       cgd 	(void)fclose(iop);
    215       1.4   mycroft 	sigemptyset(&sigset);
    216       1.4   mycroft 	sigaddset(&sigset, SIGINT);
    217       1.4   mycroft 	sigaddset(&sigset, SIGQUIT);
    218       1.4   mycroft 	sigaddset(&sigset, SIGHUP);
    219       1.4   mycroft 	sigprocmask(SIG_BLOCK, &sigset, &osigset);
    220       1.3   deraadt 	while ((pid = waitpid(pids[fdes], &status, 0)) < 0 && errno == EINTR)
    221       1.3   deraadt 		continue;
    222       1.4   mycroft 	sigprocmask(SIG_SETMASK, &osigset, NULL);
    223       1.1       cgd 	pids[fdes] = 0;
    224       1.3   deraadt 	if (pid < 0)
    225       1.3   deraadt 		return (pid);
    226       1.3   deraadt 	if (WIFEXITED(status))
    227       1.3   deraadt 		return (WEXITSTATUS(status));
    228       1.3   deraadt 	return (1);
    229       1.1       cgd }
    230