Skip to content

Commit

Permalink
Fix #100: Basic support for verifying .conf file
Browse files Browse the repository at this point in the history
Signed-off-by: Joachim Nilsson <[email protected]>
  • Loading branch information
troglobit committed Mar 19, 2019
1 parent 66b7790 commit 9633811
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 11 deletions.
16 changes: 12 additions & 4 deletions src/conf.c
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ static int add_mroute(int lineno, char *ifname, char *group, char *source, char
WARN("Invalid inbound IPv4 interface: %s", ifname);
return 1;
}

return result;
}

Expand Down Expand Up @@ -396,22 +397,29 @@ static int conf_parse(const char *file, int do_vifs)
}

/* Parse .conf file and setup routes */
void conf_read(char *file, int do_vifs)
int conf_read(char *file, int do_vifs)
{
int rc;

if (access(file, R_OK)) {
if (errno == ENOENT)
smclog(LOG_NOTICE, "Configuration file %s does not exist", file);
else
smclog(LOG_WARNING, "Unexpected error when accessing %s: %s", file, strerror(errno));

smclog(LOG_NOTICE, "Continuing anyway, waiting for client to connect.");
return;
if (!conf_vrfy)
smclog(LOG_NOTICE, "Continuing anyway, waiting for client to connect.");

return 1;
}

if (conf_parse(file, do_vifs))
rc = conf_parse(file, do_vifs);
if (rc)
smclog(LOG_WARNING, "Failed parsing %s: %s", file, strerror(errno));
else
script_exec(NULL);

return rc;
}

/**
Expand Down
4 changes: 3 additions & 1 deletion src/conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@

#include "config.h"

extern int conf_vrfy;

#ifdef ENABLE_DOTCONF
void conf_read(char *file, int do_vifs);
int conf_read(char *file, int do_vifs);
#else
#define conf_read(file, do_vifs)
#endif
Expand Down
10 changes: 9 additions & 1 deletion src/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <stdlib.h>

#include "log.h"
#include "conf.h"
#include "util.h"

int log_level = LOG_NOTICE;
Expand Down Expand Up @@ -66,7 +67,14 @@ void smclog(int severity, const char *fmt, ...)
va_list args;

va_start(args, fmt);
vsnprintf(log_message, sizeof(log_message), fmt, args);
if (!conf_vrfy) {
vsnprintf(log_message, sizeof(log_message), fmt, args);
} else {
if (severity <= log_level) {
vprintf(fmt, args);
puts("");
}
}
va_end(args);

syslog(severity, "%s", log_message);
Expand Down
30 changes: 25 additions & 5 deletions src/smcrouted.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ char *ident = PACKAGE;
char *prognm = NULL;
char *pid_file = NULL;
char *conf_file = NULL;
int conf_vrfy = 0;

static uid_t uid = 0;
static gid_t gid = 0;
Expand Down Expand Up @@ -273,6 +274,7 @@ static int usage(int code)
" have been installed, or when a (*,G) is installed\n"
#ifdef ENABLE_DOTCONF
" -f FILE Set configuration file, default uses ident NAME: %s\n"
" -F FILE Check configuration file syntax, use -l to increase verbosity\n"
#endif
" -h This help text\n"
" -I NAME Identity for config, PID file, and syslog, default: %s\n"
Expand Down Expand Up @@ -332,11 +334,11 @@ static char *progname(const char *arg0)
*/
int main(int argc, char *argv[])
{
int c;
int log_opts = LOG_NDELAY | LOG_PID;
int c, new_log_level = -1;

prognm = progname(argv[0]);
while ((c = getopt(argc, argv, "c:d:e:f:hI:l:m:nNp:P:st:v")) != EOF) {
while ((c = getopt(argc, argv, "c:d:e:f:F:hI:l:m:nNp:P:st:v")) != EOF) {
switch (c) {
case 'c': /* cache timeout */
cache_tmo = atoi(optarg);
Expand All @@ -350,13 +352,20 @@ int main(int argc, char *argv[])
script = optarg;
break;

case 'f':
#ifndef ENABLE_DOTCONF
case 'F':
case 'f':
warnx("Built without .conf file support.");
break;
#else
case 'F':
log_level = LOG_INFO; /* Raise log level for verify */
conf_vrfy = 1;
/* fallthrough */
case 'f':
conf_file = optarg;
#endif
break;
#endif

case 'h': /* help */
return usage(0);
Expand All @@ -366,7 +375,7 @@ int main(int argc, char *argv[])
break;

case 'l':
log_level = loglvl(optarg);
new_log_level = loglvl(optarg);
break;

case 'm':
Expand Down Expand Up @@ -423,8 +432,19 @@ int main(int argc, char *argv[])
}
}

if (new_log_level != -1)
log_level = new_log_level;

compose_paths();

if (conf_vrfy) {
smclog(LOG_INFO, "Verifying configuration file %s ...", conf_file);
c = conf_read(conf_file, do_vifs);
smclog(LOG_INFO, "Configuration file %s.", c ? "has unrecoverable errors" : "is OK");

return c;
}

if (!background && do_syslog < 1)
log_opts |= LOG_PERROR;

Expand Down

0 comments on commit 9633811

Please sign in to comment.