1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 1999 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
28*0Sstevel@tonic-gate /*	  All Rights Reserved  	*/
29*0Sstevel@tonic-gate 
30*0Sstevel@tonic-gate /*
31*0Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley 4.3 BSD
32*0Sstevel@tonic-gate  * under license from the Regents of the University of California.
33*0Sstevel@tonic-gate  */
34*0Sstevel@tonic-gate 
35*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
36*0Sstevel@tonic-gate 
37*0Sstevel@tonic-gate #include <stdio.h>
38*0Sstevel@tonic-gate #include <stdlib.h>
39*0Sstevel@tonic-gate #include <string.h>
40*0Sstevel@tonic-gate #include <unistd.h>
41*0Sstevel@tonic-gate #include "sharetab.h"
42*0Sstevel@tonic-gate 
43*0Sstevel@tonic-gate static int logging_specified(char *);
44*0Sstevel@tonic-gate 
45*0Sstevel@tonic-gate /*
46*0Sstevel@tonic-gate  * Get an entry from the share table.
47*0Sstevel@tonic-gate  * There should be at least 4 fields:
48*0Sstevel@tonic-gate  *
49*0Sstevel@tonic-gate  * 	pathname  resource  fstype  options  [ description ]
50*0Sstevel@tonic-gate  *
51*0Sstevel@tonic-gate  * A fifth field (description) is optional.
52*0Sstevel@tonic-gate  *
53*0Sstevel@tonic-gate  * Returns:
54*0Sstevel@tonic-gate  *	> 1  valid entry
55*0Sstevel@tonic-gate  *	= 0  end of file
56*0Sstevel@tonic-gate  *	< 0  error
57*0Sstevel@tonic-gate  */
58*0Sstevel@tonic-gate int
59*0Sstevel@tonic-gate getshare(fd, shp)
60*0Sstevel@tonic-gate 	FILE *fd;
61*0Sstevel@tonic-gate 	struct share **shp;
62*0Sstevel@tonic-gate {
63*0Sstevel@tonic-gate 	static char *line = NULL;
64*0Sstevel@tonic-gate 	static struct share *sh = NULL;
65*0Sstevel@tonic-gate 	char *p;
66*0Sstevel@tonic-gate 	char *lasts;
67*0Sstevel@tonic-gate 	char *w = " \t";
68*0Sstevel@tonic-gate 
69*0Sstevel@tonic-gate 	if (line == NULL) {
70*0Sstevel@tonic-gate 		line = (char *)malloc(MAXBUFSIZE+1);
71*0Sstevel@tonic-gate 		if (line == NULL)
72*0Sstevel@tonic-gate 			return (-1);
73*0Sstevel@tonic-gate 	}
74*0Sstevel@tonic-gate 	if (sh == NULL) {
75*0Sstevel@tonic-gate 		sh = (struct share *)malloc(sizeof (*sh));
76*0Sstevel@tonic-gate 		if (sh == NULL)
77*0Sstevel@tonic-gate 			return (-1);
78*0Sstevel@tonic-gate 	}
79*0Sstevel@tonic-gate 
80*0Sstevel@tonic-gate 	p = fgets(line, MAXBUFSIZE, fd);
81*0Sstevel@tonic-gate 	if (p == NULL)
82*0Sstevel@tonic-gate 		return (0);
83*0Sstevel@tonic-gate 	line[strlen(line) - 1] = '\0';
84*0Sstevel@tonic-gate 
85*0Sstevel@tonic-gate 	sh->sh_path = (char *)strtok_r(p, w, &lasts);
86*0Sstevel@tonic-gate 	if (sh->sh_path == NULL)
87*0Sstevel@tonic-gate 		return (-3);
88*0Sstevel@tonic-gate 	sh->sh_res = (char *)strtok_r(NULL, w, &lasts);
89*0Sstevel@tonic-gate 	if (sh->sh_res == NULL)
90*0Sstevel@tonic-gate 		return (-3);
91*0Sstevel@tonic-gate 	sh->sh_fstype = (char *)strtok_r(NULL, w, &lasts);
92*0Sstevel@tonic-gate 	if (sh->sh_fstype == NULL)
93*0Sstevel@tonic-gate 		return (-3);
94*0Sstevel@tonic-gate 	sh->sh_opts = (char *)strtok_r(NULL, w, &lasts);
95*0Sstevel@tonic-gate 	if (sh->sh_opts == NULL)
96*0Sstevel@tonic-gate 		return (-3);
97*0Sstevel@tonic-gate 	sh->sh_descr = (char *)strtok_r(NULL, "", &lasts);
98*0Sstevel@tonic-gate 	if (sh->sh_descr == NULL)
99*0Sstevel@tonic-gate 		sh->sh_descr = "";
100*0Sstevel@tonic-gate 
101*0Sstevel@tonic-gate 	*shp = sh;
102*0Sstevel@tonic-gate 	return (1);
103*0Sstevel@tonic-gate }
104*0Sstevel@tonic-gate 
105*0Sstevel@tonic-gate /*
106*0Sstevel@tonic-gate  * Append an entry to the sharetab file.
107*0Sstevel@tonic-gate  */
108*0Sstevel@tonic-gate int
109*0Sstevel@tonic-gate putshare(fd, sh)
110*0Sstevel@tonic-gate 	FILE *fd;
111*0Sstevel@tonic-gate 	struct share *sh;
112*0Sstevel@tonic-gate {
113*0Sstevel@tonic-gate 	int r;
114*0Sstevel@tonic-gate 
115*0Sstevel@tonic-gate 	if (fseek(fd, 0L, 2) < 0)
116*0Sstevel@tonic-gate 		return (-1);
117*0Sstevel@tonic-gate 
118*0Sstevel@tonic-gate 	r = fprintf(fd, "%s\t%s\t%s\t%s\t%s\n",
119*0Sstevel@tonic-gate 		sh->sh_path,
120*0Sstevel@tonic-gate 		sh->sh_res,
121*0Sstevel@tonic-gate 		sh->sh_fstype,
122*0Sstevel@tonic-gate 		sh->sh_opts,
123*0Sstevel@tonic-gate 		sh->sh_descr);
124*0Sstevel@tonic-gate 	return (r);
125*0Sstevel@tonic-gate }
126*0Sstevel@tonic-gate 
127*0Sstevel@tonic-gate /*
128*0Sstevel@tonic-gate  * The entry corresponding to path is removed from the
129*0Sstevel@tonic-gate  * sharetab file.  The file is assumed to be locked.
130*0Sstevel@tonic-gate  * Read the entries into a linked list of share structures
131*0Sstevel@tonic-gate  * minus the entry to be removed.  Then truncate the sharetab
132*0Sstevel@tonic-gate  * file and write almost all of it back to the file from the
133*0Sstevel@tonic-gate  * linked list.
134*0Sstevel@tonic-gate  *
135*0Sstevel@tonic-gate  * If logging information is requested then 'logging' is set
136*0Sstevel@tonic-gate  * to non-zero if the entry is shared with logging enabled.
137*0Sstevel@tonic-gate  *
138*0Sstevel@tonic-gate  * Note: The file is assumed to be locked.
139*0Sstevel@tonic-gate  */
140*0Sstevel@tonic-gate int
141*0Sstevel@tonic-gate remshare(fd, path, logging)
142*0Sstevel@tonic-gate 	FILE *fd;
143*0Sstevel@tonic-gate 	char *path;
144*0Sstevel@tonic-gate 	int *logging;
145*0Sstevel@tonic-gate {
146*0Sstevel@tonic-gate 	struct share *sh_tmp;
147*0Sstevel@tonic-gate 	struct shl {			/* the linked list */
148*0Sstevel@tonic-gate 		struct shl   *shl_next;
149*0Sstevel@tonic-gate 		struct share *shl_sh;
150*0Sstevel@tonic-gate 	};
151*0Sstevel@tonic-gate 	struct shl *shl_head = NULL;
152*0Sstevel@tonic-gate 	struct shl *shl, *prev, *next;
153*0Sstevel@tonic-gate 	int res, remcnt;
154*0Sstevel@tonic-gate 
155*0Sstevel@tonic-gate 	rewind(fd);
156*0Sstevel@tonic-gate 	remcnt = 0;
157*0Sstevel@tonic-gate 	shl = NULL;
158*0Sstevel@tonic-gate 	while ((res = getshare(fd, &sh_tmp)) > 0) {
159*0Sstevel@tonic-gate 		if (strcmp(path, sh_tmp->sh_path) == 0 ||
160*0Sstevel@tonic-gate 		    strcmp(path, sh_tmp->sh_res)  == 0) {
161*0Sstevel@tonic-gate 			remcnt++;
162*0Sstevel@tonic-gate 			if (logging != NULL)
163*0Sstevel@tonic-gate 				*logging = logging_specified(sh_tmp->sh_opts);
164*0Sstevel@tonic-gate 		} else {
165*0Sstevel@tonic-gate 			prev = shl;
166*0Sstevel@tonic-gate 			shl = (struct shl *)malloc(sizeof (*shl));
167*0Sstevel@tonic-gate 			if (shl == NULL) {
168*0Sstevel@tonic-gate 				res = -1;
169*0Sstevel@tonic-gate 				goto dealloc;
170*0Sstevel@tonic-gate 			}
171*0Sstevel@tonic-gate 			if (shl_head == NULL)
172*0Sstevel@tonic-gate 				shl_head = shl;
173*0Sstevel@tonic-gate 			else
174*0Sstevel@tonic-gate 				prev->shl_next = shl;
175*0Sstevel@tonic-gate 			shl->shl_next = NULL;
176*0Sstevel@tonic-gate 			shl->shl_sh = sharedup(sh_tmp);
177*0Sstevel@tonic-gate 			if (shl->shl_sh == NULL) {
178*0Sstevel@tonic-gate 				res = -3;
179*0Sstevel@tonic-gate 				goto dealloc;
180*0Sstevel@tonic-gate 			}
181*0Sstevel@tonic-gate 		}
182*0Sstevel@tonic-gate 	}
183*0Sstevel@tonic-gate 	if (res < 0)
184*0Sstevel@tonic-gate 		goto dealloc;
185*0Sstevel@tonic-gate 	if (remcnt == 0) {
186*0Sstevel@tonic-gate 		res = 1;	/* nothing removed */
187*0Sstevel@tonic-gate 		goto dealloc;
188*0Sstevel@tonic-gate 	}
189*0Sstevel@tonic-gate 
190*0Sstevel@tonic-gate 	if (ftruncate(fileno(fd), 0) < 0) {
191*0Sstevel@tonic-gate 		res = -2;
192*0Sstevel@tonic-gate 		goto dealloc;
193*0Sstevel@tonic-gate 	}
194*0Sstevel@tonic-gate 
195*0Sstevel@tonic-gate 	for (shl = shl_head; shl; shl = shl->shl_next)
196*0Sstevel@tonic-gate 		putshare(fd, shl->shl_sh);
197*0Sstevel@tonic-gate 	res = 1;
198*0Sstevel@tonic-gate 
199*0Sstevel@tonic-gate dealloc:
200*0Sstevel@tonic-gate 	for (shl = shl_head; shl; shl = next) {
201*0Sstevel@tonic-gate 		/*
202*0Sstevel@tonic-gate 		 * make sure we don't reference sharefree with NULL shl->shl_sh
203*0Sstevel@tonic-gate 		 */
204*0Sstevel@tonic-gate 		if (shl->shl_sh != NULL)
205*0Sstevel@tonic-gate 			sharefree(shl->shl_sh);
206*0Sstevel@tonic-gate 		next = shl->shl_next;
207*0Sstevel@tonic-gate 		free(shl);
208*0Sstevel@tonic-gate 	}
209*0Sstevel@tonic-gate 	return (res);
210*0Sstevel@tonic-gate }
211*0Sstevel@tonic-gate 
212*0Sstevel@tonic-gate struct share *
213*0Sstevel@tonic-gate sharedup(sh)
214*0Sstevel@tonic-gate 	struct share *sh;
215*0Sstevel@tonic-gate {
216*0Sstevel@tonic-gate 	struct share *nsh;
217*0Sstevel@tonic-gate 
218*0Sstevel@tonic-gate 	nsh = (struct share *)malloc(sizeof (*nsh));
219*0Sstevel@tonic-gate 	if (nsh == NULL)
220*0Sstevel@tonic-gate 		return (NULL);
221*0Sstevel@tonic-gate 
222*0Sstevel@tonic-gate 	(void) memset((char *)nsh, 0, sizeof (*nsh));
223*0Sstevel@tonic-gate 	if (sh->sh_path) {
224*0Sstevel@tonic-gate 		nsh->sh_path = strdup(sh->sh_path);
225*0Sstevel@tonic-gate 		if (nsh->sh_path == NULL)
226*0Sstevel@tonic-gate 			goto alloc_failed;
227*0Sstevel@tonic-gate 	}
228*0Sstevel@tonic-gate 
229*0Sstevel@tonic-gate 	if (sh->sh_res) {
230*0Sstevel@tonic-gate 		nsh->sh_res = strdup(sh->sh_res);
231*0Sstevel@tonic-gate 		if (nsh->sh_res == NULL)
232*0Sstevel@tonic-gate 			goto alloc_failed;
233*0Sstevel@tonic-gate 	}
234*0Sstevel@tonic-gate 	if (sh->sh_fstype) {
235*0Sstevel@tonic-gate 		nsh->sh_fstype = strdup(sh->sh_fstype);
236*0Sstevel@tonic-gate 		if (nsh->sh_fstype == NULL)
237*0Sstevel@tonic-gate 			goto alloc_failed;
238*0Sstevel@tonic-gate 	}
239*0Sstevel@tonic-gate 	if (sh->sh_opts) {
240*0Sstevel@tonic-gate 		nsh->sh_opts = strdup(sh->sh_opts);
241*0Sstevel@tonic-gate 		if (nsh->sh_opts == NULL)
242*0Sstevel@tonic-gate 			goto alloc_failed;
243*0Sstevel@tonic-gate 	}
244*0Sstevel@tonic-gate 	if (sh->sh_descr) {
245*0Sstevel@tonic-gate 		nsh->sh_descr = strdup(sh->sh_descr);
246*0Sstevel@tonic-gate 		if (nsh->sh_descr == NULL)
247*0Sstevel@tonic-gate 			goto alloc_failed;
248*0Sstevel@tonic-gate 	}
249*0Sstevel@tonic-gate 	return (nsh);
250*0Sstevel@tonic-gate 
251*0Sstevel@tonic-gate alloc_failed:
252*0Sstevel@tonic-gate 	sharefree(nsh);
253*0Sstevel@tonic-gate 	return (NULL);
254*0Sstevel@tonic-gate }
255*0Sstevel@tonic-gate 
256*0Sstevel@tonic-gate void
257*0Sstevel@tonic-gate sharefree(sh)
258*0Sstevel@tonic-gate 	struct share *sh;
259*0Sstevel@tonic-gate {
260*0Sstevel@tonic-gate 	if (sh->sh_path != NULL)
261*0Sstevel@tonic-gate 		free(sh->sh_path);
262*0Sstevel@tonic-gate 	if (sh->sh_res != NULL)
263*0Sstevel@tonic-gate 		free(sh->sh_res);
264*0Sstevel@tonic-gate 	if (sh->sh_fstype != NULL)
265*0Sstevel@tonic-gate 		free(sh->sh_fstype);
266*0Sstevel@tonic-gate 	if (sh->sh_opts != NULL)
267*0Sstevel@tonic-gate 		free(sh->sh_opts);
268*0Sstevel@tonic-gate 	if (sh->sh_descr != NULL)
269*0Sstevel@tonic-gate 		free(sh->sh_descr);
270*0Sstevel@tonic-gate 	free(sh);
271*0Sstevel@tonic-gate }
272*0Sstevel@tonic-gate 
273*0Sstevel@tonic-gate /*
274*0Sstevel@tonic-gate  * Return the value after "=" for option "opt"
275*0Sstevel@tonic-gate  * in option string "optlist". Caller must
276*0Sstevel@tonic-gate  * free returned value.
277*0Sstevel@tonic-gate  */
278*0Sstevel@tonic-gate char *
279*0Sstevel@tonic-gate getshareopt(optlist, opt)
280*0Sstevel@tonic-gate 	char *optlist, *opt;
281*0Sstevel@tonic-gate {
282*0Sstevel@tonic-gate 	char *p, *pe;
283*0Sstevel@tonic-gate 	char *b;
284*0Sstevel@tonic-gate 	char *bb;
285*0Sstevel@tonic-gate 	char *lasts;
286*0Sstevel@tonic-gate 	char *val = NULL;
287*0Sstevel@tonic-gate 
288*0Sstevel@tonic-gate 	b = bb = strdup(optlist);
289*0Sstevel@tonic-gate 	if (b == NULL)
290*0Sstevel@tonic-gate 		return (NULL);
291*0Sstevel@tonic-gate 
292*0Sstevel@tonic-gate 	while (p = (char *)strtok_r(b, ",", &lasts)) {
293*0Sstevel@tonic-gate 		b = NULL;
294*0Sstevel@tonic-gate 		if (pe = strchr(p, '=')) {
295*0Sstevel@tonic-gate 			*pe = '\0';
296*0Sstevel@tonic-gate 			if (strcmp(opt, p) == 0) {
297*0Sstevel@tonic-gate 				val = strdup(pe + 1);
298*0Sstevel@tonic-gate 				goto done;
299*0Sstevel@tonic-gate 			}
300*0Sstevel@tonic-gate 		}
301*0Sstevel@tonic-gate 		if (strcmp(opt, p) == 0) {
302*0Sstevel@tonic-gate 			val = strdup("");
303*0Sstevel@tonic-gate 			goto done;
304*0Sstevel@tonic-gate 		}
305*0Sstevel@tonic-gate 	}
306*0Sstevel@tonic-gate done:
307*0Sstevel@tonic-gate 	free(bb);
308*0Sstevel@tonic-gate 	return (val);
309*0Sstevel@tonic-gate }
310*0Sstevel@tonic-gate 
311*0Sstevel@tonic-gate /*
312*0Sstevel@tonic-gate  * Return 1 if the "log" option was specified in the optlist.
313*0Sstevel@tonic-gate  * Return 0 otherwise.
314*0Sstevel@tonic-gate  */
315*0Sstevel@tonic-gate static int
316*0Sstevel@tonic-gate logging_specified(optlist)
317*0Sstevel@tonic-gate 	char *optlist;
318*0Sstevel@tonic-gate {
319*0Sstevel@tonic-gate 	char *p;
320*0Sstevel@tonic-gate 	char *b, *bb, *lasts;
321*0Sstevel@tonic-gate 	int specified = 0;
322*0Sstevel@tonic-gate 
323*0Sstevel@tonic-gate 	b = bb = strdup(optlist);
324*0Sstevel@tonic-gate 	if (b == NULL)
325*0Sstevel@tonic-gate 		return (0);
326*0Sstevel@tonic-gate 
327*0Sstevel@tonic-gate 	while (p = (char *)strtok_r(b, ",", &lasts)) {
328*0Sstevel@tonic-gate 		b = NULL;
329*0Sstevel@tonic-gate 		if (strncmp(p, "log", 3) == 0)
330*0Sstevel@tonic-gate 			specified++;
331*0Sstevel@tonic-gate 	}
332*0Sstevel@tonic-gate 
333*0Sstevel@tonic-gate 	free(bb);
334*0Sstevel@tonic-gate 	return (specified ? 1 : 0);
335*0Sstevel@tonic-gate }
336