17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5f48205beScasper * Common Development and Distribution License (the "License"). 6f48205beScasper * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 217c478bd9Sstevel@tonic-gate /* 22f48205beScasper * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 237c478bd9Sstevel@tonic-gate * Use is subject to license terms. 247c478bd9Sstevel@tonic-gate */ 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate /* 277c478bd9Sstevel@tonic-gate * module: 287c478bd9Sstevel@tonic-gate * main.c 297c478bd9Sstevel@tonic-gate * 307c478bd9Sstevel@tonic-gate * purpose: 317c478bd9Sstevel@tonic-gate * argument handling and top level dispatch 327c478bd9Sstevel@tonic-gate * 337c478bd9Sstevel@tonic-gate * contents: 347c478bd9Sstevel@tonic-gate * main argument handling and main loop 357c478bd9Sstevel@tonic-gate * usage (static) print out usage message 367c478bd9Sstevel@tonic-gate * confirm prompt the user for a confirmation and get it 377c478bd9Sstevel@tonic-gate * nomem fatal error handler for malloc failures 387c478bd9Sstevel@tonic-gate * findfiles (static) locate our baseline and rules files 397c478bd9Sstevel@tonic-gate * cleanup (static) unlock baseline and delete temp file 407c478bd9Sstevel@tonic-gate * check_access (static) do we have adequate access to a file/directory 417c478bd9Sstevel@tonic-gate * whoami (static) get uid/gid/umask 427c478bd9Sstevel@tonic-gate */ 437c478bd9Sstevel@tonic-gate 447c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 457c478bd9Sstevel@tonic-gate 467c478bd9Sstevel@tonic-gate #include <unistd.h> 477c478bd9Sstevel@tonic-gate #include <stdlib.h> 487c478bd9Sstevel@tonic-gate #include <fcntl.h> 497c478bd9Sstevel@tonic-gate #include <stdio.h> 507c478bd9Sstevel@tonic-gate #include <string.h> 517c478bd9Sstevel@tonic-gate #include <ctype.h> 527c478bd9Sstevel@tonic-gate #include <errno.h> 537c478bd9Sstevel@tonic-gate #include <sys/stat.h> 547c478bd9Sstevel@tonic-gate 557c478bd9Sstevel@tonic-gate #include "filesync.h" 567c478bd9Sstevel@tonic-gate #include "database.h" 577c478bd9Sstevel@tonic-gate #include "messages.h" 587c478bd9Sstevel@tonic-gate #include "debug.h" 597c478bd9Sstevel@tonic-gate 607c478bd9Sstevel@tonic-gate /* 617c478bd9Sstevel@tonic-gate * local routines in this module: 627c478bd9Sstevel@tonic-gate */ 637c478bd9Sstevel@tonic-gate static errmask_t findfiles(); /* find rule and baseline files */ 647c478bd9Sstevel@tonic-gate static void cleanup(int); /* cleanup locks and temps */ 657c478bd9Sstevel@tonic-gate static errmask_t check_access(char *, int *); /* check access to file */ 667c478bd9Sstevel@tonic-gate static void whoami(); /* gather information about me */ 677c478bd9Sstevel@tonic-gate static void usage(void); /* general usage */ 687c478bd9Sstevel@tonic-gate 697c478bd9Sstevel@tonic-gate 707c478bd9Sstevel@tonic-gate /* 717c478bd9Sstevel@tonic-gate * globals exported to the rest of the program 727c478bd9Sstevel@tonic-gate */ 737c478bd9Sstevel@tonic-gate bool_t opt_mtime; /* preserve modification times on propagations */ 747c478bd9Sstevel@tonic-gate bool_t opt_notouch; /* don't actually make any changes */ 757c478bd9Sstevel@tonic-gate bool_t opt_quiet; /* disable reconciliation command output */ 767c478bd9Sstevel@tonic-gate bool_t opt_verbose; /* enable analysis descriptions */ 777c478bd9Sstevel@tonic-gate side_t opt_force; /* designated winner for conflicts */ 787c478bd9Sstevel@tonic-gate side_t opt_oneway; /* one way only propagation */ 797c478bd9Sstevel@tonic-gate side_t opt_onesided; /* permit one-sided evaluation */ 807c478bd9Sstevel@tonic-gate bool_t opt_everything; /* everything must agree (modes/uid/gid) */ 817c478bd9Sstevel@tonic-gate bool_t opt_yes; /* pre-confirm massive deletions are OK */ 827c478bd9Sstevel@tonic-gate bool_t opt_acls; /* always scan for acls on all files */ 837c478bd9Sstevel@tonic-gate bool_t opt_errors; /* simulate errors on specified files */ 847c478bd9Sstevel@tonic-gate bool_t opt_halt; /* halt on propagation errors */ 857c478bd9Sstevel@tonic-gate dbgmask_t opt_debug; /* debug mask */ 867c478bd9Sstevel@tonic-gate 877c478bd9Sstevel@tonic-gate uid_t my_uid; /* default UID for files I create */ 887c478bd9Sstevel@tonic-gate gid_t my_gid; /* default GID for files I create */ 897c478bd9Sstevel@tonic-gate 907c478bd9Sstevel@tonic-gate static char *file_rules; /* name of rules file */ 917c478bd9Sstevel@tonic-gate static char *file_base; /* name of baseline file */ 927c478bd9Sstevel@tonic-gate 937c478bd9Sstevel@tonic-gate static int new_baseline; /* are we creating a new baseline */ 947c478bd9Sstevel@tonic-gate static int new_rules; /* are we creating a new rules file */ 957c478bd9Sstevel@tonic-gate static int my_umask; /* default UMASK for files I create */ 967c478bd9Sstevel@tonic-gate static int lockfd; /* file descriptor for locking baseline */ 977c478bd9Sstevel@tonic-gate 987c478bd9Sstevel@tonic-gate static char *rlist[MAX_RLIST]; 997c478bd9Sstevel@tonic-gate static int num_restrs = 0; 1007c478bd9Sstevel@tonic-gate 1017c478bd9Sstevel@tonic-gate /* 1027c478bd9Sstevel@tonic-gate * routine: 1037c478bd9Sstevel@tonic-gate * main 1047c478bd9Sstevel@tonic-gate * 1057c478bd9Sstevel@tonic-gate * purpose: 1067c478bd9Sstevel@tonic-gate * argument processing and primary dispatch 1077c478bd9Sstevel@tonic-gate * 1087c478bd9Sstevel@tonic-gate * returns: 1097c478bd9Sstevel@tonic-gate * error codes per filesync.1 (ERR_* in filesync.h) 1107c478bd9Sstevel@tonic-gate * 1117c478bd9Sstevel@tonic-gate * notes: 1127c478bd9Sstevel@tonic-gate * read filesync.1 in order to understand the argument processing 1137c478bd9Sstevel@tonic-gate * 1147c478bd9Sstevel@tonic-gate * most of the command line options just set some opt_ global 1157c478bd9Sstevel@tonic-gate * variable that is later looked at by the code that actually 1167c478bd9Sstevel@tonic-gate * implements the features. Only file names are really processed 1177c478bd9Sstevel@tonic-gate * in this routine. 1187c478bd9Sstevel@tonic-gate */ 1193aceb801Sjwahlig int 1207c478bd9Sstevel@tonic-gate main(int argc, char **argv) 1217c478bd9Sstevel@tonic-gate { int i; 1227c478bd9Sstevel@tonic-gate int c; 1237c478bd9Sstevel@tonic-gate errmask_t errs = ERR_OK; 1247c478bd9Sstevel@tonic-gate int do_prune = 0; 1257c478bd9Sstevel@tonic-gate char *srcname = 0; 1267c478bd9Sstevel@tonic-gate char *dstname = 0; 1277c478bd9Sstevel@tonic-gate struct base *bp; 1287c478bd9Sstevel@tonic-gate 1297c478bd9Sstevel@tonic-gate /* keep the error messages simple */ 1307c478bd9Sstevel@tonic-gate argv[0] = "filesync"; 1317c478bd9Sstevel@tonic-gate 1327c478bd9Sstevel@tonic-gate /* gather together all of the options */ 1337c478bd9Sstevel@tonic-gate while ((c = getopt(argc, argv, "AaehmnqvyD:E:r:s:d:f:o:")) != EOF) 1347c478bd9Sstevel@tonic-gate switch (c) { 1357c478bd9Sstevel@tonic-gate case 'a': /* always scan for acls */ 1367c478bd9Sstevel@tonic-gate opt_acls = TRUE; 1377c478bd9Sstevel@tonic-gate break; 1387c478bd9Sstevel@tonic-gate case 'e': /* everything agrees */ 1397c478bd9Sstevel@tonic-gate opt_everything = TRUE; 1407c478bd9Sstevel@tonic-gate break; 1417c478bd9Sstevel@tonic-gate case 'h': /* halt on error */ 1427c478bd9Sstevel@tonic-gate opt_halt = TRUE; 1437c478bd9Sstevel@tonic-gate break; 1447c478bd9Sstevel@tonic-gate case 'm': /* preserve modtimes */ 1457c478bd9Sstevel@tonic-gate opt_mtime = TRUE; 1467c478bd9Sstevel@tonic-gate break; 1477c478bd9Sstevel@tonic-gate case 'n': /* notouch */ 1487c478bd9Sstevel@tonic-gate opt_notouch = TRUE; 1497c478bd9Sstevel@tonic-gate break; 1507c478bd9Sstevel@tonic-gate case 'q': /* quiet */ 1517c478bd9Sstevel@tonic-gate opt_quiet = TRUE; 1527c478bd9Sstevel@tonic-gate break; 1537c478bd9Sstevel@tonic-gate case 'v': /* verbose */ 1547c478bd9Sstevel@tonic-gate opt_verbose = TRUE; 1557c478bd9Sstevel@tonic-gate break; 1567c478bd9Sstevel@tonic-gate case 'y': /* yes */ 1577c478bd9Sstevel@tonic-gate opt_yes = TRUE; 1587c478bd9Sstevel@tonic-gate break; 1597c478bd9Sstevel@tonic-gate case 'D': /* debug options */ 1607c478bd9Sstevel@tonic-gate if (!isdigit(optarg[0])) { 1617c478bd9Sstevel@tonic-gate dbg_usage(); 1627c478bd9Sstevel@tonic-gate exit(ERR_INVAL); 1637c478bd9Sstevel@tonic-gate } 1647c478bd9Sstevel@tonic-gate opt_debug |= strtol(optarg, (char **)NULL, 0); 1657c478bd9Sstevel@tonic-gate break; 1667c478bd9Sstevel@tonic-gate 1677c478bd9Sstevel@tonic-gate case 'E': /* error simulation */ 1687c478bd9Sstevel@tonic-gate if (dbg_set_error(optarg)) { 1697c478bd9Sstevel@tonic-gate err_usage(); 1707c478bd9Sstevel@tonic-gate exit(ERR_INVAL); 1717c478bd9Sstevel@tonic-gate } 1727c478bd9Sstevel@tonic-gate opt_errors = TRUE; 1737c478bd9Sstevel@tonic-gate break; 1747c478bd9Sstevel@tonic-gate 1757c478bd9Sstevel@tonic-gate case 'f': /* force conflict resolution */ 1767c478bd9Sstevel@tonic-gate switch (optarg[0]) { 1777c478bd9Sstevel@tonic-gate case 's': 1787c478bd9Sstevel@tonic-gate opt_force = OPT_SRC; 1797c478bd9Sstevel@tonic-gate break; 1807c478bd9Sstevel@tonic-gate case 'd': 1817c478bd9Sstevel@tonic-gate opt_force = OPT_DST; 1827c478bd9Sstevel@tonic-gate break; 1837c478bd9Sstevel@tonic-gate case 'o': 1847c478bd9Sstevel@tonic-gate opt_force = OPT_OLD; 1857c478bd9Sstevel@tonic-gate break; 1867c478bd9Sstevel@tonic-gate case 'n': 1877c478bd9Sstevel@tonic-gate opt_force = OPT_NEW; 1887c478bd9Sstevel@tonic-gate break; 1897c478bd9Sstevel@tonic-gate default: 1907c478bd9Sstevel@tonic-gate fprintf(stderr, 1917c478bd9Sstevel@tonic-gate gettext(ERR_badopt), 1927c478bd9Sstevel@tonic-gate c, optarg); 1937c478bd9Sstevel@tonic-gate errs |= ERR_INVAL; 1947c478bd9Sstevel@tonic-gate break; 1957c478bd9Sstevel@tonic-gate } 1967c478bd9Sstevel@tonic-gate break; 1977c478bd9Sstevel@tonic-gate 1987c478bd9Sstevel@tonic-gate case 'o': /* one way propagation */ 1997c478bd9Sstevel@tonic-gate switch (optarg[0]) { 2007c478bd9Sstevel@tonic-gate case 's': 2017c478bd9Sstevel@tonic-gate opt_oneway = OPT_SRC; 2027c478bd9Sstevel@tonic-gate break; 2037c478bd9Sstevel@tonic-gate case 'd': 2047c478bd9Sstevel@tonic-gate opt_oneway = OPT_DST; 2057c478bd9Sstevel@tonic-gate break; 2067c478bd9Sstevel@tonic-gate default: 2077c478bd9Sstevel@tonic-gate fprintf(stderr, 2087c478bd9Sstevel@tonic-gate gettext(ERR_badopt), 2097c478bd9Sstevel@tonic-gate c, optarg); 2107c478bd9Sstevel@tonic-gate errs |= ERR_INVAL; 2117c478bd9Sstevel@tonic-gate break; 2127c478bd9Sstevel@tonic-gate } 2137c478bd9Sstevel@tonic-gate break; 2147c478bd9Sstevel@tonic-gate 2157c478bd9Sstevel@tonic-gate case 'r': /* restricted reconciliation */ 2167c478bd9Sstevel@tonic-gate if (num_restrs < MAX_RLIST) 2177c478bd9Sstevel@tonic-gate rlist[ num_restrs++ ] = optarg; 2187c478bd9Sstevel@tonic-gate else { 2197c478bd9Sstevel@tonic-gate fprintf(stderr, gettext(ERR_tomany), 2207c478bd9Sstevel@tonic-gate MAX_RLIST); 2217c478bd9Sstevel@tonic-gate errs |= ERR_INVAL; 2227c478bd9Sstevel@tonic-gate } 2237c478bd9Sstevel@tonic-gate break; 2247c478bd9Sstevel@tonic-gate 2257c478bd9Sstevel@tonic-gate case 's': 2267c478bd9Sstevel@tonic-gate if ((srcname = qualify(optarg)) == 0) 2277c478bd9Sstevel@tonic-gate errs |= ERR_MISSING; 2287c478bd9Sstevel@tonic-gate break; 2297c478bd9Sstevel@tonic-gate 2307c478bd9Sstevel@tonic-gate case 'd': 2317c478bd9Sstevel@tonic-gate if ((dstname = qualify(optarg)) == 0) 2327c478bd9Sstevel@tonic-gate errs |= ERR_MISSING; 2337c478bd9Sstevel@tonic-gate break; 2347c478bd9Sstevel@tonic-gate 2357c478bd9Sstevel@tonic-gate default: 2367c478bd9Sstevel@tonic-gate case '?': 2377c478bd9Sstevel@tonic-gate errs |= ERR_INVAL; 2387c478bd9Sstevel@tonic-gate break; 2397c478bd9Sstevel@tonic-gate } 2407c478bd9Sstevel@tonic-gate 2417c478bd9Sstevel@tonic-gate if (opt_debug & DBG_MISC) 2427c478bd9Sstevel@tonic-gate fprintf(stderr, "MISC: DBG=%s\n", showflags(dbgmap, opt_debug)); 2437c478bd9Sstevel@tonic-gate 2447c478bd9Sstevel@tonic-gate /* if we have file names, we need a source and destination */ 2457c478bd9Sstevel@tonic-gate if (optind < argc) { 2467c478bd9Sstevel@tonic-gate if (srcname == 0) { 2477c478bd9Sstevel@tonic-gate fprintf(stderr, gettext(ERR_nosrc)); 2487c478bd9Sstevel@tonic-gate errs |= ERR_INVAL; 2497c478bd9Sstevel@tonic-gate } 2507c478bd9Sstevel@tonic-gate if (dstname == 0) { 2517c478bd9Sstevel@tonic-gate fprintf(stderr, gettext(ERR_nodst)); 2527c478bd9Sstevel@tonic-gate errs |= ERR_INVAL; 2537c478bd9Sstevel@tonic-gate } 2547c478bd9Sstevel@tonic-gate } 2557c478bd9Sstevel@tonic-gate 2567c478bd9Sstevel@tonic-gate /* check for simple usage errors */ 2577c478bd9Sstevel@tonic-gate if (errs & ERR_INVAL) { 2587c478bd9Sstevel@tonic-gate usage(); 2597c478bd9Sstevel@tonic-gate exit(errs); 2607c478bd9Sstevel@tonic-gate } 2617c478bd9Sstevel@tonic-gate 2627c478bd9Sstevel@tonic-gate /* locate our baseline and rules files */ 2637c478bd9Sstevel@tonic-gate if (c = findfiles()) 2647c478bd9Sstevel@tonic-gate exit(c); 2657c478bd9Sstevel@tonic-gate 2667c478bd9Sstevel@tonic-gate /* figure out file creation defaults */ 2677c478bd9Sstevel@tonic-gate whoami(); 2687c478bd9Sstevel@tonic-gate 2697c478bd9Sstevel@tonic-gate /* read in our initial baseline */ 2707c478bd9Sstevel@tonic-gate if (!new_baseline && (c = read_baseline(file_base))) 2717c478bd9Sstevel@tonic-gate errs |= c; 2727c478bd9Sstevel@tonic-gate 2737c478bd9Sstevel@tonic-gate /* read in the rules file if we need or have rules */ 2747c478bd9Sstevel@tonic-gate if (optind >= argc && new_rules) { 2757c478bd9Sstevel@tonic-gate fprintf(stderr, ERR_nonames); 2767c478bd9Sstevel@tonic-gate errs |= ERR_INVAL; 2777c478bd9Sstevel@tonic-gate } else if (!new_rules) 2787c478bd9Sstevel@tonic-gate errs |= read_rules(file_rules); 2797c478bd9Sstevel@tonic-gate 2807c478bd9Sstevel@tonic-gate /* if anything has failed with our setup, go no further */ 2817c478bd9Sstevel@tonic-gate if (errs) { 2827c478bd9Sstevel@tonic-gate cleanup(errs); 2837c478bd9Sstevel@tonic-gate exit(errs); 2847c478bd9Sstevel@tonic-gate } 2857c478bd9Sstevel@tonic-gate 2867c478bd9Sstevel@tonic-gate /* 2877c478bd9Sstevel@tonic-gate * figure out whether or not we are willing to do a one-sided 2887c478bd9Sstevel@tonic-gate * analysis (where we don't even look at the other side. This 2897c478bd9Sstevel@tonic-gate * is an "I'm just curious what has changed" query, and we are 2907c478bd9Sstevel@tonic-gate * only willing to do it if: 2917c478bd9Sstevel@tonic-gate * we aren't actually going to do anything 2927c478bd9Sstevel@tonic-gate * we have a baseline we can compare against 2937c478bd9Sstevel@tonic-gate * otherwise, we are going to insist on being able to access 2947c478bd9Sstevel@tonic-gate * both the source and destination. 2957c478bd9Sstevel@tonic-gate */ 2967c478bd9Sstevel@tonic-gate if (opt_notouch && !new_baseline) 2977c478bd9Sstevel@tonic-gate opt_onesided = opt_oneway; 2987c478bd9Sstevel@tonic-gate 2997c478bd9Sstevel@tonic-gate /* 3007c478bd9Sstevel@tonic-gate * there are two interested usage scenarios: 3017c478bd9Sstevel@tonic-gate * file names specified 3027c478bd9Sstevel@tonic-gate * create new rules for the specified files 3037c478bd9Sstevel@tonic-gate * evaulate and reconcile only the specified files 3047c478bd9Sstevel@tonic-gate * no file names specified 3057c478bd9Sstevel@tonic-gate * use already existing rules 3067c478bd9Sstevel@tonic-gate * consider restricting them to specified subdirs/files 3077c478bd9Sstevel@tonic-gate */ 3087c478bd9Sstevel@tonic-gate if (optind < argc) { 3097c478bd9Sstevel@tonic-gate /* figure out what base pair we're working on */ 3107c478bd9Sstevel@tonic-gate bp = add_base(srcname, dstname); 3117c478bd9Sstevel@tonic-gate 3127c478bd9Sstevel@tonic-gate /* perverse default rules to avoid trouble */ 3137c478bd9Sstevel@tonic-gate if (new_rules) { 3147c478bd9Sstevel@tonic-gate errs |= add_ignore(0, SUFX_RULES); 3157c478bd9Sstevel@tonic-gate errs |= add_ignore(0, SUFX_BASE); 3167c478bd9Sstevel@tonic-gate } 3177c478bd9Sstevel@tonic-gate 3187c478bd9Sstevel@tonic-gate /* create include rules for each file/dir arg */ 3197c478bd9Sstevel@tonic-gate while (optind < argc) 3207c478bd9Sstevel@tonic-gate errs |= add_include(bp, argv[ optind++ ]); 3217c478bd9Sstevel@tonic-gate 3227c478bd9Sstevel@tonic-gate /* 3237c478bd9Sstevel@tonic-gate * evaluate the specified base on each side, 3247c478bd9Sstevel@tonic-gate * being careful to limit evaulation to new rules 3257c478bd9Sstevel@tonic-gate */ 3267c478bd9Sstevel@tonic-gate errs |= evaluate(bp, OPT_SRC, TRUE); 3277c478bd9Sstevel@tonic-gate errs |= evaluate(bp, OPT_DST, TRUE); 3287c478bd9Sstevel@tonic-gate } else { 3297c478bd9Sstevel@tonic-gate /* note any possible evaluation restrictions */ 3307c478bd9Sstevel@tonic-gate for (i = 0; i < num_restrs; i++) 3317c478bd9Sstevel@tonic-gate errs |= add_restr(rlist[i]); 3327c478bd9Sstevel@tonic-gate 3337c478bd9Sstevel@tonic-gate /* 3347c478bd9Sstevel@tonic-gate * we can only prune the baseline file if we have done 3357c478bd9Sstevel@tonic-gate * a complete (unrestricted) analysis. 3367c478bd9Sstevel@tonic-gate */ 3377c478bd9Sstevel@tonic-gate if (i == 0) 3387c478bd9Sstevel@tonic-gate do_prune = 1; 3397c478bd9Sstevel@tonic-gate 3407c478bd9Sstevel@tonic-gate /* evaulate each base on each side */ 3417c478bd9Sstevel@tonic-gate for (bp = bases; bp; bp = bp->b_next) { 3427c478bd9Sstevel@tonic-gate errs |= evaluate(bp, OPT_SRC, FALSE); 3437c478bd9Sstevel@tonic-gate errs |= evaluate(bp, OPT_DST, FALSE); 3447c478bd9Sstevel@tonic-gate } 3457c478bd9Sstevel@tonic-gate } 3467c478bd9Sstevel@tonic-gate 3477c478bd9Sstevel@tonic-gate /* if anything serious happened, skip reconciliation */ 3487c478bd9Sstevel@tonic-gate if (errs & ERR_FATAL) { 3497c478bd9Sstevel@tonic-gate cleanup(errs); 3507c478bd9Sstevel@tonic-gate exit(errs); 3517c478bd9Sstevel@tonic-gate } 3527c478bd9Sstevel@tonic-gate 3537c478bd9Sstevel@tonic-gate /* analyze and deal with the differenecs */ 3547c478bd9Sstevel@tonic-gate errs |= analyze(); 3557c478bd9Sstevel@tonic-gate 3567c478bd9Sstevel@tonic-gate /* see if there is any dead-wood in the baseline */ 3577c478bd9Sstevel@tonic-gate if (do_prune) { 3587c478bd9Sstevel@tonic-gate c = prune(); 3597c478bd9Sstevel@tonic-gate 3607c478bd9Sstevel@tonic-gate if (c > 0 && opt_verbose) 3617c478bd9Sstevel@tonic-gate fprintf(stdout, V_prunes, c); 3627c478bd9Sstevel@tonic-gate } 3637c478bd9Sstevel@tonic-gate 3647c478bd9Sstevel@tonic-gate /* print out a final summary */ 3657c478bd9Sstevel@tonic-gate summary(); 3667c478bd9Sstevel@tonic-gate 3677c478bd9Sstevel@tonic-gate /* update the rules and baseline files (if needed) */ 3687c478bd9Sstevel@tonic-gate (void) umask(my_umask); 3697c478bd9Sstevel@tonic-gate errs |= write_baseline(file_base); 3707c478bd9Sstevel@tonic-gate errs |= write_rules(file_rules); 3717c478bd9Sstevel@tonic-gate 3727c478bd9Sstevel@tonic-gate if (opt_debug & DBG_MISC) 3737c478bd9Sstevel@tonic-gate fprintf(stderr, "MISC: EXIT=%s\n", showflags(errmap, errs)); 3747c478bd9Sstevel@tonic-gate 3757c478bd9Sstevel@tonic-gate /* just returning ERR_RESOLVABLE upsets some people */ 3767c478bd9Sstevel@tonic-gate if (errs == ERR_RESOLVABLE && !opt_notouch) 3777c478bd9Sstevel@tonic-gate errs = 0; 3787c478bd9Sstevel@tonic-gate 3797c478bd9Sstevel@tonic-gate /* all done */ 3807c478bd9Sstevel@tonic-gate cleanup(0); 3813aceb801Sjwahlig return (errs); 3827c478bd9Sstevel@tonic-gate } 3837c478bd9Sstevel@tonic-gate 3847c478bd9Sstevel@tonic-gate 3857c478bd9Sstevel@tonic-gate /* 3867c478bd9Sstevel@tonic-gate * routine: 3877c478bd9Sstevel@tonic-gate * usage 3887c478bd9Sstevel@tonic-gate * 3897c478bd9Sstevel@tonic-gate * purpose: 3907c478bd9Sstevel@tonic-gate * print out a usage message 3917c478bd9Sstevel@tonic-gate * 3927c478bd9Sstevel@tonic-gate * parameters: 3937c478bd9Sstevel@tonic-gate * none 3947c478bd9Sstevel@tonic-gate * 3957c478bd9Sstevel@tonic-gate * returns: 3967c478bd9Sstevel@tonic-gate * none 3977c478bd9Sstevel@tonic-gate * 3987c478bd9Sstevel@tonic-gate * note: 3997c478bd9Sstevel@tonic-gate * the -D and -E switches are for development/test/support 4007c478bd9Sstevel@tonic-gate * use only and do not show up in the general usage message. 4017c478bd9Sstevel@tonic-gate */ 4027c478bd9Sstevel@tonic-gate static void 4037c478bd9Sstevel@tonic-gate usage(void) 4047c478bd9Sstevel@tonic-gate { 4057c478bd9Sstevel@tonic-gate fprintf(stderr, "%s\t%s %s\n", gettext(ERR_usage), "filesync", 4067c478bd9Sstevel@tonic-gate gettext(USE_simple)); 4077c478bd9Sstevel@tonic-gate fprintf(stderr, "\t%s %s\n", "filesync", gettext(USE_all)); 4087c478bd9Sstevel@tonic-gate fprintf(stderr, "\t-a .......... %s\n", gettext(USE_a)); 4097c478bd9Sstevel@tonic-gate fprintf(stderr, "\t-e .......... %s\n", gettext(USE_e)); 4107c478bd9Sstevel@tonic-gate fprintf(stderr, "\t-h .......... %s\n", gettext(USE_h)); 4117c478bd9Sstevel@tonic-gate fprintf(stderr, "\t-m .......... %s\n", gettext(USE_m)); 4127c478bd9Sstevel@tonic-gate fprintf(stderr, "\t-n .......... %s\n", gettext(USE_n)); 4137c478bd9Sstevel@tonic-gate fprintf(stderr, "\t-q .......... %s\n", gettext(USE_q)); 4147c478bd9Sstevel@tonic-gate fprintf(stderr, "\t-v .......... %s\n", gettext(USE_v)); 4157c478bd9Sstevel@tonic-gate fprintf(stderr, "\t-y .......... %s\n", gettext(USE_y)); 4167c478bd9Sstevel@tonic-gate fprintf(stderr, "\t-s dir ...... %s\n", gettext(USE_s)); 4177c478bd9Sstevel@tonic-gate fprintf(stderr, "\t-d dir ...... %s\n", gettext(USE_d)); 4187c478bd9Sstevel@tonic-gate fprintf(stderr, "\t-r dir ...... %s\n", gettext(USE_r)); 4197c478bd9Sstevel@tonic-gate fprintf(stderr, "\t-f [sdon].... %s\n", gettext(USE_f)); 4207c478bd9Sstevel@tonic-gate fprintf(stderr, "\t-o src/dst... %s\n", gettext(USE_o)); 4217c478bd9Sstevel@tonic-gate } 4227c478bd9Sstevel@tonic-gate 4237c478bd9Sstevel@tonic-gate /* 4247c478bd9Sstevel@tonic-gate * routine: 4257c478bd9Sstevel@tonic-gate * confirm 4267c478bd9Sstevel@tonic-gate * 4277c478bd9Sstevel@tonic-gate * purpose: 4287c478bd9Sstevel@tonic-gate * to confirm that the user is willing to do something dangerous 4297c478bd9Sstevel@tonic-gate * 4307c478bd9Sstevel@tonic-gate * parameters: 4317c478bd9Sstevel@tonic-gate * warning message to be printed 4327c478bd9Sstevel@tonic-gate * 4337c478bd9Sstevel@tonic-gate * returns: 4347c478bd9Sstevel@tonic-gate * void 4357c478bd9Sstevel@tonic-gate * 4367c478bd9Sstevel@tonic-gate * notes: 4377c478bd9Sstevel@tonic-gate * if this is a "notouch" or if the user has pre-confirmed, 4387c478bd9Sstevel@tonic-gate * we should not obtain the confirmation and just return that 4397c478bd9Sstevel@tonic-gate * the user has confirmed. 4407c478bd9Sstevel@tonic-gate */ 4417c478bd9Sstevel@tonic-gate void 4427c478bd9Sstevel@tonic-gate confirm(char *message) 4437c478bd9Sstevel@tonic-gate { FILE *ttyi, *ttyo; 4447c478bd9Sstevel@tonic-gate char ansbuf[ MAX_LINE ]; 4457c478bd9Sstevel@tonic-gate 4467c478bd9Sstevel@tonic-gate /* if user pre-confirmed, we don't have to ask */ 4477c478bd9Sstevel@tonic-gate if (opt_yes || opt_notouch) 4487c478bd9Sstevel@tonic-gate return; 4497c478bd9Sstevel@tonic-gate 4507c478bd9Sstevel@tonic-gate ttyo = fopen("/dev/tty", "w"); 4517c478bd9Sstevel@tonic-gate ttyi = fopen("/dev/tty", "r"); 4527c478bd9Sstevel@tonic-gate if (ttyi == NULL || ttyo == NULL) 4537c478bd9Sstevel@tonic-gate exit(ERR_OTHER); 4547c478bd9Sstevel@tonic-gate 4557c478bd9Sstevel@tonic-gate /* explain the problem and prompt for confirmation */ 4567c478bd9Sstevel@tonic-gate fprintf(ttyo, message); 4577c478bd9Sstevel@tonic-gate fprintf(ttyo, gettext(WARN_proceed)); 4587c478bd9Sstevel@tonic-gate 4597c478bd9Sstevel@tonic-gate /* if the user doesn't kill us, we can continue */ 4607c478bd9Sstevel@tonic-gate (void) fgets(ansbuf, sizeof (ansbuf), ttyi); 4617c478bd9Sstevel@tonic-gate 4627c478bd9Sstevel@tonic-gate /* close the files and return */ 4637c478bd9Sstevel@tonic-gate (void) fclose(ttyi); 4647c478bd9Sstevel@tonic-gate (void) fclose(ttyo); 4657c478bd9Sstevel@tonic-gate } 4667c478bd9Sstevel@tonic-gate 4677c478bd9Sstevel@tonic-gate void 4687c478bd9Sstevel@tonic-gate nomem(char *reason) 4697c478bd9Sstevel@tonic-gate { 4707c478bd9Sstevel@tonic-gate fprintf(stderr, gettext(ERR_nomem), reason); 4717c478bd9Sstevel@tonic-gate exit(ERR_OTHER); 4727c478bd9Sstevel@tonic-gate } 4737c478bd9Sstevel@tonic-gate 4747c478bd9Sstevel@tonic-gate /* 4757c478bd9Sstevel@tonic-gate * routine: 4767c478bd9Sstevel@tonic-gate * findfiles 4777c478bd9Sstevel@tonic-gate * 4787c478bd9Sstevel@tonic-gate * purpose: 4797c478bd9Sstevel@tonic-gate * to locate our baseline and rules files 4807c478bd9Sstevel@tonic-gate * 4817c478bd9Sstevel@tonic-gate * parameters: 4827c478bd9Sstevel@tonic-gate * none 4837c478bd9Sstevel@tonic-gate * 4847c478bd9Sstevel@tonic-gate * returns: 4857c478bd9Sstevel@tonic-gate * error mask 4867c478bd9Sstevel@tonic-gate * settings of file_base and file_rules 4877c478bd9Sstevel@tonic-gate * 4887c478bd9Sstevel@tonic-gate * side-effects: 4897c478bd9Sstevel@tonic-gate * in order to keep multiple filesyncs from running in parallel 4907c478bd9Sstevel@tonic-gate * we put an advisory lock on the baseline file. If the baseline 4917c478bd9Sstevel@tonic-gate * file does not exist we create one. The unlocking (and deletion 4927c478bd9Sstevel@tonic-gate * of extraneous baselines) is handled in cleanup. 4937c478bd9Sstevel@tonic-gate */ 4947c478bd9Sstevel@tonic-gate static errmask_t 4957c478bd9Sstevel@tonic-gate findfiles(void) /* find rule and baseline files */ 4967c478bd9Sstevel@tonic-gate { char *s, *where; 4977c478bd9Sstevel@tonic-gate char namebuf[MAX_PATH]; 4987c478bd9Sstevel@tonic-gate int ret; 4997c478bd9Sstevel@tonic-gate errmask_t errs = 0; 5007c478bd9Sstevel@tonic-gate 5017c478bd9Sstevel@tonic-gate /* figure out where the files should be located */ 5027c478bd9Sstevel@tonic-gate s = getenv("FILESYNC"); 5037c478bd9Sstevel@tonic-gate where = (s && *s) ? expand(s) : expand(DFLT_PRFX); 5047c478bd9Sstevel@tonic-gate 5057c478bd9Sstevel@tonic-gate /* see if we got a viable name */ 5067c478bd9Sstevel@tonic-gate if (where == 0) { 5077c478bd9Sstevel@tonic-gate fprintf(stderr, gettext(ERR_nofsync)); 5087c478bd9Sstevel@tonic-gate return (ERR_FILES); 5097c478bd9Sstevel@tonic-gate } 5107c478bd9Sstevel@tonic-gate 5117c478bd9Sstevel@tonic-gate /* try to form the name of the rules file */ 5127c478bd9Sstevel@tonic-gate strcpy(namebuf, where); 5137c478bd9Sstevel@tonic-gate strcat(namebuf, SUFX_RULES); 5147c478bd9Sstevel@tonic-gate s = strdup(namebuf); 5157c478bd9Sstevel@tonic-gate errs = check_access(namebuf, &new_rules); 5167c478bd9Sstevel@tonic-gate 5177c478bd9Sstevel@tonic-gate /* if we cannot find a proper rules file, look in the old place */ 5187c478bd9Sstevel@tonic-gate if (new_rules && errs == 0) { 5197c478bd9Sstevel@tonic-gate strcpy(namebuf, where); 5207c478bd9Sstevel@tonic-gate strcat(namebuf, SUFX_OLD); 5217c478bd9Sstevel@tonic-gate file_rules = strdup(namebuf); 5227c478bd9Sstevel@tonic-gate errs = check_access(namebuf, &new_rules); 5237c478bd9Sstevel@tonic-gate 5247c478bd9Sstevel@tonic-gate /* if we couldn't find that either, go with new name */ 5257c478bd9Sstevel@tonic-gate if (new_rules && errs == 0) 5267c478bd9Sstevel@tonic-gate file_rules = s; 5277c478bd9Sstevel@tonic-gate } else 5287c478bd9Sstevel@tonic-gate file_rules = s; 5297c478bd9Sstevel@tonic-gate 5307c478bd9Sstevel@tonic-gate /* try to form the name of the baseline file */ 5317c478bd9Sstevel@tonic-gate strcpy(namebuf, where); 5327c478bd9Sstevel@tonic-gate strcat(namebuf, SUFX_BASE); 5337c478bd9Sstevel@tonic-gate file_base = strdup(namebuf); 5347c478bd9Sstevel@tonic-gate errs |= check_access(namebuf, &new_baseline); 5357c478bd9Sstevel@tonic-gate 5367c478bd9Sstevel@tonic-gate if (opt_debug & DBG_FILES) { 5377c478bd9Sstevel@tonic-gate fprintf(stderr, "FILE: %s rules file: %s\n", 5387c478bd9Sstevel@tonic-gate new_rules ? "new" : "existing", file_rules); 5397c478bd9Sstevel@tonic-gate 5407c478bd9Sstevel@tonic-gate fprintf(stderr, "FILE: %s base file: %s\n", 5417c478bd9Sstevel@tonic-gate new_baseline ? "new" : "existing", file_base); 5427c478bd9Sstevel@tonic-gate } 5437c478bd9Sstevel@tonic-gate 5447c478bd9Sstevel@tonic-gate /* 5457c478bd9Sstevel@tonic-gate * in order to lock out other filesync programs we need some 5467c478bd9Sstevel@tonic-gate * file we can lock. We do an advisory lock on the baseline 5477c478bd9Sstevel@tonic-gate * file. If no baseline file exists, we create an empty one. 5487c478bd9Sstevel@tonic-gate */ 5497c478bd9Sstevel@tonic-gate if (new_baseline) 5507c478bd9Sstevel@tonic-gate lockfd = creat(file_base, 0666); 5517c478bd9Sstevel@tonic-gate else 5527c478bd9Sstevel@tonic-gate lockfd = open(file_base, O_RDWR); 5537c478bd9Sstevel@tonic-gate 5547c478bd9Sstevel@tonic-gate if (lockfd < 0) { 5557c478bd9Sstevel@tonic-gate fprintf(stderr, new_baseline ? ERR_creat : ERR_open, 5567c478bd9Sstevel@tonic-gate TXT_base, file_base); 5577c478bd9Sstevel@tonic-gate errs |= ERR_FILES; 5587c478bd9Sstevel@tonic-gate } else { 5597c478bd9Sstevel@tonic-gate ret = lockf(lockfd, F_TLOCK, 0L); 5607c478bd9Sstevel@tonic-gate if (ret < 0) { 5617c478bd9Sstevel@tonic-gate fprintf(stderr, ERR_lock, TXT_base, file_base); 5627c478bd9Sstevel@tonic-gate errs |= ERR_FILES; 5637c478bd9Sstevel@tonic-gate } else if (opt_debug & DBG_FILES) 5647c478bd9Sstevel@tonic-gate fprintf(stderr, "FILE: locking baseline file %s\n", 5657c478bd9Sstevel@tonic-gate file_base); 5667c478bd9Sstevel@tonic-gate } 5677c478bd9Sstevel@tonic-gate 5687c478bd9Sstevel@tonic-gate return (errs); 5697c478bd9Sstevel@tonic-gate } 5707c478bd9Sstevel@tonic-gate 5717c478bd9Sstevel@tonic-gate /* 5727c478bd9Sstevel@tonic-gate * routine: 5737c478bd9Sstevel@tonic-gate * cleanup 5747c478bd9Sstevel@tonic-gate * 5757c478bd9Sstevel@tonic-gate * purpose: 5767c478bd9Sstevel@tonic-gate * to clean up temporary files and locking prior to exit 5777c478bd9Sstevel@tonic-gate * 5787c478bd9Sstevel@tonic-gate * paremeters: 5797c478bd9Sstevel@tonic-gate * error mask 5807c478bd9Sstevel@tonic-gate * 5817c478bd9Sstevel@tonic-gate * returns: 5827c478bd9Sstevel@tonic-gate * void 5837c478bd9Sstevel@tonic-gate * 5847c478bd9Sstevel@tonic-gate * notes: 5857c478bd9Sstevel@tonic-gate * if there are no errors, the baseline file is assumed to be good. 5867c478bd9Sstevel@tonic-gate * Otherwise, if we created a temporary baseline file (just for 5877c478bd9Sstevel@tonic-gate * locking) we will delete it. 5887c478bd9Sstevel@tonic-gate */ 5897c478bd9Sstevel@tonic-gate static void 5907c478bd9Sstevel@tonic-gate cleanup(errmask_t errmask) 5917c478bd9Sstevel@tonic-gate { 5927c478bd9Sstevel@tonic-gate /* unlock the baseline file */ 5937c478bd9Sstevel@tonic-gate if (opt_debug & DBG_FILES) 5947c478bd9Sstevel@tonic-gate fprintf(stderr, "FILE: unlock baseline file %s\n", file_base); 5957c478bd9Sstevel@tonic-gate (void) lockf(lockfd, F_ULOCK, 0); 5967c478bd9Sstevel@tonic-gate 5977c478bd9Sstevel@tonic-gate /* see if we need to delete a temporary copy */ 5987c478bd9Sstevel@tonic-gate if (errmask && new_baseline) { 5997c478bd9Sstevel@tonic-gate if (opt_debug & DBG_FILES) 6007c478bd9Sstevel@tonic-gate fprintf(stderr, "FILE: unlink temp baseline file %s\n", 6017c478bd9Sstevel@tonic-gate file_base); 6027c478bd9Sstevel@tonic-gate (void) unlink(file_base); 6037c478bd9Sstevel@tonic-gate } 6047c478bd9Sstevel@tonic-gate } 6057c478bd9Sstevel@tonic-gate 6067c478bd9Sstevel@tonic-gate /* 6077c478bd9Sstevel@tonic-gate * routine: 6087c478bd9Sstevel@tonic-gate * check_access 6097c478bd9Sstevel@tonic-gate * 6107c478bd9Sstevel@tonic-gate * purpose: 6117c478bd9Sstevel@tonic-gate * to determine whether or not we can access an existing file 6127c478bd9Sstevel@tonic-gate * or create a new one 6137c478bd9Sstevel@tonic-gate * 6147c478bd9Sstevel@tonic-gate * parameters: 6157c478bd9Sstevel@tonic-gate * name of file (in a clobberable buffer) 6167c478bd9Sstevel@tonic-gate * pointer to new file flag 6177c478bd9Sstevel@tonic-gate * 6187c478bd9Sstevel@tonic-gate * returns: 6197c478bd9Sstevel@tonic-gate * error mask 6207c478bd9Sstevel@tonic-gate * setting of the new file flag 6217c478bd9Sstevel@tonic-gate * 6227c478bd9Sstevel@tonic-gate * note: 6237c478bd9Sstevel@tonic-gate * it is kind of a kluge that this routine clobbers the name, 6247c478bd9Sstevel@tonic-gate * but it is only called from one place, it needs a modified 6257c478bd9Sstevel@tonic-gate * copy of the name, and the one caller doesn't mind. 6267c478bd9Sstevel@tonic-gate */ 6277c478bd9Sstevel@tonic-gate static errmask_t 6287c478bd9Sstevel@tonic-gate check_access(char *name, int *newflag) 6297c478bd9Sstevel@tonic-gate { char *s; 6307c478bd9Sstevel@tonic-gate 6317c478bd9Sstevel@tonic-gate /* start out by asking for what we want */ 6327c478bd9Sstevel@tonic-gate if (access(name, R_OK|W_OK) == 0) { 6337c478bd9Sstevel@tonic-gate *newflag = 0; 6347c478bd9Sstevel@tonic-gate return (0); 6357c478bd9Sstevel@tonic-gate } 6367c478bd9Sstevel@tonic-gate 637*da6c28aaSamw /* if the problem is isn't non-existence, lose */ 6387c478bd9Sstevel@tonic-gate if (errno != ENOENT) { 6397c478bd9Sstevel@tonic-gate *newflag = 0; 6407c478bd9Sstevel@tonic-gate fprintf(stderr, gettext(ERR_rdwri), name); 6417c478bd9Sstevel@tonic-gate return (ERR_FILES); 6427c478bd9Sstevel@tonic-gate } 6437c478bd9Sstevel@tonic-gate 6447c478bd9Sstevel@tonic-gate /* 6457c478bd9Sstevel@tonic-gate * the file doesn't exist, so there is still hope if we can 6467c478bd9Sstevel@tonic-gate * write in the directory that should contain the file 6477c478bd9Sstevel@tonic-gate */ 6487c478bd9Sstevel@tonic-gate *newflag = 1; 6497c478bd9Sstevel@tonic-gate 6507c478bd9Sstevel@tonic-gate /* truncate the file name to its containing directory */ 6517c478bd9Sstevel@tonic-gate for (s = name; s[1]; s++); 6527c478bd9Sstevel@tonic-gate while (s > name && *s != '/') 6537c478bd9Sstevel@tonic-gate s--; 6547c478bd9Sstevel@tonic-gate if (s > name) 6557c478bd9Sstevel@tonic-gate *s = 0; 6567c478bd9Sstevel@tonic-gate else if (*s == '/') 6577c478bd9Sstevel@tonic-gate s[1] = 0; 6587c478bd9Sstevel@tonic-gate else 6597c478bd9Sstevel@tonic-gate name = "."; 6607c478bd9Sstevel@tonic-gate 6617c478bd9Sstevel@tonic-gate /* then see if we have write access to the directory */ 6627c478bd9Sstevel@tonic-gate if (access(name, W_OK) == 0) 6637c478bd9Sstevel@tonic-gate return (0); 6647c478bd9Sstevel@tonic-gate 6657c478bd9Sstevel@tonic-gate fprintf(stderr, gettext(ERR_dirwac), name); 6667c478bd9Sstevel@tonic-gate return (ERR_FILES); 6677c478bd9Sstevel@tonic-gate } 6687c478bd9Sstevel@tonic-gate 6697c478bd9Sstevel@tonic-gate /* 6707c478bd9Sstevel@tonic-gate * routine: 6717c478bd9Sstevel@tonic-gate * whoami 6727c478bd9Sstevel@tonic-gate * 6737c478bd9Sstevel@tonic-gate * purpose: 6747c478bd9Sstevel@tonic-gate * to figure out who I am and what the default modes/ownership 6757c478bd9Sstevel@tonic-gate * is on files that I create. 6767c478bd9Sstevel@tonic-gate */ 6777c478bd9Sstevel@tonic-gate static void 6787c478bd9Sstevel@tonic-gate whoami() 6797c478bd9Sstevel@tonic-gate { 6807c478bd9Sstevel@tonic-gate my_uid = geteuid(); 6817c478bd9Sstevel@tonic-gate my_gid = getegid(); 6827c478bd9Sstevel@tonic-gate my_umask = umask(0); 6837c478bd9Sstevel@tonic-gate 6847c478bd9Sstevel@tonic-gate if (opt_debug & DBG_MISC) 685f48205beScasper fprintf(stderr, "MISC: my_uid=%u, my_gid=%u, my_umask=%03o\n", 6867c478bd9Sstevel@tonic-gate my_uid, my_gid, my_umask); 6877c478bd9Sstevel@tonic-gate } 688