|
root / base / usr / src / lib / libbrand
libbrand Plain Text 2306 lines 64.1 KB
   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
#
# 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 2009 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#

include		../Makefile.lib

# Hammerhead: amd64-only
SUBDIRS = $(MACH64)

all :		TARGET = all
clean :	TARGET = clean
clobber :	TARGET = clobber
install :	TARGET = install

HDRDIR=		common
HDRS=		libbrand.h

FILEMODE =	644

BRANDDIR=	brand
ROOTBRANDDIR=	$(ROOTLIBDIR)/$(BRANDDIR)

DTDS =		dtd/brand.dtd.1 dtd/zone_platform.dtd.1
DTDDIR =	$(ROOT)/usr/share/lib/xml
ROOTDTDS=	$(DTDS:%=$(DTDDIR)/%)

$(DTDDIR)/%: %
	$(INS.file)

.KEEP_STATE:

all clean clobber install: $(SUBDIRS)

install_h: $(ROOTHDRS)

check: $(CHECKHDRS)

$(ROOTBRANDDIR):
	$(INS.dir)

install:	$(ROOTBRANDDIR)	$(ROOTDTDS)

$(SUBDIRS): FRC
	@cd $@; pwd; $(MAKE) $(TARGET)

FRC:

include ../Makefile.targ
#
# 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 2006 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#

LIBRARY=	libbrand.a
VERS=		.1

OBJECTS=	libbrand.o

include ../../Makefile.lib

LIBS=		$(DYNLIB)
LDLIBS +=	-lc
CPPFLAGS +=	-I$(ROOT)/usr/include/libxml2 \
		-I$(SRCDIR) -D_REENTRANT
$(DYNLIB) :	LDLIBS += -lxml2

SRCDIR=		../common

.KEEP_STATE:

all:	$(LIBS)

install: all

include ../../Makefile.targ
#
# 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 2006 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#

include ../Makefile.com
include ../../Makefile.lib.64

install: all $(ROOTLIBS64) $(ROOTLINKS64)
/*
 * 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 (c) 2015, Joyent, Inc.
 * Copyright 2014 Nexenta Systems, Inc. All rights reserved.
 */

#include <assert.h>
#include <dirent.h>
#include <errno.h>
#include <fnmatch.h>
#include <signal.h>
#include <stdlib.h>
#include <unistd.h>
#include <strings.h>
#include <synch.h>
#include <sys/brand.h>
#include <sys/fcntl.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/systeminfo.h>
#include <sys/types.h>
#include <thread.h>
#include <zone.h>

#include <libbrand_impl.h>
#include <libbrand.h>

#define	DTD_ELEM_ATTACH		((const xmlChar *) "attach")
#define	DTD_ELEM_BOOT		((const xmlChar *) "boot")
#define	DTD_ELEM_BRAND		((const xmlChar *) "brand")
#define	DTD_ELEM_CLONE		((const xmlChar *) "clone")
#define	DTD_ELEM_COMMENT	((const xmlChar *) "comment")
#define	DTD_ELEM_DETACH		((const xmlChar *) "detach")
#define	DTD_ELEM_DEVICE		((const xmlChar *) "device")
#define	DTD_ELEM_GLOBAL_MOUNT	((const xmlChar *) "global_mount")
#define	DTD_ELEM_HALT		((const xmlChar *) "halt")
#define	DTD_ELEM_INITNAME	((const xmlChar *) "initname")
#define	DTD_ELEM_INSTALL	((const xmlChar *) "install")
#define	DTD_ELEM_INSTALLOPTS	((const xmlChar *) "installopts")
#define	DTD_ELEM_LOGIN_CMD	((const xmlChar *) "login_cmd")
#define	DTD_ELEM_FORCELOGIN_CMD	((const xmlChar *) "forcedlogin_cmd")
#define	DTD_ELEM_MODNAME	((const xmlChar *) "modname")
#define	DTD_ELEM_MOUNT		((const xmlChar *) "mount")
#define	DTD_ELEM_RESTARTINIT	((const xmlChar *) "restartinit")
#define	DTD_ELEM_RESTARTINIT0	((const xmlChar *) "restartinit0")
#define	DTD_ELEM_RESTARTINITREBOOT	((const xmlChar *) "restartinitreboot")
#define	DTD_ELEM_POSTATTACH	((const xmlChar *) "postattach")
#define	DTD_ELEM_POSTCLONE	((const xmlChar *) "postclone")
#define	DTD_ELEM_POSTINSTALL	((const xmlChar *) "postinstall")
#define	DTD_ELEM_POSTSNAP	((const xmlChar *) "postsnap")
#define	DTD_ELEM_POSTSTATECHG	((const xmlChar *) "poststatechange")
#define	DTD_ELEM_PREDETACH	((const xmlChar *) "predetach")
#define	DTD_ELEM_PRESNAP	((const xmlChar *) "presnap")
#define	DTD_ELEM_PRESTATECHG	((const xmlChar *) "prestatechange")
#define	DTD_ELEM_PREUNINSTALL	((const xmlChar *) "preuninstall")
#define	DTD_ELEM_PRIVILEGE	((const xmlChar *) "privilege")
#define	DTD_ELEM_QUERY		((const xmlChar *) "query")
#define	DTD_ELEM_SECFLAGS	((const xmlChar *) "security-flags")
#define	DTD_ELEM_SHUTDOWN	((const xmlChar *) "shutdown")
#define	DTD_ELEM_SYMLINK	((const xmlChar *) "symlink")
#define	DTD_ELEM_SYSBOOT	((const xmlChar *) "sysboot")
#define	DTD_ELEM_UNINSTALL	((const xmlChar *) "uninstall")
#define	DTD_ELEM_USER_CMD	((const xmlChar *) "user_cmd")
#define	DTD_ELEM_VALIDSNAP	((const xmlChar *) "validatesnap")
#define	DTD_ELEM_VERIFY_CFG	((const xmlChar *) "verify_cfg")
#define	DTD_ELEM_VERIFY_ADM	((const xmlChar *) "verify_adm")

#define	DTD_ATTR_ALLOWEXCL	((const xmlChar *) "allow-exclusive-ip")
#define	DTD_ATTR_ARCH		((const xmlChar *) "arch")
#define	DTD_ATTR_DIRECTORY	((const xmlChar *) "directory")
#define	DTD_ATTR_IPTYPE		((const xmlChar *) "ip-type")
#define	DTD_ATTR_MATCH		((const xmlChar *) "match")
#define	DTD_ATTR_MODE		((const xmlChar *) "mode")
#define	DTD_ATTR_NAME		((const xmlChar *) "name")
#define	DTD_ATTR_OPT		((const xmlChar *) "opt")
#define	DTD_ATTR_PATH		((const xmlChar *) "path")
#define	DTD_ATTR_SET		((const xmlChar *) "set")
#define	DTD_ATTR_SOURCE		((const xmlChar *) "source")
#define	DTD_ATTR_SPECIAL	((const xmlChar *) "special")
#define	DTD_ATTR_TARGET		((const xmlChar *) "target")
#define	DTD_ATTR_TYPE		((const xmlChar *) "type")

#define	DTD_ENTITY_TRUE		"true"

static volatile boolean_t	libbrand_initialized = B_FALSE;
static char			i_curr_arch[MAXNAMELEN];
static char			i_curr_zone[ZONENAME_MAX];

/*ARGSUSED*/
static void
brand_error_func(void *ctx, const char *msg, ...)
{
	/*
	 * Ignore error messages from libxml
	 */
}

static boolean_t
libbrand_initialize()
{
	static mutex_t initialize_lock = DEFAULTMUTEX;

	(void) mutex_lock(&initialize_lock);

	if (libbrand_initialized) {
		(void) mutex_unlock(&initialize_lock);
		return (B_TRUE);
	}

	if (sysinfo(SI_ARCHITECTURE, i_curr_arch, sizeof (i_curr_arch)) < 0) {
		(void) mutex_unlock(&initialize_lock);
		return (B_FALSE);
	}

	if (getzonenamebyid(getzoneid(), i_curr_zone,
	    sizeof (i_curr_zone)) < 0) {
		(void) mutex_unlock(&initialize_lock);
		return (B_FALSE);
	}

	xmlSetGenericErrorFunc(NULL, brand_error_func);

	libbrand_initialized = B_TRUE;
	(void) mutex_unlock(&initialize_lock);
	return (B_TRUE);
}

