]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - bsm/libbsm.h
Vendor import of OpenBSM 1.1 alpha4, which incorporates the following
[FreeBSD/FreeBSD.git] / bsm / libbsm.h
1 /*-
2  * Copyright (c) 2004-2008 Apple Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1.  Redistributions of source code must retain the above copyright
9  *     notice, this list of conditions and the following disclaimer.
10  * 2.  Redistributions in binary form must reproduce the above copyright
11  *     notice, this list of conditions and the following disclaimer in the
12  *     documentation and/or other materials provided with the distribution.
13  * 3.  Neither the name of Apple Inc. ("Apple") nor the names of
14  *     its contributors may be used to endorse or promote products derived
15  *     from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR
21  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
25  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
26  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  *
29  * $P4: //depot/projects/trustedbsd/openbsm/bsm/libbsm.h#40 $
30  */
31
32 #ifndef _LIBBSM_H_
33 #define _LIBBSM_H_
34
35 /*
36  * NB: definitions, etc., marked with "OpenSSH compatibility" were introduced
37  * solely to allow OpenSSH to compile; Darwin/Apple code should not use them.
38  */
39
40 #include <sys/types.h>
41 #include <sys/cdefs.h>
42
43 #include <inttypes.h>           /* Required for audit.h. */
44 #include <time.h>               /* Required for clock_t on Linux. */
45
46 #include <bsm/audit.h>
47 #include <bsm/audit_record.h>
48
49 #include <stdio.h>
50
51 #ifdef __APPLE__
52 #include <mach/mach.h>          /* audit_token_t */
53 #endif
54
55 /*
56  * Size parsed token vectors for execve(2) arguments and environmental
57  * variables.  Note: changing these sizes affects the ABI of the token
58  * structure, and as the token structure is often placed in the caller stack,
59  * this is undesirable.
60  */
61 #define AUDIT_MAX_ARGS  128
62 #define AUDIT_MAX_ENV   128
63
64 /*
65  * Arguments to au_preselect(3).
66  */
67 #define AU_PRS_USECACHE 0
68 #define AU_PRS_REREAD   1
69
70 #define AU_PRS_SUCCESS  1
71 #define AU_PRS_FAILURE  2
72 #define AU_PRS_BOTH     (AU_PRS_SUCCESS|AU_PRS_FAILURE)
73
74 #define AUDIT_EVENT_FILE        "/etc/security/audit_event"
75 #define AUDIT_CLASS_FILE        "/etc/security/audit_class"
76 #define AUDIT_CONTROL_FILE      "/etc/security/audit_control"
77 #define AUDIT_USER_FILE         "/etc/security/audit_user"
78
79 #define DIR_CONTROL_ENTRY       "dir"
80 #define MINFREE_CONTROL_ENTRY   "minfree"
81 #define FILESZ_CONTROL_ENTRY    "filesz"
82 #define FLAGS_CONTROL_ENTRY     "flags"
83 #define NA_CONTROL_ENTRY        "naflags"
84 #define POLICY_CONTROL_ENTRY    "policy"
85 #define AUDIT_HOST_CONTROL_ENTRY        "host"
86
87 #define AU_CLASS_NAME_MAX       8
88 #define AU_CLASS_DESC_MAX       72
89 #define AU_EVENT_NAME_MAX       30
90 #define AU_EVENT_DESC_MAX       50
91 #define AU_USER_NAME_MAX        50
92 #define AU_LINE_MAX             256
93 #define MAX_AUDITSTRING_LEN     256
94 #define BSM_TEXTBUFSZ           MAX_AUDITSTRING_LEN     /* OpenSSH compatibility */
95
96 /*
97  * Arguments to au_close(3).
98  */
99 #define AU_TO_NO_WRITE          0       /* Abandon audit record. */
100 #define AU_TO_WRITE             1       /* Commit audit record. */
101
102 __BEGIN_DECLS
103 struct au_event_ent {
104         au_event_t       ae_number;
105         char            *ae_name;
106         char            *ae_desc;
107         au_class_t       ae_class;
108 };
109 typedef struct au_event_ent au_event_ent_t;
110
111 struct au_class_ent {
112         char            *ac_name;
113         au_class_t       ac_class;
114         char            *ac_desc;
115 };
116 typedef struct au_class_ent au_class_ent_t;
117
118 struct au_user_ent {
119         char            *au_name;
120         au_mask_t        au_always;
121         au_mask_t        au_never;
122 };
123 typedef struct au_user_ent au_user_ent_t;
124 __END_DECLS
125
126 #define ADD_TO_MASK(m, c, sel) do {                                     \
127         if (sel & AU_PRS_SUCCESS)                                       \
128                 (m)->am_success |= c;                                   \
129         if (sel & AU_PRS_FAILURE)                                       \
130                 (m)->am_failure |= c;                                   \
131 } while (0)
132
133 #define SUB_FROM_MASK(m, c, sel) do {                                   \
134         if (sel & AU_PRS_SUCCESS)                                       \
135                 (m)->am_success &= ((m)->am_success ^ c);               \
136         if (sel & AU_PRS_FAILURE)                                       \
137                 (m)->am_failure &= ((m)->am_failure ^ c);               \
138 } while (0)
139
140 #define ADDMASK(m, v) do {                                              \
141         (m)->am_success |= (v)->am_success;                             \
142         (m)->am_failure |= (v)->am_failure;                             \
143 } while(0)
144
145 #define SUBMASK(m, v) do {                                              \
146         (m)->am_success &= ((m)->am_success ^ (v)->am_success);         \
147         (m)->am_failure &= ((m)->am_failure ^ (v)->am_failure);         \
148 } while(0)
149
150 __BEGIN_DECLS
151
152 typedef struct au_tid32 {
153         u_int32_t       port;
154         u_int32_t       addr;
155 } au_tid32_t;
156
157 typedef struct au_tid64 {
158         u_int64_t       port;
159         u_int32_t       addr;
160 } au_tid64_t;
161
162 typedef struct au_tidaddr32 {
163         u_int32_t       port;
164         u_int32_t       type;
165         u_int32_t       addr[4];
166 } au_tidaddr32_t;
167
168 typedef struct au_tidaddr64 {
169         u_int64_t       port;
170         u_int32_t       type;
171         u_int32_t       addr[4];
172 } au_tidaddr64_t;
173
174 /*
175  * argument #              1 byte
176  * argument value          4 bytes/8 bytes (32-bit/64-bit value)
177  * text length             2 bytes
178  * text                    N bytes + 1 terminating NULL byte
179  */
180 typedef struct {
181         u_char           no;
182         u_int32_t        val;
183         u_int16_t        len;
184         char            *text;
185 } au_arg32_t;
186
187 typedef struct {
188         u_char           no;
189         u_int64_t        val;
190         u_int16_t        len;
191         char            *text;
192 } au_arg64_t;
193
194 /*
195  * how to print            1 byte
196  * basic unit              1 byte
197  * unit count              1 byte
198  * data items              (depends on basic unit)
199  */
200 typedef struct {
201         u_char   howtopr;
202         u_char   bu;
203         u_char   uc;
204         u_char  *data;
205 } au_arb_t;
206
207 /*
208  * file access mode        4 bytes
209  * owner user ID           4 bytes
210  * owner group ID          4 bytes
211  * file system ID          4 bytes
212  * node ID                 8 bytes
213  * device                  4 bytes/8 bytes (32-bit/64-bit)
214  */
215 typedef struct {
216         u_int32_t       mode;
217         u_int32_t       uid;
218         u_int32_t       gid;
219         u_int32_t       fsid;
220         u_int64_t       nid;
221         u_int32_t       dev;
222 } au_attr32_t;
223
224 typedef struct {
225         u_int32_t       mode;
226         u_int32_t       uid;
227         u_int32_t       gid;
228         u_int32_t       fsid;
229         u_int64_t       nid;
230         u_int64_t       dev;
231 } au_attr64_t;
232
233 /*
234  * count                   4 bytes
235  * text                    count null-terminated string(s)
236  */
237 typedef struct {
238         u_int32_t        count;
239         char            *text[AUDIT_MAX_ARGS];
240 } au_execarg_t;
241
242 /*
243  * count                   4 bytes
244  * text                    count null-terminated string(s)
245  */
246 typedef struct {
247         u_int32_t        count;
248         char            *text[AUDIT_MAX_ENV];
249 } au_execenv_t;
250
251 /*
252  * status                  4 bytes
253  * return value            4 bytes
254  */
255 typedef struct {
256         u_int32_t       status;
257         u_int32_t       ret;
258 } au_exit_t;
259
260 /*
261  * seconds of time         4 bytes
262  * milliseconds of time    4 bytes
263  * file name length        2 bytes
264  * file pathname           N bytes + 1 terminating NULL byte
265  */
266 typedef struct {
267         u_int32_t        s;
268         u_int32_t        ms;
269         u_int16_t        len;
270         char            *name;
271 } au_file_t;
272
273
274 /*
275  * number groups           2 bytes
276  * group list              N * 4 bytes
277  */
278 typedef struct {
279         u_int16_t       no;
280         u_int32_t       list[AUDIT_MAX_GROUPS];
281 } au_groups_t;
282
283 /*
284  * record byte count       4 bytes
285  * version #               1 byte    [2]
286  * event type              2 bytes
287  * event modifier          2 bytes
288  * seconds of time         4 bytes/8 bytes (32-bit/64-bit value)
289  * milliseconds of time    4 bytes/8 bytes (32-bit/64-bit value)
290  */
291 typedef struct {
292         u_int32_t       size;
293         u_char          version;
294         u_int16_t       e_type;
295         u_int16_t       e_mod;
296         u_int32_t       s;
297         u_int32_t       ms;
298 } au_header32_t;
299
300 /*
301  * record byte count       4 bytes
302  * version #               1 byte     [2]
303  * event type              2 bytes
304  * event modifier          2 bytes
305  * address type/length     1 byte (XXX: actually, 4 bytes)
306  * machine address         4 bytes/16 bytes (IPv4/IPv6 address)
307  * seconds of time         4 bytes/8 bytes  (32/64-bits)
308  * nanoseconds of time     4 bytes/8 bytes  (32/64-bits)
309  */
310 typedef struct {
311         u_int32_t       size;
312         u_char          version;
313         u_int16_t       e_type;
314         u_int16_t       e_mod;
315         u_int32_t       ad_type;
316         u_int32_t       addr[4];
317         u_int32_t       s;
318         u_int32_t       ms;
319 } au_header32_ex_t;
320
321 typedef struct {
322         u_int32_t       size;
323         u_char          version;
324         u_int16_t       e_type;
325         u_int16_t       e_mod;
326         u_int64_t       s;
327         u_int64_t       ms;
328 } au_header64_t;
329
330 typedef struct {
331         u_int32_t       size;
332         u_char          version;
333         u_int16_t       e_type;
334         u_int16_t       e_mod;
335         u_int32_t       ad_type;
336         u_int32_t       addr[4];
337         u_int64_t       s;
338         u_int64_t       ms;
339 } au_header64_ex_t;
340
341 /*
342  * internet address        4 bytes
343  */
344 typedef struct {
345         u_int32_t       addr;
346 } au_inaddr_t;
347
348 /*
349  * type                 4 bytes
350  * internet address     16 bytes
351  */
352 typedef struct {
353         u_int32_t       type;
354         u_int32_t       addr[4];
355 } au_inaddr_ex_t;
356
357 /*
358  * version and ihl         1 byte
359  * type of service         1 byte
360  * length                  2 bytes
361  * id                      2 bytes
362  * offset                  2 bytes
363  * ttl                     1 byte
364  * protocol                1 byte
365  * checksum                2 bytes
366  * source address          4 bytes
367  * destination address     4 bytes
368  */
369 typedef struct {
370         u_char          version;
371         u_char          tos;
372         u_int16_t       len;
373         u_int16_t       id;
374         u_int16_t       offset;
375         u_char          ttl;
376         u_char          prot;
377         u_int16_t       chksm;
378         u_int32_t       src;
379         u_int32_t       dest;
380 } au_ip_t;
381
382 /*
383  * object ID type          1 byte
384  * object ID               4 bytes
385  */
386 typedef struct {
387         u_char          type;
388         u_int32_t       id;
389 } au_ipc_t;
390
391 /*
392  * owner user ID           4 bytes
393  * owner group ID          4 bytes
394  * creator user ID         4 bytes
395  * creator group ID        4 bytes
396  * access mode             4 bytes
397  * slot sequence #         4 bytes
398  * key                     4 bytes
399  */
400 typedef struct {
401         u_int32_t       uid;
402         u_int32_t       gid;
403         u_int32_t       puid;
404         u_int32_t       pgid;
405         u_int32_t       mode;
406         u_int32_t       seq;
407         u_int32_t       key;
408 } au_ipcperm_t;
409
410 /*
411  * port IP address         2 bytes
412  */
413 typedef struct {
414         u_int16_t       port;
415 } au_iport_t;
416
417 /*
418  * length               2 bytes
419  * data                 length bytes
420  */
421 typedef struct {
422         u_int16_t        size;
423         char            *data;
424 } au_opaque_t;
425
426 /*
427  * path length             2 bytes
428  * path                    N bytes + 1 terminating NULL byte
429  */
430 typedef struct {
431         u_int16_t        len;
432         char            *path;
433 } au_path_t;
434
435 /*
436  * audit ID                4 bytes
437  * effective user ID       4 bytes
438  * effective group ID      4 bytes
439  * real user ID            4 bytes
440  * real group ID           4 bytes
441  * process ID              4 bytes
442  * session ID              4 bytes
443  * terminal ID
444  * port ID               4 bytes/8 bytes (32-bit/64-bit value)
445  * machine address       4 bytes
446  */
447 typedef struct {
448         u_int32_t       auid;
449         u_int32_t       euid;
450         u_int32_t       egid;
451         u_int32_t       ruid;
452         u_int32_t       rgid;
453         u_int32_t       pid;
454         u_int32_t       sid;
455         au_tid32_t      tid;
456 } au_proc32_t;
457
458 typedef struct {
459         u_int32_t       auid;
460         u_int32_t       euid;
461         u_int32_t       egid;
462         u_int32_t       ruid;
463         u_int32_t       rgid;
464         u_int32_t       pid;
465         u_int32_t       sid;
466         au_tid64_t      tid;
467 } au_proc64_t;
468
469 /*
470  * audit ID                4 bytes
471  * effective user ID       4 bytes
472  * effective group ID      4 bytes
473  * real user ID            4 bytes
474  * real group ID           4 bytes
475  * process ID              4 bytes
476  * session ID              4 bytes
477  * terminal ID
478  * port ID               4 bytes/8 bytes (32-bit/64-bit value)
479  * type                  4 bytes
480  * machine address       16 bytes
481  */
482 typedef struct {
483         u_int32_t       auid;
484         u_int32_t       euid;
485         u_int32_t       egid;
486         u_int32_t       ruid;
487         u_int32_t       rgid;
488         u_int32_t       pid;
489         u_int32_t       sid;
490         au_tidaddr32_t  tid;
491 } au_proc32ex_t;
492
493 typedef struct {
494         u_int32_t       auid;
495         u_int32_t       euid;
496         u_int32_t       egid;
497         u_int32_t       ruid;
498         u_int32_t       rgid;
499         u_int32_t       pid;
500         u_int32_t       sid;
501         au_tidaddr64_t  tid;
502 } au_proc64ex_t;
503
504 /*
505  * error status            1 byte
506  * return value            4 bytes/8 bytes (32-bit/64-bit value)
507  */
508 typedef struct {
509         u_char          status;
510         u_int32_t       ret;
511 } au_ret32_t;
512
513 typedef struct {
514         u_char          err;
515         u_int64_t       val;
516 } au_ret64_t;
517
518 /*
519  * sequence number         4 bytes
520  */
521 typedef struct {
522         u_int32_t       seqno;
523 } au_seq_t;
524
525 /*
526  * socket type             2 bytes
527  * local port              2 bytes
528  * local Internet address  4 bytes
529  * remote port             2 bytes
530  * remote Internet address 4 bytes
531  */
532 typedef struct {
533         u_int16_t       type;
534         u_int16_t       l_port;
535         u_int32_t       l_addr;
536         u_int16_t       r_port;
537         u_int32_t       r_addr;
538 } au_socket_t;
539
540 /*
541  * socket type             2 bytes
542  * local port              2 bytes
543  * address type/length     4 bytes
544  * local Internet address  4 bytes/16 bytes (IPv4/IPv6 address)
545  * remote port             4 bytes
546  * address type/length     4 bytes
547  * remote Internet address 4 bytes/16 bytes (IPv4/IPv6 address)
548  */
549 typedef struct {
550         u_int16_t       domain;
551         u_int16_t       type;
552         u_int16_t       atype;
553         u_int16_t       l_port;
554         u_int32_t       l_addr[4];
555         u_int32_t       r_port;
556         u_int32_t       r_addr[4];
557 } au_socket_ex32_t;
558
559 /*
560  * socket family           2 bytes
561  * local port              2 bytes
562  * socket address          4 bytes/16 bytes (IPv4/IPv6 address)
563  */
564 typedef struct {
565         u_int16_t       family;
566         u_int16_t       port;
567         u_int32_t       addr;
568 } au_socketinet32_t;
569
570 /*
571  * socket family           2 bytes
572  * path                    104 bytes
573  */
574 typedef struct {
575         u_int16_t       family;
576         char            path[104];
577 } au_socketunix_t;
578
579 /*
580  * audit ID                4 bytes
581  * effective user ID       4 bytes
582  * effective group ID      4 bytes
583  * real user ID            4 bytes
584  * real group ID           4 bytes
585  * process ID              4 bytes
586  * session ID              4 bytes
587  * terminal ID
588  *      port ID               4 bytes/8 bytes (32-bit/64-bit value)
589  *      machine address       4 bytes
590  */
591 typedef struct {
592         u_int32_t       auid;
593         u_int32_t       euid;
594         u_int32_t       egid;
595         u_int32_t       ruid;
596         u_int32_t       rgid;
597         u_int32_t       pid;
598         u_int32_t       sid;
599         au_tid32_t      tid;
600 } au_subject32_t;
601
602 typedef struct {
603         u_int32_t       auid;
604         u_int32_t       euid;
605         u_int32_t       egid;
606         u_int32_t       ruid;
607         u_int32_t       rgid;
608         u_int32_t       pid;
609         u_int32_t       sid;
610         au_tid64_t      tid;
611 } au_subject64_t;
612
613 /*
614  * audit ID                4 bytes
615  * effective user ID       4 bytes
616  * effective group ID      4 bytes
617  * real user ID            4 bytes
618  * real group ID           4 bytes
619  * process ID              4 bytes
620  * session ID              4 bytes
621  * terminal ID
622  * port ID               4 bytes/8 bytes (32-bit/64-bit value)
623  * type                  4 bytes
624  * machine address       16 bytes
625  */
626 typedef struct {
627         u_int32_t       auid;
628         u_int32_t       euid;
629         u_int32_t       egid;
630         u_int32_t       ruid;
631         u_int32_t       rgid;
632         u_int32_t       pid;
633         u_int32_t       sid;
634         au_tidaddr32_t  tid;
635 } au_subject32ex_t;
636
637 typedef struct {
638         u_int32_t       auid;
639         u_int32_t       euid;
640         u_int32_t       egid;
641         u_int32_t       ruid;
642         u_int32_t       rgid;
643         u_int32_t       pid;
644         u_int32_t       sid;
645         au_tidaddr64_t  tid;
646 } au_subject64ex_t;
647
648 /*
649  * text length             2 bytes
650  * text                    N bytes + 1 terminating NULL byte
651  */
652 typedef struct {
653         u_int16_t        len;
654         char            *text;
655 } au_text_t;
656
657 /*
658  * zonename length      2 bytes
659  * zonename text        N bytes + 1 NULL terminator
660  */
661 typedef struct {
662         u_int16_t        len;
663         char            *zonename;
664 } au_zonename_t;
665
666 typedef struct {
667         u_int32_t       ident;
668         u_int16_t       filter;
669         u_int16_t       flags;
670         u_int32_t       fflags;
671         u_int32_t       data;
672 } au_kevent_t;
673
674 typedef struct {
675         u_int16_t        length;
676         char            *data;
677 } au_invalid_t;
678
679 /*
680  * trailer magic number    2 bytes
681  * record byte count       4 bytes
682  */
683 typedef struct {
684         u_int16_t       magic;
685         u_int32_t       count;
686 } au_trailer_t;
687
688 struct tokenstr {
689         u_char   id;
690         u_char  *data;
691         size_t   len;
692         union {
693                 au_arg32_t              arg32;
694                 au_arg64_t              arg64;
695                 au_arb_t                arb;
696                 au_attr32_t             attr32;
697                 au_attr64_t             attr64;
698                 au_execarg_t            execarg;
699                 au_execenv_t            execenv;
700                 au_exit_t               exit;
701                 au_file_t               file;
702                 au_groups_t             grps;
703                 au_header32_t           hdr32;
704                 au_header32_ex_t        hdr32_ex;
705                 au_header64_t           hdr64;
706                 au_header64_ex_t        hdr64_ex;
707                 au_inaddr_t             inaddr;
708                 au_inaddr_ex_t          inaddr_ex;
709                 au_ip_t                 ip;
710                 au_ipc_t                ipc;
711                 au_ipcperm_t            ipcperm;
712                 au_iport_t              iport;
713                 au_opaque_t             opaque;
714                 au_path_t               path;
715                 au_proc32_t             proc32;
716                 au_proc32ex_t           proc32_ex;
717                 au_proc64_t             proc64;
718                 au_proc64ex_t           proc64_ex;
719                 au_ret32_t              ret32;
720                 au_ret64_t              ret64;
721                 au_seq_t                seq;
722                 au_socket_t             socket;
723                 au_socket_ex32_t        socket_ex32;
724                 au_socketinet32_t       sockinet32;
725                 au_socketunix_t         sockunix;
726                 au_subject32_t          subj32;
727                 au_subject32ex_t        subj32_ex;
728                 au_subject64_t          subj64;
729                 au_subject64ex_t        subj64_ex;
730                 au_text_t               text;
731                 au_kevent_t             kevent;
732                 au_invalid_t            invalid;
733                 au_trailer_t            trail;
734                 au_zonename_t           zonename;
735         } tt; /* The token is one of the above types */
736 };
737
738 typedef struct tokenstr tokenstr_t;
739
740 int                      audit_submit(short au_event, au_id_t auid,
741                             char status, int reterr, const char *fmt, ...);
742
743 /*
744  * Functions relating to querying audit class information.
745  */
746 void                     setauclass(void);
747 void                     endauclass(void);
748 struct au_class_ent     *getauclassent(void);
749 struct au_class_ent     *getauclassent_r(au_class_ent_t *class_int);
750 struct au_class_ent     *getauclassnam(const char *name);
751 struct au_class_ent     *getauclassnam_r(au_class_ent_t *class_int,
752                             const char *name);
753 struct au_class_ent     *getauclassnum(au_class_t class_number);
754 struct au_class_ent     *getauclassnum_r(au_class_ent_t *class_int,
755                             au_class_t class_number);
756
757 /*
758  * Functions relating to querying audit control information.
759  */
760 void                     setac(void);
761 void                     endac(void);
762 int                      getacdir(char *name, int len);
763 int                      getacmin(int *min_val);
764 int                      getacfilesz(size_t *size_val);
765 int                      getacflg(char *auditstr, int len);
766 int                      getacna(char *auditstr, int len);
767 int                      getacpol(char *auditstr, size_t len);
768 int                      getachost(char *auditstr, size_t len);
769 int                      getauditflagsbin(char *auditstr, au_mask_t *masks);
770 int                      getauditflagschar(char *auditstr, au_mask_t *masks,
771                             int verbose);
772 int                      au_preselect(au_event_t event, au_mask_t *mask_p,
773                             int sorf, int flag);
774 ssize_t                  au_poltostr(long policy, size_t maxsize, char *buf);
775 int                      au_strtopol(const char *polstr, long *policy);
776
777 /*
778  * Functions relating to querying audit event information.
779  */
780 void                     setauevent(void);
781 void                     endauevent(void);
782 struct au_event_ent     *getauevent(void);
783 struct au_event_ent     *getauevent_r(struct au_event_ent *e);
784 struct au_event_ent     *getauevnam(const char *name);
785 struct au_event_ent     *getauevnam_r(struct au_event_ent *e,
786                             const char *name);
787 struct au_event_ent     *getauevnum(au_event_t event_number);
788 struct au_event_ent     *getauevnum_r(struct au_event_ent *e,
789                             au_event_t event_number);
790 au_event_t              *getauevnonam(const char *event_name);
791 au_event_t              *getauevnonam_r(au_event_t *ev,
792                             const char *event_name);
793
794 /*
795  * Functions relating to querying audit user information.
796  */
797 void                     setauuser(void);
798 void                     endauuser(void);
799 struct au_user_ent      *getauuserent(void);
800 struct au_user_ent      *getauuserent_r(struct au_user_ent *u);
801 struct au_user_ent      *getauusernam(const char *name);
802 struct au_user_ent      *getauusernam_r(struct au_user_ent *u,
803                             const char *name);
804 int                      au_user_mask(char *username, au_mask_t *mask_p);
805 int                      getfauditflags(au_mask_t *usremask,
806                             au_mask_t *usrdmask, au_mask_t *lastmask);
807
808 /*
809  * Functions for reading and printing records and tokens from audit trails.
810  */
811 int                      au_read_rec(FILE *fp, u_char **buf);
812 int                      au_fetch_tok(tokenstr_t *tok, u_char *buf, int len);
813 //XXX The following interface has different prototype from BSM
814 void                     au_print_tok(FILE *outfp, tokenstr_t *tok,
815                             char *del, char raw, char sfrm);
816 void                     au_print_tok_xml(FILE *outfp, tokenstr_t *tok,
817                             char *del, char raw, char sfrm);
818
819 /* 
820  * Functions relating to XML output.
821  */
822 void                     au_print_xml_header(FILE *outfp);
823 void                     au_print_xml_footer(FILE *outfp);
824 __END_DECLS
825
826 /*
827  * Functions relating to BSM<->errno conversion.
828  */
829 int                      au_bsm_to_errno(u_char bsm_error, int *errorp);
830 u_char                   au_errno_to_bsm(int error);
831 const char              *au_strerror(u_char bsm_error);
832
833 /*
834  * The remaining APIs are associated with Apple's BSM implementation, in
835  * particular as relates to Mach IPC auditing and triggers passed via Mach
836  * IPC.
837  */
838 #ifdef __APPLE__
839 #include <sys/appleapiopts.h>
840
841 /**************************************************************************
842  **************************************************************************
843  ** The following definitions, functions, etc., are NOT officially
844  ** supported: they may be changed or removed in the future.  Do not use
845  ** them unless you are prepared to cope with that eventuality.
846  **************************************************************************
847  **************************************************************************/
848
849 #ifdef __APPLE_API_PRIVATE
850 #define __BSM_INTERNAL_NOTIFY_KEY       "com.apple.audit.change"
851 #endif /* __APPLE_API_PRIVATE */
852
853 /*
854  * au_get_state() return values
855  * XXX  use AUC_* values directly instead (<bsm/audit.h>); AUDIT_OFF and
856  * AUDIT_ON are deprecated and WILL be removed.
857  */
858 #ifdef __APPLE_API_PRIVATE
859 #define AUDIT_OFF       AUC_NOAUDIT
860 #define AUDIT_ON        AUC_AUDITING
861 #endif /* __APPLE_API_PRIVATE */
862 #endif /* !__APPLE__ */
863
864 /*
865  * Error return codes for audit_set_terminal_id(), audit_write() and its
866  * brethren.  We have 255 (not including kAUNoErr) to play with.
867  *
868  * XXXRW: In Apple's bsm-8, these are marked __APPLE_API_PRIVATE.
869  */
870 enum {
871         kAUNoErr                        = 0,
872         kAUBadParamErr                  = -66049,
873         kAUStatErr,
874         kAUSysctlErr,
875         kAUOpenErr,
876         kAUMakeSubjectTokErr,
877         kAUWriteSubjectTokErr,
878         kAUWriteCallerTokErr,
879         kAUMakeReturnTokErr,
880         kAUWriteReturnTokErr,
881         kAUCloseErr,
882         kAUMakeTextTokErr,
883         kAULastErr
884 };
885
886 #ifdef __APPLE__
887 /*
888  * Error return codes for au_get_state() and/or its private support
889  * functions.  These codes are designed to be compatible with the
890  * NOTIFY_STATUS_* codes defined in <notify.h> but non-overlapping.
891  * Any changes to notify(3) may cause these values to change in future.
892  *
893  * AU_UNIMPL should never happen unless you've changed your system software
894  * without rebooting.  Shame on you.
895  */
896 #ifdef __APPLE_API_PRIVATE
897 #define AU_UNIMPL       NOTIFY_STATUS_FAILED + 1        /* audit unimplemented */
898 #endif /* __APPLE_API_PRIVATE */
899 #endif /* !__APPLE__ */
900
901 __BEGIN_DECLS
902 /*
903  * XXX  This prototype should be in audit_record.h
904  *
905  * au_free_token()
906  *
907  * @summary - au_free_token() deallocates a token_t created by any of
908  * the au_to_*() BSM API functions.
909  *
910  * The BSM API generally manages deallocation of token_t objects.  However,
911  * if au_write() is passed a bad audit descriptor, the token_t * parameter
912  * will be left untouched.  In that case, the caller can deallocate the
913  * token_t using au_free_token() if desired.  This is, in fact, what
914  * audit_write() does, in keeping with the existing memory management model
915  * of the BSM API.
916  *
917  * @param tok - A token_t * generated by one of the au_to_*() BSM API
918  * calls.  For convenience, tok may be NULL, in which case
919  * au_free_token() returns immediately.
920  *
921  * XXXRW: In Apple's bsm-8, these are marked __APPLE_API_PRIVATE.
922  */
923 void    au_free_token(token_t *tok);
924
925 /*
926  * Lightweight check to determine if auditing is enabled.  If a client
927  * wants to use this to govern whether an entire series of audit calls
928  * should be made--as in the common case of a caller building a set of
929  * tokens, then writing them--it should cache the audit status in a local
930  * variable.  This call always returns the current state of auditing.
931  *
932  * @return - AUC_AUDITING or AUC_NOAUDIT if no error occurred.
933  * Otherwise the function can return any of the errno values defined for
934  * setaudit(2), or AU_UNIMPL if audit does not appear to be supported by
935  * the system.
936  *
937  * XXXRW: In Apple's bsm-8, these are marked __APPLE_API_PRIVATE.
938  */
939 int     au_get_state(void);
940
941 /*
942  * Initialize the audit notification.  If it has not already been initialized
943  * it will automatically on the first call of au_get_state().
944  */
945 uint32_t        au_notify_initialize(void);
946
947 /*
948  * Cancel audit notification and free the resources associated with it.
949  * Responsible code that no longer needs to use au_get_state() should call
950  * this.
951  */
952 int             au_notify_terminate(void);
953 __END_DECLS
954
955 /* OpenSSH compatibility */
956 int     cannot_audit(int);
957
958 __BEGIN_DECLS
959 /*
960  * audit_set_terminal_id()
961  *
962  * @summary - audit_set_terminal_id() fills in an au_tid_t struct, which is
963  * used in audit session initialization by processes like /usr/bin/login.
964  *
965  * @param tid - A pointer to an au_tid_t struct.
966  *
967  * @return - kAUNoErr on success; kAUBadParamErr if tid is NULL, kAUStatErr
968  * or kAUSysctlErr if one of the underlying system calls fails (a message
969  * is sent to the system log in those cases).
970  *
971  * XXXRW: In Apple's bsm-8, these are marked __APPLE_API_PRIVATE.
972  */
973 int     audit_set_terminal_id(au_tid_t *tid);
974
975 /*
976  * BEGIN au_write() WRAPPERS
977  *
978  * The following calls all wrap the existing BSM API.  They use the
979  * provided subject information, if any, to construct the subject token
980  * required for every log message.  They use the provided return/error
981  * value(s), if any, to construct the success/failure indication required
982  * for every log message.  They only permit one "miscellaneous" token,
983  * which should contain the event-specific logging information mandated by
984  * CAPP.
985  *
986  * All these calls assume the caller has previously determined that
987  * auditing is enabled by calling au_get_state().
988  */
989
990 /*
991  * audit_write()
992  *
993  * @summary - audit_write() is the basis for the other audit_write_*()
994  * calls.  Performs a basic write of an audit record (subject, additional
995  * info, success/failure).  Note that this call only permits logging one
996  * caller-specified token; clients needing to log more flexibly must use
997  * the existing BSM API (au_open(), et al.) directly.
998  *
999  * Note on memory management: audit_write() guarantees that the token_t *s
1000  * passed to it will be deallocated whether or not the underlying write to
1001  * the audit log succeeded.  This addresses an inconsistency in the
1002  * underlying BSM API in which token_t *s are usually but not always
1003  * deallocated.
1004  *
1005  * @param event_code - The code for the event being logged.  This should
1006  * be one of the AUE_ values in /usr/include/bsm/audit_uevents.h.
1007  *
1008  * @param subject - A token_t * generated by au_to_subject(),
1009  * au_to_subject32(), au_to_subject64(), or au_to_me().  If no subject is
1010  * required, subject should be NULL.
1011  *
1012  * @param misctok - A token_t * generated by one of the au_to_*() BSM API
1013  * calls.  This should correspond to the additional information required by
1014  * CAPP for the event being audited.  If no additional information is
1015  * required, misctok should be NULL.
1016  *
1017  * @param retval - The return value to be logged for this event.  This
1018  * should be 0 (zero) for success, otherwise the value is event-specific.
1019  *
1020  * @param errcode - Any error code associated with the return value (e.g.,
1021  * errno or h_errno).  If there was no error, errcode should be 0 (zero).
1022  *
1023  * @return - The status of the call: 0 (zero) on success, else one of the
1024  * kAU*Err values defined above.
1025  *
1026  * XXXRW: In Apple's bsm-8, these are marked __APPLE_API_PRIVATE.
1027  */
1028 int     audit_write(short event_code, token_t *subject, token_t *misctok,
1029             char retval, int errcode);
1030
1031 /*
1032  * audit_write_success()
1033  *
1034  * @summary - audit_write_success() records an auditable event that did not
1035  * encounter an error.  The interface is designed to require as little
1036  * direct use of the au_to_*() API as possible.  It builds a subject token
1037  * from the information passed in and uses that to invoke audit_write().
1038  * A subject, as defined by CAPP, is a process acting on the user's behalf.
1039  *
1040  * If the subject information is the same as the current process, use
1041  * au_write_success_self().
1042  *
1043  * @param event_code - The code for the event being logged.  This should
1044  * be one of the AUE_ values in /usr/include/bsm/audit_uevents.h.
1045  *
1046  * @param misctok - A token_t * generated by one of the au_to_*() BSM API
1047  * calls.  This should correspond to the additional information required by
1048  * CAPP for the event being audited.  If no additional information is
1049  * required, misctok should be NULL.
1050  *
1051  * @param auid - The subject's audit ID.
1052  *
1053  * @param euid - The subject's effective user ID.
1054  *
1055  * @param egid - The subject's effective group ID.
1056  *
1057  * @param ruid - The subject's real user ID.
1058  *
1059  * @param rgid - The subject's real group ID.
1060  *
1061  * @param pid - The subject's process ID.
1062  *
1063  * @param sid - The subject's session ID.
1064  *
1065  * @param tid - The subject's terminal ID.
1066  *
1067  * @return - The status of the call: 0 (zero) on success, else one of the
1068  * kAU*Err values defined above.
1069  *
1070  * XXXRW: In Apple's bsm-8, these are marked __APPLE_API_PRIVATE.
1071  */
1072 int     audit_write_success(short event_code, token_t *misctok, au_id_t auid,
1073             uid_t euid, gid_t egid, uid_t ruid, gid_t rgid, pid_t pid,
1074             au_asid_t sid, au_tid_t *tid);
1075
1076 /*
1077  * audit_write_success_self()
1078  *
1079  * @summary - Similar to audit_write_success(), but used when the subject
1080  * (process) is owned and operated by the auditable user him/herself.
1081  *
1082  * @param event_code - The code for the event being logged.  This should
1083  * be one of the AUE_ values in /usr/include/bsm/audit_uevents.h.
1084  *
1085  * @param misctok - A token_t * generated by one of the au_to_*() BSM API
1086  * calls.  This should correspond to the additional information required by
1087  * CAPP for the event being audited.  If no additional information is
1088  * required, misctok should be NULL.
1089  *
1090  * @return - The status of the call: 0 (zero) on success, else one of the
1091  * kAU*Err values defined above.
1092  *
1093  * XXXRW: In Apple's bsm-8, these are marked __APPLE_API_PRIVATE.
1094  */
1095 int     audit_write_success_self(short event_code, token_t *misctok);
1096
1097 /*
1098  * audit_write_failure()
1099  *
1100  * @summary - audit_write_failure() records an auditable event that
1101  * encountered an error.  The interface is designed to require as little
1102  * direct use of the au_to_*() API as possible.  It builds a subject token
1103  * from the information passed in and uses that to invoke audit_write().
1104  * A subject, as defined by CAPP, is a process acting on the user's behalf.
1105  *
1106  * If the subject information is the same as the current process, use
1107  * au_write_failure_self().
1108  *
1109  * @param event_code - The code for the event being logged.  This should
1110  * be one of the AUE_ values in /usr/include/bsm/audit_uevents.h.
1111  *
1112  * @param errmsg - A text message providing additional information about
1113  * the event being audited.
1114  *
1115  * @param errret - A numerical value providing additional information about
1116  * the error.  This is intended to store the value of errno or h_errno if
1117  * it's relevant.  This can be 0 (zero) if no additional information is
1118  * available.
1119  *
1120  * @param auid - The subject's audit ID.
1121  *
1122  * @param euid - The subject's effective user ID.
1123  *
1124  * @param egid - The subject's effective group ID.
1125  *
1126  * @param ruid - The subject's real user ID.
1127  *
1128  * @param rgid - The subject's real group ID.
1129  *
1130  * @param pid - The subject's process ID.
1131  *
1132  * @param sid - The subject's session ID.
1133  *
1134  * @param tid - The subject's terminal ID.
1135  *
1136  * @return - The status of the call: 0 (zero) on success, else one of the
1137  * kAU*Err values defined above.
1138  *
1139  * XXXRW: In Apple's bsm-8, these are marked __APPLE_API_PRIVATE.
1140  */
1141 int     audit_write_failure(short event_code, char *errmsg, int errret,
1142             au_id_t auid, uid_t euid, gid_t egid, uid_t ruid, gid_t rgid,
1143             pid_t pid, au_asid_t sid, au_tid_t *tid);
1144
1145 /*
1146  * audit_write_failure_self()
1147  *
1148  * @summary - Similar to audit_write_failure(), but used when the subject
1149  * (process) is owned and operated by the auditable user him/herself.
1150  *
1151  * @param event_code - The code for the event being logged.  This should
1152  * be one of the AUE_ values in /usr/include/bsm/audit_uevents.h.
1153  *
1154  * @param errmsg - A text message providing additional information about
1155  * the event being audited.
1156  *
1157  * @param errret - A numerical value providing additional information about
1158  * the error.  This is intended to store the value of errno or h_errno if
1159  * it's relevant.  This can be 0 (zero) if no additional information is
1160  * available.
1161  *
1162  * @return - The status of the call: 0 (zero) on success, else one of the
1163  * kAU*Err values defined above.
1164  *
1165  * XXXRW: In Apple's bsm-8, these are marked __APPLE_API_PRIVATE.
1166  */
1167 int     audit_write_failure_self(short event_code, char *errmsg, int errret);
1168
1169 /*
1170  * audit_write_failure_na()
1171  *
1172  * @summary - audit_write_failure_na() records errors during login.  Such
1173  * errors are implicitly non-attributable (i.e., not ascribable to any user).
1174  *
1175  * @param event_code - The code for the event being logged.  This should
1176  * be one of the AUE_ values in /usr/include/bsm/audit_uevents.h.
1177  *
1178  * @param errmsg - A text message providing additional information about
1179  * the event being audited.
1180  *
1181  * @param errret - A numerical value providing additional information about
1182  * the error.  This is intended to store the value of errno or h_errno if
1183  * it's relevant.  This can be 0 (zero) if no additional information is
1184  * available.
1185  *
1186  * @param euid - The subject's effective user ID.
1187  *
1188  * @param egid - The subject's effective group ID.
1189  *
1190  * @param pid - The subject's process ID.
1191  *
1192  * @param tid - The subject's terminal ID.
1193  *
1194  * @return - The status of the call: 0 (zero) on success, else one of the
1195  * kAU*Err values defined above.
1196  *
1197  * XXXRW: In Apple's bsm-8, these are marked __APPLE_API_PRIVATE.
1198  */
1199 int     audit_write_failure_na(short event_code, char *errmsg, int errret,
1200             uid_t euid, gid_t egid, pid_t pid, au_tid_t *tid);
1201
1202 /* END au_write() WRAPPERS */
1203
1204 #ifdef  __APPLE__
1205 /*
1206  * audit_token_to_au32()
1207  *
1208  * @summary - Extract information from an audit_token_t, used to identify
1209  * Mach tasks and senders of Mach messages as subjects to the audit system.
1210  * audit_tokent_to_au32() is the only method that should be used to parse
1211  * an audit_token_t, since its internal representation may change over
1212  * time.  A pointer parameter may be NULL if that information is not
1213  * needed.
1214  *
1215  * @param atoken - the audit token containing the desired information
1216  *
1217  * @param auidp - Pointer to a uid_t; on return will be set to the task or
1218  * sender's audit user ID
1219  *
1220  * @param euidp - Pointer to a uid_t; on return will be set to the task or
1221  * sender's effective user ID
1222  *
1223  * @param egidp - Pointer to a gid_t; on return will be set to the task or
1224  * sender's effective group ID
1225  *
1226  * @param ruidp - Pointer to a uid_t; on return will be set to the task or
1227  * sender's real user ID
1228  *
1229  * @param rgidp - Pointer to a gid_t; on return will be set to the task or
1230  * sender's real group ID
1231  *
1232  * @param pidp - Pointer to a pid_t; on return will be set to the task or
1233  * sender's process ID
1234  *
1235  * @param asidp - Pointer to an au_asid_t; on return will be set to the
1236  * task or sender's audit session ID
1237  *
1238  * @param tidp - Pointer to an au_tid_t; on return will be set to the task
1239  * or sender's terminal ID
1240  *
1241  * XXXRW: In Apple's bsm-8, these are marked __APPLE_API_PRIVATE.
1242  */
1243 void audit_token_to_au32(
1244         audit_token_t    atoken,
1245         uid_t           *auidp,
1246         uid_t           *euidp,
1247         gid_t           *egidp,
1248         uid_t           *ruidp,
1249         gid_t           *rgidp,
1250         pid_t           *pidp,
1251         au_asid_t       *asidp,
1252         au_tid_t        *tidp);
1253 #endif /* !__APPLE__ */
1254
1255 __END_DECLS
1256
1257 #endif /* !_LIBBSM_H_ */