// SPDX-License-Identifier: GPL-3.0-or-later
#include "plugins_d.h"
char *plugin_directories[PLUGINSD_MAX_DIRECTORIES] = { NULL };
struct plugind *pluginsd_root = NULL;
static inline int pluginsd_space(char c) {
switch(c) {
case ' ':
case '\t':
case '\r':
case '\n':
case '=':
return 1;
default:
return 0;
}
}
inline int config_isspace(char c) {
switch(c) {
case ' ':
case '\t':
case '\r':
case '\n':
case ',':
return 1;
default:
return 0;
}
}
// split a text into words, respecting quotes
static inline int quoted_strings_splitter(char *str, char **words, int max_words, int (*custom_isspace)(char)) {
char *s = str, quote = 0;
int i = 0, j;
// skip all white space
while(unlikely(custom_isspace(*s))) s++;
// check for quote
if(unlikely(*s == '\'' || *s == '"')) {
quote = *s; // remember the quote
s++; // skip the quote
}
// store the first word
words[i++] = s;
// while we have something
while(likely(*s)) {
// if it is escape
if(unlikely(*s == '\\' && s[1])) {
s += 2;
continue;
}
// if it is quote
else if(unlikely(*s == quote)) {
quote = 0;
*s = ' ';
continue;
}
// if it is a space
else if(unlikely(quote == 0 && custom_isspace(*s))) {
// terminate the word
*s++ = '\0';
// skip all white space
while(likely(custom_isspace(*s))) s++;
// check for quote
if(unlikely(*s == '\'' || *s == '"')) {
quote = *s; // remember the quote
s++; // skip the quote
}
// if we reached the end, stop
if(unlikely(!*s)) break;
// store the next word
if(likely(i < max_words)) words[i++] = s;
else break;
}
// anything else
else s++;
}
// terminate the words
j = i;
while(likely(j < max_words)) words[j++] = NULL;
return i;
}
inline int pluginsd_initialize_plugin_directories() {
char plugins_dirs[(FILENAME_MAX * 2) + 1];
static char *plugins_dir_list = NULL;
// Get the configuration entry
if(likely(!plugins_dir_list)) {
snprintfz(plugins_dirs, FILENAME_MAX * 2, "\"%s\" \"%s/custom-plugins.d\"", PLUGINS_DIR, CONFIG_DIR);
plugins_dir_list = strdupz(config_get(CONFIG_SECTION_GLOBAL, "plugins directory", plugins_dirs));
}
// Parse it and store it to plugin directories
return quoted_strings_splitter(plugins_dir_list, plugin_directories, PLUGINSD_MAX_DIRECTORIES, config_isspace);
}
inline int pluginsd_split_words(char *str, char **words, int max_words) {
return quoted_strings_splitter(str, words, max_words, pluginsd_space);
}
#ifdef ENABLE_HTTPS
/**
* Update Buffer
*
* Update the temporary buffer used to parse data received from slave
*
* @param output is a pointer to the vector where I will store the data
* @param ssl is the connection pointer with the server
*
* @return it returns the total of bytes read on success and a negative number otherwise
*/
int pluginsd_update_buffer(char *output, SSL *ssl) {
ERR_clear_error();
int bytesleft = SSL_read(ssl, output, PLUGINSD_LINE_MAX_SSL_READ);
if(bytesleft <= 0) {
int sslerrno = SSL_get_error(ssl, bytesleft);
switch(sslerrno) {
<