summaryrefslogtreecommitdiff
path: root/usr/src/cmd/fs.d/nfs/lib/nfslog_config.c
blob: aff081912b2c5e7b529208ea796ce5f6a4d41edc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
 */

#include <ctype.h>
#include <errno.h>
#include <locale.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <string.h>
#include <syslog.h>
#include <nfs/nfs.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include "nfslog_config.h"

#define	ERROR_BUFSZ	100

/*
 * This flag controls where error messages go.
 * Zero means that messages go to stderr.
 * Non-zero means that messages go to syslog.
 */
boolean_t nfsl_errs_to_syslog;

/*
 * Pointer to the global entry in the list
 */
static nfsl_config_t *global = NULL;

/*
 * Pointer to the raw global entry in the list, this is the
 * global entry without the expanded paths. This is used to
 * complete configurations.
 */
static nfsl_config_t *global_raw = NULL;

/*
 * Last modification time to config file.
 */
static timestruc_t config_last_modification = { 0 };

/*
 * Whitespace characters to delimit fields in a line.
 */
static const char *whitespace = " \t";

static int getconfiglist(nfsl_config_t **, boolean_t);
static nfsl_config_t *create_config(char *, char *, char *, char *, char *,
			char *, int, boolean_t, int *);
static nfsl_config_t *create_global_raw(int *);
static int update_config(nfsl_config_t *, char *, char *, char *,
			char *, char *, char *, int, boolean_t, boolean_t);
static int update_field(char **, char *, char *, boolean_t *);
static nfsl_config_t *findconfig(nfsl_config_t **, char *, boolean_t,
			nfsl_config_t **);
static nfsl_config_t *getlastconfig(nfsl_config_t *);
static void complete_with_global(char **, char **, char **, char **,
			char **, int *);
#ifdef DEBUG
static void remove_config(nfsl_config_t **, nfsl_config_t *, nfsl_config_t **);
void nfsl_printconfig(nfsl_config_t *);
#endif /* DEBUG */
static char *gataline(FILE *, char *, char *, int);
static int get_info(char *, char **, char **, char **, char **, char **,
			char **, int *);
static void free_config(nfsl_config_t *);
static int is_legal_tag(char *);
static boolean_t is_complete_config(char *, char *, char *, char *);

/*
 * Read the configuration file and create a list of configuration
 * parameters.  Returns zero for success or an errno value.
 * The caller is responsible for freeing the returned configlist by calling
 * nfsl_freeconfig_list().
 *
 * If the configuration file does not exist, *listpp points to a config entry
 * containing the hardwired defaults.
 */
int
nfsl_getconfig_list(nfsl_config_t **listpp)
{
	int error = 0;
	char *locale;

	/*
	 * Set the locale correctly so that we can correctly identify
	 * alphabetic characters.
	 */
	if ((locale = getenv("LC_ALL")) != NULL)
		(void) setlocale(LC_ALL, locale);
	else if ((locale = getenv("LC_CTYPE")) != NULL)
		(void) setlocale(LC_CTYPE, locale);
	else if ((locale = getenv("LANG")) != NULL)
		(void) setlocale(LC_CTYPE, locale);

	/*
	 * Allocate 'global_raw' structure, its contents are
	 * indirectly allocated by create_config().
	 */
	assert(global_raw == NULL);
	global_raw = create_global_raw(&error);
	if (global_raw == NULL)
		return (error);

	/*
	 * Build global entry with hardwired defaults first.
	 */
	assert(global == NULL);
	global = create_config(DEFAULTTAG, DEFAULTDIR, BUFFERPATH, NULL,
			FHPATH, LOGPATH, TRANSLOG_BASIC, B_TRUE, &error);
	*listpp = global;
	if (global == NULL) {
		free_config(global_raw);
		return (error);
	}

	if (error = getconfiglist(listpp, B_FALSE))
		nfsl_freeconfig_list(listpp);
	else {
		assert(global != NULL);
		/*
		 * The global entry was replaced with the one in the file,
		 * clear the UPDATED flag
		 */
		global->nc_flags &= ~NC_UPDATED;
	}
	return (error);
}

