|
root / base / usr / src / cmd / auditconfig / auditconfig.c
auditconfig.c C 2958 lines 66.0 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
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
/*
 * 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) 1992, 2010, Oracle and/or its affiliates. All rights reserved.
 */

/*
 * Copyright (c) 2019, Joyent, Inc.
 */

/*
 * auditconfig - set and display audit parameters
 */

#include <locale.h>
#include <sys/types.h>
#include <ctype.h>
#include <stdlib.h>
#include <stdarg.h>
#include <unistd.h>
#include <errno.h>
#include <sys/param.h>
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <nlist.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/mkdev.h>
#include <sys/param.h>
#include <pwd.h>
#include <libintl.h>
#include <zone.h>
#include <libscf_priv.h>
#include <tsol/label.h>
#include <bsm/libbsm.h>
#include <audit_policy.h>
#include <audit_scf.h>

enum	commands {
	AC_ARG_ACONF,
	AC_ARG_AUDIT,
	AC_ARG_CHKACONF,
	AC_ARG_CHKCONF,
	AC_ARG_CONF,
	AC_ARG_GETASID,
	AC_ARG_GETAUDIT,
	AC_ARG_GETAUID,
	AC_ARG_GETCAR,
	AC_ARG_GETCLASS,
	AC_ARG_GETCOND,
	AC_ARG_GETCWD,
	AC_ARG_GETESTATE,
	AC_ARG_GETFLAGS,
	AC_ARG_GETKAUDIT,
	AC_ARG_GETKMASK,
	AC_ARG_GETNAFLAGS,
	AC_ARG_GETPINFO,
	AC_ARG_GETPLUGIN,
	AC_ARG_GETPOLICY,
	AC_ARG_GETQBUFSZ,
	AC_ARG_GETQCTRL,
	AC_ARG_GETQDELAY,
	AC_ARG_GETQHIWATER,
	AC_ARG_GETQLOWATER,
	AC_ARG_GETSTAT,
	AC_ARG_GETTERMID,
	AC_ARG_LSEVENT,
	AC_ARG_LSPOLICY,
	AC_ARG_SETASID,
	AC_ARG_SETAUDIT,
	AC_ARG_SETAUID,
	AC_ARG_SETCLASS,
	AC_ARG_SETFLAGS,
	AC_ARG_SETKAUDIT,
	AC_ARG_SETKMASK,
	AC_ARG_SETNAFLAGS,
	AC_ARG_SETPLUGIN,
	AC_ARG_SETPMASK,
	AC_ARG_SETPOLICY,
	AC_ARG_SETQBUFSZ,
	AC_ARG_SETQCTRL,
	AC_ARG_SETQDELAY,
	AC_ARG_SETQHIWATER,
	AC_ARG_SETQLOWATER,
	AC_ARG_SETSMASK,
	AC_ARG_SETSTAT,
	AC_ARG_SETUMASK,
	AC_ARG_SET_TEMPORARY
};

#define	AC_KERN_EVENT		0
#define	AC_USER_EVENT		1

#define	NONE(s) (!strlen(s) ? gettext("none") : s)

#define	ONEK 1024

/*
 * remove this after the audit.h is fixed
 */
struct arg_entry {
	char		*arg_str;
	char		*arg_opts;
	enum commands	auditconfig_cmd;
	boolean_t	temporary_allowed;	/* -t allowed for the option */
};
typedef struct arg_entry arg_entry_t;

/* arg_table - command option and usage message table */
static arg_entry_t arg_table[] = {
	{ "-aconf",	"",			AC_ARG_ACONF,	B_FALSE},
	{ "-audit",	" event sorf retval string", AC_ARG_AUDIT, B_FALSE},
	{ "-chkaconf",	"",			AC_ARG_CHKACONF, B_FALSE},
	{ "-chkconf",	"",			AC_ARG_CHKCONF,	B_FALSE},
	{ "-conf",	"",			AC_ARG_CONF,	B_FALSE},
	{ "-getasid",	"",			AC_ARG_GETASID,	B_FALSE},
	{ "-getaudit",	"",			AC_ARG_GETAUDIT, B_FALSE},
	{ "-getauid",	"",			AC_ARG_GETAUID, B_FALSE},
	{ "-getcar",	"",			AC_ARG_GETCAR,	B_FALSE},
	{ "-getclass",	" event",		AC_ARG_GETCLASS, B_FALSE},
	{ "-getcond",	"",			AC_ARG_GETCOND,	B_FALSE},
	{ "-getcwd",	"",			AC_ARG_GETCWD,	B_FALSE},
	{ "-getestate",	" event",		AC_ARG_GETESTATE, B_FALSE},
	{ "-getflags",	"",			AC_ARG_GETFLAGS, B_FALSE},
	{ "-getkaudit",	"",			AC_ARG_GETKAUDIT, B_FALSE},
	{ "-getkmask",	"",			AC_ARG_GETKMASK, B_FALSE},
	{ "-getnaflags", "",			AC_ARG_GETNAFLAGS, B_FALSE},
	{ "-getpinfo",	" pid",			AC_ARG_GETPINFO, B_FALSE},
	{ "-getplugin",	" [plugin]",		AC_ARG_GETPLUGIN, B_FALSE},
	{ "-getpolicy",	"",			AC_ARG_GETPOLICY, B_TRUE},
	{ "-getqbufsz",	"",			AC_ARG_GETQBUFSZ, B_TRUE},
	{ "-getqctrl",	"",			AC_ARG_GETQCTRL, B_TRUE},
	{ "-getqdelay",	"",			AC_ARG_GETQDELAY, B_TRUE},
	{ "-getqhiwater", "",			AC_ARG_GETQHIWATER, B_TRUE},
	{ "-getqlowater", "",			AC_ARG_GETQLOWATER, B_TRUE},
	{ "-getstat",	"",			AC_ARG_GETSTAT,	B_FALSE},
	{ "-gettid",	"",			AC_ARG_GETTERMID, B_FALSE},
	{ "-lsevent",	"",			AC_ARG_LSEVENT,	B_FALSE},
	{ "-lspolicy",	"",			AC_ARG_LSPOLICY, B_FALSE},
	{ "-setasid",	" asid [cmd]",		AC_ARG_SETASID,	B_FALSE},
	{ "-setaudit",	" auid audit_flags termid asid [cmd]",
						AC_ARG_SETAUDIT, B_FALSE},
	{ "-setauid",	" auid [cmd]",		AC_ARG_SETAUID,	B_FALSE},
	{ "-setclass",	" event audit_flags",	AC_ARG_SETCLASS, B_FALSE},
	{ "-setflags",	" audit_flags",		AC_ARG_SETFLAGS, B_FALSE},
	{ "-setkaudit",	" type IP_address",	AC_ARG_SETKAUDIT, B_FALSE},
	{ "-setkmask",	" audit_flags",		AC_ARG_SETKMASK, B_FALSE},
	{ "-setnaflags", " audit_naflags",	AC_ARG_SETNAFLAGS, B_FALSE},
	{ "-setplugin",	" name active|inactive [attributes [qsize]]",
						AC_ARG_SETPLUGIN, B_FALSE},
	{ "-setpmask",	" pid audit_flags",	AC_ARG_SETPMASK, B_FALSE},
	{ "-setpolicy",	" [+|-]policy_flags",	AC_ARG_SETPOLICY, B_TRUE},
	{ "-setqbufsz",	" bufsz",		AC_ARG_SETQBUFSZ, B_TRUE},
	{ "-setqctrl",	" hiwater lowater bufsz delay",
						AC_ARG_SETQCTRL, B_TRUE},
	{ "-setqdelay",	" delay",		AC_ARG_SETQDELAY, B_TRUE},
	{ "-setqhiwater", " hiwater",		AC_ARG_SETQHIWATER, B_TRUE},
	{ "-setqlowater", " lowater",		AC_ARG_SETQLOWATER, B_TRUE},
	{ "-setsmask",	" asid audit_flags",	AC_ARG_SETSMASK, B_FALSE},
	{ "-setstat",	"",			AC_ARG_SETSTAT, B_FALSE},
	{ "-setumask",	" user audit_flags",	AC_ARG_SETUMASK, B_FALSE},
	{ "-t",		"",			AC_ARG_SET_TEMPORARY, B_FALSE},
};

#define	ARG_TBL_SZ (sizeof (arg_table) / sizeof (arg_entry_t))

char	*progname = "auditconfig";

/*
 * temporary_set true to get/set only kernel settings,
 *		 false to get/set kernel settings and service properties
 */
static boolean_t temporary_set = B_FALSE;

static au_event_ent_t *egetauevnam(char *event_name);
static au_event_ent_t *egetauevnum(au_event_t event_number);
static int arg_ent_compare(const void *aep1, const void *aep2);
static char *cond2str(void);
static int policy2str(uint32_t policy, char *policy_str, size_t len);
static int str2type(char *s, uint_t *type);
static int str2policy(char *policy_str, uint32_t *policy_mask);
static int str2ipaddr(char *s, uint32_t *addr, uint32_t type);
static int strisipaddr(char *s);
static int strisnum(char *s);
static arg_entry_t *get_arg_ent(char *arg_str);
static uid_t get_user_id(char *user);
static void chk_arg_len(char *argv, uint_t len);
static void chk_event_num(int etype, au_event_t event);
static void chk_event_str(int etype, char *event_str);
static void chk_known_plugin(char *plugin_str);
static void chk_retval(char *retval_str);
static void chk_sorf(char *sorf_str);
static void do_aconf(void);
static void do_args(char **argv, au_mask_t *mask);
static void do_audit(char *, char, int, char *);
static void do_chkaconf(void);
static void do_chkconf(void);
static void do_conf(void);
static void do_getasid(void);
static void do_getaudit(void);
static void do_getkaudit(void);
static void do_setkaudit(char *t, char *s);
static void do_getauid(void);
static void do_getcar(void);
static void do_getclass(char *event_str);
static void do_getcond(void);
static void do_getcwd(void);
static void do_getflags(void);
static void do_getkmask(void);
static void do_getnaflags(void);
static void do_getpinfo(char *pid_str);
static void do_getplugin(char *plugin_str);
static void do_getpolicy(void);
static void do_getqbufsz(void);
static void do_getqctrl(void);
static void do_getqdelay(void);
static void do_getqhiwater(void);
static void do_getqlowater(void);
static void do_getstat(void);
static void do_gettermid(void);
static void do_lsevent(void);
static void do_lspolicy(void);
static void do_setasid(char *sid_str, char **argv);
static void do_setaudit(char *user_str, char *mask_str, char *tid_str,
    char *sid_str, char **argv);
