agent: Handle 'Name' and/or 'SSID' fields on method call return
[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;
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                                 identity, passphrase,
196                                 passphrase_reply->user_data);
197         connman_service_unref(passphrase_reply->service);
198         dbus_message_unref(reply);
199         g_free(passphrase_reply);
200 }
201
202 static void request_input_append_alternates(DBusMessageIter *iter,
203                                                         void *user_data)
204 {
205         const char *str = user_data;
206         char **alternates, **alternative;
207
208         if (str == NULL)
209                 return;
210
211         alternates = g_strsplit(str, ",", 0);
212         if (alternates == NULL)
213                 return;
214
215         for (alternative = alternates; *alternative != NULL; alternative++)
216                 dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING,
217                                                                 alternative);
218
219         g_strfreev(alternates);
220 }
221
222 static void request_input_append_identity(DBusMessageIter *iter,
223                                                         void *user_data)
224 {
225         char *str = "string";
226
227         connman_dbus_dict_append_basic(iter, "Type",
228                                 DBUS_TYPE_STRING, &str);
229         str = "Mandatory";
230         connman_dbus_dict_append_basic(iter, "Requirement",
231                                 DBUS_TYPE_STRING, &str);
232 }
233
234 static void request_input_append_passphrase(DBusMessageIter *iter,
235                                                         void *user_data)
236 {
237         struct connman_service *service = user_data;
238         char *value;
239         const char *phase2;
240
241         switch (__connman_service_get_security(service)) {
242         case CONNMAN_SERVICE_SECURITY_WEP:
243                 value = "wep";
244                 break;
245         case CONNMAN_SERVICE_SECURITY_PSK:
246                 value = "psk";
247                 break;
248         case CONNMAN_SERVICE_SECURITY_8021X:
249                 phase2 = __connman_service_get_phase2(service);
250
251                 if (phase2 != NULL && (
252                                 g_str_has_suffix(phase2, "GTC") == TRUE ||
253                                 g_str_has_suffix(phase2, "OTP") == TRUE))
254                         value = "response";
255                 else
256                         value = "passphrase";
257
258                 break;
259         default:
260                 value = "string";
261                 break;
262         }
263         connman_dbus_dict_append_basic(iter, "Type",
264                                 DBUS_TYPE_STRING, &value);
265         value = "Mandatory";
266         connman_dbus_dict_append_basic(iter, "Requirement",
267                                 DBUS_TYPE_STRING, &value);
268
269         if (__connman_service_wps_enabled(service) == TRUE) {
270                 connman_dbus_dict_append_array(iter, "Alternates",
271                                         DBUS_TYPE_STRING,
272                                         request_input_append_alternates,
273                                         "WPS");
274         }
275 }
276
277 static void request_input_append_wps(DBusMessageIter *iter, void *user_data)
278 {
279         const char *str = "wpspin";
280
281         connman_dbus_dict_append_basic(iter, "Type",
282                                 DBUS_TYPE_STRING, &str);
283         str = "alternate";
284         connman_dbus_dict_append_basic(iter, "Requirement",
285                                 DBUS_TYPE_STRING, &str);
286 }
287
288 static void request_input_append_name(DBusMessageIter *iter, void *user_data)
289 {
290         const char *str = "string";
291
292         connman_dbus_dict_append_basic(iter, "Type",
293                                 DBUS_TYPE_STRING, &str);
294         str = "mandatory";
295         connman_dbus_dict_append_basic(iter, "Requirement",
296                                 DBUS_TYPE_STRING, &str);
297         connman_dbus_dict_append_array(iter, "Alternates",
298                                 DBUS_TYPE_STRING,
299                                 request_input_append_alternates,
300                                 "SSID");
301 }
302
303 static void request_input_append_ssid(DBusMessageIter *iter, void *user_data)
304 {
305         const char *str = "ssid";
306
307         connman_dbus_dict_append_basic(iter, "Type",
308                                 DBUS_TYPE_STRING, &str);
309         str = "alternate";
310         connman_dbus_dict_append_basic(iter, "Requirement",
311                                 DBUS_TYPE_STRING, &str);
312 }
313
314 static void request_input_append_password(DBusMessageIter *iter,
315                                                         void *user_data)
316 {
317         char *str = "passphrase";
318
319         connman_dbus_dict_append_basic(iter, "Type",
320                                 DBUS_TYPE_STRING, &str);
321         str = "Mandatory";
322         connman_dbus_dict_append_basic(iter, "Requirement",
323                                 DBUS_TYPE_STRING, &str);
324 }
325
326 static void request_input_login_reply(DBusPendingCall *call, void *user_data)
327 {
328         struct request_input_reply *username_password_reply = user_data;
329         char *username = NULL;
330         char *password = NULL;
331         char *key;
332         DBusMessageIter iter, dict;
333         DBusMessage *reply = dbus_pending_call_steal_reply(call);
334
335         if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR)
336                 goto done;
337
338         dbus_message_iter_init(reply, &iter);
339         dbus_message_iter_recurse(&iter, &dict);
340         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
341                 DBusMessageIter entry, value;
342
343                 dbus_message_iter_recurse(&dict, &entry);
344                 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
345                         break;
346
347                 dbus_message_iter_get_basic(&entry, &key);
348
349                 if (g_str_equal(key, "Username")) {
350                         dbus_message_iter_next(&entry);
351                         if (dbus_message_iter_get_arg_type(&entry)
352                                                         != DBUS_TYPE_VARIANT)
353                                 break;
354                         dbus_message_iter_recurse(&entry, &value);
355                         dbus_message_iter_get_basic(&value, &username);
356
357                 } else if (g_str_equal(key, "Password")) {
358                         dbus_message_iter_next(&entry);
359                         if (dbus_message_iter_get_arg_type(&entry) !=
360                                                         DBUS_TYPE_VARIANT)
361                                 break;
362                         dbus_message_iter_recurse(&entry, &value);
363                         dbus_message_iter_get_basic(&value, &password);
364                 }
365
366                 dbus_message_iter_next(&dict);
367         }
368
369 done:
370         username_password_reply->callback(username_password_reply->service,
371                                         TRUE,
372                                         username, password,
373                                         username_password_reply->user_data);
374         connman_service_unref(username_password_reply->service);
375         dbus_message_unref(reply);
376         g_free(username_password_reply);
377 }
378
379 int __connman_agent_request_passphrase_input(struct connman_service *service,
380                                 authentication_cb_t callback, void *user_data)
381 {
382         DBusMessage *message;
383         const char *path;
384         DBusMessageIter iter;
385         DBusMessageIter dict;
386         DBusPendingCall *call;
387         struct request_input_reply *passphrase_reply;
388
389         if (service == NULL || agent_path == NULL || callback == NULL)
390                 return -ESRCH;
391
392         message = dbus_message_new_method_call(agent_sender, agent_path,
393                                         CONNMAN_AGENT_INTERFACE,
394                                         "RequestInput");
395         if (message == NULL)
396                 return -ENOMEM;
397
398         dbus_message_iter_init_append(message, &iter);
399
400         path = __connman_service_get_path(service);
401         dbus_message_iter_append_basic(&iter,
402                                 DBUS_TYPE_OBJECT_PATH, &path);
403
404         connman_dbus_dict_open(&iter, &dict);
405
406         if (__connman_service_is_hidden(service)) {
407                 connman_dbus_dict_append_dict(&dict, "Name",
408                                         request_input_append_name, NULL);
409                 connman_dbus_dict_append_dict(&dict, "SSID",
410                                         request_input_append_ssid, NULL);
411         }
412
413         if (__connman_service_get_security(service) ==
414                         CONNMAN_SERVICE_SECURITY_8021X) {
415                 connman_dbus_dict_append_dict(&dict, "Identity",
416                                         request_input_append_identity, service);
417         }
418
419         connman_dbus_dict_append_dict(&dict, "Passphrase",
420                                 request_input_append_passphrase, service);
421
422         if (__connman_service_wps_enabled(service) == TRUE) {
423             connman_dbus_dict_append_dict(&dict, "WPS",
424                                 request_input_append_wps, NULL);
425         }
426
427         connman_dbus_dict_close(&iter, &dict);
428
429         passphrase_reply = g_try_new0(struct request_input_reply, 1);
430         if (passphrase_reply == NULL) {
431                 dbus_message_unref(message);
432                 return -ENOMEM;
433         }
434
435         if (dbus_connection_send_with_reply(connection, message,
436                                                 &call, -1) == FALSE) {
437                 dbus_message_unref(message);
438                 g_free(passphrase_reply);
439                 return -ESRCH;
440         }
441
442         if (call == NULL) {
443                 dbus_message_unref(message);
444                 g_free(passphrase_reply);
445                 return -ESRCH;
446         }
447
448         passphrase_reply->service = connman_service_ref(service);
449         passphrase_reply->callback = callback;
450         passphrase_reply->user_data = user_data;
451
452         dbus_pending_call_set_notify(call, request_input_passphrase_reply,
453                                 passphrase_reply, NULL);
454
455         dbus_message_unref(message);
456
457         return -EIO;
458 }
459
460 int __connman_agent_request_login_input(struct connman_service *service,
461                                 authentication_cb_t callback, void *user_data)
462 {
463         DBusMessage *message;
464         const char *path;
465         DBusMessageIter iter;
466         DBusMessageIter dict;
467         DBusPendingCall *call;
468         struct request_input_reply *username_password_reply;
469
470         if (service == NULL || agent_path == NULL || callback == NULL)
471                 return -ESRCH;
472
473         message = dbus_message_new_method_call(agent_sender, agent_path,
474                                         CONNMAN_AGENT_INTERFACE,
475                                         "RequestInput");
476         if (message == NULL)
477                 return -ENOMEM;
478
479         dbus_message_iter_init_append(message, &iter);
480
481         path = __connman_service_get_path(service);
482         dbus_message_iter_append_basic(&iter,
483                                 DBUS_TYPE_OBJECT_PATH, &path);
484
485         connman_dbus_dict_open(&iter, &dict);
486
487         connman_dbus_dict_append_dict(&dict, "Username",
488                                 request_input_append_identity, service);
489
490         connman_dbus_dict_append_dict(&dict, "Password",
491                                 request_input_append_password, service);
492
493         connman_dbus_dict_close(&iter, &dict);
494
495         username_password_reply = g_try_new0(struct request_input_reply, 1);
496         if (username_password_reply == NULL) {
497                 dbus_message_unref(message);
498                 return -ENOMEM;
499         }
500
501         if (dbus_connection_send_with_reply(connection, message,
502                                                         &call, -1) == FALSE) {
503                 dbus_message_unref(message);
504                 g_free(username_password_reply);
505                 return -ESRCH;
506         }
507
508         if (call == NULL) {
509                 dbus_message_unref(message);
510                 g_free(username_password_reply);
511                 return -ESRCH;
512         }
513
514         username_password_reply->service = connman_service_ref(service);
515         username_password_reply->callback = callback;
516         username_password_reply->user_data = user_data;
517
518         dbus_pending_call_set_notify(call, request_input_login_reply,
519                                                 username_password_reply, NULL);
520
521         dbus_message_unref(message);
522
523         return -EIO;
524 }
525
526 struct report_error_data {
527         struct connman_service *service;
528         report_error_cb_t callback;
529         void *user_data;
530 };
531
532 static void report_error_reply(DBusPendingCall *call, void *user_data)
533 {
534         struct report_error_data *report_error = user_data;
535         DBusMessage *reply = dbus_pending_call_steal_reply(call);
536         gboolean retry = FALSE;
537         const char *dbus_err;
538
539         if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR) {
540                 dbus_err = dbus_message_get_error_name(reply);
541                 if (dbus_err != NULL &&
542                         strcmp(dbus_err,
543                                 CONNMAN_AGENT_INTERFACE ".Error.Retry") == 0)
544                         retry = TRUE;
545         }
546
547         report_error->callback(report_error->service, retry,
548                         report_error->user_data);
549         connman_service_unref(report_error->service);
550         g_free(report_error);
551         dbus_message_unref(reply);
552 }
553
554 int __connman_agent_report_error(struct connman_service *service,
555                                 const char *error,
556                                 report_error_cb_t callback, void *user_data)
557 {
558         DBusMessage *message;
559         DBusMessageIter iter;
560         const char *path;
561         struct report_error_data *report_error;
562         DBusPendingCall *call;
563
564         if (service == NULL || agent_path == NULL || error == NULL ||
565                 callback == NULL)
566                 return -ESRCH;
567
568         message = dbus_message_new_method_call(agent_sender, agent_path,
569                                         CONNMAN_AGENT_INTERFACE,
570                                         "ReportError");
571         if (message == NULL)
572                 return -ENOMEM;
573
574         dbus_message_iter_init_append(message, &iter);
575
576         path = __connman_service_get_path(service);
577         dbus_message_iter_append_basic(&iter,
578                                 DBUS_TYPE_OBJECT_PATH, &path);
579         dbus_message_iter_append_basic(&iter,
580                                 DBUS_TYPE_STRING, &error);
581
582         report_error = g_try_new0(struct report_error_data, 1);
583         if (report_error == NULL) {
584                 dbus_message_unref(message);
585                 return -ENOMEM;
586         }
587
588         if (dbus_connection_send_with_reply(connection, message,
589                                                 &call, -1) == FALSE) {
590                 dbus_message_unref(message);
591                 g_free(report_error);
592                 return -ESRCH;
593         }
594
595         if (call == NULL) {
596                 dbus_message_unref(message);
597                 g_free(report_error);
598                 return -ESRCH;
599         }
600
601         report_error->service = connman_service_ref(service);
602         report_error->callback = callback;
603         report_error->user_data = user_data;
604         dbus_pending_call_set_notify(call, report_error_reply,
605                                 report_error, NULL);
606         dbus_message_unref(message);
607
608         return -EIO;
609 }
610
611 int __connman_agent_init(void)
612 {
613         DBG("");
614
615         connection = connman_dbus_get_connection();
616         if (connection == NULL)
617                 return -1;
618
619         return 0;
620 }
621
622 void __connman_agent_cleanup(void)
623 {
624         DBusMessage *message;
625
626         DBG("");
627
628         if (connection == NULL)
629                 return;
630
631         if (agent_watch > 0)
632                 g_dbus_remove_watch(connection, agent_watch);
633
634         if (agent_path == NULL)
635                 return;
636
637         message = dbus_message_new_method_call(agent_sender, agent_path,
638                                         CONNMAN_AGENT_INTERFACE, "Release");
639         if (message == NULL)
640                 return;
641
642         dbus_message_set_no_reply(message, TRUE);
643
644         g_dbus_send_message(connection, message);
645
646         agent_free();
647
648         dbus_connection_unref(connection);
649 }