From bd2aa2f426ac0e0516defe2ef01d0886107a2330 Mon Sep 17 00:00:00 2001 From: "Roger A. Light" Date: Fri, 20 Jun 2014 20:59:04 +0100 Subject: [PATCH] Add batch mode to mosquitto_passwd. --- ChangeLog.txt | 2 ++ man/mosquitto_passwd.1.xml | 23 +++++++++++++++++++++ src/mosquitto_passwd.c | 41 ++++++++++++++++++++++++++++---------- 3 files changed, 56 insertions(+), 10 deletions(-) diff --git a/ChangeLog.txt b/ChangeLog.txt index 7ec9bd0e..8b9e3ce4 100644 --- a/ChangeLog.txt +++ b/ChangeLog.txt @@ -33,6 +33,8 @@ Broker: v3.1.1 client connects with a zero length client id. - Anonymous clients are no longer accidently disconnected from the broker after a SIGHUP. +- mosquitto_passwd now supports -b (batch mode) to allow the password to be + provided at the command line. Clients: - Both clients can now load default configuration options from a file. diff --git a/man/mosquitto_passwd.1.xml b/man/mosquitto_passwd.1.xml index 1679760e..9cb4c5d1 100644 --- a/man/mosquitto_passwd.1.xml +++ b/man/mosquitto_passwd.1.xml @@ -24,6 +24,13 @@ passwordfile username + + mosquitto_passwd + + passwordfile + username + password + mosquitto_passwd @@ -43,6 +50,16 @@ Options + + + + Run in batch mode. This allows the password to be + provided at the command line which can be convenient + but should be used with care because the password will + be visible on the command line and in command + history. + + @@ -82,6 +99,12 @@ The username to add/update/delete. + + + + The password to use when in batch mode. + + diff --git a/src/mosquitto_passwd.c b/src/mosquitto_passwd.c index b8a518f1..02c9055d 100644 --- a/src/mosquitto_passwd.c +++ b/src/mosquitto_passwd.c @@ -73,7 +73,9 @@ void print_usage(void) { printf("mosquitto_passwd is a tool for managing password files for mosquitto.\n\n"); printf("Usage: mosquitto_passwd [-c | -D] passwordfile username\n"); + printf(" mosquitto_passwd -b passwordfile username password\n"); printf(" mosquitto_passwd -U passwordfile\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(" -D : delete the username rather than adding/updating its password.\n"); printf(" -U : update a plain text password file to use hashed passwords.\n"); @@ -346,6 +348,8 @@ int main(int argc, char *argv[]) { char *password_file = NULL; char *username = NULL; + char *password_cmd = NULL; + bool batch_mode = false; bool create_new = false; bool delete_user = false; FILE *fptr, *ftmp; @@ -359,11 +363,23 @@ int main(int argc, char *argv[]) OpenSSL_add_all_digests(); - if(argc == 4){ + if(argc == 5){ + if(!strcmp(argv[1], "-b")){ + batch_mode = true; + }else{ + fprintf(stderr, "Error: Unknown option '%s'\n", argv[1]); + } + password_file = argv[2]; + username = argv[3]; + password_cmd = argv[4]; + }else if(argc == 4){ if(!strcmp(argv[1], "-c")){ create_new = true; }else if(!strcmp(argv[1], "-D")){ delete_user = true; + }else{ + fprintf(stderr, "Error: Unknown option '%s'\n", argv[1]); + return 1; } password_file = argv[2]; username = argv[3]; @@ -419,16 +435,21 @@ int main(int argc, char *argv[]) }else if(do_update_file){ rc = update_file(fptr, ftmp); }else{ - rc = get_password(password, 1024); - if(rc){ - fclose(fptr); - fclose(ftmp); - unlink(backup_file); - free(backup_file); - return rc; + if(batch_mode){ + /* Update password for individual user */ + rc = update_pwuser(fptr, ftmp, username, password_cmd); + }else{ + rc = get_password(password, 1024); + if(rc){ + fclose(fptr); + fclose(ftmp); + unlink(backup_file); + free(backup_file); + return rc; + } + /* Update password for individual user */ + rc = update_pwuser(fptr, ftmp, username, password); } - /* Update password for individual user */ - rc = update_pwuser(fptr, ftmp, username, password); } if(rc){ fclose(fptr);