|
| 1 | +/* |
| 2 | +Copyright (C) by ebblo Switzerland GmbH, CH-8212 Neuhausen, Switzerland |
| 3 | +
|
| 4 | +This project and the accompanying materials are made available under the |
| 5 | +terms of the Eclipse Public License 2.0 and 3-Clause BSD License which |
| 6 | +accompany this distribution. |
| 7 | +
|
| 8 | +The Eclipse Public License is available at |
| 9 | + https://www.eclipse.org/legal/epl-2.0 |
| 10 | +and the 3-Clause BSD License is available at |
| 11 | + https://opensource.org/license/BSD-3-Clause |
| 12 | +
|
| 13 | +SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause |
| 14 | +
|
| 15 | +Contributors: |
| 16 | + - Thorsten Wendt <thorsten.wendt@ebblo.com> - Adding means for brigde authentication. |
| 17 | +*/ |
| 18 | + |
| 19 | +/* |
| 20 | + * This is an example plugin showing how to use the authentication |
| 21 | + * callback to provide username and password for connecting a bridge to a broker. |
| 22 | + * |
| 23 | + * This callback is called when a new bridge is going to be established. |
| 24 | + * |
| 25 | + * The callback have to provide username/passwort. The broker (in bridge mode) |
| 26 | + * is going to use these credentials for authentication against the broker. |
| 27 | + * |
| 28 | + * The following return values are supported: |
| 29 | + * |
| 30 | + * MOSQ_ERR_SUCCESS - This plugin provided username and password |
| 31 | + * MOSQ_ERR_PLUGIN_DEFER - This plugin is not able to provide username/password |
| 32 | + * MOSQ_ERR_AUTH_DENIED - Authentication is denied by the plugin and |
| 33 | + * MOSQ_ERR_NOMEM - Out of memory |
| 34 | + * |
| 35 | + * |
| 36 | + * Compile with: |
| 37 | + * gcc -I<path to mosquitto-repo/include> -fPIC -shared mosquitto_auth_by_bridge.c -o mosquitto_auth_by_bridge.so |
| 38 | + * |
| 39 | + * Use in config with: |
| 40 | + * |
| 41 | + * plugin /path/to/mosquitto_auth_by_bridge.so |
| 42 | + * |
| 43 | + * Note that this only works on Mosquitto 2.1 or later. |
| 44 | + */ |
| 45 | +#include <stdio.h> |
| 46 | +#include <stdlib.h> |
| 47 | +#include <string.h> |
| 48 | + |
| 49 | +#include "mosquitto.h" |
| 50 | + |
| 51 | +#include <config.h> |
| 52 | + |
| 53 | +#define PLUGIN_NAME "auth-by-bridge" |
| 54 | +#define PLUGIN_VERSION "0.0.1" |
| 55 | + |
| 56 | +#define ENV_AUTH_PLUGIN_USER "AUTH_PLUGIN_USER" |
| 57 | +#define ENV_AUTH_PLUGIN_PASS "AUTH_PLUGIN_PASS" |
| 58 | + |
| 59 | +MOSQUITTO_PLUGIN_DECLARE_VERSION( 5 ); |
| 60 | + |
| 61 | +static mosquitto_plugin_id_t *mosq_pid = NULL; |
| 62 | + |
| 63 | +static int basic_auth_callback(int event, void *event_data, void *userdata) |
| 64 | +{ |
| 65 | + struct mosquitto_evt_load_bridge_cred* ed = event_data; |
| 66 | + |
| 67 | + UNUSED( event ); |
| 68 | + UNUSED( userdata ); |
| 69 | + |
| 70 | + char* env_auth_plugin_user = getenv( ENV_AUTH_PLUGIN_USER ); |
| 71 | + char* env_auth_plugin_pass = getenv( ENV_AUTH_PLUGIN_PASS ); |
| 72 | + |
| 73 | + if ( ( env_auth_plugin_user && strlen( env_auth_plugin_user ) > 0 ) && |
| 74 | + ( env_auth_plugin_pass && strlen( env_auth_plugin_pass ) > 0 ) ) |
| 75 | + { |
| 76 | + char* auth_plugin_username = mosquitto_strdup( env_auth_plugin_user ); |
| 77 | + if ( !auth_plugin_username ) |
| 78 | + { |
| 79 | + mosquitto_log_printf( MOSQ_LOG_ERR, "Out of memory." ); |
| 80 | + return MOSQ_ERR_NOMEM; |
| 81 | + } |
| 82 | + |
| 83 | + char* auth_plugin_password = mosquitto_strdup( env_auth_plugin_pass ); |
| 84 | + if ( !auth_plugin_password ) |
| 85 | + { |
| 86 | + mosquitto_free( auth_plugin_username ); |
| 87 | + mosquitto_log_printf( MOSQ_LOG_ERR, "Out of memory." ); |
| 88 | + return MOSQ_ERR_NOMEM; |
| 89 | + } |
| 90 | + |
| 91 | + ed->username = auth_plugin_username; |
| 92 | + ed->password = auth_plugin_password; |
| 93 | + return MOSQ_ERR_SUCCESS; |
| 94 | + } |
| 95 | + |
| 96 | + return MOSQ_ERR_NOT_FOUND; |
| 97 | +} |
| 98 | + |
| 99 | + |
| 100 | +int mosquitto_plugin_init(mosquitto_plugin_id_t *identifier, void **user_data, struct mosquitto_opt *opts, int opt_count) |
| 101 | +{ |
| 102 | + UNUSED( user_data ); |
| 103 | + UNUSED( opts ); |
| 104 | + UNUSED( opt_count ); |
| 105 | + |
| 106 | + mosq_pid = identifier; |
| 107 | + mosquitto_plugin_set_info( identifier, PLUGIN_NAME, PLUGIN_VERSION ); |
| 108 | + return mosquitto_callback_register( mosq_pid, MOSQ_EVT_LOAD_BRIDGE_CRED, basic_auth_callback, NULL, NULL ); |
| 109 | +} |
| 110 | + |
| 111 | + |
| 112 | +/* mosquitto_plugin_cleanup() is optional in 2.1 and later. Use it only if you have your own cleanup to do */ |
| 113 | +int mosquitto_plugin_cleanup(void *user_data, struct mosquitto_opt *opts, int opt_count) |
| 114 | +{ |
| 115 | + UNUSED( user_data ); |
| 116 | + UNUSED( opts ); |
| 117 | + UNUSED( opt_count ); |
| 118 | + |
| 119 | + return MOSQ_ERR_SUCCESS; |
| 120 | +} |
0 commit comments