xref: /haiku/src/bin/network/ftpd/popen.c (revision 0c84b78dd3334d625001d87ca08e789c99cb2384)
1*0c84b78dSAugustin Cavalier /*-
2*0c84b78dSAugustin Cavalier  * SPDX-License-Identifier: BSD-3-Clause
3*0c84b78dSAugustin Cavalier  *
404819365SAxel Dörfler  * Copyright (c) 1988, 1993, 1994
504819365SAxel Dörfler  *	The Regents of the University of California.  All rights reserved.
604819365SAxel Dörfler  *
704819365SAxel Dörfler  * This code is derived from software written by Ken Arnold and
804819365SAxel Dörfler  * published in UNIX Review, Vol. 6, No. 8.
904819365SAxel Dörfler  *
1004819365SAxel Dörfler  * Redistribution and use in source and binary forms, with or without
1104819365SAxel Dörfler  * modification, are permitted provided that the following conditions
1204819365SAxel Dörfler  * are met:
1304819365SAxel Dörfler  * 1. Redistributions of source code must retain the above copyright
1404819365SAxel Dörfler  *    notice, this list of conditions and the following disclaimer.
1504819365SAxel Dörfler  * 2. Redistributions in binary form must reproduce the above copyright
1604819365SAxel Dörfler  *    notice, this list of conditions and the following disclaimer in the
1704819365SAxel Dörfler  *    documentation and/or other materials provided with the distribution.
18*0c84b78dSAugustin Cavalier  * 3. Neither the name of the University nor the names of its contributors
1904819365SAxel Dörfler  *    may be used to endorse or promote products derived from this software
2004819365SAxel Dörfler  *    without specific prior written permission.
2104819365SAxel Dörfler  *
2204819365SAxel Dörfler  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2304819365SAxel Dörfler  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2404819365SAxel Dörfler  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2504819365SAxel Dörfler  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2604819365SAxel Dörfler  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2704819365SAxel Dörfler  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2804819365SAxel Dörfler  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2904819365SAxel Dörfler  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3004819365SAxel Dörfler  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3104819365SAxel Dörfler  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3204819365SAxel Dörfler  * SUCH DAMAGE.
3304819365SAxel Dörfler  */
3404819365SAxel Dörfler 
3504819365SAxel Dörfler #ifndef lint
3604819365SAxel Dörfler #if 0
3704819365SAxel Dörfler static char sccsid[] = "@(#)popen.c	8.3 (Berkeley) 4/6/94";
3804819365SAxel Dörfler #endif
3904819365SAxel Dörfler #endif /* not lint */
4004819365SAxel Dörfler 
4104819365SAxel Dörfler #include <sys/cdefs.h>
4204819365SAxel Dörfler #include <sys/types.h>
4304819365SAxel Dörfler #include <sys/wait.h>
4404819365SAxel Dörfler #include <netinet/in.h>
4504819365SAxel Dörfler 
4604819365SAxel Dörfler #include <errno.h>
4704819365SAxel Dörfler #include <glob.h>
4804819365SAxel Dörfler #include <signal.h>
4904819365SAxel Dörfler #include <stdio.h>
5004819365SAxel Dörfler #include <stdlib.h>
5104819365SAxel Dörfler #include <string.h>
5204819365SAxel Dörfler #include <unistd.h>
5304819365SAxel Dörfler 
5404819365SAxel Dörfler #include "extern.h"
5504819365SAxel Dörfler #include "pathnames.h"
5604819365SAxel Dörfler #include <syslog.h>
5704819365SAxel Dörfler #include <time.h>
5804819365SAxel Dörfler 
5904819365SAxel Dörfler #define	MAXUSRARGS	100
6004819365SAxel Dörfler #define	MAXGLOBARGS	1000
6104819365SAxel Dörfler 
6204819365SAxel Dörfler /*
6304819365SAxel Dörfler  * Special version of popen which avoids call to shell.  This ensures no one
6404819365SAxel Dörfler  * may create a pipe to a hidden program as a side effect of a list or dir
6504819365SAxel Dörfler  * command.
6604819365SAxel Dörfler  */
6704819365SAxel Dörfler static int *pids;
6804819365SAxel Dörfler static int fds;
6904819365SAxel Dörfler 
7004819365SAxel Dörfler FILE *
ftpd_popen(char * program,char * type)7104819365SAxel Dörfler ftpd_popen(char *program, char *type)
7204819365SAxel Dörfler {
7304819365SAxel Dörfler 	char *cp;
7404819365SAxel Dörfler 	FILE *iop;
7504819365SAxel Dörfler 	int argc, gargc, pdes[2], pid;
7604819365SAxel Dörfler 	char **pop, *argv[MAXUSRARGS], *gargv[MAXGLOBARGS];
7704819365SAxel Dörfler 
7804819365SAxel Dörfler 	if (((*type != 'r') && (*type != 'w')) || type[1])
7904819365SAxel Dörfler 		return (NULL);
8004819365SAxel Dörfler 
8104819365SAxel Dörfler 	if (!pids) {
8204819365SAxel Dörfler 		if ((fds = getdtablesize()) <= 0)
8304819365SAxel Dörfler 			return (NULL);
84925f9125SColdfirex 		if ((pids = calloc(fds, sizeof(int))) == NULL)
8504819365SAxel Dörfler 			return (NULL);
8604819365SAxel Dörfler 	}
8704819365SAxel Dörfler 	if (pipe(pdes) < 0)
8804819365SAxel Dörfler 		return (NULL);
8904819365SAxel Dörfler 
9004819365SAxel Dörfler 	/* break up string into pieces */
9104819365SAxel Dörfler 	for (argc = 0, cp = program; argc < MAXUSRARGS; cp = NULL) {
9204819365SAxel Dörfler 		if (!(argv[argc++] = strtok(cp, " \t\n")))
9304819365SAxel Dörfler 			break;
9404819365SAxel Dörfler 	}
9504819365SAxel Dörfler 	argv[argc - 1] = NULL;
9604819365SAxel Dörfler 
9704819365SAxel Dörfler 	/* glob each piece */
9804819365SAxel Dörfler 	gargv[0] = argv[0];
9904819365SAxel Dörfler 	for (gargc = argc = 1; argv[argc] && gargc < (MAXGLOBARGS-1); argc++) {
10004819365SAxel Dörfler 		glob_t gl;
10104819365SAxel Dörfler 		int flags = GLOB_BRACE|GLOB_NOCHECK|GLOB_TILDE;
10204819365SAxel Dörfler 
10304819365SAxel Dörfler 		memset(&gl, 0, sizeof(gl));
10404819365SAxel Dörfler 		gl.gl_matchc = MAXGLOBARGS;
10504819365SAxel Dörfler 		flags |= GLOB_LIMIT;
10604819365SAxel Dörfler 		if (glob(argv[argc], flags, NULL, &gl))
10704819365SAxel Dörfler 			gargv[gargc++] = strdup(argv[argc]);
108925f9125SColdfirex 		else if (gl.gl_pathc > 0) {
10904819365SAxel Dörfler 			for (pop = gl.gl_pathv; *pop && gargc < (MAXGLOBARGS-1);
11004819365SAxel Dörfler 			     pop++)
11104819365SAxel Dörfler 				gargv[gargc++] = strdup(*pop);
112925f9125SColdfirex 		}
11304819365SAxel Dörfler 		globfree(&gl);
11404819365SAxel Dörfler 	}
11504819365SAxel Dörfler 	gargv[gargc] = NULL;
11604819365SAxel Dörfler 
11704819365SAxel Dörfler 	iop = NULL;
11804819365SAxel Dörfler 	fflush(NULL);
11904819365SAxel Dörfler #ifdef BUILTIN_LS
12004819365SAxel Dörfler 	pid = (strcmp(gargv[0], _PATH_LS) == 0) ? fork() : vfork();
12104819365SAxel Dörfler #else
12204819365SAxel Dörfler 	pid = fork();
12304819365SAxel Dörfler #endif
12404819365SAxel Dörfler 	switch(pid) {
12504819365SAxel Dörfler 	case -1:			/* error */
12604819365SAxel Dörfler 		(void)close(pdes[0]);
12704819365SAxel Dörfler 		(void)close(pdes[1]);
12804819365SAxel Dörfler 		goto pfree;
12904819365SAxel Dörfler 		/* NOTREACHED */
13004819365SAxel Dörfler 	case 0:				/* child */
13104819365SAxel Dörfler 		if (*type == 'r') {
13204819365SAxel Dörfler 			if (pdes[1] != STDOUT_FILENO) {
13304819365SAxel Dörfler 				dup2(pdes[1], STDOUT_FILENO);
13404819365SAxel Dörfler 				(void)close(pdes[1]);
13504819365SAxel Dörfler 			}
13604819365SAxel Dörfler 			dup2(STDOUT_FILENO, STDERR_FILENO); /* stderr too! */
13704819365SAxel Dörfler 			(void)close(pdes[0]);
13804819365SAxel Dörfler 		} else {
13904819365SAxel Dörfler 			if (pdes[0] != STDIN_FILENO) {
14004819365SAxel Dörfler 				dup2(pdes[0], STDIN_FILENO);
14104819365SAxel Dörfler 				(void)close(pdes[0]);
14204819365SAxel Dörfler 			}
14304819365SAxel Dörfler 			(void)close(pdes[1]);
14404819365SAxel Dörfler 		}
14504819365SAxel Dörfler #ifdef BUILTIN_LS
146*0c84b78dSAugustin Cavalier 		/* Drop privileges before proceeding */
147*0c84b78dSAugustin Cavalier 		if (getuid() != geteuid() && setuid(geteuid()) < 0)
148*0c84b78dSAugustin Cavalier 			_exit(1);
14904819365SAxel Dörfler 		if (strcmp(gargv[0], _PATH_LS) == 0) {
15004819365SAxel Dörfler 			/* Reset getopt for ls_main() */
15104819365SAxel Dörfler 			optreset = optind = optopt = 1;
15204819365SAxel Dörfler 			/* Close syslogging to remove pwd.db missing msgs */
15304819365SAxel Dörfler 			closelog();
15404819365SAxel Dörfler 			/* Trigger to sense new /etc/localtime after chroot */
15504819365SAxel Dörfler 			if (getenv("TZ") == NULL) {
15604819365SAxel Dörfler 				setenv("TZ", "", 0);
15704819365SAxel Dörfler 				tzset();
15804819365SAxel Dörfler 				unsetenv("TZ");
15904819365SAxel Dörfler 				tzset();
16004819365SAxel Dörfler 			}
16104819365SAxel Dörfler 			exit(ls_main(gargc, gargv));
16204819365SAxel Dörfler 		}
16304819365SAxel Dörfler #endif
16404819365SAxel Dörfler 		execv(gargv[0], gargv);
16504819365SAxel Dörfler 		_exit(1);
16604819365SAxel Dörfler 	}
16704819365SAxel Dörfler 	/* parent; assume fdopen can't fail...  */
16804819365SAxel Dörfler 	if (*type == 'r') {
16904819365SAxel Dörfler 		iop = fdopen(pdes[0], type);
17004819365SAxel Dörfler 		(void)close(pdes[1]);
17104819365SAxel Dörfler 	} else {
17204819365SAxel Dörfler 		iop = fdopen(pdes[1], type);
17304819365SAxel Dörfler 		(void)close(pdes[0]);
17404819365SAxel Dörfler 	}
17504819365SAxel Dörfler 	pids[fileno(iop)] = pid;
17604819365SAxel Dörfler 
17704819365SAxel Dörfler pfree:	for (argc = 1; gargv[argc] != NULL; argc++)
17804819365SAxel Dörfler 		free(gargv[argc]);
17904819365SAxel Dörfler 
18004819365SAxel Dörfler 	return (iop);
18104819365SAxel Dörfler }
18204819365SAxel Dörfler 
18304819365SAxel Dörfler int
ftpd_pclose(FILE * iop)18404819365SAxel Dörfler ftpd_pclose(FILE *iop)
18504819365SAxel Dörfler {
18604819365SAxel Dörfler 	int fdes, omask, status;
18704819365SAxel Dörfler 	pid_t pid;
18804819365SAxel Dörfler 
18904819365SAxel Dörfler 	/*
19004819365SAxel Dörfler 	 * pclose returns -1 if stream is not associated with a
19104819365SAxel Dörfler 	 * `popened' command, or, if already `pclosed'.
19204819365SAxel Dörfler 	 */
193925f9125SColdfirex 	if (pids == NULL || pids[fdes = fileno(iop)] == 0)
19404819365SAxel Dörfler 		return (-1);
19504819365SAxel Dörfler 	(void)fclose(iop);
19604819365SAxel Dörfler 	omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGHUP));
19704819365SAxel Dörfler 	while ((pid = waitpid(pids[fdes], &status, 0)) < 0 && errno == EINTR)
19804819365SAxel Dörfler 		continue;
19904819365SAxel Dörfler 	(void)sigsetmask(omask);
20004819365SAxel Dörfler 	pids[fdes] = 0;
20104819365SAxel Dörfler 	if (pid < 0)
20204819365SAxel Dörfler 		return (pid);
20304819365SAxel Dörfler 	if (WIFEXITED(status))
20404819365SAxel Dörfler 		return (WEXITSTATUS(status));
20504819365SAxel Dörfler 	return (1);
20604819365SAxel Dörfler }
207