10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
52812Sjc144527 * Common Development and Distribution License (the "License").
62812Sjc144527 * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
210Sstevel@tonic-gate /*
2212369SJohn.Zolnowsky@Sun.COM * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
230Sstevel@tonic-gate */
240Sstevel@tonic-gate
250Sstevel@tonic-gate #include <signal.h>
260Sstevel@tonic-gate #include <unistd.h>
270Sstevel@tonic-gate #include <sys/acl.h>
280Sstevel@tonic-gate #include <sys/statvfs.h>
290Sstevel@tonic-gate #include <sys/wait.h>
300Sstevel@tonic-gate #include "bart.h"
31789Sahrens #include <aclutils.h>
320Sstevel@tonic-gate
330Sstevel@tonic-gate static int sanitize_reloc_root(char *root, size_t bufsize);
340Sstevel@tonic-gate static int create_manifest_filelist(char **argv, char *reloc_root);
350Sstevel@tonic-gate static int create_manifest_rule(char *reloc_root, FILE *rule_fp);
360Sstevel@tonic-gate static void output_manifest(void);
37*13116SJan.Parcel@Sun.COM static int eval_file(const char *fname, const struct stat64 *statb,
38*13116SJan.Parcel@Sun.COM struct FTW *ftwx);
390Sstevel@tonic-gate static char *sanitized_fname(const char *, boolean_t);
400Sstevel@tonic-gate static char *get_acl_string(const char *fname, const struct stat64 *statb,
410Sstevel@tonic-gate int *err_code);
420Sstevel@tonic-gate static int generate_hash(int fdin, char *hash_str);
430Sstevel@tonic-gate static int read_filelist(char *reloc_root, char **argv, char *buf,
440Sstevel@tonic-gate size_t bufsize);
450Sstevel@tonic-gate static int walker(const char *name, const struct stat64 *sp,
460Sstevel@tonic-gate int type, struct FTW *ftwx);
470Sstevel@tonic-gate
480Sstevel@tonic-gate /*
490Sstevel@tonic-gate * The following globals are necessary due to the "walker" function
500Sstevel@tonic-gate * provided by nftw(). Since there is no way to pass them through to the
510Sstevel@tonic-gate * walker function, they must be global.
520Sstevel@tonic-gate */
530Sstevel@tonic-gate static int compute_chksum = 1, eval_err = 0;
540Sstevel@tonic-gate static struct rule *subtree_root;
550Sstevel@tonic-gate static char reloc_root[PATH_MAX];
569298SWilliam.Young@Sun.COM static struct statvfs64 parent_vfs;
570Sstevel@tonic-gate
580Sstevel@tonic-gate int
bart_create(int argc,char ** argv)590Sstevel@tonic-gate bart_create(int argc, char **argv)
600Sstevel@tonic-gate {
610Sstevel@tonic-gate boolean_t filelist_input;
620Sstevel@tonic-gate int ret, c, output_pipe[2];
630Sstevel@tonic-gate FILE *rules_fd = NULL;
640Sstevel@tonic-gate pid_t pid;
650Sstevel@tonic-gate
660Sstevel@tonic-gate filelist_input = B_FALSE;
670Sstevel@tonic-gate reloc_root[0] = '\0';
680Sstevel@tonic-gate
690Sstevel@tonic-gate while ((c = getopt(argc, argv, "Inr:R:")) != EOF) {
700Sstevel@tonic-gate switch (c) {
710Sstevel@tonic-gate case 'I':
720Sstevel@tonic-gate if (rules_fd != NULL) {
730Sstevel@tonic-gate (void) fprintf(stderr, "%s", INPUT_ERR);
740Sstevel@tonic-gate usage();
750Sstevel@tonic-gate }
760Sstevel@tonic-gate filelist_input = B_TRUE;
770Sstevel@tonic-gate break;
780Sstevel@tonic-gate
790Sstevel@tonic-gate case 'n':
800Sstevel@tonic-gate compute_chksum = 0;
810Sstevel@tonic-gate break;
820Sstevel@tonic-gate
830Sstevel@tonic-gate case 'r':
840Sstevel@tonic-gate if (strcmp(optarg, "-") == 0)
850Sstevel@tonic-gate rules_fd = stdin;
860Sstevel@tonic-gate else
870Sstevel@tonic-gate rules_fd = fopen(optarg, "r");
880Sstevel@tonic-gate if (rules_fd == NULL) {
890Sstevel@tonic-gate perror(optarg);
900Sstevel@tonic-gate usage();
910Sstevel@tonic-gate }
920Sstevel@tonic-gate break;
930Sstevel@tonic-gate
940Sstevel@tonic-gate case 'R':
950Sstevel@tonic-gate (void) strlcpy(reloc_root, optarg, sizeof (reloc_root));
960Sstevel@tonic-gate ret = sanitize_reloc_root(reloc_root,
970Sstevel@tonic-gate sizeof (reloc_root));
980Sstevel@tonic-gate if (ret == 0)
990Sstevel@tonic-gate usage();
1000Sstevel@tonic-gate break;
1010Sstevel@tonic-gate
1020Sstevel@tonic-gate case '?':
1030Sstevel@tonic-gate default :
1040Sstevel@tonic-gate usage();
1050Sstevel@tonic-gate }
1060Sstevel@tonic-gate }
1070Sstevel@tonic-gate argv += optind;
1080Sstevel@tonic-gate
1090Sstevel@tonic-gate if (pipe(output_pipe) < 0) {
1100Sstevel@tonic-gate perror("");
1110Sstevel@tonic-gate exit(FATAL_EXIT);
1120Sstevel@tonic-gate }
1130Sstevel@tonic-gate
1140Sstevel@tonic-gate pid = fork();
1150Sstevel@tonic-gate if (pid < 0) {
1160Sstevel@tonic-gate perror(NULL);
1170Sstevel@tonic-gate exit(FATAL_EXIT);
1180Sstevel@tonic-gate }
1190Sstevel@tonic-gate
1200Sstevel@tonic-gate /*
1210Sstevel@tonic-gate * Break the creation of a manifest into two parts: the parent process
1220Sstevel@tonic-gate * generated the data whereas the child process sorts the data.
1230Sstevel@tonic-gate *
1240Sstevel@tonic-gate * The processes communicate through the pipe.
1250Sstevel@tonic-gate */
1260Sstevel@tonic-gate if (pid > 0) {
1270Sstevel@tonic-gate /*
1280Sstevel@tonic-gate * Redirect the stdout of this process so it goes into
1290Sstevel@tonic-gate * output_pipe[0]. The output of this process will be read
1300Sstevel@tonic-gate * by the child, which will sort the output.
1310Sstevel@tonic-gate */
1320Sstevel@tonic-gate if (dup2(output_pipe[0], STDOUT_FILENO) != STDOUT_FILENO) {
1330Sstevel@tonic-gate perror(NULL);
1340Sstevel@tonic-gate exit(FATAL_EXIT);
1350Sstevel@tonic-gate }
1360Sstevel@tonic-gate (void) close(output_pipe[0]);
1370Sstevel@tonic-gate (void) close(output_pipe[1]);
1380Sstevel@tonic-gate
1390Sstevel@tonic-gate if (filelist_input == B_TRUE) {
1400Sstevel@tonic-gate ret = create_manifest_filelist(argv, reloc_root);
1410Sstevel@tonic-gate } else {
1420Sstevel@tonic-gate ret = create_manifest_rule(reloc_root, rules_fd);
1430Sstevel@tonic-gate }
1440Sstevel@tonic-gate
1450Sstevel@tonic-gate /* Close stdout so the sort in the child proc will complete */
1460Sstevel@tonic-gate (void) fclose(stdout);
1470Sstevel@tonic-gate } else {
1480Sstevel@tonic-gate /*
1490Sstevel@tonic-gate * Redirect the stdin of this process so its read in from
1500Sstevel@tonic-gate * the pipe, which is the parent process in this case.
1510Sstevel@tonic-gate */
1520Sstevel@tonic-gate if (dup2(output_pipe[1], STDIN_FILENO) != STDIN_FILENO) {
1530Sstevel@tonic-gate perror(NULL);
1540Sstevel@tonic-gate exit(FATAL_EXIT);
1550Sstevel@tonic-gate }
1560Sstevel@tonic-gate (void) close(output_pipe[0]);
1570Sstevel@tonic-gate
1580Sstevel@tonic-gate output_manifest();
1590Sstevel@tonic-gate }
1600Sstevel@tonic-gate
1610Sstevel@tonic-gate /* Wait for the child proc (the sort) to complete */
1620Sstevel@tonic-gate (void) wait(0);
1630Sstevel@tonic-gate
1640Sstevel@tonic-gate return (ret);
1650Sstevel@tonic-gate }
1660Sstevel@tonic-gate
1670Sstevel@tonic-gate /*
1680Sstevel@tonic-gate * Handle the -R option and sets 'root' to be the absolute path of the
1690Sstevel@tonic-gate * relocatable root. This is useful when the user specifies '-R ../../foo'.
1700Sstevel@tonic-gate *
1710Sstevel@tonic-gate * Return code is whether or not the location spec'd by the -R flag is a
1720Sstevel@tonic-gate * directory or not.
1730Sstevel@tonic-gate */
1740Sstevel@tonic-gate static int
sanitize_reloc_root(char * root,size_t bufsize)1750Sstevel@tonic-gate sanitize_reloc_root(char *root, size_t bufsize)
1760Sstevel@tonic-gate {
1770Sstevel@tonic-gate char pwd[PATH_MAX];
1780Sstevel@tonic-gate
1790Sstevel@tonic-gate /*
1800Sstevel@tonic-gate * First, save the current directory and go to the location
1810Sstevel@tonic-gate * specified with the -R option.
1820Sstevel@tonic-gate */
1830Sstevel@tonic-gate (void) getcwd(pwd, sizeof (pwd));
1840Sstevel@tonic-gate if (chdir(root) < 0) {
1850Sstevel@tonic-gate /* Failed to change directory, something is wrong.... */
1860Sstevel@tonic-gate perror(root);
1870Sstevel@tonic-gate return (0);
1880Sstevel@tonic-gate }
1890Sstevel@tonic-gate
1900Sstevel@tonic-gate /*
1910Sstevel@tonic-gate * Save the absolute path of the relocatable root directory.
1920Sstevel@tonic-gate */
1930Sstevel@tonic-gate (void) getcwd(root, bufsize);
1940Sstevel@tonic-gate
1950Sstevel@tonic-gate /*
1960Sstevel@tonic-gate * Now, go back to where we started, necessary for picking up a rules
1970Sstevel@tonic-gate * file.
1980Sstevel@tonic-gate */
1990Sstevel@tonic-gate if (chdir(pwd) < 0) {
2000Sstevel@tonic-gate /* Failed to change directory, something is wrong.... */
2010Sstevel@tonic-gate perror(root);
2020Sstevel@tonic-gate return (0);
2030Sstevel@tonic-gate }
2040Sstevel@tonic-gate
2050Sstevel@tonic-gate /*
2060Sstevel@tonic-gate * Make sure the path returned does not have a trailing /. This
2070Sstevel@tonic-gate * can only happen when the entire pathname is "/".
2080Sstevel@tonic-gate */
2090Sstevel@tonic-gate if (strcmp(root, "/") == 0)
2100Sstevel@tonic-gate root[0] = '\0';
2110Sstevel@tonic-gate
2120Sstevel@tonic-gate /*
2130Sstevel@tonic-gate * Since the earlier chdir() succeeded, return success.
2140Sstevel@tonic-gate */
2150Sstevel@tonic-gate return (1);
2160Sstevel@tonic-gate }
2170Sstevel@tonic-gate
2180Sstevel@tonic-gate /*
2190Sstevel@tonic-gate * This is the worker bee which creates the manifest based upon the command
2200Sstevel@tonic-gate * line options supplied by the user.
2210Sstevel@tonic-gate *
2220Sstevel@tonic-gate * NOTE: create_manifest() eventually outputs data to a pipe, which is read in
2230Sstevel@tonic-gate * by the child process. The child process is running output_manifest(), which
2240Sstevel@tonic-gate * is responsible for generating sorted output.
2250Sstevel@tonic-gate */
2260Sstevel@tonic-gate static int
create_manifest_rule(char * reloc_root,FILE * rule_fp)2270Sstevel@tonic-gate create_manifest_rule(char *reloc_root, FILE *rule_fp)
2280Sstevel@tonic-gate {
2290Sstevel@tonic-gate struct rule *root;
2300Sstevel@tonic-gate int ret_status = EXIT;
2310Sstevel@tonic-gate uint_t flags;
2320Sstevel@tonic-gate
2330Sstevel@tonic-gate if (compute_chksum)
2340Sstevel@tonic-gate flags = ATTR_CONTENTS;
2350Sstevel@tonic-gate else
2360Sstevel@tonic-gate flags = 0;
2370Sstevel@tonic-gate ret_status = read_rules(rule_fp, reloc_root, flags, 1);
2380Sstevel@tonic-gate
2390Sstevel@tonic-gate /* Loop through every single subtree */
2400Sstevel@tonic-gate for (root = get_first_subtree(); root != NULL;
2410Sstevel@tonic-gate root = get_next_subtree(root)) {
2420Sstevel@tonic-gate
2430Sstevel@tonic-gate /*
2440Sstevel@tonic-gate * Check to see if this subtree should have contents
2450Sstevel@tonic-gate * checking turned on or off.
2460Sstevel@tonic-gate *
2470Sstevel@tonic-gate * NOTE: The 'compute_chksum' and 'parent_vfs'
2480Sstevel@tonic-gate * are a necessary hack: the variables are used in
2490Sstevel@tonic-gate * walker(), both directly and indirectly. Since
2500Sstevel@tonic-gate * the parameters to walker() are defined by nftw(),
2510Sstevel@tonic-gate * the globals are really a backdoor mechanism.
2520Sstevel@tonic-gate */
2539298SWilliam.Young@Sun.COM ret_status = statvfs64(root->subtree, &parent_vfs);
2540Sstevel@tonic-gate if (ret_status < 0) {
2550Sstevel@tonic-gate perror(root->subtree);
2560Sstevel@tonic-gate continue;
2570Sstevel@tonic-gate }
2580Sstevel@tonic-gate
2590Sstevel@tonic-gate /*
26012369SJohn.Zolnowsky@Sun.COM * Walk the subtree and invoke the callback function walker()
26112369SJohn.Zolnowsky@Sun.COM * Use FTW_ANYERR to get FTW_NS and FTW_DNR entries *and*
26212369SJohn.Zolnowsky@Sun.COM * to continue past those errors.
2630Sstevel@tonic-gate */
2640Sstevel@tonic-gate subtree_root = root;
26512369SJohn.Zolnowsky@Sun.COM (void) nftw64(root->subtree, &walker, 20, FTW_PHYS|FTW_ANYERR);
2660Sstevel@tonic-gate
2670Sstevel@tonic-gate /*
2680Sstevel@tonic-gate * Ugly but necessary:
2690Sstevel@tonic-gate *
2700Sstevel@tonic-gate * walker() must return 0, or the tree walk will stop,
2710Sstevel@tonic-gate * so warning flags must be set through a global.
2720Sstevel@tonic-gate */
2730Sstevel@tonic-gate if (eval_err == WARNING_EXIT)
2740Sstevel@tonic-gate ret_status = WARNING_EXIT;
2750Sstevel@tonic-gate
2760Sstevel@tonic-gate }
2770Sstevel@tonic-gate return (ret_status);
2780Sstevel@tonic-gate }
2790Sstevel@tonic-gate
2800Sstevel@tonic-gate static int
create_manifest_filelist(char ** argv,char * reloc_root)2810Sstevel@tonic-gate create_manifest_filelist(char **argv, char *reloc_root)
2820Sstevel@tonic-gate {
2830Sstevel@tonic-gate int ret_status = EXIT;
2840Sstevel@tonic-gate char input_fname[PATH_MAX];
2850Sstevel@tonic-gate
2860Sstevel@tonic-gate while (read_filelist(reloc_root, argv,
2870Sstevel@tonic-gate input_fname, sizeof (input_fname)) != -1) {
2880Sstevel@tonic-gate
2890Sstevel@tonic-gate struct stat64 stat_buf;
2900Sstevel@tonic-gate int ret;
2910Sstevel@tonic-gate
2920Sstevel@tonic-gate ret = lstat64(input_fname, &stat_buf);
2930Sstevel@tonic-gate if (ret < 0) {
2940Sstevel@tonic-gate ret_status = WARNING_EXIT;
2950Sstevel@tonic-gate perror(input_fname);
2960Sstevel@tonic-gate } else {
297*13116SJan.Parcel@Sun.COM ret = eval_file(input_fname, &stat_buf, NULL);
2980Sstevel@tonic-gate
2990Sstevel@tonic-gate if (ret == WARNING_EXIT)
3000Sstevel@tonic-gate ret_status = WARNING_EXIT;
3010Sstevel@tonic-gate }
3020Sstevel@tonic-gate }
3030Sstevel@tonic-gate
3040Sstevel@tonic-gate return (ret_status);
3050Sstevel@tonic-gate }
3060Sstevel@tonic-gate
3070Sstevel@tonic-gate /*
3080Sstevel@tonic-gate * output_manifest() the child process. It reads in the output from
3090Sstevel@tonic-gate * create_manifest() and sorts it.
3100Sstevel@tonic-gate */
3110Sstevel@tonic-gate static void
output_manifest(void)3120Sstevel@tonic-gate output_manifest(void)
3130Sstevel@tonic-gate {
3140Sstevel@tonic-gate char *env[] = {"LC_CTYPE=C", "LC_COLLATE=C", "LC_NUMERIC=C", NULL};
3150Sstevel@tonic-gate time_t time_val;
3160Sstevel@tonic-gate struct tm *tm;
3170Sstevel@tonic-gate char time_buf[1024];
3180Sstevel@tonic-gate
3190Sstevel@tonic-gate (void) printf("%s", MANIFEST_VER);
3200Sstevel@tonic-gate time_val = time((time_t)0);
3210Sstevel@tonic-gate tm = localtime(&time_val);
3220Sstevel@tonic-gate (void) strftime(time_buf, sizeof (time_buf), "%A, %B %d, %Y (%T)", tm);
3230Sstevel@tonic-gate (void) printf("! %s\n", time_buf);
3240Sstevel@tonic-gate (void) printf("%s", FORMAT_STR);
3250Sstevel@tonic-gate (void) fflush(stdout);
3260Sstevel@tonic-gate /*
3270Sstevel@tonic-gate * Simply run sort and read from the the current stdin, which is really
3280Sstevel@tonic-gate * the output of create_manifest().
3290Sstevel@tonic-gate * Also, make sure the output is unique, since a given file may be
3300Sstevel@tonic-gate * included by several stanzas.
3310Sstevel@tonic-gate */
3322813Srm88369 if (execle("/usr/bin/sort", "sort", "-u", NULL, env) < 0) {
3330Sstevel@tonic-gate perror("");
3340Sstevel@tonic-gate exit(FATAL_EXIT);
3350Sstevel@tonic-gate }
3360Sstevel@tonic-gate
3370Sstevel@tonic-gate /*NOTREACHED*/
3380Sstevel@tonic-gate }
3390Sstevel@tonic-gate
3400Sstevel@tonic-gate /*
3410Sstevel@tonic-gate * Callback function for nftw()
3420Sstevel@tonic-gate */
3430Sstevel@tonic-gate static int
walker(const char * name,const struct stat64 * sp,int type,struct FTW * ftwx)3440Sstevel@tonic-gate walker(const char *name, const struct stat64 *sp, int type, struct FTW *ftwx)
3450Sstevel@tonic-gate {
3469298SWilliam.Young@Sun.COM int ret;
3479298SWilliam.Young@Sun.COM struct statvfs64 path_vfs;
3489298SWilliam.Young@Sun.COM boolean_t dir_flag = B_FALSE;
3499298SWilliam.Young@Sun.COM struct rule *rule;
3500Sstevel@tonic-gate
3510Sstevel@tonic-gate switch (type) {
3520Sstevel@tonic-gate case FTW_F: /* file */
3530Sstevel@tonic-gate rule = check_rules(name, 'F');
3540Sstevel@tonic-gate if (rule != NULL) {
3550Sstevel@tonic-gate if (rule->attr_list & ATTR_CONTENTS)
3560Sstevel@tonic-gate compute_chksum = 1;
3570Sstevel@tonic-gate else
3580Sstevel@tonic-gate compute_chksum = 0;
3590Sstevel@tonic-gate }
3600Sstevel@tonic-gate break;
36112369SJohn.Zolnowsky@Sun.COM case FTW_SL: /* symbolic link, FTW_PHYS */
36212369SJohn.Zolnowsky@Sun.COM case FTW_SLN: /* symbolic link, ~FTW_PHYS */
3630Sstevel@tonic-gate break;
36412369SJohn.Zolnowsky@Sun.COM case FTW_DP: /* end of directory, FTW_DEPTH */
36512369SJohn.Zolnowsky@Sun.COM case FTW_D: /* enter directory, ~FTW_DEPTH */
3660Sstevel@tonic-gate dir_flag = B_TRUE;
3679298SWilliam.Young@Sun.COM ret = statvfs64(name, &path_vfs);
3680Sstevel@tonic-gate if (ret < 0)
3690Sstevel@tonic-gate eval_err = WARNING_EXIT;
3700Sstevel@tonic-gate break;
37112369SJohn.Zolnowsky@Sun.COM case FTW_NS: /* unstatable file */
37212369SJohn.Zolnowsky@Sun.COM (void) fprintf(stderr, UNKNOWN_FILE, name);
37312369SJohn.Zolnowsky@Sun.COM eval_err = WARNING_EXIT;
37412369SJohn.Zolnowsky@Sun.COM return (0);
37512369SJohn.Zolnowsky@Sun.COM case FTW_DNR: /* unreadable directory */
37612369SJohn.Zolnowsky@Sun.COM (void) fprintf(stderr, CANTLIST_DIR, name);
37712369SJohn.Zolnowsky@Sun.COM eval_err = WARNING_EXIT;
37812369SJohn.Zolnowsky@Sun.COM return (0);
3790Sstevel@tonic-gate default:
38012369SJohn.Zolnowsky@Sun.COM (void) fprintf(stderr, INTERNAL_ERR, name);
3810Sstevel@tonic-gate eval_err = WARNING_EXIT;
38212369SJohn.Zolnowsky@Sun.COM return (0);
3830Sstevel@tonic-gate }
3840Sstevel@tonic-gate
3850Sstevel@tonic-gate /* This is the function which really processes the file */
386*13116SJan.Parcel@Sun.COM ret = eval_file(name, sp, ftwx);
3870Sstevel@tonic-gate
3880Sstevel@tonic-gate /*
3890Sstevel@tonic-gate * Since the parameters to walker() are constrained by nftw(),
3900Sstevel@tonic-gate * need to use a global to reflect a WARNING. Sigh.
3910Sstevel@tonic-gate */
3920Sstevel@tonic-gate if (ret == WARNING_EXIT)
3930Sstevel@tonic-gate eval_err = WARNING_EXIT;
3940Sstevel@tonic-gate
3950Sstevel@tonic-gate /*
3960Sstevel@tonic-gate * This is a case of a directory which crosses into a mounted
3970Sstevel@tonic-gate * filesystem of a different type, e.g., UFS -> NFS.
3980Sstevel@tonic-gate * BART should not walk the new filesystem (by specification), so
3990Sstevel@tonic-gate * set this consolidation-private flag so the rest of the subtree
4000Sstevel@tonic-gate * under this directory is not waled.
4010Sstevel@tonic-gate */
4020Sstevel@tonic-gate if (dir_flag &&
4030Sstevel@tonic-gate (strcmp(parent_vfs.f_basetype, path_vfs.f_basetype) != 0))
4040Sstevel@tonic-gate ftwx->quit = FTW_PRUNE;
4050Sstevel@tonic-gate
4060Sstevel@tonic-gate return (0);
4070Sstevel@tonic-gate }
4080Sstevel@tonic-gate
4090Sstevel@tonic-gate /*
4100Sstevel@tonic-gate * This file does the per-file evaluation and is run to generate every entry
4110Sstevel@tonic-gate * in the manifest.
4120Sstevel@tonic-gate *
4130Sstevel@tonic-gate * All output is written to a pipe which is read by the child process,
4140Sstevel@tonic-gate * which is running output_manifest().
4150Sstevel@tonic-gate */
4160Sstevel@tonic-gate static int
eval_file(const char * fname,const struct stat64 * statb,struct FTW * ftwx)417*13116SJan.Parcel@Sun.COM eval_file(const char *fname, const struct stat64 *statb, struct FTW *ftwx)
4180Sstevel@tonic-gate {
419*13116SJan.Parcel@Sun.COM int fd, ret, err_code, i, result;
4209298SWilliam.Young@Sun.COM char last_field[PATH_MAX], ftype, *acl_str;
4219298SWilliam.Young@Sun.COM char *quoted_name;
4220Sstevel@tonic-gate
4230Sstevel@tonic-gate err_code = EXIT;
4240Sstevel@tonic-gate
4250Sstevel@tonic-gate switch (statb->st_mode & S_IFMT) {
4260Sstevel@tonic-gate /* Regular file */
4270Sstevel@tonic-gate case S_IFREG: ftype = 'F'; break;
4280Sstevel@tonic-gate
4290Sstevel@tonic-gate /* Directory */
4300Sstevel@tonic-gate case S_IFDIR: ftype = 'D'; break;
4310Sstevel@tonic-gate
4320Sstevel@tonic-gate /* Block Device */
4330Sstevel@tonic-gate case S_IFBLK: ftype = 'B'; break;
4340Sstevel@tonic-gate
4350Sstevel@tonic-gate /* Character Device */
4360Sstevel@tonic-gate case S_IFCHR: ftype = 'C'; break;
4370Sstevel@tonic-gate
4380Sstevel@tonic-gate /* Named Pipe */
4390Sstevel@tonic-gate case S_IFIFO: ftype = 'P'; break;
4400Sstevel@tonic-gate
4410Sstevel@tonic-gate /* Socket */
4420Sstevel@tonic-gate case S_IFSOCK: ftype = 'S'; break;
4430Sstevel@tonic-gate
4440Sstevel@tonic-gate /* Door */
4450Sstevel@tonic-gate case S_IFDOOR: ftype = 'O'; break;
4460Sstevel@tonic-gate
4470Sstevel@tonic-gate /* Symbolic link */
4480Sstevel@tonic-gate case S_IFLNK: ftype = 'L'; break;
4490Sstevel@tonic-gate
4500Sstevel@tonic-gate default: ftype = '-'; break;
4510Sstevel@tonic-gate }
4520Sstevel@tonic-gate
4530Sstevel@tonic-gate /* First, make sure this file should be cataloged */
4540Sstevel@tonic-gate
4550Sstevel@tonic-gate if ((subtree_root != NULL) &&
456*13116SJan.Parcel@Sun.COM ((result = exclude_fname(fname, ftype, subtree_root)) !=
457*13116SJan.Parcel@Sun.COM NO_EXCLUDE)) {
458*13116SJan.Parcel@Sun.COM if ((result == EXCLUDE_PRUNE) && (ftwx != (struct FTW *)NULL))
459*13116SJan.Parcel@Sun.COM ftwx->quit = FTW_PRUNE;
4600Sstevel@tonic-gate return (err_code);
461*13116SJan.Parcel@Sun.COM }
4620Sstevel@tonic-gate for (i = 0; i < PATH_MAX; i++)
4630Sstevel@tonic-gate last_field[i] = '\0';
4640Sstevel@tonic-gate
4650Sstevel@tonic-gate /*
4660Sstevel@tonic-gate * Regular files, compute the MD5 checksum and put it into 'last_field'
4670Sstevel@tonic-gate * UNLESS instructed to ignore the checksums.
4680Sstevel@tonic-gate */
4690Sstevel@tonic-gate if (ftype == 'F') {
4700Sstevel@tonic-gate if (compute_chksum) {
4710Sstevel@tonic-gate fd = open(fname, O_RDONLY|O_LARGEFILE);
4720Sstevel@tonic-gate if (fd < 0) {
4730Sstevel@tonic-gate err_code = WARNING_EXIT;
4740Sstevel@tonic-gate perror(fname);
4750Sstevel@tonic-gate
4760Sstevel@tonic-gate /* default value since the computution failed */
4770Sstevel@tonic-gate (void) strcpy(last_field, "-");
4780Sstevel@tonic-gate } else {
4790Sstevel@tonic-gate if (generate_hash(fd, last_field) != 0) {
4800Sstevel@tonic-gate err_code = WARNING_EXIT;
4810Sstevel@tonic-gate (void) fprintf(stderr, CONTENTS_WARN,
4820Sstevel@tonic-gate fname);
4830Sstevel@tonic-gate (void) strcpy(last_field, "-");
4840Sstevel@tonic-gate }
4850Sstevel@tonic-gate }
4860Sstevel@tonic-gate (void) close(fd);
4870Sstevel@tonic-gate }
4880Sstevel@tonic-gate /* Instructed to ignore checksums, just put in a '-' */
4890Sstevel@tonic-gate else
4900Sstevel@tonic-gate (void) strcpy(last_field, "-");
4910Sstevel@tonic-gate }
4920Sstevel@tonic-gate
4930Sstevel@tonic-gate /*
4940Sstevel@tonic-gate * For symbolic links, put the destination of the symbolic link into
4950Sstevel@tonic-gate * 'last_field'
4960Sstevel@tonic-gate */
4970Sstevel@tonic-gate if (ftype == 'L') {
4980Sstevel@tonic-gate ret = readlink(fname, last_field, sizeof (last_field));
4990Sstevel@tonic-gate if (ret < 0) {
5000Sstevel@tonic-gate err_code = WARNING_EXIT;
5010Sstevel@tonic-gate perror(fname);
5020Sstevel@tonic-gate
5030Sstevel@tonic-gate /* default value since the computation failed */
5040Sstevel@tonic-gate (void) strcpy(last_field, "-");
5050Sstevel@tonic-gate }
5060Sstevel@tonic-gate else
5070Sstevel@tonic-gate (void) strlcpy(last_field,
5080Sstevel@tonic-gate sanitized_fname(last_field, B_FALSE),
5090Sstevel@tonic-gate sizeof (last_field));
5100Sstevel@tonic-gate
5110Sstevel@tonic-gate /*
5120Sstevel@tonic-gate * Boundary condition: possible for a symlink to point to
5130Sstevel@tonic-gate * nothing [ ln -s '' link_name ]. For this case, set the
5140Sstevel@tonic-gate * destination to "\000".
5150Sstevel@tonic-gate */
5160Sstevel@tonic-gate if (strlen(last_field) == 0)
5170Sstevel@tonic-gate (void) strcpy(last_field, "\\000");
5180Sstevel@tonic-gate }
5190Sstevel@tonic-gate
5200Sstevel@tonic-gate acl_str = get_acl_string(fname, statb, &err_code);
5210Sstevel@tonic-gate
5220Sstevel@tonic-gate /* Sanitize 'fname', so its in the proper format for the manifest */
5230Sstevel@tonic-gate quoted_name = sanitized_fname(fname, B_TRUE);
5240Sstevel@tonic-gate
5250Sstevel@tonic-gate /* Start to build the entry.... */
5260Sstevel@tonic-gate (void) printf("%s %c %d %o %s %x %d %d", quoted_name, ftype,
5270Sstevel@tonic-gate (int)statb->st_size, (int)statb->st_mode, acl_str,
5280Sstevel@tonic-gate (int)statb->st_mtime, (int)statb->st_uid, (int)statb->st_gid);
5290Sstevel@tonic-gate
5300Sstevel@tonic-gate /* Finish it off based upon whether or not it's a device node */
5312812Sjc144527 if ((ftype == 'B') || (ftype == 'C'))
5320Sstevel@tonic-gate (void) printf(" %x\n", (int)statb->st_rdev);
5330Sstevel@tonic-gate else if (strlen(last_field) > 0)
5340Sstevel@tonic-gate (void) printf(" %s\n", last_field);
5350Sstevel@tonic-gate else
5360Sstevel@tonic-gate (void) printf("\n");
5370Sstevel@tonic-gate
5380Sstevel@tonic-gate /* free the memory consumed */
5390Sstevel@tonic-gate free(acl_str);
5400Sstevel@tonic-gate free(quoted_name);
5410Sstevel@tonic-gate
5420Sstevel@tonic-gate return (err_code);
5430Sstevel@tonic-gate }
5440Sstevel@tonic-gate
5450Sstevel@tonic-gate /*
5460Sstevel@tonic-gate * When creating a manifest, make sure all '?', tabs, space, newline, '/'
5470Sstevel@tonic-gate * and '[' are all properly quoted. Convert them to a "\ooo" where the 'ooo'
5480Sstevel@tonic-gate * represents their octal value. For filesystem objects, as opposed to symlink
5490Sstevel@tonic-gate * targets, also canonicalize the pathname.
5500Sstevel@tonic-gate */
5510Sstevel@tonic-gate static char *
sanitized_fname(const char * fname,boolean_t canon_path)5520Sstevel@tonic-gate sanitized_fname(const char *fname, boolean_t canon_path)
5530Sstevel@tonic-gate {
5540Sstevel@tonic-gate const char *ip;
5550Sstevel@tonic-gate unsigned char ch;
5560Sstevel@tonic-gate char *op, *quoted_name;
5570Sstevel@tonic-gate
5580Sstevel@tonic-gate /* Initialize everything */
5590Sstevel@tonic-gate quoted_name = safe_calloc((4 * PATH_MAX) + 1);
5600Sstevel@tonic-gate ip = fname;
5610Sstevel@tonic-gate op = quoted_name;
5620Sstevel@tonic-gate
5630Sstevel@tonic-gate if (canon_path) {
5640Sstevel@tonic-gate /*
5650Sstevel@tonic-gate * In the case when a relocatable root was used, the relocatable
5660Sstevel@tonic-gate * root should *not* be part of the manifest.
5670Sstevel@tonic-gate */
5680Sstevel@tonic-gate ip += strlen(reloc_root);
5690Sstevel@tonic-gate
5700Sstevel@tonic-gate /*
5710Sstevel@tonic-gate * In the case when the '-I' option was used, make sure
5720Sstevel@tonic-gate * the quoted_name starts with a '/'.
5730Sstevel@tonic-gate */
5740Sstevel@tonic-gate if (*ip != '/')
5750Sstevel@tonic-gate *op++ = '/';
5760Sstevel@tonic-gate }
5770Sstevel@tonic-gate
5780Sstevel@tonic-gate /* Now walk through 'fname' and build the quoted string */
5790Sstevel@tonic-gate while ((ch = *ip++) != 0) {
5800Sstevel@tonic-gate switch (ch) {
5810Sstevel@tonic-gate /* Quote the following characters */
5820Sstevel@tonic-gate case ' ':
5830Sstevel@tonic-gate case '*':
5840Sstevel@tonic-gate case '\n':
5850Sstevel@tonic-gate case '?':
5860Sstevel@tonic-gate case '[':
5870Sstevel@tonic-gate case '\\':
5880Sstevel@tonic-gate case '\t':
5890Sstevel@tonic-gate op += sprintf(op, "\\%.3o", (unsigned char)ch);
5900Sstevel@tonic-gate break;
5910Sstevel@tonic-gate
5920Sstevel@tonic-gate /* Otherwise, simply append them */
5930Sstevel@tonic-gate default:
5940Sstevel@tonic-gate *op++ = ch;
5950Sstevel@tonic-gate break;
5960Sstevel@tonic-gate }
5970Sstevel@tonic-gate }
5980Sstevel@tonic-gate
5990Sstevel@tonic-gate *op = 0;
6000Sstevel@tonic-gate
6010Sstevel@tonic-gate return (quoted_name);
6020Sstevel@tonic-gate }
6030Sstevel@tonic-gate
6040Sstevel@tonic-gate /*
6050Sstevel@tonic-gate * Function responsible for generating the ACL information for a given
6060Sstevel@tonic-gate * file. Note, the string is put into buffer malloc'd by this function.
6079298SWilliam.Young@Sun.COM * It's the responsibility of the caller to free the buffer. This function
6089298SWilliam.Young@Sun.COM * should never return a NULL pointer.
6090Sstevel@tonic-gate */
6100Sstevel@tonic-gate static char *
get_acl_string(const char * fname,const struct stat64 * statb,int * err_code)6110Sstevel@tonic-gate get_acl_string(const char *fname, const struct stat64 *statb, int *err_code)
6120Sstevel@tonic-gate {
613789Sahrens acl_t *aclp;
614789Sahrens char *acltext;
615789Sahrens int error;
6160Sstevel@tonic-gate
6170Sstevel@tonic-gate if (S_ISLNK(statb->st_mode)) {
6180Sstevel@tonic-gate return (safe_strdup("-"));
6190Sstevel@tonic-gate }
6200Sstevel@tonic-gate
621789Sahrens /*
622789Sahrens * Include trivial acl's
623789Sahrens */
624789Sahrens error = acl_get(fname, 0, &aclp);
6250Sstevel@tonic-gate
626789Sahrens if (error != 0) {
6270Sstevel@tonic-gate *err_code = WARNING_EXIT;
628789Sahrens (void) fprintf(stderr, "%s: %s\n", fname, acl_strerror(error));
6290Sstevel@tonic-gate return (safe_strdup("-"));
630789Sahrens } else {
6311420Smarks acltext = acl_totext(aclp, 0);
632789Sahrens acl_free(aclp);
6339298SWilliam.Young@Sun.COM if (acltext == NULL)
6349298SWilliam.Young@Sun.COM return (safe_strdup("-"));
6359298SWilliam.Young@Sun.COM else
6369298SWilliam.Young@Sun.COM return (acltext);
6370Sstevel@tonic-gate }
6380Sstevel@tonic-gate }
6390Sstevel@tonic-gate
6400Sstevel@tonic-gate
6410Sstevel@tonic-gate /*
6420Sstevel@tonic-gate *
6430Sstevel@tonic-gate * description: This routine reads stdin in BUF_SIZE chunks, uses the bits
6440Sstevel@tonic-gate * to update the md5 hash buffer, and outputs the chunks
6450Sstevel@tonic-gate * to stdout. When stdin is exhausted, the hash is computed,
6460Sstevel@tonic-gate * converted to a hexadecimal string, and returned.
6470Sstevel@tonic-gate *
6480Sstevel@tonic-gate * returns: The md5 hash of stdin, or NULL if unsuccessful for any reason.
6490Sstevel@tonic-gate */
6500Sstevel@tonic-gate static int
generate_hash(int fdin,char * hash_str)6510Sstevel@tonic-gate generate_hash(int fdin, char *hash_str)
6520Sstevel@tonic-gate {
6530Sstevel@tonic-gate unsigned char buf[BUF_SIZE];
6540Sstevel@tonic-gate unsigned char hash[MD5_DIGEST_LENGTH];
6550Sstevel@tonic-gate int i, amtread;
6560Sstevel@tonic-gate MD5_CTX ctx;
6570Sstevel@tonic-gate
6580Sstevel@tonic-gate MD5Init(&ctx);
6590Sstevel@tonic-gate
6600Sstevel@tonic-gate for (;;) {
6610Sstevel@tonic-gate amtread = read(fdin, buf, sizeof (buf));
6620Sstevel@tonic-gate if (amtread == 0)
6630Sstevel@tonic-gate break;
6640Sstevel@tonic-gate if (amtread < 0)
6650Sstevel@tonic-gate return (1);
6660Sstevel@tonic-gate
6670Sstevel@tonic-gate /* got some data. Now update hash */
6680Sstevel@tonic-gate MD5Update(&ctx, buf, amtread);
6690Sstevel@tonic-gate }
6700Sstevel@tonic-gate
6710Sstevel@tonic-gate /* done passing through data, calculate hash */
6720Sstevel@tonic-gate MD5Final(hash, &ctx);
6730Sstevel@tonic-gate
6740Sstevel@tonic-gate for (i = 0; i < MD5_DIGEST_LENGTH; i++)
6750Sstevel@tonic-gate (void) sprintf(hash_str + (i*2), "%2.2x", hash[i]);
6760Sstevel@tonic-gate
6770Sstevel@tonic-gate return (0);
6780Sstevel@tonic-gate }
6790Sstevel@tonic-gate
6800Sstevel@tonic-gate /*
6810Sstevel@tonic-gate * Used by 'bart create' with the '-I' option. Return each entry into a 'buf'
6820Sstevel@tonic-gate * with the appropriate exit code: '0' for success and '-1' for failure.
6830Sstevel@tonic-gate */
6840Sstevel@tonic-gate static int
read_filelist(char * reloc_root,char ** argv,char * buf,size_t bufsize)6850Sstevel@tonic-gate read_filelist(char *reloc_root, char **argv, char *buf, size_t bufsize)
6860Sstevel@tonic-gate {
6870Sstevel@tonic-gate static int argv_index = -1;
6880Sstevel@tonic-gate static boolean_t read_stdinput = B_FALSE;
6890Sstevel@tonic-gate char temp_buf[PATH_MAX];
6900Sstevel@tonic-gate char *cp;
6910Sstevel@tonic-gate
6920Sstevel@tonic-gate /*
6930Sstevel@tonic-gate * INITIALIZATION:
6940Sstevel@tonic-gate * Setup this code so it knows whether or not to read sdtin.
6950Sstevel@tonic-gate * Also, if reading from argv, setup the index, "argv_index"
6960Sstevel@tonic-gate */
6970Sstevel@tonic-gate if (argv_index == -1) {
6980Sstevel@tonic-gate argv_index = 0;
6990Sstevel@tonic-gate
7000Sstevel@tonic-gate /* In this case, no args after '-I', so read stdin */
7010Sstevel@tonic-gate if (argv[0] == NULL)
7020Sstevel@tonic-gate read_stdinput = B_TRUE;
7030Sstevel@tonic-gate }
7040Sstevel@tonic-gate
7050Sstevel@tonic-gate buf[0] = '\0';
7060Sstevel@tonic-gate
7070Sstevel@tonic-gate if (read_stdinput) {
7080Sstevel@tonic-gate if (fgets(temp_buf, PATH_MAX, stdin) == NULL)
7090Sstevel@tonic-gate return (-1);
7100Sstevel@tonic-gate cp = strtok(temp_buf, "\n");
7110Sstevel@tonic-gate } else {
7120Sstevel@tonic-gate cp = argv[argv_index++];
7130Sstevel@tonic-gate }
7140Sstevel@tonic-gate
7150Sstevel@tonic-gate if (cp == NULL)
7160Sstevel@tonic-gate return (-1);
7170Sstevel@tonic-gate
7180Sstevel@tonic-gate /*
7190Sstevel@tonic-gate * Unlike similar code elsewhere, avoid adding a leading
7200Sstevel@tonic-gate * slash for relative pathnames.
7210Sstevel@tonic-gate */
7220Sstevel@tonic-gate (void) snprintf(buf, bufsize,
7230Sstevel@tonic-gate (reloc_root[0] == '\0' || cp[0] == '/') ? "%s%s" : "%s/%s",
7240Sstevel@tonic-gate reloc_root, cp);
7250Sstevel@tonic-gate
7260Sstevel@tonic-gate return (0);
7270Sstevel@tonic-gate }
728