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