static void do_setauid(char *user, char **argv);
static void do_setclass(char *event_str, au_mask_t *mask);
static void do_setflags(char *audit_flags, au_mask_t *amask);
static void do_setkmask(au_mask_t *pmask);
static void do_setnaflags(char *audit_naflags, au_mask_t *namask);
static void do_setpmask(char *pid_str, au_mask_t *mask);
static void do_setsmask(char *asid_str, au_mask_t *mask);
static void do_setumask(char *auid_str, au_mask_t *mask);
static void do_setplugin(char *plugin_str, boolean_t plugin_state,
    char *plugin_attr, int plugin_qsize);
static void do_setpolicy(char *policy_str);
static void do_setqbufsz(char *bufsz);
static void do_setqctrl(char *hiwater, char *lowater, char *bufsz, char *delay);
static void do_setqdelay(char *delay);
static void do_setqhiwater(char *hiwater);
static void do_setqlowater(char *lowater);
static void do_setstat(void);
static void str2tid(char *tid_str, au_tid_addr_t *tp);

static void eauditon(int cmd, caddr_t data, int length);
static void echkflags(char *auditflags, au_mask_t *mask);
static void egetaudit(auditinfo_addr_t *ai, int size);
static void egetauditflagsbin(char *auditflags, au_mask_t *pmask);
static void egetauid(au_id_t *auid);
static void egetkaudit(auditinfo_addr_t *ai, int size);
static void esetaudit(auditinfo_addr_t *ai, int size);
static void esetauid(au_id_t *auid);
static void esetkaudit(auditinfo_addr_t *ai, int size);
static void execit(char **argv);
static void exit_error(char *fmt, ...);
static void exit_usage(int status);
static void parse_args(int argc, char **argv, au_mask_t *mask);
static void print_asid(au_asid_t asid);
static void print_auid(au_id_t auid);
static void print_mask(char *desc, au_mask_t *pmp);
static void print_plugin(char *plugin_name, kva_t *plugin_kva);
static void print_tid_ex(au_tid_addr_t *tidp);

#if !defined(TEXT_DOMAIN)
#define	TEXT_DOMAIN	"SUNW_OST_OSCMD"
#endif

int
main(int argc, char **argv)
{
	au_mask_t mask;			/* for options manipulating flags */

	(void) setlocale(LC_ALL, "");
	(void) textdomain(TEXT_DOMAIN);

	if (argc == 1) {
		exit_usage(0);
	}

	if (argc == 2 &&
	    (argv[1][0] == '?' ||
	    strcmp(argv[1], "-h") == 0 ||
	    strcmp(argv[1], "-?") == 0)) {
		exit_usage(0);
	}

	parse_args(argc, argv, &mask);
	do_args(argv, &mask);

	return (0);
}

/*
 * parse_args()
 *     Desc: Checks command line argument syntax.
 *     Inputs: Command line argv;
 *     Returns: If a syntax error is detected, a usage message is printed
 *              and exit() is called. If a syntax error is not detected,
 *              parse_args() returns without a value.
 */
static void
parse_args(int argc, char **argv, au_mask_t *mask)
{
	arg_entry_t *ae;

	uint_t type;
	uint_t addr[4];

	for (++argv; *argv; argv++) {
		if ((ae = get_arg_ent(*argv)) == NULL) {
			exit_usage(1);
		}

		switch (ae->auditconfig_cmd) {

		case AC_ARG_AUDIT:
			++argv;
			if (!*argv)
				exit_usage(1);
			if (strisnum(*argv)) {
				chk_event_num(AC_USER_EVENT,
				    (au_event_t)atol(*argv));
			} else {
				chk_event_str(AC_USER_EVENT, *argv);
			}
			++argv;
			if (!*argv)
				exit_usage(1);
			chk_sorf(*argv);
			++argv;
			if (!*argv)
				exit_usage(1);
			chk_retval(*argv);
			++argv;
			if (!*argv)
				exit_usage(1);
			break;

		case AC_ARG_CHKCONF:
		case AC_ARG_CONF:
		case AC_ARG_ACONF:
		case AC_ARG_CHKACONF:
		case AC_ARG_GETASID:
		case AC_ARG_GETAUID:
		case AC_ARG_GETAUDIT:
		case AC_ARG_GETKAUDIT:
			break;

		case AC_ARG_GETCLASS:
		case AC_ARG_GETESTATE:
			++argv;
			if (!*argv)
				exit_usage(1);
			if (strisnum(*argv)) {
				chk_event_num(AC_KERN_EVENT,
				    (au_event_t)atol(*argv));
			} else {
				chk_event_str(AC_KERN_EVENT, *argv);
			}
			break;

		case AC_ARG_GETCAR:
		case AC_ARG_GETCOND:
		case AC_ARG_GETCWD:
		case AC_ARG_GETFLAGS:
		case AC_ARG_GETKMASK:
		case AC_ARG_GETNAFLAGS:
			break;

		case AC_ARG_GETPLUGIN:
			if (*++argv == NULL) {
				--argv;
				break;
			}
			if (get_arg_ent(*argv) != NULL) {
				--argv;
			} else {
				chk_arg_len(*argv, PLUGIN_MAXBUF);
				chk_known_plugin(*argv);
			}
			break;

		case AC_ARG_GETPOLICY:
		case AC_ARG_GETQBUFSZ:
		case AC_ARG_GETQCTRL:
		case AC_ARG_GETQDELAY:
		case AC_ARG_GETQHIWATER:
		case AC_ARG_GETQLOWATER:
		case AC_ARG_GETSTAT:
		case AC_ARG_GETTERMID:
		case AC_ARG_LSEVENT:
		case AC_ARG_LSPOLICY:
			break;

		case AC_ARG_SETASID:
		case AC_ARG_SETAUID:
		case AC_ARG_SETAUDIT:
			++argv;
			if (!*argv)
				exit_usage(1);

			while (*argv)
				++argv;
			--argv;

			break;

		case AC_ARG_SETKAUDIT:
			++argv;
			if (!*argv)
				exit_usage(1);
			if (str2type (*argv, &type))
				exit_error(gettext(
				    "Invalid IP address type specified."));
			++argv;
			if (!*argv)
				exit_usage(1);

			if (str2ipaddr(*argv, addr, type))
				exit_error(
				    gettext("Invalid IP address specified."));
			break;

		case AC_ARG_SETCLASS:
			++argv;
			if (!*argv)
				exit_usage(1);
			if (strisnum(*argv))
				chk_event_num(AC_KERN_EVENT,
				    (au_event_t)atol(*argv));
			else
				chk_event_str(AC_KERN_EVENT, *argv);
			++argv;
			if (!*argv)
				exit_usage(1);
			echkflags(*argv, mask);
			break;

		case AC_ARG_SETFLAGS:
			++argv;
			if (!*argv)
				exit_usage(1);
			chk_arg_len(*argv, PRESELECTION_MAXBUF);
			echkflags(*argv, mask);
			break;

		case AC_ARG_SETKMASK:
			++argv;
			if (!*argv)
				exit_usage(1);
			echkflags(*argv, mask);
			break;

		case AC_ARG_SETNAFLAGS:
			++argv;
			if (!*argv)
				exit_usage(1);
			chk_arg_len(*argv, PRESELECTION_MAXBUF);
			echkflags(*argv, mask);
			break;

		case AC_ARG_SETPLUGIN:
			if (*++argv == NULL || get_arg_ent(*argv) != NULL) {
				exit_usage(1);
			}
			chk_known_plugin(*argv);
			chk_arg_len(*argv, PLUGIN_MAXBUF);
			if (*++argv == NULL || strcmp(*argv, "active") != 0 &&
			    strcmp(*argv, "inactive") != 0) {
				exit_usage(1);
			}
			if (*++argv == NULL || get_arg_ent(*argv) != NULL) {
				--argv;
				break;
			}
			chk_arg_len(*argv, PLUGIN_MAXATT);
			if (*++argv == NULL || get_arg_ent(*argv) != NULL) {
				--argv;
				break;
			}
			if (atoi(*argv) < 0) {
				exit_error(gettext("Incorrect qsize specified "
				    "(%s)."), *argv);
			}
			break;

		case AC_ARG_SETPOLICY:
			++argv;
			if (!*argv)
				exit_usage(1);
			break;

		case AC_ARG_SETSTAT:
			break;

		case AC_ARG_GETPINFO:
			++argv;
			if (!*argv)
				exit_usage(1);
			break;

		case AC_ARG_SETPMASK:
			++argv;
			if (!*argv)
				exit_usage(1);
			++argv;
			if (!*argv)
				exit_usage(1);
			echkflags(*argv, mask);
			break;

		case AC_ARG_SETQBUFSZ:
			++argv;
			if (!*argv)
				exit_usage(1);
			if (!strisnum(*argv))
				exit_error(gettext("Invalid bufsz specified."));
			break;

		case AC_ARG_SETQCTRL:
			++argv;
			if (!*argv)
				exit_usage(1);
			if (!strisnum(*argv))
				exit_error(
				    gettext("Invalid hiwater specified."));
			++argv;
			if (!*argv)
				exit_usage(1);
			if (!strisnum(*argv))
				exit_error(
				    gettext("Invalid lowater specified."));
			++argv;
			if (!*argv)
				exit_usage(1);
			if (!strisnum(*argv))
				exit_error(gettext("Invalid bufsz specified."));
			++argv;
			if (!*argv)
				exit_usage(1);
			if (!strisnum(*argv))
				exit_error(gettext("Invalid delay specified."));
			break;

		case AC_ARG_SETQDELAY:
			++argv;
			if (!*argv)
				exit_usage(1);
			if (!strisnum(*argv))
				exit_error(gettext("Invalid delay specified."));
			break;

		case AC_ARG_SETQHIWATER:
			++argv;
			if (!*argv)
				exit_usage(1);
			if (!strisnum(*argv)) {
				exit_error(
				    gettext("Invalid hiwater specified."));
			}
			break;

		case AC_ARG_SETQLOWATER:
			++argv;
			if (!*argv)
				exit_usage(1);
			if (!strisnum(*argv)) {
				exit_error(
				    gettext("Invalid lowater specified."));
			}
			break;

		case AC_ARG_SETSMASK:
		case AC_ARG_SETUMASK:
			++argv;
			if (!*argv)
				exit_usage(1);
			++argv;
			if (!*argv)
				exit_usage(1);
			echkflags(*argv, mask);
			break;

		case AC_ARG_SET_TEMPORARY:
			/* Do not accept single -t option. */
			if (argc == 2) {
				exit_error(
				    gettext("Only the -t option specified "
				    "(it is not a standalone option)."));
			}
			temporary_set = B_TRUE;
			break;

		default:
			exit_error(gettext("Internal error #1."));
			break;
		}
	}
}


/*
 * do_args() - do command line arguments in the order in which they appear.
 * Function return values returned by the underlying functions; the semantics
 * they should follow is to return B_TRUE on successful execution, B_FALSE
 * otherwise.
 */
