summaryrefslogtreecommitdiff
path: root/usr/src/lib/libshare/common/libshare.c
blob: 6d43efbcffa33919c154dbcda7dec500248936de (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
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
/*
 * 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 2007 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

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

/*
 * Share control API
 */
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <libxml/parser.h>
#include <libxml/tree.h>
#include "libshare.h"
#include "libshare_impl.h"
#include <libscf.h>
#include "scfutil.h"
#include <ctype.h>
#include <libintl.h>
#include <thread.h>
#include <synch.h>

#if _NOT_SMF
#define	CONFIG_FILE	"/var/tmp/share.cfg"
#define	CONFIG_FILE_TMP	"/var/tmp/share.cfg.tmp"
#endif
#define	TSTAMP(tm)	(uint64_t)(((uint64_t)tm.tv_sec << 32) | \
					(tm.tv_nsec & 0xffffffff))

#define	DFS_LOCK_FILE	"/etc/dfs/fstypes"
#define	SA_STRSIZE	256	/* max string size for names */

/*
 * internal data structures
 */

extern struct sa_proto_plugin *sap_proto_list;

/* current SMF/SVC repository handle */
extern void getlegacyconfig(sa_handle_t, char *, xmlNodePtr *);
extern int gettransients(sa_handle_impl_t, xmlNodePtr *);
extern int sa_valid_property(void *, char *, sa_property_t);
extern char *sa_fstype(char *);
extern int sa_is_share(void *);
extern ssize_t scf_max_name_len; /* defined in scfutil during initialization */
extern int sa_group_is_zfs(sa_group_t);
extern int sa_path_is_zfs(char *);
extern int sa_zfs_set_sharenfs(sa_group_t, char *, int);
extern void update_legacy_config(sa_handle_t);
extern int issubdir(char *, char *);
extern int sa_zfs_init(sa_handle_impl_t);
extern void sa_zfs_fini(sa_handle_impl_t);
extern void sablocksigs(sigset_t *);
extern void saunblocksigs(sigset_t *);

/*
 * Data structures for finding/managing the document root to access
 * handle mapping. The list isn't expected to grow very large so a
 * simple list is acceptable. The purpose is to provide a way to start
 * with a group or share and find the library handle needed for
 * various operations.
 */
mutex_t sa_global_lock;
struct doc2handle {
	struct doc2handle	*next;
	xmlNodePtr		root;
	sa_handle_impl_t	handle;
};

/* definitions used in a couple of property functions */
#define	SA_PROP_OP_REMOVE	1
#define	SA_PROP_OP_ADD		2
#define	SA_PROP_OP_UPDATE	3

static struct doc2handle *sa_global_handles = NULL;

/* helper functions */

/*
 * sa_errorstr(err)
 *
 * convert an error value to an error string
 */

char *
sa_errorstr(int err)
{
	static char errstr[32];
	char *ret = NULL;

	switch (err) {
	case SA_OK:
		ret = dgettext(TEXT_DOMAIN, "ok");
		break;
	case SA_NO_SUCH_PATH:
		ret = dgettext(TEXT_DOMAIN, "path doesn't exist");
		break;
	case SA_NO_MEMORY:
		ret = dgettext(TEXT_DOMAIN, "no memory");
		break;
	case SA_DUPLICATE_NAME:
		ret = dgettext(TEXT_DOMAIN, "name in use");
		break;
	case SA_BAD_PATH:
		ret = dgettext(TEXT_DOMAIN, "bad path");
		break;
	case SA_NO_SUCH_GROUP:
		ret = dgettext(TEXT_DOMAIN, "no such group");
		break;
	case SA_CONFIG_ERR:
		ret = dgettext(TEXT_DOMAIN, "configuration error");
		break;
	case SA_SYSTEM_ERR:
		ret = dgettext(TEXT_DOMAIN, "system error");
		break;
	case SA_SYNTAX_ERR:
		ret = dgettext(TEXT_DOMAIN, "syntax error");
		break;
	case SA_NO_PERMISSION:
		ret = dgettext(TEXT_DOMAIN, "no permission");
		break;
	case SA_BUSY:
		ret = dgettext(TEXT_DOMAIN, "busy");
		break;
	case SA_NO_SUCH_PROP:
		ret = dgettext(TEXT_DOMAIN, "no such property");
		break;
	case SA_INVALID_NAME:
		ret = dgettext(TEXT_DOMAIN, "invalid name");
		break;
	case SA_INVALID_PROTOCOL:
		ret = dgettext(TEXT_DOMAIN, "invalid protocol");
		break;
	case SA_NOT_ALLOWED:
		ret = dgettext(TEXT_DOMAIN, "operation not allowed");
		break;
	case SA_BAD_VALUE:
		ret = dgettext(TEXT_DOMAIN, "bad property value");
		break;
	case SA_INVALID_SECURITY:
		ret = dgettext(TEXT_DOMAIN, "invalid security type");
		break;
	case SA_NO_SUCH_SECURITY:
		ret = dgettext(TEXT_DOMAIN, "security type not found");
		break;
	case SA_VALUE_CONFLICT:
		ret = dgettext(TEXT_DOMAIN, "property value conflict");
		break;
	case SA_NOT_IMPLEMENTED:
		ret = dgettext(TEXT_DOMAIN, "not implemented");
		break;
	case SA_INVALID_PATH:
		ret = dgettext(TEXT_DOMAIN, "invalid path");
		break;
	case SA_NOT_SUPPORTED:
		ret = dgettext(TEXT_DOMAIN, "operation not supported");
		break;
	case SA_PROP_SHARE_ONLY:
		ret = dgettext(TEXT_DOMAIN, "property not valid for group");
		break;
	case SA_NOT_SHARED:
		ret = dgettext(TEXT_DOMAIN, "not shared");
		break;
	default:
		(void) snprintf(errstr, sizeof (errstr),
		    dgettext(TEXT_DOMAIN, "unknown %d"), err);
		ret = errstr;
	}
	return (ret);
}

/*
 * Document root to active handle mapping functions.  These are only
 * used internally. A mutex is used to prevent access while the list
 * is changing. In general, the list will be relatively short - one
 * item per thread that has called sa_init().
 */

sa_handle_impl_t
get_handle_for_root(xmlNodePtr root)
{
	struct doc2handle *item;

	(void) mutex_lock(&sa_global_lock);
	for (item = sa_global_handles; item != NULL; item = item->next) {
		if (item->root == root)
			break;
	}
	(void) mutex_unlock(&sa_global_lock);
	if (item != NULL)
		return (item->handle);
	return (NULL);
}

static int
add_handle_for_root(xmlNodePtr root, sa_handle_impl_t handle)
{
	struct doc2handle *item;
	int ret = SA_NO_MEMORY;

	item = (struct doc2handle *)calloc(sizeof (struct doc2handle), 1);
	if (item != NULL) {
		item->root = root;
		item->handle = handle;
		(void) mutex_lock(&sa_global_lock);
		item->next = sa_global_handles;
		sa_global_handles = item;
		(void) mutex_unlock(&sa_global_lock);
		ret = SA_OK;
	}
	return (ret);
}

/*
 * remove_handle_for_root(root)
 *
 * Walks the list of handles and removes the one for this "root" from
 * the list. It is up to the caller to free the data.
 */

static void
remove_handle_for_root(xmlNodePtr root)
{
	struct doc2handle *item, *prev;

	(void) mutex_lock(&sa_global_lock);
	for (prev = NULL, item = sa_global_handles; item != NULL;
	    item = item->next) {
		if (item->root == root) {
			/* first in the list */
			if (prev == NULL)
				sa_global_handles = sa_global_handles->next;
			else
				prev->next = item->next;
			/* Item is out of the list so free the list structure */
			free(item);
			break;
		}
		prev = item;
	}
	(void) mutex_unlock(&sa_global_lock);
}

/*
 * sa_find_group_handle(sa_group_t group)
 *
 * Find the sa_handle_t for the configuration associated with this
 * group.
 */
sa_handle_t
sa_find_group_handle(sa_group_t group)
{
	xmlNodePtr node = (xmlNodePtr)group;
	sa_handle_t handle;

	while (node != NULL) {
		if (strcmp((char *)(node->name), "sharecfg") == 0) {
			/* have the root so get the handle */
			handle = (sa_handle_t)get_handle_for_root(node);
			return (handle);
		}
		node = node->parent;
	}
	return (NULL);
}

/*
 * set_legacy_timestamp(root, path, timevalue)
 *
 * add the current timestamp value to the configuration for use in
 * determining when to update the legacy files.  For SMF, this
 * property is kept in default/operation/legacy_timestamp
 */

static void
set_legacy_timestamp(xmlNodePtr root, char *path, uint64_t tval)
{
	xmlNodePtr node;
	xmlChar *lpath = NULL;
	sa_handle_impl_t handle;

	/* Have to have a handle or else we weren't initialized. */
	handle = get_handle_for_root(root);
	if (handle == NULL)
		return;

	for (node = root->xmlChildrenNode; node != NULL;
	    node = node->next) {
		if (xmlStrcmp(node->name, (xmlChar *)"legacy") == 0) {
			/* a possible legacy node for this path */
			lpath = xmlGetProp(node, (xmlChar *)"path");
			if (lpath != NULL &&
			    xmlStrcmp(lpath, (xmlChar *)path) == 0) {
				xmlFree(lpath);
				break;
			}
			if (lpath != NULL)
				xmlFree(lpath);
		}
	}
	if (node == NULL) {
		/* need to create the first legacy timestamp node */
		node = xmlNewChild(root, NULL, (xmlChar *)"legacy", NULL);
	}
	if (node != NULL) {
		char tstring[32];
		int ret;

		(void) snprintf(tstring, sizeof (tstring), "%lld", tval);
		xmlSetProp(node, (xmlChar *)"timestamp", (xmlChar *)tstring);
		xmlSetProp(node, (xmlChar *)"path", (xmlChar *)path);
		/* now commit to SMF */
		ret = sa_get_instance(handle->scfhandle, "default");
		if (ret == SA_OK) {
			ret = sa_start_transaction(handle->scfhandle,
			    "operation");
			if (ret == SA_OK) {
				ret = sa_set_property(handle->scfhandle,
				    "legacy-timestamp", tstring);
				if (ret == SA_OK) {
					(void) sa_end_transaction(
					    handle->scfhandle);
				} else {
					sa_abort_transaction(handle->scfhandle);
				}
			}
		}
	}
}

/*
 * is_shared(share)
 *
 * determine if the specified share is currently shared or not.
 */
static int
is_shared(sa_share_t share)
{
	char *shared;
	int result = 0; /* assume not */

	shared = sa_get_share_attr(share, "shared");
	if (shared != NULL) {
		if (strcmp(shared, "true") == 0)
			result = 1;
		sa_free_attr_string(shared);
	}
	return (result);
}

