Skip to content
10 changes: 7 additions & 3 deletions etc/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ EXTRA_DIST = mptcpd.conf.in

pkgsysconf_DATA = mptcpd.conf
pkgsysconfdir = $(sysconfdir)/@PACKAGE@
pluginsconfdir = $(pkgsysconfdir)/plugins.conf.d

## The configure script won't fully expand $pkglibdir so leverage
## `make' based variable expansion instead.
Expand All @@ -27,8 +28,11 @@ CLEANFILES = mptcpd.conf
# writable.
install-data-hook: installcheck-local
chmod o-w $(DESTDIR)$(pkgsysconfdir)
mkdir -p $(DESTDIR)$(pluginsconfdir)
chmod o-w $(DESTDIR)$(pluginsconfdir)

installcheck-local:
$(top_srcdir)/scripts/check-permissions \
$(DESTDIR)$(pkgsysconfdir) \
$(DESTDIR)$(pkgsysconfdir)/mptcpd.conf
$(top_srcdir)/scripts/check-permissions \
$(DESTDIR)$(pkgsysconfdir) \
$(DESTDIR)$(pkgsysconfdir)/mptcpd.conf \
$(DESTDIR)$(pluginsconfdir)
4 changes: 4 additions & 0 deletions include/mptcpd/plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,10 @@ MPTCPD_API bool mptcpd_plugin_register_ops(
char const *name,
struct mptcpd_plugin_ops const *ops);

MPTCPD_API bool mptcpd_plugin_read_config(char const *filename,
mptcpd_parse_func_t fun,
void *user_data);

#ifdef __cplusplus
}
#endif
Expand Down
9 changes: 9 additions & 0 deletions include/mptcpd/private/configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#ifndef MPTCPD_CONFIGURATION_H
#define MPTCPD_CONFIGURATION_H

#include <mptcpd/export.h>
#include <mptcpd/types.h>

/**
* Function pointer corresponding to the ELL functions that set the
Expand Down Expand Up @@ -66,6 +68,9 @@ struct mptcpd_config

/// A list of plugins to load.
struct l_queue *plugins_to_load;

/// Location of mptcpd plugins configuration files
char *plugins_conf_dir;
};

/**
Expand All @@ -87,6 +92,10 @@ struct mptcpd_config *mptcpd_config_create(int argc, char *argv[]);
*/
void mptcpd_config_destroy(struct mptcpd_config *config);

MPTCPD_API bool mptcpd_config_read(char const *filename,
mptcpd_parse_func_t fun,
void *user_data);

#endif // MPTCPD_CONFIGURATION_H

/*
Expand Down
1 change: 1 addition & 0 deletions include/mptcpd/private/plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ struct mptcpd_interface;
*/
MPTCPD_API bool mptcpd_plugin_load(char const *dir,
char const *default_name,
char const *plugins_conf_dir,
struct l_queue const *plugins_to_load,
struct mptcpd_pm *pm);

Expand Down
5 changes: 5 additions & 0 deletions include/mptcpd/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

#include <stddef.h>
#include <inttypes.h>
#include <ell/settings.h>

#ifdef __cplusplus
extern "C" {
Expand Down Expand Up @@ -162,6 +163,10 @@ typedef void (*mptcpd_pm_get_limits_cb)(
size_t len,
void *callback_data);

typedef void (*mptcpd_parse_func_t) (
struct l_settings *const settings,
void *user_data);

#ifdef __cplusplus
}
#endif
Expand Down
1 change: 1 addition & 0 deletions lib/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ libmptcpd_la_LDFLAGS = \

libmptcpd_la_SOURCES = \
addr_info.c \
configuration.c \
id_manager.c \
network_monitor.c \
path_manager.c \
Expand Down
84 changes: 84 additions & 0 deletions lib/configuration.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#include <stdlib.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>
#include <sys/stat.h>
#include <errno.h>

#include <ell/log.h>
#include <ell/util.h>
#include <ell/settings.h>

#include <mptcpd/types.h>

#include <mptcpd/private/configuration.h>

