| 1 |
/* |
| 2 |
Copyright (C) 1994-2026 Free Software Foundation, Inc. |
| 3 |
|
| 4 |
This file is part of GNU Inetutils. |
| 5 |
|
| 6 |
GNU Inetutils is free software: you can redistribute it and/or modify |
| 7 |
it under the terms of the GNU General Public License as published by |
| 8 |
the Free Software Foundation, either version 3 of the License, or (at |
| 9 |
your option) any later version. |
| 10 |
|
| 11 |
GNU Inetutils is distributed in the hope that it will be useful, but |
| 12 |
WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
General Public License for more details. |
| 15 |
|
| 16 |
You should have received a copy of the GNU General Public License |
| 17 |
along with this program. If not, see `http://www.gnu.org/licenses/'. */ |
| 18 |
|
| 19 |
/* |
| 20 |
* Copyright (c) 1988, 1993, 1994 |
| 21 |
* The Regents of the University of California. All rights reserved. |
| 22 |
* |
| 23 |
* Redistribution and use in source and binary forms, with or without |
| 24 |
* modification, are permitted provided that the following conditions |
| 25 |
* are met: |
| 26 |
* 1. Redistributions of source code must retain the above copyright |
| 27 |
* notice, this list of conditions and the following disclaimer. |
| 28 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 29 |
* notice, this list of conditions and the following disclaimer in the |
| 30 |
* documentation and/or other materials provided with the distribution. |
| 31 |
* 3. Neither the name of the University nor the names of its contributors |
| 32 |
* may be used to endorse or promote products derived from this software |
| 33 |
* without specific prior written permission. |
| 34 |
* |
| 35 |
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 36 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 37 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 38 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 39 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 40 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 41 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 42 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 43 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 44 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 45 |
* SUCH DAMAGE. |
| 46 |
*/ |
| 47 |
|
| 48 |
/* This code is derived from software written by Ken Arnold and |
| 49 |
published in UNIX Review, Vol. 6, No. 8. */ |
| 50 |
|
| 51 |
#include <config.h> |
| 52 |
|
| 53 |
#include <sys/types.h> |
| 54 |
#include <sys/wait.h> |
| 55 |
|
| 56 |
#include <errno.h> |
| 57 |
#include <signal.h> |
| 58 |
#include <stdio.h> |
| 59 |
#include <stdlib.h> |
| 60 |
#include <string.h> |
| 61 |
#include <unistd.h> |
| 62 |
/* Include glob.h last, because it may define "const" which breaks |
| 63 |
system headers on some platforms. */ |
| 64 |
#include <glob.h> |
| 65 |
|
| 66 |
#include "extern.h" |
| 67 |
|
| 68 |
/* |
| 69 |
* Special version of popen which avoids call to shell. This ensures no one |
| 70 |
* may create a pipe to a hidden program as a side effect of a list or dir |
| 71 |
* command. |
| 72 |
*/ |
| 73 |
|
| 74 |
#define MAX_ARGC 100 |
| 75 |
#define MAX_GARGC 1000 |
| 76 |
|
| 77 |
struct file_pid |
| 78 |
{ |
| 79 |
FILE *file; |
| 80 |
pid_t pid; |
| 81 |
struct file_pid *next; |
| 82 |
}; |
| 83 |
|
| 84 |
/* A linked list associating ftpd_popen'd FILEs with pids. */ |
| 85 |
struct file_pid *file_pids = 0; |
| 86 |
|
| 87 |
#ifdef WITH_LIBLS |
| 88 |
extern int ls_main (int argc, char *argv[]); |
| 89 |
#endif |
| 90 |
|
| 91 |
FILE * |
| 92 |
ftpd_popen (char *program, const char *type) |
| 93 |
{ |
| 94 |
char *cp; |
| 95 |
FILE *iop; |
| 96 |
struct file_pid *fpid; |
| 97 |
int argc, gargc, pdes[2], pid; |
| 98 |
char **pop, *argv[MAX_ARGC], *gargv[MAX_GARGC]; |
| 99 |
|
| 100 |
if (((*type != 'r') && (*type != 'w')) || type[1]) |
| 101 |
return (NULL); |
| 102 |
|
| 103 |
if (pipe (pdes) < 0) |
| 104 |
return (NULL); |
| 105 |
|
| 106 |
/* break up string into pieces */ |
| 107 |
for (argc = 0, cp = program; argc < MAX_ARGC - 1; cp = NULL, argc++) |
| 108 |
if (!(argv[argc] = strtok (cp, " \t\n"))) |
| 109 |
break; |
| 110 |
argv[MAX_ARGC - 1] = NULL; |
| 111 |
|
| 112 |
/* glob each piece */ |
| 113 |
gargv[0] = argv[0]; |
| 114 |
for (gargc = argc = 1; argv[argc]; argc++) |
| 115 |
{ |
| 116 |
glob_t gl; |
| 117 |
int flags = GLOB_NOCHECK; |
| 118 |
|
| 119 |
#ifdef GLOB_BRACE |
| 120 |
flags |= GLOB_BRACE; |
| 121 |
#endif |
| 122 |
#ifdef GLOB_QUOTE |
| 123 |
flags |= GLOB_QUOTE; |
| 124 |
#endif |
| 125 |
#ifdef GLOB_TILDE |
| 126 |
flags |= GLOB_TILDE; |
| 127 |
#endif |
| 128 |
|
| 129 |
memset (&gl, 0, sizeof (gl)); |
| 130 |
if (glob (argv[argc], flags, NULL, &gl)) |
| 131 |
gargv[gargc++] = strdup (argv[argc]); |
| 132 |
else if (gl.gl_pathc > 0) |
| 133 |
for (pop = gl.gl_pathv; *pop && (gargc < MAX_GARGC - 1); pop++) |
| 134 |
gargv[gargc++] = strdup (*pop); |
| 135 |
globfree (&gl); |
| 136 |
} |
| 137 |
gargv[gargc] = NULL; |
| 138 |
|
| 139 |
iop = NULL; |
| 140 |
|
| 141 |
#ifdef WITH_LIBLS |
| 142 |
/* Do not use vfork() for internal ls. */ |
| 143 |
pid = (strcmp (gargv[0], "/bin/ls") == 0) ? fork () : vfork (); |
| 144 |
switch (pid) |
| 145 |
#else |
| 146 |
switch (pid = vfork ()) |
| 147 |
#endif |
| 148 |
{ |
| 149 |
case -1: /* error */ |
| 150 |
close (pdes[0]); |
| 151 |
close (pdes[1]); |
| 152 |
goto pfree; |
| 153 |
case 0: /* child */ |
| 154 |
if (*type == 'r') |
| 155 |
{ |
| 156 |
if (pdes[1] != STDOUT_FILENO) |
| 157 |
{ |
| 158 |
dup2 (pdes[1], STDOUT_FILENO); |
| 159 |
close (pdes[1]); |
| 160 |
} |
| 161 |
dup2 (STDOUT_FILENO, STDERR_FILENO); /* stderr too! */ |
| 162 |
close (pdes[0]); |
| 163 |
} |
| 164 |
else |
| 165 |
{ |
| 166 |
if (pdes[0] != STDIN_FILENO) |
| 167 |
{ |
| 168 |
dup2 (pdes[0], STDIN_FILENO); |
| 169 |
close (pdes[0]); |
| 170 |
} |
| 171 |
close (pdes[1]); |
| 172 |
} |
| 173 |
|
| 174 |
#ifdef WITH_LIBLS |
| 175 |
/* mvo: should this be a config-option? */ |
| 176 |
if (strcmp (gargv[0], "/bin/ls") == 0) |
| 177 |
{ |
| 178 |
optind = 0; |
| 179 |
exit (ls_main (gargc, gargv)); |
| 180 |
} |
| 181 |
#endif |
| 182 |
|
| 183 |
execv (gargv[0], gargv); |
| 184 |
_exit (EXIT_FAILURE); |
| 185 |
} |
| 186 |
/* parent; assume fdopen can't fail... */ |
| 187 |
if (*type == 'r') |
| 188 |
{ |
| 189 |
iop = fdopen (pdes[0], type); |
| 190 |
close (pdes[1]); |
| 191 |
} |
| 192 |
else |
| 193 |
{ |
| 194 |
iop = fdopen (pdes[1], type); |
| 195 |
close (pdes[0]); |
| 196 |
} |
| 197 |
|
| 198 |
fpid = (struct file_pid *) malloc (sizeof (struct file_pid)); |
| 199 |
if (fpid) |
| 200 |
{ |
| 201 |
fpid->file = iop; |
| 202 |
fpid->pid = pid; |
| 203 |
fpid->next = file_pids; |
| 204 |
file_pids = fpid; |
| 205 |
} |
| 206 |
|
| 207 |
pfree:for (argc = 1; gargv[argc] != NULL; argc++) |
| 208 |
free (gargv[argc]); |
| 209 |
|
| 210 |
return (iop); |
| 211 |
} |
| 212 |
|
| 213 |
int |
| 214 |
ftpd_pclose (FILE *iop) |
| 215 |
{ |
| 216 |
struct file_pid *fpid = file_pids, *prev_fpid = 0; |
| 217 |
int status; |
| 218 |
#ifdef HAVE_SIGACTION |
| 219 |
sigset_t sigs, osigs; |
| 220 |
#else |
| 221 |
int omask; |
| 222 |
#endif |
| 223 |
pid_t pid; |
| 224 |
|
| 225 |
/* |
| 226 |
* pclose returns -1 if stream is not associated with a |
| 227 |
* `popened' command, or, if already `pclosed'. |
| 228 |
*/ |
| 229 |
while (fpid && fpid->file != iop) |
| 230 |
{ |
| 231 |
prev_fpid = fpid; |
| 232 |
fpid = fpid->next; |
| 233 |
} |
| 234 |
if (!fpid) |
| 235 |
return -1; |
| 236 |
|
| 237 |
if (prev_fpid) |
| 238 |
prev_fpid->next = fpid->next; |
| 239 |
else |
| 240 |
file_pids = fpid->next; |
| 241 |
|
| 242 |
fclose (iop); |
| 243 |
#ifdef HAVE_SIGACTION |
| 244 |
sigemptyset (&sigs); |
| 245 |
sigaddset (&sigs, SIGINT); |
| 246 |
sigaddset (&sigs, SIGQUIT); |
| 247 |
sigaddset (&sigs, SIGHUP); |
| 248 |
sigprocmask (SIG_BLOCK, &sigs, &osigs); |
| 249 |
#else |
| 250 |
omask = sigblock (sigmask (SIGINT) | sigmask (SIGQUIT) | sigmask (SIGHUP)); |
| 251 |
#endif |
| 252 |
while ((pid = waitpid (fpid->pid, &status, 0)) < 0 && errno == EINTR) |
| 253 |
continue; |
| 254 |
|
| 255 |
free (fpid); |
| 256 |
|
| 257 |
#ifdef HAVE_SIGACTION |
| 258 |
sigprocmask (SIG_SETMASK, &osigs, 0); |
| 259 |
#else |
| 260 |
sigsetmask (omask); |
| 261 |
#endif |
| 262 |
if (pid < 0) |
| 263 |
return (pid); |
| 264 |
if (WIFEXITED (status)) |
| 265 |
return (WEXITSTATUS (status)); |
| 266 |
return (1); |
| 267 |
} |