summaryrefslogtreecommitdiff
path: root/usr/src/cmd/praudit/token.c
blob: 4c1c0ba02bc9154d460d597aed01cd3a8bb634e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
/*
 * 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 <ctype.h>
#include <dirent.h>
#include <grp.h>
#include <libintl.h>
#include <limits.h>
#include <locale.h>
#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/inttypes.h>
#include <sys/file.h>
#include <sys/param.h>
#include <sys/uio.h>
#include <sys/stat.h>
#include <sys/acl.h>
#include <sys/socket.h>
#include <sys/errno.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <sys/systm.h>
#include <netinet/in.h>
#include <sys/tiuser.h>
#include <rpc/types.h>
#include <rpc/auth.h>
#include <rpc/auth_unix.h>
#include <rpc/svc.h>
#include <rpc/xdr.h>
#include <nfs/nfs.h>
#include <sys/fs/ufs_quota.h>
#include <sys/time.h>
#include <sys/mkdev.h>
#include <unistd.h>

#include <bsm/audit.h>
#include <bsm/audit_record.h>
#include <bsm/libbsm.h>

#include <tsol/label.h>

#include "praudit.h"
#include "toktable.h"

#include <netdb.h>
#include <arpa/inet.h>

static char *anchor_path(char *);
static char *collapse_path(char *);


/*
 * -----------------------------------------------------------------------
 * is_file_token:
 *		  Tests whether the specified token id represents a type
 *		  of file token.
 * return codes :  1 - tokenid is a file token type
 *		:  0 - otherwise
 * -----------------------------------------------------------------------
 */
int
is_file_token(int tokenid)
{
	if ((tokenid == AUT_OTHER_FILE32) || (tokenid == AUT_OTHER_FILE64))
		return (1);

	return (0);
}

/*
 * -----------------------------------------------------------------------
 * is_header_token:
 *		  Tests whether the specified token id represents a type
 *		  of header token (signifying the start of a record).
 * return codes :  1 - tokenid is a header type
 *		:  0 - otherwise
 * -----------------------------------------------------------------------
 */
int
is_header_token(int tokenid)
{
	if ((tokenid == AUT_OHEADER) || (tokenid == AUT_HEADER32) ||
	    (tokenid == AUT_HEADER32_EX) || (tokenid == AUT_HEADER64) ||
	    (tokenid == AUT_HEADER64_EX))
		return (1);

	return (0);
}

/*
 * -----------------------------------------------------------------------
 * is_token:
 *		  Tests whether the specified token id represents a true
 *		  token, as opposed to a regular tag.
 * return codes :  1 - tokenid is a true token
 *		:  0 - otherwise
 * -----------------------------------------------------------------------
 */
int
is_token(int tokenid)
{
	if ((tokenid > 0) && (tokenid <= MAXTOKEN))
		return (1);

	return (0);
}


/*
 * -----------------------------------------------------------------------
 * exit_token() 	: Process information label token and display contents
 * return codes		: -1 - error
 *			:  0 - successful
 * NOTE: At the time of call, the label token id has been retrieved
 *
 * Format of exit token:
 *	exit token id		adr_char
 * -----------------------------------------------------------------------
 */
int
exit_token(pr_context_t *context)
{
	int	returnstat;
	int	retval;
	uval_t	uval;

	if ((returnstat = open_tag(context, TAG_ERRVAL)) != 0)
		return (returnstat);

	if ((returnstat = pr_adr_int32(context, (int32_t *)&retval, 1)) == 0) {
		if (!(context->format & PRF_RAWM)) {
			char *emsg = strerror(retval);

			if (emsg == NULL)
				uval.string_val = gettext("Unknown errno");
			else
				uval.string_val = gettext(emsg);
			uval.uvaltype = PRA_STRING;
		} else {
			uval.uvaltype = PRA_INT32;
			uval.int32_val = retval;
		}
		returnstat = pa_print(context, &uval, 0);
	}
	if (returnstat == 0)
		returnstat = close_tag(context, TAG_ERRVAL);

	return (process_tag(context, TAG_RETVAL, returnstat, 1));
}

/*
 * ------------------------------------------------------------------
 * file_token()	: prints out seconds of time and other file name
 * return codes : -1 - error
 *		:  0 - successful, valid file token fields
 * At the time of entry, the file token ID has already been retrieved
 *
 * Format of file token:
 *	file token id		adr_char
 *	seconds of time		adr_u_int
 *	name of other file	adr_string
 * ------------------------------------------------------------------
 */
int
file_token(pr_context_t *context)
{
	int	returnstat;

	returnstat = pa_utime32(context, 0, 0);		/* time from usecs */

	/* other file name */
	returnstat = pa_file_string(context, returnstat, 1);

	return (returnstat);
}

int
file64_token(pr_context_t *context)
{
	int	returnstat;

	returnstat = pa_utime64(context, 0, 0);		/* time from usecs */

	/* other file name */
	returnstat = pa_file_string(context, returnstat, 1);

	return (returnstat);
}

/*
 * -----------------------------------------------------------------------
 * header_token()	: Process record header token and display contents
 * return codes		: -1 - error
 *			:  0 - successful
 *			:  1 - warning, password entry not found
 *
 * NOTE: At the time of call, the header token id has been retrieved
 *
 * Format of header token:
 *	header token id 	adr_char
 * 	record byte count	adr_u_int
 *	event type		adr_u_short (printed either ASCII or raw)
 *	event class		adr_u_int   (printed either ASCII or raw)
 *	event action		adr_u_int
 *	if extended:		extended host name (IPv4/IPv6)
 *	seconds of time		adr_u_int   (printed either ASCII or raw)
 *	nanoseconds of time	adr_u_int
 * -----------------------------------------------------------------------
 */
int
header_token(pr_context_t *context)
{
	int	returnstat;

	returnstat = pa_reclen(context, 0);		/* record byte */
	/* version ID */
	returnstat = process_tag(context, TAG_TOKVERS, returnstat, 0);
	/* event type */
	returnstat = process_tag(context, TAG_EVTYPE, returnstat, 0);
	/* event modifier */
	returnstat = pa_event_modifier(context, returnstat, 0);
	/* time from nsec */
	returnstat = pa_ntime32(context, returnstat, 1);

	return (returnstat);
}

int
header64_token(pr_context_t *context)
{
	int	returnstat;

	returnstat = pa_reclen(context, 0);		/* record byte */
	/* version ID */
	returnstat = process_tag(context, TAG_TOKVERS, returnstat, 0);
	/* event type */
	returnstat = process_tag(context, TAG_EVTYPE, returnstat, 0);
	/* event modifier */
	returnstat = pa_event_modifier(context, returnstat, 0);
	/* time from nsec */
	returnstat = pa_ntime64(context, returnstat, 1);

	return (returnstat);
}

int
header32_ex_token(pr_context_t *context)
{
	int	returnstat;

	returnstat = pa_reclen(context, 0);		/* record byte */
	/* version ID */
	returnstat = process_tag(context, TAG_TOKVERS, returnstat, 0);
	/* event type */
	returnstat = process_tag(context, TAG_EVTYPE, returnstat, 0);
	/* event modifier */
	returnstat = pa_event_modifier(context, returnstat, 0);
	/* machine name */
	returnstat = pa_hostname_ex(context, returnstat, 0);
	/* time from nsec */
	returnstat = pa_ntime32(context, returnstat, 1);

	return (returnstat);
}

int
header64_ex_token(pr_context_t *context)
{
	int	returnstat;

	returnstat = pa_reclen(context, 0);		/* record byte */
	/* version ID */
	returnstat = process_tag(context, TAG_TOKVERS, returnstat, 0);
	/* event type */
	returnstat = process_tag(context, TAG_EVTYPE, returnstat, 0);
	/* event modifier */
	returnstat = pa_event_modifier(context, returnstat, 0);
	/* machine name */
	returnstat = pa_hostname_ex(context, returnstat, 0);
	/* time from nsec */
	returnstat = pa_ntime64(context, returnstat, 1);

	return (returnstat);
}

/*
 * -----------------------------------------------------------------------
 * trailer_token()	: Process record trailer token and display contents
 * return codes		: -1 - error
 *			:  0 - successful
 * NOTE: At the time of call, the trailer token id has already been
 * retrieved
 *
 * Format of trailer token:
 * 	trailer token id	adr_char
 * 	record sequence no	adr_u_short (should be AUT_TRAILER_MAGIC)
 *	record byte count	adr_u_int
 * -----------------------------------------------------------------------
 */
