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 From 3d96c576287736ebdf87e4a7b956842a6c6e055f Mon Sep 17 00:00:00 2001 From: shtrophic Date: Fri, 24 Jan 2025 22:24:05 +0100 Subject: enforce tls when supported && add tests --- .gitignore | 3 ++- activitypub.c | 3 ++- tests/smtp.c | 24 ++++++++++++++++++++++++ utils.c | 9 +++++++-- xs_curl.h | 22 ++++++++++++++++------ 5 files changed, 51 insertions(+), 10 deletions(-) create mode 100644 tests/smtp.c (limited to 'utils.c') diff --git a/.gitignore b/.gitignore index 7ead966..6ebb150 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ -*.o +**/*.o +tests/smtp snac diff --git a/activitypub.c b/activitypub.c index 4cb779a..f3b2bae 100644 --- a/activitypub.c +++ b/activitypub.c @@ -2538,8 +2538,9 @@ int send_email(const xs_dict *mailinfo) *from = xs_dict_get(mailinfo, "from"), *to = xs_dict_get(mailinfo, "to"), *body = xs_dict_get(mailinfo, "body"); + int smtp_port = parse_port(url, NULL); - return xs_smtp_request(url, user, pass, from, to, body); + return xs_smtp_request(url, user, pass, from, to, body, smtp_port == 465 || smtp_port == 587); } diff --git a/tests/smtp.c b/tests/smtp.c new file mode 100644 index 0000000..1100a9d --- /dev/null +++ b/tests/smtp.c @@ -0,0 +1,24 @@ +/* snac - A simple, minimalistic ActivityPub instance */ +/* copyright (c) 2022 - 2025 grunfink et al. / MIT license */ + +#define XS_IMPLEMENTATION +#include "../xs.h" +#include "../xs_curl.h" + +#define FROM "" + +int main(void) { + xs *to = xs_fmt("<%s@localhost>", getenv("USER")), + *body = xs_fmt("" + "To: %s \r\n" + "From: " FROM "\r\n" + "Subject: snac smtp test\r\n" + "\r\n" + "If you read this as an email, it probably worked!\r\n", + to); + + return xs_smtp_request("smtp://localhost", NULL, NULL, + FROM, + to, + body, 0); +} \ No newline at end of file diff --git a/utils.c b/utils.c index faf6d12..dbf798a 100644 --- a/utils.c +++ b/utils.c @@ -931,6 +931,7 @@ int parse_port(const char *url, const char **errstr) if (!(col = strchr(url, ':'))) { if (errstr) *errstr = "bad url"; + return -1; } @@ -950,13 +951,17 @@ int parse_port(const char *url, const char **errstr) if (ret != -1) return ret; - *errstr = strerror(errno); + if (errstr) + *errstr = strerror(errno); + return -1; } return tmp; } - *errstr = "unknown protocol"; + if (errstr) + *errstr = "unknown protocol"; + return -1; } diff --git a/xs_curl.h b/xs_curl.h index 886aee0..d98ac4c 100644 --- a/xs_curl.h +++ b/xs_curl.h @@ -10,7 +10,8 @@ xs_dict *xs_http_request(const char *method, const char *url, xs_str **payload, int *p_size, int timeout); int xs_smtp_request(const char *url, const char *user, const char *pass, - const char *from, const char *to, const xs_str *body); + const char *from, const char *to, const xs_str *body, + int use_ssl); #ifdef XS_IMPLEMENTATION @@ -198,7 +199,8 @@ xs_dict *xs_http_request(const char *method, const char *url, } int xs_smtp_request(const char *url, const char *user, const char *pass, - const char *from, const char *to, const xs_str *body) + const char *from, const char *to, const xs_str *body, + int use_ssl) { CURL *curl; CURLcode res = CURLE_OK; @@ -212,11 +214,19 @@ int xs_smtp_request(const char *url, const char *user, const char *pass, curl = curl_easy_init(); curl_easy_setopt(curl, CURLOPT_URL, url); - curl_easy_setopt(curl, CURLOPT_USERNAME, user); - curl_easy_setopt(curl, CURLOPT_PASSWORD, pass); + if (user && pass) { + /* allow authless connections, to, e.g. localhost */ + curl_easy_setopt(curl, CURLOPT_USERNAME, user); + curl_easy_setopt(curl, CURLOPT_PASSWORD, pass); + } + + if (use_ssl) + curl_easy_setopt(curl, CURLOPT_USE_SSL, (long)CURLUSESSL_ALL); curl_easy_setopt(curl, CURLOPT_MAIL_FROM, from); - curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, rcpt = curl_slist_append(rcpt, to)); + + rcpt = curl_slist_append(rcpt, to); + curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, rcpt); curl_easy_setopt(curl, CURLOPT_READDATA, &pd); curl_easy_setopt(curl, CURLOPT_READFUNCTION, _post_callback); @@ -224,8 +234,8 @@ int xs_smtp_request(const char *url, const char *user, const char *pass, res = curl_easy_perform(curl); - curl_slist_free_all(rcpt); curl_easy_cleanup(curl); + curl_slist_free_all(rcpt); return (int)res; } -- cgit From ae9afd47e7b9df111312bb4a6243fdc05101dd55 Mon Sep 17 00:00:00 2001 From: grunfink Date: Sun, 27 Apr 2025 05:14:09 +0200 Subject: Replaced tabs with spaces. --- utils.c | 68 ++++++++++++++++++++++++++++++++--------------------------------- 1 file changed, 34 insertions(+), 34 deletions(-) (limited to 'utils.c') diff --git a/utils.c b/utils.c index bf55b9f..576b17c 100644 --- a/utils.c +++ b/utils.c @@ -914,58 +914,58 @@ void import_csv(snac *user) } static const struct { - const char *proto; - unsigned short default_port; + const char *proto; + unsigned short default_port; } FALLBACK_PORTS[] = { - /* caution: https > http, smpts > smtp */ - {"https", 443}, - {"http", 80}, + /* caution: https > http, smpts > smtp */ + {"https", 443}, + {"http", 80}, {"smtps", 465}, - {"smtp", 25} + {"smtp", 25} }; int parse_port(const char *url, const char **errstr) { - const char *col, *rcol; - int tmp, ret = -1; + const char *col, *rcol; + int tmp, ret = -1; if (errstr) - *errstr = NULL; + *errstr = NULL; - if (!(col = strchr(url, ':'))) { + 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); + *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) + if (ret != -1) return ret; - + if (errstr) *errstr = strerror(errno); return -1; } - - return tmp; - } + + return tmp; + } if (errstr) - *errstr = "unknown protocol"; + *errstr = "unknown protocol"; - return -1; + return -1; } -- cgit From bf94f6af7741f488f96deb16071707f11e692550 Mon Sep 17 00:00:00 2001 From: postscriptum Date: Tue, 20 May 2025 21:29:17 +0300 Subject: make greetings theme adaptive; define style in header --- utils.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'utils.c') diff --git a/utils.c b/utils.c index 576b17c..d50707a 100644 --- a/utils.c +++ b/utils.c @@ -101,8 +101,9 @@ static const char *greeting_html = "\n" "\n" "\n" + "\n" "Welcome to %host%\n\n" - "\n" + "\n" "%blurb%" "

The following users are part of this community:

\n" "\n" -- cgit