static const char *
get_curr_arch(void)
{
	if (!libbrand_initialize())
		return (NULL);

	return (i_curr_arch);
}

static const char *
get_curr_zone(void)
{
	if (!libbrand_initialize())
		return (NULL);

	return (i_curr_zone);
}

/*
 * Internal function to open an XML file
 *
 * Returns the XML doc pointer, or NULL on failure.  It will validate the
 * document, as well as removing any comments from the document structure.
 */
static xmlDocPtr
open_xml_file(const char *file)
{
	xmlDocPtr doc;
	xmlValidCtxtPtr cvp;
	int valid;

	if (!libbrand_initialize())
		return (NULL);

	/*
	 * Parse the file
	 */
	doc = xmlReadFile(file, NULL, XML_PARSE_DTDLOAD |
	    XML_PARSE_DTDVALID | XML_PARSE_NOBLANKS | XML_PARSE_NOWARNING);
	if (doc == NULL)
		return (NULL);

	/*
	 * Validate the file
	 */
	if ((cvp = xmlNewValidCtxt()) == NULL) {
		xmlFreeDoc(doc);
		return (NULL);
	}
	cvp->error = brand_error_func;
	cvp->warning = brand_error_func;
	valid = xmlValidateDocument(cvp, doc);
	xmlFreeValidCtxt(cvp);
	if (valid == 0) {
		xmlFreeDoc(doc);
		return (NULL);
	}

	return (doc);
}
/*
 * Open a handle to the named brand.
 *
 * Returns a handle to the named brand, which is used for all subsequent brand
 * interaction, or NULL if unable to open or initialize the brand.
 */
brand_handle_t
brand_open(const char *name)
{
	struct brand_handle *bhp;
	char path[MAXPATHLEN];
	xmlNodePtr node;
	xmlChar *property;
	struct stat statbuf;

	/*
	 * Make sure brand name isn't too long
	 */
	if (strlen(name) >= MAXNAMELEN)
		return (NULL);

	/*
	 * Check that the brand exists
	 */
	(void) snprintf(path, sizeof (path), "%s/%s", BRAND_DIR, name);

	if (stat(path, &statbuf) != 0)
		return (NULL);

	/*
	 * Allocate brand handle
	 */
	if ((bhp = malloc(sizeof (struct brand_handle))) == NULL)
		return (NULL);
	bzero(bhp, sizeof (struct brand_handle));

	(void) strcpy(bhp->bh_name, name);

	/*
	 * Open the configuration file
	 */
	(void) snprintf(path, sizeof (path), "%s/%s/%s", BRAND_DIR, name,
	    BRAND_CONFIG);
	if ((bhp->bh_config = open_xml_file(path)) == NULL) {
		brand_close((brand_handle_t)bhp);
		return (NULL);
	}

	/*
	 * Verify that the name of the brand matches the directory in which it
	 * is installed.
	 */
	if ((node = xmlDocGetRootElement(bhp->bh_config)) == NULL) {
		brand_close((brand_handle_t)bhp);
		return (NULL);
	}

	if (xmlStrcmp(node->name, DTD_ELEM_BRAND) != 0) {
		brand_close((brand_handle_t)bhp);
		return (NULL);
	}

	if ((property = xmlGetProp(node, DTD_ATTR_NAME)) == NULL) {
		brand_close((brand_handle_t)bhp);
		return (NULL);
	}

	if (strcmp((char *)property, name) != 0) {
		xmlFree(property);
		brand_close((brand_handle_t)bhp);
		return (NULL);
	}
	xmlFree(property);

	/*
	 * Open handle to platform configuration file.
	 */
	(void) snprintf(path, sizeof (path), "%s/%s/%s", BRAND_DIR, name,
	    BRAND_PLATFORM);
	if ((bhp->bh_platform = open_xml_file(path)) == NULL) {
		brand_close((brand_handle_t)bhp);
		return (NULL);
	}

	return ((brand_handle_t)bhp);
}

/*
 * Closes the given brand handle
 */
void
brand_close(brand_handle_t bh)
{
	struct brand_handle *bhp = (struct brand_handle *)bh;
	if (bhp->bh_platform != NULL)
		xmlFreeDoc(bhp->bh_platform);
	if (bhp->bh_config != NULL)
		xmlFreeDoc(bhp->bh_config);
	free(bhp);
}

static int
i_substitute_tokens(const char *sbuf, char *dbuf, int dbuf_size,
    const char *zonename, const char *zonepath, const char *username,
    const char *curr_zone)
{
	int dst, src;

	/*
	 * Walk through the characters, substituting values as needed.
	 */
	dbuf[0] = '\0';
	dst = 0;
	for (src = 0; src < strlen((char *)sbuf) && dst < dbuf_size; src++) {
		if (sbuf[src] != '%') {
			dbuf[dst++] = sbuf[src];
			continue;
		}

		switch (sbuf[++src]) {
		case '%':
			dst += strlcpy(dbuf + dst, "%", dbuf_size - dst);
			break;
		case 'R':
			if (zonepath == NULL)
				break;
			dst += strlcpy(dbuf + dst, zonepath, dbuf_size - dst);
			break;
		case 'u':
			if (username == NULL)
				break;
			dst += strlcpy(dbuf + dst, username, dbuf_size - dst);
			break;
		case 'Z':
			if (curr_zone == NULL)
				break;
			/* name of the zone we're running in */
			dst += strlcpy(dbuf + dst, curr_zone, dbuf_size - dst);
			break;
		case 'z':
			/* name of the zone we're operating on */
			if (zonename == NULL)
				break;
			dst += strlcpy(dbuf + dst, zonename, dbuf_size - dst);
			break;
		}
	}

	if (dst >= dbuf_size)
		return (-1);

	dbuf[dst] = '\0';
	return (0);
}

/*
 * Retrieve the given tag from the brand.
 * Perform the following substitutions as necessary:
 *
 *	%%	%
 *	%u	Username
 *	%z	Name of target zone
 *	%Z	Name of current zone
 *	%R	Zonepath of zone
 *
 * Returns 0 on success, -1 on failure.
 */
static int
brand_get_value(struct brand_handle *bhp, const char *zonename,
    const char *zonepath, const char *username, const char *curr_zone,
    char *buf, size_t len, const xmlChar *tagname,
    boolean_t substitute, boolean_t optional)
{
	xmlNodePtr node;
	xmlChar *content;
	int err = 0;

	/*
	 * Retrieve the specified value from the XML doc
	 */
	if ((node = xmlDocGetRootElement(bhp->bh_config)) == NULL)
		return (-1);

	if (xmlStrcmp(node->name, DTD_ELEM_BRAND) != 0)
		return (-1);

	for (node = node->xmlChildrenNode; node != NULL;
	    node = node->next) {
		if (xmlStrcmp(node->name, tagname) == 0)
			break;
	}

	if (node == NULL) {
		if (optional) {
			buf[0] = '\0';
			return (0);
		} else {
			return (-1);
		}
	}

	if ((content = xmlNodeGetContent(node)) == NULL)
		return (-1);

	if (strlen((char *)content) == 0) {
		/*
		 * If the entry in the config file is empty, check to see
		 * whether this is an optional field.  If so, we return the
		 * empty buffer.  If not, we return an error.
		 */
		if (optional) {
			buf[0] = '\0';
		} else {
			err = -1;
		}
	} else {
		/* Substitute token values as needed. */
		if (substitute) {
			if (i_substitute_tokens((char *)content, buf, len,
			    zonename, zonepath, username, curr_zone) != 0)
				err = -1;
		} else {
			if (strlcpy(buf, (char *)content, len) >= len)
				err = -1;
		}
	}

	xmlFree(content);

	return (err);
}

int
brand_get_attach(brand_handle_t bh, const char *zonename,
    const char *zonepath, char *buf, size_t len)
{
	struct brand_handle *bhp = (struct brand_handle *)bh;
	return (brand_get_value(bhp, zonename, zonepath, NULL, NULL,
	    buf, len, DTD_ELEM_ATTACH, B_TRUE, B_TRUE));
}

