agent: Fix capital letter for mandatory requirement
[framework/connectivity/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 struct report_error_data {
528         struct connman_service *service;
529         report_error_cb_t callback;
530         void *user_data;
531 };
532
533 static void report_error_reply(DBusPendingCall *call, void *user_data)
534 {
535         struct report_error_data *report_error = user_data;
536         DBusMessage *reply = dbus_pending_call_steal_reply(call);
537         gboolean retry = FALSE;
538         const char *dbus_err;
539
540         if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR) {
541                 dbus_err = dbus_message_get_error_name(reply);
542                 if (dbus_err != NULL &&
543                         strcmp(dbus_err,
544                                 CONNMAN_AGENT_INTERFACE ".Error.Retry") == 0)
545                         retry = TRUE;
546         }
547
548         report_error->callback(report_error->service, retry,
549                         report_error->user_data);
550         connman_service_unref(report_error->service);
551         g_free(report_error);
552         dbus_message_unref(reply);
553 }
554
555 int __connman_agent_report_error(struct connman_service *service,
556                                 const char *error,
557                                 report_error_cb_t callback, void *user_data)
558 {
559         DBusMessage *message;
560         DBusMessageIter iter;
561         const char *path;
562         struct report_error_data *report_error;
563         DBusPendingCall *call;
564
565         if (service == NULL || agent_path == NULL || error == NULL ||
566                 callback == NULL)
567                 return -ESRCH;
568
569         message = dbus_message_new_method_call(agent_sender, agent_path,
570                                         CONNMAN_AGENT_INTERFACE,
571                                         "ReportError");
572         if (message == NULL)
573                 return -ENOMEM;
574
575         dbus_message_iter_init_append(message, &iter);
576
577         path = __connman_service_get_path(service);
578         dbus_message_iter_append_basic(&iter,
579                                 DBUS_TYPE_OBJECT_PATH, &path);
580         dbus_message_iter_append_basic(&iter,
581                                 DBUS_TYPE_STRING, &error);
582
583         report_error = g_try_new0(struct report_error_data, 1);
584         if (report_error == NULL) {
585                 dbus_message_unref(message);
586                 return -ENOMEM;
587         }
588
589         if (dbus_connection_send_with_reply(connection, message,
590                                                 &call, -1) == FALSE) {
591                 dbus_message_unref(message);
592                 g_free(report_error);
593                 return -ESRCH;
594         }
595
596         if (call == NULL) {
597                 dbus_message_unref(message);
598                 g_free(report_error);
599                 return -ESRCH;
600         }
601
602         report_error->service = connman_service_ref(service);
603         report_error->callback = callback;
604         report_error->user_data = user_data;
605         dbus_pending_call_set_notify(call, report_error_reply,
606                                 report_error, NULL);
607         dbus_message_unref(message);
608
609         return -EIO;
610 }
611
612 int __connman_agent_init(void)
613 {
614         DBG("");
615
616         connection = connman_dbus_get_connection();
617         if (connection == NULL)
618                 return -1;
619
620         return 0;
621 }
622
623 void __connman_agent_cleanup(void)
624 {
625         DBusMessage *message;
626
627         DBG("");
628
629         if (connection == NULL)
630                 return;
631
632         if (agent_watch > 0)
633                 g_dbus_remove_watch(connection, agent_watch);
634
635         if (agent_path == NULL)
636                 return;
637
638         message = dbus_message_new_method_call(agent_sender, agent_path,
639                                         CONNMAN_AGENT_INTERFACE, "Release");
640         if (message == NULL)
641                 return;
642
643         dbus_message_set_no_reply(message, TRUE);
644
645         g_dbus_send_message(connection, message);
646
647         agent_free();
648
649         dbus_connection_unref(connection);
650 }