home *** CD-ROM | disk | FTP | other *** search
/ Big Green CD 8 / BGCD_8_Dev.iso / NEXTSTEP / UNIX / Networking / wu-ftpd-2.4.2b13-MIHS / src / popen.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-03-01  |  7.7 KB  |  275 lines

  1. /* Copyright (c) 1988 The Regents of the University of California. All rights
  2.  * reserved.
  3.  *
  4.  * This code is derived from software written by Ken Arnold and published in
  5.  * UNIX Review, Vol. 6, No. 8.
  6.  *
  7.  * Redistribution and use in source and binary forms, with or without
  8.  * modification, are permitted provided that the following conditions are
  9.  * met: 1. Redistributions of source code must retain the above copyright
  10.  * notice, this list of conditions and the following disclaimer. 2.
  11.  * Redistributions in binary form must reproduce the above copyright notice,
  12.  * this list of conditions and the following disclaimer in the documentation
  13.  * and/or other materials provided with the distribution. 3. All advertising
  14.  * materials mentioning features or use of this software must display the
  15.  * following acknowledgement: This product includes software developed by the
  16.  * University of California, Berkeley and its contributors. 4. Neither the
  17.  * name of the University nor the names of its contributors may be used to
  18.  * endorse or promote products derived from this software without specific
  19.  * prior written permission.
  20.  *
  21.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
  22.  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  23.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  24.  * DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR
  25.  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  27.  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  28.  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31.  * SUCH DAMAGE.
  32.  *
  33.  */
  34.  
  35. #ifndef lint
  36. static char sccsid[] = "@(#)popen.c 5.9 (Berkeley) 2/25/91";
  37. #endif /* not lint */
  38.  
  39. #include "config.h"
  40.  
  41. #include <sys/types.h>
  42. #include <sys/wait.h>
  43. #include <signal.h>
  44. #include <stdio.h>
  45. #include <string.h>
  46. #ifdef HAVE_GETRLIMIT
  47. #include <time.h>
  48. #include <sys/resource.h>
  49. #endif
  50.  
  51. /* 
  52.  * Special version of popen which avoids call to shell.  This insures noone
  53.  * may create a pipe to a hidden program as a side effect of a list or dir
  54.  * command. 
  55.  */
  56. static int *pids;
  57. static int fds;
  58. #define MAX_ARGV 100
  59. #define MAX_GARGV 1000
  60. FILE *
  61. #ifdef __STDC__
  62. ftpd_popen(char *program, char *type, int closestderr)
  63. #else
  64. ftpd_popen(program,type,closestderr)
  65. char *program;
  66. char *type;
  67. int closestderr;
  68. #endif
  69. {
  70.     register char *cp;
  71.     FILE *iop;
  72.     int argc,
  73.       gargc,
  74.       pdes[2],
  75.       pid,i;
  76.     char **pop,
  77.      *argv[MAX_ARGV],
  78.      *gargv[MAX_GARGV],
  79.      *vv[2];
  80. #ifdef __STDC__
  81.     extern char **ftpglob(register char *v),
  82.     **copyblk(register char **v),
  83.      *strspl(register char *cp, register char *dp);
  84. #else
  85.     extern char **ftpglob(),
  86.     **copyblk(),
  87.      *strspl();
  88. #endif
  89. #ifdef HAVE_GETRLIMIT
  90.         struct rlimit rlp;
  91.  
  92.         rlp.rlim_cur = rlp.rlim_max = RLIM_INFINITY;
  93.         if (getrlimit( RLIMIT_NOFILE, &rlp ) )
  94.             return(NULL);
  95.         fds = rlp.rlim_cur;
  96. #else
  97. #ifdef HAVE_GETDTABLESIZE
  98.         if ((fds = getdtablesize()) <= 0)
  99.             return (NULL);
  100. #else
  101. #ifdef HAVE_SYSCONF
  102.        fds = sysconf(_SC_OPEN_MAX);
  103. #else
  104. #ifdef OPEN_MAX
  105.     fds=OPEN_MAX; /* need to include limits.h somehow */
  106. #else
  107.     fds = 31; /* XXX -- magic cookie*/
  108. #endif
  109. #endif
  110. #endif
  111. #endif
  112.     if (*type != 'r' && *type != 'w' || type[1])
  113.         return (NULL);
  114.  
  115.     if (!pids) {
  116.         if ((pids = (int *) malloc((u_int) (fds * sizeof(int)))) == NULL)
  117.               return (NULL);
  118. #ifdef USG
  119.         (void) memset((char *)pids, fds * sizeof(int), 0);
  120. #else
  121.         bzero((char *) pids, fds * sizeof(int));
  122. #endif
  123.     }
  124.     if (pipe(pdes) < 0)
  125.         return (NULL);
  126.  
  127.     /* break up string into pieces */
  128.     for (argc = 0, cp = program;argc < MAX_ARGV ; cp = NULL)
  129.         if (!(argv[argc++] = strtok(cp, " \t\n")))
  130.             break;
  131.  
  132.     /* glob each piece */
  133.     gargv[0] = argv[0];
  134.     for (gargc = argc = 1; argv[argc]; argc++) {
  135.         if (!(pop = ftpglob(argv[argc]))) { /* globbing failed */
  136.             vv[0] = strspl(argv[argc], "");
  137.             vv[1] = NULL;
  138.             pop = copyblk(vv);
  139.         }
  140.         argv[argc] = (char *) pop;  /* save to free later */
  141.         while (*pop && gargc < (MAX_GARGV - 1 ))
  142.             gargv[gargc++] = *pop++;
  143.     }
  144.     gargv[gargc] = NULL;
  145.  
  146.     iop = NULL;
  147.     switch (pid = vfork()) {
  148.     case -1:                    /* error */
  149.         (void) close(pdes[0]);
  150.         (void) close(pdes[1]);
  151.         goto pfree;
  152.         /* NOTREACHED */
  153.     case 0:                 /* child */
  154.         if (*type == 'r') {
  155.             if (pdes[1] != 1) {
  156.                 dup2(pdes[1], 1);
  157.                 if (closestderr)
  158.                     (void) close(2);
  159.                 else 
  160.                     dup2(pdes[1], 2);  /* stderr, too! */
  161.                 (void) close(pdes[1]);
  162.             }
  163.             (void) close(pdes[0]);
  164.         } else {
  165.             if (pdes[0] != 0) {
  166.                 dup2(pdes[0], 0);
  167.                 (void) close(pdes[0]);
  168.             }
  169.             (void) close(pdes[1]);
  170.         }
  171.     for (i = 3; i < fds; i++)
  172.                 close(i);
  173.     /* begin CERT suggested fixes */
  174.     close(0); 
  175.         i = geteuid();
  176.     delay_signaling(); /* we can't allow any signals while euid==0: kinch */
  177.         seteuid(0);
  178.         setgid(getegid());
  179.         setuid(i);
  180.     enable_signaling(); /* we can allow signals once again: kinch */
  181.     /* end CERT suggested fixes */
  182.     execv(gargv[0], gargv);
  183.         _exit(1);
  184.     }
  185.     /* parent; assume fdopen can't fail...  */
  186.     if (*type == 'r') {
  187.         iop = fdopen(pdes[0], type);
  188.         (void) close(pdes[1]);
  189.     } else {
  190.         iop = fdopen(pdes[1], type);
  191.         (void) close(pdes[0]);
  192.     }
  193.     pids[fileno(iop)] = pid;
  194.  
  195.   pfree:for (argc = 1; argv[argc] != NULL; argc++) {
  196.         blkfree((char **) argv[argc]);
  197.         free((char *) argv[argc]);
  198.     }
  199.     return (iop);
  200. }
  201.  
  202. #ifdef __STDC__
  203. ftpd_pclose(FILE * iop)
  204. #else
  205. ftpd_pclose(iop)
  206. FILE * iop;
  207. #endif
  208. {
  209.     register int fdes;
  210.     int pid;
  211. #if defined (SVR4)
  212.     sigset_t sig, omask;
  213.     int stat_loc;
  214.     sigemptyset(&sig);
  215.     sigaddset(&sig, SIGINT); sigaddset(&sig,SIGQUIT); sigaddset(&sig, SIGHUP);
  216. #elif defined (_OSF_SOURCE)
  217.     int omask;
  218.     int status;
  219. #elif defined(M_UNIX)
  220.     int stat_loc;
  221.     sigset_t oldmask, newmask;
  222. #else
  223.     int omask;
  224.     union wait stat_loc;
  225. #endif
  226.  
  227.  
  228.     /* pclose returns -1 if stream is not associated with a `popened'
  229.      * command, or, if already `pclosed'. */
  230.     if (pids == 0 || pids[fdes = fileno(iop)] == 0)
  231.         return (-1);
  232.     (void) fclose(iop);
  233. #ifdef SVR4
  234.     sigprocmask( SIG_BLOCK, &sig, &omask);
  235. #elif defined(M_UNIX)
  236.     sigemptyset(&newmask);
  237.     sigaddset(&newmask, SIGINT);
  238.     sigaddset(&newmask, SIGQUIT);
  239.     sigaddset(&newmask, SIGHUP);
  240.     (void) sigprocmask(SIG_BLOCK, &newmask ,&oldmask);
  241. #else
  242.     omask = sigblock(sigmask(SIGINT) | sigmask(SIGQUIT) | sigmask(SIGHUP));
  243. #endif
  244.  
  245. #ifdef _OSF_SOURCE
  246.     while ((pid = wait(&status)) != pids[fdes] && pid != -1) ;
  247. #else
  248. #ifndef NeXT
  249.     while ((pid = wait((int *) &stat_loc)) != pids[fdes] && pid != -1) ;
  250. #else
  251.     while ((pid = wait(&stat_loc)) != pids[fdes] && pid != -1) ;
  252. #endif
  253. #endif
  254.     pids[fdes] = 0;
  255. #ifdef SVR4
  256.     sigprocmask( SIG_SETMASK, &omask, (sigset_t *)NULL);
  257.     return(pid == -1 ? -1 : WEXITSTATUS(stat_loc));
  258. #elif defined(M_UNIX)
  259.     (void) sigprocmask(SIG_SETMASK, &oldmask, (sigset_t)0);
  260. #else
  261.     (void)sigsetmask(omask);
  262. #ifdef _OSF_SOURCE
  263.     return (pid == -1 ? -1 : status);
  264. #elif defined(M_UNIX) || defined(LINUX)
  265.     return (pid == -1 ? -1 : WEXITSTATUS(stat_loc));
  266. #else
  267.     return (pid == -1 ? -1 : stat_loc.w_status);
  268. #endif
  269. #endif
  270. }
  271.  
  272.  
  273.  
  274.  
  275.