Fix mosquitto_passwd not being able to create pwfile with -b.

pull/1691/head
Roger A. Light 6 years ago
parent fdaeaee6dd
commit 3758e0c328

@ -133,7 +133,7 @@ void print_usage(void)
{ {
printf("mosquitto_passwd is a tool for managing password files for mosquitto.\n\n"); printf("mosquitto_passwd is a tool for managing password files for mosquitto.\n\n");
printf("Usage: mosquitto_passwd [-c | -D] passwordfile username\n"); printf("Usage: mosquitto_passwd [-c | -D] passwordfile username\n");
printf(" mosquitto_passwd -b passwordfile username password\n"); printf(" mosquitto_passwd [-c] -b passwordfile username password\n");
printf(" mosquitto_passwd -U passwordfile\n"); printf(" mosquitto_passwd -U passwordfile\n");
printf(" -b : run in batch mode to allow passing passwords on the command line.\n"); printf(" -b : run in batch mode to allow passing passwords on the command line.\n");
printf(" -c : create a new password file. This will overwrite existing files.\n"); printf(" -c : create a new password file. This will overwrite existing files.\n");
@ -331,10 +331,8 @@ static int update_pwuser_cb(FILE *fptr, FILE *ftmp, const char *username, const
{ {
int rc = 0; int rc = 0;
printf("%s\n", username);
if(strcmp(username, helper->username)){ if(strcmp(username, helper->username)){
/* If this isn't the matching user, then writing out the exiting line */ /* If this isn't the matching user, then writing out the exiting line */
printf("%s\n", line);
fprintf(ftmp, "%s", line); fprintf(ftmp, "%s", line);
}else{ }else{
/* Write out a new line for our matching username */ /* Write out a new line for our matching username */
@ -539,12 +537,22 @@ int main(int argc, char *argv[])
if(!strcmp(argv[1], "-c")){ if(!strcmp(argv[1], "-c")){
create_new = true; create_new = true;
if(argc != 4){ if(argc == 4){
fprintf(stderr, "Error: -c argument given but password file or username missing.\n");
return 1;
}else{
password_file_tmp = argv[2]; password_file_tmp = argv[2];
username = argv[3]; username = argv[3];
}else if(argc == 6){
if(!strcmp(argv[2], "-b")){
batch_mode = true;
password_file_tmp = argv[3];
username = argv[4];
password_cmd = argv[5];
}else{
fprintf(stderr, "Error: Incorrect number of arguments.\n");
return 1;
}
}else{
fprintf(stderr, "Error: -c argument given but password file or username missing.\n");
return 1;
} }
}else if(!strcmp(argv[1], "-D")){ }else if(!strcmp(argv[1], "-D")){
delete_user = true; delete_user = true;
@ -612,10 +620,13 @@ int main(int argc, char *argv[])
#endif #endif
if(create_new){ if(create_new){
rc = get_password(password, MAX_BUFFER_LEN); if(batch_mode == false){
if(rc){ rc = get_password(password, MAX_BUFFER_LEN);
free(password_file); if(rc){
return rc; free(password_file);
return rc;
}
password_cmd = password;
} }
fptr = fopen(password_file, "wt"); fptr = fopen(password_file, "wt");
if(!fptr){ if(!fptr){
@ -624,7 +635,7 @@ int main(int argc, char *argv[])
return 1; return 1;
} }
free(password_file); free(password_file);
rc = output_new_password(fptr, username, password); rc = output_new_password(fptr, username, password_cmd);
fclose(fptr); fclose(fptr);
return rc; return rc;
}else{ }else{

Loading…
Cancel
Save