/*
 * checksubdirgroup(group, newpath, strictness)
 *
 * check all the specified newpath against all the paths in the
 * group. This is a helper function for checksubdir to make it easier
 * to also check ZFS subgroups.
 * The strictness values mean:
 * SA_CHECK_NORMAL == only check newpath against shares that are active
 * SA_CHECK_STRICT == check newpath against both active shares and those
 *		      stored in the repository
 */
static int
checksubdirgroup(sa_group_t group, char *newpath, int strictness)
{
	sa_share_t share;
	char *path;
	int issub = SA_OK;

	for (share = sa_get_share(group, NULL); share != NULL;
	    share = sa_get_next_share(share)) {
		/*
		 * The original behavior of share never checked
		 * against the permanent configuration
		 * (/etc/dfs/dfstab).  PIT has a number of cases where
		 * it depends on this older behavior even though it
		 * could be considered incorrect.  We may tighten this
		 * up in the future.
		 */
		if (strictness == SA_CHECK_NORMAL && !is_shared(share))
			continue;

		path = sa_get_share_attr(share, "path");
		/*
		 * If path is NULL, then a share is in the process of
		 * construction or someone has modified the property
		 * group inappropriately. It should be
		 * ignored. issubdir() comes from the original share
		 * implementation and does the difficult part of
		 * checking subdirectories.
		 */
		if (path == NULL)
			continue;
		if (newpath != NULL &&
		    (strcmp(path, newpath) == 0 || issubdir(newpath, path) ||
		    issubdir(path, newpath))) {
			sa_free_attr_string(path);
			path = NULL;
			issub = SA_INVALID_PATH;
			break;
		}
		sa_free_attr_string(path);
		path = NULL;
	}
	return (issub);
}

/*
 * checksubdir(newpath, strictness)
 *
 * checksubdir determines if the specified path (newpath) is a
 * subdirectory of another share. It calls checksubdirgroup() to do
 * the complicated work. The strictness parameter determines how
 * strict a check to make against the path. The strictness values
 * mean: SA_CHECK_NORMAL == only check newpath against shares that are
 * active SA_CHECK_STRICT == check newpath against both active shares
 * and those * stored in the repository
 */
static int
checksubdir(sa_handle_t handle, char *newpath, int strictness)
{
	sa_group_t group;
	int issub;
	char *path = NULL;

	for (issub = 0, group = sa_get_group(handle, NULL);
	    group != NULL && !issub; group = sa_get_next_group(group)) {
		if (sa_group_is_zfs(group)) {
			sa_group_t subgroup;
			for (subgroup = sa_get_sub_group(group);
			    subgroup != NULL && !issub;
			    subgroup = sa_get_next_group(subgroup))
				issub = checksubdirgroup(subgroup, newpath,
				    strictness);
		} else {
			issub = checksubdirgroup(group, newpath, strictness);
		}
	}
	if (path != NULL)
		sa_free_attr_string(path);
	return (issub);
}

/*
 * validpath(path, strictness)
 * determine if the provided path is valid for a share. It shouldn't
 * be a sub-dir of an already shared path or the parent directory of a
 * share path.
 */
static int
validpath(sa_handle_t handle, char *path, int strictness)
{
	int error = SA_OK;
	struct stat st;
	sa_share_t share;
	char *fstype;

	if (*path != '/')
		return (SA_BAD_PATH);

	if (stat(path, &st) < 0) {
		error = SA_NO_SUCH_PATH;
	} else {
		share = sa_find_share(handle, path);
		if (share != NULL)
			error = SA_DUPLICATE_NAME;

		if (error == SA_OK) {
			/*
			 * check for special case with file system
			 * that might have restrictions.  For now, ZFS
			 * is the only case since it has its own idea
			 * of how to configure shares. We do this
			 * before subdir checking since things like
			 * ZFS will do that for us. This should also
			 * be done via plugin interface.
			 */
			fstype = sa_fstype(path);
			if (fstype != NULL && strcmp(fstype, "zfs") == 0) {
				if (sa_zfs_is_shared(handle, path))
					error = SA_INVALID_NAME;
			}
			if (fstype != NULL)
				sa_free_fstype(fstype);
		}
		if (error == SA_OK)
			error = checksubdir(handle, path, strictness);
	}
	return (error);
}

/*
 * check to see if group/share is persistent.
 */
static int
is_persistent(sa_group_t group)
{
	char *type;
	int persist = 1;

	type = sa_get_group_attr(group, "type");
	if (type != NULL && strcmp(type, "transient") == 0)
		persist = 0;
	if (type != NULL)
		sa_free_attr_string(type);
	return (persist);
}

/*
 * sa_valid_group_name(name)
 *
 * check that the "name" contains only valid characters and otherwise
 * fits the required naming conventions. Valid names must start with
 * an alphabetic and the remainder may consist of only alphanumeric
 * plus the '-' and '_' characters. This name limitation comes from
 * inherent limitations in SMF.
 */

int
sa_valid_group_name(char *name)
{
	int ret = 1;
	ssize_t len;

	if (name != NULL && isalpha(*name)) {
		char c;
		len = strlen(name);
		if (len < (scf_max_name_len - sizeof ("group:"))) {
			for (c = *name++; c != '\0' && ret != 0; c = *name++) {
				if (!isalnum(c) && c != '-' && c != '_')
					ret = 0;
			}
		} else {
			ret = 0;
		}
	} else {
		ret = 0;
	}
	return (ret);
}


/*
 * is_zfs_group(group)
 *	Determine if the specified group is a ZFS sharenfs group
 */
static int
is_zfs_group(sa_group_t group)
{
	int ret = 0;
	xmlNodePtr parent;
	xmlChar *zfs;

	if (strcmp((char *)((xmlNodePtr)group)->name, "share") == 0)
		parent = (xmlNodePtr)sa_get_parent_group(group);
	else
		parent = (xmlNodePtr)group;
	zfs = xmlGetProp(parent, (xmlChar *)"zfs");
	if (zfs != NULL) {
		xmlFree(zfs);
		ret = 1;
	}
	return (ret);
}

/*
 * sa_optionset_name(optionset, oname, len, id)
 *	return the SMF name for the optionset. If id is not NULL, it
 *	will have the GUID value for a share and should be used
 *	instead of the keyword "optionset" which is used for
 *	groups. If the optionset doesn't have a protocol type
 *	associated with it, "default" is used. This shouldn't happen
 *	at this point but may be desirable in the future if there are
 *	protocol independent properties added. The name is returned in
 *	oname.
 */

static int
sa_optionset_name(sa_optionset_t optionset, char *oname, size_t len, char *id)
{
	char *proto;

	if (id == NULL)
		id = "optionset";

	proto = sa_get_optionset_attr(optionset, "type");
	len = snprintf(oname, len, "%s_%s", id, proto ? proto : "default");

	if (proto != NULL)
		sa_free_attr_string(proto);
	return (len);
}

/*
 * sa_security_name(optionset, oname, len, id)
 *
 * return the SMF name for the security. If id is not NULL, it will
 * have the GUID value for a share and should be used instead of the
 * keyword "optionset" which is used for groups. If the optionset
 * doesn't have a protocol type associated with it, "default" is
 * used. This shouldn't happen at this point but may be desirable in
 * the future if there are protocol independent properties added. The
 * name is returned in oname. The security type is also encoded into
 * the name. In the future, this wil *be handled a bit differently.
 */

static int
sa_security_name(sa_security_t security, char *oname, size_t len, char *id)
{
	char *proto;
	char *sectype;

	if (id == NULL)
		id = "optionset";

	proto = sa_get_security_attr(security, "type");
	sectype = sa_get_security_attr(security, "sectype");
	len = snprintf(oname, len, "%s_%s_%s", id, proto ? proto : "default",
	    sectype ? sectype : "default");
	if (proto != NULL)
		sa_free_attr_string(proto);
	if (sectype != NULL)
		sa_free_attr_string(sectype);
	return (len);
}

/*
 * verifydefgroupopts(handle)
 *
 * Make sure a "default" group exists and has default protocols enabled.
 */
static void
verifydefgroupopts(sa_handle_t handle)
{
	sa_group_t defgrp;
	sa_optionset_t opt;
	defgrp = sa_get_group(handle, "default");
	if (defgrp != NULL) {
		opt = sa_get_optionset(defgrp, NULL);
		/*
		 * NFS is the default for default group
		 */
		if (opt == NULL)
			opt = sa_create_optionset(defgrp, "nfs");
	}
}

/*
 * sa_init(init_service)
 *	Initialize the API
 *	find all the shared objects
 *	init the tables with all objects
 *	read in the current configuration
 */

#define	GETPROP(prop)	scf_simple_prop_next_astring(prop)
#define	CHECKTSTAMP(st, tval)	stat(SA_LEGACY_DFSTAB, &st) >= 0 && \
	tval != TSTAMP(st.st_ctim)

