/src/mosquitto/plugins/dynamic-security/default_acl.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 | | int dynsec__process_set_default_acl_access(struct dynsec__data *data, struct mosquitto_control_cmd *cmd) |
32 | 0 | { |
33 | 0 | cJSON *j_actions, *j_action; |
34 | 0 | bool allow; |
35 | 0 | const char *admin_clientid, *admin_username; |
36 | 0 | const char *acltype; |
37 | |
|
38 | 0 | j_actions = cJSON_GetObjectItem(cmd->j_command, "acls"); |
39 | 0 | if(j_actions == NULL || !cJSON_IsArray(j_actions)){ |
40 | 0 | mosquitto_control_command_reply(cmd, "Missing/invalid actions array"); |
41 | 0 | return MOSQ_ERR_INVAL; |
42 | 0 | } |
43 | | |
44 | 0 | admin_clientid = mosquitto_client_id(cmd->client); |
45 | 0 | admin_username = mosquitto_client_username(cmd->client); |
46 | |
|
47 | 0 | cJSON_ArrayForEach(j_action, j_actions){ |
48 | 0 | if(json_get_string(j_action, "acltype", &acltype, false) == MOSQ_ERR_SUCCESS |
49 | 0 | && json_get_bool(j_action, "allow", &allow, false, false) == MOSQ_ERR_SUCCESS){ |
50 | |
|
51 | 0 | if(!strcasecmp(acltype, ACL_TYPE_PUB_C_SEND)){ |
52 | 0 | data->default_access.publish_c_send = allow; |
53 | 0 | }else if(!strcasecmp(acltype, ACL_TYPE_PUB_C_RECV)){ |
54 | 0 | data->default_access.publish_c_recv = allow; |
55 | 0 | }else if(!strcasecmp(acltype, ACL_TYPE_SUB_GENERIC)){ |
56 | 0 | data->default_access.subscribe = allow; |
57 | 0 | }else if(!strcasecmp(acltype, ACL_TYPE_UNSUB_GENERIC)){ |
58 | 0 | data->default_access.unsubscribe = allow; |
59 | 0 | } |
60 | 0 | mosquitto_log_printf(MOSQ_LOG_INFO, "dynsec: %s/%s | setDefaultACLAccess | acltype=%s | allow=%s", |
61 | 0 | admin_clientid, admin_username, acltype, allow?"true":"false"); |
62 | 0 | } |
63 | 0 | } |
64 | |
|
65 | 0 | dynsec__config_batch_save(data); |
66 | 0 | mosquitto_control_command_reply(cmd, NULL); |
67 | 0 | return MOSQ_ERR_SUCCESS; |
68 | 0 | } |
69 | | |
70 | | |
71 | | int dynsec__process_get_default_acl_access(struct dynsec__data *data, struct mosquitto_control_cmd *cmd) |
72 | 0 | { |
73 | 0 | cJSON *tree, *jtmp, *j_data, *j_acls, *j_acl; |
74 | 0 | const char *admin_clientid, *admin_username; |
75 | |
|
76 | 0 | tree = cJSON_CreateObject(); |
77 | 0 | if(tree == NULL){ |
78 | 0 | mosquitto_control_command_reply(cmd, "Internal error"); |
79 | 0 | return MOSQ_ERR_NOMEM; |
80 | 0 | } |
81 | | |
82 | 0 | admin_clientid = mosquitto_client_id(cmd->client); |
83 | 0 | admin_username = mosquitto_client_username(cmd->client); |
84 | 0 | mosquitto_log_printf(MOSQ_LOG_INFO, "dynsec: %s/%s | getDefaultACLAccess", |
85 | 0 | admin_clientid, admin_username); |
86 | |
|
87 | 0 | if(cJSON_AddStringToObject(tree, "command", "getDefaultACLAccess") == NULL |
88 | 0 | || ((j_data = cJSON_AddObjectToObject(tree, "data")) == NULL) |
89 | |
|
90 | 0 | ){ |
91 | 0 | goto internal_error; |
92 | 0 | } |
93 | | |
94 | 0 | j_acls = cJSON_AddArrayToObject(j_data, "acls"); |
95 | 0 | if(j_acls == NULL){ |
96 | 0 | goto internal_error; |
97 | 0 | } |
98 | | |
99 | | /* publishClientSend */ |
100 | 0 | j_acl = cJSON_CreateObject(); |
101 | 0 | if(j_acl == NULL){ |
102 | 0 | goto internal_error; |
103 | 0 | } |
104 | 0 | cJSON_AddItemToArray(j_acls, j_acl); |
105 | 0 | if(cJSON_AddStringToObject(j_acl, "acltype", ACL_TYPE_PUB_C_SEND) == NULL |
106 | 0 | || cJSON_AddBoolToObject(j_acl, "allow", data->default_access.publish_c_send) == NULL |
107 | 0 | ){ |
108 | |
|
109 | 0 | goto internal_error; |
110 | 0 | } |
111 | | |
112 | | /* publishClientReceive */ |
113 | 0 | j_acl = cJSON_CreateObject(); |
114 | 0 | if(j_acl == NULL){ |
115 | 0 | goto internal_error; |
116 | 0 | } |
117 | 0 | cJSON_AddItemToArray(j_acls, j_acl); |
118 | 0 | if(cJSON_AddStringToObject(j_acl, "acltype", ACL_TYPE_PUB_C_RECV) == NULL |
119 | 0 | || cJSON_AddBoolToObject(j_acl, "allow", data->default_access.publish_c_recv) == NULL |
120 | 0 | ){ |
121 | |
|
122 | 0 | goto internal_error; |
123 | 0 | } |
124 | | |
125 | | /* subscribe */ |
126 | 0 | j_acl = cJSON_CreateObject(); |
127 | 0 | if(j_acl == NULL){ |
128 | 0 | goto internal_error; |
129 | 0 | } |
130 | 0 | cJSON_AddItemToArray(j_acls, j_acl); |
131 | 0 | if(cJSON_AddStringToObject(j_acl, "acltype", ACL_TYPE_SUB_GENERIC) == NULL |
132 | 0 | || cJSON_AddBoolToObject(j_acl, "allow", data->default_access.subscribe) == NULL |
133 | 0 | ){ |
134 | |
|
135 | 0 | goto internal_error; |
136 | 0 | } |
137 | | |
138 | | /* unsubscribe */ |
139 | 0 | j_acl = cJSON_CreateObject(); |
140 | 0 | if(j_acl == NULL){ |
141 | 0 | goto internal_error; |
142 | 0 | } |
143 | 0 | cJSON_AddItemToArray(j_acls, j_acl); |
144 | 0 | if(cJSON_AddStringToObject(j_acl, "acltype", ACL_TYPE_UNSUB_GENERIC) == NULL |
145 | 0 | || cJSON_AddBoolToObject(j_acl, "allow", data->default_access.unsubscribe) == NULL |
146 | 0 | ){ |
147 | |
|
148 | 0 | goto internal_error; |
149 | 0 | } |
150 | | |
151 | 0 | cJSON_AddItemToArray(cmd->j_responses, tree); |
152 | |
|
153 | 0 | if(cmd->correlation_data){ |
154 | 0 | jtmp = cJSON_AddStringToObject(tree, "correlationData", cmd->correlation_data); |
155 | 0 | if(jtmp == NULL){ |
156 | 0 | goto internal_error; |
157 | 0 | } |
158 | 0 | } |
159 | | |
160 | 0 | return MOSQ_ERR_SUCCESS; |
161 | | |
162 | 0 | internal_error: |
163 | 0 | cJSON_Delete(tree); |
164 | 0 | mosquitto_control_command_reply(cmd, "Internal error"); |
165 | 0 | return MOSQ_ERR_NOMEM; |
166 | 0 | } |