| 1 | /* $OpenBSD: pat_rep.c,v 1.43 2017/09/16 07:42:34 otto Exp $ */ |
| 2 | /* $NetBSD: pat_rep.c,v 1.4 1995/03/21 09:07:33 cgd Exp $ */ |
| 3 | |
| 4 | /*- |
| 5 | * Copyright (c) 2018 |
| 6 | * mirabilos <[email protected]> |
| 7 | * Copyright (c) 1992 Keith Muller. |
| 8 | * Copyright (c) 1992, 1993 |
| 9 | * The Regents of the University of California. All rights reserved. |
| 10 | * |
| 11 | * This code is derived from software contributed to Berkeley by |
| 12 | * Keith Muller of the University of California, San Diego. |
| 13 | * |
| 14 | * Redistribution and use in source and binary forms, with or without |
| 15 | * modification, are permitted provided that the following conditions |
| 16 | * are met: |
| 17 | * 1. Redistributions of source code must retain the above copyright |
| 18 | * notice, this list of conditions and the following disclaimer. |
| 19 | * 2. Redistributions in binary form must reproduce the above copyright |
| 20 | * notice, this list of conditions and the following disclaimer in the |
| 21 | * documentation and/or other materials provided with the distribution. |
| 22 | * 3. Neither the name of the University nor the names of its contributors |
| 23 | * may be used to endorse or promote products derived from this software |
| 24 | * without specific prior written permission. |
| 25 | * |
| 26 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 27 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 28 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 29 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 30 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 31 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 32 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 33 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 34 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 35 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 36 | * SUCH DAMAGE. |
| 37 | */ |
| 38 | |
| 39 | #include <sys/types.h> |
| 40 | #include <sys/stat.h> |
| 41 | #include <regex.h> |
| 42 | #include <stdio.h> |
| 43 | #include <stdlib.h> |
| 44 | #include <string.h> |
| 45 | #if HAVE_STRINGS_H |
| 46 | #include <strings.h> |
| 47 | #endif |
| 48 | |
| 49 | #include "pax.h" |
| 50 | #include "extern.h" |
| 51 | |
| 52 | __RCSID("$MirOS: src/bin/pax/pat_rep.c,v 1.17 2018/12/13 07:09:11 tg Exp $"); |
| 53 | |
| 54 | /* |
| 55 | * data structure for storing user supplied replacement strings (-s) |
| 56 | */ |
| 57 | typedef struct replace { |
| 58 | char *nstr; /* the new string we will substitute with */ |
| 59 | regex_t rcmp; /* compiled regular expression used to match */ |
| 60 | int flgs; /* print conversions? global in operation? */ |
| 61 | #define PRNT 0x1 |
| 62 | #define GLOB 0x2 |
| 63 | struct replace *fow; /* pointer to next pattern */ |
| 64 | } REPLACE; |
| 65 | |
| 66 | /* |
| 67 | * routines to handle pattern matching, name modification (regular expression |
| 68 | * substitution and interactive renames), and destination name modification for |
| 69 | * copy (-rw). Both file name and link names are adjusted as required in these |
| 70 | * routines. |
| 71 | */ |
| 72 | |
| 73 | #define MAXSUBEXP 10 /* max subexpressions, DO NOT CHANGE */ |
| 74 | static PATTERN *pathead = NULL; /* file pattern match list head */ |
| 75 | static PATTERN *pattail = NULL; /* file pattern match list tail */ |
| 76 | static REPLACE *rephead = NULL; /* replacement string list head */ |
| 77 | static REPLACE *reptail = NULL; /* replacement string list tail */ |
| 78 | |
| 79 | static int rep_name(char *, size_t, int *, int); |
| 80 | static int tty_rename(ARCHD *); |
| 81 | static int fix_path(char *, int *, char *, int); |
| 82 | static int fn_match(char *, char *, char **); |
| 83 | static char * range_match(char *, int); |
| 84 | static int resub(regex_t *, regmatch_t *, char *, char *, char *, char *); |
| 85 | |
| 86 | /* |
| 87 | * rep_add() |
| 88 | * parses the -s replacement string; compiles the regular expression |
| 89 | * and stores the compiled value and it's replacement string together in |
| 90 | * replacement string list. Input to this function is of the form: |
| 91 | * /old/new/pg |
| 92 | * The first char in the string specifies the delimiter used by this |
| 93 | * replacement string. "Old" is a regular expression in "ed" format which |
| 94 | * is compiled by regcomp() and is applied to filenames. "new" is the |
| 95 | * substitution string; p and g are options flags for printing and global |
| 96 | * replacement (over the single filename) |
| 97 | * Return: |
| 98 | * 0 if a proper replacement string and regular expression was added to |
| 99 | * the list of replacement patterns; -1 otherwise. |
| 100 | */ |
| 101 | |
| 102 | int |
| 103 | rep_add(char *str) |
| 104 | { |
| 105 | char *pt1; |
| 106 | char *pt2; |
| 107 | REPLACE *rep; |
| 108 | int res; |
| 109 | char rebuf[BUFSIZ]; |
| 110 | |
| 111 | /* |
| 112 | * throw out the bad parameters |
| 113 | */ |
| 114 | if ((str == NULL) || (*str == '\0')) { |
| 115 | paxwarn(1, "Empty replacement string"); |
| 116 | return(-1); |
| 117 | } |
| 118 | |
| 119 | /* |
| 120 | * first character in the string specifies what the delimiter is for |
| 121 | * this expression |
| 122 | */ |
| 123 | for (pt1 = str+1; *pt1; pt1++) { |
| 124 | if (*pt1 == '\\') { |
| 125 | pt1++; |
| 126 | continue; |
| 127 | } |
| 128 | if (*pt1 == *str) |
| 129 | break; |
| 130 | } |
| 131 | if (*pt1 == '\0') { |
| 132 | paxwarn(1, "%s %s", "Invalid replacement string", str); |
| 133 | return (-1); |
| 134 | } |
| 135 | |
| 136 | /* |
| 137 | * allocate space for the node that handles this replacement pattern |
| 138 | * and split out the regular expression and try to compile it |
| 139 | */ |
| 140 | if ((rep = malloc(sizeof(REPLACE))) == NULL) { |
| 141 | paxwarn(1, "%s for %s", "Out of memory", |
| 142 | "replacement string"); |
| 143 | return (-1); |
| 144 | } |
| 145 | |
| 146 | *pt1 = '\0'; |
| 147 | if ((res = regcomp(&(rep->rcmp), str+1, 0)) != 0) { |
| 148 | regerror(res, &(rep->rcmp), rebuf, sizeof(rebuf)); |
| 149 | paxwarn(1, "%s while compiling regular expression %s", rebuf, str); |
| 150 | free(rep); |
| 151 | return(-1); |
| 152 | } |
| 153 | |
| 154 | /* |
| 155 | * put the delimiter back in case we need an error message and |
| 156 | * locate the delimiter at the end of the replacement string |
| 157 | * we then point the node at the new substitution string |
| 158 | */ |
| 159 | *pt1++ = *str; |
| 160 | for (pt2 = pt1; *pt2; pt2++) { |
| 161 | if (*pt2 == '\\') { |
| 162 | pt2++; |
| 163 | continue; |
| 164 | } |
| 165 | if (*pt2 == *str) |
| 166 | break; |
| 167 | } |
| 168 | if (*pt2 == '\0') { |
| 169 | regfree(&(rep->rcmp)); |
| 170 | free(rep); |
| 171 | paxwarn(1, "%s %s", "Invalid replacement string", str); |
| 172 | return (-1); |
| 173 | } |
| 174 | |
| 175 | *pt2 = '\0'; |
| 176 | rep->nstr = pt1; |
| 177 | pt1 = pt2++; |
| 178 | rep->flgs = 0; |
| 179 | |
| 180 | /* |
| 181 | * set the options if any |
| 182 | */ |
| 183 | while (*pt2 != '\0') { |
| 184 | switch (*pt2) { |
| 185 | case 'g': |
| 186 | case 'G': |
| 187 | rep->flgs |= GLOB; |
| 188 | break; |
| 189 | case 'p': |
| 190 | case 'P': |
| 191 | rep->flgs |= PRNT; |
| 192 | break; |
| 193 | default: |
| 194 | regfree(&(rep->rcmp)); |
| 195 | free(rep); |
| 196 | *pt1 = *str; |
| 197 | paxwarn(1, "%s option %s", |
| 198 | "Invalid replacement string", str); |
| 199 | return (-1); |
| 200 | } |
| 201 | ++pt2; |
| 202 | } |
| 203 | |
| 204 | /* |
| 205 | * all done, link it in at the end |
| 206 | */ |
| 207 | rep->fow = NULL; |
| 208 | if (rephead == NULL) { |
| 209 | reptail = rephead = rep; |
| 210 | return(0); |
| 211 | } |
| 212 | reptail->fow = rep; |
| 213 | reptail = rep; |
| 214 | return(0); |
| 215 | } |
| 216 | |
| 217 | /* |
| 218 | * pat_add() |
| 219 | * add a pattern match to the pattern match list. Pattern matches are used |
| 220 | * to select which archive members are extracted. (They appear as |
| 221 | * arguments to pax in the list and read modes). If no patterns are |
| 222 | * supplied to pax, all members in the archive will be selected (and the |
| 223 | * pattern match list is empty). |
| 224 | * Return: |
| 225 | * 0 if the pattern was added to the list, -1 otherwise |
| 226 | */ |
| 227 | |
| 228 | int |
| 229 | pat_add(char *str, char *chdirname) |
| 230 | { |
| 231 | PATTERN *pt; |
| 232 | |
| 233 | /* |
| 234 | * throw out the junk |
| 235 | */ |
| 236 | if ((str == NULL) || (*str == '\0')) { |
| 237 | paxwarn(1, "Empty pattern string"); |
| 238 | return(-1); |
| 239 | } |
| 240 | |
| 241 | /* |
| 242 | * allocate space for the pattern and store the pattern. the pattern is |
| 243 | * part of argv so do not bother to copy it, just point at it. Add the |
| 244 | * node to the end of the pattern list |
| 245 | */ |
| 246 | if ((pt = malloc(sizeof(PATTERN))) == NULL) { |
| 247 | paxwarn(1, "%s for %s", "Out of memory", |
| 248 | "pattern string"); |
| 249 | return (-1); |
| 250 | } |
| 251 | |
| 252 | pt->pstr = str; |
| 253 | pt->pend = NULL; |
| 254 | pt->plen = strlen(str); |
| 255 | pt->fow = NULL; |
| 256 | pt->flgs = 0; |
| 257 | pt->chdname = chdirname; |
| 258 | |
| 259 | if (pathead == NULL) { |
| 260 | pattail = pathead = pt; |
| 261 | return(0); |
| 262 | } |
| 263 | pattail->fow = pt; |
| 264 | pattail = pt; |
| 265 | return(0); |
| 266 | } |
| 267 | |
| 268 | /* |
| 269 | * pat_chk() |
| 270 | * complain if any the user supplied pattern did not result in a match to |
| 271 | * a selected archive member. |
| 272 | */ |
| 273 | |
| 274 | void |
| 275 | pat_chk(void) |
| 276 | { |
| 277 | PATTERN *pt; |
| 278 | int wban = 0; |
| 279 | |
| 280 | /* |
| 281 | * walk down the list checking the flags to make sure MTCH was set, |
| 282 | * if not complain |
| 283 | */ |
| 284 | for (pt = pathead; pt != NULL; pt = pt->fow) { |
| 285 | if (pt->flgs & MTCH) |
| 286 | continue; |
| 287 | if (!wban) { |
| 288 | paxwarn(1, "WARNING! These patterns were not matched:"); |
| 289 | ++wban; |
| 290 | } |
| 291 | (void)fprintf(stderr, "%s\n", pt->pstr); |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | /* |
| 296 | * pat_sel() |
| 297 | * the archive member which matches a pattern was selected. Mark the |
| 298 | * pattern as having selected an archive member. arcn->pat points at the |
| 299 | * pattern that was matched. arcn->pat is set in pat_match() |
| 300 | * |
| 301 | * NOTE: When the -c option is used, we are called when there was no match |
| 302 | * by pat_match() (that means we did match before the inverted sense of |
| 303 | * the logic). Now this seems really strange at first, but with -c we |
| 304 | * need to keep track of those patterns that cause an archive member to NOT |
| 305 | * be selected (it found an archive member with a specified pattern) |
| 306 | * Return: |
| 307 | * 0 if the pattern pointed at by arcn->pat was tagged as creating a |
| 308 | * match, -1 otherwise. |
| 309 | */ |
| 310 | |
| 311 | int |
| 312 | pat_sel(ARCHD *arcn) |
| 313 | { |
| 314 | PATTERN *pt; |
| 315 | PATTERN **ppt; |
| 316 | size_t len; |
| 317 | |
| 318 | /* |
| 319 | * if no patterns just return |
| 320 | */ |
| 321 | if ((pathead == NULL) || ((pt = arcn->pat) == NULL)) |
| 322 | return(0); |
| 323 | |
| 324 | /* |
| 325 | * when we are NOT limited to a single match per pattern mark the |
| 326 | * pattern and return |
| 327 | */ |
| 328 | if (!nflag) { |
| 329 | pt->flgs |= MTCH; |
| 330 | return(0); |
| 331 | } |
| 332 | |
| 333 | /* |
| 334 | * we reach this point only when we allow a single selected match per |
| 335 | * pattern, if the pattern matches a directory and we do not have -d |
| 336 | * (dflag) we are done with this pattern. We may also be handed a file |
| 337 | * in the subtree of a directory. in that case when we are operating |
| 338 | * with -d, this pattern was already selected and we are done |
| 339 | */ |
| 340 | if (pt->flgs & DIR_MTCH) |
| 341 | return(0); |
| 342 | |
| 343 | if (!dflag && ((pt->pend != NULL) || (arcn->type == PAX_DIR))) { |
| 344 | /* |
| 345 | * ok we matched a directory and we are allowing |
| 346 | * subtree matches but because of the -n only its children will |
| 347 | * match. This is tagged as a DIR_MTCH type. |
| 348 | * WATCH IT, the code assumes that pt->pend points |
| 349 | * into arcn->name and arcn->name has not been modified. |
| 350 | * If not we will have a big mess. Yup this is another kludge |
| 351 | */ |
| 352 | |
| 353 | /* |
| 354 | * if this was a prefix match, remove trailing part of path |
| 355 | * so we can copy it. Future matches will be exact prefix match |
| 356 | */ |
| 357 | if (pt->pend != NULL) |
| 358 | *pt->pend = '\0'; |
| 359 | |
| 360 | if ((pt->pstr = strdup(arcn->name)) == NULL) { |
| 361 | paxwarn(1, "%s for %s", "Out of memory", |
| 362 | "pattern select"); |
| 363 | if (pt->pend != NULL) |
| 364 | *pt->pend = '/'; |
| 365 | pt->pend = NULL; |
| 366 | return(-1); |
| 367 | } |
| 368 | |
| 369 | /* |
| 370 | * put the trailing / back in the source string |
| 371 | */ |
| 372 | if (pt->pend != NULL) { |
| 373 | *pt->pend = '/'; |
| 374 | pt->pend = NULL; |
| 375 | } |
| 376 | pt->plen = strlen(pt->pstr); |
| 377 | |
| 378 | /* |
| 379 | * strip off any trailing /, this should really never happen |
| 380 | */ |
| 381 | len = pt->plen - 1; |
| 382 | if (*(pt->pstr + len) == '/') { |
| 383 | *(pt->pstr + len) = '\0'; |
| 384 | pt->plen = len; |
| 385 | } |
| 386 | pt->flgs = DIR_MTCH | MTCH; |
| 387 | arcn->pat = pt; |
| 388 | return(0); |
| 389 | } |
| 390 | |
| 391 | /* |
| 392 | * we are then done with this pattern, so we delete it from the list |
| 393 | * because it can never be used for another match. |
| 394 | * Seems kind of strange to do for a -c, but the pax spec is really |
| 395 | * vague on the interaction of -c, -n and -d. We assume that when -c |
| 396 | * and the pattern rejects a member (i.e. it matched it) it is done. |
| 397 | * In effect we place the order of the flags as having -c last. |
| 398 | */ |
| 399 | pt = pathead; |
| 400 | ppt = &pathead; |
| 401 | while ((pt != NULL) && (pt != arcn->pat)) { |
| 402 | ppt = &(pt->fow); |
| 403 | pt = pt->fow; |
| 404 | } |
| 405 | |
| 406 | if (pt == NULL) { |
| 407 | /* |
| 408 | * should never happen.... |
| 409 | */ |
| 410 | paxwarn(1, "Pattern list inconsistent"); |
| 411 | return(-1); |
| 412 | } |
| 413 | *ppt = pt->fow; |
| 414 | free(pt); |
| 415 | arcn->pat = NULL; |
| 416 | return(0); |
| 417 | } |
| 418 | |
| 419 | /* |
| 420 | * pat_match() |
| 421 | * see if this archive member matches any supplied pattern, if a match |
| 422 | * is found, arcn->pat is set to point at the potential pattern. Later if |
| 423 | * this archive member is "selected" we process and mark the pattern as |
| 424 | * one which matched a selected archive member (see pat_sel()) |
| 425 | * Return: |
| 426 | * 0 if this archive member should be processed, 1 if it should be |
| 427 | * skipped and -1 if we are done with all patterns (and pax should quit |
| 428 | * looking for more members) |
| 429 | */ |
| 430 | |
| 431 | int |
| 432 | pat_match(ARCHD *arcn) |
| 433 | { |
| 434 | PATTERN *pt; |
| 435 | |
| 436 | arcn->pat = NULL; |
| 437 | |
| 438 | /* |
| 439 | * if there are no more patterns and we have -n (and not -c) we are |
| 440 | * done. otherwise with no patterns to match, matches all |
| 441 | */ |
| 442 | if (pathead == NULL) { |
| 443 | if (nflag && !cflag) |
| 444 | return(-1); |
| 445 | return(0); |
| 446 | } |
| 447 | |
| 448 | /* |
| 449 | * have to search down the list one at a time looking for a match. |
| 450 | */ |
| 451 | pt = pathead; |
| 452 | while (pt != NULL) { |
| 453 | /* |
| 454 | * check for a file name match unless we have DIR_MTCH set in |
| 455 | * this pattern then we want a prefix match |
| 456 | */ |
| 457 | if (pt->flgs & DIR_MTCH) { |
| 458 | /* |
| 459 | * this pattern was matched before to a directory |
| 460 | * as we must have -n set for this (but not -d). We can |
| 461 | * only match CHILDREN of that directory so we must use |
| 462 | * an exact prefix match (no wildcards). |
| 463 | */ |
| 464 | if ((arcn->name[pt->plen] == '/') && |
| 465 | (strncmp(pt->pstr, arcn->name, pt->plen) == 0)) |
| 466 | break; |
| 467 | } else if (fn_match(pt->pstr, arcn->name, &pt->pend) == 0) |
| 468 | break; |
| 469 | pt = pt->fow; |
| 470 | } |
| 471 | |
| 472 | /* |
| 473 | * return the result, remember that cflag (-c) inverts the sense of a |
| 474 | * match |
| 475 | */ |
| 476 | if (pt == NULL) |
| 477 | return(cflag ? 0 : 1); |
| 478 | |
| 479 | /* |
| 480 | * we had a match, now when we invert the sense (-c) we reject this |
| 481 | * member. However we have to tag the pattern a being successful, (in a |
| 482 | * match, not in selecting a archive member) so we call pat_sel() here. |
| 483 | */ |
| 484 | arcn->pat = pt; |
| 485 | if (!cflag) |
| 486 | return(0); |
| 487 | |
| 488 | if (pat_sel(arcn) < 0) |
| 489 | return(-1); |
| 490 | arcn->pat = NULL; |
| 491 | return(1); |
| 492 | } |
| 493 | |
| 494 | /* |
| 495 | * fn_match() |
| 496 | * Return: |
| 497 | * 0 if this archive member should be processed, 1 if it should be |
| 498 | * skipped and -1 if we are done with all patterns (and pax should quit |
| 499 | * looking for more members) |
| 500 | * Note: *pend may be changed to show where the prefix ends. |
| 501 | */ |
| 502 | |
| 503 | static int |
| 504 | fn_match(char *pattern, char *string, char **pend) |
| 505 | { |
| 506 | char c; |
| 507 | char test; |
| 508 | |
| 509 | *pend = NULL; |
| 510 | for (;;) { |
| 511 | switch (c = *pattern++) { |
| 512 | case '\0': |
| 513 | /* |
| 514 | * Ok we found an exact match |
| 515 | */ |
| 516 | if (*string == '\0') |
| 517 | return(0); |
| 518 | |
| 519 | /* |
| 520 | * Check if it is a prefix match |
| 521 | */ |
| 522 | if ((dflag == 1) || (*string != '/')) |
| 523 | return(-1); |
| 524 | |
| 525 | /* |
| 526 | * It is a prefix match, remember where the trailing |
| 527 | * / is located |
| 528 | */ |
| 529 | *pend = string; |
| 530 | return(0); |
| 531 | case '?': |
| 532 | if ((test = *string++) == '\0') |
| 533 | return (-1); |
| 534 | break; |
| 535 | case '*': |
| 536 | c = *pattern; |
| 537 | /* |
| 538 | * collapse multiple asterisks |
| 539 | */ |
| 540 | while (c == '*') |
| 541 | c = *++pattern; |
| 542 | |
| 543 | /* |
| 544 | * optimised hack for pattern with a * at the end |
| 545 | */ |
| 546 | if (c == '\0') |
| 547 | return (0); |
| 548 | |
| 549 | /* |
| 550 | * General case, use recursion. |
| 551 | */ |
| 552 | while ((test = *string) != '\0') { |
| 553 | if (!fn_match(pattern, string, pend)) |
| 554 | return (0); |
| 555 | ++string; |
| 556 | } |
| 557 | return (-1); |
| 558 | case '[': |
| 559 | /* |
| 560 | * range match |
| 561 | */ |
| 562 | if (((test = *string++) == '\0') || |
| 563 | ((pattern = range_match(pattern, test)) == NULL)) |
| 564 | return (-1); |
| 565 | break; |
| 566 | case '\\': |
| 567 | if ((c = *pattern++) == '\0') |
| 568 | return (-1); |
| 569 | /* FALLTHROUGH */ |
| 570 | default: |
| 571 | if (c != *string++) |
| 572 | return (-1); |
| 573 | break; |
| 574 | } |
| 575 | } |
| 576 | /* NOTREACHED */ |
| 577 | } |
| 578 | |
| 579 | static char * |
| 580 | range_match(char *pattern, int test) |
| 581 | { |
| 582 | char c; |
| 583 | char c2; |
| 584 | int negate; |
| 585 | int ok = 0; |
| 586 | |
| 587 | if ((negate = (*pattern == '!')) != 0) |
| 588 | ++pattern; |
| 589 | |
| 590 | while ((c = *pattern++) != ']') { |
| 591 | /* |
| 592 | * Illegal pattern |
| 593 | */ |
| 594 | if (c == '\0') |
| 595 | return (NULL); |
| 596 | |
| 597 | if ((*pattern == '-') && ((c2 = pattern[1]) != '\0') && |
| 598 | (c2 != ']')) { |
| 599 | if ((c <= test) && (test <= c2)) |
| 600 | ok = 1; |
| 601 | pattern += 2; |
| 602 | } else if (c == test) |
| 603 | ok = 1; |
| 604 | } |
| 605 | return (ok == negate ? NULL : pattern); |
| 606 | } |
| 607 | |
| 608 | /* |
| 609 | * has_dotdot() |
| 610 | * Returns true iff the supplied path contains a ".." component. |
| 611 | */ |
| 612 | |
| 613 | int |
| 614 | has_dotdot(const char *path) |
| 615 | { |
| 616 | const char *p = path; |
| 617 | |
| 618 | while ((p = strstr(p, "..")) != NULL) { |
| 619 | if ((p == path || p[-1] == '/') && |
| 620 | (p[2] == '/' || p[2] == '\0')) |
| 621 | return (1); |
| 622 | p += 2; |
| 623 | } |
| 624 | return (0); |
| 625 | } |
| 626 | |
| 627 | /* |
| 628 | * mod_name() |
| 629 | * modify a selected file name. first attempt to apply replacement string |
| 630 | * expressions, then apply interactive file rename. We apply replacement |
| 631 | * string expressions to both filenames and file links (if we didn't the |
| 632 | * links would point to the wrong place, and we could never be able to |
| 633 | * move an archive that has a file link in it). When we rename files |
| 634 | * interactively, we store that mapping (old name to user input name) so |
| 635 | * if we spot any file links to the old file name in the future, we will |
| 636 | * know exactly how to fix the file link. |
| 637 | * Return: |
| 638 | * 0 continue to process file, 1 skip this file, -1 pax is finished |
| 639 | */ |
| 640 | |
| 641 | int |
| 642 | mod_name(ARCHD *arcn) |
| 643 | { |
| 644 | int res = 0; |
| 645 | |
| 646 | if (rmleadslash) { |
| 647 | /* CVE-2016-6321: completely skip names with dotdot in them */ |
| 648 | const char *p = arcn->name; |
| 649 | |
| 650 | while ((p = strstr(p, ".."))) { |
| 651 | if ((p == arcn->name || p[-1] == '/') && |
| 652 | (p[2] == '/' || p[2] == '\0')) { |
| 653 | paxwarn(1, |
| 654 | "Skipping pathname with dotdot components: %s", |
| 655 | arcn->name); |
| 656 | return (1); |
| 657 | } |
| 658 | ++p; |
| 659 | } |
| 660 | } |
| 661 | |
| 662 | /* |
| 663 | * Strip off leading '/' if appropriate. |
| 664 | * Currently, this option is only set for the tar format. |
| 665 | */ |
| 666 | while (rmleadslash && arcn->name[0] == '/') { |
| 667 | if (arcn->name[1] == '\0') { |
| 668 | arcn->name[0] = '.'; |
| 669 | } else { |
| 670 | (void)memmove(arcn->name, &arcn->name[1], |
| 671 | strlen(arcn->name)); |
| 672 | arcn->nlen--; |
| 673 | } |
| 674 | if (rmleadslash < 2) { |
| 675 | rmleadslash = 2; |
| 676 | paxwarn(0, "Removing leading / from absolute path names in the archive"); |
| 677 | } |
| 678 | } |
| 679 | while (rmleadslash && arcn->ln_name[0] == '/' && |
| 680 | PAX_IS_HARDLINK(arcn->type)) { |
| 681 | if (arcn->ln_name[1] == '\0') { |
| 682 | arcn->ln_name[0] = '.'; |
| 683 | } else { |
| 684 | (void)memmove(arcn->ln_name, &arcn->ln_name[1], |
| 685 | strlen(arcn->ln_name)); |
| 686 | arcn->ln_nlen--; |
| 687 | } |
| 688 | if (rmleadslash < 2) { |
| 689 | rmleadslash = 2; |
| 690 | paxwarn(0, "Removing leading / from absolute path names in the archive"); |
| 691 | } |
| 692 | } |
| 693 | |
| 694 | /* |
| 695 | * IMPORTANT: We have a problem. what do we do with symlinks? |
| 696 | * Modifying a hard link name makes sense, as we know the file it |
| 697 | * points at should have been seen already in the archive (and if it |
| 698 | * wasn't seen because of a read error or a bad archive, we lose |
| 699 | * anyway). But there are no such requirements for symlinks. On one |
| 700 | * hand the symlink that refers to a file in the archive will have to |
| 701 | * be modified to so it will still work at its new location in the |
| 702 | * filesystem. On the other hand a symlink that points elsewhere (and |
| 703 | * should continue to do so) should not be modified. There is clearly |
| 704 | * no perfect solution here. So we handle them like hardlinks. Clearly |
| 705 | * a replacement made by the interactive rename mapping is very likely |
| 706 | * to be correct since it applies to a single file and is an exact |
| 707 | * match. The regular expression replacements are a little harder to |
| 708 | * justify though. We claim that the symlink name is only likely |
| 709 | * to be replaced when it points within the file tree being moved and |
| 710 | * in that case it should be modified. what we really need to do is to |
| 711 | * call an oracle here. :) |
| 712 | */ |
| 713 | if (rephead != NULL) { |
| 714 | /* |
| 715 | * we have replacement strings, modify the name and the link |
| 716 | * name if any. |
| 717 | */ |
| 718 | if ((res = rep_name(arcn->name, sizeof(arcn->name), &(arcn->nlen), 1)) != 0) |
| 719 | return(res); |
| 720 | |
| 721 | if (PAX_IS_LINK(arcn->type)) { |
| 722 | if ((res = rep_name(arcn->ln_name, |
| 723 | sizeof(arcn->ln_name), &(arcn->ln_nlen), 0)) != 0) |
| 724 | return(res); |
| 725 | } |
| 726 | } |
| 727 | |
| 728 | if (iflag) { |
| 729 | /* |
| 730 | * perform interactive file rename, then map the link if any |
| 731 | */ |
| 732 | if ((res = tty_rename(arcn)) != 0) |
| 733 | return(res); |
| 734 | if (PAX_IS_LINK(arcn->type)) |
| 735 | sub_name(arcn->ln_name, &(arcn->ln_nlen), |
| 736 | sizeof(arcn->ln_name)); |
| 737 | } |
| 738 | return(res); |
| 739 | } |
| 740 | |
| 741 | /* |
| 742 | * tty_rename() |
| 743 | * Prompt the user for a replacement file name. A "." keeps the old name, |
| 744 | * a empty line skips the file, and an EOF on reading the tty, will cause |
| 745 | * pax to stop processing and exit. Otherwise the file name input, replaces |
| 746 | * the old one. |
| 747 | * Return: |
| 748 | * 0 process this file, 1 skip this file, -1 we need to exit pax |
| 749 | */ |
| 750 | |
| 751 | static int |
| 752 | tty_rename(ARCHD *arcn) |
| 753 | { |
| 754 | char *tmpname; |
| 755 | int res; |
| 756 | |
| 757 | /* |
| 758 | * prompt user for the replacement name for a file, keep trying until |
| 759 | * we get some reasonable input. Archives may have more than one file |
| 760 | * on them with the same name (from updates etc). We print verbose info |
| 761 | * on the file so the user knows what is up. |
| 762 | */ |
| 763 | tty_prnt("\nATTENTION: %s interactive file rename operation.\n", argv0); |
| 764 | |
| 765 | for (;;) { |
| 766 | ls_tty(arcn); |
| 767 | tty_prnt("Input new name, or a \".\" to keep the old name, "); |
| 768 | tty_prnt("or a \"return\" to skip this file.\n"); |
| 769 | tty_prnt("Input > "); |
| 770 | if ((tmpname = tty_rd()) == NULL) |
| 771 | return (-1); |
| 772 | if (strcmp(tmpname, "..") == 0) { |
| 773 | tty_prnt("Try again, illegal file name: ..\n"); |
| 774 | free(tmpname); |
| 775 | continue; |
| 776 | } |
| 777 | if (strlen(tmpname) > PAXPATHLEN) { |
| 778 | tty_prnt("Try again, file name too long\n"); |
| 779 | free(tmpname); |
| 780 | continue; |
| 781 | } |
| 782 | break; |
| 783 | } |
| 784 | |
| 785 | /* |
| 786 | * empty file name, skips this file. a "." leaves it alone |
| 787 | */ |
| 788 | if (tmpname[0] == '\0') { |
| 789 | tty_prnt("Skipping file.\n"); |
| 790 | free(tmpname); |
| 791 | return (1); |
| 792 | } |
| 793 | if ((tmpname[0] == '.') && (tmpname[1] == '\0')) { |
| 794 | tty_prnt("Processing continues, name unchanged.\n"); |
| 795 | free(tmpname); |
| 796 | return (0); |
| 797 | } |
| 798 | |
| 799 | /* |
| 800 | * ok the name changed. We may run into links that point at this |
| 801 | * file later. we have to remember where the user sent the file |
| 802 | * in order to repair any links. |
| 803 | */ |
| 804 | tty_prnt("Processing continues, name changed to: %s\n", tmpname); |
| 805 | res = add_name(arcn->name, arcn->nlen, tmpname); |
| 806 | arcn->nlen = strlcpy(arcn->name, tmpname, sizeof(arcn->name)); |
| 807 | if ((size_t)arcn->nlen >= sizeof(arcn->name)) |
| 808 | arcn->nlen = sizeof(arcn->name) - 1; /* XXX truncate? */ |
| 809 | free(tmpname); |
| 810 | if (res < 0) |
| 811 | return (-1); |
| 812 | return (0); |
| 813 | } |
| 814 | |
| 815 | /* |
| 816 | * set_dest() |
| 817 | * fix up the file name and the link name (if any) so this file will land |
| 818 | * in the destination directory (used during copy() -rw). |
| 819 | * Return: |
| 820 | * 0 if ok, -1 if failure (name too long) |
| 821 | */ |
| 822 | |
| 823 | int |
| 824 | set_dest(ARCHD *arcn, char *dest_dir, int dir_len) |
| 825 | { |
| 826 | if (fix_path(arcn->name, &(arcn->nlen), dest_dir, dir_len) < 0) |
| 827 | return(-1); |
| 828 | |
| 829 | /* |
| 830 | * It is really hard to deal with symlinks here, we cannot be sure |
| 831 | * if the name they point was moved (or will be moved). It is best to |
| 832 | * leave them alone. |
| 833 | */ |
| 834 | if (!PAX_IS_HARDLINK(arcn->type)) |
| 835 | return(0); |
| 836 | |
| 837 | if (fix_path(arcn->ln_name, &(arcn->ln_nlen), dest_dir, dir_len) < 0) |
| 838 | return(-1); |
| 839 | return(0); |
| 840 | } |
| 841 | |
| 842 | /* |
| 843 | * fix_path |
| 844 | * concatenate dir_name and or_name and store the result in or_name (if |
| 845 | * it fits). This is one ugly function. |
| 846 | * Return: |
| 847 | * 0 if ok, -1 if the final name is too long |
| 848 | */ |
| 849 | |
| 850 | static int |
| 851 | fix_path(char *or_name, int *or_len, char *dir_name, int dir_len) |
| 852 | { |
| 853 | char *src; |
| 854 | char *dest; |
| 855 | char *start; |
| 856 | int len; |
| 857 | |
| 858 | /* |
| 859 | * we shift the or_name to the right enough to tack in the dir_name |
| 860 | * at the front. We make sure we have enough space for it all before |
| 861 | * we start. since dest always ends in a slash, we skip of or_name |
| 862 | * if it also starts with one. |
| 863 | */ |
| 864 | start = or_name; |
| 865 | src = start + *or_len; |
| 866 | dest = src + dir_len; |
| 867 | if (*start == '/') { |
| 868 | ++start; |
| 869 | --dest; |
| 870 | } |
| 871 | if ((len = dest - or_name) > PAXPATHLEN) { |
| 872 | paxwarn(1, "File name %s/%s, too long", dir_name, start); |
| 873 | return(-1); |
| 874 | } |
| 875 | *or_len = len; |
| 876 | |
| 877 | /* |
| 878 | * enough space, shift |
| 879 | */ |
| 880 | while (src >= start) |
| 881 | *dest-- = *src--; |
| 882 | src = dir_name + dir_len - 1; |
| 883 | |
| 884 | /* |
| 885 | * splice in the destination directory name |
| 886 | */ |
| 887 | while (src >= dir_name) |
| 888 | *dest-- = *src--; |
| 889 | |
| 890 | *(or_name + len) = '\0'; |
| 891 | return(0); |
| 892 | } |
| 893 | |
| 894 | /* |
| 895 | * rep_name() |
| 896 | * walk down the list of replacement strings applying each one in order. |
| 897 | * when we find one with a successful substitution, we modify the name |
| 898 | * as specified. if required, we print the results. if the resulting name |
| 899 | * is empty, we will skip this archive member. We use the regexp(3) |
| 900 | * routines (regexp() ought to win a price as having the most cryptic |
| 901 | * library function manual page). |
| 902 | * --Parameters-- |
| 903 | * name is the file name we are going to apply the regular expressions to |
| 904 | * (and may be modified) |
| 905 | * nsize is the size of the name buffer. |
| 906 | * nlen is the length of this name (and is modified to hold the length of |
| 907 | * the final string). |
| 908 | * prnt is a flag that says whether to print the final result. |
| 909 | * Return: |
| 910 | * 0 if substitution was successful, 1 if we are to skip the file (the name |
| 911 | * ended up empty) |
| 912 | */ |
| 913 | |
| 914 | static int |
| 915 | rep_name(char *name, size_t nsize, int *nlen, int prnt) |
| 916 | { |
| 917 | REPLACE *pt; |
| 918 | char *inpt; |
| 919 | char *outpt; |
| 920 | char *endpt; |
| 921 | char *rpt; |
| 922 | int found = 0; |
| 923 | int res; |
| 924 | regmatch_t pm[MAXSUBEXP]; |
| 925 | char nname[PAXPATHLEN+1]; /* final result of all replacements */ |
| 926 | char buf1[PAXPATHLEN+1]; /* where we work on the name */ |
| 927 | |
| 928 | /* |
| 929 | * copy the name into buf1, where we will work on it. We need to keep |
| 930 | * the orig string around so we can print out the result of the final |
| 931 | * replacement. We build up the final result in nname. inpt points at |
| 932 | * the string we apply the regular expression to. prnt is used to |
| 933 | * suppress printing when we handle replacements on the link field |
| 934 | * (the user already saw that substitution go by) |
| 935 | */ |
| 936 | pt = rephead; |
| 937 | (void)strlcpy(buf1, name, sizeof(buf1)); |
| 938 | inpt = buf1; |
| 939 | outpt = nname; |
| 940 | endpt = outpt + PAXPATHLEN; |
| 941 | |
| 942 | /* |
| 943 | * try each replacement string in order |
| 944 | */ |
| 945 | while (pt != NULL) { |
| 946 | do { |
| 947 | char *oinpt = inpt; |
| 948 | /* |
| 949 | * check for a successful substitution, if not go to |
| 950 | * the next pattern, or cleanup if we were global |
| 951 | */ |
| 952 | if (regexec(&(pt->rcmp), inpt, MAXSUBEXP, pm, 0) != 0) |
| 953 | break; |
| 954 | |
| 955 | /* |
| 956 | * ok we found one. We have three parts, the prefix |
| 957 | * which did not match, the section that did and the |
| 958 | * tail (that also did not match). Copy the prefix to |
| 959 | * the final output buffer (watching to make sure we |
| 960 | * do not create a string too long). |
| 961 | */ |
| 962 | found = 1; |
| 963 | rpt = inpt + pm[0].rm_so; |
| 964 | |
| 965 | while ((inpt < rpt) && (outpt < endpt)) |
| 966 | *outpt++ = *inpt++; |
| 967 | if (outpt == endpt) |
| 968 | break; |
| 969 | |
| 970 | /* |
| 971 | * for the second part (which matched the regular |
| 972 | * expression) apply the substitution using the |
| 973 | * replacement string and place it the prefix in the |
| 974 | * final output. If we have problems, skip it. |
| 975 | */ |
| 976 | if ((res = resub(&(pt->rcmp),pm,pt->nstr,oinpt,outpt,endpt)) |
| 977 | < 0) { |
| 978 | if (prnt) |
| 979 | paxwarn(1, "Replacement name error %s", |
| 980 | name); |
| 981 | return(1); |
| 982 | } |
| 983 | outpt += res; |
| 984 | |
| 985 | /* |
| 986 | * we set up to look again starting at the first |
| 987 | * character in the tail (of the input string right |
| 988 | * after the last character matched by the regular |
| 989 | * expression (inpt always points at the first char in |
| 990 | * the string to process). If we are not doing a global |
| 991 | * substitution, we will use inpt to copy the tail to |
| 992 | * the final result. Make sure we do not overrun the |
| 993 | * output buffer |
| 994 | */ |
| 995 | inpt += pm[0].rm_eo - pm[0].rm_so; |
| 996 | |
| 997 | if ((outpt == endpt) || (*inpt == '\0')) |
| 998 | break; |
| 999 | |
| 1000 | /* |
| 1001 | * if the user wants global we keep trying to |
| 1002 | * substitute until it fails, then we are done. |
| 1003 | */ |
| 1004 | } while (pt->flgs & GLOB); |
| 1005 | |
| 1006 | if (found) |
| 1007 | break; |
| 1008 | |
| 1009 | /* |
| 1010 | * a successful substitution did NOT occur, try the next one |
| 1011 | */ |
| 1012 | pt = pt->fow; |
| 1013 | } |
| 1014 | |
| 1015 | if (found) { |
| 1016 | /* |
| 1017 | * we had a substitution, copy the last tail piece (if there is |
| 1018 | * room) to the final result |
| 1019 | */ |
| 1020 | while ((outpt < endpt) && (*inpt != '\0')) |
| 1021 | *outpt++ = *inpt++; |
| 1022 | |
| 1023 | *outpt = '\0'; |
| 1024 | if ((outpt == endpt) && (*inpt != '\0')) { |
| 1025 | if (prnt) |
| 1026 | paxwarn(1,"Replacement name too long %s >> %s", |
| 1027 | name, nname); |
| 1028 | return(1); |
| 1029 | } |
| 1030 | |
| 1031 | /* |
| 1032 | * inform the user of the result if wanted |
| 1033 | */ |
| 1034 | if (prnt && (pt->flgs & PRNT)) { |
| 1035 | (void)fprintf(stderr, "%s >> %s", |
| 1036 | name, *nname == '\0' ? "<empty string>" : nname); |
| 1037 | (void)fputc('\n', stderr); |
| 1038 | } |
| 1039 | |
| 1040 | /* |
| 1041 | * if empty inform the caller this file is to be skipped |
| 1042 | * otherwise copy the new name over the orig name and return |
| 1043 | */ |
| 1044 | if (*nname == '\0') |
| 1045 | return(1); |
| 1046 | *nlen = strlcpy(name, nname, nsize); |
| 1047 | } |
| 1048 | return(0); |
| 1049 | } |
| 1050 | |
| 1051 | /* |
| 1052 | * resub() |
| 1053 | * apply the replacement to the matched expression. expand out the old |
| 1054 | * style ed(1) subexpression expansion. |
| 1055 | * Return: |
| 1056 | * -1 if error, or the number of characters added to the destination. |
| 1057 | */ |
| 1058 | |
| 1059 | static int |
| 1060 | resub(regex_t *rp, regmatch_t *pm, char *src, char *inpt, char *dest, |
| 1061 | char *destend) |
| 1062 | { |
| 1063 | char *spt; |
| 1064 | char *dpt; |
| 1065 | char c; |
| 1066 | regmatch_t *pmpt; |
| 1067 | int len; |
| 1068 | int subexcnt; |
| 1069 | |
| 1070 | spt = src; |
| 1071 | dpt = dest; |
| 1072 | subexcnt = rp->re_nsub; |
| 1073 | while ((dpt < destend) && ((c = *spt++) != '\0')) { |
| 1074 | /* |
| 1075 | * see if we just have an ordinary replacement character |
| 1076 | * or we refer to a subexpression. |
| 1077 | */ |
| 1078 | if (c == '&') { |
| 1079 | pmpt = pm; |
| 1080 | } else if ((c == '\\') && (*spt >= '0') && (*spt <= '9')) { |
| 1081 | /* |
| 1082 | * make sure there is a subexpression as specified |
| 1083 | */ |
| 1084 | if ((len = *spt++ - '0') > subexcnt) |
| 1085 | return(-1); |
| 1086 | pmpt = pm + len; |
| 1087 | } else { |
| 1088 | /* |
| 1089 | * Ordinary character, just copy it |
| 1090 | */ |
| 1091 | if ((c == '\\') && (*spt != '\0')) |
| 1092 | c = *spt++; |
| 1093 | *dpt++ = c; |
| 1094 | continue; |
| 1095 | } |
| 1096 | |
| 1097 | /* |
| 1098 | * continue if the subexpression is bogus |
| 1099 | */ |
| 1100 | if ((pmpt->rm_so < 0) || (pmpt->rm_eo < 0) || |
| 1101 | ((len = pmpt->rm_eo - pmpt->rm_so) <= 0)) |
| 1102 | continue; |
| 1103 | |
| 1104 | /* |
| 1105 | * copy the subexpression to the destination. |
| 1106 | * fail if we run out of space or the match string is damaged |
| 1107 | */ |
| 1108 | if (len > (destend - dpt)) |
| 1109 | return (-1); |
| 1110 | strncpy(dpt, inpt + pmpt->rm_so, len); |
| 1111 | dpt += len; |
| 1112 | } |
| 1113 | return(dpt - dest); |
| 1114 | } |