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