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

From: Timmy Douglas <[email-masked]>
Date: Fri, 01 Dec 2023 21:15:12 -0800

Tobias Geerinckx-Rice <[email-masked]> writes:

> [snip]
> I'm running [email-masked].  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@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@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@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);
}