1 /*- 2 * Copyright (c) 2002 Tim J. Robbins. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 * 26 * $FreeBSD: src/lib/libc/gen/wordexp.c,v 1.6 2004/06/30 13:55:08 tjr Exp $ 27 */ 28 29 #include "namespace.h" 30 #include <sys/types.h> 31 #include <sys/wait.h> 32 #include <fcntl.h> 33 #include <paths.h> 34 #include <stdio.h> 35 #include <stdlib.h> 36 #include <string.h> 37 #include <fcntl.h> 38 #include <unistd.h> 39 #include <wordexp.h> 40 #include "un-namespace.h" 41 42 static int we_askshell(const char *, wordexp_t *, int); 43 static int we_check(const char *, int); 44 45 /* 46 * wordexp -- 47 * Perform shell word expansion on `words' and place the resulting list 48 * of words in `we'. See wordexp(3). 49 * 50 * Specified by IEEE Std. 1003.1-2001. 51 */ 52 int 53 wordexp(const char * __restrict words, wordexp_t * __restrict we, int flags) 54 { 55 int error; 56 57 if (flags & WRDE_REUSE) 58 wordfree(we); 59 if ((flags & WRDE_APPEND) == 0) { 60 we->we_wordc = 0; 61 we->we_wordv = NULL; 62 we->we_strings = NULL; 63 we->we_nbytes = 0; 64 } 65 if ((error = we_check(words, flags)) != 0) { 66 wordfree(we); 67 return (error); 68 } 69 if ((error = we_askshell(words, we, flags)) != 0) { 70 wordfree(we); 71 return (error); 72 } 73 return (0); 74 } 75 76 /* 77 * we_askshell -- 78 * Use the `wordexp' /bin/sh builtin function to do most of the work 79 * in expanding the word string. This function is complicated by 80 * memory management. 81 */ 82 static int 83 we_askshell(const char *words, wordexp_t *we, int flags) 84 { 85 int pdes[2]; /* Pipe to child */ 86 char bbuf[9]; /* Buffer for byte count */ 87 char wbuf[9]; /* Buffer for word count */ 88 long nwords, nbytes; /* Number of words, bytes from child */ 89 long i; /* Handy integer */ 90 size_t sofs; /* Offset into we->we_strings */ 91 size_t vofs; /* Offset into we->we_wordv */ 92 pid_t pid; /* Process ID of child */ 93 int status; /* Child exit status */ 94 char *ifs; /* IFS env. var. */ 95 char *np, *p; /* Handy pointers */ 96 char *nstrings; /* Temporary for realloc() */ 97 char **nwv; /* Temporary for realloc() */ 98 99 if ((ifs = getenv("IFS")) == NULL) 100 ifs = " \t\n"; 101 102 if (pipe2(pdes, O_CLOEXEC) < 0) 103 return (WRDE_NOSPACE); /* XXX */ 104 if ((pid = fork()) < 0) { 105 _close(pdes[0]); 106 _close(pdes[1]); 107 return (WRDE_NOSPACE); /* XXX */ 108 } 109 else if (pid == 0) { 110 /* 111 * We are the child; just get /bin/sh to run the wordexp 112 * builtin on `words'. 113 */ 114 int devnull; 115 char *cmd; 116 117 _close(pdes[0]); 118 _fcntl(pdes[1], F_SETFD, 0); 119 if (_dup2(pdes[1], STDOUT_FILENO) < 0) 120 _exit(1); 121 _close(pdes[1]); 122 if (asprintf(&cmd, "wordexp%c%s\n", *ifs, words) < 0) 123 _exit(1); 124 if ((flags & WRDE_SHOWERR) == 0) { 125 if ((devnull = _open(_PATH_DEVNULL, O_RDWR, 0666)) < 0) 126 _exit(1); 127 if (_dup2(devnull, STDERR_FILENO) < 0) 128 _exit(1); 129 _close(devnull); 130 } 131 execl(_PATH_BSHELL, "sh", flags & WRDE_UNDEF ? "-u" : "+u", 132 "-c", cmd, NULL); 133 _exit(1); 134 } 135 136 /* 137 * We are the parent; read the output of the shell wordexp function, 138 * which is a 32-bit hexadecimal word count, a 32-bit hexadecimal 139 * byte count (not including terminating null bytes), followed by 140 * the expanded words separated by nulls. 141 */ 142 _close(pdes[1]); 143 if (_read(pdes[0], wbuf, 8) != 8 || _read(pdes[0], bbuf, 8) != 8) { 144 _close(pdes[0]); 145 _waitpid(pid, &status, 0); 146 return (flags & WRDE_UNDEF ? WRDE_BADVAL : WRDE_SYNTAX); 147 } 148 wbuf[8] = bbuf[8] = '\0'; 149 nwords = strtol(wbuf, NULL, 16); 150 nbytes = strtol(bbuf, NULL, 16) + nwords; 151 152 /* 153 * Allocate or reallocate (when flags & WRDE_APPEND) the word vector 154 * and string storage buffers for the expanded words we're about to 155 * read from the child. 156 */ 157 sofs = we->we_nbytes; 158 vofs = we->we_wordc; 159 if ((flags & (WRDE_DOOFFS|WRDE_APPEND)) == (WRDE_DOOFFS|WRDE_APPEND)) 160 vofs += we->we_offs; 161 we->we_wordc += nwords; 162 we->we_nbytes += nbytes; 163 if ((nwv = realloc(we->we_wordv, (we->we_wordc + 1 + 164 (flags & WRDE_DOOFFS ? we->we_offs : 0)) * 165 sizeof(char *))) == NULL) { 166 _close(pdes[0]); 167 _waitpid(pid, &status, 0); 168 return (WRDE_NOSPACE); 169 } 170 we->we_wordv = nwv; 171 if ((nstrings = realloc(we->we_strings, we->we_nbytes)) == NULL) { 172 _close(pdes[0]); 173 _waitpid(pid, &status, 0); 174 return (WRDE_NOSPACE); 175 } 176 for (i = 0; i < vofs; i++) 177 if (we->we_wordv[i] != NULL) 178 we->we_wordv[i] += nstrings - we->we_strings; 179 we->we_strings = nstrings; 180 181 if (_read(pdes[0], we->we_strings + sofs, nbytes) != nbytes) { 182 _close(pdes[0]); 183 _waitpid(pid, &status, 0); 184 return (flags & WRDE_UNDEF ? WRDE_BADVAL : WRDE_SYNTAX); 185 } 186 187 if (_waitpid(pid, &status, 0) < 0 || !WIFEXITED(status) || 188 WEXITSTATUS(status) != 0) { 189 _close(pdes[0]); 190 return (flags & WRDE_UNDEF ? WRDE_BADVAL : WRDE_SYNTAX); 191 } 192 _close(pdes[0]); 193 194 /* 195 * Break the null-terminated expanded word strings out into 196 * the vector. 197 */ 198 if (vofs == 0 && flags & WRDE_DOOFFS) 199 while (vofs < we->we_offs) 200 we->we_wordv[vofs++] = NULL; 201 p = we->we_strings + sofs; 202 while (nwords-- != 0) { 203 we->we_wordv[vofs++] = p; 204 if ((np = memchr(p, '\0', nbytes)) == NULL) 205 return (WRDE_NOSPACE); /* XXX */ 206 nbytes -= np - p + 1; 207 p = np + 1; 208 } 209 we->we_wordv[vofs] = NULL; 210 211 return (0); 212 } 213 214 /* 215 * we_check -- 216 * Check that the string contains none of the following unquoted 217 * special characters: <newline> |&;<>(){} 218 * or command substitutions when WRDE_NOCMD is set in flags. 219 */ 220 static int 221 we_check(const char *words, int flags) 222 { 223 char c; 224 int dquote, level, quote, squote; 225 226 quote = squote = dquote = 0; 227 while ((c = *words++) != '\0') { 228 switch (c) { 229 case '\\': 230 quote ^= 1; 231 continue; 232 case '\'': 233 if (quote + dquote == 0) 234 squote ^= 1; 235 break; 236 case '"': 237 if (quote + squote == 0) 238 dquote ^= 1; 239 break; 240 case '`': 241 if (quote + squote == 0 && flags & WRDE_NOCMD) 242 return (WRDE_CMDSUB); 243 while ((c = *words++) != '\0' && c != '`') 244 if (c == '\\' && (c = *words++) == '\0') 245 break; 246 if (c == '\0') 247 return (WRDE_SYNTAX); 248 break; 249 case '|': case '&': case ';': case '<': case '>': 250 case '{': case '}': case '(': case ')': case '\n': 251 if (quote + squote + dquote == 0) 252 return (WRDE_BADCHAR); 253 break; 254 case '$': 255 if ((c = *words++) == '\0') 256 break; 257 else if (quote + squote == 0 && c == '(') { 258 if (flags & WRDE_NOCMD && *words != '(') 259 return (WRDE_CMDSUB); 260 level = 1; 261 while ((c = *words++) != '\0') { 262 if (c == '\\') { 263 if ((c = *words++) == '\0') 264 break; 265 } else if (c == '(') 266 level++; 267 else if (c == ')' && --level == 0) 268 break; 269 } 270 if (c == '\0' || level != 0) 271 return (WRDE_SYNTAX); 272 } else if (quote + squote == 0 && c == '{') { 273 level = 1; 274 while ((c = *words++) != '\0') { 275 if (c == '\\') { 276 if ((c = *words++) == '\0') 277 break; 278 } else if (c == '{') 279 level++; 280 else if (c == '}' && --level == 0) 281 break; 282 } 283 if (c == '\0' || level != 0) 284 return (WRDE_SYNTAX); 285 } else 286 c = *--words; 287 break; 288 default: 289 break; 290 } 291 quote = 0; 292 } 293 if (quote + squote + dquote != 0) 294 return (WRDE_SYNTAX); 295 296 return (0); 297 } 298 299 /* 300 * wordfree -- 301 * Free the result of wordexp(). See wordexp(3). 302 * 303 * Specified by IEEE Std. 1003.1-2001. 304 */ 305 void 306 wordfree(wordexp_t *we) 307 { 308 309 if (we == NULL) 310 return; 311 free(we->we_wordv); 312 free(we->we_strings); 313 we->we_wordv = NULL; 314 we->we_strings = NULL; 315 we->we_nbytes = 0; 316 we->we_wordc = 0; 317 } 318