static void
do_args(char **argv, au_mask_t *mask)
{
	arg_entry_t	*ae;

	for (++argv; *argv; argv++) {
		ae = get_arg_ent(*argv);

		switch (ae->auditconfig_cmd) {

		case AC_ARG_AUDIT:
			{
				char sorf;
				int  retval;
				char *event_name;
				char *audit_str;

				++argv;
				event_name = *argv;
				++argv;
				sorf = (char)atoi(*argv);
				++argv;
				retval = atoi(*argv);
				++argv;
				audit_str = *argv;
				do_audit(event_name, sorf, retval, audit_str);
			}
			break;

		case AC_ARG_CHKCONF:
			do_chkconf();
			break;

		case AC_ARG_CONF:
			do_conf();
			break;

		case AC_ARG_CHKACONF:
			do_chkaconf();
			break;

		case AC_ARG_ACONF:
			do_aconf();
			break;

		case AC_ARG_GETASID:
			do_getasid();
			break;

		case AC_ARG_GETAUID:
			do_getauid();
			break;

		case AC_ARG_GETAUDIT:
			do_getaudit();
			break;

		case AC_ARG_GETKAUDIT:
			do_getkaudit();
			break;

		case AC_ARG_GETCLASS:
		case AC_ARG_GETESTATE:
			++argv;
			do_getclass(*argv);
			break;

		case AC_ARG_GETCAR:
			do_getcar();
			break;

		case AC_ARG_GETCOND:
			do_getcond();
			break;

		case AC_ARG_GETCWD:
			do_getcwd();
			break;

		case AC_ARG_GETFLAGS:
			do_getflags();
			break;

		case AC_ARG_GETKMASK:
			do_getkmask();
			break;

		case AC_ARG_GETNAFLAGS:
			do_getnaflags();
			break;

		case AC_ARG_GETPLUGIN:
			{
				char	*plugin_str = NULL;

				++argv;
				if (*argv != NULL) {
					if (get_arg_ent(*argv) != NULL) {
						--argv;
					} else {
						plugin_str = *argv;
					}
				} else {
					--argv;
				}

				do_getplugin(plugin_str);
			}
			break;

		case AC_ARG_GETPOLICY:
			do_getpolicy();
			break;

		case AC_ARG_GETQBUFSZ:
			do_getqbufsz();
			break;

		case AC_ARG_GETQCTRL:
			do_getqctrl();
			break;

		case AC_ARG_GETQDELAY:
			do_getqdelay();
			break;

		case AC_ARG_GETQHIWATER:
			do_getqhiwater();
			break;

		case AC_ARG_GETQLOWATER:
			do_getqlowater();
			break;

		case AC_ARG_GETSTAT:
			do_getstat();
			break;

		case AC_ARG_GETTERMID:
			do_gettermid();
			break;

		case AC_ARG_LSEVENT:
			do_lsevent();
			break;

		case AC_ARG_LSPOLICY:
			do_lspolicy();
			break;

		case AC_ARG_SETASID:
			{
				char *sid_str;

				++argv;
				sid_str = *argv;
				++argv;
				do_setasid(sid_str, argv);
			}
			break;

		case AC_ARG_SETAUID:
			{
				char *user;

				++argv;
				user = *argv;
				++argv;
				do_setauid(user, argv);
			}
			break;

		case AC_ARG_SETAUDIT:
			{
				char *user_str;
				char *mask_str;
				char *tid_str;
				char *sid_str;

				++argv;
				user_str = *argv;
				++argv;
				mask_str = *argv;
				++argv;
				tid_str = *argv;
				++argv;
				sid_str = *argv;
				++argv;
				do_setaudit(user_str, mask_str, tid_str,
				    sid_str, argv);
			}
			break;

		case AC_ARG_SETKAUDIT:
			{
				char *address_type, *address;

				++argv; address_type = *argv;
				++argv; address = *argv;
				do_setkaudit(address_type, address);
			}
			break;

		case AC_ARG_SETCLASS:
			{
				char *event_str;

				++argv;
				event_str = *argv;
				do_setclass(event_str, mask);

				++argv;
			}
			break;

		case AC_ARG_SETFLAGS:
			++argv;
			do_setflags(*argv, mask);
			break;

		case AC_ARG_SETKMASK:
			++argv;
			do_setkmask(mask);
			break;

		case AC_ARG_SETNAFLAGS:
			++argv;
			do_setnaflags(*argv, mask);
			break;

		case AC_ARG_SETPLUGIN:
			{
				char		*plugin_str = NULL;
				boolean_t	plugin_state = B_FALSE;
				char		*plugin_att = NULL;
				int		plugin_qsize = -1;

				plugin_str = *++argv;
				if (strcmp(*++argv, "active") == 0) {
					plugin_state = B_TRUE;
				}
				if (*++argv != NULL &&
				    get_arg_ent(*argv) == NULL) {
					plugin_att = *argv;
					if (*++argv != NULL &&
					    get_arg_ent(*argv) == NULL) {
						plugin_qsize = atoi(*argv);
					} else {
						--argv;
					}
				} else {
					--argv;
				}

				do_setplugin(plugin_str, plugin_state,
				    plugin_att, plugin_qsize);
			}
			break;

		case AC_ARG_SETPOLICY:
			++argv;
			do_setpolicy(*argv);
			break;

		case AC_ARG_GETPINFO:
			{
				char *pid_str;

				++argv;
				pid_str = *argv;
				do_getpinfo(pid_str);
			}
			break;

		case AC_ARG_SETPMASK:
			{
				char *pid_str;

				++argv;
				pid_str = *argv;
				do_setpmask(pid_str, mask);

				++argv;
			}
			break;

		case AC_ARG_SETSTAT:
			do_setstat();
			break;

		case AC_ARG_SETQBUFSZ:
			++argv;
			do_setqbufsz(*argv);
			break;

		case AC_ARG_SETQCTRL:
			{
				char *hiwater, *lowater, *bufsz, *delay;

				++argv; hiwater = *argv;
				++argv; lowater = *argv;
				++argv; bufsz = *argv;
				++argv; delay = *argv;
				do_setqctrl(hiwater, lowater, bufsz, delay);
			}
			break;
		case AC_ARG_SETQDELAY:
			++argv;
			do_setqdelay(*argv);
			break;

		case AC_ARG_SETQHIWATER:
			++argv;
			do_setqhiwater(*argv);
			break;

		case AC_ARG_SETQLOWATER:
			++argv;
			do_setqlowater(*argv);
			break;

		case AC_ARG_SETSMASK:
			{
				char *asid_str;

				++argv;
				asid_str = *argv;
				do_setsmask(asid_str, mask);

				++argv;
			}
			break;
		case AC_ARG_SETUMASK:
			{
				char *auid_str;

				++argv;
				auid_str = *argv;
				do_setumask(auid_str, mask);

				++argv;
			}
			break;
		case AC_ARG_SET_TEMPORARY:
			break;

		default:
			exit_error(gettext("Internal error #2."));
			break;
		}
	}
}

/*
 * do_chkconf() - the returned value is for the global zone unless AUDIT_PERZONE
 * is set.
 */
static void
do_chkconf(void)
{
	register au_event_ent_t *evp;
	au_mask_t pmask;
	char conf_aflags[256];
	char run_aflags[256];
	au_stat_t as;
	int class;
	int			len;
	struct au_evclass_map	cmap;

	pmask.am_success = pmask.am_failure = 0;
	eauditon(A_GETSTAT, (caddr_t)&as, 0);

	setauevent();
	if (getauevent() == NULL) {
		exit_error(gettext("NO AUDIT EVENTS: Could not read %s\n."),
		    AUDITEVENTFILE);
	}

	setauevent();
	while ((evp = getauevent()) != NULL) {
		cmap.ec_number = evp->ae_number;
		len = sizeof (struct au_evclass_map);
		if (evp->ae_number <= as.as_numevent) {
			if (auditon(A_GETCLASS, (caddr_t)&cmap, len) == -1) {
				(void) printf("%s(%hu):%s",
				    evp->ae_name, evp->ae_number,
				    gettext("UNKNOWN EVENT: Could not get "
				    "class for event. Configuration may "
				    "be bad.\n"));
			} else {
				class = cmap.ec_class;
				if (class != evp->ae_class) {
					conf_aflags[0] = run_aflags[0] = '\0';
					pmask.am_success = class;
					pmask.am_failure = class;
					(void) getauditflagschar(run_aflags,
					    &pmask, 0);
					pmask.am_success = evp->ae_class;
					pmask.am_failure = evp->ae_class;
					(void) getauditflagschar(conf_aflags,
					    &pmask, 0);

					(void) printf(gettext(
					    "%s(%hu): CLASS MISMATCH: "
					    "runtime class (%s) != "
					    "configured class (%s)\n"),
					    evp->ae_name, evp->ae_number,
					    NONE(run_aflags),
					    NONE(conf_aflags));
				}
			}
		}
	}
	endauevent();
}

/*
 * do_conf() - configure the kernel events. The value returned to the user is
 * for the global zone unless AUDIT_PERZONE is set.
 */
static void
do_conf(void)
{
	register au_event_ent_t *evp;
	register int i;
	au_evclass_map_t ec;
	au_stat_t as;

	eauditon(A_GETSTAT, (caddr_t)&as, 0);

	i = 0;
	setauevent();
	while ((evp = getauevent()) != NULL) {
		if (evp->ae_number <= as.as_numevent) {
			++i;
			ec.ec_number = evp->ae_number;
			ec.ec_class = evp->ae_class;
			eauditon(A_SETCLASS, (caddr_t)&ec, sizeof (ec));
		}
	}
	endauevent();
	(void) printf(gettext("Configured %d kernel events.\n"), i);

}

/*
 * do_chkaconf() - report a mismatch if the runtime class mask of a kernel audit
 * event does not match the configured class mask. The value returned to the
 * user is for the global zone unless AUDIT_PERZONE is set.
 */
static void
do_chkaconf(void)
{
	char		*namask_cfg;
	au_mask_t	pmask, kmask;

	if (!do_getnaflags_scf(&namask_cfg) || namask_cfg == NULL) {
		exit_error(gettext("Could not get configured value."));
	}
	egetauditflagsbin(namask_cfg, &pmask);

	eauditon(A_GETKMASK, (caddr_t)&kmask, sizeof (kmask));

	if ((pmask.am_success != kmask.am_success) ||
	    (pmask.am_failure != kmask.am_failure)) {
		char kbuf[2048];
		if (getauditflagschar(kbuf, &kmask, 0) < 0) {
			free(namask_cfg);
			(void) fprintf(stderr,
			    gettext("bad kernel non-attributable mask\n"));
			exit(1);
		}
		(void) printf(
		    gettext("non-attributable event flags mismatch:\n"));
		(void) printf(gettext("active non-attributable audit flags "
		    "= %s\n"), kbuf);
		(void) printf(gettext("configured non-attributable audit flags "
		    "= %s\n"), namask_cfg);
	}
	free(namask_cfg);
}

