1 /*
   2  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright 2013 Nexenta Systems, Inc. All rights reserved.
   4  */
   5 
   6 /*
   7  * BSD 3 Clause License
   8  *
   9  * Copyright (c) 2007, The Storage Networking Industry Association.
  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  *      - Redistributions of source code must retain the above copyright
  15  *        notice, this list of conditions and the following disclaimer.
  16  *
  17  *      - Redistributions in binary form must reproduce the above copyright
  18  *        notice, this list of conditions and the following disclaimer in
  19  *        the documentation and/or other materials provided with the
  20  *        distribution.
  21  *
  22  *      - Neither the name of The Storage Networking Industry Association (SNIA)
  23  *        nor the names of its contributors may be used to endorse or promote
  24  *        products derived from this software without specific prior written
  25  *        permission.
  26  *
  27  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  28  * AND 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 COPYRIGHT OWNER OR CONTRIBUTORS BE
  31  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  37  * POSSIBILITY OF SUCH DAMAGE.
  38  */
  39 #include <stdlib.h>
  40 #include <ctype.h>
  41 #include <stdio.h>
  42 #include <limits.h>
  43 #include <string.h>
  44 #include <time.h>
  45 #include <sys/types.h>
  46 #include <sys/acl.h>
  47 #include <sys/mkdev.h>
  48 #include <utime.h>
  49 #include <unistd.h>
  50 #include <pthread.h>
  51 #include <archives.h>
  52 #include <priv.h>
  53 #include <tlm.h>
  54 #include <libzfs.h>
  55 #include <pwd.h>
  56 #include <grp.h>
  57 #include <ndmpd_prop.h>
  58 #include "tlm_proto.h"
 
 
 211         return (sp->se_name);
 212 }
 213 
 214 /*
 215  * NBU and EBS may not send us the correct file list containing hardlinks
 216  * during a DAR restore, e.g. they appear always send the first name
 217  * associated with an inode, even if other link names were
 218  * selected for the restore.  As a workaround, we use the file name entry
 219  * in sels[] (ignore the name in the tar header) as restore target.
 220  */
 221 static char *
 222 rs_darhl_new_name(struct rs_name_maker *rnp, char *name, char **sels, int *pos,
 223     char *longname)
 224 {
 225         int x;
 226 
 227         for (x = 0; sels[x] != NULL; x++) {
 228                 if (strcmp(sels[x], " ")) {
 229                         *pos = x;
 230                         (void) strlcpy(longname, sels[x], TLM_MAX_PATH_NAME);
 231                         NDMP_LOG(LOG_DEBUG,
 232                             "to replace hardlink name [%s], pos [%d]",
 233                             longname, *pos);
 234 
 235                         return (rs_new_name(rnp, name, *pos, longname));
 236                 }
 237         }
 238 
 239         return (NULL);
 240 }
 241 
 242 
 243 /*
 244  * Main dir restore function for tar
 245  *
 246  * If this function returns non-zero return value it means that fatal error
 247  * was encountered.
 248  */
 249 int
 250 tar_getdir(tlm_commands_t *commands,
 251     tlm_cmd_t *local_commands,
 252     tlm_job_stats_t *job_stats,
 253     struct rs_name_maker *rnp,
 254     int lib,
 255     int drv,
 256     char **sels, /* what to get off the tape */
 257     char **exls, /* what to leave behind */
 258     int flags,
 259     int DAR,
 260     char *bk_path,
 261     struct hardlink_q *hardlink_q)
 262 {
 263         int     fp = 0;         /* file being restored ... */
 264                                 /*  ...need to preserve across volume changes */
 265         tlm_acls_t *acls;       /* file access info */
 266         char    *longname;
 267         boolean_t is_long_name = FALSE;
 268         char    *longlink;
 269         char    *hugename;
 270         longlong_t huge_size = 0;       /* size of a HUGE file */
 271         long    acl_spot;               /* any ACL info on the next volume */
 272         long    file_size;              /* size of file to restore */
 273         long    size_left = 0;          /* need this after volume change */
 274         int     last_action = 0;        /* what we are doing at EOT */
 275         boolean_t multi_volume = FALSE; /* is this a multi-volume switch ? */
 276         int     chk_rv;                 /* scratch area */
 277 
 278         int     mchtype, pos;
 279                                         /*
 280                                          * if an exact match is found for
 281                                          * restore and its position in the
 282                                          * selections list
 283                                          */
 284         int     nzerohdr;               /* the number of empty tar headers */
 285         int     rv;
 286         long nm_end, lnk_end;
 287         char    *name, *nmp;
 288         cstack_t *stp;
 289         char    *bkpath;
 290         char    *parentlnk;
 291         int dir_dar = 0;
 292 
 
 320                 free(longname);
 321                 free(longlink);
 322                 free(hugename);
 323                 free(parentlnk);
 324                 free(name);
 325                 free(acls);
 326                 free(thname_buf);
 327                 return (-TLM_NO_SCRATCH_SPACE);
 328         }
 329 
 330         acl_spot = 0;
 331         *hugename = '\0';
 332         *parentlnk = '\0';
 333         nm_end = 0;
 334         *longname = '\0';
 335         lnk_end = 0;
 336         *longlink = '\0';
 337         (void) memset(acls, 0, sizeof (tlm_acls_t));
 338         if (IS_SET(flags, RSFLG_OVR_ALWAYS)) {
 339                 acls->acl_overwrite = TRUE;
 340                 NDMP_LOG(LOG_DEBUG, "RSFLG_OVR_ALWAYS");
 341         } else if (IS_SET(flags, RSFLG_OVR_UPDATE)) {
 342                 acls->acl_update = TRUE;
 343                 NDMP_LOG(LOG_DEBUG, "RSFLG_OVR_UPDATE");
 344         }
 345 
 346         /*
 347          * work
 348          */
 349         rv = 0;
 350         nzerohdr = 0;
 351         while (commands->tcs_writer != TLM_ABORT &&
 352             local_commands->tc_writer != TLM_STOP && rv == 0) {
 353                 tlm_tar_hdr_t fake_tar_hdr;
 354                 char    *file_name;
 355                 char    *link_name;
 356                 int     erc;
 357                 int     actual_size;
 358                 boolean_t want_this_file;
 359                 int     want = sizeof (tlm_tar_hdr_t);
 360                 tlm_tar_hdr_t *tar_hdr;
 361 
 362                 /* The inode of an LF_LINK type. */
 363                 unsigned long hardlink_inode = 0;
 
 366                  * Indicate whether a file with the same inode has been
 367                  * restored.
 368                  */
 369                 int hardlink_done = 0;
 370 
 371                 /* The path of the restored hardlink file */
 372                 char *hardlink_target = NULL;
 373                 int is_hardlink = 0;
 374 
 375                 /*
 376                  * Whether a temporary file should be created for restoring
 377                  * hardlink.
 378                  */
 379                 int hardlink_tmp_file = 0;
 380                 char *hardlink_tmp_name = ".tmphlrsnondar";
 381 
 382                 /* used to make up hardlink_tmp_name */
 383                 static int hardlink_tmp_idx = 0;
 384 
 385                 if (multi_volume) {
 386                         NDMP_LOG(LOG_DEBUG, "multi_volume %c %d",
 387                             last_action, size_left);
 388 
 389                         /*
 390                          * the previous volume is out of data
 391                          * and is back in the rack, a new tape
 392                          * is loaded and ready to read.
 393                          *
 394                          * We need to pick up where we left off.
 395                          */
 396                         (void) memset(&fake_tar_hdr, 0, sizeof (fake_tar_hdr));
 397                         file_size = size_left;
 398                         tar_hdr = &fake_tar_hdr;
 399                         tar_hdr->th_linkflag = last_action;
 400 
 401                         multi_volume = FALSE;
 402                         last_action = 0;
 403                 } else {
 404                         tar_hdr = (tlm_tar_hdr_t *)get_read_buffer(want,
 405                             &erc, &actual_size, local_commands);
 406 
 
 417                          */
 418                         if (actual_size < want) {
 419                                 /*
 420                                  * EOF hits here
 421                                  *
 422                                  * wait for another buffer to come along
 423                                  * or until the Reader thread tells us
 424                                  * that no more tapes will be loaded ...
 425                                  * time to stop.
 426                                  */
 427                                 continue;
 428                         }
 429 
 430                         /*
 431                          * check for "we are lost"
 432                          */
 433                         chk_rv = tlm_vfy_tar_checksum(tar_hdr);
 434                         if (chk_rv == 0) {
 435                                 /* one of the end of tar file marks */
 436                                 if (++nzerohdr >= 2) {
 437                                         NDMP_LOG(LOG_DEBUG,
 438                                             "nzerohdr %d, breaking",
 439                                             nzerohdr);
 440                                         /* end of tar file */
 441                                         break;
 442                                 }
 443                                 NDMP_LOG(LOG_DEBUG, "nzerohdr %d, continuing",
 444                                     nzerohdr);
 445                                 continue;
 446                         } else if (chk_rv < 0) {
 447                                 nzerohdr = 0;
 448                                 /* skip this record */
 449                                 continue;
 450                         }
 451                         nzerohdr = 0;
 452 
 453                         /*
 454                          * When files are spanned to the next tape, the
 455                          * information of the acls must not be over-written
 456                          * by the information of the LF_MULTIVOL and LF_VOLHDR
 457                          * header, whose information is irrelevant to the file.
 458                          * The information of the original header must be
 459                          * kept in the 'acl'.
 460                          */
 461                         if (tar_hdr->th_linkflag != LF_MULTIVOL &&
 462                             tar_hdr->th_linkflag != LF_VOLHDR) {
 463                                 if (tar_hdr->th_linkflag != LF_HUMONGUS) {
 
 467                                             oct_atoi(tar_hdr->th_size);
 468                                         acls->acl_attr.st_uid =
 469                                             oct_atoi(tar_hdr->th_uid);
 470                                         acls->acl_attr.st_gid =
 471                                             oct_atoi(tar_hdr->th_gid);
 472                                         acls->acl_attr.st_mtime =
 473                                             oct_atoi(tar_hdr->th_mtime);
 474                                         (void) strlcpy(acls->uname,
 475                                             tar_hdr->th_uname,
 476                                             sizeof (acls->uname));
 477                                         (void) strlcpy(acls->gname,
 478                                             tar_hdr->th_gname,
 479                                             sizeof (acls->gname));
 480                                 }
 481                                 file_size = oct_atoi(tar_hdr->th_size);
 482                                 acl_spot = 0;
 483                                 last_action = tar_hdr->th_linkflag;
 484                         }
 485                 }
 486 
 487                 NDMP_LOG(LOG_DEBUG, "n [%s] f [%c] s %lld m %o u %d g %d t %d",
 488                     tar_hdr->th_name, tar_hdr->th_linkflag,
 489                     acls->acl_attr.st_size, acls->acl_attr.st_mode,
 490                     acls->acl_attr.st_uid, acls->acl_attr.st_gid,
 491                     acls->acl_attr.st_mtime);
 492 
 493                 /*
 494                  * If the restore is running using DAR we should check for
 495                  * extended attribute entries
 496                  */
 497                 if (dar_recovered &&
 498                     tar_hdr->th_linkflag != LF_XATTR)
 499                         break;
 500 
 501                 rs_create_new_bkpath(bk_path, tar_hdr->th_name, thname_buf);
 502 
 503                 switch (tar_hdr->th_linkflag) {
 504                 case LF_MULTIVOL:
 505                         multi_volume = TRUE;
 506                         break;
 507                 case LF_LINK:
 508                         is_hardlink = 1;
 509                         hardlink_inode =
 510                             oct_atoi(tar_hdr->th_shared.th_hlink_ino);
 511 
 512                         /*
 513                          * Check if we have restored a link with the same inode
 514                          * If the inode is 0, we have to restore it as a
 515                          * regular file.
 516                          */
 517                         if (hardlink_inode) {
 518                                 hardlink_done = !hardlink_q_get(hardlink_q,
 519                                     hardlink_inode, 0, &hardlink_target);
 520                         }
 521 
 522                         if (hardlink_done) {
 523                                 NDMP_LOG(LOG_DEBUG,
 524                                     "found hardlink, inode = %u, target = [%s]",
 525                                     hardlink_inode,
 526                                     hardlink_target? hardlink_target : "--");
 527 
 528                                 /* create a hardlink to hardlink_target */
 529                                 file_name = (*longname == 0) ?
 530                                     thname_buf : longname;
 531 
 532                                 if (!is_file_wanted(file_name, sels, exls,
 533                                     flags, &mchtype, &pos)) {
 534                                         nmp = NULL;
 535                                         /*
 536                                          * This means that DMA did not send us
 537                                          * the correct fh_info for the file
 538                                          * in restore list.  We use the file
 539                                          * name entry in sels[] (ignore the
 540                                          * name in the tar header) as restore
 541                                          * target.
 542                                          */
 543                                         if (DAR) {
 544                                                 nmp = rs_darhl_new_name(rnp,
 545                                                     name, sels, &pos,
 546                                                     file_name);
 547                                         }
 548                                 } else {
 549                                         nmp = rs_new_name(rnp, name, pos,
 550                                             file_name);
 551                                         if (!nmp) {
 552                                                 NDMP_LOG(LOG_DEBUG,
 553                                                     "can't make name for %s",
 554                                                     longname);
 555                                         }
 556                                 }
 557 
 558                                 if (nmp) {
 559                                         if (hardlink_target) {
 560                                                 erc = create_hard_link(
 561                                                     hardlink_target, nmp,
 562                                                     acls, job_stats);
 563                                                 if (ERROR_IS_FATAL(erc)) {
 564                                                         rv = erc;
 565                                                         continue;
 566                                                 }
 567                                                 if (erc == 0) {
 568                                                         (void)
 569                                                             tlm_entry_restored(
 570                                                             job_stats,
 571                                                             file_name, pos);
 572                                                         NDMP_LOG(LOG_DEBUG,
 573                                                             "restored %s -> %s",
 574                                                             nmp,
 575                                                             hardlink_target);
 576                                                 }
 577                                         } else {
 578                                                 NDMP_LOG(LOG_DEBUG,
 579                                                     "no target for hardlink %s",
 580                                                     nmp);
 581                                         }
 582 
 583                                         name[0] = 0;
 584                                         is_long_name = FALSE;
 585                                 }
 586 
 587                                 nm_end = 0;
 588                                 longname[0] = 0;
 589                                 lnk_end = 0;
 590                                 longlink[0] = 0;
 591 
 592                                 break;
 593                         }
 594                         /* otherwise fall through, restore like a normal file */
 595                         /*FALLTHROUGH*/
 596                 case LF_OLDNORMAL:
 597                         /*
 598                          * check for TAR's end-of-tape method
 
 673                          * restore were interrupted, e.g. by a system reboot,
 674                          * they would have to be cleaned up manually in order
 675                          * for the disk space to be freed.
 676                          *
 677                          * If tmplink_dir is NULL, no temperorary files are
 678                          * created during a restore.  This may result in some
 679                          * hardlinks not being restored during a partial
 680                          * restore.
 681                          */
 682                         if (is_hardlink && !DAR && !want_this_file && !nmp) {
 683                                 if (tmplink_dir) {
 684                                         (void) snprintf(name, TLM_MAX_PATH_NAME,
 685                                             "%s/%s_%d", tmplink_dir,
 686                                             hardlink_tmp_name,
 687                                             hardlink_tmp_idx);
 688                                         nmp = name;
 689 
 690                                         hardlink_tmp_idx++;
 691                                         hardlink_tmp_file = 1;
 692                                         want_this_file = TRUE;
 693                                         NDMP_LOG(LOG_DEBUG,
 694                                             "To restore temp hardlink file %s.",
 695                                             nmp);
 696                                 } else {
 697                                         NDMP_LOG(LOG_DEBUG,
 698                                             "No tmplink_dir specified.");
 699                                 }
 700                         }
 701 
 702                         rv = restore_file(&fp, nmp, file_size,
 703                             huge_size, acls, want_this_file, local_commands,
 704                             job_stats, &size_left);
 705                         if (rv != 0)
 706                                 continue;
 707 
 708                         /*
 709                          * In the case of non-DAR, we have to record the first
 710                          * link for an inode that has multiple links. That's
 711                          * the only link with data records actually backed up.
 712                          * In this way, when we run into the other links, they
 713                          * will be treated as links, and we won't go to look
 714                          * for the data records to restore.  This is not a
 715                          * problem for DAR, where DMA tells the tape where
 716                          * to locate the data records.
 717                          */
 718                         if (is_hardlink && !DAR) {
 719                                 if (hardlink_q_add(hardlink_q, hardlink_inode,
 720                                     0, nmp, hardlink_tmp_file))
 721                                         NDMP_LOG(LOG_DEBUG,
 722                                             "failed to add (%u, %s) to HL q",
 723                                             hardlink_inode, nmp);
 724                         }
 725 
 726                         /* remove / reverse the temporary stuff */
 727                         if (hardlink_tmp_file) {
 728                                 nmp = NULL;
 729                                 want_this_file = FALSE;
 730                                 hardlink_tmp_file = 0;
 731                         }
 732 
 733                         /*
 734                          * Check if it is time to set the attribute
 735                          * of the restored directory
 736                          */
 737                         while (nmp && ((bkpath = dtree_peek(stp)) != NULL)) {
 738                                 int erc;
 739 
 740                                 if (strstr(nmp, bkpath))
 741                                         break;
 742 
 743                                 erc = dtree_pop(stp);
 744                                 if (ERROR_IS_FATAL(erc)) {
 745                                         rv = erc;
 746                                         break;
 747                                 }
 748                         }
 749                         if (rv != 0)
 750                                 continue;
 751 
 752                         NDMP_LOG(LOG_DEBUG, "sizeleft %s %d, %lld", longname,
 753                             size_left, huge_size);
 754 
 755                         if (want_this_file) {
 756                                 job_stats->js_bytes_total += file_size;
 757                                 job_stats->js_files_total++;
 758                         }
 759 
 760                         huge_size -= file_size;
 761                         if (huge_size < 0) {
 762                                 huge_size = 0;
 763                         }
 764                         if (size_left == 0 && huge_size == 0) {
 765                                 if (PM_EXACT_OR_CHILD(mchtype)) {
 766                                         (void) tlm_entry_restored(job_stats,
 767                                             longname, pos);
 768 
 769                                         /*
 770                                          * Add an entry to hardlink_q to record
 771                                          * this hardlink.
 772                                          */
 773                                         if (is_hardlink) {
 774                                                 NDMP_LOG(LOG_DEBUG,
 775                                                     "Restored hardlink file %s",
 776                                                     nmp);
 777 
 778                                                 if (DAR) {
 779                                                         (void) hardlink_q_add(
 780                                                             hardlink_q,
 781                                                             hardlink_inode, 0,
 782                                                             nmp, 0);
 783                                                 }
 784                                         }
 785                                 }
 786 
 787                                 nm_end = 0;
 788                                 longname[0] = 0;
 789                                 lnk_end = 0;
 790                                 longlink[0] = 0;
 791                                 hugename[0] = 0;
 792                                 name[0] = 0;
 793                                 is_long_name = FALSE;
 794                         }
 795                         break;
 796                 case LF_XATTR:
 797                         file_name = (*longname == 0) ? thname_buf :
 798                             longname;
 799 
 800                         size_left = restore_xattr_hdr(&fp, parentlnk,
 801                             file_name, file_size, acls, local_commands,
 802                             job_stats);
 803 
 804                         break;
 805                 case LF_SYMLINK:
 806                         file_name = (*longname == 0) ? thname_buf :
 807                             longname;
 808                         link_name = (*longlink == 0) ?
 809                             tar_hdr->th_linkname : longlink;
 810                         NDMP_LOG(LOG_DEBUG, "file_name[%s]", file_name);
 811                         NDMP_LOG(LOG_DEBUG, "link_name[%s]", link_name);
 812                         if (is_file_wanted(file_name, sels, exls, flags,
 813                             &mchtype, &pos)) {
 814                                 nmp = rs_new_name(rnp, name, pos, file_name);
 815                                 if (nmp) {
 816                                         erc = create_sym_link(nmp, link_name,
 817                                             acls, job_stats);
 818                                         if (ERROR_IS_FATAL(erc)) {
 819                                                 rv = erc;
 820                                                 continue;
 821                                         }
 822                                         if (erc == 0 &&
 823                                             PM_EXACT_OR_CHILD(mchtype))
 824                                                 (void) tlm_entry_restored(
 825                                                     job_stats, file_name, pos);
 826                                         name[0] = 0;
 827                                 }
 828                         }
 829                         nm_end = 0;
 830                         longname[0] = 0;
 831                         lnk_end = 0;
 
 903                                         if (erc == 0 &&
 904                                             PM_EXACT_OR_CHILD(mchtype))
 905                                                 (void) tlm_entry_restored(
 906                                                     job_stats, file_name, pos);
 907                                         name[0] = 0;
 908                                 }
 909                         }
 910                         nm_end = 0;
 911                         longname[0] = 0;
 912                         lnk_end = 0;
 913                         longlink[0] = 0;
 914                         break;
 915                 case LF_LONGLINK:
 916                         file_size = min(file_size,
 917                             TLM_MAX_PATH_NAME - lnk_end - 1);
 918                         file_size = max(0, file_size);
 919                         size_left = get_long_name(lib, drv, file_size, longlink,
 920                             &lnk_end, local_commands);
 921 
 922                         if (size_left != 0)
 923                                 NDMP_LOG(LOG_DEBUG,
 924                                     "fsize %d sleft %d lnkend %d",
 925                                     file_size, size_left, lnk_end);
 926                         break;
 927                 case LF_LONGNAME:
 928                         file_size = min(file_size,
 929                             TLM_MAX_PATH_NAME - nm_end - 1);
 930                         file_size = max(0, file_size);
 931                         size_left = get_long_name(lib, drv, file_size, longname,
 932                             &nm_end, local_commands);
 933 
 934                         if (size_left != 0)
 935                                 NDMP_LOG(LOG_DEBUG,
 936                                     "fsize %d sleft %d nmend %d",
 937                                     file_size, size_left, nm_end);
 938                         is_long_name = TRUE;
 939                         break;
 940                 case LF_ACL:
 941                         size_left = load_acl_info(lib, drv, file_size, acls,
 942                             &acl_spot, local_commands);
 943                         break;
 944                 case LF_VOLHDR:
 945                         break;
 946                 case LF_HUMONGUS:
 947                         (void) memset(hugename, 0, TLM_MAX_PATH_NAME);
 948                         (void) get_humongus_file_header(lib, drv, file_size,
 949                             &huge_size, hugename, local_commands);
 950                         break;
 951                 default:
 952                         break;
 953 
 954                 }
 955 
 
 
 981         free(hugename);
 982         free(name);
 983         free(thname_buf);
 984         return (rv);
 985 }
 986 
 987 /*
 988  * Main file restore function for tar (should run as a thread)
 989  */
 990 int
 991 tar_getfile(tlm_backup_restore_arg_t *argp)
 992 {
 993         tlm_job_stats_t *job_stats;
 994         char    **sels;         /* list of files desired */
 995         char    **exls;         /* list of files not wanted */
 996         char    *dir;           /* where to restore the files */
 997         char    job[TLM_MAX_BACKUP_JOB_NAME+1];
 998                                 /* the restore job name */
 999         int     erc;            /* error return codes */
1000         int     flags;
1001         struct  rs_name_maker rn;
1002         tlm_commands_t *commands;
1003         tlm_cmd_t *local_commands;
1004         char *list = NULL;
1005 
1006         commands = argp->ba_commands;
1007         local_commands = argp->ba_cmd;
1008 
1009         flags = 0;
1010 
1011         dir = ndmp_malloc(TLM_MAX_PATH_NAME);
1012         if (dir == NULL) {
1013                 local_commands->tc_reader = TLM_STOP;
1014                 (void) pthread_barrier_wait(&argp->ba_barrier);
1015                 return (-1);
1016         }
1017 
1018         (void) strlcpy(job, argp->ba_job, TLM_MAX_BACKUP_JOB_NAME+1);
1019         (void) strlcpy(dir, argp->ba_dir, TLM_MAX_PATH_NAME);
1020 
1021         flags |= RSFLG_OVR_ALWAYS;
1022         flags |= RSFLG_IGNORE_CASE;
1023 
1024         /*
1025          * do not test for "dir" having no string, since that
1026          * is a legal condition.  Restore to origional location
1027          * will not have a restore directory.
1028          */
1029         if (*job == '\0') {
1030                 NDMP_LOG(LOG_DEBUG, "No job defined");
1031                 local_commands->tc_reader = TLM_STOP;
1032                 free(dir);
1033                 (void) pthread_barrier_wait(&argp->ba_barrier);
1034                 return (-1);
1035         }
1036 
1037         sels = argp->ba_sels;
1038         if (sels == NULL) {
1039                 local_commands->tc_reader = TLM_STOP;
1040                 free(dir);
1041                 (void) pthread_barrier_wait(&argp->ba_barrier);
1042                 return (-1);
1043         }
1044         exls = &list;
1045 
1046         tlm_log_list("selections", sels);
1047         tlm_log_list("exclusions", exls);
1048 
1049         if (wildcard_enabled())
1050                 flags |= RSFLG_MATCH_WCARD;
1051 
1052         local_commands->tc_ref++;
1053         commands->tcs_writer_count++;
1054 
1055         /*
1056          * let the launcher continue
1057          */
1058         (void) pthread_barrier_wait(&argp->ba_barrier);
1059 
1060         job_stats = tlm_ref_job_stats(job);
1061 
1062         rn.rn_fp = catnames;
1063         rn.rn_nlp = dir;
1064 
1065         /*
1066          * work
1067          */
1068         NDMP_LOG(LOG_DEBUG, "start restore job %s", job);
1069         erc = tar_getdir(commands, local_commands, job_stats, &rn, 1, 1,
1070             sels, exls, flags, 0, NULL, NULL);
1071 
1072         /*
1073          * teardown
1074          */
1075         NDMP_LOG(LOG_DEBUG, "end restore job %s", job);
1076         tlm_un_ref_job_stats(job);
1077         tlm_release_list(sels);
1078         tlm_release_list(exls);
1079 
1080         commands->tcs_writer_count--;
1081         local_commands->tc_reader = TLM_STOP;
1082         tlm_release_reader_writer_ipc(local_commands);
1083         free(dir);
1084         return (erc);
1085 }
1086 
1087 /*
1088  * Creates the directories all the way down to the
1089  * end if they dont exist
1090  */
1091 int
1092 make_dirs(char *dir)
1093 {
1094         char c;
1095         char *cp, *end;
1096         struct stat64 st;
1097 
1098         cp = dir;
1099         cp += strspn(cp, "/");
1100         end = dir + strlen(dir);
1101         do {
1102                 if (*cp == '\0' || *cp == '/') {
1103                         c = *cp;
1104                         *cp = '\0';
1105                         if (lstat64(dir, &st) < 0)
1106                                 if (mkdir(dir, 0777) < 0) {
1107                                         NDMP_LOG(LOG_DEBUG, "Error %d"
1108                                             " creating directory %s",
1109                                             errno, dir);
1110                                         *cp = c;
1111                                         return (errno);
1112                                 }
1113 
1114                         *cp = c;
1115                 }
1116         } while (++cp <= end);
1117 
1118         return (0);
1119 }
1120 
1121 /*
1122  * Creates the directories leading to the given path
1123  */
1124 int
1125 mkbasedir(char *path)
1126 {
1127         int rv;
1128         char *cp;
1129         struct stat64 st;
1130 
1131         if (!path || !*path) {
1132                 NDMP_LOG(LOG_DEBUG, "Invalid argument");
1133                 return (-1);
1134         }
1135 
1136         cp = strrchr(path, '/');
1137         if (cp)
1138                 *cp = '\0';
1139         rv = lstat64(path, &st);
1140         if (rv < 0)  /* need new directories */
1141                 rv = make_dirs(path);
1142         if (cp)
1143                 *cp = '/';
1144 
1145         return (rv);
1146 }
1147 
1148 
1149 /*
1150  * read the file off the tape back onto disk
1151  *
1152  * If the function returns a non-zero return code, it means that fatal error
1153  * was encountered and restore should terminate immediately.
1154  */
1155 static int
1156 restore_file(int *fp,
1157     char *real_name,
1158     long size,
1159     longlong_t huge_size,
1160     tlm_acls_t *acls,
1161     boolean_t want_this_file,
1162     tlm_cmd_t *local_commands,
1163     tlm_job_stats_t *job_stats,
1164     long *size_left)
1165 {
1166         struct stat64   attr;
1167         int     ret, rv;
1168 
1169         *size_left = 0;
1170         if (!real_name) {
1171                 if (want_this_file) {
1172                         NDMP_LOG(LOG_DEBUG, "No file name but wanted!");
1173                         want_this_file = FALSE;
1174                 }
1175         } else
1176                 NDMP_LOG(LOG_DEBUG, "new file[%s]", real_name);
1177 
1178         /*
1179          * OK, some FM is creeping in here ...
1180          * int *fp is used to keep the
1181          * backup file channel open through
1182          * the interruption of EOT and
1183          * processing the headers of the
1184          * next tape.  So, if *fp is zero
1185          * then no file is open yet and all
1186          * is normal.  If *fp has a number
1187          * then we are returning after an
1188          * EOT break.
1189          *
1190          * *fp is now also open for HUGE files
1191          * that are put back in sections.
1192          */
1193 
1194         if (*fp == 0 && want_this_file) {
1195 
1196                 ret = mkbasedir(real_name);
 
1210                 } else if (acls->acl_update) {
1211                         if (attr.st_mtime < acls->acl_attr.st_mtime) {
1212                                 /*EMPTY*/
1213                                 /* tape is newer */
1214                         } else {
1215                                 /* disk file is newer */
1216                                 want_this_file = FALSE;
1217                         }
1218                 } else {
1219                         /*
1220                          * no overwrite, no update,
1221                          * do not ever replace old files.
1222                          */
1223                         want_this_file = TRUE;
1224                 }
1225                 if (want_this_file) {
1226 
1227                         *fp = open(real_name, O_CREAT | O_TRUNC | O_WRONLY,
1228                             S_IRUSR | S_IWUSR);
1229                         if (*fp == -1) {
1230                                 NDMP_LOG(LOG_ERR,
1231                                     "Could not open %s for restore: %d",
1232                                     real_name, errno);
1233                                 job_stats->js_errors++;
1234                                 want_this_file = FALSE;
1235                                 /*
1236                                  * In case of non-fatal error we cannot return
1237                                  * here, because the file is still on the tape
1238                                  * and must be skipped over.
1239                                  */
1240                                 if (ERROR_IS_FATAL(errno))
1241                                         return (errno);
1242                         }
1243                 }
1244                 (void) strlcpy(local_commands->tc_file_name, real_name,
1245                     TLM_MAX_PATH_NAME);
1246         }
1247 
1248         /*
1249          * this is the size left in the next segment
1250          */
 
