xref: /titanic_41/usr/src/lib/libbsm/common/adt_token.c (revision 2449e17f82f6097fd2c665b64723e31ceecbeca6)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * adt_token.c
23  *
24  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
25  * Use is subject to license terms.
26  *
27  * This file does not provide any user callable functions.  See adt.c
28  */
29 #pragma ident	"%Z%%M%	%I%	%E% SMI"
30 
31 #include <bsm/adt.h>
32 #include <bsm/adt_event.h>
33 #include <bsm/audit.h>
34 #include <adt_xlate.h>
35 #include <assert.h>
36 #include <netdb.h>
37 #include <priv.h>
38 #include <string.h>
39 #include <strings.h>
40 #include <stdlib.h>
41 #include <sys/priv_names.h>
42 #include <sys/types.h>
43 #include <sys/vnode.h>
44 #include <tsol/label.h>
45 #include <time.h>
46 #include <unistd.h>
47 
48 #ifdef	C2_DEBUG
49 #define	DPRINTF(x) {printf x; }
50 #define	DFLUSH fflush(stdout);
51 
52 /* 0x + Classification + Compartments + end of string */
53 #define	HEX_SIZE 2 + 2*2 + 2*32 + 1
54 
55 static char *
56 dprt_label(m_label_t *label)
57 {
58 	static char	hex[HEX_SIZE];
59 	char		*direct = NULL;
60 
61 	if (label_to_str(label, &direct, M_INTERNAL, DEF_NAMES) != 0) {
62 		adt_write_syslog("label_to_str(M_INTERNAL)", errno);
63 		return ("hex label failed");
64 	}
65 	(void) strlcpy(hex, direct, sizeof (hex));
66 	free(direct);
67 	return (hex);
68 }
69 #else	/* !C2_DEBUG */
70 #define	DPRINTF(x)
71 #define	DFLUSH
72 #endif	/* C2_DEBUG */
73 
74 static adt_token_func_t adt_getTokenFunction(char);
75 
76 static char	*empty = "";
77 
78 /*
79  * call adt_token_open() first and adt_token_close() last.
80  *
81  * au_open is sort of broken; it returns a -1 when out of memory that
82  * you're supposed to ignore; au_write and au_close return without
83  * doing anything when a -1 is passed.  This code sort of follows the
84  * au_open model except that it calls syslog to indicate underlying
85  * brokenness.  Other than that, -1 is ignored.
86  */
87 
88 void
89 adt_token_open(struct adt_event_state *event)
90 {
91 	static int	have_syslogged = 0;
92 
93 	event->ae_event_handle = au_open();
94 	if (event->ae_event_handle < 0) {
95 		if (!have_syslogged) {
96 			adt_write_syslog("au_open failed", ENOMEM);
97 			have_syslogged = 1;
98 		}
99 	}
100 	else
101 		have_syslogged = 0;
102 }
103 
104 /*
105  * call generate_token for each token in the order you want the tokens
106  * generated.
107  */
108 
109 void
110 adt_generate_token(struct entry *p_entry, void *p_data,
111     struct adt_event_state *event)
112 {
113 	adt_token_func_t	p_func;
114 
115 	assert((p_entry != NULL) && (p_data != NULL) && (event != NULL));
116 
117 	p_func = adt_getTokenFunction(p_entry->en_token_id);
118 	assert(p_func != NULL);
119 
120 	DPRINTF(("p_entry=%08X, p_data=%08X, offset=%X, msgFmt=%X\n",
121 	    p_entry, p_data, p_entry->en_offset, p_entry->en_msg_format));
122 	DFLUSH
123 
124 	(*p_func)(p_entry->en_type_def,
125 	    (char *)p_data + p_entry->en_offset, p_entry->en_required, event,
126 	    p_entry->en_msg_format);
127 }
128 
129 /* call this last */
130 
131 void
132 adt_token_close(struct adt_event_state *event)
133 {
134 	int	rc;
135 
136 	rc = au_close(event->ae_event_handle, AU_TO_WRITE,
137 	    event->ae_internal_id);
138 	if (rc < 0)
139 		adt_write_syslog("au_close failed", errno);
140 }
141 
142 /*
143  * one function per token -- see the jump table at the end of file
144  */
145 
146 /* ARGSUSED */
147 static void
148 adt_to_return(datadef *def, void *p_data, int required,
149     struct adt_event_state *event, char *notUsed)
150 {
151 
152 #ifdef _LP64
153 	(void) au_write(event->ae_event_handle,
154 	    au_to_return64((int64_t)event->ae_rc, event->ae_type));
155 #else
156 	(void) au_write(event->ae_event_handle,
157 	    au_to_return32((int32_t)event->ae_rc, event->ae_type));
158 #endif
159 }
160 
161 /*
162  * AUT_CMD
163  *
164  * the command line is described with argc and argv and the environment
165  * with envp.  The envp list is NULL terminated and has no separate
166  * counter; envp will be a NULL list unless the AUDIT_ARGE policy is
167  * set.
168  */
169 
170 /* ARGSUSED */
171 static void
172 adt_to_cmd(datadef *def, void *p_data, int required,
173     struct adt_event_state *event, char *notUsed)
174 {
175 	struct adt_internal_state	*sp = event->ae_session;
176 	int				argc;
177 	char				**argv;
178 	char				**envp = NULL;
179 
180 	argc = ((union convert *)p_data)->tint;
181 	p_data = adt_adjust_address(p_data, sizeof (int), sizeof (char **));
182 	argv = ((union convert *)p_data)->tchar2star;
183 	p_data = adt_adjust_address(p_data, sizeof (char **), sizeof (char **));
184 
185 	if (sp->as_kernel_audit_policy & AUDIT_ARGE)
186 		envp = ((union convert *)p_data)->tchar2star;
187 
188 	(void) au_write(event->ae_event_handle,
189 	    au_to_cmd(argc, argv, envp));
190 }
191 
192 /*
193  * special case of AUT_CMD with 1 argument that is
194  * a string showing the whole command and no envp
195  */
196 /* ARGSUSED */
197 static void
198 adt_to_cmd1(datadef *def, void *p_data, int required,
199     struct adt_event_state *event, char *notUsed)
200 {
201 	char	*string;
202 
203 	string = ((union convert *)p_data)->tcharstar;
204 
205 	if (string == NULL) {
206 		if (required)
207 			string = empty;
208 		else
209 			return;
210 	}
211 	/* argc is hardcoded as 1 */
212 	(void) au_write(event->ae_event_handle, au_to_cmd(1, &string,
213 	    NULL));
214 }
215 
216 /*
217  * adt_to_tid	-- generic address (ip is only one defined at present)
218  *	input:
219  *		terminal type:  ADT_IPv4, ADT_IPv6...
220  *		case: ADT_IPv4 or ADT_IPv6...
221  *			ip type
222  *			remote port
223  *			local port
224  *			address
225  *		case: not defined...
226  */
227 /* ARGSUSED */
228 static void
229 adt_to_tid(datadef *def, void *p_data, int required,
230     struct adt_event_state *event, char *notUsed)
231 {
232 	au_generic_tid_t	tid;
233 	uint32_t		type;
234 	au_ip_t			*ip;
235 
236 	type = ((union convert *)p_data)->tuint32;
237 
238 	switch (type) {
239 	case ADT_IPv4:
240 	case ADT_IPv6:
241 		p_data = adt_adjust_address(p_data, sizeof (uint32_t),
242 		    sizeof (uint32_t));
243 
244 		tid.gt_type = AU_IPADR;
245 		ip = &(tid.gt_adr.at_ip);
246 
247 		ip->at_type = (type == ADT_IPv4) ?
248 		    AU_IPv4 : AU_IPv6;
249 
250 		ip->at_r_port = ((union convert *)p_data)->tuint16;
251 		p_data = adt_adjust_address(p_data, sizeof (uint16_t),
252 		    sizeof (uint16_t));
253 
254 		ip->at_l_port = ((union convert *)p_data)->tuint16;
255 
256 		/* arg3 is for the array element, not the array size */
257 		p_data = adt_adjust_address(p_data, sizeof (uint16_t),
258 		    sizeof (uint32_t));
259 
260 		(void) memcpy(ip->at_addr, p_data, ip->at_type);
261 		break;
262 	default:
263 		adt_write_syslog("Invalid terminal id type", EINVAL);
264 		return;
265 	}
266 	(void) au_write(event->ae_event_handle, au_to_tid(&tid));
267 }
268 
269 /*
270  * au_to_frmi takes a char * that is the fmri.
271  */
272 /* ARGSUSED */
273 static void
274 adt_to_frmi(datadef *def, void *p_data, int required,
275     struct adt_event_state *event, char *notUsed)
276 {
277 	char		*fmri;
278 
279 	DPRINTF(("  adt_to_fmri dd_datatype=%d\n", def->dd_datatype));
280 
281 	fmri = ((union convert *)p_data)->tcharstar;
282 
283 	if (fmri == NULL) {
284 		if (required)
285 			fmri = empty;
286 		else
287 			return;
288 	}
289 	DPRINTF(("  fmri=%s\n", fmri));
290 	(void) au_write(event->ae_event_handle, au_to_fmri(fmri));
291 }
292 
293 /*
294  * au_to_label takes an m_label_t * that is the label.
295  */
296 /* ARGSUSED */
297 static void
298 adt_to_label(datadef *def, void *p_data, int required,
299     struct adt_event_state *event, char *notUsed)
300 {
301 	m_label_t	*label;
302 
303 	DPRINTF(("  adt_to_label dd_datatype=%d\n", def->dd_datatype));
304 
305 	label = ((union convert *)p_data)->tm_label;
306 
307 	if (label != NULL) {
308 		DPRINTF(("  label=%s\n", dprt_label(label)));
309 		DFLUSH
310 		(void) au_write(event->ae_event_handle, au_to_label(label));
311 	} else {
312 		DPRINTF(("  Null label\n"));
313 		if (required)
314 			adt_write_syslog("adt_to_label no required label", 0);
315 	}
316 }
317 
318 /*
319  * au_to_newgroups takes a length and an array of gids
320  * as input.  The input to adt_to_newgroups is a length
321  * and a pointer to an array of gids.
322  */
323 
324 /* ARGSUSED */
325 static void
326 adt_to_newgroups(datadef *def, void *p_data, int required,
327     struct adt_event_state *event, char *notUsed)
328 {
329 	int	n;
330 	gid_t	*groups;
331 
332 	n = ((union convert *)p_data)->tint;
333 	if (n < 1) {
334 		if (required) {
335 			n = 0;  /* in case negative n was passed */
336 		} else
337 			return;
338 	}
339 	p_data = adt_adjust_address(p_data, sizeof (int), sizeof (int32_t *));
340 
341 	groups = ((union convert *)p_data)->tgidstar;
342 
343 	(void) au_write(event->ae_event_handle, au_to_newgroups(n, groups));
344 }
345 
346 /* ARGSUSED */
347 static void
348 adt_to_path(datadef *def, void *p_data, int required,
349     struct adt_event_state *event, char *notUsed)
350 {
351 	char	*path;
352 
353 	path = ((union convert *)p_data)->tcharstar;
354 
355 	if (path != NULL) {
356 		DPRINTF(("  path=%s\n", path));
357 		(void) au_write(event->ae_event_handle, au_to_path(path));
358 	} else {
359 		DPRINTF(("  Null path\n"));
360 		if (required)
361 			(void) au_write(event->ae_event_handle,
362 			    au_to_path(empty));
363 	}
364 }
365 
366 /*
367  * dummy token id:  AUT_PATHLIST
368  */
369 
370 /* ARGSUSED */
371 static void
372 adt_to_pathlist(datadef *def, void *p_data, int required,
373     struct adt_event_state *event, char *notUsed)
374 {
375 	char	*path;
376 	char	*working_buf;
377 	char	*pathlist;
378 	char	*last_str;
379 
380 	pathlist = ((union convert *)p_data)->tcharstar;
381 
382 	if (pathlist != NULL) {
383 		working_buf = strdup(pathlist);
384 		if (working_buf == NULL) {
385 			adt_write_syslog("audit failure", errno);
386 			if (required)
387 				(void) au_write(event->ae_event_handle,
388 				    au_to_path(empty));
389 			return;
390 		}
391 		for (path = strtok_r(working_buf, " ", &last_str);
392 		    path; path = strtok_r(NULL, " ", &last_str)) {
393 			DPRINTF(("  path=%s\n", path));
394 			(void) au_write(event->ae_event_handle,
395 			    au_to_path(path));
396 		}
397 	} else {
398 		DPRINTF(("  Null path list\n"));
399 		if (required)
400 			(void) au_write(event->ae_event_handle,
401 			    au_to_path(empty));
402 	}
403 }
404 
405 /*
406  * AUT_PRIV
407  */
408 
409 /* ARGSUSED */
410 static void
411 adt_to_priv(datadef *def, void *p_data, int required,
412     struct adt_event_state *event, const char *priv_type)
413 {
414 	priv_set_t	*privilege;
415 
416 	privilege = ((union convert *)p_data)->tprivstar;
417 
418 	if (privilege != NULL) {
419 		(void) au_write(event->ae_event_handle,
420 		    au_to_privset(priv_type, privilege));
421 	} else {
422 		if (required) {
423 			DPRINTF(("  Null privilege\n"));
424 			(void) au_write(event->ae_event_handle,
425 			    au_to_privset(empty, NULL));
426 		}
427 	}
428 }
429 
430 /*
431  * -AUT_PRIV_L	AUT_PRIV for a limit set
432  */
433 
434 /* ARGSUSED */
435 static void
436 adt_to_priv_limit(datadef *def, void *p_data, int required,
437     struct adt_event_state *event, char *notUsed)
438 {
439 	adt_to_priv(def, p_data, required, event, PRIV_LIMIT);
440 }
441 
442 /*
443  * -AUT_PRIV_I	AUT_PRIV for an inherit set
444  */
445 
446 /* ARGSUSED */
447 static void
448 adt_to_priv_inherit(datadef *def, void *p_data, int required,
449     struct adt_event_state *event, char *notUsed)
450 {
451 	adt_to_priv(def, p_data, required, event, PRIV_INHERITABLE);
452 }
453 
454 /* ARGSUSED */
455 static void
456 adt_to_priv_effective(datadef *def, void *p_data, int required,
457     struct adt_event_state *event, char *notUsed)
458 {
459 	adt_to_priv(def, p_data, required, event, PRIV_EFFECTIVE);
460 }
461 
462 static void
463 getCharacteristics(struct auditpinfo_addr *info, pid_t *pid)
464 {
465 	int	rc;
466 
467 	if (*pid == 0)			/* getpinfo for this pid */
468 		info->ap_pid = getpid();
469 	else
470 		info->ap_pid = *pid;
471 
472 	rc = auditon(A_GETPINFO_ADDR, (caddr_t)info,
473 	    sizeof (struct auditpinfo_addr));
474 	if (rc == -1) {
475 		info->ap_auid = AU_NOAUDITID;
476 		info->ap_asid = 0;
477 		(void) memset((void *)&(info->ap_termid), 0,
478 		    sizeof (au_tid_addr_t));
479 		info->ap_termid.at_type = AU_IPv4;
480 	}
481 }
482 
483 /*
484  * AUT_PROCESS
485  *
486  */
487 
488 /* ARGSUSED */
489 static void
490 adt_to_process(datadef *def, void *p_data, int required,
491     struct adt_event_state *event, char *notUsed)
492 {
493 	au_id_t			auid;
494 	uid_t			euid;
495 	gid_t			egid;
496 	uid_t			ruid;
497 	gid_t			rgid;
498 	pid_t			pid;
499 	au_asid_t		sid;
500 	au_tid_addr_t		*tid;
501 	struct auditpinfo_addr	info;
502 
503 	auid = ((union convert *)p_data)->tuid;
504 	p_data = adt_adjust_address(p_data, sizeof (uid_t), sizeof (uid_t));
505 	euid = ((union convert *)p_data)->tuid;
506 	p_data = adt_adjust_address(p_data, sizeof (uid_t), sizeof (gid_t));
507 	egid = ((union convert *)p_data)->tgid;
508 	p_data = adt_adjust_address(p_data, sizeof (gid_t), sizeof (uid_t));
509 	ruid = ((union convert *)p_data)->tuid;
510 	p_data = adt_adjust_address(p_data, sizeof (uid_t), sizeof (gid_t));
511 	rgid = ((union convert *)p_data)->tgid;
512 	p_data = adt_adjust_address(p_data, sizeof (gid_t), sizeof (pid_t));
513 	pid  = ((union convert *)p_data)->tpid;
514 	p_data = adt_adjust_address(p_data, sizeof (pid_t), sizeof (uint32_t));
515 	sid  = ((union convert *)p_data)->tuint32;
516 	p_data = adt_adjust_address(p_data, sizeof (uint32_t),
517 	    sizeof (au_tid_addr_t *));
518 	tid  = ((union convert *)p_data)->ttermid;
519 
520 	getCharacteristics(&info, &pid);
521 
522 	if (auid == AU_NOAUDITID)
523 		auid = info.ap_auid;
524 
525 	if (euid == AU_NOAUDITID)
526 		euid = geteuid();
527 
528 	if (egid == AU_NOAUDITID)
529 		egid = getegid();
530 
531 	if (ruid == AU_NOAUDITID)
532 		ruid = getuid();
533 
534 	if (rgid == AU_NOAUDITID)
535 		rgid = getgid();
536 
537 	if (tid == NULL)
538 		tid = &(info.ap_termid);
539 
540 	if (sid == 0)
541 		sid = info.ap_asid;
542 
543 	if (pid == 0)
544 		pid = info.ap_pid;
545 
546 	(void) au_write(event->ae_event_handle,
547 	    au_to_process_ex(auid, euid, egid, ruid, rgid, pid, sid, tid));
548 }
549 
550 /*
551  * Generate subject information.
552  * If labels are present, generate the subject label token.
553  * If the group audit policy is set, generate the subject group token.
554  *
555  * The required flag does not apply here.
556  *
557  * Non-attributable records are indicated by an auid of AU_NOAUDITID;
558  * no subject token or group token is generated for a non-attributable
559  * record.
560  */
561 
562 /* ARGSUSED */
563 static void
564 adt_to_subject(datadef *def, void *p_data, int required,
565     struct adt_event_state *event, char *notUsed)
566 {
567 	struct adt_internal_state	*sp = event->ae_session;
568 
569 	if (sp->as_info.ai_auid == AU_NOAUDITID)
570 		return;
571 
572 	assert(sp->as_have_user_data == ADT_HAVE_ALL);
573 
574 	(void) au_write(event->ae_event_handle,
575 	    au_to_subject_ex(sp->as_info.ai_auid,
576 		sp->as_euid, sp->as_egid, sp->as_ruid, sp->as_rgid,
577 		sp->as_pid, sp->as_info.ai_asid,
578 		&(sp->as_info.ai_termid)));
579 	if (is_system_labeled()) {
580 		(void) au_write(event->ae_event_handle,
581 		    au_to_label(sp->as_label));
582 	}
583 	/*
584 	 * Add optional tokens if in the process model.
585 	 * In a session model, the groups list is undefined and label
586 	 * is in the state.
587 	 */
588 	if (sp->as_session_model == ADT_PROCESS_MODEL) {
589 		if (sp->as_kernel_audit_policy & AUDIT_GROUP) {
590 			int group_count;
591 			gid_t grouplist[NGROUPS_MAX];
592 
593 			if ((group_count = getgroups(NGROUPS_UMAX,
594 			    grouplist)) > 0) {
595 				(void) au_write(event->ae_event_handle,
596 				    au_to_newgroups(group_count, grouplist));
597 			}
598 		}
599 	}
600 }
601 
602 /*
603  * adt_to_text()
604  *
605  * The format string, normally null, is sort of a wrapper around
606  * the input.  adt_write_text() is a wrapper around au_write that
607  * handles the format string
608  *
609  */
610 #define	TEXT_LENGTH 49
611 
612 static void
613 adt_write_text(int handle, char *main_text, const char *format)
614 {
615 	char	buffer[TEXT_LENGTH * 2 + 1];
616 
617 	if (format == NULL)
618 		(void) au_write(handle, au_to_text(main_text));
619 	else {
620 		(void) snprintf(buffer, TEXT_LENGTH * 2, format, main_text);
621 		(void) au_write(handle, au_to_text(buffer));
622 	}
623 }
624 
625 static void
626 adt_to_text(datadef *def, void *p_data, int required,
627     struct adt_event_state *event, char *format)
628 {
629 	static int	have_syslogged = 0;
630 	char		*string;
631 	char		**string_list;
632 	char		buffer[TEXT_LENGTH + 1];
633 	time_t		date;
634 	struct tm	tm;
635 	uint32_t	*int_list;
636 	int		written, available;
637 	int		i, arrayCount;
638 	struct msg_text *list;
639 	int		list_index;
640 
641 	DPRINTF(("  adt_to_text dd_datatype=%d\n", def->dd_datatype));
642 	switch (def->dd_datatype) {
643 	case ADT_DATE:
644 		/*
645 		 * Consider creating a separate token type for dates
646 		 * -- store as longs and format them in praudit.
647 		 * For now, a date is input as a time_t and output as
648 		 * a text token.  If we do this, we need to consider
649 		 * carrying timezone info so that praudit can
650 		 * represent times in an unambiguous manner.
651 		 */
652 		date = ((union convert *)p_data)->tlong;
653 		if (strftime(buffer, sizeof (buffer), "%x",
654 		    localtime_r(&date, &tm)) > TEXT_LENGTH) {
655 			if (required)
656 				(void) strncpy(buffer, "invalid date",
657 				    TEXT_LENGTH);
658 			else
659 				break;
660 		}
661 		DPRINTF(("  text=%s\n", buffer));
662 		adt_write_text(event->ae_event_handle, buffer, format);
663 		break;
664 		/*
665 		 * The "input size" is overloaded to mean the list number
666 		 * and the msg_selector indexes the desired string in
667 		 * that list
668 		 */
669 	case ADT_MSG:
670 		list = &adt_msg_text[(enum adt_login_text)def->dd_input_size];
671 		list_index = ((union convert *)p_data)->msg_selector;
672 
673 		if ((list_index < list->ml_min_index) |
674 		    (list_index > list->ml_max_index))
675 			string = "Invalid message index";
676 		else
677 			string = list->ml_msg_list[list_index +
678 			    list->ml_offset];
679 
680 		if (string == NULL) {	/* null is valid; means skip */
681 			if (required) {
682 				string = empty;
683 			} else
684 				break;
685 		}
686 		DPRINTF(("  text=%s\n", string));
687 		adt_write_text(event->ae_event_handle, string, format);
688 		break;
689 	case ADT_UID:
690 	case ADT_GID:
691 	case ADT_UINT:
692 		(void) snprintf(buffer, TEXT_LENGTH, "%d",
693 		    ((union convert *)p_data)->tuint);
694 
695 		DPRINTF(("  text=%s\n", buffer));
696 		adt_write_text(event->ae_event_handle, buffer, format);
697 		break;
698 	case ADT_UIDSTAR:
699 	case ADT_GIDSTAR:
700 	case ADT_UINT32STAR:
701 		int_list = ((union convert *)p_data)->tuint32star;
702 		p_data = adt_adjust_address(p_data, sizeof (int *),
703 		    sizeof (int));
704 		arrayCount = ((union convert *)p_data)->tint;
705 
706 		string = buffer;
707 		available = TEXT_LENGTH;	/* space available in buffer */
708 
709 		if (arrayCount < 0)
710 			arrayCount = 0;
711 
712 		if ((arrayCount > 0) && (int_list != NULL)) {
713 			for (; arrayCount > 0; arrayCount--) {
714 				written = snprintf(string, available,
715 				    "%d ", *int_list++);
716 				if (written < 1)
717 					break;
718 				string += written;
719 				available -= written;
720 			}
721 		} else if (required)
722 			string = empty;
723 		else
724 			break;
725 
726 		adt_write_text(event->ae_event_handle, buffer, format);
727 		break;
728 	case ADT_ULONG:
729 		(void) snprintf(buffer, TEXT_LENGTH, "%ld",
730 		    ((union convert *)p_data)->tulong);
731 
732 		DPRINTF(("  text=%s\n", buffer));
733 		adt_write_text(event->ae_event_handle, buffer, format);
734 		break;
735 	case ADT_CHARSTAR:
736 		string = ((union convert *)p_data)->tcharstar;
737 
738 		if (string == NULL) {
739 			if (required)
740 				string = empty;
741 			else
742 				break;
743 		}
744 		DPRINTF(("  text=%s\n", string));
745 		adt_write_text(event->ae_event_handle, string, format);
746 		break;
747 	case ADT_CHAR2STAR:
748 		string_list = ((union convert *)p_data)->tchar2star;
749 		p_data = adt_adjust_address(p_data, sizeof (char **),
750 		    sizeof (int));
751 		arrayCount = ((union convert *)p_data)->tint;
752 
753 		if (arrayCount < 0)
754 			arrayCount = 0;
755 
756 		if ((arrayCount > 0) && (string_list != NULL)) {
757 			for (i = 0; i < arrayCount; i++) {
758 				string = string_list[i];
759 				if (string != NULL)
760 					adt_write_text(event->ae_event_handle,
761 					    string, format);
762 			}
763 		} else if (required)
764 			adt_write_text(event->ae_event_handle, empty, format);
765 		else
766 			break;
767 		break;
768 	default:
769 		if (!have_syslogged) { /* don't flood the log */
770 			adt_write_syslog("unsupported data conversion",
771 			    ENOTSUP);
772 			have_syslogged = 1;
773 		}
774 		break;
775 	}
776 	DFLUSH
777 }
778 
779 /* ARGSUSED */
780 static void
781 adt_to_uauth(datadef *def, void *p_data, int required,
782     struct adt_event_state *event, char *format)
783 {
784 	char		*string;
785 
786 	DPRINTF(("  adt_to_uauth dd_datatype=%d\n", def->dd_datatype));
787 
788 	string = ((union convert *)p_data)->tcharstar;
789 
790 	if (string == NULL) {
791 		if (required)
792 			string = empty;
793 		else
794 			return;
795 	}
796 	DPRINTF(("  text=%s\n", string));
797 	(void) au_write(event->ae_event_handle, au_to_uauth(string));
798 }
799 
800 /* ARGSUSED */
801 static void
802 adt_to_zonename(datadef *def, void *p_data, int required,
803     struct adt_event_state *event, char *notUsed)
804 {
805 	char	*name;
806 
807 	name = ((union convert *)p_data)->tcharstar;
808 
809 	if (name != NULL) {
810 		DPRINTF(("  name=%s\n", name));
811 		(void) au_write(event->ae_event_handle, au_to_zonename(name));
812 	} else {
813 		DPRINTF(("  Null name\n"));
814 		if (required)
815 			(void) au_write(event->ae_event_handle,
816 			    au_to_zonename(empty));
817 	}
818 }
819 
820 
821 /*
822  * no function for header -- the header is generated by au_close()
823  *
824  * no function for trailer -- the trailer is generated by au_close()
825  */
826 
827 #define	MAX_TOKEN_JMP 17
828 
829 static struct token_jmp token_table[MAX_TOKEN_JMP] =
830 {
831 	{AUT_CMD, adt_to_cmd},
832 	{ADT_CMD_ALT, adt_to_cmd1},
833 	{ADT_AUT_PRIV_L, adt_to_priv_limit},
834 	{ADT_AUT_PRIV_I, adt_to_priv_inherit},
835 	{ADT_AUT_PRIV_E, adt_to_priv_effective},
836 	{AUT_NEWGROUPS, adt_to_newgroups},
837 	{AUT_FMRI, adt_to_frmi},
838 	{AUT_LABEL, adt_to_label},
839 	{AUT_PATH, adt_to_path},
840 	{-AUT_PATH, adt_to_pathlist},	/* private extension of token values */
841 	{AUT_PROCESS, adt_to_process},
842 	{AUT_RETURN, adt_to_return},
843 	{AUT_SUBJECT, adt_to_subject},
844 	{AUT_TEXT, adt_to_text},
845 	{AUT_TID, adt_to_tid},
846 	{AUT_UAUTH, adt_to_uauth},
847 	{AUT_ZONENAME, adt_to_zonename}
848 };
849 /*
850  * {AUT_ARG, adt_to_arg},		   not used
851  * {AUT_ACL, adt_to_acl},                  not used
852  * {AUT_ARBITRARY, adt_to_arbitrary},      AUT_ARBITRARY is undefined
853  * {AUT_ATTR, adt_to_attr},		   not used in mountd
854  * {AUT_EXEC_ARGS, adt_to_exec_args},      not used
855  * {AUT_EXEC_ENV, adt_to_exec_env},        not used
856  * {AUT_EXIT, adt_to_exit},		   obsolete
857  * {AUT_FILE, adt_to_file},                AUT_FILE is undefined
858  * {AUT_GROUPS, adt_to_groups},            obsolete
859  * {AUT_HEADER, adt_to_header}             not used
860  * {AUT_IN_ADDR, adt_to_in_addr},	   not used
861  * {AUT_IP, adt_to_ip},			   not used
862  * {AUT_IPC, adt_to_ipc},                  not used
863  * {AUT_IPC_PERM, adt_to_ipc_perm},        not used
864  * {AUT_OPAQUE, adt_to_opaque},            not used
865  * {AUT_SEQ, adt_to_seq},                  not used
866  * {AUT_SOCKET, adt_to_socket},            not used
867  * {AUT_SOCKET_INET, adt_to_socket_inet},  AUT_SOCKET_INET is undefined
868  * {AUT_TRAILER, adt_to_trailer}           not used
869  */
870 
871 /* find function to generate token */
872 
873 static adt_token_func_t
874 adt_getTokenFunction(char token_id)
875 {
876 	int	i;
877 	struct token_jmp	*p_jmp = token_table;
878 
879 	for (i = 0; i < MAX_TOKEN_JMP; i++) {
880 		if (token_id == p_jmp->jmp_id) {
881 			return (p_jmp->jmp_to);
882 		}
883 		p_jmp++;
884 	}
885 	errno = EINVAL;
886 	return (NULL);
887 }
888 
889 /*
890  * adjustAddress -- given the address of data, its size, and the type of
891  * the next data field, calculate the offset to the next piece of data.
892  * Depending on the caller, "current" and "next" mean the current pointer
893  * and the next pointer or the last pointer and the current pointer.
894  */
895 void *
896 adt_adjust_address(void *current_address, size_t current_size,
897     size_t next_size)
898 {
899 	ptrdiff_t adjustment;
900 	ptrdiff_t remainder;
901 
902 	adjustment = (size_t)current_address + current_size;
903 
904 	if (next_size) {
905 		remainder = adjustment % next_size;
906 		if (remainder != 0)
907 			adjustment += next_size - remainder;
908 	}
909 	return ((char *)adjustment);
910 }
911