/*
 * do_aconf - configures the non-attributable events. The value returned to the
 * user is for the global zone unless AUDIT_PERZONE is set.
 */
static void
do_aconf(void)
{
	au_mask_t	namask;
	char		*namask_cfg;

	if (!do_getnaflags_scf(&namask_cfg) || namask_cfg == NULL) {
		exit_error(gettext("Could not get configured value."));
	}
	egetauditflagsbin(namask_cfg, &namask);
	free(namask_cfg);

	eauditon(A_SETKMASK, (caddr_t)&namask, sizeof (namask));
	(void) printf(gettext("Configured non-attributable event mask.\n"));
}

/*
 * do_audit() - construct an audit record for audit event event using the
 * process's audit characteristics containing a text token string audit_str. The
 * return token is constructed from the success/failure flag sort. Returned
 * value retval is an errno value.
 */
static void
do_audit(char *event, char sorf, int retval, char *audit_str)
{
	int rtn;
	int rd;
	au_event_t event_num;
	au_event_ent_t *evp;
	auditinfo_addr_t ai;
	token_t *tokp;

	egetaudit(&ai, sizeof (ai));

	if (strisnum(event)) {
		event_num = (au_event_t)atoi(event);
		evp = egetauevnum(event_num);
	} else {
		evp = egetauevnam(event);
	}

	rtn = au_preselect(evp->ae_number, &ai.ai_mask, (int)sorf,
	    AU_PRS_USECACHE);

	if (rtn == -1) {
		exit_error("%s\n%s %hu\n",
		    gettext("Check audit event configuration."),
		    gettext("Could not get audit class for event number"),
		    evp->ae_number);
	}

	/* record is preselected */
	if (rtn == 1) {
		if ((rd = au_open()) == -1) {
			exit_error(gettext(
			    "Could not get and audit record descriptor\n"));
		}
		if ((tokp = au_to_me()) == NULL) {
			exit_error(
			    gettext("Could not allocate subject token\n"));
		}
		if (au_write(rd, tokp) == -1) {
			exit_error(gettext("Could not construct subject token "
			    "of audit record\n"));
		}
		if (is_system_labeled()) {
			if ((tokp = au_to_mylabel()) == NULL) {
				exit_error(gettext(
				    "Could not allocate label token\n"));
			}
			if (au_write(rd, tokp) == -1) {
				exit_error(gettext("Could not "
				    "construct label token of audit record\n"));
			}
		}

		if ((tokp = au_to_text(audit_str)) == NULL)
			exit_error(gettext("Could not allocate text token\n"));
		if (au_write(rd, tokp) == -1)
			exit_error(gettext("Could not construct text token of "
			    "audit record\n"));
#ifdef _LP64
		if ((tokp = au_to_return64(sorf, retval)) == NULL)
#else
		if ((tokp = au_to_return32(sorf, retval)) == NULL)
#endif
			exit_error(
			    gettext("Could not allocate return token\n"));
		if (au_write(rd, tokp) == -1) {
			exit_error(gettext("Could not construct return token "
			    "of audit record\n"));
		}
		if (au_close(rd, 1, evp->ae_number) == -1) {
			exit_error(
			    gettext("Could not write audit record: %s\n"),
			    strerror(errno));
		}
	}
}

/*
 * do_getauid() - print the audit id of the current process.
 */
static void
do_getauid(void)
{
	au_id_t auid;

	egetauid(&auid);
	print_auid(auid);
}

/*
 * do_getaudit() - print the audit characteristics of the current process.
 */
static void
do_getaudit(void)
{
	auditinfo_addr_t ai;

	egetaudit(&ai, sizeof (ai));
	print_auid(ai.ai_auid);
	print_mask(gettext("process preselection mask"), &ai.ai_mask);
	print_tid_ex(&ai.ai_termid);
	print_asid(ai.ai_asid);
}

/*
 * do_getkaudit() - print the audit characteristics of the current zone.
 */
static void
do_getkaudit(void)
{
	auditinfo_addr_t ai;

	egetkaudit(&ai, sizeof (ai));
	print_auid(ai.ai_auid);
	print_mask(gettext("process preselection mask"), &ai.ai_mask);
	print_tid_ex(&ai.ai_termid);
	print_asid(ai.ai_asid);
}

/*
 * do_setkaudit() - set IP address_type/address of machine to specified values;
 * valid per zone if AUDIT_PERZONE is set, else only in global zone.
 */
static void
do_setkaudit(char *t, char *s)
{
	uint_t type;
	auditinfo_addr_t ai;

	egetkaudit(&ai, sizeof (ai));
	(void) str2type(t, &type);
	(void) str2ipaddr(s, &ai.ai_termid.at_addr[0], type);
	ai.ai_termid.at_type = type;
	esetkaudit(&ai, sizeof (ai));
}

/*
 * do_getcar() - print the zone-relative root
 */
static void
do_getcar(void)
{
	char path[MAXPATHLEN];

	eauditon(A_GETCAR, (caddr_t)path, sizeof (path));
	(void) printf(gettext("current active root = %s\n"), path);
}

/*
 * do_getclass() - print the preselection mask associated with the specified
 * kernel audit event. The displayed value is for the global zone unless
 * AUDIT_PERZONE is set.
 */
static void
do_getclass(char *event_str)
{
	au_evclass_map_t ec;
	au_event_ent_t *evp;
	au_event_t event_number;
	char *event_name;

	if (strisnum(event_str)) {
		event_number = atol(event_str);
		if ((evp = egetauevnum(event_number)) != NULL) {
			event_number = evp->ae_number;
			event_name = evp->ae_name;
		} else {
			event_name = gettext("unknown");
		}
	} else {
		event_name = event_str;
		if ((evp = egetauevnam(event_str)) != NULL) {
			event_number = evp->ae_number;
		}
	}

	ec.ec_number = event_number;
	eauditon(A_GETCLASS, (caddr_t)&ec, 0);

	(void) printf(gettext("audit class mask for event %s(%hu) = 0x%x\n"),
	    event_name, event_number, ec.ec_class);
}

/*
 * do_getcond() - the printed value is for the global zone unless
 * AUDIT_PERZONE is set. (AUC_DISABLED is always global, the other states are
 * per zone if AUDIT_PERZONE is set)
 */
static void
do_getcond(void)
{
	(void) printf(gettext("audit condition = %s\n"), cond2str());
}

/*
 * do_getcwd() - the printed path is relative to the current zone root
 */
static void
do_getcwd(void)
{
	char path[MAXPATHLEN];

	eauditon(A_GETCWD, (caddr_t)path, sizeof (path));
	(void) printf(gettext("current working directory = %s\n"), path);
}

/*
 * do_getflags() - the printed value is for the global zone unless AUDIT_PERZONE
 * is set.
 */
static void
do_getflags(void)
{
	au_mask_t	amask;
	char		*amask_cfg;

	eauditon(A_GETAMASK, (caddr_t)&amask, sizeof (amask));
	print_mask(gettext("active user default audit flags"), &amask);

	if (!do_getflags_scf(&amask_cfg) || amask_cfg == NULL) {
		exit_error(gettext("Could not get configured value."));
	}
	egetauditflagsbin(amask_cfg, &amask);
	print_mask(gettext("configured user default audit flags"), &amask);
	free(amask_cfg);
}

/*
 * do_getkmask() - the printed value is for the global zone unless AUDIT_PERZONE
 * is set.
 */
static void
do_getkmask(void)
{
	au_mask_t pmask;

	eauditon(A_GETKMASK, (caddr_t)&pmask, sizeof (pmask));
	print_mask(gettext("active non-attributable audit flags"), &pmask);
}

/*
 * do_getnaflags() - the printed value is for the global zone unless
 * AUDIT_PERZONE is set.
 */
static void
do_getnaflags(void)
{
	au_mask_t	namask;
	char		*namask_cfg;

	eauditon(A_GETKMASK, (caddr_t)&namask, sizeof (namask));
	print_mask(gettext("active non-attributable audit flags"), &namask);

	if (!do_getnaflags_scf(&namask_cfg) || namask_cfg == NULL) {
		exit_error(gettext("Could not get configured value."));
	}
	egetauditflagsbin(namask_cfg, &namask);
	print_mask(gettext("configured non-attributable audit flags"), &namask);
	free(namask_cfg);
}

/*
 * do_getpolicy() - print active and configured kernel audit policy relative to
 * the current zone.
 */
static void
do_getpolicy(void)
{
	char			policy_str[1024];
	uint32_t		policy;

	if (!temporary_set) {
		if (!do_getpolicy_scf(&policy)) {
			exit_error(gettext("Could not get configured values."));
		}
		(void) policy2str(policy, policy_str, sizeof (policy_str));
		(void) printf(gettext("configured audit policies = %s\n"),
		    policy_str);
	}

	eauditon(A_GETPOLICY, (caddr_t)&policy, 0);
	(void) policy2str(policy, policy_str, sizeof (policy_str));
	(void) printf(gettext("active audit policies = %s\n"), policy_str);
}


/*
 * do_getpinfo() - print the audit ID, preselection mask, terminal ID, and
 * audit session ID for the specified process.
 */
static void
do_getpinfo(char *pid_str)
{
	struct auditpinfo_addr ap;

	if (strisnum(pid_str))
		ap.ap_pid = (pid_t)atoi(pid_str);
	else
		exit_usage(1);

	eauditon(A_GETPINFO_ADDR, (caddr_t)&ap, sizeof (ap));

	print_auid(ap.ap_auid);
	print_mask(gettext("process preselection mask"), &(ap.ap_mask));
	print_tid_ex(&(ap.ap_termid));
	print_asid(ap.ap_asid);
}

/*
 * do_getplugin() - print plugin configuration.
 */
static void
do_getplugin(char *plugin_str)
{
	scf_plugin_kva_node_t	*plugin_kva_ll;
	scf_plugin_kva_node_t	*plugin_kva_ll_head;

	if (!do_getpluginconfig_scf(plugin_str, &plugin_kva_ll)) {
		exit_error(gettext("Could not get plugin configuration."));
	}

	plugin_kva_ll_head = plugin_kva_ll;

	while (plugin_kva_ll != NULL) {
		print_plugin(plugin_kva_ll->plugin_name,
		    plugin_kva_ll->plugin_kva);
		plugin_kva_ll = plugin_kva_ll->next;
		if (plugin_kva_ll != NULL) {
			(void) printf("\n");
		}
	}
	plugin_kva_ll_free(plugin_kva_ll_head);
}

/*
 * do_getqbufsz() - print the active and configured audit queue write buffer
 * size relative to the current zone.
 */
