Bläddra i källkod

enforce tls when supported && add tests

shtrophic 1 år sedan
förälder
incheckning
3d96c57628
5 ändrade filer med 51 tillägg och 10 borttagningar
  1. 2 1
      .gitignore
  2. 2 1
      activitypub.c
  3. 24 0
      tests/smtp.c
  4. 7 2
      utils.c
  5. 16 6
      xs_curl.h

+ 2 - 1
.gitignore

@@ -1,2 +1,3 @@
-*.o
+**/*.o
+tests/smtp
 snac

+ 2 - 1
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);
 }
 
 

+ 24 - 0
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);
+}

+ 7 - 2
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;
 }

+ 16 - 6
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;
 }