/**
* @brief Verify file permissions are secure.
*
* Mptcpd requires that its files are only writable by the owner and
* group. Verify that the "other" write mode, @c S_IWOTH, isn't set.
*
* @param[in] f Name of file to check for expected permissions.
*
* @note There is a TOCTOU race condition between this file
* permissions check and subsequent calls to functions that
* access the given file @a f, such as the call to
* @c l_settings_load_from_file(). There is currently no way
* to avoid that with the existing ELL API.
*/
static bool check_file_perms(char const *f)
{
struct stat sb;
bool perms_ok = false;

if (stat(f, &sb) == 0) {
perms_ok = S_ISREG(sb.st_mode)
&& (sb.st_mode & S_IWOTH) == 0;

if (!perms_ok)
l_error("\"%s\" should be a file that is not "
"world writable.",
f);
} else if (errno == ENOENT) {
perms_ok = true;

l_debug("File \"%s\" does not exist.", f);
} else {
l_debug("Unexpected error during file "
"permissions check.");
}

return perms_ok;
}

bool mptcpd_config_read(char const *filename,
mptcpd_parse_func_t fun,
void *user_data)
{
assert(filename != NULL);
assert(fun != NULL);

if (!check_file_perms(filename))
return false;

struct l_settings *const settings = l_settings_new();
if (settings == NULL) {
l_error("Unable to create mptcpd settings.");

return false;
}

bool success = l_settings_load_from_file(settings, filename);

if (success)
fun(settings, user_data);
else
l_debug("Unable to load mptcpd settings from file '%s'",
filename);

l_settings_free(settings);

return success;
}

31 changes: 31 additions & 0 deletions lib/plugin.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
# define MPTCP_PM_NAME_LEN GENL_NAMSIZ
#endif

#include <mptcpd/private/configuration.h>
#include <mptcpd/private/plugin.h>
#include <mptcpd/plugin.h>

Expand Down Expand Up @@ -86,6 +87,8 @@ static char _default_name[MPTCP_PM_NAME_LEN + 1];
*/
static struct mptcpd_plugin_ops const *_default_ops;

static char *_conf_dir;

// ----------------------------------------------------------------
// Implementation Details
// ----------------------------------------------------------------
Expand Down Expand Up @@ -432,6 +435,7 @@ static void unload_plugins(struct mptcpd_pm *pm)

bool mptcpd_plugin_load(char const *dir,
char const *default_name,
char const *plugins_conf_dir,
struct l_queue const *plugins_to_load,
struct mptcpd_pm *pm)
{
Expand All @@ -440,6 +444,14 @@ bool mptcpd_plugin_load(char const *dir,
return false;
}

if (plugins_conf_dir == NULL) {
l_error("No plugins configuration directory specified.");
return false;
}

if (_conf_dir == NULL)
_conf_dir = l_strdup(plugins_conf_dir);

if (_plugin_infos == NULL)
_plugin_infos = l_queue_new();

Expand Down Expand Up @@ -569,6 +581,25 @@ bool mptcpd_plugin_register_ops(char const *name,
return registered;
}

bool mptcpd_plugin_read_config(char const *filename,
mptcpd_parse_func_t fun,
void *user_data)
{
assert(filename != NULL);
assert(fun != NULL);

char *const path = l_strdup_printf("%s/%s.conf",
_conf_dir,
filename);

bool success = mptcpd_config_read(path, fun, user_data);

l_free(path);

return success;
}


// ----------------------------------------------------------------
// Plugin Operation Callback Invocation
// ----------------------------------------------------------------
Expand Down
8 changes: 5 additions & 3 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,12 @@ libpath_manager_la_LIBADD = \
$(top_builddir)/lib/libmptcpd.la \
$(ELL_LIBS) $(CODE_COVERAGE_LIBS)

libpath_manager_la_CPPFLAGS = \
$(AM_CPPFLAGS) \
libpath_manager_la_CPPFLAGS = \
$(AM_CPPFLAGS) \
-DMPTCPD_CONFIG_FILE='"$(sysconfdir)/@PACKAGE@/@[email protected]"' \
-DMPTCPD_DEFAULT_PLUGINDIR='"$(libdir)/@PACKAGE@"'
-DMPTCPD_DEFAULT_PLUGINDIR='"$(libdir)/@PACKAGE@"' \
-DMPTCPD_DEFAULT_PLUGINSCONFDIR='"$(sysconfdir)/@PACKAGE@/$\
plugins.conf.d/"'

EXTRA_DIST = mptcp.service.in

Expand Down
Loading