GNU bug report logs - #53547
Error sending email with OpenSMTPD, fatal: time_to_text: bsnprintf

Previous Next

Package: guix;

Reported by: Timmy Douglas <mail <at> timmydouglas.com>

Date: Wed, 26 Jan 2022 05:52:01 UTC

Severity: normal

To reply to this bug, email your comments to 53547 AT debbugs.gnu.org.

Toggle the display of automated, internal messages from the tracker.

View this report as an mbox folder, status mbox, maintainer mbox


Report forwarded to bug-guix <at> gnu.org:
bug#53547; Package guix. (Wed, 26 Jan 2022 05:52:01 GMT) Full text and rfc822 format available.

Acknowledgement sent to Timmy Douglas <mail <at> timmydouglas.com>:
New bug report received and forwarded. Copy sent to bug-guix <at> gnu.org. (Wed, 26 Jan 2022 05:52:01 GMT) Full text and rfc822 format available.

Message #5 received at submit <at> debbugs.gnu.org (full text, mbox):

From: Timmy Douglas <mail <at> timmydouglas.com>
To: bug-guix <at> gnu.org
Subject: Error sending email with OpenSMTPD, fatal: time_to_text: bsnprintf
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*/




Information forwarded to bug-guix <at> gnu.org:
bug#53547; Package guix. (Tue, 21 Nov 2023 07:10:02 GMT) Full text and rfc822 format available.

Message #8 received at submit <at> debbugs.gnu.org (full text, mbox):

From: Timmy Douglas <mail <at> timmydouglas.com>
To: bug-guix <at> gnu.org
Cc: Tobias Geerinckx-Rice <me <at> tobias.gr>
Subject: Re: Error sending email with OpenSMTPD, fatal: time_to_text: bsnprintf
Date: Mon, 20 Nov 2023 23:09:05 -0800
Tobias, I noticed you recently updated opensmtpd to 7.4.0 with commit
5e7f27d6ca7be84453e6d4de3d860b700ba3aef7.

I was curious if you or anyone else has seen this issue, and if I should
try submitting a patch to guix for it.


Timmy Douglas <mail <at> timmydouglas.com> writes:

> 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*/




Information forwarded to bug-guix <at> gnu.org:
bug#53547; Package guix. (Wed, 22 Nov 2023 18:04:02 GMT) Full text and rfc822 format available.

Message #11 received at submit <at> debbugs.gnu.org (full text, mbox):

From: Nathan <nathan_mail <at> nborghese.com>
To: mail <at> timmydouglas.com, bug-guix <at> gnu.org
Subject: bug#53547: Error sending email with OpenSMTPD, fatal: time_to_text:
 bsnprintf
Date: Wed, 22 Nov 2023 12:57:09 -0500
[Message part 1 (text/plain, inline)]
I don't have this problem on my server.