int
trailer_token(pr_context_t *context)
{
	short	magic_number;

	if (pr_adr_u_short(context, (ushort_t *)&magic_number, 1) < 0) {
		(void) fprintf(stderr, gettext(
		    "praudit: Cannot retrieve trailer magic number\n"));
		return (-1);
	} else {
		if (magic_number != AUT_TRAILER_MAGIC) {
			(void) fprintf(stderr, gettext(
			    "praudit: Invalid trailer magic number\n"));
			return (-1);
		} else
			/* Do not display trailer in XML mode */
			if (context->format & PRF_XMLM) {
				uint32_t	junk;
				int		retstat;

				retstat = pr_adr_u_int32(context, &junk, 1);
				return (retstat);
			} else {
				return (pa_adr_u_int32(context, 0, 1));
			}
	}
}

/*
 * -----------------------------------------------------------------------
 * arbitrary_data_token():
 *			  Process arbitrary data token and display contents
 * return codes		: -1 - error
 *			:  0 - successful
 * NOTE: At the time of call, the arbitrary data token id has already
 * been retrieved
 *
 * Format of arbitrary data token:
 *	arbitrary data token id	adr char
 * 	how to print		adr_char
 *				From audit_record.h, this may be either:
 *				AUP_BINARY	binary
 *				AUP_OCTAL	octal
 *				AUP_DECIMAL	decimal
 *				AUP_HEX		hexadecimal
 *	basic unit		adr_char
 *				From audit_record.h, this may be either:
 *				AUR_BYTE	byte
 *				AUR_CHAR	char
 *				AUR_SHORT	short
 *				AUR_INT32	int32_t
 *				AUR_INT64	int64_t
 *	unit count		adr_char, specifying number of units of
 *				data in the "data items" parameter below
 *	data items		depends on basic unit
 *
 * -----------------------------------------------------------------------
 */
int
arbitrary_data_token(pr_context_t *context)
{
	int	returnstat;
	int	i;
	char	c1;
	short	c2;
	int32_t	c3;
	int64_t c4;
	char	how_to_print, basic_unit, unit_count, fwid;
	char	*p;
	int	index = 0;
	char	*pformat = "%*s";

	uval_t	uval;

	if ((returnstat = pr_adr_char(context, &how_to_print, 1)) != 0)
		return (returnstat);

	if ((returnstat = pr_adr_char(context, &basic_unit, 1)) != 0)
		return (returnstat);

	if ((returnstat = pr_adr_char(context, &unit_count, 1)) != 0)
		return (returnstat);

	if (!(context->format & PRF_RAWM)) {
		uval.uvaltype = PRA_STRING;
		uval.string_val = htp2string(how_to_print);
	} else {
		uval.uvaltype = PRA_INT32;
		uval.int32_val = (int)how_to_print;
	}

	if ((returnstat = open_tag(context, TAG_ARBPRINT)) != 0)
		return (returnstat);
	if ((returnstat = pa_print(context, &uval, 0)) < 0)
		return (returnstat);
	if ((returnstat = close_tag(context, TAG_ARBPRINT)) != 0)
		return (returnstat);

	if (!(context->format & PRF_RAWM)) {
		uval.uvaltype = PRA_STRING;
		uval.string_val = bu2string(basic_unit);
	} else {
		uval.uvaltype = PRA_INT32;
		uval.int32_val = (int32_t)basic_unit;
	}

	if ((returnstat = open_tag(context, TAG_ARBTYPE)) != 0)
		return (returnstat);
	if ((returnstat = pa_print(context, &uval, 0)) < 0)
		return (returnstat);
	if ((returnstat = close_tag(context, TAG_ARBTYPE)) != 0)
		return (returnstat);

	uval.uvaltype = PRA_INT32;
	uval.int32_val = (int32_t)unit_count;

	if ((returnstat = open_tag(context, TAG_ARBCOUNT)) != 0)
		return (returnstat);
	if ((returnstat = pa_print(context, &uval, 1)) < 0)
		return (returnstat);
	if ((returnstat = close_tag(context, TAG_ARBCOUNT)) != 0)
		return (returnstat);

	/* Done with attributes; force end of token open */
	if ((returnstat = finish_open_tag(context)) != 0)
		return (returnstat);

	/* get the field width in case we need to format output */
	fwid = findfieldwidth(basic_unit, how_to_print);
	p = (char *)malloc(80);

	/* now get the data items and print them */
	for (i = 0; (i < unit_count); i++) {
		switch (basic_unit) {
			/* case AUR_BYTE: */
		case AUR_CHAR:
			if (pr_adr_char(context, &c1, 1) == 0)
				(void) convert_char_to_string(how_to_print,
				    c1, p);
			else {
				free(p);
				return (-1);
			}
			break;
		case AUR_SHORT:
			if (pr_adr_short(context, &c2, 1) == 0)
				(void) convert_short_to_string(how_to_print,
				    c2, p);
			else {
				free(p);
				return (-1);
			}
			break;
		case AUR_INT32:
			if (pr_adr_int32(context, &c3, 1) == 0)
				(void) convert_int32_to_string(how_to_print,
				    c3, p);
			else {
				free(p);
				return (-1);
			}
			break;
		case AUR_INT64:
			if (pr_adr_int64(context, &c4, 1) == 0)
				(void) convert_int64_to_string(how_to_print,
				    c4, p);
			else {
				free(p);
				return (-1);
			}
			break;
		default:
			free(p);
			return (-1);
			/*NOTREACHED*/
		}

		/*
		 * At this point, we have successfully retrieved a data
		 * item and converted it into an ASCII string pointed to
		 * by p. If all output is to be printed on one line,
		 * simply separate the data items by a space (or by the
		 * delimiter if this is the last data item), otherwise, we
		 * need to format the output before display.
		 */
		if (context->format & PRF_ONELINE) {
			returnstat = pr_printf(context, "%s", p);
			if ((returnstat >= 0) && (i == (unit_count - 1)))
				returnstat = pr_printf(context, "%s",
				    context->SEPARATOR);
			else
				returnstat = pr_putchar(context, ' ');
		} else {	/* format output */
			returnstat = pr_printf(context, pformat, fwid, p);
			index += fwid;
			if ((returnstat >= 0) &&
			    (((index + fwid) > 75) ||
			    (i == (unit_count - 1)))) {
				returnstat = pr_putchar(context, '\n');
				index = 0;
			}
		} /* else if PRF_ONELINE */
		if (returnstat < 0) {
			free(p);
			return (returnstat);
		}
	}
	free(p);

	return (returnstat);
}

/*
 * -----------------------------------------------------------------------
 * opaque_token() 	: Process opaque token and display contents
 * return codes		: -1 - error
 *			:  0 - successful
 * NOTE: At the time of call, the opaque token id has already been
 * retrieved
 *
 * Format of opaque token:
 *	opaque token id		adr_char
 *	size			adr_short
 *	data			adr_char, size times
 * -----------------------------------------------------------------------
 */
int
opaque_token(pr_context_t *context)
{
	int	returnstat;
	short	size;
	char	*charp;
	uval_t	uval;


	/* print the size of the token */
	if (pr_adr_short(context, &size, 1) == 0) {
		uval.uvaltype = PRA_SHORT;
		uval.short_val = size;
		returnstat = pa_print(context, &uval, 0);
	} else
		returnstat = -1;

	/* now print out the data field in hexadecimal */
	if (returnstat >= 0) {
		/* try to allocate memory for the character string */
		if ((charp = (char *)malloc(size * sizeof (char))) == NULL)
			returnstat = -1;
		else {
			if ((returnstat = pr_adr_char(context, charp,
			    size)) == 0) {
				/* print out in hexadecimal format */
				uval.uvaltype = PRA_STRING;
				uval.string_val = hexconvert(charp, size, size);
				if (uval.string_val) {
					returnstat = pa_print(context,
					    &uval, 1);
					free(uval.string_val);
				}
			}
			free(charp);
		}
	}

	return (returnstat);
}

/*
 * -----------------------------------------------------------------------
 * path_token() 	: Process path token and display contents
 * return codes		: -1 - error
 *			:  0 - successful
 * NOTE: At the time of call, the path token id has been retrieved
 *
 * Format of path token:
 *	token id	adr_char
 *	path		adr_string
 * -----------------------------------------------------------------------
 */
