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