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