8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / cmd / fs.d / nfs / mountd / exportlist.c
blob9743101e06d68b059233302daebbc9957ff11d31
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
23 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <sys/types.h>
32 #include <string.h>
33 #include <syslog.h>
34 #include <sys/param.h>
35 #include <sys/stat.h>
36 #include <sys/file.h>
37 #include <sys/time.h>
38 #include <errno.h>
39 #include <rpcsvc/mount.h>
40 #include <sys/pathconf.h>
41 #include <sys/systeminfo.h>
42 #include <sys/utsname.h>
43 #include <signal.h>
44 #include <locale.h>
45 #include <unistd.h>
46 #include <thread.h>
47 #include <sharefs/share.h>
48 #include <sharefs/sharetab.h>
49 #include "../lib/sharetab.h"
50 #include "mountd.h"
52 static void freeexports(struct exportnode *);
53 static struct groupnode **newgroup(char *, struct groupnode **);
54 static struct exportnode **newexport(char *, struct groupnode *,
55 struct exportnode **);
57 static char *optlist[] = {
58 #define OPT_RO 0
59 SHOPT_RO,
60 #define OPT_RW 1
61 SHOPT_RW,
62 NULL
66 * Send current export list to a client
68 void
69 export(struct svc_req *rqstp)
71 SVCXPRT *transp;
72 struct exportnode *exportlist;
73 struct exportnode **tail;
74 struct groupnode *groups;
75 struct groupnode **grtail;
76 struct share *sh;
77 struct sh_list *shp;
78 char *gr, *p, *opts, *val, *lasts;
80 int export_to_everyone;
82 transp = rqstp->rq_xprt;
83 if (!svc_getargs(transp, xdr_void, NULL)) {
84 svcerr_decode(transp);
85 return;
88 check_sharetab();
90 exportlist = NULL;
91 tail = &exportlist;
93 (void) rw_rdlock(&sharetab_lock);
95 for (shp = share_list; shp; shp = shp->shl_next) {
97 groups = NULL;
98 grtail = &groups;
100 sh = shp->shl_sh;
103 * Check for "ro" or "rw" list without argument values. This
104 * indicates export to everyone. Unfortunately, SunOS 4.x
105 * automounter uses this, and it is indicated indirectly with
106 * 'showmount -e'.
108 * If export_to_everyone is 1, then groups should be NULL to
109 * indicate export to everyone.
112 opts = strdup(sh->sh_opts);
113 p = opts;
116 export_to_everyone = 0;
117 while (*p) {
118 switch (getsubopt(&p, optlist, &val)) {
119 case OPT_RO:
120 case OPT_RW:
121 if (val == NULL)
122 export_to_everyone = 1;
123 break;
127 free(opts);
129 if (export_to_everyone == 0) {
131 opts = strdup(sh->sh_opts);
132 p = opts;
135 * Just concatenate all the hostnames/groups
136 * from the "ro" and "rw" lists for each flavor.
137 * This list is rather meaningless now, but
138 * that's what the protocol demands.
140 while (*p) {
141 switch (getsubopt(&p, optlist, &val)) {
142 case OPT_RO:
143 case OPT_RW:
145 while ((gr = strtok_r(val, ":", &lasts))
146 != NULL) {
147 val = NULL;
148 grtail = newgroup(gr, grtail);
150 break;
154 free(opts);
156 tail = newexport(sh->sh_path, groups, tail);
159 (void) rw_unlock(&sharetab_lock);
161 errno = 0;
162 if (!svc_sendreply(transp, xdr_exports, (char *)&exportlist))
163 log_cant_reply(transp);
165 freeexports(exportlist);
169 static void
170 freeexports(struct exportnode *ex)
172 struct groupnode *groups, *tmpgroups;
173 struct exportnode *tmpex;
175 while (ex) {
176 groups = ex->ex_groups;
177 while (groups) {
178 tmpgroups = groups->gr_next;
179 free(groups->gr_name);
180 free(groups);
181 groups = tmpgroups;
183 tmpex = ex->ex_next;
184 free(ex->ex_dir);
185 free(ex);
186 ex = tmpex;
191 static struct groupnode **
192 newgroup(char *grname, struct groupnode **tail)
194 struct groupnode *new;
195 char *newname;
197 new = exmalloc(sizeof (*new));
198 newname = exmalloc(strlen(grname) + 1);
199 (void) strcpy(newname, grname);
201 new->gr_name = newname;
202 new->gr_next = NULL;
203 *tail = new;
204 return (&new->gr_next);
208 static struct exportnode **
209 newexport(char *grname, struct groupnode *grplist, struct exportnode **tail)
211 struct exportnode *new;
212 char *newname;
214 new = exmalloc(sizeof (*new));
215 newname = exmalloc(strlen(grname) + 1);
216 (void) strcpy(newname, grname);
218 new->ex_dir = newname;
219 new->ex_groups = grplist;
220 new->ex_next = NULL;
221 *tail = new;
222 return (&new->ex_next);