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. 165efaea4cSChristian Brueffer * 3. Neither the name of the University nor the names of its contributors 17ea022d16SRodney W. Grimes * may be used to endorse or promote products derived from this software 18ea022d16SRodney W. Grimes * without specific prior written permission. 19ea022d16SRodney W. Grimes * 20ea022d16SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21ea022d16SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22ea022d16SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23ea022d16SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24ea022d16SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25ea022d16SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26ea022d16SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27ea022d16SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28ea022d16SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29ea022d16SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30ea022d16SRodney W. Grimes * SUCH DAMAGE. 31ea022d16SRodney W. Grimes */ 32148531efSWolfram Schneider 33ea022d16SRodney W. Grimes #ifndef lint 34e02897faSPhilippe Charnier #if 0 35ea022d16SRodney W. Grimes static char sccsid[] = "@(#)popen.c 8.3 (Berkeley) 4/6/94"; 369aca17cbSMark Murray #endif 37e02897faSPhilippe Charnier #endif /* not lint */ 38ea022d16SRodney W. Grimes 390c4b401fSYaroslav Tykhiy #include <sys/cdefs.h> 400c4b401fSYaroslav Tykhiy __FBSDID("$FreeBSD$"); 410c4b401fSYaroslav Tykhiy 42ea022d16SRodney W. Grimes #include <sys/types.h> 43ea022d16SRodney W. Grimes #include <sys/wait.h> 444dd8b5abSYoshinobu Inoue #include <netinet/in.h> 45ea022d16SRodney W. Grimes 46ea022d16SRodney W. Grimes #include <errno.h> 47ea022d16SRodney W. Grimes #include <glob.h> 48ea022d16SRodney W. Grimes #include <signal.h> 49ea022d16SRodney W. Grimes #include <stdio.h> 50ea022d16SRodney W. Grimes #include <stdlib.h> 51ea022d16SRodney W. Grimes #include <string.h> 52ea022d16SRodney W. Grimes #include <unistd.h> 53ea022d16SRodney W. Grimes 54ea022d16SRodney W. Grimes #include "extern.h" 55af85d782SDavid Nugent #include "pathnames.h" 5620d2e1eeSDaniel O'Callaghan #include <syslog.h> 5723ea9f7eSAndrey A. Chernov #include <time.h> 58ea022d16SRodney W. Grimes 59312c86cfSPaul Traina #define MAXUSRARGS 100 60312c86cfSPaul Traina #define MAXGLOBARGS 1000 61312c86cfSPaul Traina 62ea022d16SRodney W. Grimes /* 63ea022d16SRodney W. Grimes * Special version of popen which avoids call to shell. This ensures noone 64ea022d16SRodney W. Grimes * may create a pipe to a hidden program as a side effect of a list or dir 65ea022d16SRodney W. Grimes * command. 66ea022d16SRodney W. Grimes */ 67ea022d16SRodney W. Grimes static int *pids; 68ea022d16SRodney W. Grimes static int fds; 69ea022d16SRodney W. Grimes 70ea022d16SRodney W. Grimes FILE * 71e4bc453cSWarner Losh ftpd_popen(char *program, char *type) 72ea022d16SRodney W. Grimes { 73ea022d16SRodney W. Grimes char *cp; 74ea022d16SRodney W. Grimes FILE *iop; 75ea022d16SRodney W. Grimes int argc, gargc, pdes[2], pid; 76312c86cfSPaul Traina char **pop, *argv[MAXUSRARGS], *gargv[MAXGLOBARGS]; 77ea022d16SRodney W. Grimes 7839ea627dSPaul Traina if (((*type != 'r') && (*type != 'w')) || type[1]) 79ea022d16SRodney W. Grimes return (NULL); 80ea022d16SRodney W. Grimes 81ea022d16SRodney W. Grimes if (!pids) { 82ea022d16SRodney W. Grimes if ((fds = getdtablesize()) <= 0) 83ea022d16SRodney W. Grimes return (NULL); 84*6e4fcca0SPedro F. Giffuni if ((pids = calloc(fds, sizeof(int))) == NULL) 85ea022d16SRodney W. Grimes return (NULL); 86ea022d16SRodney W. Grimes } 87ea022d16SRodney W. Grimes if (pipe(pdes) < 0) 88ea022d16SRodney W. Grimes return (NULL); 89ea022d16SRodney W. Grimes 90ea022d16SRodney W. Grimes /* break up string into pieces */ 91b81d7e37SDavid Greenman for (argc = 0, cp = program; argc < MAXUSRARGS; cp = NULL) { 92ea022d16SRodney W. Grimes if (!(argv[argc++] = strtok(cp, " \t\n"))) 93ea022d16SRodney W. Grimes break; 94b81d7e37SDavid Greenman } 95b81d7e37SDavid Greenman argv[argc - 1] = NULL; 96ea022d16SRodney W. Grimes 97ea022d16SRodney W. Grimes /* glob each piece */ 98ea022d16SRodney W. Grimes gargv[0] = argv[0]; 99312c86cfSPaul Traina for (gargc = argc = 1; argv[argc] && gargc < (MAXGLOBARGS-1); argc++) { 100ea022d16SRodney W. Grimes glob_t gl; 10112da320bSMike Heffner int flags = GLOB_BRACE|GLOB_NOCHECK|GLOB_TILDE; 102ea022d16SRodney W. Grimes 103ea022d16SRodney W. Grimes memset(&gl, 0, sizeof(gl)); 1046d10cb2fSJonathan Lemon gl.gl_matchc = MAXGLOBARGS; 10575dc5f1aSMike Heffner flags |= GLOB_LIMIT; 106ea022d16SRodney W. Grimes if (glob(argv[argc], flags, NULL, &gl)) 107ea022d16SRodney W. Grimes gargv[gargc++] = strdup(argv[argc]); 108d56cc559SXin LI else if (gl.gl_pathc > 0) { 109312c86cfSPaul Traina for (pop = gl.gl_pathv; *pop && gargc < (MAXGLOBARGS-1); 110312c86cfSPaul Traina pop++) 111ea022d16SRodney W. Grimes gargv[gargc++] = strdup(*pop); 112d56cc559SXin LI } 113ea022d16SRodney W. Grimes globfree(&gl); 114ea022d16SRodney W. Grimes } 115ea022d16SRodney W. Grimes gargv[gargc] = NULL; 116ea022d16SRodney W. Grimes 117ea022d16SRodney W. Grimes iop = NULL; 118af85d782SDavid Nugent fflush(NULL); 119f3503617SAndrey A. Chernov pid = (strcmp(gargv[0], _PATH_LS) == 0) ? fork() : vfork(); 120af85d782SDavid Nugent switch(pid) { 121ea022d16SRodney W. Grimes case -1: /* error */ 122ea022d16SRodney W. Grimes (void)close(pdes[0]); 123ea022d16SRodney W. Grimes (void)close(pdes[1]); 124ea022d16SRodney W. Grimes goto pfree; 125ea022d16SRodney W. Grimes /* NOTREACHED */ 126ea022d16SRodney W. Grimes case 0: /* child */ 127ea022d16SRodney W. Grimes if (*type == 'r') { 128ea022d16SRodney W. Grimes if (pdes[1] != STDOUT_FILENO) { 129ea022d16SRodney W. Grimes dup2(pdes[1], STDOUT_FILENO); 130ea022d16SRodney W. Grimes (void)close(pdes[1]); 131ea022d16SRodney W. Grimes } 132ea022d16SRodney W. Grimes dup2(STDOUT_FILENO, STDERR_FILENO); /* stderr too! */ 133ea022d16SRodney W. Grimes (void)close(pdes[0]); 134ea022d16SRodney W. Grimes } else { 135ea022d16SRodney W. Grimes if (pdes[0] != STDIN_FILENO) { 136ea022d16SRodney W. Grimes dup2(pdes[0], STDIN_FILENO); 137ea022d16SRodney W. Grimes (void)close(pdes[0]); 138ea022d16SRodney W. Grimes } 139ea022d16SRodney W. Grimes (void)close(pdes[1]); 140ea022d16SRodney W. Grimes } 1413e65b9c6SColin Percival /* Drop privileges before proceeding */ 1423e65b9c6SColin Percival if (getuid() != geteuid() && setuid(geteuid()) < 0) 1433e65b9c6SColin Percival _exit(1); 144af85d782SDavid Nugent if (strcmp(gargv[0], _PATH_LS) == 0) { 145af85d782SDavid Nugent /* Reset getopt for ls_main() */ 146af85d782SDavid Nugent optreset = optind = optopt = 1; 14720d2e1eeSDaniel O'Callaghan /* Close syslogging to remove pwd.db missing msgs */ 14820d2e1eeSDaniel O'Callaghan closelog(); 14923ea9f7eSAndrey A. Chernov /* Trigger to sense new /etc/localtime after chroot */ 15023ea9f7eSAndrey A. Chernov if (getenv("TZ") == NULL) { 15123ea9f7eSAndrey A. Chernov setenv("TZ", "", 0); 15223ea9f7eSAndrey A. Chernov tzset(); 15323ea9f7eSAndrey A. Chernov unsetenv("TZ"); 15423ea9f7eSAndrey A. Chernov tzset(); 15523ea9f7eSAndrey A. Chernov } 156af85d782SDavid Nugent exit(ls_main(gargc, gargv)); 157af85d782SDavid Nugent } 15823ea9f7eSAndrey A. Chernov execv(gargv[0], gargv); 159ea022d16SRodney W. Grimes _exit(1); 160ea022d16SRodney W. Grimes } 161ea022d16SRodney W. Grimes /* parent; assume fdopen can't fail... */ 162ea022d16SRodney W. Grimes if (*type == 'r') { 163ea022d16SRodney W. Grimes iop = fdopen(pdes[0], type); 164ea022d16SRodney W. Grimes (void)close(pdes[1]); 165ea022d16SRodney W. Grimes } else { 166ea022d16SRodney W. Grimes iop = fdopen(pdes[1], type); 167ea022d16SRodney W. Grimes (void)close(pdes[0]); 168ea022d16SRodney W. Grimes } 169ea022d16SRodney W. Grimes pids[fileno(iop)] = pid; 170ea022d16SRodney W. Grimes 171ea022d16SRodney W. Grimes pfree: for (argc = 1; gargv[argc] != NULL; argc++) 172ea022d16SRodney W. Grimes free(gargv[argc]); 173ea022d16SRodney W. Grimes 174ea022d16SRodney W. Grimes return (iop); 175ea022d16SRodney W. Grimes } 176ea022d16SRodney W. Grimes 177ea022d16SRodney W. Grimes int 178e4bc453cSWarner Losh ftpd_pclose(FILE *iop) 179ea022d16SRodney W. Grimes { 180ea022d16SRodney W. Grimes int fdes, omask, status; 181ea022d16SRodney W. Grimes pid_t pid; 182ea022d16SRodney W. Grimes 183ea022d16SRodney W. Grimes /* 184ea022d16SRodney W. Grimes * pclose returns -1 if stream is not associated with a 185ea022d16SRodney W. Grimes * `popened' command, or, if already `pclosed'. 186ea022d16SRodney W. Grimes */ 187*6e4fcca0SPedro F. Giffuni if (pids == NULL || pids[fdes = fileno(iop)] == 0) 188ea022d16SRodney W. Grimes return (-1); 189ea022d16SRodney W. Grimes (void)fclose(iop); 190ea022d16SRodney W. Grimes omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGHUP)); 191ea022d16SRodney W. Grimes while ((pid = waitpid(pids[fdes], &status, 0)) < 0 && errno == EINTR) 192ea022d16SRodney W. Grimes continue; 193ea022d16SRodney W. Grimes (void)sigsetmask(omask); 194ea022d16SRodney W. Grimes pids[fdes] = 0; 195ea022d16SRodney W. Grimes if (pid < 0) 196ea022d16SRodney W. Grimes return (pid); 197ea022d16SRodney W. Grimes if (WIFEXITED(status)) 198ea022d16SRodney W. Grimes return (WEXITSTATUS(status)); 199ea022d16SRodney W. Grimes return (1); 200ea022d16SRodney W. Grimes } 201