123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253 |
- #include "config.h"
- #include "dynamic_security.h"
- #include "mosquitto.h"
- #include "mosquitto_broker.h"
- #include "mosquitto_plugin.h"
- typedef int (*MOSQ_FUNC_acl_check)(struct mosquitto_evt_acl_check *, struct dynsec__rolelist *);
- static int acl_check_publish_c_recv(struct mosquitto_evt_acl_check *ed, struct dynsec__rolelist *base_rolelist)
- {
- struct dynsec__rolelist *rolelist, *rolelist_tmp = NULL;
- struct dynsec__acl *acl, *acl_tmp = NULL;
- bool result;
- HASH_ITER(hh, base_rolelist, rolelist, rolelist_tmp){
- HASH_ITER(hh, rolelist->role->acls.publish_c_recv, acl, acl_tmp){
- mosquitto_topic_matches_sub(acl->topic, ed->topic, &result);
- if(result){
- if(acl->allow){
- return MOSQ_ERR_SUCCESS;
- }else{
- return MOSQ_ERR_ACL_DENIED;
- }
- }
- }
- }
- return MOSQ_ERR_NOT_FOUND;
- }
- static int acl_check_publish_c_send(struct mosquitto_evt_acl_check *ed, struct dynsec__rolelist *base_rolelist)
- {
- struct dynsec__rolelist *rolelist, *rolelist_tmp = NULL;
- struct dynsec__acl *acl, *acl_tmp = NULL;
- bool result;
- HASH_ITER(hh, base_rolelist, rolelist, rolelist_tmp){
- HASH_ITER(hh, rolelist->role->acls.publish_c_send, acl, acl_tmp){
- mosquitto_topic_matches_sub(acl->topic, ed->topic, &result);
- if(result){
- if(acl->allow){
- return MOSQ_ERR_SUCCESS;
- }else{
- return MOSQ_ERR_ACL_DENIED;
- }
- }
- }
- }
- return MOSQ_ERR_NOT_FOUND;
- }
- static int acl_check_subscribe(struct mosquitto_evt_acl_check *ed, struct dynsec__rolelist *base_rolelist)
- {
- struct dynsec__rolelist *rolelist, *rolelist_tmp = NULL;
- struct dynsec__acl *acl, *acl_tmp = NULL;
- size_t len;
- len = strlen(ed->topic);
- HASH_ITER(hh, base_rolelist, rolelist, rolelist_tmp){
- HASH_FIND(hh, rolelist->role->acls.subscribe_literal, ed->topic, len, acl);
- if(acl){
- if(acl->allow){
- return MOSQ_ERR_SUCCESS;
- }else{
- return MOSQ_ERR_ACL_DENIED;
- }
- }
- HASH_ITER(hh, rolelist->role->acls.subscribe_pattern, acl, acl_tmp){
- if(sub_acl_check(acl->topic, ed->topic)){
- if(acl->allow){
- return MOSQ_ERR_SUCCESS;
- }else{
- return MOSQ_ERR_ACL_DENIED;
- }
- }
- }
- }
- return MOSQ_ERR_NOT_FOUND;
- }
- static int acl_check_unsubscribe(struct mosquitto_evt_acl_check *ed, struct dynsec__rolelist *base_rolelist)
- {
- struct dynsec__rolelist *rolelist, *rolelist_tmp = NULL;
- struct dynsec__acl *acl, *acl_tmp = NULL;
- size_t len;
- len = strlen(ed->topic);
- HASH_ITER(hh, base_rolelist, rolelist, rolelist_tmp){
- HASH_FIND(hh, rolelist->role->acls.unsubscribe_literal, ed->topic, len, acl);
- if(acl){
- if(acl->allow){
- return MOSQ_ERR_SUCCESS;
- }else{
- return MOSQ_ERR_ACL_DENIED;
- }
- }
- HASH_ITER(hh, rolelist->role->acls.unsubscribe_pattern, acl, acl_tmp){
- if(sub_acl_check(acl->topic, ed->topic)){
- if(acl->allow){
- return MOSQ_ERR_SUCCESS;
- }else{
- return MOSQ_ERR_ACL_DENIED;
- }
- }
- }
- }
- return MOSQ_ERR_NOT_FOUND;
- }
- static int acl_check(struct mosquitto_evt_acl_check *ed, MOSQ_FUNC_acl_check check, bool acl_default_access)
- {
- struct dynsec__client *client;
- struct dynsec__grouplist *grouplist, *grouplist_tmp = NULL;
- const char *username;
- int rc;
- username = mosquitto_client_username(ed->client);
- if(username){
- client = dynsec_clients__find(username);
- if(client == NULL) return MOSQ_ERR_PLUGIN_DEFER;
-
- rc = check(ed, client->rolelist);
- if(rc != MOSQ_ERR_NOT_FOUND){
- return rc;
- }
- HASH_ITER(hh, client->grouplist, grouplist, grouplist_tmp){
- rc = check(ed, grouplist->group->rolelist);
- if(rc != MOSQ_ERR_NOT_FOUND){
- return rc;
- }
- }
- }else if(dynsec_anonymous_group){
-
- rc = check(ed, dynsec_anonymous_group->rolelist);
- if(rc != MOSQ_ERR_NOT_FOUND){
- return rc;
- }
- }
- if(acl_default_access == false){
- return MOSQ_ERR_PLUGIN_DEFER;
- }else{
- if(!strncmp(ed->topic, "$CONTROL", strlen("$CONTROL"))){
-
- return MOSQ_ERR_PLUGIN_DEFER;
- }else{
- return MOSQ_ERR_SUCCESS;
- }
- }
- }
- int dynsec__acl_check_callback(int event, void *event_data, void *userdata)
- {
- struct mosquitto_evt_acl_check *ed = event_data;
- UNUSED(event);
- UNUSED(userdata);
-
- switch(ed->access){
- case MOSQ_ACL_SUBSCRIBE:
- return acl_check(event_data, acl_check_subscribe, default_access.subscribe);
- break;
- case MOSQ_ACL_UNSUBSCRIBE:
- return acl_check(event_data, acl_check_unsubscribe, default_access.unsubscribe);
- break;
- case MOSQ_ACL_WRITE:
- return acl_check(event_data, acl_check_publish_c_send, default_access.publish_c_send);
- break;
- case MOSQ_ACL_READ:
- return acl_check(event_data, acl_check_publish_c_recv, default_access.publish_c_recv);
- break;
- default:
- return MOSQ_ERR_PLUGIN_DEFER;
- }
- return MOSQ_ERR_PLUGIN_DEFER;
- }
|