dynsec_role.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 <stdlib.h>
  18. #include <string.h>
  19. #include "mosquitto.h"
  20. #include "mosquitto_ctrl.h"
  21. #include "password_mosq.h"
  22. int dynsec_role__create(int argc, char *argv[], cJSON *j_command)
  23. {
  24. char *rolename = NULL;
  25. if(argc == 1){
  26. rolename = argv[0];
  27. }else{
  28. return MOSQ_ERR_INVAL;
  29. }
  30. if(cJSON_AddStringToObject(j_command, "command", "createRole") == NULL
  31. || cJSON_AddStringToObject(j_command, "rolename", rolename) == NULL
  32. ){
  33. return MOSQ_ERR_NOMEM;
  34. }else{
  35. return MOSQ_ERR_SUCCESS;
  36. }
  37. }
  38. int dynsec_role__delete(int argc, char *argv[], cJSON *j_command)
  39. {
  40. char *rolename = NULL;
  41. if(argc == 1){
  42. rolename = argv[0];
  43. }else{
  44. return MOSQ_ERR_INVAL;
  45. }
  46. if(cJSON_AddStringToObject(j_command, "command", "deleteRole") == NULL
  47. || cJSON_AddStringToObject(j_command, "rolename", rolename) == NULL
  48. ){
  49. return MOSQ_ERR_NOMEM;
  50. }else{
  51. return MOSQ_ERR_SUCCESS;
  52. }
  53. }
  54. int dynsec_role__get(int argc, char *argv[], cJSON *j_command)
  55. {
  56. char *rolename = NULL;
  57. if(argc == 1){
  58. rolename = argv[0];
  59. }else{
  60. return MOSQ_ERR_INVAL;
  61. }
  62. if(cJSON_AddStringToObject(j_command, "command", "getRole") == NULL
  63. || cJSON_AddStringToObject(j_command, "rolename", rolename) == NULL
  64. ){
  65. return MOSQ_ERR_NOMEM;
  66. }else{
  67. return MOSQ_ERR_SUCCESS;
  68. }
  69. }
  70. int dynsec_role__list_all(int argc, char *argv[], cJSON *j_command)
  71. {
  72. int count = -1, offset = -1;
  73. if(argc == 0){
  74. /* All roles */
  75. }else if(argc == 1){
  76. count = atoi(argv[0]);
  77. }else if(argc == 2){
  78. count = atoi(argv[0]);
  79. offset = atoi(argv[1]);
  80. }else{
  81. return MOSQ_ERR_INVAL;
  82. }
  83. if(cJSON_AddStringToObject(j_command, "command", "listRoles") == NULL
  84. || (count > 0 && cJSON_AddIntToObject(j_command, "count", count) == NULL)
  85. || (offset > 0 && cJSON_AddIntToObject(j_command, "offset", offset) == NULL)
  86. ){
  87. return MOSQ_ERR_NOMEM;
  88. }else{
  89. return MOSQ_ERR_SUCCESS;
  90. }
  91. }
  92. int dynsec_role__add_acl(int argc, char *argv[], cJSON *j_command)
  93. {
  94. char *rolename, *acltype, *topic, *action;
  95. bool allow;
  96. int priority = -1;
  97. if(argc == 5){
  98. rolename = argv[0];
  99. acltype = argv[1];
  100. topic = argv[2];
  101. action = argv[3];
  102. priority = atoi(argv[4]);
  103. }else if(argc == 4){
  104. rolename = argv[0];
  105. acltype = argv[1];
  106. topic = argv[2];
  107. action = argv[3];
  108. }else{
  109. return MOSQ_ERR_INVAL;
  110. }
  111. if(strcasecmp(acltype, "publishClientSend")
  112. && strcasecmp(acltype, "publishClientReceive")
  113. && strcasecmp(acltype, "subscribeLiteral")
  114. && strcasecmp(acltype, "subscribePattern")
  115. && strcasecmp(acltype, "unsubscribeLiteral")
  116. && strcasecmp(acltype, "unsubscribePattern")){
  117. return MOSQ_ERR_INVAL;
  118. }
  119. if(!strcasecmp(action, "allow")){
  120. allow = true;
  121. }else if(!strcasecmp(action, "deny")){
  122. allow = false;
  123. }else{
  124. return MOSQ_ERR_INVAL;
  125. }
  126. if(cJSON_AddStringToObject(j_command, "command", "addRoleACL") == NULL
  127. || cJSON_AddStringToObject(j_command, "rolename", rolename) == NULL
  128. || cJSON_AddStringToObject(j_command, "acltype", acltype) == NULL
  129. || cJSON_AddStringToObject(j_command, "topic", topic) == NULL
  130. || cJSON_AddBoolToObject(j_command, "allow", allow) == NULL
  131. || (priority != -1 && cJSON_AddIntToObject(j_command, "priority", priority) == NULL)
  132. ){
  133. return MOSQ_ERR_NOMEM;
  134. }else{
  135. return MOSQ_ERR_SUCCESS;
  136. }
  137. }
  138. int dynsec_role__remove_acl(int argc, char *argv[], cJSON *j_command)
  139. {
  140. char *rolename, *acltype, *topic;
  141. if(argc == 3){
  142. rolename = argv[0];
  143. acltype = argv[1];
  144. topic = argv[2];
  145. }else{
  146. return MOSQ_ERR_INVAL;
  147. }
  148. if(strcasecmp(acltype, "publishClientSend")
  149. && strcasecmp(acltype, "publishClientReceive")
  150. && strcasecmp(acltype, "subscribeLiteral")
  151. && strcasecmp(acltype, "subscribePattern")
  152. && strcasecmp(acltype, "unsubscribeLiteral")
  153. && strcasecmp(acltype, "unsubscribePattern")){
  154. return MOSQ_ERR_INVAL;
  155. }
  156. if(cJSON_AddStringToObject(j_command, "command", "removeRoleACL") == NULL
  157. || cJSON_AddStringToObject(j_command, "rolename", rolename) == NULL
  158. || cJSON_AddStringToObject(j_command, "acltype", acltype) == NULL
  159. || cJSON_AddStringToObject(j_command, "topic", topic) == NULL
  160. ){
  161. return MOSQ_ERR_NOMEM;
  162. }else{
  163. return MOSQ_ERR_SUCCESS;
  164. }
  165. }