summaryrefslogtreecommitdiff
path: root/utils.c
diff options
context:
space:
mode:
authorGravatar shtrophic2025-01-20 22:59:30 +0100
committerGravatar shtrophic2025-01-20 22:59:30 +0100
commit4c1a2d24d374d00c656c4489db7d28f80d64f9dc (patch)
tree81f7ba7093202247ec51861aa3a6d304d3068d84 /utils.c
parentadd xs_smtp_request (diff)
downloadsnac2-4c1a2d24d374d00c656c4489db7d28f80d64f9dc.tar.gz
snac2-4c1a2d24d374d00c656c4489db7d28f80d64f9dc.tar.xz
snac2-4c1a2d24d374d00c656c4489db7d28f80d64f9dc.zip
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)
904 else 904 else
905 snac_log(user, xs_fmt("Cannot open file %s", fn)); 905 snac_log(user, xs_fmt("Cannot open file %s", fn));
906} 906}
907
908static const struct {
909 const char *proto;
910 unsigned short default_port;
911} FALLBACK_PORTS[] = {
912 /* caution: https > http, smpts > smtp */
913 {"https", 443},
914 {"http", 80},
915 {"smtps", 465},
916 {"smtp", 25}
917};
918
919int parse_port(const char *url, const char **errstr)
920{
921 const char *col, *rcol;
922 int tmp, ret = -1;
923
924 if (errstr)
925 *errstr = NULL;
926
927 if (!(col = strchr(url, ':'))) {
928 if (errstr)
929 *errstr = "bad url";
930 return -1;
931 }
932
933 for (size_t i = 0; i < sizeof(FALLBACK_PORTS) / sizeof(*FALLBACK_PORTS); ++i) {
934 if (memcmp(url, FALLBACK_PORTS[i].proto, strlen(FALLBACK_PORTS[i].proto)) == 0) {
935 ret = FALLBACK_PORTS[i].default_port;
936 break;
937 }
938 }
939
940 if (!(rcol = strchr(col + 1, ':')))
941 rcol = col;
942
943 if (rcol) {
944 tmp = atoi(rcol + 1);
945 if (tmp == 0) {
946 if (ret != -1)
947 return ret;
948
949 *errstr = strerror(errno);
950 return -1;
951 }
952
953 return tmp;
954 }
955
956 *errstr = "unknown protocol";
957 return -1;
958}