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