int
brand_get_boot(brand_handle_t bh, const char *zonename,
    const char *zonepath, char *buf, size_t len)
{
	struct brand_handle *bhp = (struct brand_handle *)bh;
	return (brand_get_value(bhp, zonename, zonepath, NULL, NULL,
	    buf, len, DTD_ELEM_BOOT, B_TRUE, B_TRUE));
}

int
brand_get_brandname(brand_handle_t bh, char *buf, size_t len)
{
	struct brand_handle *bhp = (struct brand_handle *)bh;
	if (len <= strlen(bhp->bh_name))
		return (-1);

	(void) strcpy(buf, bhp->bh_name);

	return (0);
}

int
brand_get_clone(brand_handle_t bh, const char *zonename,
    const char *zonepath, char *buf, size_t len)
{
	struct brand_handle *bhp = (struct brand_handle *)bh;
	return (brand_get_value(bhp, zonename, zonepath, NULL, NULL,
	    buf, len, DTD_ELEM_CLONE, B_TRUE, B_TRUE));
}

int
brand_get_detach(brand_handle_t bh, const char *zonename,
    const char *zonepath, char *buf, size_t len)
{
	struct brand_handle *bhp = (struct brand_handle *)bh;
	return (brand_get_value(bhp, zonename, zonepath, NULL, NULL,
	    buf, len, DTD_ELEM_DETACH, B_TRUE, B_TRUE));
}

int
brand_get_halt(brand_handle_t bh, const char *zonename,
    const char *zonepath, char *buf, size_t len)
{
	struct brand_handle *bhp = (struct brand_handle *)bh;
	return (brand_get_value(bhp, zonename, zonepath, NULL, NULL,
	    buf, len, DTD_ELEM_HALT, B_TRUE, B_TRUE));
}

int
brand_get_shutdown(brand_handle_t bh, const char *zonename,
    const char *zonepath, char *buf, size_t len)
{
	struct brand_handle *bhp = (struct brand_handle *)bh;
	return (brand_get_value(bhp, zonename, zonepath, NULL, NULL,
	    buf, len, DTD_ELEM_SHUTDOWN, B_TRUE, B_TRUE));
}

int
brand_get_initname(brand_handle_t bh, char *buf, size_t len)
{
	struct brand_handle *bhp = (struct brand_handle *)bh;
	return (brand_get_value(bhp, NULL, NULL, NULL, NULL,
	    buf, len, DTD_ELEM_INITNAME, B_FALSE, B_FALSE));
}

static boolean_t
i_brand_restartinit(brand_handle_t bh, const xmlChar *tagname, boolean_t deflt)
{
	struct brand_handle *bhp = (struct brand_handle *)bh;
	char val[80];

	if (brand_get_value(bhp, NULL, NULL, NULL, NULL,
	    val, sizeof (val), tagname, B_FALSE, B_FALSE) != 0) {
		return (deflt);
	}

	if (strcmp(val, "false") == 0)
		return (B_FALSE);
	return (B_TRUE);
}

boolean_t
brand_restartinit(brand_handle_t bh)
{
	return (i_brand_restartinit(bh, DTD_ELEM_RESTARTINIT, B_TRUE));
}

boolean_t
brand_restartinit0(brand_handle_t bh)
{
	return (i_brand_restartinit(bh, DTD_ELEM_RESTARTINIT0, B_FALSE));
}

boolean_t
brand_restartinitreboot(brand_handle_t bh)
{
	return (i_brand_restartinit(bh, DTD_ELEM_RESTARTINITREBOOT, B_FALSE));
}

int
brand_get_login_cmd(brand_handle_t bh, const char *username,
    char *buf, size_t len)
{
	struct brand_handle *bhp = (struct brand_handle *)bh;
	const char *curr_zone = get_curr_zone();
	return (brand_get_value(bhp, NULL, NULL, username, curr_zone,
	    buf, len, DTD_ELEM_LOGIN_CMD, B_TRUE, B_FALSE));
}

int
brand_get_forcedlogin_cmd(brand_handle_t bh, const char *username,
    char *buf, size_t len)
{
	struct brand_handle *bhp = (struct brand_handle *)bh;
	const char *curr_zone = get_curr_zone();
	return (brand_get_value(bhp, NULL, NULL, username, curr_zone,
	    buf, len, DTD_ELEM_FORCELOGIN_CMD, B_TRUE, B_FALSE));
}

int
brand_get_user_cmd(brand_handle_t bh, const char *username,
    char *buf, size_t len)
{
	struct brand_handle *bhp = (struct brand_handle *)bh;

	return (brand_get_value(bhp, NULL, NULL, username, NULL,
	    buf, len, DTD_ELEM_USER_CMD, B_TRUE, B_FALSE));
}

int
brand_get_install(brand_handle_t bh, const char *zonename,
    const char *zonepath, char *buf, size_t len)
{
	struct brand_handle *bhp = (struct brand_handle *)bh;
	return (brand_get_value(bhp, zonename, zonepath, NULL, NULL,
	    buf, len, DTD_ELEM_INSTALL, B_TRUE, B_FALSE));
}

int
brand_get_installopts(brand_handle_t bh, char *buf, size_t len)
{
	struct brand_handle *bhp = (struct brand_handle *)bh;
	return (brand_get_value(bhp, NULL, NULL, NULL, NULL,
	    buf, len, DTD_ELEM_INSTALLOPTS, B_FALSE, B_TRUE));
}

int
brand_get_modname(brand_handle_t bh, char *buf, size_t len)
{
	struct brand_handle *bhp = (struct brand_handle *)bh;
	return (brand_get_value(bhp, NULL, NULL, NULL, NULL,
	    buf, len, DTD_ELEM_MODNAME, B_FALSE, B_TRUE));
}

int
brand_get_postattach(brand_handle_t bh, const char *zonename,
    const char *zonepath, char *buf, size_t len)
{
	struct brand_handle *bhp = (struct brand_handle *)bh;
	return (brand_get_value(bhp, zonename, zonepath, NULL, NULL,
	    buf, len, DTD_ELEM_POSTATTACH, B_TRUE, B_TRUE));
}

int
brand_get_postclone(brand_handle_t bh, const char *zonename,
    const char *zonepath, char *buf, size_t len)
{
	struct brand_handle *bhp = (struct brand_handle *)bh;
	return (brand_get_value(bhp, zonename, zonepath, NULL, NULL,
	    buf, len, DTD_ELEM_POSTCLONE, B_TRUE, B_TRUE));
}

int
brand_get_postinstall(brand_handle_t bh, const char *zonename,
    const char *zonepath, char *buf, size_t len)
{
	struct brand_handle *bhp = (struct brand_handle *)bh;
	return (brand_get_value(bhp, zonename, zonepath, NULL, NULL,
	    buf, len, DTD_ELEM_POSTINSTALL, B_TRUE, B_TRUE));
}

int
brand_get_postsnap(brand_handle_t bh, const char *zonename,
    const char *zonepath, char *buf, size_t len)
{
	struct brand_handle *bhp = (struct brand_handle *)bh;
	return (brand_get_value(bhp, zonename, zonepath, NULL, NULL,
	    buf, len, DTD_ELEM_POSTSNAP, B_TRUE, B_TRUE));
}

int
brand_get_poststatechange(brand_handle_t bh, const char *zonename,
    const char *zonepath, char *buf, size_t len)
{
	struct brand_handle *bhp = (struct brand_handle *)bh;
	return (brand_get_value(bhp, zonename, zonepath, NULL, NULL,
	    buf, len, DTD_ELEM_POSTSTATECHG, B_TRUE, B_TRUE));
}

int
brand_get_predetach(brand_handle_t bh, const char *zonename,
    const char *zonepath, char *buf, size_t len)
{
	struct brand_handle *bhp = (struct brand_handle *)bh;
	return (brand_get_value(bhp, zonename, zonepath, NULL, NULL,
	    buf, len, DTD_ELEM_PREDETACH, B_TRUE, B_TRUE));
}

int
brand_get_presnap(brand_handle_t bh, const char *zonename,
    const char *zonepath, char *buf, size_t len)
{
	struct brand_handle *bhp = (struct brand_handle *)bh;
	return (brand_get_value(bhp, zonename, zonepath, NULL, NULL,
	    buf, len, DTD_ELEM_PRESNAP, B_TRUE, B_TRUE));
}

