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
37#ifndef lint
38#if 0
39static char sccsid[] = "@(#)popen.c	8.3 (Berkeley) 4/6/94";
40#endif
41#endif /* not lint */
42
43#include <sys/cdefs.h>
44__FBSDID("$FreeBSD: src/libexec/ftpd/popen.c,v 1.26 2004/11/18 13:46:29 yar Exp $");
45
46#include <sys/types.h>
47#include <sys/wait.h>
48#include <netinet/in.h>
49
50#include <errno.h>
51#include <glob.h>
52#include <signal.h>
53#include <stdio.h>
54#include <stdlib.h>
55#include <string.h>
56#include <unistd.h>
57
58#include "extern.h"
59#include "pathnames.h"
60#include <syslog.h>
61#include <time.h>
62
63#define	MAXUSRARGS	100
64#define	MAXGLOBARGS	1000
65
66/*
67 * Special version of popen which avoids call to shell.  This ensures noone
68 * may create a pipe to a hidden program as a side effect of a list or dir
69 * command.
70 */
71static int *pids;
72static int fds;
73
74FILE *
75ftpd_popen(char *program, char *type)
76{
77	char *cp;
78	FILE *iop;
79	int argc, gargc, pdes[2], pid;
80	char **pop, *argv[MAXUSRARGS], *gargv[MAXGLOBARGS];
81
82	if (((*type != 'r') && (*type != 'w')) || type[1])
83		return (NULL);
84
85	if (!pids) {
86		if ((fds = getdtablesize()) <= 0)
87			return (NULL);
88		if ((pids = calloc(fds, sizeof(int))) == NULL)
89			return (NULL);
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	argv[argc - 1] = NULL;
100
101	/* glob each piece */
102	gargv[0] = argv[0];
103	for (gargc = argc = 1; argv[argc] && gargc < (MAXGLOBARGS-1); argc++) {
104		glob_t gl;
105		int flags = GLOB_BRACE|GLOB_NOCHECK|GLOB_TILDE;
106
107		memset(&gl, 0, sizeof(gl));
108		gl.gl_matchc = MAXGLOBARGS;
109		flags |= GLOB_LIMIT;
110		if (glob(argv[argc], flags, NULL, &gl))
111			gargv[gargc++] = strdup(argv[argc]);
112		else if (gl.gl_pathc > 0) {
113			for (pop = gl.gl_pathv; *pop && gargc < (MAXGLOBARGS-1);
114			     pop++)
115				gargv[gargc++] = strdup(*pop);
116		}
117		globfree(&gl);
118	}
119	gargv[gargc] = NULL;
120
121	iop = NULL;
122	fflush(NULL);
123#ifdef BUILTIN_LS
124	pid = (strcmp(gargv[0], _PATH_LS) == 0) ? fork() : vfork();
125#else
126	pid = fork();
127#endif
128	switch(pid) {
129	case -1:			/* error */
130		(void)close(pdes[0]);
131		(void)close(pdes[1]);
132		goto pfree;
133		/* NOTREACHED */
134	case 0:				/* child */
135		if (*type == 'r') {
136			if (pdes[1] != STDOUT_FILENO) {
137				dup2(pdes[1], STDOUT_FILENO);
138				(void)close(pdes[1]);
139			}
140			dup2(STDOUT_FILENO, STDERR_FILENO); /* stderr too! */
141			(void)close(pdes[0]);
142		} else {
143			if (pdes[0] != STDIN_FILENO) {
144				dup2(pdes[0], STDIN_FILENO);
145				(void)close(pdes[0]);
146			}
147			(void)close(pdes[1]);
148		}
149#ifdef BUILTIN_LS
150		if (strcmp(gargv[0], _PATH_LS) == 0) {
151			/* Reset getopt for ls_main() */
152			optreset = optind = optopt = 1;
153			/* Close syslogging to remove pwd.db missing msgs */
154			closelog();
155			/* Trigger to sense new /etc/localtime after chroot */
156			if (getenv("TZ") == NULL) {
157				setenv("TZ", "", 0);
158				tzset();
159				unsetenv("TZ");
160				tzset();
161			}
162			exit(ls_main(gargc, gargv));
163		}
164#endif
165		execv(gargv[0], gargv);
166		_exit(1);
167	}
168	/* parent; assume fdopen can't fail...  */
169	if (*type == 'r') {
170		iop = fdopen(pdes[0], type);
171		(void)close(pdes[1]);
172	} else {
173		iop = fdopen(pdes[1], type);
174		(void)close(pdes[0]);
175	}
176	pids[fileno(iop)] = pid;
177
178pfree:	for (argc = 1; gargv[argc] != NULL; argc++)
179		free(gargv[argc]);
180
181	return (iop);
182}
183
184int
185ftpd_pclose(FILE *iop)
186{
187	int fdes, omask, status;
188	pid_t pid;
189
190	/*
191	 * pclose returns -1 if stream is not associated with a
192	 * `popened' command, or, if already `pclosed'.
193	 */
194	if (pids == NULL || pids[fdes = fileno(iop)] == 0)
195		return (-1);
196	(void)fclose(iop);
197	omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGHUP));
198	while ((pid = waitpid(pids[fdes], &status, 0)) < 0 && errno == EINTR)
199		continue;
200	(void)sigsetmask(omask);
201	pids[fdes] = 0;
202	if (pid < 0)
203		return (pid);
204	if (WIFEXITED(status))
205		return (WEXITSTATUS(status));
206	return (1);
207}
208