8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / lib / libbsm / common / adt_token.c
blobcad7c905b69d418722df62dec8672cca67087557
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
22 * adt_token.c
24 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
25 * Use is subject to license terms.
27 * This file does not provide any user callable functions. See adt.c
30 #include <bsm/adt.h>
31 #include <bsm/adt_event.h>
32 #include <bsm/audit.h>
34 #include <adt_xlate.h>
35 #include <alloca.h>
36 #include <assert.h>
37 #include <netdb.h>
38 #include <priv.h>
39 #include <string.h>
40 #include <strings.h>
41 #include <stdlib.h>
42 #include <time.h>
43 #include <unistd.h>
45 #include <sys/priv_names.h>
46 #include <sys/socket.h>
47 #include <sys/types.h>
48 #include <sys/vnode.h>
50 #include <tsol/label.h>
52 #ifdef C2_DEBUG
53 #define DPRINTF(x) { (void) printf x; }
54 #define DFLUSH (void) fflush(stdout);
56 /* 0x + Classification + Compartments + end of string */
57 #define HEX_SIZE 2 + 2*2 + 2*32 + 1
59 static char *
60 dprt_label(m_label_t *label)
62 static char hex[HEX_SIZE];
63 char *direct = NULL;
65 if (label_to_str(label, &direct, M_INTERNAL, DEF_NAMES) != 0) {
66 adt_write_syslog("label_to_str(M_INTERNAL)", errno);
67 return ("hex label failed");
69 (void) strlcpy(hex, direct, sizeof (hex));
70 free(direct);
71 return (hex);
73 #else /* !C2_DEBUG */
74 #define DPRINTF(x)
75 #define DFLUSH
76 #endif /* C2_DEBUG */
78 static adt_token_func_t adt_getTokenFunction(char);
80 static char *empty = "";
83 * call adt_token_open() first and adt_token_close() last.
85 * au_open is sort of broken; it returns a -1 when out of memory that
86 * you're supposed to ignore; au_write and au_close return without
87 * doing anything when a -1 is passed. This code sort of follows the
88 * au_open model except that it calls syslog to indicate underlying
89 * brokenness. Other than that, -1 is ignored.
92 void
93 adt_token_open(struct adt_event_state *event)
95 static int have_syslogged = 0;
97 event->ae_event_handle = au_open();
98 if (event->ae_event_handle < 0) {
99 if (!have_syslogged) {
100 adt_write_syslog("au_open failed", ENOMEM);
101 have_syslogged = 1;
103 } else {
104 have_syslogged = 0;
109 * call generate_token for each token in the order you want the tokens
110 * generated.
113 void
114 adt_generate_token(struct entry *p_entry, void *p_data,
115 struct adt_event_state *event)
117 adt_token_func_t p_func;
119 assert((p_entry != NULL) && (p_data != NULL) && (event != NULL));
121 p_func = adt_getTokenFunction(p_entry->en_token_id);
122 assert(p_func != NULL);
124 DPRINTF(("p_entry=%p, p_data=%p, offset=%llu, msgFmt=%s\n",
125 (void *)p_entry, p_data, (long long)p_entry->en_offset,
126 p_entry->en_msg_format));
127 DFLUSH
129 (*p_func)(p_entry->en_type_def,
130 (char *)p_data + p_entry->en_offset, p_entry->en_required, event,
131 p_entry->en_msg_format);
134 /* call this last */
137 adt_token_close(struct adt_event_state *event)
139 int rc;
141 rc = au_close(event->ae_event_handle, AU_TO_WRITE,
142 event->ae_internal_id);
143 if (rc < 0)
144 adt_write_syslog("au_close failed", errno);
145 return (rc);
149 * one function per token -- see the jump table at the end of file
152 /* ARGSUSED */
153 static void
154 adt_to_return(datadef *def, void *p_data, int required,
155 struct adt_event_state *event, char *notUsed)
158 #ifdef _LP64
159 (void) au_write(event->ae_event_handle,
160 au_to_return64((int64_t)event->ae_rc, event->ae_type));
161 #else
162 (void) au_write(event->ae_event_handle,
163 au_to_return32((int32_t)event->ae_rc, event->ae_type));
164 #endif
168 * AUT_CMD
170 * the command line is described with argc and argv and the environment
171 * with envp. The envp list is NULL terminated and has no separate
172 * counter; envp will be a NULL list unless the AUDIT_ARGE policy is
173 * set.
176 /* ARGSUSED */
177 static void
178 adt_to_cmd(datadef *def, void *p_data, int required,
179 struct adt_event_state *event, char *notUsed)
181 struct adt_internal_state *sp = event->ae_session;
182 int argc;
183 char **argv;
184 char **envp = NULL;
186 argc = ((union convert *)p_data)->tint;
187 p_data = adt_adjust_address(p_data, sizeof (int), sizeof (char **));
188 argv = ((union convert *)p_data)->tchar2star;
189 p_data = adt_adjust_address(p_data, sizeof (char **), sizeof (char **));
191 if (sp->as_kernel_audit_policy & AUDIT_ARGE)
192 envp = ((union convert *)p_data)->tchar2star;
194 (void) au_write(event->ae_event_handle,
195 au_to_cmd(argc, argv, envp));
199 * special case of AUT_CMD with 1 argument that is
200 * a string showing the whole command and no envp
202 /* ARGSUSED */
203 static void
204 adt_to_cmd1(datadef *def, void *p_data, int required,
205 struct adt_event_state *event, char *notUsed)
207 char *string;
209 string = ((union convert *)p_data)->tcharstar;
211 if (string == NULL) {
212 if (required) {
213 string = empty;
214 } else {
215 return;
218 /* argc is hardcoded as 1 */
219 (void) au_write(event->ae_event_handle, au_to_cmd(1, &string,
220 NULL));
224 * adt_to_tid -- generic address (ip is only one defined at present)
225 * input:
226 * terminal type: ADT_IPv4, ADT_IPv6...
227 * case: ADT_IPv4 or ADT_IPv6...
228 * ip type
229 * remote port
230 * local port
231 * address
232 * case: not defined...
234 /* ARGSUSED */
235 static void
236 adt_to_tid(datadef *def, void *p_data, int required,
237 struct adt_event_state *event, char *notUsed)
239 au_generic_tid_t tid;
240 uint32_t type;
241 au_ip_t *ip;
243 type = ((union convert *)p_data)->tuint32;
245 switch (type) {
246 case ADT_IPv4:
247 case ADT_IPv6:
248 p_data = adt_adjust_address(p_data, sizeof (uint32_t),
249 sizeof (uint32_t));
251 tid.gt_type = AU_IPADR;
252 ip = &(tid.gt_adr.at_ip);
254 ip->at_type = (type == ADT_IPv4) ?
255 AU_IPv4 : AU_IPv6;
257 ip->at_r_port = ((union convert *)p_data)->tuint16;
258 p_data = adt_adjust_address(p_data, sizeof (uint16_t),
259 sizeof (uint16_t));
261 ip->at_l_port = ((union convert *)p_data)->tuint16;
263 /* arg3 is for the array element, not the array size */
264 p_data = adt_adjust_address(p_data, sizeof (uint16_t),
265 sizeof (uint32_t));
267 (void) memcpy(ip->at_addr, p_data, ip->at_type);
268 break;
269 default:
270 adt_write_syslog("Invalid terminal id type", EINVAL);
271 return;
273 (void) au_write(event->ae_event_handle, au_to_tid(&tid));
277 * au_to_frmi takes a char * that is the fmri.
279 /* ARGSUSED */
280 static void
281 adt_to_frmi(datadef *def, void *p_data, int required,
282 struct adt_event_state *event, char *notUsed)
284 char *fmri;
286 DPRINTF((" adt_to_fmri dd_datatype=%d\n", def->dd_datatype));
288 fmri = ((union convert *)p_data)->tcharstar;
290 if (fmri == NULL) {
291 if (required) {
292 fmri = empty;
293 } else {
294 return;
297 DPRINTF((" fmri=%s\n", fmri));
298 (void) au_write(event->ae_event_handle, au_to_fmri(fmri));
302 * au_to_label takes an m_label_t * that is the label.
304 /* ARGSUSED */
305 static void
306 adt_to_label(datadef *def, void *p_data, int required,
307 struct adt_event_state *event, char *notUsed)
309 m_label_t *label;
311 DPRINTF((" adt_to_label dd_datatype=%d\n", def->dd_datatype));
313 label = ((union convert *)p_data)->tm_label;
315 if (label != NULL) {
316 DPRINTF((" label=%s\n", dprt_label(label)));
317 DFLUSH
318 (void) au_write(event->ae_event_handle, au_to_label(label));
319 } else {
320 DPRINTF((" Null label\n"));
321 if (required)
322 adt_write_syslog("adt_to_label no required label", 0);
327 * au_to_newgroups takes a length and an array of gids
328 * as input. The input to adt_to_newgroups is a length
329 * and a pointer to an array of gids.
332 /* ARGSUSED */
333 static void
334 adt_to_newgroups(datadef *def, void *p_data, int required,
335 struct adt_event_state *event, char *notUsed)
337 int n;
338 gid_t *groups;
340 n = ((union convert *)p_data)->tint;
341 if (n < 1) {
342 if (required) {
343 n = 0; /* in case negative n was passed */
344 } else {
345 return;
348 p_data = adt_adjust_address(p_data, sizeof (int), sizeof (int32_t *));
350 groups = ((union convert *)p_data)->tgidstar;
352 (void) au_write(event->ae_event_handle, au_to_newgroups(n, groups));
355 /* ARGSUSED */
356 static void
357 adt_to_path(datadef *def, void *p_data, int required,
358 struct adt_event_state *event, char *notUsed)
360 char *path;
362 path = ((union convert *)p_data)->tcharstar;
364 if (path != NULL) {
365 DPRINTF((" path=%s\n", path));
366 (void) au_write(event->ae_event_handle, au_to_path(path));
367 } else {
368 DPRINTF((" Null path\n"));
369 if (required) {
370 (void) au_write(event->ae_event_handle,
371 au_to_path(empty));
377 * dummy token id: AUT_PATHLIST
380 /* ARGSUSED */
381 static void
382 adt_to_pathlist(datadef *def, void *p_data, int required,
383 struct adt_event_state *event, char *notUsed)
385 char *path;
386 char *working_buf;
387 char *pathlist;
388 char *last_str;
390 pathlist = ((union convert *)p_data)->tcharstar;
392 if (pathlist != NULL) {
393 working_buf = strdup(pathlist);
394 if (working_buf == NULL) {
395 adt_write_syslog("audit failure", errno);
396 if (required) {
397 (void) au_write(event->ae_event_handle,
398 au_to_path(empty));
400 return;
402 for (path = strtok_r(working_buf, " ", &last_str);
403 path; path = strtok_r(NULL, " ", &last_str)) {
404 DPRINTF((" path=%s\n", path));
405 (void) au_write(event->ae_event_handle,
406 au_to_path(path));
408 } else {
409 DPRINTF((" Null path list\n"));
410 if (required)
411 (void) au_write(event->ae_event_handle,
412 au_to_path(empty));
417 * AUT_PRIV
420 /* ARGSUSED */
421 static void
422 adt_to_priv(datadef *def, void *p_data, int required,
423 struct adt_event_state *event, const char *priv_type)
425 priv_set_t *privilege;
427 privilege = ((union convert *)p_data)->tprivstar;
429 if (privilege != NULL) {
430 (void) au_write(event->ae_event_handle,
431 au_to_privset(priv_type, privilege));
432 } else {
433 if (required) {
434 DPRINTF((" Null privilege\n"));
435 (void) au_write(event->ae_event_handle,
436 au_to_privset(empty, NULL));
442 * -AUT_PRIV_L AUT_PRIV for a limit set
445 /* ARGSUSED */
446 static void
447 adt_to_priv_limit(datadef *def, void *p_data, int required,
448 struct adt_event_state *event, char *notUsed)
450 adt_to_priv(def, p_data, required, event, PRIV_LIMIT);
454 * -AUT_PRIV_I AUT_PRIV for an inherit set
457 /* ARGSUSED */
458 static void
459 adt_to_priv_inherit(datadef *def, void *p_data, int required,
460 struct adt_event_state *event, char *notUsed)
462 adt_to_priv(def, p_data, required, event, PRIV_INHERITABLE);
465 /* ARGSUSED */
466 static void
467 adt_to_priv_effective(datadef *def, void *p_data, int required,
468 struct adt_event_state *event, char *notUsed)
470 adt_to_priv(def, p_data, required, event, PRIV_EFFECTIVE);
473 static void
474 getCharacteristics(struct auditpinfo_addr *info, pid_t *pid)
476 int rc;
478 if (*pid == 0) { /* getpinfo for this pid */
479 info->ap_pid = getpid();
480 } else {
481 info->ap_pid = *pid;
484 rc = auditon(A_GETPINFO_ADDR, (caddr_t)info,
485 sizeof (struct auditpinfo_addr));
486 if (rc == -1) {
487 info->ap_auid = AU_NOAUDITID;
488 info->ap_asid = 0;
489 (void) memset((void *)&(info->ap_termid), 0,
490 sizeof (au_tid_addr_t));
491 info->ap_termid.at_type = AU_IPv4;
496 * AUT_PROCESS
500 /* ARGSUSED */
501 static void
502 adt_to_process(datadef *def, void *p_data, int required,
503 struct adt_event_state *event, char *notUsed)
505 au_id_t auid;
506 uid_t euid;
507 gid_t egid;
508 uid_t ruid;
509 gid_t rgid;
510 pid_t pid;
511 au_asid_t sid;
512 au_tid_addr_t *tid;
513 struct auditpinfo_addr info;
515 auid = ((union convert *)p_data)->tuid;
516 p_data = adt_adjust_address(p_data, sizeof (uid_t), sizeof (uid_t));
517 euid = ((union convert *)p_data)->tuid;
518 p_data = adt_adjust_address(p_data, sizeof (uid_t), sizeof (gid_t));
519 egid = ((union convert *)p_data)->tgid;
520 p_data = adt_adjust_address(p_data, sizeof (gid_t), sizeof (uid_t));
521 ruid = ((union convert *)p_data)->tuid;
522 p_data = adt_adjust_address(p_data, sizeof (uid_t), sizeof (gid_t));
523 rgid = ((union convert *)p_data)->tgid;
524 p_data = adt_adjust_address(p_data, sizeof (gid_t), sizeof (pid_t));
525 pid = ((union convert *)p_data)->tpid;
526 p_data = adt_adjust_address(p_data, sizeof (pid_t), sizeof (uint32_t));
527 sid = ((union convert *)p_data)->tuint32;
528 p_data = adt_adjust_address(p_data, sizeof (uint32_t),
529 sizeof (au_tid_addr_t *));
530 tid = ((union convert *)p_data)->ttermid;
532 getCharacteristics(&info, &pid);
534 if (auid == AU_NOAUDITID)
535 auid = info.ap_auid;
537 if (euid == AU_NOAUDITID)
538 euid = geteuid();
540 if (egid == AU_NOAUDITID)
541 egid = getegid();
543 if (ruid == AU_NOAUDITID)
544 ruid = getuid();
546 if (rgid == AU_NOAUDITID)
547 rgid = getgid();
549 if (tid == NULL)
550 tid = &(info.ap_termid);
552 if (sid == 0)
553 sid = info.ap_asid;
555 if (pid == 0)
556 pid = info.ap_pid;
558 (void) au_write(event->ae_event_handle,
559 au_to_process_ex(auid, euid, egid, ruid, rgid, pid, sid, tid));
563 * Generate subject information.
564 * If labels are present, generate the subject label token.
565 * If the group audit policy is set, generate the subject group token.
567 * The required flag does not apply here.
569 * Non-attributable records are indicated by an auid of AU_NOAUDITID;
570 * no subject token or group token is generated for a non-attributable
571 * record.
574 /* ARGSUSED */
575 static void
576 adt_to_subject(datadef *def, void *p_data, int required,
577 struct adt_event_state *event, char *notUsed)
579 struct adt_internal_state *sp = event->ae_session;
581 if (sp->as_info.ai_auid == AU_NOAUDITID)
582 return;
584 assert(sp->as_have_user_data == ADT_HAVE_ALL);
586 (void) au_write(event->ae_event_handle,
587 au_to_subject_ex(sp->as_info.ai_auid,
588 sp->as_euid, sp->as_egid, sp->as_ruid, sp->as_rgid,
589 sp->as_pid, sp->as_info.ai_asid,
590 &(sp->as_info.ai_termid)));
591 if (is_system_labeled()) {
592 (void) au_write(event->ae_event_handle,
593 au_to_label(sp->as_label));
596 * Add optional tokens if in the process model.
597 * In a session model, the groups list is undefined and label
598 * is in the state.
600 if (sp->as_session_model == ADT_PROCESS_MODEL) {
601 if (sp->as_kernel_audit_policy & AUDIT_GROUP) {
602 int group_count;
603 int maxgrp = getgroups(0, NULL);
604 gid_t *grouplist = alloca(maxgrp * sizeof (gid_t));
606 if ((group_count = getgroups(maxgrp, grouplist)) > 0) {
607 (void) au_write(event->ae_event_handle,
608 au_to_newgroups(group_count, grouplist));
615 * adt_to_text()
617 * The format string, normally null, is sort of a wrapper around
618 * the input. adt_write_text() is a wrapper around au_write that
619 * handles the format string
622 #define TEXT_LENGTH 49
624 static void
625 adt_write_text(int handle, char *main_text, const char *format)
627 char buffer[TEXT_LENGTH * 2 + 1];
629 if (format == NULL) {
630 (void) au_write(handle, au_to_text(main_text));
631 } else {
632 (void) snprintf(buffer, TEXT_LENGTH * 2, format, main_text);
633 (void) au_write(handle, au_to_text(buffer));
637 static void
638 adt_to_text(datadef *def, void *p_data, int required,
639 struct adt_event_state *event, char *format)
641 static int have_syslogged = 0;
642 char *string;
643 char **string_list;
644 char buffer[TEXT_LENGTH + 1];
645 time_t date;
646 struct tm tm;
647 uint32_t *int_list;
648 int written, available;
649 int i, arrayCount;
650 struct msg_text *list;
651 int list_index;
653 DPRINTF((" adt_to_text dd_datatype=%d\n", def->dd_datatype));
654 switch (def->dd_datatype) {
655 case ADT_DATE:
657 * Consider creating a separate token type for dates
658 * -- store as longs and format them in praudit.
659 * For now, a date is input as a time_t and output as
660 * a text token. If we do this, we need to consider
661 * carrying timezone info so that praudit can
662 * represent times in an unambiguous manner.
664 date = ((union convert *)p_data)->tlong;
665 if (strftime(buffer, sizeof (buffer), "%x",
666 localtime_r(&date, &tm)) > TEXT_LENGTH) {
667 if (required) {
668 (void) strncpy(buffer, "invalid date",
669 TEXT_LENGTH);
670 } else {
671 break;
674 DPRINTF((" text=%s\n", buffer));
675 adt_write_text(event->ae_event_handle, buffer, format);
676 break;
678 * The "input size" is overloaded to mean the list number
679 * and the msg_selector indexes the desired string in
680 * that list
682 case ADT_MSG:
683 list = &adt_msg_text[(enum adt_msg_list)def->dd_input_size];
684 list_index = ((union convert *)p_data)->msg_selector;
686 if ((list_index + list->ml_offset < list->ml_min_index) ||
687 (list_index + list->ml_offset > list->ml_max_index)) {
688 string = "Invalid message index";
689 } else {
690 string = list->ml_msg_list[list_index +
691 list->ml_offset];
694 if (string == NULL) { /* null is valid; means skip */
695 if (required) {
696 string = empty;
697 } else {
698 break;
701 DPRINTF((" text=%s\n", string));
702 adt_write_text(event->ae_event_handle, string, format);
703 break;
704 case ADT_UID:
705 case ADT_GID:
706 case ADT_UINT:
707 case ADT_UINT32:
708 (void) snprintf(buffer, TEXT_LENGTH, "%u",
709 ((union convert *)p_data)->tuint);
711 DPRINTF((" text=%s\n", buffer));
712 adt_write_text(event->ae_event_handle, buffer, format);
713 break;
714 case ADT_INT:
715 case ADT_INT32:
716 (void) snprintf(buffer, TEXT_LENGTH, "%d",
717 ((union convert *)p_data)->tint);
719 DPRINTF((" text=%s\n", buffer));
720 adt_write_text(event->ae_event_handle, buffer, format);
721 break;
722 case ADT_LONG:
723 (void) snprintf(buffer, TEXT_LENGTH, "%ld",
724 ((union convert *)p_data)->tlong);
726 DPRINTF((" text=%s\n", buffer));
727 adt_write_text(event->ae_event_handle, buffer, format);
728 break;
729 case ADT_UIDSTAR:
730 case ADT_GIDSTAR:
731 case ADT_UINT32STAR:
732 int_list = ((union convert *)p_data)->tuint32star;
733 p_data = adt_adjust_address(p_data, sizeof (int *),
734 sizeof (int));
735 arrayCount = ((union convert *)p_data)->tint;
737 string = buffer;
738 available = TEXT_LENGTH; /* space available in buffer */
740 if (arrayCount < 0)
741 arrayCount = 0;
743 if ((arrayCount > 0) && (int_list != NULL)) {
744 for (; arrayCount > 0; arrayCount--) {
745 written = snprintf(string, available,
746 "%d ", *int_list++);
747 if (written < 1)
748 break;
749 string += written;
750 available -= written;
752 } else if (required) {
753 string = empty;
754 } else {
755 break;
758 adt_write_text(event->ae_event_handle, buffer, format);
759 break;
760 case ADT_ULONG:
761 (void) snprintf(buffer, TEXT_LENGTH, "%lu",
762 ((union convert *)p_data)->tulong);
764 DPRINTF((" text=%s\n", buffer));
765 adt_write_text(event->ae_event_handle, buffer, format);
766 break;
767 case ADT_UINT64:
768 (void) snprintf(buffer, TEXT_LENGTH, "%llu",
769 ((union convert *)p_data)->tuint64);
771 DPRINTF((" text=%s\n", buffer));
772 adt_write_text(event->ae_event_handle, buffer, format);
773 break;
774 case ADT_CHARSTAR:
775 string = ((union convert *)p_data)->tcharstar;
777 if (string == NULL) {
778 if (required) {
779 string = empty;
780 } else {
781 break;
784 DPRINTF((" text=%s\n", string));
785 adt_write_text(event->ae_event_handle, string, format);
786 break;
787 case ADT_CHAR2STAR:
788 string_list = ((union convert *)p_data)->tchar2star;
789 p_data = adt_adjust_address(p_data, sizeof (char **),
790 sizeof (int));
791 arrayCount = ((union convert *)p_data)->tint;
793 if (arrayCount < 0)
794 arrayCount = 0;
796 if ((arrayCount > 0) && (string_list != NULL)) {
797 for (i = 0; i < arrayCount; i++) {
798 string = string_list[i];
799 if (string != NULL)
800 adt_write_text(event->ae_event_handle,
801 string, format);
803 } else if (required) {
804 adt_write_text(event->ae_event_handle, empty, format);
805 } else {
806 break;
808 break;
809 default:
810 if (!have_syslogged) { /* don't flood the log */
811 adt_write_syslog("unsupported data conversion",
812 ENOTSUP);
813 have_syslogged = 1;
815 break;
817 DFLUSH
821 * AUT_UAUTH
824 /* ARGSUSED */
825 static void
826 adt_to_uauth(datadef *def, void *p_data, int required,
827 struct adt_event_state *event, char *format)
829 char *string;
831 DPRINTF((" adt_to_uauth dd_datatype=%d\n", def->dd_datatype));
833 string = ((union convert *)p_data)->tcharstar;
835 if (string == NULL) {
836 if (required) {
837 string = empty;
838 } else {
839 return;
842 DPRINTF((" text=%s\n", string));
843 (void) au_write(event->ae_event_handle, au_to_uauth(string));
847 * AUT_USER
850 /* ARGSUSED */
851 static void
852 adt_to_user(datadef *def, void *p_data, int required,
853 struct adt_event_state *event, char *format)
855 uid_t uid;
856 char *username;
858 DPRINTF((" adt_to_user dd_datatype=%d\n", def->dd_datatype));
860 uid = ((union convert *)p_data)->tuid;
861 p_data = adt_adjust_address(p_data, sizeof (uid_t), sizeof (uid_t));
863 username = ((union convert *)p_data)->tcharstar;
865 if (username == NULL) {
866 if (required) {
867 username = empty;
868 } else {
869 return;
872 DPRINTF((" username=%s\n", username));
873 (void) au_write(event->ae_event_handle, au_to_user(uid, username));
877 * AUT_ZONENAME
880 /* ARGSUSED */
881 static void
882 adt_to_zonename(datadef *def, void *p_data, int required,
883 struct adt_event_state *event, char *notUsed)
885 char *name;
887 name = ((union convert *)p_data)->tcharstar;
889 if (name != NULL) {
890 DPRINTF((" name=%s\n", name));
891 (void) au_write(event->ae_event_handle, au_to_zonename(name));
892 } else {
893 DPRINTF((" Null name\n"));
894 if (required) {
895 (void) au_write(event->ae_event_handle,
896 au_to_zonename(empty));
902 * ADT_IN_PEER dummy token
905 /* ARGSUSED */
906 static void
907 adt_to_in_peer(datadef *def, void *p_data, int required,
908 struct adt_event_state *event, char *notUsed)
910 int sock;
911 struct sockaddr_in6 peer;
912 int peerlen = sizeof (peer);
914 DPRINTF((" adt_to_in_peer dd_datatype=%d\n", def->dd_datatype));
916 sock = ((union convert *)p_data)->tfd;
918 if (sock < 0) {
919 DPRINTF((" Socket fd %d\n", sock));
920 if (required) {
921 adt_write_syslog("adt_to_in_peer no required socket",
924 return;
926 if (getpeername(sock, (struct sockaddr *)&peer, (socklen_t *)&peerlen)
927 < 0) {
929 adt_write_syslog("adt_to_in_addr getpeername", errno);
930 return;
932 if (peer.sin6_family == AF_INET6) {
933 (void) au_write(event->ae_event_handle,
934 au_to_in_addr_ex(&(peer.sin6_addr)));
935 (void) au_write(event->ae_event_handle,
936 au_to_iport((ushort_t)peer.sin6_port));
937 } else {
938 (void) au_write(event->ae_event_handle,
939 au_to_in_addr(&(((struct sockaddr_in *)&peer)->sin_addr)));
940 (void) au_write(event->ae_event_handle,
941 au_to_iport(
942 (ushort_t)(((struct sockaddr_in *)&peer)->sin_port)));
947 * ADT_IN_REMOTE dummy token
949 * Similar to ADT_IN_PEER except the input is
950 * an IP address type (ADT_IPv4 | ADT_IPv6) and an address V4/V6
953 /* ARGSUSED */
954 static void
955 adt_to_in_remote(datadef *def, void *p_data, int required,
956 struct adt_event_state *event, char *notUsed)
958 int32_t type;
960 DPRINTF((" adt_to_in_remote dd_datatype=%d\n", def->dd_datatype));
962 type = ((union convert *)p_data)->tuint32;
964 if (type == 0) {
965 if (required == 0) {
966 return;
968 /* required and not specified */
969 adt_write_syslog("adt_to_in_remote required address not "
970 "specified", 0);
971 type = ADT_IPv4;
973 p_data = adt_adjust_address(p_data, sizeof (int32_t),
974 sizeof (uint32_t));
976 switch (type) {
977 case ADT_IPv4:
978 (void) au_write(event->ae_event_handle, au_to_in_addr(
979 (struct in_addr *)&(((union convert *)p_data)->tuint32)));
980 break;
981 case ADT_IPv6:
982 (void) au_write(event->ae_event_handle, au_to_in_addr_ex(
983 (struct in6_addr *)&(((union convert *)p_data)->tuint32)));
984 break;
985 default:
986 adt_write_syslog("adt_to_in_remote invalid type", EINVAL);
987 return;
992 * adt_to_iport takes a uint16_t IP port.
995 /* ARGSUSED */
996 static void
997 adt_to_iport(datadef *def, void *p_data, int required,
998 struct adt_event_state *event, char *notUsed)
1000 ushort_t port;
1002 DPRINTF((" adt_to_iport dd_datatype=%d\n", def->dd_datatype));
1004 port = ((union convert *)p_data)->tuint16;
1006 if (port == 0) {
1007 if (required == 0) {
1008 return;
1010 /* required and not specified */
1011 adt_write_syslog("adt_to_iport no required port", 0);
1013 (void) au_write(event->ae_event_handle, au_to_iport(port));
1019 * This is a compact table that defines only the tokens that are
1020 * actually generated in the adt.xml file. It can't be a pure
1021 * indexed table because the adt.xml language defines internal extension
1022 * tokens for some processing. VIZ. ADT_CMD_ALT, ADT_AUT_PRIV_* (see
1023 * adt_xlate.h), and the -AUT_PATH value.
1026 #define MAX_TOKEN_JMP 21
1028 static struct token_jmp token_table[MAX_TOKEN_JMP] =
1030 {AUT_CMD, adt_to_cmd},
1031 {ADT_CMD_ALT, adt_to_cmd1},
1032 {AUT_FMRI, adt_to_frmi},
1033 {ADT_IN_PEER, adt_to_in_peer},
1034 {ADT_IN_REMOTE, adt_to_in_remote},
1035 {AUT_IPORT, adt_to_iport},
1036 {AUT_LABEL, adt_to_label},
1037 {AUT_NEWGROUPS, adt_to_newgroups},
1038 {AUT_PATH, adt_to_path},
1039 {-AUT_PATH, adt_to_pathlist}, /* private extension of token values */
1040 {ADT_AUT_PRIV_L, adt_to_priv_limit},
1041 {ADT_AUT_PRIV_I, adt_to_priv_inherit},
1042 {ADT_AUT_PRIV_E, adt_to_priv_effective},
1043 {AUT_PROCESS, adt_to_process},
1044 {AUT_RETURN, adt_to_return},
1045 {AUT_SUBJECT, adt_to_subject},
1046 {AUT_TEXT, adt_to_text},
1047 {AUT_TID, adt_to_tid},
1048 {AUT_UAUTH, adt_to_uauth},
1049 {AUT_USER, adt_to_user},
1050 {AUT_ZONENAME, adt_to_zonename}
1054 * {AUT_ACL, adt_to_acl}, not used
1055 * {AUT_ARBITRARY, adt_to_arbitrary}, AUT_ARBITRARY is undefined
1056 * {AUT_ARG, adt_to_arg}, not used
1057 * {AUT_ATTR, adt_to_attr}, not used in mountd
1058 * {AUT_XATOM, adt_to_atom}, not used
1059 * {AUT_EXEC_ARGS, adt_to_exec_args}, not used
1060 * {AUT_EXEC_ENV, adt_to_exec_env}, not used
1061 * {AUT_EXIT, adt_to_exit}, obsolete
1062 * {AUT_FILE, adt_to_file}, AUT_FILE is undefined
1063 * {AUT_XCOLORMAP, adt_to_colormap}, not used
1064 * {AUT_XCURSOR, adt_to_cursor}, not used
1065 * {AUT_XFONT, adt_to_font}, not used
1066 * {AUT_XGC, adt_to_gc}, not used
1067 * {AUT_GROUPS, adt_to_groups}, obsolete
1068 * {AUT_HEADER, adt_to_header}, generated by au_close
1069 * {AUT_IP, adt_to_ip}, not used
1070 * {AUT_IPC, adt_to_ipc}, not used
1071 * {AUT_IPC_PERM, adt_to_ipc_perm}, not used
1072 * {AUT_OPAQUE, adt_to_opaque}, not used
1073 * {AUT_XPIXMAP, adt_to_pixmap}, not used
1074 * {AUT_XPROPERTY, adt_to_property}, not used
1075 * {AUT_SEQ, adt_to_seq}, not used
1076 * {AUT_SOCKET, adt_to_socket}, not used
1077 * {AUT_SOCKET_INET, adt_to_socket_inet}, AUT_SOCKET_INET is undefined
1078 * {AUT_TRAILER, adt_to_trailer}, generated by au_close
1079 * {AUT_XCLIENT, adt_to_xclient} not used
1082 /* find function to generate token */
1084 static adt_token_func_t
1085 adt_getTokenFunction(char token_id)
1087 int i;
1088 struct token_jmp *p_jmp = token_table;
1090 for (i = 0; i < MAX_TOKEN_JMP; i++) {
1091 if (token_id == p_jmp->jmp_id) {
1092 return (p_jmp->jmp_to);
1094 p_jmp++;
1096 errno = EINVAL;
1097 return (NULL);
1101 * adjustAddress -- given the address of data, its size, and the type of
1102 * the next data field, calculate the offset to the next piece of data.
1103 * Depending on the caller, "current" and "next" mean the current pointer
1104 * and the next pointer or the last pointer and the current pointer.
1106 void *
1107 adt_adjust_address(void *current_address, size_t current_size,
1108 size_t next_size)
1110 ptrdiff_t adjustment;
1111 ptrdiff_t remainder;
1113 adjustment = (size_t)current_address + current_size;
1115 if (next_size) {
1116 remainder = adjustment % next_size;
1117 if (remainder != 0)
1118 adjustment += next_size - remainder;
1120 return ((char *)adjustment);