Coverage Report

Created: 2023-09-19 06:58

/src/mosquitto/plugins/dynamic-security/control.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
Copyright (c) 2020-2021 Roger Light <roger@atchoo.org>
3
4
All rights reserved. This program and the accompanying materials
5
are made available under the terms of the Eclipse Public License 2.0
6
and Eclipse Distribution License v1.0 which accompany this distribution.
7
8
The Eclipse Public License is available at
9
   https://www.eclipse.org/legal/epl-2.0/
10
and the Eclipse Distribution License is available at
11
  http://www.eclipse.org/org/documents/edl-v10.php.
12
13
SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
14
15
Contributors:
16
   Roger Light - initial implementation and documentation.
17
*/
18
19
#include "config.h"
20
21
#include <cjson/cJSON.h>
22
#include <errno.h>
23
#include <stdio.h>
24
#include <stdlib.h>
25
#include <string.h>
26
#include <sys/stat.h>
27
28
#include "json_help.h"
29
#include "mosquitto.h"
30
#include "mosquitto_broker.h"
31
#include "mosquitto_plugin.h"
32
#include "mqtt_protocol.h"
33
34
#include "dynamic_security.h"
35
36
0
#define RESPONSE_TOPIC "$CONTROL/dynamic-security/v1/response"
37
38
static int dynsec__handle_command(struct mosquitto_control_cmd *cmd, struct mosquitto *context, void *userdata)
39
0
{
40
0
  struct dynsec__data *data = userdata;
41
0
  int rc = MOSQ_ERR_SUCCESS;
42
43
  /* Plugin */
44
0
  if(!strcasecmp(cmd->command_name, "setDefaultACLAccess")){
45
0
    rc = dynsec__process_set_default_acl_access(data, cmd, context);
46
0
  }else if(!strcasecmp(cmd->command_name, "getDefaultACLAccess")){
47
0
    rc = dynsec__process_get_default_acl_access(data, cmd, context);
48
49
  /* Clients */
50
0
  }else if(!strcasecmp(cmd->command_name, "createClient")){
51
0
    rc = dynsec_clients__process_create(data, cmd, context);
52
0
  }else if(!strcasecmp(cmd->command_name, "deleteClient")){
53
0
    rc = dynsec_clients__process_delete(data, cmd, context);
54
0
  }else if(!strcasecmp(cmd->command_name, "getClient")){
55
0
    rc = dynsec_clients__process_get(data, cmd, context);
56
0
  }else if(!strcasecmp(cmd->command_name, "listClients")){
57
0
    rc = dynsec_clients__process_list(data, cmd, context);
58
0
  }else if(!strcasecmp(cmd->command_name, "modifyClient")){
59
0
    rc = dynsec_clients__process_modify(data, cmd, context);
60
0
  }else if(!strcasecmp(cmd->command_name, "setClientPassword")){
61
0
    rc = dynsec_clients__process_set_password(data, cmd, context);
62
0
  }else if(!strcasecmp(cmd->command_name, "setClientId")){
63
0
    rc = dynsec_clients__process_set_id(data, cmd, context);
64
0
  }else if(!strcasecmp(cmd->command_name, "addClientRole")){
65
0
    rc = dynsec_clients__process_add_role(data, cmd, context);
66
0
  }else if(!strcasecmp(cmd->command_name, "removeClientRole")){
67
0
    rc = dynsec_clients__process_remove_role(data, cmd, context);
68
0
  }else if(!strcasecmp(cmd->command_name, "enableClient")){
69
0
    rc = dynsec_clients__process_enable(data, cmd, context);
70
0
  }else if(!strcasecmp(cmd->command_name, "disableClient")){
71
0
    rc = dynsec_clients__process_disable(data, cmd, context);
72
73
  /* Groups */
74
0
  }else if(!strcasecmp(cmd->command_name, "addGroupClient")){
75
0
    rc = dynsec_groups__process_add_client(data, cmd, context);
76
0
  }else if(!strcasecmp(cmd->command_name, "createGroup")){
77
0
    rc = dynsec_groups__process_create(data, cmd, context);
78
0
  }else if(!strcasecmp(cmd->command_name, "deleteGroup")){
79
0
    rc = dynsec_groups__process_delete(data, cmd, context);
80
0
  }else if(!strcasecmp(cmd->command_name, "getGroup")){
81
0
    rc = dynsec_groups__process_get(data, cmd, context);
82
0
  }else if(!strcasecmp(cmd->command_name, "listGroups")){
83
0
    rc = dynsec_groups__process_list(data, cmd, context);
84
0
  }else if(!strcasecmp(cmd->command_name, "modifyGroup")){
85
0
    rc = dynsec_groups__process_modify(data, cmd, context);
86
0
  }else if(!strcasecmp(cmd->command_name, "removeGroupClient")){
87
0
    rc = dynsec_groups__process_remove_client(data, cmd, context);
88
0
  }else if(!strcasecmp(cmd->command_name, "addGroupRole")){
89
0
    rc = dynsec_groups__process_add_role(data, cmd, context);
90
0
  }else if(!strcasecmp(cmd->command_name, "removeGroupRole")){
91
0
    rc = dynsec_groups__process_remove_role(data, cmd, context);
92
0
  }else if(!strcasecmp(cmd->command_name, "setAnonymousGroup")){
93
0
    rc = dynsec_groups__process_set_anonymous_group(data, cmd, context);
94
0
  }else if(!strcasecmp(cmd->command_name, "getAnonymousGroup")){
95
0
    rc = dynsec_groups__process_get_anonymous_group(data, cmd, context);
96
97
  /* Roles */
98
0
  }else if(!strcasecmp(cmd->command_name, "createRole")){
99
0
    rc = dynsec_roles__process_create(data, cmd, context);
100
0
  }else if(!strcasecmp(cmd->command_name, "getRole")){
101
0
    rc = dynsec_roles__process_get(data, cmd, context);
102
0
  }else if(!strcasecmp(cmd->command_name, "listRoles")){
103
0
    rc = dynsec_roles__process_list(data, cmd, context);
104
0
  }else if(!strcasecmp(cmd->command_name, "modifyRole")){
105
0
    rc = dynsec_roles__process_modify(data, cmd, context);
106
0
  }else if(!strcasecmp(cmd->command_name, "deleteRole")){
107
0
    rc = dynsec_roles__process_delete(data, cmd, context);
108
0
  }else if(!strcasecmp(cmd->command_name, "addRoleACL")){
109
0
    rc = dynsec_roles__process_add_acl(data, cmd, context);
110
0
  }else if(!strcasecmp(cmd->command_name, "removeRoleACL")){
111
0
    rc = dynsec_roles__process_remove_acl(data, cmd, context);
112
113
  /* Unknown */
114
0
  }else{
115
0
    mosquitto_control_command_reply(cmd, "Unknown command");
116
0
    rc = MOSQ_ERR_INVAL;
117
0
  }
118
119
0
  return rc;
120
0
}
121
122
123
int dynsec_control_callback(int event, void *event_data, void *userdata)
124
0
{
125
0
  struct mosquitto_evt_control *ed = event_data;
126
0
  struct dynsec__data *data = userdata;
127
0
  int rc;
128
129
0
  UNUSED(event);
130
131
0
  data->need_save = false;
132
0
  rc = mosquitto_control_generic_callback(ed, RESPONSE_TOPIC, userdata, dynsec__handle_command);
133
0
  if(rc == MOSQ_ERR_SUCCESS && data->need_save){
134
0
    dynsec__config_save(data);
135
0
  }
136
0
  return rc;
137
0
}