int
brand_get_prestatechange(brand_handle_t bh, const char *zonename,
    const char *zonepath, char *buf, size_t len)
{
	struct brand_handle *bhp = (struct brand_handle *)bh;
	return (brand_get_value(bhp, zonename, zonepath, NULL, NULL,
	    buf, len, DTD_ELEM_PRESTATECHG, B_TRUE, B_TRUE));
}

int
brand_get_preuninstall(brand_handle_t bh, const char *zonename,
    const char *zonepath, char *buf, size_t len)
{
	struct brand_handle *bhp = (struct brand_handle *)bh;
	return (brand_get_value(bhp, zonename, zonepath, NULL, NULL,
	    buf, len, DTD_ELEM_PREUNINSTALL, B_TRUE, B_TRUE));
}

int
brand_get_query(brand_handle_t bh, const char *zonename,
    const char *zonepath, char *buf, size_t len)
{
	struct brand_handle *bhp = (struct brand_handle *)bh;
	return (brand_get_value(bhp, zonename, zonepath, NULL, NULL,
	    buf, len, DTD_ELEM_QUERY, B_TRUE, B_TRUE));
}

int
brand_get_secflags(brand_handle_t bh, char *buf, size_t len)
{
	struct brand_handle *bhp = (struct brand_handle *)bh;
	return (brand_get_value(bhp, NULL, NULL, NULL, NULL,
	    buf, len, DTD_ELEM_SECFLAGS, B_FALSE, B_TRUE));
}

int
brand_get_uninstall(brand_handle_t bh, const char *zonename,
    const char *zonepath, char *buf, size_t len)
{
	struct brand_handle *bhp = (struct brand_handle *)bh;
	return (brand_get_value(bhp, zonename, zonepath, NULL, NULL,
	    buf, len, DTD_ELEM_UNINSTALL, B_TRUE, B_TRUE));
}

int
brand_get_validatesnap(brand_handle_t bh, const char *zonename,
    const char *zonepath, char *buf, size_t len)
{
	struct brand_handle *bhp = (struct brand_handle *)bh;
	return (brand_get_value(bhp, zonename, zonepath, NULL, NULL,
	    buf, len, DTD_ELEM_VALIDSNAP, B_TRUE, B_TRUE));
}

int
brand_get_verify_cfg(brand_handle_t bh, char *buf, size_t len)
{
	struct brand_handle *bhp = (struct brand_handle *)bh;
	return (brand_get_value(bhp, NULL, NULL, NULL, NULL,
	    buf, len, DTD_ELEM_VERIFY_CFG, B_FALSE, B_TRUE));
}

int
brand_get_verify_adm(brand_handle_t bh, const char *zonename,
    const char *zonepath, char *buf, size_t len)
{
	struct brand_handle *bhp = (struct brand_handle *)bh;
	return (brand_get_value(bhp, zonename, zonepath, NULL, NULL,
	    buf, len, DTD_ELEM_VERIFY_ADM, B_TRUE, B_TRUE));
}

int
brand_get_sysboot(brand_handle_t bh, const char *zonename,
    const char *zonepath, char *buf, size_t len)
{
	struct brand_handle *bhp = (struct brand_handle *)bh;
	return (brand_get_value(bhp, zonename, zonepath, NULL, NULL,
	    buf, len, DTD_ELEM_SYSBOOT, B_TRUE, B_TRUE));
}

boolean_t
brand_allow_exclusive_ip(brand_handle_t bh)
{
	struct brand_handle	*bhp = (struct brand_handle *)bh;
	xmlNodePtr		node;
	xmlChar			*allow_excl;
	boolean_t		ret;

	assert(bhp != NULL);

	if ((node = xmlDocGetRootElement(bhp->bh_platform)) == NULL)
		return (B_FALSE);

	allow_excl = xmlGetProp(node, DTD_ATTR_ALLOWEXCL);
	if (allow_excl == NULL)
		return (B_FALSE);

	/* Note: only return B_TRUE if it's "true" */
	if (strcmp((char *)allow_excl, DTD_ENTITY_TRUE) == 0)
		ret = B_TRUE;
	else
		ret = B_FALSE;

	xmlFree(allow_excl);

	return (ret);
}

/*
 * Iterate over brand privileges
 *
 * Walks the brand config, searching for <privilege> elements, calling the
 * specified callback for each.  Returns 0 on success, or -1 on failure.
 */
int
brand_config_iter_privilege(brand_handle_t bh,
    int (*func)(void *, priv_iter_t *), void *data)
{
	struct brand_handle	*bhp = (struct brand_handle *)bh;
	xmlNodePtr		node;
	xmlChar			*name, *set, *iptype;
	priv_iter_t		priv_iter;
	int			ret;

	if ((node = xmlDocGetRootElement(bhp->bh_config)) == NULL)
		return (-1);

	for (node = node->xmlChildrenNode; node != NULL; node = node->next) {

		if (xmlStrcmp(node->name, DTD_ELEM_PRIVILEGE) != 0)
			continue;

		name = xmlGetProp(node, DTD_ATTR_NAME);
		set = xmlGetProp(node, DTD_ATTR_SET);
		iptype = xmlGetProp(node, DTD_ATTR_IPTYPE);

		if (name == NULL || set == NULL || iptype == NULL) {
			if (name != NULL)
				xmlFree(name);
			if (set != NULL)
				xmlFree(set);
			if (iptype != NULL)
				xmlFree(iptype);
			return (-1);
		}

		priv_iter.pi_name = (char *)name;
		priv_iter.pi_set = (char *)set;
		priv_iter.pi_iptype = (char *)iptype;

		ret = func(data, &priv_iter);

		xmlFree(name);
		xmlFree(set);
		xmlFree(iptype);

		if (ret != 0)
			return (-1);
	}

	return (0);
}

static int
i_brand_platform_iter_mounts(struct brand_handle *bhp, const char *zonename,
    const char *zonepath, int (*func)(void *, const char *, const char *,
    const char *, const char *), void *data, const xmlChar *mount_type)
{
	xmlNodePtr node;
	xmlChar *special, *dir, *type, *opt;
	char special_exp[MAXPATHLEN];
	char opt_exp[MAXPATHLEN];
	int ret;

	if ((node = xmlDocGetRootElement(bhp->bh_platform)) == NULL)
		return (-1);

	for (node = node->xmlChildrenNode; node != NULL; node = node->next) {

		if (xmlStrcmp(node->name, mount_type) != 0)
			continue;

		special = xmlGetProp(node, DTD_ATTR_SPECIAL);
		dir = xmlGetProp(node, DTD_ATTR_DIRECTORY);
		type = xmlGetProp(node, DTD_ATTR_TYPE);
		opt = xmlGetProp(node, DTD_ATTR_OPT);
		if ((special == NULL) || (dir == NULL) || (type == NULL) ||
		    (opt == NULL)) {
			ret = -1;
			goto next;
		}

		/* Substitute token values as needed. */
		if ((ret = i_substitute_tokens((char *)special,
		    special_exp, sizeof (special_exp),
		    zonename, zonepath, NULL, NULL)) != 0)
			goto next;

		/* opt might not be defined */
		if (strlen((const char *)opt) == 0) {
			xmlFree(opt);
			opt = NULL;
		} else {
			if ((ret = i_substitute_tokens((char *)opt,
			    opt_exp, sizeof (opt_exp),
			    zonename, zonepath, NULL, NULL)) != 0)
				goto next;
		}

		ret = func(data, (char *)special_exp, (char *)dir,
		    (char *)type, ((opt != NULL) ? opt_exp : NULL));

next:
		if (special != NULL)
			xmlFree(special);
		if (dir != NULL)
			xmlFree(dir);
		if (type != NULL)
			xmlFree(type);
		if (opt != NULL)
			xmlFree(opt);
		if (ret != 0)
			return (-1);
	}
	return (0);
}


/*
 * Iterate over global platform filesystems
 *
 * Walks the platform, searching for <global_mount> elements, calling the
 * specified callback for each.  Returns 0 on success, or -1 on failure.
 *
 * Perform the following substitutions as necessary:
 *
 *	%R	Zonepath of zone
 */
