Skip to content

Commit

Permalink
Fix confusing error message when dynamic security config file was a d…
Browse files Browse the repository at this point in the history
…irectory.

Closes #2520. Thanks to sezanzeb
  • Loading branch information
ralight committed Aug 12, 2022
1 parent 775bd2e commit 80c7726
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
2 changes: 2 additions & 0 deletions ChangeLog.txt
Expand Up @@ -20,6 +20,8 @@ Broker:
taken over. Closes #2607.
- Fix confusing "out of memory" error when a client is kicked in the dynamic
security plugin. Closes #2525.
- Fix confusing error message when dynamic security config file was a
directory. Closes #2520.

Client library:
- Fix threads library detection on Windows under cmake. Bumps the minimum
Expand Down
18 changes: 12 additions & 6 deletions plugins/dynamic-security/plugin.c
Expand Up @@ -361,15 +361,21 @@ static int dynsec__config_load(void)
fptr = fopen(config_file, "rb");
if(fptr == NULL){
mosquitto_log_printf(MOSQ_LOG_ERR, "Error loading Dynamic security plugin config: File is not readable - check permissions.\n");
return 1;
return MOSQ_ERR_ERRNO;
}
#ifndef WIN32
if(errno == ENOTDIR || errno == EISDIR){
mosquitto_log_printf(MOSQ_LOG_ERR, "Error loading Dynamic security plugin config: Config is not a file.\n");
return MOSQ_ERR_ERRNO;
}
#endif

fseek(fptr, 0, SEEK_END);
flen_l = ftell(fptr);
if(flen_l < 0){
mosquitto_log_printf(MOSQ_LOG_ERR, "Error loading Dynamic security plugin config: %s\n", strerror(errno));
fclose(fptr);
return 1;
return MOSQ_ERR_ERRNO;
}else if(flen_l == 0){
fclose(fptr);
return 0;
Expand All @@ -380,21 +386,21 @@ static int dynsec__config_load(void)
if(json_str == NULL){
mosquitto_log_printf(MOSQ_LOG_ERR, "Error: Out of memory.");
fclose(fptr);
return 1;
return MOSQ_ERR_NOMEM;
}
if(fread(json_str, 1, flen, fptr) != flen){
mosquitto_log_printf(MOSQ_LOG_WARNING, "Error loading Dynamic security plugin config: Unable to read file contents.\n");
mosquitto_free(json_str);
fclose(fptr);
return 1;
return MOSQ_ERR_ERRNO;
}
fclose(fptr);

tree = cJSON_Parse(json_str);
mosquitto_free(json_str);
if(tree == NULL){
mosquitto_log_printf(MOSQ_LOG_ERR, "Error loading Dynamic security plugin config: File is not valid JSON.\n");
return 1;
return MOSQ_ERR_INVAL;
}

if(dynsec__general_config_load(tree)
Expand All @@ -404,7 +410,7 @@ static int dynsec__config_load(void)
){

cJSON_Delete(tree);
return 1;
return MOSQ_ERR_NOMEM;
}

cJSON_Delete(tree);
Expand Down

0 comments on commit 80c7726

Please sign in to comment.