sa_handle_t
sa_init(int init_service)
{
	struct stat st;
	int legacy = 0;
	uint64_t tval = 0;
	int lockfd;
	sigset_t old;
	int updatelegacy = B_FALSE;
	scf_simple_prop_t *prop;
	sa_handle_impl_t handle;
	int err;

	handle = calloc(sizeof (struct sa_handle_impl), 1);

	if (handle != NULL) {
		/* get protocol specific structures */
		(void) proto_plugin_init();
		if (init_service & SA_INIT_SHARE_API) {
			/*
			 * initialize access into libzfs. We use this
			 * when collecting info about ZFS datasets and
			 * shares.
			 */
			if (sa_zfs_init(handle) == B_FALSE) {
				free(handle);
				(void) proto_plugin_fini();
				return (NULL);
			}
			/*
			 * since we want to use SMF, initialize an svc handle
			 * and find out what is there.
			 */
			handle->scfhandle = sa_scf_init(handle);
			if (handle->scfhandle != NULL) {
				/*
				 * Need to lock the extraction of the
				 * configuration if the dfstab file has
				 * changed. Lock everything now and release if
				 * not needed.  Use a file that isn't being
				 * manipulated by other parts of the system in
				 * order to not interfere with locking. Using
				 * dfstab doesn't work.
				 */
				sablocksigs(&old);
				lockfd = open(DFS_LOCK_FILE, O_RDWR);
				if (lockfd >= 0) {
					extern int errno;
					errno = 0;
					(void) lockf(lockfd, F_LOCK, 0);
					/*
					 * Check whether we are going to need
					 * to merge any dfstab changes. This
					 * is done by comparing the value of
					 * legacy-timestamp with the current
					 * st_ctim of the file. If they are
					 * different, an update is needed and
					 * the file must remain locked until
					 * the merge is done in order to
					 * prevent multiple startups from
					 * changing the SMF repository at the
					 * same time.  The first to get the
					 * lock will make any changes before
					 * the others can read the repository.
					 */
					prop = scf_simple_prop_get
					    (handle->scfhandle->handle,
					    (const char *)SA_SVC_FMRI_BASE
					    ":default", "operation",
					    "legacy-timestamp");
					if (prop != NULL) {
						char *i64;
						i64 = GETPROP(prop);
						if (i64 != NULL)
							tval = strtoull(i64,
							    NULL, 0);
						if (CHECKTSTAMP(st, tval))
							updatelegacy = B_TRUE;
						scf_simple_prop_free(prop);
					} else {
						/*
						 * We haven't set the
						 * timestamp before so do it.
						 */
						updatelegacy = B_TRUE;
					}
				}
				if (updatelegacy == B_FALSE) {
					/* Don't need the lock anymore */
					(void) lockf(lockfd, F_ULOCK, 0);
					(void) close(lockfd);
				}

				/*
				 * It is essential that the document tree and
				 * the internal list of roots to handles be
				 * setup before anything that might try to
				 * create a new object is called. The document
				 * tree is the combination of handle->doc and
				 * handle->tree. This allows searches,
				 * etc. when all you have is an object in the
				 * tree.
				 */
				handle->doc = xmlNewDoc((xmlChar *)"1.0");
				handle->tree = xmlNewNode(NULL,
				    (xmlChar *)"sharecfg");
				if (handle->doc != NULL &&
				    handle->tree != NULL) {
					xmlDocSetRootElement(handle->doc,
					    handle->tree);
					err = add_handle_for_root(handle->tree,
					    handle);
					if (err == SA_OK)
						err = sa_get_config(
						    handle->scfhandle,
						    handle->tree, handle);
				} else {
					if (handle->doc != NULL)
						xmlFreeDoc(handle->doc);
					if (handle->tree != NULL)
						xmlFreeNode(handle->tree);
					err = SA_NO_MEMORY;
				}

				saunblocksigs(&old);

				if (err != SA_OK) {
					/*
					 * If we couldn't add the tree handle
					 * to the list, then things are going
					 * to fail badly. Might as well undo
					 * everything now and fail the
					 * sa_init().
					 */
					sa_fini(handle);
					return (NULL);
				}

				if (tval == 0) {
					/*
					 * first time so make sure
					 * default is setup
					 */
					verifydefgroupopts(handle);
				}

			if (updatelegacy == B_TRUE) {
				sablocksigs(&old);
				getlegacyconfig((sa_handle_t)handle,
				    SA_LEGACY_DFSTAB, &handle->tree);
				if (stat(SA_LEGACY_DFSTAB, &st) >= 0)
					set_legacy_timestamp(handle->tree,
					    SA_LEGACY_DFSTAB,
					    TSTAMP(st.st_ctim));
				saunblocksigs(&old);
				/* Safe to unlock now to allow others to run */
				(void) lockf(lockfd, F_ULOCK, 0);
				(void) close(lockfd);
			}
			legacy |= sa_get_zfs_shares(handle, "zfs");
			legacy |= gettransients(handle, &handle->tree);
		}
		}
	}
	return ((sa_handle_t)handle);
}

/*
 * sa_fini(handle)
 *	Uninitialize the API structures including the configuration
 *	data structures and ZFS related data.
 */

void
sa_fini(sa_handle_t handle)
{
	sa_handle_impl_t impl_handle = (sa_handle_impl_t)handle;

	if (impl_handle != NULL) {
		/*
		 * Free the config trees and any other data structures
		 * used in the handle.
		 */
		if (impl_handle->doc != NULL)
			xmlFreeDoc(impl_handle->doc);
		sa_scf_fini(impl_handle->scfhandle);
		sa_zfs_fini(impl_handle);

		/* Remove and free the entry in the global list. */
		remove_handle_for_root(impl_handle->tree);

		/* Make sure we free the handle */
		free(impl_handle);

		/*
		 * If this was the last handle to release, unload the
		 * plugins that were loaded.
		 */
		if (sa_global_handles == NULL)
			(void) proto_plugin_fini();

	}
}

/*
 * sa_get_protocols(char **protocol)
 *	Get array of protocols that are supported
 *	Returns pointer to an allocated and NULL terminated
 *	array of strings.  Caller must free.
 *	This really should be determined dynamically.
 *	If there aren't any defined, return -1.
 *	Use free() to return memory.
 */

int
sa_get_protocols(char ***protocols)
{
	int numproto = -1;

	if (protocols != NULL) {
		struct sa_proto_plugin *plug;
		for (numproto = 0, plug = sap_proto_list; plug != NULL;
		    plug = plug->plugin_next) {
			numproto++;
		}

		*protocols = calloc(numproto + 1,  sizeof (char *));
		if (*protocols != NULL) {
			int ret = 0;
			for (plug = sap_proto_list; plug != NULL;
			    plug = plug->plugin_next) {
				/* faking for now */
				(*protocols)[ret++] =
				    plug->plugin_ops->sa_protocol;
			}
		} else {
			numproto = -1;
		}
	}
	return (numproto);
}

/*
 * find_group_by_name(node, group)
 *
 * search the XML document subtree specified by node to find the group
 * specified by group. Searching subtree allows subgroups to be
 * searched for.
 */

static xmlNodePtr
find_group_by_name(xmlNodePtr node, xmlChar *group)
{
	xmlChar *name = NULL;

	for (node = node->xmlChildrenNode; node != NULL;
	    node = node->next) {
		if (xmlStrcmp(node->name, (xmlChar *)"group") == 0) {
			/* if no groupname, return the first found */
			if (group == NULL)
				break;
			name = xmlGetProp(node, (xmlChar *)"name");
			if (name != NULL && xmlStrcmp(name, group) == 0)
				break;
			if (name != NULL) {
				xmlFree(name);
				name = NULL;
			}
		}
	}
	if (name != NULL)
		xmlFree(name);
	return (node);
}

/*
 * sa_get_group(groupname)
 *	Return the "group" specified.  If groupname is NULL,
 *	return the first group of the list of groups.
 */
sa_group_t
sa_get_group(sa_handle_t handle, char *groupname)
{
	xmlNodePtr node = NULL;
	char *subgroup = NULL;
	char *group = NULL;
	sa_handle_impl_t impl_handle = (sa_handle_impl_t)handle;

	if (impl_handle != NULL && impl_handle->tree != NULL) {
		if (groupname != NULL) {
			group = strdup(groupname);
			subgroup = strchr(group, '/');
			if (subgroup != NULL)
				*subgroup++ = '\0';
		}
		node = find_group_by_name(impl_handle->tree, (xmlChar *)group);
		/* if a subgroup, find it before returning */
		if (subgroup != NULL && node != NULL)
			node = find_group_by_name(node, (xmlChar *)subgroup);
	}
	if (node != NULL && (char *)group != NULL)
		(void) sa_get_instance(impl_handle->scfhandle, (char *)group);
	if (group != NULL)
		free(group);
	return ((sa_group_t)(node));
}

/*
 * sa_get_next_group(group)
 *	Return the "next" group after the specified group from
 *	the internal group list.  NULL if there are no more.
 */
sa_group_t
sa_get_next_group(sa_group_t group)
{
	xmlNodePtr ngroup = NULL;
	if (group != NULL) {
		for (ngroup = ((xmlNodePtr)group)->next; ngroup != NULL;
		    ngroup = ngroup->next) {
			if (xmlStrcmp(ngroup->name, (xmlChar *)"group") == 0)
				break;
		}
	}
	return ((sa_group_t)ngroup);
}

/*
 * sa_get_share(group, sharepath)
 *	Return the share object for the share specified. The share
 *	must be in the specified group.  Return NULL if not found.
 */
sa_share_t
sa_get_share(sa_group_t group, char *sharepath)
{
	xmlNodePtr node = NULL;
	xmlChar *path;

	/*
	 * For future scalability, this should end up building a cache
	 * since it will get called regularly by the mountd and info
	 * services.
	 */
	if (group != NULL) {
		for (node = ((xmlNodePtr)group)->children; node != NULL;
		    node = node->next) {
			if (xmlStrcmp(node->name, (xmlChar *)"share") == 0) {
				if (sharepath == NULL) {
					break;
				} else {
					/* is it the correct share? */
					path = xmlGetProp(node,
					    (xmlChar *)"path");
					if (path != NULL &&
					    xmlStrcmp(path,
					    (xmlChar *)sharepath) == 0) {
						xmlFree(path);
						break;
					}
					xmlFree(path);
				}
			}
		}
	}
	return ((sa_share_t)node);
}

/*
 * sa_get_next_share(share)
 *	Return the next share following the specified share
 *	from the internal list of shares. Returns NULL if there
 *	are no more shares.  The list is relative to the same
 *	group.
 */
sa_share_t
sa_get_next_share(sa_share_t share)
{
	xmlNodePtr node = NULL;

	if (share != NULL) {
		for (node = ((xmlNodePtr)share)->next; node != NULL;
		    node = node->next) {
			if (xmlStrcmp(node->name, (xmlChar *)"share") == 0) {
				break;
			}
		}
	}
	return ((sa_share_t)node);
}

/*
 * _sa_get_child_node(node, type)
 *
 * find the child node of the specified node that has "type". This is
 * used to implement several internal functions.
 */

static xmlNodePtr
_sa_get_child_node(xmlNodePtr node, xmlChar *type)
{
	xmlNodePtr child;
	for (child = node->xmlChildrenNode; child != NULL;
	    child = child->next)
		if (xmlStrcmp(child->name, type) == 0)
			return (child);
	return ((xmlNodePtr)NULL);
}

/*
 *  find_share(group, path)
 *
 * Search all the shares in the specified group for one that has the
 * specified path.
 */

static sa_share_t
find_share(sa_group_t group, char *sharepath)
{
	sa_share_t share;
	char *path;

	for (share = sa_get_share(group, NULL); share != NULL;
	    share = sa_get_next_share(share)) {
		path = sa_get_share_attr(share, "path");
		if (path != NULL && strcmp(path, sharepath) == 0) {
			sa_free_attr_string(path);
			break;
		}
		if (path != NULL)
			sa_free_attr_string(path);
	}
	return (share);
}

/*
 * sa_get_sub_group(group)
 *
 * Get the first sub-group of group. The sa_get_next_group() function
 * can be used to get the rest. This is currently only used for ZFS
 * sub-groups but could be used to implement a more general mechanism.
 */

sa_group_t
sa_get_sub_group(sa_group_t group)
{
	return ((sa_group_t)_sa_get_child_node((xmlNodePtr)group,
	    (xmlChar *)"group"));
}

