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