dbus: Do not include libdbus dbus/dbus.h into dbus_{old,new}.h
[mech_eap.git] / wpa_supplicant / dbus / dbus_old.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 #include <dbus/dbus.h>
17
18 #include "common.h"
19 #include "eloop.h"
20 #include "drivers/driver.h"
21 #include "wps/wps.h"
22 #include "../config.h"
23 #include "../wpa_supplicant_i.h"
24 #include "dbus_old.h"
25 #include "dbus_old_handlers.h"
26 #include "dbus_common.h"
27 #include "dbus_common_i.h"
28
29
30 /**
31  * wpas_dbus_decompose_object_path - Decompose an interface object path into parts
32  * @path: The dbus object path
33  * @network: (out) the configured network this object path refers to, if any
34  * @bssid: (out) the scanned bssid this object path refers to, if any
35  * Returns: The object path of the network interface this path refers to
36  *
37  * For a given object path, decomposes the object path into object id, network,
38  * and BSSID parts, if those parts exist.
39  */
40 char * wpas_dbus_decompose_object_path(const char *path, char **network,
41                                        char **bssid)
42 {
43         const unsigned int dev_path_prefix_len =
44                 strlen(WPAS_DBUS_PATH_INTERFACES "/");
45         char *obj_path_only;
46         char *next_sep;
47
48         /* Be a bit paranoid about path */
49         if (!path || strncmp(path, WPAS_DBUS_PATH_INTERFACES "/",
50                              dev_path_prefix_len))
51                 return NULL;
52
53         /* Ensure there's something at the end of the path */
54         if ((path + dev_path_prefix_len)[0] == '\0')
55                 return NULL;
56
57         obj_path_only = os_strdup(path);
58         if (obj_path_only == NULL)
59                 return NULL;
60
61         next_sep = strchr(obj_path_only + dev_path_prefix_len, '/');
62         if (next_sep != NULL) {
63                 const char *net_part = strstr(next_sep,
64                                               WPAS_DBUS_NETWORKS_PART "/");
65                 const char *bssid_part = strstr(next_sep,
66                                                 WPAS_DBUS_BSSIDS_PART "/");
67
68                 if (network && net_part) {
69                         /* Deal with a request for a configured network */
70                         const char *net_name = net_part +
71                                 strlen(WPAS_DBUS_NETWORKS_PART "/");
72                         *network = NULL;
73                         if (strlen(net_name))
74                                 *network = os_strdup(net_name);
75                 } else if (bssid && bssid_part) {
76                         /* Deal with a request for a scanned BSSID */
77                         const char *bssid_name = bssid_part +
78                                 strlen(WPAS_DBUS_BSSIDS_PART "/");
79                         if (strlen(bssid_name))
80                                 *bssid = os_strdup(bssid_name);
81                         else
82                                 *bssid = NULL;
83                 }
84
85                 /* Cut off interface object path before "/" */
86                 *next_sep = '\0';
87         }
88
89         return obj_path_only;
90 }
91
92
93 /**
94  * wpas_dbus_new_invalid_iface_error - Return a new invalid interface error message
95  * @message: Pointer to incoming dbus message this error refers to
96  * Returns: A dbus error message
97  *
98  * Convenience function to create and return an invalid interface error
99  */
100 DBusMessage * wpas_dbus_new_invalid_iface_error(DBusMessage *message)
101 {
102         return dbus_message_new_error(message, WPAS_ERROR_INVALID_IFACE,
103                                       "wpa_supplicant knows nothing about "
104                                       "this interface.");
105 }
106
107
108 /**
109  * wpas_dbus_new_invalid_network_error - Return a new invalid network error message
110  * @message: Pointer to incoming dbus message this error refers to
111  * Returns: a dbus error message
112  *
113  * Convenience function to create and return an invalid network error
114  */
115 DBusMessage * wpas_dbus_new_invalid_network_error(DBusMessage *message)
116 {
117         return dbus_message_new_error(message, WPAS_ERROR_INVALID_NETWORK,
118                                       "The requested network does not exist.");
119 }
120
121
122 /**
123  * wpas_dbus_new_invalid_bssid_error - Return a new invalid bssid error message
124  * @message: Pointer to incoming dbus message this error refers to
125  * Returns: a dbus error message
126  *
127  * Convenience function to create and return an invalid bssid error
128  */
129 static DBusMessage * wpas_dbus_new_invalid_bssid_error(DBusMessage *message)
130 {
131         return dbus_message_new_error(message, WPAS_ERROR_INVALID_BSSID,
132                                       "The BSSID requested was invalid.");
133 }
134
135
136 /**
137  * wpas_dispatch_network_method - dispatch messages for configured networks
138  * @message: the incoming dbus message
139  * @wpa_s: a network interface's data
140  * @network_id: id of the configured network we're interested in
141  * Returns: a reply dbus message, or a dbus error message
142  *
143  * This function dispatches all incoming dbus messages for configured networks.
144  */
145 static DBusMessage * wpas_dispatch_network_method(DBusMessage *message,
146                                                   struct wpa_supplicant *wpa_s,
147                                                   int network_id)
148 {
149         DBusMessage *reply = NULL;
150         const char *method = dbus_message_get_member(message);
151         struct wpa_ssid *ssid;
152
153         ssid = wpa_config_get_network(wpa_s->conf, network_id);
154         if (ssid == NULL)
155                 return wpas_dbus_new_invalid_network_error(message);
156
157         if (!strcmp(method, "set"))
158                 reply = wpas_dbus_iface_set_network(message, wpa_s, ssid);
159         else if (!strcmp(method, "enable"))
160                 reply = wpas_dbus_iface_enable_network(message, wpa_s, ssid);
161         else if (!strcmp(method, "disable"))
162                 reply = wpas_dbus_iface_disable_network(message, wpa_s, ssid);
163
164         return reply;
165 }
166
167
168 /**
169  * wpas_dispatch_bssid_method - dispatch messages for scanned networks
170  * @message: the incoming dbus message
171  * @wpa_s: a network interface's data
172  * @bssid: bssid of the scanned network we're interested in
173  * Returns: a reply dbus message, or a dbus error message
174  *
175  * This function dispatches all incoming dbus messages for scanned networks.
176  */
177 static DBusMessage * wpas_dispatch_bssid_method(DBusMessage *message,
178                                                 struct wpa_supplicant *wpa_s,
179                                                 const char *bssid)
180 {
181         DBusMessage *reply = NULL;
182         const char *method = dbus_message_get_member(message);
183         struct wpa_scan_res *res = NULL;
184         size_t i;
185
186         /* Ensure we actually have scan data */
187         if (wpa_s->scan_res == NULL &&
188             wpa_supplicant_get_scan_results(wpa_s) < 0) {
189                 reply = wpas_dbus_new_invalid_bssid_error(message);
190                 goto out;
191         }
192
193         /* Find the bssid's scan data */
194         for (i = 0; i < wpa_s->scan_res->num; i++) {
195                 struct wpa_scan_res *search_res = wpa_s->scan_res->res[i];
196                 char mac_str[18];
197
198                 memset(mac_str, 0, sizeof(mac_str));
199                 snprintf(mac_str, sizeof(mac_str) - 1, WPAS_DBUS_BSSID_FORMAT,
200                          MAC2STR(search_res->bssid));
201                 if (!strcmp(bssid, mac_str)) {
202                         res = search_res;
203                         break;
204                 }
205         }
206
207         if (!res) {
208                 reply = wpas_dbus_new_invalid_bssid_error(message);
209                 goto out;
210         }
211
212         /* Dispatch the method call against the scanned bssid */
213         if (!strcmp(method, "properties"))
214                 reply = wpas_dbus_bssid_properties(message, wpa_s, res);
215
216 out:
217         return reply;
218 }
219
220
221 /**
222  * wpas_iface_message_handler - Dispatch messages for interfaces or networks
223  * @connection: Connection to the system message bus
224  * @message: An incoming dbus message
225  * @user_data: A pointer to a dbus control interface data structure
226  * Returns: Whether or not the message was handled
227  *
228  * This function dispatches all incoming dbus messages for network interfaces,
229  * or objects owned by them, such as scanned BSSIDs and configured networks.
230  */
231 static DBusHandlerResult wpas_iface_message_handler(DBusConnection *connection,
232                                                     DBusMessage *message,
233                                                     void *user_data)
234 {
235         struct wpa_supplicant *wpa_s = user_data;
236         const char *method = dbus_message_get_member(message);
237         const char *path = dbus_message_get_path(message);
238         const char *msg_interface = dbus_message_get_interface(message);
239         char *iface_obj_path = NULL;
240         char *network = NULL;
241         char *bssid = NULL;
242         DBusMessage *reply = NULL;
243
244         /* Caller must specify a message interface */
245         if (!msg_interface)
246                 goto out;
247
248         iface_obj_path = wpas_dbus_decompose_object_path(path, &network,
249                                                          &bssid);
250         if (iface_obj_path == NULL) {
251                 reply = wpas_dbus_new_invalid_iface_error(message);
252                 goto out;
253         }
254
255         /* Make sure the message's object path actually refers to the
256          * wpa_supplicant structure it's supposed to (which is wpa_s)
257          */
258         if (wpa_supplicant_get_iface_by_dbus_path(wpa_s->global,
259                                                   iface_obj_path) != wpa_s) {
260                 reply = wpas_dbus_new_invalid_iface_error(message);
261                 goto out;
262         }
263
264         if (network && !strcmp(msg_interface, WPAS_DBUS_IFACE_NETWORK)) {
265                 /* A method for one of this interface's configured networks */
266                 int nid = strtoul(network, NULL, 10);
267                 if (errno != EINVAL)
268                         reply = wpas_dispatch_network_method(message, wpa_s,
269                                                              nid);
270                 else
271                         reply = wpas_dbus_new_invalid_network_error(message);
272         } else if (bssid && !strcmp(msg_interface, WPAS_DBUS_IFACE_BSSID)) {
273                 /* A method for one of this interface's scanned BSSIDs */
274                 reply = wpas_dispatch_bssid_method(message, wpa_s, bssid);
275         } else if (!strcmp(msg_interface, WPAS_DBUS_IFACE_INTERFACE)) {
276                 /* A method for an interface only. */
277                 if (!strcmp(method, "scan"))
278                         reply = wpas_dbus_iface_scan(message, wpa_s);
279                 else if (!strcmp(method, "scanResults"))
280                         reply = wpas_dbus_iface_scan_results(message, wpa_s);
281                 else if (!strcmp(method, "addNetwork"))
282                         reply = wpas_dbus_iface_add_network(message, wpa_s);
283                 else if (!strcmp(method, "removeNetwork"))
284                         reply = wpas_dbus_iface_remove_network(message, wpa_s);
285                 else if (!strcmp(method, "selectNetwork"))
286                         reply = wpas_dbus_iface_select_network(message, wpa_s);
287                 else if (!strcmp(method, "capabilities"))
288                         reply = wpas_dbus_iface_capabilities(message, wpa_s);
289                 else if (!strcmp(method, "disconnect"))
290                         reply = wpas_dbus_iface_disconnect(message, wpa_s);
291                 else if (!strcmp(method, "setAPScan"))
292                         reply = wpas_dbus_iface_set_ap_scan(message, wpa_s);
293                 else if (!strcmp(method, "setSmartcardModules"))
294                         reply = wpas_dbus_iface_set_smartcard_modules(message,
295                                                                       wpa_s);
296                 else if (!strcmp(method, "state"))
297                         reply = wpas_dbus_iface_get_state(message, wpa_s);
298                 else if (!strcmp(method, "scanning"))
299                         reply = wpas_dbus_iface_get_scanning(message, wpa_s);
300                 else if (!strcmp(method, "setBlobs"))
301                         reply = wpas_dbus_iface_set_blobs(message, wpa_s);
302                 else if (!strcmp(method, "removeBlobs"))
303                         reply = wpas_dbus_iface_remove_blobs(message, wpa_s);
304 #ifdef CONFIG_WPS
305                 else if (!os_strcmp(method, "wpsPbc"))
306                         reply = wpas_dbus_iface_wps_pbc(message, wpa_s);
307                 else if (!os_strcmp(method, "wpsPin"))
308                         reply = wpas_dbus_iface_wps_pin(message, wpa_s);
309                 else if (!os_strcmp(method, "wpsReg"))
310                         reply = wpas_dbus_iface_wps_reg(message, wpa_s);
311 #endif /* CONFIG_WPS */
312         }
313
314         /* If the message was handled, send back the reply */
315         if (reply) {
316                 if (!dbus_message_get_no_reply(message))
317                         dbus_connection_send(connection, reply, NULL);
318                 dbus_message_unref(reply);
319         }
320
321 out:
322         os_free(iface_obj_path);
323         os_free(network);
324         os_free(bssid);
325         return reply ? DBUS_HANDLER_RESULT_HANDLED :
326                 DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
327 }
328
329
330 /**
331  * wpas_message_handler - dispatch incoming dbus messages
332  * @connection: connection to the system message bus
333  * @message: an incoming dbus message
334  * @user_data: a pointer to a dbus control interface data structure
335  * Returns: whether or not the message was handled
336  *
337  * This function dispatches all incoming dbus messages to the correct
338  * handlers, depending on what the message's target object path is,
339  * and what the method call is.
340  */
341 static DBusHandlerResult wpas_message_handler(DBusConnection *connection,
342         DBusMessage *message, void *user_data)
343 {
344         struct wpas_dbus_priv *ctrl_iface = user_data;
345         const char *method;
346         const char *path;
347         const char *msg_interface;
348         DBusMessage *reply = NULL;
349
350         method = dbus_message_get_member(message);
351         path = dbus_message_get_path(message);
352         msg_interface = dbus_message_get_interface(message);
353         if (!method || !path || !ctrl_iface || !msg_interface)
354                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
355
356         /* Validate the method interface */
357         if (strcmp(msg_interface, WPAS_DBUS_INTERFACE) != 0)
358                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
359
360         if (!strcmp(path, WPAS_DBUS_PATH)) {
361                 /* dispatch methods against our global dbus interface here */
362                 if (!strcmp(method, "addInterface")) {
363                         reply = wpas_dbus_global_add_interface(
364                                 message, ctrl_iface->global);
365                 } else if (!strcmp(method, "removeInterface")) {
366                         reply = wpas_dbus_global_remove_interface(
367                                 message, ctrl_iface->global);
368                 } else if (!strcmp(method, "getInterface")) {
369                         reply = wpas_dbus_global_get_interface(
370                                 message, ctrl_iface->global);
371                 } else if (!strcmp(method, "setDebugParams")) {
372                         reply = wpas_dbus_global_set_debugparams(
373                                 message, ctrl_iface->global);
374                 }
375         }
376
377         /* If the message was handled, send back the reply */
378         if (reply) {
379                 if (!dbus_message_get_no_reply(message))
380                         dbus_connection_send(connection, reply, NULL);
381                 dbus_message_unref(reply);
382         }
383
384         return reply ? DBUS_HANDLER_RESULT_HANDLED :
385                 DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
386 }
387
388
389 /**
390  * wpa_supplicant_dbus_notify_scan_results - Send a scan results signal
391  * @wpa_s: %wpa_supplicant network interface data
392  * Returns: 0 on success, -1 on failure
393  *
394  * Notify listeners that this interface has updated scan results.
395  */
396 void wpa_supplicant_dbus_notify_scan_results(struct wpa_supplicant *wpa_s)
397 {
398         struct wpas_dbus_priv *iface = wpa_s->global->dbus;
399         DBusMessage *_signal;
400         const char *path;
401
402         /* Do nothing if the control interface is not turned on */
403         if (iface == NULL)
404                 return;
405
406         path = wpa_supplicant_get_dbus_path(wpa_s);
407         if (path == NULL) {
408                 perror("wpa_supplicant_dbus_notify_scan_results[dbus]: "
409                        "interface didn't have a dbus path");
410                 wpa_printf(MSG_ERROR,
411                            "wpa_supplicant_dbus_notify_scan_results[dbus]: "
412                            "interface didn't have a dbus path; can't send "
413                            "scan result signal.");
414                 return;
415         }
416         _signal = dbus_message_new_signal(path, WPAS_DBUS_IFACE_INTERFACE,
417                                           "ScanResultsAvailable");
418         if (_signal == NULL) {
419                 perror("wpa_supplicant_dbus_notify_scan_results[dbus]: "
420                        "couldn't create dbus signal; likely out of memory");
421                 wpa_printf(MSG_ERROR, "dbus control interface: not enough "
422                            "memory to send scan results signal.");
423                 return;
424         }
425         dbus_connection_send(iface->con, _signal, NULL);
426         dbus_message_unref(_signal);
427 }
428
429
430 /**
431  * wpa_supplicant_dbus_notify_state_change - Send a state change signal
432  * @wpa_s: %wpa_supplicant network interface data
433  * @new_state: new state wpa_supplicant is entering
434  * @old_state: old state wpa_supplicant is leaving
435  * Returns: 0 on success, -1 on failure
436  *
437  * Notify listeners that wpa_supplicant has changed state
438  */
439 void wpa_supplicant_dbus_notify_state_change(struct wpa_supplicant *wpa_s,
440                                              enum wpa_states new_state,
441                                              enum wpa_states old_state)
442 {
443         struct wpas_dbus_priv *iface;
444         DBusMessage *_signal = NULL;
445         const char *path;
446         const char *new_state_str, *old_state_str;
447
448         /* Do nothing if the control interface is not turned on */
449         if (wpa_s->global == NULL)
450                 return;
451         iface = wpa_s->global->dbus;
452         if (iface == NULL)
453                 return;
454
455         /* Only send signal if state really changed */
456         if (new_state == old_state)
457                 return;
458
459         path = wpa_supplicant_get_dbus_path(wpa_s);
460         if (path == NULL) {
461                 perror("wpa_supplicant_dbus_notify_state_change[dbus]: "
462                        "interface didn't have a dbus path");
463                 wpa_printf(MSG_ERROR,
464                            "wpa_supplicant_dbus_notify_state_change[dbus]: "
465                            "interface didn't have a dbus path; can't send "
466                            "signal.");
467                 return;
468         }
469         _signal = dbus_message_new_signal(path, WPAS_DBUS_IFACE_INTERFACE,
470                                           "StateChange");
471         if (_signal == NULL) {
472                 perror("wpa_supplicant_dbus_notify_state_change[dbus]: "
473                        "couldn't create dbus signal; likely out of memory");
474                 wpa_printf(MSG_ERROR,
475                            "wpa_supplicant_dbus_notify_state_change[dbus]: "
476                            "couldn't create dbus signal; likely out of "
477                            "memory.");
478                 return;
479         }
480
481         new_state_str = wpa_supplicant_state_txt(new_state);
482         old_state_str = wpa_supplicant_state_txt(old_state);
483         if (new_state_str == NULL || old_state_str == NULL) {
484                 perror("wpa_supplicant_dbus_notify_state_change[dbus]: "
485                        "couldn't convert state strings");
486                 wpa_printf(MSG_ERROR,
487                            "wpa_supplicant_dbus_notify_state_change[dbus]: "
488                            "couldn't convert state strings.");
489                 goto out;
490         }
491
492         if (!dbus_message_append_args(_signal,
493                                       DBUS_TYPE_STRING, &new_state_str,
494                                       DBUS_TYPE_STRING, &old_state_str,
495                                       DBUS_TYPE_INVALID)) {
496                 perror("wpa_supplicant_dbus_notify_state_change[dbus]: "
497                        "not enough memory to construct state change signal.");
498                 wpa_printf(MSG_ERROR,
499                            "wpa_supplicant_dbus_notify_state_change[dbus]: "
500                            "not enough memory to construct state change "
501                            "signal.");
502                 goto out;
503         }
504
505         dbus_connection_send(iface->con, _signal, NULL);
506
507 out:
508         dbus_message_unref(_signal);
509 }
510
511
512 /**
513  * wpa_supplicant_dbus_notify_scanning - send scanning status
514  * @wpa_s: %wpa_supplicant network interface data
515  * Returns: 0 on success, -1 on failure
516  *
517  * Notify listeners of interface scanning state changes
518  */
519 void wpa_supplicant_dbus_notify_scanning(struct wpa_supplicant *wpa_s)
520 {
521         struct wpas_dbus_priv *iface = wpa_s->global->dbus;
522         DBusMessage *_signal;
523         const char *path;
524         dbus_bool_t scanning = wpa_s->scanning ? TRUE : FALSE;
525
526         /* Do nothing if the control interface is not turned on */
527         if (iface == NULL)
528                 return;
529
530         path = wpa_supplicant_get_dbus_path(wpa_s);
531         if (path == NULL) {
532                 perror("wpa_supplicant_dbus_notify_scanning[dbus]: interface "
533                        "didn't have a dbus path");
534                 wpa_printf(MSG_ERROR,
535                            "%s[dbus]: interface didn't have a dbus path; "
536                            "can't send scanning signal.", __FUNCTION__);
537                 return;
538         }
539         _signal = dbus_message_new_signal(path, WPAS_DBUS_IFACE_INTERFACE,
540                                           "Scanning");
541         if (_signal == NULL) {
542                 perror("wpa_supplicant_dbus_notify_scanning[dbus]: couldn't "
543                        "create dbus signal; likely out of memory");
544                 wpa_printf(MSG_ERROR, "%s[dbus]: dbus control interface: not "
545                            "enough memory to send scan results signal.",
546                            __FUNCTION__);
547                 return;
548         }
549
550         if (dbus_message_append_args(_signal,
551                                      DBUS_TYPE_BOOLEAN, &scanning,
552                                      DBUS_TYPE_INVALID)) {
553                 dbus_connection_send(iface->con, _signal, NULL);
554         } else {
555                 perror("wpa_supplicant_dbus_notify_scanning[dbus]: not enough "
556                        "memory to construct signal.");
557                 wpa_printf(MSG_ERROR, "%s[dbus]: not enough memory to "
558                            "construct signal.", __FUNCTION__);
559         }
560         dbus_message_unref(_signal);
561 }
562
563
564 #ifdef CONFIG_WPS
565 void wpa_supplicant_dbus_notify_wps_cred(struct wpa_supplicant *wpa_s,
566                                          const struct wps_credential *cred)
567 {
568         struct wpas_dbus_priv *iface;
569         DBusMessage *_signal = NULL;
570         const char *path;
571
572         /* Do nothing if the control interface is not turned on */
573         if (wpa_s->global == NULL)
574                 return;
575         iface = wpa_s->global->dbus;
576         if (iface == NULL)
577                 return;
578
579         path = wpa_supplicant_get_dbus_path(wpa_s);
580         if (path == NULL) {
581                 perror("wpa_supplicant_dbus_notify_wps_cred[dbus]: "
582                        "interface didn't have a dbus path");
583                 wpa_printf(MSG_ERROR,
584                            "wpa_supplicant_dbus_notify_wps_cred[dbus]: "
585                            "interface didn't have a dbus path; can't send "
586                            "signal.");
587                 return;
588         }
589         _signal = dbus_message_new_signal(path, WPAS_DBUS_IFACE_INTERFACE,
590                                           "WpsCred");
591         if (_signal == NULL) {
592                 perror("wpa_supplicant_dbus_notify_wps_cred[dbus]: "
593                        "couldn't create dbus signal; likely out of memory");
594                 wpa_printf(MSG_ERROR,
595                            "wpa_supplicant_dbus_notify_wps_cred[dbus]: "
596                            "couldn't create dbus signal; likely out of "
597                            "memory.");
598                 return;
599         }
600
601         if (!dbus_message_append_args(_signal,
602                                       DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE,
603                                       &cred->cred_attr, cred->cred_attr_len,
604                                       DBUS_TYPE_INVALID)) {
605                 perror("wpa_supplicant_dbus_notify_wps_cred[dbus]: "
606                        "not enough memory to construct signal.");
607                 wpa_printf(MSG_ERROR,
608                            "wpa_supplicant_dbus_notify_wps_cred[dbus]: "
609                            "not enough memory to construct signal.");
610                 goto out;
611         }
612
613         dbus_connection_send(iface->con, _signal, NULL);
614
615 out:
616         dbus_message_unref(_signal);
617 }
618 #else /* CONFIG_WPS */
619 void wpa_supplicant_dbus_notify_wps_cred(struct wpa_supplicant *wpa_s,
620                                          const struct wps_credential *cred)
621 {
622 }
623 #endif /* CONFIG_WPS */
624
625
626 /**
627  * wpa_supplicant_dbus_ctrl_iface_init - Initialize dbus control interface
628  * @global: Pointer to global data from wpa_supplicant_init()
629  * Returns: 0 on success, -1 on failure
630  *
631  * Initialize the dbus control interface and start receiving commands from
632  * external programs over the bus.
633  */
634 int wpa_supplicant_dbus_ctrl_iface_init(struct wpas_dbus_priv *iface)
635 {
636         DBusError error;
637         int ret = -1;
638         DBusObjectPathVTable wpas_vtable = {
639                 NULL, &wpas_message_handler, NULL, NULL, NULL, NULL
640         };
641
642         /* Register the message handler for the global dbus interface */
643         if (!dbus_connection_register_object_path(iface->con,
644                                                   WPAS_DBUS_PATH, &wpas_vtable,
645                                                   iface)) {
646                 perror("dbus_connection_register_object_path[dbus]");
647                 wpa_printf(MSG_ERROR, "Could not set up DBus message "
648                            "handler.");
649                 return -1;
650         }
651
652         /* Register our service with the message bus */
653         dbus_error_init(&error);
654         switch (dbus_bus_request_name(iface->con, WPAS_DBUS_SERVICE,
655                                       0, &error)) {
656         case DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER:
657                 ret = 0;
658                 break;
659         case DBUS_REQUEST_NAME_REPLY_EXISTS:
660         case DBUS_REQUEST_NAME_REPLY_IN_QUEUE:
661         case DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER:
662                 perror("dbus_bus_request_name[dbus]");
663                 wpa_printf(MSG_ERROR, "Could not request DBus service name: "
664                            "already registered.");
665                 break;
666         default:
667                 perror("dbus_bus_request_name[dbus]");
668                 wpa_printf(MSG_ERROR, "Could not request DBus service name: "
669                            "%s %s.", error.name, error.message);
670                 break;
671         }
672         dbus_error_free(&error);
673
674         if (ret != 0)
675                 return -1;
676
677         wpa_printf(MSG_DEBUG, "Providing DBus service '" WPAS_DBUS_SERVICE
678                    "'.");
679
680         return 0;
681 }
682
683
684 /**
685  * wpas_dbus_register_new_iface - Register a new interface with dbus
686  * @wpa_s: %wpa_supplicant interface description structure to register
687  * Returns: 0 on success, -1 on error
688  *
689  * Registers a new interface with dbus and assigns it a dbus object path.
690  */
691 int wpas_dbus_register_iface(struct wpa_supplicant *wpa_s)
692 {
693         struct wpas_dbus_priv *ctrl_iface = wpa_s->global->dbus;
694         DBusConnection * con;
695         u32 next;
696         DBusObjectPathVTable vtable = {
697                 NULL, &wpas_iface_message_handler, NULL, NULL, NULL, NULL
698         };
699         char *path;
700         int ret = -1;
701
702         /* Do nothing if the control interface is not turned on */
703         if (ctrl_iface == NULL)
704                 return 0;
705
706         con = ctrl_iface->con;
707         next = ctrl_iface->next_objid++;
708
709         /* Create and set the interface's object path */
710         path = os_zalloc(WPAS_DBUS_OBJECT_PATH_MAX);
711         if (path == NULL)
712                 return -1;
713         snprintf(path, WPAS_DBUS_OBJECT_PATH_MAX,
714                  WPAS_DBUS_PATH_INTERFACES "/%u",
715                  next);
716         if (wpa_supplicant_set_dbus_path(wpa_s, path)) {
717                 wpa_printf(MSG_DEBUG,
718                            "Failed to set dbus path for interface %s",
719                            wpa_s->ifname);
720                 goto out;
721         }
722
723         /* Register the message handler for the interface functions */
724         if (!dbus_connection_register_fallback(con, path, &vtable, wpa_s)) {
725                 perror("wpas_dbus_register_iface [dbus]");
726                 wpa_printf(MSG_ERROR, "Could not set up DBus message "
727                            "handler for interface %s.", wpa_s->ifname);
728                 goto out;
729         }
730         ret = 0;
731
732 out:
733         os_free(path);
734         return ret;
735 }
736
737
738 /**
739  * wpas_dbus_unregister_iface - Unregister an interface from dbus
740  * @wpa_s: wpa_supplicant interface structure
741  * Returns: 0 on success, -1 on failure
742  *
743  * Unregisters the interface with dbus
744  */
745 int wpas_dbus_unregister_iface(struct wpa_supplicant *wpa_s)
746 {
747         struct wpas_dbus_priv *ctrl_iface;
748         DBusConnection *con;
749         const char *path;
750
751         /* Do nothing if the control interface is not turned on */
752         if (wpa_s == NULL || wpa_s->global == NULL)
753                 return 0;
754         ctrl_iface = wpa_s->global->dbus;
755         if (ctrl_iface == NULL)
756                 return 0;
757
758         con = ctrl_iface->con;
759         path = wpa_supplicant_get_dbus_path(wpa_s);
760
761         if (!dbus_connection_unregister_object_path(con, path))
762                 return -1;
763
764         os_free(wpa_s->dbus_path);
765         wpa_s->dbus_path = NULL;
766
767         return 0;
768 }
769
770
771 /**
772  * wpa_supplicant_get_iface_by_dbus_path - Get a new network interface
773  * @global: Pointer to global data from wpa_supplicant_init()
774  * @path: Pointer to a dbus object path representing an interface
775  * Returns: Pointer to the interface or %NULL if not found
776  */
777 struct wpa_supplicant * wpa_supplicant_get_iface_by_dbus_path(
778         struct wpa_global *global, const char *path)
779 {
780         struct wpa_supplicant *wpa_s;
781
782         for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
783                 if (strcmp(wpa_s->dbus_path, path) == 0)
784                         return wpa_s;
785         }
786         return NULL;
787 }
788
789
790 /**
791  * wpa_supplicant_set_dbus_path - Assign a dbus path to an interface
792  * @wpa_s: wpa_supplicant interface structure
793  * @path: dbus path to set on the interface
794  * Returns: 0 on succes, -1 on error
795  */
796 int wpa_supplicant_set_dbus_path(struct wpa_supplicant *wpa_s,
797                                   const char *path)
798 {
799         u32 len = strlen (path);
800         if (len >= WPAS_DBUS_OBJECT_PATH_MAX)
801                 return -1;
802         if (wpa_s->dbus_path)
803                 return -1;
804         wpa_s->dbus_path = os_strdup(path);
805         return 0;
806 }
807
808
809 /**
810  * wpa_supplicant_get_dbus_path - Get an interface's dbus path
811  * @wpa_s: %wpa_supplicant interface structure
812  * Returns: Interface's dbus object path, or %NULL on error
813  */
814 const char * wpa_supplicant_get_dbus_path(struct wpa_supplicant *wpa_s)
815 {
816         return wpa_s->dbus_path;
817 }