/*
 * sa_find_share(sharepath)
 *	Finds a share regardless of group.  In the future, this
 *	function should utilize a cache and hash table of some kind.
 *	The current assumption is that a path will only be shared
 *	once.  In the future, this may change as implementation of
 *	resource names comes into being.
 */
sa_share_t
sa_find_share(sa_handle_t handle, char *sharepath)
{
	sa_group_t group;
	sa_group_t zgroup;
	sa_share_t share = NULL;
	int done = 0;

	for (group = sa_get_group(handle, NULL); group != NULL && !done;
	    group = sa_get_next_group(group)) {
		if (is_zfs_group(group)) {
			for (zgroup =
			    (sa_group_t)_sa_get_child_node((xmlNodePtr)group,
			    (xmlChar *)"group");
			    zgroup != NULL;
			    zgroup = sa_get_next_group(zgroup)) {
				share = find_share(zgroup, sharepath);
				if (share != NULL)
					break;
			}
		} else {
			share = find_share(group, sharepath);
		}
		if (share != NULL)
			break;
	}
	return (share);
}

/*
 *  sa_check_path(group, path, strictness)
 *
 * check that path is a valid path relative to the group.  Currently,
 * we are ignoring the group and checking only the NFS rules. Later,
 * we may want to use the group to then check against the protocols
 * enabled on the group. The strictness values mean:
 * SA_CHECK_NORMAL == only check newpath against shares that are active
 * SA_CHECK_STRICT == check newpath against both active shares and those
 *		      stored in the repository
 */

int
sa_check_path(sa_group_t group, char *path, int strictness)
{
	sa_handle_t handle;

	handle = sa_find_group_handle(group);
	return (validpath(handle, path, strictness));
}

/*
 * _sa_add_share(group, sharepath, persist, *error)
 *
 * common code for all types of add_share. sa_add_share() is the
 * public API, we also need to be able to do this when parsing legacy
 * files and construction of the internal configuration while
 * extracting config info from SMF.
 */

sa_share_t
_sa_add_share(sa_group_t group, char *sharepath, int persist, int *error)
{
	xmlNodePtr node = NULL;
	int err;

	err  = SA_OK; /* assume success */

	node = xmlNewChild((xmlNodePtr)group, NULL, (xmlChar *)"share", NULL);
	if (node != NULL) {
		xmlSetProp(node, (xmlChar *)"path", (xmlChar *)sharepath);
		xmlSetProp(node, (xmlChar *)"type",
		    persist ? (xmlChar *)"persist" : (xmlChar *)"transient");
		if (persist != SA_SHARE_TRANSIENT) {
			/*
			 * persistent shares come in two flavors: SMF and
			 * ZFS. Sort this one out based on target group and
			 * path type. Currently, only NFS is supported in the
			 * ZFS group and it is always on.
			 */
			if (sa_group_is_zfs(group) &&
			    sa_path_is_zfs(sharepath)) {
				err = sa_zfs_set_sharenfs(group, sharepath, 1);
			} else {
				sa_handle_impl_t impl_handle;
				impl_handle =
				    (sa_handle_impl_t)sa_find_group_handle(
				    group);
				if (impl_handle != NULL) {
					err = sa_commit_share(
					    impl_handle->scfhandle, group,
					    (sa_share_t)node);
				} else {
					err = SA_SYSTEM_ERR;
				}
			}
		}
		if (err == SA_NO_PERMISSION && persist & SA_SHARE_PARSER) {
			/* called by the dfstab parser so could be a show */
			err = SA_OK;
		}
		if (err != SA_OK) {
			/*
			 * we couldn't commit to the repository so undo
			 * our internal state to reflect reality.
			 */
			xmlUnlinkNode(node);
			xmlFreeNode(node);
			node = NULL;
		}
	} else {
		err = SA_NO_MEMORY;
	}
	if (error != NULL)
		*error = err;
	return (node);
}

/*
 * sa_add_share(group, sharepath, persist, *error)
 *
 *	Add a new share object to the specified group.  The share will
 *	have the specified sharepath and will only be constructed if
 *	it is a valid path to be shared.  NULL is returned on error
 *	and a detailed error value will be returned via the error
 *	pointer.
 */
sa_share_t
sa_add_share(sa_group_t group, char *sharepath, int persist, int *error)
{
	xmlNodePtr node = NULL;
	sa_share_t dup;
	int strictness = SA_CHECK_NORMAL;
	sa_handle_t handle;

	/*
	 * If the share is to be permanent, use strict checking so a
	 * bad config doesn't get created. Transient shares only need
	 * to check against the currently active
	 * shares. SA_SHARE_PARSER is a modifier used internally to
	 * indicate that we are being called by the dfstab parser and
	 * that we need strict checking in all cases. Normally persist
	 * is in integer value but SA_SHARE_PARSER may be or'd into
	 * it as an override.
	 */
	if (persist & SA_SHARE_PARSER || persist == SA_SHARE_PERMANENT)
		strictness = SA_CHECK_STRICT;

	handle = sa_find_group_handle(group);

	if ((dup = sa_find_share(handle, sharepath)) == NULL &&
	    (*error = sa_check_path(group, sharepath, strictness)) == SA_OK) {
		node = _sa_add_share(group, sharepath, persist, error);
	}
	if (dup != NULL)
		*error = SA_DUPLICATE_NAME;

	return ((sa_share_t)node);
}

/*
 * sa_enable_share(share, protocol)
 *	Enable the specified share to the specified protocol.
 *	If protocol is NULL, then all protocols.
 */
int
sa_enable_share(sa_share_t share, char *protocol)
{
	char *sharepath;
	struct stat st;
	int err = 0;

	sharepath = sa_get_share_attr(share, "path");
	if (stat(sharepath, &st) < 0) {
		err = SA_NO_SUCH_PATH;
	} else {
		/* tell the server about the share */
		if (protocol != NULL) {
			/* lookup protocol specific handler */
			err = sa_proto_share(protocol, share);
			if (err == SA_OK)
				(void) sa_set_share_attr(share, "shared",
				    "true");
		} else {
			/*
			 * Tell all protocols.  Only NFS for now but
			 * SMB is coming.
			 */
			err = sa_proto_share("nfs", share);
			(void) sa_set_share_attr(share, "shared", "true");
		}
	}
	if (sharepath != NULL)
		sa_free_attr_string(sharepath);
	return (err);
}

/*
 * sa_disable_share(share, protocol)
 *	Disable the specified share to the specified protocol.
 *	If protocol is NULL, then all protocols.
 */
int
sa_disable_share(sa_share_t share, char *protocol)
{
	char *path;
	char *shared;
	int ret = SA_OK;

	path = sa_get_share_attr(share, "path");
	shared = sa_get_share_attr(share, "shared");

	if (protocol != NULL) {
		ret = sa_proto_unshare(protocol, path);
	} else {
		/* need to do all protocols */
		ret = sa_proto_unshare("nfs", path);
	}
	if (ret == SA_OK)
		(void) sa_set_share_attr(share, "shared", NULL);
	if (path != NULL)
		sa_free_attr_string(path);
	if (shared != NULL)
		sa_free_attr_string(shared);
	return (ret);
}

/*
 * sa_remove_share(share)
 *
 * remove the specified share from its containing group.
 * Remove from the SMF or ZFS configuration space.
 */

int
sa_remove_share(sa_share_t share)
{
	sa_group_t group;
	int ret = SA_OK;
	char *type;
	int transient = 0;
	char *groupname;
	char *zfs;

	type = sa_get_share_attr(share, "type");
	group = sa_get_parent_group(share);
	zfs = sa_get_group_attr(group, "zfs");
	groupname = sa_get_group_attr(group, "name");
	if (type != NULL && strcmp(type, "persist") != 0)
		transient = 1;
	if (type != NULL)
		sa_free_attr_string(type);

	/* remove the node from its group then free the memory */

	/*
	 * need to test if "busy"
	 */
	/* only do SMF action if permanent */
	if (!transient || zfs != NULL) {
		/* remove from legacy dfstab as well as possible SMF */
		ret = sa_delete_legacy(share);
		if (ret == SA_OK) {
			if (!sa_group_is_zfs(group)) {
				sa_handle_impl_t impl_handle;
				impl_handle = (sa_handle_impl_t)
				    sa_find_group_handle(group);
				if (impl_handle != NULL) {
					ret = sa_delete_share(
					    impl_handle->scfhandle, group,
					    share);
				} else {
					ret = SA_SYSTEM_ERR;
				}
			} else {
				char *sharepath = sa_get_share_attr(share,
				    "path");
				if (sharepath != NULL) {
					ret = sa_zfs_set_sharenfs(group,
					    sharepath, 0);
					sa_free_attr_string(sharepath);
				}
			}
		}
	}
	if (groupname != NULL)
		sa_free_attr_string(groupname);
	if (zfs != NULL)
		sa_free_attr_string(zfs);

	xmlUnlinkNode((xmlNodePtr)share);
	xmlFreeNode((xmlNodePtr)share);
	return (ret);
}

/*
 * sa_move_share(group, share)
 *
 * move the specified share to the specified group.  Update SMF
 * appropriately.
 */

int
sa_move_share(sa_group_t group, sa_share_t share)
{
	sa_group_t oldgroup;
	int ret = SA_OK;

	/* remove the node from its group then free the memory */

	oldgroup = sa_get_parent_group(share);
	if (oldgroup != group) {
		sa_handle_impl_t impl_handle;
		xmlUnlinkNode((xmlNodePtr)share);
		/*
		 * now that the share isn't in its old group, add to
		 * the new one
		 */
		xmlAddChild((xmlNodePtr)group, (xmlNodePtr)share);
		/* need to deal with SMF */
		impl_handle = (sa_handle_impl_t)sa_find_group_handle(group);
		if (impl_handle != NULL) {
			/*
			 * need to remove from old group first and then add to
			 * new group. Ideally, we would do the other order but
			 * need to avoid having the share in two groups at the
			 * same time.
			 */
			ret = sa_delete_share(impl_handle->scfhandle, oldgroup,
			    share);
			if (ret == SA_OK)
				ret = sa_commit_share(impl_handle->scfhandle,
				    group, share);
		} else {
			ret = SA_SYSTEM_ERR;
		}
	}
	return (ret);
}

/*
 * sa_get_parent_group(share)
 *
 * Return the containg group for the share. If a group was actually
 * passed in, we don't want a parent so return NULL.
 */

sa_group_t
sa_get_parent_group(sa_share_t share)
{
	xmlNodePtr node = NULL;
	if (share != NULL) {
		node = ((xmlNodePtr)share)->parent;
		/*
		 * make sure parent is a group and not sharecfg since
		 * we may be cheating and passing in a group.
		 * Eventually, groups of groups might come into being.
		 */
		if (node == NULL ||
		    xmlStrcmp(node->name, (xmlChar *)"sharecfg") == 0)
			node = NULL;
	}
	return ((sa_group_t)node);
}