static void
do_getqbufsz(void)
{
	struct au_qctrl qctrl;

	if (!temporary_set) {
		if (!do_getqbufsz_scf(&qctrl.aq_bufsz)) {
			exit_error(gettext("Could not get configured value."));
		}

		if (qctrl.aq_bufsz == 0) {
			(void) printf(gettext(
			    "no configured audit queue buffer size\n"));
		} else {
			(void) printf(gettext("configured audit queue "
			    "buffer size (bytes) = %d\n"), qctrl.aq_bufsz);
		}
	}

	eauditon(A_GETQCTRL, (caddr_t)&qctrl, 0);
	(void) printf(gettext("active audit queue buffer size (bytes) = %d\n"),
	    qctrl.aq_bufsz);
}

/*
 * do_getqctrl() - print the configured and active audit queue write buffer
 * size, audit queue hiwater mark, audit queue lowater mark, audit queue prod
 * interval (ticks) relative to the current zone.
 */
static void
do_getqctrl(void)
{
	struct au_qctrl	qctrl;

	if (!temporary_set) {
		if (!do_getqctrl_scf(&qctrl)) {
			exit_error(gettext("Could not get configured values."));
		}

		if (qctrl.aq_hiwater == 0) {
			(void) printf(gettext(
			    "no configured audit queue hiwater mark\n"));
		} else {
			(void) printf(gettext("configured audit queue "
			    "hiwater mark (records) = %d\n"), qctrl.aq_hiwater);
		}
		if (qctrl.aq_lowater == 0) {
			(void) printf(gettext(
			    "no configured audit queue lowater mark\n"));
		} else {
			(void) printf(gettext("configured audit queue "
			    "lowater mark (records) = %d\n"), qctrl.aq_lowater);
		}
		if (qctrl.aq_bufsz == 0) {
			(void) printf(gettext(
			    "no configured audit queue buffer size\n"));
		} else {
			(void) printf(gettext("configured audit queue "
			    "buffer size (bytes) = %d\n"), qctrl.aq_bufsz);
		}
		if (qctrl.aq_delay == 0) {
			(void) printf(gettext(
			    "no configured audit queue delay\n"));
		} else {
			(void) printf(gettext("configured audit queue "
			    "delay (ticks) = %ld\n"), qctrl.aq_delay);
		}
	}

	eauditon(A_GETQCTRL, (caddr_t)&qctrl, 0);
	(void) printf(gettext("active audit queue hiwater mark "
	    "(records) = %d\n"), qctrl.aq_hiwater);
	(void) printf(gettext("active audit queue lowater mark "
	    "(records) = %d\n"), qctrl.aq_lowater);
	(void) printf(gettext("active audit queue buffer size (bytes) = %d\n"),
	    qctrl.aq_bufsz);
	(void) printf(gettext("active audit queue delay (ticks) = %ld\n"),
	    qctrl.aq_delay);
}

/*
 * do_getqdelay() - print, relative to the current zone, the configured and
 * active interval at which audit queue is prodded to start output.
 */
static void
do_getqdelay(void)
{
	struct au_qctrl qctrl;

	if (!temporary_set) {
		if (!do_getqdelay_scf(&qctrl.aq_delay)) {
			exit_error(gettext("Could not get configured value."));
		}

		if (qctrl.aq_delay == 0) {
			(void) printf(gettext(
			    "no configured audit queue delay\n"));
		} else {
			(void) printf(gettext("configured audit queue "
			    "delay (ticks) = %ld\n"), qctrl.aq_delay);
		}
	}

	eauditon(A_GETQCTRL, (caddr_t)&qctrl, 0);
	(void) printf(gettext("active audit queue delay (ticks) = %ld\n"),
	    qctrl.aq_delay);
}

/*
 * do_getqhiwater() - print, relative to the current zone, the high water
 * point in undelivered audit records when audit generation will block.
 */
static void
do_getqhiwater(void)
{
	struct au_qctrl qctrl;

	if (!temporary_set) {
		if (!do_getqhiwater_scf(&qctrl.aq_hiwater)) {
			exit_error(gettext("Could not get configured value."));
		}

		if (qctrl.aq_hiwater == 0) {
			(void) printf(gettext(
			    "no configured audit queue hiwater mark\n"));
		} else {
			(void) printf(gettext("configured audit queue "
			    "hiwater mark (records) = %d\n"), qctrl.aq_hiwater);
		}
	}

	eauditon(A_GETQCTRL, (caddr_t)&qctrl, 0);
	(void) printf(gettext("active audit queue hiwater mark "
	    "(records) = %d\n"), qctrl.aq_hiwater);
}

/*
 * do_getqlowater() - print, relative to the current zone, the low water point
 * in undelivered audit records where blocked processes will resume.
 */
static void
do_getqlowater(void)
{
	struct au_qctrl qctrl;

	if (!temporary_set) {
		if (!do_getqlowater_scf(&qctrl.aq_lowater)) {
			exit_error(gettext("Could not get configured value."));
		}

		if (qctrl.aq_lowater == 0) {
			(void) printf(gettext(
			    "no configured audit queue lowater mark\n"));
		} else {
			(void) printf(gettext("configured audit queue "
			    "lowater mark (records) = %d\n"), qctrl.aq_lowater);
		}
	}

	eauditon(A_GETQCTRL, (caddr_t)&qctrl, 0);
	(void) printf(gettext("active audit queue lowater mark "
	    "(records) = %d\n"), qctrl.aq_lowater);
}

/*
 * do_getasid() - print out the audit session-ID.
 */
static void
do_getasid(void)
{
	auditinfo_addr_t ai;

	if (getaudit_addr(&ai, sizeof (ai))) {
		exit_error(gettext("getaudit_addr(2) failed"));
	}
	print_asid(ai.ai_asid);
}

/*
 * do_getstat() - the printed statistics are for the entire system unless
 * AUDIT_PERZONE is set.
 */
static void
do_getstat(void)
{
	au_stat_t as;
	int offset[12];   /* used to line the header up correctly */
	char buf[512];

	eauditon(A_GETSTAT, (caddr_t)&as, 0);
	(void) sprintf(buf, "%4lu %n%4lu %n%4lu %n%4lu %n%4lu %n%4lu %n%4lu "
	    "%n%4lu %n%4lu %n%4lu %n%4lu %n%4lu%n",
	    (ulong_t)as.as_generated,	&(offset[0]),
	    (ulong_t)as.as_nonattrib,	&(offset[1]),
	    (ulong_t)as.as_kernel,	&(offset[2]),
	    (ulong_t)as.as_audit,	&(offset[3]),
	    (ulong_t)as.as_auditctl,	&(offset[4]),
	    (ulong_t)as.as_enqueue,	&(offset[5]),
	    (ulong_t)as.as_written,	&(offset[6]),
	    (ulong_t)as.as_wblocked,	&(offset[7]),
	    (ulong_t)as.as_rblocked,	&(offset[8]),
	    (ulong_t)as.as_dropped,	&(offset[9]),
	    (ulong_t)as.as_totalsize / ONEK, &(offset[10]),
	    (ulong_t)as.as_memused / ONEK, &(offset[11]));

	/*
	 * TRANSLATION_NOTE
	 *	Print a properly aligned header.
	 */
	(void) printf("%*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s\n",
	    offset[0] - 1,		gettext("gen"),
	    offset[1] - offset[0] -1,	gettext("nona"),
	    offset[2] - offset[1] -1,	gettext("kern"),
	    offset[3] - offset[2] -1,	gettext("aud"),
	    offset[4] - offset[3] -1,	gettext("ctl"),
	    offset[5] - offset[4] -1,	gettext("enq"),
	    offset[6] - offset[5] -1,	gettext("wrtn"),
	    offset[7] - offset[6] -1,	gettext("wblk"),
	    offset[8] - offset[7] -1,	gettext("rblk"),
	    offset[9] - offset[8] -1,	gettext("drop"),
	    offset[10] - offset[9] -1,	gettext("tot"),
	    offset[11] - offset[10],	gettext("mem"));

	(void) printf("%s\n", buf);
}

/*
 * do_gettermid() - print audit terminal ID for current process.
 */
static void
do_gettermid(void)
{
	auditinfo_addr_t ai;

	if (getaudit_addr(&ai, sizeof (ai))) {
		exit_error(gettext("getaudit_addr(2) failed"));
	}
	print_tid_ex(&ai.ai_termid);
}

/*
 * do_lsevent() - display the active kernel and user level audit event
 * information. The printed events are for the global zone unless AUDIT_PERZONE
 * is set.
 */
static void
do_lsevent(void)
{
	register au_event_ent_t *evp;
	au_mask_t pmask;
	char auflags[256];

	setauevent();
	if (getauevent() == NULL) {
		exit_error(gettext("NO AUDIT EVENTS: Could not read %s\n."),
		    AUDITEVENTFILE);
	}

	setauevent();
	while ((evp = getauevent()) != NULL) {
		pmask.am_success = pmask.am_failure = evp->ae_class;
		if (getauditflagschar(auflags, &pmask, 0) == -1)
			(void) strcpy(auflags, "unknown");
		(void) printf("%-30s %5hu %s %s\n",
		    evp->ae_name, evp->ae_number, auflags, evp->ae_desc);
	}
	endauevent();
}

/*
 * do_lspolicy() - display the kernel audit policies with a description  of each
 * policy. The printed value is for the global zone unless AUDIT_PERZONE is set.
 */
static void
do_lspolicy(void)
{
	int i;

	/*
	 * TRANSLATION_NOTE
	 *	Print a properly aligned header.
	 */
	(void) printf(gettext("policy string    description:\n"));
	for (i = 0; i < POLICY_TBL_SZ; i++) {
		(void) printf("%-17s%s\n", policy_table[i].policy_str,
		    gettext(policy_table[i].policy_desc));
	}
}

/*
 * do_setasid() - execute shell or cmd with specified session-ID.
 */
static void
do_setasid(char *sid_str, char **argv)
{
	struct auditinfo_addr ai;

	if (getaudit_addr(&ai, sizeof (ai))) {
		exit_error(gettext("getaudit_addr(2) failed"));
	}
	ai.ai_asid = (au_asid_t)atol(sid_str);
	if (setaudit_addr(&ai, sizeof (ai))) {
		exit_error(gettext("setaudit_addr(2) failed"));
	}
	execit(argv);
}

/*
 * do_setaudit() - execute shell or cmd with specified audit characteristics.
 */
static void
do_setaudit(char *user_str, char *mask_str, char *tid_str, char *sid_str,
    char **argv)
{
	auditinfo_addr_t ai;

	ai.ai_auid = (au_id_t)get_user_id(user_str);
	egetauditflagsbin(mask_str, &ai.ai_mask),
	    str2tid(tid_str, &ai.ai_termid);
	ai.ai_asid = (au_asid_t)atol(sid_str);

	esetaudit(&ai, sizeof (ai));
	execit(argv);
}

