Download raw body.
Adding Message-ID to mail(1) portable version ;-)
On Wed, Aug 21, 2024 at 11:04:57AM +0200, Sven M. Hallberg wrote:
> Claus Assmann on Wed, Aug 21 2024:
> > So if two people on the same system send a message within the
> > same seccond they get the same Message-ID?
>
> Yeah. Just pick 16 bytes (128 bits) of randomness, print them in hex,
> and be done with it. Anything else is needlessly fanciful.
>
> -p
>
>
Sven and Claus, both are right. Thank for your corrections. Anything
else you notice don't hesitate in letting me know.
So, here we go with the random numbers versions:
#########################################
## NON-PORTABLE POSIX COPLIANT 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 random[32];
+ static char hostname[HOST_NAME_MAX+1];
+ static char buf[sizeof(random) + sizeof(hostname) + 4];
+ int error = 0;
+
+ while (n < sizeof(random) - 1)
+ snprintf(&random[n++], sizeof(&random[n]), "%u",
+ arc4random_uniform(9));
+
+ if (gethostname(hostname, sizeof(hostname)))
+ errx(1, "genmsgid: gethostname");
+
+ error = snprintf(buf, sizeof(buf), "<%s@%s>", random, hostname);
+ if (error < 0 || error >= sizeof(buf))
+ errx(1, "genmsgid: snprintf");
+
+ return(buf);
+}
######################
## PORTABLE VERSION ##
######################
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:18:44 -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:18:44 -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:18:44 -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:18:44 -0000
@@ -641,3 +641,41 @@ clearnew(void)
}
}
}
+
+/* Generate Message-ID */
+char*
+genmsgid(void)
+{
+ size_t n = 0;
+ static char random[32];
+ struct addrinfo hints, *info, *p;
+ char *fqdn = NULL;
+ static char hostname[NI_MAXHOST];
+ static char buf[sizeof(random) + sizeof(hostname) + 4];
+ int error = 0;
+
+ while (n < sizeof(random) - 1)
+ snprintf(&random[n++], sizeof(&random[n]), "%u",
+ arc4random_uniform(9));
+
+ 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>", random, fqdn);
+ if (error < 0 || error >= sizeof(buf))
+ errx(1, "genmsgid: snprintf");
+
+ freeaddrinfo(info);
+ return(buf);
+}
--
Walter
Adding Message-ID to mail(1) portable version ;-)