5 * Copyright (C) 2012 Intel Corporation. All rights reserved.
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.
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.
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
31 #include <connman/agent.h>
32 #include <connman/setting.h>
33 #include <connman/service.h>
37 static connman_bool_t check_reply_has_dict(DBusMessage *reply)
39 const char *signature = DBUS_TYPE_ARRAY_AS_STRING
40 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
41 DBUS_TYPE_STRING_AS_STRING
42 DBUS_TYPE_VARIANT_AS_STRING
43 DBUS_DICT_ENTRY_END_CHAR_AS_STRING;
45 if (dbus_message_has_signature(reply, signature) == TRUE)
48 connman_warn("Reply %s to %s from %s has wrong signature %s",
50 dbus_message_get_interface(reply),
51 dbus_message_get_sender(reply),
52 dbus_message_get_signature(reply));
57 struct request_input_reply {
58 struct connman_service *service;
59 authentication_cb_t callback;
63 static void request_input_passphrase_reply(DBusMessage *reply, void *user_data)
65 struct request_input_reply *passphrase_reply = user_data;
66 connman_bool_t values_received = FALSE;
67 connman_bool_t wps = FALSE;
68 const char *error = NULL;
69 char *identity = NULL;
70 char *passphrase = NULL;
75 DBusMessageIter iter, dict;
77 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR) {
78 error = dbus_message_get_error_name(reply);
82 if (check_reply_has_dict(reply) == FALSE)
85 values_received = TRUE;
87 dbus_message_iter_init(reply, &iter);
88 dbus_message_iter_recurse(&iter, &dict);
89 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
90 DBusMessageIter entry, value;
92 dbus_message_iter_recurse(&dict, &entry);
93 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
96 dbus_message_iter_get_basic(&entry, &key);
98 if (g_str_equal(key, "Identity")) {
99 dbus_message_iter_next(&entry);
100 if (dbus_message_iter_get_arg_type(&entry)
101 != DBUS_TYPE_VARIANT)
103 dbus_message_iter_recurse(&entry, &value);
104 dbus_message_iter_get_basic(&value, &identity);
106 } else if (g_str_equal(key, "Passphrase")) {
107 dbus_message_iter_next(&entry);
108 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_VARIANT)
110 dbus_message_iter_recurse(&entry, &value);
111 dbus_message_iter_get_basic(&value, &passphrase);
113 } else if (g_str_equal(key, "WPS")) {
116 dbus_message_iter_next(&entry);
117 if (dbus_message_iter_get_arg_type(&entry)
118 != DBUS_TYPE_VARIANT)
120 dbus_message_iter_recurse(&entry, &value);
121 dbus_message_iter_get_basic(&value, &wpspin);
123 } else if (g_str_equal(key, "Name")) {
124 dbus_message_iter_next(&entry);
125 if (dbus_message_iter_get_arg_type(&entry)
126 != DBUS_TYPE_VARIANT)
128 dbus_message_iter_recurse(&entry, &value);
129 dbus_message_iter_get_basic(&value, &name);
130 name_len = strlen(name);
131 } else if (g_str_equal(key, "SSID")) {
132 dbus_message_iter_next(&entry);
133 if (dbus_message_iter_get_arg_type(&entry)
134 != DBUS_TYPE_VARIANT)
136 dbus_message_iter_recurse(&entry, &value);
137 if (dbus_message_iter_get_arg_type(&value)
138 != DBUS_TYPE_VARIANT)
140 if (dbus_message_iter_get_element_type(&value)
141 != DBUS_TYPE_VARIANT)
143 dbus_message_iter_get_fixed_array(&value, &name,
146 dbus_message_iter_next(&dict);
150 passphrase_reply->callback(passphrase_reply->service, values_received,
152 identity, passphrase,
154 passphrase_reply->user_data);
155 g_free(passphrase_reply);
158 static void request_input_append_alternates(DBusMessageIter *iter,
161 const char *str = user_data;
162 char **alternates, **alternative;
167 alternates = g_strsplit(str, ",", 0);
168 if (alternates == NULL)
171 for (alternative = alternates; *alternative != NULL; alternative++)
172 dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING,
175 g_strfreev(alternates);
178 static void request_input_append_identity(DBusMessageIter *iter,
181 char *str = "string";
183 connman_dbus_dict_append_basic(iter, "Type",
184 DBUS_TYPE_STRING, &str);
186 connman_dbus_dict_append_basic(iter, "Requirement",
187 DBUS_TYPE_STRING, &str);
190 static void request_input_append_passphrase(DBusMessageIter *iter,
193 struct connman_service *service = user_data;
197 switch (__connman_service_get_security(service)) {
198 case CONNMAN_SERVICE_SECURITY_WEP:
201 case CONNMAN_SERVICE_SECURITY_PSK:
204 case CONNMAN_SERVICE_SECURITY_8021X:
205 phase2 = __connman_service_get_phase2(service);
207 if (phase2 != NULL && (
208 g_str_has_suffix(phase2, "GTC") == TRUE ||
209 g_str_has_suffix(phase2, "OTP") == TRUE))
212 value = "passphrase";
219 connman_dbus_dict_append_basic(iter, "Type",
220 DBUS_TYPE_STRING, &value);
222 connman_dbus_dict_append_basic(iter, "Requirement",
223 DBUS_TYPE_STRING, &value);
225 if (__connman_service_wps_enabled(service) == TRUE) {
226 connman_dbus_dict_append_array(iter, "Alternates",
228 request_input_append_alternates,
233 static void request_input_append_wps(DBusMessageIter *iter, void *user_data)
235 const char *str = "wpspin";
237 connman_dbus_dict_append_basic(iter, "Type",
238 DBUS_TYPE_STRING, &str);
240 connman_dbus_dict_append_basic(iter, "Requirement",
241 DBUS_TYPE_STRING, &str);
244 static void request_input_append_name(DBusMessageIter *iter, void *user_data)
246 const char *str = "string";
248 connman_dbus_dict_append_basic(iter, "Type",
249 DBUS_TYPE_STRING, &str);
251 connman_dbus_dict_append_basic(iter, "Requirement",
252 DBUS_TYPE_STRING, &str);
253 connman_dbus_dict_append_array(iter, "Alternates",
255 request_input_append_alternates,
259 static void request_input_append_ssid(DBusMessageIter *iter, void *user_data)
261 const char *str = "ssid";
263 connman_dbus_dict_append_basic(iter, "Type",
264 DBUS_TYPE_STRING, &str);
266 connman_dbus_dict_append_basic(iter, "Requirement",
267 DBUS_TYPE_STRING, &str);
270 static void request_input_append_password(DBusMessageIter *iter,
273 char *str = "passphrase";
275 connman_dbus_dict_append_basic(iter, "Type",
276 DBUS_TYPE_STRING, &str);
278 connman_dbus_dict_append_basic(iter, "Requirement",
279 DBUS_TYPE_STRING, &str);
282 struct previous_passphrase_data {
283 const char *passphrase;
287 static void request_input_append_previouspassphrase(DBusMessageIter *iter,
290 struct previous_passphrase_data *data = user_data;
291 const char *requirement = "informational";
293 connman_dbus_dict_append_basic(iter, "Type",
294 DBUS_TYPE_STRING, &data->type);
296 connman_dbus_dict_append_basic(iter, "Requirement",
297 DBUS_TYPE_STRING, &requirement);
299 connman_dbus_dict_append_basic(iter, "Value",
300 DBUS_TYPE_STRING, &data->passphrase);
303 static void previous_passphrase_handler(DBusMessageIter *iter,
304 struct connman_service *service)
306 enum connman_service_security security;
307 struct previous_passphrase_data data;
308 struct connman_network *network;
310 network = __connman_service_get_network(service);
311 data.passphrase = connman_network_get_string(network, "WiFi.PinWPS");
313 if (connman_network_get_bool(network, "WiFi.UseWPS") == TRUE &&
314 data.passphrase != NULL) {
315 data.type = "wpspin";
317 data.passphrase = __connman_service_get_passphrase(service);
318 if (data.passphrase == NULL)
321 security = __connman_service_get_security(service);
323 case CONNMAN_SERVICE_SECURITY_WEP:
326 case CONNMAN_SERVICE_SECURITY_PSK:
330 * This should never happen: no passphrase is set if security
331 * is not one of the above. */
337 connman_dbus_dict_append_dict(iter, "PreviousPassphrase",
338 request_input_append_previouspassphrase, &data);
341 static void request_input_login_reply(DBusMessage *reply, void *user_data)
343 struct request_input_reply *username_password_reply = user_data;
344 const char *error = NULL;
345 connman_bool_t values_received = FALSE;
346 char *username = NULL;
347 char *password = NULL;
349 DBusMessageIter iter, dict;
351 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR) {
352 error = dbus_message_get_error_name(reply);
356 if (check_reply_has_dict(reply) == FALSE)
359 values_received = TRUE;
361 dbus_message_iter_init(reply, &iter);
362 dbus_message_iter_recurse(&iter, &dict);
363 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
364 DBusMessageIter entry, value;
366 dbus_message_iter_recurse(&dict, &entry);
367 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
370 dbus_message_iter_get_basic(&entry, &key);
372 if (g_str_equal(key, "Username")) {
373 dbus_message_iter_next(&entry);
374 if (dbus_message_iter_get_arg_type(&entry)
375 != DBUS_TYPE_VARIANT)
377 dbus_message_iter_recurse(&entry, &value);
378 dbus_message_iter_get_basic(&value, &username);
380 } else if (g_str_equal(key, "Password")) {
381 dbus_message_iter_next(&entry);
382 if (dbus_message_iter_get_arg_type(&entry) !=
385 dbus_message_iter_recurse(&entry, &value);
386 dbus_message_iter_get_basic(&value, &password);
389 dbus_message_iter_next(&dict);
393 username_password_reply->callback(username_password_reply->service,
394 values_received, NULL, 0,
397 username_password_reply->user_data);
398 g_free(username_password_reply);
401 int __connman_agent_request_passphrase_input(struct connman_service *service,
402 authentication_cb_t callback, void *user_data)
404 DBusMessage *message;
405 const char *path, *agent_sender, *agent_path;
406 DBusMessageIter iter;
407 DBusMessageIter dict;
408 struct request_input_reply *passphrase_reply;
411 connman_agent_get_info(&agent_sender, &agent_path);
413 if (service == NULL || agent_path == NULL || callback == NULL)
416 message = dbus_message_new_method_call(agent_sender, agent_path,
417 CONNMAN_AGENT_INTERFACE,
422 dbus_message_iter_init_append(message, &iter);
424 path = __connman_service_get_path(service);
425 dbus_message_iter_append_basic(&iter,
426 DBUS_TYPE_OBJECT_PATH, &path);
428 connman_dbus_dict_open(&iter, &dict);
430 if (__connman_service_is_hidden(service)) {
431 connman_dbus_dict_append_dict(&dict, "Name",
432 request_input_append_name, NULL);
433 connman_dbus_dict_append_dict(&dict, "SSID",
434 request_input_append_ssid, NULL);
437 if (__connman_service_get_security(service) ==
438 CONNMAN_SERVICE_SECURITY_8021X) {
439 connman_dbus_dict_append_dict(&dict, "Identity",
440 request_input_append_identity, service);
443 if (__connman_service_get_security(service) !=
444 CONNMAN_SERVICE_SECURITY_NONE) {
445 connman_dbus_dict_append_dict(&dict, "Passphrase",
446 request_input_append_passphrase, service);
448 previous_passphrase_handler(&dict, service);
451 if (__connman_service_wps_enabled(service) == TRUE) {
452 connman_dbus_dict_append_dict(&dict, "WPS",
453 request_input_append_wps, NULL);
456 connman_dbus_dict_close(&iter, &dict);
458 passphrase_reply = g_try_new0(struct request_input_reply, 1);
459 if (passphrase_reply == NULL) {
460 dbus_message_unref(message);
464 passphrase_reply->service = service;
465 passphrase_reply->callback = callback;
466 passphrase_reply->user_data = user_data;
468 err = connman_agent_queue_message(service, message,
469 connman_timeout_input_request(),
470 request_input_passphrase_reply,
473 if (err < 0 && err != -EBUSY) {
474 DBG("error %d sending agent message", err);
475 dbus_message_unref(message);
476 g_free(passphrase_reply);
480 dbus_message_unref(message);
485 int __connman_agent_request_login_input(struct connman_service *service,
486 authentication_cb_t callback, void *user_data)
488 DBusMessage *message;
489 const char *path, *agent_sender, *agent_path;
490 DBusMessageIter iter;
491 DBusMessageIter dict;
492 struct request_input_reply *username_password_reply;
495 connman_agent_get_info(&agent_sender, &agent_path);
497 if (service == NULL || agent_path == NULL || callback == NULL)
500 message = dbus_message_new_method_call(agent_sender, agent_path,
501 CONNMAN_AGENT_INTERFACE,
506 dbus_message_iter_init_append(message, &iter);
508 path = __connman_service_get_path(service);
509 dbus_message_iter_append_basic(&iter,
510 DBUS_TYPE_OBJECT_PATH, &path);
512 connman_dbus_dict_open(&iter, &dict);
514 connman_dbus_dict_append_dict(&dict, "Username",
515 request_input_append_identity, service);
517 connman_dbus_dict_append_dict(&dict, "Password",
518 request_input_append_password, service);
520 connman_dbus_dict_close(&iter, &dict);
522 username_password_reply = g_try_new0(struct request_input_reply, 1);
523 if (username_password_reply == NULL) {
524 dbus_message_unref(message);
528 username_password_reply->service = service;
529 username_password_reply->callback = callback;
530 username_password_reply->user_data = user_data;
532 err = connman_agent_queue_message(service, message,
533 connman_timeout_input_request(),
534 request_input_login_reply, username_password_reply);
535 if (err < 0 && err != -EBUSY) {
536 DBG("error %d sending agent request", err);
537 dbus_message_unref(message);
538 g_free(username_password_reply);
542 dbus_message_unref(message);
547 struct request_browser_reply_data {
548 struct connman_service *service;
549 browser_authentication_cb_t callback;
553 static void request_browser_reply(DBusMessage *reply, void *user_data)
555 struct request_browser_reply_data *browser_reply_data = user_data;
556 connman_bool_t result = FALSE;
557 const char *error = NULL;
559 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR) {
560 error = dbus_message_get_error_name(reply);
567 browser_reply_data->callback(browser_reply_data->service, result,
568 error, browser_reply_data->user_data);
569 g_free(browser_reply_data);
572 int __connman_agent_request_browser(struct connman_service *service,
573 browser_authentication_cb_t callback,
574 const char *url, void *user_data)
576 struct request_browser_reply_data *browser_reply_data;
577 DBusMessage *message;
578 DBusMessageIter iter;
579 const char *path, *agent_sender, *agent_path;
582 connman_agent_get_info(&agent_sender, &agent_path);
584 if (service == NULL || agent_path == NULL || callback == NULL)
590 message = dbus_message_new_method_call(agent_sender, agent_path,
591 CONNMAN_AGENT_INTERFACE,
596 dbus_message_iter_init_append(message, &iter);
598 path = __connman_service_get_path(service);
599 dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH, &path);
601 dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &url);
603 browser_reply_data = g_try_new0(struct request_browser_reply_data, 1);
604 if (browser_reply_data == NULL) {
605 dbus_message_unref(message);
609 browser_reply_data->service = service;
610 browser_reply_data->callback = callback;
611 browser_reply_data->user_data = user_data;
613 err = connman_agent_queue_message(service, message,
614 connman_timeout_browser_launch(),
615 request_browser_reply, browser_reply_data);
617 if (err < 0 && err != -EBUSY) {
618 DBG("error %d sending browser request", err);
619 dbus_message_unref(message);
620 g_free(browser_reply_data);
624 dbus_message_unref(message);