mirror of
https://github.com/eclipse/mosquitto.git
synced 2025-05-09 01:01:11 +08:00
mosquitto_ctrl dynsec init uses open( , O_EXCL | O_CREAT)
This allows us to refuse to open an existing file, without a race condition.
This commit is contained in:
parent
00b24e0eb0
commit
3ab0a9a3fd
@ -48,6 +48,8 @@ Clients:
|
||||
|
||||
Apps:
|
||||
- mosquitto_passwd uses mkstemp() for backup files.
|
||||
- `mosquitto_ctrl dynsec init` will refuse to overwrite an existing file,
|
||||
without a race-condition.
|
||||
|
||||
|
||||
2.0.15 - 2022-08-16
|
||||
|
@ -23,6 +23,8 @@ Contributors:
|
||||
#include <string.h>
|
||||
|
||||
#ifndef WIN32
|
||||
# include <errno.h>
|
||||
# include <fcntl.h>
|
||||
# include <strings.h>
|
||||
#endif
|
||||
|
||||
@ -739,13 +741,6 @@ static int dynsec_init(int argc, char *argv[])
|
||||
admin_password = password;
|
||||
}
|
||||
|
||||
fptr = mosquitto__fopen(filename, "rb", true);
|
||||
if(fptr){
|
||||
fclose(fptr);
|
||||
fprintf(stderr, "dynsec init: '%s' already exists. Remove the file or use a different location..\n", filename);
|
||||
return -1;
|
||||
}
|
||||
|
||||
tree = init_create(admin_user, admin_password, "admin");
|
||||
if(tree == NULL){
|
||||
fprintf(stderr, "dynsec init: Out of memory.\n");
|
||||
@ -754,7 +749,17 @@ static int dynsec_init(int argc, char *argv[])
|
||||
json_str = cJSON_Print(tree);
|
||||
cJSON_Delete(tree);
|
||||
|
||||
#ifdef WIN32
|
||||
fptr = mosquitto__fopen(filename, "wb", true);
|
||||
#else
|
||||
int fd = open(filename, O_CREAT | O_EXCL | O_WRONLY, 0640);
|
||||
if(fd < 0){
|
||||
free(json_str);
|
||||
fprintf(stderr, "dynsec init: Unable to open '%s' for writing (%s).\n", filename, strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
fptr = fdopen(fd, "wb");
|
||||
#endif
|
||||
if(fptr){
|
||||
fprintf(fptr, "%s", json_str);
|
||||
free(json_str);
|
||||
|
Loading…
x
Reference in New Issue
Block a user