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