Index | Thread | Search

From:
Walter Alejandro Iglesias <wai@roquesor.com>
Subject:
Re: Adding Message-ID to mail(1) (CORRECTION)
To:
tech@openbsd.org
Date:
Thu, 22 Aug 2024 08:10:44 +0200

Download raw body.

Thread
I was doing something wrong with the size check in snprintf().  I also
shortened the UID string and converted it to hexadecimal.


	########################
	# NON PORTABLE VERSION #
	########################

Index: extern.h
===================================================================
RCS file: /cvs/src/usr.bin/mail/extern.h,v
diff -u -p -r1.30 extern.h
--- extern.h	21 May 2024 05:00:48 -0000	1.30
+++ extern.h	21 Aug 2024 17:16:25 -0000
@@ -129,6 +129,7 @@ int	 forward(char *, FILE *, char *, int
 void	 free_child(pid_t);
 int	 from(void *);
 off_t	 fsize(FILE *);
+char*	 genmsgid(void);
 int	 getfold(char *, int);
 int	 gethfield(FILE *, char *, int, char **);
 int	 gethfromtty(struct header *, int);
Index: send.c
===================================================================
RCS file: /cvs/src/usr.bin/mail/send.c,v
diff -u -p -r1.26 send.c
--- send.c	8 Mar 2023 04:43:11 -0000	1.26
+++ send.c	21 Aug 2024 17:16:25 -0000
@@ -525,6 +525,8 @@ puthead(struct header *hp, FILE *fo, int
 		fmt("To:", hp->h_to, fo, w&GCOMMA), gotcha++;
 	if (hp->h_subject != NULL && w & GSUBJECT)
 		fprintf(fo, "Subject: %s\n", hp->h_subject), gotcha++;
+	if (fo != stdout)
+		fprintf(fo, "Message-ID: %s\n", genmsgid()), gotcha++;
 	if (hp->h_cc != NULL && w & GCC)
 		fmt("Cc:", hp->h_cc, fo, w&GCOMMA), gotcha++;
 	if (hp->h_bcc != NULL && w & GBCC)
Index: util.c
===================================================================
RCS file: /cvs/src/usr.bin/mail/util.c,v
diff -u -p -r1.2 util.c
--- util.c	26 Dec 2022 19:16:01 -0000	1.2
+++ util.c	21 Aug 2024 17:16:25 -0000
@@ -641,3 +641,27 @@ clearnew(void)
 		}
 	}
 }
+
+/* Generate Message-ID */
+char*
+genmsgid(void)
+{
+	size_t n = 0;
+	static char r[24];
+	static char hostname[HOST_NAME_MAX+1];
+	static char buf[sizeof(r) + sizeof(hostname) + 4];
+	int error = 0;
+
+	while (n < sizeof(r) - 1)
+		if (snprintf(&r[n++], 2, "%x", arc4random_uniform(16)) != 1)
+			errx(1, "genmsgid: snprintf");
+
+	if (gethostname(hostname, sizeof(hostname)))
+		errx(1, "genmsgid: gethostname");
+
+	error = snprintf(buf, sizeof(buf), "<%s@%s>", r, hostname);
+	if (error < 0 || error >= sizeof(buf))
+		errx(1, "genmsgid: snprintf");
+
+	return(buf);
+}




	##################################################
	# PORTABLE VERSION (AKA fit to survive on Linux) #
	##################################################

Index: def.h
===================================================================
RCS file: /cvs/src/usr.bin/mail/def.h,v
diff -u -p -r1.17 def.h
--- def.h	28 Jan 2022 06:18:41 -0000	1.17
+++ def.h	21 Aug 2024 17:36:03 -0000
@@ -55,6 +55,9 @@
 #include <limits.h>
 #include <vis.h>
 #include "pathnames.h"
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netdb.h>
 
 #define	APPEND				/* New mail goes to end of mailbox */
 
Index: extern.h
===================================================================
RCS file: /cvs/src/usr.bin/mail/extern.h,v
diff -u -p -r1.30 extern.h
--- extern.h	21 May 2024 05:00:48 -0000	1.30
+++ extern.h	21 Aug 2024 17:36:04 -0000
@@ -129,6 +129,7 @@ int	 forward(char *, FILE *, char *, int
 void	 free_child(pid_t);
 int	 from(void *);
 off_t	 fsize(FILE *);
+char*	 genmsgid(void);
 int	 getfold(char *, int);
 int	 gethfield(FILE *, char *, int, char **);
 int	 gethfromtty(struct header *, int);
Index: send.c
===================================================================
RCS file: /cvs/src/usr.bin/mail/send.c,v
diff -u -p -r1.26 send.c
--- send.c	8 Mar 2023 04:43:11 -0000	1.26
+++ send.c	21 Aug 2024 17:36:04 -0000
@@ -525,6 +525,8 @@ puthead(struct header *hp, FILE *fo, int
 		fmt("To:", hp->h_to, fo, w&GCOMMA), gotcha++;
 	if (hp->h_subject != NULL && w & GSUBJECT)
 		fprintf(fo, "Subject: %s\n", hp->h_subject), gotcha++;
+	if (fo != stdout)
+		fprintf(fo, "Message-ID: %s\n", genmsgid()), gotcha++;
 	if (hp->h_cc != NULL && w & GCC)
 		fmt("Cc:", hp->h_cc, fo, w&GCOMMA), gotcha++;
 	if (hp->h_bcc != NULL && w & GBCC)
Index: util.c
===================================================================
RCS file: /cvs/src/usr.bin/mail/util.c,v
diff -u -p -r1.2 util.c
--- util.c	26 Dec 2022 19:16:01 -0000	1.2
+++ util.c	21 Aug 2024 17:36:04 -0000
@@ -641,3 +641,41 @@ clearnew(void)
 		}
 	}
 }
+
+/* Generate Message-ID */
+char*
+genmsgid(void)
+{
+	size_t n = 0;
+	static char r[24];
+	struct addrinfo hints, *info, *p;
+	char *fqdn = NULL;
+	static char hostname[NI_MAXHOST];
+	static char buf[sizeof(r) + sizeof(hostname) + 4];
+	int error = 0;
+
+	while (n < sizeof(r) - 1)
+		if (snprintf(&r[n++], 2, "%x", arc4random_uniform(16)) != 1)
+			errx(1, "genmsgid: snprintf");
+
+	if (gethostname(hostname, sizeof(hostname)))
+		errx(1, "genmsgid: gethostname");
+
+	memset(&hints, 0, sizeof hints);
+	hints.ai_family = AF_UNSPEC;
+	hints.ai_socktype = SOCK_STREAM;
+	hints.ai_flags = AI_CANONNAME;
+
+	if (getaddrinfo(hostname, NULL, &hints, &info) != 0)
+		errx(1, "genmsgid: getaddrinfo");
+
+	for (p = info; p != NULL; p = p->ai_next)
+		fqdn = p->ai_canonname;
+
+	error = snprintf(buf, sizeof(buf), "<%s@%s>", r, fqdn);
+	if (error < 0 || error >= sizeof(buf))
+		errx(1, "genmsgid: snprintf");
+
+	freeaddrinfo(info);
+	return(buf);
+}


-- 
Walter