2 * Copyright (c) 2003 Networks Associates Technology, Inc.
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
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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 * 3. The names of the authors may not be used to endorse or promote
20 * products derived from this software without specific prior written
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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
38 #include <sys/param.h>
39 #include <sys/mount.h>
41 #include <ufs/ufs/ufsmount.h>
56 main(int argc, char **argv)
58 char *dir, *cp, path[PATH_MAX];
59 struct statfs stfsbuf;
69 memset(&args, 0, sizeof args);
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
80 if (strlen(args.fspec) >= PATH_MAX)
81 errx(1, "pathname too long %s", args.fspec);
82 cp = strrchr(args.fspec, '/');
84 strlcpy(path, ".", PATH_MAX);
85 } else if (cp == args.fspec) {
86 strlcpy(path, "/", PATH_MAX);
88 strlcpy(path, args.fspec, cp - args.fspec + 1);
90 if (statfs(path, &stfsbuf) < 0)
92 if (stat(path, &stbuf) < 0)
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);
102 * Having verified access to the directory in which the
103 * snapshot is to be built, proceed with creating it.
105 if ((grp = getgrnam("operator")) == NULL)
106 errx(1, "Cannot retrieve operator gid");
107 if (mount("ufs", dir, MNT_UPDATE | MNT_SNAPSHOT | stfsbuf.f_flags,
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);
112 if (fstat(fd, &stbuf) != 0)
113 err(1, "Cannot stat %s", args.fspec);
114 if ((stbuf.st_flags & SF_SNAPSHOT) == 0)
115 errx(1, "File %s is not a snapshot", args.fspec);
116 if (fchown(fd, -1, grp->gr_gid) != 0)
117 err(1, "Cannot chown %s", args.fspec);
118 if (fchmod(fd, S_IRUSR | S_IRGRP) != 0)
119 err(1, "Cannot chmod %s", args.fspec);
128 fprintf(stderr, "usage: mksnap_ffs mountpoint snapshot_name\n");