/*
 * Allocates memory for the 'global_raw' structure.
 * The actual allocation of values for its components happens in
 * update_config().
 */
static nfsl_config_t *
create_global_raw(int *error)
{
	nfsl_config_t *p;

	*error = 0;
	if (p = (nfsl_config_t *)malloc(sizeof (*p)))
		(void) memset((void *)p, 0, sizeof (*p));
	else
		*error = ENOMEM;

	return (p);
}

/*
 * Checks if the the configuration file has been modified since we last
 * read it, if not simply returns, otherwise it re-reads it adding new
 * configuration entries. Note that existing entries that no longer
 * exist in the configuration file are not removed. Existing entries
 * that are modified in the configuration file are updated in the list
 * as well.
 * if 'updated' is defined then it is set to TRUE if the list was modified.
 *
 * Note that if an error occurs, the list may be corrupted.
 * It is the responsibility of the caller to free the list.
 * If the configuration file does not exist, we simply return the list
 * that we previously had, log a message and return success.
 */
int
nfsl_checkconfig_list(nfsl_config_t **listpp, boolean_t *updated)
{
	struct stat st;
	int error = 0;

	if (updated != NULL)
		*updated = B_FALSE;

	if (stat(NFSL_CONFIG_FILE_PATH, &st) == -1) {
		error = errno;
		if (nfsl_errs_to_syslog) {
			syslog(LOG_ERR, gettext(
				"Can't stat %s - %s"), NFSL_CONFIG_FILE_PATH,
				strerror(error));
		} else {
			(void) fprintf(stderr, gettext(
				"Can't stat %s - %s\n"), NFSL_CONFIG_FILE_PATH,
				strerror(error));
		}
		return (0);
	}

	if (config_last_modification.tv_sec == st.st_mtim.tv_sec &&
	    config_last_modification.tv_nsec == st.st_mtim.tv_nsec)
		return (0);

	if (updated != NULL)
		*updated = B_TRUE;

	return (getconfiglist(listpp, B_TRUE));
}

/*
 * Does the real work. Reads the configuration file and creates the
 * list of entries. Assumes that *listpp contains at least one entry.
 * The caller is responsible for freeing any config entries added to
 * the list whether this routine returns an error or not.
 *
 * Returns 0 on success and updates the '*listpp' config list,
 * Returns non-zero error value otherwise.
 */
static int
getconfiglist(nfsl_config_t **listpp, boolean_t updating)
{
	FILE *fp;
	int error = 0;
	nfsl_config_t *listp = NULL, *tail = NULL;
	char linebuf[MAX_LINESZ];
	char errorbuf[ERROR_BUFSZ];
	char *tag, *defaultdir, *bufferpath, *rpclogpath, *fhpath, *logpath;
	int logformat;
	flock_t flock;
	struct stat st;

	fp = fopen(NFSL_CONFIG_FILE_PATH, "r");
	if (fp == NULL) {
		if (updating) {
			(void) sprintf(errorbuf, "Can't open %s",
				NFSL_CONFIG_FILE_PATH);
		} else {
			(void) sprintf(errorbuf,
				"Can't open %s - using hardwired defaults",
				NFSL_CONFIG_FILE_PATH);
		}

		/*
		 * Use hardwired config.
		 */
		if (nfsl_errs_to_syslog)
			syslog(LOG_ERR, gettext("%s"), errorbuf);
		else
			(void) fprintf(stderr, gettext("%s\n"), errorbuf);

		return (0);
	}

	(void) memset((void *) &flock, 0, sizeof (flock));
	flock.l_type = F_RDLCK;
	if (fcntl(fileno(fp), F_SETLKW, &flock) == -1) {
		error = errno;
		if (nfsl_errs_to_syslog) {
			syslog(LOG_ERR, gettext(
				"Can't lock %s - %s"), NFSL_CONFIG_FILE_PATH,
				strerror(error));
		} else {
			(void) fprintf(stderr, gettext(
				"Can't lock %s - %s\n"), NFSL_CONFIG_FILE_PATH,
				strerror(error));
		}
		goto done;
	}

	assert (*listpp != NULL);
	tail = getlastconfig(*listpp);

	while (gataline(fp, NFSL_CONFIG_FILE_PATH, linebuf, sizeof (linebuf))) {
		if (linebuf[0] == '\0') {
			/*
			 * ignore lines that exceed max size
			 */
			continue;
		}

		if (error = get_info(linebuf, &tag, &defaultdir, &bufferpath,
		    &rpclogpath, &fhpath, &logpath, &logformat))
			break;

		if (listp = findconfig(listpp, tag, B_FALSE, &tail)) {
			/*
			 * An entry with the same tag name exists,
			 * update the fields that changed.
			 */
			error = update_config(listp, tag, defaultdir,
					bufferpath, rpclogpath, fhpath, logpath,
					logformat, B_TRUE, B_TRUE);
			if (error)
				break;
		} else {
			/*
			 * New entry, create it.
			 */
			listp = create_config(tag, defaultdir,
					bufferpath, rpclogpath, fhpath,
					logpath, logformat, B_TRUE, &error);
			if (listp == NULL)
				break;

			if (*listpp == NULL)
				*listpp = listp;
			else
				tail->nc_next = listp;
			tail = listp;
		}

		assert(global != NULL);
	}

	if (!error) {
		/*
		 * Get mtime while we have file locked
		 */
		if (error = fstat(fileno(fp), &st)) {
			error = errno;
			if (nfsl_errs_to_syslog) {
				syslog(LOG_ERR, gettext(
				"Can't stat %s - %s"), NFSL_CONFIG_FILE_PATH,
				strerror(error));
			} else {
				(void) fprintf(stderr, gettext(
				"Can't stat %s - %s\n"), NFSL_CONFIG_FILE_PATH,
				strerror(error));
			}
		}
		config_last_modification = st.st_mtim;
	}

done:
	(void) fclose(fp);
	return (error);
}