int
path_token(pr_context_t *context)
{
	char	*path;	/* path */
	char	*apath;	/* anchored path */
	char	*cpath;	/* collapsed path */
	short	length;
	int	returnstat;
	uval_t	uval;

	/*
	 * We need to know how much space to allocate for our string, so
	 * read the length first, then call pr_adr_char to read those bytes.
	 */
	if (pr_adr_short(context, &length, 1) == 0) {
		if ((path = (char *)malloc(length + 1)) == NULL) {
			returnstat = -1;
		} else if (pr_adr_char(context, path, length) == 0) {
			path[length] = '\0';
			uval.uvaltype = PRA_STRING;
			if (*path != '/') {
				apath = anchor_path(path);
				free(path);
			} else
				apath = path;
			cpath = collapse_path(apath);
			uval.string_val = cpath;
			returnstat = pa_print(context, &uval, 1);
			free(cpath);
		} else {
			free(path);
			returnstat = -1;
		}
		return (returnstat);
	} else
		return (-1);
}

/*
 * anchor a path name with a slash
 */
char *
anchor_path(char *sp)
{
	char	*dp; /* destination path */
	char	*tp; /* temporary path */
	size_t	len;

	len = strlen(sp) + 2;
	if ((dp = tp = (char *)calloc(1, len)) == (char *)0)
		return ((char *)0);

	*dp++ = '/';

	(void) strlcpy(dp, sp, len);

	return (tp);
}

/*
 * copy path to collapsed path.
 * collapsed path does not contain:
 *	successive slashes
 *	instances of dot-slash
 *	instances of dot-dot-slash
 * passed path must be anchored with a '/'
 */
char *
collapse_path(char *s)
{
	int	id;	/* index of where we are in destination string */
	int	is;		/* index of where we are in source string */
	int	slashseen;	/* have we seen a slash */
	int	ls;		/* length of source string */

	ls = strlen(s) + 1;

	slashseen = 0;
	for (is = 0, id = 0; is < ls; is++) {
		/* thats all folks, we've reached the end of input */
		if (s[is] == '\0') {
			if (id > 1 && s[id-1] == '/') {
				--id;
			}
			s[id++] = '\0';
			break;
		}
		/* previous character was a / */
		if (slashseen) {
			if (s[is] == '/')
				continue;	/* another slash, ignore it */
		} else if (s[is] == '/') {
			/* we see a /, just copy it and try again */
			slashseen = 1;
			s[id++] = '/';
			continue;
		}
		/* /./ seen */
		if (s[is] == '.' && s[is+1] == '/') {
			is += 1;
			continue;
		}
		/* XXX/. seen */
		if (s[is] == '.' && s[is+1] == '\0') {
			if (id > 1)
				id--;
			continue;
		}
		/* XXX/.. seen */
		if (s[is] == '.' && s[is+1] == '.' && s[is+2] == '\0') {
			is += 1;
			if (id > 0)
				id--;
			while (id > 0 && s[--id] != '/')
				continue;
			id++;
			continue;
		}
		/* XXX/../ seen */
		if (s[is] == '.' && s[is+1] == '.' && s[is+2] == '/') {
			is += 2;
			if (id > 0)
				id--;
			while (id > 0 && s[--id] != '/')
				continue;
			id++;
			continue;
		}
		while (is < ls && (s[id++] = s[is++]) != '/')
			continue;
		is--;
	}
	return (s);
}

/*
 * -----------------------------------------------------------------------
 * cmd_token()		: Process cmd token and display contents
 * return codes		: -1 - error
 *			:  0 - successful
 * NOTE: At the time of call, the cmd token id has been retrieved
 *
 * Format of command token:
 *	token id	adr_char
 *	argc		adr_short
 *	N*argv[i]	adr_string (short, string)
 *	env cnt		adr_short
 *	N*arge[i]	adr_string (short, string)
 * -----------------------------------------------------------------------
 */
int
cmd_token(pr_context_t *context)
{
	int	returnstat;
	short num;

	returnstat = pr_adr_short(context, &num, 1);
	if (returnstat < 0)
		return (returnstat);

	if (!(context->format & PRF_XMLM)) {
		returnstat = pr_printf(context, "%s%s%d%s",
		    (context->format & PRF_ONELINE) ? "" : gettext("argcnt"),
		    (context->format & PRF_ONELINE) ? "" : context->SEPARATOR,
		    num, context->SEPARATOR);
		if (returnstat < 0)
			return (returnstat);
	}

	for (; num > 0; num--) {
		if ((returnstat = process_tag(context, TAG_ARGV,
		    returnstat, 0)) < 0)
			return (returnstat);
	}

	if ((returnstat = pr_adr_short(context, &num, 1)) < 0)
		return (returnstat);

	if (!(context->format & PRF_XMLM)) {
		returnstat = pr_printf(context, "%s%s%d%s",
		    (context->format & PRF_ONELINE) ? "" : gettext("envcnt"),
		    (context->format & PRF_ONELINE) ? "" : context->SEPARATOR,
		    num, context->SEPARATOR);
		if (returnstat < 0)
			return (returnstat);
	}

	if ((num == 0) && !(context->format & PRF_XMLM)) {
		returnstat = do_newline(context, 1);
		if (returnstat < 0)
			return (returnstat);
	}

	for (; num > 1; num--) {
		if ((returnstat = process_tag(context, TAG_ARGE,
		    returnstat, 0)) < 0)
			return (returnstat);
	}
	if (num)
		returnstat = process_tag(context, TAG_ARGE, returnstat, 1);

	return (returnstat);

}

/*
 * -----------------------------------------------------------------------
 * argument32_token()	: Process argument token and display contents
 * return codes		: -1 - error
 *			:  0 - successful
 * NOTE: At the time of call, the arg token id has been retrieved
 *
 * Format of argument token:
 *	current directory token id	adr_char
 *	argument number			adr_char
 *	argument value			adr_int32
 *	argument description		adr_string
 * -----------------------------------------------------------------------
 */
int
argument32_token(pr_context_t *context)
{
	int	returnstat;

	returnstat = process_tag(context, TAG_ARGNUM, 0, 0);
	returnstat = process_tag(context, TAG_ARGVAL32, returnstat, 0);
	returnstat = process_tag(context, TAG_ARGDESC, returnstat, 1);

	return (returnstat);

}

/*
 * -----------------------------------------------------------------------
 * argument64_token()	: Process argument token and display contents
 * return codes		: -1 - error
 *			:  0 - successful
 * NOTE: At the time of call, the arg token id has been retrieved
 *
 * Format of 64 bit argument token:
 *	current directory token id	adr_char
 *	argument number			adr_char
 *	argument value			adr_int64
 *	argument description		adr_string
 * -----------------------------------------------------------------------
 */
int
argument64_token(pr_context_t *context)
{
	int	returnstat;

	returnstat = process_tag(context, TAG_ARGNUM, 0, 0);
	returnstat = process_tag(context, TAG_ARGVAL64, returnstat, 0);
	returnstat = process_tag(context, TAG_ARGDESC, returnstat, 1);

	return (returnstat);

}

/*
 * -----------------------------------------------------------------------
 * process_token() 	: Process process token and display contents
 * return codes		: -1 - error
 *			:  0 - successful
 * NOTE: At the time of call, the process token id has been retrieved
 *
 * Format of process token:
 *	process token id	adr_char
 *	auid			adr_u_int32
 *	euid			adr_u_int32
 *	egid			adr_u_int32
 *	ruid			adr_u_int32
 *	egid			adr_u_int32
 *	pid			adr_u_int32
 *	sid			adr_u_int32
 *	tid			adr_u_int32, adr_u_int32
 * -----------------------------------------------------------------------
 */
int
process32_token(pr_context_t *context)
{
	int	returnstat;

		/* auid */
	returnstat = process_tag(context, TAG_AUID, 0, 0);
		/* uid */
	returnstat = process_tag(context, TAG_UID, returnstat, 0);
		/* gid */
	returnstat = process_tag(context, TAG_GID, returnstat, 0);
		/* ruid */
	returnstat = process_tag(context, TAG_RUID, returnstat, 0);
		/* rgid */
	returnstat = process_tag(context, TAG_RGID, returnstat, 0);
		/* pid */
	returnstat = process_tag(context, TAG_PID, returnstat, 0);
		/* sid */
	returnstat = process_tag(context, TAG_SID, returnstat, 0);
		/* tid */
	returnstat = process_tag(context, TAG_TID32, returnstat, 1);

	return (returnstat);
}

int
process64_token(pr_context_t *context)
{
	int	returnstat;

		/* auid */
	returnstat = process_tag(context, TAG_AUID, 0, 0);
		/* uid */
	returnstat = process_tag(context, TAG_UID, returnstat, 0);
		/* gid */
	returnstat = process_tag(context, TAG_GID, returnstat, 0);
		/* ruid */
	returnstat = process_tag(context, TAG_RUID, returnstat, 0);
		/* rgid */
	returnstat = process_tag(context, TAG_RGID, returnstat, 0);
		/* pid */
	returnstat = process_tag(context, TAG_PID, returnstat, 0);
		/* sid */
	returnstat = process_tag(context, TAG_SID, returnstat, 0);
		/* tid */
	returnstat = process_tag(context, TAG_TID64, returnstat, 1);

	return (returnstat);
}

