1170762Sdelphij/* 2170762Sdelphij * Copyright (c) 2003 Networks Associates Technology, Inc. 3170762Sdelphij * All rights reserved. 4171490Sdelphij * 5171490Sdelphij * This software was developed for the FreeBSD Project by Marshall 6171490Sdelphij * Kirk McKusick and Network Associates Laboratories, the Security 7171490Sdelphij * Research Division of Network Associates, Inc. under DARPA/SPAWAR 8171490Sdelphij * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS 9170762Sdelphij * research program. 10171490Sdelphij * 11171490Sdelphij * Redistribution and use in source and binary forms, with or without 12171490Sdelphij * modification, are permitted provided that the following conditions 13171490Sdelphij * are met: 14171490Sdelphij * 1. Redistributions of source code must retain the above copyright 15171490Sdelphij * notice, this list of conditions and the following disclaimer. 16171490Sdelphij * 2. Redistributions in binary form must reproduce the above copyright 17171490Sdelphij * notice, this list of conditions and the following disclaimer in the 18171490Sdelphij * documentation and/or other materials provided with the distribution. 19171490Sdelphij * 3. The names of the authors may not be used to endorse or promote 20171490Sdelphij * products derived from this software without specific prior written 21171490Sdelphij * permission. 22171490Sdelphij * 23171490Sdelphij * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 24171490Sdelphij * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25171490Sdelphij * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26171490Sdelphij * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 27171490Sdelphij * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28171490Sdelphij * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29171490Sdelphij * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30171490Sdelphij * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31171490Sdelphij * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32171490Sdelphij * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33171490Sdelphij * SUCH DAMAGE. 34171490Sdelphij * 35171490Sdelphij * $FreeBSD$ 36171490Sdelphij */ 37171490Sdelphij 38171490Sdelphij#include <sys/param.h> 39171490Sdelphij#include <sys/mount.h> 40171490Sdelphij#include <sys/stat.h> 41171490Sdelphij#include <ufs/ufs/ufsmount.h> 42171490Sdelphij#include <err.h> 43171490Sdelphij#include <errno.h> 44171490Sdelphij#include <fcntl.h> 45171490Sdelphij#include <grp.h> 46171490Sdelphij#include <limits.h> 47171490Sdelphij#include <mntopts.h> 48171490Sdelphij#include <stdio.h> 49171490Sdelphij#include <stdlib.h> 50171490Sdelphij#include <string.h> 51171490Sdelphij#include <sysexits.h> 52171490Sdelphij#include <unistd.h> 53171490Sdelphij 54171490Sdelphijstatic void 55170762Sdelphijusage(void) 56171490Sdelphij{ 57171490Sdelphij 58170762Sdelphij errx(EX_USAGE, "usage: mksnap_ffs snapshot_name"); 59170762Sdelphij} 60171490Sdelphij 61170762Sdelphijint 62main(int argc, char **argv) 63{ 64 char errmsg[255], path[PATH_MAX]; 65 char *cp, *snapname; 66 struct statfs stfsbuf; 67 struct group *grp; 68 struct stat stbuf; 69 struct iovec *iov; 70 int fd, iovlen; 71 72 if (argc == 2) 73 snapname = argv[1]; 74 else if (argc == 3) 75 snapname = argv[2]; /* Old usage. */ 76 else 77 usage(); 78 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 */ 87 if (strlen(snapname) >= PATH_MAX) 88 errx(1, "pathname too long %s", snapname); 89 cp = strrchr(snapname, '/'); 90 if (cp == NULL) { 91 strlcpy(path, ".", PATH_MAX); 92 } else if (cp == snapname) { 93 strlcpy(path, "/", PATH_MAX); 94 } else { 95 strlcpy(path, snapname, cp - snapname + 1); 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"); 114 115 iov = NULL; 116 iovlen = 0; 117 build_iovec(&iov, &iovlen, "fstype", "ffs", 4); 118 build_iovec(&iov, &iovlen, "from", snapname, (size_t)-1); 119 build_iovec(&iov, &iovlen, "fspath", stfsbuf.f_mntonname, (size_t)-1); 120 build_iovec(&iov, &iovlen, "errmsg", errmsg, sizeof(errmsg)); 121 build_iovec(&iov, &iovlen, "update", NULL, 0); 122 build_iovec(&iov, &iovlen, "snapshot", NULL, 0); 123 124 *errmsg = '\0'; 125 if (nmount(iov, iovlen, stfsbuf.f_flags) < 0) { 126 errmsg[sizeof(errmsg) - 1] = '\0'; 127 err(1, "Cannot create snapshot %s%s%s", snapname, 128 *errmsg != '\0' ? ": " : "", errmsg); 129 } 130 if ((fd = open(snapname, O_RDONLY)) < 0) 131 err(1, "Cannot open %s", snapname); 132 if (fstat(fd, &stbuf) != 0) 133 err(1, "Cannot stat %s", snapname); 134 if ((stbuf.st_flags & SF_SNAPSHOT) == 0) 135 errx(1, "File %s is not a snapshot", snapname); 136 if (fchown(fd, -1, grp->gr_gid) != 0) 137 err(1, "Cannot chown %s", snapname); 138 if (fchmod(fd, S_IRUSR | S_IRGRP) != 0) 139 err(1, "Cannot chmod %s", snapname); 140 141 exit(EXIT_SUCCESS); 142} 143