xref: /freebsd/libexec/ftpd/popen.c (revision 20d2e1ee59a61eebdb7ad5da5ccb79cfba48549e)
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[] =
4220d2e1eeSDaniel O'Callaghan 	"$Id: popen.c,v 1.9 1997/11/21 07:38:43 charnier 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>
6020d2e1eeSDaniel O'Callaghan #include <varargs.h>
61af85d782SDavid Nugent #endif
62ea022d16SRodney W. Grimes 
63312c86cfSPaul Traina #define	MAXUSRARGS	100
64312c86cfSPaul Traina #define	MAXGLOBARGS	1000
65312c86cfSPaul Traina 
66ea022d16SRodney W. Grimes /*
67ea022d16SRodney W. Grimes  * Special version of popen which avoids call to shell.  This ensures noone
68ea022d16SRodney W. Grimes  * may create a pipe to a hidden program as a side effect of a list or dir
69ea022d16SRodney W. Grimes  * command.
70ea022d16SRodney W. Grimes  */
71ea022d16SRodney W. Grimes static int *pids;
72ea022d16SRodney W. Grimes static int fds;
73ea022d16SRodney W. Grimes 
74ea022d16SRodney W. Grimes FILE *
75ea022d16SRodney W. Grimes ftpd_popen(program, type)
76ea022d16SRodney W. Grimes 	char *program, *type;
77ea022d16SRodney W. Grimes {
78ea022d16SRodney W. Grimes 	char *cp;
79ea022d16SRodney W. Grimes 	FILE *iop;
80ea022d16SRodney W. Grimes 	int argc, gargc, pdes[2], pid;
81312c86cfSPaul Traina 	char **pop, *argv[MAXUSRARGS], *gargv[MAXGLOBARGS];
82ea022d16SRodney W. Grimes 
8339ea627dSPaul Traina 	if (((*type != 'r') && (*type != 'w')) || type[1])
84ea022d16SRodney W. Grimes 		return (NULL);
85ea022d16SRodney W. Grimes 
86ea022d16SRodney W. Grimes 	if (!pids) {
87ea022d16SRodney W. Grimes 		if ((fds = getdtablesize()) <= 0)
88ea022d16SRodney W. Grimes 			return (NULL);
89ea022d16SRodney W. Grimes 		if ((pids = (int *)malloc((u_int)(fds * sizeof(int)))) == NULL)
90ea022d16SRodney W. Grimes 			return (NULL);
91ea022d16SRodney W. Grimes 		memset(pids, 0, fds * sizeof(int));
92ea022d16SRodney W. Grimes 	}
93ea022d16SRodney W. Grimes 	if (pipe(pdes) < 0)
94ea022d16SRodney W. Grimes 		return (NULL);
95ea022d16SRodney W. Grimes 
96ea022d16SRodney W. Grimes 	/* break up string into pieces */
97312c86cfSPaul Traina 	for (argc = 0, cp = program; argc < MAXUSRARGS; cp = NULL)
98ea022d16SRodney W. Grimes 		if (!(argv[argc++] = strtok(cp, " \t\n")))
99ea022d16SRodney W. Grimes 			break;
100ea022d16SRodney W. Grimes 
101ea022d16SRodney W. Grimes 	/* glob each piece */
102ea022d16SRodney W. Grimes 	gargv[0] = argv[0];
103312c86cfSPaul Traina 	for (gargc = argc = 1; argv[argc] && gargc < (MAXGLOBARGS-1); argc++) {
104ea022d16SRodney W. Grimes 		glob_t gl;
105ea022d16SRodney W. Grimes 		int flags = GLOB_BRACE|GLOB_NOCHECK|GLOB_QUOTE|GLOB_TILDE;
106ea022d16SRodney W. Grimes 
107ea022d16SRodney W. Grimes 		memset(&gl, 0, sizeof(gl));
108ea022d16SRodney W. Grimes 		if (glob(argv[argc], flags, NULL, &gl))
109ea022d16SRodney W. Grimes 			gargv[gargc++] = strdup(argv[argc]);
110ea022d16SRodney W. Grimes 		else
111312c86cfSPaul Traina 			for (pop = gl.gl_pathv; *pop && gargc < (MAXGLOBARGS-1);
112312c86cfSPaul Traina 			     pop++)
113ea022d16SRodney W. Grimes 				gargv[gargc++] = strdup(*pop);
114ea022d16SRodney W. Grimes 		globfree(&gl);
115ea022d16SRodney W. Grimes 	}
116ea022d16SRodney W. Grimes 	gargv[gargc] = NULL;
117ea022d16SRodney W. Grimes 
118ea022d16SRodney W. Grimes 	iop = NULL;
119af85d782SDavid Nugent #ifdef	INTERNAL_LS
120af85d782SDavid Nugent 	fflush(NULL);
121af85d782SDavid Nugent 	pid = (strcmp(gargv[0], _PATH_LS) == 0) ? fork() : vfork();
122af85d782SDavid Nugent #else
123af85d782SDavid Nugent 	pid = vfork();
124af85d782SDavid Nugent #endif
125af85d782SDavid Nugent 	switch(pid) {
126ea022d16SRodney W. Grimes 	case -1:			/* error */
127ea022d16SRodney W. Grimes 		(void)close(pdes[0]);
128ea022d16SRodney W. Grimes 		(void)close(pdes[1]);
129ea022d16SRodney W. Grimes 		goto pfree;
130ea022d16SRodney W. Grimes 		/* NOTREACHED */
131ea022d16SRodney W. Grimes 	case 0:				/* child */
132ea022d16SRodney W. Grimes 		if (*type == 'r') {
133ea022d16SRodney W. Grimes 			if (pdes[1] != STDOUT_FILENO) {
134ea022d16SRodney W. Grimes 				dup2(pdes[1], STDOUT_FILENO);
135ea022d16SRodney W. Grimes 				(void)close(pdes[1]);
136ea022d16SRodney W. Grimes 			}
137ea022d16SRodney W. Grimes 			dup2(STDOUT_FILENO, STDERR_FILENO); /* stderr too! */
138ea022d16SRodney W. Grimes 			(void)close(pdes[0]);
139ea022d16SRodney W. Grimes 		} else {
140ea022d16SRodney W. Grimes 			if (pdes[0] != STDIN_FILENO) {
141ea022d16SRodney W. Grimes 				dup2(pdes[0], STDIN_FILENO);
142ea022d16SRodney W. Grimes 				(void)close(pdes[0]);
143ea022d16SRodney W. Grimes 			}
144ea022d16SRodney W. Grimes 			(void)close(pdes[1]);
145ea022d16SRodney W. Grimes 		}
146af85d782SDavid Nugent #ifdef	INTERNAL_LS
147af85d782SDavid Nugent 		if (strcmp(gargv[0], _PATH_LS) == 0) {
148af85d782SDavid Nugent 			extern	int optreset;
149af85d782SDavid Nugent 			/* Reset getopt for ls_main() */
150af85d782SDavid Nugent 			optreset = optind = optopt = 1;
15120d2e1eeSDaniel O'Callaghan 			/* Close syslogging to remove pwd.db missing msgs */
15220d2e1eeSDaniel O'Callaghan 			closelog();
153af85d782SDavid Nugent 			exit(ls_main(gargc, gargv));
154af85d782SDavid Nugent 		}
155af85d782SDavid Nugent #endif
156ea022d16SRodney W. Grimes 		execv(gargv[0], gargv);
157ea022d16SRodney W. Grimes 		_exit(1);
158ea022d16SRodney W. Grimes 	}
159ea022d16SRodney W. Grimes 	/* parent; assume fdopen can't fail...  */
160ea022d16SRodney W. Grimes 	if (*type == 'r') {
161ea022d16SRodney W. Grimes 		iop = fdopen(pdes[0], type);
162ea022d16SRodney W. Grimes 		(void)close(pdes[1]);
163ea022d16SRodney W. Grimes 	} else {
164ea022d16SRodney W. Grimes 		iop = fdopen(pdes[1], type);
165ea022d16SRodney W. Grimes 		(void)close(pdes[0]);
166ea022d16SRodney W. Grimes 	}
167ea022d16SRodney W. Grimes 	pids[fileno(iop)] = pid;
168ea022d16SRodney W. Grimes 
169ea022d16SRodney W. Grimes pfree:	for (argc = 1; gargv[argc] != NULL; argc++)
170ea022d16SRodney W. Grimes 		free(gargv[argc]);
171ea022d16SRodney W. Grimes 
172ea022d16SRodney W. Grimes 	return (iop);
173ea022d16SRodney W. Grimes }
174ea022d16SRodney W. Grimes 
175ea022d16SRodney W. Grimes int
176ea022d16SRodney W. Grimes ftpd_pclose(iop)
177ea022d16SRodney W. Grimes 	FILE *iop;
178ea022d16SRodney W. Grimes {
179ea022d16SRodney W. Grimes 	int fdes, omask, status;
180ea022d16SRodney W. Grimes 	pid_t pid;
181ea022d16SRodney W. Grimes 
182ea022d16SRodney W. Grimes 	/*
183ea022d16SRodney W. Grimes 	 * pclose returns -1 if stream is not associated with a
184ea022d16SRodney W. Grimes 	 * `popened' command, or, if already `pclosed'.
185ea022d16SRodney W. Grimes 	 */
186ea022d16SRodney W. Grimes 	if (pids == 0 || pids[fdes = fileno(iop)] == 0)
187ea022d16SRodney W. Grimes 		return (-1);
188ea022d16SRodney W. Grimes 	(void)fclose(iop);
189ea022d16SRodney W. Grimes 	omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGHUP));
190ea022d16SRodney W. Grimes 	while ((pid = waitpid(pids[fdes], &status, 0)) < 0 && errno == EINTR)
191ea022d16SRodney W. Grimes 		continue;
192ea022d16SRodney W. Grimes 	(void)sigsetmask(omask);
193ea022d16SRodney W. Grimes 	pids[fdes] = 0;
194ea022d16SRodney W. Grimes 	if (pid < 0)
195ea022d16SRodney W. Grimes 		return (pid);
196ea022d16SRodney W. Grimes 	if (WIFEXITED(status))
197ea022d16SRodney W. Grimes 		return (WEXITSTATUS(status));
198ea022d16SRodney W. Grimes 	return (1);
199ea022d16SRodney W. Grimes }
200