Deleted Added
full compact
mksnap_ffs.c (138559) mksnap_ffs.c (193051)
1/*
2 * Copyright (c) 2003 Networks Associates Technology, Inc.
3 * All rights reserved.
4 *
5 * This software was developed for the FreeBSD Project by Marshall
6 * Kirk McKusick and Network Associates Laboratories, the Security
7 * Research Division of Network Associates, Inc. under DARPA/SPAWAR
8 * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS

--- 18 unchanged lines hidden (view full) ---

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 *
1/*
2 * Copyright (c) 2003 Networks Associates Technology, Inc.
3 * All rights reserved.
4 *
5 * This software was developed for the FreeBSD Project by Marshall
6 * Kirk McKusick and Network Associates Laboratories, the Security
7 * Research Division of Network Associates, Inc. under DARPA/SPAWAR
8 * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS

--- 18 unchanged lines hidden (view full) ---

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 * $FreeBSD: head/sbin/mksnap_ffs/mksnap_ffs.c 138559 2004-12-08 11:54:50Z phk $
35 * $FreeBSD: head/sbin/mksnap_ffs/mksnap_ffs.c 193051 2009-05-29 19:18:41Z pjd $
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>
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>
47#include <stdio.h>
48#include <stdlib.h>
49#include <string.h>
50#include <sysexits.h>
51#include <unistd.h>
52
48#include <stdio.h>
49#include <stdlib.h>
50#include <string.h>
51#include <sysexits.h>
52#include <unistd.h>
53
53void usage(void);
54static void
55usage(void)
56{
54
57
58 errx(EX_USAGE, "usage: mksnap_ffs snapshot_name");
59}
60
55int
56main(int argc, char **argv)
57{
61int
62main(int argc, char **argv)
63{
58 char *dir, *cp, path[PATH_MAX];
64 char errmsg[255], path[PATH_MAX];
65 char *cp, *snapname;
59 struct statfs stfsbuf;
66 struct statfs stfsbuf;
60 struct ufs_args args;
61 struct group *grp;
62 struct stat stbuf;
67 struct group *grp;
68 struct stat stbuf;
63 int fd;
69 struct iovec *iov;
70 int fd, iovlen;
64
71
65 if (argc != 3)
72 if (argc == 2)
73 snapname = argv[1];
74 else if (argc == 3)
75 snapname = argv[2]; /* Old usage. */
76 else
66 usage();
67
77 usage();
78
68 dir = argv[1];
69 memset(&args, 0, sizeof args);
70 args.fspec = argv[2];
71
72 /*
73 * Check that the user running this program has permission
74 * to create and remove a snapshot file from the directory
75 * in which they have requested to have it made. If the
76 * directory is sticky and not owned by the user, then they
77 * will not be able to remove the snapshot when they are
78 * done with it.
79 */
79 /*
80 * Check that the user running this program has permission
81 * to create and remove a snapshot file from the directory
82 * in which they have requested to have it made. If the
83 * directory is sticky and not owned by the user, then they
84 * will not be able to remove the snapshot when they are
85 * done with it.
86 */
80 if (strlen(args.fspec) >= PATH_MAX)
81 errx(1, "pathname too long %s", args.fspec);
82 cp = strrchr(args.fspec, '/');
87 if (strlen(snapname) >= PATH_MAX)
88 errx(1, "pathname too long %s", snapname);
89 cp = strrchr(snapname, '/');
83 if (cp == NULL) {
84 strlcpy(path, ".", PATH_MAX);
90 if (cp == NULL) {
91 strlcpy(path, ".", PATH_MAX);
85 } else if (cp == args.fspec) {
92 } else if (cp == snapname) {
86 strlcpy(path, "/", PATH_MAX);
87 } else {
93 strlcpy(path, "/", PATH_MAX);
94 } else {
88 strlcpy(path, args.fspec, cp - args.fspec + 1);
95 strlcpy(path, snapname, cp - snapname + 1);
89 }
90 if (statfs(path, &stfsbuf) < 0)
91 err(1, "%s", path);
92 if (stat(path, &stbuf) < 0)
93 err(1, "%s", path);
94 if (!S_ISDIR(stbuf.st_mode))
95 errx(1, "%s: Not a directory", path);
96 if (access(path, W_OK) < 0)
97 err(1, "Lack write permission in %s", path);
98 if ((stbuf.st_mode & S_ISTXT) && stbuf.st_uid != getuid())
99 errx(1, "Lack write permission in %s: Sticky bit set", path);
100
101 /*
102 * Having verified access to the directory in which the
103 * snapshot is to be built, proceed with creating it.
104 */
105 if ((grp = getgrnam("operator")) == NULL)
106 errx(1, "Cannot retrieve operator gid");
96 }
97 if (statfs(path, &stfsbuf) < 0)
98 err(1, "%s", path);
99 if (stat(path, &stbuf) < 0)
100 err(1, "%s", path);
101 if (!S_ISDIR(stbuf.st_mode))
102 errx(1, "%s: Not a directory", path);
103 if (access(path, W_OK) < 0)
104 err(1, "Lack write permission in %s", path);
105 if ((stbuf.st_mode & S_ISTXT) && stbuf.st_uid != getuid())
106 errx(1, "Lack write permission in %s: Sticky bit set", path);
107
108 /*
109 * Having verified access to the directory in which the
110 * snapshot is to be built, proceed with creating it.
111 */
112 if ((grp = getgrnam("operator")) == NULL)
113 errx(1, "Cannot retrieve operator gid");
107 if (mount("ufs", dir, MNT_UPDATE | MNT_SNAPSHOT | stfsbuf.f_flags,
108 &args) < 0)
109 err(1, "Cannot create %s", args.fspec);
110 if ((fd = open(args.fspec, O_RDONLY)) < 0)
111 err(1, "Cannot open %s", args.fspec);
114
115 build_iovec(&iov, &iovlen, "fstype", "ffs", 4);
116 build_iovec(&iov, &iovlen, "from", snapname, (size_t)-1);
117 build_iovec(&iov, &iovlen, "fspath", stfsbuf.f_mntonname, (size_t)-1);
118 build_iovec(&iov, &iovlen, "errmsg", errmsg, sizeof(errmsg));
119 build_iovec(&iov, &iovlen, "update", NULL, 0);
120 build_iovec(&iov, &iovlen, "snapshot", NULL, 0);
121
122 if (nmount(iov, iovlen, stfsbuf.f_flags) < 0)
123 err(1, "Cannot create snapshot %s: %s", snapname, errmsg);
124 if ((fd = open(snapname, O_RDONLY)) < 0)
125 err(1, "Cannot open %s", snapname);
112 if (fstat(fd, &stbuf) != 0)
126 if (fstat(fd, &stbuf) != 0)
113 err(1, "Cannot stat %s", args.fspec);
127 err(1, "Cannot stat %s", snapname);
114 if ((stbuf.st_flags & SF_SNAPSHOT) == 0)
128 if ((stbuf.st_flags & SF_SNAPSHOT) == 0)
115 errx(1, "File %s is not a snapshot", args.fspec);
129 errx(1, "File %s is not a snapshot", snapname);
116 if (fchown(fd, -1, grp->gr_gid) != 0)
130 if (fchown(fd, -1, grp->gr_gid) != 0)
117 err(1, "Cannot chown %s", args.fspec);
131 err(1, "Cannot chown %s", snapname);
118 if (fchmod(fd, S_IRUSR | S_IRGRP) != 0)
132 if (fchmod(fd, S_IRUSR | S_IRGRP) != 0)
119 err(1, "Cannot chmod %s", args.fspec);
133 err(1, "Cannot chmod %s", snapname);
120
121 exit(EXIT_SUCCESS);
122}
134
135 exit(EXIT_SUCCESS);
136}
123
124void
125usage()
126{
127
128 fprintf(stderr, "usage: mksnap_ffs mountpoint snapshot_name\n");
129 exit(EX_USAGE);
130}