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