/*
 * Creates the config structure with the values specified by the
 * parameters. If defaultdir has been specified, all relative paths
 * are prepended with this defaultdir.
 * If 'complete' is set then this must represent a complete config entry
 * as specified by is_complete_config(), otherwise no work is perfomed, and
 * NULL is returned.
 *
 * Returns the newly created config structure on success.
 * Returns NULL on failure and sets error to the appropriate error.
 */
static nfsl_config_t *
create_config(
	char *tag,
	char *defaultdir,
	char *bufferpath,
	char *rpclogpath,
	char *fhpath,
	char *logpath,
	int   logformat,
	boolean_t complete,
	int  *error)
{
	nfsl_config_t *config;

	if ((config = (nfsl_config_t *)malloc(sizeof (*config))) == NULL) {
		*error = ENOMEM;
		return (NULL);
	}
	(void) memset((void *)config, 0, sizeof (*config));

	*error = update_config(config, tag, defaultdir, bufferpath, rpclogpath,
			fhpath, logpath, logformat, complete, B_TRUE);
	if (*error) {
		free(config);
		return (NULL);
	}

	config->nc_flags &= ~NC_UPDATED;	/* This is a new entry */

	return (config);
}


/*
 * Updates the configuration entry with the new information provided,
 * sets NC_UPDATED to indicate so. The entry is left untouched if all
 * the fields are the same (except for 'nc_rpccookie', 'nc_transcookie'
 * and 'nc_next').
 * Prepends each path component with 'defauldir' if 'prepend' is set.
 *
 * Returns 0 on success, error otherwise.
 * On error, the config entry is left in an inconsistent state.
 * The only thing the caller can really do with it is free it.
 */
