Home | History | Annotate | Line # | Download | only in libzfs
fsshare.c revision 1.3.2.1
      1  1.3.2.1  christos /*	$NetBSD: fsshare.c,v 1.3.2.1 2019/06/10 21:52:03 christos Exp $	*/
      2      1.1      haad 
      3      1.1      haad /*-
      4      1.1      haad  * Copyright (c) 2007 Pawel Jakub Dawidek <pjd (at) FreeBSD.org>
      5      1.1      haad  * All rights reserved.
      6      1.1      haad  *
      7      1.1      haad  * Redistribution and use in source and binary forms, with or without
      8      1.1      haad  * modification, are permitted provided that the following conditions
      9      1.1      haad  * are met:
     10      1.1      haad  * 1. Redistributions of source code must retain the above copyright
     11      1.1      haad  *    notice, this list of conditions and the following disclaimer.
     12      1.1      haad  * 2. Redistributions in binary form must reproduce the above copyright
     13      1.1      haad  *    notice, this list of conditions and the following disclaimer in the
     14      1.1      haad  *    documentation and/or other materials provided with the distribution.
     15      1.1      haad  *
     16      1.1      haad  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
     17      1.1      haad  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18      1.1      haad  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19      1.1      haad  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
     20      1.1      haad  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21      1.1      haad  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22      1.1      haad  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23      1.1      haad  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24      1.1      haad  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25      1.1      haad  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26      1.1      haad  * SUCH DAMAGE.
     27      1.1      haad  */
     28      1.1      haad 
     29      1.1      haad #include <sys/cdefs.h>
     30      1.3       chs /* __FBSDID("$FreeBSD: head/cddl/compat/opensolaris/misc/fsshare.c 299342 2016-05-10 07:50:57Z bapt $"); */
     31      1.3       chs 
     32  1.3.2.1  christos __RCSID("$NetBSD: fsshare.c,v 1.3.2.1 2019/06/10 21:52:03 christos Exp $");
     33      1.1      haad 
     34      1.1      haad #include <sys/param.h>
     35      1.3       chs 
     36      1.3       chs #include <assert.h>
     37      1.3       chs #include <errno.h>
     38      1.1      haad #include <fcntl.h>
     39      1.3       chs #include <fsshare.h>
     40      1.3       chs #include <pathnames.h>	/* _PATH_MOUNTDPID */
     41      1.1      haad #include <signal.h>
     42      1.3       chs #include <stdio.h>
     43      1.1      haad #include <string.h>
     44      1.3       chs #include <unistd.h>
     45      1.1      haad #include <stdlib.h>
     46      1.1      haad 
     47      1.1      haad #define	FILE_HEADER	"# !!! DO NOT EDIT THIS FILE MANUALLY !!!\n\n"
     48      1.1      haad #define	OPTSSIZE	1024
     49      1.1      haad #define	MAXLINESIZE	(PATH_MAX + OPTSSIZE)
     50      1.1      haad 
     51      1.1      haad static void
     52      1.1      haad restart_mountd(void)
     53      1.1      haad {
     54      1.1      haad 	int pid;
     55      1.1      haad 	FILE *pfh;
     56      1.1      haad 
     57      1.1      haad 	pfh = fopen(_PATH_MOUNTDPID, "r");
     58      1.1      haad 	if (pfh == NULL)
     59      1.1      haad 		return;
     60      1.1      haad 	fscanf(pfh, "%d", &pid);
     61      1.1      haad 	fclose(pfh);
     62      1.1      haad 	kill((pid_t)pid, SIGHUP);
     63      1.1      haad }
     64      1.1      haad 
     65      1.1      haad /*
     66      1.1      haad  * Read one line from a file. Skip comments, empty lines and a line with a
     67      1.1      haad  * mountpoint specified in the 'skip' argument.
     68      1.1      haad  */
     69      1.1      haad static char *
     70      1.2      haad zgetline(FILE *fd, const char *skip)
     71      1.1      haad {
     72      1.1      haad 	static char line[MAXLINESIZE];
     73      1.1      haad 	size_t len, skiplen;
     74      1.1      haad 	char *s, last;
     75      1.1      haad 
     76      1.1      haad 	if (skip != NULL)
     77      1.1      haad 		skiplen = strlen(skip);
     78      1.1      haad 	for (;;) {
     79      1.1      haad 		s = fgets(line, sizeof(line), fd);
     80      1.1      haad 		if (s == NULL)
     81      1.1      haad 			return (NULL);
     82      1.1      haad 		/* Skip empty lines and comments. */
     83      1.1      haad 		if (line[0] == '\n' || line[0] == '#')
     84      1.1      haad 			continue;
     85      1.1      haad 		len = strlen(line);
     86      1.1      haad 		if (line[len - 1] == '\n')
     87      1.1      haad 			line[len - 1] = '\0';
     88      1.1      haad 		last = line[skiplen];
     89      1.1      haad 		/* Skip the given mountpoint. */
     90      1.1      haad 		if (skip != NULL && strncmp(skip, line, skiplen) == 0 &&
     91      1.1      haad 		    (last == '\t' || last == ' ' || last == '\0')) {
     92      1.1      haad 			continue;
     93      1.1      haad 		}
     94      1.1      haad 		break;
     95      1.1      haad 	}
     96      1.1      haad 	return (line);
     97      1.1      haad }
     98      1.1      haad 
     99      1.1      haad /*
    100      1.1      haad  * Function translate options to a format acceptable by exports(5), eg.
    101      1.1      haad  *
    102      1.1      haad  *	-ro -network=192.168.0.0 -mask=255.255.255.0 -maproot=0 freefall.freebsd.org 69.147.83.54
    103      1.1      haad  *
    104      1.1      haad  * Accepted input formats:
    105      1.1      haad  *
    106      1.1      haad  *	ro,network=192.168.0.0,mask=255.255.255.0,maproot=0,freefall.freebsd.org
    107      1.1      haad  *	ro network=192.168.0.0 mask=255.255.255.0 maproot=0 freefall.freebsd.org
    108      1.1      haad  *	-ro,-network=192.168.0.0,-mask=255.255.255.0,-maproot=0,freefall.freebsd.org
    109      1.1      haad  *	-ro -network=192.168.0.0 -mask=255.255.255.0 -maproot=0 freefall.freebsd.org
    110      1.1      haad  *
    111      1.1      haad  * Recognized keywords:
    112      1.1      haad  *
    113      1.1      haad  *	ro, maproot, mapall, mask, network, alldirs, public, webnfs, index, quiet
    114      1.1      haad  *
    115      1.1      haad  */
    116      1.1      haad static const char *known_opts[] = { "ro", "maproot", "mapall", "mask",
    117      1.1      haad     "network", "alldirs", "public", "webnfs", "index", "quiet", NULL };
    118      1.1      haad static char *
    119      1.1      haad translate_opts(const char *shareopts)
    120      1.1      haad {
    121      1.1      haad 	static char newopts[OPTSSIZE];
    122      1.1      haad 	char oldopts[OPTSSIZE];
    123      1.1      haad 	char *o, *s = NULL;
    124      1.1      haad 	unsigned int i;
    125      1.1      haad 	size_t len;
    126      1.1      haad 
    127      1.1      haad 	strlcpy(oldopts, shareopts, sizeof(oldopts));
    128      1.1      haad 	newopts[0] = '\0';
    129      1.1      haad 	s = oldopts;
    130      1.1      haad 	while ((o = strsep(&s, "-, ")) != NULL) {
    131      1.1      haad 		if (o[0] == '\0')
    132      1.1      haad 			continue;
    133      1.1      haad 		for (i = 0; known_opts[i] != NULL; i++) {
    134      1.1      haad 			len = strlen(known_opts[i]);
    135      1.1      haad 			if (strncmp(known_opts[i], o, len) == 0 &&
    136      1.1      haad 			    (o[len] == '\0' || o[len] == '=')) {
    137      1.1      haad 				strlcat(newopts, "-", sizeof(newopts));
    138      1.1      haad 				break;
    139      1.1      haad 			}
    140      1.1      haad 		}
    141      1.1      haad 		strlcat(newopts, o, sizeof(newopts));
    142      1.1      haad 		strlcat(newopts, " ", sizeof(newopts));
    143      1.1      haad 	}
    144      1.1      haad 	return (newopts);
    145      1.1      haad }
    146      1.1      haad 
    147      1.1      haad static int
    148      1.1      haad fsshare_main(const char *file, const char *mountpoint, const char *shareopts,
    149      1.1      haad     int share)
    150      1.1      haad {
    151      1.1      haad 	char tmpfile[PATH_MAX];
    152      1.1      haad 	char *line;
    153      1.1      haad 	FILE *newfd, *oldfd;
    154      1.1      haad 	int fd, error;
    155      1.1      haad 
    156      1.1      haad 	newfd = oldfd = NULL;
    157      1.1      haad 	error = 0;
    158      1.1      haad 
    159      1.1      haad 	/*
    160      1.1      haad 	 * Create temporary file in the same directory, so we can atomically
    161      1.1      haad 	 * rename it.
    162      1.1      haad 	 */
    163      1.1      haad 	if (strlcpy(tmpfile, file, sizeof(tmpfile)) >= sizeof(tmpfile))
    164      1.1      haad 		return (ENAMETOOLONG);
    165      1.1      haad 	if (strlcat(tmpfile, ".XXXXXXXX", sizeof(tmpfile)) >= sizeof(tmpfile))
    166      1.1      haad 		return (ENAMETOOLONG);
    167      1.1      haad 	fd = mkstemp(tmpfile);
    168      1.1      haad 	if (fd == -1)
    169      1.1      haad 		return (errno);
    170      1.1      haad 	/*
    171      1.1      haad 	 * File name is random, so we don't really need file lock now, but it
    172      1.1      haad 	 * will be needed after rename(2).
    173      1.1      haad 	 */
    174      1.1      haad 	error = flock(fd, LOCK_EX);
    175      1.1      haad 	assert(error == 0 || (error == -1 && errno == EOPNOTSUPP));
    176      1.1      haad 	newfd = fdopen(fd, "r+");
    177      1.1      haad 	assert(newfd != NULL);
    178      1.1      haad 	/* Open old exports file. */
    179      1.1      haad 	oldfd = fopen(file, "r");
    180      1.1      haad 	if (oldfd == NULL) {
    181      1.1      haad 		if (share) {
    182      1.1      haad 			if (errno != ENOENT) {
    183      1.1      haad 				error = errno;
    184      1.1      haad 				goto out;
    185      1.1      haad 			}
    186      1.1      haad 		} else {
    187      1.1      haad 			/* If there is no exports file, ignore the error. */
    188      1.1      haad 			if (errno == ENOENT)
    189      1.1      haad 				errno = 0;
    190      1.1      haad 			error = errno;
    191      1.1      haad 			goto out;
    192      1.1      haad 		}
    193      1.1      haad 	} else {
    194      1.1      haad 		error = flock(fileno(oldfd), LOCK_EX);
    195      1.1      haad 		assert(error == 0 || (error == -1 && errno == EOPNOTSUPP));
    196      1.1      haad 		error = 0;
    197      1.1      haad 	}
    198      1.1      haad 
    199      1.1      haad 	/* Place big, fat warning at the begining of the file. */
    200      1.1      haad 	fprintf(newfd, "%s", FILE_HEADER);
    201      1.2      haad 	while (oldfd != NULL && (line = zgetline(oldfd, mountpoint)) != NULL)
    202      1.1      haad 		fprintf(newfd, "%s\n", line);
    203      1.1      haad 	if (oldfd != NULL && ferror(oldfd) != 0) {
    204      1.1      haad 		error = ferror(oldfd);
    205      1.1      haad 		goto out;
    206      1.1      haad 	}
    207      1.1      haad 	if (ferror(newfd) != 0) {
    208      1.1      haad 		error = ferror(newfd);
    209      1.1      haad 		goto out;
    210      1.1      haad 	}
    211      1.1      haad 	if (share) {
    212      1.1      haad 		fprintf(newfd, "%s\t%s\n", mountpoint,
    213      1.1      haad 		    translate_opts(shareopts));
    214      1.1      haad 	}
    215      1.1      haad 
    216      1.1      haad out:
    217      1.1      haad 	if (error != 0)
    218      1.1      haad 		unlink(tmpfile);
    219      1.1      haad 	else {
    220      1.1      haad 		if (rename(tmpfile, file) == -1) {
    221      1.1      haad 			error = errno;
    222      1.1      haad 			unlink(tmpfile);
    223      1.1      haad 		} else {
    224      1.1      haad 			/*
    225      1.1      haad 			 * Send SIGHUP to mountd, but unlock exports file later.
    226      1.1      haad 			 */
    227      1.1      haad 			restart_mountd();
    228      1.1      haad 		}
    229      1.1      haad 	}
    230      1.1      haad 	if (oldfd != NULL) {
    231      1.1      haad 		flock(fileno(oldfd), LOCK_UN);
    232      1.1      haad 		fclose(oldfd);
    233      1.1      haad 	}
    234      1.1      haad 	if (newfd != NULL) {
    235      1.1      haad 		flock(fileno(newfd), LOCK_UN);
    236      1.1      haad 		fclose(newfd);
    237      1.1      haad 	}
    238      1.1      haad 	return (error);
    239      1.1      haad }
    240      1.1      haad 
    241      1.1      haad /*
    242      1.1      haad  * Add the given mountpoint to the given exports file.
    243      1.1      haad  */
    244      1.1      haad int
    245      1.1      haad fsshare(const char *file, const char *mountpoint, const char *shareopts)
    246      1.1      haad {
    247      1.1      haad 
    248      1.1      haad 	return (fsshare_main(file, mountpoint, shareopts, 1));
    249      1.1      haad }
    250      1.1      haad 
    251      1.1      haad /*
    252      1.1      haad  * Remove the given mountpoint from the given exports file.
    253      1.1      haad  */
    254      1.1      haad int
    255      1.1      haad fsunshare(const char *file, const char *mountpoint)
    256      1.1      haad {
    257      1.1      haad 
    258      1.1      haad 	return (fsshare_main(file, mountpoint, NULL, 0));
    259      1.1      haad }
    260