xref: /freebsd/sbin/mount_unionfs/mount_unionfs.c (revision 39beb93c3f8bdbf72a61fda42300b5ebed7390c8)
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
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
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
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
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
129 main(int argc, char *argv[])
130 {
131 	struct iovec	*iov;
132 	int ch, mntflags, 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 	mntflags = 0;
141 	memset(errmsg, 0, sizeof(errmsg));
142 
143 	while ((ch = getopt(argc, argv, "bo:")) != -1) {
144 		switch (ch) {
145 		case 'b':
146 			printf("\n  -b is deprecated.  Use \"-o below\" instead\n");
147 			build_iovec(&iov, &iovlen, "below", NULL, 0);
148 			break;
149 		case 'o':
150                         p = strchr(optarg, '=');
151                         val = NULL;
152                         if (p != NULL) {
153                                 *p = '\0';
154                                 val = p + 1;
155 				if (strcmp(optarg, "gid") == 0) {
156 					parse_gid(val, gid_str, sizeof(gid_str));
157 					val = gid_str;
158 				}
159 				else if (strcmp(optarg, "uid") == 0) {
160 					parse_uid(val, uid_str, sizeof(uid_str));
161 					val = uid_str;
162 				}
163                         }
164                         build_iovec(&iov, &iovlen, optarg, val, (size_t)-1);
165 			break;
166 		case '?':
167 		default:
168 			usage();
169 			/* NOTREACHED */
170 		}
171 	}
172 	argc -= optind;
173 	argv += optind;
174 
175 	if (argc != 2)
176 		usage();
177 
178 	/* resolve both target and source with realpath(3) */
179 	(void)checkpath(argv[0], target);
180 	(void)checkpath(argv[1], 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, mntflags))
192 		err(EX_OSERR, "%s: %s", source, errmsg);
193 	exit(0);
194 }
195