Skip to content

Commit

Permalink
mosquitto_passwd uses mkstemp() for backup files.
Browse files Browse the repository at this point in the history
  • Loading branch information
ralight committed Aug 16, 2023
1 parent 9bb8058 commit 44b9487
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
3 changes: 3 additions & 0 deletions ChangeLog.txt
Expand Up @@ -29,6 +29,9 @@ Clients:
- Fix incorrect topic-alias property value in mosquitto_sub json output.
- Fix confusing message on TLS certificate verification. Closes #2746.

Apps:
- mosquitto_passwd uses mkstemp() for backup files.


2.0.15 - 2022-08-16
===================
Expand Down
18 changes: 15 additions & 3 deletions apps/mosquitto_passwd/mosquitto_passwd.c
Expand Up @@ -370,15 +370,27 @@ static int copy_contents(FILE *src, FILE *dest)
return 0;
}

static int create_backup(const char *backup_file, FILE *fptr)
static int create_backup(char *backup_file, FILE *fptr)
{
FILE *fbackup;

#ifdef WIN32
fbackup = mosquitto__fopen(backup_file, "wt", true);
#else
int fd;
umask(077);
fd = mkstemp(backup_file);
if(fd < 0){
fprintf(stderr, "Error creating backup password file \"%s\", not continuing.\n", backup_file);
return 1;
}
fbackup = fdopen(fd, "wt");
#endif
if(!fbackup){
fprintf(stderr, "Error creating backup password file \"%s\", not continuing.\n", backup_file);
return 1;
}

if(copy_contents(fptr, fbackup)){
fprintf(stderr, "Error copying data to backup password file \"%s\", not continuing.\n", backup_file);
fclose(fbackup);
Expand Down Expand Up @@ -617,13 +629,13 @@ int main(int argc, char *argv[])
return 1;
}

backup_file = malloc((size_t)strlen(password_file)+5);
backup_file = malloc((size_t)strlen(password_file)+strlen(".backup.XXXXXX"));
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);
snprintf(backup_file, strlen(password_file)+5, "%s.backup.XXXXXX", password_file);
free(password_file);
password_file = NULL;

Expand Down

0 comments on commit 44b9487

Please sign in to comment.