From 6a250c8a1e581287d7ff3c88582ab337e21ac50f Mon Sep 17 00:00:00 2001 From: Stef Walter Date: Fri, 3 Sep 2004 02:53:40 +0000 Subject: - Fixes to configuration file stuff --- src/clamsmtpd.c | 10 +++++++--- src/clstate.c | 46 +++++++++++++++++++--------------------------- 2 files changed, 26 insertions(+), 30 deletions(-) (limited to 'src') diff --git a/src/clamsmtpd.c b/src/clamsmtpd.c index a9e8f9d..e985fa3 100644 --- a/src/clamsmtpd.c +++ b/src/clamsmtpd.c @@ -171,7 +171,7 @@ int main(int argc, char* argv[]) clstate_init(&g_state); /* Parse the arguments nicely */ - while((ch = getopt(argc, argv, "bc:d:D:h:l:m:p:qt:v")) != -1) + while((ch = getopt(argc, argv, "bc:d:D:f:h:l:m:p:qt:v")) != -1) { switch(ch) { @@ -267,7 +267,7 @@ int main(int argc, char* argv[]) } } - if(warnargs); + if(warnargs) warnx("please use configuration file instead of command-line flags: %s", configfile); argc -= optind; @@ -357,9 +357,13 @@ int main(int argc, char* argv[]) if(g_state.pidfile) pid_file(0); - clstate_cleanup(&g_state); messagex(NULL, LOG_DEBUG, "stopped"); + /* + * We have to do this at the very end because even printing + * messages requires that g_state is valid. + */ + clstate_cleanup(&g_state); return 0; } diff --git a/src/clstate.c b/src/clstate.c index 1d5f2af..3c6084c 100644 --- a/src/clstate.c +++ b/src/clstate.c @@ -159,7 +159,7 @@ int clstate_parse_config(clstate_t* state, const char* configfile) err(1, "couldn't open config file: %s", configfile); } - if(fseek(f, 0, SEEK_END) == -1 || (len = ftell(f)) == -1) + if(fseek(f, 0, SEEK_END) == -1 || (len = ftell(f)) == -1 || fseek(f, 0, SEEK_SET) == -1) err(1, "couldn't seek config file: %s", configfile); if((state->_p = (char*)malloc(len + 2)) == NULL) @@ -169,27 +169,21 @@ int clstate_parse_config(clstate_t* state, const char* configfile) err(1, "couldn't read config file: %s", configfile); fclose(f); - messagex(NULL, LOG_DEBUG, "successfully opened config file: %s", configfile); + messagex(NULL, LOG_DEBUG, "opened config file: %s", configfile); /* Double null terminate the data */ p = state->_p; p[len] = 0; p[len + 1] = 0; - /* Now split string at new lines */ - while((t = strchr(p, '\n')) != NULL) - { - *t = 0; - p = t + 1; - } - n = state->_p; /* Go through lines and process them */ - while(*n != 0) + while((t = strchr(n, '\n')) != NULL) { - p = n; /* Do this before trimming below */ - n = p + strlen(p) + 1; + *t = 0; + p = n; /* Do this before cleaning below */ + n = t + 1; p = trim_space(p); @@ -251,9 +245,9 @@ int clstate_parse_config(clstate_t* state, const char* configfile) /* Unrecognized option */ else - errx(2, "unrecognized line in config file: %s", p); + errx(2, "invalid config line: %s", p); - messagex(NULL, LOG_DEBUG, "successfully parsed line: %s", p); + messagex(NULL, LOG_DEBUG, "parsed line: %s", p); } return 0; @@ -264,18 +258,18 @@ void clstate_validate(clstate_t* state) ASSERT(state); messagex(NULL, LOG_DEBUG, "validating configuration options"); - if(state->debug_level < -1 || state->debug_level > 4) + if(!(state->debug_level == -1 || state->debug_level <= LOG_DEBUG)) errx(2, "invalid debug log level (must be between 1 and 4)"); if(state->max_threads <= 1 || state->max_threads >= 1024) - errx(2, "invalid " CFG_MAXTHREADS " (must be between 1 and 1024)"); + errx(2, "invalid setting: " CFG_MAXTHREADS " (must be between 1 and 1024)"); if(state->timeout.tv_sec <= 0) - errx(2, "invalid " CFG_TIMEOUT); + errx(2, "invalid setting: " CFG_TIMEOUT); /* This option has no default, but is required */ if(state->outname == NULL) - errx(2, "no " CFG_OUTADDR " specified in config file."); + errx(2, "no " CFG_OUTADDR " specified."); if(state->bounce == -1) errx(2, "invalid value for " CFG_BOUNCE); @@ -293,9 +287,9 @@ void clstate_validate(clstate_t* state) errx(2, "invalid " CFG_CLAMADDR " socket name: %s", state->clamname); if(strlen(state->directory) == 0) - errx(2, "invalid " CFG_DIRECTORY); + errx(2, "invalid setting: " CFG_DIRECTORY); if(state->pidfile && strlen(state->pidfile) == 0) - errx(2, "invalid " CFG_PIDFILE); + errx(2, "invalid setting: " CFG_PIDFILE); if(state->header) { @@ -312,14 +306,12 @@ void clstate_validate(clstate_t* state) void clstate_cleanup(clstate_t* state) { - if(state->_p) - { - free(state->_p); - memset(state, 0, sizeof(*state)); - messagex(NULL, LOG_DEBUG, "freed configuration option memory"); - } - /* Close the mutex */ pthread_mutex_destroy(&(state->mutex)); pthread_mutexattr_destroy(&(state->_mtxattr)); + + if(state->_p) + free(state->_p); + + memset(state, 0, sizeof(*state)); } -- cgit v1.2.3