1253         /*
1254          * work
1255          */
1256         rv = 0;
1257         while (size > 0 && local_commands->tc_writer == TLM_RESTORE_RUN) {
1258                 int     actual_size;
1259                 int     error;
1260                 char    *rec;
1261                 int     write_size;
1262 
1263                 /*
1264                  * Use bytes_in_file field to tell reader the amount
1265                  * of data still need to be read for this file.
1266                  */
1267                 job_stats->js_bytes_in_file = size;
1268 
1269                 error = 0;
1270                 rec = get_read_buffer(size, &error, &actual_size,
1271                     local_commands);
1272                 if (actual_size <= 0) {
1273                         NDMP_LOG(LOG_DEBUG,
1274                             "RESTORE WRITER> error %d, actual_size %d",
1275                             error, actual_size);
1276 
1277                         /* no more data for this file for now */
1278                         job_stats->js_bytes_in_file = 0;
1279                         *size_left = size;
1280                         return (0);
1281                 } else if (error) {
1282                         NDMP_LOG(LOG_DEBUG, "Error %d in file [%s]",
1283                             error, local_commands->tc_file_name);
1284                         break;
1285                 }
1286 
1287                 write_size = min(size, actual_size);
1288                 if (want_this_file) {
1289                         ret = write(*fp, rec, write_size);
1290                         if (ret < 0) {
1291                                 NDMP_LOG(LOG_ERR,
1292                                     "Write error %d for file [%s]", errno,
1293                                     local_commands->tc_file_name);
1294                                 job_stats->js_errors++;
1295                                 if (ERROR_IS_FATAL(errno)) {
1296                                         rv = errno;
1297                                         break;
1298                                 }
1299                         } else {
1300                                 NS_ADD(wdisk, ret);
1301                                 NS_INC(wfile);
1302                                 if (ret < write_size) {
1303                                         NDMP_LOG(LOG_ERR,
1304                                             "Partial write for file [%s]",
1305                                             local_commands->tc_file_name);
1306                                 }
1307                         }
1308                 }
1309                 size -= write_size;
1310         }
1311 
1312         /* no more data for this file for now */
1313         job_stats->js_bytes_in_file = 0;
1314 
1315         /*
1316          * teardown
1317          */
1318         if (*fp != 0 && huge_size <= 0) {
1319                 (void) close(*fp);
1320                 *fp = 0;
1321                 if (rv == 0) {
1322                         ret = set_acl(real_name, acls);
1323                         if (ERROR_IS_FATAL(ret))
 
1383  * it to the file
1384  */
1385 static long
1386 restore_xattr_hdr(int *fp,
1387     char *name,
1388     char *fname,
1389     long size,
1390     tlm_acls_t *acls,
1391     tlm_cmd_t *local_commands,
1392     tlm_job_stats_t *job_stats)
1393 {
1394         tlm_tar_hdr_t *tar_hdr;
1395         struct xattr_hdr *xhdr;
1396         struct xattr_buf *xbuf;
1397         int namelen;
1398         char *xattrname;
1399         int actual_size;
1400         int error;
1401 
1402         if (!fname) {
1403                 NDMP_LOG(LOG_DEBUG, "No file name but wanted!");
1404         } else {
1405                 NDMP_LOG(LOG_DEBUG, "new xattr[%s]", fname);
1406         }
1407 
1408         error = 0;
1409         xhdr = (struct xattr_hdr *)get_read_buffer(size, &error,
1410             &actual_size, local_commands);
1411         if (xhdr == NULL || error != 0) {
1412                 NDMP_LOG(LOG_DEBUG,
1413                     "Could not read xattr [%s:%s] for restore. ",
1414                     name, fname);
1415                 job_stats->js_errors++;
1416                 return (0);
1417         }
1418 
1419         /* Check extended attribute header */
1420         if (strcmp(xhdr->h_version, XATTR_ARCH_VERS) != 0) {
1421                 NDMP_LOG(LOG_DEBUG,
1422                     "Unrecognized header format [%s]", xhdr->h_version);
1423                 return (0);
1424         }
1425         xbuf = (struct xattr_buf *)(((char *)xhdr) + sizeof (struct xattr_hdr));
1426 
1427         (void) sscanf(xbuf->h_namesz, "%7d", &namelen);
1428         xattrname = xbuf->h_names + strlen(xbuf->h_names) + 1;
1429 
1430         if (*fp == 0) {
1431                 int fd;
1432 
1433                 fd = attropen(name, xattrname, O_CREAT | O_RDWR, 0755);
1434                 if (fd == -1) {
1435                         NDMP_LOG(LOG_DEBUG,
1436                             "Could not open xattr [%s:%s] for restore err=%d.",
1437                             name, xattrname, errno);
1438                         job_stats->js_errors++;
1439                         return (0);
1440                 }
1441                 (void) strlcpy(local_commands->tc_file_name, xattrname,
1442                     TLM_MAX_PATH_NAME);
1443                 *fp = fd;
1444         }
1445 
1446         /* Get the actual extended attribute file */
1447         tar_hdr = (tlm_tar_hdr_t *)get_read_buffer(sizeof (*tar_hdr),
1448             &error, &actual_size, local_commands);
1449         if (tar_hdr == NULL || error != 0) {
1450                 NDMP_LOG(LOG_DEBUG,
1451                     "Could not read xattr data [%s:%s] for restore. ",
1452                     fname, xattrname);
1453                 job_stats->js_errors++;
1454                 return (0);
1455         }
1456         acls->acl_attr.st_mode = oct_atoi(tar_hdr->th_mode);
1457         acls->acl_attr.st_size = oct_atoi(tar_hdr->th_size);
1458         acls->acl_attr.st_uid = oct_atoi(tar_hdr->th_uid);
1459         acls->acl_attr.st_gid = oct_atoi(tar_hdr->th_gid);
1460         acls->acl_attr.st_mtime = oct_atoi(tar_hdr->th_mtime);
1461 
1462         NDMP_LOG(LOG_DEBUG, "xattr_hdr: %s size %d mode %06o uid %d gid %d",
1463             xattrname, acls->acl_attr.st_size, acls->acl_attr.st_mode,
1464             acls->acl_attr.st_uid, acls->acl_attr.st_gid);
1465 
1466         size = acls->acl_attr.st_size;
1467         while (size > 0 && local_commands->tc_writer == TLM_RESTORE_RUN) {
1468                 char    *rec;
1469                 int     write_size;
1470                 int     sysattr_write = 0;
1471 
1472                 error = 0;
1473                 rec = get_read_buffer(size, &error, &actual_size,
1474                     local_commands);
1475 
1476                 if ((actual_size < size) && sysattr_rw(xattrname)) {
1477                         rec = get_read_one_buf(rec, actual_size, size, &error,
1478                             local_commands);
1479                         if (rec == NULL) {
1480                                 NDMP_LOG(LOG_DEBUG, "Error %d in file [%s]",
1481                                     error, xattrname);
1482                                 return (size);
1483                         }
1484                         actual_size = size;
1485                         sysattr_write = 1;
1486                 }
1487                 if (actual_size <= 0) {
1488                         NDMP_LOG(LOG_DEBUG,
1489                             "RESTORE WRITER> error %d, actual_size %d",
1490                             error, actual_size);
1491 
1492                         return (size);
1493                 } else if (error) {
1494                         NDMP_LOG(LOG_DEBUG, "Error %d in file [%s]",
1495                             error, local_commands->tc_file_name);
1496                         break;
1497                 } else {
1498                         write_size = min(size, actual_size);
1499                         if ((write_size = write(*fp, rec, write_size)) < 0) {
1500                                 if (sysattr_write)
1501                                         free(rec);
1502 
1503                                 break;
1504                         }
1505 
1506                         NS_ADD(wdisk, write_size);
1507                         NS_INC(wfile);
1508                         size -= write_size;
1509                 }
1510                 if (sysattr_write)
1511                         free(rec);
1512         }
1513 
1514         if (*fp != 0) {
 
1517                 *fp = 0;
1518         }
1519         return (0);
1520 }
1521 
1522 /*
1523  * Match the name with the list
1524  */
1525 static int
1526 exact_find(char *name, char **list)
1527 {
1528         boolean_t found;
1529         int i;
1530         char *cp;
1531 
1532         found = FALSE;
1533         for (i = 0; *list != NULL; list++, i++) {
1534                 cp = *list + strspn(*list, "/");
1535                 if (match(cp, name)) {
1536                         found = TRUE;
1537                         NDMP_LOG(LOG_DEBUG, "exact_find> found[%s]", cp);
1538                         break;
1539                 }
1540         }
1541 
1542         return (found);
1543 }
1544 
1545 /*
1546  * On error, return FALSE and prevent restoring(probably) unwanted data.
1547  */
1548 static int
1549 is_parent(char *parent, char *child, int flags)
1550 {
1551         char tmp[TLM_MAX_PATH_NAME];
1552         boolean_t rv;
1553 
1554         if (IS_SET(flags, RSFLG_MATCH_WCARD)) {
1555                 if (!tlm_cat_path(tmp, parent, "*")) {
1556                         NDMP_LOG(LOG_DEBUG,
1557                             "is_parent> path too long [%s]", parent);
1558                         rv = FALSE;
1559                 } else
1560                         rv = (match(tmp, child) != 0) ? TRUE : FALSE;
1561         } else {
1562                 if (!tlm_cat_path(tmp, parent, "/")) {
1563                         NDMP_LOG(LOG_DEBUG,
1564                             "is_parent> path too long [%s]", parent);
1565                         rv = FALSE;
1566                 } else
1567                         rv = (strncmp(tmp, child, strlen(tmp)) == 0) ?
1568                             TRUE : FALSE;
1569         }
1570 
1571         return (rv);
1572 }
1573 
1574 /*
1575  * Used to match the filename inside the list
1576  */
1577 static boolean_t
1578 strexactcmp(char *s, char *t)
1579 {
1580         return ((strcmp(s, t) == 0) ? TRUE : FALSE);
1581 }
1582 
1583 /*
1584  * Check if the file is needed to be restored
1585  */
1586 static boolean_t
1587 is_file_wanted(char *name,
1588     char **sels,
1589     char **exls,
1590     int flags,
1591     int *mchtype,
1592     int *pos)
1593 {
1594         char *p_sel;
1595         char *uc_name, *retry, *namep;
1596         boolean_t found;
1597         int i;
1598         name_match_fp_t *cmp_fp;
1599 
1600         if (name == NULL || sels == NULL || exls == NULL)
1601                 return (FALSE);
1602 
1603         found = FALSE;
1604         if (mchtype != NULL)
1605                 *mchtype = PM_NONE;
1606         if (pos != NULL)
1607                 *pos = 0;
1608 
1609         /*
1610          * For empty selection, restore everything
1611          */
1612         if (*sels == NULL || **sels == '\0') {
1613                 NDMP_LOG(LOG_DEBUG, "is_file_wanted: Restore all");
1614                 return (TRUE);
1615         }
1616 
1617         retry = ndmp_malloc(TLM_MAX_PATH_NAME);
1618         if (retry == NULL)
1619                 return (FALSE);
1620 
1621         if (IS_SET(flags, RSFLG_MATCH_WCARD))
1622                 cmp_fp = match;
1623         else
1624                 cmp_fp = strexactcmp;
1625 
1626         namep = name + strspn(name, "/");
1627 
1628         if (IS_SET(flags, RSFLG_IGNORE_CASE)) {
1629                 uc_name = ndmp_malloc(TLM_MAX_PATH_NAME);
1630                 if (uc_name == NULL) {
1631                         free(retry);
1632                         return (FALSE);
1633                 }
1634                 (void) strlcpy(uc_name, namep, TLM_MAX_PATH_NAME);
1635                 (void) strupr(uc_name);
1636                 namep = uc_name;
1637         }
1638         NDMP_LOG(LOG_DEBUG, "is_file_wanted> flg: 0x%x name: [%s]",
1639             flags, name);
1640 
1641         for (i = 0; *sels != NULL; sels++, i++) {
1642                 p_sel = *sels + strspn(*sels, "/");
1643 
1644                 /*
1645                  * Try exact match.
1646                  */
1647                 if ((*cmp_fp)(p_sel, namep)) {
1648                         NDMP_LOG(LOG_DEBUG, "match1> pos: %d [%s][%s]",
1649                             i, p_sel, name);
1650                         found = TRUE;
1651                         if (mchtype != NULL)
1652                                 *mchtype = PM_EXACT;
1653                         break;
1654                 }
1655                 /*
1656                  * Try "entry/" and the current selection.  The
1657                  * current selection may be something like "<something>/".
1658                  */
1659                 (void) tlm_cat_path(retry, namep, "/");
1660                 if ((*cmp_fp)(p_sel, retry)) {
1661                         NDMP_LOG(LOG_DEBUG, "match2> pos %d [%s][%s]",
1662                             i, p_sel, name);
1663                         found = TRUE;
1664                         if (mchtype != NULL)
1665                                 *mchtype = PM_EXACT;
1666                         break;
1667                 }
1668                 /*
1669                  * If the following check returns true it means that the
1670                  * 'name' is an entry below the 'p_sel' hierarchy.
1671                  */
1672                 if (is_parent(p_sel, namep, flags)) {
1673                         NDMP_LOG(LOG_DEBUG, "parent1> pos %d [%s][%s]",
1674                             i, p_sel, name);
1675                         found = TRUE;
1676                         if (mchtype != NULL)
1677                                 *mchtype = PM_CHILD;
1678                         break;
1679                 }
1680                 /*
1681                  * There is a special case for parent directories of a
1682                  * selection.  If 'p_sel' is something like "*d1", the
1683                  * middle directories of the final entry can't be determined
1684                  * until the final entry matches with 'p_sel'.  At that
1685                  * time the middle directories of the entry have been passed
1686                  * and they can't be restored.
1687                  */
1688                 if (is_parent(namep, p_sel, flags)) {
1689                         NDMP_LOG(LOG_DEBUG, "parent2> pos %d [%s][%s]",
1690                             i, p_sel, name);
1691                         found = TRUE;
1692                         if (mchtype != NULL)
1693                                 *mchtype = PM_PARENT;
1694                         break;
1695                 }
1696         }
1697 
1698         /* Check for exclusions.  */
1699         if (found && exact_find(namep, exls)) {
1700                 if (mchtype != NULL)
1701                         *mchtype = PM_NONE;
1702                 found = FALSE;
1703         }
1704         if (found && pos != NULL)
1705                 *pos = i;
1706 
1707         if (IS_SET(flags, RSFLG_IGNORE_CASE))
1708                 free(uc_name);
1709         free(retry);
 
1711 }
1712 
1713 /*
1714  * Read the specified amount data into the buffer.  Detects EOT or EOF
1715  * during read.
1716  *
1717  * Returns the number of bytes actually read.  On error returns -1.
1718  */
1719 static int
1720 input_mem(int l,
1721     int d,
1722     tlm_cmd_t *lcmds,
1723     char *mem,
1724     int len)
1725 {
1726         int err;
1727         int toread, actual_size, rec_size;
1728         char *rec;
1729 
1730         if (l <= 0 || d <= 0 || !lcmds || !mem) {
1731                 NDMP_LOG(LOG_DEBUG, "Invalid argument");
1732                 return (-1);
1733         }
1734 
1735         toread = len;
1736         while (toread > 0) {
1737                 rec = get_read_buffer(toread, &err, &actual_size, lcmds);
1738                 if (actual_size <= 0) {
1739                         NDMP_LOG(LOG_DEBUG, "err %d act_size %d detected",
1740                             err, actual_size);
1741                         break;
1742                 } else if (err) {
1743                         NDMP_LOG(LOG_DEBUG, "error %d reading data", err);
1744                         return (-1);
1745                 }
1746                 rec_size = min(actual_size, toread);
1747                 (void) memcpy(mem, rec, rec_size);
1748                 mem += rec_size;
1749                 toread -= rec_size;
1750         }
1751 
1752         return (len - toread);
1753 }
1754 
1755 /*
1756  * pick up the name and size of a HUGE file
1757  */
1758 static  int
1759 get_humongus_file_header(int lib,
1760     int drv,
1761     long recsize,
1762     longlong_t *size,
1763     char *name,
1764     tlm_cmd_t *local_commands)
1765 {
1766         char *p_record, *value;
1767         int rv;
1768 
1769         NDMP_LOG(LOG_DEBUG, "HUGE Record found: %d", recsize);
1770 
1771         rv = 0;
1772         if (recsize == 0) {
1773                 /*
1774                  * The humongus_file_header was written in a
1775                  * RECORDSIZE block and the header.size field of this
1776                  * record was 0 before this fix.  For backward compatiblity
1777                  * read only one RECORDSIZE-size block if the header.size
1778                  * field is 0.  Otherwise the header.size field should show
1779                  * the length of the data of this header.
1780                  */
1781                 NDMP_LOG(LOG_DEBUG, "Old HUGE record found");
1782                 recsize = RECORDSIZE;
1783         }
1784 
1785         if (input_mem(lib, drv, local_commands, name, recsize) != recsize) {
1786                 rv = -1;
1787                 *size = 0;
1788                 *name = '\0';
1789                 NDMP_LOG(LOG_DEBUG, "Error reading a HUGE file name");
1790         } else {
1791                 NDMP_LOG(LOG_DEBUG, "HUGE [%s]", name);
1792 
1793                 p_record = name;
1794                 value = parse(&p_record, " ");
1795                 *size = atoll(value);
1796                 /*
1797                  * Note: Since the backed up names are not longer than
1798                  * NAME_MAX and the buffer passed to us is
1799                  * TLM_MAX_PATH_NAME, it should be safe to use strlcpy
1800                  * without check on the buffer size.
1801                  */
1802                 (void) strlcpy(name, p_record, TLM_MAX_PATH_NAME);
1803         }
1804 
1805         NDMP_LOG(LOG_DEBUG, "HUGE Record %lld [%s]", *size, name);
1806 
1807         return (rv);
1808 }
1809 
1810 /*
1811  * pick up the long name from the special tape file
1812  */
1813 static int
1814 get_long_name(int lib,
1815     int drv,
1816     long recsize,
1817     char *name,
1818     long *buf_spot,
1819     tlm_cmd_t *local_commands)
1820 {
1821         int nread;
1822 
1823         NDMP_LOG(LOG_DEBUG, "LONGNAME Record found rs %d bs %d", recsize,
1824             *buf_spot);
1825 
1826         if (*buf_spot < 0)
1827                 *buf_spot = 0;
1828 
1829         nread = input_mem(lib, drv, local_commands, name + *buf_spot,
1830             recsize);
1831         if (nread < 0) {
1832                 nread = recsize; /* return 0 as size left */
1833                 name[*buf_spot] = '\0';
1834                 NDMP_LOG(LOG_ERR, "Error %d reading a long file name %s.",
1835                     nread, name);
1836         } else {
1837                 *buf_spot += nread;
1838                 name[*buf_spot] = '\0';
1839                 NDMP_LOG(LOG_DEBUG, "LONGNAME [%s]", name);
1840         }
1841 
1842         return (recsize - nread);
1843 }
1844 
1845 /*
1846  * create a new directory
1847  */
1848 static int
1849 create_directory(char *dir, tlm_job_stats_t *job_stats)
1850 {
1851         struct stat64 attr;
1852         char    *p;
1853         char    temp;
1854         int     erc;
1855 
1856         /*
1857          * Make sure all directories in this path exist, create them if
1858          * needed.
1859          */
1860         NDMP_LOG(LOG_DEBUG, "new dir[%s]", dir);
1861 
1862         erc = 0;
1863         p = &dir[1];
1864         do {
1865                 temp = *p;
1866                 if (temp == '/' || temp == 0) {
1867                         *p = 0;
1868                         if (stat64(dir, &attr) < 0) {
1869                                 if (mkdir(dir, 0777) != 0 && errno != EEXIST) {
1870                                         erc = errno;
1871                                         job_stats->js_errors++;
1872                                         NDMP_LOG(LOG_DEBUG,
1873                                             "Could not create directory %s: %d",
1874                                             dir, errno);
1875                                         break;
1876                                 }
1877                         }
1878                         *p = temp;
1879                 }
1880                 p++;
1881         } while (temp != 0);
1882 
1883         return (erc);
1884 }
1885 
1886 /*
1887  * create a new hardlink
1888  */
1889 static int
1890 create_hard_link(char *name_old, char *name_new,
1891     tlm_acls_t *acls, tlm_job_stats_t *job_stats)
1892 {
1893         int erc;
1894 
1895         erc = mkbasedir(name_new);
1896         if (erc != 0)
1897                 return (erc);
1898 
1899         if (link(name_old, name_new) != 0)
1900                 erc = errno;
1901 
1902         if (erc) {
1903                 /* Nothing to do if the destination already exists */
1904                 if (erc == EEXIST)
1905                         return (0);
1906                 job_stats->js_errors++;
1907                 NDMP_LOG(LOG_DEBUG, "error %d (errno %d) hardlink [%s] to [%s]",
1908                     erc, errno, name_new, name_old);
1909                 return (erc);
1910         }
1911         return (set_acl(name_new, acls));
1912 }
1913 
1914 /*
1915  * create a new symlink
1916  */
1917 /*ARGSUSED*/
1918 static int
1919 create_sym_link(char *dst, char *target, tlm_acls_t *acls,
1920     tlm_job_stats_t *job_stats)
1921 {
1922         int erc;
1923         struct stat64 *st;
1924 
1925         erc = mkbasedir(dst);
1926         if (erc != 0)
1927                 return (erc);
1928 
1929         st = &acls->acl_attr;
1930         if (symlink(target, dst) != 0) {
1931                 erc = errno;
1932                 job_stats->js_errors++;
1933                 NDMP_LOG(LOG_DEBUG, "error %d softlink [%s] to [%s]",
1934                     errno, dst, target);
1935         } else {
1936                 st->st_mode |= S_IFLNK;
1937                 erc = set_acl(dst, acls);
1938         }
1939 
1940         return (erc);
1941 }
1942 
1943 /*
1944  * create a new FIFO, char/block device special files
1945  */
1946 static int
1947 create_special(char flag, char *name, tlm_acls_t *acls, int major, int minor,
1948     tlm_job_stats_t *job_stats)
1949 {
1950         dev_t dev;
1951         mode_t mode;
1952 
1953         switch (flag) {
1954         case LF_CHR:
1955                 mode = S_IFCHR;
1956                 dev = makedev(major, minor);
1957                 break;
1958         case LF_BLK:
1959                 mode = S_IFBLK;
1960                 dev = makedev(major, minor);
1961                 break;
1962         case LF_FIFO:
1963                 mode = S_IFIFO;
1964                 dev = 0;
1965                 break;
1966         default:
1967                 NDMP_LOG(LOG_ERR, "unsupported flag %d", flag);
1968                 return (-1);
1969         }
1970 
1971         /* Remove the old entry first */
1972         if (rmdir(name) < 0) {
1973                 if (errno == ENOTDIR)
1974                         (void) unlink(name);
1975         }
1976         if (mknod(name, 0777 | mode, dev) != 0) {
1977                 job_stats->js_errors++;
1978                 NDMP_LOG(LOG_DEBUG, "error %d mknod [%s] major"
1979                     " %d minor %d", errno, name, major, minor);
1980                 return (errno);
1981         }
1982         return (set_acl(name, acls));
1983 }
1984 
1985 /*
1986  * read in the ACLs for the next file
1987  */
1988 static long
1989 load_acl_info(int lib,
1990     int drv,
1991     long file_size,
1992     tlm_acls_t *acls,
1993     long *acl_spot,
1994     tlm_cmd_t *local_commands)
1995 {
1996         char *bp;
1997         int nread;
1998 
1999         /*
2000          * If the ACL is spanned on tapes, then the acl_spot should NOT be
2001          * 0 on next calls to this function to read the rest of the ACL
2002          * on next tapes.
2003          */
2004         if (*acl_spot == 0) {
2005                 (void) memset(acls, 0, sizeof (tlm_acls_t));
2006         }
2007 
2008         bp = ((char *)&acls->acl_info) + *acl_spot;
2009         nread = input_mem(lib, drv, local_commands, (void *)bp, file_size);
2010         if (nread < 0) {
2011                 *acl_spot = 0;
2012                 (void) memset(acls, 0, sizeof (tlm_acls_t));
2013                 NDMP_LOG(LOG_DEBUG, "Error reading ACL data");
2014                 return (0);
2015         }
2016         *acl_spot += nread;
2017         acls->acl_non_trivial = TRUE;
2018 
2019         return (file_size - nread);
2020 }
2021 
2022 static int
2023 ndmp_set_eprivs_least(void)
2024 {
2025         priv_set_t *priv_set;
2026 
2027         if ((priv_set = priv_allocset()) == NULL) {
2028                 NDMP_LOG(LOG_ERR, "Out of memory.");
2029                 return (-1);
2030         }
2031 
2032         priv_basicset(priv_set);
2033 
2034         (void) priv_addset(priv_set, PRIV_PROC_AUDIT);
2035         (void) priv_addset(priv_set, PRIV_PROC_SETID);
2036         (void) priv_addset(priv_set, PRIV_PROC_OWNER);
2037         (void) priv_addset(priv_set, PRIV_FILE_CHOWN);
2038         (void) priv_addset(priv_set, PRIV_FILE_CHOWN_SELF);
2039         (void) priv_addset(priv_set, PRIV_FILE_DAC_READ);
2040         (void) priv_addset(priv_set, PRIV_FILE_DAC_SEARCH);
2041         (void) priv_addset(priv_set, PRIV_FILE_DAC_WRITE);
2042         (void) priv_addset(priv_set, PRIV_FILE_OWNER);
2043         (void) priv_addset(priv_set, PRIV_FILE_SETID);
2044         (void) priv_addset(priv_set, PRIV_SYS_LINKDIR);
2045         (void) priv_addset(priv_set, PRIV_SYS_DEVICES);
2046         (void) priv_addset(priv_set, PRIV_SYS_MOUNT);
2047         (void) priv_addset(priv_set, PRIV_SYS_CONFIG);
2048 
2049         if (setppriv(PRIV_SET, PRIV_EFFECTIVE, priv_set) == -1) {
2050                 NDMP_LOG(LOG_ERR, "Additional privileges required.");
2051                 priv_freeset(priv_set);
2052                 return (-1);
2053         }
2054         priv_freeset(priv_set);
2055         return (0);
2056 }
2057 
2058 static int
2059 ndmp_set_eprivs_all(void)
2060 {
2061         priv_set_t *priv_set;
2062 
2063         if ((priv_set = priv_allocset()) == NULL) {
2064                 NDMP_LOG(LOG_ERR, "Out of memory.");
2065                 return (-1);
2066         }
2067 
2068         priv_fillset(priv_set);
2069 
2070         if (setppriv(PRIV_SET, PRIV_EFFECTIVE, priv_set) != 0) {
2071                 NDMP_LOG(LOG_ERR, "Additional privileges required.");
2072                 return (-1);
2073         }
2074         priv_freeset(priv_set);
2075         return (0);
2076 }
2077 
2078 /*
2079  * Set the standard attributes of the file
2080  */
2081 static int
2082 set_attr(char *name, tlm_acls_t *acls)
2083 {
2084         struct utimbuf tbuf;
2085         boolean_t priv_all = FALSE;
2086         struct stat64 *st;
2087         uid_t uid;
2088         gid_t gid;
2089         struct passwd *pwd;
2090         struct group *grp;
2091         int erc = 0;
2092 
2093 
2094         if (!name || !acls)
2095                 return (0);
2096 
2097         st = &acls->acl_attr;
2098         NDMP_LOG(LOG_DEBUG, "set_attr: %s uid %d gid %d uname %s gname %s "
2099             "mode %o", name, st->st_uid, st->st_gid, acls->uname, acls->gname,
2100             st->st_mode);
2101 
2102         uid = st->st_uid;
2103         if ((pwd = getpwnam(acls->uname)) != NULL) {
2104                 NDMP_LOG(LOG_DEBUG, "set_attr: new uid %d old %d",
2105                     pwd->pw_uid, uid);
2106                 uid = pwd->pw_uid;
2107         }
2108 
2109         gid = st->st_gid;
2110         if ((grp = getgrnam(acls->gname)) != NULL) {
2111                 NDMP_LOG(LOG_DEBUG, "set_attr: new gid %d old %d",
2112                     grp->gr_gid, gid);
2113                 gid = grp->gr_gid;
2114         }
2115 
2116         erc = lchown(name, uid, gid);
2117         if (erc != 0) {
2118                 erc = errno;
2119                 NDMP_LOG(LOG_ERR,
2120                     "Could not set uid or/and gid for file %s.", name);
2121         }
2122 
2123         if ((st->st_mode & (S_ISUID | S_ISGID)) != 0) {
2124                 /*
2125                  * Change effective privileges to 'all' which is required to
2126                  * change setuid bit for 'root' owned files. If fails, just
2127                  * send error to log file and proceed.
2128                  */
2129                 if (ndmp_set_eprivs_all()) {
2130                         NDMP_LOG(LOG_ERR,
2131                             "Could not set effective privileges to 'all'.");
2132                 } else {
2133                         priv_all = TRUE;
2134                 }
2135         }
2136 
2137         if (!S_ISLNK(st->st_mode)) {
2138                 erc = chmod(name, st->st_mode);
2139                 if (erc != 0) {
2140                         erc = errno;
2141                         NDMP_LOG(LOG_ERR, "Could not set correct file"
2142                             " permission for file %s: %d", name, errno);
2143                 }
2144 
2145                 tbuf.modtime = st->st_mtime;
2146                 tbuf.actime = st->st_atime;
2147                 (void) utime(name, &tbuf);
2148         }
2149 
2150         if (priv_all == TRUE) {
2151                 /*
2152                  * Give up the 'all' privileges for effective sets and go back
2153                  * to least required privileges. If fails, just send error to
2154                  * log file and proceed.
2155                  */
2156                 if (ndmp_set_eprivs_least())
2157                         NDMP_LOG(LOG_ERR,
2158                             "Could not set least required privileges.");
2159         }
2160 
2161         return (erc);
2162 }
2163 
2164 /*
2165  * Set the ACL info for the file
2166  */
2167 static int
2168 set_acl(char *name, tlm_acls_t *acls)
2169 {
2170         int erc;
2171         acl_t *aclp = NULL;
2172 
2173         if (name)
2174                 NDMP_LOG(LOG_DEBUG, "set_acl: %s", name);
2175         if (acls == NULL)
2176                 return (0);
2177 
2178         /* Need a place to save real modification time */
2179 
2180         erc = set_attr(name, acls);
2181         if (ERROR_IS_FATAL(erc))
2182                 return (erc);
2183 
2184         if (!acls->acl_non_trivial) {
2185                 (void) memset(acls, 0, sizeof (tlm_acls_t));
2186                 NDMP_LOG(LOG_DEBUG, "set_acl: skipping trivial");
2187                 return (erc);
2188         }
2189 
2190         erc = acl_fromtext(acls->acl_info.attr_info, &aclp);
2191         if (erc != 0) {
2192                 NDMP_LOG(LOG_DEBUG,
2193                     "TAPE RESTORE> acl_fromtext errno %d", erc);
2194         }
2195         if (aclp) {
2196                 erc = acl_set(name, aclp);
2197                 if (erc < 0) {
2198                         erc = errno;
2199                         NDMP_LOG(LOG_DEBUG,
2200                             "TAPE RESTORE> acl_set errno %d", errno);
2201                 }
2202                 acl_free(aclp);
2203         }
2204         (void) memset(acls, 0, sizeof (tlm_acls_t));
2205         return (erc);
2206 }
2207 
2208 /*
2209  * a wrapper to tlm_get_read_buffer so that
2210  * we can cleanly detect ABORT commands
2211  * without involving the TLM library with
2212  * our problems.
2213  */
2214 static char *
2215 get_read_buffer(int want,
2216     int *error,
2217     int *actual_size,
2218     tlm_cmd_t *local_commands)
2219 {
 
2241 wildcard_enabled(void)
2242 {
2243         char *cp;
2244 
2245         cp = ndmpd_get_prop_default(NDMP_RESTORE_WILDCARD_ENABLE, "n");
2246         return ((toupper(*cp) == 'Y') ? TRUE : FALSE);
2247 }
2248 
2249 
2250 /*
2251  * Concatenate two names
2252  */
2253 /*ARGSUSED*/
2254 static char *
2255 catnames(struct rs_name_maker *rnp, char *buf, int pos, char *path)
2256 {
2257         char *rv;
2258 
2259         rv = NULL;
2260         if (!buf) {
2261                 NDMP_LOG(LOG_DEBUG, "buf is NULL");
2262         } else if (!path) {
2263                 NDMP_LOG(LOG_DEBUG, "path is NULL");
2264         } else if (!rnp->rn_nlp) {
2265                 NDMP_LOG(LOG_DEBUG, "rn_nlp is NULL [%s]", path);
2266         } else if (!tlm_cat_path(buf, rnp->rn_nlp, path)) {
2267                 NDMP_LOG(LOG_DEBUG, "Path too long [%s][%s]",
2268                     rnp->rn_nlp, path);
2269         } else
2270                 rv = buf;
2271 
2272         return (rv);
2273 }
2274 
2275 
2276 /*
2277  * Create a new name path for restore
2278  */
2279 static char *
2280 rs_new_name(struct rs_name_maker *rnp, char *buf, int pos, char *path)
2281 {
2282         if (!rnp || !rnp->rn_fp)
2283                 return (NULL);
2284 
2285         return (*rnp->rn_fp)(rnp, buf, pos, path);
2286 }
2287 
2288 /*
2289  * Clear the extra "/" in the tar header if exists
2290  */
2291 static void
2292 rs_create_new_bkpath(char *bk_path, char *path, char *pbuf)
2293 {
2294         char *p, *slashp;
2295 
2296         if ((p = strstr(path, bk_path)) == NULL) {
2297                 (void) strlcpy(pbuf, path, TLM_MAX_PATH_NAME);
2298                 return;
2299         }
2300         if (*(p += strlen(bk_path)) == '/')
2301                 p++;
2302 
2303         slashp = bk_path + strlen(bk_path) - 1;
2304         if (*slashp == '/')
2305                 (void) snprintf(pbuf, TLM_MAX_PATH_NAME, "%s%s", bk_path, p);
2306         else
2307                 (void) snprintf(pbuf, TLM_MAX_PATH_NAME, "%s/%s", bk_path, p);
2308 
2309         NDMP_LOG(LOG_DEBUG, "old path [%s] new path [%s]", path, pbuf);
2310 }
2311 
2312 
2313 /*
2314  * Iterate over ZFS metadata stored in the backup stream and use the callback
2315  * to restore it.
2316  */
2317 int
2318 ndmp_iter_zfs(ndmp_context_t *nctx, int (*np_restore_property)(nvlist_t *,
2319     void *), void *ptr)
2320 {
2321         tlm_commands_t *cmds;
2322         ndmp_metadata_header_t *mhp;
2323         ndmp_metadata_header_ext_t *mhpx;
2324         ndmp_metadata_property_t *mpp;
2325         ndmp_metadata_property_ext_t *mppx;
2326         tlm_cmd_t *lcmd;
2327         int actual_size;
2328         nvlist_t *nvl;
2329         nvlist_t *valp;
 
2391                         (void) memcpy(pp, tp, lsize);
2392                         sz += lsize;
2393                         pp += lsize;
2394                 }
2395                 if (sz > size) {
2396                         tlm_unget_read_buffer(lcmd->tc_buffers, sz - size);
2397                 }
2398 
2399                 /* LINTED improper alignment */
2400                 mhp = (ndmp_metadata_header_t *)mhbuf;
2401 
2402                 nvl = NULL;
2403                 if (strncmp(mhp->nh_magic, ZFS_META_MAGIC_EXT,
2404                     sizeof (mhp->nh_magic)) == 0) {
2405                         /* New metadata format */
2406                         /* LINTED improper alignment */
2407                         mhpx = (ndmp_metadata_header_ext_t *)mhbuf;
2408 
2409                         if (mhpx->nh_major > META_HDR_MAJOR_VERSION) {
2410                                 /* Major header mismatch */
2411                                 NDMP_LOG(LOG_ERR, "metadata header mismatch",
2412                                     "M%d != M%d", mhpx->nh_major,
2413                                     META_HDR_MAJOR_VERSION);
2414                                 free(mhbuf);
2415                                 return (-1);
2416                         }
2417                         if (mhpx->nh_major == META_HDR_MAJOR_VERSION &&
2418                             mhpx->nh_minor > META_HDR_MINOR_VERSION) {
2419                                 /* Minor header mismatch */
2420                                 NDMP_LOG(LOG_ERR, "Warning:"
2421                                     "metadata header mismatch m%d != m%d",
2422                                     mhpx->nh_minor,
2423                                     META_HDR_MINOR_VERSION);
2424                                 continue;
2425                         }
2426 
2427                         nctx->nc_plversion = mhpx->nh_plversion;
2428                         (void) strlcpy(plname, mhpx->nh_plname,
2429                             sizeof (plname));
2430 
2431                         if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
2432                                 goto nvlist_err;
2433 
2434                         mppx = &mhpx->nh_property[0];
2435                         for (i = 0; i < mhpx->nh_count && mppx; i++, mppx++) {
2436                                 if (!*mppx->mp_name)
2437                                         continue;
2438                                 valp = NULL;
2439                                 if (nvlist_alloc(&valp,
2440                                     NV_UNIQUE_NAME, 0) != 0 ||
 
 | 
   1 /*
   2  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
   3  */
   4 
   5 /*
   6  * BSD 3 Clause License
   7  *
   8  * Copyright (c) 2007, The Storage Networking Industry Association.
   9  *
  10  * Redistribution and use in source and binary forms, with or without
  11  * modification, are permitted provided that the following conditions
  12  * are met:
  13  *      - Redistributions of source code must retain the above copyright
  14  *        notice, this list of conditions and the following disclaimer.
  15  *
  16  *      - Redistributions in binary form must reproduce the above copyright
  17  *        notice, this list of conditions and the following disclaimer in
  18  *        the documentation and/or other materials provided with the
  19  *        distribution.
  20  *
  21  *      - Neither the name of The Storage Networking Industry Association (SNIA)
  22  *        nor the names of its contributors may be used to endorse or promote
  23  *        products derived from this software without specific prior written
  24  *        permission.
  25  *
  26  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  27  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  29  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  30  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  36  * POSSIBILITY OF SUCH DAMAGE.
  37  */
  38 /* Copyright 2017 Nexenta Systems, Inc. All rights reserved. */
  39 
  40 #include <syslog.h>
  41 #include <stdlib.h>
  42 #include <ctype.h>
  43 #include <stdio.h>
  44 #include <limits.h>
  45 #include <string.h>
  46 #include <time.h>
  47 #include <sys/types.h>
  48 #include <sys/acl.h>
  49 #include <sys/mkdev.h>
  50 #include <utime.h>
  51 #include <unistd.h>
  52 #include <pthread.h>
  53 #include <archives.h>
  54 #include <priv.h>
  55 #include <tlm.h>
  56 #include <libzfs.h>
  57 #include <pwd.h>
  58 #include <grp.h>
  59 #include <ndmpd_prop.h>
  60 #include "tlm_proto.h"
 
 
 213         return (sp->se_name);
 214 }
 215 
 216 /*
 217  * NBU and EBS may not send us the correct file list containing hardlinks
 218  * during a DAR restore, e.g. they appear always send the first name
 219  * associated with an inode, even if other link names were
 220  * selected for the restore.  As a workaround, we use the file name entry
 221  * in sels[] (ignore the name in the tar header) as restore target.
 222  */
 223 static char *
 224 rs_darhl_new_name(struct rs_name_maker *rnp, char *name, char **sels, int *pos,
 225     char *longname)
 226 {
 227         int x;
 228 
 229         for (x = 0; sels[x] != NULL; x++) {
 230                 if (strcmp(sels[x], " ")) {
 231                         *pos = x;
 232                         (void) strlcpy(longname, sels[x], TLM_MAX_PATH_NAME);
 233                         syslog(LOG_DEBUG,
 234                             "to replace hardlink name [%s], pos [%d]",
 235                             longname, *pos);
 236 
 237                         return (rs_new_name(rnp, name, *pos, longname));
 238                 }
 239         }
 240 
 241         return (NULL);
 242 }
 243 
 244 
 245 /*
 246  * Main dir restore function for tar
 247  *
 248  * If this function returns non-zero return value it means that fatal error
 249  * was encountered.
 250  */
 251 int
 252 tar_getdir(tlm_commands_t *commands,
 253     tlm_cmd_t *local_commands,
 254     tlm_job_stats_t *job_stats,
 255     struct rs_name_maker *rnp,
 256     int lib,
 257     int drv,
 258     char **sels, /* what to get off the tape */
 259     char **exls, /* what to leave behind */
 260     int flags,
 261     int DAR,
 262     char *bk_path,
 263     struct hardlink_q *hardlink_q)
 264 {
 265         int     fp = 0;         /* file being restored ... */
 266                                 /*  ...need to preserve across volume changes */
 267         tlm_acls_t *acls;       /* file access info */
 268         char    *longname;
 269         boolean_t is_long_name = FALSE;
 270         char    *longlink;
 271         char    *hugename;
 272         longlong_t huge_size = 0;       /* size of a HUGE file */
 273         long    acl_spot;               /* any ACL info on the next volume */
 274         long    file_size = 0;          /* size of file to restore */
 275         long    size_left = 0;          /* need this after volume change */
 276         int     last_action = 0;        /* what we are doing at EOT */
 277         boolean_t multi_volume = FALSE; /* is this a multi-volume switch ? */
 278         int     chk_rv;                 /* scratch area */
 279 
 280         int     mchtype, pos;
 281                                         /*
 282                                          * if an exact match is found for
 283                                          * restore and its position in the
 284                                          * selections list
 285                                          */
 286         int     nzerohdr;               /* the number of empty tar headers */
 287         int     rv;
 288         long nm_end, lnk_end;
 289         char    *name, *nmp;
 290         cstack_t *stp;
 291         char    *bkpath;
 292         char    *parentlnk;
 293         int dir_dar = 0;
 294 
 
 322                 free(longname);
 323                 free(longlink);
 324                 free(hugename);
 325                 free(parentlnk);
 326                 free(name);
 327                 free(acls);
 328                 free(thname_buf);
 329                 return (-TLM_NO_SCRATCH_SPACE);
 330         }
 331 
 332         acl_spot = 0;
 333         *hugename = '\0';
 334         *parentlnk = '\0';
 335         nm_end = 0;
 336         *longname = '\0';
 337         lnk_end = 0;
 338         *longlink = '\0';
 339         (void) memset(acls, 0, sizeof (tlm_acls_t));
 340         if (IS_SET(flags, RSFLG_OVR_ALWAYS)) {
 341                 acls->acl_overwrite = TRUE;
 342                 syslog(LOG_DEBUG, "RSFLG_OVR_ALWAYS");
 343         } else if (IS_SET(flags, RSFLG_OVR_UPDATE)) {
 344                 acls->acl_update = TRUE;
 345                 syslog(LOG_DEBUG, "RSFLG_OVR_UPDATE");
 346         }
 347 
 348         /*
 349          * work
 350          */
 351         rv = 0;
 352         nzerohdr = 0;
 353         while (commands->tcs_writer != TLM_ABORT &&
 354             local_commands->tc_writer != TLM_STOP && rv == 0) {
 355                 tlm_tar_hdr_t fake_tar_hdr;
 356                 char    *file_name;
 357                 char    *link_name;
 358                 int     erc;
 359                 int     actual_size;
 360                 boolean_t want_this_file;
 361                 int     want = sizeof (tlm_tar_hdr_t);
 362                 tlm_tar_hdr_t *tar_hdr;
 363 
 364                 /* The inode of an LF_LINK type. */
 365                 unsigned long hardlink_inode = 0;
 
 368                  * Indicate whether a file with the same inode has been
 369                  * restored.
 370                  */
 371                 int hardlink_done = 0;
 372 
 373                 /* The path of the restored hardlink file */
 374                 char *hardlink_target = NULL;
 375                 int is_hardlink = 0;
 376 
 377                 /*
 378                  * Whether a temporary file should be created for restoring
 379                  * hardlink.
 380                  */
 381                 int hardlink_tmp_file = 0;
 382                 char *hardlink_tmp_name = ".tmphlrsnondar";
 383 
 384                 /* used to make up hardlink_tmp_name */
 385                 static int hardlink_tmp_idx = 0;
 386 
 387                 if (multi_volume) {
 388                         syslog(LOG_DEBUG, "multi_volume %c %d",
 389                             last_action, size_left);
 390 
 391                         /*
 392                          * the previous volume is out of data
 393                          * and is back in the rack, a new tape
 394                          * is loaded and ready to read.
 395                          *
 396                          * We need to pick up where we left off.
 397                          */
 398                         (void) memset(&fake_tar_hdr, 0, sizeof (fake_tar_hdr));
 399                         file_size = size_left;
 400                         tar_hdr = &fake_tar_hdr;
 401                         tar_hdr->th_linkflag = last_action;
 402 
 403                         multi_volume = FALSE;
 404                         last_action = 0;
 405                 } else {
 406                         tar_hdr = (tlm_tar_hdr_t *)get_read_buffer(want,
 407                             &erc, &actual_size, local_commands);
 408 
 
 419                          */
 420                         if (actual_size < want) {
 421                                 /*
 422                                  * EOF hits here
 423                                  *
 424                                  * wait for another buffer to come along
 425                                  * or until the Reader thread tells us
 426                                  * that no more tapes will be loaded ...
 427                                  * time to stop.
 428                                  */
 429                                 continue;
 430                         }
 431 
 432                         /*
 433                          * check for "we are lost"
 434                          */
 435                         chk_rv = tlm_vfy_tar_checksum(tar_hdr);
 436                         if (chk_rv == 0) {
 437                                 /* one of the end of tar file marks */
 438                                 if (++nzerohdr >= 2) {
 439                                         syslog(LOG_DEBUG,
 440                                             "nzerohdr %d, breaking",
 441                                             nzerohdr);
 442                                         /* end of tar file */
 443                                         break;
 444                                 }
 445                                 syslog(LOG_DEBUG, "nzerohdr %d, continuing",
 446                                     nzerohdr);
 447                                 continue;
 448                         } else if (chk_rv < 0) {
 449                                 nzerohdr = 0;
 450                                 /* skip this record */
 451                                 continue;
 452                         }
 453                         nzerohdr = 0;
 454 
 455                         /*
 456                          * When files are spanned to the next tape, the
 457                          * information of the acls must not be over-written
 458                          * by the information of the LF_MULTIVOL and LF_VOLHDR
 459                          * header, whose information is irrelevant to the file.
 460                          * The information of the original header must be
 461                          * kept in the 'acl'.
 462                          */
 463                         if (tar_hdr->th_linkflag != LF_MULTIVOL &&
 464                             tar_hdr->th_linkflag != LF_VOLHDR) {
 465                                 if (tar_hdr->th_linkflag != LF_HUMONGUS) {
 
 469                                             oct_atoi(tar_hdr->th_size);
 470                                         acls->acl_attr.st_uid =
 471                                             oct_atoi(tar_hdr->th_uid);
 472                                         acls->acl_attr.st_gid =
 473                                             oct_atoi(tar_hdr->th_gid);
 474                                         acls->acl_attr.st_mtime =
 475                                             oct_atoi(tar_hdr->th_mtime);
 476                                         (void) strlcpy(acls->uname,
 477                                             tar_hdr->th_uname,
 478                                             sizeof (acls->uname));
 479                                         (void) strlcpy(acls->gname,
 480                                             tar_hdr->th_gname,
 481                                             sizeof (acls->gname));
 482                                 }
 483                                 file_size = oct_atoi(tar_hdr->th_size);
 484                                 acl_spot = 0;
 485                                 last_action = tar_hdr->th_linkflag;
 486                         }
 487                 }
 488 
 489                 /*
 490                  * If the restore is running using DAR we should check for
 491                  * extended attribute entries
 492                  */
 493                 if (dar_recovered &&
 494                     tar_hdr->th_linkflag != LF_XATTR)
 495                         break;
 496 
 497                 rs_create_new_bkpath(bk_path, tar_hdr->th_name, thname_buf);
 498 
 499                 switch (tar_hdr->th_linkflag) {
 500                 case LF_MULTIVOL:
 501                         multi_volume = TRUE;
 502                         break;
 503                 case LF_LINK:
 504                         is_hardlink = 1;
 505                         hardlink_inode =
 506                             oct_atoi(tar_hdr->th_shared.th_hlink_ino);
 507 
 508                         /*
 509                          * Check if we have restored a link with the same inode
 510                          * If the inode is 0, we have to restore it as a
 511                          * regular file.
 512                          */
 513                         if (hardlink_inode) {
 514                                 hardlink_done = !hardlink_q_get(hardlink_q,
 515                                     hardlink_inode, 0, &hardlink_target);
 516                         }
 517 
 518                         if (hardlink_done) {
 519                                 syslog(LOG_DEBUG,
 520                                     "found hardlink, inode = %u, target = [%s]",
 521                                     hardlink_inode,
 522                                     hardlink_target? hardlink_target : "--");
 523 
 524                                 /* create a hardlink to hardlink_target */
 525                                 file_name = (*longname == 0) ?
 526                                     thname_buf : longname;
 527 
 528                                 if (!is_file_wanted(file_name, sels, exls,
 529                                     flags, &mchtype, &pos)) {
 530                                         nmp = NULL;
 531                                         /*
 532                                          * This means that DMA did not send us
 533                                          * the correct fh_info for the file
 534                                          * in restore list.  We use the file
 535                                          * name entry in sels[] (ignore the
 536                                          * name in the tar header) as restore
 537                                          * target.
 538                                          */
 539                                         if (DAR) {
 540                                                 nmp = rs_darhl_new_name(rnp,
 541                                                     name, sels, &pos,
 542                                                     file_name);
 543                                         }
 544                                 } else {
 545                                         nmp = rs_new_name(rnp, name, pos,
 546                                             file_name);
 547                                         if (!nmp) {
 548                                                 syslog(LOG_ERR,
 549                                                     "can't make name for %s",
 550                                                     longname);
 551                                         }
 552                                 }
 553 
 554                                 if (nmp) {
 555                                         if (hardlink_target) {
 556                                                 erc = create_hard_link(
 557                                                     hardlink_target, nmp,
 558                                                     acls, job_stats);
 559                                                 if (ERROR_IS_FATAL(erc)) {
 560                                                         rv = erc;
 561                                                         continue;
 562                                                 }
 563                                                 if (erc == 0) {
 564                                                         (void)
 565                                                             tlm_entry_restored(
 566                                                             job_stats,
 567                                                             file_name, pos);
 568                                                         syslog(LOG_DEBUG,
 569                                                             "restored %s -> %s",
 570                                                             nmp,
 571                                                             hardlink_target);
 572                                                 }
 573                                         } else {
 574                                                 syslog(LOG_DEBUG,
 575                                                     "no target for hardlink %s",
 576                                                     nmp);
 577                                         }
 578 
 579                                         name[0] = 0;
 580                                         is_long_name = FALSE;
 581                                 }
 582 
 583                                 nm_end = 0;
 584                                 longname[0] = 0;
 585                                 lnk_end = 0;
 586                                 longlink[0] = 0;
 587 
 588                                 break;
 589                         }
 590                         /* otherwise fall through, restore like a normal file */
 591                         /*FALLTHROUGH*/
 592                 case LF_OLDNORMAL:
 593                         /*
 594                          * check for TAR's end-of-tape method
 
 669                          * restore were interrupted, e.g. by a system reboot,
 670                          * they would have to be cleaned up manually in order
 671                          * for the disk space to be freed.
 672                          *
 673                          * If tmplink_dir is NULL, no temperorary files are
 674                          * created during a restore.  This may result in some
 675                          * hardlinks not being restored during a partial
 676                          * restore.
 677                          */
 678                         if (is_hardlink && !DAR && !want_this_file && !nmp) {
 679                                 if (tmplink_dir) {
 680                                         (void) snprintf(name, TLM_MAX_PATH_NAME,
 681                                             "%s/%s_%d", tmplink_dir,
 682                                             hardlink_tmp_name,
 683                                             hardlink_tmp_idx);
 684                                         nmp = name;
 685 
 686                                         hardlink_tmp_idx++;
 687                                         hardlink_tmp_file = 1;
 688                                         want_this_file = TRUE;
 689                                         syslog(LOG_DEBUG,
 690                                             "To restore temp hardlink file %s.",
 691                                             nmp);
 692                                 } else {
 693                                         syslog(LOG_DEBUG,
 694                                             "No tmplink_dir specified.");
 695                                 }
 696                         }
 697 
 698                         rv = restore_file(&fp, nmp, file_size,
 699                             huge_size, acls, want_this_file, local_commands,
 700                             job_stats, &size_left);
 701                         if (rv != 0)
 702                                 continue;
 703 
 704                         /*
 705                          * In the case of non-DAR, we have to record the first
 706                          * link for an inode that has multiple links. That's
 707                          * the only link with data records actually backed up.
 708                          * In this way, when we run into the other links, they
 709                          * will be treated as links, and we won't go to look
 710                          * for the data records to restore.  This is not a
 711                          * problem for DAR, where DMA tells the tape where
 712                          * to locate the data records.
 713                          */
 714                         if (is_hardlink && !DAR) {
 715                                 if (hardlink_q_add(hardlink_q, hardlink_inode,
 716                                     0, nmp, hardlink_tmp_file))
 717                                         syslog(LOG_ERR,
 718                                             "failed to add (%u, %s) to HL q",
 719                                             hardlink_inode, nmp);
 720                         }
 721 
 722                         /* remove / reverse the temporary stuff */
 723                         if (hardlink_tmp_file) {
 724                                 nmp = NULL;
 725                                 want_this_file = FALSE;
 726                                 hardlink_tmp_file = 0;
 727                         }
 728 
 729                         /*
 730                          * Check if it is time to set the attribute
 731                          * of the restored directory
 732                          */
 733                         while (nmp && ((bkpath = dtree_peek(stp)) != NULL)) {
 734                                 int erc;
 735 
 736                                 if (strstr(nmp, bkpath))
 737                                         break;
 738 
 739                                 erc = dtree_pop(stp);
 740                                 if (ERROR_IS_FATAL(erc)) {
 741                                         rv = erc;
 742                                         break;
 743                                 }
 744                         }
 745                         if (rv != 0)
 746                                 continue;
 747 
 748                         if (want_this_file) {
 749                                 job_stats->js_bytes_total += file_size;
 750                                 job_stats->js_files_total++;
 751                         }
 752 
 753                         huge_size -= file_size;
 754                         if (huge_size < 0) {
 755                                 huge_size = 0;
 756                         }
 757                         if (size_left == 0 && huge_size == 0) {
 758                                 if (PM_EXACT_OR_CHILD(mchtype)) {
 759                                         (void) tlm_entry_restored(job_stats,
 760                                             longname, pos);
 761 
 762                                         /*
 763                                          * Add an entry to hardlink_q to record
 764                                          * this hardlink.
 765                                          */
 766                                         if (is_hardlink) {
 767                                                 syslog(LOG_DEBUG,
 768                                                     "Restored hardlink file %s",
 769                                                     nmp);
 770 
 771                                                 if (DAR) {
 772                                                         (void) hardlink_q_add(
 773                                                             hardlink_q,
 774                                                             hardlink_inode, 0,
 775                                                             nmp, 0);
 776                                                 }
 777                                         }
 778                                 }
 779 
 780                                 nm_end = 0;
 781                                 longname[0] = 0;
 782                                 lnk_end = 0;
 783                                 longlink[0] = 0;
 784                                 hugename[0] = 0;
 785                                 name[0] = 0;
 786                                 is_long_name = FALSE;
 787                         }
 788                         break;
 789                 case LF_XATTR:
 790                         file_name = (*longname == 0) ? thname_buf :
 791                             longname;
 792 
 793                         size_left = restore_xattr_hdr(&fp, parentlnk,
 794                             file_name, file_size, acls, local_commands,
 795                             job_stats);
 796 
 797                         break;
 798                 case LF_SYMLINK:
 799                         file_name = (*longname == 0) ? thname_buf :
 800                             longname;
 801                         link_name = (*longlink == 0) ?
 802                             tar_hdr->th_linkname : longlink;
 803                         syslog(LOG_DEBUG, "file_name[%s]", file_name);
 804                         syslog(LOG_DEBUG, "link_name[%s]", link_name);
 805                         if (is_file_wanted(file_name, sels, exls, flags,
 806                             &mchtype, &pos)) {
 807                                 nmp = rs_new_name(rnp, name, pos, file_name);
 808                                 if (nmp) {
 809                                         erc = create_sym_link(nmp, link_name,
 810                                             acls, job_stats);
 811                                         if (ERROR_IS_FATAL(erc)) {
 812                                                 rv = erc;
 813                                                 continue;
 814                                         }
 815                                         if (erc == 0 &&
 816                                             PM_EXACT_OR_CHILD(mchtype))
 817                                                 (void) tlm_entry_restored(
 818                                                     job_stats, file_name, pos);
 819                                         name[0] = 0;
 820                                 }
 821                         }
 822                         nm_end = 0;
 823                         longname[0] = 0;
 824                         lnk_end = 0;
 
 896                                         if (erc == 0 &&
 897                                             PM_EXACT_OR_CHILD(mchtype))
 898                                                 (void) tlm_entry_restored(
 899                                                     job_stats, file_name, pos);
 900                                         name[0] = 0;
 901                                 }
 902                         }
 903                         nm_end = 0;
 904                         longname[0] = 0;
 905                         lnk_end = 0;
 906                         longlink[0] = 0;
 907                         break;
 908                 case LF_LONGLINK:
 909                         file_size = min(file_size,
 910                             TLM_MAX_PATH_NAME - lnk_end - 1);
 911                         file_size = max(0, file_size);
 912                         size_left = get_long_name(lib, drv, file_size, longlink,
 913                             &lnk_end, local_commands);
 914 
 915                         if (size_left != 0)
 916                                 syslog(LOG_DEBUG,
 917                                     "fsize %d sleft %d lnkend %d",
 918                                     file_size, size_left, lnk_end);
 919                         break;
 920                 case LF_LONGNAME:
 921                         file_size = min(file_size,
 922                             TLM_MAX_PATH_NAME - nm_end - 1);
 923                         file_size = max(0, file_size);
 924                         size_left = get_long_name(lib, drv, file_size, longname,
 925                             &nm_end, local_commands);
 926 
 927                         if (size_left != 0)
 928                                 syslog(LOG_DEBUG,
 929                                     "fsize %d sleft %d nmend %d",
 930                                     file_size, size_left, nm_end);
 931                         is_long_name = TRUE;
 932                         break;
 933                 case LF_ACL:
 934                         size_left = load_acl_info(lib, drv, file_size, acls,
 935                             &acl_spot, local_commands);
 936                         break;
 937                 case LF_VOLHDR:
 938                         break;
 939                 case LF_HUMONGUS:
 940                         (void) memset(hugename, 0, TLM_MAX_PATH_NAME);
 941                         (void) get_humongus_file_header(lib, drv, file_size,
 942                             &huge_size, hugename, local_commands);
 943                         break;
 944                 default:
 945                         break;
 946 
 947                 }
 948 
 
 
 974         free(hugename);
 975         free(name);
 976         free(thname_buf);
 977         return (rv);
 978 }
 979 
 980 /*
 981  * Main file restore function for tar (should run as a thread)
 982  */
 983 int
 984 tar_getfile(tlm_backup_restore_arg_t *argp)
 985 {
 986         tlm_job_stats_t *job_stats;
 987         char    **sels;         /* list of files desired */
 988         char    **exls;         /* list of files not wanted */
 989         char    *dir;           /* where to restore the files */
 990         char    job[TLM_MAX_BACKUP_JOB_NAME+1];
 991                                 /* the restore job name */
 992         int     erc;            /* error return codes */
 993         int     flags;
 994         int     i;
 995         struct  rs_name_maker rn;
 996         tlm_commands_t *commands;
 997         tlm_cmd_t *local_commands;
 998         char *list = NULL;
 999 
1000         commands = argp->ba_commands;
1001         local_commands = argp->ba_cmd;
1002 
1003         flags = 0;
1004 
1005         dir = ndmp_malloc(TLM_MAX_PATH_NAME);
1006         if (dir == NULL) {
1007                 local_commands->tc_reader = TLM_STOP;
1008                 (void) pthread_barrier_wait(&argp->ba_barrier);
1009                 return (-1);
1010         }
1011 
1012         (void) strlcpy(job, argp->ba_job, TLM_MAX_BACKUP_JOB_NAME+1);
1013         (void) strlcpy(dir, argp->ba_dir, TLM_MAX_PATH_NAME);
1014 
1015         flags |= RSFLG_OVR_ALWAYS;
1016         flags |= RSFLG_IGNORE_CASE;
1017 
1018         /*
1019          * do not test for "dir" having no string, since that
1020          * is a legal condition.  Restore to origional location
1021          * will not have a restore directory.
1022          */
1023         if (*job == '\0') {
1024                 syslog(LOG_DEBUG, "No job defined");
1025                 local_commands->tc_reader = TLM_STOP;
1026                 free(dir);
1027                 (void) pthread_barrier_wait(&argp->ba_barrier);
1028                 return (-1);
1029         }
1030 
1031         sels = ndmp_malloc(sizeof (char *) * (argp->ba_count + 1));
1032             /* One extra for NULL terminate */
1033         if (sels == NULL) {
1034                 local_commands->tc_reader = TLM_STOP;
1035                 free(dir);
1036                 (void) pthread_barrier_wait(&argp->ba_barrier);
1037                 return (-1);
1038         }
1039 
1040         (void) memset(sels, 0, (argp->ba_count + 1) * sizeof (char *));
1041         for (i = 0; i < argp->ba_count; i++) {
1042                 sels[i] = argp->ba_sels[i];
1043         }
1044 
1045         exls = &list;
1046 
1047         tlm_log_list("selections", sels);
1048 
1049         if (wildcard_enabled())
1050                 flags |= RSFLG_MATCH_WCARD;
1051 
1052         local_commands->tc_ref++;
1053         commands->tcs_writer_count++;
1054 
1055         /*
1056          * let the launcher continue
1057          */
1058         (void) pthread_barrier_wait(&argp->ba_barrier);
1059 
1060         job_stats = tlm_ref_job_stats(job);
1061 
1062         rn.rn_fp = catnames;
1063         rn.rn_nlp = dir;
1064 
1065         /*
1066          * work
1067          */
1068         erc = tar_getdir(commands, local_commands, job_stats, &rn, 1, 1,
1069             sels, exls, flags, 0, NULL, NULL);
1070 
1071         /*
1072          * teardown
1073          */
1074         tlm_un_ref_job_stats(job);
1075         tlm_release_list(sels);
1076 
1077         commands->tcs_writer_count--;
1078         local_commands->tc_reader = TLM_STOP;
1079         tlm_release_reader_writer_ipc(local_commands);
1080         free(dir);
1081         return (erc);
1082 }
1083 
1084 /*
1085  * Creates the directories all the way down to the
1086  * end if they dont exist
1087  */
1088 int
1089 make_dirs(char *dir)
1090 {
1091         char c;
1092         char *cp, *end;
1093         struct stat64 st;
1094 
1095         cp = dir;
1096         cp += strspn(cp, "/");
1097         end = dir + strlen(dir);
1098         do {
1099                 if (*cp == '\0' || *cp == '/') {
1100                         c = *cp;
1101                         *cp = '\0';
1102                         if (lstat64(dir, &st) < 0)
1103                                 if (mkdir(dir, 0777) < 0) {
1104                                         syslog(LOG_ERR, "Error %d"
1105                                             " creating directory %s",
1106                                             errno, dir);
1107                                         *cp = c;
1108                                         return (errno);
1109                                 }
1110 
1111                         *cp = c;
1112                 }
1113         } while (++cp <= end);
1114 
1115         return (0);
1116 }
1117 
1118 /*
1119  * Creates the directories leading to the given path
1120  */
1121 int
1122 mkbasedir(char *path)
1123 {
1124         int rv;
1125         char *cp;
1126         struct stat64 st;
1127 
1128         if (!path || !*path) {
1129                 syslog(LOG_ERR, "Invalid argument");
1130                 return (-1);
1131         }
1132 
1133         cp = strrchr(path, '/');
1134         if (cp)
1135                 *cp = '\0';
1136         rv = lstat64(path, &st);
1137         if (rv < 0)  /* need new directories */
1138                 rv = make_dirs(path);
1139         if (cp)
1140                 *cp = '/';
1141 
1142         return (rv);
1143 }
1144 
1145 
1146 /*
1147  * read the file off the tape back onto disk
1148  *
1149  * If the function returns a non-zero return code, it means that fatal error
1150  * was encountered and restore should terminate immediately.
1151  */
1152 static int
1153 restore_file(int *fp,
1154     char *real_name,
1155     long size,
1156     longlong_t huge_size,
1157     tlm_acls_t *acls,
1158     boolean_t want_this_file,
1159     tlm_cmd_t *local_commands,
1160     tlm_job_stats_t *job_stats,
1161     long *size_left)
1162 {
1163         struct stat64   attr;
1164         int     ret, rv;
1165 
1166         *size_left = 0;
1167         if (!real_name) {
1168                 if (want_this_file) {
1169                         syslog(LOG_DEBUG, "No file name but wanted!");
1170                         want_this_file = FALSE;
1171                 }
1172         }
1173 
1174         /*
1175          * OK, some FM is creeping in here ...
1176          * int *fp is used to keep the
1177          * backup file channel open through
1178          * the interruption of EOT and
1179          * processing the headers of the
1180          * next tape.  So, if *fp is zero
1181          * then no file is open yet and all
1182          * is normal.  If *fp has a number
1183          * then we are returning after an
1184          * EOT break.
1185          *
1186          * *fp is now also open for HUGE files
1187          * that are put back in sections.
1188          */
1189 
1190         if (*fp == 0 && want_this_file) {
1191 
1192                 ret = mkbasedir(real_name);
 
1206                 } else if (acls->acl_update) {
1207                         if (attr.st_mtime < acls->acl_attr.st_mtime) {
1208                                 /*EMPTY*/
1209                                 /* tape is newer */
1210                         } else {
1211                                 /* disk file is newer */
1212                                 want_this_file = FALSE;
1213                         }
1214                 } else {
1215                         /*
1216                          * no overwrite, no update,
1217                          * do not ever replace old files.
1218                          */
1219                         want_this_file = TRUE;
1220                 }
1221                 if (want_this_file) {
1222 
1223                         *fp = open(real_name, O_CREAT | O_TRUNC | O_WRONLY,
1224                             S_IRUSR | S_IWUSR);
1225                         if (*fp == -1) {
1226                                 syslog(LOG_ERR,
1227                                     "Could not open %s for restore: %d",
1228                                     real_name, errno);
1229                                 job_stats->js_errors++;
1230                                 want_this_file = FALSE;
1231                                 /*
1232                                  * In case of non-fatal error we cannot return
1233                                  * here, because the file is still on the tape
1234                                  * and must be skipped over.
1235                                  */
1236                                 if (ERROR_IS_FATAL(errno))
1237                                         return (errno);
1238                         }
1239                 }
1240                 (void) strlcpy(local_commands->tc_file_name, real_name,
1241                     TLM_MAX_PATH_NAME);
1242         }
1243 
1244         /*
1245          * this is the size left in the next segment
1246          */
 
1249         /*
1250          * work
1251          */
1252         rv = 0;
1253         while (size > 0 && local_commands->tc_writer == TLM_RESTORE_RUN) {
1254                 int     actual_size;
1255                 int     error;
1256                 char    *rec;
1257                 int     write_size;
1258 
1259                 /*
1260                  * Use bytes_in_file field to tell reader the amount
1261                  * of data still need to be read for this file.
1262                  */
1263                 job_stats->js_bytes_in_file = size;
1264 
1265                 error = 0;
1266                 rec = get_read_buffer(size, &error, &actual_size,
1267                     local_commands);
1268                 if (actual_size <= 0) {
1269                         syslog(LOG_ERR,
1270                             "RESTORE WRITER> error %d, actual_size %d",
1271                             error, actual_size);
1272 
1273                         /* no more data for this file for now */
1274                         job_stats->js_bytes_in_file = 0;
1275                         *size_left = size;
1276                         return (0);
1277                 } else if (error) {
1278                         syslog(LOG_ERR, "Error %d in file [%s]",
1279                             error, local_commands->tc_file_name);
1280                         break;
1281                 }
1282 
1283                 write_size = min(size, actual_size);
1284                 if (want_this_file) {
1285                         ret = write(*fp, rec, write_size);
1286                         if (ret < 0) {
1287                                 syslog(LOG_ERR,
1288                                     "Write error %d for file [%s]", errno,
1289                                     local_commands->tc_file_name);
1290                                 job_stats->js_errors++;
1291                                 if (ERROR_IS_FATAL(errno)) {
1292                                         rv = errno;
1293                                         break;
1294                                 }
1295                         } else {
1296                                 NS_ADD(wdisk, ret);
1297                                 NS_INC(wfile);
1298                                 if (ret < write_size) {
1299                                         syslog(LOG_ERR,
1300                                             "Partial write for file [%s]",
1301                                             local_commands->tc_file_name);
1302                                 }
1303                         }
1304                 }
1305                 size -= write_size;
1306         }
1307 
1308         /* no more data for this file for now */
1309         job_stats->js_bytes_in_file = 0;
1310 
1311         /*
1312          * teardown
1313          */
1314         if (*fp != 0 && huge_size <= 0) {
1315                 (void) close(*fp);
1316                 *fp = 0;
1317                 if (rv == 0) {
1318                         ret = set_acl(real_name, acls);
1319                         if (ERROR_IS_FATAL(ret))
 
1379  * it to the file
1380  */
1381 static long
1382 restore_xattr_hdr(int *fp,
1383     char *name,
1384     char *fname,
1385     long size,
1386     tlm_acls_t *acls,
1387     tlm_cmd_t *local_commands,
1388     tlm_job_stats_t *job_stats)
1389 {
1390         tlm_tar_hdr_t *tar_hdr;
1391         struct xattr_hdr *xhdr;
1392         struct xattr_buf *xbuf;
1393         int namelen;
1394         char *xattrname;
1395         int actual_size;
1396         int error;
1397 
1398         if (!fname) {
1399                 syslog(LOG_DEBUG, "No file name but wanted!");
1400         }
1401 
1402         error = 0;
1403         xhdr = (struct xattr_hdr *)get_read_buffer(size, &error,
1404             &actual_size, local_commands);
1405         if (xhdr == NULL || error != 0) {
1406                 syslog(LOG_ERR,
1407                     "Could not read xattr [%s:%s] for restore. ",
1408                     name, fname);
1409                 job_stats->js_errors++;
1410                 return (0);
1411         }
1412 
1413         /* Check extended attribute header */
1414         if (strcmp(xhdr->h_version, XATTR_ARCH_VERS) != 0) {
1415                 syslog(LOG_ERR,
1416                     "Unrecognized header format [%s]", xhdr->h_version);
1417                 return (0);
1418         }
1419         xbuf = (struct xattr_buf *)(((char *)xhdr) + sizeof (struct xattr_hdr));
1420 
1421         (void) sscanf(xbuf->h_namesz, "%7d", &namelen);
1422         xattrname = xbuf->h_names + strlen(xbuf->h_names) + 1;
1423 
1424         if (*fp == 0) {
1425                 int fd;
1426 
1427                 fd = attropen(name, xattrname, O_CREAT | O_RDWR, 0755);
1428                 if (fd == -1) {
1429                         syslog(LOG_ERR,
1430                             "Could not open xattr [%s:%s] for restore err=%d.",
1431                             name, xattrname, errno);
1432                         job_stats->js_errors++;
1433                         return (0);
1434                 }
1435                 (void) strlcpy(local_commands->tc_file_name, xattrname,
1436                     TLM_MAX_PATH_NAME);
1437                 *fp = fd;
1438         }
1439 
1440         /* Get the actual extended attribute file */
1441         tar_hdr = (tlm_tar_hdr_t *)get_read_buffer(sizeof (*tar_hdr),
1442             &error, &actual_size, local_commands);
1443         if (tar_hdr == NULL || error != 0) {
1444                 syslog(LOG_ERR,
1445                     "Could not read xattr data [%s:%s] for restore. ",
1446                     fname, xattrname);
1447                 job_stats->js_errors++;
1448                 return (0);
1449         }
1450         acls->acl_attr.st_mode = oct_atoi(tar_hdr->th_mode);
1451         acls->acl_attr.st_size = oct_atoi(tar_hdr->th_size);
1452         acls->acl_attr.st_uid = oct_atoi(tar_hdr->th_uid);
1453         acls->acl_attr.st_gid = oct_atoi(tar_hdr->th_gid);
1454         acls->acl_attr.st_mtime = oct_atoi(tar_hdr->th_mtime);
1455 
1456         size = acls->acl_attr.st_size;
1457         while (size > 0 && local_commands->tc_writer == TLM_RESTORE_RUN) {
1458                 char    *rec;
1459                 int     write_size;
1460                 int     sysattr_write = 0;
1461 
1462                 error = 0;
1463                 rec = get_read_buffer(size, &error, &actual_size,
1464                     local_commands);
1465 
1466                 if ((actual_size < size) && sysattr_rw(xattrname)) {
1467                         rec = get_read_one_buf(rec, actual_size, size, &error,
1468                             local_commands);
1469                         if (rec == NULL) {
1470                                 syslog(LOG_ERR, "Error %d in file [%s]",
1471                                     error, xattrname);
1472                                 return (size);
1473                         }
1474                         actual_size = size;
1475                         sysattr_write = 1;
1476                 }
1477                 if (actual_size <= 0) {
1478                         syslog(LOG_ERR,
1479                             "RESTORE WRITER> error %d, actual_size %d",
1480                             error, actual_size);
1481 
1482                         return (size);
1483                 } else if (error) {
1484                         syslog(LOG_ERR, "Error %d in file [%s]",
1485                             error, local_commands->tc_file_name);
1486                         break;
1487                 } else {
1488                         write_size = min(size, actual_size);
1489                         if ((write_size = write(*fp, rec, write_size)) < 0) {
1490                                 if (sysattr_write)
1491                                         free(rec);
1492 
1493                                 break;
1494                         }
1495 
1496                         NS_ADD(wdisk, write_size);
1497                         NS_INC(wfile);
1498                         size -= write_size;
1499                 }
1500                 if (sysattr_write)
1501                         free(rec);
1502         }
1503 
1504         if (*fp != 0) {
 
1507                 *fp = 0;
1508         }
1509         return (0);
1510 }
1511 
1512 /*
1513  * Match the name with the list
1514  */
1515 static int
1516 exact_find(char *name, char **list)
1517 {
1518         boolean_t found;
1519         int i;
1520         char *cp;
1521 
1522         found = FALSE;
1523         for (i = 0; *list != NULL; list++, i++) {
1524                 cp = *list + strspn(*list, "/");
1525                 if (match(cp, name)) {
1526                         found = TRUE;
1527                         syslog(LOG_DEBUG, "exact_find> found[%s]", cp);
1528                         break;
1529                 }
1530         }
1531 
1532         return (found);
1533 }
1534 
1535 /*
1536  * On error, return FALSE and prevent restoring(probably) unwanted data.
1537  */
1538 static int
1539 is_parent(char *parent, char *child, int flags)
1540 {
1541         char tmp[TLM_MAX_PATH_NAME];
1542         boolean_t rv;
1543 
1544         if (IS_SET(flags, RSFLG_MATCH_WCARD)) {
1545                 if (!tlm_cat_path(tmp, parent, "*")) {
1546                         syslog(LOG_ERR,
1547                             "is_parent> path too long [%s]", parent);
1548                         rv = FALSE;
1549                 } else
1550                         rv = (match(tmp, child) != 0) ? TRUE : FALSE;
1551         } else {
1552                 if (!tlm_cat_path(tmp, parent, "/")) {
1553                         syslog(LOG_ERR,
1554                             "is_parent> path too long [%s]", parent);
1555                         rv = FALSE;
1556                 } else
1557                         rv = (strncmp(tmp, child, strlen(tmp)) == 0) ?
1558                             TRUE : FALSE;
1559         }
1560 
1561         return (rv);
1562 }
1563 
1564 /*
1565  * Used to match the filename inside the list
1566  */
1567 static boolean_t
1568 strexactcmp(char *s, char *t)
1569 {
1570         return ((strcmp(s, t) == 0) ? TRUE : FALSE);
1571 }
1572 
1573 /*
1574  * Check if the file is needed to be restored
1575  */
1576 static boolean_t
1577 is_file_wanted(char *name,
1578     char **sels,
1579     char **exls,
1580     int flags,
1581     int *mchtype,
1582     int *pos)
1583 {
1584         char *p_sel;
1585         char *uc_name = NULL, *retry, *namep;
1586         boolean_t found;
1587         int i;
1588         name_match_fp_t *cmp_fp;
1589 
1590         if (name == NULL || sels == NULL || exls == NULL)
1591                 return (FALSE);
1592 
1593         found = FALSE;
1594         if (mchtype != NULL)
1595                 *mchtype = PM_NONE;
1596         if (pos != NULL)
1597                 *pos = 0;
1598 
1599         /*
1600          * For empty selection, restore everything
1601          */
1602         if (*sels == NULL || **sels == '\0') {
1603                 syslog(LOG_DEBUG, "is_file_wanted: Restore all");
1604                 return (TRUE);
1605         }
1606 
1607         retry = ndmp_malloc(TLM_MAX_PATH_NAME);
1608         if (retry == NULL)
1609                 return (FALSE);
1610 
1611         if (IS_SET(flags, RSFLG_MATCH_WCARD))
1612                 cmp_fp = match;
1613         else
1614                 cmp_fp = strexactcmp;
1615 
1616         namep = name + strspn(name, "/");
1617 
1618         if (IS_SET(flags, RSFLG_IGNORE_CASE)) {
1619                 uc_name = ndmp_malloc(TLM_MAX_PATH_NAME);
1620                 if (uc_name == NULL) {
1621                         free(retry);
1622                         return (FALSE);
1623                 }
1624                 (void) strlcpy(uc_name, namep, TLM_MAX_PATH_NAME);
1625                 (void) strupr(uc_name);
1626                 namep = uc_name;
1627         }
1628 
1629         for (i = 0; *sels != NULL; sels++, i++) {
1630                 p_sel = *sels + strspn(*sels, "/");
1631 
1632                 /*
1633                  * Try exact match.
1634                  */
1635                 if ((*cmp_fp)(p_sel, namep)) {
1636                         found = TRUE;
1637                         if (mchtype != NULL)
1638                                 *mchtype = PM_EXACT;
1639                         break;
1640                 }
1641                 /*
1642                  * Try "entry/" and the current selection.  The
1643                  * current selection may be something like "<something>/".
1644                  */
1645                 (void) tlm_cat_path(retry, namep, "/");
1646                 if ((*cmp_fp)(p_sel, retry)) {
1647                         syslog(LOG_DEBUG, "match2> pos %d [%s][%s]",
1648                             i, p_sel, name);
1649                         found = TRUE;
1650                         if (mchtype != NULL)
1651                                 *mchtype = PM_EXACT;
1652                         break;
1653                 }
1654                 /*
1655                  * If the following check returns true it means that the
1656                  * 'name' is an entry below the 'p_sel' hierarchy.
1657                  */
1658                 if (is_parent(p_sel, namep, flags)) {
1659                         found = TRUE;
1660                         if (mchtype != NULL)
1661                                 *mchtype = PM_CHILD;
1662                         break;
1663                 }
1664                 /*
1665                  * There is a special case for parent directories of a
1666                  * selection.  If 'p_sel' is something like "*d1", the
1667                  * middle directories of the final entry can't be determined
1668                  * until the final entry matches with 'p_sel'.  At that
1669                  * time the middle directories of the entry have been passed
1670                  * and they can't be restored.
1671                  */
1672                 if (is_parent(namep, p_sel, flags)) {
1673                         syslog(LOG_DEBUG, "parent2> pos %d [%s][%s]",
1674                             i, p_sel, name);
1675                         found = TRUE;
1676                         if (mchtype != NULL)
1677                                 *mchtype = PM_PARENT;
1678                         break;
1679                 }
1680         }
1681 
1682         /* Check for exclusions.  */
1683         if (found && exact_find(namep, exls)) {
1684                 if (mchtype != NULL)
1685                         *mchtype = PM_NONE;
1686                 found = FALSE;
1687         }
1688         if (found && pos != NULL)
1689                 *pos = i;
1690 
1691         if (IS_SET(flags, RSFLG_IGNORE_CASE))
1692                 free(uc_name);
1693         free(retry);
 
1695 }
1696 
1697 /*
1698  * Read the specified amount data into the buffer.  Detects EOT or EOF
1699  * during read.
1700  *
1701  * Returns the number of bytes actually read.  On error returns -1.
1702  */
1703 static int
1704 input_mem(int l,
1705     int d,
1706     tlm_cmd_t *lcmds,
1707     char *mem,
1708     int len)
1709 {
1710         int err;
1711         int toread, actual_size, rec_size;
1712         char *rec;
1713 
1714         if (l <= 0 || d <= 0 || !lcmds || !mem) {
1715                 syslog(LOG_ERR, "Invalid argument");
1716                 return (-1);
1717         }
1718 
1719         toread = len;
1720         while (toread > 0) {
1721                 rec = get_read_buffer(toread, &err, &actual_size, lcmds);
1722                 if (actual_size <= 0) {
1723                         syslog(LOG_ERR, "err %d act_size %d detected",
1724                             err, actual_size);
1725                         break;
1726                 } else if (err) {
1727                         syslog(LOG_ERR, "error %d reading data", err);
1728                         return (-1);
1729                 }
1730                 rec_size = min(actual_size, toread);
1731                 (void) memcpy(mem, rec, rec_size);
1732                 mem += rec_size;
1733                 toread -= rec_size;
1734         }
1735 
1736         return (len - toread);
1737 }
1738 
1739 /*
1740  * pick up the name and size of a HUGE file
1741  */
1742 static  int
1743 get_humongus_file_header(int lib,
1744     int drv,
1745     long recsize,
1746     longlong_t *size,
1747     char *name,
1748     tlm_cmd_t *local_commands)
1749 {
1750         char *p_record, *value;
1751         int rv;
1752 
1753         syslog(LOG_WARNING, "HUGE Record found: %d", recsize);
1754 
1755         rv = 0;
1756         if (recsize == 0) {
1757                 /*
1758                  * The humongus_file_header was written in a
1759                  * RECORDSIZE block and the header.size field of this
1760                  * record was 0 before this fix.  For backward compatiblity
1761                  * read only one RECORDSIZE-size block if the header.size
1762                  * field is 0.  Otherwise the header.size field should show
1763                  * the length of the data of this header.
1764                  */
1765                 syslog(LOG_WARNING, "Old HUGE record found");
1766                 recsize = RECORDSIZE;
1767         }
1768 
1769         if (input_mem(lib, drv, local_commands, name, recsize) != recsize) {
1770                 rv = -1;
1771                 *size = 0;
1772                 *name = '\0';
1773                 syslog(LOG_ERR, "Error reading a HUGE file name");
1774         } else {
1775                 syslog(LOG_DEBUG, "HUGE [%s]", name);
1776 
1777                 p_record = name;
1778                 value = parse(&p_record, " ");
1779                 *size = atoll(value);
1780                 /*
1781                  * Note: Since the backed up names are not longer than
1782                  * NAME_MAX and the buffer passed to us is
1783                  * TLM_MAX_PATH_NAME, it should be safe to use strlcpy
1784                  * without check on the buffer size.
1785                  */
1786                 (void) strlcpy(name, p_record, TLM_MAX_PATH_NAME);
1787         }
1788 
1789         syslog(LOG_DEBUG, "HUGE Record %lld [%s]", *size, name);
1790 
1791         return (rv);
1792 }
1793 
1794 /*
1795  * pick up the long name from the special tape file
1796  */
1797 static int
1798 get_long_name(int lib,
1799     int drv,
1800     long recsize,
1801     char *name,
1802     long *buf_spot,
1803     tlm_cmd_t *local_commands)
1804 {
1805         int nread;
1806 
1807         syslog(LOG_DEBUG, "LONGNAME Record found rs %d bs %d", recsize,
1808             *buf_spot);
1809 
1810         if (*buf_spot < 0)
1811                 *buf_spot = 0;
1812 
1813         nread = input_mem(lib, drv, local_commands, name + *buf_spot,
1814             recsize);
1815         if (nread < 0) {
1816                 nread = recsize; /* return 0 as size left */
1817                 name[*buf_spot] = '\0';
1818                 syslog(LOG_ERR, "Error %d reading a long file name %s.",
1819                     nread, name);
1820         } else {
1821                 *buf_spot += nread;
1822                 name[*buf_spot] = '\0';
1823                 syslog(LOG_DEBUG, "LONGNAME [%s]", name);
1824         }
1825 
1826         return (recsize - nread);
1827 }
1828 
1829 /*
1830  * create a new directory
1831  */
1832 static int
1833 create_directory(char *dir, tlm_job_stats_t *job_stats)
1834 {
1835         struct stat64 attr;
1836         char    *p;
1837         char    temp;
1838         int     erc;
1839 
1840         /*
1841          * Make sure all directories in this path exist, create them if
1842          * needed.
1843          */
1844 
1845         erc = 0;
1846         p = &dir[1];
1847         do {
1848                 temp = *p;
1849                 if (temp == '/' || temp == 0) {
1850                         *p = 0;
1851                         if (stat64(dir, &attr) < 0) {
1852                                 if (mkdir(dir, 0777) != 0 && errno != EEXIST) {
1853                                         erc = errno;
1854                                         job_stats->js_errors++;
1855                                         syslog(LOG_ERR,
1856                                             "Could not create directory %s: %d",
1857                                             dir, errno);
1858                                         break;
1859                                 }
1860                         }
1861                         *p = temp;
1862                 }
1863                 p++;
1864         } while (temp != 0);
1865 
1866         return (erc);
1867 }
1868 
1869 /*
1870  * create a new hardlink
1871  */
1872 static int
1873 create_hard_link(char *name_old, char *name_new,
1874     tlm_acls_t *acls, tlm_job_stats_t *job_stats)
1875 {
1876         int erc;
1877 
1878         erc = mkbasedir(name_new);
1879         if (erc != 0)
1880                 return (erc);
1881 
1882         if (link(name_old, name_new) != 0)
1883                 erc = errno;
1884 
1885         if (erc) {
1886                 /* Nothing to do if the destination already exists */
1887                 if (erc == EEXIST)
1888                         return (0);
1889                 job_stats->js_errors++;
1890                 syslog(LOG_ERR, "error %d (errno %d) hardlink [%s] to [%s]",
1891                     erc, errno, name_new, name_old);
1892                 return (erc);
1893         }
1894         return (set_acl(name_new, acls));
1895 }
1896 
1897 /*
1898  * create a new symlink
1899  */
1900 /*ARGSUSED*/
1901 static int
1902 create_sym_link(char *dst, char *target, tlm_acls_t *acls,
1903     tlm_job_stats_t *job_stats)
1904 {
1905         int erc;
1906         struct stat64 *st;
1907 
1908         erc = mkbasedir(dst);
1909         if (erc != 0)
1910                 return (erc);
1911 
1912         st = &acls->acl_attr;
1913         if (symlink(target, dst) != 0) {
1914                 erc = errno;
1915                 if (errno == EEXIST) {
1916                         erc = 0;
1917                         syslog(LOG_DEBUG,
1918                             "softlink [%s] to [%s] already existed",
1919                             dst, target);
1920                 } else {
1921                         job_stats->js_errors++;
1922                         syslog(LOG_ERR, "error %d softlink [%s] to [%s]",
1923                             errno, dst, target);
1924                 }
1925         } else {
1926                 st->st_mode |= S_IFLNK;
1927                 erc = set_acl(dst, acls);
1928         }
1929 
1930         return (erc);
1931 }
1932 
1933 /*
1934  * create a new FIFO, char/block device special files
1935  */
1936 static int
1937 create_special(char flag, char *name, tlm_acls_t *acls, int major, int minor,
1938     tlm_job_stats_t *job_stats)
1939 {
1940         dev_t dev;
1941         mode_t mode;
1942 
1943         switch (flag) {
1944         case LF_CHR:
1945                 mode = S_IFCHR;
1946                 dev = makedev(major, minor);
1947                 break;
1948         case LF_BLK:
1949                 mode = S_IFBLK;
1950                 dev = makedev(major, minor);
1951                 break;
1952         case LF_FIFO:
1953                 mode = S_IFIFO;
1954                 dev = 0;
1955                 break;
1956         default:
1957                 syslog(LOG_ERR, "unsupported flag %d", flag);
1958                 return (-1);
1959         }
1960 
1961         /* Remove the old entry first */
1962         if (rmdir(name) < 0) {
1963                 if (errno == ENOTDIR)
1964                         (void) unlink(name);
1965         }
1966         if (mknod(name, 0777 | mode, dev) != 0) {
1967                 job_stats->js_errors++;
1968                 syslog(LOG_ERR, "error %d mknod [%s] major"
1969                     " %d minor %d", errno, name, major, minor);
1970                 return (errno);
1971         }
1972         return (set_acl(name, acls));
1973 }
1974 
1975 /*
1976  * read in the ACLs for the next file
1977  */
1978 static long
1979 load_acl_info(int lib,
1980     int drv,
1981     long file_size,
1982     tlm_acls_t *acls,
1983     long *acl_spot,
1984     tlm_cmd_t *local_commands)
1985 {
1986         char *bp;
1987         int nread;
1988 
1989         /*
1990          * If the ACL is spanned on tapes, then the acl_spot should NOT be
1991          * 0 on next calls to this function to read the rest of the ACL
1992          * on next tapes.
1993          */
1994         if (*acl_spot == 0) {
1995                 (void) memset(acls, 0, sizeof (tlm_acls_t));
1996         }
1997 
1998         bp = ((char *)&acls->acl_info) + *acl_spot;
1999         nread = input_mem(lib, drv, local_commands, (void *)bp, file_size);
2000         if (nread < 0) {
2001                 *acl_spot = 0;
2002                 (void) memset(acls, 0, sizeof (tlm_acls_t));
2003                 syslog(LOG_ERR, "Error reading ACL data");
2004                 return (0);
2005         }
2006         *acl_spot += nread;
2007         acls->acl_non_trivial = TRUE;
2008 
2009         return (file_size - nread);
2010 }
2011 
2012 static int
2013 ndmp_set_eprivs_least(void)
2014 {
2015         priv_set_t *priv_set;
2016 
2017         if ((priv_set = priv_allocset()) == NULL) {
2018                 syslog(LOG_ERR, "Out of memory.");
2019                 return (-1);
2020         }
2021 
2022         priv_basicset(priv_set);
2023 
2024         (void) priv_addset(priv_set, PRIV_PROC_AUDIT);
2025         (void) priv_addset(priv_set, PRIV_PROC_SETID);
2026         (void) priv_addset(priv_set, PRIV_PROC_OWNER);
2027         (void) priv_addset(priv_set, PRIV_FILE_CHOWN);
2028         (void) priv_addset(priv_set, PRIV_FILE_CHOWN_SELF);
2029         (void) priv_addset(priv_set, PRIV_FILE_DAC_READ);
2030         (void) priv_addset(priv_set, PRIV_FILE_DAC_SEARCH);
2031         (void) priv_addset(priv_set, PRIV_FILE_DAC_WRITE);
2032         (void) priv_addset(priv_set, PRIV_FILE_OWNER);
2033         (void) priv_addset(priv_set, PRIV_FILE_SETID);
2034         (void) priv_addset(priv_set, PRIV_SYS_LINKDIR);
2035         (void) priv_addset(priv_set, PRIV_SYS_DEVICES);
2036         (void) priv_addset(priv_set, PRIV_SYS_MOUNT);
2037         (void) priv_addset(priv_set, PRIV_SYS_CONFIG);
2038 
2039         if (setppriv(PRIV_SET, PRIV_EFFECTIVE, priv_set) == -1) {
2040                 syslog(LOG_ERR, "Additional privileges required.");
2041                 priv_freeset(priv_set);
2042                 return (-1);
2043         }
2044         priv_freeset(priv_set);
2045         return (0);
2046 }
2047 
2048 static int
2049 ndmp_set_eprivs_all(void)
2050 {
2051         priv_set_t *priv_set;
2052 
2053         if ((priv_set = priv_allocset()) == NULL) {
2054                 syslog(LOG_ERR, "Out of memory.");
2055                 return (-1);
2056         }
2057 
2058         priv_fillset(priv_set);
2059 
2060         if (setppriv(PRIV_SET, PRIV_EFFECTIVE, priv_set) != 0) {
2061                 syslog(LOG_ERR, "Additional privileges required.");
2062                 return (-1);
2063         }
2064         priv_freeset(priv_set);
2065         return (0);
2066 }
2067 
2068 /*
2069  * Set the standard attributes of the file
2070  */
2071 static int
2072 set_attr(char *name, tlm_acls_t *acls)
2073 {
2074         struct utimbuf tbuf;
2075         boolean_t priv_all = FALSE;
2076         struct stat64 *st;
2077         uid_t uid;
2078         gid_t gid;
2079         struct passwd *pwd;
2080         struct group *grp;
2081         int erc = 0;
2082 
2083 
2084         if (!name || !acls)
2085                 return (0);
2086 
2087         st = &acls->acl_attr;
2088 
2089         uid = st->st_uid;
2090         if ((pwd = getpwnam(acls->uname)) != NULL) {
2091                 uid = pwd->pw_uid;
2092         }
2093 
2094         gid = st->st_gid;
2095         if ((grp = getgrnam(acls->gname)) != NULL) {
2096                 gid = grp->gr_gid;
2097         }
2098 
2099         erc = lchown(name, uid, gid);
2100         if (erc != 0) {
2101                 erc = errno;
2102                 syslog(LOG_ERR,
2103                     "Could not set uid or/and gid for file %s.", name);
2104         }
2105 
2106         if ((st->st_mode & (S_ISUID | S_ISGID)) != 0) {
2107                 /*
2108                  * Change effective privileges to 'all' which is required to
2109                  * change setuid bit for 'root' owned files. If fails, just
2110                  * send error to log file and proceed.
2111                  */
2112                 if (ndmp_set_eprivs_all()) {
2113                         syslog(LOG_ERR,
2114                             "Could not set effective privileges to 'all'.");
2115                 } else {
2116                         priv_all = TRUE;
2117                 }
2118         }
2119 
2120         if (!S_ISLNK(st->st_mode)) {
2121                 erc = chmod(name, st->st_mode);
2122                 if (erc != 0) {
2123                         erc = errno;
2124                         syslog(LOG_ERR, "Could not set correct file"
2125                             " permission for file %s: %d", name, errno);
2126                 }
2127 
2128                 tbuf.modtime = st->st_mtime;
2129                 tbuf.actime = st->st_atime;
2130                 (void) utime(name, &tbuf);
2131         }
2132 
2133         if (priv_all == TRUE) {
2134                 /*
2135                  * Give up the 'all' privileges for effective sets and go back
2136                  * to least required privileges. If fails, just send error to
2137                  * log file and proceed.
2138                  */
2139                 if (ndmp_set_eprivs_least())
2140                         syslog(LOG_ERR,
2141                             "Could not set least required privileges.");
2142         }
2143 
2144         return (erc);
2145 }
2146 
2147 /*
2148  * Set the ACL info for the file
2149  */
2150 static int
2151 set_acl(char *name, tlm_acls_t *acls)
2152 {
2153         int erc;
2154         acl_t *aclp = NULL;
2155 
2156         if (acls == NULL)
2157                 return (0);
2158 
2159         /* Need a place to save real modification time */
2160 
2161         erc = set_attr(name, acls);
2162         if (ERROR_IS_FATAL(erc))
2163                 return (erc);
2164 
2165         if (!acls->acl_non_trivial) {
2166                 (void) memset(acls, 0, sizeof (tlm_acls_t));
2167                 return (erc);
2168         }
2169 
2170         erc = acl_fromtext(acls->acl_info.attr_info, &aclp);
2171         if (erc != 0) {
2172                 syslog(LOG_ERR,
2173                     "TAPE RESTORE> acl_fromtext errno %d", erc);
2174         }
2175         if (aclp) {
2176                 erc = acl_set(name, aclp);
2177                 if (erc < 0) {
2178                         erc = errno;
2179                         syslog(LOG_ERR,
2180                             "TAPE RESTORE> acl_set errno %d", errno);
2181                 }
2182                 acl_free(aclp);
2183         }
2184         (void) memset(acls, 0, sizeof (tlm_acls_t));
2185         return (erc);
2186 }
2187 
2188 /*
2189  * a wrapper to tlm_get_read_buffer so that
2190  * we can cleanly detect ABORT commands
2191  * without involving the TLM library with
2192  * our problems.
2193  */
2194 static char *
2195 get_read_buffer(int want,
2196     int *error,
2197     int *actual_size,
2198     tlm_cmd_t *local_commands)
2199 {
 
2221 wildcard_enabled(void)
2222 {
2223         char *cp;
2224 
2225         cp = ndmpd_get_prop_default(NDMP_RESTORE_WILDCARD_ENABLE, "n");
2226         return ((toupper(*cp) == 'Y') ? TRUE : FALSE);
2227 }
2228 
2229 
2230 /*
2231  * Concatenate two names
2232  */
2233 /*ARGSUSED*/
2234 static char *
2235 catnames(struct rs_name_maker *rnp, char *buf, int pos, char *path)
2236 {
2237         char *rv;
2238 
2239         rv = NULL;
2240         if (!buf) {
2241                 syslog(LOG_DEBUG, "buf is NULL");
2242         } else if (!path) {
2243                 syslog(LOG_DEBUG, "path is NULL");
2244         } else if (!rnp->rn_nlp) {
2245                 syslog(LOG_DEBUG, "rn_nlp is NULL [%s]", path);
2246         } else if (!tlm_cat_path(buf, rnp->rn_nlp, path)) {
2247                 syslog(LOG_DEBUG, "Path too long [%s][%s]",
2248                     rnp->rn_nlp, path);
2249         } else
2250                 rv = buf;
2251 
2252         return (rv);
2253 }
2254 
2255 
2256 /*
2257  * Create a new name path for restore
2258  */
2259 static char *
2260 rs_new_name(struct rs_name_maker *rnp, char *buf, int pos, char *path)
2261 {
2262         if (!rnp || !rnp->rn_fp)
2263                 return (NULL);
2264 
2265         return (*rnp->rn_fp)(rnp, buf, pos, path);
2266 }
2267 
2268 /*
2269  * Clear the extra "/" in the tar header if exists
2270  */
2271 static void
2272 rs_create_new_bkpath(char *bk_path, char *path, char *pbuf)
2273 {
2274         char *p, *slashp;
2275 
2276         if ((p = strstr(path, bk_path)) == NULL) {
2277                 (void) strlcpy(pbuf, path, TLM_MAX_PATH_NAME);
2278                 return;
2279         }
2280         if (*(p += strlen(bk_path)) == '/')
2281                 p++;
2282 
2283         slashp = bk_path + strlen(bk_path) - 1;
2284         if (*slashp == '/')
2285                 (void) snprintf(pbuf, TLM_MAX_PATH_NAME, "%s%s", bk_path, p);
2286         else
2287                 (void) snprintf(pbuf, TLM_MAX_PATH_NAME, "%s/%s", bk_path, p);
2288 }
2289 
2290 
2291 /*
2292  * Iterate over ZFS metadata stored in the backup stream and use the callback
2293  * to restore it.
2294  */
2295 int
2296 ndmp_iter_zfs(ndmp_context_t *nctx, int (*np_restore_property)(nvlist_t *,
2297     void *), void *ptr)
2298 {
2299         tlm_commands_t *cmds;
2300         ndmp_metadata_header_t *mhp;
2301         ndmp_metadata_header_ext_t *mhpx;
2302         ndmp_metadata_property_t *mpp;
2303         ndmp_metadata_property_ext_t *mppx;
2304         tlm_cmd_t *lcmd;
2305         int actual_size;
2306         nvlist_t *nvl;
2307         nvlist_t *valp;
 
2369                         (void) memcpy(pp, tp, lsize);
2370                         sz += lsize;
2371                         pp += lsize;
2372                 }
2373                 if (sz > size) {
2374                         tlm_unget_read_buffer(lcmd->tc_buffers, sz - size);
2375                 }
2376 
2377                 /* LINTED improper alignment */
2378                 mhp = (ndmp_metadata_header_t *)mhbuf;
2379 
2380                 nvl = NULL;
2381                 if (strncmp(mhp->nh_magic, ZFS_META_MAGIC_EXT,
2382                     sizeof (mhp->nh_magic)) == 0) {
2383                         /* New metadata format */
2384                         /* LINTED improper alignment */
2385                         mhpx = (ndmp_metadata_header_ext_t *)mhbuf;
2386 
2387                         if (mhpx->nh_major > META_HDR_MAJOR_VERSION) {
2388                                 /* Major header mismatch */
2389                                 syslog(LOG_ERR, "metadata header mismatch",
2390                                     "M%d != M%d", mhpx->nh_major,
2391                                     META_HDR_MAJOR_VERSION);
2392                                 free(mhbuf);
2393                                 return (-1);
2394                         }
2395                         if (mhpx->nh_major == META_HDR_MAJOR_VERSION &&
2396                             mhpx->nh_minor > META_HDR_MINOR_VERSION) {
2397                                 /* Minor header mismatch */
2398                                 syslog(LOG_ERR, "Warning:"
2399                                     "metadata header mismatch m%d != m%d",
2400                                     mhpx->nh_minor,
2401                                     META_HDR_MINOR_VERSION);
2402                                 continue;
2403                         }
2404 
2405                         nctx->nc_plversion = mhpx->nh_plversion;
2406                         (void) strlcpy(plname, mhpx->nh_plname,
2407                             sizeof (plname));
2408 
2409                         if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
2410                                 goto nvlist_err;
2411 
2412                         mppx = &mhpx->nh_property[0];
2413                         for (i = 0; i < mhpx->nh_count && mppx; i++, mppx++) {
2414                                 if (!*mppx->mp_name)
2415                                         continue;
2416                                 valp = NULL;
2417                                 if (nvlist_alloc(&valp,
2418                                     NV_UNIQUE_NAME, 0) != 0 ||
 
 |