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