/src/mosquitto/plugins/dynamic-security/grouplist.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 <stdio.h> |
23 | | #include <uthash.h> |
24 | | |
25 | | #include "dynamic_security.h" |
26 | | #include "json_help.h" |
27 | | |
28 | | |
29 | | /* ################################################################ |
30 | | * # |
31 | | * # Plugin global variables |
32 | | * # |
33 | | * ################################################################ */ |
34 | | |
35 | | |
36 | | /* ################################################################ |
37 | | * # |
38 | | * # Function declarations |
39 | | * # |
40 | | * ################################################################ */ |
41 | | |
42 | | |
43 | | /* ################################################################ |
44 | | * # |
45 | | * # Local variables |
46 | | * # |
47 | | * ################################################################ */ |
48 | | |
49 | | |
50 | | /* ################################################################ |
51 | | * # |
52 | | * # Utility functions |
53 | | * # |
54 | | * ################################################################ */ |
55 | | |
56 | | |
57 | | static int dynsec_grouplist__cmp(void *a, void *b) |
58 | 144k | { |
59 | 144k | int prio; |
60 | 144k | struct dynsec__grouplist *grouplist_a = a; |
61 | 144k | struct dynsec__grouplist *grouplist_b = b; |
62 | | |
63 | 144k | prio = grouplist_b->priority - grouplist_a->priority; |
64 | 144k | if(prio == 0){ |
65 | 143k | return strcmp(grouplist_a->group->groupname, grouplist_b->group->groupname); |
66 | 143k | }else{ |
67 | 647 | return prio; |
68 | 647 | } |
69 | 144k | } |
70 | | |
71 | | |
72 | | cJSON *dynsec_grouplist__all_to_json(struct dynsec__grouplist *base_grouplist) |
73 | 0 | { |
74 | 0 | struct dynsec__grouplist *grouplist, *grouplist_tmp; |
75 | 0 | cJSON *j_groups, *j_group; |
76 | |
|
77 | 0 | j_groups = cJSON_CreateArray(); |
78 | 0 | if(j_groups == NULL){ |
79 | 0 | return NULL; |
80 | 0 | } |
81 | | |
82 | 0 | HASH_ITER(hh, base_grouplist, grouplist, grouplist_tmp){ |
83 | 0 | j_group = cJSON_CreateObject(); |
84 | 0 | if(j_group == NULL){ |
85 | 0 | cJSON_Delete(j_groups); |
86 | 0 | return NULL; |
87 | 0 | } |
88 | 0 | cJSON_AddItemToArray(j_groups, j_group); |
89 | |
|
90 | 0 | if(cJSON_AddStringToObject(j_group, "groupname", grouplist->group->groupname) == NULL |
91 | 0 | || (grouplist->priority != -1 && cJSON_AddIntToObject(j_group, "priority", grouplist->priority) == NULL) |
92 | 0 | ){ |
93 | |
|
94 | 0 | cJSON_Delete(j_groups); |
95 | 0 | return NULL; |
96 | 0 | } |
97 | 0 | } |
98 | 0 | return j_groups; |
99 | 0 | } |
100 | | |
101 | | |
102 | | int dynsec_grouplist__add(struct dynsec__grouplist **base_grouplist, struct dynsec__group *group, int priority) |
103 | 33.5k | { |
104 | 33.5k | struct dynsec__grouplist *grouplist; |
105 | | |
106 | 33.5k | HASH_FIND(hh, *base_grouplist, group->groupname, strlen(group->groupname), grouplist); |
107 | 33.5k | if(grouplist != NULL){ |
108 | | /* Group is already in the list */ |
109 | 0 | return MOSQ_ERR_SUCCESS; |
110 | 0 | } |
111 | | |
112 | 33.5k | grouplist = mosquitto_malloc(sizeof(struct dynsec__grouplist)); |
113 | 33.5k | if(grouplist == NULL){ |
114 | 0 | return MOSQ_ERR_NOMEM; |
115 | 0 | } |
116 | | |
117 | 33.5k | grouplist->group = group; |
118 | 33.5k | grouplist->priority = priority; |
119 | 33.5k | HASH_ADD_KEYPTR_INORDER(hh, *base_grouplist, grouplist->group->groupname, strlen(grouplist->group->groupname), grouplist, dynsec_grouplist__cmp); |
120 | | |
121 | 33.5k | return MOSQ_ERR_SUCCESS; |
122 | 33.5k | } |
123 | | |
124 | | |
125 | | void dynsec_grouplist__cleanup(struct dynsec__grouplist **base_grouplist) |
126 | 42.7k | { |
127 | 42.7k | struct dynsec__grouplist *grouplist, *grouplist_tmp; |
128 | | |
129 | 42.7k | HASH_ITER(hh, *base_grouplist, grouplist, grouplist_tmp){ |
130 | 11.6k | HASH_DELETE(hh, *base_grouplist, grouplist); |
131 | 11.6k | mosquitto_free(grouplist); |
132 | 11.6k | } |
133 | 42.7k | } |
134 | | |
135 | | |
136 | | void dynsec_grouplist__remove(struct dynsec__grouplist **base_grouplist, struct dynsec__group *group) |
137 | 21.8k | { |
138 | 21.8k | struct dynsec__grouplist *grouplist; |
139 | | |
140 | 21.8k | HASH_FIND(hh, *base_grouplist, group->groupname, strlen(group->groupname), grouplist); |
141 | 21.8k | if(grouplist){ |
142 | | HASH_DELETE(hh, *base_grouplist, grouplist); |
143 | 21.8k | mosquitto_free(grouplist); |
144 | 21.8k | } |
145 | 21.8k | } |