Error sending email with OpenSMTPD, fatal: time_to_text: bsnprintf

  • Open
  • quality assurance status badge
Details
2 participants
  • Timmy Douglas
  • Nathan
Owner
unassigned
Submitted by
Timmy Douglas
Severity
normal
T
T
Timmy Douglas wrote on 26 Jan 2022 06:51
(address . bug-guix@gnu.org)
87mtjjkpsw.fsf@timmydouglas.com
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:

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:



Toggle diff (51 lines)
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*/
T
T
Timmy Douglas wrote on 21 Nov 2023 08:09
(address . bug-guix@gnu.org)(name . Tobias Geerinckx-Rice)(address . me@tobias.gr)
87jzqba8ha.fsf@timmydouglas.com
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@timmydouglas.com> writes:

Toggle quote (94 lines)
> 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*/
N
N
Nathan wrote on 22 Nov 2023 18:57
bug#53547: Error sending email with OpenSMTPD, fatal: time_to_text: bsnprintf
87r0khis2j.fsf@nborghese.com
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.
Attachment: a.c
T
T
Timmy Douglas wrote on 23 Nov 2023 01:03
87h6ld9vz7.fsf@timmydouglas.com
Nathan <nathan_mail@nborghese.com> writes:

Toggle quote (10 lines)
> 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))
T
T
Timmy Douglas wrote on 2 Dec 2023 06:15
8734wlqj6n.fsf@timmydouglas.com
Tobias Geerinckx-Rice <me@tobias.gr> writes:

Toggle quote (8 lines)
> [snip]
> I'm running opensmtpd@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@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);
}
?