/*
 * _sa_create_group(impl_handle, groupname)
 *
 * Create a group in the document. The caller will need to deal with
 * configuration store and activation.
 */

sa_group_t
_sa_create_group(sa_handle_impl_t impl_handle, char *groupname)
{
	xmlNodePtr node = NULL;

	if (sa_valid_group_name(groupname)) {
		node = xmlNewChild(impl_handle->tree, NULL, (xmlChar *)"group",
		    NULL);
		if (node != NULL) {
			xmlSetProp(node, (xmlChar *)"name",
			    (xmlChar *)groupname);
			xmlSetProp(node, (xmlChar *)"state",
			    (xmlChar *)"enabled");
		}
	}
	return ((sa_group_t)node);
}

/*
 * _sa_create_zfs_group(group, groupname)
 *
 * Create a ZFS subgroup under the specified group. This may
 * eventually form the basis of general sub-groups, but is currently
 * restricted to ZFS.
 */
sa_group_t
_sa_create_zfs_group(sa_group_t group, char *groupname)
{
	xmlNodePtr node = NULL;

	node = xmlNewChild((xmlNodePtr)group, NULL, (xmlChar *)"group", NULL);
	if (node != NULL) {
		xmlSetProp(node, (xmlChar *)"name", (xmlChar *)groupname);
		xmlSetProp(node, (xmlChar *)"state", (xmlChar *)"enabled");
	}

	return ((sa_group_t)node);
}

/*
 * sa_create_group(groupname, *error)
 *
 * Create a new group with groupname.  Need to validate that it is a
 * legal name for SMF and the construct the SMF service instance of
 * svc:/network/shares/group to implement the group. All necessary
 * operational properties must be added to the group at this point
 * (via the SMF transaction model).
 */
sa_group_t
sa_create_group(sa_handle_t handle, char *groupname, int *error)
{
	xmlNodePtr node = NULL;
	sa_group_t group;
	int ret;
	char rbacstr[SA_STRSIZE];
	sa_handle_impl_t impl_handle = (sa_handle_impl_t)handle;

	ret = SA_OK;

	if (impl_handle == NULL || impl_handle->scfhandle == NULL) {
		ret = SA_SYSTEM_ERR;
		goto err;
	}

	group = sa_get_group(handle, groupname);
	if (group != NULL) {
		ret = SA_DUPLICATE_NAME;
	} else {
		if (sa_valid_group_name(groupname)) {
			node = xmlNewChild(impl_handle->tree, NULL,
			    (xmlChar *)"group", NULL);
			if (node != NULL) {
				xmlSetProp(node, (xmlChar *)"name",
				    (xmlChar *)groupname);
				/* default to the group being enabled */
				xmlSetProp(node, (xmlChar *)"state",
				    (xmlChar *)"enabled");
				ret = sa_create_instance(impl_handle->scfhandle,
				    groupname);
				if (ret == SA_OK) {
					ret = sa_start_transaction(
					    impl_handle->scfhandle,
					    "operation");
				}
				if (ret == SA_OK) {
					ret = sa_set_property(
					    impl_handle->scfhandle,
					    "state", "enabled");
					if (ret == SA_OK) {
						ret = sa_end_transaction(
						    impl_handle->scfhandle);
					} else {
						sa_abort_transaction(
						    impl_handle->scfhandle);
					}
				}
				if (ret == SA_OK) {
					/* initialize the RBAC strings */
					ret = sa_start_transaction(
					    impl_handle->scfhandle,
					    "general");
					if (ret == SA_OK) {
						(void) snprintf(rbacstr,
						    sizeof (rbacstr), "%s.%s",
						    SA_RBAC_MANAGE, groupname);
						ret = sa_set_property(
						    impl_handle->scfhandle,
						    "action_authorization",
						    rbacstr);
					}
					if (ret == SA_OK) {
						(void) snprintf(rbacstr,
						    sizeof (rbacstr), "%s.%s",
						    SA_RBAC_VALUE, groupname);
						ret = sa_set_property(
						    impl_handle->scfhandle,
						    "value_authorization",
						    rbacstr);
					}
					if (ret == SA_OK) {
						ret = sa_end_transaction(
						    impl_handle->scfhandle);
					} else {
						sa_abort_transaction(
						    impl_handle->scfhandle);
					}
				}
				if (ret != SA_OK) {
					/*
					 * Couldn't commit the group
					 * so we need to undo
					 * internally.
					 */
					xmlUnlinkNode(node);
					xmlFreeNode(node);
					node = NULL;
				}
			} else {
				ret = SA_NO_MEMORY;
			}
		} else {
			ret = SA_INVALID_NAME;
		}
	}
err:
	if (error != NULL)
		*error = ret;
	return ((sa_group_t)node);
}

/*
 * sa_remove_group(group)
 *
 * Remove the specified group. This deletes from the SMF repository.
 * All property groups and properties are removed.
 */

int
sa_remove_group(sa_group_t group)
{
	char *name;
	int ret = SA_OK;
	sa_handle_impl_t impl_handle;

	impl_handle = (sa_handle_impl_t)sa_find_group_handle(group);
	if (impl_handle != NULL) {
		name = sa_get_group_attr(group, "name");
		if (name != NULL) {
			ret = sa_delete_instance(impl_handle->scfhandle, name);
			sa_free_attr_string(name);
		}
		xmlUnlinkNode((xmlNodePtr)group); /* make sure unlinked */
		xmlFreeNode((xmlNodePtr)group);   /* now it is gone */
	} else {
		ret = SA_SYSTEM_ERR;
	}
	return (ret);
}

/*
 * sa_update_config()
 *
 * Used to update legacy files that need to be updated in bulk
 * Currently, this is a placeholder and will go away in a future
 * release.
 */

int
sa_update_config(sa_handle_t handle)
{
	/*
	 * do legacy files first so we can tell when they change.
	 * This will go away when we start updating individual records
	 * rather than the whole file.
	 */
	update_legacy_config(handle);
	return (SA_OK);
}

/*
 * get_node_attr(node, tag)
 *
 * Get the speficied tag(attribute) if it exists on the node.  This is
 * used internally by a number of attribute oriented functions.
 */

static char *
get_node_attr(void *nodehdl, char *tag)
{
	xmlNodePtr node = (xmlNodePtr)nodehdl;
	xmlChar *name = NULL;

	if (node != NULL)
		name = xmlGetProp(node, (xmlChar *)tag);
	return ((char *)name);
}

/*
 * get_node_attr(node, tag)
 *
 * Set the speficied tag(attribute) to the specified value This is
 * used internally by a number of attribute oriented functions. It
 * doesn't update the repository, only the internal document state.
 */

void
set_node_attr(void *nodehdl, char *tag, char *value)
{
	xmlNodePtr node = (xmlNodePtr)nodehdl;
	if (node != NULL && tag != NULL) {
		if (value != NULL)
			xmlSetProp(node, (xmlChar *)tag, (xmlChar *)value);
		else
			xmlUnsetProp(node, (xmlChar *)tag);
	}
}

/*
 * sa_get_group_attr(group, tag)
 *
 * Get the specied attribute, if defined, for the group.
 */

char *
sa_get_group_attr(sa_group_t group, char *tag)
{
	return (get_node_attr((void *)group, tag));
}

/*
 * sa_set_group_attr(group, tag, value)
 *
 * set the specified tag/attribute on the group using value as its
 * value.
 *
 * This will result in setting the property in the SMF repository as
 * well as in the internal document.
 */

int
sa_set_group_attr(sa_group_t group, char *tag, char *value)
{
	int ret;
	char *groupname;
	sa_handle_impl_t impl_handle;

	impl_handle = (sa_handle_impl_t)sa_find_group_handle(group);
	if (impl_handle != NULL) {
		groupname = sa_get_group_attr(group, "name");
		ret = sa_get_instance(impl_handle->scfhandle, groupname);
		if (ret == SA_OK) {
			set_node_attr((void *)group, tag, value);
			ret = sa_start_transaction(impl_handle->scfhandle,
			    "operation");
			if (ret == SA_OK) {
				ret = sa_set_property(impl_handle->scfhandle,
				    tag, value);
				if (ret == SA_OK)
					(void) sa_end_transaction(
					    impl_handle->scfhandle);
				else
					sa_abort_transaction(
					    impl_handle->scfhandle);
			}
		}
		if (groupname != NULL)
			sa_free_attr_string(groupname);
	} else {
		ret = SA_SYSTEM_ERR;
	}
	return (ret);
}

/*
 * sa_get_share_attr(share, tag)
 *
 * Return the value of the tag/attribute set on the specified
 * share. Returns NULL if the tag doesn't exist.
 */

char *
sa_get_share_attr(sa_share_t share, char *tag)
{
	return (get_node_attr((void *)share, tag));
}

/*
 * sa_get_resource(group, resource)
 *
 * Search all the shares in the speified group for a share with a
 * resource name matching the one specified.
 *
 * In the future, it may be advantageous to allow group to be NULL and
 * search all groups but that isn't needed at present.
 */

sa_share_t
sa_get_resource(sa_group_t group, char *resource)
{
	sa_share_t share = NULL;
	char *name = NULL;

	if (resource != NULL) {
		for (share = sa_get_share(group, NULL); share != NULL;
		    share = sa_get_next_share(share)) {
			name = sa_get_share_attr(share, "resource");
			if (name != NULL) {
				if (strcmp(name, resource) == 0)
					break;
				sa_free_attr_string(name);
				name = NULL;
			}
		}
		if (name != NULL)
			sa_free_attr_string(name);
	}
	return ((sa_share_t)share);
}

/*
 * _sa_set_share_description(share, description)
 *
 * Add a description tag with text contents to the specified share.
 * A separate XML tag is used rather than a property.
 */

xmlNodePtr
_sa_set_share_description(sa_share_t share, char *content)
{
	xmlNodePtr node;
	node = xmlNewChild((xmlNodePtr)share, NULL, (xmlChar *)"description",
	    NULL);
	xmlNodeSetContent(node, (xmlChar *)content);
	return (node);
}

/*
 * sa_set_share_attr(share, tag, value)
 *
 * Set the share attribute specified by tag to the specified value. In
 * the case of "resource", enforce a no duplicates in a group rule. If
 * the share is not transient, commit the changes to the repository
 * else just update the share internally.
 */

