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