grouplist.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. Copyright (c) 2020 Roger Light <roger@atchoo.org>
  3. All rights reserved. This program and the accompanying materials
  4. are made available under the terms of the Eclipse Public License 2.0
  5. and Eclipse Distribution License v1.0 which accompany this distribution.
  6. The Eclipse Public License is available at
  7. https://www.eclipse.org/legal/epl-2.0/
  8. and the Eclipse Distribution License is available at
  9. http://www.eclipse.org/org/documents/edl-v10.php.
  10. SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
  11. Contributors:
  12. Roger Light - initial implementation and documentation.
  13. */
  14. #include "config.h"
  15. #include <cjson/cJSON.h>
  16. #include <stdio.h>
  17. #include <uthash.h>
  18. #include "mosquitto.h"
  19. #include "mosquitto_broker.h"
  20. #include "json_help.h"
  21. #include "dynamic_security.h"
  22. /* ################################################################
  23. * #
  24. * # Plugin global variables
  25. * #
  26. * ################################################################ */
  27. /* ################################################################
  28. * #
  29. * # Function declarations
  30. * #
  31. * ################################################################ */
  32. /* ################################################################
  33. * #
  34. * # Local variables
  35. * #
  36. * ################################################################ */
  37. /* ################################################################
  38. * #
  39. * # Utility functions
  40. * #
  41. * ################################################################ */
  42. static int dynsec_grouplist__cmp(void *a, void *b)
  43. {
  44. int prio;
  45. struct dynsec__grouplist *grouplist_a = a;
  46. struct dynsec__grouplist *grouplist_b = b;
  47. prio = grouplist_b->priority - grouplist_a->priority;
  48. if(prio == 0){
  49. return strcmp(grouplist_a->group->groupname, grouplist_b->group->groupname);
  50. }else{
  51. return prio;
  52. }
  53. }
  54. cJSON *dynsec_grouplist__all_to_json(struct dynsec__grouplist *base_grouplist)
  55. {
  56. struct dynsec__grouplist *grouplist, *grouplist_tmp;
  57. cJSON *j_groups, *j_group;
  58. j_groups = cJSON_CreateArray();
  59. if(j_groups == NULL) return NULL;
  60. HASH_ITER(hh, base_grouplist, grouplist, grouplist_tmp){
  61. j_group = cJSON_CreateObject();
  62. if(j_group == NULL){
  63. cJSON_Delete(j_groups);
  64. return NULL;
  65. }
  66. cJSON_AddItemToArray(j_groups, j_group);
  67. if(cJSON_AddStringToObject(j_group, "groupname", grouplist->group->groupname) == NULL
  68. || (grouplist->priority != -1 && cJSON_AddIntToObject(j_group, "priority", grouplist->priority) == NULL)
  69. ){
  70. cJSON_Delete(j_groups);
  71. return NULL;
  72. }
  73. }
  74. return j_groups;
  75. }
  76. int dynsec_grouplist__add(struct dynsec__grouplist **base_grouplist, struct dynsec__group *group, int priority)
  77. {
  78. struct dynsec__grouplist *grouplist;
  79. HASH_FIND(hh, *base_grouplist, group->groupname, strlen(group->groupname), grouplist);
  80. if(grouplist != NULL){
  81. /* Group is already in the list */
  82. return MOSQ_ERR_SUCCESS;
  83. }
  84. grouplist = mosquitto_malloc(sizeof(struct dynsec__grouplist));
  85. if(grouplist == NULL){
  86. return MOSQ_ERR_NOMEM;
  87. }
  88. grouplist->group = group;
  89. grouplist->priority = priority;
  90. HASH_ADD_KEYPTR_INORDER(hh, *base_grouplist, grouplist->group->groupname, strlen(grouplist->group->groupname), grouplist, dynsec_grouplist__cmp);
  91. return MOSQ_ERR_SUCCESS;
  92. }
  93. void dynsec_grouplist__cleanup(struct dynsec__grouplist **base_grouplist)
  94. {
  95. struct dynsec__grouplist *grouplist, *grouplist_tmp;
  96. HASH_ITER(hh, *base_grouplist, grouplist, grouplist_tmp){
  97. HASH_DELETE(hh, *base_grouplist, grouplist);
  98. mosquitto_free(grouplist);
  99. }
  100. }
  101. void dynsec_grouplist__remove(struct dynsec__grouplist **base_grouplist, struct dynsec__group *group)
  102. {
  103. struct dynsec__grouplist *grouplist;
  104. HASH_FIND(hh, *base_grouplist, group->groupname, strlen(group->groupname), grouplist);
  105. if(grouplist){
  106. HASH_DELETE(hh, *base_grouplist, grouplist);
  107. mosquitto_free(grouplist);
  108. }
  109. }