mosquitto_plugin.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. /*
  2. Copyright (c) 2012-2020 Roger Light <roger@atchoo.org>
  3. All rights reserved. This program and the accompanying materials
  4. are made available under the terms of the Eclipse Public License 2.0
  5. and Eclipse Distribution License v1.0 which accompany this distribution.
  6. The Eclipse Public License is available at
  7. https://www.eclipse.org/legal/epl-2.0/
  8. and the Eclipse Distribution License is available at
  9. http://www.eclipse.org/org/documents/edl-v10.php.
  10. SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
  11. Contributors:
  12. Roger Light - initial implementation and documentation.
  13. */
  14. #ifndef MOSQUITTO_PLUGIN_H
  15. #define MOSQUITTO_PLUGIN_H
  16. /*
  17. * File: mosquitto_plugin.h
  18. *
  19. * This header contains function declarations for use when writing a Mosquitto plugin.
  20. */
  21. #ifdef __cplusplus
  22. extern "C" {
  23. #endif
  24. /* The generic plugin interface starts at version 5 */
  25. #define MOSQ_PLUGIN_VERSION 5
  26. /* The old auth only interface stopped at version 4 */
  27. #define MOSQ_AUTH_PLUGIN_VERSION 4
  28. #define MOSQ_ACL_NONE 0x00
  29. #define MOSQ_ACL_READ 0x01
  30. #define MOSQ_ACL_WRITE 0x02
  31. #define MOSQ_ACL_SUBSCRIBE 0x04
  32. #define MOSQ_ACL_UNSUBSCRIBE 0x08
  33. #include <stdbool.h>
  34. #include <stdint.h>
  35. #include <mosquitto_broker.h>
  36. struct mosquitto;
  37. struct mosquitto_opt {
  38. char *key;
  39. char *value;
  40. };
  41. struct mosquitto_auth_opt {
  42. char *key;
  43. char *value;
  44. };
  45. struct mosquitto_acl_msg {
  46. const char *topic;
  47. const void *payload;
  48. long payloadlen;
  49. int qos;
  50. bool retain;
  51. };
  52. #ifdef WIN32
  53. # define mosq_plugin_EXPORT __declspec(dllexport)
  54. #else
  55. # define mosq_plugin_EXPORT
  56. #endif
  57. /*
  58. * To create an authentication plugin you must include this file then implement
  59. * the functions listed in the "Plugin Functions" section below. The resulting
  60. * code should then be compiled as a shared library. Using gcc this can be
  61. * achieved as follows:
  62. *
  63. * gcc -I<path to mosquitto_plugin.h> -fPIC -shared plugin.c -o plugin.so
  64. *
  65. * On Mac OS X:
  66. *
  67. * gcc -I<path to mosquitto_plugin.h> -fPIC -shared plugin.c -undefined dynamic_lookup -o plugin.so
  68. *
  69. * Authentication plugins can implement one or both of authentication and
  70. * access control. If your plugin does not wish to handle either of
  71. * authentication or access control it should return MOSQ_ERR_PLUGIN_DEFER. In
  72. * this case, the next plugin will handle it. If all plugins return
  73. * MOSQ_ERR_PLUGIN_DEFER, the request will be denied.
  74. *
  75. * For each check, the following flow happens:
  76. *
  77. * * The default password file and/or acl file checks are made. If either one
  78. * of these is not defined, then they are considered to be deferred. If either
  79. * one accepts the check, no further checks are made. If an error occurs, the
  80. * check is denied
  81. * * The first plugin does the check, if it returns anything other than
  82. * MOSQ_ERR_PLUGIN_DEFER, then the check returns immediately. If the plugin
  83. * returns MOSQ_ERR_PLUGIN_DEFER then the next plugin runs its check.
  84. * * If the final plugin returns MOSQ_ERR_PLUGIN_DEFER, then access will be
  85. * denied.
  86. */
  87. /* =========================================================================
  88. *
  89. * Helper Functions
  90. *
  91. * ========================================================================= */
  92. /* There are functions that are available for plugin developers to use in
  93. * mosquitto_broker.h, including logging and accessor functions.
  94. */
  95. /* =========================================================================
  96. *
  97. * Section: Plugin Functions v5
  98. *
  99. * This is the plugin version 5 interface, which covers authentication, access
  100. * control, the $CONTROL topic space handling, and message inspection and
  101. * modification.
  102. *
  103. * This interface is available from v2.0 onwards.
  104. *
  105. * There are just three functions to implement in your plugin. You should
  106. * register callbacks to handle different events in your
  107. * mosquitto_plugin_init() function. See mosquitto_broker.h for the events and
  108. * callback registering functions.
  109. *
  110. * ========================================================================= */
  111. /*
  112. * Function: mosquitto_plugin_version
  113. *
  114. * The broker will attempt to call this function immediately after loading the
  115. * plugin to check it is a supported plugin version. Your code must simply
  116. * return the plugin interface version you support, i.e. 5.
  117. *
  118. * The supported_versions array tells you which plugin versions the broker supports.
  119. *
  120. * If the broker does not support the version that you require, return -1 to
  121. * indicate failure.
  122. */
  123. mosq_plugin_EXPORT int mosquitto_plugin_version(int supported_version_count, const int *supported_versions);
  124. /*
  125. * Function: mosquitto_plugin_init
  126. *
  127. * Called after the plugin has been loaded and <mosquitto_plugin_version>
  128. * has been called. This will only ever be called once and can be used to
  129. * initialise the plugin.
  130. *
  131. * Parameters:
  132. *
  133. * identifier - This is a pointer to an opaque structure which you must
  134. * save and use when registering/unregistering callbacks.
  135. * user_data - The pointer set here will be passed to the other plugin
  136. * functions. Use to hold connection information for example.
  137. * opts - Pointer to an array of struct mosquitto_opt, which
  138. * provides the plugin options defined in the configuration file.
  139. * opt_count - The number of elements in the opts array.
  140. *
  141. * Return value:
  142. * Return 0 on success
  143. * Return >0 on failure.
  144. */
  145. mosq_plugin_EXPORT int mosquitto_plugin_init(mosquitto_plugin_id_t *identifier, void **userdata, struct mosquitto_opt *options, int option_count);
  146. /*
  147. * Function: mosquitto_plugin_cleanup
  148. *
  149. * Called when the broker is shutting down. This will only ever be called once
  150. * per plugin.
  151. *
  152. * Parameters:
  153. *
  154. * user_data - The pointer provided in <mosquitto_plugin_init>.
  155. * opts - Pointer to an array of struct mosquitto_opt, which
  156. * provides the plugin options defined in the configuration file.
  157. * opt_count - The number of elements in the opts array.
  158. *
  159. * Return value:
  160. * Return 0 on success
  161. * Return >0 on failure.
  162. */
  163. mosq_plugin_EXPORT int mosquitto_plugin_cleanup(void *userdata, struct mosquitto_opt *options, int option_count);
  164. /* =========================================================================
  165. *
  166. * Section: Plugin Functions v4
  167. *
  168. * This is the plugin version 4 interface, which is exclusively for
  169. * authentication and access control, and which is still supported for existing
  170. * plugins. If you are developing a new plugin, please use the v5 interface.
  171. *
  172. * You must implement these functions in your plugin.
  173. *
  174. * ========================================================================= */
  175. /*
  176. * Function: mosquitto_auth_plugin_version
  177. *
  178. * The broker will call this function immediately after loading the plugin to
  179. * check it is a supported plugin version. Your code must simply return
  180. * the version of the plugin interface you support, i.e. 4.
  181. */
  182. mosq_plugin_EXPORT int mosquitto_auth_plugin_version(void);
  183. /*
  184. * Function: mosquitto_auth_plugin_init
  185. *
  186. * Called after the plugin has been loaded and <mosquitto_auth_plugin_version>
  187. * has been called. This will only ever be called once and can be used to
  188. * initialise the plugin.
  189. *
  190. * Parameters:
  191. *
  192. * user_data - The pointer set here will be passed to the other plugin
  193. * functions. Use to hold connection information for example.
  194. * opts - Pointer to an array of struct mosquitto_opt, which
  195. * provides the plugin options defined in the configuration file.
  196. * opt_count - The number of elements in the opts array.
  197. *
  198. * Return value:
  199. * Return 0 on success
  200. * Return >0 on failure.
  201. */
  202. mosq_plugin_EXPORT int mosquitto_auth_plugin_init(void **user_data, struct mosquitto_opt *opts, int opt_count);
  203. /*
  204. * Function: mosquitto_auth_plugin_cleanup
  205. *
  206. * Called when the broker is shutting down. This will only ever be called once
  207. * per plugin.
  208. * Note that <mosquitto_auth_security_cleanup> will be called directly before
  209. * this function.
  210. *
  211. * Parameters:
  212. *
  213. * user_data - The pointer provided in <mosquitto_auth_plugin_init>.
  214. * opts - Pointer to an array of struct mosquitto_opt, which
  215. * provides the plugin options defined in the configuration file.
  216. * opt_count - The number of elements in the opts array.
  217. *
  218. * Return value:
  219. * Return 0 on success
  220. * Return >0 on failure.
  221. */
  222. mosq_plugin_EXPORT int mosquitto_auth_plugin_cleanup(void *user_data, struct mosquitto_opt *opts, int opt_count);
  223. /*
  224. * Function: mosquitto_auth_security_init
  225. *
  226. * This function is called in two scenarios:
  227. *
  228. * 1. When the broker starts up.
  229. * 2. If the broker is requested to reload its configuration whilst running. In
  230. * this case, <mosquitto_auth_security_cleanup> will be called first, then
  231. * this function will be called. In this situation, the reload parameter
  232. * will be true.
  233. *
  234. * Parameters:
  235. *
  236. * user_data - The pointer provided in <mosquitto_auth_plugin_init>.
  237. * opts - Pointer to an array of struct mosquitto_opt, which
  238. * provides the plugin options defined in the configuration file.
  239. * opt_count - The number of elements in the opts array.
  240. * reload - If set to false, this is the first time the function has
  241. * been called. If true, the broker has received a signal
  242. * asking to reload its configuration.
  243. *
  244. * Return value:
  245. * Return 0 on success
  246. * Return >0 on failure.
  247. */
  248. mosq_plugin_EXPORT int mosquitto_auth_security_init(void *user_data, struct mosquitto_opt *opts, int opt_count, bool reload);
  249. /*
  250. * Function: mosquitto_auth_security_cleanup
  251. *
  252. * This function is called in two scenarios:
  253. *
  254. * 1. When the broker is shutting down.
  255. * 2. If the broker is requested to reload its configuration whilst running. In
  256. * this case, this function will be called, followed by
  257. * <mosquitto_auth_security_init>. In this situation, the reload parameter
  258. * will be true.
  259. *
  260. * Parameters:
  261. *
  262. * user_data - The pointer provided in <mosquitto_auth_plugin_init>.
  263. * opts - Pointer to an array of struct mosquitto_opt, which
  264. * provides the plugin options defined in the configuration file.
  265. * opt_count - The number of elements in the opts array.
  266. * reload - If set to false, this is the first time the function has
  267. * been called. If true, the broker has received a signal
  268. * asking to reload its configuration.
  269. *
  270. * Return value:
  271. * Return 0 on success
  272. * Return >0 on failure.
  273. */
  274. mosq_plugin_EXPORT int mosquitto_auth_security_cleanup(void *user_data, struct mosquitto_opt *opts, int opt_count, bool reload);
  275. /*
  276. * Function: mosquitto_auth_acl_check
  277. *
  278. * Called by the broker when topic access must be checked. access will be one
  279. * of:
  280. * MOSQ_ACL_SUBSCRIBE when a client is asking to subscribe to a topic string.
  281. * This differs from MOSQ_ACL_READ in that it allows you to
  282. * deny access to topic strings rather than by pattern. For
  283. * example, you may use MOSQ_ACL_SUBSCRIBE to deny
  284. * subscriptions to '#', but allow all topics in
  285. * MOSQ_ACL_READ. This allows clients to subscribe to any
  286. * topic they want, but not discover what topics are in use
  287. * on the server.
  288. * MOSQ_ACL_READ when a message is about to be sent to a client (i.e. whether
  289. * it can read that topic or not).
  290. * MOSQ_ACL_WRITE when a message has been received from a client (i.e. whether
  291. * it can write to that topic or not).
  292. *
  293. * Return:
  294. * MOSQ_ERR_SUCCESS if access was granted.
  295. * MOSQ_ERR_ACL_DENIED if access was not granted.
  296. * MOSQ_ERR_UNKNOWN for an application specific error.
  297. * MOSQ_ERR_PLUGIN_DEFER if your plugin does not wish to handle this check.
  298. */
  299. mosq_plugin_EXPORT int mosquitto_auth_acl_check(void *user_data, int access, struct mosquitto *client, const struct mosquitto_acl_msg *msg);
  300. /*
  301. * Function: mosquitto_auth_unpwd_check
  302. *
  303. * This function is OPTIONAL. Only include this function in your plugin if you
  304. * are making basic username/password checks.
  305. *
  306. * Called by the broker when a username/password must be checked.
  307. *
  308. * Return:
  309. * MOSQ_ERR_SUCCESS if the user is authenticated.
  310. * MOSQ_ERR_AUTH if authentication failed.
  311. * MOSQ_ERR_UNKNOWN for an application specific error.
  312. * MOSQ_ERR_PLUGIN_DEFER if your plugin does not wish to handle this check.
  313. */
  314. mosq_plugin_EXPORT int mosquitto_auth_unpwd_check(void *user_data, struct mosquitto *client, const char *username, const char *password);
  315. /*
  316. * Function: mosquitto_psk_key_get
  317. *
  318. * This function is OPTIONAL. Only include this function in your plugin if you
  319. * are making TLS-PSK checks.
  320. *
  321. * Called by the broker when a client connects to a listener using TLS/PSK.
  322. * This is used to retrieve the pre-shared-key associated with a client
  323. * identity.
  324. *
  325. * Examine hint and identity to determine the required PSK (which must be a
  326. * hexadecimal string with no leading "0x") and copy this string into key.
  327. *
  328. * Parameters:
  329. * user_data - the pointer provided in <mosquitto_auth_plugin_init>.
  330. * hint - the psk_hint for the listener the client is connecting to.
  331. * identity - the identity string provided by the client
  332. * key - a string where the hex PSK should be copied
  333. * max_key_len - the size of key
  334. *
  335. * Return value:
  336. * Return 0 on success.
  337. * Return >0 on failure.
  338. * Return MOSQ_ERR_PLUGIN_DEFER if your plugin does not wish to handle this check.
  339. */
  340. mosq_plugin_EXPORT int mosquitto_auth_psk_key_get(void *user_data, struct mosquitto *client, const char *hint, const char *identity, char *key, int max_key_len);
  341. /*
  342. * Function: mosquitto_auth_start
  343. *
  344. * This function is OPTIONAL. Only include this function in your plugin if you
  345. * are making extended authentication checks.
  346. *
  347. * Parameters:
  348. * user_data - the pointer provided in <mosquitto_auth_plugin_init>.
  349. * method - the authentication method
  350. * reauth - this is set to false if this is the first authentication attempt
  351. * on a connection, set to true if the client is attempting to
  352. * reauthenticate.
  353. * data_in - pointer to authentication data, or NULL
  354. * data_in_len - length of data_in, in bytes
  355. * data_out - if your plugin wishes to send authentication data back to the
  356. * client, allocate some memory using malloc or friends and set
  357. * data_out. The broker will free the memory after use.
  358. * data_out_len - Set the length of data_out in bytes.
  359. *
  360. * Return value:
  361. * Return MOSQ_ERR_SUCCESS if authentication was successful.
  362. * Return MOSQ_ERR_AUTH_CONTINUE if the authentication is a multi step process and can continue.
  363. * Return MOSQ_ERR_AUTH if authentication was valid but did not succeed.
  364. * Return any other relevant positive integer MOSQ_ERR_* to produce an error.
  365. */
  366. mosq_plugin_EXPORT int mosquitto_auth_start(void *user_data, struct mosquitto *client, const char *method, bool reauth, const void *data_in, uint16_t data_in_len, void **data_out, uint16_t *data_out_len);
  367. mosq_plugin_EXPORT int mosquitto_auth_continue(void *user_data, struct mosquitto *client, const char *method, const void *data_in, uint16_t data_in_len, void **data_out, uint16_t *data_out_len);
  368. #ifdef __cplusplus
  369. }
  370. #endif
  371. #endif