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