aboutsummaryrefslogtreecommitdiff
path: root/utils.c
diff options
context:
space:
mode:
authorshtrophic <christoph@liebender.dev>2025-01-20 22:59:30 +0100
committershtrophic <christoph@liebender.dev>2025-01-20 22:59:30 +0100
commit4c1a2d24d374d00c656c4489db7d28f80d64f9dc (patch)
tree81f7ba7093202247ec51861aa3a6d304d3068d84 /utils.c
parent4e3d596bee1cee2c4146265eb6ac827f09820c53 (diff)
add port parsing for sandboxing
Diffstat (limited to 'utils.c')
-rw-r--r--utils.c52
1 files changed, 52 insertions, 0 deletions
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;
+}