Split out code to parse host:port into a utility function.

This commit is contained in:
Todd C. Miller
2019-11-02 12:04:26 -06:00
parent f1d0c99e03
commit b270b0c887
5 changed files with 102 additions and 30 deletions

View File

@@ -125,6 +125,7 @@ lib/util/getopt_long.c
lib/util/gettime.c
lib/util/gidlist.c
lib/util/glob.c
lib/util/host_port.c
lib/util/inet_ntop.c
lib/util/inet_pton.c
lib/util/isblank.c

View File

@@ -207,6 +207,10 @@ __dso_public int sudo_parse_gids_v1(const char *gidstr, const gid_t *basegid, GE
__dso_public int sudo_getgrouplist2_v1(const char *name, gid_t basegid, GETGROUPS_T **groupsp, int *ngroupsp);
#define sudo_getgrouplist2(_a, _b, _c, _d) sudo_getgrouplist2_v1((_a), (_b), (_c), (_d))
/* host_port.c */
__dso_public bool sudo_parse_host_port_v1(char *str, char **hostp, char **portp, char *defport);
#define sudo_parse_host_port(_a, _b, _c, _d) sudo_parse_host_port_v1((_a), (_b), (_c), (_d))
/* key_val.c */
__dso_public char *sudo_new_key_val_v1(const char *key, const char *value);
#define sudo_new_key_val(_a, _b) sudo_new_key_val_v1((_a), (_b))

View File

@@ -115,8 +115,8 @@ DEVEL = @DEVEL@
SHELL = @SHELL@
LTOBJS = @DIGEST@ event.lo fatal.lo key_val.lo gethostname.lo \
gettime.lo getgrouplist.lo gidlist.lo lbuf.lo locking.lo \
LTOBJS = @DIGEST@ event.lo fatal.lo key_val.lo gethostname.lo gettime.lo \
getgrouplist.lo gidlist.lo host_port.lo lbuf.lo locking.lo \
logfac.lo logpri.lo mkdir_parents.lo parseln.lo progname.lo \
roundup.lo secure_path.lo setgroups.lo strsplit.lo strtobool.lo \
strtoid.lo strtomode.lo strtonum.lo sudo_conf.lo \
@@ -744,6 +744,18 @@ hltq_test.i: $(srcdir)/regress/tailq/hltq_test.c $(incdir)/compat/stdbool.h \
$(CC) -E -o $@ $(CPPFLAGS) $<
hltq_test.plog: hltq_test.i
rm -f $@; pvs-studio --cfg $(PVS_CFG) --sourcetree-root $(top_srcdir) --skip-cl-exe yes --source-file $(srcdir)/regress/tailq/hltq_test.c --i-file $< --output-file $@
host_port.lo: $(srcdir)/host_port.c $(incdir)/compat/stdbool.h \
$(incdir)/sudo_compat.h $(incdir)/sudo_debug.h \
$(incdir)/sudo_gettext.h $(incdir)/sudo_queue.h \
$(incdir)/sudo_util.h $(top_builddir)/config.h
$(LIBTOOL) $(LTFLAGS) --mode=compile $(CC) -c -o $@ $(CPPFLAGS) $(CFLAGS) $(ASAN_CFLAGS) $(PIE_CFLAGS) $(SSP_CFLAGS) $(srcdir)/host_port.c
host_port.i: $(srcdir)/host_port.c $(incdir)/compat/stdbool.h \
$(incdir)/sudo_compat.h $(incdir)/sudo_debug.h \
$(incdir)/sudo_gettext.h $(incdir)/sudo_queue.h \
$(incdir)/sudo_util.h $(top_builddir)/config.h
$(CC) -E -o $@ $(CPPFLAGS) $<
host_port.plog: host_port.i
rm -f $@; pvs-studio --cfg $(PVS_CFG) --sourcetree-root $(top_srcdir) --skip-cl-exe yes --source-file $(srcdir)/host_port.c --i-file $< --output-file $@
inet_pton.lo: $(srcdir)/inet_pton.c $(incdir)/sudo_compat.h \
$(top_builddir)/config.h
$(LIBTOOL) $(LTFLAGS) --mode=compile $(CC) -c -o $@ $(CPPFLAGS) $(CFLAGS) $(ASAN_CFLAGS) $(PIE_CFLAGS) $(SSP_CFLAGS) $(srcdir)/inet_pton.c

79
lib/util/host_port.c Normal file
View File

@@ -0,0 +1,79 @@
/*
* Copyright (c) 2019 Todd C. Miller <Todd.Miller@courtesan.com>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "config.h"
#include <sys/types.h>
#ifdef HAVE_STDBOOL_H
# include <stdbool.h>
#else
# include "compat/stdbool.h"
#endif /* HAVE_STDBOOL_H */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "sudo_gettext.h" /* must be included before sudo_compat.h */
#include "sudo_compat.h"
#include "sudo_debug.h"
#include "sudo_util.h"
/*
* Parse a string in the form host[:port] where host can also be
* an IPv4 address or an IPv6 address in square brackets.
* Modifies str.
*/
bool
sudo_parse_host_port_v1(char *str, char **hostp, char **portp, char *defport)
{
char *port, *host = str;
bool ret = false;
debug_decl(sudo_parse_host_port, SUDO_DEBUG_UTIL)
/* Check for IPv6 address like [::0] followed by optional port */
if (*host == '[') {
host++;
port = strchr(host, ']');
if (port == NULL) {
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
"invalid IPv6 address %s", str);
goto done;
}
*port++ = '\0';
if (*port == ':') {
port++;
} else if (*port == '\0') {
port = NULL; /* no port specified */
} else {
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
"invalid IPv6 address %s", str);
goto done;
}
} else {
port = strrchr(host, ':');
if (port != NULL)
*port++ = '\0';
}
if (port == NULL)
port = defport;
ret = true;
done:
debug_return_bool(ret);
}

View File

@@ -321,38 +321,14 @@ cb_listen_address(struct logsrvd_config *config, const char *str)
sudo_warn(NULL);
debug_return_bool(false);
}
host = copy;
/* Check for IPv6 address like [::0] followed by optional port */
if (*host == '[') {
host++;
port = strchr(host, ']');
if (port == NULL) {
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
"invalid IPv6 address %s", str);
/* Parse host[:port] */
if (!sudo_parse_host_port(copy, &host, &port, DEFAULT_PORT_STR))
goto done;
}
*port++ = '\0';
if (*port == ':') {
port++;
} else if (*port == '\0') {
port = NULL; /* no port specified */
} else {
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
"invalid IPv6 address %s", str);
goto done;
}
} else {
port = strrchr(host, ':');
if (port != NULL)
*port++ = '\0';
}
if (port == NULL)
port = DEFAULT_PORT_STR;
if (host[0] == '*' && host[1] == '\0')
host = NULL;
/* Resolve host (and port if it is a service). */
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;