dnsproxy: Only one copy of the relevant buffers will be made to a TCP request
[framework/connectivity/connman.git] / tools / polkit-test.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2012  Intel Corporation. All rights reserved.
6  *
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.
10  *
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.
15  *
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
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdio.h>
27 #include <errno.h>
28
29 #include <string.h>
30 #include <signal.h>
31
32 #include <dbus/dbus.h>
33
34 static volatile sig_atomic_t __io_terminate = 0;
35
36 static void sig_term(int sig)
37 {
38         __io_terminate = 1;
39 }
40
41 static void add_dict_with_string_value(DBusMessageIter *iter,
42                                         const char *key, const char *str)
43 {
44         DBusMessageIter dict, entry, value;
45
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,
51                                                                 NULL, &entry);
52
53         dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
54
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);
59
60         dbus_message_iter_close_container(&dict, &entry);
61         dbus_message_iter_close_container(iter, &dict);
62 }
63
64 static void add_empty_string_dict(DBusMessageIter *iter)
65 {
66         DBusMessageIter dict;
67
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);
72
73         dbus_message_iter_close_container(iter, &dict);
74 }
75
76 static void add_arguments(DBusConnection *conn, DBusMessageIter *iter)
77 {
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;
84
85         dbus_message_iter_open_container(iter, DBUS_TYPE_STRUCT,
86                                                         NULL, &subject);
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);
90
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);
95 }
96
97 static void print_arguments(DBusMessageIter *iter)
98 {
99         DBusMessageIter result;
100         dbus_bool_t authorized, challenge;
101
102         dbus_message_iter_recurse(iter, &result);
103
104         dbus_message_iter_get_basic(&result, &authorized);
105         dbus_message_iter_get_basic(&result, &challenge);
106
107         printf("Authorized %d (Challenge %d)\n", authorized, challenge);
108 }
109
110 #define AUTHORITY_DBUS  "org.freedesktop.PolicyKit1"
111 #define AUTHORITY_INTF  "org.freedesktop.PolicyKit1.Authority"
112 #define AUTHORITY_PATH  "/org/freedesktop/PolicyKit1/Authority"
113
114 static int check_authorization(DBusConnection *conn)
115 {
116         DBusMessage *msg, *reply;
117         DBusMessageIter iter;
118         DBusError err;
119
120         msg = dbus_message_new_method_call(AUTHORITY_DBUS, AUTHORITY_PATH,
121                                 AUTHORITY_INTF, "CheckAuthorization");
122         if (!msg) {
123                 fprintf(stderr, "Can't allocate new method call\n");
124                 return -ENOMEM;
125         }
126
127         dbus_message_iter_init_append(msg, &iter);
128         add_arguments(conn, &iter);
129
130         dbus_error_init(&err);
131
132         reply = dbus_connection_send_with_reply_and_block(conn, msg, -1, &err);
133
134         dbus_message_unref(msg);
135
136         if (!reply) {
137                 if (dbus_error_is_set(&err)) {
138                         fprintf(stderr, "%s\n", err.message);
139                         dbus_error_free(&err);
140                 } else
141                         fprintf(stderr, "Can't check authorization\n");
142                 return -EIO;
143         }
144
145         if (dbus_message_has_signature(reply, "(bba{ss})") == TRUE) {
146                 dbus_message_iter_init(reply, &iter);
147                 print_arguments(&iter);
148         }
149
150         dbus_message_unref(reply);
151
152         return 0;
153 }
154
155 int main(int argc, char *argv[])
156 {
157         DBusConnection *conn;
158         struct sigaction sa;
159
160         conn = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
161         if (!conn) {
162                 fprintf(stderr, "Can't get on system bus");
163                 return 1;
164         }
165
166         check_authorization(conn);
167
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);
173
174 #if 0
175         while (!__io_terminate) {
176                 if (dbus_connection_read_write_dispatch(conn, 500) == FALSE)
177                         break;
178         }
179 #endif
180
181         dbus_connection_unref(conn);
182
183         return 0;
184 }