Skip to content

Commit 7d3e771

Browse files
committed
Fix popular server processing to have defaults
Default value will also cause warnings to be printed.
1 parent 96d2b34 commit 7d3e771

File tree

2 files changed

+74
-36
lines changed

2 files changed

+74
-36
lines changed

src/conf.c

Lines changed: 74 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -149,13 +149,17 @@ static const struct {
149149
"sslallowedcipherlist", oSSLAllowedCipherList}, {
150150
NULL, oBadOption},};
151151

152-
static void config_notnull(const void *parm, const char *parmname);
152+
static void config_notnull(const void *, const char *);
153153
static int parse_boolean_value(char *);
154154
static void parse_auth_server(FILE *, const char *, int *);
155-
static int _parse_firewall_rule(const char *ruleset, char *leftover);
155+
static int _parse_firewall_rule(const char *, char *);
156156
static void parse_firewall_ruleset(const char *, FILE *, const char *, int *);
157+
static void parse_trusted_mac_list(const char *);
158+
static void parse_popular_servers(const char *);
159+
static void validate_popular_servers(void);
160+
static void add_popular_server(const char *);
157161

158-
static OpCodes config_parse_token(const char *cp, const char *filename, int linenum);
162+
static OpCodes config_parse_token(const char *, const char *, int);
159163

160164
/** Accessor for the current gateway configuration
161165
@return: A pointer to the current config. The pointer isn't opaque, but should be treated as READ-ONLY
@@ -633,7 +637,7 @@ void
633637
config_read(const char *filename)
634638
{
635639
FILE *fd;
636-
char line[MAX_BUF], *s, *p1, *p2;
640+
char line[MAX_BUF], *s, *p1, *p2, *rawarg = NULL;
637641
int linenum = 0, opcode, value;
638642
size_t len;
639643

@@ -669,7 +673,7 @@ config_read(const char *filename)
669673
break;
670674
len = strlen(p1);
671675
}
672-
676+
rawarg = safe_strdup(p1);
673677
if ((p2 = strchr(p1, ' '))) {
674678
p2[0] = '\0';
675679
} else if ((p2 = strstr(p1, "\r\n"))) {
@@ -722,7 +726,7 @@ config_read(const char *filename)
722726
parse_trusted_mac_list(p1);
723727
break;
724728
case oPopularServers:
725-
parse_popular_servers(p1);
729+
parse_popular_servers(rawarg);
726730
break;
727731
case oHTTPDName:
728732
config.httpdname = safe_strdup(p1);
@@ -791,6 +795,10 @@ config_read(const char *filename)
791795
}
792796
}
793797
}
798+
if (rawarg) {
799+
free(rawarg);
800+
rawarg = NULL;
801+
}
794802
}
795803

796804
if (config.httpdusername && !config.httpdpassword) {
@@ -823,7 +831,8 @@ parse_boolean_value(char *line)
823831
return -1;
824832
}
825833

826-
/* Parse possiblemac to see if it is valid MAC address format */
834+
/**
835+
* Parse possiblemac to see if it is valid MAC address format */
827836
int
828837
check_mac_format(char *possiblemac)
829838
{
@@ -834,7 +843,10 @@ check_mac_format(char *possiblemac)
834843
hex2, hex2, hex2, hex2, hex2, hex2) == 6;
835844
}
836845

