hostapd: Fix internal crypto build without TLS
[wpasupplicant] / wpa_supplicant / ctrl_iface_dbus.c
1 /*
2  * WPA Supplicant / dbus-based control interface
3  * Copyright (c) 2006, Dan Williams <dcbw@redhat.com> and Red Hat, Inc.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  *
9  * Alternatively, this software may be distributed under the terms of BSD
10  * license.
11  *
12  * See README and COPYING for more details.
13  */
14
15 #include "includes.h"
16
17 #include "common.h"
18 #include "eloop.h"
19 #include "config.h"
20 #include "wpa_supplicant_i.h"
21 #include "drivers/driver.h"
22 #include "wps/wps.h"
23 #include "ctrl_iface_dbus.h"
24 #include "ctrl_iface_dbus_handlers.h"
25
26 #define _DBUS_VERSION (DBUS_VERSION_MAJOR << 8 | DBUS_VERSION_MINOR)
27 #define DBUS_VER(major, minor) ((major) << 8 | (minor))
28
29 #if _DBUS_VERSION < DBUS_VER(1,1)
30 #define dbus_watch_get_unix_fd dbus_watch_get_fd
31 #endif
32
33
34 struct ctrl_iface_dbus_priv {
35         DBusConnection *con;
36         int should_dispatch;
37         struct wpa_global *global;
38
39         u32 next_objid;
40 };
41
42
43 static void process_watch(struct ctrl_iface_dbus_priv *iface,
44                           DBusWatch *watch, eloop_event_type type)
45 {
46         dbus_connection_ref(iface->con);
47
48         iface->should_dispatch = 0;
49
50         if (type == EVENT_TYPE_READ)
51                 dbus_watch_handle(watch, DBUS_WATCH_READABLE);
52         else if (type == EVENT_TYPE_WRITE)
53                 dbus_watch_handle(watch, DBUS_WATCH_WRITABLE);
54         else if (type == EVENT_TYPE_EXCEPTION)
55                 dbus_watch_handle(watch, DBUS_WATCH_ERROR);
56
57         if (iface->should_dispatch) {
58                 while (dbus_connection_get_dispatch_status(iface->con) ==
59                        DBUS_DISPATCH_DATA_REMAINS)
60                         dbus_connection_dispatch(iface->con);
61                 iface->should_dispatch = 0;
62         }
63
64         dbus_connection_unref(iface->con);
65 }
66
67
68 static void process_watch_exception(int sock, void *eloop_ctx, void *sock_ctx)
69 {
70         process_watch(eloop_ctx, sock_ctx, EVENT_TYPE_EXCEPTION);
71 }
72
73
74 static void process_watch_read(int sock, void *eloop_ctx, void *sock_ctx)
75 {
76         process_watch(eloop_ctx, sock_ctx, EVENT_TYPE_READ);
77 }
78
79
80 static void process_watch_write(int sock, void *eloop_ctx, void *sock_ctx)
81 {
82         process_watch(eloop_ctx, sock_ctx, EVENT_TYPE_WRITE);
83 }
84
85
86 static void connection_setup_add_watch(struct ctrl_iface_dbus_priv *iface,
87                                        DBusWatch *watch)
88 {
89         unsigned int flags;
90         int fd;
91
92         if (!dbus_watch_get_enabled(watch))
93                 return;
94
95         flags = dbus_watch_get_flags(watch);
96         fd = dbus_watch_get_unix_fd(watch);
97
98         eloop_register_sock(fd, EVENT_TYPE_EXCEPTION, process_watch_exception,
99                             iface, watch);
100
101         if (flags & DBUS_WATCH_READABLE) {
102                 eloop_register_sock(fd, EVENT_TYPE_READ, process_watch_read,
103                                     iface, watch);
104         }
105         if (flags & DBUS_WATCH_WRITABLE) {
106                 eloop_register_sock(fd, EVENT_TYPE_WRITE, process_watch_write,
107                                     iface, watch);
108         }
109
110         dbus_watch_set_data(watch, iface, NULL);
111 }
112
113
114 static void connection_setup_remove_watch(struct ctrl_iface_dbus_priv *iface,
115                                           DBusWatch *watch)
116 {
117         unsigned int flags;
118         int fd;
119
120         flags = dbus_watch_get_flags(watch);
121         fd = dbus_watch_get_unix_fd(watch);
122
123         eloop_unregister_sock(fd, EVENT_TYPE_EXCEPTION);
124
125         if (flags & DBUS_WATCH_READABLE)
126                 eloop_unregister_sock(fd, EVENT_TYPE_READ);
127         if (flags & DBUS_WATCH_WRITABLE)
128                 eloop_unregister_sock(fd, EVENT_TYPE_WRITE);
129
130         dbus_watch_set_data(watch, NULL, NULL);
131 }
132
133
134 static dbus_bool_t add_watch(DBusWatch *watch, void *data)
135 {
136         connection_setup_add_watch(data, watch);
137         return TRUE;
138 }
139
140
141 static void remove_watch(DBusWatch *watch, void *data)
142 {
143         connection_setup_remove_watch(data, watch);
144 }
145
146
147 static void watch_toggled(DBusWatch *watch, void *data)
148 {
149         if (dbus_watch_get_enabled(watch))
150                 add_watch(watch, data);
151         else
152                 remove_watch(watch, data);
153 }
154
155
156 static void process_timeout(void *eloop_ctx, void *sock_ctx)
157 {
158         DBusTimeout *timeout = sock_ctx;
159
160         dbus_timeout_handle(timeout);
161 }
162
163
164 static void connection_setup_add_timeout(struct ctrl_iface_dbus_priv *iface,
165                                          DBusTimeout *timeout)
166 {
167         if (!dbus_timeout_get_enabled(timeout))
168                 return;
169
170         eloop_register_timeout(0, dbus_timeout_get_interval(timeout) * 1000,
171                                process_timeout, iface, timeout);
172
173         dbus_timeout_set_data(timeout, iface, NULL);
174 }
175
176
177 static void connection_setup_remove_timeout(struct ctrl_iface_dbus_priv *iface,
178                                             DBusTimeout *timeout)
179 {
180         eloop_cancel_timeout(process_timeout, iface, timeout);
181         dbus_timeout_set_data(timeout, NULL, NULL);
182 }
183
184
185 static dbus_bool_t add_timeout(DBusTimeout *timeout, void *data)
186 {
187         if (!dbus_timeout_get_enabled(timeout))
188                 return TRUE;
189
190         connection_setup_add_timeout(data, timeout);
191
192         return TRUE;
193 }
194
195
196 static void remove_timeout(DBusTimeout *timeout, void *data)
197 {
198         connection_setup_remove_timeout(data, timeout);
199 }
200
201
202 static void timeout_toggled(DBusTimeout *timeout, void *data)
203 {
204         if (dbus_timeout_get_enabled(timeout))
205                 add_timeout(timeout, data);
206         else
207                 remove_timeout(timeout, data);
208 }
209
210
211 static void process_wakeup_main(int sig, void *eloop_ctx, void *signal_ctx)
212 {
213         struct ctrl_iface_dbus_priv *iface = signal_ctx;
214
215         if (sig != SIGPOLL || !iface->con)
216                 return;
217
218         if (dbus_connection_get_dispatch_status(iface->con) !=
219             DBUS_DISPATCH_DATA_REMAINS)
220                 return;
221
222         /* Only dispatch once - we do not want to starve other events */
223         dbus_connection_ref(iface->con);
224         dbus_connection_dispatch(iface->con);
225         dbus_connection_unref(iface->con);
226 }
227
228
229 /**
230  * wakeup_main - Attempt to wake our mainloop up
231  * @data: dbus control interface private data
232  *
233  * Try to wake up the main eloop so it will process
234  * dbus events that may have happened.
235  */
236 static void wakeup_main(void *data)
237 {
238         struct ctrl_iface_dbus_priv *iface = data;
239
240         /* Use SIGPOLL to break out of the eloop select() */
241         raise(SIGPOLL);
242         iface->should_dispatch = 1;
243 }
244
245
246 /**
247  * connection_setup_wakeup_main - Tell dbus about our wakeup_main function
248  * @iface: dbus control interface private data
249  * Returns: 0 on success, -1 on failure
250  *
251  * Register our wakeup_main handler with dbus
252  */
253 static int connection_setup_wakeup_main(struct ctrl_iface_dbus_priv *iface)
254 {
255         if (eloop_register_signal(SIGPOLL, process_wakeup_main, iface))
256                 return -1;
257
258         dbus_connection_set_wakeup_main_function(iface->con, wakeup_main,
259                                                  iface, NULL);
260
261         return 0;
262 }
263
264
265 /**
266  * wpa_supplicant_dbus_next_objid - Return next available object id
267  * @iface: dbus control interface private data
268  * Returns: Object id
269  */
270 u32 wpa_supplicant_dbus_next_objid (struct ctrl_iface_dbus_priv *iface)
271 {
272         return iface->next_objid++;
273 }
274
275
276 /**
277  * wpas_dbus_decompose_object_path - Decompose an interface object path into parts
278  * @path: The dbus object path
279  * @network: (out) the configured network this object path refers to, if any
280  * @bssid: (out) the scanned bssid this object path refers to, if any
281  * Returns: The object path of the network interface this path refers to
282  *
283  * For a given object path, decomposes the object path into object id, network,
284  * and BSSID parts, if those parts exist.
285  */
286 char * wpas_dbus_decompose_object_path(const char *path, char **network,
287                                        char **bssid)
288 {
289         const unsigned int dev_path_prefix_len =
290                 strlen(WPAS_DBUS_PATH_INTERFACES "/");
291         char *obj_path_only;
292         char *next_sep;
293
294         /* Be a bit paranoid about path */
295         if (!path || strncmp(path, WPAS_DBUS_PATH_INTERFACES "/",
296                              dev_path_prefix_len))
297                 return NULL;
298
299         /* Ensure there's something at the end of the path */
300         if ((path + dev_path_prefix_len)[0] == '\0')
301                 return NULL;
302
303         obj_path_only = strdup(path);
304         if (obj_path_only == NULL)
305                 return NULL;
306
307         next_sep = strchr(obj_path_only + dev_path_prefix_len, '/');
308         if (next_sep != NULL) {
309                 const char *net_part = strstr(next_sep,
310                                               WPAS_DBUS_NETWORKS_PART "/");
311                 const char *bssid_part = strstr(next_sep,
312                                                 WPAS_DBUS_BSSIDS_PART "/");
313
314                 if (network && net_part) {
315                         /* Deal with a request for a configured network */
316                         const char *net_name = net_part +
317                                 strlen(WPAS_DBUS_NETWORKS_PART "/");
318                         *network = NULL;
319                         if (strlen(net_name))
320                                 *network = strdup(net_name);
321                 } else if (bssid && bssid_part) {
322                         /* Deal with a request for a scanned BSSID */
323                         const char *bssid_name = bssid_part +
324                                 strlen(WPAS_DBUS_BSSIDS_PART "/");
325                         if (strlen(bssid_name))
326                                 *bssid = strdup(bssid_name);
327                         else
328                                 *bssid = NULL;
329                 }
330
331                 /* Cut off interface object path before "/" */
332                 *next_sep = '\0';
333         }
334
335         return obj_path_only;
336 }
337
338
339 /**
340  * wpas_dbus_new_invalid_iface_error - Return a new invalid interface error message
341  * @message: Pointer to incoming dbus message this error refers to
342  * Returns: A dbus error message
343  *
344  * Convenience function to create and return an invalid interface error
345  */
346 DBusMessage * wpas_dbus_new_invalid_iface_error(DBusMessage *message)
347 {
348         return dbus_message_new_error(message, WPAS_ERROR_INVALID_IFACE,
349                                       "wpa_supplicant knows nothing about "
350                                       "this interface.");
351 }
352
353
354 /**
355  * wpas_dbus_new_invalid_network_error - Return a new invalid network error message
356  * @message: Pointer to incoming dbus message this error refers to
357  * Returns: a dbus error message
358  *
359  * Convenience function to create and return an invalid network error
360  */
361 DBusMessage * wpas_dbus_new_invalid_network_error(DBusMessage *message)
362 {
363         return dbus_message_new_error(message, WPAS_ERROR_INVALID_NETWORK,
364                                       "The requested network does not exist.");
365 }
366
367
368 /**
369  * wpas_dbus_new_invalid_bssid_error - Return a new invalid bssid error message
370  * @message: Pointer to incoming dbus message this error refers to
371  * Returns: a dbus error message
372  *
373  * Convenience function to create and return an invalid bssid error
374  */
375 static DBusMessage * wpas_dbus_new_invalid_bssid_error(DBusMessage *message)
376 {
377         return dbus_message_new_error(message, WPAS_ERROR_INVALID_BSSID,
378                                       "The BSSID requested was invalid.");
379 }
380
381
382 /**
383  * wpas_dispatch_network_method - dispatch messages for configured networks
384  * @message: the incoming dbus message
385  * @wpa_s: a network interface's data
386  * @network_id: id of the configured network we're interested in
387  * Returns: a reply dbus message, or a dbus error message
388  *
389  * This function dispatches all incoming dbus messages for configured networks.
390  */
391 static DBusMessage * wpas_dispatch_network_method(DBusMessage *message,
392                                                   struct wpa_supplicant *wpa_s,
393                                                   int network_id)
394 {
395         DBusMessage *reply = NULL;
396         const char *method = dbus_message_get_member(message);
397         struct wpa_ssid *ssid;
398
399         ssid = wpa_config_get_network(wpa_s->conf, network_id);
400         if (ssid == NULL)
401                 return wpas_dbus_new_invalid_network_error(message);
402
403         if (!strcmp(method, "set"))
404                 reply = wpas_dbus_iface_set_network(message, wpa_s, ssid);
405         else if (!strcmp(method, "enable"))
406                 reply = wpas_dbus_iface_enable_network(message, wpa_s, ssid);
407         else if (!strcmp(method, "disable"))
408                 reply = wpas_dbus_iface_disable_network(message, wpa_s, ssid);
409
410         return reply;
411 }
412
413
414 /**
415  * wpas_dispatch_bssid_method - dispatch messages for scanned networks
416  * @message: the incoming dbus message
417  * @wpa_s: a network interface's data
418  * @bssid: bssid of the scanned network we're interested in
419  * Returns: a reply dbus message, or a dbus error message
420  *
421  * This function dispatches all incoming dbus messages for scanned networks.
422  */
423 static DBusMessage * wpas_dispatch_bssid_method(DBusMessage *message,
424                                                 struct wpa_supplicant *wpa_s,
425                                                 const char *bssid)
426 {
427         DBusMessage *reply = NULL;
428         const char *method = dbus_message_get_member(message);
429         struct wpa_scan_res *res = NULL;
430         size_t i;
431
432         /* Ensure we actually have scan data */
433         if (wpa_s->scan_res == NULL &&
434             wpa_supplicant_get_scan_results(wpa_s) < 0) {
435                 reply = wpas_dbus_new_invalid_bssid_error(message);
436                 goto out;
437         }
438
439         /* Find the bssid's scan data */
440         for (i = 0; i < wpa_s->scan_res->num; i++) {
441                 struct wpa_scan_res *search_res = wpa_s->scan_res->res[i];
442                 char mac_str[18];
443
444                 memset(mac_str, 0, sizeof(mac_str));
445                 snprintf(mac_str, sizeof(mac_str) - 1, WPAS_DBUS_BSSID_FORMAT,
446                          MAC2STR(search_res->bssid));
447                 if (!strcmp(bssid, mac_str)) {
448                         res = search_res;
449                         break;
450                 }
451         }
452
453         if (!res) {
454                 reply = wpas_dbus_new_invalid_bssid_error(message);
455                 goto out;
456         }
457
458         /* Dispatch the method call against the scanned bssid */
459         if (!strcmp(method, "properties"))
460                 reply = wpas_dbus_bssid_properties(message, wpa_s, res);
461
462 out:
463         return reply;
464 }
465
466
467 /**
468  * wpas_iface_message_handler - Dispatch messages for interfaces or networks
469  * @connection: Connection to the system message bus
470  * @message: An incoming dbus message
471  * @user_data: A pointer to a dbus control interface data structure
472  * Returns: Whether or not the message was handled
473  *
474  * This function dispatches all incoming dbus messages for network interfaces,
475  * or objects owned by them, such as scanned BSSIDs and configured networks.
476  */
477 static DBusHandlerResult wpas_iface_message_handler(DBusConnection *connection,
478                                                     DBusMessage *message,
479                                                     void *user_data)
480 {
481         struct wpa_supplicant *wpa_s = user_data;
482         const char *method = dbus_message_get_member(message);
483         const char *path = dbus_message_get_path(message);
484         const char *msg_interface = dbus_message_get_interface(message);
485         char *iface_obj_path = NULL;
486         char *network = NULL;
487         char *bssid = NULL;
488         DBusMessage *reply = NULL;
489
490         /* Caller must specify a message interface */
491         if (!msg_interface)
492                 goto out;
493
494         iface_obj_path = wpas_dbus_decompose_object_path(path, &network,
495                                                          &bssid);
496         if (iface_obj_path == NULL) {
497                 reply = wpas_dbus_new_invalid_iface_error(message);
498                 goto out;
499         }
500
501         /* Make sure the message's object path actually refers to the
502          * wpa_supplicant structure it's supposed to (which is wpa_s)
503          */
504         if (wpa_supplicant_get_iface_by_dbus_path(wpa_s->global,
505                                                   iface_obj_path) != wpa_s) {
506                 reply = wpas_dbus_new_invalid_iface_error(message);
507                 goto out;
508         }
509
510         if (network && !strcmp(msg_interface, WPAS_DBUS_IFACE_NETWORK)) {
511                 /* A method for one of this interface's configured networks */
512                 int nid = strtoul(network, NULL, 10);
513                 if (errno != EINVAL)
514                         reply = wpas_dispatch_network_method(message, wpa_s,
515                                                              nid);
516                 else
517                         reply = wpas_dbus_new_invalid_network_error(message);
518         } else if (bssid && !strcmp(msg_interface, WPAS_DBUS_IFACE_BSSID)) {
519                 /* A method for one of this interface's scanned BSSIDs */
520                 reply = wpas_dispatch_bssid_method(message, wpa_s, bssid);
521         } else if (!strcmp(msg_interface, WPAS_DBUS_IFACE_INTERFACE)) {
522                 /* A method for an interface only. */
523                 if (!strcmp(method, "scan"))
524                         reply = wpas_dbus_iface_scan(message, wpa_s);
525                 else if (!strcmp(method, "scanResults"))
526                         reply = wpas_dbus_iface_scan_results(message, wpa_s);
527                 else if (!strcmp(method, "addNetwork"))
528                         reply = wpas_dbus_iface_add_network(message, wpa_s);
529                 else if (!strcmp(method, "removeNetwork"))
530                         reply = wpas_dbus_iface_remove_network(message, wpa_s);
531                 else if (!strcmp(method, "selectNetwork"))
532                         reply = wpas_dbus_iface_select_network(message, wpa_s);
533                 else if (!strcmp(method, "capabilities"))
534                         reply = wpas_dbus_iface_capabilities(message, wpa_s);
535                 else if (!strcmp(method, "disconnect"))
536                         reply = wpas_dbus_iface_disconnect(message, wpa_s);
537                 else if (!strcmp(method, "setAPScan"))
538                         reply = wpas_dbus_iface_set_ap_scan(message, wpa_s);
539                 else if (!strcmp(method, "setSmartcardModules"))
540                         reply = wpas_dbus_iface_set_smartcard_modules(message,
541                                                                       wpa_s);
542                 else if (!strcmp(method, "state"))
543                         reply = wpas_dbus_iface_get_state(message, wpa_s);
544                 else if (!strcmp(method, "setBlobs"))
545                         reply = wpas_dbus_iface_set_blobs(message, wpa_s);
546                 else if (!strcmp(method, "removeBlobs"))
547                         reply = wpas_dbus_iface_remove_blobs(message, wpa_s);
548 #ifdef CONFIG_WPS
549                 else if (!os_strcmp(method, "wpsPbc"))
550                         reply = wpas_dbus_iface_wps_pbc(message, wpa_s);
551                 else if (!os_strcmp(method, "wpsPin"))
552                         reply = wpas_dbus_iface_wps_pin(message, wpa_s);
553                 else if (!os_strcmp(method, "wpsReg"))
554                         reply = wpas_dbus_iface_wps_reg(message, wpa_s);
555 #endif /* CONFIG_WPS */
556         }
557
558         /* If the message was handled, send back the reply */
559         if (reply) {
560                 if (!dbus_message_get_no_reply(message))
561                         dbus_connection_send(connection, reply, NULL);
562                 dbus_message_unref(reply);
563         }
564
565 out:
566         free(iface_obj_path);
567         free(network);
568         free(bssid);
569         return reply ? DBUS_HANDLER_RESULT_HANDLED :
570                 DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
571 }
572
573
574 /**
575  * wpas_message_handler - dispatch incoming dbus messages
576  * @connection: connection to the system message bus
577  * @message: an incoming dbus message
578  * @user_data: a pointer to a dbus control interface data structure
579  * Returns: whether or not the message was handled
580  *
581  * This function dispatches all incoming dbus messages to the correct
582  * handlers, depending on what the message's target object path is,
583  * and what the method call is.
584  */
585 static DBusHandlerResult wpas_message_handler(DBusConnection *connection,
586         DBusMessage *message, void *user_data)
587 {
588         struct ctrl_iface_dbus_priv *ctrl_iface = user_data;
589         const char *method;
590         const char *path;
591         const char *msg_interface;
592         DBusMessage *reply = NULL;
593
594         method = dbus_message_get_member(message);
595         path = dbus_message_get_path(message);
596         msg_interface = dbus_message_get_interface(message);
597         if (!method || !path || !ctrl_iface || !msg_interface)
598                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
599
600         /* Validate the method interface */
601         if (strcmp(msg_interface, WPAS_DBUS_INTERFACE) != 0)
602                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
603
604         if (!strcmp(path, WPAS_DBUS_PATH)) {
605                 /* dispatch methods against our global dbus interface here */
606                 if (!strcmp(method, "addInterface")) {
607                         reply = wpas_dbus_global_add_interface(
608                                 message, ctrl_iface->global);
609                 } else if (!strcmp(method, "removeInterface")) {
610                         reply = wpas_dbus_global_remove_interface(
611                                 message, ctrl_iface->global);
612                 } else if (!strcmp(method, "getInterface")) {
613                         reply = wpas_dbus_global_get_interface(
614                                 message, ctrl_iface->global);
615                 } else if (!strcmp(method, "setDebugParams")) {
616                         reply = wpas_dbus_global_set_debugparams(
617                                 message, ctrl_iface->global);
618                 }
619         }
620
621         /* If the message was handled, send back the reply */
622         if (reply) {
623                 if (!dbus_message_get_no_reply(message))
624                         dbus_connection_send(connection, reply, NULL);
625                 dbus_message_unref(reply);
626         }
627
628         return reply ? DBUS_HANDLER_RESULT_HANDLED :
629                 DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
630 }
631
632
633 /**
634  * wpa_supplicant_dbus_notify_scan_results - Send a scan results signal
635  * @wpa_s: %wpa_supplicant network interface data
636  * Returns: 0 on success, -1 on failure
637  *
638  * Notify listeners that this interface has updated scan results.
639  */
640 void wpa_supplicant_dbus_notify_scan_results(struct wpa_supplicant *wpa_s)
641 {
642         struct ctrl_iface_dbus_priv *iface = wpa_s->global->dbus_ctrl_iface;
643         DBusMessage *_signal;
644         const char *path;
645
646         /* Do nothing if the control interface is not turned on */
647         if (iface == NULL)
648                 return;
649
650         path = wpa_supplicant_get_dbus_path(wpa_s);
651         if (path == NULL) {
652                 perror("wpa_supplicant_dbus_notify_scan_results[dbus]: "
653                        "interface didn't have a dbus path");
654                 wpa_printf(MSG_ERROR,
655                            "wpa_supplicant_dbus_notify_scan_results[dbus]: "
656                            "interface didn't have a dbus path; can't send "
657                            "scan result signal.");
658                 return;
659         }
660         _signal = dbus_message_new_signal(path, WPAS_DBUS_IFACE_INTERFACE,
661                                           "ScanResultsAvailable");
662         if (_signal == NULL) {
663                 perror("wpa_supplicant_dbus_notify_scan_results[dbus]: "
664                        "couldn't create dbus signal; likely out of memory");
665                 wpa_printf(MSG_ERROR, "dbus control interface: not enough "
666                            "memory to send scan results signal.");
667                 return;
668         }
669         dbus_connection_send(iface->con, _signal, NULL);
670         dbus_message_unref(_signal);
671 }
672
673
674 /**
675  * wpa_supplicant_dbus_notify_state_change - Send a state change signal
676  * @wpa_s: %wpa_supplicant network interface data
677  * @new_state: new state wpa_supplicant is entering
678  * @old_state: old state wpa_supplicant is leaving
679  * Returns: 0 on success, -1 on failure
680  *
681  * Notify listeners that wpa_supplicant has changed state
682  */
683 void wpa_supplicant_dbus_notify_state_change(struct wpa_supplicant *wpa_s,
684                                              wpa_states new_state,
685                                              wpa_states old_state)
686 {
687         struct ctrl_iface_dbus_priv *iface;
688         DBusMessage *_signal = NULL;
689         const char *path;
690         const char *new_state_str, *old_state_str;
691
692         /* Do nothing if the control interface is not turned on */
693         if (wpa_s->global == NULL)
694                 return;
695         iface = wpa_s->global->dbus_ctrl_iface;
696         if (iface == NULL)
697                 return;
698
699         /* Only send signal if state really changed */
700         if (new_state == old_state)
701                 return;
702
703         path = wpa_supplicant_get_dbus_path(wpa_s);
704         if (path == NULL) {
705                 perror("wpa_supplicant_dbus_notify_state_change[dbus]: "
706                        "interface didn't have a dbus path");
707                 wpa_printf(MSG_ERROR,
708                            "wpa_supplicant_dbus_notify_state_change[dbus]: "
709                            "interface didn't have a dbus path; can't send "
710                            "signal.");
711                 return;
712         }
713         _signal = dbus_message_new_signal(path, WPAS_DBUS_IFACE_INTERFACE,
714                                           "StateChange");
715         if (_signal == NULL) {
716                 perror("wpa_supplicant_dbus_notify_state_change[dbus]: "
717                        "couldn't create dbus signal; likely out of memory");
718                 wpa_printf(MSG_ERROR,
719                            "wpa_supplicant_dbus_notify_state_change[dbus]: "
720                            "couldn't create dbus signal; likely out of "
721                            "memory.");
722                 return;
723         }
724
725         new_state_str = wpa_supplicant_state_txt(new_state);
726         old_state_str = wpa_supplicant_state_txt(old_state);
727         if (new_state_str == NULL || old_state_str == NULL) {
728                 perror("wpa_supplicant_dbus_notify_state_change[dbus]: "
729                        "couldn't convert state strings");
730                 wpa_printf(MSG_ERROR,
731                            "wpa_supplicant_dbus_notify_state_change[dbus]: "
732                            "couldn't convert state strings.");
733                 goto out;
734         }
735
736         if (!dbus_message_append_args(_signal,
737                                       DBUS_TYPE_STRING, &new_state_str,
738                                       DBUS_TYPE_STRING, &old_state_str,
739                                       DBUS_TYPE_INVALID)) {
740                 perror("wpa_supplicant_dbus_notify_state_change[dbus]: "
741                        "not enough memory to construct state change signal.");
742                 wpa_printf(MSG_ERROR,
743                            "wpa_supplicant_dbus_notify_state_change[dbus]: "
744                            "not enough memory to construct state change "
745                            "signal.");
746                 goto out;
747         }
748
749         dbus_connection_send(iface->con, _signal, NULL);
750
751 out:
752         dbus_message_unref(_signal);
753 }
754
755
756 #ifdef CONFIG_WPS
757 void wpa_supplicant_dbus_notify_wps_cred(struct wpa_supplicant *wpa_s,
758                                          const struct wps_credential *cred)
759 {
760         struct ctrl_iface_dbus_priv *iface;
761         DBusMessage *_signal = NULL;
762         const char *path;
763
764         /* Do nothing if the control interface is not turned on */
765         if (wpa_s->global == NULL)
766                 return;
767         iface = wpa_s->global->dbus_ctrl_iface;
768         if (iface == NULL)
769                 return;
770
771         path = wpa_supplicant_get_dbus_path(wpa_s);
772         if (path == NULL) {
773                 perror("wpa_supplicant_dbus_notify_wps_cred[dbus]: "
774                        "interface didn't have a dbus path");
775                 wpa_printf(MSG_ERROR,
776                            "wpa_supplicant_dbus_notify_wps_cred[dbus]: "
777                            "interface didn't have a dbus path; can't send "
778                            "signal.");
779                 return;
780         }
781         _signal = dbus_message_new_signal(path, WPAS_DBUS_IFACE_INTERFACE,
782                                           "WpsCred");
783         if (_signal == NULL) {
784                 perror("wpa_supplicant_dbus_notify_wps_cred[dbus]: "
785                        "couldn't create dbus signal; likely out of memory");
786                 wpa_printf(MSG_ERROR,
787                            "wpa_supplicant_dbus_notify_wps_cred[dbus]: "
788                            "couldn't create dbus signal; likely out of "
789                            "memory.");
790                 return;
791         }
792
793         if (!dbus_message_append_args(_signal,
794                                       DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE,
795                                       &cred->cred_attr, cred->cred_attr_len,
796                                       DBUS_TYPE_INVALID)) {
797                 perror("wpa_supplicant_dbus_notify_wps_cred[dbus]: "
798                        "not enough memory to construct signal.");
799                 wpa_printf(MSG_ERROR,
800                            "wpa_supplicant_dbus_notify_wps_cred[dbus]: "
801                            "not enough memory to construct signal.");
802                 goto out;
803         }
804
805         dbus_connection_send(iface->con, _signal, NULL);
806
807 out:
808         dbus_message_unref(_signal);
809 }
810 #endif /* CONFIG_WPS */
811
812
813 /**
814  * integrate_with_eloop - Register our mainloop integration with dbus
815  * @connection: connection to the system message bus
816  * @iface: a dbus control interface data structure
817  * Returns: 0 on success, -1 on failure
818  *
819  * We register our mainloop integration functions with dbus here.
820  */
821 static int integrate_with_eloop(DBusConnection *connection,
822         struct ctrl_iface_dbus_priv *iface)
823 {
824         if (!dbus_connection_set_watch_functions(connection, add_watch,
825                                                  remove_watch, watch_toggled,
826                                                  iface, NULL)) {
827                 perror("dbus_connection_set_watch_functions[dbus]");
828                 wpa_printf(MSG_ERROR, "Not enough memory to set up dbus.");
829                 return -1;
830         }
831
832         if (!dbus_connection_set_timeout_functions(connection, add_timeout,
833                                                    remove_timeout,
834                                                    timeout_toggled, iface,
835                                                    NULL)) {
836                 perror("dbus_connection_set_timeout_functions[dbus]");
837                 wpa_printf(MSG_ERROR, "Not enough memory to set up dbus.");
838                 return -1;
839         }
840
841         if (connection_setup_wakeup_main(iface) < 0) {
842                 perror("connection_setup_wakeup_main[dbus]");
843                 wpa_printf(MSG_ERROR, "Could not setup main wakeup function.");
844                 return -1;
845         }
846
847         return 0;
848 }
849
850
851 /**
852  * dispatch_initial_dbus_messages - Dispatch initial dbus messages after
853  *     claiming bus name
854  * @eloop_ctx: the DBusConnection to dispatch on
855  * @timeout_ctx: unused
856  *
857  * If clients are quick to notice that wpa_supplicant claimed its bus name,
858  * there may have been messages that came in before initialization was
859  * all finished.  Dispatch those here.
860  */
861 static void dispatch_initial_dbus_messages(void *eloop_ctx, void *timeout_ctx)
862 {
863         DBusConnection *con = eloop_ctx;
864
865         while (dbus_connection_get_dispatch_status(con) ==
866                DBUS_DISPATCH_DATA_REMAINS)
867                 dbus_connection_dispatch(con);
868 }
869
870
871 /**
872  * wpa_supplicant_dbus_ctrl_iface_init - Initialize dbus control interface
873  * @global: Pointer to global data from wpa_supplicant_init()
874  * Returns: Pointer to dbus_ctrl_iface date or %NULL on failure
875  *
876  * Initialize the dbus control interface and start receiving commands from
877  * external programs over the bus.
878  */
879 struct ctrl_iface_dbus_priv *
880 wpa_supplicant_dbus_ctrl_iface_init(struct wpa_global *global)
881 {
882         struct ctrl_iface_dbus_priv *iface;
883         DBusError error;
884         int ret = -1;
885         DBusObjectPathVTable wpas_vtable = {
886                 NULL, &wpas_message_handler, NULL, NULL, NULL, NULL
887         };
888
889         iface = os_zalloc(sizeof(struct ctrl_iface_dbus_priv));
890         if (iface == NULL)
891                 return NULL;
892
893         iface->global = global;
894
895         /* Get a reference to the system bus */
896         dbus_error_init(&error);
897         iface->con = dbus_bus_get(DBUS_BUS_SYSTEM, &error);
898         dbus_error_free(&error);
899         if (!iface->con) {
900                 perror("dbus_bus_get[ctrl_iface_dbus]");
901                 wpa_printf(MSG_ERROR, "Could not acquire the system bus.");
902                 goto fail;
903         }
904
905         /* Tell dbus about our mainloop integration functions */
906         if (integrate_with_eloop(iface->con, iface))
907                 goto fail;
908
909         /* Register the message handler for the global dbus interface */
910         if (!dbus_connection_register_object_path(iface->con,
911                                                   WPAS_DBUS_PATH, &wpas_vtable,
912                                                   iface)) {
913                 perror("dbus_connection_register_object_path[dbus]");
914                 wpa_printf(MSG_ERROR, "Could not set up DBus message "
915                            "handler.");
916                 goto fail;
917         }
918
919         /* Register our service with the message bus */
920         dbus_error_init(&error);
921         switch (dbus_bus_request_name(iface->con, WPAS_DBUS_SERVICE,
922                                       0, &error)) {
923         case DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER:
924                 ret = 0;
925                 break;
926         case DBUS_REQUEST_NAME_REPLY_EXISTS:
927         case DBUS_REQUEST_NAME_REPLY_IN_QUEUE:
928         case DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER:
929                 perror("dbus_bus_request_name[dbus]");
930                 wpa_printf(MSG_ERROR, "Could not request DBus service name: "
931                            "already registered.");
932                 break;
933         default:
934                 perror("dbus_bus_request_name[dbus]");
935                 wpa_printf(MSG_ERROR, "Could not request DBus service name: "
936                            "%s %s.", error.name, error.message);
937                 break;
938         }
939         dbus_error_free(&error);
940
941         if (ret != 0)
942                 goto fail;
943
944         wpa_printf(MSG_DEBUG, "Providing DBus service '" WPAS_DBUS_SERVICE
945                    "'.");
946
947         /*
948          * Dispatch initial DBus messages that may have come in since the bus
949          * name was claimed above. Happens when clients are quick to notice the
950          * wpa_supplicant service.
951          *
952          * FIXME: is there a better solution to this problem?
953          */
954         eloop_register_timeout(0, 50, dispatch_initial_dbus_messages,
955                                iface->con, NULL);
956
957         return iface;
958
959 fail:
960         wpa_supplicant_dbus_ctrl_iface_deinit(iface);
961         return NULL;
962 }
963
964
965 /**
966  * wpa_supplicant_dbus_ctrl_iface_deinit - Deinitialize dbus ctrl interface
967  * @iface: Pointer to dbus private data from
968  * wpa_supplicant_dbus_ctrl_iface_init()
969  *
970  * Deinitialize the dbus control interface that was initialized with
971  * wpa_supplicant_dbus_ctrl_iface_init().
972  */
973 void wpa_supplicant_dbus_ctrl_iface_deinit(struct ctrl_iface_dbus_priv *iface)
974 {
975         if (iface == NULL)
976                 return;
977
978         if (iface->con) {
979                 eloop_cancel_timeout(dispatch_initial_dbus_messages,
980                                      iface->con, NULL);
981                 dbus_connection_set_watch_functions(iface->con, NULL, NULL,
982                                                     NULL, NULL, NULL);
983                 dbus_connection_set_timeout_functions(iface->con, NULL, NULL,
984                                                       NULL, NULL, NULL);
985                 dbus_connection_unref(iface->con);
986         }
987
988         memset(iface, 0, sizeof(struct ctrl_iface_dbus_priv));
989         free(iface);
990 }
991
992
993 /**
994  * wpas_dbus_register_new_iface - Register a new interface with dbus
995  * @wpa_s: %wpa_supplicant interface description structure to register
996  * Returns: 0 on success, -1 on error
997  *
998  * Registers a new interface with dbus and assigns it a dbus object path.
999  */
1000 int wpas_dbus_register_iface(struct wpa_supplicant *wpa_s)
1001 {
1002         struct ctrl_iface_dbus_priv *ctrl_iface =
1003                 wpa_s->global->dbus_ctrl_iface;
1004         DBusConnection * con;
1005         u32 next;
1006         DBusObjectPathVTable vtable = {
1007                 NULL, &wpas_iface_message_handler, NULL, NULL, NULL, NULL
1008         };
1009         char *path;
1010         int ret = -1;
1011
1012         /* Do nothing if the control interface is not turned on */
1013         if (ctrl_iface == NULL)
1014                 return 0;
1015
1016         con = ctrl_iface->con;
1017         next = wpa_supplicant_dbus_next_objid(ctrl_iface);
1018
1019         /* Create and set the interface's object path */
1020         path = os_zalloc(WPAS_DBUS_OBJECT_PATH_MAX);
1021         if (path == NULL)
1022                 return -1;
1023         snprintf(path, WPAS_DBUS_OBJECT_PATH_MAX,
1024                  WPAS_DBUS_PATH_INTERFACES "/%u",
1025                  next);
1026         if (wpa_supplicant_set_dbus_path(wpa_s, path)) {
1027                 wpa_printf(MSG_DEBUG,
1028                            "Failed to set dbus path for interface %s",
1029                            wpa_s->ifname);
1030                 goto out;
1031         }
1032
1033         /* Register the message handler for the interface functions */
1034         if (!dbus_connection_register_fallback(con, path, &vtable, wpa_s)) {
1035                 perror("wpas_dbus_register_iface [dbus]");
1036                 wpa_printf(MSG_ERROR, "Could not set up DBus message "
1037                            "handler for interface %s.", wpa_s->ifname);
1038                 goto out;
1039         }
1040         ret = 0;
1041
1042 out:
1043         free(path);
1044         return ret;
1045 }
1046
1047
1048 /**
1049  * wpas_dbus_unregister_iface - Unregister an interface from dbus
1050  * @wpa_s: wpa_supplicant interface structure
1051  * Returns: 0 on success, -1 on failure
1052  *
1053  * Unregisters the interface with dbus
1054  */
1055 int wpas_dbus_unregister_iface(struct wpa_supplicant *wpa_s)
1056 {
1057         struct ctrl_iface_dbus_priv *ctrl_iface;
1058         DBusConnection *con;
1059         const char *path;
1060
1061         /* Do nothing if the control interface is not turned on */
1062         if (wpa_s == NULL || wpa_s->global == NULL)
1063                 return 0;
1064         ctrl_iface = wpa_s->global->dbus_ctrl_iface;
1065         if (ctrl_iface == NULL)
1066                 return 0;
1067
1068         con = ctrl_iface->con;
1069         path = wpa_supplicant_get_dbus_path(wpa_s);
1070
1071         if (!dbus_connection_unregister_object_path(con, path))
1072                 return -1;
1073
1074         free(wpa_s->dbus_path);
1075         wpa_s->dbus_path = NULL;
1076
1077         return 0;
1078 }
1079
1080
1081 /**
1082  * wpa_supplicant_get_iface_by_dbus_path - Get a new network interface
1083  * @global: Pointer to global data from wpa_supplicant_init()
1084  * @path: Pointer to a dbus object path representing an interface
1085  * Returns: Pointer to the interface or %NULL if not found
1086  */
1087 struct wpa_supplicant * wpa_supplicant_get_iface_by_dbus_path(
1088         struct wpa_global *global, const char *path)
1089 {
1090         struct wpa_supplicant *wpa_s;
1091
1092         for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
1093                 if (strcmp(wpa_s->dbus_path, path) == 0)
1094                         return wpa_s;
1095         }
1096         return NULL;
1097 }
1098
1099
1100 /**
1101  * wpa_supplicant_set_dbus_path - Assign a dbus path to an interface
1102  * @wpa_s: wpa_supplicant interface structure
1103  * @path: dbus path to set on the interface
1104  * Returns: 0 on succes, -1 on error
1105  */
1106 int wpa_supplicant_set_dbus_path(struct wpa_supplicant *wpa_s,
1107                                   const char *path)
1108 {
1109         u32 len = strlen (path);
1110         if (len >= WPAS_DBUS_OBJECT_PATH_MAX)
1111                 return -1;
1112         if (wpa_s->dbus_path)
1113                 return -1;
1114         wpa_s->dbus_path = strdup(path);
1115         return 0;
1116 }
1117
1118
1119 /**
1120  * wpa_supplicant_get_dbus_path - Get an interface's dbus path
1121  * @wpa_s: %wpa_supplicant interface structure
1122  * Returns: Interface's dbus object path, or %NULL on error
1123  */
1124 const char * wpa_supplicant_get_dbus_path(struct wpa_supplicant *wpa_s)
1125 {
1126         return wpa_s->dbus_path;
1127 }