xref: /freebsd/sbin/mount_unionfs/mount_unionfs.c (revision a123502ef781ea052e842af25c7aeb64fd9fe217)
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 #include <sys/param.h>
39 #include <sys/mount.h>
40 #include <sys/uio.h>
41 #include <sys/errno.h>
42 
43 #include <err.h>
44 #include <mntopts.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <sysexits.h>
49 #include <unistd.h>
50 #include <grp.h>
51 #include <pwd.h>
52 
53 static int
subdir(const char * p,const char * dir)54 subdir(const char *p, const char *dir)
55 {
56 	int		l;
57 
58 	l = strlen(dir);
59 	if (l <= 1)
60 		return (1);
61 
62 	if ((strncmp(p, dir, l) == 0) && (p[l] == '/' || p[l] == '\0'))
63 		return (1);
64 
65 	return (0);
66 }
67 
68 static void
usage(void)69 usage(void)
70 {
71 	(void)fprintf(stderr,
72 	    "usage: mount_unionfs [-o options] directory uniondir\n");
73 	exit(EX_USAGE);
74 }
75 
76 static void
parse_gid(const char * s,char * buf,size_t bufsize)77 parse_gid(const char *s, char *buf, size_t bufsize)
78 {
79 	struct group *gr;
80 	char *inval;
81 
82 	if ((gr = getgrnam(s)) != NULL)
83 		snprintf(buf, bufsize, "%d", gr->gr_gid);
84 	else {
85 		strtol(s, &inval, 10);
86 		if (*inval != 0) {
87                        errx(EX_NOUSER, "unknown group id: %s", s);
88                        usage();
89 		} else {
90 			strncpy(buf, s, bufsize);
91 		}
92 	}
93 }
94 
95 static void
parse_uid(const char * s,char * buf,size_t bufsize)96 parse_uid(const char *s, char *buf, size_t bufsize)
97 {
98 	struct passwd  *pw;
99 	char *inval;
100 
101 	if ((pw = getpwnam(s)) != NULL)
102 		snprintf(buf, bufsize, "%d", pw->pw_uid);
103 	else {
104 		strtol(s, &inval, 10);
105 		if (*inval != 0) {
106                        errx(EX_NOUSER, "unknown user id: %s", s);
107                        usage();
108 		} else {
109 			strncpy(buf, s, bufsize);
110 		}
111 	}
112 }
113 
114 int
main(int argc,char * argv[])115 main(int argc, char *argv[])
116 {
117 	struct iovec	*iov;
118 	int ch, iovlen;
119 	char source [MAXPATHLEN], target[MAXPATHLEN], errmsg[255];
120 	char uid_str[20], gid_str[20];
121 	char fstype[] = "unionfs";
122 	char *p, *val;
123 
124 	iov = NULL;
125 	iovlen = 0;
126 	memset(errmsg, 0, sizeof(errmsg));
127 
128 	while ((ch = getopt(argc, argv, "bo:")) != -1) {
129 		switch (ch) {
130 		case 'b':
131 			printf("\n  -b is deprecated.  Use \"-o below\" instead\n");
132 			build_iovec(&iov, &iovlen, "below", NULL, 0);
133 			break;
134 		case 'o':
135                         p = strchr(optarg, '=');
136                         val = NULL;
137                         if (p != NULL) {
138                                 *p = '\0';
139                                 val = p + 1;
140 				if (strcmp(optarg, "gid") == 0) {
141 					parse_gid(val, gid_str, sizeof(gid_str));
142 					val = gid_str;
143 				}
144 				else if (strcmp(optarg, "uid") == 0) {
145 					parse_uid(val, uid_str, sizeof(uid_str));
146 					val = uid_str;
147 				}
148                         }
149                         build_iovec(&iov, &iovlen, optarg, val, (size_t)-1);
150 			break;
151 		case '?':
152 		default:
153 			usage();
154 			/* NOTREACHED */
155 		}
156 	}
157 	argc -= optind;
158 	argv += optind;
159 
160 	if (argc != 2)
161 		usage();
162 
163 	/* resolve both target and source with realpath(3) */
164 	if (checkpath(argv[0], target) != 0)
165 		err(EX_USAGE, "%s", target);
166 	if (checkpath(argv[1], source) != 0)
167 		err(EX_USAGE, "%s", source);
168 
169 	if (subdir(target, source) || subdir(source, target))
170 		errx(EX_USAGE, "%s (%s) and %s (%s) are not distinct paths",
171 		     argv[0], target, argv[1], source);
172 
173 	build_iovec(&iov, &iovlen, "fstype", fstype, (size_t)-1);
174 	build_iovec(&iov, &iovlen, "fspath", source, (size_t)-1);
175 	build_iovec(&iov, &iovlen, "from", target, (size_t)-1);
176 	build_iovec(&iov, &iovlen, "errmsg", errmsg, sizeof(errmsg));
177 
178 	if (nmount(iov, iovlen, 0))
179 		err(EX_OSERR, "%s: %s", source, errmsg);
180 	exit(0);
181 }
182