Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/include config: Support including additional config files using new config file directive #623

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

cpitchford
Copy link

This PR add support in the configuration file for an "include" directive.

If line appears in the config file loaded by the daemon:

include /etc/miniupnpd/miniupnpd.conf.d/*.conf

It will pause processing the config file and begin processing each config that matches the pattern/glob in turn before resuming processing of the original file.

The directive is recursive, but limited to a depth of 5 config files.

This process allows for a config folder, or including a "local" file to override package defaults

I have in my setup, used:

/etc/miniupnpd/miniupnpd.conf.d/10-default.conf # Default config values
/etc/miniupnpd/miniupnpd.conf.d/19-nft-chains.conf # Override NFT table/chain names
/etc/miniupnpd/miniupnpd.conf.d/20-allow-hosts.conf # list of allow directives for the hosts I grant access to
/etc/miniupnpd/miniupnpd.conf.d/99-final-deny.conf # a final deny to drop all other hosts

This capability is enabled using a new .h/.c file configlocations.

struct ConfigLocations is a handle that provides a fgets like interface to read lines.

The handle can be instructed to open a file, folder, or glob pattern.

calling the fgets method against the handle will return a line from the current file, automatically rolling forward to other files matched by folder or glob and rolling back to paused files.

The "include" directive is included in the config file reading loop. When an "include" directive is found, the parameter is passed into the open_glob method to load all config files that match the pattern.

This functionality is proving useful for me as I dynamically create the allow list based on other system information and I would prefer not to re-write the entire config file each time. I also like having the ability to override defaults in the package config independently

ConfigLocations_create():
  Creates a config file location handle
ConfigLocations_fgets():
  Read a line from the current config file that is open
ConfigLocations_open_file():
  Open a new current config file
ConfigLocations_open_folder():
  Open all config files (sorted alphanumerically) from a folder
ConfigLocations_open_glob():
  Open all config files matching a fnmatch/glob pattern
ConfigLocations_close()
  Close the current config file (and move to next)
ConfigLocations_free()
  Close all config files and free all resources

The fgets method also returns the line number/file path of the current file

As we process the lines returned by fgets we're able to ask the
handle to open a new pattern:

```C
    int debug_flag = 1
    const char * config_file_path = "my_config.conf";
    char buffer[1024];
    const char * current_file;
    int current_line_number;

    struct ConfigLocations *handle = ConfigLocations_create();
    ConfigLocations_open_file(handle, config_file_path, debug_flag);

    while (ConfigLocations_fgets(handle, &buffer, sizeof(buffer),
                                &current_file, &current_line_number,
                                debug_flag))
    {
        // Remove new line
        chat * end = strchr(buffer, '\n');
        if (end) *end = '\0';

        // Dump current file, line number, and line that was read:
        printf("%s[%d]: Read \"%s\"\n", current_file, line_number, buffer);

        // Look for lines starting "include "
        // We'll use this as an instruction to include additional files
        if (memcmp(buffer, "include ", sizeof("include ")-1) == 0) {
            // Remove the "include " from the start of the line
            char * pattern = buffer + sizeof("include ") - 1;
            // Treat the remainder as a glob:
            ConfigLocations_open_glob(handle, pattern, debug_flag);
            // Continue with the read loop that will now jump
            // to the included files
            continue;
        }
        // Do something with the config file lines we read
    }
    ConfigLocations_free(handle);
```

In this demo, we parse our config file (my_config.conf).
When we encounter a line "include glob_patter", we resolve all the
files that match this pattern and open them in order.

Each call to ConfigLocations_fgets reads from the current file
At EOF, it jumps to the next file matching the current glob
before finally returned back to the previous file

After a line like:
  include my_config.d/*.conf

each subsequent call to fgets will return a line from each *.conf file in that folder
before resuming reading from my_config.conf
…ective

Using the ConfigLocations methods, the options fgets loop now supports
including additional config files based on the glob set in the config

the fgets call will stop reading the current file and start reading from
each config file that matched the pattern/glob before finally resuming
the original config file.

The number of inclusions is limited by the CONFIG_RECURSION_DEPTH defined
in the configlocations.h
Used in the options.c loop that reads from the config file
@miniupnp miniupnp self-assigned this Sep 11, 2022
@cpitchford cpitchford changed the title Feature/include config Feature/include config: Support including additional config files using new config file directive Sep 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants