1 /* $OpenBSD: mtree.c,v 1.26 2018/09/16 12:43:40 millert Exp $ */ 2 /* $NetBSD: mtree.c,v 1.7 1996/09/05 23:29:22 thorpej Exp $ */ 3 4 /*- 5 * Copyright (c) 1989, 1990, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33 #include <sys/stat.h> 34 #include <err.h> 35 #include <errno.h> 36 #include <unistd.h> 37 #include <stdio.h> 38 #include <limits.h> 39 #include <fts.h> 40 #include <grp.h> 41 #include <pwd.h> 42 #include "mtree.h" 43 #include "extern.h" 44 45 extern u_int32_t crc_total; 46 47 int ftsoptions = FTS_PHYSICAL; 48 int cflag, dflag, eflag, iflag, lflag, nflag, qflag, rflag, sflag, tflag, 49 uflag, Uflag; 50 u_int keys; 51 char fullpath[PATH_MAX]; 52 53 static void usage(void); 54 55 int 56 main(int argc, char *argv[]) 57 { 58 extern int optind; 59 extern char *optarg; 60 int ch; 61 char *dir, *p; 62 int status; 63 64 dir = NULL; 65 keys = KEYDEFAULT; 66 while ((ch = getopt(argc, argv, "cdef:iK:k:lnp:qrs:tUux")) != -1) 67 switch(ch) { 68 case 'c': 69 cflag = 1; 70 break; 71 case 'd': 72 dflag = 1; 73 break; 74 case 'e': 75 eflag = 1; 76 break; 77 case 'f': 78 if (!(freopen(optarg, "r", stdin))) 79 error("%s: %s", optarg, strerror(errno)); 80 break; 81 case 'i': 82 iflag = 1; 83 break; 84 case 'K': 85 while ((p = strsep(&optarg, " \t,")) != NULL) 86 if (*p != '\0') 87 keys |= parsekey(p, NULL); 88 break; 89 case 'k': 90 keys = F_TYPE; 91 while ((p = strsep(&optarg, " \t,")) != NULL) 92 if (*p != '\0') 93 keys |= parsekey(p, NULL); 94 break; 95 case 'l': 96 lflag = 1; 97 break; 98 case 'n': 99 nflag = 1; 100 break; 101 case 'p': 102 dir = optarg; 103 break; 104 case 'q': 105 qflag = 1; 106 break; 107 case 'r': 108 rflag = 1; 109 break; 110 case 's': 111 sflag = 1; 112 crc_total = ~strtol(optarg, &p, 0); 113 if (*p) 114 error("illegal seed value -- %s", optarg); 115 break; 116 case 't': 117 tflag = 1; 118 break; 119 case 'U': 120 Uflag = 1; 121 uflag = 1; 122 break; 123 case 'u': 124 uflag = 1; 125 break; 126 case 'x': 127 ftsoptions |= FTS_XDEV; 128 break; 129 case '?': 130 default: 131 usage(); 132 } 133 argc -= optind; 134 argv += optind; 135 136 if (argc) 137 usage(); 138 139 /* 140 * If uflag is set we can't make any pledges because we must be able 141 * to chown and place setugid bits. Make sure that we're pledged 142 * when -c was specified. 143 */ 144 if (!uflag || cflag) { 145 if (rflag && tflag) { 146 if (pledge("stdio rpath cpath getpw fattr", NULL) == -1) 147 err(1, "pledge"); 148 } else if (rflag && !tflag) { 149 if (pledge("stdio rpath cpath getpw", NULL) == -1) 150 err(1, "pledge"); 151 } else if (!rflag && tflag) { 152 if (pledge("stdio rpath getpw fattr", NULL) == -1) 153 err(1, "pledge"); 154 } else { 155 if (pledge("stdio rpath getpw", NULL) == -1) 156 err(1, "pledge"); 157 } 158 } 159 160 /* Keep passwd and group files open for faster lookups. */ 161 setpassent(1); 162 setgroupent(1); 163 164 if (dir && chdir(dir)) 165 error("%s: %s", dir, strerror(errno)); 166 167 if ((cflag || sflag) && !getcwd(fullpath, sizeof fullpath)) 168 error("getcwd: %s", strerror(errno)); 169 170 if (lflag == 1 && uflag == 1) 171 error("-l and -u flags are mutually exclusive"); 172 173 if (cflag) { 174 cwalk(); 175 exit(0); 176 } 177 status = verify(); 178 if (Uflag && (status == MISMATCHEXIT)) 179 status = 0; 180 exit(status); 181 } 182 183 static void 184 usage(void) 185 { 186 (void)fprintf(stderr, 187 "usage: mtree [-cdeilnqrtUux] [-f spec] [-K keywords] " 188 "[-k keywords] [-p path]\n" 189 " [-s seed]\n"); 190 exit(1); 191 } 192