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