Skip to content

Commit 1188232

Browse files
committed
Merge pull request #203 from acv/remove-hardcoded-servers
Remove hardcoded servers
2 parents 24c8e65 + 7d3e771 commit 1188232

File tree

4 files changed

+116
-22
lines changed

4 files changed

+116
-22
lines changed

src/centralserver.c

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -159,15 +159,10 @@ _connect_auth_server(int level)
159159
{
160160
s_config *config = config_get_config();
161161
t_auth_serv *auth_server = NULL;
162+
t_popular_server *popular_server = NULL;
162163
struct in_addr *h_addr;
163164
int num_servers = 0;
164165
char *hostname = NULL;
165-
char *popular_servers[] = {
166-
"www.google.com",
167-
"www.yahoo.com",
168-
NULL
169-
};
170-
char **popularserver;
171166
char *ip;
172167
struct sockaddr_in their_addr;
173168
int sockfd;
@@ -207,20 +202,18 @@ _connect_auth_server(int level)
207202
if (!h_addr) {
208203
/*
209204
* DNS resolving it failed
210-
*
211-
* Can we resolve any of the popular servers ?
212205
*/
213206
debug(LOG_DEBUG, "Level %d: Resolving auth server [%s] failed", level, hostname);
214207

215-
for (popularserver = popular_servers; *popularserver; popularserver++) {
216-
debug(LOG_DEBUG, "Level %d: Resolving popular server [%s]", level, *popularserver);
217-
h_addr = wd_gethostbyname(*popularserver);
208+
for (popular_server = config->popular_servers; popular_server; popular_server = popular_server->next) {
209+
debug(LOG_DEBUG, "Level %d: Resolving popular server [%s]", level, popular_server->hostname);
210+
h_addr = wd_gethostbyname(popular_server->hostname);
218211
if (h_addr) {
219-
debug(LOG_DEBUG, "Level %d: Resolving popular server [%s] succeeded = [%s]", level, *popularserver,
212+
debug(LOG_DEBUG, "Level %d: Resolving popular server [%s] succeeded = [%s]", level, popular_server->hostname,
220213
inet_ntoa(*h_addr));
221214
break;
222215
} else {
223-
debug(LOG_DEBUG, "Level %d: Resolving popular server [%s] failed", level, *popularserver);
216+
debug(LOG_DEBUG, "Level %d: Resolving popular server [%s] failed", level, popular_server->hostname);
224217
}
225218
}
226219

src/conf.c

Lines changed: 97 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ typedef enum {
9696
oFirewallRule,
9797
oFirewallRuleSet,
9898
oTrustedMACList,
99+
oPopularServers,
99100
oHtmlMessageFile,
100101
oProxyPort,
101102
oSSLPeerVerification,
@@ -140,20 +141,25 @@ static const struct {
140141
"firewallruleset", oFirewallRuleSet}, {
141142
"firewallrule", oFirewallRule}, {
142143
"trustedmaclist", oTrustedMACList}, {
144+
"popularservers", oPopularServers}, {
143145
"htmlmessagefile", oHtmlMessageFile}, {
144146
"proxyport", oProxyPort}, {
145147
"sslpeerverification", oSSLPeerVerification}, {
146148
"sslcertpath", oSSLCertPath}, {
147149
"sslallowedcipherlist", oSSLAllowedCipherList}, {
148150
NULL, oBadOption},};
149151

150-
static void config_notnull(const void *parm, const char *parmname);
152+
static void config_notnull(const void *, const char *);
151153
static int parse_boolean_value(char *);
152154
static void parse_auth_server(FILE *, const char *, int *);
153-
static int _parse_firewall_rule(const char *ruleset, char *leftover);
155+
static int _parse_firewall_rule(const char *, char *);
154156
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 *);
155161

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

158164
/** Accessor for the current gateway configuration
159165
@return: A pointer to the current config. The pointer isn't opaque, but should be treated as READ-ONLY
@@ -189,6 +195,7 @@ config_init(void)
189195
config.internal_sock = safe_strdup(DEFAULT_INTERNAL_SOCK);
190196
config.rulesets = NULL;
191197
config.trustedmaclist = NULL;
198+
config.popular_servers = NULL;
192199
config.proxy_port = 0;
193200
config.ssl_certs = safe_strdup(DEFAULT_AUTHSERVSSLCERTPATH);
194201
config.ssl_verify = DEFAULT_AUTHSERVSSLPEERVER;
@@ -630,7 +637,7 @@ void
630637
config_read(const char *filename)
631638
{
632639
FILE *fd;
633-
char line[MAX_BUF], *s, *p1, *p2;
640+
char line[MAX_BUF], *s, *p1, *p2, *rawarg = NULL;
634641
int linenum = 0, opcode, value;
635642
size_t len;
636643

@@ -666,7 +673,7 @@ config_read(const char *filename)
666673
break;
667674
len = strlen(p1);
668675
}
669-
676+
rawarg = safe_strdup(p1);
670677
if ((p2 = strchr(p1, ' '))) {
671678
p2[0] = '\0';
672679
} else if ((p2 = strstr(p1, "\r\n"))) {
@@ -718,6 +725,9 @@ config_read(const char *filename)
718725
case oTrustedMACList:
719726
parse_trusted_mac_list(p1);
720727
break;
728+
case oPopularServers:
729+
parse_popular_servers(rawarg);
730+
break;
721731
case oHTTPDName:
722732
config.httpdname = safe_strdup(p1);
723733
break;
@@ -785,6 +795,10 @@ config_read(const char *filename)
785795
}
786796
}
787797
}
798+
if (rawarg) {
799+
free(rawarg);
800+
rawarg = NULL;
801+
}
788802
}
789803

790804
if (config.httpdusername && !config.httpdpassword) {
@@ -817,7 +831,8 @@ parse_boolean_value(char *line)
817831
return -1;
818832
}
819833

820-
/* Parse possiblemac to see if it is valid MAC address format */
834+
/**
835+
* Parse possiblemac to see if it is valid MAC address format */
821836
int
822837
check_mac_format(char *possiblemac)
823838
{
@@ -828,7 +843,10 @@ check_mac_format(char *possiblemac)
828843
hex2, hex2, hex2, hex2, hex2, hex2) == 6;
829844
}
830845

831-
void
846+
/** @internal
847+
* Parse the trusted mac list.
848+
*/
849+
static void
832850
parse_trusted_mac_list(const char *ptr)
833851
{
834852
char *ptrcopy = NULL;
@@ -843,7 +861,7 @@ parse_trusted_mac_list(const char *ptr)
843861
/* strsep modifies original, so let's make a copy */
844862
ptrcopy = safe_strdup(ptr);
845863

846-
while ((possiblemac = strsep(&ptrcopy, ", "))) {
864+
while ((possiblemac = strsep(&ptrcopy, ","))) {
847865
/* check for valid format */
848866
if (!check_mac_format(possiblemac)) {
849867
debug(LOG_ERR,
@@ -900,19 +918,90 @@ parse_trusted_mac_list(const char *ptr)
900918

901919
}
902920

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
943+
parse_popular_servers(const char *ptr)
944+
{
945+
char *ptrcopy = NULL;
946+
char *hostname = NULL;
947+
char *tmp = NULL;
948+
949+
debug(LOG_DEBUG, "Parsing string [%s] for popular servers", ptr);
950+
951+
/* strsep modifies original, so let's make a copy */
952+
ptrcopy = safe_strdup(ptr);
953+
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 */
961+
continue;
962+
}
963+
/* Remove any trailing blanks. */
964+
tmp = hostname;
965+
while (*tmp != '\0' && !isblank(*tmp)) {
966+
tmp++;
967+
}
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);
973+
}
974+
975+
free(ptrcopy);
976+
}
977+
903978
/** Verifies if the configuration is complete and valid. Terminates the program if it isn't */
904979
void
905980
config_validate(void)
906981
{
907982
config_notnull(config.gw_interface, "GatewayInterface");
908983
config_notnull(config.auth_servers, "AuthServer");
984+
validate_popular_servers();
909985

910986
if (missing_parms) {
911987
debug(LOG_ERR, "Configuration is not complete, exiting...");
912988
exit(-1);
913989
}
914990
}
915991

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+
9161005
/** @internal
9171006
Verifies that a required parameter is not a null pointer
9181007
*/

src/conf.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,14 @@ typedef struct _trusted_mac_t {
143143
struct _trusted_mac_t *next;
144144
} t_trusted_mac;
145145

146+
/**
147+
* Popular Servers
148+
*/
149+
typedef struct _popular_server_t {
150+
char *hostname;
151+
struct _popular_server_t *next;
152+
} t_popular_server;
153+
146154
/**
147155
* Configuration structure
148156
*/
@@ -183,6 +191,7 @@ typedef struct {
183191
t_trusted_mac *trustedmaclist; /**< @brief list of trusted macs */
184192
char *arp_table_path; /**< @brief Path to custom ARP table, formatted
185193
like /proc/net/arp */
194+
t_popular_server *popular_servers; /**< @brief list of popular servers */
186195
} s_config;
187196

188197
/** @brief Get the current gateway configuration */
@@ -209,7 +218,6 @@ void mark_auth_server_bad(t_auth_serv *);
209218
/** @brief Fetch a firewall rule set. */
210219
t_firewall_rule *get_ruleset(const char *);
211220

212-
void parse_trusted_mac_list(const char *);
213221

214222
#define LOCK_CONFIG() do { \
215223
debug(LOG_DEBUG, "Locking config"); \

wifidog.conf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,10 @@ ClientTimeout 5
221221
# Default: none
222222
# Optional
223223
#
224+
225+
# Check DNS health by querying IPs of these hosts
226+
PopularServers kernel.org,ieee.org
227+
224228
# Comma separated list of MAC addresses who are allowed to pass
225229
# through without authentication.
226230
# N.B.: weak security, since MAC addresses are easy to spoof.

0 commit comments

Comments
 (0)