summaryrefslogtreecommitdiff
path: root/usr/src/cmd/fs.d/nfs/share/share.c
blob: 58a98324d26cd1dc13a201d3a0014a63c8050187 (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
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (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 2005 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
/*	  All Rights Reserved  	*/

/*
 * University Copyright- Copyright (c) 1982, 1986, 1988
 * The Regents of the University of California
 * All Rights Reserved
 *
 * University Acknowledgment- Portions of this document are derived from
 * software developed by the University of California, Berkeley, and its
 * contributors.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

/*
 * nfs share
 */
#define	_REENTRANT

#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <unistd.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/param.h>	/* for UID_NOBODY */
#include <sys/stat.h>
#include <errno.h>
#include <rpc/rpc.h>
#include <netconfig.h>
#include <netdir.h>
#include <nfs/nfs_sec.h>
#include <nfs/export.h>
#include <locale.h>
#include <zone.h>
#include <rpcsvc/daemon_utils.h>
#include "../lib/nfslog_config.h"
#include "../lib/sharetab.h"
#include "../lib/nfslogtab.h"

#define	RET_OK		0
#define	RET_ERR		32

static int addlogconfig(struct exportdata *, char *, nfsl_config_t *);
static void configlog(struct exportdata *, char *);
static int direq(char *, char *);
static int newopts(char *);
static void parseopts_old(struct exportdata *, char *);
static void parseopts_new(struct exportdata *, char *);
static void pr_err(char *, ...);
static int shareable(char *);
static int sharetab_add(char *, char *, char *, char *, int);
static int sharepub_exist(char *);
static caddr_t *get_rootnames(seconfig_t *, char *, int *);
static void usage();
static void exportindex(struct exportdata *, char *);
static int nfslogtab_add(char *, char *, char *);
static int nfslogtab_deactivate(char *);

extern int issubdir();
extern int exportfs();
int nfs_getseconfig_byname(char *, seconfig_t *);
static void printarg(char *, struct exportdata *);
static struct exportdata ex;

/*
 * list of support services needed
 */
static char *service_list[] =
	{ STATD, LOCKD, MOUNTD, NFSD, NFSMAPID, RQUOTAD, NULL };

int
main(int argc, char *argv[])
{
	extern int optind;
	extern char *optarg;
	char dir[MAXPATHLEN];
	char *res = "-";
	char *opts = "rw";
	char *descr = "";
	char *services;
	int c;
	int replace = 0;
	int verbose = 0;

	/* Don't drop core if the NFS module isn't loaded. */
	signal(SIGSYS, SIG_IGN);

	(void) setlocale(LC_ALL, "");
#if !defined(TEXT_DOMAIN)
#define	TEXT_DOMAIN	"SYS_TEST"
#endif
	(void) textdomain(TEXT_DOMAIN);

	while ((c = getopt(argc, argv, "o:d:v")) != EOF) {
		switch (c) {
		case 'o':
			opts = optarg;
			break;
		case 'd':
			descr = optarg;
			break;
		case 'v':
			verbose++;
			break;
		default:
			usage();
			exit(RET_ERR);
		}
	}

	if (argc <= optind || argc - optind > 2) {
		usage();
		exit(RET_ERR);
	}
	if (realpath(argv[optind], dir) == NULL)
		pr_err("%s: %s\n", argv[optind], strerror(errno));

	if (argc - optind > 1)
		res = argv[optind + 1];

	switch (shareable(dir)) {
	case 0:
		exit(RET_ERR);
		break;
	case 1:
		break;
	case 2:
		replace = 1;
		break;
	}

	ex.ex_path = dir;
	ex.ex_pathlen = strlen(dir) + 1;

	if (newopts(opts))
		parseopts_new(&ex, opts);
	else
		parseopts_old(&ex, opts);

	/*
	 * If -o public was specified, check for any existing directory
	 * shared with -o public. If so, fail.
	 */
	if (ex.ex_flags & EX_PUBLIC) {
		if (sharepub_exist(dir) == 1) {
			errno = 0;
			pr_err(
			gettext("Cannot share more than filesystem with"
				" 'public' option\n"));
		}
	}

	if (verbose)
		printarg(dir, &ex);

	if (exportfs(dir, &ex) < 0) {
		switch (errno) {
		case EREMOTE:
			pr_err(gettext("Cannot share remote filesystem: %s\n"),
				dir);
			break;
		case EPERM:
			if (getzoneid() != GLOBAL_ZONEID) {
				pr_err(gettext("Cannot share filesystems "
					"in non-global zones: %s\n"),
					dir);
				break;
			}
			/* FALLTHRU */
		default:
			pr_err("%s: %s\n", dir, strerror(errno));
			break;
		}
	}

	if (sharetab_add(dir, res, opts, descr, replace) < 0)
		exit(RET_ERR);

	if (ex.ex_flags & EX_LOG)
		if (nfslogtab_add(dir, ex.ex_log_buffer, ex.ex_tag))
			exit(RET_ERR);

	/*
	 * enable services as needed.
	 */
	_check_services(service_list);

	return (RET_OK);
}

/*
 * Check if there already is an entry shared with -o public.
 */
static int
sharepub_exist(char *dir)
{
	struct share *sh;
	int res;
	FILE *f;
	char *val;

	f = fopen(SHARETAB, "r");

	if (f == NULL) {
		if (errno == ENOENT)
			return (0);
		pr_err("%s: %s\n", SHARETAB, strerror(errno));
	}

	while ((res = getshare(f, &sh)) > 0) {
		if (strcmp(sh->sh_fstype, "nfs") != 0)
			continue;

		if (strcmp(sh->sh_path, dir) == 0)
			continue;

		if (val = getshareopt(sh->sh_opts, SHOPT_PUBLIC)) {
			free(val);
			return (1);
		}
	}

	if (res < 0) {
		pr_err(gettext("error reading %s\n"), SHARETAB);
		(void) fclose(f);
	}

	(void) fclose(f);
	return (0);
}

/*
 * Check the nfs share entries in sharetab file.
 * Returns:
 *	0  dir not shareable
 *	1  dir is shareable
 *	2  dir is already shared (can modify options)
 */
static int
shareable(path)
	char *path;
{
	FILE *f;
	struct share *sh;
	struct stat st;
	int res;

	errno = 0;
	if (*path != '/')
		pr_err(gettext("%s: not a full pathname\n"), path);

	if (stat(path, &st) < 0) 	/* does it exist ? */
		pr_err(gettext("%s: %s\n"), path, strerror(errno));

	/*
	 * We make the assumption that if we can't open the SHARETAB
	 * file for some reason other than it doesn't exist, then we
	 * won't share the directory.  Since we can't complete the
	 * operation correctly, then let's not do it at all.
	 */
	f = fopen(SHARETAB, "r");
	if (f == NULL) {
		if (errno == ENOENT)
			return (1);
		pr_err("%s: %s\n", SHARETAB, strerror(errno));
		return (0);
	}

	while ((res = getshare(f, &sh)) > 0) {
		if (strcmp(sh->sh_fstype, "nfs") != 0)
			continue;

		if (direq(path, sh->sh_path)) {
			(void) fclose(f);
			return (2);
		}

		if (issubdir(sh->sh_path, path)) {
			pr_err(gettext("%s: sub-directory "
				"(%s) already shared\n"),
				path, sh->sh_path);
		}
		if (issubdir(path, sh->sh_path)) {
			pr_err(gettext("%s: parent-directory "
				"(%s) already shared\n"),
				path, sh->sh_path);
		}
	}

	if (res < 0)
		pr_err(gettext("error reading %s\n"), SHARETAB);

	(void) fclose(f);
	return (1);
}

static int
direq(dir1, dir2)
	char *dir1, *dir2;
{
	struct stat st1, st2;

	if (strcmp(dir1, dir2) == 0)
		return (1);
	if (stat(dir1, &st1) < 0 || stat(dir2, &st2) < 0)
		return (0);
	return (st1.st_ino == st2.st_ino && st1.st_dev == st2.st_dev);
}

static char *optlist[] = {
#define	OPT_RO		0
	SHOPT_RO,
#define	OPT_RW		1
	SHOPT_RW,
#define	OPT_ROOT	2
	SHOPT_ROOT,
#define	OPT_SECURE	3
	SHOPT_SECURE,
#define	OPT_ANON	4
	SHOPT_ANON,
#define	OPT_WINDOW	5
	SHOPT_WINDOW,
#define	OPT_NOSUID	6
	SHOPT_NOSUID,
#define	OPT_ACLOK	7
	SHOPT_ACLOK,
#define	OPT_NOSUB	8
	SHOPT_NOSUB,
#define	OPT_SEC		9
	SHOPT_SEC,
#define	OPT_PUBLIC	10
	SHOPT_PUBLIC,
#define	OPT_INDEX	11
	SHOPT_INDEX,
#define	OPT_LOG		12
	SHOPT_LOG,
#ifdef VOLATILE_FH_TEST	/* XXX added for testing volatile fh's only */
#define	OPT_VOLFH	13
	SHOPT_VOLFH,
#endif /* VOLATILE_FH_TEST */
	NULL
};

/*
 * If the option string contains a "sec="
 * option, then use new option syntax.
 */
static int
newopts(char *opts)
{
	char *p, *val;

	p = strdup(opts);
	if (p == NULL)
		pr_err(gettext("opts: no memory\n"));

	while (*p)
		if (getsubopt(&p, optlist, &val) == OPT_SEC)
			return (1);

	return (0);
}

#ifdef VOLATILE_FH_TEST	/* XXX added for testing volatile fh's only */
/*
 * Set the ex_flags to indicate which fh expire type. Return 0 for success,
 * error otherwise.
 */
static int
nfs4_set_volfh_flags(struct exportdata *exp, char *volfhtypes)
{
	char	*voltype, *next;
	int	err = 0;

	for (voltype = volfhtypes; !err && voltype != NULL; voltype = next) {
		while (*voltype == ':') voltype++;
		next = strchr(voltype, ':');
		if (next != NULL)
			*next = '\0';
		if (strcmp(voltype, "any") == 0)
			exp->ex_flags |= EX_VOLFH;
		else if (strcmp(voltype, "rnm") == 0)
			exp->ex_flags |= EX_VOLRNM;
		else if (strcmp(voltype, "mig") == 0)
			exp->ex_flags |= EX_VOLMIG;
		else if (strcmp(voltype, "noexpopen") == 0)
			exp->ex_flags |= EX_NOEXPOPEN;
		else {
			err = EINVAL;		/* invalid arg */
		}
		if (next != NULL)
			*next = ':';
	}
	return (err);
}
#endif /* VOLATILE_FH_TEST */

#define	badnum(x) ((x) == NULL || !isdigit(*(x)))
#define	DEF_WIN	30000

/*
 * Parse the share options from the "-o" flag.
 * The extracted data is moved into the exports
 * structure which is passed into the kernel via
 * the exportfs() system call.
 */
static void
parseopts_old(struct exportdata *exp, char *opts)
{
	char *p, *savep, *val, *rootlist;
	struct secinfo *sp;
	int done_aclok = 0;
	int done_nosuid = 0;
	int done_anon = 0;


	p = strdup(opts);
	if (p == NULL)
		pr_err(gettext("opts: no memory\n"));

	exp->ex_version = 2;
	exp->ex_anon = UID_NOBODY;
	exp->ex_seccnt = 1;
	exp->ex_index = NULL;

	sp = (struct secinfo *)malloc(sizeof (struct secinfo));
	if (sp == NULL)
		pr_err(gettext("ex_secinfo: no memory\n"));
	exp->ex_secinfo = sp;

	/*
	 * Initialize some fields
	 */
	sp->s_flags = 0;
	sp->s_window = DEF_WIN;
	sp->s_rootcnt = 0;
	sp->s_rootnames = NULL;

	if (nfs_getseconfig_default(&sp->s_secinfo))
		pr_err(gettext("failed to get default security mode\n"));

	while (*p) {
		savep = p;
		switch (getsubopt(&p, optlist, &val)) {

		case OPT_RO:

			sp->s_flags |= val ? M_ROL : M_RO;

			if (sp->s_flags & M_RO && sp->s_flags & M_RW)
				pr_err(gettext("rw vs ro conflict\n"));
			if (sp->s_flags & M_RO && sp->s_flags & M_ROL)
				pr_err(gettext("Ambiguous ro options\n"));
			break;

		case OPT_RW:

			sp->s_flags |= val ? M_RWL : M_RW;

			if (sp->s_flags & M_RO && sp->s_flags & M_RW)
				pr_err(gettext("ro vs rw conflict\n"));
			if (sp->s_flags & M_RW && sp->s_flags & M_RWL)
				pr_err(gettext("Ambiguous rw options\n"));
			break;

		case OPT_ROOT:
			if (val == NULL)
				pr_err(gettext("missing root list\n"));
			rootlist = val;
			sp->s_flags |= M_ROOT;
			break;

		case OPT_SECURE:
			if (nfs_getseconfig_byname("dh", &sp->s_secinfo)) {
				pr_err(gettext("invalid sec name\n"));
			}
			break;

		case OPT_ANON:
			if (done_anon++)
				pr_err(gettext("option anon repeated\n"));

			if (!val) {
				pr_err(gettext("missing anon value\n"));
			}
			/* check for special "-1" value, which is ok */
			if (strcmp(val, "-1") != 0 && badnum(val)) {
				pr_err(gettext("invalid anon value\n"));
			}
			exp->ex_anon = atoi(val);
			break;

		case OPT_WINDOW:
			if (badnum(val))
				pr_err(gettext("invalid window value\n"));
			sp->s_window = atoi(val);
			break;

		case OPT_NOSUID:
			if (done_nosuid++)
				pr_err(gettext("option nosuid repeated\n"));

			exp->ex_flags |= EX_NOSUID;
			break;

		case OPT_ACLOK:
			if (done_aclok++)
				pr_err(gettext("option aclok repeated\n"));
			exp->ex_flags |= EX_ACLOK;
			break;

		case OPT_NOSUB:
			/*
			 * The "don't allow mount of subdirectories" option.
			 */
			exp->ex_flags |= EX_NOSUB;
			break;

		case OPT_PUBLIC:
			exp->ex_flags |= EX_PUBLIC;
			break;

		case OPT_INDEX:
			exportindex(exp, val);
			break;

		case OPT_LOG:
			configlog(exp, val);
			break;

#ifdef VOLATILE_FH_TEST	/* XXX added for testing volatile fh's only */
		case OPT_VOLFH:
			/* volatile filehandles - expire on share */
			if (val == NULL)
				pr_err(gettext("missing volatile fh types\n"));
			if (nfs4_set_volfh_flags(exp, val))
				pr_err(gettext("invalid volatile fh types\n"));
			break;
#endif /* VOLATILE_FH_TEST */

		default:
			pr_err(gettext("invalid share option: '%s'\n"), savep);
		}
	}
	if (sp->s_flags & M_ROOT && sp->s_secinfo.sc_rpcnum != AUTH_UNIX) {
		sp->s_rootnames = get_rootnames(&sp->s_secinfo, rootlist,
				&sp->s_rootcnt);
		if (sp->s_rootnames == NULL)
			pr_err(gettext("Bad root list\n"));
	}

	/*
	 * Set uninitialized flags to "rw"
	 */
	if ((sp->s_flags & (M_RO|M_RW|M_RWL|M_ROL)) == 0)
		sp->s_flags |= M_RW;
}

/*
 * Parse the new share options from the "-o" flag.
 * Parsing is more complicated than the old case
 * Since we may be setting up multiple secinfo entries.
 * Syntax is more restrictive: the flavor-dependent
 * options: ro, rw, root, window can only follow
 * a sec option.
 */
static void
parseopts_new(struct exportdata *exp, char *opts)
{
	char *p, *q, *savep, *val;
	char *f, *lasts;
	struct secinfo *sp1, *sp, *pt;
	int i, secopt;
	int count = 0;
	int done_aclok = 0;
	int done_nosuid = 0;
	int done_anon = 0;

	exp->ex_version = 2;
	exp->ex_anon = UID_NOBODY;
	exp->ex_index = NULL;

	p = strdup(opts);
	if (p == NULL)
		pr_err(gettext("opts: no memory\n"));

	/*
	 * Count the number of security modes
	 */
	while (*p) {
		switch (getsubopt(&p, optlist, &val)) {
		case OPT_SECURE:
			pr_err(gettext("Cannot mix options "
				"secure and sec\n"));
			break;
		case OPT_SEC:
			count++;
			for (q = val; *q; q++)
				if (*q == ':')
					count++;
			break;
		}
	}

	exp->ex_seccnt = count;

	sp = (struct secinfo *)calloc(count, sizeof (struct secinfo));
	if (sp == NULL)
		pr_err(gettext("ex_secinfo: no memory\n"));

	/*
	 * Initialize some fields
	 */
	for (i = 0; i < count; i++) {
		sp[i].s_flags = 0;
		sp[i].s_window = DEF_WIN;
		sp[i].s_rootcnt = 0;
		sp[i].s_rootnames = NULL;
	}

	exp->ex_secinfo = sp;
	sp1 = sp;

	p = strdup(opts);
	if (p == NULL)
		pr_err(gettext("opts: no memory\n"));

	if (nfs_getseconfig_default(&sp->s_secinfo))
		pr_err(gettext("failed to get default security mode\n"));

	secopt = 0;

	while (*p) {
		savep = p;
		switch (getsubopt(&p, optlist, &val)) {

		case OPT_SEC:
			if (secopt)
				sp++;
			sp1 = sp;
			secopt++;

			while ((f = strtok_r(val, ":", &lasts)) != NULL) {
				if (nfs_getseconfig_byname(f, &sp->s_secinfo))
					pr_err(gettext("Invalid security mode"
						" \"%s\"\n"), f);
				val = NULL;
				if (lasts)
					sp++;
			}
			break;

		case OPT_RO:
			if (secopt == 0)
				pr_err(gettext("need sec option before ro\n"));

			sp->s_flags |= val ? M_ROL : M_RO;

			if (sp->s_flags & M_RO && sp->s_flags & M_RW)
				pr_err(gettext("rw vs ro conflict\n"));
			if (sp->s_flags & M_RO && sp->s_flags & M_ROL)
				pr_err(gettext("Ambiguous ro options\n"));

			for (pt = sp1; pt < sp; pt++)
				pt->s_flags = sp->s_flags;
			break;

		case OPT_RW:
			if (secopt == 0)
				pr_err(gettext("need sec option before rw\n"));

			sp->s_flags |= val ? M_RWL : M_RW;

			if (sp->s_flags & M_RO && sp->s_flags & M_RW)
				pr_err(gettext("ro vs rw conflict\n"));
			if (sp->s_flags & M_RW && sp->s_flags & M_RWL)
				pr_err(gettext("Ambiguous rw options\n"));

			for (pt = sp1; pt < sp; pt++)
				pt->s_flags = sp->s_flags;
			break;

		case OPT_ROOT:
			if (secopt == 0)
				pr_err(gettext("need sec option before "
					"root\n"));

			if (val == NULL)
				pr_err(gettext("missing root list\n"));

			for (pt = sp1; pt <= sp; pt++) {
				pt->s_flags |= M_ROOT;

				/*
				 * Can treat AUTH_UNIX root lists
				 * as a special case and have
				 * the nfsauth service check the
				 * list just like any other access
				 * list, i.e. supports netgroups,
				 * domain suffixes, etc.
				 */
				if (pt->s_secinfo.sc_rpcnum == AUTH_UNIX)
					continue;

				/*
				 * Root lists for other sec types
				 * need to be checked in the
				 * kernel. Build a list of names
				 * to be fed into the kernel via
				 * exportfs().
				 */
				pt->s_rootnames =
					get_rootnames(&pt->s_secinfo, val,
						&pt->s_rootcnt);
				if (pt->s_rootnames == NULL)
					pr_err(gettext("Bad root list\n"));
			}
			break;

		case OPT_WINDOW:
			if (secopt == 0)
				pr_err(gettext("need sec option before "
					"window\n"));

			if (badnum(val))
				pr_err(gettext("invalid window value\n"));
			sp->s_window = atoi(val);

			for (pt = sp1; pt < sp; pt++)
				pt->s_window = sp->s_window;
			break;

		case OPT_ANON:
			if (done_anon++)
				pr_err(gettext("option anon repeated\n"));

			if (!val) {
				pr_err(gettext("missing anon value\n"));
			}
			/* check for special "-1" value, which is ok */
			if (strcmp(val, "-1") != 0 && badnum(val))
				pr_err(gettext("invalid anon value\n"));

			exp->ex_anon = atoi(val);
			break;

		case OPT_NOSUID:
			if (done_nosuid++)
				pr_err(gettext("option nosuid repeated\n"));

			exp->ex_flags |= EX_NOSUID;
			break;

		case OPT_ACLOK:
			if (done_aclok++)
				pr_err(gettext("option aclok repeated\n"));
			exp->ex_flags |= EX_ACLOK;
			break;

		case OPT_NOSUB:
			/*
			 * The "don't allow mount of subdirectories" option.
			 */
			exp->ex_flags |= EX_NOSUB;
			break;

		case OPT_PUBLIC:
			exp->ex_flags |= EX_PUBLIC;
			break;

		case OPT_INDEX:
			exportindex(exp, val);
			break;

		case OPT_LOG:
			configlog(exp, val);
			break;

#ifdef VOLATILE_FH_TEST	/* XXX added for testing volatile fh's only */
		case OPT_VOLFH:
			/* volatile filehandles - expire on share */
			if (val == NULL)
				pr_err(gettext("missing volatile fh types\n"));
			if (nfs4_set_volfh_flags(exp, val))
				pr_err(gettext("invalid volatile fh types\n"));
			break;
#endif /* VOLATILE_FH_TEST */

		default:
			pr_err(gettext("invalid share option: '%s'\n"), savep);
		}
	}

	/*
	 * Set uninitialized flags to "rw"
	 */
	sp = exp->ex_secinfo;
	for (i = 0; i < count; i++) {
		if ((sp[i].s_flags & (M_RO|M_RW|M_RWL|M_ROL)) == 0)
			sp[i].s_flags |= M_RW;
	}
}

/*
 * check the argument specified with the index option and set
 * export index file and flags
 */
static void
exportindex(struct exportdata *exp, char *val)
{
	char *p = val;

	if (val == NULL)
		goto badindexarg;

	p = val;
	while (*p != '\0') {
		if (*p == '/')
			goto badindexarg;
		p++;
	}

	if (strcmp(val, "..") == 0)
		goto badindexarg;

	/*
	 * treat a "." or an empty index string as if the
	 * index option is not present.
	 */
	if (val[0] == '\0' || (strcmp(val, ".") == 0))
		return;

	exp->ex_index = strdup(val);
	if (!exp->ex_index) {
		pr_err(gettext("exportindex: out of memory\n"));
		return;
	}
	exp->ex_flags |= EX_INDEX;

	return;

badindexarg:
	pr_err(gettext("index option requires a filename as argument\n"));
}

/*
 * Given a seconfig entry and a colon-separated
 * list of names, allocate an array big enough
 * to hold the root list, then convert each name to
 * a principal name according to the security
 * info and assign it to an array element.
 * Return the array and its size.
 */
static caddr_t *
get_rootnames(seconfig_t *sec, char *list, int *count)
{
	caddr_t *a;
	int c, i;
	char *host, *p;

	list = strdup(list);
	if (list == NULL)
		pr_err(gettext("get_rootnames: no memory\n"));

	/*
	 * Count the number of strings in the list.
	 * This is the number of colon separators + 1.
	 */
	c = 1;
	for (p = list; *p; p++)
		if (*p == ':')
			c++;
	*count = c;

	a = (caddr_t *)malloc(c * sizeof (char *));
	if (a == NULL)
		pr_err(gettext("get_rootnames: no memory\n"));

	for (i = 0; i < c; i++) {
		host = strtok(list, ":");
		if (!nfs_get_root_principal(sec, host, &a[i])) {
			a = NULL;
			break;
		}
		list = NULL;
	}

	return (a);
}

/*
 * Append an entry to the sharetab file
 */
static int
sharetab_add(dir, res, opts, descr, replace)
	char *dir, *res, *opts, *descr;
	int replace;
{
	int ret;
	FILE *f;
	struct share sh;
	int logging = 0;

	/*
	 * Open the file for update and create it if necessary.
	 * This may leave the I/O offset at the end of the file,
	 * so rewind back to the beginning of the file.
	 */
	f = fopen(SHARETAB, "a+");
	if (f == NULL) {
		pr_err("%s: %s\n", SHARETAB, strerror(errno));
		return (-1);
	}
	rewind(f);

	if (lockf(fileno(f), F_LOCK, 0L) < 0) {
		pr_err(gettext("cannot lock %s: %s\n"),
			SHARETAB, strerror(errno));
		(void) fclose(f);
		return (-1);
	}

	/*
	 * If re-sharing an old share with new options
	 * then first remove the old share entry.
	 */
	if (replace) {
		if ((ret = remshare(f, dir, &logging)) < 0) {
			switch (ret) {
			case -1:
				pr_err(gettext("share complete, however, "
					"failed to remove old sharetab"
					" entry, no memory\n"));
				break;
			case -2:
				pr_err(gettext("share complete, however, "
					"sharetab may be corrupt, ftrucate call"
					" failure during sharetab update\n"));
				break;
			case -3:
				pr_err(gettext("share complete, however, "
					"failed to remove old sharetab entry,"
					" corrupt sharetab file\n"));
				break;
			}
		}

		if (logging) {
			/*
			 * Entry replaced was logged, deactivate it in
			 * nfslogtab.
			 */
			(void) nfslogtab_deactivate(dir);
		}
	}

	sh.sh_path = dir;
	sh.sh_res = res;
	sh.sh_fstype = "nfs";
	sh.sh_opts = opts;
	sh.sh_descr = descr;

	if (putshare(f, &sh) < 0)
		pr_err(gettext("addshare: couldn't add %s to %s\n"),
			dir, SHARETAB);

	(void) fclose(f);
	return (0);
}

/*
 * Append an entry to the nfslogtab file
 */
static int
nfslogtab_add(dir, buffer, tag)
	char *dir, *buffer, *tag;
{
	FILE *f;
	struct logtab_ent lep;
	int error = 0;

	/*
	 * Open the file for update and create it if necessary.
	 * This may leave the I/O offset at the end of the file,
	 * so rewind back to the beginning of the file.
	 */
	f = fopen(NFSLOGTAB, "a+");
	if (f == NULL) {
		error = errno;
		pr_err(gettext(
			"share complete, however failed to open %s "
			"for update: %s\n"), NFSLOGTAB, strerror(errno));
		goto out;
	}
	rewind(f);

	if (lockf(fileno(f), F_LOCK, 0L) < 0) {
		pr_err(gettext(
			"share complete, however failed to lock %s "
			"for update: %s\n"), NFSLOGTAB, strerror(errno));
		error = -1;
		goto out;
	}

	if (logtab_deactivate_after_boot(f) == -1) {
		pr_err(gettext(
			"share complete, however could not deactivate "
			"entries in %s\n"), NFSLOGTAB);
		error = -1;
		goto out;
	}

	/*
	 * Remove entries matching buffer and sharepoint since we're
	 * going to replace it with perhaps an entry with a new tag.
	 */
	if (logtab_rement(f, buffer, dir, NULL, -1)) {
		pr_err(gettext(
			"share complete, however could not remove matching "
			"entries in %s\n"), NFSLOGTAB);
		error = -1;
		goto out;
	}

	/*
	 * Deactivate all active entries matching this sharepoint
	 */
	if (logtab_deactivate(f, NULL, dir, NULL)) {
		pr_err(gettext(
			"share complete, however could not deactivate matching "
			"entries in %s\n"), NFSLOGTAB);
		error = -1;
		goto out;
	}

	lep.le_buffer = buffer;
	lep.le_path = dir;
	lep.le_tag = tag;
	lep.le_state = LES_ACTIVE;

	/*
	 * Add new sharepoint / buffer location to nfslogtab
	 */
	if (logtab_putent(f, &lep) < 0) {
		pr_err(gettext(
			"share complete, however could not add %s to %s\n"),
			dir, NFSLOGTAB);
		error = -1;
	}

out:
	if (f != NULL)
		(void) fclose(f);
	return (error);
}

/*
 * Deactivate an entry from the nfslogtab file
 */
static int
nfslogtab_deactivate(path)
	char *path;
{
	FILE *f;
	int error = 0;

	f = fopen(NFSLOGTAB, "r+");
	if (f == NULL) {
		error = errno;
		fprintf(stderr, gettext(
			"share complete, however could not open %s for "
			"update: %s\n"), NFSLOGTAB, strerror(error));
		goto out;
	}
	if (lockf(fileno(f), F_LOCK, 0L) < 0) {
		error = errno;
		pr_err(gettext(
			"share complete, however could not lock %s for "
			"update: %s\n"), NFSLOGTAB, strerror(error));
		goto out;
	}
	if (logtab_deactivate(f, NULL, path, NULL) == -1) {
		error = -1;
		pr_err(gettext("share complete, however could not "
			"deactivate %s in %s\n"), path, NFSLOGTAB);
		goto out;
	}

out:	if (f != NULL)
		(void) fclose(f);

	return (error);
}

static void
usage()
{
	(void) fprintf(stderr,
	    "Usage: share [-o options] [-d description] pathname [resource]\n");
}

/*
 * This is for testing only
 * It displays the export structure that
 * goes into the kernel.
 */
static void
printarg(char *path, struct exportdata *ep)
{
	int i, j;
	struct secinfo *sp;

	printf("%s:\n", path);
	printf("\tex_version = %d\n", ep->ex_version);
	printf("\tex_path = %s\n", ep->ex_path);
	printf("\tex_pathlen = %d\n", ep->ex_pathlen);
	printf("\tex_flags: (0x%02x) ", ep->ex_flags);
	if (ep->ex_flags & EX_NOSUID)
		printf("NOSUID ");
	if (ep->ex_flags & EX_ACLOK)
		printf("ACLOK ");
	if (ep->ex_flags & EX_PUBLIC)
		printf("PUBLIC ");
	if (ep->ex_flags & EX_NOSUB)
		printf("NOSUB ");
	if (ep->ex_flags & EX_LOG)
		printf("LOG ");
	if (ep->ex_flags & EX_LOG_ALLOPS)
		printf("LOG_ALLOPS ");
	if (ep->ex_flags == 0)
		printf("(none)");
	printf("\n");
	if (ep->ex_flags & EX_LOG) {
		printf("\tex_log_buffer = %s\n",
			(ep->ex_log_buffer ? ep->ex_log_buffer : "(NULL)"));
		printf("\tex_tag = %s\n", (ep->ex_tag ? ep->ex_tag : "(NULL)"));
	}
	printf("\tex_anon = %d\n", ep->ex_anon);
	printf("\tex_seccnt = %d\n", ep->ex_seccnt);
	printf("\n");
	for (i = 0; i < ep->ex_seccnt; i++) {
		sp = &ep->ex_secinfo[i];
		printf("\t\ts_secinfo = %s\n", sp->s_secinfo.sc_name);
		printf("\t\ts_flags: (0x%02x) ", sp->s_flags);
		if (sp->s_flags & M_ROOT) printf("M_ROOT ");
		if (sp->s_flags & M_RO) printf("M_RO ");
		if (sp->s_flags & M_ROL) printf("M_ROL ");
		if (sp->s_flags & M_RW) printf("M_RW ");
		if (sp->s_flags & M_RWL) printf("M_RWL ");
		if (sp->s_flags == 0) printf("(none)");
		printf("\n");
		printf("\t\ts_window = %d\n", sp->s_window);
		printf("\t\ts_rootcnt = %d ", sp->s_rootcnt);
		for (j = 0; j < sp->s_rootcnt; j++)
			printf("%s ", sp->s_rootnames[j]);
		printf("\n\n");
	}
}

/*
 * Look for the specified tag in the configuration file. If it is found,
 * enable logging and set the logging configuration information for exp.
 */
static void
configlog(struct exportdata *exp, char *tag)
{
	nfsl_config_t *configlist, *configp;
	int error = 0;
	char globaltag[] = DEFAULTTAG;

	/*
	 * Sends config errors to stderr
	 */
	nfsl_errs_to_syslog = B_FALSE;

	/*
	 * get the list of configuration settings
	 */
	error = nfsl_getconfig_list(&configlist);
	if (error) {
		pr_err(gettext("Cannot get log configuration: %s\n"),
			strerror(error));
	}

	if (tag == NULL)
		tag = globaltag;
	if ((configp = nfsl_findconfig(configlist, tag, &error)) == NULL) {
		nfsl_freeconfig_list(&configlist);
		pr_err(gettext("No tags matching \"%s\"\n"), tag);
	}

	if ((exp->ex_tag = strdup(tag)) == NULL) {
		error = ENOMEM;
		goto out;
	}
	if ((exp->ex_log_buffer = strdup(configp->nc_bufferpath)) == NULL) {
		error = ENOMEM;
		goto out;
	}
	exp->ex_flags |= EX_LOG;
	if (configp->nc_rpclogpath != NULL)
		exp->ex_flags |= EX_LOG_ALLOPS;
out:
	nfsl_freeconfig_list(&configlist);
	if (error != 0) {
		if (exp->ex_flags != NULL)
			free(exp->ex_tag);
		if (exp->ex_log_buffer != NULL)
			free(exp->ex_log_buffer);
		pr_err(gettext("Cannot set log configuration: %m"),
			strerror(error));
	}
}

/*VARARGS1*/
static void
pr_err(char *fmt, ...)
{
	va_list ap;

	va_start(ap, fmt);
	(void) fprintf(stderr, "share_nfs: ");
	(void) vfprintf(stderr, fmt, ap);
	va_end(ap);
	exit(RET_ERR);
}