/*
 * -----------------------------------------------------------------------
 * process_ex_token()	: Process process token and display contents
 * return codes		: -1 - error
 *			:  0 - successful
 * NOTE: At the time of call, the process token id has been retrieved
 *
 * Format of extended process token:
 *	process token id	adr_char
 *	auid			adr_u_int32
 *	euid			adr_u_int32
 *	egid			adr_u_int32
 *	ruid			adr_u_int32
 *	egid			adr_u_int32
 *	pid			adr_u_int32
 *	sid			adr_u_int32
 *	tid			adr_u_int32, adr_u_int32, 4*adr_u_int32
 * -----------------------------------------------------------------------
 */
int
process32_ex_token(pr_context_t *context)
{
	int	returnstat;

		/* auid */
	returnstat = process_tag(context, TAG_AUID, 0, 0);
		/* uid */
	returnstat = process_tag(context, TAG_UID, returnstat, 0);
		/* gid */
	returnstat = process_tag(context, TAG_GID, returnstat, 0);
		/* ruid */
	returnstat = process_tag(context, TAG_RUID, returnstat, 0);
		/* rgid */
	returnstat = process_tag(context, TAG_RGID, returnstat, 0);
		/* pid */
	returnstat = process_tag(context, TAG_PID, returnstat, 0);
		/* sid */
	returnstat = process_tag(context, TAG_SID, returnstat, 0);
		/* tid */
	returnstat = process_tag(context, TAG_TID32_EX, returnstat, 1);

	return (returnstat);
}

int
process64_ex_token(pr_context_t *context)
{
	int	returnstat;

		/* auid */
	returnstat = process_tag(context, TAG_AUID, 0, 0);
		/* uid */
	returnstat = process_tag(context, TAG_UID, returnstat, 0);
		/* gid */
	returnstat = process_tag(context, TAG_GID, returnstat, 0);
		/* ruid */
	returnstat = process_tag(context, TAG_RUID, returnstat, 0);
		/* rgid */
	returnstat = process_tag(context, TAG_RGID, returnstat, 0);
		/* pid */
	returnstat = process_tag(context, TAG_PID, returnstat, 0);
		/* sid */
	returnstat = process_tag(context, TAG_SID, returnstat, 0);
		/* tid */
	returnstat = process_tag(context, TAG_TID64_EX, returnstat, 1);

	return (returnstat);
}

/*
 * -----------------------------------------------------------------------
 * return_value32_token(): Process return value and display contents
 * return codes		: -1 - error
 *			:  0 - successful
 * NOTE: At the time of call, the return value token id has been retrieved
 *
 * Format of return value token:
 * 	return value token id	adr_char
 *	error number		adr_char
 *	return value		adr_int32
 * -----------------------------------------------------------------------
 */
int
return_value32_token(pr_context_t *context)
{
	int		returnstat;
	uchar_t		number;
	int32_t		value;
	char		pb[512];    /* print buffer */
	uval_t		uval;
	bool_t		used_ret_val = 0;

	/*
	 * Every audit record generated contains a return token.
	 *
	 * The return token is a special token. It indicates the success
	 * or failure of the event that contains it.
	 * The return32 token contains two pieces of data:
	 *
	 * 	char	number;
	 * 	int32_t	return_value;
	 *
	 * For audit records generated by the kernel:
	 * The kernel always puts a positive value in "number".
	 * Upon success "number" is 0.
	 * Upon failure "number" is a positive errno value that is less than
	 * sys_nerr.
	 *
	 * For audit records generated at the user level:
	 * Upon success "number" is 0.
	 * Upon failure "number" is -1.
	 *
	 * For both kernel and user land the value of "return_value" is
	 * arbitrary. For the kernel it contains the return value of
	 * the system call. For user land it contains an arbitrary return
	 * value if it is less than ADT_FAIL_VALUE; ADT_FAIL_VALUE
	 * and above are messages defined in adt_event.h.   ADT_FAIL_PAM and
	 * above are messages from pam_strerror().  No interpretation is done
	 * on "return_value" if it is outside the range of ADT_FAIL_VALUE_* or
	 * ADT_FAIL_PAM values.
	 */
	if ((returnstat = open_tag(context, TAG_ERRVAL)) != 0)
		return (returnstat);

	if ((returnstat = pr_adr_u_char(context, &number, 1)) == 0) {
		if (!(context->format & PRF_RAWM)) {
			used_ret_val = 1;
			pa_error(number, pb, sizeof (pb));
			uval.uvaltype = PRA_STRING;
			uval.string_val = pb;
			if ((returnstat = pa_print(context, &uval, 0)) != 0)
				return (returnstat);
			if ((returnstat = close_tag(context, TAG_ERRVAL)) != 0)
				return (returnstat);
			if ((returnstat = open_tag(context, TAG_RETVAL)) != 0)
				return (returnstat);

			if ((returnstat = pr_adr_int32(
			    context, &value, 1)) != 0)
				return (returnstat);

			pa_retval(number, value, pb, sizeof (pb));
		} else {
			uval.uvaltype = PRA_INT32;
			if ((char)number == -1)
				uval.int32_val = -1;
			else
				uval.int32_val = number;
		}
		returnstat = pa_print(context, &uval, used_ret_val);
	}
	if (used_ret_val) {
		if (returnstat == 0)
			returnstat = close_tag(context, TAG_RETVAL);
		return (returnstat);
	}
	if (!returnstat)
		if (returnstat = close_tag(context, TAG_ERRVAL))
			return (returnstat);

	return (process_tag(context, TAG_RETVAL, returnstat, 1));
}

/*
 * -----------------------------------------------------------------------
 * return_value64_token(): Process return value and display contents
 * return codes		: -1 - error
 *			:  0 - successful
 * NOTE: At the time of call, the return value token id has been retrieved
 *
 * Format of return value token:
 * 	return value token id	adr_char
 *	error number		adr_char
 *	return value		adr_int64
 *
 * HOWEVER, the 64 bit return value is a concatenation of two
 * 32 bit return values; the first of which is the same as is
 * carried in the return32 token.  The second 32 bits are ignored
 * here so that the displayed return token will have the same
 * number whether the application is 32 or 64 bits.
 * -----------------------------------------------------------------------
 */
int
return_value64_token(pr_context_t *context)
{
	int		returnstat;
	uchar_t		number;
	rval_t		rval;
	char		pb[512];    /* print buffer */
	uval_t		uval;

	/*
	 * Every audit record generated contains a return token.
	 *
	 * The return token is a special token. It indicates the success
	 * or failure of the event that contains it.
	 * The return64 token contains two pieces of data:
	 *
	 * 	char	number;
	 * 	int64_t	return_value;
	 *
	 * For audit records generated by the kernel:
	 * The kernel always puts a positive value in "number".
	 * Upon success "number" is 0.
	 * Upon failure "number" is a positive errno value that is less than
	 * sys_nerr.
	 *
	 * For audit records generated at the user level:
	 * Upon success "number" is 0.
	 * Upon failure "number" is -1.
	 *
	 * For both kernel and user land the value of "return_value" is
	 * arbitrary. For the kernel it contains the return value of
	 * the system call. For user land it contains an arbitrary return
	 * value if it is less than ADT_FAIL_VALUE; ADT_FAIL_VALUE
	 * and above are messages defined in adt_event.h.   ADT_FAIL_PAM and
	 * above are messages from pam_strerror().  No interpretation is done
	 * on "return_value" if it is outside the range of ADT_FAIL_VALUE_* or
	 * ADT_FAIL_PAM values.
	 *
	 * The 64 bit return value consists of two 32bit parts; for
	 * system calls, the first part is the value returned by the
	 * system call and the second part depends on the system call
	 * implementation.  In most cases, the second part is either 0
	 * or garbage; because of that, it is omitted from the praudit
	 * output.
	 */
	if ((returnstat = open_tag(context, TAG_ERRVAL)) != 0)
		return (returnstat);

	if ((returnstat = pr_adr_u_char(context, &number, 1)) == 0) {
		if (!(context->format & PRF_RAWM)) {
			pa_error(number, pb, sizeof (pb));
			uval.uvaltype = PRA_STRING;
			uval.string_val = pb;
			if ((returnstat = pa_print(context, &uval, 0)) != 0)
				return (returnstat);

			if ((returnstat = close_tag(context, TAG_ERRVAL)) != 0)
				return (returnstat);
			if ((returnstat = open_tag(context, TAG_RETVAL)) != 0)
				return (returnstat);

			if ((returnstat = pr_adr_int64(context,
			    &rval.r_vals, 1)) != 0)
				return (returnstat);
			pa_retval(number, rval.r_val1, pb, sizeof (pb));
		} else {
			uval.uvaltype = PRA_INT32;
			if ((char)number == -1)
				uval.int32_val = -1;
			else
				uval.int32_val = number;

			if ((returnstat = pa_print(context, &uval, 0)) != 0)
				return (returnstat);

			if ((returnstat = close_tag(context, TAG_ERRVAL)) != 0)
				return (returnstat);
			if ((returnstat = open_tag(context, TAG_RETVAL)) != 0)
				return (returnstat);

			if ((returnstat = pr_adr_int64(context,
			    &rval.r_vals, 1)) != 0)
				return (returnstat);
			uval.int32_val = rval.r_val1;
		}
		returnstat = pa_print(context, &uval, 1);
	} else {
		return (returnstat);
	}

	if (returnstat == 0)
		returnstat = close_tag(context, TAG_RETVAL);

	return (returnstat);
}

