xref: /titanic_51/usr/src/cmd/hal/utils/adt_data.c (revision 9f7c4232898c7267aead321833a9ff322daa257d)
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
13 
14 #include <stdio.h>
15 #include <errno.h>
16 #include <string.h>
17 #include <stdlib.h>
18 #include <glib.h>
19 #include <dbus/dbus-glib-lowlevel.h>
20 #include <dbus/dbus-glib.h>
21 #include "../hald/logger.h"
22 #include "adt_data.h"
23 
24 adt_export_data_t *
25 get_audit_export_data(DBusConnection *bus, const char *invoked_by_syscon_name, size_t *data_size)
26 {
27 	DBusMessage *message;
28 	DBusMessage *reply;
29 	DBusMessageIter iter, subiter;
30 	DBusError error;
31 	int count, bufsize;
32 	uchar_t *buf;
33 	uchar_t value;
34 
35 	message = dbus_message_new_method_call ("org.freedesktop.DBus",
36 						"/org/freedesktop/DBus",
37 						"org.freedesktop.DBus",
38 						"GetAuditSessionData");
39 	if (message == NULL) {
40 		printf ("cannot get GetAuditSessionData message\n");
41 		return NULL;
42 	}
43 
44 	if (!dbus_message_append_args(message, DBUS_TYPE_STRING, &invoked_by_syscon_name,
45 	    DBUS_TYPE_INVALID)) {
46 		dbus_message_unref(message);
47 		return NULL;
48 	}
49 
50 	dbus_error_init (&error);
51 	reply = dbus_connection_send_with_reply_and_block (bus,
52 							   message, -1,
53 							   &error);
54 	if (dbus_error_is_set (&error)) {
55 		printf ("send failed %s\n", error.message);
56 		dbus_error_free (&error);
57 		dbus_message_unref (message);
58 		return NULL;
59 	}
60 	if (reply == NULL) {
61 		dbus_message_unref (message);
62 		return NULL;
63 	}
64 
65 	dbus_message_iter_init (reply, &iter);
66 
67 	if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_ARRAY  ||
68 	    dbus_message_iter_get_element_type (&iter) != DBUS_TYPE_BYTE) {
69 		printf ("expecting an array of byte entries\n");
70 		dbus_message_unref (message);
71 		dbus_message_unref (reply);
72 		return NULL;
73 	}
74 	dbus_message_iter_recurse (&iter, &subiter);
75 
76 	count = 0;
77 	bufsize = 256;
78 	buf = (uchar_t *)malloc (bufsize);
79 
80 	while (dbus_message_iter_get_arg_type (&subiter) == DBUS_TYPE_BYTE) {
81 		if (count == bufsize) {
82 			bufsize += 256;
83 			buf = realloc (buf, bufsize);
84 			if (buf == NULL) {
85 				dbus_message_unref (message);
86 				dbus_message_unref (reply);
87 				return NULL;
88 			}
89 		}
90 
91 		dbus_message_iter_get_basic (&subiter, &value);
92 		buf[count++] = value;
93 		dbus_message_iter_next(&subiter);
94 	}
95 
96 	dbus_message_unref (message);
97 	dbus_message_unref (reply);
98 
99 	*data_size = count;
100 	if (count == 0) {
101 		free (buf);
102 		buf = NULL;
103 	}
104 
105 	return (adt_export_data_t *)buf;
106 }
107