/*
 * do_setauid() - execute shell or cmd with specified audit-ID.
 */
static void
do_setauid(char *user, char **argv)
{
	au_id_t auid;

	auid = get_user_id(user);
	esetauid(&auid);
	execit(argv);
}

/*
 * do_setpmask() - set the preselection mask of the specified process; valid
 * per zone if AUDIT_PERZONE is set, else only in global zone.
 */
static void
do_setpmask(char *pid_str, au_mask_t *mask)
{
	struct auditpinfo ap;

	if (strisnum(pid_str)) {
		ap.ap_pid = (pid_t)atoi(pid_str);
	} else {
		exit_usage(1);
	}

	ap.ap_mask.am_success = mask->am_success;
	ap.ap_mask.am_failure = mask->am_failure;

	eauditon(A_SETPMASK, (caddr_t)&ap, sizeof (ap));
}

/*
 * do_setsmask() - set the preselection mask of all processes with the specified
 * audit session-ID; valid per zone if AUDIT_PERZONE is set, else only in global
 * zone.
 */
static void
do_setsmask(char *asid_str, au_mask_t *mask)
{
	struct auditinfo ainfo;

	if (strisnum(asid_str)) {
		ainfo.ai_asid = (au_asid_t)atoi(asid_str);
	} else {
		exit_usage(1);
	}

	ainfo.ai_mask.am_success = mask->am_success;
	ainfo.ai_mask.am_failure = mask->am_failure;

	eauditon(A_SETSMASK, (caddr_t)&ainfo, sizeof (ainfo));
}

/*
 * do_setumask() -  set the preselection mask of all processes with the
 * specified audit-ID; valid per zone if AUDIT_PERZONE is set, else only in
 * global zone.
 */
static void
do_setumask(char *auid_str, au_mask_t *mask)
{
	struct auditinfo ainfo;

	if (strisnum(auid_str)) {
		ainfo.ai_auid = (au_id_t)atoi(auid_str);
	} else {
		exit_usage(1);
	}

	ainfo.ai_mask.am_success = mask->am_success;
	ainfo.ai_mask.am_failure = mask->am_failure;

	eauditon(A_SETUMASK, (caddr_t)&ainfo, sizeof (ainfo));
}

/*
 * do_setstat() - reset audit statistics counters; local zone use is valid if
 * AUDIT_PERZONE is set, otherwise the syscall returns EPERM.
 */
static void
do_setstat(void)
{
	au_stat_t as;

	as.as_audit	= (uint_t)-1;
	as.as_auditctl	= (uint_t)-1;
	as.as_dropped	= (uint_t)-1;
	as.as_enqueue	= (uint_t)-1;
	as.as_generated	= (uint_t)-1;
	as.as_kernel	= (uint_t)-1;
	as.as_nonattrib	= (uint_t)-1;
	as.as_rblocked	= (uint_t)-1;
	as.as_totalsize	= (uint_t)-1;
	as.as_wblocked	= (uint_t)-1;
	as.as_written	= (uint_t)-1;

	eauditon(A_SETSTAT, (caddr_t)&as, sizeof (as));
	(void) printf("%s\n", gettext("audit stats reset"));
}

/*
 * do_setclass() - map the kernel event event_str to the classes specified by
 * audit flags (mask); valid per zone if AUDIT_PERZONE is set, else only in
 * global zone.
 */
static void
do_setclass(char *event_str, au_mask_t *mask)
{
	au_event_t event;
	au_evclass_map_t ec;
	au_event_ent_t *evp;

	if (strisnum(event_str)) {
		event = (uint_t)atol(event_str);
	} else {
		if ((evp = egetauevnam(event_str)) != NULL) {
			event = evp->ae_number;
		}
	}

	ec.ec_number = event;
	ec.ec_class = (mask->am_success | mask->am_failure);

	eauditon(A_SETCLASS, (caddr_t)&ec, sizeof (ec));
}

/*
 * do_setflags() - set configured and active default user preselection masks;
 * valid per zone if AUDIT_PERZONE is set, else only in global zone.
 */
static void
do_setflags(char *audit_flags, au_mask_t *amask)
{
	eauditon(A_SETAMASK, (caddr_t)amask, sizeof (*amask));

	if (!do_setflags_scf(audit_flags)) {
		print_mask(gettext("active user default audit flags"), amask);
		exit_error(gettext("Could not store configuration value."));
	}
	print_mask(gettext("user default audit flags"), amask);
}

/*
 * do_setkmask() - set non-attributable audit flags of machine; valid per zone
 * if AUDIT_PERZONE is set, else only in global zone.
 */
static void
do_setkmask(au_mask_t *pmask)
{
	eauditon(A_SETKMASK, (caddr_t)pmask, sizeof (*pmask));
	print_mask(gettext("active non-attributable audit flags"), pmask);
}

/*
 * do_setnaflags() - set configured and active non-attributable selection flags
 * of machine; valid per zone if AUDIT_PERZONE is set, else only in global zone.
 */
static void
do_setnaflags(char *audit_naflags, au_mask_t *namask)
{
	eauditon(A_SETKMASK, (caddr_t)namask, sizeof (*namask));

	if (!do_setnaflags_scf(audit_naflags)) {
		print_mask(
		    gettext("active non-attributable audit flags"), namask);
		exit_error(gettext("Could not store configuration value."));
	}
	print_mask(gettext("non-attributable audit flags"), namask);
}

/*
 * do_setplugin() - set the given plugin plugin_str configuration values.
 */
static void
do_setplugin(char *plugin_str, boolean_t plugin_state, char *plugin_attr,
    int plugin_qsize)
{
	if (!do_setpluginconfig_scf(plugin_str, plugin_state, plugin_attr,
	    plugin_qsize)) {
		exit_error(gettext("Could not set plugin configuration."));
	}
}

/*
 * do_setpolicy() - set the active and configured kernel audit policy; active
 * values can be changed per zone if AUDIT_PERZONE is set, else only in global
 * zone.
 *
 * ahlt and perzone are global zone only. The kernel ensures that a local zone
 * can't change ahlt and perzone (EINVAL).
 */
static void
do_setpolicy(char *policy_str)
{
	uint32_t	policy = 0;

	switch (str2policy(policy_str, &policy)) {
	case 0:
		if (!temporary_set) {
			if (!do_getpolicy_scf(&policy)) {
				exit_error(gettext("Unable to get current "
				    "policy values from the SMF repository"));
			}
			(void) str2policy(policy_str, &policy);

			if (!do_setpolicy_scf(policy)) {
				exit_error(gettext("Could not store "
				    "configuration values."));
			}
		}
		eauditon(A_SETPOLICY, (caddr_t)&policy, 0);
		break;
	case 2:
		exit_error(gettext("policy (%s) invalid in a local zone."),
		    policy_str);
		break;
	default:
		exit_error(gettext("Invalid policy (%s) specified."),
		    policy_str);
		break;
	}
}

/*
 * do_setqbufsz() - set the active and configured audit queue write buffer size
 * (bytes); active values can be changed per zone if AUDIT_PERZONE is set, else
 * only in global zone.
 */
static void
do_setqbufsz(char *bufsz)
{
	struct au_qctrl qctrl;

	if (!temporary_set) {
		qctrl.aq_bufsz = (size_t)atol(bufsz);
		if (!do_setqbufsz_scf(&qctrl.aq_bufsz)) {
			exit_error(gettext(
			    "Could not store configuration value."));
		}
		if (qctrl.aq_bufsz == 0) {
			return;
		}
	}

	eauditon(A_GETQCTRL, (caddr_t)&qctrl, 0);
	qctrl.aq_bufsz = (size_t)atol(bufsz);
	eauditon(A_SETQCTRL, (caddr_t)&qctrl, 0);
}

/*
 * do_setqctrl() - set the active and configured audit queue write buffer size
 * (bytes), hiwater audit record count, lowater audit record count, and wakeup
 * interval (ticks); active values can be changed per zone if AUDIT_PERZONE is
 * set, else only in global zone.
 */
static void
do_setqctrl(char *hiwater, char *lowater, char *bufsz, char *delay)
{
	struct au_qctrl	qctrl;

	qctrl.aq_hiwater = (size_t)atol(hiwater);
	qctrl.aq_lowater = (size_t)atol(lowater);
	qctrl.aq_bufsz = (size_t)atol(bufsz);
	qctrl.aq_delay = (clock_t)atol(delay);

	if (!temporary_set) {
		struct au_qctrl qctrl_act;

		if (!do_setqctrl_scf(&qctrl)) {
			exit_error(gettext(
			    "Could not store configuration values."));
		}

		eauditon(A_GETQCTRL, (caddr_t)&qctrl_act, 0);
		if (qctrl.aq_hiwater == 0) {
			qctrl.aq_hiwater = qctrl_act.aq_hiwater;
		}
		if (qctrl.aq_lowater == 0) {
			qctrl.aq_lowater = qctrl_act.aq_lowater;
		}
		if (qctrl.aq_bufsz == 0) {
			qctrl.aq_bufsz = qctrl_act.aq_bufsz;
		}
		if (qctrl.aq_delay == 0) {
			qctrl.aq_delay = qctrl_act.aq_delay;
		}
	}

	eauditon(A_SETQCTRL, (caddr_t)&qctrl, 0);
}

/*
 * do_setqdelay() - set the active and configured audit queue wakeup interval
 * (ticks); active values can be changed per zone if AUDIT_PERZONE is set, else
 * only in global zone.
 */
static void
do_setqdelay(char *delay)
{
	struct au_qctrl qctrl;

	if (!temporary_set) {
		qctrl.aq_delay = (clock_t)atol(delay);
		if (!do_setqdelay_scf(&qctrl.aq_delay)) {
			exit_error(gettext(
			    "Could not store configuration value."));
		}
		if (qctrl.aq_delay == 0) {
			return;
		}
	}

	eauditon(A_GETQCTRL, (caddr_t)&qctrl, 0);
	qctrl.aq_delay = (clock_t)atol(delay);
	eauditon(A_SETQCTRL, (caddr_t)&qctrl, 0);
}

/*
 * do_setqhiwater() - sets the active and configured number of undelivered audit
 * records in the audit queue at which audit record generation blocks; active
 * values can be changed per zone if AUDIT_PERZONE is set, else only in global
 * zone.
 */
static void
do_setqhiwater(char *hiwater)
{
	struct au_qctrl qctrl;

	if (!temporary_set) {
		qctrl.aq_hiwater = (size_t)atol(hiwater);
		if (!do_setqhiwater_scf(&qctrl.aq_hiwater)) {
			exit_error(gettext(
			    "Could not store configuration value."));
		}
		if (qctrl.aq_hiwater == 0) {
			return;
		}
	}

	eauditon(A_GETQCTRL, (caddr_t)&qctrl, 0);
	qctrl.aq_hiwater = (size_t)atol(hiwater);
	eauditon(A_SETQCTRL, (caddr_t)&qctrl, 0);
}

