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