static int
update_config(
	nfsl_config_t *config,
	char *tag,
	char *defaultdir,
	char *bufferpath,
	char *rpclogpath,
	char *fhpath,
	char *logpath,
	int   logformat,
	boolean_t complete,
	boolean_t prepend)
{
	boolean_t updated, config_updated = B_FALSE;
	int error = 0;

	if (complete && !is_complete_config(tag, bufferpath, fhpath, logpath)) {
		/*
		 * Not a complete entry
		 */
		if (nfsl_errs_to_syslog) {
			syslog(LOG_ERR, gettext(
			"update_config: \"%s\" not a complete config entry."),
			tag);
		} else {
			(void) fprintf(stderr, gettext(
			"update_config: \"%s\" not a complete config entry.\n"),
			tag);
		}
		return (EINVAL);
	}

	assert(tag != NULL);
	if (config->nc_name == NULL) {
		/*
		 * New entry
		 */
		if ((config->nc_name = strdup(tag)) == NULL) {
			error = ENOMEM;
			goto errout;
		}
	} else
		assert(strcmp(config->nc_name, tag) == 0);

	if (error = update_field(
	    &config->nc_defaultdir, defaultdir, NULL, &updated))
		goto errout;
	if (!prepend) {
		/*
		 * Do not prepend default directory.
		 */
		defaultdir = NULL;
	}
	config_updated |= updated;
	if (error = update_field(
	    &config->nc_bufferpath, bufferpath, defaultdir, &updated))
		goto errout;
	config_updated |= updated;
	if (error = update_field(
	    &config->nc_rpclogpath, rpclogpath, defaultdir, &updated))
		goto errout;
	config_updated |= updated;
	if (error = update_field(
	    &config->nc_fhpath, fhpath, defaultdir, &updated))
		goto errout;
	config_updated |= updated;
	if (error = update_field(
	    &config->nc_logpath, logpath, defaultdir, &updated))
		goto errout;
	config_updated |= updated;
	updated = (config->nc_logformat != logformat);
	if (updated)
		config->nc_logformat = logformat;
	config_updated |= updated;

	if (config_updated)
		config->nc_flags |= NC_UPDATED;

	if (strcmp(tag, DEFAULTTAG) == 0) {
		/*
		 * Have the default global config point to this entry.
		 */
		global = config;

		/*
		 * Update the global_raw configuration entry.
		 * Make sure no expanding of paths occurs.
		 */
		if (error = update_config(global_raw, DEFAULTRAWTAG, defaultdir,
			bufferpath, rpclogpath, fhpath, logpath, logformat,
			complete, B_FALSE))
				goto errout;
	}

	return (error);

errout:
	if (nfsl_errs_to_syslog) {
		syslog(LOG_ERR, gettext(
			"update_config: Can't process \"%s\" config entry: %s"),
			tag, strerror(error));
	} else {
		(void) fprintf(stderr, gettext(
		"update_config: Can't process \"%s\" config entry: %s\n"),
		tag, strerror(error));
	}
	return (error);
}

/*
 * Prepends 'prependir' to 'new' if 'prependir' is defined.
 * Compares the value of '*old' with 'new', if it has changed,
 * then sets whatever 'old' references equal to 'new'.
 * Returns 0 on success, error otherwise.
 * Sets '*updated' to B_TRUE if field was modified.
 * The value of '*updated' is undefined on error.
 */
static int
update_field(
	char **old,		/* pointer to config field */
	char *new,		/* updated value */
	char *prependdir,	/* prepend this directory to new */
	boolean_t *updated)	/* field was modified */
{
	char *tmp_new = NULL;
	int need_update = 0;

	if (new != NULL) {
		if (prependdir != NULL && new[0] != '/') {
			tmp_new = malloc(strlen(prependdir) + strlen(new) + 2);
			if (tmp_new == NULL)
				return (ENOMEM);
			(void) sprintf(tmp_new, "%s/%s", prependdir, new);
		} else {
			if ((tmp_new = strdup(new)) == NULL)
				return (ENOMEM);
		}
	}

	if (tmp_new != NULL) {
		if (*old == NULL)
			need_update++;
		else if (strcmp(tmp_new, *old) != 0) {
			free(*old);
			need_update++;
		}
		if (need_update)
			*old = tmp_new;
	} else if (*old != NULL) {
		need_update++;
		free(*old);
		*old = NULL;
	}

	*updated = need_update != 0;
	return (0);
}

#ifdef DEBUG
/*
 * Removes and frees the 'config' entry from the list
 * pointed to by '*listpp'.
 * No error is reported if the entry does not exist.
 * Updates '*tail' to point to the last item in the list.
 */
