From 4c1a2d24d374d00c656c4489db7d28f80d64f9dc Mon Sep 17 00:00:00 2001 From: shtrophic Date: Mon, 20 Jan 2025 22:59:30 +0100 Subject: add port parsing for sandboxing --- utils.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) (limited to 'utils.c') diff --git a/utils.c b/utils.c index a5b1124..3b0a78f 100644 --- a/utils.c +++ b/utils.c @@ -904,3 +904,55 @@ void import_csv(snac *user) else snac_log(user, xs_fmt("Cannot open file %s", fn)); } + +static const struct { + const char *proto; + unsigned short default_port; +} FALLBACK_PORTS[] = { + /* caution: https > http, smpts > smtp */ + {"https", 443}, + {"http", 80}, + {"smtps", 465}, + {"smtp", 25} +}; + +int parse_port(const char *url, const char **errstr) +{ + const char *col, *rcol; + int tmp, ret = -1; + + if (errstr) + *errstr = NULL; + + if (!(col = strchr(url, ':'))) { + if (errstr) + *errstr = "bad url"; + return -1; + } + + for (size_t i = 0; i < sizeof(FALLBACK_PORTS) / sizeof(*FALLBACK_PORTS); ++i) { + if (memcmp(url, FALLBACK_PORTS[i].proto, strlen(FALLBACK_PORTS[i].proto)) == 0) { + ret = FALLBACK_PORTS[i].default_port; + break; + } + } + + if (!(rcol = strchr(col + 1, ':'))) + rcol = col; + + if (rcol) { + tmp = atoi(rcol + 1); + if (tmp == 0) { + if (ret != -1) + return ret; + + *errstr = strerror(errno); + return -1; + } + + return tmp; + } + + *errstr = "unknown protocol"; + return -1; +} -- cgit v1.2.3