1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1992, 1993, 1994 5 * The Regents of the University of California. 6 * Copyright (c) 2005, 2006 Masanori Ozawa <ozawa@ongs.co.jp>, ONGS Inc. 7 * Copyright (c) 2006 Daichi Goto <daichi@freebsd.org> 8 * All rights reserved. 9 * 10 * This code is derived from software donated to Berkeley by 11 * Jan-Simon Pendry. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 2. Redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution. 21 * 3. Neither the name of the University nor the names of its contributors 22 * may be used to endorse or promote products derived from this software 23 * without specific prior written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35 * SUCH DAMAGE. 36 */ 37 38 #ifndef lint 39 static const char copyright[] = 40 "@(#) Copyright (c) 1992, 1993, 1994\n\ 41 The Regents of the University of California. All rights reserved.\n"; 42 #endif /* not lint */ 43 44 #ifndef lint 45 #if 0 46 static char sccsid[] = "@(#)mount_union.c 8.5 (Berkeley) 3/27/94"; 47 #endif 48 #endif /* not lint */ 49 50 #include <sys/param.h> 51 #include <sys/mount.h> 52 #include <sys/uio.h> 53 #include <sys/errno.h> 54 55 #include <err.h> 56 #include <stdio.h> 57 #include <stdlib.h> 58 #include <string.h> 59 #include <sysexits.h> 60 #include <unistd.h> 61 #include <grp.h> 62 #include <pwd.h> 63 64 #include "mntopts.h" 65 66 static int 67 subdir(const char *p, const char *dir) 68 { 69 int l; 70 71 l = strlen(dir); 72 if (l <= 1) 73 return (1); 74 75 if ((strncmp(p, dir, l) == 0) && (p[l] == '/' || p[l] == '\0')) 76 return (1); 77 78 return (0); 79 } 80 81 static void 82 usage(void) 83 { 84 (void)fprintf(stderr, 85 "usage: mount_unionfs [-o options] directory uniondir\n"); 86 exit(EX_USAGE); 87 } 88 89 static void 90 parse_gid(const char *s, char *buf, size_t bufsize) 91 { 92 struct group *gr; 93 char *inval; 94 95 if ((gr = getgrnam(s)) != NULL) 96 snprintf(buf, bufsize, "%d", gr->gr_gid); 97 else { 98 strtol(s, &inval, 10); 99 if (*inval != 0) { 100 errx(EX_NOUSER, "unknown group id: %s", s); 101 usage(); 102 } else { 103 strncpy(buf, s, bufsize); 104 } 105 } 106 } 107 108 static void 109 parse_uid(const char *s, char *buf, size_t bufsize) 110 { 111 struct passwd *pw; 112 char *inval; 113 114 if ((pw = getpwnam(s)) != NULL) 115 snprintf(buf, bufsize, "%d", pw->pw_uid); 116 else { 117 strtol(s, &inval, 10); 118 if (*inval != 0) { 119 errx(EX_NOUSER, "unknown user id: %s", s); 120 usage(); 121 } else { 122 strncpy(buf, s, bufsize); 123 } 124 } 125 } 126 127 int 128 main(int argc, char *argv[]) 129 { 130 struct iovec *iov; 131 int ch, iovlen; 132 char source [MAXPATHLEN], target[MAXPATHLEN], errmsg[255]; 133 char uid_str[20], gid_str[20]; 134 char fstype[] = "unionfs"; 135 char *p, *val; 136 137 iov = NULL; 138 iovlen = 0; 139 memset(errmsg, 0, sizeof(errmsg)); 140 141 while ((ch = getopt(argc, argv, "bo:")) != -1) { 142 switch (ch) { 143 case 'b': 144 printf("\n -b is deprecated. Use \"-o below\" instead\n"); 145 build_iovec(&iov, &iovlen, "below", NULL, 0); 146 break; 147 case 'o': 148 p = strchr(optarg, '='); 149 val = NULL; 150 if (p != NULL) { 151 *p = '\0'; 152 val = p + 1; 153 if (strcmp(optarg, "gid") == 0) { 154 parse_gid(val, gid_str, sizeof(gid_str)); 155 val = gid_str; 156 } 157 else if (strcmp(optarg, "uid") == 0) { 158 parse_uid(val, uid_str, sizeof(uid_str)); 159 val = uid_str; 160 } 161 } 162 build_iovec(&iov, &iovlen, optarg, val, (size_t)-1); 163 break; 164 case '?': 165 default: 166 usage(); 167 /* NOTREACHED */ 168 } 169 } 170 argc -= optind; 171 argv += optind; 172 173 if (argc != 2) 174 usage(); 175 176 /* resolve both target and source with realpath(3) */ 177 if (checkpath(argv[0], target) != 0) 178 err(EX_USAGE, "%s", target); 179 if (checkpath(argv[1], source) != 0) 180 err(EX_USAGE, "%s", source); 181 182 if (subdir(target, source) || subdir(source, target)) 183 errx(EX_USAGE, "%s (%s) and %s (%s) are not distinct paths", 184 argv[0], target, argv[1], source); 185 186 build_iovec(&iov, &iovlen, "fstype", fstype, (size_t)-1); 187 build_iovec(&iov, &iovlen, "fspath", source, (size_t)-1); 188 build_iovec(&iov, &iovlen, "from", target, (size_t)-1); 189 build_iovec(&iov, &iovlen, "errmsg", errmsg, sizeof(errmsg)); 190 191 if (nmount(iov, iovlen, 0)) 192 err(EX_OSERR, "%s: %s", source, errmsg); 193 exit(0); 194 } 195