]> git.ipfire.org Git - people/ms/dma.git/commitdiff
properly randomize the message id
authorSimon Schubert <2@0x2c.org>
Mon, 28 Jun 2010 22:49:42 +0000 (00:49 +0200)
committerSimon Schubert <2@0x2c.org>
Mon, 28 Jun 2010 22:50:50 +0000 (00:50 +0200)
We would only use the queue id of the message as its message id, which
is generated from the inode number of the message's queue file.  Inode
numbers are readily reused on many file systems, which leads to
repeating message ids.

Create real randomized message ids instead.

Submitted-by: Peter Pentchev <roam@ringlet.net> (earlier version)
dma.c
dma.h
mail.c
util.c

diff --git a/dma.c b/dma.c
index 1b823837a268f99ddf558abc1d2689bc68389c85..61cae6839c944b54cb09cf7f691116d16544f9d1 100644 (file)
--- a/dma.c
+++ b/dma.c
@@ -365,6 +365,7 @@ main(int argc, char **argv)
        int recp_from_header = 0;
 
        atexit(deltmp);
+       init_random();
 
        bzero(&queue, sizeof(queue));
        LIST_INIT(&queue.queue);
diff --git a/dma.h b/dma.h
index a2ee868fa6cfee8a1a1ccba58c61a7b75ec684b4..cd9214ef5a6cb2cdc4cf3ee97b167b879b53d8d0 100644 (file)
--- a/dma.h
+++ b/dma.h
@@ -201,5 +201,6 @@ void deltmp(void);
 int open_locked(const char *, int, ...);
 char *rfc822date(void);
 int strprefixcmp(const char *, const char *);
+void init_random(void);
 
 #endif
diff --git a/mail.c b/mail.c
index afb8c8fc80a1832c3e511a2f4c907bd97ed12c3e..db2bca43eef7798b5dc02fce3473ae31548ad033 100644 (file)
--- a/mail.c
+++ b/mail.c
@@ -33,6 +33,8 @@
  */
 
 #include <errno.h>
+#include <inttypes.h>
+#include <signal.h>
 #include <syslog.h>
 #include <unistd.h>
 
@@ -406,10 +408,13 @@ readmail(struct queue *queue, int nodot, int recp_from_header)
                                        had_date = 1;
                                        snprintf(line, sizeof(line), "Date: %s\n", rfc822date());
                                } else if (!had_messagid) {
-                                       /* XXX better msgid, assign earlier and log? */
+                                       /* XXX msgid, assign earlier and log? */
                                        had_messagid = 1;
-                                       snprintf(line, sizeof(line), "Message-Id: <%s@%s>\n",
-                                                queue->id, hostname());
+                                       snprintf(line, sizeof(line), "Message-Id: <%"PRIxMAX".%s.%"PRIxMAX"@%s>\n",
+                                                (uintmax_t)time(NULL),
+                                                queue->id,
+                                                random(),
+                                                hostname());
                                } else if (!had_from) {
                                        had_from = 1;
                                        snprintf(line, sizeof(line), "From: <%s>\n", queue->sender);
diff --git a/util.c b/util.c
index e6d43e9928fea86b3db12ef67cc1ab9e4b839cb2..a43080858c775cde3b7287c4894ac5aaf6cc68ec 100644 (file)
--- a/util.c
+++ b/util.c
@@ -262,3 +262,21 @@ strprefixcmp(const char *str, const char *prefix)
        return (strncasecmp(str, prefix, strlen(prefix)));
 }
 
+void
+init_random(void)
+{
+       unsigned int seed;
+       int rf;
+
+       rf = open("/dev/urandom", O_RDONLY);
+       if (rf == -1)
+               rf = open("/dev/random", O_RDONLY);
+
+       if (!(rf != -1 && read(rf, &seed, sizeof(seed)) == sizeof(seed)))
+               seed = (time(NULL) ^ getpid()) + (uintptr_t)&seed;
+
+       srandom(seed);
+
+       if (rf != -1)
+               close(rf);
+}