From 2a05602d8469a1e1819b7ed044b5c1550f5f299f Mon Sep 17 00:00:00 2001 From: "Roger A. Light" Date: Tue, 27 Mar 2018 07:32:41 +0100 Subject: [PATCH] Obtain canonical name for password file. --- src/mosquitto_passwd.c | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/src/mosquitto_passwd.c b/src/mosquitto_passwd.c index 7ce18820..0523a3b4 100644 --- a/src/mosquitto_passwd.c +++ b/src/mosquitto_passwd.c @@ -368,7 +368,7 @@ void handle_sigint(int signal) int main(int argc, char *argv[]) { char *password_file_tmp = NULL; - char password_file[1024]; + char *password_file = NULL; char *username = NULL; char *password_cmd = NULL; bool batch_mode = false; @@ -434,7 +434,19 @@ int main(int argc, char *argv[]) return 1; } - snprintf(password_file, 1024, "%s", password_file_tmp); +#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; + } +#else + password_file = realpath(password_file_tmp, NULL); + if(!password_file){ + fprintf(stderr, "Error reading password file: %s\n", strerror(errno)); + return 1; + } +#endif if(create_new){ rc = get_password(password, 1024); @@ -442,8 +454,10 @@ int main(int argc, char *argv[]) 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); rc = output_new_password(fptr, username, password); fclose(fptr); return rc; @@ -451,15 +465,19 @@ int main(int argc, char *argv[]) fptr = fopen(password_file, "r+t"); if(!fptr){ fprintf(stderr, "Error: Unable to open password file %s. %s.\n", password_file, strerror(errno)); + free(password_file); return 1; } backup_file = malloc(strlen(password_file)+5); if(!backup_file){ fprintf(stderr, "Error: Out of memory.\n"); + free(password_file); return 1; } snprintf(backup_file, strlen(password_file)+5, "%s.tmp", password_file); + free(password_file); + password_file = NULL; if(create_backup(backup_file, fptr)){ fclose(fptr);