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