/*
 * do_setqlowater() - set the active and configured number of undelivered audit
 * records in the audit queue at which blocked auditing processes unblock;
 * active values can be changed per zone if AUDIT_PERZONE is set, else only in
 * global zone.
 */
static void
do_setqlowater(char *lowater)
{
	struct au_qctrl qctrl;

	if (!temporary_set) {
		qctrl.aq_lowater = (size_t)atol(lowater);
		if (!do_setqlowater_scf(&qctrl.aq_lowater)) {
			exit_error(gettext(
			    "Could not store configuration value."));
		}
		if (qctrl.aq_lowater == 0) {
			return;
		}
	}

	eauditon(A_GETQCTRL, (caddr_t)&qctrl, 0);
	qctrl.aq_lowater = (size_t)atol(lowater);
	eauditon(A_SETQCTRL, (caddr_t)&qctrl, 0);
}

static void
eauditon(int cmd, caddr_t data, int length)
{
	if (auditon(cmd, data, length) == -1)
		exit_error(gettext("auditon(2) failed."));
}

static void
egetauid(au_id_t *auid)
{
	if (getauid(auid) == -1)
		exit_error(gettext("getauid(2) failed."));
}

static void
egetaudit(auditinfo_addr_t *ai, int size)
{
	if (getaudit_addr(ai, size) == -1)
		exit_error(gettext("getaudit_addr(2) failed."));
}

static void
egetkaudit(auditinfo_addr_t *ai, int size)
{
	if (auditon(A_GETKAUDIT, (char *)ai, size) < 0)
		exit_error(gettext("auditon: A_GETKAUDIT failed."));
}

static void
esetkaudit(auditinfo_addr_t *ai, int size)
{
	if (auditon(A_SETKAUDIT, (char *)ai, size) < 0)
		exit_error(gettext("auditon: A_SETKAUDIT failed."));
}

static void
egetauditflagsbin(char *auditflags, au_mask_t *pmask)
{
	if (strcmp(auditflags, "none") == 0) {
		pmask->am_success = pmask->am_failure = 0;
		return;
	}

	if (getauditflagsbin(auditflags, pmask) < 0) {
		exit_error(gettext("Could not get audit flags (%s)"),
		    auditflags);
	}
}

static void
echkflags(char *auditflags, au_mask_t *mask)
{
	char		*err = "";
	char		*err_ptr;

	if (!__chkflags(auditflags, mask, B_FALSE, &err)) {
		err_ptr = err;
		while (*err_ptr != ',' && *err_ptr != '\0') {
			err_ptr++;
		}
		*err_ptr = '\0';
		exit_error(gettext("Unknown audit flags and/or prefixes "
		    "encountered: %s"), err);
	}
}

static au_event_ent_t *
egetauevnum(au_event_t event_number)
{
	au_event_ent_t *evp;

	if ((evp = getauevnum(event_number)) == NULL) {
		exit_error(gettext("Could not get audit event %hu"),
		    event_number);
	}

	return (evp);
}

static au_event_ent_t *
egetauevnam(char *event_name)
{
	register au_event_ent_t *evp;

	if ((evp = getauevnam(event_name)) == NULL)
		exit_error(gettext("Could not get audit event %s"), event_name);

	return (evp);
}

static void
esetauid(au_id_t *auid)
{
	if (setauid(auid) == -1)
		exit_error(gettext("setauid(2) failed."));
}

static void
esetaudit(auditinfo_addr_t *ai, int size)
{
	if (setaudit_addr(ai, size) == -1)
		exit_error(gettext("setaudit_addr(2) failed."));
}

static uid_t
get_user_id(char *user)
{
	struct passwd *pwd;
	uid_t uid;

	if (isdigit(*user)) {
		uid = atoi(user);
		if ((pwd = getpwuid(uid)) == NULL) {
			exit_error(gettext("Invalid user: %s"), user);
		}
	} else {
		if ((pwd = getpwnam(user)) == NULL) {
			exit_error(gettext("Invalid user: %s"), user);
		}
	}

	return (pwd->pw_uid);
}

/*
 * get_arg_ent()
 *     Inputs: command line argument string
 *     Returns ptr to struct arg_entry if found; null, if not found
 */
static arg_entry_t *
get_arg_ent(char *arg_str)
{
	arg_entry_t key;

	key.arg_str = arg_str;

	return ((arg_entry_t *)bsearch((char *)&key, (char *)arg_table,
	    ARG_TBL_SZ, sizeof (arg_entry_t), arg_ent_compare));
}

/*
 * arg_ent_compare()
 *     Compares two command line arguments to determine which is
 *       lexicographically greater.
 *     Inputs: two argument map table entry pointers
 *     Returns: > 1: aep1->arg_str > aep2->arg_str
 *              < 1: aep1->arg_str < aep2->arg_str
 *                0: aep1->arg_str = aep->arg_str2
 */
static int
arg_ent_compare(const void *aep1, const void *aep2)
{
	return (strcmp(((arg_entry_t *)aep1)->arg_str,
	    ((arg_entry_t *)aep2)->arg_str));
}

/*
 * tid_str is major,minor,host  -- host is a name or an ip address
 */
static void
str2tid(char *tid_str, au_tid_addr_t *tp)
{
	char *major_str;
	char *minor_str;
	char *host_str = NULL;
	major_t major = 0;
	major_t minor = 0;
	dev_t dev = 0;
	struct hostent *phe;
	int err;
	uint32_t ibuf;
	uint32_t ibuf6[4];

	tp->at_port = 0;
	tp->at_type = 0;
	bzero(tp->at_addr, 16);

	major_str = tid_str;
	if ((minor_str = strchr(tid_str, ',')) != NULL) {
		*minor_str = '\0';
		minor_str++;
	}

	if (minor_str) {
		if ((host_str = strchr(minor_str, ',')) != NULL) {
			*host_str = '\0';
			host_str++;
		}
	}

	if (major_str)
		major = (major_t)atoi(major_str);

	if (minor_str)
		minor = (minor_t)atoi(minor_str);

	if ((dev = makedev(major, minor)) != NODEV)
		tp->at_port = dev;

	if (host_str) {
		if (strisipaddr(host_str)) {
			if (inet_pton(AF_INET, host_str, &ibuf)) {
				tp->at_addr[0] = ibuf;
				tp->at_type = AU_IPv4;
			} else if (inet_pton(AF_INET6, host_str, ibuf6)) {
				tp->at_addr[0] = ibuf6[0];
				tp->at_addr[1] = ibuf6[1];
				tp->at_addr[2] = ibuf6[2];
				tp->at_addr[3] = ibuf6[3];
				tp->at_type = AU_IPv6;
			}
		} else {
			phe = getipnodebyname((const void *)host_str,
			    AF_INET, 0, &err);
			if (phe == 0) {
				phe = getipnodebyname((const void *)host_str,
				    AF_INET6, 0, &err);
			}

			if (phe != NULL) {
				if (phe->h_addrtype == AF_INET6) {
					/* address is IPv6 (128 bits) */
					(void) memcpy(&tp->at_addr[0],
					    phe->h_addr_list[0], 16);
					tp->at_type = AU_IPv6;
				} else {
					/* address is IPv4 (32 bits) */
					(void) memcpy(&tp->at_addr[0],
					    phe->h_addr_list[0], 4);
					tp->at_type = AU_IPv4;
				}
				freehostent(phe);
			}
		}
	}
}

static char *
cond2str(void)
{
	uint_t cond;

	eauditon(A_GETCOND, (caddr_t)&cond, sizeof (cond));

	switch (cond) {

	case AUC_AUDITING:
		return ("auditing");

	case AUC_NOAUDIT:
	case AUC_INIT_AUDIT:
		return ("noaudit");

	case AUC_UNSET:
		return ("unset");

	case AUC_NOSPACE:
		return ("nospace");

	default:
		return ("");
	}
}

/*
 *	exit = 0, success
 *	       1, error
 *	       2, bad zone
 */
static int
str2policy(char *policy_str, uint32_t *policy_mask)
{
	char		*buf;
	char		*tok;
	char		pfix;
	boolean_t	is_all = B_FALSE;
	uint32_t	pm = 0;
	uint32_t	curp;

	pfix = *policy_str;

	if (pfix == '-' || pfix == '+' || pfix == '=')
		++policy_str;

	if ((buf = strdup(policy_str)) == NULL)
		return (1);

	for (tok = strtok(buf, ","); tok != NULL; tok = strtok(NULL, ",")) {
		uint32_t tok_pm;
		if (((tok_pm = get_policy(tok)) == 0) &&
		    ((strcasecmp(tok, "none") != 0))) {
			free(buf);
			return (1);
		} else {
			pm |= tok_pm;
			if (tok_pm == ALL_POLICIES) {
				is_all = B_TRUE;
			}
		}
	}
	free(buf);

	/* reuse policy mask if already set to some value */
	if (*policy_mask != 0) {
		curp = *policy_mask;
	} else {
		(void) auditon(A_GETPOLICY, (caddr_t)&curp, 0);
	}

	if (pfix == '-') {
		if (!is_all &&
		    (getzoneid() != GLOBAL_ZONEID) &&
		    (pm & ~AUDIT_LOCAL)) {
			return (2);
		}

		if (getzoneid() != GLOBAL_ZONEID)
			curp &= AUDIT_LOCAL;
		*policy_mask = curp & ~pm;

	} else if (pfix == '+') {
		/*
		 * In a local zone, accept specifying "all", but not
		 * individually specifying global-zone only policies.
		 * Limit to all locally allowed, so system call doesn't
		 * fail.
		 */
		if (!is_all &&
		    (getzoneid() != GLOBAL_ZONEID) &&
		    (pm & ~AUDIT_LOCAL)) {
			return (2);
		}

		if (getzoneid() != GLOBAL_ZONEID) {
			curp &= AUDIT_LOCAL;
			if (is_all) {
				pm &= AUDIT_LOCAL;
			}
		}
		*policy_mask = curp | pm;

	} else {
		/*
		 * In a local zone, accept specifying "all", but not
		 * individually specifying global-zone only policies.
		 * Limit to all locally allowed, so system call doesn't
		 * fail.
		 */
		if (!is_all &&
		    (getzoneid() != GLOBAL_ZONEID) &&
		    (pm & ~AUDIT_LOCAL)) {
			return (2);
		}

		if (is_all && (getzoneid() != GLOBAL_ZONEID)) {
			pm &= AUDIT_LOCAL;
		}
		*policy_mask = pm;
	}
	return (0);
}

