1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <sys/types.h>
30 #include <string.h>
31 #include <syslog.h>
32 #include <sys/param.h>
33 #include <sys/stat.h>
34 #include <sys/file.h>
35 #include <sys/time.h>
36 #include <errno.h>
37 #include <rpcsvc/mount.h>
38 #include <sys/pathconf.h>
39 #include <sys/systeminfo.h>
40 #include <sys/utsname.h>
41 #include <signal.h>
42 #include <locale.h>
43 #include <unistd.h>
44 #include <thread.h>
45 #include <sharefs/share.h>
46 #include <sharefs/sharetab.h>
47 #include "../lib/sharetab.h"
48 #include "mountd.h"
49 
50 static void freeexports(struct exportnode *);
51 static struct groupnode **newgroup(char *, struct groupnode **);
52 static struct exportnode **newexport(char *, struct groupnode *,
53 						struct exportnode **);
54 
55 static char *optlist[] = {
56 #define	OPT_RO		0
57 	SHOPT_RO,
58 #define	OPT_RW		1
59 	SHOPT_RW,
60 	NULL
61 };
62 
63 /*
64  * Send current export list to a client
65  */
66 void
export(struct svc_req * rqstp)67 export(struct svc_req *rqstp)
68 {
69 	SVCXPRT *transp;
70 	struct exportnode *exportlist;
71 	struct exportnode **tail;
72 	struct groupnode *groups;
73 	struct groupnode **grtail;
74 	struct share *sh;
75 	struct sh_list *shp;
76 	char *gr, *p, *opts, *val, *lasts;
77 
78 	int export_to_everyone;
79 
80 	transp = rqstp->rq_xprt;
81 	if (!svc_getargs(transp, xdr_void, NULL)) {
82 		svcerr_decode(transp);
83 		return;
84 	}
85 
86 	check_sharetab();
87 
88 	exportlist = NULL;
89 	tail = &exportlist;
90 
91 	(void) rw_rdlock(&sharetab_lock);
92 
93 	for (shp = share_list; shp; shp = shp->shl_next) {
94 
95 		groups = NULL;
96 		grtail = &groups;
97 
98 		sh = shp->shl_sh;
99 
100 		/*
101 		 * Check for "ro" or "rw" list without argument values.  This
102 		 * indicates export to everyone.  Unfortunately, SunOS 4.x
103 		 * automounter uses this, and it is indicated indirectly with
104 		 * 'showmount -e'.
105 		 *
106 		 * If export_to_everyone is 1, then groups should be NULL to
107 		 * indicate export to everyone.
108 		 */
109 
110 		opts = strdup(sh->sh_opts);
111 		p = opts;
112 
113 
114 		export_to_everyone = 0;
115 		while (*p) {
116 			switch (getsubopt(&p, optlist, &val)) {
117 			case OPT_RO:
118 			case OPT_RW:
119 				if (val == NULL)
120 					export_to_everyone = 1;
121 				break;
122 			}
123 		}
124 
125 		free(opts);
126 
127 		if (export_to_everyone == 0) {
128 
129 			opts = strdup(sh->sh_opts);
130 			p = opts;
131 
132 			/*
133 			 * Just concatenate all the hostnames/groups
134 			 * from the "ro" and "rw" lists for each flavor.
135 			 * This list is rather meaningless now, but
136 			 * that's what the protocol demands.
137 			 */
138 			while (*p) {
139 				switch (getsubopt(&p, optlist, &val)) {
140 				case OPT_RO:
141 				case OPT_RW:
142 
143 					while ((gr = strtok_r(val, ":", &lasts))
144 					    != NULL) {
145 						val = NULL;
146 						grtail = newgroup(gr, grtail);
147 					}
148 					break;
149 				}
150 			}
151 
152 			free(opts);
153 		}
154 		tail = newexport(sh->sh_path, groups, tail);
155 	}
156 
157 	(void) rw_unlock(&sharetab_lock);
158 
159 	errno = 0;
160 	if (!svc_sendreply(transp, xdr_exports, (char *)&exportlist))
161 		log_cant_reply(transp);
162 
163 	freeexports(exportlist);
164 }
165 
166 
167 static void
freeexports(struct exportnode * ex)168 freeexports(struct exportnode *ex)
169 {
170 	struct groupnode *groups, *tmpgroups;
171 	struct exportnode *tmpex;
172 
173 	while (ex) {
174 		groups = ex->ex_groups;
175 		while (groups) {
176 			tmpgroups = groups->gr_next;
177 			free(groups->gr_name);
178 			free(groups);
179 			groups = tmpgroups;
180 		}
181 		tmpex = ex->ex_next;
182 		free(ex->ex_dir);
183 		free(ex);
184 		ex = tmpex;
185 	}
186 }
187 
188 
189 static struct groupnode **
newgroup(char * grname,struct groupnode ** tail)190 newgroup(char *grname, struct groupnode **tail)
191 {
192 	struct groupnode *new;
193 	char *newname;
194 
195 	new = exmalloc(sizeof (*new));
196 	newname = exmalloc(strlen(grname) + 1);
197 	(void) strcpy(newname, grname);
198 
199 	new->gr_name = newname;
200 	new->gr_next = NULL;
201 	*tail = new;
202 	return (&new->gr_next);
203 }
204 
205 
206 static struct exportnode **
newexport(char * grname,struct groupnode * grplist,struct exportnode ** tail)207 newexport(char *grname, struct groupnode *grplist, struct exportnode **tail)
208 {
209 	struct exportnode *new;
210 	char *newname;
211 
212 	new = exmalloc(sizeof (*new));
213 	newname = exmalloc(strlen(grname) + 1);
214 	(void) strcpy(newname, grname);
215 
216 	new->ex_dir = newname;
217 	new->ex_groups = grplist;
218 	new->ex_next = NULL;
219 	*tail = new;
220 	return (&new->ex_next);
221 }
222