1185573Srwatson/*-
2191270Srwatson * Copyright (c) 2005-2009 Apple Inc.
3146759Srwatson * All rights reserved.
4146759Srwatson *
5146759Srwatson * Redistribution and use in source and binary forms, with or without
6146759Srwatson * modification, are permitted provided that the following conditions
7146759Srwatson * are met:
8146759Srwatson *
9155191Srwatson * 1.  Redistributions of source code must retain the above copyright
10155191Srwatson *     notice, this list of conditions and the following disclaimer.
11155191Srwatson * 2.  Redistributions in binary form must reproduce the above copyright
12155191Srwatson *     notice, this list of conditions and the following disclaimer in the
13155191Srwatson *     documentation and/or other materials provided with the distribution.
14181053Srwatson * 3.  Neither the name of Apple Inc. ("Apple") nor the names of
15155191Srwatson *     its contributors may be used to endorse or promote products derived
16155191Srwatson *     from this software without specific prior written permission.
17146759Srwatson *
18155191Srwatson * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
19155191Srwatson * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20155191Srwatson * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21155191Srwatson * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
22155191Srwatson * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23155191Srwatson * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24155191Srwatson * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25155191Srwatson * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26155191Srwatson * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27155191Srwatson * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28155191Srwatson *
29146759Srwatson * $FreeBSD$
30146759Srwatson */
31146759Srwatson
32191270Srwatson#ifndef	_BSM_AUDIT_H
33155191Srwatson#define	_BSM_AUDIT_H
34146759Srwatson
35191270Srwatson#include <sys/param.h>
36186647Srwatson#include <sys/types.h>
37155191Srwatson
38155191Srwatson#define	AUDIT_RECORD_MAGIC	0x828a0f1b
39155191Srwatson#define	MAX_AUDIT_RECORDS	20
40161635Srwatson#define	MAXAUDITDATA		(0x8000 - 1)
41161635Srwatson#define	MAX_AUDIT_RECORD_SIZE	MAXAUDITDATA
42155191Srwatson#define	MIN_AUDIT_FILE_SIZE	(512 * 1024)
43155191Srwatson
44146759Srwatson/*
45155191Srwatson * Minimum noumber of free blocks on the filesystem containing the audit
46155191Srwatson * log necessary to avoid a hard log rotation. DO NOT SET THIS VALUE TO 0
47155191Srwatson * as the kernel does an unsigned compare, plus we want to leave a few blocks
48155191Srwatson * free so userspace can terminate the log, etc.
49146759Srwatson */
50155191Srwatson#define	AUDIT_HARD_LIMIT_FREE_BLOCKS	4
51155191Srwatson
52155191Srwatson/*
53161635Srwatson * Triggers for the audit daemon.
54155191Srwatson */
55155191Srwatson#define	AUDIT_TRIGGER_MIN		1
56162508Srwatson#define	AUDIT_TRIGGER_LOW_SPACE		1	/* Below low watermark. */
57162508Srwatson#define	AUDIT_TRIGGER_ROTATE_KERNEL	2	/* Kernel requests rotate. */
58162508Srwatson#define	AUDIT_TRIGGER_READ_FILE		3	/* Re-read config file. */
59162508Srwatson#define	AUDIT_TRIGGER_CLOSE_AND_DIE	4	/* Terminate audit. */
60162508Srwatson#define	AUDIT_TRIGGER_NO_SPACE		5	/* Below min free space. */
61186647Srwatson#define	AUDIT_TRIGGER_ROTATE_USER	6	/* User requests rotate. */
62189279Srwatson#define	AUDIT_TRIGGER_INITIALIZE	7	/* User initialize of auditd. */
63189279Srwatson#define	AUDIT_TRIGGER_EXPIRE_TRAILS	8	/* User expiration of trails. */
64189279Srwatson#define	AUDIT_TRIGGER_MAX		8
65155191Srwatson
66155191Srwatson/*
67162508Srwatson * The special device filename (FreeBSD).
68155191Srwatson */
69155191Srwatson#define	AUDITDEV_FILENAME	"audit"
70162508Srwatson#define	AUDIT_TRIGGER_FILE	("/dev/" AUDITDEV_FILENAME)
71155191Srwatson
72155191Srwatson/*
73155191Srwatson * Pre-defined audit IDs
74155191Srwatson */
75186647Srwatson#define	AU_DEFAUDITID	(uid_t)(-1)
76186647Srwatson#define	AU_DEFAUDITSID	 0
77186647Srwatson#define	AU_ASSIGN_ASID	-1
78155191Srwatson
79155191Srwatson/*
80161635Srwatson * IPC types.
81155191Srwatson */
82155191Srwatson#define	AT_IPC_MSG	((u_char)1)	/* Message IPC id. */
83155191Srwatson#define	AT_IPC_SEM	((u_char)2)	/* Semaphore IPC id. */
84155191Srwatson#define	AT_IPC_SHM	((u_char)3)	/* Shared mem IPC id. */
85155191Srwatson
86155191Srwatson/*
87155191Srwatson * Audit conditions.
88155191Srwatson */
89155191Srwatson#define	AUC_UNSET		0
90155191Srwatson#define	AUC_AUDITING		1
91155191Srwatson#define	AUC_NOAUDIT		2
92155191Srwatson#define	AUC_DISABLED		-1
93155191Srwatson
94155191Srwatson/*
95155191Srwatson * auditon(2) commands.
96155191Srwatson */
97191270Srwatson#define	A_OLDGETPOLICY	2
98191270Srwatson#define	A_OLDSETPOLICY	3
99155191Srwatson#define	A_GETKMASK	4
100155191Srwatson#define	A_SETKMASK	5
101191270Srwatson#define	A_OLDGETQCTRL	6
102191270Srwatson#define	A_OLDSETQCTRL	7
103155191Srwatson#define	A_GETCWD	8
104155191Srwatson#define	A_GETCAR	9
105155191Srwatson#define	A_GETSTAT	12
106155191Srwatson#define	A_SETSTAT	13
107155191Srwatson#define	A_SETUMASK	14
108155191Srwatson#define	A_SETSMASK	15
109191270Srwatson#define	A_OLDGETCOND	20
110191270Srwatson#define	A_OLDSETCOND	21
111155191Srwatson#define	A_GETCLASS	22
112155191Srwatson#define	A_SETCLASS	23
113155191Srwatson#define	A_GETPINFO	24
114155191Srwatson#define	A_SETPMASK	25
115155191Srwatson#define	A_SETFSIZE	26
116155191Srwatson#define	A_GETFSIZE	27
117155191Srwatson#define	A_GETPINFO_ADDR	28
118155191Srwatson#define	A_GETKAUDIT	29
119155191Srwatson#define	A_SETKAUDIT	30
120155191Srwatson#define	A_SENDTRIGGER	31
121186647Srwatson#define	A_GETSINFO_ADDR	32
122191270Srwatson#define	A_GETPOLICY	33
123191270Srwatson#define	A_SETPOLICY	34
124191270Srwatson#define	A_GETQCTRL	35
125191270Srwatson#define	A_SETQCTRL	36
126191270Srwatson#define	A_GETCOND	37
127191270Srwatson#define	A_SETCOND	38
128155191Srwatson
129155191Srwatson/*
130155191Srwatson * Audit policy controls.
131155191Srwatson */
132155191Srwatson#define	AUDIT_CNT	0x0001
133155191Srwatson#define	AUDIT_AHLT	0x0002
134155191Srwatson#define	AUDIT_ARGV	0x0004
135155191Srwatson#define	AUDIT_ARGE	0x0008
136161635Srwatson#define	AUDIT_SEQ	0x0010
137161635Srwatson#define	AUDIT_WINDATA	0x0020
138161635Srwatson#define	AUDIT_USER	0x0040
139161635Srwatson#define	AUDIT_GROUP	0x0080
140161635Srwatson#define	AUDIT_TRAIL	0x0100
141161635Srwatson#define	AUDIT_PATH	0x0200
142161635Srwatson#define	AUDIT_SCNT	0x0400
143161635Srwatson#define	AUDIT_PUBLIC	0x0800
144161635Srwatson#define	AUDIT_ZONENAME	0x1000
145161635Srwatson#define	AUDIT_PERZONE	0x2000
146155191Srwatson
147155191Srwatson/*
148162508Srwatson * Default audit queue control parameters.
149155191Srwatson */
150155191Srwatson#define	AQ_HIWATER	100
151155191Srwatson#define	AQ_MAXHIGH	10000
152155191Srwatson#define	AQ_LOWATER	10
153162508Srwatson#define	AQ_BUFSZ	MAXAUDITDATA
154155191Srwatson#define	AQ_MAXBUFSZ	1048576
155155191Srwatson
156155191Srwatson/*
157155191Srwatson * Default minimum percentage free space on file system.
158155191Srwatson */
159155191Srwatson#define	AU_FS_MINFREE	20
160155191Srwatson
161155191Srwatson/*
162155191Srwatson * Type definitions used indicating the length of variable length addresses
163155191Srwatson * in tokens containing addresses, such as header fields.
164155191Srwatson */
165155191Srwatson#define	AU_IPv4		4
166155191Srwatson#define	AU_IPv6		16
167155191Srwatson
168155191Srwatson__BEGIN_DECLS
169155191Srwatson
170155191Srwatsontypedef	uid_t		au_id_t;
171155191Srwatsontypedef	pid_t		au_asid_t;
172146759Srwatsontypedef	u_int16_t	au_event_t;
173155191Srwatsontypedef	u_int16_t	au_emod_t;
174155191Srwatsontypedef	u_int32_t	au_class_t;
175195740Srwatsontypedef	u_int64_t	au_asflgs_t __attribute__ ((aligned (8)));
176146759Srwatson
177155191Srwatsonstruct au_tid {
178155191Srwatson	dev_t		port;
179155191Srwatson	u_int32_t	machine;
180155191Srwatson};
181155191Srwatsontypedef	struct au_tid	au_tid_t;
182155191Srwatson
183155191Srwatsonstruct au_tid_addr {
184155191Srwatson	dev_t		at_port;
185155191Srwatson	u_int32_t	at_type;
186155191Srwatson	u_int32_t	at_addr[4];
187155191Srwatson};
188155191Srwatsontypedef	struct au_tid_addr	au_tid_addr_t;
189155191Srwatson
190155191Srwatsonstruct au_mask {
191155191Srwatson	unsigned int    am_success;     /* Success bits. */
192155191Srwatson	unsigned int    am_failure;     /* Failure bits. */
193155191Srwatson};
194155191Srwatsontypedef	struct au_mask	au_mask_t;
195155191Srwatson
196155191Srwatsonstruct auditinfo {
197155191Srwatson	au_id_t		ai_auid;	/* Audit user ID. */
198155191Srwatson	au_mask_t	ai_mask;	/* Audit masks. */
199155191Srwatson	au_tid_t	ai_termid;	/* Terminal ID. */
200155191Srwatson	au_asid_t	ai_asid;	/* Audit session ID. */
201155191Srwatson};
202155191Srwatsontypedef	struct auditinfo	auditinfo_t;
203155191Srwatson
204155191Srwatsonstruct auditinfo_addr {
205155191Srwatson	au_id_t		ai_auid;	/* Audit user ID. */
206155191Srwatson	au_mask_t	ai_mask;	/* Audit masks. */
207155191Srwatson	au_tid_addr_t	ai_termid;	/* Terminal ID. */
208155191Srwatson	au_asid_t	ai_asid;	/* Audit session ID. */
209195740Srwatson	au_asflgs_t	ai_flags;	/* Audit session flags. */
210155191Srwatson};
211155191Srwatsontypedef	struct auditinfo_addr	auditinfo_addr_t;
212155191Srwatson
213155191Srwatsonstruct auditpinfo {
214155191Srwatson	pid_t		ap_pid;		/* ID of target process. */
215155191Srwatson	au_id_t		ap_auid;	/* Audit user ID. */
216155191Srwatson	au_mask_t	ap_mask;	/* Audit masks. */
217155191Srwatson	au_tid_t	ap_termid;	/* Terminal ID. */
218155191Srwatson	au_asid_t	ap_asid;	/* Audit session ID. */
219155191Srwatson};
220155191Srwatsontypedef	struct auditpinfo	auditpinfo_t;
221155191Srwatson
222155191Srwatsonstruct auditpinfo_addr {
223155191Srwatson	pid_t		ap_pid;		/* ID of target process. */
224155191Srwatson	au_id_t		ap_auid;	/* Audit user ID. */
225155191Srwatson	au_mask_t	ap_mask;	/* Audit masks. */
226155191Srwatson	au_tid_addr_t	ap_termid;	/* Terminal ID. */
227155191Srwatson	au_asid_t	ap_asid;	/* Audit session ID. */
228195740Srwatson	au_asflgs_t	ap_flags;	/* Audit session flags. */
229155191Srwatson};
230155191Srwatsontypedef	struct auditpinfo_addr	auditpinfo_addr_t;
231155191Srwatson
232186647Srwatsonstruct au_session {
233186647Srwatson	auditinfo_addr_t	*as_aia_p;	/* Ptr to full audit info. */
234186647Srwatson	au_mask_t		 as_mask;	/* Process Audit Masks. */
235186647Srwatson};
236186647Srwatsontypedef struct au_session       au_session_t;
237186647Srwatson
238156289Srwatson/*
239156289Srwatson * Contents of token_t are opaque outside of libbsm.
240156289Srwatson */
241155191Srwatsontypedef	struct au_token	token_t;
242155191Srwatson
243155191Srwatson/*
244191270Srwatson * Kernel audit queue control parameters:
245191270Srwatson * 			Default:		Maximum:
246191270Srwatson * 	aq_hiwater:	AQ_HIWATER (100)	AQ_MAXHIGH (10000)
247191270Srwatson * 	aq_lowater:	AQ_LOWATER (10)		<aq_hiwater
248191270Srwatson * 	aq_bufsz:	AQ_BUFSZ (32767)	AQ_MAXBUFSZ (1048576)
249191270Srwatson * 	aq_delay:	20			20000 (not used)
250155191Srwatson */
251155191Srwatsonstruct au_qctrl {
252191270Srwatson	int	aq_hiwater;	/* Max # of audit recs in queue when */
253191270Srwatson				/* threads with new ARs get blocked. */
254191270Srwatson
255191270Srwatson	int	aq_lowater;	/* # of audit recs in queue when */
256191270Srwatson				/* blocked threads get unblocked. */
257191270Srwatson
258191270Srwatson	int	aq_bufsz;	/* Max size of audit record for audit(2). */
259191270Srwatson	int	aq_delay;	/* Queue delay (not used). */
260155191Srwatson	int	aq_minfree;	/* Minimum filesystem percent free space. */
261155191Srwatson};
262155191Srwatsontypedef	struct au_qctrl	au_qctrl_t;
263155191Srwatson
264155191Srwatson/*
265155191Srwatson * Structure for the audit statistics.
266155191Srwatson */
267155191Srwatsonstruct audit_stat {
268155191Srwatson	unsigned int	as_version;
269155191Srwatson	unsigned int	as_numevent;
270155191Srwatson	int		as_generated;
271159256Srwatson	int		as_nonattrib;
272155191Srwatson	int		as_kernel;
273155191Srwatson	int		as_audit;
274155191Srwatson	int		as_auditctl;
275159256Srwatson	int		as_enqueue;
276155191Srwatson	int		as_written;
277155191Srwatson	int		as_wblocked;
278155191Srwatson	int		as_rblocked;
279155191Srwatson	int		as_dropped;
280155191Srwatson	int		as_totalsize;
281155191Srwatson	unsigned int	as_memused;
282155191Srwatson};
283155191Srwatsontypedef	struct audit_stat	au_stat_t;
284155191Srwatson
285155191Srwatson/*
286155191Srwatson * Structure for the audit file statistics.
287155191Srwatson */
288155191Srwatsonstruct audit_fstat {
289187214Srwatson	u_int64_t	af_filesz;
290187214Srwatson	u_int64_t	af_currsz;
291155191Srwatson};
292155191Srwatsontypedef	struct audit_fstat	au_fstat_t;
293155191Srwatson
294155191Srwatson/*
295155191Srwatson * Audit to event class mapping.
296155191Srwatson */
297155191Srwatsonstruct au_evclass_map {
298155191Srwatson	au_event_t	ec_number;
299155191Srwatson	au_class_t	ec_class;
300155191Srwatson};
301155191Srwatsontypedef	struct au_evclass_map	au_evclass_map_t;
302155191Srwatson
303156289Srwatson/*
304156289Srwatson * Audit system calls.
305156289Srwatson */
306155191Srwatson#if !defined(_KERNEL) && !defined(KERNEL)
307155191Srwatsonint	audit(const void *, int);
308155191Srwatsonint	auditon(int, void *, int);
309155191Srwatsonint	auditctl(const char *);
310155191Srwatsonint	getauid(au_id_t *);
311155191Srwatsonint	setauid(const au_id_t *);
312155191Srwatsonint	getaudit(struct auditinfo *);
313155191Srwatsonint	setaudit(const struct auditinfo *);
314155191Srwatsonint	getaudit_addr(struct auditinfo_addr *, int);
315155191Srwatsonint	setaudit_addr(const struct auditinfo_addr *, int);
316191270Srwatson
317191270Srwatson#ifdef __APPLE_API_PRIVATE
318191270Srwatson#include <mach/port.h>
319191270Srwatsonmach_port_name_t audit_session_self(void);
320191270Srwatsonau_asid_t	 audit_session_join(mach_port_name_t port);
321191270Srwatson#endif /* __APPLE_API_PRIVATE */
322191270Srwatson
323155191Srwatson#endif /* defined(_KERNEL) || defined(KERNEL) */
324155191Srwatson
325155191Srwatson__END_DECLS
326155191Srwatson
327155191Srwatson#endif /* !_BSM_AUDIT_H */
328