提交 3ab0a9a3 编写于 作者: R Roger A. Light

mosquitto_ctrl dynsec init uses open( , O_EXCL | O_CREAT)

This allows us to refuse to open an existing file, without a race
condition.
上级 00b24e0e
......@@ -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);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册