xref: /freebsd-13-stable/sbin/mksnap_ffs/mksnap_ffs.c (revision 17da660ad5b3b9cd90e164dd4dbb9beaa7203054)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 2003 Networks Associates Technology, Inc.
5  * All rights reserved.
6  *
7  * This software was developed for the FreeBSD Project by Marshall
8  * Kirk McKusick and Network Associates Laboratories, the Security
9  * Research Division of Network Associates, Inc. under DARPA/SPAWAR
10  * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS
11  * research program.
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. The names of the authors may not be used to endorse or promote
22  *    products derived from this software without specific prior written
23  *    permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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/stat.h>
41 #include <ufs/ufs/ufsmount.h>
42 #include <err.h>
43 #include <errno.h>
44 #include <fcntl.h>
45 #include <grp.h>
46 #include <limits.h>
47 #include <mntopts.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
51 #include <sysexits.h>
52 #include <unistd.h>
53 
54 static void
usage(void)55 usage(void)
56 {
57 
58 	errx(EX_USAGE, "usage: mksnap_ffs snapshot_name");
59 }
60 
61 static int
isdir(const char * path,struct stat * stbufp)62 isdir(const char *path, struct stat *stbufp)
63 {
64 
65 	if (stat(path, stbufp) < 0)
66 		return (-1);
67         if (!S_ISDIR(stbufp->st_mode))
68 		return (0);
69 	return (1);
70 }
71 
72 static int
issamefs(const char * path,struct statfs * stfsp)73 issamefs(const char *path, struct statfs *stfsp)
74 {
75 	struct statfs stfsbuf;
76 	struct stat stbuf;
77 
78 	if (isdir(path, &stbuf) != 1)
79 		return (-1);
80 	if (statfs(path, &stfsbuf) < 0)
81 		return (-1);
82 	if (fsidcmp(&stfsbuf.f_fsid, &stfsp->f_fsid) != 0)
83 		return (0);
84 	return (1);
85 }
86 
87 int
main(int argc,char ** argv)88 main(int argc, char **argv)
89 {
90 	char errmsg[255], path[PATH_MAX];
91 	char *cp, *snapname;
92 	struct statfs stfsbuf;
93 	struct group *grp;
94 	struct stat stbuf;
95 	struct iovec *iov;
96 	int fd, iovlen;
97 
98 	if (argc == 2)
99 		snapname = argv[1];
100 	else if (argc == 3)
101 		snapname = argv[2];	/* Old usage. */
102 	else
103 		usage();
104 
105 	/*
106 	 * Check that the user running this program has permission
107 	 * to create and remove a snapshot file from the directory
108 	 * in which they have requested to have it made. If the
109 	 * directory is sticky and not owned by the user, then they
110 	 * will not be able to remove the snapshot when they are
111 	 * done with it.
112 	 */
113 	if (strlen(snapname) >= PATH_MAX)
114 		errx(1, "pathname too long %s", snapname);
115 	cp = strrchr(snapname, '/');
116 	if (cp == NULL) {
117 		strlcpy(path, ".", PATH_MAX);
118 	} else if (cp == snapname) {
119 		strlcpy(path, "/", PATH_MAX);
120 	} else {
121 		strlcpy(path, snapname, cp - snapname + 1);
122 	}
123 	if (statfs(path, &stfsbuf) < 0)
124 		err(1, "%s", path);
125 	switch (isdir(path, &stbuf)) {
126 	case -1:
127 		err(1, "%s", path);
128 	case 0:
129 		errx(1, "%s: Not a directory", path);
130 	default:
131 		break;
132 	}
133 	if (access(path, W_OK) < 0)
134 		err(1, "Lack write permission in %s", path);
135 	if ((stbuf.st_mode & S_ISTXT) && stbuf.st_uid != getuid())
136 		errx(1, "Lack write permission in %s: Sticky bit set", path);
137 
138 	/*
139 	 * Work around an issue when mksnap_ffs is started in chroot'ed
140 	 * environment and f_mntonname contains absolute path within
141 	 * real root.
142 	 */
143 	for (cp = stfsbuf.f_mntonname; issamefs(cp, &stfsbuf) != 1;
144 	    cp = strchrnul(cp + 1, '/')) {
145 		if (cp[0] == '\0')
146 			errx(1, "%s: Not a mount point", stfsbuf.f_mntonname);
147 	}
148 	if (cp != stfsbuf.f_mntonname)
149 		strlcpy(stfsbuf.f_mntonname, cp, sizeof(stfsbuf.f_mntonname));
150 
151 	/*
152 	 * Having verified access to the directory in which the
153 	 * snapshot is to be built, proceed with creating it.
154 	 */
155 	if ((grp = getgrnam("operator")) == NULL)
156 		errx(1, "Cannot retrieve operator gid");
157 
158 	iov = NULL;
159 	iovlen = 0;
160 	build_iovec(&iov, &iovlen, "fstype", "ffs", 4);
161 	build_iovec(&iov, &iovlen, "from", snapname, (size_t)-1);
162 	build_iovec(&iov, &iovlen, "fspath", stfsbuf.f_mntonname, (size_t)-1);
163 	build_iovec(&iov, &iovlen, "errmsg", errmsg, sizeof(errmsg));
164 	build_iovec(&iov, &iovlen, "update", NULL, 0);
165 	build_iovec(&iov, &iovlen, "snapshot", NULL, 0);
166 
167 	*errmsg = '\0';
168 	if (nmount(iov, iovlen, stfsbuf.f_flags) < 0) {
169 		errmsg[sizeof(errmsg) - 1] = '\0';
170 		err(1, "Cannot create snapshot %s%s%s", snapname,
171 		    *errmsg != '\0' ? ": " : "", errmsg);
172 	}
173 	if ((fd = open(snapname, O_RDONLY)) < 0)
174 		err(1, "Cannot open %s", snapname);
175 	if (fstat(fd, &stbuf) != 0)
176 		err(1, "Cannot stat %s", snapname);
177 	if ((stbuf.st_flags & SF_SNAPSHOT) == 0)
178 		errx(1, "File %s is not a snapshot", snapname);
179 	if (fchown(fd, -1, grp->gr_gid) != 0)
180 		err(1, "Cannot chown %s", snapname);
181 	if (fchmod(fd, S_IRUSR | S_IRGRP) != 0)
182 		err(1, "Cannot chmod %s", snapname);
183 
184 	exit(EXIT_SUCCESS);
185 }
186