mosquitto_plugin_v2.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. Copyright (c) 2012-2014 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. Contributors:
  11. Roger Light - initial implementation and documentation.
  12. */
  13. #ifndef MOSQUITTO_PLUGIN_H
  14. #define MOSQUITTO_PLUGIN_H
  15. #define MOSQ_AUTH_PLUGIN_VERSION 2
  16. #define MOSQ_ACL_NONE 0x00
  17. #define MOSQ_ACL_READ 0x01
  18. #define MOSQ_ACL_WRITE 0x02
  19. struct mosquitto_auth_opt {
  20. char *key;
  21. char *value;
  22. };
  23. /*
  24. * To create an authentication plugin you must include this file then implement
  25. * the functions listed below. The resulting code should then be compiled as a
  26. * shared library. Using gcc this can be achieved as follows:
  27. *
  28. * gcc -I<path to mosquitto_plugin.h> -fPIC -shared plugin.c -o plugin.so
  29. *
  30. * On Mac OS X:
  31. *
  32. * gcc -I<path to mosquitto_plugin.h> -fPIC -shared plugin.c -undefined dynamic_lookup -o plugin.so
  33. *
  34. */
  35. /* =========================================================================
  36. *
  37. * Utility Functions
  38. *
  39. * Use these functions from within your plugin.
  40. *
  41. * There are also very useful functions in libmosquitto.
  42. *
  43. * ========================================================================= */
  44. /*
  45. * Function: mosquitto_log_printf
  46. *
  47. * Write a log message using the broker configured logging.
  48. *
  49. * Parameters:
  50. * level - Log message priority. Can currently be one of:
  51. *
  52. * MOSQ_LOG_INFO
  53. * MOSQ_LOG_NOTICE
  54. * MOSQ_LOG_WARNING
  55. * MOSQ_LOG_ERR
  56. * MOSQ_LOG_DEBUG
  57. * MOSQ_LOG_SUBSCRIBE (not recommended for use by plugins)
  58. * MOSQ_LOG_UNSUBSCRIBE (not recommended for use by plugins)
  59. *
  60. * These values are defined in mosquitto.h.
  61. *
  62. * fmt, ... - printf style format and arguments.
  63. */
  64. void mosquitto_log_printf(int level, const char *fmt, ...);
  65. /* =========================================================================
  66. *
  67. * Plugin Functions
  68. *
  69. * You must implement these functions in your plugin.
  70. *
  71. * ========================================================================= */
  72. /*
  73. * Function: mosquitto_auth_plugin_version
  74. *
  75. * The broker will call this function immediately after loading the plugin to
  76. * check it is a supported plugin version. Your code must simply return
  77. * MOSQ_AUTH_PLUGIN_VERSION.
  78. */
  79. int mosquitto_auth_plugin_version(void);
  80. /*
  81. * Function: mosquitto_auth_plugin_init
  82. *
  83. * Called after the plugin has been loaded and <mosquitto_auth_plugin_version>
  84. * has been called. This will only ever be called once and can be used to
  85. * initialise the plugin.
  86. *
  87. * Parameters:
  88. *
  89. * user_data : The pointer set here will be passed to the other plugin
  90. * functions. Use to hold connection information for example.
  91. * auth_opts : Pointer to an array of struct mosquitto_auth_opt, which
  92. * provides the plugin options defined in the configuration file.
  93. * auth_opt_count : The number of elements in the auth_opts array.
  94. *
  95. * Return value:
  96. * Return 0 on success
  97. * Return >0 on failure.
  98. */
  99. int mosquitto_auth_plugin_init(void **user_data, struct mosquitto_auth_opt *auth_opts, int auth_opt_count);
  100. /*
  101. * Function: mosquitto_auth_plugin_cleanup
  102. *
  103. * Called when the broker is shutting down. This will only ever be called once.
  104. * Note that <mosquitto_auth_security_cleanup> will be called directly before
  105. * this function.
  106. *
  107. * Parameters:
  108. *
  109. * user_data : The pointer provided in <mosquitto_auth_plugin_init>.
  110. * auth_opts : Pointer to an array of struct mosquitto_auth_opt, which
  111. * provides the plugin options defined in the configuration file.
  112. * auth_opt_count : The number of elements in the auth_opts array.
  113. *
  114. * Return value:
  115. * Return 0 on success
  116. * Return >0 on failure.
  117. */
  118. int mosquitto_auth_plugin_cleanup(void *user_data, struct mosquitto_auth_opt *auth_opts, int auth_opt_count);
  119. /*
  120. * Function: mosquitto_auth_security_init
  121. *
  122. * Called when the broker initialises the security functions when it starts up.
  123. * If the broker is requested to reload its configuration whilst running,
  124. * <mosquitto_auth_security_cleanup> will be called, followed by this function.
  125. * In this situation, the reload parameter will be true.
  126. *
  127. * Parameters:
  128. *
  129. * user_data : The pointer provided in <mosquitto_auth_plugin_init>.
  130. * auth_opts : Pointer to an array of struct mosquitto_auth_opt, which
  131. * provides the plugin options defined in the configuration file.
  132. * auth_opt_count : The number of elements in the auth_opts array.
  133. * reload : If set to false, this is the first time the function has
  134. * been called. If true, the broker has received a signal
  135. * asking to reload its configuration.
  136. *
  137. * Return value:
  138. * Return 0 on success
  139. * Return >0 on failure.
  140. */
  141. int mosquitto_auth_security_init(void *user_data, struct mosquitto_auth_opt *auth_opts, int auth_opt_count, bool reload);
  142. /*
  143. * Function: mosquitto_auth_security_cleanup
  144. *
  145. * Called when the broker cleans up the security functions when it shuts down.
  146. * If the broker is requested to reload its configuration whilst running,
  147. * this function will be called, followed by <mosquitto_auth_security_init>.
  148. * In this situation, the reload parameter will be true.
  149. *
  150. * Parameters:
  151. *
  152. * user_data : The pointer provided in <mosquitto_auth_plugin_init>.
  153. * auth_opts : Pointer to an array of struct mosquitto_auth_opt, which
  154. * provides the plugin options defined in the configuration file.
  155. * auth_opt_count : The number of elements in the auth_opts array.
  156. * reload : If set to false, this is the first time the function has
  157. * been called. If true, the broker has received a signal
  158. * asking to reload its configuration.
  159. *
  160. * Return value:
  161. * Return 0 on success
  162. * Return >0 on failure.
  163. */
  164. int mosquitto_auth_security_cleanup(void *user_data, struct mosquitto_auth_opt *auth_opts, int auth_opt_count, bool reload);
  165. /*
  166. * Function: mosquitto_auth_acl_check
  167. *
  168. * Called by the broker when topic access must be checked. access will be one
  169. * of MOSQ_ACL_READ (for subscriptions) or MOSQ_ACL_WRITE (for publish). Return
  170. * MOSQ_ERR_SUCCESS if access was granted, MOSQ_ERR_ACL_DENIED if access was
  171. * not granted, or MOSQ_ERR_UNKNOWN for an application specific error.
  172. */
  173. int mosquitto_auth_acl_check(void *user_data, const char *clientid, const char *username, const char *topic, int access);
  174. /*
  175. * Function: mosquitto_auth_unpwd_check
  176. *
  177. * Called by the broker when a username/password must be checked. Return
  178. * MOSQ_ERR_SUCCESS if the user is authenticated, MOSQ_ERR_AUTH if
  179. * authentication failed, or MOSQ_ERR_UNKNOWN for an application specific
  180. * error.
  181. */
  182. int mosquitto_auth_unpwd_check(void *user_data, const char *username, const char *password);
  183. /*
  184. * Function: mosquitto_psk_key_get
  185. *
  186. * Called by the broker when a client connects to a listener using TLS/PSK.
  187. * This is used to retrieve the pre-shared-key associated with a client
  188. * identity.
  189. *
  190. * Examine hint and identity to determine the required PSK (which must be a
  191. * hexadecimal string with no leading "0x") and copy this string into key.
  192. *
  193. * Parameters:
  194. * user_data : the pointer provided in <mosquitto_auth_plugin_init>.
  195. * hint : the psk_hint for the listener the client is connecting to.
  196. * identity : the identity string provided by the client
  197. * key : a string where the hex PSK should be copied
  198. * max_key_len : the size of key
  199. *
  200. * Return value:
  201. * Return 0 on success.
  202. * Return >0 on failure.
  203. * Return >0 if this function is not required.
  204. */
  205. int mosquitto_auth_psk_key_get(void *user_data, const char *hint, const char *identity, char *key, int max_key_len);
  206. #endif