snac2

Fork of https://codeberg.org/grunfink/snac2
git clone https://git.inz.fi/snac2
Log | Files | Refs | README | LICENSE

commit 3d96c576287736ebdf87e4a7b956842a6c6e055f
parent 1c4c15540360a5db9d56e6ae8d206a1f30a4f52b
Author: shtrophic <christoph@liebender.dev>
Date:   Fri, 24 Jan 2025 22:24:05 +0100

enforce tls when supported && add tests

Diffstat:
M.gitignore | 3++-
Mactivitypub.c | 3++-
Atests/smtp.c | 25+++++++++++++++++++++++++
Mutils.c | 9+++++++--
Mxs_curl.h | 22++++++++++++++++------
5 files changed, 52 insertions(+), 10 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -1,2 +1,3 @@ -*.o +**/*.o +tests/smtp snac diff --git 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 @@ -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 "<snac-smtp-test@locahost>" + +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 @@ -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 @@ -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; }