837-
void
846+
/** @internal
847+
* Parse the trusted mac list.
848+
*/
849+
static void
838850
parse_trusted_mac_list(const char *ptr)
839851
{
840852
char *ptrcopy = NULL;
@@ -849,7 +861,7 @@ parse_trusted_mac_list(const char *ptr)
849861
/* strsep modifies original, so let's make a copy */
850862
ptrcopy = safe_strdup(ptr);
851863

852-
while ((possiblemac = strsep(&ptrcopy, ", "))) {
864+
while ((possiblemac = strsep(&ptrcopy, ","))) {
853865
/* check for valid format */
854866
if (!check_mac_format(possiblemac)) {
855867
debug(LOG_ERR,
@@ -906,46 +918,61 @@ parse_trusted_mac_list(const char *ptr)
906918

907919
}
908920

909-
void
921+
/** @internal
922+
* Add a popular server to the list. It prepends for simplicity.
923+
* @param server The hostname to add.
924+
*/
925+
static void
926+
add_popular_server(const char *server)
927+
{
928+
t_popular_server *p = NULL;
929+
930+
p = (t_popular_server *)safe_malloc(sizeof(t_popular_server));
931+
p->hostname = safe_strdup(server);
932+
933+
if (config.popular_servers == NULL) {
934+
p->next = NULL;
935+
config.popular_servers = p;
936+
} else {
937+
p->next = config.popular_servers;
938+
config.popular_servers = p;
939+
}
940+
}
941+
942+
static void
910943
parse_popular_servers(const char *ptr)
911944
{
912945
char *ptrcopy = NULL;
913946
char *hostname = NULL;
914-
t_popular_server *p = NULL;
947+
char *tmp = NULL;
915948

916949
debug(LOG_DEBUG, "Parsing string [%s] for popular servers", ptr);
917950

918-
// max length of domain name is 253 characters
919-
hostname = safe_malloc(254);
920-
921951
/* strsep modifies original, so let's make a copy */
922952
ptrcopy = safe_strdup(ptr);
923953

924-
while ((hostname = strsep(&ptrcopy, ", "))) {
925-
if (strcmp(hostname, "") == 0) {
954+
while ((hostname = strsep(&ptrcopy, ","))) { /* hostname does *not* need allocation. strsep
955+
provides a pointer in ptrcopy. */
956+
/* Skip leading spaces. */
957+
while (*hostname != '\0' && isblank(*hostname)) {
958+
hostname++;
959+
}
960+
if (*hostname == '\0') { /* Equivalent to strcmp(hostname, "") == 0 */
926961
continue;
927962
}
928-
debug(LOG_DEBUG, "Adding Popular Server [%s] to list", hostname);
929-
930-
if (config.popular_servers == NULL) {
931-
config.popular_servers = safe_malloc(sizeof(t_popular_server));
932-
config.popular_servers->hostname = safe_strdup(hostname);
933-
config.popular_servers->next = NULL;
934-
} else {
935-
p = config.popular_servers;
936-
/* Advance to the last entry */
937-
while (p->next != NULL) {
938-
p = p->next;
939-
}
940-
p->next = safe_malloc(sizeof(t_popular_server));
941-
p = p->next;
942-
p->hostname = safe_strdup(hostname);
943-
p->next = NULL;
963+
/* Remove any trailing blanks. */
964+
tmp = hostname;
965+
while (*tmp != '\0' && !isblank(*tmp)) {
966+
tmp++;
944967
}
968+
if (*tmp != '\0' && isblank(*tmp)) {
969+
*tmp = '\0';
970+
}
971+
debug(LOG_DEBUG, "Adding Popular Server [%s] to list", hostname);
972+
add_popular_server(hostname);
945973
}
946974

947975
free(ptrcopy);
948-
free(hostname);
949976
}
950977

951978
/** Verifies if the configuration is complete and valid. Terminates the program if it isn't */
@@ -954,13 +981,27 @@ config_validate(void)
954981
{
955982
config_notnull(config.gw_interface, "GatewayInterface");
956983
config_notnull(config.auth_servers, "AuthServer");
984+
validate_popular_servers();
957985

958986
if (missing_parms) {
959987
debug(LOG_ERR, "Configuration is not complete, exiting...");
960988
exit(-1);
961989
}
962990
}
963991

992+
/** @internal
993+
* Validate that popular servers are populated or log a warning and set a default.
994+
*/
995+
static void
996+
validate_popular_servers(void)
997+
{
998+
if (config.popular_servers == NULL) {
999+
debug(LOG_WARNING, "PopularServers not set in config file, this will become fatal in a future version.");
1000+
add_popular_server("www.google.com");
1001+
add_popular_server("www.yahoo.com");
1002+
}
1003+
}
1004+
9641005
/** @internal
9651006
Verifies that a required parameter is not a null pointer
9661007
*/

src/conf.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -218,9 +218,6 @@ void mark_auth_server_bad(t_auth_serv *);
218218
/** @brief Fetch a firewall rule set. */
219219
t_firewall_rule *get_ruleset(const char *);
220220

221-
void parse_trusted_mac_list(const char *);
222-
223-
void parse_popular_servers(const char *);
224221

225222
#define LOCK_CONFIG() do { \
226223
debug(LOG_DEBUG, "Locking config"); \

0 commit comments

Comments
 (0)