static int
policy2str(uint32_t policy, char *policy_str, size_t len)
{
	int i, j;

	if (policy == ALL_POLICIES) {
		(void) strcpy(policy_str, "all");
		return (1);
	}

	if (policy == NO_POLICIES) {
		(void) strcpy(policy_str, "none");
		return (1);
	}

	*policy_str = '\0';

	for (i = 0, j = 0; i < POLICY_TBL_SZ; i++) {
		if (policy & policy_table[i].policy_mask &&
		    policy_table[i].policy_mask != ALL_POLICIES) {
			if (j++) {
				(void) strcat(policy_str, ",");
			}
			(void) strlcat(policy_str, policy_table[i].policy_str,
			    len);
		}
	}

	if (*policy_str)
		return (0);

	return (1);
}


static int
strisnum(char *s)
{
	if (s == NULL || !*s)
		return (0);

	for (; *s == '-' || *s == '+'; s++) {
		if (!*s)
			return (0);
	}

	for (; *s; s++) {
		if (!isdigit(*s))
			return (0);
	}

	return (1);
}

static int
strisipaddr(char *s)
{
	int dot = 0;
	int colon = 0;

	/* no string */
	if ((s == NULL) || (!*s))
		return (0);

	for (; *s; s++) {
		if (!(isxdigit(*s) || *s != '.' || *s != ':'))
			return (0);
		if (*s == '.')
			dot++;
		if (*s == ':')
			colon++;
	}

	if (dot && colon)
		return (0);

	if (!dot && !colon)
		return (0);

	return (1);
}

static void
chk_arg_len(char *argv, uint_t len)
{
	if ((strlen(argv) + 1) > len) {
		*(argv + len - 1) = '\0';
		exit_error(gettext("Argument too long (%s..)."), argv);
	}
}

static void
chk_event_num(int etype, au_event_t event)
{
	au_stat_t as;

	eauditon(A_GETSTAT, (caddr_t)&as, 0);

	if (etype == AC_KERN_EVENT) {
		if (event > as.as_numevent) {
			exit_error(gettext(
			    "Invalid kernel audit event number specified.\n"
			    "\t%hu is outside allowable range 0-%d."),
			    event, as.as_numevent);
		}
	} else  {
		/* user event */
		if (event <= as.as_numevent) {
			exit_error(gettext("Invalid user level audit event "
			    "number specified %hu."), event);
		}
	}
}

static void
chk_event_str(int etype, char *event_str)
{
	au_event_ent_t *evp;
	au_stat_t as;

	eauditon(A_GETSTAT, (caddr_t)&as, 0);

	evp = egetauevnam(event_str);
	if (etype == AC_KERN_EVENT && (evp->ae_number > as.as_numevent)) {
		exit_error(gettext(
		    "Invalid kernel audit event string specified.\n"
		    "\t\"%s\" appears to be a user level event. "
		    "Check configuration."), event_str);
	} else if (etype == AC_USER_EVENT &&
	    (evp->ae_number < as.as_numevent)) {
		exit_error(gettext(
		    "Invalid user audit event string specified.\n"
		    "\t\"%s\" appears to be a kernel event. "
		    "Check configuration."), event_str);
	}
}

static void
chk_known_plugin(char *plugin_str)
{
	if ((strlen(plugin_str) + 1) > PLUGIN_MAXBUF) {
		exit_error(gettext("Plugin name too long.\n"));
	}

	if (!plugin_avail_scf(plugin_str)) {
		exit_error(gettext("No such plugin configured: %s"),
		    plugin_str);
	}
}

static void
chk_sorf(char *sorf_str)
{
	if (!strisnum(sorf_str))
		exit_error(gettext("Invalid sorf specified: %s"), sorf_str);
}

static void
chk_retval(char *retval_str)
{
	if (!strisnum(retval_str))
		exit_error(gettext("Invalid retval specified: %s"), retval_str);
}

static void
execit(char **argv)
{
	char *args, *args_pos;
	size_t len = 0;
	size_t n = 0;
	char **argv_pos;

	if (*argv) {
		/* concatenate argument array to be passed to sh -c "..." */
		for (argv_pos = argv; *argv_pos; argv_pos++)
			len += strlen(*argv_pos) + 1;

		if ((args = malloc(len + 1)) == NULL)
			exit_error(
			    gettext("Allocation for command/arguments failed"));

		args_pos = args;
		for (argv_pos = argv; *argv_pos; argv_pos++) {
			n += snprintf(args_pos, len - n, "%s ", *argv_pos);
			args_pos = args + n;
		}
		/* strip the last space */
		args[strlen(args)] = '\0';

		(void) execl("/bin/sh", "sh", "-c", args, NULL);
	} else {
		(void) execl("/bin/sh", "sh", NULL);
	}

	exit_error(gettext("exec(2) failed"));
}

static void
exit_usage(int status)
{
	FILE *fp;
	int i;

	fp = (status ? stderr : stdout);
	(void) fprintf(fp, gettext("usage: %s option ...\n"), progname);

	for (i = 0; i < ARG_TBL_SZ; i++) {
		/* skip the -t option; it's not a standalone option */
		if (arg_table[i].auditconfig_cmd == AC_ARG_SET_TEMPORARY) {
			continue;
		}

		(void) fprintf(fp, " %s%s%s\n",
		    arg_table[i].arg_str, arg_table[i].arg_opts,
		    (arg_table[i].temporary_allowed ? " [-t]" : ""));
	}

	exit(status);
}

static void
print_asid(au_asid_t asid)
{
	(void) printf(gettext("audit session id = %u\n"), asid);
}

static void
print_auid(au_id_t auid)
{
	struct passwd *pwd;
	char *username;

	if ((pwd = getpwuid((uid_t)auid)) != NULL)
		username = pwd->pw_name;
	else
		username = gettext("unknown");

	(void) printf(gettext("audit id = %s(%d)\n"), username, auid);
}

static void
print_mask(char *desc, au_mask_t *pmp)
{
	char auflags[512];

	if (getauditflagschar(auflags, pmp, 0) < 0)
		(void) strlcpy(auflags, gettext("unknown"), sizeof (auflags));

	(void) printf("%s = %s(0x%x,0x%x)\n",
	    desc, auflags, pmp->am_success, pmp->am_failure);
}

static void
print_plugin(char *plugin_name, kva_t *plugin_kva)
{
	char		att_str[PLUGIN_MAXATT];
	boolean_t	plugin_active;
	char		*active_str;
	char		*qsize_ptr;
	int		qsize;

	if ((active_str = kva_match(plugin_kva, "active")) == NULL) {
		(void) printf(gettext("Audit service configuration error: "
		    "\"active\" property not found\n"));
		return;
	}

	plugin_active = (boolean_t)atoi(active_str);
	qsize_ptr = kva_match(plugin_kva, "qsize");
	qsize = atoi(qsize_ptr == NULL ? "-1" : qsize_ptr);

	(void) printf(gettext("Plugin: %s (%s)\n"), plugin_name,
	    plugin_active ? "active" : "inactive");

	free_static_att_kva(plugin_kva);

	switch (_kva2str(plugin_kva, att_str, PLUGIN_MAXATT, "=", ";")) {
	case 0:
		(void) printf(gettext("\tAttributes: %s\n"), att_str);
		break;
	case 1:
		exit_error(gettext("Internal error - buffer size too small."));
		break;
	default:
		exit_error(gettext("Internal error."));
		break;
	}

	if (qsize != 0) {
		(void) printf(gettext("\tQueue size: %d %s\n"), qsize,
		    qsize == -1 ? "(internal error: value not available)" : "");
	}
}

static void
print_tid_ex(au_tid_addr_t *tidp)
{
	struct hostent *phe;
	char *hostname;
	struct in_addr ia;
	uint32_t *addr;
	int err;
	char buf[INET6_ADDRSTRLEN];
	char *bufp;


	/* IPV6 or IPV4 address */
	if (tidp->at_type == AU_IPv4) {
		if ((phe = gethostbyaddr((char *)&tidp->at_addr[0],
		    sizeof (tidp->at_addr[0]), AF_INET)) != NULL) {
			hostname = phe->h_name;
		} else {
			hostname = gettext("unknown");
		}

		ia.s_addr = tidp->at_addr[0];

		(void) printf(gettext(
		    "terminal id (maj,min,host) = %lu,%lu,%s(%s)\n"),
		    major(tidp->at_port), minor(tidp->at_port),
		    hostname, inet_ntoa(ia));
	} else {
		addr = &tidp->at_addr[0];
		phe = getipnodebyaddr((const void *)addr, 16, AF_INET6, &err);

		bzero(buf, sizeof (buf));

		(void) inet_ntop(AF_INET6, (void *)addr, buf, sizeof (buf));
		if (phe == NULL) {
			bufp = gettext("unknown");
		} else {
			bufp = phe->h_name;
		}

		(void) printf(gettext(
		    "terminal id (maj,min,host) = %lu,%lu,%s(%s)\n"),
		    major(tidp->at_port), minor(tidp->at_port),
		    bufp, buf);
		if (phe) {
			freehostent(phe);
		}
	}
}

static int
str2ipaddr(char *s, uint32_t *addr, uint32_t type)
{
	int j, sl;
	char *ss;
	unsigned int v;

	bzero(addr, 16);
	if (strisipaddr(s)) {
		if (type == AU_IPv4) {
			if (inet_pton(AF_INET, s, addr)) {
				return (0);
			}
			return (1);
		} else if (type == AU_IPv6) {
			if (inet_pton(AF_INET6, s, addr))
				return (0);
			return (1);
		}
		return (1);
	} else {
		if (type == AU_IPv4) {
			(void) sscanf(s, "%x", &addr[0]);
			return (0);
		} else if (type == AU_IPv6) {
			sl = strlen(s);
			ss = s;
			for (j = 3; j >= 0; j--) {
				if ((sl - 8) <= 0) {
					(void) sscanf(s, "%x", &v);
					addr[j] = v;
					return (0);
				}
				ss = &s[sl-8];
				(void) sscanf(ss, "%x", &v);
				addr[j] = v;
				sl -= 8;
				*ss = '\0';
			}
		}
		return (0);
	}
}

static int
str2type(char *s, uint_t *type)
{
	if (strcmp(s, "ipv6") == 0) {
		*type = AU_IPv6;
		return (0);
	}
	if (strcmp(s, "ipv4") == 0) {
		*type = AU_IPv4;
		return (0);
	}

	return (1);
}

/*
 * exit_error() - print an error message along with corresponding system error
 * number and error message, then exit. Inputs - program error format and
 * message.
 */
/*PRINTFLIKE1*/
static void
exit_error(char *fmt, ...)
{
	va_list	args;

	va_start(args, fmt);
	prt_error_va(fmt, args);
	va_end(args);

	exit(1);
}