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