dbus_new_helpers.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061
  1. /*
  2. * WPA Supplicant / dbus-based control interface
  3. * Copyright (c) 2006, Dan Williams <dcbw@redhat.com> and Red Hat, Inc.
  4. * Copyright (c) 2009, Witold Sowa <witold.sowa@gmail.com>
  5. *
  6. * This software may be distributed under the terms of the BSD license.
  7. * See README for more details.
  8. */
  9. #include "utils/includes.h"
  10. #include "utils/common.h"
  11. #include "utils/eloop.h"
  12. #include "dbus_common.h"
  13. #include "dbus_common_i.h"
  14. #include "dbus_new.h"
  15. #include "dbus_new_helpers.h"
  16. #include "dbus_dict_helpers.h"
  17. static dbus_bool_t fill_dict_with_properties(
  18. DBusMessageIter *dict_iter,
  19. const struct wpa_dbus_property_desc *props,
  20. const char *interface, void *user_data, DBusError *error)
  21. {
  22. DBusMessageIter entry_iter;
  23. const struct wpa_dbus_property_desc *dsc;
  24. for (dsc = props; dsc && dsc->dbus_property; dsc++) {
  25. /* Only return properties for the requested D-Bus interface */
  26. if (os_strncmp(dsc->dbus_interface, interface,
  27. WPAS_DBUS_INTERFACE_MAX) != 0)
  28. continue;
  29. /* Skip write-only properties */
  30. if (dsc->getter == NULL)
  31. continue;
  32. if (!dbus_message_iter_open_container(dict_iter,
  33. DBUS_TYPE_DICT_ENTRY,
  34. NULL, &entry_iter)) {
  35. dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY,
  36. "no memory");
  37. return FALSE;
  38. }
  39. if (!dbus_message_iter_append_basic(&entry_iter,
  40. DBUS_TYPE_STRING,
  41. &dsc->dbus_property)) {
  42. dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY,
  43. "no memory");
  44. return FALSE;
  45. }
  46. /* An error getting a property fails the request entirely */
  47. if (!dsc->getter(&entry_iter, error, user_data))
  48. return FALSE;
  49. dbus_message_iter_close_container(dict_iter, &entry_iter);
  50. }
  51. return TRUE;
  52. }
  53. /**
  54. * get_all_properties - Responds for GetAll properties calls on object
  55. * @message: Message with GetAll call
  56. * @interface: interface name which properties will be returned
  57. * @property_dsc: list of object's properties
  58. * Returns: Message with dict of variants as argument with properties values
  59. *
  60. * Iterates over all properties registered with object and execute getters
  61. * of those, which are readable and which interface matches interface
  62. * specified as argument. Returned message contains one dict argument
  63. * with properties names as keys and theirs values as values.
  64. */
  65. static DBusMessage * get_all_properties(DBusMessage *message, char *interface,
  66. struct wpa_dbus_object_desc *obj_dsc)
  67. {
  68. DBusMessage *reply;
  69. DBusMessageIter iter, dict_iter;
  70. DBusError error;
  71. reply = dbus_message_new_method_return(message);
  72. if (reply == NULL) {
  73. wpa_printf(MSG_ERROR, "%s: out of memory creating dbus reply",
  74. __func__);
  75. return NULL;
  76. }
  77. dbus_message_iter_init_append(reply, &iter);
  78. if (!wpa_dbus_dict_open_write(&iter, &dict_iter)) {
  79. wpa_printf(MSG_ERROR, "%s: out of memory creating reply",
  80. __func__);
  81. dbus_message_unref(reply);
  82. reply = dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
  83. "out of memory");
  84. return reply;
  85. }
  86. dbus_error_init(&error);
  87. if (!fill_dict_with_properties(&dict_iter, obj_dsc->properties,
  88. interface, obj_dsc->user_data, &error))
  89. {
  90. dbus_message_unref(reply);
  91. reply = wpas_dbus_reply_new_from_error(message, &error,
  92. DBUS_ERROR_INVALID_ARGS,
  93. "No readable properties"
  94. " in this interface");
  95. dbus_error_free(&error);
  96. return reply;
  97. }
  98. wpa_dbus_dict_close_write(&iter, &dict_iter);
  99. return reply;
  100. }
  101. static int is_signature_correct(DBusMessage *message,
  102. const struct wpa_dbus_method_desc *method_dsc)
  103. {
  104. /* According to DBus documentation max length of signature is 255 */
  105. #define MAX_SIG_LEN 256
  106. char registered_sig[MAX_SIG_LEN], *pos;
  107. const char *sig = dbus_message_get_signature(message);
  108. int ret;
  109. const struct wpa_dbus_argument *arg;
  110. pos = registered_sig;
  111. *pos = '\0';
  112. for (arg = method_dsc->args; arg && arg->name; arg++) {
  113. if (arg->dir == ARG_IN) {
  114. size_t blen = registered_sig + MAX_SIG_LEN - pos;
  115. ret = os_snprintf(pos, blen, "%s", arg->type);
  116. if (ret < 0 || (size_t) ret >= blen)
  117. return 0;
  118. pos += ret;
  119. }
  120. }
  121. return !os_strncmp(registered_sig, sig, MAX_SIG_LEN);
  122. }
  123. static DBusMessage * properties_get_all(DBusMessage *message, char *interface,
  124. struct wpa_dbus_object_desc *obj_dsc)
  125. {
  126. if (os_strcmp(dbus_message_get_signature(message), "s") != 0)
  127. return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
  128. NULL);
  129. return get_all_properties(message, interface, obj_dsc);
  130. }
  131. static DBusMessage * properties_get(DBusMessage *message,
  132. const struct wpa_dbus_property_desc *dsc,
  133. void *user_data)
  134. {
  135. DBusMessage *reply;
  136. DBusMessageIter iter;
  137. DBusError error;
  138. if (os_strcmp(dbus_message_get_signature(message), "ss")) {
  139. return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
  140. NULL);
  141. }
  142. if (dsc->getter == NULL) {
  143. return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
  144. "Property is write-only");
  145. }
  146. reply = dbus_message_new_method_return(message);
  147. dbus_message_iter_init_append(reply, &iter);
  148. dbus_error_init(&error);
  149. if (dsc->getter(&iter, &error, user_data) == FALSE) {
  150. dbus_message_unref(reply);
  151. reply = wpas_dbus_reply_new_from_error(
  152. message, &error, DBUS_ERROR_FAILED,
  153. "Failed to read property");
  154. dbus_error_free(&error);
  155. }
  156. return reply;
  157. }
  158. static DBusMessage * properties_set(DBusMessage *message,
  159. const struct wpa_dbus_property_desc *dsc,
  160. void *user_data)
  161. {
  162. DBusMessage *reply;
  163. DBusMessageIter iter;
  164. DBusError error;
  165. if (os_strcmp(dbus_message_get_signature(message), "ssv")) {
  166. return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
  167. NULL);
  168. }
  169. if (dsc->setter == NULL) {
  170. return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
  171. "Property is read-only");
  172. }
  173. dbus_message_iter_init(message, &iter);
  174. /* Skip the interface name and the property name */
  175. dbus_message_iter_next(&iter);
  176. dbus_message_iter_next(&iter);
  177. /* Iter will now point to the property's new value */
  178. dbus_error_init(&error);
  179. if (dsc->setter(&iter, &error, user_data) == TRUE) {
  180. /* Success */
  181. reply = dbus_message_new_method_return(message);
  182. } else {
  183. reply = wpas_dbus_reply_new_from_error(
  184. message, &error, DBUS_ERROR_FAILED,
  185. "Failed to set property");
  186. dbus_error_free(&error);
  187. }
  188. return reply;
  189. }
  190. static DBusMessage *
  191. properties_get_or_set(DBusMessage *message, DBusMessageIter *iter,
  192. char *interface,
  193. struct wpa_dbus_object_desc *obj_dsc)
  194. {
  195. const struct wpa_dbus_property_desc *property_dsc;
  196. char *property;
  197. const char *method;
  198. method = dbus_message_get_member(message);
  199. property_dsc = obj_dsc->properties;
  200. /* Second argument: property name (DBUS_TYPE_STRING) */
  201. if (!dbus_message_iter_next(iter) ||
  202. dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_STRING) {
  203. return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
  204. NULL);
  205. }
  206. dbus_message_iter_get_basic(iter, &property);
  207. while (property_dsc && property_dsc->dbus_property) {
  208. /* compare property names and
  209. * interfaces */
  210. if (!os_strncmp(property_dsc->dbus_property, property,
  211. WPAS_DBUS_METHOD_SIGNAL_PROP_MAX) &&
  212. !os_strncmp(property_dsc->dbus_interface, interface,
  213. WPAS_DBUS_INTERFACE_MAX))
  214. break;
  215. property_dsc++;
  216. }
  217. if (property_dsc == NULL || property_dsc->dbus_property == NULL) {
  218. wpa_printf(MSG_DEBUG, "no property handler for %s.%s on %s",
  219. interface, property,
  220. dbus_message_get_path(message));
  221. return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
  222. "No such property");
  223. }
  224. if (os_strncmp(WPA_DBUS_PROPERTIES_GET, method,
  225. WPAS_DBUS_METHOD_SIGNAL_PROP_MAX) == 0)
  226. return properties_get(message, property_dsc,
  227. obj_dsc->user_data);
  228. return properties_set(message, property_dsc, obj_dsc->user_data);
  229. }
  230. static DBusMessage * properties_handler(DBusMessage *message,
  231. struct wpa_dbus_object_desc *obj_dsc)
  232. {
  233. DBusMessageIter iter;
  234. char *interface;
  235. const char *method;
  236. method = dbus_message_get_member(message);
  237. dbus_message_iter_init(message, &iter);
  238. if (!os_strncmp(WPA_DBUS_PROPERTIES_GET, method,
  239. WPAS_DBUS_METHOD_SIGNAL_PROP_MAX) ||
  240. !os_strncmp(WPA_DBUS_PROPERTIES_SET, method,
  241. WPAS_DBUS_METHOD_SIGNAL_PROP_MAX) ||
  242. !os_strncmp(WPA_DBUS_PROPERTIES_GETALL, method,
  243. WPAS_DBUS_METHOD_SIGNAL_PROP_MAX)) {
  244. /* First argument: interface name (DBUS_TYPE_STRING) */
  245. if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
  246. {
  247. return dbus_message_new_error(message,
  248. DBUS_ERROR_INVALID_ARGS,
  249. NULL);
  250. }
  251. dbus_message_iter_get_basic(&iter, &interface);
  252. if (!os_strncmp(WPA_DBUS_PROPERTIES_GETALL, method,
  253. WPAS_DBUS_METHOD_SIGNAL_PROP_MAX)) {
  254. /* GetAll */
  255. return properties_get_all(message, interface, obj_dsc);
  256. }
  257. /* Get or Set */
  258. return properties_get_or_set(message, &iter, interface,
  259. obj_dsc);
  260. }
  261. return dbus_message_new_error(message, DBUS_ERROR_UNKNOWN_METHOD,
  262. NULL);
  263. }
  264. static DBusMessage * msg_method_handler(DBusMessage *message,
  265. struct wpa_dbus_object_desc *obj_dsc)
  266. {
  267. const struct wpa_dbus_method_desc *method_dsc = obj_dsc->methods;
  268. const char *method;
  269. const char *msg_interface;
  270. method = dbus_message_get_member(message);
  271. msg_interface = dbus_message_get_interface(message);
  272. /* try match call to any registered method */
  273. while (method_dsc && method_dsc->dbus_method) {
  274. /* compare method names and interfaces */
  275. if (!os_strncmp(method_dsc->dbus_method, method,
  276. WPAS_DBUS_METHOD_SIGNAL_PROP_MAX) &&
  277. !os_strncmp(method_dsc->dbus_interface, msg_interface,
  278. WPAS_DBUS_INTERFACE_MAX))
  279. break;
  280. method_dsc++;
  281. }
  282. if (method_dsc == NULL || method_dsc->dbus_method == NULL) {
  283. wpa_printf(MSG_DEBUG, "no method handler for %s.%s on %s",
  284. msg_interface, method,
  285. dbus_message_get_path(message));
  286. return dbus_message_new_error(message,
  287. DBUS_ERROR_UNKNOWN_METHOD, NULL);
  288. }
  289. if (!is_signature_correct(message, method_dsc)) {
  290. return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
  291. NULL);
  292. }
  293. return method_dsc->method_handler(message,
  294. obj_dsc->user_data);
  295. }
  296. /**
  297. * message_handler - Handles incoming DBus messages
  298. * @connection: DBus connection on which message was received
  299. * @message: Received message
  300. * @user_data: pointer to description of object to which message was sent
  301. * Returns: Returns information whether message was handled or not
  302. *
  303. * Reads message interface and method name, then checks if they matches one
  304. * of the special cases i.e. introspection call or properties get/getall/set
  305. * methods and handles it. Else it iterates over registered methods list
  306. * and tries to match method's name and interface to those read from message
  307. * If appropriate method was found its handler function is called and
  308. * response is sent. Otherwise, the DBUS_ERROR_UNKNOWN_METHOD error message
  309. * will be sent.
  310. */
  311. static DBusHandlerResult message_handler(DBusConnection *connection,
  312. DBusMessage *message, void *user_data)
  313. {
  314. struct wpa_dbus_object_desc *obj_dsc = user_data;
  315. const char *method;
  316. const char *path;
  317. const char *msg_interface;
  318. DBusMessage *reply;
  319. /* get method, interface and path the message is addressed to */
  320. method = dbus_message_get_member(message);
  321. path = dbus_message_get_path(message);
  322. msg_interface = dbus_message_get_interface(message);
  323. if (!method || !path || !msg_interface)
  324. return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
  325. wpa_printf(MSG_MSGDUMP, "dbus: %s.%s (%s)",
  326. msg_interface, method, path);
  327. /* if message is introspection method call */
  328. if (!os_strncmp(WPA_DBUS_INTROSPECTION_METHOD, method,
  329. WPAS_DBUS_METHOD_SIGNAL_PROP_MAX) &&
  330. !os_strncmp(WPA_DBUS_INTROSPECTION_INTERFACE, msg_interface,
  331. WPAS_DBUS_INTERFACE_MAX)) {
  332. #ifdef CONFIG_CTRL_IFACE_DBUS_INTRO
  333. reply = wpa_dbus_introspect(message, obj_dsc);
  334. #else /* CONFIG_CTRL_IFACE_DBUS_INTRO */
  335. reply = dbus_message_new_error(
  336. message, DBUS_ERROR_UNKNOWN_METHOD,
  337. "wpa_supplicant was compiled without "
  338. "introspection support.");
  339. #endif /* CONFIG_CTRL_IFACE_DBUS_INTRO */
  340. } else if (!os_strncmp(WPA_DBUS_PROPERTIES_INTERFACE, msg_interface,
  341. WPAS_DBUS_INTERFACE_MAX)) {
  342. /* if message is properties method call */
  343. reply = properties_handler(message, obj_dsc);
  344. } else {
  345. reply = msg_method_handler(message, obj_dsc);
  346. }
  347. /* If handler succeed returning NULL, reply empty message */
  348. if (!reply)
  349. reply = dbus_message_new_method_return(message);
  350. if (reply) {
  351. if (!dbus_message_get_no_reply(message))
  352. dbus_connection_send(connection, reply, NULL);
  353. dbus_message_unref(reply);
  354. }
  355. wpa_dbus_flush_all_changed_properties(connection);
  356. return DBUS_HANDLER_RESULT_HANDLED;
  357. }
  358. /**
  359. * free_dbus_object_desc - Frees object description data structure
  360. * @connection: DBus connection
  361. * @obj_dsc: Object description to free
  362. *
  363. * Frees each of properties, methods and signals description lists and
  364. * the object description structure itself.
  365. */
  366. void free_dbus_object_desc(struct wpa_dbus_object_desc *obj_dsc)
  367. {
  368. if (!obj_dsc)
  369. return;
  370. /* free handler's argument */
  371. if (obj_dsc->user_data_free_func)
  372. obj_dsc->user_data_free_func(obj_dsc->user_data);
  373. os_free(obj_dsc->path);
  374. os_free(obj_dsc->prop_changed_flags);
  375. os_free(obj_dsc);
  376. }
  377. static void free_dbus_object_desc_cb(DBusConnection *connection, void *obj_dsc)
  378. {
  379. free_dbus_object_desc(obj_dsc);
  380. }
  381. /**
  382. * wpa_dbus_ctrl_iface_init - Initialize dbus control interface
  383. * @application_data: Pointer to application specific data structure
  384. * @dbus_path: DBus path to interface object
  385. * @dbus_service: DBus service name to register with
  386. * @messageHandler: a pointer to function which will handle dbus messages
  387. * coming on interface
  388. * Returns: 0 on success, -1 on failure
  389. *
  390. * Initialize the dbus control interface and start receiving commands from
  391. * external programs over the bus.
  392. */
  393. int wpa_dbus_ctrl_iface_init(struct wpas_dbus_priv *iface,
  394. char *dbus_path, char *dbus_service,
  395. struct wpa_dbus_object_desc *obj_desc)
  396. {
  397. DBusError error;
  398. int ret = -1;
  399. DBusObjectPathVTable wpa_vtable = {
  400. &free_dbus_object_desc_cb, &message_handler,
  401. NULL, NULL, NULL, NULL
  402. };
  403. obj_desc->connection = iface->con;
  404. obj_desc->path = os_strdup(dbus_path);
  405. /* Register the message handler for the global dbus interface */
  406. if (!dbus_connection_register_object_path(iface->con,
  407. dbus_path, &wpa_vtable,
  408. obj_desc)) {
  409. wpa_printf(MSG_ERROR, "dbus: Could not set up message "
  410. "handler");
  411. return -1;
  412. }
  413. /* Register our service with the message bus */
  414. dbus_error_init(&error);
  415. switch (dbus_bus_request_name(iface->con, dbus_service,
  416. 0, &error)) {
  417. case DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER:
  418. ret = 0;
  419. break;
  420. case DBUS_REQUEST_NAME_REPLY_EXISTS:
  421. case DBUS_REQUEST_NAME_REPLY_IN_QUEUE:
  422. case DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER:
  423. wpa_printf(MSG_ERROR, "dbus: Could not request service name: "
  424. "already registered");
  425. break;
  426. default:
  427. wpa_printf(MSG_ERROR, "dbus: Could not request service name: "
  428. "%s %s", error.name, error.message);
  429. break;
  430. }
  431. dbus_error_free(&error);
  432. if (ret != 0)
  433. return -1;
  434. wpa_printf(MSG_DEBUG, "Providing DBus service '%s'.", dbus_service);
  435. return 0;
  436. }
  437. /**
  438. * wpa_dbus_register_object_per_iface - Register a new object with dbus
  439. * @ctrl_iface: pointer to dbus private data
  440. * @path: DBus path to object
  441. * @ifname: interface name
  442. * @obj_desc: description of object's methods, signals and properties
  443. * Returns: 0 on success, -1 on error
  444. *
  445. * Registers a new interface with dbus and assigns it a dbus object path.
  446. */
  447. int wpa_dbus_register_object_per_iface(
  448. struct wpas_dbus_priv *ctrl_iface,
  449. const char *path, const char *ifname,
  450. struct wpa_dbus_object_desc *obj_desc)
  451. {
  452. DBusConnection *con;
  453. DBusError error;
  454. DBusObjectPathVTable vtable = {
  455. &free_dbus_object_desc_cb, &message_handler,
  456. NULL, NULL, NULL, NULL
  457. };
  458. /* Do nothing if the control interface is not turned on */
  459. if (ctrl_iface == NULL)
  460. return 0;
  461. con = ctrl_iface->con;
  462. obj_desc->connection = con;
  463. obj_desc->path = os_strdup(path);
  464. dbus_error_init(&error);
  465. /* Register the message handler for the interface functions */
  466. if (!dbus_connection_try_register_object_path(con, path, &vtable,
  467. obj_desc, &error)) {
  468. if (!os_strcmp(error.name, DBUS_ERROR_OBJECT_PATH_IN_USE)) {
  469. wpa_printf(MSG_DEBUG, "dbus: %s", error.message);
  470. } else {
  471. wpa_printf(MSG_ERROR, "dbus: Could not set up message "
  472. "handler for interface %s object %s",
  473. ifname, path);
  474. wpa_printf(MSG_ERROR, "dbus error: %s", error.name);
  475. wpa_printf(MSG_ERROR, "dbus: %s", error.message);
  476. }
  477. dbus_error_free(&error);
  478. return -1;
  479. }
  480. dbus_error_free(&error);
  481. return 0;
  482. }
  483. static void flush_object_timeout_handler(void *eloop_ctx, void *timeout_ctx);
  484. /**
  485. * wpa_dbus_unregister_object_per_iface - Unregisters DBus object
  486. * @ctrl_iface: Pointer to dbus private data
  487. * @path: DBus path to object which will be unregistered
  488. * Returns: Zero on success and -1 on failure
  489. *
  490. * Unregisters DBus object given by its path
  491. */
  492. int wpa_dbus_unregister_object_per_iface(
  493. struct wpas_dbus_priv *ctrl_iface, const char *path)
  494. {
  495. DBusConnection *con = ctrl_iface->con;
  496. struct wpa_dbus_object_desc *obj_desc = NULL;
  497. dbus_connection_get_object_path_data(con, path, (void **) &obj_desc);
  498. if (!obj_desc) {
  499. wpa_printf(MSG_ERROR, "dbus: %s: Could not obtain object's "
  500. "private data: %s", __func__, path);
  501. } else {
  502. eloop_cancel_timeout(flush_object_timeout_handler, con,
  503. obj_desc);
  504. }
  505. if (!dbus_connection_unregister_object_path(con, path))
  506. return -1;
  507. return 0;
  508. }
  509. static dbus_bool_t put_changed_properties(
  510. const struct wpa_dbus_object_desc *obj_dsc, const char *interface,
  511. DBusMessageIter *dict_iter, int clear_changed)
  512. {
  513. DBusMessageIter entry_iter;
  514. const struct wpa_dbus_property_desc *dsc;
  515. int i;
  516. DBusError error;
  517. for (dsc = obj_dsc->properties, i = 0; dsc && dsc->dbus_property;
  518. dsc++, i++) {
  519. if (obj_dsc->prop_changed_flags == NULL ||
  520. !obj_dsc->prop_changed_flags[i])
  521. continue;
  522. if (os_strcmp(dsc->dbus_interface, interface) != 0)
  523. continue;
  524. if (clear_changed)
  525. obj_dsc->prop_changed_flags[i] = 0;
  526. if (!dbus_message_iter_open_container(dict_iter,
  527. DBUS_TYPE_DICT_ENTRY,
  528. NULL, &entry_iter))
  529. return FALSE;
  530. if (!dbus_message_iter_append_basic(&entry_iter,
  531. DBUS_TYPE_STRING,
  532. &dsc->dbus_property))
  533. return FALSE;
  534. dbus_error_init(&error);
  535. if (!dsc->getter(&entry_iter, &error, obj_dsc->user_data)) {
  536. if (dbus_error_is_set (&error)) {
  537. wpa_printf(MSG_ERROR, "dbus: %s: Cannot get "
  538. "new value of property %s: (%s) %s",
  539. __func__, dsc->dbus_property,
  540. error.name, error.message);
  541. } else {
  542. wpa_printf(MSG_ERROR, "dbus: %s: Cannot get "
  543. "new value of property %s",
  544. __func__, dsc->dbus_property);
  545. }
  546. dbus_error_free(&error);
  547. return FALSE;
  548. }
  549. if (!dbus_message_iter_close_container(dict_iter, &entry_iter))
  550. return FALSE;
  551. }
  552. return TRUE;
  553. }
  554. static void do_send_prop_changed_signal(
  555. DBusConnection *con, const char *path, const char *interface,
  556. const struct wpa_dbus_object_desc *obj_dsc)
  557. {
  558. DBusMessage *msg;
  559. DBusMessageIter signal_iter, dict_iter;
  560. msg = dbus_message_new_signal(path, DBUS_INTERFACE_PROPERTIES,
  561. "PropertiesChanged");
  562. if (msg == NULL)
  563. return;
  564. dbus_message_iter_init_append(msg, &signal_iter);
  565. if (!dbus_message_iter_append_basic(&signal_iter, DBUS_TYPE_STRING,
  566. &interface))
  567. goto err;
  568. /* Changed properties dict */
  569. if (!dbus_message_iter_open_container(&signal_iter, DBUS_TYPE_ARRAY,
  570. "{sv}", &dict_iter))
  571. goto err;
  572. if (!put_changed_properties(obj_dsc, interface, &dict_iter, 0))
  573. goto err;
  574. if (!dbus_message_iter_close_container(&signal_iter, &dict_iter))
  575. goto err;
  576. /* Invalidated properties array (empty) */
  577. if (!dbus_message_iter_open_container(&signal_iter, DBUS_TYPE_ARRAY,
  578. "s", &dict_iter))
  579. goto err;
  580. if (!dbus_message_iter_close_container(&signal_iter, &dict_iter))
  581. goto err;
  582. dbus_connection_send(con, msg, NULL);
  583. out:
  584. dbus_message_unref(msg);
  585. return;
  586. err:
  587. wpa_printf(MSG_DEBUG, "dbus: %s: Failed to construct signal",
  588. __func__);
  589. goto out;
  590. }
  591. static void do_send_deprecated_prop_changed_signal(
  592. DBusConnection *con, const char *path, const char *interface,
  593. const struct wpa_dbus_object_desc *obj_dsc)
  594. {
  595. DBusMessage *msg;
  596. DBusMessageIter signal_iter, dict_iter;
  597. msg = dbus_message_new_signal(path, interface, "PropertiesChanged");
  598. if (msg == NULL)
  599. return;
  600. dbus_message_iter_init_append(msg, &signal_iter);
  601. if (!dbus_message_iter_open_container(&signal_iter, DBUS_TYPE_ARRAY,
  602. "{sv}", &dict_iter))
  603. goto err;
  604. if (!put_changed_properties(obj_dsc, interface, &dict_iter, 1))
  605. goto err;
  606. if (!dbus_message_iter_close_container(&signal_iter, &dict_iter))
  607. goto err;
  608. dbus_connection_send(con, msg, NULL);
  609. out:
  610. dbus_message_unref(msg);
  611. return;
  612. err:
  613. wpa_printf(MSG_DEBUG, "dbus: %s: Failed to construct signal",
  614. __func__);
  615. goto out;
  616. }
  617. static void send_prop_changed_signal(
  618. DBusConnection *con, const char *path, const char *interface,
  619. const struct wpa_dbus_object_desc *obj_dsc)
  620. {
  621. /*
  622. * First, send property change notification on the standardized
  623. * org.freedesktop.DBus.Properties interface. This call will not
  624. * clear the property change bits, so that they are preserved for
  625. * the call that follows.
  626. */
  627. do_send_prop_changed_signal(con, path, interface, obj_dsc);
  628. /*
  629. * Now send PropertiesChanged on our own interface for backwards
  630. * compatibility. This is deprecated and will be removed in a future
  631. * release.
  632. */
  633. do_send_deprecated_prop_changed_signal(con, path, interface, obj_dsc);
  634. /* Property change bits have now been cleared. */
  635. }
  636. static void flush_object_timeout_handler(void *eloop_ctx, void *timeout_ctx)
  637. {
  638. DBusConnection *con = eloop_ctx;
  639. struct wpa_dbus_object_desc *obj_desc = timeout_ctx;
  640. wpa_printf(MSG_DEBUG, "dbus: %s: Timeout - sending changed properties "
  641. "of object %s", __func__, obj_desc->path);
  642. wpa_dbus_flush_object_changed_properties(con, obj_desc->path);
  643. }
  644. static void recursive_flush_changed_properties(DBusConnection *con,
  645. const char *path)
  646. {
  647. char **objects = NULL;
  648. char subobj_path[WPAS_DBUS_OBJECT_PATH_MAX];
  649. int i;
  650. wpa_dbus_flush_object_changed_properties(con, path);
  651. if (!dbus_connection_list_registered(con, path, &objects))
  652. goto out;
  653. for (i = 0; objects[i]; i++) {
  654. os_snprintf(subobj_path, WPAS_DBUS_OBJECT_PATH_MAX,
  655. "%s/%s", path, objects[i]);
  656. recursive_flush_changed_properties(con, subobj_path);
  657. }
  658. out:
  659. dbus_free_string_array(objects);
  660. }
  661. /**
  662. * wpa_dbus_flush_all_changed_properties - Send all PropertiesChanged signals
  663. * @con: DBus connection
  664. *
  665. * Traverses through all registered objects and sends PropertiesChanged for
  666. * each properties.
  667. */
  668. void wpa_dbus_flush_all_changed_properties(DBusConnection *con)
  669. {
  670. recursive_flush_changed_properties(con, WPAS_DBUS_NEW_PATH);
  671. }
  672. /**
  673. * wpa_dbus_flush_object_changed_properties - Send PropertiesChanged for object
  674. * @con: DBus connection
  675. * @path: path to a DBus object for which PropertiesChanged will be sent.
  676. *
  677. * Iterates over all properties registered with object and for each interface
  678. * containing properties marked as changed, sends a PropertiesChanged signal
  679. * containing names and new values of properties that have changed.
  680. *
  681. * You need to call this function after wpa_dbus_mark_property_changed()
  682. * if you want to send PropertiesChanged signal immediately (i.e., without
  683. * waiting timeout to expire). PropertiesChanged signal for an object is sent
  684. * automatically short time after first marking property as changed. All
  685. * PropertiesChanged signals are sent automatically after responding on DBus
  686. * message, so if you marked a property changed as a result of DBus call
  687. * (e.g., param setter), you usually do not need to call this function.
  688. */
  689. void wpa_dbus_flush_object_changed_properties(DBusConnection *con,
  690. const char *path)
  691. {
  692. struct wpa_dbus_object_desc *obj_desc = NULL;
  693. const struct wpa_dbus_property_desc *dsc;
  694. int i;
  695. dbus_connection_get_object_path_data(con, path, (void **) &obj_desc);
  696. if (!obj_desc)
  697. return;
  698. eloop_cancel_timeout(flush_object_timeout_handler, con, obj_desc);
  699. dsc = obj_desc->properties;
  700. for (dsc = obj_desc->properties, i = 0; dsc && dsc->dbus_property;
  701. dsc++, i++) {
  702. if (obj_desc->prop_changed_flags == NULL ||
  703. !obj_desc->prop_changed_flags[i])
  704. continue;
  705. send_prop_changed_signal(con, path, dsc->dbus_interface,
  706. obj_desc);
  707. }
  708. }
  709. #define WPA_DBUS_SEND_PROP_CHANGED_TIMEOUT 5000
  710. /**
  711. * wpa_dbus_mark_property_changed - Mark a property as changed and
  712. * @iface: dbus priv struct
  713. * @path: path to DBus object which property has changed
  714. * @interface: interface containing changed property
  715. * @property: property name which has changed
  716. *
  717. * Iterates over all properties registered with an object and marks the one
  718. * given in parameters as changed. All parameters registered for an object
  719. * within a single interface will be aggregated together and sent in one
  720. * PropertiesChanged signal when function
  721. * wpa_dbus_flush_object_changed_properties() is called.
  722. */
  723. void wpa_dbus_mark_property_changed(struct wpas_dbus_priv *iface,
  724. const char *path, const char *interface,
  725. const char *property)
  726. {
  727. struct wpa_dbus_object_desc *obj_desc = NULL;
  728. const struct wpa_dbus_property_desc *dsc;
  729. int i = 0;
  730. if (iface == NULL)
  731. return;
  732. dbus_connection_get_object_path_data(iface->con, path,
  733. (void **) &obj_desc);
  734. if (!obj_desc) {
  735. wpa_printf(MSG_ERROR, "dbus: wpa_dbus_property_changed: "
  736. "could not obtain object's private data: %s", path);
  737. return;
  738. }
  739. for (dsc = obj_desc->properties; dsc && dsc->dbus_property; dsc++, i++)
  740. if (os_strcmp(property, dsc->dbus_property) == 0 &&
  741. os_strcmp(interface, dsc->dbus_interface) == 0) {
  742. if (obj_desc->prop_changed_flags)
  743. obj_desc->prop_changed_flags[i] = 1;
  744. break;
  745. }
  746. if (!dsc || !dsc->dbus_property) {
  747. wpa_printf(MSG_ERROR, "dbus: wpa_dbus_property_changed: "
  748. "no property %s in object %s", property, path);
  749. return;
  750. }
  751. if (!eloop_is_timeout_registered(flush_object_timeout_handler,
  752. iface->con, obj_desc->path)) {
  753. eloop_register_timeout(0, WPA_DBUS_SEND_PROP_CHANGED_TIMEOUT,
  754. flush_object_timeout_handler,
  755. iface->con, obj_desc);
  756. }
  757. }
  758. /**
  759. * wpa_dbus_get_object_properties - Put object's properties into dictionary
  760. * @iface: dbus priv struct
  761. * @path: path to DBus object which properties will be obtained
  762. * @interface: interface name which properties will be obtained
  763. * @iter: DBus message iter at which to append property dictionary.
  764. *
  765. * Iterates over all properties registered with object and execute getters
  766. * of those, which are readable and which interface matches interface
  767. * specified as argument. Obtained properties values are stored in
  768. * dict_iter dictionary.
  769. */
  770. dbus_bool_t wpa_dbus_get_object_properties(struct wpas_dbus_priv *iface,
  771. const char *path,
  772. const char *interface,
  773. DBusMessageIter *iter)
  774. {
  775. struct wpa_dbus_object_desc *obj_desc = NULL;
  776. DBusMessageIter dict_iter;
  777. DBusError error;
  778. dbus_connection_get_object_path_data(iface->con, path,
  779. (void **) &obj_desc);
  780. if (!obj_desc) {
  781. wpa_printf(MSG_ERROR, "dbus: %s: could not obtain object's "
  782. "private data: %s", __func__, path);
  783. return FALSE;
  784. }
  785. if (!wpa_dbus_dict_open_write(iter, &dict_iter)) {
  786. wpa_printf(MSG_ERROR, "dbus: %s: failed to open message dict",
  787. __func__);
  788. return FALSE;
  789. }
  790. dbus_error_init(&error);
  791. if (!fill_dict_with_properties(&dict_iter, obj_desc->properties,
  792. interface, obj_desc->user_data,
  793. &error)) {
  794. wpa_printf(MSG_ERROR, "dbus: %s: failed to get object"
  795. " properties: (%s) %s", __func__,
  796. dbus_error_is_set(&error) ? error.name : "none",
  797. dbus_error_is_set(&error) ? error.message : "none");
  798. dbus_error_free(&error);
  799. return FALSE;
  800. }
  801. return wpa_dbus_dict_close_write(iter, &dict_iter);
  802. }
  803. /**
  804. * wpas_dbus_new_decompose_object_path - Decompose an interface object path into parts
  805. * @path: The dbus object path
  806. * @p2p_persistent_group: indicates whether to parse the path as a P2P
  807. * persistent group object
  808. * @network: (out) the configured network this object path refers to, if any
  809. * @bssid: (out) the scanned bssid this object path refers to, if any
  810. * Returns: The object path of the network interface this path refers to
  811. *
  812. * For a given object path, decomposes the object path into object id, network,
  813. * and BSSID parts, if those parts exist.
  814. */
  815. char *wpas_dbus_new_decompose_object_path(const char *path,
  816. int p2p_persistent_group,
  817. char **network,
  818. char **bssid)
  819. {
  820. const unsigned int dev_path_prefix_len =
  821. os_strlen(WPAS_DBUS_NEW_PATH_INTERFACES "/");
  822. char *obj_path_only;
  823. char *next_sep;
  824. /* Be a bit paranoid about path */
  825. if (!path || os_strncmp(path, WPAS_DBUS_NEW_PATH_INTERFACES "/",
  826. dev_path_prefix_len))
  827. return NULL;
  828. /* Ensure there's something at the end of the path */
  829. if ((path + dev_path_prefix_len)[0] == '\0')
  830. return NULL;
  831. obj_path_only = os_strdup(path);
  832. if (obj_path_only == NULL)
  833. return NULL;
  834. next_sep = os_strchr(obj_path_only + dev_path_prefix_len, '/');
  835. if (next_sep != NULL) {
  836. const char *net_part = os_strstr(
  837. next_sep, p2p_persistent_group ?
  838. WPAS_DBUS_NEW_PERSISTENT_GROUPS_PART "/" :
  839. WPAS_DBUS_NEW_NETWORKS_PART "/");
  840. const char *bssid_part = os_strstr(
  841. next_sep, WPAS_DBUS_NEW_BSSIDS_PART "/");
  842. if (network && net_part) {
  843. /* Deal with a request for a configured network */
  844. const char *net_name = net_part +
  845. os_strlen(p2p_persistent_group ?
  846. WPAS_DBUS_NEW_PERSISTENT_GROUPS_PART
  847. "/" :
  848. WPAS_DBUS_NEW_NETWORKS_PART "/");
  849. *network = NULL;
  850. if (os_strlen(net_name))
  851. *network = os_strdup(net_name);
  852. } else if (bssid && bssid_part) {
  853. /* Deal with a request for a scanned BSSID */
  854. const char *bssid_name = bssid_part +
  855. os_strlen(WPAS_DBUS_NEW_BSSIDS_PART "/");
  856. if (os_strlen(bssid_name))
  857. *bssid = os_strdup(bssid_name);
  858. else
  859. *bssid = NULL;
  860. }
  861. /* Cut off interface object path before "/" */
  862. *next_sep = '\0';
  863. }
  864. return obj_path_only;
  865. }
  866. /**
  867. * wpas_dbus_reply_new_from_error - Create a new D-Bus error message from a
  868. * dbus error structure
  869. * @message: The original request message for which the error is a reply
  870. * @error: The error containing a name and a descriptive error cause
  871. * @fallback_name: A generic error name if @error was not set
  872. * @fallback_string: A generic error string if @error was not set
  873. * Returns: A new D-Bus error message
  874. *
  875. * Given a DBusMessage structure, creates a new D-Bus error message using
  876. * the error name and string contained in that structure.
  877. */
  878. DBusMessage * wpas_dbus_reply_new_from_error(DBusMessage *message,
  879. DBusError *error,
  880. const char *fallback_name,
  881. const char *fallback_string)
  882. {
  883. if (error && error->name && error->message) {
  884. return dbus_message_new_error(message, error->name,
  885. error->message);
  886. }
  887. if (fallback_name && fallback_string) {
  888. return dbus_message_new_error(message, fallback_name,
  889. fallback_string);
  890. }
  891. return NULL;
  892. }