diff options
author | Jannis Hoffmann <jannis@fehcom.de> | 2024-07-03 15:52:39 +0200 |
---|---|---|
committer | Jannis Hoffmann <jannis@fehcom.de> | 2024-07-03 15:52:39 +0200 |
commit | a6a7d6ce079cabdaf2fa502b2e2cf15e5428ac6f (patch) | |
tree | b88cc7a8457658d67e0321718556ac807f6bccf3 /src/qmail-local.c | |
parent | 00be7622c428f279872f84569f098ce16150f8a8 (diff) |
format files
Diffstat (limited to 'src/qmail-local.c')
-rw-r--r-- | src/qmail-local.c | 660 |
1 files changed, 362 insertions, 298 deletions
diff --git a/src/qmail-local.c b/src/qmail-local.c index 7d1e6a3..4479e9b 100644 --- a/src/qmail-local.c +++ b/src/qmail-local.c @@ -1,33 +1,35 @@ -#include <sys/types.h> -#include <sys/time.h> -#include <sys/stat.h> #include <fcntl.h> +#include <sys/stat.h> +#include <sys/time.h> +#include <sys/types.h> #include <unistd.h> -#include "sig.h" -#include "env.h" + +#include "alloc.h" +#include "buffer.h" #include "byte.h" +#include "case.h" +#include "env.h" #include "exit.h" -#include "open.h" -#include "wait.h" -#include "lock.h" -#include "seek.h" -#include "buffer.h" +#include "fmt.h" #include "getln.h" #include "getoptb.h" -#include "alloc.h" +#include "lock.h" #include "logmsg.h" -#include "stralloc.h" -#include "fmt.h" -#include "str.h" -#include "now.h" -#include "case.h" -#include "quote.h" -#include "qmail.h" +#include "open.h" #include "readclose.h" -#include "myctime.h" -#include "gfrom.h" +#include "seek.h" +#include "sig.h" +#include "str.h" +#include "stralloc.h" +#include "wait.h" + #include "auto_break.h" #include "auto_patrn.h" +#include "gfrom.h" +#include "myctime.h" +#include "now.h" +#include "qmail.h" +#include "quote.h" /** @file qmail-local @@ -38,23 +40,45 @@ #define WHO "qmail-local" -void usage() { logmsg(WHO,100,USAGE,"qmail-local [ -nN ] user homedir local dash ext domain sender aliasempty"); } +void usage() +{ + logmsg(WHO, 100, USAGE, "qmail-local [ -nN ] user homedir local dash ext domain sender aliasempty"); +} -void temp_nomem() { logmsg(WHO,111,FATAL,"Out of memory. (#4.3.0)"); } -void temp_rewind() { logmsg(WHO,111,FATAL,"Unable to rewind message. (#4.3.0)"); } -void temp_childcrashed() { logmsg(WHO,111,FATAL,"Aack, child crashed. (#4.3.0)"); } -void temp_fork() { logmsg(WHO,111,FATAL,B("Unable to fork: ",error_str(errno),". (#4.3.0)")); } -void temp_read() { logmsg(WHO,111,ERROR,B("Unable to read message: ",error_str(errno),". (#4.3.0)")); } +void temp_nomem() +{ + logmsg(WHO, 111, FATAL, "Out of memory. (#4.3.0)"); +} +void temp_rewind() +{ + logmsg(WHO, 111, FATAL, "Unable to rewind message. (#4.3.0)"); +} +void temp_childcrashed() +{ + logmsg(WHO, 111, FATAL, "Aack, child crashed. (#4.3.0)"); +} +void temp_fork() +{ + logmsg(WHO, 111, FATAL, B("Unable to fork: ", error_str(errno), ". (#4.3.0)")); +} +void temp_read() +{ + logmsg(WHO, 111, ERROR, B("Unable to read message: ", error_str(errno), ". (#4.3.0)")); +} void temp_slowlock() -{ logmsg(WHO,111,ERROR,"File has been locked for 30 seconds straight. (#4.3.0)"); } +{ + logmsg(WHO, 111, ERROR, "File has been locked for 30 seconds straight. (#4.3.0)"); +} void temp_qmail(fn) char *fn; -{ logmsg(WHO,111,FATAL,B("Unable to open: ",fn," ",error_str(errno),". (#4.3.0)")); } +{ + logmsg(WHO, 111, FATAL, B("Unable to open: ", fn, " ", error_str(errno), ". (#4.3.0)")); +} int flagdoit; int flag99; char *user; -char *homedir; +char *homedir; char *local; char *dash; char *ext; @@ -81,8 +105,15 @@ char bufout[1024]; char fntmptph[80 + FMT_ULONG * 2]; char fnnewtph[80 + FMT_ULONG * 2]; -void tryunlinktmp() { unlink(fntmptph); } -void sigalrm() { tryunlinktmp(); _exit(3); } +void tryunlinktmp() +{ + unlink(fntmptph); +} +void sigalrm() +{ + tryunlinktmp(); + _exit(3); +} void maildir_child(char *dir) { @@ -97,34 +128,43 @@ void maildir_child(char *dir) buffer bo; sig_alarmcatch(sigalrm); - if (chdir(dir) == -1) { if (errno != ENOENT) _exit(1); _exit(2); } + if (chdir(dir) == -1) { + if (errno != ENOENT) _exit(1); + _exit(2); + } pid = getpid(); host[0] = 0; - gethostname(host,sizeof(host)); + gethostname(host, sizeof(host)); s = host; for (loop = 0; loop < str_len(host); ++loop) { if (host[loop] == '/') { - if (!stralloc_cats(&hostname,"\\057")) temp_nomem(); - continue; + if (!stralloc_cats(&hostname, "\\057")) temp_nomem(); + continue; } if (host[loop] == ':') { - if (!stralloc_cats(&hostname,"\\072")) temp_nomem(); + if (!stralloc_cats(&hostname, "\\072")) temp_nomem(); continue; } - if (!stralloc_append(&hostname,s+loop)) temp_nomem(); + if (!stralloc_append(&hostname, s + loop)) temp_nomem(); } - for (loop = 0 ;; ++loop) { - gettimeofday(&time,0); + for (loop = 0;; ++loop) { + gettimeofday(&time, 0); s = fntmptph; - s += fmt_str(s,"tmp/"); - s += fmt_ulong(s,time.tv_sec); *s++ = '.'; - *s++ = 'M'; s += fmt_ulong(s,time.tv_usec); - *s++ = 'P'; s += fmt_ulong(s,pid); *s++ = '.'; - s += fmt_strn(s,hostname.s,hostname.len); *s++ = 0; - - if (stat(fntmptph,&st) == -1) if (errno == ENOENT) break; + s += fmt_str(s, "tmp/"); + s += fmt_ulong(s, time.tv_sec); + *s++ = '.'; + *s++ = 'M'; + s += fmt_ulong(s, time.tv_usec); + *s++ = 'P'; + s += fmt_ulong(s, pid); + *s++ = '.'; + s += fmt_strn(s, hostname.s, hostname.len); + *s++ = 0; + + if (stat(fntmptph, &st) == -1) + if (errno == ENOENT) break; /* really should never get to this point */ if (loop == 2) _exit(1); sleep(2); @@ -134,41 +174,51 @@ void maildir_child(char *dir) fd = open_excl(fntmptph); if (fd == -1) _exit(1); - buffer_init(&bi,read,0,bufin,sizeof(bufin)); - buffer_init(&bo,write,fd,bufout,sizeof(bufout)); - if (buffer_put(&bo,rpline.s,rpline.len) == -1) goto FAIL; - if (buffer_put(&bo,dtline.s,dtline.len) == -1) goto FAIL; + buffer_init(&bi, read, 0, bufin, sizeof(bufin)); + buffer_init(&bo, write, fd, bufout, sizeof(bufout)); + if (buffer_put(&bo, rpline.s, rpline.len) == -1) goto FAIL; + if (buffer_put(&bo, dtline.s, dtline.len) == -1) goto FAIL; - switch (buffer_copy(&bo,&bi)) { + switch (buffer_copy(&bo, &bi)) { case -2: tryunlinktmp(); _exit(4); case -3: goto FAIL; } if (buffer_flush(&bo) == -1) goto FAIL; - if (fstat(fd,&st) == -1) goto FAIL; + if (fstat(fd, &st) == -1) goto FAIL; if (fsync(fd) == -1) goto FAIL; if (close(fd) == -1) goto FAIL; /* NFS dorks */ s = fnnewtph; - s += fmt_str(s,"new/"); - s += fmt_ulong(s,time.tv_sec); *s++ = '.'; - + s += fmt_str(s, "new/"); + s += fmt_ulong(s, time.tv_sec); + *s++ = '.'; + /* in hexadecimal */ - *s++ = 'I'; s += fmt_xlong(s,st.st_ino); - *s++ = 'V'; s += fmt_xlong(s,st.st_dev); - - /* in decimal */ - *s++ = 'M'; s += fmt_ulong(s,time.tv_usec); - *s++ = 'P'; s += fmt_ulong(s,pid); *s++ = '.'; - - s += fmt_strn(s,hostname.s,hostname.len); *s++ = 0; + *s++ = 'I'; + s += fmt_xlong(s, st.st_ino); + *s++ = 'V'; + s += fmt_xlong(s, st.st_dev); - if (link(fntmptph,fnnewtph) == -1) goto FAIL; - if ((fd = open(fnnewtph,O_RDONLY)) < 0 || fsync(fd) < 0 || close(fd)) goto FAIL; - /* DJB: if it was error_exist, almost certainly successful; i hate NFS -- FEH: Reiser patch */ - tryunlinktmp(); _exit(0); + /* in decimal */ + *s++ = 'M'; + s += fmt_ulong(s, time.tv_usec); + *s++ = 'P'; + s += fmt_ulong(s, pid); + *s++ = '.'; + + s += fmt_strn(s, hostname.s, hostname.len); + *s++ = 0; + + if (link(fntmptph, fnnewtph) == -1) goto FAIL; + if ((fd = open(fnnewtph, O_RDONLY)) < 0 || fsync(fd) < 0 || close(fd)) goto FAIL; + /* DJB: if it was error_exist, almost certainly successful; i hate NFS -- FEH: Reiser patch */ + tryunlinktmp(); + _exit(0); - FAIL: tryunlinktmp(); _exit(1); +FAIL: + tryunlinktmp(); + _exit(1); } /* end child process */ @@ -181,23 +231,19 @@ void maildir(char *fn) if (seek_begin(0) == -1) temp_rewind(); switch (child = fork()) { - case -1: - temp_fork(); - case 0: - maildir_child(fn); - _exit(111); + case -1: temp_fork(); + case 0: maildir_child(fn); _exit(111); } - wait_pid(&wstat,child); - if (wait_crashed(wstat)) - temp_childcrashed(); + wait_pid(&wstat, child); + if (wait_crashed(wstat)) temp_childcrashed(); switch (wait_exitcode(wstat)) { - case 0: break; - case 2: logmsg(WHO,111,ERROR,"Unable to chdir to maildir. (#4.2.1)"); - case 3: logmsg(WHO,111,ERROR,"Timeout on maildir delivery. (#4.3.0)"); - case 4: logmsg(WHO,111,ERROR,"Unable to read message. (#4.3.0)"); - default: logmsg(WHO,111,ERROR,"Temporary error on maildir delivery. (#4.3.0)"); + case 0: break; + case 2: logmsg(WHO, 111, ERROR, "Unable to chdir to maildir. (#4.2.1)"); + case 3: logmsg(WHO, 111, ERROR, "Timeout on maildir delivery. (#4.3.0)"); + case 4: logmsg(WHO, 111, ERROR, "Unable to read message. (#4.3.0)"); + default: logmsg(WHO, 111, ERROR, "Temporary error on maildir delivery. (#4.3.0)"); } } @@ -213,8 +259,7 @@ void mailfile(char *fn) if (seek_begin(0) == -1) temp_rewind(); fd = open_append(fn); - if (fd == -1) - logmsg(WHO,111,ERROR,B("Unable to open:",fn," ",error_str(errno),". (#4.2.1)")); + if (fd == -1) logmsg(WHO, 111, ERROR, B("Unable to open:", fn, " ", error_str(errno), ". (#4.2.1)")); sig_alarmcatch(temp_slowlock); alarm(30); @@ -225,38 +270,38 @@ void mailfile(char *fn) seek_end(fd); pos = seek_cur(fd); - buffer_init(&bi,read,0,bufin,sizeof(bufin)); - buffer_init(&bo,write,fd,bufout,sizeof(bufout)); - if (buffer_put(&bo,ufline.s,ufline.len)) goto WRITERRS; - if (buffer_put(&bo,rpline.s,rpline.len)) goto WRITERRS; - if (buffer_put(&bo,dtline.s,dtline.len)) goto WRITERRS; - + buffer_init(&bi, read, 0, bufin, sizeof(bufin)); + buffer_init(&bo, write, fd, bufout, sizeof(bufout)); + if (buffer_put(&bo, ufline.s, ufline.len)) goto WRITERRS; + if (buffer_put(&bo, rpline.s, rpline.len)) goto WRITERRS; + if (buffer_put(&bo, dtline.s, dtline.len)) goto WRITERRS; + for (;;) { - if (getln(&bi,&messline,&match,'\n') != 0) { - logmsg(WHO,0,WARN,B("Unable to read message: ",error_str(errno),". (#4.3.0)")); - if (flaglocked) seek_trunc(fd,pos); + if (getln(&bi, &messline, &match, '\n') != 0) { + logmsg(WHO, 0, WARN, B("Unable to read message: ", error_str(errno), ". (#4.3.0)")); + if (flaglocked) seek_trunc(fd, pos); close(fd); _exit(111); } if (!match && !messline.len) break; - if (gfrom(messline.s,messline.len)) - if (buffer_puts(&bo,">")) goto WRITERRS; - if (buffer_put(&bo,messline.s,messline.len)) goto WRITERRS; + if (gfrom(messline.s, messline.len)) + if (buffer_puts(&bo, ">")) goto WRITERRS; + if (buffer_put(&bo, messline.s, messline.len)) goto WRITERRS; if (!match) { - if (buffer_puts(&bo,"\n")) goto WRITERRS; + if (buffer_puts(&bo, "\n")) goto WRITERRS; break; } } - if (buffer_puts(&bo,"\n")) goto WRITERRS; + if (buffer_puts(&bo, "\n")) goto WRITERRS; if (buffer_flush(&bo)) goto WRITERRS; if (fsync(fd) == -1) goto WRITERRS; close(fd); return; - WRITERRS: - logmsg(WHO,0,WARN,B("Unable to write ",fn,": ",error_str(errno),". (#4.3.0)")); - if (flaglocked) seek_trunc(fd,pos); +WRITERRS: + logmsg(WHO, 0, WARN, B("Unable to write ", fn, ": ", error_str(errno), ". (#4.3.0)")); + if (flaglocked) seek_trunc(fd, pos); close(fd); _exit(111); } @@ -270,29 +315,33 @@ void mailprogram(char *prog) if (seek_begin(0) == -1) temp_rewind(); switch (child = fork()) { - case -1: - temp_fork(); + case -1: temp_fork(); case 0: - args[0] = "/bin/sh"; - args[1] = "-c"; - args[2] = prog; + args[0] = "/bin/sh"; + args[1] = "-c"; + args[2] = prog; args[3] = 0; sig_pipedefault(); - execv(*args,args); - logmsg(WHO,0,ERROR,B("Unable to run /bin/sh: ",error_str(errno),". (#4.3.0)")); + execv(*args, args); + logmsg(WHO, 0, ERROR, B("Unable to run /bin/sh: ", error_str(errno), ". (#4.3.0)")); } - wait_pid(&wstat,child); - if (wait_crashed(wstat)) - temp_childcrashed(); + wait_pid(&wstat, child); + if (wait_crashed(wstat)) temp_childcrashed(); switch (wait_exitcode(wstat)) { case 100: - case 64: case 65: case 70: case 76: case 77: case 78: case 112: _exit(100); - case 0: break; - case 99: flag99 = 1; break; - default: _exit(111); - } + case 64: + case 65: + case 70: + case 76: + case 77: + case 78: + case 112: _exit(100); + case 0: break; + case 99: flag99 = 1; break; + default: _exit(111); + } } unsigned long mailforward_qp = 0; @@ -305,22 +354,25 @@ void mailforward(char **recips) int match; if (seek_begin(0) == -1) temp_rewind(); - buffer_init(&bi,read,0,bufin,sizeof(bufin)); + buffer_init(&bi, read, 0, bufin, sizeof(bufin)); if (qmail_open(&qqt) == -1) temp_fork(); mailforward_qp = qmail_qp(&qqt); - qmail_put(&qqt,dtline.s,dtline.len); + qmail_put(&qqt, dtline.s, dtline.len); do { - if (getln(&bi,&messline,&match,'\n') != 0) { qmail_fail(&qqt); break; } - qmail_put(&qqt,messline.s,messline.len); + if (getln(&bi, &messline, &match, '\n') != 0) { + qmail_fail(&qqt); + break; + } + qmail_put(&qqt, messline.s, messline.len); } while (match); - qmail_from(&qqt,ueo.s); - while (*recips) qmail_to(&qqt,*recips++); + qmail_from(&qqt, ueo.s); + while (*recips) qmail_to(&qqt, *recips++); qqx = qmail_close(&qqt); if (!*qqx) return; - logmsg(WHO,*qqx == 'D' ? 100 : 111,ERROR,B("Unable to forward message: ",qqx + 1,".")); + logmsg(WHO, *qqx == 'D' ? 100 : 111, ERROR, B("Unable to forward message: ", qqx + 1, ".")); } void bouncexf() @@ -329,16 +381,16 @@ void bouncexf() buffer bi; if (seek_begin(0) == -1) temp_rewind(); - buffer_init(&bi,read,0,bufin,sizeof(bufin)); + buffer_init(&bi, read, 0, bufin, sizeof(bufin)); for (;;) { - if (getln(&bi,&messline,&match,'\n') != 0) temp_read(); + if (getln(&bi, &messline, &match, '\n') != 0) temp_read(); if (!match) break; - if (messline.len <= 1) - break; + if (messline.len <= 1) break; if (messline.len == dtline.len) - if (!str_diffn(messline.s,dtline.s,dtline.len)) - logmsg(WHO,100,ERROR,"This message is looping: it already has my Delivered-To line. (#5.4.6)"); + if (!str_diffn(messline.s, dtline.s, dtline.len)) + logmsg( + WHO, 100, ERROR, "This message is looping: it already has my Delivered-To line. (#5.4.6)"); } } @@ -346,35 +398,34 @@ void checkhome() { struct stat st; - if (stat(".",&st) == -1) - logmsg(WHO,111,ERROR,B("Unable to stat home directory: ",error_str(errno),". (#4.3.0)")); - if (st.st_mode & auto_patrn) - logmsg(WHO,111,ERROR,"Uh-oh: home directory is writable. (#4.7.0)"); + if (stat(".", &st) == -1) + logmsg(WHO, 111, ERROR, B("Unable to stat home directory: ", error_str(errno), ". (#4.3.0)")); + if (st.st_mode & auto_patrn) logmsg(WHO, 111, ERROR, "Uh-oh: home directory is writable. (#4.7.0)"); if (st.st_mode & 01000) if (flagdoit) - logmsg(WHO,111,ERROR,"Home directory is sticky: user is editing his .qmail file. (#4.2.1)"); + logmsg(WHO, 111, ERROR, "Home directory is sticky: user is editing his .qmail file. (#4.2.1)"); else - logmsg(WHO,0,WARN,"Warning: home directory is sticky."); + logmsg(WHO, 0, WARN, "Warning: home directory is sticky."); } int qmeox(char *dashowner) { struct stat st; - - if (!stralloc_copys(&qme,".qmail")) temp_nomem(); - if (!stralloc_cats(&qme,dash)) temp_nomem(); - if (!stralloc_cat(&qme,&safeext)) temp_nomem(); - if (!stralloc_cats(&qme,dashowner)) temp_nomem(); + + if (!stralloc_copys(&qme, ".qmail")) temp_nomem(); + if (!stralloc_cats(&qme, dash)) temp_nomem(); + if (!stralloc_cat(&qme, &safeext)) temp_nomem(); + if (!stralloc_cats(&qme, dashowner)) temp_nomem(); if (!stralloc_0(&qme)) temp_nomem(); - if (stat(qme.s,&st) == -1) { + if (stat(qme.s, &st) == -1) { if (errno != ENOENT) temp_qmail(qme.s); return -1; } - return 0; + return 0; } -int qmeexists(int *fd,int *cutable) +int qmeexists(int *fd, int *cutable) { struct stat st; @@ -388,10 +439,9 @@ int qmeexists(int *fd,int *cutable) return 0; } - if (fstat(*fd,&st) == -1) temp_qmail(qme.s); + if (fstat(*fd, &st) == -1) temp_qmail(qme.s); if ((st.st_mode & S_IFMT) == S_IFREG) { - if (st.st_mode & auto_patrn) - logmsg(WHO,111,ERROR,"Uh-oh: .qmail file is writable. (#4.7.0)"); + if (st.st_mode & auto_patrn) logmsg(WHO, 111, ERROR, "Uh-oh: .qmail file is writable. (#4.7.0)"); *cutable = !!(st.st_mode & 0100); return 1; } @@ -407,32 +457,32 @@ int qmeexists(int *fd,int *cutable) /* "-/" "a-b-": "-/a-b-" "-/a-b-default" "-/a-default" "-/default" */ /* "-/" "a-b-c": "-/a-b-c" "-/a-b-default" "-/a-default" "-/default" */ -void qmesearch(int *fd,int *cutable) +void qmesearch(int *fd, int *cutable) { int i; - if (!stralloc_copys(&qme,".qmail")) temp_nomem(); - if (!stralloc_cats(&qme,dash)) temp_nomem(); - if (!stralloc_cat(&qme,&safeext)) temp_nomem(); - if (qmeexists(fd,cutable)) { + if (!stralloc_copys(&qme, ".qmail")) temp_nomem(); + if (!stralloc_cats(&qme, dash)) temp_nomem(); + if (!stralloc_cat(&qme, &safeext)) temp_nomem(); + if (qmeexists(fd, cutable)) { if (safeext.len >= 7) { i = safeext.len - 7; - if (!byte_diff("default",7,safeext.s + i)) + if (!byte_diff("default", 7, safeext.s + i)) if (i <= str_len(ext)) /* paranoia */ - if (!env_put("DEFAULT",ext + i)) temp_nomem(); + if (!env_put("DEFAULT", ext + i)) temp_nomem(); } return; } - for (i = safeext.len; i >= 0 ;--i) + for (i = safeext.len; i >= 0; --i) if (!i || (safeext.s[i - 1] == '-')) { - if (!stralloc_copys(&qme,".qmail")) temp_nomem(); - if (!stralloc_cats(&qme,dash)) temp_nomem(); - if (!stralloc_catb(&qme,safeext.s,i)) temp_nomem(); - if (!stralloc_cats(&qme,"default")) temp_nomem(); - if (qmeexists(fd,cutable)) { + if (!stralloc_copys(&qme, ".qmail")) temp_nomem(); + if (!stralloc_cats(&qme, dash)) temp_nomem(); + if (!stralloc_catb(&qme, safeext.s, i)) temp_nomem(); + if (!stralloc_cats(&qme, "default")) temp_nomem(); + if (qmeexists(fd, cutable)) { if (i <= str_len(ext)) /* paranoia */ - if (!env_put("DEFAULT",ext + i)) temp_nomem(); + if (!env_put("DEFAULT", ext + i)) temp_nomem(); return; } } @@ -445,34 +495,34 @@ unsigned long count_forward = 0; unsigned long count_program = 0; char count_buf[FMT_ULONG]; char buflog[256]; -buffer bl = BUFFER_INIT(write,1,buflog,sizeof(buflog)); +buffer bl = BUFFER_INIT(write, 1, buflog, sizeof(buflog)); void count_print() { - buffer_puts(&bl,"did "); - buffer_put(&bl,count_buf,fmt_ulong(count_buf,count_file)); - buffer_puts(&bl,"+"); - buffer_put(&bl,count_buf,fmt_ulong(count_buf,count_forward)); - buffer_puts(&bl,"+"); - buffer_put(&bl,count_buf,fmt_ulong(count_buf,count_program)); - buffer_puts(&bl,"\n"); + buffer_puts(&bl, "did "); + buffer_put(&bl, count_buf, fmt_ulong(count_buf, count_file)); + buffer_puts(&bl, "+"); + buffer_put(&bl, count_buf, fmt_ulong(count_buf, count_forward)); + buffer_puts(&bl, "+"); + buffer_put(&bl, count_buf, fmt_ulong(count_buf, count_program)); + buffer_puts(&bl, "\n"); if (mailforward_qp) { - buffer_puts(&bl,"qp "); - buffer_put(&bl,count_buf,fmt_ulong(count_buf,mailforward_qp)); - buffer_puts(&bl,"\n"); + buffer_puts(&bl, "qp "); + buffer_put(&bl, count_buf, fmt_ulong(count_buf, mailforward_qp)); + buffer_puts(&bl, "\n"); } buffer_flush(&bl); } -void sayit(char *type,char *cmd,int len) +void sayit(char *type, char *cmd, int len) { - buffer_puts(&bl,type); - buffer_put(&bl,cmd,len); - buffer_putsflush(&bl,"\n"); + buffer_puts(&bl, type); + buffer_put(&bl, cmd, len); + buffer_putsflush(&bl, "\n"); } -int main(int argc,char **argv) +int main(int argc, char **argv) { int opt; int i, j, k; @@ -489,11 +539,11 @@ int main(int argc,char **argv) if (!env_init()) temp_nomem(); flagdoit = 1; - while ((opt = getopt(argc,argv,"nN")) != opteof) - switch (opt) { - case 'n': flagdoit = 0; break; - case 'N': flagdoit = 1; break; - default: usage(); + while ((opt = getopt(argc, argv, "nN")) != opteof) switch (opt) + { + case 'n': flagdoit = 0; break; + case 'N': flagdoit = 1; break; + default: usage(); } argc -= optind; argv += optind; @@ -510,159 +560,167 @@ int main(int argc,char **argv) if (homedir[0] != '/') usage(); if (chdir(homedir) == -1) - logmsg(WHO,111,ERROR,B("Unable to switch to: ",homedir," ",error_str(errno),". (#4.3.0)")); + logmsg(WHO, 111, ERROR, B("Unable to switch to: ", homedir, " ", error_str(errno), ". (#4.3.0)")); checkhome(); - if (!env_put("HOST",host)) temp_nomem(); - if (!env_put("HOME",homedir)) temp_nomem(); - if (!env_put("USER",user)) temp_nomem(); - if (!env_put("LOCAL",local)) temp_nomem(); + if (!env_put("HOST", host)) temp_nomem(); + if (!env_put("HOME", homedir)) temp_nomem(); + if (!env_put("USER", user)) temp_nomem(); + if (!env_put("LOCAL", local)) temp_nomem(); #ifdef HIDEVIRTUALUSER if (str_len(ext) > 1) { - i = str_chr(local,*auto_break); - if (!stralloc_copys(&envrecip,local + i + 1)) temp_nomem(); - } else + i = str_chr(local, *auto_break); + if (!stralloc_copys(&envrecip, local + i + 1)) temp_nomem(); + } else #endif - if (!stralloc_copys(&envrecip,local)) temp_nomem(); - if (!stralloc_cats(&envrecip,"@")) temp_nomem(); - if (!stralloc_cats(&envrecip,host)) temp_nomem(); + if (!stralloc_copys(&envrecip, local)) + temp_nomem(); + if (!stralloc_cats(&envrecip, "@")) temp_nomem(); + if (!stralloc_cats(&envrecip, host)) temp_nomem(); - if (!stralloc_copy(&foo,&envrecip)) temp_nomem(); + if (!stralloc_copy(&foo, &envrecip)) temp_nomem(); if (!stralloc_0(&foo)) temp_nomem(); - if (!env_put("RECIPIENT",foo.s)) temp_nomem(); + if (!env_put("RECIPIENT", foo.s)) temp_nomem(); + + if (!stralloc_copys(&dtline, "Delivered-To: ")) temp_nomem(); + if (!stralloc_cat(&dtline, &envrecip)) temp_nomem(); - if (!stralloc_copys(&dtline,"Delivered-To: ")) temp_nomem(); - if (!stralloc_cat(&dtline,&envrecip)) temp_nomem(); - - for (i = 0; i < dtline.len; ++i) - if (dtline.s[i] == '\n') dtline.s[i] = '_'; - if (!stralloc_cats(&dtline,"\n")) temp_nomem(); + for (i = 0; i < dtline.len; ++i) + if (dtline.s[i] == '\n') dtline.s[i] = '_'; + if (!stralloc_cats(&dtline, "\n")) temp_nomem(); - if (!stralloc_copy(&foo,&dtline)) temp_nomem(); + if (!stralloc_copy(&foo, &dtline)) temp_nomem(); if (!stralloc_0(&foo)) temp_nomem(); - if (!env_put("DTLINE",foo.s)) temp_nomem(); + if (!env_put("DTLINE", foo.s)) temp_nomem(); if (flagdoit) bouncexf(); - if (!env_put("SENDER",sender)) temp_nomem(); + if (!env_put("SENDER", sender)) temp_nomem(); - if (!quote2(&foo,sender)) temp_nomem(); - if (!stralloc_copys(&rpline,"Return-Path: <")) temp_nomem(); - if (!stralloc_cat(&rpline,&foo)) temp_nomem(); - for (i = 0;i < rpline.len;++i) if (rpline.s[i] == '\n') rpline.s[i] = '_'; - if (!stralloc_cats(&rpline,">\n")) temp_nomem(); + if (!quote2(&foo, sender)) temp_nomem(); + if (!stralloc_copys(&rpline, "Return-Path: <")) temp_nomem(); + if (!stralloc_cat(&rpline, &foo)) temp_nomem(); + for (i = 0; i < rpline.len; ++i) + if (rpline.s[i] == '\n') rpline.s[i] = '_'; + if (!stralloc_cats(&rpline, ">\n")) temp_nomem(); - if (!stralloc_copy(&foo,&rpline)) temp_nomem(); + if (!stralloc_copy(&foo, &rpline)) temp_nomem(); if (!stralloc_0(&foo)) temp_nomem(); - if (!env_put("RPLINE",foo.s)) temp_nomem(); + if (!env_put("RPLINE", foo.s)) temp_nomem(); - if (!stralloc_copys(&ufline,"From ")) temp_nomem(); + if (!stralloc_copys(&ufline, "From ")) temp_nomem(); if (*sender) { - int len; int i; char ch; + int len; + int i; + char ch; len = str_len(sender); - if (!stralloc_readyplus(&ufline,len)) temp_nomem(); + if (!stralloc_readyplus(&ufline, len)) temp_nomem(); - for (i = 0;i < len;++i) { + for (i = 0; i < len; ++i) { ch = sender[i]; if ((ch == ' ') || (ch == '\t') || (ch == '\n')) ch = '-'; ufline.s[ufline.len + i] = ch; } ufline.len += len; - } else - if (!stralloc_cats(&ufline,"MAILER-DAEMON")) temp_nomem(); + } else if (!stralloc_cats(&ufline, "MAILER-DAEMON")) + temp_nomem(); - if (!stralloc_cats(&ufline," ")) temp_nomem(); + if (!stralloc_cats(&ufline, " ")) temp_nomem(); starttime = now(); - if (!stralloc_cats(&ufline,myctime(starttime))) temp_nomem(); + if (!stralloc_cats(&ufline, myctime(starttime))) temp_nomem(); - if (!stralloc_copy(&foo,&ufline)) temp_nomem(); + if (!stralloc_copy(&foo, &ufline)) temp_nomem(); if (!stralloc_0(&foo)) temp_nomem(); - if (!env_put("UFLINE",foo.s)) temp_nomem(); + if (!env_put("UFLINE", foo.s)) temp_nomem(); x = ext; - if (!env_put("EXT",x)) temp_nomem(); - x += str_chr(x,'-'); if (*x) ++x; - if (!env_put("EXT2",x)) temp_nomem(); - x += str_chr(x,'-'); if (*x) ++x; - if (!env_put("EXT3",x)) temp_nomem(); - x += str_chr(x,'-'); if (*x) ++x; - if (!env_put("EXT4",x)) temp_nomem(); - - if (!stralloc_copys(&safeext,ext)) temp_nomem(); - case_lowerb(safeext.s,safeext.len); + if (!env_put("EXT", x)) temp_nomem(); + x += str_chr(x, '-'); + if (*x) ++x; + if (!env_put("EXT2", x)) temp_nomem(); + x += str_chr(x, '-'); + if (*x) ++x; + if (!env_put("EXT3", x)) temp_nomem(); + x += str_chr(x, '-'); + if (*x) ++x; + if (!env_put("EXT4", x)) temp_nomem(); + + if (!stralloc_copys(&safeext, ext)) temp_nomem(); + case_lowerb(safeext.s, safeext.len); for (i = 0; i < safeext.len; ++i) - if (safeext.s[i] == '.') - safeext.s[i] = ':'; + if (safeext.s[i] == '.') safeext.s[i] = ':'; i = str_len(host); - i = byte_rchr(host,i,'.'); - if (!stralloc_copyb(&foo,host,i)) temp_nomem(); + i = byte_rchr(host, i, '.'); + if (!stralloc_copyb(&foo, host, i)) temp_nomem(); if (!stralloc_0(&foo)) temp_nomem(); - if (!env_put("HOST2",foo.s)) temp_nomem(); - i = byte_rchr(host,i,'.'); - if (!stralloc_copyb(&foo,host,i)) temp_nomem(); + if (!env_put("HOST2", foo.s)) temp_nomem(); + i = byte_rchr(host, i, '.'); + if (!stralloc_copyb(&foo, host, i)) temp_nomem(); if (!stralloc_0(&foo)) temp_nomem(); - if (!env_put("HOST3",foo.s)) temp_nomem(); - i = byte_rchr(host,i,'.'); - if (!stralloc_copyb(&foo,host,i)) temp_nomem(); + if (!env_put("HOST3", foo.s)) temp_nomem(); + i = byte_rchr(host, i, '.'); + if (!stralloc_copyb(&foo, host, i)) temp_nomem(); if (!stralloc_0(&foo)) temp_nomem(); - if (!env_put("HOST4",foo.s)) temp_nomem(); + if (!env_put("HOST4", foo.s)) temp_nomem(); flagforwardonly = 0; - qmesearch(&fd,&flagforwardonly); + qmesearch(&fd, &flagforwardonly); if (fd == -1) - if (*dash) - logmsg(WHO,100,ERROR,"Sorry, no mailbox here by that name. (#5.1.1)"); + if (*dash) logmsg(WHO, 100, ERROR, "Sorry, no mailbox here by that name. (#5.1.1)"); - if (!stralloc_copys(&ueo,sender)) temp_nomem(); - if (str_diff(sender,"")) - if (str_diff(sender,"#@[]")) + if (!stralloc_copys(&ueo, sender)) temp_nomem(); + if (str_diff(sender, "")) + if (str_diff(sender, "#@[]")) if (qmeox("-owner") == 0) { if (qmeox("-owner-default") == 0) { - if (!stralloc_copys(&ueo,local)) temp_nomem(); - if (!stralloc_cats(&ueo,"-owner-@")) temp_nomem(); - if (!stralloc_cats(&ueo,host)) temp_nomem(); - if (!stralloc_cats(&ueo,"-@[]")) temp_nomem(); + if (!stralloc_copys(&ueo, local)) temp_nomem(); + if (!stralloc_cats(&ueo, "-owner-@")) temp_nomem(); + if (!stralloc_cats(&ueo, host)) temp_nomem(); + if (!stralloc_cats(&ueo, "-@[]")) temp_nomem(); } else { - if (!stralloc_copys(&ueo,local)) temp_nomem(); - if (!stralloc_cats(&ueo,"-owner@")) temp_nomem(); - if (!stralloc_cats(&ueo,host)) temp_nomem(); + if (!stralloc_copys(&ueo, local)) temp_nomem(); + if (!stralloc_cats(&ueo, "-owner@")) temp_nomem(); + if (!stralloc_cats(&ueo, host)) temp_nomem(); } } if (!stralloc_0(&ueo)) temp_nomem(); - if (!env_put("NEWSENDER",ueo.s)) temp_nomem(); + if (!env_put("NEWSENDER", ueo.s)) temp_nomem(); - if (!stralloc_ready(&cmds,0)) temp_nomem(); + if (!stralloc_ready(&cmds, 0)) temp_nomem(); cmds.len = 0; if (fd != -1) - if (readclose_append(fd,&cmds,256) == -1) temp_nomem(); + if (readclose_append(fd, &cmds, 256) == -1) temp_nomem(); if (!cmds.len) { - if (!stralloc_copys(&cmds,aliasempty)) temp_nomem(); + if (!stralloc_copys(&cmds, aliasempty)) temp_nomem(); flagforwardonly = 0; } if (!cmds.len || (cmds.s[cmds.len - 1] != '\n')) - if (!stralloc_cats(&cmds,"\n")) temp_nomem(); + if (!stralloc_cats(&cmds, "\n")) temp_nomem(); numforward = 0; i = 0; for (j = 0; j < cmds.len; ++j) if (cmds.s[j] == '\n') { - switch (cmds.s[i]) { - case '#': case '.': case '/': case '|': break; - default: ++numforward; + switch (cmds.s[i]) { + case '#': + case '.': + case '/': + case '|': break; + default: ++numforward; } i = j + 1; } - recips = (char **) alloc((numforward + 1) * sizeof(char *)); + recips = (char **)alloc((numforward + 1) * sizeof(char *)); if (!recips) temp_nomem(); numforward = 0; @@ -673,52 +731,58 @@ int main(int argc,char **argv) if (cmds.s[j] == '\n') { cmds.s[j] = 0; k = j; - /* Patch contributed by Erik Sjolund <erik.sjolund@gmail.com>. */ - while ((k > i) && ((cmds.s[k - 1] == ' ') || (cmds.s[k - 1] == '\t'))) - cmds.s[--k] = 0; + /* Patch contributed by Erik Sjolund <erik.sjolund@gmail.com>. */ + while ((k > i) && ((cmds.s[k - 1] == ' ') || (cmds.s[k - 1] == '\t'))) cmds.s[--k] = 0; switch (cmds.s[i]) { case 0: /* k == i */ if (i) break; - logmsg(WHO,111,ERROR,"Uh-oh: first line of .qmail file is blank. (#4.2.1)"); - case '#': - break; + logmsg(WHO, 111, ERROR, "Uh-oh: first line of .qmail file is blank. (#4.2.1)"); + case '#': break; case '.': case '/': - ++count_file; - if (flagforwardonly) logmsg(WHO,111,ERROR,"Uh-oh: .qmail has file delivery but has x bit set. (#4.7.0)"); - if (cmds.s[k - 1] == '/') - if (flagdoit) maildir(cmds.s + i); - else sayit("maildir ",cmds.s + i,k - i); - else - if (flagdoit) mailfile(cmds.s + i); - else sayit("mbox ",cmds.s + i,k - i); - break; - case '|': - ++count_program; - if (flagforwardonly) logmsg(WHO,111,ERROR,"Uh-oh: .qmail has prog delivery but has x bit set. (#4.7.0)"); - if (flagdoit) mailprogram(cmds.s + i + 1); - else sayit("program ",cmds.s + i + 1,k - i - 1); - break; - case '+': - if (str_equal(cmds.s + i + 1,"list")) - flagforwardonly = 1; - break; - case '&': - ++i; - default: - ++count_forward; - if (flagdoit) recips[numforward++] = cmds.s + i; - else sayit("forward ",cmds.s + i,k - i); - break; + ++count_file; + if (flagforwardonly) + logmsg(WHO, 111, ERROR, "Uh-oh: .qmail has file delivery but has x bit set. (#4.7.0)"); + if (cmds.s[k - 1] == '/') + if (flagdoit) + maildir(cmds.s + i); + else + sayit("maildir ", cmds.s + i, k - i); + else if (flagdoit) + mailfile(cmds.s + i); + else + sayit("mbox ", cmds.s + i, k - i); + break; + case '|': + ++count_program; + if (flagforwardonly) + logmsg(WHO, 111, ERROR, "Uh-oh: .qmail has prog delivery but has x bit set. (#4.7.0)"); + if (flagdoit) + mailprogram(cmds.s + i + 1); + else + sayit("program ", cmds.s + i + 1, k - i - 1); + break; + case '+': + if (str_equal(cmds.s + i + 1, "list")) flagforwardonly = 1; + break; + case '&': ++i; + default: + ++count_forward; + if (flagdoit) + recips[numforward++] = cmds.s + i; + else + sayit("forward ", cmds.s + i, k - i); + break; } i = j + 1; if (flag99) break; } - if (numforward) if (flagdoit) { - recips[numforward] = 0; - mailforward(recips); - } + if (numforward) + if (flagdoit) { + recips[numforward] = 0; + mailforward(recips); + } count_print(); _exit(0); |