xref: /titanic_52/usr/src/lib/libbsm/common/adt_token.c (revision 80b80bf0416a7eb4be4215b2e192cafd03ca80b7)
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 2007 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 	case ADT_UINT32:
693 		(void) snprintf(buffer, TEXT_LENGTH, "%u",
694 		    ((union convert *)p_data)->tuint);
695 
696 		DPRINTF(("  text=%s\n", buffer));
697 		adt_write_text(event->ae_event_handle, buffer, format);
698 		break;
699 	case ADT_INT:
700 	case ADT_INT32:
701 		(void) snprintf(buffer, TEXT_LENGTH, "%d",
702 		    ((union convert *)p_data)->tint);
703 
704 		DPRINTF(("  text=%s\n", buffer));
705 		adt_write_text(event->ae_event_handle, buffer, format);
706 		break;
707 	case ADT_LONG:
708 		(void) snprintf(buffer, TEXT_LENGTH, "%ld",
709 		    ((union convert *)p_data)->tlong);
710 
711 		DPRINTF(("  text=%s\n", buffer));
712 		adt_write_text(event->ae_event_handle, buffer, format);
713 		break;
714 	case ADT_UIDSTAR:
715 	case ADT_GIDSTAR:
716 	case ADT_UINT32STAR:
717 		int_list = ((union convert *)p_data)->tuint32star;
718 		p_data = adt_adjust_address(p_data, sizeof (int *),
719 		    sizeof (int));
720 		arrayCount = ((union convert *)p_data)->tint;
721 
722 		string = buffer;
723 		available = TEXT_LENGTH;	/* space available in buffer */
724 
725 		if (arrayCount < 0)
726 			arrayCount = 0;
727 
728 		if ((arrayCount > 0) && (int_list != NULL)) {
729 			for (; arrayCount > 0; arrayCount--) {
730 				written = snprintf(string, available,
731 				    "%d ", *int_list++);
732 				if (written < 1)
733 					break;
734 				string += written;
735 				available -= written;
736 			}
737 		} else if (required)
738 			string = empty;
739 		else
740 			break;
741 
742 		adt_write_text(event->ae_event_handle, buffer, format);
743 		break;
744 	case ADT_ULONG:
745 		(void) snprintf(buffer, TEXT_LENGTH, "%lu",
746 		    ((union convert *)p_data)->tulong);
747 
748 		DPRINTF(("  text=%s\n", buffer));
749 		adt_write_text(event->ae_event_handle, buffer, format);
750 		break;
751 	case ADT_UINT64:
752 		(void) snprintf(buffer, TEXT_LENGTH, "%llu",
753 		    ((union convert *)p_data)->tuint64);
754 
755 		DPRINTF(("  text=%s\n", buffer));
756 		adt_write_text(event->ae_event_handle, buffer, format);
757 		break;
758 	case ADT_CHARSTAR:
759 		string = ((union convert *)p_data)->tcharstar;
760 
761 		if (string == NULL) {
762 			if (required)
763 				string = empty;
764 			else
765 				break;
766 		}
767 		DPRINTF(("  text=%s\n", string));
768 		adt_write_text(event->ae_event_handle, string, format);
769 		break;
770 	case ADT_CHAR2STAR:
771 		string_list = ((union convert *)p_data)->tchar2star;
772 		p_data = adt_adjust_address(p_data, sizeof (char **),
773 		    sizeof (int));
774 		arrayCount = ((union convert *)p_data)->tint;
775 
776 		if (arrayCount < 0)
777 			arrayCount = 0;
778 
779 		if ((arrayCount > 0) && (string_list != NULL)) {
780 			for (i = 0; i < arrayCount; i++) {
781 				string = string_list[i];
782 				if (string != NULL)
783 					adt_write_text(event->ae_event_handle,
784 					    string, format);
785 			}
786 		} else if (required)
787 			adt_write_text(event->ae_event_handle, empty, format);
788 		else
789 			break;
790 		break;
791 	default:
792 		if (!have_syslogged) { /* don't flood the log */
793 			adt_write_syslog("unsupported data conversion",
794 			    ENOTSUP);
795 			have_syslogged = 1;
796 		}
797 		break;
798 	}
799 	DFLUSH
800 }
801 
802 /* ARGSUSED */
803 static void
804 adt_to_uauth(datadef *def, void *p_data, int required,
805     struct adt_event_state *event, char *format)
806 {
807 	char		*string;
808 
809 	DPRINTF(("  adt_to_uauth dd_datatype=%d\n", def->dd_datatype));
810 
811 	string = ((union convert *)p_data)->tcharstar;
812 
813 	if (string == NULL) {
814 		if (required)
815 			string = empty;
816 		else
817 			return;
818 	}
819 	DPRINTF(("  text=%s\n", string));
820 	(void) au_write(event->ae_event_handle, au_to_uauth(string));
821 }
822 
823 /* ARGSUSED */
824 static void
825 adt_to_zonename(datadef *def, void *p_data, int required,
826     struct adt_event_state *event, char *notUsed)
827 {
828 	char	*name;
829 
830 	name = ((union convert *)p_data)->tcharstar;
831 
832 	if (name != NULL) {
833 		DPRINTF(("  name=%s\n", name));
834 		(void) au_write(event->ae_event_handle, au_to_zonename(name));
835 	} else {
836 		DPRINTF(("  Null name\n"));
837 		if (required)
838 			(void) au_write(event->ae_event_handle,
839 			    au_to_zonename(empty));
840 	}
841 }
842 
843 
844 /*
845  * no function for header -- the header is generated by au_close()
846  *
847  * no function for trailer -- the trailer is generated by au_close()
848  */
849 
850 #define	MAX_TOKEN_JMP 17
851 
852 static struct token_jmp token_table[MAX_TOKEN_JMP] =
853 {
854 	{AUT_CMD, adt_to_cmd},
855 	{ADT_CMD_ALT, adt_to_cmd1},
856 	{ADT_AUT_PRIV_L, adt_to_priv_limit},
857 	{ADT_AUT_PRIV_I, adt_to_priv_inherit},
858 	{ADT_AUT_PRIV_E, adt_to_priv_effective},
859 	{AUT_NEWGROUPS, adt_to_newgroups},
860 	{AUT_FMRI, adt_to_frmi},
861 	{AUT_LABEL, adt_to_label},
862 	{AUT_PATH, adt_to_path},
863 	{-AUT_PATH, adt_to_pathlist},	/* private extension of token values */
864 	{AUT_PROCESS, adt_to_process},
865 	{AUT_RETURN, adt_to_return},
866 	{AUT_SUBJECT, adt_to_subject},
867 	{AUT_TEXT, adt_to_text},
868 	{AUT_TID, adt_to_tid},
869 	{AUT_UAUTH, adt_to_uauth},
870 	{AUT_ZONENAME, adt_to_zonename}
871 };
872 /*
873  * {AUT_ARG, adt_to_arg},		   not used
874  * {AUT_ACL, adt_to_acl},                  not used
875  * {AUT_ARBITRARY, adt_to_arbitrary},      AUT_ARBITRARY is undefined
876  * {AUT_ATTR, adt_to_attr},		   not used in mountd
877  * {AUT_EXEC_ARGS, adt_to_exec_args},      not used
878  * {AUT_EXEC_ENV, adt_to_exec_env},        not used
879  * {AUT_EXIT, adt_to_exit},		   obsolete
880  * {AUT_FILE, adt_to_file},                AUT_FILE is undefined
881  * {AUT_GROUPS, adt_to_groups},            obsolete
882  * {AUT_HEADER, adt_to_header}             not used
883  * {AUT_IN_ADDR, adt_to_in_addr},	   not used
884  * {AUT_IP, adt_to_ip},			   not used
885  * {AUT_IPC, adt_to_ipc},                  not used
886  * {AUT_IPC_PERM, adt_to_ipc_perm},        not used
887  * {AUT_OPAQUE, adt_to_opaque},            not used
888  * {AUT_SEQ, adt_to_seq},                  not used
889  * {AUT_SOCKET, adt_to_socket},            not used
890  * {AUT_SOCKET_INET, adt_to_socket_inet},  AUT_SOCKET_INET is undefined
891  * {AUT_TRAILER, adt_to_trailer}           not used
892  */
893 
894 /* find function to generate token */
895 
896 static adt_token_func_t
897 adt_getTokenFunction(char token_id)
898 {
899 	int	i;
900 	struct token_jmp	*p_jmp = token_table;
901 
902 	for (i = 0; i < MAX_TOKEN_JMP; i++) {
903 		if (token_id == p_jmp->jmp_id) {
904 			return (p_jmp->jmp_to);
905 		}
906 		p_jmp++;
907 	}
908 	errno = EINVAL;
909 	return (NULL);
910 }
911 
912 /*
913  * adjustAddress -- given the address of data, its size, and the type of
914  * the next data field, calculate the offset to the next piece of data.
915  * Depending on the caller, "current" and "next" mean the current pointer
916  * and the next pointer or the last pointer and the current pointer.
917  */
918 void *
919 adt_adjust_address(void *current_address, size_t current_size,
920     size_t next_size)
921 {
922 	ptrdiff_t adjustment;
923 	ptrdiff_t remainder;
924 
925 	adjustment = (size_t)current_address + current_size;
926 
927 	if (next_size) {
928 		remainder = adjustment % next_size;
929 		if (remainder != 0)
930 			adjustment += next_size - remainder;
931 	}
932 	return ((char *)adjustment);
933 }
934