8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / lib / auditd_plugins / syslog / systoken.c
blobce0c2671762c37b00e584677eb0e51f0fa73581d
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
22 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
24 * Copyright 2012 Milan Jurik. All rights reserved.
29 * Token processing for sysupd; each token function does one
30 * or more operations. All of them bump the buffer pointer
31 * to the next token; some of them extract one or more data
32 * from the token.
35 #define DEBUG 0
36 #if DEBUG
37 #define DPRINT(x) { (void) fprintf x; }
38 #else
39 #define DPRINT(x)
40 #endif
42 #include <locale.h>
43 #include <stdlib.h>
44 #include <stdio.h>
45 #include <string.h>
46 #include <sys/types.h>
47 #include <bsm/libbsm.h>
48 #include <sys/tsol/label.h>
49 #include "toktable.h" /* ../praudit */
50 #include "sysplugin.h"
51 #include "systoken.h"
52 #include <audit_plugin.h>
54 #if DEBUG
55 static FILE *dbfp; /* debug file */
56 #endif
58 static void anchor_path(char *);
59 static size_t collapse_path(char *, size_t);
60 static void get_bytes_to_string(parse_context_t *, size_t *, char **,
61 size_t);
62 static void skip_bytes(parse_context_t *);
63 static void skip_string(parse_context_t *);
64 static int xgeneric(parse_context_t *);
67 * Process a token in a record to (1) extract data of interest if any
68 * and (2) point to the next token.
70 * returns 0 if ok. + or - values are of debug value:
72 * returns -1 if the parsing of the token failed.
74 * returns +<previous id> if the token is not found. This value
75 * is used to help determine where in the record the problem
76 * occurred. The common failure case is that the parsing of
77 * token M is incorrect and the buffer pointer ends up pointing
78 * to garbage. The positive error value of M *may* be the id of
79 * the incorrectly parsed token.
82 int
83 parse_token(parse_context_t *ctx)
85 char tokenid;
86 static char prev_tokenid = -1;
87 int rc;
89 #if DEBUG
90 static boolean_t first = 1;
92 if (first) {
93 dbfp = __auditd_debug_file_open();
94 first = 0;
96 #endif
98 adrm_char(&(ctx->adr), &tokenid, 1);
100 if ((tokenid > 0) && (tokentable[tokenid].func != NOFUNC)) {
101 rc = (*tokentable[tokenid].func)(ctx);
102 prev_tokenid = tokenid;
103 return (rc);
105 /* here if token id is not in table */
106 return (prev_tokenid);
109 /* There should not be any file tokens in the middle of a record */
111 /* ARGSUSED */
113 file_token(parse_context_t *ctx)
116 return (-1);
119 /* ARGSUSED */
121 file64_token(parse_context_t *ctx)
123 return (-1);
126 static void
127 common_header(parse_context_t *ctx)
129 adrm_u_int32(&(ctx->adr), &(ctx->out.sf_reclen), 1);
130 ctx->adr.adr_now += sizeof (char); /* version number */
131 adrm_u_short(&(ctx->adr), &(ctx->out.sf_eventid), 1);
132 ctx->adr.adr_now += sizeof (short); /* modifier */
136 * 32bit header
139 header_token(parse_context_t *ctx)
141 common_header(ctx);
142 ctx->adr.adr_now += 2 * sizeof (int32_t); /* time */
144 return (0);
149 header32_ex_token(parse_context_t *ctx)
151 int32_t type;
153 common_header(ctx);
155 adrm_int32(&(ctx->adr), &type, 1); /* tid type */
156 ctx->adr.adr_now += type * sizeof (char); /* ip address */
158 ctx->adr.adr_now += 2 * sizeof (int32_t); /* time */
160 return (0);
165 header64_ex_token(parse_context_t *ctx)
167 int32_t type;
169 common_header(ctx);
171 adrm_int32(&(ctx->adr), &type, 1); /* tid type */
172 ctx->adr.adr_now += type * sizeof (char); /* ip address */
174 ctx->adr.adr_now += 2 * sizeof (int64_t); /* time */
176 return (0);
181 header64_token(parse_context_t *ctx)
183 common_header(ctx);
185 ctx->adr.adr_now += 2 * sizeof (int64_t); /* time */
187 return (0);
192 * ======================================================
193 * The following token processing routines return
194 * 0: if parsed ok
195 * -1: can't parse and can't determine location of next token
196 * ======================================================
200 trailer_token(parse_context_t *ctx)
202 short magic_number;
203 uint32_t bytes;
205 adrm_u_short(&(ctx->adr), (ushort_t *)&magic_number, 1);
206 if (magic_number != AUT_TRAILER_MAGIC)
207 return (-1);
209 adrm_u_int32(&(ctx->adr), &bytes, 1);
211 return (0);
216 * Format of arbitrary data token:
217 * arbitrary data token id &(ctx->adr) char
218 * how to print adr_char
219 * basic unit adr_char
220 * unit count adr_char, specifying number of units of
221 * data items depends on basic unit
225 arbitrary_data_token(parse_context_t *ctx)
227 char basic_unit, unit_count;
229 ctx->adr.adr_now += sizeof (char); /* how to print */
231 adrm_char(&(ctx->adr), &basic_unit, 1);
232 adrm_char(&(ctx->adr), &unit_count, 1);
234 switch (basic_unit) {
235 case AUR_CHAR: /* same as AUR_BYTE */
236 ctx->adr.adr_now += unit_count * sizeof (char);
237 break;
238 case AUR_SHORT:
239 ctx->adr.adr_now += unit_count * sizeof (short);
240 break;
241 case AUR_INT32: /* same as AUR_INT */
242 ctx->adr.adr_now += unit_count * sizeof (int32_t);
243 break;
244 case AUR_INT64:
245 ctx->adr.adr_now += unit_count * sizeof (int64_t);
246 break;
247 default:
248 return (-1);
250 return (0);
255 * Format of opaque token:
256 * opaque token id adr_char
257 * size adr_short
258 * data adr_char, size times
262 opaque_token(parse_context_t *ctx)
264 skip_bytes(ctx);
265 return (0);
270 * Format of return32 value token:
271 * return value token id adr_char
272 * error number adr_char
273 * return value adr_u_int32
277 return_value32_token(parse_context_t *ctx)
279 char errnum;
281 adrm_char(&(ctx->adr), &errnum, 1); /* pass / fail */
282 ctx->adr.adr_now += sizeof (int32_t); /* error code */
284 ctx->out.sf_pass = (errnum == 0) ? 1 : -1;
286 return (0);
290 * Format of return64 value token:
291 * return value token id adr_char
292 * error number adr_char
293 * return value adr_u_int64
297 return_value64_token(parse_context_t *ctx)
299 char errnum;
301 adrm_char(&(ctx->adr), &errnum, 1); /* pass / fail */
302 ctx->adr.adr_now += sizeof (int64_t); /* error code */
304 ctx->out.sf_pass = (errnum == 0) ? 1 : -1;
306 return (0);
311 * Format of sequence token:
312 * sequence token id adr_char
313 * audit_count int32_t
317 sequence_token(parse_context_t *ctx)
319 adrm_int32(&(ctx->adr), &(ctx->out.sf_sequence), 1);
320 return (0);
325 * Format of text token:
326 * text token id adr_char
327 * text adr_string
330 text_token(parse_context_t *ctx)
332 ushort_t len;
333 size_t separator_sz = 0;
334 char *bp; /* pointer to output string */
336 adrm_u_short(&(ctx->adr), &len, 1);
338 if (ctx->out.sf_textlen > 0)
339 separator_sz = sizeof (AU_TEXT_NAME) - 1;
341 DPRINT((dbfp, "text_token: start length=%d, add length=%d+%d\n",
342 ctx->out.sf_textlen, (size_t)len, separator_sz));
344 ctx->out.sf_text = realloc(ctx->out.sf_text,
345 ctx->out.sf_textlen + (size_t)len + separator_sz);
347 if (ctx->out.sf_text == NULL)
348 return (-1);
350 bp = ctx->out.sf_text;
352 if (ctx->out.sf_textlen != 0) { /* concatenation? */
353 bp += ctx->out.sf_textlen;
354 bp += strlcpy(bp, AU_TEXT_NAME, separator_sz + 1);
355 ctx->out.sf_textlen += separator_sz;
356 DPRINT((dbfp, "text_token: l is %d\n%s\n", ctx->out.sf_textlen,
357 ctx->out.sf_text));
359 adrm_char(&(ctx->adr), bp, len);
360 len--; /* includes EOS */
361 *(bp + len) = '\0';
363 ctx->out.sf_textlen += len;
364 DPRINT((dbfp, "text_token: l=%d\n%s\n", ctx->out.sf_textlen,
365 ctx->out.sf_text));
367 return (0);
371 * Format of tid token:
372 * ip token id adr_char
373 * terminal type adr_char
374 * terminal type = AU_IPADR:
375 * remote port: ushort
376 * local port: ushort
377 * IP type: int32 -- AU_IPv4 or AU_IPv6
378 * address: int32 if IPv4, else 4 * int32
381 tid_token(parse_context_t *ctx)
383 uchar_t type;
384 int32_t ip_length;
386 adrm_char(&(ctx->adr), (char *)&type, 1);
388 switch (type) {
389 default:
390 return (-1); /* other than IP type is not implemented */
391 case AU_IPADR:
392 ctx->adr.adr_now += 2 * sizeof (ushort_t);
393 adrm_int32(&(ctx->adr), &ip_length, 1);
394 ctx->adr.adr_now += ip_length;
395 break;
397 return (0);
401 * Format of ip_addr token:
402 * ip token id adr_char
403 * address adr_int32
407 ip_addr_token(parse_context_t *ctx)
409 ctx->adr.adr_now += sizeof (int32_t);
411 return (0);
415 * Format of ip_addr_ex token:
416 * ip token id adr_char
417 * ip type adr_int32
418 * ip address adr_u_char*type
422 ip_addr_ex_token(parse_context_t *ctx)
424 int32_t type;
426 adrm_int32(&(ctx->adr), &type, 1); /* ip type */
427 ctx->adr.adr_now += type * sizeof (uchar_t); /* ip address */
429 return (0);
433 * Format of ip token:
434 * ip header token id adr_char
435 * version adr_char
436 * type of service adr_char
437 * length adr_short
438 * id adr_u_short
439 * offset adr_u_short
440 * ttl adr_char
441 * protocol adr_char
442 * checksum adr_u_short
443 * source address adr_int32
444 * destination address adr_int32
448 ip_token(parse_context_t *ctx)
450 ctx->adr.adr_now += (2 * sizeof (char)) + (3 * sizeof (short)) +
451 (2 * sizeof (char)) + sizeof (short) + (2 * sizeof (int32_t));
452 return (0);
457 * Format of iport token:
458 * ip port address token id adr_char
459 * port address adr_short
463 iport_token(parse_context_t *ctx)
465 ctx->adr.adr_now += sizeof (short);
467 return (0);
472 * Format of groups token:
473 * group token id adr_char
474 * group list adr_int32, 16 times
478 group_token(parse_context_t *ctx)
480 ctx->adr.adr_now += 16 * sizeof (int32_t);
482 return (0);
486 * Format of newgroups token:
487 * group token id adr_char
488 * number of groups adr_short
489 * group list adr_int32, "number" times
493 newgroup_token(parse_context_t *ctx)
495 short int number;
497 adrm_short(&(ctx->adr), &number, 1);
499 ctx->adr.adr_now += number * sizeof (int32_t);
501 return (0);
505 * Format of argument32 token:
506 * argument token id adr_char
507 * argument number adr_char
508 * argument value adr_int32
509 * argument description adr_string
513 argument32_token(parse_context_t *ctx)
515 ctx->adr.adr_now += sizeof (char) + sizeof (int32_t);
516 skip_bytes(ctx);
518 return (0);
522 * Format of argument64 token:
523 * argument token id adr_char
524 * argument number adr_char
525 * argument value adr_int64
526 * argument description adr_string
530 argument64_token(parse_context_t *ctx)
532 ctx->adr.adr_now += sizeof (char) + sizeof (int64_t);
533 skip_bytes(ctx);
535 return (0);
539 * Format of acl token:
540 * acl token id adr_char
541 * type adr_u_int32
542 * value adr_u_int32
543 * mode adr_u_int32
546 acl_token(parse_context_t *ctx)
548 ctx->adr.adr_now += 3 * sizeof (uint32_t);
550 return (0);
554 * Format of ace token:
555 * ace token id adr_char
556 * id adr_u_int32
557 * access_mask adr_u_int32
558 * flags adr_u_short
559 * type adr_u_short
562 ace_token(parse_context_t *ctx)
564 ctx->adr.adr_now += 2 * sizeof (uint32_t) + 2 * sizeof (ushort_t);
566 return (0);
570 * Format of attribute token: (old pre SunOS 5.7 format)
571 * attribute token id adr_char
572 * mode adr_int32 (printed in octal)
573 * uid adr_int32
574 * gid adr_int32
575 * file system id adr_int32
576 * node id adr_int32
577 * device adr_int32
581 attribute_token(parse_context_t *ctx)
583 ctx->adr.adr_now += 6 * sizeof (int32_t);
585 return (0);
589 * Format of attribute32 token:
590 * attribute token id adr_char
591 * mode adr_int32 (printed in octal)
592 * uid adr_int32
593 * gid adr_int32
594 * file system id adr_int32
595 * node id adr_int64
596 * device adr_int32
600 attribute32_token(parse_context_t *ctx)
602 ctx->adr.adr_now += (5 * sizeof (int32_t)) + sizeof (int64_t);
604 return (0);
608 * Format of attribute64 token:
609 * attribute token id adr_char
610 * mode adr_int32 (printed in octal)
611 * uid adr_int32
612 * gid adr_int32
613 * file system id adr_int32
614 * node id adr_int64
615 * device adr_int64
619 attribute64_token(parse_context_t *ctx)
621 ctx->adr.adr_now += (4 * sizeof (int32_t)) + (2 * sizeof (int64_t));
623 return (0);
628 * Format of command token:
629 * attribute token id adr_char
630 * argc adr_short
631 * argv len adr_short variable amount of argv len
632 * argv text argv len and text
636 * envp count adr_short variable amount of envp len
637 * envp len adr_short and text
638 * envp text envp len
645 cmd_token(parse_context_t *ctx)
647 short cnt;
648 short i;
650 adrm_short(&(ctx->adr), &cnt, 1);
652 for (i = 0; i < cnt; i++)
653 skip_bytes(ctx);
655 adrm_short(&(ctx->adr), &cnt, 1);
657 for (i = 0; i < cnt; i++)
658 skip_bytes(ctx);
660 return (0);
665 * Format of exit token:
666 * attribute token id adr_char
667 * return value adr_int32
668 * errno adr_int32
672 exit_token(parse_context_t *ctx)
674 int32_t retval;
676 adrm_int32(&(ctx->adr), &retval, 1);
677 ctx->adr.adr_now += sizeof (int32_t);
679 ctx->out.sf_pass = (retval == 0) ? 1 : -1;
680 return (0);
684 * Format of exec_args token:
685 * attribute token id adr_char
686 * count value adr_int32
687 * strings null terminated strings
691 exec_args_token(parse_context_t *ctx)
693 int count, i;
695 adrm_int32(&(ctx->adr), (int32_t *)&count, 1);
696 for (i = 1; i <= count; i++) {
697 skip_string(ctx);
700 return (0);
704 * Format of exec_env token:
705 * attribute token id adr_char
706 * count value adr_int32
707 * strings null terminated strings
711 exec_env_token(parse_context_t *ctx)
713 int count, i;
715 adrm_int32(&(ctx->adr), (int32_t *)&count, 1);
716 for (i = 1; i <= count; i++)
717 skip_string(ctx);
719 return (0);
723 * Format of liaison token:
726 liaison_token(parse_context_t *ctx)
728 ctx->adr.adr_now += sizeof (int32_t);
730 return (0);
735 * Format of path token:
736 * path adr_string
739 path_token(parse_context_t *ctx)
741 get_bytes_to_string(ctx, &(ctx->out.sf_pathlen), &(ctx->out.sf_path),
743 if (ctx->out.sf_path == NULL)
744 return (-1);
746 * anchor the path because collapse_path needs it
748 if (*(ctx->out.sf_path) != '/') {
749 anchor_path(ctx->out.sf_path);
750 ctx->out.sf_pathlen++;
752 ctx->out.sf_pathlen = collapse_path(ctx->out.sf_path,
753 ctx->out.sf_pathlen);
755 return (0);
759 * path attr token / AUT_XATPATH
761 * Format of path attr token:
762 * token id adr_char
763 * string count adr_int32
764 * strings adr_string
766 * the sequence of strings is converted to a single string with
767 * a blank separator replacing the EOS for all but the last
768 * string.
771 path_attr_token(parse_context_t *ctx)
773 int count, i;
774 int last_len;
775 size_t offset;
776 char *p;
778 adrm_int32(&(ctx->adr), &count, 1);
780 offset = ctx->out.sf_atpathlen;
781 p = ctx->adr.adr_now;
782 for (i = 0; i <= count; i++) {
783 last_len = strlen(p);
784 ctx->out.sf_atpathlen += last_len + 1;
785 p += last_len + 1;
787 ctx->out.sf_atpath = realloc(ctx->out.sf_atpath, ctx->out.sf_atpathlen);
788 ctx->out.sf_atpath += offset;
789 p = ctx->out.sf_atpath; /* save for fix up, below */
790 (void) memcpy(ctx->out.sf_atpath, ctx->adr.adr_now,
791 ctx->out.sf_atpathlen - offset);
792 ctx->out.sf_atpathlen--;
794 /* fix up: replace each eos except the last with ' ' */
796 for (i = 0; i < count; i++) {
797 while (*p++ != '\0')
799 *(p - 1) = ' ';
801 return (0);
806 * Format of System V IPC permission token:
807 * System V IPC permission token id adr_char
808 * uid adr_int32
809 * gid adr_int32
810 * cuid adr_int32
811 * cgid adr_int32
812 * mode adr_int32
813 * seq adr_int32
814 * key adr_int32
817 s5_IPC_perm_token(parse_context_t *ctx)
819 ctx->adr.adr_now += (7 * sizeof (int32_t));
820 return (0);
823 static void
824 common_process(parse_context_t *ctx)
826 int32_t ruid, rgid, egid, pid;
827 uint32_t asid;
829 adrm_u_int32(&(ctx->adr), (uint32_t *)&(ctx->out.sf_pauid), 1);
830 adrm_u_int32(&(ctx->adr), (uint32_t *)&(ctx->out.sf_peuid), 1);
831 adrm_int32(&(ctx->adr), &egid, 1);
832 adrm_int32(&(ctx->adr), &ruid, 1);
833 adrm_int32(&(ctx->adr), &rgid, 1);
834 adrm_int32(&(ctx->adr), &pid, 1);
835 adrm_u_int32(&(ctx->adr), &asid, 1);
839 * Format of process32 token:
840 * process token id adr_char
841 * auid adr_int32
842 * euid adr_int32
843 * egid adr_int32
844 * ruid adr_int32
845 * rgid adr_int32
846 * pid adr_int32
847 * sid adr_int32
848 * termid adr_int32*2
852 process32_token(parse_context_t *ctx)
854 int32_t port, machine;
856 common_process(ctx);
858 adrm_int32(&(ctx->adr), &port, 1);
859 adrm_int32(&(ctx->adr), &machine, 1);
861 return (0);
865 * Format of process32_ex token:
866 * process token id adr_char
867 * auid adr_int32
868 * euid adr_int32
869 * egid adr_int32
870 * ruid adr_int32
871 * rgid adr_int32
872 * pid adr_int32
873 * sid adr_int32
874 * termid
875 * port adr_int32
876 * type adr_int32
877 * ip address adr_u_char*type
881 process32_ex_token(parse_context_t *ctx)
883 int32_t port, type;
884 uchar_t addr[16];
886 common_process(ctx);
888 adrm_int32(&(ctx->adr), &port, 1);
889 adrm_int32(&(ctx->adr), &type, 1);
890 adrm_u_char(&(ctx->adr), addr, type);
892 return (0);
896 * Format of process64 token:
897 * process token id adr_char
898 * auid adr_int32
899 * euid adr_int32
900 * egid adr_int32
901 * ruid adr_int32
902 * rgid adr_int32
903 * pid adr_int32
904 * sid adr_int32
905 * termid adr_int64+adr_int32
909 process64_token(parse_context_t *ctx)
911 int64_t port;
912 int32_t machine;
914 common_process(ctx);
916 adrm_int64(&(ctx->adr), &port, 1);
917 adrm_int32(&(ctx->adr), &machine, 1);
919 return (0);
923 * Format of process64_ex token:
924 * process token id adr_char
925 * auid adr_int32
926 * euid adr_int32
927 * egid adr_int32
928 * ruid adr_int32
929 * rgid adr_int32
930 * pid adr_int32
931 * sid adr_int32
932 * termid
933 * port adr_int64
934 * type adr_int32
935 * ip address adr_u_char*type
939 process64_ex_token(parse_context_t *ctx)
941 int64_t port;
942 int32_t type;
943 uchar_t addr[16];
945 common_process(ctx);
947 adrm_int64(&(ctx->adr), &port, 1);
948 adrm_int32(&(ctx->adr), &type, 1);
949 adrm_u_char(&(ctx->adr), addr, type);
951 return (0);
955 * Format of System V IPC token:
956 * System V IPC token id adr_char
957 * System V IPC type adr_char
958 * object id adr_int32
962 s5_IPC_token(parse_context_t *ctx)
964 ctx->adr.adr_now += sizeof (char);
965 ctx->adr.adr_now += sizeof (int32_t);
967 return (0);
972 * Format of socket token:
973 * socket_type adrm_short
974 * remote_port adrm_short
975 * remote_inaddr adrm_int32
979 socket_token(parse_context_t *ctx)
981 ctx->adr.adr_now += (2 * sizeof (short)) + sizeof (int32_t);
983 return (0);
988 * Format of socket_ex token:
989 * socket_domain adrm_short
990 * socket_type adrm_short
991 * address_type adrm_short
992 * local_port adrm_short
993 * local_inaddr adrm_u_char*address_type
994 * remote_port adrm_short
995 * remote_inaddr adrm_u_char*address_type
999 socket_ex_token(parse_context_t *ctx)
1001 short ip_size;
1003 ctx->adr.adr_now += (2 * sizeof (short));
1004 adrm_short(&(ctx->adr), &ip_size, 1);
1006 ctx->adr.adr_now += sizeof (short) + (ip_size * sizeof (char)) +
1007 sizeof (short) + (ip_size * sizeof (char));
1008 return (0);
1012 static void
1013 common_subject(parse_context_t *ctx)
1015 int32_t ruid, rgid, pid;
1017 adrm_u_int32(&(ctx->adr), (uint32_t *)&(ctx->out.sf_auid), 1);
1018 adrm_u_int32(&(ctx->adr), (uint32_t *)&(ctx->out.sf_euid), 1);
1019 adrm_u_int32(&(ctx->adr), (uint32_t *)&(ctx->out.sf_egid), 1);
1020 adrm_int32(&(ctx->adr), &ruid, 1);
1021 adrm_int32(&(ctx->adr), &rgid, 1);
1022 adrm_int32(&(ctx->adr), &pid, 1);
1023 adrm_u_int32(&(ctx->adr), (uint32_t *)&(ctx->out.sf_asid), 1);
1027 * Format of subject32 token:
1028 * subject token id adr_char
1029 * auid adr_int32
1030 * euid adr_int32
1031 * egid adr_int32
1032 * ruid adr_int32
1033 * rgid adr_int32
1034 * pid adr_int32
1035 * sid adr_int32
1036 * termid adr_int32*2
1040 subject32_token(parse_context_t *ctx)
1042 int32_t port; /* not used in output */
1044 common_subject(ctx);
1046 adrm_int32(&(ctx->adr), &port, 1);
1047 ctx->out.sf_tid.at_type = AU_IPv4;
1048 adrm_u_char(&(ctx->adr), (uchar_t *)&(ctx->out.sf_tid.at_addr[0]), 4);
1050 return (0);
1054 * Format of subject32_ex token:
1055 * subject token id adr_char
1056 * auid adr_int32
1057 * euid adr_int32
1058 * egid adr_int32
1059 * ruid adr_int32
1060 * rgid adr_int32
1061 * pid adr_int32
1062 * sid adr_int32
1063 * termid
1064 * port adr_int32
1065 * type adr_int32
1066 * ip address adr_u_char*type
1070 subject32_ex_token(parse_context_t *ctx)
1072 int32_t port; /* not used in output */
1074 common_subject(ctx);
1076 adrm_int32(&(ctx->adr), &port, 1);
1077 adrm_u_int32(&(ctx->adr), &(ctx->out.sf_tid.at_type), 1);
1078 adrm_u_char(&(ctx->adr), (uchar_t *)&(ctx->out.sf_tid.at_addr[0]),
1079 ctx->out.sf_tid.at_type);
1081 return (0);
1085 * Format of subject64 token:
1086 * subject token id adr_char
1087 * auid adr_int32
1088 * euid adr_int32
1089 * egid adr_int32
1090 * ruid adr_int32
1091 * rgid adr_int32
1092 * pid adr_int32
1093 * sid adr_int32
1094 * termid adr_int64+adr_int32
1098 subject64_token(parse_context_t *ctx)
1100 int64_t port;
1102 common_subject(ctx);
1104 adrm_int64(&(ctx->adr), &port, 1);
1105 ctx->out.sf_tid.at_type = AU_IPv4;
1106 adrm_u_char(&(ctx->adr), (uchar_t *)&(ctx->out.sf_tid.at_addr[0]), 4);
1108 return (0);
1112 * Format of subject64_ex token:
1113 * subject token id adr_char
1114 * auid adr_int32
1115 * euid adr_int32
1116 * egid adr_int32
1117 * ruid adr_int32
1118 * rgid adr_int32
1119 * pid adr_int32
1120 * sid adr_int32
1121 * termid
1122 * port adr_int64
1123 * type adr_int32
1124 * ip address adr_u_char*type
1128 subject64_ex_token(parse_context_t *ctx)
1130 int64_t port;
1132 common_subject(ctx);
1134 adrm_int64(&(ctx->adr), &port, 1);
1135 adrm_u_int32(&(ctx->adr), &(ctx->out.sf_tid.at_type), 1);
1136 adrm_u_char(&(ctx->adr), (uchar_t *)&(ctx->out.sf_tid.at_addr[0]),
1137 ctx->out.sf_tid.at_type);
1139 return (0);
1144 xatom_token(parse_context_t *ctx)
1146 skip_bytes(ctx);
1148 return (0);
1153 xselect_token(parse_context_t *ctx)
1155 skip_bytes(ctx);
1156 skip_bytes(ctx);
1157 skip_bytes(ctx);
1159 return (0);
1163 * anchor a path name with a slash
1164 * assume we have enough space
1166 static void
1167 anchor_path(char *path)
1170 (void) memmove((void *)(path + 1), (void *)path, strlen(path) + 1);
1171 *path = '/';
1176 * copy path to collapsed path.
1177 * collapsed path does not contain:
1178 * successive slashes
1179 * instances of dot-slash
1180 * instances of dot-dot-slash
1181 * passed path must be anchored with a '/'
1183 static size_t
1184 collapse_path(char *s, size_t ls)
1186 int id; /* index of where we are in destination string */
1187 int is; /* index of where we are in source string */
1188 int slashseen; /* have we seen a slash */
1190 ls++; /* source length including '\0' */
1192 slashseen = 0;
1193 for (is = 0, id = 0; is < ls; is++) {
1194 if (s[is] == '\0') {
1195 if (id > 1 && s[id-1] == '/') {
1196 --id;
1198 s[id++] = '\0';
1199 break;
1201 /* previous character was a / */
1202 if (slashseen) {
1203 if (s[is] == '/')
1204 continue; /* another slash, ignore it */
1205 } else if (s[is] == '/') {
1206 /* we see a /, just copy it and try again */
1207 slashseen = 1;
1208 s[id++] = '/';
1209 continue;
1211 /* /./ seen */
1212 if (s[is] == '.' && s[is+1] == '/') {
1213 is += 1;
1214 continue;
1216 /* XXX/. seen */
1217 if (s[is] == '.' && s[is+1] == '\0') {
1218 if (id > 1)
1219 id--;
1220 continue;
1222 /* XXX/.. seen */
1223 if (s[is] == '.' && s[is+1] == '.' && s[is+2] == '\0') {
1224 is += 1;
1225 if (id > 0)
1226 id--;
1227 while (id > 0 && s[--id] != '/')
1229 id++;
1230 continue;
1232 /* XXX/../ seen */
1233 if (s[is] == '.' && s[is+1] == '.' && s[is+2] == '/') {
1234 is += 2;
1235 if (id > 0)
1236 id--;
1237 while (id > 0 && s[--id] != '/')
1239 id++;
1240 continue;
1242 while (is < ls && (s[id++] = s[is++]) != '/')
1244 is--;
1246 return ((size_t)id - 1);
1250 * for tokens with sub-fields that include a length, this
1251 * skips the sub-field.
1254 static void
1255 skip_bytes(parse_context_t *ctx)
1257 ushort_t c;
1259 adrm_u_short(&(ctx->adr), &c, 1);
1260 ctx->adr.adr_now += c;
1263 static void
1264 skip_string(parse_context_t *ctx)
1266 char c;
1268 do {
1269 adrm_char(&(ctx->adr), &c, 1);
1270 } while (c != (char)0);
1274 * add a byte to specified length so there can be a prefix of
1275 * '/' added (if needed for paths). Another is added for '\0'
1277 * if offset is zero, new data overwrites old, if any. Otherwise
1278 * new data is appended to the end.
1281 static void
1282 get_bytes_to_string(parse_context_t *ctx, size_t *l, char **p,
1283 size_t offset)
1285 ushort_t len;
1286 char *bp;
1288 adrm_u_short(&(ctx->adr), &len, 1);
1290 len++; /* in case need to add '/' prefix */
1291 *p = realloc(*p, 1 + (size_t)len + offset);
1292 if (*p == NULL) {
1293 perror("audit_sysudp.so");
1294 return;
1296 if (offset > 0)
1297 offset--; /* overwrite end of string */
1299 *l = (size_t)len - 2 + offset;
1301 bp = *p + offset;
1302 adrm_char(&(ctx->adr), bp, len - 1);
1303 *(bp + len - 1) = '\0';
1308 * Format of host token:
1309 * host adr_uint32
1312 host_token(parse_context_t *ctx)
1314 ctx->adr.adr_now += sizeof (int32_t);
1316 return (0);
1320 * Format of useofauth token:
1321 * uauth token id adr_char
1322 * uauth adr_string
1326 useofauth_token(parse_context_t *ctx)
1328 get_bytes_to_string(ctx, &(ctx->out.sf_uauthlen),
1329 &(ctx->out.sf_uauth), 0);
1331 return (0);
1335 * Format of user token:
1336 * user token id adr_char
1337 * uid adr_uid
1338 * username adr_string
1342 user_token(parse_context_t *ctx)
1344 ctx->adr.adr_now += sizeof (uid_t);
1345 skip_bytes(ctx);
1347 return (0);
1351 * Format of zonename token:
1352 * zonename token id adr_char
1353 * zonename adr_string
1357 zonename_token(parse_context_t *ctx)
1359 get_bytes_to_string(ctx,
1360 &(ctx->out.sf_zonelen),
1361 &(ctx->out.sf_zonename),
1364 return (0);
1368 * Format of fmri token:
1369 * fmri token id adr_char
1370 * fmri adr_string
1373 fmri_token(parse_context_t *ctx)
1375 skip_bytes(ctx);
1377 return (0);
1381 xcolormap_token(parse_context_t *ctx)
1383 return (xgeneric(ctx));
1387 xcursor_token(parse_context_t *ctx)
1389 return (xgeneric(ctx));
1393 xfont_token(parse_context_t *ctx)
1395 return (xgeneric(ctx));
1399 xgc_token(parse_context_t *ctx)
1401 return (xgeneric(ctx));
1405 xpixmap_token(parse_context_t *ctx)
1407 return (xgeneric(ctx));
1411 xwindow_token(parse_context_t *ctx)
1413 return (xgeneric(ctx));
1416 * Format of xgeneric token:
1417 * XID adr_int32
1418 * creator UID adr_int32
1420 * Includes: xcolormap, xcursor, xfont, xgc, xpixmap, and xwindow
1422 static int
1423 xgeneric(parse_context_t *ctx)
1425 ctx->adr.adr_now += 2 * sizeof (int32_t);
1427 return (0);
1430 * Format of xproperty token:
1431 * XID adr_int32
1432 * creator UID adr_int32
1433 * atom string adr_string
1436 xproperty_token(parse_context_t *ctx)
1438 ctx->adr.adr_now += 2 * sizeof (int32_t);
1440 return (0);
1443 * Format of xclient token:
1444 * xclient id adr_int32
1447 xclient_token(parse_context_t *ctx)
1449 ctx->adr.adr_now += sizeof (int32_t);
1451 return (0);
1455 * -----------------------------------------------------------------------
1456 * privilege_token() : Process privilege token and display contents
1458 * Format of privilege token:
1459 * privilege token id adr_char
1460 * privilege type adr_string
1461 * privilege adr_string
1462 * -----------------------------------------------------------------------
1466 privilege_token(parse_context_t *ctx)
1468 skip_bytes(ctx);
1469 skip_bytes(ctx);
1471 return (0);
1475 * -----------------------------------------------------------------------
1476 * secflags_token() : Process secflags token and display contents
1478 * Format of privilege token:
1479 * secflags token id adr_char
1480 * secflag set name adr_string
1481 * secflags adr_string
1482 * -----------------------------------------------------------------------
1485 secflags_token(parse_context_t *ctx)
1487 skip_bytes(ctx);
1488 skip_bytes(ctx);
1490 return (0);
1494 * Format of label token:
1495 * label ID 1 byte
1496 * compartment length 1 byte
1497 * classification 2 bytes
1498 * compartment words <compartment length> * 4 bytes
1501 label_token(parse_context_t *ctx)
1503 char c;
1505 ctx->adr.adr_now += sizeof (char); /* label ID */
1506 adrm_char(&(ctx->adr), &c, 1);
1508 ctx->adr.adr_now += sizeof (ushort_t); /* classification */
1509 ctx->adr.adr_now += 4 * c; /* compartments */
1511 return (0);
1515 * Format of useofpriv token:
1516 * priv_type adr_char
1517 * priv_set_t adr_short
1518 * priv_set adr_char*(sizeof (priv_set_t))
1521 useofpriv_token(parse_context_t *ctx)
1523 ctx->adr.adr_now += sizeof (char); /* success / fail */
1524 skip_bytes(ctx);
1526 return (0);