mosquitto/plugins/dynamic-security/plugin.c

87 lines
2.4 KiB
C
Raw Normal View History

2020-09-23 23:59:31 +02:00
/*
2021-11-03 23:08:24 +01:00
Copyright (c) 2020-2021 Roger Light <roger@atchoo.org>
2020-09-23 23:59:31 +02:00
All rights reserved. This program and the accompanying materials
2020-11-25 18:34:21 +01:00
are made available under the terms of the Eclipse Public License 2.0
2020-09-23 23:59:31 +02:00
and Eclipse Distribution License v1.0 which accompany this distribution.
The Eclipse Public License is available at
2020-11-25 18:34:21 +01:00
https://www.eclipse.org/legal/epl-2.0/
2020-09-23 23:59:31 +02:00
and the Eclipse Distribution License is available at
http://www.eclipse.org/org/documents/edl-v10.php.
SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
2020-12-01 19:21:59 +01:00
2020-09-23 23:59:31 +02:00
Contributors:
Roger Light - initial implementation and documentation.
*/
#include "config.h"
#include <errno.h>
2020-09-23 23:59:31 +02:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include "mosquitto.h"
#include "mosquitto_broker.h"
#include "mosquitto_plugin.h"
2020-11-03 11:09:18 +01:00
#include "mqtt_protocol.h"
2020-09-23 23:59:31 +02:00
#include "dynamic_security.h"
2021-09-30 11:56:48 +02:00
MOSQUITTO_PLUGIN_DECLARE_VERSION(5);
2022-01-27 17:09:09 +01:00
struct dynsec__data g_dynsec_data;
2020-09-23 23:59:31 +02:00
static mosquitto_plugin_id_t *plg_id = NULL;
2021-12-13 10:14:05 +01:00
char *g_config_file = NULL;
2020-09-23 23:59:31 +02:00
int mosquitto_plugin_init(mosquitto_plugin_id_t *identifier, void **user_data, struct mosquitto_opt *options, int option_count)
{
int i;
UNUSED(user_data);
2022-01-27 17:09:09 +01:00
memset(&g_dynsec_data, 0, sizeof(struct dynsec__data));
2020-09-23 23:59:31 +02:00
for(i=0; i<option_count; i++){
if(!strcasecmp(options[i].key, "config_file")){
2021-12-13 10:14:05 +01:00
g_config_file = mosquitto_strdup(options[i].value);
if(g_config_file == NULL){
2020-09-23 23:59:31 +02:00
return MOSQ_ERR_NOMEM;
}
break;
}
}
2021-12-13 10:14:05 +01:00
if(g_config_file == NULL){
2020-09-23 23:59:31 +02:00
mosquitto_log_printf(MOSQ_LOG_WARNING, "Warning: Dynamic security plugin has no plugin_opt_config_file defined. The plugin will not be activated.");
return MOSQ_ERR_SUCCESS;
}
plg_id = identifier;
mosquitto_plugin_set_info(identifier, "dynamic-security", NULL);
2020-09-23 23:59:31 +02:00
dynsec__config_load();
mosquitto_callback_register(plg_id, MOSQ_EVT_CONTROL, dynsec_control_callback, "$CONTROL/dynamic-security/v1", NULL);
mosquitto_callback_register(plg_id, MOSQ_EVT_BASIC_AUTH, dynsec_auth__basic_auth_callback, NULL, NULL);
mosquitto_callback_register(plg_id, MOSQ_EVT_ACL_CHECK, dynsec__acl_check_callback, NULL, NULL);
return MOSQ_ERR_SUCCESS;
}
int mosquitto_plugin_cleanup(void *user_data, struct mosquitto_opt *options, int option_count)
{
UNUSED(user_data);
UNUSED(options);
UNUSED(option_count);
2020-09-23 23:59:31 +02:00
dynsec_groups__cleanup();
dynsec_clients__cleanup();
dynsec_roles__cleanup();
2021-12-13 10:14:05 +01:00
mosquitto_free(g_config_file);
g_config_file = NULL;
2020-09-23 23:59:31 +02:00
return MOSQ_ERR_SUCCESS;
}