agent: Add support for requesting browser action
[platform/upstream/connman.git] / src / agent.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2010  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 <errno.h>
27 #include <stdlib.h>
28 #include <string.h>
29
30 #include <gdbus.h>
31
32 #include "connman.h"
33
34 static DBusConnection *connection = NULL;
35 static guint agent_watch = 0;
36 static gchar *agent_path = NULL;
37 static gchar *agent_sender = NULL;
38
39 static void agent_free(void)
40 {
41         agent_watch = 0;
42
43         g_free(agent_sender);
44         agent_sender = NULL;
45
46         g_free(agent_path);
47         agent_path = NULL;
48 }
49
50 static void agent_disconnect(DBusConnection *connection, void *data)
51 {
52         DBG("data %p", data);
53
54         agent_free();
55 }
56
57 int __connman_agent_register(const char *sender, const char *path)
58 {
59         DBG("sender %s path %s", sender, path);
60
61         if (agent_path != NULL)
62                 return -EEXIST;
63
64         agent_sender = g_strdup(sender);
65         agent_path = g_strdup(path);
66
67         agent_watch = g_dbus_add_disconnect_watch(connection, sender,
68                                                 agent_disconnect, NULL, NULL);
69
70         return 0;
71 }
72
73 int __connman_agent_unregister(const char *sender, const char *path)
74 {
75         DBG("sender %s path %s", sender, path);
76
77         if (agent_path == NULL)
78                 return -ESRCH;
79
80         if (agent_watch > 0)
81                 g_dbus_remove_watch(connection, agent_watch);
82
83         agent_free();
84
85         return 0;
86 }
87
88 struct request_input_reply {
89         struct connman_service *service;
90         authentication_cb_t callback;
91         void *user_data;
92 };
93
94 static void request_input_passphrase_reply(DBusPendingCall *call, void *user_data)
95 {
96         struct request_input_reply *passphrase_reply = user_data;
97         connman_bool_t values_received = FALSE;
98         connman_bool_t wps = FALSE;
99         char *identity = NULL;
100         char *passphrase = NULL;
101         char *wpspin = NULL;
102         char *key;
103         char *name = NULL;
104         int name_len = 0;
105         DBusMessageIter iter, dict;
106         DBusMessage *reply = dbus_pending_call_steal_reply(call);
107
108         if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR)
109                 goto done;
110
111         values_received = TRUE;
112
113         dbus_message_iter_init(reply, &iter);
114         dbus_message_iter_recurse(&iter, &dict);
115         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
116                 DBusMessageIter entry, value;
117
118                 dbus_message_iter_recurse(&dict, &entry);
119                 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
120                         break;
121
122                 dbus_message_iter_get_basic(&entry, &key);
123
124                 if (g_str_equal(key, "Identity")) {
125                         dbus_message_iter_next(&entry);
126                         if (dbus_message_iter_get_arg_type(&entry)
127                                                         != DBUS_TYPE_VARIANT)
128                                 break;
129                         dbus_message_iter_recurse(&entry, &value);
130                         dbus_message_iter_get_basic(&value, &identity);
131
132                 } else if (g_str_equal(key, "Passphrase")) {
133                         dbus_message_iter_next(&entry);
134                         if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_VARIANT)
135                                 break;
136                         dbus_message_iter_recurse(&entry, &value);
137                         dbus_message_iter_get_basic(&value, &passphrase);
138
139                 } else if (g_str_equal(key, "WPS")) {
140                         wps = TRUE;
141
142                         dbus_message_iter_next(&entry);
143                         if (dbus_message_iter_get_arg_type(&entry)
144                                                         != DBUS_TYPE_VARIANT)
145                                 break;
146                         dbus_message_iter_recurse(&entry, &value);
147                         dbus_message_iter_get_basic(&value, &wpspin);
148                         break;
149                 } else if (g_str_equal(key, "Name")) {
150                         dbus_message_iter_next(&entry);
151                         if (dbus_message_iter_get_arg_type(&entry)
152                                                         != DBUS_TYPE_VARIANT)
153                                 break;
154                         dbus_message_iter_recurse(&entry, &value);
155                         dbus_message_iter_get_basic(&value, &name);
156                         name_len = strlen(name);
157                 } else if (g_str_equal(key, "SSID")) {
158                         dbus_message_iter_next(&entry);
159                         if (dbus_message_iter_get_arg_type(&entry)
160                                                         != DBUS_TYPE_VARIANT)
161                                 break;
162                         dbus_message_iter_recurse(&entry, &value);
163                         if (dbus_message_iter_get_arg_type(&value)
164                                                         != DBUS_TYPE_VARIANT)
165                                 break;
166                         if (dbus_message_iter_get_element_type(&value)
167                                                         != DBUS_TYPE_VARIANT)
168                                 break;
169                         dbus_message_iter_get_fixed_array(&value, &name,
170                                                         &name_len);
171                 }
172                 dbus_message_iter_next(&dict);
173         }
174
175         if (wps == TRUE) {
176                 struct connman_network *network;
177
178                 network = __connman_service_get_network(
179                                                 passphrase_reply->service);
180                 if (network == NULL)
181                         goto done;
182
183                 connman_network_set_bool(network, "WiFi.UseWPS", wps);
184
185                 if (wpspin != NULL && strlen(wpspin) > 0)
186                         connman_network_set_string(network,
187                                                 "WiFi.PinWPS", wpspin);
188                 else
189                         connman_network_set_string(network,
190                                                 "WiFi.PinWPS", NULL);
191         }
192
193 done:
194         passphrase_reply->callback(passphrase_reply->service, values_received,
195                                 name, name_len,
196                                 identity, passphrase,
197                                 passphrase_reply->user_data);
198         connman_service_unref(passphrase_reply->service);
199         dbus_message_unref(reply);
200         g_free(passphrase_reply);
201 }
202
203 static void request_input_append_alternates(DBusMessageIter *iter,
204                                                         void *user_data)
205 {
206         const char *str = user_data;
207         char **alternates, **alternative;
208
209         if (str == NULL)
210                 return;
211
212         alternates = g_strsplit(str, ",", 0);
213         if (alternates == NULL)
214                 return;
215
216         for (alternative = alternates; *alternative != NULL; alternative++)
217                 dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING,
218                                                                 alternative);
219
220         g_strfreev(alternates);
221 }
222
223 static void request_input_append_identity(DBusMessageIter *iter,
224                                                         void *user_data)
225 {
226         char *str = "string";
227
228         connman_dbus_dict_append_basic(iter, "Type",
229                                 DBUS_TYPE_STRING, &str);
230         str = "mandatory";
231         connman_dbus_dict_append_basic(iter, "Requirement",
232                                 DBUS_TYPE_STRING, &str);
233 }
234
235 static void request_input_append_passphrase(DBusMessageIter *iter,
236                                                         void *user_data)
237 {
238         struct connman_service *service = user_data;
239         char *value;
240         const char *phase2;
241
242         switch (__connman_service_get_security(service)) {
243         case CONNMAN_SERVICE_SECURITY_WEP:
244                 value = "wep";
245                 break;
246         case CONNMAN_SERVICE_SECURITY_PSK:
247                 value = "psk";
248                 break;
249         case CONNMAN_SERVICE_SECURITY_8021X:
250                 phase2 = __connman_service_get_phase2(service);
251
252                 if (phase2 != NULL && (
253                                 g_str_has_suffix(phase2, "GTC") == TRUE ||
254                                 g_str_has_suffix(phase2, "OTP") == TRUE))
255                         value = "response";
256                 else
257                         value = "passphrase";
258
259                 break;
260         default:
261                 value = "string";
262                 break;
263         }
264         connman_dbus_dict_append_basic(iter, "Type",
265                                 DBUS_TYPE_STRING, &value);
266         value = "mandatory";
267         connman_dbus_dict_append_basic(iter, "Requirement",
268                                 DBUS_TYPE_STRING, &value);
269
270         if (__connman_service_wps_enabled(service) == TRUE) {
271                 connman_dbus_dict_append_array(iter, "Alternates",
272                                         DBUS_TYPE_STRING,
273                                         request_input_append_alternates,
274                                         "WPS");
275         }
276 }
277
278 static void request_input_append_wps(DBusMessageIter *iter, void *user_data)
279 {
280         const char *str = "wpspin";
281
282         connman_dbus_dict_append_basic(iter, "Type",
283                                 DBUS_TYPE_STRING, &str);
284         str = "alternate";
285         connman_dbus_dict_append_basic(iter, "Requirement",
286                                 DBUS_TYPE_STRING, &str);
287 }
288
289 static void request_input_append_name(DBusMessageIter *iter, void *user_data)
290 {
291         const char *str = "string";
292
293         connman_dbus_dict_append_basic(iter, "Type",
294                                 DBUS_TYPE_STRING, &str);
295         str = "mandatory";
296         connman_dbus_dict_append_basic(iter, "Requirement",
297                                 DBUS_TYPE_STRING, &str);
298         connman_dbus_dict_append_array(iter, "Alternates",
299                                 DBUS_TYPE_STRING,
300                                 request_input_append_alternates,
301                                 "SSID");
302 }
303
304 static void request_input_append_ssid(DBusMessageIter *iter, void *user_data)
305 {
306         const char *str = "ssid";
307
308         connman_dbus_dict_append_basic(iter, "Type",
309                                 DBUS_TYPE_STRING, &str);
310         str = "alternate";
311         connman_dbus_dict_append_basic(iter, "Requirement",
312                                 DBUS_TYPE_STRING, &str);
313 }
314
315 static void request_input_append_password(DBusMessageIter *iter,
316                                                         void *user_data)
317 {
318         char *str = "passphrase";
319
320         connman_dbus_dict_append_basic(iter, "Type",
321                                 DBUS_TYPE_STRING, &str);
322         str = "mandatory";
323         connman_dbus_dict_append_basic(iter, "Requirement",
324                                 DBUS_TYPE_STRING, &str);
325 }
326
327 static void request_input_login_reply(DBusPendingCall *call, void *user_data)
328 {
329         struct request_input_reply *username_password_reply = user_data;
330         char *username = NULL;
331         char *password = NULL;
332         char *key;
333         DBusMessageIter iter, dict;
334         DBusMessage *reply = dbus_pending_call_steal_reply(call);
335
336         if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR)
337                 goto done;
338
339         dbus_message_iter_init(reply, &iter);
340         dbus_message_iter_recurse(&iter, &dict);
341         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
342                 DBusMessageIter entry, value;
343
344                 dbus_message_iter_recurse(&dict, &entry);
345                 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
346                         break;
347
348                 dbus_message_iter_get_basic(&entry, &key);
349
350                 if (g_str_equal(key, "Username")) {
351                         dbus_message_iter_next(&entry);
352                         if (dbus_message_iter_get_arg_type(&entry)
353                                                         != DBUS_TYPE_VARIANT)
354                                 break;
355                         dbus_message_iter_recurse(&entry, &value);
356                         dbus_message_iter_get_basic(&value, &username);
357
358                 } else if (g_str_equal(key, "Password")) {
359                         dbus_message_iter_next(&entry);
360                         if (dbus_message_iter_get_arg_type(&entry) !=
361                                                         DBUS_TYPE_VARIANT)
362                                 break;
363                         dbus_message_iter_recurse(&entry, &value);
364                         dbus_message_iter_get_basic(&value, &password);
365                 }
366
367                 dbus_message_iter_next(&dict);
368         }
369
370 done:
371         username_password_reply->callback(username_password_reply->service,
372                                         TRUE, NULL, 0,
373                                         username, password,
374                                         username_password_reply->user_data);
375         connman_service_unref(username_password_reply->service);
376         dbus_message_unref(reply);
377         g_free(username_password_reply);
378 }
379
380 int __connman_agent_request_passphrase_input(struct connman_service *service,
381                                 authentication_cb_t callback, void *user_data)
382 {
383         DBusMessage *message;
384         const char *path;
385         DBusMessageIter iter;
386         DBusMessageIter dict;
387         DBusPendingCall *call;
388         struct request_input_reply *passphrase_reply;
389
390         if (service == NULL || agent_path == NULL || callback == NULL)
391                 return -ESRCH;
392
393         message = dbus_message_new_method_call(agent_sender, agent_path,
394                                         CONNMAN_AGENT_INTERFACE,
395                                         "RequestInput");
396         if (message == NULL)
397                 return -ENOMEM;
398
399         dbus_message_iter_init_append(message, &iter);
400
401         path = __connman_service_get_path(service);
402         dbus_message_iter_append_basic(&iter,
403                                 DBUS_TYPE_OBJECT_PATH, &path);
404
405         connman_dbus_dict_open(&iter, &dict);
406
407         if (__connman_service_is_hidden(service)) {
408                 connman_dbus_dict_append_dict(&dict, "Name",
409                                         request_input_append_name, NULL);
410                 connman_dbus_dict_append_dict(&dict, "SSID",
411                                         request_input_append_ssid, NULL);
412         }
413
414         if (__connman_service_get_security(service) ==
415                         CONNMAN_SERVICE_SECURITY_8021X) {
416                 connman_dbus_dict_append_dict(&dict, "Identity",
417                                         request_input_append_identity, service);
418         }
419
420         connman_dbus_dict_append_dict(&dict, "Passphrase",
421                                 request_input_append_passphrase, service);
422
423         if (__connman_service_wps_enabled(service) == TRUE) {
424             connman_dbus_dict_append_dict(&dict, "WPS",
425                                 request_input_append_wps, NULL);
426         }
427
428         connman_dbus_dict_close(&iter, &dict);
429
430         passphrase_reply = g_try_new0(struct request_input_reply, 1);
431         if (passphrase_reply == NULL) {
432                 dbus_message_unref(message);
433                 return -ENOMEM;
434         }
435
436         if (dbus_connection_send_with_reply(connection, message,
437                                                 &call, -1) == FALSE) {
438                 dbus_message_unref(message);
439                 g_free(passphrase_reply);
440                 return -ESRCH;
441         }
442
443         if (call == NULL) {
444                 dbus_message_unref(message);
445                 g_free(passphrase_reply);
446                 return -ESRCH;
447         }
448
449         passphrase_reply->service = connman_service_ref(service);
450         passphrase_reply->callback = callback;
451         passphrase_reply->user_data = user_data;
452
453         dbus_pending_call_set_notify(call, request_input_passphrase_reply,
454                                 passphrase_reply, NULL);
455
456         dbus_message_unref(message);
457
458         return -EIO;
459 }
460
461 int __connman_agent_request_login_input(struct connman_service *service,
462                                 authentication_cb_t callback, void *user_data)
463 {
464         DBusMessage *message;
465         const char *path;
466         DBusMessageIter iter;
467         DBusMessageIter dict;
468         DBusPendingCall *call;
469         struct request_input_reply *username_password_reply;
470
471         if (service == NULL || agent_path == NULL || callback == NULL)
472                 return -ESRCH;
473
474         message = dbus_message_new_method_call(agent_sender, agent_path,
475                                         CONNMAN_AGENT_INTERFACE,
476                                         "RequestInput");
477         if (message == NULL)
478                 return -ENOMEM;
479
480         dbus_message_iter_init_append(message, &iter);
481
482         path = __connman_service_get_path(service);
483         dbus_message_iter_append_basic(&iter,
484                                 DBUS_TYPE_OBJECT_PATH, &path);
485
486         connman_dbus_dict_open(&iter, &dict);
487
488         connman_dbus_dict_append_dict(&dict, "Username",
489                                 request_input_append_identity, service);
490
491         connman_dbus_dict_append_dict(&dict, "Password",
492                                 request_input_append_password, service);
493
494         connman_dbus_dict_close(&iter, &dict);
495
496         username_password_reply = g_try_new0(struct request_input_reply, 1);
497         if (username_password_reply == NULL) {
498                 dbus_message_unref(message);
499                 return -ENOMEM;
500         }
501
502         if (dbus_connection_send_with_reply(connection, message,
503                                                         &call, -1) == FALSE) {
504                 dbus_message_unref(message);
505                 g_free(username_password_reply);
506                 return -ESRCH;
507         }
508
509         if (call == NULL) {
510                 dbus_message_unref(message);
511                 g_free(username_password_reply);
512                 return -ESRCH;
513         }
514
515         username_password_reply->service = connman_service_ref(service);
516         username_password_reply->callback = callback;
517         username_password_reply->user_data = user_data;
518
519         dbus_pending_call_set_notify(call, request_input_login_reply,
520                                                 username_password_reply, NULL);
521
522         dbus_message_unref(message);
523
524         return -EIO;
525 }
526
527 int __connman_agent_request_browser(struct connman_service *service,
528                                 browser_authentication_cb_t callback,
529                                 const char *url, void *user_data)
530 {
531         DBusPendingCall *call;
532         DBusMessage *message;
533         DBusMessageIter iter;
534         const char *path;
535
536         if (service == NULL || agent_path == NULL || callback == NULL)
537                 return -ESRCH;
538
539         if (url == NULL)
540                 url = "";
541
542         message = dbus_message_new_method_call(agent_sender, agent_path,
543                                         CONNMAN_AGENT_INTERFACE,
544                                         "RequestBrowser");
545         if (message == NULL)
546                 return -ENOMEM;
547
548         dbus_message_iter_init_append(message, &iter);
549
550         path = __connman_service_get_path(service);
551         dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH, &path);
552
553         dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &url);
554
555         if (dbus_connection_send_with_reply(connection, message,
556                                                         &call, -1) == FALSE) {
557                 dbus_message_unref(message);
558                 return -ESRCH;
559         }
560
561         if (call == NULL) {
562                 dbus_message_unref(message);
563                 return -ESRCH;
564         }
565
566         dbus_message_unref(message);
567
568         return -EIO;
569 }
570
571 struct report_error_data {
572         struct connman_service *service;
573         report_error_cb_t callback;
574         void *user_data;
575 };
576
577 static void report_error_reply(DBusPendingCall *call, void *user_data)
578 {
579         struct report_error_data *report_error = user_data;
580         DBusMessage *reply = dbus_pending_call_steal_reply(call);
581         gboolean retry = FALSE;
582         const char *dbus_err;
583
584         if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR) {
585                 dbus_err = dbus_message_get_error_name(reply);
586                 if (dbus_err != NULL &&
587                         strcmp(dbus_err,
588                                 CONNMAN_AGENT_INTERFACE ".Error.Retry") == 0)
589                         retry = TRUE;
590         }
591
592         report_error->callback(report_error->service, retry,
593                         report_error->user_data);
594         connman_service_unref(report_error->service);
595         g_free(report_error);
596         dbus_message_unref(reply);
597 }
598
599 int __connman_agent_report_error(struct connman_service *service,
600                                 const char *error,
601                                 report_error_cb_t callback, void *user_data)
602 {
603         DBusMessage *message;
604         DBusMessageIter iter;
605         const char *path;
606         struct report_error_data *report_error;
607         DBusPendingCall *call;
608
609         if (service == NULL || agent_path == NULL || error == NULL ||
610                 callback == NULL)
611                 return -ESRCH;
612
613         message = dbus_message_new_method_call(agent_sender, agent_path,
614                                         CONNMAN_AGENT_INTERFACE,
615                                         "ReportError");
616         if (message == NULL)
617                 return -ENOMEM;
618
619         dbus_message_iter_init_append(message, &iter);
620
621         path = __connman_service_get_path(service);
622         dbus_message_iter_append_basic(&iter,
623                                 DBUS_TYPE_OBJECT_PATH, &path);
624         dbus_message_iter_append_basic(&iter,
625                                 DBUS_TYPE_STRING, &error);
626
627         report_error = g_try_new0(struct report_error_data, 1);
628         if (report_error == NULL) {
629                 dbus_message_unref(message);
630                 return -ENOMEM;
631         }
632
633         if (dbus_connection_send_with_reply(connection, message,
634                                                 &call, -1) == FALSE) {
635                 dbus_message_unref(message);
636                 g_free(report_error);
637                 return -ESRCH;
638         }
639
640         if (call == NULL) {
641                 dbus_message_unref(message);
642                 g_free(report_error);
643                 return -ESRCH;
644         }
645
646         report_error->service = connman_service_ref(service);
647         report_error->callback = callback;
648         report_error->user_data = user_data;
649         dbus_pending_call_set_notify(call, report_error_reply,
650                                 report_error, NULL);
651         dbus_message_unref(message);
652
653         return -EIO;
654 }
655
656 int __connman_agent_init(void)
657 {
658         DBG("");
659
660         connection = connman_dbus_get_connection();
661         if (connection == NULL)
662                 return -1;
663
664         return 0;
665 }
666
667 void __connman_agent_cleanup(void)
668 {
669         DBusMessage *message;
670
671         DBG("");
672
673         if (connection == NULL)
674                 return;
675
676         if (agent_watch > 0)
677                 g_dbus_remove_watch(connection, agent_watch);
678
679         if (agent_path == NULL)
680                 return;
681
682         message = dbus_message_new_method_call(agent_sender, agent_path,
683                                         CONNMAN_AGENT_INTERFACE, "Release");
684         if (message == NULL)
685                 return;
686
687         dbus_message_set_no_reply(message, TRUE);
688
689         g_dbus_send_message(connection, message);
690
691         agent_free();
692
693         dbus_connection_unref(connection);
694 }