Debian Codegraph @

src/agent.c

raw
1 /*
2 * ConnMan GTK GUI
3 *
4 * Copyright (C) 2015 Intel Corporation. All rights reserved.
5 * Author: Jaakko Hannikainen <[email protected]>
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 as published by
9 * the Free Software Foundation, either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include <gio/gio.h>
22 #include <glib/gi18n.h>
23 #include <gtk/gtk.h>
24 #include <string.h>
25 #include <unistd.h>
26
27 #include "config.h"
28 #include "agent.h"
29 #include "dialog.h"
30 #include "interfaces.h"
31 #include "main.h"
32 #include "style.h"
33 #include "openconnect_helper.h"
34
35 GDBusConnection *conn = NULL;
36
37 struct agent {
38 gint id;
39 const gchar *cancel;
40 } *normal_agent, *vpn_agent;
41
42 static void release(struct agent *agent, GDBusMethodInvocation *invocation)
43 {
44 g_dbus_method_invocation_return_value(invocation, NULL);
45 }
46
47 static void report_error(struct agent *agent, GDBusMethodInvocation *invocation,
48 GVariant *parameters)
49 {
50 g_dbus_method_invocation_return_dbus_error(invocation,
51 "net.connman.Agent.Error.Retry", "");
52 }
53
54 static void report_peer_error(struct agent *agent,
55 GDBusMethodInvocation *invocation,
56 GVariant *parameters)
57 {
58 g_dbus_method_invocation_return_value(invocation, NULL);
59 }
60
61 static void request_browser(struct agent *agent,
62 GDBusMethodInvocation *invocation,
63 GVariant *parameters)
64 {
65 g_dbus_method_invocation_return_dbus_error(invocation,
66 "net.connman.Agent.Error.Canceled",
67 "User canceled browser");
68 }
69
70 /* Generate dialog entries from Agent.RequestInput's parameters */
71 static GPtrArray *generate_entries(GVariant *args)
72 {
73 gchar *key;
74 GVariant *value, *parameters;
75 GVariantIter *iter;
76 GVariantDict *argdict;
77 GPtrArray *array;
78
79 array = g_ptr_array_new_full(1, (GDestroyNotify)free_token_element);
80 parameters = g_variant_get_child_value(args, 1);
81 argdict = g_variant_dict_new(parameters);
82 iter = g_variant_iter_new(parameters);
83 while(g_variant_iter_loop(iter, "{sv}", &key, &value)) {
84 struct token_element *elem;
85 const gchar *req, *previous, *pp = "PreviousPassphrase";
86 GVariantDict *prevdict, *dict;
87 GVariant *prevv;
88
89 dict = g_variant_dict_new(value);
90 g_variant_dict_lookup(dict, "Requirement", "&s", &req);
91
92 if(strcmp(req, "mandatory")) {
93 // TODO: parse non-mandatory fields
94 g_variant_dict_unref(dict);
95 continue;
96 }
97
98 if(strcmp(key, "Passphrase") ||
99 !g_variant_dict_contains(argdict, pp)) {
100 elem = token_new_entry(key, TRUE);
101
102 g_ptr_array_add(array, elem);
103 g_variant_dict_unref(dict);
104 continue;
105 }
106
107 prevv = g_variant_dict_lookup_value(argdict, pp, NULL);
108 prevdict = g_variant_dict_new(prevv);
109 g_variant_dict_lookup(prevdict, "Value", "&s", &previous);
110 elem = token_new_entry_full(key, TRUE, previous, NULL);
111 g_variant_dict_unref(prevdict);
112 g_variant_unref(prevv);
113
114 g_ptr_array_add(array, elem);
115 g_variant_dict_unref(dict);
116 }
117 g_variant_iter_free(iter);
118 g_variant_unref(parameters);
119 g_variant_dict_unref(argdict);
120
121 return array;
122 }
123
124 /* Convert array of entries to a dict */
125 static GVariantDict *generate_dict(GPtrArray *elements)
126 {
127 GVariantDict *dict;
128 int i;
129
130 dict = g_variant_dict_new(NULL);
131 for(i = 0; i < elements->len; i++) {
132 struct token_element *elem = elements->pdata[i];
133 if(elem->type > TOKEN_ELEMENT_TEXT)
134 g_variant_dict_insert(dict, elem->name, "s",
135 elem->value);
136 }
137 return dict;
138 }
139
140 struct request_input_params {
141 struct agent *agent;
142 GDBusMethodInvocation *invocation;
143 GVariant *parameters;
144 };
145
146 static gpointer request_input(gpointer data)
147 {
148 struct request_input_params *params = data;
149 struct agent *agent = params->agent;
150 GDBusMethodInvocation *invocation = params->invocation;
151 GVariant *parameters = params->parameters;;
152 GVariantDict *dict = NULL;
153 GVariant *ret, *ret_v;
154 GPtrArray *entries = NULL;
155 int success = 0;
156
157 if(!is_openconnect(parameters)) {
158 entries = generate_entries(parameters);
159 success = dialog_ask_tokens(_("Authentication required"),
160 entries);
161 if(success)
162 dict = generate_dict(entries);
163 }
164 else
165 dict = openconnect_handle(invocation, parameters);
166
167 if(!dict) {
168 g_dbus_method_invocation_return_dbus_error(invocation,
169 agent->cancel, "User canceled password dialog");
170 goto out;
171 }
172
173 ret = g_variant_dict_end(dict);
174 ret_v = g_variant_new("(@a{sv})", ret);
175 g_variant_ref_sink(ret_v);
176 g_dbus_method_invocation_return_value(invocation, ret_v);
177 g_variant_unref(ret_v);
178 g_variant_dict_unref(dict);
179 out:
180 if(entries)
181 g_ptr_array_free(entries, TRUE);
182 g_free(data);
183 return NULL;
184 }
185
186 static void request_input_async(struct agent *agent,
187 GDBusMethodInvocation *invocation,
188 GVariant *parameters)
189 {
190 struct request_input_params *params = g_malloc(sizeof(*params));
191 params->agent = agent;
192 params->invocation = invocation;
193 params->parameters = parameters;
194 g_thread_new("request_input", (GThreadFunc)request_input, params);
195 }
196
197 static void request_peer_authorization(struct agent *agent,
198 GDBusMethodInvocation *invocation,
199 GVariant *parameters)
200 {
201 g_dbus_method_invocation_return_dbus_error(invocation,
202 "net.connman.Agent.Error.Canceled",
203 "User canceled password dialog");
204 }
205
206 static void cancel(GDBusMethodInvocation *invocation)
207 {
208 g_dbus_method_invocation_return_value(invocation, NULL);
209 }
210
211 static void method_call(GDBusConnection *connection, const gchar *sender,
212 const gchar *object_path, const gchar *interface_name,
213 const gchar *method_name, GVariant *parameters,
214 GDBusMethodInvocation *invocation, gpointer user_data)
215 {
216 if(!strcmp(method_name, "Release"))
217 release(user_data, invocation);
218 else if(!strcmp(method_name, "ReportError"))
219 report_error(user_data, invocation, parameters);
220 else if(!strcmp(method_name, "ReportPeerError"))
221 report_peer_error(user_data, invocation, parameters);
222 else if(!strcmp(method_name, "RequestBrowser"))
223 request_browser(user_data, invocation, parameters);
224 else if(!strcmp(method_name, "RequestInput"))
225 request_input_async(user_data, invocation, parameters);
226 else if(!strcmp(method_name, "RequestPeerAuthorization"))
227 request_peer_authorization(user_data, invocation, parameters);
228 else
229 cancel(invocation);
230 }
231
232 static char *agent_path(void)
233 {
234 static char *path = NULL;
235 if(!path)
236 path = g_strdup_printf("/net/connman/gtk/agent%d", getpid());
237 return path;
238 }
239
240 static char *vpn_agent_path(void)
241 {
242 static char *path = NULL;
243 if(!path)
244 path = g_strdup_printf("/net/connman/gtk/vpn/agent%d",
245 getpid());
246 return path;
247 }
248
249 static const GDBusInterfaceVTable vtable = {
250 method_call,
251 NULL,
252 NULL
253 };
254
255 static struct agent *agent_create(GDBusConnection *connection,
256 GDBusProxy *manager,
257 const gchar *interface_name,
258 const gchar *agent_path, const gchar *path,
259 const gchar *cancel)
260 {
261 GError *error = NULL;
262 struct agent *agent;
263 GVariant *ret;
264 GDBusNodeInfo *info;
265 GDBusInterfaceInfo *interface;
266
267 info = g_dbus_node_info_new_for_xml(interface_name, &error);
268 if(error) {
269 g_critical("Failed to load agent interface: %s",
270 error->message);
271 g_error_free(error);
272 return NULL;
273 }
274
275 agent = g_malloc(sizeof(*agent));
276 interface = g_dbus_node_info_lookup_interface(info, agent_path),
277 agent->id = g_dbus_connection_register_object(connection, path,
278 interface, &vtable, agent,
279 NULL, &error);
280 conn = connection;
281 g_dbus_node_info_unref(info);
282 if(error) {
283 g_critical("Failed to register agent object: %s",
284 error->message);
285 g_error_free(error);
286 g_free(agent);
287 return NULL;
288 }
289
290 ret = g_dbus_proxy_call_sync(manager, "RegisterAgent",
291 g_variant_new("(o)", path),
292 G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
293 if(error) {
294 g_critical("Failed to register agent: %s",
295 error->message);
296 g_error_free(error);
297 g_free(agent);
298 return NULL;
299 }
300 g_variant_unref(ret);
301
302 agent->cancel = cancel;
303 return agent;
304 }
305
306
307 void register_agent(GDBusConnection *connection, GDBusProxy *manager)
308 {
309 normal_agent = agent_create(connection, manager, AGENT_INTERFACE,
310 AGENT_NAME, agent_path(),
311 "net.connman.Agent.Error.Canceled");
312 }
313
314 void register_vpn_agent(GDBusConnection *connection, GDBusProxy *vpn_manager)
315 {
316 vpn_agent = agent_create(connection, vpn_manager, VPN_AGENT_INTERFACE,
317 VPN_AGENT_NAME, vpn_agent_path(),
318 "net.connman.vpn.Agent.Error.Canceled");
319 }
320
321 void agent_release(void)
322 {
323 if(conn && normal_agent) {
324 g_dbus_connection_unregister_object(conn, normal_agent->id);
325 g_free(normal_agent);
326 }
327 normal_agent = NULL;
328 }
329
330 void vpn_agent_release(void)
331 {
332 if(conn && vpn_agent) {
333 g_dbus_connection_unregister_object(conn, vpn_agent->id);
334 g_free(vpn_agent);
335 }
336 vpn_agent = NULL;
337 }