xref: /titanic_44/usr/src/lib/libbsm/auditxml (revision a60349c89adffc0902b2353230891d8e7f2b24d9)
1#!/usr/perl5/bin/perl -w
2#
3# CDDL HEADER START
4#
5# The contents of this file are subject to the terms of the
6# Common Development and Distribution License (the "License").
7# You may not use this file except in compliance with the License.
8#
9# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10# or http://www.opensolaris.org/os/licensing.
11# See the License for the specific language governing permissions
12# and limitations under the License.
13#
14# When distributing Covered Code, include this CDDL HEADER in each
15# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16# If applicable, add the following below this CDDL HEADER, with the
17# fields enclosed by brackets "[]" replaced with your own identifying
18# information: Portions Copyright [yyyy] [name of copyright owner]
19#
20# CDDL HEADER END
21#
22#
23# Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
24# Use is subject to license terms.
25#
26
27# auditxml takes the audit record description (.xml file) and
28# generates the files needed for the C audit api.
29
30my $prog = $0; $prog =~ s|.*/||g;
31my $usage = <<EOF;
32
33Usage: $prog [options] <xml-input-file>
34Options:
35	-d	Enable debug output
36	-e pfx	Internal event prefix (default: AUE)
37	-i pfx	Interface prefix (default: adt)
38		External event prefix is uppercase version of this string.
39	-o dir	Output directory (default: current dir)
40
41EOF
42
43use auditxml;
44use Getopt::Std;
45use strict;
46
47our $debug = 0; # normal use is to set via the file being parsed.
48               # <debug set="on"/> or <debug set="off"/> or <debug/>
49               # if the set attribute is omitted, debug state is toggled
50               # Override with appDebug, but toggle won't do what you
51               # want.
52my $appDebug = 0; # used after return from "new auditxml";
53
54# Process command-line options
55our ($opt_d, $opt_e, $opt_i, $opt_o);
56$opt_e = "";
57$opt_i = "";
58$opt_o = "";
59if (!getopts('de:i:o:') || $#ARGV != 0) {
60    die $usage;
61}
62my $outdir = $opt_o || ".";
63my $pfx_adt = lc($opt_i) || "adt";
64my $pfx_ADT = uc($pfx_adt);
65my $pfx_AUE = uc($opt_e) || "AUE";
66
67$appDebug = $opt_d;
68
69my $uniLabel = "adr";
70my $xlateUniLabelInc = 0;
71
72
73# where everything comes from and where it goes:
74
75my $xlateFile = "$outdir/${pfx_adt}_xlate.c";
76my $headerFile = "$outdir/${pfx_adt}_event_N.h";
77
78my $filename = $ARGV[0];  # input XML file
79my $doc = new auditxml ($filename);
80$filename =~ s|.*/||g;
81
82$debug = $appDebug;
83
84my $genNotice = "
85DO NOT EDIT. This file is auto generated by the Solaris Audit
86system from $filename.
87
88See http://opensolaris.org/os/project/audit/
89";
90
91# trim leading/trailing newlines
92$genNotice =~ s/^\n//s;
93$genNotice =~ s/\n$//s;
94
95my %xlateEventTable = ();
96my @xlateTypeList = ();
97my %xlateTypeList = ();
98my %eventAPI = ();
99my %eventExtra = ();
100my %headers = ();
101my %externalIdNo = ();
102my @outputState = ();
103my %nameTranslation = ();
104my @xlateDefaults = ();
105my %xlateDefault = ();
106my %msg_list = ();
107
108my $event;
109while ($event = $doc->getNextEvent()) {
110    my $eventId = $event->getId();
111    my $eventHeader = $event->getHeader();
112    my $idNo = $event->getIdNo();
113    $externalIdNo{$eventId} = $idNo;
114    addHeader($eventHeader) if defined ($eventHeader);
115    my $super;
116    my $omit = $event->getOmit();
117    my $eventType = '';
118    if ($super = $event->getSuperClass()) {
119	$event = $super;
120	$eventType = 'instance';
121    } else {
122	$eventType = $event->getType();
123    }
124
125    # header file for API use
126    generateAPIFile($event, $eventId, $eventType, $eventHeader, $idNo)
127        unless $omit eq 'always';
128
129    # c file table for translation
130    generateTableC($event, $eventId, $eventType, $eventHeader, $omit);
131}
132
133my $textList;
134while ($textList = $doc->getNextMsgId()) {
135    generateMsgLists($textList);  # enum -> text mappings
136}
137
138printTableC($xlateFile);
139printAPIFile($headerFile, $doc);
140
141exit 0;
142
143
144sub printTableC {
145    my $file = shift;
146
147    unless (open(Cfile, ">$file")) {
148	print STDERR "can't open output file ($file): $!\n";
149	return;
150    }
151
152    my $notice = $genNotice;
153    $notice =~ s/\n/\n * /gs;
154    $notice =~ s/\s+\n/\n/gs;
155    print Cfile <<EOF;
156/*
157 * $notice
158 */
159
160#include <bsm/libbsm.h>
161#include <adt_xlate.h>
162#include <libintl.h>
163
164EOF
165    print Cfile "#ifndef _PRAUDIT\n";
166    print Cfile "/* Internal data type definitions */\n\n";
167    my $extDef;
168    foreach $extDef (@xlateTypeList) {
169      print Cfile "static $extDef\n";
170    }
171    @xlateTypeList = ();
172
173    print Cfile "\n/* External event structure to internal event structure */\n\n";
174
175    my @pointers = ();
176
177    foreach my $eventId (sort keys %xlateEventTable) {
178	if ($xlateEventTable{$eventId}) {
179	    my ($ref1, $eventType, $firstToken, $eventHeader) =
180	      @{$xlateEventTable{$eventId}};
181	    my @entries = @$ref1;
182	    my $entry;
183	    my $entries = $#entries;
184	    my $count = $entries + 1;
185	    my $externalName = $nameTranslation{$eventId};
186	    my $externalRoot = $externalName;
187	    $externalRoot =~ s/${pfx_AUE}_//;
188	    my $structName = "XX_$externalRoot";
189	    my $root = $eventId;
190	    $root =~ s/${pfx_AUE}_//;
191	    my $externalId = $eventId;
192	    $externalId =~ s/${pfx_AUE}_/${pfx_ADT}_/;
193
194	    unless ($eventType eq 'generic') {
195		print Cfile "static struct entry $structName\[$count\] = {\n";
196		foreach $entry (@entries) {
197		    if ($entries--) {
198			$entry =~ s/EOL/,/;
199		    }
200		    else {
201			$entry =~ s/EOL//;
202		    }
203		    $entry =~ s/selfReference/$structName/;
204		    print Cfile "\t$entry\n";
205		}
206		print Cfile "};\n";
207
208		print Cfile "static struct translation X_$externalRoot = {\n";
209		push (@pointers, "X_$externalRoot");
210
211		print Cfile "\t0,\n";   # tx_offsetsCalculated = 0
212		print Cfile "\t$externalId,\n";
213		print Cfile "\t$externalName,\n";
214
215		print Cfile "\t$count,\n";
216		print Cfile "\t&XX_$externalRoot\[$firstToken\],\n";
217		print Cfile "\t&XX_$externalRoot\[0\]\n};\n";
218	    }
219	} else {
220	    print STDERR "expected entry for $eventId but none found\n";
221	}
222    }
223
224    my $count = $#pointers + 2;
225    print Cfile "adt_translation_t *${pfx_adt}_xlate_table[$count] = {\n";
226
227    my $firstEvent = 1;
228    foreach my $eventId (@pointers) {
229	if ($firstEvent) {
230	    $firstEvent = 0;
231	}
232	else {
233	    print Cfile ",\n";
234	}
235	print Cfile "\t&$eventId";
236    }
237    print Cfile ",\n\tNULL\n};\n";
238
239    # generate the Event preload() function
240
241    print Cfile <<EOF;
242
243void
244${pfx_adt}_preload(au_event_t event_id, adt_event_data_t *event_data)
245{
246	switch (event_id) {
247EOF
248
249        foreach my $id (@xlateDefaults) {
250		my $adtID = $id;
251		$adtID =~ s/${pfx_AUE}/${pfx_ADT}/;
252
253		print Cfile <<EOF;
254	case $adtID:
255EOF
256		my @preloads = @{$xlateDefault{$id}};
257		while (@preloads) {
258			my $fieldName = shift @preloads;
259			my $default = shift @preloads;
260			$id =~ s/${pfx_AUE}_/${pfx_adt}_/;
261
262			print Cfile <<EOF;
263		event_data->$id.$fieldName = $default;
264EOF
265		}
266
267		print Cfile <<EOF;
268		break;
269EOF
270	}
271
272    print Cfile <<EOF;
273	default:
274		break;
275	}
276}
277#endif
278
279EOF
280
281    print Cfile "/* message lists */\n\n";
282    my $listName;
283    my @listName;
284    foreach $listName (sort keys %msg_list) {
285        my ($listRef, $headref) = @{$msg_list{$listName}};
286	my ($header, $start, $public, $deprecated) = @$headref;
287
288	my @listValue =  @$listRef;
289	my $listValue;
290	my $listLength = $#listValue + 1;
291
292	$listName = 'NULL' if ($#listValue < 0);
293
294        push (@listName, [$listName, $listLength - 1, $start, $public]);
295
296	next if ($#listValue < 0);
297
298	print Cfile "/* Deprecated message list */\n" if ($deprecated);
299	print Cfile "static char *msg_$listName\[$listLength] = {\n";
300
301	my $ffirst = 1;
302	foreach $listValue (@listValue) {
303	    print Cfile ",\n" unless $ffirst;
304	    $ffirst = 0;
305	    my ($id, $text) = split(/\s*::\s*/, $listValue);
306	    if ($text) {
307	        print Cfile "\t\"$text\"";
308	    }
309	    else {
310	        print Cfile "\tNULL";
311	    }
312	}
313	print Cfile "\n};\n";
314    }
315
316    if ($#listName >= 0) {
317	print Cfile "\nstruct msg_text ${pfx_adt}_msg_text[", $#listName + 1,
318			"] = {\n";
319	my $ffirst = 1;
320	foreach $listName (@listName) {
321            my ($name, $max, $start) = @$listName;
322	    $start = -$start if $start;
323            print Cfile ",\n" unless $ffirst;
324	    $ffirst = 0;
325	    $name = "msg_$name" if ($name ne 'NULL');
326            print Cfile "\t{0, $max, $name, $start}";
327	}
328	print Cfile "\n};\n";
329    }
330
331    close Cfile;
332}
333
334sub printAPIFile {
335    my $file = shift;
336    my $xmlDoc = shift;
337
338    my @Hfile;
339    @Hfile = openHeaderFiles($file);
340
341    my $notice = $genNotice;
342    $notice =~ s/\n/\n * /gs;
343    $notice =~ s/\s+\n/\n/gs;
344
345    foreach my $header (keys %headers) {
346    	next unless $Hfile[$header];
347	*Hfile = $Hfile[$header];
348	my $include = "adt.h";
349	my $adt_event_n = "_${pfx_ADT}_EVENT_H";
350	if ($header > 0) {
351	    $include = "${pfx_adt}_event.h";
352	    $adt_event_n = "_${pfx_ADT}_EVENT_".$header."_H";
353	}
354	print Hfile <<EOF;
355/*
356 * $notice
357 */
358
359#ifndef $adt_event_n
360#define	$adt_event_n
361
362#include <bsm/$include>
363
364#ifdef	__cplusplus
365extern "C" {
366#endif
367
368/*
369 * adt_put_event() status values.  Positive values are for kernel-generated
370 * failure, -1 for user-space.  For ADT_SUCCESS, the adt_put_event() return_val
371 * is not used; the convention is to set it to ADT_SUCCESS.
372 */
373#define	ADT_SUCCESS	0
374#define	ADT_FAILURE	-1
375
376EOF
377    }
378
379    foreach my $listName (sort keys %msg_list) {
380	my $shortName = uc $listName;
381	$shortName =~ s/_TEXT//;
382
383        my ($listRef, $headref) = @{$msg_list{$listName}};
384	my ($header, $start, $public, $deprecated) = @$headref;
385	next unless $Hfile[$header];
386	*Hfile = $Hfile[$header];
387
388	print Hfile "/* Deprecated message list */\n" if $deprecated;
389	print Hfile "#define\t${pfx_ADT}_$shortName\t$start\n" if $start;
390
391	my @listValue =  @$listRef;
392	next unless ($#listValue >= 0);
393	print Hfile "enum\t${pfx_adt}_$listName", " {\n";
394
395	my $listValue;
396	my $i = 0;
397	my $j = $#listValue;
398	my $comma = ',';
399	foreach $listValue (@listValue) {
400	    my ($id, $text) = split(/\s*::\s*/, $listValue);
401	    $comma = '' if $i++ == $j;
402	    if ($start) {
403		$start = " = $start$comma";
404	    } else {
405	        $start = "$comma\t";
406	    }
407	    $text = "(no token will be generated)" unless $text;
408	    my $line = "\t${pfx_ADT}_$shortName"."_$id$start\t/* ";
409	    # ensure whole line does not exceed 80 chars
410	    my $eline = $line.$text;
411	    #expand tabs
412	    1 while $eline =~ s/\t+/' ' x (length($&) * 8 - length($`) % 8)/e;
413	    if ((length($eline) > 77) && ($line =~ /\t\t/)) {
414	    	# 77 = 80 - length(" */")
415		# strip off double tab so that comment can be longer
416		$line =~ s/\t\t/\t/;
417		# shorten eline; don't mind where the spaces are removed, it is
418		# only $eline length which matters
419		$eline =~ s/ {8}//;
420	    }
421	    if (length($eline) > 77) { # 80 - length(" */")
422	    	# here we use negative length in substr to leave off from the
423		# right side; 74 = 77 - length("...")
424	    	$line .= substr($text, 0, 74 - length($eline));
425		# strip off part of last word (already cut)
426		$line =~ s/\s(\S+)$/ /;
427		$line .= "...";
428	    } else {
429	    	$line .= $text;
430	    }
431	    print Hfile "$line */\n";
432	    $start = '';
433	}
434	print Hfile "};\n";
435    }
436
437    # generate defines for external event names
438
439    foreach my $eventId (sort keys %eventAPI) {
440        my ($header, $idNo) = @{$eventExtra{$eventId}};
441	unless (defined ($header)) {
442	    print STDERR "missing header selection for $eventId\n";
443	    next;
444	}
445	*Hfile = $Hfile[$header];
446	next unless $Hfile[$header];
447
448	my $l = length($eventId) + 8; # label plus preceding #define\t
449	$l = 5 - int(($l + 8)/8);
450	$l = 1 if $l < 1;
451	my $tab = "\t" x $l;
452
453        print STDERR "missing id number for $eventId\n" unless $idNo;
454
455	$eventId =~ s/${pfx_AUE}_/${pfx_ADT}_/;
456	print Hfile "#define\t$eventId$tab$idNo\n";
457    }
458
459
460    # generate per-event structures
461
462    foreach my $eventId (sort keys %eventAPI) {
463        my ($header, $idNo) = @{$eventExtra{$eventId}};
464	my $dataId = $eventId;
465	$dataId =~ s/^${pfx_AUE}_/${pfx_adt}_/;
466	unless(defined ($header)) {
467	    print STDERR "$eventId is missing the header assignment\n";
468	    next;
469	}
470	*Hfile = $Hfile[$header];
471	next unless $Hfile[$header];
472
473	my $externalId = $eventId;
474	$externalId =~ s/${pfx_AUE}_/${pfx_ADT}_/;
475
476	print Hfile "\nstruct $dataId {\t/* $externalId */\n";
477
478	my @entries = @{$eventAPI{$eventId}};
479	my $entry;
480	if ($#entries < 0) {
481	    print Hfile "\tint\tdummy;\t/* not used */\n";
482	} else {
483	    foreach $entry (@entries) {
484		$entry =~ s/termid/adt_termid_t/;
485		print Hfile "\t$entry\n";
486	    }
487	}
488	print Hfile "};\n";
489	$eventId =~ s/^${pfx_AUE}_/${pfx_adt}_/;
490	print Hfile "typedef struct $dataId $eventId","_t;\n";
491    }
492
493    foreach my $header (sort keys %headers) {
494	$outputState[$header] = 0;
495    }
496
497    foreach my $eventId (sort keys %eventAPI) {
498        my ($header, $idNo) = @{$eventExtra{$eventId}};
499	unless(defined ($header)) {
500	    # don't print duplicate error message
501	    next;
502	}
503	*Hfile = $Hfile[$header];
504	next unless $Hfile[$header];
505	if ($outputState[$header] == 0) {
506	    $outputState[$header] = 1;
507	    my $suffix = '';
508	    $suffix = "_$header" if $header;
509	    print Hfile "\nunion adt_event_data$suffix {\n";
510	}
511        my $elementName = $eventId;
512	$elementName =~ s/^${pfx_AUE}_/${pfx_adt}_/;
513	$eventId =~ s/^${pfx_AUE}_/${pfx_adt}_/;
514	$elementName =~ s/_t$//;
515
516	print Hfile "\t\t$eventId","_t\t$elementName;\n";
517    }
518    foreach my $header (sort keys %headers) {
519	if ($outputState[$header]) {
520	    *Hfile = $Hfile[$header];
521	    next unless $Hfile[$header];
522	    print Hfile "};\n";
523	}
524    }
525    foreach my $header (keys %headers) {
526    	next unless $Hfile[$header];
527	*Hfile = $Hfile[$header];
528	my $adt_event_n = "_${pfx_ADT}_EVENT_H";
529	if ($header > 0) {
530	    $adt_event_n = "_${pfx_ADT}_EVENT_".$header."_H";
531	}
532	print Hfile <<EOF;
533
534
535#ifndef	${pfx_ADT}_PRIVATE
536#define	${pfx_ADT}_PRIVATE
537
538/*
539 * These interfaces are project private and will change without
540 * notice as needed for the Solaris Audit project.
541 */
542
543extern	void	adt_get_auid(const adt_session_data_t *, au_id_t *);
544extern	void	adt_set_auid(const adt_session_data_t *, const au_id_t);
545
546extern	void	adt_get_mask(const adt_session_data_t *, au_mask_t *);
547extern	void	adt_set_mask(const adt_session_data_t *, const au_mask_t *);
548
549extern	void	adt_get_termid(const adt_session_data_t *, au_tid_addr_t *);
550extern	void	adt_set_termid(const adt_session_data_t *,
551    const au_tid_addr_t *);
552
553extern	void	adt_get_asid(const adt_session_data_t *, au_asid_t *);
554extern	void	adt_set_asid(const adt_session_data_t *, const au_asid_t);
555extern	au_asid_t adt_get_unique_id(au_id_t);
556extern	void	adt_load_table(const adt_session_data_t *, adt_translation_t **,
557    void (*preload)(au_event_t, adt_event_data_t *));
558
559extern	void	${pfx_adt}_preload(au_event_t, adt_event_data_t *);
560
561extern adt_translation_t *${pfx_adt}_xlate_table[];
562
563#endif
564
565#ifdef	__cplusplus
566}
567#endif
568
569#endif	/* $adt_event_n */
570EOF
571    }
572    closeHeaderFiles(@Hfile);
573}
574
575sub generateTableC {
576    my $event = shift;
577    my $eventId = shift;
578    my $eventType = shift;
579    my $eventHeader = shift;
580    my $omit = shift;
581
582    my %tokenType = (
583	#
584	#	tokenTypes are the ones that are actually defined
585	#	for use in adt.xml audit records
586	#
587
588	#	  'acl'			=> 'AUT_ACL',		# not defined
589	#	  'arbitrary'		=> 'AUT_ARBITRARY',	# not defined
590	#	  'arg'			=> 'AUT_ARG',		# not defined
591	#	  'attr'		=> 'AUT_ATTR',
592		  'command'		=> 'AUT_CMD',
593		  'command_alt'		=> 'ADT_CMD_ALT',	# dummy token id
594	#	  'date'		=> 'AUT_TEXT',		# not used
595	#	  'exec_args'   	=> 'AUT_EXEC_ARGS',	# not defined
596	#	  'exec_env'    	=> 'AUT_EXEC_ENV',	# not defined
597	#	  'exit'        	=> 'AUT_EXIT',		# not defined
598		  'fmri'        	=> 'AUT_FMRI',
599	#	  'groups'      	=> 'AUT_GROUPS',	# not defined
600	#	  'header'      	=> 'AUT_HEADER',	# not defined
601		  'in_peer'     	=> 'ADT_IN_PEER',	# dummy token id
602		  'in_remote'     	=> 'ADT_IN_REMOTE',	# dummy token id
603	#	  'ipc'         	=> 'AUT_IPC',		# not defined
604	#	  'ipc_perm'    	=> 'AUT_IPC_PERM',	# not defined
605		  'iport'		=> 'AUT_IPORT',
606		  'label'		=> 'AUT_LABEL',
607		  'newgroups'   	=> 'AUT_NEWGROUPS',
608	#	  'opaque'      	=> 'AUT_OPAQUE',	# not defined
609		  'path'        	=> 'AUT_PATH',
610		  'path_list'		=> '-AUT_PATH',		# dummy token id
611		  'process'     	=> 'AUT_PROCESS',
612		  'priv_effective'	=> 'ADT_AUT_PRIV_E',	# dummy token id
613		  'priv_limit'		=> 'ADT_AUT_PRIV_L', 	# dummy token id
614		  'priv_inherit'	=> 'ADT_AUT_PRIV_I',	# dummy token id
615		  'return'      	=> 'AUT_RETURN',
616	#	  'seq'         	=> 'AUT_SEQ',		# not defined
617	#	  'socket'      	=> 'AUT_SOCKET',	# not defined
618	#	  'socket-inet' 	=> 'AUT_SOCKET_INET',
619		  'subject'     	=> 'AUT_SUBJECT',
620		  'text'        	=> 'AUT_TEXT',
621		  'tid'          	=> 'AUT_TID',
622	#	  'trailer'     	=> 'AUT_TRAILER',	# not defined
623		  'uauth'		=> 'AUT_UAUTH',
624		  'user'		=> 'AUT_USER',
625		  'zonename'		=> 'AUT_ZONENAME'
626		 );
627
628    my @xlateEntryList = ();
629
630    my $external = $event->getExternal();
631    my $internal = $event->getInternal();
632
633    unless ($external) {
634	print STDERR "No external object captured for event $eventId\n";
635	return;
636    }
637    if ($eventType) {
638	$nameTranslation{$eventId} = $eventId;
639    } else {
640	$nameTranslation{$eventId} = $external->getInternalName();
641    }
642    unless ($internal) {
643	print STDERR "No internal object captured for event $eventId\n";
644	return;
645    }
646    my @entryRef = $internal->getEntries();
647    my $entryRef;
648    my @tokenOrder = ();
649    my $firstTokenIndex = 0; # djdj not used yet, djdj BUG!
650    			     # needs to be used by translate table
651
652    if ($internal->isReorder()) { # prescan the entry list to get the token order
653      my @inputOrder;
654      foreach $entryRef (@entryRef) {
655	my ($intEntry, $entry) = @$entryRef;
656	push (@inputOrder, $intEntry->getAttr('order'));
657      }
658
659      my $i; # walk down the inputOrder list once
660      my $k = 1; # discover next in line
661      my $l = 0; # who should point to next in line
662      for ($i = 0; $i <= $#inputOrder; $i++) {
663	my $j;
664	for ($j = 0; $j <= $#inputOrder; $j++) {
665	  if ($k == $inputOrder[$j]) {
666	    if ($k == 1) {
667	        $firstTokenIndex = $j;
668	    } else {
669	        $tokenOrder[$l] = "&(selfReference[$j])";
670	    }
671	    $l = $j;
672	    last;
673	  }
674	}
675	$k++;
676      }
677      $tokenOrder[$l] = 'NULL';
678    }
679    else { # default order -- input order same as output
680      my $i;
681      my $j;
682      for ($i = 0; $i < $#entryRef; $i++) {
683	my $j = $i + 1;
684	$tokenOrder[$i] = "&(selfReference[$j])";
685      }
686      $tokenOrder[$#entryRef] = 'NULL';
687    }
688
689    my $sequence = 0;
690    foreach $entryRef (@entryRef) {
691      my ($intEntry, $entry) = @$entryRef;
692      my $entryId = $entry->getAttr('id');
693
694      my ($extEntry, $unusedEntry, $tokenId) =
695	$external->getEntry($entryId);
696      my $opt = $extEntry->getAttr('opt');
697
698      if ($opt eq 'none') {
699	if (defined ($doc->getToken($tokenId))) {
700	  if (defined ($tokenType{$tokenId})) {
701	    $tokenId = $tokenType{$tokenId};
702	  }
703	  else {
704	    print STDERR "token id $tokenId not implemented\n";
705	  }
706	}
707	else {
708	  print STDERR "token = $tokenId is undefined\n";
709	  $tokenId = 'error';
710	}
711	my ($xlate, $jni) =
712	  formatTableEntry ('', $tokenId, $eventId, '', 0, 0,
713			    $tokenOrder[$sequence], 'NULL', '', $omit);
714	push (@xlateEntryList, $xlate);
715      }
716      else {
717	my $dataType = $extEntry->getAttr('type');
718	$dataType =~ s/\s+//g;   # remove blanks (char * => char*)
719
720	my $enumGroup = '';
721	if ($dataType =~ /^msg/i) {
722	    $enumGroup = $dataType;
723	    $enumGroup =~ s/^msg\s*//i;
724	    $enumGroup = "${pfx_adt}_" . $enumGroup;
725	}
726	my $required = ($opt eq 'required') ? 1 : 0;
727	my $tsol = 0;
728	my $tokenId = $intEntry->getAttr('token');
729	my $token;
730	my $tokenName;
731	my $tokenFormat = $intEntry->getAttr('format');
732	if (defined ($tokenFormat)) {
733	  $tokenFormat = "\"$tokenFormat\"";
734	}
735	else {
736	  $tokenFormat = 'NULL';
737	}
738
739	if (defined ($token = $doc->getToken($tokenId))) {
740	  $tsol = (lc $token->getUsage() eq 'tsol') ? 1 : 0;
741	  if (defined ($tokenType{$tokenId})) {
742	    $tokenName = $tokenType{$tokenId};
743	  }
744	  else {
745	    print STDERR "token id $tokenId not implemented\n";
746	  }
747	}
748	else {
749	  print STDERR
750	    "$tokenId is an unimplemented token ($entryId in $eventId)\n";
751	  $tokenName = 'AUT_TEXT';
752	}
753	my ($xlate, $jni) =
754	  formatTableEntry($entryId, $tokenName, $eventId, $dataType, $required,
755			   $tsol, $tokenOrder[$sequence], $tokenFormat,
756			   $enumGroup, $omit);
757	push (@xlateEntryList, $xlate);
758      }
759      $sequence++;
760    }
761    $xlateEventTable{$eventId} = [\@xlateEntryList, $eventType, $firstTokenIndex,
762				 $eventHeader];
763}
764
765sub formatTableEntry {
766    my ($id, $token, $eventId, $type, $required, $tsol, $sequence, $format,
767	$enumGroup, $omitEntry) = @_;
768
769
770    # does this map belong in the xml source?  (at least the defaults?)
771    # fill in the default value only if it is other than zero.
772    #		      base type		    adt name,	default value
773    my %entryDef = ( 'au_asid_t'       	=> ['ADT_UINT32',	''],
774		     'uint_t'		=> ['ADT_UINT32',      	''],
775		     'int'		=> ['ADT_INT',		''],
776		     'int32_t'		=> ['ADT_INT32',	''],
777		     'uid_t'		=> ['ADT_UID',		'AU_NOAUDITID'],
778		     'gid_t'		=> ['ADT_GID',		'AU_NOAUDITID'],
779		     'uid_t*'		=> ['ADT_UIDSTAR',	''],
780		     'gid_t*'		=> ['ADT_GIDSTAR',	''],
781		     'char'		=> ['ADT_CHAR',		''],
782		     'char*'		=> ['ADT_CHARSTAR',	''],
783		     'char**'		=> ['ADT_CHAR2STAR',	''],
784		     'long'		=> ['ADT_LONG',		''],
785		     'pid_t'		=> ['ADT_PID',		''],
786		     'priv_set_t*'	=> ['ADT_PRIVSTAR',	''],
787		     'ulong_t'		=> ['ADT_ULONG',	''],
788		     'uint16_t',	=> ['ADT_UINT16',	''],
789		     'uint32_t'		=> ['ADT_UINT32',	''],
790		     'uint32_t*'	=> ['ADT_UINT32STAR',	''],
791		     'uint32_t[]'	=> ['ADT_UINT32ARRAY',  ''],
792		     'uint64_t'		=> ['ADT_UINT64',	''],
793		     'uint64_t*'	=> ['ADT_UINT64STAR',	''],
794		     'm_label_t*'	=> ['ADT_MLABELSTAR',	''],
795		     'fd_t'		=> ['ADT_FD',		'-1'],
796		    );
797    my $xlateLabel = $uniLabel.$xlateUniLabelInc;
798    my $xlateLabelInc = 0;
799    my $xlateLine = '';
800    my @jniLine = ();
801
802	# the list handling should be a simple loop with a loop of one
803        # falling out naturally.
804
805    unless ($type =~ /,/) {	# if list, then generate sequence of entries
806      my $dataType;
807      my $dataSize;
808      my $xlateLabelRef = '';
809
810      my $arraySize = '';
811      $arraySize = $1 if ($type =~ s/\[(\d+)\]/[]/);
812
813      my $entryType = ${$entryDef{$type}}[0];
814
815      my @xlateType = ();	# for adt_xlate.c
816      my $typeCount = 1;
817
818      if ($entryType) {
819	$dataType = $entryType;
820	$type =~ s/([^*]+)\s*(\*+)/$1 $2/;
821	$type =~ s/\[\]//;
822	$dataSize = "sizeof ($type)";
823	if ($arraySize) {
824		$dataSize = "$arraySize * " . $dataSize;
825	}
826	$xlateLine = "{{$dataType, $dataSize}}";
827	push (@jniLine, [$id, $dataType, $format, $enumGroup, $required]);
828      } elsif ($type eq '') {
829	  $xlateLabelRef = 'NULL';
830      } elsif ($type =~ /^msg/i) {
831	$type =~ s/^msg//i;
832	$dataType = 'ADT_MSG';
833	my $dataEnum = 'ADT_LIST_' . uc $type;
834	$xlateLine = "{{$dataType, $dataEnum}}";
835	push (@jniLine, [$id, $dataType, $format, $enumGroup, $required]);
836      } elsif ($type =~ /time_t/i) {
837	$dataType = 'ADT_DATE';
838	$dataSize = "sizeof (time_t)";
839	$xlateLine = "{{$dataType, $dataSize}}";
840	push (@jniLine, [$id, $dataType, $format, $enumGroup, $required]);
841      } elsif ($type =~ /termid/i) {
842	$dataType = 'ADT_TERMIDSTAR';
843	$dataSize = "sizeof (au_tid_addr_t *)";
844	$xlateLine = "{{$dataType, $dataSize}}";
845	push (@jniLine, [$id, $dataType, $format, $enumGroup, $required]);
846      } elsif (uc $omitEntry eq 'JNI') {
847	$xlateLabelRef = 'NULL';
848      } else {
849	print STDERR "$type is not an implemented data type\n";
850	$xlateLabelRef = 'NULL';
851      }
852      if ($xlateLine && !($xlateTypeList{$xlateLine})) {
853	$xlateTypeList{$xlateLine} = $xlateLabel;
854	push (@xlateTypeList, "datadef\t$xlateLabel\[1\] =\t$xlateLine;");
855	$xlateLabelInc = 1;
856      } else {
857	$xlateLabel = $xlateTypeList{$xlateLine};
858      }
859      $xlateLabelRef = '&' . $xlateLabel . '[0]'
860	unless $xlateLabelRef eq 'NULL';
861
862      # "EOL" is where a comma should go unless end of list
863      $xlateLine = "{$token,\t1,\t$xlateLabelRef,\t$sequence,\n" .
864	  "\t\t0,\t$required,\t$tsol,\t$format}EOL";
865
866      if (uc $omitEntry ne 'ALWAYS' && ${$entryDef{$type}}[1]) {
867	  my @list = ();
868	  if ($xlateDefault{$eventId}) {
869	      @list = @{$xlateDefault{$eventId}};
870	  } else {
871	      push (@xlateDefaults, $eventId);
872	  }
873	  push (@list, $id, ${$entryDef{$type}}[1]);
874	  $xlateDefault{$eventId} = \@list;
875      }
876    } else {	# is a list
877      my @type = split(/,/, $type);
878      my @arraySize = ();
879      my @id   = split(/,/, $id);
880      my @jniId  = @id;
881      my $dataType;
882      my $typeCount = ($#type + 1);
883      my @xlateType = ();
884      my @default = ();
885
886      foreach my $dtype (@type) {
887	my $jniId = shift @jniId;
888	my $id = shift @id;
889	my $arraySize = '';
890	$arraySize = $1 if ($dtype =~ s/\[(\d+)\]/[]/);
891
892	my $entryType = ${$entryDef{$dtype}}[0];
893	if ($entryType) {
894	  my $type = $dtype;
895	  $type =~ s/([^*]+)\s*(\*+)/$1 $2/;
896	  $type =~ s/\[\]//;
897
898	  my $sizeString = "sizeof";
899	  $sizeString = "$arraySize * " . $sizeString if $arraySize;
900	  push (@xlateType, "\{$entryType, $sizeString ($type)\}");
901	  push (@jniLine, [$jniId, $entryType, $format, $enumGroup, $required]);
902	} elsif ($type =~ /^msg/i) {
903	  $type =~ s/^msg//i;
904	  $dataType = 'ADT_MSG';
905	  my $dataEnum = 'ADT_LIST_' . uc $type;
906	  push (@xlateType, "\{$dataType, $dataEnum\}};");
907	  push (@jniLine, [$jniId, $dataType, $format, $enumGroup, $required]);
908	} elsif ($type =~ /time_t/i) {
909	  $dataType = 'ADT_DATE';
910	  push (@xlateType, "\{$entryType, sizeof ($type)\}");
911	  push (@jniLine, [$jniId, $entryType, $format, $enumGroup, $required]);
912	} elsif ($type =~ /termid/i) {
913	  $dataType = 'ADT_TERMIDSTAR';
914	  push (@xlateType, "\{$dataType, sizeof (au_tid_addr_t *)\}");
915	  push (@jniLine, [$jniId, $dataType, $format, $enumGroup, $required]);
916	} elsif (uc $omitEntry eq 'JNI') {
917	  # nothing to do.
918	} else {
919	  print STDERR "$dtype is not an implemented data type\n";
920	}
921	if (uc $omitEntry ne 'ALWAYS' && ${$entryDef{$dtype}}[1]) {
922	  push (@default, $id, ${$entryDef{$dtype}}[1]);
923	}
924      }
925      my $xlateArray = "\[$typeCount\] =\t{" . join(",\n\t\t\t\t", @xlateType) . "};";
926
927      unless ($xlateTypeList{$xlateArray}) {
928	$xlateTypeList{$xlateArray} = $xlateLabel;
929	$xlateArray = "datadef\t$xlateLabel" . $xlateArray;
930	push (@xlateTypeList, $xlateArray);
931	$xlateLabelInc = 1;
932      } else {
933	$xlateLabel = $xlateTypeList{$xlateArray};
934      }
935      $xlateLine =
936	"{$token,\t$typeCount,\t&$xlateLabel\[0\],\t$sequence,\n" .
937        "\t\t0,\t$required,\t$tsol,\t$format}EOL";
938      if (@default) {
939	  my @list = ();
940	  if ($xlateDefault{$eventId}) {
941	      @list = @{$xlateDefault{$eventId}};
942	  } else {
943	      push (@xlateDefaults, $eventId);
944	  }
945	  push (@list, @default);
946	  $xlateDefault{$eventId} = \@list;
947      }
948    }
949    $xlateUniLabelInc++ if $xlateLabelInc;
950    return ($xlateLine, \@jniLine);
951}
952
953sub generateAPIFile {
954    my $event = shift;
955    my $eventId = shift;
956    my $eventType = shift;
957    my $eventHeader = shift;
958    my $idNo = shift;
959
960    my @entryList = ();
961
962    my $external = $event->getExternal();
963
964    if ($eventType && $debug) {
965	print STDERR "event $eventId is of type $eventType\n";
966    }
967
968    return unless $external;
969
970    my ($extEntry, $entry, $tokenId, $format);
971    while (($extEntry, $entry, $tokenId, $format) = $external->getNextEntry()) {
972	last unless $entry;
973	my $entryId = $entry->getAttr('id');
974
975	unless (defined $entryId) {
976	    print STDERR "undefined entry id for external $eventId\n";
977	    next;
978	}
979	my $option = $extEntry->getAttr('opt');
980	next if ($option eq 'none');
981
982	if (defined (my $token = $doc->getToken($tokenId))) {
983	  $option = 'Trusted Solaris only'
984	    if (lc $token->getUsage() eq 'tsol') ? 1 : 0;
985	}
986	$option .= " (format: $format)" if $format;
987
988	my $dataType = $extEntry->getAttr('type');
989	unless (defined $dataType) {
990	  print STDERR "no type defined for external tag for $eventId\n";
991	  $dataType = "error";
992	}
993
994	my $comment = $entry->getContent();
995
996	if (($dataType =~ /,/) || ($entryId =~ /,/)) {
997	  my @type = split(/\s*,\s*/, $dataType);
998	  my @id   = split(/\s*,\s*/, $entryId);
999	  if ($#type != $#id) {
1000	    print STDERR
1001	      "number of data types ($dataType) does not match number of ids ($entryId)",
1002	      " for event $eventId\n";
1003	    if ($#type < $#id) {
1004	      $#id = $#type;
1005	    }
1006	    else {
1007	      $#type = $#id;
1008	    }
1009	  }
1010
1011	  my $i;
1012	  my $line = '';
1013	  $line = "/* $comment */\n\t" if defined $comment;
1014	  for ($i = 0; $i <= $#type; $i++) {
1015	    my ($primitive, $dereference) =
1016	        ($type[$i] =~ /([^\*]+)\s*(\**)/);
1017	    $id[$i] .= $1 if ($primitive =~ s/(\[\d+\])//);
1018	    $line .= "$primitive\t$dereference$id[$i];\t/*  $option  */";
1019	    push (@entryList, $line);
1020	    $line = '';
1021	  }
1022	}
1023	else {
1024	  my $line = '';
1025	  $line = "/* $comment */\n\t" if defined $comment;
1026	  if ($dataType =~ /^msg/i) {
1027	      $dataType =~ s/^msg\s*//i;
1028	      $line .= "enum ${pfx_adt}_$dataType" . "\t$entryId;\t/*  $option  */";
1029	  }
1030	  elsif ($dataType =~ /time_t/i) {
1031	      $line .= "time_t\t$entryId;\t/* $option */";
1032	  }
1033	  else {
1034	    my ($primitive, $dereference) =
1035	        ($dataType =~ /([^\*]+)\s*(\**)/);
1036	    $entryId .= $1 if ($primitive =~ s/(\[\d+\])//);
1037	    $line .= "$primitive\t$dereference$entryId;\t/* $option */";
1038	  }
1039	  push (@entryList, $line);
1040	}
1041    }
1042    $eventExtra{$eventId} = [$eventHeader, $idNo];
1043    $eventAPI{$eventId} = \@entryList;
1044}
1045
1046sub generateMsgLists {
1047    my $textList = shift;
1048
1049    my $textName = $textList->getId();
1050    my $header = $textList->getHeader();
1051    my $start = $textList->getMsgStart();
1052    my $public = $textList->getMsgPublic();
1053    my $deprecated = $textList->getDeprecated();
1054
1055    addHeader($header);
1056    print "$textName starts at $start\n" if $debug;
1057
1058    my $entry;
1059    my @entry;
1060    while ($entry = $textList->getNextMsg()) {
1061        if ($debug) {
1062	    my ($id, $text) = split(/\s*::\s*/, $entry);
1063	    print "   $id = $text\n";
1064	}
1065	unshift (@entry, $entry);
1066    }
1067    $msg_list{$textName} =
1068	[\@entry, [$header, $start, $public, $deprecated]];
1069}
1070
1071sub addHeader {
1072    my $header_index = shift;
1073
1074    die "invalid adt_event_N.h index: $header_index\n"
1075        unless ($header_index =~ /^\d+$/);
1076
1077    $headers{$header_index} = $header_index;
1078}
1079
1080# $header = 0 is a special case; it is for adt_event.h
1081# $header > 0 creates adt_event_N.h, where N = $header
1082
1083sub openHeaderFiles {
1084    my $outfile = shift;	# path to an adt_event_N.h file
1085
1086    my $header;
1087    my @Hfile = (); # potentially sparse array of file handles
1088    my @HfileName = (); # parallel array to Hfile, file name (not path)
1089    foreach $header (sort keys %headers) {
1090        my $file = $outfile;
1091	if ($header > 0) {
1092	    $file =~ s/_N/_$header/;
1093	} else {
1094	    $file =~ s/_N//;
1095	}
1096	unless (open($Hfile[$header], ">$file")) {
1097	    print STDERR "can't open output ($file): $!\n";
1098	    $HfileName[$header] = '';
1099	    $Hfile[$header] = '';
1100	} else {
1101	    my @tmp = split(/\//, $file);
1102	    $HfileName[$header] = $tmp[$#tmp];
1103	}
1104    }
1105    return (@Hfile);
1106}
1107
1108sub closeHeaderFiles {
1109    my @Hfile = @_;
1110
1111    my $header;
1112    foreach $header (sort keys %headers) {
1113	close $Hfile[$header] if $Hfile[$header];
1114    }
1115}
1116