static void
remove_config(
	nfsl_config_t **listpp,
	nfsl_config_t *config,
	nfsl_config_t **tail)
{
	nfsl_config_t *p, *prev;

	prev = *listpp;
	for (p = *listpp; p != NULL; p = p->nc_next) {
		if (p == config) {
			if (p == prev) {
				/*
				 * first element of the list
				 */
				*listpp = prev->nc_next;
			} else
				prev->nc_next = p->nc_next;
			free_config(p);
			break;
		}
		prev = p;
	}

	/*
	 * Find tail of the list.
	 */
	for (*tail = prev; (*tail)->nc_next != NULL; *tail = (*tail)->nc_next)
		;
}
#endif /* DEBUG */

static void
free_config(nfsl_config_t *config)
{
	if (config == NULL)
		return;
	if (config->nc_name)
		free(config->nc_name);
	if (config->nc_defaultdir)
		free(config->nc_defaultdir);
	if (config->nc_bufferpath)
		free(config->nc_bufferpath);
	if (config->nc_rpclogpath)
		free(config->nc_rpclogpath);
	if (config->nc_fhpath)
		free(config->nc_fhpath);
	if (config->nc_logpath)
		free(config->nc_logpath);
	if (config == global)
		global = NULL;
	if (config == global_raw)
		global_raw = NULL;
	free(config);
}

void
nfsl_freeconfig_list(nfsl_config_t **listpp)
{
	nfsl_config_t *next;

	if (*listpp == NULL)
		return;

	do {
		next = (*listpp)->nc_next;
		free_config(*listpp);
		*listpp = next;
	} while (*listpp);

	free_config(global_raw);
}

/*
 * Returns a pointer to the first instance of 'tag' in the list.
 * If 'remove' is true, then the entry is removed from the list and
 * a pointer to it is returned.
 * If '*tail' is not NULL, then it will point to the last element of
 * the list. Note that this function assumes that *tail already
 * points at the last element of the list.
 * Returns NULL if the entry does not exist.
 */
static nfsl_config_t *
findconfig(
	nfsl_config_t **listpp,
	char *tag, boolean_t remove,
	nfsl_config_t **tail)
{
	nfsl_config_t *p, *prev;

	prev = *listpp;
	for (p = *listpp; p != NULL; p = p->nc_next) {
		if (strcmp(p->nc_name, tag) == 0) {
			if (remove) {
				if (p == prev) {
					/*
					 * first element of the list
					 */
					*listpp = prev->nc_next;
				} else
					prev->nc_next = p->nc_next;

				if (tail != NULL && p == *tail) {
					/*
					 * Only update *tail if we removed
					 * the last element of the list, and we
					 * requested *tail to be updated.
					 */
					*tail = prev;
				}
			}
			return (p);
		}
		prev = p;
	}

	return (NULL);
}

static nfsl_config_t *
getlastconfig(nfsl_config_t *listp)
{
	nfsl_config_t *lastp = NULL;

	for (; listp != NULL; listp = listp->nc_next)
		lastp = listp;

	return (lastp);
}

/*
 * Returns a pointer to the first instance of 'tag' in the list.
 * Returns NULL if the entry does not exist.
 * Sets 'error' if the update of the list failed if necessary, and
 * returns NULL.
 */
nfsl_config_t *
nfsl_findconfig(nfsl_config_t *listp, char *tag, int *error)
{
	nfsl_config_t *config;
	boolean_t updated;

	*error = 0;
	config = findconfig(&listp, tag, B_FALSE, (nfsl_config_t **)NULL);
	if (config == NULL) {
		/*
		 * Rebuild our list if the file has changed.
		 */
		if (*error = nfsl_checkconfig_list(&listp, &updated)) {
			/*
			 * List may be corrupted, notify caller.
			 */
			return (NULL);
		}
		if (updated) {
			/*
			 * Search for tag again.
			 */
			config = findconfig(&listp, tag, B_FALSE,
				(nfsl_config_t **)NULL);
		}
	}

	return (config);
}

/*
 * Use the raw global values if any of the parameters is not defined.
 */