int
brand_platform_iter_gmounts(brand_handle_t bh, const char *zonename,
    const char *zonepath, int (*func)(void *, const char *, const char *,
    const char *, const char *), void *data)
{
	struct brand_handle *bhp = (struct brand_handle *)bh;
	return (i_brand_platform_iter_mounts(bhp, zonename, zonepath, func,
	    data, DTD_ELEM_GLOBAL_MOUNT));
}

/*
 * Iterate over non-global zone platform filesystems
 *
 * Walks the platform, searching for <mount> elements, calling the
 * specified callback for each.  Returns 0 on success, or -1 on failure.
 */
int
brand_platform_iter_mounts(brand_handle_t bh, int (*func)(void *,
    const char *, const char *, const char *, const char *), void *data)
{
	struct brand_handle *bhp = (struct brand_handle *)bh;
	return (i_brand_platform_iter_mounts(bhp, NULL, NULL, func, data,
	    DTD_ELEM_MOUNT));
}

/*
 * Iterate over platform symlinks
 *
 * Walks the platform, searching for <symlink> elements, calling the
 * specified callback for each.  Returns 0 on success, or -1 on failure.
 */
int
brand_platform_iter_link(brand_handle_t bh,
    int (*func)(void *, const char *, const char *), void *data)
{
	struct brand_handle *bhp = (struct brand_handle *)bh;
	xmlNodePtr node;
	xmlChar *source, *target;
	int ret;

	if ((node = xmlDocGetRootElement(bhp->bh_platform)) == NULL)
		return (-1);

	for (node = node->xmlChildrenNode; node != NULL; node = node->next) {

		if (xmlStrcmp(node->name, DTD_ELEM_SYMLINK) != 0)
			continue;

		source = xmlGetProp(node, DTD_ATTR_SOURCE);
		target = xmlGetProp(node, DTD_ATTR_TARGET);

		if (source == NULL || target == NULL) {
			if (source != NULL)
				xmlFree(source);
			if (target != NULL)
				xmlFree(target);
			return (-1);
		}

		ret = func(data, (char *)source, (char *)target);

		xmlFree(source);
		xmlFree(target);

		if (ret != 0)
			return (-1);
	}

	return (0);
}

/*
 * Iterate over platform devices
 *
 * Walks the platform, searching for <device> elements, calling the
 * specified callback for each.  Returns 0 on success, or -1 on failure.
 */
int
brand_platform_iter_devices(brand_handle_t bh, const char *zonename,
    int (*func)(void *, const char *, const char *), void *data,
    const char *curr_iptype)
{
	struct brand_handle	*bhp = (struct brand_handle *)bh;
	const char		*curr_arch = get_curr_arch();
	xmlNodePtr		node;
	xmlChar			*match, *name, *arch, *iptype;
	char			match_exp[MAXPATHLEN];
	boolean_t		err = B_FALSE;
	int			ret = 0;


	assert(bhp != NULL);
	assert(zonename != NULL);
	assert(func != NULL);
	assert(curr_iptype != NULL);

	if ((node = xmlDocGetRootElement(bhp->bh_platform)) == NULL)
		return (-1);

	for (node = node->xmlChildrenNode; node != NULL; node = node->next) {

		if (xmlStrcmp(node->name, DTD_ELEM_DEVICE) != 0)
			continue;

		match = xmlGetProp(node, DTD_ATTR_MATCH);
		name = xmlGetProp(node, DTD_ATTR_NAME);
		arch = xmlGetProp(node, DTD_ATTR_ARCH);
		iptype = xmlGetProp(node, DTD_ATTR_IPTYPE);
		if ((match == NULL) || (name == NULL) || (arch == NULL) ||
		    (iptype == NULL)) {
			err = B_TRUE;
			goto next;
		}

		/* check if the arch matches */
		if ((strcmp((char *)arch, "all") != 0) &&
		    (strcmp((char *)arch, curr_arch) != 0))
			goto next;

		/* check if the iptype matches */
		if ((strcmp((char *)iptype, "all") != 0) &&
		    (strcmp((char *)iptype, curr_iptype) != 0))
			goto next;

		/* Substitute token values as needed. */
		if ((ret = i_substitute_tokens((char *)match,
		    match_exp, sizeof (match_exp),
		    zonename, NULL, NULL, NULL)) != 0) {
			err = B_TRUE;
			goto next;
		}

		/* name might not be defined */
		if (strlen((const char *)name) == 0) {
			xmlFree(name);
			name = NULL;
		}

		/* invoke the callback */
		ret = func(data, (const char *)match_exp, (const char *)name);

next:
		if (match != NULL)
			xmlFree(match);
		if (name != NULL)
			xmlFree(name);
		if (arch != NULL)
			xmlFree(arch);
		if (iptype != NULL)
			xmlFree(iptype);
		if (err)
			return (-1);
		if (ret != 0)
			return (-1);
	}

	return (0);
}
/*
 * 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 (c) 2015, Joyent, Inc.
 * Copyright 2014 Nexenta Systems, Inc. All rights reserved.
 */

#ifndef	_LIBBRAND_H
#define	_LIBBRAND_H

#ifdef	__cplusplus
extern "C" {
#endif

#include <sys/types.h>

typedef struct __brand_handle *brand_handle_t;

typedef struct priv_iter_s {
	char	*pi_name;
	char	*pi_set;
	char	*pi_iptype;
} priv_iter_t;

extern brand_handle_t brand_open(const char *);
extern void brand_close(brand_handle_t);

extern boolean_t brand_allow_exclusive_ip(brand_handle_t);

extern int brand_get_attach(brand_handle_t, const char *, const char *,
    char *, size_t);
extern int brand_get_boot(brand_handle_t, const char *, const char *,
    char *, size_t);
extern int brand_get_brandname(brand_handle_t, char *, size_t);
extern int brand_get_clone(brand_handle_t, const char *, const char *,
    char *, size_t);
extern int brand_get_detach(brand_handle_t, const char *, const char *,
    char *, size_t);
extern int brand_get_shutdown(brand_handle_t, const char *, const char *,
    char *, size_t);
extern int brand_get_halt(brand_handle_t, const char *, const char *,
    char *, size_t);
extern int brand_get_initname(brand_handle_t, char *, size_t);
extern boolean_t brand_restartinit(brand_handle_t);
extern boolean_t brand_restartinit0(brand_handle_t);
extern boolean_t brand_restartinitreboot(brand_handle_t);
extern int brand_get_install(brand_handle_t, const char *, const char *,
    char *, size_t);
extern int brand_get_installopts(brand_handle_t, char *, size_t);
extern int brand_get_login_cmd(brand_handle_t, const char *, char *, size_t);
extern int brand_get_forcedlogin_cmd(brand_handle_t, const char *,
    char *, size_t);
extern int brand_get_modname(brand_handle_t, char *, size_t);
extern int brand_get_postattach(brand_handle_t, const char *, const char *,
    char *, size_t);
extern int brand_get_postclone(brand_handle_t, const char *, const char *,
    char *, size_t);
extern int brand_get_postinstall(brand_handle_t, const char *, const char *,
    char *, size_t);
extern int brand_get_postsnap(brand_handle_t, const char *, const char *,
    char *, size_t);
extern int brand_get_poststatechange(brand_handle_t, const char *, const char *,
    char *, size_t);
extern int brand_get_predetach(brand_handle_t, const char *, const char *,
    char *, size_t);
extern int brand_get_presnap(brand_handle_t, const char *, const char *,
    char *, size_t);
extern int brand_get_prestatechange(brand_handle_t, const char *, const char *,
    char *, size_t);
extern int brand_get_preuninstall(brand_handle_t, const char *, const char *,
    char *, size_t);
extern int brand_get_query(brand_handle_t, const char *, const char *,
    char *, size_t);
extern int brand_get_secflags(brand_handle_t, char *, size_t);
extern int brand_get_uninstall(brand_handle_t, const char *, const char *,
    char *, size_t);
extern int brand_get_validatesnap(brand_handle_t, const char *, const char *,
    char *, size_t);
extern int brand_get_user_cmd(brand_handle_t, const char *, char *, size_t);
extern int brand_get_verify_cfg(brand_handle_t, char *, size_t);
extern int brand_get_verify_adm(brand_handle_t, const char *, const char *,
    char *, size_t);
extern int brand_get_sysboot(brand_handle_t, const char *, const char *, char *,
    size_t);

extern int brand_config_iter_privilege(brand_handle_t,
    int (*func)(void *, priv_iter_t *), void *);

extern int brand_platform_iter_devices(brand_handle_t, const char *,
    int (*)(void *, const char *, const char *), void *, const char *);
extern int brand_platform_iter_gmounts(brand_handle_t, const char *,
    const char *, int (*)(void *, const char *, const char *, const char *,
    const char *), void *);
extern int brand_platform_iter_link(brand_handle_t, int (*)(void *,
    const char *, const char *), void *);
extern int brand_platform_iter_mounts(brand_handle_t, int (*)(void *,
    const char *, const char *, const char *, const char *), void *);

#ifdef	__cplusplus
}
#endif

