123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199 |
- /*
- Copyright (c) 2020 Roger Light <roger@atchoo.org>
- All rights reserved. This program and the accompanying materials
- are made available under the terms of the Eclipse Public License 2.0
- and Eclipse Distribution License v1.0 which accompany this distribution.
- The Eclipse Public License is available at
- https://www.eclipse.org/legal/epl-2.0/
- and the Eclipse Distribution License is available at
- http://www.eclipse.org/org/documents/edl-v10.php.
- SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
- Contributors:
- Roger Light - initial implementation and documentation.
- */
- #include "config.h"
- #include <cjson/cJSON.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include "mosquitto.h"
- #include "mosquitto_ctrl.h"
- #include "password_mosq.h"
- int dynsec_role__create(int argc, char *argv[], cJSON *j_command)
- {
- char *rolename = NULL;
- if(argc == 1){
- rolename = argv[0];
- }else{
- return MOSQ_ERR_INVAL;
- }
- if(cJSON_AddStringToObject(j_command, "command", "createRole") == NULL
- || cJSON_AddStringToObject(j_command, "rolename", rolename) == NULL
- ){
- return MOSQ_ERR_NOMEM;
- }else{
- return MOSQ_ERR_SUCCESS;
- }
- }
- int dynsec_role__delete(int argc, char *argv[], cJSON *j_command)
- {
- char *rolename = NULL;
- if(argc == 1){
- rolename = argv[0];
- }else{
- return MOSQ_ERR_INVAL;
- }
- if(cJSON_AddStringToObject(j_command, "command", "deleteRole") == NULL
- || cJSON_AddStringToObject(j_command, "rolename", rolename) == NULL
- ){
- return MOSQ_ERR_NOMEM;
- }else{
- return MOSQ_ERR_SUCCESS;
- }
- }
- int dynsec_role__get(int argc, char *argv[], cJSON *j_command)
- {
- char *rolename = NULL;
- if(argc == 1){
- rolename = argv[0];
- }else{
- return MOSQ_ERR_INVAL;
- }
- if(cJSON_AddStringToObject(j_command, "command", "getRole") == NULL
- || cJSON_AddStringToObject(j_command, "rolename", rolename) == NULL
- ){
- return MOSQ_ERR_NOMEM;
- }else{
- return MOSQ_ERR_SUCCESS;
- }
- }
- int dynsec_role__list_all(int argc, char *argv[], cJSON *j_command)
- {
- int count = -1, offset = -1;
- if(argc == 0){
- /* All roles */
- }else if(argc == 1){
- count = atoi(argv[0]);
- }else if(argc == 2){
- count = atoi(argv[0]);
- offset = atoi(argv[1]);
- }else{
- return MOSQ_ERR_INVAL;
- }
- if(cJSON_AddStringToObject(j_command, "command", "listRoles") == NULL
- || (count > 0 && cJSON_AddIntToObject(j_command, "count", count) == NULL)
- || (offset > 0 && cJSON_AddIntToObject(j_command, "offset", offset) == NULL)
- ){
- return MOSQ_ERR_NOMEM;
- }else{
- return MOSQ_ERR_SUCCESS;
- }
- }
- int dynsec_role__add_acl(int argc, char *argv[], cJSON *j_command)
- {
- char *rolename, *acltype, *topic, *action;
- bool allow;
- int priority = -1;
- if(argc == 5){
- rolename = argv[0];
- acltype = argv[1];
- topic = argv[2];
- action = argv[3];
- priority = atoi(argv[4]);
- }else if(argc == 4){
- rolename = argv[0];
- acltype = argv[1];
- topic = argv[2];
- action = argv[3];
- }else{
- return MOSQ_ERR_INVAL;
- }
- if(strcasecmp(acltype, "publishClientSend")
- && strcasecmp(acltype, "publishClientReceive")
- && strcasecmp(acltype, "subscribeLiteral")
- && strcasecmp(acltype, "subscribePattern")
- && strcasecmp(acltype, "unsubscribeLiteral")
- && strcasecmp(acltype, "unsubscribePattern")){
- return MOSQ_ERR_INVAL;
- }
- if(!strcasecmp(action, "allow")){
- allow = true;
- }else if(!strcasecmp(action, "deny")){
- allow = false;
- }else{
- return MOSQ_ERR_INVAL;
- }
- if(cJSON_AddStringToObject(j_command, "command", "addRoleACL") == NULL
- || cJSON_AddStringToObject(j_command, "rolename", rolename) == NULL
- || cJSON_AddStringToObject(j_command, "acltype", acltype) == NULL
- || cJSON_AddStringToObject(j_command, "topic", topic) == NULL
- || cJSON_AddBoolToObject(j_command, "allow", allow) == NULL
- || (priority != -1 && cJSON_AddIntToObject(j_command, "priority", priority) == NULL)
- ){
- return MOSQ_ERR_NOMEM;
- }else{
- return MOSQ_ERR_SUCCESS;
- }
- }
- int dynsec_role__remove_acl(int argc, char *argv[], cJSON *j_command)
- {
- char *rolename, *acltype, *topic;
- if(argc == 3){
- rolename = argv[0];
- acltype = argv[1];
- topic = argv[2];
- }else{
- return MOSQ_ERR_INVAL;
- }
- if(strcasecmp(acltype, "publishClientSend")
- && strcasecmp(acltype, "publishClientReceive")
- && strcasecmp(acltype, "subscribeLiteral")
- && strcasecmp(acltype, "subscribePattern")
- && strcasecmp(acltype, "unsubscribeLiteral")
- && strcasecmp(acltype, "unsubscribePattern")){
- return MOSQ_ERR_INVAL;
- }
- if(cJSON_AddStringToObject(j_command, "command", "removeRoleACL") == NULL
- || cJSON_AddStringToObject(j_command, "rolename", rolename) == NULL
- || cJSON_AddStringToObject(j_command, "acltype", acltype) == NULL
- || cJSON_AddStringToObject(j_command, "topic", topic) == NULL
- ){
- return MOSQ_ERR_NOMEM;
- }else{
- return MOSQ_ERR_SUCCESS;
- }
- }
|