From 10740bbfefe68763b4f0d74b25a074e95a2e13e3 Mon Sep 17 00:00:00 2001 From: manuel Date: Thu, 30 Jan 2014 12:27:13 +0100 Subject: replace \r\n with \n in sendmail wrapper --- Makefile | 11 ++++++----- sendmail.c | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 60 insertions(+), 8 deletions(-) diff --git a/Makefile b/Makefile index 0b98ee3..eac4649 100644 --- a/Makefile +++ b/Makefile @@ -1819,14 +1819,15 @@ compile trysysel.c select.h1 select.h2 rm -f trysysel.o trysysel sendmail: \ -load sendmail.o env.a getopt.a alloc.a substdio.a error.a str.a \ -auto_qmail.o - ./load sendmail env.a getopt.a alloc.a substdio.a error.a \ - str.a auto_qmail.o +load sendmail.o env.a getopt.a alloc.a getln.a substdio.a error.a str.a \ +auto_qmail.o stralloc.a wait.a fd.a sig.a + ./load sendmail env.a getopt.a alloc.a getln.a substdio.a error.a \ + str.a auto_qmail.o stralloc.a wait.a fd.a sig.a sendmail.o: \ compile sendmail.c sgetopt.h subgetopt.h substdio.h subfd.h \ -substdio.h alloc.h auto_qmail.h exit.h env.h str.h +substdio.h alloc.h auto_qmail.h exit.h env.h str.h stralloc.h \ +getln.h readwrite.h wait.h fd.h sig.h ./compile sendmail.c setup: \ diff --git a/sendmail.c b/sendmail.c index 5fc991a..c82a536 100644 --- a/sendmail.c +++ b/sendmail.c @@ -6,6 +6,12 @@ #include "exit.h" #include "env.h" #include "str.h" +#include "stralloc.h" +#include "getln.h" +#include "readwrite.h" +#include "wait.h" +#include "fd.h" +#include "sig.h" void nomem() { @@ -158,7 +164,52 @@ char **argv; for (i = 0;i < argc;++i) *arg++ = argv[i]; *arg = 0; - execv(*qiargv,qiargv); - substdio_putsflush(subfderr,"sendmail: fatal: unable to run qmail-inject\n"); - _exit(111); + int child, pi[2]; + if (pipe(pi) == -1) { + substdio_putsflush(subfderr,"sendmail: fatal: unable to open pipe\n"); + _exit(100); + } + + switch(child = fork()) { + case -1: + substdio_putsflush(subfderr,"sendmail: fatal: unable to fork\n"); + _exit(100); + case 0: + close(pi[1]); + if (fd_copy(0,pi[0]) == -1) { + substdio_putsflush(subfderr,"sendmail: fatal: unable to fork\n"); + _exit(100); + } + sig_pipedefault(); + execv(*qiargv,qiargv); + substdio_putsflush(subfderr,"sendmail: fatal: unable to run qmail-inject\n"); + _exit(111); + } + close(pi[0]); + + char buf[256]; + static substdio ss; + int match; + static stralloc line = {0}; + substdio_fdbuf(&ss,write,pi[1],buf,sizeof buf); + for (;;) { + if (getln(subfdin,&line,&match,'\n') != 0) { + substdio_putsflush(subfderr,"sendmail: fatal: read error\n"); + _exit(111); + } + + if (!match && !line.len) break; + + if (match && line.len >= 2 && line.s[line.len - 2] == '\r') { + line.s[line.len - 2] = '\n'; + line.len--; + } + substdio_put(&ss, line.s, line.len); + } + substdio_flush(&ss); + close(pi[1]); + + int wstat; + if (wait_pid(&wstat,child) == -1) _exit(111); + _exit(wait_exitcode(wstat)); } -- cgit v1.2.3