From 4e3d596bee1cee2c4146265eb6ac827f09820c53 Mon Sep 17 00:00:00 2001 From: shtrophic Date: Mon, 20 Jan 2025 19:43:42 +0100 Subject: add xs_smtp_request --- xs_curl.h | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) (limited to 'xs_curl.h') diff --git a/xs_curl.h b/xs_curl.h index f0cfd98..886aee0 100644 --- a/xs_curl.h +++ b/xs_curl.h @@ -9,6 +9,9 @@ xs_dict *xs_http_request(const char *method, const char *url, const xs_str *body, int b_size, int *status, 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); + #ifdef XS_IMPLEMENTATION #include @@ -194,6 +197,39 @@ xs_dict *xs_http_request(const char *method, const char *url, return response; } +int xs_smtp_request(const char *url, const char *user, const char *pass, + const char *from, const char *to, const xs_str *body) +{ + CURL *curl; + CURLcode res = CURLE_OK; + struct curl_slist *rcpt = NULL; + struct _payload_data pd = { + .data = (char *)body, + .size = xs_size(body), + .offset = 0 + }; + + 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); + + curl_easy_setopt(curl, CURLOPT_MAIL_FROM, from); + curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, rcpt = curl_slist_append(rcpt, to)); + + curl_easy_setopt(curl, CURLOPT_READDATA, &pd); + curl_easy_setopt(curl, CURLOPT_READFUNCTION, _post_callback); + curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L); + + res = curl_easy_perform(curl); + + curl_slist_free_all(rcpt); + curl_easy_cleanup(curl); + + return (int)res; +} + #endif /* XS_IMPLEMENTATION */ #endif /* _XS_CURL_H */ -- 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 'xs_curl.h') 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 94a07f93d527ecef3376de566ccf7d74fa2dadd3 Mon Sep 17 00:00:00 2001 From: grunfink Date: Sun, 27 Apr 2025 06:01:05 +0200 Subject: Fixed length error in xs_smtp_request(). --- xs_curl.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'xs_curl.h') diff --git a/xs_curl.h b/xs_curl.h index 0609a08..ac543b1 100644 --- a/xs_curl.h +++ b/xs_curl.h @@ -200,6 +200,7 @@ xs_dict *xs_http_request(const char *method, const char *url, return response; } + int xs_smtp_request(const char *url, const char *user, const char *pass, const char *from, const char *to, const xs_str *body, int use_ssl) @@ -209,7 +210,7 @@ int xs_smtp_request(const char *url, const char *user, const char *pass, struct curl_slist *rcpt = NULL; struct _payload_data pd = { .data = (char *)body, - .size = xs_size(body), + .size = strlen(body), .offset = 0 }; -- cgit From 64d99af19ce864ffefec50fcf0d19d2d65411c35 Mon Sep 17 00:00:00 2001 From: grunfink Date: Sun, 4 May 2025 11:05:47 +0200 Subject: xs_webmention.h new file. --- Makefile | 6 +-- Makefile.NetBSD | 5 ++- snac.c | 1 + xs_curl.h | 4 +- xs_version.h | 2 +- xs_webmention.h | 118 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 128 insertions(+), 8 deletions(-) create mode 100644 xs_webmention.h (limited to 'xs_curl.h') diff --git a/Makefile b/Makefile index d6389b5..46fdb09 100644 --- a/Makefile +++ b/Makefile @@ -56,7 +56,7 @@ format.o: format.c xs.h xs_regex.h xs_mime.h xs_html.h xs_json.h \ xs_time.h xs_match.h snac.h http_codes.h html.o: html.c xs.h xs_io.h xs_json.h xs_regex.h xs_set.h xs_openssl.h \ xs_time.h xs_mime.h xs_match.h xs_html.h xs_curl.h xs_unicode.h xs_url.h \ - snac.h http_codes.h + xs_random.h snac.h http_codes.h http.o: http.c xs.h xs_io.h xs_openssl.h xs_curl.h xs_time.h xs_json.h \ snac.h http_codes.h httpd.o: httpd.c xs.h xs_io.h xs_json.h xs_socket.h xs_unix_socket.h \ @@ -71,10 +71,10 @@ sandbox.o: sandbox.c xs.h snac.h http_codes.h snac.o: snac.c xs.h xs_hex.h xs_io.h xs_unicode_tbl.h xs_unicode.h \ xs_json.h xs_curl.h xs_openssl.h xs_socket.h xs_unix_socket.h xs_url.h \ xs_httpd.h xs_mime.h xs_regex.h xs_set.h xs_time.h xs_glob.h xs_random.h \ - xs_match.h xs_fcgi.h xs_html.h xs_po.h snac.h http_codes.h + xs_match.h xs_fcgi.h xs_html.h xs_po.h xs_webmention.h snac.h \ + http_codes.h upgrade.o: upgrade.c xs.h xs_io.h xs_json.h xs_glob.h snac.h http_codes.h utils.o: utils.c xs.h xs_io.h xs_json.h xs_time.h xs_openssl.h \ xs_random.h xs_glob.h xs_curl.h xs_regex.h snac.h http_codes.h webfinger.o: webfinger.c xs.h xs_json.h xs_curl.h xs_mime.h snac.h \ http_codes.h -tests/smtp.o: tests/smtp.c xs.h xs_curl.h diff --git a/Makefile.NetBSD b/Makefile.NetBSD index 51c8181..ecf8205 100644 --- a/Makefile.NetBSD +++ b/Makefile.NetBSD @@ -45,7 +45,7 @@ format.o: format.c xs.h xs_regex.h xs_mime.h xs_html.h xs_json.h \ xs_time.h xs_match.h snac.h http_codes.h html.o: html.c xs.h xs_io.h xs_json.h xs_regex.h xs_set.h xs_openssl.h \ xs_time.h xs_mime.h xs_match.h xs_html.h xs_curl.h xs_unicode.h xs_url.h \ - snac.h http_codes.h + xs_random.h snac.h http_codes.h http.o: http.c xs.h xs_io.h xs_openssl.h xs_curl.h xs_time.h xs_json.h \ snac.h http_codes.h httpd.o: httpd.c xs.h xs_io.h xs_json.h xs_socket.h xs_unix_socket.h \ @@ -60,7 +60,8 @@ sandbox.o: sandbox.c xs.h snac.h http_codes.h snac.o: snac.c xs.h xs_hex.h xs_io.h xs_unicode_tbl.h xs_unicode.h \ xs_json.h xs_curl.h xs_openssl.h xs_socket.h xs_unix_socket.h xs_url.h \ xs_httpd.h xs_mime.h xs_regex.h xs_set.h xs_time.h xs_glob.h xs_random.h \ - xs_match.h xs_fcgi.h xs_html.h xs_po.h snac.h http_codes.h + xs_match.h xs_fcgi.h xs_html.h xs_po.h xs_webmention.h snac.h \ + http_codes.h upgrade.o: upgrade.c xs.h xs_io.h xs_json.h xs_glob.h snac.h http_codes.h utils.o: utils.c xs.h xs_io.h xs_json.h xs_time.h xs_openssl.h \ xs_random.h xs_glob.h xs_curl.h xs_regex.h snac.h http_codes.h diff --git a/snac.c b/snac.c index 0a1c9cf..a35cd06 100644 --- a/snac.c +++ b/snac.c @@ -25,6 +25,7 @@ #include "xs_fcgi.h" #include "xs_html.h" #include "xs_po.h" +#include "xs_webmention.h" #include "snac.h" diff --git a/xs_curl.h b/xs_curl.h index ac543b1..2301661 100644 --- a/xs_curl.h +++ b/xs_curl.h @@ -225,7 +225,7 @@ int xs_smtp_request(const char *url, const char *user, const char *pass, if (use_ssl) curl_easy_setopt(curl, CURLOPT_USE_SSL, (long)CURLUSESSL_ALL); - + curl_easy_setopt(curl, CURLOPT_MAIL_FROM, from); rcpt = curl_slist_append(rcpt, to); @@ -236,7 +236,7 @@ int xs_smtp_request(const char *url, const char *user, const char *pass, curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L); res = curl_easy_perform(curl); - + curl_easy_cleanup(curl); curl_slist_free_all(rcpt); diff --git a/xs_version.h b/xs_version.h index f899dcb..c7789a7 100644 --- a/xs_version.h +++ b/xs_version.h @@ -1 +1 @@ -/* d467dc71e518603250a55c8a67e26cf40e1710e9 2025-02-14T10:21:15+01:00 */ +/* 871d420cef893b6efe32869407294baf084ce3ab 2025-05-04T11:01:01+02:00 */ diff --git a/xs_webmention.h b/xs_webmention.h new file mode 100644 index 0000000..4ef2308 --- /dev/null +++ b/xs_webmention.h @@ -0,0 +1,118 @@ +/* copyright (c) 2025 grunfink et al. / MIT license */ + +#ifndef _XS_WEBMENTION_H + +#define _XS_WEBMENTION_H + +int xs_webmention_send(const char *source, const char *target, const char *user_agent); + + +#ifdef XS_IMPLEMENTATION + +int xs_webmention_send(const char *source, const char *target, const char *user_agent) +/* sends a Webmention to target. + Returns: < 0, error; 0, no Webmention endpoint; > 0, Webmention sent */ +{ + int status = 0; + xs *endpoint = NULL; + + xs *ua = xs_fmt("%s (Webmention)", user_agent ? user_agent : "xs_webmention"); + xs *headers = xs_dict_new(); + headers = xs_dict_set(headers, "accept", "text/html"); + headers = xs_dict_set(headers, "user-agent", ua); + + xs *h_req = NULL; + int p_size = 0; + + /* try first a HEAD, to see if there is a Webmention Link header */ + h_req = xs_http_request("HEAD", target, headers, NULL, 0, &status, NULL, &p_size, 0); + + /* return immediate failures */ + if (status < 200 || status > 299) + return -1; + + const char *link = xs_dict_get(h_req, "link"); + + if (xs_is_string(link) && xs_regex_match(link, "rel *= *(\"|')?webmention")) { + /* endpoint is between < and > */ + xs *r = xs_regex_select_n(link, "<[^>]+>", 1); + + if (xs_list_len(r) == 1) { + endpoint = xs_dup(xs_list_get(r, 0)); + endpoint = xs_strip_chars_i(endpoint, "<>"); + } + } + + if (endpoint == NULL) { + /* no Link header; get the content */ + xs *g_req = NULL; + xs *payload = NULL; + + g_req = xs_http_request("GET", target, headers, NULL, 0, &status, &payload, &p_size, 0); + + if (status < 200 || status > 299) + return -1; + + const char *ctype = xs_dict_get(g_req, "content-type"); + + /* not HTML? no point in looking inside */ + if (!xs_is_string(ctype) || xs_str_in(ctype, "text/html") == -1) + return -2; + + if (!xs_is_string(payload)) + return -3; + + xs *links = xs_regex_select(payload, "<(a +|link +)[^>]+>"); + const char *link; + + xs_list_foreach(links, link) { + if (xs_regex_match(link, "rel *= *(\"|')?webmention")) { + /* found; extract the href */ + xs *r = xs_regex_select_n(link, "href *= *(\"|')?[^\"]+(\"|')", 1); + + if (xs_list_len(r) == 1) { + xs *l = xs_split_n(xs_list_get(r, 0), "=", 1); + + if (xs_list_len(l) == 2) { + endpoint = xs_dup(xs_list_get(l, 1)); + endpoint = xs_strip_chars_i(endpoint, " \"'"); + + break; + } + } + } + } + } + + /* is it a relative endpoint? */ + if (xs_is_string(endpoint)) { + if (!xs_startswith(endpoint, "https://") && !xs_startswith(endpoint, "http://")) { + xs *l = xs_split(target, "/"); + + if (xs_list_len(l) < 3) + endpoint = xs_free(endpoint); + else { + xs *s = xs_fmt("%s/" "/%s", xs_list_get(l, 0), xs_list_get(l, 2)); + endpoint = xs_str_wrap_i(s, endpoint, NULL); + } + } + } + + if (xs_is_string(endpoint)) { + /* got it! */ + headers = xs_dict_set(headers, "content-type", "application/x-www-form-urlencoded"); + + xs *body = xs_fmt("source=%s&target=%s", source, target); + + xs *rsp = xs_http_request("POST", endpoint, headers, body, strlen(body), &status, NULL, 0, 0); + } + else + status = 0; + + return status >= 200 && status <= 299; +} + + +#endif /* XS_IMPLEMENTATION */ + +#endif /* _XS_WEBMENTION_H */ -- cgit