5 * Copyright (C) 2007-2010 Intel Corporation. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
32 #include <dbus/dbus.h>
34 static volatile sig_atomic_t __io_terminate = 0;
36 static void sig_term(int sig)
41 static void add_dict_with_string_value(DBusMessageIter *iter,
42 const char *key, const char *str)
44 DBusMessageIter dict, entry, value;
46 dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY,
47 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
48 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
49 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
50 dbus_message_iter_open_container(&dict, DBUS_TYPE_DICT_ENTRY,
53 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
55 dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
56 DBUS_TYPE_STRING_AS_STRING, &value);
57 dbus_message_iter_append_basic(&value, DBUS_TYPE_STRING, &str);
58 dbus_message_iter_close_container(&entry, &value);
60 dbus_message_iter_close_container(&dict, &entry);
61 dbus_message_iter_close_container(iter, &dict);
64 static void add_empty_string_dict(DBusMessageIter *iter)
68 dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY,
69 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
70 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_STRING_AS_STRING
71 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
73 dbus_message_iter_close_container(iter, &dict);
76 static void add_arguments(DBusConnection *conn, DBusMessageIter *iter)
78 const char *busname = dbus_bus_get_unique_name(conn);
79 const char *kind = "system-bus-name";
80 const char *action = "org.freedesktop.policykit.exec";
81 const char *cancel = "";
82 dbus_uint32_t flags = 0x00000001;
83 DBusMessageIter subject;
85 dbus_message_iter_open_container(iter, DBUS_TYPE_STRUCT,
87 dbus_message_iter_append_basic(&subject, DBUS_TYPE_STRING, &kind);
88 add_dict_with_string_value(&subject, "name", busname);
89 dbus_message_iter_close_container(iter, &subject);
91 dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &action);
92 add_empty_string_dict(iter);
93 dbus_message_iter_append_basic(iter, DBUS_TYPE_UINT32, &flags);
94 dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &cancel);
97 static void print_arguments(DBusMessageIter *iter)
99 DBusMessageIter result;
100 dbus_bool_t authorized, challenge;
102 dbus_message_iter_recurse(iter, &result);
104 dbus_message_iter_get_basic(&result, &authorized);
105 dbus_message_iter_get_basic(&result, &challenge);
107 printf("Authorized %d (Challenge %d)\n", authorized, challenge);
110 #define AUTHORITY_DBUS "org.freedesktop.PolicyKit1"
111 #define AUTHORITY_INTF "org.freedesktop.PolicyKit1.Authority"
112 #define AUTHORITY_PATH "/org/freedesktop/PolicyKit1/Authority"
114 static int check_authorization(DBusConnection *conn)
116 DBusMessage *msg, *reply;
117 DBusMessageIter iter;
120 msg = dbus_message_new_method_call(AUTHORITY_DBUS, AUTHORITY_PATH,
121 AUTHORITY_INTF, "CheckAuthorization");
123 fprintf(stderr, "Can't allocate new method call\n");
127 dbus_message_iter_init_append(msg, &iter);
128 add_arguments(conn, &iter);
130 dbus_error_init(&err);
132 reply = dbus_connection_send_with_reply_and_block(conn, msg, -1, &err);
134 dbus_message_unref(msg);
137 if (dbus_error_is_set(&err)) {
138 fprintf(stderr, "%s\n", err.message);
139 dbus_error_free(&err);
141 fprintf(stderr, "Can't check authorization\n");
145 if (dbus_message_has_signature(reply, "(bba{ss})") == TRUE) {
146 dbus_message_iter_init(reply, &iter);
147 print_arguments(&iter);
150 dbus_message_unref(reply);
155 int main(int argc, char *argv[])
157 DBusConnection *conn;
160 conn = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
162 fprintf(stderr, "Can't get on system bus");
166 check_authorization(conn);
168 memset(&sa, 0, sizeof(sa));
169 sa.sa_flags = SA_NOCLDSTOP;
170 sa.sa_handler = sig_term;
171 sigaction(SIGTERM, &sa, NULL);
172 sigaction(SIGINT, &sa, NULL);
175 while (!__io_terminate) {
176 if (dbus_connection_read_write_dispatch(conn, 500) == FALSE)
181 dbus_connection_unref(conn);