Debian Codegraph @

lib/agent.c

raw symbols SCIP
1 /*
2 *
3 * Connection Manager UI
4 *
5 * Copyright (C) 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 #include <gdbus/gdbus.h>
23
24 #include <connman-private.h>
25 #include <cui-dbus.h>
26
27 #include <string.h>
28
29 #define CONNMAN_AGENT_INTERFACE CONNMAN_DBUS_NAME ".Agent"
30 #define CONNMAN_AGENT_PATH "/net/connman/agent/connmanui"
31
32 struct connman_agent {
33 DBusConnection *dbus_cnx;
34
35 DBusMessage *pending_reply;
36
37 agent_error_cb_f error_cb;
38 agent_browser_cb_f browser_cb;
39 agent_input_cb_f input_cb;
40 agent_cancel_cb_f cancel_cb;
41 };
42
43 static struct connman_agent *agent_if;
44
45 static DBusMessage *agent_release_method(DBusConnection *dbus_cnx,
46 DBusMessage *msg, void *data)
47 {
48 if (agent_if == NULL)
49 goto reply;
50
51 g_dbus_unregister_interface(agent_if->dbus_cnx,
52 CONNMAN_AGENT_PATH, CONNMAN_AGENT_INTERFACE);
53
54 if (agent_if->pending_reply != NULL)
55 dbus_message_unref(agent_if->pending_reply);
56
57 dbus_connection_unref(agent_if->dbus_cnx);
58
59 g_free(agent_if);
60 agent_if = NULL;
61
62 reply:
63 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
64 }
65
66 static DBusMessage *agent_report_error_method(DBusConnection *dbus_cnx,
67 DBusMessage *msg, void *data)
68 {
69 DBusMessageIter arg;
70 const char *error;
71 const char *path;
72
73 if (agent_if == NULL || agent_if->error_cb == NULL)
74 goto error;
75
76 if (dbus_message_iter_init(msg, &arg) == FALSE)
77 goto error;
78
79 if (cui_dbus_get_basic(&arg, DBUS_TYPE_OBJECT_PATH, &path) != 0)
80 goto error;
81
82 dbus_message_iter_next(&arg);
83
84 if (cui_dbus_get_basic(&arg, DBUS_TYPE_STRING, &error) != 0)
85 goto error;
86
87 agent_if->pending_reply = dbus_message_ref(msg);
88 agent_if->error_cb(path, error);
89
90 return NULL;
91
92 error:
93 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
94 }
95
96 static DBusMessage *agent_request_browser_method(DBusConnection *dbus_cnx,
97 DBusMessage *msg, void *data)
98 {
99 DBusMessageIter arg;
100 const char *path;
101 const char *url;
102
103 if (agent_if == NULL || agent_if->browser_cb == NULL)
104 goto error;
105
106 if (dbus_message_iter_init(msg, &arg) == FALSE)
107 goto error;
108
109 if (cui_dbus_get_basic(&arg, DBUS_TYPE_OBJECT_PATH, &path) != 0)
110 goto error;
111
112 dbus_message_iter_next(&arg);
113
114 if (cui_dbus_get_basic(&arg, DBUS_TYPE_STRING, &url) != 0)
115 goto error;
116
117 agent_if->pending_reply = dbus_message_ref(msg);
118 agent_if->browser_cb(path, url);
119
120 return NULL;
121
122 error:
123 return g_dbus_create_error(msg, CONNMAN_ERROR ".Canceled", NULL);
124 }
125
126 struct agent_input_data {
127 gboolean hidden;
128 gboolean identity;
129 gboolean passphrase;
130 gboolean wpspin;
131 gboolean login;
132 const char *previous_passphrase;
133 const char *previous_wpspin;
134 };
135
136 static bool parse_input_request(DBusMessageIter *arg, void *user_data)
137 {
138 struct agent_input_data *data = user_data;
139 const char *name;
140
141 if (cui_dbus_get_basic(arg, DBUS_TYPE_STRING, &name) != 0)
142 return FALSE;
143
144 dbus_message_iter_next(arg);
145
146 if (g_strcmp0(name, "Passphrase") == 0)
147 data->passphrase = TRUE;
148 else if (g_strcmp0(name, "WPS") == 0)
149 data->wpspin = TRUE;
150 else if (g_strcmp0(name, "Name") == 0)
151 data->hidden = TRUE;
152 else if (g_strcmp0(name, "Identity") == 0)
153 data->identity = TRUE;
154 else if (g_strcmp0(name, "Username") == 0)
155 data->login = TRUE;
156 else if (g_strcmp0(name, "PreviousPassphrase") == 0) {
157 DBusMessageIter dict;
158 const char **value;
159 const char *type;
160
161 dbus_message_iter_recurse(arg, &dict);
162
163 cui_dbus_get_dict_entry_basic(&dict,
164 "Type", DBUS_TYPE_STRING, &type);
165
166 if (g_strcmp0(type, "psk") == 0)
167 value = &data->previous_passphrase;
168 else if (g_strcmp0(type, "wpspin") == 0)
169 value = &data->previous_wpspin;
170
171 cui_dbus_get_dict_entry_basic(&dict,
172 "Value", DBUS_TYPE_STRING, &value);
173 }
174
175 return FALSE;
176 }
177
178 static DBusMessage *agent_request_input_method(DBusConnection *dbus_cnx,
179 DBusMessage *msg, void *data)
180 {
181 struct agent_input_data arg_data;
182 DBusMessageIter arg;
183 const char *path;
184
185 if (agent_if == NULL || agent_if->input_cb == NULL)
186 goto error;
187
188 memset(&arg_data, 0, sizeof(struct agent_input_data));
189
190 if (dbus_message_iter_init(msg, &arg) == FALSE)
191 goto error;
192
193 if (cui_dbus_get_basic(&arg, DBUS_TYPE_OBJECT_PATH, &path) != 0)
194 goto error;
195
196 dbus_message_iter_next(&arg);
197
198 cui_dbus_foreach_dict_entry(&arg, parse_input_request, &arg_data);
199
200 if (arg_data.hidden == FALSE &&
201 arg_data.identity == FALSE &&
202 arg_data.passphrase == FALSE &&
203 arg_data.login == FALSE)
204 goto error;
205
206 agent_if->pending_reply = dbus_message_ref(msg);
207 agent_if->input_cb(path, arg_data.hidden, arg_data.identity,
208 arg_data.passphrase, arg_data.previous_passphrase,
209 arg_data.wpspin, arg_data.previous_wpspin,
210 arg_data.login);
211
212 return NULL;
213
214 error:
215 return g_dbus_create_error(msg, CONNMAN_ERROR ".Canceled", NULL);
216 }
217
218 static DBusMessage *agent_cancel_method(DBusConnection *dbus_cnx,
219 DBusMessage *msg, void *data)
220 {
221 if (agent_if == NULL)
222 goto reply;
223
224 if (agent_if->pending_reply != NULL) {
225 dbus_message_unref(agent_if->pending_reply);
226 agent_if->pending_reply = NULL;
227 }
228
229 if (agent_if->cancel_cb != NULL)
230 agent_if->cancel_cb();
231
232 reply:
233 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
234 }
235
236 static GDBusMethodTable agent_methods[] = {
237 { GDBUS_METHOD("Release", NULL, NULL, agent_release_method) },
238 { GDBUS_ASYNC_METHOD("ReportError",
239 GDBUS_ARGS({"service", "o"}, {"error", "s"}),
240 NULL, agent_report_error_method) },
241 { GDBUS_ASYNC_METHOD("RequestBrowser",
242 GDBUS_ARGS({"service", "o"}, {"url", "s"}),
243 NULL, agent_request_browser_method) },
244 { GDBUS_ASYNC_METHOD("RequestInput",
245 GDBUS_ARGS({"service", "o"}, {"fields", "a{sv}"}),
246 GDBUS_ARGS({"inputs", "a{sv}"}),
247 agent_request_input_method) },
248 { GDBUS_METHOD("Cancel", NULL, NULL, agent_cancel_method) },
249 { NULL },
250 };
251
252 static inline void send_agent_reply(DBusMessage *reply)
253 {
254 if (reply == NULL)
255 goto error;
256
257 g_dbus_send_message(agent_if->dbus_cnx, reply);
258
259 error:
260 dbus_message_unref(agent_if->pending_reply);
261 agent_if->pending_reply = NULL;
262 }
263
264 static void agent_reply_error(const char *error, const char *msg)
265 {
266 DBusMessage *reply;
267
268 if (agent_if == NULL || agent_if->pending_reply == NULL)
269 return;
270
271 reply = g_dbus_create_error(agent_if->pending_reply, error, "%s", msg);
272
273 send_agent_reply(reply);
274 }
275
276 int connman_agent_init(void)
277 {
278 if (connman == NULL)
279 return -EINVAL;
280
281 if (agent_if != NULL)
282 return 0;
283
284 agent_if = g_try_malloc0(sizeof(struct connman_agent));
285 if (agent_if == NULL)
286 return -ENOMEM;
287
288 agent_if->dbus_cnx = dbus_connection_ref(connman->dbus_cnx);
289
290 g_dbus_register_interface(agent_if->dbus_cnx,
291 CONNMAN_AGENT_PATH, CONNMAN_AGENT_INTERFACE,
292 agent_methods, NULL, NULL, NULL, NULL);
293
294 connman_manager_register_agent(CONNMAN_AGENT_PATH);
295
296 return 0;
297 }
298
299 void connman_agent_finalize(void)
300 {
301 if (agent_if == NULL)
302 return;
303
304 connman_manager_unregister_agent(CONNMAN_AGENT_PATH);
305
306 g_dbus_unregister_interface(agent_if->dbus_cnx,
307 CONNMAN_AGENT_PATH, CONNMAN_AGENT_INTERFACE);
308
309 if (agent_if->pending_reply != NULL)
310 dbus_message_unref(agent_if->pending_reply);
311
312 dbus_connection_unref(agent_if->dbus_cnx);
313
314 g_free(agent_if);
315 agent_if = NULL;
316 }
317
318 int connman_agent_set_error_cb(agent_error_cb_f error_cb)
319 {
320 if (agent_if == NULL)
321 return -EINVAL;
322
323 agent_if->error_cb = error_cb;
324
325 return 0;
326 }
327
328 int connman_agent_set_browser_cb(agent_browser_cb_f browser_cb)
329 {
330 if (agent_if == NULL)
331 return -EINVAL;
332
333 agent_if->browser_cb = browser_cb;
334
335 return 0;
336 }
337
338 int connman_agent_set_input_cb(agent_input_cb_f input_cb)
339 {
340 if (agent_if == NULL)
341 return -EINVAL;
342
343 agent_if->input_cb = input_cb;
344
345 return 0;
346 }
347
348 int connman_agent_set_cancel_cb(agent_cancel_cb_f cancel_cb)
349 {
350 if (agent_if == NULL)
351 return -EINVAL;
352
353 agent_if->cancel_cb = cancel_cb;
354
355 return 0;
356 }
357
358 void connman_agent_reply_retry(void)
359 {
360 agent_reply_error(CONNMAN_AGENT_INTERFACE ".Error.Retry", "Retry");
361 }
362
363 void connman_agent_reply_canceled(void)
364 {
365 agent_reply_error(CONNMAN_AGENT_INTERFACE ".Error.Canceled",
366 "Canceled");
367 }
368
369 void connman_agent_reply_launch_browser(void)
370 {
371 agent_reply_error(CONNMAN_AGENT_INTERFACE ".Error.LaunchBrowser",
372 "Launch the Browser");
373 }
374
375 int connman_agent_reply_identity(const char *identity, const char *passphrase)
376 {
377 DBusMessageIter arg, dict;
378 DBusMessage *reply;
379
380 if (agent_if == NULL || agent_if->pending_reply == NULL)
381 return 0;
382
383 if (identity == NULL || passphrase == NULL)
384 return -EINVAL;
385
386 reply = dbus_message_new_method_return(agent_if->pending_reply);
387 if (reply == NULL)
388 return -ENOMEM;
389
390 dbus_message_iter_init_append(reply, &arg);
391
392 if (cui_dbus_open_dict(&arg, &dict) == FALSE)
393 goto error;
394
395 cui_dbus_append_dict_entry_basic(&dict, "Identity",
396 DBUS_TYPE_STRING, &identity);
397 cui_dbus_append_dict_entry_basic(&dict, "Passphrase",
398 DBUS_TYPE_STRING, &passphrase);
399
400 dbus_message_iter_close_container(&dict, &arg);
401
402 send_agent_reply(reply);
403
404 return 0;
405
406 error:
407 dbus_message_unref(reply);
408
409 dbus_message_unref(agent_if->pending_reply);
410 agent_if->pending_reply = NULL;
411
412 return -ENOMEM;
413 }
414
415 int connman_agent_reply_passphrase(const char *hidden_name,
416 const char *passphrase, gboolean wps, const char *wpspin)
417 {
418 DBusMessageIter arg, dict;
419 DBusMessage *reply;
420
421 if (agent_if == NULL || agent_if->pending_reply == NULL)
422 return 0;
423
424 if (passphrase == NULL && hidden_name == NULL && wps == FALSE)
425 return -EINVAL;
426
427 reply = dbus_message_new_method_return(agent_if->pending_reply);
428 if (reply == NULL)
429 return -ENOMEM;
430
431 dbus_message_iter_init_append(reply, &arg);
432
433 if (cui_dbus_open_dict(&arg, &dict) == FALSE)
434 goto error;
435
436 if (hidden_name != NULL)
437 cui_dbus_append_dict_entry_basic(&dict, "Name",
438 DBUS_TYPE_STRING, &hidden_name);
439
440 if (passphrase != NULL)
441 cui_dbus_append_dict_entry_basic(&dict, "Passphrase",
442 DBUS_TYPE_STRING, &passphrase);
443 else if (wps == TRUE) {
444 if (wpspin == NULL)
445 wpspin = "";
446
447 cui_dbus_append_dict_entry_basic(&dict, "WPS",
448 DBUS_TYPE_STRING, &wpspin);
449 }
450
451 dbus_message_iter_close_container(&arg, &dict);
452
453 send_agent_reply(reply);
454
455 return 0;
456
457 error:
458 dbus_message_unref(reply);
459
460 dbus_message_unref(agent_if->pending_reply);
461 agent_if->pending_reply = NULL;
462
463 return -ENOMEM;
464 }
465
466 int connman_agent_reply_login(const char *username, const char *password)
467 {
468 DBusMessageIter arg, dict;
469 DBusMessage *reply;
470
471 if (agent_if == NULL || agent_if->pending_reply == NULL)
472 return 0;
473
474 if (username == NULL && password == NULL)
475 return -EINVAL;
476
477 reply = dbus_message_new_method_return(agent_if->pending_reply);
478 if (reply == NULL)
479 return -ENOMEM;
480
481 dbus_message_iter_init_append(reply, &arg);
482
483 if (cui_dbus_open_dict(&arg, &dict) == FALSE)
484 goto error;
485
486 cui_dbus_append_dict_entry_basic(&dict, "Username",
487 DBUS_TYPE_STRING, &username);
488 cui_dbus_append_dict_entry_basic(&dict, "Password",
489 DBUS_TYPE_STRING, &password);
490
491 dbus_message_iter_close_container(&dict, &arg);
492
493 send_agent_reply(reply);
494
495 return 0;
496
497 error:
498 dbus_message_unref(reply);
499
500 dbus_message_unref(agent_if->pending_reply);
501 agent_if->pending_reply = NULL;
502
503 return -ENOMEM;
504 }
505