xref: /illumos-gate/usr/src/cmd/hal/utils/adt_data.c (revision dd72704bd9e794056c558153663c739e2012d721)
1 /***************************************************************************
2  *
3  * adt_data.c : Provides Audit functionalities
4  *
5  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
6  * Use is subject to license terms.
7  *
8  * Licensed under the Academic Free License version 2.1
9  *
10  ***************************************************************************/
11 
12 #include <stdio.h>
13 #include <errno.h>
14 #include <string.h>
15 #include <stdlib.h>
16 #include <glib.h>
17 #include <dbus/dbus-glib-lowlevel.h>
18 #include <dbus/dbus-glib.h>
19 #include "../hald/logger.h"
20 #include "adt_data.h"
21 
22 adt_export_data_t *
23 get_audit_export_data(DBusConnection *bus, const char *invoked_by_syscon_name, size_t *data_size)
24 {
25 	DBusMessage *message;
26 	DBusMessage *reply;
27 	DBusMessageIter iter, subiter;
28 	DBusError error;
29 	int count, bufsize;
30 	uchar_t *buf;
31 	uchar_t value;
32 
33 	message = dbus_message_new_method_call ("org.freedesktop.DBus",
34 						"/org/freedesktop/DBus",
35 						"org.freedesktop.DBus",
36 						"GetAdtAuditSessionData");
37 	if (message == NULL) {
38 		printf ("cannot get GetAdtAuditSessionData message\n");
39 		return NULL;
40 	}
41 
42 	if (!dbus_message_append_args(message, DBUS_TYPE_STRING, &invoked_by_syscon_name,
43 	    DBUS_TYPE_INVALID)) {
44 		dbus_message_unref(message);
45 		return NULL;
46 	}
47 
48 	dbus_error_init (&error);
49 	reply = dbus_connection_send_with_reply_and_block (bus,
50 							   message, -1,
51 							   &error);
52 	if (dbus_error_is_set (&error)) {
53 		printf ("send failed %s\n", error.message);
54 		dbus_error_free (&error);
55 		dbus_message_unref (message);
56 		return NULL;
57 	}
58 	if (reply == NULL) {
59 		dbus_message_unref (message);
60 		return NULL;
61 	}
62 
63 	dbus_message_iter_init (reply, &iter);
64 
65 	if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_ARRAY  ||
66 	    dbus_message_iter_get_element_type (&iter) != DBUS_TYPE_BYTE) {
67 		printf ("expecting an array of byte entries\n");
68 		dbus_message_unref (message);
69 		dbus_message_unref (reply);
70 		return NULL;
71 	}
72 	dbus_message_iter_recurse (&iter, &subiter);
73 
74 	count = 0;
75 	bufsize = 256;
76 	buf = (uchar_t *)malloc (bufsize);
77 
78 	while (dbus_message_iter_get_arg_type (&subiter) == DBUS_TYPE_BYTE) {
79 		if (count == bufsize) {
80 			bufsize += 256;
81 			buf = realloc (buf, bufsize);
82 			if (buf == NULL) {
83 				dbus_message_unref (message);
84 				dbus_message_unref (reply);
85 				return NULL;
86 			}
87 		}
88 
89 		dbus_message_iter_get_basic (&subiter, &value);
90 		buf[count++] = value;
91 		dbus_message_iter_next(&subiter);
92 	}
93 
94 	dbus_message_unref (message);
95 	dbus_message_unref (reply);
96 
97 	*data_size = count;
98 	if (count == 0) {
99 		free (buf);
100 		buf = NULL;
101 	}
102 
103 	return (adt_export_data_t *)buf;
104 }
105