5 * Copyright (C) 2004-2011 Marcel Holtmann <marcel@holtmann.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
30 #include <dbus/dbus.h>
34 int polkit_check_authorization(DBusConnection *conn,
35 const char *action, gboolean interaction,
36 void (*function) (dbus_bool_t authorized,
38 void *user_data, int timeout);
40 static void add_dict_with_string_value(DBusMessageIter *iter,
41 const char *key, const char *str)
43 DBusMessageIter dict, entry, value;
45 dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY,
46 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
47 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
48 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
49 dbus_message_iter_open_container(&dict, DBUS_TYPE_DICT_ENTRY,
52 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
54 dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
55 DBUS_TYPE_STRING_AS_STRING, &value);
56 dbus_message_iter_append_basic(&value, DBUS_TYPE_STRING, &str);
57 dbus_message_iter_close_container(&entry, &value);
59 dbus_message_iter_close_container(&dict, &entry);
60 dbus_message_iter_close_container(iter, &dict);
63 static void add_empty_string_dict(DBusMessageIter *iter)
67 dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY,
68 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
69 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_STRING_AS_STRING
70 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
72 dbus_message_iter_close_container(iter, &dict);
75 static void add_arguments(DBusConnection *conn, DBusMessageIter *iter,
76 const char *action, dbus_uint32_t flags)
78 const char *busname = dbus_bus_get_unique_name(conn);
79 const char *kind = "system-bus-name";
80 const char *cancel = "";
81 DBusMessageIter subject;
83 dbus_message_iter_open_container(iter, DBUS_TYPE_STRUCT,
85 dbus_message_iter_append_basic(&subject, DBUS_TYPE_STRING, &kind);
86 add_dict_with_string_value(&subject, "name", busname);
87 dbus_message_iter_close_container(iter, &subject);
89 dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &action);
90 add_empty_string_dict(iter);
91 dbus_message_iter_append_basic(iter, DBUS_TYPE_UINT32, &flags);
92 dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &cancel);
95 static dbus_bool_t parse_result(DBusMessageIter *iter)
97 DBusMessageIter result;
98 dbus_bool_t authorized, challenge;
100 dbus_message_iter_recurse(iter, &result);
102 dbus_message_iter_get_basic(&result, &authorized);
103 dbus_message_iter_get_basic(&result, &challenge);
108 struct authorization_data {
109 void (*function) (dbus_bool_t authorized, void *user_data);
113 static void authorization_reply(DBusPendingCall *call, void *user_data)
115 struct authorization_data *data = user_data;
117 DBusMessageIter iter;
118 dbus_bool_t authorized = FALSE;
120 reply = dbus_pending_call_steal_reply(call);
122 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR)
125 if (dbus_message_has_signature(reply, "(bba{ss})") == FALSE)
128 dbus_message_iter_init(reply, &iter);
130 authorized = parse_result(&iter);
133 if (data->function != NULL)
134 data->function(authorized, data->user_data);
136 dbus_message_unref(reply);
138 dbus_pending_call_unref(call);
141 #define AUTHORITY_DBUS "org.freedesktop.PolicyKit1"
142 #define AUTHORITY_INTF "org.freedesktop.PolicyKit1.Authority"
143 #define AUTHORITY_PATH "/org/freedesktop/PolicyKit1/Authority"
145 int polkit_check_authorization(DBusConnection *conn,
146 const char *action, gboolean interaction,
147 void (*function) (dbus_bool_t authorized,
149 void *user_data, int timeout)
151 struct authorization_data *data;
153 DBusMessageIter iter;
154 DBusPendingCall *call;
155 dbus_uint32_t flags = 0x00000000;
160 data = dbus_malloc0(sizeof(*data));
164 msg = dbus_message_new_method_call(AUTHORITY_DBUS, AUTHORITY_PATH,
165 AUTHORITY_INTF, "CheckAuthorization");
171 if (interaction == TRUE)
175 action = "org.freedesktop.policykit.exec";
177 dbus_message_iter_init_append(msg, &iter);
178 add_arguments(conn, &iter, action, flags);
180 if (dbus_connection_send_with_reply(conn, msg,
181 &call, timeout) == FALSE) {
182 dbus_message_unref(msg);
188 dbus_message_unref(msg);
193 data->function = function;
194 data->user_data = user_data;
196 dbus_pending_call_set_notify(call, authorization_reply,
199 dbus_message_unref(msg);