From: Willy Tarreau Date: Tue, 8 Sep 2015 14:16:35 +0000 (+0200) Subject: BUG/MEDIUM: dns: use the correct server hostname when resolving X-Git-Tag: v1.6-dev5~10 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=07101d5a162a125232d992648a8598bfdeee3f3f;p=thirdparty%2Fhaproxy.git BUG/MEDIUM: dns: use the correct server hostname when resolving The server's host name picked for resolution was incorrect, it did not skip the address family specifier, did not resolve environment variables, and messed up with the optional trailing colon. Instead, let's get the fqdn returned by str2sa_range() and use that exclusively. --- diff --git a/src/server.c b/src/server.c index 0e5c07999f..f8fbe6a9a1 100644 --- a/src/server.c +++ b/src/server.c @@ -835,6 +835,7 @@ int parse_server(const char *file, int linenum, char **args, struct proxy *curpr char *errmsg = NULL; int err_code = 0; unsigned val; + char *fqdn = NULL; if (!strcmp(args[0], "server") || !strcmp(args[0], "default-server")) { /* server address */ int cur_arg; @@ -904,7 +905,7 @@ int parse_server(const char *file, int linenum, char **args, struct proxy *curpr * - IP:+N => port=+N, relative * - IP:-N => port=-N, relative */ - sk = str2sa_range(args[2], &port1, &port2, &errmsg, NULL, NULL); + sk = str2sa_range(args[2], &port1, &port2, &errmsg, NULL, &fqdn); if (!sk) { Alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg); err_code |= ERR_ALERT | ERR_FATAL; @@ -936,29 +937,11 @@ int parse_server(const char *file, int linenum, char **args, struct proxy *curpr } /* save hostname and create associated name resolution */ - switch (sk->ss_family) { - case AF_INET: { - /* remove the port if any */ - char *c; - if ((c = rindex(args[2], ':')) != NULL) { - newsrv->hostname = my_strndup(args[2], c - args[2]); - } - else { - newsrv->hostname = strdup(args[2]); - } - } - break; - case AF_INET6: - newsrv->hostname = strdup(args[2]); - break; - default: - goto skip_name_resolution; - } - - /* no name resolution if an IP has been provided */ - if (inet_pton(sk->ss_family, newsrv->hostname, trash.str) == 1) + newsrv->hostname = fqdn; + if (!fqdn) goto skip_name_resolution; + fqdn = NULL; if ((curr_resolution = calloc(1, sizeof(struct dns_resolution))) == NULL) goto skip_name_resolution; @@ -1779,9 +1762,11 @@ int parse_server(const char *file, int linenum, char **args, struct proxy *curpr srv_lb_commit_status(newsrv); } } + free(fqdn); return 0; out: + free(fqdn); free(errmsg); return err_code; }