Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-06-01 08:54:12

0001 /*
0002 Copyright (c) 2012-2020 Roger Light <roger@atchoo.org>
0003 
0004 All rights reserved. This program and the accompanying materials
0005 are made available under the terms of the Eclipse Public License 2.0
0006 and Eclipse Distribution License v1.0 which accompany this distribution.
0007 
0008 The Eclipse Public License is available at
0009    https://www.eclipse.org/legal/epl-2.0/
0010 and the Eclipse Distribution License is available at
0011   http://www.eclipse.org/org/documents/edl-v10.php.
0012 
0013 SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
0014 
0015 Contributors:
0016    Roger Light - initial implementation and documentation.
0017 */
0018 
0019 #ifndef MOSQUITTO_PLUGIN_H
0020 #define MOSQUITTO_PLUGIN_H
0021 
0022 /*
0023  * File: mosquitto_plugin.h
0024  *
0025  * This header contains function declarations for use when writing a Mosquitto plugin.
0026  */
0027 
0028 #ifdef __cplusplus
0029 extern "C" {
0030 #endif
0031 
0032 /* The generic plugin interface starts at version 5 */
0033 #define MOSQ_PLUGIN_VERSION 5
0034 
0035 /* The old auth only interface stopped at version 4 */
0036 #define MOSQ_AUTH_PLUGIN_VERSION 4
0037 
0038 #define MOSQ_ACL_NONE 0x00
0039 #define MOSQ_ACL_READ 0x01
0040 #define MOSQ_ACL_WRITE 0x02
0041 #define MOSQ_ACL_SUBSCRIBE 0x04
0042 #define MOSQ_ACL_UNSUBSCRIBE 0x08
0043 
0044 #include <stdbool.h>
0045 #include <stdint.h>
0046 
0047 #include <mosquitto_broker.h>
0048 
0049 struct mosquitto;
0050 
0051 struct mosquitto_opt {
0052     char *key;
0053     char *value;
0054 };
0055 
0056 struct mosquitto_auth_opt {
0057     char *key;
0058     char *value;
0059 };
0060 
0061 struct mosquitto_acl_msg {
0062     const char *topic;
0063     const void *payload;
0064     long payloadlen;
0065     int qos;
0066     bool retain;
0067 };
0068 
0069 #ifdef WIN32
0070 #  define mosq_plugin_EXPORT __declspec(dllexport)
0071 #else
0072 #  define mosq_plugin_EXPORT
0073 #endif
0074 
0075 /*
0076  * To create an authentication plugin you must include this file then implement
0077  * the functions listed in the "Plugin Functions" section below. The resulting
0078  * code should then be compiled as a shared library. Using gcc this can be
0079  * achieved as follows:
0080  *
0081  * gcc -I<path to mosquitto_plugin.h> -fPIC -shared plugin.c -o plugin.so
0082  *
0083  * On Mac OS X:
0084  *
0085  * gcc -I<path to mosquitto_plugin.h> -fPIC -shared plugin.c -undefined dynamic_lookup -o plugin.so
0086  *
0087  * Authentication plugins can implement one or both of authentication and
0088  * access control. If your plugin does not wish to handle either of
0089  * authentication or access control it should return MOSQ_ERR_PLUGIN_DEFER. In
0090  * this case, the next plugin will handle it. If all plugins return
0091  * MOSQ_ERR_PLUGIN_DEFER, the request will be denied.
0092  *
0093  * For each check, the following flow happens:
0094  *
0095  * * The default password file and/or acl file checks are made. If either one
0096  *   of these is not defined, then they are considered to be deferred. If either
0097  *   one accepts the check, no further checks are made. If an error occurs, the
0098  *   check is denied
0099  * * The first plugin does the check, if it returns anything other than
0100  *   MOSQ_ERR_PLUGIN_DEFER, then the check returns immediately. If the plugin
0101  *   returns MOSQ_ERR_PLUGIN_DEFER then the next plugin runs its check.
0102  * * If the final plugin returns MOSQ_ERR_PLUGIN_DEFER, then access will be
0103  *   denied.
0104  */
0105 
0106 /* =========================================================================
0107  *
0108  * Helper Functions
0109  *
0110  * ========================================================================= */
0111 
0112 /* There are functions that are available for plugin developers to use in
0113  * mosquitto_broker.h, including logging and accessor functions.
0114  */
0115 
0116 
0117 /* =========================================================================
0118  *
0119  * Section: Plugin Functions v5
0120  *
0121  * This is the plugin version 5 interface, which covers authentication, access
0122  * control, the $CONTROL topic space handling, and message inspection and
0123  * modification.
0124  *
0125  * This interface is available from v2.0 onwards.
0126  *
0127  * There are just three functions to implement in your plugin. You should
0128  * register callbacks to handle different events in your
0129  * mosquitto_plugin_init() function. See mosquitto_broker.h for the events and
0130  * callback registering functions.
0131  *
0132  * ========================================================================= */
0133 
0134 /*
0135  * Function: mosquitto_plugin_version
0136  *
0137  * The broker will attempt to call this function immediately after loading the
0138  * plugin to check it is a supported plugin version. Your code must simply
0139  * return the plugin interface version you support, i.e. 5.
0140  *
0141  * The supported_versions array tells you which plugin versions the broker supports.
0142  *
0143  * If the broker does not support the version that you require, return -1 to
0144  * indicate failure.
0145  */
0146 mosq_plugin_EXPORT int mosquitto_plugin_version(int supported_version_count, const int *supported_versions);
0147 
0148 /*
0149  * Function: mosquitto_plugin_init
0150  *
0151  * Called after the plugin has been loaded and <mosquitto_plugin_version>
0152  * has been called. This will only ever be called once and can be used to
0153  * initialise the plugin.
0154  *
0155  * Parameters:
0156  *
0157  *  identifier -     This is a pointer to an opaque structure which you must
0158  *                   save and use when registering/unregistering callbacks.
0159  *  user_data -      The pointer set here will be passed to the other plugin
0160  *                   functions. Use to hold connection information for example.
0161  *  opts -           Pointer to an array of struct mosquitto_opt, which
0162  *                   provides the plugin options defined in the configuration file.
0163  *  opt_count -      The number of elements in the opts array.
0164  *
0165  * Return value:
0166  *  Return 0 on success
0167  *  Return >0 on failure.
0168  */
0169 mosq_plugin_EXPORT int mosquitto_plugin_init(mosquitto_plugin_id_t *identifier, void **userdata, struct mosquitto_opt *options, int option_count);
0170 
0171 
0172 /*
0173  * Function: mosquitto_plugin_cleanup
0174  *
0175  * Called when the broker is shutting down. This will only ever be called once
0176  * per plugin.
0177  *
0178  * Parameters:
0179  *
0180  *  user_data -      The pointer provided in <mosquitto_plugin_init>.
0181  *  opts -           Pointer to an array of struct mosquitto_opt, which
0182  *                   provides the plugin options defined in the configuration file.
0183  *  opt_count -      The number of elements in the opts array.
0184  *
0185  * Return value:
0186  *  Return 0 on success
0187  *  Return >0 on failure.
0188  */
0189 mosq_plugin_EXPORT int mosquitto_plugin_cleanup(void *userdata, struct mosquitto_opt *options, int option_count);
0190 
0191 
0192 
0193 /* =========================================================================
0194  *
0195  * Section: Plugin Functions v4
0196  *
0197  * This is the plugin version 4 interface, which is exclusively for
0198  * authentication and access control, and which is still supported for existing
0199  * plugins. If you are developing a new plugin, please use the v5 interface.
0200  *
0201  * You must implement these functions in your plugin.
0202  *
0203  * ========================================================================= */
0204 
0205 /*
0206  * Function: mosquitto_auth_plugin_version
0207  *
0208  * The broker will call this function immediately after loading the plugin to
0209  * check it is a supported plugin version. Your code must simply return
0210  * the version of the plugin interface you support, i.e. 4.
0211  */
0212 mosq_plugin_EXPORT int mosquitto_auth_plugin_version(void);
0213 
0214 
0215 /*
0216  * Function: mosquitto_auth_plugin_init
0217  *
0218  * Called after the plugin has been loaded and <mosquitto_auth_plugin_version>
0219  * has been called. This will only ever be called once and can be used to
0220  * initialise the plugin.
0221  *
0222  * Parameters:
0223  *
0224  *  user_data -      The pointer set here will be passed to the other plugin
0225  *                   functions. Use to hold connection information for example.
0226  *  opts -           Pointer to an array of struct mosquitto_opt, which
0227  *                   provides the plugin options defined in the configuration file.
0228  *  opt_count -      The number of elements in the opts array.
0229  *
0230  * Return value:
0231  *  Return 0 on success
0232  *  Return >0 on failure.
0233  */
0234 mosq_plugin_EXPORT int mosquitto_auth_plugin_init(void **user_data, struct mosquitto_opt *opts, int opt_count);
0235 
0236 
0237 /*
0238  * Function: mosquitto_auth_plugin_cleanup
0239  *
0240  * Called when the broker is shutting down. This will only ever be called once
0241  * per plugin.
0242  * Note that <mosquitto_auth_security_cleanup> will be called directly before
0243  * this function.
0244  *
0245  * Parameters:
0246  *
0247  *  user_data -      The pointer provided in <mosquitto_auth_plugin_init>.
0248  *  opts -           Pointer to an array of struct mosquitto_opt, which
0249  *                   provides the plugin options defined in the configuration file.
0250  *  opt_count -      The number of elements in the opts array.
0251  *
0252  * Return value:
0253  *  Return 0 on success
0254  *  Return >0 on failure.
0255  */
0256 mosq_plugin_EXPORT int mosquitto_auth_plugin_cleanup(void *user_data, struct mosquitto_opt *opts, int opt_count);
0257 
0258 
0259 /*
0260  * Function: mosquitto_auth_security_init
0261  *
0262  * This function is called in two scenarios:
0263  *
0264  * 1. When the broker starts up.
0265  * 2. If the broker is requested to reload its configuration whilst running. In
0266  *    this case, <mosquitto_auth_security_cleanup> will be called first, then
0267  *    this function will be called.  In this situation, the reload parameter
0268  *    will be true.
0269  *
0270  * Parameters:
0271  *
0272  *  user_data -      The pointer provided in <mosquitto_auth_plugin_init>.
0273  *  opts -           Pointer to an array of struct mosquitto_opt, which
0274  *                   provides the plugin options defined in the configuration file.
0275  *  opt_count -      The number of elements in the opts array.
0276  *  reload -         If set to false, this is the first time the function has
0277  *                   been called. If true, the broker has received a signal
0278  *                   asking to reload its configuration.
0279  *
0280  * Return value:
0281  *  Return 0 on success
0282  *  Return >0 on failure.
0283  */
0284 mosq_plugin_EXPORT int mosquitto_auth_security_init(void *user_data, struct mosquitto_opt *opts, int opt_count, bool reload);
0285 
0286 
0287 /*
0288  * Function: mosquitto_auth_security_cleanup
0289  *
0290  * This function is called in two scenarios:
0291  *
0292  * 1. When the broker is shutting down.
0293  * 2. If the broker is requested to reload its configuration whilst running. In
0294  *    this case, this function will be called, followed by
0295  *    <mosquitto_auth_security_init>. In this situation, the reload parameter
0296  *    will be true.
0297  *
0298  * Parameters:
0299  *
0300  *  user_data -      The pointer provided in <mosquitto_auth_plugin_init>.
0301  *  opts -           Pointer to an array of struct mosquitto_opt, which
0302  *                   provides the plugin options defined in the configuration file.
0303  *  opt_count -      The number of elements in the opts array.
0304  *  reload -         If set to false, this is the first time the function has
0305  *                   been called. If true, the broker has received a signal
0306  *                   asking to reload its configuration.
0307  *
0308  * Return value:
0309  *  Return 0 on success
0310  *  Return >0 on failure.
0311  */
0312 mosq_plugin_EXPORT int mosquitto_auth_security_cleanup(void *user_data, struct mosquitto_opt *opts, int opt_count, bool reload);
0313 
0314 
0315 /*
0316  * Function: mosquitto_auth_acl_check
0317  *
0318  * Called by the broker when topic access must be checked. access will be one
0319  * of:
0320  *  MOSQ_ACL_SUBSCRIBE when a client is asking to subscribe to a topic string.
0321  *                     This differs from MOSQ_ACL_READ in that it allows you to
0322  *                     deny access to topic strings rather than by pattern. For
0323  *                     example, you may use MOSQ_ACL_SUBSCRIBE to deny
0324  *                     subscriptions to '#', but allow all topics in
0325  *                     MOSQ_ACL_READ. This allows clients to subscribe to any
0326  *                     topic they want, but not discover what topics are in use
0327  *                     on the server.
0328  *  MOSQ_ACL_READ      when a message is about to be sent to a client (i.e. whether
0329  *                     it can read that topic or not).
0330  *  MOSQ_ACL_WRITE     when a message has been received from a client (i.e. whether
0331  *                     it can write to that topic or not).
0332  *
0333  * Return:
0334  *  MOSQ_ERR_SUCCESS if access was granted.
0335  *  MOSQ_ERR_ACL_DENIED if access was not granted.
0336  *  MOSQ_ERR_UNKNOWN for an application specific error.
0337  *  MOSQ_ERR_PLUGIN_DEFER if your plugin does not wish to handle this check.
0338  */
0339 mosq_plugin_EXPORT int mosquitto_auth_acl_check(void *user_data, int access, struct mosquitto *client, const struct mosquitto_acl_msg *msg);
0340 
0341 
0342 /*
0343  * Function: mosquitto_auth_unpwd_check
0344  *
0345  * This function is OPTIONAL. Only include this function in your plugin if you
0346  * are making basic username/password checks.
0347  *
0348  * Called by the broker when a username/password must be checked.
0349  *
0350  * Return:
0351  *  MOSQ_ERR_SUCCESS if the user is authenticated.
0352  *  MOSQ_ERR_AUTH if authentication failed.
0353  *  MOSQ_ERR_UNKNOWN for an application specific error.
0354  *  MOSQ_ERR_PLUGIN_DEFER if your plugin does not wish to handle this check.
0355  */
0356 mosq_plugin_EXPORT int mosquitto_auth_unpwd_check(void *user_data, struct mosquitto *client, const char *username, const char *password);
0357 
0358 
0359 /*
0360  * Function: mosquitto_psk_key_get
0361  *
0362  * This function is OPTIONAL. Only include this function in your plugin if you
0363  * are making TLS-PSK checks.
0364  *
0365  * Called by the broker when a client connects to a listener using TLS/PSK.
0366  * This is used to retrieve the pre-shared-key associated with a client
0367  * identity.
0368  *
0369  * Examine hint and identity to determine the required PSK (which must be a
0370  * hexadecimal string with no leading "0x") and copy this string into key.
0371  *
0372  * Parameters:
0373  *  user_data -   the pointer provided in <mosquitto_auth_plugin_init>.
0374  *  hint -        the psk_hint for the listener the client is connecting to.
0375  *  identity -    the identity string provided by the client
0376  *  key -         a string where the hex PSK should be copied
0377  *  max_key_len - the size of key
0378  *
0379  * Return value:
0380  *  Return 0 on success.
0381  *  Return >0 on failure.
0382  *  Return MOSQ_ERR_PLUGIN_DEFER if your plugin does not wish to handle this check.
0383  */
0384 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);
0385 
0386 /*
0387  * Function: mosquitto_auth_start
0388  *
0389  * This function is OPTIONAL. Only include this function in your plugin if you
0390  * are making extended authentication checks.
0391  *
0392  * Parameters:
0393  *  user_data -   the pointer provided in <mosquitto_auth_plugin_init>.
0394  *  method - the authentication method
0395  *  reauth - this is set to false if this is the first authentication attempt
0396  *           on a connection, set to true if the client is attempting to
0397  *           reauthenticate.
0398  *  data_in - pointer to authentication data, or NULL
0399  *  data_in_len - length of data_in, in bytes
0400  *  data_out - if your plugin wishes to send authentication data back to the
0401  *             client, allocate some memory using malloc or friends and set
0402  *             data_out. The broker will free the memory after use.
0403  *  data_out_len - Set the length of data_out in bytes.
0404  *
0405  * Return value:
0406  *  Return MOSQ_ERR_SUCCESS if authentication was successful.
0407  *  Return MOSQ_ERR_AUTH_CONTINUE if the authentication is a multi step process and can continue.
0408  *  Return MOSQ_ERR_AUTH if authentication was valid but did not succeed.
0409  *  Return any other relevant positive integer MOSQ_ERR_* to produce an error.
0410  */
0411 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);
0412 
0413 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);
0414 
0415 
0416 #ifdef __cplusplus
0417 }
0418 #endif
0419 
0420 #endif