From bfd3904c4439680060948402ed546ffa3532be67 Mon Sep 17 00:00:00 2001 From: mrb0nk500 Date: Fri, 11 Jun 2021 19:38:57 -0400 Subject: Change `port` from an int to a string, and move the valid port check into the config parser. --- config.c | 11 +++++++++++ config.h | 4 ++-- pullreqd.c | 9 +-------- 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/config.c b/config.c index 5f464d1..26ad023 100644 --- a/config.c +++ b/config.c @@ -2,6 +2,7 @@ #include #include #include +#include #include "config.h" char *read_file(const char *filename, long *size) { @@ -152,6 +153,16 @@ config *parse_config(const char *filename) { /* Is the config option valid? */ if (opt->name != NULL) { config_val val = parse_option_value(opt, value); + if (opt->offset == offsetof(config, port)) { + const int port = strtol(val.str, NULL, 0); + if (port <= 0 || port > 65535) { + syslog(LOG_ERR, "Invalid port %d. (Valid port must be between 1, and 65535.)", port); + cleanup_config(cfg); + free(line); + free(buf); + return NULL; + } + } set_config_opt(cfg, opt->type, opt->offset, val); } diff --git a/config.h b/config.h index e461369..5ba0978 100644 --- a/config.h +++ b/config.h @@ -21,7 +21,7 @@ struct config { char *git_root; /* Root of git server. */ char *sock_type; /* Type of socket. */ char *sock; /* Socket address. */ - int port; /* Listen port. */ + char *port; /* Listen port. */ int merge_type; /* Merge type. */ char *pr_root; /* Root of pull requests. */ char *key_path; /* Path to maintainer authentication key file. */ @@ -44,7 +44,7 @@ static const config_opt config_opts[] = { {"git-root", "Root of git server (can also be a url).", TYPE_STRING, offsetof(config, git_root)}, {"socket-type", "Socket type to use (options: unix, network. default: network).", TYPE_STRING, offsetof(config, sock_type)}, {"socket", "Path, IP address, or domain name of socket.", TYPE_STRING, offsetof(config, sock)}, - {"port", "Port to listen on (network socket only).", TYPE_INT, offsetof(config, port)}, + {"port", "Port to listen on (network socket only).", TYPE_STRING, offsetof(config, port)}, {"merge-type", "Type of merge (options: 0 = Merge individually, 1 = Merge into one).", TYPE_INT, offsetof(config, merge_type)}, {"pr-root", "Directory to store pull requests in.", TYPE_STRING, offsetof(config, pr_root)}, {"key-file", "Path to file containing gpg/pgp public keys of each maintainer.", TYPE_STRING, offsetof(config, key_path)}, diff --git a/pullreqd.c b/pullreqd.c index a00d85c..f077a50 100644 --- a/pullreqd.c +++ b/pullreqd.c @@ -109,15 +109,8 @@ int init_socket(config *cfg) { sock_addr.sa_un.sun_family = sock_type; memcpy(sock_addr.sa_un.sun_path, cfg->sock, path_length+1); } else { - if (cfg->port <= 0 || cfg->port > 65535) { - syslog(LOG_ERR, "Invalid port %d. (Valid port must be between 1, and 65535.)", cfg->port); - return -1; - } struct addrinfo hints, *ainfo_root, *ainfo; - const size_t size = snprintf(NULL, 0, "%d", cfg->port) + 1; - char *port_str = malloc(size); - sprintf(port_str, "%d", cfg->port); memset(&hints, 0, sizeof(hints)); hints.ai_family = AF_UNSPEC; @@ -125,7 +118,7 @@ int init_socket(config *cfg) { /*hints.ai_protocol = IPPROTO_SCTP;*/ hints.ai_flags = AI_PASSIVE; - const int gai_ret = getaddrinfo(cfg->sock, port_str, &hints, &ainfo_root); + const int gai_ret = getaddrinfo(cfg->sock, cfg->port, &hints, &ainfo_root); /* Did getaddrinfo fail? */ if (gai_ret) { syslog(LOG_ERR, "getaddrinfo() failed for hostname %s. Reason: %s", cfg->sock, gai_strerror(gai_ret)); -- cgit v1.2.3-13-gbd6f