static void
complete_with_global(
	char **defaultdir,
	char **bufferpath,
	char **rpclogpath,
	char **fhpath,
	char **logpath,
	int  *logformat)
{
	if (*defaultdir == NULL)
		*defaultdir = global_raw->nc_defaultdir;
	if (*bufferpath == NULL)
		*bufferpath = global_raw->nc_bufferpath;
	if (*rpclogpath == NULL)
		*rpclogpath = global_raw->nc_rpclogpath;
	if (*fhpath == NULL)
		*fhpath = global_raw->nc_fhpath;
	if (*logpath == NULL)
		*logpath = global_raw->nc_logpath;
	if (*logformat == 0)
		*logformat = global_raw->nc_logformat;
}

/*
 * Parses 'linebuf'. Returns 0 if a valid tag is found, otherwise non-zero.
 * Unknown tokens are silently ignored.
 * It is the responsibility of the caller to make a copy of the non-NULL
 * parameters if they need to be used before linebuf is freed.
 */
static int
get_info(
	char *linebuf,
	char **tag,
	char **defaultdir,
	char **bufferpath,
	char **rpclogpath,
	char **fhpath,
	char **logpath,
	int  *logformat)
{
	char *tok;
	char *tmp;

	/* tag */
	*tag = NULL;
	tok = strtok(linebuf, whitespace);
	if (tok == NULL)
		goto badtag;
	if (!is_legal_tag(tok))
		goto badtag;
	*tag = tok;

	*defaultdir = *bufferpath = *rpclogpath = NULL;
	*fhpath = *logpath = NULL;
	*logformat = 0;

	while (tok = strtok(NULL, whitespace)) {
		if (strncmp(tok, "defaultdir=", strlen("defaultdir=")) == 0) {
			*defaultdir = tok + strlen("defaultdir=");
		} else if (strncmp(tok, "buffer=", strlen("buffer=")) == 0) {
			*bufferpath = tok + strlen("buffer=");
		} else if (strncmp(tok, "rpclog=", strlen("rpclog=")) == 0) {
			*rpclogpath = tok + strlen("rpclog=");
		} else if (strncmp(tok, "fhtable=", strlen("fhtable=")) == 0) {
			*fhpath = tok + strlen("fhtable=");
		} else if (strncmp(tok, "log=", strlen("log=")) == 0) {
			*logpath = tok + strlen("log=");
		} else if (strncmp(tok, "logformat=",
				strlen("logformat=")) == 0) {
			tmp = tok + strlen("logformat=");
			if (strncmp(tmp, "extended", strlen("extended")) == 0) {
				*logformat = TRANSLOG_EXTENDED;
			} else {
				/*
				 * Use transaction log basic format if
				 * 'extended' was not specified.
				 */
				*logformat = TRANSLOG_BASIC;
			}
		}
	}

	if (strcmp(*tag, DEFAULTTAG) != 0) {
		/*
		 * Use global values for fields not specified if
		 * this tag is not the global tag.
		 */
		complete_with_global(defaultdir, bufferpath,
			rpclogpath, fhpath, logpath, logformat);
	}

	return (0);

badtag:
	if (nfsl_errs_to_syslog) {
		syslog(LOG_ERR, gettext(
			"Bad tag found in config file."));
	} else {
		(void) fprintf(stderr, gettext(
			"Bad tag found in config file.\n"));
	}
	return (-1);
}

/*
 * Returns True if we have all the elements of a complete configuration
 * entry. A complete configuration has tag, bufferpath, fhpath and logpath
 * defined to non-zero strings.
 */
static boolean_t
is_complete_config(
	char *tag,
	char *bufferpath,
	char *fhpath,
	char *logpath)
{
	assert(tag != NULL);
	assert(strlen(tag) > 0);

	if ((bufferpath != NULL && strlen(bufferpath) > 0) &&
	    (fhpath != NULL && strlen(fhpath) > 0) &&
	    (logpath != NULL && strlen(logpath) > 0))
		return (B_TRUE);
	return (B_FALSE);
}

#ifdef DEBUG
/*
 * Prints the configuration entry to stdout.
 */
