diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0852bc8 --- /dev/null +++ b/.gitignore @@ -0,0 +1,325 @@ +ID +tags +gmon.out +*~ +.*.swp + +alloc.lo +alloc_re.lo +altpath.lo +author.lo +auto-ccld.sh +auto-str +auto-str.o +auto_bin.c +auto_bin.lo +auto_cron.c +auto_cron.lo +auto_etc.c +auto_etc.lo +auto_lib.c +auto_lib.lo +auto_qmail.c +auto_qmail.lo +auto_version.c +auto_version.lo +byte_chr.lo +byte_copy.lo +byte_cr.lo +byte_diff.lo +byte_rchr.lo +byte_zero.lo +case_diffb.lo +case_diffs.lo +case_lowerb.lo +case_startb.lo +case_starts.lo +choose +compile +concatHDR.lo +config.lo +constmap.lo +cookie.lo +copy.lo +crypt.lib +date2yyyymm.lo +date822fmt.lo +dateline.lo +datetime.lo +decodeB.lo +decodeHDR.lo +decodeQ.lo +die_badaddr.lo +die_badformat.lo +die_dow.lo +die_nomem.lo +die_sender.lo +die_usage.lo +direntry.h +dl.lib +dmarc.lo +dns_dfd.lo +dns_domain.lo +dns_dtda.lo +dns_ip.lo +dns_ipq.lo +dns_mx.lo +dns_name.lo +dns_nd.lo +dns_packet.lo +dns_random.lo +dns_rcip.lo +dns_rcrw.lo +dns_resolve.lo +dns_sortip.lo +dns_transmit.lo +dns_txt.lo +encodeB.lo +encodeQ.lo +env.lo +envread.lo +error.a +error.lo +error_str.lo +ezmlm-accept +ezmlm-archive +ezmlm-archive.o +ezmlm-cgi +ezmlm-cgi.o +ezmlm-check +ezmlm-checksub +ezmlm-checksub.o +ezmlm-clean +ezmlm-clean.o +ezmlm-confirm +ezmlm-confirm.o +ezmlm-cron +ezmlm-cron.o +ezmlm-dispatch +ezmlm-dispatch.o +ezmlm-gate +ezmlm-gate.o +ezmlm-get +ezmlm-get.o +ezmlm-glconf +ezmlm-idx +ezmlm-idx.o +ezmlm-import +ezmlm-import.o +ezmlm-issubn +ezmlm-issubn.o +ezmlm-limit +ezmlm-limit.o +ezmlm-list +ezmlm-list.o +ezmlm-make +ezmlm-make.o +ezmlm-manage +ezmlm-manage.o +ezmlm-moderate +ezmlm-moderate.o +ezmlm-reject +ezmlm-reject.o +ezmlm-request +ezmlm-request.o +ezmlm-return +ezmlm-return.o +ezmlm-rmtab +ezmlm-rmtab.o +ezmlm-send +ezmlm-send.o +ezmlm-split +ezmlm-split.o +ezmlm-store +ezmlm-store.o +ezmlm-sub +ezmlm-sub.o +ezmlm-subunsub.o +ezmlm-test +ezmlm-tstdig +ezmlm-tstdig.o +ezmlm-unsub +ezmlm-unsub.o +ezmlm-warn +ezmlm-warn.o +ezmlm-weed +ezmlm-weed.o +ezmlmrc +fd_copy.lo +fd_move.lo +fill-template +find-systype +fmt_str.lo +fmt_uint.lo +fmt_uint0.lo +fmt_ulong.lo +getconf.lo +getconf_ulong.lo +getconfopt.lo +getconfopt_copy.lo +getconfopt_cstr.lo +getconfopt_cstr_flag.lo +getconfopt_flag.lo +getconfopt_str.lo +getconfopt_ulong.lo +getconfopt_ulong_flag.lo +getconfopt_version.lo +gethdrln.lo +getln.lo +getln2.lo +hasattribute.h +hasflock.h +hassgact.h +haswaitp.h +hdr_add.lo +hdr_boundary.lo +hdr_ctboundary.lo +hdr_datemsgid.lo +hdr_from.lo +hdr_listsubject.lo +hdr_mime.lo +hdr_subject.lo +hdr_transferenc.lo +idxthread.lo +iopause.h +iopause.lo +ip4_scan.lo +lang/ch_GB/ezmlmrc +lang/cs/ezmlmrc +lang/cs/text/messages +lang/da/ezmlmrc +lang/da/text/messages +lang/de/ezmlmrc +lang/de/text/messages +lang/en_US/ezmlmrc +lang/en_US/text/messages +lang/es/ezmlmrc +lang/fr/ezmlmrc +lang/fr/text/messages +lang/hu/ezmlmrc +lang/hu/text/messages +lang/id/ezmlmrc +lang/id/text/messages +lang/it/ezmlmrc +lang/it/text/messages +lang/ja/ezmlmrc +lang/nl/ezmlmrc +lang/nl/text/messages +lang/pl/ezmlmrc +lang/pl/text/messages +lang/pt/ezmlmrc +lang/pt/text/messages +lang/pt_BR/ezmlmrc +lang/pt_BR/text/messages +lang/ru/ezmlmrc +lang/sv/ezmlmrc +lang/sv/text/messages +libcompile +libezmlm.a +load +lock_ex.lo +lockfile.lo +log.lo +make-compile +make-libcompile +make-load +make-makelib +make-makeso +make-messages +make-messages-c +makehash.lo +makelang +makelib +makepath.lo +makeso +messages-txt.c +messages.lo +ndelay_on.lo +now.lo +open_append.lo +open_read.lo +open_trunc.lo +openreadclose.lo +qmail.lo +qmail_copy.lo +quote.lo +readclose.lo +scan_8long.lo +scan_ulong.lo +seek_set.lo +select.h +sender.lo +sgetopt.lo +sig_catch.lo +sig_pipe.lo +slurp.lo +slurpclose.lo +socket_bind.lo +socket_conn.lo +socket_tcp.lo +socket_udp.lo +str.a +str_chr.lo +str_cpy.lo +str_diff.lo +str_diffn.lo +str_len.lo +str_rchr.lo +str_start.lo +stralloc_arts.lo +stralloc_cat.lo +stralloc_catb.lo +stralloc_cats.lo +stralloc_copy.lo +stralloc_eady.lo +stralloc_num.lo +stralloc_opyb.lo +stralloc_opys.lo +stralloc_pend.lo +strerr.lo +sub-sqlite3.lo +sub-sqlite3.so +sub-std.lo +sub-std.so +sub_sql.lo +subdb.lo +subfderr.lo +subfdin.lo +subfdout.lo +subgetopt.lo +subhash.lo +substdi.lo +substdio.a +substdio.lo +substdio_copy.lo +substdo.lo +surf.lo +surfpcs.lo +systype +tai_pack.lo +taia_add.lo +taia_approx.lo +taia_frac.lo +taia_less.lo +taia_now.lo +taia_pack.lo +taia_sub.lo +taia_uint.lo +test-getconfopt +test-getconfopt.o +uint16_pack.lo +uint16_unpack.lo +uint32.h +uint32_unpack.lo +uint64.h +unfoldHDR.lo +wait_pid.lo +wrap_chdir.lo +wrap_execbin.lo +wrap_execsh.lo +wrap_execv.lo +wrap_exitcode.lo +wrap_fork.lo +wrap_rename.lo +wrap_stat.lo +wrap_waitpid.lo diff --git a/Makefile b/Makefile index 6db36df..565afdd 100644 --- a/Makefile +++ b/Makefile @@ -936,7 +936,7 @@ libcompile hdr_ctboundary.c hdr.h qmail.h substdio.h stralloc.h gen_alloc.h ./libcompile hdr_ctboundary.c hdr_datemsgid.lo: \ -libcompile hdr_datemsgid.c hdr.h qmail.h substdio.h stralloc.h gen_alloc.h stralloc.h gen_alloc.h datetime.h fmt.h date822fmt.h makehash.h die.h hasattribute.h idx.h +libcompile hdr_datemsgid.c hdr.h qmail.h substdio.h stralloc.h gen_alloc.h stralloc.h gen_alloc.h datetime.h fmt.h date822fmt.h makehash.h die.h hasattribute.h idx.h slurp.h ./libcompile hdr_datemsgid.c hdr_from.lo: \ @@ -1318,7 +1318,7 @@ libcompile slurpclose.c stralloc.h gen_alloc.h readwrite.h slurpclose.h error.h ./libcompile slurpclose.c slurp.lo: \ -libcompile slurp.c stralloc.h gen_alloc.h slurp.h slurpclose.h error.h open.h +libcompile slurp.c stralloc.h gen_alloc.h slurp.h slurpclose.h error.h open.h readwrite.h ./libcompile slurp.c socket_bind.lo: \ diff --git a/ezmlm-make.c b/ezmlm-make.c index 7c43506..ced1ae6 100644 --- a/ezmlm-make.c +++ b/ezmlm-make.c @@ -24,6 +24,7 @@ #include "idx.h" #include "auto_etc.h" #include "auto_version.h" +#include "slurp.h" /* defaults. All other flags are false = 0 */ const char *defflags="ap"; /* archived list -a */ @@ -70,6 +71,11 @@ void die_read(void) strerr_die2sys(111,FATAL,MSG1(ERR_READ,dirplus.s)); } +static void die_open_rnd(void) +{ + strerr_die2x(100,FATAL,"failed to open/read random device"); +} + static stralloc outline = {0}; static substdio sstext; static char textbuf[1024]; @@ -81,24 +87,8 @@ static stralloc lname = {0}; /* link name */ static stralloc template = {0}; /* template file name */ static stralloc f = {0}; static stralloc key = {0}; -static struct timeval tv; static char sz = '?'; -void keyadd(unsigned long u) -{ - char ch; - ch = (char) u; if (!stralloc_append(&key,ch)) die_nomem(); u >>= 8; - ch = (char) u; if (!stralloc_append(&key,ch)) die_nomem(); u >>= 8; - ch = (char) u; if (!stralloc_append(&key,ch)) die_nomem(); u >>= 8; - ch = (char) u; if (!stralloc_append(&key,ch)) die_nomem(); -} - -void keyaddtime(void) -{ - gettimeofday(&tv,(struct timezone *) 0); - keyadd(tv.tv_usec); -} - static stralloc dir = {0}; static stralloc dot = {0}; static stralloc local = {0}; @@ -129,7 +119,6 @@ void linkdotdir(const char *dash,const char *slash) strerr_die2sys(111,FATAL,MSG1(ERR_DELETE,dotplus.s)); if (symlink(dirplus.s,dotplus.s) == -1) strerr_die2sys(111,FATAL,MSG1(ERR_CREATE,dotplus.s)); - keyaddtime(); } void dcreate(const char *slash) @@ -138,7 +127,6 @@ void dcreate(const char *slash) if (mkdir(dirplus.s,0755) == -1) if ((errno != error_exist) || !flags['e' - 'a']) strerr_die2sys(111,FATAL,MSG1(ERR_CREATE,dirplus.s)); - keyaddtime(); } substdio ss; @@ -175,7 +163,6 @@ void f_close(void) strerr_die2sys(111,FATAL,MSG1(ERR_SYNC,dirplus.s)); if (close(ss.fd) == -1) /* NFS stupidity */ strerr_die2sys(111,FATAL,MSG1(ERR_CLOSE,dirplus.s)); - keyaddtime(); } void frm(const char *slash) @@ -363,13 +350,7 @@ int main(int argc,char **argv) const char *p; unsigned char ch; - keyadd((unsigned long) getpid()); - keyadd((unsigned long) getppid()); - euid = (unsigned long) geteuid(); - keyadd(euid); - keyadd((unsigned long) getgid()); - gettimeofday(&tv,(struct timezone *) 0); - keyadd(tv.tv_sec); + if (slurprandom(&key, 64) != 1) die_open_rnd(); (void) umask(077); /* flags with defined use. vV for version. Others free */ diff --git a/hdr_datemsgid.c b/hdr_datemsgid.c index d4a0b76..09a28fd 100644 --- a/hdr_datemsgid.c +++ b/hdr_datemsgid.c @@ -8,6 +8,10 @@ #include "makehash.h" #include "die.h" #include "idx.h" +#include "slurp.h" + +/* Length of random base32 string in Message-ID */ +#define RNDIDLEN (16) extern struct qmail qq; extern char flagcd; @@ -17,8 +21,12 @@ extern stralloc outhost; void hdr_datemsgid(unsigned long when) { + static stralloc rndid = {0}; + static const unsigned char base32[] = "abcdefghijklmnopqrstuvwxyz234567"; char date[DATE822FMT]; char strnum[FMT_ULONG]; + int i; + struct datetime dt; qmail_puts(&qq,"Date: "); datetime_tai(&dt,when); @@ -27,8 +35,15 @@ void hdr_datemsgid(unsigned long when) if (!stralloc_copyb(&line,strnum,fmt_ulong(strnum,(unsigned long)when))) die_nomem(); if (!stralloc_append(&line,'.')) die_nomem(); - if (!stralloc_catb(&line,strnum, - fmt_ulong(strnum,(unsigned long) getpid()))) die_nomem(); + if (slurprandom(&rndid, RNDIDLEN) == 1) { + for (i = 0; i < RNDIDLEN; i++) { + rndid.s[i] = base32[rndid.s[i] % 32U]; + } + if (!stralloc_catb(&line, rndid.s, RNDIDLEN)) die_nomem(); + } else { + if (!stralloc_catb(&line,strnum, + fmt_ulong(strnum,(unsigned long) getpid()))) die_nomem(); + } if (!stralloc_cats(&line,".ezmlm@")) die_nomem(); if (!stralloc_cats(&line,outhost.s)) die_nomem(); if (!stralloc_0(&line)) die_nomem(); diff --git a/readwrite.h b/readwrite.h index 34b9c98..9423ad6 100644 --- a/readwrite.h +++ b/readwrite.h @@ -1,8 +1,6 @@ #ifndef READWRITE_H #define READWRITE_H -#include -extern ssize_t read(); -extern ssize_t write(); +#include #endif diff --git a/slurp.c b/slurp.c index 62e5239..265a3a0 100644 --- a/slurp.c +++ b/slurp.c @@ -3,6 +3,7 @@ #include "slurpclose.h" #include "error.h" #include "open.h" +#include "readwrite.h" int slurp(const char *fn,stralloc *sa,int bufsize) { @@ -15,3 +16,29 @@ int slurp(const char *fn,stralloc *sa,int bufsize) if (slurpclose(fd,sa,bufsize) == -1) return -1; return 1; } + +/* Append at max nbytes bytes to sa */ +int slurprandom(stralloc *sa, unsigned long nbytes) +{ + static const char *fn[] = {"/dev/urandom", "/dev/random", "/dev/srandom", ""}; + int fd; + long ret; + int i; + + if (!stralloc_copys(sa, "")) return -1; + for (i = 0; fn[i][0]; i++) { + fd = open_read(fn[i]); + if (fd != -1) break; + } + if (fd == -1) return -1; + while (sa->len < nbytes) { + if (!stralloc_readyplus(sa, nbytes)) { close(fd); return -1; } + ret = read(fd, sa->s + sa->len, nbytes - sa->len); + if ((ret == -1) && (errno == error_intr)) continue; + if (ret <= 0) { close(fd); return -1; } + sa->len += ret; + } + close(fd); + return 1; +} + diff --git a/slurp.h b/slurp.h index 183460a..f70a8d7 100644 --- a/slurp.h +++ b/slurp.h @@ -1,7 +1,9 @@ #ifndef SLURP_H #define SLURP_H -struct stralloc; -extern int slurp(const char *fn,struct stralloc *sa,int bufsize); +#include "stralloc.h" + +extern int slurp(const char *fn,stralloc *sa,int bufsize); +extern int slurprandom(stralloc *sa, unsigned long nbytes); #endif