/*
 * -----------------------------------------------------------------------
 * subject32_token()	: Process subject token and display contents
 * return codes		: -1 - error
 *			:  0 - successful
 * NOTE: At the time of call, the subject token id has been retrieved
 *
 * Format of subject token:
 *	subject token id	adr_char
 *	auid			adr_u_int32
 *	euid			adr_u_int32
 *	egid			adr_u_int32
 *	ruid			adr_u_int32
 *	egid			adr_u_int32
 *	pid			adr_u_int32
 *	sid			adr_u_int32
 *	tid			adr_u_int32, adr_u_int32
 * -----------------------------------------------------------------------
 */
int
subject32_token(pr_context_t *context)
{
	int	returnstat;

		/* auid */
	returnstat = process_tag(context, TAG_AUID, 0, 0);
		/* uid */
	returnstat = process_tag(context, TAG_UID, returnstat, 0);
		/* gid */
	returnstat = process_tag(context, TAG_GID, returnstat, 0);
		/* ruid */
	returnstat = process_tag(context, TAG_RUID, returnstat, 0);
		/* rgid */
	returnstat = process_tag(context, TAG_RGID, returnstat, 0);
		/* pid */
	returnstat = process_tag(context, TAG_PID, returnstat, 0);
		/* sid */
	returnstat = process_tag(context, TAG_SID, returnstat, 0);
		/* tid */
	returnstat = process_tag(context, TAG_TID32, returnstat, 1);

	return (returnstat);
}

int
subject64_token(pr_context_t *context)
{
	int	returnstat;

		/* auid */
	returnstat = process_tag(context, TAG_AUID, 0, 0);
		/* uid */
	returnstat = process_tag(context, TAG_UID, returnstat, 0);
		/* gid */
	returnstat = process_tag(context, TAG_GID, returnstat, 0);
		/* ruid */
	returnstat = process_tag(context, TAG_RUID, returnstat, 0);
		/* rgid */
	returnstat = process_tag(context, TAG_RGID, returnstat, 0);
		/* pid */
	returnstat = process_tag(context, TAG_PID, returnstat, 0);
		/* sid */
	returnstat = process_tag(context, TAG_SID, returnstat, 0);
		/* tid */
	returnstat = process_tag(context, TAG_TID64, returnstat, 1);

	return (returnstat);
}

/*
 * -----------------------------------------------------------------------
 * subject_ex_token(): Process subject token and display contents
 * return codes		: -1 - error
 *			:  0 - successful
 * NOTE: At the time of call, the subject token id has been retrieved
 *
 * Format of extended subject token:
 *	subject token id	adr_char
 *	auid			adr_u_int32
 *	euid			adr_u_int32
 *	egid			adr_u_int32
 *	ruid			adr_u_int32
 *	egid			adr_u_int32
 *	pid			adr_u_int32
 *	sid			adr_u_int32
 *	tid			adr_u_int32, adr_u_int32
 * -----------------------------------------------------------------------
 */
int
subject32_ex_token(pr_context_t *context)
{
	int	returnstat;

		/* auid */
	returnstat = process_tag(context, TAG_AUID, 0, 0);
		/* uid */
	returnstat = process_tag(context, TAG_UID, returnstat, 0);
		/* gid */
	returnstat = process_tag(context, TAG_GID, returnstat, 0);
		/* ruid */
	returnstat = process_tag(context, TAG_RUID, returnstat, 0);
		/* rgid */
	returnstat = process_tag(context, TAG_RGID, returnstat, 0);
		/* pid */
	returnstat = process_tag(context, TAG_PID, returnstat, 0);
		/* sid */
	returnstat = process_tag(context, TAG_SID, returnstat, 0);
		/* tid */
	returnstat = process_tag(context, TAG_TID32_EX, returnstat, 1);

	return (returnstat);
}

int
subject64_ex_token(pr_context_t *context)
{
	int	returnstat;

		/* auid */
	returnstat = process_tag(context, TAG_AUID, 0, 0);
		/* uid */
	returnstat = process_tag(context, TAG_UID, returnstat, 0);
		/* gid */
	returnstat = process_tag(context, TAG_GID, returnstat, 0);
		/* ruid */
	returnstat = process_tag(context, TAG_RUID, returnstat, 0);
		/* rgid */
	returnstat = process_tag(context, TAG_RGID, returnstat, 0);
		/* pid */
	returnstat = process_tag(context, TAG_PID, returnstat, 0);
		/* sid */
	returnstat = process_tag(context, TAG_SID, returnstat, 0);
		/* tid */
	returnstat = process_tag(context, TAG_TID64_EX, returnstat, 1);

	return (returnstat);
}

/*
 * -----------------------------------------------------------------------
 * s5_IPC_token()	: Process System V IPC token and display contents
 * return codes		: -1 - error
 *			:  0 - successful
 * NOTE: At the time of call, the System V IPC id has been retrieved
 *
 * Format of System V IPC token:
 *	System V IPC token id	adr_char
 *	object id		adr_int32
 * -----------------------------------------------------------------------
 */
int
s5_IPC_token(pr_context_t *context)
{
	int	returnstat;
	uchar_t ipctype;
	uval_t	uval;

	/*
	 * TRANSLATION_NOTE
	 * These names refer to the type of System V IPC object:
	 * message queue, semaphore, shared memory.
	 */

	if (pr_adr_u_char(context, &ipctype, 1) == 0) {
		if ((returnstat = open_tag(context, TAG_IPCTYPE)) != 0)
			return (returnstat);

		if (!(context->format & PRF_RAWM)) {
			/* print in ASCII form */
			uval.uvaltype = PRA_STRING;
			switch (ipctype) {
			case AT_IPC_MSG:
				uval.string_val = gettext("msg");
				break;
			case AT_IPC_SEM:
				uval.string_val = gettext("sem");
				break;
			case AT_IPC_SHM:
				uval.string_val = gettext("shm");
				break;
			}
			returnstat = pa_print(context, &uval, 0);
		}
		/* print in integer form */
		if ((context->format & PRF_RAWM) || (returnstat == 1)) {
			uval.uvaltype = PRA_BYTE;
			uval.char_val = ipctype;
			returnstat = pa_print(context, &uval, 0);
		}
		if ((returnstat = close_tag(context, TAG_IPCTYPE)) != 0)
			return (returnstat);

		/* next get and print ipc id */
		return (process_tag(context, TAG_IPCID, returnstat, 1));
	} else {
		/* cannot retrieve ipc type */
		return (-1);
	}
}

/*
 * -----------------------------------------------------------------------
 * text_token()	: Process text token and display contents
 * return codes	: -1 - error
 *		:  0 - successful
 * NOTE: At the time of call, the text token id has been retrieved
 *
 * Format of text token:
 *	text token id		adr_char
 * 	text			adr_string
 * -----------------------------------------------------------------------
 */
int
text_token(pr_context_t *context)
{
	return (pa_adr_string(context, 0, 1));
}

/*
 * -----------------------------------------------------------------------
 * tid_token()		: Process a generic terminal id token / AUT_TID
 * return codes 	: -1 - error
 *			:  0 - successful
 * NOTE: At the time of call, the token id has been retrieved
 *
 * Format of tid token:
 *	ip token id	adr_char
 *	terminal type	adr_char
 *  terminal type = AU_IPADR:
 *	remote port:	adr_short
 *	local port:	adr_short
 *	IP type:	adt_int32 -- AU_IPv4 or AU_IPv6
 *	address:	adr_int32 if IPv4, else 4 * adr_int32
 * -----------------------------------------------------------------------
 */
