Subject: bug#53547: Error sending email with OpenSMTPD, fatal: time_to_text: bsnprintf

From: Timmy Douglas <[email-masked]>
Date: Tue, 25 Jan 2022 21:51:27 -0800

I recently installed opensmtpd and have been trying to send email. It
appears there is an error with the bsnprintf function:

$ ./sendmail -t
To: timmy
From: me 
Subject: hi

aoeueou
(press ^D)
sendmail: time_to_text: bsnprintf

I reported this issue here:
https://github.com/OpenSMTPD/OpenSMTPD/issues/1166

mcron appears to invoke sendmail with `-t`, which is how I discovered
the issue.

If anyone is trying to figure out how to setup the setuid on Guix
system, here is my config.scm file (I couldn't find anything online so
maybe this will help):

(operating-system
 ; ...
   (append (list (setuid-program
                  (program (file-append opensmtpd "/sbin/smtpctl"))
		  (setuid? #f)
		  (setgid? #t)
		  (user "root")
		  (group "smtpq"))
		 (setuid-program
                  (program (file-append opensmtpd "/sbin/sendmail"))
		  (setuid? #f)
		  (setgid? #t)
		  (user "root")
		  (group "smtpq")))
           %setuid-programs))

I tried building the latest opensmtpd from git, which required a few
other changes to patch the bug:

diff --git a/usr.sbin/smtpd/smtpc.c b/usr.sbin/smtpd/smtpc.c
index 49750dca..4ed506dc 100644
--- a/usr.sbin/smtpd/smtpc.c
+++ b/usr.sbin/smtpd/smtpc.c
@@ -114,8 +114,8 @@ parse_tls_options(char *opt)
                        servname = value;
                        break;
                case -1:
-                       if (suboptarg)
-                               fatalx("invalid TLS option \"%s\"", suboptarg);
+                 //if (suboptarg)
+                 //            fatalx("invalid TLS option \"%s\"", suboptarg);
                        fatalx("missing TLS option");
                }
        }
diff --git a/usr.sbin/smtpd/smtpctl.c b/usr.sbin/smtpd/smtpctl.c
index 00c49cb7..3630ef1d 100644
--- a/usr.sbin/smtpd/smtpctl.c
+++ b/usr.sbin/smtpd/smtpctl.c
@@ -52,6 +52,8 @@
 #endif
 #include <limits.h>
 
+#include <grp.h>
+
 #include "smtpd.h"
 #include "parser.h"
 #include "log.h"
diff --git a/usr.sbin/smtpd/to.c b/usr.sbin/smtpd/to.c
index 3ea04d89..9928d09b 100644
--- a/usr.sbin/smtpd/to.c
+++ b/usr.sbin/smtpd/to.c
@@ -176,7 +176,7 @@ const char *
 time_to_text(time_t when)
 {
        struct tm *lt;
-       static char buf[40];
+       static char buf[50];
        char *day[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
        char *month[] = {"Jan","Feb","Mar","Apr","May","Jun",
                         "Jul","Aug","Sep","Oct","Nov","Dec"};
@@ -193,6 +193,9 @@ time_to_text(time_t when)
 #elif defined HAVE_DECL_ALTZONE && defined HAVE_DECL_TIMEZONE
        offset = lt->tm_isdst > 0 ? altzone : timezone;
        tz = lt->tm_isdst > 0 ? tzname[1] : tzname[0];
+#else
+       offset = 0;
+       tz = "GMT";
 #endif
 
        /* We do not use strftime because it is subject to locale substitution*/