| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
#include <sys/types.h> |
| 40 |
#include <sys/stat.h> |
| 41 |
#include <ctype.h> |
| 42 |
#include <errno.h> |
| 43 |
#if HAVE_GRP_H |
| 44 |
#include <grp.h> |
| 45 |
#endif |
| 46 |
#include <limits.h> |
| 47 |
#include <pwd.h> |
| 48 |
#include <stdio.h> |
| 49 |
#include <stdlib.h> |
| 50 |
#include <string.h> |
| 51 |
#if HAVE_STRINGS_H |
| 52 |
#include <strings.h> |
| 53 |
#endif |
| 54 |
#include <unistd.h> |
| 55 |
|
| 56 |
#include "pax.h" |
| 57 |
#include "extern.h" |
| 58 |
#include "tar.h" |
| 59 |
|
| 60 |
__RCSID("$MirOS: src/bin/pax/tar.c,v 1.24 2019/02/23 22:39:25 tg Exp $"); |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
static size_t expandname(char *, size_t, char **, const char *, size_t); |
| 67 |
static u_long tar_chksm(char *, int); |
| 68 |
static char *name_split(char *, int); |
| 69 |
static int ul_oct(u_long, char *, int, int); |
| 70 |
static int ull_oct(unsigned long long, char *, int, int); |
| 71 |
#ifndef SMALL |
| 72 |
static void tar_dbgfld(const char *, const char *, size_t); |
| 73 |
static int rd_xheader(ARCHD *arcn, int, off_t); |
| 74 |
#endif |
| 75 |
|
| 76 |
static uid_t uid_nobody; |
| 77 |
static uid_t uid_warn; |
| 78 |
static gid_t gid_nobody; |
| 79 |
static gid_t gid_warn; |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
#ifndef SMALL |
| 86 |
char tar_nodir; |
| 87 |
#endif |
| 88 |
char *gnu_name_string; |
| 89 |
char *gnu_link_string; |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
int |
| 99 |
tar_endwr(void) |
| 100 |
{ |
| 101 |
return (wr_skip(NULLCNT * BLKMULT)); |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
off_t |
| 112 |
tar_endrd(void) |
| 113 |
{ |
| 114 |
return (NULLCNT * BLKMULT); |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
int |
| 129 |
tar_trail(ARCHD *ignore MKSH_A_UNUSED, char *buf, int in_resync, int *cnt) |
| 130 |
{ |
| 131 |
int i; |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
for (i = 0; i < BLKMULT; ++i) { |
| 137 |
if (buf[i] != '\0') |
| 138 |
break; |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
if (i != BLKMULT) |
| 145 |
return(-1); |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
if (!in_resync && (++*cnt >= NULLCNT)) |
| 156 |
return(0); |
| 157 |
return(1); |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
static int |
| 172 |
ul_oct(u_long val, char *str, int len, int term) |
| 173 |
{ |
| 174 |
char *pt; |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
pt = str + len - 1; |
| 180 |
switch (term) { |
| 181 |
case 3: |
| 182 |
*pt-- = '\0'; |
| 183 |
break; |
| 184 |
case 2: |
| 185 |
*pt-- = ' '; |
| 186 |
*pt-- = '\0'; |
| 187 |
break; |
| 188 |
case 1: |
| 189 |
*pt-- = ' '; |
| 190 |
break; |
| 191 |
case 0: |
| 192 |
default: |
| 193 |
*pt-- = '\0'; |
| 194 |
*pt-- = ' '; |
| 195 |
break; |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
while (pt >= str) { |
| 202 |
*pt-- = '0' + (char)(val & 0x7); |
| 203 |
val >>= 3; |
| 204 |
if (val == 0) |
| 205 |
break; |
| 206 |
} |
| 207 |
|
| 208 |
while (pt >= str) |
| 209 |
*pt-- = '0'; |
| 210 |
if (val != 0) |
| 211 |
return(-1); |
| 212 |
return(0); |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
static int |
| 227 |
ull_oct(unsigned long long val, char *str, int len, int term) |
| 228 |
{ |
| 229 |
char *pt; |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
pt = str + len - 1; |
| 235 |
switch (term) { |
| 236 |
case 3: |
| 237 |
*pt-- = '\0'; |
| 238 |
break; |
| 239 |
case 2: |
| 240 |
*pt-- = ' '; |
| 241 |
*pt-- = '\0'; |
| 242 |
break; |
| 243 |
case 1: |
| 244 |
*pt-- = ' '; |
| 245 |
break; |
| 246 |
case 0: |
| 247 |
default: |
| 248 |
*pt-- = '\0'; |
| 249 |
*pt-- = ' '; |
| 250 |
break; |
| 251 |
} |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
while (pt >= str) { |
| 257 |
*pt-- = '0' + (char)(val & 0x7); |
| 258 |
val >>= 3; |
| 259 |
if (val == 0) |
| 260 |
break; |
| 261 |
} |
| 262 |
|
| 263 |
while (pt >= str) |
| 264 |
*pt-- = '0'; |
| 265 |
if (val != 0) |
| 266 |
return(-1); |
| 267 |
return(0); |
| 268 |
} |
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
static u_long |
| 281 |
tar_chksm(char *blk, int len) |
| 282 |
{ |
| 283 |
char *stop; |
| 284 |
char *pt; |
| 285 |
u_long chksm = BLNKSUM; |
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
pt = blk; |
| 291 |
stop = blk + CHK_OFFSET; |
| 292 |
while (pt < stop) |
| 293 |
chksm += (u_long)(*pt++ & 0xff); |
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
pt += CHK_LEN; |
| 302 |
stop = blk + len; |
| 303 |
while (pt < stop) |
| 304 |
chksm += (u_long)(*pt++ & 0xff); |
| 305 |
return(chksm); |
| 306 |
} |
| 307 |
|
| 308 |
#ifndef SMALL |
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
int |
| 323 |
tar_id(char *blk, int size) |
| 324 |
{ |
| 325 |
HD_TAR *hd; |
| 326 |
HD_USTAR *uhd; |
| 327 |
|
| 328 |
if (size < BLKMULT) |
| 329 |
return (-1); |
| 330 |
hd = (HD_TAR *)blk; |
| 331 |
uhd = (HD_USTAR *)blk; |
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
if (hd->name[0] == '\0') |
| 341 |
return (-1); |
| 342 |
if (memcmp(uhd->magic, TMAGIC, TMAGLEN - 1) == 0) |
| 343 |
return (-1); |
| 344 |
if (asc_ul(hd->chksum, sizeof(hd->chksum), OCT) != tar_chksm(blk, BLKMULT)) |
| 345 |
return (-1); |
| 346 |
force_one_volume = 1; |
| 347 |
return (0); |
| 348 |
} |
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
int |
| 358 |
tar_opt(void) |
| 359 |
{ |
| 360 |
OPLIST *opt; |
| 361 |
|
| 362 |
while ((opt = opt_next()) != NULL) { |
| 363 |
if (strcmp(opt->name, TAR_OPTION) || |
| 364 |
strcmp(opt->value, TAR_NODIR)) { |
| 365 |
paxwarn(1, "Unknown tar format -o option/value pair %s=%s", |
| 366 |
opt->name, opt->value); |
| 367 |
paxwarn(1,"%s=%s is the only supported tar format option", |
| 368 |
TAR_OPTION, TAR_NODIR); |
| 369 |
return(-1); |
| 370 |
} |
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
if ((act != APPND) && (act != ARCHIVE)) { |
| 376 |
paxwarn(1, "%s=%s is only supported when writing.", |
| 377 |
opt->name, opt->value); |
| 378 |
return(-1); |
| 379 |
} |
| 380 |
tar_nodir = 1; |
| 381 |
} |
| 382 |
return(0); |
| 383 |
} |
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
int |
| 394 |
tar_rd(ARCHD *arcn, char *buf) |
| 395 |
{ |
| 396 |
HD_TAR *hd; |
| 397 |
unsigned long long val; |
| 398 |
char *pt; |
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
if (tar_id(buf, BLKMULT) < 0) |
| 404 |
return(-1); |
| 405 |
memset(arcn, 0, sizeof(*arcn)); |
| 406 |
arcn->org_name = arcn->name; |
| 407 |
arcn->sb.st_nlink = 1; |
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
hd = (HD_TAR *)buf; |
| 413 |
if (hd->linkflag != LONGLINKTYPE && hd->linkflag != LONGNAMETYPE) { |
| 414 |
arcn->nlen = expandname(arcn->name, sizeof(arcn->name), |
| 415 |
&gnu_name_string, hd->name, sizeof(hd->name)); |
| 416 |
arcn->ln_nlen = expandname(arcn->ln_name, sizeof(arcn->ln_name), |
| 417 |
&gnu_link_string, hd->linkname, sizeof(hd->linkname)); |
| 418 |
} |
| 419 |
arcn->sb.st_mode = (mode_t)(asc_ul(hd->mode,sizeof(hd->mode),OCT) & |
| 420 |
0xfff); |
| 421 |
arcn->sb.st_uid = (uid_t)asc_ul(hd->uid, sizeof(hd->uid), OCT); |
| 422 |
arcn->sb.st_gid = (gid_t)asc_ul(hd->gid, sizeof(hd->gid), OCT); |
| 423 |
arcn->sb.st_size = (off_t)asc_ull(hd->size, sizeof(hd->size), OCT); |
| 424 |
val = asc_ull(hd->mtime, sizeof(hd->mtime), OCT); |
| 425 |
if (val > MAX_TIME_T) |
| 426 |
arcn->sb.st_mtime = INT_MAX; |
| 427 |
else |
| 428 |
arcn->sb.st_mtime = val; |
| 429 |
arcn->sb.st_ctime = arcn->sb.st_atime = arcn->sb.st_mtime; |
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
pt = arcn->nlen > 0 ? &(arcn->name[arcn->nlen - 1]) : NULL; |
| 436 |
arcn->pad = 0; |
| 437 |
arcn->skip = 0; |
| 438 |
switch (hd->linkflag) { |
| 439 |
case SYMTYPE: |
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
arcn->type = PAX_SLK; |
| 445 |
arcn->sb.st_mode |= S_IFLNK; |
| 446 |
break; |
| 447 |
case LNKTYPE: |
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
arcn->type = PAX_HLK; |
| 453 |
arcn->sb.st_nlink = 2; |
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
arcn->sb.st_mode |= S_IFREG; |
| 460 |
break; |
| 461 |
case LONGLINKTYPE: |
| 462 |
case LONGNAMETYPE: |
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
arcn->type = |
| 468 |
hd->linkflag == LONGLINKTYPE ? PAX_GLL : PAX_GLF; |
| 469 |
arcn->pad = TAR_PAD(arcn->sb.st_size); |
| 470 |
arcn->skip = arcn->sb.st_size; |
| 471 |
break; |
| 472 |
case DIRTYPE: |
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
arcn->type = PAX_DIR; |
| 477 |
arcn->sb.st_mode |= S_IFDIR; |
| 478 |
arcn->sb.st_nlink = 2; |
| 479 |
break; |
| 480 |
case AREGTYPE: |
| 481 |
case REGTYPE: |
| 482 |
default: |
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
arcn->ln_name[0] = '\0'; |
| 487 |
arcn->ln_nlen = 0; |
| 488 |
if (pt && *pt == '/') { |
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
arcn->type = PAX_DIR; |
| 493 |
arcn->sb.st_mode |= S_IFDIR; |
| 494 |
arcn->sb.st_nlink = 2; |
| 495 |
} else { |
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
arcn->type = PAX_REG; |
| 502 |
arcn->sb.st_mode |= S_IFREG; |
| 503 |
arcn->pad = TAR_PAD(arcn->sb.st_size); |
| 504 |
arcn->skip = arcn->sb.st_size; |
| 505 |
} |
| 506 |
break; |
| 507 |
} |
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
if (pt && *pt == '/') { |
| 513 |
*pt = '\0'; |
| 514 |
--arcn->nlen; |
| 515 |
} |
| 516 |
return (0); |
| 517 |
} |
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
int |
| 532 |
tar_wr(ARCHD *arcn) |
| 533 |
{ |
| 534 |
HD_TAR *hd; |
| 535 |
int len; |
| 536 |
char hdblk[sizeof(HD_TAR)]; |
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
switch (arcn->type) { |
| 542 |
case PAX_DIR: |
| 543 |
#ifndef SMALL |
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
if (tar_nodir) |
| 548 |
return(1); |
| 549 |
#endif |
| 550 |
break; |
| 551 |
case PAX_CHR: |
| 552 |
paxwarn(1, "%s cannot archive a %s %s", |
| 553 |
"tar", "character device", arcn->org_name); |
| 554 |
return (1); |
| 555 |
case PAX_BLK: |
| 556 |
paxwarn(1, "%s cannot archive a %s %s", |
| 557 |
"tar", "block device", arcn->org_name); |
| 558 |
return (1); |
| 559 |
case PAX_SCK: |
| 560 |
paxwarn(1, "%s cannot archive a %s %s", |
| 561 |
"tar", "socket", arcn->org_name); |
| 562 |
return (1); |
| 563 |
case PAX_FIF: |
| 564 |
paxwarn(1, "%s cannot archive a %s %s", |
| 565 |
"tar", "FIFO", arcn->org_name); |
| 566 |
return (1); |
| 567 |
case PAX_SLK: |
| 568 |
case PAX_HLK: |
| 569 |
case PAX_HRG: |
| 570 |
if ((size_t)arcn->ln_nlen > sizeof(hd->linkname)) { |
| 571 |
paxwarn(1, "%s name too long for %s %s", |
| 572 |
"Link", "tar", arcn->ln_name); |
| 573 |
return (1); |
| 574 |
} |
| 575 |
break; |
| 576 |
case PAX_REG: |
| 577 |
case PAX_CTG: |
| 578 |
default: |
| 579 |
break; |
| 580 |
} |
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
len = arcn->nlen; |
| 586 |
if (arcn->type == PAX_DIR) |
| 587 |
++len; |
| 588 |
if ((size_t)len > sizeof(hd->name)) { |
| 589 |
paxwarn(1, "%s name too long for %s %s", |
| 590 |
"File", "tar", arcn->name); |
| 591 |
return (1); |
| 592 |
} |
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
memset(hdblk, 0, sizeof(hdblk)); |
| 603 |
hd = (HD_TAR *)hdblk; |
| 604 |
fieldcpy(hd->name, sizeof(hd->name), arcn->name, sizeof(arcn->name)); |
| 605 |
arcn->pad = 0; |
| 606 |
|
| 607 |
if (arcn->type == PAX_DIR) { |
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
hd->linkflag = AREGTYPE; |
| 614 |
hd->name[len-1] = '/'; |
| 615 |
if (ul_oct(0, hd->size, sizeof(hd->size), 1)) |
| 616 |
goto out; |
| 617 |
} else if (arcn->type == PAX_SLK) { |
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
hd->linkflag = SYMTYPE; |
| 622 |
fieldcpy(hd->linkname, sizeof(hd->linkname), arcn->ln_name, |
| 623 |
sizeof(arcn->ln_name)); |
| 624 |
if (ul_oct(0, hd->size, sizeof(hd->size), 1)) |
| 625 |
goto out; |
| 626 |
} else if (PAX_IS_HARDLINK(arcn->type)) { |
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
hd->linkflag = LNKTYPE; |
| 631 |
fieldcpy(hd->linkname, sizeof(hd->linkname), arcn->ln_name, |
| 632 |
sizeof(arcn->ln_name)); |
| 633 |
if (ul_oct(0, hd->size, sizeof(hd->size), 1)) |
| 634 |
goto out; |
| 635 |
} else { |
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
hd->linkflag = AREGTYPE; |
| 640 |
if (ull_oct(arcn->sb.st_size, hd->size, sizeof(hd->size), 1)) { |
| 641 |
paxwarn(1, "File is too large for %s format %s", |
| 642 |
"tar", arcn->org_name); |
| 643 |
return (1); |
| 644 |
} |
| 645 |
arcn->pad = TAR_PAD(arcn->sb.st_size); |
| 646 |
} |
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
if (ul_oct(arcn->sb.st_mode, hd->mode, sizeof(hd->mode), 0) || |
| 652 |
ull_oct(arcn->sb.st_mtime < 0 ? 0 : arcn->sb.st_mtime, hd->mtime, |
| 653 |
sizeof(hd->mtime), 1) || |
| 654 |
ul_oct(arcn->sb.st_uid, hd->uid, sizeof(hd->uid), 0) || |
| 655 |
ul_oct(arcn->sb.st_gid, hd->gid, sizeof(hd->gid), 0)) |
| 656 |
goto out; |
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
if (ul_oct(tar_chksm(hdblk, sizeof(HD_TAR)), hd->chksum, |
| 664 |
sizeof(hd->chksum), 3)) |
| 665 |
goto out; |
| 666 |
if (wr_rdbuf(hdblk, sizeof(HD_TAR)) < 0) |
| 667 |
return(-1); |
| 668 |
if (wr_skip(BLKMULT - sizeof(HD_TAR)) < 0) |
| 669 |
return(-1); |
| 670 |
if (PAX_IS_REG(arcn->type)) |
| 671 |
return(0); |
| 672 |
return(1); |
| 673 |
|
| 674 |
out: |
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
paxwarn(1, "%s header field is too small for file %s", |
| 679 |
"tar", arcn->org_name); |
| 680 |
return (1); |
| 681 |
} |
| 682 |
#endif |
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
int |
| 696 |
ustar_strd(void) |
| 697 |
{ |
| 698 |
#if !HAVE_UGID_FROM_UG |
| 699 |
if ((usrtb_start() < 0) || (grptb_start() < 0)) |
| 700 |
return(-1); |
| 701 |
#endif |
| 702 |
return(0); |
| 703 |
} |
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
int |
| 713 |
ustar_stwr(int is_app MKSH_A_UNUSED) |
| 714 |
{ |
| 715 |
#if !HAVE_UG_FROM_UGID |
| 716 |
if ((uidtb_start() < 0) || (gidtb_start() < 0)) |
| 717 |
return(-1); |
| 718 |
#endif |
| 719 |
return(0); |
| 720 |
} |
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
int |
| 731 |
ustar_id(char *blk, int size) |
| 732 |
{ |
| 733 |
HD_USTAR *hd; |
| 734 |
|
| 735 |
if (size < BLKMULT) |
| 736 |
return (-1); |
| 737 |
hd = (HD_USTAR *)blk; |
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
if (hd->prefix[0] == '\0' && hd->name[0] == '\0') |
| 746 |
return (-1); |
| 747 |
if (memcmp(hd->magic, TMAGIC, TMAGLEN - 1) != 0) |
| 748 |
return (-1); |
| 749 |
if (asc_ul(hd->chksum, sizeof(hd->chksum), OCT) != tar_chksm(blk, BLKMULT)) |
| 750 |
return (-1); |
| 751 |
return (0); |
| 752 |
} |
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
|
| 760 |
|
| 761 |
|
| 762 |
int |
| 763 |
ustar_rd(ARCHD *arcn, char *buf) |
| 764 |
{ |
| 765 |
HD_USTAR *hd = (HD_USTAR *)buf; |
| 766 |
char *dest; |
| 767 |
int cnt = 0; |
| 768 |
dev_t devmajor; |
| 769 |
dev_t devminor; |
| 770 |
unsigned long long val; |
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
if (ustar_id(buf, BLKMULT) < 0) |
| 776 |
return(-1); |
| 777 |
|
| 778 |
#ifndef SMALL |
| 779 |
reset: |
| 780 |
#endif |
| 781 |
memset(arcn, 0, sizeof(*arcn)); |
| 782 |
arcn->org_name = arcn->name; |
| 783 |
arcn->sb.st_nlink = 1; |
| 784 |
|
| 785 |
#ifndef SMALL |
| 786 |
|
| 787 |
if (hd->typeflag == XHDRTYPE || hd->typeflag == GHDRTYPE) { |
| 788 |
if (rd_xheader(arcn, hd->typeflag == GHDRTYPE, |
| 789 |
(off_t)asc_ul(hd->size, sizeof(hd->size), OCT)) < 0) |
| 790 |
return (-1); |
| 791 |
|
| 792 |
|
| 793 |
if (rd_wrbuf(buf, BLKMULT) != BLKMULT) |
| 794 |
return (-1); |
| 795 |
if (ustar_id(buf, BLKMULT) < 0) |
| 796 |
return(-1); |
| 797 |
|
| 798 |
|
| 799 |
if (hd->typeflag == XHDRTYPE || hd->typeflag == GHDRTYPE) |
| 800 |
goto reset; |
| 801 |
} |
| 802 |
#endif |
| 803 |
|
| 804 |
if (!arcn->nlen) { |
| 805 |
|
| 806 |
|
| 807 |
|
| 808 |
|
| 809 |
|
| 810 |
dest = arcn->name; |
| 811 |
if (*(hd->prefix) != '\0') { |
| 812 |
cnt = fieldcpy(dest, sizeof(arcn->name) - 1, |
| 813 |
hd->prefix, sizeof(hd->prefix)); |
| 814 |
dest += cnt; |
| 815 |
*dest++ = '/'; |
| 816 |
cnt++; |
| 817 |
} else |
| 818 |
cnt = 0; |
| 819 |
|
| 820 |
if (hd->typeflag != LONGLINKTYPE && |
| 821 |
hd->typeflag != LONGNAMETYPE) { |
| 822 |
arcn->nlen = cnt + expandname(dest, |
| 823 |
sizeof(arcn->name) - cnt, &gnu_name_string, |
| 824 |
hd->name, sizeof(hd->name)); |
| 825 |
} |
| 826 |
} |
| 827 |
|
| 828 |
if (!arcn->ln_nlen && |
| 829 |
hd->typeflag != LONGLINKTYPE && hd->typeflag != LONGNAMETYPE) { |
| 830 |
arcn->ln_nlen = expandname(arcn->ln_name, sizeof(arcn->ln_name), |
| 831 |
&gnu_link_string, hd->linkname, sizeof(hd->linkname)); |
| 832 |
} |
| 833 |
|
| 834 |
|
| 835 |
|
| 836 |
|
| 837 |
|
| 838 |
arcn->sb.st_mode = (mode_t)(asc_ul(hd->mode, sizeof(hd->mode), OCT) & |
| 839 |
0xfff); |
| 840 |
arcn->sb.st_size = (off_t)asc_ull(hd->size, sizeof(hd->size), OCT); |
| 841 |
val = asc_ull(hd->mtime, sizeof(hd->mtime), OCT); |
| 842 |
if (val > MAX_TIME_T) |
| 843 |
arcn->sb.st_mtime = INT_MAX; |
| 844 |
else |
| 845 |
arcn->sb.st_mtime = val; |
| 846 |
arcn->sb.st_ctime = arcn->sb.st_atime = arcn->sb.st_mtime; |
| 847 |
|
| 848 |
|
| 849 |
|
| 850 |
|
| 851 |
|
| 852 |
|
| 853 |
|
| 854 |
hd->gname[sizeof(hd->gname) - 1] = '\0'; |
| 855 |
if ((anonarch & ANON_NUMID) || |
| 856 |
gid_name(hd->gname, &(arcn->sb.st_gid)) < 0) |
| 857 |
arcn->sb.st_gid = (gid_t)asc_ul(hd->gid, sizeof(hd->gid), OCT); |
| 858 |
hd->uname[sizeof(hd->uname) - 1] = '\0'; |
| 859 |
if ((anonarch & ANON_NUMID) || |
| 860 |
uid_name(hd->uname, &(arcn->sb.st_uid)) < 0) |
| 861 |
arcn->sb.st_uid = (uid_t)asc_ul(hd->uid, sizeof(hd->uid), OCT); |
| 862 |
|
| 863 |
|
| 864 |
|
| 865 |
|
| 866 |
arcn->pad = 0; |
| 867 |
arcn->skip = 0; |
| 868 |
arcn->sb.st_rdev = (dev_t)0; |
| 869 |
|
| 870 |
|
| 871 |
|
| 872 |
|
| 873 |
switch (hd->typeflag) { |
| 874 |
case FIFOTYPE: |
| 875 |
arcn->type = PAX_FIF; |
| 876 |
arcn->sb.st_mode |= S_IFIFO; |
| 877 |
break; |
| 878 |
case DIRTYPE: |
| 879 |
arcn->type = PAX_DIR; |
| 880 |
arcn->sb.st_mode |= S_IFDIR; |
| 881 |
arcn->sb.st_nlink = 2; |
| 882 |
|
| 883 |
|
| 884 |
|
| 885 |
|
| 886 |
|
| 887 |
|
| 888 |
if (arcn->name[arcn->nlen - 1] == '/') |
| 889 |
arcn->name[--arcn->nlen] = '\0'; |
| 890 |
break; |
| 891 |
case BLKTYPE: |
| 892 |
case CHRTYPE: |
| 893 |
|
| 894 |
|
| 895 |
|
| 896 |
if (hd->typeflag == BLKTYPE) { |
| 897 |
arcn->type = PAX_BLK; |
| 898 |
arcn->sb.st_mode |= S_IFBLK; |
| 899 |
} else { |
| 900 |
arcn->type = PAX_CHR; |
| 901 |
arcn->sb.st_mode |= S_IFCHR; |
| 902 |
} |
| 903 |
devmajor = (dev_t)asc_ul(hd->devmajor,sizeof(hd->devmajor),OCT); |
| 904 |
devminor = (dev_t)asc_ul(hd->devminor,sizeof(hd->devminor),OCT); |
| 905 |
arcn->sb.st_rdev = TODEV(devmajor, devminor); |
| 906 |
break; |
| 907 |
case SYMTYPE: |
| 908 |
case LNKTYPE: |
| 909 |
if (hd->typeflag == SYMTYPE) { |
| 910 |
arcn->type = PAX_SLK; |
| 911 |
arcn->sb.st_mode |= S_IFLNK; |
| 912 |
} else { |
| 913 |
arcn->type = PAX_HLK; |
| 914 |
|
| 915 |
|
| 916 |
|
| 917 |
arcn->sb.st_mode |= S_IFREG; |
| 918 |
arcn->sb.st_nlink = 2; |
| 919 |
} |
| 920 |
break; |
| 921 |
case LONGLINKTYPE: |
| 922 |
case LONGNAMETYPE: |
| 923 |
|
| 924 |
|
| 925 |
|
| 926 |
|
| 927 |
arcn->type = |
| 928 |
hd->typeflag == LONGLINKTYPE ? PAX_GLL : PAX_GLF; |
| 929 |
arcn->pad = TAR_PAD(arcn->sb.st_size); |
| 930 |
arcn->skip = arcn->sb.st_size; |
| 931 |
break; |
| 932 |
case CONTTYPE: |
| 933 |
case AREGTYPE: |
| 934 |
case REGTYPE: |
| 935 |
default: |
| 936 |
|
| 937 |
|
| 938 |
|
| 939 |
|
| 940 |
arcn->type = PAX_REG; |
| 941 |
arcn->pad = TAR_PAD(arcn->sb.st_size); |
| 942 |
arcn->skip = arcn->sb.st_size; |
| 943 |
arcn->sb.st_mode |= S_IFREG; |
| 944 |
break; |
| 945 |
} |
| 946 |
return(0); |
| 947 |
} |
| 948 |
|
| 949 |
|
| 950 |
|
| 951 |
|
| 952 |
|
| 953 |
|
| 954 |
|
| 955 |
|
| 956 |
|
| 957 |
|
| 958 |
|
| 959 |
|
| 960 |
|
| 961 |
int |
| 962 |
ustar_wr(ARCHD *arcn) |
| 963 |
{ |
| 964 |
HD_USTAR *hd; |
| 965 |
const char *name; |
| 966 |
char *pt, hdblk[sizeof(HD_USTAR)]; |
| 967 |
u_long t_uid, t_gid; |
| 968 |
time_t t_mtime; |
| 969 |
|
| 970 |
anonarch_init(); |
| 971 |
|
| 972 |
|
| 973 |
|
| 974 |
|
| 975 |
if (arcn->type == PAX_SCK) { |
| 976 |
paxwarn(1, "%s cannot archive a %s %s", |
| 977 |
"ustar", "socket", arcn->org_name); |
| 978 |
return (1); |
| 979 |
} |
| 980 |
|
| 981 |
#ifndef SMALL |
| 982 |
|
| 983 |
|
| 984 |
|
| 985 |
if (arcn->type == PAX_DIR && tar_nodir) |
| 986 |
return (1); |
| 987 |
#endif |
| 988 |
|
| 989 |
|
| 990 |
|
| 991 |
|
| 992 |
if (PAX_IS_LINK(arcn->type) && |
| 993 |
((size_t)arcn->ln_nlen > sizeof(hd->linkname))) { |
| 994 |
paxwarn(1, "%s name too long for %s %s", |
| 995 |
"Link", "ustar", arcn->ln_name); |
| 996 |
return (1); |
| 997 |
} |
| 998 |
|
| 999 |
|
| 1000 |
|
| 1001 |
|
| 1002 |
if ((anonarch & ANON_DIRSLASH) && arcn->type == PAX_DIR && |
| 1003 |
(size_t)arcn->nlen < (sizeof(arcn->name) - 1)) { |
| 1004 |
arcn->name[arcn->nlen++] = '/'; |
| 1005 |
arcn->name[arcn->nlen] = '\0'; |
| 1006 |
} |
| 1007 |
|
| 1008 |
|
| 1009 |
|
| 1010 |
|
| 1011 |
|
| 1012 |
if ((pt = name_split(arcn->name, arcn->nlen)) == NULL) { |
| 1013 |
paxwarn(1, "%s name too long for %s %s", |
| 1014 |
"File", "ustar", arcn->name); |
| 1015 |
return (1); |
| 1016 |
} |
| 1017 |
|
| 1018 |
|
| 1019 |
|
| 1020 |
|
| 1021 |
memset(hdblk, 0, sizeof(hdblk)); |
| 1022 |
hd = (HD_USTAR *)hdblk; |
| 1023 |
arcn->pad = 0; |
| 1024 |
|
| 1025 |
|
| 1026 |
|
| 1027 |
|
| 1028 |
if (pt != arcn->name) { |
| 1029 |
|
| 1030 |
|
| 1031 |
|
| 1032 |
|
| 1033 |
*pt = '\0'; |
| 1034 |
fieldcpy(hd->prefix, sizeof(hd->prefix), arcn->name, |
| 1035 |
sizeof(arcn->name)); |
| 1036 |
*pt++ = '/'; |
| 1037 |
} |
| 1038 |
|
| 1039 |
|
| 1040 |
|
| 1041 |
|
| 1042 |
|
| 1043 |
fieldcpy(hd->name, sizeof(hd->name), pt, |
| 1044 |
sizeof(arcn->name) - (pt - arcn->name)); |
| 1045 |
|
| 1046 |
t_uid = (anonarch & ANON_UIDGID) ? 0UL : (u_long)arcn->sb.st_uid; |
| 1047 |
t_gid = (anonarch & ANON_UIDGID) ? 0UL : (u_long)arcn->sb.st_gid; |
| 1048 |
t_mtime = (anonarch & ANON_MTIME) ? 0 : arcn->sb.st_mtime; |
| 1049 |
|
| 1050 |
|
| 1051 |
|
| 1052 |
|
| 1053 |
switch (arcn->type) { |
| 1054 |
case PAX_DIR: |
| 1055 |
hd->typeflag = DIRTYPE; |
| 1056 |
if (ul_oct(0, hd->size, sizeof(hd->size), 3)) |
| 1057 |
goto out; |
| 1058 |
break; |
| 1059 |
case PAX_CHR: |
| 1060 |
case PAX_BLK: |
| 1061 |
if (arcn->type == PAX_CHR) |
| 1062 |
hd->typeflag = CHRTYPE; |
| 1063 |
else |
| 1064 |
hd->typeflag = BLKTYPE; |
| 1065 |
if (ul_oct(MAJOR(arcn->sb.st_rdev), hd->devmajor, |
| 1066 |
sizeof(hd->devmajor), 3) || |
| 1067 |
ul_oct(MINOR(arcn->sb.st_rdev), hd->devminor, |
| 1068 |
sizeof(hd->devminor), 3) || |
| 1069 |
ul_oct(0, hd->size, sizeof(hd->size), 3)) |
| 1070 |
goto out; |
| 1071 |
break; |
| 1072 |
case PAX_FIF: |
| 1073 |
hd->typeflag = FIFOTYPE; |
| 1074 |
if (ul_oct(0, hd->size, sizeof(hd->size), 3)) |
| 1075 |
goto out; |
| 1076 |
break; |
| 1077 |
case PAX_SLK: |
| 1078 |
case PAX_HLK: |
| 1079 |
case PAX_HRG: |
| 1080 |
if (arcn->type == PAX_SLK) |
| 1081 |
hd->typeflag = SYMTYPE; |
| 1082 |
else |
| 1083 |
hd->typeflag = LNKTYPE; |
| 1084 |
fieldcpy(hd->linkname, sizeof(hd->linkname), arcn->ln_name, |
| 1085 |
sizeof(arcn->ln_name)); |
| 1086 |
if (ul_oct(0, hd->size, sizeof(hd->size), 3)) |
| 1087 |
goto out; |
| 1088 |
break; |
| 1089 |
case PAX_REG: |
| 1090 |
case PAX_CTG: |
| 1091 |
default: |
| 1092 |
|
| 1093 |
|
| 1094 |
|
| 1095 |
if (arcn->type == PAX_CTG) |
| 1096 |
hd->typeflag = CONTTYPE; |
| 1097 |
else |
| 1098 |
hd->typeflag = REGTYPE; |
| 1099 |
arcn->pad = TAR_PAD(arcn->sb.st_size); |
| 1100 |
if (ull_oct(arcn->sb.st_size, hd->size, sizeof(hd->size), 3)) { |
| 1101 |
paxwarn(1, "File is too large for %s format %s", |
| 1102 |
"ustar", arcn->org_name); |
| 1103 |
return (1); |
| 1104 |
} |
| 1105 |
break; |
| 1106 |
} |
| 1107 |
|
| 1108 |
memcpy(hd->magic, TMAGIC, TMAGLEN); |
| 1109 |
memcpy(hd->version, TVERSION, TVERSLEN); |
| 1110 |
|
| 1111 |
|
| 1112 |
|
| 1113 |
|
| 1114 |
|
| 1115 |
if (ul_oct(t_uid, hd->uid, sizeof(hd->uid), 3)) { |
| 1116 |
if (uid_nobody == 0) { |
| 1117 |
if (uid_name("nobody", &uid_nobody) == -1) |
| 1118 |
goto out; |
| 1119 |
} |
| 1120 |
if (uid_warn != t_uid) { |
| 1121 |
uid_warn = t_uid; |
| 1122 |
paxwarn(1, |
| 1123 |
"ustar header field is too small for %cid %lu, " |
| 1124 |
"using nobody", 'u', t_uid); |
| 1125 |
} |
| 1126 |
if (ul_oct(uid_nobody, hd->uid, sizeof(hd->uid), 3)) |
| 1127 |
goto out; |
| 1128 |
} |
| 1129 |
if (ul_oct(t_gid, hd->gid, sizeof(hd->gid), 3)) { |
| 1130 |
if (gid_nobody == 0) { |
| 1131 |
if (gid_name("nobody", &gid_nobody) == -1) |
| 1132 |
goto out; |
| 1133 |
} |
| 1134 |
if (gid_warn != t_gid) { |
| 1135 |
gid_warn = t_gid; |
| 1136 |
paxwarn(1, |
| 1137 |
"ustar header field is too small for %cid %lu, " |
| 1138 |
"using nobody", 'g', t_gid); |
| 1139 |
} |
| 1140 |
if (ul_oct(gid_nobody, hd->gid, sizeof(hd->gid), 3)) |
| 1141 |
goto out; |
| 1142 |
} |
| 1143 |
if (ull_oct(t_mtime < 0 ? 0 : t_mtime, hd->mtime, sizeof(hd->mtime), 3) || |
| 1144 |
ul_oct(arcn->sb.st_mode, hd->mode, sizeof(hd->mode), 3)) |
| 1145 |
goto out; |
| 1146 |
if (!(anonarch & ANON_NUMID)) { |
| 1147 |
if ((name = name_uid(t_uid, 0)) != NULL) |
| 1148 |
strncpy(hd->uname, name, sizeof(hd->uname)); |
| 1149 |
if ((name = name_gid(t_gid, 0)) != NULL) |
| 1150 |
strncpy(hd->gname, name, sizeof(hd->gname)); |
| 1151 |
} |
| 1152 |
|
| 1153 |
|
| 1154 |
|
| 1155 |
|
| 1156 |
|
| 1157 |
|
| 1158 |
if (ul_oct(tar_chksm(hdblk, sizeof(HD_USTAR)), hd->chksum, |
| 1159 |
sizeof(hd->chksum), 3)) |
| 1160 |
goto out; |
| 1161 |
|
| 1162 |
#ifndef SMALL |
| 1163 |
if (anonarch & ANON_DEBUG) { |
| 1164 |
tar_dbgfld(NULL, NULL, 0); |
| 1165 |
tar_dbgfld("writing name '", hd->name, TNMSZ); |
| 1166 |
tar_dbgfld("' mode ", hd->mode, 8); |
| 1167 |
tar_dbgfld(" uid ", hd->uid, 8); |
| 1168 |
tar_dbgfld(" (", hd->uname, 32); |
| 1169 |
tar_dbgfld(") gid ", hd->gid, 8); |
| 1170 |
tar_dbgfld(" (", hd->gname, 32); |
| 1171 |
tar_dbgfld(") size ", hd->size, 12); |
| 1172 |
tar_dbgfld(" mtime ", hd->mtime, 12); |
| 1173 |
tar_dbgfld(" type ", &(hd->typeflag), 1); |
| 1174 |
tar_dbgfld(" linked to '", hd->linkname, TNMSZ); |
| 1175 |
tar_dbgfld("' magic '", hd->magic, TMAGLEN); |
| 1176 |
tar_dbgfld("' v", hd->version, TVERSLEN); |
| 1177 |
tar_dbgfld(" device '", hd->devmajor, 8); |
| 1178 |
tar_dbgfld(":", hd->devminor, 8); |
| 1179 |
tar_dbgfld("' prefix '", hd->prefix, TPFSZ); |
| 1180 |
tar_dbgfld("' checksum ", hd->chksum, CHK_LEN); |
| 1181 |
tar_dbgfld(NULL, NULL, 1); |
| 1182 |
} |
| 1183 |
#endif |
| 1184 |
|
| 1185 |
if (wr_rdbuf(hdblk, sizeof(HD_USTAR)) < 0) |
| 1186 |
return(-1); |
| 1187 |
if (wr_skip(BLKMULT - sizeof(HD_USTAR)) < 0) |
| 1188 |
return(-1); |
| 1189 |
if (PAX_IS_REG(arcn->type)) |
| 1190 |
return(0); |
| 1191 |
return(1); |
| 1192 |
|
| 1193 |
out: |
| 1194 |
|
| 1195 |
|
| 1196 |
|
| 1197 |
paxwarn(1, "%s header field is too small for file %s", |
| 1198 |
"ustar", arcn->org_name); |
| 1199 |
return (1); |
| 1200 |
} |
| 1201 |
|
| 1202 |
|
| 1203 |
|
| 1204 |
|
| 1205 |
|
| 1206 |
|
| 1207 |
|
| 1208 |
|
| 1209 |
|
| 1210 |
|
| 1211 |
|
| 1212 |
|
| 1213 |
|
| 1214 |
static char * |
| 1215 |
name_split(char *name, int len) |
| 1216 |
{ |
| 1217 |
char *start; |
| 1218 |
|
| 1219 |
|
| 1220 |
|
| 1221 |
|
| 1222 |
|
| 1223 |
|
| 1224 |
|
| 1225 |
if (len <= TNMSZ) |
| 1226 |
return(name); |
| 1227 |
if (len > (TPFSZ + TNMSZ + 1)) |
| 1228 |
return(NULL); |
| 1229 |
|
| 1230 |
|
| 1231 |
|
| 1232 |
|
| 1233 |
|
| 1234 |
|
| 1235 |
|
| 1236 |
|
| 1237 |
start = name + len - TNMSZ - 1; |
| 1238 |
|
| 1239 |
|
| 1240 |
|
| 1241 |
|
| 1242 |
|
| 1243 |
|
| 1244 |
|
| 1245 |
if (start == name) |
| 1246 |
++start; |
| 1247 |
|
| 1248 |
while ((*start != '\0') && (*start != '/')) |
| 1249 |
++start; |
| 1250 |
|
| 1251 |
|
| 1252 |
|
| 1253 |
|
| 1254 |
|
| 1255 |
if (*start == '\0') |
| 1256 |
return(NULL); |
| 1257 |
|
| 1258 |
|
| 1259 |
|
| 1260 |
|
| 1261 |
|
| 1262 |
if ((start - name) > TPFSZ) |
| 1263 |
return(NULL); |
| 1264 |
|
| 1265 |
|
| 1266 |
|
| 1267 |
|
| 1268 |
return(start); |
| 1269 |
} |
| 1270 |
|
| 1271 |
static size_t |
| 1272 |
expandname(char *buf, size_t len, char **gnu_name, const char *name, |
| 1273 |
size_t limit) |
| 1274 |
{ |
| 1275 |
size_t nlen; |
| 1276 |
|
| 1277 |
if (*gnu_name) { |
| 1278 |
|
| 1279 |
if ((nlen = strlcpy(buf, *gnu_name, len)) >= len) |
| 1280 |
nlen = len - 1; |
| 1281 |
free(*gnu_name); |
| 1282 |
*gnu_name = NULL; |
| 1283 |
} else |
| 1284 |
nlen = fieldcpy(buf, len, name, limit); |
| 1285 |
return(nlen); |
| 1286 |
} |
| 1287 |
|
| 1288 |
#ifndef SMALL |
| 1289 |
static void |
| 1290 |
tar_dbgfld(const char *pfx, const char *sp, size_t len) |
| 1291 |
{ |
| 1292 |
static char fbuf[256]; |
| 1293 |
char tbuf[256], *s; |
| 1294 |
|
| 1295 |
if ((pfx == NULL) || (sp == NULL)) { |
| 1296 |
if ((pfx == NULL) && (sp == NULL)) { |
| 1297 |
if (len == 0) { |
| 1298 |
*fbuf = 0; |
| 1299 |
} else { |
| 1300 |
paxwarn(0, "%s", fbuf); |
| 1301 |
} |
| 1302 |
} else |
| 1303 |
paxwarn(0, "tar_dbgfld: wrong call"); |
| 1304 |
return; |
| 1305 |
} |
| 1306 |
|
| 1307 |
strlcat(fbuf, pfx, sizeof (fbuf)); |
| 1308 |
|
| 1309 |
if (len == 0) |
| 1310 |
return; |
| 1311 |
|
| 1312 |
if (len > (sizeof (tbuf) - 1)) |
| 1313 |
len = sizeof (tbuf) - 1; |
| 1314 |
|
| 1315 |
memmove(s = tbuf, sp, len); |
| 1316 |
tbuf[len] = 0; |
| 1317 |
while (*s == ' ') |
| 1318 |
++s; |
| 1319 |
while (s[strlen(s) - 1] == ' ') |
| 1320 |
s[strlen(s) - 1] = 0; |
| 1321 |
|
| 1322 |
strlcat(fbuf, tbuf, sizeof (fbuf)); |
| 1323 |
} |
| 1324 |
|
| 1325 |
|
| 1326 |
#define MINXHDRSZ 5 |
| 1327 |
|
| 1328 |
|
| 1329 |
#define MAXXHDRSZ BLKMULT |
| 1330 |
|
| 1331 |
static int |
| 1332 |
rd_xheader(ARCHD *arcn, int global, off_t size) |
| 1333 |
{ |
| 1334 |
char buf[MAXXHDRSZ]; |
| 1335 |
long len; |
| 1336 |
char *delim, *keyword; |
| 1337 |
char *nextp, *p, *end; |
| 1338 |
int pad, ret = 0; |
| 1339 |
|
| 1340 |
|
| 1341 |
pad = TAR_PAD((unsigned)size); |
| 1342 |
|
| 1343 |
p = end = buf; |
| 1344 |
while (size > 0 || p < end) { |
| 1345 |
if (size > 0) { |
| 1346 |
int rdlen; |
| 1347 |
|
| 1348 |
|
| 1349 |
if (p > buf) { |
| 1350 |
memmove(buf, p, end - p); |
| 1351 |
end -= p - buf; |
| 1352 |
p = buf; |
| 1353 |
} |
| 1354 |
|
| 1355 |
|
| 1356 |
rdlen = MINIMUM(size, (buf + sizeof buf) - end); |
| 1357 |
if (rd_wrbuf(end, rdlen) != rdlen) { |
| 1358 |
ret = -1; |
| 1359 |
break; |
| 1360 |
} |
| 1361 |
size -= rdlen; |
| 1362 |
end += rdlen; |
| 1363 |
} |
| 1364 |
|
| 1365 |
|
| 1366 |
if (memchr(p, ' ', end - p) == NULL || |
| 1367 |
!isdigit((unsigned char)*p)) { |
| 1368 |
paxwarn(1, "Invalid extended header record"); |
| 1369 |
ret = -1; |
| 1370 |
break; |
| 1371 |
} |
| 1372 |
errno = 0; |
| 1373 |
len = strtol(p, &delim, 10); |
| 1374 |
if (*delim != ' ' || (errno == ERANGE && len == LONG_MAX) || |
| 1375 |
len < MINXHDRSZ) { |
| 1376 |
paxwarn(1, "%s length", |
| 1377 |
"Invalid extended header record"); |
| 1378 |
ret = -1; |
| 1379 |
break; |
| 1380 |
} |
| 1381 |
if (len > end - p) { |
| 1382 |
paxwarn(1, "Extended header record length %lu is " |
| 1383 |
"out of range", len); |
| 1384 |
|
| 1385 |
len -= end - p; |
| 1386 |
if (len <= size && rd_skip(len) == 0) { |
| 1387 |
size -= len; |
| 1388 |
p = end = buf; |
| 1389 |
continue; |
| 1390 |
} |
| 1391 |
ret = -1; |
| 1392 |
break; |
| 1393 |
} |
| 1394 |
nextp = p + len; |
| 1395 |
keyword = p = delim + 1; |
| 1396 |
p = memchr(p, '=', len); |
| 1397 |
if (!p || nextp[-1] != '\n') { |
| 1398 |
paxwarn(1, "Malformed extended header record"); |
| 1399 |
ret = -1; |
| 1400 |
break; |
| 1401 |
} |
| 1402 |
*p++ = nextp[-1] = '\0'; |
| 1403 |
if (!global) { |
| 1404 |
if (!strcmp(keyword, "path")) { |
| 1405 |
arcn->nlen = strlcpy(arcn->name, p, |
| 1406 |
sizeof(arcn->name)); |
| 1407 |
} else if (!strcmp(keyword, "linkpath")) { |
| 1408 |
arcn->ln_nlen = strlcpy(arcn->ln_name, p, |
| 1409 |
sizeof(arcn->ln_name)); |
| 1410 |
} |
| 1411 |
} |
| 1412 |
p = nextp; |
| 1413 |
} |
| 1414 |
|
| 1415 |
if (rd_skip(size + pad) < 0) |
| 1416 |
return (-1); |
| 1417 |
return (ret); |
| 1418 |
} |
| 1419 |
#endif |