xref: /onnv-gate/usr/src/cmd/fs.d/nfs/lib/nfslog_config.c (revision 0:68f95e015346)
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 (c) 1999 by Sun Microsystems, Inc.
24*0Sstevel@tonic-gate  * All rights reserved.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #include <ctype.h>
30*0Sstevel@tonic-gate #include <errno.h>
31*0Sstevel@tonic-gate #include <locale.h>
32*0Sstevel@tonic-gate #include <stdarg.h>
33*0Sstevel@tonic-gate #include <stdio.h>
34*0Sstevel@tonic-gate #include <stdlib.h>
35*0Sstevel@tonic-gate #include <strings.h>
36*0Sstevel@tonic-gate #include <string.h>
37*0Sstevel@tonic-gate #include <syslog.h>
38*0Sstevel@tonic-gate #include <nfs/nfs.h>
39*0Sstevel@tonic-gate #include <assert.h>
40*0Sstevel@tonic-gate #include <sys/types.h>
41*0Sstevel@tonic-gate #include <sys/stat.h>
42*0Sstevel@tonic-gate #include <unistd.h>
43*0Sstevel@tonic-gate #include <fcntl.h>
44*0Sstevel@tonic-gate #include "nfslog_config.h"
45*0Sstevel@tonic-gate 
46*0Sstevel@tonic-gate #define	ERROR_BUFSZ	100
47*0Sstevel@tonic-gate 
48*0Sstevel@tonic-gate /*
49*0Sstevel@tonic-gate  * This flag controls where error messages go.
50*0Sstevel@tonic-gate  * Zero means that messages go to stderr.
51*0Sstevel@tonic-gate  * Non-zero means that messages go to syslog.
52*0Sstevel@tonic-gate  */
53*0Sstevel@tonic-gate boolean_t nfsl_errs_to_syslog;
54*0Sstevel@tonic-gate 
55*0Sstevel@tonic-gate /*
56*0Sstevel@tonic-gate  * Pointer to the global entry in the list
57*0Sstevel@tonic-gate  */
58*0Sstevel@tonic-gate static nfsl_config_t *global = NULL;
59*0Sstevel@tonic-gate 
60*0Sstevel@tonic-gate /*
61*0Sstevel@tonic-gate  * Pointer to the raw global entry in the list, this is the
62*0Sstevel@tonic-gate  * global entry without the expanded paths. This is used to
63*0Sstevel@tonic-gate  * complete configurations.
64*0Sstevel@tonic-gate  */
65*0Sstevel@tonic-gate static nfsl_config_t *global_raw = NULL;
66*0Sstevel@tonic-gate 
67*0Sstevel@tonic-gate /*
68*0Sstevel@tonic-gate  * Last modification time to config file.
69*0Sstevel@tonic-gate  */
70*0Sstevel@tonic-gate static timestruc_t config_last_modification = { 0 };
71*0Sstevel@tonic-gate 
72*0Sstevel@tonic-gate /*
73*0Sstevel@tonic-gate  * Whitespace characters to delimit fields in a line.
74*0Sstevel@tonic-gate  */
75*0Sstevel@tonic-gate static const char *whitespace = " \t";
76*0Sstevel@tonic-gate 
77*0Sstevel@tonic-gate static int getconfiglist(nfsl_config_t **, boolean_t);
78*0Sstevel@tonic-gate static nfsl_config_t *create_config(char *, char *, char *, char *, char *,
79*0Sstevel@tonic-gate 			char *, int, boolean_t, int *);
80*0Sstevel@tonic-gate static nfsl_config_t *create_global_raw(int *);
81*0Sstevel@tonic-gate static int update_config(nfsl_config_t *, char *, char *, char *,
82*0Sstevel@tonic-gate 			char *, char *, char *, int, boolean_t, boolean_t);
83*0Sstevel@tonic-gate static int update_field(char **, char *, char *, boolean_t *);
84*0Sstevel@tonic-gate static nfsl_config_t *findconfig(nfsl_config_t **, char *, boolean_t,
85*0Sstevel@tonic-gate 			nfsl_config_t **);
86*0Sstevel@tonic-gate static nfsl_config_t *getlastconfig(nfsl_config_t *);
87*0Sstevel@tonic-gate static void complete_with_global(char **, char **, char **, char **,
88*0Sstevel@tonic-gate 			char **, int *);
89*0Sstevel@tonic-gate #ifdef DEBUG
90*0Sstevel@tonic-gate static void remove_config(nfsl_config_t **, nfsl_config_t *, nfsl_config_t **);
91*0Sstevel@tonic-gate void nfsl_printconfig(nfsl_config_t *);
92*0Sstevel@tonic-gate #endif /* DEBUG */
93*0Sstevel@tonic-gate static char *getline(FILE *, char *, char *, int);
94*0Sstevel@tonic-gate static int get_info(char *, char **, char **, char **, char **, char **,
95*0Sstevel@tonic-gate 			char **, int *);
96*0Sstevel@tonic-gate static void free_config(nfsl_config_t *);
97*0Sstevel@tonic-gate static int is_legal_tag(char *);
98*0Sstevel@tonic-gate static boolean_t is_complete_config(char *, char *, char *, char *);
99*0Sstevel@tonic-gate 
100*0Sstevel@tonic-gate /*
101*0Sstevel@tonic-gate  * Read the configuration file and create a list of configuration
102*0Sstevel@tonic-gate  * parameters.  Returns zero for success or an errno value.
103*0Sstevel@tonic-gate  * The caller is responsible for freeing the returned configlist by calling
104*0Sstevel@tonic-gate  * nfsl_freeconfig_list().
105*0Sstevel@tonic-gate  *
106*0Sstevel@tonic-gate  * If the configuration file does not exist, *listpp points to a config entry
107*0Sstevel@tonic-gate  * containing the hardwired defaults.
108*0Sstevel@tonic-gate  */
109*0Sstevel@tonic-gate int
110*0Sstevel@tonic-gate nfsl_getconfig_list(nfsl_config_t **listpp)
111*0Sstevel@tonic-gate {
112*0Sstevel@tonic-gate 	int error = 0;
113*0Sstevel@tonic-gate 	char *locale;
114*0Sstevel@tonic-gate 
115*0Sstevel@tonic-gate 	/*
116*0Sstevel@tonic-gate 	 * Set the locale correctly so that we can correctly identify
117*0Sstevel@tonic-gate 	 * alphabetic characters.
118*0Sstevel@tonic-gate 	 */
119*0Sstevel@tonic-gate 	if ((locale = getenv("LC_ALL")) != NULL)
120*0Sstevel@tonic-gate 		(void) setlocale(LC_ALL, locale);
121*0Sstevel@tonic-gate 	else if ((locale = getenv("LC_CTYPE")) != NULL)
122*0Sstevel@tonic-gate 		(void) setlocale(LC_CTYPE, locale);
123*0Sstevel@tonic-gate 	else if ((locale = getenv("LANG")) != NULL)
124*0Sstevel@tonic-gate 		(void) setlocale(LC_CTYPE, locale);
125*0Sstevel@tonic-gate 
126*0Sstevel@tonic-gate 	/*
127*0Sstevel@tonic-gate 	 * Allocate 'global_raw' structure, its contents are
128*0Sstevel@tonic-gate 	 * indirectly allocated by create_config().
129*0Sstevel@tonic-gate 	 */
130*0Sstevel@tonic-gate 	assert(global_raw == NULL);
131*0Sstevel@tonic-gate 	global_raw = create_global_raw(&error);
132*0Sstevel@tonic-gate 	if (global_raw == NULL)
133*0Sstevel@tonic-gate 		return (error);
134*0Sstevel@tonic-gate 
135*0Sstevel@tonic-gate 	/*
136*0Sstevel@tonic-gate 	 * Build global entry with hardwired defaults first.
137*0Sstevel@tonic-gate 	 */
138*0Sstevel@tonic-gate 	assert(global == NULL);
139*0Sstevel@tonic-gate 	global = create_config(DEFAULTTAG, DEFAULTDIR, BUFFERPATH, NULL,
140*0Sstevel@tonic-gate 			FHPATH, LOGPATH, TRANSLOG_BASIC, B_TRUE, &error);
141*0Sstevel@tonic-gate 	*listpp = global;
142*0Sstevel@tonic-gate 	if (global == NULL) {
143*0Sstevel@tonic-gate 		free_config(global_raw);
144*0Sstevel@tonic-gate 		return (error);
145*0Sstevel@tonic-gate 	}
146*0Sstevel@tonic-gate 
147*0Sstevel@tonic-gate 	if (error = getconfiglist(listpp, B_FALSE))
148*0Sstevel@tonic-gate 		nfsl_freeconfig_list(listpp);
149*0Sstevel@tonic-gate 	else {
150*0Sstevel@tonic-gate 		assert(global != NULL);
151*0Sstevel@tonic-gate 		/*
152*0Sstevel@tonic-gate 		 * The global entry was replaced with the one in the file,
153*0Sstevel@tonic-gate 		 * clear the UPDATED flag
154*0Sstevel@tonic-gate 		 */
155*0Sstevel@tonic-gate 		global->nc_flags &= ~NC_UPDATED;
156*0Sstevel@tonic-gate 	}
157*0Sstevel@tonic-gate 	return (error);
158*0Sstevel@tonic-gate }
159*0Sstevel@tonic-gate 
160*0Sstevel@tonic-gate /*
161*0Sstevel@tonic-gate  * Allocates memory for the 'global_raw' structure.
162*0Sstevel@tonic-gate  * The actual allocation of values for its components happens in
163*0Sstevel@tonic-gate  * update_config().
164*0Sstevel@tonic-gate  */
165*0Sstevel@tonic-gate static nfsl_config_t *
166*0Sstevel@tonic-gate create_global_raw(int *error)
167*0Sstevel@tonic-gate {
168*0Sstevel@tonic-gate 	nfsl_config_t *p;
169*0Sstevel@tonic-gate 
170*0Sstevel@tonic-gate 	*error = 0;
171*0Sstevel@tonic-gate 	if (p = (nfsl_config_t *)malloc(sizeof (*p)))
172*0Sstevel@tonic-gate 		(void) memset((void *)p, 0, sizeof (*p));
173*0Sstevel@tonic-gate 	else
174*0Sstevel@tonic-gate 		*error = ENOMEM;
175*0Sstevel@tonic-gate 
176*0Sstevel@tonic-gate 	return (p);
177*0Sstevel@tonic-gate }
178*0Sstevel@tonic-gate 
179*0Sstevel@tonic-gate /*
180*0Sstevel@tonic-gate  * Checks if the the configuration file has been modified since we last
181*0Sstevel@tonic-gate  * read it, if not simply returns, otherwise it re-reads it adding new
182*0Sstevel@tonic-gate  * configuration entries. Note that existing entries that no longer
183*0Sstevel@tonic-gate  * exist in the configuration file are not removed. Existing entries
184*0Sstevel@tonic-gate  * that are modified in the configuration file are updated in the list
185*0Sstevel@tonic-gate  * as well.
186*0Sstevel@tonic-gate  * if 'updated' is defined then it is set to TRUE if the list was modified.
187*0Sstevel@tonic-gate  *
188*0Sstevel@tonic-gate  * Note that if an error occurs, the list may be corrupted.
189*0Sstevel@tonic-gate  * It is the responsibility of the caller to free the list.
190*0Sstevel@tonic-gate  * If the configuration file does not exist, we simply return the list
191*0Sstevel@tonic-gate  * that we previously had, log a message and return success.
192*0Sstevel@tonic-gate  */
193*0Sstevel@tonic-gate int
194*0Sstevel@tonic-gate nfsl_checkconfig_list(nfsl_config_t **listpp, boolean_t *updated)
195*0Sstevel@tonic-gate {
196*0Sstevel@tonic-gate 	struct stat st;
197*0Sstevel@tonic-gate 	int error = 0;
198*0Sstevel@tonic-gate 
199*0Sstevel@tonic-gate 	if (updated != NULL)
200*0Sstevel@tonic-gate 		*updated = B_FALSE;
201*0Sstevel@tonic-gate 
202*0Sstevel@tonic-gate 	if (stat(NFSL_CONFIG_FILE_PATH, &st) == -1) {
203*0Sstevel@tonic-gate 		error = errno;
204*0Sstevel@tonic-gate 		if (nfsl_errs_to_syslog) {
205*0Sstevel@tonic-gate 			syslog(LOG_ERR, gettext(
206*0Sstevel@tonic-gate 				"Can't stat %s - %s"), NFSL_CONFIG_FILE_PATH,
207*0Sstevel@tonic-gate 				strerror(error));
208*0Sstevel@tonic-gate 		} else {
209*0Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
210*0Sstevel@tonic-gate 				"Can't stat %s - %s\n"), NFSL_CONFIG_FILE_PATH,
211*0Sstevel@tonic-gate 				strerror(error));
212*0Sstevel@tonic-gate 		}
213*0Sstevel@tonic-gate 		return (0);
214*0Sstevel@tonic-gate 	}
215*0Sstevel@tonic-gate 
216*0Sstevel@tonic-gate 	if (config_last_modification.tv_sec == st.st_mtim.tv_sec &&
217*0Sstevel@tonic-gate 	    config_last_modification.tv_nsec == st.st_mtim.tv_nsec)
218*0Sstevel@tonic-gate 		return (0);
219*0Sstevel@tonic-gate 
220*0Sstevel@tonic-gate 	if (updated != NULL)
221*0Sstevel@tonic-gate 		*updated = B_TRUE;
222*0Sstevel@tonic-gate 
223*0Sstevel@tonic-gate 	return (getconfiglist(listpp, B_TRUE));
224*0Sstevel@tonic-gate }
225*0Sstevel@tonic-gate 
226*0Sstevel@tonic-gate /*
227*0Sstevel@tonic-gate  * Does the real work. Reads the configuration file and creates the
228*0Sstevel@tonic-gate  * list of entries. Assumes that *listpp contains at least one entry.
229*0Sstevel@tonic-gate  * The caller is responsible for freeing any config entries added to
230*0Sstevel@tonic-gate  * the list whether this routine returns an error or not.
231*0Sstevel@tonic-gate  *
232*0Sstevel@tonic-gate  * Returns 0 on success and updates the '*listpp' config list,
233*0Sstevel@tonic-gate  * Returns non-zero error value otherwise.
234*0Sstevel@tonic-gate  */
235*0Sstevel@tonic-gate static int
236*0Sstevel@tonic-gate getconfiglist(nfsl_config_t **listpp, boolean_t updating)
237*0Sstevel@tonic-gate {
238*0Sstevel@tonic-gate 	FILE *fp;
239*0Sstevel@tonic-gate 	int error = 0;
240*0Sstevel@tonic-gate 	nfsl_config_t *listp = NULL, *tail = NULL;
241*0Sstevel@tonic-gate 	char linebuf[MAX_LINESZ];
242*0Sstevel@tonic-gate 	char errorbuf[ERROR_BUFSZ];
243*0Sstevel@tonic-gate 	char *tag, *defaultdir, *bufferpath, *rpclogpath, *fhpath, *logpath;
244*0Sstevel@tonic-gate 	int logformat;
245*0Sstevel@tonic-gate 	flock_t flock;
246*0Sstevel@tonic-gate 	struct stat st;
247*0Sstevel@tonic-gate 
248*0Sstevel@tonic-gate 	fp = fopen(NFSL_CONFIG_FILE_PATH, "r");
249*0Sstevel@tonic-gate 	if (fp == NULL) {
250*0Sstevel@tonic-gate 		if (updating) {
251*0Sstevel@tonic-gate 			(void) sprintf(errorbuf, "Can't open %s",
252*0Sstevel@tonic-gate 				NFSL_CONFIG_FILE_PATH);
253*0Sstevel@tonic-gate 		} else {
254*0Sstevel@tonic-gate 			(void) sprintf(errorbuf,
255*0Sstevel@tonic-gate 				"Can't open %s - using hardwired defaults",
256*0Sstevel@tonic-gate 				NFSL_CONFIG_FILE_PATH);
257*0Sstevel@tonic-gate 		}
258*0Sstevel@tonic-gate 
259*0Sstevel@tonic-gate 		/*
260*0Sstevel@tonic-gate 		 * Use hardwired config.
261*0Sstevel@tonic-gate 		 */
262*0Sstevel@tonic-gate 		if (nfsl_errs_to_syslog)
263*0Sstevel@tonic-gate 			syslog(LOG_ERR, gettext("%s"), errorbuf);
264*0Sstevel@tonic-gate 		else
265*0Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("%s\n"), errorbuf);
266*0Sstevel@tonic-gate 
267*0Sstevel@tonic-gate 		return (0);
268*0Sstevel@tonic-gate 	}
269*0Sstevel@tonic-gate 
270*0Sstevel@tonic-gate 	(void) memset((void *) &flock, 0, sizeof (flock));
271*0Sstevel@tonic-gate 	flock.l_type = F_RDLCK;
272*0Sstevel@tonic-gate 	if (fcntl(fileno(fp), F_SETLKW, &flock) == -1) {
273*0Sstevel@tonic-gate 		error = errno;
274*0Sstevel@tonic-gate 		if (nfsl_errs_to_syslog) {
275*0Sstevel@tonic-gate 			syslog(LOG_ERR, gettext(
276*0Sstevel@tonic-gate 				"Can't lock %s - %s"), NFSL_CONFIG_FILE_PATH,
277*0Sstevel@tonic-gate 				strerror(error));
278*0Sstevel@tonic-gate 		} else {
279*0Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
280*0Sstevel@tonic-gate 				"Can't lock %s - %s\n"), NFSL_CONFIG_FILE_PATH,
281*0Sstevel@tonic-gate 				strerror(error));
282*0Sstevel@tonic-gate 		}
283*0Sstevel@tonic-gate 		goto done;
284*0Sstevel@tonic-gate 	}
285*0Sstevel@tonic-gate 
286*0Sstevel@tonic-gate 	assert (*listpp != NULL);
287*0Sstevel@tonic-gate 	tail = getlastconfig(*listpp);
288*0Sstevel@tonic-gate 
289*0Sstevel@tonic-gate 	while (getline(fp, NFSL_CONFIG_FILE_PATH, linebuf, sizeof (linebuf))) {
290*0Sstevel@tonic-gate 		if (linebuf[0] == '\0') {
291*0Sstevel@tonic-gate 			/*
292*0Sstevel@tonic-gate 			 * ignore lines that exceed max size
293*0Sstevel@tonic-gate 			 */
294*0Sstevel@tonic-gate 			continue;
295*0Sstevel@tonic-gate 		}
296*0Sstevel@tonic-gate 
297*0Sstevel@tonic-gate 		if (error = get_info(linebuf, &tag, &defaultdir, &bufferpath,
298*0Sstevel@tonic-gate 		    &rpclogpath, &fhpath, &logpath, &logformat))
299*0Sstevel@tonic-gate 			break;
300*0Sstevel@tonic-gate 
301*0Sstevel@tonic-gate 		if (listp = findconfig(listpp, tag, B_FALSE, &tail)) {
302*0Sstevel@tonic-gate 			/*
303*0Sstevel@tonic-gate 			 * An entry with the same tag name exists,
304*0Sstevel@tonic-gate 			 * update the fields that changed.
305*0Sstevel@tonic-gate 			 */
306*0Sstevel@tonic-gate 			error = update_config(listp, tag, defaultdir,
307*0Sstevel@tonic-gate 					bufferpath, rpclogpath, fhpath, logpath,
308*0Sstevel@tonic-gate 					logformat, B_TRUE, B_TRUE);
309*0Sstevel@tonic-gate 			if (error)
310*0Sstevel@tonic-gate 				break;
311*0Sstevel@tonic-gate 		} else {
312*0Sstevel@tonic-gate 			/*
313*0Sstevel@tonic-gate 			 * New entry, create it.
314*0Sstevel@tonic-gate 			 */
315*0Sstevel@tonic-gate 			listp = create_config(tag, defaultdir,
316*0Sstevel@tonic-gate 					bufferpath, rpclogpath, fhpath,
317*0Sstevel@tonic-gate 					logpath, logformat, B_TRUE, &error);
318*0Sstevel@tonic-gate 			if (listp == NULL)
319*0Sstevel@tonic-gate 				break;
320*0Sstevel@tonic-gate 
321*0Sstevel@tonic-gate 			if (*listpp == NULL)
322*0Sstevel@tonic-gate 				*listpp = listp;
323*0Sstevel@tonic-gate 			else
324*0Sstevel@tonic-gate 				tail->nc_next = listp;
325*0Sstevel@tonic-gate 			tail = listp;
326*0Sstevel@tonic-gate 		}
327*0Sstevel@tonic-gate 
328*0Sstevel@tonic-gate 		assert(global != NULL);
329*0Sstevel@tonic-gate 	}
330*0Sstevel@tonic-gate 
331*0Sstevel@tonic-gate 	if (!error) {
332*0Sstevel@tonic-gate 		/*
333*0Sstevel@tonic-gate 		 * Get mtime while we have file locked
334*0Sstevel@tonic-gate 		 */
335*0Sstevel@tonic-gate 		if (error = fstat(fileno(fp), &st)) {
336*0Sstevel@tonic-gate 			error = errno;
337*0Sstevel@tonic-gate 			if (nfsl_errs_to_syslog) {
338*0Sstevel@tonic-gate 				syslog(LOG_ERR, gettext(
339*0Sstevel@tonic-gate 				"Can't stat %s - %s"), NFSL_CONFIG_FILE_PATH,
340*0Sstevel@tonic-gate 				strerror(error));
341*0Sstevel@tonic-gate 			} else {
342*0Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
343*0Sstevel@tonic-gate 				"Can't stat %s - %s\n"), NFSL_CONFIG_FILE_PATH,
344*0Sstevel@tonic-gate 				strerror(error));
345*0Sstevel@tonic-gate 			}
346*0Sstevel@tonic-gate 		}
347*0Sstevel@tonic-gate 		config_last_modification = st.st_mtim;
348*0Sstevel@tonic-gate 	}
349*0Sstevel@tonic-gate 
350*0Sstevel@tonic-gate done:
351*0Sstevel@tonic-gate 	(void) fclose(fp);
352*0Sstevel@tonic-gate 	return (error);
353*0Sstevel@tonic-gate }
354*0Sstevel@tonic-gate 
355*0Sstevel@tonic-gate /*
356*0Sstevel@tonic-gate  * Creates the config structure with the values specified by the
357*0Sstevel@tonic-gate  * parameters. If defaultdir has been specified, all relative paths
358*0Sstevel@tonic-gate  * are prepended with this defaultdir.
359*0Sstevel@tonic-gate  * If 'complete' is set then this must represent a complete config entry
360*0Sstevel@tonic-gate  * as specified by is_complete_config(), otherwise no work is perfomed, and
361*0Sstevel@tonic-gate  * NULL is returned.
362*0Sstevel@tonic-gate  *
363*0Sstevel@tonic-gate  * Returns the newly created config structure on success.
364*0Sstevel@tonic-gate  * Returns NULL on failure and sets error to the appropriate error.
365*0Sstevel@tonic-gate  */
366*0Sstevel@tonic-gate static nfsl_config_t *
367*0Sstevel@tonic-gate create_config(
368*0Sstevel@tonic-gate 	char *tag,
369*0Sstevel@tonic-gate 	char *defaultdir,
370*0Sstevel@tonic-gate 	char *bufferpath,
371*0Sstevel@tonic-gate 	char *rpclogpath,
372*0Sstevel@tonic-gate 	char *fhpath,
373*0Sstevel@tonic-gate 	char *logpath,
374*0Sstevel@tonic-gate 	int   logformat,
375*0Sstevel@tonic-gate 	boolean_t complete,
376*0Sstevel@tonic-gate 	int  *error)
377*0Sstevel@tonic-gate {
378*0Sstevel@tonic-gate 	nfsl_config_t *config;
379*0Sstevel@tonic-gate 
380*0Sstevel@tonic-gate 	if ((config = (nfsl_config_t *)malloc(sizeof (*config))) == NULL) {
381*0Sstevel@tonic-gate 		*error = ENOMEM;
382*0Sstevel@tonic-gate 		return (NULL);
383*0Sstevel@tonic-gate 	}
384*0Sstevel@tonic-gate 	(void) memset((void *)config, 0, sizeof (*config));
385*0Sstevel@tonic-gate 
386*0Sstevel@tonic-gate 	*error = update_config(config, tag, defaultdir, bufferpath, rpclogpath,
387*0Sstevel@tonic-gate 			fhpath, logpath, logformat, complete, B_TRUE);
388*0Sstevel@tonic-gate 	if (*error) {
389*0Sstevel@tonic-gate 		free(config);
390*0Sstevel@tonic-gate 		return (NULL);
391*0Sstevel@tonic-gate 	}
392*0Sstevel@tonic-gate 
393*0Sstevel@tonic-gate 	config->nc_flags &= ~NC_UPDATED;	/* This is a new entry */
394*0Sstevel@tonic-gate 
395*0Sstevel@tonic-gate 	return (config);
396*0Sstevel@tonic-gate }
397*0Sstevel@tonic-gate 
398*0Sstevel@tonic-gate 
399*0Sstevel@tonic-gate /*
400*0Sstevel@tonic-gate  * Updates the configuration entry with the new information provided,
401*0Sstevel@tonic-gate  * sets NC_UPDATED to indicate so. The entry is left untouched if all
402*0Sstevel@tonic-gate  * the fields are the same (except for 'nc_rpccookie', 'nc_transcookie'
403*0Sstevel@tonic-gate  * and 'nc_next').
404*0Sstevel@tonic-gate  * Prepends each path component with 'defauldir' if 'prepend' is set.
405*0Sstevel@tonic-gate  *
406*0Sstevel@tonic-gate  * Returns 0 on success, error otherwise.
407*0Sstevel@tonic-gate  * On error, the config entry is left in an inconsistent state.
408*0Sstevel@tonic-gate  * The only thing the caller can really do with it is free it.
409*0Sstevel@tonic-gate  */
410*0Sstevel@tonic-gate static int
411*0Sstevel@tonic-gate update_config(
412*0Sstevel@tonic-gate 	nfsl_config_t *config,
413*0Sstevel@tonic-gate 	char *tag,
414*0Sstevel@tonic-gate 	char *defaultdir,
415*0Sstevel@tonic-gate 	char *bufferpath,
416*0Sstevel@tonic-gate 	char *rpclogpath,
417*0Sstevel@tonic-gate 	char *fhpath,
418*0Sstevel@tonic-gate 	char *logpath,
419*0Sstevel@tonic-gate 	int   logformat,
420*0Sstevel@tonic-gate 	boolean_t complete,
421*0Sstevel@tonic-gate 	boolean_t prepend)
422*0Sstevel@tonic-gate {
423*0Sstevel@tonic-gate 	boolean_t updated, config_updated = B_FALSE;
424*0Sstevel@tonic-gate 	int error = 0;
425*0Sstevel@tonic-gate 
426*0Sstevel@tonic-gate 	if (complete && !is_complete_config(tag, bufferpath, fhpath, logpath)) {
427*0Sstevel@tonic-gate 		/*
428*0Sstevel@tonic-gate 		 * Not a complete entry
429*0Sstevel@tonic-gate 		 */
430*0Sstevel@tonic-gate 		if (nfsl_errs_to_syslog) {
431*0Sstevel@tonic-gate 			syslog(LOG_ERR, gettext(
432*0Sstevel@tonic-gate 			"update_config: \"%s\" not a complete config entry."),
433*0Sstevel@tonic-gate 			tag);
434*0Sstevel@tonic-gate 		} else {
435*0Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
436*0Sstevel@tonic-gate 			"update_config: \"%s\" not a complete config entry.\n"),
437*0Sstevel@tonic-gate 			tag);
438*0Sstevel@tonic-gate 		}
439*0Sstevel@tonic-gate 		return (EINVAL);
440*0Sstevel@tonic-gate 	}
441*0Sstevel@tonic-gate 
442*0Sstevel@tonic-gate 	assert(tag != NULL);
443*0Sstevel@tonic-gate 	if (config->nc_name == NULL) {
444*0Sstevel@tonic-gate 		/*
445*0Sstevel@tonic-gate 		 * New entry
446*0Sstevel@tonic-gate 		 */
447*0Sstevel@tonic-gate 		if ((config->nc_name = strdup(tag)) == NULL) {
448*0Sstevel@tonic-gate 			error = ENOMEM;
449*0Sstevel@tonic-gate 			goto errout;
450*0Sstevel@tonic-gate 		}
451*0Sstevel@tonic-gate 	} else
452*0Sstevel@tonic-gate 		assert(strcmp(config->nc_name, tag) == 0);
453*0Sstevel@tonic-gate 
454*0Sstevel@tonic-gate 	if (error = update_field(
455*0Sstevel@tonic-gate 	    &config->nc_defaultdir, defaultdir, NULL, &updated))
456*0Sstevel@tonic-gate 		goto errout;
457*0Sstevel@tonic-gate 	if (!prepend) {
458*0Sstevel@tonic-gate 		/*
459*0Sstevel@tonic-gate 		 * Do not prepend default directory.
460*0Sstevel@tonic-gate 		 */
461*0Sstevel@tonic-gate 		defaultdir = NULL;
462*0Sstevel@tonic-gate 	}
463*0Sstevel@tonic-gate 	config_updated |= updated;
464*0Sstevel@tonic-gate 	if (error = update_field(
465*0Sstevel@tonic-gate 	    &config->nc_bufferpath, bufferpath, defaultdir, &updated))
466*0Sstevel@tonic-gate 		goto errout;
467*0Sstevel@tonic-gate 	config_updated |= updated;
468*0Sstevel@tonic-gate 	if (error = update_field(
469*0Sstevel@tonic-gate 	    &config->nc_rpclogpath, rpclogpath, defaultdir, &updated))
470*0Sstevel@tonic-gate 		goto errout;
471*0Sstevel@tonic-gate 	config_updated |= updated;
472*0Sstevel@tonic-gate 	if (error = update_field(
473*0Sstevel@tonic-gate 	    &config->nc_fhpath, fhpath, defaultdir, &updated))
474*0Sstevel@tonic-gate 		goto errout;
475*0Sstevel@tonic-gate 	config_updated |= updated;
476*0Sstevel@tonic-gate 	if (error = update_field(
477*0Sstevel@tonic-gate 	    &config->nc_logpath, logpath, defaultdir, &updated))
478*0Sstevel@tonic-gate 		goto errout;
479*0Sstevel@tonic-gate 	config_updated |= updated;
480*0Sstevel@tonic-gate 	updated = (config->nc_logformat != logformat);
481*0Sstevel@tonic-gate 	if (updated)
482*0Sstevel@tonic-gate 		config->nc_logformat = logformat;
483*0Sstevel@tonic-gate 	config_updated |= updated;
484*0Sstevel@tonic-gate 
485*0Sstevel@tonic-gate 	if (config_updated)
486*0Sstevel@tonic-gate 		config->nc_flags |= NC_UPDATED;
487*0Sstevel@tonic-gate 
488*0Sstevel@tonic-gate 	if (strcmp(tag, DEFAULTTAG) == 0) {
489*0Sstevel@tonic-gate 		/*
490*0Sstevel@tonic-gate 		 * Have the default global config point to this entry.
491*0Sstevel@tonic-gate 		 */
492*0Sstevel@tonic-gate 		global = config;
493*0Sstevel@tonic-gate 
494*0Sstevel@tonic-gate 		/*
495*0Sstevel@tonic-gate 		 * Update the global_raw configuration entry.
496*0Sstevel@tonic-gate 		 * Make sure no expanding of paths occurs.
497*0Sstevel@tonic-gate 		 */
498*0Sstevel@tonic-gate 		if (error = update_config(global_raw, DEFAULTRAWTAG, defaultdir,
499*0Sstevel@tonic-gate 			bufferpath, rpclogpath, fhpath, logpath, logformat,
500*0Sstevel@tonic-gate 			complete, B_FALSE))
501*0Sstevel@tonic-gate 				goto errout;
502*0Sstevel@tonic-gate 	}
503*0Sstevel@tonic-gate 
504*0Sstevel@tonic-gate 	return (error);
505*0Sstevel@tonic-gate 
506*0Sstevel@tonic-gate errout:
507*0Sstevel@tonic-gate 	if (nfsl_errs_to_syslog) {
508*0Sstevel@tonic-gate 		syslog(LOG_ERR, gettext(
509*0Sstevel@tonic-gate 			"update_config: Can't process \"%s\" config entry: %s"),
510*0Sstevel@tonic-gate 			tag, strerror(error));
511*0Sstevel@tonic-gate 	} else {
512*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
513*0Sstevel@tonic-gate 		"update_config: Can't process \"%s\" config entry: %s\n"),
514*0Sstevel@tonic-gate 		tag, strerror(error));
515*0Sstevel@tonic-gate 	}
516*0Sstevel@tonic-gate 	return (error);
517*0Sstevel@tonic-gate }
518*0Sstevel@tonic-gate 
519*0Sstevel@tonic-gate /*
520*0Sstevel@tonic-gate  * Prepends 'prependir' to 'new' if 'prependir' is defined.
521*0Sstevel@tonic-gate  * Compares the value of '*old' with 'new', if it has changed,
522*0Sstevel@tonic-gate  * then sets whatever 'old' references equal to 'new'.
523*0Sstevel@tonic-gate  * Returns 0 on success, error otherwise.
524*0Sstevel@tonic-gate  * Sets '*updated' to B_TRUE if field was modified.
525*0Sstevel@tonic-gate  * The value of '*updated' is undefined on error.
526*0Sstevel@tonic-gate  */
527*0Sstevel@tonic-gate static int
528*0Sstevel@tonic-gate update_field(
529*0Sstevel@tonic-gate 	char **old,		/* pointer to config field */
530*0Sstevel@tonic-gate 	char *new,		/* updated value */
531*0Sstevel@tonic-gate 	char *prependdir,	/* prepend this directory to new */
532*0Sstevel@tonic-gate 	boolean_t *updated)	/* field was modified */
533*0Sstevel@tonic-gate {
534*0Sstevel@tonic-gate 	char *tmp_new = NULL;
535*0Sstevel@tonic-gate 	int need_update = 0;
536*0Sstevel@tonic-gate 
537*0Sstevel@tonic-gate 	if (new != NULL) {
538*0Sstevel@tonic-gate 		if (prependdir != NULL && new[0] != '/') {
539*0Sstevel@tonic-gate 			tmp_new = malloc(strlen(prependdir) + strlen(new) + 2);
540*0Sstevel@tonic-gate 			if (tmp_new == NULL)
541*0Sstevel@tonic-gate 				return (ENOMEM);
542*0Sstevel@tonic-gate 			(void) sprintf(tmp_new, "%s/%s", prependdir, new);
543*0Sstevel@tonic-gate 		} else {
544*0Sstevel@tonic-gate 			if ((tmp_new = strdup(new)) == NULL)
545*0Sstevel@tonic-gate 				return (ENOMEM);
546*0Sstevel@tonic-gate 		}
547*0Sstevel@tonic-gate 	}
548*0Sstevel@tonic-gate 
549*0Sstevel@tonic-gate 	if (tmp_new != NULL) {
550*0Sstevel@tonic-gate 		if (*old == NULL)
551*0Sstevel@tonic-gate 			need_update++;
552*0Sstevel@tonic-gate 		else if (strcmp(tmp_new, *old) != 0) {
553*0Sstevel@tonic-gate 			free(*old);
554*0Sstevel@tonic-gate 			need_update++;
555*0Sstevel@tonic-gate 		}
556*0Sstevel@tonic-gate 		if (need_update)
557*0Sstevel@tonic-gate 			*old = tmp_new;
558*0Sstevel@tonic-gate 	} else if (*old != NULL) {
559*0Sstevel@tonic-gate 		need_update++;
560*0Sstevel@tonic-gate 		free(*old);
561*0Sstevel@tonic-gate 		*old = NULL;
562*0Sstevel@tonic-gate 	}
563*0Sstevel@tonic-gate 
564*0Sstevel@tonic-gate 	*updated = need_update != 0;
565*0Sstevel@tonic-gate 	return (0);
566*0Sstevel@tonic-gate }
567*0Sstevel@tonic-gate 
568*0Sstevel@tonic-gate #ifdef DEBUG
569*0Sstevel@tonic-gate /*
570*0Sstevel@tonic-gate  * Removes and frees the 'config' entry from the list
571*0Sstevel@tonic-gate  * pointed to by '*listpp'.
572*0Sstevel@tonic-gate  * No error is reported if the entry does not exist.
573*0Sstevel@tonic-gate  * Updates '*tail' to point to the last item in the list.
574*0Sstevel@tonic-gate  */
575*0Sstevel@tonic-gate static void
576*0Sstevel@tonic-gate remove_config(
577*0Sstevel@tonic-gate 	nfsl_config_t **listpp,
578*0Sstevel@tonic-gate 	nfsl_config_t *config,
579*0Sstevel@tonic-gate 	nfsl_config_t **tail)
580*0Sstevel@tonic-gate {
581*0Sstevel@tonic-gate 	nfsl_config_t *p, *prev;
582*0Sstevel@tonic-gate 
583*0Sstevel@tonic-gate 	prev = *listpp;
584*0Sstevel@tonic-gate 	for (p = *listpp; p != NULL; p = p->nc_next) {
585*0Sstevel@tonic-gate 		if (p == config) {
586*0Sstevel@tonic-gate 			if (p == prev) {
587*0Sstevel@tonic-gate 				/*
588*0Sstevel@tonic-gate 				 * first element of the list
589*0Sstevel@tonic-gate 				 */
590*0Sstevel@tonic-gate 				*listpp = prev->nc_next;
591*0Sstevel@tonic-gate 			} else
592*0Sstevel@tonic-gate 				prev->nc_next = p->nc_next;
593*0Sstevel@tonic-gate 			free_config(p);
594*0Sstevel@tonic-gate 			break;
595*0Sstevel@tonic-gate 		}
596*0Sstevel@tonic-gate 		prev = p;
597*0Sstevel@tonic-gate 	}
598*0Sstevel@tonic-gate 
599*0Sstevel@tonic-gate 	/*
600*0Sstevel@tonic-gate 	 * Find tail of the list.
601*0Sstevel@tonic-gate 	 */
602*0Sstevel@tonic-gate 	for (*tail = prev; (*tail)->nc_next != NULL; *tail = (*tail)->nc_next)
603*0Sstevel@tonic-gate 		;
604*0Sstevel@tonic-gate }
605*0Sstevel@tonic-gate #endif /* DEBUG */
606*0Sstevel@tonic-gate 
607*0Sstevel@tonic-gate static void
608*0Sstevel@tonic-gate free_config(nfsl_config_t *config)
609*0Sstevel@tonic-gate {
610*0Sstevel@tonic-gate 	if (config == NULL)
611*0Sstevel@tonic-gate 		return;
612*0Sstevel@tonic-gate 	if (config->nc_name)
613*0Sstevel@tonic-gate 		free(config->nc_name);
614*0Sstevel@tonic-gate 	if (config->nc_defaultdir)
615*0Sstevel@tonic-gate 		free(config->nc_defaultdir);
616*0Sstevel@tonic-gate 	if (config->nc_bufferpath)
617*0Sstevel@tonic-gate 		free(config->nc_bufferpath);
618*0Sstevel@tonic-gate 	if (config->nc_rpclogpath)
619*0Sstevel@tonic-gate 		free(config->nc_rpclogpath);
620*0Sstevel@tonic-gate 	if (config->nc_fhpath)
621*0Sstevel@tonic-gate 		free(config->nc_fhpath);
622*0Sstevel@tonic-gate 	if (config->nc_logpath)
623*0Sstevel@tonic-gate 		free(config->nc_logpath);
624*0Sstevel@tonic-gate 	if (config == global)
625*0Sstevel@tonic-gate 		global = NULL;
626*0Sstevel@tonic-gate 	if (config == global_raw)
627*0Sstevel@tonic-gate 		global_raw = NULL;
628*0Sstevel@tonic-gate 	free(config);
629*0Sstevel@tonic-gate }
630*0Sstevel@tonic-gate 
631*0Sstevel@tonic-gate void
632*0Sstevel@tonic-gate nfsl_freeconfig_list(nfsl_config_t **listpp)
633*0Sstevel@tonic-gate {
634*0Sstevel@tonic-gate 	nfsl_config_t *next;
635*0Sstevel@tonic-gate 
636*0Sstevel@tonic-gate 	if (*listpp == NULL)
637*0Sstevel@tonic-gate 		return;
638*0Sstevel@tonic-gate 
639*0Sstevel@tonic-gate 	do {
640*0Sstevel@tonic-gate 		next = (*listpp)->nc_next;
641*0Sstevel@tonic-gate 		free_config(*listpp);
642*0Sstevel@tonic-gate 		*listpp = next;
643*0Sstevel@tonic-gate 	} while (*listpp);
644*0Sstevel@tonic-gate 
645*0Sstevel@tonic-gate 	free_config(global_raw);
646*0Sstevel@tonic-gate }
647*0Sstevel@tonic-gate 
648*0Sstevel@tonic-gate /*
649*0Sstevel@tonic-gate  * Returns a pointer to the first instance of 'tag' in the list.
650*0Sstevel@tonic-gate  * If 'remove' is true, then the entry is removed from the list and
651*0Sstevel@tonic-gate  * a pointer to it is returned.
652*0Sstevel@tonic-gate  * If '*tail' is not NULL, then it will point to the last element of
653*0Sstevel@tonic-gate  * the list. Note that this function assumes that *tail already
654*0Sstevel@tonic-gate  * points at the last element of the list.
655*0Sstevel@tonic-gate  * Returns NULL if the entry does not exist.
656*0Sstevel@tonic-gate  */
657*0Sstevel@tonic-gate static nfsl_config_t *
658*0Sstevel@tonic-gate findconfig(
659*0Sstevel@tonic-gate 	nfsl_config_t **listpp,
660*0Sstevel@tonic-gate 	char *tag, boolean_t remove,
661*0Sstevel@tonic-gate 	nfsl_config_t **tail)
662*0Sstevel@tonic-gate {
663*0Sstevel@tonic-gate 	nfsl_config_t *p, *prev;
664*0Sstevel@tonic-gate 
665*0Sstevel@tonic-gate 	prev = *listpp;
666*0Sstevel@tonic-gate 	for (p = *listpp; p != NULL; p = p->nc_next) {
667*0Sstevel@tonic-gate 		if (strcmp(p->nc_name, tag) == 0) {
668*0Sstevel@tonic-gate 			if (remove) {
669*0Sstevel@tonic-gate 				if (p == prev) {
670*0Sstevel@tonic-gate 					/*
671*0Sstevel@tonic-gate 					 * first element of the list
672*0Sstevel@tonic-gate 					 */
673*0Sstevel@tonic-gate 					*listpp = prev->nc_next;
674*0Sstevel@tonic-gate 				} else
675*0Sstevel@tonic-gate 					prev->nc_next = p->nc_next;
676*0Sstevel@tonic-gate 
677*0Sstevel@tonic-gate 				if (tail != NULL && p == *tail) {
678*0Sstevel@tonic-gate 					/*
679*0Sstevel@tonic-gate 					 * Only update *tail if we removed
680*0Sstevel@tonic-gate 					 * the last element of the list, and we
681*0Sstevel@tonic-gate 					 * requested *tail to be updated.
682*0Sstevel@tonic-gate 					 */
683*0Sstevel@tonic-gate 					*tail = prev;
684*0Sstevel@tonic-gate 				}
685*0Sstevel@tonic-gate 			}
686*0Sstevel@tonic-gate 			return (p);
687*0Sstevel@tonic-gate 		}
688*0Sstevel@tonic-gate 		prev = p;
689*0Sstevel@tonic-gate 	}
690*0Sstevel@tonic-gate 
691*0Sstevel@tonic-gate 	return (NULL);
692*0Sstevel@tonic-gate }
693*0Sstevel@tonic-gate 
694*0Sstevel@tonic-gate static nfsl_config_t *
695*0Sstevel@tonic-gate getlastconfig(nfsl_config_t *listp)
696*0Sstevel@tonic-gate {
697*0Sstevel@tonic-gate 	nfsl_config_t *lastp = NULL;
698*0Sstevel@tonic-gate 
699*0Sstevel@tonic-gate 	for (; listp != NULL; listp = listp->nc_next)
700*0Sstevel@tonic-gate 		lastp = listp;
701*0Sstevel@tonic-gate 
702*0Sstevel@tonic-gate 	return (lastp);
703*0Sstevel@tonic-gate }
704*0Sstevel@tonic-gate 
705*0Sstevel@tonic-gate /*
706*0Sstevel@tonic-gate  * Returns a pointer to the first instance of 'tag' in the list.
707*0Sstevel@tonic-gate  * Returns NULL if the entry does not exist.
708*0Sstevel@tonic-gate  * Sets 'error' if the update of the list failed if necessary, and
709*0Sstevel@tonic-gate  * returns NULL.
710*0Sstevel@tonic-gate  */
711*0Sstevel@tonic-gate nfsl_config_t *
712*0Sstevel@tonic-gate nfsl_findconfig(nfsl_config_t *listp, char *tag, int *error)
713*0Sstevel@tonic-gate {
714*0Sstevel@tonic-gate 	nfsl_config_t *config;
715*0Sstevel@tonic-gate 	boolean_t updated;
716*0Sstevel@tonic-gate 
717*0Sstevel@tonic-gate 	*error = 0;
718*0Sstevel@tonic-gate 	config = findconfig(&listp, tag, B_FALSE, (nfsl_config_t **)NULL);
719*0Sstevel@tonic-gate 	if (config == NULL) {
720*0Sstevel@tonic-gate 		/*
721*0Sstevel@tonic-gate 		 * Rebuild our list if the file has changed.
722*0Sstevel@tonic-gate 		 */
723*0Sstevel@tonic-gate 		if (*error = nfsl_checkconfig_list(&listp, &updated)) {
724*0Sstevel@tonic-gate 			/*
725*0Sstevel@tonic-gate 			 * List may be corrupted, notify caller.
726*0Sstevel@tonic-gate 			 */
727*0Sstevel@tonic-gate 			return (NULL);
728*0Sstevel@tonic-gate 		}
729*0Sstevel@tonic-gate 		if (updated) {
730*0Sstevel@tonic-gate 			/*
731*0Sstevel@tonic-gate 			 * Search for tag again.
732*0Sstevel@tonic-gate 			 */
733*0Sstevel@tonic-gate 			config = findconfig(&listp, tag, B_FALSE,
734*0Sstevel@tonic-gate 				(nfsl_config_t **)NULL);
735*0Sstevel@tonic-gate 		}
736*0Sstevel@tonic-gate 	}
737*0Sstevel@tonic-gate 
738*0Sstevel@tonic-gate 	return (config);
739*0Sstevel@tonic-gate }
740*0Sstevel@tonic-gate 
741*0Sstevel@tonic-gate /*
742*0Sstevel@tonic-gate  * Use the raw global values if any of the parameters is not defined.
743*0Sstevel@tonic-gate  */
744*0Sstevel@tonic-gate static void
745*0Sstevel@tonic-gate complete_with_global(
746*0Sstevel@tonic-gate 	char **defaultdir,
747*0Sstevel@tonic-gate 	char **bufferpath,
748*0Sstevel@tonic-gate 	char **rpclogpath,
749*0Sstevel@tonic-gate 	char **fhpath,
750*0Sstevel@tonic-gate 	char **logpath,
751*0Sstevel@tonic-gate 	int  *logformat)
752*0Sstevel@tonic-gate {
753*0Sstevel@tonic-gate 	if (*defaultdir == NULL)
754*0Sstevel@tonic-gate 		*defaultdir = global_raw->nc_defaultdir;
755*0Sstevel@tonic-gate 	if (*bufferpath == NULL)
756*0Sstevel@tonic-gate 		*bufferpath = global_raw->nc_bufferpath;
757*0Sstevel@tonic-gate 	if (*rpclogpath == NULL)
758*0Sstevel@tonic-gate 		*rpclogpath = global_raw->nc_rpclogpath;
759*0Sstevel@tonic-gate 	if (*fhpath == NULL)
760*0Sstevel@tonic-gate 		*fhpath = global_raw->nc_fhpath;
761*0Sstevel@tonic-gate 	if (*logpath == NULL)
762*0Sstevel@tonic-gate 		*logpath = global_raw->nc_logpath;
763*0Sstevel@tonic-gate 	if (*logformat == 0)
764*0Sstevel@tonic-gate 		*logformat = global_raw->nc_logformat;
765*0Sstevel@tonic-gate }
766*0Sstevel@tonic-gate 
767*0Sstevel@tonic-gate /*
768*0Sstevel@tonic-gate  * Parses 'linebuf'. Returns 0 if a valid tag is found, otherwise non-zero.
769*0Sstevel@tonic-gate  * Unknown tokens are silently ignored.
770*0Sstevel@tonic-gate  * It is the responsibility of the caller to make a copy of the non-NULL
771*0Sstevel@tonic-gate  * parameters if they need to be used before linebuf is freed.
772*0Sstevel@tonic-gate  */
773*0Sstevel@tonic-gate static int
774*0Sstevel@tonic-gate get_info(
775*0Sstevel@tonic-gate 	char *linebuf,
776*0Sstevel@tonic-gate 	char **tag,
777*0Sstevel@tonic-gate 	char **defaultdir,
778*0Sstevel@tonic-gate 	char **bufferpath,
779*0Sstevel@tonic-gate 	char **rpclogpath,
780*0Sstevel@tonic-gate 	char **fhpath,
781*0Sstevel@tonic-gate 	char **logpath,
782*0Sstevel@tonic-gate 	int  *logformat)
783*0Sstevel@tonic-gate {
784*0Sstevel@tonic-gate 	char *tok;
785*0Sstevel@tonic-gate 	char *tmp;
786*0Sstevel@tonic-gate 
787*0Sstevel@tonic-gate 	/* tag */
788*0Sstevel@tonic-gate 	*tag = NULL;
789*0Sstevel@tonic-gate 	tok = strtok(linebuf, whitespace);
790*0Sstevel@tonic-gate 	if (tok == NULL)
791*0Sstevel@tonic-gate 		goto badtag;
792*0Sstevel@tonic-gate 	if (!is_legal_tag(tok))
793*0Sstevel@tonic-gate 		goto badtag;
794*0Sstevel@tonic-gate 	*tag = tok;
795*0Sstevel@tonic-gate 
796*0Sstevel@tonic-gate 	*defaultdir = *bufferpath = *rpclogpath = NULL;
797*0Sstevel@tonic-gate 	*fhpath = *logpath = NULL;
798*0Sstevel@tonic-gate 	*logformat = 0;
799*0Sstevel@tonic-gate 
800*0Sstevel@tonic-gate 	while (tok = strtok(NULL, whitespace)) {
801*0Sstevel@tonic-gate 		if (strncmp(tok, "defaultdir=", strlen("defaultdir=")) == 0) {
802*0Sstevel@tonic-gate 			*defaultdir = tok + strlen("defaultdir=");
803*0Sstevel@tonic-gate 		} else if (strncmp(tok, "buffer=", strlen("buffer=")) == 0) {
804*0Sstevel@tonic-gate 			*bufferpath = tok + strlen("buffer=");
805*0Sstevel@tonic-gate 		} else if (strncmp(tok, "rpclog=", strlen("rpclog=")) == 0) {
806*0Sstevel@tonic-gate 			*rpclogpath = tok + strlen("rpclog=");
807*0Sstevel@tonic-gate 		} else if (strncmp(tok, "fhtable=", strlen("fhtable=")) == 0) {
808*0Sstevel@tonic-gate 			*fhpath = tok + strlen("fhtable=");
809*0Sstevel@tonic-gate 		} else if (strncmp(tok, "log=", strlen("log=")) == 0) {
810*0Sstevel@tonic-gate 			*logpath = tok + strlen("log=");
811*0Sstevel@tonic-gate 		} else if (strncmp(tok, "logformat=",
812*0Sstevel@tonic-gate 				strlen("logformat=")) == 0) {
813*0Sstevel@tonic-gate 			tmp = tok + strlen("logformat=");
814*0Sstevel@tonic-gate 			if (strncmp(tmp, "extended", strlen("extended")) == 0) {
815*0Sstevel@tonic-gate 				*logformat = TRANSLOG_EXTENDED;
816*0Sstevel@tonic-gate 			} else {
817*0Sstevel@tonic-gate 				/*
818*0Sstevel@tonic-gate 				 * Use transaction log basic format if
819*0Sstevel@tonic-gate 				 * 'extended' was not specified.
820*0Sstevel@tonic-gate 				 */
821*0Sstevel@tonic-gate 				*logformat = TRANSLOG_BASIC;
822*0Sstevel@tonic-gate 			}
823*0Sstevel@tonic-gate 		}
824*0Sstevel@tonic-gate 	}
825*0Sstevel@tonic-gate 
826*0Sstevel@tonic-gate 	if (strcmp(*tag, DEFAULTTAG) != 0) {
827*0Sstevel@tonic-gate 		/*
828*0Sstevel@tonic-gate 		 * Use global values for fields not specified if
829*0Sstevel@tonic-gate 		 * this tag is not the global tag.
830*0Sstevel@tonic-gate 		 */
831*0Sstevel@tonic-gate 		complete_with_global(defaultdir, bufferpath,
832*0Sstevel@tonic-gate 			rpclogpath, fhpath, logpath, logformat);
833*0Sstevel@tonic-gate 	}
834*0Sstevel@tonic-gate 
835*0Sstevel@tonic-gate 	return (0);
836*0Sstevel@tonic-gate 
837*0Sstevel@tonic-gate badtag:
838*0Sstevel@tonic-gate 	if (nfsl_errs_to_syslog) {
839*0Sstevel@tonic-gate 		syslog(LOG_ERR, gettext(
840*0Sstevel@tonic-gate 			"Bad tag found in config file."));
841*0Sstevel@tonic-gate 	} else {
842*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
843*0Sstevel@tonic-gate 			"Bad tag found in config file.\n"));
844*0Sstevel@tonic-gate 	}
845*0Sstevel@tonic-gate 	return (-1);
846*0Sstevel@tonic-gate }
847*0Sstevel@tonic-gate 
848*0Sstevel@tonic-gate /*
849*0Sstevel@tonic-gate  * Returns True if we have all the elements of a complete configuration
850*0Sstevel@tonic-gate  * entry. A complete configuration has tag, bufferpath, fhpath and logpath
851*0Sstevel@tonic-gate  * defined to non-zero strings.
852*0Sstevel@tonic-gate  */
853*0Sstevel@tonic-gate static boolean_t
854*0Sstevel@tonic-gate is_complete_config(
855*0Sstevel@tonic-gate 	char *tag,
856*0Sstevel@tonic-gate 	char *bufferpath,
857*0Sstevel@tonic-gate 	char *fhpath,
858*0Sstevel@tonic-gate 	char *logpath)
859*0Sstevel@tonic-gate {
860*0Sstevel@tonic-gate 	assert(tag != NULL);
861*0Sstevel@tonic-gate 	assert(strlen(tag) > 0);
862*0Sstevel@tonic-gate 
863*0Sstevel@tonic-gate 	if ((bufferpath != NULL && strlen(bufferpath) > 0) &&
864*0Sstevel@tonic-gate 	    (fhpath != NULL && strlen(fhpath) > 0) &&
865*0Sstevel@tonic-gate 	    (logpath != NULL && strlen(logpath) > 0))
866*0Sstevel@tonic-gate 		return (B_TRUE);
867*0Sstevel@tonic-gate 	return (B_FALSE);
868*0Sstevel@tonic-gate }
869*0Sstevel@tonic-gate 
870*0Sstevel@tonic-gate #ifdef DEBUG
871*0Sstevel@tonic-gate /*
872*0Sstevel@tonic-gate  * Prints the configuration entry to stdout.
873*0Sstevel@tonic-gate  */
874*0Sstevel@tonic-gate void
875*0Sstevel@tonic-gate nfsl_printconfig(nfsl_config_t *config)
876*0Sstevel@tonic-gate {
877*0Sstevel@tonic-gate 	if (config->nc_name)
878*0Sstevel@tonic-gate 		(void) printf("tag=%s\t", config->nc_name);
879*0Sstevel@tonic-gate 	if (config->nc_defaultdir)
880*0Sstevel@tonic-gate 		(void) printf("defaultdir=%s\t", config->nc_defaultdir);
881*0Sstevel@tonic-gate 	if (config->nc_logpath)
882*0Sstevel@tonic-gate 		(void) printf("logpath=%s\t", config->nc_logpath);
883*0Sstevel@tonic-gate 	if (config->nc_fhpath)
884*0Sstevel@tonic-gate 		(void) printf("fhpath=%s\t", config->nc_fhpath);
885*0Sstevel@tonic-gate 	if (config->nc_bufferpath)
886*0Sstevel@tonic-gate 		(void) printf("bufpath=%s\t", config->nc_bufferpath);
887*0Sstevel@tonic-gate 	if (config->nc_rpclogpath)
888*0Sstevel@tonic-gate 		(void) printf("rpclogpath=%s\t", config->nc_rpclogpath);
889*0Sstevel@tonic-gate 	if (config->nc_logformat == TRANSLOG_BASIC)
890*0Sstevel@tonic-gate 		(void) printf("logformat=basic");
891*0Sstevel@tonic-gate 	else if (config->nc_logformat == TRANSLOG_EXTENDED)
892*0Sstevel@tonic-gate 		(void) printf("logformat=extended");
893*0Sstevel@tonic-gate 	else
894*0Sstevel@tonic-gate 		(void) printf("config->nc_logformat=UNKNOWN");
895*0Sstevel@tonic-gate 
896*0Sstevel@tonic-gate 	if (config->nc_flags & NC_UPDATED)
897*0Sstevel@tonic-gate 		(void) printf("\tflags=NC_UPDATED");
898*0Sstevel@tonic-gate 	(void) printf("\n");
899*0Sstevel@tonic-gate }
900*0Sstevel@tonic-gate 
901*0Sstevel@tonic-gate /*
902*0Sstevel@tonic-gate  * Prints the configuration list to stdout.
903*0Sstevel@tonic-gate  */
904*0Sstevel@tonic-gate void
905*0Sstevel@tonic-gate nfsl_printconfig_list(nfsl_config_t *listp)
906*0Sstevel@tonic-gate {
907*0Sstevel@tonic-gate 	for (; listp != NULL; listp = listp->nc_next) {
908*0Sstevel@tonic-gate 		nfsl_printconfig(listp);
909*0Sstevel@tonic-gate 		(void) printf("\n");
910*0Sstevel@tonic-gate 	}
911*0Sstevel@tonic-gate }
912*0Sstevel@tonic-gate #endif /* DEBUG */
913*0Sstevel@tonic-gate 
914*0Sstevel@tonic-gate /*
915*0Sstevel@tonic-gate  * Returns non-zero if the given string is allowable for a tag, zero if
916*0Sstevel@tonic-gate  * not.
917*0Sstevel@tonic-gate  */
918*0Sstevel@tonic-gate static int
919*0Sstevel@tonic-gate is_legal_tag(char *tag)
920*0Sstevel@tonic-gate {
921*0Sstevel@tonic-gate 	int i;
922*0Sstevel@tonic-gate 	int len;
923*0Sstevel@tonic-gate 
924*0Sstevel@tonic-gate 	if (tag == NULL)
925*0Sstevel@tonic-gate 		return (0);
926*0Sstevel@tonic-gate 	len = strlen(tag);
927*0Sstevel@tonic-gate 	if (len == 0)
928*0Sstevel@tonic-gate 		return (0);
929*0Sstevel@tonic-gate 
930*0Sstevel@tonic-gate 	for (i = 0; i < len; i++) {
931*0Sstevel@tonic-gate 		char c;
932*0Sstevel@tonic-gate 
933*0Sstevel@tonic-gate 		c = tag[i];
934*0Sstevel@tonic-gate 		if (!(isalnum((unsigned char)c) || c == '_'))
935*0Sstevel@tonic-gate 			return (0);
936*0Sstevel@tonic-gate 	}
937*0Sstevel@tonic-gate 
938*0Sstevel@tonic-gate 	return (1);
939*0Sstevel@tonic-gate }
940*0Sstevel@tonic-gate 
941*0Sstevel@tonic-gate /*
942*0Sstevel@tonic-gate  * getline attempts to get a line from the configuration file,
943*0Sstevel@tonic-gate  * upto LINESZ. A line in the file is a concatenation of lines if the
944*0Sstevel@tonic-gate  * continuation symbol '\' is used at the end of the line. Returns
945*0Sstevel@tonic-gate  * line on success, a NULL on EOF, and an empty string on lines > linesz.
946*0Sstevel@tonic-gate  */
947*0Sstevel@tonic-gate static char *
948*0Sstevel@tonic-gate getline(FILE *fp, char *path, char *line, int linesz) {
949*0Sstevel@tonic-gate 	register char *p = line;
950*0Sstevel@tonic-gate 	register int len;
951*0Sstevel@tonic-gate 	int excess = 0;
952*0Sstevel@tonic-gate 
953*0Sstevel@tonic-gate 	*p = '\0';
954*0Sstevel@tonic-gate 
955*0Sstevel@tonic-gate 	for (;;) {
956*0Sstevel@tonic-gate 		if (fgets(p, linesz - (p-line), fp) == NULL) {
957*0Sstevel@tonic-gate 			return (*line ? line : NULL);   /* EOF */
958*0Sstevel@tonic-gate 		}
959*0Sstevel@tonic-gate 
960*0Sstevel@tonic-gate 		len = strlen(line);
961*0Sstevel@tonic-gate 		if (len <= 0) {
962*0Sstevel@tonic-gate 			p = line;
963*0Sstevel@tonic-gate 			continue;
964*0Sstevel@tonic-gate 		}
965*0Sstevel@tonic-gate 		p = &line[len - 1];
966*0Sstevel@tonic-gate 
967*0Sstevel@tonic-gate 		/*
968*0Sstevel@tonic-gate 		 * Is input line too long?
969*0Sstevel@tonic-gate 		 */
970*0Sstevel@tonic-gate 		if (*p != '\n') {
971*0Sstevel@tonic-gate 			excess = 1;
972*0Sstevel@tonic-gate 			/*
973*0Sstevel@tonic-gate 			 * Perhaps last char read was '\'. Reinsert it
974*0Sstevel@tonic-gate 			 * into the stream to ease the parsing when we
975*0Sstevel@tonic-gate 			 * read the rest of the line to discard.
976*0Sstevel@tonic-gate 			 */
977*0Sstevel@tonic-gate 			(void) ungetc(*p, fp);
978*0Sstevel@tonic-gate 			break;
979*0Sstevel@tonic-gate 		}
980*0Sstevel@tonic-gate trim:
981*0Sstevel@tonic-gate 
982*0Sstevel@tonic-gate 		/* trim trailing white space */
983*0Sstevel@tonic-gate 		while (p >= line && isspace(*(uchar_t *)p))
984*0Sstevel@tonic-gate 		*p-- = '\0';
985*0Sstevel@tonic-gate 		if (p < line) {			/* empty line */
986*0Sstevel@tonic-gate 			p = line;
987*0Sstevel@tonic-gate 			continue;
988*0Sstevel@tonic-gate 		}
989*0Sstevel@tonic-gate 
990*0Sstevel@tonic-gate 		if (*p == '\\') {		/* continuation */
991*0Sstevel@tonic-gate 			*p = '\0';
992*0Sstevel@tonic-gate 			continue;
993*0Sstevel@tonic-gate 		}
994*0Sstevel@tonic-gate 
995*0Sstevel@tonic-gate 		/*
996*0Sstevel@tonic-gate 		 * Ignore comments. Comments start with '#'
997*0Sstevel@tonic-gate 		 * which must be preceded by a whitespace, unless
998*0Sstevel@tonic-gate 		 * '#' is the first character in the line.
999*0Sstevel@tonic-gate 		 */
1000*0Sstevel@tonic-gate 		p = line;
1001*0Sstevel@tonic-gate 
1002*0Sstevel@tonic-gate 		while (p = strchr(p, '#')) {
1003*0Sstevel@tonic-gate 			if (p == line || isspace(*(p-1))) {
1004*0Sstevel@tonic-gate 				*p-- = '\0';
1005*0Sstevel@tonic-gate 				goto trim;
1006*0Sstevel@tonic-gate 			}
1007*0Sstevel@tonic-gate 			p++;
1008*0Sstevel@tonic-gate 		}
1009*0Sstevel@tonic-gate 
1010*0Sstevel@tonic-gate 		break;
1011*0Sstevel@tonic-gate 	}
1012*0Sstevel@tonic-gate 	if (excess) {
1013*0Sstevel@tonic-gate 		int c;
1014*0Sstevel@tonic-gate 
1015*0Sstevel@tonic-gate 		/*
1016*0Sstevel@tonic-gate 		 * discard rest of line and return an empty string.
1017*0Sstevel@tonic-gate 		 * done to set the stream to the correct place when
1018*0Sstevel@tonic-gate 		 * we are done with this line.
1019*0Sstevel@tonic-gate 		 */
1020*0Sstevel@tonic-gate 		while ((c = getc(fp)) != EOF) {
1021*0Sstevel@tonic-gate 			*p = c;
1022*0Sstevel@tonic-gate 			if (*p == '\n')		/* end of the long line */
1023*0Sstevel@tonic-gate 				break;
1024*0Sstevel@tonic-gate 			else if (*p == '\\') {		/* continuation */
1025*0Sstevel@tonic-gate 				if (getc(fp) == EOF)	/* ignore next char */
1026*0Sstevel@tonic-gate 					break;
1027*0Sstevel@tonic-gate 			}
1028*0Sstevel@tonic-gate 		}
1029*0Sstevel@tonic-gate 		if (nfsl_errs_to_syslog) {
1030*0Sstevel@tonic-gate 			syslog(LOG_ERR, gettext(
1031*0Sstevel@tonic-gate 				"%s: line too long - ignored (max %d chars)"),
1032*0Sstevel@tonic-gate 				path, linesz-1);
1033*0Sstevel@tonic-gate 		} else {
1034*0Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
1035*0Sstevel@tonic-gate 				"%s: line too long - ignored (max %d chars)\n"),
1036*0Sstevel@tonic-gate 				path, linesz-1);
1037*0Sstevel@tonic-gate 		}
1038*0Sstevel@tonic-gate 		*line = '\0';
1039*0Sstevel@tonic-gate 	}
1040*0Sstevel@tonic-gate 
1041*0Sstevel@tonic-gate 	return (line);
1042*0Sstevel@tonic-gate }
1043