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
|
/*
* 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 2010 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
* Copyright 2012 Milan Jurik. All rights reserved.
*/
/*
* Token processing for sysupd; each token function does one
* or more operations. All of them bump the buffer pointer
* to the next token; some of them extract one or more data
* from the token.
*/
#define DEBUG 0
#if DEBUG
#define DPRINT(x) { (void) fprintf x; }
#else
#define DPRINT(x)
#endif
#include <locale.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <bsm/libbsm.h>
#include <sys/tsol/label.h>
#include "toktable.h" /* ../praudit */
#include "sysplugin.h"
#include "systoken.h"
#include <audit_plugin.h>
#if DEBUG
static FILE *dbfp; /* debug file */
#endif
static void anchor_path(char *);
static size_t collapse_path(char *, size_t);
static void get_bytes_to_string(parse_context_t *, size_t *, char **,
size_t);
static void skip_bytes(parse_context_t *);
static void skip_string(parse_context_t *);
static int xgeneric(parse_context_t *);
/*
* Process a token in a record to (1) extract data of interest if any
* and (2) point to the next token.
*
* returns 0 if ok. + or - values are of debug value:
*
* returns -1 if the parsing of the token failed.
*
* returns +<previous id> if the token is not found. This value
* is used to help determine where in the record the problem
* occurred. The common failure case is that the parsing of
* token M is incorrect and the buffer pointer ends up pointing
* to garbage. The positive error value of M *may* be the id of
* the incorrectly parsed token.
*/
int
parse_token(parse_context_t *ctx)
{
char tokenid;
static char prev_tokenid = -1;
int rc;
#if DEBUG
static boolean_t first = 1;
if (first) {
dbfp = __auditd_debug_file_open();
first = 0;
}
#endif
adrm_char(&(ctx->adr), &tokenid, 1);
if ((tokenid > 0) && (tokentable[tokenid].func != NOFUNC)) {
rc = (*tokentable[tokenid].func)(ctx);
prev_tokenid = tokenid;
return (rc);
}
/* here if token id is not in table */
return (prev_tokenid);
}
/* There should not be any file tokens in the middle of a record */
/* ARGSUSED */
int
file_token(parse_context_t *ctx)
{
return (-1);
}
/* ARGSUSED */
int
file64_token(parse_context_t *ctx)
{
return (-1);
}
static void
common_header(parse_context_t *ctx)
{
adrm_u_int32(&(ctx->adr), &(ctx->out.sf_reclen), 1);
ctx->adr.adr_now += sizeof (char); /* version number */
adrm_u_short(&(ctx->adr), &(ctx->out.sf_eventid), 1);
ctx->adr.adr_now += sizeof (short); /* modifier */
}
/*
* 32bit header
*/
int
header_token(parse_context_t *ctx)
{
common_header(ctx);
ctx->adr.adr_now += 2 * sizeof (int32_t); /* time */
return (0);
}
int
header32_ex_token(parse_context_t *ctx)
{
int32_t type;
common_header(ctx);
adrm_int32(&(ctx->adr), &type, 1); /* tid type */
ctx->adr.adr_now += type * sizeof (char); /* ip address */
ctx->adr.adr_now += 2 * sizeof (int32_t); /* time */
return (0);
}
int
header64_ex_token(parse_context_t *ctx)
{
int32_t type;
common_header(ctx);
adrm_int32(&(ctx->adr), &type, 1); /* tid type */
ctx->adr.adr_now += type * sizeof (char); /* ip address */
ctx->adr.adr_now += 2 * sizeof (int64_t); /* time */
return (0);
}
int
header64_token(parse_context_t *ctx)
{
common_header(ctx);
ctx->adr.adr_now += 2 * sizeof (int64_t); /* time */
return (0);
}
/*
* ======================================================
* The following token processing routines return
* 0: if parsed ok
* -1: can't parse and can't determine location of next token
* ======================================================
*/
int
trailer_token(parse_context_t *ctx)
{
short magic_number;
uint32_t bytes;
adrm_u_short(&(ctx->adr), (ushort_t *)&magic_number, 1);
if (magic_number != AUT_TRAILER_MAGIC)
return (-1);
adrm_u_int32(&(ctx->adr), &bytes, 1);
return (0);
}
/*
* Format of arbitrary data token:
* arbitrary data token id &(ctx->adr) char
* how to print adr_char
* basic unit adr_char
* unit count adr_char, specifying number of units of
* data items depends on basic unit
*
*/
int
arbitrary_data_token(parse_context_t *ctx)
{
char basic_unit, unit_count;
ctx->adr.adr_now += sizeof (char); /* how to print */
adrm_char(&(ctx->adr), &basic_unit, 1);
adrm_char(&(ctx->adr), &unit_count, 1);
switch (basic_unit) {
case AUR_CHAR: /* same as AUR_BYTE */
ctx->adr.adr_now += unit_count * sizeof (char);
break;
case AUR_SHORT:
ctx->adr.adr_now += unit_count * sizeof (short);
break;
case AUR_INT32: /* same as AUR_INT */
ctx->adr.adr_now += unit_count * sizeof (int32_t);
break;
case AUR_INT64:
ctx->adr.adr_now += unit_count * sizeof (int64_t);
break;
default:
return (-1);
}
return (0);
}
/*
* Format of opaque token:
* opaque token id adr_char
* size adr_short
* data adr_char, size times
*
*/
int
opaque_token(parse_context_t *ctx)
{
skip_bytes(ctx);
return (0);
}
/*
* Format of return32 value token:
* return value token id adr_char
* error number adr_char
* return value adr_u_int32
*
*/
int
return_value32_token(parse_context_t *ctx)
{
char errnum;
adrm_char(&(ctx->adr), &errnum, 1); /* pass / fail */
ctx->adr.adr_now += sizeof (int32_t); /* error code */
ctx->out.sf_pass = (errnum == 0) ? 1 : -1;
return (0);
}
/*
* Format of return64 value token:
* return value token id adr_char
* error number adr_char
* return value adr_u_int64
*
*/
int
return_value64_token(parse_context_t *ctx)
{
char errnum;
adrm_char(&(ctx->adr), &errnum, 1); /* pass / fail */
ctx->adr.adr_now += sizeof (int64_t); /* error code */
ctx->out.sf_pass = (errnum == 0) ? 1 : -1;
return (0);
}
/*
* Format of sequence token:
* sequence token id adr_char
* audit_count int32_t
*
*/
int
sequence_token(parse_context_t *ctx)
{
adrm_int32(&(ctx->adr), &(ctx->out.sf_sequence), 1);
return (0);
}
/*
* Format of text token:
* text token id adr_char
* text adr_string
*/
int
text_token(parse_context_t *ctx)
{
ushort_t len;
size_t separator_sz = 0;
char *bp; /* pointer to output string */
adrm_u_short(&(ctx->adr), &len, 1);
if (ctx->out.sf_textlen > 0)
separator_sz = sizeof (AU_TEXT_NAME) - 1;
DPRINT((dbfp, "text_token: start length=%d, add length=%d+%d\n",
ctx->out.sf_textlen, (size_t)len, separator_sz));
ctx->out.sf_text = realloc(ctx->out.sf_text,
ctx->out.sf_textlen + (size_t)len + separator_sz);
if (ctx->out.sf_text == NULL)
return (-1);
bp = ctx->out.sf_text;
if (ctx->out.sf_textlen != 0) { /* concatenation? */
bp += ctx->out.sf_textlen;
bp += strlcpy(bp, AU_TEXT_NAME, separator_sz + 1);
ctx->out.sf_textlen += separator_sz;
DPRINT((dbfp, "text_token: l is %d\n%s\n", ctx->out.sf_textlen,
ctx->out.sf_text));
}
adrm_char(&(ctx->adr), bp, len);
len--; /* includes EOS */
*(bp + len) = '\0';
ctx->out.sf_textlen += len;
DPRINT((dbfp, "text_token: l=%d\n%s\n", ctx->out.sf_textlen,
ctx->out.sf_text));
return (0);
}
/*
* Format of tid token:
* ip token id adr_char
* terminal type adr_char
* terminal type = AU_IPADR:
* remote port: ushort
* local port: ushort
* IP type: int32 -- AU_IPv4 or AU_IPv6
* address: int32 if IPv4, else 4 * int32
*/
int
tid_token(parse_context_t *ctx)
{
uchar_t type;
int32_t ip_length;
adrm_char(&(ctx->adr), (char *)&type, 1);
switch (type) {
default:
return (-1); /* other than IP type is not implemented */
case AU_IPADR:
ctx->adr.adr_now += 2 * sizeof (ushort_t);
adrm_int32(&(ctx->adr), &ip_length, 1);
ctx->adr.adr_now += ip_length;
break;
}
return (0);
}
/*
* Format of ip_addr token:
* ip token id adr_char
* address adr_int32
*
*/
int
ip_addr_token(parse_context_t *ctx)
{
ctx->adr.adr_now += sizeof (int32_t);
return (0);
}
/*
* Format of ip_addr_ex token:
* ip token id adr_char
* ip type adr_int32
* ip address adr_u_char*type
*
*/
int
ip_addr_ex_token(parse_context_t *ctx)
{
int32_t type;
adrm_int32(&(ctx->adr), &type, 1); /* ip type */
ctx->adr.adr_now += type * sizeof (uchar_t); /* ip address */
return (0);
}
/*
* Format of ip token:
* ip header token id adr_char
* version adr_char
* type of service adr_char
* length adr_short
* id adr_u_short
* offset adr_u_short
* ttl adr_char
* protocol adr_char
* checksum adr_u_short
* source address adr_int32
* destination address adr_int32
*
*/
int
ip_token(parse_context_t *ctx)
{
ctx->adr.adr_now += (2 * sizeof (char)) + (3 * sizeof (short)) +
(2 * sizeof (char)) + sizeof (short) + (2 * sizeof (int32_t));
return (0);
}
/*
* Format of iport token:
* ip port address token id adr_char
* port address adr_short
*
*/
int
iport_token(parse_context_t *ctx)
{
ctx->adr.adr_now += sizeof (short);
return (0);
}
/*
* Format of groups token:
* group token id adr_char
* group list adr_int32, 16 times
*
*/
int
group_token(parse_context_t *ctx)
{
ctx->adr.adr_now += 16 * sizeof (int32_t);
return (0);
}
/*
* Format of newgroups token:
* group token id adr_char
* number of groups adr_short
* group list adr_int32, "number" times
*
*/
int
newgroup_token(parse_context_t *ctx)
{
short int number;
adrm_short(&(ctx->adr), &number, 1);
ctx->adr.adr_now += number * sizeof (int32_t);
return (0);
}
/*
* Format of argument32 token:
* argument token id adr_char
* argument number adr_char
* argument value adr_int32
* argument description adr_string
*
*/
int
argument32_token(parse_context_t *ctx)
{
ctx->adr.adr_now += sizeof (char) + sizeof (int32_t);
skip_bytes(ctx);
return (0);
}
/*
* Format of argument64 token:
* argument token id adr_char
* argument number adr_char
* argument value adr_int64
* argument description adr_string
*
*/
int
argument64_token(parse_context_t *ctx)
{
ctx->adr.adr_now += sizeof (char) + sizeof (int64_t);
skip_bytes(ctx);
return (0);
}
/*
* Format of acl token:
* acl token id adr_char
* type adr_u_int32
* value adr_u_int32
* mode adr_u_int32
*/
int
acl_token(parse_context_t *ctx)
{
ctx->adr.adr_now += 3 * sizeof (uint32_t);
return (0);
}
/*
* Format of ace token:
* ace token id adr_char
* id adr_u_int32
* access_mask adr_u_int32
* flags adr_u_short
* type adr_u_short
*/
int
ace_token(parse_context_t *ctx)
{
ctx->adr.adr_now += 2 * sizeof (uint32_t) + 2 * sizeof (ushort_t);
return (0);
}
/*
* Format of attribute token: (old pre SunOS 5.7 format)
* attribute token id adr_char
* mode adr_int32 (printed in octal)
* uid adr_int32
* gid adr_int32
* file system id adr_int32
* node id adr_int32
* device adr_int32
*
*/
int
attribute_token(parse_context_t *ctx)
{
ctx->adr.adr_now += 6 * sizeof (int32_t);
return (0);
}
/*
* Format of attribute32 token:
* attribute token id adr_char
* mode adr_int32 (printed in octal)
* uid adr_int32
* gid adr_int32
* file system id adr_int32
* node id adr_int64
* device adr_int32
*
*/
int
attribute32_token(parse_context_t *ctx)
{
ctx->adr.adr_now += (5 * sizeof (int32_t)) + sizeof (int64_t);
return (0);
}
/*
* Format of attribute64 token:
* attribute token id adr_char
* mode adr_int32 (printed in octal)
* uid adr_int32
* gid adr_int32
* file system id adr_int32
* node id adr_int64
* device adr_int64
*
*/
int
attribute64_token(parse_context_t *ctx)
{
ctx->adr.adr_now += (4 * sizeof (int32_t)) + (2 * sizeof (int64_t));
return (0);
}
/*
* Format of command token:
* attribute token id adr_char
* argc adr_short
* argv len adr_short variable amount of argv len
* argv text argv len and text
* .
* .
* .
* envp count adr_short variable amount of envp len
* envp len adr_short and text
* envp text envp len
* .
* .
* .
*
*/
int
cmd_token(parse_context_t *ctx)
{
short cnt;
short i;
adrm_short(&(ctx->adr), &cnt, 1);
for (i = 0; i < cnt; i++)
skip_bytes(ctx);
adrm_short(&(ctx->adr), &cnt, 1);
for (i = 0; i < cnt; i++)
skip_bytes(ctx);
return (0);
}
/*
* Format of exit token:
* attribute token id adr_char
* return value adr_int32
* errno adr_int32
*
*/
int
exit_token(parse_context_t *ctx)
{
int32_t retval;
adrm_int32(&(ctx->adr), &retval, 1);
ctx->adr.adr_now += sizeof (int32_t);
ctx->out.sf_pass = (retval == 0) ? 1 : -1;
return (0);
}
/*
* Format of exec_args token:
* attribute token id adr_char
* count value adr_int32
* strings null terminated strings
*
*/
int
exec_args_token(parse_context_t *ctx)
{
int count, i;
adrm_int32(&(ctx->adr), (int32_t *)&count, 1);
for (i = 1; i <= count; i++) {
skip_string(ctx);
}
return (0);
}
/*
* Format of exec_env token:
* attribute token id adr_char
* count value adr_int32
* strings null terminated strings
*
*/
int
exec_env_token(parse_context_t *ctx)
{
int count, i;
adrm_int32(&(ctx->adr), (int32_t *)&count, 1);
for (i = 1; i <= count; i++)
skip_string(ctx);
return (0);
}
/*
* Format of liaison token:
*/
int
liaison_token(parse_context_t *ctx)
{
ctx->adr.adr_now += sizeof (int32_t);
return (0);
}
/*
* Format of path token:
* path adr_string
*/
int
path_token(parse_context_t *ctx)
{
get_bytes_to_string(ctx, &(ctx->out.sf_pathlen), &(ctx->out.sf_path),
0);
if (ctx->out.sf_path == NULL)
return (-1);
/*
* anchor the path because collapse_path needs it
*/
if (*(ctx->out.sf_path) != '/') {
anchor_path(ctx->out.sf_path);
ctx->out.sf_pathlen++;
}
ctx->out.sf_pathlen = collapse_path(ctx->out.sf_path,
ctx->out.sf_pathlen);
return (0);
}
/*
* path attr token / AUT_XATPATH
*
* Format of path attr token:
* token id adr_char
* string count adr_int32
* strings adr_string
*
* the sequence of strings is converted to a single string with
* a blank separator replacing the EOS for all but the last
* string.
*/
int
path_attr_token(parse_context_t *ctx)
{
int count, i;
int last_len;
size_t offset;
char *p;
adrm_int32(&(ctx->adr), &count, 1);
offset = ctx->out.sf_atpathlen;
p = ctx->adr.adr_now;
for (i = 0; i <= count; i++) {
last_len = strlen(p);
ctx->out.sf_atpathlen += last_len + 1;
p += last_len + 1;
}
ctx->out.sf_atpath = realloc(ctx->out.sf_atpath, ctx->out.sf_atpathlen);
ctx->out.sf_atpath += offset;
p = ctx->out.sf_atpath; /* save for fix up, below */
(void) memcpy(ctx->out.sf_atpath, ctx->adr.adr_now,
ctx->out.sf_atpathlen - offset);
ctx->out.sf_atpathlen--;
/* fix up: replace each eos except the last with ' ' */
for (i = 0; i < count; i++) {
while (*p++ != '\0')
;
*(p - 1) = ' ';
}
return (0);
}
/*
* Format of System V IPC permission token:
* System V IPC permission token id adr_char
* uid adr_int32
* gid adr_int32
* cuid adr_int32
* cgid adr_int32
* mode adr_int32
* seq adr_int32
* key adr_int32
*/
int
s5_IPC_perm_token(parse_context_t *ctx)
{
ctx->adr.adr_now += (7 * sizeof (int32_t));
return (0);
}
static void
common_process(parse_context_t *ctx)
{
int32_t ruid, rgid, egid, pid;
uint32_t asid;
adrm_u_int32(&(ctx->adr), (uint32_t *)&(ctx->out.sf_pauid), 1);
adrm_u_int32(&(ctx->adr), (uint32_t *)&(ctx->out.sf_peuid), 1);
adrm_int32(&(ctx->adr), &egid, 1);
adrm_int32(&(ctx->adr), &ruid, 1);
adrm_int32(&(ctx->adr), &rgid, 1);
adrm_int32(&(ctx->adr), &pid, 1);
adrm_u_int32(&(ctx->adr), &asid, 1);
}
/*
* Format of process32 token:
* process token id adr_char
* auid adr_int32
* euid adr_int32
* egid adr_int32
* ruid adr_int32
* rgid adr_int32
* pid adr_int32
* sid adr_int32
* termid adr_int32*2
*
*/
int
process32_token(parse_context_t *ctx)
{
int32_t port, machine;
common_process(ctx);
adrm_int32(&(ctx->adr), &port, 1);
adrm_int32(&(ctx->adr), &machine, 1);
return (0);
}
/*
* Format of process32_ex token:
* process token id adr_char
* auid adr_int32
* euid adr_int32
* egid adr_int32
* ruid adr_int32
* rgid adr_int32
* pid adr_int32
* sid adr_int32
* termid
* port adr_int32
* type adr_int32
* ip address adr_u_char*type
*
*/
int
process32_ex_token(parse_context_t *ctx)
{
int32_t port, type;
uchar_t addr[16];
common_process(ctx);
adrm_int32(&(ctx->adr), &port, 1);
adrm_int32(&(ctx->adr), &type, 1);
adrm_u_char(&(ctx->adr), addr, type);
return (0);
}
/*
* Format of process64 token:
* process token id adr_char
* auid adr_int32
* euid adr_int32
* egid adr_int32
* ruid adr_int32
* rgid adr_int32
* pid adr_int32
* sid adr_int32
* termid adr_int64+adr_int32
*
*/
int
process64_token(parse_context_t *ctx)
{
int64_t port;
int32_t machine;
common_process(ctx);
adrm_int64(&(ctx->adr), &port, 1);
adrm_int32(&(ctx->adr), &machine, 1);
return (0);
}
/*
* Format of process64_ex token:
* process token id adr_char
* auid adr_int32
* euid adr_int32
* egid adr_int32
* ruid adr_int32
* rgid adr_int32
* pid adr_int32
* sid adr_int32
* termid
* port adr_int64
* type adr_int32
* ip address adr_u_char*type
*
*/
int
process64_ex_token(parse_context_t *ctx)
{
int64_t port;
int32_t type;
uchar_t addr[16];
common_process(ctx);
adrm_int64(&(ctx->adr), &port, 1);
adrm_int32(&(ctx->adr), &type, 1);
adrm_u_char(&(ctx->adr), addr, type);
return (0);
}
/*
* Format of System V IPC token:
* System V IPC token id adr_char
* System V IPC type adr_char
* object id adr_int32
*
*/
int
s5_IPC_token(parse_context_t *ctx)
{
ctx->adr.adr_now += sizeof (char);
ctx->adr.adr_now += sizeof (int32_t);
return (0);
}
/*
* Format of socket token:
* socket_type adrm_short
* remote_port adrm_short
* remote_inaddr adrm_int32
*
*/
int
socket_token(parse_context_t *ctx)
{
ctx->adr.adr_now += (2 * sizeof (short)) + sizeof (int32_t);
return (0);
}
/*
* Format of socket_ex token:
* socket_domain adrm_short
* socket_type adrm_short
* address_type adrm_short
* local_port adrm_short
* local_inaddr adrm_u_char*address_type
* remote_port adrm_short
* remote_inaddr adrm_u_char*address_type
*
*/
int
socket_ex_token(parse_context_t *ctx)
{
short ip_size;
ctx->adr.adr_now += (2 * sizeof (short));
adrm_short(&(ctx->adr), &ip_size, 1);
ctx->adr.adr_now += sizeof (short) + (ip_size * sizeof (char)) +
sizeof (short) + (ip_size * sizeof (char));
return (0);
}
static void
common_subject(parse_context_t *ctx)
{
int32_t ruid, rgid, pid;
adrm_u_int32(&(ctx->adr), (uint32_t *)&(ctx->out.sf_auid), 1);
adrm_u_int32(&(ctx->adr), (uint32_t *)&(ctx->out.sf_euid), 1);
adrm_u_int32(&(ctx->adr), (uint32_t *)&(ctx->out.sf_egid), 1);
adrm_int32(&(ctx->adr), &ruid, 1);
adrm_int32(&(ctx->adr), &rgid, 1);
adrm_int32(&(ctx->adr), &pid, 1);
adrm_u_int32(&(ctx->adr), (uint32_t *)&(ctx->out.sf_asid), 1);
}
/*
* Format of subject32 token:
* subject token id adr_char
* auid adr_int32
* euid adr_int32
* egid adr_int32
* ruid adr_int32
* rgid adr_int32
* pid adr_int32
* sid adr_int32
* termid adr_int32*2
*
*/
int
subject32_token(parse_context_t *ctx)
{
int32_t port; /* not used in output */
common_subject(ctx);
adrm_int32(&(ctx->adr), &port, 1);
ctx->out.sf_tid.at_type = AU_IPv4;
adrm_u_char(&(ctx->adr), (uchar_t *)&(ctx->out.sf_tid.at_addr[0]), 4);
return (0);
}
/*
* Format of subject32_ex token:
* subject token id adr_char
* auid adr_int32
* euid adr_int32
* egid adr_int32
* ruid adr_int32
* rgid adr_int32
* pid adr_int32
* sid adr_int32
* termid
* port adr_int32
* type adr_int32
* ip address adr_u_char*type
*
*/
int
subject32_ex_token(parse_context_t *ctx)
{
int32_t port; /* not used in output */
common_subject(ctx);
adrm_int32(&(ctx->adr), &port, 1);
adrm_u_int32(&(ctx->adr), &(ctx->out.sf_tid.at_type), 1);
adrm_u_char(&(ctx->adr), (uchar_t *)&(ctx->out.sf_tid.at_addr[0]),
ctx->out.sf_tid.at_type);
return (0);
}
/*
* Format of subject64 token:
* subject token id adr_char
* auid adr_int32
* euid adr_int32
* egid adr_int32
* ruid adr_int32
* rgid adr_int32
* pid adr_int32
* sid adr_int32
* termid adr_int64+adr_int32
*
*/
int
subject64_token(parse_context_t *ctx)
{
int64_t port;
common_subject(ctx);
adrm_int64(&(ctx->adr), &port, 1);
ctx->out.sf_tid.at_type = AU_IPv4;
adrm_u_char(&(ctx->adr), (uchar_t *)&(ctx->out.sf_tid.at_addr[0]), 4);
return (0);
}
/*
* Format of subject64_ex token:
* subject token id adr_char
* auid adr_int32
* euid adr_int32
* egid adr_int32
* ruid adr_int32
* rgid adr_int32
* pid adr_int32
* sid adr_int32
* termid
* port adr_int64
* type adr_int32
* ip address adr_u_char*type
*
*/
int
subject64_ex_token(parse_context_t *ctx)
{
int64_t port;
common_subject(ctx);
adrm_int64(&(ctx->adr), &port, 1);
adrm_u_int32(&(ctx->adr), &(ctx->out.sf_tid.at_type), 1);
adrm_u_char(&(ctx->adr), (uchar_t *)&(ctx->out.sf_tid.at_addr[0]),
ctx->out.sf_tid.at_type);
return (0);
}
int
xatom_token(parse_context_t *ctx)
{
skip_bytes(ctx);
return (0);
}
int
xselect_token(parse_context_t *ctx)
{
skip_bytes(ctx);
skip_bytes(ctx);
skip_bytes(ctx);
return (0);
}
/*
* anchor a path name with a slash
* assume we have enough space
*/
static void
anchor_path(char *path)
{
(void) memmove((void *)(path + 1), (void *)path, strlen(path) + 1);
*path = '/';
}
/*
* 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 '/'
*/
static size_t
collapse_path(char *s, size_t ls)
{
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 */
ls++; /* source length including '\0' */
slashseen = 0;
for (is = 0, id = 0; is < ls; is++) {
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] != '/')
;
id++;
continue;
}
/* XXX/../ seen */
if (s[is] == '.' && s[is+1] == '.' && s[is+2] == '/') {
is += 2;
if (id > 0)
id--;
while (id > 0 && s[--id] != '/')
;
id++;
continue;
}
while (is < ls && (s[id++] = s[is++]) != '/')
;
is--;
}
return ((size_t)id - 1);
}
/*
* for tokens with sub-fields that include a length, this
* skips the sub-field.
*/
static void
skip_bytes(parse_context_t *ctx)
{
ushort_t c;
adrm_u_short(&(ctx->adr), &c, 1);
ctx->adr.adr_now += c;
}
static void
skip_string(parse_context_t *ctx)
{
char c;
do {
adrm_char(&(ctx->adr), &c, 1);
} while (c != (char)0);
}
/*
* add a byte to specified length so there can be a prefix of
* '/' added (if needed for paths). Another is added for '\0'
*
* if offset is zero, new data overwrites old, if any. Otherwise
* new data is appended to the end.
*/
static void
get_bytes_to_string(parse_context_t *ctx, size_t *l, char **p,
size_t offset)
{
ushort_t len;
char *bp;
adrm_u_short(&(ctx->adr), &len, 1);
len++; /* in case need to add '/' prefix */
*p = realloc(*p, 1 + (size_t)len + offset);
if (*p == NULL) {
perror("audit_sysudp.so");
return;
}
if (offset > 0)
offset--; /* overwrite end of string */
*l = (size_t)len - 2 + offset;
bp = *p + offset;
adrm_char(&(ctx->adr), bp, len - 1);
*(bp + len - 1) = '\0';
}
/*
* Format of host token:
* host adr_uint32
*/
int
host_token(parse_context_t *ctx)
{
ctx->adr.adr_now += sizeof (int32_t);
return (0);
}
/*
* Format of useofauth token:
* uauth token id adr_char
* uauth adr_string
*
*/
int
useofauth_token(parse_context_t *ctx)
{
get_bytes_to_string(ctx, &(ctx->out.sf_uauthlen),
&(ctx->out.sf_uauth), 0);
return (0);
}
/*
* Format of user token:
* user token id adr_char
* uid adr_uid
* username adr_string
*
*/
int
user_token(parse_context_t *ctx)
{
ctx->adr.adr_now += sizeof (uid_t);
skip_bytes(ctx);
return (0);
}
/*
* Format of zonename token:
* zonename token id adr_char
* zonename adr_string
*
*/
int
zonename_token(parse_context_t *ctx)
{
get_bytes_to_string(ctx,
&(ctx->out.sf_zonelen),
&(ctx->out.sf_zonename),
0);
return (0);
}
/*
* Format of fmri token:
* fmri token id adr_char
* fmri adr_string
*/
int
fmri_token(parse_context_t *ctx)
{
skip_bytes(ctx);
return (0);
}
int
xcolormap_token(parse_context_t *ctx)
{
return (xgeneric(ctx));
}
int
xcursor_token(parse_context_t *ctx)
{
return (xgeneric(ctx));
}
int
xfont_token(parse_context_t *ctx)
{
return (xgeneric(ctx));
}
int
xgc_token(parse_context_t *ctx)
{
return (xgeneric(ctx));
}
int
xpixmap_token(parse_context_t *ctx)
{
return (xgeneric(ctx));
}
int
xwindow_token(parse_context_t *ctx)
{
return (xgeneric(ctx));
}
/*
* Format of xgeneric token:
* XID adr_int32
* creator UID adr_int32
*
* Includes: xcolormap, xcursor, xfont, xgc, xpixmap, and xwindow
*/
static int
xgeneric(parse_context_t *ctx)
{
ctx->adr.adr_now += 2 * sizeof (int32_t);
return (0);
}
/*
* Format of xproperty token:
* XID adr_int32
* creator UID adr_int32
* atom string adr_string
*/
int
xproperty_token(parse_context_t *ctx)
{
ctx->adr.adr_now += 2 * sizeof (int32_t);
return (0);
}
/*
* Format of xclient token:
* xclient id adr_int32
*/
int
xclient_token(parse_context_t *ctx)
{
ctx->adr.adr_now += sizeof (int32_t);
return (0);
}
/*
* -----------------------------------------------------------------------
* privilege_token() : Process privilege token and display contents
*
* Format of privilege token:
* privilege token id adr_char
* privilege type adr_string
* privilege adr_string
* -----------------------------------------------------------------------
*/
int
privilege_token(parse_context_t *ctx)
{
skip_bytes(ctx);
skip_bytes(ctx);
return (0);
}
/*
* -----------------------------------------------------------------------
* secflags_token() : Process secflags token and display contents
*
* Format of privilege token:
* secflags token id adr_char
* secflag set name adr_string
* secflags adr_string
* -----------------------------------------------------------------------
*/
int
secflags_token(parse_context_t *ctx)
{
skip_bytes(ctx);
skip_bytes(ctx);
return (0);
}
/*
* Format of label token:
* label ID 1 byte
* compartment length 1 byte
* classification 2 bytes
* compartment words <compartment length> * 4 bytes
*/
int
label_token(parse_context_t *ctx)
{
char c;
ctx->adr.adr_now += sizeof (char); /* label ID */
adrm_char(&(ctx->adr), &c, 1);
ctx->adr.adr_now += sizeof (ushort_t); /* classification */
ctx->adr.adr_now += 4 * c; /* compartments */
return (0);
}
/*
* Format of useofpriv token:
* priv_type adr_char
* priv_set_t adr_short
* priv_set adr_char*(sizeof (priv_set_t))
*/
int
useofpriv_token(parse_context_t *ctx)
{
ctx->adr.adr_now += sizeof (char); /* success / fail */
skip_bytes(ctx);
return (0);
}
|