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