mosquitto_ctrl: Allow optional client and password when creating a client.

This commit is contained in:
Roger A. Light 2020-11-07 23:47:49 +00:00
parent a0c39b193b
commit 4b3c76d97a
2 changed files with 26 additions and 10 deletions

View File

@ -38,7 +38,7 @@ void dynsec__print_usage(void)
printf("Set group for anonymous clients: setAnonymousGroup <groupname>\n");
printf("\nClients\n-------\n");
printf("Create a new client: createClient <username> [password]\n");
printf("Create a new client: createClient <username> [-c clientid] [-p password]\n");
printf("Delete a client: deleteClient <username>\n");
printf("Set a client password: setClientPassword <username> [password]\n");
printf("Add a role to a client: addClientRole <username> <rolename> [priority]\n");

View File

@ -25,14 +25,35 @@ Contributors:
int dynsec_client__create(int argc, char *argv[], cJSON *j_command)
{
char *username = NULL, *password = NULL;
char *username = NULL, *password = NULL, *clientid = NULL;
char prompt[200], verify_prompt[200];
char password_buf[200];
int rc;
int i;
bool request_password = true;
if(argc == 1){
username = argv[0];
username = argv[0];
for(i=1; i<argc; i++){
if(!strcmp(argv[i], "-c")){
if(i+1 == argc){
fprintf(stderr, "Error: -c argument given, but no clientid provided.\n");
return MOSQ_ERR_INVAL;
}
clientid = argv[i+1];
i++;
}else if(!strcmp(argv[i], "-p")){
if(i+1 == argc){
fprintf(stderr, "Error: -p argument given, but no password provided.\n");
return MOSQ_ERR_INVAL;
}
password = argv[i+1];
i++;
request_password = false;
}
}
if(request_password){
printf("Enter new password for %s. Press return for no password (user will be unable to login).\n", username);
snprintf(prompt, sizeof(prompt), "New password for %s: ", username);
snprintf(verify_prompt, sizeof(verify_prompt), "Reenter password for %s: ", username);
@ -43,15 +64,10 @@ int dynsec_client__create(int argc, char *argv[], cJSON *j_command)
password = NULL;
printf("\n");
}
}else if(argc == 2){
username = argv[0];
password = argv[1];
}else{
return MOSQ_ERR_INVAL;
}
if(cJSON_AddStringToObject(j_command, "command", "createClient") == NULL
|| cJSON_AddStringToObject(j_command, "username", username) == NULL
|| (clientid && cJSON_AddStringToObject(j_command, "clientid", clientid) == NULL)
|| (password && cJSON_AddStringToObject(j_command, "password", password) == NULL)
){