Coverage Report

Created: 2025-11-14 07:11

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