xref: /freebsd/contrib/openbsm/libbsm/bsm_token.c (revision b626f5a73a48f44a31a200291b141e1da408a2ff)
152267f74SRobert Watson /*-
206edd2f1SRobert Watson  * Copyright (c) 2004-2009 Apple Inc.
3ca0716f5SRobert Watson  * Copyright (c) 2005 SPARTA, Inc.
4ca0716f5SRobert Watson  * All rights reserved.
5ca0716f5SRobert Watson  *
6ca0716f5SRobert Watson  * This code was developed in part by Robert N. M. Watson, Senior Principal
7ca0716f5SRobert Watson  * Scientist, SPARTA, Inc.
8ca0716f5SRobert Watson  *
9ca0716f5SRobert Watson  * Redistribution and use in source and binary forms, with or without
10ca0716f5SRobert Watson  * modification, are permitted provided that the following conditions
11ca0716f5SRobert Watson  * are met:
12ca0716f5SRobert Watson  * 1.  Redistributions of source code must retain the above copyright
13ca0716f5SRobert Watson  *     notice, this list of conditions and the following disclaimer.
14ca0716f5SRobert Watson  * 2.  Redistributions in binary form must reproduce the above copyright
15ca0716f5SRobert Watson  *     notice, this list of conditions and the following disclaimer in the
16ca0716f5SRobert Watson  *     documentation and/or other materials provided with the distribution.
1752267f74SRobert Watson  * 3.  Neither the name of Apple Inc. ("Apple") nor the names of
18ca0716f5SRobert Watson  *     its contributors may be used to endorse or promote products derived
19ca0716f5SRobert Watson  *     from this software without specific prior written permission.
20ca0716f5SRobert Watson  *
21ca0716f5SRobert Watson  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND
22ca0716f5SRobert Watson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23ca0716f5SRobert Watson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24ca0716f5SRobert Watson  * ARE DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR
25ca0716f5SRobert Watson  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26ca0716f5SRobert Watson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27ca0716f5SRobert Watson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28ca0716f5SRobert Watson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29ca0716f5SRobert Watson  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
30ca0716f5SRobert Watson  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31ca0716f5SRobert Watson  * POSSIBILITY OF SUCH DAMAGE.
32ca0716f5SRobert Watson  */
33ca0716f5SRobert Watson 
34ca0716f5SRobert Watson #include <sys/types.h>
353b97a967SRobert Watson 
363b97a967SRobert Watson #include <config/config.h>
37*aa772005SRobert Watson 
38*aa772005SRobert Watson #ifdef USE_ENDIAN_H
393b97a967SRobert Watson #include <endian.h>
40*aa772005SRobert Watson #endif
41*aa772005SRobert Watson #ifdef USE_SYS_ENDIAN_H
42*aa772005SRobert Watson #include <sys/endian.h>
43*aa772005SRobert Watson #endif
44*aa772005SRobert Watson #ifdef USE_MACHINE_ENDIAN_H
45*aa772005SRobert Watson #include <machine/endian.h>
46*aa772005SRobert Watson #endif
47*aa772005SRobert Watson #ifdef USE_COMPAT_ENDIAN_H
483b97a967SRobert Watson #include <compat/endian.h>
49*aa772005SRobert Watson #endif
50*aa772005SRobert Watson #ifdef USE_COMPAT_ENDIAN_ENC_H
51*aa772005SRobert Watson #include <compat/endian_enc.h>
52*aa772005SRobert Watson #endif
53*aa772005SRobert Watson 
543b97a967SRobert Watson #ifdef HAVE_FULL_QUEUE_H
553b97a967SRobert Watson #include <sys/queue.h>
563b97a967SRobert Watson #else /* !HAVE_FULL_QUEUE_H */
573b97a967SRobert Watson #include <compat/queue.h>
583b97a967SRobert Watson #endif /* !HAVE_FULL_QUEUE_H */
593b97a967SRobert Watson 
60ca0716f5SRobert Watson #include <sys/socket.h>
61ca0716f5SRobert Watson #include <sys/time.h>
62ca0716f5SRobert Watson #include <sys/un.h>
63ca0716f5SRobert Watson 
64ca0716f5SRobert Watson #include <sys/ipc.h>
65ca0716f5SRobert Watson 
66ca0716f5SRobert Watson #include <netinet/in.h>
67ca0716f5SRobert Watson #include <netinet/in_systm.h>
68ca0716f5SRobert Watson #include <netinet/ip.h>
69ca0716f5SRobert Watson 
70ca0716f5SRobert Watson #include <assert.h>
71ca0716f5SRobert Watson #include <errno.h>
72ca0716f5SRobert Watson #include <string.h>
73ca0716f5SRobert Watson #include <stdlib.h>
74ca0716f5SRobert Watson #include <unistd.h>
75ca0716f5SRobert Watson 
76ca0716f5SRobert Watson #include <bsm/audit_internal.h>
77ca0716f5SRobert Watson #include <bsm/libbsm.h>
78ca0716f5SRobert Watson 
79ca0716f5SRobert Watson #define	GET_TOKEN_AREA(t, dptr, length) do {				\
80ca0716f5SRobert Watson 	(t) = malloc(sizeof(token_t));					\
81ca0716f5SRobert Watson 	if ((t) != NULL) {						\
82ca0716f5SRobert Watson 		(t)->len = (length);					\
83*aa772005SRobert Watson 		(dptr) = (t->t_data) = calloc((length), sizeof(u_char)); \
84ca0716f5SRobert Watson 		if ((dptr) == NULL) {					\
85ca0716f5SRobert Watson 			free(t);					\
86ca0716f5SRobert Watson 			(t) = NULL;					\
87*aa772005SRobert Watson 		}							\
88ca0716f5SRobert Watson 	} else								\
89ca0716f5SRobert Watson 		(dptr) = NULL;						\
9052267f74SRobert Watson 	assert((t) == NULL || (dptr) != NULL);				\
91ca0716f5SRobert Watson } while (0)
92ca0716f5SRobert Watson 
93ca0716f5SRobert Watson /*
94ca0716f5SRobert Watson  * token ID                1 byte
95*aa772005SRobert Watson  * success/failure         1 byte
96*aa772005SRobert Watson  * privstrlen              2 bytes
97*aa772005SRobert Watson  * privstr                 N bytes + 1 (\0 byte)
98*aa772005SRobert Watson  */
99*aa772005SRobert Watson token_t *
au_to_upriv(char sorf,char * priv)100*aa772005SRobert Watson au_to_upriv(char sorf, char *priv)
101*aa772005SRobert Watson {
102*aa772005SRobert Watson 	u_int16_t textlen;
103*aa772005SRobert Watson 	u_char *dptr;
104*aa772005SRobert Watson 	token_t *t;
105*aa772005SRobert Watson 
106*aa772005SRobert Watson 	textlen = strlen(priv) + 1;
107*aa772005SRobert Watson 	GET_TOKEN_AREA(t, dptr, sizeof(u_char) + sizeof(u_char) +
108*aa772005SRobert Watson 	    sizeof(u_int16_t) + textlen);
109*aa772005SRobert Watson 	if (t == NULL)
110*aa772005SRobert Watson 		return (NULL);
111*aa772005SRobert Watson 	ADD_U_CHAR(dptr, AUT_UPRIV);
112*aa772005SRobert Watson 	ADD_U_CHAR(dptr, sorf);
113*aa772005SRobert Watson 	ADD_U_INT16(dptr, textlen);
114*aa772005SRobert Watson 	ADD_STRING(dptr, priv, textlen);
115*aa772005SRobert Watson 	return (t);
116*aa772005SRobert Watson }
117*aa772005SRobert Watson 
118*aa772005SRobert Watson /*
119*aa772005SRobert Watson  * token ID		1 byte
120*aa772005SRobert Watson  * privtstrlen		2 bytes
121*aa772005SRobert Watson  * privtstr		N bytes + 1
122*aa772005SRobert Watson  * privstrlen		2 bytes
123*aa772005SRobert Watson  * privstr		N bytes + 1
124*aa772005SRobert Watson  */
125*aa772005SRobert Watson token_t *
au_to_privset(char * privtypestr,char * privstr)126*aa772005SRobert Watson au_to_privset(char *privtypestr, char *privstr)
127*aa772005SRobert Watson {
128*aa772005SRobert Watson 	u_int16_t	 type_len, priv_len;
129*aa772005SRobert Watson 	u_char		*dptr;
130*aa772005SRobert Watson 	token_t		*t;
131*aa772005SRobert Watson 
132*aa772005SRobert Watson 	type_len = strlen(privtypestr) + 1;
133*aa772005SRobert Watson 	priv_len = strlen(privstr) + 1;
134*aa772005SRobert Watson 	GET_TOKEN_AREA(t, dptr, sizeof(u_char) + sizeof(u_int16_t) +
135*aa772005SRobert Watson 	    sizeof(u_int16_t) + type_len + priv_len);
136*aa772005SRobert Watson 	if (t == NULL)
137*aa772005SRobert Watson 		return (NULL);
138*aa772005SRobert Watson 	ADD_U_CHAR(dptr, AUT_PRIV);
139*aa772005SRobert Watson 	ADD_U_INT16(dptr, type_len);
140*aa772005SRobert Watson 	ADD_STRING(dptr, privtypestr, type_len);
141*aa772005SRobert Watson 	ADD_U_INT16(dptr, priv_len);
142*aa772005SRobert Watson 	ADD_STRING(dptr, privstr, priv_len);
143*aa772005SRobert Watson 	return (t);
144*aa772005SRobert Watson }
145*aa772005SRobert Watson 
146*aa772005SRobert Watson /*
147*aa772005SRobert Watson  * token ID                1 byte
148ca0716f5SRobert Watson  * argument #              1 byte
149ca0716f5SRobert Watson  * argument value          4 bytes/8 bytes (32-bit/64-bit value)
150ca0716f5SRobert Watson  * text length             2 bytes
151ca0716f5SRobert Watson  * text                    N bytes + 1 terminating NULL byte
152ca0716f5SRobert Watson  */
153ca0716f5SRobert Watson token_t *
au_to_arg32(char n,const char * text,u_int32_t v)15452267f74SRobert Watson au_to_arg32(char n, const char *text, u_int32_t v)
155ca0716f5SRobert Watson {
156ca0716f5SRobert Watson 	token_t *t;
157ca0716f5SRobert Watson 	u_char *dptr = NULL;
158ca0716f5SRobert Watson 	u_int16_t textlen;
159ca0716f5SRobert Watson 
160ca0716f5SRobert Watson 	textlen = strlen(text);
161ca0716f5SRobert Watson 	textlen += 1;
162ca0716f5SRobert Watson 
163ca0716f5SRobert Watson 	GET_TOKEN_AREA(t, dptr, 2 * sizeof(u_char) + sizeof(u_int32_t) +
164ca0716f5SRobert Watson 	    sizeof(u_int16_t) + textlen);
165ca0716f5SRobert Watson 	if (t == NULL)
166ca0716f5SRobert Watson 		return (NULL);
167ca0716f5SRobert Watson 
168ca0716f5SRobert Watson 	ADD_U_CHAR(dptr, AUT_ARG32);
169ca0716f5SRobert Watson 	ADD_U_CHAR(dptr, n);
170ca0716f5SRobert Watson 	ADD_U_INT32(dptr, v);
171ca0716f5SRobert Watson 	ADD_U_INT16(dptr, textlen);
172ca0716f5SRobert Watson 	ADD_STRING(dptr, text, textlen);
173ca0716f5SRobert Watson 
174ca0716f5SRobert Watson 	return (t);
175ca0716f5SRobert Watson }
176ca0716f5SRobert Watson 
177ca0716f5SRobert Watson token_t *
au_to_arg64(char n,const char * text,u_int64_t v)17852267f74SRobert Watson au_to_arg64(char n, const char *text, u_int64_t v)
179ca0716f5SRobert Watson {
180ca0716f5SRobert Watson 	token_t *t;
181ca0716f5SRobert Watson 	u_char *dptr = NULL;
182ca0716f5SRobert Watson 	u_int16_t textlen;
183ca0716f5SRobert Watson 
184ca0716f5SRobert Watson 	textlen = strlen(text);
185ca0716f5SRobert Watson 	textlen += 1;
186ca0716f5SRobert Watson 
187ca0716f5SRobert Watson 	GET_TOKEN_AREA(t, dptr, 2 * sizeof(u_char) + sizeof(u_int64_t) +
188ca0716f5SRobert Watson 	    sizeof(u_int16_t) + textlen);
189ca0716f5SRobert Watson 	if (t == NULL)
190ca0716f5SRobert Watson 		return (NULL);
191ca0716f5SRobert Watson 
192ca0716f5SRobert Watson 	ADD_U_CHAR(dptr, AUT_ARG64);
193ca0716f5SRobert Watson 	ADD_U_CHAR(dptr, n);
194ca0716f5SRobert Watson 	ADD_U_INT64(dptr, v);
195ca0716f5SRobert Watson 	ADD_U_INT16(dptr, textlen);
196ca0716f5SRobert Watson 	ADD_STRING(dptr, text, textlen);
197ca0716f5SRobert Watson 
198ca0716f5SRobert Watson 	return (t);
199ca0716f5SRobert Watson }
200ca0716f5SRobert Watson 
201ca0716f5SRobert Watson token_t *
au_to_arg(char n,const char * text,u_int32_t v)20252267f74SRobert Watson au_to_arg(char n, const char *text, u_int32_t v)
203ca0716f5SRobert Watson {
204ca0716f5SRobert Watson 
205ca0716f5SRobert Watson 	return (au_to_arg32(n, text, v));
206ca0716f5SRobert Watson }
207ca0716f5SRobert Watson 
208ca0716f5SRobert Watson #if defined(_KERNEL) || defined(KERNEL)
209ca0716f5SRobert Watson /*
210ca0716f5SRobert Watson  * token ID                1 byte
211ca0716f5SRobert Watson  * file access mode        4 bytes
212ca0716f5SRobert Watson  * owner user ID           4 bytes
213ca0716f5SRobert Watson  * owner group ID          4 bytes
214ca0716f5SRobert Watson  * file system ID          4 bytes
215ca0716f5SRobert Watson  * node ID                 8 bytes
216ca0716f5SRobert Watson  * device                  4 bytes/8 bytes (32-bit/64-bit)
217ca0716f5SRobert Watson  */
218ca0716f5SRobert Watson token_t *
au_to_attr32(struct vnode_au_info * vni)219ca0716f5SRobert Watson au_to_attr32(struct vnode_au_info *vni)
220ca0716f5SRobert Watson {
221ca0716f5SRobert Watson 	token_t *t;
222ca0716f5SRobert Watson 	u_char *dptr = NULL;
223ca0716f5SRobert Watson 	u_int16_t pad0_16 = 0;
22406edd2f1SRobert Watson 	u_int32_t pad0_32 = 0;
225ca0716f5SRobert Watson 
226ca0716f5SRobert Watson 	GET_TOKEN_AREA(t, dptr, sizeof(u_char) + 2 * sizeof(u_int16_t) +
227ca0716f5SRobert Watson 	    3 * sizeof(u_int32_t) + sizeof(u_int64_t) + sizeof(u_int32_t));
228ca0716f5SRobert Watson 	if (t == NULL)
229ca0716f5SRobert Watson 		return (NULL);
230ca0716f5SRobert Watson 
231ca0716f5SRobert Watson 	ADD_U_CHAR(dptr, AUT_ATTR32);
232ca0716f5SRobert Watson 
233ca0716f5SRobert Watson 	/*
2347a0a89d2SRobert Watson 	 * BSD defines the size for the file mode as 2 bytes; BSM defines 4
2357a0a89d2SRobert Watson 	 * so pad with 0.
2367a0a89d2SRobert Watson 	 *
2377a0a89d2SRobert Watson 	 * XXXRW: Possibly should be conditionally compiled.
2387a0a89d2SRobert Watson 	 *
2397a0a89d2SRobert Watson 	 * XXXRW: Should any conversions take place on the mode?
240ca0716f5SRobert Watson 	 */
241ca0716f5SRobert Watson 	ADD_U_INT16(dptr, pad0_16);
242ca0716f5SRobert Watson 	ADD_U_INT16(dptr, vni->vn_mode);
243ca0716f5SRobert Watson 
244ca0716f5SRobert Watson 	ADD_U_INT32(dptr, vni->vn_uid);
245ca0716f5SRobert Watson 	ADD_U_INT32(dptr, vni->vn_gid);
246ca0716f5SRobert Watson 	ADD_U_INT32(dptr, vni->vn_fsid);
247ca0716f5SRobert Watson 
248ca0716f5SRobert Watson 	/*
24952267f74SRobert Watson 	 * Some systems use 32-bit file ID's, others use 64-bit file IDs.
250ca0716f5SRobert Watson 	 * Attempt to handle both, and let the compiler sort it out.  If we
251ca0716f5SRobert Watson 	 * could pick this out at compile-time, it would be better, so as to
252ca0716f5SRobert Watson 	 * avoid the else case below.
253ca0716f5SRobert Watson 	 */
254ca0716f5SRobert Watson 	if (sizeof(vni->vn_fileid) == sizeof(uint32_t)) {
255ca0716f5SRobert Watson 		ADD_U_INT32(dptr, pad0_32);
256ca0716f5SRobert Watson 		ADD_U_INT32(dptr, vni->vn_fileid);
257ca0716f5SRobert Watson 	} else if (sizeof(vni->vn_fileid) == sizeof(uint64_t))
258ca0716f5SRobert Watson 		ADD_U_INT64(dptr, vni->vn_fileid);
259ca0716f5SRobert Watson 	else
260ca0716f5SRobert Watson 		ADD_U_INT64(dptr, 0LL);
261ca0716f5SRobert Watson 
262ca0716f5SRobert Watson 	ADD_U_INT32(dptr, vni->vn_dev);
263ca0716f5SRobert Watson 
264ca0716f5SRobert Watson 	return (t);
265ca0716f5SRobert Watson }
266ca0716f5SRobert Watson 
267ca0716f5SRobert Watson token_t *
au_to_attr64(struct vnode_au_info * vni)268ca0716f5SRobert Watson au_to_attr64(struct vnode_au_info *vni)
269ca0716f5SRobert Watson {
270bc168a6cSRobert Watson 	token_t *t;
271bc168a6cSRobert Watson 	u_char *dptr = NULL;
272bc168a6cSRobert Watson 	u_int16_t pad0_16 = 0;
27306edd2f1SRobert Watson 	u_int32_t pad0_32 = 0;
274ca0716f5SRobert Watson 
275bc168a6cSRobert Watson 	GET_TOKEN_AREA(t, dptr, sizeof(u_char) + 2 * sizeof(u_int16_t) +
276bc168a6cSRobert Watson 	    3 * sizeof(u_int32_t) + sizeof(u_int64_t) * 2);
277bc168a6cSRobert Watson 	if (t == NULL)
278ca0716f5SRobert Watson 		return (NULL);
279bc168a6cSRobert Watson 
280bc168a6cSRobert Watson 	ADD_U_CHAR(dptr, AUT_ATTR64);
281bc168a6cSRobert Watson 
282bc168a6cSRobert Watson 	/*
2837a0a89d2SRobert Watson 	 * BSD defines the size for the file mode as 2 bytes; BSM defines 4
2847a0a89d2SRobert Watson 	 * so pad with 0.
2857a0a89d2SRobert Watson 	 *
2867a0a89d2SRobert Watson 	 * XXXRW: Possibly should be conditionally compiled.
2877a0a89d2SRobert Watson 	 *
2887a0a89d2SRobert Watson 	 * XXXRW: Should any conversions take place on the mode?
289bc168a6cSRobert Watson 	 */
290bc168a6cSRobert Watson 	ADD_U_INT16(dptr, pad0_16);
291bc168a6cSRobert Watson 	ADD_U_INT16(dptr, vni->vn_mode);
292bc168a6cSRobert Watson 
293bc168a6cSRobert Watson 	ADD_U_INT32(dptr, vni->vn_uid);
294bc168a6cSRobert Watson 	ADD_U_INT32(dptr, vni->vn_gid);
295bc168a6cSRobert Watson 	ADD_U_INT32(dptr, vni->vn_fsid);
296bc168a6cSRobert Watson 
297bc168a6cSRobert Watson 	/*
298bc168a6cSRobert Watson 	 * Some systems use 32-bit file ID's, other's use 64-bit file IDs.
299bc168a6cSRobert Watson 	 * Attempt to handle both, and let the compiler sort it out.  If we
300bc168a6cSRobert Watson 	 * could pick this out at compile-time, it would be better, so as to
301bc168a6cSRobert Watson 	 * avoid the else case below.
302bc168a6cSRobert Watson 	 */
303bc168a6cSRobert Watson 	if (sizeof(vni->vn_fileid) == sizeof(uint32_t)) {
304bc168a6cSRobert Watson 		ADD_U_INT32(dptr, pad0_32);
305bc168a6cSRobert Watson 		ADD_U_INT32(dptr, vni->vn_fileid);
306bc168a6cSRobert Watson 	} else if (sizeof(vni->vn_fileid) == sizeof(uint64_t))
307bc168a6cSRobert Watson 		ADD_U_INT64(dptr, vni->vn_fileid);
308bc168a6cSRobert Watson 	else
309bc168a6cSRobert Watson 		ADD_U_INT64(dptr, 0LL);
310bc168a6cSRobert Watson 
311bc168a6cSRobert Watson 	ADD_U_INT64(dptr, vni->vn_dev);
312bc168a6cSRobert Watson 
313bc168a6cSRobert Watson 	return (t);
314ca0716f5SRobert Watson }
315ca0716f5SRobert Watson 
316ca0716f5SRobert Watson token_t *
au_to_attr(struct vnode_au_info * vni)317ca0716f5SRobert Watson au_to_attr(struct vnode_au_info *vni)
318ca0716f5SRobert Watson {
319ca0716f5SRobert Watson 
320ca0716f5SRobert Watson 	return (au_to_attr32(vni));
321ca0716f5SRobert Watson }
322ca0716f5SRobert Watson #endif /* !(defined(_KERNEL) || defined(KERNEL) */
323ca0716f5SRobert Watson 
324ca0716f5SRobert Watson /*
325ca0716f5SRobert Watson  * token ID                1 byte
326ca0716f5SRobert Watson  * how to print            1 byte
327ca0716f5SRobert Watson  * basic unit              1 byte
328ca0716f5SRobert Watson  * unit count              1 byte
329ca0716f5SRobert Watson  * data items              (depends on basic unit)
330ca0716f5SRobert Watson  */
331ca0716f5SRobert Watson token_t *
au_to_data(char unit_print,char unit_type,char unit_count,const char * p)33252267f74SRobert Watson au_to_data(char unit_print, char unit_type, char unit_count, const char *p)
333ca0716f5SRobert Watson {
334ca0716f5SRobert Watson 	token_t *t;
335ca0716f5SRobert Watson 	u_char *dptr = NULL;
336ca0716f5SRobert Watson 	size_t datasize, totdata;
337ca0716f5SRobert Watson 
338ca0716f5SRobert Watson 	/* Determine the size of the basic unit. */
339ca0716f5SRobert Watson 	switch (unit_type) {
340ca0716f5SRobert Watson 	case AUR_BYTE:
341506764c6SRobert Watson 	/* case AUR_CHAR: */
342ca0716f5SRobert Watson 		datasize = AUR_BYTE_SIZE;
343ca0716f5SRobert Watson 		break;
344ca0716f5SRobert Watson 
345ca0716f5SRobert Watson 	case AUR_SHORT:
346ca0716f5SRobert Watson 		datasize = AUR_SHORT_SIZE;
347ca0716f5SRobert Watson 		break;
348ca0716f5SRobert Watson 
349506764c6SRobert Watson 	case AUR_INT32:
350506764c6SRobert Watson 	/* case AUR_INT: */
351506764c6SRobert Watson 		datasize = AUR_INT32_SIZE;
352506764c6SRobert Watson 		break;
353506764c6SRobert Watson 
354506764c6SRobert Watson 	case AUR_INT64:
355506764c6SRobert Watson 		datasize = AUR_INT64_SIZE;
356ca0716f5SRobert Watson 		break;
357ca0716f5SRobert Watson 
358ca0716f5SRobert Watson 	default:
359ca0716f5SRobert Watson 		errno = EINVAL;
360ca0716f5SRobert Watson 		return (NULL);
361ca0716f5SRobert Watson 	}
362ca0716f5SRobert Watson 
363ca0716f5SRobert Watson 	totdata = datasize * unit_count;
364ca0716f5SRobert Watson 
365506764c6SRobert Watson 	GET_TOKEN_AREA(t, dptr, 4 * sizeof(u_char) + totdata);
366ca0716f5SRobert Watson 	if (t == NULL)
367ca0716f5SRobert Watson 		return (NULL);
368ca0716f5SRobert Watson 
3697a0a89d2SRobert Watson 	/*
3707a0a89d2SRobert Watson 	 * XXXRW: We should be byte-swapping each data item for multi-byte
3717a0a89d2SRobert Watson 	 * types.
3727a0a89d2SRobert Watson 	 */
373ca0716f5SRobert Watson 	ADD_U_CHAR(dptr, AUT_DATA);
374ca0716f5SRobert Watson 	ADD_U_CHAR(dptr, unit_print);
375ca0716f5SRobert Watson 	ADD_U_CHAR(dptr, unit_type);
376ca0716f5SRobert Watson 	ADD_U_CHAR(dptr, unit_count);
377ca0716f5SRobert Watson 	ADD_MEM(dptr, p, totdata);
378ca0716f5SRobert Watson 
379ca0716f5SRobert Watson 	return (t);
380ca0716f5SRobert Watson }
381ca0716f5SRobert Watson 
382ca0716f5SRobert Watson 
383ca0716f5SRobert Watson /*
384ca0716f5SRobert Watson  * token ID                1 byte
385ca0716f5SRobert Watson  * status		   4 bytes
386ca0716f5SRobert Watson  * return value            4 bytes
387ca0716f5SRobert Watson  */
388ca0716f5SRobert Watson token_t *
au_to_exit(int retval,int err)389ca0716f5SRobert Watson au_to_exit(int retval, int err)
390ca0716f5SRobert Watson {
391ca0716f5SRobert Watson 	token_t *t;
392ca0716f5SRobert Watson 	u_char *dptr = NULL;
393ca0716f5SRobert Watson 
394ca0716f5SRobert Watson 	GET_TOKEN_AREA(t, dptr, sizeof(u_char) + 2 * sizeof(u_int32_t));
395ca0716f5SRobert Watson 	if (t == NULL)
396ca0716f5SRobert Watson 		return (NULL);
397ca0716f5SRobert Watson 
398ca0716f5SRobert Watson 	ADD_U_CHAR(dptr, AUT_EXIT);
399ca0716f5SRobert Watson 	ADD_U_INT32(dptr, err);
400ca0716f5SRobert Watson 	ADD_U_INT32(dptr, retval);
401ca0716f5SRobert Watson 
402ca0716f5SRobert Watson 	return (t);
403ca0716f5SRobert Watson }
404ca0716f5SRobert Watson 
405ca0716f5SRobert Watson /*
406ca0716f5SRobert Watson  */
407ca0716f5SRobert Watson token_t *
au_to_groups(int * groups)408ca0716f5SRobert Watson au_to_groups(int *groups)
409ca0716f5SRobert Watson {
410ca0716f5SRobert Watson 
411bc168a6cSRobert Watson 	return (au_to_newgroups(AUDIT_MAX_GROUPS, (gid_t *)groups));
412ca0716f5SRobert Watson }
413ca0716f5SRobert Watson 
414ca0716f5SRobert Watson /*
415ca0716f5SRobert Watson  * token ID                1 byte
416ca0716f5SRobert Watson  * number groups           2 bytes
417ca0716f5SRobert Watson  * group list              count * 4 bytes
418ca0716f5SRobert Watson  */
419ca0716f5SRobert Watson token_t *
au_to_newgroups(u_int16_t n,gid_t * groups)420ca0716f5SRobert Watson au_to_newgroups(u_int16_t n, gid_t *groups)
421ca0716f5SRobert Watson {
422ca0716f5SRobert Watson 	token_t *t;
423ca0716f5SRobert Watson 	u_char *dptr = NULL;
424ca0716f5SRobert Watson 	int i;
425ca0716f5SRobert Watson 
426ca0716f5SRobert Watson 	GET_TOKEN_AREA(t, dptr, sizeof(u_char) + sizeof(u_int16_t) +
427ca0716f5SRobert Watson 	    n * sizeof(u_int32_t));
428ca0716f5SRobert Watson 	if (t == NULL)
429ca0716f5SRobert Watson 		return (NULL);
430ca0716f5SRobert Watson 
431ca0716f5SRobert Watson 	ADD_U_CHAR(dptr, AUT_NEWGROUPS);
432ca0716f5SRobert Watson 	ADD_U_INT16(dptr, n);
433ca0716f5SRobert Watson 	for (i = 0; i < n; i++)
434ca0716f5SRobert Watson 		ADD_U_INT32(dptr, groups[i]);
435ca0716f5SRobert Watson 
436ca0716f5SRobert Watson 	return (t);
437ca0716f5SRobert Watson }
438ca0716f5SRobert Watson 
439ca0716f5SRobert Watson /*
440ca0716f5SRobert Watson  * token ID                1 byte
441ca0716f5SRobert Watson  * internet address        4 bytes
442ca0716f5SRobert Watson  */
443ca0716f5SRobert Watson token_t *
au_to_in_addr(struct in_addr * internet_addr)444ca0716f5SRobert Watson au_to_in_addr(struct in_addr *internet_addr)
445ca0716f5SRobert Watson {
446ca0716f5SRobert Watson 	token_t *t;
447ca0716f5SRobert Watson 	u_char *dptr = NULL;
448ca0716f5SRobert Watson 
449506764c6SRobert Watson 	GET_TOKEN_AREA(t, dptr, sizeof(u_char) + sizeof(uint32_t));
450ca0716f5SRobert Watson 	if (t == NULL)
451ca0716f5SRobert Watson 		return (NULL);
452ca0716f5SRobert Watson 
453ca0716f5SRobert Watson 	ADD_U_CHAR(dptr, AUT_IN_ADDR);
454506764c6SRobert Watson 	ADD_MEM(dptr, &internet_addr->s_addr, sizeof(uint32_t));
455ca0716f5SRobert Watson 
456ca0716f5SRobert Watson 	return (t);
457ca0716f5SRobert Watson }
458ca0716f5SRobert Watson 
459ca0716f5SRobert Watson /*
460ca0716f5SRobert Watson  * token ID                1 byte
461ca0716f5SRobert Watson  * address type/length     4 bytes
46252267f74SRobert Watson  * address                16 bytes
463ca0716f5SRobert Watson  */
464ca0716f5SRobert Watson token_t *
au_to_in_addr_ex(struct in6_addr * internet_addr)465ca0716f5SRobert Watson au_to_in_addr_ex(struct in6_addr *internet_addr)
466ca0716f5SRobert Watson {
467ca0716f5SRobert Watson 	token_t *t;
468ca0716f5SRobert Watson 	u_char *dptr = NULL;
4697a0a89d2SRobert Watson 	u_int32_t type = AU_IPv6;
470ca0716f5SRobert Watson 
471506764c6SRobert Watson 	GET_TOKEN_AREA(t, dptr, sizeof(u_char) + 5 * sizeof(uint32_t));
472ca0716f5SRobert Watson 	if (t == NULL)
473ca0716f5SRobert Watson 		return (NULL);
474ca0716f5SRobert Watson 
475ca0716f5SRobert Watson 	ADD_U_CHAR(dptr, AUT_IN_ADDR_EX);
476ca0716f5SRobert Watson 	ADD_U_INT32(dptr, type);
4770814440eSRobert Watson 	ADD_MEM(dptr, internet_addr, 4 * sizeof(uint32_t));
478ca0716f5SRobert Watson 
479ca0716f5SRobert Watson 	return (t);
480ca0716f5SRobert Watson }
481ca0716f5SRobert Watson 
482ca0716f5SRobert Watson /*
483ca0716f5SRobert Watson  * token ID                1 byte
484ca0716f5SRobert Watson  * ip header		   20 bytes
485bc168a6cSRobert Watson  *
486bc168a6cSRobert Watson  * The IP header should be submitted in network byte order.
487ca0716f5SRobert Watson  */
488ca0716f5SRobert Watson token_t *
au_to_ip(struct ip * ip)489ca0716f5SRobert Watson au_to_ip(struct ip *ip)
490ca0716f5SRobert Watson {
491ca0716f5SRobert Watson 	token_t *t;
492ca0716f5SRobert Watson 	u_char *dptr = NULL;
493ca0716f5SRobert Watson 
494ca0716f5SRobert Watson 	GET_TOKEN_AREA(t, dptr, sizeof(u_char) + sizeof(struct ip));
495ca0716f5SRobert Watson 	if (t == NULL)
496ca0716f5SRobert Watson 		return (NULL);
497ca0716f5SRobert Watson 
498ca0716f5SRobert Watson 	ADD_U_CHAR(dptr, AUT_IP);
499ca0716f5SRobert Watson 	ADD_MEM(dptr, ip, sizeof(struct ip));
500ca0716f5SRobert Watson 
501ca0716f5SRobert Watson 	return (t);
502ca0716f5SRobert Watson }
503ca0716f5SRobert Watson 
504ca0716f5SRobert Watson /*
505ca0716f5SRobert Watson  * token ID                1 byte
506ca0716f5SRobert Watson  * object ID type          1 byte
507ca0716f5SRobert Watson  * object ID               4 bytes
508ca0716f5SRobert Watson  */
509ca0716f5SRobert Watson token_t *
au_to_ipc(char type,int id)510ca0716f5SRobert Watson au_to_ipc(char type, int id)
511ca0716f5SRobert Watson {
512ca0716f5SRobert Watson 	token_t *t;
513ca0716f5SRobert Watson 	u_char *dptr = NULL;
514ca0716f5SRobert Watson 
515ca0716f5SRobert Watson 	GET_TOKEN_AREA(t, dptr, 2 * sizeof(u_char) + sizeof(u_int32_t));
516ca0716f5SRobert Watson 	if (t == NULL)
517ca0716f5SRobert Watson 		return (NULL);
518ca0716f5SRobert Watson 
519ca0716f5SRobert Watson 	ADD_U_CHAR(dptr, AUT_IPC);
520ca0716f5SRobert Watson 	ADD_U_CHAR(dptr, type);
521ca0716f5SRobert Watson 	ADD_U_INT32(dptr, id);
522ca0716f5SRobert Watson 
523ca0716f5SRobert Watson 	return (t);
524ca0716f5SRobert Watson }
525ca0716f5SRobert Watson 
526ca0716f5SRobert Watson /*
527ca0716f5SRobert Watson  * token ID                1 byte
528ca0716f5SRobert Watson  * owner user ID           4 bytes
529ca0716f5SRobert Watson  * owner group ID          4 bytes
530ca0716f5SRobert Watson  * creator user ID         4 bytes
531ca0716f5SRobert Watson  * creator group ID        4 bytes
532ca0716f5SRobert Watson  * access mode             4 bytes
533ca0716f5SRobert Watson  * slot sequence #         4 bytes
534ca0716f5SRobert Watson  * key                     4 bytes
535ca0716f5SRobert Watson  */
536ca0716f5SRobert Watson token_t *
au_to_ipc_perm(struct ipc_perm * perm)537ca0716f5SRobert Watson au_to_ipc_perm(struct ipc_perm *perm)
538ca0716f5SRobert Watson {
539ca0716f5SRobert Watson 	token_t *t;
540ca0716f5SRobert Watson 	u_char *dptr = NULL;
541ca0716f5SRobert Watson 	u_int16_t pad0 = 0;
542ca0716f5SRobert Watson 
54306edd2f1SRobert Watson 	GET_TOKEN_AREA(t, dptr, sizeof(u_char) + 12 * sizeof(u_int16_t) +
54406edd2f1SRobert Watson 	    sizeof(u_int32_t));
545ca0716f5SRobert Watson 	if (t == NULL)
546ca0716f5SRobert Watson 		return (NULL);
547ca0716f5SRobert Watson 
548ca0716f5SRobert Watson 	ADD_U_CHAR(dptr, AUT_IPC_PERM);
549ca0716f5SRobert Watson 
550ca0716f5SRobert Watson 	/*
5517a0a89d2SRobert Watson 	 * Systems vary significantly in what types they use in struct
5527a0a89d2SRobert Watson 	 * ipc_perm; at least a few still use 16-bit uid's and gid's, so
5537a0a89d2SRobert Watson 	 * allow for that, as BSM define 32-bit values here.
5547a0a89d2SRobert Watson 	 * Some systems define the sizes for ipc_perm members as 2 bytes;
5557a0a89d2SRobert Watson 	 * BSM defines 4 so pad with 0.
5567a0a89d2SRobert Watson 	 *
5577a0a89d2SRobert Watson 	 * XXXRW: Possibly shoulid be conditionally compiled, and more cases
5587a0a89d2SRobert Watson 	 * need to be handled.
559ca0716f5SRobert Watson 	 */
5607a0a89d2SRobert Watson 	if (sizeof(perm->uid) != sizeof(u_int32_t)) {
561ca0716f5SRobert Watson 		ADD_U_INT16(dptr, pad0);
562ca0716f5SRobert Watson 		ADD_U_INT16(dptr, perm->uid);
563ca0716f5SRobert Watson 		ADD_U_INT16(dptr, pad0);
564ca0716f5SRobert Watson 		ADD_U_INT16(dptr, perm->gid);
565ca0716f5SRobert Watson 		ADD_U_INT16(dptr, pad0);
566ca0716f5SRobert Watson 		ADD_U_INT16(dptr, perm->cuid);
567ca0716f5SRobert Watson 		ADD_U_INT16(dptr, pad0);
568ca0716f5SRobert Watson 		ADD_U_INT16(dptr, perm->cgid);
5697a0a89d2SRobert Watson 	} else {
5707a0a89d2SRobert Watson 		ADD_U_INT32(dptr, perm->uid);
5717a0a89d2SRobert Watson 		ADD_U_INT32(dptr, perm->gid);
5727a0a89d2SRobert Watson 		ADD_U_INT32(dptr, perm->cuid);
5737a0a89d2SRobert Watson 		ADD_U_INT32(dptr, perm->cgid);
5747a0a89d2SRobert Watson 	}
575ca0716f5SRobert Watson 
576ca0716f5SRobert Watson 	ADD_U_INT16(dptr, pad0);
577ca0716f5SRobert Watson 	ADD_U_INT16(dptr, perm->mode);
578ca0716f5SRobert Watson 
579ca0716f5SRobert Watson 	ADD_U_INT16(dptr, pad0);
580ca0716f5SRobert Watson 
5813b97a967SRobert Watson #ifdef HAVE_IPC_PERM___SEQ
5823b97a967SRobert Watson 	ADD_U_INT16(dptr, perm->__seq);
58352267f74SRobert Watson #else	/* HAVE_IPC_PERM___SEQ */
58452267f74SRobert Watson #ifdef  HAVE_IPC_PERM__SEQ
58552267f74SRobert Watson 	ADD_U_INT16(dptr, perm->_seq);
58652267f74SRobert Watson #else	/* HAVE_IPC_PERM__SEQ */
5873b97a967SRobert Watson 	ADD_U_INT16(dptr, perm->seq);
58852267f74SRobert Watson #endif	/* HAVE_IPC_PERM__SEQ */
58952267f74SRobert Watson #endif	/* HAVE_IPC_PERM___SEQ */
5903b97a967SRobert Watson 
5913b97a967SRobert Watson #ifdef HAVE_IPC_PERM___KEY
5923b97a967SRobert Watson 	ADD_U_INT32(dptr, perm->__key);
59352267f74SRobert Watson #else	/* HAVE_IPC_PERM___KEY */
59452267f74SRobert Watson #ifdef  HAVE_IPC_PERM__KEY
59552267f74SRobert Watson 	ADD_U_INT32(dptr, perm->_key);
59652267f74SRobert Watson #else	/* HAVE_IPC_PERM__KEY */
597ca0716f5SRobert Watson 	ADD_U_INT32(dptr, perm->key);
59852267f74SRobert Watson #endif	/* HAVE_IPC_PERM__KEY */
59952267f74SRobert Watson #endif	/* HAVE_IPC_PERM___KEY */
600ca0716f5SRobert Watson 
601ca0716f5SRobert Watson 	return (t);
602ca0716f5SRobert Watson }
603ca0716f5SRobert Watson 
604ca0716f5SRobert Watson /*
605ca0716f5SRobert Watson  * token ID                1 byte
606ca0716f5SRobert Watson  * port IP address         2 bytes
607ca0716f5SRobert Watson  */
608ca0716f5SRobert Watson token_t *
au_to_iport(u_int16_t iport)609ca0716f5SRobert Watson au_to_iport(u_int16_t iport)
610ca0716f5SRobert Watson {
611ca0716f5SRobert Watson 	token_t *t;
612ca0716f5SRobert Watson 	u_char *dptr = NULL;
613ca0716f5SRobert Watson 
614ca0716f5SRobert Watson 	GET_TOKEN_AREA(t, dptr, sizeof(u_char) + sizeof(u_int16_t));
615ca0716f5SRobert Watson 	if (t == NULL)
616ca0716f5SRobert Watson 		return (NULL);
617ca0716f5SRobert Watson 
618ca0716f5SRobert Watson 	ADD_U_CHAR(dptr, AUT_IPORT);
619ca0716f5SRobert Watson 	ADD_U_INT16(dptr, iport);
620ca0716f5SRobert Watson 
621ca0716f5SRobert Watson 	return (t);
622ca0716f5SRobert Watson }
623ca0716f5SRobert Watson 
624ca0716f5SRobert Watson /*
625ca0716f5SRobert Watson  * token ID                1 byte
626ca0716f5SRobert Watson  * size                    2 bytes
627ca0716f5SRobert Watson  * data                    size bytes
628ca0716f5SRobert Watson  */
629ca0716f5SRobert Watson token_t *
au_to_opaque(const char * data,u_int16_t bytes)63052267f74SRobert Watson au_to_opaque(const char *data, u_int16_t bytes)
631ca0716f5SRobert Watson {
632ca0716f5SRobert Watson 	token_t *t;
633ca0716f5SRobert Watson 	u_char *dptr = NULL;
634ca0716f5SRobert Watson 
635ca0716f5SRobert Watson 	GET_TOKEN_AREA(t, dptr, sizeof(u_char) + sizeof(u_int16_t) + bytes);
636ca0716f5SRobert Watson 	if (t == NULL)
637ca0716f5SRobert Watson 		return (NULL);
638ca0716f5SRobert Watson 
639ca0716f5SRobert Watson 	ADD_U_CHAR(dptr, AUT_OPAQUE);
640ca0716f5SRobert Watson 	ADD_U_INT16(dptr, bytes);
641ca0716f5SRobert Watson 	ADD_MEM(dptr, data, bytes);
642ca0716f5SRobert Watson 
643ca0716f5SRobert Watson 	return (t);
644ca0716f5SRobert Watson }
645ca0716f5SRobert Watson 
646ca0716f5SRobert Watson /*
647ca0716f5SRobert Watson  * token ID                1 byte
648ca0716f5SRobert Watson  * seconds of time         4 bytes
649ca0716f5SRobert Watson  * milliseconds of time    4 bytes
650ca0716f5SRobert Watson  * file name len           2 bytes
651ca0716f5SRobert Watson  * file pathname           N bytes + 1 terminating NULL byte
652ca0716f5SRobert Watson  */
653ca0716f5SRobert Watson token_t *
au_to_file(const char * file,struct timeval tm)65452267f74SRobert Watson au_to_file(const char *file, struct timeval tm)
655ca0716f5SRobert Watson {
656ca0716f5SRobert Watson 	token_t *t;
657ca0716f5SRobert Watson 	u_char *dptr = NULL;
658ca0716f5SRobert Watson 	u_int16_t filelen;
659ca0716f5SRobert Watson 	u_int32_t timems;
660ca0716f5SRobert Watson 
661ca0716f5SRobert Watson 	filelen = strlen(file);
662ca0716f5SRobert Watson 	filelen += 1;
663ca0716f5SRobert Watson 
664ca0716f5SRobert Watson 	GET_TOKEN_AREA(t, dptr, sizeof(u_char) + 2 * sizeof(u_int32_t) +
665ca0716f5SRobert Watson 	    sizeof(u_int16_t) + filelen);
666ca0716f5SRobert Watson 	if (t == NULL)
667ca0716f5SRobert Watson 		return (NULL);
668ca0716f5SRobert Watson 
669ca0716f5SRobert Watson 	timems = tm.tv_usec/1000;
670ca0716f5SRobert Watson 
671ca0716f5SRobert Watson 	ADD_U_CHAR(dptr, AUT_OTHER_FILE32);
672ca0716f5SRobert Watson 	ADD_U_INT32(dptr, tm.tv_sec);
673ca0716f5SRobert Watson 	ADD_U_INT32(dptr, timems);	/* We need time in ms. */
674ca0716f5SRobert Watson 	ADD_U_INT16(dptr, filelen);
675ca0716f5SRobert Watson 	ADD_STRING(dptr, file, filelen);
676ca0716f5SRobert Watson 
677ca0716f5SRobert Watson 	return (t);
678ca0716f5SRobert Watson }
679ca0716f5SRobert Watson 
680ca0716f5SRobert Watson /*
681ca0716f5SRobert Watson  * token ID                1 byte
682ca0716f5SRobert Watson  * text length             2 bytes
683ca0716f5SRobert Watson  * text                    N bytes + 1 terminating NULL byte
684ca0716f5SRobert Watson  */
685ca0716f5SRobert Watson token_t *
au_to_text(const char * text)68652267f74SRobert Watson au_to_text(const char *text)
687ca0716f5SRobert Watson {
688ca0716f5SRobert Watson 	token_t *t;
689ca0716f5SRobert Watson 	u_char *dptr = NULL;
690ca0716f5SRobert Watson 	u_int16_t textlen;
691ca0716f5SRobert Watson 
692ca0716f5SRobert Watson 	textlen = strlen(text);
693ca0716f5SRobert Watson 	textlen += 1;
694ca0716f5SRobert Watson 
6957a0a89d2SRobert Watson 	/* XXXRW: Should validate length against token size limit. */
6967a0a89d2SRobert Watson 
697ca0716f5SRobert Watson 	GET_TOKEN_AREA(t, dptr, sizeof(u_char) + sizeof(u_int16_t) + textlen);
698ca0716f5SRobert Watson 	if (t == NULL)
699ca0716f5SRobert Watson 		return (NULL);
700ca0716f5SRobert Watson 
701ca0716f5SRobert Watson 	ADD_U_CHAR(dptr, AUT_TEXT);
702ca0716f5SRobert Watson 	ADD_U_INT16(dptr, textlen);
703ca0716f5SRobert Watson 	ADD_STRING(dptr, text, textlen);
704ca0716f5SRobert Watson 
705ca0716f5SRobert Watson 	return (t);
706ca0716f5SRobert Watson }
707ca0716f5SRobert Watson 
708ca0716f5SRobert Watson /*
709ca0716f5SRobert Watson  * token ID                1 byte
710ca0716f5SRobert Watson  * path length             2 bytes
711ca0716f5SRobert Watson  * path                    N bytes + 1 terminating NULL byte
712ca0716f5SRobert Watson  */
713ca0716f5SRobert Watson token_t *
au_to_path(const char * text)71452267f74SRobert Watson au_to_path(const char *text)
715ca0716f5SRobert Watson {
716ca0716f5SRobert Watson 	token_t *t;
717ca0716f5SRobert Watson 	u_char *dptr = NULL;
718ca0716f5SRobert Watson 	u_int16_t textlen;
719ca0716f5SRobert Watson 
720ca0716f5SRobert Watson 	textlen = strlen(text);
721ca0716f5SRobert Watson 	textlen += 1;
722ca0716f5SRobert Watson 
723ca0716f5SRobert Watson 	GET_TOKEN_AREA(t, dptr, sizeof(u_char) + sizeof(u_int16_t) + textlen);
724ca0716f5SRobert Watson 	if (t == NULL)
725ca0716f5SRobert Watson 		return (NULL);
726ca0716f5SRobert Watson 
727ca0716f5SRobert Watson 	ADD_U_CHAR(dptr, AUT_PATH);
728ca0716f5SRobert Watson 	ADD_U_INT16(dptr, textlen);
729ca0716f5SRobert Watson 	ADD_STRING(dptr, text, textlen);
730ca0716f5SRobert Watson 
731ca0716f5SRobert Watson 	return (t);
732ca0716f5SRobert Watson }
733ca0716f5SRobert Watson 
734ca0716f5SRobert Watson /*
735ca0716f5SRobert Watson  * token ID                1 byte
736ca0716f5SRobert Watson  * audit ID                4 bytes
737ca0716f5SRobert Watson  * effective user ID       4 bytes
738ca0716f5SRobert Watson  * effective group ID      4 bytes
739ca0716f5SRobert Watson  * real user ID            4 bytes
740ca0716f5SRobert Watson  * real group ID           4 bytes
741ca0716f5SRobert Watson  * process ID              4 bytes
742ca0716f5SRobert Watson  * session ID              4 bytes
743ca0716f5SRobert Watson  * terminal ID
744ca0716f5SRobert Watson  *   port ID               4 bytes/8 bytes (32-bit/64-bit value)
745ca0716f5SRobert Watson  *   machine address       4 bytes
746ca0716f5SRobert Watson  */
747ca0716f5SRobert Watson token_t *
au_to_process32(au_id_t auid,uid_t euid,gid_t egid,uid_t ruid,gid_t rgid,pid_t pid,au_asid_t sid,au_tid_t * tid)748ca0716f5SRobert Watson au_to_process32(au_id_t auid, uid_t euid, gid_t egid, uid_t ruid, gid_t rgid,
749ca0716f5SRobert Watson     pid_t pid, au_asid_t sid, au_tid_t *tid)
750ca0716f5SRobert Watson {
751ca0716f5SRobert Watson 	token_t *t;
752ca0716f5SRobert Watson 	u_char *dptr = NULL;
753ca0716f5SRobert Watson 
754ca0716f5SRobert Watson 	GET_TOKEN_AREA(t, dptr, sizeof(u_char) + 9 * sizeof(u_int32_t));
755ca0716f5SRobert Watson 	if (t == NULL)
756ca0716f5SRobert Watson 		return (NULL);
757ca0716f5SRobert Watson 
758ca0716f5SRobert Watson 	ADD_U_CHAR(dptr, AUT_PROCESS32);
759ca0716f5SRobert Watson 	ADD_U_INT32(dptr, auid);
760ca0716f5SRobert Watson 	ADD_U_INT32(dptr, euid);
761ca0716f5SRobert Watson 	ADD_U_INT32(dptr, egid);
762ca0716f5SRobert Watson 	ADD_U_INT32(dptr, ruid);
763ca0716f5SRobert Watson 	ADD_U_INT32(dptr, rgid);
764ca0716f5SRobert Watson 	ADD_U_INT32(dptr, pid);
765ca0716f5SRobert Watson 	ADD_U_INT32(dptr, sid);
766ca0716f5SRobert Watson 	ADD_U_INT32(dptr, tid->port);
7677a0a89d2SRobert Watson 
7687a0a89d2SRobert Watson 	/*
7697a0a89d2SRobert Watson 	 * Note: Solaris will write out IPv6 addresses here as a 32-bit
7707a0a89d2SRobert Watson 	 * address type and 16 bytes of address, but for IPv4 addresses it
7717a0a89d2SRobert Watson 	 * simply writes the 4-byte address directly.  We support only IPv4
7727a0a89d2SRobert Watson 	 * addresses for process32 tokens.
7737a0a89d2SRobert Watson 	 */
774506764c6SRobert Watson 	ADD_MEM(dptr, &tid->machine, sizeof(u_int32_t));
775ca0716f5SRobert Watson 
776ca0716f5SRobert Watson 	return (t);
777ca0716f5SRobert Watson }
778ca0716f5SRobert Watson 
779ca0716f5SRobert Watson token_t *
au_to_process64(au_id_t auid,uid_t euid,gid_t egid,uid_t ruid,gid_t rgid,pid_t pid,au_asid_t sid,au_tid_t * tid)780bc168a6cSRobert Watson au_to_process64(au_id_t auid, uid_t euid, gid_t egid, uid_t ruid, gid_t rgid,
781bc168a6cSRobert Watson     pid_t pid, au_asid_t sid, au_tid_t *tid)
782ca0716f5SRobert Watson {
783bc168a6cSRobert Watson 	token_t *t;
784bc168a6cSRobert Watson 	u_char *dptr = NULL;
785ca0716f5SRobert Watson 
786bc168a6cSRobert Watson 	GET_TOKEN_AREA(t, dptr, sizeof(u_char) + 8 * sizeof(u_int32_t) +
787bc168a6cSRobert Watson 	    sizeof(u_int64_t));
788bc168a6cSRobert Watson 	if (t == NULL)
789ca0716f5SRobert Watson 		return (NULL);
790bc168a6cSRobert Watson 
791bc168a6cSRobert Watson 	ADD_U_CHAR(dptr, AUT_PROCESS64);
792bc168a6cSRobert Watson 	ADD_U_INT32(dptr, auid);
793bc168a6cSRobert Watson 	ADD_U_INT32(dptr, euid);
794bc168a6cSRobert Watson 	ADD_U_INT32(dptr, egid);
795bc168a6cSRobert Watson 	ADD_U_INT32(dptr, ruid);
796bc168a6cSRobert Watson 	ADD_U_INT32(dptr, rgid);
797bc168a6cSRobert Watson 	ADD_U_INT32(dptr, pid);
798bc168a6cSRobert Watson 	ADD_U_INT32(dptr, sid);
799bc168a6cSRobert Watson 	ADD_U_INT64(dptr, tid->port);
8007a0a89d2SRobert Watson 
8017a0a89d2SRobert Watson 	/*
8027a0a89d2SRobert Watson 	 * Note: Solaris will write out IPv6 addresses here as a 32-bit
8037a0a89d2SRobert Watson 	 * address type and 16 bytes of address, but for IPv4 addresses it
8047a0a89d2SRobert Watson 	 * simply writes the 4-byte address directly.  We support only IPv4
8057a0a89d2SRobert Watson 	 * addresses for process64 tokens.
8067a0a89d2SRobert Watson 	 */
807bc168a6cSRobert Watson 	ADD_MEM(dptr, &tid->machine, sizeof(u_int32_t));
808bc168a6cSRobert Watson 
809bc168a6cSRobert Watson 	return (t);
810ca0716f5SRobert Watson }
811ca0716f5SRobert Watson 
812ca0716f5SRobert Watson token_t *
au_to_process(au_id_t auid,uid_t euid,gid_t egid,uid_t ruid,gid_t rgid,pid_t pid,au_asid_t sid,au_tid_t * tid)813bc168a6cSRobert Watson au_to_process(au_id_t auid, uid_t euid, gid_t egid, uid_t ruid, gid_t rgid,
814bc168a6cSRobert Watson     pid_t pid, au_asid_t sid, au_tid_t *tid)
815ca0716f5SRobert Watson {
816ca0716f5SRobert Watson 
817ca0716f5SRobert Watson 	return (au_to_process32(auid, euid, egid, ruid, rgid, pid, sid,
818ca0716f5SRobert Watson 	    tid));
819ca0716f5SRobert Watson }
820ca0716f5SRobert Watson 
821ca0716f5SRobert Watson /*
822ca0716f5SRobert Watson  * token ID                1 byte
823ca0716f5SRobert Watson  * audit ID                4 bytes
824ca0716f5SRobert Watson  * effective user ID       4 bytes
825ca0716f5SRobert Watson  * effective group ID      4 bytes
826ca0716f5SRobert Watson  * real user ID            4 bytes
827ca0716f5SRobert Watson  * real group ID           4 bytes
828ca0716f5SRobert Watson  * process ID              4 bytes
829ca0716f5SRobert Watson  * session ID              4 bytes
830ca0716f5SRobert Watson  * terminal ID
831ca0716f5SRobert Watson  *   port ID               4 bytes/8 bytes (32-bit/64-bit value)
832ca0716f5SRobert Watson  *   address type-len      4 bytes
833ca0716f5SRobert Watson  *   machine address      16 bytes
834ca0716f5SRobert Watson  */
835ca0716f5SRobert Watson token_t *
au_to_process32_ex(au_id_t auid,uid_t euid,gid_t egid,uid_t ruid,gid_t rgid,pid_t pid,au_asid_t sid,au_tid_addr_t * tid)836ca0716f5SRobert Watson au_to_process32_ex(au_id_t auid, uid_t euid, gid_t egid, uid_t ruid,
837ca0716f5SRobert Watson     gid_t rgid, pid_t pid, au_asid_t sid, au_tid_addr_t *tid)
838ca0716f5SRobert Watson {
839ca0716f5SRobert Watson 	token_t *t;
840ca0716f5SRobert Watson 	u_char *dptr = NULL;
841ca0716f5SRobert Watson 
842d9af45c4SRobert Watson 	if (tid->at_type == AU_IPv4)
843d9af45c4SRobert Watson 		GET_TOKEN_AREA(t, dptr, sizeof(u_char) +
844d9af45c4SRobert Watson 		    10 * sizeof(u_int32_t));
845d9af45c4SRobert Watson 	else if (tid->at_type == AU_IPv6)
846d9af45c4SRobert Watson 		GET_TOKEN_AREA(t, dptr, sizeof(u_char) +
847d9af45c4SRobert Watson 		    13 * sizeof(u_int32_t));
848d9af45c4SRobert Watson 	else {
849d9af45c4SRobert Watson 		errno = EINVAL;
850d9af45c4SRobert Watson 		return (NULL);
851d9af45c4SRobert Watson 	}
852ca0716f5SRobert Watson 	if (t == NULL)
853ca0716f5SRobert Watson 		return (NULL);
854ca0716f5SRobert Watson 
855ca0716f5SRobert Watson 	ADD_U_CHAR(dptr, AUT_PROCESS32_EX);
856ca0716f5SRobert Watson 	ADD_U_INT32(dptr, auid);
857ca0716f5SRobert Watson 	ADD_U_INT32(dptr, euid);
858ca0716f5SRobert Watson 	ADD_U_INT32(dptr, egid);
859ca0716f5SRobert Watson 	ADD_U_INT32(dptr, ruid);
860ca0716f5SRobert Watson 	ADD_U_INT32(dptr, rgid);
861ca0716f5SRobert Watson 	ADD_U_INT32(dptr, pid);
862ca0716f5SRobert Watson 	ADD_U_INT32(dptr, sid);
863ca0716f5SRobert Watson 	ADD_U_INT32(dptr, tid->at_port);
864ca0716f5SRobert Watson 	ADD_U_INT32(dptr, tid->at_type);
865bc168a6cSRobert Watson 	ADD_MEM(dptr, &tid->at_addr[0], sizeof(u_int32_t));
866d9af45c4SRobert Watson 	if (tid->at_type == AU_IPv6) {
867bc168a6cSRobert Watson 		ADD_MEM(dptr, &tid->at_addr[1], sizeof(u_int32_t));
868bc168a6cSRobert Watson 		ADD_MEM(dptr, &tid->at_addr[2], sizeof(u_int32_t));
869bc168a6cSRobert Watson 		ADD_MEM(dptr, &tid->at_addr[3], sizeof(u_int32_t));
870d9af45c4SRobert Watson 	}
871ca0716f5SRobert Watson 
872ca0716f5SRobert Watson 	return (t);
873ca0716f5SRobert Watson }
874ca0716f5SRobert Watson 
875ca0716f5SRobert Watson token_t *
au_to_process64_ex(au_id_t auid,uid_t euid,gid_t egid,uid_t ruid,gid_t rgid,pid_t pid,au_asid_t sid,au_tid_addr_t * tid)876ca0716f5SRobert Watson au_to_process64_ex(au_id_t auid, uid_t euid, gid_t egid, uid_t ruid,
877ca0716f5SRobert Watson     gid_t rgid, pid_t pid, au_asid_t sid, au_tid_addr_t *tid)
878ca0716f5SRobert Watson {
879bc168a6cSRobert Watson 	token_t *t;
880bc168a6cSRobert Watson 	u_char *dptr = NULL;
881ca0716f5SRobert Watson 
882bc168a6cSRobert Watson 	if (tid->at_type == AU_IPv4)
883bc168a6cSRobert Watson 		GET_TOKEN_AREA(t, dptr, sizeof(u_char) +
884bc168a6cSRobert Watson 		    7 * sizeof(u_int32_t) + sizeof(u_int64_t) +
885bc168a6cSRobert Watson 		    2 * sizeof(u_int32_t));
886bc168a6cSRobert Watson 	else if (tid->at_type == AU_IPv6)
887bc168a6cSRobert Watson 		GET_TOKEN_AREA(t, dptr, sizeof(u_char) +
888bc168a6cSRobert Watson 		    7 * sizeof(u_int32_t) + sizeof(u_int64_t) +
889bc168a6cSRobert Watson 		    5 * sizeof(u_int32_t));
890bc168a6cSRobert Watson 	else {
891bc168a6cSRobert Watson 		errno = EINVAL;
892ca0716f5SRobert Watson 		return (NULL);
893ca0716f5SRobert Watson 	}
894bc168a6cSRobert Watson 	if (t == NULL)
895bc168a6cSRobert Watson 		return (NULL);
896bc168a6cSRobert Watson 
897bc168a6cSRobert Watson 	ADD_U_CHAR(dptr, AUT_PROCESS64_EX);
898bc168a6cSRobert Watson 	ADD_U_INT32(dptr, auid);
899bc168a6cSRobert Watson 	ADD_U_INT32(dptr, euid);
900bc168a6cSRobert Watson 	ADD_U_INT32(dptr, egid);
901bc168a6cSRobert Watson 	ADD_U_INT32(dptr, ruid);
902bc168a6cSRobert Watson 	ADD_U_INT32(dptr, rgid);
903bc168a6cSRobert Watson 	ADD_U_INT32(dptr, pid);
904bc168a6cSRobert Watson 	ADD_U_INT32(dptr, sid);
905bc168a6cSRobert Watson 	ADD_U_INT64(dptr, tid->at_port);
906bc168a6cSRobert Watson 	ADD_U_INT32(dptr, tid->at_type);
907bc168a6cSRobert Watson 	ADD_MEM(dptr, &tid->at_addr[0], sizeof(u_int32_t));
908bc168a6cSRobert Watson 	if (tid->at_type == AU_IPv6) {
909bc168a6cSRobert Watson 		ADD_MEM(dptr, &tid->at_addr[1], sizeof(u_int32_t));
910bc168a6cSRobert Watson 		ADD_MEM(dptr, &tid->at_addr[2], sizeof(u_int32_t));
911bc168a6cSRobert Watson 		ADD_MEM(dptr, &tid->at_addr[3], sizeof(u_int32_t));
912bc168a6cSRobert Watson 	}
913bc168a6cSRobert Watson 
914bc168a6cSRobert Watson 	return (t);
915bc168a6cSRobert Watson }
916ca0716f5SRobert Watson 
917ca0716f5SRobert Watson token_t *
au_to_process_ex(au_id_t auid,uid_t euid,gid_t egid,uid_t ruid,gid_t rgid,pid_t pid,au_asid_t sid,au_tid_addr_t * tid)918ca0716f5SRobert Watson au_to_process_ex(au_id_t auid, uid_t euid, gid_t egid, uid_t ruid,
919ca0716f5SRobert Watson     gid_t rgid, pid_t pid, au_asid_t sid, au_tid_addr_t *tid)
920ca0716f5SRobert Watson {
921ca0716f5SRobert Watson 
922ca0716f5SRobert Watson 	return (au_to_process32_ex(auid, euid, egid, ruid, rgid, pid, sid,
923ca0716f5SRobert Watson 	    tid));
924ca0716f5SRobert Watson }
925ca0716f5SRobert Watson 
926ca0716f5SRobert Watson /*
927ca0716f5SRobert Watson  * token ID                1 byte
928ca0716f5SRobert Watson  * error status            1 byte
929ca0716f5SRobert Watson  * return value            4 bytes/8 bytes (32-bit/64-bit value)
930ca0716f5SRobert Watson  */
931ca0716f5SRobert Watson token_t *
au_to_return32(char status,u_int32_t ret)932ca0716f5SRobert Watson au_to_return32(char status, u_int32_t ret)
933ca0716f5SRobert Watson {
934ca0716f5SRobert Watson 	token_t *t;
935ca0716f5SRobert Watson 	u_char *dptr = NULL;
936ca0716f5SRobert Watson 
937ca0716f5SRobert Watson 	GET_TOKEN_AREA(t, dptr, 2 * sizeof(u_char) + sizeof(u_int32_t));
938ca0716f5SRobert Watson 	if (t == NULL)
939ca0716f5SRobert Watson 		return (NULL);
940ca0716f5SRobert Watson 
941ca0716f5SRobert Watson 	ADD_U_CHAR(dptr, AUT_RETURN32);
942ca0716f5SRobert Watson 	ADD_U_CHAR(dptr, status);
943ca0716f5SRobert Watson 	ADD_U_INT32(dptr, ret);
944ca0716f5SRobert Watson 
945ca0716f5SRobert Watson 	return (t);
946ca0716f5SRobert Watson }
947ca0716f5SRobert Watson 
948ca0716f5SRobert Watson token_t *
au_to_return64(char status,u_int64_t ret)949ca0716f5SRobert Watson au_to_return64(char status, u_int64_t ret)
950ca0716f5SRobert Watson {
951ca0716f5SRobert Watson 	token_t *t;
952ca0716f5SRobert Watson 	u_char *dptr = NULL;
953ca0716f5SRobert Watson 
954ca0716f5SRobert Watson 	GET_TOKEN_AREA(t, dptr, 2 * sizeof(u_char) + sizeof(u_int64_t));
955ca0716f5SRobert Watson 	if (t == NULL)
956ca0716f5SRobert Watson 		return (NULL);
957ca0716f5SRobert Watson 
958ca0716f5SRobert Watson 	ADD_U_CHAR(dptr, AUT_RETURN64);
959ca0716f5SRobert Watson 	ADD_U_CHAR(dptr, status);
960ca0716f5SRobert Watson 	ADD_U_INT64(dptr, ret);
961ca0716f5SRobert Watson 
962ca0716f5SRobert Watson 	return (t);
963ca0716f5SRobert Watson }
964ca0716f5SRobert Watson 
965ca0716f5SRobert Watson token_t *
au_to_return(char status,u_int32_t ret)966ca0716f5SRobert Watson au_to_return(char status, u_int32_t ret)
967ca0716f5SRobert Watson {
968ca0716f5SRobert Watson 
969ca0716f5SRobert Watson 	return (au_to_return32(status, ret));
970ca0716f5SRobert Watson }
971ca0716f5SRobert Watson 
972ca0716f5SRobert Watson /*
973ca0716f5SRobert Watson  * token ID                1 byte
974ca0716f5SRobert Watson  * sequence number         4 bytes
975ca0716f5SRobert Watson  */
976ca0716f5SRobert Watson token_t *
au_to_seq(long audit_count)977ca0716f5SRobert Watson au_to_seq(long audit_count)
978ca0716f5SRobert Watson {
979ca0716f5SRobert Watson 	token_t *t;
980ca0716f5SRobert Watson 	u_char *dptr = NULL;
981ca0716f5SRobert Watson 
982ca0716f5SRobert Watson 	GET_TOKEN_AREA(t, dptr, sizeof(u_char) + sizeof(u_int32_t));
983ca0716f5SRobert Watson 	if (t == NULL)
984ca0716f5SRobert Watson 		return (NULL);
985ca0716f5SRobert Watson 
986ca0716f5SRobert Watson 	ADD_U_CHAR(dptr, AUT_SEQ);
987ca0716f5SRobert Watson 	ADD_U_INT32(dptr, audit_count);
988ca0716f5SRobert Watson 
989ca0716f5SRobert Watson 	return (t);
990ca0716f5SRobert Watson }
991ca0716f5SRobert Watson 
992ca0716f5SRobert Watson /*
993ca0716f5SRobert Watson  * token ID                1 byte
9947a0a89d2SRobert Watson  * socket domain           2 bytes
9957a0a89d2SRobert Watson  * socket type             2 bytes
9967a0a89d2SRobert Watson  * address type            2 byte
9977a0a89d2SRobert Watson  * local port              2 bytes
9987a0a89d2SRobert Watson  * local address           4 bytes/16 bytes (IPv4/IPv6 address)
9997a0a89d2SRobert Watson  * remote port             2 bytes
10007a0a89d2SRobert Watson  * remote address          4 bytes/16 bytes (IPv4/IPv6 address)
1001c74c7b73SRobert Watson  *
1002c74c7b73SRobert Watson  * Domain and type arguments to this routine are assumed to already have been
1003c74c7b73SRobert Watson  * converted to the BSM constant space, so we don't do that here.
10047a0a89d2SRobert Watson  */
10057a0a89d2SRobert Watson token_t *
au_to_socket_ex(u_short so_domain,u_short so_type,struct sockaddr * sa_local,struct sockaddr * sa_remote)10067a0a89d2SRobert Watson au_to_socket_ex(u_short so_domain, u_short so_type,
10077a0a89d2SRobert Watson     struct sockaddr *sa_local, struct sockaddr *sa_remote)
10087a0a89d2SRobert Watson {
10097a0a89d2SRobert Watson 	token_t *t;
10107a0a89d2SRobert Watson 	u_char *dptr = NULL;
10117a0a89d2SRobert Watson 	struct sockaddr_in *sin;
10127a0a89d2SRobert Watson 	struct sockaddr_in6 *sin6;
10137a0a89d2SRobert Watson 
10147a0a89d2SRobert Watson 	if (so_domain == AF_INET)
10157a0a89d2SRobert Watson 		GET_TOKEN_AREA(t, dptr, sizeof(u_char) +
10167a0a89d2SRobert Watson 		    5 * sizeof(u_int16_t) + 2 * sizeof(u_int32_t));
10177a0a89d2SRobert Watson 	else if (so_domain == AF_INET6)
10187a0a89d2SRobert Watson 		GET_TOKEN_AREA(t, dptr, sizeof(u_char) +
101906edd2f1SRobert Watson 		    5 * sizeof(u_int16_t) + 8 * sizeof(u_int32_t));
10207a0a89d2SRobert Watson 	else {
10217a0a89d2SRobert Watson 		errno = EINVAL;
10227a0a89d2SRobert Watson 		return (NULL);
10237a0a89d2SRobert Watson 	}
1024*aa772005SRobert Watson 	if (t == NULL)
1025*aa772005SRobert Watson 		return (NULL);
10267a0a89d2SRobert Watson 
10277a0a89d2SRobert Watson 	ADD_U_CHAR(dptr, AUT_SOCKET_EX);
102806edd2f1SRobert Watson 	ADD_U_INT16(dptr, au_domain_to_bsm(so_domain));
102906edd2f1SRobert Watson 	ADD_U_INT16(dptr, au_socket_type_to_bsm(so_type));
10307a0a89d2SRobert Watson 	if (so_domain == AF_INET) {
10317a0a89d2SRobert Watson 		ADD_U_INT16(dptr, AU_IPv4);
10327a0a89d2SRobert Watson 		sin = (struct sockaddr_in *)sa_local;
10337a0a89d2SRobert Watson 		ADD_MEM(dptr, &sin->sin_port, sizeof(uint16_t));
10347a0a89d2SRobert Watson 		ADD_MEM(dptr, &sin->sin_addr.s_addr, sizeof(uint32_t));
10357a0a89d2SRobert Watson 		sin = (struct sockaddr_in *)sa_remote;
10367a0a89d2SRobert Watson 		ADD_MEM(dptr, &sin->sin_port, sizeof(uint16_t));
10377a0a89d2SRobert Watson 		ADD_MEM(dptr, &sin->sin_addr.s_addr, sizeof(uint32_t));
10387a0a89d2SRobert Watson 	} else {
10397a0a89d2SRobert Watson 		ADD_U_INT16(dptr, AU_IPv6);
10407a0a89d2SRobert Watson 		sin6 = (struct sockaddr_in6 *)sa_local;
10417a0a89d2SRobert Watson 		ADD_MEM(dptr, &sin6->sin6_port, sizeof(uint16_t));
10427a0a89d2SRobert Watson 		ADD_MEM(dptr, &sin6->sin6_addr, 4 * sizeof(uint32_t));
10437a0a89d2SRobert Watson 		sin6 = (struct sockaddr_in6 *)sa_remote;
10447a0a89d2SRobert Watson 		ADD_MEM(dptr, &sin6->sin6_port, sizeof(uint16_t));
10457a0a89d2SRobert Watson 		ADD_MEM(dptr, &sin6->sin6_addr, 4 * sizeof(uint32_t));
10467a0a89d2SRobert Watson 	}
10477a0a89d2SRobert Watson 
10487a0a89d2SRobert Watson 	return (t);
10497a0a89d2SRobert Watson }
10507a0a89d2SRobert Watson 
10517a0a89d2SRobert Watson /*
10527a0a89d2SRobert Watson  * token ID                1 byte
1053ca0716f5SRobert Watson  * socket family           2 bytes
1054597df30eSRobert Watson  * path                    (up to) 104 bytes + NULL  (NULL terminated string)
1055ca0716f5SRobert Watson  */
1056ca0716f5SRobert Watson token_t *
au_to_sock_unix(struct sockaddr_un * so)1057ca0716f5SRobert Watson au_to_sock_unix(struct sockaddr_un *so)
1058ca0716f5SRobert Watson {
1059ca0716f5SRobert Watson 	token_t *t;
1060ca0716f5SRobert Watson 	u_char *dptr;
1061ca0716f5SRobert Watson 
1062ca0716f5SRobert Watson 	GET_TOKEN_AREA(t, dptr, 3 * sizeof(u_char) + strlen(so->sun_path) + 1);
1063ca0716f5SRobert Watson 	if (t == NULL)
1064ca0716f5SRobert Watson 		return (NULL);
1065ca0716f5SRobert Watson 
106652267f74SRobert Watson 	ADD_U_CHAR(dptr, AUT_SOCKUNIX);
1067ca0716f5SRobert Watson 	/* BSM token has two bytes for family */
1068ca0716f5SRobert Watson 	ADD_U_CHAR(dptr, 0);
1069ca0716f5SRobert Watson 	ADD_U_CHAR(dptr, so->sun_family);
1070ca0716f5SRobert Watson 	ADD_STRING(dptr, so->sun_path, strlen(so->sun_path) + 1);
1071ca0716f5SRobert Watson 
1072ca0716f5SRobert Watson 	return (t);
1073ca0716f5SRobert Watson }
1074ca0716f5SRobert Watson 
1075ca0716f5SRobert Watson /*
1076ca0716f5SRobert Watson  * token ID                1 byte
1077ca0716f5SRobert Watson  * socket family           2 bytes
1078ca0716f5SRobert Watson  * local port              2 bytes
1079ca0716f5SRobert Watson  * socket address          4 bytes
1080ca0716f5SRobert Watson  */
1081ca0716f5SRobert Watson token_t *
au_to_sock_inet32(struct sockaddr_in * so)1082ca0716f5SRobert Watson au_to_sock_inet32(struct sockaddr_in *so)
1083ca0716f5SRobert Watson {
1084ca0716f5SRobert Watson 	token_t *t;
1085ca0716f5SRobert Watson 	u_char *dptr = NULL;
1086506764c6SRobert Watson 	uint16_t family;
1087ca0716f5SRobert Watson 
1088506764c6SRobert Watson 	GET_TOKEN_AREA(t, dptr, sizeof(u_char) + 2 * sizeof(uint16_t) +
1089506764c6SRobert Watson 	    sizeof(uint32_t));
1090ca0716f5SRobert Watson 	if (t == NULL)
1091ca0716f5SRobert Watson 		return (NULL);
1092ca0716f5SRobert Watson 
1093ca0716f5SRobert Watson 	ADD_U_CHAR(dptr, AUT_SOCKINET32);
1094ca0716f5SRobert Watson 	/*
1095506764c6SRobert Watson 	 * BSM defines the family field as 16 bits, but many operating
1096506764c6SRobert Watson 	 * systems have an 8-bit sin_family field.  Extend to 16 bits before
1097506764c6SRobert Watson 	 * writing into the token.  Assume that both the port and the address
1098506764c6SRobert Watson 	 * in the sockaddr_in are already in network byte order, but family
1099506764c6SRobert Watson 	 * is in local byte order.
1100506764c6SRobert Watson 	 *
1101506764c6SRobert Watson 	 * XXXRW: Should a name space conversion be taking place on the value
1102506764c6SRobert Watson 	 * of sin_family?
1103ca0716f5SRobert Watson 	 */
1104506764c6SRobert Watson 	family = so->sin_family;
1105506764c6SRobert Watson 	ADD_U_INT16(dptr, family);
1106506764c6SRobert Watson 	ADD_MEM(dptr, &so->sin_port, sizeof(uint16_t));
1107506764c6SRobert Watson 	ADD_MEM(dptr, &so->sin_addr.s_addr, sizeof(uint32_t));
1108ca0716f5SRobert Watson 
1109ca0716f5SRobert Watson 	return (t);
1110ca0716f5SRobert Watson }
1111ca0716f5SRobert Watson 
1112ca0716f5SRobert Watson token_t *
au_to_sock_inet128(struct sockaddr_in6 * so)1113ca0716f5SRobert Watson au_to_sock_inet128(struct sockaddr_in6 *so)
1114ca0716f5SRobert Watson {
1115ca0716f5SRobert Watson 	token_t *t;
1116ca0716f5SRobert Watson 	u_char *dptr = NULL;
1117ca0716f5SRobert Watson 
1118ca0716f5SRobert Watson 	GET_TOKEN_AREA(t, dptr, 3 * sizeof(u_char) + sizeof(u_int16_t) +
1119ca0716f5SRobert Watson 	    4 * sizeof(u_int32_t));
1120ca0716f5SRobert Watson 	if (t == NULL)
1121ca0716f5SRobert Watson 		return (NULL);
1122ca0716f5SRobert Watson 
1123ca0716f5SRobert Watson 	ADD_U_CHAR(dptr, AUT_SOCKINET128);
1124ca0716f5SRobert Watson 	/*
11257a0a89d2SRobert Watson 	 * In BSD, sin6_family is one octet, but BSM defines the token to
11267a0a89d2SRobert Watson 	 * store two. So we copy in a 0 first.  XXXRW: Possibly should be
11277a0a89d2SRobert Watson 	 * conditionally compiled.
1128ca0716f5SRobert Watson 	 */
1129ca0716f5SRobert Watson 	ADD_U_CHAR(dptr, 0);
1130ca0716f5SRobert Watson 	ADD_U_CHAR(dptr, so->sin6_family);
1131ca0716f5SRobert Watson 
1132ca0716f5SRobert Watson 	ADD_U_INT16(dptr, so->sin6_port);
1133506764c6SRobert Watson 	ADD_MEM(dptr, &so->sin6_addr, 4 * sizeof(uint32_t));
1134ca0716f5SRobert Watson 
1135ca0716f5SRobert Watson 	return (t);
1136ca0716f5SRobert Watson }
1137ca0716f5SRobert Watson 
1138ca0716f5SRobert Watson token_t *
au_to_sock_inet(struct sockaddr_in * so)1139ca0716f5SRobert Watson au_to_sock_inet(struct sockaddr_in *so)
1140ca0716f5SRobert Watson {
1141ca0716f5SRobert Watson 
1142ca0716f5SRobert Watson 	return (au_to_sock_inet32(so));
1143ca0716f5SRobert Watson }
1144ca0716f5SRobert Watson 
1145ca0716f5SRobert Watson /*
1146ca0716f5SRobert Watson  * token ID                1 byte
1147ca0716f5SRobert Watson  * audit ID                4 bytes
1148ca0716f5SRobert Watson  * effective user ID       4 bytes
1149ca0716f5SRobert Watson  * effective group ID      4 bytes
1150ca0716f5SRobert Watson  * real user ID            4 bytes
1151ca0716f5SRobert Watson  * real group ID           4 bytes
1152ca0716f5SRobert Watson  * process ID              4 bytes
1153ca0716f5SRobert Watson  * session ID              4 bytes
1154ca0716f5SRobert Watson  * terminal ID
1155ca0716f5SRobert Watson  *   port ID               4 bytes/8 bytes (32-bit/64-bit value)
1156ca0716f5SRobert Watson  *   machine address       4 bytes
1157ca0716f5SRobert Watson  */
1158ca0716f5SRobert Watson token_t *
au_to_subject32(au_id_t auid,uid_t euid,gid_t egid,uid_t ruid,gid_t rgid,pid_t pid,au_asid_t sid,au_tid_t * tid)1159ca0716f5SRobert Watson au_to_subject32(au_id_t auid, uid_t euid, gid_t egid, uid_t ruid, gid_t rgid,
1160ca0716f5SRobert Watson     pid_t pid, au_asid_t sid, au_tid_t *tid)
1161ca0716f5SRobert Watson {
1162ca0716f5SRobert Watson 	token_t *t;
1163ca0716f5SRobert Watson 	u_char *dptr = NULL;
1164ca0716f5SRobert Watson 
1165ca0716f5SRobert Watson 	GET_TOKEN_AREA(t, dptr, sizeof(u_char) + 9 * sizeof(u_int32_t));
1166ca0716f5SRobert Watson 	if (t == NULL)
1167ca0716f5SRobert Watson 		return (NULL);
1168ca0716f5SRobert Watson 
1169ca0716f5SRobert Watson 	ADD_U_CHAR(dptr, AUT_SUBJECT32);
1170ca0716f5SRobert Watson 	ADD_U_INT32(dptr, auid);
1171ca0716f5SRobert Watson 	ADD_U_INT32(dptr, euid);
1172ca0716f5SRobert Watson 	ADD_U_INT32(dptr, egid);
1173ca0716f5SRobert Watson 	ADD_U_INT32(dptr, ruid);
1174ca0716f5SRobert Watson 	ADD_U_INT32(dptr, rgid);
1175ca0716f5SRobert Watson 	ADD_U_INT32(dptr, pid);
1176ca0716f5SRobert Watson 	ADD_U_INT32(dptr, sid);
1177ca0716f5SRobert Watson 	ADD_U_INT32(dptr, tid->port);
1178506764c6SRobert Watson 	ADD_MEM(dptr, &tid->machine, sizeof(u_int32_t));
1179ca0716f5SRobert Watson 
1180ca0716f5SRobert Watson 	return (t);
1181ca0716f5SRobert Watson }
1182ca0716f5SRobert Watson 
1183ca0716f5SRobert Watson token_t *
au_to_subject64(au_id_t auid,uid_t euid,gid_t egid,uid_t ruid,gid_t rgid,pid_t pid,au_asid_t sid,au_tid_t * tid)1184ca0716f5SRobert Watson au_to_subject64(au_id_t auid, uid_t euid, gid_t egid, uid_t ruid, gid_t rgid,
1185ca0716f5SRobert Watson     pid_t pid, au_asid_t sid, au_tid_t *tid)
1186ca0716f5SRobert Watson {
1187bc168a6cSRobert Watson 	token_t *t;
1188bc168a6cSRobert Watson 	u_char *dptr = NULL;
1189ca0716f5SRobert Watson 
1190bc168a6cSRobert Watson 	GET_TOKEN_AREA(t, dptr, sizeof(u_char) + 7 * sizeof(u_int32_t) +
1191bc168a6cSRobert Watson 	    sizeof(u_int64_t) + sizeof(u_int32_t));
1192bc168a6cSRobert Watson 	if (t == NULL)
1193ca0716f5SRobert Watson 		return (NULL);
1194bc168a6cSRobert Watson 
1195bc168a6cSRobert Watson 	ADD_U_CHAR(dptr, AUT_SUBJECT64);
1196bc168a6cSRobert Watson 	ADD_U_INT32(dptr, auid);
1197bc168a6cSRobert Watson 	ADD_U_INT32(dptr, euid);
1198bc168a6cSRobert Watson 	ADD_U_INT32(dptr, egid);
1199bc168a6cSRobert Watson 	ADD_U_INT32(dptr, ruid);
1200bc168a6cSRobert Watson 	ADD_U_INT32(dptr, rgid);
1201bc168a6cSRobert Watson 	ADD_U_INT32(dptr, pid);
1202bc168a6cSRobert Watson 	ADD_U_INT32(dptr, sid);
1203bc168a6cSRobert Watson 	ADD_U_INT64(dptr, tid->port);
1204bc168a6cSRobert Watson 	ADD_MEM(dptr, &tid->machine, sizeof(u_int32_t));
1205bc168a6cSRobert Watson 
1206bc168a6cSRobert Watson 	return (t);
1207ca0716f5SRobert Watson }
1208ca0716f5SRobert Watson 
1209ca0716f5SRobert Watson token_t *
au_to_subject(au_id_t auid,uid_t euid,gid_t egid,uid_t ruid,gid_t rgid,pid_t pid,au_asid_t sid,au_tid_t * tid)1210ca0716f5SRobert Watson au_to_subject(au_id_t auid, uid_t euid, gid_t egid, uid_t ruid, gid_t rgid,
1211ca0716f5SRobert Watson     pid_t pid, au_asid_t sid, au_tid_t *tid)
1212ca0716f5SRobert Watson {
1213ca0716f5SRobert Watson 
1214ca0716f5SRobert Watson 	return (au_to_subject32(auid, euid, egid, ruid, rgid, pid, sid,
1215ca0716f5SRobert Watson 	    tid));
1216ca0716f5SRobert Watson }
1217ca0716f5SRobert Watson 
1218ca0716f5SRobert Watson /*
1219ca0716f5SRobert Watson  * token ID                1 byte
1220ca0716f5SRobert Watson  * audit ID                4 bytes
1221ca0716f5SRobert Watson  * effective user ID       4 bytes
1222ca0716f5SRobert Watson  * effective group ID      4 bytes
1223ca0716f5SRobert Watson  * real user ID            4 bytes
1224ca0716f5SRobert Watson  * real group ID           4 bytes
1225ca0716f5SRobert Watson  * process ID              4 bytes
1226ca0716f5SRobert Watson  * session ID              4 bytes
1227ca0716f5SRobert Watson  * terminal ID
1228ca0716f5SRobert Watson  *   port ID               4 bytes/8 bytes (32-bit/64-bit value)
1229ca0716f5SRobert Watson  *   address type/length   4 bytes
1230ca0716f5SRobert Watson  *   machine address      16 bytes
1231ca0716f5SRobert Watson  */
1232ca0716f5SRobert Watson token_t *
au_to_subject32_ex(au_id_t auid,uid_t euid,gid_t egid,uid_t ruid,gid_t rgid,pid_t pid,au_asid_t sid,au_tid_addr_t * tid)1233ca0716f5SRobert Watson au_to_subject32_ex(au_id_t auid, uid_t euid, gid_t egid, uid_t ruid,
1234ca0716f5SRobert Watson     gid_t rgid, pid_t pid, au_asid_t sid, au_tid_addr_t *tid)
1235ca0716f5SRobert Watson {
1236ca0716f5SRobert Watson 	token_t *t;
1237ca0716f5SRobert Watson 	u_char *dptr = NULL;
1238ca0716f5SRobert Watson 
1239d9af45c4SRobert Watson 	if (tid->at_type == AU_IPv4)
1240d9af45c4SRobert Watson 		GET_TOKEN_AREA(t, dptr, sizeof(u_char) + 10 *
1241d9af45c4SRobert Watson 		    sizeof(u_int32_t));
1242d9af45c4SRobert Watson 	else if (tid->at_type == AU_IPv6)
1243d9af45c4SRobert Watson 		GET_TOKEN_AREA(t, dptr, sizeof(u_char) + 13 *
1244d9af45c4SRobert Watson 		    sizeof(u_int32_t));
1245d9af45c4SRobert Watson 	else {
1246d9af45c4SRobert Watson 		errno = EINVAL;
1247d9af45c4SRobert Watson 		return (NULL);
1248d9af45c4SRobert Watson 	}
1249ca0716f5SRobert Watson 	if (t == NULL)
1250ca0716f5SRobert Watson 		return (NULL);
1251ca0716f5SRobert Watson 
1252ca0716f5SRobert Watson 	ADD_U_CHAR(dptr, AUT_SUBJECT32_EX);
1253ca0716f5SRobert Watson 	ADD_U_INT32(dptr, auid);
1254ca0716f5SRobert Watson 	ADD_U_INT32(dptr, euid);
1255ca0716f5SRobert Watson 	ADD_U_INT32(dptr, egid);
1256ca0716f5SRobert Watson 	ADD_U_INT32(dptr, ruid);
1257ca0716f5SRobert Watson 	ADD_U_INT32(dptr, rgid);
1258ca0716f5SRobert Watson 	ADD_U_INT32(dptr, pid);
1259ca0716f5SRobert Watson 	ADD_U_INT32(dptr, sid);
1260ca0716f5SRobert Watson 	ADD_U_INT32(dptr, tid->at_port);
1261ca0716f5SRobert Watson 	ADD_U_INT32(dptr, tid->at_type);
1262bc168a6cSRobert Watson 	if (tid->at_type == AU_IPv6)
1263bc168a6cSRobert Watson 		ADD_MEM(dptr, &tid->at_addr[0], 4 * sizeof(u_int32_t));
1264bc168a6cSRobert Watson 	else
1265bc168a6cSRobert Watson 		ADD_MEM(dptr, &tid->at_addr[0], sizeof(u_int32_t));
1266ca0716f5SRobert Watson 
1267ca0716f5SRobert Watson 	return (t);
1268ca0716f5SRobert Watson }
1269ca0716f5SRobert Watson 
1270ca0716f5SRobert Watson token_t *
au_to_subject64_ex(au_id_t auid,uid_t euid,gid_t egid,uid_t ruid,gid_t rgid,pid_t pid,au_asid_t sid,au_tid_addr_t * tid)1271ca0716f5SRobert Watson au_to_subject64_ex(au_id_t auid, uid_t euid, gid_t egid, uid_t ruid,
1272ca0716f5SRobert Watson     gid_t rgid, pid_t pid, au_asid_t sid, au_tid_addr_t *tid)
1273ca0716f5SRobert Watson {
1274bc168a6cSRobert Watson 	token_t *t;
1275bc168a6cSRobert Watson 	u_char *dptr = NULL;
1276ca0716f5SRobert Watson 
1277bc168a6cSRobert Watson 	if (tid->at_type == AU_IPv4)
1278bc168a6cSRobert Watson 		GET_TOKEN_AREA(t, dptr, sizeof(u_char) +
1279bc168a6cSRobert Watson 		    7 * sizeof(u_int32_t) + sizeof(u_int64_t) +
1280bc168a6cSRobert Watson 		    2 * sizeof(u_int32_t));
1281bc168a6cSRobert Watson 	else if (tid->at_type == AU_IPv6)
1282bc168a6cSRobert Watson 		GET_TOKEN_AREA(t, dptr, sizeof(u_char) +
1283bc168a6cSRobert Watson 		    7 * sizeof(u_int32_t) + sizeof(u_int64_t) +
1284bc168a6cSRobert Watson 		    5 * sizeof(u_int32_t));
1285bc168a6cSRobert Watson 	else {
1286bc168a6cSRobert Watson 		errno = EINVAL;
1287ca0716f5SRobert Watson 		return (NULL);
1288ca0716f5SRobert Watson 	}
1289bc168a6cSRobert Watson 	if (t == NULL)
1290bc168a6cSRobert Watson 		return (NULL);
1291bc168a6cSRobert Watson 
1292bc168a6cSRobert Watson 	ADD_U_CHAR(dptr, AUT_SUBJECT64_EX);
1293bc168a6cSRobert Watson 	ADD_U_INT32(dptr, auid);
1294bc168a6cSRobert Watson 	ADD_U_INT32(dptr, euid);
1295bc168a6cSRobert Watson 	ADD_U_INT32(dptr, egid);
1296bc168a6cSRobert Watson 	ADD_U_INT32(dptr, ruid);
1297bc168a6cSRobert Watson 	ADD_U_INT32(dptr, rgid);
1298bc168a6cSRobert Watson 	ADD_U_INT32(dptr, pid);
1299bc168a6cSRobert Watson 	ADD_U_INT32(dptr, sid);
1300bc168a6cSRobert Watson 	ADD_U_INT64(dptr, tid->at_port);
1301bc168a6cSRobert Watson 	ADD_U_INT32(dptr, tid->at_type);
1302bc168a6cSRobert Watson 	if (tid->at_type == AU_IPv6)
1303bc168a6cSRobert Watson 		ADD_MEM(dptr, &tid->at_addr[0], 4 * sizeof(u_int32_t));
1304bc168a6cSRobert Watson 	else
1305bc168a6cSRobert Watson 		ADD_MEM(dptr, &tid->at_addr[0], sizeof(u_int32_t));
1306bc168a6cSRobert Watson 
1307bc168a6cSRobert Watson 	return (t);
1308bc168a6cSRobert Watson }
1309ca0716f5SRobert Watson 
1310ca0716f5SRobert Watson token_t *
au_to_subject_ex(au_id_t auid,uid_t euid,gid_t egid,uid_t ruid,gid_t rgid,pid_t pid,au_asid_t sid,au_tid_addr_t * tid)1311ca0716f5SRobert Watson au_to_subject_ex(au_id_t auid, uid_t euid, gid_t egid, uid_t ruid,
1312ca0716f5SRobert Watson     gid_t rgid, pid_t pid, au_asid_t sid, au_tid_addr_t *tid)
1313ca0716f5SRobert Watson {
1314ca0716f5SRobert Watson 
1315ca0716f5SRobert Watson 	return (au_to_subject32_ex(auid, euid, egid, ruid, rgid, pid, sid,
1316ca0716f5SRobert Watson 	    tid));
1317ca0716f5SRobert Watson }
1318ca0716f5SRobert Watson 
13193b97a967SRobert Watson #if !defined(_KERNEL) && !defined(KERNEL) && defined(HAVE_AUDIT_SYSCALLS)
1320ca0716f5SRobert Watson /*
132152267f74SRobert Watson  * Collects audit information for the current process and creates a subject
132252267f74SRobert Watson  * token from it.
1323ca0716f5SRobert Watson  */
1324ca0716f5SRobert Watson token_t *
au_to_me(void)1325ca0716f5SRobert Watson au_to_me(void)
1326ca0716f5SRobert Watson {
1327ca0716f5SRobert Watson 	auditinfo_t auinfo;
1328597df30eSRobert Watson 	auditinfo_addr_t aia;
1329ca0716f5SRobert Watson 
1330597df30eSRobert Watson 	/*
1331597df30eSRobert Watson 	 * Try to use getaudit_addr(2) first.  If this kernel does not support
1332597df30eSRobert Watson 	 * it, then fall back on to getaudit(2).
1333597df30eSRobert Watson 	 */
1334597df30eSRobert Watson 	if (getaudit_addr(&aia, sizeof(aia)) != 0) {
1335597df30eSRobert Watson 		if (errno == ENOSYS) {
1336ca0716f5SRobert Watson 			if (getaudit(&auinfo) != 0)
1337ca0716f5SRobert Watson 				return (NULL);
1338597df30eSRobert Watson 			return (au_to_subject32(auinfo.ai_auid, geteuid(),
1339597df30eSRobert Watson 				getegid(), getuid(), getgid(), getpid(),
1340597df30eSRobert Watson 				auinfo.ai_asid, &auinfo.ai_termid));
1341597df30eSRobert Watson 		} else {
1342597df30eSRobert Watson 			/* getaudit_addr(2) failed for some other reason. */
1343597df30eSRobert Watson 			return (NULL);
1344597df30eSRobert Watson 		}
1345597df30eSRobert Watson 	}
1346ca0716f5SRobert Watson 
1347597df30eSRobert Watson 	return (au_to_subject32_ex(aia.ai_auid, geteuid(), getegid(), getuid(),
1348597df30eSRobert Watson 		getgid(), getpid(), aia.ai_asid, &aia.ai_termid));
1349ca0716f5SRobert Watson }
1350ca0716f5SRobert Watson #endif
1351ca0716f5SRobert Watson 
1352ca0716f5SRobert Watson /*
1353ca0716f5SRobert Watson  * token ID				1 byte
1354ca0716f5SRobert Watson  * count				4 bytes
1355ca0716f5SRobert Watson  * text					count null-terminated strings
1356ca0716f5SRobert Watson  */
1357ca0716f5SRobert Watson token_t *
au_to_exec_args(char ** argv)135885feadf6SRobert Watson au_to_exec_args(char **argv)
1359ca0716f5SRobert Watson {
1360ca0716f5SRobert Watson 	token_t *t;
1361ca0716f5SRobert Watson 	u_char *dptr = NULL;
1362ca0716f5SRobert Watson 	const char *nextarg;
1363ca0716f5SRobert Watson 	int i, count = 0;
1364ca0716f5SRobert Watson 	size_t totlen = 0;
1365ca0716f5SRobert Watson 
136622ccb20dSRobert Watson 	nextarg = *argv;
1367ca0716f5SRobert Watson 
1368ca0716f5SRobert Watson 	while (nextarg != NULL) {
1369ca0716f5SRobert Watson 		int nextlen;
1370ca0716f5SRobert Watson 
1371ca0716f5SRobert Watson 		nextlen = strlen(nextarg);
1372ca0716f5SRobert Watson 		totlen += nextlen + 1;
1373ca0716f5SRobert Watson 		count++;
137422ccb20dSRobert Watson 		nextarg = *(argv + count);
1375ca0716f5SRobert Watson 	}
1376ca0716f5SRobert Watson 
1377ca0716f5SRobert Watson 	GET_TOKEN_AREA(t, dptr, sizeof(u_char) + sizeof(u_int32_t) + totlen);
1378ca0716f5SRobert Watson 	if (t == NULL)
1379ca0716f5SRobert Watson 		return (NULL);
1380ca0716f5SRobert Watson 
1381ca0716f5SRobert Watson 	ADD_U_CHAR(dptr, AUT_EXEC_ARGS);
1382ca0716f5SRobert Watson 	ADD_U_INT32(dptr, count);
1383ca0716f5SRobert Watson 
1384ca0716f5SRobert Watson 	for (i = 0; i < count; i++) {
138522ccb20dSRobert Watson 		nextarg = *(argv + i);
1386ca0716f5SRobert Watson 		ADD_MEM(dptr, nextarg, strlen(nextarg) + 1);
1387ca0716f5SRobert Watson 	}
1388ca0716f5SRobert Watson 
1389ca0716f5SRobert Watson 	return (t);
1390ca0716f5SRobert Watson }
1391ca0716f5SRobert Watson 
1392ca0716f5SRobert Watson /*
1393ca0716f5SRobert Watson  * token ID				1 byte
1394ca0716f5SRobert Watson  * count				4 bytes
1395ca0716f5SRobert Watson  * text					count null-terminated strings
1396ca0716f5SRobert Watson  */
1397ca0716f5SRobert Watson token_t *
au_to_exec_env(char ** envp)139885feadf6SRobert Watson au_to_exec_env(char **envp)
1399ca0716f5SRobert Watson {
1400ca0716f5SRobert Watson 	token_t *t;
1401ca0716f5SRobert Watson 	u_char *dptr = NULL;
1402ca0716f5SRobert Watson 	int i, count = 0;
1403ca0716f5SRobert Watson 	size_t totlen = 0;
1404ca0716f5SRobert Watson 	const char *nextenv;
1405ca0716f5SRobert Watson 
140622ccb20dSRobert Watson 	nextenv = *envp;
1407ca0716f5SRobert Watson 
1408ca0716f5SRobert Watson 	while (nextenv != NULL) {
1409ca0716f5SRobert Watson 		int nextlen;
1410ca0716f5SRobert Watson 
1411ca0716f5SRobert Watson 		nextlen = strlen(nextenv);
1412ca0716f5SRobert Watson 		totlen += nextlen + 1;
1413ca0716f5SRobert Watson 		count++;
141422ccb20dSRobert Watson 		nextenv = *(envp + count);
1415ca0716f5SRobert Watson 	}
1416ca0716f5SRobert Watson 
1417ca0716f5SRobert Watson 	GET_TOKEN_AREA(t, dptr, sizeof(u_char) + sizeof(u_int32_t) + totlen);
1418ca0716f5SRobert Watson 	if (t == NULL)
1419ca0716f5SRobert Watson 		return (NULL);
1420ca0716f5SRobert Watson 
1421ca0716f5SRobert Watson 	ADD_U_CHAR(dptr, AUT_EXEC_ENV);
1422ca0716f5SRobert Watson 	ADD_U_INT32(dptr, count);
1423ca0716f5SRobert Watson 
1424ca0716f5SRobert Watson 	for (i = 0; i < count; i++) {
142522ccb20dSRobert Watson 		nextenv = *(envp + i);
1426ca0716f5SRobert Watson 		ADD_MEM(dptr, nextenv, strlen(nextenv) + 1);
1427ca0716f5SRobert Watson 	}
1428ca0716f5SRobert Watson 
1429ca0716f5SRobert Watson 	return (t);
1430ca0716f5SRobert Watson }
1431ca0716f5SRobert Watson 
1432ca0716f5SRobert Watson /*
1433ca0716f5SRobert Watson  * token ID                1 byte
14347a0a89d2SRobert Watson  * zonename length         2 bytes
14357a0a89d2SRobert Watson  * zonename                N bytes + 1 terminating NULL byte
14367a0a89d2SRobert Watson  */
14377a0a89d2SRobert Watson token_t *
au_to_zonename(const char * zonename)14387a0a89d2SRobert Watson au_to_zonename(const char *zonename)
14397a0a89d2SRobert Watson {
14407a0a89d2SRobert Watson 	u_char *dptr = NULL;
14417a0a89d2SRobert Watson 	u_int16_t textlen;
14427a0a89d2SRobert Watson 	token_t *t;
14437a0a89d2SRobert Watson 
14447a0a89d2SRobert Watson 	textlen = strlen(zonename) + 1;
14457a0a89d2SRobert Watson 	GET_TOKEN_AREA(t, dptr, sizeof(u_char) + sizeof(u_int16_t) + textlen);
14467a0a89d2SRobert Watson 	if (t == NULL)
14477a0a89d2SRobert Watson 		return (NULL);
14487a0a89d2SRobert Watson 
14497a0a89d2SRobert Watson 	ADD_U_CHAR(dptr, AUT_ZONENAME);
14507a0a89d2SRobert Watson 	ADD_U_INT16(dptr, textlen);
14517a0a89d2SRobert Watson 	ADD_STRING(dptr, zonename, textlen);
14527a0a89d2SRobert Watson 	return (t);
14537a0a89d2SRobert Watson }
14547a0a89d2SRobert Watson 
14557a0a89d2SRobert Watson /*
14567a0a89d2SRobert Watson  * token ID                1 byte
1457ca0716f5SRobert Watson  * record byte count       4 bytes
1458ca0716f5SRobert Watson  * version #               1 byte    [2]
1459ca0716f5SRobert Watson  * event type              2 bytes
1460ca0716f5SRobert Watson  * event modifier          2 bytes
1461ca0716f5SRobert Watson  * seconds of time         4 bytes/8 bytes (32-bit/64-bit value)
1462ca0716f5SRobert Watson  * milliseconds of time    4 bytes/8 bytes (32-bit/64-bit value)
1463ca0716f5SRobert Watson  */
1464ca0716f5SRobert Watson token_t *
au_to_header32_tm(int rec_size,au_event_t e_type,au_emod_t e_mod,struct timeval tm)1465506764c6SRobert Watson au_to_header32_tm(int rec_size, au_event_t e_type, au_emod_t e_mod,
1466ca0716f5SRobert Watson     struct timeval tm)
1467ca0716f5SRobert Watson {
1468ca0716f5SRobert Watson 	token_t *t;
1469ca0716f5SRobert Watson 	u_char *dptr = NULL;
1470ca0716f5SRobert Watson 	u_int32_t timems;
1471ca0716f5SRobert Watson 
1472ca0716f5SRobert Watson 	GET_TOKEN_AREA(t, dptr, sizeof(u_char) + sizeof(u_int32_t) +
1473ca0716f5SRobert Watson 	    sizeof(u_char) + 2 * sizeof(u_int16_t) + 2 * sizeof(u_int32_t));
1474ca0716f5SRobert Watson 	if (t == NULL)
1475ca0716f5SRobert Watson 		return (NULL);
1476ca0716f5SRobert Watson 
1477ca0716f5SRobert Watson 	ADD_U_CHAR(dptr, AUT_HEADER32);
1478ca0716f5SRobert Watson 	ADD_U_INT32(dptr, rec_size);
147922ccb20dSRobert Watson 	ADD_U_CHAR(dptr, AUDIT_HEADER_VERSION_OPENBSM);
1480ca0716f5SRobert Watson 	ADD_U_INT16(dptr, e_type);
1481ca0716f5SRobert Watson 	ADD_U_INT16(dptr, e_mod);
1482ca0716f5SRobert Watson 
1483ca0716f5SRobert Watson 	timems = tm.tv_usec/1000;
1484ca0716f5SRobert Watson 	/* Add the timestamp */
1485ca0716f5SRobert Watson 	ADD_U_INT32(dptr, tm.tv_sec);
1486ca0716f5SRobert Watson 	ADD_U_INT32(dptr, timems);	/* We need time in ms. */
1487ca0716f5SRobert Watson 
1488ca0716f5SRobert Watson 	return (t);
1489ca0716f5SRobert Watson }
1490ca0716f5SRobert Watson 
149152267f74SRobert Watson /*
149252267f74SRobert Watson  * token ID                1 byte
149352267f74SRobert Watson  * record byte count       4 bytes
149452267f74SRobert Watson  * version #               1 byte    [2]
149552267f74SRobert Watson  * event type              2 bytes
149652267f74SRobert Watson  * event modifier          2 bytes
149752267f74SRobert Watson  * address type/length     4 bytes
149852267f74SRobert Watson  * machine address         4 bytes/16 bytes (IPv4/IPv6 address)
149952267f74SRobert Watson  * seconds of time         4 bytes/8 bytes (32-bit/64-bit value)
150052267f74SRobert Watson  * milliseconds of time    4 bytes/8 bytes (32-bit/64-bit value)
150152267f74SRobert Watson  */
150252267f74SRobert Watson token_t *
au_to_header32_ex_tm(int rec_size,au_event_t e_type,au_emod_t e_mod,struct timeval tm,struct auditinfo_addr * aia)150352267f74SRobert Watson au_to_header32_ex_tm(int rec_size, au_event_t e_type, au_emod_t e_mod,
150452267f74SRobert Watson     struct timeval tm, struct auditinfo_addr *aia)
150552267f74SRobert Watson {
150652267f74SRobert Watson 	token_t *t;
150752267f74SRobert Watson 	u_char *dptr = NULL;
15087a0a89d2SRobert Watson 	u_int32_t timems;
15097a0a89d2SRobert Watson 	au_tid_addr_t *tid;
151052267f74SRobert Watson 
15117a0a89d2SRobert Watson 	tid = &aia->ai_termid;
151252267f74SRobert Watson 	if (tid->at_type != AU_IPv4 && tid->at_type != AU_IPv6)
151352267f74SRobert Watson 		return (NULL);
151452267f74SRobert Watson 	GET_TOKEN_AREA(t, dptr, sizeof(u_char) + sizeof(u_int32_t) +
151552267f74SRobert Watson 	    sizeof(u_char) + 2 * sizeof(u_int16_t) + 3 *
151652267f74SRobert Watson 	    sizeof(u_int32_t) + tid->at_type);
151752267f74SRobert Watson 	if (t == NULL)
151852267f74SRobert Watson 		return (NULL);
151952267f74SRobert Watson 
152052267f74SRobert Watson 	ADD_U_CHAR(dptr, AUT_HEADER32_EX);
152152267f74SRobert Watson 	ADD_U_INT32(dptr, rec_size);
152252267f74SRobert Watson 	ADD_U_CHAR(dptr, AUDIT_HEADER_VERSION_OPENBSM);
152352267f74SRobert Watson 	ADD_U_INT16(dptr, e_type);
152452267f74SRobert Watson 	ADD_U_INT16(dptr, e_mod);
152552267f74SRobert Watson 
152652267f74SRobert Watson 	ADD_U_INT32(dptr, tid->at_type);
152752267f74SRobert Watson 	if (tid->at_type == AU_IPv6)
152852267f74SRobert Watson 		ADD_MEM(dptr, &tid->at_addr[0], 4 * sizeof(u_int32_t));
152952267f74SRobert Watson 	else
153052267f74SRobert Watson 		ADD_MEM(dptr, &tid->at_addr[0], sizeof(u_int32_t));
153152267f74SRobert Watson 	timems = tm.tv_usec/1000;
153252267f74SRobert Watson 	/* Add the timestamp */
153352267f74SRobert Watson 	ADD_U_INT32(dptr, tm.tv_sec);
153452267f74SRobert Watson 	ADD_U_INT32(dptr, timems);      /* We need time in ms. */
153552267f74SRobert Watson 
153652267f74SRobert Watson 	return (t);
153752267f74SRobert Watson }
153852267f74SRobert Watson 
1539bc168a6cSRobert Watson token_t *
au_to_header64_tm(int rec_size,au_event_t e_type,au_emod_t e_mod,struct timeval tm)1540bc168a6cSRobert Watson au_to_header64_tm(int rec_size, au_event_t e_type, au_emod_t e_mod,
1541bc168a6cSRobert Watson     struct timeval tm)
1542bc168a6cSRobert Watson {
1543bc168a6cSRobert Watson 	token_t *t;
1544bc168a6cSRobert Watson 	u_char *dptr = NULL;
1545bc168a6cSRobert Watson 	u_int32_t timems;
1546bc168a6cSRobert Watson 
1547bc168a6cSRobert Watson 	GET_TOKEN_AREA(t, dptr, sizeof(u_char) + sizeof(u_int32_t) +
1548bc168a6cSRobert Watson 	    sizeof(u_char) + 2 * sizeof(u_int16_t) + 2 * sizeof(u_int64_t));
1549bc168a6cSRobert Watson 	if (t == NULL)
1550bc168a6cSRobert Watson 		return (NULL);
1551bc168a6cSRobert Watson 
1552bc168a6cSRobert Watson 	ADD_U_CHAR(dptr, AUT_HEADER64);
1553bc168a6cSRobert Watson 	ADD_U_INT32(dptr, rec_size);
1554bc168a6cSRobert Watson 	ADD_U_CHAR(dptr, AUDIT_HEADER_VERSION_OPENBSM);
1555bc168a6cSRobert Watson 	ADD_U_INT16(dptr, e_type);
1556bc168a6cSRobert Watson 	ADD_U_INT16(dptr, e_mod);
1557bc168a6cSRobert Watson 
1558bc168a6cSRobert Watson 	timems = tm.tv_usec/1000;
1559bc168a6cSRobert Watson 	/* Add the timestamp */
1560bc168a6cSRobert Watson 	ADD_U_INT64(dptr, tm.tv_sec);
1561bc168a6cSRobert Watson 	ADD_U_INT64(dptr, timems);	/* We need time in ms. */
1562bc168a6cSRobert Watson 
1563bc168a6cSRobert Watson 	return (t);
1564bc168a6cSRobert Watson }
1565bc168a6cSRobert Watson 
1566506764c6SRobert Watson #if !defined(KERNEL) && !defined(_KERNEL)
156752267f74SRobert Watson #ifdef HAVE_AUDIT_SYSCALLS
156852267f74SRobert Watson token_t *
au_to_header32_ex(int rec_size,au_event_t e_type,au_emod_t e_mod)156952267f74SRobert Watson au_to_header32_ex(int rec_size, au_event_t e_type, au_emod_t e_mod)
157052267f74SRobert Watson {
157152267f74SRobert Watson 	struct timeval tm;
157252267f74SRobert Watson 	struct auditinfo_addr aia;
157352267f74SRobert Watson 
157452267f74SRobert Watson 	if (gettimeofday(&tm, NULL) == -1)
157552267f74SRobert Watson 		return (NULL);
1576c0020399SRobert Watson 	if (audit_get_kaudit(&aia, sizeof(aia)) != 0) {
157752267f74SRobert Watson 		if (errno != ENOSYS)
157852267f74SRobert Watson 			return (NULL);
157952267f74SRobert Watson 		return (au_to_header32_tm(rec_size, e_type, e_mod, tm));
158052267f74SRobert Watson 	}
158152267f74SRobert Watson 	return (au_to_header32_ex_tm(rec_size, e_type, e_mod, tm, &aia));
158252267f74SRobert Watson }
158352267f74SRobert Watson #endif /* HAVE_AUDIT_SYSCALLS */
158452267f74SRobert Watson 
1585506764c6SRobert Watson token_t *
au_to_header32(int rec_size,au_event_t e_type,au_emod_t e_mod)1586506764c6SRobert Watson au_to_header32(int rec_size, au_event_t e_type, au_emod_t e_mod)
1587506764c6SRobert Watson {
1588506764c6SRobert Watson 	struct timeval tm;
1589506764c6SRobert Watson 
1590506764c6SRobert Watson 	if (gettimeofday(&tm, NULL) == -1)
1591506764c6SRobert Watson 		return (NULL);
1592506764c6SRobert Watson 	return (au_to_header32_tm(rec_size, e_type, e_mod, tm));
1593506764c6SRobert Watson }
1594506764c6SRobert Watson 
1595ca0716f5SRobert Watson token_t *
au_to_header64(__unused int rec_size,__unused au_event_t e_type,__unused au_emod_t e_mod)1596ca0716f5SRobert Watson au_to_header64(__unused int rec_size, __unused au_event_t e_type,
1597ca0716f5SRobert Watson     __unused au_emod_t e_mod)
1598ca0716f5SRobert Watson {
1599bc168a6cSRobert Watson 	struct timeval tm;
1600ca0716f5SRobert Watson 
1601bc168a6cSRobert Watson 	if (gettimeofday(&tm, NULL) == -1)
1602ca0716f5SRobert Watson 		return (NULL);
1603bc168a6cSRobert Watson 	return (au_to_header64_tm(rec_size, e_type, e_mod, tm));
1604ca0716f5SRobert Watson }
1605ca0716f5SRobert Watson 
1606ca0716f5SRobert Watson token_t *
au_to_header(int rec_size,au_event_t e_type,au_emod_t e_mod)1607ca0716f5SRobert Watson au_to_header(int rec_size, au_event_t e_type, au_emod_t e_mod)
1608ca0716f5SRobert Watson {
1609ca0716f5SRobert Watson 
1610ca0716f5SRobert Watson 	return (au_to_header32(rec_size, e_type, e_mod));
1611ca0716f5SRobert Watson }
161252267f74SRobert Watson 
161352267f74SRobert Watson #ifdef HAVE_AUDIT_SYSCALLS
161452267f74SRobert Watson token_t *
au_to_header_ex(int rec_size,au_event_t e_type,au_emod_t e_mod)161552267f74SRobert Watson au_to_header_ex(int rec_size, au_event_t e_type, au_emod_t e_mod)
161652267f74SRobert Watson {
161752267f74SRobert Watson 
161852267f74SRobert Watson 	return (au_to_header32_ex(rec_size, e_type, e_mod));
161952267f74SRobert Watson }
162052267f74SRobert Watson #endif /* HAVE_AUDIT_SYSCALLS */
162152267f74SRobert Watson #endif /* !defined(KERNEL) && !defined(_KERNEL) */
1622ca0716f5SRobert Watson 
1623ca0716f5SRobert Watson /*
1624ca0716f5SRobert Watson  * token ID                1 byte
1625ca0716f5SRobert Watson  * trailer magic number    2 bytes
1626ca0716f5SRobert Watson  * record byte count       4 bytes
1627ca0716f5SRobert Watson  */
1628ca0716f5SRobert Watson token_t *
au_to_trailer(int rec_size)1629ca0716f5SRobert Watson au_to_trailer(int rec_size)
1630ca0716f5SRobert Watson {
1631ca0716f5SRobert Watson 	token_t *t;
1632ca0716f5SRobert Watson 	u_char *dptr = NULL;
16337a0a89d2SRobert Watson 	u_int16_t magic = AUT_TRAILER_MAGIC;
1634ca0716f5SRobert Watson 
1635ca0716f5SRobert Watson 	GET_TOKEN_AREA(t, dptr, sizeof(u_char) + sizeof(u_int16_t) +
1636ca0716f5SRobert Watson 	    sizeof(u_int32_t));
1637ca0716f5SRobert Watson 	if (t == NULL)
1638ca0716f5SRobert Watson 		return (NULL);
1639ca0716f5SRobert Watson 
1640ca0716f5SRobert Watson 	ADD_U_CHAR(dptr, AUT_TRAILER);
1641ca0716f5SRobert Watson 	ADD_U_INT16(dptr, magic);
1642ca0716f5SRobert Watson 	ADD_U_INT32(dptr, rec_size);
1643ca0716f5SRobert Watson 
1644ca0716f5SRobert Watson 	return (t);
1645ca0716f5SRobert Watson }
1646