1 /*- 2 * Copyright (c) 2003-2007 Tim Kientzle 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR 15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, 18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 */ 25 26 #include "bsdtar_platform.h" 27 __FBSDID("$FreeBSD: src/usr.bin/tar/util.c,v 1.23 2008/12/15 06:00:25 kientzle Exp $"); 28 29 #ifdef HAVE_SYS_STAT_H 30 #include <sys/stat.h> 31 #endif 32 #ifdef HAVE_SYS_TYPES_H 33 #include <sys/types.h> /* Linux doesn't define mode_t, etc. in sys/stat.h. */ 34 #endif 35 #include <ctype.h> 36 #ifdef HAVE_ERRNO_H 37 #include <errno.h> 38 #endif 39 #ifdef HAVE_STDARG_H 40 #include <stdarg.h> 41 #endif 42 #include <stdio.h> 43 #ifdef HAVE_STDLIB_H 44 #include <stdlib.h> 45 #endif 46 #ifdef HAVE_STRING_H 47 #include <string.h> 48 #endif 49 #ifdef HAVE_WCTYPE_H 50 #include <wctype.h> 51 #else 52 /* If we don't have wctype, we need to hack up some version of iswprint(). */ 53 #define iswprint isprint 54 #endif 55 56 #include "bsdtar.h" 57 58 static void bsdtar_vwarnc(struct bsdtar *, int code, 59 const char *fmt, va_list ap); 60 static size_t bsdtar_expand_char(char *, size_t, char); 61 static const char *strip_components(const char *path, int elements); 62 63 /* TODO: Hack up a version of mbtowc for platforms with no wide 64 * character support at all. I think the following might suffice, 65 * but it needs careful testing. 66 * #if !HAVE_MBTOWC 67 * #define mbtowc(wcp, p, n) ((*wcp = *p), 1) 68 * #endif 69 */ 70 71 /* 72 * Print a string, taking care with any non-printable characters. 73 * 74 * Note that we use a stack-allocated buffer to receive the formatted 75 * string if we can. This is partly performance (avoiding a call to 76 * malloc()), partly out of expedience (we have to call vsnprintf() 77 * before malloc() anyway to find out how big a buffer we need; we may 78 * as well point that first call at a small local buffer in case it 79 * works), but mostly for safety (so we can use this to print messages 80 * about out-of-memory conditions). 81 */ 82 83 void 84 safe_fprintf(FILE *f, const char *fmt, ...) 85 { 86 char fmtbuff_stack[256]; /* Place to format the printf() string. */ 87 char outbuff[256]; /* Buffer for outgoing characters. */ 88 char *fmtbuff_heap; /* If fmtbuff_stack is too small, we use malloc */ 89 char *fmtbuff; /* Pointer to fmtbuff_stack or fmtbuff_heap. */ 90 int fmtbuff_length; 91 int length; 92 va_list ap; 93 const char *p; 94 unsigned i; 95 wchar_t wc; 96 char try_wc; 97 98 /* Use a stack-allocated buffer if we can, for speed and safety. */ 99 fmtbuff_heap = NULL; 100 fmtbuff_length = sizeof(fmtbuff_stack); 101 fmtbuff = fmtbuff_stack; 102 103 /* Try formatting into the stack buffer. */ 104 va_start(ap, fmt); 105 length = vsnprintf(fmtbuff, fmtbuff_length, fmt, ap); 106 va_end(ap); 107 108 /* If the result was too large, allocate a buffer on the heap. */ 109 if (length >= fmtbuff_length) { 110 fmtbuff_length = length+1; 111 fmtbuff_heap = malloc(fmtbuff_length); 112 113 /* Reformat the result into the heap buffer if we can. */ 114 if (fmtbuff_heap != NULL) { 115 fmtbuff = fmtbuff_heap; 116 va_start(ap, fmt); 117 length = vsnprintf(fmtbuff, fmtbuff_length, fmt, ap); 118 va_end(ap); 119 } else { 120 /* Leave fmtbuff pointing to the truncated 121 * string in fmtbuff_stack. */ 122 length = sizeof(fmtbuff_stack) - 1; 123 } 124 } 125 126 /* Note: mbrtowc() has a cleaner API, but mbtowc() seems a bit 127 * more portable, so we use that here instead. */ 128 mbtowc(NULL, NULL, 0); /* Reset the shift state. */ 129 130 /* Write data, expanding unprintable characters. */ 131 p = fmtbuff; 132 i = 0; 133 try_wc = 1; 134 while (*p != '\0') { 135 int n; 136 137 /* Convert to wide char, test if the wide 138 * char is printable in the current locale. */ 139 if (try_wc && (n = mbtowc(&wc, p, length)) != -1) { 140 length -= n; 141 if (iswprint(wc) && wc != L'\\') { 142 /* Printable, copy the bytes through. */ 143 while (n-- > 0) 144 outbuff[i++] = *p++; 145 } else { 146 /* Not printable, format the bytes. */ 147 while (n-- > 0) 148 i += bsdtar_expand_char( 149 outbuff, i, *p++); 150 } 151 } else { 152 /* After any conversion failure, don't bother 153 * trying to convert the rest. */ 154 i += bsdtar_expand_char(outbuff, i, *p++); 155 try_wc = 0; 156 } 157 158 /* If our output buffer is full, dump it and keep going. */ 159 if (i > (sizeof(outbuff) - 20)) { 160 outbuff[i++] = '\0'; 161 fprintf(f, "%s", outbuff); 162 i = 0; 163 } 164 } 165 outbuff[i++] = '\0'; 166 fprintf(f, "%s", outbuff); 167 168 /* If we allocated a heap-based formatting buffer, free it now. */ 169 if (fmtbuff_heap != NULL) 170 free(fmtbuff_heap); 171 } 172 173 /* 174 * Render an arbitrary sequence of bytes into printable ASCII characters. 175 */ 176 static size_t 177 bsdtar_expand_char(char *buff, size_t offset, char c) 178 { 179 size_t i = offset; 180 181 if (isprint((unsigned char)c) && c != '\\') 182 buff[i++] = c; 183 else { 184 buff[i++] = '\\'; 185 switch (c) { 186 case '\a': buff[i++] = 'a'; break; 187 case '\b': buff[i++] = 'b'; break; 188 case '\f': buff[i++] = 'f'; break; 189 case '\n': buff[i++] = 'n'; break; 190 #if '\r' != '\n' 191 /* On some platforms, \n and \r are the same. */ 192 case '\r': buff[i++] = 'r'; break; 193 #endif 194 case '\t': buff[i++] = 't'; break; 195 case '\v': buff[i++] = 'v'; break; 196 case '\\': buff[i++] = '\\'; break; 197 default: 198 sprintf(buff + i, "%03o", 0xFF & (int)c); 199 i += 3; 200 } 201 } 202 203 return (i - offset); 204 } 205 206 static void 207 bsdtar_vwarnc(struct bsdtar *bsdtar, int code, const char *fmt, va_list ap) 208 { 209 fprintf(stderr, "%s: ", bsdtar->progname); 210 vfprintf(stderr, fmt, ap); 211 if (code != 0) 212 fprintf(stderr, ": %s", strerror(code)); 213 fprintf(stderr, "\n"); 214 } 215 216 void 217 bsdtar_warnc(struct bsdtar *bsdtar, int code, const char *fmt, ...) 218 { 219 va_list ap; 220 221 va_start(ap, fmt); 222 bsdtar_vwarnc(bsdtar, code, fmt, ap); 223 va_end(ap); 224 } 225 226 void 227 bsdtar_errc(struct bsdtar *bsdtar, int eval, int code, const char *fmt, ...) 228 { 229 va_list ap; 230 231 va_start(ap, fmt); 232 bsdtar_vwarnc(bsdtar, code, fmt, ap); 233 va_end(ap); 234 exit(eval); 235 } 236 237 int 238 yes(const char *fmt, ...) 239 { 240 char buff[32]; 241 char *p; 242 ssize_t l; 243 244 va_list ap; 245 va_start(ap, fmt); 246 vfprintf(stderr, fmt, ap); 247 va_end(ap); 248 fprintf(stderr, " (y/N)? "); 249 fflush(stderr); 250 251 l = read(2, buff, sizeof(buff) - 1); 252 if (l <= 0) 253 return (0); 254 buff[l] = 0; 255 256 for (p = buff; *p != '\0'; p++) { 257 if (isspace((unsigned char)*p)) 258 continue; 259 switch(*p) { 260 case 'y': case 'Y': 261 return (1); 262 case 'n': case 'N': 263 return (0); 264 default: 265 return (0); 266 } 267 } 268 269 return (0); 270 } 271 272 /* 273 * Read lines from file and do something with each one. If option_null 274 * is set, lines are terminated with zero bytes; otherwise, they're 275 * terminated with newlines. 276 * 277 * This uses a self-sizing buffer to handle arbitrarily-long lines. 278 * If the "process" function returns non-zero for any line, this 279 * function will return non-zero after attempting to process all 280 * remaining lines. 281 */ 282 int 283 process_lines(struct bsdtar *bsdtar, const char *pathname, 284 int (*process)(struct bsdtar *, const char *)) 285 { 286 FILE *f; 287 char *buff, *buff_end, *line_start, *line_end, *p; 288 size_t buff_length, new_buff_length, bytes_read, bytes_wanted; 289 int separator; 290 int ret; 291 292 separator = bsdtar->option_null ? '\0' : '\n'; 293 ret = 0; 294 295 if (strcmp(pathname, "-") == 0) 296 f = stdin; 297 else 298 f = fopen(pathname, "r"); 299 if (f == NULL) 300 bsdtar_errc(bsdtar, 1, errno, "Couldn't open %s", pathname); 301 buff_length = 8192; 302 buff = malloc(buff_length); 303 if (buff == NULL) 304 bsdtar_errc(bsdtar, 1, ENOMEM, "Can't read %s", pathname); 305 line_start = line_end = buff_end = buff; 306 for (;;) { 307 /* Get some more data into the buffer. */ 308 bytes_wanted = buff + buff_length - buff_end; 309 bytes_read = fread(buff_end, 1, bytes_wanted, f); 310 buff_end += bytes_read; 311 /* Process all complete lines in the buffer. */ 312 while (line_end < buff_end) { 313 if (*line_end == separator) { 314 *line_end = '\0'; 315 if ((*process)(bsdtar, line_start) != 0) 316 ret = -1; 317 line_start = line_end + 1; 318 line_end = line_start; 319 } else 320 line_end++; 321 } 322 if (feof(f)) 323 break; 324 if (ferror(f)) 325 bsdtar_errc(bsdtar, 1, errno, 326 "Can't read %s", pathname); 327 if (line_start > buff) { 328 /* Move a leftover fractional line to the beginning. */ 329 memmove(buff, line_start, buff_end - line_start); 330 buff_end -= line_start - buff; 331 line_end -= line_start - buff; 332 line_start = buff; 333 } else { 334 /* Line is too big; enlarge the buffer. */ 335 new_buff_length = buff_length * 2; 336 if (new_buff_length <= buff_length) 337 bsdtar_errc(bsdtar, 1, ENOMEM, 338 "Line too long in %s", pathname); 339 buff_length = new_buff_length; 340 p = realloc(buff, buff_length); 341 if (p == NULL) 342 bsdtar_errc(bsdtar, 1, ENOMEM, 343 "Line too long in %s", pathname); 344 buff_end = p + (buff_end - buff); 345 line_end = p + (line_end - buff); 346 line_start = buff = p; 347 } 348 } 349 /* At end-of-file, handle the final line. */ 350 if (line_end > line_start) { 351 *line_end = '\0'; 352 if ((*process)(bsdtar, line_start) != 0) 353 ret = -1; 354 } 355 free(buff); 356 if (f != stdin) 357 fclose(f); 358 return (ret); 359 } 360 361 /*- 362 * The logic here for -C <dir> attempts to avoid 363 * chdir() as long as possible. For example: 364 * "-C /foo -C /bar file" needs chdir("/bar") but not chdir("/foo") 365 * "-C /foo -C bar file" needs chdir("/foo/bar") 366 * "-C /foo -C bar /file1" does not need chdir() 367 * "-C /foo -C bar /file1 file2" needs chdir("/foo/bar") before file2 368 * 369 * The only correct way to handle this is to record a "pending" chdir 370 * request and combine multiple requests intelligently until we 371 * need to process a non-absolute file. set_chdir() adds the new dir 372 * to the pending list; do_chdir() actually executes any pending chdir. 373 * 374 * This way, programs that build tar command lines don't have to worry 375 * about -C with non-existent directories; such requests will only 376 * fail if the directory must be accessed. 377 */ 378 void 379 set_chdir(struct bsdtar *bsdtar, const char *newdir) 380 { 381 if (newdir[0] == '/') { 382 /* The -C /foo -C /bar case; dump first one. */ 383 free(bsdtar->pending_chdir); 384 bsdtar->pending_chdir = NULL; 385 } 386 if (bsdtar->pending_chdir == NULL) 387 /* Easy case: no previously-saved dir. */ 388 bsdtar->pending_chdir = strdup(newdir); 389 else { 390 /* The -C /foo -C bar case; concatenate */ 391 char *old_pending = bsdtar->pending_chdir; 392 size_t old_len = strlen(old_pending); 393 bsdtar->pending_chdir = malloc(old_len + strlen(newdir) + 2); 394 if (old_pending[old_len - 1] == '/') 395 old_pending[old_len - 1] = '\0'; 396 if (bsdtar->pending_chdir != NULL) 397 sprintf(bsdtar->pending_chdir, "%s/%s", 398 old_pending, newdir); 399 free(old_pending); 400 } 401 if (bsdtar->pending_chdir == NULL) 402 bsdtar_errc(bsdtar, 1, errno, "No memory"); 403 } 404 405 void 406 do_chdir(struct bsdtar *bsdtar) 407 { 408 if (bsdtar->pending_chdir == NULL) 409 return; 410 411 if (chdir(bsdtar->pending_chdir) != 0) { 412 bsdtar_errc(bsdtar, 1, 0, "could not chdir to '%s'\n", 413 bsdtar->pending_chdir); 414 } 415 free(bsdtar->pending_chdir); 416 bsdtar->pending_chdir = NULL; 417 } 418 419 const char * 420 strip_components(const char *path, int elements) 421 { 422 const char *p = path; 423 424 while (elements > 0) { 425 switch (*p++) { 426 case '/': 427 elements--; 428 path = p; 429 break; 430 case '\0': 431 /* Path is too short, skip it. */ 432 return (NULL); 433 } 434 } 435 436 while (*path == '/') 437 ++path; 438 if (*path == '\0') 439 return (NULL); 440 441 return (path); 442 } 443 444 /* 445 * Handle --strip-components and any future path-rewriting options. 446 * Returns non-zero if the pathname should not be extracted. 447 * 448 * TODO: Support pax-style regex path rewrites. 449 */ 450 int 451 edit_pathname(struct bsdtar *bsdtar, struct archive_entry *entry) 452 { 453 const char *name = archive_entry_pathname(entry); 454 #if HAVE_REGEX_H 455 char *subst_name; 456 int r; 457 #endif 458 459 #if HAVE_REGEX_H 460 r = apply_substitution(bsdtar, name, &subst_name, 0); 461 if (r == -1) { 462 bsdtar_warnc(bsdtar, 0, "Invalid substitution, skipping entry"); 463 return 1; 464 } 465 if (r == 1) { 466 archive_entry_copy_pathname(entry, subst_name); 467 if (*subst_name == '\0') { 468 free(subst_name); 469 return -1; 470 } else 471 free(subst_name); 472 name = archive_entry_pathname(entry); 473 } 474 475 if (archive_entry_hardlink(entry)) { 476 r = apply_substitution(bsdtar, archive_entry_hardlink(entry), &subst_name, 1); 477 if (r == -1) { 478 bsdtar_warnc(bsdtar, 0, "Invalid substitution, skipping entry"); 479 return 1; 480 } 481 if (r == 1) { 482 archive_entry_copy_hardlink(entry, subst_name); 483 free(subst_name); 484 } 485 } 486 if (archive_entry_symlink(entry) != NULL) { 487 r = apply_substitution(bsdtar, archive_entry_symlink(entry), &subst_name, 1); 488 if (r == -1) { 489 bsdtar_warnc(bsdtar, 0, "Invalid substitution, skipping entry"); 490 return 1; 491 } 492 if (r == 1) { 493 archive_entry_copy_symlink(entry, subst_name); 494 free(subst_name); 495 } 496 } 497 #endif 498 499 /* Strip leading dir names as per --strip-components option. */ 500 if (bsdtar->strip_components > 0) { 501 const char *linkname = archive_entry_hardlink(entry); 502 503 name = strip_components(name, bsdtar->strip_components); 504 if (name == NULL) 505 return (1); 506 507 if (linkname != NULL) { 508 linkname = strip_components(linkname, 509 bsdtar->strip_components); 510 if (linkname == NULL) 511 return (1); 512 archive_entry_copy_hardlink(entry, linkname); 513 } 514 } 515 516 /* By default, don't write or restore absolute pathnames. */ 517 if (!bsdtar->option_absolute_paths) { 518 const char *rp, *p = name; 519 int slashonly = 1; 520 521 /* Remove leading "//./" or "//?/" or "//?/UNC/" 522 * (absolute path prefixes used by Windows API) */ 523 if ((p[0] == '/' || p[0] == '\\') && 524 (p[1] == '/' || p[1] == '\\') && 525 (p[2] == '.' || p[2] == '?') && 526 (p[3] == '/' || p[3] == '\\')) 527 { 528 if (p[2] == '?' && 529 (p[4] == 'U' || p[4] == 'u') && 530 (p[5] == 'N' || p[5] == 'n') && 531 (p[6] == 'C' || p[6] == 'c') && 532 (p[7] == '/' || p[7] == '\\')) 533 p += 8; 534 else 535 p += 4; 536 slashonly = 0; 537 } 538 do { 539 rp = p; 540 /* Remove leading drive letter from archives created 541 * on Windows. */ 542 if (((p[0] >= 'a' && p[0] <= 'z') || 543 (p[0] >= 'A' && p[0] <= 'Z')) && 544 p[1] == ':') { 545 p += 2; 546 slashonly = 0; 547 } 548 /* Remove leading "/../", "//", etc. */ 549 while (p[0] == '/' || p[0] == '\\') { 550 if (p[1] == '.' && p[2] == '.' && 551 (p[3] == '/' || p[3] == '\\')) { 552 p += 3; /* Remove "/..", leave "/" 553 * for next pass. */ 554 slashonly = 0; 555 } else 556 p += 1; /* Remove "/". */ 557 } 558 } while (rp != p); 559 560 if (p != name && !bsdtar->warned_lead_slash) { 561 /* Generate a warning the first time this happens. */ 562 if (slashonly) 563 bsdtar_warnc(bsdtar, 0, 564 "Removing leading '%c' from member names", 565 name[0]); 566 else 567 bsdtar_warnc(bsdtar, 0, 568 "Removing leading drive letter from " 569 "member names"); 570 bsdtar->warned_lead_slash = 1; 571 } 572 573 /* Special case: Stripping everything yields ".". */ 574 if (*p == '\0') 575 name = "."; 576 else 577 name = p; 578 } else { 579 /* Strip redundant leading '/' characters. */ 580 while (name[0] == '/' && name[1] == '/') 581 name++; 582 } 583 584 /* Safely replace name in archive_entry. */ 585 if (name != archive_entry_pathname(entry)) { 586 char *q = strdup(name); 587 archive_entry_copy_pathname(entry, q); 588 free(q); 589 } 590 return (0); 591 } 592 593 /* 594 * Like strcmp(), but try to be a little more aware of the fact that 595 * we're comparing two paths. Right now, it just handles leading 596 * "./" and trailing '/' specially, so that "a/b/" == "./a/b" 597 * 598 * TODO: Make this better, so that "./a//b/./c/" == "a/b/c" 599 * TODO: After this works, push it down into libarchive. 600 * TODO: Publish the path normalization routines in libarchive so 601 * that bsdtar can normalize paths and use fast strcmp() instead 602 * of this. 603 */ 604 605 int 606 pathcmp(const char *a, const char *b) 607 { 608 /* Skip leading './' */ 609 if (a[0] == '.' && a[1] == '/' && a[2] != '\0') 610 a += 2; 611 if (b[0] == '.' && b[1] == '/' && b[2] != '\0') 612 b += 2; 613 /* Find the first difference, or return (0) if none. */ 614 while (*a == *b) { 615 if (*a == '\0') 616 return (0); 617 a++; 618 b++; 619 } 620 /* 621 * If one ends in '/' and the other one doesn't, 622 * they're the same. 623 */ 624 if (a[0] == '/' && a[1] == '\0' && b[0] == '\0') 625 return (0); 626 if (a[0] == '\0' && b[0] == '/' && b[1] == '\0') 627 return (0); 628 /* They're really different, return the correct sign. */ 629 return (*(const unsigned char *)a - *(const unsigned char *)b); 630 } 631