int
tid_token(pr_context_t *context)
{
	int		returnstat;
	uchar_t		type;
	uval_t		uval;

	if ((returnstat = pr_adr_u_char(context, &type, 1)) != 0)
		return (returnstat);
	uval.uvaltype = PRA_STRING;
	if ((returnstat = open_tag(context, TAG_TID_TYPE)) != 0)
		return (returnstat);

	switch (type) {
	default:
		return (-1);	/* other than IP type is not implemented */
	case AU_IPADR:
		uval.string_val = "ip";
		returnstat = pa_print(context, &uval, 0);
		returnstat = close_tag(context, TAG_TID_TYPE);
		returnstat = open_tag(context, TAG_IP);
		returnstat = process_tag(context, TAG_IP_REMOTE, returnstat, 0);
		returnstat = process_tag(context, TAG_IP_LOCAL, returnstat, 0);
		returnstat = process_tag(context, TAG_IP_ADR, returnstat, 1);
		returnstat = close_tag(context, TAG_IP);
		break;
	}
	return (returnstat);
}

/*
 * -----------------------------------------------------------------------
 * ip_addr_token() 	: Process ip token and display contents
 * return codes 	: -1 - error
 *			:  0 - successful
 * NOTE: At the time of call, the ip token id has been retrieved
 *
 * Format of ip address token:
 *	ip token id	adr_char
 *	address		adr_int32 (printed in hex)
 * -----------------------------------------------------------------------
 */

int
ip_addr_token(pr_context_t *context)
{
	return (pa_hostname(context, 0, 1));
}

int
ip_addr_ex_token(pr_context_t *context)
{
	int	returnstat;
	uint32_t	ip_addr[16];
	uint32_t	ip_type;
	struct in_addr	ia;
	char		*ipstring;
	char		buf[256];
	uval_t		uval;

	/* get address type */
	if ((returnstat = pr_adr_u_int32(context, &ip_type, 1)) != 0)
		return (returnstat);

	/* legal address types are either AU_IPv4 or AU_IPv6 only */
	if ((ip_type != AU_IPv4) && (ip_type != AU_IPv6))
		return (-1);

	/* get address (4/16) */
	if ((returnstat = pr_adr_char(context, (char *)ip_addr, ip_type)) != 0)
		return (returnstat);

	uval.uvaltype = PRA_STRING;
	if (ip_type == AU_IPv4) {
		uval.string_val = buf;

		if (!(context->format & PRF_RAWM)) {
			get_Hname(ip_addr[0], buf, sizeof (buf));
			return (pa_print(context, &uval, 1));
		}

		ia.s_addr = ip_addr[0];
		if ((ipstring = inet_ntoa(ia)) == NULL)
			return (-1);

		(void) snprintf(buf, sizeof (buf), "%s", ipstring);

	} else {
		uval.string_val = buf;

		if (!(context->format & PRF_RAWM)) {
			get_Hname_ex(ip_addr, buf, sizeof (buf));
			return (pa_print(context, &uval, 1));
		}

		(void) inet_ntop(AF_INET6, (void *) ip_addr, buf,
		    sizeof (buf));

	}

	return (pa_print(context, &uval, 1));
}

/*
 * -----------------------------------------------------------------------
 * ip_token()		: Process ip header token and display contents
 * return codes 	: -1 - error
 *			:  0 - successful
 * NOTE: At the time of call, the ip token id has been retrieved
 *
 * Format of ip header token:
 *	ip header token id	adr_char
 *	version			adr_char (printed in hex)
 *	type of service		adr_char (printed in hex)
 *	length			adr_short
 *	id			adr_u_short
 *	offset			adr_u_short
 *	ttl			adr_char (printed in hex)
 *	protocol		adr_char (printed in hex)
 *	checksum		adr_u_short
 *	source address		adr_int32 (printed in hex)
 *	destination address	adr_int32 (printed in hex)
 * -----------------------------------------------------------------------
 */
int
ip_token(pr_context_t *context)
{
	int	returnstat;

	returnstat = process_tag(context, TAG_IPVERS, 0, 0);
	returnstat = process_tag(context, TAG_IPSERV, returnstat, 0);
	returnstat = process_tag(context, TAG_IPLEN, returnstat, 0);
	returnstat = process_tag(context, TAG_IPID, returnstat, 0);
	returnstat = process_tag(context, TAG_IPOFFS, returnstat, 0);
	returnstat = process_tag(context, TAG_IPTTL, returnstat, 0);
	returnstat = process_tag(context, TAG_IPPROTO, returnstat, 0);
	returnstat = process_tag(context, TAG_IPCKSUM, returnstat, 0);
	returnstat = process_tag(context, TAG_IPSRC, returnstat, 0);
	returnstat = process_tag(context, TAG_IPDEST, returnstat, 1);

	return (returnstat);
}

/*
 * -----------------------------------------------------------------------
 * iport_token() 	: Process ip port address token and display contents
 * return codes		: -1 - error
 *			:  0 - successful
 * NOTE: At time of call, the ip port address token id has been retrieved
 *
 * Format of ip port token:
 *	ip port address token id	adr_char
 *	port address			adr_short (in_port_t == uint16_t)
 * -----------------------------------------------------------------------
 */
int
iport_token(pr_context_t *context)
{
	return (pa_adr_u_short(context, 0, 1));
}

/*
 * -----------------------------------------------------------------------
 * socket_token() 	: Process socket token and display contents
 * return codes		: -1 - error
 *			:  0 - successful
 * NOTE: At time of call, the socket token id has been retrieved
 *
 * Format of socket token:
 *	ip socket token id		adr_char
 *	socket type			adr_short (in hex)
 *	foreign port			adr_short (in hex)
 *	foreign internet address	adr_hostname/adr_int32 (in ascii/hex)
 * -----------------------------------------------------------------------
 *
 * Note: local port and local internet address have been removed for 5.x
 */
int
socket_token(pr_context_t *context)
{
	int	returnstat;

	returnstat = process_tag(context, TAG_SOCKTYPE, 0, 0);
	returnstat = process_tag(context, TAG_SOCKPORT, returnstat, 0);
	if (returnstat != 0)
		return (returnstat);

	if ((returnstat = open_tag(context, TAG_SOCKADDR)) != 0)
		return (returnstat);

	if ((returnstat = pa_hostname(context, returnstat, 1)) != 0)
		return (returnstat);

	return (close_tag(context, TAG_SOCKADDR));
}

/*
 * -----------------------------------------------------------------------
 * socket_ex_token()	: Process socket token and display contents
 * return codes		: -1 - error
 *			:  0 - successful
 * NOTE: At time of call, the extended socket token id has been retrieved
 *
 * Format of extended socket token:
 *	token id			adr_char
 *	socket domain			adr_short (in hex)
 *	socket type			adr_short (in hex)
 *	IP address type			adr_short (in hex) [not displayed]
 *	local port			adr_short (in hex)
 *	local internet address		adr_hostname/adr_int32 (in ascii/hex)
 *	foreign port			adr_short (in hex)
 *	foreign internet address	adr_hostname/adr_int32 (in ascii/hex)
 * -----------------------------------------------------------------------
 *
 * Note: local port and local internet address have been removed for 5.x
 */
int
socket_ex_token(pr_context_t *context)
{
	int	returnstat;

	returnstat = process_tag(context, TAG_SOCKEXDOM, 0, 0);
	returnstat = process_tag(context, TAG_SOCKEXTYPE, returnstat, 0);
	returnstat = pa_hostname_so(context, returnstat, 1);

	return (returnstat);
}

/*
 * -----------------------------------------------------------------------
 * sequence_token()	: Process sequence token and display contents
 * return codes		: -1 - error
 *			:  0 - successful
 * NOTE: At time of call, the socket token id has been retrieved
 *
 * Format of sequence token:
 *	sequence token id		adr_char
 *	sequence number 		adr_u_int32 (in hex)
 * -----------------------------------------------------------------------
 */
int
sequence_token(pr_context_t *context)
{
	return (process_tag(context, TAG_SEQNUM, 0, 1));
}

/*
 * -----------------------------------------------------------------------
 * acl_token()	: Process access control list term
 * return codes	: -1 - error
 *		:  0 - successful
 *
 * Format of acl token:
 *	token id	adr_char
 *	term type	adr_u_int32
 *	term value	adr_u_int32 (depends on type)
 *	file mode	adr_u_int (in octal)
 * -----------------------------------------------------------------------
 */
int
acl_token(pr_context_t *context)
{
	int	returnstat;

	returnstat = pa_pw_uid_gr_gid(context, 0, 0);

	return (process_tag(context, TAG_MODE, returnstat, 1));
}

