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