int
sa_set_share_attr(sa_share_t share, char *tag, char *value)
{
	sa_group_t group;
	sa_share_t resource;
	int ret = SA_OK;

	group = sa_get_parent_group(share);

	/*
	 * There are some attributes that may have specific
	 * restrictions on them. Initially, only "resource" has
	 * special meaning that needs to be checked. Only one instance
	 * of a resource name may exist within a group.
	 */

	if (strcmp(tag, "resource") == 0) {
		resource = sa_get_resource(group, value);
		if (resource != share && resource != NULL)
			ret = SA_DUPLICATE_NAME;
	}
	if (ret == SA_OK) {
		set_node_attr((void *)share, tag, value);
		if (group != NULL) {
			char *type;
			/* we can probably optimize this some */
			type = sa_get_share_attr(share, "type");
			if (type == NULL || strcmp(type, "transient") != 0) {
				sa_handle_impl_t impl_handle;
				impl_handle =
				    (sa_handle_impl_t)sa_find_group_handle(
				    group);
				if (impl_handle != NULL) {
					ret = sa_commit_share(
					    impl_handle->scfhandle, group,
					    share);
				} else {
					ret = SA_SYSTEM_ERR;
				}
			}
			if (type != NULL)
				sa_free_attr_string(type);
		}
	}
	return (ret);
}

/*
 * sa_get_property_attr(prop, tag)
 *
 * Get the value of the specified property attribute. Standard
 * attributes are "type" and "value".
 */

char *
sa_get_property_attr(sa_property_t prop, char *tag)
{
	return (get_node_attr((void *)prop, tag));
}

/*
 * sa_get_optionset_attr(prop, tag)
 *
 * Get the value of the specified property attribute. Standard
 * attribute is "type".
 */

char *
sa_get_optionset_attr(sa_property_t optionset, char *tag)
{
	return (get_node_attr((void *)optionset, tag));

}

/*
 * sa_set_optionset_attr(optionset, tag, value)
 *
 * Set the specified attribute(tag) to the specified value on the
 * optionset.
 */

void
sa_set_optionset_attr(sa_group_t optionset, char *tag, char *value)
{
	set_node_attr((void *)optionset, tag, value);
}

/*
 * sa_free_attr_string(string)
 *
 * Free the string that was returned in one of the sa_get_*_attr()
 * functions.
 */

void
sa_free_attr_string(char *string)
{
	xmlFree((xmlChar *)string);
}

/*
 * sa_get_optionset(group, proto)
 *
 * Return the optionset, if it exists, that is associated with the
 * specified protocol.
 */

sa_optionset_t
sa_get_optionset(void *group, char *proto)
{
	xmlNodePtr node;
	xmlChar *value = NULL;

	for (node = ((xmlNodePtr)group)->children; node != NULL;
	    node = node->next) {
		if (xmlStrcmp(node->name, (xmlChar *)"optionset") == 0) {
			value = xmlGetProp(node, (xmlChar *)"type");
			if (proto != NULL) {
				if (value != NULL &&
				    xmlStrcmp(value, (xmlChar *)proto) == 0) {
					break;
				}
				if (value != NULL) {
					xmlFree(value);
					value = NULL;
				}
			} else {
				break;
			}
		}
	}
	if (value != NULL)
		xmlFree(value);
	return ((sa_optionset_t)node);
}

/*
 * sa_get_next_optionset(optionset)
 *
 * Return the next optionset in the group. NULL if this was the last.
 */

sa_optionset_t
sa_get_next_optionset(sa_optionset_t optionset)
{
	xmlNodePtr node;

	for (node = ((xmlNodePtr)optionset)->next; node != NULL;
	    node = node->next) {
		if (xmlStrcmp(node->name, (xmlChar *)"optionset") == 0) {
			break;
		}
	}
	return ((sa_optionset_t)node);
}

/*
 * sa_get_security(group, sectype, proto)
 *
 * Return the security optionset. The internal name is a hold over
 * from the implementation and will be changed before the API is
 * finalized. This is really a named optionset that can be negotiated
 * as a group of properties (like NFS security options).
 */

sa_security_t
sa_get_security(sa_group_t group, char *sectype, char *proto)
{
	xmlNodePtr node;
	xmlChar *value = NULL;

	for (node = ((xmlNodePtr)group)->children; node != NULL;
	    node = node->next) {
		if (xmlStrcmp(node->name, (xmlChar *)"security") == 0) {
			if (proto != NULL) {
				value = xmlGetProp(node, (xmlChar *)"type");
				if (value == NULL ||
				    (value != NULL &&
				    xmlStrcmp(value, (xmlChar *)proto) != 0)) {
					/* it doesn't match so continue */
					xmlFree(value);
					value = NULL;
					continue;
				}
			}
			if (value != NULL) {
				xmlFree(value);
				value = NULL;
			}
			/* potential match */
			if (sectype != NULL) {
				value = xmlGetProp(node, (xmlChar *)"sectype");
				if (value != NULL &&
				    xmlStrcmp(value, (xmlChar *)sectype) == 0) {
					break;
				}
			} else {
				break;
			}
		}
		if (value != NULL) {
			xmlFree(value);
			value = NULL;
		}
	}
	if (value != NULL)
		xmlFree(value);
	return ((sa_security_t)node);
}

/*
 * sa_get_next_security(security)
 *
 * Get the next security optionset if one exists.
 */

sa_security_t
sa_get_next_security(sa_security_t security)
{
	xmlNodePtr node;

	for (node = ((xmlNodePtr)security)->next; node != NULL;
	    node = node->next) {
		if (xmlStrcmp(node->name, (xmlChar *)"security") == 0) {
			break;
		}
	}
	return ((sa_security_t)node);
}

/*
 * sa_get_property(optionset, prop)
 *
 * Get the property object with the name specified in prop from the
 * optionset.
 */

sa_property_t
sa_get_property(sa_optionset_t optionset, char *prop)
{
	xmlNodePtr node = (xmlNodePtr)optionset;
	xmlChar *value = NULL;

	if (optionset == NULL)
		return (NULL);

	for (node = node->children; node != NULL;
	    node = node->next) {
		if (xmlStrcmp(node->name, (xmlChar *)"option") == 0) {
			if (prop == NULL)
				break;
			value = xmlGetProp(node, (xmlChar *)"type");
			if (value != NULL &&
			    xmlStrcmp(value, (xmlChar *)prop) == 0) {
				break;
			}
			if (value != NULL) {
				xmlFree(value);
				value = NULL;
			}
		}
	}
	if (value != NULL)
		xmlFree(value);
	if (node != NULL && xmlStrcmp(node->name, (xmlChar *)"option") != 0) {
		/*
		 * avoid a non option node -- it is possible to be a
		 * text node
		 */
		node = NULL;
	}
	return ((sa_property_t)node);
}

/*
 * sa_get_next_property(property)
 *
 * Get the next property following the specified property. NULL if
 * this was the last.
 */

sa_property_t
sa_get_next_property(sa_property_t property)
{
	xmlNodePtr node;

	for (node = ((xmlNodePtr)property)->next; node != NULL;
	    node = node->next) {
		if (xmlStrcmp(node->name, (xmlChar *)"option") == 0) {
			break;
		}
	}
	return ((sa_property_t)node);
}

/*
 * sa_set_share_description(share, content)
 *
 * Set the description of share to content.
 */

int
sa_set_share_description(sa_share_t share, char *content)
{
	xmlNodePtr node;
	sa_group_t group;
	int ret = SA_OK;

	for (node = ((xmlNodePtr)share)->children; node != NULL;
	    node = node->next) {
		if (xmlStrcmp(node->name, (xmlChar *)"description") == 0) {
			break;
		}
	}
	group = sa_get_parent_group(share);
	/* no existing description but want to add */
	if (node == NULL && content != NULL) {
		/* add a description */
		node = _sa_set_share_description(share, content);
	} else if (node != NULL && content != NULL) {
		/* update a description */
		xmlNodeSetContent(node, (xmlChar *)content);
	} else if (node != NULL && content == NULL) {
		/* remove an existing description */
		xmlUnlinkNode(node);
		xmlFreeNode(node);
	}
	if (group != NULL && is_persistent((sa_group_t)share)) {
		sa_handle_impl_t impl_handle;
		impl_handle = (sa_handle_impl_t)sa_find_group_handle(group);
		if (impl_handle != NULL) {
			ret = sa_commit_share(impl_handle->scfhandle, group,
			    share);
		} else {
			ret = SA_SYSTEM_ERR;
		}
	}
	return (ret);
}

/*
 * fixproblemchars(string)
 *
 * don't want any newline or tab characters in the text since these
 * could break display of data and legacy file formats.
 */
static void
fixproblemchars(char *str)
{
	int c;
	for (c = *str; c != '\0'; c = *++str) {
		if (c == '\t' || c == '\n')
			*str = ' ';
		else if (c == '"')
			*str = '\'';
	}
}

/*
 * sa_get_share_description(share)
 *
 * Return the description text for the specified share if it
 * exists. NULL if no description exists.
 */

char *
sa_get_share_description(sa_share_t share)
{
	xmlChar *description = NULL;
	xmlNodePtr node;

	for (node = ((xmlNodePtr)share)->children; node != NULL;
	    node = node->next) {
		if (xmlStrcmp(node->name, (xmlChar *)"description") == 0) {
			break;
		}
	}
	if (node != NULL) {
		description = xmlNodeGetContent((xmlNodePtr)share);
		fixproblemchars((char *)description);
	}
	return ((char *)description);
}

/*
 * sa_free(share_description(description)
 *
 * Free the description string.
 */

void
sa_free_share_description(char *description)
{
	xmlFree((xmlChar *)description);
}

/*
 * sa_create_optionset(group, proto)
 *
 * Create an optionset for the specified protocol in the specied
 * group. This is manifested as a property group within SMF.
 */

sa_optionset_t
sa_create_optionset(sa_group_t group, char *proto)
{
	sa_optionset_t optionset;
	sa_group_t parent = group;

	optionset = sa_get_optionset(group, proto);
	if (optionset != NULL) {
		/* can't have a duplicate protocol */
		optionset = NULL;
	} else {
		optionset = (sa_optionset_t)xmlNewChild((xmlNodePtr)group,
		    NULL, (xmlChar *)"optionset", NULL);
		/*
		 * only put to repository if on a group and we were
		 * able to create an optionset.
		 */
		if (optionset != NULL) {
			char oname[SA_STRSIZE];
			char *groupname;
			char *id = NULL;

			if (sa_is_share(group))
				parent = sa_get_parent_group((sa_share_t)group);

			sa_set_optionset_attr(optionset, "type", proto);

			if (sa_is_share(group)) {
				id = sa_get_share_attr((sa_share_t)group, "id");
			}
			(void) sa_optionset_name(optionset, oname,
			    sizeof (oname), id);
			groupname = sa_get_group_attr(parent, "name");
			if (groupname != NULL && is_persistent(group)) {
				sa_handle_impl_t impl_handle;
				impl_handle = (sa_handle_impl_t)
				    sa_find_group_handle(group);
				assert(impl_handle != NULL);
				if (impl_handle != NULL) {
					(void) sa_get_instance(
					    impl_handle->scfhandle,
					    groupname);
					(void) sa_create_pgroup(
					    impl_handle->scfhandle, oname);
				}
			}
			if (groupname != NULL)
				sa_free_attr_string(groupname);
			if (id != NULL)
				sa_free_attr_string(id);
		}
	}
	return (optionset);
}

