1159248Srwatson/*-
2168777Srwatson * Copyright (c) 2006-2007 Robert N. M. Watson
3186647Srwatson * Copyright (c) 2008 Apple Inc.
4159248Srwatson * All rights reserved.
5159248Srwatson *
6159248Srwatson * Redistribution and use in source and binary forms, with or without
7159248Srwatson * modification, are permitted provided that the following conditions
8159248Srwatson * are met:
9159248Srwatson * 1. Redistributions of source code must retain the above copyright
10159248Srwatson *    notice, this list of conditions and the following disclaimer.
11159248Srwatson * 2. Redistributions in binary form must reproduce the above copyright
12159248Srwatson *    notice, this list of conditions and the following disclaimer in the
13159248Srwatson *    documentation and/or other materials provided with the distribution.
14159248Srwatson *
15159248Srwatson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16159248Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17159248Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18159248Srwatson * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19159248Srwatson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20159248Srwatson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21159248Srwatson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22159248Srwatson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23159248Srwatson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24159248Srwatson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25159248Srwatson * SUCH DAMAGE.
26159248Srwatson *
27187214Srwatson * $P4: //depot/projects/trustedbsd/openbsm/test/bsm/generate.c#14 $
28159248Srwatson */
29159248Srwatson
30159248Srwatson/*
31159248Srwatson * Generate a series of BSM token samples in the requested directory.
32159248Srwatson */
33159248Srwatson
34159248Srwatson#include <sys/types.h>
35159985Srwatson#include <sys/socket.h>
36159248Srwatson#include <sys/stat.h>
37159248Srwatson
38159248Srwatson#include <netinet/in.h>
39159248Srwatson#include <netinet/in_systm.h>
40159248Srwatson#include <netinet/ip.h>
41159248Srwatson
42159248Srwatson#include <arpa/inet.h>
43159248Srwatson
44159248Srwatson#include <bsm/audit_kevents.h>
45159248Srwatson#include <bsm/libbsm.h>
46159248Srwatson
47159248Srwatson#include <err.h>
48159248Srwatson#include <errno.h>
49159248Srwatson#include <fcntl.h>
50159248Srwatson#include <limits.h>
51159248Srwatson#include <stdio.h>
52159248Srwatson#include <stdlib.h>
53159248Srwatson#include <string.h>
54159248Srwatson#include <sysexits.h>
55159248Srwatson#include <unistd.h>
56159248Srwatson
57159248Srwatsonstatic int	do_records, do_tokens;
58159248Srwatson
59159248Srwatsonstatic void
60159248Srwatsonusage(void)
61159248Srwatson{
62159248Srwatson
63159248Srwatson	fprintf(stderr, "generate [-rt] path\n");
64159248Srwatson	exit(EX_USAGE);
65159248Srwatson}
66159248Srwatson
67159248Srwatsonstatic int
68159248Srwatsonopen_file(const char *directory, const char *name)
69159248Srwatson{
70159248Srwatson	char pathname[PATH_MAX];
71159248Srwatson	int fd;
72159248Srwatson
73159248Srwatson	snprintf(pathname, PATH_MAX, "%s/%s", directory, name);
74159248Srwatson	(void)unlink(pathname);
75159248Srwatson	fd = open(pathname, O_WRONLY | O_CREAT | O_EXCL, 0600);
76159248Srwatson	if (fd < 0)
77159248Srwatson		err(EX_CANTCREAT, "open: %s", name);
78159248Srwatson	return (fd);
79159248Srwatson}
80159248Srwatson
81159248Srwatsonstatic void
82159248Srwatsonwrite_file(int fd, void *buffer, size_t buflen, const char *filename)
83159248Srwatson{
84159248Srwatson	ssize_t len;
85159248Srwatson
86159248Srwatson	len = write(fd, buffer, buflen);
87159248Srwatson	if (len < 0)
88159248Srwatson		err(EX_OSERR, "write_file: %s", filename);
89159248Srwatson	if (len < buflen)
90159248Srwatson		err(EX_OSERR, "write_file: short write: %s", filename);
91159248Srwatson}
92159248Srwatson
93159248Srwatson/*
94159248Srwatson * Write a single token to a file.
95159248Srwatson */
96159248Srwatsonstatic void
97159248Srwatsonwrite_token(const char *directory, const char *filename, token_t *tok)
98159248Srwatson{
99159248Srwatson	u_char buffer[MAX_AUDIT_RECORD_SIZE];
100159248Srwatson	size_t buflen;
101159248Srwatson	int fd;
102159248Srwatson
103159248Srwatson	buflen = MAX_AUDIT_RECORD_SIZE;
104159248Srwatson	if (au_close_token(tok, buffer, &buflen) < 0)
105159248Srwatson		err(EX_UNAVAILABLE, "au_close_token");
106159248Srwatson	fd = open_file(directory, filename);
107159248Srwatson	write_file(fd, buffer, buflen, filename);
108159248Srwatson	close(fd);
109159248Srwatson}
110159248Srwatson
111159248Srwatson/*
112159248Srwatson * Write a token to a file, wrapped in audit record header and trailer.
113159248Srwatson */
114159248Srwatsonstatic void
115159248Srwatsonwrite_record(const char *directory, const char *filename, token_t *tok,
116159248Srwatson    short event)
117159248Srwatson{
118159248Srwatson	u_char buffer[MAX_AUDIT_RECORD_SIZE];
119159248Srwatson	size_t buflen;
120159248Srwatson	int au, fd;
121159248Srwatson
122159248Srwatson	au = au_open();
123159248Srwatson	if (au < 0)
124159248Srwatson		err(EX_UNAVAILABLE, "au_open");
125159248Srwatson	if (au_write(au, tok) < 0)
126159248Srwatson		err(EX_UNAVAILABLE, "au_write");
127159248Srwatson	buflen = MAX_AUDIT_RECORD_SIZE;
128159248Srwatson	if (au_close_buffer(au, event, buffer, &buflen) < 0)
129159248Srwatson		err(EX_UNAVAILABLE, "au_close_buffer");
130159248Srwatson	fd = open_file(directory, filename);
131159248Srwatson	write_file(fd, buffer, buflen, filename);
132159248Srwatson	close(fd);
133159248Srwatson}
134159248Srwatson
135159248Srwatsonstatic struct timeval	 file_token_timeval = { 0x12345, 0x67890} ;
136159248Srwatson
137159248Srwatsonstatic void
138159248Srwatsongenerate_file_token(const char *directory, const char *token_filename)
139159248Srwatson{
140159248Srwatson	token_t *file_token;
141159248Srwatson
142159248Srwatson	file_token = au_to_file("test", file_token_timeval);
143159248Srwatson	if (file_token == NULL)
144159248Srwatson		err(EX_UNAVAILABLE, "au_to_file");
145159248Srwatson	write_token(directory, token_filename, file_token);
146159248Srwatson}
147159248Srwatson
148161630Srwatsonstatic void
149161630Srwatsongenerate_file_record(const char *directory, const char *record_filename)
150161630Srwatson{
151161630Srwatson	token_t *file_token;
152161630Srwatson
153161630Srwatson	file_token = au_to_file("test", file_token_timeval);
154161630Srwatson	if (file_token == NULL)
155161630Srwatson		err(EX_UNAVAILABLE, "au_to_file");
156161630Srwatson	write_record(directory, record_filename, file_token, AUE_NULL);
157161630Srwatson}
158161630Srwatson
159159248Srwatson/*
160159248Srwatson * AUT_OHEADER
161159248Srwatson */
162159248Srwatson
163159248Srwatsonstatic int		 trailer_token_len = 0x12345678;
164159248Srwatson
165159248Srwatsonstatic void
166159248Srwatsongenerate_trailer_token(const char *directory, const char *token_filename)
167159248Srwatson{
168159248Srwatson	token_t *trailer_token;
169159248Srwatson
170159248Srwatson	trailer_token = au_to_trailer(trailer_token_len);
171159248Srwatson	if (trailer_token == NULL)
172159248Srwatson		err(EX_UNAVAILABLE, "au_to_trailer");
173159248Srwatson	write_token(directory, token_filename, trailer_token);
174159248Srwatson}
175159248Srwatson
176159248Srwatsonstatic int		 header32_token_len = 0x12345678;
177159248Srwatsonstatic au_event_t	 header32_e_type = AUE_OPEN;
178159248Srwatsonstatic au_emod_t	 header32_e_mod = 0x4567;
179159248Srwatsonstatic struct timeval	 header32_tm = { 0x12345, 0x67890 };
180159248Srwatson
181159248Srwatsonstatic void
182159248Srwatsongenerate_header32_token(const char *directory, const char *token_filename)
183159248Srwatson{
184159248Srwatson	token_t *header32_token;
185159248Srwatson
186159248Srwatson	header32_token = au_to_header32_tm(header32_token_len,
187159248Srwatson	    header32_e_type, header32_e_mod, header32_tm);
188159248Srwatson	if (header32_token == NULL)
189159248Srwatson		err(EX_UNAVAILABLE, "au_to_header32");
190159248Srwatson	write_token(directory, token_filename, header32_token);
191159248Srwatson}
192159248Srwatson
193159248Srwatson/*
194159248Srwatson * AUT_HEADER32_EX
195159248Srwatson */
196159248Srwatson
197159248Srwatsonstatic char		 data_token_unit_print = AUP_STRING;
198159248Srwatsonstatic char		 data_token_unit_type = AUR_CHAR;
199159248Srwatsonstatic char		*data_token_data = "SomeData";
200159248Srwatsonstatic char		 data_token_unit_count = sizeof("SomeData") + 1;
201159248Srwatson
202159248Srwatsonstatic void
203159248Srwatsongenerate_data_token(const char *directory, const char *token_filename)
204159248Srwatson{
205159248Srwatson	token_t *data_token;
206159248Srwatson
207159248Srwatson	data_token = au_to_data(data_token_unit_print, data_token_unit_type,
208159248Srwatson	    data_token_unit_count, data_token_data);
209159248Srwatson	if (data_token == NULL)
210159248Srwatson		err(EX_UNAVAILABLE, "au_to_data");
211159248Srwatson	write_token(directory, token_filename, data_token);
212159248Srwatson}
213159248Srwatson
214159248Srwatsonstatic void
215159248Srwatsongenerate_data_record(const char *directory, const char *record_filename)
216159248Srwatson{
217159248Srwatson	token_t *data_token;
218159248Srwatson
219159248Srwatson	data_token = au_to_data(data_token_unit_print, data_token_unit_type,
220159248Srwatson	    data_token_unit_count, data_token_data);
221159248Srwatson	if (data_token == NULL)
222159248Srwatson		err(EX_UNAVAILABLE, "au_to_data");
223159248Srwatson	write_record(directory, record_filename, data_token, AUE_NULL);
224159248Srwatson}
225159248Srwatson
226159248Srwatsonstatic char		 ipc_type = AT_IPC_MSG;
227159248Srwatsonstatic int		 ipc_id = 0x12345678;
228159248Srwatson
229159248Srwatsonstatic void
230159248Srwatsongenerate_ipc_token(const char *directory, const char *token_filename)
231159248Srwatson{
232159248Srwatson	token_t *ipc_token;
233159248Srwatson
234159248Srwatson	ipc_token = au_to_ipc(ipc_type, ipc_id);
235159248Srwatson	if (ipc_token == NULL)
236159248Srwatson		err(EX_UNAVAILABLE, "au_to_ipc");
237159248Srwatson	write_token(directory, token_filename, ipc_token);
238159248Srwatson}
239159248Srwatson
240159248Srwatsonstatic void
241159248Srwatsongenerate_ipc_record(const char *directory, const char *record_filename)
242159248Srwatson{
243159248Srwatson	token_t *ipc_token;
244159248Srwatson
245159248Srwatson	ipc_token = au_to_ipc(ipc_type, ipc_id);
246159248Srwatson	if (ipc_token == NULL)
247159248Srwatson		err(EX_UNAVAILABLE, "au_to_ipc");
248159248Srwatson	write_record(directory, record_filename, ipc_token, AUE_NULL);
249159248Srwatson}
250159248Srwatson
251159248Srwatsonstatic char		*path_token_path = "/test/this/is/a/test";
252159248Srwatson
253159248Srwatsonstatic void
254159248Srwatsongenerate_path_token(const char *directory, const char *token_filename)
255159248Srwatson{
256159248Srwatson	token_t *path_token;
257159248Srwatson
258159248Srwatson	path_token = au_to_path(path_token_path);
259159248Srwatson	if (path_token == NULL)
260159248Srwatson		err(EX_UNAVAILABLE, "au_to_path");
261159248Srwatson	write_token(directory, token_filename, path_token);
262159248Srwatson}
263159248Srwatson
264159248Srwatsonstatic void
265159248Srwatsongenerate_path_record(const char *directory, const char *record_filename)
266159248Srwatson{
267159248Srwatson	token_t *path_token;
268159248Srwatson
269159248Srwatson	path_token = au_to_path(path_token_path);
270159248Srwatson	if (path_token == NULL)
271159248Srwatson		err(EX_UNAVAILABLE, "au_to_path");
272159248Srwatson	write_record(directory, record_filename, path_token, AUE_NULL);
273159248Srwatson}
274159248Srwatson
275159248Srwatsonstatic au_id_t		 subject32_auid = 0x12345678;
276159248Srwatsonstatic uid_t		 subject32_euid = 0x01234567;
277159248Srwatsonstatic gid_t		 subject32_egid = 0x23456789;
278159248Srwatsonstatic uid_t		 subject32_ruid = 0x98765432;
279159248Srwatsonstatic gid_t		 subject32_rgid = 0x09876543;
280159248Srwatsonstatic pid_t		 subject32_pid = 0x13243546;
281159248Srwatsonstatic au_asid_t	 subject32_sid = 0x97867564;
282159248Srwatsonstatic au_tid_t		 subject32_tid = { 0x16593746 };
283159985Srwatsonstatic au_tid_addr_t	 subject32_tid_addr = { 0x16593746 };
284159248Srwatson
285159248Srwatsonstatic void
286159248Srwatsongenerate_subject32_token(const char *directory, const char *token_filename)
287159248Srwatson{
288159248Srwatson	token_t *subject32_token;
289159248Srwatson
290159248Srwatson	subject32_tid.machine = inet_addr("127.0.0.1");
291159248Srwatson
292159248Srwatson	subject32_token = au_to_subject32(subject32_auid, subject32_euid,
293159248Srwatson	    subject32_egid, subject32_ruid, subject32_rgid, subject32_pid,
294159248Srwatson	    subject32_sid, &subject32_tid);
295159248Srwatson	if (subject32_token == NULL)
296159248Srwatson		err(EX_UNAVAILABLE, "au_to_subject32");
297159248Srwatson	write_token(directory, token_filename, subject32_token);
298159248Srwatson}
299159248Srwatson
300159248Srwatsonstatic void
301159248Srwatsongenerate_subject32_record(const char *directory, const char *record_filename)
302159248Srwatson{
303159248Srwatson	token_t *subject32_token;
304159248Srwatson
305159248Srwatson	subject32_tid.machine = inet_addr("127.0.0.1");
306159248Srwatson
307159248Srwatson	subject32_token = au_to_subject32(subject32_auid, subject32_euid,
308159248Srwatson	    subject32_egid, subject32_ruid, subject32_rgid, subject32_pid,
309159248Srwatson	    subject32_sid, &subject32_tid);
310159248Srwatson	if (subject32_token == NULL)
311159248Srwatson		err(EX_UNAVAILABLE, "au_to_subject32");
312159248Srwatson	write_record(directory, record_filename, subject32_token, AUE_NULL);
313159248Srwatson}
314159248Srwatson
315159985Srwatsonstatic void
316159985Srwatsongenerate_subject32ex_token(const char *directory, const char *token_filename,
317159985Srwatson    u_int32_t type)
318159985Srwatson{
319159985Srwatson	token_t *subject32ex_token;
320159985Srwatson	char *buf;
321159985Srwatson
322159985Srwatson	buf = (char *)malloc(strlen(token_filename) + 6);
323159985Srwatson	if (type == AU_IPv6) {
324159985Srwatson		inet_pton(AF_INET6, "fe80::1", subject32_tid_addr.at_addr);
325159985Srwatson		subject32_tid_addr.at_type = AU_IPv6;
326159985Srwatson		sprintf(buf, "%s%s", token_filename, "-IPv6");
327159985Srwatson	} else {
328159985Srwatson		subject32_tid_addr.at_addr[0] = inet_addr("127.0.0.1");
329159985Srwatson		subject32_tid_addr.at_type = AU_IPv4;
330159985Srwatson		sprintf(buf, "%s%s", token_filename, "-IPv4");
331159985Srwatson	}
332159985Srwatson
333159985Srwatson	subject32ex_token = au_to_subject32_ex(subject32_auid, subject32_euid,
334159985Srwatson	    subject32_egid, subject32_ruid, subject32_rgid, subject32_pid,
335159985Srwatson	    subject32_sid, &subject32_tid_addr);
336159985Srwatson	if (subject32ex_token == NULL)
337159985Srwatson		err(EX_UNAVAILABLE, "au_to_subject32_ex");
338159985Srwatson	write_token(directory, buf, subject32ex_token);
339168777Srwatson	free(buf);
340159985Srwatson}
341159985Srwatson
342161630Srwatsonstatic void
343161630Srwatsongenerate_subject32ex_record(const char *directory, const char *record_filename,
344161630Srwatson    u_int32_t type)
345161630Srwatson{
346161630Srwatson	token_t *subject32ex_token;
347161630Srwatson	char *buf;
348161630Srwatson
349161630Srwatson	buf = (char *)malloc(strlen(record_filename) + 6);
350161630Srwatson	if (type == AU_IPv6) {
351161630Srwatson		inet_pton(AF_INET6, "fe80::1", subject32_tid_addr.at_addr);
352161630Srwatson		subject32_tid_addr.at_type = AU_IPv6;
353161630Srwatson		sprintf(buf, "%s%s", record_filename, "-IPv6");
354161630Srwatson	} else {
355161630Srwatson		subject32_tid_addr.at_addr[0] = inet_addr("127.0.0.1");
356161630Srwatson		subject32_tid_addr.at_type = AU_IPv4;
357161630Srwatson		sprintf(buf, "%s%s", record_filename, "-IPv4");
358161630Srwatson	}
359161630Srwatson
360161630Srwatson	subject32ex_token = au_to_subject32_ex(subject32_auid, subject32_euid,
361161630Srwatson	    subject32_egid, subject32_ruid, subject32_rgid, subject32_pid,
362161630Srwatson	    subject32_sid, &subject32_tid_addr);
363161630Srwatson	if (subject32ex_token == NULL)
364161630Srwatson		err(EX_UNAVAILABLE, "au_to_subject32_ex");
365161630Srwatson	write_record(directory, record_filename, subject32ex_token, AUE_NULL);
366168777Srwatson	free(buf);
367161630Srwatson}
368161630Srwatson
369159248Srwatsonstatic au_id_t		 process32_auid = 0x12345678;
370159248Srwatsonstatic uid_t		 process32_euid = 0x01234567;
371159248Srwatsonstatic gid_t		 process32_egid = 0x23456789;
372159248Srwatsonstatic uid_t		 process32_ruid = 0x98765432;
373159248Srwatsonstatic gid_t		 process32_rgid = 0x09876543;
374159248Srwatsonstatic pid_t		 process32_pid = 0x13243546;
375159248Srwatsonstatic au_asid_t	 process32_sid = 0x97867564;
376159248Srwatsonstatic au_tid_t		 process32_tid = { 0x16593746 };
377159985Srwatsonstatic au_tid_addr_t	 process32_tid_addr = { 0x16593746 };
378159248Srwatson
379159248Srwatsonstatic void
380159248Srwatsongenerate_process32_token(const char *directory, const char *token_filename)
381159248Srwatson{
382159248Srwatson	token_t *process32_token;
383159248Srwatson
384159248Srwatson	process32_tid.machine = inet_addr("127.0.0.1");
385159248Srwatson
386159248Srwatson	process32_token = au_to_process32(process32_auid, process32_euid,
387159248Srwatson	    process32_egid, process32_ruid, process32_rgid, process32_pid,
388159248Srwatson	    process32_sid, &process32_tid);
389159248Srwatson	if (process32_token == NULL)
390159248Srwatson		err(EX_UNAVAILABLE, "au_to_process32");
391159248Srwatson	write_token(directory, token_filename, process32_token);
392159248Srwatson}
393159248Srwatson
394159248Srwatsonstatic void
395159248Srwatsongenerate_process32_record(const char *directory, const char *record_filename)
396159248Srwatson{
397159248Srwatson	token_t *process32_token;
398159248Srwatson
399159248Srwatson	process32_tid.machine = inet_addr("127.0.0.1");
400159248Srwatson
401159248Srwatson	process32_token = au_to_process32(process32_auid, process32_euid,
402159248Srwatson	    process32_egid, process32_ruid, process32_rgid, process32_pid,
403159248Srwatson	    process32_sid, &process32_tid);
404159248Srwatson	if (process32_token == NULL)
405159248Srwatson		err(EX_UNAVAILABLE, "au_ti_process32");
406159248Srwatson	write_record(directory, record_filename, process32_token, AUE_NULL);
407159248Srwatson}
408159248Srwatson
409159985Srwatsonstatic void
410168777Srwatsongenerate_process32ex_token(const char *directory, const char *token_filename,
411168777Srwatson    u_int32_t type)
412159985Srwatson{
413159985Srwatson	token_t *process32ex_token;
414168777Srwatson	char *buf;
415159985Srwatson
416168777Srwatson	buf = (char *)malloc(strlen(token_filename) + 6);
417168777Srwatson	if (type == AU_IPv6) {
418168777Srwatson		inet_pton(AF_INET6, "fe80::1", process32_tid_addr.at_addr);
419168777Srwatson		process32_tid_addr.at_type = AU_IPv6;
420168777Srwatson		sprintf(buf, "%s%s", token_filename, "-IPv6");
421168777Srwatson	} else {
422168777Srwatson		process32_tid_addr.at_addr[0] = inet_addr("127.0.0.1");
423168777Srwatson		process32_tid_addr.at_type = AU_IPv4;
424168777Srwatson		sprintf(buf, "%s%s", token_filename, "-IPv4");
425168777Srwatson	}
426159985Srwatson
427159985Srwatson	process32ex_token = au_to_process32_ex(process32_auid, process32_euid,
428159985Srwatson	    process32_egid, process32_ruid, process32_rgid, process32_pid,
429159985Srwatson	    process32_sid, &process32_tid_addr);
430159985Srwatson	if (process32ex_token == NULL)
431159985Srwatson		err(EX_UNAVAILABLE, "au_to_process32_ex");
432168777Srwatson	write_token(directory, buf, process32ex_token);
433168777Srwatson	free(buf);
434159985Srwatson}
435159985Srwatson
436161630Srwatsonstatic void
437168777Srwatsongenerate_process32ex_record(const char *directory, const char *record_filename,
438168777Srwatson    u_int32_t type)
439161630Srwatson{
440161630Srwatson	token_t *process32ex_token;
441168777Srwatson	char *buf;
442161630Srwatson
443168777Srwatson	buf = (char *)malloc(strlen(record_filename) + 6);
444168777Srwatson	if (type == AU_IPv6) {
445168777Srwatson		inet_pton(AF_INET6, "fe80::1", process32_tid_addr.at_addr);
446168777Srwatson		process32_tid_addr.at_type = AU_IPv6;
447168777Srwatson		sprintf(buf, "%s%s", record_filename, "-IPv6");
448168777Srwatson	} else {
449168777Srwatson		process32_tid_addr.at_addr[0] = inet_addr("127.0.0.1");
450168777Srwatson		process32_tid_addr.at_type = AU_IPv4;
451168777Srwatson		sprintf(buf, "%s%s", record_filename, "-IPv4");
452168777Srwatson	}
453161630Srwatson
454161630Srwatson	process32ex_token = au_to_process32_ex(process32_auid, process32_euid,
455161630Srwatson	    process32_egid, process32_ruid, process32_rgid, process32_pid,
456161630Srwatson	    process32_sid, &process32_tid_addr);
457161630Srwatson	if (process32ex_token == NULL)
458161630Srwatson		err(EX_UNAVAILABLE, "au_to_process32_ex");
459168777Srwatson	write_record(directory, buf, process32ex_token, AUE_NULL);
460168777Srwatson	free(buf);
461161630Srwatson}
462161630Srwatson
463168777Srwatsonstatic au_id_t		 process64_auid = 0x12345678;
464168777Srwatsonstatic uid_t		 process64_euid = 0x01234567;
465168777Srwatsonstatic gid_t		 process64_egid = 0x23456789;
466168777Srwatsonstatic uid_t		 process64_ruid = 0x98765432;
467168777Srwatsonstatic gid_t		 process64_rgid = 0x09876543;
468168777Srwatsonstatic pid_t		 process64_pid = 0x13243546;
469168777Srwatsonstatic au_asid_t	 process64_sid = 0x97867564;
470168777Srwatsonstatic au_tid_t		 process64_tid = { 0x16593746 };
471168777Srwatsonstatic au_tid_addr_t	 process64_tid_addr = { 0x16593746 };
472168777Srwatson
473168777Srwatsonstatic void
474168777Srwatsongenerate_process64_token(const char *directory, const char *token_filename)
475168777Srwatson{
476168777Srwatson	token_t *process64_token;
477168777Srwatson
478168777Srwatson	process64_tid.machine = inet_addr("127.0.0.1");
479168777Srwatson
480168777Srwatson	process64_token = au_to_process64(process64_auid, process64_euid,
481168777Srwatson	    process64_egid, process64_ruid, process64_rgid, process64_pid,
482168777Srwatson	    process64_sid, &process64_tid);
483168777Srwatson	if (process64_token == NULL)
484168777Srwatson		err(EX_UNAVAILABLE, "au_to_process64");
485168777Srwatson	write_token(directory, token_filename, process64_token);
486168777Srwatson}
487168777Srwatson
488168777Srwatsonstatic void
489168777Srwatsongenerate_process64_record(const char *directory, const char *record_filename)
490168777Srwatson{
491168777Srwatson	token_t *process64_token;
492168777Srwatson
493168777Srwatson	process64_tid.machine = inet_addr("127.0.0.1");
494168777Srwatson
495168777Srwatson	process64_token = au_to_process64(process64_auid, process64_euid,
496168777Srwatson	    process64_egid, process64_ruid, process64_rgid, process64_pid,
497168777Srwatson	    process64_sid, &process64_tid);
498168777Srwatson	if (process64_token == NULL)
499168777Srwatson		err(EX_UNAVAILABLE, "au_ti_process64");
500168777Srwatson	write_record(directory, record_filename, process64_token, AUE_NULL);
501168777Srwatson}
502168777Srwatson
503168777Srwatsonstatic void
504168777Srwatsongenerate_process64ex_token(const char *directory, const char *token_filename,
505168777Srwatson    u_int32_t type)
506168777Srwatson{
507168777Srwatson	token_t *process64ex_token;
508168777Srwatson	char *buf;
509168777Srwatson
510168777Srwatson	buf = (char *)malloc(strlen(token_filename) + 6);
511168777Srwatson	if (type == AU_IPv6) {
512168777Srwatson		inet_pton(AF_INET6, "fe80::1", process64_tid_addr.at_addr);
513168777Srwatson		process64_tid_addr.at_type = AU_IPv6;
514168777Srwatson		sprintf(buf, "%s%s", token_filename, "-IPv6");
515168777Srwatson	} else {
516168777Srwatson		process64_tid_addr.at_addr[0] = inet_addr("127.0.0.1");
517168777Srwatson		process64_tid_addr.at_type = AU_IPv4;
518168777Srwatson		sprintf(buf, "%s%s", token_filename, "-IPv4");
519168777Srwatson	}
520168777Srwatson
521168777Srwatson	process64ex_token = au_to_process64_ex(process64_auid, process64_euid,
522168777Srwatson	    process64_egid, process64_ruid, process64_rgid, process64_pid,
523168777Srwatson	    process64_sid, &process64_tid_addr);
524168777Srwatson	if (process64ex_token == NULL)
525168777Srwatson		err(EX_UNAVAILABLE, "au_to_process64_ex");
526168777Srwatson	write_token(directory, buf, process64ex_token);
527168777Srwatson	free(buf);
528168777Srwatson}
529168777Srwatson
530168777Srwatsonstatic void
531168777Srwatsongenerate_process64ex_record(const char *directory, const char *record_filename,
532168777Srwatson    u_int32_t type)
533168777Srwatson{
534168777Srwatson	token_t *process64ex_token;
535168777Srwatson	char *buf;
536168777Srwatson
537168777Srwatson	buf = (char *)malloc(strlen(record_filename) + 6);
538168777Srwatson	if (type == AU_IPv6) {
539168777Srwatson		inet_pton(AF_INET6, "fe80::1", process64_tid_addr.at_addr);
540168777Srwatson		process64_tid_addr.at_type = AU_IPv6;
541168777Srwatson		sprintf(buf, "%s%s", record_filename, "-IPv6");
542168777Srwatson	} else {
543168777Srwatson		process64_tid_addr.at_addr[0] = inet_addr("127.0.0.1");
544168777Srwatson		process64_tid_addr.at_type = AU_IPv4;
545168777Srwatson		sprintf(buf, "%s%s", record_filename, "-IPv4");
546168777Srwatson	}
547168777Srwatson
548168777Srwatson	process64ex_token = au_to_process64_ex(process64_auid, process64_euid,
549168777Srwatson	    process64_egid, process64_ruid, process64_rgid, process64_pid,
550168777Srwatson	    process64_sid, &process64_tid_addr);
551168777Srwatson	if (process64ex_token == NULL)
552168777Srwatson		err(EX_UNAVAILABLE, "au_to_process64_ex");
553168777Srwatson	write_record(directory, buf, process64ex_token, AUE_NULL);
554168777Srwatson	free(buf);
555168777Srwatson}
556168777Srwatson
557186647Srwatsonstatic char		 return32_status = EINVAL;
558159248Srwatsonstatic uint32_t		 return32_ret = 0x12345678;
559159248Srwatson
560159248Srwatsonstatic void
561159248Srwatsongenerate_return32_token(const char *directory, const char *token_filename)
562159248Srwatson{
563159248Srwatson	token_t *return32_token;
564159248Srwatson
565186647Srwatson	return32_token = au_to_return32(au_errno_to_bsm(return32_status),
566186647Srwatson	    return32_ret);
567159248Srwatson	if (return32_token == NULL)
568159248Srwatson		err(EX_UNAVAILABLE, "au_to_return32");
569159248Srwatson	write_token(directory, token_filename, return32_token);
570159248Srwatson}
571159248Srwatson
572159248Srwatsonstatic void
573159248Srwatsongenerate_return32_record(const char *directory, const char *record_filename)
574159248Srwatson{
575159248Srwatson	token_t *return32_token;
576159248Srwatson
577186647Srwatson	return32_token = au_to_return32(au_errno_to_bsm(return32_status),
578186647Srwatson	    return32_ret);
579159248Srwatson	if (return32_token == NULL)
580159248Srwatson		err(EX_UNAVAILABLE, "au_to_return32");
581159248Srwatson	write_record(directory, record_filename, return32_token, AUE_NULL);
582159248Srwatson}
583159248Srwatson
584159248Srwatsonstatic char		*text_token_text = "This is a test.";
585159248Srwatson
586159248Srwatsonstatic void
587159248Srwatsongenerate_text_token(const char *directory, const char *token_filename)
588159248Srwatson{
589159248Srwatson	token_t *text_token;
590159248Srwatson
591159248Srwatson	text_token = au_to_text(text_token_text);
592159248Srwatson	if (text_token == NULL)
593159248Srwatson		err(EX_UNAVAILABLE, "au_to_text");
594159248Srwatson	write_token(directory, token_filename, text_token);
595159248Srwatson}
596159248Srwatson
597159248Srwatsonstatic void
598159248Srwatsongenerate_text_record(const char *directory, const char *record_filename)
599159248Srwatson{
600159248Srwatson	token_t *text_token;
601159248Srwatson
602159248Srwatson	text_token = au_to_text(text_token_text);
603159248Srwatson	if (text_token == NULL)
604159248Srwatson		err(EX_UNAVAILABLE, "au_to_text");
605159248Srwatson	write_record(directory, record_filename, text_token, AUE_NULL);
606159248Srwatson}
607159248Srwatson
608159248Srwatsonstatic char		 opaque_token_data[] = {0xaa, 0xbb, 0xcc, 0xdd};
609159248Srwatsonstatic int		 opaque_token_bytes = sizeof(opaque_token_data);
610159248Srwatson
611159248Srwatsonstatic void
612159248Srwatsongenerate_opaque_token(const char *directory, const char *token_filename)
613159248Srwatson{
614159248Srwatson	token_t *opaque_token;
615159248Srwatson
616159248Srwatson	opaque_token = au_to_opaque(opaque_token_data, opaque_token_bytes);
617159248Srwatson	if (opaque_token == NULL)
618159248Srwatson		err(EX_UNAVAILABLE, "au_to_opaque");
619159248Srwatson	write_token(directory, token_filename, opaque_token);
620159248Srwatson}
621159248Srwatson
622159248Srwatsonstatic void
623159248Srwatsongenerate_opaque_record(const char *directory, const char *record_filename)
624159248Srwatson{
625159248Srwatson	token_t *opaque_token;
626159248Srwatson
627159248Srwatson	opaque_token = au_to_opaque(opaque_token_data, opaque_token_bytes);
628159248Srwatson	if (opaque_token == NULL)
629159248Srwatson		err(EX_UNAVAILABLE, "au_to_opaque");
630159248Srwatson	write_record(directory, record_filename, opaque_token, AUE_NULL);
631159248Srwatson}
632159248Srwatson
633159248Srwatsonstatic struct in_addr	 in_addr_token_addr;
634159248Srwatson
635159248Srwatsonstatic void
636159248Srwatsongenerate_in_addr_token(const char *directory, const char *token_filename)
637159248Srwatson{
638159248Srwatson	token_t *in_addr_token;
639159248Srwatson
640159248Srwatson	in_addr_token_addr.s_addr = inet_addr("192.168.100.15");
641159248Srwatson
642159248Srwatson	in_addr_token = au_to_in_addr(&in_addr_token_addr);
643159248Srwatson	if (in_addr_token == NULL)
644159248Srwatson		err(EX_UNAVAILABLE, "au_to_in_addr");
645159248Srwatson	write_token(directory, token_filename, in_addr_token);
646159248Srwatson}
647159248Srwatson
648159248Srwatsonstatic void
649159248Srwatsongenerate_in_addr_record(const char *directory, const char *record_filename)
650159248Srwatson{
651159248Srwatson	token_t *in_addr_token;
652159248Srwatson
653159248Srwatson	in_addr_token_addr.s_addr = inet_addr("192.168.100.15");
654159248Srwatson
655159248Srwatson	in_addr_token = au_to_in_addr(&in_addr_token_addr);
656159248Srwatson	if (in_addr_token == NULL)
657159248Srwatson		err(EX_UNAVAILABLE, "au_to_in_addr");
658159248Srwatson	write_record(directory, record_filename, in_addr_token, AUE_NULL);
659159248Srwatson}
660159248Srwatson
661159248Srwatsonstatic struct ip	 ip_token_ip;
662159248Srwatsonstatic u_char		 ip_token_ip_v = 4;
663159248Srwatsonstatic uint16_t		 ip_token_ip_id = 0x5478;
664159248Srwatsonstatic u_char		 ip_token_ip_ttl = 64;
665159248Srwatsonstatic u_char		 ip_token_ip_p = IPPROTO_ICMP;
666159248Srwatsonstatic struct in_addr	 ip_token_ip_src;
667159248Srwatsonstatic struct in_addr	 ip_token_ip_dst;
668159248Srwatson
669159248Srwatsonstatic void
670159248Srwatsongenerate_ip_token(const char *directory, const char *token_filename)
671159248Srwatson{
672159248Srwatson	token_t *ip_token;
673159248Srwatson
674159248Srwatson	ip_token_ip_src.s_addr = inet_addr("192.168.100.155");
675159248Srwatson	ip_token_ip_dst.s_addr = inet_addr("192.168.110.48");
676159248Srwatson
677159248Srwatson	memset(&ip_token_ip, 0, sizeof(ip_token_ip));
678159248Srwatson	ip_token_ip.ip_v = ip_token_ip_v;
679159248Srwatson	ip_token_ip.ip_len = htons(sizeof(ip_token_ip));
680159248Srwatson	ip_token_ip.ip_id = htons(ip_token_ip_id);
681159248Srwatson	ip_token_ip.ip_ttl = ip_token_ip_ttl;
682159248Srwatson	ip_token_ip.ip_p = ip_token_ip_p;
683159248Srwatson	ip_token_ip.ip_src = ip_token_ip_src;
684159248Srwatson	ip_token_ip.ip_dst = ip_token_ip_dst;
685159248Srwatson
686159248Srwatson	ip_token = au_to_ip(&ip_token_ip);
687159248Srwatson	if (ip_token == NULL)
688159248Srwatson		err(EX_UNAVAILABLE, "au_to_ip");
689159248Srwatson	write_token(directory, token_filename, ip_token);
690159248Srwatson}
691159248Srwatson
692159248Srwatsonstatic void
693159248Srwatsongenerate_ip_record(const char *directory, const char *record_filename)
694159248Srwatson{
695159248Srwatson	token_t *ip_token;
696159248Srwatson
697159248Srwatson	ip_token_ip_src.s_addr = inet_addr("192.168.100.155");
698159248Srwatson	ip_token_ip_dst.s_addr = inet_addr("192.168.110.48");
699159248Srwatson
700159248Srwatson	memset(&ip_token_ip, 0, sizeof(ip_token_ip));
701159248Srwatson	ip_token_ip.ip_v = ip_token_ip_v;
702159248Srwatson	ip_token_ip.ip_len = htons(sizeof(ip_token_ip));
703159248Srwatson	ip_token_ip.ip_id = htons(ip_token_ip_id);
704159248Srwatson	ip_token_ip.ip_ttl = ip_token_ip_ttl;
705159248Srwatson	ip_token_ip.ip_p = ip_token_ip_p;
706159248Srwatson	ip_token_ip.ip_src = ip_token_ip_src;
707159248Srwatson	ip_token_ip.ip_dst = ip_token_ip_dst;
708159248Srwatson
709159248Srwatson	ip_token = au_to_ip(&ip_token_ip);
710159248Srwatson	if (ip_token == NULL)
711159248Srwatson		err(EX_UNAVAILABLE, "au_to_ip");
712159248Srwatson	write_record(directory, record_filename, ip_token, AUE_NULL);
713159248Srwatson}
714159248Srwatson
715159248Srwatsonstatic u_int16_t		 iport_token_iport;
716159248Srwatson
717159248Srwatsonstatic void
718159248Srwatsongenerate_iport_token(const char *directory, const char *token_filename)
719159248Srwatson{
720159248Srwatson	token_t *iport_token;
721159248Srwatson
722159248Srwatson	iport_token_iport = htons(80);
723159248Srwatson
724159248Srwatson	iport_token = au_to_iport(iport_token_iport);
725159248Srwatson	if (iport_token == NULL)
726159248Srwatson		err(EX_UNAVAILABLE, "au_to_iport");
727159248Srwatson	write_token(directory, token_filename, iport_token);
728159248Srwatson}
729159248Srwatson
730159248Srwatsonstatic void
731159248Srwatsongenerate_iport_record(const char *directory, const char *record_filename)
732159248Srwatson{
733159248Srwatson	token_t *iport_token;
734159248Srwatson
735159248Srwatson	iport_token_iport = htons(80);
736159248Srwatson
737159248Srwatson	iport_token = au_to_iport(iport_token_iport);
738159248Srwatson	if (iport_token == NULL)
739159248Srwatson		err(EX_UNAVAILABLE, "au_to_iport");
740159248Srwatson	write_record(directory, record_filename, iport_token, AUE_NULL);
741159248Srwatson}
742159248Srwatson
743159248Srwatsonstatic char	 arg32_token_n = 3;
744159248Srwatsonstatic char	*arg32_token_text = "test_arg32_token";
745159248Srwatsonstatic uint32_t	 arg32_token_v = 0xabcdef00;
746159248Srwatson
747159248Srwatsonstatic void
748159248Srwatsongenerate_arg32_token(const char *directory, const char *token_filename)
749159248Srwatson{
750159248Srwatson	token_t *arg32_token;
751159248Srwatson
752159248Srwatson	arg32_token = au_to_arg32(arg32_token_n, arg32_token_text,
753159248Srwatson	    arg32_token_v);
754159248Srwatson	if (arg32_token == NULL)
755159248Srwatson		err(EX_UNAVAILABLE, "au_to_arg32");
756159248Srwatson	write_token(directory, token_filename, arg32_token);
757159248Srwatson}
758159248Srwatson
759159248Srwatsonstatic void
760159248Srwatsongenerate_arg32_record(const char *directory, const char *record_filename)
761159248Srwatson{
762159248Srwatson	token_t *arg32_token;
763159248Srwatson
764159248Srwatson	arg32_token = au_to_arg32(arg32_token_n, arg32_token_text,
765159248Srwatson	    arg32_token_v);
766159248Srwatson	if (arg32_token == NULL)
767159248Srwatson		err(EX_UNAVAILABLE, "au_to_arg32");
768159248Srwatson	write_record(directory, record_filename, arg32_token, AUE_NULL);
769159248Srwatson}
770159248Srwatson
771159248Srwatsonstatic long	 seq_audit_count = 0x12345678;
772159248Srwatson
773159248Srwatsonstatic void
774159248Srwatsongenerate_seq_token(const char *directory, const char *token_filename)
775159248Srwatson{
776159248Srwatson	token_t *seq_token;
777159248Srwatson
778159248Srwatson	seq_token = au_to_seq(seq_audit_count);
779159248Srwatson	if (seq_token == NULL)
780159248Srwatson		err(EX_UNAVAILABLE, "au_to_seq");
781159248Srwatson	write_token(directory, token_filename, seq_token);
782159248Srwatson}
783159248Srwatson
784159248Srwatsonstatic void
785159248Srwatsongenerate_seq_record(const char *directory, const char *record_filename)
786159248Srwatson{
787159248Srwatson	token_t *seq_token;
788159248Srwatson
789159248Srwatson	seq_token = au_to_seq(seq_audit_count);
790159248Srwatson	if (seq_token == NULL)
791159248Srwatson		err(EX_UNAVAILABLE, "au_to_seq");
792159248Srwatson	write_record(directory, record_filename, seq_token, AUE_NULL);
793159248Srwatson}
794159248Srwatson
795187214Srwatson#if 0
796159248Srwatson/*
797159248Srwatson * AUT_ACL
798159248Srwatson */
799159248Srwatson
800159248Srwatsonstatic void
801159248Srwatsongenerate_attr_token(const char *directory, const char *token_filename)
802159248Srwatson{
803159248Srwatson	token_t *attr_token;
804159248Srwatson
805159248Srwatson}
806159248Srwatson
807159248Srwatsonstatic void
808159248Srwatsongenerate_attr_record(const char *directory, const char *record_filename)
809159248Srwatson{
810159248Srwatson	token_t *attr_token;
811159248Srwatson
812159248Srwatson}
813159248Srwatson
814159248Srwatsonstatic void
815159248Srwatsongenerate_ipc_perm_token(const char *directory, const char *token_filename)
816159248Srwatson{
817159248Srwatson	token_t *ipc_perm_token;
818159248Srwatson
819159248Srwatson}
820159248Srwatson
821159248Srwatsonstatic void
822159248Srwatsongenerate_ipc_perm_record(const char *directory, const char *record_filename)
823159248Srwatson{
824159248Srwatson	token_t *ipc_perm_token;
825159248Srwatson
826159248Srwatson}
827187214Srwatson#endif
828159248Srwatson
829187214Srwatson#if 0
830159248Srwatson/*
831159248Srwatson * AUT_LABEL
832159248Srwatson */
833159248Srwatson
834159248Srwatsonstatic void
835159248Srwatsongenerate_groups_token(const char *directory, const char *token_filename)
836159248Srwatson{
837159248Srwatson	token_t *groups_token;
838159248Srwatson
839159248Srwatson}
840159248Srwatson
841159248Srwatsonstatic void
842159248Srwatsongenerate_groups_record(const char *directory, const char *record_filename)
843159248Srwatson{
844159248Srwatson	token_t *groups_token;
845159248Srwatson
846159248Srwatson}
847187214Srwatson#endif
848159248Srwatson
849159248Srwatson/*
850159248Srwatson * AUT_ILABEL
851159248Srwatson */
852159248Srwatson
853159248Srwatson/*
854159248Srwatson * AUT_SLABEL
855159248Srwatson */
856159248Srwatson
857159248Srwatson/*
858159248Srwatson * AUT_CLEAR
859159248Srwatson */
860159248Srwatson
861159248Srwatson/*
862159248Srwatson * AUT_PRIV
863159248Srwatson */
864159248Srwatson
865159248Srwatson/*
866159248Srwatson * AUT_UPRIV
867159248Srwatson */
868159248Srwatson
869159248Srwatson/*
870159248Srwatson * AUT_LIAISON
871159248Srwatson */
872159248Srwatson
873159248Srwatson/*
874159248Srwatson * AUT_NEWGROUPS
875159248Srwatson */
876159248Srwatson
877159248Srwatson/*
878159248Srwatson * AUT_EXEC_ARGS
879159248Srwatson */
880159248Srwatson
881159248Srwatson/*
882159248Srwatson * AUT_EXEC_ENV
883159248Srwatson */
884159248Srwatson
885187214Srwatson#if 0
886159248Srwatsonstatic void
887159248Srwatsongenerate_attr32_token(const char *directory, const char *token_filename)
888159248Srwatson{
889159248Srwatson	token_t *attr32_token;
890159248Srwatson
891159248Srwatson}
892159248Srwatson
893159248Srwatsonstatic void
894159248Srwatsongenerate_attr32_record(const char *directory, const char *record_filename)
895159248Srwatson{
896159248Srwatson	token_t *attr32_token;
897159248Srwatson
898159248Srwatson}
899187214Srwatson#endif
900159248Srwatson
901168777Srwatsonstatic char	*zonename_sample = "testzone";
902168777Srwatson
903168777Srwatsonstatic void
904168777Srwatsongenerate_zonename_token(const char *directory, const char *token_filename)
905168777Srwatson{
906168777Srwatson	token_t *zonename_token;
907168777Srwatson
908168777Srwatson	zonename_token = au_to_zonename(zonename_sample);
909168777Srwatson	if (zonename_token == NULL)
910168777Srwatson		err(EX_UNAVAILABLE, "au_to_zonename");
911168777Srwatson	write_token(directory, token_filename, zonename_token);
912168777Srwatson}
913168777Srwatson
914168777Srwatsonstatic void
915168777Srwatsongenerate_zonename_record(const char *directory, const char *record_filename)
916168777Srwatson{
917168777Srwatson	token_t *zonename_token;
918168777Srwatson
919168777Srwatson	zonename_token = au_to_zonename(zonename_sample);
920168777Srwatson	if (zonename_token == NULL)
921168777Srwatson		err(EX_UNAVAILABLE, "au_to_zonename");
922168777Srwatson	write_record(directory, record_filename, zonename_token, AUE_NULL);
923168777Srwatson}
924168777Srwatson
925187214Srwatsonstatic u_short socketex_domain = PF_INET;
926186647Srwatsonstatic u_short socketex_type = SOCK_STREAM;
927186647Srwatsonstatic struct sockaddr_in socketex_laddr, socketex_raddr;
928186647Srwatson
929186647Srwatsonstatic void
930186647Srwatsongenerate_socketex_token(const char *directory, const char *token_filename)
931186647Srwatson{
932186647Srwatson	token_t *socketex_token;
933186647Srwatson
934186647Srwatson	bzero(&socketex_laddr, sizeof(socketex_laddr));
935186647Srwatson	socketex_laddr.sin_family = AF_INET;
936186647Srwatson	socketex_laddr.sin_len = sizeof(socketex_laddr);
937186647Srwatson	socketex_laddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
938186647Srwatson
939186647Srwatson	bzero(&socketex_raddr, sizeof(socketex_raddr));
940186647Srwatson	socketex_raddr.sin_family = AF_INET;
941186647Srwatson	socketex_raddr.sin_len = sizeof(socketex_raddr);
942186647Srwatson	socketex_raddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
943186647Srwatson
944187214Srwatson	socketex_token = au_to_socket_ex(au_domain_to_bsm(socketex_domain),
945187214Srwatson	    au_socket_type_to_bsm(socketex_type),
946186647Srwatson	    (struct sockaddr *)&socketex_laddr,
947186647Srwatson	    (struct sockaddr *)&socketex_raddr);
948186647Srwatson	if (socketex_token == NULL)
949186647Srwatson		err(EX_UNAVAILABLE, "au_to_socket_ex");
950186647Srwatson	write_token(directory, token_filename, socketex_token);
951186647Srwatson}
952186647Srwatson
953186647Srwatsonstatic void
954186647Srwatsongenerate_socketex_record(const char *directory, const char *record_filename)
955186647Srwatson{
956186647Srwatson	token_t *socketex_token;
957186647Srwatson
958186647Srwatson	bzero(&socketex_laddr, sizeof(socketex_laddr));
959186647Srwatson	socketex_laddr.sin_family = AF_INET;
960186647Srwatson	socketex_laddr.sin_len = sizeof(socketex_laddr);
961186647Srwatson	socketex_laddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
962186647Srwatson
963186647Srwatson	bzero(&socketex_raddr, sizeof(socketex_raddr));
964186647Srwatson	socketex_raddr.sin_family = AF_INET;
965186647Srwatson	socketex_raddr.sin_len = sizeof(socketex_raddr);
966186647Srwatson	socketex_raddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
967186647Srwatson
968187214Srwatson	socketex_token = au_to_socket_ex(au_domain_to_bsm(socketex_domain),
969187214Srwatson	    au_socket_type_to_bsm(socketex_type),
970186647Srwatson	    (struct sockaddr *)&socketex_laddr,
971186647Srwatson	    (struct sockaddr *)&socketex_raddr);
972186647Srwatson	if (socketex_token == NULL)
973186647Srwatson		err(EX_UNAVAILABLE, "au_to_socket_ex");
974186647Srwatson	write_record(directory, record_filename, socketex_token, AUE_NULL);
975186647Srwatson}
976186647Srwatson
977186647Srwatson/*
978186647Srwatson * Generate a series of error-number specific return tokens in records.
979186647Srwatson */
980186647Srwatsonstatic void
981186647Srwatsongenerate_error_record(const char *directory, const char *filename, int error)
982186647Srwatson{
983186647Srwatson	char pathname[PATH_MAX];
984186647Srwatson	token_t *return32_token;
985186647Srwatson
986186647Srwatson	return32_token = au_to_return32(au_errno_to_bsm(error), -1);
987186647Srwatson	if (return32_token == NULL)
988186647Srwatson		err(EX_UNAVAILABLE, "au_to_return32");
989186647Srwatson	(void)snprintf(pathname, PATH_MAX, "%s_record", filename);
990186647Srwatson	write_record(directory, pathname, return32_token, AUE_NULL);
991186647Srwatson}
992186647Srwatson
993186647Srwatson/*
994186647Srwatson * Not all the error numbers, just a few present on all platforms for now.
995186647Srwatson */
996186647Srwatsonconst struct {
997186647Srwatson	int error_number;
998186647Srwatson	const char *error_name;
999186647Srwatson} error_list[] = {
1000186647Srwatson	{ EPERM, "EPERM" },
1001186647Srwatson	{ ENOENT, "ENOENT" },
1002186647Srwatson	{ ESRCH, "ESRCH" },
1003186647Srwatson	{ EINTR, "EINTR" },
1004186647Srwatson	{ EIO, "EIO" },
1005186647Srwatson	{ ENXIO, "ENXIO" },
1006186647Srwatson	{ E2BIG, "E2BIG" },
1007186647Srwatson	{ ENOEXEC, "ENOEXEC" },
1008186647Srwatson	{ EBADF, "EBADF" },
1009186647Srwatson	{ ECHILD, "ECHILD" },
1010186647Srwatson	{ EDEADLK, "EDEADLK" },
1011186647Srwatson	{ ENOMEM, "ENOMEM" },
1012186647Srwatson	{ EACCES, "EACCES" },
1013186647Srwatson	{ EFAULT, "EFAULT" },
1014186647Srwatson	{ ENOTBLK, "ENOTBLK" },
1015186647Srwatson	{ EBUSY, "EBUSY" },
1016186647Srwatson	{ EEXIST, "EEXIST" },
1017186647Srwatson	{ EXDEV, "EXDEV" },
1018186647Srwatson	{ ENODEV, "ENODEV" },
1019186647Srwatson	{ ENOTDIR, "ENOTDIR" },
1020186647Srwatson	{ EISDIR, "EISDIR" },
1021186647Srwatson	{ EINVAL, "EINVAL" },
1022186647Srwatson	{ ENFILE, "ENFILE" },
1023186647Srwatson	{ EMFILE, "EMFILE" },
1024186647Srwatson	{ ENOTTY, "ENOTTY" },
1025186647Srwatson	{ ETXTBSY, "ETXTBSY" },
1026186647Srwatson	{ EFBIG, "EFBIG" },
1027186647Srwatson	{ ENOSPC, "ENOSPC" },
1028186647Srwatson	{ ESPIPE, "ESPIPE" },
1029186647Srwatson	{ EROFS, "EROFS" },
1030186647Srwatson	{ EMLINK, "EMLINK" },
1031186647Srwatson	{ EPIPE, "EPIPE" }
1032186647Srwatson};
1033186647Srwatsonconst int error_list_count = sizeof(error_list)/sizeof(error_list[0]);
1034186647Srwatson
1035186647Srwatsonstatic void
1036186647Srwatsondo_error_records(const char *directory)
1037186647Srwatson{
1038186647Srwatson	int i;
1039186647Srwatson
1040186647Srwatson	for (i = 0; i < error_list_count; i++)
1041186647Srwatson		generate_error_record(directory, error_list[i].error_name,
1042186647Srwatson		    error_list[i].error_number);
1043186647Srwatson}
1044186647Srwatson
1045159248Srwatsonint
1046159248Srwatsonmain(int argc, char *argv[])
1047159248Srwatson{
1048159248Srwatson	const char *directory;
1049159248Srwatson	int ch;
1050159248Srwatson
1051159248Srwatson	while ((ch = getopt(argc, argv, "rt")) != -1) {
1052159248Srwatson		switch (ch) {
1053159248Srwatson		case 'r':
1054159248Srwatson			do_records++;
1055159248Srwatson			break;
1056159248Srwatson
1057159248Srwatson		case 't':
1058159248Srwatson			do_tokens++;
1059159248Srwatson			break;
1060159248Srwatson
1061159248Srwatson		default:
1062159248Srwatson			usage();
1063159248Srwatson		}
1064159248Srwatson	}
1065159248Srwatson
1066159248Srwatson	argc -= optind;
1067159248Srwatson	argv += optind;
1068159248Srwatson
1069159248Srwatson	if (argc != 1)
1070159248Srwatson		usage();
1071159248Srwatson
1072159248Srwatson	directory = argv[0];
1073159248Srwatson
1074159248Srwatson	if (mkdir(directory, 0755) < 0 && errno != EEXIST)
1075159248Srwatson		err(EX_OSERR, "mkdir: %s", directory);
1076159248Srwatson
1077159248Srwatson	if (do_tokens) {
1078159248Srwatson		generate_file_token(directory, "file_token");
1079159248Srwatson		generate_trailer_token(directory, "trailer_token");
1080159248Srwatson		generate_header32_token(directory, "header32_token");
1081159248Srwatson		generate_data_token(directory, "data_token");
1082159248Srwatson		generate_ipc_token(directory, "ipc_token");
1083159248Srwatson		generate_path_token(directory, "path_token");
1084159248Srwatson		generate_subject32_token(directory, "subject32_token");
1085168777Srwatson		generate_subject32ex_token(directory, "subject32ex_token",
1086168777Srwatson		    AU_IPv4);
1087168777Srwatson		generate_subject32ex_token(directory, "subject32ex_token",
1088168777Srwatson		    AU_IPv6);
1089159248Srwatson		generate_process32_token(directory, "process32_token");
1090168777Srwatson		generate_process32ex_token(directory, "process32ex_token",
1091168777Srwatson		    AU_IPv4);
1092168777Srwatson		generate_process32ex_token(directory, "process32ex_token",
1093168777Srwatson		    AU_IPv6);
1094168777Srwatson		generate_process64_token(directory, "process64_token");
1095168777Srwatson		generate_process64ex_token(directory, "process64ex_token",
1096168777Srwatson		    AU_IPv4);
1097168777Srwatson		generate_process64ex_token(directory, "process64ex_token",
1098168777Srwatson		    AU_IPv6);
1099159248Srwatson		generate_return32_token(directory, "return32_token");
1100159248Srwatson		generate_text_token(directory, "text_token");
1101159248Srwatson		generate_opaque_token(directory, "opaque_token");
1102159248Srwatson		generate_in_addr_token(directory, "in_addr_token");
1103159248Srwatson		generate_ip_token(directory, "ip_token");
1104159248Srwatson		generate_iport_token(directory, "iport_token");
1105159248Srwatson		generate_arg32_token(directory, "arg32_token");
1106159248Srwatson		generate_seq_token(directory, "seq_token");
1107187214Srwatson#if 0
1108159248Srwatson		generate_attr_token(directory,  "attr_token");
1109159248Srwatson		generate_ipc_perm_token(directory, "ipc_perm_token");
1110159248Srwatson		generate_groups_token(directory, "groups_token");
1111159248Srwatson		generate_attr32_token(directory, "attr32_token");
1112187214Srwatson#endif
1113168777Srwatson		generate_zonename_token(directory, "zonename_token");
1114186647Srwatson		generate_socketex_token(directory, "socketex_token");
1115159248Srwatson	}
1116159248Srwatson
1117159248Srwatson	if (do_records) {
1118161630Srwatson		generate_file_record(directory, "file_record");
1119161630Srwatson		generate_data_record(directory, "data_record");
1120161630Srwatson		generate_ipc_record(directory, "ipc_record");
1121161630Srwatson		generate_path_record(directory, "path_record");
1122161630Srwatson		generate_subject32_record(directory, "subject32_record");
1123161630Srwatson		generate_subject32ex_record(directory, "subject32ex_record",
1124161630Srwatson		    AU_IPv4);
1125161630Srwatson		generate_subject32ex_record(directory, "subject32ex_record",
1126161630Srwatson		    AU_IPv6);
1127161630Srwatson		generate_process32_record(directory, "process32_record");
1128168777Srwatson		generate_process32ex_record(directory, "process32ex_record",
1129168777Srwatson		    AU_IPv4);
1130168777Srwatson		generate_process32ex_record(directory, "process32ex_record",
1131168777Srwatson		    AU_IPv6);
1132168777Srwatson		generate_process64_record(directory, "process64_record");
1133168777Srwatson		generate_process64ex_record(directory, "process64ex_record",
1134168777Srwatson		    AU_IPv4);
1135168777Srwatson		generate_process64ex_record(directory, "process64ex_record",
1136168777Srwatson		    AU_IPv6);
1137161630Srwatson		generate_return32_record(directory, "return32_record");
1138161630Srwatson		generate_text_record(directory, "text_record");
1139161630Srwatson		generate_opaque_record(directory, "opaque_record");
1140161630Srwatson		generate_in_addr_record(directory, "in_addr_record");
1141161630Srwatson		generate_ip_record(directory, "ip_record");
1142161630Srwatson		generate_iport_record(directory, "iport_record");
1143161630Srwatson		generate_arg32_record(directory, "arg32_record");
1144161630Srwatson		generate_seq_record(directory, "seq_record");
1145187214Srwatson#if 0
1146161630Srwatson		generate_attr_record(directory,  "attr_record");
1147161630Srwatson		generate_ipc_perm_record(directory, "ipc_perm_record");
1148161630Srwatson		generate_groups_record(directory, "groups_record");
1149161630Srwatson		generate_attr32_record(directory, "attr32_record");
1150187214Srwatson#endif
1151168777Srwatson		generate_zonename_record(directory, "zonename_record");
1152186647Srwatson		generate_socketex_record(directory, "socketex_record");
1153186647Srwatson		do_error_records(directory);
1154159248Srwatson	}
1155159248Srwatson
1156159248Srwatson	return (0);
1157159248Srwatson}
1158