Coverage Report

Created: 2025-08-29 06:24

/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 "dynamic_security.h"
29
#include "json_help.h"
30
31
0
#define RESPONSE_TOPIC "$CONTROL/dynamic-security/v1/response"
32
33
static int dynsec__handle_command(struct mosquitto_control_cmd *cmd, void *userdata)
34
0
{
35
0
  struct dynsec__data *data = userdata;
36
0
  int rc = MOSQ_ERR_SUCCESS;
37
38
  /* Plugin */
39
0
  if(!strcasecmp(cmd->command_name, "setDefaultACLAccess")){
40
0
    rc = dynsec__process_set_default_acl_access(data, cmd);
41
0
  }else if(!strcasecmp(cmd->command_name, "getDefaultACLAccess")){
42
0
    rc = dynsec__process_get_default_acl_access(data, cmd);
43
0
  }else if(!strcasecmp(cmd->command_name, "getDetails")){
44
0
    rc = dynsec_details__process_get(data, cmd);
45
46
  /* Clients */
47
0
  }else if(!strcasecmp(cmd->command_name, "createClient")){
48
0
    rc = dynsec_clients__process_create(data, cmd);
49
0
  }else if(!strcasecmp(cmd->command_name, "deleteClient")){
50
0
    rc = dynsec_clients__process_delete(data, cmd);
51
0
  }else if(!strcasecmp(cmd->command_name, "getClient")){
52
0
    rc = dynsec_clients__process_get(data, cmd);
53
0
  }else if(!strcasecmp(cmd->command_name, "listClients")){
54
0
    rc = dynsec_clients__process_list(data, cmd);
55
0
  }else if(!strcasecmp(cmd->command_name, "modifyClient")){
56
0
    rc = dynsec_clients__process_modify(data, cmd);
57
0
  }else if(!strcasecmp(cmd->command_name, "setClientPassword")){
58
0
    rc = dynsec_clients__process_set_password(data, cmd);
59
0
  }else if(!strcasecmp(cmd->command_name, "setClientId")){
60
0
    rc = dynsec_clients__process_set_id(data, cmd);
61
0
  }else if(!strcasecmp(cmd->command_name, "addClientRole")){
62
0
    rc = dynsec_clients__process_add_role(data, cmd);
63
0
  }else if(!strcasecmp(cmd->command_name, "removeClientRole")){
64
0
    rc = dynsec_clients__process_remove_role(data, cmd);
65
0
  }else if(!strcasecmp(cmd->command_name, "enableClient")){
66
0
    rc = dynsec_clients__process_enable(data, cmd);
67
0
  }else if(!strcasecmp(cmd->command_name, "disableClient")){
68
0
    rc = dynsec_clients__process_disable(data, cmd);
69
70
  /* Groups */
71
0
  }else if(!strcasecmp(cmd->command_name, "addGroupClient")){
72
0
    rc = dynsec_groups__process_add_client(data, cmd);
73
0
  }else if(!strcasecmp(cmd->command_name, "createGroup")){
74
0
    rc = dynsec_groups__process_create(data, cmd);
75
0
  }else if(!strcasecmp(cmd->command_name, "deleteGroup")){
76
0
    rc = dynsec_groups__process_delete(data, cmd);
77
0
  }else if(!strcasecmp(cmd->command_name, "getGroup")){
78
0
    rc = dynsec_groups__process_get(data, cmd);
79
0
  }else if(!strcasecmp(cmd->command_name, "listGroups")){
80
0
    rc = dynsec_groups__process_list(data, cmd);
81
0
  }else if(!strcasecmp(cmd->command_name, "modifyGroup")){
82
0
    rc = dynsec_groups__process_modify(data, cmd);
83
0
  }else if(!strcasecmp(cmd->command_name, "removeGroupClient")){
84
0
    rc = dynsec_groups__process_remove_client(data, cmd);
85
0
  }else if(!strcasecmp(cmd->command_name, "addGroupRole")){
86
0
    rc = dynsec_groups__process_add_role(data, cmd);
87
0
  }else if(!strcasecmp(cmd->command_name, "removeGroupRole")){
88
0
    rc = dynsec_groups__process_remove_role(data, cmd);
89
0
  }else if(!strcasecmp(cmd->command_name, "setAnonymousGroup")){
90
0
    rc = dynsec_groups__process_set_anonymous_group(data, cmd);
91
0
  }else if(!strcasecmp(cmd->command_name, "getAnonymousGroup")){
92
0
    rc = dynsec_groups__process_get_anonymous_group(data, cmd);
93
94
  /* Roles */
95
0
  }else if(!strcasecmp(cmd->command_name, "createRole")){
96
0
    rc = dynsec_roles__process_create(data, cmd);
97
0
  }else if(!strcasecmp(cmd->command_name, "getRole")){
98
0
    rc = dynsec_roles__process_get(data, cmd);
99
0
  }else if(!strcasecmp(cmd->command_name, "listRoles")){
100
0
    rc = dynsec_roles__process_list(data, cmd);
101
0
  }else if(!strcasecmp(cmd->command_name, "modifyRole")){
102
0
    rc = dynsec_roles__process_modify(data, cmd);
103
0
  }else if(!strcasecmp(cmd->command_name, "deleteRole")){
104
0
    rc = dynsec_roles__process_delete(data, cmd);
105
0
  }else if(!strcasecmp(cmd->command_name, "addRoleACL")){
106
0
    rc = dynsec_roles__process_add_acl(data, cmd);
107
0
  }else if(!strcasecmp(cmd->command_name, "removeRoleACL")){
108
0
    rc = dynsec_roles__process_remove_acl(data, cmd);
109
110
  /* Unknown */
111
0
  }else{
112
0
    mosquitto_control_command_reply(cmd, "Unknown command");
113
0
    rc = MOSQ_ERR_INVAL;
114
0
  }
115
116
0
  return rc;
117
0
}
118
119
120
int dynsec_control_callback(int event, void *event_data, void *userdata)
121
0
{
122
0
  struct mosquitto_evt_control *ed = event_data;
123
0
  struct dynsec__data *data = userdata;
124
0
  int rc;
125
126
0
  UNUSED(event);
127
128
0
  data->need_save = false;
129
0
  rc = mosquitto_control_generic_callback(ed, RESPONSE_TOPIC, userdata, dynsec__handle_command);
130
0
  if(rc == MOSQ_ERR_SUCCESS && data->need_save){
131
0
    dynsec__config_save(data);
132
0
  }
133
0
  return rc;
134
0
}