summaryrefslogtreecommitdiff
path: root/usr/src/lib/libnwam/common/libnwam_files.c
blob: 898f555de38d015cfc6088c860b2c697f3c9fb02 (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
/*
 * 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 2010 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#include <assert.h>
#include <dirent.h>
#include <ctype.h>
#include <libgen.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/param.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <unistd.h>

#include "libnwam_impl.h"
#include <libnwam_priv.h>
#include <libnwam.h>

/*
 * Implementation of files backend for libnwam configuration objects.
 * /etc/dladm/datalink.conf-like format is used.
 */
#define	NWAM_FILE_LINE_MAX		2048
#define	NWAM_FILE_PROP_ESCAPE		'\\'
#define	NWAM_FILE_PROP_DELIMITER	';'
#define	NWAM_FILE_PROP_ASSIGN		'='
#define	NWAM_FILE_VALUE_DELIMITER	','
#define	NWAM_FILE_BOOLEAN_TRUE		"true"
#define	NWAM_FILE_BOOLEAN_FALSE		"false"

/*
 * strtok_r-like function that takes a string, finds the next unescaped
 * delimiter char after in, nullifies it and sets nextp to point to the
 * remaining string (if any). Returns in, setting nextp to NULL if no such
 * delimiter is found.
 */
char *
nwam_tokenize_by_unescaped_delim(char *in, char delim, char **nextp)
{
	boolean_t escaped = B_FALSE;
	size_t totlen;

	if (in == NULL)
		return (NULL);

	totlen = strlen(in);

	for (*nextp = in; (*nextp - in) < strlen(in); (*nextp)++) {
		if ((*nextp)[0] == NWAM_FILE_PROP_ESCAPE) {
			escaped = !escaped;
		} else if (!escaped && (*nextp)[0] == delim) {
			/* Nullify delimiter */
			(*nextp)[0] = '\0';
			/*
			 * If more string left to go, nextp points to string
			 * after delimiter, otherwise NULL.
			 */
			(*nextp)++;
			*nextp = ((*nextp - in) < totlen) ? (*nextp) : NULL;
			return (in);
		} else {
			escaped = B_FALSE;
		}
	}
	*nextp = NULL;
	return (in);
}

/* Add escape chars to value string */
static void
value_add_escapes(char *in, char *out)
{
	int i, j = 0;

	/*
	 * It is safe to use strlen() as we sanitycheck string length on value
	 * creation, so no string longer than NWAM_MAX_VALUE_LEN is accepted.
	 */
	for (i = 0; i < strlen(in); i++) {
		switch (in[i]) {
		case NWAM_FILE_VALUE_DELIMITER:
		case NWAM_FILE_PROP_DELIMITER:
		case NWAM_FILE_PROP_ESCAPE:
			out[j++] = NWAM_FILE_PROP_ESCAPE;
			out[j++] = in[i];
			break;
		default:
			out[j++] = in[i];
			break;
		}
	}
	out[j] = '\0';
}

static char *
value_remove_escapes(char *in)
{
	char *out;
	int i, j = 0;

	if ((out = strdup(in)) == NULL)
		return (NULL);

	/*
	 * It is safe to use strlen() as we sanitycheck string length on value
	 * creation (i.e. before they are written to the file), so no string
	 * longer than NWAM_MAX_VALUE_LEN is accepted.
	 */
	for (i = 0; i < strlen(in); i++) {
		if (in[i] == NWAM_FILE_PROP_ESCAPE)
			out[j++] = in[++i];
		else
			out[j++] = in[i];
	}
	out[j] = '\0';
	return (out);
}


/*
 * Parse line into name and object list of properties.
 * Each line has the format:
 *
 * objname	[prop=type:val1[,val2..];..]
 */
nwam_error_t
nwam_line_to_object(char *line, char **objname, void *proplist)
{
	char *next = line, *prop, *nextprop, *propname, *proptypestr, *nextval;
	char **valstr, **newvalstr;
	boolean_t *valbool, *newvalbool;
	int64_t *valint, *newvalint;
	uint64_t *valuint, *newvaluint;
	uint_t nelem, i;
	nwam_value_type_t proptype;
	nwam_value_t val = NULL;
	nwam_error_t err;

	if ((err = nwam_alloc_object_list(proplist)) != NWAM_SUCCESS)
		return (err);

	*objname = line;

	if ((*objname = nwam_tokenize_by_unescaped_delim(line, '\t', &prop))
	    == NULL) {
		nwam_free_object_list(*((char **)proplist));
		return (NWAM_ENTITY_INVALID);
	}

	while ((prop = nwam_tokenize_by_unescaped_delim(prop,
	    NWAM_FILE_PROP_DELIMITER, &nextprop)) != NULL) {
		/*
		 * Parse property into name=type,val[,val]
		 */
		if ((propname = nwam_tokenize_by_unescaped_delim(prop,
		    NWAM_FILE_PROP_ASSIGN, &next)) == NULL ||
		    (proptypestr = nwam_tokenize_by_unescaped_delim(next,
		    NWAM_FILE_VALUE_DELIMITER, &next)) == NULL) {
			nwam_free_object_list(*((char **)proplist));
			return (NWAM_ENTITY_INVALID);
		}
		if ((proptype = nwam_string_to_value_type(proptypestr)) ==
		    NWAM_VALUE_TYPE_UNKNOWN) {
			nwam_free_object_list(*((char **)proplist));
			return (NWAM_ENTITY_INVALID);
		}
		valbool = NULL;
		valint = NULL;
		valstr = NULL;
		switch (proptype) {
		case NWAM_VALUE_TYPE_BOOLEAN:
			valbool = calloc(NWAM_MAX_NUM_VALUES,
			    sizeof (boolean_t));
			break;
		case NWAM_VALUE_TYPE_INT64:
			valint = calloc(NWAM_MAX_NUM_VALUES,
			    sizeof (int64_t));
			break;
		case NWAM_VALUE_TYPE_UINT64:
			valuint = calloc(NWAM_MAX_NUM_VALUES,
			    sizeof (uint64_t));
			break;
		case NWAM_VALUE_TYPE_STRING:
			valstr = calloc(NWAM_MAX_NUM_VALUES,
			    sizeof (char *));
			break;
		default:
			nwam_free_object_list(*((char **)proplist));
			return (NWAM_ENTITY_INVALID_VALUE);
		}
		if (valbool == NULL && valint == NULL && valuint == NULL &&
		    valstr == NULL) {
			/* Memory allocation failed */
			nwam_free_object_list(*((char **)proplist));
			return (NWAM_NO_MEMORY);
		}
		nelem = 0;
		while ((nextval = nwam_tokenize_by_unescaped_delim(next,
		    NWAM_FILE_VALUE_DELIMITER, &next)) != NULL) {
			nelem++;
			switch (proptype) {
			case NWAM_VALUE_TYPE_BOOLEAN:
				if (strncmp(nextval, NWAM_FILE_BOOLEAN_TRUE,
				    strlen(nextval)) == 0) {
					valbool[nelem - 1] = B_TRUE;
				} else if (strncmp(nextval,
				    NWAM_FILE_BOOLEAN_FALSE, strlen(nextval))
				    == 0) {
					valbool[nelem - 1] = B_FALSE;
				} else {
					nwam_free_object_list
					    (*((char **)proplist));
					return (NWAM_ENTITY_INVALID_VALUE);
				}
				break;
			case NWAM_VALUE_TYPE_INT64:
				valint[nelem - 1] = (int64_t)atoll(nextval);
				break;
			case NWAM_VALUE_TYPE_UINT64:
				valuint[nelem - 1] = (uint64_t)atoll(nextval);
				break;
			case NWAM_VALUE_TYPE_STRING:
				valstr[nelem - 1] =
				    value_remove_escapes(nextval);
				break;
			default:
				nwam_free_object_list(*((char **)proplist));
				return (NWAM_ENTITY_INVALID_VALUE);
			}
		}
		switch (proptype) {
		case NWAM_VALUE_TYPE_BOOLEAN:
			if ((newvalbool = realloc(valbool,
			    nelem * sizeof (boolean_t))) == NULL) {
				nwam_free_object_list(*((char **)proplist));
				return (NWAM_NO_MEMORY);
			}
			if ((err = nwam_value_create_boolean_array(newvalbool,
			    nelem, &val)) != NWAM_SUCCESS ||
			    (err = nwam_set_prop_value(*((char **)proplist),
			    propname, val)) != NWAM_SUCCESS) {
				free(newvalbool);
				nwam_value_free(val);
				nwam_free_object_list(*((char **)proplist));
				return (err);
			}
			free(newvalbool);
			nwam_value_free(val);
			break;
		case NWAM_VALUE_TYPE_INT64:
			if ((newvalint = realloc(valint,
			    nelem * sizeof (int64_t))) == NULL) {
				nwam_free_object_list(*((char **)proplist));
				return (NWAM_NO_MEMORY);
			}
			if ((err = nwam_value_create_int64_array(newvalint,
			    nelem, &val)) != NWAM_SUCCESS ||
			    (err = nwam_set_prop_value(*((char **)proplist),
			    propname, val)) != NWAM_SUCCESS) {
				free(newvalint);
				nwam_value_free(val);
				nwam_free_object_list(*((char **)proplist));
				return (err);
			}
			free(newvalint);
			nwam_value_free(val);
			break;
		case NWAM_VALUE_TYPE_UINT64:
			if ((newvaluint = realloc(valuint,
			    nelem * sizeof (uint64_t))) == NULL) {
				nwam_free_object_list(*((char **)proplist));
				return (NWAM_NO_MEMORY);
			}
			if ((err = nwam_value_create_uint64_array(newvaluint,
			    nelem, &val)) != NWAM_SUCCESS ||
			    (err = nwam_set_prop_value(*((char **)proplist),
			    propname, val)) != NWAM_SUCCESS) {
				free(newvaluint);
				nwam_value_free(val);
				nwam_free_object_list(*((char **)proplist));
				return (err);
			}
			free(newvaluint);
			nwam_value_free(val);
			break;
		case NWAM_VALUE_TYPE_STRING:
			if ((newvalstr = realloc(valstr,
			    nelem * sizeof (char *))) == NULL) {
				nwam_free_object_list(*((char **)proplist));
				return (NWAM_NO_MEMORY);
			}
			if ((err = nwam_value_create_string_array(newvalstr,
			    nelem, &val)) != NWAM_SUCCESS ||
			    (err = nwam_set_prop_value(*((char **)proplist),
			    propname, val)) != NWAM_SUCCESS) {
				for (i = 0; i < nelem; i++)
					free(newvalstr[i]);
				free(newvalstr);
				nwam_value_free(val);
				nwam_free_object_list(*((char **)proplist));
				return (err);
			}
			for (i = 0; i < nelem; i++)
				free(newvalstr[i]);
			free(newvalstr);
			nwam_value_free(val);
			break;
		}
		prop = nextprop;
	}

	return (NWAM_SUCCESS);
}

/*
 * Create list of NCP files used for walk of NCPs and for case-insensitive
 * matching of NCP name to file.
 */
static nwam_error_t
create_ncp_file_list(char ***ncpfilesp, uint_t *num_filesp)
{
	DIR *dirp = NULL;
	struct dirent *dp;
	char *ncpname, **ncpfiles = NULL;
	nwam_error_t err = NWAM_SUCCESS;
	uint_t i;

	ncpfiles = calloc(NWAM_MAX_NUM_OBJECTS, sizeof (char *));
	if (ncpfiles == NULL)
		return (NWAM_NO_MEMORY);
	*num_filesp = 0;

	/*
	 * Construct NCP list by finding all files in NWAM directory
	 * that match the NCP filename format.
	 */
	if ((dirp = opendir(NWAM_CONF_DIR)) == NULL) {
		err = nwam_errno_to_nwam_error(errno);
		goto done;
	}

	while ((dp = readdir(dirp)) != NULL) {
		uint_t filenamelen;

		/* Ensure filename is valid */
		if (nwam_ncp_file_to_name(dp->d_name, &ncpname) != NWAM_SUCCESS)
			continue;
		free(ncpname);
		filenamelen = strlen(NWAM_CONF_DIR) + strlen(dp->d_name) + 1;
		if ((ncpfiles[*num_filesp] = malloc(filenamelen)) == NULL) {
			err = NWAM_NO_MEMORY;
			goto done;
		}
		(void) strlcpy(ncpfiles[*num_filesp], NWAM_CONF_DIR,
		    strlen(NWAM_CONF_DIR) + 1);
		(void) strlcpy(ncpfiles[*num_filesp] + strlen(NWAM_CONF_DIR),
		    dp->d_name, filenamelen - strlen(NWAM_CONF_DIR));
		(*num_filesp)++;
	}
done:
	if (dirp != NULL)
		(void) closedir(dirp);

	if (err != NWAM_SUCCESS) {
		for (i = 0; i < *num_filesp; i++)
			free(ncpfiles[i]);
		free(ncpfiles);
	} else {
		*ncpfilesp = realloc(ncpfiles, sizeof (char *) * (*num_filesp));
		if (*num_filesp != 0 && *ncpfilesp == NULL)
			err = NWAM_NO_MEMORY;
	}
	return (err);
}

/*
 * Read object specified by objname from file, converting it to
 * an object list.  If filename is NULL, a list of configuration object
 * containers is returned, represented as an object lists with elements "enms"
 * "locs" and "ncps". Each of these is a list of configuration files for each
 * object. This corresponds to the enm.conf file, loc.conf file and list of
 * ncp conf files. If objname is NULL, read all objects, and create
 * an nvlist with one element - "object-list" - which has as its values
 * the names of the objects found.  Otherwise obj points to an object list
 * of properties for the first object in the file that case-insensitively
 * matches objname.  We write the found name into objname so that it can be
 * returned to the caller (and set in the object handle).
 */
/* ARGSUSED2 */
nwam_error_t
nwam_read_object_from_files_backend(char *filename, char *objname,
    uint64_t flags, void *obj)
{
	char line[NWAM_FILE_LINE_MAX];
	char *cp, *foundobjname, **objnames = NULL, **ncpfiles = NULL;
	uint_t num_files = 0;
	FILE *fp = NULL;
	nwam_error_t err;
	void *objlist = NULL, *proplist = NULL;
	uint_t i = 0, j = 0;
	nwam_value_t objnamesval = NULL;

	assert(obj != NULL);

	*((char **)obj) = NULL;

	if (filename == NULL) {
		nwam_value_t enmval = NULL, locval = NULL, ncpval = NULL;

		/*
		 * When the filename is not specified, it signifies a
		 * request for the list of configuration object containers -
		 * in this case files.
		 *
		 * A list of all object files is returned. For ENMs
		 * and locations, only the default loc.conf and enm.conf
		 * files are used, but for NCPs we need to walk the
		 * files in the NWAM directory retrieving each one that
		 * matches the NCP pattern.
		 */
		if ((err = nwam_alloc_object_list(&objlist)) != NWAM_SUCCESS)
			return (err);

		if ((err = nwam_value_create_string(NWAM_ENM_CONF_FILE,
		    &enmval)) != NWAM_SUCCESS ||
		    (err = nwam_value_create_string(NWAM_LOC_CONF_FILE,
		    &locval)) != NWAM_SUCCESS ||
		    (err = nwam_set_prop_value(objlist, NWAM_ENM_OBJECT_STRING,
		    enmval)) != NWAM_SUCCESS ||
		    (err = nwam_set_prop_value(objlist, NWAM_LOC_OBJECT_STRING,
		    locval)) != NWAM_SUCCESS)
			goto done_with_containers;

		/*
		 * Construct NCP list by finding all files in NWAM directory
		 * that match the NCP filename format.
		 */
		if ((err = create_ncp_file_list(&ncpfiles, &num_files))
		    != NWAM_SUCCESS)
			goto done_with_containers;

		if ((err = nwam_value_create_string_array(ncpfiles, num_files,
		    &ncpval)) == NWAM_SUCCESS) {
			err = nwam_set_prop_value(objlist,
			    NWAM_NCP_OBJECT_STRING, ncpval);
		}

done_with_containers:
		nwam_value_free(enmval);
		nwam_value_free(locval);
		nwam_value_free(ncpval);
		if (ncpfiles != NULL) {
			for (j = 0; j < num_files; j++)
				free(ncpfiles[j]);
			free(ncpfiles);
		}
		if (err != NWAM_SUCCESS)
			nwam_free_object_list(objlist);
		else
			*((char **)obj) = objlist;
		return (err);
	}

	if (objname == NULL) {
		/* Allocate string array to store object names */
		if ((objnames = calloc(NWAM_MAX_NUM_OBJECTS, sizeof (char *)))
		    == NULL)
			return (NWAM_NO_MEMORY);
	}

	fp = fopen(filename, "r");
	if (fp == NULL) {
		if (errno != ENOENT) {
			if (objname == NULL)
				free(objnames);
			return (NWAM_ERROR_INTERNAL);
		}

		/*
		 * Check NCP file list in case filename passed in was derived
		 * from a case-insensitive NCP name.
		 */
		if ((err = create_ncp_file_list(&ncpfiles, &num_files))
		    == NWAM_SUCCESS) {
			for (j = 0; j < num_files; j++) {
				if (strcasecmp(ncpfiles[j], filename) == 0) {
					fp = fopen(ncpfiles[j], "r");
					if (fp != NULL) {
						/* Copy real filename back */
						(void) strlcpy(filename,
						    ncpfiles[j],
						    strlen(filename) + 1);
						break;
					}
				}
			}
			for (j = 0; j < num_files; j++)
				free(ncpfiles[j]);
			free(ncpfiles);
		}
		/* Return NOT_FOUND if file not found */
		if (fp == NULL) {
			if (objname == NULL)
				free(objnames);
			return (NWAM_ENTITY_NOT_FOUND);
		}
	}

	while (fgets(line, sizeof (line), fp) != NULL) {
		if (line[strlen(line) - 1] == '\n')
			line[strlen(line) - 1] = '\0';

		cp = line;

		while (isspace(*cp))
			cp++;

		if (*cp == '#' || *cp == '\0')
			continue;

		if ((err = nwam_line_to_object(cp, &foundobjname, &proplist))
		    != NWAM_SUCCESS)
			goto done;

		if (objname != NULL) {
			/*
			 * Is this the specified object?  If so set objname and
			 * obj and bail.
			 */
			if (strcasecmp(objname, foundobjname) == 0) {
				*((char **)obj) = proplist;
				(void) strlcpy(objname, foundobjname,
				    NWAM_MAX_NAME_LEN);
				break;
			} else {
				nwam_free_object_list(proplist);
			}
		} else {
			objnames[i] = strdup(foundobjname);
			nwam_free_object_list(proplist);
			if (objnames[i] == NULL) {
				err = NWAM_NO_MEMORY;
				goto done;
			}
			i++;
		}

	}
	if (objname == NULL) {
		/*
		 * Allocate object list with one value named
		 * NWAM_OBJECT_NAMES_STRING - it's values are the names of
		 * the objects found.
		 */
		if ((err = nwam_alloc_object_list(&objlist)) == NWAM_SUCCESS &&
		    (err = nwam_value_create_string_array(objnames, i,
		    &objnamesval)) == NWAM_SUCCESS) {
			err = nwam_set_prop_value(objlist,
			    NWAM_OBJECT_NAMES_STRING, objnamesval);
		}
	}

done:
	if (fp != NULL)
		(void) fclose(fp);

	/*
	 * We're done, either we have success, and return our object list
	 * containing object names, or we have failure and we need to free
	 * the object list.
	 */
	if (objname == NULL) {
		for (j = 0; j < i; j++)
			free(objnames[j]);
		free(objnames);
		nwam_value_free(objnamesval);
		if (err == NWAM_SUCCESS) {
			*((char **)obj) = objlist;
		} else {
			*((char **)obj) = NULL;
			nwam_free_object_list(objlist);
		}
	} else {
		/* Check if to-be-read object was not found */
		if (*((char **)obj) == NULL && err == NWAM_SUCCESS)
			return (NWAM_ENTITY_NOT_FOUND);
	}

	return (err);
}

nwam_error_t
nwam_object_to_line(FILE *fp, const char *objname, void *proplist)
{
	char *propname, *lastpropname = NULL;
	boolean_t *valbool;
	int64_t *valint;
	uint64_t *valuint;
	char **valstr;
	uint_t nelem, i;
	nwam_value_t val;
	nwam_value_type_t type;

	(void) fprintf(fp, "%s\t", objname);

	while (nwam_next_object_prop(proplist, lastpropname, &propname, &val)
	    == NWAM_SUCCESS) {

		(void) fprintf(fp, "%s%c", propname, NWAM_FILE_PROP_ASSIGN);

		if (nwam_value_get_type(val, &type) != NWAM_SUCCESS)
			return (NWAM_INVALID_ARG);

		switch (type) {
		case NWAM_VALUE_TYPE_BOOLEAN:
			(void) fprintf(fp, "%s",
			    nwam_value_type_to_string(NWAM_VALUE_TYPE_BOOLEAN));
			if (nwam_value_get_boolean_array(val, &valbool, &nelem)
			    != NWAM_SUCCESS) {
				nwam_value_free(val);
				return (NWAM_INVALID_ARG);
			}
			for (i = 0; i < nelem; i++) {
				(void) fprintf(fp, "%c",
				    NWAM_FILE_VALUE_DELIMITER);
				if (valbool[i]) {
					(void) fprintf(fp,
					    NWAM_FILE_BOOLEAN_TRUE);
				} else {
					(void) fprintf(fp,
					    NWAM_FILE_BOOLEAN_FALSE);
				}
			}
			break;

		case NWAM_VALUE_TYPE_INT64:
			(void) fprintf(fp, "%s",
			    nwam_value_type_to_string(NWAM_VALUE_TYPE_INT64));
			if (nwam_value_get_int64_array(val, &valint, &nelem)
			    != NWAM_SUCCESS) {
				nwam_value_free(val);
				return (NWAM_INVALID_ARG);
			}
			for (i = 0; i < nelem; i++) {
				(void) fprintf(fp, "%c%lld",
				    NWAM_FILE_VALUE_DELIMITER, valint[i]);
			}
			break;

		case NWAM_VALUE_TYPE_UINT64:
			(void) fprintf(fp, "%s",
			    nwam_value_type_to_string(NWAM_VALUE_TYPE_UINT64));
			if (nwam_value_get_uint64_array(val, &valuint, &nelem)
			    != NWAM_SUCCESS) {
				nwam_value_free(val);
				return (NWAM_INVALID_ARG);
			}
			for (i = 0; i < nelem; i++) {
				(void) fprintf(fp, "%c%lld",
				    NWAM_FILE_VALUE_DELIMITER, valuint[i]);
			}
			break;

		case NWAM_VALUE_TYPE_STRING:
			(void) fprintf(fp, "%s",
			    nwam_value_type_to_string(NWAM_VALUE_TYPE_STRING));
			if (nwam_value_get_string_array(val, &valstr, &nelem)
			    != NWAM_SUCCESS) {
				nwam_value_free(val);
				return (NWAM_INVALID_ARG);
			}
			for (i = 0; i < nelem; i++) {
				char evalstr[NWAM_MAX_VALUE_LEN];
				/* Add escape chars as necessary */
				value_add_escapes(valstr[i], evalstr);
				(void) fprintf(fp, "%c%s",
				    NWAM_FILE_VALUE_DELIMITER, evalstr);
			}
			break;
		default:
			nwam_value_free(val);
			return (NWAM_INVALID_ARG);
		}
		nwam_value_free(val);
		(void) fprintf(fp, "%c", NWAM_FILE_PROP_DELIMITER);

		lastpropname = propname;

	}
	(void) fprintf(fp, "\n");
	return (NWAM_SUCCESS);
}

/*
 * Write object specified by objname to file.  If objname is NULL, objlist
 * must be a list of lists, where each list corresponds to an
 * object to write to the file.  Otherwise objlist should point to a list of
 * properties for the object specified by objname.  The write operation is
 * first done to filename.new, and if this succeeds, the file is renamed to
 * filename.  Since rename(2) is atomic, this approach guarantees a complete
 * configuration will end up in filename as a result of an aborted operation.
 */
nwam_error_t
nwam_write_object_to_files_backend(const char *filename, const char *objname,
    uint64_t flags, void *objlist)
{
	void *proplist;
	char *currobjname, *lastobjname = NULL;
	int fd, cmd;
	nwam_error_t err = NWAM_SUCCESS;
	char *dir;
	char tmpfilename[MAXPATHLEN], filename_copy[MAXPATHLEN];
	FILE *fp;
	mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
	mode_t dirmode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
	struct flock fl = { F_WRLCK, SEEK_SET, 0, 0, 0};
	struct flock fu = { F_UNLCK, SEEK_SET, 0, 0, 0};

	assert(filename != NULL);

	/* Create the directory in case it does not exist. */
	(void) strlcpy(filename_copy, filename, MAXPATHLEN);
	if ((dir = dirname(filename_copy)) == NULL)
		return (nwam_errno_to_nwam_error(errno));
	if (mkdir(dir, dirmode) != 0) {
		if (errno != EEXIST)
			return (nwam_errno_to_nwam_error(errno));
	}

	(void) snprintf(tmpfilename, MAXPATHLEN, "%s.new", filename);

	if ((fd = open(tmpfilename, O_RDWR | O_CREAT | O_TRUNC, mode)) < 0)
			return (nwam_errno_to_nwam_error(errno));

	if ((fp = fdopen(fd, "w")) == NULL) {
		err = nwam_errno_to_nwam_error(errno);
		goto done;
	}
	/*
	 * Need to lock filename.new to prevent multiple commits colliding
	 * at this point.
	 */
	if (flags & NWAM_FLAG_BLOCKING)
		cmd = F_SETLKW;
	else
		cmd = F_SETLK;
	if (fcntl(fd, cmd, &fl) != 0) {
		if (errno == EAGAIN)
			return (NWAM_ENTITY_IN_USE);
		else
			return (NWAM_ERROR_INTERNAL);
	}

	if (objname != NULL) {
		/* Only one object to write */
		err = nwam_object_to_line(fp, objname, objlist);
	} else {
		if (objlist == NULL) {
			err = NWAM_SUCCESS;
			goto done;
		}
		/* Otherwise, write each object in turn. */
		while ((err = nwam_next_object_list(objlist, lastobjname,
		    &currobjname, &proplist)) == NWAM_SUCCESS) {
			if ((err = nwam_object_to_line(fp, currobjname,
			    proplist)) != NWAM_SUCCESS)
				break;
			lastobjname = currobjname;
		}
		if (err == NWAM_LIST_END)
			err = NWAM_SUCCESS;
	}
done:
	if (err == NWAM_SUCCESS) {
		if (rename(tmpfilename, filename) == 0) {
			(void) fcntl(fd, F_SETLKW, &fu);
			(void) fclose(fp);
			return (NWAM_SUCCESS);
		} else {
			err = nwam_errno_to_nwam_error(errno);
		}
	}
	(void) fcntl(fd, F_SETLKW, &fu);
	(void) fclose(fp);
	(void) unlink(tmpfilename);

	return (err);
}

/*
 * Read in all objects from file and update object corresponding to objname
 * with properties recorded in proplist, and then write results to filename.
 * If objname is empty, no object needs to be updated.  If proplist is NULL,
 * object is to be removed (this is done by simply not adding it to the list
 * of objects).
 */
nwam_error_t
nwam_update_object_in_files_backend(char *filename, char *objname,
    uint64_t flags, void *proplist)
{
	nwam_error_t err;
	void *objlist, *objnamelist;
	char **object_names;
	nwam_value_t value = NULL;
	uint_t i, num_objects;

	assert(filename != NULL);

	/*  If we find existing object, fail if creation was specified */
	if (flags & NWAM_FLAG_CREATE) {
		char discard_objname[NWAM_MAX_NAME_LEN];
		void *discard_objlist;

		(void) strlcpy(discard_objname, objname,
		    sizeof (discard_objname));
		if ((err = nwam_read_object_from_files_backend(filename,
		    discard_objname, 0, &discard_objlist)) == NWAM_SUCCESS) {
			nwam_free_object_list(discard_objlist);
			return (NWAM_ENTITY_EXISTS);
		}
	}

	/* Get existing list of object names (if any) */
	err = nwam_read_object_from_files_backend(filename, NULL, flags,
	    &objnamelist);
	switch (err) {
	case NWAM_SUCCESS:
		/*
		 * For each object name on list other than the one to be
		 * updated,  add an object list consisting of its properties.
		 * The object to be updated (if any) will be added below.
		 */
		if ((err = nwam_alloc_object_list(&objlist)) != NWAM_SUCCESS) {
			nwam_free_object_list(objnamelist);
			return (err);
		}
		if ((err = nwam_get_prop_value(objnamelist,
		    NWAM_OBJECT_NAMES_STRING, &value)) != NWAM_SUCCESS ||
		    (err = nwam_value_get_string_array(value, &object_names,
		    &num_objects)) != NWAM_SUCCESS) {
			nwam_value_free(value);
			nwam_free_object_list(objnamelist);
			nwam_free_object_list(objlist);
			return (err);
		}
		nwam_free_object_list(objnamelist);

		for (i = 0; i < num_objects; i++) {
			void *oproplist = NULL;

			if (objname != NULL &&
			    strcmp(objname, object_names[i]) == 0)
					continue;

			if ((err = nwam_read_object_from_files_backend(filename,
			    object_names[i], flags, &oproplist))
			    != NWAM_SUCCESS ||
			    (err = nwam_object_list_add_object_list(objlist,
			    object_names[i], oproplist)) != NWAM_SUCCESS) {
				nwam_free_object_list(oproplist);
				nwam_free_object_list(objlist);
				nwam_value_free(value);
				return (err);
			}
			nwam_free_object_list(oproplist);
		}
		nwam_value_free(value);
		break;

	case NWAM_ENTITY_NOT_FOUND:
		/*
		 * Just need to write/remove this single object.
		 */
		return (nwam_write_object_to_files_backend(filename, objname,
		    flags, proplist));

	default:
		return (err);
	}

	/*
	 * Add the object to be updated to our list of objects if the
	 * property list is non-NULL (NULL signifies remove the object).
	 */
	if (objname != NULL && proplist != NULL) {
		if ((err = nwam_object_list_add_object_list(objlist,
		    (char *)objname, proplist)) != NWAM_SUCCESS) {
			nwam_free_object_list(objlist);
			return (err);
		}
	}

	err = nwam_write_object_to_files_backend(filename, NULL, flags,
	    objlist);

	nwam_free_object_list(objlist);

	return (err);
}

/*
 * Remove specified object from file by reading in the list of objects,
 * removing objname and writing the remainder.
 */
nwam_error_t
nwam_remove_object_from_files_backend(char *filename, char *objname,
    uint64_t flags)
{
	int uerr;

	assert(filename != NULL);

	if (objname == NULL) {
		/*
		 * NULL objname signifies remove file.
		 */
		uerr = unlink(filename);
		if (uerr != 0)
			return (nwam_errno_to_nwam_error(errno));
		return (NWAM_SUCCESS);
	}

	return (nwam_update_object_in_files_backend(filename, objname, flags,
	    NULL));
}