Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add stdout option to mosquitto_passwd #2833

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
add stdout option to mosquitto_passwd
parse a dash '-' to output a password to stdout when using -c 'create
new password file'
  • Loading branch information
trysten committed Jun 29, 2023
commit 838238a138e22e804716eb38157d0985987ef0fa
53 changes: 32 additions & 21 deletions apps/mosquitto_passwd/mosquitto_passwd.c
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,7 @@ int main(int argc, char *argv[])
bool batch_mode = false;
bool create_new = false;
bool delete_user = false;
bool use_stdout = false;
FILE *fptr, *ftmp;
char password[MAX_BUFFER_LEN];
int rc;
Expand Down Expand Up @@ -529,7 +530,11 @@ int main(int argc, char *argv[])
fprintf(stderr, "Error: -c argument given but password file or username missing.\n");
return 1;
}else{
password_file_tmp = argv[idx];
if (!strcmp(argv[idx], "-")){
use_stdout = true;
}else{
password_file_tmp = argv[idx];
}
username = argv[idx+1];
}
}
Expand Down Expand Up @@ -568,27 +573,29 @@ int main(int argc, char *argv[])
return 1;
}

if(!use_stdout){
#ifdef WIN32
password_file = _fullpath(NULL, password_file_tmp, 0);
if(!password_file){
fprintf(stderr, "Error getting full path for password file.\n");
return 1;
}
password_file = _fullpath(NULL, password_file_tmp, 0);
if(!password_file){
fprintf(stderr, "Error getting full path for password file.\n");
return 1;
}
#else
password_file = realpath(password_file_tmp, NULL);
if(!password_file){
if(errno == ENOENT){
password_file = strdup(password_file_tmp);
if(!password_file){
fprintf(stderr, "Error: Out of memory.\n");
password_file = realpath(password_file_tmp, NULL);
if(!password_file){
if(errno == ENOENT){
password_file = strdup(password_file_tmp);
if(!password_file){
fprintf(stderr, "Error: Out of memory.\n");
return 1;
}
}else{
fprintf(stderr, "Error reading password file: %s\n", strerror(errno));
return 1;
}
}else{
fprintf(stderr, "Error reading password file: %s\n", strerror(errno));
return 1;
}
}
#endif
}

if(create_new){
if(batch_mode == false){
Expand All @@ -599,13 +606,17 @@ int main(int argc, char *argv[])
}
password_cmd = password;
}
fptr = fopen(password_file, "wt");
if(!fptr){
fprintf(stderr, "Error: Unable to open file %s for writing. %s.\n", password_file, strerror(errno));
if(use_stdout){
fptr = stdout;
}else{
fptr = fopen(password_file, "wt");
if(!fptr){
fprintf(stderr, "Error: Unable to open file %s for writing. %s.\n", password_file, strerror(errno));
free(password_file);
return 1;
}
free(password_file);
return 1;
}
free(password_file);
rc = output_new_password(fptr, username, password_cmd, iterations);
fclose(fptr);
return rc;
Expand Down