/*
 * -----------------------------------------------------------------------
 * ace_token()	: Process ZFS/NFSv4 access control list term
 * return codes	: -1 - error
 *		:  0 - successful
 *
 * Format of ace token:
 *	token id	adr_char
 *	term who	adr_u_int32 (uid/gid)
 *	term mask	adr_u_int32
 *	term flags	adr_u_int16
 *	term type	adr_u_int16
 * -----------------------------------------------------------------------
 */
int
ace_token(pr_context_t *context)
{
	return (pa_ace(context, 0, 1));
}

/*
 * -----------------------------------------------------------------------
 * attribute_token()	: Process attribute token and display contents
 * return codes 	: -1 - error
 *			:  0 - successful
 * NOTE: At the time of call, the attribute token id has been retrieved
 *
 * Format of attribute token:
 *	attribute token id	adr_char
 * 	mode			adr_u_int (printed in octal)
 *	uid			adr_u_int
 *	gid			adr_u_int
 *	file system id		adr_int
 *
 *	node id			adr_int		(attribute_token
 *						 pre SunOS 5.7)
 *	device			adr_u_int
 * or
 *	node id			adr_int64	(attribute32_token)
 *	device			adr_u_int
 * or
 *	node id			adr_int64	(attribute64_token)
 *	device			adr_u_int64
 * -----------------------------------------------------------------------
 */
int
attribute_token(pr_context_t *context)
{
	int	returnstat;

	returnstat = process_tag(context, TAG_MODE, 0, 0);
	returnstat = process_tag(context, TAG_UID, returnstat, 0);
	returnstat = process_tag(context, TAG_GID, returnstat, 0);
	returnstat = process_tag(context, TAG_FSID, returnstat, 0);
	returnstat = process_tag(context, TAG_NODEID32, returnstat, 0);
	returnstat = process_tag(context, TAG_DEVICE32, returnstat, 1);

	return (returnstat);
}

int
attribute32_token(pr_context_t *context)
{
	int	returnstat;

	returnstat = process_tag(context, TAG_MODE, 0, 0);
	returnstat = process_tag(context, TAG_UID, returnstat, 0);
	returnstat = process_tag(context, TAG_GID, returnstat, 0);
	returnstat = process_tag(context, TAG_FSID, returnstat, 0);
	returnstat = process_tag(context, TAG_NODEID64, returnstat, 0);
	returnstat = process_tag(context, TAG_DEVICE32, returnstat, 1);

	return (returnstat);
}

int
attribute64_token(pr_context_t *context)
{
	int	returnstat;

	returnstat = process_tag(context, TAG_MODE, 0, 0);
	returnstat = process_tag(context, TAG_UID, returnstat, 0);
	returnstat = process_tag(context, TAG_GID, returnstat, 0);
	returnstat = process_tag(context, TAG_FSID, returnstat, 0);
	returnstat = process_tag(context, TAG_NODEID64, returnstat, 0);
	returnstat = process_tag(context, TAG_DEVICE64, returnstat, 1);

	return (returnstat);
}

/*
 * -----------------------------------------------------------------------
 * group_token() 	: Process group token and display contents
 * return codes 	: -1 - error
 *			:  0 - successful
 * NOTE: At the time of call, the group token id has been retrieved
 *
 * Format of group token:
 *	group token id		adr_char
 *	group list		adr_long, 16 times
 * -----------------------------------------------------------------------
 */
int
group_token(pr_context_t *context)
{
	int	returnstat = 0;
	int	i;

	for (i = 0; i < NGROUPS_MAX - 1; i++) {
		if ((returnstat = process_tag(context, TAG_GROUPID,
		    returnstat, 0)) < 0)
			return (returnstat);
	}

	return (process_tag(context, TAG_GROUPID, returnstat, 1));
}

/*
 * -----------------------------------------------------------------------
 * newgroup_token() 	: Process group token and display contents
 * return codes 	: -1 - error
 *			:  0 - successful
 * NOTE: At the time of call, the group token id has been retrieved
 *
 * Format of new group token:
 *	group token id		adr_char
 *	group number		adr_short
 *	group list		adr_int32, group number times
 * -----------------------------------------------------------------------
 */
int
newgroup_token(pr_context_t *context)
{
	int	returnstat;
	int	i, num;
	short	n_groups;

	returnstat = pr_adr_short(context, &n_groups, 1);
	if (returnstat != 0)
		return (returnstat);

	num = (int)n_groups;
	if (num == 0) {
		if (!(context->format & PRF_XMLM)) {
			returnstat = do_newline(context, 1);
		}
		return (returnstat);
	}
	for (i = 0; i < num - 1; i++) {
		if ((returnstat = process_tag(context, TAG_GROUPID,
		    returnstat, 0)) < 0)
			return (returnstat);
	}

	return (process_tag(context, TAG_GROUPID, returnstat, 1));
}

static int
string_token_common(pr_context_t *context, int tag)
{
	int	returnstat;
	int	num;

	returnstat = pr_adr_int32(context, (int32_t *)&num, 1);
	if (returnstat != 0)
		return (returnstat);

	if (!(context->format & PRF_XMLM)) {
		returnstat = pr_printf(context, "%d%s", num,
		    context->SEPARATOR);
		if (returnstat != 0)
			return (returnstat);
	}

	if (num == 0)
		return (do_newline(context, 1));

	for (; num > 1; num--) {
		if ((returnstat = (process_tag(context, tag,
		    returnstat, 0))) < 0)
			return (returnstat);
	}

	return (process_tag(context, tag, returnstat, 1));
}

int
path_attr_token(pr_context_t *context)
{
	return (string_token_common(context, TAG_XAT));
}

int
exec_args_token(pr_context_t *context)
{
	return (string_token_common(context, TAG_ARG));
}

int
exec_env_token(pr_context_t *context)
{
	return (string_token_common(context, TAG_ENV));
}

/*
 * -----------------------------------------------------------------------
 * s5_IPC_perm_token() : Process System V IPC permission token and display
 *			 contents
 * return codes 	: -1 - error
 *			:  0 - successful
 * NOTE: At the time of call, the System V IPC permission token id
 * has been retrieved
 *
 * Format of System V IPC permission token:
 *	System V IPC permission token id	adr_char
 * 	uid					adr_u_int32
 *	gid					adr_u_int32
 *	cuid					adr_u_int32
 *	cgid					adr_u_int32
 *	mode					adr_u_int32
 *	seq					adr_u_int32
 *	key					adr_int32
 * -----------------------------------------------------------------------
 */
int
s5_IPC_perm_token(pr_context_t *context)
{
	int	returnstat;

	returnstat = process_tag(context, TAG_UID, 0, 0);
	returnstat = process_tag(context, TAG_GID, returnstat, 0);
	returnstat = process_tag(context, TAG_CUID, returnstat, 0);
	returnstat = process_tag(context, TAG_CGID, returnstat, 0);
	returnstat = process_tag(context, TAG_MODE, returnstat, 0);
	returnstat = process_tag(context, TAG_SEQ, returnstat, 0);
	returnstat = process_tag(context, TAG_KEY, returnstat, 1);

	return (returnstat);
}

/*
 * -----------------------------------------------------------------------
 * host_token()	: Process host token and display contents
 * return codes	: -1 - error
 *		:  0 - successful
 * NOTE: At the time of call, the host token id has been retrieved
 *
 * Format of host token:
 *	host token id		adr_char
 *	hostid			adr_u_int32
 * -----------------------------------------------------------------------
 */
int
host_token(pr_context_t *context)
{
	return (pa_hostname(context, 0, 1));
}

/*
 * -----------------------------------------------------------------------
 * liaison_token()	: Process liaison token and display contents
 * return codes 	: -1 - error
 *			:  0 - successful
 * NOTE: At the time of call, the liaison token id has been retrieved
 *
 * Format of liaison token:
 *	liaison token id	adr_char
 *	liaison			adr_u_int32
 * -----------------------------------------------------------------------
 */
int
liaison_token(pr_context_t *context)
{
	return (pa_liaison(context, 0, 1));
}

/*
 * -----------------------------------------------------------------------
 * useofauth_token(): Process useofauth token and display contents
 * return codes	: -1 - error
 *		:  0 - successful
 * NOTE: At the time of call, the uauth token id has been retrieved
 *
 * Format of useofauth token:
 *	uauth token id		adr_char
 * 	uauth			adr_string
 * -----------------------------------------------------------------------
 */
int
useofauth_token(pr_context_t *context)
{
	return (pa_adr_string(context, 0, 1));
}

