diff options
| author | 2025-01-20 22:59:30 +0100 | |
|---|---|---|
| committer | 2025-01-20 22:59:30 +0100 | |
| commit | 4c1a2d24d374d00c656c4489db7d28f80d64f9dc (patch) | |
| tree | 81f7ba7093202247ec51861aa3a6d304d3068d84 /utils.c | |
| parent | add xs_smtp_request (diff) | |
| download | penes-snac2-4c1a2d24d374d00c656c4489db7d28f80d64f9dc.tar.gz penes-snac2-4c1a2d24d374d00c656c4489db7d28f80d64f9dc.tar.xz penes-snac2-4c1a2d24d374d00c656c4489db7d28f80d64f9dc.zip | |
add port parsing for sandboxing
Diffstat (limited to '')
| -rw-r--r-- | utils.c | 52 |
1 files changed, 52 insertions, 0 deletions
| @@ -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 | |||
| 908 | static 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 | |||
| 919 | int 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 | } | ||