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[] = 427f3dea24SPeter Wemm "$FreeBSD$"; 43e02897faSPhilippe Charnier #endif /* not lint */ 44ea022d16SRodney W. Grimes 45ea022d16SRodney W. Grimes #include <sys/types.h> 46ea022d16SRodney W. Grimes #include <sys/wait.h> 474dd8b5abSYoshinobu Inoue #include <netinet/in.h> 48ea022d16SRodney W. Grimes 49ea022d16SRodney W. Grimes #include <errno.h> 50ea022d16SRodney W. Grimes #include <glob.h> 51ea022d16SRodney W. Grimes #include <signal.h> 52ea022d16SRodney W. Grimes #include <stdio.h> 53ea022d16SRodney W. Grimes #include <stdlib.h> 54ea022d16SRodney W. Grimes #include <string.h> 55ea022d16SRodney W. Grimes #include <unistd.h> 56ea022d16SRodney W. Grimes 57ea022d16SRodney W. Grimes #include "extern.h" 58af85d782SDavid Nugent #include "pathnames.h" 5920d2e1eeSDaniel O'Callaghan #include <syslog.h> 6023ea9f7eSAndrey A. Chernov #include <time.h> 6120d2e1eeSDaniel O'Callaghan #include <varargs.h> 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 * 75e4bc453cSWarner Losh ftpd_popen(char *program, char *type) 76ea022d16SRodney W. Grimes { 77ea022d16SRodney W. Grimes char *cp; 78ea022d16SRodney W. Grimes FILE *iop; 79ea022d16SRodney W. Grimes int argc, gargc, pdes[2], pid; 80312c86cfSPaul Traina char **pop, *argv[MAXUSRARGS], *gargv[MAXGLOBARGS]; 81ea022d16SRodney W. Grimes 8239ea627dSPaul Traina if (((*type != 'r') && (*type != 'w')) || type[1]) 83ea022d16SRodney W. Grimes return (NULL); 84ea022d16SRodney W. Grimes 85ea022d16SRodney W. Grimes if (!pids) { 86ea022d16SRodney W. Grimes if ((fds = getdtablesize()) <= 0) 87ea022d16SRodney W. Grimes return (NULL); 88ea022d16SRodney W. Grimes if ((pids = (int *)malloc((u_int)(fds * sizeof(int)))) == NULL) 89ea022d16SRodney W. Grimes return (NULL); 90ea022d16SRodney W. Grimes memset(pids, 0, fds * sizeof(int)); 91ea022d16SRodney W. Grimes } 92ea022d16SRodney W. Grimes if (pipe(pdes) < 0) 93ea022d16SRodney W. Grimes return (NULL); 94ea022d16SRodney W. Grimes 95ea022d16SRodney W. Grimes /* break up string into pieces */ 96b81d7e37SDavid Greenman for (argc = 0, cp = program; argc < MAXUSRARGS; cp = NULL) { 97ea022d16SRodney W. Grimes if (!(argv[argc++] = strtok(cp, " \t\n"))) 98ea022d16SRodney W. Grimes break; 99b81d7e37SDavid Greenman } 100b81d7e37SDavid Greenman argv[argc - 1] = NULL; 101ea022d16SRodney W. Grimes 102ea022d16SRodney W. Grimes /* glob each piece */ 103ea022d16SRodney W. Grimes gargv[0] = argv[0]; 104312c86cfSPaul Traina for (gargc = argc = 1; argv[argc] && gargc < (MAXGLOBARGS-1); argc++) { 105ea022d16SRodney W. Grimes glob_t gl; 106ea022d16SRodney W. Grimes int flags = GLOB_BRACE|GLOB_NOCHECK|GLOB_QUOTE|GLOB_TILDE; 107ea022d16SRodney W. Grimes 108ea022d16SRodney W. Grimes memset(&gl, 0, sizeof(gl)); 1096d10cb2fSJonathan Lemon gl.gl_matchc = MAXGLOBARGS; 11075dc5f1aSMike Heffner flags |= GLOB_LIMIT; 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 fflush(NULL); 123f3503617SAndrey A. Chernov pid = (strcmp(gargv[0], _PATH_LS) == 0) ? fork() : vfork(); 124af85d782SDavid Nugent switch(pid) { 125ea022d16SRodney W. Grimes case -1: /* error */ 126ea022d16SRodney W. Grimes (void)close(pdes[0]); 127ea022d16SRodney W. Grimes (void)close(pdes[1]); 128ea022d16SRodney W. Grimes goto pfree; 129ea022d16SRodney W. Grimes /* NOTREACHED */ 130ea022d16SRodney W. Grimes case 0: /* child */ 131ea022d16SRodney W. Grimes if (*type == 'r') { 132ea022d16SRodney W. Grimes if (pdes[1] != STDOUT_FILENO) { 133ea022d16SRodney W. Grimes dup2(pdes[1], STDOUT_FILENO); 134ea022d16SRodney W. Grimes (void)close(pdes[1]); 135ea022d16SRodney W. Grimes } 136ea022d16SRodney W. Grimes dup2(STDOUT_FILENO, STDERR_FILENO); /* stderr too! */ 137ea022d16SRodney W. Grimes (void)close(pdes[0]); 138ea022d16SRodney W. Grimes } else { 139ea022d16SRodney W. Grimes if (pdes[0] != STDIN_FILENO) { 140ea022d16SRodney W. Grimes dup2(pdes[0], STDIN_FILENO); 141ea022d16SRodney W. Grimes (void)close(pdes[0]); 142ea022d16SRodney W. Grimes } 143ea022d16SRodney W. Grimes (void)close(pdes[1]); 144ea022d16SRodney W. Grimes } 145af85d782SDavid Nugent if (strcmp(gargv[0], _PATH_LS) == 0) { 146af85d782SDavid Nugent /* Reset getopt for ls_main() */ 147af85d782SDavid Nugent optreset = optind = optopt = 1; 14820d2e1eeSDaniel O'Callaghan /* Close syslogging to remove pwd.db missing msgs */ 14920d2e1eeSDaniel O'Callaghan closelog(); 15023ea9f7eSAndrey A. Chernov /* Trigger to sense new /etc/localtime after chroot */ 15123ea9f7eSAndrey A. Chernov if (getenv("TZ") == NULL) { 15223ea9f7eSAndrey A. Chernov setenv("TZ", "", 0); 15323ea9f7eSAndrey A. Chernov tzset(); 15423ea9f7eSAndrey A. Chernov unsetenv("TZ"); 15523ea9f7eSAndrey A. Chernov tzset(); 15623ea9f7eSAndrey A. Chernov } 157af85d782SDavid Nugent exit(ls_main(gargc, gargv)); 158af85d782SDavid Nugent } 15923ea9f7eSAndrey A. Chernov execv(gargv[0], gargv); 160ea022d16SRodney W. Grimes _exit(1); 161ea022d16SRodney W. Grimes } 162ea022d16SRodney W. Grimes /* parent; assume fdopen can't fail... */ 163ea022d16SRodney W. Grimes if (*type == 'r') { 164ea022d16SRodney W. Grimes iop = fdopen(pdes[0], type); 165ea022d16SRodney W. Grimes (void)close(pdes[1]); 166ea022d16SRodney W. Grimes } else { 167ea022d16SRodney W. Grimes iop = fdopen(pdes[1], type); 168ea022d16SRodney W. Grimes (void)close(pdes[0]); 169ea022d16SRodney W. Grimes } 170ea022d16SRodney W. Grimes pids[fileno(iop)] = pid; 171ea022d16SRodney W. Grimes 172ea022d16SRodney W. Grimes pfree: for (argc = 1; gargv[argc] != NULL; argc++) 173ea022d16SRodney W. Grimes free(gargv[argc]); 174ea022d16SRodney W. Grimes 175ea022d16SRodney W. Grimes return (iop); 176ea022d16SRodney W. Grimes } 177ea022d16SRodney W. Grimes 178ea022d16SRodney W. Grimes int 179e4bc453cSWarner Losh ftpd_pclose(FILE *iop) 180ea022d16SRodney W. Grimes { 181ea022d16SRodney W. Grimes int fdes, omask, status; 182ea022d16SRodney W. Grimes pid_t pid; 183ea022d16SRodney W. Grimes 184ea022d16SRodney W. Grimes /* 185ea022d16SRodney W. Grimes * pclose returns -1 if stream is not associated with a 186ea022d16SRodney W. Grimes * `popened' command, or, if already `pclosed'. 187ea022d16SRodney W. Grimes */ 188ea022d16SRodney W. Grimes if (pids == 0 || pids[fdes = fileno(iop)] == 0) 189ea022d16SRodney W. Grimes return (-1); 190ea022d16SRodney W. Grimes (void)fclose(iop); 191ea022d16SRodney W. Grimes omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGHUP)); 192ea022d16SRodney W. Grimes while ((pid = waitpid(pids[fdes], &status, 0)) < 0 && errno == EINTR) 193ea022d16SRodney W. Grimes continue; 194ea022d16SRodney W. Grimes (void)sigsetmask(omask); 195ea022d16SRodney W. Grimes pids[fdes] = 0; 196ea022d16SRodney W. Grimes if (pid < 0) 197ea022d16SRodney W. Grimes return (pid); 198ea022d16SRodney W. Grimes if (WIFEXITED(status)) 199ea022d16SRodney W. Grimes return (WEXITSTATUS(status)); 200ea022d16SRodney W. Grimes return (1); 201ea022d16SRodney W. Grimes } 202