void
nfsl_printconfig(nfsl_config_t *config)
{
	if (config->nc_name)
		(void) printf("tag=%s\t", config->nc_name);
	if (config->nc_defaultdir)
		(void) printf("defaultdir=%s\t", config->nc_defaultdir);
	if (config->nc_logpath)
		(void) printf("logpath=%s\t", config->nc_logpath);
	if (config->nc_fhpath)
		(void) printf("fhpath=%s\t", config->nc_fhpath);
	if (config->nc_bufferpath)
		(void) printf("bufpath=%s\t", config->nc_bufferpath);
	if (config->nc_rpclogpath)
		(void) printf("rpclogpath=%s\t", config->nc_rpclogpath);
	if (config->nc_logformat == TRANSLOG_BASIC)
		(void) printf("logformat=basic");
	else if (config->nc_logformat == TRANSLOG_EXTENDED)
		(void) printf("logformat=extended");
	else
		(void) printf("config->nc_logformat=UNKNOWN");

	if (config->nc_flags & NC_UPDATED)
		(void) printf("\tflags=NC_UPDATED");
	(void) printf("\n");
}

/*
 * Prints the configuration list to stdout.
 */
void
nfsl_printconfig_list(nfsl_config_t *listp)
{
	for (; listp != NULL; listp = listp->nc_next) {
		nfsl_printconfig(listp);
		(void) printf("\n");
	}
}
#endif /* DEBUG */

/*
 * Returns non-zero if the given string is allowable for a tag, zero if
 * not.
 */
static int
is_legal_tag(char *tag)
{
	int i;
	int len;

	if (tag == NULL)
		return (0);
	len = strlen(tag);
	if (len == 0)
		return (0);

	for (i = 0; i < len; i++) {
		char c;

		c = tag[i];
		if (!(isalnum((unsigned char)c) || c == '_'))
			return (0);
	}

	return (1);
}

/*
 * gataline attempts to get a line from the configuration file,
 * upto LINESZ. A line in the file is a concatenation of lines if the
 * continuation symbol '\' is used at the end of the line. Returns
 * line on success, a NULL on EOF, and an empty string on lines > linesz.
 */
static char *
gataline(FILE *fp, char *path, char *line, int linesz) {
	register char *p = line;
	register int len;
	int excess = 0;

	*p = '\0';

	for (;;) {
		if (fgets(p, linesz - (p-line), fp) == NULL) {
			return (*line ? line : NULL);   /* EOF */
		}

		len = strlen(line);
		if (len <= 0) {
			p = line;
			continue;
		}
		p = &line[len - 1];

		/*
		 * Is input line too long?
		 */
		if (*p != '\n') {
			excess = 1;
			/*
			 * Perhaps last char read was '\'. Reinsert it
			 * into the stream to ease the parsing when we
			 * read the rest of the line to discard.
			 */
			(void) ungetc(*p, fp);
			break;
		}
trim:

		/* trim trailing white space */
		while (p >= line && isspace(*(uchar_t *)p))
		*p-- = '\0';
		if (p < line) {			/* empty line */
			p = line;
			continue;
		}

		if (*p == '\\') {		/* continuation */
			*p = '\0';
			continue;
		}

		/*
		 * Ignore comments. Comments start with '#'
		 * which must be preceded by a whitespace, unless
		 * '#' is the first character in the line.
		 */
		p = line;

		while (p = strchr(p, '#')) {
			if (p == line || isspace(*(p-1))) {
				*p-- = '\0';
				goto trim;
			}
			p++;
		}

		break;
	}
	if (excess) {
		int c;

		/*
		 * discard rest of line and return an empty string.
		 * done to set the stream to the correct place when
		 * we are done with this line.
		 */
		while ((c = getc(fp)) != EOF) {
			*p = c;
			if (*p == '\n')		/* end of the long line */
				break;
			else if (*p == '\\') {		/* continuation */
				if (getc(fp) == EOF)	/* ignore next char */
					break;
			}
		}
		if (nfsl_errs_to_syslog) {
			syslog(LOG_ERR, gettext(
				"%s: line too long - ignored (max %d chars)"),
				path, linesz-1);
		} else {
			(void) fprintf(stderr, gettext(
				"%s: line too long - ignored (max %d chars)\n"),
				path, linesz-1);
		}
		*line = '\0';
	}

	return (line);
}