/*
 * -----------------------------------------------------------------------
 * zonename_token(): Process zonename token and display contents
 * return codes	: -1 - error
 *		:  0 - successful
 * NOTE: At the time of call, the zonename token id has been retrieved
 *
 * Format of zonename token:
 *	zonename token id	adr_char
 * 	zone name		adr_string
 * -----------------------------------------------------------------------
 */
int
zonename_token(pr_context_t *context)
{
	return (process_tag(context, TAG_ZONENAME, 0, 1));
}

/*
 * -----------------------------------------------------------------------
 * fmri_token(): Process fmri token and display contents
 * return codes	: -1 - error
 *		:  0 - successful
 * NOTE: At the time of call, the fmri token id has been retrieved
 *
 * Format of fmri token:
 *	fmri token id		adr_char
 * 	service instance name	adr_string
 * -----------------------------------------------------------------------
 */
int
fmri_token(pr_context_t *context)
{
	return (pa_adr_string(context, 0, 1));
}

/*
 * -----------------------------------------------------------------------
 * xatom_token()	: Process Xatom token and display contents in hex.
 * return codes		: -1 - error
 *			:  0 - successful
 * NOTE: At the time of call, the xatom token id has been retrieved
 *
 * Format of xatom token:
 *	token id		adr_char
 * 	length			adr_short
 * 	atom			adr_char length times
 * -----------------------------------------------------------------------
 */
int
xatom_token(pr_context_t *context)
{
	return (pa_adr_string(context, 0, 1));
}

int
xcolormap_token(pr_context_t *context)
{
	return (pa_xgeneric(context));
}

int
xcursor_token(pr_context_t *context)
{
	return (pa_xgeneric(context));
}

int
xfont_token(pr_context_t *context)
{
	return (pa_xgeneric(context));
}

int
xgc_token(pr_context_t *context)
{
	return (pa_xgeneric(context));
}

int
xpixmap_token(pr_context_t *context)
{
	return (pa_xgeneric(context));
}

int
xwindow_token(pr_context_t *context)
{
	return (pa_xgeneric(context));
}

/*
 * -----------------------------------------------------------------------
 * xproperty_token(): Process Xproperty token and display contents
 *
 * return codes		: -1 - error
 *			:  0 - successful
 * NOTE: At the time of call, the xproperty token id has been retrieved
 *
 * Format of xproperty token:
 *	token id		adr_char
 *	XID			adr_u_int32
 *	creator UID		adr_u_int32
 *	text			adr_text
 * -----------------------------------------------------------------------
 */
int
xproperty_token(pr_context_t *context)
{
	int	returnstat;

	returnstat = process_tag(context, TAG_XID, 0, 0);
	returnstat = process_tag(context, TAG_XCUID, returnstat, 0);

	/* Done with attributes; force end of token open */
	if (returnstat == 0)
		returnstat = finish_open_tag(context);

	returnstat = pa_adr_string(context, returnstat, 1);

	return (returnstat);
}

/*
 * -----------------------------------------------------------------------
 * xselect_token(): Process Xselect token and display contents in hex
 *
 * return codes		: -1 - error
 *			:  0 - successful
 * NOTE: At the time of call, the xselect token id has been retrieved
 *
 * Format of xselect token
 *	text token id		adr_char
 * 	property text		adr_string
 * 	property type		adr_string
 * 	property data		adr_string
 * -----------------------------------------------------------------------
 */
int
xselect_token(pr_context_t *context)
{
	int	returnstat;

	returnstat = process_tag(context, TAG_XSELTEXT, 0, 0);
	returnstat = process_tag(context, TAG_XSELTYPE, returnstat, 0);
	returnstat = process_tag(context, TAG_XSELDATA, returnstat, 1);

	return (returnstat);
}

/*
 * -----------------------------------------------------------------------
 * xclient_token(): Process Xclient token and display contents in hex.
 *
 * return codes		: -1 - error
 *			:  0 - successful
 *
 * Format of xclient token:
 *	token id		adr_char
 * 	client			adr_int32
 * -----------------------------------------------------------------------
 */
int
xclient_token(pr_context_t *context)
{
	return (pa_adr_int32(context, 0, 1));
}

/*
 * -----------------------------------------------------------------------
 * label_token() 	: Process label token and display contents
 * return codes 	: -1 - error
 *			: 0 - successful
 * NOTE: At the time of call, the label token id has been retrieved
 *
 * Format of label token:
 *	label token id			adr_char
 *      label ID                	adr_char
 *      label compartment length	adr_char
 *      label classification		adr_short
 *      label compartment words		<compartment length> * 4 adr_char
 * -----------------------------------------------------------------------
 */
/*ARGSUSED*/
int
label_token(pr_context_t *context)
{
	static m_label_t *label = NULL;
	static size32_t l_size;
	int	len;
	int	returnstat;
	uval_t	uval;

	if (label == NULL) {
		if ((label = m_label_alloc(MAC_LABEL)) == NULL) {
			return (-1);
		}
		l_size = blabel_size() - 4;
	}
	if ((returnstat = pr_adr_char(context, (char *)label, 4)) == 0) {
		len = (int)(((char *)label)[1] * 4);
		if ((len > l_size) ||
		    (pr_adr_char(context, &((char *)label)[4], len) != 0)) {
			return (-1);
		}
		uval.uvaltype = PRA_STRING;
		if (!(context->format & PRF_RAWM)) {
			/* print in ASCII form */
			if (label_to_str(label, &uval.string_val, M_LABEL,
			    DEF_NAMES) == 0) {
				returnstat = pa_print(context, &uval, 1);
			} else /* cannot convert to string */
				returnstat = 1;
		}
		/* print in hexadecimal form */
		if ((context->format & PRF_RAWM) || (returnstat == 1)) {
			uval.string_val = hexconvert((char *)label, len, len);
			if (uval.string_val) {
				returnstat = pa_print(context, &uval, 1);
			}
		}
		free(uval.string_val);
	}
	return (returnstat);
}

/*
 * -----------------------------------------------------------------------
 * useofpriv_token() : Process priv token and display contents
 * return codes 	: -1 - error
 *			:  0 - successful
 * NOTE: At the time of call, the useofpriv token id has been retrieved
 *
 * Format of useofpriv token:
 *	useofpriv token id	adr_char
 *	success/failure flag	adr_char
 *	priv			adr_int32 (Trusted Solaris)
 *	priv_set		'\0' separated privileges.
 * -----------------------------------------------------------------------
 */
/*ARGSUSED*/
int
useofpriv_token(pr_context_t *context)
{
	int	returnstat;
	char	sf;
	uval_t	uval;

	if ((returnstat = pr_adr_char(context, &sf, 1)) != 0) {
		return (returnstat);
	}
	if (!(context->format & PRF_RAWM)) {
		/* print in ASCII form */

		if ((returnstat = open_tag(context, TAG_RESULT)) != 0)
			return (returnstat);

		uval.uvaltype = PRA_STRING;
		if (sf) {
			uval.string_val = gettext("successful use of priv");
			returnstat = pa_print(context, &uval, 0);
		} else {
			uval.string_val = gettext("failed use of priv");
			returnstat = pa_print(context, &uval, 0);
		}
		if (returnstat == 0)
			returnstat = close_tag(context, TAG_RESULT);

		/* Done with attributes; force end of token open */
		if (returnstat == 0)
			returnstat = finish_open_tag(context);
	} else {
		/* print in hexadecimal form */
		if ((returnstat = open_tag(context, TAG_RESULT)) != 0)
			return (returnstat);
		uval.uvaltype = PRA_SHORT;
		uval.short_val = sf;
		returnstat = pa_print(context, &uval, 0);
		if (returnstat == 0)
			returnstat = close_tag(context, TAG_RESULT);

		/* Done with attributes; force end of token open */
		if (returnstat == 0)
			returnstat = finish_open_tag(context);
	}
	return (pa_adr_string(context, 0, 1));
}

/*
 * -----------------------------------------------------------------------
 * privilege_token()	: Process privilege token and display contents
 * return codes 	: -1 - error
 *			:  0 - successful
 * NOTE: At the time of call, the privilege token id has been retrieved
 *
 * Format of privilege token:
 *	privilege token id	adr_char
 *	privilege type		adr_string
 *	privilege		adr_string
 * -----------------------------------------------------------------------
 */
int
privilege_token(pr_context_t *context)
{
	int	returnstat;

	/* privilege type: */
	returnstat = process_tag(context, TAG_SETTYPE, 0, 0);

	/* Done with attributes; force end of token open */
	if (returnstat == 0)
		returnstat = finish_open_tag(context);

	/* privilege: */
	return (pa_adr_string(context, returnstat, 1));
}