dbus_new_helpers.c 31 KB

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