#endif	/* _LIBBRAND_H */
/*
 * 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 2006 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#ifndef	_LIBBRAND_IMPL_H
#define	_LIBBRAND_IMPL_H

#include <libxml/xmlmemory.h>
#include <libxml/parser.h>
#include <libbrand.h>

#ifdef	__cplusplus
extern "C" {
#endif

struct brand_handle {
	char		bh_name[MAXNAMELEN];
	xmlDocPtr	bh_config;
	xmlDocPtr	bh_platform;
};

#define	BRAND_DIR	"/usr/lib/brand"
#define	BRAND_CONFIG	"config.xml"
#define	BRAND_PLATFORM	"platform.xml"

#ifdef	__cplusplus
}
#endif

#endif	/* _LIBBRAND_IMPL_H */
#
# 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 (c) 2011, Joyent, Inc. All rights reserved.
# Copyright 2014 Nexenta Systems, Inc. All rights reserved.
#

#
# MAPFILE HEADER START
#
# WARNING:  STOP NOW.  DO NOT MODIFY THIS FILE.
# Object versioning must comply with the rules detailed in
#
#	usr/src/lib/README.mapfiles
#
# You should not be making modifications here until you've read the most current
# copy of that file. If you need help, contact a gatekeeper for guidance.
#
# MAPFILE HEADER END
#

$mapfile_version 2

SYMBOL_VERSION SUNWprivate {
    global:
	brand_allow_exclusive_ip;
	brand_close;
	brand_config_iter_privilege;
	brand_get_attach;
	brand_get_boot;
	brand_get_brandname;
	brand_get_clone;
	brand_get_detach;
	brand_get_forcedlogin_cmd;
	brand_get_halt;
	brand_get_initname;
	brand_get_install;
	brand_get_installopts;
	brand_get_login_cmd;
	brand_get_modname;
	brand_get_postattach;
	brand_get_postclone;
	brand_get_postinstall;
	brand_get_postsnap;
	brand_get_poststatechange;
	brand_get_predetach;
	brand_get_presnap;
	brand_get_prestatechange;
	brand_get_preuninstall;
	brand_get_query;
	brand_get_secflags;
	brand_get_sysboot;
	brand_get_shutdown;
	brand_get_uninstall;
	brand_get_user_cmd;
	brand_get_validatesnap;
	brand_get_verify_adm;
	brand_get_verify_cfg;
	brand_open;
	brand_platform_iter_devices;
	brand_platform_iter_gmounts;
	brand_platform_iter_link;
	brand_platform_iter_mounts;
	brand_restartinit;
	brand_restartinit0;
	brand_restartinitreboot;
    local:
	*;
};
<?xml version='1.0' encoding='UTF-8' ?>

<!--
 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 (c) 2011, Joyent, Inc. All rights reserved.

 DO NOT EDIT THIS FILE.

 Copyright 2014 Nexenta Systems, Inc. All rights reserved.
-->

<!--
  verify_cfg

    Identifies the program to be invoked by zonecfg to verify that the
    zone's configuration is legal, and that all the configured devices,
    attributes, etc. are legal for this brand.

    The program is called with a single argument: the path to a file
    containing a temporary config.xml file the zone.  It should return 0
    on success and non-0 on failure.  Any detailed error messages should be
    displayed to stderr.

    It has no attributes.

