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