commit 3cb1725

Andrew Alderwick  ·  2023-01-13 12:25:14 +0000 UTC
parent a99f742
Added OpenBSD's unveil() and pledge() support.
2 files changed,  +42, -16
M data.c
+32, -10
 1@@ -13,6 +13,8 @@
 2 
 3 #include "snac.h"
 4 
 5+#include <sys/wait.h>
 6+
 7 const char *public_address = "https:/" "/www.w3.org/ns/activitystreams#Public";
 8 
 9 int activitypub_request(snac *snac, char *url, d_char **data)
10@@ -999,6 +1001,35 @@ int process_message(snac *snac, char *msg, char *req)
11 }
12 
13 
14+int send_email(char *msg)
15+/* invoke sendmail with email headers and body in msg */
16+{
17+    FILE *f;
18+    int status;
19+    int fds[2];
20+    pid_t pid;
21+    if (pipe(fds) == -1) return -1;
22+    pid = vfork();
23+    if (pid == -1) return -1;
24+    else if (pid == 0) {
25+        dup2(fds[0], 0);
26+        close(fds[0]);
27+        close(fds[1]);
28+        execl("/usr/sbin/sendmail", "sendmail", "-t", (char *) NULL);
29+        _exit(1);
30+    }
31+    close(fds[0]);
32+    if ((f = fdopen(fds[1], "w")) == NULL) {
33+        close(fds[1]);
34+        return -1;
35+    }
36+    fprintf(f, "%s\n", msg);
37+    fclose(f);
38+    if (waitpid(pid, &status, 0) == -1) return -1;
39+    return status;
40+}
41+
42+
43 void process_queue(snac *snac)
44 /* processes the queue */
45 {
46@@ -1085,17 +1116,8 @@ void process_queue(snac *snac)
47             /* send this email */
48             char *msg   = xs_dict_get(q_item, "message");
49             int retries = xs_number_get(xs_dict_get(q_item, "retries"));
50-            FILE *f;
51-            int ok = 0;
52-
53-            if ((f = popen("/usr/sbin/sendmail -t", "w")) != NULL) {
54-                fprintf(f, "%s\n", msg);
55-
56-                if (pclose(f) != -1)
57-                    ok = 1;
58-            }
59 
60-            if (ok)
61+            if (!send_email(msg))
62                 snac_debug(snac, 1, xs_fmt("email message sent"));
63             else {
64                 if (retries > queue_retry_max)
M data.c
+10, -6
 1@@ -86,15 +86,19 @@ int srv_open(char *basedir, int auto_upgrade)
 2     if (error != NULL)
 3         srv_log(error);
 4 
 5-/* disabled temporarily; messages can't be sent (libcurl issue?) */
 6-#if 0
 7 #ifdef __OpenBSD__
 8     srv_debug(2, xs_fmt("Calling unveil()"));
 9-    unveil(basedir,     "rwc");
10-    unveil("/usr/sbin", "x");
11-    unveil(NULL,        NULL);
12+    unveil(basedir,                "rwc");
13+    unveil("/usr/sbin/sendmail",   "x");
14+    unveil("/etc/resolv.conf",     "r");
15+    unveil("/etc/hosts",           "r");
16+    unveil("/etc/ssl/openssl.cnf", "r");
17+    unveil("/etc/ssl/cert.pem",    "r");
18+    unveil("/usr/share/zoneinfo",  "r");
19+    unveil(NULL,                   NULL);
20+    srv_debug(2, xs_fmt("Calling pledge()"));
21+    pledge("stdio rpath wpath cpath flock inet proc exec dns", NULL);
22 #endif /* __OpenBSD__ */
23-#endif
24 
25     return ret;
26 }