-->
<!ELEMENT verify_cfg	(#PCDATA) >
<!ATTLIST verify_cfg>
<!--
  verify_adm

    Identifies the program invoked by zoneadm to perform brand-specific
    checks as to the viability of a zone on this specific machine.

    The following replacements are performed:

      %z	Name of zone
      %R	Zonepath of zone
      Additional arguments, if any, are appended.

    The program should return 0 on success and non-0 on failure.  Any
    detailed error messages should be displayed to stderr.

    It has no attributes.

-->
<!ELEMENT verify_adm	(#PCDATA) >
<!ATTLIST verify_adm>

<!--
  install

    Identifies the program to invoke when installing a zone.  The following
    replacements are performed:

      %z	Name of zone
      %R	Zonepath of zone
      Additional arguments, if any, are appended.

    It has no attributes.
-->
<!ELEMENT install	(#PCDATA) >
<!ATTLIST install>

<!--
  installopts

    Identifies the command-line options supported by the brand's
    installation program, allowing zoneadm to parse the install line
    properly.

    It has no attributes.
-->
<!ELEMENT installopts	(#PCDATA) >
<!ATTLIST installopts>

<!--
  boot

    This is a program which gets run by zoneadmd when a zone is booted.
    The program will be invoked as the last step in the zone booting
    process before the the first process is spawned inside the zone.

    If this programs succeeds it should not generate any output.
    If this program returns an error, any output generated by the
    program will be sent to the zoneadmd message log.

    The following replacements are performed:

      %z	Name of zone
      %R	Zonepath of zone
      Additional arguments, if any, are appended.

    It has no attributes.
-->
<!ELEMENT boot	(#PCDATA) >
<!ATTLIST boot>

<!--
  sysboot

    This is a program that will be run by zoneadm during system boot for an
    installed zone that won't automatically boot.

    If the program succeeds, then it should not generate output.
    If the program returns an error, then the output it generates will be
    sent to the zones SMF service's message log.

    The following replacements are performed:

      %z	Name of the target zone
      %R	Zonepath of the target zone
      Additional arguments, if any, are appended.

    This element has no attributes.
-->
<!ELEMENT sysboot	(#PCDATA) >
<!ATTLIST sysboot>

<!--
  halt

    This is a program which gets run by zoneadmd when a zone is being
    halted.  This callback is provided to allow a brand to cleanup any
    special configuration that was setup during boot.

    This program will also be invoked by zoneadmd if any part of the zone
    booting process fail, even if the booting process failed before the
    brand boot program was invoked.  It is also possible that if the zone
    fails to halt after invoking this program, future attempts to halt the
    zone will invoke this program again.  So this program should be
    designed to clean up any resources allocated to a zone but it should
    also be able to gracefully handle the case where resources that it
    expects to release are not actually allocated (or have been already
    released.)

    If this programs succeeds it should not generate any output.  If this
    program returns an error, any output generated by the program will be
    sent to the zoneadmd message log.

    The following replacements are performed:

      %z	Name of zone
      %R	Zonepath of zone
      Additional arguments, if any, are appended.

    It has no attributes.
-->
<!ELEMENT halt	(#PCDATA) >
<!ATTLIST halt>

<!--
  shutdown

    This is a program which gets run by zoneadmd when a zone is being
    shutdown gracefully. Currently only asynchronous mode is supported.

    If this program succeeds it should not generate any output. If this
    program returns an error, any output generated by the program will be
    sent to the zoneadmd message log.

    The following replacements are performed:

      %z	Name of zone
      %R	Zonepath of zone
      Additional arguments, if any, are appended.

    It has no attributes.
-->
<!ELEMENT shutdown	(#PCDATA) >
<!ATTLIST shutdown>

<!--
  modname

    Path to the kernel module that implements the kernel-level
    functionality of the brand.

    It has no attributes.
-->
<!ELEMENT modname	(#PCDATA) >
<!ATTLIST modname>

<!--
  initname

    Path to the initial executable that should be launched when booting a
    branded zone.

    It has no attributes.
-->
<!ELEMENT initname	(#PCDATA) >
<!ATTLIST initname>

<!--
  restartinit, restartinit0 and restartinitreboot

    These three boolean elements control what action is taken when the
    program specified by the 'initname' element exits.

    The default values are:

        restartinit:       true
        restartinit0:      false
        restartinitreboot: false

    If 'restartinit' is set to false then the init process will never be
    restarted and the zone will shut down once init exits. In this case, the
    other restartinit elements are ignored.

    When 'restartinit0' is set, init will only be restarted if it exited with
    an exit status of 0, otherwise the zone will shut down.

    If 'restartinitreboot' is set to true then whenever init should be
    restarted, based on the other restartinit elements, the zone will instead
    be rebooted.

    These have no attributes.
-->
<!ELEMENT restartinit	(#PCDATA) >
<!ATTLIST restartinit>
<!ELEMENT restartinit0	(#PCDATA) >
<!ATTLIST restartinit0>
<!ELEMENT restartinitreboot	(#PCDATA) >
<!ATTLIST restartinitreboot>

<!--
  login_cmd

    Path to the initial login binary that should be executed when
    attempting to zlogin into a branded zone.

    The following replacements are performed:

      %Z	Name of the current zone
      %u	User login name

    It has no attributes.
-->
<!ELEMENT login_cmd	(#PCDATA) >
<!ATTLIST login_cmd>

<!--
  forcedlogin_cmd

    Path to the initial login binary that should be executed when
    attempting to zlogin into a branded zone without authentication.

    The following replacements are performed:

      %Z	Name of the current zone
      %u	User login name

    It has no attributes.
-->
<!ELEMENT forcedlogin_cmd	(#PCDATA) >
<!ATTLIST forcedlogin_cmd>

<!--
  user_cmd

    Path to the binary that will translate a user name to a passwd(5) entry.

    The following replacements are performed:

      %u        User login name

    It has no attributes.  The passwd(5) entry is used to determine $LOGNAME,
    $HOME, and $SHELL for non-interactive "zlogin -l <user> <cmd>".
-->
<!ELEMENT user_cmd   (#PCDATA) >
<!ATTLIST user_cmd>

<!--
  attach

    Path to a hook that will perform any necessary processing on
    a zone to allow it to be attached.  The zone will be in the "configured"
    state when this hook is run.  This hook is never called when the zone
    is "force attached" (-F).

    If this hook exits with a non-zero exit status, the attach operation
    will fail.

    The following replacements are performed:

      %z	Name of zone
      %R	Zonepath of zone
      Additional arguments, if any, are appended.

    If no hook is provided, the internal zoneadm attach code will be used.

    It has no attributes.
-->
<!ELEMENT attach	(#PCDATA) >
<!ATTLIST attach>

<!--
  postattach

    Path to a hook that will perform any necessary post-processing on
    a zone after it has been attached.  The zone will be in the "installed"
    state when this hook is run.  This hook is never called when the zone
    is "force attached" (-F).

    If this hook exits with a non-zero exit status, the attach operation
    will fail and the zone state will be reset to "configured".

    The following replacements are performed:

      %z	Name of zone
      %R	Zonepath of zone
      Additional arguments, if any, are appended.

    It has no attributes.
-->
<!ELEMENT postattach	(#PCDATA) >
<!ATTLIST postattach>

<!--
  postclone

    Path to a hook that will perform any necessary post-processing on
    a zone after it has been cloned.  The zone will be in the "incomplete"
    state when this hook is run.

    If this hook exits with a non-zero exit status, the clone operation
    will fail and the zone will be left in the "incomplete" state,
    otherwise the state will be changed to the "installed" state.

    The following replacements are performed:

      %z	Name of zone
      %R	Zonepath of zone
      Additional arguments, if any, are appended.

    It has no attributes.
-->
<!ELEMENT postclone	(#PCDATA) >
<!ATTLIST postclone>

<!--
  postinstall

    Path to a script that will perform any necessary post-processing on
    a zone after it has been freshly installed.  This hook will run after the
    install hook completes and the zone is in the installed state.  The
    additional arguments are the same as what is passed to the install hook.

    The following replacements are performed:

      %z	Name of zone
      %R	Zonepath of zone
      Additional arguments, if any, are appended.

    It has no attributes.
-->
<!ELEMENT postinstall	(#PCDATA) >
<!ATTLIST postinstall>

<!--
  predetach

    Path to a hook that will perform any necessary pre-processing on
    a zone before it is detached.  The zone will be in the "installed"
    state when this hook is run.

    It is possible that if the zone fails to detach after invoking this
    hook, future attempts to detach the zone will invoke this hook again.
    So this hook should be designed to gracefully handle the case where
    it is run multiple times on the same zone.  If this hook exits with
    a non-zero exit status, the detach operation will fail.

    This hook is most commonly used when there is pre-processing for detaching
    a zone but the built-in detach support will be used for the actual
    detach.  Otherwise, if a detach hook is provided, then it can be used
    to do both preprocessing as well as the actual detach.

    The following replacements are performed:

      %z	Name of zone
      %R	Zonepath of zone
      Additional arguments, if any, are appended.

    It has no attributes.
-->
<!ELEMENT predetach	(#PCDATA) >
<!ATTLIST predetach>

<!--
  detach

    Path to a hook that will perform any necessary processing on
    a zone to allow it to be detached.  The zone will be in the "installed"
    state when this hook is run.

    It is possible that if the zone fails to detach while running this
    hook, future attempts to detach the zone will invoke this hook again.
    So this hook should be designed to gracefully handle the case where
    it is run multiple times on the same zone.  If this hook exits with
    a non-zero exit status, the detach operation will fail and the zone will
    be left in the "installed" state, otherwise the state will be changed
    to "configured".

    The following replacements are performed:

      %z	Name of zone
      %R	Zonepath of zone
      Additional arguments, if any, are appended.

    If no hook is provided, the internal zoneadm detach code will be used.

    It has no attributes.
-->
<!ELEMENT detach	(#PCDATA) >
<!ATTLIST detach>

<!--
  clone
    Path to a hook that will perform any necessary processing on a zone to
    allow it to be installed via cloning.  Cloning is an alternative to
    installing so this hook should result in the same effect for the zone.
    The zone will be in the "incomplete" state when this hook is run.

    If this hook exits with a non-zero exit status, the clone operation
    will fail and the zone will be left in the "incomplete" state, otherwise
    the state will be changed to "installed".

    The following replacements are performed:

      %z	Name of zone
      %R	Zonepath of zone
      1st arg   name of source zone
      Additional arguments, if any, are appended.

    If no hook is provided, the internal zoneadm cloning code will be used.
-->
<!ELEMENT clone	(#PCDATA) >
<!ATTLIST clone>

<!--
  preuninstall

    Path to a script that will perform any necessary pre-processing on
    a zone before it is uninstalled.  The zone will be in the "installed"
    state when this hook is run.

    It is possible that if the zone fails to uninstall after invoking this
    hook, future attempts to uninstall the zone will invoke this hook
    again.  So this hook should be designed to gracefully handle the case
    where it is run multiple times on the same zone.  If this hook exits
    with a non-zero exit status, the uninstall operation will fail.

    The following replacements are performed:

      %z	Name of zone
      %R	Zonepath of zone
      Additional arguments, if any, are appended.

    It has no attributes.
-->
<!ELEMENT preuninstall	(#PCDATA) >
<!ATTLIST preuninstall>

<!--
  uninstall
    Identifies the hook to invoke when uninstalling a zone.  The zone will
    be in the "incomplete" state when this hook is run.

    If this hook exits with a non-zero exit status, the uninstall operation
    will fail and the zone will be left in the "incomplete" state, otherwise
    the state will be changed to "configured".

    The following replacements are performed:

      %z	Name of zone
      %R	Zonepath of zone
      Additional arguments, if any, are appended.

    If no hook is provided, the internal zoneadm uninstall code will be used.
-->
<!ELEMENT uninstall	(#PCDATA) >
<!ATTLIST uninstall>

<!--
  presnap
    Identifies the hook to invoke before snapshotting a zone using the
    built-in ZFS clone support.

    If this hook exits with a non-zero exit status, the snapshot operation
    will fail and the zfs clone operation will fail.

    The following replacements are performed:

      %z	Name of zone
      %R	Zonepath of zone
-->
<!ELEMENT presnap	(#PCDATA) >
<!ATTLIST presnap>

<!--
  postsnap
    Identifies the hook to invoke after snapshotting a zone using the
    built-in ZFS clone support.

    If this hook exits with a non-zero exit status, the zfs clone operation
    will fail.

    The following replacements are performed:

      %z	Name of zone
      %R	Zonepath of zone
-->
<!ELEMENT postsnap	(#PCDATA) >
<!ATTLIST postsnap>

<!--
  validatesnap
    Identifies the hook to invoke to validate a snapshot of a zone using the
    built-in ZFS clone support.  This will validate a snapshot that was
    explicitly specified to the clone command when the user wants to
    re-use a snapshot from an earlier clone operation.

    If this hook exits with a non-zero exit status, the snapshot validation
    operation will fail, meaning the zfs snapshot cannot be used to install
    the zone.

    The following replacements are performed:

      %z	Name of zone
      %R	Zonepath of zone
      1st arg   snapshot name
      2nd arg   snapshot path
-->
<!ELEMENT validatesnap	(#PCDATA) >
<!ATTLIST validatesnap>

<!--
  prestatechange
    Identifies the hook to invoke before zoneadmd makes a state change.
    If this hook exits with a non-zero exit status, the action failed
    and no further state change activity will take place.

    The following replacements are performed:

      %z	Name of zone
      %R	Zonepath of zone
      1st arg   integer representing current state of zone
			2 - installed
			3 - ready
			4 - running
			5 - shutting down
			6 - down
			7 - mounted
      2nd arg   integer representing transition command
			0 - ready
			1 - boot
			4 - halt
      3rd arg   Alternate root (zonepath is mounted under this root)
			empty string if zone not mounted under alternate root
-->
<!ELEMENT prestatechange	(#PCDATA) >
<!ATTLIST prestatechange>

<!--
  poststatechange
    Identifies the hook to invoke after zoneadmd makes a successful state
    change.  If this hook exits with a non-zero exit status, the action failed
    and zoneadmd treats the overall state change as failed, although
    all of the actions up to running the hook will have taken place.

    The following replacements are performed:

      %z	Name of zone
      %R	Zonepath of zone
    See prestatechange comment for 1st, 2nd and 3rd argument values.
-->
<!ELEMENT poststatechange	(#PCDATA) >
<!ATTLIST poststatechange>

<!--
  query
    Identifies a hook which can be called to get brand-specific information
    about the zone.  There is no specific place in zones where this is called,
    calls within the zone infrastructure can be added as needed.

    One example of the use of this hook is to query the implicit ZFS datasets
    supported by the brand.

    If this hook exits with a non-zero exit status, the query failed,
    although in general, this hook shouldn't return non-zero.

    The following replacements are performed:

      %z	Name of zone
      %R	Zonepath of zone
      1st arg   Arbitrary string which the hook can use to determine what
		data to return.  Brands implementing this hook should be
		tolerant of arguments they don't support and simply do
		nothing.
-->
<!ELEMENT query	(#PCDATA) >
<!ATTLIST query>

<!--
  security-flags
    Specifies the default security flags applied to zones of this brand. This
    set can be overridden via security-flags in zonecfg(8).
-->
<!ELEMENT security-flags	(#PCDATA) >
<!ATTLIST security-flags>

<!--
  privilege

    Add a privilege to the default, prohibited, or required set for all
    zones of this brand with ip-type matched.  If a privilege is added
    to the default set all zones of this brand with ip-type matched on
    the system will inherit this privilege unless the privilege is
    removed via limitpriv in zonecfg(8).  If a privilege is added to
    the prohibited set it can not be added to any zones with ip-type
    matched via limitpriv in zonecfg(8).  If a privilege is added to
    the required set then all zones of this brand with ip-type matched
    on the system will inherit this privilege and it can't be removed via
    limitpriv in zonecfg(8).

    Its attributes are
      set	The name of the set the privilege should go into.
      name	The name of the privilege.
      ip-type	Optional, indicates that adding of the privilege to the
		set only applies to certain IP types. Can be "shared" or
		"exclusive". If it is not specified, the default value
		"all" will be used, which means it is applicable regardless
		the IP type.

-->
<!ELEMENT privilege	(#PCDATA) >
<!ATTLIST privilege	set	( default | prohibited | required ) #REQUIRED
			name	CDATA #REQUIRED
			ip-type ( shared | exclusive ) "all" >

<!--
  brand

    The toplevel container for a brand configuration.

    Its attributes are

      name	The name of the brand.  This must match the name of the
		directory in which the configuration file is stored.
-->

<!ELEMENT brand		(modname?, initname, restartinit?,
			restartinit0?, restartinitreboot?,
			login_cmd,
			forcedlogin_cmd, user_cmd, install,
			installopts?, boot?, sysboot?, halt?, shutdown?,
			verify_cfg?, verify_adm?, postattach?, postclone?,
			postinstall?, predetach?, attach?, detach?, clone?,
			presnap?, postsnap?, validatesnap?,
			preuninstall?, uninstall?,
			prestatechange?, poststatechange?, query?,
			security-flags?, privilege+)>

<!ATTLIST brand		name		CDATA #REQUIRED>
<?xml version='1.0' encoding='UTF-8' ?>

<!--
 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.

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

 DO NOT EDIT THIS FILE.
-->

<!--
  device

    Defines a device (or set of devices) to be exported into the zone.

    Its attributes are

      match	Pattern to match under /dev.  Follows fnmatch(3c) rules.
		The following replacements are performed:
			%z	Name of zone

      name	Name of device in non-global zone.  This is optional; the
		default is the same name as the global zone.

      arch	Identifies devices only available for certain architectures.
		Can be "sparc" or "i386".

      ip-type	Optional, identifies devices only available for certain IP
		types. Can be "shared" or "exclusive". If it's not specified,
		the default value "all" will be used, which means it's
		available regardless the IP type.

    For example, the following entry:
	<device match="brand/windows/foo" name="bar" arch="sparc" />
    would result in mapping the following global zone device:
	/dev/brand/windows/foo
    into the zone (disregarding its IP type) as:
	/dev/bar
    but the mapping would only exist on sparc machines.

-->
<!ELEMENT device	EMPTY >

<!ATTLIST device	match	CDATA #REQUIRED
			name	CDATA ""
			arch	( sparc | i386 ) "all" 
			ip-type	( shared | exclusive ) "all" >

<!--
  symlink

    Defines a symlink to be created under /dev.

    Its attributes are

      source	Link source

      target	Link target
-->
<!ELEMENT symlink	EMPTY >

<!ATTLIST symlink	source	CDATA #REQUIRED
			target	CDATA #REQUIRED >

<!--
  global_mount

    Describes a filesystem that must be mounted before the zone is booted.
    This mount is performed by a thread executing in the the context of
    the global zone.

    Its attributes are

      special	The special device as used by the mount command.
		This path is relative to the global zone.
		The following replacements are performed:
			%R	Root of zone

      directory	The directory where it will be mounted.
		This path is relative to the non-global zone.

      type	The filesystem type
-->
<!ELEMENT global_mount	EMPTY >

<!ATTLIST global_mount	special		CDATA #REQUIRED
			directory	CDATA #REQUIRED
			opt		CDATA ""
			type		CDATA #REQUIRED>

<!--
  mount

    Describes a filesystem that must be mounted before the zone is booted.
    This mount is performed by a thread executing in the the context of
    the non-global zone.

    Its attributes are

      special	The special device as used by the mount command
		This path is relative to the non-global zone.

      directory	The directory where it will be mounted.
		This path is relative to the non-global zone.

      type	The filesystem type
-->
<!ELEMENT mount	EMPTY >

<!ATTLIST mount	special		CDATA #REQUIRED
				directory	CDATA #REQUIRED
				opt		CDATA ""
				type		CDATA #REQUIRED>

<!--
  platform

    The toplevel container for a virtual platform configuration.  The virtual
    platform describes the basic elements to bring up the necessary services
    (filesystems, devices, etc) to boot the zone.

    Its attributes are

      name	The name of the brand.  This must match the name of the
		directory in which this file is stored, as well as the name
		of the brand that refers to it.
      allow-exclusive-ip	Whether the zones of this brand can have their
				own exclusive IP stack. It is a boolean value. 
-->
<!ELEMENT platform	(device | global_mount | mount | symlink)* >

<!ATTLIST platform	name			CDATA #REQUIRED
			allow-exclusive-ip	(true | false) #REQUIRED>