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