summaryrefslogtreecommitdiff
path: root/usr/src/lib/libshare/common/libsharecore.c
blob: c15291d19dce7a70e351ab8cfb2e1540b2618829 (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
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
/*
 * 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) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
 * Copyright 2014 Nexenta Systems, Inc.  All rights reserved.
 * Copyright (c) 2016 by Delphix. All rights reserved.
 */

/*
 * core library for common functions across all config store types
 * and file systems to be exported. This includes legacy dfstab/sharetab
 * parsing. Need to eliminate XML where possible.
 */

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include <limits.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <libxml/parser.h>
#include <libxml/tree.h>
#include "libshare.h"
#include "libshare_impl.h"
#include <fcntl.h>
#include <thread.h>
#include <grp.h>
#include <limits.h>
#include <sys/param.h>
#include <signal.h>
#include <libintl.h>
#include <dirent.h>

#include <sharefs/share.h>
#include "sharetab.h"

#define	DFSTAB_NOTICE_LINES	5
static char *notice[DFSTAB_NOTICE_LINES] =	{
	"# Do not modify this file directly.\n",
	"# Use the sharemgr(8) command for all share management\n",
	"# This file is reconstructed and only maintained for backward\n",
	"# compatibility. Configuration lines could be lost.\n",
	"#\n"
};

#define	STRNCAT(x, y, z)	(xmlChar *)strncat((char *)x, (char *)y, z)

/* will be much smaller, but this handles bad syntax in the file */
#define	MAXARGSFORSHARE	256

static mutex_t sharetab_lock = DEFAULTMUTEX;
extern mutex_t sa_dfstab_lock;

/* used internally only */
typedef
struct sharelist {
    struct sharelist *next;
    int   persist;
    char *path;
    char *resource;
    char *fstype;
    char *options;
    char *description;
    char *group;
    char *origline;
    int lineno;
} xfs_sharelist_t;
static void parse_dfstab(sa_handle_t, char *, xmlNodePtr);
extern char *_sa_get_token(char *);
static void dfs_free_list(xfs_sharelist_t *);
/* prototypes */
void getlegacyconfig(sa_handle_t, char *, xmlNodePtr *);
extern sa_share_t _sa_add_share(sa_group_t, char *, int, int *, uint64_t);
extern sa_group_t _sa_create_group(sa_handle_impl_t, char *);
static void outdfstab(FILE *, xfs_sharelist_t *);
extern int _sa_remove_optionset(sa_optionset_t);
extern int set_node_share(void *, char *, char *);
extern void set_node_attr(void *, char *, char *);

/*
 * sablocksigs(*sigs)
 *
 * block important signals for a critical region. Arg is a pointer to
 * a sigset_t that is used later for the unblock.
 */
void
sablocksigs(sigset_t *sigs)
{
	sigset_t new;

	if (sigs != NULL) {
		(void) sigprocmask(SIG_BLOCK, NULL, &new);
		(void) sigaddset(&new, SIGHUP);
		(void) sigaddset(&new, SIGINT);
		(void) sigaddset(&new, SIGQUIT);
		(void) sigaddset(&new, SIGTSTP);
		(void) sigprocmask(SIG_SETMASK, &new, sigs);
	}
}

/*
 * saunblocksigs(*sigs)
 *
 * unblock previously blocked signals from the sigs arg.
 */
void
saunblocksigs(sigset_t *sigs)
{
	if (sigs != NULL)
		(void) sigprocmask(SIG_SETMASK, sigs, NULL);
}

/*
 * alloc_sharelist()
 *
 * allocator function to return an zfs_sharelist_t
 */

static xfs_sharelist_t *
alloc_sharelist()
{
	xfs_sharelist_t *item;

	item = (xfs_sharelist_t *)malloc(sizeof (xfs_sharelist_t));
	if (item != NULL)
		(void) memset(item, '\0', sizeof (xfs_sharelist_t));
	return (item);
}

/*
 * fix_notice(list)
 *
 * Look at the beginning of the current /etc/dfs/dfstab file and add
 * the do not modify notice if it doesn't exist.
 */

static xfs_sharelist_t *
fix_notice(xfs_sharelist_t *list)
{
	xfs_sharelist_t *item, *prev;
	int i;

	if (list == NULL) {
		/* zero length dfstab */
		list = alloc_sharelist();
		if (list == NULL)
			return (NULL);
		list->description = strdup("#\n");
	}
	if (list->path == NULL && list->description != NULL &&
	    strcmp(list->description, notice[0]) != 0) {
		for (prev = NULL, i = 0; i < DFSTAB_NOTICE_LINES; i++) {
			item = alloc_sharelist();
			if (item != NULL) {
				item->description = strdup(notice[i]);
				if (prev == NULL) {
					item->next = list;
					prev = item;
					list = item;
				} else {
					item->next = prev->next;
					prev->next = item;
					prev = item;
				}
			}
		}
	}
	return (list);
}

/*
 * getdfstab(dfs)
 *
 * Returns an zfs_sharelist_t list of lines from the dfstab file
 * pointed to by the FILE pointer dfs. Each entry is parsed and the
 * original line is also preserved. Used in parsing and updating the
 * dfstab file.
 */

static xfs_sharelist_t *
getdfstab(FILE *dfs)
{
	char buff[_POSIX_ARG_MAX]; /* reasonable size given syntax of share */
	char *bp;
	char *token;
	char *args[MAXARGSFORSHARE];
	int argc;
	int c;
	static int line = 0;
	xfs_sharelist_t *item = NULL, *first = NULL, *last;

	if (dfs != NULL) {
		first = NULL;
		line = 0;
		while (fgets(buff, sizeof (buff), dfs) != NULL) {
			line++;
			bp = buff;
			if (buff[0] == '#') {
				item = alloc_sharelist();
				if (item != NULL) {
					/* if no path, then comment */
					item->lineno = line;
					item->description = strdup(buff);
					if (first == NULL) {
						first = item;
						last = item;
					} else {
						last->next = item;
						last = item;
					}
				} else {
					break;
				}
				continue;
			} else if (buff[0] == '\n') {
				continue;
			}
			optind = 1;
			item = alloc_sharelist();
			if (item == NULL) {
				break;
			} else if (first == NULL) {
				first = item;
				last = item;
			} else {
				last->next = item;
				last = item;
			}
			item->lineno = line;
			item->origline = strdup(buff);
			(void) _sa_get_token(NULL); /* reset to new pointers */
			argc = 0;
			while ((token = _sa_get_token(bp)) != NULL) {
				if (argc < MAXARGSFORSHARE)
					args[argc++] = token;
			}
			while ((c = getopt(argc, args, "F:o:d:pg:")) != -1) {
				switch (c) {
				case 'p':
					item->persist = 1;
					break;
				case 'F':
					item->fstype = strdup(optarg);
					break;
				case 'o':
					item->options = strdup(optarg);
					break;
				case 'd':
					item->description = strdup(optarg);
					break;
				case 'g':
					item->group = strdup(optarg);
					break;
				default:
					break;
				}
			}
			if (optind < argc) {
				item->path = strdup(args[optind]);
				optind++;
				if (optind < argc) {
					char *resource;
					char *optgroup;
					/* resource and/or groupname */
					resource = args[optind];
					optgroup = strchr(resource, '@');
					if (optgroup != NULL)
						*optgroup++ = '\0';
					if (optgroup != NULL)
						item->group = strdup(optgroup);
					if (resource != NULL &&
					    strlen(resource) > 0)
						item->resource =
						    strdup(resource);
				}
			}
			/* NFS is the default if none defined */
			if (item != NULL && item->fstype == NULL)
				item->fstype = strdup("nfs");
		}
	}
	first = fix_notice(first);
	return (first);
}

/*
 * finddfsentry(list, path)
 *
 * Look for path in the zfs_sharelist_t list and return the entry if it
 * exists.
 */

static xfs_sharelist_t *
finddfsentry(xfs_sharelist_t *list, char *path)
{
	xfs_sharelist_t *item;

	for (item = list; item != NULL; item = item->next) {
		if (item->path != NULL && strcmp(item->path, path) == 0)
		return (item);
	}
	return (NULL);
}

/*
 * remdfsentry(list, path, proto)
 *
 * Remove the specified path (with protocol) from the list. This will
 * remove it from dfstab when the file is rewritten.
 */

static xfs_sharelist_t *
remdfsentry(xfs_sharelist_t *list, char *path, char *proto)
{
	xfs_sharelist_t *item, *prev = NULL;


	for (item = prev = list; item != NULL; item = item->next) {
	    /* skip comment entry but don't lose it */
		if (item->path == NULL) {
			prev = item;
			continue;
		}
		/* if proto is NULL, remove all protocols */
		if (proto == NULL || (strcmp(item->path, path) == 0 &&
		    (item->fstype != NULL && strcmp(item->fstype, proto) == 0)))
			break;
		if (item->fstype == NULL &&
		    (proto == NULL || strcmp(proto, "nfs") == 0))
			break;
		prev = item;
	}
	if (item != NULL) {
		if (item == prev)
			list = item->next; /* this must be the first one */
		else
			prev->next = item->next;
		item->next = NULL;
		dfs_free_list(item);
	}
	return (list);
}

/*
 * remdfsline(list, line)
 *
 * Remove the line specified from the list.
 */

static xfs_sharelist_t *
remdfsline(xfs_sharelist_t *list, char *line)
{
	xfs_sharelist_t *item, *prev = NULL;

	for (item = prev = list; item != NULL; item = item->next) {
		/* skip comment entry but don't lose it */
		if (item->path == NULL) {
		prev = item;
		continue;
		}
		if (strcmp(item->origline, line) == 0)
			break;
		prev = item;
	}
	if (item != NULL) {
		if (item == prev)
			list = item->next; /* this must be the first one */
		else
			prev->next = item->next;
		item->next = NULL;
		dfs_free_list(item);
	}
	return (list);
}

/*
 * adddfsentry(list, share, proto)
 *
 * Add an entry to the dfstab list for share (relative to proto). This
 * is used to update dfstab for legacy purposes.
 */

static xfs_sharelist_t *
adddfsentry(xfs_sharelist_t *list, sa_share_t share, char *proto)
{
	xfs_sharelist_t *item, *tmp;
	sa_group_t parent;
	char *groupname;

	item = alloc_sharelist();
	if (item != NULL) {
		parent = sa_get_parent_group(share);
		groupname = sa_get_group_attr(parent, "name");
		if (groupname != NULL && strcmp(groupname, "default") == 0) {
			sa_free_attr_string(groupname);
			groupname = NULL;
		}
		item->path = sa_get_share_attr(share, "path");
		item->resource = sa_get_share_attr(share, "resource");
		item->group = groupname;
		item->fstype = strdup(proto);
		item->options = sa_proto_legacy_format(proto, share, 1);
		if (item->options != NULL && strlen(item->options) == 0) {
			free(item->options);
			item->options = NULL;
		}
		item->description = sa_get_share_description(share);
		if (item->description != NULL &&
		    strlen(item->description) == 0) {
			sa_free_share_description(item->description);
			item->description = NULL;
		}
		if (list == NULL) {
			list = item;
		} else {
			for (tmp = list; tmp->next != NULL; tmp = tmp->next)
				/* do nothing */;
			tmp->next = item;
		}
	}
	return (list);
}

/*
 * outdfstab(dfstab, list)
 *
 * Output the list to dfstab making sure the file is truncated.
 * Comments and errors are preserved.
 */

static void
outdfstab(FILE *dfstab, xfs_sharelist_t *list)
{
	xfs_sharelist_t *item;

	(void) ftruncate(fileno(dfstab), 0);

	for (item = list; item != NULL; item = item->next) {
		if (item->path != NULL) {
			if (*item->path == '/') {
				(void) fprintf(dfstab,
				    "share %s%s%s%s%s%s%s %s%s%s%s%s\n",
				    (item->fstype != NULL) ? "-F " : "",
				    (item->fstype != NULL) ? item->fstype : "",
				    (item->options != NULL) ? " -o " : "",
				    (item->options != NULL) ?
				    item->options : "",
				    (item->description != NULL) ?
				    " -d \"" : "",
				    (item->description != NULL) ?
				    item->description : "",
				    (item->description != NULL) ? "\"" : "",
				    item->path,
				    ((item->resource != NULL) ||
				    (item->group != NULL)) ? " " : "",
				    (item->resource != NULL) ?
				    item->resource : "",
				    item->group != NULL ? "@" : "",
				    item->group != NULL ? item->group : "");
			} else {
				(void) fprintf(dfstab, "%s", item->origline);
			}
		} else {
			if (item->description != NULL)
				(void) fprintf(dfstab, "%s", item->description);
			else
				(void) fprintf(dfstab, "%s", item->origline);
		}
	}
}

/*
 * open_dfstab(file)
 *
 * Open the specified dfstab file. If the owner/group/perms are wrong,
 * fix them.
 */

static FILE *
open_dfstab(char *file)
{
	struct group *grp;
	struct group group;
	char *buff;
	int grsize;
	FILE *dfstab;

	dfstab = fopen(file, "r+");
	if (dfstab == NULL) {
		dfstab = fopen(file, "w+");
	}
	if (dfstab != NULL) {
		grsize = sysconf(_SC_GETGR_R_SIZE_MAX);
		buff = malloc(grsize);
		if (buff != NULL)
			grp = getgrnam_r(SA_DEFAULT_FILE_GRP, &group, buff,
			    grsize);
		else
			grp = getgrnam(SA_DEFAULT_FILE_GRP);
		(void) fchmod(fileno(dfstab), 0644);
		(void) fchown(fileno(dfstab), 0,
		    grp != NULL ? grp->gr_gid : 3);
		if (buff != NULL)
			free(buff);
		rewind(dfstab);
	}
	return (dfstab);
}

/*
 * sa_comment_line(line, err)
 *
 * Add a comment to the dfstab file with err as a prefix to the
 * original line.
 */

static void
sa_comment_line(char *line, char *err)
{
	FILE *dfstab;
	xfs_sharelist_t *list;
	sigset_t old;

	dfstab = open_dfstab(SA_LEGACY_DFSTAB);
	if (dfstab != NULL) {
		(void) setvbuf(dfstab, NULL, _IOLBF, BUFSIZ * 8);
		sablocksigs(&old);
		(void) lockf(fileno(dfstab), F_LOCK, 0);
		(void) mutex_lock(&sa_dfstab_lock);
		list = getdfstab(dfstab);
		rewind(dfstab);
		/*
		 * don't ignore the return since the list could have
		 * gone to NULL if the file only had one line in it.
		 */
		list = remdfsline(list, line);
		outdfstab(dfstab, list);
		(void) fprintf(dfstab, "# Error: %s: %s", err, line);
		(void) fsync(fileno(dfstab));
		(void) mutex_unlock(&sa_dfstab_lock);
		(void) lockf(fileno(dfstab), F_ULOCK, 0);
		(void) fclose(dfstab);
		saunblocksigs(&old);
		if (list != NULL)
			dfs_free_list(list);
	}
}

/*
 * sa_delete_legacy(share, protocol)
 *
 * Delete the specified share from the legacy config file.
 */

int
sa_delete_legacy(sa_share_t share, char *protocol)
{
	FILE *dfstab;
	int err;
	int ret = SA_OK;
	xfs_sharelist_t *list;
	char *path;
	sa_optionset_t optionset;
	sa_group_t parent;
	sigset_t old;

	/*
	 * Protect against shares that don't have paths. This is not
	 * really an error at this point.
	 */
	path = sa_get_share_attr(share, "path");
	if (path == NULL)
		return (ret);

	dfstab = open_dfstab(SA_LEGACY_DFSTAB);
	if (dfstab != NULL) {
		(void) setvbuf(dfstab, NULL, _IOLBF, BUFSIZ * 8);
		sablocksigs(&old);
		parent = sa_get_parent_group(share);
		if (parent != NULL) {
			(void) lockf(fileno(dfstab), F_LOCK, 0);
			(void) mutex_lock(&sa_dfstab_lock);
			list = getdfstab(dfstab);
			rewind(dfstab);
			if (protocol != NULL) {
				if (list != NULL)
					list = remdfsentry(list, path,
					    protocol);
			} else {
				for (optionset = sa_get_optionset(parent, NULL);
				    optionset != NULL;
				    optionset =
				    sa_get_next_optionset(optionset)) {
					char *proto = sa_get_optionset_attr(
					    optionset, "type");

					if (list != NULL && proto != NULL)
						list = remdfsentry(list, path,
						    proto);
					if (proto == NULL)
						ret = SA_NO_MEMORY;
					/*
					 * may want to only do the dfstab if
					 * this call returns NOT IMPLEMENTED
					 * but it shouldn't hurt.
					 */
					if (ret == SA_OK) {
						err = sa_proto_delete_legacy(
						    proto, share);
						if (err != SA_NOT_IMPLEMENTED)
							ret = err;
					}
					if (proto != NULL)
						sa_free_attr_string(proto);
				}
			}
			outdfstab(dfstab, list);
			if (list != NULL)
				dfs_free_list(list);
			(void) fflush(dfstab);
			(void) mutex_unlock(&sa_dfstab_lock);
			(void) lockf(fileno(dfstab), F_ULOCK, 0);
		}
		(void) fsync(fileno(dfstab));
		saunblocksigs(&old);
		(void) fclose(dfstab);
	} else {
		if (errno == EACCES || errno == EPERM)
			ret = SA_NO_PERMISSION;
		else
			ret = SA_CONFIG_ERR;
	}

	if (path != NULL)
		sa_free_attr_string(path);

	return (ret);
}

/*
 * sa_update_legacy(share, proto)
 *
 * There is an assumption that dfstab will be the most common form of
 * legacy configuration file for shares, but not the only one. Because
 * of that, dfstab handling is done in the main code with calls to
 * this function and protocol specific calls to deal with formatting
 * options into dfstab/share compatible syntax. Since not everything
 * will be dfstab, there is a provision for calling a protocol
 * specific plugin interface that allows the protocol plugin to do its
 * own legacy files and skip the dfstab update.
 */

int
sa_update_legacy(sa_share_t share, char *proto)
{
	FILE *dfstab;
	int ret = SA_OK;
	xfs_sharelist_t *list;
	char *path;
	sigset_t old;
	char *persist;
	uint64_t features;

	ret = sa_proto_update_legacy(proto, share);
	if (ret != SA_NOT_IMPLEMENTED)
		return (ret);

	features = sa_proto_get_featureset(proto);
	if (!(features & SA_FEATURE_DFSTAB))
		return (ret);

	/* do the dfstab format */
	persist = sa_get_share_attr(share, "type");
	/*
	 * only update if the share is not transient -- no share type
	 * set or the type is not "transient".
	 */
	if (persist == NULL || strcmp(persist, "transient") != 0) {
		path = sa_get_share_attr(share, "path");
		if (path == NULL) {
			ret = SA_NO_MEMORY;
			goto out;
		}
		dfstab = open_dfstab(SA_LEGACY_DFSTAB);
		if (dfstab != NULL) {
			(void) setvbuf(dfstab, NULL, _IOLBF, BUFSIZ * 8);
			sablocksigs(&old);
			(void) lockf(fileno(dfstab), F_LOCK, 0);
			(void) mutex_lock(&sa_dfstab_lock);
			list = getdfstab(dfstab);
			rewind(dfstab);
			if (list != NULL)
				list = remdfsentry(list, path, proto);
			list = adddfsentry(list, share, proto);
			outdfstab(dfstab, list);
			(void) fflush(dfstab);
			(void) mutex_unlock(&sa_dfstab_lock);
			(void) lockf(fileno(dfstab), F_ULOCK, 0);
			(void) fsync(fileno(dfstab));
			saunblocksigs(&old);
			(void) fclose(dfstab);
			if (list != NULL)
				dfs_free_list(list);
		} else {
			if (errno == EACCES || errno == EPERM)
				ret = SA_NO_PERMISSION;
			else
				ret = SA_CONFIG_ERR;
		}
		sa_free_attr_string(path);
	}
out:
	if (persist != NULL)
		sa_free_attr_string(persist);
	return (ret);
}

/*
 * sa_is_security(optname, proto)
 *
 * Check to see if optname is a security (named optionset) specific
 * property for the specified protocol.
 */

int
sa_is_security(char *optname, char *proto)
{
	int ret = 0;
	if (proto != NULL)
		ret = sa_proto_security_prop(proto, optname);
	return (ret);
}

/*
 * add_syntax_comment(root, line, err, todfstab)
 *
 * Add a comment to the document indicating a syntax error. If
 * todfstab is set, write it back to the dfstab file as well.
 */

static void
add_syntax_comment(xmlNodePtr root, char *line, char *err, int todfstab)
{
	xmlNodePtr node;

	node = xmlNewChild(root, NULL, (xmlChar *)"error", (xmlChar *)line);
	if (node != NULL)
		(void) xmlSetProp(node, (xmlChar *)"type", (xmlChar *)err);
	if (todfstab)
		sa_comment_line(line, err);
}

/*
 * sa_is_share(object)
 *
 * returns true if the object is of type "share".
 */

int
sa_is_share(void *object)
{
	if (object != NULL) {
		if (strcmp((char *)((xmlNodePtr)object)->name, "share") == 0)
		return (1);
	}
	return (0);
}
/*
 * sa_is_resource(object)
 *
 * returns true if the object is of type "resource".
 */

int
sa_is_resource(void *object)
{
	if (object != NULL) {
		if (strcmp((char *)((xmlNodePtr)object)->name, "resource") == 0)
			return (1);
	}
	return (0);
}

/*
 * _sa_remove_property(property)
 *
 * remove a property only from the document.
 */

static void
_sa_remove_property(sa_property_t property)
{
	xmlUnlinkNode((xmlNodePtr)property);
	xmlFreeNode((xmlNodePtr)property);
}

/*
 * _sa_create_dummy_share()
 *
 * Create a share entry suitable for parsing but not tied to any real
 * config tree.  Need to have a parent as well as the node to parse
 * on.  Free using _sa_free_dummy_share(share);
 */

static sa_group_t
_sa_create_dummy_share()
{
	xmlNodePtr parent_node = NULL;
	xmlNodePtr child_node = NULL;

	parent_node = xmlNewNode(NULL, (xmlChar *)"group");
	if (parent_node != NULL) {
		child_node = xmlNewChild(parent_node, NULL, (xmlChar *)"share",
		    NULL);
		if (child_node != NULL) {
			/*
			 * Use a "zfs" tag since that will make sure nothing
			 * really attempts to put values into the
			 * repository. Also ZFS is currently the only user of
			 * this interface.
			 */
			set_node_attr(parent_node, "type", "transient");
			set_node_attr(parent_node, "zfs", "true");
			set_node_attr(child_node, "type", "transient");
			set_node_attr(child_node, "zfs", "true");
		} else {
			xmlFreeNode(parent_node);
		}
	}
	return (child_node);
}

/*
 * _sa_free_dummy_share(share)
 *
 * Free the dummy share and its parent.  It is an error to try and
 * free something that isn't a dummy.
 */

static int
_sa_free_dummy_share(sa_share_t share)
{
	xmlNodePtr node = (xmlNodePtr)share;
	xmlNodePtr parent;
	int ret = SA_OK;
	char *name;

	if (node != NULL) {
		parent = node->parent;
		name = (char *)xmlGetProp(node, (xmlChar *)"path");
		if (name != NULL) {
			/* Real shares always have a path but a dummy doesn't */
			ret = SA_NOT_ALLOWED;
			sa_free_attr_string(name);
		} else {
			/*
			 * If there is a parent, do the free on that since
			 * xmlFreeNode is a recursive function and free's an
			 * child nodes.
			 */
			if (parent != NULL) {
				node = parent;
			}
			xmlUnlinkNode(node);
			xmlFreeNode(node);
		}
	}
	return (ret);
}


/*
 * sa_parse_legacy_options(group, options, proto)
 *
 * In order to support legacy configurations, we allow the protocol
 * specific plugin to parse legacy syntax options (like those in
 * /etc/dfs/dfstab). This adds a new optionset to the group (or
 * share).
 *
 * Once the optionset has been created, we then get the derived
 * optionset of the parent (options from the optionset of the parent
 * and any parent it might have) and remove those from the created
 * optionset. This avoids duplication of options.
 */

int
sa_parse_legacy_options(sa_group_t group, char *options, char *proto)
{
	int ret = SA_INVALID_PROTOCOL;
	sa_group_t parent;
	int using_dummy = B_FALSE;
	char *pvalue;
	sa_optionset_t optionset;
	sa_property_t popt, prop;
	sa_optionset_t localoptions;

	/*
	 * If "group" is NULL, this is just a parse without saving
	 * anything in either SMF or ZFS.  Create a dummy group to
	 * handle this case.
	 */
	if (group == NULL) {
		group = (sa_group_t)_sa_create_dummy_share();
		using_dummy = B_TRUE;
	}

	parent = sa_get_parent_group(group);

	if (proto != NULL)
		ret = sa_proto_legacy_opts(proto, group, options);

	if (using_dummy) {
		/* Since this is a dummy parse, cleanup and quit here */
		(void) _sa_free_dummy_share(parent);
		return (ret);
	}

	if (ret != SA_OK)
		return (ret);

	/*
	 * If in a group, remove the inherited options and security
	 */

	if (parent == NULL)
		return (ret);

	/* Find parent options to remove from child */
	optionset = sa_get_derived_optionset(parent, proto, 1);
	localoptions = sa_get_optionset(group, proto);
	if (optionset != NULL) {
		for (popt = sa_get_property(optionset, NULL);
		    popt != NULL;
		    popt = sa_get_next_property(popt)) {
			char *tag;
			char *value;
			tag = sa_get_property_attr(popt, "type");
			if (tag == NULL)
				continue;
			prop = sa_get_property(localoptions, tag);
			if (prop != NULL) {
				value = sa_get_property_attr(popt,
				    "value");
				pvalue = sa_get_property_attr(prop,
				    "value");
				if (value != NULL && pvalue != NULL &&
				    strcmp(value, pvalue) == 0) {
					/*
					 * Remove the property
					 * from the
					 * child. While we
					 * removed it, we
					 * don't need to reset
					 * as we do below
					 * since we always
					 * search from the
					 * beginning.
					 */
					(void) _sa_remove_property(
					    prop);
				}
				if (value != NULL)
					sa_free_attr_string(value);
				if (pvalue != NULL)
					sa_free_attr_string(pvalue);
			}
			sa_free_attr_string(tag);
		}
		prop = sa_get_property(localoptions, NULL);
		if (prop == NULL && sa_is_share(group)) {
			/*
			 * All properties removed so remove the
			 * optionset if it is on a share
			 */
			(void) _sa_remove_optionset(localoptions);
		}
		sa_free_derived_optionset(optionset);
	}
	/*
	 * Need to remove security here. If there are no
	 * security options on the local group/share, don't
	 * bother since those are the only ones that would be
	 * affected.
	 */
	localoptions = sa_get_all_security_types(group, proto, 0);
	if (localoptions != NULL) {
		for (prop = sa_get_property(localoptions, NULL);
		    prop != NULL;
		    prop = sa_get_next_property(prop)) {
			char *tag;
			sa_security_t security;
			tag = sa_get_property_attr(prop, "type");
			if (tag != NULL) {
				sa_property_t nextpopt = NULL;
				security = sa_get_security(group, tag, proto);
				sa_free_attr_string(tag);
				/*
				 * prop's value only changes outside this loop
				 */
				pvalue = sa_get_property_attr(prop, "value");
				for (popt = sa_get_property(security, NULL);
				    popt != NULL;
				    popt = nextpopt) {
					char *value;
					/*
					 * Need to get the next prop
					 * now since we could break
					 * the list during removal.
					 */
					nextpopt = sa_get_next_property(popt);
					/* remove Duplicates from this level */
					value = sa_get_property_attr(popt,
					    "value");
					if (value != NULL && pvalue != NULL &&
					    strcmp(value, pvalue) == 0) {
						/*
						 * remove the property
						 * from the child
						 */
						(void) _sa_remove_property
						    (popt);
					}
					if (value != NULL)
						sa_free_attr_string(value);
				}
				if (pvalue != NULL)
					sa_free_attr_string(pvalue);
			}
		}
		(void) sa_destroy_optionset(localoptions);
	}
	return (ret);
}

/*
 * dfs_free_list(list)
 *
 * Free the data in each list entry of the list as well as freeing the
 * entries themselves. We need to avoid memory leaks and don't want to
 * dereference any NULL members.
 */

static void
dfs_free_list(xfs_sharelist_t *list)
{
	xfs_sharelist_t *entry;
	for (entry = list; entry != NULL; entry = list) {
		if (entry->path != NULL)
			free(entry->path);
		if (entry->resource != NULL)
			free(entry->resource);
		if (entry->fstype != NULL)
			free(entry->fstype);
		if (entry->options != NULL)
			free(entry->options);
		if (entry->description != NULL)
			free(entry->description);
		if (entry->origline != NULL)
			free(entry->origline);
		if (entry->group != NULL)
			free(entry->group);
		list = list->next;
			free(entry);
	}
}

/*
 * parse_dfstab(dfstab, root)
 *
 * Open and read the existing dfstab, parsing each line and adding it
 * to the internal configuration. Make sure syntax errors, etc are
 * preserved as comments.
 */

static void
parse_dfstab(sa_handle_t handle, char *dfstab, xmlNodePtr root)
{
	sa_share_t share;
	sa_group_t group;
	sa_group_t sgroup = NULL;
	sa_group_t defgroup;
	xfs_sharelist_t *head, *list;
	int err;
	int defined_group;
	FILE *dfs;
	char *oldprops;

	/* read the dfstab format file and fill in the doc tree */

	dfs = fopen(dfstab, "r");
	if (dfs == NULL)
		return;

	defgroup = sa_get_group(handle, "default");

	for (head = list = getdfstab(dfs);
	    list != NULL;
	    list = list->next) {
		share = NULL;
		group = NULL;
		defined_group = 0;
		err = 0;

		if (list->origline == NULL) {
			/*
			 * Comment line that we will likely skip.
			 * If the line has the syntax:
			 *	# error: string: string
			 * It should be preserved until manually deleted.
			 */
			if (list->description != NULL &&
			    strncmp(list->description, "# Error: ", 9) == 0) {
				char *line;
				char *error;
				char *cmd;
				line = strdup(list->description);
				if (line != NULL) {
					error = line + 9;
					cmd = strchr(error, ':');
					if (cmd != NULL) {
						int len;
						*cmd = '\0';
						cmd += 2;
						len = strlen(cmd);
						cmd[len - 1] = '\0';
						add_syntax_comment(root, cmd,
						    error, 0);
					}
					free(line);
				}
			}
			continue;
		}
		if (list->path != NULL && strlen(list->path) > 0 &&
		    *list->path == '/') {
			share = sa_find_share(handle, list->path);
			if (share != NULL)
				sgroup = sa_get_parent_group(share);
			else
				sgroup = NULL;
		} else {
			(void) printf(dgettext(TEXT_DOMAIN,
			    "No share specified in dfstab: "
			    "line %d: %s\n"),
			    list->lineno, list->origline);
			add_syntax_comment(root, list->origline,
			    dgettext(TEXT_DOMAIN, "No share specified"), 1);
			continue;
		}
		if (list->group != NULL && strlen(list->group) > 0) {
			group = sa_get_group(handle, list->group);
			defined_group = 1;
		} else {
			group = defgroup;
		}
		if (defined_group && group == NULL) {
			(void) printf(dgettext(TEXT_DOMAIN,
			    "Unknown group used in dfstab: line %d: %s\n"),
			    list->lineno, list->origline);
			add_syntax_comment(root, list->origline,
			    dgettext(TEXT_DOMAIN, "Unknown group specified"),
			    1);
			continue;
		}
		if (group == NULL) {
			/* Shouldn't happen unless an SMF error */
			err = SA_CONFIG_ERR;
			continue;
		}
		if (share == NULL) {
			if (defined_group || group != defgroup)
				continue;
			/* This is an OK add for legacy */
			share = sa_add_share(defgroup, list->path,
			    SA_SHARE_PERMANENT | SA_SHARE_PARSER, &err);
			if (share != NULL) {
				if (list->description != NULL &&
				    strlen(list->description) > 0)
					(void) sa_set_share_description(share,
					    list->description);
				if (list->options != NULL &&
				    strlen(list->options) > 0) {
					(void) sa_parse_legacy_options(share,
					    list->options, list->fstype);
				}
				if (list->resource != NULL)
					(void) sa_set_share_attr(share,
					    "resource", list->resource);
			} else {
				(void) printf(dgettext(TEXT_DOMAIN,
				    "Error in dfstab: line %d: %s\n"),
				    list->lineno, list->origline);
				if (err != SA_BAD_PATH)
					add_syntax_comment(root, list->origline,
					    dgettext(TEXT_DOMAIN, "Syntax"), 1);
				else
					add_syntax_comment(root, list->origline,
					    dgettext(TEXT_DOMAIN,
					    "Path"), 1);
				continue;
			}
		} else {
			if (group != sgroup) {
				(void) printf(dgettext(TEXT_DOMAIN,
				    "Attempt to change configuration in "
				    "dfstab: line %d: %s\n"),
				    list->lineno, list->origline);
				add_syntax_comment(root, list->origline,
				    dgettext(TEXT_DOMAIN,
				    "Attempt to change configuration"), 1);
				continue;
			}
			/*
			 * It is the same group but could have changed
			 * options. Make sure we include the group's
			 * properties so we don't end up moving them to
			 * the share inadvertantly. The last arg being
			 * true says to get the inherited properties as well
			 * as the local properties.
			 */
			oldprops = sa_proto_legacy_format(list->fstype, share,
			    B_TRUE);

			if (oldprops == NULL)
				continue;

			if (list->options != NULL &&
			    strcmp(oldprops, list->options) != 0) {
				sa_optionset_t opts;
				sa_security_t secs;

				/* possibly different values */
				opts = sa_get_optionset((sa_group_t)
				    share, list->fstype);
				(void) sa_destroy_optionset(opts);

				for (secs = sa_get_security(
				    (sa_group_t)share, NULL, list->fstype);
				    secs != NULL;
				    secs = sa_get_security((sa_group_t)share,
				    NULL, list->fstype)) {
					(void) sa_destroy_security(
					    secs);
				}
				(void) sa_parse_legacy_options(share,
				    list->options, list->fstype);
			}
			sa_format_free(oldprops);
		}
	}
	dfs_free_list(head);
}

/*
 * legacy_removes(group, file)
 *
 * Find any shares that are "missing" from the legacy file. These
 * should be removed from the configuration since they are likely from
 * a legacy app or the admin modified the dfstab file directly. We
 * have to support this even if it is not the recommended way to do
 * things.
 */

static void
legacy_removes(sa_group_t group, char *file)
{
	sa_share_t share;
	char *path;
	xfs_sharelist_t *list, *item;
	FILE *dfstab;

	dfstab = fopen(file, "r");
	if (dfstab != NULL) {
		list = getdfstab(dfstab);
		(void) fclose(dfstab);
retry:
		for (share = sa_get_share(group, NULL);
		    share != NULL;
		    share = sa_get_next_share(share)) {
			/* now see if the share is in the dfstab file */
			path = sa_get_share_attr(share, "path");
			if (path != NULL) {
				item = finddfsentry(list, path);
				sa_free_attr_string(path);
				if (item == NULL) {
					/* The share was removed this way */
					(void) sa_remove_share(share);

					/*
					 * Start over since the list was broken
					 */
					goto retry;
				}
			}
		}
		if (list != NULL)
			dfs_free_list(list);
	}
}

/*
 * getlegacyconfig(path, root)
 *
 * Parse dfstab and build the legacy configuration. This only gets
 * called when a change was detected.
 */

void
getlegacyconfig(sa_handle_t handle, char *path, xmlNodePtr *root)
{
	sa_group_t defgroup;

	if (root != NULL) {
		if (*root == NULL)
			*root = xmlNewNode(NULL, (xmlChar *)"sharecfg");
		if (*root != NULL) {
			if (strcmp(path, SA_LEGACY_DFSTAB) == 0) {
				/*
				 * Walk the default shares and find anything
				 * missing.  we do this first to make sure it
				 * is cleaned up since there may be legacy
				 * code add/del via dfstab and we need to
				 * cleanup SMF.
				 */
				defgroup = sa_get_group(handle, "default");
				if (defgroup != NULL)
					legacy_removes(defgroup, path);
				/* Parse the dfstab and add anything new */
				parse_dfstab(handle, path, *root);
			}
		}
	}
}

/*
 * get_share_list(&err)
 *
 * Get a linked list of all the shares on the system from
 * /etc/dfs/sharetab. This is partially copied from libfsmgt which we
 * can't use due to package dependencies.
 */
static xfs_sharelist_t *
get_share_list(int *errp)
{
	xfs_sharelist_t	*newp;
	xfs_sharelist_t	*headp;
	xfs_sharelist_t	*tailp;
	FILE		*fp;

	headp = NULL;
	tailp = NULL;

	if ((fp = fopen(SHARETAB, "r")) != NULL) {
		struct share	*sharetab_entry;

		(void) lockf(fileno(fp), F_LOCK, 0);
		(void) mutex_lock(&sharetab_lock);

		while (getshare(fp, &sharetab_entry) > 0) {
			newp = alloc_sharelist();
			if (newp == NULL) {
				(void) mutex_unlock(&sharetab_lock);
				(void) lockf(fileno(fp), F_ULOCK, 0);
				goto err;
			}

			/*
			 * Link into the list here so we don't leak
			 * memory on a failure from strdup().
			 */
			if (headp == NULL) {
				headp = newp;
				tailp = newp;
			} else {
				tailp->next = newp;
				tailp = newp;
			}

			newp->path = strdup(sharetab_entry->sh_path);
			newp->resource = strdup(sharetab_entry->sh_res);
			newp->fstype = strdup(sharetab_entry->sh_fstype);
			newp->options = strdup(sharetab_entry->sh_opts);
			newp->description = strdup(sharetab_entry->sh_descr);

			if (newp->path == NULL || newp->resource == NULL ||
			    newp->fstype == NULL || newp->options == NULL ||
			    newp->description == NULL) {
				(void) mutex_unlock(&sharetab_lock);
				(void) lockf(fileno(fp), F_ULOCK, 0);
				goto err;
			}
		}

		(void) mutex_unlock(&sharetab_lock);
		(void) lockf(fileno(fp), F_ULOCK, 0);
		(void) fclose(fp);
	} else {
		*errp = errno;
	}

	/*
	 * Caller must free the mount list
	 */
	return (headp);
err:
	/*
	 * Out of memory so cleanup and leave.
	 */
	dfs_free_list(headp);
	(void) fclose(fp);
	return (NULL);
}

/*
 * parse_sharetab_impl(handle, tmplist, lgroup)
 *
 * Read the /etc/dfs/sharetab file and see which entries don't exist
 * in the repository. These shares are marked transient.  We also need
 * to see if they are ZFS shares since ZFS bypasses the SMF
 * repository.
 *
 * The return value is used to contribute to values indicating if it is a legacy
 * share. Right now this is 0 or 1, but as multiple systems become legacy that
 * could change.
 */

static int
parse_sharetab_impl(sa_handle_t handle, xfs_sharelist_t *tmplist,
    sa_group_t lgroup)
{
	int err = 0;
	sa_share_t share;
	sa_group_t group;
	char *groupname;
	int legacy = 0;
	char shareopts[MAXNAMLEN];

	group = NULL;
	share = sa_find_share(handle, tmplist->path);
	if (share != NULL) {
		/*
		 * If this is a legacy share, mark as shared so we only update
		 * sharetab appropriately. We also keep the sharetab options in
		 * order to display for legacy share with no arguments.
		 */
		set_node_attr(share, "shared", "true");
		(void) snprintf(shareopts, MAXNAMLEN, "shareopts-%s",
		    tmplist->fstype);
		set_node_attr(share, shareopts, tmplist->options);
		return (1);
	}

	/*
	 * This share is transient so needs to be added. Initially, this will be
	 * under default(legacy) unless it is a ZFS share. If zfs, we need a zfs
	 * group.
	 */
	if (tmplist->resource != NULL &&
	    (groupname = strchr(tmplist->resource, '@')) != NULL) {
		/* There is a defined group */
		*groupname++ = '\0';
		group = sa_get_group(handle, groupname);
		if (group != NULL) {
			share = _sa_add_share(group, tmplist->path,
			    SA_SHARE_TRANSIENT, &err,
			    (uint64_t)SA_FEATURE_NONE);
		} else {
			/*
			 * While this case shouldn't occur very often, it does
			 * occur out of a "zfs set sharenfs=off" when the
			 * dataset is also set to canmount=off. A warning will
			 * then cause the zfs command to abort. Since we add it
			 * to the default list, everything works properly anyway
			 * and the library doesn't need to give a warning.
			 */
			share = _sa_add_share(lgroup,
			    tmplist->path, SA_SHARE_TRANSIENT,
			    &err, (uint64_t)SA_FEATURE_NONE);
		}
	} else {
		if (sa_zfs_is_shared(handle, tmplist->path)) {
			group = sa_get_group(handle, "zfs");
			if (group == NULL) {
				group = sa_create_group(handle,
				    "zfs", &err);
				if (group == NULL &&
				    err == SA_NO_PERMISSION) {
					group = _sa_create_group(
					    (sa_handle_impl_t)
					    handle,
					    "zfs");
				}
				if (group != NULL) {
					(void) sa_create_optionset(
					    group, tmplist->fstype);
					(void) sa_set_group_attr(group,
					    "zfs", "true");
				}
			}
			if (group != NULL) {
				share = _sa_add_share(group,
				    tmplist->path, SA_SHARE_TRANSIENT,
				    &err, (uint64_t)SA_FEATURE_NONE);
			}
		} else {
			share = _sa_add_share(lgroup, tmplist->path,
			    SA_SHARE_TRANSIENT, &err,
			    (uint64_t)SA_FEATURE_NONE);
		}
	}
	if (share == NULL)
		(void) printf(dgettext(TEXT_DOMAIN,
		    "Problem with transient: %s\n"), sa_errorstr(err));
	if (share != NULL)
		set_node_attr(share, "shared", "true");
	if (err == SA_OK) {
		if (tmplist->options != NULL &&
		    strlen(tmplist->options) > 0) {
			(void) sa_parse_legacy_options(share,
			    tmplist->options, tmplist->fstype);
		}
		if (tmplist->resource != NULL &&
		    strcmp(tmplist->resource, "-") != 0)
			set_node_attr(share, "resource",
			    tmplist->resource);
		if (tmplist->description != NULL) {
			xmlNodePtr node;
			node = xmlNewChild((xmlNodePtr)share, NULL,
			    (xmlChar *)"description", NULL);
			xmlNodeSetContent(node,
			    (xmlChar *)tmplist->description);
		}
		legacy = 1;
	}
	return (legacy);
}

/*
 * Given an array of paths, parses the sharetab in /etc/dfs/sharetab looking for
 * matches to each path that could be transients from that file.
 *
 * parse_sharetab_impl has more information on what exactly it is looking for.
 */
int
parse_sharetab_for_paths(sa_handle_t handle, char **paths, size_t paths_len)
{
	int err = 0;
	int legacy = 0;
	sa_group_t lgroup;
	xfs_sharelist_t *list = get_share_list(&err);

	if (list == NULL)
		return (legacy);

	lgroup = sa_get_group(handle, "default");
	for (int i = 0; i < paths_len; ++i) {
		xfs_sharelist_t *tmplist;

		for (tmplist = list; tmplist != NULL; tmplist = tmplist->next) {
			if (strcmp(tmplist->path, paths[i]) == 0) {
				break;
			}
		}
		if (tmplist == NULL) {
			continue;
		}

		legacy = parse_sharetab_impl(handle, tmplist, lgroup);

	}
	dfs_free_list(list);
	return (legacy);
}

/*
 * Runs parse_sharetab_impl on all the different share lists on the system.
 */
int
parse_sharetab(sa_handle_t handle)
{
	int err = 0;
	int legacy = 0;
	sa_group_t lgroup;
	xfs_sharelist_t *list = get_share_list(&err);

	list = get_share_list(&err);
	if (list == NULL)
		return (legacy);

	lgroup = sa_get_group(handle, "default");

	for (xfs_sharelist_t *tmplist = list; tmplist != NULL;
	    tmplist = tmplist->next) {
		legacy |= parse_sharetab_impl(handle, tmplist, lgroup);
	}
	dfs_free_list(list);
	return (legacy);
}

/*
 * This is the same as gettransients except when parsing sharetab the entries
 * are only processed if the path matches an element of paths.
 */
int
get_one_transient(sa_handle_impl_t ihandle, xmlNodePtr *root, char **paths,
    size_t paths_len)
{
	int legacy = 0;
	char **protocols = NULL;

	if (root != NULL) {
		if (*root == NULL)
			*root = xmlNewNode(NULL, (xmlChar *)"sharecfg");
		if (*root != NULL) {
			legacy = parse_sharetab_for_paths(ihandle, paths,
			    paths_len);
			int numproto = sa_get_protocols(&protocols);
			for (int i = 0; i < numproto; i++)
				legacy |= sa_proto_get_transients(
				    (sa_handle_t)ihandle, protocols[i]);
			if (protocols != NULL)
				free(protocols);
		}
	}
	return (legacy);
}

/*
 * Get the transient shares from the sharetab (or other) file. Since
 * these are transient, they only appear in the working file and not
 * in a repository.
 */
int
gettransients(sa_handle_impl_t ihandle, xmlNodePtr *root)
{
	int legacy = 0;
	int numproto;
	char **protocols = NULL;
	int i;

	if (root != NULL) {
		if (*root == NULL)
			*root = xmlNewNode(NULL, (xmlChar *)"sharecfg");
		if (*root != NULL) {
			legacy = parse_sharetab(ihandle);
			numproto = sa_get_protocols(&protocols);
			for (i = 0; i < numproto; i++)
				legacy |= sa_proto_get_transients(
				    (sa_handle_t)ihandle, protocols[i]);
			if (protocols != NULL)
				free(protocols);
		}
	}
	return (legacy);
}

/*
 * sa_has_prop(optionset, prop)
 *
 * Is the specified property a member of the optionset?
 */

int
sa_has_prop(sa_optionset_t optionset, sa_property_t prop)
{
	char *name;
	sa_property_t otherprop;
	int result = 0;

	if (optionset != NULL) {
		name = sa_get_property_attr(prop, "type");
		if (name != NULL) {
			otherprop = sa_get_property(optionset, name);
			if (otherprop != NULL)
				result = 1;
			sa_free_attr_string(name);
		}
	}
	return (result);
}

/*
 * Update legacy files
 *
 * Provides functions to add/remove/modify individual entries
 * in dfstab and sharetab
 */

void
update_legacy_config(sa_handle_t handle)
{
	/*
	 * no longer used -- this is a placeholder in case we need to
	 * add it back later.
	 */
#ifdef lint
	handle = handle;
#endif
}

/*
 * sa_valid_property(handle, object, proto, property)
 *
 * check to see if the specified property is valid relative to the
 * specified protocol. The protocol plugin is called to do the work.
 */

int
sa_valid_property(sa_handle_t handle, void *object, char *proto,
    sa_property_t property)
{
	int ret = SA_OK;

	if (proto != NULL && property != NULL) {
		ret = sa_proto_valid_prop(handle, proto, property, object);
	}

	return (ret);
}

/*
 * sa_fstype(path)
 *
 * Given path, return the string representing the path's file system
 * type. This is used to discover ZFS shares.
 */

char *
sa_fstype(char *path)
{
	int err;
	struct stat st;

	err = stat(path, &st);
	if (err < 0)
		err = SA_NO_SUCH_PATH;
	else
		err = SA_OK;

	/*
	 * If we have a valid path at this point ret, return the fstype.
	 */
	if (err == SA_OK)
		return (strdup(st.st_fstype));

	return (NULL);
}

void
sa_free_fstype(char *type)
{
	free(type);
}

/*
 * sa_get_derived_optionset(object, proto, hier)
 *
 *	Work backward to the top of the share object tree and start
 *	copying protocol specific optionsets into a newly created
 *	optionset that doesn't have a parent (it will be freed
 *	later). This provides for the property inheritance model. That
 *	is, properties closer to the share take precedence over group
 *	level. This also provides for groups of groups in the future.
 */

sa_optionset_t
sa_get_derived_optionset(void *object, char *proto, int hier)
{
	sa_optionset_t newoptionset;
	sa_optionset_t optionset;
	sa_group_t group;

	if (hier &&
	    (group = sa_get_parent_group((sa_share_t)object)) != NULL) {
		newoptionset = sa_get_derived_optionset((void *)group, proto,
		    hier);
	} else {
		newoptionset = (sa_optionset_t)xmlNewNode(NULL,
		    (xmlChar *)"optionset");
		if (newoptionset != NULL) {
			sa_set_optionset_attr(newoptionset, "type", proto);
		}
	}
	/* Dont' do anything if memory wasn't allocated */
	if (newoptionset == NULL)
		return (NULL);

	/* Found the top so working back down the stack */
	optionset = sa_get_optionset((sa_optionset_t)object, proto);
	if (optionset != NULL) {
		sa_property_t prop;
		/* add optionset to the newoptionset */
		for (prop = sa_get_property(optionset, NULL);
		    prop != NULL;
		    prop = sa_get_next_property(prop)) {
			sa_property_t newprop;
			char *name;
			char *value;
			name = sa_get_property_attr(prop, "type");
			value = sa_get_property_attr(prop, "value");
			if (name == NULL)
				continue;
			newprop = sa_get_property(newoptionset, name);
			/* Replace the value with the new value */
			if (newprop != NULL) {
				/*
				 * Only set if value is non NULL, old value ok
				 * if it is NULL.
				 */
				if (value != NULL)
					set_node_attr(newprop, "value", value);
			} else {
				/* an entirely new property */
				if (value != NULL) {
					newprop = sa_create_property(name,
					    value);
					if (newprop != NULL) {
						newprop = (sa_property_t)
						    xmlAddChild(
						    (xmlNodePtr)newoptionset,
						    (xmlNodePtr)newprop);
					}
				}
			}
			sa_free_attr_string(name);

			if (value != NULL)
				sa_free_attr_string(value);
		}
	}
	return (newoptionset);
}

void
sa_free_derived_optionset(sa_optionset_t optionset)
{
	/* While it shouldn't be linked, it doesn't hurt */
	if (optionset != NULL) {
		xmlUnlinkNode((xmlNodePtr) optionset);
		xmlFreeNode((xmlNodePtr) optionset);
	}
}

/*
 *  sa_get_all_security_types(object, proto, hier)
 *
 *	Find all the security types set for this object.  This is
 *	preliminary to getting a derived security set. The return value is an
 *	optionset containg properties which are the sectype values found by
 *	walking up the XML document structure. The returned optionset
 *	is a derived optionset.
 *
 *	If hier is 0, only look at object. If non-zero, walk up the tree.
 */
sa_optionset_t
sa_get_all_security_types(void *object, char *proto, int hier)
{
	sa_optionset_t options;
	sa_security_t security;
	sa_group_t group;
	sa_property_t prop;

	options = NULL;

	if (hier &&
	    (group = sa_get_parent_group((sa_share_t)object)) != NULL)
		options = sa_get_all_security_types((void *)group, proto, hier);
	else
		options = (sa_optionset_t)xmlNewNode(NULL,
		    (xmlChar *)"optionset");

	if (options == NULL)
		return (options);

	/* Hit the top so collect the security types working back. */
	for (security = sa_get_security((sa_group_t)object, NULL, NULL);
	    security != NULL;
	    security = sa_get_next_security(security)) {
		char *type;
		char *sectype;

		type = sa_get_security_attr(security, "type");
		if (type != NULL) {
			if (strcmp(type, proto) != 0) {
				sa_free_attr_string(type);
				continue;
			}
			sectype = sa_get_security_attr(security, "sectype");
			if (sectype != NULL) {
				/*
				 * Have a security type, check to see if
				 * already present in optionset and add if it
				 * isn't.
				 */
				if (sa_get_property(options, sectype) == NULL) {
					prop = sa_create_property(sectype,
					    "true");
					if (prop != NULL)
						prop = (sa_property_t)
						    xmlAddChild(
						    (xmlNodePtr)options,
						    (xmlNodePtr)prop);
				}
				sa_free_attr_string(sectype);
			}
			sa_free_attr_string(type);
		}
	}

	return (options);
}

/*
 * sa_get_derived_security(object, sectype, proto, hier)
 *
 * Get the derived security(named optionset) for the object given the
 * sectype and proto. If hier is non-zero, walk up the tree to get all
 * properties defined for this object, otherwise just those on the
 * object.
 */

sa_security_t
sa_get_derived_security(void *object, char *sectype, char *proto, int hier)
{
	sa_security_t newsecurity;
	sa_security_t security;
	sa_group_t group;
	sa_property_t prop;

	if (hier &&
	    (group = sa_get_parent_group((sa_share_t)object)) != NULL) {
		newsecurity = sa_get_derived_security((void *)group,
		    sectype, proto, hier);
	} else {
		newsecurity = (sa_security_t)xmlNewNode(NULL,
		    (xmlChar *)"security");
		if (newsecurity != NULL) {
			sa_set_security_attr(newsecurity, "type", proto);
			sa_set_security_attr(newsecurity, "sectype", sectype);
		}
	}
	/* Don't do anything if memory wasn't allocated */
	if (newsecurity == NULL)
		return (newsecurity);

	/* Found the top so working back down the stack. */
	security = sa_get_security((sa_security_t)object, sectype, proto);
	if (security == NULL)
		return (newsecurity);

	/* add security to the newsecurity */
	for (prop = sa_get_property(security, NULL);
	    prop != NULL; prop = sa_get_next_property(prop)) {
		sa_property_t newprop;
		char *name;
		char *value;
		name = sa_get_property_attr(prop, "type");
		value = sa_get_property_attr(prop, "value");
		if (name != NULL) {
			newprop = sa_get_property(newsecurity, name);
			/* Replace the value with the new value */
			if (newprop != NULL) {
				/*
				 * Only set if value is non NULL, old
				 * value ok if it is NULL. The value
				 * must be associated with the "value"
				 * tag within XML.
				 */
				if (value != NULL)
					set_node_attr(newprop, "value", value);
			} else {
				/* An entirely new property */
				if (value != NULL) {
					newprop = sa_create_property(name,
					    value);
					newprop = (sa_property_t)
					    xmlAddChild((xmlNodePtr)newsecurity,
					    (xmlNodePtr)newprop);
				}
			}
			sa_free_attr_string(name);
		}
		if (value != NULL)
			sa_free_attr_string(value);
	}
	return (newsecurity);
}

void
sa_free_derived_security(sa_security_t security)
{
	/* while it shouldn't be linked, it doesn't hurt */
	if (security != NULL) {
		xmlUnlinkNode((xmlNodePtr)security);
		xmlFreeNode((xmlNodePtr)security);
	}
}

/*
 * sharetab utility functions
 *
 * Makes use of the original sharetab.c from fs.d/nfs/lib
 */

/*
 * sa_fillshare(share, proto, sh)
 *
 * Fill the struct share with values obtained from the share object.
 */
void
sa_fillshare(sa_share_t share, char *proto, struct share *sh)
{
	char *groupname = NULL;
	char *value;
	sa_group_t group;
	char *buff;
	char *zfs;
	sa_resource_t resource;
	char *rsrcname = NULL;
	char *defprop;

	/*
	 * We only want to deal with the path level shares for the
	 * sharetab file. If a resource, get the parent.
	 */
	if (sa_is_resource(share)) {
		resource = (sa_resource_t)share;
		share = sa_get_resource_parent(resource);
		rsrcname = sa_get_resource_attr(resource, "name");
	}

	group = sa_get_parent_group(share);
	if (group != NULL) {
		zfs = sa_get_group_attr(group, "zfs");
		groupname = sa_get_group_attr(group, "name");

		if (groupname != NULL &&
		    (strcmp(groupname, "default") == 0 || zfs != NULL)) {
			/*
			 * since the groupname is either "default" or the
			 * group is a ZFS group, we don't want to keep
			 * groupname. We do want it if it is any other type of
			 * group.
			 */
			sa_free_attr_string(groupname);
			groupname = NULL;
		}
		if (zfs != NULL)
			sa_free_attr_string(zfs);
	}

	value = sa_get_share_attr(share, "path");
	if (value != NULL) {
		sh->sh_path = strdup(value);
		sa_free_attr_string(value);
	}

	if (rsrcname != NULL || groupname != NULL) {
		int len = 0;

		if (rsrcname != NULL)
			len += strlen(rsrcname);
		if (groupname != NULL)
			len += strlen(groupname);
		len += 3; /* worst case */
		buff = malloc(len);
		(void) snprintf(buff, len, "%s%s%s",
		    (rsrcname != NULL &&
		    strlen(rsrcname) > 0) ? rsrcname : "-",
		    groupname != NULL ? "@" : "",
		    groupname != NULL ? groupname : "");
		sh->sh_res = buff;
		if (rsrcname != NULL)
			sa_free_attr_string(rsrcname);
		if (groupname != NULL)
			sa_free_attr_string(groupname);
	} else {
		sh->sh_res = strdup("-");
	}

	/*
	 * Get correct default prop string. NFS uses "rw", others use
	 * "".
	 */
	if (strcmp(proto, "nfs") != 0)
		defprop = "\"\"";
	else
		defprop = "rw";

	sh->sh_fstype = strdup(proto);
	value = sa_proto_legacy_format(proto, share, 1);
	if (value != NULL) {
		if (strlen(value) > 0)
			sh->sh_opts = strdup(value);
		else
			sh->sh_opts = strdup(defprop);
		free(value);
	} else {
		sh->sh_opts = strdup(defprop);
	}

	value = sa_get_share_description(share);
	if (value != NULL) {
		sh->sh_descr = strdup(value);
		sa_free_share_description(value);
	} else {
		sh->sh_descr = strdup("");
	}
}

/*
 * sa_emptyshare(sh)
 *
 * Free the strings in the non-NULL members of sh.
 */

void
sa_emptyshare(struct share *sh)
{
	if (sh->sh_path != NULL)
		free(sh->sh_path);
	sh->sh_path = NULL;
	if (sh->sh_res != NULL)
		free(sh->sh_res);
	sh->sh_res = NULL;
	if (sh->sh_fstype != NULL)
		free(sh->sh_fstype);
	sh->sh_fstype = NULL;
	if (sh->sh_opts != NULL)
		free(sh->sh_opts);
	sh->sh_opts = NULL;
	if (sh->sh_descr != NULL)
		free(sh->sh_descr);
	sh->sh_descr = NULL;
}

/*
 * sa_update_sharetab_ts(handle)
 *
 * Update the internal timestamp of when sharetab was last
 * changed. This needs to be public for ZFS to get at it.
 */

void
sa_update_sharetab_ts(sa_handle_t handle)
{
	struct stat st;
	sa_handle_impl_t implhandle = (sa_handle_impl_t)handle;

	if (implhandle != NULL && stat(SA_LEGACY_SHARETAB, &st) == 0)
		implhandle->tssharetab = TSTAMP(st.st_mtim);
}

/*
 * sa_update_sharetab(share, proto)
 *
 * Update the sharetab file with info from the specified share.
 * This could be an update or add.
 */

int
sa_update_sharetab(sa_share_t share, char *proto)
{
	int	ret = SA_OK;
	share_t	sh;
	char	*path;
	sa_handle_t handle;

	path = sa_get_share_attr(share, "path");
	if (path != NULL) {
		(void) memset(&sh, '\0', sizeof (sh));

		handle = sa_find_group_handle((sa_group_t)share);
		if (handle != NULL) {
			/*
			 * Fill in share structure and send it to the kernel.
			 */
			(void) sa_fillshare(share, proto, &sh);
			(void) _sharefs(SHAREFS_ADD, &sh);
			/*
			 * We need the timestamp of the sharetab file right
			 * after the update was done. This lets us detect a
			 * change that made by a different process.
			 */
			sa_update_sharetab_ts(handle);
			sa_emptyshare(&sh);
		} else {
			ret = SA_CONFIG_ERR;
		}
		sa_free_attr_string(path);
	}

	return (ret);
}

/*
 * sa_delete_sharetab(handle, path, proto)
 *
 * remove the specified share from sharetab.
 */

int
sa_delete_sharetab(sa_handle_t handle, char *path, char *proto)
{
	int	ret = SA_OK;
	struct stat st;

	share_t	sh;
	/*
	 * Both the path and the proto are
	 * keys into the sharetab.
	 */
	if (path != NULL && proto != NULL) {
		(void) memset(&sh, '\0', sizeof (sh));
		sh.sh_path = path;
		sh.sh_fstype = proto;

		ret = _sharefs(SHAREFS_REMOVE, &sh);
		if (handle != NULL && stat(SA_LEGACY_SHARETAB, &st) == 0)
			sa_update_sharetab_ts(handle);
	}
	return (ret);
}

/*
 * sa_needs_refresh(handle)
 *
 * Returns B_TRUE if the internal cache needs to be refreshed due to a
 * change by another process.  B_FALSE returned otherwise.
 */
boolean_t
sa_needs_refresh(sa_handle_t handle)
{
	sa_handle_impl_t implhandle = (sa_handle_impl_t)handle;
	struct stat st;
	char *str;
	uint64_t tstamp;
	scf_simple_prop_t *prop;

	if (handle == NULL || implhandle->sa_service &
	    (SA_INIT_ONE_SHARE_FROM_NAME | SA_INIT_ONE_SHARE_FROM_HANDLE))
		return (B_TRUE);

	/*
	 * If sharetab has changed, then there was an external
	 * change. Check sharetab first since it is updated by ZFS as
	 * well as sharemgr.  This is where external ZFS changes are
	 * caught.
	 */
	if (stat(SA_LEGACY_SHARETAB, &st) == 0 &&
	    TSTAMP(st.st_mtim) != implhandle->tssharetab)
		return (B_TRUE);

	/*
	 * If sharetab wasn't changed, check whether there were any
	 * SMF transactions that modified the config but didn't
	 * initiate a share.  This is less common but does happen.
	 */
	prop = scf_simple_prop_get(implhandle->scfhandle->handle,
	    (const char *)SA_SVC_FMRI_BASE ":default", "state",
	    "lastupdate");
	if (prop != NULL) {
		str = scf_simple_prop_next_astring(prop);
		if (str != NULL)
			tstamp = strtoull(str, NULL, 0);
		else
			tstamp = 0;
		scf_simple_prop_free(prop);
		if (tstamp != implhandle->tstrans)
			return (B_TRUE);
	}

	return (B_FALSE);
}

/*
 * sa_fix_resource_name(path)
 *
 * Convert invalid characters in a resource name (SMB share name)
 * to underscores ('_').  The list of invalid characters includes
 * control characters and the following:
 *
 *	" / \ [ ] : | < > + ; , ? * =
 *
 * The caller must pass a valid path.  Leading and trailing slashes
 * are stripped from the path before converting invalid characters.
 * Resource names are restricted to SA_MAX_RESOURCE_NAME characters.
 */
void
sa_fix_resource_name(char *path)
{
	char *invalid = "\"/\\[]:|<>+;,?*=";
	char *p = path;
	char *q;
	size_t len;

	assert(path != NULL);

	/*
	 * Strip leading and trailing /'s.
	 */
	p += strspn(p, "/");
	q = strchr(p, '\0');
	if (q != NULL && q != path) {
		while ((--q, *q == '/'))
			*q = '\0';
	}

	if (*p == '\0') {
		(void) strcpy(path, "_");
		return;
	}

	/*
	 * Stride over path components until the remaining
	 * path is no longer than SA_MAX_RESOURCE_NAME.
	 */
	q = p;
	while ((q != NULL) && (strlen(q) > SA_MAX_RESOURCE_NAME)) {
		if ((q = strchr(q, '/')) != NULL) {
			++q;
			p = q;
		}
	}

	/*
	 * If the path is still longer than SA_MAX_RESOURCE_NAME,
	 * take the trailing SA_MAX_RESOURCE_NAME characters.
	 */
	if ((len = strlen(p)) > SA_MAX_RESOURCE_NAME) {
		len = SA_MAX_RESOURCE_NAME;
		p = strchr(p, '\0') - (SA_MAX_RESOURCE_NAME - 1);
	}

	(void) memmove(path, p, len);
	path[len] = '\0';

	for (p = path; *p != '\0'; ++p) {
		if ((iscntrl(*p)) || strchr(invalid, *p))
			*p = '_';
	}
}