/*
 * sa_get_property_parent(property)
 *
 * Given a property, return the object it is a property of. This will
 * be an optionset of some type.
 */

static sa_optionset_t
sa_get_property_parent(sa_property_t property)
{
	xmlNodePtr node = NULL;

	if (property != NULL)
		node = ((xmlNodePtr)property)->parent;
	return ((sa_optionset_t)node);
}

/*
 * sa_get_optionset_parent(optionset)
 *
 * Return the parent of the specified optionset. This could be a group
 * or a share.
 */

static sa_group_t
sa_get_optionset_parent(sa_optionset_t optionset)
{
	xmlNodePtr node = NULL;

	if (optionset != NULL)
		node = ((xmlNodePtr)optionset)->parent;
	return ((sa_group_t)node);
}

/*
 * zfs_needs_update(share)
 *
 * In order to avoid making multiple updates to a ZFS share when
 * setting properties, the share attribute "changed" will be set to
 * true when a property is added or modifed.  When done adding
 * properties, we can then detect that an update is needed.  We then
 * clear the state here to detect additional changes.
 */

static int
zfs_needs_update(sa_share_t share)
{
	char *attr;
	int result = 0;

	attr = sa_get_share_attr(share, "changed");
	if (attr != NULL) {
		sa_free_attr_string(attr);
		result = 1;
	}
	set_node_attr((void *)share, "changed", NULL);
	return (result);
}

/*
 * zfs_set_update(share)
 *
 * Set the changed attribute of the share to true.
 */

static void
zfs_set_update(sa_share_t share)
{
	set_node_attr((void *)share, "changed", "true");
}

/*
 * sa_commit_properties(optionset, clear)
 *
 * Check if SMF or ZFS config and either update or abort the pending
 * changes.
 */

int
sa_commit_properties(sa_optionset_t optionset, int clear)
{
	sa_group_t group;
	sa_group_t parent;
	int zfs = 0;
	int needsupdate = 0;
	int ret = SA_OK;
	sa_handle_impl_t impl_handle;

	group = sa_get_optionset_parent(optionset);
	if (group != NULL && (sa_is_share(group) || is_zfs_group(group))) {
		/* only update ZFS if on a share */
		parent = sa_get_parent_group(group);
		zfs++;
		if (parent != NULL && is_zfs_group(parent))
			needsupdate = zfs_needs_update(group);
		else
			zfs = 0;
	}
	if (zfs) {
		if (!clear && needsupdate)
			ret = sa_zfs_update((sa_share_t)group);
	} else {
		impl_handle = (sa_handle_impl_t)sa_find_group_handle(group);
		if (impl_handle != NULL) {
			if (clear) {
				(void) sa_abort_transaction(
				    impl_handle->scfhandle);
			} else {
				ret = sa_end_transaction(
				    impl_handle->scfhandle);
			}
		} else {
			ret = SA_SYSTEM_ERR;
		}
	}
	return (ret);
}

/*
 * sa_destroy_optionset(optionset)
 *
 * Remove the optionset from its group. Update the repostory to
 * reflect this change.
 */

int
sa_destroy_optionset(sa_optionset_t optionset)
{
	char name[SA_STRSIZE];
	int len;
	int ret;
	char *id = NULL;
	sa_group_t group;
	int ispersist = 1;

	/* now delete the prop group */
	group = sa_get_optionset_parent(optionset);
	if (group != NULL && sa_is_share(group)) {
		ispersist = is_persistent(group);
		id = sa_get_share_attr((sa_share_t)group, "id");
	}
	if (ispersist) {
		sa_handle_impl_t impl_handle;
		len = sa_optionset_name(optionset, name, sizeof (name), id);
		impl_handle = (sa_handle_impl_t)sa_find_group_handle(group);
		if (impl_handle != NULL) {
			if (len > 0) {
				ret = sa_delete_pgroup(impl_handle->scfhandle,
				    name);
			}
		} else {
			ret = SA_SYSTEM_ERR;
		}
	}
	xmlUnlinkNode((xmlNodePtr)optionset);
	xmlFreeNode((xmlNodePtr)optionset);
	if (id != NULL)
		sa_free_attr_string(id);
	return (ret);
}

/* private to the implementation */
int
_sa_remove_optionset(sa_optionset_t optionset)
{
	int ret = SA_OK;

	xmlUnlinkNode((xmlNodePtr)optionset);
	xmlFreeNode((xmlNodePtr)optionset);
	return (ret);
}

/*
 * sa_create_security(group, sectype, proto)
 *
 * Create a security optionset (one that has a type name and a
 * proto). Security is left over from a pure NFS implementation. The
 * naming will change in the future when the API is released.
 */
sa_security_t
sa_create_security(sa_group_t group, char *sectype, char *proto)
{
	sa_security_t security;
	char *id = NULL;
	sa_group_t parent;
	char *groupname = NULL;

	if (group != NULL && sa_is_share(group)) {
		id = sa_get_share_attr((sa_share_t)group, "id");
		parent = sa_get_parent_group(group);
		if (parent != NULL)
			groupname = sa_get_group_attr(parent, "name");
	} else if (group != NULL) {
		groupname = sa_get_group_attr(group, "name");
	}

	security = sa_get_security(group, sectype, proto);
	if (security != NULL) {
		/* can't have a duplicate security option */
		security = NULL;
	} else {
		security = (sa_security_t)xmlNewChild((xmlNodePtr)group,
		    NULL, (xmlChar *)"security", NULL);
		if (security != NULL) {
			char oname[SA_STRSIZE];
			sa_set_security_attr(security, "type", proto);

			sa_set_security_attr(security, "sectype", sectype);
			(void) sa_security_name(security, oname,
			    sizeof (oname), id);
			if (groupname != NULL && is_persistent(group)) {
				sa_handle_impl_t impl_handle;
				impl_handle =
				    (sa_handle_impl_t)sa_find_group_handle(
				    group);
				if (impl_handle != NULL) {
					(void) sa_get_instance(
					    impl_handle->scfhandle, groupname);
					(void) sa_create_pgroup(
					    impl_handle->scfhandle, oname);
				}
			}
		}
	}
	if (groupname != NULL)
		sa_free_attr_string(groupname);
	return (security);
}

/*
 * sa_destroy_security(security)
 *
 * Remove the specified optionset from the document and the
 * configuration.
 */

int
sa_destroy_security(sa_security_t security)
{
	char name[SA_STRSIZE];
	int len;
	int ret = SA_OK;
	char *id = NULL;
	sa_group_t group;
	int iszfs = 0;
	int ispersist = 1;

	group = sa_get_optionset_parent(security);

	if (group != NULL)
		iszfs = sa_group_is_zfs(group);

	if (group != NULL && !iszfs) {
		if (sa_is_share(group))
			ispersist = is_persistent(group);
		id = sa_get_share_attr((sa_share_t)group, "id");
	}
	if (ispersist) {
		len = sa_security_name(security, name, sizeof (name), id);
		if (!iszfs && len > 0) {
			sa_handle_impl_t impl_handle;
			impl_handle =
			    (sa_handle_impl_t)sa_find_group_handle(group);
			if (impl_handle != NULL) {
				ret = sa_delete_pgroup(impl_handle->scfhandle,
				    name);
			} else {
				ret = SA_SYSTEM_ERR;
			}
		}
	}
	xmlUnlinkNode((xmlNodePtr)security);
	xmlFreeNode((xmlNodePtr)security);
	if (iszfs)
		ret = sa_zfs_update(group);
	if (id != NULL)
		sa_free_attr_string(id);
	return (ret);
}

/*
 * sa_get_security_attr(optionset, tag)
 *
 * Return the specified attribute value from the optionset.
 */

char *
sa_get_security_attr(sa_property_t optionset, char *tag)
{
	return (get_node_attr((void *)optionset, tag));

}

/*
 * sa_set_security_attr(optionset, tag, value)
 *
 * Set the optioset attribute specied by tag to the specified value.
 */

void
sa_set_security_attr(sa_group_t optionset, char *tag, char *value)
{
	set_node_attr((void *)optionset, tag, value);
}

/*
 * is_nodetype(node, type)
 *
 * Check to see if node is of the type specified.
 */

static int
is_nodetype(void *node, char *type)
{
	return (strcmp((char *)((xmlNodePtr)node)->name, type) == 0);
}


/*
 * add_or_update()
 *
 * Add or update a property. Pulled out of sa_set_prop_by_prop for
 * readability.
 */
static int
add_or_update(scfutilhandle_t *scf_handle, int type, scf_value_t *value,
    scf_transaction_entry_t *entry, char *name, char *valstr)
{
	int ret = SA_SYSTEM_ERR;

	if (value != NULL) {
		if (type == SA_PROP_OP_ADD)
			ret = scf_transaction_property_new(scf_handle->trans,
			    entry, name, SCF_TYPE_ASTRING);
		else
			ret = scf_transaction_property_change(scf_handle->trans,
			    entry, name, SCF_TYPE_ASTRING);
		if (ret == 0) {
			ret = scf_value_set_astring(value, valstr);
			if (ret == 0)
				ret = scf_entry_add_value(entry, value);
			if (ret == 0)
				return (ret);
			scf_value_destroy(value);
		} else {
			scf_entry_destroy(entry);
		}
	}
	return (SA_SYSTEM_ERR);
}

/*
 * sa_set_prop_by_prop(optionset, group, prop, type)
 *
 * Add/remove/update the specified property prop into the optionset or
 * share. If a share, sort out which property group based on GUID. In
 * all cases, the appropriate transaction is set (or ZFS share is
 * marked as needing an update)
 */

