17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*a237e38eSth  * Common Development and Distribution License (the "License").
6*a237e38eSth  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21*a237e38eSth 
227c478bd9Sstevel@tonic-gate /*
23*a237e38eSth  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24*a237e38eSth  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #include <stdio.h>
287c478bd9Sstevel@tonic-gate #include <stdlib.h>
297c478bd9Sstevel@tonic-gate #include <sys/types.h>
307c478bd9Sstevel@tonic-gate #include <string.h>
317c478bd9Sstevel@tonic-gate #include <syslog.h>
327c478bd9Sstevel@tonic-gate #include <sys/param.h>
337c478bd9Sstevel@tonic-gate #include <sys/stat.h>
347c478bd9Sstevel@tonic-gate #include <sys/file.h>
357c478bd9Sstevel@tonic-gate #include <sys/time.h>
367c478bd9Sstevel@tonic-gate #include <errno.h>
377c478bd9Sstevel@tonic-gate #include <rpcsvc/mount.h>
387c478bd9Sstevel@tonic-gate #include <sys/pathconf.h>
397c478bd9Sstevel@tonic-gate #include <sys/systeminfo.h>
407c478bd9Sstevel@tonic-gate #include <sys/utsname.h>
417c478bd9Sstevel@tonic-gate #include <signal.h>
427c478bd9Sstevel@tonic-gate #include <locale.h>
437c478bd9Sstevel@tonic-gate #include <unistd.h>
447c478bd9Sstevel@tonic-gate #include <thread.h>
45*a237e38eSth #include <sharefs/share.h>
46*a237e38eSth #include <sharefs/sharetab.h>
477c478bd9Sstevel@tonic-gate #include "../lib/sharetab.h"
487c478bd9Sstevel@tonic-gate #include "mountd.h"
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate static void freeexports(struct exportnode *);
517c478bd9Sstevel@tonic-gate static struct groupnode **newgroup(char *, struct groupnode **);
527c478bd9Sstevel@tonic-gate static struct exportnode **newexport(char *, struct groupnode *,
537c478bd9Sstevel@tonic-gate 						struct exportnode **);
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate static char *optlist[] = {
567c478bd9Sstevel@tonic-gate #define	OPT_RO		0
577c478bd9Sstevel@tonic-gate 	SHOPT_RO,
587c478bd9Sstevel@tonic-gate #define	OPT_RW		1
597c478bd9Sstevel@tonic-gate 	SHOPT_RW,
607c478bd9Sstevel@tonic-gate 	NULL
617c478bd9Sstevel@tonic-gate };
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate /*
647c478bd9Sstevel@tonic-gate  * Send current export list to a client
657c478bd9Sstevel@tonic-gate  */
667c478bd9Sstevel@tonic-gate void
export(struct svc_req * rqstp)677c478bd9Sstevel@tonic-gate export(struct svc_req *rqstp)
687c478bd9Sstevel@tonic-gate {
697c478bd9Sstevel@tonic-gate 	SVCXPRT *transp;
707c478bd9Sstevel@tonic-gate 	struct exportnode *exportlist;
717c478bd9Sstevel@tonic-gate 	struct exportnode **tail;
727c478bd9Sstevel@tonic-gate 	struct groupnode *groups;
737c478bd9Sstevel@tonic-gate 	struct groupnode **grtail;
747c478bd9Sstevel@tonic-gate 	struct share *sh;
757c478bd9Sstevel@tonic-gate 	struct sh_list *shp;
767c478bd9Sstevel@tonic-gate 	char *gr, *p, *opts, *val, *lasts;
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate 	int export_to_everyone;
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate 	transp = rqstp->rq_xprt;
817c478bd9Sstevel@tonic-gate 	if (!svc_getargs(transp, xdr_void, NULL)) {
827c478bd9Sstevel@tonic-gate 		svcerr_decode(transp);
837c478bd9Sstevel@tonic-gate 		return;
847c478bd9Sstevel@tonic-gate 	}
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate 	check_sharetab();
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate 	exportlist = NULL;
897c478bd9Sstevel@tonic-gate 	tail = &exportlist;
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate 	(void) rw_rdlock(&sharetab_lock);
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate 	for (shp = share_list; shp; shp = shp->shl_next) {
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate 		groups = NULL;
967c478bd9Sstevel@tonic-gate 		grtail = &groups;
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate 		sh = shp->shl_sh;
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate 		/*
1017c478bd9Sstevel@tonic-gate 		 * Check for "ro" or "rw" list without argument values.  This
1027c478bd9Sstevel@tonic-gate 		 * indicates export to everyone.  Unfortunately, SunOS 4.x
1037c478bd9Sstevel@tonic-gate 		 * automounter uses this, and it is indicated indirectly with
1047c478bd9Sstevel@tonic-gate 		 * 'showmount -e'.
1057c478bd9Sstevel@tonic-gate 		 *
1067c478bd9Sstevel@tonic-gate 		 * If export_to_everyone is 1, then groups should be NULL to
1077c478bd9Sstevel@tonic-gate 		 * indicate export to everyone.
1087c478bd9Sstevel@tonic-gate 		 */
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate 		opts = strdup(sh->sh_opts);
1117c478bd9Sstevel@tonic-gate 		p = opts;
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate 		export_to_everyone = 0;
1157c478bd9Sstevel@tonic-gate 		while (*p) {
1167c478bd9Sstevel@tonic-gate 			switch (getsubopt(&p, optlist, &val)) {
1177c478bd9Sstevel@tonic-gate 			case OPT_RO:
1187c478bd9Sstevel@tonic-gate 			case OPT_RW:
1197c478bd9Sstevel@tonic-gate 				if (val == NULL)
1207c478bd9Sstevel@tonic-gate 					export_to_everyone = 1;
1217c478bd9Sstevel@tonic-gate 				break;
1227c478bd9Sstevel@tonic-gate 			}
1237c478bd9Sstevel@tonic-gate 		}
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate 		free(opts);
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 		if (export_to_everyone == 0) {
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate 			opts = strdup(sh->sh_opts);
1307c478bd9Sstevel@tonic-gate 			p = opts;
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate 			/*
1337c478bd9Sstevel@tonic-gate 			 * Just concatenate all the hostnames/groups
1347c478bd9Sstevel@tonic-gate 			 * from the "ro" and "rw" lists for each flavor.
1357c478bd9Sstevel@tonic-gate 			 * This list is rather meaningless now, but
1367c478bd9Sstevel@tonic-gate 			 * that's what the protocol demands.
1377c478bd9Sstevel@tonic-gate 			 */
1387c478bd9Sstevel@tonic-gate 			while (*p) {
1397c478bd9Sstevel@tonic-gate 				switch (getsubopt(&p, optlist, &val)) {
1407c478bd9Sstevel@tonic-gate 				case OPT_RO:
1417c478bd9Sstevel@tonic-gate 				case OPT_RW:
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate 					while ((gr = strtok_r(val, ":", &lasts))
1447c478bd9Sstevel@tonic-gate 					    != NULL) {
1457c478bd9Sstevel@tonic-gate 						val = NULL;
1467c478bd9Sstevel@tonic-gate 						grtail = newgroup(gr, grtail);
1477c478bd9Sstevel@tonic-gate 					}
1487c478bd9Sstevel@tonic-gate 					break;
1497c478bd9Sstevel@tonic-gate 				}
1507c478bd9Sstevel@tonic-gate 			}
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 			free(opts);
1537c478bd9Sstevel@tonic-gate 		}
1547c478bd9Sstevel@tonic-gate 		tail = newexport(sh->sh_path, groups, tail);
1557c478bd9Sstevel@tonic-gate 	}
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate 	(void) rw_unlock(&sharetab_lock);
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate 	errno = 0;
1607c478bd9Sstevel@tonic-gate 	if (!svc_sendreply(transp, xdr_exports, (char *)&exportlist))
1617c478bd9Sstevel@tonic-gate 		log_cant_reply(transp);
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate 	freeexports(exportlist);
1647c478bd9Sstevel@tonic-gate }
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate static void
freeexports(struct exportnode * ex)1687c478bd9Sstevel@tonic-gate freeexports(struct exportnode *ex)
1697c478bd9Sstevel@tonic-gate {
1707c478bd9Sstevel@tonic-gate 	struct groupnode *groups, *tmpgroups;
1717c478bd9Sstevel@tonic-gate 	struct exportnode *tmpex;
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 	while (ex) {
1747c478bd9Sstevel@tonic-gate 		groups = ex->ex_groups;
1757c478bd9Sstevel@tonic-gate 		while (groups) {
1767c478bd9Sstevel@tonic-gate 			tmpgroups = groups->gr_next;
1777c478bd9Sstevel@tonic-gate 			free(groups->gr_name);
1787c478bd9Sstevel@tonic-gate 			free(groups);
1797c478bd9Sstevel@tonic-gate 			groups = tmpgroups;
1807c478bd9Sstevel@tonic-gate 		}
1817c478bd9Sstevel@tonic-gate 		tmpex = ex->ex_next;
1827c478bd9Sstevel@tonic-gate 		free(ex->ex_dir);
1837c478bd9Sstevel@tonic-gate 		free(ex);
1847c478bd9Sstevel@tonic-gate 		ex = tmpex;
1857c478bd9Sstevel@tonic-gate 	}
1867c478bd9Sstevel@tonic-gate }
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate static struct groupnode **
newgroup(char * grname,struct groupnode ** tail)1907c478bd9Sstevel@tonic-gate newgroup(char *grname, struct groupnode **tail)
1917c478bd9Sstevel@tonic-gate {
1927c478bd9Sstevel@tonic-gate 	struct groupnode *new;
1937c478bd9Sstevel@tonic-gate 	char *newname;
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate 	new = exmalloc(sizeof (*new));
1967c478bd9Sstevel@tonic-gate 	newname = exmalloc(strlen(grname) + 1);
1977c478bd9Sstevel@tonic-gate 	(void) strcpy(newname, grname);
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate 	new->gr_name = newname;
2007c478bd9Sstevel@tonic-gate 	new->gr_next = NULL;
2017c478bd9Sstevel@tonic-gate 	*tail = new;
2027c478bd9Sstevel@tonic-gate 	return (&new->gr_next);
2037c478bd9Sstevel@tonic-gate }
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate static struct exportnode **
newexport(char * grname,struct groupnode * grplist,struct exportnode ** tail)2077c478bd9Sstevel@tonic-gate newexport(char *grname, struct groupnode *grplist, struct exportnode **tail)
2087c478bd9Sstevel@tonic-gate {
2097c478bd9Sstevel@tonic-gate 	struct exportnode *new;
2107c478bd9Sstevel@tonic-gate 	char *newname;
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate 	new = exmalloc(sizeof (*new));
2137c478bd9Sstevel@tonic-gate 	newname = exmalloc(strlen(grname) + 1);
2147c478bd9Sstevel@tonic-gate 	(void) strcpy(newname, grname);
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate 	new->ex_dir = newname;
2177c478bd9Sstevel@tonic-gate 	new->ex_groups = grplist;
2187c478bd9Sstevel@tonic-gate 	new->ex_next = NULL;
2197c478bd9Sstevel@tonic-gate 	*tail = new;
2207c478bd9Sstevel@tonic-gate 	return (&new->ex_next);
2217c478bd9Sstevel@tonic-gate }
222