I looked at your github issue page.
Those macros are defined by the `configure' script.
I can see that guix's `configure' output defines HAVE_STRUCT_TM_TM_GMTOFF in config.h.
(if neither of the #if match, then the `tz' pointer is likely garbage and the program will crash.)

A problem with your time zone configuration seems like a reasonable guess.
I'd check it with a simple C program like the one attached.

[a.c (application/octet-stream, attachment)]

Information forwarded to bug-guix <at> gnu.org:
bug#53547; Package guix. (Thu, 23 Nov 2023 00:05:01 GMT) Full text and rfc822 format available.

Message #14 received at submit <at> debbugs.gnu.org (full text, mbox):

From: Timmy Douglas <mail <at> timmydouglas.com>
To: Nathan <nathan_mail <at> nborghese.com>, bug-guix <at> gnu.org
Subject: Re: bug#53547: Error sending email with OpenSMTPD, fatal:
 time_to_text: bsnprintf
Date: Wed, 22 Nov 2023 16:03:40 -0800
Nathan <nathan_mail <at> nborghese.com> writes:

> I don't have this problem on my server.
>
> I looked at your github issue page.
> Those macros are defined by the `configure' script.
> I can see that guix's `configure' output defines HAVE_STRUCT_TM_TM_GMTOFF in config.h.
> (if neither of the #if match, then the `tz' pointer is likely garbage and the program will crash.)
>
> A problem with your time zone configuration seems like a reasonable guess.
> I'd check it with a simple C program like the one attached.

your app prints my timezone as expected. if you run something like this
does it work for you?

cat << EOF | /run/setuid-programs/sendmail $(whoami)
To: $(whoami)
Subject: test


blah
EOF

for me, it prints:

sendmail: time_to_text: bsnprintf


my config.scm:


(operating-system
  ...
  (setuid-programs
   (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))




Information forwarded to bug-guix <at> gnu.org:
bug#53547; Package guix. (Sat, 02 Dec 2023 05:16:01 GMT) Full text and rfc822 format available.

Message #17 received at submit <at> debbugs.gnu.org (full text, mbox):

From: Timmy Douglas <mail <at> timmydouglas.com>
To: Tobias Geerinckx-Rice <me <at> tobias.gr>, bug-guix <at> gnu.org
Subject: Re: bug#53547: Error sending email with OpenSMTPD, fatal:
 time_to_text: bsnprintf
Date: Fri, 01 Dec 2023 21:15:12 -0800
Tobias Geerinckx-Rice <me <at> tobias.gr> writes:

> [snip]
> I'm running opensmtpd <at> 7.4.0p1.  Have you tried that yet?
>
> Guix would certainly accept a patch to fix a bug, but I'm also 
> paranoid and would prefer to first see some response from upstream 
> that this is the correct diagnosis.


So I found a workaround on my computer with that script that didn't seem
to repro. If I set TZ=UTC instead of what I had
(TZ=America/Los_Angeles), I was able to make that script work. I should
be able to propagate that to mcron and work around the issue...


What I tried before that:

When I straced sendmail earlier I saw this:

[pid 29134] newfstatat(5, "", {st_mode=S_IFSOCK|0777, st_size=0, ...}, AT_EMPTY_PATH) = 0
[pid 29134] read(5, "220 localhost ESMTP OpenSMTPD\r\n", 4096) = 31
[pid 29134] write(5, "EHLO localhost\r\n", 16) = 16
[pid 29134] read(5, "250-localhost Hello localhost [l"..., 4096) = 128
[pid 29134] write(5, "MAIL FROM:<timmy <at> localhost>  \r\n", 31) = 31
[pid 29134] read(5, "250 2.0.0 Ok\r\n", 4096) = 14
[pid 29134] write(5, "RCPT TO:<root <at> localhost> \r\n", 27) = 27
[pid 29134] read(5, "250 2.1.5 Destination address va"..., 4096) = 51
[pid 29134] write(5, "DATA\r\n", 6)     = 6
[pid 29134] read(5, "354 Enter mail, end with \".\" on "..., 4096) = 50
[pid 29134] write(2, "sendmail: time_to_text: bsnprint"..., 34sendmail: time_to_text: bsnprintf
) = 34
[pid 29134] write(5, "From: timmy <timmy <at> localhost>\r\n", 31) = 31
[pid 29134] exit_group(1)               = ?

It looks like the code of the sendmail client will try to add a Date
header if one wasn't passed to the client. (smtp_session.c:2647)

I was able to send an email directly with telnet, so that verified the
code wasn't on the server. I tried throwing together a standalone repro
(below), but didn't have any luck reproducing it. With opensmtpd's
sendmail being setgid, I didn't really feel like debugging further.

// compile with
// gcc ./a.c
// run with
// ./a.out
#include <stdio.h>
#include <time.h>
#include <stdarg.h>
#include <stdlib.h>

int
bsnprintf(char *str, size_t size, const char *format, ...)
{
	int ret;
	va_list ap;

	va_start(ap, format);
	ret = vsnprintf(str, size, format, ap);
	va_end(ap);
	if (ret < 0 || (size_t)ret >= size)
		return 0;

	return 1;
}


const char *
time_to_text(time_t when)
{
	struct tm *lt;
	static char buf[40];
	const char *day[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
	const char *month[] = {"Jan","Feb","Mar","Apr","May","Jun",
			 "Jul","Aug","Sep","Oct","Nov","Dec"};
	const char *tz;
	long offset;

	lt = localtime(&when);
	if (lt == NULL || when == 0)
		perror("time_to_text: localtime");

#if 1
	offset = lt->tm_gmtoff;
	tz = lt->tm_zone;
#elif 0
	offset = lt->tm_isdst > 0 ? altzone : timezone;
	tz = lt->tm_isdst > 0 ? tzname[1] : tzname[0];
#endif

	/* We do not use strftime because it is subject to locale substitution*/
	if (!bsnprintf(buf, sizeof(buf),
	    "%s, %d %s %d %02d:%02d:%02d %c%02d%02d (%s)",
	    day[lt->tm_wday], lt->tm_mday, month[lt->tm_mon],
	    lt->tm_year + 1900,
	    lt->tm_hour, lt->tm_min, lt->tm_sec,
	    offset >= 0 ? '+' : '-',
	    abs((int)offset / 3600),
	    abs((int)offset % 3600) / 60,
	    tz))
		perror("time_to_text: bsnprintf");
	else
	  {
	    printf("'%s'", buf);
	  }
	return buf;
}

int main(int argc, char** argv){
	time_t now=time(NULL);
	time_to_text(now);
	struct tm *lt=localtime(&now);
}




This bug report was last modified 140 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.