static int
sa_set_prop_by_prop(sa_optionset_t optionset, sa_group_t group,
			sa_property_t prop, int type)
{
	char *name;
	char *valstr;
	int ret = SA_OK;
	scf_transaction_entry_t *entry;
	scf_value_t *value;
	int opttype; /* 1 == optionset, 0 == security */
	char *id = NULL;
	int iszfs = 0;
	int isshare = 0;
	sa_group_t parent = NULL;
	sa_handle_impl_t impl_handle;
	scfutilhandle_t  *scf_handle;

	if (!is_persistent(group)) {
		/*
		 * if the group/share is not persistent we don't need
		 * to do anything here
		 */
		return (SA_OK);
	}
	impl_handle = (sa_handle_impl_t)sa_find_group_handle(group);
	if (impl_handle == NULL || impl_handle->scfhandle == NULL)
		return (SA_SYSTEM_ERR);
	scf_handle = impl_handle->scfhandle;
	name = sa_get_property_attr(prop, "type");
	valstr = sa_get_property_attr(prop, "value");
	entry = scf_entry_create(scf_handle->handle);
	opttype = is_nodetype((void *)optionset, "optionset");

	if (valstr != NULL && entry != NULL) {
		if (sa_is_share(group)) {
			isshare = 1;
			parent = sa_get_parent_group(group);
			if (parent != NULL)
				iszfs = is_zfs_group(parent);
		} else {
			iszfs = is_zfs_group(group);
		}
		if (!iszfs) {
			if (scf_handle->trans == NULL) {
				char oname[SA_STRSIZE];
				char *groupname = NULL;
				if (isshare) {
					if (parent != NULL) {
						groupname =
						    sa_get_group_attr(parent,
						    "name");
					}
					id =
					    sa_get_share_attr((sa_share_t)group,
					    "id");
				} else {
					groupname = sa_get_group_attr(group,
					    "name");
				}
				if (groupname != NULL) {
					ret = sa_get_instance(scf_handle,
					    groupname);
					sa_free_attr_string(groupname);
				}
				if (opttype)
					(void) sa_optionset_name(optionset,
					    oname, sizeof (oname), id);
				else
					(void) sa_security_name(optionset,
					    oname, sizeof (oname), id);
				ret = sa_start_transaction(scf_handle, oname);
			}
			if (ret == SA_OK) {
				switch (type) {
				case SA_PROP_OP_REMOVE:
					ret = scf_transaction_property_delete(
					    scf_handle->trans, entry, name);
					break;
				case SA_PROP_OP_ADD:
				case SA_PROP_OP_UPDATE:
					value = scf_value_create(
					    scf_handle->handle);
					ret = add_or_update(scf_handle, type,
					    value, entry, name, valstr);
					break;
				}
			}
		} else {
			/*
			 * ZFS update. The calling function would have updated
			 * the internal XML structure. Just need to flag it as
			 * changed for ZFS.
			 */
			zfs_set_update((sa_share_t)group);
		}
	}

	if (name != NULL)
		sa_free_attr_string(name);
	if (valstr != NULL)
		sa_free_attr_string(valstr);
	else if (entry != NULL)
		scf_entry_destroy(entry);

	if (ret == -1)
		ret = SA_SYSTEM_ERR;

	return (ret);
}

/*
 * sa_create_property(name, value)
 *
 * Create a new property with the specified name and value.
 */

sa_property_t
sa_create_property(char *name, char *value)
{
	xmlNodePtr node;

	node = xmlNewNode(NULL, (xmlChar *)"option");
	if (node != NULL) {
		xmlSetProp(node, (xmlChar *)"type", (xmlChar *)name);
		xmlSetProp(node, (xmlChar *)"value", (xmlChar *)value);
	}
	return ((sa_property_t)node);
}

/*
 * sa_add_property(object, property)
 *
 * Add the specified property to the object. Issue the appropriate
 * transaction or mark a ZFS object as needing an update.
 */

int
sa_add_property(void *object, sa_property_t property)
{
	int ret = SA_OK;
	sa_group_t parent;
	sa_group_t group;
	char *proto;

	proto = sa_get_optionset_attr(object, "type");
	if (property != NULL) {
		if ((ret = sa_valid_property(object, proto, property)) ==
		    SA_OK) {
			property = (sa_property_t)xmlAddChild(
			    (xmlNodePtr)object, (xmlNodePtr)property);
		} else {
			if (proto != NULL)
				sa_free_attr_string(proto);
			return (ret);
		}
	}

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

	parent = sa_get_parent_group(object);
	if (!is_persistent(parent)) {
		return (ret);
	}

	if (sa_is_share(parent))
		group = sa_get_parent_group(parent);
	else
		group = parent;

	if (property == NULL) {
		ret = SA_NO_MEMORY;
	} else {
		char oname[SA_STRSIZE];

		if (!is_zfs_group(group)) {
			char *id = NULL;
			sa_handle_impl_t impl_handle;
			scfutilhandle_t  *scf_handle;

			impl_handle = (sa_handle_impl_t)sa_find_group_handle(
			    group);
			if (impl_handle == NULL ||
			    impl_handle->scfhandle == NULL)
				ret = SA_SYSTEM_ERR;
			if (ret == SA_OK) {
				scf_handle = impl_handle->scfhandle;
				if (sa_is_share((sa_group_t)parent)) {
					id = sa_get_share_attr(
					    (sa_share_t)parent, "id");
				}
				if (scf_handle->trans == NULL) {
					if (is_nodetype(object, "optionset")) {
						(void) sa_optionset_name(
						    (sa_optionset_t)object,
						    oname, sizeof (oname), id);
					} else {
						(void) sa_security_name(
						    (sa_optionset_t)object,
						    oname, sizeof (oname), id);
					}
					ret = sa_start_transaction(scf_handle,
					    oname);
				}
				if (ret == SA_OK) {
					char *name;
					char *value;
					name = sa_get_property_attr(property,
					    "type");
					value = sa_get_property_attr(property,
					    "value");
					if (name != NULL && value != NULL) {
						if (scf_handle->scf_state ==
						    SCH_STATE_INIT) {
							ret = sa_set_property(
							    scf_handle, name,
							    value);
						}
					} else {
						ret = SA_CONFIG_ERR;
					}
					if (name != NULL)
						sa_free_attr_string(
						    name);
					if (value != NULL)
						sa_free_attr_string(value);
				}
				if (id != NULL)
					sa_free_attr_string(id);
			}
		} else {
			/*
			 * ZFS is a special case. We do want
			 * to allow editing property/security
			 * lists since we can have a better
			 * syntax and we also want to keep
			 * things consistent when possible.
			 *
			 * Right now, we defer until the
			 * sa_commit_properties so we can get
			 * them all at once. We do need to
			 * mark the share as "changed"
			 */
			zfs_set_update((sa_share_t)parent);
		}
	}
	return (ret);
}

/*
 * sa_remove_property(property)
 *
 * Remove the specied property from its containing object. Update the
 * repository as appropriate.
 */

int
sa_remove_property(sa_property_t property)
{
	int ret = SA_OK;

	if (property != NULL) {
		sa_optionset_t optionset;
		sa_group_t group;
		optionset = sa_get_property_parent(property);
		if (optionset != NULL) {
			group = sa_get_optionset_parent(optionset);
			if (group != NULL) {
				ret = sa_set_prop_by_prop(optionset, group,
				    property, SA_PROP_OP_REMOVE);
			}
		}
		xmlUnlinkNode((xmlNodePtr)property);
		xmlFreeNode((xmlNodePtr)property);
	} else {
		ret = SA_NO_SUCH_PROP;
	}
	return (ret);
}

/*
 * sa_update_property(property, value)
 *
 * Update the specified property to the new value.  If value is NULL,
 * we currently treat this as a remove.
 */

int
sa_update_property(sa_property_t property, char *value)
{
	int ret = SA_OK;
	if (value == NULL) {
		return (sa_remove_property(property));
	} else {
		sa_optionset_t optionset;
		sa_group_t group;
		set_node_attr((void *)property, "value", value);
		optionset = sa_get_property_parent(property);
		if (optionset != NULL) {
			group = sa_get_optionset_parent(optionset);
			if (group != NULL) {
				ret = sa_set_prop_by_prop(optionset, group,
				    property, SA_PROP_OP_UPDATE);
			}
		} else {
			ret = SA_NO_SUCH_PROP;
		}
	}
	return (ret);
}

/*
 * sa_get_protocol_property(propset, prop)
 *
 * Get the specified protocol specific property. These are global to
 * the protocol and not specific to a group or share.
 */

sa_property_t
sa_get_protocol_property(sa_protocol_properties_t propset, char *prop)
{
	xmlNodePtr node = (xmlNodePtr)propset;
	xmlChar *value = NULL;

	for (node = node->children; node != NULL;
	    node = node->next) {
		if (xmlStrcmp(node->name, (xmlChar *)"option") == 0) {
			if (prop == NULL)
				break;
			value = xmlGetProp(node, (xmlChar *)"type");
			if (value != NULL &&
			    xmlStrcasecmp(value, (xmlChar *)prop) == 0) {
				break;
			}
			if (value != NULL) {
				xmlFree(value);
				value = NULL;
			}
		}
	}
	if (value != NULL)
		xmlFree(value);
	if (node != NULL && xmlStrcmp(node->name, (xmlChar *)"option") != 0) {
		/*
		 * avoid a non option node -- it is possible to be a
		 * text node
		 */
		node = NULL;
	}
	return ((sa_property_t)node);
}

/*
 * sa_get_next_protocol_property(prop)
 *
 * Get the next protocol specific property in the list.
 */

sa_property_t
sa_get_next_protocol_property(sa_property_t prop)
{
	xmlNodePtr node;

	for (node = ((xmlNodePtr)prop)->next; node != NULL;
	    node = node->next) {
		if (xmlStrcmp(node->name, (xmlChar *)"option") == 0) {
			break;
		}
	}
	return ((sa_property_t)node);
}

/*
 * sa_set_protocol_property(prop, value)
 *
 * Set the specified property to have the new value.  The protocol
 * specific plugin will then be called to update the property.
 */

int
sa_set_protocol_property(sa_property_t prop, char *value)
{
	sa_protocol_properties_t propset;
	char *proto;
	int ret = SA_INVALID_PROTOCOL;

	propset = ((xmlNodePtr)prop)->parent;
	if (propset != NULL) {
		proto = sa_get_optionset_attr(propset, "type");
		if (proto != NULL) {
			set_node_attr((xmlNodePtr)prop, "value", value);
			ret = sa_proto_set_property(proto, prop);
			sa_free_attr_string(proto);
		}
	}
	return (ret);
}

/*
 * sa_add_protocol_property(propset, prop)
 *
 * Add a new property to the protocol sepcific property set.
 */

int
sa_add_protocol_property(sa_protocol_properties_t propset, sa_property_t prop)
{
	xmlNodePtr node;

	/* should check for legitimacy */
	node = xmlAddChild((xmlNodePtr)propset, (xmlNodePtr)prop);
	if (node != NULL)
		return (SA_OK);
	return (SA_NO_MEMORY);
}

/*
 * sa_create_protocol_properties(proto)
 *
 * Create a protocol specifity property set.
 */

sa_protocol_properties_t
sa_create_protocol_properties(char *proto)
{
	xmlNodePtr node;

	node = xmlNewNode(NULL, (xmlChar *)"propertyset");
	if (node != NULL)
		xmlSetProp(node, (xmlChar *)"type", (xmlChar *)proto);
	return (node);
}