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 | |
parent | 00be7622c428f279872f84569f098ce16150f8a8 (diff) |
format files
Diffstat (limited to 'src')
195 files changed, 13814 insertions, 11299 deletions
diff --git a/src/auto-gid.c b/src/auto-gid.c index c5a39df..ba386c7 100644 --- a/src/auto-gid.c +++ b/src/auto-gid.c @@ -1,17 +1,18 @@ -#include <unistd.h> -#include <sys/types.h> #include <grp.h> +#include <sys/types.h> +#include <unistd.h> + #include "buffer.h" #include "exit.h" -#include "scan.h" #include "fmt.h" +#include "scan.h" char inbuf[256]; -buffer b = BUFFER_INIT(write,1,inbuf,sizeof(inbuf)); +buffer b = BUFFER_INIT(write, 1, inbuf, sizeof(inbuf)); void outs(char *s) { - if (buffer_puts(&b,s) == -1) _exit(111); + if (buffer_puts(&b, s) == -1) _exit(111); } int main(int argc, char **argv) @@ -28,14 +29,14 @@ int main(int argc, char **argv) gr = getgrnam(value); if (!gr) { - buffer_puts(buffer_2,"fatal: unable to find group "); - buffer_puts(buffer_2,value); - buffer_puts(buffer_2,"\n"); + buffer_puts(buffer_2, "fatal: unable to find group "); + buffer_puts(buffer_2, value); + buffer_puts(buffer_2, "\n"); buffer_flush(buffer_2); _exit(111); } - strnum[fmt_ulong(strnum,(unsigned long) gr->gr_gid)] = 0; + strnum[fmt_ulong(strnum, (unsigned long)gr->gr_gid)] = 0; outs("int "); outs(name); diff --git a/src/auto-int.c b/src/auto-int.c index 58f44b2..e3a6d56 100644 --- a/src/auto-int.c +++ b/src/auto-int.c @@ -1,16 +1,16 @@ #include <unistd.h> + #include "buffer.h" -#include <unistd.h> #include "exit.h" -#include "scan.h" #include "fmt.h" +#include "scan.h" char inbuf[256]; -buffer b = BUFFER_INIT(write,1,inbuf,sizeof(inbuf)); +buffer b = BUFFER_INIT(write, 1, inbuf, sizeof(inbuf)); void out(char *s) { - if (buffer_puts(&b,s) == -1) _exit(111); + if (buffer_puts(&b, s) == -1) _exit(111); } int main(int argc, char **argv) @@ -25,8 +25,8 @@ int main(int argc, char **argv) value = argv[2]; if (!value) _exit(100); - scan_ulong(value,&num); - strnum[fmt_ulong(strnum,num)] = 0; + scan_ulong(value, &num); + strnum[fmt_ulong(strnum, num)] = 0; out("int "); out(name); diff --git a/src/auto-int8.c b/src/auto-int8.c index fd5ead6..aa0465c 100644 --- a/src/auto-int8.c +++ b/src/auto-int8.c @@ -1,15 +1,16 @@ #include <unistd.h> + #include "buffer.h" #include "exit.h" -#include "scan.h" #include "fmt.h" +#include "scan.h" char inbuf[256]; -buffer b = BUFFER_INIT(write,1,inbuf,sizeof(inbuf)); +buffer b = BUFFER_INIT(write, 1, inbuf, sizeof(inbuf)); void out(char *s) { - if (buffer_puts(&b,s) == -1) _exit(111); + if (buffer_puts(&b, s) == -1) _exit(111); } int main(int argc, char **argv) @@ -24,8 +25,8 @@ int main(int argc, char **argv) value = argv[2]; if (!value) _exit(100); - scan_8long(value,&num); - strnum[fmt_ulong(strnum,num)] = 0; + scan_8long(value, &num); + strnum[fmt_ulong(strnum, num)] = 0; out("int "); out(name); diff --git a/src/auto-str.c b/src/auto-str.c index 72e93bd..cad14ce 100644 --- a/src/auto-str.c +++ b/src/auto-str.c @@ -1,13 +1,14 @@ #include <unistd.h> + #include "buffer.h" #include "exit.h" char inbuf[BUFFER_SMALL]; -buffer b = BUFFER_INIT(write,1,inbuf,sizeof(inbuf)); +buffer b = BUFFER_INIT(write, 1, inbuf, sizeof(inbuf)); void out(char *s) { - if (buffer_puts(&b,s) == -1) _exit(111); + if (buffer_puts(&b, s) == -1) _exit(111); } int main(int argc, char **argv) @@ -29,8 +30,10 @@ int main(int argc, char **argv) while ((ch = *value++)) { out("\\"); octal[3] = 0; - octal[2] = '0' + (ch & 7); ch >>= 3; - octal[1] = '0' + (ch & 7); ch >>= 3; + octal[2] = '0' + (ch & 7); + ch >>= 3; + octal[1] = '0' + (ch & 7); + ch >>= 3; octal[0] = '0' + (ch & 7); out(octal); } diff --git a/src/auto-uid.c b/src/auto-uid.c index 21f469b..4eaaa2d 100644 --- a/src/auto-uid.c +++ b/src/auto-uid.c @@ -1,17 +1,18 @@ -#include <unistd.h> -#include <sys/types.h> #include <pwd.h> +#include <sys/types.h> +#include <unistd.h> + #include "buffer.h" #include "exit.h" -#include "scan.h" #include "fmt.h" +#include "scan.h" char inbuf[256]; -buffer b = BUFFER_INIT(write,1,inbuf,sizeof(inbuf)); +buffer b = BUFFER_INIT(write, 1, inbuf, sizeof(inbuf)); void outs(char *s) /* was named puts, but Solaris pwd.h includes stdio.h. dorks. */ { - if (buffer_puts(&b,s) == -1) _exit(111); + if (buffer_puts(&b, s) == -1) _exit(111); } int main(int argc, char **argv) @@ -28,14 +29,14 @@ int main(int argc, char **argv) pw = getpwnam(value); if (!pw) { - buffer_puts(buffer_2,"fatal: unable to find user "); - buffer_puts(buffer_2,value); - buffer_puts(buffer_2,"\n"); + buffer_puts(buffer_2, "fatal: unable to find user "); + buffer_puts(buffer_2, value); + buffer_puts(buffer_2, "\n"); buffer_flush(buffer_2); _exit(111); } - strnum[fmt_ulong(strnum,(unsigned long) pw->pw_uid)] = 0; + strnum[fmt_ulong(strnum, (unsigned long)pw->pw_uid)] = 0; outs("int "); outs(name); diff --git a/src/base64.c b/src/base64.c index fd38fe3..fca31c9 100644 --- a/src/base64.c +++ b/src/base64.c @@ -1,13 +1,13 @@ #include "base64.h" + #include "str.h" -static char *b64alpha = - "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; +static char *b64alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; #define B64PAD '=' /* returns 0 ok, 1 illegal, -1 problem */ -int b64decode(const unsigned char *in,int l,stralloc *out) +int b64decode(const unsigned char *in, int l, stralloc *out) /* stralloc *out => not null terminated */ { int p = 0; @@ -18,18 +18,18 @@ int b64decode(const unsigned char *in,int l,stralloc *out) unsigned char b[3]; if (l == 0) { - if (!stralloc_copys(out,"")) return -1; + if (!stralloc_copys(out, "")) return -1; return 0; } - while (in[l-1] == B64PAD) { - p ++; + while (in[l - 1] == B64PAD) { + p++; l--; } n = (l + p) / 4; i = (n * 3) - p; - if (!stralloc_ready(out,i)) return -1; + if (!stralloc_ready(out, i)) return -1; out->len = i; s = out->s; @@ -48,13 +48,18 @@ int b64decode(const unsigned char *in,int l,stralloc *out) x = (x << 6) + 63; else if (in[j] == '=') x = (x << 6); - else return 1; + else + return 1; } - s[2] = (unsigned char)(x & 255); x >>= 8; - s[1] = (unsigned char)(x & 255); x >>= 8; - s[0] = (unsigned char)(x & 255); x >>= 8; - s += 3; in += 4; + s[2] = (unsigned char)(x & 255); + x >>= 8; + s[1] = (unsigned char)(x & 255); + x >>= 8; + s[0] = (unsigned char)(x & 255); + x >>= 8; + s += 3; + in += 4; } x = 0; @@ -71,33 +76,35 @@ int b64decode(const unsigned char *in,int l,stralloc *out) x = (x << 6) + 63; else if (in[j] == '=') x = (x << 6); - else return 1; + else + return 1; } - b[2] = (unsigned char)(x & 255); x >>= 8; - b[1] = (unsigned char)(x & 255); x >>= 8; - b[0] = (unsigned char)(x & 255); x >>= 8; + b[2] = (unsigned char)(x & 255); + x >>= 8; + b[1] = (unsigned char)(x & 255); + x >>= 8; + b[0] = (unsigned char)(x & 255); + x >>= 8; - for (i = 0; i < 3 - p; i++) - s[i] = b[i]; + for (i = 0; i < 3 - p; i++) s[i] = b[i]; return 0; } -int b64encode(stralloc *in,stralloc *out) +int b64encode(stralloc *in, stralloc *out) { unsigned char a, b, c; int i; char *s; - if (in->len == 0) - { - if (!stralloc_copys(out,"")) return -1; + if (in->len == 0) { + if (!stralloc_copys(out, "")) return -1; return 0; } - i = in->len / 3 * 4 + 4; - if (!stralloc_ready(out,i)) return -1; + i = in->len / 3 * 4 + 4; + if (!stralloc_ready(out, i)) return -1; s = out->s; for (i = 0; i < in->len; i += 3) { @@ -106,13 +113,17 @@ int b64encode(stralloc *in,stralloc *out) c = i + 2 < in->len ? in->s[i + 2] : 0; *s++ = b64alpha[a >> 2]; - *s++ = b64alpha[((a & 3 ) << 4) | (b >> 4)]; + *s++ = b64alpha[((a & 3) << 4) | (b >> 4)]; - if (i + 1 >= in->len) *s++ = B64PAD; - else *s++ = b64alpha[((b & 0x0f) << 2) | (c >> 6)]; + if (i + 1 >= in->len) + *s++ = B64PAD; + else + *s++ = b64alpha[((b & 0x0f) << 2) | (c >> 6)]; - if (i + 2 >= in->len) *s++ = B64PAD; - else *s++ = b64alpha[c & 0x3f]; + if (i + 2 >= in->len) + *s++ = B64PAD; + else + *s++ = b64alpha[c & 0x3f]; } out->len = s - out->s; return 0; diff --git a/src/bouncesaying.c b/src/bouncesaying.c index 416d76d..3d40b69 100644 --- a/src/bouncesaying.c +++ b/src/bouncesaying.c @@ -1,38 +1,35 @@ #include <unistd.h> + +#include "exit.h" #include "logmsg.h" -#include "wait.h" #include "sig.h" -#include "exit.h" +#include "wait.h" #define WHO "bouncesaying" -int main(int argc,char **argv) +int main(int argc, char **argv) { int pid; int wstat; - if (!argv[1]) - logmsg(WHO,100,USAGE,"bouncesaying error [ program [ arg ... ] ]"); + if (!argv[1]) logmsg(WHO, 100, USAGE, "bouncesaying error [ program [ arg ... ] ]"); if (argv[2]) { pid = fork(); - if (pid == -1) - logmsg(WHO,111,FATAL,"unable to fork: "); + if (pid == -1) logmsg(WHO, 111, FATAL, "unable to fork: "); if (pid == 0) { - execvp(argv[2],argv + 2); + execvp(argv[2], argv + 2); if (errno) _exit(111); _exit(100); } - if (wait_pid(&wstat,pid) == -1) - logmsg(WHO,111,FATAL,"wait failed"); - if (wait_crashed(wstat)) - logmsg(WHO,111,FATAL,"child crashed"); + if (wait_pid(&wstat, pid) == -1) logmsg(WHO, 111, FATAL, "wait failed"); + if (wait_crashed(wstat)) logmsg(WHO, 111, FATAL, "child crashed"); switch (wait_exitcode(wstat)) { - case 0: break; - case 111: logmsg(WHO,111,FATAL,"temporary child error"); - default: _exit(0); + case 0: break; + case 111: logmsg(WHO, 111, FATAL, "temporary child error"); + default: _exit(0); } } - logmsg(WHO,100,LOG,argv[1]); + logmsg(WHO, 100, LOG, argv[1]); } diff --git a/src/chkshsgr.c b/src/chkshsgr.c index fc752bd..a98089b 100644 --- a/src/chkshsgr.c +++ b/src/chkshsgr.c @@ -1,6 +1,7 @@ #include <grp.h> #include <sys/types.h> #include <unistd.h> + #include "exit.h" int main() @@ -8,6 +9,7 @@ int main() short x[4]; x[0] = x[1] = 0; - if (getgroups(1,x) == 0) if (setgroups(1,x) == -1) _exit(1); + if (getgroups(1, x) == 0) + if (setgroups(1, x) == -1) _exit(1); _exit(0); } diff --git a/src/chkspawn.c b/src/chkspawn.c index 511489a..2023a9e 100644 --- a/src/chkspawn.c +++ b/src/chkspawn.c @@ -1,9 +1,10 @@ #include "buffer.h" +#include "exit.h" #include "fmt.h" #include "select.h" -#include "exit.h" + #include "auto_spawn.h" -#define MAXSPAWN 1000 /* Silent spawn limit increased to 1000 */ +#define MAXSPAWN 1000 /* Silent spawn limit increased to 1000 */ char num[FMT_ULONG]; fd_set fds; @@ -12,35 +13,39 @@ int main() { unsigned long hiddenlimit; unsigned long maxnumd; - + hiddenlimit = sizeof(fds) * 8; maxnumd = (hiddenlimit - 5) / 2; - + if (auto_spawn < 1) { - buffer_puts(buffer_2,"Oops. You have set conf-spawn lower than 1.\n"); + buffer_puts(buffer_2, "Oops. You have set conf-spawn lower than 1.\n"); buffer_flush(buffer_2); _exit(1); } if (auto_spawn > MAXSPAWN) { - buffer_puts(buffer_2,"Oops. You have set conf-spawn higher than MAXSPAWN.\n"); + buffer_puts(buffer_2, "Oops. You have set conf-spawn higher than MAXSPAWN.\n"); buffer_flush(buffer_2); _exit(1); } if (auto_spawn > maxnumd) { - buffer_puts(buffer_2,"Oops. Your system's FD_SET() has a hidden limit of "); - buffer_put(buffer_2,num,fmt_ulong(num,hiddenlimit)); - buffer_puts(buffer_2," descriptors.\n\ + buffer_puts(buffer_2, "Oops. Your system's FD_SET() has a hidden limit of "); + buffer_put(buffer_2, num, fmt_ulong(num, hiddenlimit)); + buffer_puts( + buffer_2, + " descriptors.\n\ This means that the qmail daemons could crash if you set the run-time\n\ concurrency higher than "); - buffer_put(buffer_2,num,fmt_ulong(num,maxnumd)); - buffer_puts(buffer_2,". So I'm going to insist that the concurrency\n\ + buffer_put(buffer_2, num, fmt_ulong(num, maxnumd)); + buffer_puts( + buffer_2, + ". So I'm going to insist that the concurrency\n\ limit in conf-spawn be at most "); - buffer_put(buffer_2,num,fmt_ulong(num,maxnumd)); - buffer_puts(buffer_2,". Right now it's "); - buffer_put(buffer_2,num,fmt_ulong(num,(unsigned long) auto_spawn)); - buffer_puts(buffer_2,".\n"); + buffer_put(buffer_2, num, fmt_ulong(num, maxnumd)); + buffer_puts(buffer_2, ". Right now it's "); + buffer_put(buffer_2, num, fmt_ulong(num, (unsigned long)auto_spawn)); + buffer_puts(buffer_2, ".\n"); buffer_flush(buffer_2); _exit(1); } diff --git a/src/columnt.c b/src/columnt.c index 9e4cf0e..638c078 100644 --- a/src/columnt.c +++ b/src/columnt.c @@ -1,21 +1,31 @@ #include <unistd.h> + #include "alloc.h" -#include "logmsg.h" #include "buffer.h" -#include "stralloc.h" #include "exit.h" +#include "logmsg.h" #include "readclose.h" +#include "stralloc.h" #define WHO "columnt" #define BSIZE 4096 char outbuf[BSIZE]; -buffer bo = BUFFER_INIT(write,1,outbuf,sizeof(outbuf)); +buffer bo = BUFFER_INIT(write, 1, outbuf, sizeof(outbuf)); -void nomem() { logmsg(WHO,111,FATAL,"out of memory"); } -void die_read() { logmsg(WHO,110,ERROR,"unable to read input: "); } -void die_write() { logmsg(WHO,110,ERROR,"unable to write output: "); } +void nomem() +{ + logmsg(WHO, 111, FATAL, "out of memory"); +} +void die_read() +{ + logmsg(WHO, 110, ERROR, "unable to read input: "); +} +void die_write() +{ + logmsg(WHO, 110, ERROR, "unable to write output: "); +} stralloc file = {0}; int *width; @@ -28,15 +38,15 @@ void nothing() void printline() { - if (buffer_put(&bo,"\n",1) == -1) die_write(); + if (buffer_put(&bo, "\n", 1) == -1) die_write(); } -void maxfield_check(int fieldnum,char *buf,int len) +void maxfield_check(int fieldnum, char *buf, int len) { if (fieldnum > maxfield) maxfield = fieldnum; } -void width_check(int fieldnum,char *buf,int len) +void width_check(int fieldnum, char *buf, int len) { if (len > width[fieldnum]) width[fieldnum] = len; } @@ -45,24 +55,23 @@ void width_init() { int i; - width = (int *) alloc((maxfield + 1) * sizeof(int)); + width = (int *)alloc((maxfield + 1) * sizeof(int)); if (!width) nomem(); - for (i = 0; i <= maxfield; ++i) - width[i] = 0; + for (i = 0; i <= maxfield; ++i) width[i] = 0; } -void printfield(int fieldnum,char *buf,int len) +void printfield(int fieldnum, char *buf, int len) { int i; if (fieldnum < maxfield) for (i = len; i < width[fieldnum]; ++i) - if (buffer_put(&bo," ",1) == -1) die_write(); + if (buffer_put(&bo, " ", 1) == -1) die_write(); - if (buffer_put(&bo,buf,len) == -1) die_write(); + if (buffer_put(&bo, buf, len) == -1) die_write(); if (fieldnum < maxfield) - if (buffer_put(&bo," ",2) == -1) die_write(); + if (buffer_put(&bo, " ", 2) == -1) die_write(); } void split(void (*dofield)(), void (*doline)()) @@ -80,7 +89,7 @@ void split(void (*dofield)(), void (*doline)()) if (i == j) break; fieldpos = i; while ((file.s[i] != ' ') && (file.s[i] != '\t') && (file.s[i] != '\n')) ++i; - dofield(fieldnum++,file.s + fieldpos,i - fieldpos); + dofield(fieldnum++, file.s + fieldpos, i - fieldpos); } doline(); i = j + 1; @@ -89,15 +98,15 @@ void split(void (*dofield)(), void (*doline)()) int main() { - if (readclose_append(0,&file,BSIZE) == -1) die_read(); + if (readclose_append(0, &file, BSIZE) == -1) die_read(); if (!file.len) _exit(0); if (file.s[file.len - 1] != '\n') - if (!stralloc_append(&file,"\n")) nomem(); + if (!stralloc_append(&file, "\n")) nomem(); - split(maxfield_check,nothing); + split(maxfield_check, nothing); width_init(); - split(width_check,nothing); - split(printfield,printline); + split(width_check, nothing); + split(printfield, printline); if (buffer_flush(&bo) == -1) die_write(); _exit(0); diff --git a/src/commands.c b/src/commands.c index 8602f7c..8cb81de 100644 --- a/src/commands.c +++ b/src/commands.c @@ -1,38 +1,40 @@ #include "commands.h" + #include "buffer.h" -#include "stralloc.h" -#include "str.h" #include "case.h" +#include "str.h" +#include "stralloc.h" static stralloc cmd = {0}; -int commands(buffer *b,struct commands *c) +int commands(buffer *b, struct commands *c) { int i; char *arg; for (;;) { - if (!stralloc_copys(&cmd,"")) return -1; + if (!stralloc_copys(&cmd, "")) return -1; for (;;) { - if (!stralloc_readyplus(&cmd,1)) return -1; - i = buffer_get(b,cmd.s + cmd.len,1); + if (!stralloc_readyplus(&cmd, 1)) return -1; + i = buffer_get(b, cmd.s + cmd.len, 1); if (i != 1) return i; if (cmd.s[cmd.len] == '\n') break; ++cmd.len; } - if (cmd.len > 0) if (cmd.s[cmd.len - 1] == '\r') --cmd.len; + if (cmd.len > 0) + if (cmd.s[cmd.len - 1] == '\r') --cmd.len; cmd.s[cmd.len] = 0; - i = str_chr(cmd.s,' '); + i = str_chr(cmd.s, ' '); arg = cmd.s + i; while (*arg == ' ') ++arg; cmd.s[i] = 0; - for (i = 0; c[i].text; ++i) - if (case_equals(c[i].text,cmd.s)) break; + for (i = 0; c[i].text; ++i) + if (case_equals(c[i].text, cmd.s)) break; c[i].fun(arg); if (c[i].flush) c[i].flush(); diff --git a/src/condredirect.c b/src/condredirect.c index 6f2d89a..534dac7 100644 --- a/src/condredirect.c +++ b/src/condredirect.c @@ -1,81 +1,76 @@ #include <unistd.h> -#include "sig.h" -#include "exit.h" + +#include "buffer.h" #include "env.h" +#include "exit.h" +#include "fmt.h" #include "logmsg.h" -#include "wait.h" #include "seek.h" +#include "sig.h" +#include "wait.h" + #include "qmail.h" -#include "buffer.h" -#include "fmt.h" #define WHO "condredirect" struct qmail qqt; -ssize_t mywrite(int fd,char *buf,int len) +ssize_t mywrite(int fd, char *buf, int len) { - qmail_put(&qqt,buf,len); + qmail_put(&qqt, buf, len); return len; } char inbuf[BUFFER_INSIZE]; -buffer bi = BUFFER_INIT(read,0,inbuf,sizeof(inbuf)); +buffer bi = BUFFER_INIT(read, 0, inbuf, sizeof(inbuf)); char outbuf[1]; -buffer bo = BUFFER_INIT(mywrite,-1,outbuf,sizeof(outbuf)); +buffer bo = BUFFER_INIT(mywrite, -1, outbuf, sizeof(outbuf)); char num[FMT_ULONG]; -int main(int argc,char **argv) +int main(int argc, char **argv) { char *sender; char *dtline; int pid; int wstat; char *qqx; - - if (!argv[1] || !argv[2]) - logmsg(WHO,100,USAGE,"condredirect newaddress program [ arg ... ]"); - + + if (!argv[1] || !argv[2]) logmsg(WHO, 100, USAGE, "condredirect newaddress program [ arg ... ]"); + pid = fork(); - if (pid == -1) - logmsg(WHO,111,FATAL,"unable to fork: "); + if (pid == -1) logmsg(WHO, 111, FATAL, "unable to fork: "); if (pid == 0) { - execvp(argv[2],argv + 2); + execvp(argv[2], argv + 2); if (errno) _exit(111); _exit(100); } - if (wait_pid(&wstat,pid) == -1) - logmsg(WHO,111,FATAL,"wait failed"); - if (wait_crashed(wstat)) - logmsg(WHO,111,FATAL,"child crashed"); + if (wait_pid(&wstat, pid) == -1) logmsg(WHO, 111, FATAL, "wait failed"); + if (wait_crashed(wstat)) logmsg(WHO, 111, FATAL, "child crashed"); switch (wait_exitcode(wstat)) { - case 0: break; - case 111: logmsg(WHO,111,FATAL,"temporary child error"); - default: _exit(0); + case 0: break; + case 111: logmsg(WHO, 111, FATAL, "temporary child error"); + default: _exit(0); } - if (seek_begin(0) == -1) - logmsg(WHO,111,FATAL,"unable to rewind: "); + if (seek_begin(0) == -1) logmsg(WHO, 111, FATAL, "unable to rewind: "); sig_pipeignore(); - + sender = env_get("SENDER"); - if (!sender) logmsg(WHO,100,ERROR,"SENDER not set"); + if (!sender) logmsg(WHO, 100, ERROR, "SENDER not set"); dtline = env_get("DTLINE"); - if (!dtline) logmsg(WHO,100,ERROR,"DTLINE not set"); - - if (qmail_open(&qqt) == -1) - logmsg(WHO,111,FATAL,"unable to fork: "); - qmail_puts(&qqt,dtline); - if (buffer_copy(&bo,&bi) != 0) - logmsg(WHO,111,FATAL,"unable to read message: "); + if (!dtline) logmsg(WHO, 100, ERROR, "DTLINE not set"); + + if (qmail_open(&qqt) == -1) logmsg(WHO, 111, FATAL, "unable to fork: "); + qmail_puts(&qqt, dtline); + if (buffer_copy(&bo, &bi) != 0) logmsg(WHO, 111, FATAL, "unable to read message: "); buffer_flush(&bo); - - num[fmt_ulong(num,qmail_qp(&qqt))] = 0; - qmail_from(&qqt,sender); - qmail_to(&qqt,argv[1]); + num[fmt_ulong(num, qmail_qp(&qqt))] = 0; + + qmail_from(&qqt, sender); + qmail_to(&qqt, argv[1]); qqx = qmail_close(&qqt); - if (*qqx) logmsg(WHO,*qqx == 'D' ? 100 : 111,FATAL,qqx + 1); - logmsg(WHO,0,LOG,B("qp ",num)); + if (*qqx) logmsg(WHO, *qqx == 'D' ? 100 : 111, FATAL, qqx + 1); + logmsg(WHO, 0, LOG, B("qp ", num)); } diff --git a/src/constmap.c b/src/constmap.c index ea153ea..decc42e 100644 --- a/src/constmap.c +++ b/src/constmap.c @@ -1,8 +1,9 @@ #include "constmap.h" + #include "alloc.h" #include "case.h" -static constmap_hash hash(char *s,int len) +static constmap_hash hash(char *s, int len) { unsigned char ch; constmap_hash h; @@ -16,49 +17,48 @@ static constmap_hash hash(char *s,int len) return h; } -char *constmap(struct constmap *cm,char *s,int len) +char *constmap(struct constmap *cm, char *s, int len) { constmap_hash h; int pos; - h = hash(s,len); + h = hash(s, len); pos = cm->first[h & cm->mask]; while (pos != -1) { if (h == cm->hash[pos]) if (len == cm->inputlen[pos]) - if (!case_diffb(cm->input[pos],len,s)) - return cm->input[pos] + cm->inputlen[pos] + 1; + if (!case_diffb(cm->input[pos], len, s)) return cm->input[pos] + cm->inputlen[pos] + 1; pos = cm->next[pos]; } return 0; } -int constmap_init(struct constmap *cm,char *s,int len,int flagcolon) +int constmap_init(struct constmap *cm, char *s, int len, int flagcolon) { int i; int j; int k; int pos; constmap_hash h; - + cm->num = 0; - for (j = 0; j < len; ++j) if (!s[j]) ++cm->num; - + for (j = 0; j < len; ++j) + if (!s[j]) ++cm->num; + h = 64; while (h && (h < cm->num)) h += h; cm->mask = h - 1; - - cm->first = (int *) alloc(sizeof(int) * h); + + cm->first = (int *)alloc(sizeof(int) * h); if (cm->first) { - cm->input = (char **) alloc(sizeof(char *) * cm->num); + cm->input = (char **)alloc(sizeof(char *) * cm->num); if (cm->input) { - cm->inputlen = (int *) alloc(sizeof(int) * cm->num); + cm->inputlen = (int *)alloc(sizeof(int) * cm->num); if (cm->inputlen) { - cm->hash = (constmap_hash *) alloc(sizeof(constmap_hash) * cm->num); + cm->hash = (constmap_hash *)alloc(sizeof(constmap_hash) * cm->num); if (cm->hash) { - cm->next = (int *) alloc(sizeof(int) * cm->num); + cm->next = (int *)alloc(sizeof(int) * cm->num); if (cm->next) { - for (h = 0; h <= cm->mask; ++h) - cm->first[h] = -1; + for (h = 0; h <= cm->mask; ++h) cm->first[h] = -1; pos = 0; i = 0; for (j = 0; j < len; ++j) @@ -67,12 +67,15 @@ int constmap_init(struct constmap *cm,char *s,int len,int flagcolon) if (flagcolon) { for (k = i; k < j; ++k) if (s[k] == ':') break; - if (k >= j) { i = j + 1; continue; } + if (k >= j) { + i = j + 1; + continue; + } k -= i; } cm->input[pos] = s + i; cm->inputlen[pos] = k; - h = hash(s + i,k); + h = hash(s + i, k); cm->hash[pos] = h; h &= cm->mask; cm->next[pos] = cm->first[h]; @@ -93,7 +96,7 @@ int constmap_init(struct constmap *cm,char *s,int len,int flagcolon) return 0; } -int constmap_init_char(struct constmap *cm,char *s,int len,int flagcolon,char flagchar) +int constmap_init_char(struct constmap *cm, char *s, int len, int flagcolon, char flagchar) { int i; int j; @@ -106,24 +109,24 @@ int constmap_init_char(struct constmap *cm,char *s,int len,int flagcolon,char fl } cm->num = 0; - for (j = 0; j < len; ++j) if (!s[j]) ++cm->num; + for (j = 0; j < len; ++j) + if (!s[j]) ++cm->num; h = 64; while (h && (h < cm->num)) h += h; cm->mask = h - 1; - cm->first = (int *) alloc(sizeof(int) * h); + cm->first = (int *)alloc(sizeof(int) * h); if (cm->first) { - cm->input = (char **) alloc(sizeof(char *) * cm->num); + cm->input = (char **)alloc(sizeof(char *) * cm->num); if (cm->input) { - cm->inputlen = (int *) alloc(sizeof(int) * cm->num); + cm->inputlen = (int *)alloc(sizeof(int) * cm->num); if (cm->inputlen) { - cm->hash = (constmap_hash *) alloc(sizeof(constmap_hash) * cm->num); + cm->hash = (constmap_hash *)alloc(sizeof(constmap_hash) * cm->num); if (cm->hash) { - cm->next = (int *) alloc(sizeof(int) * cm->num); + cm->next = (int *)alloc(sizeof(int) * cm->num); if (cm->next) { - for (h = 0; h <= cm->mask; ++h) - cm->first[h] = -1; + for (h = 0; h <= cm->mask; ++h) cm->first[h] = -1; pos = 0; i = 0; for (j = 0; j < len; ++j) @@ -132,12 +135,15 @@ int constmap_init_char(struct constmap *cm,char *s,int len,int flagcolon,char fl if (flagcolon) { for (k = i; k < j; ++k) if (s[k] == flagchar) break; - if (k >= j) { i = j + 1; continue; } + if (k >= j) { + i = j + 1; + continue; + } k -= i; } cm->input[pos] = s + i; cm->inputlen[pos] = k; - h = hash(s + i,k); + h = hash(s + i, k); cm->hash[pos] = h; h &= cm->mask; cm->next[pos] = cm->first[h]; @@ -145,9 +151,9 @@ int constmap_init_char(struct constmap *cm,char *s,int len,int flagcolon,char fl ++pos; i = j + 1; } - return 1; - } - alloc_free(cm->hash); + return 1; + } + alloc_free(cm->hash); } alloc_free(cm->inputlen); } diff --git a/src/control.c b/src/control.c index 2558225..a40030d 100644 --- a/src/control.c +++ b/src/control.c @@ -1,13 +1,15 @@ +#include "control.h" + #include <unistd.h> -#include "open.h" -#include "getln.h" -#include "stralloc.h" + +#include "alloc.h" #include "buffer.h" +#include "error.h" +#include "getln.h" #include "logmsg.h" -#include "control.h" -#include "alloc.h" +#include "open.h" #include "scan.h" -#include "error.h" +#include "stralloc.h" static char inbuf[2048]; static stralloc line = {0}; @@ -19,13 +21,12 @@ static int meok = 0; static void striptrailingwhitespace(stralloc *sa) { - while (sa->len > 0) - switch (sa->s[sa->len - 1]) { - case '\n': case ' ': case '\t': - --sa->len; - break; - default: - return; + while (sa->len > 0) switch (sa->s[sa->len - 1]) + { + case '\n': + case ' ': + case '\t': --sa->len; break; + default: return; } } @@ -33,34 +34,41 @@ int control_init(void) { int r; - r = control_readline(&me,"control/me"); + r = control_readline(&me, "control/me"); if (r == 1) meok = 1; return r; } -int control_rldef(stralloc *sa,char *fn,int flagme,char *def) +int control_rldef(stralloc *sa, char *fn, int flagme, char *def) { int r; - r = control_readline(sa,fn); + r = control_readline(sa, fn); if (r) return r; - if (flagme) if (meok) return stralloc_copy(sa,&me) ? 1 : -1; - if (def) return stralloc_copys(sa,def) ? 1 : -1; + if (flagme) + if (meok) return stralloc_copy(sa, &me) ? 1 : -1; + if (def) return stralloc_copys(sa, def) ? 1 : -1; return r; } -int control_readline(stralloc *sa,char *fn) +int control_readline(stralloc *sa, char *fn) { buffer b; int fd; int match; fd = open_read(fn); - if (fd == -1) { if (errno == ENOENT) return 0; return -1; } - - buffer_init(&b,read,fd,inbuf,sizeof(inbuf)); + if (fd == -1) { + if (errno == ENOENT) return 0; + return -1; + } - if (getln(&b,sa,&match,'\n') == -1) { close(fd); return -1; } + buffer_init(&b, read, fd, inbuf, sizeof(inbuf)); + + if (getln(&b, sa, &match, '\n') == -1) { + close(fd); + return -1; + } striptrailingwhitespace(sa); @@ -68,34 +76,34 @@ int control_readline(stralloc *sa,char *fn) return 1; } -int control_readint(int *i,char *fn) +int control_readint(int *i, char *fn) { unsigned long u; - switch (control_readline(&line,fn)) { - case 0: return 0; + switch (control_readline(&line, fn)) { + case 0: return 0; case -1: return -1; } if (!stralloc_0(&line)) return -1; - if (!scan_ulong(line.s,&u)) return 0; + if (!scan_ulong(line.s, &u)) return 0; *i = u; return 1; } -int control_readfile(stralloc *sa,char *fn,int flagme) +int control_readfile(stralloc *sa, char *fn, int flagme) { buffer b; int fd; int match; - if (!stralloc_copys(sa,"")) return -1; + if (!stralloc_copys(sa, "")) return -1; fd = open_read(fn); if (fd == -1) { if (errno == ENOENT) { if (flagme && meok) { - if (!stralloc_copy(sa,&me)) return -1; + if (!stralloc_copy(sa, &me)) return -1; if (!stralloc_0(sa)) return -1; return 1; } @@ -104,17 +112,23 @@ int control_readfile(stralloc *sa,char *fn,int flagme) return -1; } - buffer_init(&b,read,fd,inbuf,sizeof(inbuf)); + buffer_init(&b, read, fd, inbuf, sizeof(inbuf)); for (;;) { - if (getln(&b,&line,&match,'\n') == -1) break; - if (!match && !line.len) { close(fd); return 1; } + if (getln(&b, &line, &match, '\n') == -1) break; + if (!match && !line.len) { + close(fd); + return 1; + } striptrailingwhitespace(&line); if (!stralloc_0(&line)) break; if (line.s[0]) if (line.s[0] != '#') - if (!stralloc_cat(sa,&line)) break; - if (!match) { close(fd); return 1; } + if (!stralloc_cat(sa, &line)) break; + if (!match) { + close(fd); + return 1; + } } close(fd); diff --git a/src/date822fmt.c b/src/date822fmt.c index fc2d1f7..7e0333a 100644 --- a/src/date822fmt.c +++ b/src/date822fmt.c @@ -1,29 +1,54 @@ -#include "datetime.h" -#include "fmt.h" #include "date822fmt.h" +#include "fmt.h" + +#include "datetime.h" + static char *montab[12] = { -"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec" -}; + "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; -unsigned int date822fmt(char *s,struct datetime *dt) +unsigned int date822fmt(char *s, struct datetime *dt) { unsigned int i; unsigned int len; len = 0; - i = fmt_uint(s,dt->mday); len += i; if (s) s += i; - i = fmt_str(s," "); len += i; if (s) s += i; - i = fmt_str(s,montab[dt->mon]); len += i; if (s) s += i; - i = fmt_str(s," "); len += i; if (s) s += i; - i = fmt_uint(s,dt->year + 1900); len += i; if (s) s += i; - i = fmt_str(s," "); len += i; if (s) s += i; - i = fmt_uint0(s,dt->hour,2); len += i; if (s) s += i; - i = fmt_str(s,":"); len += i; if (s) s += i; - i = fmt_uint0(s,dt->min,2); len += i; if (s) s += i; - i = fmt_str(s,":"); len += i; if (s) s += i; - i = fmt_uint0(s,dt->sec,2); len += i; if (s) s += i; - i = fmt_str(s," -0000\n"); len += i; if (s) s += i; + i = fmt_uint(s, dt->mday); + len += i; + if (s) s += i; + i = fmt_str(s, " "); + len += i; + if (s) s += i; + i = fmt_str(s, montab[dt->mon]); + len += i; + if (s) s += i; + i = fmt_str(s, " "); + len += i; + if (s) s += i; + i = fmt_uint(s, dt->year + 1900); + len += i; + if (s) s += i; + i = fmt_str(s, " "); + len += i; + if (s) s += i; + i = fmt_uint0(s, dt->hour, 2); + len += i; + if (s) s += i; + i = fmt_str(s, ":"); + len += i; + if (s) s += i; + i = fmt_uint0(s, dt->min, 2); + len += i; + if (s) s += i; + i = fmt_str(s, ":"); + len += i; + if (s) s += i; + i = fmt_uint0(s, dt->sec, 2); + len += i; + if (s) s += i; + i = fmt_str(s, " -0000\n"); + len += i; + if (s) s += i; return len; } diff --git a/src/datetime.c b/src/datetime.c index 3db5f7f..5f5d9bf 100644 --- a/src/datetime.c +++ b/src/datetime.c @@ -1,7 +1,7 @@ /* 19950925 */ #include "datetime.h" -void datetime_tai(struct datetime *dt,datetime_sec t) +void datetime_tai(struct datetime *dt, datetime_sec t) { int day; int tod; @@ -9,43 +9,67 @@ void datetime_tai(struct datetime *dt,datetime_sec t) int yday; int wday; int mon; - + tod = t % 86400; day = t / 86400; - if (tod < 0) { tod += 86400; --day; } - + if (tod < 0) { + tod += 86400; + --day; + } + dt->hour = tod / 3600; tod %= 3600; dt->min = tod / 60; dt->sec = tod % 60; - - wday = (day + 4) % 7; if (wday < 0) wday += 7; + + wday = (day + 4) % 7; + if (wday < 0) wday += 7; dt->wday = wday; - + day -= 11017; /* day 0 is march 1, 2000 */ year = 5 + day / 146097; - day = day % 146097; if (day < 0) { day += 146097; --year; } + day = day % 146097; + if (day < 0) { + day += 146097; + --year; + } /* from now on, day is nonnegative */ year *= 4; - if (day == 146096) { year += 3; day = 36524; } - else { year += day / 36524; day %= 36524; } + if (day == 146096) { + year += 3; + day = 36524; + } else { + year += day / 36524; + day %= 36524; + } year *= 25; year += day / 1461; day %= 1461; year *= 4; yday = (day < 306); - if (day == 1460) { year += 3; day = 365; } - else { year += day / 365; day %= 365; } + if (day == 1460) { + year += 3; + day = 365; + } else { + year += day / 365; + day %= 365; + } yday += day; - + day *= 10; mon = (day + 5) / 306; day = day + 5 - 306 * mon; day /= 10; - if (mon >= 10) { yday -= 306; ++year; mon -= 10; } - else { yday += 59; mon += 2; } - + if (mon >= 10) { + yday -= 306; + ++year; + mon -= 10; + } else { + yday += 59; + mon += 2; + } + dt->yday = yday; dt->year = year - 1900; dt->mon = mon; diff --git a/src/datetime_un.c b/src/datetime_un.c index e84806d..f8d24f2 100644 --- a/src/datetime_un.c +++ b/src/datetime_un.c @@ -7,29 +7,41 @@ datetime_sec datetime_untai(struct datetime *dt) int year; int day; int mon; - + year = dt->year + 1900; - + mon = dt->mon; - if (mon >= 2) { mon -= 2; } - else { mon += 10; --year; } - + if (mon >= 2) { + mon -= 2; + } else { + mon += 10; + --year; + } + day = (dt->mday - 1) * 10 + 5 + 306 * mon; day /= 10; - - if (day == 365) { year -= 3; day = 1460; } - else { day += 365 * (year % 4); } + + if (day == 365) { + year -= 3; + day = 1460; + } else { + day += 365 * (year % 4); + } year /= 4; - + day += 1461 * (year % 25); year /= 25; - - if (day == 36524) { year -= 3; day = 146096; } - else { day += 36524 * (year % 4); } + + if (day == 36524) { + year -= 3; + day = 146096; + } else { + day += 36524 * (year % 4); + } year /= 4; - + day += 146097 * (year - 5); day += 11017; - + return ((day * 24 + dt->hour) * 60 + dt->min) * 60 + dt->sec; } diff --git a/src/dkim.cpp b/src/dkim.cpp index 8f36644..1ce8eee 100644 --- a/src/dkim.cpp +++ b/src/dkim.cpp @@ -19,72 +19,78 @@ * Changes done by ¢feh@fehcom.de obeying the above license * *****************************************************************************/ -#include <string.h> #include "dkim.h" + +#include <string.h> + #include "dkimsign.h" #include "dkimverify.h" -#define DKIMID ('D' | 'K'<<8 | 'I'<<16 | 'M'<<24) +#define DKIMID ('D' | 'K' << 8 | 'I' << 16 | 'M' << 24) /* taken from removed file "ressource.h" */ #ifdef VERSION -#define VERSION_STRING VERSION + #define VERSION_STRING VERSION #else -#define VERSION_STRING "1.4.0" + #define VERSION_STRING "1.4.0" #endif -static void InitContext(DKIMContext* pContext,bool bSign,void* pObject) +static void InitContext(DKIMContext *pContext, bool bSign, void *pObject) { pContext->reserved1 = DKIMID; pContext->reserved2 = bSign ? 1 : 0; pContext->reserved3 = pObject; } -static void* ValidateContext(DKIMContext* pContext,bool bSign) +static void *ValidateContext(DKIMContext *pContext, bool bSign) { - if (pContext->reserved1 != DKIMID) - return NULL; + if (pContext->reserved1 != DKIMID) return NULL; - if (pContext->reserved2 != (unsigned int)(bSign ? 1 : 0)) - return NULL; + if (pContext->reserved2 != (unsigned int)(bSign ? 1 : 0)) return NULL; return pContext->reserved3; } -int DKIM_CALL DKIMSignInit(DKIMContext* pSignContext,DKIMSignOptions* pOptions) +int DKIM_CALL DKIMSignInit(DKIMContext *pSignContext, DKIMSignOptions *pOptions) { int nRet = DKIM_OUT_OF_MEMORY; - CDKIMSign* pSign = new CDKIMSign; + CDKIMSign *pSign = new CDKIMSign; if (pSign) { nRet = pSign->Init(pOptions); - if (nRet != DKIM_SUCCESS) - delete pSign; + if (nRet != DKIM_SUCCESS) delete pSign; } - if (nRet == DKIM_SUCCESS) { InitContext(pSignContext,true,pSign); } + if (nRet == DKIM_SUCCESS) { + InitContext(pSignContext, true, pSign); + } return nRet; } -int DKIM_CALL DKIMSignProcess(DKIMContext* pSignContext,char* szBuffer,int nBufLength) +int DKIM_CALL DKIMSignProcess(DKIMContext *pSignContext, char *szBuffer, int nBufLength) { - CDKIMSign* pSign = (CDKIMSign*)ValidateContext(pSignContext,true); + CDKIMSign *pSign = (CDKIMSign *)ValidateContext(pSignContext, true); - if (pSign) { return pSign->Process(szBuffer,nBufLength,false); } + if (pSign) { + return pSign->Process(szBuffer, nBufLength, false); + } return DKIM_INVALID_CONTEXT; } -int DKIM_CALL DKIMSignGetSig2(DKIMContext* pSignContext,char* szRSAPrivKey,char* szECCPrivKey,char** pszSignature) +int DKIM_CALL DKIMSignGetSig2( + DKIMContext *pSignContext, char *szRSAPrivKey, char *szECCPrivKey, char **pszSignature) { - CDKIMSign* pSign = (CDKIMSign*)ValidateContext(pSignContext,true); + CDKIMSign *pSign = (CDKIMSign *)ValidateContext(pSignContext, true); - if (pSign) { return pSign->GetSig2(szRSAPrivKey,szECCPrivKey,pszSignature); } + if (pSign) { + return pSign->GetSig2(szRSAPrivKey, szECCPrivKey, pszSignature); + } return DKIM_INVALID_CONTEXT; } -void DKIM_CALL DKIMSignFree(DKIMContext* pSignContext) +void DKIM_CALL DKIMSignFree(DKIMContext *pSignContext) { - CDKIMSign* pSign = (CDKIMSign*)ValidateContext(pSignContext,true); + CDKIMSign *pSign = (CDKIMSign *)ValidateContext(pSignContext, true); if (pSign) { delete pSign; @@ -92,65 +98,65 @@ void DKIM_CALL DKIMSignFree(DKIMContext* pSignContext) } } -int DKIM_CALL DKIMVerifyInit(DKIMContext* pVerifyContext,DKIMVerifyOptions* pOptions) +int DKIM_CALL DKIMVerifyInit(DKIMContext *pVerifyContext, DKIMVerifyOptions *pOptions) { int nRet = DKIM_OUT_OF_MEMORY; - CDKIMVerify* pVerify = new CDKIMVerify; + CDKIMVerify *pVerify = new CDKIMVerify; if (pVerify) { nRet = pVerify->Init(pOptions); - if (nRet != DKIM_SUCCESS) - delete pVerify; + if (nRet != DKIM_SUCCESS) delete pVerify; } if (nRet == DKIM_SUCCESS) { - InitContext(pVerifyContext,false,pVerify); + InitContext(pVerifyContext, false, pVerify); } return nRet; } -int DKIM_CALL DKIMVerifyProcess(DKIMContext* pVerifyContext,const char* const szBuffer,int nBufLength) +int DKIM_CALL DKIMVerifyProcess(DKIMContext *pVerifyContext, const char *const szBuffer, int nBufLength) { - CDKIMVerify* pVerify = (CDKIMVerify*)ValidateContext(pVerifyContext,false); + CDKIMVerify *pVerify = (CDKIMVerify *)ValidateContext(pVerifyContext, false); if (pVerify) { - return pVerify->Process(szBuffer,nBufLength,false); + return pVerify->Process(szBuffer, nBufLength, false); } - + return DKIM_INVALID_CONTEXT; } -int DKIM_CALL DKIMVerifyResults(DKIMContext* pVerifyContext) +int DKIM_CALL DKIMVerifyResults(DKIMContext *pVerifyContext) { - CDKIMVerify* pVerify = (CDKIMVerify*)ValidateContext(pVerifyContext,false); + CDKIMVerify *pVerify = (CDKIMVerify *)ValidateContext(pVerifyContext, false); - if (pVerify) { - return pVerify->GetResults(); + if (pVerify) { + return pVerify->GetResults(); } return DKIM_INVALID_CONTEXT; } -int DKIM_CALL DKIMVerifyGetDetails(DKIMContext* pVerifyContext,int* nSigCount,DKIMVerifyDetails** pDetails,char* szPractices) +int DKIM_CALL DKIMVerifyGetDetails( + DKIMContext *pVerifyContext, int *nSigCount, DKIMVerifyDetails **pDetails, char *szPractices) { szPractices[0] = '\0'; - CDKIMVerify* pVerify = (CDKIMVerify*)ValidateContext(pVerifyContext,false); + CDKIMVerify *pVerify = (CDKIMVerify *)ValidateContext(pVerifyContext, false); if (pVerify) { - strcpy(szPractices,pVerify->GetPractices()); - return pVerify->GetDetails(nSigCount,pDetails); + strcpy(szPractices, pVerify->GetPractices()); + return pVerify->GetDetails(nSigCount, pDetails); } return DKIM_INVALID_CONTEXT; } -void DKIM_CALL DKIMVerifyFree(DKIMContext* pVerifyContext) +void DKIM_CALL DKIMVerifyFree(DKIMContext *pVerifyContext) { - CDKIMVerify* pVerify = (CDKIMVerify*)ValidateContext(pVerifyContext,false); + CDKIMVerify *pVerify = (CDKIMVerify *)ValidateContext(pVerifyContext, false); if (pVerify) { delete pVerify; @@ -158,40 +164,41 @@ void DKIM_CALL DKIMVerifyFree(DKIMContext* pVerifyContext) } } -const char* DKIM_CALL DKIMVersion() +const char *DKIM_CALL DKIMVersion() { return VERSION_STRING; } -static const char* DKIMErrorStrings[-1-DKIM_MAX_ERROR] = { - "DKIM_FAIL", - "DKIM_BAD_SYNTAX", - "DKIM_SIGNATURE_BAD", - "DKIM_SIGNATURE_BAD_BUT_TESTING", - "DKIM_SIGNATURE_EXPIRED", - "DKIM_SELECTOR_INVALID", - "DKIM_SELECTOR_GRANULARITY_MISMATCH", - "DKIM_SELECTOR_KEY_REVOKED", - "DKIM_SELECTOR_DOMAIN_NAME_TOO_LONG", - "DKIM_SELECTOR_DNS_TEMP_FAILURE", - "DKIM_SELECTOR_DNS_PERM_FAILURE", - "DKIM_SELECTOR_PUBLIC_KEY_INVALID", - "DKIM_NO_SIGNATURES", - "DKIM_NO_VALID_SIGNATURES", - "DKIM_BODY_HASH_MISMATCH", - "DKIM_SELECTOR_ALGORITHM_MISMATCH", - "DKIM_STAT_INCOMPAT", - "DKIM_UNSIGNED_FROM", - "DKIM_OUT_OF_MEMORY", - "DKIM_INVALID_CONTEXT", - "DKIM_NO_SENDER", - "DKIM_BAD_PRIVATE_KEY", - "DKIM_BUFFER_TOO_SMALL", +static const char *DKIMErrorStrings[-1 - DKIM_MAX_ERROR] = { + "DKIM_FAIL", + "DKIM_BAD_SYNTAX", + "DKIM_SIGNATURE_BAD", + "DKIM_SIGNATURE_BAD_BUT_TESTING", + "DKIM_SIGNATURE_EXPIRED", + "DKIM_SELECTOR_INVALID", + "DKIM_SELECTOR_GRANULARITY_MISMATCH", + "DKIM_SELECTOR_KEY_REVOKED", + "DKIM_SELECTOR_DOMAIN_NAME_TOO_LONG", + "DKIM_SELECTOR_DNS_TEMP_FAILURE", + "DKIM_SELECTOR_DNS_PERM_FAILURE", + "DKIM_SELECTOR_PUBLIC_KEY_INVALID", + "DKIM_NO_SIGNATURES", + "DKIM_NO_VALID_SIGNATURES", + "DKIM_BODY_HASH_MISMATCH", + "DKIM_SELECTOR_ALGORITHM_MISMATCH", + "DKIM_STAT_INCOMPAT", + "DKIM_UNSIGNED_FROM", + "DKIM_OUT_OF_MEMORY", + "DKIM_INVALID_CONTEXT", + "DKIM_NO_SENDER", + "DKIM_BAD_PRIVATE_KEY", + "DKIM_BUFFER_TOO_SMALL", }; -const char* DKIM_CALL DKIMGetErrorString(int ErrorCode) { +const char *DKIM_CALL DKIMGetErrorString(int ErrorCode) +{ if (ErrorCode >= 0 || ErrorCode <= DKIM_MAX_ERROR) return "Unknown"; else - return DKIMErrorStrings[-1-ErrorCode]; + return DKIMErrorStrings[-1 - ErrorCode]; } diff --git a/src/dkimbase.cpp b/src/dkimbase.cpp index f6abf45..c27e767 100644 --- a/src/dkimbase.cpp +++ b/src/dkimbase.cpp @@ -19,10 +19,13 @@ * Changes done by ¢feh@fehcom.de obeying the above license * *****************************************************************************/ +#include "dkimbase.h" + #include <string.h> + #include <algorithm> + #include "dkim.h" -#include "dkimbase.h" CDKIMBase::CDKIMBase() @@ -52,11 +55,11 @@ int CDKIMBase::Init(void) } //////////////////////////////////////////////////////////////////////////////// -// +// // Alloc - allocate buffer // //////////////////////////////////////////////////////////////////////////////// -int CDKIMBase::Alloc(char*& szBuffer,int nRequiredSize) +int CDKIMBase::Alloc(char *& szBuffer, int nRequiredSize) { szBuffer = new char[nRequiredSize]; @@ -64,25 +67,25 @@ int CDKIMBase::Alloc(char*& szBuffer,int nRequiredSize) } //////////////////////////////////////////////////////////////////////////////// -// +// // ReAlloc - extend buffer if necessary, leaving room for future expansion // //////////////////////////////////////////////////////////////////////////////// -int CDKIMBase::ReAlloc(char*& szBuffer,int& nBufferSize,int nRequiredSize) +int CDKIMBase::ReAlloc(char *& szBuffer, int& nBufferSize, int nRequiredSize) { if (nRequiredSize > nBufferSize) { - char* newp; - int nNewSize = nRequiredSize + BUFFER_ALLOC_INCREMENT; + char *newp; + int nNewSize = nRequiredSize + BUFFER_ALLOC_INCREMENT; - if (Alloc(newp,nNewSize) == DKIM_SUCCESS) { + if (Alloc(newp, nNewSize) == DKIM_SUCCESS) { if (szBuffer != NULL && nBufferSize > 0) { - memcpy(newp,szBuffer,nBufferSize); + memcpy(newp, szBuffer, nBufferSize); delete[] szBuffer; } szBuffer = newp; - nBufferSize = nNewSize; + nBufferSize = nNewSize; } else { - return DKIM_OUT_OF_MEMORY; // memory alloc error! + return DKIM_OUT_OF_MEMORY; // memory alloc error! } } @@ -90,30 +93,30 @@ int CDKIMBase::ReAlloc(char*& szBuffer,int& nBufferSize,int nRequiredSize) } //////////////////////////////////////////////////////////////////////////////// -// +// // Process - split buffers into lines without any CRs or LFs at the end. // //////////////////////////////////////////////////////////////////////////////// -void CDKIMBase::Free(char* szBuffer) +void CDKIMBase::Free(char *szBuffer) { - if (szBuffer) - delete[] szBuffer; + if (szBuffer) delete[] szBuffer; } //////////////////////////////////////////////////////////////////////////////// -// +// // Process - split buffers into lines without any CRs or LFs at the end. // //////////////////////////////////////////////////////////////////////////////// -int CDKIMBase::Process(const char* szBuffer,int nBufLength,bool bEOF) -{ const char* p = szBuffer; - const char* e = szBuffer + nBufLength; +int CDKIMBase::Process(const char *szBuffer, int nBufLength, bool bEOF) +{ + const char *p = szBuffer; + const char *e = szBuffer + nBufLength; while (p < e) { if (*p != '\n' || m_LinePos == 0 || m_Line[m_LinePos - 1] != '\r') { // add char to line if (m_LinePos >= m_LineSize) { - int nRet = ReAlloc(m_Line,m_LineSize,m_LinePos + 1); + int nRet = ReAlloc(m_Line, m_LineSize, m_LinePos + 1); if (nRet != DKIM_SUCCESS) return nRet; } m_Line[m_LinePos++] = *p; @@ -126,16 +129,15 @@ int CDKIMBase::Process(const char* szBuffer,int nBufLength,bool bEOF) if (m_LinePos == 0) { m_InHeaders = false; int Result = ProcessHeaders(); - if (Result != DKIM_SUCCESS) - return Result; + if (Result != DKIM_SUCCESS) return Result; } else { // append the header to the headers list if (m_Line[0] != ' ' && m_Line[0] != '\t') { - HeaderList.push_back(string(m_Line,m_LinePos)); -// fprintf(stderr," dkimbase.cpp:Process:Input: %s \n",m_Line); + HeaderList.push_back(string(m_Line, m_LinePos)); + // fprintf(stderr," dkimbase.cpp:Process:Input: %s \n",m_Line); } else { if (!HeaderList.empty()) { - HeaderList.back().append("\r\n",2).append(m_Line,m_LinePos); + HeaderList.back().append("\r\n", 2).append(m_Line, m_LinePos); } else { // no header to append to... } @@ -143,7 +145,7 @@ int CDKIMBase::Process(const char* szBuffer,int nBufLength,bool bEOF) } } else { // process body line - int Result = ProcessBody(m_Line,m_LinePos,bEOF); + int Result = ProcessBody(m_Line, m_LinePos, bEOF); if (Result != DKIM_SUCCESS) { m_LinePos = 0; return Result; @@ -160,28 +162,28 @@ int CDKIMBase::Process(const char* szBuffer,int nBufLength,bool bEOF) } //////////////////////////////////////////////////////////////////////////////// -// +// // ProcessFinal - process leftovers if stopping before the body or mid-line // //////////////////////////////////////////////////////////////////////////////// int CDKIMBase::ProcessFinal(void) { if (m_LinePos > 0) { - Process("\r\n",2,true); + Process("\r\n", 2, true); } if (m_InHeaders) { m_InHeaders = false; ProcessHeaders(); /* type conversion should be safe as length is zero */ - ProcessBody((char *)"",0,true); + ProcessBody((char *)"", 0, true); } return DKIM_SUCCESS; } //////////////////////////////////////////////////////////////////////////////// -// +// // ProcessHeaders - process the headers (to be implemented by derived class) // //////////////////////////////////////////////////////////////////////////////// @@ -191,52 +193,54 @@ int CDKIMBase::ProcessHeaders() } //////////////////////////////////////////////////////////////////////////////// -// +// // ProcessBody - process body line (to be implemented by derived class) // //////////////////////////////////////////////////////////////////////////////// -int CDKIMBase::ProcessBody(char* szBuffer, int nBufLength, bool bEOF) +int CDKIMBase::ProcessBody(char *szBuffer, int nBufLength, bool bEOF) { return DKIM_SUCCESS; } //////////////////////////////////////////////////////////////////////////////// -// +// // RemoveSWSP - remove streaming white space from buffer/string inline // //////////////////////////////////////////////////////////////////////////////// -struct isswsp -{ - bool operator()(char ch) { return(ch == ' ' || ch == '\t' || ch == '\r' || ch == '\n'); } +struct isswsp { + bool operator()(char ch) + { + return (ch == ' ' || ch == '\t' || ch == '\r' || ch == '\n'); + } }; -void CDKIMBase::RemoveSWSP(char* szBuffer) +void CDKIMBase::RemoveSWSP(char *szBuffer) { - *remove_if(szBuffer,szBuffer + strlen(szBuffer),isswsp()) = '\0'; + *remove_if(szBuffer, szBuffer + strlen(szBuffer), isswsp()) = '\0'; } -void CDKIMBase::RemoveSWSP(char* pBuffer,int& nBufLength) +void CDKIMBase::RemoveSWSP(char *pBuffer, int& nBufLength) { - nBufLength = remove_if(pBuffer,pBuffer+nBufLength,isswsp()) - pBuffer; + nBufLength = remove_if(pBuffer, pBuffer + nBufLength, isswsp()) - pBuffer; } void CDKIMBase::RemoveSWSP(string& sBuffer) { - sBuffer.erase(remove_if(sBuffer.begin(),sBuffer.end(),isswsp()),sBuffer.end()); + sBuffer.erase(remove_if(sBuffer.begin(), sBuffer.end(), isswsp()), sBuffer.end()); } ////////////////////////////////////////////////////////////////////////////////////////// -// +// // CompressSWSP - compress streaming white space into single spaces from buffer/string inline // ////////////////////////////////////////////////////////////////////////////////////////// -void CDKIMBase::CompressSWSP(char* pBuffer,int& nBufLength) +void CDKIMBase::CompressSWSP(char *pBuffer, int& nBufLength) { - char* pSrc = pBuffer; - char* pDst = pBuffer; - char* pEnd = pBuffer + nBufLength; + char *pSrc = pBuffer; + char *pDst = pBuffer; + char *pEnd = pBuffer + nBufLength; while (pSrc != pEnd) { if (isswsp()(*pSrc)) { @@ -245,8 +249,7 @@ void CDKIMBase::CompressSWSP(char* pBuffer,int& nBufLength) ++pSrc; } while (pSrc != pEnd && isswsp()(*pSrc)); - if (pSrc == pEnd) - break; + if (pSrc == pEnd) break; *pDst++ = ' '; } @@ -270,8 +273,7 @@ void CDKIMBase::CompressSWSP(string& sBuffer) ++iSrc; } while (iSrc != iEnd && isswsp()(*iSrc)); - if (iSrc == iEnd) - break; + if (iSrc == iEnd) break; *iDst++ = ' '; } @@ -283,7 +285,7 @@ void CDKIMBase::CompressSWSP(string& sBuffer) } ////////////////////////////////////////////////////////////////////////////////////////// -// +// // RelaxHeader - relax a header field (lower case the name, remove swsp before and after :) // // modified 4/21/06 STB to remove white space before colon @@ -303,17 +305,14 @@ string CDKIMBase::RelaxHeader(const string& sHeader) } else { // lower case the header field name for (unsigned i = 0; i < cpos; i++) { - if (sTemp[i] >= 'A' && sTemp[i] <= 'Z') - sTemp[i] += 'a'-'A'; + if (sTemp[i] >= 'A' && sTemp[i] <= 'Z') sTemp[i] += 'a' - 'A'; } // remove the space after the : - if (cpos + 1 < sTemp.length() && sTemp[cpos+1] == ' ') - sTemp.erase(cpos + 1, 1); + if (cpos + 1 < sTemp.length() && sTemp[cpos + 1] == ' ') sTemp.erase(cpos + 1, 1); // remove the space before the : - if (cpos > 0 && sTemp[cpos - 1] == ' ') - sTemp.erase(cpos - 1,1); + if (cpos > 0 && sTemp[cpos - 1] == ' ') sTemp.erase(cpos - 1, 1); } return sTemp; diff --git a/src/dkimsign.cpp b/src/dkimsign.cpp index 03b03e2..8be3fc6 100644 --- a/src/dkimsign.cpp +++ b/src/dkimsign.cpp @@ -21,15 +21,17 @@ *****************************************************************************/ #define _strnicmp strncasecmp -#define _stricmp strcasecmp +#define _stricmp strcasecmp #define LOWORD(l) ((unsigned)(l) & 0xffff) #define HIWORD(l) ((unsigned)(l) >> 16) +#include "dkimsign.h" + #include <string.h> + #include <map> #include "dkim.h" -#include "dkimsign.h" /***************************************************************************** * @@ -119,63 +121,67 @@ CDKIMSign::CDKIMSign() m_EmptyLineCount = 0; m_pfnHdrCallback = NULL; -#if ((OPENSSL_VERSION_NUMBER < 0x10100000L) || (LIBRESSL_VERSION_NUMBER > 0 && LIBRESSL_VERSION_NUMBER < 0x20700000L)) - EVP_SignInit(&m_Hdr_sha1ctx,EVP_sha1()); - EVP_SignInit(&m_Hdr_sha256ctx,EVP_sha256()); - EVP_DigestInit(&m_Bdy_sha1ctx,EVP_sha1()); - EVP_DigestInit(&m_Bdy_sha256ctx,EVP_sha256()); +#if ( \ + (OPENSSL_VERSION_NUMBER < 0x10100000L) \ + || (LIBRESSL_VERSION_NUMBER > 0 && LIBRESSL_VERSION_NUMBER < 0x20700000L)) + EVP_SignInit(&m_Hdr_sha1ctx, EVP_sha1()); + EVP_SignInit(&m_Hdr_sha256ctx, EVP_sha256()); + EVP_DigestInit(&m_Bdy_sha1ctx, EVP_sha1()); + EVP_DigestInit(&m_Bdy_sha256ctx, EVP_sha256()); #else m_Hdr_sha1ctx = EVP_MD_CTX_create(); - EVP_SignInit_ex(m_Hdr_sha1ctx,EVP_sha1(),NULL); + EVP_SignInit_ex(m_Hdr_sha1ctx, EVP_sha1(), NULL); m_Hdr_sha256ctx = EVP_MD_CTX_create(); - EVP_SignInit_ex(m_Hdr_sha256ctx,EVP_sha256(),NULL); + EVP_SignInit_ex(m_Hdr_sha256ctx, EVP_sha256(), NULL); m_Bdy_sha1ctx = EVP_MD_CTX_create(); - EVP_DigestInit_ex(m_Bdy_sha1ctx,EVP_sha1(),NULL); + EVP_DigestInit_ex(m_Bdy_sha1ctx, EVP_sha1(), NULL); m_Bdy_sha256ctx = EVP_MD_CTX_create(); - EVP_DigestInit_ex(m_Bdy_sha256ctx,EVP_sha256(),NULL); + EVP_DigestInit_ex(m_Bdy_sha256ctx, EVP_sha256(), NULL); m_Hdr_ed25519ctx = EVP_MD_CTX_create(); m_Edy_sha256ctx = EVP_MD_CTX_create(); - EVP_DigestInit_ex(m_Edy_sha256ctx,EVP_sha256(),NULL); + EVP_DigestInit_ex(m_Edy_sha256ctx, EVP_sha256(), NULL); #endif } CDKIMSign::~CDKIMSign() { -#if ((OPENSSL_VERSION_NUMBER < 0x10100000L) || (LIBRESSL_VERSION_NUMBER > 0 && LIBRESSL_VERSION_NUMBER < 0x20700000L)) - EVP_MD_CTX_cleanup(&m_Hdr_sha1ctx); - EVP_MD_CTX_cleanup(&m_Hdr_sha256ctx); - EVP_MD_CTX_cleanup(&m_Bdy_sha1ctx); - EVP_MD_CTX_cleanup(&m_Bdy_sha256ctx); +#if ( \ + (OPENSSL_VERSION_NUMBER < 0x10100000L) \ + || (LIBRESSL_VERSION_NUMBER > 0 && LIBRESSL_VERSION_NUMBER < 0x20700000L)) + EVP_MD_CTX_cleanup(&m_Hdr_sha1ctx); + EVP_MD_CTX_cleanup(&m_Hdr_sha256ctx); + EVP_MD_CTX_cleanup(&m_Bdy_sha1ctx); + EVP_MD_CTX_cleanup(&m_Bdy_sha256ctx); #else - EVP_MD_CTX_free(m_Hdr_sha1ctx); - EVP_MD_CTX_free(m_Hdr_sha256ctx); - EVP_MD_CTX_free(m_Hdr_ed25519ctx); - EVP_MD_CTX_free(m_Bdy_sha1ctx); - EVP_MD_CTX_free(m_Bdy_sha256ctx); - EVP_MD_CTX_free(m_Edy_sha256ctx); + EVP_MD_CTX_free(m_Hdr_sha1ctx); + EVP_MD_CTX_free(m_Hdr_sha256ctx); + EVP_MD_CTX_free(m_Hdr_ed25519ctx); + EVP_MD_CTX_free(m_Bdy_sha1ctx); + EVP_MD_CTX_free(m_Bdy_sha256ctx); + EVP_MD_CTX_free(m_Edy_sha256ctx); #endif } //////////////////////////////////////////////////////////////////////////////// -// +// // Init - save the options // //////////////////////////////////////////////////////////////////////////////// -int CDKIMSign::Init(DKIMSignOptions* pOptions) +int CDKIMSign::Init(DKIMSignOptions *pOptions) { int nRet = CDKIMBase::Init(); m_Canon = pOptions->nCanon; // as of draft 01, these are the only allowed signing types: - if ((m_Canon != DKIM_SIGN_SIMPLE_RELAXED) && - (m_Canon != DKIM_SIGN_RELAXED) && - (m_Canon != DKIM_SIGN_RELAXED_SIMPLE)) { + if ((m_Canon != DKIM_SIGN_SIMPLE_RELAXED) && (m_Canon != DKIM_SIGN_RELAXED) + && (m_Canon != DKIM_SIGN_RELAXED_SIMPLE)) + { m_Canon = DKIM_SIGN_SIMPLE; } @@ -203,8 +209,7 @@ int CDKIMSign::Init(DKIMSignOptions* pOptions) //sRequiredHeaders.assign(pOptions->szRequiredHeaders); //make sure there is a colon after the last header in the list - if ((sRequiredHeaders.size() > 0) && - sRequiredHeaders.at(sRequiredHeaders.size() - 1) != ':') { + if ((sRequiredHeaders.size() > 0) && sRequiredHeaders.at(sRequiredHeaders.size() - 1) != ':') { sRequiredHeaders.append(":"); } @@ -222,11 +227,11 @@ int CDKIMSign::Init(DKIMSignOptions* pOptions) } //////////////////////////////////////////////////////////////////////////////// -// +// // Hash - update the hash // //////////////////////////////////////////////////////////////////////////////// -void CDKIMSign::Hash(const char *szBuffer,int nBufLength,bool bHdr) +void CDKIMSign::Hash(const char *szBuffer, int nBufLength, bool bHdr) { /** START DEBUG CODE ** @@ -247,53 +252,56 @@ void CDKIMSign::Hash(const char *szBuffer,int nBufLength,bool bHdr) ** END DEBUG CODE **/ - if (bHdr) { /* Generate signature: b=... */ - if ((m_nHash == DKIM_HASH_SHA1) || - (m_nHash == DKIM_HASH_SHA1_AND_SHA256)) -#if ((OPENSSL_VERSION_NUMBER < 0x10100000L) || (LIBRESSL_VERSION_NUMBER > 0 && LIBRESSL_VERSION_NUMBER < 0x20700000L)) - EVP_SignUpdate(&m_Hdr_sha1ctx,szBuffer,nBufLength); -#else - EVP_SignUpdate(m_Hdr_sha1ctx,szBuffer,nBufLength); + if (bHdr) { /* Generate signature: b=... */ + if ((m_nHash == DKIM_HASH_SHA1) || (m_nHash == DKIM_HASH_SHA1_AND_SHA256)) +#if ( \ + (OPENSSL_VERSION_NUMBER < 0x10100000L) \ + || (LIBRESSL_VERSION_NUMBER > 0 && LIBRESSL_VERSION_NUMBER < 0x20700000L)) + EVP_SignUpdate(&m_Hdr_sha1ctx, szBuffer, nBufLength); +#else + EVP_SignUpdate(m_Hdr_sha1ctx, szBuffer, nBufLength); #endif - if ((m_nHash == DKIM_HASH_SHA256) || - (m_nHash == DKIM_HASH_SHA1_AND_SHA256) || - (m_nHash == DKIM_HASH_RSA256_AND_ED25519)) -#if ((OPENSSL_VERSION_NUMBER < 0x10100000L) || (LIBRESSL_VERSION_NUMBER > 0 && LIBRESSL_VERSION_NUMBER < 0x20700000L)) - EVP_SignUpdate(&m_Hdr_sha256ctx,szBuffer,nBufLength); -#else - EVP_SignUpdate(m_Hdr_sha256ctx,szBuffer,nBufLength); + if ((m_nHash == DKIM_HASH_SHA256) || (m_nHash == DKIM_HASH_SHA1_AND_SHA256) + || (m_nHash == DKIM_HASH_RSA256_AND_ED25519)) +#if ( \ + (OPENSSL_VERSION_NUMBER < 0x10100000L) \ + || (LIBRESSL_VERSION_NUMBER > 0 && LIBRESSL_VERSION_NUMBER < 0x20700000L)) + EVP_SignUpdate(&m_Hdr_sha256ctx, szBuffer, nBufLength); +#else + EVP_SignUpdate(m_Hdr_sha256ctx, szBuffer, nBufLength); #endif #if ((OPENSSL_VERSION_NUMBER > 0x10101000L)) - if ((m_nHash == DKIM_HASH_ED25519) || - (m_nHash == DKIM_HASH_RSA256_AND_ED25519)) { - SigHdrs.append(szBuffer,nBufLength); + if ((m_nHash == DKIM_HASH_ED25519) || (m_nHash == DKIM_HASH_RSA256_AND_ED25519)) { + SigHdrs.append(szBuffer, nBufLength); m_SigHdrs += nBufLength; } #endif - } else { /* lets go for body hash values: bh=... (either SHA1 or SHA256) */ - if ((m_nHash == DKIM_HASH_SHA1) || - (m_nHash == DKIM_HASH_SHA1_AND_SHA256)) -#if ((OPENSSL_VERSION_NUMBER < 0x10100000L) || (LIBRESSL_VERSION_NUMBER > 0 && LIBRESSL_VERSION_NUMBER < 0x20700000L)) - EVP_DigestUpdate(&m_Bdy_sha1ctx,szBuffer,nBufLength); -#else - EVP_DigestUpdate(m_Bdy_sha1ctx,szBuffer,nBufLength); + } else { /* lets go for body hash values: bh=... (either SHA1 or SHA256) */ + if ((m_nHash == DKIM_HASH_SHA1) || (m_nHash == DKIM_HASH_SHA1_AND_SHA256)) +#if ( \ + (OPENSSL_VERSION_NUMBER < 0x10100000L) \ + || (LIBRESSL_VERSION_NUMBER > 0 && LIBRESSL_VERSION_NUMBER < 0x20700000L)) + EVP_DigestUpdate(&m_Bdy_sha1ctx, szBuffer, nBufLength); +#else + EVP_DigestUpdate(m_Bdy_sha1ctx, szBuffer, nBufLength); #endif if (m_nHash != DKIM_HASH_SHA1) -#if ((OPENSSL_VERSION_NUMBER < 0x10100000L) || (LIBRESSL_VERSION_NUMBER > 0 && LIBRESSL_VERSION_NUMBER < 0x20700000L)) - EVP_DigestUpdate(&m_Bdy_sha256ctx,szBuffer,nBufLength); +#if ( \ + (OPENSSL_VERSION_NUMBER < 0x10100000L) \ + || (LIBRESSL_VERSION_NUMBER > 0 && LIBRESSL_VERSION_NUMBER < 0x20700000L)) + EVP_DigestUpdate(&m_Bdy_sha256ctx, szBuffer, nBufLength); #else - EVP_DigestUpdate(m_Bdy_sha256ctx,szBuffer,nBufLength); + EVP_DigestUpdate(m_Bdy_sha256ctx, szBuffer, nBufLength); #endif #if ((OPENSSL_VERSION_NUMBER > 0x10101000L)) - if ((m_nHash == DKIM_HASH_ED25519) || - (m_nHash == DKIM_HASH_RSA256_AND_ED25519)) - EVP_DigestUpdate(m_Edy_sha256ctx,szBuffer,nBufLength); + if ((m_nHash == DKIM_HASH_ED25519) || (m_nHash == DKIM_HASH_RSA256_AND_ED25519)) + EVP_DigestUpdate(m_Edy_sha256ctx, szBuffer, nBufLength); #endif } } //////////////////////////////////////////////////////////////////////////////// -// +// // SignThisTag - return boolean whether or not to sign this tag // //////////////////////////////////////////////////////////////////////////////// @@ -301,25 +309,25 @@ bool CDKIMSign::SignThisTag(const string& sTag) { bool bRet = true; - if (_strnicmp(sTag.c_str(),"X-",2) == 0 || - _stricmp(sTag.c_str(),"Authentication-Results:") == 0 || - _stricmp(sTag.c_str(),"Return-Path:") == 0) { + if (_strnicmp(sTag.c_str(), "X-", 2) == 0 || _stricmp(sTag.c_str(), "Authentication-Results:") == 0 + || _stricmp(sTag.c_str(), "Return-Path:") == 0) + { bRet = false; } return bRet; } -bool ConvertHeaderToQuotedPrintable(const char* source, char* dest) +bool ConvertHeaderToQuotedPrintable(const char *source, char *dest) { bool bConvert = false; // do quoted printable - static unsigned char hexchars[16] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'}; + static unsigned char hexchars[16] = { + '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; - unsigned char *d = (unsigned char*)dest; - for (const unsigned char *s = (const unsigned char *)source; *s != '\0'; s++) - { + unsigned char *d = (unsigned char *)dest; + for (const unsigned char *s = (const unsigned char *)source; *s != '\0'; s++) { if (*s >= 33 && *s <= 126 && *s != '=' && *s != ':' && *s != ';' && *s != '|') { *d++ = *s; } else { @@ -335,15 +343,19 @@ bool ConvertHeaderToQuotedPrintable(const char* source, char* dest) } //////////////////////////////////////////////////////////////////////////////// -// +// // GetHeaderParams - Extract any needed header parameters // //////////////////////////////////////////////////////////////////////////////// -void CDKIMSign::GetHeaderParams(const string &sHdr) +void CDKIMSign::GetHeaderParams(const string& sHdr) { - if (_strnicmp(sHdr.c_str(),"X",1) == 0) return; - if (_strnicmp(sHdr.c_str(),"From:",5) == 0) { sFrom.assign(sHdr.c_str() + 5); } - if (_strnicmp(sHdr.c_str(),"Sender:",7) == 0) { sSender.assign(sHdr.c_str() + 7); } + if (_strnicmp(sHdr.c_str(), "X", 1) == 0) return; + if (_strnicmp(sHdr.c_str(), "From:", 5) == 0) { + sFrom.assign(sHdr.c_str() + 5); + } + if (_strnicmp(sHdr.c_str(), "Sender:", 7) == 0) { + sSender.assign(sHdr.c_str() + 7); + } if (m_nIncludeCopiedHeaders) { string::size_type pos = sHdr.find(':'); @@ -352,13 +364,16 @@ void CDKIMSign::GetHeaderParams(const string &sHdr) string sTag, sValue; char *workBuffer = new char[sHdr.size() * 3 + 1]; - sTag.assign(sHdr.substr(0,pos)); - sValue.assign(sHdr.substr(pos + 1,string::npos)); + sTag.assign(sHdr.substr(0, pos)); + sValue.assign(sHdr.substr(pos + 1, string::npos)); - ConvertHeaderToQuotedPrintable(sTag.c_str(),workBuffer); - if (!m_sCopiedHeaders.empty()) { m_sCopiedHeaders.append("|"); } - m_sCopiedHeaders.append(workBuffer); m_sCopiedHeaders.append(":"); - ConvertHeaderToQuotedPrintable(sValue.c_str(),workBuffer); + ConvertHeaderToQuotedPrintable(sTag.c_str(), workBuffer); + if (!m_sCopiedHeaders.empty()) { + m_sCopiedHeaders.append("|"); + } + m_sCopiedHeaders.append(workBuffer); + m_sCopiedHeaders.append(":"); + ConvertHeaderToQuotedPrintable(sValue.c_str(), workBuffer); m_sCopiedHeaders.append(workBuffer); delete[] workBuffer; @@ -367,14 +382,14 @@ void CDKIMSign::GetHeaderParams(const string &sHdr) } //////////////////////////////////////////////////////////////////////////////// -// +// // ProcessHeaders - sign headers and save needed parameters (this is a lie) // //////////////////////////////////////////////////////////////////////////////// int CDKIMSign::ProcessHeaders(void) { - map<string,list<string>::reverse_iterator> IterMap; - map<string,list<string>::reverse_iterator>::iterator IterMapIter; + map<string, list<string>::reverse_iterator> IterMap; + map<string, list<string>::reverse_iterator>::iterator IterMapIter; list<string>::reverse_iterator riter; list<string>::iterator iter; string sTag; @@ -391,20 +406,20 @@ int CDKIMSign::ProcessHeaders(void) int nSignThisTag = 1; // hack off anything past the colon - sTag.erase(pos + 1,string::npos); + sTag.erase(pos + 1, string::npos); // is this the From: header? - if (_stricmp(sTag.c_str(),"From:") == 0) { + if (_stricmp(sTag.c_str(), "From:") == 0) { bFromHeaderFound = true; nSignThisTag = 1; IsRequiredHeader(sTag); // remove from required header list } // is this in the list of headers that must be signed? - else if (IsRequiredHeader(sTag)) { + else if (IsRequiredHeader(sTag)) + { nSignThisTag = 1; - } - else { - if(m_pfnHdrCallback) { + } else { + if (m_pfnHdrCallback) { nSignThisTag = m_pfnHdrCallback(iter->c_str()); } else { nSignThisTag = SignThisTag(sTag) ? 1 : 0; @@ -424,7 +439,7 @@ int CDKIMSign::ProcessHeaders(void) // walk the list in reverse looking for the last instance of this header while (riter != HeaderList.rend()) { - if (_strnicmp(riter->c_str(),sTag.c_str(),sTag.size()) == 0) { + if (_strnicmp(riter->c_str(), sTag.c_str(), sTag.size()) == 0) { ProcessHeader(*riter); // save the reverse iterator position for this tag @@ -433,106 +448,102 @@ int CDKIMSign::ProcessHeaders(void) break; } riter++; - } + } } } } - if(!bFromHeaderFound) { + if (!bFromHeaderFound) { string sFrom("From:"); hParam.append(sFrom); - IsRequiredHeader(sFrom); // remove from required header list -// Hash("\r\n",2); + IsRequiredHeader(sFrom); // remove from required header list + // Hash("\r\n",2); } hParam.append(sRequiredHeaders); -// string::size_type end = sRequiredHeaders.find(':'); -// while (end != string::npos) -// { -// Hash("\r\n",2); -// end = sRequiredHeaders.find(':', end+1); -// } + // string::size_type end = sRequiredHeaders.find(':'); + // while (end != string::npos) + // { + // Hash("\r\n",2); + // end = sRequiredHeaders.find(':', end+1); + // } // remove the last colon from h= - if (hParam.at(hParam.size() - 1) == ':') - hParam.erase(hParam.size() - 1,string::npos); + if (hParam.at(hParam.size() - 1) == ':') hParam.erase(hParam.size() - 1, string::npos); return DKIM_SUCCESS; } -void CDKIMSign::ProcessHeader(const string &sHdr) +void CDKIMSign::ProcessHeader(const string& sHdr) { switch (HIWORD(m_Canon)) { case DKIM_CANON_SIMPLE: - Hash(sHdr.c_str(),sHdr.size(),true); - Hash("\r\n",2,true); + Hash(sHdr.c_str(), sHdr.size(), true); + Hash("\r\n", 2, true); break; - case DKIM_CANON_NOWSP: { + case DKIM_CANON_NOWSP: { string sTemp = sHdr; RemoveSWSP(sTemp); // convert characters before ':' to lower case - for (char *s = (char*)sTemp.c_str(); *s != '\0' && *s != ':'; s++) { - if (*s >= 'A' && *s <= 'Z') - *s += 'a' - 'A'; + for (char *s = (char *)sTemp.c_str(); *s != '\0' && *s != ':'; s++) { + if (*s >= 'A' && *s <= 'Z') *s += 'a' - 'A'; } - Hash(sTemp.c_str(),sTemp.size(),true); - Hash("\r\n",2,true); - } - break; + Hash(sTemp.c_str(), sTemp.size(), true); + Hash("\r\n", 2, true); + } break; - case DKIM_CANON_RELAXED: { + case DKIM_CANON_RELAXED: { string sTemp = RelaxHeader(sHdr); - Hash(sTemp.c_str(),sTemp.length(),true); - Hash("\r\n",2,true); - } - break; + Hash(sTemp.c_str(), sTemp.length(), true); + Hash("\r\n", 2, true); + } break; } } -int CDKIMSign::ProcessBody(char *szBuffer,int nBufLength,bool bEOF) +int CDKIMSign::ProcessBody(char *szBuffer, int nBufLength, bool bEOF) { - switch(LOWORD(m_Canon)) { + switch (LOWORD(m_Canon)) { case DKIM_CANON_SIMPLE: if (nBufLength > 0) { while (m_EmptyLineCount > 0) { - Hash("\r\n",2,false); + Hash("\r\n", 2, false); m_nBodyLength += 2; m_EmptyLineCount--; } - Hash(szBuffer,nBufLength,false); - Hash("\r\n",2,false); + Hash(szBuffer, nBufLength, false); + Hash("\r\n", 2, false); m_nBodyLength += nBufLength + 2; } else { m_EmptyLineCount++; if (bEOF) { - Hash("\r\n",2,false); + Hash("\r\n", 2, false); m_nBodyLength += 2; } } break; case DKIM_CANON_NOWSP: - RemoveSWSP(szBuffer,nBufLength); + RemoveSWSP(szBuffer, nBufLength); if (nBufLength > 0) { - Hash(szBuffer,nBufLength,false); + Hash(szBuffer, nBufLength, false); m_nBodyLength += nBufLength; } break; case DKIM_CANON_RELAXED: - CompressSWSP(szBuffer,nBufLength); + CompressSWSP(szBuffer, nBufLength); if (nBufLength > 0) { while (m_EmptyLineCount > 0) { - Hash("\r\n",2,false); + Hash("\r\n", 2, false); m_nBodyLength += 2; m_EmptyLineCount--; } - Hash(szBuffer,nBufLength,false); + Hash(szBuffer, nBufLength, false); m_nBodyLength += nBufLength; if (!bEOF) { - Hash("\r\n",2,false); + Hash("\r\n", 2, false); m_nBodyLength += 2; } } else @@ -560,21 +571,18 @@ bool CDKIMSign::ParseFromAddress(void) // remove '<' and anything before it pos = sAddress.find('<'); - if(pos != string::npos) - sAddress.erase(0,pos); + if (pos != string::npos) sAddress.erase(0, pos); // remove '>' and anything after it pos = sAddress.find('>'); - if (pos != string::npos) - sAddress.erase(pos,string::npos); + if (pos != string::npos) sAddress.erase(pos, string::npos); // look for '@' symbol pos = sAddress.find('@'); - if (pos == string::npos) - return false; + if (pos == string::npos) return false; if (sDomain.empty()) { - sDomain.assign (sAddress.c_str() + pos + 1); + sDomain.assign(sAddress.c_str() + pos + 1); RemoveSWSP(sDomain); } @@ -582,7 +590,7 @@ bool CDKIMSign::ParseFromAddress(void) } //////////////////////////////////////////////////////////////////////////////// -// +// // InitSig - initialize signature folding algorithm // //////////////////////////////////////////////////////////////////////////////// @@ -594,12 +602,12 @@ void CDKIMSign::InitSig(void) } //////////////////////////////////////////////////////////////////////////////// -// +// // AddTagToSig - add tag and value to signature folding if necessary // if bFold, fold at cbrk char // //////////////////////////////////////////////////////////////////////////////// -void CDKIMSign::AddTagToSig(const char* const Tag,const string &sValue,char cbrk,bool bFold) +void CDKIMSign::AddTagToSig(const char *const Tag, const string& sValue, char cbrk, bool bFold) { int nTagLen = strlen(Tag); @@ -613,35 +621,35 @@ void CDKIMSign::AddTagToSig(const char* const Tag,const string &sValue,char cbrk m_sSig.append(sValue); m_nSigPos += sValue.size(); } else { - AddFoldedValueToSig(sValue,cbrk); + AddFoldedValueToSig(sValue, cbrk); } m_sSig.append(";"); m_nSigPos++; } //////////////////////////////////////////////////////////////////////////////// -// +// // AddTagToSig - add tag and numeric value to signature folding if necessary // //////////////////////////////////////////////////////////////////////////////// -void CDKIMSign::AddTagToSig(const char* const Tag,unsigned long nValue) +void CDKIMSign::AddTagToSig(const char *const Tag, unsigned long nValue) { char szValue[64]; - sprintf(szValue,"%lu",nValue); - AddTagToSig(Tag,szValue,0,false); + sprintf(szValue, "%lu", nValue); + AddTagToSig(Tag, szValue, 0, false); } //////////////////////////////////////////////////////////////////////////////// -// +// // AddInterTagSpace - add space or fold here // //////////////////////////////////////////////////////////////////////////////// void CDKIMSign::AddInterTagSpace(int nSizeOfNextTag) { if (m_nSigPos + nSizeOfNextTag + 1 > OptimalHeaderLineLength) { -// m_sSig.append("\r\n\t"); - m_sSig.append("\r\n "); /* s/qmail style */ - m_nSigPos = 1; + // m_sSig.append("\r\n\t"); + m_sSig.append("\r\n "); /* s/qmail style */ + m_nSigPos = 1; } else { m_sSig.append(" "); m_nSigPos++; @@ -649,12 +657,12 @@ void CDKIMSign::AddInterTagSpace(int nSizeOfNextTag) } //////////////////////////////////////////////////////////////////////////////// -// +// // AddTagToSig - add value to signature folding if necessary // if cbrk == 0 fold anywhere, otherwise fold only at cbrk // //////////////////////////////////////////////////////////////////////////////// -void CDKIMSign::AddFoldedValueToSig(const string &sValue,char cbrk) +void CDKIMSign::AddFoldedValueToSig(const string& sValue, char cbrk) { string::size_type pos = 0; @@ -662,15 +670,14 @@ void CDKIMSign::AddFoldedValueToSig(const string &sValue,char cbrk) // fold anywhere while (pos < sValue.size()) { string::size_type len = OptimalHeaderLineLength - m_nSigPos; - if (len > sValue.size() - pos) - len = sValue.size() - pos; - m_sSig.append(sValue.substr(pos,len)); + if (len > sValue.size() - pos) len = sValue.size() - pos; + m_sSig.append(sValue.substr(pos, len)); m_nSigPos += len; pos += len; if (pos < sValue.size()) { -// m_sSig.append("\r\n\t"); - m_sSig.append("\r\n "); /* s/qmail style */ + // m_sSig.append("\r\n\t"); + m_sSig.append("\r\n "); /* s/qmail style */ m_nSigPos = 1; } } @@ -683,11 +690,11 @@ void CDKIMSign::AddFoldedValueToSig(const string &sValue,char cbrk) if (sValue.size() - pos < len) { brkpos = sValue.size(); } else { - brkpos = sValue.rfind(cbrk,pos + len); + brkpos = sValue.rfind(cbrk, pos + len); } if (brkpos == string::npos || brkpos < pos) { - brkpos = sValue.find(cbrk,pos); + brkpos = sValue.find(cbrk, pos); if (brkpos == string::npos) { brkpos = sValue.size(); } @@ -695,14 +702,14 @@ void CDKIMSign::AddFoldedValueToSig(const string &sValue,char cbrk) len = brkpos - pos + 1; - m_sSig.append(sValue.substr(pos,len)); + m_sSig.append(sValue.substr(pos, len)); m_nSigPos += len; pos += len; if (pos < sValue.size()) { -// m_sSig.append("\r\n\t"); - m_sSig.append("\r\n "); /* s/qmail style */ + // m_sSig.append("\r\n\t"); + m_sSig.append("\r\n "); /* s/qmail style */ m_nSigPos = 1; } } @@ -710,11 +717,11 @@ void CDKIMSign::AddFoldedValueToSig(const string &sValue,char cbrk) } //////////////////////////////////////////////////////////////////////////////// -// +// // GetSig - compute hash and return signature header in szSignature // //////////////////////////////////////////////////////////////////////////////// -int CDKIMSign::GetSig2(char* szRSAKey,char* szECCKey,char** pszSignature) +int CDKIMSign::GetSig2(char *szRSAKey, char *szECCKey, char **pszSignature) { if (szRSAKey == NULL && szECCKey == NULL) { return DKIM_BAD_PRIVATE_KEY; @@ -724,19 +731,18 @@ int CDKIMSign::GetSig2(char* szRSAKey,char* szECCKey,char** pszSignature) return DKIM_BUFFER_TOO_SMALL; } - int nRet = AssembleReturnedSig(szRSAKey,szECCKey); + int nRet = AssembleReturnedSig(szRSAKey, szECCKey); - if (nRet != DKIM_SUCCESS) - return nRet; + if (nRet != DKIM_SUCCESS) return nRet; - *pszSignature = (char*)m_sReturnedSig.c_str(); + *pszSignature = (char *)m_sReturnedSig.c_str(); return DKIM_SUCCESS; } //////////////////////////////////////////////////////////////////////////////// -// +// // IsRequiredHeader - Check if header in required list. If so, delete // header from list. // @@ -748,118 +754,132 @@ bool CDKIMSign::IsRequiredHeader(const string& sTag) while (end != string::npos) { // check for a zero-length header - if(start == end) { - sRequiredHeaders.erase(start,1); + if (start == end) { + sRequiredHeaders.erase(start, 1); } else { - if (_stricmp(sTag.c_str(),sRequiredHeaders.substr(start,end - start + 1).c_str()) == 0) { - sRequiredHeaders.erase(start,end - start + 1); + if (_stricmp(sTag.c_str(), sRequiredHeaders.substr(start, end - start + 1).c_str()) == 0) { + sRequiredHeaders.erase(start, end - start + 1); return true; } else { start = end + 1; } } - end = sRequiredHeaders.find(':',start); + end = sRequiredHeaders.find(':', start); } return false; } //////////////////////////////////////////////////////////////////////////////// -// +// // ConstructSignature // // Here, we don't construct the 'signature' but rather the DKIM header // multiply and indidually crafted for each distinct nSigAlg method -// +// // nSigAlg: DKIM_HASH_SHA1, DKIM_HASH_SHA256, DKIM_HASH_ED25519 // //////////////////////////////////////////////////////////////////////////////// -int CDKIMSign::ConstructSignature(char* szPrivKey,int nSigAlg) +int CDKIMSign::ConstructSignature(char *szPrivKey, int nSigAlg) { string sSignedSig; - unsigned char* sig; + unsigned char *sig; EVP_PKEY *pkey = 0; BIO *bio, *b64; unsigned int siglen; int size; int len; - char* buf; + char *buf; int nSignRet; /* construct the DKIM-Signature: header and add to hash */ InitSig(); - AddTagToSig("v","1",0,false); + AddTagToSig("v", "1", 0, false); switch (nSigAlg) { - case DKIM_HASH_SHA1: - AddTagToSig("a","rsa-sha1",0,false); break; - case DKIM_HASH_SHA256: - AddTagToSig("a","rsa-sha256",0,false); break; - case DKIM_HASH_ED25519: - AddTagToSig("a","ed25519-sha256",0,false); break; + case DKIM_HASH_SHA1: AddTagToSig("a", "rsa-sha1", 0, false); break; + case DKIM_HASH_SHA256: AddTagToSig("a", "rsa-sha256", 0, false); break; + case DKIM_HASH_ED25519: AddTagToSig("a", "ed25519-sha256", 0, false); break; } switch (m_Canon) { - case DKIM_SIGN_SIMPLE: - AddTagToSig("c","simple/simple",0,false); break; - case DKIM_SIGN_SIMPLE_RELAXED: - AddTagToSig("c","simple/relaxed",0,false); break; - case DKIM_SIGN_RELAXED: - AddTagToSig("c","relaxed/relaxed",0,false); break; - case DKIM_SIGN_RELAXED_SIMPLE: - AddTagToSig("c","relaxed/simple",0,false); break; + case DKIM_SIGN_SIMPLE: AddTagToSig("c", "simple/simple", 0, false); break; + case DKIM_SIGN_SIMPLE_RELAXED: AddTagToSig("c", "simple/relaxed", 0, false); break; + case DKIM_SIGN_RELAXED: AddTagToSig("c", "relaxed/relaxed", 0, false); break; + case DKIM_SIGN_RELAXED_SIMPLE: AddTagToSig("c", "relaxed/simple", 0, false); break; } - AddTagToSig("d",sDomain,0,false); + AddTagToSig("d", sDomain, 0, false); if (nSigAlg == DKIM_HASH_ED25519) - AddTagToSig("s",eSelector,0,false); + AddTagToSig("s", eSelector, 0, false); else - AddTagToSig("s",sSelector,0,false); - if (m_IncludeBodyLengthTag) { AddTagToSig("l",m_nBodyLength); } - if (m_nIncludeTimeStamp != 0) { time_t t; time(&t); AddTagToSig("t",t); } - if (m_ExpireTime != 0) { AddTagToSig("x",m_ExpireTime); } - if (!sIdentity.empty()) { AddTagToSig("i",sIdentity,0,false); } - if (m_nIncludeQueryMethod) { AddTagToSig("q","dns/txt",0,false); } + AddTagToSig("s", sSelector, 0, false); + if (m_IncludeBodyLengthTag) { + AddTagToSig("l", m_nBodyLength); + } + if (m_nIncludeTimeStamp != 0) { + time_t t; + time(&t); + AddTagToSig("t", t); + } + if (m_ExpireTime != 0) { + AddTagToSig("x", m_ExpireTime); + } + if (!sIdentity.empty()) { + AddTagToSig("i", sIdentity, 0, false); + } + if (m_nIncludeQueryMethod) { + AddTagToSig("q", "dns/txt", 0, false); + } - AddTagToSig("h",hParam,':',true); // copied headers follow the ':' - if (m_nIncludeCopiedHeaders) { AddTagToSig("z",m_sCopiedHeaders,0,true); } + AddTagToSig("h", hParam, ':', true); // copied headers follow the ':' + if (m_nIncludeCopiedHeaders) { + AddTagToSig("z", m_sCopiedHeaders, 0, true); + } /* Set up context for (body) hash */ - + unsigned char Hash[4096]; unsigned int nHashLen = 0; switch (nSigAlg) { - case DKIM_HASH_SHA1: -#if ((OPENSSL_VERSION_NUMBER < 0x10100000L) || (LIBRESSL_VERSION_NUMBER > 0 && LIBRESSL_VERSION_NUMBER < 0x20700000L)) - EVP_DigestFinal(&m_Bdy_sha1ctx,Hash,&nHashLen); break; + case DKIM_HASH_SHA1: +#if ( \ + (OPENSSL_VERSION_NUMBER < 0x10100000L) \ + || (LIBRESSL_VERSION_NUMBER > 0 && LIBRESSL_VERSION_NUMBER < 0x20700000L)) + EVP_DigestFinal(&m_Bdy_sha1ctx, Hash, &nHashLen); + break; #else - EVP_DigestFinal_ex(m_Bdy_sha1ctx,Hash,&nHashLen); break; + EVP_DigestFinal_ex(m_Bdy_sha1ctx, Hash, &nHashLen); + break; #endif - case DKIM_HASH_SHA256: -#if ((OPENSSL_VERSION_NUMBER < 0x10100000L) || (LIBRESSL_VERSION_NUMBER > 0 && LIBRESSL_VERSION_NUMBER < 0x20700000L)) - EVP_DigestFinal(&m_Bdy_sha256ctx,Hash,&nHashLen); break; + case DKIM_HASH_SHA256: +#if ( \ + (OPENSSL_VERSION_NUMBER < 0x10100000L) \ + || (LIBRESSL_VERSION_NUMBER > 0 && LIBRESSL_VERSION_NUMBER < 0x20700000L)) + EVP_DigestFinal(&m_Bdy_sha256ctx, Hash, &nHashLen); + break; #else - EVP_DigestFinal_ex(m_Bdy_sha256ctx,Hash,&nHashLen); break; + EVP_DigestFinal_ex(m_Bdy_sha256ctx, Hash, &nHashLen); + break; #endif -#if (OPENSSL_VERSION_NUMBER > 0x10101000L) - case DKIM_HASH_ED25519: - EVP_DigestFinal_ex(m_Edy_sha256ctx,Hash,&nHashLen); break; +#if (OPENSSL_VERSION_NUMBER > 0x10101000L) + case DKIM_HASH_ED25519: EVP_DigestFinal_ex(m_Edy_sha256ctx, Hash, &nHashLen); break; #endif } bio = BIO_new(BIO_s_mem()); - if (!bio) return DKIM_OUT_OF_MEMORY; - + if (!bio) return DKIM_OUT_OF_MEMORY; + b64 = BIO_new(BIO_f_base64()); if (!b64) { BIO_free(bio); return DKIM_OUT_OF_MEMORY; } - BIO_set_flags(b64,BIO_FLAGS_BASE64_NO_NL); - BIO_push(b64,bio); - if (BIO_write(b64,Hash,nHashLen) < (int)nHashLen) { + BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL); + BIO_push(b64, bio); + if (BIO_write(b64, Hash, nHashLen) < (int)nHashLen) { BIO_free_all(b64); return DKIM_OUT_OF_MEMORY; } @@ -873,17 +893,17 @@ int CDKIMSign::ConstructSignature(char* szPrivKey,int nSigAlg) return DKIM_OUT_OF_MEMORY; } - size = BIO_read(bio,buf,len); + size = BIO_read(bio, buf, len); BIO_free_all(b64); // this should never happen if (size >= len) { - delete[] buf; - return DKIM_OUT_OF_MEMORY; + delete[] buf; + return DKIM_OUT_OF_MEMORY; } buf[size] = '\0'; - AddTagToSig("bh",buf,0,true); + AddTagToSig("bh", buf, 0, true); delete[] buf; AddInterTagSpace(3); @@ -908,35 +928,46 @@ int CDKIMSign::ConstructSignature(char* szPrivKey,int nSigAlg) /* Update streaming signatures */ switch (nSigAlg) { - case DKIM_HASH_SHA1: -#if ((OPENSSL_VERSION_NUMBER < 0x10100000L) || (LIBRESSL_VERSION_NUMBER > 0 && LIBRESSL_VERSION_NUMBER < 0x20700000L)) - EVP_SignUpdate(&m_Hdr_sha1ctx,sTemp.c_str(),sTemp.size()); break; + case DKIM_HASH_SHA1: +#if ( \ + (OPENSSL_VERSION_NUMBER < 0x10100000L) \ + || (LIBRESSL_VERSION_NUMBER > 0 && LIBRESSL_VERSION_NUMBER < 0x20700000L)) + EVP_SignUpdate(&m_Hdr_sha1ctx, sTemp.c_str(), sTemp.size()); + break; #else - EVP_SignUpdate(m_Hdr_sha1ctx,sTemp.c_str(),sTemp.size()); break; + EVP_SignUpdate(m_Hdr_sha1ctx, sTemp.c_str(), sTemp.size()); + break; #endif - case DKIM_HASH_SHA256: -#if ((OPENSSL_VERSION_NUMBER < 0x10100000L) || (LIBRESSL_VERSION_NUMBER > 0 && LIBRESSL_VERSION_NUMBER < 0x20700000L)) - EVP_SignUpdate(&m_Hdr_sha256ctx,sTemp.c_str(),sTemp.size()); break; + case DKIM_HASH_SHA256: +#if ( \ + (OPENSSL_VERSION_NUMBER < 0x10100000L) \ + || (LIBRESSL_VERSION_NUMBER > 0 && LIBRESSL_VERSION_NUMBER < 0x20700000L)) + EVP_SignUpdate(&m_Hdr_sha256ctx, sTemp.c_str(), sTemp.size()); + break; #else - EVP_SignUpdate(m_Hdr_sha256ctx,sTemp.c_str(),sTemp.size()); break; + EVP_SignUpdate(m_Hdr_sha256ctx, sTemp.c_str(), sTemp.size()); + break; #endif #if ((OPENSSL_VERSION_NUMBER > 0x10101000L)) - case DKIM_HASH_ED25519: - SigHdrs.append(sTemp.c_str(),sTemp.size()); - m_SigHdrs += sTemp.size(); break; + case DKIM_HASH_ED25519: + SigHdrs.append(sTemp.c_str(), sTemp.size()); + m_SigHdrs += sTemp.size(); + break; #endif } bio = BIO_new_mem_buf(szPrivKey, -1); if (bio == NULL) return DKIM_OUT_OF_MEMORY; - pkey = PEM_read_bio_PrivateKey(bio,NULL,NULL,NULL); // FIXME - done + pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL); // FIXME - done BIO_free(bio); - if (!pkey) { return DKIM_BAD_PRIVATE_KEY; } + if (!pkey) { + return DKIM_BAD_PRIVATE_KEY; + } siglen = EVP_PKEY_size(pkey); - sig = (unsigned char*) OPENSSL_malloc(siglen); + sig = (unsigned char *)OPENSSL_malloc(siglen); if (sig == NULL) { EVP_PKEY_free(pkey); return DKIM_OUT_OF_MEMORY; @@ -945,41 +976,50 @@ int CDKIMSign::ConstructSignature(char* szPrivKey,int nSigAlg) /* Finish streaming signature and potentially go for Ed25519 signatures */ size_t sig_len; - unsigned char* SignMsg; + unsigned char *SignMsg; switch (nSigAlg) { - case DKIM_HASH_SHA1: -#if ((OPENSSL_VERSION_NUMBER < 0x10100000L) || (LIBRESSL_VERSION_NUMBER > 0 && LIBRESSL_VERSION_NUMBER < 0x20700000L)) - nSignRet = EVP_SignFinal(&m_Hdr_sha1ctx,sig,&siglen,pkey); break; + case DKIM_HASH_SHA1: +#if ( \ + (OPENSSL_VERSION_NUMBER < 0x10100000L) \ + || (LIBRESSL_VERSION_NUMBER > 0 && LIBRESSL_VERSION_NUMBER < 0x20700000L)) + nSignRet = EVP_SignFinal(&m_Hdr_sha1ctx, sig, &siglen, pkey); + break; #else - nSignRet = EVP_SignFinal(m_Hdr_sha1ctx,sig,&siglen,pkey); break; + nSignRet = EVP_SignFinal(m_Hdr_sha1ctx, sig, &siglen, pkey); + break; #endif - case DKIM_HASH_SHA256: -#if ((OPENSSL_VERSION_NUMBER < 0x10100000L) || (LIBRESSL_VERSION_NUMBER > 0 && LIBRESSL_VERSION_NUMBER < 0x20700000L)) - nSignRet = EVP_SignFinal(&m_Hdr_sha256ctx,sig,&siglen,pkey); break; + case DKIM_HASH_SHA256: +#if ( \ + (OPENSSL_VERSION_NUMBER < 0x10100000L) \ + || (LIBRESSL_VERSION_NUMBER > 0 && LIBRESSL_VERSION_NUMBER < 0x20700000L)) + nSignRet = EVP_SignFinal(&m_Hdr_sha256ctx, sig, &siglen, pkey); + break; #else - nSignRet = EVP_SignFinal(m_Hdr_sha256ctx,sig,&siglen,pkey); break; + nSignRet = EVP_SignFinal(m_Hdr_sha256ctx, sig, &siglen, pkey); + break; #endif -#if (OPENSSL_VERSION_NUMBER > 0x10101000L) - case DKIM_HASH_ED25519: - EVP_DigestSignInit(m_Hdr_ed25519ctx,NULL,NULL,NULL,pkey); - SignMsg = (unsigned char*) SigHdrs.c_str(); - EVP_DigestSign(m_Hdr_ed25519ctx,NULL,&sig_len,SignMsg,m_SigHdrs); - sig = (unsigned char*) OPENSSL_malloc(sig_len); - nSignRet = EVP_DigestSign(m_Hdr_ed25519ctx,sig,&sig_len,SignMsg,m_SigHdrs); - siglen = (unsigned int) sig_len; break; +#if (OPENSSL_VERSION_NUMBER > 0x10101000L) + case DKIM_HASH_ED25519: + EVP_DigestSignInit(m_Hdr_ed25519ctx, NULL, NULL, NULL, pkey); + SignMsg = (unsigned char *)SigHdrs.c_str(); + EVP_DigestSign(m_Hdr_ed25519ctx, NULL, &sig_len, SignMsg, m_SigHdrs); + sig = (unsigned char *)OPENSSL_malloc(sig_len); + nSignRet = EVP_DigestSign(m_Hdr_ed25519ctx, sig, &sig_len, SignMsg, m_SigHdrs); + siglen = (unsigned int)sig_len; + break; #endif } EVP_PKEY_free(pkey); if (!nSignRet) { OPENSSL_free(sig); - return DKIM_BAD_PRIVATE_KEY; // key too small + return DKIM_BAD_PRIVATE_KEY; // key too small } bio = BIO_new(BIO_s_mem()); if (!bio) { - return DKIM_OUT_OF_MEMORY; + return DKIM_OUT_OF_MEMORY; } b64 = BIO_new(BIO_f_base64()); @@ -988,10 +1028,10 @@ int CDKIMSign::ConstructSignature(char* szPrivKey,int nSigAlg) return DKIM_OUT_OF_MEMORY; } - BIO_set_flags(b64,BIO_FLAGS_BASE64_NO_NL); - BIO_push(b64,bio); + BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL); + BIO_push(b64, bio); - if (BIO_write(b64,sig,siglen) < (int) siglen) { + if (BIO_write(b64, sig, siglen) < (int)siglen) { OPENSSL_free(sig); BIO_free_all(b64); return DKIM_OUT_OF_MEMORY; @@ -1007,35 +1047,34 @@ int CDKIMSign::ConstructSignature(char* szPrivKey,int nSigAlg) return DKIM_OUT_OF_MEMORY; } - size = BIO_read(bio,buf,len); + size = BIO_read(bio, buf, len); BIO_free_all(b64); // this should never happen if (size >= len) { delete[] buf; - return DKIM_OUT_OF_MEMORY; + return DKIM_OUT_OF_MEMORY; } buf[size] = '\0'; - AddFoldedValueToSig(buf,0); + AddFoldedValueToSig(buf, 0); delete[] buf; return DKIM_SUCCESS; } //////////////////////////////////////////////////////////////////////////////// -// +// // AssembleReturnSig // -// calls ConstructSignature +// calls ConstructSignature // for all different hashes and signature key files // //////////////////////////////////////////////////////////////////////////////// -int CDKIMSign::AssembleReturnedSig(char* szRSAKey,char* szECCKey) +int CDKIMSign::AssembleReturnedSig(char *szRSAKey, char *szECCKey) { int nRet; - if (m_bReturnedSigAssembled) - return DKIM_SUCCESS; + if (m_bReturnedSigAssembled) return DKIM_SUCCESS; ProcessFinal(); @@ -1045,30 +1084,28 @@ int CDKIMSign::AssembleReturnedSig(char* szRSAKey,char* szECCKey) string ed25519Sig, sha256Sig, sha1Sig; - if ((m_nHash == DKIM_HASH_ED25519) || - (m_nHash == DKIM_HASH_RSA256_AND_ED25519)) { - nRet = ConstructSignature(szECCKey,DKIM_HASH_ED25519); + if ((m_nHash == DKIM_HASH_ED25519) || (m_nHash == DKIM_HASH_RSA256_AND_ED25519)) { + nRet = ConstructSignature(szECCKey, DKIM_HASH_ED25519); if (nRet == DKIM_SUCCESS) { ed25519Sig.assign(m_sSig); } else { return nRet; - } + } } - if ((m_nHash == DKIM_HASH_SHA256) || - (m_nHash == DKIM_HASH_SHA1_AND_SHA256) || - (m_nHash == DKIM_HASH_RSA256_AND_ED25519)) { - nRet = ConstructSignature(szRSAKey,DKIM_HASH_SHA256); + if ((m_nHash == DKIM_HASH_SHA256) || (m_nHash == DKIM_HASH_SHA1_AND_SHA256) + || (m_nHash == DKIM_HASH_RSA256_AND_ED25519)) + { + nRet = ConstructSignature(szRSAKey, DKIM_HASH_SHA256); if (nRet == DKIM_SUCCESS) { sha256Sig.assign(m_sSig); } else { return nRet; - } + } } - if ((m_nHash == DKIM_HASH_SHA1) || - (m_nHash == DKIM_HASH_SHA1_AND_SHA256)) { - nRet = ConstructSignature(szRSAKey,DKIM_HASH_SHA1); + if ((m_nHash == DKIM_HASH_SHA1) || (m_nHash == DKIM_HASH_SHA1_AND_SHA256)) { + nRet = ConstructSignature(szRSAKey, DKIM_HASH_SHA1); if (nRet == DKIM_SUCCESS) { sha1Sig.assign(m_sSig); } else { @@ -1076,11 +1113,11 @@ int CDKIMSign::AssembleReturnedSig(char* szRSAKey,char* szECCKey) } } -// fclose(fpdebug); -// fpdebug = NULL; + // fclose(fpdebug); + // fpdebug = NULL; if (!ed25519Sig.empty()) { -/* if (!m_sReturnedSig.empty()) { + /* if (!m_sReturnedSig.empty()) { m_sReturnedSig.append("\r\n"); } */ diff --git a/src/dkimverify.cpp b/src/dkimverify.cpp index c9f1003..6693d8e 100644 --- a/src/dkimverify.cpp +++ b/src/dkimverify.cpp @@ -18,17 +18,21 @@ * Changes done by ¢feh@fehcom.de obeying the above license * *****************************************************************************/ -#include <string.h> -#include <ctype.h> +#include "dkimverify.h" + #include <assert.h> -#include <vector> +#include <ctype.h> +#include <string.h> + #include <algorithm> +#include <vector> + #include "dkim.h" -#include "dkimverify.h" #include "dnsgettxt.h" -extern "C" { -#include "dns.h" +extern "C" { #include "stralloc.h" + +#include "dns.h" } /***************************************************************************** @@ -49,17 +53,17 @@ extern "C" { * *****************************************************************************/ -#define _strnicmp strncasecmp -#define _stricmp strcasecmp -#define MAX_SIGNATURES 10 // maximum number of DKIM signatures to process/message -#define FDLOG stderr /* writing to another FD requires a method */ +#define _strnicmp strncasecmp +#define _stricmp strcasecmp +#define MAX_SIGNATURES 10 // maximum number of DKIM signatures to process/message +#define FDLOG stderr /* writing to another FD requires a method */ string SigHdr; size_t m_SigHdr; -extern "C" int stralloc_copys(stralloc *,char const *); +extern "C" int stralloc_copys(stralloc *, const char *); -int dig_ascii(char *digascii,unsigned const char *digest,const int len) +int dig_ascii(char *digascii, const unsigned char *digest, const int len) { static const char hextab[] = "0123456789abcdef"; int j; @@ -74,81 +78,70 @@ int dig_ascii(char *digascii,unsigned const char *digest,const int len) } -int _DNSGetTXT(const char *szFQDN,char *Buffer,int nBufLen) +int _DNSGetTXT(const char *szFQDN, char *Buffer, int nBufLen) { - stralloc out = {0}; - stralloc sa = {0}; - Buffer[0] = '\0'; // need to be initialized - - if (!stralloc_copys(&sa,szFQDN)) return -1; - - DNS_INIT - - switch (dns_txt(&out,&sa)) { - case -1: return -1; - case 0: return 0; - } - - if (nBufLen < out.len) - return -2; - - if (!stralloc_0(&out)) return -1; - memcpy(Buffer,out.s,out.len); // Return-by-value; sigh - - return out.len; + stralloc out = {0}; + stralloc sa = {0}; + Buffer[0] = '\0'; // need to be initialized + + if (!stralloc_copys(&sa, szFQDN)) return -1; + + DNS_INIT + + switch (dns_txt(&out, &sa)) { + case -1: return -1; + case 0: return 0; + } + + if (nBufLen < out.len) return -2; + + if (!stralloc_0(&out)) return -1; + memcpy(Buffer, out.s, out.len); // Return-by-value; sigh + + return out.len; } -int _DKIM_ReportResult(const char* ResFile,const char* result,const char* reason) +int _DKIM_ReportResult(const char *ResFile, const char *result, const char *reason) { int len = 0; - FILE* out = fopen(ResFile,"wb+"); + FILE *out = fopen(ResFile, "wb+"); if (out == NULL) return -1; if (result) { len = strlen(result); - fwrite(result,1,len,out); - fwrite("\r",1,1,out); + fwrite(result, 1, len, out); + fwrite("\r", 1, 1, out); } if (reason) { - fwrite(reason,1,strlen(reason),out); - fwrite("\r",1,1,out); + fwrite(reason, 1, strlen(reason), out); + fwrite("\r", 1, 1, out); } fclose(out); return len; } -const char* DKIM_ErrorResult(const int res) +const char *DKIM_ErrorResult(const int res) { - const char* errormsg = ""; + const char *errormsg = ""; switch (res) { - case DKIM_FAIL: - errormsg = " (verify error: message is suspicious)"; - break; - case DKIM_BAD_SYNTAX: - errormsg = " (signature error: could not parse or has bad tags/values)"; - break; - case DKIM_SIGNATURE_BAD: - errormsg = " (signature error: RSA/ED25519 verify failed)"; - break; + case DKIM_FAIL: errormsg = " (verify error: message is suspicious)"; break; + case DKIM_BAD_SYNTAX: errormsg = " (signature error: could not parse or has bad tags/values)"; break; + case DKIM_SIGNATURE_BAD: errormsg = " (signature error: RSA/ED25519 verify failed)"; break; case DKIM_SIGNATURE_BAD_BUT_TESTING: errormsg = " (signature error: RSA/ED25519 verify failed but testing)"; break; - case DKIM_SIGNATURE_EXPIRED: - errormsg = " (signature error: signature x= value expired)"; - break; + case DKIM_SIGNATURE_EXPIRED: errormsg = " (signature error: signature x= value expired)"; break; case DKIM_SELECTOR_INVALID: errormsg = " (signature error: selector doesn't parse or contains invalid values)"; break; case DKIM_SELECTOR_GRANULARITY_MISMATCH: errormsg = " (signature error: selector g= doesn't match i=)"; break; - case DKIM_SELECTOR_KEY_REVOKED: - errormsg = " (signature error: revoked p= empty)"; - break; + case DKIM_SELECTOR_KEY_REVOKED: errormsg = " (signature error: revoked p= empty)"; break; case DKIM_SELECTOR_DOMAIN_NAME_TOO_LONG: errormsg = " (dns error: selector domain name too long to request)"; break; @@ -161,36 +154,26 @@ const char* DKIM_ErrorResult(const int res) case DKIM_SELECTOR_PUBLIC_KEY_INVALID: errormsg = " (signature error: selector p= value invalid or wrong format)"; break; - case DKIM_NO_SIGNATURES: - errormsg = " (process error: no signatures)"; - break; - case DKIM_NO_VALID_SIGNATURES: - errormsg = " (process error: no valid signatures)"; - break; + case DKIM_NO_SIGNATURES: errormsg = " (process error: no signatures)"; break; + case DKIM_NO_VALID_SIGNATURES: errormsg = " (process error: no valid signatures)"; break; case DKIM_BODY_HASH_MISMATCH: errormsg = " (signature verify error: message body does not hash to bh= value)"; break; case DKIM_SELECTOR_ALGORITHM_MISMATCH: errormsg = " (signature error: selector h= doesn't match signature a=)"; break; - case DKIM_STAT_INCOMPAT: - errormsg = " (signature error: incompatible v= value)"; - break; + case DKIM_STAT_INCOMPAT: errormsg = " (signature error: incompatible v= value)"; break; case DKIM_UNSIGNED_FROM: errormsg = " (signature error: not all message's From headers in signature)"; break; - case DKIM_OUT_OF_MEMORY: - errormsg = " (internal error: memory allocation failed)"; - break; + case DKIM_OUT_OF_MEMORY: errormsg = " (internal error: memory allocation failed)"; break; case DKIM_INVALID_CONTEXT: errormsg = " (internal error: DKIMContext structure invalid for this operation)"; break; case DKIM_NO_SENDER: errormsg = " (signing error: Could not find From: or Sender: header in message)"; break; - case DKIM_BAD_PRIVATE_KEY: - errormsg = " (signing error: Could not parse private key)"; - break; + case DKIM_BAD_PRIVATE_KEY: errormsg = " (signing error: Could not parse private key)"; break; case DKIM_BUFFER_TOO_SMALL: errormsg = " (signing error: Buffer passed in is not large enough)"; break; @@ -204,7 +187,9 @@ SignatureInfo::SignatureInfo(bool s) VerifiedBodyCount = 0; UnverifiedBodyCount = 0; -#if ((OPENSSL_VERSION_NUMBER < 0x10100000L) || (LIBRESSL_VERSION_NUMBER > 0 && LIBRESSL_VERSION_NUMBER < 0x20700000L)) +#if ( \ + (OPENSSL_VERSION_NUMBER < 0x10100000L) \ + || (LIBRESSL_VERSION_NUMBER > 0 && LIBRESSL_VERSION_NUMBER < 0x20700000L)) EVP_MD_CTX_init(&m_Hdr_ctx); EVP_MD_CTX_init(&m_Bdy_ctx); #else @@ -223,12 +208,14 @@ SignatureInfo::SignatureInfo(bool s) SignatureInfo::~SignatureInfo() { -#if ((OPENSSL_VERSION_NUMBER < 0x10100000L) || (LIBRESSL_VERSION_NUMBER > 0 && LIBRESSL_VERSION_NUMBER < 0x20700000L)) +#if ( \ + (OPENSSL_VERSION_NUMBER < 0x10100000L) \ + || (LIBRESSL_VERSION_NUMBER > 0 && LIBRESSL_VERSION_NUMBER < 0x20700000L)) EVP_MD_CTX_cleanup(&m_Hdr_ctx); EVP_MD_CTX_cleanup(&m_Bdy_ctx); #else /** FIXME: No free but reset ! **/ - EVP_MD_CTX_reset(m_Hdr_ctx); + EVP_MD_CTX_reset(m_Hdr_ctx); EVP_MD_CTX_reset(m_Bdy_ctx); #endif #if (OPENSSL_VERSION_NUMBER > 0x10101000L) @@ -242,26 +229,23 @@ inline bool isswsp(char ch) } //////////////////////////////////////////////////////////////////////////////// -// +// // Parse a DKIM tag-list. Returns true for success // //////////////////////////////////////////////////////////////////////////////// -bool ParseTagValueList(char *tagvaluelist,const char *wanted[],char *values[]) +bool ParseTagValueList(char *tagvaluelist, const char *wanted[], char *values[]) { char *s = tagvaluelist; for (;;) { // skip whitespace - while (isswsp(*s)) - s++; + while (isswsp(*s)) s++; // if at the end of the string, return success. Note: this allows a list with no entries - if (*s == '\0') - return true; + if (*s == '\0') return true; // get tag name - if (!isalpha(*s)) - return false; + if (!isalpha(*s)) return false; char *tag = s; do { @@ -271,26 +255,22 @@ bool ParseTagValueList(char *tagvaluelist,const char *wanted[],char *values[]) char *endtag = s; // skip whitespace before equals - while (isswsp(*s)) - s++; + while (isswsp(*s)) s++; // next character must be equals - if (*s != '=') - return false; + if (*s != '=') return false; s++; // null-terminate tag name *endtag = '\0'; // skip whitespace after equals - while (isswsp(*s)) - s++; + while (isswsp(*s)) s++; // get tag value char *value = s; - while (*s != ';' && ((*s == '\t' || *s == '\r' || *s == '\n') || (*s >= ' ' && *s <= '~'))) - s++; + while (*s != ';' && ((*s == '\t' || *s == '\r' || *s == '\n') || (*s >= ' ' && *s <= '~'))) s++; char *e = s; @@ -299,31 +279,27 @@ bool ParseTagValueList(char *tagvaluelist,const char *wanted[],char *values[]) if (*s == '\0') done = true; else { - if (*s != ';') - return false; + if (*s != ';') return false; s++; } // skip backwards past any trailing whitespace - while (e > value && isswsp(e[-1])) - e--; + while (e > value && isswsp(e[-1])) e--; // null-terminate tag value *e = '\0'; // check to see if we want this tag for (unsigned i = 0; wanted[i] != NULL; i++) { - if (strcmp(wanted[i],tag) == 0) { + if (strcmp(wanted[i], tag) == 0) { // return failure if we already have a value for this tag (duplicates not allowed) - if (values[i] != NULL) - return false; + if (values[i] != NULL) return false; values[i] = value; break; } } - if (done) - return true; + if (done) return true; } } //////////////////////////////////////////////////////////////////////////////// @@ -345,18 +321,16 @@ char Tohex(char ch) } } //////////////////////////////////////////////////////////////////////////////// -// +// // Decode quoted printable string in-place // //////////////////////////////////////////////////////////////////////////////// -void DecodeQuotedPrintable(char* ptr) +void DecodeQuotedPrintable(char *ptr) { char *s = ptr; - while (*s != '\0' && *s != '=') - s++; + while (*s != '\0' && *s != '=') s++; - if (*s == '\0') - return; + if (*s == '\0') return; char *d = s; do { @@ -370,30 +344,33 @@ void DecodeQuotedPrintable(char* ptr) *d = '\0'; } //////////////////////////////////////////////////////////////////////////////// -// +// // Decode base64 string in-place, returns number of bytes output // //////////////////////////////////////////////////////////////////////////////// unsigned DecodeBase64(char *ptr) { static const char base64_table[256] = { - -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, - -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63,52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-1,-1,-1, - -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1, - -1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1, - -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, - -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, - -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, - -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1}; - - unsigned char* s = (unsigned char* )ptr; - unsigned char* d = (unsigned char* )ptr; + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, + 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, + 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, + -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, + 49, 50, 51, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}; + + unsigned char *s = (unsigned char *)ptr; + unsigned char *d = (unsigned char *)ptr; unsigned b64accum = 0; unsigned char b64shift = 0; while (*s != '\0') { unsigned char value = base64_table[*s++]; - if ((signed char) value >= 0) { + if ((signed char)value >= 0) { b64accum = (b64accum << 6) | value; b64shift += 6; if (b64shift >= 8) { @@ -403,11 +380,11 @@ unsigned DecodeBase64(char *ptr) } } - return (char* )d-ptr; + return (char *)d - ptr; } //////////////////////////////////////////////////////////////////////////////// -// +// // Match a string with a pattern (used for g= value) // Supports a single, optional "*" wildcard character. // @@ -415,35 +392,34 @@ unsigned DecodeBase64(char *ptr) bool WildcardMatch(const char *p, const char *s) { // special case: An empty "g=" value never matches any addresses - if (*p == '\0') - return false; + if (*p == '\0') return false; - const char* wildcard = strchr(p,'*'); + const char *wildcard = strchr(p, '*'); if (wildcard == NULL) { return strcmp(s, p) == 0; } else { unsigned beforewildcardlen = wildcard - p; unsigned afterwildcardlen = strlen(wildcard + 1); unsigned slen = strlen(s); - return (slen >= beforewildcardlen + afterwildcardlen) && - (strncmp(s,p,beforewildcardlen) == 0) && strcmp(s + slen - afterwildcardlen,wildcard + 1) == 0; + return (slen >= beforewildcardlen + afterwildcardlen) && (strncmp(s, p, beforewildcardlen) == 0) + && strcmp(s + slen - afterwildcardlen, wildcard + 1) == 0; } } //////////////////////////////////////////////////////////////////////////////// -// +// // Parse addresses from a string. Returns true if at least one address found // //////////////////////////////////////////////////////////////////////////////// -bool ParseAddresses(string str,vector<string> &Addresses) +bool ParseAddresses(string str, vector<string>& Addresses) { - char* s = (char* )str.c_str(); + char *s = (char *)str.c_str(); while (*s != '\0') { - char* start = s; - char* from = s; - char* to = s; - char* lt = NULL; // pointer to less than character (<) which starts the address if found + char *start = s; + char *from = s; + char *to = s; + char *lt = NULL; // pointer to less than character (<) which starts the address if found while (*from != '\0') { if (*from == '(') { @@ -459,16 +435,14 @@ bool ParseAddresses(string str,vector<string> &Addresses) else if (*from == '\\' && from[1] != '\0') from++; } - } - else if (*from == ')') { + } else if (*from == ')') { // ignore closing parenthesis outside of comment from++; } else if (*from == ',' || *from == ';') { // comma/semicolon ends the address from++; break; - } - else if (*from == ' ' || *from == '\t' || *from == '\r' || *from == '\n') { + } else if (*from == ' ' || *from == '\t' || *from == '\r' || *from == '\n') { // ignore whitespace from++; } else if (*from == '"') { @@ -490,8 +464,7 @@ bool ParseAddresses(string str,vector<string> &Addresses) // copy any other char *to = *from++; // save pointer to '<' for later... - if (*to == '<') - lt = to; + if (*to == '<') lt = to; to++; } } @@ -500,22 +473,20 @@ bool ParseAddresses(string str,vector<string> &Addresses) // if there's < > get what's inside if (lt != NULL) { - start = lt+1; + start = lt + 1; char *gt = strchr(start, '>'); - if (gt != NULL) - *gt = '\0'; + if (gt != NULL) *gt = '\0'; } else { // look for and strip group name char *colon = strchr(start, ':'); if (colon != NULL) { char *at = strchr(start, '@'); - if (at == NULL || colon < at) - start = colon+1; + if (at == NULL || colon < at) start = colon + 1; } } if (*start != '\0' && strchr(start, '@') != NULL) { - Addresses.push_back(start); // save address + Addresses.push_back(start); // save address } s = from; @@ -529,24 +500,24 @@ bool ParseAddresses(string str,vector<string> &Addresses) CDKIMVerify::CDKIMVerify() { m_pfnSelectorCallback = NULL; -// m_pfnPracticesCallback = NULL; + // m_pfnPracticesCallback = NULL; m_HonorBodyLengthTag = false; m_CheckPractices = false; -// Kai: -// m_SubjectIsRequired = true; + // Kai: + // m_SubjectIsRequired = true; m_SubjectIsRequired = false; m_SaveCanonicalizedData = false; m_AllowUnsignedFromHeaders = false; } -CDKIMVerify::~CDKIMVerify() {} // Destructor +CDKIMVerify::~CDKIMVerify() {} // Destructor //////////////////////////////////////////////////////////////////////////////// -// +// // Init - save the options // //////////////////////////////////////////////////////////////////////////////// -int CDKIMVerify::Init(DKIMVerifyOptions* pOptions) +int CDKIMVerify::Init(DKIMVerifyOptions *pOptions) { int nRet = CDKIMBase::Init(); @@ -555,8 +526,8 @@ int CDKIMVerify::Init(DKIMVerifyOptions* pOptions) m_HonorBodyLengthTag = pOptions->nHonorBodyLengthTag != 0; m_CheckPractices = pOptions->nCheckPractices != 0; -// Kai: -// m_SubjectIsRequired = pOptions->nSubjectRequired == 0; + // Kai: + // m_SubjectIsRequired = pOptions->nSubjectRequired == 0; m_SubjectIsRequired = pOptions->nSubjectRequired == 1; m_SaveCanonicalizedData = pOptions->nSaveCanonicalizedData != 0; m_AllowUnsignedFromHeaders = pOptions->nAllowUnsignedFromHeaders != 0; @@ -565,7 +536,7 @@ int CDKIMVerify::Init(DKIMVerifyOptions* pOptions) } //////////////////////////////////////////////////////////////////////////////// -// +// // GetResults - return the pass/fail/neutral verification result // //////////////////////////////////////////////////////////////////////////////// @@ -573,7 +544,7 @@ int CDKIMVerify::GetResults(void) { // char mdi[128]; // char digi[128]; - + ProcessFinal(); unsigned char *SignMsg; @@ -590,16 +561,18 @@ int CDKIMVerify::GetResults(void) unsigned char md[EVP_MAX_MD_SIZE]; unsigned len = 0; -#if ((OPENSSL_VERSION_NUMBER < 0x10100000L) || (LIBRESSL_VERSION_NUMBER > 0 && LIBRESSL_VERSION_NUMBER < 0x20700000L)) - res = EVP_DigestFinal(&i->m_Bdy_ctx,md,&len); +#if ( \ + (OPENSSL_VERSION_NUMBER < 0x10100000L) \ + || (LIBRESSL_VERSION_NUMBER > 0 && LIBRESSL_VERSION_NUMBER < 0x20700000L)) + res = EVP_DigestFinal(&i->m_Bdy_ctx, md, &len); #else - res = EVP_DigestFinal_ex(i->m_Bdy_ctx,md,&len); + res = EVP_DigestFinal_ex(i->m_Bdy_ctx, md, &len); EVP_MD_CTX_reset(i->m_Bdy_ctx); #endif // dig_ascii(digi,md,32); // dig_ascii(mdi,(unsigned const char *)i->BodyHashData.data(),32); - if (!res || len != i->BodyHashData.length() || memcmp(i->BodyHashData.data(),md,len) != 0) { + if (!res || len != i->BodyHashData.length() || memcmp(i->BodyHashData.data(), md, len) != 0) { // body hash mismatch // if the selector is in testing mode... @@ -614,7 +587,7 @@ int CDKIMVerify::GetResults(void) } } else { // hash CRLF separating the body from the signature - i->Hash("\r\n",2); + i->Hash("\r\n", 2); } // SECOND: Fetch the signature @@ -622,39 +595,53 @@ int CDKIMVerify::GetResults(void) string sSignedSig = i->Header; string sSigValue = sSignedSig.substr(sSignedSig.find(':') + 1); - static const char* tags[] = {"b",NULL}; - char* values[sizeof(tags)/sizeof(tags[0])] = {NULL}; + static const char *tags[] = {"b", NULL}; + char *values[sizeof(tags) / sizeof(tags[0])] = {NULL}; - char* pSigValue = (char* ) sSigValue.c_str(); // our signature - if (ParseTagValueList(pSigValue,tags,values) && values[0] != NULL) { - sSignedSig.erase(15 + values[0] - pSigValue,strlen(values[0])); + char *pSigValue = (char *)sSigValue.c_str(); // our signature + if (ParseTagValueList(pSigValue, tags, values) && values[0] != NULL) { + sSignedSig.erase(15 + values[0] - pSigValue, strlen(values[0])); } if (i->HeaderCanonicalization == DKIM_CANON_RELAXED) { sSignedSig = RelaxHeader(sSignedSig); - } - else if (i->HeaderCanonicalization == DKIM_CANON_NOWSP) { + } else if (i->HeaderCanonicalization == DKIM_CANON_NOWSP) { RemoveSWSP(sSignedSig); // convert "DKIM-Signature" to lower case - sSignedSig.replace(0,14,"dkim-signature",14); + sSignedSig.replace(0, 14, "dkim-signature", 14); } - i->Hash(sSignedSig.c_str(),sSignedSig.length()); // include generated DKIM signature header + i->Hash(sSignedSig.c_str(), sSignedSig.length()); // include generated DKIM signature header assert(i->m_pSelector != NULL); - if (EVP_PKEY_base_id(i->m_pSelector->PublicKey) != EVP_PKEY_ED25519) -#if ((OPENSSL_VERSION_NUMBER < 0x10100000L) || (LIBRESSL_VERSION_NUMBER > 0 && LIBRESSL_VERSION_NUMBER < 0x20700000L)) - res = EVP_VerifyFinal(&i->m_Hdr_ctx,(unsigned char *)i->SignatureData.data(),i->SignatureData.length(),i->m_pSelector->PublicKey); + if (EVP_PKEY_base_id(i->m_pSelector->PublicKey) != EVP_PKEY_ED25519) +#if ( \ + (OPENSSL_VERSION_NUMBER < 0x10100000L) \ + || (LIBRESSL_VERSION_NUMBER > 0 && LIBRESSL_VERSION_NUMBER < 0x20700000L)) + res = EVP_VerifyFinal( + &i->m_Hdr_ctx, + (unsigned char *)i->SignatureData.data(), + i->SignatureData.length(), + i->m_pSelector->PublicKey); #else - res = EVP_VerifyFinal(i->m_Hdr_ctx,(unsigned char *)i->SignatureData.data(),i->SignatureData.length(),i->m_pSelector->PublicKey); + res = EVP_VerifyFinal( + i->m_Hdr_ctx, + (unsigned char *)i->SignatureData.data(), + i->SignatureData.length(), + i->m_pSelector->PublicKey); #endif #if (OPENSSL_VERSION_NUMBER > 0x10101000L) else if (EVP_PKEY_base_id(i->m_pSelector->PublicKey) == EVP_PKEY_ED25519) { - EVP_DigestVerifyInit(i->m_Msg_ctx,NULL,NULL,NULL,i->m_pSelector->PublicKey); // late initialization - - SignMsg = (unsigned char *) SigHdr.data(); - res = EVP_DigestVerify(i->m_Msg_ctx,(unsigned char *)i->SignatureData.data(),(size_t)i->SignatureData.length(), - SignMsg,m_SigHdr); + EVP_DigestVerifyInit( + i->m_Msg_ctx, NULL, NULL, NULL, i->m_pSelector->PublicKey); // late initialization + + SignMsg = (unsigned char *)SigHdr.data(); + res = EVP_DigestVerify( + i->m_Msg_ctx, + (unsigned char *)i->SignatureData.data(), + (size_t)i->SignatureData.length(), + SignMsg, + m_SigHdr); } #endif @@ -675,25 +662,25 @@ int CDKIMVerify::GetResults(void) RealFailures++; } } - } else if (i->Status == DKIM_SELECTOR_GRANULARITY_MISMATCH || - i->Status == DKIM_SELECTOR_ALGORITHM_MISMATCH || - i->Status == DKIM_SELECTOR_KEY_REVOKED) { + } else if ( + i->Status == DKIM_SELECTOR_GRANULARITY_MISMATCH || i->Status == DKIM_SELECTOR_ALGORITHM_MISMATCH + || i->Status == DKIM_SELECTOR_KEY_REVOKED) + { // treat these as failures // todo: maybe see if the selector is in testing mode? RealFailures++; } - } // loop over signature infos done + } // loop over signature infos done // get the From address's domain if we might need it string sFromDomain; if (SuccessCount > 0 || m_CheckPractices) { for (list<string>::iterator i = HeaderList.begin(); i != HeaderList.end(); ++i) { - if (_strnicmp(i->c_str(),"From",4) == 0) { + if (_strnicmp(i->c_str(), "From", 4) == 0) { // skip over whitespace between the header name and : - const char* s = i->c_str() + 4; - while (*s == ' ' || *s == '\t') - s++; + const char *s = i->c_str() + 4; + while (*s == ' ' || *s == '\t') s++; if (*s == ':') { vector<string> Addresses; if (ParseAddresses(s + 1, Addresses)) { @@ -705,17 +692,17 @@ int CDKIMVerify::GetResults(void) } } } - + // if a signature from the From domain verified successfully, return success now // without checking the author domain signing practices if (SuccessCount > 0 && !sFromDomain.empty()) { for (list<string>::iterator i = SuccessfulDomains.begin(); i != SuccessfulDomains.end(); ++i) { // see if the successful domain is the same as or a parent of the From domain - if (i->length() > sFromDomain.length()) - continue; - if (_stricmp(i->c_str(),sFromDomain.c_str() + sFromDomain.length() - i->length()) != 0) - continue; - if (i->length() == sFromDomain.length() || sFromDomain.c_str()[sFromDomain.length() - i->length() - 1] == '.') { + if (i->length() > sFromDomain.length()) continue; + if (_stricmp(i->c_str(), sFromDomain.c_str() + sFromDomain.length() - i->length()) != 0) continue; + if (i->length() == sFromDomain.length() + || sFromDomain.c_str()[sFromDomain.length() - i->length() - 1] == '.') + { return SuccessCount == Signatures.size() ? DKIM_SUCCESS : DKIM_PARTIAL_SUCCESS; } } @@ -728,11 +715,11 @@ int CDKIMVerify::GetResults(void) } //////////////////////////////////////////////////////////////////////////////// -// +// // Hash - update the hash or update the Ed25519 signature input // //////////////////////////////////////////////////////////////////////////////// -void SignatureInfo::Hash(const char* szBuffer,unsigned nBufLength,bool IsBody) +void SignatureInfo::Hash(const char *szBuffer, unsigned nBufLength, bool IsBody) { #if 0 /** START DEBUG CODE **/ @@ -748,7 +735,7 @@ void SignatureInfo::Hash(const char* szBuffer,unsigned nBufLength,bool IsBody) /** END DEBUG CODE **/ #endif - if (IsBody && BodyLength != (unsigned) -1) { // trick: 2's complement + if (IsBody && BodyLength != (unsigned)-1) { // trick: 2's complement VerifiedBodyCount += nBufLength; if (VerifiedBodyCount > BodyLength) { nBufLength = BodyLength - (VerifiedBodyCount - nBufLength); @@ -759,29 +746,31 @@ void SignatureInfo::Hash(const char* szBuffer,unsigned nBufLength,bool IsBody) } if (IsBody && !BodyHashData.empty()) { -#if ((OPENSSL_VERSION_NUMBER < 0x10100000L) || (LIBRESSL_VERSION_NUMBER > 0 && LIBRESSL_VERSION_NUMBER < 0x20700000L)) - EVP_DigestUpdate(&m_Bdy_ctx,szBuffer,nBufLength); +#if ( \ + (OPENSSL_VERSION_NUMBER < 0x10100000L) \ + || (LIBRESSL_VERSION_NUMBER > 0 && LIBRESSL_VERSION_NUMBER < 0x20700000L)) + EVP_DigestUpdate(&m_Bdy_ctx, szBuffer, nBufLength); } else { - EVP_VerifyUpdate(&m_Hdr_ctx,szBuffer,nBufLength); + EVP_VerifyUpdate(&m_Hdr_ctx, szBuffer, nBufLength); #else - EVP_DigestUpdate(m_Bdy_ctx,szBuffer,nBufLength); + EVP_DigestUpdate(m_Bdy_ctx, szBuffer, nBufLength); } else { - EVP_VerifyUpdate(m_Hdr_ctx,szBuffer,nBufLength); + EVP_VerifyUpdate(m_Hdr_ctx, szBuffer, nBufLength); #endif #if (OPENSSL_VERSION_NUMBER > 0x10101000L) - SigHdr.append(szBuffer,nBufLength); + SigHdr.append(szBuffer, nBufLength); m_SigHdr += nBufLength; #endif } if (m_SaveCanonicalizedData) { - CanonicalizedData.append(szBuffer,nBufLength); + CanonicalizedData.append(szBuffer, nBufLength); } } //////////////////////////////////////////////////////////////////////////////// -// +// // ProcessHeaders - Look for DKIM-Signatures and start processing them // look for DKIM-Signature header(s) // @@ -789,49 +778,46 @@ void SignatureInfo::Hash(const char* szBuffer,unsigned nBufLength,bool IsBody) int CDKIMVerify::ProcessHeaders(void) { for (list<string>::iterator i = HeaderList.begin(); i != HeaderList.end(); ++i) { - if (strlen(i->c_str()) < 14) continue; // too short - if (_strnicmp(i->c_str(),"DKIM-Signature",14) == 0) { + if (strlen(i->c_str()) < 14) continue; // too short + if (_strnicmp(i->c_str(), "DKIM-Signature", 14) == 0) { // skip over whitespace between the header name and : const char *s = i->c_str() + 14; - while (*s == ' ' || *s == '\t') - s++; + while (*s == ' ' || *s == '\t') s++; if (*s == ':') { // found SignatureInfo sig(m_SaveCanonicalizedData); - sig.Status = ParseDKIMSignature(*i,sig); - Signatures.push_back(sig); // save signature + sig.Status = ParseDKIMSignature(*i, sig); + Signatures.push_back(sig); // save signature - if (Signatures.size() >= MAX_SIGNATURES) - break; + if (Signatures.size() >= MAX_SIGNATURES) break; } } } - if (Signatures.empty()) - return DKIM_NO_SIGNATURES; + if (Signatures.empty()) return DKIM_NO_SIGNATURES; bool ValidSigFound = false; for (list<SignatureInfo>::iterator s = Signatures.begin(); s != Signatures.end(); ++s) { - SignatureInfo &sig = *s; + SignatureInfo& sig = *s; if (sig.Status != DKIM_SUCCESS) continue; - SelectorInfo &sel = GetSelector(sig.Selector,sig.Domain); + SelectorInfo& sel = GetSelector(sig.Selector, sig.Domain); sig.m_pSelector = &sel; if (sel.Status != DKIM_SUCCESS) { sig.Status = sel.Status; } else { // check the granularity - if (!WildcardMatch(sel.Granularity.c_str(),sig.IdentityLocalPart.c_str())) - sig.Status = DKIM_SELECTOR_GRANULARITY_MISMATCH; // this error causes the signature to fail + if (!WildcardMatch(sel.Granularity.c_str(), sig.IdentityLocalPart.c_str())) + sig.Status = DKIM_SELECTOR_GRANULARITY_MISMATCH; // this error causes the signature to fail // check the hash algorithm - if ((sig.m_nHash == DKIM_HASH_SHA1 && !sel.AllowSHA1) || - (sig.m_nHash == DKIM_HASH_SHA256 && !sel.AllowSHA256)) - sig.Status = DKIM_SELECTOR_ALGORITHM_MISMATCH; // causes signature to fail + if ((sig.m_nHash == DKIM_HASH_SHA1 && !sel.AllowSHA1) + || (sig.m_nHash == DKIM_HASH_SHA256 && !sel.AllowSHA256)) + sig.Status = DKIM_SELECTOR_ALGORITHM_MISMATCH; // causes signature to fail // check for same domain - if (sel.SameDomain && _stricmp(sig.Domain.c_str(),sig.IdentityDomain.c_str()) != 0) + if (sel.SameDomain && _stricmp(sig.Domain.c_str(), sig.IdentityDomain.c_str()) != 0) sig.Status = DKIM_BAD_SYNTAX; } @@ -839,23 +825,27 @@ int CDKIMVerify::ProcessHeaders(void) // initialize the hashes if (sig.m_nHash == DKIM_HASH_SHA1) { -#if ((OPENSSL_VERSION_NUMBER < 0x10100000L) || (LIBRESSL_VERSION_NUMBER > 0 && LIBRESSL_VERSION_NUMBER < 0x20700000L)) - EVP_VerifyInit(&sig.m_Hdr_ctx,EVP_sha1()); - EVP_DigestInit(&sig.m_Bdy_ctx,EVP_sha1()); +#if ( \ + (OPENSSL_VERSION_NUMBER < 0x10100000L) \ + || (LIBRESSL_VERSION_NUMBER > 0 && LIBRESSL_VERSION_NUMBER < 0x20700000L)) + EVP_VerifyInit(&sig.m_Hdr_ctx, EVP_sha1()); + EVP_DigestInit(&sig.m_Bdy_ctx, EVP_sha1()); #else - EVP_VerifyInit_ex(sig.m_Hdr_ctx,EVP_sha1(),NULL); - EVP_DigestInit_ex(sig.m_Bdy_ctx,EVP_sha1(),NULL); + EVP_VerifyInit_ex(sig.m_Hdr_ctx, EVP_sha1(), NULL); + EVP_DigestInit_ex(sig.m_Bdy_ctx, EVP_sha1(), NULL); #endif } if (sig.m_nHash == DKIM_HASH_SHA256) { -#if ((OPENSSL_VERSION_NUMBER < 0x10100000L) || (LIBRESSL_VERSION_NUMBER > 0 && LIBRESSL_VERSION_NUMBER < 0x20700000L)) - EVP_VerifyInit(&sig.m_Hdr_ctx,EVP_sha256()); - EVP_DigestInit(&sig.m_Bdy_ctx,EVP_sha256()); +#if ( \ + (OPENSSL_VERSION_NUMBER < 0x10100000L) \ + || (LIBRESSL_VERSION_NUMBER > 0 && LIBRESSL_VERSION_NUMBER < 0x20700000L)) + EVP_VerifyInit(&sig.m_Hdr_ctx, EVP_sha256()); + EVP_DigestInit(&sig.m_Bdy_ctx, EVP_sha256()); #else - EVP_VerifyInit_ex(sig.m_Hdr_ctx,EVP_sha256(),NULL); - EVP_DigestInit_ex(sig.m_Bdy_ctx,EVP_sha256(),NULL); + EVP_VerifyInit_ex(sig.m_Hdr_ctx, EVP_sha256(), NULL); + EVP_DigestInit_ex(sig.m_Bdy_ctx, EVP_sha256(), NULL); #endif -#if (OPENSSL_VERSION_NUMBER > 0x10101000L) +#if (OPENSSL_VERSION_NUMBER > 0x10101000L) SigHdr.assign(""); m_SigHdr = 0; } @@ -867,13 +857,11 @@ int CDKIMVerify::ProcessHeaders(void) for (vector<string>::iterator x = sig.SignedHeaders.begin(); x != sig.SignedHeaders.end(); ++x) { list<string>::reverse_iterator i; for (i = HeaderList.rbegin(); i != HeaderList.rend(); ++i) { - if (_strnicmp(i->c_str(),x->c_str(),x->length()) == 0) { + if (_strnicmp(i->c_str(), x->c_str(), x->length()) == 0) { // skip over whitespace between the header name and : - const char* s = i->c_str()+x->length(); - while (*s == ' ' || *s == '\t') - s++; - if (*s == ':' && find(used.begin(),used.end(),i) == used.end()) - break; + const char *s = i->c_str() + x->length(); + while (*s == ' ' || *s == '\t') s++; + if (*s == ':' && find(used.begin(), used.end(), i) == used.end()) break; } } @@ -882,41 +870,39 @@ int CDKIMVerify::ProcessHeaders(void) // hash this header if (sig.HeaderCanonicalization == DKIM_CANON_SIMPLE) { - sig.Hash(i->c_str(),i->length()); + sig.Hash(i->c_str(), i->length()); } else if (sig.HeaderCanonicalization == DKIM_CANON_RELAXED) { string sTemp = RelaxHeader(*i); - sig.Hash(sTemp.c_str(),sTemp.length()); + sig.Hash(sTemp.c_str(), sTemp.length()); } else if (sig.HeaderCanonicalization == DKIM_CANON_NOWSP) { string sTemp = *i; RemoveSWSP(sTemp); // convert characters before ':' to lower case - for (char* s = (char*)sTemp.c_str(); *s != '\0' && *s != ':'; s++) { - if (*s >= 'A' && *s <= 'Z') - *s += 'a' - 'A'; + for (char *s = (char *)sTemp.c_str(); *s != '\0' && *s != ':'; s++) { + if (*s >= 'A' && *s <= 'Z') *s += 'a' - 'A'; } - sig.Hash(sTemp.c_str(),sTemp.length()); + sig.Hash(sTemp.c_str(), sTemp.length()); } - sig.Hash("\r\n",2); + sig.Hash("\r\n", 2); } } if (sig.BodyHashData.empty()) { // hash CRLF separating headers from body - sig.Hash("\r\n",2); + sig.Hash("\r\n", 2); } if (!m_AllowUnsignedFromHeaders) { // make sure the message has no unsigned From headers list<string>::reverse_iterator i; for (i = HeaderList.rbegin(); i != HeaderList.rend(); ++i) { - if (_strnicmp(i->c_str(),"From",4) == 0) { + if (_strnicmp(i->c_str(), "From", 4) == 0) { // skip over whitespace between the header name and : const char *s = i->c_str() + 4; - while (*s == ' ' || *s == '\t') - s++; + while (*s == ' ' || *s == '\t') s++; if (*s == ':') { - if (find(used.begin(),used.end(),i) == used.end()) { + if (find(used.begin(), used.end(), i) == used.end()) { // this From header was not signed break; } @@ -933,8 +919,7 @@ int CDKIMVerify::ProcessHeaders(void) ValidSigFound = true; } - if (!ValidSigFound) - return DKIM_NO_VALID_SIGNATURES; + if (!ValidSigFound) return DKIM_NO_VALID_SIGNATURES; return DKIM_SUCCESS; } @@ -952,12 +937,10 @@ bool ParseUnsigned(const char *s, unsigned *result) bool overflowed = false; do { - if (*s < '0' || *s > '9') - return false; // returns false for an initial '\0' + if (*s < '0' || *s > '9') return false; // returns false for an initial '\0' temp = temp * 10 + (*s - '0'); - if (temp < last) - overflowed = true; + if (temp < last) overflowed = true; last = temp; s++; @@ -969,11 +952,11 @@ bool ParseUnsigned(const char *s, unsigned *result) //////////////////////////////////////////////////////////////////////////////// -// +// // ParseDKIMSignature - Parse a DKIM-Signature header field // //////////////////////////////////////////////////////////////////////////////// -int CDKIMVerify::ParseDKIMSignature(const string& sHeader,SignatureInfo &sig) +int CDKIMVerify::ParseDKIMSignature(const string& sHeader, SignatureInfo& sig) { // for strtok_r() char *saveptr; @@ -983,77 +966,72 @@ int CDKIMVerify::ParseDKIMSignature(const string& sHeader,SignatureInfo &sig) string sValue = sHeader.substr(sHeader.find(':') + 1); - static const char *tags[] = {"v","a","b","d","h","s","c","i","l","q","t","x","bh",NULL}; - char *values[sizeof(tags)/sizeof(tags[0])] = {NULL}; + static const char *tags[] = {"v", "a", "b", "d", "h", "s", "c", "i", "l", "q", "t", "x", "bh", NULL}; + char *values[sizeof(tags) / sizeof(tags[0])] = {NULL}; - if (!ParseTagValueList((char*) sValue.c_str(),tags,values)) - return DKIM_BAD_SYNTAX; + if (!ParseTagValueList((char *)sValue.c_str(), tags, values)) return DKIM_BAD_SYNTAX; // check signature version if (values[0] == NULL) return DKIM_BAD_SYNTAX; - + // signature MUST have a=, b=, d=, h=, s= - if (values[1] == NULL || values[2] == NULL || values[3] == NULL || values[4] == NULL || values[5] == NULL) + if (values[1] == NULL || values[2] == NULL || values[3] == NULL || values[4] == NULL + || values[5] == NULL) return DKIM_BAD_SYNTAX; // algorithm ('a=') can be "rsa-sha1" or "rsa-sha256" or "ed25519" - if (strcmp(values[1],"rsa-sha1") == 0) { + if (strcmp(values[1], "rsa-sha1") == 0) { sig.m_nHash = DKIM_HASH_SHA1; - } else if (strcmp(values[1],"rsa-sha256") == 0) { + } else if (strcmp(values[1], "rsa-sha256") == 0) { sig.m_nHash = DKIM_HASH_SHA256; -#if (OPENSSL_VERSION_NUMBER > 0x10101000L) - } else if (strcmp(values[1],"ed25519-sha256") == 0) { +#if (OPENSSL_VERSION_NUMBER > 0x10101000L) + } else if (strcmp(values[1], "ed25519-sha256") == 0) { sig.m_nHash = DKIM_HASH_SHA256; #endif } else { - return DKIM_BAD_SYNTAX; // todo: maybe create a new error code for unknown algorithm + return DKIM_BAD_SYNTAX; // todo: maybe create a new error code for unknown algorithm } // make sure the signature data is not empty: b=[...] unsigned SigDataLen = DecodeBase64(values[2]); - if (SigDataLen == 0) - return DKIM_BAD_SYNTAX; + if (SigDataLen == 0) return DKIM_BAD_SYNTAX; - sig.SignatureData.assign(values[2],SigDataLen); + sig.SignatureData.assign(values[2], SigDataLen); // check for body hash in DKIM header: bh=[...]; unsigned BodyHashLen = DecodeBase64(values[12]); if (BodyHashLen == 0) return DKIM_BAD_SYNTAX; - sig.BodyHashData.assign(values[12],BodyHashLen); + sig.BodyHashData.assign(values[12], BodyHashLen); // domain must not be empty - if (*values[3] == '\0') - return DKIM_BAD_SYNTAX; + if (*values[3] == '\0') return DKIM_BAD_SYNTAX; sig.Domain = values[3]; // signed headers must not be empty (more verification is done later) - if (*values[4] == '\0') - return DKIM_BAD_SYNTAX; + if (*values[4] == '\0') return DKIM_BAD_SYNTAX; // selector must not be empty - if (*values[5] == '\0') - return DKIM_BAD_SYNTAX; + if (*values[5] == '\0') return DKIM_BAD_SYNTAX; sig.Selector = values[5]; // canonicalization if (values[6] == NULL) { sig.HeaderCanonicalization = sig.BodyCanonicalization = DKIM_CANON_SIMPLE; } else { - char* slash = strchr(values[6],'/'); - if (slash != NULL) - *slash = '\0'; + char *slash = strchr(values[6], '/'); + if (slash != NULL) *slash = '\0'; - if (strcmp(values[6],"simple") == 0) + if (strcmp(values[6], "simple") == 0) sig.HeaderCanonicalization = DKIM_CANON_SIMPLE; - else if (strcmp(values[6],"relaxed") == 0) + else if (strcmp(values[6], "relaxed") == 0) sig.HeaderCanonicalization = DKIM_CANON_RELAXED; else return DKIM_BAD_SYNTAX; - if (slash == NULL || strcmp(slash + 1,"simple") == 0) + if (slash == NULL || strcmp(slash + 1, "simple") == 0) sig.BodyCanonicalization = DKIM_CANON_SIMPLE; - else if (strcmp(slash + 1,"relaxed") == 0) + else if (strcmp(slash + 1, "relaxed") == 0) sig.BodyCanonicalization = DKIM_CANON_RELAXED; else return DKIM_BAD_SYNTAX; @@ -1068,25 +1046,21 @@ int CDKIMVerify::ParseDKIMSignature(const string& sHeader,SignatureInfo &sig) DecodeQuotedPrintable(values[7]); // must have a '@' separating the local part from the domain - char* at = strchr(values[7],'@'); - if (at == NULL) - return DKIM_BAD_SYNTAX; + char *at = strchr(values[7], '@'); + if (at == NULL) return DKIM_BAD_SYNTAX; *at = '\0'; - char* ilocalpart = values[7]; - char* idomain = at + 1; + char *ilocalpart = values[7]; + char *idomain = at + 1; // i= domain must be the same as or a subdomain of the d= domain int idomainlen = strlen(idomain); int ddomainlen = strlen(values[3]); // todo: maybe create a new error code for invalid identity domain - if (idomainlen < ddomainlen) - return DKIM_BAD_SYNTAX; - if (_stricmp(idomain + idomainlen - ddomainlen,values[3]) != 0) - return DKIM_BAD_SYNTAX; - if (idomainlen > ddomainlen && idomain[idomainlen - ddomainlen - 1] != '.') - return DKIM_BAD_SYNTAX; + if (idomainlen < ddomainlen) return DKIM_BAD_SYNTAX; + if (_stricmp(idomain + idomainlen - ddomainlen, values[3]) != 0) return DKIM_BAD_SYNTAX; + if (idomainlen > ddomainlen && idomain[idomainlen - ddomainlen - 1] != '.') return DKIM_BAD_SYNTAX; sig.IdentityLocalPart = ilocalpart; sig.IdentityDomain = idomain; @@ -1094,83 +1068,76 @@ int CDKIMVerify::ParseDKIMSignature(const string& sHeader,SignatureInfo &sig) // body count if (values[8] == NULL || !m_HonorBodyLengthTag) { - sig.BodyLength = (unsigned) -1; + sig.BodyLength = (unsigned)-1; } else { - if (!ParseUnsigned(values[8],&sig.BodyLength)) - return DKIM_BAD_SYNTAX; + if (!ParseUnsigned(values[8], &sig.BodyLength)) return DKIM_BAD_SYNTAX; } // query methods if (values[9] != NULL) { // make sure "dns" is in the list bool HasDNS = false; - char* s = strtok_r(values[9],":",&saveptr); + char *s = strtok_r(values[9], ":", &saveptr); while (s != NULL) { - if (strncmp(s,"dns",3) == 0 && (s[3] == '\0' || s[3] == '/')) { + if (strncmp(s, "dns", 3) == 0 && (s[3] == '\0' || s[3] == '/')) { HasDNS = true; break; } - s = strtok_r(NULL,": \t",&saveptr); /* FIXME */ -// s = strtok_r(NULL,": ",&saveptr); /* FIXME */ + s = strtok_r(NULL, ": \t", &saveptr); /* FIXME */ + // s = strtok_r(NULL,": ",&saveptr); /* FIXME */ } - if (!HasDNS) - return DKIM_BAD_SYNTAX; // todo: maybe create a new error code for unknown query method + if (!HasDNS) return DKIM_BAD_SYNTAX; // todo: maybe create a new error code for unknown query method } // signature time unsigned SignedTime = -1; if (values[10] != NULL) { - if (!ParseUnsigned(values[10],&SignedTime)) - return DKIM_BAD_SYNTAX; + if (!ParseUnsigned(values[10], &SignedTime)) return DKIM_BAD_SYNTAX; } // expiration time if (values[11] == NULL) { - sig.ExpireTime = (unsigned) -1; // common trick; feh + sig.ExpireTime = (unsigned)-1; // common trick; feh } else { - if (!ParseUnsigned(values[11],&sig.ExpireTime)) - return DKIM_BAD_SYNTAX; + if (!ParseUnsigned(values[11], &sig.ExpireTime)) return DKIM_BAD_SYNTAX; - if (sig.ExpireTime != (unsigned) -1) { + if (sig.ExpireTime != (unsigned)-1) { // the value of x= MUST be greater than the value of t= if both are present - if (SignedTime != (unsigned) -1 && sig.ExpireTime <= SignedTime) - return DKIM_BAD_SYNTAX; + if (SignedTime != (unsigned)-1 && sig.ExpireTime <= SignedTime) return DKIM_BAD_SYNTAX; // todo: if possible, use the received date/time instead of the current time unsigned curtime = time(NULL); - if (curtime > sig.ExpireTime) - return DKIM_SIGNATURE_EXPIRED; + if (curtime > sig.ExpireTime) return DKIM_SIGNATURE_EXPIRED; } } // parse the signed headers list bool HasFrom = false, HasSubject = false; - RemoveSWSP(values[4]); // header names shouldn't have spaces in them so this should be ok... - char* s = strtok_r(values[4],":",&saveptr); + RemoveSWSP(values[4]); // header names shouldn't have spaces in them so this should be ok... + char *s = strtok_r(values[4], ":", &saveptr); while (s != NULL) { - if (_stricmp(s,"From") == 0) + if (_stricmp(s, "From") == 0) HasFrom = true; - else if (_stricmp(s,"Subject") == 0) + else if (_stricmp(s, "Subject") == 0) HasSubject = true; sig.SignedHeaders.push_back(s); - s = strtok_r(NULL,":",&saveptr); + s = strtok_r(NULL, ":", &saveptr); } - if (!HasFrom) - return DKIM_BAD_SYNTAX; // todo: maybe create a new error code for h= missing From + if (!HasFrom) return DKIM_BAD_SYNTAX; // todo: maybe create a new error code for h= missing From if (m_SubjectIsRequired && !HasSubject) - return DKIM_BAD_SYNTAX; // todo: maybe create a new error code for h= missing Subject + return DKIM_BAD_SYNTAX; // todo: maybe create a new error code for h= missing Subject return DKIM_SUCCESS; } //////////////////////////////////////////////////////////////////////////////// -// +// // ProcessBody - Process message body data // //////////////////////////////////////////////////////////////////////////////// -int CDKIMVerify::ProcessBody(char* szBuffer,int nBufLength,bool bEOF) +int CDKIMVerify::ProcessBody(char *szBuffer, int nBufLength, bool bEOF) { bool MoreBodyNeeded = false; @@ -1179,44 +1146,43 @@ int CDKIMVerify::ProcessBody(char* szBuffer,int nBufLength,bool bEOF) if (i->BodyCanonicalization == DKIM_CANON_SIMPLE) { if (nBufLength > 0) { while (i->EmptyLineCount > 0) { - i->Hash("\r\n",2,true); + i->Hash("\r\n", 2, true); i->EmptyLineCount--; } - i->Hash(szBuffer,nBufLength,true); - i->Hash("\r\n",2,true); + i->Hash(szBuffer, nBufLength, true); + i->Hash("\r\n", 2, true); } else { i->EmptyLineCount++; - if (bEOF) - i->Hash("\r\n",2,true); + if (bEOF) i->Hash("\r\n", 2, true); } } else if (i->BodyCanonicalization == DKIM_CANON_RELAXED) { CompressSWSP(szBuffer, nBufLength); if (nBufLength > 0) { while (i->EmptyLineCount > 0) { - i->Hash("\r\n",2,true); + i->Hash("\r\n", 2, true); i->EmptyLineCount--; } - i->Hash(szBuffer,nBufLength,true); - if (!bEOF) - i->Hash("\r\n",2,true); - } else i->EmptyLineCount++; + i->Hash(szBuffer, nBufLength, true); + if (!bEOF) i->Hash("\r\n", 2, true); + } else + i->EmptyLineCount++; } else if (i->BodyCanonicalization == DKIM_CANON_NOWSP) { - RemoveSWSP(szBuffer,nBufLength); - i->Hash(szBuffer,nBufLength,true); + RemoveSWSP(szBuffer, nBufLength); + i->Hash(szBuffer, nBufLength, true); } - if (i->UnverifiedBodyCount == 0) - MoreBodyNeeded = true; + if (i->UnverifiedBodyCount == 0) MoreBodyNeeded = true; } } - if (!MoreBodyNeeded) - return DKIM_FINISHED_BODY; + if (!MoreBodyNeeded) return DKIM_FINISHED_BODY; return DKIM_SUCCESS; } -SelectorInfo::SelectorInfo(const string &sSelector,const string &sDomain) : Domain(sDomain),Selector(sSelector) +SelectorInfo::SelectorInfo(const string& sSelector, const string& sDomain) : + Domain(sDomain), + Selector(sSelector) { AllowSHA1 = true; AllowSHA256 = true; @@ -1234,30 +1200,30 @@ SelectorInfo::~SelectorInfo() } //////////////////////////////////////////////////////////////////////////////// -// +// // Parse - Parse a DKIM selector from DNS data // //////////////////////////////////////////////////////////////////////////////// -int SelectorInfo::Parse(char* Buffer) +int SelectorInfo::Parse(char *Buffer) { // for strtok_r() char *saveptr; char *PubKeyBase64; /*- public key Base64 encoded */ char ed25519PubKey[61]; - static const char *tags[] = {"v","g","h","k","p","s","t","n",NULL}; // 0, 1, 2, 3, 4 - char *values[sizeof(tags)/sizeof(tags[0])] = {NULL}; + static const char *tags[] = {"v", "g", "h", "k", "p", "s", "t", "n", NULL}; // 0, 1, 2, 3, 4 + char *values[sizeof(tags) / sizeof(tags[0])] = {NULL}; + + ParseTagValueList(Buffer, tags, values); - ParseTagValueList(Buffer,tags,values); - // return DKIM_SELECTOR_INVALID; if (values[0] != NULL) { // make sure the version is "DKIM1" - if (strcmp(values[0],"DKIM1") != 0) - return DKIM_SELECTOR_INVALID; // todo: maybe create a new error code for unsupported selector version + if (strcmp(values[0], "DKIM1") != 0) + return DKIM_SELECTOR_INVALID; // todo: maybe create a new error code for unsupported selector version // make sure v= is the first tag in the response // todo: maybe don't enforce this, it seems unnecessary - for (unsigned j = 1; j < sizeof(values)/sizeof(values[0]); j++) { + for (unsigned j = 1; j < sizeof(values) / sizeof(values[0]); j++) { if (values[j] != NULL && values[j] < values[0]) { return DKIM_SELECTOR_INVALID; } @@ -1265,10 +1231,9 @@ int SelectorInfo::Parse(char* Buffer) } // selector MUST have p= tag - if (values[4] == NULL) - return DKIM_SELECTOR_INVALID; + if (values[4] == NULL) return DKIM_SELECTOR_INVALID; - PubKeyBase64 = values[4]; // gotcha + PubKeyBase64 = values[4]; // gotcha // granularity -- [g= ... ] if (values[1] == NULL) @@ -1282,13 +1247,16 @@ int SelectorInfo::Parse(char* Buffer) AllowSHA256 = true; } else { // MUST include "sha1" or "sha256" - char* s = strtok_r(values[2],":",&saveptr); + char *s = strtok_r(values[2], ":", &saveptr); while (s != NULL) { - if (strcmp(s,"sha1") == 0) - { AllowSHA1 = true; AllowSHA256 = false; } - else if (strcmp(s,"sha256") == 0) - { AllowSHA256 = true; AllowSHA1 = false; } - s = strtok_r(NULL,":",&saveptr); + if (strcmp(s, "sha1") == 0) { + AllowSHA1 = true; + AllowSHA256 = false; + } else if (strcmp(s, "sha256") == 0) { + AllowSHA256 = true; + AllowSHA1 = false; + } + s = strtok_r(NULL, ":", &saveptr); } if (!(AllowSHA1 || AllowSHA256)) return DKIM_SELECTOR_INVALID; // todo: maybe create a new error code for unsupported hash algorithm @@ -1297,50 +1265,48 @@ int SelectorInfo::Parse(char* Buffer) // key type -- [k=rsa|ed25519] (not required) if (values[3] != NULL) { // key type MUST be "rsa" or "ed25519" - if (strcmp(values[3],"rsa") != 0 && strcmp(values[3],"ed25519") != 0) // none of either + if (strcmp(values[3], "rsa") != 0 && strcmp(values[3], "ed25519") != 0) // none of either return DKIM_SELECTOR_INVALID; - if (strcmp(values[3],"ed25519") == 0) { - AllowSHA1 = false; - AllowSHA256 = true; - strcpy(ed25519PubKey,"MCowBQYDK2VwAyEA"); + if (strcmp(values[3], "ed25519") == 0) { + AllowSHA1 = false; + AllowSHA256 = true; + strcpy(ed25519PubKey, "MCowBQYDK2VwAyEA"); /* * rfc8463 * since Ed25519 public keys are 256 bits long, * the base64-encoded key is only 44 octets */ - if (strlen(values[4]) > 44) - return DKIM_SELECTOR_PUBLIC_KEY_INVALID; - strcat(ed25519PubKey,values[4]); + if (strlen(values[4]) > 44) return DKIM_SELECTOR_PUBLIC_KEY_INVALID; + strcat(ed25519PubKey, values[4]); PubKeyBase64 = ed25519PubKey; - } + } } // service type -- [s= ...] (not required) if (values[5] != NULL) { // make sure "*" or "email" is in the list bool ServiceTypeMatch = false; - char* s = strtok_r(values[5],":",&saveptr); + char *s = strtok_r(values[5], ":", &saveptr); while (s != NULL) { - if (strcmp(s, "*") == 0 || strcmp(s,"email") == 0) { + if (strcmp(s, "*") == 0 || strcmp(s, "email") == 0) { ServiceTypeMatch = true; break; } - s = strtok_r(NULL,":",&saveptr); + s = strtok_r(NULL, ":", &saveptr); } - if (!ServiceTypeMatch) - return DKIM_SELECTOR_INVALID; + if (!ServiceTypeMatch) return DKIM_SELECTOR_INVALID; } // flags -- [t= ...] (not required) if (values[6] != NULL) { - char *s = strtok_r(values[6],":",&saveptr); + char *s = strtok_r(values[6], ":", &saveptr); while (s != NULL) { - if (strcmp(s,"y") == 0) { + if (strcmp(s, "y") == 0) { Testing = true; - } else if (strcmp(s,"s") == 0) { + } else if (strcmp(s, "s") == 0) { SameDomain = true; } - s = strtok_r(NULL,":",&saveptr); + s = strtok_r(NULL, ":", &saveptr); } } @@ -1350,20 +1316,22 @@ int SelectorInfo::Parse(char* Buffer) if (PublicKeyLen == 0) { return DKIM_SELECTOR_KEY_REVOKED; // this error causes the signature to fail } else { - const unsigned char *PublicKeyData = (unsigned char* )PubKeyBase64; // 0-terminated + const unsigned char *PublicKeyData = (unsigned char *)PubKeyBase64; // 0-terminated - EVP_PKEY *pkey = d2i_PUBKEY(NULL,&PublicKeyData,PublicKeyLen); /* retrieve and return PubKey from data */ + EVP_PKEY *pkey = + d2i_PUBKEY(NULL, &PublicKeyData, PublicKeyLen); /* retrieve and return PubKey from data */ - if (pkey == NULL) - return DKIM_SELECTOR_PUBLIC_KEY_INVALID; + if (pkey == NULL) return DKIM_SELECTOR_PUBLIC_KEY_INVALID; - // make sure public key is the correct type (we only support rsa & ed25519) -#if ((OPENSSL_VERSION_NUMBER < 0x10101000L) || (LIBRESSL_VERSION_NUMBER > 0 && LIBRESSL_VERSION_NUMBER < 0x20700000L)) + // make sure public key is the correct type (we only support rsa & ed25519) +#if ( \ + (OPENSSL_VERSION_NUMBER < 0x10101000L) \ + || (LIBRESSL_VERSION_NUMBER > 0 && LIBRESSL_VERSION_NUMBER < 0x20700000L)) if (pkey->type == EVP_PKEY_RSA || pkey->type == EVP_PKEY_RSA2) { #else - if ((EVP_PKEY_base_id(pkey) == EVP_PKEY_RSA) || - (EVP_PKEY_base_id(pkey) == EVP_PKEY_RSA2) || - (EVP_PKEY_base_id(pkey) == EVP_PKEY_ED25519)) { + if ((EVP_PKEY_base_id(pkey) == EVP_PKEY_RSA) || (EVP_PKEY_base_id(pkey) == EVP_PKEY_RSA2) + || (EVP_PKEY_base_id(pkey) == EVP_PKEY_ED25519)) + { #endif PublicKey = pkey; } else { @@ -1376,62 +1344,68 @@ int SelectorInfo::Parse(char* Buffer) } //////////////////////////////////////////////////////////////////////////////// -// +// // GetSelector - Get a DKIM selector for a domain // //////////////////////////////////////////////////////////////////////////////// -SelectorInfo& CDKIMVerify::GetSelector(const string &sSelector,const string &sDomain) +SelectorInfo& CDKIMVerify::GetSelector(const string& sSelector, const string& sDomain) { // see if we already have this selector for (list<SelectorInfo>::iterator i = Selectors.begin(); i != Selectors.end(); ++i) { - if (_stricmp(i->Selector.c_str(),sSelector.c_str()) == 0 && _stricmp(i->Domain.c_str(),sDomain.c_str()) == 0) { + if (_stricmp(i->Selector.c_str(), sSelector.c_str()) == 0 + && _stricmp(i->Domain.c_str(), sDomain.c_str()) == 0) + { return *i; } } - Selectors.push_back(SelectorInfo(sSelector,sDomain)); + Selectors.push_back(SelectorInfo(sSelector, sDomain)); SelectorInfo& sel = Selectors.back(); string sFQDN = sSelector; sFQDN += "._domainkey."; sFQDN += sDomain; - int BufLen = 1024; + int BufLen = 1024; char Buffer[BufLen]; int DNSResult; - if (m_pfnSelectorCallback) { - DNSResult = m_pfnSelectorCallback(sFQDN.c_str(),Buffer,BufLen); + if (m_pfnSelectorCallback) { + DNSResult = m_pfnSelectorCallback(sFQDN.c_str(), Buffer, BufLen); } else - DNSResult = _DNSGetTXT(sFQDN.c_str(),Buffer,BufLen); + DNSResult = _DNSGetTXT(sFQDN.c_str(), Buffer, BufLen); -// Buffer++; BufLen--; + // Buffer++; BufLen--; switch (DNSResult) { - case -1: case -2: case -3: case -5: sel.Status = DKIM_SELECTOR_DNS_TEMP_FAILURE; break; - case 0: case -6: sel.Status = DKIM_SELECTOR_DNS_PERM_FAILURE; break; + case -1: + case -2: + case -3: + case -5: sel.Status = DKIM_SELECTOR_DNS_TEMP_FAILURE; break; + case 0: + case -6: sel.Status = DKIM_SELECTOR_DNS_PERM_FAILURE; break; default: sel.Status = sel.Parse(Buffer); - } + } return sel; } //////////////////////////////////////////////////////////////////////////////// -// +// // GetDetails - Get DKIM verification details (per signature) // //////////////////////////////////////////////////////////////////////////////// -int CDKIMVerify::GetDetails(int* nSigCount,DKIMVerifyDetails** pDetails) +int CDKIMVerify::GetDetails(int *nSigCount, DKIMVerifyDetails **pDetails) { Details.clear(); - for (list < SignatureInfo>::iterator i = Signatures.begin(); i != Signatures.end(); ++i) { + for (list<SignatureInfo>::iterator i = Signatures.begin(); i != Signatures.end(); ++i) { DKIMVerifyDetails d; - d.szSignature = (char* )i->Header.c_str(); - d.szSignatureDomain = (char* )i->Domain.c_str(); - d.szIdentityDomain = (char* )i->IdentityDomain.c_str(); - d.szCanonicalizedData = (char* )i->CanonicalizedData.c_str(); + d.szSignature = (char *)i->Header.c_str(); + d.szSignatureDomain = (char *)i->Domain.c_str(); + d.szIdentityDomain = (char *)i->IdentityDomain.c_str(); + d.szCanonicalizedData = (char *)i->CanonicalizedData.c_str(); d.nResult = i->Status; Details.push_back(d); } @@ -1,20 +1,25 @@ +#include "dns.h" + +#include <arpa/nameser.h> + #include <netdb.h> -#include <string.h> -#include <sys/types.h> #include <netinet/in.h> -#include <arpa/nameser.h> #include <sys/socket.h> -#include "ip.h" -#include "ipalloc.h" -#include "fmt.h" +#include <sys/types.h> + +#include <string.h> + #include "alloc.h" -#include "str.h" -#include "stralloc.h" -#include "dnsresolv.h" -#include "case.h" -#include "dns.h" #include "buffer.h" +#include "case.h" +#include "dnsresolv.h" #include "exit.h" +#include "fmt.h" +#include "ip.h" +#include "str.h" +#include "stralloc.h" + +#include "ipalloc.h" /** @file dns.c @@ -24,86 +29,96 @@ static stralloc glue = {0}; static stralloc ip = {0}; -static int dns_ipplus(ipalloc *ia,stralloc *sa,int pref) +static int dns_ipplus(ipalloc *ia, stralloc *sa, int pref) { struct ip_mx ix; int error = 0; char ip4[4]; char ip6[16]; - int i; + int i; /* Case 1: sa is just IPv4 */ - if (ip4_scanbracket(sa->s,ip4)) { - if (!stralloc_copys(&glue,sa->s)) return DNS_MEM; + if (ip4_scanbracket(sa->s, ip4)) { + if (!stralloc_copys(&glue, sa->s)) return DNS_MEM; if (!stralloc_0(&glue)) return DNS_MEM; if (glue.s[0]) { ix.pref = 0; ix.af = AF_INET; - byte_copy(&ix.addr,4,ip4); // = ip; //cp - if (!ipalloc_append(ia,&ix)) return DNS_MEM; + byte_copy(&ix.addr, 4, ip4); // = ip; //cp + if (!ipalloc_append(ia, &ix)) return DNS_MEM; return 0; } } /* Case 2: sa is just IPv6 */ - if (ip6_scanbracket(sa->s,ip6)) { - if (!stralloc_copys(&glue,sa->s)) return DNS_MEM; + if (ip6_scanbracket(sa->s, ip6)) { + if (!stralloc_copys(&glue, sa->s)) return DNS_MEM; if (!stralloc_0(&glue)) return DNS_MEM; if (glue.s[0]) { ix.pref = 0; ix.af = AF_INET6; - byte_copy(&ix.addr,16,ip6); // = ip; //cp - if (!ipalloc_append(ia,&ix)) return DNS_MEM; + byte_copy(&ix.addr, 16, ip6); // = ip; //cp + if (!ipalloc_append(ia, &ix)) return DNS_MEM; return 0; } } /* Case 3: sa is fqdn and looking for IPv6 */ - if (dns_ip6(&ip,sa) > 0) { + if (dns_ip6(&ip, sa) > 0) { for (i = 0; i + 16 <= ip.len; i += 16) { if (ip6_isv4mapped(ip.s + i)) continue; - ix.af = AF_INET6; - ix.pref = pref; - byte_copy(&ix.addr,16,ip.s + i); // = ip; //cp - str_copy(ix.mxh,sa->s); // mx hostname - if (!ipalloc_append(ia,&ix)) { error = DNS_MEM; break; } - error = 0; + ix.af = AF_INET6; + ix.pref = pref; + byte_copy(&ix.addr, 16, ip.s + i); // = ip; //cp + str_copy(ix.mxh, sa->s); // mx hostname + if (!ipalloc_append(ia, &ix)) { + error = DNS_MEM; + break; } + error = 0; + } } else error = 1; /* Case 4: sa is fqdn and looking for IPv4 */ - if (dns_ip4(&ip,sa) > 0) { + if (dns_ip4(&ip, sa) > 0) { for (i = 0; i + 4 <= ip.len; i += 4) { ix.af = AF_INET; ix.pref = pref; - byte_copy(&ix.addr,4,ip.s + i); // = ip; //cp - str_copy(ix.mxh,sa->s); // mx hostname - if (!ipalloc_append(ia,&ix)) { error = DNS_MEM; break; } + byte_copy(&ix.addr, 4, ip.s + i); // = ip; //cp + str_copy(ix.mxh, sa->s); // mx hostname + if (!ipalloc_append(ia, &ix)) { + error = DNS_MEM; + break; + } error = 0; } } else - error += 2; + error += 2; return error; } -int dns_ipalloc(ipalloc *ia,stralloc *sa) +int dns_ipalloc(ipalloc *ia, stralloc *sa) { - if (!ipalloc_readyplus(ia,0)) return DNS_MEM; + if (!ipalloc_readyplus(ia, 0)) return DNS_MEM; ia->len = 0; - return dns_ipplus(ia,sa,0); + return dns_ipplus(ia, sa, 0); } /* dns_mxip */ -int dns_mxip(ipalloc *ia,stralloc *sa,unsigned long random) { - struct mx { stralloc sa; unsigned short p; } *mx; +int dns_mxip(ipalloc *ia, stralloc *sa, unsigned long random) +{ + struct mx { + stralloc sa; + unsigned short p; + } *mx; struct ip_mx ix; int nummx; int i; @@ -114,23 +129,25 @@ int dns_mxip(ipalloc *ia,stralloc *sa,unsigned long random) { /* Case 1: sa is just IPv4 or IPv6 */ - if (!ipalloc_readyplus(ia,0)) return DNS_MEM; + if (!ipalloc_readyplus(ia, 0)) return DNS_MEM; ia->len = 0; - if (!stralloc_copys(&glue,sa->s)) return DNS_MEM; + if (!stralloc_copys(&glue, sa->s)) return DNS_MEM; if (!stralloc_0(&glue)) return DNS_MEM; if (glue.s[0]) { ix.pref = 0; - if (!glue.s[ip4_scan(glue.s,(char *)&ix.addr.ip4)] || \ - !glue.s[ip4_scanbracket(glue.s,(char *)&ix.addr.ip4)]) { + if (!glue.s[ip4_scan(glue.s, (char *)&ix.addr.ip4)] + || !glue.s[ip4_scanbracket(glue.s, (char *)&ix.addr.ip4)]) + { ix.af = AF_INET; - if (!ipalloc_append(ia,&ix)) return DNS_MEM; + if (!ipalloc_append(ia, &ix)) return DNS_MEM; return 0; } - if (!glue.s[ip6_scan(glue.s,(char *)&ix.addr.ip6)] || \ - !glue.s[ip6_scanbracket(glue.s,(char *)&ix.addr.ip6)]) { + if (!glue.s[ip6_scan(glue.s, (char *)&ix.addr.ip6)] + || !glue.s[ip6_scanbracket(glue.s, (char *)&ix.addr.ip6)]) + { ix.af = AF_INET6; - if (!ipalloc_append(ia,&ix)) return DNS_MEM; + if (!ipalloc_append(ia, &ix)) return DNS_MEM; return 0; } } @@ -140,26 +157,27 @@ int dns_mxip(ipalloc *ia,stralloc *sa,unsigned long random) { DNS_INIT nummx = 0; len = 0; - i = dns_mx(&ip,sa); - mx = (struct mx *) alloc(i * sizeof(struct mx)); + i = dns_mx(&ip, sa); + mx = (struct mx *)alloc(i * sizeof(struct mx)); if (!mx) return DNS_MEM; if (i) { do { - j = str_chr(ip.s + len + 2,'\0'); /* several answers */ + j = str_chr(ip.s + len + 2, '\0'); /* several answers */ mx[nummx].sa.s = 0; - if (!stralloc_copys(&mx[nummx].sa,ip.s + len + 2)) { /* mxhost name */ - alloc_free(mx); return DNS_MEM; + if (!stralloc_copys(&mx[nummx].sa, ip.s + len + 2)) { /* mxhost name */ + alloc_free(mx); + return DNS_MEM; } ip.s[len + 3] = '\0'; - uint16_unpack_big(ip.s + len,&pref); + uint16_unpack_big(ip.s + len, &pref); mx[nummx].p = pref; len += j + 3; ++nummx; } while (len < ip.len); } - if (!nummx) return dns_ipalloc(ia,sa); /* e.g., CNAME -> A */ + if (!nummx) return dns_ipalloc(ia, sa); /* e.g., CNAME -> A */ flagsoft = 0; while (nummx > 0) { @@ -170,17 +188,17 @@ int dns_mxip(ipalloc *ia,stralloc *sa,unsigned long random) { if (mx[j].p < mx[i].p) { i = j; numsame = 1; - } - else if (mx[j].p == mx[i].p) { + } else if (mx[j].p == mx[i].p) { ++numsame; random = random * 69069 + 1; if ((random / 2) < (2147483647 / numsame)) i = j; } } - switch (dns_ipplus(ia,&mx[i].sa,mx[i].p)) { + switch (dns_ipplus(ia, &mx[i].sa, mx[i].p)) { case -1: return DNS_MEM; - case -2: case -3: flagsoft = -5; break; + case -2: + case -3: flagsoft = -5; break; } alloc_free(mx[i].sa.s); @@ -191,11 +209,11 @@ int dns_mxip(ipalloc *ia,stralloc *sa,unsigned long random) { return flagsoft; } -int dns_ip(ipalloc *ia,stralloc *sa) +int dns_ip(ipalloc *ia, stralloc *sa) { - if (!ipalloc_readyplus(ia,0)) return DNS_MEM; + if (!ipalloc_readyplus(ia, 0)) return DNS_MEM; ia->len = 0; - return dns_ipplus(ia,sa,0); + return dns_ipplus(ia, sa, 0); } diff --git a/src/dns_tlsa.c b/src/dns_tlsa.c index 4b674c1..52f7d83 100644 --- a/src/dns_tlsa.c +++ b/src/dns_tlsa.c @@ -1,12 +1,13 @@ #include "byte.h" +#include "logmsg.h" #include "stralloc.h" #include "uint_t.h" + #include "dns.h" -#include "logmsg.h" static char *q = 0; -int dns_tlsa_packet(stralloc *out,const char *buf,unsigned int len) +int dns_tlsa_packet(stralloc *out, const char *buf, unsigned int len) { unsigned int pos; char header[12]; @@ -14,21 +15,25 @@ int dns_tlsa_packet(stralloc *out,const char *buf,unsigned int len) uint16 numanswers; int ranswers = 0; - if (!stralloc_copys(out,"")) return DNS_MEM; + if (!stralloc_copys(out, "")) return DNS_MEM; - pos = dns_packet_copy(buf,len,0,header,12); if (!pos) return DNS_ERR; - uint16_unpack_big(header + 6,&numanswers); - pos = dns_packet_skipname(buf,len,pos); if (!pos) return DNS_ERR; + pos = dns_packet_copy(buf, len, 0, header, 12); + if (!pos) return DNS_ERR; + uint16_unpack_big(header + 6, &numanswers); + pos = dns_packet_skipname(buf, len, pos); + if (!pos) return DNS_ERR; pos += 4; while (numanswers--) { - pos = dns_packet_skipname(buf,len,pos); if (!pos) return DNS_ERR; - pos = dns_packet_copy(buf,len,pos,header,10); if (!pos) return DNS_ERR; - uint16_unpack_big(header + 8,&datalen); - if (byte_equal(header,2,DNS_T_TLSA)) - if (byte_equal(header + 2,2,DNS_C_IN)) { + pos = dns_packet_skipname(buf, len, pos); + if (!pos) return DNS_ERR; + pos = dns_packet_copy(buf, len, pos, header, 10); + if (!pos) return DNS_ERR; + uint16_unpack_big(header + 8, &datalen); + if (byte_equal(header, 2, DNS_T_TLSA)) + if (byte_equal(header + 2, 2, DNS_C_IN)) { if (pos + datalen > len) return DNS_ERR; - if (!stralloc_catb(out,buf + pos,datalen)) return DNS_MEM; + if (!stralloc_catb(out, buf + pos, datalen)) return DNS_MEM; } pos += datalen; ++ranswers; @@ -38,13 +43,13 @@ int dns_tlsa_packet(stralloc *out,const char *buf,unsigned int len) return ranswers; } -int dns_tlsa(stralloc *out,const stralloc *fqdn) +int dns_tlsa(stralloc *out, const stralloc *fqdn) { int rc = 0; - if (dns_domain_fromdot(&q,fqdn->s,fqdn->len) <= 0) return DNS_ERR; - if (dns_resolve(q,DNS_T_TLSA) >= 0) { - if ((rc = dns_tlsa_packet(out,dns_resolve_tx.packet,dns_resolve_tx.packetlen)) < 0) return DNS_ERR; + if (dns_domain_fromdot(&q, fqdn->s, fqdn->len) <= 0) return DNS_ERR; + if (dns_resolve(q, DNS_T_TLSA) >= 0) { + if ((rc = dns_tlsa_packet(out, dns_resolve_tx.packet, dns_resolve_tx.packetlen)) < 0) return DNS_ERR; dns_transmit_free(&dns_resolve_tx); dns_domain_free(&q); } diff --git a/src/dnscname.c b/src/dnscname.c index 546d273..17265d3 100644 --- a/src/dnscname.c +++ b/src/dnscname.c @@ -1,31 +1,31 @@ #include <unistd.h> + #include "buffer.h" -#include "stralloc.h" #include "dnsresolv.h" #include "exit.h" -#include "dns.h" #include "logmsg.h" +#include "stralloc.h" + +#include "dns.h" #define WHO "dnscname" stralloc sa = {0}; stralloc out = {0}; -int main(int argc,char **argv) +int main(int argc, char **argv) { - int r; + int r; - if (!argv[1]) - logmsg(WHO,100,USAGE,"dnscname fqdn"); + if (!argv[1]) logmsg(WHO, 100, USAGE, "dnscname fqdn"); - if (!stralloc_copys(&sa,argv[1])) - logmsg(WHO,111,FATAL,"out of memory"); + if (!stralloc_copys(&sa, argv[1])) logmsg(WHO, 111, FATAL, "out of memory"); DNS_INIT - if ((r = dns_cname(&out,&sa)) < 0) _exit(1); + if ((r = dns_cname(&out, &sa)) < 0) _exit(1); if (r > 0) { - buffer_putflush(buffer_1,out.s,out.len); - buffer_putsflush(buffer_1,"\n"); + buffer_putflush(buffer_1, out.s, out.len); + buffer_putsflush(buffer_1, "\n"); } _exit(0); diff --git a/src/dnsdoe.c b/src/dnsdoe.c index ad6b253..579eb04 100644 --- a/src/dnsdoe.c +++ b/src/dnsdoe.c @@ -1,14 +1,16 @@ #include <unistd.h> + #include "buffer.h" -#include "exit.h" #include "dnsresolv.h" +#include "exit.h" + #include "dns.h" void dnsdoe(int r) { switch (r) { - case DNS_HARD: buffer_putsflush(buffer_2,"hard error\n"); _exit(100); - case DNS_SOFT: buffer_putsflush(buffer_2,"soft error\n"); _exit(111); - case DNS_MEM: buffer_putsflush(buffer_2,"out of memory\n"); _exit(111); + case DNS_HARD: buffer_putsflush(buffer_2, "hard error\n"); _exit(100); + case DNS_SOFT: buffer_putsflush(buffer_2, "soft error\n"); _exit(111); + case DNS_MEM: buffer_putsflush(buffer_2, "out of memory\n"); _exit(111); } } diff --git a/src/dnsfq.c b/src/dnsfq.c index a174541..65c49fe 100644 --- a/src/dnsfq.c +++ b/src/dnsfq.c @@ -1,61 +1,60 @@ -#include <sys/types.h> #include <sys/socket.h> +#include <sys/types.h> #include <unistd.h> + #include "buffer.h" -#include "stralloc.h" #include "dnsresolv.h" -#include "dns.h" -#include "ip.h" #include "exit.h" +#include "ip.h" #include "logmsg.h" +#include "stralloc.h" + +#include "dns.h" -#define WHO "dnsfq" +#define WHO "dnsfq" #define MAXCNAME 10 stralloc ca = {0}; stralloc sa = {0}; stralloc ia = {0}; -int main(int argc,char **argv) +int main(int argc, char **argv) { int i, r; char ip4str[IP4_FMT]; char ip6str[IP6_FMT]; - if (!argv[1]) - logmsg(WHO,100,USAGE,"dnsfq fqdn"); + if (!argv[1]) logmsg(WHO, 100, USAGE, "dnsfq fqdn"); - if (!stralloc_copys(&sa,argv[1])) - logmsg(WHO,111,FATAL,"out of memory"); + if (!stralloc_copys(&sa, argv[1])) logmsg(WHO, 111, FATAL, "out of memory"); DNS_INIT - buffer_put(buffer_1,sa.s,sa.len); - buffer_puts(buffer_1," "); + buffer_put(buffer_1, sa.s, sa.len); + buffer_puts(buffer_1, " "); for (i = 0; i <= MAXCNAME; i++) { - if ((r = dns_cname(&ca,&sa)) < 0) _exit(1); + if ((r = dns_cname(&ca, &sa)) < 0) _exit(1); if (r > 0) { - if (!stralloc_copy(&sa,&ca)) - logmsg(WHO,111,FATAL,"out of memory"); - buffer_puts(buffer_1,"-> "); - buffer_put(buffer_1,sa.s,sa.len); - buffer_puts(buffer_1," "); - } - else break; + if (!stralloc_copy(&sa, &ca)) logmsg(WHO, 111, FATAL, "out of memory"); + buffer_puts(buffer_1, "-> "); + buffer_put(buffer_1, sa.s, sa.len); + buffer_puts(buffer_1, " "); + } else + break; } - buffer_putsflush(buffer_1,"\n"); + buffer_putsflush(buffer_1, "\n"); - if ((i = dns_ip6(&ia,&sa)) > 0) { + if ((i = dns_ip6(&ia, &sa)) > 0) { for (i = 0; i + 16 <= ia.len; i += 16) { if (ip6_isv4mapped(ia.s + i)) continue; - buffer_put(buffer_1,ip6str,ip6_fmt(ip6str,ia.s + i)); - buffer_puts(buffer_1,"\n"); + buffer_put(buffer_1, ip6str, ip6_fmt(ip6str, ia.s + i)); + buffer_puts(buffer_1, "\n"); } } - if ((i = dns_ip4(&ia,&sa)) > 0) { - for (i = 0; i + 4 <= ia.len;i += 4) { - buffer_put(buffer_1,ip4str,ip4_fmt(ip4str,ia.s + i)); - buffer_puts(buffer_1,"\n"); + if ((i = dns_ip4(&ia, &sa)) > 0) { + for (i = 0; i + 4 <= ia.len; i += 4) { + buffer_put(buffer_1, ip4str, ip4_fmt(ip4str, ia.s + i)); + buffer_puts(buffer_1, "\n"); } } buffer_flush(buffer_1); diff --git a/src/dnsip.c b/src/dnsip.c index 2c84d04..bbb2557 100644 --- a/src/dnsip.c +++ b/src/dnsip.c @@ -1,14 +1,16 @@ -#include <sys/types.h> #include <sys/socket.h> +#include <sys/types.h> #include <unistd.h> + #include "buffer.h" -#include "stralloc.h" #include "dnsresolv.h" -#include "ip.h" #include "exit.h" #include "fmt.h" -#include "dns.h" +#include "ip.h" #include "logmsg.h" +#include "stralloc.h" + +#include "dns.h" #define WHO "dnsip" @@ -21,26 +23,24 @@ int main(int argc, char **argv) char ip4str[IP4_FMT]; char ip6str[IP6_FMT]; - if (!argv[1]) - logmsg(WHO,100,USAGE,"dnsip fqdn"); + if (!argv[1]) logmsg(WHO, 100, USAGE, "dnsip fqdn"); - if (!stralloc_copys(&sa,argv[1])) - logmsg(WHO,111,FATAL,"out of memory"); + if (!stralloc_copys(&sa, argv[1])) logmsg(WHO, 111, FATAL, "out of memory"); DNS_INIT - if (dns_ip6(&out,&sa) > 0) /* IPv6 first */ + if (dns_ip6(&out, &sa) > 0) /* IPv6 first */ for (i = 0; i + 16 <= out.len; i += 16) { - if (ip6_isv4mapped(out.s + i)) continue; - buffer_put(buffer_1,ip6str,ip6_fmt(ip6str,out.s + i)); - buffer_puts(buffer_1,"\n"); + if (ip6_isv4mapped(out.s + i)) continue; + buffer_put(buffer_1, ip6str, ip6_fmt(ip6str, out.s + i)); + buffer_puts(buffer_1, "\n"); } - if (dns_ip4(&out,&sa) > 0) - for (i = 0; i + 4 <= out.len;i += 4) { - buffer_put(buffer_1,ip4str,ip4_fmt(ip4str,out.s + i)); - buffer_puts(buffer_1,"\n"); + if (dns_ip4(&out, &sa) > 0) + for (i = 0; i + 4 <= out.len; i += 4) { + buffer_put(buffer_1, ip4str, ip4_fmt(ip4str, out.s + i)); + buffer_puts(buffer_1, "\n"); } - buffer_putsflush(buffer_1,""); + buffer_putsflush(buffer_1, ""); _exit(0); } diff --git a/src/dnsmxip.c b/src/dnsmxip.c index de3bb7c..9148bdc 100644 --- a/src/dnsmxip.c +++ b/src/dnsmxip.c @@ -1,25 +1,27 @@ -#include <sys/types.h> #include <sys/socket.h> +#include <sys/types.h> #include <unistd.h> + #include "buffer.h" -#include "stralloc.h" -#include "fmt.h" #include "dnsresolv.h" -#include "ip.h" -#include "now.h" #include "exit.h" -#include "dns.h" +#include "fmt.h" +#include "ip.h" #include "logmsg.h" #include "str.h" +#include "stralloc.h" + +#include "dns.h" +#include "now.h" #define WHO "dnsmxip" static stralloc sa = {0}; -static stralloc ia = {0}; +static stralloc ia = {0}; static stralloc out = {0}; -static stralloc ipaddr = {0}; +static stralloc ipaddr = {0}; -int dns_ia(stralloc *ip,char *s) +int dns_ia(stralloc *ip, char *s) { int i; int j = 0; @@ -27,79 +29,77 @@ int dns_ia(stralloc *ip,char *s) char ip4str[IP4_FMT]; char ip6str[IP6_FMT]; - if (!stralloc_copys(&sa,s)) return -1; - if (sa.s[sa.len-1] != '.') - if (!stralloc_append(&sa,".")) return -1; - if (!stralloc_copys(ip,"")) return -1; - - DNS_INIT - if (dns_ip6(&ia,&sa) > 0) { + if (!stralloc_copys(&sa, s)) return -1; + if (sa.s[sa.len - 1] != '.') + if (!stralloc_append(&sa, ".")) return -1; + if (!stralloc_copys(ip, "")) return -1; + + DNS_INIT + if (dns_ip6(&ia, &sa) > 0) { for (i = 0; i + 16 <= ia.len; i += 16) { - if (ip6_isv4mapped(ia.s + i)) continue; - j = ip6_fmt(ip6str,ia.s + i); + if (ip6_isv4mapped(ia.s + i)) continue; + j = ip6_fmt(ip6str, ia.s + i); r += j; - if (!stralloc_catb(ip,ip6str,j)) return -1; - if (!stralloc_cats(ip," ")) return -1; + if (!stralloc_catb(ip, ip6str, j)) return -1; + if (!stralloc_cats(ip, " ")) return -1; r++; } } - if (dns_ip4(&ia,&sa) > 0) { + if (dns_ip4(&ia, &sa) > 0) { for (i = 0; i + 4 <= ia.len; i += 4) { - j = ip4_fmt(ip4str,ia.s + i); + j = ip4_fmt(ip4str, ia.s + i); r += j; - if (!stralloc_catb(ip,ip4str,j)) return -1; - if (!stralloc_cats(ip," ")) return -1; + if (!stralloc_catb(ip, ip4str, j)) return -1; + if (!stralloc_cats(ip, " ")) return -1; r++; } } if (!stralloc_0(ip)) return -1; - return r?r-1:0; + return r ? r - 1 : 0; } -int main(int argc,char **argv) +int main(int argc, char **argv) { int j, k, r; uint16 u; int len; char num[FMT_ULONG]; - if (!argv[1]) - logmsg(WHO,100,USAGE,"dnsmx fqdn"); - if (!stralloc_copys(&sa,argv[1])) - logmsg(WHO,111,FATAL,"out of memory"); + if (!argv[1]) logmsg(WHO, 100, USAGE, "dnsmx fqdn"); + if (!stralloc_copys(&sa, argv[1])) logmsg(WHO, 111, FATAL, "out of memory"); - DNS_INIT - if ((r = dns_mx(&out,&sa)) < 0) _exit(1); + DNS_INIT + if ((r = dns_mx(&out, &sa)) < 0) _exit(1); if (r > 0) { j = len = 0; do { - j = str_chr(out.s + len + 2,'\0'); - k = dns_ia(&ipaddr,out.s + len + 2); /* IP */ + j = str_chr(out.s + len + 2, '\0'); + k = dns_ia(&ipaddr, out.s + len + 2); /* IP */ if (k) { - buffer_put(buffer_1,out.s + len + 2,j); /* MX */ - buffer_puts(buffer_1,": "); + buffer_put(buffer_1, out.s + len + 2, j); /* MX */ + buffer_puts(buffer_1, ": "); out.s[len + 3] = '\0'; - uint16_unpack_big(out.s + len,&u); - buffer_put(buffer_1,num,fmt_ulong(num,u)) ; - buffer_puts(buffer_1," ["); - buffer_put(buffer_1,ipaddr.s,k); - buffer_puts(buffer_1,"]"); + uint16_unpack_big(out.s + len, &u); + buffer_put(buffer_1, num, fmt_ulong(num, u)); + buffer_puts(buffer_1, " ["); + buffer_put(buffer_1, ipaddr.s, k); + buffer_puts(buffer_1, "]"); } - buffer_putsflush(buffer_1,"\n"); + buffer_putsflush(buffer_1, "\n"); len += j + 3; } while (len < out.len); - } else { /* A/AAAA */ - k = dns_ia(&ipaddr,argv[1]); /* IP */ + } else { /* A/AAAA */ + k = dns_ia(&ipaddr, argv[1]); /* IP */ if (k) { - buffer_puts(buffer_1,argv[1]); - buffer_puts(buffer_1,": -"); - buffer_puts(buffer_1," ["); - buffer_put(buffer_1,ipaddr.s,k); - buffer_puts(buffer_1,"]"); - buffer_putsflush(buffer_1,"\n"); - } + buffer_puts(buffer_1, argv[1]); + buffer_puts(buffer_1, ": -"); + buffer_puts(buffer_1, " ["); + buffer_put(buffer_1, ipaddr.s, k); + buffer_puts(buffer_1, "]"); + buffer_putsflush(buffer_1, "\n"); + } } _exit(0); diff --git a/src/dnsptr.c b/src/dnsptr.c index 25a4731..dd88922 100644 --- a/src/dnsptr.c +++ b/src/dnsptr.c @@ -1,37 +1,34 @@ #include <unistd.h> + #include "buffer.h" -#include "stralloc.h" -#include "str.h" #include "dnsresolv.h" -#include "dns.h" -#include "ip.h" #include "exit.h" +#include "ip.h" #include "logmsg.h" +#include "str.h" +#include "stralloc.h" + +#include "dns.h" #define WHO "dnsptr" stralloc out = {0}; char ip4[4]; char ip6[16]; - -int main(int argc,char **argv) + +int main(int argc, char **argv) { - if (!argv[1]) - logmsg(WHO,100,USAGE,"dnsptr ipv4 || ipv6 (compactified)"); + if (!argv[1]) logmsg(WHO, 100, USAGE, "dnsptr ipv4 || ipv6 (compactified)"); DNS_INIT - if (str_chr(argv[1],':') < str_len(argv[1])) { - if (!ip6_scan(argv[1],ip6)) - logmsg(WHO,111,FATAL,"wrong IPv6 format"); - if (dns_name6(&out,ip6) > 0) - buffer_put(buffer_1,out.s,out.len); + if (str_chr(argv[1], ':') < str_len(argv[1])) { + if (!ip6_scan(argv[1], ip6)) logmsg(WHO, 111, FATAL, "wrong IPv6 format"); + if (dns_name6(&out, ip6) > 0) buffer_put(buffer_1, out.s, out.len); } else { - if (!ip4_scan(argv[1],ip4)) - logmsg(WHO,111,FATAL,"wrong IPv4 format"); - if (dns_name4(&out,ip4) > 0) - buffer_put(buffer_1,out.s,out.len); + if (!ip4_scan(argv[1], ip4)) logmsg(WHO, 111, FATAL, "wrong IPv4 format"); + if (dns_name4(&out, ip4) > 0) buffer_put(buffer_1, out.s, out.len); } - buffer_putsflush(buffer_1,"\n"); + buffer_putsflush(buffer_1, "\n"); _exit(0); } diff --git a/src/dnstlsa.c b/src/dnstlsa.c index 9871fff..284f7d1 100644 --- a/src/dnstlsa.c +++ b/src/dnstlsa.c @@ -1,13 +1,15 @@ #include <unistd.h> + #include "buffer.h" -#include "stralloc.h" +#include "byte.h" #include "dnsresolv.h" -#include "dns.h" #include "exit.h" -#include "logmsg.h" #include "getoptb.h" +#include "logmsg.h" #include "str.h" -#include "byte.h" +#include "stralloc.h" + +#include "dns.h" #define WHO "dnstlsa" @@ -15,7 +17,7 @@ static stralloc cn = {0}; static stralloc sa = {0}; static stralloc out = {0}; -int main(int argc,char **argv) +int main(int argc, char **argv) { int r; uint16 usage; @@ -29,68 +31,68 @@ int main(int argc,char **argv) int i, j, k; int verbose = 0; - if (!argv[1]) - logmsg(WHO,100,USAGE,"dnstlsa [-v] [-p port] [-u(dp)|-t(cp)] host (tcp on port 25 is default)" ); + if (!argv[1]) + logmsg(WHO, 100, USAGE, "dnstlsa [-v] [-p port] [-u(dp)|-t(cp)] host (tcp on port 25 is default)"); - while ((opt = getopt(argc,argv,"vutp:")) != opteof) { + while ((opt = getopt(argc, argv, "vutp:")) != opteof) { switch (opt) { case 'p': port = optarg; break; case 't': break; - case 'u': str_copy(proto,"._udp."); break; + case 'u': str_copy(proto, "._udp."); break; case 'v': verbose = 1; } } - if (optind < argc) - host = argv[optind++]; + if (optind < argc) host = argv[optind++]; - if (!stralloc_copyb(&sa, "_",1)) logmsg(WHO,111,FATAL,"out of memory"); - if (!stralloc_cats(&sa,port)) logmsg(WHO,111,FATAL,"out of memory"); - if (!stralloc_cats(&sa,proto)) logmsg(WHO,111,FATAL,"out of memory"); - if (!stralloc_cats(&sa,host)) logmsg(WHO,111,FATAL,"out of memory"); + if (!stralloc_copyb(&sa, "_", 1)) logmsg(WHO, 111, FATAL, "out of memory"); + if (!stralloc_cats(&sa, port)) logmsg(WHO, 111, FATAL, "out of memory"); + if (!stralloc_cats(&sa, proto)) logmsg(WHO, 111, FATAL, "out of memory"); + if (!stralloc_cats(&sa, host)) logmsg(WHO, 111, FATAL, "out of memory"); DNS_INIT - if (dns_cname(&cn,&sa) > 0) - { if ((r = dns_tlsa(&out,&cn)) < 0) _exit(1); } - else - if ((r = dns_tlsa(&out,&sa)) < 0) _exit(1); - if (!stralloc_0(&sa)) logmsg(WHO,111,FATAL,"out of memory"); - if (verbose) logmsg(WHO,0,INFO,B("checking for TLSA records: ",sa.s,"\n")); + if (dns_cname(&cn, &sa) > 0) { + if ((r = dns_tlsa(&out, &cn)) < 0) _exit(1); + } else if ((r = dns_tlsa(&out, &sa)) < 0) + _exit(1); + if (!stralloc_0(&sa)) logmsg(WHO, 111, FATAL, "out of memory"); + if (verbose) logmsg(WHO, 0, INFO, B("checking for TLSA records: ", sa.s, "\n")); if (r > 0 && out.len > 4) { for (i = 0; i <= out.len; i++) { - usage = (unsigned char) out.s[i]; - selector = (unsigned char) out.s[i + 1]; - type = (unsigned char) out.s[i + 2]; + usage = (unsigned char)out.s[i]; + selector = (unsigned char)out.s[i + 1]; + type = (unsigned char)out.s[i + 2]; - if (usage == 0) buffer_puts(buffer_1,"Usage: [0], "); - if (usage == 1) buffer_puts(buffer_1,"Usage: [1], "); - if (usage == 2) buffer_puts(buffer_1,"Usage: [2], "); - if (usage == 3) buffer_puts(buffer_1,"Usage: [3], "); + if (usage == 0) buffer_puts(buffer_1, "Usage: [0], "); + if (usage == 1) buffer_puts(buffer_1, "Usage: [1], "); + if (usage == 2) buffer_puts(buffer_1, "Usage: [2], "); + if (usage == 3) buffer_puts(buffer_1, "Usage: [3], "); - if (selector == 0) buffer_puts(buffer_1,"Selector: [0], "); - if (selector == 1) buffer_puts(buffer_1,"Selector: [1], "); + if (selector == 0) buffer_puts(buffer_1, "Selector: [0], "); + if (selector == 1) buffer_puts(buffer_1, "Selector: [1], "); - if (type == 0) buffer_puts(buffer_1,"Type: [0] "); // full cert - if (type == 1) buffer_puts(buffer_1,"Type: [1] "); // sha256 - if (type == 2) buffer_puts(buffer_1,"Type: [2] "); // sha512 + if (type == 0) buffer_puts(buffer_1, "Type: [0] "); // full cert + if (type == 1) buffer_puts(buffer_1, "Type: [1] "); // sha256 + if (type == 2) buffer_puts(buffer_1, "Type: [2] "); // sha512 /* Staff of Ra "(is) six kadams high." However, the builder (h)as to subtract one kadam out of respect for the Hebrew God. */ for (j = i + 3, k = 0; j <= out.len; ++j) { - ch = (unsigned char) out.s[j]; + ch = (unsigned char)out.s[j]; if ((type == 1 && k == 32) || (type == 2 && k == 64)) { - buffer_putsflush(buffer_1,"\n"); - i = j - 1; break; - } else { - buffer_put(buffer_1,"0123456789abcdef" + (ch >> 4),1); - buffer_put(buffer_1,"0123456789abcdef" + (ch & 0x0f),1); + buffer_putsflush(buffer_1, "\n"); + i = j - 1; + break; + } else { + buffer_put(buffer_1, "0123456789abcdef" + (ch >> 4), 1); + buffer_put(buffer_1, "0123456789abcdef" + (ch & 0x0f), 1); k++; } } } } - + _exit(0); } diff --git a/src/dnstxt.c b/src/dnstxt.c index 385928e..a838d64 100644 --- a/src/dnstxt.c +++ b/src/dnstxt.c @@ -1,32 +1,32 @@ #include <unistd.h> + #include "buffer.h" -#include "stralloc.h" #include "dnsresolv.h" -#include "dns.h" #include "exit.h" #include "logmsg.h" +#include "stralloc.h" + +#include "dns.h" #define WHO "dnstext" stralloc sa = {0}; stralloc out = {0}; -int main(int argc,char **argv) +int main(int argc, char **argv) { int r; - if (!argv[1]) - logmsg(WHO,100,USAGE,"dnstxt fqdn"); + if (!argv[1]) logmsg(WHO, 100, USAGE, "dnstxt fqdn"); - if (!stralloc_copys(&sa,argv[1])) - logmsg(WHO,111,FATAL,"out of memory"); + if (!stralloc_copys(&sa, argv[1])) logmsg(WHO, 111, FATAL, "out of memory"); DNS_INIT - if ((r = dns_txt(&out,&sa)) < 0) _exit(1); + if ((r = dns_txt(&out, &sa)) < 0) _exit(1); if (r > 0) { - buffer_put(buffer_1,out.s,out.len); - buffer_putsflush(buffer_1,"\n"); + buffer_put(buffer_1, out.s, out.len); + buffer_putsflush(buffer_1, "\n"); } - + _exit(0); } diff --git a/src/except.c b/src/except.c index edee976..2d2df4a 100644 --- a/src/except.c +++ b/src/except.c @@ -1,7 +1,8 @@ #include <unistd.h> -#include "wait.h" -#include "logmsg.h" + #include "exit.h" +#include "logmsg.h" +#include "wait.h" #define WHO "except" @@ -10,25 +11,21 @@ int main(int argc, char **argv) int pid; int wstat; - if (!argv[1]) - logmsg(WHO,100,USAGE,"except program [ arg ... ]"); + if (!argv[1]) logmsg(WHO, 100, USAGE, "except program [ arg ... ]"); pid = fork(); - if (pid == -1) - logmsg(WHO,111,FATAL,"unable to fork: "); + if (pid == -1) logmsg(WHO, 111, FATAL, "unable to fork: "); if (pid == 0) { - execvp(argv[1],argv + 1); + execvp(argv[1], argv + 1); if (errno) _exit(111); _exit(100); } - if (wait_pid(&wstat,pid) == -1) - logmsg(WHO,111,FATAL,"wait failed"); - if (wait_crashed(wstat)) - logmsg(WHO,111,FATAL,"child crashed"); + if (wait_pid(&wstat, pid) == -1) logmsg(WHO, 111, FATAL, "wait failed"); + if (wait_crashed(wstat)) logmsg(WHO, 111, FATAL, "child crashed"); switch (wait_exitcode(wstat)) { - case 0: _exit(100); - case 111: logmsg(WHO,111,FATAL,"temporary child error"); - default: _exit(0); + case 0: _exit(100); + case 111: logmsg(WHO, 111, FATAL, "temporary child error"); + default: _exit(0); } } diff --git a/src/fastforward.c b/src/fastforward.c index 52e2e12..a702d41 100644 --- a/src/fastforward.c +++ b/src/fastforward.c @@ -1,42 +1,44 @@ -#include <sys/types.h> #include <sys/stat.h> +#include <sys/types.h> #include <unistd.h> -#include "readclose.h" -#include "stralloc.h" + +#include "alloc.h" #include "buffer.h" -#include "strset.h" -#include "getoptb.h" -#include "exit.h" -#include "logmsg.h" +#include "byte.h" +#include "case.h" +#include "cdbread.h" #include "env.h" -#include "sig.h" -#include "qmail.h" +#include "exit.h" #include "fmt.h" -#include "case.h" -#include "alloc.h" +#include "getoptb.h" +#include "logmsg.h" +#include "open.h" +#include "readclose.h" #include "seek.h" -#include "wait.h" -#include "byte.h" +#include "sig.h" #include "str.h" -#include "open.h" -#include "cdbread.h" +#include "stralloc.h" +#include "wait.h" + +#include "qmail.h" +#include "strset.h" #define WHO "fastforward" static void usage() { - logmsg(WHO,100,USAGE,"fastforward [ -nNpP ] data.cdb"); + logmsg(WHO, 100, USAGE, "fastforward [ -nNpP ] data.cdb"); } static void nomem() { - logmsg(WHO,111,FATAL,"out of memory"); + logmsg(WHO, 111, FATAL, "out of memory"); } static void print(char *s) { char ch; while ((ch = *s++)) { - buffer_put(buffer_2,&ch,1); + buffer_put(buffer_2, &ch, 1); } } @@ -45,7 +47,7 @@ static void printsafe(char *s) char ch; while ((ch = *s++)) { if (ch < 32) ch = '_'; - buffer_put(buffer_2,&ch,1); + buffer_put(buffer_2, &ch, 1); } } @@ -53,16 +55,16 @@ struct qmail qq; char qp[FMT_ULONG]; char qqbuf[1]; -ssize_t qqwrite(int fd,char *buf,int len) +ssize_t qqwrite(int fd, char *buf, int len) { - qmail_put(&qq,buf,len); + qmail_put(&qq, buf, len); return len; } -buffer bufq = BUFFER_INIT(qqwrite,-1,qqbuf,sizeof(qqbuf)); +buffer bufq = BUFFER_INIT(qqwrite, -1, qqbuf, sizeof(qqbuf)); char messbuf[4096]; -buffer mess = BUFFER_INIT(read,0,messbuf,sizeof(messbuf)); +buffer mess = BUFFER_INIT(read, 0, messbuf, sizeof(messbuf)); int flagdeliver = 1; int flagpassthrough = 0; @@ -84,27 +86,22 @@ void dofile(char *fn) int i; int j; - if (!stralloc_copys(&mailinglist,"")) nomem(); + if (!stralloc_copys(&mailinglist, "")) nomem(); fd = open_read(fn); - if (fd == -1) - logmsg(WHO,111,FATAL,B("unable to read: ",fn)); - if (fstat(fd,&st) == -1) - logmsg(WHO,111,FATAL,B("unable to read: ",fn)); - if ((st.st_mode & 0444) != 0444) - logmsg(WHO,111,FATAL,B(fn," is not world-readable")); - if (readclose_append(fd,&mailinglist,1024) == -1) - logmsg(WHO,111,FATAL,B("unable to read: ",fn)); + if (fd == -1) logmsg(WHO, 111, FATAL, B("unable to read: ", fn)); + if (fstat(fd, &st) == -1) logmsg(WHO, 111, FATAL, B("unable to read: ", fn)); + if ((st.st_mode & 0444) != 0444) logmsg(WHO, 111, FATAL, B(fn, " is not world-readable")); + if (readclose_append(fd, &mailinglist, 1024) == -1) logmsg(WHO, 111, FATAL, B("unable to read: ", fn)); i = 0; for (j = 0; j < mailinglist.len; ++j) if (!mailinglist.s[j]) { if ((mailinglist.s[i] == '.') || (mailinglist.s[i] == '/')) { - if (!stralloc_cats(&todo,mailinglist.s + i)) nomem(); + if (!stralloc_cats(&todo, mailinglist.s + i)) nomem(); if (!stralloc_0(&todo)) nomem(); - } - else if ((mailinglist.s[i] == '&') && (j - i < 900)) { - if (!stralloc_cats(&todo,mailinglist.s + i)) nomem(); + } else if ((mailinglist.s[i] == '&') && (j - i < 900)) { + if (!stralloc_cats(&todo, mailinglist.s + i)) nomem(); if (!stralloc_0(&todo)) nomem(); } i = j + 1; @@ -120,53 +117,52 @@ struct cdb cdb; void cdbreaderror() { - logmsg(WHO,111,FATAL,B("unable to read: ",fncdb)); + logmsg(WHO, 111, FATAL, B("unable to read: ", fncdb)); } -int findtarget(int flagwild,char *prepend,char *addr) +int findtarget(int flagwild, char *prepend, char *addr) { int r; int at; - if (!stralloc_copys(&key,prepend)) nomem(); - if (!stralloc_cats(&key,addr)) nomem(); - case_lowerb(key.s,key.len); + if (!stralloc_copys(&key, prepend)) nomem(); + if (!stralloc_cats(&key, addr)) nomem(); + case_lowerb(key.s, key.len); - r = cdb_find(&cdb,key.s,key.len); + r = cdb_find(&cdb, key.s, key.len); if (r == -1) cdbreaderror(); - if (r) return 1; + if (r) return 1; if (!flagwild) return 0; - at = str_rchr(addr,'@'); + at = str_rchr(addr, '@'); if (!addr[at]) return 0; - if (!stralloc_copys(&key,prepend)) nomem(); - if (!stralloc_cats(&key,addr + at)) nomem(); - case_lowerb(key.s,key.len); + if (!stralloc_copys(&key, prepend)) nomem(); + if (!stralloc_cats(&key, addr + at)) nomem(); + case_lowerb(key.s, key.len); - r = cdb_find(&cdb,key.s,key.len); + r = cdb_find(&cdb, key.s, key.len); if (r == -1) cdbreaderror(); - if (r) return 1; + if (r) return 1; - if (!stralloc_copys(&key,prepend)) nomem(); - if (!stralloc_catb(&key,addr,at + 1)) nomem(); - case_lowerb(key.s,key.len); + if (!stralloc_copys(&key, prepend)) nomem(); + if (!stralloc_catb(&key, addr, at + 1)) nomem(); + case_lowerb(key.s, key.len); - r = cdb_find(&cdb,key.s,key.len); + r = cdb_find(&cdb, key.s, key.len); if (r == -1) cdbreaderror(); if (r) return 1; return 0; } -int gettarget(int flagwild,char *prepend,char *addr) +int gettarget(int flagwild, char *prepend, char *addr) { - if (!findtarget(flagwild,prepend,addr)) return 0; + if (!findtarget(flagwild, prepend, addr)) return 0; dlen = cdb_datalen(&cdb); - if (!stralloc_ready(&data,(unsigned int) dlen)) nomem(); + if (!stralloc_ready(&data, (unsigned int)dlen)) nomem(); data.len = dlen; - if (cdb_read(&cdb,data.s,data.len,cdb_datapos(&cdb)) == -1) - cdbreaderror(); + if (cdb_read(&cdb, data.s, data.len, cdb_datapos(&cdb)) == -1) cdbreaderror(); return 1; } @@ -191,8 +187,7 @@ void doprogram(char *arg) args[2] = "-c"; args[3] = arg + 1; args[4] = 0; - } - else { + } else { args[0] = "sh"; args[1] = "-c"; args[2] = arg + 1; @@ -200,27 +195,30 @@ void doprogram(char *arg) } switch (child = vfork()) { - case -1: - logmsg(WHO,111,FATAL,"unable to fork: "); + case -1: logmsg(WHO, 111, FATAL, "unable to fork: "); case 0: sig_pipedefault(); - execvp(*args,args); - logmsg(WHO,111,FATAL,B("unable to run: ",arg)); + execvp(*args, args); + logmsg(WHO, 111, FATAL, B("unable to run: ", arg)); } - wait_pid(&wstat,child); - if (wait_crashed(wstat)) - logmsg(WHO,111,FATAL,B("child crashed in: ",arg)); + wait_pid(&wstat, child); + if (wait_crashed(wstat)) logmsg(WHO, 111, FATAL, B("child crashed in: ", arg)); switch (wait_exitcode(wstat)) { - case 64: case 65: case 70: case 76: case 77: case 78: case 112: + case 64: + case 65: + case 70: + case 76: + case 77: + case 78: + case 112: case 100: _exit(100); - case 0: break; - default: _exit(111); + case 0: break; + default: _exit(111); } - if (seek_begin(0) == -1) - logmsg(WHO,111,FATAL,"unable to rewind input: "); + if (seek_begin(0) == -1) logmsg(WHO, 111, FATAL, "unable to rewind input: "); } void dodata() @@ -234,11 +232,10 @@ void dodata() if ((data.s[i] == '|') || (data.s[i] == '!')) doprogram(data.s + i); else if ((data.s[i] == '.') || (data.s[i] == '/')) { - if (!stralloc_cats(&todo,data.s + i)) nomem(); + if (!stralloc_cats(&todo, data.s + i)) nomem(); if (!stralloc_0(&todo)) nomem(); - } - else if ((data.s[i] == '&') && (j - i < 900)) { - if (!stralloc_cats(&todo,data.s + i)) nomem(); + } else if ((data.s[i] == '&') && (j - i < 900)) { + if (!stralloc_cats(&todo, data.s + i)) nomem(); if (!stralloc_0(&todo)) nomem(); } i = j + 1; @@ -248,33 +245,33 @@ void dodata() void dorecip(char *addr) { - if (!findtarget(0,"?",addr)) - if (gettarget(0,":",addr)) { + if (!findtarget(0, "?", addr)) + if (gettarget(0, ":", addr)) { dodata(); return; } - if (!stralloc_cats(&forward,addr)) nomem(); + if (!stralloc_cats(&forward, addr)) nomem(); if (!stralloc_0(&forward)) nomem(); } void doorigrecip(char *addr) { if (sender.len) - if ((sender.len != 4) || byte_diff(sender.s,4,"#@[]")) - if (gettarget(1,"?",addr)) - if (!stralloc_copy(&sender,&data)) nomem(); - if (!gettarget(1,":",addr)) + if ((sender.len != 4) || byte_diff(sender.s, 4, "#@[]")) + if (gettarget(1, "?", addr)) + if (!stralloc_copy(&sender, &data)) nomem(); + if (!gettarget(1, ":", addr)) if (flagpassthrough) _exit(0); else - logmsg(WHO,100,ERROR,"Sorry, no mailbox here by that name. (#5.1.1)"); + logmsg(WHO, 100, ERROR, "Sorry, no mailbox here by that name. (#5.1.1)"); dodata(); } stralloc recipient = {0}; int flagdefault = 0; -int main(int argc,char **argv) +int main(int argc, char **argv) { int opt; char *x; @@ -287,20 +284,20 @@ int main(int argc,char **argv) x = env_get("SENDER"); if (!x) x = "original envelope sender"; - if (!stralloc_copys(&sender,x)) nomem(); + if (!stralloc_copys(&sender, x)) nomem(); - if (!stralloc_copys(&forward,"")) nomem(); + if (!stralloc_copys(&forward, "")) nomem(); if (!strset_init(&done)) nomem(); - while ((opt = getopt(argc,argv,"nNpPdD")) != opteof) - switch (opt) { + while ((opt = getopt(argc, argv, "nNpPdD")) != opteof) switch (opt) + { case 'n': flagdeliver = 0; break; case 'N': flagdeliver = 1; break; case 'p': flagpassthrough = 1; break; case 'P': flagpassthrough = 0; break; case 'd': flagdefault = 1; break; case 'D': flagdefault = 0; break; - default: usage(); + default: usage(); } argv += optind; @@ -308,25 +305,24 @@ int main(int argc,char **argv) if (!fncdb) usage(); fdcdb = open_read(fncdb); if (fdcdb == -1) cdbreaderror(); - cdb_init(&cdb,fdcdb); + cdb_init(&cdb, fdcdb); if (flagdefault) { x = env_get("DEFAULT"); if (!x) x = env_get("EXT"); - if (!x) logmsg(WHO,100,FATAL,"$DEFAULT or $EXT must be set"); - if (!stralloc_copys(&recipient,x)) nomem(); - if (!stralloc_cats(&recipient,"@")) nomem(); + if (!x) logmsg(WHO, 100, FATAL, "$DEFAULT or $EXT must be set"); + if (!stralloc_copys(&recipient, x)) nomem(); + if (!stralloc_cats(&recipient, "@")) nomem(); x = env_get("HOST"); - if (!x) logmsg(WHO,100,FATAL,"$HOST must be set"); - if (!stralloc_cats(&recipient,x)) nomem(); + if (!x) logmsg(WHO, 100, FATAL, "$HOST must be set"); + if (!stralloc_cats(&recipient, x)) nomem(); if (!stralloc_0(&recipient)) nomem(); x = recipient.s; - } - else { + } else { x = env_get("RECIPIENT"); - if (!x) logmsg(WHO,100,FATAL,"$RECIPIENT must be set"); + if (!x) logmsg(WHO, 100, FATAL, "$RECIPIENT must be set"); } - if (!strset_add(&done,x)) nomem(); + if (!strset_add(&done, x)) nomem(); doorigrecip(x); while (todo.len) { @@ -334,12 +330,12 @@ int main(int argc,char **argv) while ((i > 0) && todo.s[i - 1]) --i; todo.len = i; - if (strset_in(&done,todo.s + i)) continue; + if (strset_in(&done, todo.s + i)) continue; x = alloc(str_len(todo.s + i) + 1); if (!x) nomem(); - str_copy(x,todo.s + i); - if (!strset_add(&done,x)) nomem(); + str_copy(x, todo.s + i); + if (!strset_add(&done, x)) nomem(); x = todo.s + i; if (*x == 0) @@ -376,24 +372,22 @@ int main(int argc,char **argv) _exit(flagpassthrough ? 99 : 0); } - if (qmail_open(&qq) == -1) - logmsg(WHO,111,FATAL,"unable to fork: "); - qmail_puts(&qq,dtline); - if (buffer_copy(&bufq,&mess) != 0) - logmsg(WHO,111,FATAL,"unable to read message: "); + if (qmail_open(&qq) == -1) logmsg(WHO, 111, FATAL, "unable to fork: "); + qmail_puts(&qq, dtline); + if (buffer_copy(&bufq, &mess) != 0) logmsg(WHO, 111, FATAL, "unable to read message: "); buffer_flush(&bufq); - qp[fmt_ulong(qp,qmail_qp(&qq))] = 0; + qp[fmt_ulong(qp, qmail_qp(&qq))] = 0; - qmail_from(&qq,sender.s); + qmail_from(&qq, sender.s); while (forward.len) { i = forward.len - 1; while ((i > 0) && forward.s[i - 1]) --i; forward.len = i; - qmail_to(&qq,forward.s + i); + qmail_to(&qq, forward.s + i); } x = qmail_close(&qq); - if (*x) logmsg(WHO,*x == 'D' ? 100 : 111,FATAL,x + 1); - logmsg(WHO,flagpassthrough ? 99 : 0,LOG,B("qp ",qp)); + if (*x) logmsg(WHO, *x == 'D' ? 100 : 111, FATAL, x + 1); + logmsg(WHO, flagpassthrough ? 99 : 0, LOG, B("qp ", qp)); } @@ -1,9 +1,16 @@ -#include <sys/types.h> -#include <sys/stat.h> #include "fifo.h" +#include <sys/stat.h> +#include <sys/types.h> + #ifdef HASMKFIFO -int fifo_make(char *fn, int mode) { return mkfifo(fn,mode); } +int fifo_make(char *fn, int mode) +{ + return mkfifo(fn, mode); +} #else -int fifo_make(char *fn, int mode) { return mknod(fn,S_IFIFO | mode,0); } +int fifo_make(char *fn, int mode) +{ + return mknod(fn, S_IFIFO | mode, 0); +} #endif diff --git a/src/fmtqfn.c b/src/fmtqfn.c index 139cccf..8cb8644 100644 --- a/src/fmtqfn.c +++ b/src/fmtqfn.c @@ -1,22 +1,33 @@ #include "fmtqfn.h" + #include "fmt.h" + #include "auto_split.h" -unsigned int fmtqfn(char *s,char *dirslash,unsigned long id,int flagsplit) +unsigned int fmtqfn(char *s, char *dirslash, unsigned long id, int flagsplit) { - unsigned int len; - unsigned int i; + unsigned int len; + unsigned int i; + + len = 0; + i = fmt_str(s, dirslash); + len += i; + if (s) s += i; - len = 0; - i = fmt_str(s,dirslash); len += i; if (s) s += i; + if (flagsplit) { + i = fmt_ulong(s, id % auto_split); + len += i; + if (s) s += i; + i = fmt_str(s, "/"); + len += i; + if (s) s += i; + } - if (flagsplit) { - i = fmt_ulong(s,id % auto_split); len += i; if (s) s += i; - i = fmt_str(s,"/"); len += i; if (s) s += i; - } + i = fmt_ulong(s, id); + len += i; + if (s) s += i; + if (s) *s++ = 0; + ++len; - i = fmt_ulong(s,id); len += i; if (s) s += i; - if (s) *s++ = 0; ++len; - - return len; + return len; } diff --git a/src/forward.c b/src/forward.c index 7cc8e53..e8c7eb0 100644 --- a/src/forward.c +++ b/src/forward.c @@ -1,28 +1,33 @@ #include <unistd.h> -#include "sig.h" -#include "exit.h" -#include "env.h" -#include "qmail.h" -#include "logmsg.h" + #include "buffer.h" +#include "env.h" +#include "exit.h" #include "fmt.h" +#include "logmsg.h" +#include "sig.h" + +#include "qmail.h" #define WHO "forward" -void die_nomem() { logmsg(WHO,111,FATAL,"out of memory"); } +void die_nomem() +{ + logmsg(WHO, 111, FATAL, "out of memory"); +} struct qmail qqt; ssize_t mywrite(int fd, char *buf, int len) { - qmail_put(&qqt,buf,len); + qmail_put(&qqt, buf, len); return len; } char inbuf[BUFFER_INSIZE]; -buffer bi = BUFFER_INIT(read,0,inbuf,sizeof(inbuf)); +buffer bi = BUFFER_INIT(read, 0, inbuf, sizeof(inbuf)); char outbuf[1]; -buffer bo = BUFFER_INIT(mywrite,-1,outbuf,sizeof(outbuf)); +buffer bo = BUFFER_INIT(mywrite, -1, outbuf, sizeof(outbuf)); char num[FMT_ULONG]; @@ -31,28 +36,24 @@ int main(int argc, char **argv) char *sender; char *dtline; char *qqx; - + sig_pipeignore(); - + sender = env_get("NEWSENDER"); - if (!sender) - logmsg(WHO,100,FATAL,"NEWSENDER not set"); + if (!sender) logmsg(WHO, 100, FATAL, "NEWSENDER not set"); dtline = env_get("DTLINE"); - if (!dtline) - logmsg(WHO,100,FATAL,"DTLINE not set"); - - if (qmail_open(&qqt) == -1) - logmsg(WHO,111,FATAL,"unable to fork: "); - qmail_puts(&qqt,dtline); - if (buffer_copy(&bo,&bi) != 0) - logmsg(WHO,111,FATAL,"unable to read message: "); + if (!dtline) logmsg(WHO, 100, FATAL, "DTLINE not set"); + + if (qmail_open(&qqt) == -1) logmsg(WHO, 111, FATAL, "unable to fork: "); + qmail_puts(&qqt, dtline); + if (buffer_copy(&bo, &bi) != 0) logmsg(WHO, 111, FATAL, "unable to read message: "); buffer_flush(&bo); - num[fmt_ulong(num,qmail_qp(&qqt))] = 0; - - qmail_from(&qqt,sender); - while (*++argv) qmail_to(&qqt,*argv); + num[fmt_ulong(num, qmail_qp(&qqt))] = 0; + + qmail_from(&qqt, sender); + while (*++argv) qmail_to(&qqt, *argv); qqx = qmail_close(&qqt); - if (*qqx) logmsg(WHO,*qqx == 'D' ? 100 : 111,FATAL,qqx + 1); - logmsg(WHO,0,LOG,B("qp ",num)); + if (*qqx) logmsg(WHO, *qqx == 'D' ? 100 : 111, FATAL, qqx + 1); + logmsg(WHO, 0, LOG, B("qp ", num)); } diff --git a/src/gfrom.c b/src/gfrom.c index c04b65f..09eaa80 100644 --- a/src/gfrom.c +++ b/src/gfrom.c @@ -1,8 +1,12 @@ -#include "str.h" #include "gfrom.h" -int gfrom(char *s,int len) +#include "str.h" + +int gfrom(char *s, int len) { - while ((len > 0) && (*s == '>')) { ++s; --len; } - return (len >= 5) && !str_diffn(s,"From ",5); + while ((len > 0) && (*s == '>')) { + ++s; + --len; + } + return (len >= 5) && !str_diffn(s, "From ", 5); } diff --git a/src/headerbody.c b/src/headerbody.c index 82c5684..4173824 100644 --- a/src/headerbody.c +++ b/src/headerbody.c @@ -1,16 +1,18 @@ -#include "stralloc.h" +#include "headerbody.h" + #include "buffer.h" #include "getln.h" +#include "stralloc.h" + #include "hfield.h" -#include "headerbody.h" -static int getsa(buffer *b,stralloc *sa,int *match) +static int getsa(buffer *b, stralloc *sa, int *match) { if (!*match) return 0; - if (getln(b,sa,match,'\n') == -1) return -1; + if (getln(b, sa, match, '\n') == -1) return -1; if (*match) return 1; if (!sa->len) return 0; - if (!stralloc_append(sa,"\n")) return -1; + if (!stralloc_append(sa, "\n")) return -1; return 1; } @@ -18,7 +20,7 @@ static int getsa(buffer *b,stralloc *sa,int *match) static stralloc line = {0}; static stralloc nextline = {0}; -int headerbody(b,dohf,hdone,dobl) +int headerbody(b, dohf, hdone, dobl) buffer *b; void (*dohf)(); void (*hdone)(); @@ -30,9 +32,8 @@ void (*dobl)(); flaglineok = 0; for (;;) { - switch (getsa(b,&nextline,&match)) { - case -1: - return -1; + switch (getsa(b, &nextline, &match)) { + case -1: return -1; case 0: if (flaglineok) dohf(&line); hdone(); @@ -42,7 +43,7 @@ void (*dobl)(); if (flaglineok) { if ((nextline.s[0] == ' ') || (nextline.s[0] == '\t')) { - if (!stralloc_cat(&line,&nextline)) return -1; + if (!stralloc_cat(&line, &nextline)) return -1; continue; } dohf(&line); @@ -54,14 +55,14 @@ void (*dobl)(); break; } - if (stralloc_starts(&nextline,"From ")) { - if (!stralloc_copys(&line,"MBOX-Line: ")) return -1; - if (!stralloc_cat(&line,&nextline)) return -1; - } else if (hfield_valid(nextline.s,nextline.len)) { - if (!stralloc_copy(&line,&nextline)) return -1; + if (stralloc_starts(&nextline, "From ")) { + if (!stralloc_copys(&line, "MBOX-Line: ")) return -1; + if (!stralloc_cat(&line, &nextline)) return -1; + } else if (hfield_valid(nextline.s, nextline.len)) { + if (!stralloc_copy(&line, &nextline)) return -1; } else { hdone(); - if (!stralloc_copys(&line,"\n")) return -1; + if (!stralloc_copys(&line, "\n")) return -1; dobl(&line); dobl(&nextline); break; @@ -69,10 +70,10 @@ void (*dobl)(); flaglineok = 1; } - for (;;) - switch (getsa(b,&nextline,&match)) { + for (;;) switch (getsa(b, &nextline, &match)) + { case -1: return -1; - case 0: return 0; - case 1: dobl(&nextline); + case 0: return 0; + case 1: dobl(&nextline); } } diff --git a/src/hfield.c b/src/hfield.c index 2376e1f..3043327 100644 --- a/src/hfield.c +++ b/src/hfield.c @@ -1,39 +1,38 @@ #include "hfield.h" static char *(hname[]) = { - "unknown-header" -, "sender" -, "from" -, "reply-to" -, "to" -, "cc" -, "bcc" -, "date" -, "message-id" -, "subject" -, "resent-sender" -, "resent-from" -, "resent-reply-to" -, "resent-to" -, "resent-cc" -, "resent-bcc" -, "resent-date" -, "resent-message-id" -, "return-receipt-to" -, "errors-to" -, "apparently-to" -, "received" -, "return-path" -, "delivered-to" -, "content-length" -, "content-type" -, "content-transfer-encoding" -, "notice-requested-upon-delivery-to" -, "mail-followup-to" -, 0 -}; + "unknown-header", + "sender", + "from", + "reply-to", + "to", + "cc", + "bcc", + "date", + "message-id", + "subject", + "resent-sender", + "resent-from", + "resent-reply-to", + "resent-to", + "resent-cc", + "resent-bcc", + "resent-date", + "resent-message-id", + "return-receipt-to", + "errors-to", + "apparently-to", + "received", + "return-path", + "delivered-to", + "content-length", + "content-type", + "content-transfer-encoding", + "notice-requested-upon-delivery-to", + "mail-followup-to", + 0}; -static int hmatch( char *s,int len,char *t) +static int hmatch(char *s, int len, char *t) { int i; char ch; @@ -54,19 +53,18 @@ static int hmatch( char *s,int len,char *t) } } -int hfield_known(char *s,int len) +int hfield_known(char *s, int len) { int i; char *t; for (i = 1; (t = hname[i]); ++i) - if (hmatch(s,len,t)) - return i; - - return 0; + if (hmatch(s, len, t)) return i; + + return 0; } -int hfield_valid(char *s,int len) +int hfield_valid(char *s, int len) { int i; int j; @@ -76,11 +74,10 @@ int hfield_valid(char *s,int len) if (s[j] == ':') break; if (j >= len) return 0; - + while (j) { ch = s[j - 1]; - if ((ch != ' ') && (ch != '\t')) - break; + if ((ch != ' ') && (ch != '\t')) break; --j; } if (!j) return 0; @@ -89,11 +86,11 @@ int hfield_valid(char *s,int len) ch = s[i]; if (ch <= 32) return 0; if (ch >= 127) return 0; - } + } return 1; } -unsigned int hfield_skipname(char *s,int len) +unsigned int hfield_skipname(char *s, int len) { int i; char ch; @@ -102,12 +99,11 @@ unsigned int hfield_skipname(char *s,int len) if (s[i] == ':') break; if (i < len) ++i; - while (i < len) { - ch = s[i]; - if ((ch != '\t') && (ch != '\n') && (ch != '\r') && (ch != ' ')) - break; - ++i; - } + while (i < len) { + ch = s[i]; + if ((ch != '\t') && (ch != '\n') && (ch != '\r') && (ch != ' ')) break; + ++i; + } return i; } @@ -1,163 +1,165 @@ +#include "hier.h" + +#include "fifo.h" +#include "fmt.h" + #include "auto_qmail.h" #include "auto_split.h" #include "auto_uids.h" -#include "fmt.h" -#include "fifo.h" #include "ipalloc.h" #include "tcpto.h" -#include "hier.h" char buf[100 + FMT_ULONG]; -void dsplit(char *base,int uid,int mode) /* base must be under 100 bytes */ +void dsplit(char *base, int uid, int mode) /* base must be under 100 bytes */ { char *x; unsigned long i; - d(auto_qmail,base,uid,auto_gidq,mode); + d(auto_qmail, base, uid, auto_gidq, mode); for (i = 0; i < auto_split; ++i) { x = buf; - x += fmt_str(x,base); - x += fmt_str(x,"/"); - x += fmt_ulong(x,i); + x += fmt_str(x, base); + x += fmt_str(x, "/"); + x += fmt_ulong(x, i); *x = 0; - d(auto_qmail,buf,uid,auto_gidq,mode); + d(auto_qmail, buf, uid, auto_gidq, mode); } } void hier() { - h(auto_qmail,auto_uido,auto_gidq,0755); - - d(auto_qmail,"control",auto_uido,auto_gidq,0755); - d(auto_qmail,"users",auto_uido,auto_gidq,0755); - d(auto_qmail,"bin",auto_uido,auto_gidq,0755); - d(auto_qmail,"alias",auto_uida,auto_gidq,02755); - - d(auto_qmail,"queue",auto_uidq,auto_gidq,0750); - d(auto_qmail,"queue/pid",auto_uidq,auto_gidq,0700); - d(auto_qmail,"queue/bounce",auto_uids,auto_gidq,0700); - - dsplit("queue/dkim",auto_uidq,0750); - dsplit("queue/mess",auto_uidq,0750); - dsplit("queue/todo",auto_uidq,0750); - dsplit("queue/intd",auto_uidq,0700); - dsplit("queue/info",auto_uids,0700); - dsplit("queue/local",auto_uids,0700); - dsplit("queue/remote",auto_uids,0700); - - d(auto_qmail,"queue/lock",auto_uidq,auto_gidq,0750); - z(auto_qmail,"queue/lock/tcpto",TCPTO_BUFSIZ,auto_uidr,auto_gidq,0644); - z(auto_qmail,"queue/lock/sendmutex",0,auto_uids,auto_gidq,0600); - p(auto_qmail,"queue/lock/trigger",auto_uids,auto_gidq,0622); - - c(auto_qmail,"bin","qmail-queue",auto_uidq,auto_gidq,04711); - c(auto_qmail,"bin","qmail-qmaint",auto_uidq,auto_gidq,0711); - c(auto_qmail,"bin","qmail-lspawn",auto_uido,auto_gidq,0700); - c(auto_qmail,"bin","qmail-start",auto_uido,auto_gidq,0700); - c(auto_qmail,"bin","qmail-getpw",auto_uido,auto_gidq,0711); - c(auto_qmail,"bin","qmail-local",auto_uido,auto_gidq,0711); - c(auto_qmail,"bin","qmail-remote",auto_uido,auto_gidq,0711); - c(auto_qmail,"bin","qmail-smtpam",auto_uido,auto_gidq,0711); - c(auto_qmail,"bin","qmail-rspawn",auto_uido,auto_gidq,0711); - c(auto_qmail,"bin","qmail-clean",auto_uido,auto_gidq,0711); - c(auto_qmail,"bin","qmail-send",auto_uido,auto_gidq,0711); - c(auto_qmail,"bin","qmail-todo",auto_uido,auto_gidq,0711); - c(auto_qmail,"bin","splogger",auto_uido,auto_gidq,0711); - c(auto_qmail,"bin","qmail-newu",auto_uido,auto_gidq,0700); - c(auto_qmail,"bin","qmail-newmrh",auto_uido,auto_gidq,0700); - - c(auto_qmail,"bin","qmail-dkim",auto_uidq,auto_gidq,0711); - c(auto_qmail,"bin","qmail-dksign",auto_uidq,auto_gidq,04711); - c(auto_qmail,"bin","qmail-dkverify",auto_uidq,auto_gidq,04711); - - c(auto_qmail,"bin","qmail-authuser",auto_uido,auto_gidq,06711); - c(auto_qmail,"bin","qmail-vmailuser",auto_uido,auto_gidq,06711); - c(auto_qmail,"bin","qmail-postgrey",auto_uido,auto_gidq,06711); - c(auto_qmail,"bin","qmail-badloadertypes",auto_uido,auto_gidq,0711); - c(auto_qmail,"bin","qmail-badmimetypes",auto_uido,auto_gidq,0711); - c(auto_qmail,"bin","qmail-recipients",auto_uido,auto_gidq,0711); - c(auto_qmail,"bin","qmail-mfrules",auto_uido,auto_gidq,0711); - c(auto_qmail,"bin","qmail-mrtg",auto_uido,auto_gidq,0755); - c(auto_qmail,"bin","qmail-mrtg-queue",auto_uido,auto_gidq,0755); - - c(auto_qmail,"bin","qmail-pw2u",auto_uido,auto_gidq,0711); - c(auto_qmail,"bin","qmail-inject",auto_uido,auto_gidq,0755); - c(auto_qmail,"bin","qmail-showctl",auto_uido,auto_gidq,0755); - c(auto_qmail,"bin","qmail-qread",auto_uido,auto_gidq,0755); - c(auto_qmail,"bin","qmail-qstat",auto_uido,auto_gidq,0755); - c(auto_qmail,"bin","qmail-tcpto",auto_uido,auto_gidq,0755); - c(auto_qmail,"bin","qmail-tcpok",auto_uido,auto_gidq,0755); - c(auto_qmail,"bin","qmail-pop3d",auto_uido,auto_gidq,0755); - c(auto_qmail,"bin","qmail-popup",auto_uido,auto_gidq,0711); - c(auto_qmail,"bin","qmail-qmqpc",auto_uido,auto_gidq,0755); - c(auto_qmail,"bin","qmail-qmqpd",auto_uido,auto_gidq,0755); - c(auto_qmail,"bin","qmail-qmtpd",auto_uido,auto_gidq,0755); - c(auto_qmail,"bin","qmail-smtpd",auto_uido,auto_gidq,0755); - - c(auto_qmail,"bin","predate",auto_uido,auto_gidq,0755); - c(auto_qmail,"bin","datemail",auto_uido,auto_gidq,0755); - c(auto_qmail,"bin","mailsubj",auto_uido,auto_gidq,0755); - c(auto_qmail,"bin","sendmail",auto_uido,auto_gidq,0755); - c(auto_qmail,"bin","qreceipt",auto_uido,auto_gidq,0755); - c(auto_qmail,"bin","qbiff",auto_uido,auto_gidq,0755); - c(auto_qmail,"bin","forward",auto_uido,auto_gidq,0755); - c(auto_qmail,"bin","preline",auto_uido,auto_gidq,0755); - c(auto_qmail,"bin","condredirect",auto_uido,auto_gidq,0755); - c(auto_qmail,"bin","bouncesaying",auto_uido,auto_gidq,0755); - c(auto_qmail,"bin","except",auto_uido,auto_gidq,0755); - c(auto_qmail,"bin","maildirmake",auto_uido,auto_gidq,0755); - c(auto_qmail,"bin","maildir2mbox",auto_uido,auto_gidq,0755); - c(auto_qmail,"bin","maildirwatch",auto_uido,auto_gidq,0755); - - c(auto_qmail,"bin","fastforward",auto_uido,auto_gidq,0755); - c(auto_qmail,"bin","printforward",auto_uido,auto_gidq,0755); - c(auto_qmail,"bin","setforward",auto_uido,auto_gidq,0755); - c(auto_qmail,"bin","newaliases",auto_uido,auto_gidq,0755); - c(auto_qmail,"bin","printmaillist",auto_uido,auto_gidq,0755); - c(auto_qmail,"bin","setmaillist",auto_uido,auto_gidq,0755); - c(auto_qmail,"bin","newinclude",auto_uido,auto_gidq,0755); - c(auto_qmail,"bin","srsforward",auto_uido,auto_gidq,0755); - c(auto_qmail,"bin","srsreverse",auto_uido,auto_gidq,0755); - - c(auto_qmail,"bin","ipmeprint",auto_uido,auto_gidq,0755); - c(auto_qmail,"bin","spfquery",auto_uido,auto_gidq,0755); - c(auto_qmail,"bin","dnscname",auto_uido,auto_gidq,0755); - c(auto_qmail,"bin","dnsfq",auto_uido,auto_gidq,0755); - c(auto_qmail,"bin","dnsip",auto_uido,auto_gidq,0755); - c(auto_qmail,"bin","dnsmxip",auto_uido,auto_gidq,0755); - c(auto_qmail,"bin","dnsptr",auto_uido,auto_gidq,0755); - c(auto_qmail,"bin","dnstlsa",auto_uido,auto_gidq,0755); - c(auto_qmail,"bin","dnstxt",auto_uido,auto_gidq,0755); - c(auto_qmail,"bin","hostname",auto_uido,auto_gidq,0755); - - c(auto_qmail,"bin","columnt",auto_uido,auto_gidq,0755); - c(auto_qmail,"bin","ddist",auto_uido,auto_gidq,0755); - c(auto_qmail,"bin","deferrals",auto_uido,auto_gidq,0755); - c(auto_qmail,"bin","failures",auto_uido,auto_gidq,0755); - c(auto_qmail,"bin","matchup",auto_uido,auto_gidq,0755); - c(auto_qmail,"bin","recipients",auto_uido,auto_gidq,0755); - c(auto_qmail,"bin","rhosts",auto_uido,auto_gidq,0755); - c(auto_qmail,"bin","rxdelay",auto_uido,auto_gidq,0755); - c(auto_qmail,"bin","senders",auto_uido,auto_gidq,0755); - c(auto_qmail,"bin","successes",auto_uido,auto_gidq,0755); - c(auto_qmail,"bin","suids",auto_uido,auto_gidq,0755); - c(auto_qmail,"bin","tai64nfrac",auto_uido,auto_gidq,0755); - c(auto_qmail,"bin","xqp",auto_uido,auto_gidq,0755); - c(auto_qmail,"bin","xrecipient",auto_uido,auto_gidq,0755); - c(auto_qmail,"bin","xsender",auto_uido,auto_gidq,0755); - c(auto_qmail,"bin","zddist",auto_uido,auto_gidq,0755); - c(auto_qmail,"bin","zdeferrals",auto_uido,auto_gidq,0755); - c(auto_qmail,"bin","zfailures",auto_uido,auto_gidq,0755); - c(auto_qmail,"bin","zfailures",auto_uido,auto_gidq,0755); - c(auto_qmail,"bin","zoverall",auto_uido,auto_gidq,0755); - c(auto_qmail,"bin","zrecipients",auto_uido,auto_gidq,0755); - c(auto_qmail,"bin","zrhosts",auto_uido,auto_gidq,0755); - c(auto_qmail,"bin","zrxdelay",auto_uido,auto_gidq,0755); - c(auto_qmail,"bin","zsenders",auto_uido,auto_gidq,0755); - c(auto_qmail,"bin","zsendmail",auto_uido,auto_gidq,0755); - c(auto_qmail,"bin","zsuccesses",auto_uido,auto_gidq,0755); - c(auto_qmail,"bin","zsuids",auto_uido,auto_gidq,0755); + h(auto_qmail, auto_uido, auto_gidq, 0755); + + d(auto_qmail, "control", auto_uido, auto_gidq, 0755); + d(auto_qmail, "users", auto_uido, auto_gidq, 0755); + d(auto_qmail, "bin", auto_uido, auto_gidq, 0755); + d(auto_qmail, "alias", auto_uida, auto_gidq, 02755); + + d(auto_qmail, "queue", auto_uidq, auto_gidq, 0750); + d(auto_qmail, "queue/pid", auto_uidq, auto_gidq, 0700); + d(auto_qmail, "queue/bounce", auto_uids, auto_gidq, 0700); + + dsplit("queue/dkim", auto_uidq, 0750); + dsplit("queue/mess", auto_uidq, 0750); + dsplit("queue/todo", auto_uidq, 0750); + dsplit("queue/intd", auto_uidq, 0700); + dsplit("queue/info", auto_uids, 0700); + dsplit("queue/local", auto_uids, 0700); + dsplit("queue/remote", auto_uids, 0700); + + d(auto_qmail, "queue/lock", auto_uidq, auto_gidq, 0750); + z(auto_qmail, "queue/lock/tcpto", TCPTO_BUFSIZ, auto_uidr, auto_gidq, 0644); + z(auto_qmail, "queue/lock/sendmutex", 0, auto_uids, auto_gidq, 0600); + p(auto_qmail, "queue/lock/trigger", auto_uids, auto_gidq, 0622); + + c(auto_qmail, "bin", "qmail-queue", auto_uidq, auto_gidq, 04711); + c(auto_qmail, "bin", "qmail-qmaint", auto_uidq, auto_gidq, 0711); + c(auto_qmail, "bin", "qmail-lspawn", auto_uido, auto_gidq, 0700); + c(auto_qmail, "bin", "qmail-start", auto_uido, auto_gidq, 0700); + c(auto_qmail, "bin", "qmail-getpw", auto_uido, auto_gidq, 0711); + c(auto_qmail, "bin", "qmail-local", auto_uido, auto_gidq, 0711); + c(auto_qmail, "bin", "qmail-remote", auto_uido, auto_gidq, 0711); + c(auto_qmail, "bin", "qmail-smtpam", auto_uido, auto_gidq, 0711); + c(auto_qmail, "bin", "qmail-rspawn", auto_uido, auto_gidq, 0711); + c(auto_qmail, "bin", "qmail-clean", auto_uido, auto_gidq, 0711); + c(auto_qmail, "bin", "qmail-send", auto_uido, auto_gidq, 0711); + c(auto_qmail, "bin", "qmail-todo", auto_uido, auto_gidq, 0711); + c(auto_qmail, "bin", "splogger", auto_uido, auto_gidq, 0711); + c(auto_qmail, "bin", "qmail-newu", auto_uido, auto_gidq, 0700); + c(auto_qmail, "bin", "qmail-newmrh", auto_uido, auto_gidq, 0700); + + c(auto_qmail, "bin", "qmail-dkim", auto_uidq, auto_gidq, 0711); + c(auto_qmail, "bin", "qmail-dksign", auto_uidq, auto_gidq, 04711); + c(auto_qmail, "bin", "qmail-dkverify", auto_uidq, auto_gidq, 04711); + + c(auto_qmail, "bin", "qmail-authuser", auto_uido, auto_gidq, 06711); + c(auto_qmail, "bin", "qmail-vmailuser", auto_uido, auto_gidq, 06711); + c(auto_qmail, "bin", "qmail-postgrey", auto_uido, auto_gidq, 06711); + c(auto_qmail, "bin", "qmail-badloadertypes", auto_uido, auto_gidq, 0711); + c(auto_qmail, "bin", "qmail-badmimetypes", auto_uido, auto_gidq, 0711); + c(auto_qmail, "bin", "qmail-recipients", auto_uido, auto_gidq, 0711); + c(auto_qmail, "bin", "qmail-mfrules", auto_uido, auto_gidq, 0711); + c(auto_qmail, "bin", "qmail-mrtg", auto_uido, auto_gidq, 0755); + c(auto_qmail, "bin", "qmail-mrtg-queue", auto_uido, auto_gidq, 0755); + + c(auto_qmail, "bin", "qmail-pw2u", auto_uido, auto_gidq, 0711); + c(auto_qmail, "bin", "qmail-inject", auto_uido, auto_gidq, 0755); + c(auto_qmail, "bin", "qmail-showctl", auto_uido, auto_gidq, 0755); + c(auto_qmail, "bin", "qmail-qread", auto_uido, auto_gidq, 0755); + c(auto_qmail, "bin", "qmail-qstat", auto_uido, auto_gidq, 0755); + c(auto_qmail, "bin", "qmail-tcpto", auto_uido, auto_gidq, 0755); + c(auto_qmail, "bin", "qmail-tcpok", auto_uido, auto_gidq, 0755); + c(auto_qmail, "bin", "qmail-pop3d", auto_uido, auto_gidq, 0755); + c(auto_qmail, "bin", "qmail-popup", auto_uido, auto_gidq, 0711); + c(auto_qmail, "bin", "qmail-qmqpc", auto_uido, auto_gidq, 0755); + c(auto_qmail, "bin", "qmail-qmqpd", auto_uido, auto_gidq, 0755); + c(auto_qmail, "bin", "qmail-qmtpd", auto_uido, auto_gidq, 0755); + c(auto_qmail, "bin", "qmail-smtpd", auto_uido, auto_gidq, 0755); + + c(auto_qmail, "bin", "predate", auto_uido, auto_gidq, 0755); + c(auto_qmail, "bin", "datemail", auto_uido, auto_gidq, 0755); + c(auto_qmail, "bin", "mailsubj", auto_uido, auto_gidq, 0755); + c(auto_qmail, "bin", "sendmail", auto_uido, auto_gidq, 0755); + c(auto_qmail, "bin", "qreceipt", auto_uido, auto_gidq, 0755); + c(auto_qmail, "bin", "qbiff", auto_uido, auto_gidq, 0755); + c(auto_qmail, "bin", "forward", auto_uido, auto_gidq, 0755); + c(auto_qmail, "bin", "preline", auto_uido, auto_gidq, 0755); + c(auto_qmail, "bin", "condredirect", auto_uido, auto_gidq, 0755); + c(auto_qmail, "bin", "bouncesaying", auto_uido, auto_gidq, 0755); + c(auto_qmail, "bin", "except", auto_uido, auto_gidq, 0755); + c(auto_qmail, "bin", "maildirmake", auto_uido, auto_gidq, 0755); + c(auto_qmail, "bin", "maildir2mbox", auto_uido, auto_gidq, 0755); + c(auto_qmail, "bin", "maildirwatch", auto_uido, auto_gidq, 0755); + + c(auto_qmail, "bin", "fastforward", auto_uido, auto_gidq, 0755); + c(auto_qmail, "bin", "printforward", auto_uido, auto_gidq, 0755); + c(auto_qmail, "bin", "setforward", auto_uido, auto_gidq, 0755); + c(auto_qmail, "bin", "newaliases", auto_uido, auto_gidq, 0755); + c(auto_qmail, "bin", "printmaillist", auto_uido, auto_gidq, 0755); + c(auto_qmail, "bin", "setmaillist", auto_uido, auto_gidq, 0755); + c(auto_qmail, "bin", "newinclude", auto_uido, auto_gidq, 0755); + c(auto_qmail, "bin", "srsforward", auto_uido, auto_gidq, 0755); + c(auto_qmail, "bin", "srsreverse", auto_uido, auto_gidq, 0755); + + c(auto_qmail, "bin", "ipmeprint", auto_uido, auto_gidq, 0755); + c(auto_qmail, "bin", "spfquery", auto_uido, auto_gidq, 0755); + c(auto_qmail, "bin", "dnscname", auto_uido, auto_gidq, 0755); + c(auto_qmail, "bin", "dnsfq", auto_uido, auto_gidq, 0755); + c(auto_qmail, "bin", "dnsip", auto_uido, auto_gidq, 0755); + c(auto_qmail, "bin", "dnsmxip", auto_uido, auto_gidq, 0755); + c(auto_qmail, "bin", "dnsptr", auto_uido, auto_gidq, 0755); + c(auto_qmail, "bin", "dnstlsa", auto_uido, auto_gidq, 0755); + c(auto_qmail, "bin", "dnstxt", auto_uido, auto_gidq, 0755); + c(auto_qmail, "bin", "hostname", auto_uido, auto_gidq, 0755); + + c(auto_qmail, "bin", "columnt", auto_uido, auto_gidq, 0755); + c(auto_qmail, "bin", "ddist", auto_uido, auto_gidq, 0755); + c(auto_qmail, "bin", "deferrals", auto_uido, auto_gidq, 0755); + c(auto_qmail, "bin", "failures", auto_uido, auto_gidq, 0755); + c(auto_qmail, "bin", "matchup", auto_uido, auto_gidq, 0755); + c(auto_qmail, "bin", "recipients", auto_uido, auto_gidq, 0755); + c(auto_qmail, "bin", "rhosts", auto_uido, auto_gidq, 0755); + c(auto_qmail, "bin", "rxdelay", auto_uido, auto_gidq, 0755); + c(auto_qmail, "bin", "senders", auto_uido, auto_gidq, 0755); + c(auto_qmail, "bin", "successes", auto_uido, auto_gidq, 0755); + c(auto_qmail, "bin", "suids", auto_uido, auto_gidq, 0755); + c(auto_qmail, "bin", "tai64nfrac", auto_uido, auto_gidq, 0755); + c(auto_qmail, "bin", "xqp", auto_uido, auto_gidq, 0755); + c(auto_qmail, "bin", "xrecipient", auto_uido, auto_gidq, 0755); + c(auto_qmail, "bin", "xsender", auto_uido, auto_gidq, 0755); + c(auto_qmail, "bin", "zddist", auto_uido, auto_gidq, 0755); + c(auto_qmail, "bin", "zdeferrals", auto_uido, auto_gidq, 0755); + c(auto_qmail, "bin", "zfailures", auto_uido, auto_gidq, 0755); + c(auto_qmail, "bin", "zfailures", auto_uido, auto_gidq, 0755); + c(auto_qmail, "bin", "zoverall", auto_uido, auto_gidq, 0755); + c(auto_qmail, "bin", "zrecipients", auto_uido, auto_gidq, 0755); + c(auto_qmail, "bin", "zrhosts", auto_uido, auto_gidq, 0755); + c(auto_qmail, "bin", "zrxdelay", auto_uido, auto_gidq, 0755); + c(auto_qmail, "bin", "zsenders", auto_uido, auto_gidq, 0755); + c(auto_qmail, "bin", "zsendmail", auto_uido, auto_gidq, 0755); + c(auto_qmail, "bin", "zsuccesses", auto_uido, auto_gidq, 0755); + c(auto_qmail, "bin", "zsuids", auto_uido, auto_gidq, 0755); } diff --git a/src/hmac_md5.c b/src/hmac_md5.c index 310f0ef..5c46653 100644 --- a/src/hmac_md5.c +++ b/src/hmac_md5.c @@ -1,7 +1,8 @@ +#include "byte.h" +#include "str.h" + #include "global.h" #include "md5.h" -#include "str.h" -#include "byte.h" /** @file hmac_md5 @@ -13,40 +14,40 @@ output: unsigned char *digest: pointer to calculated digest */ -void hmac_md5(unsigned char *text,int text_len,unsigned char * key,int key_len,unsigned char *digest) +void hmac_md5(unsigned char *text, int text_len, unsigned char *key, int key_len, unsigned char *digest) { - MD5_CTX context; - unsigned char k_ipad[65]; /* inner padding - key XORd with ipad */ - unsigned char k_opad[65]; /* outer padding - key XORd with opad */ - unsigned char tk[16]; - int i; - - if (key_len > 64) { - MD5_CTX tctx; - MD5Init(&tctx); - MD5Update(&tctx,key,key_len); - MD5Final(tk,&tctx); - key = tk; - key_len = 16; - } - - byte_zero(k_ipad,sizeof(k_ipad)); - byte_zero(k_opad,sizeof(k_opad)); - byte_copy(k_ipad,key_len,key); - byte_copy(k_opad,key_len,key); - - for (i = 0; i < 64; i++) { - k_ipad[i] ^= 0x36; - k_opad[i] ^= 0x5c; - } - - MD5Init(&context); /* init context for 1st pass */ - MD5Update(&context,k_ipad,64); /* start with inner pad */ - MD5Update(&context,text,text_len); /* then text of datagram */ - MD5Final(digest,&context); /* finish up 1st pass */ - - MD5Init(&context); /* init context for 2nd pass */ - MD5Update(&context,k_opad,64); /* start with outer pad */ - MD5Update(&context,digest,16); /* then results of 1st hash */ - MD5Final(digest,&context); /* finish up 2nd pass */ + MD5_CTX context; + unsigned char k_ipad[65]; /* inner padding - key XORd with ipad */ + unsigned char k_opad[65]; /* outer padding - key XORd with opad */ + unsigned char tk[16]; + int i; + + if (key_len > 64) { + MD5_CTX tctx; + MD5Init(&tctx); + MD5Update(&tctx, key, key_len); + MD5Final(tk, &tctx); + key = tk; + key_len = 16; + } + + byte_zero(k_ipad, sizeof(k_ipad)); + byte_zero(k_opad, sizeof(k_opad)); + byte_copy(k_ipad, key_len, key); + byte_copy(k_opad, key_len, key); + + for (i = 0; i < 64; i++) { + k_ipad[i] ^= 0x36; + k_opad[i] ^= 0x5c; + } + + MD5Init(&context); /* init context for 1st pass */ + MD5Update(&context, k_ipad, 64); /* start with inner pad */ + MD5Update(&context, text, text_len); /* then text of datagram */ + MD5Final(digest, &context); /* finish up 1st pass */ + + MD5Init(&context); /* init context for 2nd pass */ + MD5Update(&context, k_opad, 64); /* start with outer pad */ + MD5Update(&context, digest, 16); /* then results of 1st hash */ + MD5Final(digest, &context); /* finish up 2nd pass */ } diff --git a/src/hostname.c b/src/hostname.c index 6a55309..2794102 100644 --- a/src/hostname.c +++ b/src/hostname.c @@ -1,4 +1,5 @@ #include <unistd.h> + #include "buffer.h" #include "exit.h" @@ -7,10 +8,10 @@ char host[256]; int main() { host[0] = 0; /* sigh */ - gethostname(host,sizeof(host)); + gethostname(host, sizeof(host)); host[sizeof(host) - 1] = 0; - buffer_puts(buffer_1small,host); - buffer_puts(buffer_1small,"\n"); + buffer_puts(buffer_1small, host); + buffer_puts(buffer_1small, "\n"); buffer_flush(buffer_1small); _exit(0); } diff --git a/src/include/base64.h b/src/include/base64.h index 9384411..88c8543 100644 --- a/src/include/base64.h +++ b/src/include/base64.h @@ -3,7 +3,7 @@ #include "stralloc.h" -extern int b64decode(const unsigned char *,int,stralloc *); -extern int b64encode(stralloc *,stralloc *); +extern int b64decode(const unsigned char *, int, stralloc *); +extern int b64encode(stralloc *, stralloc *); #endif diff --git a/src/include/commands.h b/src/include/commands.h index 3fd2cb8..fed35e7 100644 --- a/src/include/commands.h +++ b/src/include/commands.h @@ -5,7 +5,7 @@ struct commands { char *text; void (*fun)(); void (*flush)(); -} ; +}; int commands(); diff --git a/src/include/constmap.h b/src/include/constmap.h index 750702e..0a43e98 100644 --- a/src/include/constmap.h +++ b/src/include/constmap.h @@ -11,10 +11,10 @@ struct constmap { int *next; char **input; int *inputlen; -} ; +}; -int constmap_init(struct constmap *,char *,int,int); -int constmap_init_char(struct constmap *,char *,int,int,char); +int constmap_init(struct constmap *, char *, int, int); +int constmap_init_char(struct constmap *, char *, int, int, char); void constmap_free(); char *constmap(); diff --git a/src/include/control.h b/src/include/control.h index 732042c..fdd72b3 100644 --- a/src/include/control.h +++ b/src/include/control.h @@ -4,9 +4,9 @@ #include "stralloc.h" int control_init(void); -int control_readline(stralloc *,char *); -int control_rldef(stralloc *,char *,int,char *); -int control_readint(int *,char *); -int control_readfile(stralloc *,char *,int); +int control_readline(stralloc *, char *); +int control_rldef(stralloc *, char *, int, char *); +int control_readint(int *, char *); +int control_readfile(stralloc *, char *, int); #endif diff --git a/src/include/date822fmt.h b/src/include/date822fmt.h index a2f1432..fc721a0 100644 --- a/src/include/date822fmt.h +++ b/src/include/date822fmt.h @@ -1,7 +1,7 @@ #ifndef DATE822FMT_H #define DATE822FMT_H -unsigned int date822fmt(char *,struct datetime *); +unsigned int date822fmt(char *, struct datetime *); #define DATE822FMT 60 #endif diff --git a/src/include/datetime.h b/src/include/datetime.h index 68d1618..eb8abc6 100644 --- a/src/include/datetime.h +++ b/src/include/datetime.h @@ -10,7 +10,7 @@ struct datetime { int yday; int mon; int year; -} ; +}; typedef long datetime_sec; diff --git a/src/include/dkim.h b/src/include/dkim.h index 508b2df..dd2a70d 100644 --- a/src/include/dkim.h +++ b/src/include/dkim.h @@ -20,110 +20,108 @@ * *****************************************************************************/ #define DKIM_CALL -#define MAKELONG(a,b) ((long)(((unsigned)(a) & 0xffff) | (((unsigned)(b) & 0xffff) << 16))) +#define MAKELONG(a, b) ((long)(((unsigned)(a) & 0xffff) | (((unsigned)(b) & 0xffff) << 16))) #ifdef __cplusplus extern "C" { #endif // DKIM hash algorithms -#define DKIM_HASH_SHA1 1 -#define DKIM_HASH_SHA256 2 -#define DKIM_HASH_SHA1_AND_SHA256 3 -#define DKIM_HASH_ED25519 4 -#define DKIM_HASH_RSA256_AND_ED25519 5 +#define DKIM_HASH_SHA1 1 +#define DKIM_HASH_SHA256 2 +#define DKIM_HASH_SHA1_AND_SHA256 3 +#define DKIM_HASH_ED25519 4 +#define DKIM_HASH_RSA256_AND_ED25519 5 // DKIM canonicalization methods -#define DKIM_CANON_SIMPLE 1 -#define DKIM_CANON_NOWSP 2 -#define DKIM_CANON_RELAXED 3 +#define DKIM_CANON_SIMPLE 1 +#define DKIM_CANON_NOWSP 2 +#define DKIM_CANON_RELAXED 3 -#define DKIM_SIGN_SIMPLE MAKELONG(DKIM_CANON_SIMPLE,DKIM_CANON_SIMPLE) -#define DKIM_SIGN_SIMPLE_RELAXED MAKELONG(DKIM_CANON_RELAXED,DKIM_CANON_SIMPLE) -#define DKIM_SIGN_RELAXED MAKELONG(DKIM_CANON_RELAXED,DKIM_CANON_RELAXED) -#define DKIM_SIGN_RELAXED_SIMPLE MAKELONG(DKIM_CANON_SIMPLE,DKIM_CANON_RELAXED) +#define DKIM_SIGN_SIMPLE MAKELONG(DKIM_CANON_SIMPLE, DKIM_CANON_SIMPLE) +#define DKIM_SIGN_SIMPLE_RELAXED MAKELONG(DKIM_CANON_RELAXED, DKIM_CANON_SIMPLE) +#define DKIM_SIGN_RELAXED MAKELONG(DKIM_CANON_RELAXED, DKIM_CANON_RELAXED) +#define DKIM_SIGN_RELAXED_SIMPLE MAKELONG(DKIM_CANON_SIMPLE, DKIM_CANON_RELAXED) // DKIM Error codes -#define DKIM_SUCCESS 0 // operation successful -#define DKIM_FAIL -1 // verify error: message is suspicious -#define DKIM_BAD_SYNTAX -2 // signature error: DKIM-Signature could not parse or has bad tags/values -#define DKIM_SIGNATURE_BAD -3 // signature error: RSA/ED25519 verify failed -#define DKIM_SIGNATURE_BAD_BUT_TESTING -4 // signature error: RSA/ED25519 verify failed but testing -#define DKIM_SIGNATURE_EXPIRED -5 // signature error: x= is old -#define DKIM_SELECTOR_INVALID -6 // signature error: selector doesn't parse or contains invalid values -#define DKIM_SELECTOR_GRANULARITY_MISMATCH -7 // signature error: selector g= doesn't match i= -#define DKIM_SELECTOR_KEY_REVOKED -8 // signature error: selector p= empty -#define DKIM_SELECTOR_DOMAIN_NAME_TOO_LONG -9 // signature error: selector domain name too long to request -#define DKIM_SELECTOR_DNS_TEMP_FAILURE -10 // signature error: temporary dns failure requesting selector -#define DKIM_SELECTOR_DNS_PERM_FAILURE -11 // signature error: permanent dns failure requesting selector -#define DKIM_SELECTOR_PUBLIC_KEY_INVALID -12 // signature error: selector p= value invalid or wrong format -#define DKIM_NO_SIGNATURES -13 // process error, no sigs -#define DKIM_NO_VALID_SIGNATURES -14 // process error, no valid sigs -#define DKIM_BODY_HASH_MISMATCH -15 // sigature verify error: message body does not hash to bh value -#define DKIM_SELECTOR_ALGORITHM_MISMATCH -16 // signature error: selector h= doesn't match signature a= -#define DKIM_STAT_INCOMPAT -17 // signature error: incompatible v= -#define DKIM_UNSIGNED_FROM -18 // signature error: not all message's From headers in signature -#define DKIM_OUT_OF_MEMORY -20 // memory allocation failed -#define DKIM_INVALID_CONTEXT -21 // DKIMContext structure invalid for this operation -#define DKIM_NO_SENDER -22 // signing error: Could not find From: or Sender: header in message -#define DKIM_BAD_PRIVATE_KEY -23 // signing error: Could not parse private key -#define DKIM_BUFFER_TOO_SMALL -24 // signing error: Buffer passed in is not large enough -#define DKIM_MAX_ERROR -25 // set this to 1 greater than the highest error code (but negative) +#define DKIM_SUCCESS 0 // operation successful +#define DKIM_FAIL -1 // verify error: message is suspicious +#define DKIM_BAD_SYNTAX -2 // signature error: DKIM-Signature could not parse or has bad tags/values +#define DKIM_SIGNATURE_BAD -3 // signature error: RSA/ED25519 verify failed +#define DKIM_SIGNATURE_BAD_BUT_TESTING -4 // signature error: RSA/ED25519 verify failed but testing +#define DKIM_SIGNATURE_EXPIRED -5 // signature error: x= is old +#define DKIM_SELECTOR_INVALID -6 // signature error: selector doesn't parse or contains invalid values +#define DKIM_SELECTOR_GRANULARITY_MISMATCH -7 // signature error: selector g= doesn't match i= +#define DKIM_SELECTOR_KEY_REVOKED -8 // signature error: selector p= empty +#define DKIM_SELECTOR_DOMAIN_NAME_TOO_LONG \ + -9 // signature error: selector domain name too long to request +#define DKIM_SELECTOR_DNS_TEMP_FAILURE -10 // signature error: temporary dns failure requesting selector +#define DKIM_SELECTOR_DNS_PERM_FAILURE -11 // signature error: permanent dns failure requesting selector +#define DKIM_SELECTOR_PUBLIC_KEY_INVALID \ + -12 // signature error: selector p= value invalid or wrong format +#define DKIM_NO_SIGNATURES -13 // process error, no sigs +#define DKIM_NO_VALID_SIGNATURES -14 // process error, no valid sigs +#define DKIM_BODY_HASH_MISMATCH -15 // sigature verify error: message body does not hash to bh value +#define DKIM_SELECTOR_ALGORITHM_MISMATCH -16 // signature error: selector h= doesn't match signature a= +#define DKIM_STAT_INCOMPAT -17 // signature error: incompatible v= +#define DKIM_UNSIGNED_FROM -18 // signature error: not all message's From headers in signature +#define DKIM_OUT_OF_MEMORY -20 // memory allocation failed +#define DKIM_INVALID_CONTEXT -21 // DKIMContext structure invalid for this operation +#define DKIM_NO_SENDER -22 // signing error: Could not find From: or Sender: header in message +#define DKIM_BAD_PRIVATE_KEY -23 // signing error: Could not parse private key +#define DKIM_BUFFER_TOO_SMALL -24 // signing error: Buffer passed in is not large enough +#define DKIM_MAX_ERROR -25 // set this to 1 greater than the highest error code (but negative) // DKIM_SUCCESS // verify result: all signatures verified - // signature result: signature verified -#define DKIM_FINISHED_BODY 1 // process result: no more message body is needed -#define DKIM_PARTIAL_SUCCESS 2 // verify result: at least one but not all signatures verified -#define DKIM_NEUTRAL 3 // verify result: no signatures verified but message is not suspicous -#define DKIM_SUCCESS_BUT_EXTRA 4 // signature result: signature verified but it did not include all of the body - +// signature result: signature verified +#define DKIM_FINISHED_BODY 1 // process result: no more message body is needed +#define DKIM_PARTIAL_SUCCESS 2 // verify result: at least one but not all signatures verified +#define DKIM_NEUTRAL 3 // verify result: no signatures verified but message is not suspicous +#define DKIM_SUCCESS_BUT_EXTRA \ + 4 // signature result: signature verified but it did not include all of the body // This function is called once for each header in the message // return 1 to include this header in the signature and 0 to exclude. -typedef int (DKIM_CALL *DKIMHEADERCALLBACK)(const char* szHeader); +typedef int(DKIM_CALL *DKIMHEADERCALLBACK)(const char *szHeader); // This function is called to retrieve a TXT record from DNS -typedef int (DKIM_CALL *DKIMDNSCALLBACK)(const char* szFQDN,char* szBuffer,int nBufLen); +typedef int(DKIM_CALL *DKIMDNSCALLBACK)(const char *szFQDN, char *szBuffer, int nBufLen); -typedef struct DKIMContext_t -{ +typedef struct DKIMContext_t { unsigned int reserved1; unsigned int reserved2; - void* reserved3; + void *reserved3; } DKIMContext; -typedef struct DKIMSignOptions_t -{ - int nCanon; // canonization - int nIncludeBodyLengthTag; // 0 = don't include l= tag, 1 = include l= tag - int nIncludeTimeStamp; // 0 = don't include t= tag, 1 = include t= tag - int nIncludeQueryMethod; // 0 = don't include q= tag, 1 = include q= tag - char szSelector[64]; // selector - required - char szSelectorE[64]; // 2nd selector - optional - char szDomain[256]; // domain - optional - if empty, domain is computed from sender - char szIdentity[256]; // for i= tag, if empty tag will not be included in sig - unsigned long expireTime; // for x= tag, if 0 tag will not be included in sig - DKIMHEADERCALLBACK pfnHeaderCallback; // header callback - char szRequiredHeaders[256]; // colon-separated list of headers that must be signed - int nHash; // use one of the DKIM_HASH_xx constants here - // even if not present in the message - int nIncludeCopiedHeaders; // 0 = don't include z= tag, 1 = include z= tag +typedef struct DKIMSignOptions_t { + int nCanon; // canonization + int nIncludeBodyLengthTag; // 0 = don't include l= tag, 1 = include l= tag + int nIncludeTimeStamp; // 0 = don't include t= tag, 1 = include t= tag + int nIncludeQueryMethod; // 0 = don't include q= tag, 1 = include q= tag + char szSelector[64]; // selector - required + char szSelectorE[64]; // 2nd selector - optional + char szDomain[256]; // domain - optional - if empty, domain is computed from sender + char szIdentity[256]; // for i= tag, if empty tag will not be included in sig + unsigned long expireTime; // for x= tag, if 0 tag will not be included in sig + DKIMHEADERCALLBACK pfnHeaderCallback; // header callback + char szRequiredHeaders[256]; // colon-separated list of headers that must be signed + int nHash; // use one of the DKIM_HASH_xx constants here + // even if not present in the message + int nIncludeCopiedHeaders; // 0 = don't include z= tag, 1 = include z= tag } DKIMSignOptions; -typedef struct DKIMVerifyOptions_t -{ - DKIMDNSCALLBACK pfnSelectorCallback; // selector record callback - DKIMDNSCALLBACK pfnPracticesCallback; // ADSP record callback - int nHonorBodyLengthTag; // 0 = ignore l= tag, 1 = use l= tag to limit the amount of body verified - int nCheckPractices; // 0 = use default (unknown) practices, 1 = request and use author domain signing practices - int nSubjectRequired; // 0 = subject is required to be signed, 1 = not required - int nSaveCanonicalizedData; // 0 = canonicalized data is not saved, 1 = canonicalized data is saved - int nAllowUnsignedFromHeaders; // 0 = From headers not included in the signature are not allowed, 1 = allowed +typedef struct DKIMVerifyOptions_t { + DKIMDNSCALLBACK pfnSelectorCallback; // selector record callback + DKIMDNSCALLBACK pfnPracticesCallback; // ADSP record callback + int nHonorBodyLengthTag; // 0 = ignore l= tag, 1 = use l= tag to limit the amount of body verified + int nCheckPractices; // 0 = use default (unknown) practices, 1 = request and use author domain signing practices + int nSubjectRequired; // 0 = subject is required to be signed, 1 = not required + int nSaveCanonicalizedData; // 0 = canonicalized data is not saved, 1 = canonicalized data is saved + int nAllowUnsignedFromHeaders; // 0 = From headers not included in the signature are not allowed, 1 = allowed } DKIMVerifyOptions; -typedef struct DKIMVerifyDetails_t -{ +typedef struct DKIMVerifyDetails_t { char *szSignature; char *szSignatureDomain; char *szIdentityDomain; @@ -131,22 +129,24 @@ typedef struct DKIMVerifyDetails_t int nResult; } DKIMVerifyDetails; -int DKIM_CALL DKIMSignInit(DKIMContext* pSignContext,DKIMSignOptions* pOptions); -int DKIM_CALL DKIMSignProcess(DKIMContext* pSignContext,char* szBuffer,int nBufLength); -int DKIM_CALL DKIMSignGetSig2(DKIMContext* pSignContext,char* szRSAPrivKey,char *szECCPrivKey,char** pszSignature); -void DKIM_CALL DKIMSignFree(DKIMContext* pSignContext); +int DKIM_CALL DKIMSignInit(DKIMContext *pSignContext, DKIMSignOptions *pOptions); +int DKIM_CALL DKIMSignProcess(DKIMContext *pSignContext, char *szBuffer, int nBufLength); +int DKIM_CALL DKIMSignGetSig2( + DKIMContext *pSignContext, char *szRSAPrivKey, char *szECCPrivKey, char **pszSignature); +void DKIM_CALL DKIMSignFree(DKIMContext *pSignContext); -int DKIM_CALL DKIMVerifyInit(DKIMContext* pVerifyContext,DKIMVerifyOptions* pOptions); -int DKIM_CALL DKIMVerifyProcess(DKIMContext* pVerifyContext,const char* szBuffer,int nBufLength); -int DKIM_CALL DKIMVerifyResults(DKIMContext* pVerifyContext); -int DKIM_CALL DKIMVerifyGetDetails(DKIMContext* pVerifyContext,int* nSigCount,DKIMVerifyDetails** pDetails,char* szPractices); -void DKIM_CALL DKIMVerifyFree(DKIMContext* pVerifyContext); +int DKIM_CALL DKIMVerifyInit(DKIMContext *pVerifyContext, DKIMVerifyOptions *pOptions); +int DKIM_CALL DKIMVerifyProcess(DKIMContext *pVerifyContext, const char *szBuffer, int nBufLength); +int DKIM_CALL DKIMVerifyResults(DKIMContext *pVerifyContext); +int DKIM_CALL DKIMVerifyGetDetails( + DKIMContext *pVerifyContext, int *nSigCount, DKIMVerifyDetails **pDetails, char *szPractices); +void DKIM_CALL DKIMVerifyFree(DKIMContext *pVerifyContext); // const char *DKIM_CALL DKIMVersion(); const char *DKIM_CALL DKIMGetErrorString(int ErrorCode); -int _DKIM_ReportResult(char const *,char const *,char const *); +int _DKIM_ReportResult(const char *, const char *, const char *); const char *DKIM_ErrorResult(const int); #ifdef __cplusplus diff --git a/src/include/dkimbase.h b/src/include/dkimbase.h index 25aac02..bfecb5d 100644 --- a/src/include/dkimbase.h +++ b/src/include/dkimbase.h @@ -22,58 +22,56 @@ #ifndef DKIMBASE_H #define DKIMBASE_H +#include <openssl/err.h> #include <openssl/evp.h> #include <openssl/pem.h> -#include <openssl/err.h> #define BUFFER_ALLOC_INCREMENT 256 -#include <string> #include <list> +#include <string> using namespace std; -class CDKIMBase -{ +class CDKIMBase { public: + CDKIMBase(); + ~CDKIMBase(); - CDKIMBase(); - ~CDKIMBase(); - - int Init(void); + int Init(void); - int Process(const char* szBuffer,int nBufLength,bool bEOF); - int ProcessFinal(void); + int Process(const char *szBuffer, int nBufLength, bool bEOF); + int ProcessFinal(void); - int Alloc(char*& szBuffer,int nRequiredSize); - int ReAlloc(char*& szBuffer,int& nBufferLength,int nRequiredSize); - void Free(char* szBuffer); + int Alloc(char *& szBuffer, int nRequiredSize); + int ReAlloc(char *& szBuffer, int& nBufferLength, int nRequiredSize); + void Free(char *szBuffer); - static void RemoveSWSP(char* szBuffer); - static void RemoveSWSP(char* pBuffer,int& nBufLength); - static void RemoveSWSP(string& sBuffer); + static void RemoveSWSP(char *szBuffer); + static void RemoveSWSP(char *pBuffer, int& nBufLength); + static void RemoveSWSP(string& sBuffer); - static void CompressSWSP(char* pBuffer,int& nBufLength); - static void CompressSWSP(string& sBuffer); + static void CompressSWSP(char *pBuffer, int& nBufLength); + static void CompressSWSP(string& sBuffer); - static string RelaxHeader(const string& sHeader); + static string RelaxHeader(const string& sHeader); - virtual int ProcessHeaders(void); - virtual int ProcessBody(char* szBuffer,int nBufLength,bool bEOF); + virtual int ProcessHeaders(void); + virtual int ProcessBody(char *szBuffer, int nBufLength, bool bEOF); protected: - char* m_From; - char* m_Sender; - char* m_hTag; - int m_hTagSize; - int m_hTagPos; - char* m_Line; - int m_LineSize; - int m_LinePos; - bool m_InHeaders; - - list<string> HeaderList; + char *m_From; + char *m_Sender; + char *m_hTag; + int m_hTagSize; + int m_hTagPos; + char *m_Line; + int m_LineSize; + int m_LinePos; + bool m_InHeaders; + + list<string> HeaderList; }; -#endif // DKIMBASE_H +#endif // DKIMBASE_H diff --git a/src/include/dkimsign.h b/src/include/dkimsign.h index 150a0b8..af1dd70 100644 --- a/src/include/dkimsign.h +++ b/src/include/dkimsign.h @@ -24,58 +24,60 @@ #include "dkimbase.h" -class CDKIMSign : public CDKIMBase -{ +class CDKIMSign : public CDKIMBase { public: CDKIMSign(); ~CDKIMSign(); //int Init() = delete; - int Init(DKIMSignOptions* pOptions); - int GetSig2(char* szRSAPrivKey,char* szECCPrivKey,char** pszSignature); + int Init(DKIMSignOptions *pOptions); + int GetSig2(char *szRSAPrivKey, char *szECCPrivKey, char **pszSignature); virtual int ProcessHeaders(void) override; - virtual int ProcessBody(char* szBuffer,int nBufLength,bool bEOF) override; + virtual int ProcessBody(char *szBuffer, int nBufLength, bool bEOF) override; - enum CKDKIMConstants { OptimalHeaderLineLength = 65 }; + enum CKDKIMConstants { + OptimalHeaderLineLength = 65 + }; - void Hash(const char* szBuffer,int nBufLength,bool bHdr); + void Hash(const char *szBuffer, int nBufLength, bool bHdr); protected: - bool SignThisTag(const string& sTag); void GetHeaderParams(const string& sHdr); void ProcessHeader(const string& sHdr); bool ParseFromAddress(void); void InitSig(void); - void AddTagToSig(const char* const Tag,const string &sValue,char cbrk,bool bFold); - void AddTagToSig(const char* const Tag,unsigned long nValue); + void AddTagToSig(const char *const Tag, const string& sValue, char cbrk, bool bFold); + void AddTagToSig(const char *const Tag, unsigned long nValue); void AddInterTagSpace(int nSizeOfNextTag); - void AddFoldedValueToSig(const string &sValue,char cbrk); + void AddFoldedValueToSig(const string& sValue, char cbrk); bool IsRequiredHeader(const string& sTag); - int ConstructSignature(char* szSignKey,int nSigAlg); + int ConstructSignature(char *szSignKey, int nSigAlg); - int AssembleReturnedSig(char* szRSAPrivKey,char* szECCPrivKey); + int AssembleReturnedSig(char *szRSAPrivKey, char *szECCPrivKey); -#if ((OPENSSL_VERSION_NUMBER < 0x10100000L) || (LIBRESSL_VERSION_NUMBER > 0 && LIBRESSL_VERSION_NUMBER < 0x20700000L)) - EVP_MD_CTX m_Hdr_sha1ctx; /* the RSA SHA1 signature */ - EVP_MD_CTX m_Hdr_sha256ctx; /* the RSA SHA256 signature */ +#if ( \ + (OPENSSL_VERSION_NUMBER < 0x10100000L) \ + || (LIBRESSL_VERSION_NUMBER > 0 && LIBRESSL_VERSION_NUMBER < 0x20700000L)) + EVP_MD_CTX m_Hdr_sha1ctx; /* the RSA SHA1 signature */ + EVP_MD_CTX m_Hdr_sha256ctx; /* the RSA SHA256 signature */ - EVP_MD_CTX m_Bdy_sha1ctx; /* the SHA1 digest */ - EVP_MD_CTX m_Bdy_sha256ctx; /* the SHA256 digest */ + EVP_MD_CTX m_Bdy_sha1ctx; /* the SHA1 digest */ + EVP_MD_CTX m_Bdy_sha256ctx; /* the SHA256 digest */ #else EVP_MD_CTX *m_Hdr_sha1ctx; /* the RSA SHA1 signature */ EVP_MD_CTX *m_Hdr_sha256ctx; /* the RSA SHA256 signature */ EVP_MD_CTX *m_Hdr_ed25519ctx; /* the PureEd25519 signature */ - EVP_MD_CTX *m_Bdy_sha1ctx; /* the SHA1 digest */ - EVP_MD_CTX *m_Bdy_sha256ctx; /* the SHA256 digest for RSA */ - EVP_MD_CTX *m_Edy_sha256ctx; /* the SHA256 digest for Ed25519 */ + EVP_MD_CTX *m_Bdy_sha1ctx; /* the SHA1 digest */ + EVP_MD_CTX *m_Bdy_sha256ctx; /* the SHA256 digest for RSA */ + EVP_MD_CTX *m_Edy_sha256ctx; /* the SHA256 digest for Ed25519 */ #endif - int m_Canon; /* canonization method */ + int m_Canon; /* canonization method */ int m_EmptyLineCount; @@ -83,9 +85,9 @@ protected: string sFrom; string sSender; string sSelector; - string eSelector; /* Used for Ed25519 signatures */ + string eSelector; /* Used for Ed25519 signatures */ string sDomain; - string sIdentity; /* for i= tag, if empty tag will not be included in sig */ + string sIdentity; /* for i= tag, if empty tag will not be included in sig */ string sRequiredHeaders; bool m_IncludeBodyLengthTag; @@ -98,7 +100,7 @@ protected: DKIMHEADERCALLBACK m_pfnHdrCallback; - string m_sSig; // DKIM-Signature .... + string m_sSig; // DKIM-Signature .... int m_nSigPos; string m_sReturnedSig; @@ -110,4 +112,4 @@ protected: int m_SigHdrs; }; -#endif // DKIMSIGN_H +#endif // DKIMSIGN_H diff --git a/src/include/dkimverify.h b/src/include/dkimverify.h index 64de2a1..685426b 100644 --- a/src/include/dkimverify.h +++ b/src/include/dkimverify.h @@ -22,131 +22,132 @@ #ifndef DKIMVERIFY_H #define DKIMVERIFY_H -#include "dkimbase.h" #include <vector> +#include "dkimbase.h" + /* not used anymore #define DKIM_ADSP_UNKNOWN 1 #define DKIM_ADSP_ALL 2 #define DKIM_ADSP_DISCARDABLE 3 */ -#define DKIM_POLICY_DOMAIN_NAME_TOO_LONG -50 // internal error -#define DKIM_POLICY_DNS_TEMP_FAILURE -51 // internal error -#define DKIM_POLICY_DNS_PERM_FAILURE -52 // internal error -#define DKIM_POLICY_INVALID -53 // internal error +#define DKIM_POLICY_DOMAIN_NAME_TOO_LONG -50 // internal error +#define DKIM_POLICY_DNS_TEMP_FAILURE -51 // internal error +#define DKIM_POLICY_DNS_PERM_FAILURE -52 // internal error +#define DKIM_POLICY_INVALID -53 // internal error /* dito #define DKIM_SIG_VERSION_PRE_02 0 #define DKIM_SIG_VERSION_02_PLUS 1 */ -class SelectorInfo -{ +class SelectorInfo { public: - SelectorInfo(const string &sSelector,const string &sDomain); - ~SelectorInfo(); + SelectorInfo(const string& sSelector, const string& sDomain); + ~SelectorInfo(); - string Domain; - string Selector; - string Granularity; - bool AllowSHA1; - bool AllowSHA256; - EVP_PKEY *PublicKey; /* the public key */ - bool Testing; - bool SameDomain; + string Domain; + string Selector; + string Granularity; + bool AllowSHA1; + bool AllowSHA256; + EVP_PKEY *PublicKey; /* the public key */ + bool Testing; + bool SameDomain; - int Status; + int Status; - int Parse(char* Buffer); + int Parse(char *Buffer); }; -class SignatureInfo -{ +class SignatureInfo { public: - SignatureInfo(bool SaveCanonicalizedData); - ~SignatureInfo(); - - void Hash(const char* szBuffer,unsigned nBufLength,bool IsBody=false); - - string Header; - unsigned Version; - string Domain; - string Selector; - string SignatureData; - string BodyHashData; - string IdentityLocalPart; - string IdentityDomain; - string CanonicalizedData; - vector<string> SignedHeaders; - unsigned BodyLength; - unsigned HeaderCanonicalization; - unsigned BodyCanonicalization; - unsigned ExpireTime; - - unsigned VerifiedBodyCount; - unsigned UnverifiedBodyCount; - -#if ((OPENSSL_VERSION_NUMBER < 0x10100000L) || (LIBRESSL_VERSION_NUMBER > 0 && LIBRESSL_VERSION_NUMBER < 0x20700000L)) - EVP_MD_CTX m_Hdr_ctx; - EVP_MD_CTX m_Bdy_ctx; + SignatureInfo(bool SaveCanonicalizedData); + ~SignatureInfo(); + + void Hash(const char *szBuffer, unsigned nBufLength, bool IsBody = false); + + string Header; + unsigned Version; + string Domain; + string Selector; + string SignatureData; + string BodyHashData; + string IdentityLocalPart; + string IdentityDomain; + string CanonicalizedData; + vector<string> SignedHeaders; + unsigned BodyLength; + unsigned HeaderCanonicalization; + unsigned BodyCanonicalization; + unsigned ExpireTime; + + unsigned VerifiedBodyCount; + unsigned UnverifiedBodyCount; + +#if ( \ + (OPENSSL_VERSION_NUMBER < 0x10100000L) \ + || (LIBRESSL_VERSION_NUMBER > 0 && LIBRESSL_VERSION_NUMBER < 0x20700000L)) + EVP_MD_CTX m_Hdr_ctx; + EVP_MD_CTX m_Bdy_ctx; #else - EVP_MD_CTX *m_Hdr_ctx; - EVP_MD_CTX *m_Bdy_ctx; + EVP_MD_CTX *m_Hdr_ctx; + EVP_MD_CTX *m_Bdy_ctx; #endif #if (OPENSSL_VERSION_NUMBER > 0x10101000L) - EVP_MD_CTX *m_Msg_ctx; + EVP_MD_CTX *m_Msg_ctx; #endif - SelectorInfo *m_pSelector; + SelectorInfo *m_pSelector; - int Status; - int m_nHash; // use one of the DKIM_HASH_xxx constants here - unsigned EmptyLineCount; - bool m_SaveCanonicalizedData; + int Status; + int m_nHash; // use one of the DKIM_HASH_xxx constants here + unsigned EmptyLineCount; + bool m_SaveCanonicalizedData; }; -class CDKIMVerify : public CDKIMBase -{ +class CDKIMVerify : public CDKIMBase { public: + CDKIMVerify(); + ~CDKIMVerify(); + // virtual ~CDKIMVerify() = 0; - CDKIMVerify(); - ~CDKIMVerify(); -// virtual ~CDKIMVerify() = 0; + int Init(DKIMVerifyOptions *pOptions); - int Init(DKIMVerifyOptions* pOptions); + int GetResults(void); + int GetDetails(int *nSigCount, DKIMVerifyDetails **pDetails); + // int _DNSGetTXT(const char* szFQDN,char* Buffer,int nBufLen); - int GetResults(void); - int GetDetails(int* nSigCount,DKIMVerifyDetails** pDetails); -// int _DNSGetTXT(const char* szFQDN,char* Buffer,int nBufLen); + virtual int ProcessHeaders(void); + virtual int ProcessBody(char *szBuffer, int nBufLength, bool bEOF); - virtual int ProcessHeaders(void); - virtual int ProcessBody(char* szBuffer,int nBufLength,bool bEOF); - - const char* GetPractices() { return Practices.c_str(); } + const char *GetPractices() + { + return Practices.c_str(); + } protected: + int ParseDKIMSignature(const string& sHeader, SignatureInfo& sig); - int ParseDKIMSignature(const string& sHeader,SignatureInfo &sig); - - SelectorInfo& GetSelector(const string &sSelector,const string &sDomain); + SelectorInfo& GetSelector(const string& sSelector, const string& sDomain); -// int GetADSP(const string &sDomain,int &iADSP); + // int GetADSP(const string &sDomain,int &iADSP); - list<SignatureInfo> Signatures; - list<SelectorInfo> Selectors; + list<SignatureInfo> Signatures; + list<SelectorInfo> Selectors; - DKIMDNSCALLBACK m_pfnSelectorCallback; // selector record callback -// DKIMDNSCALLBACK m_pfnPracticesCallback; // ADSP record callback + DKIMDNSCALLBACK m_pfnSelectorCallback; // selector record callback + // DKIMDNSCALLBACK m_pfnPracticesCallback; // ADSP record callback - bool m_HonorBodyLengthTag; - bool m_CheckPractices; - bool m_SubjectIsRequired; - bool m_SaveCanonicalizedData; - bool m_AllowUnsignedFromHeaders; + bool m_HonorBodyLengthTag; + bool m_CheckPractices; + bool m_SubjectIsRequired; + bool m_SaveCanonicalizedData; + bool m_AllowUnsignedFromHeaders; - vector<DKIMVerifyDetails> Details; - string Practices; + vector<DKIMVerifyDetails> Details; + string Practices; }; -#endif //DKIMVERIFY_H +#endif //DKIMVERIFY_H diff --git a/src/include/dns.h b/src/include/dns.h index 6293478..e914cda 100644 --- a/src/include/dns.h +++ b/src/include/dns.h @@ -6,19 +6,22 @@ extern "C" { #endif #include "dnsresolv.h" -#include "ipalloc.h" #include "stralloc.h" -#define DNS_INIT static char seed[128]; dns_random_init(seed); -#define DNS_NXD 0 -#define DNS_SOFT -5 -#define DNS_HARD -6 +#include "ipalloc.h" + +#define DNS_INIT \ + static char seed[128]; \ + dns_random_init(seed); +#define DNS_NXD 0 +#define DNS_SOFT -5 +#define DNS_HARD -6 void dns_init(int); -int dns_ip(ipalloc *,stralloc *); -int dns_mxip(ipalloc *,stralloc *,unsigned long); -int dns_tlsa(stralloc *,const stralloc *); -int dns_mxhost(stralloc *,const stralloc *); +int dns_ip(ipalloc *, stralloc *); +int dns_mxip(ipalloc *, stralloc *, unsigned long); +int dns_tlsa(stralloc *, const stralloc *); +int dns_mxhost(stralloc *, const stralloc *); #ifdef __cplusplus } diff --git a/src/include/dnsgettxt.h b/src/include/dnsgettxt.h index 6d6b8ea..3158fe0 100644 --- a/src/include/dnsgettxt.h +++ b/src/include/dnsgettxt.h @@ -1,7 +1,7 @@ #ifdef cplusplus extern "C" { -int DNSGetTXT(const char* szFQDN,char* Buffer,int nBufLen); -extern void dns_random_init(const char [12]); +int DNSGetTXT(const char *szFQDN, char *Buffer, int nBufLen); +extern void dns_random_init(const char[12]); #endif diff --git a/src/include/extra.h b/src/include/extra.h index c598175..df43a26 100644 --- a/src/include/extra.h +++ b/src/include/extra.h @@ -1,7 +1,7 @@ #ifndef EXTRA_H #define EXTRA_H -#define QUEUE_EXTRA "" +#define QUEUE_EXTRA "" #define QUEUE_EXTRALEN 0 #endif diff --git a/src/include/fifo.h b/src/include/fifo.h index f48c863..cfaa801 100644 --- a/src/include/fifo.h +++ b/src/include/fifo.h @@ -1,6 +1,6 @@ #ifndef FIFO_H #define FIFO_H -int fifo_make(char *,int); +int fifo_make(char *, int); #endif diff --git a/src/include/fmtqfn.h b/src/include/fmtqfn.h index e11e51e..11f48d3 100644 --- a/src/include/fmtqfn.h +++ b/src/include/fmtqfn.h @@ -1,7 +1,7 @@ #ifndef FMTQFN_H #define FMTQFN_H -unsigned int fmtqfn(char *,char *,unsigned long,int); +unsigned int fmtqfn(char *, char *, unsigned long, int); #define FMTQFN 40 /* maximum space needed, if len(dirslash) <= 10 */ diff --git a/src/include/gfrom.h b/src/include/gfrom.h index 121ff6d..40fb769 100644 --- a/src/include/gfrom.h +++ b/src/include/gfrom.h @@ -1,6 +1,6 @@ #ifndef GFROM_H #define GFROM_H -int gfrom(char *,int); +int gfrom(char *, int); #endif diff --git a/src/include/global.h b/src/include/global.h index 2d8ccf4..7e9b673 100644 --- a/src/include/global.h +++ b/src/include/global.h @@ -1,6 +1,7 @@ /* GLOBAL.H - RSAREF types and constants */ #include <string.h> + #include "uint_t.h" /* Copyright (C) RSA Laboratories, a division of RSA Data Security, @@ -8,16 +9,16 @@ */ #ifndef _GLOBAL_H_ -#define _GLOBAL_H_ 1 + #define _GLOBAL_H_ 1 -/* PROTOTYPES should be set to one if and only if the compiler supports + /* PROTOTYPES should be set to one if and only if the compiler supports function argument prototyping. The following makes PROTOTYPES default to 1 if it has not already been defined as 0 with C compiler flags. */ -#ifndef PROTOTYPES -#define PROTOTYPES 1 -#endif + #ifndef PROTOTYPES + #define PROTOTYPES 1 + #endif /* POINTER defines a generic pointer type */ typedef unsigned char *POINTER; @@ -25,29 +26,29 @@ typedef unsigned char *POINTER; /* UINT2 defines a two byte word */ typedef unsigned short int UINT2; -/* UINT4 defines a four byte word */ -#ifdef UINT32_H -#define UINT4 uint32 -#else + /* UINT4 defines a four byte word */ + #ifdef UINT32_H + #define UINT4 uint32 + #else typedef unsigned long int UINT4; -#endif + #endif -#ifndef NULL_PTR -#define NULL_PTR ((POINTER)0) -#endif + #ifndef NULL_PTR + #define NULL_PTR ((POINTER)0) + #endif -#ifndef UNUSED_ARG -#define UNUSED_ARG(x) x = *(&x); -#endif + #ifndef UNUSED_ARG + #define UNUSED_ARG(x) x = *(&x); + #endif -/* PROTO_LIST is defined depending on how PROTOTYPES is defined above. + /* PROTO_LIST is defined depending on how PROTOTYPES is defined above. If using PROTOTYPES, then PROTO_LIST returns the list, otherwise it returns an empty list. */ -#if PROTOTYPES -#define PROTO_LIST(list) list -#else -#define PROTO_LIST(list) () -#endif + #if PROTOTYPES + #define PROTO_LIST(list) list + #else + #define PROTO_LIST(list) () + #endif #endif /* end _GLOBAL_H_ */ diff --git a/src/include/hfield.h b/src/include/hfield.h index ab367f4..f2110b5 100644 --- a/src/include/hfield.h +++ b/src/include/hfield.h @@ -5,34 +5,34 @@ unsigned int hfield_skipname(); int hfield_known(); int hfield_valid(); -#define H_SENDER 1 -#define H_FROM 2 -#define H_REPLYTO 3 -#define H_TO 4 -#define H_CC 5 -#define H_BCC 6 -#define H_DATE 7 -#define H_MESSAGEID 8 -#define H_SUBJECT 9 -#define H_R_SENDER 10 -#define H_R_FROM 11 -#define H_R_REPLYTO 12 -#define H_R_TO 13 -#define H_R_CC 14 -#define H_R_BCC 15 -#define H_R_DATE 16 -#define H_R_MESSAGEID 17 -#define H_RETURNRECEIPTTO 18 -#define H_ERRORSTO 19 -#define H_APPARENTLYTO 20 -#define H_RECEIVED 21 -#define H_RETURNPATH 22 -#define H_DELIVEREDTO 23 -#define H_CONTENTLENGTH 24 -#define H_CONTENTTYPE 25 -#define H_CONTENTTRANSFERENCODING 26 +#define H_SENDER 1 +#define H_FROM 2 +#define H_REPLYTO 3 +#define H_TO 4 +#define H_CC 5 +#define H_BCC 6 +#define H_DATE 7 +#define H_MESSAGEID 8 +#define H_SUBJECT 9 +#define H_R_SENDER 10 +#define H_R_FROM 11 +#define H_R_REPLYTO 12 +#define H_R_TO 13 +#define H_R_CC 14 +#define H_R_BCC 15 +#define H_R_DATE 16 +#define H_R_MESSAGEID 17 +#define H_RETURNRECEIPTTO 18 +#define H_ERRORSTO 19 +#define H_APPARENTLYTO 20 +#define H_RECEIVED 21 +#define H_RETURNPATH 22 +#define H_DELIVEREDTO 23 +#define H_CONTENTLENGTH 24 +#define H_CONTENTTYPE 25 +#define H_CONTENTTRANSFERENCODING 26 #define H_NOTICEREQUESTEDUPONDELIVERYTO 27 -#define H_MAILFOLLOWUPTO 28 -#define H_NUM 29 +#define H_MAILFOLLOWUPTO 28 +#define H_NUM 29 #endif diff --git a/src/include/hier.h b/src/include/hier.h index f040e79..21413cc 100644 --- a/src/include/hier.h +++ b/src/include/hier.h @@ -1,10 +1,10 @@ #ifndef HIER_H #define HIER_H -void c(char *,char *,char *,int,int,int); -void h(char *,int,int,int); -void d(char *,char *,int,int,int); -void p(char *,char *,int,int,int); -void z(char *,char *,int,int,int,int); +void c(char *, char *, char *, int, int, int); +void h(char *, int, int, int); +void d(char *, char *, int, int, int); +void p(char *, char *, int, int, int); +void z(char *, char *, int, int, int, int); #endif diff --git a/src/include/hmac_md5.h b/src/include/hmac_md5.h index 87a2c37..aa8c00d 100644 --- a/src/include/hmac_md5.h +++ b/src/include/hmac_md5.h @@ -1,7 +1,6 @@ #ifndef HMAC_MD5_H #define HMAC_MD5_H -void hmac_md5(unsigned char *,int,unsigned char *,int,unsigned char *); +void hmac_md5(unsigned char *, int, unsigned char *, int, unsigned char *); #endif - diff --git a/src/include/ipalloc.h b/src/include/ipalloc.h index 0f58e92..6626ecc 100644 --- a/src/include/ipalloc.h +++ b/src/include/ipalloc.h @@ -1,8 +1,8 @@ #ifndef IPALLOC_H #define IPALLOC_H -#include "ip.h" #include "genalloc.h" +#include "ip.h" #define NAME_LEN 256 struct ip_mx { @@ -10,13 +10,12 @@ struct ip_mx { union { struct ip4_address ip4; struct ip6_address ip6; - } addr; + } addr; int pref; char mxh[NAME_LEN]; }; -GEN_ALLOC_typedef(ipalloc,struct ip_mx,ix,len,a) -int ipalloc_readyplus(); +GEN_ALLOC_typedef(ipalloc, struct ip_mx, ix, len, a) int ipalloc_readyplus(); int ipalloc_append(); #endif diff --git a/src/include/ipme.h b/src/include/ipme.h index 9705f45..f9dff4e 100644 --- a/src/include/ipme.h +++ b/src/include/ipme.h @@ -2,6 +2,7 @@ #define IPME_H #include "ip.h" + #include "ipalloc.h" extern ipalloc ipme; diff --git a/src/include/maildir.h b/src/include/maildir.h index 5e48822..69e679b 100644 --- a/src/include/maildir.h +++ b/src/include/maildir.h @@ -2,12 +2,13 @@ #define MAILDIR_H #include "logmsg.h" -#include "prioq.h" + +#include "prioq.h" extern struct strerr maildir_chdir_err; extern struct strerr maildir_scan_err; int maildir_chdir(void); void maildir_clean(stralloc *); -int maildir_scan(prioq *,stralloc *,int,int); +int maildir_scan(prioq *, stralloc *, int, int); #endif diff --git a/src/include/md5.h b/src/include/md5.h index 94774ba..bb12b74 100644 --- a/src/include/md5.h +++ b/src/include/md5.h @@ -32,15 +32,14 @@ extern "C" { /* MD5 context. */ typedef struct { - UINT4 state[4]; /* state (ABCD) */ - UINT4 count[2]; /* number of bits, modulo 2^64 (lsb first) */ - unsigned char buffer[64]; /* input buffer */ + UINT4 state[4]; /* state (ABCD) */ + UINT4 count[2]; /* number of bits, modulo 2^64 (lsb first) */ + unsigned char buffer[64]; /* input buffer */ } MD5_CTX; -void MD5Init PROTO_LIST ((MD5_CTX *)); -void MD5Update PROTO_LIST - ((MD5_CTX *, unsigned char *, unsigned int)); -void MD5Final PROTO_LIST ((unsigned char [16], MD5_CTX *)); +void MD5Init PROTO_LIST((MD5_CTX *)); +void MD5Update PROTO_LIST((MD5_CTX *, unsigned char *, unsigned int)); +void MD5Final PROTO_LIST((unsigned char[16], MD5_CTX *)); #ifdef __cplusplus } diff --git a/src/include/mfrules.h b/src/include/mfrules.h index b79f338..68e678b 100644 --- a/src/include/mfrules.h +++ b/src/include/mfrules.h @@ -4,6 +4,6 @@ #include "stralloc.h" extern stralloc key; -int mfrules(int,char *,char *,char *,char *); +int mfrules(int, char *, char *, char *, char *); #endif diff --git a/src/include/prioq.h b/src/include/prioq.h index 3547b1c..cbddd9b 100644 --- a/src/include/prioq.h +++ b/src/include/prioq.h @@ -1,14 +1,18 @@ #ifndef PRIOQ_H #define PRIOQ_H -#include "datetime.h" #include "genalloc.h" -struct prioq_elt { datetime_sec dt; unsigned long id; } ; +#include "datetime.h" + +struct prioq_elt { + datetime_sec dt; + unsigned long id; +}; -GEN_ALLOC_typedef(prioq,struct prioq_elt,p,len,a) +GEN_ALLOC_typedef(prioq, struct prioq_elt, p, len, a) -int prioq_insert(); + int prioq_insert(); int prioq_min(); void prioq_delmin(); diff --git a/src/include/qlx.h b/src/include/qlx.h index 713946d..9008b31 100644 --- a/src/include/qlx.h +++ b/src/include/qlx.h @@ -3,16 +3,16 @@ /* 0, 111, 100 are qmail-local success, soft, hard */ -#define QLX_USAGE 112 -#define QLX_BUG 101 -#define QLX_ROOT 113 -#define QLX_NFS 115 -#define QLX_NOALIAS 116 -#define QLX_CDB 117 -#define QLX_SYS 118 -#define QLX_NOMEM 119 +#define QLX_USAGE 112 +#define QLX_BUG 101 +#define QLX_ROOT 113 +#define QLX_NFS 115 +#define QLX_NOALIAS 116 +#define QLX_CDB 117 +#define QLX_SYS 118 +#define QLX_NOMEM 119 #define QLX_EXECSOFT 120 -#define QLX_EXECPW 121 +#define QLX_EXECPW 121 #define QLX_EXECHARD 126 #endif diff --git a/src/include/qmail.h b/src/include/qmail.h index 7db214d..8e0ad3d 100644 --- a/src/include/qmail.h +++ b/src/include/qmail.h @@ -10,13 +10,13 @@ struct qmail { int fde; buffer ss; char buf[1024]; -} ; +}; extern int qmail_open(struct qmail *); -extern void qmail_put(struct qmail *,char *, int); -extern void qmail_puts(struct qmail *,char *); -extern void qmail_from(struct qmail *,char *); -extern void qmail_to(struct qmail *,char *); +extern void qmail_put(struct qmail *, char *, int); +extern void qmail_puts(struct qmail *, char *); +extern void qmail_from(struct qmail *, char *); +extern void qmail_to(struct qmail *, char *); extern void qmail_fail(struct qmail *); extern char *qmail_close(struct qmail *); extern unsigned long qmail_qp(struct qmail *); diff --git a/src/include/qsutil.h b/src/include/qsutil.h index a77a3f8..7ef8ec1 100644 --- a/src/include/qsutil.h +++ b/src/include/qsutil.h @@ -4,10 +4,10 @@ #include "stralloc.h" void log1s(char *); -void log2s(char *,char *); -void log3s(char *,char *,char *); -void log4s(char *,char *,char *,char *); -void log5s(char *,char *,char *,char *,char *); +void log2s(char *, char *); +void log3s(char *, char *, char *); +void log4s(char *, char *, char *, char *); +void log5s(char *, char *, char *, char *, char *); void logsa(stralloc *); void nomem(); void pausedir(char *); diff --git a/src/include/quote.h b/src/include/quote.h index 4afbc94..4f1c467 100644 --- a/src/include/quote.h +++ b/src/include/quote.h @@ -3,8 +3,8 @@ #include "stralloc.h" -int quote_need(char *,unsigned int); +int quote_need(char *, unsigned int); int quote(stralloc *, stralloc *); -int quote2(stralloc *,char *); +int quote2(stralloc *, char *); #endif diff --git a/src/include/readsubdir.h b/src/include/readsubdir.h index e612fac..3910693 100644 --- a/src/include/readsubdir.h +++ b/src/include/readsubdir.h @@ -3,14 +3,12 @@ #include "direntry.h" -typedef struct readsubdir -{ +typedef struct readsubdir { DIR *dir; int pos; char *name; void (*pause)(); -} -readsubdir; +} readsubdir; void readsubdir_init(); int readsubdir_next(); diff --git a/src/include/received.h b/src/include/received.h index 2b2ce75..eac017f 100644 --- a/src/include/received.h +++ b/src/include/received.h @@ -3,7 +3,7 @@ #include "qmail.h" -void received(struct qmail *,char *,char *,char *,char *,char *,char *,char *,char *); -void spfheader(struct qmail *,char *,char *,char *,char *,char *); +void received(struct qmail *, char *, char *, char *, char *, char *, char *, char *, char *); +void spfheader(struct qmail *, char *, char *, char *, char *, char *); #endif diff --git a/src/include/recipients.h b/src/include/recipients.h index b73f37e..e666bd1 100644 --- a/src/include/recipients.h +++ b/src/include/recipients.h @@ -2,7 +2,7 @@ #define RECIPIENTS_H int recipients_init(void); -int recipients(char *,int); +int recipients(char *, int); ssize_t safewrite(); #endif diff --git a/src/include/sendtodo.h b/src/include/sendtodo.h index 86b4434..c2c6819 100644 --- a/src/include/sendtodo.h +++ b/src/include/sendtodo.h @@ -4,11 +4,11 @@ /* critical timing feature #1: if not triggered, do not busy-loop */ /* critical timing feature #2: if triggered, respond within fixed time */ /* important timing feature: when triggered, respond instantly */ -#define SLEEP_TODO 1500 /* check todo/ every 25 minutes in any case */ -#define SLEEP_FUZZ 1 /* slop a bit on sleeps to avoid zeno effect */ +#define SLEEP_TODO 1500 /* check todo/ every 25 minutes in any case */ +#define SLEEP_FUZZ 1 /* slop a bit on sleeps to avoid zeno effect */ #define SLEEP_FOREVER 86400 /* absolute maximum time spent in select() */ #define SLEEP_CLEANUP 76431 /* time between cleanups */ #define SLEEP_SYSFAIL 123 -#define OSSIFIED 129600 /* 36 hours; _must_ exceed q-q's DEATH (24 hours) */ +#define OSSIFIED 129600 /* 36 hours; _must_ exceed q-q's DEATH (24 hours) */ #endif diff --git a/src/include/sha1.h b/src/include/sha1.h index 43d141d..3d35750 100644 --- a/src/include/sha1.h +++ b/src/include/sha1.h @@ -15,8 +15,7 @@ #define SHA1_BLOCKSIZE 64 #define SHA1_DIGESTSIZE 20 -typedef struct -{ +typedef struct { uint32_t state[5]; uint32_t count[2]; uint8_t buffer[SHA1_BLOCKSIZE]; diff --git a/src/include/sha256.h b/src/include/sha256.h index e8979c5..8c920b1 100644 --- a/src/include/sha256.h +++ b/src/include/sha256.h @@ -1,8 +1,7 @@ #ifndef SHA256_H #define SHA256_H -typedef struct -{ +typedef struct { uint8_t data[64]; uint32_t datalen; uint32_t bitlen[2]; @@ -13,6 +12,6 @@ static void sha256_init(sha256_ctx *ctx); static void sha256_transform(sha256_ctx *ctx, uint8_t *data); static void sha256_update(sha256_ctx *ctx, uint8_t *data, uint32_t len); static void sha256_final(uint8_t *hash, sha256_ctx *ctx); -extern void sha256_hash(char *hash,const char *data, size_t len); +extern void sha256_hash(char *hash, const char *data, size_t len); #endif diff --git a/src/include/smtpdlog.h b/src/include/smtpdlog.h index 0feb126..9b305c5 100644 --- a/src/include/smtpdlog.h +++ b/src/include/smtpdlog.h @@ -6,12 +6,12 @@ void flush(); void out(); void smtpdlog_init(void); -void smtp_loga(char *,char *,char *,char *,char *,char *,char *,char *,char *); -void smtp_logb(char *,char *,char *,char *,char *,char *,char *); -void smtp_logg(char *,char *,char *,char *,char *,char *,char *); -void smtp_logh(char *,char *,char *,char *,char *); -void smtp_logi(char *,char *,char *,char *,char *,char *,char *,char *); -void smtp_logr(char *,char *,char *,char *,char *,char *,char *,char *); +void smtp_loga(char *, char *, char *, char *, char *, char *, char *, char *, char *); +void smtp_logb(char *, char *, char *, char *, char *, char *, char *); +void smtp_logg(char *, char *, char *, char *, char *, char *, char *); +void smtp_logh(char *, char *, char *, char *, char *); +void smtp_logi(char *, char *, char *, char *, char *, char *, char *, char *); +void smtp_logr(char *, char *, char *, char *, char *, char *, char *, char *); void die_read(void); void die_alarm(void); @@ -34,40 +34,40 @@ int err_fork(void); int err_pipe(void); int err_write(void); int err_starttls(void); -void err_tlsreq(char *,char *,char *,char *,char *); +void err_tlsreq(char *, char *, char *, char *, char *); -void err_helo(char *,char *,char *,char *,char *,char *,char *,char *); -void err_spf(char *,char *,char *,char *,char *,char *,char *,char *); +void err_helo(char *, char *, char *, char *, char *, char *, char *, char *); +void err_spf(char *, char *, char *, char *, char *, char *, char *, char *); -void err_authsetup(char *,char *,char *,char *,char *); +void err_authsetup(char *, char *, char *, char *, char *); void err_authd(void); -void err_authmail(void); -void err_authfail(char *,char *,char *,char *,char *,char *,char *); -void err_authinvalid(char *,char *,char *,char *,char *); +void err_authmail(void); +void err_authfail(char *, char *, char *, char *, char *, char *, char *); +void err_authinvalid(char *, char *, char *, char *, char *); void err_authabrt(void); -void err_authreq(char *,char *,char *,char *,char *); -void err_submission(char *,char *,char *,char *,char *); +void err_authreq(char *, char *, char *, char *, char *); +void err_submission(char *, char *, char *, char *, char *); int err_authabort(void); -int err_authinput(void); +int err_authinput(void); int err_noauth(void); void err_wantmail(void); -void err_mav(char *,char *,char *,char *,char *,char *,char *); -void err_bmf(char *,char *,char *,char *,char *,char *,char *,char *); -void err_mfdns(char *,char *,char *,char *,char *,char *,char *); +void err_mav(char *, char *, char *, char *, char *, char *, char *); +void err_bmf(char *, char *, char *, char *, char *, char *, char *, char *); +void err_mfdns(char *, char *, char *, char *, char *, char *, char *); -void err_nogateway(char *,char *,char *,char *,char *,char *,char *); -void err_brt(char *,char *,char *,char *,char *,char *,char *); -void err_rcpts(char *,char *,char *,char *,char *,char *,char *); -void err_recipient(char *,char *,char *,char *,char *,char *,char *); +void err_nogateway(char *, char *, char *, char *, char *, char *, char *); +void err_brt(char *, char *, char *, char *, char *, char *, char *); +void err_rcpts(char *, char *, char *, char *, char *, char *, char *); +void err_recipient(char *, char *, char *, char *, char *, char *, char *); -void straynewline(void); +void straynewline(void); void err_notorious(void); -void err_size(char *,char *,char *,char *,char *,char *,char *); -void err_data(char *,char *,char *,char *,char *,char *,char *,char *); +void err_size(char *, char *, char *, char *, char *, char *, char *); +void err_data(char *, char *, char *, char *, char *, char *, char *, char *); int err_postgl(void); int err_forkgl(void); -void postgrey(char *,char *,char *,char *,char *,char *,char *); +void postgrey(char *, char *, char *, char *, char *, char *, char *); #endif diff --git a/src/include/spf.h b/src/include/spf.h index ca20418..0b8b0fc 100644 --- a/src/include/spf.h +++ b/src/include/spf.h @@ -2,12 +2,13 @@ #define SPF_H #include "stralloc.h" + #include "ipalloc.h" /* (Internal) Processing codes */ #define SPF_INIT -1 -#define SPF_EXT -2 /* x */ +#define SPF_EXT -2 /* x */ #define SPF_ME -3 #define SPF_EXHAUST -4 #define SPF_LOOP -5 @@ -15,23 +16,23 @@ #define SPF_LOCAL -7 #define SPF_ERROR -8 #define SPF_NOMEM -9 -#define SPF_SYNTAX -10 /* Setup problem */ +#define SPF_SYNTAX -10 /* Setup problem */ /* (External) Resulting codes */ -#define SPF_OK 0 /* + Pass */ -#define SPF_NONE 1 /* o None */ -#define SPF_UNKNOWN 2 /* u Unknown method */ -#define SPF_NEUTRAL 3 /* ? Neutral */ -#define SPF_SOFTFAIL 4 /* ~ Softfail */ -#define SPF_FAIL 5 /* - Not Permitted */ -#define SPF_DNSSOFT 6 /* d From DNS; not used */ +#define SPF_OK 0 /* + Pass */ +#define SPF_NONE 1 /* o None */ +#define SPF_UNKNOWN 2 /* u Unknown method */ +#define SPF_NEUTRAL 3 /* ? Neutral */ +#define SPF_SOFTFAIL 4 /* ~ Softfail */ +#define SPF_FAIL 5 /* - Not Permitted */ +#define SPF_DNSSOFT 6 /* d From DNS; not used */ #define LOOKUP_LIMIT 10 /* spfinfo: S=remoteip|O=mailfrom|C=identity/domain|H=helo|M(echanism)=query|D=redirect|I=domain|P=problem|R:result */ -#define SPF_DEFEXP "See http://%{d}/why.html?sender=%{s}&ip=%{i}&receiver=%{r}" +#define SPF_DEFEXP "See http://%{d}/why.html?sender=%{s}&ip=%{i}&receiver=%{r}" extern int flagip6; extern stralloc spfmf; @@ -48,64 +49,60 @@ extern stralloc spfexpmsg; /* this table and macro came from wget more or less */ /* and was in turn stolen by me++ from libspf as is :) */ -const static unsigned char urlchr_table[256] = -{ - 1, 1, 1, 1, 1, 1, 1, 1, /* NUL SOH STX ETX EOT ENQ ACK BEL */ - 1, 1, 1, 1, 1, 1, 1, 1, /* BS HT LF VT FF CR SO SI */ - 1, 1, 1, 1, 1, 1, 1, 1, /* DLE DC1 DC2 DC3 DC4 NAK SYN ETB */ - 1, 1, 1, 1, 1, 1, 1, 1, /* CAN EM SUB ESC FS GS RS US */ - 1, 0, 1, 1, 0, 1, 1, 0, /* SP ! " # $ % & ' */ - 0, 0, 0, 1, 0, 0, 0, 1, /* ( ) * + , - . / */ - 0, 0, 0, 0, 0, 0, 0, 0, /* 0 1 2 3 4 5 6 7 */ - 0, 0, 1, 1, 1, 1, 1, 1, /* 8 9 : ; < = > ? */ - 1, 0, 0, 0, 0, 0, 0, 0, /* @ A B C D E F G */ - 0, 0, 0, 0, 0, 0, 0, 0, /* H I J K L M N O */ - 0, 0, 0, 0, 0, 0, 0, 0, /* P Q R S T U V W */ - 0, 0, 0, 1, 1, 1, 1, 0, /* X Y Z [ \ ] ^ _ */ - 1, 0, 0, 0, 0, 0, 0, 0, /* ` a b c d e f g */ - 0, 0, 0, 0, 0, 0, 0, 0, /* h i j k l m n o */ - 0, 0, 0, 0, 0, 0, 0, 0, /* p q r s t u v w */ - 0, 0, 0, 1, 1, 1, 1, 1, /* x y z { | } ~ DEL */ - - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, +const static unsigned char urlchr_table[256] = { + 1, 1, 1, 1, 1, 1, 1, 1, /* NUL SOH STX ETX EOT ENQ ACK BEL */ + 1, 1, 1, 1, 1, 1, 1, 1, /* BS HT LF VT FF CR SO SI */ + 1, 1, 1, 1, 1, 1, 1, 1, /* DLE DC1 DC2 DC3 DC4 NAK SYN ETB */ + 1, 1, 1, 1, 1, 1, 1, 1, /* CAN EM SUB ESC FS GS RS US */ + 1, 0, 1, 1, 0, 1, 1, 0, /* SP ! " # $ % & ' */ + 0, 0, 0, 1, 0, 0, 0, 1, /* ( ) * + , - . / */ + 0, 0, 0, 0, 0, 0, 0, 0, /* 0 1 2 3 4 5 6 7 */ + 0, 0, 1, 1, 1, 1, 1, 1, /* 8 9 : ; < = > ? */ + 1, 0, 0, 0, 0, 0, 0, 0, /* @ A B C D E F G */ + 0, 0, 0, 0, 0, 0, 0, 0, /* H I J K L M N O */ + 0, 0, 0, 0, 0, 0, 0, 0, /* P Q R S T U V W */ + 0, 0, 0, 1, 1, 1, 1, 0, /* X Y Z [ \ ] ^ _ */ + 1, 0, 0, 0, 0, 0, 0, 0, /* ` a b c d e f g */ + 0, 0, 0, 0, 0, 0, 0, 0, /* h i j k l m n o */ + 0, 0, 0, 0, 0, 0, 0, 0, /* p q r s t u v w */ + 0, 0, 0, 1, 1, 1, 1, 1, /* x y z { | } ~ DEL */ + + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, }; -#define WSPACE(x) ((x) == ' ' || (x) == '\t' || (x) == '\r' || (x) == '\n') -#define NXTOK(b, p, a) do { (b) = (p); \ - while((p) < (a)->len && !WSPACE((a)->s[(p)])) ++(p); \ - while((p) < (a)->len && WSPACE((a)->s[(p)])) (a)->s[(p)++] = 0; \ - } while(0) +#define WSPACE(x) ((x) == ' ' || (x) == '\t' || (x) == '\r' || (x) == '\n') +#define NXTOK(b, p, a) \ + do { \ + (b) = (p); \ + while ((p) < (a)->len && !WSPACE((a)->s[(p)])) ++(p); \ + while ((p) < (a)->len && WSPACE((a)->s[(p)])) (a)->s[(p)++] = 0; \ + } while (0) /* spfdnsip.c */ -int match_ip4(unsigned char [4],int,char [4]); -int match_ip6(unsigned char [16],int,char [16]); +int match_ip4(unsigned char[4], int, char[4]); +int match_ip6(unsigned char[16], int, char[16]); int get_prefix(char *); -int spf_records(stralloc *,stralloc *); -int spf_include(char *,char *); -int spf_a(char *,char *); -int spf_mx(char *,char *); -int spf_ptr(char *,char *); -int spf_ip4(char *,char *); -int spf_ip6(char *,char *); -int spf_exists(char *,char *); +int spf_records(stralloc *, stralloc *); +int spf_include(char *, char *); +int spf_a(char *, char *); +int spf_mx(char *, char *); +int spf_ptr(char *, char *); +int spf_ip4(char *, char *); +int spf_ip6(char *, char *); +int spf_exists(char *, char *); /* spf.c */ -int spf_query(const char *,const char *,const char *,const char *,const int); +int spf_query(const char *, const char *, const char *, const char *, const int); int spf_lookup(stralloc *); -int spf_mechanism(char *,char *,char *,char *); -int spf_parse(stralloc *,char *,char *); -int spf_macros(stralloc *,char *,char *); -int spf_info(char *,const char *); +int spf_mechanism(char *, char *, char *, char *); +int spf_parse(stralloc *, char *, char *); +int spf_macros(stralloc *, char *, char *); +int spf_info(char *, const char *); #endif - diff --git a/src/include/srs2.h b/src/include/srs2.h index e993928..9856636 100644 --- a/src/include/srs2.h +++ b/src/include/srs2.h @@ -19,12 +19,12 @@ */ /* This is ugly, but reasonably safe. */ -#undef TRUE +#undef TRUE #define TRUE 1 -#undef FALSE +#undef FALSE #define FALSE 0 -#define SRSSEP '=' +#define SRSSEP '=' #define SRS0TAG "SRS0" #define SRS1TAG "SRS1" @@ -37,15 +37,15 @@ #define SRS_ERRTYPE_SYNTAX 0x4000 #define SRS_ERRTYPE_SRS 0x8000 -#define SRS_SUCCESS (0) -#define SRS_ENOTSRSADDRESS (1) -#define SRS_ENOTREWRITTEN (2) +#define SRS_SUCCESS (0) +#define SRS_ENOTSRSADDRESS (1) +#define SRS_ENOTREWRITTEN (2) #define SRS_ENOSECRETS (SRS_ERRTYPE_CONFIG | 1) #define SRS_ESEPARATORINVALID (SRS_ERRTYPE_CONFIG | 2) -#define SRS_ENOSENDERATSIGN (SRS_ERRTYPE_INPUT | 1) -#define SRS_EBUFTOOSMALL (SRS_ERRTYPE_INPUT | 2) +#define SRS_ENOSENDERATSIGN (SRS_ERRTYPE_INPUT | 1) +#define SRS_EBUFTOOSMALL (SRS_ERRTYPE_INPUT | 2) #define SRS_ENOSRS0HOST (SRS_ERRTYPE_SYNTAX | 1) #define SRS_ENOSRS0USER (SRS_ERRTYPE_SYNTAX | 2) @@ -58,69 +58,67 @@ #define SRS_EHASHTOOSHORT (SRS_ERRTYPE_SYNTAX | 9) #define SRS_ETIMESTAMPOUTOFDATE (SRS_ERRTYPE_SRS | 1) -#define SRS_EHASHINVALID (SRS_ERRTYPE_SRS | 2) +#define SRS_EHASHINVALID (SRS_ERRTYPE_SRS | 2) #define SRS_ERROR_TYPE(x) ((x) & SRS_ERRTYPE_MASK) /* SRS implementation */ -#define SRS_IS_SRS_ADDRESS(x) ( \ - (strncasecmp((x),"SRS",3) == 0) && \ - (strchr("01", (x)[3]) != NULL) && \ - (strchr("-+=", (x)[4]) != NULL) \ -) +#define SRS_IS_SRS_ADDRESS(x) \ + ((strncasecmp((x), "SRS", 3) == 0) && (strchr("01", (x)[3]) != NULL) \ + && (strchr("-+=", (x)[4]) != NULL)) typedef void *(*srs_malloc_t)(size_t); -typedef void *(*srs_realloc_t)(void *,size_t); +typedef void *(*srs_realloc_t)(void *, size_t); typedef void (*srs_free_t)(void *); typedef int srs_bool; typedef struct _srs_t { /* Rewriting parameters */ -// stralloc cookies; + // stralloc cookies; char **secrets; int numsecrets; char separator; /* Security parameters */ - int maxage; /* Maximum allowed age in seconds */ + int maxage; /* Maximum allowed age in seconds */ int hashlen; int hashmin; /* Behaviour parameters */ - srs_bool alwaysrewrite; /* Rewrite even into same domain? */ - srs_bool noforward; /* Never perform forwards rewriting */ - srs_bool noreverse; /* Never perform reverse rewriting */ - char **neverrewrite; /* A list of non-rewritten domains */ + srs_bool alwaysrewrite; /* Rewrite even into same domain? */ + srs_bool noforward; /* Never perform forwards rewriting */ + srs_bool noreverse; /* Never perform reverse rewriting */ + char **neverrewrite; /* A list of non-rewritten domains */ } srs_t; /* Interface */ -int srs_set_malloc(srs_malloc_t m,srs_realloc_t r,srs_free_t f); +int srs_set_malloc(srs_malloc_t m, srs_realloc_t r, srs_free_t f); srs_t *srs_new(); void srs_init(srs_t *); void srs_free(srs_t *); -int srs_forward(srs_t *,char *,int,const char *,const char *); -int srs_forward_alloc(srs_t *,char **,const char *,const char *); -int srs_reverse(srs_t *,char *,int, const char *); -int srs_reverse_alloc(srs_t *,char **,const char *); +int srs_forward(srs_t *, char *, int, const char *, const char *); +int srs_forward_alloc(srs_t *, char **, const char *, const char *); +int srs_reverse(srs_t *, char *, int, const char *); +int srs_reverse_alloc(srs_t *, char **, const char *); const char *srs_strerror(int); -int srs_add_secret(srs_t *,const char *); -const char * srs_get_secret(srs_t *,int); - /* You probably shouldn't call these. */ -int srs_timestamp_create(srs_t *,char *,time_t); -int srs_timestamp_check(srs_t *,const char *); - -#define SRS_PARAM_DECLARE(n, t) \ - int srs_set_ ## n (srs_t *srs, t value); \ - t srs_get_ ## n (srs_t *srs); - -SRS_PARAM_DECLARE(alwaysrewrite,srs_bool) -SRS_PARAM_DECLARE(separator,char) -SRS_PARAM_DECLARE(maxage,int) -SRS_PARAM_DECLARE(hashlen,int) -SRS_PARAM_DECLARE(hashmin,int) -SRS_PARAM_DECLARE(noforward,srs_bool) -SRS_PARAM_DECLARE(noreverse,srs_bool) - -#endif /* SRS2_H */ +int srs_add_secret(srs_t *, const char *); +const char *srs_get_secret(srs_t *, int); +/* You probably shouldn't call these. */ +int srs_timestamp_create(srs_t *, char *, time_t); +int srs_timestamp_check(srs_t *, const char *); + +#define SRS_PARAM_DECLARE(n, t) \ + int srs_set_##n(srs_t *srs, t value); \ + t srs_get_##n(srs_t *srs); + +SRS_PARAM_DECLARE(alwaysrewrite, srs_bool) +SRS_PARAM_DECLARE(separator, char) +SRS_PARAM_DECLARE(maxage, int) +SRS_PARAM_DECLARE(hashlen, int) +SRS_PARAM_DECLARE(hashmin, int) +SRS_PARAM_DECLARE(noforward, srs_bool) +SRS_PARAM_DECLARE(noreverse, srs_bool) + +#endif /* SRS2_H */ diff --git a/src/include/strset.h b/src/include/strset.h index 4a5703e..e3a2e88 100644 --- a/src/include/strset.h +++ b/src/include/strset.h @@ -3,27 +3,23 @@ #include "uint_t.h" -typedef struct strset_list -{ +typedef struct strset_list { uint32 h; int next; -} -strset_list; +} strset_list; -typedef struct -{ - int mask; /* mask + 1 is power of 2, size of hash table */ - int n; /* number of entries used in list and x */ - int a; /* number of entries allocated in list and x */ - int *first; /* first[h] is front of hash list h */ +typedef struct { + int mask; /* mask + 1 is power of 2, size of hash table */ + int n; /* number of entries used in list and x */ + int a; /* number of entries allocated in list and x */ + int *first; /* first[h] is front of hash list h */ strset_list *p; /* p[i].next is next; p[i].h is hash of x[i] */ - char **x; /* x[i] is entry i */ -} -strset; + char **x; /* x[i] is entry i */ +} strset; extern uint32 strset_hash(char *); extern int strset_init(strset *); -extern char *strset_in(strset *,char *); -extern int strset_add(strset *,char *); +extern char *strset_in(strset *, char *); +extern int strset_add(strset *, char *); #endif diff --git a/src/include/tls_remote.h b/src/include/tls_remote.h index c3c7933..7df0776 100644 --- a/src/include/tls_remote.h +++ b/src/include/tls_remote.h @@ -5,7 +5,7 @@ /* the version is like this: 0xMNNFFPPS: major minor fix patch status */ #if OPENSSL_VERSION_NUMBER < 0x00908000L -# error "Need OpenSSL version at least 0.9.8" + #error "Need OpenSSL version at least 0.9.8" #endif extern char *tlsdestinfo; @@ -16,17 +16,17 @@ extern stralloc ciphers; int tls_domaincerts(const stralloc); int tls_destination(const stralloc); -int tlsa_check(const STACK_OF(X509) *,const stralloc,const unsigned long); -int tls_fingerprint(X509 *,const char *,const int); -int tls_chainfile(SSL_CTX *,const char *); -int tls_certkey(SSL_CTX *,const char *,const char *,char *); -int tls_conn(SSL *,int); -int tls_setup(int,char *,char *); -int tls_checkpeer(SSL *,X509 *,const stralloc,const int,const int); +int tlsa_check(const STACK_OF(X509) *, const stralloc, const unsigned long); +int tls_fingerprint(X509 *, const char *, const int); +int tls_chainfile(SSL_CTX *, const char *); +int tls_certkey(SSL_CTX *, const char *, const char *, char *); +int tls_conn(SSL *, int); +int tls_setup(int, char *, char *); +int tls_checkpeer(SSL *, X509 *, const stralloc, const int, const int); int tls_checkcrl(SSL *); int tls_error(void); int tls_exit(SSL *); -int utf8string(unsigned char *,int); +int utf8string(unsigned char *, int); #endif diff --git a/src/include/token822.h b/src/include/token822.h index 42c909f..e1cef4a 100644 --- a/src/include/token822.h +++ b/src/include/token822.h @@ -9,9 +9,9 @@ struct token822 { int slen; }; -GEN_ALLOC_typedef(token822_alloc,struct token822,t,len,a) +GEN_ALLOC_typedef(token822_alloc, struct token822, t, len, a) -int token822_parse(); + int token822_parse(); int token822_addrlist(); int token822_unquote(); int token822_unparse(); @@ -21,16 +21,16 @@ int token822_ready(); int token822_readyplus(); int token822_append(); -#define TOKEN822_ATOM 1 -#define TOKEN822_QUOTE 2 +#define TOKEN822_ATOM 1 +#define TOKEN822_QUOTE 2 #define TOKEN822_LITERAL 3 #define TOKEN822_COMMENT 4 -#define TOKEN822_LEFT 5 -#define TOKEN822_RIGHT 6 -#define TOKEN822_AT 7 -#define TOKEN822_COMMA 8 -#define TOKEN822_SEMI 9 -#define TOKEN822_COLON 10 -#define TOKEN822_DOT 11 +#define TOKEN822_LEFT 5 +#define TOKEN822_RIGHT 6 +#define TOKEN822_AT 7 +#define TOKEN822_COMMA 8 +#define TOKEN822_SEMI 9 +#define TOKEN822_COLON 10 +#define TOKEN822_DOT 11 #endif diff --git a/src/include/ucspitls.h b/src/include/ucspitls.h index 40f8a81..f0505c7 100644 --- a/src/include/ucspitls.h +++ b/src/include/ucspitls.h @@ -1,45 +1,46 @@ #ifndef UCSPITLS_H #define UCSPITLS_H -#include <openssl/ssl.h> -#include <openssl/opensslv.h> #include <openssl/ec.h> +#include <openssl/opensslv.h> +#include <openssl/ssl.h> + #include "stralloc.h" -#define SSL_NAME_LEN 256 +#define SSL_NAME_LEN 256 #define OPENSSL_VERSION_NUMBER 0x101010100L #if (OPENSSL_VERSION_NUMBER < 0x10100000L) -#define ssl_client() (ssl_context(SSLv23_client_method())) -#define ssl_server() (ssl_context(SSLv23_server_method())) + #define ssl_client() (ssl_context(SSLv23_client_method())) + #define ssl_server() (ssl_context(SSLv23_server_method())) #else -#define ssl_client() (ssl_context(TLS_client_method())) -#define ssl_server() (ssl_context(TLS_server_method())) + #define ssl_client() (ssl_context(TLS_client_method())) + #define ssl_server() (ssl_context(TLS_server_method())) #endif extern int ssl_errno; -int ssl_io(SSL *,int,int,unsigned int); +int ssl_io(SSL *, int, int, unsigned int); SSL_CTX *ssl_context(SSL_METHOD *); -int ssl_timeoutconn(SSL *,unsigned int); -int ssl_timeoutaccept(SSL *,unsigned int); -SSL *ssl_new(SSL_CTX *,int); -int ssl_certkey(SSL_CTX *,const char *,const char *,pem_password_cb *); -int ssl_ca(SSL_CTX *,const char *,const char *,int); -int ssl_cca(SSL_CTX *,const char *); -int ssl_ciphers(SSL_CTX *,const char *); -int ssl_verify(SSL *,const char *); -int ssl_params(SSL_CTX *,const char *,int); -int ssl_server_env(SSL *,stralloc *); -int ssl_client_env(SSL *,stralloc *); +int ssl_timeoutconn(SSL *, unsigned int); +int ssl_timeoutaccept(SSL *, unsigned int); +SSL *ssl_new(SSL_CTX *, int); +int ssl_certkey(SSL_CTX *, const char *, const char *, pem_password_cb *); +int ssl_ca(SSL_CTX *, const char *, const char *, int); +int ssl_cca(SSL_CTX *, const char *); +int ssl_ciphers(SSL_CTX *, const char *); +int ssl_verify(SSL *, const char *); +int ssl_params(SSL_CTX *, const char *, int); +int ssl_server_env(SSL *, stralloc *); +int ssl_client_env(SSL *, stralloc *); char *ssl_error_str(int); -#define ssl_errstr() (SSL_load_error_strings()) -#define ssl_free(ssl) (SSL_free((ssl))) +#define ssl_errstr() (SSL_load_error_strings()) +#define ssl_free(ssl) (SSL_free((ssl))) #define ssl_close(ssl) (close(SSL_get_fd((ssl)))) -#define ssl_pending(ssl) (SSL_pending((ssl))) -#define ssl_shutdown(ssl) (SSL_shutdown((ssl))) +#define ssl_pending(ssl) (SSL_pending((ssl))) +#define ssl_shutdown(ssl) (SSL_shutdown((ssl))) #define ssl_shutdown_pending(ssl) (SSL_get_shutdown((ssl)) & SSL_RECEIVED_SHUTDOWN) -#define ssl_shutdown_sent(ssl) (SSL_get_shutdown((ssl)) & SSL_SENT_SHUTDOWN) +#define ssl_shutdown_sent(ssl) (SSL_get_shutdown((ssl)) & SSL_SENT_SHUTDOWN) #endif diff --git a/src/include/wildmat.h b/src/include/wildmat.h index 501b7e4..99b275b 100644 --- a/src/include/wildmat.h +++ b/src/include/wildmat.h @@ -1,6 +1,6 @@ #ifndef WILDMAT_H #define WILDMAT_H -extern int wildmat(char *,char *); +extern int wildmat(char *, char *); #endif diff --git a/src/install.c b/src/install.c index c738fb7..e6f95a9 100644 --- a/src/install.c +++ b/src/install.c @@ -1,10 +1,11 @@ -#include <unistd.h> #include <sys/stat.h> +#include <unistd.h> + #include "buffer.h" -#include "logmsg.h" -#include "open.h" #include "exit.h" #include "fifo.h" +#include "logmsg.h" +#include "open.h" extern void hier(); @@ -12,41 +13,30 @@ extern void hier(); int fdsourcedir = -1; -void h(char *home,int uid,int gid,int mode) +void h(char *home, int uid, int gid, int mode) { - if (mkdir(home,0700) == -1) - if (errno != EEXIST) - logmsg(WHO,111,FATAL,B("unable to mkdir: ",home)); - if (chown(home,uid,gid) == -1) - logmsg(WHO,111,FATAL,B("unable to chown: ",home)); - if (chmod(home,mode) == -1) - logmsg(WHO,111,FATAL,B("unable to chmod: ",home)); + if (mkdir(home, 0700) == -1) + if (errno != EEXIST) logmsg(WHO, 111, FATAL, B("unable to mkdir: ", home)); + if (chown(home, uid, gid) == -1) logmsg(WHO, 111, FATAL, B("unable to chown: ", home)); + if (chmod(home, mode) == -1) logmsg(WHO, 111, FATAL, B("unable to chmod: ", home)); } -void d(char *home,char *subdir,int uid,int gid,int mode) +void d(char *home, char *subdir, int uid, int gid, int mode) { - if (chdir(home) == -1) - logmsg(WHO,110,FATAL,B("unable to switch to: ",home)); - if (mkdir(subdir,0700) == -1) - if (errno != EEXIST) - logmsg(WHO,111,FATAL,B("unable to mkdir: ",home,"/",subdir)); - if (chown(subdir,uid,gid) == -1) - logmsg(WHO,111,FATAL,B("unable to chown: ",home,"/",subdir)); - if (chmod(subdir,mode) == -1) - logmsg(WHO,111,FATAL,B("unable to chmod: ",home,"/",subdir)); + if (chdir(home) == -1) logmsg(WHO, 110, FATAL, B("unable to switch to: ", home)); + if (mkdir(subdir, 0700) == -1) + if (errno != EEXIST) logmsg(WHO, 111, FATAL, B("unable to mkdir: ", home, "/", subdir)); + if (chown(subdir, uid, gid) == -1) logmsg(WHO, 111, FATAL, B("unable to chown: ", home, "/", subdir)); + if (chmod(subdir, mode) == -1) logmsg(WHO, 111, FATAL, B("unable to chmod: ", home, "/", subdir)); } -void p(char *home,char *fifo,int uid,int gid,int mode) +void p(char *home, char *fifo, int uid, int gid, int mode) { - if (chdir(home) == -1) - logmsg(WHO,110,FATAL,B("unable to switch to: ",home)); - if (fifo_make(fifo,0700) == -1) - if (errno != EEXIST) - logmsg(WHO,111,FATAL,B("unable to mkfifo: ",home,"/",fifo)); - if (chown(fifo,uid,gid) == -1) - logmsg(WHO,111,FATAL,B("unable to chown: ",home,"/",fifo)); - if (chmod(fifo,mode) == -1) - logmsg(WHO,111,FATAL,B("unable to chmod: ",home,"/",fifo)); + if (chdir(home) == -1) logmsg(WHO, 110, FATAL, B("unable to switch to: ", home)); + if (fifo_make(fifo, 0700) == -1) + if (errno != EEXIST) logmsg(WHO, 111, FATAL, B("unable to mkfifo: ", home, "/", fifo)); + if (chown(fifo, uid, gid) == -1) logmsg(WHO, 111, FATAL, B("unable to chown: ", home, "/", fifo)); + if (chmod(fifo, mode) == -1) logmsg(WHO, 111, FATAL, B("unable to chmod: ", home, "/", fifo)); } char inbuf[BUFFER_INSIZE]; @@ -54,84 +44,65 @@ buffer bi; char outbuf[BUFFER_OUTSIZE]; buffer bo; -void c(char *home,char *subdir,char *file,int uid,int gid,int mode) +void c(char *home, char *subdir, char *file, int uid, int gid, int mode) { int fdin; int fdout; - if (fchdir(fdsourcedir) == -1) - logmsg(WHO,110,FATAL,"unable to switch back to source directory: "); + if (fchdir(fdsourcedir) == -1) logmsg(WHO, 110, FATAL, "unable to switch back to source directory: "); fdin = open_read(file); - if (fdin == -1) - logmsg(WHO,111,FATAL,B("unable to read: ",file)); - buffer_init(&bi,read,fdin,inbuf,sizeof(inbuf)); + if (fdin == -1) logmsg(WHO, 111, FATAL, B("unable to read: ", file)); + buffer_init(&bi, read, fdin, inbuf, sizeof(inbuf)); - if (chdir(home) == -1) - logmsg(WHO,110,FATAL,B("unable to switch to: ",home)); - if (chdir(subdir) == -1) - logmsg(WHO,110,FATAL,B("unable to switch to: ",home,"/",subdir)); + if (chdir(home) == -1) logmsg(WHO, 110, FATAL, B("unable to switch to: ", home)); + if (chdir(subdir) == -1) logmsg(WHO, 110, FATAL, B("unable to switch to: ", home, "/", subdir)); fdout = open_trunc(file); - if (fdout == -1) - logmsg(WHO,111,FATAL,B("unable to write .../",subdir,"/",file)); - buffer_init(&bo,write,fdout,outbuf,sizeof(outbuf)); - - switch (buffer_copy(&bo,&bi)) { - case -2: - logmsg(WHO,111,FATAL,B("unable to read: ",file)); - case -3: - logmsg(WHO,111,FATAL,B("unable to write .../",subdir,"/",file)); + if (fdout == -1) logmsg(WHO, 111, FATAL, B("unable to write .../", subdir, "/", file)); + buffer_init(&bo, write, fdout, outbuf, sizeof(outbuf)); + + switch (buffer_copy(&bo, &bi)) { + case -2: logmsg(WHO, 111, FATAL, B("unable to read: ", file)); + case -3: logmsg(WHO, 111, FATAL, B("unable to write .../", subdir, "/", file)); } close(fdin); - if (buffer_flush(&bo) == -1) - logmsg(WHO,111,FATAL,B("unable to write .../",subdir,"/",file)); - if (fsync(fdout) == -1) - logmsg(WHO,111,FATAL,B("unable to write .../",subdir,"/",file)); + if (buffer_flush(&bo) == -1) logmsg(WHO, 111, FATAL, B("unable to write .../", subdir, "/", file)); + if (fsync(fdout) == -1) logmsg(WHO, 111, FATAL, B("unable to write .../", subdir, "/", file)); if (close(fdout) == -1) /* NFS silliness */ - logmsg(WHO,111,FATAL,B("unable to write .../",subdir,"/",file)); + logmsg(WHO, 111, FATAL, B("unable to write .../", subdir, "/", file)); - if (chown(file,uid,gid) == -1) - logmsg(WHO,111,FATAL,B("unable to chown .../",subdir,"/",file)); - if (chmod(file,mode) == -1) - logmsg(WHO,111,FATAL,B("unable to chmod .../",subdir,"/",file)); + if (chown(file, uid, gid) == -1) logmsg(WHO, 111, FATAL, B("unable to chown .../", subdir, "/", file)); + if (chmod(file, mode) == -1) logmsg(WHO, 111, FATAL, B("unable to chmod .../", subdir, "/", file)); } -void z(char *home,char *file,int len,int uid,int gid,int mode) +void z(char *home, char *file, int len, int uid, int gid, int mode) { int fdout; - if (chdir(home) == -1) - logmsg(WHO,110,FATAL,B("unable to switch to: ",home)); + if (chdir(home) == -1) logmsg(WHO, 110, FATAL, B("unable to switch to: ", home)); fdout = open_trunc(file); - if (fdout == -1) - logmsg(WHO,111,FATAL,B("unable to write: ",home,"/",file)); - buffer_init(&bo,write,fdout,outbuf,sizeof(outbuf)); + if (fdout == -1) logmsg(WHO, 111, FATAL, B("unable to write: ", home, "/", file)); + buffer_init(&bo, write, fdout, outbuf, sizeof(outbuf)); while (len-- > 0) - if (buffer_put(&bo,"",1) == -1) - logmsg(WHO,111,FATAL,B("unable to write: ",home,"/",file)); + if (buffer_put(&bo, "", 1) == -1) logmsg(WHO, 111, FATAL, B("unable to write: ", home, "/", file)); - if (buffer_flush(&bo) == -1) - logmsg(WHO,111,FATAL,B("unable to write: ",home,"/",file)); - if (fsync(fdout) == -1) - logmsg(WHO,111,FATAL,B("unable to write: ",home,"/",file)); + if (buffer_flush(&bo) == -1) logmsg(WHO, 111, FATAL, B("unable to write: ", home, "/", file)); + if (fsync(fdout) == -1) logmsg(WHO, 111, FATAL, B("unable to write: ", home, "/", file)); if (close(fdout) == -1) /* NFS silliness */ - logmsg(WHO,111,FATAL,B("unable to write: ",home,"/",file)); + logmsg(WHO, 111, FATAL, B("unable to write: ", home, "/", file)); - if (chown(file,uid,gid) == -1) - logmsg(WHO,111,FATAL,B("unable to chown: ",home,"/",file)); - if (chmod(file,mode) == -1) - logmsg(WHO,111,FATAL,B("unable to chmod: ",home,"/",file)); + if (chown(file, uid, gid) == -1) logmsg(WHO, 111, FATAL, B("unable to chown: ", home, "/", file)); + if (chmod(file, mode) == -1) logmsg(WHO, 111, FATAL, B("unable to chmod: ", home, "/", file)); } int main() { fdsourcedir = open_read("."); - if (fdsourcedir == -1) - logmsg(WHO,110,FATAL,"unable to open current directory: "); + if (fdsourcedir == -1) logmsg(WHO, 110, FATAL, "unable to open current directory: "); umask(077); hier(); diff --git a/src/instcheck.c b/src/instcheck.c index e47da87..126ad11 100644 --- a/src/instcheck.c +++ b/src/instcheck.c @@ -1,69 +1,66 @@ -#include <sys/types.h> #include <sys/stat.h> +#include <sys/types.h> #include <unistd.h> -#include "logmsg.h" + #include "exit.h" +#include "logmsg.h" + #include "hier.h" extern void hier(); #define WHO "instcheck" -void perm(char *prefix1,char *prefix2,char *prefix3,char *file,int type,int uid,int gid,int mode) +void perm(char *prefix1, char *prefix2, char *prefix3, char *file, int type, int uid, int gid, int mode) { struct stat st; - if (stat(file,&st) == -1) { + if (stat(file, &st) == -1) { if (errno == ENOENT) - logmsg(WHO,0,WARN,B("file does nost exist:",prefix1,prefix2,prefix3,file)); + logmsg(WHO, 0, WARN, B("file does nost exist:", prefix1, prefix2, prefix3, file)); else - logmsg(WHO,errno,WARN,B("unable to stat: ../",file)); + logmsg(WHO, errno, WARN, B("unable to stat: ../", file)); return; } if ((uid != -1) && (st.st_uid != uid)) - logmsg(WHO,0,WARN,B("file has wrong owner: ",prefix1,prefix2,prefix3,file)); + logmsg(WHO, 0, WARN, B("file has wrong owner: ", prefix1, prefix2, prefix3, file)); if ((gid != -1) && (st.st_gid != gid)) - logmsg(WHO,0,WARN,B("file has wrong group: ",prefix1,prefix2,prefix3,file)); + logmsg(WHO, 0, WARN, B("file has wrong group: ", prefix1, prefix2, prefix3, file)); if ((st.st_mode & 07777) != mode) - logmsg(WHO,0,WARN,B("file has wrong permissions: ",prefix1,prefix2,prefix3,file)); + logmsg(WHO, 0, WARN, B("file has wrong permissions: ", prefix1, prefix2, prefix3, file)); if ((st.st_mode & S_IFMT) != type) - logmsg(WHO,0,WARN,B("file has wrong type: ",prefix1,prefix2,prefix3,file)); + logmsg(WHO, 0, WARN, B("file has wrong type: ", prefix1, prefix2, prefix3, file)); } -void h(char *home,int uid,int gid,int mode) +void h(char *home, int uid, int gid, int mode) { - perm("","","",home,S_IFDIR,uid,gid,mode); + perm("", "", "", home, S_IFDIR, uid, gid, mode); } -void d(char *home,char *subdir,int uid,int gid,int mode) +void d(char *home, char *subdir, int uid, int gid, int mode) { - if (chdir(home) == -1) - logmsg(WHO,111,FATAL,B("unable to switch to: ",home)); - perm("",home,"/",subdir,S_IFDIR,uid,gid,mode); + if (chdir(home) == -1) logmsg(WHO, 111, FATAL, B("unable to switch to: ", home)); + perm("", home, "/", subdir, S_IFDIR, uid, gid, mode); } -void p(char *home,char *fifo,int uid,int gid,int mode) +void p(char *home, char *fifo, int uid, int gid, int mode) { - if (chdir(home) == -1) - logmsg(WHO,111,FATAL,B("unable to switch to: ",home)); - perm("",home,"/",fifo,S_IFIFO,uid,gid,mode); + if (chdir(home) == -1) logmsg(WHO, 111, FATAL, B("unable to switch to: ", home)); + perm("", home, "/", fifo, S_IFIFO, uid, gid, mode); } -void c(char *home,char *subdir,char *file,int uid,int gid,int mode) +void c(char *home, char *subdir, char *file, int uid, int gid, int mode) { - if (chdir(home) == -1) - logmsg(WHO,111,FATAL,B("unable to switch to: ",home)); - if (chdir(subdir) == -1) - logmsg(WHO,111,FATAL,B("unable to switch to: ",home,"/",subdir)); - perm(".../",subdir,"/",file,S_IFREG,uid,gid,mode); + if (chdir(home) == -1) logmsg(WHO, 111, FATAL, B("unable to switch to: ", home)); + if (chdir(subdir) == -1) logmsg(WHO, 111, FATAL, B("unable to switch to: ", home, "/", subdir)); + perm(".../", subdir, "/", file, S_IFREG, uid, gid, mode); } -void z(char *home,char *file,int len,int uid,int gid,int mode) +void z(char *home, char *file, int len, int uid, int gid, int mode) { - if (chdir(home) == -1) - logmsg(WHO,111,FATAL,B("unable to switch to: ",home)); - perm("",home,"/",file,S_IFREG,uid,gid,mode); + if (chdir(home) == -1) logmsg(WHO, 111, FATAL, B("unable to switch to: ", home)); + perm("", home, "/", file, S_IFREG, uid, gid, mode); } int main() diff --git a/src/ipalloc.c b/src/ipalloc.c index 390bd7b..c90a1d3 100644 --- a/src/ipalloc.c +++ b/src/ipalloc.c @@ -1,7 +1,8 @@ -#include "alloc.h" -#include "ip.h" #include "ipalloc.h" + +#include "alloc.h" #include "genalloc.h" +#include "ip.h" -GEN_ALLOC_readyplus(ipalloc,struct ip_mx,ix,len,a,i,n,x,22,ipalloc_readyplus) -GEN_ALLOC_append(ipalloc,struct ip_mx,ix,len,a,i,n,x,22,ipalloc_readyplus,ipalloc_append) +GEN_ALLOC_readyplus(ipalloc, struct ip_mx, ix, len, a, i, n, x, 22, ipalloc_readyplus) + GEN_ALLOC_append(ipalloc, struct ip_mx, ix, len, a, i, n, x, 22, ipalloc_readyplus, ipalloc_append) @@ -1,19 +1,21 @@ -#include <sys/types.h> -#include <sys/param.h> -#include <sys/time.h> -#include <sys/ioctl.h> -#include <sys/socket.h> +#include <ifaddrs.h> + #include <net/if.h> #include <netinet/in.h> -#include <ifaddrs.h> +#include <sys/ioctl.h> +#include <sys/param.h> +#include <sys/socket.h> +#include <sys/time.h> +#include <sys/types.h> #ifndef SIOCGIFCONF /* whatever works */ -#include <sys/sockio.h> + #include <sys/sockio.h> #endif -#include "hassalen.h" #include "byte.h" #include "ip.h" -#include "ipalloc.h" #include "stralloc.h" + +#include "hassalen.h" +#include "ipalloc.h" #include "ipme.h" /** @file ipme.c @@ -30,8 +32,7 @@ int ipme_is4(struct ip4_address *ip) if (ipme_init() != 1) return -1; for (i = 0; i < ipme.len; ++i) - if (ipme.ix[i].af == AF_INET && byte_equal(&ipme.ix[i].addr,4,ip)) - return 1; + if (ipme.ix[i].af == AF_INET && byte_equal(&ipme.ix[i].addr, 4, ip)) return 1; return 0; } @@ -42,8 +43,7 @@ int ipme_is6(struct ip6_address *ip) if (ipme_init() != 1) return -1; for (i = 0; i < ipme.len; ++i) - if (ipme.ix[i].af == AF_INET6 && byte_equal(&ipme.ix[i].addr,16,ip)) - return 1; + if (ipme.ix[i].af == AF_INET6 && byte_equal(&ipme.ix[i].addr, 16, ip)) return 1; return 0; } @@ -66,26 +66,26 @@ int ipme_init() struct ip_mx ix; if (ipmeok) return 1; - if (!ipalloc_readyplus(&ipme,0)) return 0; + if (!ipalloc_readyplus(&ipme, 0)) return 0; ipme.len = 0; ix.pref = 0; if (getifaddrs(&ifap)) return 0; - for (ifa = ifap; ifa; ifa = ifa->ifa_next) + for (ifa = ifap; ifa; ifa = ifa->ifa_next) if (ifa->ifa_addr) { if (ifa->ifa_addr->sa_family == AF_INET) { - sin = (struct sockaddr_in *) ifa->ifa_addr; - byte_copy(&ix.addr.ip4,4,&sin->sin_addr); + sin = (struct sockaddr_in *)ifa->ifa_addr; + byte_copy(&ix.addr.ip4, 4, &sin->sin_addr); ix.af = AF_INET; - if (!ipalloc_append(&ipme,&ix)) return 0; + if (!ipalloc_append(&ipme, &ix)) return 0; } if (ifa->ifa_addr->sa_family == AF_INET6) { - sin6 = (struct sockaddr_in6 *) ifa->ifa_addr; - byte_copy(&ix.addr.ip6,16,&sin6->sin6_addr); + sin6 = (struct sockaddr_in6 *)ifa->ifa_addr; + byte_copy(&ix.addr.ip6, 16, &sin6->sin6_addr); ix.af = AF_INET6; - if (!ipalloc_append(&ipme,&ix)) return 0; - } + if (!ipalloc_append(&ipme, &ix)) return 0; + } } freeifaddrs(ifap); diff --git a/src/ipmeprint.c b/src/ipmeprint.c index 6379219..9c92138 100644 --- a/src/ipmeprint.c +++ b/src/ipmeprint.c @@ -1,39 +1,37 @@ -#include <sys/types.h> -#include <sys/socket.h> #include <netinet/in.h> +#include <sys/socket.h> +#include <sys/types.h> #include <unistd.h> + #include "buffer.h" -#include "ip.h" -#include "ipme.h" #include "exit.h" #include "fmt.h" +#include "ip.h" + +#include "ipme.h" char ipaddr[IPFMT]; int main() { - int j; + int j; - switch (ipme_init()) { - case 0: buffer_putsflush(buffer_2,"out of memory\n"); _exit(111); - case -1: buffer_putsflush(buffer_2,"hard error\n"); _exit(100); - } + switch (ipme_init()) { + case 0: buffer_putsflush(buffer_2, "out of memory\n"); _exit(111); + case -1: buffer_putsflush(buffer_2, "hard error\n"); _exit(100); + } - for (j = 0;j < ipme.len;++j) { - switch (ipme.ix[j].af) { - case AF_INET: - buffer_put(buffer_1,ipaddr,ip4_fmt(ipaddr,&ipme.ix[j].addr.ip4.d)); - break; - case AF_INET6: - buffer_put(buffer_1,ipaddr,ip6_fmt(ipaddr,&ipme.ix[j].addr.ip6.d)); - break; - default: - buffer_puts(buffer_1,"Unknown address family = "); - buffer_put(buffer_1,ipaddr,fmt_ulong(ipaddr,ipme.ix[j].af)); - } - buffer_puts(buffer_1,"\n"); - } + for (j = 0; j < ipme.len; ++j) { + switch (ipme.ix[j].af) { + case AF_INET: buffer_put(buffer_1, ipaddr, ip4_fmt(ipaddr, &ipme.ix[j].addr.ip4.d)); break; + case AF_INET6: buffer_put(buffer_1, ipaddr, ip6_fmt(ipaddr, &ipme.ix[j].addr.ip6.d)); break; + default: + buffer_puts(buffer_1, "Unknown address family = "); + buffer_put(buffer_1, ipaddr, fmt_ulong(ipaddr, ipme.ix[j].af)); + } + buffer_puts(buffer_1, "\n"); + } - buffer_flush(buffer_1); - _exit(0); + buffer_flush(buffer_1); + _exit(0); } diff --git a/src/maildir.c b/src/maildir.c index 8832d75..c360bc2 100644 --- a/src/maildir.c +++ b/src/maildir.c @@ -1,15 +1,18 @@ -#include <unistd.h> -#include <sys/types.h> +#include "maildir.h" + #include <sys/stat.h> -#include "prioq.h" +#include <sys/types.h> +#include <unistd.h> + +#include "direntry.h" #include "env.h" +#include "logmsg.h" +#include "str.h" #include "stralloc.h" -#include "direntry.h" + #include "datetime.h" #include "now.h" -#include "str.h" -#include "maildir.h" -#include "logmsg.h" +#include "prioq.h" #define WHO "maildir" @@ -17,10 +20,8 @@ int maildir_chdir() { char *maildir; maildir = env_get("MAILDIR"); - if (!maildir) - logmsg(WHO,111,ERROR,"MAILDIR not set"); - if (chdir(maildir) == -1) - logmsg(WHO,110,FATAL,B("unable to chdir to: ",maildir)); + if (!maildir) logmsg(WHO, 111, ERROR, "MAILDIR not set"); + if (chdir(maildir) == -1) logmsg(WHO, 110, FATAL, B("unable to chdir to: ", maildir)); return 0; } @@ -37,13 +38,12 @@ void maildir_clean(stralloc *tmpname) if (!dir) return; while ((d = readdir(dir))) { - if (d->d_name[0] == '.') continue; - if (!stralloc_copys(tmpname,"tmp/")) break; - if (!stralloc_cats(tmpname,d->d_name)) break; - if (!stralloc_0(tmpname)) break; - if (stat(tmpname->s,&st) == 0) - if (time > st.st_atime + 129600) - unlink(tmpname->s); + if (d->d_name[0] == '.') continue; + if (!stralloc_copys(tmpname, "tmp/")) break; + if (!stralloc_cats(tmpname, d->d_name)) break; + if (!stralloc_0(tmpname)) break; + if (stat(tmpname->s, &st) == 0) + if (time > st.st_atime + 129600) unlink(tmpname->s); } closedir(dir); } @@ -57,26 +57,25 @@ static int append(prioq *pq, stralloc *filenames, char *subdir, datetime_sec tim struct stat st; dir = opendir(subdir); - if (!dir) - logmsg(WHO,112,FATAL,B("unable to scan $MAILDIR/:",subdir)); + if (!dir) logmsg(WHO, 112, FATAL, B("unable to scan $MAILDIR/:", subdir)); while ((d = readdir(dir))) { if (d->d_name[0] == '.') continue; pos = filenames->len; - if (!stralloc_cats(filenames,subdir)) break; - if (!stralloc_cats(filenames,"/")) break; - if (!stralloc_cats(filenames,d->d_name)) break; + if (!stralloc_cats(filenames, subdir)) break; + if (!stralloc_cats(filenames, "/")) break; + if (!stralloc_cats(filenames, d->d_name)) break; if (!stralloc_0(filenames)) break; - if (stat(filenames->s + pos,&st) == 0) + if (stat(filenames->s + pos, &st) == 0) if (st.st_mtime < time) { /* don't want to mix up the order */ pe.dt = st.st_mtime; pe.id = pos; - if (!prioq_insert(pq,&pe)) break; + if (!prioq_insert(pq, &pe)) break; } } closedir(dir); - if (d) logmsg(WHO,112,FATAL,B("unable to read $MAILDIR/:",subdir)); + if (d) logmsg(WHO, 112, FATAL, B("unable to read $MAILDIR/:", subdir)); return 0; } @@ -85,13 +84,14 @@ int maildir_scan(prioq *pq, stralloc *filenames, int flagnew, int flagcur) struct prioq_elt pe; datetime_sec time; - if (!stralloc_copys(filenames,"")) return 0; - while (prioq_min(pq,&pe)) - prioq_delmin(pq); + if (!stralloc_copys(filenames, "")) return 0; + while (prioq_min(pq, &pe)) prioq_delmin(pq); time = now(); - if (flagnew) if (append(pq,filenames,"new",time) == -1) return -1; - if (flagcur) if (append(pq,filenames,"cur",time) == -1) return -1; + if (flagnew) + if (append(pq, filenames, "new", time) == -1) return -1; + if (flagcur) + if (append(pq, filenames, "cur", time) == -1) return -1; return 0; } diff --git a/src/maildir2mbox.c b/src/maildir2mbox.c index ba187e7..5e31951 100644 --- a/src/maildir2mbox.c +++ b/src/maildir2mbox.c @@ -1,24 +1,26 @@ -#include <unistd.h> #include <sys/stat.h> +#include <unistd.h> + +#include "buffer.h" #include "env.h" +#include "exit.h" #include "genalloc.h" -#include "stralloc.h" -#include "buffer.h" #include "getln.h" +#include "lock.h" #include "logmsg.h" #include "open.h" -#include "lock.h" -#include "gfrom.h" #include "str.h" -#include "exit.h" -#include "myctime.h" +#include "stralloc.h" + +#include "gfrom.h" #include "maildir.h" +#include "myctime.h" #include "prioq.h" char *mbox; char *mboxtmp; -int rename(const char *,const char *); // stdio.h +int rename(const char *, const char *); // stdio.h stralloc filenames = {0}; prioq pq = {0}; @@ -33,124 +35,118 @@ char outbuf[BUFFER_OUTSIZE]; #define WHO "maildir2mbox" -void die_nomem() { logmsg(WHO,111,FATAL,"out of memory"); } +void die_nomem() +{ + logmsg(WHO, 111, FATAL, "out of memory"); +} int main() { - buffer bi; - buffer bo; - struct prioq_elt pe; - int fdoldmbox; - int fdnewmbox; - int fd; - int match; - int fdlock; - - umask(077); - - mbox = env_get("MAIL"); - if (!mbox) logmsg(WHO,111,FATAL,"MAIL not set"); - mboxtmp = env_get("MAILTMP"); - if (!mboxtmp) logmsg(WHO,111,FATAL,"MAILTMP not set"); - - if (maildir_chdir() == -1) - logmsg(WHO,110,FATAL,"Can't changet maildir"); - maildir_clean(&filenames); - if (maildir_scan(&pq,&filenames,1,1) == -1) - logmsg(WHO,112,FATAL,"Can't read maidir"); - - if (!prioq_min(&pq,&pe)) _exit(0); /* nothing new */ - - fdlock = open_append(mbox); - if (fdlock == -1) - logmsg(WHO,111,FATAL,B("unable to lock: ",mbox)); - if (lock_ex(fdlock) == -1) - logmsg(WHO,111,FATAL,B("unable to lock: ",mbox)); - - fdoldmbox = open_read(mbox); - if (fdoldmbox == -1) - logmsg(WHO,112,FATAL,B("unable to read: ",mbox)); - - fdnewmbox = open_trunc(mboxtmp); - if (fdnewmbox == -1) - logmsg(WHO,112,FATAL,B("unable to create: ",mboxtmp)); - - buffer_init(&bi,read,fdoldmbox,inbuf,sizeof(inbuf)); - buffer_init(&bo,write,fdnewmbox,outbuf,sizeof(outbuf)); - - switch (buffer_copy(&bo,&bi)) { - case -2: logmsg(WHO,112,FATAL,B("unable to read: ",mbox)); - case -3: logmsg(WHO,112,FATAL,B("unable to write to: ",mboxtmp)); - } - - while (prioq_min(&pq,&pe)) { - prioq_delmin(&pq); - if (!prioq_insert(&pq2,&pe)) die_nomem(); - - fd = open_read(filenames.s + pe.id); - if (fd == -1) - logmsg(WHO,112,FATAL,B("unable to read: $MAILDIR/",filenames.s + pe.id)); - buffer_init(&bi,read,fd,inbuf,sizeof(inbuf)); - - if (getln(&bi,&line,&match,'\n') != 0) - logmsg(WHO,112,FATAL,B("unable to read: $MAILDIR/",filenames.s + pe.id)); - - if (!stralloc_copys(&ufline,"From XXX ")) die_nomem(); - if (match) - if (stralloc_starts(&line,"Return-Path: <")) { - if (line.s[14] == '>') { - if (!stralloc_copys(&ufline,"From MAILER-DAEMON ")) die_nomem(); - } else { - int i; - if (!stralloc_ready(&ufline,line.len)) die_nomem(); - if (!stralloc_copys(&ufline,"From ")) die_nomem(); - - for (i = 14;i < line.len - 2;++i) - if ((line.s[i] == ' ') || (line.s[i] == '\t')) - ufline.s[ufline.len++] = '-'; - else - ufline.s[ufline.len++] = line.s[i]; - if (!stralloc_cats(&ufline," ")) die_nomem(); - } + buffer bi; + buffer bo; + struct prioq_elt pe; + int fdoldmbox; + int fdnewmbox; + int fd; + int match; + int fdlock; + + umask(077); + + mbox = env_get("MAIL"); + if (!mbox) logmsg(WHO, 111, FATAL, "MAIL not set"); + mboxtmp = env_get("MAILTMP"); + if (!mboxtmp) logmsg(WHO, 111, FATAL, "MAILTMP not set"); + + if (maildir_chdir() == -1) logmsg(WHO, 110, FATAL, "Can't changet maildir"); + maildir_clean(&filenames); + if (maildir_scan(&pq, &filenames, 1, 1) == -1) logmsg(WHO, 112, FATAL, "Can't read maidir"); + + if (!prioq_min(&pq, &pe)) _exit(0); /* nothing new */ + + fdlock = open_append(mbox); + if (fdlock == -1) logmsg(WHO, 111, FATAL, B("unable to lock: ", mbox)); + if (lock_ex(fdlock) == -1) logmsg(WHO, 111, FATAL, B("unable to lock: ", mbox)); + + fdoldmbox = open_read(mbox); + if (fdoldmbox == -1) logmsg(WHO, 112, FATAL, B("unable to read: ", mbox)); + + fdnewmbox = open_trunc(mboxtmp); + if (fdnewmbox == -1) logmsg(WHO, 112, FATAL, B("unable to create: ", mboxtmp)); + + buffer_init(&bi, read, fdoldmbox, inbuf, sizeof(inbuf)); + buffer_init(&bo, write, fdnewmbox, outbuf, sizeof(outbuf)); + + switch (buffer_copy(&bo, &bi)) { + case -2: logmsg(WHO, 112, FATAL, B("unable to read: ", mbox)); + case -3: logmsg(WHO, 112, FATAL, B("unable to write to: ", mboxtmp)); + } + + while (prioq_min(&pq, &pe)) { + prioq_delmin(&pq); + if (!prioq_insert(&pq2, &pe)) die_nomem(); + + fd = open_read(filenames.s + pe.id); + if (fd == -1) logmsg(WHO, 112, FATAL, B("unable to read: $MAILDIR/", filenames.s + pe.id)); + buffer_init(&bi, read, fd, inbuf, sizeof(inbuf)); + + if (getln(&bi, &line, &match, '\n') != 0) + logmsg(WHO, 112, FATAL, B("unable to read: $MAILDIR/", filenames.s + pe.id)); + + if (!stralloc_copys(&ufline, "From XXX ")) die_nomem(); + if (match) + if (stralloc_starts(&line, "Return-Path: <")) { + if (line.s[14] == '>') { + if (!stralloc_copys(&ufline, "From MAILER-DAEMON ")) die_nomem(); + } else { + int i; + if (!stralloc_ready(&ufline, line.len)) die_nomem(); + if (!stralloc_copys(&ufline, "From ")) die_nomem(); + + for (i = 14; i < line.len - 2; ++i) + if ((line.s[i] == ' ') || (line.s[i] == '\t')) + ufline.s[ufline.len++] = '-'; + else + ufline.s[ufline.len++] = line.s[i]; + if (!stralloc_cats(&ufline, " ")) die_nomem(); + } + } + if (!stralloc_cats(&ufline, myctime(pe.dt))) die_nomem(); + if (buffer_put(&bo, ufline.s, ufline.len) == -1) + logmsg(WHO, 112, FATAL, B("unable to write to: ", mboxtmp)); + + while (match && line.len) { + if (gfrom(line.s, line.len)) + if (buffer_puts(&bo, ">") == -1) logmsg(WHO, 112, FATAL, B("unable to write to: ", mboxtmp)); + if (buffer_put(&bo, line.s, line.len) == -1) + logmsg(WHO, 112, FATAL, B("unable to write to: ", mboxtmp)); + if (!match) { + if (buffer_puts(&bo, "\n") == -1) logmsg(WHO, 112, FATAL, B("unable to write to: ", mboxtmp)); + break; } - if (!stralloc_cats(&ufline,myctime(pe.dt))) die_nomem(); - if (buffer_put(&bo,ufline.s,ufline.len) == -1) - logmsg(WHO,112,FATAL,B("unable to write to: ",mboxtmp)); - - while (match && line.len) { - if (gfrom(line.s,line.len)) - if (buffer_puts(&bo,">") == -1) - logmsg(WHO,112,FATAL,B("unable to write to: ",mboxtmp)); - if (buffer_put(&bo,line.s,line.len) == -1) - logmsg(WHO,112,FATAL,B("unable to write to: ",mboxtmp)); - if (!match) { - if (buffer_puts(&bo,"\n") == -1) - logmsg(WHO,112,FATAL,B("unable to write to: ",mboxtmp)); - break; - } - if (getln(&bi,&line,&match,'\n') != 0) - logmsg(WHO,112,FATAL,B("unable to read: $MAILDIR/",filenames.s + pe.id)); - } - if (buffer_puts(&bo,"\n")) - logmsg(WHO,112,FATAL,B("unable to write to: ",mboxtmp)); - - close(fd); - } - - if (buffer_flush(&bo) == -1) - logmsg(WHO,112,FATAL,B("unable to write to: ",mboxtmp)); - if (fsync(fdnewmbox) == -1) - logmsg(WHO,112,FATAL,B("unable to write to: ",mboxtmp)); - if (close(fdnewmbox) == -1) /* NFS dorks */ - logmsg(WHO,112,FATAL,B("unable to write to: ",mboxtmp)); - if (rename(mboxtmp,mbox) == -1) - logmsg(WHO,112,FATAL,B("unable to move ",mboxtmp," to: ",mbox)); - - while (prioq_min(&pq2,&pe)) { - prioq_delmin(&pq2); - if (unlink(filenames.s + pe.id) == -1) - logmsg(WHO,0,WARN,B("$MAILDIR/",filenames.s + pe.id," will be delivered twice; unable to unlink")); - } - - _exit(0); + if (getln(&bi, &line, &match, '\n') != 0) + logmsg(WHO, 112, FATAL, B("unable to read: $MAILDIR/", filenames.s + pe.id)); + } + if (buffer_puts(&bo, "\n")) logmsg(WHO, 112, FATAL, B("unable to write to: ", mboxtmp)); + + close(fd); + } + + if (buffer_flush(&bo) == -1) logmsg(WHO, 112, FATAL, B("unable to write to: ", mboxtmp)); + if (fsync(fdnewmbox) == -1) logmsg(WHO, 112, FATAL, B("unable to write to: ", mboxtmp)); + if (close(fdnewmbox) == -1) /* NFS dorks */ + logmsg(WHO, 112, FATAL, B("unable to write to: ", mboxtmp)); + if (rename(mboxtmp, mbox) == -1) logmsg(WHO, 112, FATAL, B("unable to move ", mboxtmp, " to: ", mbox)); + + while (prioq_min(&pq2, &pe)) { + prioq_delmin(&pq2); + if (unlink(filenames.s + pe.id) == -1) + logmsg( + WHO, + 0, + WARN, + B("$MAILDIR/", filenames.s + pe.id, " will be delivered twice; unable to unlink")); + } + + _exit(0); } diff --git a/src/maildirmake.c b/src/maildirmake.c index 47edc44..6828df9 100644 --- a/src/maildirmake.c +++ b/src/maildirmake.c @@ -1,24 +1,19 @@ #include <sys/stat.h> #include <unistd.h> -#include "logmsg.h" + #include "exit.h" +#include "logmsg.h" #define WHO "maildirmake" int main(int argc, char **argv) { umask(077); - if (!argv[1]) - logmsg(WHO,100,USAGE,"maildirmake name"); - if (mkdir(argv[1],0700) == -1) - logmsg(WHO,111,FATAL,B("unable to mkdir: ",argv[1])); - if (chdir(argv[1]) == -1) - logmsg(WHO,111,FATAL,B("unable to chdir to: ",argv[1])); - if (mkdir("tmp",0700) == -1) - logmsg(WHO,111,FATAL,B("unable to mkdir: ",argv[1],"/tmp")); - if (mkdir("new",0700) == -1) - logmsg(WHO,111,FATAL,B("unable to mkdir: ",argv[1],"/new")); - if (mkdir("cur",0700) == -1) - logmsg(WHO,111,FATAL,B("unable to mkdir: ",argv[1],"/cur")); + if (!argv[1]) logmsg(WHO, 100, USAGE, "maildirmake name"); + if (mkdir(argv[1], 0700) == -1) logmsg(WHO, 111, FATAL, B("unable to mkdir: ", argv[1])); + if (chdir(argv[1]) == -1) logmsg(WHO, 111, FATAL, B("unable to chdir to: ", argv[1])); + if (mkdir("tmp", 0700) == -1) logmsg(WHO, 111, FATAL, B("unable to mkdir: ", argv[1], "/tmp")); + if (mkdir("new", 0700) == -1) logmsg(WHO, 111, FATAL, B("unable to mkdir: ", argv[1], "/new")); + if (mkdir("cur", 0700) == -1) logmsg(WHO, 111, FATAL, B("unable to mkdir: ", argv[1], "/cur")); _exit(0); } diff --git a/src/maildirwatch.c b/src/maildirwatch.c index aea5cbb..1496e20 100644 --- a/src/maildirwatch.c +++ b/src/maildirwatch.c @@ -1,19 +1,24 @@ #include <unistd.h> -#include "getln.h" + #include "buffer.h" -#include "prioq.h" -#include "stralloc.h" -#include "str.h" #include "exit.h" -#include "hfield.h" +#include "getln.h" #include "logmsg.h" #include "open.h" +#include "str.h" +#include "stralloc.h" + #include "headerbody.h" +#include "hfield.h" #include "maildir.h" +#include "prioq.h" #define WHO "maildirwatch" -void die_nomem() { logmsg(WHO,111,FATAL,"out of memory"); } +void die_nomem() +{ + logmsg(WHO, 111, FATAL, "out of memory"); +} stralloc recipient = {0}; stralloc sender = {0}; @@ -22,36 +27,42 @@ stralloc text = {0}; void addtext(char *s, int n) { - if (!stralloc_catb(&text,s,n)) die_nomem(); - if (text.len > 158) text.len = 158; + if (!stralloc_catb(&text, s, n)) die_nomem(); + if (text.len > 158) text.len = 158; } -void dobody(stralloc *h) { addtext(h->s,h->len); } +void dobody(stralloc *h) +{ + addtext(h->s, h->len); +} void doheader(stralloc *h) { int i; - switch (hfield_known(h->s,h->len)) { + switch (hfield_known(h->s, h->len)) { case H_SUBJECT: - i = hfield_skipname(h->s,h->len); - addtext(h->s + i,h->len - i); + i = hfield_skipname(h->s, h->len); + addtext(h->s + i, h->len - i); break; case H_DELIVEREDTO: - i = hfield_skipname(h->s,h->len); + i = hfield_skipname(h->s, h->len); if (i < h->len) - if (!stralloc_copyb(&recipient,h->s + i,h->len - i - 1)) die_nomem(); + if (!stralloc_copyb(&recipient, h->s + i, h->len - i - 1)) die_nomem(); break; case H_RETURNPATH: - i = hfield_skipname(h->s,h->len); + i = hfield_skipname(h->s, h->len); if (i < h->len) - if (!stralloc_copyb(&sender,h->s + i,h->len - i - 1)) die_nomem(); + if (!stralloc_copyb(&sender, h->s + i, h->len - i - 1)) die_nomem(); break; case H_FROM: - if (!stralloc_copyb(&fromline,h->s,h->len - 1)) die_nomem(); + if (!stralloc_copyb(&fromline, h->s, h->len - 1)) die_nomem(); break; } } -void finishheader() { ; } +void finishheader() +{ + ; +} stralloc filenames = {0}; prioq pq = {0}; @@ -65,54 +76,48 @@ int main() int fd; int i; - if (maildir_chdir() == -1) - logmsg(WHO,111,FATAL,"Can't change to maildir"); + if (maildir_chdir() == -1) logmsg(WHO, 111, FATAL, "Can't change to maildir"); for (;;) { maildir_clean(&filenames); - if (maildir_scan(&pq,&filenames,1,0) == -1) - logmsg(WHO,111,FATAL,"Can't read maildir"); + if (maildir_scan(&pq, &filenames, 1, 0) == -1) logmsg(WHO, 111, FATAL, "Can't read maildir"); - buffer_putsflush(buffer_1,"\033[;H\033[;J"); + buffer_putsflush(buffer_1, "\033[;H\033[;J"); - while (prioq_min(&pq,&pe)) { + while (prioq_min(&pq, &pe)) { prioq_delmin(&pq); fd = open_read(filenames.s + pe.id); if (fd == -1) continue; - buffer_init(&bi,read,fd,inbuf,sizeof(inbuf)); + buffer_init(&bi, read, fd, inbuf, sizeof(inbuf)); - if (!stralloc_copys(&sender,"?")) die_nomem(); - if (!stralloc_copys(&recipient,"?")) die_nomem(); - if (!stralloc_copys(&fromline,"")) die_nomem(); - if (!stralloc_copys(&text,"")) die_nomem(); - if (headerbody(&bi,doheader,finishheader,dobody) == -1) - logmsg(WHO,111,FATAL,"trouble reading new message"); + if (!stralloc_copys(&sender, "?")) die_nomem(); + if (!stralloc_copys(&recipient, "?")) die_nomem(); + if (!stralloc_copys(&fromline, "")) die_nomem(); + if (!stralloc_copys(&text, "")) die_nomem(); + if (headerbody(&bi, doheader, finishheader, dobody) == -1) + logmsg(WHO, 111, FATAL, "trouble reading new message"); for (i = 0; i < fromline.len; ++i) - if ((fromline.s[i] < 32) || (fromline.s[i] > 126)) - fromline.s[i] = '/'; + if ((fromline.s[i] < 32) || (fromline.s[i] > 126)) fromline.s[i] = '/'; for (i = 0; i < sender.len; ++i) - if ((sender.s[i] < 32) || (sender.s[i] > 126)) - sender.s[i] = '?'; + if ((sender.s[i] < 32) || (sender.s[i] > 126)) sender.s[i] = '?'; for (i = 0; i < recipient.len; ++i) - if ((recipient.s[i] < 32) || (recipient.s[i] > 126)) - recipient.s[i] = '?'; + if ((recipient.s[i] < 32) || (recipient.s[i] > 126)) recipient.s[i] = '?'; for (i = 0; i < text.len; ++i) - if ((text.s[i] < 32) || (text.s[i] > 126)) - text.s[i] = '/'; - buffer_puts(buffer_1,"FROM "); - buffer_put(buffer_1,sender.s,sender.len); - buffer_puts(buffer_1," TO <"); - buffer_put(buffer_1,recipient.s,recipient.len); - buffer_puts(buffer_1,">\n"); + if ((text.s[i] < 32) || (text.s[i] > 126)) text.s[i] = '/'; + buffer_puts(buffer_1, "FROM "); + buffer_put(buffer_1, sender.s, sender.len); + buffer_puts(buffer_1, " TO <"); + buffer_put(buffer_1, recipient.s, recipient.len); + buffer_puts(buffer_1, ">\n"); if (fromline.len) { - buffer_puts(buffer_1,"\033[1m"); - buffer_put(buffer_1,fromline.s,fromline.len); - buffer_puts(buffer_1,"\033[0m\n"); + buffer_puts(buffer_1, "\033[1m"); + buffer_put(buffer_1, fromline.s, fromline.len); + buffer_puts(buffer_1, "\033[0m\n"); } - buffer_put(buffer_1,text.s,text.len); - buffer_puts(buffer_1,"\n\n"); + buffer_put(buffer_1, text.s, text.len); + buffer_puts(buffer_1, "\n\n"); close(fd); } diff --git a/src/matchup.c b/src/matchup.c index 047ea65..06cd481 100644 --- a/src/matchup.c +++ b/src/matchup.c @@ -1,52 +1,63 @@ #include <unistd.h> -#include "genalloc.h" + #include "alloc.h" -#include "stralloc.h" -#include "logmsg.h" -#include "getln.h" #include "buffer.h" -#include "readwrite.h" +#include "case.h" #include "exit.h" -#include "str.h" #include "fmt.h" +#include "genalloc.h" +#include "getln.h" +#include "logmsg.h" +#include "readwrite.h" #include "scan.h" -#include "case.h" +#include "str.h" +#include "stralloc.h" #define WHO "matchup" -void nomem() { logmsg(WHO,111,FATAL,"out of memory"); } -void die_read() { logmsg(WHO,110,ERROR,"unable to read input: "); } -void die_write() { logmsg(WHO,110,ERROR,"unable to write output: "); } -void die_write5() { logmsg(WHO,111,FATAL,"unable to write fd 5: "); } +void nomem() +{ + logmsg(WHO, 111, FATAL, "out of memory"); +} +void die_read() +{ + logmsg(WHO, 110, ERROR, "unable to read input: "); +} +void die_write() +{ + logmsg(WHO, 110, ERROR, "unable to write output: "); +} +void die_write5() +{ + logmsg(WHO, 111, FATAL, "unable to write fd 5: "); +} -void out(char *buf,int len) -{ - if (buffer_put(buffer_1,buf,len) == -1) die_write(); +void out(char *buf, int len) +{ + if (buffer_put(buffer_1, buf, len) == -1) die_write(); } -void outs(char *buf) -{ - if (buffer_puts(buffer_1,buf) == -1) die_write(); +void outs(char *buf) +{ + if (buffer_puts(buffer_1, buf) == -1) die_write(); } char buf5[512]; -buffer bo5 = BUFFER_INIT(write,5,buf5,sizeof(buf5)); +buffer bo5 = BUFFER_INIT(write, 5, buf5, sizeof(buf5)); -void out5(char *buf,int len) -{ - if (buffer_put(&bo5,buf,len) == -1) - die_write5(); +void out5(char *buf, int len) +{ + if (buffer_put(&bo5, buf, len) == -1) die_write5(); } -void outs5(char *buf) -{ - if (buffer_puts(&bo5,buf) == -1) - die_write5(); +void outs5(char *buf) +{ + if (buffer_puts(&bo5, buf) == -1) die_write5(); } -GEN_ALLOC_typedef(ulongalloc,unsigned long,u,len,a) -GEN_ALLOC_ready(ulongalloc,unsigned long,u,len,a,i,n,x,30,ulongalloc_ready) -GEN_ALLOC_readyplus(ulongalloc,unsigned long,u,len,a,i,n,x,30,ulongalloc_readyplus) +GEN_ALLOC_typedef(ulongalloc, unsigned long, u, len, a) + GEN_ALLOC_ready(ulongalloc, unsigned long, u, len, a, i, n, x, 30, ulongalloc_ready) + GEN_ALLOC_readyplus(ulongalloc, unsigned long, u, len, a, i, n, x, 30, ulongalloc_readyplus) -char strnum[FMT_ULONG]; + char strnum[FMT_ULONG]; stralloc pool = {0}; unsigned int poolbytes = 0; @@ -62,34 +73,34 @@ ulongalloc numz = {0}; ulongalloc sender = {0}; ulongalloc birth = {0}; -int msg_find(unsigned long m) +int msg_find(unsigned long m) { int i; - for (i = 0; i < nummsg; ++i) + for (i = 0; i < nummsg; ++i) if (msg.u[i] == m) return i; return -1; } -int msg_add(unsigned long m) +int msg_add(unsigned long m) { int i; - for (i = 0; i < nummsg; ++i) + for (i = 0; i < nummsg; ++i) if (msg.u[i] == m) return i; i = nummsg++; - if (!ulongalloc_ready(&msg,nummsg)) nomem(); - if (!ulongalloc_ready(&bytes,nummsg)) nomem(); - if (!ulongalloc_ready(&qp,nummsg)) nomem(); - if (!ulongalloc_ready(&uid,nummsg)) nomem(); - if (!ulongalloc_ready(&numk,nummsg)) nomem(); - if (!ulongalloc_ready(&numd,nummsg)) nomem(); - if (!ulongalloc_ready(&numz,nummsg)) nomem(); - if (!ulongalloc_ready(&sender,nummsg)) nomem(); - if (!ulongalloc_ready(&birth,nummsg)) nomem(); + if (!ulongalloc_ready(&msg, nummsg)) nomem(); + if (!ulongalloc_ready(&bytes, nummsg)) nomem(); + if (!ulongalloc_ready(&qp, nummsg)) nomem(); + if (!ulongalloc_ready(&uid, nummsg)) nomem(); + if (!ulongalloc_ready(&numk, nummsg)) nomem(); + if (!ulongalloc_ready(&numd, nummsg)) nomem(); + if (!ulongalloc_ready(&numz, nummsg)) nomem(); + if (!ulongalloc_ready(&sender, nummsg)) nomem(); + if (!ulongalloc_ready(&birth, nummsg)) nomem(); msg.u[i] = m; return i; } -void msg_kill(int i) +void msg_kill(int i) { poolbytes -= str_len(pool.s + sender.u[i]) + 1; poolbytes -= str_len(pool.s + birth.u[i]) + 1; @@ -113,30 +124,30 @@ ulongalloc dchan = {0}; ulongalloc drecip = {0}; ulongalloc dstart = {0}; -int del_find(unsigned long d) +int del_find(unsigned long d) { int i; - for (i = 0; i < numdel; ++i) + for (i = 0; i < numdel; ++i) if (del.u[i] == d) return i; return -1; } -int del_add(unsigned long d) +int del_add(unsigned long d) { int i; - for (i = 0; i < numdel; ++i) + for (i = 0; i < numdel; ++i) if (del.u[i] == d) return i; i = numdel++; - if (!ulongalloc_ready(&del,numdel)) nomem(); - if (!ulongalloc_ready(&dmsg,numdel)) nomem(); - if (!ulongalloc_ready(&dchan,numdel)) nomem(); - if (!ulongalloc_ready(&drecip,numdel)) nomem(); - if (!ulongalloc_ready(&dstart,numdel)) nomem(); + if (!ulongalloc_ready(&del, numdel)) nomem(); + if (!ulongalloc_ready(&dmsg, numdel)) nomem(); + if (!ulongalloc_ready(&dchan, numdel)) nomem(); + if (!ulongalloc_ready(&drecip, numdel)) nomem(); + if (!ulongalloc_ready(&dstart, numdel)) nomem(); del.u[i] = d; return i; } -void del_kill(int i) +void del_kill(int i) { poolbytes -= str_len(pool.s + dchan.u[i]) + 1; poolbytes -= str_len(pool.s + drecip.u[i]) + 1; @@ -158,35 +169,35 @@ void garbage() if (pool.len - poolbytes < poolbytes + 4096) return; - if (!stralloc_copys(&pool2,"")) nomem(); + if (!stralloc_copys(&pool2, "")) nomem(); for (i = 0; i < nummsg; ++i) { x = pool.s + birth.u[i]; birth.u[i] = pool2.len; - if (!stralloc_cats(&pool2,x)) nomem(); + if (!stralloc_cats(&pool2, x)) nomem(); if (!stralloc_0(&pool2)) nomem(); x = pool.s + sender.u[i]; sender.u[i] = pool2.len; - if (!stralloc_cats(&pool2,x)) nomem(); + if (!stralloc_cats(&pool2, x)) nomem(); if (!stralloc_0(&pool2)) nomem(); } for (i = 0; i < numdel; ++i) { x = pool.s + dstart.u[i]; dstart.u[i] = pool2.len; - if (!stralloc_cats(&pool2,x)) nomem(); + if (!stralloc_cats(&pool2, x)) nomem(); if (!stralloc_0(&pool2)) nomem(); x = pool.s + dchan.u[i]; dchan.u[i] = pool2.len; - if (!stralloc_cats(&pool2,x)) nomem(); + if (!stralloc_cats(&pool2, x)) nomem(); if (!stralloc_0(&pool2)) nomem(); x = pool.s + drecip.u[i]; drecip.u[i] = pool2.len; - if (!stralloc_cats(&pool2,x)) nomem(); + if (!stralloc_cats(&pool2, x)) nomem(); if (!stralloc_0(&pool2)) nomem(); } - if (!stralloc_copy(&pool,&pool2)) nomem(); + if (!stralloc_copy(&pool, &pool2)) nomem(); poolbytes = pool.len; /* redundant, but doesn't hurt */ } @@ -199,8 +210,7 @@ int field[FIELDS]; void clear() { - while (numdel > 0) - del_kill(0); + while (numdel > 0) del_kill(0); garbage(); } @@ -210,23 +220,23 @@ void starting() unsigned long m; int dpos; - scan_ulong(line.s + field[3],&d); - scan_ulong(line.s + field[5],&m); + scan_ulong(line.s + field[3], &d); + scan_ulong(line.s + field[5], &m); dpos = del_add(d); dmsg.u[dpos] = m; dstart.u[dpos] = pool.len; - if (!stralloc_cats(&pool,line.s + field[0])) nomem(); + if (!stralloc_cats(&pool, line.s + field[0])) nomem(); if (!stralloc_0(&pool)) nomem(); dchan.u[dpos] = pool.len; - if (!stralloc_cats(&pool,line.s + field[7])) nomem(); + if (!stralloc_cats(&pool, line.s + field[7])) nomem(); if (!stralloc_0(&pool)) nomem(); drecip.u[dpos] = pool.len; - if (!stralloc_cats(&pool,line.s + field[8])) nomem(); + if (!stralloc_cats(&pool, line.s + field[8])) nomem(); if (!stralloc_0(&pool)) nomem(); case_lowers(pool.s + drecip.u[dpos]); @@ -242,7 +252,7 @@ void delivery() char *result = "?"; char *reason = ""; - scan_ulong(line.s + field[2],&d); + scan_ulong(line.s + field[2], &d); dpos = del_find(d); if (dpos == -1) return; @@ -250,22 +260,19 @@ void delivery() m = dmsg.u[dpos]; mpos = msg_find(m); - if (str_start(line.s + field[3],"succ")) { + if (str_start(line.s + field[3], "succ")) { if (mpos != -1) ++numk.u[mpos]; result = "d k "; reason = line.s + field[4]; - } - else if (str_start(line.s + field[3],"fail")) { + } else if (str_start(line.s + field[3], "fail")) { if (mpos != -1) ++numd.u[mpos]; result = "d d "; reason = line.s + field[4]; - } - else if (str_start(line.s + field[3],"defer")) { + } else if (str_start(line.s + field[3], "defer")) { if (mpos != -1) ++numz.u[mpos]; result = "d z "; reason = line.s + field[4]; - } - else if (str_start(line.s + field[3],"report")) { + } else if (str_start(line.s + field[3], "report")) { if (mpos != -1) ++numz.u[mpos]; result = "d z "; reason = "report_mangled"; @@ -275,22 +282,36 @@ void delivery() if (mpos != -1) { outs(pool.s + birth.u[mpos]); - outs(" "); outs(pool.s + dstart.u[dpos]); - outs(" "); outs(line.s + field[0]); - outs(" "); out(strnum,fmt_ulong(strnum,bytes.u[mpos])); - outs(" "); outs(pool.s + sender.u[mpos]); - outs(" "); outs(pool.s + dchan.u[dpos]); - outs("."); outs(pool.s + drecip.u[dpos]); - outs(" "); out(strnum,fmt_ulong(strnum,qp.u[mpos])); - outs(" "); out(strnum,fmt_ulong(strnum,uid.u[mpos])); - outs(" "); outs(reason); + outs(" "); + outs(pool.s + dstart.u[dpos]); + outs(" "); + outs(line.s + field[0]); + outs(" "); + out(strnum, fmt_ulong(strnum, bytes.u[mpos])); + outs(" "); + outs(pool.s + sender.u[mpos]); + outs(" "); + outs(pool.s + dchan.u[dpos]); + outs("."); + outs(pool.s + drecip.u[dpos]); + outs(" "); + out(strnum, fmt_ulong(strnum, qp.u[mpos])); + outs(" "); + out(strnum, fmt_ulong(strnum, uid.u[mpos])); + outs(" "); + outs(reason); } else { outs(pool.s + dstart.u[dpos]); - outs(" "); outs(pool.s + dstart.u[dpos]); - outs(" "); outs(line.s + field[0]); - outs(" 0 ? "); outs(pool.s + dchan.u[dpos]); - outs("."); outs(pool.s + drecip.u[dpos]); - outs(" ? ? "); outs(reason); + outs(" "); + outs(pool.s + dstart.u[dpos]); + outs(" "); + outs(line.s + field[0]); + outs(" 0 ? "); + outs(pool.s + dchan.u[dpos]); + outs("."); + outs(pool.s + drecip.u[dpos]); + outs(" ? ? "); + outs(reason); } outs("\n"); @@ -304,7 +325,7 @@ void newmsg() unsigned long m; int mpos; - scan_ulong(line.s + field[3],&m); + scan_ulong(line.s + field[3], &m); mpos = msg_find(m); if (mpos == -1) return; msg_kill(mpos); @@ -316,19 +337,28 @@ void endmsg() unsigned long m; int mpos; - scan_ulong(line.s + field[3],&m); + scan_ulong(line.s + field[3], &m); mpos = msg_find(m); if (mpos == -1) return; - outs("m "); outs(pool.s + birth.u[mpos]); - outs(" "); outs(line.s + field[0]); - outs(" "); out(strnum,fmt_ulong(strnum,bytes.u[mpos])); - outs(" "); out(strnum,fmt_ulong(strnum,numk.u[mpos])); - outs(" "); out(strnum,fmt_ulong(strnum,numd.u[mpos])); - outs(" "); out(strnum,fmt_ulong(strnum,numz.u[mpos])); - outs(" "); outs(pool.s + sender.u[mpos]); - outs(" "); out(strnum,fmt_ulong(strnum,qp.u[mpos])); - outs(" "); out(strnum,fmt_ulong(strnum,uid.u[mpos])); + outs("m "); + outs(pool.s + birth.u[mpos]); + outs(" "); + outs(line.s + field[0]); + outs(" "); + out(strnum, fmt_ulong(strnum, bytes.u[mpos])); + outs(" "); + out(strnum, fmt_ulong(strnum, numk.u[mpos])); + outs(" "); + out(strnum, fmt_ulong(strnum, numd.u[mpos])); + outs(" "); + out(strnum, fmt_ulong(strnum, numz.u[mpos])); + outs(" "); + outs(pool.s + sender.u[mpos]); + outs(" "); + out(strnum, fmt_ulong(strnum, qp.u[mpos])); + outs(" "); + out(strnum, fmt_ulong(strnum, uid.u[mpos])); outs("\n"); msg_kill(mpos); @@ -340,23 +370,23 @@ void info() unsigned long m; int mpos; - scan_ulong(line.s + field[3],&m); + scan_ulong(line.s + field[3], &m); mpos = msg_add(m); - scan_ulong(line.s + field[5],&bytes.u[mpos]); - scan_ulong(line.s + field[9],&qp.u[mpos]); - scan_ulong(line.s + field[11],&uid.u[mpos]); + scan_ulong(line.s + field[5], &bytes.u[mpos]); + scan_ulong(line.s + field[9], &qp.u[mpos]); + scan_ulong(line.s + field[11], &uid.u[mpos]); numk.u[mpos] = 0; numd.u[mpos] = 0; numz.u[mpos] = 0; birth.u[mpos] = pool.len; - if (!stralloc_cats(&pool,line.s + field[0])) nomem(); + if (!stralloc_cats(&pool, line.s + field[0])) nomem(); if (!stralloc_0(&pool)) nomem(); sender.u[mpos] = pool.len; - if (!stralloc_cats(&pool,line.s + field[7])) nomem(); + if (!stralloc_cats(&pool, line.s + field[7])) nomem(); if (!stralloc_0(&pool)) nomem(); case_lowers(pool.s + sender.u[mpos]); @@ -368,13 +398,13 @@ void extra() unsigned long m; int mpos; - scan_ulong(line.s + field[2],&m); + scan_ulong(line.s + field[2], &m); mpos = msg_find(m); if (mpos == -1) return; - scan_ulong(line.s + field[3],&numk.u[mpos]); - scan_ulong(line.s + field[4],&numz.u[mpos]); - scan_ulong(line.s + field[5],&numd.u[mpos]); + scan_ulong(line.s + field[3], &numk.u[mpos]); + scan_ulong(line.s + field[4], &numz.u[mpos]); + scan_ulong(line.s + field[5], &numd.u[mpos]); } void pending() @@ -384,34 +414,34 @@ void pending() for (i = 0; i < nummsg; ++i) { outs5(pool.s + birth.u[i]); outs5(" info msg "); - out5(strnum,fmt_ulong(strnum,msg.u[i])); + out5(strnum, fmt_ulong(strnum, msg.u[i])); outs5(": bytes "); - out5(strnum,fmt_ulong(strnum,bytes.u[i])); + out5(strnum, fmt_ulong(strnum, bytes.u[i])); outs5(" from "); outs5(pool.s + sender.u[i]); outs5(" qp "); - out5(strnum,fmt_ulong(strnum,qp.u[i])); + out5(strnum, fmt_ulong(strnum, qp.u[i])); outs5(" uid "); - out5(strnum,fmt_ulong(strnum,uid.u[i])); + out5(strnum, fmt_ulong(strnum, uid.u[i])); outs5("\n"); outs5(pool.s + birth.u[i]); outs5(" extra "); - out5(strnum,fmt_ulong(strnum,msg.u[i])); + out5(strnum, fmt_ulong(strnum, msg.u[i])); outs5(" "); - out5(strnum,fmt_ulong(strnum,numk.u[i])); + out5(strnum, fmt_ulong(strnum, numk.u[i])); outs5(" "); - out5(strnum,fmt_ulong(strnum,numz.u[i])); + out5(strnum, fmt_ulong(strnum, numz.u[i])); outs5(" "); - out5(strnum,fmt_ulong(strnum,numd.u[i])); + out5(strnum, fmt_ulong(strnum, numd.u[i])); outs5("\n"); } for (i = 0; i < numdel; ++i) { outs5(pool.s + dstart.u[i]); outs5(" starting delivery "); - out5(strnum,fmt_ulong(strnum,del.u[i])); + out5(strnum, fmt_ulong(strnum, del.u[i])); outs5(": msg "); - out5(strnum,fmt_ulong(strnum,dmsg.u[i])); + out5(strnum, fmt_ulong(strnum, dmsg.u[i])); outs5(" to "); outs5(pool.s + dchan.u[i]); outs5(" "); @@ -419,7 +449,7 @@ void pending() outs5("\n"); } - out5(line.s,line.len); + out5(line.s, line.len); if (buffer_flush(&bo5) == -1) die_write5(); } @@ -431,23 +461,23 @@ int main() int j; char ch; - if (!stralloc_copys(&pool,"")) nomem(); + if (!stralloc_copys(&pool, "")) nomem(); - if (!ulongalloc_ready(&msg,1)) nomem(); - if (!ulongalloc_ready(&bytes,1)) nomem(); - if (!ulongalloc_ready(&qp,1)) nomem(); - if (!ulongalloc_ready(&uid,1)) nomem(); - if (!ulongalloc_ready(&numk,1)) nomem(); - if (!ulongalloc_ready(&numd,1)) nomem(); - if (!ulongalloc_ready(&numz,1)) nomem(); - if (!ulongalloc_ready(&del,1)) nomem(); - if (!ulongalloc_ready(&dmsg,1)) nomem(); + if (!ulongalloc_ready(&msg, 1)) nomem(); + if (!ulongalloc_ready(&bytes, 1)) nomem(); + if (!ulongalloc_ready(&qp, 1)) nomem(); + if (!ulongalloc_ready(&uid, 1)) nomem(); + if (!ulongalloc_ready(&numk, 1)) nomem(); + if (!ulongalloc_ready(&numd, 1)) nomem(); + if (!ulongalloc_ready(&numz, 1)) nomem(); + if (!ulongalloc_ready(&del, 1)) nomem(); + if (!ulongalloc_ready(&dmsg, 1)) nomem(); for (;;) { - if (getln(buffer_0,&line,&match,'\n') == -1) die_read(); + if (getln(buffer_0, &line, &match, '\n') == -1) die_read(); if (!match) break; - if (!stralloc_copy(&outline,&line)) nomem(); + if (!stralloc_copy(&outline, &line)) nomem(); for (i = 0; i < line.len; ++i) { ch = line.s[i]; @@ -455,29 +485,51 @@ int main() } j = 0; for (i = 0; i < FIELDS; ++i) { - while (j < line.len) if (line.s[j]) break; else ++j; + while (j < line.len) + if (line.s[j]) + break; + else + ++j; field[i] = j; - while (j < line.len) if (!line.s[j]) break; else ++j; + while (j < line.len) + if (!line.s[j]) + break; + else + ++j; } if (!stralloc_0(&line)) nomem(); - if (str_equal(line.s + field[1],"status:")) ; - else if (str_equal(line.s + field[1],"starting")) starting(); - else if (str_equal(line.s + field[1],"delivery")) delivery(); - else if (str_equal(line.s + field[1],"new")) newmsg(); - else if (str_equal(line.s + field[1],"end")) endmsg(); - else if (str_equal(line.s + field[1],"info")) info(); - else if (str_equal(line.s + field[1],"extra")) extra(); - else if (str_equal(line.s + field[1],"running")) clear(); - else if (str_equal(line.s + field[1],"exiting")) clear(); - else if (str_equal(line.s + field[1],"number")) ; - else if (str_equal(line.s + field[1],"local")) ; - else if (str_equal(line.s + field[1],"remote")) ; - else if (str_equal(line.s + field[1],"warning:")) out(outline.s,outline.len); - else if (str_equal(line.s + field[1],"alert:")) out(outline.s,outline.len); + if (str_equal(line.s + field[1], "status:")) + ; + else if (str_equal(line.s + field[1], "starting")) + starting(); + else if (str_equal(line.s + field[1], "delivery")) + delivery(); + else if (str_equal(line.s + field[1], "new")) + newmsg(); + else if (str_equal(line.s + field[1], "end")) + endmsg(); + else if (str_equal(line.s + field[1], "info")) + info(); + else if (str_equal(line.s + field[1], "extra")) + extra(); + else if (str_equal(line.s + field[1], "running")) + clear(); + else if (str_equal(line.s + field[1], "exiting")) + clear(); + else if (str_equal(line.s + field[1], "number")) + ; + else if (str_equal(line.s + field[1], "local")) + ; + else if (str_equal(line.s + field[1], "remote")) + ; + else if (str_equal(line.s + field[1], "warning:")) + out(outline.s, outline.len); + else if (str_equal(line.s + field[1], "alert:")) + out(outline.s, outline.len); else { outs("? "); - out(outline.s,outline.len); + out(outline.s, outline.len); } } @@ -23,9 +23,10 @@ documentation and/or software. */ +#include <stdint.h> + #include "global.h" #include "md5.h" -#include <stdint.h> /* Constants for MD5Transform routine. */ #define S11 7 @@ -45,19 +46,15 @@ #define S43 15 #define S44 21 -static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64])); -static void Encode PROTO_LIST - ((unsigned char *, UINT4 *, unsigned int)); -static void Decode PROTO_LIST - ((UINT4 *, unsigned char *, unsigned int)); -static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int)); -static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int)); +static void MD5Transform PROTO_LIST((UINT4[4], unsigned char[64])); +static void Encode PROTO_LIST((unsigned char *, UINT4 *, unsigned int)); +static void Decode PROTO_LIST((UINT4 *, unsigned char *, unsigned int)); +static void MD5_memcpy PROTO_LIST((POINTER, POINTER, unsigned int)); +static void MD5_memset PROTO_LIST((POINTER, int, unsigned int)); static unsigned char PADDING[64] = { - 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 -}; + 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; /* F, G, H and I are basic MD5 functions. */ #define F(x, y, z) (((x) & (y)) | ((~x) & (z))) @@ -66,35 +63,38 @@ static unsigned char PADDING[64] = { #define I(x, y, z) ((y) ^ ((x) | (~z))) /* ROTATE_LEFT rotates x left n bits. */ -#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n)))) +#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32 - (n)))) /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4. Rotation is separate from addition to prevent recomputation. */ -#define FF(a, b, c, d, x, s, ac) { \ - (a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \ - (a) = ROTATE_LEFT ((a), (s)); \ - (a) += (b); \ +#define FF(a, b, c, d, x, s, ac) \ + { \ + (a) += F((b), (c), (d)) + (x) + (UINT4)(ac); \ + (a) = ROTATE_LEFT((a), (s)); \ + (a) += (b); \ } -#define GG(a, b, c, d, x, s, ac) { \ - (a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \ - (a) = ROTATE_LEFT ((a), (s)); \ - (a) += (b); \ +#define GG(a, b, c, d, x, s, ac) \ + { \ + (a) += G((b), (c), (d)) + (x) + (UINT4)(ac); \ + (a) = ROTATE_LEFT((a), (s)); \ + (a) += (b); \ } -#define HH(a, b, c, d, x, s, ac) { \ - (a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \ - (a) = ROTATE_LEFT ((a), (s)); \ - (a) += (b); \ +#define HH(a, b, c, d, x, s, ac) \ + { \ + (a) += H((b), (c), (d)) + (x) + (UINT4)(ac); \ + (a) = ROTATE_LEFT((a), (s)); \ + (a) += (b); \ } -#define II(a, b, c, d, x, s, ac) { \ - (a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \ - (a) = ROTATE_LEFT ((a), (s)); \ - (a) += (b); \ +#define II(a, b, c, d, x, s, ac) \ + { \ + (a) += I((b), (c), (d)) + (x) + (UINT4)(ac); \ + (a) = ROTATE_LEFT((a), (s)); \ + (a) += (b); \ } /* MD5 initialization. Begins an MD5 operation, writing a new context. */ -void MD5Init (context) -MD5_CTX *context; /* context */ +void MD5Init(context) MD5_CTX *context; /* context */ { context->count[0] = context->count[1] = 0; @@ -110,10 +110,9 @@ MD5_CTX *context; /* context */ operation, processing another message block, and updating the context. */ -void MD5Update (context, input, inputLen) -MD5_CTX *context; /* context */ -unsigned char *input; /* input block */ -unsigned int inputLen; /* length of input block */ +void MD5Update(context, input, inputLen) MD5_CTX *context; /* context */ +unsigned char *input; /* input block */ +unsigned int inputLen; /* length of input block */ { unsigned int i, index, partLen; @@ -121,157 +120,147 @@ unsigned int inputLen; /* length of input block */ index = (unsigned int)((context->count[0] >> 3) & 0x3F); /* Update number of bits */ - if ((context->count[0] += ((UINT4)inputLen << 3)) - < ((UINT4)inputLen << 3)) - context->count[1]++; + if ((context->count[0] += ((UINT4)inputLen << 3)) < ((UINT4)inputLen << 3)) context->count[1]++; context->count[1] += ((UINT4)inputLen >> 29); - + partLen = 64 - index; - + /* Transform as many times as possible. */ if (inputLen >= partLen) { - MD5_memcpy - ((POINTER)&context->buffer[index], (POINTER)input, partLen); - MD5Transform (context->state, context->buffer); - - for (i = partLen; i + 63 < inputLen; i += 64) - MD5Transform (context->state, &input[i]); - + MD5_memcpy((POINTER)&context->buffer[index], (POINTER)input, partLen); + MD5Transform(context->state, context->buffer); + + for (i = partLen; i + 63 < inputLen; i += 64) MD5Transform(context->state, &input[i]); + index = 0; - } - else + } else i = 0; - + /* Buffer remaining input */ - MD5_memcpy - ((POINTER)&context->buffer[index], (POINTER)&input[i], - inputLen-i); + MD5_memcpy((POINTER)&context->buffer[index], (POINTER)&input[i], inputLen - i); } /* MD5 finalization. Ends an MD5 message-digest operation, writing the the message digest and zeroizing the context. */ -void MD5Final (digest, context) -unsigned char digest[16]; /* message digest */ -MD5_CTX *context; /* context */ +void MD5Final(digest, context) unsigned char digest[16]; /* message digest */ +MD5_CTX *context; /* context */ { unsigned char bits[8]; unsigned int index, padLen; /* Save number of bits */ - Encode (bits, context->count, 8); + Encode(bits, context->count, 8); /* Pad out to 56 mod 64. */ index = (unsigned int)((context->count[0] >> 3) & 0x3f); padLen = (index < 56) ? (56 - index) : (120 - index); - MD5Update (context, PADDING, padLen); - + MD5Update(context, PADDING, padLen); + /* Append length (before padding) */ - MD5Update (context, bits, 8); + MD5Update(context, bits, 8); /* Store state in digest */ - Encode (digest, context->state, 16); - + Encode(digest, context->state, 16); + /* Zeroize sensitive information. */ - MD5_memset ((POINTER)context, 0, sizeof (*context)); + MD5_memset((POINTER)context, 0, sizeof(*context)); } /* MD5 basic transformation. Transforms state based on block. */ -static void MD5Transform (state, block) -UINT4 state[4]; +static void MD5Transform(state, block) UINT4 state[4]; unsigned char block[64]; { UINT4 a = state[0], b = state[1], c = state[2], d = state[3], x[16]; - - Decode (x, block, 64); + + Decode(x, block, 64); /* Round 1 */ - FF (a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */ - FF (d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */ - FF (c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */ - FF (b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */ - FF (a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */ - FF (d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */ - FF (c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */ - FF (b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */ - FF (a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */ - FF (d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */ - FF (c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */ - FF (b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */ - FF (a, b, c, d, x[12], S11, 0x6b901122); /* 13 */ - FF (d, a, b, c, x[13], S12, 0xfd987193); /* 14 */ - FF (c, d, a, b, x[14], S13, 0xa679438e); /* 15 */ - FF (b, c, d, a, x[15], S14, 0x49b40821); /* 16 */ + FF(a, b, c, d, x[0], S11, 0xd76aa478); /* 1 */ + FF(d, a, b, c, x[1], S12, 0xe8c7b756); /* 2 */ + FF(c, d, a, b, x[2], S13, 0x242070db); /* 3 */ + FF(b, c, d, a, x[3], S14, 0xc1bdceee); /* 4 */ + FF(a, b, c, d, x[4], S11, 0xf57c0faf); /* 5 */ + FF(d, a, b, c, x[5], S12, 0x4787c62a); /* 6 */ + FF(c, d, a, b, x[6], S13, 0xa8304613); /* 7 */ + FF(b, c, d, a, x[7], S14, 0xfd469501); /* 8 */ + FF(a, b, c, d, x[8], S11, 0x698098d8); /* 9 */ + FF(d, a, b, c, x[9], S12, 0x8b44f7af); /* 10 */ + FF(c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */ + FF(b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */ + FF(a, b, c, d, x[12], S11, 0x6b901122); /* 13 */ + FF(d, a, b, c, x[13], S12, 0xfd987193); /* 14 */ + FF(c, d, a, b, x[14], S13, 0xa679438e); /* 15 */ + FF(b, c, d, a, x[15], S14, 0x49b40821); /* 16 */ /* Round 2 */ - GG (a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */ - GG (d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */ - GG (c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */ - GG (b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */ - GG (a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */ - GG (d, a, b, c, x[10], S22, 0x2441453); /* 22 */ - GG (c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */ - GG (b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */ - GG (a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */ - GG (d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */ - GG (c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */ - GG (b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */ - GG (a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */ - GG (d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */ - GG (c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */ - GG (b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */ + GG(a, b, c, d, x[1], S21, 0xf61e2562); /* 17 */ + GG(d, a, b, c, x[6], S22, 0xc040b340); /* 18 */ + GG(c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */ + GG(b, c, d, a, x[0], S24, 0xe9b6c7aa); /* 20 */ + GG(a, b, c, d, x[5], S21, 0xd62f105d); /* 21 */ + GG(d, a, b, c, x[10], S22, 0x2441453); /* 22 */ + GG(c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */ + GG(b, c, d, a, x[4], S24, 0xe7d3fbc8); /* 24 */ + GG(a, b, c, d, x[9], S21, 0x21e1cde6); /* 25 */ + GG(d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */ + GG(c, d, a, b, x[3], S23, 0xf4d50d87); /* 27 */ + GG(b, c, d, a, x[8], S24, 0x455a14ed); /* 28 */ + GG(a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */ + GG(d, a, b, c, x[2], S22, 0xfcefa3f8); /* 30 */ + GG(c, d, a, b, x[7], S23, 0x676f02d9); /* 31 */ + GG(b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */ /* Round 3 */ - HH (a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */ - HH (d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */ - HH (c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */ - HH (b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */ - HH (a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */ - HH (d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */ - HH (c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */ - HH (b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */ - HH (a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */ - HH (d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */ - HH (c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */ - HH (b, c, d, a, x[ 6], S34, 0x4881d05); /* 44 */ - HH (a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */ - HH (d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */ - HH (c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */ - HH (b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */ + HH(a, b, c, d, x[5], S31, 0xfffa3942); /* 33 */ + HH(d, a, b, c, x[8], S32, 0x8771f681); /* 34 */ + HH(c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */ + HH(b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */ + HH(a, b, c, d, x[1], S31, 0xa4beea44); /* 37 */ + HH(d, a, b, c, x[4], S32, 0x4bdecfa9); /* 38 */ + HH(c, d, a, b, x[7], S33, 0xf6bb4b60); /* 39 */ + HH(b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */ + HH(a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */ + HH(d, a, b, c, x[0], S32, 0xeaa127fa); /* 42 */ + HH(c, d, a, b, x[3], S33, 0xd4ef3085); /* 43 */ + HH(b, c, d, a, x[6], S34, 0x4881d05); /* 44 */ + HH(a, b, c, d, x[9], S31, 0xd9d4d039); /* 45 */ + HH(d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */ + HH(c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */ + HH(b, c, d, a, x[2], S34, 0xc4ac5665); /* 48 */ /* Round 4 */ - II (a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */ - II (d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */ - II (c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */ - II (b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */ - II (a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */ - II (d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */ - II (c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */ - II (b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */ - II (a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */ - II (d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */ - II (c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */ - II (b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */ - II (a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */ - II (d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */ - II (c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */ - II (b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */ + II(a, b, c, d, x[0], S41, 0xf4292244); /* 49 */ + II(d, a, b, c, x[7], S42, 0x432aff97); /* 50 */ + II(c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */ + II(b, c, d, a, x[5], S44, 0xfc93a039); /* 52 */ + II(a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */ + II(d, a, b, c, x[3], S42, 0x8f0ccc92); /* 54 */ + II(c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */ + II(b, c, d, a, x[1], S44, 0x85845dd1); /* 56 */ + II(a, b, c, d, x[8], S41, 0x6fa87e4f); /* 57 */ + II(d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */ + II(c, d, a, b, x[6], S43, 0xa3014314); /* 59 */ + II(b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */ + II(a, b, c, d, x[4], S41, 0xf7537e82); /* 61 */ + II(d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */ + II(c, d, a, b, x[2], S43, 0x2ad7d2bb); /* 63 */ + II(b, c, d, a, x[9], S44, 0xeb86d391); /* 64 */ state[0] += a; state[1] += b; state[2] += c; state[3] += d; - + /* Zeroize sensitive information. */ - MD5_memset ((POINTER)x, 0, sizeof (x)); + MD5_memset((POINTER)x, 0, sizeof(x)); } /* Encodes input (UINT4) into output (unsigned char). Assumes len is a multiple of 4. */ -static void Encode (output, input, len) -unsigned char *output; +static void Encode(output, input, len) unsigned char *output; UINT4 *input; unsigned int len; { @@ -279,49 +268,44 @@ unsigned int len; for (i = 0, j = 0; j < len; i++, j += 4) { output[j] = (unsigned char)(input[i] & 0xff); - output[j+1] = (unsigned char)((input[i] >> 8) & 0xff); - output[j+2] = (unsigned char)((input[i] >> 16) & 0xff); - output[j+3] = (unsigned char)((input[i] >> 24) & 0xff); + output[j + 1] = (unsigned char)((input[i] >> 8) & 0xff); + output[j + 2] = (unsigned char)((input[i] >> 16) & 0xff); + output[j + 3] = (unsigned char)((input[i] >> 24) & 0xff); } } /* Decodes input (unsigned char) into output (UINT4). Assumes len is a multiple of 4. */ -static void Decode (output, input, len) -UINT4 *output; +static void Decode(output, input, len) UINT4 *output; unsigned char *input; unsigned int len; { unsigned int i, j; for (i = 0, j = 0; j < len; i++, j += 4) - output[i] = ((UINT4)input[j]) | (((UINT4)input[j+1]) << 8) | - (((UINT4)input[j+2]) << 16) | (((UINT4)input[j+3]) << 24); + output[i] = ((UINT4)input[j]) | (((UINT4)input[j + 1]) << 8) | (((UINT4)input[j + 2]) << 16) + | (((UINT4)input[j + 3]) << 24); } /* Note: Replace "for loop" with standard memcpy if possible. */ -static void MD5_memcpy (output, input, len) -POINTER output; +static void MD5_memcpy(output, input, len) POINTER output; POINTER input; unsigned int len; { unsigned int i; - - for (i = 0; i < len; i++) - output[i] = input[i]; + + for (i = 0; i < len; i++) output[i] = input[i]; } /* Note: Replace "for loop" with standard memset if possible. */ -static void MD5_memset (output, value, len) -POINTER output; +static void MD5_memset(output, value, len) POINTER output; int value; unsigned int len; { unsigned int i; - - for (i = 0; i < len; i++) - ((char *)output)[i] = (char)value; + + for (i = 0; i < len; i++) ((char *)output)[i] = (char)value; } diff --git a/src/mfrules.c b/src/mfrules.c index 9c6240f..be32422 100644 --- a/src/mfrules.c +++ b/src/mfrules.c @@ -1,12 +1,13 @@ -#include "alloc.h" -#include "stralloc.h" -#include "open.h" -#include "cdbread.h" -#include "case.h" #include "mfrules.h" -#include "str.h" + +#include "alloc.h" #include "byte.h" +#include "case.h" +#include "cdbread.h" #include "close.h" +#include "open.h" +#include "str.h" +#include "stralloc.h" /* return -9: problems reading cdb */ /* return -1: key matches; data not */ @@ -29,9 +30,9 @@ static int mffind(char *mf) int mflen; int delta; - switch (cdb_find(&cdb,key.s,key.len)) { + switch (cdb_find(&cdb, key.s, key.len)) { case -1: return -9; - case 0: return 0; + case 0: return 0; } datalen = cdb_datalen(&cdb); @@ -40,61 +41,65 @@ static int mffind(char *mf) if (!datalen) return 1; mflen = str_len(mf); - if (cdb_read(&cdb,data,datalen,cdb_datapos(&cdb)) == -1) { + if (cdb_read(&cdb, data, datalen, cdb_datapos(&cdb)) == -1) { alloc_free(data); return -9; } - x = data; dlen = datalen - 1; /* trailing separator */ - + x = data; + dlen = datalen - 1; /* trailing separator */ + while (dlen > 0) { - plus = byte_rchr(data,dlen,'+'); + plus = byte_rchr(data, dlen, '+'); x = data + plus + 1; len = dlen - plus; delta = (mflen > len) ? mflen - len : 0; - if (!byte_diff(x,len,mf + delta)) { alloc_free(data); return 2; } + if (!byte_diff(x, len, mf + delta)) { + alloc_free(data); + return 2; + } dlen = plus - 1; } alloc_free(data); return -1; -} +} -int mfsearch(char *ip,char *host,char *info,char *mf) +int mfsearch(char *ip, char *host, char *info, char *mf) { int r; if (info) { - if (!stralloc_copys(&key,info)) return -9; + if (!stralloc_copys(&key, info)) return -9; r = mffind(mf); if (r < -1 || r > 0) return r; - if (!stralloc_cats(&key,"@")) return -9; - if (!stralloc_cats(&key,ip)) return -9; + if (!stralloc_cats(&key, "@")) return -9; + if (!stralloc_cats(&key, ip)) return -9; r = mffind(mf); if (r < -1 || r > 0) return r; if (host) { - if (!stralloc_copys(&key,info)) return -9; - if (!stralloc_cats(&key,"@=")) return -9; - if (!stralloc_cats(&key,host)) return -9; + if (!stralloc_copys(&key, info)) return -9; + if (!stralloc_cats(&key, "@=")) return -9; + if (!stralloc_cats(&key, host)) return -9; r = mffind(mf); if (r < -1 || r > 0) return r; } } - if (!stralloc_copys(&key,ip)) return -9; + if (!stralloc_copys(&key, ip)) return -9; r = mffind(mf); if (r < -1 || r > 0) return r; if (host) { - if (!stralloc_copys(&key,"=")) return -9; - if (!stralloc_cats(&key,host)) return -9; + if (!stralloc_copys(&key, "=")) return -9; + if (!stralloc_cats(&key, host)) return -9; r = mffind(mf); if (r < -1 || r > 0) return r; } - if (!stralloc_copys(&key,ip)) return -9; /* IPv6 */ + if (!stralloc_copys(&key, ip)) return -9; /* IPv6 */ while (key.len > 0) { if (ip[key.len - 1] == ':') { r = mffind(mf); @@ -103,7 +108,7 @@ int mfsearch(char *ip,char *host,char *info,char *mf) --key.len; } - if (!stralloc_copys(&key,ip)) return -9; /* IPv4 */ + if (!stralloc_copys(&key, ip)) return -9; /* IPv4 */ while (key.len > 0) { if (ip[key.len - 1] == '.') { r = mffind(mf); @@ -115,30 +120,30 @@ int mfsearch(char *ip,char *host,char *info,char *mf) if (host) { while (*host) { if (*host == '.') { - if (!stralloc_copys(&key,"=")) return -9; - if (!stralloc_cats(&key,host)) return -9; + if (!stralloc_copys(&key, "=")) return -9; + if (!stralloc_cats(&key, host)) return -9; r = mffind(mf); if (r < -1 || r > 0) return r; } ++host; } - if (!stralloc_copys(&key,"=")) return -9; + if (!stralloc_copys(&key, "=")) return -9; r = mffind(mf); if (r < -1 || r > 0) return r; } key.len = 0; -/* return mffind(mf); */ + /* return mffind(mf); */ return -1; } -int mfrules(int fd,char *ip,char *host,char *info,char *mf) +int mfrules(int fd, char *ip, char *host, char *info, char *mf) { int r; - cdb_init(&cdb,fd); + cdb_init(&cdb, fd); case_lowers(mf); - r = mfsearch(ip,host,info,mf); + r = mfsearch(ip, host, info, mf); cdb_free(&cdb); close(fd); diff --git a/src/myctime.c b/src/myctime.c index 6f554c9..ebb01ba 100644 --- a/src/myctime.c +++ b/src/myctime.c @@ -1,13 +1,12 @@ -#include "datetime.h" -#include "fmt.h" #include "myctime.h" -static char *daytab[7] = { -"Sun","Mon","Tue","Wed","Thu","Fri","Sat" -}; +#include "fmt.h" + +#include "datetime.h" + +static char *daytab[7] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; static char *montab[12] = { -"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec" -}; + "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; static char result[30]; @@ -15,21 +14,21 @@ char *myctime(datetime_sec t) { struct datetime dt; unsigned int len; - datetime_tai(&dt,t); + datetime_tai(&dt, t); len = 0; - len += fmt_str(result + len,daytab[dt.wday]); + len += fmt_str(result + len, daytab[dt.wday]); result[len++] = ' '; - len += fmt_str(result + len,montab[dt.mon]); + len += fmt_str(result + len, montab[dt.mon]); result[len++] = ' '; - len += fmt_uint0(result + len,dt.mday,2); + len += fmt_uint0(result + len, dt.mday, 2); result[len++] = ' '; - len += fmt_uint0(result + len,dt.hour,2); + len += fmt_uint0(result + len, dt.hour, 2); result[len++] = ':'; - len += fmt_uint0(result + len,dt.min,2); + len += fmt_uint0(result + len, dt.min, 2); result[len++] = ':'; - len += fmt_uint0(result + len,dt.sec,2); + len += fmt_uint0(result + len, dt.sec, 2); result[len++] = ' '; - len += fmt_uint(result + len,1900 + dt.year); + len += fmt_uint(result + len, 1900 + dt.year); result[len++] = '\n'; result[len++] = 0; return result; diff --git a/src/newaliases.c b/src/newaliases.c index 98aa095..08106a4 100644 --- a/src/newaliases.c +++ b/src/newaliases.c @@ -1,45 +1,47 @@ -#include <unistd.h> #include <sys/stat.h> +#include <unistd.h> + #include "buffer.h" -#include "logmsg.h" +#include "byte.h" +#include "case.h" +#include "cdbmake.h" #include "genalloc.h" -#include "stralloc.h" #include "getln.h" +#include "logmsg.h" #include "open.h" -#include "token822.h" -#include "control.h" +#include "stralloc.h" + #include "auto_qmail.h" -#include "case.h" -#include "cdbmake.h" -#include "byte.h" +#include "control.h" +#include "token822.h" #define WHO "newaliases" -int rename(const char *,const char *); // stdio.h +int rename(const char *, const char *); // stdio.h void nomem() { - logmsg(WHO,111,FATAL,"out of memory"); + logmsg(WHO, 111, FATAL, "out of memory"); } void nulbyte() { - logmsg(WHO,100,FATAL,"NUL bytes are not permitted"); + logmsg(WHO, 100, FATAL, "NUL bytes are not permitted"); } void longaddress() { - logmsg(WHO,100,FATAL,"addresses over 800 bytes are not permitted"); + logmsg(WHO, 100, FATAL, "addresses over 800 bytes are not permitted"); } void writeerr() { - logmsg(WHO,111,FATAL,"unable to write to /etc/aliases.tmp"); + logmsg(WHO, 111, FATAL, "unable to write to /etc/aliases.tmp"); } void readerr() { - logmsg(WHO,111,FATAL,"unable to read /etc/aliases"); + logmsg(WHO, 111, FATAL, "unable to read /etc/aliases"); } void die_control() { - logmsg(WHO,111,FATAL,"unable to read controls"); + logmsg(WHO, 111, FATAL, "unable to read controls"); } stralloc me = {0}; @@ -53,30 +55,31 @@ void readcontrols() int fddir; fddir = open_read("."); - if (fddir == -1) - logmsg(WHO,111,FATAL,"unable to open current directory"); + if (fddir == -1) logmsg(WHO, 111, FATAL, "unable to open current directory"); - if (chdir(auto_qmail) == -1) - logmsg(WHO,111,FATAL,B("unable to chdir to: ",auto_qmail)); + if (chdir(auto_qmail) == -1) logmsg(WHO, 111, FATAL, B("unable to chdir to: ", auto_qmail)); - r = control_readline(&me,"control/me"); + r = control_readline(&me, "control/me"); if (r == -1) die_control(); - if (!r) if (!stralloc_copys(&me,"me")) nomem(); + if (!r) + if (!stralloc_copys(&me, "me")) nomem(); - r = control_readline(&defaultdomain,"control/defaultdomain"); + r = control_readline(&defaultdomain, "control/defaultdomain"); if (r == -1) die_control(); - if (!r) if (!stralloc_copy(&defaultdomain,&me)) nomem(); + if (!r) + if (!stralloc_copy(&defaultdomain, &me)) nomem(); - r = control_readline(&defaulthost,"control/defaulthost"); + r = control_readline(&defaulthost, "control/defaulthost"); if (r == -1) die_control(); - if (!r) if (!stralloc_copy(&defaulthost,&me)) nomem(); + if (!r) + if (!stralloc_copy(&defaulthost, &me)) nomem(); - r = control_readline(&plusdomain,"control/plusdomain"); + r = control_readline(&plusdomain, "control/plusdomain"); if (r == -1) die_control(); - if (!r) if (!stralloc_copy(&plusdomain,&me)) nomem(); + if (!r) + if (!stralloc_copy(&plusdomain, &me)) nomem(); - if (fchdir(fddir) == -1) - logmsg(WHO,111,FATAL,"unable to set current directory"); + if (fchdir(fddir) == -1) logmsg(WHO, 111, FATAL, "unable to set current directory"); } stralloc target = {0}; @@ -91,18 +94,17 @@ stralloc address = {0}; void gotincl() { token822_reverse(&tokaddr); - if (token822_unquote(&address,&tokaddr) != 1) nomem(); + if (token822_unquote(&address, &tokaddr) != 1) nomem(); tokaddr.len = 0; - if (!address.len) - logmsg(WHO,111,FATAL,"empty :include: filenames not permitted"); - if (byte_chr(address.s,address.len,'\0') < address.len) - logmsg(WHO,111,FATAL,"NUL not permitted in :include: filenames"); + if (!address.len) logmsg(WHO, 111, FATAL, "empty :include: filenames not permitted"); + if (byte_chr(address.s, address.len, '\0') < address.len) + logmsg(WHO, 111, FATAL, "NUL not permitted in :include: filenames"); if ((address.s[0] != '.') && (address.s[0] != '/')) - if (!stralloc_cats(&instr,"./")) nomem(); - if (!stralloc_cat(&instr,&address)) nomem(); - if (!stralloc_cats(&instr,".bin")) nomem(); + if (!stralloc_cats(&instr, "./")) nomem(); + if (!stralloc_cat(&instr, &address)) nomem(); + if (!stralloc_cats(&instr, ".bin")) nomem(); if (!stralloc_0(&instr)) nomem(); } @@ -111,18 +113,16 @@ void gotaddr() int i; int j; int flaghasat; - + token822_reverse(&tokaddr); - if (token822_unquote(&address,&tokaddr) != 1) nomem(); + if (token822_unquote(&address, &tokaddr) != 1) nomem(); + + if (!address.len) logmsg(WHO, 111, FATAL, "empty recipient addresses not permitted"); - if (!address.len) - logmsg(WHO,111,FATAL,"empty recipient addresses not permitted"); - flaghasat = 0; for (i = 0; i < tokaddr.len; ++i) - if (tokaddr.t[i].type == TOKEN822_AT) - flaghasat = 1; - + if (tokaddr.t[i].type == TOKEN822_AT) flaghasat = 1; + tokaddr.len = 0; if (!address.len) return; @@ -130,49 +130,51 @@ void gotaddr() if (!flaghasat) if (address.s[0] == '/') { if (!stralloc_0(&address)) nomem(); - logmsg(WHO,111,FATAL,B("file delivery for ",address.s," not supported")); + logmsg(WHO, 111, FATAL, B("file delivery for ", address.s, " not supported")); } if (!flaghasat) if (address.s[0] == '|') { - if (byte_chr(address.s,address.len,'\0') < address.len) - logmsg(WHO,111,FATAL,"NUL not permitted in program names"); - if (!stralloc_cats(&instr,"!")) nomem(); - if (!stralloc_catb(&instr,address.s + 1,address.len - 1)) nomem(); + if (byte_chr(address.s, address.len, '\0') < address.len) + logmsg(WHO, 111, FATAL, "NUL not permitted in program names"); + if (!stralloc_cats(&instr, "!")) nomem(); + if (!stralloc_catb(&instr, address.s + 1, address.len - 1)) nomem(); if (!stralloc_0(&instr)) nomem(); return; } if (target.len) { - if (!stralloc_cats(&instr,"&")) nomem(); - if (!stralloc_cat(&instr,&fulltarget)) nomem(); + if (!stralloc_cats(&instr, "&")) nomem(); + if (!stralloc_cat(&instr, &fulltarget)) nomem(); if (!stralloc_0(&instr)) nomem(); } if (!flaghasat) - if (!stralloc_cats(&address,"@")) nomem(); + if (!stralloc_cats(&address, "@")) nomem(); - if (!stralloc_copy(&target,&address)) nomem(); - if (!stralloc_copy(&fulltarget,&address)) nomem(); + if (!stralloc_copy(&target, &address)) nomem(); + if (!stralloc_copy(&fulltarget, &address)) nomem(); if (fulltarget.s[fulltarget.len - 1] == '@') - if (!stralloc_cat(&fulltarget,&defaulthost)) nomem(); + if (!stralloc_cat(&fulltarget, &defaulthost)) nomem(); if (fulltarget.s[fulltarget.len - 1] == '+') { fulltarget.s[fulltarget.len - 1] = '.'; - if (!stralloc_cat(&fulltarget,&plusdomain)) nomem(); + if (!stralloc_cat(&fulltarget, &plusdomain)) nomem(); } j = 0; - for (i = 0;i < fulltarget.len;++i) if (fulltarget.s[i] == '@') j = i; - for (i = j;i < fulltarget.len;++i) if (fulltarget.s[i] == '.') break; + for (i = 0; i < fulltarget.len; ++i) + if (fulltarget.s[i] == '@') j = i; + for (i = j; i < fulltarget.len; ++i) + if (fulltarget.s[i] == '.') break; if (i == fulltarget.len) { - if (!stralloc_cats(&fulltarget,".")) nomem(); - if (!stralloc_cat(&fulltarget,&defaultdomain)) nomem(); + if (!stralloc_cats(&fulltarget, ".")) nomem(); + if (!stralloc_cat(&fulltarget, &defaultdomain)) nomem(); } if (fulltarget.len > 800) longaddress(); - if (byte_chr(fulltarget.s,fulltarget.len,'\0') < fulltarget.len) - logmsg(WHO,111,FATAL,"NUL not permitted in recipient addresses"); + if (byte_chr(fulltarget.s, fulltarget.len, '\0') < fulltarget.len) + logmsg(WHO, 111, FATAL, "NUL not permitted in recipient addresses"); } stralloc line = {0}; @@ -182,7 +184,7 @@ int match; void parseerr() { if (!stralloc_0(&line)) nomem(); - logmsg(WHO,111,FATAL,B("unable to parse this line: ",line.s)); + logmsg(WHO, 111, FATAL, B("unable to parse this line: ", line.s)); } void parseline() @@ -190,29 +192,28 @@ void parseline() int wordok; struct token822 *t; struct token822 *beginning; - - switch (token822_parse(&toks,&line,&cbuf)) { + + switch (token822_parse(&toks, &line, &cbuf)) { case -1: nomem(); - case 0: parseerr(); + case 0: parseerr(); } beginning = toks.t; t = toks.t + toks.len; wordok = 1; - - if (!token822_readyplus(&tokaddr,1)) nomem(); + + if (!token822_readyplus(&tokaddr, 1)) nomem(); tokaddr.len = 0; - - while (t > beginning) - switch ((--t)->type) { - case TOKEN822_SEMI: - break; /*XXX*/ + + while (t > beginning) switch ((--t)->type) + { + case TOKEN822_SEMI: break; /*XXX*/ case TOKEN822_COLON: if (t >= beginning + 2) if (t[-2].type == TOKEN822_COLON) if (t[-1].type == TOKEN822_ATOM) if (t[-1].slen == 7) - if (!byte_diff(t[-1].s,7,"include")) { + if (!byte_diff(t[-1].s, 7, "include")) { gotincl(); t -= 2; } @@ -220,18 +221,24 @@ void parseline() case TOKEN822_RIGHT: if (tokaddr.len) gotaddr(); while ((t > beginning) && (t[-1].type != TOKEN822_LEFT)) - if (!token822_append(&tokaddr,--t)) nomem(); + if (!token822_append(&tokaddr, --t)) nomem(); gotaddr(); if (t <= beginning) parseerr(); --t; - while ((t > beginning) && ((t[-1].type == TOKEN822_COMMENT) || (t[-1].type == TOKEN822_ATOM) || (t[-1].type == TOKEN822_QUOTE) || (t[-1].type == TOKEN822_AT) || (t[-1].type == TOKEN822_DOT))) + while ((t > beginning) + && ((t[-1].type == TOKEN822_COMMENT) || (t[-1].type == TOKEN822_ATOM) + || (t[-1].type == TOKEN822_QUOTE) || (t[-1].type == TOKEN822_AT) + || (t[-1].type == TOKEN822_DOT))) --t; wordok = 0; continue; - case TOKEN822_ATOM: case TOKEN822_QUOTE: case TOKEN822_LITERAL: - if (!wordok) if (tokaddr.len) gotaddr(); + case TOKEN822_ATOM: + case TOKEN822_QUOTE: + case TOKEN822_LITERAL: + if (!wordok) + if (tokaddr.len) gotaddr(); wordok = 0; - if (!token822_append(&tokaddr,t)) nomem(); + if (!token822_append(&tokaddr, t)) nomem(); continue; case TOKEN822_COMMENT: /* comment is lexically a space; shouldn't affect wordok */ @@ -242,7 +249,7 @@ void parseline() break; default: wordok = 1; - if (!token822_append(&tokaddr,t)) nomem(); + if (!token822_append(&tokaddr, t)) nomem(); continue; } if (tokaddr.len) gotaddr(); @@ -262,17 +269,17 @@ void doit() if (!target.len) parseerr(); - if (stralloc_starts(&target,"owner-")) { - if (!stralloc_copys(&key,"?")) nomem(); - if (!stralloc_catb(&key,target.s + 6,target.len - 6)) nomem(); - case_lowerb(key.s,key.len); - if (cdb_make_add(&cdb,key.s,key.len,fulltarget.s,fulltarget.len) == -1) writeerr(); + if (stralloc_starts(&target, "owner-")) { + if (!stralloc_copys(&key, "?")) nomem(); + if (!stralloc_catb(&key, target.s + 6, target.len - 6)) nomem(); + case_lowerb(key.s, key.len); + if (cdb_make_add(&cdb, key.s, key.len, fulltarget.s, fulltarget.len) == -1) writeerr(); } - if (!stralloc_copys(&key,":")) nomem(); - if (!stralloc_cat(&key,&target)) nomem(); - case_lowerb(key.s,key.len); - if (cdb_make_add(&cdb,key.s,key.len,instr.s,instr.len) == -1) writeerr(); + if (!stralloc_copys(&key, ":")) nomem(); + if (!stralloc_cat(&key, &target)) nomem(); + case_lowerb(key.s, key.len); + if (cdb_make_add(&cdb, key.s, key.len, instr.s, instr.len) == -1) writeerr(); } int main() @@ -284,43 +291,43 @@ int main() fd = open_read("/etc/aliases"); if (fd == -1) readerr(); - buffer_init(&bi,read,fd,inbuf,sizeof(inbuf)); + buffer_init(&bi, read, fd, inbuf, sizeof(inbuf)); fd = open_trunc("/etc/aliases.tmp"); - if (fd == -1) logmsg(WHO,111,FATAL,"unable to create /etc/aliases.tmp"); - if (cdb_make_start(&cdb,fd) == -1) writeerr(); + if (fd == -1) logmsg(WHO, 111, FATAL, "unable to create /etc/aliases.tmp"); + if (cdb_make_start(&cdb, fd) == -1) writeerr(); - if (!stralloc_copys(&line,"")) nomem(); + if (!stralloc_copys(&line, "")) nomem(); for (;;) { - if (getln(&bi,&newline,&match,'\n') != 0) readerr(); + if (getln(&bi, &newline, &match, '\n') != 0) readerr(); if (match && (newline.s[0] == '\n')) continue; if (match && ((newline.s[0] == ' ') || (newline.s[0] == '\t'))) { - if (!stralloc_cat(&line,&newline)) nomem(); + if (!stralloc_cat(&line, &newline)) nomem(); continue; } if (line.len) if (line.s[0] != '#') { - if (!stralloc_copys(&target,"")) nomem(); - if (!stralloc_copys(&fulltarget,"")) nomem(); - if (!stralloc_copys(&instr,"")) nomem(); + if (!stralloc_copys(&target, "")) nomem(); + if (!stralloc_copys(&fulltarget, "")) nomem(); + if (!stralloc_copys(&instr, "")) nomem(); parseline(); - doit(); + doit(); } if (!match) break; - if (!stralloc_copy(&line,&newline)) nomem(); + if (!stralloc_copy(&line, &newline)) nomem(); } if (cdb_make_finish(&cdb) == -1) writeerr(); if (fsync(fd) == -1) writeerr(); if (close(fd) == -1) writeerr(); /* NFS stupidity */ - if (rename("/etc/aliases.tmp","/etc/aliases.cdb") == -1) - logmsg(WHO,111,FATAL,"unable to move /etc/aliases.tmp to /etc/aliases.cdb"); - + if (rename("/etc/aliases.tmp", "/etc/aliases.cdb") == -1) + logmsg(WHO, 111, FATAL, "unable to move /etc/aliases.tmp to /etc/aliases.cdb"); + _exit(0); } diff --git a/src/newfield.c b/src/newfield.c index 6d69ec6..2e6b07f 100644 --- a/src/newfield.c +++ b/src/newfield.c @@ -1,9 +1,12 @@ +#include "newfield.h" + #include <unistd.h> + #include "fmt.h" -#include "datetime.h" #include "stralloc.h" + #include "date822fmt.h" -#include "newfield.h" +#include "datetime.h" /* "Date: 26 Sep 1995 04:46:53 -0000\n" */ stralloc newfield_date = {0}; @@ -15,10 +18,14 @@ static unsigned int datefmt(char *s, datetime_sec when) unsigned int i; unsigned int len; struct datetime dt; - datetime_tai(&dt,when); + datetime_tai(&dt, when); len = 0; - i = fmt_str(s,"Date: "); len += i; if (s) s += i; - i = date822fmt(s,&dt); len += i; if (s) s += i; + i = fmt_str(s, "Date: "); + len += i; + if (s) s += i; + i = date822fmt(s, &dt); + len += i; + if (s) s += i; return len; } @@ -27,33 +34,57 @@ static unsigned int msgidfmt(char *s, char *idhost, int idhostlen, datetime_sec unsigned int i; unsigned int len; struct datetime dt; - datetime_tai(&dt,when); + datetime_tai(&dt, when); len = 0; - i = fmt_str(s,"Message-ID: <"); len += i; if (s) s += i; - i = fmt_uint(s,dt.year + 1900); len += i; if (s) s += i; - i = fmt_uint0(s,dt.mon + 1,2); len += i; if (s) s += i; - i = fmt_uint0(s,dt.mday,2); len += i; if (s) s += i; - i = fmt_uint0(s,dt.hour,2); len += i; if (s) s += i; - i = fmt_uint0(s,dt.min,2); len += i; if (s) s += i; - i = fmt_uint0(s,dt.sec,2); len += i; if (s) s += i; - i = fmt_str(s,"."); len += i; if (s) s += i; - i = fmt_uint(s,getpid()); len += i; if (s) s += i; - i = fmt_str(s,".qmail@"); len += i; if (s) s += i; - i = fmt_strn(s,idhost,idhostlen); len += i; if (s) s += i; - i = fmt_str(s,">\n"); len += i; if (s) s += i; + i = fmt_str(s, "Message-ID: <"); + len += i; + if (s) s += i; + i = fmt_uint(s, dt.year + 1900); + len += i; + if (s) s += i; + i = fmt_uint0(s, dt.mon + 1, 2); + len += i; + if (s) s += i; + i = fmt_uint0(s, dt.mday, 2); + len += i; + if (s) s += i; + i = fmt_uint0(s, dt.hour, 2); + len += i; + if (s) s += i; + i = fmt_uint0(s, dt.min, 2); + len += i; + if (s) s += i; + i = fmt_uint0(s, dt.sec, 2); + len += i; + if (s) s += i; + i = fmt_str(s, "."); + len += i; + if (s) s += i; + i = fmt_uint(s, getpid()); + len += i; + if (s) s += i; + i = fmt_str(s, ".qmail@"); + len += i; + if (s) s += i; + i = fmt_strn(s, idhost, idhostlen); + len += i; + if (s) s += i; + i = fmt_str(s, ">\n"); + len += i; + if (s) s += i; return len; } int newfield_datemake(datetime_sec when) { - if (!stralloc_ready(&newfield_date,datefmt(FMT_LEN,when))) return 0; - newfield_date.len = datefmt(newfield_date.s,when); + if (!stralloc_ready(&newfield_date, datefmt(FMT_LEN, when))) return 0; + newfield_date.len = datefmt(newfield_date.s, when); return 1; } int newfield_msgidmake(char *idhost, int idhostlen, datetime_sec when) { - if (!stralloc_ready(&newfield_msgid,msgidfmt(FMT_LEN,idhost,idhostlen,when))) return 0; - newfield_msgid.len = msgidfmt(newfield_msgid.s,idhost,idhostlen,when); + if (!stralloc_ready(&newfield_msgid, msgidfmt(FMT_LEN, idhost, idhostlen, when))) return 0; + newfield_msgid.len = msgidfmt(newfield_msgid.s, idhost, idhostlen, when); return 1; } diff --git a/src/newinclude.c b/src/newinclude.c index 746b6a4..4e1bab2 100644 --- a/src/newinclude.c +++ b/src/newinclude.c @@ -1,29 +1,32 @@ #include <sys/stat.h> #include <unistd.h> -#include <stdio.h> // rename + +#include <stdio.h> // rename + #include "buffer.h" -#include "logmsg.h" +#include "byte.h" +#include "env.h" #include "genalloc.h" -#include "stralloc.h" #include "getln.h" +#include "logmsg.h" #include "open.h" -#include "token822.h" -#include "control.h" +#include "stralloc.h" + #include "auto_qmail.h" -#include "byte.h" -#include "env.h" +#include "control.h" +#include "token822.h" #define WHO "newinclude" -int rename(const char *,const char *); // stdio.h +int rename(const char *, const char *); // stdio.h void nomem() { - logmsg(WHO,111,FATAL,"out of memory"); + logmsg(WHO, 111, FATAL, "out of memory"); } void usage() { - logmsg(WHO,100,USAGE,"newinclude list"); + logmsg(WHO, 100, USAGE, "newinclude list"); } char *fnlist; @@ -40,57 +43,51 @@ buffer bt; void readerr() { - logmsg(WHO,111,FATAL,B("unable to read: ",fnlist)); + logmsg(WHO, 111, FATAL, B("unable to read: ", fnlist)); } void writeerr() { - logmsg(WHO,111,FATAL,B("unable to write to ",fntmp)); + logmsg(WHO, 111, FATAL, B("unable to write to ", fntmp)); } -static void out(char *s,int len) +static void out(char *s, int len) { - if (buffer_put(&bt,s,len) == -1) writeerr(); + if (buffer_put(&bt, s, len) == -1) writeerr(); } -void doincl(buf,len) -char *buf; +void doincl(buf, len) char *buf; int len; { - if (!len) - logmsg(WHO,111,FATAL,"empty :include: filenames not permitted"); - if (byte_chr(buf,len,'\n') != len) - logmsg(WHO,111,FATAL,"newlines not permitted in :include: filenames"); - if (byte_chr(buf,len,'\0') != len) - logmsg(WHO,111,FATAL,"NUL not permitted in :include: filenames"); - if ((buf[0] != '.') && (buf[0] != '/')) - out("./",2); - out(buf,len); - out("",1); + if (!len) logmsg(WHO, 111, FATAL, "empty :include: filenames not permitted"); + if (byte_chr(buf, len, '\n') != len) + logmsg(WHO, 111, FATAL, "newlines not permitted in :include: filenames"); + if (byte_chr(buf, len, '\0') != len) + logmsg(WHO, 111, FATAL, "NUL not permitted in :include: filenames"); + if ((buf[0] != '.') && (buf[0] != '/')) out("./", 2); + out(buf, len); + out("", 1); } -void dorecip(buf,len) -char *buf; +void dorecip(buf, len) char *buf; int len; { - if (!len) - logmsg(WHO,111,FATAL,"empty recipient addresses not permitted"); - if (byte_chr(buf,len,'\n') != len) - logmsg(WHO,111,FATAL,"newlines not permitted in recipient addresses"); - if (byte_chr(buf,len,'\0') != len) - logmsg(WHO,111,FATAL,"NUL not permitted in recipient addresses"); - if (len > 800) - logmsg(WHO,111,FATAL,"addresses must be under 800 bytes"); + if (!len) logmsg(WHO, 111, FATAL, "empty recipient addresses not permitted"); + if (byte_chr(buf, len, '\n') != len) + logmsg(WHO, 111, FATAL, "newlines not permitted in recipient addresses"); + if (byte_chr(buf, len, '\0') != len) + logmsg(WHO, 111, FATAL, "NUL not permitted in recipient addresses"); + if (len > 800) logmsg(WHO, 111, FATAL, "addresses must be under 800 bytes"); if ((buf[len - 1] == ' ') || (buf[len - 1] == '\t')) - logmsg(WHO,111,FATAL,"spaces and tabs not permitted at ends of addresses"); - out("&",1); - out(buf,len); - out("",1); + logmsg(WHO, 111, FATAL, "spaces and tabs not permitted at ends of addresses"); + out("&", 1); + out(buf, len); + out("", 1); } void die_control() { - logmsg(WHO,111,FATAL,"unable to read controls"); + logmsg(WHO, 111, FATAL, "unable to read controls"); } stralloc me = {0}; @@ -105,36 +102,40 @@ void readcontrols() char *x; fddir = open_read("."); - if (fddir == -1) - logmsg(WHO,111,FATAL,"unable to open current directory"); + if (fddir == -1) logmsg(WHO, 111, FATAL, "unable to open current directory"); - if (chdir(auto_qmail) == -1) - logmsg(WHO,111,FATAL,B("unable to chdir to: ",auto_qmail)); + if (chdir(auto_qmail) == -1) logmsg(WHO, 111, FATAL, B("unable to chdir to: ", auto_qmail)); - r = control_readline(&me,"control/me"); + r = control_readline(&me, "control/me"); if (r == -1) die_control(); - if (!r) if (!stralloc_copys(&me,"me")) nomem(); + if (!r) + if (!stralloc_copys(&me, "me")) nomem(); - r = control_readline(&defaultdomain,"control/defaultdomain"); + r = control_readline(&defaultdomain, "control/defaultdomain"); if (r == -1) die_control(); - if (!r) if (!stralloc_copy(&defaultdomain,&me)) nomem(); + if (!r) + if (!stralloc_copy(&defaultdomain, &me)) nomem(); x = env_get("QMAILDEFAULTDOMAIN"); - if (x) if (!stralloc_copys(&defaultdomain,x)) nomem(); + if (x) + if (!stralloc_copys(&defaultdomain, x)) nomem(); - r = control_readline(&defaulthost,"control/defaulthost"); + r = control_readline(&defaulthost, "control/defaulthost"); if (r == -1) die_control(); - if (!r) if (!stralloc_copy(&defaulthost,&me)) nomem(); + if (!r) + if (!stralloc_copy(&defaulthost, &me)) nomem(); x = env_get("QMAILDEFAULTHOST"); - if (x) if (!stralloc_copys(&defaulthost,x)) nomem(); + if (x) + if (!stralloc_copys(&defaulthost, x)) nomem(); - r = control_readline(&plusdomain,"control/plusdomain"); + r = control_readline(&plusdomain, "control/plusdomain"); if (r == -1) die_control(); - if (!r) if (!stralloc_copy(&plusdomain,&me)) nomem(); + if (!r) + if (!stralloc_copy(&plusdomain, &me)) nomem(); x = env_get("QMAILPLUSDOMAIN"); - if (x) if (!stralloc_copys(&plusdomain,x)) nomem(); + if (x) + if (!stralloc_copys(&plusdomain, x)) nomem(); - if (fchdir(fddir) == -1) - logmsg(WHO,111,FATAL,"unable to set current directory"); + if (fchdir(fddir) == -1) logmsg(WHO, 111, FATAL, "unable to set current directory"); } stralloc cbuf = {0}; @@ -145,9 +146,9 @@ stralloc address = {0}; void gotincl() { token822_reverse(&tokaddr); - if (token822_unquote(&address,&tokaddr) != 1) nomem(); + if (token822_unquote(&address, &tokaddr) != 1) nomem(); tokaddr.len = 0; - doincl(address.s,address.len); + doincl(address.s, address.len); } void gotaddr() @@ -155,15 +156,14 @@ void gotaddr() int i; int j; int flaghasat; - + token822_reverse(&tokaddr); - if (token822_unquote(&address,&tokaddr) != 1) nomem(); - + if (token822_unquote(&address, &tokaddr) != 1) nomem(); + flaghasat = 0; - for (i = 0;i < tokaddr.len;++i) - if (tokaddr.t[i].type == TOKEN822_AT) - flaghasat = 1; - + for (i = 0; i < tokaddr.len; ++i) + if (tokaddr.t[i].type == TOKEN822_AT) flaghasat = 1; + tokaddr.len = 0; if (!address.len) return; @@ -171,31 +171,33 @@ void gotaddr() if (!flaghasat) if (address.s[0] == '/') { if (!stralloc_0(&address)) nomem(); - logmsg(WHO,111,FATAL,B("file delivery for ",address.s," not supported")); + logmsg(WHO, 111, FATAL, B("file delivery for ", address.s, " not supported")); } if (!flaghasat) if (address.s[0] == '|') { if (!stralloc_0(&address)) nomem(); - logmsg(WHO,111,FATAL,B("program delivery for ",address.s," not supported")); + logmsg(WHO, 111, FATAL, B("program delivery for ", address.s, " not supported")); } if (!flaghasat) { - if (!stralloc_cats(&address,"@")) nomem(); - if (!stralloc_cat(&address,&defaulthost)) nomem(); + if (!stralloc_cats(&address, "@")) nomem(); + if (!stralloc_cat(&address, &defaulthost)) nomem(); } if (address.s[address.len - 1] == '+') { address.s[address.len - 1] = '.'; - if (!stralloc_cat(&address,&plusdomain)) nomem(); + if (!stralloc_cat(&address, &plusdomain)) nomem(); } j = 0; - for (i = 0;i < address.len;++i) if (address.s[i] == '@') j = i; - for (i = j;i < address.len;++i) if (address.s[i] == '.') break; + for (i = 0; i < address.len; ++i) + if (address.s[i] == '@') j = i; + for (i = j; i < address.len; ++i) + if (address.s[i] == '.') break; if (i == address.len) { - if (!stralloc_cats(&address,".")) nomem(); - if (!stralloc_cat(&address,&defaultdomain)) nomem(); + if (!stralloc_cats(&address, ".")) nomem(); + if (!stralloc_cat(&address, &defaultdomain)) nomem(); } - dorecip(address.s,address.len); + dorecip(address.s, address.len); } @@ -205,7 +207,7 @@ int match; void parseerr() { if (!stralloc_0(&line)) nomem(); - logmsg(WHO,111,FATAL,B("unable to parse this line: ",line.s)); + logmsg(WHO, 111, FATAL, B("unable to parse this line: ", line.s)); } void parseline() @@ -213,29 +215,28 @@ void parseline() int wordok; struct token822 *t; struct token822 *beginning; - - switch (token822_parse(&toks,&line,&cbuf)) { + + switch (token822_parse(&toks, &line, &cbuf)) { case -1: nomem(); - case 0: parseerr(); + case 0: parseerr(); } beginning = toks.t; t = toks.t + toks.len; wordok = 1; - - if (!token822_readyplus(&tokaddr,1)) nomem(); + + if (!token822_readyplus(&tokaddr, 1)) nomem(); tokaddr.len = 0; - - while (t > beginning) - switch ((--t)->type) { - case TOKEN822_SEMI: - break; /*XXX*/ + + while (t > beginning) switch ((--t)->type) + { + case TOKEN822_SEMI: break; /*XXX*/ case TOKEN822_COLON: if (t >= beginning + 2) if (t[-2].type == TOKEN822_COLON) if (t[-1].type == TOKEN822_ATOM) if (t[-1].slen == 7) - if (!byte_diff(t[-1].s,7,"include")) { + if (!byte_diff(t[-1].s, 7, "include")) { gotincl(); t -= 2; } @@ -243,18 +244,24 @@ void parseline() case TOKEN822_RIGHT: if (tokaddr.len) gotaddr(); while ((t > beginning) && (t[-1].type != TOKEN822_LEFT)) - if (!token822_append(&tokaddr,--t)) nomem(); + if (!token822_append(&tokaddr, --t)) nomem(); gotaddr(); if (t <= beginning) parseerr(); --t; - while ((t > beginning) && ((t[-1].type == TOKEN822_COMMENT) || (t[-1].type == TOKEN822_ATOM) || (t[-1].type == TOKEN822_QUOTE) || (t[-1].type == TOKEN822_AT) || (t[-1].type == TOKEN822_DOT))) + while ((t > beginning) + && ((t[-1].type == TOKEN822_COMMENT) || (t[-1].type == TOKEN822_ATOM) + || (t[-1].type == TOKEN822_QUOTE) || (t[-1].type == TOKEN822_AT) + || (t[-1].type == TOKEN822_DOT))) --t; wordok = 0; continue; - case TOKEN822_ATOM: case TOKEN822_QUOTE: case TOKEN822_LITERAL: - if (!wordok) if (tokaddr.len) gotaddr(); + case TOKEN822_ATOM: + case TOKEN822_QUOTE: + case TOKEN822_LITERAL: + if (!wordok) + if (tokaddr.len) gotaddr(); wordok = 0; - if (!token822_append(&tokaddr,t)) nomem(); + if (!token822_append(&tokaddr, t)) nomem(); continue; case TOKEN822_COMMENT: /* comment is lexically a space; shouldn't affect wordok */ @@ -265,14 +272,14 @@ void parseline() break; default: wordok = 1; - if (!token822_append(&tokaddr,t)) nomem(); + if (!token822_append(&tokaddr, t)) nomem(); continue; } if (tokaddr.len) gotaddr(); } - -int main(argc,argv) + +int main(argc, argv) int argc; char **argv; { @@ -281,26 +288,27 @@ char **argv; umask(033); readcontrols(); - fnlist = argv[1]; if (!fnlist) usage(); + fnlist = argv[1]; + if (!fnlist) usage(); - if (!stralloc_copys(&bin,fnlist)) nomem(); - if (!stralloc_cats(&bin,".bin")) nomem(); + if (!stralloc_copys(&bin, fnlist)) nomem(); + if (!stralloc_cats(&bin, ".bin")) nomem(); if (!stralloc_0(&bin)) nomem(); - if (!stralloc_copys(&tmp,fnlist)) nomem(); - if (!stralloc_cats(&tmp,".tmp")) nomem(); + if (!stralloc_copys(&tmp, fnlist)) nomem(); + if (!stralloc_cats(&tmp, ".tmp")) nomem(); if (!stralloc_0(&tmp)) nomem(); fd = open_read(fnlist); if (fd == -1) readerr(); - buffer_init(&bi,read,fd,listbuf,sizeof(listbuf)); + buffer_init(&bi, read, fd, listbuf, sizeof(listbuf)); fd = open_trunc(fntmp); if (fd == -1) writeerr(); - buffer_init(&bt,write,fd,tmpbuf,sizeof(tmpbuf)); + buffer_init(&bt, write, fd, tmpbuf, sizeof(tmpbuf)); for (;;) { - if (getln(&bi,&line,&match,'\n') == -1) readerr(); + if (getln(&bi, &line, &match, '\n') == -1) readerr(); if (!line.len) break; if (line.s[0] != '#') parseline(); if (!match) break; @@ -310,8 +318,7 @@ char **argv; if (fsync(fd) == -1) writeerr(); if (close(fd) == -1) writeerr(); /* NFS stupidity */ - if (rename(fntmp,fnbin) == -1) - logmsg(WHO,111,FATAL,B("unable to move ",fntmp," to: ",fnbin)); - + if (rename(fntmp, fnbin) == -1) logmsg(WHO, 111, FATAL, B("unable to move ", fntmp, " to: ", fnbin)); + _exit(0); } @@ -1,8 +1,10 @@ +#include "now.h" + #include <time.h> + #include "datetime.h" -#include "now.h" datetime_sec now() { - return time((long *) 0); + return time((long *)0); } diff --git a/src/predate.c b/src/predate.c index f6007b3..6512d20 100644 --- a/src/predate.c +++ b/src/predate.c @@ -1,20 +1,22 @@ #include <sys/types.h> -#include <time.h> #include <unistd.h> -#include "datetime.h" -#include "wait.h" + +#include <time.h> + +#include "buffer.h" +#include "exit.h" #include "fd.h" #include "fmt.h" #include "logmsg.h" -#include "buffer.h" -#include "exit.h" #include "sig.h" +#include "wait.h" + +#include "datetime.h" #define WHO "predate" static char *montab[12] = { -"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec" -}; + "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; char num[FMT_ULONG]; char outbuf[1024]; @@ -34,25 +36,21 @@ int main(int argc, char **argv) sig_pipeignore(); - if (!argv[1]) - logmsg(WHO,100,USAGE,"predate child"); + if (!argv[1]) logmsg(WHO, 100, USAGE, "predate child"); - if (pipe(pi) == -1) - logmsg(WHO,111,FATAL,"unable to create pipe"); + if (pipe(pi) == -1) logmsg(WHO, 111, FATAL, "unable to create pipe"); switch (pid = fork()) { - case -1: - logmsg(WHO,111,FATAL,"unable to fork"); + case -1: logmsg(WHO, 111, FATAL, "unable to fork"); case 0: close(pi[1]); - if (fd_move(0,pi[0]) == -1) - logmsg(WHO,111,FATAL,"unable to set up fds"); + if (fd_move(0, pi[0]) == -1) logmsg(WHO, 111, FATAL, "unable to set up fds"); sig_pipedefault(); - execvp(argv[1],argv + 1); - logmsg(WHO,111,FATAL,B("unable to run: ",argv[1])); + execvp(argv[1], argv + 1); + logmsg(WHO, 111, FATAL, B("unable to run: ", argv[1])); } close(pi[0]); - buffer_init(&bo,write,pi[1],outbuf,sizeof(outbuf)); + buffer_init(&bo, write, pi[1], outbuf, sizeof(outbuf)); time(&now); @@ -74,40 +72,37 @@ int main(int argc, char **argv) dt.sec = tm->tm_sec; local = datetime_untai(&dt); - buffer_puts(&bo,"Date: "); - buffer_put(&bo,num,fmt_uint(num,dt.mday)); - buffer_puts(&bo," "); - buffer_puts(&bo,montab[dt.mon]); - buffer_puts(&bo," "); - buffer_put(&bo,num,fmt_uint(num,dt.year + 1900)); - buffer_puts(&bo," "); - buffer_put(&bo,num,fmt_uint0(num,dt.hour,2)); - buffer_puts(&bo,":"); - buffer_put(&bo,num,fmt_uint0(num,dt.min,2)); - buffer_puts(&bo,":"); - buffer_put(&bo,num,fmt_uint0(num,dt.sec,2)); + buffer_puts(&bo, "Date: "); + buffer_put(&bo, num, fmt_uint(num, dt.mday)); + buffer_puts(&bo, " "); + buffer_puts(&bo, montab[dt.mon]); + buffer_puts(&bo, " "); + buffer_put(&bo, num, fmt_uint(num, dt.year + 1900)); + buffer_puts(&bo, " "); + buffer_put(&bo, num, fmt_uint0(num, dt.hour, 2)); + buffer_puts(&bo, ":"); + buffer_put(&bo, num, fmt_uint0(num, dt.min, 2)); + buffer_puts(&bo, ":"); + buffer_put(&bo, num, fmt_uint0(num, dt.sec, 2)); if (local < utc) { minutes = (utc - local + 30) / 60; - buffer_puts(&bo," -"); - buffer_put(&bo,num,fmt_uint0(num,minutes / 60,2)); - buffer_put(&bo,num,fmt_uint0(num,minutes % 60,2)); - } - else { + buffer_puts(&bo, " -"); + buffer_put(&bo, num, fmt_uint0(num, minutes / 60, 2)); + buffer_put(&bo, num, fmt_uint0(num, minutes % 60, 2)); + } else { minutes = (local - utc + 30) / 60; - buffer_puts(&bo," +"); - buffer_put(&bo,num,fmt_uint0(num,minutes / 60,2)); - buffer_put(&bo,num,fmt_uint0(num,minutes % 60,2)); + buffer_puts(&bo, " +"); + buffer_put(&bo, num, fmt_uint0(num, minutes / 60, 2)); + buffer_put(&bo, num, fmt_uint0(num, minutes % 60, 2)); } - buffer_puts(&bo,"\n"); - buffer_copy(&bo,buffer_0); + buffer_puts(&bo, "\n"); + buffer_copy(&bo, buffer_0); buffer_flush(&bo); close(pi[1]); - if (wait_pid(&wstat,pid) == -1) - logmsg(WHO,111,FATAL,"wait failed"); - if (wait_crashed(wstat)) - logmsg(WHO,111,FATAL,"child crashed"); + if (wait_pid(&wstat, pid) == -1) logmsg(WHO, 111, FATAL, "wait failed"); + if (wait_crashed(wstat)) logmsg(WHO, 111, FATAL, "child crashed"); _exit(wait_exitcode(wstat)); } diff --git a/src/preline.c b/src/preline.c index fb4af06..e61c588 100644 --- a/src/preline.c +++ b/src/preline.c @@ -1,28 +1,32 @@ #include <unistd.h> -#include "fd.h" + #include "buffer.h" -#include "exit.h" -#include "wait.h" #include "env.h" -#include "sig.h" +#include "exit.h" +#include "fd.h" #include "getoptb.h" #include "logmsg.h" +#include "sig.h" +#include "wait.h" #define WHO "preline" void die_usage() { - logmsg(WHO,100,USAGE,"preline cmd [ arg ... ]"); + logmsg(WHO, 100, USAGE, "preline cmd [ arg ... ]"); } -int flagufline = 1; char *ufline; -int flagrpline = 1; char *rpline; -int flagdtline = 1; char *dtline; +int flagufline = 1; +char *ufline; +int flagrpline = 1; +char *rpline; +int flagdtline = 1; +char *dtline; char outbuf[BUFFER_OUTSIZE]; -buffer bo = BUFFER_INIT(write,1,outbuf,sizeof(outbuf)); +buffer bo = BUFFER_INIT(write, 1, outbuf, sizeof(outbuf)); char inbuf[BUFFER_INSIZE]; -buffer bi = BUFFER_INIT(read,0,inbuf,sizeof(inbuf)); +buffer bi = BUFFER_INIT(read, 0, inbuf, sizeof(inbuf)); int main(int argc, char **argv) { @@ -30,57 +34,50 @@ int main(int argc, char **argv) int pi[2]; int pid; int wstat; - + sig_pipeignore(); - + if (!(ufline = env_get("UFLINE"))) die_usage(); if (!(rpline = env_get("RPLINE"))) die_usage(); if (!(dtline = env_get("DTLINE"))) die_usage(); - - while ((opt = getopt(argc,argv,"frdFRD")) != opteof) - switch (opt) { + + while ((opt = getopt(argc, argv, "frdFRD")) != opteof) switch (opt) + { case 'f': flagufline = 0; break; case 'r': flagrpline = 0; break; case 'd': flagdtline = 0; break; case 'F': flagufline = 1; break; case 'R': flagrpline = 1; break; case 'D': flagdtline = 1; break; - default: die_usage(); + default: die_usage(); } argc -= optind; argv += optind; if (!*argv) die_usage(); - - if (pipe(pi) == -1) - logmsg(WHO,111,FATAL,"unable to create pipe"); + + if (pipe(pi) == -1) logmsg(WHO, 111, FATAL, "unable to create pipe"); pid = fork(); - if (pid == -1) - logmsg(WHO,111,FATAL,"unable to fork"); + if (pid == -1) logmsg(WHO, 111, FATAL, "unable to fork"); if (pid == 0) { close(pi[1]); - if (fd_move(0,pi[0]) == -1) - logmsg(WHO,111,FATAL,"unable to set up fds"); + if (fd_move(0, pi[0]) == -1) logmsg(WHO, 111, FATAL, "unable to set up fds"); sig_pipedefault(); - execvp(*argv,argv); - logmsg(WHO,errno,FATAL,B("unable to run: ",*argv)); + execvp(*argv, argv); + logmsg(WHO, errno, FATAL, B("unable to run: ", *argv)); } close(pi[0]); - if (fd_move(1,pi[1]) == -1) - logmsg(WHO,111,FATAL,"unable to set up fds"); - - if (flagufline) buffer_puts(&bo,ufline); - if (flagrpline) buffer_puts(&bo,rpline); - if (flagdtline) buffer_puts(&bo,dtline); - if (buffer_copy(&bo,&bi) != 0) - logmsg(WHO,111,FATAL,"unable to copy input"); + if (fd_move(1, pi[1]) == -1) logmsg(WHO, 111, FATAL, "unable to set up fds"); + + if (flagufline) buffer_puts(&bo, ufline); + if (flagrpline) buffer_puts(&bo, rpline); + if (flagdtline) buffer_puts(&bo, dtline); + if (buffer_copy(&bo, &bi) != 0) logmsg(WHO, 111, FATAL, "unable to copy input"); buffer_flush(&bo); close(1); - - if (wait_pid(&wstat,pid) == -1) - logmsg(WHO,111,FATAL,"wait failed"); - if (wait_crashed(wstat)) - logmsg(WHO,111,FATAL,"child crashed"); + + if (wait_pid(&wstat, pid) == -1) logmsg(WHO, 111, FATAL, "wait failed"); + if (wait_crashed(wstat)) logmsg(WHO, 111, FATAL, "child crashed"); _exit(wait_exitcode(wstat)); } diff --git a/src/printforward.c b/src/printforward.c index 0ab7ad9..594ccad 100644 --- a/src/printforward.c +++ b/src/printforward.c @@ -1,43 +1,40 @@ #include <unistd.h> + #include "buffer.h" +#include "cdbread.h" #include "logmsg.h" #include "stralloc.h" -#include "cdbread.h" #define WHO "printmaillist" void badformat() { - logmsg(WHO,100,FATAL,"bad database format"); + logmsg(WHO, 100, FATAL, "bad database format"); } void nomem() { - logmsg(WHO,111,FATAL,"out of memory"); + logmsg(WHO, 111, FATAL, "out of memory"); } void getch(char *ch) { int r; - r = buffer_get(buffer_0small,ch,1); - if (r == -1) - logmsg(WHO,111,FATAL,"unable to read input"); - if (r == 0) - badformat(); + r = buffer_get(buffer_0small, ch, 1); + if (r == -1) logmsg(WHO, 111, FATAL, "unable to read input"); + if (r == 0) badformat(); } void out(char *ch) { - if (buffer_put(buffer_1small,ch,1) == -1) - logmsg(WHO,111,FATAL,"unable to write output"); + if (buffer_put(buffer_1small, ch, 1) == -1) logmsg(WHO, 111, FATAL, "unable to write output"); } void printbuf(char *buf) { - while (*buf) - out(buf++); + while (*buf) out(buf++); } -void printsafe(char *buf,int len) +void printsafe(char *buf, int len) { char ch; @@ -65,12 +62,10 @@ int main() int i; int j; - for (i = 0; i < 4; ++i) - getch(buf + i); + for (i = 0; i < 4; ++i) getch(buf + i); eod = cdb_unpack(buf); - for (i = 4; i < 2048; ++i) - getch(&ch); + for (i = 4; i < 2048; ++i) getch(&ch); pos = 2048; while (pos < eod) { @@ -80,33 +75,32 @@ int main() klen = cdb_unpack(buf); dlen = cdb_unpack(buf + 4); - if (!stralloc_copys(&key,"")) nomem(); + if (!stralloc_copys(&key, "")) nomem(); if (eod - pos < klen) badformat(); pos += klen; while (klen) { --klen; getch(&ch); - if (!stralloc_append(&key,&ch)) nomem(); + if (!stralloc_append(&key, &ch)) nomem(); } if (eod - pos < dlen) badformat(); pos += dlen; - if (!stralloc_copys(&data,"")) nomem(); + if (!stralloc_copys(&data, "")) nomem(); while (dlen) { --dlen; getch(&ch); - if (!stralloc_append(&data,&ch)) nomem(); + if (!stralloc_append(&data, &ch)) nomem(); } if (!key.len) badformat(); if (key.s[0] == '?') { - printsafe(key.s + 1,key.len - 1); + printsafe(key.s + 1, key.len - 1); printbuf(": ?"); - printsafe(data.s,data.len); + printsafe(data.s, data.len); printbuf(";\n"); - } - else if (key.s[0] == ':') { - printsafe(key.s + 1,key.len - 1); + } else if (key.s[0] == ':') { + printsafe(key.s + 1, key.len - 1); printbuf(":\n"); i = 0; @@ -114,29 +108,26 @@ int main() if (!data.s[j]) { if ((data.s[i] == '.') || (data.s[i] == '/')) { printbuf(", "); - printsafe(data.s + i,j - i); + printsafe(data.s + i, j - i); printbuf("\n"); - } - else if ((data.s[i] == '|') || (data.s[i] == '!')) { + } else if ((data.s[i] == '|') || (data.s[i] == '!')) { printbuf(", "); - printsafe(data.s + i,j - i); + printsafe(data.s + i, j - i); printbuf("\n"); - } - else if ((data.s[i] == '&') && (j - i < 900)) { + } else if ((data.s[i] == '&') && (j - i < 900)) { printbuf(", "); - printsafe(data.s + i,j - i); + printsafe(data.s + i, j - i); printbuf("\n"); - } - else badformat(); + } else + badformat(); i = j + 1; } if (i != j) badformat(); printbuf(";\n"); - } - else badformat(); + } else + badformat(); } - if (buffer_flush(buffer_1small) == -1) - logmsg(WHO,111,FATAL,"unable to write output"); + if (buffer_flush(buffer_1small) == -1) logmsg(WHO, 111, FATAL, "unable to write output"); _exit(0); } diff --git a/src/printmaillist.c b/src/printmaillist.c index 6edb3b3..8de8b69 100644 --- a/src/printmaillist.c +++ b/src/printmaillist.c @@ -1,15 +1,16 @@ #include <unistd.h> + #include "buffer.h" -#include "logmsg.h" -#include "stralloc.h" #include "getln.h" +#include "logmsg.h" #include "str.h" +#include "stralloc.h" #define WHO "printmaillist" void badformat() { - logmsg(WHO,100,FATAL,"bad mailing list format"); + logmsg(WHO, 100, FATAL, "bad mailing list format"); } stralloc line = {0}; @@ -18,33 +19,27 @@ int match; int main() { for (;;) { - if (getln(buffer_1small,&line,&match,'\0') == -1) - logmsg(WHO,111,FATAL,"unable to read input: "); + if (getln(buffer_1small, &line, &match, '\0') == -1) + logmsg(WHO, 111, FATAL, "unable to read input: "); if (!match) { - if (line.len) - badformat(); - if (buffer_flush(buffer_1small) == -1) - logmsg(WHO,111,FATAL,"unable to write output: "); + if (line.len) badformat(); + if (buffer_flush(buffer_1small) == -1) logmsg(WHO, 111, FATAL, "unable to write output: "); _exit(0); } - if (line.s[str_chr(line.s,'\n')]) badformat(); + if (line.s[str_chr(line.s, '\n')]) badformat(); if (line.s[line.len - 1] == ' ') badformat(); if (line.s[line.len - 1] == '\t') badformat(); if ((line.s[0] == '.') || (line.s[0] == '/')) { - if (buffer_puts(buffer_1small,line.s) == -1) - logmsg(WHO,111,FATAL,"unable to write output: "); - if (buffer_puts(buffer_1small,"\n") == -1) - logmsg(WHO,111,FATAL,"unable to write output: "); + if (buffer_puts(buffer_1small, line.s) == -1) logmsg(WHO, 111, FATAL, "unable to write output: "); + if (buffer_puts(buffer_1small, "\n") == -1) logmsg(WHO, 111, FATAL, "unable to write output: "); continue; } if (line.s[0] == '&') { if (line.len > 900) badformat(); - if (buffer_puts(buffer_1small,line.s) == -1) - logmsg(WHO,111,FATAL,"unable to write output: "); - if (buffer_puts(buffer_1small,"\n") == -1) - logmsg(WHO,111,FATAL,"unable to write output: "); + if (buffer_puts(buffer_1small, line.s) == -1) logmsg(WHO, 111, FATAL, "unable to write output: "); + if (buffer_puts(buffer_1small, "\n") == -1) logmsg(WHO, 111, FATAL, "unable to write output: "); continue; } diff --git a/src/prioq.c b/src/prioq.c index 9559d31..7cd29e8 100644 --- a/src/prioq.c +++ b/src/prioq.c @@ -1,18 +1,19 @@ +#include "prioq.h" + #include "alloc.h" #include "genalloc.h" -#include "prioq.h" -GEN_ALLOC_readyplus(prioq,struct prioq_elt,p,len,a,i,n,x,100,prioq_readyplus) +GEN_ALLOC_readyplus(prioq, struct prioq_elt, p, len, a, i, n, x, 100, prioq_readyplus) -int prioq_insert(prioq *pq, struct prioq_elt *pe) + int prioq_insert(prioq *pq, struct prioq_elt *pe) { int i; int j; - if (!prioq_readyplus(pq,1)) return 0; + if (!prioq_readyplus(pq, 1)) return 0; j = pq->len++; while (j) { - i = (j - 1)/2; + i = (j - 1) / 2; if (pq->p[i].dt <= pe->dt) break; pq->p[j] = pq->p[i]; j = i; @@ -49,6 +50,6 @@ void prioq_delmin(prioq *pq) pq->p[i] = pq->p[j]; i = j; } - pq->p[i] = pq->p[n]; - pq->len = n; + pq->p[i] = pq->p[n]; + pq->len = n; } @@ -1,21 +1,23 @@ -#include "hasshsgr.h" #include "prot.h" +#include "hasshsgr.h" + /* XXX: there are more portability problems here waiting to leap out at me */ -int prot_gid(int gid) +int prot_gid(int gid) { #ifdef HASSHORTSETGROUPS short x[2]; - x[0] = gid; x[1] = 73; /* catch errors */ - if (setgroups(1,x) == -1) return -1; + x[0] = gid; + x[1] = 73; /* catch errors */ + if (setgroups(1, x) == -1) return -1; #else - if (setgroups(1,&gid) == -1) return -1; + if (setgroups(1, &gid) == -1) return -1; #endif return setgid(gid); /* _should_ be redundant, but on some systems it isn't */ } -int prot_uid(int uid) +int prot_uid(int uid) { return setuid(uid); } diff --git a/src/qbiff.c b/src/qbiff.c index b9b55bf..186e524 100644 --- a/src/qbiff.c +++ b/src/qbiff.c @@ -1,28 +1,30 @@ -#include <sys/types.h> #include <sys/stat.h> +#include <sys/types.h> #include <unistd.h> + #include "hasutmp.h" #ifdef HASUTMP -#include <utmp.h> -#ifndef UTMP_FILE -#ifdef _PATH_UTMP -#define UTMP_FILE _PATH_UTMP -#else -#define UTMP_FILE "/etc/utmp" -#endif -#endif + #include <utmp.h> + #ifndef UTMP_FILE + #ifdef _PATH_UTMP + #define UTMP_FILE _PATH_UTMP + #else + #define UTMP_FILE "/etc/utmp" + #endif + #endif #else -#include <utmpx.h> + #include <utmpx.h> #endif -#include "stralloc.h" #include "buffer.h" -#include "open.h" #include "byte.h" +#include "env.h" +#include "exit.h" +#include "open.h" #include "str.h" +#include "stralloc.h" + #include "headerbody.h" #include "hfield.h" -#include "env.h" -#include "exit.h" buffer b; #ifdef HASUTMP @@ -46,23 +48,29 @@ stralloc text = {0}; void doit(char *s, int n) { - if (!stralloc_catb(&text,s,n)) _exit(0); + if (!stralloc_catb(&text, s, n)) _exit(0); if (text.len > 78) text.len = 78; } -void dobody(stralloc *h) { doit(h->s,h->len); } +void dobody(stralloc *h) +{ + doit(h->s, h->len); +} void doheader(stralloc *h) { int i; - if (hfield_known(h->s,h->len) == H_SUBJECT) { - i = hfield_skipname(h->s,h->len); - doit(h->s + i,h->len - i); + if (hfield_known(h->s, h->len) == H_SUBJECT) { + i = hfield_skipname(h->s, h->len); + doit(h->s + i, h->len - i); } } -void finishheader() { ; } +void finishheader() +{ + ; +} int main() { @@ -83,58 +91,65 @@ int main() if (str_len(user) > sizeof(ut->ut_user)) _exit(0); #endif - if (!stralloc_copys(&tofrom,"*** TO <")) _exit(0); - if (!stralloc_cats(&tofrom,userext)) _exit(0); - if (!stralloc_cats(&tofrom,"> FROM <")) _exit(0); - if (!stralloc_cats(&tofrom,sender)) _exit(0); - if (!stralloc_cats(&tofrom,">")) _exit(0); + if (!stralloc_copys(&tofrom, "*** TO <")) _exit(0); + if (!stralloc_cats(&tofrom, userext)) _exit(0); + if (!stralloc_cats(&tofrom, "> FROM <")) _exit(0); + if (!stralloc_cats(&tofrom, sender)) _exit(0); + if (!stralloc_cats(&tofrom, ">")) _exit(0); for (i = 0; i < tofrom.len; ++i) - if ((tofrom.s[i] < 32) || (tofrom.s[i] > 126)) - tofrom.s[i] = '_'; + if ((tofrom.s[i] < 32) || (tofrom.s[i] > 126)) tofrom.s[i] = '_'; - if (!stralloc_copys(&text," ")) _exit(0); - if (headerbody(buffer_0,doheader,finishheader,dobody) == -1) _exit(0); + if (!stralloc_copys(&text, " ")) _exit(0); + if (headerbody(buffer_0, doheader, finishheader, dobody) == -1) _exit(0); for (i = 0; i < text.len; ++i) - if ((text.s[i] < 32) || (text.s[i] > 126)) - text.s[i] = '/'; + if ((text.s[i] < 32) || (text.s[i] > 126)) text.s[i] = '/'; - if (!stralloc_copys(&woof,"\015\n\007")) _exit(0); - if (!stralloc_cat(&woof,&tofrom)) _exit(0); - if (!stralloc_cats(&woof,"\015\n")) _exit(0); - if (!stralloc_cat(&woof,&text)) _exit(0); - if (!stralloc_cats(&woof,"\015\n")) _exit(0); + if (!stralloc_copys(&woof, "\015\n\007")) _exit(0); + if (!stralloc_cat(&woof, &tofrom)) _exit(0); + if (!stralloc_cats(&woof, "\015\n")) _exit(0); + if (!stralloc_cat(&woof, &text)) _exit(0); + if (!stralloc_cats(&woof, "\015\n")) _exit(0); #ifdef HASUTMP fdutmp = open_read(UTMP_FILE); if (fdutmp == -1) _exit(0); - buffer_init(&b,read,fdutmp,bufutmp,sizeof(bufutmp)); + buffer_init(&b, read, fdutmp, bufutmp, sizeof(bufutmp)); - while (buffer_get(&b,&ut,sizeof(ut)) == sizeof(ut)) - if (!str_diffn(ut.ut_name,user,sizeof(ut.ut_name))) { + while (buffer_get(&b, &ut, sizeof(ut)) == sizeof(ut)) + if (!str_diffn(ut.ut_name, user, sizeof(ut.ut_name))) { #else while ((ut = getutxent()) != 0) - if (ut->ut_type == USER_PROCESS && !str_diffn(ut->ut_user,user,sizeof(ut->ut_user))) { + if (ut->ut_type == USER_PROCESS && !str_diffn(ut->ut_user, user, sizeof(ut->ut_user))) { #endif #ifdef HASUTMP - byte_copy(line,sizeof(ut.ut_line),ut.ut_line); + byte_copy(line, sizeof(ut.ut_line), ut.ut_line); line[sizeof(ut.ut_line)] = 0; #else - byte_copy(line,sizeof(ut->ut_line),ut->ut_line); + byte_copy(line, sizeof(ut->ut_line), ut->ut_line); line[sizeof(ut->ut_line)] = 0; #endif if (line[0] == '/') continue; if (!line[0]) continue; - if (line[str_chr(line,'.')]) continue; + if (line[str_chr(line, '.')]) continue; fdtty = open_append(line); if (fdtty == -1) continue; - if (fstat(fdtty,&st) == -1) { close(fdtty); continue; } - if (!(st.st_mode & 0100)) { close(fdtty); continue; } - if (st.st_uid != getuid()) { close(fdtty); continue; } - buffer_init(&b,write,fdtty,buftty,sizeof(buftty)); - buffer_putflush(&b,woof.s,woof.len); + if (fstat(fdtty, &st) == -1) { + close(fdtty); + continue; + } + if (!(st.st_mode & 0100)) { + close(fdtty); + continue; + } + if (st.st_uid != getuid()) { + close(fdtty); + continue; + } + buffer_init(&b, write, fdtty, buftty, sizeof(buftty)); + buffer_putflush(&b, woof.s, woof.len); close(fdtty); } _exit(0); diff --git a/src/qmail-authuser.c b/src/qmail-authuser.c index e90468f..2cdb01e 100644 --- a/src/qmail-authuser.c +++ b/src/qmail-authuser.c @@ -1,32 +1,35 @@ -#include <stdio.h> #include <unistd.h> -#include "global.h" -#include "stralloc.h" + +#include <stdio.h> + #include "buffer.h" -#include "auto_qmail.h" +#include "byte.h" #include "case.h" -#include "control.h" #include "constmap.h" -#include "str.h" -#include "fmt.h" +#include "error.h" #include "fd.h" +#include "fmt.h" #include "open.h" -#include "byte.h" +#include "pathexec.h" +#include "prot.h" #include "scan.h" -#include "md5.h" +#include "sig.h" +#include "str.h" +#include "stralloc.h" +#include "wait.h" + +#include "auto_qmail.h" +#include "control.h" +#include "global.h" #include "hmac_md5.h" +#include "md5.h" #include "sha1.h" #include "sha256.h" -#include "pathexec.h" -#include "prot.h" -#include "wait.h" -#include "sig.h" -#include "error.h" -#define FDAUTH 3 -#define FDGOSSIP 1 -#define SOCKET_CALL "-s" +#define FDAUTH 3 +#define FDGOSSIP 1 +#define SOCKET_CALL "-s" #define DOVECOT_SERVICE "-x" -#define POP_USER "qmail-pop3d" +#define POP_USER "qmail-pop3d" extern char *crypt(); #include <pwd.h> @@ -34,13 +37,13 @@ static struct passwd *pw; #include "hasspnam.h" #ifdef HASGETSPNAM -#include <shadow.h> + #include <shadow.h> static struct spwd *spw; #endif #include "hasuserpw.h" #ifdef HASUSERPW -#include <userpw.h> + #include <userpw.h> static struct userpw *upw; #endif @@ -53,12 +56,12 @@ static struct userpw *upw; */ char authbuf[512]; -buffer ba = BUFFER_INIT(write,FDAUTH,authbuf,sizeof(authbuf)); +buffer ba = BUFFER_INIT(write, FDAUTH, authbuf, sizeof(authbuf)); struct constmap mapauthuser; stralloc authfile = {0}; stralloc disabled = {0}; -stralloc user = {0}; // user w/o domain appended +stralloc user = {0}; // user w/o domain appended stralloc homedir = {0}; stralloc shell = {0}; @@ -78,11 +81,11 @@ void exit(int fail) { int i; - for (i = 0; i < sizeof(authbuf); ++i) authbuf[i] = 0; + for (i = 0; i < sizeof(authbuf); ++i) authbuf[i] = 0; _exit(fail); } -int dig_ascii(char *digascii,const char *digest,const int len) +int dig_ascii(char *digascii, const char *digest, const int len) { static const char hextab[] = "0123456789abcdef"; int j; @@ -93,56 +96,56 @@ int dig_ascii(char *digascii,const char *digest,const int len) } digascii[2 * len] = '\0'; - return (2*j); // 2*len + return (2 * j); // 2*len } -int auth_sha1(char *pwdhash,char *response) +int auth_sha1(char *pwdhash, char *response) { unsigned char digest[20]; unsigned char digascii[41]; - sha1_hash(digest,response,str_len(response)); - dig_ascii(digascii,digest,20); + sha1_hash(digest, response, str_len(response)); + dig_ascii(digascii, digest, 20); - return str_diffn(digascii,pwdhash,40); -} + return str_diffn(digascii, pwdhash, 40); +} -int auth_sha256(char *pwdhash,char *response) +int auth_sha256(char *pwdhash, char *response) { unsigned char digest[32]; unsigned char digascii[65]; - sha256_hash(digest,response,str_len(response)); - dig_ascii(digascii,digest,32); + sha256_hash(digest, response, str_len(response)); + dig_ascii(digascii, digest, 32); - return str_diffn(digascii,pwdhash,64); -} + return str_diffn(digascii, pwdhash, 64); +} -int auth_md5(char *pwdhash,char *response) -{ +int auth_md5(char *pwdhash, char *response) +{ MD5_CTX ctx; unsigned char digest[16]; unsigned char digascii[33]; MD5Init(&ctx); - MD5Update(&ctx,response,str_len(response)); - MD5Final(digest,&ctx); - dig_ascii(digascii,digest,16); + MD5Update(&ctx, response, str_len(response)); + MD5Final(digest, &ctx); + dig_ascii(digascii, digest, 16); - return str_diffn(digascii,pwdhash,32); + return str_diffn(digascii, pwdhash, 32); } -int auth_hash(char *password,char *response) +int auth_hash(char *password, char *response) { switch (str_len(password)) { - case 32: return auth_md5(password,response); - case 40: return auth_sha1(password,response); - case 64: return auth_sha256(password,response); - default: return -1; + case 32: return auth_md5(password, response); + case 40: return auth_sha1(password, response); + case 64: return auth_sha256(password, response); + default: return -1; } } -int auth_unix(char *user,char* response) +int auth_unix(char *user, char *response) { char *encrypted = 0; char *stored = 0; @@ -151,8 +154,8 @@ int auth_unix(char *user,char* response) pw = getpwnam(user); if (pw) { stored = pw->pw_passwd; - if (!stralloc_copys(&homedir,pw->pw_dir)) exit(111); - if (!stralloc_copys(&shell,pw->pw_shell)) exit(111); + if (!stralloc_copys(&homedir, pw->pw_dir)) exit(111); + if (!stralloc_copys(&shell, pw->pw_shell)) exit(111); } else { if (errno == ETXTBSY) exit(111); exit(1); @@ -163,68 +166,67 @@ int auth_unix(char *user,char* response) upw = getuserpw(user); if (upw) stored = upw->upw_passwd; - else - if (errno == ETXTBSY) exit(111); + else if (errno == ETXTBSY) + exit(111); #elif HASGETSPNAM spw = getspnam(user); if (spw) stored = spw->sp_pwdp; - else - if (errno == ETXTBSY) exit(111); + else if (errno == ETXTBSY) + exit(111); #endif if (!stored || !*stored) exit(111); - encrypted = crypt(response,stored); + encrypted = crypt(response, stored); if (!encrypted) exit(111); // no password given (tx. M.B.) - r = str_diff(encrypted,stored); + r = str_diff(encrypted, stored); } if (r == 0 || !response) { - if (prot_gid((int) pw->pw_gid) == -1) exit(1); - if (prot_uid((int) pw->pw_uid) == -1) exit(1); + if (prot_gid((int)pw->pw_gid) == -1) exit(1); + if (prot_uid((int)pw->pw_uid) == -1) exit(1); if (chdir(pw->pw_dir) == -1) exit(111); } return r; } -int auth_apop(unsigned char *password,unsigned char *response,unsigned char *challenge) +int auth_apop(unsigned char *password, unsigned char *response, unsigned char *challenge) { MD5_CTX context; unsigned char digest[16]; unsigned char digascii[33]; MD5Init(&context); - MD5Update(&context,challenge,str_len(challenge)); - MD5Update(&context,password,str_len(password)); - MD5Final(digest,&context); - dig_ascii(digascii,digest,16); + MD5Update(&context, challenge, str_len(challenge)); + MD5Update(&context, password, str_len(password)); + MD5Final(digest, &context); + dig_ascii(digascii, digest, 16); - return (str_diff(digascii,response)); + return (str_diff(digascii, response)); } -int auth_cram(unsigned char *password,unsigned char *response,unsigned char *challenge) +int auth_cram(unsigned char *password, unsigned char *response, unsigned char *challenge) { unsigned char digest[16]; unsigned char digascii[33]; - hmac_md5(challenge,str_len(challenge),password,str_len(password),digest); - dig_ascii(digascii,digest,16); + hmac_md5(challenge, str_len(challenge), password, str_len(password), digest); + dig_ascii(digascii, digest, 16); - return (str_diff(digascii,response) && str_diff(password,response)); + return (str_diff(digascii, response) && str_diff(password, response)); } -int auth_dovecot(char *user,char *response,char *socket,char *service) +int auth_dovecot(char *user, char *response, char *socket, char *service) { int wstat; int child; char *wrapper[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; int i = 0; - close(FDGOSSIP); /* gossiping doveadm */ + close(FDGOSSIP); /* gossiping doveadm */ switch (child = fork()) { - case -1: - exit(111); + case -1: exit(111); case 0: wrapper[i] = "doveadm"; wrapper[++i] = "auth"; @@ -238,56 +240,55 @@ int auth_dovecot(char *user,char *response,char *socket,char *service) wrapper[++i] = service; } wrapper[++i] = user; - wrapper[++i] = response; + wrapper[++i] = response; wrapper[++i] = 0; - execvp(wrapper[0],wrapper); + execvp(wrapper[0], wrapper); exit(111); } - if (wait_pid(&wstat,child) == -1) exit(111); + if (wait_pid(&wstat, child) == -1) exit(111); if (wait_crashed(wstat)) exit(111); - return wait_exitcode(wstat); + return wait_exitcode(wstat); } -int auth_wrapper(char *pam,char *arg1,char *arg2,char *auth,int len) +int auth_wrapper(char *pam, char *arg1, char *arg2, char *auth, int len) { int wstat; int child; int pi[2]; char *wrapper[4] = {0, 0, 0, 0}; - + if (pipe(pi) == -1) exit(111); if (pi[0] != FDAUTH) exit(111); switch (child = fork()) { - case -1: - exit(111); - case 0: + case -1: exit(111); + case 0: close(pi[1]); - if (fd_copy(FDAUTH,pi[0]) == -1) exit(111); + if (fd_copy(FDAUTH, pi[0]) == -1) exit(111); wrapper[0] = pam; wrapper[1] = arg1; wrapper[2] = arg2; wrapper[3] = 0; sig_pipedefault(); - execvp(wrapper[0],wrapper); + execvp(wrapper[0], wrapper); exit(111); } close(pi[0]); - buffer_init(&ba,write,pi[1],authbuf,sizeof(authbuf)); - if (buffer_put(&ba,auth,len) == -1) exit(111); + buffer_init(&ba, write, pi[1], authbuf, sizeof(authbuf)); + if (buffer_put(&ba, auth, len) == -1) exit(111); if (buffer_flush(&ba) == -1) exit(111); close(pi[1]); - if (wait_pid(&wstat,child) == -1) exit(111); + if (wait_pid(&wstat, child) == -1) exit(111); if (wait_crashed(wstat)) exit(111); return wait_exitcode(wstat); } -int main(int argc,char **argv) +int main(int argc, char **argv) { char *authuser; char *authpass; @@ -300,31 +301,31 @@ int main(int argc,char **argv) int rc = -1; /* initialise: -1; ok: 0; !ok: > 0 */ int authlen = 0; int buflen = 0; - int domlen = 0; + int domlen = 0; int popuser = 0; int i = 0; int r; - if (!argv[1]) + if (!argv[1]) exit(2); else if (argv[2]) { // pop user with homedir - if (!case_diffs(argv[1],POP_USER)) { + if (!case_diffs(argv[1], POP_USER)) { if (!argv[3]) exit(2); maildirname = argv[2]; - popuser = 1; + popuser = 1; } - if (!case_diffs(argv[1],SOCKET_CALL)) { // dovecot socket + if (!case_diffs(argv[1], SOCKET_CALL)) { // dovecot socket if (!argv[3]) exit(2); authsocket = argv[2]; - if (!case_diffs(argv[3],DOVECOT_SERVICE)) { // ++ dovecot service + if (!case_diffs(argv[3], DOVECOT_SERVICE)) { // ++ dovecot service service = argv[4]; if (!argv[5]) exit(2); } } - if (!case_diffs(argv[1],DOVECOT_SERVICE)) { // dovecot service + if (!case_diffs(argv[1], DOVECOT_SERVICE)) { // dovecot service if (!argv[3]) exit(2); service = argv[2]; - if (!case_diffs(argv[3],SOCKET_CALL)) { // ++ dovecot socket + if (!case_diffs(argv[3], SOCKET_CALL)) { // ++ dovecot socket if (!argv[5]) exit(2); authsocket = argv[4]; } @@ -333,9 +334,8 @@ int main(int argc,char **argv) /* Read input on FDAUTH */ - for (;;) { - do - r = read(FDAUTH,authbuf + buflen,sizeof(authbuf) - buflen); + for (;;) { + do r = read(FDAUTH, authbuf + buflen, sizeof(authbuf) - buflen); while ((r == -1) && (errno == EINTR)); if (r == -1) exit(111); if (r == 0) break; @@ -344,98 +344,106 @@ int main(int argc,char **argv) } close(FDAUTH); - authuser = authbuf + i; /* username */ + authuser = authbuf + i; /* username */ if (i == buflen) exit(2); - while (authbuf[i++]) /* response */ + while (authbuf[i++]) /* response */ if (i == buflen) exit(2); response = authbuf + i; if (i == buflen) exit(2); - while (authbuf[i++]) /* challenge */ + while (authbuf[i++]) /* challenge */ if (i == buflen) exit(2); challenge = authbuf + i; - authlen = str_len(authuser); - if (!stralloc_copyb(&user,authuser,authlen)) exit(111); + authlen = str_len(authuser); + if (!stralloc_copyb(&user, authuser, authlen)) exit(111); - if ((i = byte_rchr(authuser,authlen,'@'))) /* @domain */ + if ((i = byte_rchr(authuser, authlen, '@'))) /* @domain */ if (i < authlen && authuser[i] == '@') { domain = authuser + i; domlen = str_len(domain); - case_lowerb(domain,domlen); + case_lowerb(domain, domlen); user.len = 0; - if (!stralloc_copyb(&user,authuser,i)) exit(111); + if (!stralloc_copyb(&user, authuser, i)) exit(111); } if (!stralloc_0(&user)) exit(111); /* Read control file users/authuser and go for checks */ if (chdir(auto_qmail) == -1) exit(110); - - switch (control_readfile(&authfile,"users/authuser",0)) { + + switch (control_readfile(&authfile, "users/authuser", 0)) { case -1: exit(110); - case 0: if (!constmap_init(&mapauthuser,"",0,1)) exit(111); - case 1: if (!constmap_init(&mapauthuser,authfile.s,authfile.len,1)) exit(111); + case 0: + if (!constmap_init(&mapauthuser, "", 0, 1)) exit(111); + case 1: + if (!constmap_init(&mapauthuser, authfile.s, authfile.len, 1)) exit(111); } /* Check for disabled authuser/domains */ - if (!stralloc_copys(&disabled,"!")) exit(111); - if (!stralloc_catb(&disabled,authuser,authlen)) exit(111); - if (constmap(&mapauthuser,disabled.s,disabled.len)) exit(1); + if (!stralloc_copys(&disabled, "!")) exit(111); + if (!stralloc_catb(&disabled, authuser, authlen)) exit(111); + if (constmap(&mapauthuser, disabled.s, disabled.len)) exit(1); if (domlen) { disabled.len = 0; - if (!stralloc_copys(&disabled,"!")) exit(111); - if (!stralloc_catb(&disabled,domain,domlen)) exit(111); - if (constmap(&mapauthuser,disabled.s,disabled.len)) exit(1); + if (!stralloc_copys(&disabled, "!")) exit(111); + if (!stralloc_catb(&disabled, domain, domlen)) exit(111); + if (constmap(&mapauthuser, disabled.s, disabled.len)) exit(1); } /* Virtual and system user accounts */ - authpass = constmap(&mapauthuser,authuser,authlen); + authpass = constmap(&mapauthuser, authuser, authlen); - if (!authpass && domlen) - authpass = constmap(&mapauthuser,domain,domlen); // 1. authuser accounts - if (!authpass) - authpass = constmap(&mapauthuser,"*",1); // 2. system accounts - if (!authpass) - authpass = constmap(&mapauthuser,"@",1); // 3. virtual user accounts + if (!authpass && domlen) authpass = constmap(&mapauthuser, domain, domlen); // 1. authuser accounts + if (!authpass) authpass = constmap(&mapauthuser, "*", 1); // 2. system accounts + if (!authpass) authpass = constmap(&mapauthuser, "@", 1); // 3. virtual user accounts if (!authpass) exit(1); if (str_len(authpass) == 1) { switch (authpass[0]) { - case '?': rc = auth_unix(user.s,response); break; - case '+': if (popuser) - rc = auth_wrapper("checkvpw","qmail-pop3d",maildirname,authbuf,buflen); - else - rc = auth_wrapper("checkvpw","true","Maildir",authbuf,buflen); break; - case '&': if (popuser) - rc = auth_wrapper("vchkpw","qmail-pop3d",maildirname,authbuf,buflen); - else - rc = auth_wrapper("vchkpw","true",0,authbuf,buflen); break; - case '=': rc = auth_dovecot(authuser,response,authsocket,service); break; + case '?': rc = auth_unix(user.s, response); break; + case '+': + if (popuser) + rc = auth_wrapper("checkvpw", "qmail-pop3d", maildirname, authbuf, buflen); + else + rc = auth_wrapper("checkvpw", "true", "Maildir", authbuf, buflen); + break; + case '&': + if (popuser) + rc = auth_wrapper("vchkpw", "qmail-pop3d", maildirname, authbuf, buflen); + else + rc = auth_wrapper("vchkpw", "true", 0, authbuf, buflen); + break; + case '=': rc = auth_dovecot(authuser, response, authsocket, service); break; default: rc = 2; break; } } else { switch (authpass[0]) { - case '%': rc = auth_hash(authpass + 1,response); break; - default: if (rc) { - if (popuser) { - if ((rc = auth_apop(authpass,response,challenge)) == 0) { - auth_unix(user.s,0); // Unix environment only - } - } else rc = auth_cram(authpass,response,challenge); - } break; - } + case '%': rc = auth_hash(authpass + 1, response); break; + default: + if (rc) { + if (popuser) { + if ((rc = auth_apop(authpass, response, challenge)) == 0) { + auth_unix(user.s, 0); // Unix environment only + } + } else + rc = auth_cram(authpass, response, challenge); + } + break; + } } if (rc) exit(rc); - for (i = 0; i < sizeof(authbuf); ++i) authbuf[i] = 0; + for (i = 0; i < sizeof(authbuf); ++i) authbuf[i] = 0; if (authsocket && service) pathexec(argv + 5); - if (authsocket || service || popuser) pathexec(argv + 3); - else pathexec(argv + 1); + if (authsocket || service || popuser) + pathexec(argv + 3); + else + pathexec(argv + 1); exit(111); } diff --git a/src/qmail-badloadertypes.c b/src/qmail-badloadertypes.c index 3472fd5..779c73a 100644 --- a/src/qmail-badloadertypes.c +++ b/src/qmail-badloadertypes.c @@ -1,27 +1,30 @@ +#include <sys/stat.h> #include <unistd.h> + #include <stdlib.h> -#include <sys/stat.h> -#include "logmsg.h" -#include "stralloc.h" + #include "buffer.h" -#include "getln.h" +#include "cdbmake.h" #include "exit.h" +#include "getln.h" +#include "logmsg.h" #include "open.h" +#include "stralloc.h" + #include "auto_qmail.h" -#include "cdbmake.h" -#define WHO "qmail-badloadertypes" +#define WHO "qmail-badloadertypes" #define LOADER_LEN 5 -int rename(const char *,const char *); // stdio.h +int rename(const char *, const char *); // stdio.h void die_read() { - logmsg(WHO,111,FATAL,"unable to read control/badloadertypes"); + logmsg(WHO, 111, FATAL, "unable to read control/badloadertypes"); } void die_write() { - logmsg(WHO,111,FATAL,"unable to write to control/badloadertypes.tmp"); + logmsg(WHO, 111, FATAL, "unable to write to control/badloadertypes.tmp"); } char inbuf[1024]; @@ -37,32 +40,30 @@ int match; int main() { umask(033); - if (chdir(auto_qmail) == -1) - logmsg(WHO,111,FATAL,B("unable to chdir to: ",auto_qmail)); + if (chdir(auto_qmail) == -1) logmsg(WHO, 111, FATAL, B("unable to chdir to: ", auto_qmail)); fd = open_read("control/badloadertypes"); if (fd == -1) die_read(); - buffer_init(&b,read,fd,inbuf,sizeof(inbuf)); + buffer_init(&b, read, fd, inbuf, sizeof(inbuf)); fdtemp = open_trunc("control/badloadertypes.tmp"); if (fdtemp == -1) die_write(); - if (cdb_make_start(&cdb,fdtemp) == -1) die_write(); + if (cdb_make_start(&cdb, fdtemp) == -1) die_write(); for (;;) { - if (getln(&b,&line,&match,'\n') != 0) die_read(); + if (getln(&b, &line, &match, '\n') != 0) die_read(); if (line.s[0] != '#' && line.len > LOADER_LEN) - if (cdb_make_add(&cdb,line.s,LOADER_LEN,"",0) == -1) - die_write(); + if (cdb_make_add(&cdb, line.s, LOADER_LEN, "", 0) == -1) die_write(); if (!match) break; } if (cdb_make_finish(&cdb) == -1) die_write(); if (fsync(fdtemp) == -1) die_write(); if (close(fdtemp) == -1) die_write(); /* NFS stupidity */ - if (rename("control/badloadertypes.tmp","control/badloadertypes.cdb") == -1) - logmsg(WHO,111,FATAL,"unable to move control/badloadertypes.tmp to control/badloadertypes.cdb"); + if (rename("control/badloadertypes.tmp", "control/badloadertypes.cdb") == -1) + logmsg(WHO, 111, FATAL, "unable to move control/badloadertypes.tmp to control/badloadertypes.cdb"); _exit(0); } diff --git a/src/qmail-badmimetypes.c b/src/qmail-badmimetypes.c index a75ad4c..d56854e 100644 --- a/src/qmail-badmimetypes.c +++ b/src/qmail-badmimetypes.c @@ -1,26 +1,28 @@ -#include <unistd.h> #include <sys/stat.h> -#include "logmsg.h" -#include "stralloc.h" +#include <unistd.h> + #include "buffer.h" -#include "getln.h" +#include "cdbmake.h" #include "exit.h" +#include "getln.h" +#include "logmsg.h" #include "open.h" +#include "stralloc.h" + #include "auto_qmail.h" -#include "cdbmake.h" -#define WHO "qmail-badmimetypes" +#define WHO "qmail-badmimetypes" #define MIMETYPE_LEN 9 -int rename(const char *,const char *); // stdio.h +int rename(const char *, const char *); // stdio.h void die_read() { - logmsg(WHO,111,FATAL,"unable to read control/badmimetypes"); + logmsg(WHO, 111, FATAL, "unable to read control/badmimetypes"); } void die_write() { - logmsg(WHO,111,FATAL,"unable to write to control/badmimetypes.tmp"); + logmsg(WHO, 111, FATAL, "unable to write to control/badmimetypes.tmp"); } char inbuf[1024]; @@ -36,32 +38,30 @@ int match; int main() { umask(033); - if (chdir(auto_qmail) == -1) - logmsg(WHO,111,FATAL,B("unable to chdir to: ",auto_qmail)); + if (chdir(auto_qmail) == -1) logmsg(WHO, 111, FATAL, B("unable to chdir to: ", auto_qmail)); fd = open_read("control/badmimetypes"); if (fd == -1) die_read(); - buffer_init(&b,read,fd,inbuf,sizeof(inbuf)); + buffer_init(&b, read, fd, inbuf, sizeof(inbuf)); fdtemp = open_trunc("control/badmimetypes.tmp"); if (fdtemp == -1) die_write(); - if (cdb_make_start(&cdb,fdtemp) == -1) die_write(); + if (cdb_make_start(&cdb, fdtemp) == -1) die_write(); for (;;) { - if (getln(&b,&line,&match,'\n') != 0) die_read(); + if (getln(&b, &line, &match, '\n') != 0) die_read(); if (line.s[0] != '#' && line.len > MIMETYPE_LEN) - if (cdb_make_add(&cdb,line.s,MIMETYPE_LEN,"",0) == -1) - die_write(); + if (cdb_make_add(&cdb, line.s, MIMETYPE_LEN, "", 0) == -1) die_write(); if (!match) break; } if (cdb_make_finish(&cdb) == -1) die_write(); if (fsync(fdtemp) == -1) die_write(); if (close(fdtemp) == -1) die_write(); /* NFS stupidity */ - if (rename("control/badmimetypes.tmp","control/badmimetypes.cdb") == -1) - logmsg(WHO,111,FATAL,"unable to move control/badmimetypes.tmp to control/badmimetypes.cdb"); + if (rename("control/badmimetypes.tmp", "control/badmimetypes.cdb") == -1) + logmsg(WHO, 111, FATAL, "unable to move control/badmimetypes.tmp to control/badmimetypes.cdb"); _exit(0); } diff --git a/src/qmail-clean.c b/src/qmail-clean.c index df149a5..2a9d706 100644 --- a/src/qmail-clean.c +++ b/src/qmail-clean.c @@ -1,20 +1,22 @@ -#include <unistd.h> -#include <sys/types.h> #include <sys/stat.h> -#include "sig.h" -#include "now.h" -#include "str.h" -#include "direntry.h" -#include "getln.h" -#include "stralloc.h" +#include <sys/types.h> +#include <unistd.h> + #include "buffer.h" #include "byte.h" -#include "scan.h" -#include "fmt.h" -#include "exit.h" +#include "direntry.h" #include "error.h" -#include "fmtqfn.h" +#include "exit.h" +#include "fmt.h" +#include "getln.h" +#include "scan.h" +#include "sig.h" +#include "str.h" +#include "stralloc.h" + #include "auto_qmail.h" +#include "fmtqfn.h" +#include "now.h" #define OSSIFIED 129600 /* see qmail-send.c */ @@ -30,14 +32,14 @@ void cleanuppid() time = now(); dir = opendir("pid"); if (!dir) return; - + while ((d = readdir(dir))) { - if (str_equal(d->d_name,".")) continue; - if (str_equal(d->d_name,"..")) continue; - if (!stralloc_copys(&line,"pid/")) continue; - if (!stralloc_cats(&line,d->d_name)) continue; + if (str_equal(d->d_name, ".")) continue; + if (str_equal(d->d_name, "..")) continue; + if (!stralloc_copys(&line, "pid/")) continue; + if (!stralloc_cats(&line, d->d_name)) continue; if (!stralloc_0(&line)) continue; - if (stat(line.s,&st) == -1) continue; + if (stat(line.s, &st) == -1) continue; if (time < st.st_atime + OSSIFIED) continue; unlink(line.s); } @@ -46,9 +48,9 @@ void cleanuppid() char fnbuf[FMTQFN]; -void respond(char *s) -{ - if (buffer_putflush(buffer_1small,s,1) == -1) _exit(100); +void respond(char *s) +{ + if (buffer_putflush(buffer_1small, s, 1) == -1) _exit(100); } int main() @@ -63,37 +65,60 @@ int main() sig_pipeignore(); - if (!stralloc_ready(&line,200)) _exit(111); + if (!stralloc_ready(&line, 200)) _exit(111); cleanuploop = 0; for (;;) { - if (cleanuploop) --cleanuploop; else { cleanuppid(); cleanuploop = 30; } - if (getln(buffer_0small,&line,&match,'\0') == -1) break; + if (cleanuploop) + --cleanuploop; + else { + cleanuppid(); + cleanuploop = 30; + } + if (getln(buffer_0small, &line, &match, '\0') == -1) break; if (!match) break; - if (line.len < 7) { respond("x"); continue; } - if (line.len > 100) { respond("x"); continue; } - if (line.s[line.len - 1]) { respond("x"); continue; } /* impossible */ + if (line.len < 7) { + respond("x"); + continue; + } + if (line.len > 100) { + respond("x"); + continue; + } + if (line.s[line.len - 1]) { + respond("x"); + continue; + } /* impossible */ for (i = line.len - 2; i > 4; --i) { if (line.s[i] == '/') break; - if ((unsigned char) (line.s[i] - '0') > 9) - { respond("x"); continue; } + if ((unsigned char)(line.s[i] - '0') > 9) { + respond("x"); + continue; + } } - if (line.s[i] == '/') - if (!scan_ulong(line.s + i + 1,&id)) { respond("x"); continue; } - if (byte_equal(line.s,5,"foop/")) { -#define U(prefix,flag) fmtqfn(fnbuf,prefix,id,flag); \ - if (unlink(fnbuf) == -1) if (errno != ENOENT) { respond("!"); continue; } - U("intd/",1) - U("mess/",1) + if (line.s[i] == '/') + if (!scan_ulong(line.s + i + 1, &id)) { + respond("x"); + continue; + } + if (byte_equal(line.s, 5, "foop/")) { +#define U(prefix, flag) \ + fmtqfn(fnbuf, prefix, id, flag); \ + if (unlink(fnbuf) == -1) \ + if (errno != ENOENT) { \ + respond("!"); \ + continue; \ + } + U("intd/", 1) + U("mess/", 1) respond("+"); - } else if (byte_equal(line.s,4,"todo/")) { - U("intd/",1) - U("todo/",1) + } else if (byte_equal(line.s, 4, "todo/")) { + U("intd/", 1) + U("todo/", 1) respond("+"); - } - else + } else respond("x"); } _exit(0); diff --git a/src/qmail-dkim.cpp b/src/qmail-dkim.cpp index fba94fe..53e1b27 100644 --- a/src/qmail-dkim.cpp +++ b/src/qmail-dkim.cpp @@ -26,32 +26,36 @@ * Network: Sigh, exchanged internal DNS routines by fehQlibs resolver * *****************************************************************************/ +#include <unistd.h> + #include <stdio.h> +#include <stdlib.h> #include <string.h> #include <time.h> -#include <stdlib.h> -#include <unistd.h> + #include "dkim.h" extern "C" { #include "dns.h" } // change these to your selector name, domain name, etc -#define MYRSASELECTOR "default" -#define MYECCSELECTOR "eddy" -#define MYDOMAIN "" //"bardenhagen.com" -#define MYIDENTITY "" //"dkimtest@bardenhagen.com" +#define MYRSASELECTOR "default" +#define MYECCSELECTOR "eddy" +#define MYDOMAIN "" //"bardenhagen.com" +#define MYIDENTITY "" //"dkimtest@bardenhagen.com" -#define strnicmp strncasecmp -#define FDLOG stderr /* writing to another FD requires a method */ +#define strnicmp strncasecmp +#define FDLOG stderr /* writing to another FD requires a method */ -int DKIM_CALL SignThisHeader(const char* szHeader) +int DKIM_CALL SignThisHeader(const char *szHeader) { - if (strnicmp(szHeader,"X-",2) == 0 ) { return 0; } + if (strnicmp(szHeader, "X-", 2) == 0) { + return 0; + } return 1; } -int DKIM_CALL SelectorCallback(const char* szFQDN,char* szBuffer,int nBufLen) +int DKIM_CALL SelectorCallback(const char *szFQDN, char *szBuffer, int nBufLen) { return 0; } @@ -59,34 +63,50 @@ int DKIM_CALL SelectorCallback(const char* szFQDN,char* szBuffer,int nBufLen) void usage() { char version[] = "1.4.0"; - fprintf(FDLOG,"qmail-dkim %s \n",version); - fprintf(FDLOG,"Usage: qmail-dkim [-h|-v|-s] [tags] <msgfile> [<RSAkeyfile> <outfile> <Ed25519keyfile>]\n\n"); + fprintf(FDLOG, "qmail-dkim %s \n", version); + fprintf( + FDLOG, + "Usage: qmail-dkim [-h|-v|-s] [tags] <msgfile> [<RSAkeyfile> <outfile> <Ed25519keyfile>]\n\n"); fprintf(FDLOG, "Options:\n\t-h show this help\n"); fprintf(FDLOG, "\t-s sign the message \n"); fprintf(FDLOG, "\t-v verify the message\n"); fprintf(FDLOG, "\t-V verify the message and write result to output file (Pass/Fail)\n\n"); fprintf(FDLOG, "These tags are available:\n"); - fprintf(FDLOG, "\t-c<canonicalization> - r=relaxed [DEFAULT], s=simple, t=relaxed/simple, u=simple/relaxed\n"); - fprintf(FDLOG, "\t-d<sdid> - Signing Domain Identifier (if not provided it will be determined from the sender/from header)\n"); - fprintf(FDLOG, "\t-i<auid> - Agent User Identifier, usually the sender's email address (optional)\n"); + fprintf( + FDLOG, + "\t-c<canonicalization> - r=relaxed [DEFAULT], s=simple, t=relaxed/simple, u=simple/relaxed\n"); + fprintf( + FDLOG, + "\t-d<sdid> - Signing Domain Identifier (if not provided it will be determined from " + "the sender/from header)\n"); + fprintf( + FDLOG, + "\t-i<auid> - Agent User Identifier, usually the sender's email address " + "(optional)\n"); fprintf(FDLOG, "\t-l - include body length tag (optional)\n"); fprintf(FDLOG, "\t-q - include query method tag\n"); fprintf(FDLOG, "\t-t - include a timestamp tag (optional)\n"); - fprintf(FDLOG, "\t-x<expire_time> - the expire time in seconds since epoch (optional, DEFAULT = current time + 604800)\n"); + fprintf( + FDLOG, + "\t-x<expire_time> - the expire time in seconds since epoch (optional, DEFAULT = current " + "time + 604800)\n"); fprintf(FDLOG, "\t-y<selector> - set RSA selector (DEFAULT: default)\n"); fprintf(FDLOG, "\t-Y<selector> - set Ed25519 selector (DEFAULT: default)\n"); - fprintf(FDLOG, "\t-z<hash> - set signature algorithm type (1=rsa-sha1, 2=rsa-sha256, 3=both, 4=ed25519, 5=hybrid)\n"); + fprintf( + FDLOG, + "\t-z<hash> - set signature algorithm type (1=rsa-sha1, 2=rsa-sha256, 3=both, " + "4=ed25519, 5=hybrid)\n"); } -int main(int argc, char* argv[]) +int main(int argc, char *argv[]) { int n; - const char* RSAKeyFile = "rsa.pem"; - const char* ECCKeyFile = "ed25519.pem"; - const char* MsgFile = "test.msg"; - const char* OutFile = "signed.msg"; + const char *RSAKeyFile = "rsa.pem"; + const char *ECCKeyFile = "ed25519.pem"; + const char *MsgFile = "test.msg"; + const char *OutFile = "signed.msg"; int nKeyLen; - char RSAPrivKey[4196]; // storge for private key FILE including header and DER envelope + char RSAPrivKey[4196]; // storge for private key FILE including header and DER envelope char ECCPrivKey[128]; char Buffer[1000]; int BufLen; @@ -103,20 +123,20 @@ int main(int argc, char* argv[]) opts.nIncludeBodyLengthTag = 0; opts.nIncludeQueryMethod = 0; opts.nIncludeTimeStamp = 0; - opts.expireTime = t + 604800; // expires in 1 week - strcpy(opts.szSelector,MYRSASELECTOR); - strcpy(opts.szSelectorE,MYECCSELECTOR); - strcpy(opts.szDomain,MYDOMAIN); - strcpy(opts.szIdentity,MYIDENTITY); + opts.expireTime = t + 604800; // expires in 1 week + strcpy(opts.szSelector, MYRSASELECTOR); + strcpy(opts.szSelectorE, MYECCSELECTOR); + strcpy(opts.szDomain, MYDOMAIN); + strcpy(opts.szIdentity, MYIDENTITY); opts.pfnHeaderCallback = SignThisHeader; - strcpy(opts.szRequiredHeaders,"NonExistant"); + strcpy(opts.szRequiredHeaders, "NonExistant"); opts.nIncludeCopiedHeaders = 0; int nArgParseState = 0; bool bSign = true; bool bRes = false; - if (argc < 2){ + if (argc < 2) { usage(); exit(1); } @@ -124,92 +144,93 @@ int main(int argc, char* argv[]) for (n = 1; n < argc; n++) { if (argv[n][0] == '-' && strlen(argv[n]) > 1) { switch (argv[n][1]) { - case 'c': // canonicalization - if (argv[n][2] == 'r') { opts.nCanon = DKIM_SIGN_RELAXED; } - else if (argv[n][2] == 's') { opts.nCanon = DKIM_SIGN_SIMPLE; } - else if (argv[n][2] == 't') { opts.nCanon = DKIM_SIGN_RELAXED_SIMPLE; } - else if (argv[n][2] == 'u') { opts.nCanon = DKIM_SIGN_SIMPLE_RELAXED; } - break; - case 'd': - strncpy(opts.szDomain,(const char*)(argv[n] + 2),sizeof(opts.szDomain) - 1); + case 'c': // canonicalization + if (argv[n][2] == 'r') { + opts.nCanon = DKIM_SIGN_RELAXED; + } else if (argv[n][2] == 's') { + opts.nCanon = DKIM_SIGN_SIMPLE; + } else if (argv[n][2] == 't') { + opts.nCanon = DKIM_SIGN_RELAXED_SIMPLE; + } else if (argv[n][2] == 'u') { + opts.nCanon = DKIM_SIGN_SIMPLE_RELAXED; + } break; - case 'l': // body length tag + case 'd': strncpy(opts.szDomain, (const char *)(argv[n] + 2), sizeof(opts.szDomain) - 1); break; + case 'l': // body length tag opts.nIncludeBodyLengthTag = 1; break; - case 'h': - usage(); - return 0; - case 'i': // identity - if (argv[n][2] == '-') { opts.szIdentity[0] = '\0'; } - else { strncpy(opts.szIdentity, argv[n] + 2,sizeof(opts.szIdentity) - 1); } + case 'h': usage(); return 0; + case 'i': // identity + if (argv[n][2] == '-') { + opts.szIdentity[0] = '\0'; + } else { + strncpy(opts.szIdentity, argv[n] + 2, sizeof(opts.szIdentity) - 1); + } break; - case 'q': // query method tag + case 'q': // query method tag opts.nIncludeQueryMethod = 1; break; - case 's': // sign with and use potentially Ed25519 private key + case 's': // sign with and use potentially Ed25519 private key bSign = true; break; - case 't': // timestamp tag + case 't': // timestamp tag opts.nIncludeTimeStamp = 1; break; - case 'v': // verify + case 'v': // verify bSign = false; break; - case 'V': // verify and write result to OutFile + case 'V': // verify and write result to OutFile bSign = false; bRes = true; break; - case 'x': // expire time - if (argv[n][2] == '-') { opts.expireTime = 0; } - else { opts.expireTime = t + atoi(argv[n] + 2); } - break; - case 'y': - strncpy(opts.szSelector,argv[n] + 2,sizeof(opts.szSelector) - 1); + case 'x': // expire time + if (argv[n][2] == '-') { + opts.expireTime = 0; + } else { + opts.expireTime = t + atoi(argv[n] + 2); + } break; - case 'Y': - strncpy(opts.szSelectorE,argv[n] + 2,sizeof(opts.szSelectorE) - 1); - break; - case 'z': // sign w/ sha1, sha256, both, ed25519, hybrid + case 'y': strncpy(opts.szSelector, argv[n] + 2, sizeof(opts.szSelector) - 1); break; + case 'Y': strncpy(opts.szSelectorE, argv[n] + 2, sizeof(opts.szSelectorE) - 1); break; + case 'z': // sign w/ sha1, sha256, both, ed25519, hybrid opts.nHash = atoi(&argv[n][2]); } - } - else { + } else { switch (nArgParseState) { - case 0: - MsgFile = argv[n]; - break; - case 1: - RSAKeyFile = argv[n]; - break; - case 2: - OutFile = argv[n]; - break; - case 3: - ECCKeyFile = argv[n]; - break; + case 0: MsgFile = argv[n]; break; + case 1: RSAKeyFile = argv[n]; break; + case 2: OutFile = argv[n]; break; + case 3: ECCKeyFile = argv[n]; break; } nArgParseState++; } } - -/** Go for DKIM signing ... **/ + + /** Go for DKIM signing ... **/ if (bSign) { if (opts.nHash != 4) { - FILE* RSAPrivKeyFP = fopen(RSAKeyFile,"r"); + FILE *RSAPrivKeyFP = fopen(RSAKeyFile, "r"); if (RSAPrivKeyFP == NULL) { #ifdef SHOWLOG - fprintf(FDLOG," qmail-dkim: can't open private key file (%s) \n",RSAKeyFile); + fprintf(FDLOG, " qmail-dkim: can't open private key file (%s) \n", RSAKeyFile); #endif exit(1); - } - nKeyLen = fread(RSAPrivKey,1,sizeof(RSAPrivKey),RSAPrivKeyFP); // we read sizeof(RSAPrivKey) members with size of 1 byte each; sigh + } + nKeyLen = fread( + RSAPrivKey, + 1, + sizeof(RSAPrivKey), + RSAPrivKeyFP); // we read sizeof(RSAPrivKey) members with size of 1 byte each; sigh #ifdef SHOWLOG - fprintf(FDLOG," qmail-dkim: private key file (%s) - length %i \n",RSAKeyFile,nKeyLen); + fprintf(FDLOG, " qmail-dkim: private key file (%s) - length %i \n", RSAKeyFile, nKeyLen); #endif if (nKeyLen >= sizeof(RSAPrivKey)) { /* (TC9) on return, we get the number of members read! */ #ifdef SHOWLOG - fprintf(FDLOG," qmail-dkim: private key buffer isn't big enough for private key length %i \n",nKeyLen); + fprintf( + FDLOG, + " qmail-dkim: private key buffer isn't big enough for private key length %i \n", + nKeyLen); #endif exit(1); } @@ -217,124 +238,136 @@ int main(int argc, char* argv[]) fclose(RSAPrivKeyFP); } -/** Ed25519 signing **/ + /** Ed25519 signing **/ if (opts.nHash == 4 || opts.nHash == 5) { - FILE* ECCPrivKeyFP = fopen(ECCKeyFile,"r"); + FILE *ECCPrivKeyFP = fopen(ECCKeyFile, "r"); if (ECCPrivKeyFP == NULL) { #ifdef SHOWLOG - fprintf(FDLOG," qmail-dkim: can't open Ed25519 private key file (%s) \n",ECCKeyFile); + fprintf(FDLOG, " qmail-dkim: can't open Ed25519 private key file (%s) \n", ECCKeyFile); #endif exit(1); } - nKeyLen = fread(ECCPrivKey,1,sizeof(ECCPrivKey),ECCPrivKeyFP); + nKeyLen = fread(ECCPrivKey, 1, sizeof(ECCPrivKey), ECCPrivKeyFP); #ifdef SHOWLOG - fprintf(FDLOG," qmail-dkim: Ed25519 private key file (%s) - length %i \n",ECCKeyFile,nKeyLen); + fprintf(FDLOG, " qmail-dkim: Ed25519 private key file (%s) - length %i \n", ECCKeyFile, nKeyLen); #endif if (nKeyLen >= sizeof(ECCPrivKey)) { #ifdef SHOWLOG - fprintf(FDLOG," qmail-dkim: ECC private key buffer isn't big enough for ECC private key length %i \n",nKeyLen); + fprintf( + FDLOG, + " qmail-dkim: ECC private key buffer isn't big enough for ECC private key length %i \n", + nKeyLen); #endif exit(1); } ECCPrivKey[nKeyLen] = '\0'; fclose(ECCPrivKeyFP); - } + } -/** Input message for signing **/ + /** Input message for signing **/ - FILE* MsgFP = fopen(MsgFile,"rb"); + FILE *MsgFP = fopen(MsgFile, "rb"); if (MsgFP == NULL) { #ifdef SHOWLOG - fprintf(FDLOG," qmail-dkim: can't open msg file (%s) \n",MsgFile); + fprintf(FDLOG, " qmail-dkim: can't open msg file (%s) \n", MsgFile); #endif exit(1); } - n = DKIMSignInit(&ctxt,&opts); + n = DKIMSignInit(&ctxt, &opts); while (1) { - BufLen = fread(Buffer,1,sizeof(Buffer),MsgFP); - if (BufLen > 0) { DKIMSignProcess(&ctxt,Buffer,BufLen); } - else { break; } + BufLen = fread(Buffer, 1, sizeof(Buffer), MsgFP); + if (BufLen > 0) { + DKIMSignProcess(&ctxt, Buffer, BufLen); + } else { + break; + } } fclose(MsgFP); - char* pSig = NULL; + char *pSig = NULL; -/** Do the actual signing **/ + /** Do the actual signing **/ - n = DKIMSignGetSig2(&ctxt,RSAPrivKey,ECCPrivKey,&pSig); + n = DKIMSignGetSig2(&ctxt, RSAPrivKey, ECCPrivKey, &pSig); - strcpy(szSignature,pSig); + strcpy(szSignature, pSig); DKIMSignFree(&ctxt); - FILE* in = fopen(MsgFile,"rb"); - FILE* out = fopen(OutFile,"wb+"); + FILE *in = fopen(MsgFile, "rb"); + FILE *out = fopen(OutFile, "wb+"); #ifdef SHOWLOG - fprintf(FDLOG," outfile written %s \n",OutFile); + fprintf(FDLOG, " outfile written %s \n", OutFile); #endif - fwrite(szSignature,1,strlen(szSignature),out); - fwrite("\r\n",1,2,out); + fwrite(szSignature, 1, strlen(szSignature), out); + fwrite("\r\n", 1, 2, out); while (1) { - BufLen = fread(Buffer,1,sizeof(Buffer),in); - if (BufLen > 0) { fwrite(Buffer,1,BufLen,out); } - else { break; } + BufLen = fread(Buffer, 1, sizeof(Buffer), in); + if (BufLen > 0) { + fwrite(Buffer, 1, BufLen, out); + } else { + break; + } } fclose(in); } -/** Now go for verification **/ + /** Now go for verification **/ - else { - FILE* in = fopen(MsgFile,"rb"); + else + { + FILE *in = fopen(MsgFile, "rb"); if (in == NULL) { -//#ifdef SHOWLOG - fprintf(FDLOG," qmail-dkim: can't open input file\n"); -//#endif - return 0; // bad option -- no CTX set up yet + //#ifdef SHOWLOG + fprintf(FDLOG, " qmail-dkim: can't open input file\n"); + //#endif + return 0; // bad option -- no CTX set up yet } DKIMVerifyOptions vopts = {0}; - vopts.pfnSelectorCallback = NULL; //SelectorCallback; + vopts.pfnSelectorCallback = NULL; //SelectorCallback; - n = DKIMVerifyInit(&ctxt,&vopts); + n = DKIMVerifyInit(&ctxt, &vopts); while (1) { - BufLen = fread(Buffer,1,sizeof(Buffer),in); - if (BufLen > 0) { DKIMVerifyProcess(&ctxt,Buffer,BufLen); } - else { break; } + BufLen = fread(Buffer, 1, sizeof(Buffer), in); + if (BufLen > 0) { + DKIMVerifyProcess(&ctxt, Buffer, BufLen); + } else { + break; + } } n = DKIMVerifyResults(&ctxt); int nSigCount = 0; - DKIMVerifyDetails* pDetails; + DKIMVerifyDetails *pDetails; char szPolicy[512]; - n = DKIMVerifyGetDetails(&ctxt,&nSigCount,&pDetails,szPolicy); + n = DKIMVerifyGetDetails(&ctxt, &nSigCount, &pDetails, szPolicy); for (int i = 0; i < nSigCount; i++) { const char s[] = "pass"; const char f[] = "fail"; - const char* error = DKIM_ErrorResult(pDetails[i].nResult); - if (!bRes) - fprintf(FDLOG," Signature #%d: ",i + 1); - if (pDetails[i].nResult >= 0 ) { + const char *error = DKIM_ErrorResult(pDetails[i].nResult); + if (!bRes) fprintf(FDLOG, " Signature #%d: ", i + 1); + if (pDetails[i].nResult >= 0) { if (bRes) { - _DKIM_ReportResult(OutFile,s,0); + _DKIM_ReportResult(OutFile, s, 0); } else - printf(" Pass\n"); - } else { // fail + printf(" Pass\n"); + } else { // fail if (bRes) { - _DKIM_ReportResult(OutFile,f,error); + _DKIM_ReportResult(OutFile, f, error); } else - printf(" Fail %s \n",error); + printf(" Fail %s \n", error); } } DKIMVerifyFree(&ctxt); diff --git a/src/qmail-dksign.c b/src/qmail-dksign.c index 5135cd4..06fee37 100644 --- a/src/qmail-dksign.c +++ b/src/qmail-dksign.c @@ -1,29 +1,31 @@ -#include <sys/types.h> +#include <arpa/inet.h> +#include <netinet/in.h> #include <sys/socket.h> #include <sys/stat.h> -#include <netinet/in.h> -#include <arpa/inet.h> +#include <sys/types.h> #include <unistd.h> -#include "sig.h" -#include "stralloc.h" + #include "buffer.h" -#include "error.h" -#include "auto_qmail.h" -#include "control.h" -#include "str.h" -#include "exit.h" #include "case.h" #include "constmap.h" -#include "uint_t.h" +#include "error.h" +#include "exit.h" #include "fd.h" +#include "fmt.h" #include "logmsg.h" #include "open.h" -#include "fmt.h" -#include "fmtqfn.h" +#include "pathexec.h" #include "readwrite.h" -#include "qmail.h" +#include "sig.h" +#include "str.h" +#include "stralloc.h" +#include "uint_t.h" #include "wait.h" -#include "pathexec.h" + +#include "auto_qmail.h" +#include "control.h" +#include "fmtqfn.h" +#include "qmail.h" #include "rcpthosts.h" #define WHO "qmail-dksign" @@ -54,21 +56,42 @@ */ char bufin[1000]; // RFC 5322: 998 chars - why? -buffer bi = BUFFER_INIT(read,0,bufin,sizeof(bufin)); +buffer bi = BUFFER_INIT(read, 0, bufin, sizeof(bufin)); char bufout[1000]; -buffer bo = BUFFER_INIT(write,1,bufout,sizeof(bufout)); +buffer bo = BUFFER_INIT(write, 1, bufout, sizeof(bufout)); -void die(int e) { _exit(e); } -void die_write(char *fn) { unlink(fn); die(53); }; -void die_read() { die(54); }; -void out(char *s) { if (buffer_puts(&bo,s) == -1) _exit(111); } -void zero() { if (buffer_put(&bo,"\0",1) == -1) _exit(111); } -void zerodie() { zero(); buffer_flush(&bo); _exit(111); } +void die(int e) +{ + _exit(e); +} +void die_write(char *fn) +{ + unlink(fn); + die(53); +} +void die_read() +{ + die(54); +} +void out(char *s) +{ + if (buffer_puts(&bo, s) == -1) _exit(111); +} +void zero() +{ + if (buffer_put(&bo, "\0", 1) == -1) _exit(111); +} +void zerodie() +{ + zero(); + buffer_flush(&bo); + _exit(111); +} stralloc fndkin = {0}; stralloc fndkout = {0}; -stralloc sender = {0}; // will be re-written +stralloc sender = {0}; // will be re-written stralloc senddomain = {0}; stralloc originator = {0}; stralloc dkimdomains = {0}; @@ -78,34 +101,35 @@ stralloc ecckey = {0}; stralloc rsakey = {0}; char *dkimparams = 0; -void temp_nomem() +void temp_nomem() { out("ZOut of memory. (#4.3.0)\n"); zerodie(); } -void temp_chdir() +void temp_chdir() { out("ZUnable to switch to target directory. (#4.3.0)\n"); zerodie(); } -void temp_create() +void temp_create() { out("ZUnable to create DKIM stage file: "); out(error_str(errno)); - out(fndkin.s); out(". (#4.3.0)\n"); + out(fndkin.s); + out(". (#4.3.0)\n"); zerodie(); } -void temp_unlink() +void temp_unlink() { out("ZUnable to unlink DKIM stage file. (#4.3.0)\n"); zerodie(); } -void temp_control() +void temp_control() { out("ZUnable to read DKIM control files. (#4.3.0)\n"); zerodie(); } -void perm_usage() +void perm_usage() { out("Zqmail-dksign was invoked improperly. (#5.3.5)\n"); zerodie(); @@ -116,12 +140,12 @@ void temp_read() zerodie(); } void temp_nosignkey() -{ +{ out("DCan't read sign key: "); out(rsakey.s); out(" or "); out(ecckey.s); - out(". (#4.3.0)\n"); + out(". (#4.3.0)\n"); zerodie(); } @@ -132,46 +156,50 @@ int get_controls() if (control_init() == -1) temp_control(); - switch (control_readfile(&dkimdomains,"control/dkimdomains",0)) { + switch (control_readfile(&dkimdomains, "control/dkimdomains", 0)) { case -1: return 0; - case 0: if (!constmap_init(&mapdkimdomains,"",0,1)) temp_nomem(); break; - case 1: if (!constmap_init(&mapdkimdomains,dkimdomains.s,dkimdomains.len,1)) temp_nomem(); break; + case 0: + if (!constmap_init(&mapdkimdomains, "", 0, 1)) temp_nomem(); + break; + case 1: + if (!constmap_init(&mapdkimdomains, dkimdomains.s, dkimdomains.len, 1)) temp_nomem(); + break; } -/* Check for disabled DKIM send domains */ + /* Check for disabled DKIM send domains */ - if (!stralloc_copys(&domname,"!")) temp_nomem(); - if (!stralloc_cats(&domname,senddomain.s)) temp_nomem(); - if (constmap(&mapdkimdomains,domname.s,domname.len)) return 0; + if (!stralloc_copys(&domname, "!")) temp_nomem(); + if (!stralloc_cats(&domname, senddomain.s)) temp_nomem(); + if (constmap(&mapdkimdomains, domname.s, domname.len)) return 0; -/* Parenting domains; senddomain 0-terminated; lowercase */ + /* Parenting domains; senddomain 0-terminated; lowercase */ for (i = 0; i <= senddomain.len; ++i) { if ((i == 0) || (senddomain.s[i] == '.')) - if ((dkimparams = constmap(&mapdkimdomains,senddomain.s + i,senddomain.len - i - 1))) { - if (!stralloc_copys(&sender,senddomain.s + i)) temp_nomem(); + if ((dkimparams = constmap(&mapdkimdomains, senddomain.s + i, senddomain.len - i - 1))) { + if (!stralloc_copys(&sender, senddomain.s + i)) temp_nomem(); if (!stralloc_0(&sender)) temp_nomem(); return 3; } } -/* We sign only senddomains we take responsibility for: rcpthosts */ + /* We sign only senddomains we take responsibility for: rcpthosts */ - if ((dkimparams = constmap(&mapdkimdomains,"=",1))) { + if ((dkimparams = constmap(&mapdkimdomains, "=", 1))) { if (rcpthosts_init() == -1) temp_control(); - if (rcpthosts(originator.s,originator.len)) { - if ((control_readline(&sender,"control/defaultdomain") != 1)) - if (control_readline(&sender,"control/me") == -1) temp_control(); + if (rcpthosts(originator.s, originator.len)) { + if ((control_readline(&sender, "control/defaultdomain") != 1)) + if (control_readline(&sender, "control/me") == -1) temp_control(); if (!stralloc_0(&sender)) temp_nomem(); return 2; } } -/* Default settings for MTA: 'defaultdomain' or even 'me' */ + /* Default settings for MTA: 'defaultdomain' or even 'me' */ - if ((dkimparams = constmap(&mapdkimdomains,"*",1))) { - if ((control_readline(&sender,"control/defaultdomain") != 1)) - if (control_readline(&sender,"control/me") == -1) temp_control(); + if ((dkimparams = constmap(&mapdkimdomains, "*", 1))) { + if ((control_readline(&sender, "control/defaultdomain") != 1)) + if (control_readline(&sender, "control/me") == -1) temp_control(); if (!stralloc_0(&sender)) temp_nomem(); return 1; } @@ -179,18 +207,18 @@ int get_controls() return 0; } -void fnmake_dkim(unsigned long id) -{ - fndkin.len = fmtqfn(fndkin.s,"queue/dkim/",id,1); +void fnmake_dkim(unsigned long id) +{ + fndkin.len = fmtqfn(fndkin.s, "queue/dkim/", id, 1); id += id; - fndkout.len = fmtqfn(fndkout.s,"queue/dkim/",id,1); + fndkout.len = fmtqfn(fndkout.s, "queue/dkim/", id, 1); } void dkim_unlink() { - if (unlink(fndkin.s) == -1) + if (unlink(fndkin.s) == -1) if (errno != ENOENT) temp_unlink(); - if (unlink(fndkout.s) == -1) + if (unlink(fndkout.s) == -1) if (errno != ENOENT) temp_unlink(); } @@ -201,33 +229,33 @@ void dkim_stage() char ch; struct stat st; - if (!stralloc_ready(&fndkin,FMTQFN)) temp_nomem(); - if (!stralloc_ready(&fndkout,FMTQFN)) temp_nomem(); + if (!stralloc_ready(&fndkin, FMTQFN)) temp_nomem(); + if (!stralloc_ready(&fndkout, FMTQFN)) temp_nomem(); fnmake_dkim(getpid()); // pre-staging - dkim_unlink(); // duplicate, left over file + dkim_unlink(); // duplicate, left over file fd = open_excl(fndkin.s); if (fd == -1) die_write(fndkin.s); - buffer_init(&bi,read,0,bufin,sizeof(bufin)); - buffer_init(&bo,write,fd,bufout,sizeof(bufout)); + buffer_init(&bi, read, 0, bufin, sizeof(bufin)); + buffer_init(&bo, write, fd, bufout, sizeof(bufout)); for (;;) { - r = buffer_get(&bi,&ch,1); + r = buffer_get(&bi, &ch, 1); if (r == 0) break; if (r == -1) temp_read(); if (ch == '\r') continue; while (ch != '\n') { - buffer_put(&bo,&ch,1); - r = buffer_get(&bi,&ch,1); + buffer_put(&bo, &ch, 1); + r = buffer_get(&bi, &ch, 1); if (r == -1) temp_read(); } - buffer_put(&bo,"\r\n",2); + buffer_put(&bo, "\r\n", 2); } if (buffer_flush(&bo) == -1) die(51); - if (fstat(fd,&st) == -1) die_read(); + if (fstat(fd, &st) == -1) die_read(); if (fsync(fd) == -1) die_write(fndkin.s); if (close(fd) == -1) die_write(fndkin.s); } @@ -261,28 +289,61 @@ stralloc length = {0}; // -l -z<hash> - set signature type (1=sha1, 2=sha256, 3=both, 4=ed25519, 5=hybrid) */ -int dkim_sign(const char *rsakeyfile,const char *ecckeyfile,const char *fnin,const char *fnout) +int dkim_sign(const char *rsakeyfile, const char *ecckeyfile, const char *fnin, const char *fnout) { int child; int wstat; char *(args[17]); int i = 0; - args[i] = "qmail-dkim"; ++i; - args[i] = "-s"; ++i; - args[i] = "-q"; ++i; - if (sdid.len > 3) { args[i] = sdid.s; ++i; } - if (selector.len > 3) { args[i] = selector.s; ++i; } - if (selectore.len > 3) { args[i] = selectore.s; ++i; } - if (auid.len > 3) { args[i] = auid.s; ++i; } - if (expire.len > 3) { args[i] = expire.s; ++i; } - if (canon.len > 2) { args[i] = canon.s; ++i; } - if (hash.len > 2) { args[i] = hash.s; ++i; } - if (length.len > 2) { args[i] = length.s; ++i; } - args[i] = fnin; ++i; - args[i] = rsakeyfile; ++i; - args[i] = fnout; ++i; - if (str_len(ecckeyfile) > 3) { args[i] = ecckeyfile; ++i; } + args[i] = "qmail-dkim"; + ++i; + args[i] = "-s"; + ++i; + args[i] = "-q"; + ++i; + if (sdid.len > 3) { + args[i] = sdid.s; + ++i; + } + if (selector.len > 3) { + args[i] = selector.s; + ++i; + } + if (selectore.len > 3) { + args[i] = selectore.s; + ++i; + } + if (auid.len > 3) { + args[i] = auid.s; + ++i; + } + if (expire.len > 3) { + args[i] = expire.s; + ++i; + } + if (canon.len > 2) { + args[i] = canon.s; + ++i; + } + if (hash.len > 2) { + args[i] = hash.s; + ++i; + } + if (length.len > 2) { + args[i] = length.s; + ++i; + } + args[i] = fnin; + ++i; + args[i] = rsakeyfile; + ++i; + args[i] = fnout; + ++i; + if (str_len(ecckeyfile) > 3) { + args[i] = ecckeyfile; + ++i; + } args[i] = 0; if (!(child = vfork())) { @@ -291,16 +352,16 @@ int dkim_sign(const char *rsakeyfile,const char *ecckeyfile,const char *fnin,con _exit(100); } - wait_pid(&wstat,child); + wait_pid(&wstat, child); if (wait_crashed(wstat)) return 1; switch (wait_exitcode(wstat)) { - case 1: return 1; + case 1: return 1; default: return 0; } } -int qmail_remote(char **qargs,int fd) +int qmail_remote(char **qargs, int fd) { int child; int wstat; @@ -314,20 +375,20 @@ int qmail_remote(char **qargs,int fd) if (!(child = vfork())) { if (fd) { - if (fd_move(0,fd) == -1) _exit(111); - if (fd_copy(2,1) == -1) _exit(111); + if (fd_move(0, fd) == -1) _exit(111); + if (fd_copy(2, 1) == -1) _exit(111); } pathexec(args); if (errno) _exit(111); _exit(100); } - - wait_pid(&wstat,child); + + wait_pid(&wstat, child); if (wait_crashed(wstat)) return 1; switch (wait_exitcode(wstat)) { case 111: return 1; - default: return 0; + default: return 0; } } @@ -338,104 +399,106 @@ void dkim_setup() /* defaults: selector=default, IETF format, q=dns/txt, z=2, c=r */ - if (!stralloc_copys(&sdid,"-d")) temp_nomem(); - if (!stralloc_cat(&sdid,&sender)) temp_nomem(); + if (!stralloc_copys(&sdid, "-d")) temp_nomem(); + if (!stralloc_cat(&sdid, &sender)) temp_nomem(); if (!stralloc_0(&sdid)) temp_nomem(); - if (!stralloc_copys(&selector,"-ydefault")) temp_nomem(); + if (!stralloc_copys(&selector, "-ydefault")) temp_nomem(); if (!stralloc_0(&selector)) temp_nomem(); - if (!stralloc_copys(&selectore,"-Yeddy")) temp_nomem(); + if (!stralloc_copys(&selectore, "-Yeddy")) temp_nomem(); if (!stralloc_0(&selectore)) temp_nomem(); - if (!stralloc_copys(&canon,"-cr")) temp_nomem(); + if (!stralloc_copys(&canon, "-cr")) temp_nomem(); if (!stralloc_0(&canon)) temp_nomem(); - if (!stralloc_copys(&hash,"-z2")) temp_nomem(); + if (!stralloc_copys(&hash, "-z2")) temp_nomem(); if (!stralloc_0(&hash)) temp_nomem(); /* domain:selector,selectore|sdid|[auid|~]|expire|c:z:l; c=[r|s|t|u], z=[1,2,3,4,5], l=l */ if (dkimparams && *dkimparams) { - i = str_chr(dkimparams,'|'); + i = str_chr(dkimparams, '|'); pos = dkimparams + i; - if (*pos == '|' || *pos == '\0') { // selector + if (*pos == '|' || *pos == '\0') { // selector dkimparams[i] = '\0'; - c = str_chr(dkimparams,','); // selectore=eddy + c = str_chr(dkimparams, ','); // selectore=eddy if (dkimparams[c] == ',') { dkimparams[c] = '\0'; if (str_len(dkimparams + c + 1)) { - if (!stralloc_copys(&selectore,"-Y")) temp_nomem(); - if (!stralloc_cats(&selectore,dkimparams + c + 1)) temp_nomem(); + if (!stralloc_copys(&selectore, "-Y")) temp_nomem(); + if (!stralloc_cats(&selectore, dkimparams + c + 1)) temp_nomem(); if (!stralloc_0(&selectore)) temp_nomem(); } - } else if (str_len(dkimparams)) { // selector=default - if (!stralloc_copys(&selector,"-y")) temp_nomem(); - if (!stralloc_cats(&selector,dkimparams)) temp_nomem(); + } else if (str_len(dkimparams)) { // selector=default + if (!stralloc_copys(&selector, "-y")) temp_nomem(); + if (!stralloc_cats(&selector, dkimparams)) temp_nomem(); if (!stralloc_0(&selector)) temp_nomem(); } - j = str_chr(dkimparams + i + 1,'|'); + j = str_chr(dkimparams + i + 1, '|'); pos = dkimparams + i + j + 1; - if (*pos == '|' || *pos == '\0') { // sdid; domain in DKIM header + if (*pos == '|' || *pos == '\0') { // sdid; domain in DKIM header dkimparams[i + j + 1] = '\0'; - if (!stralloc_copys(&sdid,"-d")) temp_nomem(); - if (!stralloc_cats(&sdid,dkimparams + i + 1)) temp_nomem(); + if (!stralloc_copys(&sdid, "-d")) temp_nomem(); + if (!stralloc_cats(&sdid, dkimparams + i + 1)) temp_nomem(); if (!stralloc_0(&sdid)) temp_nomem(); - k = str_chr(dkimparams + i + j + 2,'|'); + k = str_chr(dkimparams + i + j + 2, '|'); pos = dkimparams + i + j + k + 2; - if (*pos == '|' || *pos == '\0') { // auid = identifier + if (*pos == '|' || *pos == '\0') { // auid = identifier dkimparams[i + j + k + 2] = '\0'; - if (!stralloc_copys(&auid,"-i")) temp_nomem(); + if (!stralloc_copys(&auid, "-i")) temp_nomem(); if (dkimparams[i + j + 2] == '~') { - if (!stralloc_cat(&auid,&originator)) temp_nomem(); - } else - if (!stralloc_cats(&auid,dkimparams + i + j + 2)) temp_nomem(); - + if (!stralloc_cat(&auid, &originator)) temp_nomem(); + } else if (!stralloc_cats(&auid, dkimparams + i + j + 2)) + temp_nomem(); + if (!stralloc_0(&auid)) temp_nomem(); - - l = str_chr(dkimparams + i + j + k + 3,'|'); + + l = str_chr(dkimparams + i + j + k + 3, '|'); pos = dkimparams + i + j + k + l + 3; - if (*pos == '|' || *pos == '\0') { // expire after n secs + if (*pos == '|' || *pos == '\0') { // expire after n secs dkimparams[i + j + k + l + 3] = '\0'; - if (!stralloc_copys(&expire,"-x")) temp_nomem(); - if (!stralloc_cats(&expire,dkimparams + i + j + k + 3)) temp_nomem(); + if (!stralloc_copys(&expire, "-x")) temp_nomem(); + if (!stralloc_cats(&expire, dkimparams + i + j + k + 3)) temp_nomem(); if (!stralloc_0(&expire)) temp_nomem(); - /* Options to follow */ + /* Options to follow */ opt = dkimparams + i + j + k + l + 4; if (*opt == '\0') return; - if (*opt != ':') { - if (!stralloc_copys(&canon,"-c")) temp_nomem(); // canonicalization - if (!stralloc_catb(&canon,opt,1)) temp_nomem(); + if (*opt != ':') { + if (!stralloc_copys(&canon, "-c")) temp_nomem(); // canonicalization + if (!stralloc_catb(&canon, opt, 1)) temp_nomem(); if (!stralloc_0(&canon)) temp_nomem(); - ++opt; if (*opt == '\0') return; // next colon + ++opt; + if (*opt == '\0') return; // next colon } if (*opt != ':' || *opt == '\0') return; if (*opt == ':') ++opt; if (*opt != ':') { - if (!stralloc_copys(&hash,"-z")) temp_nomem(); // hash - if (!stralloc_catb(&hash,opt,1)) temp_nomem(); + if (!stralloc_copys(&hash, "-z")) temp_nomem(); // hash + if (!stralloc_catb(&hash, opt, 1)) temp_nomem(); if (!stralloc_0(&hash)) temp_nomem(); - ++opt; if (*opt == '\0') return; // next colon + ++opt; + if (*opt == '\0') return; // next colon } if (*opt != ':' || *opt == '\0') return; if (*opt == ':') ++opt; if (*opt != ':' && *opt == 'l') { - if (!stralloc_copys(&length,"-l")) temp_nomem(); // length + if (!stralloc_copys(&length, "-l")) temp_nomem(); // length if (!stralloc_0(&length)) temp_nomem(); } } } - } - } + } + } } return; } -int main(int argc,char **args) +int main(int argc, char **args) { - int i; - int fdin = 0; // initial read from FD 0 + int i; + int fdin = 0; // initial read from FD 0 int nkey = 0; char *(qargs[4]); struct stat st; @@ -451,15 +514,15 @@ int main(int argc,char **args) if (chdir(auto_qmail) == -1) temp_chdir(); if (str_len(args[2]) > 2) { - i = str_chr(args[2],'@'); - if (*(args[2] + i) == '@') - if (!stralloc_copys(&senddomain,args[2] + i + 1)) temp_nomem(); + i = str_chr(args[2], '@'); + if (*(args[2] + i) == '@') + if (!stralloc_copys(&senddomain, args[2] + i + 1)) temp_nomem(); } - if (!stralloc_0(&senddomain)) temp_nomem(); - if (!stralloc_copys(&originator,args[2])) temp_nomem(); + if (!stralloc_0(&senddomain)) temp_nomem(); + if (!stralloc_copys(&originator, args[2])) temp_nomem(); - if (!get_controls()) { - qmail_remote(qargs,fdin); + if (!get_controls()) { + qmail_remote(qargs, fdin); _exit(0); } @@ -467,34 +530,34 @@ int main(int argc,char **args) /* Setup keys: they are composed from selector */ - case_lowerb(sender.s,sender.len); // needs to be lowercase - if (!stralloc_copys(&rsakey,DOMAINKEYS)) temp_nomem(); - if (!stralloc_cats(&rsakey,sender.s)) temp_nomem(); - if (!stralloc_cats(&rsakey,"/")) temp_nomem(); + case_lowerb(sender.s, sender.len); // needs to be lowercase + if (!stralloc_copys(&rsakey, DOMAINKEYS)) temp_nomem(); + if (!stralloc_cats(&rsakey, sender.s)) temp_nomem(); + if (!stralloc_cats(&rsakey, "/")) temp_nomem(); - if (!stralloc_copys(&ecckey,DOMAINKEYS)) temp_nomem(); - if (!stralloc_cats(&ecckey,sender.s)) temp_nomem(); - if (!stralloc_cats(&ecckey,"/")) temp_nomem(); + if (!stralloc_copys(&ecckey, DOMAINKEYS)) temp_nomem(); + if (!stralloc_cats(&ecckey, sender.s)) temp_nomem(); + if (!stralloc_cats(&ecckey, "/")) temp_nomem(); /* RSA key common for SHA1 and SHA256: rsakeyfile -> selector */ - if (!stralloc_cats(&rsakey,selector.s + 2)) temp_nomem(); // -y prepended + if (!stralloc_cats(&rsakey, selector.s + 2)) temp_nomem(); // -y prepended if (!stralloc_0(&rsakey)) temp_nomem(); - if (stat(rsakey.s,&st) != -1) + if (stat(rsakey.s, &st) != -1) if (open_read(rsakey.s) > 0) ++nkey; /* ECC key follows: ecckeyfile -> (,)selector2 */ - if (!stralloc_cats(&ecckey,selectore.s + 2)) temp_nomem(); // -Y prepended + if (!stralloc_cats(&ecckey, selectore.s + 2)) temp_nomem(); // -Y prepended if (!stralloc_0(&ecckey)) temp_nomem(); - if (stat(ecckey.s,&st) != -1) + if (stat(ecckey.s, &st) != -1) if (open_read(ecckey.s) > 0) ++nkey; /* We got keys - go for staging */ - - if (nkey) { // otherwise no key exists; why bother + + if (nkey) { // otherwise no key exists; why bother dkim_stage(); - if (!dkim_sign(rsakey.s,ecckey.s,fndkin.s,fndkout.s)) { + if (!dkim_sign(rsakey.s, ecckey.s, fndkin.s, fndkout.s)) { fdin = open_read(fndkout.s); if (fdin == -1) die_read(); } else { @@ -503,8 +566,8 @@ int main(int argc,char **args) } } else temp_nosignkey(); - - qmail_remote(qargs,fdin); // closes fdin + + qmail_remote(qargs, fdin); // closes fdin if (nkey) dkim_unlink(); _exit(0); diff --git a/src/qmail-dkverify.c b/src/qmail-dkverify.c index e607e08..3d64e51 100644 --- a/src/qmail-dkverify.c +++ b/src/qmail-dkverify.c @@ -1,30 +1,32 @@ -#include <sys/types.h> -#include <sys/stat.h> -#include <unistd.h> #include <fcntl.h> #include <sys/socket.h> -#include "sig.h" -#include "stralloc.h" +#include <sys/stat.h> +#include <sys/types.h> +#include <unistd.h> + #include "buffer.h" +#include "byte.h" +#include "case.h" +#include "env.h" #include "error.h" -#include "auto_qmail.h" -#include "str.h" #include "exit.h" -#include "uint_t.h" #include "fd.h" -#include "open.h" #include "fmt.h" -#include "fmtqfn.h" -#include "readwrite.h" #include "getln.h" -#include "qmail.h" +#include "logmsg.h" +#include "open.h" +#include "pathexec.h" +#include "readwrite.h" +#include "sig.h" +#include "str.h" +#include "stralloc.h" +#include "uint_t.h" #include "wait.h" -#include "byte.h" -#include "case.h" + +#include "auto_qmail.h" #include "control.h" -#include "pathexec.h" -#include "env.h" -#include "logmsg.h" +#include "fmtqfn.h" +#include "qmail.h" #define WHO "qmail-dkverify" @@ -41,35 +43,60 @@ */ -char bufin[1024]; // RFC 5322: 998 chars - why? -buffer bi = BUFFER_INIT(read,0,bufin,sizeof(bufin)); // read buffer +char bufin[1024]; // RFC 5322: 998 chars - why? +buffer bi = BUFFER_INIT(read, 0, bufin, sizeof(bufin)); // read buffer char bufout[1024]; -buffer bo = BUFFER_INIT(write,1,bufout,sizeof(bufout)); // output message +buffer bo = BUFFER_INIT(write, 1, bufout, sizeof(bufout)); // output message -void die(int e) { _exit(e); } -void die_pipe(char *fn) { unlink(fn); die(53); }; -void die_write(char *fn) { unlink(fn); die(53); }; -void die_read() { die(54); }; -void out(char *s) { if (buffer_puts(&bo,s) == -1) _exit(111); } -void zero() { if (buffer_put(&bo,"\0",1) == -1) _exit(111); } -void zerodie() { zero(); buffer_flush(&bo); _exit(111); } +void die(int e) +{ + _exit(e); +} +void die_pipe(char *fn) +{ + unlink(fn); + die(53); +} +void die_write(char *fn) +{ + unlink(fn); + die(53); +} +void die_read() +{ + die(54); +} +void out(char *s) +{ + if (buffer_puts(&bo, s) == -1) _exit(111); +} +void zero() +{ + if (buffer_put(&bo, "\0", 1) == -1) _exit(111); +} +void zerodie() +{ + zero(); + buffer_flush(&bo); + _exit(111); +} -void temp_nomem() +void temp_nomem() { out("ZOut of memory. (#4.3.0)\n"); zerodie(); } -void temp_chdir() +void temp_chdir() { out("ZUnable to switch to target directory. (#4.3.0)\n"); zerodie(); } -void temp_create() +void temp_create() { out("ZUnable to create DKIM stage file. (#4.3.0)\n"); zerodie(); } -void temp_unlink() +void temp_unlink() { out("ZUnable to unlink DKIM stage file. (#4.3.0)\n"); zerodie(); @@ -96,13 +123,13 @@ static stralloc dkheader = {0}; static stralloc fndkin = {0}; static stralloc fndkout = {0}; static stralloc result = {0}; - -void fnmake_dkim(unsigned long id) -{ - fndkin.len = fmtqfn(fndkin.s,"queue/dkim/",id,1); + +void fnmake_dkim(unsigned long id) +{ + fndkin.len = fmtqfn(fndkin.s, "queue/dkim/", id, 1); id += id; - fndkout.len = fmtqfn(fndkout.s,"queue/dkim/",id,1); -} + fndkout.len = fmtqfn(fndkout.s, "queue/dkim/", id, 1); +} void dkim_stage() { @@ -111,32 +138,32 @@ void dkim_stage() char ch; struct stat st; - if (!stralloc_ready(&fndkin,FMTQFN)) temp_nomem(); - if (!stralloc_ready(&fndkout,FMTQFN)) temp_nomem(); + if (!stralloc_ready(&fndkin, FMTQFN)) temp_nomem(); + if (!stralloc_ready(&fndkout, FMTQFN)) temp_nomem(); fnmake_dkim(getpid()); // pre-staging fd = open_excl(fndkin.s); if (fd == -1) die_write(fndkin.s); - buffer_init(&bi,read,0,bufin,sizeof(bufin)); - buffer_init(&bo,write,fd,bufout,sizeof(bufout)); + buffer_init(&bi, read, 0, bufin, sizeof(bufin)); + buffer_init(&bo, write, fd, bufout, sizeof(bufout)); for (int i = 0;;) { - r = buffer_get(&bi,&ch,1); + r = buffer_get(&bi, &ch, 1); if (r == 0) break; if (r == -1) temp_read(); while (ch != '\n') { - if (ch != '\r') buffer_put(&bo,&ch,1); - r = buffer_get(&bi,&ch,1); + if (ch != '\r') buffer_put(&bo, &ch, 1); + r = buffer_get(&bi, &ch, 1); if (r == -1) temp_read(); i++; } - buffer_put(&bo,"\r\n",2); + buffer_put(&bo, "\r\n", 2); } if (buffer_flush(&bo) == -1) die(51); - if (fstat(fd,&st) == -1) die_write(fndkin.s); + if (fstat(fd, &st) == -1) die_write(fndkin.s); if (fsync(fd) == -1) die_write(fndkin.s); if (close(fd) == -1) die_write(fndkin.s); } @@ -154,34 +181,34 @@ int mess_dkim() int i; fd = open_read(fndkin.s); - if (fd == -1) die_read(); - buffer_init(&bi,read,fd,bufin,sizeof(bufin)); + if (fd == -1) die_read(); + buffer_init(&bi, read, fd, bufin, sizeof(bufin)); + + if (!stralloc_copys(&senddomain, "")) temp_nomem(); - if (!stralloc_copys(&senddomain,"")) temp_nomem(); - for (;;) { - if (getln(&bi,&line,&match,'\n') == -1) temp_read(); - if (case_starts(line.s,"DKIM-Signature: ")) r = 1; - if (r == 1) { - if (case_starts(line.s,"From: ")) { // fallback: From - at = str_chr(line.s,'@'); + if (getln(&bi, &line, &match, '\n') == -1) temp_read(); + if (case_starts(line.s, "DKIM-Signature: ")) r = 1; + if (r == 1) { + if (case_starts(line.s, "From: ")) { // fallback: From + at = str_chr(line.s, '@'); if (at < line.len) { - end = str_chr(line.s,'\n'); // From: user@senddomain\n - ket = str_chr(line.s,'>'); // From: User <user@senddomain> + end = str_chr(line.s, '\n'); // From: user@senddomain\n + ket = str_chr(line.s, '>'); // From: User <user@senddomain> len = (ket < end) ? ket : end; len -= at - 1; if (len) { - if (!stralloc_copyb(&senddomain,line.s + at + 1,len)) temp_nomem(); - } else - if (!stralloc_copys(&senddomain,"uknown")) temp_nomem(); + if (!stralloc_copyb(&senddomain, line.s + at + 1, len)) temp_nomem(); + } else if (!stralloc_copys(&senddomain, "uknown")) + temp_nomem(); r = 2; } } - for (i = 0; i < line.len; ++i) { // d=domain.tld + for (i = 0; i < line.len; ++i) { // d=domain.tld if (*(line.s + i) == '=' && *(line.s + i - 1) == 'd') { - ++i; // gotcha + ++i; // gotcha while (*(line.s + i) != ';') { - if (!stralloc_catb(&senddomain,line.s + i,1)) temp_nomem(); + if (!stralloc_catb(&senddomain, line.s + i, 1)) temp_nomem(); i++; r = 3; } @@ -190,8 +217,8 @@ int mess_dkim() } if (r >= 2 || !match) break; } - if (senddomain.len < 2) - if (!stralloc_copys(&senddomain,"unknown")) temp_nomem(); + if (senddomain.len < 2) + if (!stralloc_copys(&senddomain, "unknown")) temp_nomem(); if (!stralloc_0(&senddomain)) temp_nomem(); return r; @@ -211,13 +238,13 @@ int dkim_verify() args[4] = fndkout.s; args[5] = 0; - if (!(child = fork())) { + if (!(child = fork())) { pathexec(args); if (errno) _exit(111); _exit(100); } - wait_pid(&wstat,child); + wait_pid(&wstat, child); if (wait_crashed(wstat)) return 1; switch (r = wait_exitcode(wstat)) { @@ -232,44 +259,45 @@ int dkim_result(const char *me) int fd; int j; char ch; - int r = 0; + int r = 0; - if (!stralloc_copys(&result,"")) temp_nomem(); + if (!stralloc_copys(&result, "")) temp_nomem(); - if ((fd = open_read(fndkout.s)) == -1) return 0; // nothing to read - while ((r = read(fd,bufin,sizeof(bufin))) > 0) - if (!stralloc_catb(&result,bufin,r)) temp_nomem(); + if ((fd = open_read(fndkout.s)) == -1) return 0; // nothing to read + while ((r = read(fd, bufin, sizeof(bufin))) > 0) + if (!stralloc_catb(&result, bufin, r)) temp_nomem(); if (!stralloc_0(&result)) temp_nomem(); if (result.len > 2) { - if (case_starts(result.s,"pass")) r = 0; - if (case_starts(result.s,"fail")) r = 35; - } else - if (!stralloc_copys(&result,"unknown")) temp_nomem(); + if (case_starts(result.s, "pass")) r = 0; + if (case_starts(result.s, "fail")) r = 35; + } else if (!stralloc_copys(&result, "unknown")) + temp_nomem(); - if (!stralloc_copys(&dkheader,"X-Authentication-Results: ")) temp_nomem(); - if (!stralloc_cats(&dkheader,senddomain.s)) temp_nomem(); - if (!stralloc_cats(&dkheader,"; dkim=")) temp_nomem(); + if (!stralloc_copys(&dkheader, "X-Authentication-Results: ")) temp_nomem(); + if (!stralloc_cats(&dkheader, senddomain.s)) temp_nomem(); + if (!stralloc_cats(&dkheader, "; dkim=")) temp_nomem(); for (j = 0; j < result.len; j++) { ch = result.s[j]; if (ch == '\r' || ch == '\n' || ch == '\0') continue; - if (j <= max) if (!stralloc_catb(&dkheader,&ch,1)) temp_nomem(); + if (j <= max) + if (!stralloc_catb(&dkheader, &ch, 1)) temp_nomem(); if (ch == ' ' && (j > max)) { - if (!stralloc_cats(&dkheader,"\n ")) temp_nomem(); + if (!stralloc_cats(&dkheader, "\n ")) temp_nomem(); max += max; } } - if (!stralloc_cats(&dkheader,"; ")) temp_nomem(); - if (!stralloc_cats(&dkheader,me)) temp_nomem(); + if (!stralloc_cats(&dkheader, "; ")) temp_nomem(); + if (!stralloc_cats(&dkheader, me)) temp_nomem(); if (!stralloc_0(&dkheader)) temp_nomem(); return r; } -int qmail_queue() +int qmail_queue() { int fd; int r; @@ -285,12 +313,13 @@ int qmail_queue() args[1] = 0; switch (child = vfork()) { - case -1: - close(pi[0]); close(pi[1]); - die_write(fndkin.s); - case 0: + case -1: + close(pi[0]); + close(pi[1]); + die_write(fndkin.s); + case 0: close(pi[1]); - if (fd_move(0,pi[0]) == -1) die_pipe(fndkin.s); + if (fd_move(0, pi[0]) == -1) die_pipe(fndkin.s); sig_pipedefault(); pathexec(args); if (errno) _exit(111); @@ -298,25 +327,25 @@ int qmail_queue() } close(pi[0]); - buffer_init(&bo,write,pi[1],bufout,sizeof(bufout)); + buffer_init(&bo, write, pi[1], bufout, sizeof(bufout)); - if (dkheader.len > 2) { // write DKIM header - if (buffer_put(&bo,dkheader.s,dkheader.len - 1) == -1) die_write(fndkout.s); - if (buffer_put(&bo,"\n",1) == -1) die_write(fndkout.s); + if (dkheader.len > 2) { // write DKIM header + if (buffer_put(&bo, dkheader.s, dkheader.len - 1) == -1) die_write(fndkout.s); + if (buffer_put(&bo, "\n", 1) == -1) die_write(fndkout.s); if (buffer_flush(&bo) == -1) die_write(fndkout.s); } /* read/write message byte-by-byte; we need to remove the CR (inefficient) */ if ((fd = open_read(fndkin.s)) == -1) die_read(); - while ((r = read(fd,&ch,1)) > 0) + while ((r = read(fd, &ch, 1)) > 0) if (ch != '\r') - if (buffer_put(&bo,&ch,1) == -1) die_write(fndkin.s); + if (buffer_put(&bo, &ch, 1) == -1) die_write(fndkin.s); if (buffer_flush(&bo) == -1) die_write(fndkin.s); close(pi[1]); - wait_pid(&wstat,child); + wait_pid(&wstat, child); if (wait_crashed(wstat)) return 1; switch (r = wait_exitcode(wstat)) { @@ -343,13 +372,13 @@ int main() umask(033); if (chdir(auto_qmail) == -1) temp_chdir(); if (control_init() == -1) temp_control(); - if (control_readline(&me,"control/me") == -1) temp_control(); + if (control_readline(&me, "control/me") == -1) temp_control(); if (!stralloc_0(&me)) temp_nomem(); dkim_stage(); if (mess_dkim()) { - dkim_verify(); + dkim_verify(); r = dkim_result(me.s); } diff --git a/src/qmail-getpw.c b/src/qmail-getpw.c index f801c3c..9de7ec6 100644 --- a/src/qmail-getpw.c +++ b/src/qmail-getpw.c @@ -1,16 +1,18 @@ -#include <sys/types.h> -#include <sys/stat.h> #include <pwd.h> +#include <sys/stat.h> +#include <sys/types.h> #include <unistd.h> -#include "error.h" + #include "buffer.h" -#include "exit.h" #include "byte.h" -#include "str.h" #include "case.h" +#include "error.h" +#include "exit.h" #include "fmt.h" -#include "auto_usera.h" +#include "str.h" + #include "auto_break.h" +#include "auto_usera.h" #include "qlx.h" #define GETPW_USERLEN 32 @@ -29,7 +31,7 @@ int userext() for (;;) { if (extension - local < sizeof(username)) if (!*extension || (*extension == *auto_break)) { - byte_copy(username,extension - local,local); + byte_copy(username, extension - local, local); username[extension - local] = 0; case_lowers(username); errno = 0; @@ -37,24 +39,27 @@ int userext() if (errno == ETXTBSY) _exit(QLX_SYS); if (pw) if (pw->pw_uid) - if (stat(pw->pw_dir,&st) == 0) { + if (stat(pw->pw_dir, &st) == 0) { if (st.st_uid == pw->pw_uid) { dash = ""; - if (*extension) { ++extension; dash = "-"; } + if (*extension) { + ++extension; + dash = "-"; + } return 1; } } else { if (errno) _exit(QLX_NFS); } } - if (extension == local) return 0; + if (extension == local) return 0; --extension; } } char num[FMT_ULONG]; -int main(int argc,char **argv) +int main(int argc, char **argv) { local = argv[1]; if (!local) _exit(100); @@ -67,18 +72,18 @@ int main(int argc,char **argv) if (!pw) _exit(QLX_NOALIAS); - buffer_puts(buffer_1small,pw->pw_name); - buffer_put(buffer_1small,"",1); - buffer_put(buffer_1small,num,fmt_ulong(num,(long) pw->pw_uid)); - buffer_put(buffer_1small,"",1); - buffer_put(buffer_1small,num,fmt_ulong(num,(long) pw->pw_gid)); - buffer_put(buffer_1small,"",1); - buffer_puts(buffer_1small,pw->pw_dir); - buffer_put(buffer_1small,"",1); - buffer_puts(buffer_1small,dash); - buffer_put(buffer_1small,"",1); - buffer_puts(buffer_1small,extension); - buffer_put(buffer_1small,"",1); + buffer_puts(buffer_1small, pw->pw_name); + buffer_put(buffer_1small, "", 1); + buffer_put(buffer_1small, num, fmt_ulong(num, (long)pw->pw_uid)); + buffer_put(buffer_1small, "", 1); + buffer_put(buffer_1small, num, fmt_ulong(num, (long)pw->pw_gid)); + buffer_put(buffer_1small, "", 1); + buffer_puts(buffer_1small, pw->pw_dir); + buffer_put(buffer_1small, "", 1); + buffer_puts(buffer_1small, dash); + buffer_put(buffer_1small, "", 1); + buffer_puts(buffer_1small, extension); + buffer_put(buffer_1small, "", 1); buffer_flush(buffer_1small); _exit(0); diff --git a/src/qmail-inject.c b/src/qmail-inject.c index 0071316..dd8bb21 100644 --- a/src/qmail-inject.c +++ b/src/qmail-inject.c @@ -1,26 +1,28 @@ #include <unistd.h> -#include "sig.h" + +#include "alloc.h" #include "buffer.h" +#include "constmap.h" +#include "env.h" +#include "error.h" +#include "exit.h" +#include "fmt.h" #include "genalloc.h" -#include "stralloc.h" -#include "getoptb.h" #include "getln.h" -#include "alloc.h" +#include "getoptb.h" +#include "sig.h" #include "str.h" -#include "fmt.h" -#include "hfield.h" -#include "token822.h" +#include "stralloc.h" + +#include "auto_qmail.h" #include "control.h" -#include "env.h" -#include "qmail.h" -#include "now.h" -#include "exit.h" -#include "error.h" -#include "quote.h" #include "headerbody.h" -#include "auto_qmail.h" +#include "hfield.h" #include "newfield.h" -#include "constmap.h" +#include "now.h" +#include "qmail.h" +#include "quote.h" +#include "token822.h" #define LINELEN 80 @@ -52,57 +54,68 @@ int flagrh; int flagqueue; struct qmail qqt; -void out(char *s,int len) +void out(char *s, int len) { - if (flagqueue) qmail_put(&qqt,s,len); - else buffer_putflush(buffer_1,s,len); + if (flagqueue) + qmail_put(&qqt, s, len); + else + buffer_putflush(buffer_1, s, len); } -void outs(char *s) { out(s,str_len(s)); } +void outs(char *s) +{ + out(s, str_len(s)); +} -void perm() { _exit(100); } -void temp() { _exit(111); } +void perm() +{ + _exit(100); +} +void temp() +{ + _exit(111); +} -void die_nomem() +void die_nomem() { - buffer_putsflush(buffer_2,"qmail-inject: fatal: out of memory\n"); - temp(); + buffer_putsflush(buffer_2, "qmail-inject: fatal: out of memory\n"); + temp(); } -void die_invalid(stralloc *sa) +void die_invalid(stralloc *sa) { - buffer_putsflush(buffer_2,"qmail-inject: fatal: invalid header field: "); - buffer_putflush(buffer_2,sa->s,sa->len); - perm(); + buffer_putsflush(buffer_2, "qmail-inject: fatal: invalid header field: "); + buffer_putflush(buffer_2, sa->s, sa->len); + perm(); } -void die_qqt() +void die_qqt() { - buffer_putsflush(buffer_2,"qmail-inject: fatal: unable to run qmail-queue\n"); - temp(); + buffer_putsflush(buffer_2, "qmail-inject: fatal: unable to run qmail-queue\n"); + temp(); } -void die_chdir() +void die_chdir() { - buffer_putsflush(buffer_2,"qmail-inject: fatal: internal bug\n"); - temp(); + buffer_putsflush(buffer_2, "qmail-inject: fatal: internal bug\n"); + temp(); } -void die_read() +void die_read() { if (errno == ENOMEM) die_nomem(); - buffer_putsflush(buffer_2,"qmail-inject: fatal: read error\n"); - temp(); + buffer_putsflush(buffer_2, "qmail-inject: fatal: read error\n"); + temp(); } -void doordie(stralloc *sa,int r) +void doordie(stralloc *sa, int r) { - if (r == 1) return; + if (r == 1) return; if (r == -1) die_nomem(); - buffer_putsflush(buffer_2,"qmail-inject: fatal: unable to parse this line:\n"); - buffer_putflush(buffer_2,sa->s,sa->len); - perm(); + buffer_putsflush(buffer_2, "qmail-inject: fatal: unable to parse this line:\n"); + buffer_putflush(buffer_2, sa->s, sa->len); + perm(); } -GEN_ALLOC_typedef(saa,stralloc,sa,len,a) -GEN_ALLOC_readyplus(saa,stralloc,sa,len,a,i,n,x,10,saa_readyplus) +GEN_ALLOC_typedef(saa, stralloc, sa, len, a) + GEN_ALLOC_readyplus(saa, stralloc, sa, len, a, i, n, x, 10, saa_readyplus) -static stralloc sauninit = {0}; + static stralloc sauninit = {0}; saa savedh = {0}; saa hrlist = {0}; @@ -113,60 +126,60 @@ int flagresent; void exitnicely() { - char *qqx; - - if (!flagqueue) buffer_flush(buffer_1); - - if (flagqueue) { - int i; - - if (!stralloc_0(&sender)) die_nomem(); - qmail_from(&qqt,sender.s); - - for (i = 0; i < reciplist.len; ++i) { - if (!stralloc_0(&reciplist.sa[i])) die_nomem(); - qmail_to(&qqt,reciplist.sa[i].s); - } - if (flagrh) { - if (flagresent) { - for (i = 0; i < hrrlist.len; ++i) { - if (!stralloc_0(&hrrlist.sa[i])) die_nomem(); - qmail_to(&qqt,hrrlist.sa[i].s); - } - } else { - for (i = 0; i < hrlist.len; ++i) { - if (!stralloc_0(&hrlist.sa[i])) die_nomem(); - qmail_to(&qqt,hrlist.sa[i].s); - } - } - } - - qqx = qmail_close(&qqt); - if (*qqx) { - if (*qqx == 'D') { - buffer_puts(buffer_2,"qmail-inject: fatal: "); - buffer_puts(buffer_2,qqx + 1); - buffer_puts(buffer_2,"\n"); - buffer_flush(buffer_2); - perm(); - } else { - buffer_puts(buffer_2,"qmail-inject: fatal: "); - buffer_puts(buffer_2,qqx + 1); - buffer_puts(buffer_2,"\n"); - buffer_flush(buffer_2); - temp(); - } - } - } - - _exit(0); + char *qqx; + + if (!flagqueue) buffer_flush(buffer_1); + + if (flagqueue) { + int i; + + if (!stralloc_0(&sender)) die_nomem(); + qmail_from(&qqt, sender.s); + + for (i = 0; i < reciplist.len; ++i) { + if (!stralloc_0(&reciplist.sa[i])) die_nomem(); + qmail_to(&qqt, reciplist.sa[i].s); + } + if (flagrh) { + if (flagresent) { + for (i = 0; i < hrrlist.len; ++i) { + if (!stralloc_0(&hrrlist.sa[i])) die_nomem(); + qmail_to(&qqt, hrrlist.sa[i].s); + } + } else { + for (i = 0; i < hrlist.len; ++i) { + if (!stralloc_0(&hrlist.sa[i])) die_nomem(); + qmail_to(&qqt, hrlist.sa[i].s); + } + } + } + + qqx = qmail_close(&qqt); + if (*qqx) { + if (*qqx == 'D') { + buffer_puts(buffer_2, "qmail-inject: fatal: "); + buffer_puts(buffer_2, qqx + 1); + buffer_puts(buffer_2, "\n"); + buffer_flush(buffer_2); + perm(); + } else { + buffer_puts(buffer_2, "qmail-inject: fatal: "); + buffer_puts(buffer_2, qqx + 1); + buffer_puts(buffer_2, "\n"); + buffer_flush(buffer_2); + temp(); + } + } + } + + _exit(0); } void savedh_append(stralloc *h) { - if (!saa_readyplus(&savedh,1)) die_nomem(); + if (!saa_readyplus(&savedh, 1)) die_nomem(); savedh.sa[savedh.len] = sauninit; - if (!stralloc_copy(savedh.sa + savedh.len,h)) die_nomem(); + if (!stralloc_copy(savedh.sa + savedh.len, h)) die_nomem(); ++savedh.len; } @@ -174,8 +187,7 @@ void savedh_print() { int i; - for (i = 0; i < savedh.len; ++i) - out(savedh.sa[i].s,savedh.sa[i].len); + for (i = 0; i < savedh.len; ++i) out(savedh.sa[i].s, savedh.sa[i].len); } stralloc defaultdomainbuf = {0}; @@ -189,8 +201,7 @@ void rwroute(token822_alloc *addr) { if (addr->t[addr->len - 1].type == TOKEN822_AT) while (addr->len) - if (addr->t[--addr->len].type == TOKEN822_COLON) - return; + if (addr->t[--addr->len].type == TOKEN822_COLON) return; } void rwextraat(token822_alloc *addr) @@ -199,9 +210,8 @@ void rwextraat(token822_alloc *addr) if (addr->t[0].type == TOKEN822_AT) { --addr->len; - for (i = 0; i < addr->len; ++i) - addr->t[i] = addr->t[i + 1]; - } + for (i = 0; i < addr->len; ++i) addr->t[i] = addr->t[i + 1]; + } } void rwextradot(token822_alloc *addr) @@ -210,8 +220,7 @@ void rwextradot(token822_alloc *addr) if (addr->t[0].type == TOKEN822_DOT) { --addr->len; - for (i = 0; i < addr->len; ++i) - addr->t[i] = addr->t[i + 1]; + for (i = 0; i < addr->len; ++i) addr->t[i] = addr->t[i + 1]; } } @@ -221,17 +230,14 @@ void rwnoat(token822_alloc *addr) int shift; for (i = 0; i < addr->len; ++i) - if (addr->t[i].type == TOKEN822_AT) - return; + if (addr->t[i].type == TOKEN822_AT) return; shift = defaulthost.len; - if (!token822_readyplus(addr,shift)) die_nomem(); - for (i = addr->len - 1; i >= 0; --i) - addr->t[i + shift] = addr->t[i]; + if (!token822_readyplus(addr, shift)) die_nomem(); + for (i = addr->len - 1; i >= 0; --i) addr->t[i + shift] = addr->t[i]; addr->len += shift; - for (i = 0; i < shift; ++i) - addr->t[i] = defaulthost.t[shift - 1 - i]; + for (i = 0; i < shift; ++i) addr->t[i] = defaulthost.t[shift - 1 - i]; } void rwnodot(token822_alloc *addr) @@ -240,27 +246,21 @@ void rwnodot(token822_alloc *addr) int shift; for (i = 0; i < addr->len; ++i) { - if (addr->t[i].type == TOKEN822_DOT) - return; - if (addr->t[i].type == TOKEN822_AT) - break; + if (addr->t[i].type == TOKEN822_DOT) return; + if (addr->t[i].type == TOKEN822_AT) break; } for (i = 0; i < addr->len; ++i) { - if (addr->t[i].type == TOKEN822_LITERAL) - return; - if (addr->t[i].type == TOKEN822_AT) - break; + if (addr->t[i].type == TOKEN822_LITERAL) return; + if (addr->t[i].type == TOKEN822_AT) break; } shift = defaultdomain.len; - if (!token822_readyplus(addr,shift)) die_nomem(); + if (!token822_readyplus(addr, shift)) die_nomem(); - for (i = addr->len - 1; i >= 0; --i) - addr->t[i + shift] = addr->t[i]; + for (i = addr->len - 1; i >= 0; --i) addr->t[i + shift] = addr->t[i]; addr->len += shift; - - for (i = 0; i < shift; ++i) - addr->t[i] = defaultdomain.t[shift - 1 - i]; + + for (i = 0; i < shift; ++i) addr->t[i] = defaultdomain.t[shift - 1 - i]; } void rwplus(token822_alloc *addr) @@ -275,15 +275,13 @@ void rwplus(token822_alloc *addr) --addr->t[0].slen; /* remove + */ shift = plusdomain.len; - if (!token822_readyplus(addr,shift)) die_nomem(); + if (!token822_readyplus(addr, shift)) die_nomem(); + + for (i = addr->len - 1; i >= 0; --i) addr->t[i + shift] = addr->t[i]; - for (i = addr->len - 1; i >= 0; --i) - addr->t[i + shift] = addr->t[i]; - addr->len += shift; - - for (i = 0; i < shift; ++i) - addr->t[i] = plusdomain.t[shift - 1 - i]; + + for (i = 0; i < shift; ++i) addr->t[i] = plusdomain.t[shift - 1 - i]; } void rwgeneric(token822_alloc *addr) @@ -293,7 +291,7 @@ void rwgeneric(token822_alloc *addr) if (addr->t[1].type == TOKEN822_AT) if (addr->t[0].type == TOKEN822_LITERAL) if (!addr->t[0].slen) /* don't rewrite <foo@[]> */ - return; + return; rwroute(addr); if (!addr->len) return; /* <@foo:> -> <> */ @@ -310,9 +308,9 @@ int setreturn(token822_alloc *addr) { if (!sender.s) { token822_reverse(addr); - if (token822_unquote(&sender,addr) != 1) die_nomem(); + if (token822_unquote(&sender, addr) != 1) die_nomem(); if (flaghackrecip) - if (!stralloc_cats(&sender,"-@[]")) die_nomem(); + if (!stralloc_cats(&sender, "-@[]")) die_nomem(); token822_reverse(addr); } return 1; @@ -331,36 +329,36 @@ int rwsender(token822_alloc *addr) return 1; } -void rwappend(token822_alloc *addr,saa *xl) +void rwappend(token822_alloc *addr, saa *xl) { token822_reverse(addr); - if (!saa_readyplus(xl,1)) die_nomem(); + if (!saa_readyplus(xl, 1)) die_nomem(); xl->sa[xl->len] = sauninit; - if (token822_unquote(&xl->sa[xl->len],addr) != 1) die_nomem(); + if (token822_unquote(&xl->sa[xl->len], addr) != 1) die_nomem(); ++xl->len; token822_reverse(addr); } int rwhrr(token822_alloc *addr) -{ - rwgeneric(addr); - rwappend(addr,&hrrlist); - return 1; +{ + rwgeneric(addr); + rwappend(addr, &hrrlist); + return 1; } int rwhr(token822_alloc *addr) -{ - rwgeneric(addr); - rwappend(addr,&hrlist); - return 1; +{ + rwgeneric(addr); + rwappend(addr, &hrlist); + return 1; } int rwtocc(token822_alloc *addr) -{ - rwgeneric(addr); - rwappend(addr,&hrlist); - rwappend(addr,&tocclist); - return 1; +{ + rwgeneric(addr); + rwappend(addr, &hrlist); + rwappend(addr, &tocclist); + return 1; } int htypeseen[H_NUM]; @@ -373,40 +371,45 @@ void doheaderfield(stralloc *h) { int htype; int (*rw)() = 0; - - htype = hfield_known(h->s,h->len); - if (flagdeletefrom) if (htype == H_FROM) return; - if (flagdeletemessid) if (htype == H_MESSAGEID) return; - if (flagdeletesender) if (htype == H_RETURNPATH) return; - + + htype = hfield_known(h->s, h->len); + if (flagdeletefrom) + if (htype == H_FROM) return; + if (flagdeletemessid) + if (htype == H_MESSAGEID) return; + if (flagdeletesender) + if (htype == H_RETURNPATH) return; + if (htype) htypeseen[htype] = 1; - else - if (!hfield_valid(h->s,h->len)) - die_invalid(h); - + else if (!hfield_valid(h->s, h->len)) + die_invalid(h); + switch (htype) { - case H_TO: case H_CC: - rw = rwtocc; break; - case H_BCC: case H_APPARENTLYTO: - rw = rwhr; break; - case H_R_TO: case H_R_CC: case H_R_BCC: - rw = rwhrr; break; - case H_RETURNPATH: - rw = rwreturn; break; - case H_SENDER: case H_FROM: case H_REPLYTO: - case H_RETURNRECEIPTTO: case H_ERRORSTO: - case H_R_SENDER: case H_R_FROM: case H_R_REPLYTO: - rw = rwsender; break; + case H_TO: + case H_CC: rw = rwtocc; break; + case H_BCC: + case H_APPARENTLYTO: rw = rwhr; break; + case H_R_TO: + case H_R_CC: + case H_R_BCC: rw = rwhrr; break; + case H_RETURNPATH: rw = rwreturn; break; + case H_SENDER: + case H_FROM: + case H_REPLYTO: + case H_RETURNRECEIPTTO: + case H_ERRORSTO: + case H_R_SENDER: + case H_R_FROM: + case H_R_REPLYTO: rw = rwsender; break; } if (rw) { - doordie(h,token822_parse(&hfin,h,&hfbuf)); - doordie(h,token822_addrlist(&hfrewrite,&hfaddr,&hfin,rw)); - if (token822_unparse(h,&hfrewrite,LINELEN) != 1) - die_nomem(); + doordie(h, token822_parse(&hfin, h, &hfbuf)); + doordie(h, token822_addrlist(&hfrewrite, &hfaddr, &hfin, rw)); + if (token822_unparse(h, &hfrewrite, LINELEN) != 1) die_nomem(); } - + if (htype == H_BCC) return; if (htype == H_R_BCC) return; if (htype == H_RETURNPATH) return; @@ -416,7 +419,7 @@ void doheaderfield(stralloc *h) void dobody(stralloc *h) { - out(h->s,h->len); + out(h->s, h->len); } stralloc torecip = {0}; @@ -424,19 +427,19 @@ token822_alloc tr = {0}; void dorecip(char *s) { - if (!quote2(&torecip,s)) die_nomem(); + if (!quote2(&torecip, s)) die_nomem(); - switch (token822_parse(&tr,&torecip,&hfbuf)) { + switch (token822_parse(&tr, &torecip, &hfbuf)) { case -1: die_nomem(); case 0: - buffer_puts(buffer_2,"qmail-inject: fatal: unable to parse address: "); - buffer_puts(buffer_2,s); - buffer_putsflush(buffer_2,"\n"); + buffer_puts(buffer_2, "qmail-inject: fatal: unable to parse address: "); + buffer_puts(buffer_2, s); + buffer_putsflush(buffer_2, "\n"); perm(); } token822_reverse(&tr); rwgeneric(&tr); - rwappend(&tr,&reciplist); + rwappend(&tr, &reciplist); } stralloc defaultfrom = {0}; @@ -448,7 +451,7 @@ void defaultfrommake() fullname = env_get("QMAILNAME"); if (!fullname) fullname = env_get("MAILNAME"); if (!fullname) fullname = env_get("NAME"); - if (!token822_ready(&df,20)) die_nomem(); + if (!token822_ready(&df, 20)) die_nomem(); df.len = 0; df.t[df.len].type = TOKEN822_ATOM; @@ -493,10 +496,10 @@ void defaultfrommake() ++df.len; } - if (token822_unparse(&defaultfrom,&df,LINELEN) != 1) die_nomem(); - doordie(&defaultfrom,token822_parse(&df,&defaultfrom,&hfbuf)); - doordie(&defaultfrom,token822_addrlist(&hfrewrite,&hfaddr,&df,rwsender)); - if (token822_unparse(&defaultfrom,&hfrewrite,LINELEN) != 1) die_nomem(); + if (token822_unparse(&defaultfrom, &df, LINELEN) != 1) die_nomem(); + doordie(&defaultfrom, token822_parse(&df, &defaultfrom, &hfbuf)); + doordie(&defaultfrom, token822_addrlist(&hfrewrite, &hfaddr, &df, rwsender)); + if (token822_unparse(&defaultfrom, &hfrewrite, LINELEN) != 1) die_nomem(); } stralloc defaultreturnpath = {0}; @@ -506,18 +509,18 @@ char strnum[FMT_ULONG]; void dodefaultreturnpath() { - if (!stralloc_copys(&hackedruser,mailruser)) die_nomem(); + if (!stralloc_copys(&hackedruser, mailruser)) die_nomem(); if (flaghackmess) { - if (!stralloc_cats(&hackedruser,"-")) die_nomem(); - if (!stralloc_catb(&hackedruser,strnum,fmt_ulong(strnum,(unsigned long) starttime))) die_nomem(); - if (!stralloc_cats(&hackedruser,".")) die_nomem(); - if (!stralloc_catb(&hackedruser,strnum,fmt_ulong(strnum,(unsigned long) getpid()))) die_nomem(); + if (!stralloc_cats(&hackedruser, "-")) die_nomem(); + if (!stralloc_catb(&hackedruser, strnum, fmt_ulong(strnum, (unsigned long)starttime))) die_nomem(); + if (!stralloc_cats(&hackedruser, ".")) die_nomem(); + if (!stralloc_catb(&hackedruser, strnum, fmt_ulong(strnum, (unsigned long)getpid()))) die_nomem(); } if (flaghackrecip) - if (!stralloc_cats(&hackedruser,"-")) die_nomem(); + if (!stralloc_cats(&hackedruser, "-")) die_nomem(); - if (!token822_ready(&drp,10)) die_nomem(); + if (!token822_ready(&drp, 10)) die_nomem(); drp.len = 0; drp.t[drp.len].type = TOKEN822_ATOM; @@ -540,10 +543,10 @@ void dodefaultreturnpath() ++drp.len; } - if (token822_unparse(&defaultreturnpath,&drp,LINELEN) != 1) die_nomem(); - doordie(&defaultreturnpath,token822_parse(&drp,&defaultreturnpath,&hfbuf)); - doordie(&defaultreturnpath,token822_addrlist(&hfrewrite,&hfaddr,&drp,rwreturn)); - if (token822_unparse(&defaultreturnpath,&hfrewrite,LINELEN) != 1) die_nomem(); + if (token822_unparse(&defaultreturnpath, &drp, LINELEN) != 1) die_nomem(); + doordie(&defaultreturnpath, token822_parse(&drp, &defaultreturnpath, &hfbuf)); + doordie(&defaultreturnpath, token822_addrlist(&hfrewrite, &hfaddr, &drp, rwreturn)); + if (token822_unparse(&defaultreturnpath, &hfrewrite, LINELEN) != 1) die_nomem(); } int flagmft = 0; @@ -558,11 +561,11 @@ void mft_init() x = env_get("QMAILMFTFILE"); if (!x) return; - r = control_readfile(&mft,x,0); + r = control_readfile(&mft, x, 0); if (r == -1) die_read(); /*XXX*/ if (!r) return; - if (!constmap_init(&mapmft,mft.s,mft.len,0)) die_nomem(); + if (!constmap_init(&mapmft, mft.s, mft.len, 0)) die_nomem(); flagmft = 1; } @@ -576,18 +579,17 @@ void finishmft() if (htypeseen[H_MAILFOLLOWUPTO]) return; for (i = 0; i < tocclist.len; ++i) - if (constmap(&mapmft,tocclist.sa[i].s,tocclist.sa[i].len)) - break; + if (constmap(&mapmft, tocclist.sa[i].s, tocclist.sa[i].len)) break; if (i == tocclist.len) return; outs("Mail-Followup-To: "); i = tocclist.len; while (i--) { - if (!stralloc_copy(&sa,&tocclist.sa[i])) die_nomem(); + if (!stralloc_copy(&sa, &tocclist.sa[i])) die_nomem(); if (!stralloc_0(&sa)) die_nomem(); - if (!quote2(&sa2,sa.s)) die_nomem(); - out(sa2.s,sa2.len); + if (!quote2(&sa2, sa.s)) die_nomem(); + out(sa2.s, sa2.len); if (i) outs(",\n "); } outs("\n"); @@ -596,27 +598,25 @@ void finishmft() void finishheader() { flagresent = - htypeseen[H_R_SENDER] || htypeseen[H_R_FROM] || htypeseen[H_R_REPLYTO] - || htypeseen[H_R_TO] || htypeseen[H_R_CC] || htypeseen[H_R_BCC] - || htypeseen[H_R_DATE] || htypeseen[H_R_MESSAGEID]; + htypeseen[H_R_SENDER] || htypeseen[H_R_FROM] || htypeseen[H_R_REPLYTO] || htypeseen[H_R_TO] + || htypeseen[H_R_CC] || htypeseen[H_R_BCC] || htypeseen[H_R_DATE] || htypeseen[H_R_MESSAGEID]; - if (!sender.s) - dodefaultreturnpath(); + if (!sender.s) dodefaultreturnpath(); if (!flagqueue) { static stralloc sa = {0}; static stralloc sa2 = {0}; - if (!stralloc_copy(&sa,&sender)) die_nomem(); + if (!stralloc_copy(&sa, &sender)) die_nomem(); if (!stralloc_0(&sa)) die_nomem(); - if (!quote2(&sa2,sa.s)) die_nomem(); + if (!quote2(&sa2, sa.s)) die_nomem(); outs("Return-Path: <"); - out(sa2.s,sa2.len); + out(sa2.s, sa2.len); outs(">\n"); } - /* could check at this point whether there are any recipients */ + /* could check at this point whether there are any recipients */ if (flagqueue) if (qmail_open(&qqt) == -1) die_qqt(); @@ -624,35 +624,33 @@ void finishheader() if (!htypeseen[H_R_DATE]) { if (!newfield_datemake(starttime)) die_nomem(); outs("Resent-"); - out(newfield_date.s,newfield_date.len); + out(newfield_date.s, newfield_date.len); } if (!htypeseen[H_R_MESSAGEID]) { - if (!newfield_msgidmake(control_idhost.s,control_idhost.len,starttime)) die_nomem(); + if (!newfield_msgidmake(control_idhost.s, control_idhost.len, starttime)) die_nomem(); outs("Resent-"); - out(newfield_msgid.s,newfield_msgid.len); + out(newfield_msgid.s, newfield_msgid.len); } if (!htypeseen[H_R_FROM]) { defaultfrommake(); outs("Resent-"); - out(defaultfrom.s,defaultfrom.len); + out(defaultfrom.s, defaultfrom.len); } - if (!htypeseen[H_R_TO] && !htypeseen[H_R_CC]) - outs("Resent-Cc: recipient list not shown: ;\n"); + if (!htypeseen[H_R_TO] && !htypeseen[H_R_CC]) outs("Resent-Cc: recipient list not shown: ;\n"); } else { if (!htypeseen[H_DATE]) { if (!newfield_datemake(starttime)) die_nomem(); - out(newfield_date.s,newfield_date.len); + out(newfield_date.s, newfield_date.len); } if (!htypeseen[H_MESSAGEID]) { - if (!newfield_msgidmake(control_idhost.s,control_idhost.len,starttime)) die_nomem(); - out(newfield_msgid.s,newfield_msgid.len); + if (!newfield_msgidmake(control_idhost.s, control_idhost.len, starttime)) die_nomem(); + out(newfield_msgid.s, newfield_msgid.len); } if (!htypeseen[H_FROM]) { defaultfrommake(); - out(defaultfrom.s,defaultfrom.len); + out(defaultfrom.s, defaultfrom.len); } - if (!htypeseen[H_TO] && !htypeseen[H_CC]) - outs("Cc: recipient list not shown: ;\n"); + if (!htypeseen[H_TO] && !htypeseen[H_CC]) outs("Cc: recipient list not shown: ;\n"); finishmft(); } @@ -669,42 +667,43 @@ void getcontrols() if (chdir(auto_qmail) == -1) die_chdir(); if (control_init() == -1) die_read(); - if (control_rldef(&control_defaultdomain,"control/defaultdomain",1,"defaultdomain") != 1) + if (control_rldef(&control_defaultdomain, "control/defaultdomain", 1, "defaultdomain") != 1) die_read(); x = env_get("QMAILDEFAULTDOMAIN"); - if (x) if (!stralloc_copys(&control_defaultdomain,x)) die_nomem(); - if (!stralloc_copys(&sa,".")) die_nomem(); - if (!stralloc_cat(&sa,&control_defaultdomain)) die_nomem(); - doordie(&sa,token822_parse(&defaultdomain,&sa,&defaultdomainbuf)); + if (x) + if (!stralloc_copys(&control_defaultdomain, x)) die_nomem(); + if (!stralloc_copys(&sa, ".")) die_nomem(); + if (!stralloc_cat(&sa, &control_defaultdomain)) die_nomem(); + doordie(&sa, token822_parse(&defaultdomain, &sa, &defaultdomainbuf)); - if (control_rldef(&control_defaulthost,"control/defaulthost",1,"defaulthost") != 1) - die_read(); + if (control_rldef(&control_defaulthost, "control/defaulthost", 1, "defaulthost") != 1) die_read(); x = env_get("QMAILDEFAULTHOST"); - if (x) if (!stralloc_copys(&control_defaulthost,x)) die_nomem(); - if (!stralloc_copys(&sa,"@")) die_nomem(); - if (!stralloc_cat(&sa,&control_defaulthost)) die_nomem(); - doordie(&sa,token822_parse(&defaulthost,&sa,&defaulthostbuf)); + if (x) + if (!stralloc_copys(&control_defaulthost, x)) die_nomem(); + if (!stralloc_copys(&sa, "@")) die_nomem(); + if (!stralloc_cat(&sa, &control_defaulthost)) die_nomem(); + doordie(&sa, token822_parse(&defaulthost, &sa, &defaulthostbuf)); - if (control_rldef(&control_plusdomain,"control/plusdomain",1,"plusdomain") != 1) - die_read(); + if (control_rldef(&control_plusdomain, "control/plusdomain", 1, "plusdomain") != 1) die_read(); x = env_get("QMAILPLUSDOMAIN"); - if (x) if (!stralloc_copys(&control_plusdomain,x)) die_nomem(); - if (!stralloc_copys(&sa,".")) die_nomem(); - if (!stralloc_cat(&sa,&control_plusdomain)) die_nomem(); - doordie(&sa,token822_parse(&plusdomain,&sa,&plusdomainbuf)); + if (x) + if (!stralloc_copys(&control_plusdomain, x)) die_nomem(); + if (!stralloc_copys(&sa, ".")) die_nomem(); + if (!stralloc_cat(&sa, &control_plusdomain)) die_nomem(); + doordie(&sa, token822_parse(&plusdomain, &sa, &plusdomainbuf)); - if (control_rldef(&control_idhost,"control/idhost",1,"idhost") != 1) - die_read(); + if (control_rldef(&control_idhost, "control/idhost", 1, "idhost") != 1) die_read(); x = env_get("QMAILIDHOST"); - if (x) if (!stralloc_copys(&control_idhost,x)) die_nomem(); + if (x) + if (!stralloc_copys(&control_idhost, x)) die_nomem(); } #define RECIP_DEFAULT 1 -#define RECIP_ARGS 2 -#define RECIP_HEADER 3 -#define RECIP_AH 4 +#define RECIP_ARGS 2 +#define RECIP_HEADER 3 +#define RECIP_AH 4 -int main(int argc,char **argv) +int main(int argc, char **argv) { int i; int opt; @@ -716,8 +715,8 @@ int main(int argc,char **argv) qmopts = env_get("QMAILINJECT"); if (qmopts) - while (*qmopts) - switch (*qmopts++) { + while (*qmopts) switch (*qmopts++) + { case 'c': flagnamecomment = 1; break; case 's': flagdeletesender = 1; break; case 'f': flagdeletefrom = 1; break; @@ -737,7 +736,7 @@ int main(int argc,char **argv) if (!mailuser) mailuser = env_get("LOGNAME"); if (!mailuser) mailuser = "anonymous"; mailusertokentype = TOKEN822_ATOM; - if (quote_need(mailuser,str_len(mailuser))) mailusertokentype = TOKEN822_QUOTE; + if (quote_need(mailuser, str_len(mailuser))) mailusertokentype = TOKEN822_QUOTE; mailruser = env_get("QMAILSUSER"); if (!mailruser) mailruser = mailuser; @@ -748,13 +747,13 @@ int main(int argc,char **argv) getcontrols(); - if (!saa_readyplus(&hrlist,1)) die_nomem(); - if (!saa_readyplus(&tocclist,1)) die_nomem(); - if (!saa_readyplus(&hrrlist,1)) die_nomem(); - if (!saa_readyplus(&reciplist,1)) die_nomem(); + if (!saa_readyplus(&hrlist, 1)) die_nomem(); + if (!saa_readyplus(&tocclist, 1)) die_nomem(); + if (!saa_readyplus(&hrrlist, 1)) die_nomem(); + if (!saa_readyplus(&reciplist, 1)) die_nomem(); - while ((opt = getopt(argc,argv,"aAhHnNf:")) != opteof) - switch (opt) { + while ((opt = getopt(argc, argv, "aAhHnNf:")) != opteof) switch (opt) + { case 'a': recipstrategy = RECIP_ARGS; break; case 'A': recipstrategy = RECIP_DEFAULT; break; case 'h': recipstrategy = RECIP_HEADER; break; @@ -762,32 +761,28 @@ int main(int argc,char **argv) case 'n': flagqueue = 0; break; case 'N': flagqueue = 1; break; case 'f': - if (!quote2(&sender,optarg)) die_nomem(); - doordie(&sender,token822_parse(&envs,&sender,&envsbuf)); + if (!quote2(&sender, optarg)) die_nomem(); + doordie(&sender, token822_parse(&envs, &sender, &envsbuf)); token822_reverse(&envs); rwgeneric(&envs); token822_reverse(&envs); - if (token822_unquote(&sender,&envs) != 1) die_nomem(); + if (token822_unquote(&sender, &envs) != 1) die_nomem(); break; case '?': - default: - perm(); + default: perm(); } argc -= optind; argv += optind; - if (recipstrategy == RECIP_DEFAULT) - recipstrategy = (*argv ? RECIP_ARGS : RECIP_HEADER); + if (recipstrategy == RECIP_DEFAULT) recipstrategy = (*argv ? RECIP_ARGS : RECIP_HEADER); if (recipstrategy != RECIP_HEADER) - while (*argv) - dorecip(*argv++); + while (*argv) dorecip(*argv++); flagrh = (recipstrategy != RECIP_ARGS); - if (headerbody(buffer_0,doheaderfield,finishheader,dobody) == -1) - die_read(); + if (headerbody(buffer_0, doheaderfield, finishheader, dobody) == -1) die_read(); exitnicely(); } 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); diff --git a/src/qmail-lspawn.c b/src/qmail-lspawn.c index fed5c4c..29c1b7a 100644 --- a/src/qmail-lspawn.c +++ b/src/qmail-lspawn.c @@ -1,24 +1,26 @@ #include <unistd.h> -#include "fd.h" -#include "wait.h" -#include "prot.h" + #include "buffer.h" -#include "stralloc.h" -#include "scan.h" -#include "exit.h" -#include "cdbread.h" +#include "byte.h" #include "case.h" +#include "cdbread.h" +#include "error.h" +#include "exit.h" +#include "fd.h" +#include "open.h" +#include "prot.h" #include "readclose.h" +#include "scan.h" +#include "stralloc.h" +#include "wait.h" + #include "auto_qmail.h" #include "auto_uids.h" #include "qlx.h" -#include "error.h" -#include "open.h" -#include "byte.h" char *aliasempty; -void initialize(int argc,char **argv) +void initialize(int argc, char **argv) { aliasempty = argv[1]; if (!aliasempty) _exit(100); @@ -26,45 +28,40 @@ void initialize(int argc,char **argv) int truncreport = 3000; -void report(buffer *log,int wstat,char *s,int len) +void report(buffer *log, int wstat, char *s, int len) { int i; - if (wait_crashed(wstat)) { buffer_putsflush(log,"Zqmail-lspawn: qmail-local crashed.\n"); return; } + if (wait_crashed(wstat)) { + buffer_putsflush(log, "Zqmail-lspawn: qmail-local crashed.\n"); + return; + } switch (wait_exitcode(wstat)) { - case QLX_CDB: - buffer_putsflush(log,"Zqmail-lspawn: Trouble reading users/assign.cdb.\n"); return; - case QLX_NOMEM: - buffer_putsflush(log,"Zqmail-lspawn: Out of memory.\n"); return; - case QLX_SYS: - buffer_putsflush(log,"Zqmail-lspawn: Temporary failure.\n"); return; - case QLX_NOALIAS: - buffer_putsflush(log,"Zqmail-lspawn: Unable to find alias user!\n"); return; + case QLX_CDB: buffer_putsflush(log, "Zqmail-lspawn: Trouble reading users/assign.cdb.\n"); return; + case QLX_NOMEM: buffer_putsflush(log, "Zqmail-lspawn: Out of memory.\n"); return; + case QLX_SYS: buffer_putsflush(log, "Zqmail-lspawn: Temporary failure.\n"); return; + case QLX_NOALIAS: buffer_putsflush(log, "Zqmail-lspawn: Unable to find alias user!\n"); return; case QLX_ROOT: - buffer_putsflush(log,"Zqmail-spawn: Not allowed to perform deliveries as root.\n"); return; - case QLX_USAGE: - buffer_putsflush(log,"Zqmail-spawn: Internal bug.\n"); return; - case QLX_NFS: - buffer_putsflush(log,"Zqmail-spawn: NFS failure in qmail-local.\n"); return; - case QLX_EXECHARD: - buffer_putsflush(log,"Dqmail-spawn: Unable to run qmail-local.\n"); return; - case QLX_EXECSOFT: - buffer_putsflush(log,"Zqmail-spawn: Unable to run qmail-local.\n"); return; - case QLX_EXECPW: - buffer_putsflush(log,"Zqmail-spawn: Unable to run qmail-getpw.\n"); return; - case 111: case 71: case 74: case 75: - buffer_put(log,"Z",1); break; - case 0: - buffer_put(log,"K",1); break; + buffer_putsflush(log, "Zqmail-spawn: Not allowed to perform deliveries as root.\n"); + return; + case QLX_USAGE: buffer_putsflush(log, "Zqmail-spawn: Internal bug.\n"); return; + case QLX_NFS: buffer_putsflush(log, "Zqmail-spawn: NFS failure in qmail-local.\n"); return; + case QLX_EXECHARD: buffer_putsflush(log, "Dqmail-spawn: Unable to run qmail-local.\n"); return; + case QLX_EXECSOFT: buffer_putsflush(log, "Zqmail-spawn: Unable to run qmail-local.\n"); return; + case QLX_EXECPW: buffer_putsflush(log, "Zqmail-spawn: Unable to run qmail-getpw.\n"); return; + case 111: + case 71: + case 74: + case 75: buffer_put(log, "Z", 1); break; + case 0: buffer_put(log, "K", 1); break; case 100: - default: - buffer_put(log,"D",1); break; + default: buffer_put(log, "D", 1); break; } - for (i = 0; i < len; ++i) + for (i = 0; i < len; ++i) if (!s[i]) break; - buffer_put(log,s,i); + buffer_put(log, s, i); } stralloc lower = {0}; @@ -83,42 +80,43 @@ void nughde_get(char *local) int fd; int flagwild; - if (!stralloc_copys(&lower,"!")) _exit(QLX_NOMEM); - if (!stralloc_cats(&lower,local)) _exit(QLX_NOMEM); + if (!stralloc_copys(&lower, "!")) _exit(QLX_NOMEM); + if (!stralloc_cats(&lower, local)) _exit(QLX_NOMEM); if (!stralloc_0(&lower)) _exit(QLX_NOMEM); - case_lowerb(lower.s,lower.len); + case_lowerb(lower.s, lower.len); - if (!stralloc_copys(&nughde,"")) _exit(QLX_NOMEM); + if (!stralloc_copys(&nughde, "")) _exit(QLX_NOMEM); fd = open_read("users/assign.cdb"); if (fd == -1) - if (errno != ENOENT) - _exit(QLX_CDB); + if (errno != ENOENT) _exit(QLX_CDB); if (fd != -1) { unsigned int i; - cdb_init(&c,fd); - r = cdb_find(&c,"",0); + cdb_init(&c, fd); + r = cdb_find(&c, "", 0); if (r != 1) _exit(QLX_CDB); - if (!stralloc_ready(&wildchars,cdb_datalen(&c))) _exit(QLX_NOMEM); + if (!stralloc_ready(&wildchars, cdb_datalen(&c))) _exit(QLX_NOMEM); wildchars.len = cdb_datalen(&c); - if (cdb_read(&c,wildchars.s,wildchars.len,cdb_datapos(&c)) == -1) _exit(QLX_CDB); + if (cdb_read(&c, wildchars.s, wildchars.len, cdb_datapos(&c)) == -1) _exit(QLX_CDB); i = lower.len; flagwild = 0; do { /* i > 0 */ - if (!flagwild || (i == 1) || (byte_chr(wildchars.s,wildchars.len,lower.s[i - 1]) < wildchars.len)) { - r = cdb_find(&c,lower.s,i); + if (!flagwild || (i == 1) + || (byte_chr(wildchars.s, wildchars.len, lower.s[i - 1]) < wildchars.len)) + { + r = cdb_find(&c, lower.s, i); if (r == -1) _exit(QLX_CDB); if (r == 1) { - if (!stralloc_ready(&nughde,cdb_datalen(&c))) _exit(QLX_NOMEM); + if (!stralloc_ready(&nughde, cdb_datalen(&c))) _exit(QLX_NOMEM); nughde.len = cdb_datalen(&c); - if (cdb_read(&c,nughde.s,nughde.len,cdb_datapos(&c)) == -1) _exit(QLX_CDB); + if (cdb_read(&c, nughde.s, nughde.len, cdb_datapos(&c)) == -1) _exit(QLX_CDB); if (flagwild) - if (!stralloc_cats(&nughde,local + i - 1)) _exit(QLX_NOMEM); + if (!stralloc_cats(&nughde, local + i - 1)) _exit(QLX_NOMEM); if (!stralloc_0(&nughde)) _exit(QLX_NOMEM); close(fd); return; @@ -137,27 +135,26 @@ void nughde_get(char *local) args[1] = local; args[2] = 0; switch (gpwpid = fork()) { - case -1: - _exit(QLX_SYS); + case -1: _exit(QLX_SYS); case 0: if (prot_gid(auto_gidn) == -1) _exit(QLX_USAGE); if (prot_uid(auto_uidp) == -1) _exit(QLX_USAGE); close(pi[0]); - if (fd_move(1,pi[1]) == -1) _exit(QLX_SYS); - execv(*args,args); + if (fd_move(1, pi[1]) == -1) _exit(QLX_SYS); + execv(*args, args); _exit(QLX_EXECPW); } close(pi[1]); - if (readclose_append(pi[0],&nughde,128) == -1) _exit(QLX_SYS); + if (readclose_append(pi[0], &nughde, 128) == -1) _exit(QLX_SYS); - if (wait_pid(&gpwstat,gpwpid) != -1) { + if (wait_pid(&gpwstat, gpwpid) != -1) { if (wait_crashed(gpwstat)) _exit(QLX_SYS); if (wait_exitcode(gpwstat) != 0) _exit(wait_exitcode(gpwstat)); } } -int spawn(int fdmess,int fdout,const char *s,char *r,const int at) +int spawn(int fdmess, int fdout, const char *s, char *r, const int at) { int f; @@ -169,7 +166,7 @@ int spawn(int fdmess,int fdout,const char *s,char *r,const int at) int gid; char *x; unsigned int xlen; - + r[at] = 0; if (!r[0]) _exit(0); /* <> */ @@ -183,42 +180,42 @@ int spawn(int fdmess,int fdout,const char *s,char *r,const int at) args[0] = "bin/qmail-local"; args[1] = "--"; args[2] = x; - n = byte_chr(x,xlen,0); - if (n++ == xlen) _exit(QLX_USAGE); - x += n; + n = byte_chr(x, xlen, 0); + if (n++ == xlen) _exit(QLX_USAGE); + x += n; xlen -= n; - scan_ulong(x,&u); + scan_ulong(x, &u); uid = u; - n = byte_chr(x,xlen,0); - if (n++ == xlen) _exit(QLX_USAGE); - x += n; + n = byte_chr(x, xlen, 0); + if (n++ == xlen) _exit(QLX_USAGE); + x += n; xlen -= n; - scan_ulong(x,&u); + scan_ulong(x, &u); gid = u; - n = byte_chr(x,xlen,0); - if (n++ == xlen) _exit(QLX_USAGE); - x += n; + n = byte_chr(x, xlen, 0); + if (n++ == xlen) _exit(QLX_USAGE); + x += n; xlen -= n; args[3] = x; - n = byte_chr(x,xlen,0); - if (n++ == xlen) _exit(QLX_USAGE); - x += n; + n = byte_chr(x, xlen, 0); + if (n++ == xlen) _exit(QLX_USAGE); + x += n; xlen -= n; args[4] = r; args[5] = x; - n = byte_chr(x,xlen,0); - if (n++ == xlen) _exit(QLX_USAGE); - x += n; + n = byte_chr(x, xlen, 0); + if (n++ == xlen) _exit(QLX_USAGE); + x += n; xlen -= n; args[6] = x; - n = byte_chr(x,xlen,0); - if (n++ == xlen) _exit(QLX_USAGE); - x += n; + n = byte_chr(x, xlen, 0); + if (n++ == xlen) _exit(QLX_USAGE); + x += n; xlen -= n; args[7] = r + at + 1; @@ -226,14 +223,14 @@ int spawn(int fdmess,int fdout,const char *s,char *r,const int at) args[9] = aliasempty; args[10] = 0; - if (fd_move(0,fdmess) == -1) _exit(QLX_SYS); - if (fd_move(1,fdout) == -1) _exit(QLX_SYS); - if (fd_copy(2,1) == -1) _exit(QLX_SYS); + if (fd_move(0, fdmess) == -1) _exit(QLX_SYS); + if (fd_move(1, fdout) == -1) _exit(QLX_SYS); + if (fd_copy(2, 1) == -1) _exit(QLX_SYS); if (prot_gid(gid) == -1) _exit(QLX_USAGE); if (prot_uid(uid) == -1) _exit(QLX_USAGE); if (!getuid()) _exit(QLX_ROOT); - execv(*args,args); + execv(*args, args); if (errno) _exit(QLX_EXECSOFT); _exit(QLX_EXECHARD); } diff --git a/src/qmail-mfrules.c b/src/qmail-mfrules.c index e8cfc94..7247cf7 100644 --- a/src/qmail-mfrules.c +++ b/src/qmail-mfrules.c @@ -1,22 +1,25 @@ #include <sys/stat.h> -#include <stdio.h> // rename -#include "logmsg.h" -#include "stralloc.h" -#include "buffer.h" -#include "getln.h" -#include "exit.h" #include <unistd.h> -#include "open.h" -#include "auto_qmail.h" + +#include <stdio.h> // rename + +#include "buffer.h" +#include "byte.h" +#include "case.h" #include "cdbmake.h" +#include "exit.h" #include "fmt.h" +#include "getln.h" +#include "logmsg.h" +#include "open.h" #include "scan.h" -#include "byte.h" -#include "case.h" +#include "stralloc.h" + +#include "auto_qmail.h" #define WHO "qmail-mfrules" -int rename(const char *,const char *); // stdio.h +int rename(const char *, const char *); // stdio.h stralloc address = {0}; stralloc data = {0}; @@ -34,30 +37,30 @@ struct cdb_make cdb; void die_nomem() { - logmsg(WHO,112,FATAL,"out of memory"); + logmsg(WHO, 112, FATAL, "out of memory"); } void die_parse() { if (!stralloc_0(&line)) die_nomem(); - logmsg(WHO,100,ERROR,B("unable to parse this line: ",line.s)); + logmsg(WHO, 100, ERROR, B("unable to parse this line: ", line.s)); } void die_read() { - logmsg(WHO,111,ERROR,"unable to read control/mailfromrules"); + logmsg(WHO, 111, ERROR, "unable to read control/mailfromrules"); } void die_write() { - logmsg(WHO,111,ERROR,"unable to write to control/mailfromrules.tmp"); + logmsg(WHO, 111, ERROR, "unable to write to control/mailfromrules.tmp"); } char strnum[FMT_ULONG]; stralloc sanum = {0}; -void getnum(char *buf,int len,unsigned long *u) +void getnum(char *buf, int len, unsigned long *u) { - if (!stralloc_copyb(&sanum,buf,len)) die_nomem(); + if (!stralloc_copyb(&sanum, buf, len)) die_nomem(); if (!stralloc_0(&sanum)) die_nomem(); - if (sanum.s[scan_ulong(sanum.s,u)]) die_parse(); + if (sanum.s[scan_ulong(sanum.s, u)]) die_parse(); } void doaddressdata() @@ -68,26 +71,29 @@ void doaddressdata() unsigned long bot; unsigned long top; - if (byte_chr(address.s,address.len,'=') == address.len) - if (byte_chr(address.s,address.len,'@') == address.len) { - i = byte_chr(address.s,address.len,'-'); + if (byte_chr(address.s, address.len, '=') == address.len) + if (byte_chr(address.s, address.len, '@') == address.len) { + i = byte_chr(address.s, address.len, '-'); if (i < address.len) { - left = byte_rchr(address.s,i,'.'); - if (left == i) left = 0; else ++left; + left = byte_rchr(address.s, i, '.'); + if (left == i) + left = 0; + else + ++left; ++i; - right = i + byte_chr(address.s + i,address.len - i,'.'); - - getnum(address.s + left,i - 1 - left,&bot); - getnum(address.s + i,right - i,&top); + right = i + byte_chr(address.s + i, address.len - i, '.'); + + getnum(address.s + left, i - 1 - left, &bot); + getnum(address.s + i, right - i, &top); if (top > 255) top = 255; while (bot <= top) { - if (!stralloc_copyb(&key,address.s,left)) die_nomem(); - if (!stralloc_catb(&key,strnum,fmt_ulong(strnum,bot))) die_nomem(); - if (!stralloc_catb(&key,address.s + right,address.len - right)) die_nomem(); - case_lowerb(key.s,key.len); - if (cdb_make_add(&cdb,key.s,key.len,data.s,data.len) == -1) die_write(); + if (!stralloc_copyb(&key, address.s, left)) die_nomem(); + if (!stralloc_catb(&key, strnum, fmt_ulong(strnum, bot))) die_nomem(); + if (!stralloc_catb(&key, address.s + right, address.len - right)) die_nomem(); + case_lowerb(key.s, key.len); + if (cdb_make_add(&cdb, key.s, key.len, data.s, data.len) == -1) die_write(); ++bot; } @@ -95,9 +101,9 @@ void doaddressdata() } } - case_lowerb(address.s,address.len); - case_lowerb(data.s,data.len); - if (cdb_make_add(&cdb,address.s,address.len,data.s,data.len) == -1) die_write(); + case_lowerb(address.s, address.len); + case_lowerb(data.s, data.len); + if (cdb_make_add(&cdb, address.s, address.len, data.s, data.len) == -1) die_write(); } int main() @@ -109,23 +115,23 @@ int main() char ch; umask(033); - if (chdir(auto_qmail) == -1) - logmsg(WHO,111,ERROR,B("unable to chdir to: ",auto_qmail)); + if (chdir(auto_qmail) == -1) logmsg(WHO, 111, ERROR, B("unable to chdir to: ", auto_qmail)); fd = open_read("control/mailfromrules"); if (fd == -1) die_read(); - buffer_init(&bi,read,fd,inbuf,sizeof(inbuf)); + buffer_init(&bi, read, fd, inbuf, sizeof(inbuf)); fdtemp = open_trunc("control/mailfromrules.tmp"); if (fdtemp == -1) die_write(); - if (cdb_make_start(&cdb,fdtemp) == -1) die_write(); + if (cdb_make_start(&cdb, fdtemp) == -1) die_write(); while (match) { - if (getln(&bi,&line,&match,'\n') != 0) die_read(); + if (getln(&bi, &line, &match, '\n') != 0) die_read(); - x = line.s; len = line.len; + x = line.s; + len = line.len; if (!len) break; if (x[0] == '#') continue; @@ -133,32 +139,38 @@ int main() while (len) { ch = x[len - 1]; - if (ch != '\n') if (ch != ' ') if (ch != '\t') break; + if (ch != '\n') + if (ch != ' ') + if (ch != '\t') break; --len; } line.len = len; /* for die_parse() */ - amper = byte_chr(x,len,'&'); + amper = byte_chr(x, len, '&'); if (!amper) die_parse(); - if (amper) if (amper == len || amper < 2) die_parse(); + if (amper) + if (amper == len || amper < 2) die_parse(); - if (!stralloc_copyb(&address,x,amper)) die_nomem(); - if (!stralloc_copys(&data,"")) die_nomem(); + if (!stralloc_copyb(&address, x, amper)) die_nomem(); + if (!stralloc_copys(&data, "")) die_nomem(); - x = line.s + amper + 1; len = line.len - amper - 1; + x = line.s + amper + 1; + len = line.len - amper - 1; while (len) { - if (len < 3) die_parse(); /* input checks */ - if ( *x == ',' || *x == ' ' || *x == '\t') die_parse(); - i = byte_chr(x,len,','); /* &addr1,addr2,.. */ + if (len < 3) die_parse(); /* input checks */ + if (*x == ',' || *x == ' ' || *x == '\t') die_parse(); + i = byte_chr(x, len, ','); /* &addr1,addr2,.. */ if (i > 0 && i < len) { - if (!stralloc_catb(&data,"+",1)) die_nomem(); - if (!stralloc_catb(&data,x,i)) die_nomem(); - x += i + 1; len -= i + 1; } - else { - if (!stralloc_catb(&data,"+",1)) die_nomem(); - if (!stralloc_catb(&data,x,len)) die_nomem(); - len = 0; } + if (!stralloc_catb(&data, "+", 1)) die_nomem(); + if (!stralloc_catb(&data, x, i)) die_nomem(); + x += i + 1; + len -= i + 1; + } else { + if (!stralloc_catb(&data, "+", 1)) die_nomem(); + if (!stralloc_catb(&data, x, len)) die_nomem(); + len = 0; + } } doaddressdata(); } @@ -166,8 +178,8 @@ int main() if (cdb_make_finish(&cdb) == -1) die_write(); if (fsync(fdtemp) == -1) die_write(); if (close(fdtemp) == -1) die_write(); /* NFS stupidity */ - if (rename("control/mailfromrules.tmp","control/mailfromrules.cdb") == -1) - logmsg(WHO,111,ERROR,"unable to move control/mailfromrules.tmp to control/mailfromrules.cdb"); + if (rename("control/mailfromrules.tmp", "control/mailfromrules.cdb") == -1) + logmsg(WHO, 111, ERROR, "unable to move control/mailfromrules.tmp to control/mailfromrules.cdb"); _exit(0); } diff --git a/src/qmail-mrtg.c b/src/qmail-mrtg.c index 1ccedeb..cecd067 100644 --- a/src/qmail-mrtg.c +++ b/src/qmail-mrtg.c @@ -1,18 +1,20 @@ #include <unistd.h> -#include "stralloc.h" + #include "buffer.h" -#include "getln.h" +#include "case.h" #include "exit.h" +#include "fmt.h" +#include "getln.h" +#include "logmsg.h" #include "open.h" #include "scan.h" -#include "fmt.h" -#include "case.h" -#include "now.h" #include "str.h" +#include "stralloc.h" + #include "datetime.h" -#include "logmsg.h" +#include "now.h" -#define WHO "qmail-mrtg" +#define WHO "qmail-mrtg" #define TAI64NLEN 24 /** @file qmail-mrtg.c @@ -75,174 +77,262 @@ int grey = 0; int rbl = 0; char bufsmall[64]; -buffer bo = BUFFER_INIT(write,1,bufsmall,sizeof(bufsmall)); +buffer bo = BUFFER_INIT(write, 1, bufsmall, sizeof(bufsmall)); -static void outs(char *s) +static void outs(char *s) { - if (buffer_puts(&bo,s) == -1) _exit(1); - if (buffer_puts(&bo,"\n") == -1) _exit(1); + if (buffer_puts(&bo, s) == -1) _exit(1); + if (buffer_puts(&bo, "\n") == -1) _exit(1); if (buffer_flush(&bo) == -1) _exit(1); } -static void out(int i) +static void out(int i) { char num[FMT_ULONG]; - if (buffer_put(&bo,num,fmt_ulong(num,(unsigned long) i)) == -1) _exit(1); - if (buffer_puts(&bo,"\n") == -1) _exit(1); + if (buffer_put(&bo, num, fmt_ulong(num, (unsigned long)i)) == -1) _exit(1); + if (buffer_puts(&bo, "\n") == -1) _exit(1); if (buffer_flush(&bo) == -1) _exit(1); } char bufspace[1024]; -buffer bi = BUFFER_INIT(read,0,bufspace,sizeof(bufspace)); +buffer bi = BUFFER_INIT(read, 0, bufspace, sizeof(bufspace)); -void mrtg_results(char flag) +void mrtg_results(char flag) { switch (flag) { - case '1': out(success); out(tlstrans); break; - case '2': bytes = bytes/1024; out(bytes); out(bytes); break; - case '3': out(local); out(remote); break; - case '4': out(failure); out(deferral); break; - case '5': out(bounces); out(triples); break; - case '6': qmtps += qmtp; out(qmtp); out(qmtps); break; /* QMTP */ - - case 'a': out(asessions); out(rsessions); break; /* total */ - case 'b': out(aorig); out(arcpt); break; /* accepted */ - case 'c': out(rsend); out(rhelo); break; /* rejected MTA */ - case 'd': out(rorigbad); out(rorigdns); break; /* Orig */ - case 'e': out(rrcptbad); out(rrcptfail); break; /* Recipient */ - case 'f': out(rmime); out(rloader); break; /* Warlord */ - case 'g': out(rvirus); out(rspam); break; /* Infected/Spam */ - case 'h': out(aauth); out(rauth); break; /* Auth */ - case 'i': out(atls); out(rtls); break; /* TLS */ - case 'j': out(spfpass); out(spfail); break; /* SPF */ - case 'k': out(grey); break; /* Greylisted */ - case 'z': sdeny +=rbl; out(sok); out(sdeny); break; /* reject session */ - - case 'A': out(apop); out(rpop); break; - case 'B': out(pok); out(pdeny); break; + case '1': + out(success); + out(tlstrans); + break; + case '2': + bytes = bytes / 1024; + out(bytes); + out(bytes); + break; + case '3': + out(local); + out(remote); + break; + case '4': + out(failure); + out(deferral); + break; + case '5': + out(bounces); + out(triples); + break; + case '6': + qmtps += qmtp; + out(qmtp); + out(qmtps); + break; /* QMTP */ + + case 'a': + out(asessions); + out(rsessions); + break; /* total */ + case 'b': + out(aorig); + out(arcpt); + break; /* accepted */ + case 'c': + out(rsend); + out(rhelo); + break; /* rejected MTA */ + case 'd': + out(rorigbad); + out(rorigdns); + break; /* Orig */ + case 'e': + out(rrcptbad); + out(rrcptfail); + break; /* Recipient */ + case 'f': + out(rmime); + out(rloader); + break; /* Warlord */ + case 'g': + out(rvirus); + out(rspam); + break; /* Infected/Spam */ + case 'h': + out(aauth); + out(rauth); + break; /* Auth */ + case 'i': + out(atls); + out(rtls); + break; /* TLS */ + case 'j': + out(spfpass); + out(spfail); + break; /* SPF */ + case 'k': out(grey); break; /* Greylisted */ + case 'z': + sdeny += rbl; + out(sok); + out(sdeny); + break; /* reject session */ + + case 'A': + out(apop); + out(rpop); + break; + case 'B': + out(pok); + out(pdeny); + break; default: break; } } -void mrtg_sendlog(char *in, char flag) +void mrtg_sendlog(char *in, char flag) { int i, j, k = 0; - switch (flag) { - case '1': if (case_starts(in,"delivery")) { - i = str_chr(in,':') + 2; - if (case_starts(in + i,"success:")) success++; - i = str_chr(in,'T'); - if (case_starts(in + i,"TLS_")) tlstrans++; - }; break; - case '2': if (case_starts(in,"info msg")) { - i = str_chr(in,':') + 8; - if ((j = str_chr(in + i,' '))) in[i + j] = '\0'; - bytes += atoi(in + i); - }; break; - case '3': if (case_starts(in,"status:")) { - i = str_rchr(in,'c') + 4; - k = str_rchr(in,'r') + 7; - if ((j = str_chr(in + i,'/'))) in[i + j] = '\0'; - if (atoi(in + i) > local) local = atoi(in + i); - if ((j = str_chr(in + k,'/'))) in[k + j] = '\0'; - if (atoi(in + k) > remote) remote = atoi(in + k); - }; break; - case '4': if (case_starts(in,"delivery")) { - i = str_chr(in,':') + 2; - if (case_starts(in + i,"failure:")) failure++; - if (case_starts(in + i,"deferral:")) deferral++; - }; break; - case '5': if (case_starts(in,"bounce msg")) bounces++; - if (case_starts(in,"triple bounce:")) triples++; - break; - case '6': if (case_starts(in,"delivery")) { - i = str_chr(in,'q'); - if (case_starts(in + i,"qmtp:_ok")) qmtp++; - if (case_starts(in + i,"qmtps:_ok")) qmtps++; - }; break; - default: break; + switch (flag) { + case '1': + if (case_starts(in, "delivery")) { + i = str_chr(in, ':') + 2; + if (case_starts(in + i, "success:")) success++; + i = str_chr(in, 'T'); + if (case_starts(in + i, "TLS_")) tlstrans++; + }; + break; + case '2': + if (case_starts(in, "info msg")) { + i = str_chr(in, ':') + 8; + if ((j = str_chr(in + i, ' '))) in[i + j] = '\0'; + bytes += atoi(in + i); + }; + break; + case '3': + if (case_starts(in, "status:")) { + i = str_rchr(in, 'c') + 4; + k = str_rchr(in, 'r') + 7; + if ((j = str_chr(in + i, '/'))) in[i + j] = '\0'; + if (atoi(in + i) > local) local = atoi(in + i); + if ((j = str_chr(in + k, '/'))) in[k + j] = '\0'; + if (atoi(in + k) > remote) remote = atoi(in + k); + }; + break; + case '4': + if (case_starts(in, "delivery")) { + i = str_chr(in, ':') + 2; + if (case_starts(in + i, "failure:")) failure++; + if (case_starts(in + i, "deferral:")) deferral++; + }; + break; + case '5': + if (case_starts(in, "bounce msg")) bounces++; + if (case_starts(in, "triple bounce:")) triples++; + break; + case '6': + if (case_starts(in, "delivery")) { + i = str_chr(in, 'q'); + if (case_starts(in + i, "qmtp:_ok")) qmtp++; + if (case_starts(in + i, "qmtps:_ok")) qmtps++; + }; + break; + default: break; } } -void mrtg_smtplog(char *in, char flag) +void mrtg_smtplog(char *in, char flag) { int i, j, k = 0; - i = str_chr(in,'A'); - j = str_chr(in,'R'); - k = str_chr(in,'P'); + i = str_chr(in, 'A'); + j = str_chr(in, 'R'); + k = str_chr(in, 'P'); switch (flag) { - case 'a': if (case_starts(in + i,"Accept")) asessions++; - if (case_starts(in + j,"Reject")) rsessions++; - break; - case 'b': if (case_starts(in + i,"Accept::ORIG:")) aorig++; - if (case_starts(in + i,"Accept::RCPT:")) arcpt++; - break; - case 'c': if (case_starts(in + j,"Reject::SNDR::Invalid_Relay")) rsend++; - if (case_starts(in + j,"Reject::SNDR::Bad_Helo")) rhelo++; - if (case_starts(in + j,"Reject::SNDR::DNS_Helo")) rhelo++; - break; - case 'd': if (case_starts(in + j,"Reject::ORIG::Bad_Mailfrom")) rorigbad++; - if (case_starts(in + j,"Reject::ORIG::DNS_MF")) rorigdns++; - break; - case 'e': if (case_starts(in + j,"Reject::RCPT::Bad_Rcptto")) rrcptbad++; - if (case_starts(in + j,"Reject::RCPT::Failed_Rcptto")) rrcptfail++; - break; - case 'f': if (case_starts(in + j,"Reject::DATA::Invalid_Size")) rsize++; - if (case_starts(in + j,"Reject::DATA::Bad_MIME")) rmime++; - if (case_starts(in + j,"Reject::DATA::MIME_Attach")) rmime++; - if (case_starts(in + j,"Reject::DATA::Bad_Loader")) rloader++; - break; - case 'g': if (case_starts(in + j,"Reject::DATA::Spam_Message")) rspam++; - if (case_starts(in + j,"Reject::DATA::Virus_Infected")) rvirus++; - break; - case 'h': if (case_starts(in + i,"Accept::AUTH:")) aauth++; - if (case_starts(in + j,"Reject::AUTH:")) rauth++; - break; - case 'i': if (case_starts(in + k,"P:ESMTPS")) atls++; - if (case_starts(in + j,"Reject::TLS:")) rtls++; - break; - case 'j': if (case_starts(in + i,"Accept::SPF:")) spfpass++; - if (case_starts(in + j,"Reject::SPF:")) spfail++; - break; - case 'k': if (case_starts(in + i,"Deferred::SNDR::Grey_Listed")) grey++; - break; - case 'z': if (case_starts(in,"tcpserver") || case_starts(in,"sslserver") || case_starts(in,"rblsmtpd")) { - i = str_chr(in,':') + 2; - if (case_starts(in + i,"ok")) sok++; - if (case_starts(in + i,"deny")) sdeny++; - j = str_chr(in+i,':') + 2; - if (case_starts(in + i + j,"451")) rbl++; - if (case_starts(in + i + j,"553")) rbl++; - if (case_starts(in + i + j,"greetdelay:")) greet++; - } break; - default: break; + case 'a': + if (case_starts(in + i, "Accept")) asessions++; + if (case_starts(in + j, "Reject")) rsessions++; + break; + case 'b': + if (case_starts(in + i, "Accept::ORIG:")) aorig++; + if (case_starts(in + i, "Accept::RCPT:")) arcpt++; + break; + case 'c': + if (case_starts(in + j, "Reject::SNDR::Invalid_Relay")) rsend++; + if (case_starts(in + j, "Reject::SNDR::Bad_Helo")) rhelo++; + if (case_starts(in + j, "Reject::SNDR::DNS_Helo")) rhelo++; + break; + case 'd': + if (case_starts(in + j, "Reject::ORIG::Bad_Mailfrom")) rorigbad++; + if (case_starts(in + j, "Reject::ORIG::DNS_MF")) rorigdns++; + break; + case 'e': + if (case_starts(in + j, "Reject::RCPT::Bad_Rcptto")) rrcptbad++; + if (case_starts(in + j, "Reject::RCPT::Failed_Rcptto")) rrcptfail++; + break; + case 'f': + if (case_starts(in + j, "Reject::DATA::Invalid_Size")) rsize++; + if (case_starts(in + j, "Reject::DATA::Bad_MIME")) rmime++; + if (case_starts(in + j, "Reject::DATA::MIME_Attach")) rmime++; + if (case_starts(in + j, "Reject::DATA::Bad_Loader")) rloader++; + break; + case 'g': + if (case_starts(in + j, "Reject::DATA::Spam_Message")) rspam++; + if (case_starts(in + j, "Reject::DATA::Virus_Infected")) rvirus++; + break; + case 'h': + if (case_starts(in + i, "Accept::AUTH:")) aauth++; + if (case_starts(in + j, "Reject::AUTH:")) rauth++; + break; + case 'i': + if (case_starts(in + k, "P:ESMTPS")) atls++; + if (case_starts(in + j, "Reject::TLS:")) rtls++; + break; + case 'j': + if (case_starts(in + i, "Accept::SPF:")) spfpass++; + if (case_starts(in + j, "Reject::SPF:")) spfail++; + break; + case 'k': + if (case_starts(in + i, "Deferred::SNDR::Grey_Listed")) grey++; + break; + case 'z': + if (case_starts(in, "tcpserver") || case_starts(in, "sslserver") || case_starts(in, "rblsmtpd")) { + i = str_chr(in, ':') + 2; + if (case_starts(in + i, "ok")) sok++; + if (case_starts(in + i, "deny")) sdeny++; + j = str_chr(in + i, ':') + 2; + if (case_starts(in + i + j, "451")) rbl++; + if (case_starts(in + i + j, "553")) rbl++; + if (case_starts(in + i + j, "greetdelay:")) greet++; + } + break; + default: break; } -} +} -void mrtg_pop3log(char *in, char flag) +void mrtg_pop3log(char *in, char flag) { int i, j = 0; switch (flag) { - case 'A': i = str_chr(in,'A'); j = str_chr(in,'R'); - if (case_starts(in + i,"Accept::AUTH:")) apop++; - if (case_starts(in + j,"Reject::AUTH:")) rpop++; - break; - case 'B': if (case_starts(in,"tcpserver:") || case_starts(in,"sslserver:")) { - i = str_chr(in,':') + 2; - if (case_starts(in + i,"ok")) pok++; - if (case_starts(in + i,"deny")) pdeny++; - }; break; - default: break; + case 'A': + i = str_chr(in, 'A'); + j = str_chr(in, 'R'); + if (case_starts(in + i, "Accept::AUTH:")) apop++; + if (case_starts(in + j, "Reject::AUTH:")) rpop++; + break; + case 'B': + if (case_starts(in, "tcpserver:") || case_starts(in, "sslserver:")) { + i = str_chr(in, ':') + 2; + if (case_starts(in + i, "ok")) pok++; + if (case_starts(in + i, "deny")) pdeny++; + }; + break; + default: break; } } -int main(int argc, char **argv) +int main(int argc, char **argv) { int i; int c; @@ -258,27 +348,34 @@ int main(int argc, char **argv) stralloc line = {0}; calltime = now(); - if (argc < 2) - logmsg(WHO,100,USAGE,"qmail-mrtg [ -1 | -2 | -3 | -4 | -5 | -6 |\ + if (argc < 2) + logmsg( + WHO, + 100, + USAGE, + "qmail-mrtg [ -1 | -2 | -3 | -4 | -5 | -6 |\ -a | -b | -c | -d | -e | -f | -g | -h | -i | -j | -k | -z | -A | -B ] [time] \n\ qmail-mrtg needs to be called every [time] minutes (i.e. by crontab) - default 305 secs"); flag = *(argv[1] + 1); - if (argc == 3) { scan_ulong(argv[2],&u); history = 60 * u; } + if (argc == 3) { + scan_ulong(argv[2], &u); + history = 60 * u; + } -/* Read input lines sequentially */ + /* Read input lines sequentially */ - buffer_init(&bi,read,0,bufspace,sizeof(bufspace)); + buffer_init(&bi, read, 0, bufspace, sizeof(bufspace)); for (;;) { - if (getln(&bi,&line,&match,'\n') != 0) _exit(1); + if (getln(&bi, &line, &match, '\n') != 0) _exit(1); if (!match) break; if (!stralloc_0(&line)) _exit(1); seconds = 0; nanoseconds = 0; - if (line.s[0] == '@') { /* tai64 timestamp */ + if (line.s[0] == '@') { /* tai64 timestamp */ for (i = 1; i <= TAI64NLEN; i++) { c = (int)line.s[i]; u = c - '0'; @@ -300,23 +397,26 @@ int main(int argc, char **argv) _exit(1); } - if (enoughtime) { /* default history = 305 sec => evaluate logs every ~5 mins */ + if (enoughtime) { /* default history = 305 sec => evaluate logs every ~5 mins */ if (seconds <= calltime && seconds >= (calltime - history)) { - if (flag >= '1' && flag <= '9') mrtg_sendlog(line.s + TAI64NLEN + 2,flag); - else if (flag >= 'a' && flag <= 'z') mrtg_smtplog(line.s + TAI64NLEN + 2,flag); - else if (flag >= 'A' && flag <= 'Z') mrtg_pop3log(line.s + TAI64NLEN + 2,flag); + if (flag >= '1' && flag <= '9') + mrtg_sendlog(line.s + TAI64NLEN + 2, flag); + else if (flag >= 'a' && flag <= 'z') + mrtg_smtplog(line.s + TAI64NLEN + 2, flag); + else if (flag >= 'A' && flag <= 'Z') + mrtg_pop3log(line.s + TAI64NLEN + 2, flag); } - } else { + } else { if (seconds) { enoughtime++; } else { outs("Warning: Not enough time left between calls"); _exit(1); - } + } } } mrtg_results(flag); - + _exit(0); } diff --git a/src/qmail-newmrh.c b/src/qmail-newmrh.c index 4a74698..20945a4 100644 --- a/src/qmail-newmrh.c +++ b/src/qmail-newmrh.c @@ -1,27 +1,30 @@ -#include <unistd.h> #include <sys/stat.h> -#include <stdio.h> // rename -#include "logmsg.h" -#include "stralloc.h" +#include <unistd.h> + +#include <stdio.h> // rename + #include "buffer.h" -#include "getln.h" +#include "case.h" +#include "cdbmake.h" #include "exit.h" +#include "getln.h" +#include "logmsg.h" #include "open.h" +#include "stralloc.h" + #include "auto_qmail.h" -#include "cdbmake.h" -#include "case.h" #define WHO "qmail-newmrh" -int rename(const char *,const char *); // stdio.h +int rename(const char *, const char *); // stdio.h void die_read() { - logmsg(WHO,111,ERROR,"unable to read control/morercpthosts"); + logmsg(WHO, 111, ERROR, "unable to read control/morercpthosts"); } void die_write() { - logmsg(WHO,111,ERROR,"unable to write to control/morercpthosts.tmp"); + logmsg(WHO, 111, ERROR, "unable to write to control/morercpthosts.tmp"); } char inbuf[1024]; @@ -37,29 +40,36 @@ int match; int main() { umask(033); - if (chdir(auto_qmail) == -1) - logmsg(WHO,111,ERROR,B("unable to chdir to: ",auto_qmail)); + if (chdir(auto_qmail) == -1) logmsg(WHO, 111, ERROR, B("unable to chdir to: ", auto_qmail)); fd = open_read("control/morercpthosts"); if (fd == -1) die_read(); - buffer_init(&bi,read,fd,inbuf,sizeof(inbuf)); + buffer_init(&bi, read, fd, inbuf, sizeof(inbuf)); fdtemp = open_trunc("control/morercpthosts.tmp"); if (fdtemp == -1) die_write(); - if (cdb_make_start(&cdb,fdtemp) == -1) die_write(); + if (cdb_make_start(&cdb, fdtemp) == -1) die_write(); for (;;) { - if (getln(&bi,&line,&match,'\n') != 0) die_read(); - case_lowerb(line.s,line.len); + if (getln(&bi, &line, &match, '\n') != 0) die_read(); + case_lowerb(line.s, line.len); while (line.len) { - if (line.s[line.len - 1] == ' ') { --line.len; continue; } - if (line.s[line.len - 1] == '\n') { --line.len; continue; } - if (line.s[line.len - 1] == '\t') { --line.len; continue; } + if (line.s[line.len - 1] == ' ') { + --line.len; + continue; + } + if (line.s[line.len - 1] == '\n') { + --line.len; + continue; + } + if (line.s[line.len - 1] == '\t') { + --line.len; + continue; + } if (line.s[0] != '#') - if (cdb_make_add(&cdb,line.s,line.len,"",0) == -1) - die_write(); + if (cdb_make_add(&cdb, line.s, line.len, "", 0) == -1) die_write(); break; } if (!match) break; @@ -68,8 +78,8 @@ int main() if (cdb_make_finish(&cdb) == -1) die_write(); if (fsync(fdtemp) == -1) die_write(); if (close(fdtemp) == -1) die_write(); /* NFS stupidity */ - if (rename("control/morercpthosts.tmp","control/morercpthosts.cdb") == -1) - logmsg(WHO,111,ERROR,"unable to move control/morercpthosts.tmp to control/morercpthosts.cdb"); + if (rename("control/morercpthosts.tmp", "control/morercpthosts.cdb") == -1) + logmsg(WHO, 111, ERROR, "unable to move control/morercpthosts.tmp to control/morercpthosts.cdb"); _exit(0); } diff --git a/src/qmail-newu.c b/src/qmail-newu.c index d5e9baa..f2e33cd 100644 --- a/src/qmail-newu.c +++ b/src/qmail-newu.c @@ -1,52 +1,55 @@ -#include <unistd.h> #include <sys/stat.h> -#include <stdio.h> // rename -#include "stralloc.h" -#include "getln.h" +#include <unistd.h> + +#include <stdio.h> // rename + #include "buffer.h" +#include "byte.h" +#include "case.h" #include "cdbmake.h" #include "exit.h" -#include "open.h" +#include "getln.h" #include "logmsg.h" -#include "case.h" -#include "byte.h" +#include "open.h" +#include "stralloc.h" + #include "auto_qmail.h" #define WHO "qmail-newu" -int rename(const char *,const char *); // stdio.h +int rename(const char *, const char *); // stdio.h void die_chdir() { - logmsg(WHO,110,ERROR,"unable to chdir"); + logmsg(WHO, 110, ERROR, "unable to chdir"); } void die_nomem() { - logmsg(WHO,111,FATAL,"fatal: out of memory"); + logmsg(WHO, 111, FATAL, "fatal: out of memory"); } void die_opena() { - logmsg(WHO,112,ERROR,"unable to open users/assign"); + logmsg(WHO, 112, ERROR, "unable to open users/assign"); } void die_reada() { - logmsg(WHO,110,ERROR,"unable to read users/assign"); + logmsg(WHO, 110, ERROR, "unable to read users/assign"); } void die_format() { - logmsg(WHO,112,ERROR,"bad format in users/assign"); + logmsg(WHO, 112, ERROR, "bad format in users/assign"); } void die_opent() { - logmsg(WHO,112,ERROR,"unable to open users/assign.cdb.tmp"); + logmsg(WHO, 112, ERROR, "unable to open users/assign.cdb.tmp"); } void die_writet() { - logmsg(WHO,112,ERROR,"unable to write users/assign.cdb.tmp"); + logmsg(WHO, 112, ERROR, "unable to write users/assign.cdb.tmp"); } void die_rename() { - logmsg(WHO,112,ERROR,"unable to move users/cdb.tmp to users/assign.cdb"); + logmsg(WHO, 112, ERROR, "unable to move users/cdb.tmp to users/assign.cdb"); } struct cdb_make cdb; @@ -75,39 +78,38 @@ int main() fd = open_read("users/assign"); if (fd == -1) die_opena(); - buffer_init(&bi,read,fd,inbuf,sizeof(inbuf)); + buffer_init(&bi, read, fd, inbuf, sizeof(inbuf)); fdtemp = open_trunc("users/assign.cdb.tmp"); if (fdtemp == -1) die_opent(); - if (cdb_make_start(&cdb,fdtemp) == -1) die_writet(); + if (cdb_make_start(&cdb, fdtemp) == -1) die_writet(); - if (!stralloc_copys(&wildchars,"")) die_nomem(); + if (!stralloc_copys(&wildchars, "")) die_nomem(); for (;;) { - if (getln(&bi,&line,&match,'\n') != 0) die_reada(); + if (getln(&bi, &line, &match, '\n') != 0) die_reada(); if (line.len && (line.s[0] == '.')) break; if (!match) die_format(); - if (byte_chr(line.s,line.len,'\0') < line.len) die_format(); - i = byte_chr(line.s,line.len,':'); + if (byte_chr(line.s, line.len, '\0') < line.len) die_format(); + i = byte_chr(line.s, line.len, ':'); if (i == line.len) die_format(); if (i == 0) die_format(); - if (!stralloc_copys(&key,"!")) die_nomem(); + if (!stralloc_copys(&key, "!")) die_nomem(); if (line.s[0] == '+') { - if (!stralloc_catb(&key,line.s + 1,i - 1)) die_nomem(); - case_lowerb(key.s,key.len); + if (!stralloc_catb(&key, line.s + 1, i - 1)) die_nomem(); + case_lowerb(key.s, key.len); if (i >= 2) - if (byte_chr(wildchars.s,wildchars.len,line.s[i - 1]) == wildchars.len) - if (!stralloc_append(&wildchars,line.s + i - 1)) die_nomem(); - } - else { - if (!stralloc_catb(&key,line.s + 1,i - 1)) die_nomem(); + if (byte_chr(wildchars.s, wildchars.len, line.s[i - 1]) == wildchars.len) + if (!stralloc_append(&wildchars, line.s + i - 1)) die_nomem(); + } else { + if (!stralloc_catb(&key, line.s + 1, i - 1)) die_nomem(); if (!stralloc_0(&key)) die_nomem(); - case_lowerb(key.s,key.len); + case_lowerb(key.s, key.len); } - if (!stralloc_copyb(&data,line.s + i + 1,line.len - i - 1)) die_nomem(); + if (!stralloc_copyb(&data, line.s + i + 1, line.len - i - 1)) die_nomem(); numcolons = 0; for (i = 0; i < data.len; ++i) @@ -118,15 +120,15 @@ int main() if (numcolons < 6) die_format(); data.len = i; - if (cdb_make_add(&cdb,key.s,key.len,data.s,data.len) == -1) die_writet(); + if (cdb_make_add(&cdb, key.s, key.len, data.s, data.len) == -1) die_writet(); } - if (cdb_make_add(&cdb,"",0,wildchars.s,wildchars.len) == -1) die_writet(); + if (cdb_make_add(&cdb, "", 0, wildchars.s, wildchars.len) == -1) die_writet(); if (cdb_make_finish(&cdb) == -1) die_writet(); if (fsync(fdtemp) == -1) die_writet(); if (close(fdtemp) == -1) die_writet(); /* NFS stupidity */ - if (rename("users/assign.cdb.tmp","users/assign.cdb") == -1) die_rename(); + if (rename("users/assign.cdb.tmp", "users/assign.cdb") == -1) die_rename(); _exit(0); } diff --git a/src/qmail-pop3d.c b/src/qmail-pop3d.c index 2d26c16..bd46d7b 100644 --- a/src/qmail-pop3d.c +++ b/src/qmail-pop3d.c @@ -1,57 +1,62 @@ -#include <unistd.h> -#include <sys/types.h> #include <sys/stat.h> -#include "commands.h" -#include "sig.h" -#include "getln.h" -#include "stralloc.h" -#include "buffer.h" +#include <sys/types.h> +#include <unistd.h> + #include "alloc.h" +#include "buffer.h" +#include "exit.h" +#include "fmt.h" +#include "getln.h" #include "open.h" -#include "prioq.h" #include "scan.h" -#include "fmt.h" +#include "sig.h" #include "str.h" -#include "exit.h" -#include "maildir.h" +#include "stralloc.h" #include "timeout.h" -#define FDIN 0 +#include "commands.h" +#include "maildir.h" +#include "prioq.h" + +#define FDIN 0 #define FDOUT 1 -int rename(const char *,const char *); // stdio.h +int rename(const char *, const char *); // stdio.h -void die() { _exit(0); } +void die() +{ + _exit(0); +} -ssize_t saferead(int fd,char *buf,int len) +ssize_t saferead(int fd, char *buf, int len) { int r; - r = timeoutread(1200,fd,buf,len); + r = timeoutread(1200, fd, buf, len); if (r <= 0) die(); return r; } -ssize_t safewrite(int fd,char *buf,int len) +ssize_t safewrite(int fd, char *buf, int len) { int r; - r = timeoutwrite(1200,fd,buf,len); + r = timeoutwrite(1200, fd, buf, len); if (r <= 0) die(); return r; } char outbuf[1024]; -buffer bo = BUFFER_INIT(safewrite,FDOUT,outbuf,sizeof(outbuf)); +buffer bo = BUFFER_INIT(safewrite, FDOUT, outbuf, sizeof(outbuf)); char inbuf[128]; -buffer bi = BUFFER_INIT(saferead,FDIN,inbuf,sizeof(inbuf)); +buffer bi = BUFFER_INIT(saferead, FDIN, inbuf, sizeof(inbuf)); -void out(char *buf,int len) +void out(char *buf, int len) { - buffer_put(&bo,buf,len); + buffer_put(&bo, buf, len); } void outs(char *s) { - buffer_puts(&bo,s); + buffer_puts(&bo, s); } void flush() { @@ -65,50 +70,88 @@ void err(char *s) flush(); } -void die_nomem() { err("out of memory"); die(); } -void die_nomaildir() { err("this user has no $HOME/Maildir"); die(); } -void die_scan() { err("unable to scan $HOME/Maildir"); die(); } +void die_nomem() +{ + err("out of memory"); + die(); +} +void die_nomaildir() +{ + err("this user has no $HOME/Maildir"); + die(); +} +void die_scan() +{ + err("unable to scan $HOME/Maildir"); + die(); +} -void err_syntax() { err("syntax error"); } -void err_unimpl() { err("unimplemented"); } -void err_deleted() { err("already deleted"); } -void err_nozero() { err("messages are counted from 1"); } -void err_toobig() { err("not that many messages"); } -void err_nosuch() { err("unable to open that message"); } -void err_nounlink() { err("unable to unlink all deleted messages"); } +void err_syntax() +{ + err("syntax error"); +} +void err_unimpl() +{ + err("unimplemented"); +} +void err_deleted() +{ + err("already deleted"); +} +void err_nozero() +{ + err("messages are counted from 1"); +} +void err_toobig() +{ + err("not that many messages"); +} +void err_nosuch() +{ + err("unable to open that message"); +} +void err_nounlink() +{ + err("unable to unlink all deleted messages"); +} -void okay() { outs("+OK \r\n"); flush(); } +void okay() +{ + outs("+OK \r\n"); + flush(); +} void printfn(char *fn) { fn += 4; - out(fn,str_chr(fn,':')); + out(fn, str_chr(fn, ':')); } char strnum[FMT_ULONG]; stralloc line = {0}; -void blast(buffer *bf,unsigned long limit) +void blast(buffer *bf, unsigned long limit) { int match; int inheaders = 1; - + for (;;) { - if (getln(bf,&line,&match,'\n') != 0) die(); + if (getln(bf, &line, &match, '\n') != 0) die(); if (!match && !line.len) break; if (match) --line.len; /* no way to pass this info over POP */ - if (limit) if (!inheaders) if (!--limit) break; + if (limit) + if (!inheaders) + if (!--limit) break; if (!line.len) inheaders = 0; - else - if (line.s[0] == '.') - out(".",1); - out(line.s,line.len); - out("\r\n",2); - + else if (line.s[0] == '.') + out(".", 1); + out(line.s, line.len); + out("\r\n", 2); + if (!match) break; } - out("\r\n.\r\n",5); + out("\r\n.\r\n", 5); flush(); } @@ -129,20 +172,23 @@ void getlist() struct prioq_elt pe; struct stat st; int i; - + maildir_clean(&line); - if (maildir_scan(&pq,&filenames,1,1) == -1) die_scan(); - + if (maildir_scan(&pq, &filenames, 1, 1) == -1) die_scan(); + numm = pq.p ? pq.len : 0; - m = (struct message *) alloc(numm * sizeof(struct message)); + m = (struct message *)alloc(numm * sizeof(struct message)); if (!m) die_nomem(); - + for (i = 0; i < numm; ++i) { - if (!prioq_min(&pq,&pe)) { numm = i; break; } + if (!prioq_min(&pq, &pe)) { + numm = i; + break; + } prioq_delmin(&pq); m[i].fn = filenames.s + pe.id; m[i].flagdeleted = 0; - if (stat(m[i].fn,&st) == -1) + if (stat(m[i].fn, &st) == -1) m[i].size = 0; else m[i].size = st.st_size; @@ -153,15 +199,15 @@ void pop3_stat() { int i; unsigned long total; - + total = 0; - for (i = 0; i < numm; ++i) + for (i = 0; i < numm; ++i) if (!m[i].flagdeleted) total += m[i].size; outs("+OK "); - out(strnum,fmt_uint(strnum,numm)); + out(strnum, fmt_uint(strnum, numm)); outs(" "); - out(strnum,fmt_ulong(strnum,total)); + out(strnum, fmt_ulong(strnum, total)); outs("\r\n"); flush(); } @@ -170,8 +216,7 @@ void pop3_rset() { int i; - for (i = 0; i < numm; ++i) - m[i].flagdeleted = 0; + for (i = 0; i < numm; ++i) m[i].flagdeleted = 0; last = 0; okay(); } @@ -179,7 +224,7 @@ void pop3_rset() void pop3_last() { outs("+OK "); - out(strnum,fmt_uint(strnum,last)); + out(strnum, fmt_uint(strnum, last)); outs("\r\n"); flush(); } @@ -192,12 +237,12 @@ void pop3_quit() if (m[i].flagdeleted) { if (unlink(m[i].fn) == -1) err_nounlink(); } else { - if (str_start(m[i].fn,"new/")) { - if (!stralloc_copys(&line,"cur/")) die_nomem(); - if (!stralloc_cats(&line,m[i].fn + 4)) die_nomem(); - if (!stralloc_cats(&line,":2,")) die_nomem(); + if (str_start(m[i].fn, "new/")) { + if (!stralloc_copys(&line, "cur/")) die_nomem(); + if (!stralloc_cats(&line, m[i].fn + 4)) die_nomem(); + if (!stralloc_cats(&line, ":2,")) die_nomem(); if (!stralloc_0(&line)) die_nomem(); - rename(m[i].fn,line.s); /* if it fails, bummer */ + rename(m[i].fn, line.s); /* if it fails, bummer */ } } okay(); @@ -208,11 +253,23 @@ int msgno(char *arg) { unsigned long u; - if (!scan_ulong(arg,&u)) { err_syntax(); return -1; } - if (!u) { err_nozero(); return -1; } + if (!scan_ulong(arg, &u)) { + err_syntax(); + return -1; + } + if (!u) { + err_nozero(); + return -1; + } --u; - if (u >= numm) { err_toobig(); return -1; } - if (m[u].flagdeleted) { err_deleted(); return -1; } + if (u >= numm) { + err_toobig(); + return -1; + } + if (m[u].flagdeleted) { + err_deleted(); + return -1; + } return u; } @@ -227,16 +284,18 @@ void pop3_dele(char *arg) okay(); } -void list(int i,int flaguidl) +void list(int i, int flaguidl) { - out(strnum,fmt_uint(strnum,i + 1)); + out(strnum, fmt_uint(strnum, i + 1)); outs(" "); - if (flaguidl) printfn(m[i].fn); - else out(strnum,fmt_ulong(strnum,m[i].size)); + if (flaguidl) + printfn(m[i].fn); + else + out(strnum, fmt_ulong(strnum, m[i].size)); outs("\r\n"); } -void dolisting(char *arg,int flaguidl) +void dolisting(char *arg, int flaguidl) { unsigned int i; @@ -245,69 +304,80 @@ void dolisting(char *arg,int flaguidl) if (i == -1) return; outs("+OK "); - list(i,flaguidl); + list(i, flaguidl); } else { okay(); for (i = 0; i < numm; ++i) - if (!m[i].flagdeleted) list(i,flaguidl); + if (!m[i].flagdeleted) list(i, flaguidl); outs(".\r\n"); } flush(); } -void pop3_uidl(char *arg) { dolisting(arg,1); } -void pop3_list(char *arg) { dolisting(arg,0); } +void pop3_uidl(char *arg) +{ + dolisting(arg, 1); +} +void pop3_list(char *arg) +{ + dolisting(arg, 0); +} char msgbuf[1024]; -buffer bm; +buffer bm; void pop3_top(char *arg) { int i; unsigned long limit; int fd; - + i = msgno(arg); if (i == -1) return; - - arg += scan_ulong(arg,&limit); + + arg += scan_ulong(arg, &limit); while (*arg == ' ') ++arg; - if (scan_ulong(arg,&limit)) ++limit; - else limit = 0; - + if (scan_ulong(arg, &limit)) + ++limit; + else + limit = 0; + fd = open_read(m[i].fn); - if (fd == -1) { err_nosuch(); return; } + if (fd == -1) { + err_nosuch(); + return; + } okay(); - buffer_init(&bm,read,fd,msgbuf,sizeof(msgbuf)); - blast(&bm,limit); + buffer_init(&bm, read, fd, msgbuf, sizeof(msgbuf)); + blast(&bm, limit); close(fd); } struct commands pop3commands[] = { - { "quit", pop3_quit, 0 } -, { "stat", pop3_stat, 0 } -, { "list", pop3_list, 0 } -, { "uidl", pop3_uidl, 0 } -, { "dele", pop3_dele, 0 } -, { "retr", pop3_top, 0 } -, { "rset", pop3_rset, 0 } -, { "last", pop3_last, 0 } -, { "top", pop3_top, 0 } -, { "noop", okay, 0 } -, { 0, err_unimpl, 0 } -} ; - -int main(int argc,char **argv) + {"quit", pop3_quit, 0}, + {"stat", pop3_stat, 0}, + {"list", pop3_list, 0}, + {"uidl", pop3_uidl, 0}, + {"dele", pop3_dele, 0}, + {"retr", pop3_top, 0}, + {"rset", pop3_rset, 0}, + {"last", pop3_last, 0}, + { "top", pop3_top, 0}, + {"noop", okay, 0}, + { 0, err_unimpl, 0} +}; + +int main(int argc, char **argv) { sig_alarmcatch(die); sig_pipeignore(); - + if (!argv[1]) die_nomaildir(); if (chdir(argv[1]) == -1) die_nomaildir(); - + getlist(); okay(); - commands(&bi,pop3commands); + commands(&bi, pop3commands); die(); } diff --git a/src/qmail-popup.c b/src/qmail-popup.c index 522e719..832a56f 100644 --- a/src/qmail-popup.c +++ b/src/qmail-popup.c @@ -1,55 +1,60 @@ #include <unistd.h> -#include "commands.h" -#include "fd.h" -#include "sig.h" -#include "stralloc.h" -#include "buffer.h" + #include "alloc.h" -#include "wait.h" -#include "str.h" +#include "buffer.h" #include "byte.h" -#include "now.h" -#include "fmt.h" #include "case.h" +#include "env.h" #include "exit.h" +#include "fd.h" +#include "fmt.h" +#include "ip.h" +#include "sig.h" +#include "str.h" +#include "stralloc.h" #include "timeout.h" -#include "env.h" +#include "wait.h" + +#include "commands.h" +#include "now.h" #include "tls_start.h" -#include "ip.h" #define PORT_POP3S "995" -#define FDIN 0 -#define FDOUT 1 -#define FDAUTH 3 -#define FDLOG 5 +#define FDIN 0 +#define FDOUT 1 +#define FDAUTH 3 +#define FDLOG 5 -void die() { _exit(1); } +void die() +{ + _exit(1); +} -ssize_t saferead(int fd,char *buf,int len) +ssize_t saferead(int fd, char *buf, int len) { int r; - r = timeoutread(1200,fd,buf,len); + r = timeoutread(1200, fd, buf, len); if (r <= 0) die(); return r; } -ssize_t safewrite(int fd,char *buf,int len) +ssize_t safewrite(int fd, char *buf, int len) { int r; - r = timeoutwrite(1200,fd,buf,len); + r = timeoutwrite(1200, fd, buf, len); if (r <= 0) die(); return r; } char outbuf[128]; -buffer bo = BUFFER_INIT(safewrite,FDOUT,outbuf,sizeof(outbuf)); +buffer bo = BUFFER_INIT(safewrite, FDOUT, outbuf, sizeof(outbuf)); char inbuf[128]; -buffer bi = BUFFER_INIT(saferead,FDIN,inbuf,sizeof(inbuf)); +buffer bi = BUFFER_INIT(saferead, FDIN, inbuf, sizeof(inbuf)); void outs(char *s) { - buffer_puts(&bo,s); + buffer_puts(&bo, s); } void flush() { @@ -65,7 +70,7 @@ void err(char *s) /* Logging */ -stralloc protocol = {0}; +stralloc protocol = {0}; stralloc auth = {0}; char *localport; char *remoteip; @@ -73,49 +78,131 @@ char *remotehost; char strnum[FMT_ULONG]; char logbuf[512]; -buffer bl = BUFFER_INIT(safewrite,FDLOG,logbuf,sizeof(logbuf)); - -void logs(char *s) { if (buffer_puts(&bl,s) == -1) _exit(1); } -void logp(char *s) { logs(" P:"); logs(s); } -void logh(char *s1, char *s2) { logs(" S:"); logs(s1); logs(":"); logs(s2); } -void logu(char *s) { logs(" ?~ '"); logs(s); logs("'"); } -void logn(char *s) { if (buffer_puts(&bl,s) == -1) _exit(1); if (buffer_flush(&bl) == -1) _exit(1); } -void logpid() { strnum[fmt_ulong(strnum,getpid())] = 0; logs("qmail-popup: pid "); logs(strnum); logs(" "); } -void log_pop(char *s1,char *s2,char *s3,char *s4,char *s5,char *s6) - { logpid(); logs(s1); logs(s2); logp(s3); logh(s4,s5), logu(s6), logn("\n"); } - -void die_usage() { err("usage: popup hostname subprogram"); die(); } -void die_nomem() { err("out of memory"); die(); } -void die_pipe() { err("unable to open pipe"); die(); } -void die_write() { err("unable to write pipe"); die(); } -void die_fork() { err("unable to fork"); die(); } -void die_childcrashed() { err("aack, child crashed"); } -void die_badauth() { err("authorization failed"); } -void die_tls() { err("TLS startup failed"); die(); } -void die_notls() { - err("TLS required but not negotiated"); - log_pop("Reject::STLS::","Any","POP3",remoteip,remotehost,"unknown"); - die(); -} - -void err_syntax() { err("syntax error"); } -void err_wantuser() { err("USER first"); } -void err_authoriz() { err("authorization first"); } - -void okay() { outs("+OK \r\n"); flush(); } -void pop3_quit() { okay(); die(); } +buffer bl = BUFFER_INIT(safewrite, FDLOG, logbuf, sizeof(logbuf)); + +void logs(char *s) +{ + if (buffer_puts(&bl, s) == -1) _exit(1); +} +void logp(char *s) +{ + logs(" P:"); + logs(s); +} +void logh(char *s1, char *s2) +{ + logs(" S:"); + logs(s1); + logs(":"); + logs(s2); +} +void logu(char *s) +{ + logs(" ?~ '"); + logs(s); + logs("'"); +} +void logn(char *s) +{ + if (buffer_puts(&bl, s) == -1) _exit(1); + if (buffer_flush(&bl) == -1) _exit(1); +} +void logpid() +{ + strnum[fmt_ulong(strnum, getpid())] = 0; + logs("qmail-popup: pid "); + logs(strnum); + logs(" "); +} +void log_pop(char *s1, char *s2, char *s3, char *s4, char *s5, char *s6) +{ + logpid(); + logs(s1); + logs(s2); + logp(s3); + logh(s4, s5), logu(s6), logn("\n"); +} + +void die_usage() +{ + err("usage: popup hostname subprogram"); + die(); +} +void die_nomem() +{ + err("out of memory"); + die(); +} +void die_pipe() +{ + err("unable to open pipe"); + die(); +} +void die_write() +{ + err("unable to write pipe"); + die(); +} +void die_fork() +{ + err("unable to fork"); + die(); +} +void die_childcrashed() +{ + err("aack, child crashed"); +} +void die_badauth() +{ + err("authorization failed"); +} +void die_tls() +{ + err("TLS startup failed"); + die(); +} +void die_notls() +{ + err("TLS required but not negotiated"); + log_pop("Reject::STLS::", "Any", "POP3", remoteip, remotehost, "unknown"); + die(); +} + +void err_syntax() +{ + err("syntax error"); +} +void err_wantuser() +{ + err("USER first"); +} +void err_authoriz() +{ + err("authorization first"); +} + +void okay() +{ + outs("+OK \r\n"); + flush(); +} +void pop3_quit() +{ + okay(); + die(); +} void poplog_init() { - if (!stralloc_copys(&protocol,"POP3")) die_nomem(); + if (!stralloc_copys(&protocol, "POP3")) die_nomem(); localport = env_get("TCP6LOCALPORT"); - if (!localport) localport = env_get("TCPLOCALPORT"); + if (!localport) localport = env_get("TCPLOCALPORT"); if (!localport) localport = "unknown"; - if (!case_diffs(localport,PORT_POP3S)) - if (!stralloc_cats(&protocol,"S")) die_nomem(); + if (!case_diffs(localport, PORT_POP3S)) + if (!stralloc_cats(&protocol, "S")) die_nomem(); remoteip = env_get("TCP6REMOTEIP"); - if (remoteip && byte_equal(remoteip,7,V4MAPPREFIX)) remoteip = remoteip + 7; - if (!remoteip) remoteip = env_get("TCPREMOTEIP"); + if (remoteip && byte_equal(remoteip, 7, V4MAPPREFIX)) remoteip = remoteip + 7; + if (!remoteip) remoteip = env_get("TCPREMOTEIP"); if (!remoteip) remoteip = "unknown"; remotehost = env_get("TCP6REMOTEHOST"); if (!remotehost) remotehost = env_get("TCPREMOTEHOST"); @@ -133,47 +220,45 @@ int stls = 0; int seenstls = 0; int apop = 0; -void doanddie(char *user,unsigned int userlen,char *pass) /* userlen: including 0 byte */ +void doanddie(char *user, unsigned int userlen, char *pass) /* userlen: including 0 byte */ { int child; int wstat; int pi[2]; - - if (fd_copy(2,1) == -1) die_pipe(); + + if (fd_copy(2, 1) == -1) die_pipe(); close(FDAUTH); if (pipe(pi) == -1) die_pipe(); if (pi[0] != FDAUTH) die_pipe(); switch (child = fork()) { - case -1: - die_fork(); + case -1: die_fork(); case 0: close(pi[1]); sig_pipedefault(); - execvp(*childargs,childargs); + execvp(*childargs, childargs); _exit(1); } close(pi[0]); - buffer_init(&ba,write,pi[1],authbuf,sizeof(authbuf)); - if (buffer_put(&ba,user,userlen) == -1) die_write(); - if (buffer_put(&ba,pass,str_len(pass) + 1) == -1) die_write(); - if (buffer_puts(&ba,"<") == -1) die_write(); - if (buffer_puts(&ba,unique) == -1) die_write(); - if (buffer_puts(&ba,hostname) == -1) die_write(); - if (buffer_put(&ba,">",2) == -1) die_write(); + buffer_init(&ba, write, pi[1], authbuf, sizeof(authbuf)); + if (buffer_put(&ba, user, userlen) == -1) die_write(); + if (buffer_put(&ba, pass, str_len(pass) + 1) == -1) die_write(); + if (buffer_puts(&ba, "<") == -1) die_write(); + if (buffer_puts(&ba, unique) == -1) die_write(); + if (buffer_puts(&ba, hostname) == -1) die_write(); + if (buffer_put(&ba, ">", 2) == -1) die_write(); if (buffer_flush(&ba) == -1) die_write(); close(pi[1]); - byte_zero(pass,str_len(pass)); - byte_zero(authbuf,sizeof(authbuf)); - if (wait_pid(&wstat,child) == -1) die(); + byte_zero(pass, str_len(pass)); + byte_zero(authbuf, sizeof(authbuf)); + if (wait_pid(&wstat, child) == -1) die(); if (wait_crashed(wstat)) die_childcrashed(); if (!stralloc_0(&auth)) die_nomem(); if (!stralloc_0(&protocol)) die_nomem(); - if (wait_exitcode(wstat)) { - die_badauth(); - log_pop("Reject::AUTH::",auth.s,protocol.s,remoteip,remotehost,user); - } - else - log_pop("Accept::AUTH::",auth.s,protocol.s,remoteip,remotehost,user); + if (wait_exitcode(wstat)) { + die_badauth(); + log_pop("Reject::AUTH::", auth.s, protocol.s, remoteip, remotehost, user); + } else + log_pop("Accept::AUTH::", auth.s, protocol.s, remoteip, remotehost, user); die(); } @@ -181,9 +266,9 @@ void pop3_greet() { char *s; s = unique; - s += fmt_uint(s,getpid()); + s += fmt_uint(s, getpid()); *s++ = '.'; - s += fmt_ulong(s,(unsigned long) now()); + s += fmt_ulong(s, (unsigned long)now()); *s++ = '@'; *s++ = 0; @@ -201,19 +286,28 @@ void pop3_greet() void pop3_user(char *arg) { if (stls == 2 && !seenstls) die_notls(); - if (!*arg) { err_syntax(); return; } + if (!*arg) { + err_syntax(); + return; + } okay(); seenuser = 1; - if (!stralloc_copys(&username,arg)) die_nomem(); - if (!stralloc_0(&username)) die_nomem(); + if (!stralloc_copys(&username, arg)) die_nomem(); + if (!stralloc_0(&username)) die_nomem(); } void pop3_pass(char *arg) { - if (!seenuser) { err_wantuser(); return; } - if (!*arg) { err_syntax(); return; } - if (!stralloc_copys(&auth,"User")) die_nomem(); - doanddie(username.s,username.len,arg); + if (!seenuser) { + err_wantuser(); + return; + } + if (!*arg) { + err_syntax(); + return; + } + if (!stralloc_copys(&auth, "User")) die_nomem(); + doanddie(username.s, username.len, arg); } void pop3_apop(char *arg) @@ -221,11 +315,14 @@ void pop3_apop(char *arg) char *space; if (stls == 2 && !seenstls) die_notls(); - space = arg + str_chr(arg,' '); - if (!*space) { err_syntax(); return; } + space = arg + str_chr(arg, ' '); + if (!*space) { + err_syntax(); + return; + } *space++ = 0; - if (!stralloc_copys(&auth,"Apop")) die_nomem(); - doanddie(arg,space - arg,space); + if (!stralloc_copys(&auth, "Apop")) die_nomem(); + doanddie(arg, space - arg, space); } void pop3_capa(char *arg) @@ -234,49 +331,46 @@ void pop3_capa(char *arg) outs("TOP\r\n"); outs("USER\r\n"); outs("UIDL\r\n"); - if (apop) - outs("APOP\r\n"); - if (stls > 0) - outs("STLS\r\n"); + if (apop) outs("APOP\r\n"); + if (stls > 0) outs("STLS\r\n"); outs(".\r\n"); flush(); } void pop3_stls(char *arg) { - if (stls == 0 || seenstls == 1) - return err("STLS not available"); + if (stls == 0 || seenstls == 1) return err("STLS not available"); outs("+OK starting TLS negotiation\r\n"); flush(); if (!starttls_init()) die_tls(); - buffer_init(&bi,saferead,FDIN,inbuf,sizeof(inbuf)); + buffer_init(&bi, saferead, FDIN, inbuf, sizeof(inbuf)); seenstls = 1; - -/* reset state */ + + /* reset state */ seenuser = 0; - if (!stralloc_cats(&protocol,"S")) die_nomem(); + if (!stralloc_cats(&protocol, "S")) die_nomem(); } struct commands pop3commands[] = { - { "user", pop3_user, 0 } -, { "pass", pop3_pass, 0 } -, { "apop", pop3_apop, 0 } -, { "quit", pop3_quit, 0 } -, { "capa", pop3_capa, 0 } -, { "stls", pop3_stls, 0 } -, { "noop", okay, 0 } -, { 0, err_authoriz, 0 } + {"user", pop3_user, 0}, + {"pass", pop3_pass, 0}, + {"apop", pop3_apop, 0}, + {"quit", pop3_quit, 0}, + {"capa", pop3_capa, 0}, + {"stls", pop3_stls, 0}, + {"noop", okay, 0}, + { 0, err_authoriz, 0} }; -int main(int argc,char **argv) +int main(int argc, char **argv) { char *pop3auth; char *ucspitls; sig_alarmcatch(die); sig_pipeignore(); - + hostname = argv[1]; if (!hostname) die_usage(); childargs = argv + 2; @@ -285,17 +379,17 @@ int main(int argc,char **argv) ucspitls = env_get("UCSPITLS"); if (ucspitls) { stls = 1; - if (!case_diffs(ucspitls,"-")) stls = 0; - if (!case_diffs(ucspitls,"!")) stls = 2; + if (!case_diffs(ucspitls, "-")) stls = 0; + if (!case_diffs(ucspitls, "!")) stls = 2; } pop3auth = env_get("POP3AUTH"); if (pop3auth) { - if (case_starts(pop3auth,"apop")) apop = 2; - if (case_starts(pop3auth,"+apop")) apop = 1; + if (case_starts(pop3auth, "apop")) apop = 2; + if (case_starts(pop3auth, "+apop")) apop = 1; } - poplog_init(); + poplog_init(); pop3_greet(); - commands(&bi,pop3commands); + commands(&bi, pop3commands); die(); } diff --git a/src/qmail-postgrey.c b/src/qmail-postgrey.c index bd88389..942dee6 100644 --- a/src/qmail-postgrey.c +++ b/src/qmail-postgrey.c @@ -1,16 +1,17 @@ -#include <unistd.h> -#include <sys/socket.h> #include <netinet/in.h> -#include "stralloc.h" -#include "logmsg.h" -#include "ip.h" +#include <sys/socket.h> +#include <unistd.h> + #include "case.h" -#include "str.h" #include "exit.h" +#include "ip.h" +#include "logmsg.h" #include "scan.h" +#include "socket_if.h" +#include "str.h" +#include "stralloc.h" #include "timeout.h" #include "timeoutconn.h" -#include "socket_if.h" #define WHO "qmail-postgrey" @@ -33,73 +34,76 @@ int main(int argc, char **argv) int i, j, r; if (argc != 6) - logmsg(WHO,100,USAGE,"qmail-postgrey ip%ifidx;port sender recipient client_address client_name"); + logmsg(WHO, 100, USAGE, "qmail-postgrey ip%ifidx;port sender recipient client_address client_name"); remoteip = argv[1]; - i = str_chr(remoteip,':'); + i = str_chr(remoteip, ':'); if (remoteip[i] == ':') { - j = str_chr(remoteip,'%'); /* IF index */ + j = str_chr(remoteip, '%'); /* IF index */ if (remoteip[j] == '%') { remoteip[j] = 0; netif = &remoteip[j + 1]; ifidx = socket_getifidx(netif); } - if (!ip6_scan(remoteip,(char *)&ip6.d)) - logmsg(WHO,111,FATAL,B("No valid IPv6 address provided: ",remoteip)); - pgfd = socket(AF_INET6,SOCK_STREAM,0); - if (pgfd == -1) - logmsg(WHO,111,FATAL,"Can't bind to IPv6 socket."); - r = timeoutconn6(pgfd,(char *)&ip6.d,port,CT,ifidx); + if (!ip6_scan(remoteip, (char *)&ip6.d)) + logmsg(WHO, 111, FATAL, B("No valid IPv6 address provided: ", remoteip)); + pgfd = socket(AF_INET6, SOCK_STREAM, 0); + if (pgfd == -1) logmsg(WHO, 111, FATAL, "Can't bind to IPv6 socket."); + r = timeoutconn6(pgfd, (char *)&ip6.d, port, CT, ifidx); } else { - if (!ip4_scan(remoteip,(char *)&ip4.d)) - logmsg(WHO,111,FATAL,B("No valid IPv6 address provided: ",remoteip)); - pgfd = socket(AF_INET,SOCK_STREAM,0); - if (pgfd == -1) - logmsg(WHO,111,FATAL,"Can't bind to IPv4 socket."); - r = timeoutconn4(pgfd,(char *)&ip4.d,port,CT); + if (!ip4_scan(remoteip, (char *)&ip4.d)) + logmsg(WHO, 111, FATAL, B("No valid IPv6 address provided: ", remoteip)); + pgfd = socket(AF_INET, SOCK_STREAM, 0); + if (pgfd == -1) logmsg(WHO, 111, FATAL, "Can't bind to IPv4 socket."); + r = timeoutconn4(pgfd, (char *)&ip4.d, port, CT); } if (r != 0) { - if (errno == ETIMEDOUT) - close(pgfd); - logmsg(WHO,111,FATAL,B("Can't communicate with postgrey server: ",remoteip)); + if (errno == ETIMEDOUT) close(pgfd); + logmsg(WHO, 111, FATAL, B("Can't communicate with postgrey server: ", remoteip)); _exit(1); } /* Provide SMTP connect vector to postgrey server */ - if (!stralloc_copys(&query,"request=smtpd_access_policy\nclient_address=")) _exit(1); - if (!stralloc_cats(&query,argv[4])) _exit(1); - if (!stralloc_cats(&query,"\nclient_name=")) _exit(1); - if (!stralloc_cats(&query,argv[5])) _exit(1); - if (!stralloc_cats(&query,"\nsender=")) _exit(1); - if (!stralloc_cats(&query,argv[2])) _exit(1); - if (!stralloc_cats(&query,"\nrecipient=")) _exit(1); - if (!stralloc_cats(&query,argv[3])) _exit(1); - if (!stralloc_cats(&query,"\n\n")) _exit(1); + if (!stralloc_copys(&query, "request=smtpd_access_policy\nclient_address=")) _exit(1); + if (!stralloc_cats(&query, argv[4])) _exit(1); + if (!stralloc_cats(&query, "\nclient_name=")) _exit(1); + if (!stralloc_cats(&query, argv[5])) _exit(1); + if (!stralloc_cats(&query, "\nsender=")) _exit(1); + if (!stralloc_cats(&query, argv[2])) _exit(1); + if (!stralloc_cats(&query, "\nrecipient=")) _exit(1); + if (!stralloc_cats(&query, argv[3])) _exit(1); + if (!stralloc_cats(&query, "\n\n")) _exit(1); do { - r = timeoutwrite(WT,pgfd,query.s,query.len); + r = timeoutwrite(WT, pgfd, query.s, query.len); } while (r == -1 && errno == EINTR); - if (r != query.len) { close(pgfd); _exit(1); } + if (r != query.len) { + close(pgfd); + _exit(1); + } /* Read response */ do { - r = timeoutread(RT,pgfd,buf,sizeof(buf)); + r = timeoutread(RT, pgfd, buf, sizeof(buf)); } while (r == -1 && errno == EINTR); - if (r == -1) { close(pgfd); _exit(1); } + if (r == -1) { + close(pgfd); + _exit(1); + } close(pgfd); -// logmsg(WHO,0,INFO,buf); + // logmsg(WHO,0,INFO,buf); if (r >= 12) - if (!case_diffb(buf,12,"action=dunno")) _exit(0); + if (!case_diffb(buf, 12, "action=dunno")) _exit(0); if (r >= 14) - if (!case_diffb(buf,14,"action=prepend")) _exit(0); + if (!case_diffb(buf, 14, "action=prepend")) _exit(0); if (r >= 22) - if (!case_diffb(buf,22,"action=defer_if_permit")) _exit(10); + if (!case_diffb(buf, 22, "action=defer_if_permit")) _exit(10); exit(1); } diff --git a/src/qmail-pw2u.c b/src/qmail-pw2u.c index 4ca7d25..9b0a09b 100644 --- a/src/qmail-pw2u.c +++ b/src/qmail-pw2u.c @@ -1,78 +1,80 @@ -#include <unistd.h> -#include <sys/types.h> #include <sys/stat.h> +#include <sys/types.h> +#include <unistd.h> + #include "buffer.h" -#include "getoptb.h" -#include "control.h" +#include "byte.h" #include "constmap.h" -#include "stralloc.h" -#include "fmt.h" -#include "str.h" -#include "scan.h" -#include "open.h" -#include "logmsg.h" #include "exit.h" +#include "fmt.h" #include "getln.h" -#include "byte.h" +#include "getoptb.h" +#include "logmsg.h" +#include "open.h" +#include "scan.h" +#include "str.h" +#include "stralloc.h" + #include "auto_break.h" #include "auto_qmail.h" #include "auto_usera.h" +#include "control.h" #define WHO "qmail-pw2u" void die_chdir() { - buffer_putsflush(buffer_2,"qmail-pw2u: fatal: unable to chdir\n"); + buffer_putsflush(buffer_2, "qmail-pw2u: fatal: unable to chdir\n"); _exit(111); } void die_nomem() { - buffer_putsflush(buffer_2,"qmail-pw2u: fatal: out of memory\n"); + buffer_putsflush(buffer_2, "qmail-pw2u: fatal: out of memory\n"); _exit(111); } void die_read() { - buffer_putsflush(buffer_2,"qmail-pw2u: fatal: unable to read input\n"); + buffer_putsflush(buffer_2, "qmail-pw2u: fatal: unable to read input\n"); _exit(111); } void die_write() { - buffer_putsflush(buffer_2,"qmail-pw2u: fatal: unable to write output\n"); + buffer_putsflush(buffer_2, "qmail-pw2u: fatal: unable to write output\n"); _exit(111); } void die_control() { - buffer_putsflush(buffer_2,"qmail-pw2u: fatal: unable to read controls\n"); + buffer_putsflush(buffer_2, "qmail-pw2u: fatal: unable to read controls\n"); _exit(111); } void die_alias() { - buffer_puts(buffer_2,"qmail-pw2u: fatal: unable to find "); - buffer_puts(buffer_2,auto_usera); - buffer_puts(buffer_2," user\n"); + buffer_puts(buffer_2, "qmail-pw2u: fatal: unable to find "); + buffer_puts(buffer_2, auto_usera); + buffer_puts(buffer_2, " user\n"); buffer_flush(buffer_2); _exit(111); } void die_home(char *fn) { - buffer_puts(buffer_2,"qmail-pw2u: fatal: unable to stat "); - buffer_puts(buffer_2,fn); - buffer_puts(buffer_2,"\n"); + buffer_puts(buffer_2, "qmail-pw2u: fatal: unable to stat "); + buffer_puts(buffer_2, fn); + buffer_puts(buffer_2, "\n"); buffer_flush(buffer_2); _exit(111); } -void die_user(char *s,unsigned int len) +void die_user(char *s, unsigned int len) { - buffer_puts(buffer_2,"qmail-pw2u: fatal: unable to find "); - buffer_put(buffer_2,s,len); - buffer_puts(buffer_2," user for subuser\n"); + buffer_puts(buffer_2, "qmail-pw2u: fatal: unable to find "); + buffer_put(buffer_2, s, len); + buffer_puts(buffer_2, " user for subuser\n"); buffer_flush(buffer_2); _exit(111); } @@ -85,11 +87,18 @@ int homestrategy = 2; /* 1: stop if home does not exist; skip if home is not owned by user */ /* 0: don't worry about home */ -int okincl; stralloc incl = {0}; struct constmap mapincl; -int okexcl; stralloc excl = {0}; struct constmap mapexcl; -int okmana; stralloc mana = {0}; struct constmap mapmana; +int okincl; +stralloc incl = {0}; +struct constmap mapincl; +int okexcl; +stralloc excl = {0}; +struct constmap mapexcl; +int okmana; +stralloc mana = {0}; +struct constmap mapmana; -stralloc allusers = {0}; struct constmap mapuser; +stralloc allusers = {0}; +struct constmap mapuser; stralloc uugh = {0}; stralloc user = {0}; @@ -108,37 +117,57 @@ void doaccount() char *x; unsigned int xlen; - if (byte_chr(line.s,line.len,'\0') < line.len) return; + if (byte_chr(line.s, line.len, '\0') < line.len) return; - x = line.s; xlen = line.len; i = byte_chr(x,xlen,':'); if (i == xlen) return; - if (!stralloc_copyb(&user,x,i)) die_nomem(); + x = line.s; + xlen = line.len; + i = byte_chr(x, xlen, ':'); + if (i == xlen) return; + if (!stralloc_copyb(&user, x, i)) die_nomem(); if (!stralloc_0(&user)) die_nomem(); - ++i; x += i; xlen -= i; i = byte_chr(x,xlen,':'); if (i == xlen) return; - ++i; x += i; xlen -= i; i = byte_chr(x,xlen,':'); if (i == xlen) return; - if (!stralloc_copyb(&uidstr,x,i)) die_nomem(); + ++i; + x += i; + xlen -= i; + i = byte_chr(x, xlen, ':'); + if (i == xlen) return; + ++i; + x += i; + xlen -= i; + i = byte_chr(x, xlen, ':'); + if (i == xlen) return; + if (!stralloc_copyb(&uidstr, x, i)) die_nomem(); if (!stralloc_0(&uidstr)) die_nomem(); - scan_ulong(uidstr.s,&uid); - ++i; x += i; xlen -= i; i = byte_chr(x,xlen,':'); if (i == xlen) return; - if (!stralloc_copyb(&gidstr,x,i)) die_nomem(); + scan_ulong(uidstr.s, &uid); + ++i; + x += i; + xlen -= i; + i = byte_chr(x, xlen, ':'); + if (i == xlen) return; + if (!stralloc_copyb(&gidstr, x, i)) die_nomem(); if (!stralloc_0(&gidstr)) die_nomem(); - ++i; x += i; xlen -= i; i = byte_chr(x,xlen,':'); if (i == xlen) return; - ++i; x += i; xlen -= i; i = byte_chr(x,xlen,':'); if (i == xlen) return; - if (!stralloc_copyb(&home,x,i)) die_nomem(); + ++i; + x += i; + xlen -= i; + i = byte_chr(x, xlen, ':'); + if (i == xlen) return; + ++i; + x += i; + xlen -= i; + i = byte_chr(x, xlen, ':'); + if (i == xlen) return; + if (!stralloc_copyb(&home, x, i)) die_nomem(); if (!stralloc_0(&home)) die_nomem(); if (!uid) return; if (flagnoupper) for (i = 0; i < user.len; ++i) - if ((user.s[i] >= 'A') && (user.s[i] <= 'Z')) - return; + if ((user.s[i] >= 'A') && (user.s[i] <= 'Z')) return; if (okincl) - if (!constmap(&mapincl,user.s,user.len - 1)) - return; + if (!constmap(&mapincl, user.s, user.len - 1)) return; if (okexcl) - if (constmap(&mapexcl,user.s,user.len - 1)) - return; + if (constmap(&mapexcl, user.s, user.len - 1)) return; if (homestrategy) { - if (stat(home.s,&st) == -1) { + if (stat(home.s, &st) == -1) { if (errno != ENOENT) die_home(home.s); if (homestrategy == 1) die_home(home.s); return; @@ -146,54 +175,52 @@ void doaccount() if (st.st_uid != uid) return; } - if (!stralloc_copys(&uugh,":")) die_nomem(); - if (!stralloc_cats(&uugh,user.s)) die_nomem(); - if (!stralloc_cats(&uugh,":")) die_nomem(); - if (!stralloc_cats(&uugh,uidstr.s)) die_nomem(); - if (!stralloc_cats(&uugh,":")) die_nomem(); - if (!stralloc_cats(&uugh,gidstr.s)) die_nomem(); - if (!stralloc_cats(&uugh,":")) die_nomem(); - if (!stralloc_cats(&uugh,home.s)) die_nomem(); - if (!stralloc_cats(&uugh,":")) die_nomem(); + if (!stralloc_copys(&uugh, ":")) die_nomem(); + if (!stralloc_cats(&uugh, user.s)) die_nomem(); + if (!stralloc_cats(&uugh, ":")) die_nomem(); + if (!stralloc_cats(&uugh, uidstr.s)) die_nomem(); + if (!stralloc_cats(&uugh, ":")) die_nomem(); + if (!stralloc_cats(&uugh, gidstr.s)) die_nomem(); + if (!stralloc_cats(&uugh, ":")) die_nomem(); + if (!stralloc_cats(&uugh, home.s)) die_nomem(); + if (!stralloc_cats(&uugh, ":")) die_nomem(); /* XXX: avoid recording in allusers unlein sub actually needs it */ - if (!stralloc_cats(&allusers,user.s)) die_nomem(); - if (!stralloc_cats(&allusers,":")) die_nomem(); - if (!stralloc_catb(&allusers,uugh.s,uugh.len)) die_nomem(); + if (!stralloc_cats(&allusers, user.s)) die_nomem(); + if (!stralloc_cats(&allusers, ":")) die_nomem(); + if (!stralloc_catb(&allusers, uugh.s, uugh.len)) die_nomem(); if (!stralloc_0(&allusers)) die_nomem(); - if (str_equal(user.s,auto_usera)) { - if (buffer_puts(buffer_1,"+") == -1) die_write(); - if (buffer_put(buffer_1,uugh.s,uugh.len) == -1) die_write(); - if (buffer_puts(buffer_1,dashcolon) == -1) die_write(); - if (buffer_puts(buffer_1,":\n") == -1) die_write(); + if (str_equal(user.s, auto_usera)) { + if (buffer_puts(buffer_1, "+") == -1) die_write(); + if (buffer_put(buffer_1, uugh.s, uugh.len) == -1) die_write(); + if (buffer_puts(buffer_1, dashcolon) == -1) die_write(); + if (buffer_puts(buffer_1, ":\n") == -1) die_write(); flagalias = 1; } mailnames = 0; - if (okmana) - mailnames = constmap(&mapmana,user.s,user.len - 1); - if (!mailnames) - mailnames = user.s; + if (okmana) mailnames = constmap(&mapmana, user.s, user.len - 1); + if (!mailnames) mailnames = user.s; for (;;) { while (*mailnames == ':') ++mailnames; if (!*mailnames) break; - i = str_chr(mailnames,':'); + i = str_chr(mailnames, ':'); + + if (buffer_puts(buffer_1, "=") == -1) die_write(); + if (buffer_put(buffer_1, mailnames, i) == -1) die_write(); + if (buffer_put(buffer_1, uugh.s, uugh.len) == -1) die_write(); + if (buffer_puts(buffer_1, "::\n") == -1) die_write(); - if (buffer_puts(buffer_1,"=") == -1) die_write(); - if (buffer_put(buffer_1,mailnames,i) == -1) die_write(); - if (buffer_put(buffer_1,uugh.s,uugh.len) == -1) die_write(); - if (buffer_puts(buffer_1,"::\n") == -1) die_write(); - if (*auto_break) { - if (buffer_puts(buffer_1,"+") == -1) die_write(); - if (buffer_put(buffer_1,mailnames,i) == -1) die_write(); - if (buffer_put(buffer_1,auto_break,1) == -1) die_write(); - if (buffer_put(buffer_1,uugh.s,uugh.len) == -1) die_write(); - if (buffer_puts(buffer_1,dashcolon) == -1) die_write(); - if (buffer_puts(buffer_1,":\n") == -1) die_write(); + if (buffer_puts(buffer_1, "+") == -1) die_write(); + if (buffer_put(buffer_1, mailnames, i) == -1) die_write(); + if (buffer_put(buffer_1, auto_break, 1) == -1) die_write(); + if (buffer_put(buffer_1, uugh.s, uugh.len) == -1) die_write(); + if (buffer_puts(buffer_1, dashcolon) == -1) die_write(); + if (buffer_puts(buffer_1, ":\n") == -1) die_write(); } mailnames += i; @@ -209,28 +236,39 @@ void dosubuser() unsigned int xlen; char *uugh; - x = line.s; xlen = line.len; i = byte_chr(x,xlen,':'); if (i == xlen) return; - if (!stralloc_copyb(&sub,x,i)) die_nomem(); - ++i; x += i; xlen -= i; i = byte_chr(x,xlen,':'); if (i == xlen) return; - uugh = constmap(&mapuser,x,i); - if (!uugh) die_user(x,i); - ++i; x += i; xlen -= i; i = byte_chr(x,xlen,':'); if (i == xlen) return; - - if (buffer_puts(buffer_1,"=") == -1) die_write(); - if (buffer_put(buffer_1,sub.s,sub.len) == -1) die_write(); - if (buffer_puts(buffer_1,uugh) == -1) die_write(); - if (buffer_puts(buffer_1,dashcolon) == -1) die_write(); - if (buffer_put(buffer_1,x,i) == -1) die_write(); - if (buffer_puts(buffer_1,":\n") == -1) die_write(); + x = line.s; + xlen = line.len; + i = byte_chr(x, xlen, ':'); + if (i == xlen) return; + if (!stralloc_copyb(&sub, x, i)) die_nomem(); + ++i; + x += i; + xlen -= i; + i = byte_chr(x, xlen, ':'); + if (i == xlen) return; + uugh = constmap(&mapuser, x, i); + if (!uugh) die_user(x, i); + ++i; + x += i; + xlen -= i; + i = byte_chr(x, xlen, ':'); + if (i == xlen) return; + + if (buffer_puts(buffer_1, "=") == -1) die_write(); + if (buffer_put(buffer_1, sub.s, sub.len) == -1) die_write(); + if (buffer_puts(buffer_1, uugh) == -1) die_write(); + if (buffer_puts(buffer_1, dashcolon) == -1) die_write(); + if (buffer_put(buffer_1, x, i) == -1) die_write(); + if (buffer_puts(buffer_1, ":\n") == -1) die_write(); if (*auto_break) { - if (buffer_puts(buffer_1,"+") == -1) die_write(); - if (buffer_put(buffer_1,sub.s,sub.len) == -1) die_write(); - if (buffer_put(buffer_1,auto_break,1) == -1) die_write(); - if (buffer_puts(buffer_1,uugh) == -1) die_write(); - if (buffer_puts(buffer_1,dashcolon) == -1) die_write(); - if (buffer_put(buffer_1,x,i) == -1) die_write(); - if (buffer_puts(buffer_1,"-:\n") == -1) die_write(); + if (buffer_puts(buffer_1, "+") == -1) die_write(); + if (buffer_put(buffer_1, sub.s, sub.len) == -1) die_write(); + if (buffer_put(buffer_1, auto_break, 1) == -1) die_write(); + if (buffer_puts(buffer_1, uugh) == -1) die_write(); + if (buffer_puts(buffer_1, dashcolon) == -1) die_write(); + if (buffer_put(buffer_1, x, i) == -1) die_write(); + if (buffer_puts(buffer_1, "-:\n") == -1) die_write(); } } @@ -238,13 +276,13 @@ int fd; char inbuf[BUFFER_INSIZE]; buffer in; -int main(int argc,char **argv) +int main(int argc, char **argv) { int opt; int match; - while ((opt = getopt(argc,argv,"/ohHuUc:C")) != opteof) - switch (opt) { + while ((opt = getopt(argc, argv, "/ohHuUc:C")) != opteof) switch (opt) + { case '/': dashcolon = "-/:"; break; case 'o': homestrategy = 2; break; case 'h': homestrategy = 1; break; @@ -254,30 +292,32 @@ int main(int argc,char **argv) case 'c': *auto_break = *optarg; break; case 'C': *auto_break = 0; break; case '?': - default: - _exit(100); + default: _exit(100); } if (chdir(auto_qmail) == -1) die_chdir(); /* no need for control_init() */ - okincl = control_readfile(&incl,"users/include",0); + okincl = control_readfile(&incl, "users/include", 0); if (okincl == -1) die_control(); - if (okincl) if (!constmap_init(&mapincl,incl.s,incl.len,0)) die_nomem(); + if (okincl) + if (!constmap_init(&mapincl, incl.s, incl.len, 0)) die_nomem(); - okexcl = control_readfile(&excl,"users/exclude",0); + okexcl = control_readfile(&excl, "users/exclude", 0); if (okexcl == -1) die_control(); - if (okexcl) if (!constmap_init(&mapexcl,excl.s,excl.len,0)) die_nomem(); + if (okexcl) + if (!constmap_init(&mapexcl, excl.s, excl.len, 0)) die_nomem(); - okmana = control_readfile(&mana,"users/mailnames",0); + okmana = control_readfile(&mana, "users/mailnames", 0); if (okmana == -1) die_control(); - if (okmana) if (!constmap_init(&mapmana,mana.s,mana.len,1)) die_nomem(); + if (okmana) + if (!constmap_init(&mapmana, mana.s, mana.len, 1)) die_nomem(); - if (!stralloc_copys(&allusers,"")) die_nomem(); + if (!stralloc_copys(&allusers, "")) die_nomem(); for (;;) { - if (getln(buffer_0,&line,&match,'\n') == -1) die_read(); + if (getln(buffer_0, &line, &match, '\n') == -1) die_read(); doaccount(); if (!match) break; } @@ -286,14 +326,13 @@ int main(int argc,char **argv) fd = open_read("users/subusers"); if (fd == -1) { if (errno != ENOENT) die_control(); - } - else { - buffer_init(&in,read,fd,inbuf,sizeof(inbuf)); + } else { + buffer_init(&in, read, fd, inbuf, sizeof(inbuf)); - if (!constmap_init(&mapuser,allusers.s,allusers.len,1)) die_nomem(); + if (!constmap_init(&mapuser, allusers.s, allusers.len, 1)) die_nomem(); for (;;) { - if (getln(&in,&line,&match,'\n') == -1) die_read(); + if (getln(&in, &line, &match, '\n') == -1) die_read(); dosubuser(); if (!match) break; } @@ -304,17 +343,16 @@ int main(int argc,char **argv) fd = open_read("users/append"); if (fd == -1) { if (errno != ENOENT) die_control(); - } - else { - buffer_init(&in,read,fd,inbuf,sizeof(inbuf)); + } else { + buffer_init(&in, read, fd, inbuf, sizeof(inbuf)); for (;;) { - if (getln(&in,&line,&match,'\n') == -1) die_read(); - if (buffer_put(buffer_1,line.s,line.len) == -1) die_write(); + if (getln(&in, &line, &match, '\n') == -1) die_read(); + if (buffer_put(buffer_1, line.s, line.len) == -1) die_write(); if (!match) break; } } - if (buffer_puts(buffer_1,".\n") == -1) die_write(); + if (buffer_puts(buffer_1, ".\n") == -1) die_write(); if (buffer_flush(buffer_1) == -1) die_write(); _exit(0); } diff --git a/src/qmail-qmaint.c b/src/qmail-qmaint.c index e83ab6f..70605c8 100644 --- a/src/qmail-qmaint.c +++ b/src/qmail-qmaint.c @@ -1,38 +1,40 @@ /* Based on an implementation of queue-fix 1.2 by Eric Huss */ -#include <unistd.h> -#include <sys/stat.h> -#include <pwd.h> #include <grp.h> -#include "stralloc.h" +#include <pwd.h> +#include <sys/stat.h> +#include <unistd.h> + +#include "buffer.h" #include "direntry.h" -#include "fmt.h" -#include "fmtqfn.h" #include "error.h" -#include "buffer.h" +#include "exit.h" +#include "fifo.h" +#include "fmt.h" #include "getln.h" -#include "str.h" +#include "logmsg.h" #include "open.h" -#include "fifo.h" #include "scan.h" -#include "readsubdir.h" -#include "logmsg.h" -#include "exit.h" +#include "str.h" +#include "stralloc.h" + #include "auto_qmail.h" #include "auto_split.h" #include "auto_uids.h" +#include "fmtqfn.h" +#include "readsubdir.h" #define WHO "qmail-qmaint" -stralloc queue_dir = {0}; /*the root queue dir with trailing slash*/ -stralloc check_dir = {0}; /*the current directory being checked*/ -stralloc temp_dirname = {0}; /*temporary used for checking directories */ +stralloc queue_dir = {0}; /*the root queue dir with trailing slash*/ +stralloc check_dir = {0}; /*the current directory being checked*/ +stralloc temp_dirname = {0}; /*temporary used for checking directories */ stralloc temp_filename = {0}; /*temporary used for checking individuals*/ -stralloc old_name = {0}; /*used in rename*/ -stralloc new_name = {0}; /*used in rename*/ -stralloc mess_dir = {0}; /*used for renaming in mess dir*/ -stralloc query = {0}; /*used in interactive query function*/ +stralloc old_name = {0}; /*used in rename*/ +stralloc new_name = {0}; /*used in rename*/ +stralloc mess_dir = {0}; /*used for renaming in mess dir*/ +stralloc query = {0}; /*used in interactive query function*/ char strnum[FMT_ULONG]; int flag_interactive = 0; @@ -50,32 +52,41 @@ int split_num; void die_make(char *name) { - logmsg(WHO,111,ERROR,B("Failed to make: ",name)); + logmsg(WHO, 111, ERROR, B("Failed to make: ", name)); } void die_user(char *user) { - logmsg(WHO,111,ERROR,B("Failed to determine uid of: ",user)); + logmsg(WHO, 111, ERROR, B("Failed to determine uid of: ", user)); } void die_group(char *group) { - logmsg(WHO,111,ERROR,B("Failed to determine gid of: ",group)); + logmsg(WHO, 111, ERROR, B("Failed to determine gid of: ", group)); } void die_check() { - logmsg(WHO,111,ERROR,"Failed while checking directory structure. \nEnsure the given queue exists and you have permission to access it."); + logmsg( + WHO, + 111, + ERROR, + "Failed while checking directory structure. \nEnsure the given queue exists and you have " + "permission to access it."); } void die_recon() { - logmsg(WHO,110,ERROR,"Failed to reconstruct queue. \nEnsure the queue exists and you have permission to modify it."); + logmsg( + WHO, + 110, + ERROR, + "Failed to reconstruct queue. \nEnsure the queue exists and you have permission to modify it."); } void die_nomem() { - logmsg(WHO,110,ERROR,"Out of memory."); + logmsg(WHO, 110, ERROR, "Out of memory."); } /*returns 1==yes, 0==no*/ @@ -84,7 +95,7 @@ int confirm() { int match; - if (getln(buffer_0,&query,&match,'\n')) return 0; + if (getln(buffer_0, &query, &match, '\n')) return 0; if (!match) return 0; if (query.s[0] == 'y' || query.s[0] == 'Y' || query.s[0] == '\n') return 1; return 0; @@ -92,16 +103,19 @@ int confirm() /*gid may be -1 on files for "unknown*/ -#define DIRS logmsg(WHO,0,WARN,"It looks like some directories don't exist, should I create them? (Y/n)") -#define FILES logmsg(WHO,0,WARN,"It looks like some files don't exist, should I create them? (Y/n)") +#define DIRS \ + logmsg(WHO, 0, WARN, "It looks like some directories don't exist, should I create them? (Y/n)") +#define FILES logmsg(WHO, 0, WARN, "It looks like some files don't exist, should I create them? (Y/n)") -#define PERMS logmsg(WHO,0,WARN,B("It looks like permissions are wrong for ",name," should I fix them? (Y/n)")) -#define CPERMS logmsg(WHO,0,WARN,B("Changing permissions: ",name," => ",pnum)) +#define PERMS \ + logmsg(WHO, 0, WARN, B("It looks like permissions are wrong for ", name, " should I fix them? (Y/n)")) +#define CPERMS logmsg(WHO, 0, WARN, B("Changing permissions: ", name, " => ", pnum)) -#define OWNER logmsg(WHO,0,WARN,B("It looks like ownerships are wrong for ",name," should I fix them? (Y/n)")) -#define COWNER logmsg(WHO,0,WARN,B("Changing ownership: ",name," => ",unum,"/",gnum)) +#define OWNER \ + logmsg(WHO, 0, WARN, B("It looks like ownerships are wrong for ", name, " should I fix them? (Y/n)")) +#define COWNER logmsg(WHO, 0, WARN, B("Changing ownership: ", name, " => ", unum, "/", gnum)) -int check_item(char *name,int uid,int gid,int perm,char type,int size) +int check_item(char *name, int uid, int gid, int perm, char type, int size) { struct stat st; int fd; @@ -110,131 +124,189 @@ int check_item(char *name,int uid,int gid,int perm,char type,int size) char gnum[12]; char pnum[12]; - /*check for existence and proper credentials*/ + /*check for existence and proper credentials*/ - strnum[fmt_ulong(unum,uid)] = 0; - strnum[fmt_ulong(gnum,gid)] = 0; - strnum[fmt_ulong(pnum,perm)] = 0; + strnum[fmt_ulong(unum, uid)] = 0; + strnum[fmt_ulong(gnum, gid)] = 0; + strnum[fmt_ulong(pnum, perm)] = 0; switch (type) { - case 'd': /*directory*/ - if (stat(name,&st)) { - if (errno != ENOENT) return -1; - if (!flag_dircreate && flag_interactive) { - DIRS; if (!confirm()) return -1; - flag_dircreate = 1; - } - /*create it*/ - logmsg(WHO,0,INFO,B("Creating directory: ",name)); - if (mkdir(name,perm)) die_make(name); - CPERMS; if (chmod(name,perm)) die_make(name); - COWNER; if (chown(name,uid,gid)) die_make(name); - return 0; - } - /*check the values*/ - if (st.st_uid != uid || st.st_gid != gid) { - if (!flag_permfix && flag_interactive) { OWNER; if (!confirm()) return -1; flag_permfix = 1; } - COWNER; if (chown(name,uid,gid)) die_make(name); - } - if ((st.st_mode & 07777) != perm) { - if (!flag_permfix && flag_interactive) { PERMS; if (!confirm()) return -1; flag_permfix = 1; } - CPERMS; if (chmod(name,perm)) die_make(name); - } - return 0; - case 'f': /*regular file*/ - if (stat(name,&st)) return -1; - /*check the values*/ - if (st.st_uid != uid || (st.st_gid != gid && gid != -1)) { - if (!flag_permfix && flag_interactive) { OWNER; if (!confirm()) return -1; flag_permfix = 1; } - COWNER; if (chown(name,uid,gid)) die_make(name); - } - if ((st.st_mode & 07777) != perm) { - if (!flag_permfix && flag_interactive) { PERMS; if (!confirm()) return -1; flag_permfix = 1; } - CPERMS; if (chmod(name,perm)) die_make(name); - } - return 0; - case 'z': /*regular file with a size*/ - if (stat(name,&st)) { - if (errno != ENOENT) return -1; - if (!flag_filecreate && flag_interactive) { - FILES; if (!confirm()) return -1; - flag_filecreate = 1; - } - /*create it*/ - - strnum[fmt_ulong(num,size)] = 0; - logmsg(WHO,0,INFO,B("Creating: ",name," with size ",num)); - fd = open_trunc(name); - if (fd == -1) die_make(name); - while (size--) { if (write(fd,"",1)!=1) die_make(name); } - close(fd); - CPERMS; if (chmod(name,perm)) die_make(name); - COWNER; if (chown(name,uid,gid)) die_make(name); - return 0; - } - /*check the values*/ - if (st.st_uid != uid || (st.st_gid != gid && gid != -1)) { - if (!flag_permfix && flag_interactive) { OWNER; if (!confirm()) return -1; flag_permfix = 1; } - COWNER; if (chown(name,uid,gid)) die_make(name); - } - if ((st.st_mode & 07777) != perm) { - if (!flag_permfix && flag_interactive) { PERMS; if (!confirm()) return -1; flag_permfix = 1; } - CPERMS; if (chmod(name,perm)) die_make(name); - } - if (st.st_size != size) { - logmsg(WHO,0,WARN,B("File ",name," has not the right size. I will not fix it, please investigate.")); - } - return 0; + case 'd': /*directory*/ + if (stat(name, &st)) { + if (errno != ENOENT) return -1; + if (!flag_dircreate && flag_interactive) { + DIRS; + if (!confirm()) return -1; + flag_dircreate = 1; + } + /*create it*/ + logmsg(WHO, 0, INFO, B("Creating directory: ", name)); + if (mkdir(name, perm)) die_make(name); + CPERMS; + if (chmod(name, perm)) die_make(name); + COWNER; + if (chown(name, uid, gid)) die_make(name); + return 0; + } + /*check the values*/ + if (st.st_uid != uid || st.st_gid != gid) { + if (!flag_permfix && flag_interactive) { + OWNER; + if (!confirm()) return -1; + flag_permfix = 1; + } + COWNER; + if (chown(name, uid, gid)) die_make(name); + } + if ((st.st_mode & 07777) != perm) { + if (!flag_permfix && flag_interactive) { + PERMS; + if (!confirm()) return -1; + flag_permfix = 1; + } + CPERMS; + if (chmod(name, perm)) die_make(name); + } + return 0; + case 'f': /*regular file*/ + if (stat(name, &st)) return -1; + /*check the values*/ + if (st.st_uid != uid || (st.st_gid != gid && gid != -1)) { + if (!flag_permfix && flag_interactive) { + OWNER; + if (!confirm()) return -1; + flag_permfix = 1; + } + COWNER; + if (chown(name, uid, gid)) die_make(name); + } + if ((st.st_mode & 07777) != perm) { + if (!flag_permfix && flag_interactive) { + PERMS; + if (!confirm()) return -1; + flag_permfix = 1; + } + CPERMS; + if (chmod(name, perm)) die_make(name); + } + return 0; + case 'z': /*regular file with a size*/ + if (stat(name, &st)) { + if (errno != ENOENT) return -1; + if (!flag_filecreate && flag_interactive) { + FILES; + if (!confirm()) return -1; + flag_filecreate = 1; + } + /*create it*/ + + strnum[fmt_ulong(num, size)] = 0; + logmsg(WHO, 0, INFO, B("Creating: ", name, " with size ", num)); + fd = open_trunc(name); + if (fd == -1) die_make(name); + while (size--) { + if (write(fd, "", 1) != 1) die_make(name); + } + close(fd); + CPERMS; + if (chmod(name, perm)) die_make(name); + COWNER; + if (chown(name, uid, gid)) die_make(name); + return 0; + } + /*check the values*/ + if (st.st_uid != uid || (st.st_gid != gid && gid != -1)) { + if (!flag_permfix && flag_interactive) { + OWNER; + if (!confirm()) return -1; + flag_permfix = 1; + } + COWNER; + if (chown(name, uid, gid)) die_make(name); + } + if ((st.st_mode & 07777) != perm) { + if (!flag_permfix && flag_interactive) { + PERMS; + if (!confirm()) return -1; + flag_permfix = 1; + } + CPERMS; + if (chmod(name, perm)) die_make(name); + } + if (st.st_size != size) { + logmsg( + WHO, + 0, + WARN, + B("File ", name, " has not the right size. I will not fix it, please investigate.")); + } + return 0; case 'p': /*a named pipe*/ - if (stat(name,&st)) { - if (errno != ENOENT) return -1; - if (!flag_filecreate && flag_interactive) { - FILES; if (!confirm()) return -1; - flag_filecreate = 1; - } - /*create it*/ - logmsg(WHO,INFO,0,B("Creating fifo: ",name)); - if (fifo_make(name,perm)) die_make(name); - CPERMS; if (chmod(name,perm)) die_make(name); - COWNER; if (chown(name,uid,gid)) die_make(name); - return 0; - } - /*check the values*/ - if (st.st_uid != uid || (st.st_gid != gid && gid != -1)) { - if (!flag_permfix && flag_interactive) { OWNER; if (!confirm()) return -1; flag_permfix = 1; } - COWNER; if (chown(name,uid,gid)) die_make(name); - } - if ((st.st_mode & 07777) != perm) { - if (!flag_permfix && flag_interactive) { PERMS; if (!confirm()) return -1; flag_permfix = 1; } - CPERMS; if (chmod(name,perm)) die_make(name); - } - return 0; - } + if (stat(name, &st)) { + if (errno != ENOENT) return -1; + if (!flag_filecreate && flag_interactive) { + FILES; + if (!confirm()) return -1; + flag_filecreate = 1; + } + /*create it*/ + logmsg(WHO, INFO, 0, B("Creating fifo: ", name)); + if (fifo_make(name, perm)) die_make(name); + CPERMS; + if (chmod(name, perm)) die_make(name); + COWNER; + if (chown(name, uid, gid)) die_make(name); + return 0; + } + /*check the values*/ + if (st.st_uid != uid || (st.st_gid != gid && gid != -1)) { + if (!flag_permfix && flag_interactive) { + OWNER; + if (!confirm()) return -1; + flag_permfix = 1; + } + COWNER; + if (chown(name, uid, gid)) die_make(name); + } + if ((st.st_mode & 07777) != perm) { + if (!flag_permfix && flag_interactive) { + PERMS; + if (!confirm()) return -1; + flag_permfix = 1; + } + CPERMS; + if (chmod(name, perm)) die_make(name); + } + return 0; + } return 0; } -int check_files(char * directory,int uid,int gid,int perm) +int check_files(char *directory, int uid, int gid, int perm) { DIR *dir; direntry *d; dir = opendir(directory); - + if (!dir) return -1; while ((d = readdir(dir))) { if (d->d_name[0] == '.') continue; - if (!stralloc_copys(&temp_filename,directory)) die_nomem(); - if (!stralloc_append(&temp_filename,"/")) die_nomem(); - if (!stralloc_cats(&temp_filename,d->d_name)) die_nomem(); + if (!stralloc_copys(&temp_filename, directory)) die_nomem(); + if (!stralloc_append(&temp_filename, "/")) die_nomem(); + if (!stralloc_cats(&temp_filename, d->d_name)) die_nomem(); if (!stralloc_0(&temp_filename)) die_nomem(); - if (check_item(temp_filename.s,uid,gid,perm,'f',0)) { closedir(dir); return -1; } + if (check_item(temp_filename.s, uid, gid, perm, 'f', 0)) { + closedir(dir); + return -1; + } } closedir(dir); return 0; } -void warn_files(char * directory) +void warn_files(char *directory) { DIR *dir; direntry *d; @@ -251,71 +323,80 @@ void warn_files(char * directory) closedir(dir); - if (found) - logmsg(WHO,0,WARN,B("Found files in ",directory," that shouldn't be there. I will not remove them. You should consider checking it out.")); + if (found) + logmsg( + WHO, + 0, + WARN, + B("Found files in ", + directory, + " that shouldn't be there. I will not remove them. You should consider checking it out.")); } -int check_splits(char * directory,int dir_uid,int dir_gid,int dir_perm,int file_gid,int file_perm) +int check_splits(char *directory, int dir_uid, int dir_gid, int dir_perm, int file_gid, int file_perm) { DIR *dir; direntry *d; int i; - for (i = 0; i < split_num ; i++) { - strnum[fmt_ulong(strnum,i)] = 0; - if (!stralloc_copys(&temp_dirname,directory)) die_nomem(); - if (!stralloc_append(&temp_dirname,"/")) die_nomem(); - if (!stralloc_cats(&temp_dirname,strnum)) die_nomem(); + for (i = 0; i < split_num; i++) { + strnum[fmt_ulong(strnum, i)] = 0; + if (!stralloc_copys(&temp_dirname, directory)) die_nomem(); + if (!stralloc_append(&temp_dirname, "/")) die_nomem(); + if (!stralloc_cats(&temp_dirname, strnum)) die_nomem(); if (!stralloc_0(&temp_dirname)) die_nomem(); /*check the split dir*/ - if (check_item(temp_dirname.s,dir_uid,dir_gid,dir_perm,'d',0)) return -1; + if (check_item(temp_dirname.s, dir_uid, dir_gid, dir_perm, 'd', 0)) return -1; /*check its contents*/ dir = opendir(temp_dirname.s); if (!dir) return -1; while ((d = readdir(dir))) { if (d->d_name[0] == '.') continue; - if (!stralloc_copys(&temp_filename,temp_dirname.s)) die_nomem(); - if (!stralloc_append(&temp_filename,"/")) die_nomem(); - if (!stralloc_cats(&temp_filename,d->d_name)) die_nomem(); + if (!stralloc_copys(&temp_filename, temp_dirname.s)) die_nomem(); + if (!stralloc_append(&temp_filename, "/")) die_nomem(); + if (!stralloc_cats(&temp_filename, d->d_name)) die_nomem(); if (!stralloc_0(&temp_filename)) die_nomem(); - if (check_item(temp_filename.s,dir_uid,file_gid,file_perm,'f',0)) { closedir(dir); return -1; } + if (check_item(temp_filename.s, dir_uid, file_gid, file_perm, 'f', 0)) { + closedir(dir); + return -1; + } } closedir(dir); - } + } return 0; } int rename_mess(char *dir, char *part, char *new_part, char *old_filename, char *new_filename) { - + if (flag_interactive && !flag_namefix) { - logmsg(WHO,0,INFO,"It looks like some files need to be renamed, should I rename them? (Y/n)\n"); + logmsg(WHO, 0, INFO, "It looks like some files need to be renamed, should I rename them? (Y/n)\n"); if (!confirm()) return -1; flag_namefix = 1; } /*prepare the old filename*/ - if (!stralloc_copy(&old_name,&queue_dir)) die_nomem(); - if (!stralloc_cats(&old_name,dir)) die_nomem(); - if (!stralloc_cats(&old_name,part)) die_nomem(); - if (!stralloc_append(&old_name,"/")) die_nomem(); - if (!stralloc_cats(&old_name,old_filename)) die_nomem(); + if (!stralloc_copy(&old_name, &queue_dir)) die_nomem(); + if (!stralloc_cats(&old_name, dir)) die_nomem(); + if (!stralloc_cats(&old_name, part)) die_nomem(); + if (!stralloc_append(&old_name, "/")) die_nomem(); + if (!stralloc_cats(&old_name, old_filename)) die_nomem(); if (!stralloc_0(&old_name)) die_nomem(); /*prepare the new filename*/ - if (!stralloc_copy(&new_name,&queue_dir)) die_nomem(); - if (!stralloc_cats(&new_name,dir)) die_nomem(); - if (!stralloc_cats(&new_name,new_part)) die_nomem(); - if (!stralloc_append(&new_name,"/")) die_nomem(); - if (!stralloc_cats(&new_name,new_filename)) die_nomem(); + if (!stralloc_copy(&new_name, &queue_dir)) die_nomem(); + if (!stralloc_cats(&new_name, dir)) die_nomem(); + if (!stralloc_cats(&new_name, new_part)) die_nomem(); + if (!stralloc_append(&new_name, "/")) die_nomem(); + if (!stralloc_cats(&new_name, new_filename)) die_nomem(); if (!stralloc_0(&new_name)) die_nomem(); - logmsg(WHO,0,INFO,B("Renaming ",old_name.s," to ",new_name.s)); - if (rename(old_name.s,new_name.s)) { - if (errno != ENOENT) return -1; + logmsg(WHO, 0, INFO, B("Renaming ", old_name.s, " to ", new_name.s)); + if (rename(old_name.s, new_name.s)) { + if (errno != ENOENT) return -1; } return 0; @@ -332,11 +413,11 @@ int fix_part(char *part) int part_num; int correct_part_num; - scan_uint(part,&part_num); + scan_uint(part, &part_num); - if (!stralloc_copy(&mess_dir,&queue_dir)) die_nomem(); - if (!stralloc_cats(&mess_dir,"mess/")) die_nomem(); - if (!stralloc_cats(&mess_dir,part)) die_nomem(); + if (!stralloc_copy(&mess_dir, &queue_dir)) die_nomem(); + if (!stralloc_cats(&mess_dir, "mess/")) die_nomem(); + if (!stralloc_cats(&mess_dir, part)) die_nomem(); if (!stralloc_0(&mess_dir)) die_nomem(); dir = opendir(mess_dir.s); @@ -345,28 +426,52 @@ int fix_part(char *part) while ((d = readdir(dir))) { if (d->d_name[0] == '.') continue; /*check from mess*/ - if (!stralloc_copys(&temp_filename,mess_dir.s)) die_nomem(); - if (!stralloc_append(&temp_filename,"/")) die_nomem(); - if (!stralloc_cats(&temp_filename,d->d_name)) die_nomem(); + if (!stralloc_copys(&temp_filename, mess_dir.s)) die_nomem(); + if (!stralloc_append(&temp_filename, "/")) die_nomem(); + if (!stralloc_cats(&temp_filename, d->d_name)) die_nomem(); if (!stralloc_0(&temp_filename)) die_nomem(); - if (stat(temp_filename.s,&st)) { closedir(dir); return -1; } + if (stat(temp_filename.s, &st)) { + closedir(dir); + return -1; + } /*check that filename == inode number*/ /*check that inode%auto_split == part_num*/ - scan_uint(d->d_name,&old_inode); + scan_uint(d->d_name, &old_inode); correct_part_num = st.st_ino % split_num; if (st.st_ino != old_inode || part_num != correct_part_num) { /*rename*/ - inode[fmt_ulong(inode,st.st_ino)] = 0; - new_part[fmt_ulong(new_part,correct_part_num)] = 0; - if (rename_mess("mess/",part,new_part,d->d_name,inode)) { closedir(dir); return -1; } - if (rename_mess("info/",part,new_part,d->d_name,inode)) { closedir(dir); return -1; } - if (rename_mess("local/",part,new_part,d->d_name,inode)) { closedir(dir); return -1; } - if (rename_mess("remote/",part,new_part,d->d_name,inode)) { closedir(dir); return -1; } - if (rename_mess("todo/",part,new_part,d->d_name,inode)) { closedir(dir); return -1; } - if (rename_mess("intd/",part,new_part,d->d_name,inode)) { closedir(dir); return -1; } - - if (rename_mess("bounce","","",d->d_name,inode)) { closedir(dir); return -1; } + inode[fmt_ulong(inode, st.st_ino)] = 0; + new_part[fmt_ulong(new_part, correct_part_num)] = 0; + if (rename_mess("mess/", part, new_part, d->d_name, inode)) { + closedir(dir); + return -1; + } + if (rename_mess("info/", part, new_part, d->d_name, inode)) { + closedir(dir); + return -1; + } + if (rename_mess("local/", part, new_part, d->d_name, inode)) { + closedir(dir); + return -1; + } + if (rename_mess("remote/", part, new_part, d->d_name, inode)) { + closedir(dir); + return -1; + } + if (rename_mess("todo/", part, new_part, d->d_name, inode)) { + closedir(dir); + return -1; + } + if (rename_mess("intd/", part, new_part, d->d_name, inode)) { + closedir(dir); + return -1; + } + + if (rename_mess("bounce", "", "", d->d_name, inode)) { + closedir(dir); + return -1; + } } } @@ -378,102 +483,102 @@ int fix_names() { int i; - if (!stralloc_copy(&check_dir,&queue_dir)) die_nomem(); - if (!stralloc_cats(&check_dir,"mess")) die_nomem(); + if (!stralloc_copy(&check_dir, &queue_dir)) die_nomem(); + if (!stralloc_cats(&check_dir, "mess")) die_nomem(); if (!stralloc_0(&check_dir)) die_nomem(); for (i = 0; i < split_num; i++) { - strnum[fmt_ulong(strnum,i)] = 0; + strnum[fmt_ulong(strnum, i)] = 0; if (fix_part(strnum)) return -1; } - return 0; + return 0; } int check_dirs() { /*check root existence*/ - if (!stralloc_copy(&check_dir,&queue_dir)) die_nomem(); + if (!stralloc_copy(&check_dir, &queue_dir)) die_nomem(); if (!stralloc_0(&check_dir)) die_nomem(); - if (check_item(check_dir.s,qmailq_uid,qmail_gid,0750,'d',0)) return -1; + if (check_item(check_dir.s, qmailq_uid, qmail_gid, 0750, 'd', 0)) return -1; /*check the bigtodo queue */ - if (!stralloc_copy(&check_dir,&queue_dir)) die_nomem(); - if (!stralloc_cats(&check_dir,"info")) die_nomem(); + if (!stralloc_copy(&check_dir, &queue_dir)) die_nomem(); + if (!stralloc_cats(&check_dir, "info")) die_nomem(); if (!stralloc_0(&check_dir)) die_nomem(); - if (check_item(check_dir.s,qmails_uid,qmail_gid,0700,'d',0)) return -1; - if (check_splits(check_dir.s,qmails_uid,qmail_gid,0700,qmail_gid,0600)) return -1; + if (check_item(check_dir.s, qmails_uid, qmail_gid, 0700, 'd', 0)) return -1; + if (check_splits(check_dir.s, qmails_uid, qmail_gid, 0700, qmail_gid, 0600)) return -1; - if (!stralloc_copy(&check_dir,&queue_dir)) die_nomem(); - if (!stralloc_cats(&check_dir,"mess")) die_nomem(); + if (!stralloc_copy(&check_dir, &queue_dir)) die_nomem(); + if (!stralloc_cats(&check_dir, "mess")) die_nomem(); if (!stralloc_0(&check_dir)) die_nomem(); - if (check_item(check_dir.s,qmailq_uid,qmail_gid,0750,'d',0)) return -1; - if (check_splits(check_dir.s,qmailq_uid,qmail_gid,0750,-1,0644)) return -1; + if (check_item(check_dir.s, qmailq_uid, qmail_gid, 0750, 'd', 0)) return -1; + if (check_splits(check_dir.s, qmailq_uid, qmail_gid, 0750, -1, 0644)) return -1; - if (!stralloc_copy(&check_dir,&queue_dir)) die_nomem(); - if (!stralloc_cats(&check_dir,"remote")) die_nomem(); + if (!stralloc_copy(&check_dir, &queue_dir)) die_nomem(); + if (!stralloc_cats(&check_dir, "remote")) die_nomem(); if (!stralloc_0(&check_dir)) die_nomem(); - if (check_item(check_dir.s,qmails_uid,qmail_gid,0700,'d',0)) return -1; - if (check_splits(check_dir.s,qmails_uid,qmail_gid,0700,qmail_gid,0600)) return -1; + if (check_item(check_dir.s, qmails_uid, qmail_gid, 0700, 'd', 0)) return -1; + if (check_splits(check_dir.s, qmails_uid, qmail_gid, 0700, qmail_gid, 0600)) return -1; - if (!stralloc_copy(&check_dir,&queue_dir)) die_nomem(); - if (!stralloc_cats(&check_dir,"local")) die_nomem(); + if (!stralloc_copy(&check_dir, &queue_dir)) die_nomem(); + if (!stralloc_cats(&check_dir, "local")) die_nomem(); if (!stralloc_0(&check_dir)) die_nomem(); - if (check_item(check_dir.s,qmails_uid,qmail_gid,0700,'d',0)) return -1; - if (check_splits(check_dir.s,qmails_uid,qmail_gid,0700,qmail_gid,0600)) return -1; + if (check_item(check_dir.s, qmails_uid, qmail_gid, 0700, 'd', 0)) return -1; + if (check_splits(check_dir.s, qmails_uid, qmail_gid, 0700, qmail_gid, 0600)) return -1; - if (!stralloc_copy(&check_dir,&queue_dir)) die_nomem(); - if (!stralloc_cats(&check_dir,"intd")) die_nomem(); + if (!stralloc_copy(&check_dir, &queue_dir)) die_nomem(); + if (!stralloc_cats(&check_dir, "intd")) die_nomem(); if (!stralloc_0(&check_dir)) die_nomem(); - if (check_item(check_dir.s,qmailq_uid,qmail_gid,0700,'d',0)) return -1; - if (check_splits(check_dir.s,qmailq_uid,qmail_gid,0700,qmail_gid,0600)) return -1; + if (check_item(check_dir.s, qmailq_uid, qmail_gid, 0700, 'd', 0)) return -1; + if (check_splits(check_dir.s, qmailq_uid, qmail_gid, 0700, qmail_gid, 0600)) return -1; - if (!stralloc_copy(&check_dir,&queue_dir)) die_nomem(); - if (!stralloc_cats(&check_dir,"todo")) die_nomem(); + if (!stralloc_copy(&check_dir, &queue_dir)) die_nomem(); + if (!stralloc_cats(&check_dir, "todo")) die_nomem(); if (!stralloc_0(&check_dir)) die_nomem(); - if (check_item(check_dir.s,qmailq_uid,qmail_gid,0750,'d',0)) return -1; - if (check_splits(check_dir.s,qmailq_uid,qmail_gid,0750,-1,0644)) return -1; + if (check_item(check_dir.s, qmailq_uid, qmail_gid, 0750, 'd', 0)) return -1; + if (check_splits(check_dir.s, qmailq_uid, qmail_gid, 0750, -1, 0644)) return -1; - if (!stralloc_copy(&check_dir,&queue_dir)) die_nomem(); - if (!stralloc_cats(&check_dir,"dkim")) die_nomem(); + if (!stralloc_copy(&check_dir, &queue_dir)) die_nomem(); + if (!stralloc_cats(&check_dir, "dkim")) die_nomem(); if (!stralloc_0(&check_dir)) die_nomem(); - if (check_item(check_dir.s,qmailq_uid,qmail_gid,0750,'d',0)) return -1; - if (check_splits(check_dir.s,qmailq_uid,qmail_gid,0750,qmail_gid,0644)) return -1; + if (check_item(check_dir.s, qmailq_uid, qmail_gid, 0750, 'd', 0)) return -1; + if (check_splits(check_dir.s, qmailq_uid, qmail_gid, 0750, qmail_gid, 0644)) return -1; /*check the others*/ - if (!stralloc_copy(&check_dir,&queue_dir)) die_nomem(); - if (!stralloc_cats(&check_dir,"bounce")) die_nomem(); + if (!stralloc_copy(&check_dir, &queue_dir)) die_nomem(); + if (!stralloc_cats(&check_dir, "bounce")) die_nomem(); if (!stralloc_0(&check_dir)) die_nomem(); - if (check_item(check_dir.s,qmails_uid,qmail_gid,0700,'d',0)) return -1; - if (check_files(check_dir.s,qmails_uid,qmail_gid,0600)) return -1; + if (check_item(check_dir.s, qmails_uid, qmail_gid, 0700, 'd', 0)) return -1; + if (check_files(check_dir.s, qmails_uid, qmail_gid, 0600)) return -1; - if (!stralloc_copy(&check_dir,&queue_dir)) die_nomem(); - if (!stralloc_cats(&check_dir,"pid")) die_nomem(); + if (!stralloc_copy(&check_dir, &queue_dir)) die_nomem(); + if (!stralloc_cats(&check_dir, "pid")) die_nomem(); if (!stralloc_0(&check_dir)) die_nomem(); - if (check_item(check_dir.s,qmailq_uid,qmail_gid,0700,'d',0)) return -1; + if (check_item(check_dir.s, qmailq_uid, qmail_gid, 0700, 'd', 0)) return -1; warn_files(check_dir.s); /*lock has special files that must exist*/ - if (!stralloc_copy(&check_dir,&queue_dir)) die_nomem(); - if (!stralloc_cats(&check_dir,"lock")) die_nomem(); + if (!stralloc_copy(&check_dir, &queue_dir)) die_nomem(); + if (!stralloc_cats(&check_dir, "lock")) die_nomem(); if (!stralloc_0(&check_dir)) die_nomem(); - if (check_item(check_dir.s,qmailq_uid,qmail_gid,0750,'d',0)) return -1; + if (check_item(check_dir.s, qmailq_uid, qmail_gid, 0750, 'd', 0)) return -1; - if (!stralloc_copy(&check_dir,&queue_dir)) die_nomem(); - if (!stralloc_cats(&check_dir,"lock/sendmutex")) die_nomem(); + if (!stralloc_copy(&check_dir, &queue_dir)) die_nomem(); + if (!stralloc_cats(&check_dir, "lock/sendmutex")) die_nomem(); if (!stralloc_0(&check_dir)) die_nomem(); - if (check_item(check_dir.s,qmails_uid,qmail_gid,0600,'z',0)) return -1; + if (check_item(check_dir.s, qmails_uid, qmail_gid, 0600, 'z', 0)) return -1; - if (!stralloc_copy(&check_dir,&queue_dir)) die_nomem(); - if (!stralloc_cats(&check_dir,"lock/tcpto")) die_nomem(); + if (!stralloc_copy(&check_dir, &queue_dir)) die_nomem(); + if (!stralloc_cats(&check_dir, "lock/tcpto")) die_nomem(); if (!stralloc_0(&check_dir)) die_nomem(); - if (check_item(check_dir.s,qmailr_uid,qmail_gid,0644,'z',1024)) return -1; + if (check_item(check_dir.s, qmailr_uid, qmail_gid, 0644, 'z', 1024)) return -1; - if (!stralloc_copy(&check_dir,&queue_dir)) die_nomem(); - if (!stralloc_cats(&check_dir,"lock/trigger")) die_nomem(); + if (!stralloc_copy(&check_dir, &queue_dir)) die_nomem(); + if (!stralloc_cats(&check_dir, "lock/trigger")) die_nomem(); if (!stralloc_0(&check_dir)) die_nomem(); - if (check_item(check_dir.s,qmails_uid,qmail_gid,0622,'p',0)) return -1; + if (check_item(check_dir.s, qmails_uid, qmail_gid, 0622, 'p', 0)) return -1; return 0; } @@ -482,31 +587,52 @@ int check_dirs() stralloc fn = {0}; -void fnmake_init() { while (!stralloc_ready(&fn,FMTQFN)) die_nomem(); } -void fnmake_local(unsigned long id) { fn.len = fmtqfn(fn.s,"local/",id,1); } -void fnmake_remote(unsigned long id) { fn.len = fmtqfn(fn.s,"remote/",id,1); } -void fnmake_mess(unsigned long id) { fn.len = fmtqfn(fn.s,"mess/",id,1); } -void fnmake_dkim(unsigned long id) { fn.len = fmtqfn(fn.s,"dkim/",id,1); } -void fnmake_info(unsigned long id) { fn.len = fmtqfn(fn.s,"info/",id,1); } -void fnmake_bounce(unsigned long id) { fn.len = fmtqfn(fn.s,"bounce/",id,0); } +void fnmake_init() +{ + while (!stralloc_ready(&fn, FMTQFN)) die_nomem(); +} +void fnmake_local(unsigned long id) +{ + fn.len = fmtqfn(fn.s, "local/", id, 1); +} +void fnmake_remote(unsigned long id) +{ + fn.len = fmtqfn(fn.s, "remote/", id, 1); +} +void fnmake_mess(unsigned long id) +{ + fn.len = fmtqfn(fn.s, "mess/", id, 1); +} +void fnmake_dkim(unsigned long id) +{ + fn.len = fmtqfn(fn.s, "dkim/", id, 1); +} +void fnmake_info(unsigned long id) +{ + fn.len = fmtqfn(fn.s, "info/", id, 1); +} +void fnmake_bounce(unsigned long id) +{ + fn.len = fmtqfn(fn.s, "bounce/", id, 0); +} void warn_unlink(unsigned long id) { - char foo[FMT_ULONG]; - foo[fmt_ulong(foo,id)] = 0; - logmsg(WHO,99,WARN,B("no such file to unlink #",foo)); + char foo[FMT_ULONG]; + foo[fmt_ulong(foo, id)] = 0; + logmsg(WHO, 99, WARN, B("no such file to unlink #", foo)); } void err_unlink(unsigned long id) { - char foo[FMT_ULONG]; - foo[fmt_ulong(foo,id)] = 0; - logmsg(WHO,100,ERROR,B("trouble with unlinking #",foo)); + char foo[FMT_ULONG]; + foo[fmt_ulong(foo, id)] = 0; + logmsg(WHO, 100, ERROR, B("trouble with unlinking #", foo)); } void err_chdir() { - logmsg(WHO,110,FATAL,"unable to chdir"); + logmsg(WHO, 110, FATAL, "unable to chdir"); } int delete_msg(unsigned long id) @@ -514,38 +640,43 @@ int delete_msg(unsigned long id) struct stat st; int bounce = 1; - if (chdir(auto_qmail) == -1) err_chdir(); - if (chdir("queue") == -1) err_chdir(); + if (chdir(auto_qmail) == -1) err_chdir(); + if (chdir("queue") == -1) err_chdir(); fnmake_init(); fnmake_mess(id); // regular message pre-processed - if (stat(fn.s,&st) == -1) err_unlink(id); - else bounce = 0; - if (!bounce && unlink(fn.s) == -1) + if (stat(fn.s, &st) == -1) + err_unlink(id); + else + bounce = 0; + if (!bounce && unlink(fn.s) == -1) if (errno != ENOENT) err_unlink(id); fnmake_info(id); // not delivered yet - if (!stat(fn.s,&st)) - if (unlink(fn.s) == -1) + if (!stat(fn.s, &st)) + if (unlink(fn.s) == -1) if (errno != ENOENT) err_unlink(id); - + if (bounce) { fnmake_bounce(id); - if (!stat(fn.s,&st)) { warn_unlink(id); return 1; } - if (unlink(fn.s) == -1) - if (errno != ENOENT) err_unlink(id); + if (!stat(fn.s, &st)) { + warn_unlink(id); + return 1; + } + if (unlink(fn.s) == -1) + if (errno != ENOENT) err_unlink(id); } fnmake_remote(id); - if (!stat(fn.s,&st)) - if (unlink(fn.s) == -1) - if (errno != ENOENT) err_unlink(id); - + if (!stat(fn.s, &st)) + if (unlink(fn.s) == -1) + if (errno != ENOENT) err_unlink(id); + fnmake_local(id); - if (!stat(fn.s,&st)) - if (unlink(fn.s) == -1) - if (errno != ENOENT) err_unlink(id); - + if (!stat(fn.s, &st)) + if (unlink(fn.s) == -1) + if (errno != ENOENT) err_unlink(id); + return 0; } @@ -555,20 +686,20 @@ int main(int argc, char **argv) unsigned long id = 0; if (argc > 1) { - if (!str_diff(argv[1],"-i")) { + if (!str_diff(argv[1], "-i")) { flag_interactive = 1; - } else if (!str_diff(argv[1],"-d")) { - if (!argv[2]) logmsg(WHO,111,USAGE,"qmail-qmaint [-i] || [-d messid]"); + } else if (!str_diff(argv[1], "-d")) { + if (!argv[2]) logmsg(WHO, 111, USAGE, "qmail-qmaint [-i] || [-d messid]"); mess = argv[2]; flag_delete = 1; - scan_ulong(mess,&id); + scan_ulong(mess, &id); } } - if (!stralloc_copys(&queue_dir,auto_qmail)) die_nomem(); - if (!stralloc_cats(&queue_dir,"/queue/")) die_nomem(); + if (!stralloc_copys(&queue_dir, auto_qmail)) die_nomem(); + if (!stralloc_cats(&queue_dir, "/queue/")) die_nomem(); - logmsg(WHO,0,INFO,B("Checking s/qmail queue at: ",auto_qmail,"/queue/")); + logmsg(WHO, 0, INFO, B("Checking s/qmail queue at: ", auto_qmail, "/queue/")); /* get constants */ @@ -583,12 +714,11 @@ int main(int argc, char **argv) if (check_dirs()) die_check(); if (flag_delete) { - if (!delete_msg(id)) - logmsg(WHO,0,INFO,B("file ",mess," from queue deleted.")); - } else - if (fix_names()) die_check(); - - logmsg(WHO,0,INFO,"done."); - - _exit (0); + if (!delete_msg(id)) logmsg(WHO, 0, INFO, B("file ", mess, " from queue deleted.")); + } else if (fix_names()) + die_check(); + + logmsg(WHO, 0, INFO, "done."); + + _exit(0); } diff --git a/src/qmail-qmqpc.c b/src/qmail-qmqpc.c index c92e072..2bbfdfe 100644 --- a/src/qmail-qmqpc.c +++ b/src/qmail-qmqpc.c @@ -1,61 +1,94 @@ -#include <unistd.h> -#include <sys/types.h> -#include <sys/socket.h> -#include <netinet/in.h> #include <arpa/inet.h> +#include <netinet/in.h> +#include <sys/socket.h> +#include <sys/types.h> +#include <unistd.h> + #include "buffer.h" -#include "getln.h" #include "exit.h" -#include "stralloc.h" -#include "readclose.h" -#include "timeoutconn.h" +#include "fmt.h" +#include "getln.h" +#include "ip.h" #include "logmsg.h" -#include "str.h" +#include "readclose.h" #include "sig.h" -#include "ip.h" +#include "socket_if.h" +#include "str.h" +#include "stralloc.h" #include "timeout.h" +#include "timeoutconn.h" +#include "uint_t.h" + #include "auto_qmail.h" #include "control.h" -#include "fmt.h" -#include "uint_t.h" -#include "socket_if.h" #define PORT_QMQP 628 -void die_success() { _exit(0); } -void die_perm() { _exit(31); } -void nomem() { _exit(51); } -void die_read() { if (errno == ENOMEM) nomem(); _exit(54); } -void die_control() { _exit(55); } -void die_socket() { _exit(56); } -void die_home() { _exit(61); } -void die_temp() { _exit(71); } -void die_conn() { _exit(74); } -void die_format() { _exit(91); } +void die_success() +{ + _exit(0); +} +void die_perm() +{ + _exit(31); +} +void nomem() +{ + _exit(51); +} +void die_read() +{ + if (errno == ENOMEM) nomem(); + _exit(54); +} +void die_control() +{ + _exit(55); +} +void die_socket() +{ + _exit(56); +} +void die_home() +{ + _exit(61); +} +void die_temp() +{ + _exit(71); +} +void die_conn() +{ + _exit(74); +} +void die_format() +{ + _exit(91); +} int lasterror = 55; int qmqpfd; -ssize_t saferead(int fd,char *buf,int len) +ssize_t saferead(int fd, char *buf, int len) { int r; - r = timeoutread(60,qmqpfd,buf,len); + r = timeoutread(60, qmqpfd, buf, len); if (r <= 0) die_conn(); return r; } -ssize_t safewrite(int fd,char *buf,int len) +ssize_t safewrite(int fd, char *buf, int len) { int r; - r = timeoutwrite(60,qmqpfd,buf,len); + r = timeoutwrite(60, qmqpfd, buf, len); if (r <= 0) die_conn(); return r; } char buf[1024]; -buffer bo = BUFFER_INIT(safewrite,-1,buf,sizeof(buf)); -buffer bi = BUFFER_INIT(saferead,-1,buf,sizeof(buf)); -buffer be = BUFFER_INIT(read,1,buf,sizeof(buf)); // envelope +buffer bo = BUFFER_INIT(safewrite, -1, buf, sizeof(buf)); +buffer bi = BUFFER_INIT(saferead, -1, buf, sizeof(buf)); +buffer be = BUFFER_INIT(read, 1, buf, sizeof(buf)); // envelope /* WARNING: can use only one of these at a time! */ stralloc beforemessage = {0}; @@ -69,35 +102,35 @@ void getmess() { int match; - if (readclose_append(0,&message,1024) == -1) die_read(); + if (readclose_append(0, &message, 1024) == -1) die_read(); - strnum[fmt_ulong(strnum,(unsigned long) message.len)] = 0; - if (!stralloc_copys(&beforemessage,strnum)) nomem(); - if (!stralloc_cats(&beforemessage,":")) nomem(); - if (!stralloc_copys(&aftermessage,",")) nomem(); + strnum[fmt_ulong(strnum, (unsigned long)message.len)] = 0; + if (!stralloc_copys(&beforemessage, strnum)) nomem(); + if (!stralloc_cats(&beforemessage, ":")) nomem(); + if (!stralloc_copys(&aftermessage, ",")) nomem(); - if (getln(&be,&line,&match,'\0') == -1) die_read(); + if (getln(&be, &line, &match, '\0') == -1) die_read(); if (!match) die_format(); if (line.len < 2) die_format(); if (line.s[0] != 'F') die_format(); - strnum[fmt_ulong(strnum,(unsigned long) line.len - 2)] = 0; - if (!stralloc_cats(&aftermessage,strnum)) nomem(); - if (!stralloc_cats(&aftermessage,":")) nomem(); - if (!stralloc_catb(&aftermessage,line.s + 1,line.len - 2)) nomem(); - if (!stralloc_cats(&aftermessage,",")) nomem(); + strnum[fmt_ulong(strnum, (unsigned long)line.len - 2)] = 0; + if (!stralloc_cats(&aftermessage, strnum)) nomem(); + if (!stralloc_cats(&aftermessage, ":")) nomem(); + if (!stralloc_catb(&aftermessage, line.s + 1, line.len - 2)) nomem(); + if (!stralloc_cats(&aftermessage, ",")) nomem(); for (;;) { - if (getln(&be,&line,&match,'\0') == -1) die_read(); + if (getln(&be, &line, &match, '\0') == -1) die_read(); if (!match) die_format(); if (line.len < 2) break; if (line.s[0] != 'T') die_format(); - strnum[fmt_ulong(strnum,(unsigned long) line.len - 2)] = 0; - if (!stralloc_cats(&aftermessage,strnum)) nomem(); - if (!stralloc_cats(&aftermessage,":")) nomem(); - if (!stralloc_catb(&aftermessage,line.s + 1,line.len - 2)) nomem(); - if (!stralloc_cats(&aftermessage,",")) nomem(); + strnum[fmt_ulong(strnum, (unsigned long)line.len - 2)] = 0; + if (!stralloc_cats(&aftermessage, strnum)) nomem(); + if (!stralloc_cats(&aftermessage, ":")) nomem(); + if (!stralloc_catb(&aftermessage, line.s + 1, line.len - 2)) nomem(); + if (!stralloc_cats(&aftermessage, ",")) nomem(); } } @@ -110,23 +143,23 @@ void doit(char *server) char ch; int i, j, r; - i = str_chr(server,':'); + i = str_chr(server, ':'); if (server[i] == ':') { - j = str_chr(server,'%'); /* IF index */ + j = str_chr(server, '%'); /* IF index */ if (server[j] == '%') { server[j] = 0; netif = &server[j + 1]; ifidx = socket_getifidx(netif); - } - if (!ip6_scan(server,(char *)&ip6s.d)) return; - qmqpfd = socket(AF_INET6,SOCK_STREAM,0); + } + if (!ip6_scan(server, (char *)&ip6s.d)) return; + qmqpfd = socket(AF_INET6, SOCK_STREAM, 0); if (qmqpfd == -1) die_socket(); - r = timeoutconn6(qmqpfd,(char *)&ip6s.d,PORT_QMQP,10,ifidx); + r = timeoutconn6(qmqpfd, (char *)&ip6s.d, PORT_QMQP, 10, ifidx); } else { - if (!ip4_scan(server,(char *)&ip4s.d)) return; - qmqpfd = socket(AF_INET,SOCK_STREAM,0); + if (!ip4_scan(server, (char *)&ip4s.d)) return; + qmqpfd = socket(AF_INET, SOCK_STREAM, 0); if (qmqpfd == -1) die_socket(); - r = timeoutconn4(qmqpfd,(char *)&ip4s.d,PORT_QMQP,10); + r = timeoutconn4(qmqpfd, (char *)&ip4s.d, PORT_QMQP, 10); } if (r != 0) { lasterror = 73; @@ -135,17 +168,17 @@ void doit(char *server) return; } - strnum[fmt_ulong(strnum,(unsigned long) (beforemessage.len + message.len + aftermessage.len))] = 0; - buffer_puts(&bo,strnum); - buffer_puts(&bo,":"); - buffer_put(&bo,beforemessage.s,beforemessage.len); - buffer_put(&bo,message.s,message.len); - buffer_put(&bo,aftermessage.s,aftermessage.len); - buffer_puts(&bo,","); + strnum[fmt_ulong(strnum, (unsigned long)(beforemessage.len + message.len + aftermessage.len))] = 0; + buffer_puts(&bo, strnum); + buffer_puts(&bo, ":"); + buffer_put(&bo, beforemessage.s, beforemessage.len); + buffer_put(&bo, message.s, message.len); + buffer_put(&bo, aftermessage.s, aftermessage.len); + buffer_puts(&bo, ","); buffer_flush(&bo); for (;;) { - buffer_get(&bi,&ch,1); + buffer_get(&bi, &ch, 1); if (ch == 'K') die_success(); if (ch == 'Z') die_temp(); if (ch == 'D') die_perm(); @@ -163,7 +196,7 @@ int main() if (chdir(auto_qmail) == -1) die_home(); if (control_init() == -1) die_control(); - if (control_readfile(&servers,"control/qmqpservers",0) != 1) die_control(); + if (control_readfile(&servers, "control/qmqpservers", 0) != 1) die_control(); getmess(); diff --git a/src/qmail-qmqpd.c b/src/qmail-qmqpd.c index 86404a9..b949338 100644 --- a/src/qmail-qmqpd.c +++ b/src/qmail-qmqpd.c @@ -1,49 +1,54 @@ #include <unistd.h> -#include "auto_qmail.h" -#include "qmail.h" -#include "received.h" -#include "sig.h" + #include "buffer.h" +#include "byte.h" +#include "case.h" +#include "env.h" #include "exit.h" -#include "now.h" #include "fmt.h" -#include "env.h" -#include "case.h" -#include "byte.h" #include "ip.h" +#include "sig.h" #include "str.h" +#include "auto_qmail.h" +#include "now.h" +#include "qmail.h" +#include "received.h" + #define PORT_QMQP "628" -void resources() { _exit(111); } +void resources() +{ + _exit(111); +} -ssize_t safewrite(int fd,char *buf,int len) +ssize_t safewrite(int fd, char *buf, int len) { int r; - r = write(fd,buf,len); + r = write(fd, buf, len); if (r <= 0) _exit(0); return r; } -ssize_t saferead(int fd,char *buf,int len) +ssize_t saferead(int fd, char *buf, int len) { int r; - r = read(fd,buf,len); + r = read(fd, buf, len); if (r <= 0) _exit(0); return r; } char inbuf[512]; -buffer bi = BUFFER_INIT(saferead,0,inbuf,sizeof(inbuf)); +buffer bi = BUFFER_INIT(saferead, 0, inbuf, sizeof(inbuf)); char outbuf[256]; -buffer bo = BUFFER_INIT(safewrite,1,outbuf,sizeof(outbuf)); +buffer bo = BUFFER_INIT(safewrite, 1, outbuf, sizeof(outbuf)); unsigned long bytesleft = 100; void getbyte(char *ch) { if (!bytesleft--) _exit(100); - buffer_get(&bi,ch,1); + buffer_get(&bi, ch, 1); } unsigned long getlen() @@ -82,7 +87,7 @@ void identify() remoteinfo = env_get("TCP6REMOTEINFO"); if (!remoteinfo) remoteinfo = env_get("TCPREMOTEINFO"); remoteip = env_get("TCP6REMOTEIP"); - if (remoteip && byte_equal(remoteip,7,V4MAPPREFIX)) remoteip=remoteip+7; + if (remoteip && byte_equal(remoteip, 7, V4MAPPREFIX)) remoteip = remoteip + 7; if (!remoteip) remoteip = env_get("TCPREMOTEIP"); if (!remoteip) remoteip = "unknown"; local = env_get("TCP6LOCALHOST"); @@ -91,10 +96,10 @@ void identify() if (!local) local = env_get("TCPLOCALIP"); if (!local) local = "unknown"; localport = env_get("TCP6LOCALPORT"); - if (!localport) localport = env_get("TCPLOCALPORT"); + if (!localport) localport = env_get("TCPLOCALPORT"); if (!localport) localport = "0"; - received(&qq,"QMQP",local,remoteip,remotehost,remoteinfo,(char *) 0,(char *) 0,(char *) 0); + received(&qq, "QMQP", local, remoteip, remotehost, remoteinfo, (char *)0, (char *)0, (char *)0); } char buf[1000]; @@ -116,7 +121,7 @@ int getbuf() for (i = 0; i < len; ++i) getbyte(buf + i); getcomma(); buf[len] = 0; - return byte_chr(buf,len,'\0') == len; + return byte_chr(buf, len, '\0') == len; } int flagok = 1; @@ -144,21 +149,21 @@ int main() while (len > 0) { /* XXX: could speed this up */ getbyte(&ch); --len; - qmail_put(&qq,&ch,1); + qmail_put(&qq, &ch, 1); } getcomma(); if (getbuf()) - qmail_from(&qq,buf); + qmail_from(&qq, buf); else { - qmail_from(&qq,""); + qmail_from(&qq, ""); qmail_fail(&qq); flagok = 0; } while (bytesleft) if (getbuf()) - qmail_to(&qq,buf); + qmail_to(&qq, buf); else { qmail_fail(&qq); flagok = 0; @@ -170,21 +175,20 @@ int main() result = qmail_close(&qq); if (!*result) { - len = fmt_str(buf,"Kok "); - len += fmt_ulong(buf + len,(unsigned long) now()); - len += fmt_str(buf + len," qp "); - len += fmt_ulong(buf + len,qp); + len = fmt_str(buf, "Kok "); + len += fmt_ulong(buf + len, (unsigned long)now()); + len += fmt_str(buf + len, " qp "); + len += fmt_ulong(buf + len, qp); buf[len] = 0; result = buf; } - if (!flagok) - result = "Dsorry, I can't accept addresses like that (#5.1.3)"; + if (!flagok) result = "Dsorry, I can't accept addresses like that (#5.1.3)"; - buffer_put(&bo,strnum,fmt_ulong(strnum,(unsigned long) str_len(result))); - buffer_puts(&bo,":"); - buffer_puts(&bo,result); - buffer_puts(&bo,","); + buffer_put(&bo, strnum, fmt_ulong(strnum, (unsigned long)str_len(result))); + buffer_puts(&bo, ":"); + buffer_puts(&bo, result); + buffer_puts(&bo, ","); buffer_flush(&bo); _exit(0); } diff --git a/src/qmail-qmtpd.c b/src/qmail-qmtpd.c index fbe998b..8ae81e0 100644 --- a/src/qmail-qmtpd.c +++ b/src/qmail-qmtpd.c @@ -1,60 +1,68 @@ #include <unistd.h> -#include "stralloc.h" + #include "buffer.h" -#include "qmail.h" -#include "now.h" -#include "str.h" -#include "fmt.h" -#include "env.h" -#include "sig.h" +#include "byte.h" #include "case.h" +#include "env.h" #include "exit.h" +#include "fmt.h" +#include "ip.h" #include "scan.h" -#include "rcpthosts.h" +#include "sig.h" +#include "str.h" +#include "stralloc.h" + #include "auto_qmail.h" #include "control.h" +#include "now.h" +#include "qmail.h" +#include "rcpthosts.h" #include "received.h" -#include "ip.h" -#include "byte.h" -#define PORT_QMTP "209" +#define PORT_QMTP "209" #define PORT_QMTPS "6209" /** @file qmail-qmtpd.c -- QMTP/QMTPS server @brief requires sslserver */ -void badproto() { _exit(100); } -void resources() { _exit(111); } +void badproto() +{ + _exit(100); +} +void resources() +{ + _exit(111); +} -ssize_t safewrite(int fd,char *buf,int len) +ssize_t safewrite(int fd, char *buf, int len) { int r; - r = write(fd,buf,len); + r = write(fd, buf, len); if (r <= 0) _exit(0); return r; } char outbuf[256]; -buffer bo = BUFFER_INIT(safewrite,1,outbuf,sizeof(outbuf)); +buffer bo = BUFFER_INIT(safewrite, 1, outbuf, sizeof(outbuf)); -ssize_t saferead(int fd,char *buf,int len) +ssize_t saferead(int fd, char *buf, int len) { int r; buffer_flush(&bo); - r = read(fd,buf,len); + r = read(fd, buf, len); if (r <= 0) _exit(0); return r; } char inbuf[512]; -buffer bi = BUFFER_INIT(saferead,0,inbuf,sizeof(inbuf)); +buffer bi = BUFFER_INIT(saferead, 0, inbuf, sizeof(inbuf)); unsigned long getlen() { unsigned long len = 0; char ch; for (;;) { - buffer_get(&bi,&ch,1); + buffer_get(&bi, &ch, 1); if (ch == ':') return len; if (ch < '0' || ch > '9') resources(); if (len > 200000000) resources(); @@ -65,7 +73,7 @@ unsigned long getlen() void getcomma() { char ch; - buffer_get(&bi,&ch,1); + buffer_get(&bi, &ch, 1); if (ch != ',') badproto(); } @@ -113,30 +121,31 @@ int modssl_info() cipherused = env_get("SSL_CIPHER_USEKEYSIZE"); if (!cipherused) cipherused = "unknown"; clientdn = env_get("SSL_CLIENT_S_DN"); - if (clientdn) seentls = 2; + if (clientdn) + seentls = 2; else clientdn = "none"; - if (!stralloc_copys(&tlsinfo,tlsversion)) resources(); - if (!stralloc_cats(&tlsinfo,": ")) resources(); - if (!stralloc_cats(&tlsinfo,cipher)) resources(); - if (!stralloc_cats(&tlsinfo," [")) resources(); - if (!stralloc_cats(&tlsinfo,cipherused)) resources(); - if (!stralloc_cats(&tlsinfo,"/")) resources(); - if (!stralloc_cats(&tlsinfo,cipherperm)) resources(); - if (!stralloc_cats(&tlsinfo,"] \n")) resources(); - if (!stralloc_cats(&tlsinfo," DN=")) resources(); - if (!stralloc_cats(&tlsinfo,clientdn)) resources(); + if (!stralloc_copys(&tlsinfo, tlsversion)) resources(); + if (!stralloc_cats(&tlsinfo, ": ")) resources(); + if (!stralloc_cats(&tlsinfo, cipher)) resources(); + if (!stralloc_cats(&tlsinfo, " [")) resources(); + if (!stralloc_cats(&tlsinfo, cipherused)) resources(); + if (!stralloc_cats(&tlsinfo, "/")) resources(); + if (!stralloc_cats(&tlsinfo, cipherperm)) resources(); + if (!stralloc_cats(&tlsinfo, "] \n")) resources(); + if (!stralloc_cats(&tlsinfo, " DN=")) resources(); + if (!stralloc_cats(&tlsinfo, clientdn)) resources(); if (!stralloc_0(&tlsinfo)) resources(); - if (!stralloc_append(&protocol,"S")) resources(); + if (!stralloc_append(&protocol, "S")) resources(); if (seentls == 2) { clientcn = env_get("SSL_CLIENT_S_DN_CN"); - remoteinfo = clientcn ? clientcn : clientdn; + remoteinfo = clientcn ? clientcn : clientdn; dnemail = env_get("SSL_CLIENT_S_DN_Email"); if (!dnemail) dnemail = "unknown"; - if (!stralloc_append(&protocol,"A")) resources(); + if (!stralloc_append(&protocol, "A")) resources(); relayclient = ""; } return 1; @@ -155,21 +164,24 @@ int main() char *result; char *x; unsigned long u; - + sig_pipeignore(); sig_alarmcatch(resources); alarm(3600); - + if (chdir(auto_qmail) == -1) resources(); - + if (control_init() == -1) resources(); if (rcpthosts_init() == -1) resources(); - if (control_readint(&databytes,"control/databytes") == -1) resources(); + if (control_readint(&databytes, "control/databytes") == -1) resources(); x = env_get("DATABYTES"); - if (x) { scan_ulong(x,&u); databytes = u; } + if (x) { + scan_ulong(x, &u); + databytes = u; + } if (!(databytes + 1)) --databytes; - + relayclient = env_get("RELAYCLIENT"); remotehost = env_get("TCP6REMOTEHOST"); if (!remotehost) remotehost = env_get("TCPREMOTEHOST"); @@ -178,7 +190,7 @@ int main() if (!remoteinfo) remoteinfo = env_get("TCPREMOTEINFO"); remoteip = env_get("TCP6REMOTEIP"); if (!remoteip) remoteip = env_get("TCPREMOTEIP"); - if (remoteip && byte_equal(remoteip,7,V4MAPPREFIX)) remoteip = remoteip + 7; + if (remoteip && byte_equal(remoteip, 7, V4MAPPREFIX)) remoteip = remoteip + 7; if (!remoteip) remoteip = "unknown"; local = env_get("TCP6LOCALHOST"); if (!local) local = env_get("TCPLOCALHOST"); @@ -186,52 +198,56 @@ int main() if (!local) local = env_get("TCPLOCALIP"); if (!local) local = "unknown"; localport = env_get("TCP6LOCALPORT"); - if (!localport) localport = env_get("TCPLOCALPORT"); + if (!localport) localport = env_get("TCPLOCALPORT"); if (!localport) localport = "0"; - - if (!stralloc_copys(&protocol,"QMTP")) resources(); - if (!case_diffs(localport,PORT_QMTPS)) + + if (!stralloc_copys(&protocol, "QMTP")) resources(); + if (!case_diffs(localport, PORT_QMTPS)) if (!modssl_info()) resources(); - if (relayclient) - relayclientlen = str_len(relayclient); - + if (relayclient) relayclientlen = str_len(relayclient); + for (;;) { - if (!stralloc_copys(&failure,"")) resources(); + if (!stralloc_copys(&failure, "")) resources(); flagsenderok = 1; - + len = getlen(); if (len == 0) badproto(); - + if (databytes) bytestooverflow = databytes + 1; if (qmail_open(&qq) == -1) resources(); qp = qmail_qp(&qq); - - buffer_get(&bi,&ch,1); + + buffer_get(&bi, &ch, 1); --len; - if (ch == 10) flagdos = 0; - else if (ch == 13) flagdos = 1; - else badproto(); + if (ch == 10) + flagdos = 0; + else if (ch == 13) + flagdos = 1; + else + badproto(); /* no fakehelo, no spfinfo */ - - received(&qq,protocol.s,local,remoteip,remotehost,remoteinfo,(char *) 0,tlsinfo.s,(char *) 0); - + + received(&qq, protocol.s, local, remoteip, remotehost, remoteinfo, (char *)0, tlsinfo.s, (char *)0); + /* XXX: check for loops? only if len is big? */ - + if (flagdos) while (len > 0) { - buffer_get(&bi,&ch,1); + buffer_get(&bi, &ch, 1); --len; while ((ch == 13) && len) { - buffer_get(&bi,&ch,1); + buffer_get(&bi, &ch, 1); --len; if (ch == 10) break; - if (bytestooverflow) if (!--bytestooverflow) qmail_fail(&qq); - qmail_put(&qq,"\015",1); + if (bytestooverflow) + if (!--bytestooverflow) qmail_fail(&qq); + qmail_put(&qq, "\015", 1); } - if (bytestooverflow) if (!--bytestooverflow) qmail_fail(&qq); - qmail_put(&qq,&ch,1); + if (bytestooverflow) + if (!--bytestooverflow) qmail_fail(&qq); + qmail_put(&qq, &ch, 1); } else { if (databytes) @@ -240,42 +256,40 @@ int main() qmail_fail(&qq); } while (len > 0) { /* XXX: could speed this up, obviously */ - buffer_get(&bi,&ch,1); + buffer_get(&bi, &ch, 1); --len; - qmail_put(&qq,&ch,1); + qmail_put(&qq, &ch, 1); } } getcomma(); - + len = getlen(); - + if (len >= 1000) { buf[0] = 0; flagsenderok = 0; - for (i = 0; i < len; ++i) - buffer_get(&bi,&ch,1); - } - else { + for (i = 0; i < len; ++i) buffer_get(&bi, &ch, 1); + } else { for (i = 0; i < len; ++i) { - buffer_get(&bi,buf + i,1); + buffer_get(&bi, buf + i, 1); if (!buf[i]) flagsenderok = 0; } buf[len] = 0; } getcomma(); - + flagbother = 0; - qmail_from(&qq,buf); + qmail_from(&qq, buf); if (!flagsenderok) qmail_fail(&qq); - + biglen = getlen(); while (biglen > 0) { - if (!stralloc_append(&failure,"")) resources(); - + if (!stralloc_append(&failure, "")) resources(); + len = 0; for (;;) { if (!biglen) badproto(); - buffer_get(&bi,&ch,1); + buffer_get(&bi, &ch, 1); --biglen; if (ch == ':') break; if (ch < '0' || ch > '9') resources(); @@ -285,26 +299,23 @@ int main() if (len >= biglen) badproto(); if (len + relayclientlen >= 1000) { failure.s[failure.len - 1] = 'L'; - for (i = 0; i < len; ++i) - buffer_get(&bi,&ch,1); - } - else { + for (i = 0; i < len; ++i) buffer_get(&bi, &ch, 1); + } else { for (i = 0; i < len; ++i) { - buffer_get(&bi,buf + i,1); + buffer_get(&bi, buf + i, 1); if (!buf[i]) failure.s[failure.len - 1] = 'N'; } buf[len] = 0; - - if (relayclientlen) - str_copy(buf + len,relayclient); - if (!relayclient) - switch (rcpthosts(buf,len)) { + + if (relayclientlen) str_copy(buf + len, relayclient); + if (!relayclient) switch (rcpthosts(buf, len)) + { case -1: resources(); - case 0: failure.s[failure.len - 1] = 'D'; + case 0: failure.s[failure.len - 1] = 'D'; } - + if (!failure.s[failure.len - 1]) { - qmail_to(&qq,buf); + qmail_to(&qq, buf); flagbother = 1; } } @@ -312,43 +323,40 @@ int main() biglen -= (len + 1); } getcomma(); - + if (!flagbother) qmail_fail(&qq); result = qmail_close(&qq); if (!flagsenderok) result = "D Unacceptable sender (#5.1.7)"; - if (databytes) if (!bytestooverflow) result = "D Sorry, that message size exceeds my databytes limit (#5.3.4)"; - + if (databytes) + if (!bytestooverflow) result = "D Sorry, that message size exceeds my databytes limit (#5.3.4)"; + if (*result) len = str_len(result); else { /* success! */ len = 0; - len += fmt_str(buf2 + len,"K Ok "); - len += fmt_ulong(buf2 + len,(unsigned long) now()); - len += fmt_str(buf2 + len," qp "); - len += fmt_ulong(buf2 + len,qp); + len += fmt_str(buf2 + len, "K Ok "); + len += fmt_ulong(buf2 + len, (unsigned long)now()); + len += fmt_str(buf2 + len, " qp "); + len += fmt_ulong(buf2 + len, qp); buf2[len] = 0; result = buf2; } - - len = fmt_ulong(buf,len); + + len = fmt_ulong(buf, len); buf[len++] = ':'; - len += fmt_str(buf + len,result); + len += fmt_str(buf + len, result); buf[len++] = ','; - - for (i = 0; i < failure.len; ++i) - switch (failure.s[i]) { - case 0: - buffer_put(&bo,buf,len); - break; + + for (i = 0; i < failure.len; ++i) switch (failure.s[i]) + { + case 0: buffer_put(&bo, buf, len); break; case 'D': - buffer_puts(&bo,"66:D Sorry, that domain isn't in my list of allowed rcpthosts (#5.7.1),"); - break; - default: - buffer_puts(&bo,"46:D Sorry, I can't handle that recipient (#5.1.3),"); + buffer_puts(&bo, "66:D Sorry, that domain isn't in my list of allowed rcpthosts (#5.7.1),"); break; + default: buffer_puts(&bo, "46:D Sorry, I can't handle that recipient (#5.1.3),"); break; } - + /* bo will be flushed when we read from the network again */ } } diff --git a/src/qmail-qread.c b/src/qmail-qread.c index 888c14a..039af0c 100644 --- a/src/qmail-qread.c +++ b/src/qmail-qread.c @@ -1,43 +1,61 @@ -#include <unistd.h> -#include <sys/types.h> #include <sys/stat.h> -#include "stralloc.h" +#include <sys/types.h> +#include <unistd.h> + +#include "error.h" +#include "exit.h" #include "fmt.h" -#include "str.h" #include "getln.h" -#include "fmtqfn.h" -#include "readsubdir.h" -#include "auto_qmail.h" #include "open.h" -#include "datetime.h" +#include "str.h" +#include "stralloc.h" + +#include "auto_qmail.h" #include "date822fmt.h" -#include "error.h" -#include "exit.h" +#include "datetime.h" +#include "fmtqfn.h" +#include "readsubdir.h" readsubdir rs; -void die(int n) { buffer_flush(buffer_1); _exit(n); } +void die(int n) +{ + buffer_flush(buffer_1); + _exit(n); +} -void warn(char *s1,char *s2) +void warn(char *s1, char *s2) { - char *x; - x = error_str(errno); - buffer_puts(buffer_1,s1); - buffer_puts(buffer_1,s2); - buffer_puts(buffer_1,": "); - buffer_puts(buffer_1,x); - buffer_puts(buffer_1,"\n"); + char *x; + x = error_str(errno); + buffer_puts(buffer_1, s1); + buffer_puts(buffer_1, s2); + buffer_puts(buffer_1, ": "); + buffer_puts(buffer_1, x); + buffer_puts(buffer_1, "\n"); } -void die_nomem() { buffer_puts(buffer_1,"fatal: out of memory\n"); die(111); } -void die_chdir() { warn("fatal: unable to chdir",""); die(110); } -void die_opendir(fn) char *fn; { warn("fatal: unable to opendir ",fn); die(110); } +void die_nomem() +{ + buffer_puts(buffer_1, "fatal: out of memory\n"); + die(111); +} +void die_chdir() +{ + warn("fatal: unable to chdir", ""); + die(110); +} +void die_opendir(fn) char *fn; +{ + warn("fatal: unable to opendir ", fn); + die(110); +} void err(unsigned long id) { - char foo[FMT_ULONG]; - foo[fmt_ulong(foo,id)] = 0; - warn("warning: trouble with #",foo); + char foo[FMT_ULONG]; + foo[fmt_ulong(foo, id)] = 0; + warn("warning: trouble with #", foo); } char fnmess[FMTQFN]; @@ -61,40 +79,64 @@ unsigned int fmtstats(char *s) unsigned int i; len = 0; - datetime_tai(&dt,qtime); - i = date822fmt(s,&dt) - 7/*XXX*/; len += i; if (s) s += i; - i = fmt_str(s," GMT #"); len += i; if (s) s += i; - i = fmt_ulong(s,id); len += i; if (s) s += i; - i = fmt_str(s," "); len += i; if (s) s += i; - i = fmt_ulong(s,size); len += i; if (s) s += i; - i = fmt_str(s," <"); len += i; if (s) s += i; - i = fmt_str(s,sender.s + 1); len += i; if (s) s += i; - i = fmt_str(s,"> "); len += i; if (s) s += i; + datetime_tai(&dt, qtime); + i = date822fmt(s, &dt) - 7 /*XXX*/; + len += i; + if (s) s += i; + i = fmt_str(s, " GMT #"); + len += i; + if (s) s += i; + i = fmt_ulong(s, id); + len += i; + if (s) s += i; + i = fmt_str(s, " "); + len += i; + if (s) s += i; + i = fmt_ulong(s, size); + len += i; + if (s) s += i; + i = fmt_str(s, " <"); + len += i; + if (s) s += i; + i = fmt_str(s, sender.s + 1); + len += i; + if (s) s += i; + i = fmt_str(s, "> "); + len += i; + if (s) s += i; if (flagbounce) { - i = fmt_str(s," bouncing"); len += i; if (s) s += i; + i = fmt_str(s, " bouncing"); + len += i; + if (s) s += i; } - return len; + return len; } stralloc stats = {0}; -void out(char *s,unsigned int n) +void out(char *s, unsigned int n) { while (n > 0) { - buffer_put(buffer_1,((*s >= 32) && (*s <= 126)) ? s : "_",1); + buffer_put(buffer_1, ((*s >= 32) && (*s <= 126)) ? s : "_", 1); --n; ++s; } } -void outs(char *s) { out(s,str_len(s)); } -void outok(char *s) { buffer_puts(buffer_1,s); } +void outs(char *s) +{ + out(s, str_len(s)); +} +void outok(char *s) +{ + buffer_puts(buffer_1, s); +} void putstats() { - if (!stralloc_ready(&stats,fmtstats(FMT_LEN))) die_nomem(); + if (!stralloc_ready(&stats, fmtstats(FMT_LEN))) die_nomem(); stats.len = fmtstats(stats.s); - out(stats.s,stats.len); + out(stats.s, stats.len); outok("\n"); } @@ -111,25 +153,35 @@ int main() if (chdir(auto_qmail) == -1) die_chdir(); if (chdir("queue") == -1) die_chdir(); - readsubdir_init(&rs,"info",die_opendir); + readsubdir_init(&rs, "info", die_opendir); - while ((x = readsubdir_next(&rs,&id))) + while ((x = readsubdir_next(&rs, &id))) if (x > 0) { - fmtqfn(fnmess,"mess/",id,1); - fmtqfn(fninfo,"info/",id,1); - fmtqfn(fnlocal,"local/",id,1); - fmtqfn(fnremote,"remote/",id,1); - fmtqfn(fnbounce,"bounce/",id,0); - - if (stat(fnmess,&st) == -1) { err(id); continue; } + fmtqfn(fnmess, "mess/", id, 1); + fmtqfn(fninfo, "info/", id, 1); + fmtqfn(fnlocal, "local/", id, 1); + fmtqfn(fnremote, "remote/", id, 1); + fmtqfn(fnbounce, "bounce/", id, 0); + + if (stat(fnmess, &st) == -1) { + err(id); + continue; + } size = st.st_size; - flagbounce = !stat(fnbounce,&st); + flagbounce = !stat(fnbounce, &st); fd = open_read(fninfo); - if (fd == -1) { err(id); continue; } - buffer_init(&b,read,fd,inbuf,sizeof(inbuf)); - if (getln(&b,&sender,&match,0) == -1) die_nomem(); - if (fstat(fd,&st) == -1) { close(fd); err(id); continue; } + if (fd == -1) { + err(id); + continue; + } + buffer_init(&b, read, fd, inbuf, sizeof(inbuf)); + if (getln(&b, &sender, &match, 0) == -1) die_nomem(); + if (fstat(fd, &st) == -1) { + close(fd); + err(id); + continue; + } close(fd); qtime = st.st_mtime; @@ -141,22 +193,21 @@ int main() if (errno != ENOENT) err(id); } else { for (;;) { - if (getln(&b,&line,&match,0) == -1) die_nomem(); - if (!match) break; - switch (line.s[0]) { - case 'D': - outok(" done"); - case 'T': - outok(channel ? "\tremote\t" : "\tlocal\t"); - outs(line.s + 1); - outok("\n"); - break; - } + if (getln(&b, &line, &match, 0) == -1) die_nomem(); + if (!match) break; + switch (line.s[0]) { + case 'D': outok(" done"); + case 'T': + outok(channel ? "\tremote\t" : "\tlocal\t"); + outs(line.s + 1); + outok("\n"); + break; + } } close(fd); } - } - } + } + } die(0); } diff --git a/src/qmail-queue.c b/src/qmail-queue.c index b1289dc..ee9a3df 100644 --- a/src/qmail-queue.c +++ b/src/qmail-queue.c @@ -1,28 +1,30 @@ -#include <unistd.h> -#include <sys/types.h> -#include <sys/stat.h> #include <fcntl.h> -#include "sig.h" +#include <sys/stat.h> +#include <sys/types.h> +#include <unistd.h> + +#include "alloc.h" +#include "buffer.h" +#include "env.h" #include "exit.h" +#include "fmt.h" #include "open.h" +#include "scan.h" #include "seek.h" -#include "fmt.h" -#include "alloc.h" -#include "buffer.h" -#include "datetime.h" -#include "now.h" -#include "triggerpull.h" -#include "extra.h" +#include "sig.h" +#include "wait.h" + #include "auto_qmail.h" #include "auto_uids.h" #include "date822fmt.h" +#include "datetime.h" +#include "extra.h" #include "fmtqfn.h" -#include "env.h" -#include "wait.h" -#include "scan.h" +#include "now.h" +#include "triggerpull.h" #define DEATH 86400 /* 24 hours; _must_ be below q-s's OSSIFIED (36 hours) */ -#define ADDR 1003 +#define ADDR 1003 char inbuf[2048]; struct buffer bi; @@ -47,21 +49,42 @@ int flagmadeintd = 0; void cleanup() { if (flagmadeintd) { - seek_trunc(intdfd,0); - if (unlink(intdfn) == -1) return; + seek_trunc(intdfd, 0); + if (unlink(intdfn) == -1) return; } if (flagmademess) { - seek_trunc(messfd,0); + seek_trunc(messfd, 0); if (unlink(messfn) == -1) return; } } -void die(int e) { _exit(e); } -void die_qhpsi() { cleanup(); die(71); } -void die_write() { cleanup(); die(53); } -void die_read() { cleanup(); die(54); } -void sigalrm() { /* thou shalt not clean up here */ die(52); } -void sigbug() { die(81); } +void die(int e) +{ + _exit(e); +} +void die_qhpsi() +{ + cleanup(); + die(71); +} +void die_write() +{ + cleanup(); + die(53); +} +void die_read() +{ + cleanup(); + die(54); +} +void sigalrm() +{ /* thou shalt not clean up here */ + die(52); +} +void sigbug() +{ + die(81); +} unsigned int receivedlen; char *received; @@ -73,56 +96,89 @@ static unsigned int receivedfmt(char *s) unsigned int len; len = 0; - i = fmt_str(s,"Received: (qmail "); len += i; if (s) s += i; - i = fmt_ulong(s,mypid); len += i; if (s) s += i; - i = fmt_str(s," invoked "); len += i; if (s) s += i; - if (uid == auto_uida) { - i = fmt_str(s,"by alias"); len += i; if (s) s += i; - } else if (uid == auto_uidd) { - i = fmt_str(s,"from network"); len += i; if (s) s += i; - } else if (uid == auto_uids) { - i = fmt_str(s,"for bounce"); len += i; if (s) s += i; + i = fmt_str(s, "Received: (qmail "); + len += i; + if (s) s += i; + i = fmt_ulong(s, mypid); + len += i; + if (s) s += i; + i = fmt_str(s, " invoked "); + len += i; + if (s) s += i; + if (uid == auto_uida) { + i = fmt_str(s, "by alias"); + len += i; + if (s) s += i; + } else if (uid == auto_uidd) { + i = fmt_str(s, "from network"); + len += i; + if (s) s += i; + } else if (uid == auto_uids) { + i = fmt_str(s, "for bounce"); + len += i; + if (s) s += i; } else { - i = fmt_str(s,"by uid "); len += i; if (s) s += i; - i = fmt_ulong(s,uid); len += i; if (s) s += i; + i = fmt_str(s, "by uid "); + len += i; + if (s) s += i; + i = fmt_ulong(s, uid); + len += i; + if (s) s += i; } - i = fmt_str(s,"); "); len += i; if (s) s += i; - i = date822fmt(s,&dt); len += i; if (s) s += i; + i = fmt_str(s, "); "); + len += i; + if (s) s += i; + i = date822fmt(s, &dt); + len += i; + if (s) s += i; return len; } void received_setup() { - receivedlen = receivedfmt((char *) 0); + receivedlen = receivedfmt((char *)0); received = alloc(receivedlen + 1); if (!received) die(51); receivedfmt(received); } -unsigned int pidfmt(char *s,unsigned long seq) +unsigned int pidfmt(char *s, unsigned long seq) { unsigned int i; unsigned int len; len = 0; - i = fmt_str(s,"pid/"); len += i; if (s) s += i; - i = fmt_ulong(s,mypid); len += i; if (s) s += i; - i = fmt_str(s,"."); len += i; if (s) s += i; - i = fmt_ulong(s,starttime); len += i; if (s) s += i; - i = fmt_str(s,"."); len += i; if (s) s += i; - i = fmt_ulong(s,seq); len += i; if (s) s += i; - ++len; if (s) *s++ = 0; + i = fmt_str(s, "pid/"); + len += i; + if (s) s += i; + i = fmt_ulong(s, mypid); + len += i; + if (s) s += i; + i = fmt_str(s, "."); + len += i; + if (s) s += i; + i = fmt_ulong(s, starttime); + len += i; + if (s) s += i; + i = fmt_str(s, "."); + len += i; + if (s) s += i; + i = fmt_ulong(s, seq); + len += i; + if (s) s += i; + ++len; + if (s) *s++ = 0; return len; } -char *fnnum(char *dirslash,int flagsplit) +char *fnnum(char *dirslash, int flagsplit) { char *s; - s = alloc(fmtqfn((char *) 0,dirslash,messnum,flagsplit)); + s = alloc(fmtqfn((char *)0, dirslash, messnum, flagsplit)); if (!s) die(51); - fmtqfn(s,dirslash,messnum,flagsplit); + fmtqfn(s, dirslash, messnum, flagsplit); return s; } @@ -132,18 +188,18 @@ void pidopen(void) unsigned long seq; seq = 1; - len = pidfmt((char *) 0,seq); + len = pidfmt((char *)0, seq); pidfn = alloc(len); if (!pidfn) die(51); for (seq = 1; seq < 10; ++seq) { - if (pidfmt((char *) 0,seq) > len) die(81); /* paranoia */ - pidfmt(pidfn,seq); + if (pidfmt((char *)0, seq) > len) die(81); /* paranoia */ + pidfmt(pidfn, seq); messfd = open_excl(pidfn); if (messfd != -1) return; } - die(63); + die(63); } char *qhpsi; @@ -152,10 +208,10 @@ void qhpsiprog(char *arg) { int wstat; int child; - char *qhpsiargs[6] = { 0, 0, 0, 0, 0, 0 }; + char *qhpsiargs[6] = {0, 0, 0, 0, 0, 0}; char *x; - unsigned long u; - int childrc; + unsigned long u; + int childrc; int qhpsirc = 1; unsigned int size; unsigned int qhpsiminsize = 0; @@ -163,39 +219,52 @@ void qhpsiprog(char *arg) struct stat st; - if (stat(messfn,&st) == -1) die(63); - size = (unsigned int) st.st_size; + if (stat(messfn, &st) == -1) die(63); + size = (unsigned int)st.st_size; x = env_get("QHPSIMINSIZE"); - if (x) { scan_ulong(x,&u); qhpsiminsize = (int) u; } - if (qhpsiminsize) if (size < qhpsiminsize) return; + if (x) { + scan_ulong(x, &u); + qhpsiminsize = (int)u; + } + if (qhpsiminsize) + if (size < qhpsiminsize) return; x = env_get("QHPSIMAXSIZE"); - if (x) { scan_ulong(x,&u); qhpsimaxsize = (int) u; } - if (qhpsimaxsize) if (size > qhpsimaxsize) return; + if (x) { + scan_ulong(x, &u); + qhpsimaxsize = (int)u; + } + if (qhpsimaxsize) + if (size > qhpsimaxsize) return; if (*arg) { switch (child = fork()) { - case -1: - die_qhpsi(); + case -1: die_qhpsi(); case 0: - qhpsiargs[0] = arg; + qhpsiargs[0] = arg; qhpsiargs[1] = messfn; qhpsiargs[2] = env_get("QHPSIARG1"); - if (!qhpsiargs[2]) qhpsiargs[2] = 0; + if (!qhpsiargs[2]) qhpsiargs[2] = 0; qhpsiargs[3] = env_get("QHPSIARG2"); - if (!qhpsiargs[3]) qhpsiargs[3] = 0; + if (!qhpsiargs[3]) qhpsiargs[3] = 0; qhpsiargs[4] = env_get("QHPSIARG3"); - if (!qhpsiargs[4]) qhpsiargs[4] = 0; + if (!qhpsiargs[4]) qhpsiargs[4] = 0; x = env_get("QHPSIRC"); - if (x) { scan_ulong(x,&u); qhpsirc = (int) u; } - execvp(*qhpsiargs,qhpsiargs); + if (x) { + scan_ulong(x, &u); + qhpsirc = (int)u; + } + execvp(*qhpsiargs, qhpsiargs); die_qhpsi(); } - if (wait_pid(&wstat,child) == -1) die_qhpsi(); + if (wait_pid(&wstat, child) == -1) die_qhpsi(); if (wait_crashed(wstat)) die_qhpsi(); - childrc = wait_exitcode(wstat); - if (childrc == qhpsirc) { cleanup(); die(32); } - else if (childrc != 0) die_qhpsi(); + childrc = wait_exitcode(wstat); + if (childrc == qhpsirc) { + cleanup(); + die(32); + } else if (childrc != 0) + die_qhpsi(); } } @@ -215,7 +284,7 @@ int main() mypid = getpid(); uid = getuid(); starttime = now(); - datetime_tai(&dt,starttime); + datetime_tai(&dt, starttime); qhpsi = env_get("QHPSI"); received_setup(); @@ -228,23 +297,23 @@ int main() alarm(DEATH); pidopen(); - if (fstat(messfd,&pidst) == -1) die(63); + if (fstat(messfd, &pidst) == -1) die(63); messnum = pidst.st_ino; - messfn = fnnum("mess/",1); - todofn = fnnum("todo/",1); - intdfn = fnnum("intd/",1); + messfn = fnnum("mess/", 1); + todofn = fnnum("todo/", 1); + intdfn = fnnum("intd/", 1); - if (link(pidfn,messfn) == -1) die(64); + if (link(pidfn, messfn) == -1) die(64); if (unlink(pidfn) == -1) die(63); flagmademess = 1; - buffer_init(&bo,write,messfd,outbuf,sizeof(outbuf)); - buffer_init(&bi,read,0,inbuf,sizeof(inbuf)); + buffer_init(&bo, write, messfd, outbuf, sizeof(outbuf)); + buffer_init(&bi, read, 0, inbuf, sizeof(inbuf)); - if (buffer_put(&bo,received,receivedlen) == -1) die_write(); + if (buffer_put(&bo, received, receivedlen) == -1) die_write(); - switch (buffer_copy(&bo,&bi)) { + switch (buffer_copy(&bo, &bi)) { case -2: die_read(); case -3: die_write(); } @@ -255,38 +324,41 @@ int main() if (intdfd == -1) die(65); flagmadeintd = 1; - buffer_init(&bo,write,intdfd,outbuf,sizeof(outbuf)); - buffer_init(&bi,read,1,inbuf,sizeof(inbuf)); + buffer_init(&bo, write, intdfd, outbuf, sizeof(outbuf)); + buffer_init(&bi, read, 1, inbuf, sizeof(inbuf)); - if (buffer_put(&bo,"u",1) == -1) die_write(); - if (buffer_put(&bo,tmp,fmt_ulong(tmp,uid)) == -1) die_write(); - if (buffer_put(&bo,"",1) == -1) die_write(); + if (buffer_put(&bo, "u", 1) == -1) die_write(); + if (buffer_put(&bo, tmp, fmt_ulong(tmp, uid)) == -1) die_write(); + if (buffer_put(&bo, "", 1) == -1) die_write(); - if (buffer_put(&bo,"p",1) == -1) die_write(); - if (buffer_put(&bo,tmp,fmt_ulong(tmp,mypid)) == -1) die_write(); - if (buffer_put(&bo,"",1) == -1) die_write(); + if (buffer_put(&bo, "p", 1) == -1) die_write(); + if (buffer_put(&bo, tmp, fmt_ulong(tmp, mypid)) == -1) die_write(); + if (buffer_put(&bo, "", 1) == -1) die_write(); - if (buffer_get(&bi,&ch,1) < 1) die_read(); + if (buffer_get(&bi, &ch, 1) < 1) die_read(); if (ch != 'F') die(91); - if (buffer_put(&bo,&ch,1) == -1) die_write(); + if (buffer_put(&bo, &ch, 1) == -1) die_write(); for (len = 0; len < ADDR; ++len) { - if (buffer_get(&bi,&ch,1) < 1) die_read(); - if (buffer_put(&bo,&ch,1) == -1) die_write(); + if (buffer_get(&bi, &ch, 1) < 1) die_read(); + if (buffer_put(&bo, &ch, 1) == -1) die_write(); if (!ch) break; } if (len >= ADDR) die(11); - if (buffer_put(&bo,QUEUE_EXTRA,QUEUE_EXTRALEN) == -1) die_write(); + if (buffer_put(&bo, QUEUE_EXTRA, QUEUE_EXTRALEN) == -1) die_write(); for (;;) { - if (buffer_get(&bi,&ch,1) < 1) die_read(); + if (buffer_get(&bi, &ch, 1) < 1) die_read(); if (!ch) break; - if (ch == 'Q') { qhpsi = 0; break; } + if (ch == 'Q') { + qhpsi = 0; + break; + } if (ch != 'T') die(91); - if (buffer_put(&bo,&ch,1) == -1) die_write(); + if (buffer_put(&bo, &ch, 1) == -1) die_write(); for (len = 0; len < ADDR; ++len) { - if (buffer_get(&bi,&ch,1) < 1) die_read(); - if (buffer_put(&bo,&ch,1) == -1) die_write(); + if (buffer_get(&bi, &ch, 1) < 1) die_read(); + if (buffer_put(&bo, &ch, 1) == -1) die_write(); if (!ch) break; } if (len >= ADDR) die(11); @@ -297,8 +369,8 @@ int main() if (buffer_flush(&bo) == -1) die_write(); if (fsync(intdfd) == -1) die_write(); - if (link(intdfn,todofn) == -1) die(66); - if ((fd = open(todofn,O_RDONLY)) < 0 || fsync(fd) < 0 || close(fd)) die(66); + if (link(intdfn, todofn) == -1) die(66); + if ((fd = open(todofn, O_RDONLY)) < 0 || fsync(fd) < 0 || close(fd)) die(66); triggerpull(); die(0); diff --git a/src/qmail-recipients.c b/src/qmail-recipients.c index 058994a..4fb2fb8 100644 --- a/src/qmail-recipients.c +++ b/src/qmail-recipients.c @@ -1,26 +1,28 @@ -#include <unistd.h> #include <sys/stat.h> -#include "logmsg.h" -#include "stralloc.h" +#include <unistd.h> + #include "buffer.h" -#include "getln.h" +#include "case.h" +#include "cdbmake.h" #include "exit.h" +#include "getln.h" +#include "logmsg.h" #include "open.h" -#include "case.h" +#include "stralloc.h" + #include "auto_qmail.h" -#include "cdbmake.h" #define WHO "qmail-recipients" -int rename(const char *,const char *); // stdio.h +int rename(const char *, const char *); // stdio.h void die_read() { - logmsg(WHO,111,ERROR,"unable to read users/recipients"); + logmsg(WHO, 111, ERROR, "unable to read users/recipients"); } void die_write() { - logmsg(WHO,111,ERROR,"unable to write to users/recipients.tmp"); + logmsg(WHO, 111, ERROR, "unable to write to users/recipients.tmp"); } char inbuf[1024]; @@ -31,36 +33,43 @@ int fdtemp; struct cdb_make cdb; stralloc line = {0}; -stralloc key = {0}; +stralloc key = {0}; int match; int main() { umask(033); - if (chdir(auto_qmail) == -1) - logmsg(WHO,110,ERROR,B("unable to chdir to: ",auto_qmail)); + if (chdir(auto_qmail) == -1) logmsg(WHO, 110, ERROR, B("unable to chdir to: ", auto_qmail)); fd = open_read("users/recipients"); if (fd == -1) die_read(); - buffer_init(&b,read,fd,inbuf,sizeof(inbuf)); + buffer_init(&b, read, fd, inbuf, sizeof(inbuf)); fdtemp = open_trunc("users/recipients.tmp"); if (fdtemp == -1) die_write(); - if (cdb_make_start(&cdb,fdtemp) == -1) die_write(); + if (cdb_make_start(&cdb, fdtemp) == -1) die_write(); for (;;) { - stralloc_copys(&key,":"); - if (getln(&b,&line,&match,'\n') != 0) die_read(); + stralloc_copys(&key, ":"); + if (getln(&b, &line, &match, '\n') != 0) die_read(); while (line.len) { - if (line.s[line.len - 1] == ' ') { --line.len; continue; } - if (line.s[line.len - 1] == '\n') { --line.len; continue; } - if (line.s[line.len - 1] == '\t') { --line.len; continue; } - if (line.s[0] != '#' && stralloc_cat(&key,&line)) { - case_lowerb(key.s,key.len); - if (cdb_make_add(&cdb,key.s,key.len,"",0) == -1) - die_write(); + if (line.s[line.len - 1] == ' ') { + --line.len; + continue; + } + if (line.s[line.len - 1] == '\n') { + --line.len; + continue; + } + if (line.s[line.len - 1] == '\t') { + --line.len; + continue; + } + if (line.s[0] != '#' && stralloc_cat(&key, &line)) { + case_lowerb(key.s, key.len); + if (cdb_make_add(&cdb, key.s, key.len, "", 0) == -1) die_write(); } break; } @@ -70,8 +79,8 @@ int main() if (cdb_make_finish(&cdb) == -1) die_write(); if (fsync(fdtemp) == -1) die_write(); if (close(fdtemp) == -1) die_write(); /* NFS stupidity */ - if (rename("users/recipients.tmp","users/recipients.cdb") == -1) - logmsg(WHO,111,ERROR,"unable to move users/recipients.tmp to users/recipients.cdb"); + if (rename("users/recipients.tmp", "users/recipients.cdb") == -1) + logmsg(WHO, 111, ERROR, "unable to move users/recipients.tmp to users/recipients.cdb"); _exit(0); } diff --git a/src/qmail-remote.c b/src/qmail-remote.c index 5ef9465..2a7498c 100644 --- a/src/qmail-remote.c +++ b/src/qmail-remote.c @@ -1,76 +1,77 @@ #ifdef IDN2 -#include <idn2.h> + #include <idn2.h> #endif -#include <sys/types.h> -#include <sys/stat.h> -#include <sys/socket.h> -#include <netinet/in.h> #include <arpa/inet.h> +#include <netinet/in.h> +#include <sys/socket.h> +#include <sys/stat.h> +#include <sys/types.h> #include <unistd.h> -#include "sig.h" -#include "stralloc.h" + +#include "alloc.h" +#include "base64.h" #include "buffer.h" -#include "scan.h" -#include "case.h" #include "byte.h" +#include "case.h" +#include "constmap.h" +#include "exit.h" +#include "fmt.h" +#include "genalloc.h" +#include "ip.h" #include "logmsg.h" +#include "scan.h" +#include "sig.h" +#include "socket_if.h" +#include "str.h" +#include "stralloc.h" +#include "timeout.h" +#include "timeoutconn.h" +#include "uint_t.h" + #include "auto_qmail.h" #include "control.h" #include "dns.h" -#include "alloc.h" -#include "genalloc.h" -#include "quote.h" -#include "fmt.h" -#include "ip.h" +#include "hmac_md5.h" #include "ipalloc.h" #include "ipme.h" -#include "str.h" #include "now.h" -#include "exit.h" -#include "constmap.h" +#include "quote.h" #include "tcpto.h" -#include "timeout.h" -#include "timeoutconn.h" -#include "base64.h" -#include "socket_if.h" -#include "ucspissl.h" -#include "hmac_md5.h" -#include "tls_remote.h" #include "tls_errors.h" +#include "tls_remote.h" #include "tls_timeoutio.h" -#include "uint_t.h" +#include "ucspissl.h" #define WHO "qmail-remote" -#define MAX_SIZE 200000000 -#define HUGESMTPTEXT 1000 /* RFC 5322; was 5000 chars/line */ -#define PORT_SMTP 25 /* silly rabbit, /etc/services is for users */ -#define PORT_QMTP 209 -#define PORT_SMTPS 465 +#define MAX_SIZE 200000000 +#define HUGESMTPTEXT 1000 /* RFC 5322; was 5000 chars/line */ +#define PORT_SMTP 25 /* silly rabbit, /etc/services is for users */ +#define PORT_QMTP 209 +#define PORT_SMTPS 465 #define PORT_SUBMISSION 587 -#define PORT_QMTPS 6209 -#define VERIFYDEPTH 1 +#define PORT_QMTPS 6209 +#define VERIFYDEPTH 1 unsigned long port = PORT_SMTP; /** @file qmail-remote.c -- versatile SMTP(S)/QMTP(S) client */ -int flagauth = 0; /* 1 = login; 2 = plain; 3 = crammd5 */ -int flagsmtps = 0; /* RFC 8314 - 'implicit TLS' */ -int flagtlsdomain = 0; /* 0 = no; 1 = yes; 2 = cert */ -int flagtls = 0; /* flagtls: XYZ +int flagauth = 0; /* 1 = login; 2 = plain; 3 = crammd5 */ +int flagsmtps = 0; /* RFC 8314 - 'implicit TLS' */ +int flagtlsdomain = 0; /* 0 = no; 1 = yes; 2 = cert */ +int flagtls = 0; /* flagtls: XYZ (mode) Z: -2 = rejected; -1 = not; 0 = no, default; Z > 0 see tls_remote.c (prot) Y: 0 = StartTLS; 1 = SMTPS; 2 = QMTPS (active) X: 1 = running TLS connection (after DNS lookup) (done) Z: 1: CA chain; 2: Cert wildname; 3: Cert exactname; 4: Cert fingerprint; 5: TLSA record */ -int flagverify = 0; /* 1 = verify Cert against CA; 2 = verify against Dir; 3 = triggerd by TLSA; - -2 = Cert pinning; -1 = no TLSA validation */ +int flagverify = 0; /* 1 = verify Cert against CA; 2 = verify against Dir; 3 = triggerd by TLSA; + -2 = Cert pinning; -1 = no TLSA validation */ int flagutf8 = 0; -GEN_ALLOC_typedef(saa,stralloc,sa,len,a) -GEN_ALLOC_readyplus(saa,stralloc,sa,len,a,i,n,x,10,saa_readyplus) -static stralloc sauninit = {0}; +GEN_ALLOC_typedef(saa, stralloc, sa, len, a) GEN_ALLOC_readyplus( + saa, stralloc, sa, len, a, i, n, x, 10, saa_readyplus) static stralloc sauninit = {0}; stralloc helohost = {0}; stralloc eaihost = {0}; @@ -103,22 +104,20 @@ unsigned long msize = 0; struct ip_mx partner; SSL *ssl; -SSL_CTX *ctx; +SSL_CTX *ctx; char smallbuf[BUFFER_SMALL]; -buffer bs = BUFFER_INIT(write,1,smallbuf,sizeof(smallbuf)); - -void out(char *s) -{ - if (buffer_puts(&bs,s) == -1) - _exit(0); - } -void zero() -{ - if (buffer_put(&bs,"\0",1) == -1) - _exit(0); +buffer bs = BUFFER_INIT(write, 1, smallbuf, sizeof(smallbuf)); + +void out(char *s) +{ + if (buffer_puts(&bs, s) == -1) _exit(0); +} +void zero() +{ + if (buffer_put(&bs, "\0", 1) == -1) _exit(0); } -void zerodie() +void zerodie() { zero(); buffer_flush(&bs); @@ -126,127 +125,127 @@ void zerodie() _exit(0); } -void outsafe(stralloc *sa) -{ - int i; +void outsafe(stralloc *sa) +{ + int i; char ch; for (i = 0; i < sa->len; ++i) { - ch = sa->s[i]; + ch = sa->s[i]; if (ch == 0) continue; - if (ch < 33) ch = '?'; + if (ch < 33) ch = '?'; if (ch > 126) ch = '?'; - if (buffer_put(&bs,&ch,1) == -1) _exit(0); - } + if (buffer_put(&bs, &ch, 1) == -1) _exit(0); + } } -void temp_noip() -{ - out("ZInvalid ipaddr in control/domainips (#4.3.0)\n"); - zerodie(); +void temp_noip() +{ + out("ZInvalid ipaddr in control/domainips (#4.3.0)\n"); + zerodie(); } -void temp_nomem() -{ - out("ZOut of memory. (#4.3.0)\n"); - zerodie(); +void temp_nomem() +{ + out("ZOut of memory. (#4.3.0)\n"); + zerodie(); } -void temp_oserr() -{ - out("ZSystem resources temporarily unavailable. (#4.3.0)\n"); - zerodie(); +void temp_oserr() +{ + out("ZSystem resources temporarily unavailable. (#4.3.0)\n"); + zerodie(); } -void temp_osip() -{ - out("ZCan't bind to local ip address: "); - outsafe(&sendip); +void temp_osip() +{ + out("ZCan't bind to local ip address: "); + outsafe(&sendip); out(". (#4.3.0)\n"); - zerodie(); + zerodie(); } -void temp_noconn() -{ +void temp_noconn() +{ out("ZSorry, I wasn't able to establish an SMTP connection: "); - outsafe(&canonhost); + outsafe(&canonhost); out(". (#4.3.0)\n"); - zerodie(); + zerodie(); } -void temp_qmtpnoc() -{ +void temp_qmtpnoc() +{ out("ZSorry, I wasn't able to establish an QMTP connection: "); - outsafe(&canonhost); + outsafe(&canonhost); out(". (#4.3.1)\n"); - zerodie(); + zerodie(); } -void temp_read() -{ - out("ZUnable to read message. (#4.3.0)\n"); - zerodie(); +void temp_read() +{ + out("ZUnable to read message. (#4.3.0)\n"); + zerodie(); } -void temp_dnscanon() -{ - out("ZCNAME lookup failed temporarily for: "); - outsafe(&canonhost); +void temp_dnscanon() +{ + out("ZCNAME lookup failed temporarily for: "); + outsafe(&canonhost); out(". (#4.4.3)\n"); - zerodie(); + zerodie(); } -void temp_dns() -{ - out("ZSorry, I couldn't find any host named: "); - outsafe(&host); - out(". (#4.1.2)\n"); - zerodie(); +void temp_dns() +{ + out("ZSorry, I couldn't find any host named: "); + outsafe(&host); + out(". (#4.1.2)\n"); + zerodie(); } -void temp_nomx() -{ +void temp_nomx() +{ out("ZSorry, I couldn't find a mail exchanger or IP address for: "); - outsafe(&host); - out(". Will try again. (#4.1.2)\n"); - zerodie(); + outsafe(&host); + out(". Will try again. (#4.1.2)\n"); + zerodie(); } -void temp_chdir() -{ - out("ZUnable to switch to home directory. (#4.3.0)\n"); - zerodie(); +void temp_chdir() +{ + out("ZUnable to switch to home directory. (#4.3.0)\n"); + zerodie(); } -void temp_control() -{ - out("ZUnable to read control files. (#4.3.0)\n"); - zerodie(); +void temp_control() +{ + out("ZUnable to read control files. (#4.3.0)\n"); + zerodie(); } -void perm_partialline() -{ - out("DSMTP cannot transfer messages with partial final lines. (#5.6.2)\n"); - zerodie(); +void perm_partialline() +{ + out("DSMTP cannot transfer messages with partial final lines. (#5.6.2)\n"); + zerodie(); } -void temp_proto() -{ - out("ZRecipient did not talk proper QMTP (#4.3.0)\n"); - zerodie(); +void temp_proto() +{ + out("ZRecipient did not talk proper QMTP (#4.3.0)\n"); + zerodie(); } -void perm_usage() -{ - out("Dqmail-remote was invoked improperly. (#5.3.5)\n"); - zerodie(); +void perm_usage() +{ + out("Dqmail-remote was invoked improperly. (#5.3.5)\n"); + zerodie(); } -void perm_dns() -{ - out("DSorry, I couldn't find any host named: "); - outsafe(&host); - out(". (#5.1.2)\n"); - zerodie(); +void perm_dns() +{ + out("DSorry, I couldn't find any host named: "); + outsafe(&host); + out(". (#5.1.2)\n"); + zerodie(); } -void perm_nomx() -{ +void perm_nomx() +{ out("DSorry, I couldn't find a mail exchanger or IP address for: "); - outsafe(&host); - out(". (#5.4.4)\n"); - zerodie(); + outsafe(&host); + out(". (#5.4.4)\n"); + zerodie(); } -void perm_ambigmx() -{ +void perm_ambigmx() +{ out("DSorry. Although I'm listed as a best-preference MX or A for that host,\n\ it isn't in my control/locals file, so I don't treat it as local. (#5.4.6)\n"); - zerodie(); + zerodie(); } -void err_authprot() +void err_authprot() { out("KNo supported AUTH method found, continuing without authentication.\n"); } @@ -257,17 +256,15 @@ void outhost() int len; switch (partner.af) { - case AF_INET: - len = ip4_fmt(ipaddr,(char *)&partner.addr.ip4.d); break; - case AF_INET6: - len = ip6_fmt(ipaddr,(char *)&partner.addr.ip6.d); break; - } - if (buffer_put(&bs,ipaddr,len) == -1) _exit(0); + case AF_INET: len = ip4_fmt(ipaddr, (char *)&partner.addr.ip4.d); break; + case AF_INET6: len = ip6_fmt(ipaddr, (char *)&partner.addr.ip6.d); break; + } + if (buffer_put(&bs, ipaddr, len) == -1) _exit(0); } int flagcritical = 0; -void dropped() +void dropped() { out("ZConnected to "); outhost(); @@ -281,48 +278,48 @@ int timeoutconnect = 60; int smtpfd; int timeout = 1200; -ssize_t saferead(int fd,char *buf,int len) +ssize_t saferead(int fd, char *buf, int len) { int r; if (ssl) { - r = tls_timeoutread(timeout,smtpfd,smtpfd,ssl,buf,len); + r = tls_timeoutread(timeout, smtpfd, smtpfd, ssl, buf, len); if (r < 0) temp_tlserr(); } else { - r = timeoutread(timeout,smtpfd,buf,len); + r = timeoutread(timeout, smtpfd, buf, len); } if (r <= 0) dropped(); return r; } -ssize_t safewrite(int fd,char *buf,int len) +ssize_t safewrite(int fd, char *buf, int len) { int r; if (ssl) { - r = tls_timeoutwrite(timeout,smtpfd,smtpfd,ssl,buf,len); + r = tls_timeoutwrite(timeout, smtpfd, smtpfd, ssl, buf, len); if (r < 0) temp_tlserr(); } else { - r = timeoutwrite(timeout,smtpfd,buf,len); + r = timeoutwrite(timeout, smtpfd, buf, len); } if (r <= 0) dropped(); return r; } char inbuf[BUFFER_MTUSIZE]; -buffer bi = BUFFER_INIT(read,0,inbuf,sizeof(inbuf)); +buffer bi = BUFFER_INIT(read, 0, inbuf, sizeof(inbuf)); char outbuf[BUFFER_MTUSIZE]; -buffer bo = BUFFER_INIT(safewrite,-1,outbuf,sizeof(outbuf)); +buffer bo = BUFFER_INIT(safewrite, -1, outbuf, sizeof(outbuf)); char frombuf[BUFFER_SMALL]; -buffer bf = BUFFER_INIT(saferead,-1,frombuf,sizeof(frombuf)); +buffer bf = BUFFER_INIT(saferead, -1, frombuf, sizeof(frombuf)); static stralloc smtptext = {0}; static stralloc header = {0}; - + void get(char *ch) { - buffer_get(&bf,ch,1); + buffer_get(&bf, ch, 1); if (*ch != '\r') if (smtptext.len < HUGESMTPTEXT) - if (!stralloc_append(&smtptext,ch)) temp_nomem(); + if (!stralloc_append(&smtptext, ch)) temp_nomem(); } unsigned long smtpcode() @@ -330,11 +327,14 @@ unsigned long smtpcode() unsigned char ch; unsigned long code; - if (!stralloc_copys(&smtptext,"")) temp_nomem(); + if (!stralloc_copys(&smtptext, "")) temp_nomem(); - get(&ch); code = ch - '0'; - get(&ch); code = code * 10 + (ch - '0'); - get(&ch); code = code * 10 + (ch - '0'); + get(&ch); + code = ch - '0'; + get(&ch); + code = code * 10 + (ch - '0'); + get(&ch); + code = code * 10 + (ch - '0'); for (;;) { get(&ch); if (ch != '-') break; @@ -350,19 +350,20 @@ unsigned long smtpcode() void outsmtptext() { - int i; - if (smtptext.s) if (smtptext.len) { - out("Remote host said: "); - for (i = 0; i < smtptext.len; ++i) - if (!smtptext.s[i]) smtptext.s[i] = '?'; - if (buffer_put(&bs,smtptext.s,smtptext.len) == -1) _exit(0); - smtptext.len = 0; - } + int i; + if (smtptext.s) + if (smtptext.len) { + out("Remote host said: "); + for (i = 0; i < smtptext.len; ++i) + if (!smtptext.s[i]) smtptext.s[i] = '?'; + if (buffer_put(&bs, smtptext.s, smtptext.len) == -1) _exit(0); + smtptext.len = 0; + } } -void quit(char *prepend,char *append) +void quit(char *prepend, char *append) { - buffer_putsflush(&bo,"QUIT\r\n"); + buffer_putsflush(&bo, "QUIT\r\n"); /* waiting for remote side is just too ridiculous */ out(prepend); outhost(); @@ -378,23 +379,22 @@ void blast() char ch; for (;;) { - r = buffer_get(&bi,&ch,1); + r = buffer_get(&bi, &ch, 1); if (r == 0) break; if (r == -1) temp_read(); - if (ch == '.') buffer_put(&bo,".",1); + if (ch == '.') buffer_put(&bo, ".", 1); while (ch != '\n') { - if (ch != '\r') - buffer_put(&bo,&ch,1); // DKIM input - r = buffer_get(&bi,&ch,1); + if (ch != '\r') buffer_put(&bo, &ch, 1); // DKIM input + r = buffer_get(&bi, &ch, 1); if (r == 0) perm_partialline(); if (r == -1) temp_read(); } - buffer_put(&bo,"\r\n",2); + buffer_put(&bo, "\r\n", 2); } flagcritical = 1; - buffer_put(&bo,".\r\n",3); + buffer_put(&bo, ".\r\n", 3); buffer_flush(&bo); } @@ -422,47 +422,46 @@ void tls_init() ssl_errstr(); if (!ctx) temp_tlsctx(); -/* Fetch CA infos for dest */ + /* Fetch CA infos for dest */ - if (flagverify > 0) - if (cafile.len || cadir.len) - if (!ssl_ca(ctx,cafile.s,cadir.s,(int) verifydepth)) temp_tlsca(); + if (flagverify > 0) + if (cafile.len || cadir.len) + if (!ssl_ca(ctx, cafile.s, cadir.s, (int)verifydepth)) temp_tlsca(); - if (ciphers.len) - if (!ssl_ciphers(ctx,ciphers.s)) temp_tlscipher(); + if (ciphers.len) + if (!ssl_ciphers(ctx, ciphers.s)) temp_tlscipher(); -/* Prepare for Certificate Request */ + /* Prepare for Certificate Request */ - if (flagtlsdomain == 2) - switch (tls_certkey(ctx,certfile.s,keyfile.s,keypwd.s)) { - case 0: break; - case -1: temp_tlscert(); + if (flagtlsdomain == 2) switch (tls_certkey(ctx, certfile.s, keyfile.s, keypwd.s)) + { + case 0: break; + case -1: temp_tlscert(); case -2: temp_tlskey(); - case -3: temp_tlschk(); + case -3: temp_tlschk(); } -/* Set SSL Context */ + /* Set SSL Context */ - ssl = ssl_new(ctx,smtpfd); + ssl = ssl_new(ctx, smtpfd); if (!ssl) temp_tlsctx(); -/* Setup SSL FDs */ - - if (!tls_conn(ssl,smtpfd)) temp_tlscon(); + /* Setup SSL FDs */ + + if (!tls_conn(ssl, smtpfd)) temp_tlscon(); -/* Go on in none-blocking mode */ + /* Go on in none-blocking mode */ - if (tls_timeoutconn(timeout,smtpfd,smtpfd,ssl) <= 0) - temp_tlserr(); + if (tls_timeoutconn(timeout, smtpfd, smtpfd, ssl) <= 0) temp_tlserr(); } int starttls_peer() { int i = 0; - while ((i += str_chr(smtptext.s+i,'\n') + 1) && - (i + 8 < smtptext.len) ) { - if (!str_diffn(smtptext.s + i + 4,"STARTTLS",8)) return 1; } + while ((i += str_chr(smtptext.s + i, '\n') + 1) && (i + 8 < smtptext.len)) { + if (!str_diffn(smtptext.s + i + 4, "STARTTLS", 8)) return 1; + } return 0; } @@ -470,48 +469,61 @@ int starttls_peer() void tls_peercheck() { X509 *cert; - STACK_OF(X509) *certs; - + STACK_OF(X509) * certs; + cert = SSL_get_peer_certificate(ssl); - if (!cert) { flagtls = 100; return; } + if (!cert) { + flagtls = 100; + return; + } if ((certs = SSL_get_peer_cert_chain(ssl)) == NULL) { certs = sk_X509_new_null(); sk_X509_push(certs, cert); } - if (flagverify == -2) { // fingerprinting is silent - if (cafile.len) case_lowerb(cafile.s,cafile.len); - switch (tls_fingerprint(cert,cafile.s + 1,cafile.len - 2)) { - case -1: temp_tlspeercert(); - case -2: temp_tlsdigest(); - case -3: temp_invaliddigest(); - case 0: temp_tlscertfp(); - case 1: flagtls = 104; break; + if (flagverify == -2) { // fingerprinting is silent + if (cafile.len) case_lowerb(cafile.s, cafile.len); + switch (tls_fingerprint(cert, cafile.s + 1, cafile.len - 2)) { + case -1: temp_tlspeercert(); + case -2: temp_tlsdigest(); + case -3: temp_invaliddigest(); + case 0: temp_tlscertfp(); + case 1: flagtls = 104; break; } - } - - if (flagverify >= 0) { // TLSA is default - switch (tlsa_check(certs,remotehost,port)) { - case -4: temp_tlsamissing(); break; /* FIXME */ - case -3: temp_tlsainvalid(); break; - case -2: break; // unsupported type; may happen - case -1: break; // weird TLSA record - case 0: break; // no TLSA record given - case 1: case 2: flagtls = 107; flagverify = 3; break; // full certchain available (-PKIX) - case 3: flagtls = 106; flagverify = 0; break; // TA-CA; verify wont work - case 4: flagtls = 105; flagverify = 0; break; // Endpoint only + } + + if (flagverify >= 0) { // TLSA is default + switch (tlsa_check(certs, remotehost, port)) { + case -4: temp_tlsamissing(); break; /* FIXME */ + case -3: temp_tlsainvalid(); break; + case -2: break; // unsupported type; may happen + case -1: break; // weird TLSA record + case 0: break; // no TLSA record given + case 1: + case 2: + flagtls = 107; + flagverify = 3; + break; // full certchain available (-PKIX) + case 3: + flagtls = 106; + flagverify = 0; + break; // TA-CA; verify wont work + case 4: + flagtls = 105; + flagverify = 0; + break; // Endpoint only } } if (flagverify > 0) { - switch (tls_checkpeer(ssl,cert,remotehost,flagtls,flagverify)) { + switch (tls_checkpeer(ssl, cert, remotehost, flagtls, flagverify)) { case -1: temp_tlspeercert(); - case -2: temp_tlspeerverify(); - case -3: temp_tlspeervalid(); - case 1: flagtls = 101; break; - case 2: flagtls = 102; break; - case 3: flagtls = 103; break; + case -2: temp_tlspeerverify(); + case -3: temp_tlspeervalid(); + case 1: flagtls = 101; break; + case 2: flagtls = 102; break; + case 3: flagtls = 103; break; } } @@ -519,13 +531,13 @@ void tls_peercheck() X509_free(cert); X509_free(certs); - + return; } /* this file is too long --------------------------------------- smtp UTF8 */ -int utf8string(unsigned char *ch,int len) +int utf8string(unsigned char *ch, int len) { int i = 0; while (i < len) @@ -541,31 +553,31 @@ int utf8received() char ch; stralloc receivedline = {0}; -/* we consider only our own last written header */ + /* we consider only our own last written header */ for (;;) { - r = buffer_get(&bi,&ch,1); + r = buffer_get(&bi, &ch, 1); if (r == 0) break; if (r == -1) temp_read(); - if (ch == '\r') continue; // DKIM + if (ch == '\r') continue; // DKIM if (ch == '\n') { - if (!stralloc_append(&header,"\r")) temp_nomem(); /* received.c does not add '\r' */ - if (!stralloc_append(&header,"\n")) temp_nomem(); - if (case_starts(receivedline.s,"Date:")) return 0; /* header to quit asap */ - if (case_starts(receivedline.s,"Received: from")) received++; /* found Received header */ + if (!stralloc_append(&header, "\r")) temp_nomem(); /* received.c does not add '\r' */ + if (!stralloc_append(&header, "\n")) temp_nomem(); + if (case_starts(receivedline.s, "Date:")) return 0; /* header to quit asap */ + if (case_starts(receivedline.s, "Received: from")) received++; /* found Received header */ if (received) { - if (case_starts(receivedline.s," by ")) { - for (i = 6; i < receivedline.len - 6; ++i) + if (case_starts(receivedline.s, " by ")) { + for (i = 6; i < receivedline.len - 6; ++i) if (*(receivedline.s + i) == ' ') - if (case_starts(receivedline.s + i + 1,"with UTF8")) return 1; + if (case_starts(receivedline.s + i + 1, "with UTF8")) return 1; return 0; - } + } } - if (!stralloc_copys(&receivedline,"")) temp_nomem(); + if (!stralloc_copys(&receivedline, "")) temp_nomem(); } else { - if (!stralloc_append(&header,&ch)) temp_nomem(); - if (!stralloc_catb(&receivedline,&ch,1)) temp_nomem(); + if (!stralloc_append(&header, &ch)) temp_nomem(); + if (!stralloc_catb(&receivedline, &ch, 1)) temp_nomem(); } } return 0; @@ -581,60 +593,59 @@ int smtp_size() int i; if (smtptext.len > 10) for (i = 0; i < smtptext.len; ++i) { - if (case_starts(smtptext.s + i,"SIZE ")) return 1; - } - return 0;; + if (case_starts(smtptext.s + i, "SIZE ")) return 1; + } + return 0; + ; } void smtp_greeting() { - buffer_puts(&bo,"EHLO "); - buffer_put(&bo,helohost.s,helohost.len); - buffer_puts(&bo,"\r\n"); + buffer_puts(&bo, "EHLO "); + buffer_put(&bo, helohost.s, helohost.len); + buffer_puts(&bo, "\r\n"); buffer_flush(&bo); if (smtpcode() != 250) { - buffer_puts(&bo,"HELO "); - buffer_put(&bo,helohost.s,helohost.len); - buffer_puts(&bo,"\r\n"); + buffer_puts(&bo, "HELO "); + buffer_put(&bo, helohost.s, helohost.len); + buffer_puts(&bo, "\r\n"); buffer_flush(&bo); code = smtpcode(); authsender = 0; - if (code >= 500) quit("DConnected to "," but my name was rejected"); - if (code != 250) quit("ZConnected to "," but my name was rejected"); + if (code >= 500) quit("DConnected to ", " but my name was rejected"); + if (code != 250) quit("ZConnected to ", " but my name was rejected"); } flagsize = smtp_size(); } void smtp_starttls() { - buffer_puts(&bo,"STARTTLS\r\n"); + buffer_puts(&bo, "STARTTLS\r\n"); buffer_flush(&bo); if (smtpcode() == 220) { tls_init(); tls_peercheck(); smtp_greeting(); - } - else { + } else { flagtls = -2; - quit("ZConnected to "," but STARTTLS was rejected"); + quit("ZConnected to ", " but STARTTLS was rejected"); } } void mailfrom() { - buffer_puts(&bo,"MAIL FROM:<"); - buffer_put(&bo,sender.s,sender.len); - buffer_puts(&bo,">"); - if (flagutf8 || utf8received()) - buffer_puts(&bo," SMTPUTF8"); + buffer_puts(&bo, "MAIL FROM:<"); + buffer_put(&bo, sender.s, sender.len); + buffer_puts(&bo, ">"); + if (flagutf8 || utf8received()) buffer_puts(&bo, " SMTPUTF8"); if (flagsize && msize) { - buffer_puts(&bo," SIZE="); - buffer_puts(&bo,msgsize); + buffer_puts(&bo, " SIZE="); + buffer_puts(&bo, msgsize); } - buffer_puts(&bo,"\r\n"); + buffer_puts(&bo, "\r\n"); buffer_flush(&bo); } @@ -653,22 +664,22 @@ stralloc xuser = {0}; static const char hextab[] = "0123456789abcdef"; -int xtext(stralloc *sa,char *s,int len) +int xtext(stralloc *sa, char *s, int len) { int i; unsigned char c; char xch[2]; - if (!stralloc_copys(sa,"")) temp_nomem(); + if (!stralloc_copys(sa, "")) temp_nomem(); for (i = 0; i < len; i++) { c = s[i]; if (c < 33 || c > 126 || c == '=' || c == '+') { xch[0] = hextab[(c >> 4) & 0x0f]; xch[1] = hextab[c & 0x0f]; - if (!stralloc_catb(sa,xch,2)) temp_nomem(); - } else - if (!stralloc_catb(sa,s + i,1)) temp_nomem(); + if (!stralloc_catb(sa, xch, 2)) temp_nomem(); + } else if (!stralloc_catb(sa, s + i, 1)) + temp_nomem(); } return sa->len; @@ -676,73 +687,72 @@ int xtext(stralloc *sa,char *s,int len) void mailfrom_xtext() { - if (!xtext(&xuser,user.s,user.len)) temp_nomem(); - buffer_puts(&bo,"MAIL FROM:<"); - buffer_put(&bo,sender.s,sender.len); - buffer_puts(&bo,"> AUTH="); - buffer_put(&bo,xuser.s,xuser.len); - if (flagutf8 || utf8received()) - buffer_puts(&bo," SMTPUTF8"); + if (!xtext(&xuser, user.s, user.len)) temp_nomem(); + buffer_puts(&bo, "MAIL FROM:<"); + buffer_put(&bo, sender.s, sender.len); + buffer_puts(&bo, "> AUTH="); + buffer_put(&bo, xuser.s, xuser.len); + if (flagutf8 || utf8received()) buffer_puts(&bo, " SMTPUTF8"); if (flagsize && msize) { - buffer_puts(&bo," SIZE="); - buffer_puts(&bo,msgsize); + buffer_puts(&bo, " SIZE="); + buffer_puts(&bo, msgsize); } - buffer_puts(&bo,"\r\n"); + buffer_puts(&bo, "\r\n"); buffer_flush(&bo); } - + int mailfrom_plain() { - buffer_puts(&bo,"AUTH PLAIN\r\n"); + buffer_puts(&bo, "AUTH PLAIN\r\n"); buffer_flush(&bo); - if (smtpcode() != 334) quit("ZConnected to "," but authentication was rejected (AUTH PLAIN)"); + if (smtpcode() != 334) quit("ZConnected to ", " but authentication was rejected (AUTH PLAIN)"); - if (!stralloc_cats(&plain,"")) temp_nomem(); /* RFC 4616 section 2 */ + if (!stralloc_cats(&plain, "")) temp_nomem(); /* RFC 4616 section 2 */ if (!stralloc_0(&plain)) temp_nomem(); - if (!stralloc_cat(&plain,&user)) temp_nomem(); /* user-id */ + if (!stralloc_cat(&plain, &user)) temp_nomem(); /* user-id */ if (!stralloc_0(&plain)) temp_nomem(); - if (!stralloc_cat(&plain,&pass)) temp_nomem(); /* password */ - if (b64encode(&plain,&auth)) quit("ZConnected to "," but unable to base64encode (plain)"); - buffer_put(&bo,auth.s,auth.len); - buffer_puts(&bo,"\r\n"); + if (!stralloc_cat(&plain, &pass)) temp_nomem(); /* password */ + if (b64encode(&plain, &auth)) quit("ZConnected to ", " but unable to base64encode (plain)"); + buffer_put(&bo, auth.s, auth.len); + buffer_puts(&bo, "\r\n"); buffer_flush(&bo); switch (smtpcode()) { case 235: mailfrom_xtext(); break; - case 432: quit("DConnected to "," but password expired"); - case 534: quit("ZConnected to "," but authentication mechamism too weak (plain)"); - default: quit("ZConnected to "," but authentication was rejected (plain)"); + case 432: quit("DConnected to ", " but password expired"); + case 534: quit("ZConnected to ", " but authentication mechamism too weak (plain)"); + default: quit("ZConnected to ", " but authentication was rejected (plain)"); } return 0; } int mailfrom_login() { - buffer_puts(&bo,"AUTH LOGIN\r\n"); + buffer_puts(&bo, "AUTH LOGIN\r\n"); buffer_flush(&bo); - if (smtpcode() != 334) quit("ZConnected to "," but authentication was rejected (AUTH LOGIN)"); - if (!stralloc_copys(&auth,"")) temp_nomem(); - if (b64encode(&user,&auth)) quit("ZConnected to "," but unable to base64encode user"); + if (smtpcode() != 334) quit("ZConnected to ", " but authentication was rejected (AUTH LOGIN)"); + if (!stralloc_copys(&auth, "")) temp_nomem(); + if (b64encode(&user, &auth)) quit("ZConnected to ", " but unable to base64encode user"); - buffer_put(&bo,auth.s,auth.len); - buffer_puts(&bo,"\r\n"); + buffer_put(&bo, auth.s, auth.len); + buffer_puts(&bo, "\r\n"); buffer_flush(&bo); - if (smtpcode() != 334) quit("ZConnected to "," but authentication was rejected (username)"); + if (smtpcode() != 334) quit("ZConnected to ", " but authentication was rejected (username)"); - if (!stralloc_copys(&auth,"")) temp_nomem(); - if (b64encode(&pass,&auth)) quit("ZConnected to "," but unable to base64encode pass"); - buffer_put(&bo,auth.s,auth.len); - buffer_puts(&bo,"\r\n"); + if (!stralloc_copys(&auth, "")) temp_nomem(); + if (b64encode(&pass, &auth)) quit("ZConnected to ", " but unable to base64encode pass"); + buffer_put(&bo, auth.s, auth.len); + buffer_puts(&bo, "\r\n"); buffer_flush(&bo); switch (smtpcode()) { case 235: mailfrom_xtext(); break; - case 432: quit("DConnected to "," but password expired"); - case 534: quit("ZConnected to "," but authentication mechanism is too weak (login)"); - default: quit("ZConnected to "," but authentication was rejected (login)"); + case 432: quit("DConnected to ", " but password expired"); + case 534: quit("ZConnected to ", " but authentication mechanism is too weak (login)"); + default: quit("ZConnected to ", " but authentication was rejected (login)"); } return 0; } @@ -753,41 +763,42 @@ int mailfrom_cram() unsigned char digest[16]; unsigned char digascii[33]; - buffer_puts(&bo,"AUTH CRAM-MD5\r\n"); + buffer_puts(&bo, "AUTH CRAM-MD5\r\n"); buffer_flush(&bo); - if (smtpcode() != 334) quit("ZConnected to "," but authentication was rejected (AUTH CRAM-MD5)"); - if (str_chr(smtptext.s + 4,' ')) { /* Challenge */ - if (!stralloc_copys(&slop,"")) temp_nomem(); - if (!stralloc_copyb(&slop,smtptext.s + 4,smtptext.len - 5)) temp_nomem(); - if (b64decode(slop.s,slop.len,&chal)) quit("ZConnected to "," but unable to base64decode challenge"); + if (smtpcode() != 334) quit("ZConnected to ", " but authentication was rejected (AUTH CRAM-MD5)"); + if (str_chr(smtptext.s + 4, ' ')) { /* Challenge */ + if (!stralloc_copys(&slop, "")) temp_nomem(); + if (!stralloc_copyb(&slop, smtptext.s + 4, smtptext.len - 5)) temp_nomem(); + if (b64decode(slop.s, slop.len, &chal)) + quit("ZConnected to ", " but unable to base64decode challenge"); } - hmac_md5((unsigned char *)chal.s,chal.len,pass.s,pass.len,digest); + hmac_md5((unsigned char *)chal.s, chal.len, pass.s, pass.len, digest); - for (j = 0; j < 16; j++) { /* HEX => ASCII */ + for (j = 0; j < 16; j++) { /* HEX => ASCII */ digascii[2 * j] = hextab[digest[j] >> 4]; digascii[2 * j + 1] = hextab[digest[j] & 0x0f]; } - digascii[32]=0; + digascii[32] = 0; - if (!stralloc_copys(&slop,"")) temp_nomem(); - if (!stralloc_cat(&slop,&user)) temp_nomem(); /* user-id */ - if (!stralloc_cats(&slop," ")) temp_nomem(); - if (!stralloc_catb(&slop,digascii,32)) temp_nomem(); /* digest */ + if (!stralloc_copys(&slop, "")) temp_nomem(); + if (!stralloc_cat(&slop, &user)) temp_nomem(); /* user-id */ + if (!stralloc_cats(&slop, " ")) temp_nomem(); + if (!stralloc_catb(&slop, digascii, 32)) temp_nomem(); /* digest */ - if (!stralloc_copys(&auth,"")) temp_nomem(); - if (b64encode(&slop,&auth)) quit("ZConnected to "," but unable to base64encode username+digest"); + if (!stralloc_copys(&auth, "")) temp_nomem(); + if (b64encode(&slop, &auth)) quit("ZConnected to ", " but unable to base64encode username+digest"); - buffer_put(&bo,auth.s,auth.len); - buffer_puts(&bo,"\r\n"); + buffer_put(&bo, auth.s, auth.len); + buffer_puts(&bo, "\r\n"); buffer_flush(&bo); switch (smtpcode()) { case 235: mailfrom_xtext(); break; - case 432: quit("DConnected to "," but password expired"); - case 534: quit("ZConnected to "," but authentication mechamism too weak (cram)"); - default: quit("ZConnected to "," but authentication was rejected (cram)"); + case 432: quit("DConnected to ", " but password expired"); + case 534: quit("ZConnected to ", " but authentication mechamism too weak (cram)"); + default: quit("ZConnected to ", " but authentication was rejected (cram)"); } return 0; } @@ -796,13 +807,13 @@ void smtp_auth() { int i; - if (smtptext.len > 8) + if (smtptext.len > 8) for (i = 4; i < smtptext.len - 5; ++i) { - if (case_starts(smtptext.s + i,"CRAM")) + if (case_starts(smtptext.s + i, "CRAM")) if (mailfrom_cram() >= 0) return; - if (case_starts(smtptext.s + i,"LOGIN")) + if (case_starts(smtptext.s + i, "LOGIN")) if (mailfrom_login() >= 0) return; - if (case_starts(smtptext.s + i,"PLAIN")) + if (case_starts(smtptext.s + i, "PLAIN")) if (mailfrom_plain() >= 0) return; } err_authprot(); @@ -816,87 +827,105 @@ void smtp() int flagbother; int i; - if (flagtls > 10 && flagtls < 20) { /* SMTPS */ + if (flagtls > 10 && flagtls < 20) { /* SMTPS */ tls_init(); - tls_peercheck(); + tls_peercheck(); } code = smtpcode(); - if (code >= 500) quit("DConnected to "," but sender was rejected"); - if (code == 421 || code == 450) quit("ZConnected to "," but probably greylisted"); /* RFC 6647 */ - if (code >= 400) quit("ZConnected to "," but sender was rejected"); - if (code != 220) quit("ZConnected to "," but greeting failed"); + if (code >= 500) quit("DConnected to ", " but sender was rejected"); + if (code == 421 || code == 450) quit("ZConnected to ", " but probably greylisted"); /* RFC 6647 */ + if (code >= 400) quit("ZConnected to ", " but sender was rejected"); + if (code != 220) quit("ZConnected to ", " but greeting failed"); smtp_greeting(); - if (flagtls > 0 && flagtls < 10) { /* STARTTLS */ - if (starttls_peer()) - smtp_starttls(); + if (flagtls > 0 && flagtls < 10) { /* STARTTLS */ + if (starttls_peer()) + smtp_starttls(); else if (flagtls > 3 && flagtls != 9) { - if (!stralloc_0(&host)) temp_nomem(); - temp_tlshost(); + if (!stralloc_0(&host)) temp_nomem(); + temp_tlshost(); } } - if (user.len && pass.len) /* AUTH */ + if (user.len && pass.len) /* AUTH */ smtp_auth(); - else - mailfrom(); /* Mail From */ + else + mailfrom(); /* Mail From */ code = smtpcode(); - if (code >= 500) quit("DConnected to "," but sender was rejected"); - if (code >= 400) quit("ZConnected to "," but sender was probably greylisted"); + if (code >= 500) quit("DConnected to ", " but sender was rejected"); + if (code >= 400) quit("ZConnected to ", " but sender was probably greylisted"); - flagbother = 0; /* Rcpt To */ + flagbother = 0; /* Rcpt To */ for (i = 0; i < reciplist.len; ++i) { - buffer_puts(&bo,"RCPT TO:<"); - buffer_put(&bo,reciplist.sa[i].s,reciplist.sa[i].len); - buffer_puts(&bo,">\r\n"); + buffer_puts(&bo, "RCPT TO:<"); + buffer_put(&bo, reciplist.sa[i].s, reciplist.sa[i].len); + buffer_puts(&bo, ">\r\n"); buffer_flush(&bo); - code = smtpcode(); /* Data */ + code = smtpcode(); /* Data */ if (flagsize) { - if (code == 552) quit("DConnected to "," but message size is too large"); - if (code == 452) quit("ZConnected to "," however insufficient storage space available"); - } + if (code == 552) quit("DConnected to ", " but message size is too large"); + if (code == 452) quit("ZConnected to ", " however insufficient storage space available"); + } if (code == 421 || code == 450) { // Postfix merde ;-) - out("s"); outhost(); out(" sender is greylisting.\n"); - outsmtptext(); zero(); + out("s"); + outhost(); + out(" sender is greylisting.\n"); + outsmtptext(); + zero(); } else if (code >= 500) { - out("h"); outhost(); out(" does not like recipient.\n"); - outsmtptext(); zero(); + out("h"); + outhost(); + out(" does not like recipient.\n"); + outsmtptext(); + zero(); } else if (code >= 400) { - out("s"); outhost(); out(" does not like recipient.\n"); - outsmtptext(); zero(); + out("s"); + outhost(); + out(" does not like recipient.\n"); + outsmtptext(); + zero(); } else { - out("r"); zero(); + out("r"); + zero(); flagbother = 1; } } - if (!flagbother) quit("DGiving up on ",""); + if (!flagbother) quit("DGiving up on ", ""); - buffer_putsflush(&bo,"DATA\r\n"); + buffer_putsflush(&bo, "DATA\r\n"); code = smtpcode(); - if (code >= 500) quit("D"," failed on DATA command"); - if (code >= 400) quit("Z"," failed on DATA command"); + if (code >= 500) quit("D", " failed on DATA command"); + if (code >= 400) quit("Z", " failed on DATA command"); + + buffer_putflush(&bo, header.s, header.len); - buffer_putflush(&bo,header.s,header.len); - blast(); code = smtpcode(); flagcritical = 0; - if (code >= 500) quit("D"," failed after I sent the message"); - if (code >= 400) quit("Z"," failed after I sent the message"); + if (code >= 500) quit("D", " failed after I sent the message"); + if (code >= 400) quit("Z", " failed after I sent the message"); switch (flagtls) { // StartTLS + SMTPS - case 100: case 110: quit("K"," TLS transmitted message accepted"); break; - case 101: case 111: quit("K"," TLS (verified CA) transmitted message accepted"); break; - case 102: case 112: quit("K"," TLS (validated CA+DN*) transmitted message accepted"); break; - case 103: case 113: quit("K"," TLS (validated CA+DN) transmitted message accepted"); break; - case 104: case 114: quit("K"," TLS (CERT pinning) transmitted message accepted"); break; - case 105: case 115: quit("K"," TLS (TLSA EE validated) transmitted message accepted"); break; - case 106: case 116: quit("K"," TLS (TLSA TA validated) transmitted message accepted"); break; - case 107: case 117: quit("K"," TLS (TLSA PKIX verified) transmitted message accepted"); break; - default: quit("K"," accepted message"); break; + case 100: + case 110: quit("K", " TLS transmitted message accepted"); break; + case 101: + case 111: quit("K", " TLS (verified CA) transmitted message accepted"); break; + case 102: + case 112: quit("K", " TLS (validated CA+DN*) transmitted message accepted"); break; + case 103: + case 113: quit("K", " TLS (validated CA+DN) transmitted message accepted"); break; + case 104: + case 114: quit("K", " TLS (CERT pinning) transmitted message accepted"); break; + case 105: + case 115: quit("K", " TLS (TLSA EE validated) transmitted message accepted"); break; + case 106: + case 116: quit("K", " TLS (TLSA TA validated) transmitted message accepted"); break; + case 107: + case 117: quit("K", " TLS (TLSA PKIX verified) transmitted message accepted"); break; + default: quit("K", " accepted message"); break; } } @@ -914,44 +943,44 @@ void qmtp() char num[FMT_ULONG]; int flagallok; - if (qmtpsend == 2) { /* QMTPS */ + if (qmtpsend == 2) { /* QMTPS */ tls_init(); tls_peercheck(); } -/* the following code was substantially taken from serialmail's serialqmtp.c */ + /* the following code was substantially taken from serialmail's serialqmtp.c */ - scan_ulong(msgsize,&len); - buffer_put(&bo,num,fmt_ulong(num,len + 1)); - buffer_put(&bo,":\n",2); + scan_ulong(msgsize, &len); + buffer_put(&bo, num, fmt_ulong(num, len + 1)); + buffer_put(&bo, ":\n", 2); while (len > 0) { n = buffer_feed(&bi); - if (n <= 0) _exit(1); /* wise guy again */ + if (n <= 0) _exit(1); /* wise guy again */ x = buffer_PEEK(&bi); - buffer_put(&bo,x,n); - buffer_SEEK(&bi,n); + buffer_put(&bo, x, n); + buffer_SEEK(&bi, n); len -= n; } - buffer_put(&bo,",",1); + buffer_put(&bo, ",", 1); len = sender.len; - buffer_put(&bo,num,fmt_ulong(num,len)); - buffer_put(&bo,":",1); - buffer_put(&bo,sender.s,sender.len); - buffer_put(&bo,",",1); + buffer_put(&bo, num, fmt_ulong(num, len)); + buffer_put(&bo, ":", 1); + buffer_put(&bo, sender.s, sender.len); + buffer_put(&bo, ",", 1); len = 0; for (i = 0; i < reciplist.len; ++i) - len += fmt_ulong(num,reciplist.sa[i].len) + 1 + reciplist.sa[i].len + 1; - buffer_put(&bo,num,fmt_ulong(num,len)); - buffer_put(&bo,":",1); + len += fmt_ulong(num, reciplist.sa[i].len) + 1 + reciplist.sa[i].len + 1; + buffer_put(&bo, num, fmt_ulong(num, len)); + buffer_put(&bo, ":", 1); for (i = 0; i < reciplist.len; ++i) { - buffer_put(&bo,num,fmt_ulong(num,reciplist.sa[i].len)); - buffer_put(&bo,":",1); - buffer_put(&bo,reciplist.sa[i].s,reciplist.sa[i].len); - buffer_put(&bo,",",1); + buffer_put(&bo, num, fmt_ulong(num, reciplist.sa[i].len)); + buffer_put(&bo, ":", 1); + buffer_put(&bo, reciplist.sa[i].s, reciplist.sa[i].len); + buffer_put(&bo, ",", 1); } - buffer_put(&bo,",",1); + buffer_put(&bo, ",", 1); buffer_flush(&bo); flagallok = 1; @@ -966,14 +995,15 @@ void qmtp() len = 10 * len + (ch - '0'); } if (!len) temp_proto(); - get(&ch); --len; + get(&ch); + --len; if ((ch != 'Z') && (ch != 'D') && (ch != 'K')) temp_proto(); - if (!stralloc_copyb(&smtptext,&ch,1)) temp_proto(); + if (!stralloc_copyb(&smtptext, &ch, 1)) temp_proto(); if (flagtls == 100) { - if (!stralloc_cats(&smtptext,"qmtps:")) temp_nomem(); + if (!stralloc_cats(&smtptext, "qmtps:")) temp_nomem(); } else { - if (!stralloc_cats(&smtptext,"qmtp:")) temp_nomem(); + if (!stralloc_cats(&smtptext, "qmtp:")) temp_nomem(); } while (len > 0) { @@ -989,22 +1019,26 @@ void qmtp() if (ch != ',') temp_proto(); smtptext.s[smtptext.len - 1] = '\n'; - if (smtptext.s[0] == 'K') out("r"); + if (smtptext.s[0] == 'K') + out("r"); else if (smtptext.s[0] == 'D') { out("h"); flagallok = 0; - } - else { /* if (smtptext.s[0] == 'Z') */ + } else { /* if (smtptext.s[0] == 'Z') */ out("s"); flagallok = 0; } - if (buffer_put(&bs,smtptext.s + 1,smtptext.len - 1) == -1) temp_qmtpnoc(); + if (buffer_put(&bs, smtptext.s + 1, smtptext.len - 1) == -1) temp_qmtpnoc(); zero(); } if (!flagallok) { - out("DGiving up on "); outhost(); out("\n"); + out("DGiving up on "); + outhost(); + out("\n"); } else { - out("KAll received okay by "); outhost(); out("\n"); + out("KAll received okay by "); + outhost(); + out("\n"); } zerodie(); } @@ -1013,86 +1047,108 @@ void qmtp() /* host has to be canonical [A/AAAA record], box has to be quoted */ -void addrmangle(stralloc *saout,char *address,int *flagalias,int flagcname) +void addrmangle(stralloc *saout, char *address, int *flagalias, int flagcname) { int at; int r = 0; stralloc cn = {0}; - *flagalias = flagcname; /* saout + flagalias are output */ - if (!flagutf8) - flagutf8 = utf8string(address,str_len(address)); + *flagalias = flagcname; /* saout + flagalias are output */ + if (!flagutf8) flagutf8 = utf8string(address, str_len(address)); - at = str_rchr(address,'@'); + at = str_rchr(address, '@'); if (!address[at]) { - if (!stralloc_copys(saout,address)) temp_nomem(); + if (!stralloc_copys(saout, address)) temp_nomem(); return; } - if (!stralloc_copys(&canonbox,address)) temp_nomem(); + if (!stralloc_copys(&canonbox, address)) temp_nomem(); canonbox.len = at; - if (!quote(saout,&canonbox)) temp_nomem(); /* saout = 'inbox' name without quotes ;-) */ - if (!stralloc_cats(saout,"@")) temp_nomem(); + if (!quote(saout, &canonbox)) temp_nomem(); /* saout = 'inbox' name without quotes ;-) */ + if (!stralloc_cats(saout, "@")) temp_nomem(); - if (!stralloc_copys(&canonhost,address + at + 1)) temp_nomem(); - if (flagcname) { /* no relayhost */ + if (!stralloc_copys(&canonhost, address + at + 1)) temp_nomem(); + if (flagcname) { /* no relayhost */ DNS_INIT - switch ((r = dns_cname(&cn,&canonhost))) { - case DNS_MEM: temp_nomem(); + switch ((r = dns_cname(&cn, &canonhost))) { + case DNS_MEM: temp_nomem(); case DNS_SOFT: temp_dnscanon(); case DNS_HARD: ; /* alias loop, not our problem */ - default: if (r > 0) *flagalias = 0; + default: + if (r > 0) *flagalias = 0; } } - if (!stralloc_cat(saout,&canonhost)) temp_nomem(); + if (!stralloc_cat(saout, &canonhost)) temp_nomem(); } void getcontrols() { if (control_init() == -1) temp_control(); - if (control_readint(&timeout,"control/timeoutremote") == -1) temp_control(); - if (control_readint(&timeoutconnect,"control/timeoutconnect") == -1) - temp_control(); - if (control_rldef(&helohost,"control/helohost",1,(char *) 0) != 1) - temp_control(); - switch (control_readfile(&smtproutes,"control/smtproutes",0)) { + if (control_readint(&timeout, "control/timeoutremote") == -1) temp_control(); + if (control_readint(&timeoutconnect, "control/timeoutconnect") == -1) temp_control(); + if (control_rldef(&helohost, "control/helohost", 1, (char *)0) != 1) temp_control(); + switch (control_readfile(&smtproutes, "control/smtproutes", 0)) { case -1: temp_control(); - case 0: if (!constmap_init(&mapsmtproutes,"",0,1)) temp_nomem(); break; - case 1: if (!constmap_init(&mapsmtproutes,smtproutes.s,smtproutes.len,1)) temp_nomem(); break; + case 0: + if (!constmap_init(&mapsmtproutes, "", 0, 1)) temp_nomem(); + break; + case 1: + if (!constmap_init(&mapsmtproutes, smtproutes.s, smtproutes.len, 1)) temp_nomem(); + break; } - switch (control_readfile(&domainips,"control/domainips",0)) { + switch (control_readfile(&domainips, "control/domainips", 0)) { case -1: temp_control(); - case 0: if (!constmap_init(&mapdomainips,"",0,1)) temp_nomem(); break; - case 1: if (!constmap_init(&mapdomainips,domainips.s,domainips.len,1)) temp_nomem(); break; + case 0: + if (!constmap_init(&mapdomainips, "", 0, 1)) temp_nomem(); + break; + case 1: + if (!constmap_init(&mapdomainips, domainips.s, domainips.len, 1)) temp_nomem(); + break; } - switch (control_readfile(&authsenders,"control/authsenders",0)) { + switch (control_readfile(&authsenders, "control/authsenders", 0)) { case -1: temp_control(); - case 0: if (!constmap_init(&mapauthsenders,"",0,1)) temp_nomem(); break; - case 1: if (!constmap_init(&mapauthsenders,authsenders.s,authsenders.len,1)) temp_nomem(); break; + case 0: + if (!constmap_init(&mapauthsenders, "", 0, 1)) temp_nomem(); + break; + case 1: + if (!constmap_init(&mapauthsenders, authsenders.s, authsenders.len, 1)) temp_nomem(); + break; } - switch (control_readfile(&qmtproutes,"control/qmtproutes",0)) { + switch (control_readfile(&qmtproutes, "control/qmtproutes", 0)) { case -1: temp_control(); - case 0: if (!constmap_init(&mapqmtproutes,"",0,1)) temp_nomem(); break; - case 1: if (!constmap_init(&mapqmtproutes,qmtproutes.s,qmtproutes.len,1)) temp_nomem(); break; + case 0: + if (!constmap_init(&mapqmtproutes, "", 0, 1)) temp_nomem(); + break; + case 1: + if (!constmap_init(&mapqmtproutes, qmtproutes.s, qmtproutes.len, 1)) temp_nomem(); + break; } - switch (control_readfile(&domaincerts,"control/domaincerts",0)) { + switch (control_readfile(&domaincerts, "control/domaincerts", 0)) { case -1: temp_control(); - case 0: if (!constmap_init(&mapdomaincerts,"",0,1)) temp_nomem(); break; - case 1: if (!constmap_init(&mapdomaincerts,domaincerts.s,domaincerts.len,1)) temp_nomem(); break; + case 0: + if (!constmap_init(&mapdomaincerts, "", 0, 1)) temp_nomem(); + break; + case 1: + if (!constmap_init(&mapdomaincerts, domaincerts.s, domaincerts.len, 1)) temp_nomem(); + break; } - switch (control_readfile(&tlsdestinations,"control/tlsdestinations",0)) { + switch (control_readfile(&tlsdestinations, "control/tlsdestinations", 0)) { case -1: temp_control(); - case 0: if (!constmap_init(&maptlsdestinations,"",0,1)) temp_nomem(); break; - case 1: if (!constmap_init(&maptlsdestinations,tlsdestinations.s,tlsdestinations.len,1)) temp_nomem(); break; + case 0: + if (!constmap_init(&maptlsdestinations, "", 0, 1)) temp_nomem(); + break; + case 1: + if (!constmap_init(&maptlsdestinations, tlsdestinations.s, tlsdestinations.len, 1)) temp_nomem(); + break; } } -int main(int argc,char **argv) +int main(int argc, char **argv) { static ipalloc ip = {0}; stralloc netif = {0}; struct stat st; - int i, j, k; + int i, j, k; int p; /* reserved for port */ int r; /* reserved for return code */ unsigned long random; @@ -1109,184 +1165,191 @@ int main(int argc,char **argv) if (chdir(auto_qmail) == -1) temp_chdir(); getcontrols(); - if (!stralloc_copys(&host,argv[1])) temp_nomem(); + if (!stralloc_copys(&host, argv[1])) temp_nomem(); authsender = 0; relayhost = 0; - addrmangle(&sender,argv[2],&flagalias,0); + addrmangle(&sender, argv[2], &flagalias, 0); if (sender.len > 1) { - i = str_chr(sender.s,'@'); + i = str_chr(sender.s, '@'); if (sender.s[i] == '@') - if (!stralloc_copyb(&senddomain,sender.s + i + 1,sender.len - i - 1)) temp_nomem(); // un-terminated + if (!stralloc_copyb(&senddomain, sender.s + i + 1, sender.len - i - 1)) + temp_nomem(); // un-terminated } -/* this file is too long -------------------------------------- set domain ip + helohost */ + /* this file is too long -------------------------------------- set domain ip + helohost */ localip = 0; - + for (i = 0; i <= senddomain.len; ++i) - if ((i == 0) || (senddomain.s[i] == '.')) - if ((localip = constmap(&mapdomainips,senddomain.s + i,senddomain.len - i))) - break; + if ((i == 0) || (senddomain.s[i] == '.')) + if ((localip = constmap(&mapdomainips, senddomain.s + i, senddomain.len - i))) break; - if (!localip) - localip = constmap(&mapdomainips,"*",1); /* one for all */ + if (!localip) localip = constmap(&mapdomainips, "*", 1); /* one for all */ if (localip) { - j = str_chr(localip,'%'); + j = str_chr(localip, '%'); if (localip[j] != '%') j = 0; - k = str_chr(localip,'|'); + k = str_chr(localip, '|'); if (localip[k] != '|') k = 0; - if (k) { /* helohost */ - if (!stralloc_copys(&helohost,localip + k + 1)) temp_nomem(); + if (k) { /* helohost */ + if (!stralloc_copys(&helohost, localip + k + 1)) temp_nomem(); if (!stralloc_0(&helohost)) temp_nomem(); localip[k] = 0; } - if (j) { /* IF index */ + if (j) { /* IF index */ localip[j] = 0; - if (!stralloc_copys(&netif,localip + j + 1)) temp_nomem(); + if (!stralloc_copys(&netif, localip + j + 1)) temp_nomem(); if (!stralloc_0(&netif)) temp_nomem(); } } -/* this file is too long -------------------------------------- authsender routes */ + /* this file is too long -------------------------------------- authsender routes */ for (i = 0; i <= sender.len; ++i) if ((i == 0) || (i == sender.len) || (sender.s[i] == '.') || (sender.s[i] == '@')) - if ((authsender = constmap(&mapauthsenders,sender.s + i,sender.len - i))) - break; + if ((authsender = constmap(&mapauthsenders, sender.s + i, sender.len - i))) break; if (authsender && !*authsender) authsender = 0; if (authsender) { - i = str_chr(authsender,'|'); + i = str_chr(authsender, '|'); if (authsender[i] == '|') { - j = str_chr(authsender + i + 1,'|'); + j = str_chr(authsender + i + 1, '|'); if (authsender[i + j + 1] == '|') { authsender[i] = 0; authsender[i + j + 1] = 0; - if (!stralloc_copys(&user,"")) temp_nomem(); - if (!stralloc_copys(&user,authsender + i + 1)) temp_nomem(); - if (!stralloc_copys(&pass,"")) temp_nomem(); - if (!stralloc_copys(&pass,authsender + i + j + 2)) temp_nomem(); + if (!stralloc_copys(&user, "")) temp_nomem(); + if (!stralloc_copys(&user, authsender + i + 1)) temp_nomem(); + if (!stralloc_copys(&pass, "")) temp_nomem(); + if (!stralloc_copys(&pass, authsender + i + j + 2)) temp_nomem(); } } - p = str_chr(authsender,';'); + p = str_chr(authsender, ';'); if (authsender[p] == ';') { - if (authsender[p + 1] == 's') { flagsmtps = 1, p++; } - scan_ulong(authsender + p + 1,&port); + if (authsender[p + 1] == 's') { + flagsmtps = 1, p++; + } + scan_ulong(authsender + p + 1, &port); authsender[p] = 0; } relayhost = authsender; - if (!stralloc_copys(&host,authsender)) temp_nomem(); + if (!stralloc_copys(&host, authsender)) temp_nomem(); } -/* this file is too long -------------------------------------- standard routes */ + /* this file is too long -------------------------------------- standard routes */ if (!authsender) { - if (sender.len == 0) { /* bounce routes */ - if ((relayhost = constmap(&mapqmtproutes,"!@",2))) { - qmtpsend = 1; port = PORT_QMTP; + if (sender.len == 0) { /* bounce routes */ + if ((relayhost = constmap(&mapqmtproutes, "!@", 2))) { + qmtpsend = 1; + port = PORT_QMTP; } else - relayhost = constmap(&mapsmtproutes,"!@",2); + relayhost = constmap(&mapsmtproutes, "!@", 2); } if (relayhost && !*relayhost) relayhost = 0; if (!relayhost) { - for (i = 0; i <= host.len; ++i) { /* qmtproutes */ + for (i = 0; i <= host.len; ++i) { /* qmtproutes */ if ((i == 0) || (i == host.len) || (host.s[i] == '.')) - if ((relayhost = constmap(&mapqmtproutes,host.s + i,host.len - i))) { - qmtpsend = 1; port = PORT_QMTP; - break; - } /* default smtproutes */ - if ((relayhost = constmap(&mapsmtproutes,host.s + i,host.len - i))) + if ((relayhost = constmap(&mapqmtproutes, host.s + i, host.len - i))) { + qmtpsend = 1; + port = PORT_QMTP; break; + } /* default smtproutes */ + if ((relayhost = constmap(&mapsmtproutes, host.s + i, host.len - i))) break; } } if (relayhost && !*relayhost) relayhost = 0; - if (relayhost) { /* default smtproutes -- authenticated */ - i = str_chr(relayhost,'|'); + if (relayhost) { /* default smtproutes -- authenticated */ + i = str_chr(relayhost, '|'); if (relayhost[i] == '|') { - j = str_chr(relayhost + i + 1,'|'); // authenticate + j = str_chr(relayhost + i + 1, '|'); // authenticate if (relayhost[i + j + 1] == '|') { relayhost[i] = 0; relayhost[i + j + 1] = 0; - if (!stralloc_copys(&user,"")) temp_nomem(); - if (!stralloc_copys(&user,relayhost + i + 1)) temp_nomem(); - if (!stralloc_copys(&pass,"")) temp_nomem(); - k = str_chr(relayhost + i + j + 2,'|'); // local ip + if (!stralloc_copys(&user, "")) temp_nomem(); + if (!stralloc_copys(&user, relayhost + i + 1)) temp_nomem(); + if (!stralloc_copys(&pass, "")) temp_nomem(); + k = str_chr(relayhost + i + j + 2, '|'); // local ip if (relayhost[i + j + k + 2] == '|') { relayhost[i + j + k + 2] = 0; localip = relayhost + i + j + k + 3; } - if (!stralloc_copys(&pass,relayhost + i + j + 2)) temp_nomem(); + if (!stralloc_copys(&pass, relayhost + i + j + 2)) temp_nomem(); } } - p = str_chr(relayhost,';'); + p = str_chr(relayhost, ';'); if (relayhost[p] == ';') { - if (relayhost[p + 1] == 's') { flagsmtps = 1; p++; } // RFC 8314 - scan_ulong(relayhost + p + 1,&port); + if (relayhost[p + 1] == 's') { + flagsmtps = 1; + p++; + } // RFC 8314 + scan_ulong(relayhost + p + 1, &port); if (qmtpsend && port == PORT_QMTPS) qmtpsend = 2; relayhost[p] = 0; } - if (!stralloc_copys(&host,relayhost)) temp_nomem(); + if (!stralloc_copys(&host, relayhost)) temp_nomem(); #ifdef IDN2 } else { char *asciihost = 0; if (!stralloc_0(&host)) temp_nomem(); - switch (idn2_lookup_u8(host.s,(uint8_t**)&asciihost,IDN2_NFC_INPUT)) { + switch (idn2_lookup_u8(host.s, (uint8_t **)&asciihost, IDN2_NFC_INPUT)) { case IDN2_OK: break; case IDN2_MALLOC: temp_nomem(); default: perm_dns(); } - if (!stralloc_copys(&idnhost,asciihost)) temp_nomem(); + if (!stralloc_copys(&idnhost, asciihost)) temp_nomem(); #endif } } -/* this file is too long -------------------------------------- TLS destinations */ + /* this file is too long -------------------------------------- TLS destinations */ - flagtls = tls_destination((const stralloc) host); // host may not be 0-terminated + flagtls = tls_destination((const stralloc)host); // host may not be 0-terminated if (flagtls > 0) { - if (tlsdestinfo) { - i = str_chr(tlsdestinfo,'|'); /* ca file/dir or cert fingerprint */ + if (tlsdestinfo) { + i = str_chr(tlsdestinfo, '|'); /* ca file/dir or cert fingerprint */ if (tlsdestinfo[i] == '|') { tlsdestinfo[i] = 0; - j = str_chr(tlsdestinfo + i + 1,'|'); /* cipher */ + j = str_chr(tlsdestinfo + i + 1, '|'); /* cipher */ if (tlsdestinfo[i + j + 1] == '|') { - tlsdestinfo[i + j + 1] = 0; - k = str_chr(tlsdestinfo + i + j + 2,'|'); /* cone domain */ + tlsdestinfo[i + j + 1] = 0; + k = str_chr(tlsdestinfo + i + j + 2, '|'); /* cone domain */ if (tlsdestinfo[i + j + k + 2] == '|') { - tlsdestinfo[i + j + k + 2] = 0; - if (str_diffn(tlsdestinfo + j + k + 3,canonhost.s,canonhost.len)) flagtls = 0; + tlsdestinfo[i + j + k + 2] = 0; + if (str_diffn(tlsdestinfo + j + k + 3, canonhost.s, canonhost.len)) flagtls = 0; } - p = str_chr(tlsdestinfo + i + j + 2,';'); /* verifydepth;port */ + p = str_chr(tlsdestinfo + i + j + 2, ';'); /* verifydepth;port */ if (tlsdestinfo[i + j + p + 2] == ';') { tlsdestinfo[i + j + p + 2] = 0; - if (p > 0) scan_ulong(tlsdestinfo + i + j + 2,&verifydepth); - if (tlsdestinfo[i + j + p + 3] == 's') { flagsmtps = 1; p++; } /* RFC 8314 */ - scan_ulong(tlsdestinfo + i + j + p + 3,&port); + if (p > 0) scan_ulong(tlsdestinfo + i + j + 2, &verifydepth); + if (tlsdestinfo[i + j + p + 3] == 's') { + flagsmtps = 1; + p++; + } /* RFC 8314 */ + scan_ulong(tlsdestinfo + i + j + p + 3, &port); } } if (j) - if (!stralloc_copys(&ciphers,tlsdestinfo + i + 1)) temp_nomem(); - } + if (!stralloc_copys(&ciphers, tlsdestinfo + i + 1)) temp_nomem(); + } /* either ':[=]cafile/cadir' -or- ':;port' */ - if (tlsdestinfo[0] == ';') - scan_ulong(tlsdestinfo + 1,&port); - else - if (!stralloc_copys(&cafile,tlsdestinfo)) temp_nomem(); + if (tlsdestinfo[0] == ';') + scan_ulong(tlsdestinfo + 1, &port); + else if (!stralloc_copys(&cafile, tlsdestinfo)) + temp_nomem(); } -/* cafile starts with '=' => it is a fingerprint + /* cafile starts with '=' => it is a fingerprint cafile ends with '/' => consider it as cadir cafile and cadir are now 0-terminated ciphers are alway 0-terminated if given */ @@ -1296,7 +1359,7 @@ int main(int argc,char **argv) if (cafile.s[cafile.len] == '/') { cafile.len = 0; flagverify = 2; - if (!stralloc_copys(&cadir,tlsdestinfo)) temp_nomem(); + if (!stralloc_copys(&cadir, tlsdestinfo)) temp_nomem(); if (!stralloc_0(&cadir)) temp_nomem(); } else { if (cafile.s[0] == '=') flagverify = -2; @@ -1305,60 +1368,61 @@ int main(int argc,char **argv) } else cafile.len = cadir.len = 0; - if (ciphers.len > 4) /* otherwise garbage */ - if (!stralloc_0(&ciphers)) temp_nomem(); - else - ciphers.len = 0; + if (ciphers.len > 4) /* otherwise garbage */ + if (!stralloc_0(&ciphers)) + temp_nomem(); + else + ciphers.len = 0; if (port == PORT_SMTPS || flagsmtps) flagtls += 10; if (port == PORT_QMTPS) flagtls += 20; } - if (flagtls == 8) flagverify = -1; - if (!flagtls && qmtpsend == 2) flagtls = 20; /* QMTPS */ + if (flagtls == 8) flagverify = -1; + if (!flagtls && qmtpsend == 2) flagtls = 20; /* QMTPS */ -/* this file is too long -------------------------------------- Our Certs - per senddomain */ + /* this file is too long -------------------------------------- Our Certs - per senddomain */ if (flagtls > 0) { - flagtlsdomain = tls_domaincerts((const stralloc) senddomain); // senddomain un-terminated + flagtlsdomain = tls_domaincerts((const stralloc)senddomain); // senddomain un-terminated if (flagtlsdomain && tlsdomaininfo) { - i = str_chr(tlsdomaininfo,'|'); + i = str_chr(tlsdomaininfo, '|'); if (tlsdomaininfo[i] == '|') { tlsdomaininfo[i] = 0; - j = str_chr(tlsdomaininfo + i + 1,'|'); + j = str_chr(tlsdomaininfo + i + 1, '|'); if (tlsdomaininfo[i + j + 1] == '|') { tlsdomaininfo[i + j + 1] = 0; - if (!stralloc_copys(&keypwd,"")) temp_nomem(); - if (!stralloc_copys(&keypwd,tlsdomaininfo + i + j + 2)) temp_nomem(); + if (!stralloc_copys(&keypwd, "")) temp_nomem(); + if (!stralloc_copys(&keypwd, tlsdomaininfo + i + j + 2)) temp_nomem(); if (!stralloc_0(&keypwd)) temp_nomem(); } - if (!stralloc_copys(&keyfile,tlsdomaininfo + i + 1)) temp_nomem(); + if (!stralloc_copys(&keyfile, tlsdomaininfo + i + 1)) temp_nomem(); if (!stralloc_0(&keyfile)) temp_nomem(); } - if (!stralloc_copys(&certfile,tlsdomaininfo)) temp_nomem(); + if (!stralloc_copys(&certfile, tlsdomaininfo)) temp_nomem(); if (!stralloc_0(&certfile)) temp_nomem(); flagtlsdomain = 2; } } -/* this file is too long -------------------------------------- work thru reciplist */ + /* this file is too long -------------------------------------- work thru reciplist */ - if (!saa_readyplus(&reciplist,0)) temp_nomem(); + if (!saa_readyplus(&reciplist, 0)) temp_nomem(); if (ipme_init() != 1) temp_oserr(); - + flagallaliases = 1; recips = argv + 3; - if (fstat(0,&st) == -1) quit("Z", " unable to fstat stdin"); + if (fstat(0, &st) == -1) quit("Z", " unable to fstat stdin"); msize = st.st_size; - fmt_ulong(msgsize,msize); + fmt_ulong(msgsize, msize); while (*recips) { - if (!saa_readyplus(&reciplist,1)) temp_nomem(); + if (!saa_readyplus(&reciplist, 1)) temp_nomem(); reciplist.sa[reciplist.len] = sauninit; - addrmangle(reciplist.sa + reciplist.len,*recips,&flagalias,!relayhost); + addrmangle(reciplist.sa + reciplist.len, *recips, &flagalias, !relayhost); if (!flagalias) flagallaliases = 0; ++reciplist.len; ++recips; @@ -1366,91 +1430,94 @@ int main(int argc,char **argv) random = now() + (getpid() << 16); #ifdef IDN2 - switch (relayhost ? dns_ip(&ip,&host) : dns_mxip(&ip,&idnhost,random)) { + switch (relayhost ? dns_ip(&ip, &host) : dns_mxip(&ip, &idnhost, random)) { #else - switch (relayhost ? dns_ip(&ip,&host) : dns_mxip(&ip,&host,random)) { + switch (relayhost ? dns_ip(&ip, &host) : dns_mxip(&ip, &host, random)) { #endif - case DNS_MEM: temp_nomem(); - case DNS_ERR: temp_dns(); - case DNS_COM: temp_dns(); + case DNS_MEM: temp_nomem(); + case DNS_ERR: temp_dns(); + case DNS_COM: temp_dns(); case DNS_SOFT: temp_dns(); #ifdef DEFERREDBOUNCES - default: if (!ip.len) temp_nomx(); + default: + if (!ip.len) temp_nomx(); #else - default: if (!ip.len) perm_nomx(); + default: + if (!ip.len) perm_nomx(); #endif } prefme = 100000; for (i = 0; i < ip.len; ++i) if (ipme_is(&ip.ix[i])) - if (ip.ix[i].pref < prefme) - prefme = ip.ix[i].pref; - + if (ip.ix[i].pref < prefme) prefme = ip.ix[i].pref; + if (relayhost) prefme = 300000; if (flagallaliases) prefme = 500000; - + if (localip) { - i = str_chr(localip,':'); - if (localip[i] == ':') ip6flag = 1; - else ip6flag = -1; + i = str_chr(localip, ':'); + if (localip[i] == ':') + ip6flag = 1; + else + ip6flag = -1; } - for (i = 0; i < ip.len; ++i) { /* MX with smallest distance */ - if (ip6flag == -1 && ip.ix[i].af == AF_INET6) continue; - if (ip6flag == 1 && ip.ix[i].af == AF_INET) continue; + for (i = 0; i < ip.len; ++i) { /* MX with smallest distance */ + if (ip6flag == -1 && ip.ix[i].af == AF_INET6) continue; + if (ip6flag == 1 && ip.ix[i].af == AF_INET) continue; if (ip.ix[i].pref < prefme) break; } - if (i >= ip.len) - perm_ambigmx(); + if (i >= ip.len) perm_ambigmx(); - if (!stralloc_copys(&remotehost,ip.ix[i].mxh)) temp_nomem(); /* take MX hostname for TLSA */ + if (!stralloc_copys(&remotehost, ip.ix[i].mxh)) temp_nomem(); /* take MX hostname for TLSA */ if (!stralloc_0(&remotehost)) temp_nomem(); for (i = 0; i < ip.len; ++i) { if (ip.ix[i].pref < prefme) { - if (ip6flag == -1 && ip.ix[i].af == AF_INET6) continue; /* explicit binding */ - if (ip6flag == 1 && ip.ix[i].af == AF_INET) continue; + if (ip6flag == -1 && ip.ix[i].af == AF_INET6) continue; /* explicit binding */ + if (ip6flag == 1 && ip.ix[i].af == AF_INET) continue; if (tcpto(&ip.ix[i])) continue; - - smtpfd = socket(ip.ix[i].af,SOCK_STREAM,0); + + smtpfd = socket(ip.ix[i].af, SOCK_STREAM, 0); if (smtpfd == -1) continue; - if (localip) { /* set domain ip */ - if (!stralloc_copyb(&sendip,localip,str_len(localip))) temp_nomem(); - j = str_chr(localip,':'); - if (localip[j] == ':') { - if (!ip6_scan(localip,ip6)) temp_noip(); /* IPv6 */ - if (byte_equal(ip.ix[i].addr.ip6.d,16,ip6)) continue; - ifidx = socket_getifidx(netif.s); - if (socket_bind6(smtpfd,ip6,0,ifidx) < 0) temp_osip(); - } else { - if (!ip4_scan(localip,ip4)) temp_noip(); /* IPv4 */ - if (byte_equal(ip.ix[i].addr.ip4.d,4,ip4)) continue; - if (socket_bind4(smtpfd,ip4,0) < 0) temp_osip(); - } - } - - - AGAIN: + if (localip) { /* set domain ip */ + if (!stralloc_copyb(&sendip, localip, str_len(localip))) temp_nomem(); + j = str_chr(localip, ':'); + if (localip[j] == ':') { + if (!ip6_scan(localip, ip6)) temp_noip(); /* IPv6 */ + if (byte_equal(ip.ix[i].addr.ip6.d, 16, ip6)) continue; + ifidx = socket_getifidx(netif.s); + if (socket_bind6(smtpfd, ip6, 0, ifidx) < 0) temp_osip(); + } else { + if (!ip4_scan(localip, ip4)) temp_noip(); /* IPv4 */ + if (byte_equal(ip.ix[i].addr.ip4.d, 4, ip4)) continue; + if (socket_bind4(smtpfd, ip4, 0) < 0) temp_osip(); + } + } + + + AGAIN: if (ip.ix[i].af == AF_INET6) - r = timeoutconn6(smtpfd,(char *)&ip.ix[i].addr.ip6.d,(unsigned int) port,timeoutconnect,ifidx); + r = timeoutconn6( + smtpfd, (char *)&ip.ix[i].addr.ip6.d, (unsigned int)port, timeoutconnect, ifidx); else - r = timeoutconn4(smtpfd,(char *)&ip.ix[i].addr.ip4.d,(unsigned int) port,timeoutconnect); + r = timeoutconn4(smtpfd, (char *)&ip.ix[i].addr.ip4.d, (unsigned int)port, timeoutconnect); if (r == 0) { - tcpto_err(&ip.ix[i],0); + tcpto_err(&ip.ix[i], 0); partner = ip.ix[i]; - if (qmtpsend) - qmtp(); - else - smtp(); /* read qmail/THOUGHTS; section 6 */ + if (qmtpsend) + qmtp(); + else + smtp(); /* read qmail/THOUGHTS; section 6 */ } if (flagtls == 9 && errno == EPROTO) { - flagtls = 0; goto AGAIN; + flagtls = 0; + goto AGAIN; } - if (errno == ETIMEDOUT || errno == ECONNREFUSED || errno == EPROTO) - tcpto_err(&ip.ix[i],1); + if (errno == ETIMEDOUT || errno == ECONNREFUSED || errno == EPROTO) tcpto_err(&ip.ix[i], 1); close(smtpfd); } } diff --git a/src/qmail-rspawn.c b/src/qmail-rspawn.c index 66a8b70..939488d 100644 --- a/src/qmail-rspawn.c +++ b/src/qmail-rspawn.c @@ -1,44 +1,61 @@ -#include <unistd.h> #include <sys/stat.h> -#include "fd.h" -#include "wait.h" +#include <unistd.h> + #include "buffer.h" -#include "exit.h" #include "error.h" -#include "ipalloc.h" -#include "tcpto.h" -#include "auto_qmail.h" +#include "exit.h" +#include "fd.h" #include "open.h" #include "pathexec.h" +#include "wait.h" + +#include "auto_qmail.h" +#include "ipalloc.h" +#include "tcpto.h" -void initialize(int argc,char **argv) { tcpto_clean(); } +void initialize(int argc, char **argv) +{ + tcpto_clean(); +} int truncreport = 0; -void report(buffer *log,int wstat,char *s,int len) +void report(buffer *log, int wstat, char *s, int len) { int j; int k; int result; int orr; - if (wait_crashed(wstat)) { buffer_putsflush(log,"Zqmail-spawn: qmail-remote crashed.\n"); return; } + if (wait_crashed(wstat)) { + buffer_putsflush(log, "Zqmail-spawn: qmail-remote crashed.\n"); + return; + } switch (wait_exitcode(wstat)) { - case 0: break; - case 111: buffer_putsflush(log,"Zqmail-rspawn: Unable to run qmail-remote.\n"); break; - default: buffer_putsflush(log,"Dqmail-rspawn: Unable to run qmail-remote. \n"); return; + case 0: break; + case 111: buffer_putsflush(log, "Zqmail-rspawn: Unable to run qmail-remote.\n"); break; + default: buffer_putsflush(log, "Dqmail-rspawn: Unable to run qmail-remote. \n"); return; } - if (!len) { buffer_putsflush(log,"Zqmail-rspawn: qmail-remote produced no output.\n"); return; } + if (!len) { + buffer_putsflush(log, "Zqmail-rspawn: qmail-remote produced no output.\n"); + return; + } result = -1; j = 0; for (k = 0; k < len; ++k) if (!s[k]) { - if (s[j] == 'K') { result = 1; break; } - if (s[j] == 'Z') { result = 0; break; } + if (s[j] == 'K') { + result = 1; + break; + } + if (s[j] == 'Z') { + result = 0; + break; + } if (s[j] == 'D') break; j = k + 1; } @@ -51,34 +68,35 @@ void report(buffer *log,int wstat,char *s,int len) } switch (orr) { - case 1: buffer_put(log,"K",1); break; - case 0: buffer_put(log,"Z",1); break; - case -1: buffer_put(log,"D",1); break; + case 1: buffer_put(log, "K", 1); break; + case 0: buffer_put(log, "Z", 1); break; + case -1: buffer_put(log, "D", 1); break; } for (k = 1; k < len;) if (!s[k++]) { - buffer_puts(log,s + 1); + buffer_puts(log, s + 1); if (result <= orr) - if (k < len) - switch (s[k]) { - case 'Z': case 'D': case 'K': - buffer_puts(log,s + k + 1); + if (k < len) switch (s[k]) + { + case 'Z': + case 'D': + case 'K': buffer_puts(log, s + k + 1); } - break; - } + break; + } } -int spawn(int fdmess,int fdout,const char *s,char *r,const int at) +int spawn(int fdmess, int fdout, const char *s, char *r, const int at) { int f; char *(args[5]); struct stat st; if (chdir(auto_qmail) == -1) _exit(110); - if (!stat("control/dkimdomains",&st)) + if (!stat("control/dkimdomains", &st)) args[0] = "qmail-dksign"; - else + else args[0] = "qmail-remote"; args[1] = r + at + 1; args[2] = s; @@ -88,9 +106,9 @@ int spawn(int fdmess,int fdout,const char *s,char *r,const int at) if (chdir("queue/mess") == -1) _exit(110); if (!(f = vfork())) { - if (fd_move(0,fdmess) == -1) _exit(111); - if (fd_move(1,fdout) == -1) _exit(111); - if (fd_copy(2,1) == -1) _exit(111); + if (fd_move(0, fdmess) == -1) _exit(111); + if (fd_move(1, fdout) == -1) _exit(111); + if (fd_copy(2, 1) == -1) _exit(111); pathexec(args); if (errno) _exit(111); _exit(100); diff --git a/src/qmail-send.c b/src/qmail-send.c index 76925d2..5305d95 100644 --- a/src/qmail-send.c +++ b/src/qmail-send.c @@ -1,41 +1,44 @@ -#include <sys/types.h> #include <sys/stat.h> -#include <string.h> +#include <sys/types.h> #include <unistd.h> #include <utime.h> -#include "error.h" -#include "sig.h" + +#include <string.h> + +#include "alloc.h" +#include "buffer.h" +#include "byte.h" +#include "case.h" +#include "constmap.h" #include "direntry.h" -#include "control.h" -#include "select.h" -#include "open.h" -#include "seek.h" +#include "error.h" #include "exit.h" -#include "lock.h" -#include "ndelay.h" -#include "now.h" -#include "getln.h" -#include "buffer.h" -#include "alloc.h" +#include "fmt.h" #include "genalloc.h" -#include "stralloc.h" +#include "getln.h" +#include "lock.h" #include "logmsg.h" -#include "str.h" -#include "byte.h" -#include "fmt.h" +#include "ndelay.h" +#include "open.h" #include "scan.h" -#include "case.h" +#include "seek.h" +#include "select.h" +#include "sig.h" +#include "str.h" +#include "stralloc.h" + #include "auto_qmail.h" -#include "trigger.h" +#include "control.h" +#include "fmtqfn.h" #include "newfield.h" -#include "quote.h" +#include "now.h" +#include "prioq.h" #include "qmail.h" #include "qsutil.h" -#include "prioq.h" -#include "constmap.h" -#include "fmtqfn.h" +#include "quote.h" #include "readsubdir.h" #include "sendtodo.h" +#include "trigger.h" int lifetime = 604800; int bouncemaxbytes = 0; @@ -56,30 +59,42 @@ char strnum2[FMT_ULONG]; char strnum3[FMT_ULONG]; #define CHANNELS 2 -char *chanaddr[CHANNELS] = { "local/", "remote/" }; -char *chanstatusmsg[CHANNELS] = { " local ", " remote " }; -char *tochan[CHANNELS] = { " to local ", " to remote " }; -int chanfdout[CHANNELS] = { 1, 3 }; -int chanfdin[CHANNELS] = { 2, 4 }; -int chanskip[CHANNELS] = { 10, 20 }; - -int flagexitasap = 0; void sigterm() { flagexitasap = 1; } -int flagrunasap = 0; void sigalrm() { flagrunasap = 1; } -int flagreadasap = 0; void sighup() { flagreadasap = 1; } - -void cleandied() -{ - log1s("alert: lost connection to qmail-clean ... exiting\n"); - flagexitasap = 1; +char *chanaddr[CHANNELS] = {"local/", "remote/"}; +char *chanstatusmsg[CHANNELS] = {" local ", " remote "}; +char *tochan[CHANNELS] = {" to local ", " to remote "}; +int chanfdout[CHANNELS] = {1, 3}; +int chanfdin[CHANNELS] = {2, 4}; +int chanskip[CHANNELS] = {10, 20}; + +int flagexitasap = 0; +void sigterm() +{ + flagexitasap = 1; +} +int flagrunasap = 0; +void sigalrm() +{ + flagrunasap = 1; +} +int flagreadasap = 0; +void sighup() +{ + flagreadasap = 1; +} + +void cleandied() +{ + log1s("alert: lost connection to qmail-clean ... exiting\n"); + flagexitasap = 1; } int flagspawnalive[CHANNELS]; -void spawndied(int c) -{ - log1s("alert: oh no! lost spawn connection! dying...\n"); - flagspawnalive[c] = 0; - flagexitasap = 1; +void spawndied(int c) +{ + log1s("alert: oh no! lost spawn connection! dying...\n"); + flagspawnalive[c] = 0; + flagexitasap = 1; } #define REPORTMAX 10000 @@ -95,22 +110,43 @@ char fnmake_strnum[FMT_ULONG]; void fnmake_init() { - while (!stralloc_ready(&fn,FMTQFN)) nomem(); - while (!stralloc_ready(&fn2,FMTQFN)) nomem(); + while (!stralloc_ready(&fn, FMTQFN)) nomem(); + while (!stralloc_ready(&fn2, FMTQFN)) nomem(); } -void fnmake_info(unsigned long id) { fn.len = fmtqfn(fn.s,"info/",id,1); } -void fnmake_todo(unsigned long id) { fn.len = fmtqfn(fn.s,"todo/",id,1); } -void fnmake_mess(unsigned long id) { fn.len = fmtqfn(fn.s,"mess/",id,1); } -void fnmake_foop(unsigned long id) { fn.len = fmtqfn(fn.s,"foop/",id,0); } -void fnmake_split(unsigned long id) { fn.len = fmtqfn(fn.s,"",id,1); } -void fnmake2_bounce(unsigned long id) { fn2.len = fmtqfn(fn2.s,"bounce/",id,0); } -void fnmake_chanaddr(unsigned long id,int c) { fn.len = fmtqfn(fn.s,chanaddr[c],id,1); } +void fnmake_info(unsigned long id) +{ + fn.len = fmtqfn(fn.s, "info/", id, 1); +} +void fnmake_todo(unsigned long id) +{ + fn.len = fmtqfn(fn.s, "todo/", id, 1); +} +void fnmake_mess(unsigned long id) +{ + fn.len = fmtqfn(fn.s, "mess/", id, 1); +} +void fnmake_foop(unsigned long id) +{ + fn.len = fmtqfn(fn.s, "foop/", id, 0); +} +void fnmake_split(unsigned long id) +{ + fn.len = fmtqfn(fn.s, "", id, 1); +} +void fnmake2_bounce(unsigned long id) +{ + fn2.len = fmtqfn(fn2.s, "bounce/", id, 0); +} +void fnmake_chanaddr(unsigned long id, int c) +{ + fn.len = fmtqfn(fn.s, chanaddr[c], id, 1); +} /* this file is too long ----------------------------------------- REWRITING */ -void senderadd(stralloc *sa,char *sender,char *recip) +void senderadd(stralloc *sa, char *sender, char *recip) { int i; int j; @@ -118,27 +154,27 @@ void senderadd(stralloc *sa,char *sender,char *recip) i = str_len(sender); if (i >= 4) - if (str_equal(sender + i - 4,"-@[]")) { - j = byte_rchr(sender,i - 4,'@'); - k = str_rchr(recip,'@'); + if (str_equal(sender + i - 4, "-@[]")) { + j = byte_rchr(sender, i - 4, '@'); + k = str_rchr(recip, '@'); if (recip[k] && (j + 5 <= i)) { /* owner-@host-@[] -> owner-recipbox=reciphost@host */ - while (!stralloc_catb(sa,sender,j)) nomem(); - while (!stralloc_catb(sa,recip,k)) nomem(); - while (!stralloc_cats(sa,"=")) nomem(); - while (!stralloc_cats(sa,recip + k + 1)) nomem(); - while (!stralloc_cats(sa,"@")) nomem(); - while (!stralloc_catb(sa,sender + j + 1,i - 5 - j)) nomem(); + while (!stralloc_catb(sa, sender, j)) nomem(); + while (!stralloc_catb(sa, recip, k)) nomem(); + while (!stralloc_cats(sa, "=")) nomem(); + while (!stralloc_cats(sa, recip + k + 1)) nomem(); + while (!stralloc_cats(sa, "@")) nomem(); + while (!stralloc_catb(sa, sender + j + 1, i - 5 - j)) nomem(); return; } } - while (!stralloc_cats(sa,sender)) nomem(); + while (!stralloc_cats(sa, sender)) nomem(); } /* this file is too long ---------------------------------------------- INFO */ -int getinfo(stralloc *sa,datetime_sec *dt,unsigned long id) +int getinfo(stralloc *sa, datetime_sec *dt, unsigned long id) { int fdnumber; struct stat st; @@ -150,15 +186,21 @@ int getinfo(stralloc *sa,datetime_sec *dt,unsigned long id) fnmake_info(id); fdnumber = open_read(fn.s); if (fdnumber == -1) return 0; - if (fstat(fdnumber,&st) == -1) { close(fdnumber); return 0; } - buffer_init(&b,read,fdnumber,buf,sizeof(buf)); - if (getln(&b,&line,&match,'\0') == -1) { close(fdnumber); return 0; } + if (fstat(fdnumber, &st) == -1) { + close(fdnumber); + return 0; + } + buffer_init(&b, read, fdnumber, buf, sizeof(buf)); + if (getln(&b, &line, &match, '\0') == -1) { + close(fdnumber); + return 0; + } close(fdnumber); if (!match) return 0; if (line.s[0] != 'F') return 0; *dt = st.st_mtime; - while (!stralloc_copys(sa,line.s + 1)) nomem(); + while (!stralloc_copys(sa, line.s + 1)) nomem(); while (!stralloc_0(sa)) nomem(); return 1; } @@ -166,21 +208,23 @@ int getinfo(stralloc *sa,datetime_sec *dt,unsigned long id) /* this file is too long ------------------------------------- COMMUNICATION */ -buffer toqc; char toqcbuf[1024]; -buffer fromqc; char fromqcbuf[1024]; -stralloc comm_buf[CHANNELS] = { {0}, {0} }; +buffer toqc; +char toqcbuf[1024]; +buffer fromqc; +char fromqcbuf[1024]; +stralloc comm_buf[CHANNELS] = {{0}, {0}}; int comm_pos[CHANNELS]; void comm_init() { int c; - buffer_init(&toqc,write,5,toqcbuf,sizeof(toqcbuf)); - buffer_init(&fromqc,read,6,fromqcbuf,sizeof(fromqcbuf)); + buffer_init(&toqc, write, 5, toqcbuf, sizeof(toqcbuf)); + buffer_init(&fromqc, read, 6, fromqcbuf, sizeof(fromqcbuf)); for (c = 0; c < CHANNELS; ++c) if (ndelay_on(chanfdout[c]) == -1) - /* this is so stupid: NDELAY semantics should be default on write */ - spawndied(c); /* drastic, but better than risking deadlock */ + /* this is so stupid: NDELAY semantics should be default on write */ + spawndied(c); /* drastic, but better than risking deadlock */ } int comm_canwrite(int c) @@ -190,34 +234,33 @@ int comm_canwrite(int c) return 1; } -void comm_write(int c,int delnum,unsigned long id,char *sender,char *recip) +void comm_write(int c, int delnum, unsigned long id, char *sender, char *recip) { char ch; if (comm_buf[c].s && comm_buf[c].len) return; - while (!stralloc_copys(&comm_buf[c],"")) nomem(); + while (!stralloc_copys(&comm_buf[c], "")) nomem(); ch = delnum; - while (!stralloc_append(&comm_buf[c],&ch)) nomem(); + while (!stralloc_append(&comm_buf[c], &ch)) nomem(); fnmake_split(id); - while (!stralloc_cats(&comm_buf[c],fn.s)) nomem(); + while (!stralloc_cats(&comm_buf[c], fn.s)) nomem(); while (!stralloc_0(&comm_buf[c])) nomem(); - senderadd(&comm_buf[c],sender,recip); + senderadd(&comm_buf[c], sender, recip); while (!stralloc_0(&comm_buf[c])) nomem(); - while (!stralloc_cats(&comm_buf[c],recip)) nomem(); + while (!stralloc_cats(&comm_buf[c], recip)) nomem(); while (!stralloc_0(&comm_buf[c])) nomem(); comm_pos[c] = 0; } -void comm_selprep(int *nfds,fd_set *wfds) +void comm_selprep(int *nfds, fd_set *wfds) { int c; for (c = 0; c < CHANNELS; ++c) if (flagspawnalive[c]) if (comm_buf[c].s && comm_buf[c].len) { - FD_SET(chanfdout[c],wfds); - if (*nfds <= chanfdout[c]) - *nfds = chanfdout[c] + 1; + FD_SET(chanfdout[c], wfds); + if (*nfds <= chanfdout[c]) *nfds = chanfdout[c] + 1; } } @@ -228,23 +271,22 @@ void comm_do(fd_set *wfds) for (c = 0; c < CHANNELS; ++c) if (flagspawnalive[c]) if (comm_buf[c].s && comm_buf[c].len) - if (FD_ISSET(chanfdout[c],wfds)) { + if (FD_ISSET(chanfdout[c], wfds)) { int w; int len; len = comm_buf[c].len; - w = write(chanfdout[c],comm_buf[c].s + comm_pos[c],len - comm_pos[c]); + w = write(chanfdout[c], comm_buf[c].s + comm_pos[c], len - comm_pos[c]); if (w <= 0) { if ((w == -1) && (errno == EPIPE)) spawndied(c); else - continue; /* kernel select() bug; can't avoid busy-looping */ + continue; /* kernel select() bug; can't avoid busy-looping */ } else { - comm_pos[c] += w; - if (comm_pos[c] == len) - comm_buf[c].len = 0; + comm_pos[c] += w; + if (comm_pos[c] == len) comm_buf[c].len = 0; } - } + } } @@ -274,40 +316,45 @@ void cleanup_do() if (!flagcleanup) { if (recent < cleanuptime) return; - readsubdir_init(&cleanupdir,"mess",pausedir); + readsubdir_init(&cleanupdir, "mess", pausedir); flagcleanup = 1; } - switch (readsubdir_next(&cleanupdir,&id)) { - case 1: break; + switch (readsubdir_next(&cleanupdir, &id)) { + case 1: break; case 0: flagcleanup = 0; cleanuptime = recent + SLEEP_CLEANUP; default: return; } fnmake_mess(id); - if (stat(fn.s,&st) == -1) return; /* probably qmail-queue deleted it */ + if (stat(fn.s, &st) == -1) return; /* probably qmail-queue deleted it */ if (recent <= st.st_atime + OSSIFIED) return; fnmake_info(id); - if (stat(fn.s,&st) == 0) return; + if (stat(fn.s, &st) == 0) return; if (errno != ENOENT) return; fnmake_todo(id); - if (stat(fn.s,&st) == 0) return; + if (stat(fn.s, &st) == 0) return; if (errno != ENOENT) return; fnmake_foop(id); - if (buffer_putflush(&toqc,fn.s,fn.len) == -1) { cleandied(); return; } - if (buffer_get(&fromqc,&ch,1) != 1) { cleandied(); return; } - if (ch != '+') - log3s("warning: qmail-clean unable to clean up ",fn.s,"\n"); + if (buffer_putflush(&toqc, fn.s, fn.len) == -1) { + cleandied(); + return; + } + if (buffer_get(&fromqc, &ch, 1) != 1) { + cleandied(); + return; + } + if (ch != '+') log3s("warning: qmail-clean unable to clean up ", fn.s, "\n"); } /* this file is too long ----------------------------------- PRIORITY QUEUES */ prioq pqdone = {0}; /* -todo +info; HOPEFULLY -local -remote */ -prioq pqchan[CHANNELS] = { {0}, {0} }; +prioq pqchan[CHANNELS] = {{0}, {0}}; /* pqchan 0: -todo +info +local ?remote */ /* pqchan 1: -todo +info ?local +remote */ prioq pqfail = {0}; /* stat() failure; has to be pqadded again */ @@ -320,42 +367,51 @@ void pqadd(unsigned long id) struct stat st; int c; -#define CHECKSTAT if (errno != ENOENT) goto FAIL; +#define CHECKSTAT \ + if (errno != ENOENT) goto FAIL; fnmake_info(id); - if (stat(fn.s,&st) == -1) { + if (stat(fn.s, &st) == -1) { CHECKSTAT return; /* someone yanking our chain */ } fnmake_todo(id); - if (stat(fn.s,&st) != -1) return; /* look, ma, dad crashed writing info! */ + if (stat(fn.s, &st) != -1) return; /* look, ma, dad crashed writing info! */ CHECKSTAT for (c = 0; c < CHANNELS; ++c) { - fnmake_chanaddr(id,c); - if (stat(fn.s,&st) == -1) { flagchan[c] = 0; CHECKSTAT } - else { flagchan[c] = 1; pechan[c].id = id; pechan[c].dt = st.st_mtime; } + fnmake_chanaddr(id, c); + if (stat(fn.s, &st) == -1) { + flagchan[c] = 0; + CHECKSTAT + } else { + flagchan[c] = 1; + pechan[c].id = id; + pechan[c].dt = st.st_mtime; + } } for (c = 0; c < CHANNELS; ++c) if (flagchan[c]) - while (!prioq_insert(&pqchan[c],&pechan[c])) nomem(); + while (!prioq_insert(&pqchan[c], &pechan[c])) nomem(); - for (c = 0; c < CHANNELS; ++c) + for (c = 0; c < CHANNELS; ++c) if (flagchan[c]) break; if (c == CHANNELS) { - pe.id = id; pe.dt = now(); - while (!prioq_insert(&pqdone,&pe)) nomem(); + pe.id = id; + pe.dt = now(); + while (!prioq_insert(&pqdone, &pe)) nomem(); } return; - FAIL: - log3s("warning: unable to stat ",fn.s,"; will try again later\n"); - pe.id = id; pe.dt = now() + SLEEP_SYSFAIL; - while (!prioq_insert(&pqfail,&pe)) nomem(); +FAIL: + log3s("warning: unable to stat ", fn.s, "; will try again later\n"); + pe.id = id; + pe.dt = now() + SLEEP_SYSFAIL; + while (!prioq_insert(&pqfail, &pe)) nomem(); } void pqstart() @@ -364,9 +420,9 @@ void pqstart() int x; unsigned long id; - readsubdir_init(&rs,"info",pausedir); + readsubdir_init(&rs, "info", pausedir); - while ((x = readsubdir_next(&rs,&id))) + while ((x = readsubdir_next(&rs, &id))) if (x > 0) pqadd(id); } @@ -377,12 +433,12 @@ void pqfinish() time_t ut[2]; /* XXX: more portable than utimbuf, but still worrisome */ for (c = 0; c < CHANNELS; ++c) - while (prioq_min(&pqchan[c],&pe)) { + while (prioq_min(&pqchan[c], &pe)) { prioq_delmin(&pqchan[c]); - fnmake_chanaddr(pe.id,c); + fnmake_chanaddr(pe.id, c); ut[0] = ut[1] = pe.dt; - if (utime(fn.s,ut) == -1) - log3s("warning: unable to utime ",fn.s,"; message will be retried too soon\n"); + if (utime(fn.s, ut) == -1) + log3s("warning: unable to utime ", fn.s, "; message will be retried too soon\n"); } } @@ -394,16 +450,14 @@ void pqrun() for (c = 0; c < CHANNELS; ++c) if (pqchan[c].p) if (pqchan[c].len) - for (i = 0; i < pqchan[c].len; ++i) - pqchan[c].p[i].dt = recent; + for (i = 0; i < pqchan[c].len; ++i) pqchan[c].p[i].dt = recent; } /* this file is too long ---------------------------------------------- JOBS */ -struct job -{ - int refs; /* if 0, this struct is unused */ +struct job { + int refs; /* if 0, this struct is unused */ unsigned long id; int channel; datetime_sec retry; @@ -420,7 +474,7 @@ void job_init() { int j; - while (!(jo = (struct job *) alloc(numjobs * sizeof(struct job)))) nomem(); + while (!(jo = (struct job *)alloc(numjobs * sizeof(struct job)))) nomem(); for (j = 0; j < numjobs; ++j) { jo[j].refs = 0; jo[j].sender.s = 0; @@ -431,16 +485,16 @@ int job_avail() { int j; - for (j = 0; j < numjobs; ++j) + for (j = 0; j < numjobs; ++j) if (!jo[j].refs) return 1; return 0; } -int job_open(unsigned long id,int channel) +int job_open(unsigned long id, int channel) { int j; - for (j = 0; j < numjobs; ++j) + for (j = 0; j < numjobs; ++j) if (!jo[j].refs) break; if (j == numjobs) return -1; jo[j].refs = 1; @@ -463,26 +517,27 @@ void job_close(int j) pe.dt = jo[j].retry; if (jo[j].flaghiteof && !jo[j].numtodo) { - fnmake_chanaddr(jo[j].id,jo[j].channel); + fnmake_chanaddr(jo[j].id, jo[j].channel); if (unlink(fn.s) == -1) { - log3s("warning: unable to unlink ",fn.s,"; will try again later\n"); + log3s("warning: unable to unlink ", fn.s, "; will try again later\n"); pe.dt = now() + SLEEP_SYSFAIL; } else { - for (c = 0; c < CHANNELS; ++c) if (c != jo[j].channel) { - fnmake_chanaddr(jo[j].id,c); - if (stat(fn.s,&st) == 0) return; /* more channels going */ - if (errno != ENOENT) { - log3s("warning: unable to stat ",fn.s,"\n"); - break; /* this is the only reason for HOPEFULLY */ - } - } - pe.dt = now(); - while (!prioq_insert(&pqdone,&pe)) nomem(); - return; + for (c = 0; c < CHANNELS; ++c) + if (c != jo[j].channel) { + fnmake_chanaddr(jo[j].id, c); + if (stat(fn.s, &st) == 0) return; /* more channels going */ + if (errno != ENOENT) { + log3s("warning: unable to stat ", fn.s, "\n"); + break; /* this is the only reason for HOPEFULLY */ + } + } + pe.dt = now(); + while (!prioq_insert(&pqdone, &pe)) nomem(); + return; } } - while (!prioq_insert(&pqchan[jo[j].channel],&pe)) nomem(); + while (!prioq_insert(&pqchan[jo[j].channel], &pe)) nomem(); } @@ -495,17 +550,17 @@ char *stripvdomprepend(char *recip) int domainlen; char *prepend; - i = str_rchr(recip,'@'); + i = str_rchr(recip, '@'); if (!recip[i]) return recip; domain = recip + i + 1; domainlen = str_len(domain); for (i = 0; i <= domainlen; ++i) if ((i == 0) || (i == domainlen) || (domain[i] == '.')) - if ((prepend = constmap(&mapvdoms,domain + i,domainlen - i))) { + if ((prepend = constmap(&mapvdoms, domain + i, domainlen - i))) { if (!*prepend) break; i = str_len(prepend); - if (str_diffn(recip,prepend,i)) break; + if (str_diffn(recip, prepend, i)) break; if (recip[i] != '-') break; return recip + i + 1; } @@ -515,32 +570,30 @@ char *stripvdomprepend(char *recip) stralloc bouncetext = {0}; -void addbounce(unsigned long id,char *recip,char *report) +void addbounce(unsigned long id, char *recip, char *report) { int fd; int pos; int w; - while (!stralloc_copys(&bouncetext,"<")) nomem(); - while (!stralloc_cats(&bouncetext,stripvdomprepend(recip))) nomem(); + while (!stralloc_copys(&bouncetext, "<")) nomem(); + while (!stralloc_cats(&bouncetext, stripvdomprepend(recip))) nomem(); for (pos = 0; pos < bouncetext.len; ++pos) - if (bouncetext.s[pos] == '\n') - bouncetext.s[pos] = '_'; + if (bouncetext.s[pos] == '\n') bouncetext.s[pos] = '_'; - while (!stralloc_cats(&bouncetext,">:\n")) nomem(); - while (!stralloc_cats(&bouncetext,report)) nomem(); + while (!stralloc_cats(&bouncetext, ">:\n")) nomem(); + while (!stralloc_cats(&bouncetext, report)) nomem(); if (report[0]) if (report[str_len(report) - 1] != '\n') - while (!stralloc_cats(&bouncetext,"\n")) nomem(); + while (!stralloc_cats(&bouncetext, "\n")) nomem(); for (pos = bouncetext.len - 2; pos > 0; --pos) if (bouncetext.s[pos] == '\n') - if (bouncetext.s[pos - 1] == '\n') - bouncetext.s[pos] = '/'; + if (bouncetext.s[pos - 1] == '\n') bouncetext.s[pos] = '/'; - while (!stralloc_cats(&bouncetext,"\n")) nomem(); + while (!stralloc_cats(&bouncetext, "\n")) nomem(); fnmake2_bounce(id); for (;;) { @@ -553,12 +606,11 @@ void addbounce(unsigned long id,char *recip,char *report) pos = 0; while (pos < bouncetext.len) { - w = write(fd,bouncetext.s + pos,bouncetext.len - pos); + w = write(fd, bouncetext.s + pos, bouncetext.len - pos); if (w <= 0) { log1s("alert: unable to append to bounce message; HELP! sleeping...\n"); sleep(10); - } - else + } else pos += w; } close(fd); @@ -582,11 +634,11 @@ int injectbounce(unsigned long id) int bytestogo; int bytestoget; - if (!getinfo(&sender,&birth,id)) return 0; /* XXX: print warning */ + if (!getinfo(&sender, &birth, id)) return 0; /* XXX: print warning */ /* owner-@host-@[] -> owner-@host */ if (sender.len >= 5) - if (str_equal(sender.s + sender.len - 5,"-@[]")) { + if (str_equal(sender.s + sender.len - 5, "-@[]")) { sender.len -= 4; sender.s[sender.len - 1] = 0; } @@ -594,109 +646,121 @@ int injectbounce(unsigned long id) fnmake2_bounce(id); fnmake_mess(id); - if (stat(fn2.s,&st) == -1) { + if (stat(fn2.s, &st) == -1) { if (errno == ENOENT) return 1; - log3s("warning: unable to stat ",fn2.s,"\n"); + log3s("warning: unable to stat ", fn2.s, "\n"); return 0; } - - if (str_equal(sender.s,"#@[]")) - log3s("triple bounce: discarding ",fn2.s,"\n"); - else if (!*sender.s && *doublebounceto.s == '@') - log3s("double bounce: discarding ",fn2.s,"\n"); + + if (str_equal(sender.s, "#@[]")) + log3s("triple bounce: discarding ", fn2.s, "\n"); + else if (!*sender.s && *doublebounceto.s == '@') + log3s("double bounce: discarding ", fn2.s, "\n"); else { - if (qmail_open(&qqt) == -1) - { log1s("warning: unable to start qmail-queue, will try later\n"); return 0; } + if (qmail_open(&qqt) == -1) { + log1s("warning: unable to start qmail-queue, will try later\n"); + return 0; + } qp = qmail_qp(&qqt); - if (*sender.s) { bouncesender = ""; bouncerecip = sender.s; } - else { bouncesender = "#@[]"; bouncerecip = doublebounceto.s; } + if (*sender.s) { + bouncesender = ""; + bouncerecip = sender.s; + } else { + bouncesender = "#@[]"; + bouncerecip = doublebounceto.s; + } while (!newfield_datemake(now())) nomem(); - qmail_put(&qqt,newfield_date.s,newfield_date.len); - qmail_puts(&qqt,"From: "); - while (!quote("ed,&bouncefrom)) nomem(); - qmail_put(&qqt,quoted.s,quoted.len); - qmail_puts(&qqt,"@"); - qmail_put(&qqt,bouncehost.s,bouncehost.len); - qmail_puts(&qqt,"\nTo: "); - while (!quote2("ed,bouncerecip)) nomem(); - qmail_put(&qqt,quoted.s,quoted.len); - qmail_puts(&qqt,"\n\ + qmail_put(&qqt, newfield_date.s, newfield_date.len); + qmail_puts(&qqt, "From: "); + while (!quote("ed, &bouncefrom)) nomem(); + qmail_put(&qqt, quoted.s, quoted.len); + qmail_puts(&qqt, "@"); + qmail_put(&qqt, bouncehost.s, bouncehost.len); + qmail_puts(&qqt, "\nTo: "); + while (!quote2("ed, bouncerecip)) nomem(); + qmail_put(&qqt, quoted.s, quoted.len); + qmail_puts( + &qqt, + "\n\ Subject: failure notice\n\ \n\ Hi. This is the qmail-send program at "); - qmail_put(&qqt,bouncehost.s,bouncehost.len); - qmail_puts(&qqt,*sender.s ? ".\n\ + qmail_put(&qqt, bouncehost.s, bouncehost.len); + qmail_puts( + &qqt, + *sender.s ? ".\n\ I'm afraid I wasn't able to deliver your message to the following addresses.\n\ This is a permanent error; I've given up. Sorry it didn't work out.\n\ \n\ -" : ".\n\ +" + : ".\n\ I tried to deliver a bounce message to this address, but the bounce bounced!\n\ \n\ "); fd = open_read(fn2.s); if (fd == -1) - qmail_fail(&qqt); + qmail_fail(&qqt); else { - buffer_init(&bi,read,fd,inbuf,sizeof(inbuf)); - while ((r = buffer_get(&bi,buf,sizeof(buf))) > 0) - qmail_put(&qqt,buf,r); + buffer_init(&bi, read, fd, inbuf, sizeof(inbuf)); + while ((r = buffer_get(&bi, buf, sizeof(buf))) > 0) qmail_put(&qqt, buf, r); close(fd); if (r == -1) qmail_fail(&qqt); } - qmail_puts(&qqt,*sender.s ? "--- Below this line is a copy of the message.\n\n" : "--- Below this line is the original bounce.\n\n"); - qmail_puts(&qqt,"Return-Path: <"); - while (!quote2("ed,sender.s)) nomem(); - qmail_put(&qqt,quoted.s,quoted.len); - qmail_puts(&qqt,">\n"); + qmail_puts( + &qqt, + *sender.s ? "--- Below this line is a copy of the message.\n\n" + : "--- Below this line is the original bounce.\n\n"); + qmail_puts(&qqt, "Return-Path: <"); + while (!quote2("ed, sender.s)) nomem(); + qmail_put(&qqt, quoted.s, quoted.len); + qmail_puts(&qqt, ">\n"); fd = open_read(fn.s); - if (fd == -1) + if (fd == -1) qmail_fail(&qqt); else { if (bouncemaxbytes) { bytestogo = bouncemaxbytes; bytestoget = (bytestogo < sizeof(buf)) ? bytestogo : sizeof(buf); - buffer_init(&bi,read,fd,inbuf,sizeof(inbuf)); + buffer_init(&bi, read, fd, inbuf, sizeof(inbuf)); - while (bytestoget > 0 && (r = buffer_get(&bi,buf,bytestoget)) > 0) { - qmail_put(&qqt,buf,r); + while (bytestoget > 0 && (r = buffer_get(&bi, buf, bytestoget)) > 0) { + qmail_put(&qqt, buf, r); bytestogo -= bytestoget; bytestoget = (bytestogo < sizeof(buf)) ? bytestogo : sizeof(buf); } - if (r > 0) - qmail_puts(&qqt,"\n\n--- Rest of message truncated.\n"); - } else { /* preserve default behavior */ - buffer_init(&bi,read,fd,inbuf,sizeof(inbuf)); + if (r > 0) qmail_puts(&qqt, "\n\n--- Rest of message truncated.\n"); + } else { /* preserve default behavior */ + buffer_init(&bi, read, fd, inbuf, sizeof(inbuf)); - while ((r = buffer_get(&bi,buf,sizeof(buf))) > 0) - qmail_put(&qqt,buf,r); + while ((r = buffer_get(&bi, buf, sizeof(buf))) > 0) qmail_put(&qqt, buf, r); } close(fd); if (r == -1) qmail_fail(&qqt); } - qmail_from(&qqt,bouncesender); - qmail_to(&qqt,bouncerecip); + qmail_from(&qqt, bouncesender); + qmail_to(&qqt, bouncerecip); - if (*qmail_close(&qqt)) { + if (*qmail_close(&qqt)) { log1s("warning: trouble injecting bounce message, will try later\n"); - return 0; + return 0; } - strnum2[fmt_ulong(strnum2,id)] = 0; - log2s("bounce msg ",strnum2); - strnum2[fmt_ulong(strnum2,qp)] = 0; - log3s(" qp ",strnum2,"\n"); + strnum2[fmt_ulong(strnum2, id)] = 0; + log2s("bounce msg ", strnum2); + strnum2[fmt_ulong(strnum2, qp)] = 0; + log3s(" qp ", strnum2, "\n"); } if (unlink(fn2.s) != 0) { - log3s("warning: unable to unlink ",fn2.s,"\n"); + log3s("warning: unable to unlink ", fn2.s, "\n"); return 0; } @@ -715,8 +779,8 @@ struct del { }; unsigned long masterdelid = 1; -unsigned int concurrency[CHANNELS] = { 10, 20 }; -unsigned int concurrencyused[CHANNELS] = { 0, 0 }; +unsigned int concurrency[CHANNELS] = {10, 20}; +unsigned int concurrencyused[CHANNELS] = {0, 0}; struct del *d[CHANNELS]; stralloc dline[CHANNELS]; char delbuf[2048]; @@ -727,10 +791,10 @@ void del_status() log1s("status:"); for (c = 0; c < CHANNELS; ++c) { - strnum2[fmt_ulong(strnum2,(unsigned long) concurrencyused[c])] = 0; - strnum3[fmt_ulong(strnum3,(unsigned long) concurrency[c])] = 0; - log2s(chanstatusmsg[c],strnum2); - log2s("/",strnum3); + strnum2[fmt_ulong(strnum2, (unsigned long)concurrencyused[c])] = 0; + strnum3[fmt_ulong(strnum3, (unsigned long)concurrency[c])] = 0; + log2s(chanstatusmsg[c], strnum2); + log2s("/", strnum3); } if (flagexitasap) log1s(" exitasap"); log1s("\n"); @@ -743,12 +807,13 @@ void del_init() for (c = 0; c < CHANNELS; ++c) { flagspawnalive[c] = 1; - while (!(d[c] = (struct del *) alloc(concurrency[c] * sizeof(struct del)))) - nomem(); - for (i = 0; i < concurrency[c]; ++i) - { d[c][i].used = 0; d[c][i].recip.s = 0; } - dline[c].s = 0; - while (!stralloc_copys(&dline[c],"")) nomem(); + while (!(d[c] = (struct del *)alloc(concurrency[c] * sizeof(struct del)))) nomem(); + for (i = 0; i < concurrency[c]; ++i) { + d[c][i].used = 0; + d[c][i].recip.s = 0; + } + dline[c].s = 0; + while (!stralloc_copys(&dline[c], "")) nomem(); } del_status(); @@ -759,8 +824,8 @@ int del_canexit() int c; for (c = 0; c < CHANNELS; ++c) - if (flagspawnalive[c]) /* if dead, nothing we can do about its jobs */ - if (concurrencyused[c]) return 0; + if (flagspawnalive[c]) /* if dead, nothing we can do about its jobs */ + if (concurrencyused[c]) return 0; return 1; } @@ -769,7 +834,7 @@ int del_avail(int c) return flagspawnalive[c] && comm_canwrite(c) && (concurrencyused[c] < concurrency[c]); } -void del_start(int j,seek_pos mpos,char *recip) +void del_start(int j, seek_pos mpos, char *recip) { int i; int c; @@ -778,46 +843,63 @@ void del_start(int j,seek_pos mpos,char *recip) if (!flagspawnalive[c]) return; if (!comm_canwrite(c)) return; - for (i = 0; i < concurrency[c]; ++i) + for (i = 0; i < concurrency[c]; ++i) if (!d[c][i].used) break; if (i == concurrency[c]) return; - if (!stralloc_copys(&d[c][i].recip,recip)) { nomem(); return; } - if (!stralloc_0(&d[c][i].recip)) { nomem(); return; } - d[c][i].j = j; ++jo[j].refs; + if (!stralloc_copys(&d[c][i].recip, recip)) { + nomem(); + return; + } + if (!stralloc_0(&d[c][i].recip)) { + nomem(); + return; + } + d[c][i].j = j; + ++jo[j].refs; d[c][i].delid = masterdelid++; d[c][i].mpos = mpos; - d[c][i].used = 1; ++concurrencyused[c]; + d[c][i].used = 1; + ++concurrencyused[c]; - comm_write(c,i,jo[j].id,jo[j].sender.s,recip); + comm_write(c, i, jo[j].id, jo[j].sender.s, recip); - strnum2[fmt_ulong(strnum2,d[c][i].delid)] = 0; - strnum3[fmt_ulong(strnum3,jo[j].id)] = 0; - log2s("starting delivery ",strnum2); - log3s(": msg ",strnum3,tochan[c]); + strnum2[fmt_ulong(strnum2, d[c][i].delid)] = 0; + strnum3[fmt_ulong(strnum3, jo[j].id)] = 0; + log2s("starting delivery ", strnum2); + log3s(": msg ", strnum3, tochan[c]); logsafe(recip); log1s("\n"); del_status(); } -void markdone(int c,unsigned long id,seek_pos pos) +void markdone(int c, unsigned long id, seek_pos pos) { struct stat st; int fd; - fnmake_chanaddr(id,c); + fnmake_chanaddr(id, c); for (;;) { fd = open_write(fn.s); if (fd == -1) break; - if (fstat(fd,&st) == -1) { close(fd); break; } - if (seek_set(fd,pos) == -1) { close(fd); break; } - if (write(fd,"D",1) != 1) { close(fd); break; } + if (fstat(fd, &st) == -1) { + close(fd); + break; + } + if (seek_set(fd, pos) == -1) { + close(fd); + break; + } + if (write(fd, "D", 1) != 1) { + close(fd); + break; + } /* further errors -> double delivery without us knowing about it, oh well */ close(fd); return; } - log3s("warning: trouble marking ",fn.s,"; message will be delivered twice!\n"); + log3s("warning: trouble marking ", fn.s, "; message will be delivered twice!\n"); } void del_dochan(int c) @@ -826,78 +908,80 @@ void del_dochan(int c) char ch; int i; int delnum; - - r = read(chanfdin[c],delbuf,sizeof(delbuf)); + + r = read(chanfdin[c], delbuf, sizeof(delbuf)); if (r == -1) return; - if (r == 0) { spawndied(c); return; } + if (r == 0) { + spawndied(c); + return; + } for (i = 0; i < r; ++i) { ch = delbuf[i]; - while (!stralloc_append(&dline[c],&ch)) nomem(); - - if (dline[c].len > REPORTMAX) - dline[c].len = REPORTMAX; - /* qmail-lspawn and qmail-rspawn are responsible for keeping it short */ - /* but from a security point of view, we don't trust rspawn */ + while (!stralloc_append(&dline[c], &ch)) nomem(); + + if (dline[c].len > REPORTMAX) dline[c].len = REPORTMAX; + /* qmail-lspawn and qmail-rspawn are responsible for keeping it short */ + /* but from a security point of view, we don't trust rspawn */ if (!ch && (dline[c].len > 1)) { - delnum = (unsigned int) (unsigned char) dline[c].s[0]; + delnum = (unsigned int)(unsigned char)dline[c].s[0]; if ((delnum < 0) || (delnum >= concurrency[c]) || !d[c][delnum].used) log1s("warning: internal error: delivery report out of range\n"); else { - strnum3[fmt_ulong(strnum3,d[c][delnum].delid)] = 0; + strnum3[fmt_ulong(strnum3, d[c][delnum].delid)] = 0; if (dline[c].s[1] == 'Z') - if (jo[d[c][delnum].j].flagdying) { - dline[c].s[1] = 'D'; - --dline[c].len; - while (!stralloc_cats(&dline[c],"I'm not going to try again; this message has been in the queue too long.\n")) nomem(); - while (!stralloc_0(&dline[c])) nomem(); - } + if (jo[d[c][delnum].j].flagdying) { + dline[c].s[1] = 'D'; + --dline[c].len; + while (!stralloc_cats( + &dline[c], "I'm not going to try again; this message has been in the queue too long.\n")) + nomem(); + while (!stralloc_0(&dline[c])) nomem(); + } switch (dline[c].s[1]) { case 'K': - log3s("delivery ",strnum3,": success: "); + log3s("delivery ", strnum3, ": success: "); logsafe(dline[c].s + 2); log1s("\n"); - markdone(c,jo[d[c][delnum].j].id,d[c][delnum].mpos); + markdone(c, jo[d[c][delnum].j].id, d[c][delnum].mpos); --jo[d[c][delnum].j].numtodo; - break; - case 'Z': - log3s("delivery ",strnum3,": deferral: "); - logsafe(dline[c].s + 2); - log1s("\n"); - break; - case 'D': - log3s("delivery ",strnum3,": failure: "); - logsafe(dline[c].s + 2); - log1s("\n"); - addbounce(jo[d[c][delnum].j].id,d[c][delnum].recip.s,dline[c].s + 2); - markdone(c,jo[d[c][delnum].j].id,d[c][delnum].mpos); - --jo[d[c][delnum].j].numtodo; - break; - default: - log3s("delivery ",strnum3,": report mangled, will defer\n"); - } - - job_close(d[c][delnum].j); - d[c][delnum].used = 0; --concurrencyused[c]; + break; + case 'Z': + log3s("delivery ", strnum3, ": deferral: "); + logsafe(dline[c].s + 2); + log1s("\n"); + break; + case 'D': + log3s("delivery ", strnum3, ": failure: "); + logsafe(dline[c].s + 2); + log1s("\n"); + addbounce(jo[d[c][delnum].j].id, d[c][delnum].recip.s, dline[c].s + 2); + markdone(c, jo[d[c][delnum].j].id, d[c][delnum].mpos); + --jo[d[c][delnum].j].numtodo; + break; + default: log3s("delivery ", strnum3, ": report mangled, will defer\n"); + } + + job_close(d[c][delnum].j); + d[c][delnum].used = 0; + --concurrencyused[c]; del_status(); - } dline[c].len = 0; } } } -void del_selprep(int *nfds,fd_set *rfds) +void del_selprep(int *nfds, fd_set *rfds) { int c; for (c = 0; c < CHANNELS; ++c) if (flagspawnalive[c]) { - FD_SET(chanfdin[c],rfds); - if (*nfds <= chanfdin[c]) - *nfds = chanfdin[c] + 1; + FD_SET(chanfdin[c], rfds); + if (*nfds <= chanfdin[c]) *nfds = chanfdin[c] + 1; } } @@ -907,23 +991,20 @@ void del_do(fd_set *rfds) for (c = 0; c < CHANNELS; ++c) if (flagspawnalive[c]) - if (FD_ISSET(chanfdin[c],rfds)) - del_dochan(c); + if (FD_ISSET(chanfdin[c], rfds)) del_dochan(c); } /* this file is too long -------------------------------------------- PASSES */ -struct -{ +struct { unsigned long id; /* if 0, need a new pass */ int j; /* defined if id; job number */ int fd; /* defined if id; reading from {local,remote} */ seek_pos mpos; /* defined if id; mark position */ buffer b; char buf[128]; -} -pass[CHANNELS]; +} pass[CHANNELS]; void pass_init() { @@ -937,24 +1018,25 @@ void pass_selprep(datetime_sec *wakeup) int c; struct prioq_elt pe; if (flagexitasap) return; - + for (c = 0; c < CHANNELS; ++c) if (pass[c].id) - if (del_avail(c)) - { *wakeup = 0; return; } - + if (del_avail(c)) { + *wakeup = 0; + return; + } + if (job_avail()) for (c = 0; c < CHANNELS; ++c) if (!pass[c].id) - if (prioq_min(&pqchan[c],&pe)) + if (prioq_min(&pqchan[c], &pe)) if (*wakeup > pe.dt) *wakeup = pe.dt; - - if (prioq_min(&pqfail,&pe)) - if (*wakeup > pe.dt) - *wakeup = pe.dt; - if (prioq_min(&pqdone,&pe)) - if (*wakeup > pe.dt)*wakeup = pe.dt; + if (prioq_min(&pqfail, &pe)) + if (*wakeup > pe.dt) *wakeup = pe.dt; + + if (prioq_min(&pqdone, &pe)) + if (*wakeup > pe.dt) *wakeup = pe.dt; } static datetime_sec squareroot(datetime_sec x) /* result^2 <= x < (result + 1)^2 ; assuming: >= 0 */ @@ -964,20 +1046,26 @@ static datetime_sec squareroot(datetime_sec x) /* result^2 <= x < (result + 1)^2 datetime_sec y21; int j; - y = 0; yy = 0; + y = 0; + yy = 0; for (j = 15; j >= 0; --j) { y21 = (y << (j + 1)) + (1 << (j + j)); - if (y21 <= x - yy) { y += (1 << j); yy += y21; } + if (y21 <= x - yy) { + y += (1 << j); + yy += y21; + } } return y; } -datetime_sec nextretry(datetime_sec birth,int c) +datetime_sec nextretry(datetime_sec birth, int c) { int n; - if (birth > recent) n = 0; - else n = squareroot(recent - birth); /* no need to add fuzz to recent */ + if (birth > recent) + n = 0; + else + n = squareroot(recent - birth); /* no need to add fuzz to recent */ n += chanskip[c]; return birth + n * n; @@ -994,28 +1082,31 @@ void pass_dochan(int c) if (!pass[c].id) { if (!job_avail()) return; - if (!prioq_min(&pqchan[c],&pe)) return; + if (!prioq_min(&pqchan[c], &pe)) return; if (pe.dt > recent) return; - fnmake_chanaddr(pe.id,c); + fnmake_chanaddr(pe.id, c); prioq_delmin(&pqchan[c]); pass[c].mpos = 0; pass[c].fd = open_read(fn.s); if (pass[c].fd == -1) goto trouble; - if (!getinfo(&line,&birth,pe.id)) { close(pass[c].fd); goto trouble; } + if (!getinfo(&line, &birth, pe.id)) { + close(pass[c].fd); + goto trouble; + } pass[c].id = pe.id; - buffer_init(&pass[c].b,read,pass[c].fd,pass[c].buf,sizeof(pass[c].buf)); - pass[c].j = job_open(pe.id,c); - jo[pass[c].j].retry = nextretry(birth,c); + buffer_init(&pass[c].b, read, pass[c].fd, pass[c].buf, sizeof(pass[c].buf)); + pass[c].j = job_open(pe.id, c); + jo[pass[c].j].retry = nextretry(birth, c); jo[pass[c].j].flagdying = (recent > birth + lifetime); - while (!stralloc_copy(&jo[pass[c].j].sender,&line)) nomem(); + while (!stralloc_copy(&jo[pass[c].j].sender, &line)) nomem(); } if (!del_avail(c)) return; - if (getln(&pass[c].b,&line,&match,'\0') == -1) { - fnmake_chanaddr(pass[c].id,c); - log3s("warning: trouble reading ",fn.s,"; will try again later\n"); + if (getln(&pass[c].b, &line, &match, '\0') == -1) { + fnmake_chanaddr(pass[c].id, c); + log3s("warning: trouble reading ", fn.s, "; will try again later\n"); close(pass[c].fd); job_close(pass[c].j); pass[c].id = 0; @@ -1033,26 +1124,25 @@ void pass_dochan(int c) switch (line.s[0]) { case 'T': ++jo[pass[c].j].numtodo; - del_start(pass[c].j,pass[c].mpos,line.s + 1); - break; - case 'D': + del_start(pass[c].j, pass[c].mpos, line.s + 1); break; + case 'D': break; default: - fnmake_chanaddr(pass[c].id,c); - log3s("warning: unknown record type in ",fn.s,"!\n"); + fnmake_chanaddr(pass[c].id, c); + log3s("warning: unknown record type in ", fn.s, "!\n"); close(pass[c].fd); job_close(pass[c].j); pass[c].id = 0; return; - } + } pass[c].mpos += line.len; return; - trouble: - log3s("warning: trouble opening ",fn.s,"; will try again later\n"); +trouble: + log3s("warning: trouble opening ", fn.s, "; will try again later\n"); pe.dt = recent + SLEEP_SYSFAIL; - while (!prioq_insert(&pqchan[c],&pe)) nomem(); + while (!prioq_insert(&pqchan[c], &pe)) nomem(); } void messdone(unsigned long id) @@ -1063,53 +1153,59 @@ void messdone(unsigned long id) struct stat st; for (c = 0; c < CHANNELS; ++c) { - fnmake_chanaddr(id,c); - if (stat(fn.s,&st) == 0) return; /* false alarm; consequence of HOPEFULLY */ + fnmake_chanaddr(id, c); + if (stat(fn.s, &st) == 0) return; /* false alarm; consequence of HOPEFULLY */ if (errno != ENOENT) { - log3s("warning: unable to stat ",fn.s,"; will try again later\n"); + log3s("warning: unable to stat ", fn.s, "; will try again later\n"); goto FAIL; } } fnmake_todo(id); - if (stat(fn.s,&st) == 0) return; + if (stat(fn.s, &st) == 0) return; if (errno != ENOENT) { - log3s("warning: unable to stat ",fn.s,"; will try again later\n"); + log3s("warning: unable to stat ", fn.s, "; will try again later\n"); goto FAIL; - } - + } + fnmake_info(id); - if (stat(fn.s,&st) == -1) { + if (stat(fn.s, &st) == -1) { if (errno == ENOENT) return; - log3s("warning: unable to stat ",fn.s,"; will try again later\n"); + log3s("warning: unable to stat ", fn.s, "; will try again later\n"); goto FAIL; } - - /* -todo +info -local -remote ?bounce */ - if (!injectbounce(id)) - goto FAIL; /* injectbounce() produced error message */ - strnum3[fmt_ulong(strnum3,id)] = 0; - log3s("end msg ",strnum3,"\n"); + /* -todo +info -local -remote ?bounce */ + if (!injectbounce(id)) goto FAIL; /* injectbounce() produced error message */ + + strnum3[fmt_ulong(strnum3, id)] = 0; + log3s("end msg ", strnum3, "\n"); - /* -todo +info -local -remote -bounce */ + /* -todo +info -local -remote -bounce */ fnmake_info(id); if (unlink(fn.s) == -1) { - log3s("warning: unable to unlink ",fn.s,"; will try again later\n"); + log3s("warning: unable to unlink ", fn.s, "; will try again later\n"); goto FAIL; } - /* -todo -info -local -remote -bounce; we can relax */ + /* -todo -info -local -remote -bounce; we can relax */ fnmake_foop(id); - if (buffer_putflush(&toqc,fn.s,fn.len) == -1) { cleandied(); return; } - if (buffer_get(&fromqc,&ch,1) != 1) { cleandied(); return; } - if (ch != '+') log3s("warning: qmail-clean unable to clean up ",fn.s,"\n"); + if (buffer_putflush(&toqc, fn.s, fn.len) == -1) { + cleandied(); + return; + } + if (buffer_get(&fromqc, &ch, 1) != 1) { + cleandied(); + return; + } + if (ch != '+') log3s("warning: qmail-clean unable to clean up ", fn.s, "\n"); return; - FAIL: - pe.id = id; pe.dt = now() + SLEEP_SYSFAIL; - while (!prioq_insert(&pqdone,&pe)) nomem(); +FAIL: + pe.id = id; + pe.dt = now() + SLEEP_SYSFAIL; + while (!prioq_insert(&pqdone, &pe)) nomem(); } void pass_do() @@ -1117,20 +1213,19 @@ void pass_do() int c; struct prioq_elt pe; - for (c = 0; c < CHANNELS; ++c) - pass_dochan(c); + for (c = 0; c < CHANNELS; ++c) pass_dochan(c); - if (prioq_min(&pqfail,&pe)) + if (prioq_min(&pqfail, &pe)) if (pe.dt <= recent) { prioq_delmin(&pqfail); pqadd(pe.id); } - if (prioq_min(&pqdone,&pe)) + if (prioq_min(&pqdone, &pe)) if (pe.dt <= recent) { prioq_delmin(&pqdone); messdone(pe.id); - } + } } @@ -1142,10 +1237,11 @@ int todofdin; int todofdout; int flagtodoalive; -void tododied() { - log1s("alert: lost connection to qmail-todo ... exiting\n"); - flagexitasap = 1; - flagtodoalive = 0; +void tododied() +{ + log1s("alert: lost connection to qmail-todo ... exiting\n"); + flagexitasap = 1; + flagtodoalive = 0; } void todo_init() @@ -1154,72 +1250,64 @@ void todo_init() todofdin = 8; flagtodoalive = 1; /* sync with external todo */ - if (write(todofdout,"S",1) != 1) tododied(); - + if (write(todofdout, "S", 1) != 1) tododied(); + return; } -void todo_selprep(int *nfds,fd_set *rfds,datetime_sec *wakeup) +void todo_selprep(int *nfds, fd_set *rfds, datetime_sec *wakeup) { if (flagexitasap) { if (flagtodoalive) { - write(todofdout,"X",1); + write(todofdout, "X", 1); } } if (flagtodoalive) { - FD_SET(todofdin,rfds); - if (*nfds <= todofdin) - *nfds = todofdin + 1; + FD_SET(todofdin, rfds); + if (*nfds <= todofdin) *nfds = todofdin + 1; } } -void todo_del(char* s) +void todo_del(char *s) { - int flagchan[CHANNELS]; - struct prioq_elt pe; - unsigned long id; - unsigned int len; - int c; - - for (c = 0; c < CHANNELS; ++c) - flagchan[c] = 0; - - switch (*s++) { - case 'L': - flagchan[0] = 1; - break; - case 'R': - flagchan[1] = 1; - break; - case 'B': - flagchan[0] = 1; - flagchan[1] = 1; - break; - case 'X': - break; - default: + int flagchan[CHANNELS]; + struct prioq_elt pe; + unsigned long id; + unsigned int len; + int c; + + for (c = 0; c < CHANNELS; ++c) flagchan[c] = 0; + + switch (*s++) { + case 'L': flagchan[0] = 1; break; + case 'R': flagchan[1] = 1; break; + case 'B': + flagchan[0] = 1; + flagchan[1] = 1; + break; + case 'X': break; + default: log1s("warning: qmail-send unable to understand qmail-todo\n"); return; + } + + len = scan_ulong(s, &id); + if (!len || s[len]) { log1s("warning: qmail-send unable to understand qmail-todo\n"); return; - } - - len = scan_ulong(s,&id); - if (!len || s[len]) { - log1s("warning: qmail-send unable to understand qmail-todo\n"); - return; - } + } - pe.id = id; pe.dt = now(); - for (c = 0; c < CHANNELS; ++c) - if (flagchan[c]) - while (!prioq_insert(&pqchan[c],&pe)) nomem(); + pe.id = id; + pe.dt = now(); + for (c = 0; c < CHANNELS; ++c) + if (flagchan[c]) + while (!prioq_insert(&pqchan[c], &pe)) nomem(); - for (c = 0; c < CHANNELS; ++c) - if (flagchan[c]) break; + for (c = 0; c < CHANNELS; ++c) + if (flagchan[c]) break; - if (c == CHANNELS) - while (!prioq_insert(&pqdone,&pe)) nomem(); + if (c == CHANNELS) + while (!prioq_insert(&pqdone, &pe)) nomem(); - return; + return; } void todo_do(fd_set *rfds) @@ -1227,11 +1315,11 @@ void todo_do(fd_set *rfds) int r; char ch; int i; - + if (!flagtodoalive) return; - if (!FD_ISSET(todofdin,rfds)) return; + if (!FD_ISSET(todofdin, rfds)) return; - r = read(todofdin,todobuf,sizeof(todobuf)); + r = read(todofdin, todobuf, sizeof(todobuf)); if (r == -1) return; if (r == 0) { if (flagexitasap) @@ -1243,28 +1331,23 @@ void todo_do(fd_set *rfds) for (i = 0; i < r; ++i) { ch = todobuf[i]; - while (!stralloc_append(&todoline,&ch)) nomem(); - if (todoline.len > REPORTMAX) - todoline.len = REPORTMAX; - /* qmail-todo is responsible for keeping it short */ + while (!stralloc_append(&todoline, &ch)) nomem(); + if (todoline.len > REPORTMAX) todoline.len = REPORTMAX; + /* qmail-todo is responsible for keeping it short */ if (!ch && (todoline.len > 1)) { switch (todoline.s[0]) { - case 'D': - if (flagexitasap) break; - todo_del(todoline.s + 1); - break; - case 'L': - log1s(todoline.s + 1); - break; - case 'X': - if (flagexitasap) - flagtodoalive = 0; - else - tododied(); - break; - default: - log1s("warning: qmail-send unable to understand qmail-todo: report mangled\n"); - break; + case 'D': + if (flagexitasap) break; + todo_del(todoline.s + 1); + break; + case 'L': log1s(todoline.s + 1); break; + case 'X': + if (flagexitasap) + flagtodoalive = 0; + else + tododied(); + break; + default: log1s("warning: qmail-send unable to understand qmail-todo: report mangled\n"); break; } todoline.len = 0; } @@ -1273,34 +1356,42 @@ void todo_do(fd_set *rfds) /* this file is too long ---------------------------------------------- MAIN */ -int getcontrols() -{ +int getcontrols() +{ if (control_init() == -1) return 0; - if (control_readint(&lifetime,"control/queuelifetime") == -1) return 0; - if (control_readint(&concurrency[0],"control/concurrencylocal") == -1) return 0; - if (control_readint(&concurrency[1],"control/concurrencyremote") == -1) return 0; - if (control_rldef(&envnoathost,"control/envnoathost",1,"envnoathost") != 1) return 0; - if (control_rldef(&bouncefrom,"control/bouncefrom",0,"MAILER-DAEMON") != 1) return 0; - if (control_rldef(&bouncehost,"control/bouncehost",1,"bouncehost") != 1) return 0; - if (control_readint(&bouncemaxbytes,"control/bouncemaxbytes") == -1) return 0; - if (control_rldef(&doublebouncehost,"control/doublebouncehost",1,"doublebouncehost") != 1) return 0; - if (control_rldef(&doublebounceto,"control/doublebounceto",0,"postmaster") != 1) return 0; - if (!stralloc_cats(&doublebounceto,"@")) return 0; - if (!stralloc_cat(&doublebounceto,&doublebouncehost)) return 0; + if (control_readint(&lifetime, "control/queuelifetime") == -1) return 0; + if (control_readint(&concurrency[0], "control/concurrencylocal") == -1) return 0; + if (control_readint(&concurrency[1], "control/concurrencyremote") == -1) return 0; + if (control_rldef(&envnoathost, "control/envnoathost", 1, "envnoathost") != 1) return 0; + if (control_rldef(&bouncefrom, "control/bouncefrom", 0, "MAILER-DAEMON") != 1) return 0; + if (control_rldef(&bouncehost, "control/bouncehost", 1, "bouncehost") != 1) return 0; + if (control_readint(&bouncemaxbytes, "control/bouncemaxbytes") == -1) return 0; + if (control_rldef(&doublebouncehost, "control/doublebouncehost", 1, "doublebouncehost") != 1) return 0; + if (control_rldef(&doublebounceto, "control/doublebounceto", 0, "postmaster") != 1) return 0; + if (!stralloc_cats(&doublebounceto, "@")) return 0; + if (!stralloc_cat(&doublebounceto, &doublebouncehost)) return 0; if (!stralloc_0(&doublebounceto)) return 0; - if (control_readfile(&locals,"control/locals",1) != 1) return 0; - if (!constmap_init(&maplocals,locals.s,locals.len,0)) return 0; - switch (control_readfile(&percenthack,"control/percenthack",0)) { + if (control_readfile(&locals, "control/locals", 1) != 1) return 0; + if (!constmap_init(&maplocals, locals.s, locals.len, 0)) return 0; + switch (control_readfile(&percenthack, "control/percenthack", 0)) { case -1: return 0; - case 0: if (!constmap_init(&mappercenthack,"",0,0)) return 0; break; - case 1: if (!constmap_init(&mappercenthack,percenthack.s,percenthack.len,0)) return 0; break; + case 0: + if (!constmap_init(&mappercenthack, "", 0, 0)) return 0; + break; + case 1: + if (!constmap_init(&mappercenthack, percenthack.s, percenthack.len, 0)) return 0; + break; } - switch (control_readfile(&vdoms,"control/virtualdomains",0)) { + switch (control_readfile(&vdoms, "control/virtualdomains", 0)) { case -1: return 0; - case 0: if (!constmap_init(&mapvdoms,"",0,1)) return 0; break; - case 1: if (!constmap_init(&mapvdoms,vdoms.s,vdoms.len,1)) return 0; break; + case 0: + if (!constmap_init(&mapvdoms, "", 0, 1)) return 0; + break; + case 1: + if (!constmap_init(&mapvdoms, vdoms.s, vdoms.len, 1)) return 0; + break; } - return 1; + return 1; } stralloc newlocals = {0}; @@ -1310,25 +1401,40 @@ void regetcontrols() { int r; - if (control_readfile(&newlocals,"control/locals",1) != 1) { log1s("alert: unable to reread control/locals\n"); return; } - if (control_readint(&concurrency[0],"control/concurrencylocal") == -1) { log1s("alert: unable to reread control/concurrencylocal\n"); return; } - if (control_readint(&concurrency[1],"control/concurrencyremote") == -1) { log1s("alert: unable to reread control/concurrencyremote\n"); return; } - if (control_readint(&lifetime,"control/queuelifetime") == -1) { log1s("alert: unable to reread control/queuelifetime\n"); return; } + if (control_readfile(&newlocals, "control/locals", 1) != 1) { + log1s("alert: unable to reread control/locals\n"); + return; + } + if (control_readint(&concurrency[0], "control/concurrencylocal") == -1) { + log1s("alert: unable to reread control/concurrencylocal\n"); + return; + } + if (control_readint(&concurrency[1], "control/concurrencyremote") == -1) { + log1s("alert: unable to reread control/concurrencyremote\n"); + return; + } + if (control_readint(&lifetime, "control/queuelifetime") == -1) { + log1s("alert: unable to reread control/queuelifetime\n"); + return; + } - r = control_readfile(&newvdoms,"control/virtualdomains",0); - if (r == -1) { log1s("alert: unable to reread control/virtualdomains\n"); return; } + r = control_readfile(&newvdoms, "control/virtualdomains", 0); + if (r == -1) { + log1s("alert: unable to reread control/virtualdomains\n"); + return; + } constmap_free(&maplocals); constmap_free(&mapvdoms); - while (!stralloc_copy(&locals,&newlocals)) nomem(); - while (!constmap_init(&maplocals,locals.s,locals.len,0)) nomem(); + while (!stralloc_copy(&locals, &newlocals)) nomem(); + while (!constmap_init(&maplocals, locals.s, locals.len, 0)) nomem(); if (r) { - while (!stralloc_copy(&vdoms,&newvdoms)) nomem(); - while (!constmap_init(&mapvdoms,vdoms.s,vdoms.len,1)) nomem(); + while (!stralloc_copy(&vdoms, &newvdoms)) nomem(); + while (!constmap_init(&mapvdoms, vdoms.s, vdoms.len, 1)) nomem(); } else - while (!constmap_init(&mapvdoms,"",0,1)) nomem(); + while (!constmap_init(&mapvdoms, "", 0, 1)) nomem(); } void reread() @@ -1337,7 +1443,7 @@ void reread() log1s("alert: unable to reread controls: unable to switch to home directory\n"); return; } - write(todofdout,"H",1); + write(todofdout, "H", 1); regetcontrols(); while (chdir("queue") == -1) { @@ -1359,9 +1465,18 @@ int main() int r; char ch; - if (chdir(auto_qmail) == -1) { log1s("alert: cannot start: unable to switch to home directory\n"); _exit(110); } - if (!getcontrols()) { log1s("alert: cannot start: unable to read controls\n"); _exit(111); } - if (chdir("queue") == -1) { log1s("alert: cannot start: unable to switch to queue directory\n"); _exit(110); } + if (chdir(auto_qmail) == -1) { + log1s("alert: cannot start: unable to switch to home directory\n"); + _exit(110); + } + if (!getcontrols()) { + log1s("alert: cannot start: unable to read controls\n"); + _exit(111); + } + if (chdir("queue") == -1) { + log1s("alert: cannot start: unable to switch to queue directory\n"); + _exit(110); + } sig_pipeignore(); sig_termcatch(sigterm); sig_alarmcatch(sigalrm); @@ -1370,18 +1485,26 @@ int main() umask(077); fd = open_write("lock/sendmutex"); - if (fd == -1) { log1s("alert: cannot start: unable to open mutex\n"); _exit(111); } - if (lock_exnb(fd) == -1) { log1s("alert: cannot start: qmail-send is already running\n"); _exit(111); } + if (fd == -1) { + log1s("alert: cannot start: unable to open mutex\n"); + _exit(111); + } + if (lock_exnb(fd) == -1) { + log1s("alert: cannot start: qmail-send is already running\n"); + _exit(111); + } numjobs = 0; - for (c = 0;c < CHANNELS;++c) { - do - r = read(chanfdin[c],&ch,1); + for (c = 0; c < CHANNELS; ++c) { + do r = read(chanfdin[c], &ch, 1); while ((r == -1) && (errno == EINTR)); - if (r < 1) { log1s("alert: cannot start: hath the daemon spawn no fire?\n"); _exit(111); } + if (r < 1) { + log1s("alert: cannot start: hath the daemon spawn no fire?\n"); + _exit(111); + } - u = (unsigned int) (unsigned char) ch; + u = (unsigned int)(unsigned char)ch; if (concurrency[c] > u) concurrency[c] = u; numjobs += concurrency[c]; } @@ -1400,25 +1523,33 @@ int main() while (!flagexitasap || !del_canexit() || flagtodoalive) { recent = now(); - if (flagrunasap) { flagrunasap = 0; pqrun(); } - if (flagreadasap) { flagreadasap = 0; reread(); } + if (flagrunasap) { + flagrunasap = 0; + pqrun(); + } + if (flagreadasap) { + flagreadasap = 0; + reread(); + } wakeup = recent + SLEEP_FOREVER; FD_ZERO(&rfds); FD_ZERO(&wfds); nfds = 1; - comm_selprep(&nfds,&wfds); - del_selprep(&nfds,&rfds); + comm_selprep(&nfds, &wfds); + del_selprep(&nfds, &rfds); pass_selprep(&wakeup); - todo_selprep(&nfds,&rfds,&wakeup); + todo_selprep(&nfds, &rfds, &wakeup); cleanup_selprep(&wakeup); - if (wakeup <= recent) tv.tv_sec = 0; - else tv.tv_sec = wakeup - recent + SLEEP_FUZZ; + if (wakeup <= recent) + tv.tv_sec = 0; + else + tv.tv_sec = wakeup - recent + SLEEP_FUZZ; tv.tv_usec = 0; - if (select(nfds,&rfds,&wfds,(fd_set *) 0,&tv) == -1) + if (select(nfds, &rfds, &wfds, (fd_set *)0, &tv) == -1) if (errno == EINTR) ; else @@ -1433,7 +1564,7 @@ int main() cleanup_do(); } } - + pqfinish(); log1s("status: exiting\n"); _exit(0); diff --git a/src/qmail-showctl.c b/src/qmail-showctl.c index 2b4cc0a..7aaeb95 100644 --- a/src/qmail-showctl.c +++ b/src/qmail-showctl.c @@ -1,21 +1,23 @@ -#include <sys/types.h> #include <sys/stat.h> +#include <sys/types.h> #include <unistd.h> -#include "spf.h" + #include "buffer.h" +#include "constmap.h" +#include "direntry.h" #include "exit.h" #include "fmt.h" #include "str.h" -#include "control.h" -#include "constmap.h" #include "stralloc.h" -#include "direntry.h" -#include "auto_uids.h" -#include "auto_qmail.h" + #include "auto_break.h" #include "auto_patrn.h" +#include "auto_qmail.h" #include "auto_spawn.h" #include "auto_split.h" +#include "auto_uids.h" +#include "control.h" +#include "spf.h" stralloc me = {0}; int meok; @@ -23,103 +25,97 @@ int meok; stralloc line = {0}; char num[FMT_ULONG]; -void safeput(char *buf,unsigned int len) +void safeput(char *buf, unsigned int len) { char ch; while (len > 0) { ch = *buf; if ((ch < 32) || (ch > 126)) ch = '?'; - buffer_put(buffer_1,&ch,1); + buffer_put(buffer_1, &ch, 1); ++buf; --len; } } -void do_int(char *fn,char *def,char *pre,char *post) +void do_int(char *fn, char *def, char *pre, char *post) { int i; - buffer_puts(buffer_1,"\n"); - buffer_puts(buffer_1,fn); - buffer_puts(buffer_1,": "); - switch (control_readint(&i,fn)) { + buffer_puts(buffer_1, "\n"); + buffer_puts(buffer_1, fn); + buffer_puts(buffer_1, ": "); + switch (control_readint(&i, fn)) { case 0: - buffer_puts(buffer_1,"(Default.) "); - buffer_puts(buffer_1,pre); - buffer_puts(buffer_1,def); - buffer_puts(buffer_1,post); - buffer_puts(buffer_1,".\n"); + buffer_puts(buffer_1, "(Default.) "); + buffer_puts(buffer_1, pre); + buffer_puts(buffer_1, def); + buffer_puts(buffer_1, post); + buffer_puts(buffer_1, ".\n"); break; case 1: if (i < 0) i = 0; - buffer_puts(buffer_1,pre); - buffer_put(buffer_1,num,fmt_uint(num,i)); - buffer_puts(buffer_1,post); - buffer_puts(buffer_1,".\n"); - break; - default: - buffer_puts(buffer_1,"Oops! Trouble reading this file.\n"); + buffer_puts(buffer_1, pre); + buffer_put(buffer_1, num, fmt_uint(num, i)); + buffer_puts(buffer_1, post); + buffer_puts(buffer_1, ".\n"); break; + default: buffer_puts(buffer_1, "Oops! Trouble reading this file.\n"); break; } } -void do_str(char *fn,int flagme,char *def,char *pre) +void do_str(char *fn, int flagme, char *def, char *pre) { - buffer_puts(buffer_1,"\n"); - buffer_puts(buffer_1,fn); - buffer_puts(buffer_1,": "); - switch (control_readline(&line,fn)) { + buffer_puts(buffer_1, "\n"); + buffer_puts(buffer_1, fn); + buffer_puts(buffer_1, ": "); + switch (control_readline(&line, fn)) { case 0: - buffer_puts(buffer_1,"(Default.) "); - if (!stralloc_copys(&line,def)) { - buffer_puts(buffer_1,"Oops! Out of memory.\n"); + buffer_puts(buffer_1, "(Default.) "); + if (!stralloc_copys(&line, def)) { + buffer_puts(buffer_1, "Oops! Out of memory.\n"); break; }; if (flagme && meok) - if (!stralloc_copy(&line,&me)) { - buffer_puts(buffer_1,"Oops! Out of memory.\n"); + if (!stralloc_copy(&line, &me)) { + buffer_puts(buffer_1, "Oops! Out of memory.\n"); break; }; case 1: - buffer_puts(buffer_1,pre); - safeput(line.s,line.len); - buffer_puts(buffer_1,".\n"); - break; - default: - buffer_puts(buffer_1,"Oops! Trouble reading this file.\n"); + buffer_puts(buffer_1, pre); + safeput(line.s, line.len); + buffer_puts(buffer_1, ".\n"); break; + default: buffer_puts(buffer_1, "Oops! Trouble reading this file.\n"); break; } } -int do_lst(char *fn,char *def,char *pre,char *post) +int do_lst(char *fn, char *def, char *pre, char *post) { int i; int j; - buffer_puts(buffer_1,"\n"); - buffer_puts(buffer_1,fn); - buffer_puts(buffer_1,": "); - switch (control_readfile(&line,fn,0)) { + buffer_puts(buffer_1, "\n"); + buffer_puts(buffer_1, fn); + buffer_puts(buffer_1, ": "); + switch (control_readfile(&line, fn, 0)) { case 0: - buffer_puts(buffer_1,"(Default.) "); - buffer_puts(buffer_1,def); - buffer_puts(buffer_1,"\n"); + buffer_puts(buffer_1, "(Default.) "); + buffer_puts(buffer_1, def); + buffer_puts(buffer_1, "\n"); return 0; case 1: - buffer_puts(buffer_1,"\n"); + buffer_puts(buffer_1, "\n"); i = 0; for (j = 0; j < line.len; ++j) - if (!line.s[j]) { - buffer_puts(buffer_1,pre); - safeput(line.s + i,j - i); - buffer_puts(buffer_1,post); - buffer_puts(buffer_1,"\n"); - i = j + 1; - } + if (!line.s[j]) { + buffer_puts(buffer_1, pre); + safeput(line.s + i, j - i); + buffer_puts(buffer_1, post); + buffer_puts(buffer_1, "\n"); + i = j + 1; + } return 1; - default: - buffer_puts(buffer_1,"Oops! Trouble reading this file.\n"); - return -1; + default: buffer_puts(buffer_1, "Oops! Trouble reading this file.\n"); return -1; } } @@ -130,241 +126,257 @@ int main() struct stat stmrh; struct stat stmrhcdb; - buffer_puts(buffer_1,"s/qmail home directory: "); - buffer_puts(buffer_1,auto_qmail); - buffer_puts(buffer_1,".\n"); + buffer_puts(buffer_1, "s/qmail home directory: "); + buffer_puts(buffer_1, auto_qmail); + buffer_puts(buffer_1, ".\n"); - buffer_puts(buffer_1,"user-ext delimiter: "); - buffer_puts(buffer_1,auto_break); - buffer_puts(buffer_1,".\n"); + buffer_puts(buffer_1, "user-ext delimiter: "); + buffer_puts(buffer_1, auto_break); + buffer_puts(buffer_1, ".\n"); - buffer_puts(buffer_1,"paternalism (in decimal): "); - buffer_put(buffer_1,num,fmt_ulong(num,(unsigned long) auto_patrn)); - buffer_puts(buffer_1,".\n"); + buffer_puts(buffer_1, "paternalism (in decimal): "); + buffer_put(buffer_1, num, fmt_ulong(num, (unsigned long)auto_patrn)); + buffer_puts(buffer_1, ".\n"); - buffer_puts(buffer_1,"silent concurrency limit: "); - buffer_put(buffer_1,num,fmt_ulong(num,(unsigned long) auto_spawn)); - buffer_puts(buffer_1,".\n"); + buffer_puts(buffer_1, "silent concurrency limit: "); + buffer_put(buffer_1, num, fmt_ulong(num, (unsigned long)auto_spawn)); + buffer_puts(buffer_1, ".\n"); - buffer_puts(buffer_1,"subdirectory split: "); - buffer_put(buffer_1,num,fmt_ulong(num,(unsigned long) auto_split)); - buffer_puts(buffer_1,".\n"); + buffer_puts(buffer_1, "subdirectory split: "); + buffer_put(buffer_1, num, fmt_ulong(num, (unsigned long)auto_split)); + buffer_puts(buffer_1, ".\n"); - buffer_puts(buffer_1,"user ids: "); - buffer_put(buffer_1,num,fmt_ulong(num,(unsigned long) auto_uida)); - buffer_puts(buffer_1,", "); - buffer_put(buffer_1,num,fmt_ulong(num,(unsigned long) auto_uidd)); - buffer_puts(buffer_1,", "); - buffer_put(buffer_1,num,fmt_ulong(num,(unsigned long) auto_uidl)); - buffer_puts(buffer_1,", "); - buffer_put(buffer_1,num,fmt_ulong(num,(unsigned long) auto_uido)); - buffer_puts(buffer_1,", "); - buffer_put(buffer_1,num,fmt_ulong(num,(unsigned long) auto_uidp)); - buffer_puts(buffer_1,", "); - buffer_put(buffer_1,num,fmt_ulong(num,(unsigned long) auto_uidq)); - buffer_puts(buffer_1,", "); - buffer_put(buffer_1,num,fmt_ulong(num,(unsigned long) auto_uidr)); - buffer_puts(buffer_1,", "); - buffer_put(buffer_1,num,fmt_ulong(num,(unsigned long) auto_uids)); - buffer_puts(buffer_1,".\n"); + buffer_puts(buffer_1, "user ids: "); + buffer_put(buffer_1, num, fmt_ulong(num, (unsigned long)auto_uida)); + buffer_puts(buffer_1, ", "); + buffer_put(buffer_1, num, fmt_ulong(num, (unsigned long)auto_uidd)); + buffer_puts(buffer_1, ", "); + buffer_put(buffer_1, num, fmt_ulong(num, (unsigned long)auto_uidl)); + buffer_puts(buffer_1, ", "); + buffer_put(buffer_1, num, fmt_ulong(num, (unsigned long)auto_uido)); + buffer_puts(buffer_1, ", "); + buffer_put(buffer_1, num, fmt_ulong(num, (unsigned long)auto_uidp)); + buffer_puts(buffer_1, ", "); + buffer_put(buffer_1, num, fmt_ulong(num, (unsigned long)auto_uidq)); + buffer_puts(buffer_1, ", "); + buffer_put(buffer_1, num, fmt_ulong(num, (unsigned long)auto_uidr)); + buffer_puts(buffer_1, ", "); + buffer_put(buffer_1, num, fmt_ulong(num, (unsigned long)auto_uids)); + buffer_puts(buffer_1, ".\n"); - buffer_puts(buffer_1,"group ids: "); - buffer_put(buffer_1,num,fmt_ulong(num,(unsigned long) auto_gidn)); - buffer_puts(buffer_1,", "); - buffer_put(buffer_1,num,fmt_ulong(num,(unsigned long) auto_gidq)); - buffer_puts(buffer_1,".\n"); + buffer_puts(buffer_1, "group ids: "); + buffer_put(buffer_1, num, fmt_ulong(num, (unsigned long)auto_gidn)); + buffer_puts(buffer_1, ", "); + buffer_put(buffer_1, num, fmt_ulong(num, (unsigned long)auto_gidq)); + buffer_puts(buffer_1, ".\n"); if (chdir(auto_qmail) == -1) { - buffer_puts(buffer_1,"Oops! Unable to chdir to "); - buffer_puts(buffer_1,auto_qmail); - buffer_puts(buffer_1,".\n"); + buffer_puts(buffer_1, "Oops! Unable to chdir to "); + buffer_puts(buffer_1, auto_qmail); + buffer_puts(buffer_1, ".\n"); buffer_flush(buffer_1); _exit(110); } if (chdir("control") == -1) { - buffer_puts(buffer_1,"Oops! Unable to chdir to control.\n"); + buffer_puts(buffer_1, "Oops! Unable to chdir to control.\n"); buffer_flush(buffer_1); _exit(110); } dir = opendir("."); if (!dir) { - buffer_puts(buffer_1,"Oops! Unable to open current directory.\n"); + buffer_puts(buffer_1, "Oops! Unable to open current directory.\n"); buffer_flush(buffer_1); _exit(110); } - meok = control_readline(&me,"me"); + meok = control_readline(&me, "me"); if (meok == -1) { - buffer_puts(buffer_1,"Oops! Trouble reading control/me."); + buffer_puts(buffer_1, "Oops! Trouble reading control/me."); buffer_flush(buffer_1); _exit(112); } - do_lst("authsenders","No authenticated SMTP senders.","Authenticated SMTP senders: ",""); - do_lst("badhelo","Any HELO/EHLO greeting is allowed.",""," not accepted in HELO/EHLO; exception token is '!'."); - do_lst("badmailfrom","Any MAIL FROM is allowed.",""," are rejected or treated special in MAIL FROM depending on tokens: '!', '?', '=', '~', '+'."); - do_lst("badloadertypes","Any loader types are accepted.",""," not accepted as loader type."); + do_lst("authsenders", "No authenticated SMTP senders.", "Authenticated SMTP senders: ", ""); + do_lst( + "badhelo", + "Any HELO/EHLO greeting is allowed.", + "", + " not accepted in HELO/EHLO; exception token is '!'."); + do_lst( + "badmailfrom", + "Any MAIL FROM is allowed.", + "", + " are rejected or treated special in MAIL FROM depending on tokens: '!', '?', '=', '~', '+'."); + do_lst("badloadertypes", "Any loader types are accepted.", "", " not accepted as loader type."); /* XXX: check badloadertypes.cdb contents */ - buffer_puts(buffer_1,"\nbadloadertypes.cdb: "); - if (stat("badloadertypes",&stmrh) == -1) - if (stat("badloadertypes.cdb",&stmrhcdb) == -1) - buffer_puts(buffer_1,"(Default.) No effect.\n"); + buffer_puts(buffer_1, "\nbadloadertypes.cdb: "); + if (stat("badloadertypes", &stmrh) == -1) + if (stat("badloadertypes.cdb", &stmrhcdb) == -1) + buffer_puts(buffer_1, "(Default.) No effect.\n"); else - buffer_puts(buffer_1,"Oops! badloadertypes.cdb exists but badloadertypes doesn't.\n"); + buffer_puts(buffer_1, "Oops! badloadertypes.cdb exists but badloadertypes doesn't.\n"); + else if (stat("badloadertypes.cdb", &stmrhcdb) == -1) + buffer_puts(buffer_1, "Oops! badloadertypes exists but badloadertypes.cdb doesn't.\n"); + else if (stmrh.st_mtime > stmrhcdb.st_mtime) + buffer_puts(buffer_1, "Oops! badloadertypes.cdb is older than badloadertypes.\n"); else - if (stat("badloadertypes.cdb",&stmrhcdb) == -1) - buffer_puts(buffer_1,"Oops! badloadertypes exists but badloadertypes.cdb doesn't.\n"); - else - if (stmrh.st_mtime > stmrhcdb.st_mtime) - buffer_puts(buffer_1,"Oops! badloadertypes.cdb is older than badloadertypes.\n"); - else - buffer_puts(buffer_1,"Modified recently enough; hopefully up to date.\n"); - do_lst("badmimetypes","Any MIME types are accepted.",""," not accepted as MIME type."); + buffer_puts(buffer_1, "Modified recently enough; hopefully up to date.\n"); + do_lst("badmimetypes", "Any MIME types are accepted.", "", " not accepted as MIME type."); /* XXX: check badmimetypes.cdb contents */ - buffer_puts(buffer_1,"\nbadmimetypes.cdb: "); - if (stat("badmimetypes",&stmrh) == -1) - if (stat("badmimetypes.cdb",&stmrhcdb) == -1) - buffer_puts(buffer_1,"(Default.) No effect.\n"); + buffer_puts(buffer_1, "\nbadmimetypes.cdb: "); + if (stat("badmimetypes", &stmrh) == -1) + if (stat("badmimetypes.cdb", &stmrhcdb) == -1) + buffer_puts(buffer_1, "(Default.) No effect.\n"); else - buffer_puts(buffer_1,"Oops! badmimetypes.cdb exists but badmimetypes doesn't.\n"); + buffer_puts(buffer_1, "Oops! badmimetypes.cdb exists but badmimetypes doesn't.\n"); + else if (stat("badmimetypes.cdb", &stmrhcdb) == -1) + buffer_puts(buffer_1, "Oops! badmimetypes exists but badmimetypes.cdb doesn't.\n"); + else if (stmrh.st_mtime > stmrhcdb.st_mtime) + buffer_puts(buffer_1, "Oops! badmimetypes.cdb is older than badmimetypes.\n"); else - if (stat("badmimetypes.cdb",&stmrhcdb) == -1) - buffer_puts(buffer_1,"Oops! badmimetypes exists but badmimetypes.cdb doesn't.\n"); - else - if (stmrh.st_mtime > stmrhcdb.st_mtime) - buffer_puts(buffer_1,"Oops! badmimetypes.cdb is older than badmimetypes.\n"); - else - buffer_puts(buffer_1,"Modified recently enough; hopefully up to date.\n"); - do_lst("badrcptto","Any RCPT TO is allowed.",""," not accepted in RCPT TO."); - do_str("bouncefrom",0,"MAILER-DAEMON","Bounce user name is "); - do_str("bouncehost",1,"bouncehost","Bounce host name is "); - do_int("bouncemaxbytes","0","Bounce size limit is "," bytes"); - do_int("concurrencylocal","10","Local concurrency is ",""); - do_int("concurrencyremote","20","Remote concurrency is ",""); - do_int("databytes","0","SMTP DATA limit is "," bytes"); - do_str("defaultdomain",1,"defaultdomain","Default domain name is "); - do_str("defaulthost",1,"defaulthost","Default host name is "); - do_lst("dkimdomains","No DKIM domains defined for signing.","DKIM domains: ",""); - do_lst("domaincerts","No domain certs defined.","Domain certs: ",""); - do_lst("domainips","No domain ip mappings defined.","Mappping sender domain part to local ip: ",""); - do_str("doublebouncehost",1,"doublebouncehost","2B recipient host: "); - do_str("doublebounceto",0,"postmaster","2B recipient user: "); - do_str("envnoathost",1,"envnoathost","Presumed domain name is "); - do_str("helohost",1,"helohost","SMTP client HELO host name is "); - do_str("idhost",1,"idhost","Message-ID host name is "); - do_str("localiphost",1,"localiphost","Local IP address becomes "); - do_lst("locals","Messages for me are delivered locally.","Messages for "," are delivered locally."); - do_str("me",0,"undefined! Uh-oh","My name is "); + buffer_puts(buffer_1, "Modified recently enough; hopefully up to date.\n"); + do_lst("badrcptto", "Any RCPT TO is allowed.", "", " not accepted in RCPT TO."); + do_str("bouncefrom", 0, "MAILER-DAEMON", "Bounce user name is "); + do_str("bouncehost", 1, "bouncehost", "Bounce host name is "); + do_int("bouncemaxbytes", "0", "Bounce size limit is ", " bytes"); + do_int("concurrencylocal", "10", "Local concurrency is ", ""); + do_int("concurrencyremote", "20", "Remote concurrency is ", ""); + do_int("databytes", "0", "SMTP DATA limit is ", " bytes"); + do_str("defaultdomain", 1, "defaultdomain", "Default domain name is "); + do_str("defaulthost", 1, "defaulthost", "Default host name is "); + do_lst("dkimdomains", "No DKIM domains defined for signing.", "DKIM domains: ", ""); + do_lst("domaincerts", "No domain certs defined.", "Domain certs: ", ""); + do_lst("domainips", "No domain ip mappings defined.", "Mappping sender domain part to local ip: ", ""); + do_str("doublebouncehost", 1, "doublebouncehost", "2B recipient host: "); + do_str("doublebounceto", 0, "postmaster", "2B recipient user: "); + do_str("envnoathost", 1, "envnoathost", "Presumed domain name is "); + do_str("helohost", 1, "helohost", "SMTP client HELO host name is "); + do_str("idhost", 1, "idhost", "Message-ID host name is "); + do_str("localiphost", 1, "localiphost", "Local IP address becomes "); + do_lst("locals", "Messages for me are delivered locally.", "Messages for ", " are delivered locally."); + do_str("me", 0, "undefined! Uh-oh", "My name is "); - do_lst("mailfromrules","Any envelope sender are accepted.",""," (MAV rule)."); + do_lst("mailfromrules", "Any envelope sender are accepted.", "", " (MAV rule)."); /* XXX: check mailfromrules.cdb contents */ - buffer_puts(buffer_1,"\nmailfromrules.cdb: "); - if (stat("mailfromrules",&stmrh) == -1) - if (stat("mailfromrules.cdb",&stmrhcdb) == -1) - buffer_puts(buffer_1,"(Default.) No effect.\n"); + buffer_puts(buffer_1, "\nmailfromrules.cdb: "); + if (stat("mailfromrules", &stmrh) == -1) + if (stat("mailfromrules.cdb", &stmrhcdb) == -1) + buffer_puts(buffer_1, "(Default.) No effect.\n"); else - buffer_puts(buffer_1,"Oops! mailfromrules.cdb exists but mailfromrules doesn't.\n"); + buffer_puts(buffer_1, "Oops! mailfromrules.cdb exists but mailfromrules doesn't.\n"); + else if (stat("mailfromrules.cdb", &stmrhcdb) == -1) + buffer_puts(buffer_1, "Oops! mailfromrules exists but mailfromrules.cdb doesn't.\n"); + else if (stmrh.st_mtime > stmrhcdb.st_mtime) + buffer_puts(buffer_1, "Oops! mailfromrules.cdb is older than mailfromrules.\n"); else - if (stat("mailfromrules.cdb",&stmrhcdb) == -1) - buffer_puts(buffer_1,"Oops! mailfromrules exists but mailfromrules.cdb doesn't.\n"); - else - if (stmrh.st_mtime > stmrhcdb.st_mtime) - buffer_puts(buffer_1,"Oops! mailfromrules.cdb is older than mailfromrules.\n"); - else - buffer_puts(buffer_1,"Modified recently enough; hopefully up to date.\n"); + buffer_puts(buffer_1, "Modified recently enough; hopefully up to date.\n"); - do_lst("percenthack","The percent hack is not allowed.","The percent hack is allowed for user%host@","."); - do_str("plusdomain",1,"plusdomain","Plus domain name is "); - do_lst("qmqpservers","No QMQP servers.","QMQP server: ","."); - do_int("queuelifetime","604800","Message lifetime in the queue is "," seconds"); + do_lst( + "percenthack", + "The percent hack is not allowed.", + "The percent hack is allowed for user%host@", + "."); + do_str("plusdomain", 1, "plusdomain", "Plus domain name is "); + do_lst("qmqpservers", "No QMQP servers.", "QMQP server: ", "."); + do_int("queuelifetime", "604800", "Message lifetime in the queue is ", " seconds"); - if (do_lst("rcpthosts","SMTP clients may send messages to any recipient.","SMTP clients may send messages to recipients at ",".")) - do_lst("morercpthosts","No effect.","SMTP clients may send messages to recipients at ","."); + if (do_lst( + "rcpthosts", + "SMTP clients may send messages to any recipient.", + "SMTP clients may send messages to recipients at ", + ".")) + do_lst("morercpthosts", "No effect.", "SMTP clients may send messages to recipients at ", "."); else - do_lst("morercpthosts","No rcpthosts; morercpthosts is irrelevant.","No rcpthosts; doesn't matter that morercpthosts has ","."); + do_lst( + "morercpthosts", + "No rcpthosts; morercpthosts is irrelevant.", + "No rcpthosts; doesn't matter that morercpthosts has ", + "."); /* XXX: check morercpthosts.cdb contents */ - buffer_puts(buffer_1,"\nmorercpthosts.cdb: "); - if (stat("morercpthosts",&stmrh) == -1) - if (stat("morercpthosts.cdb",&stmrhcdb) == -1) - buffer_puts(buffer_1,"(Default.) No effect.\n"); + buffer_puts(buffer_1, "\nmorercpthosts.cdb: "); + if (stat("morercpthosts", &stmrh) == -1) + if (stat("morercpthosts.cdb", &stmrhcdb) == -1) + buffer_puts(buffer_1, "(Default.) No effect.\n"); else - buffer_puts(buffer_1,"Oops! morercpthosts.cdb exists but morercpthosts doesn't.\n"); + buffer_puts(buffer_1, "Oops! morercpthosts.cdb exists but morercpthosts doesn't.\n"); + else if (stat("morercpthosts.cdb", &stmrhcdb) == -1) + buffer_puts(buffer_1, "Oops! morercpthosts exists but morercpthosts.cdb doesn't.\n"); + else if (stmrh.st_mtime > stmrhcdb.st_mtime) + buffer_puts(buffer_1, "Oops! morercpthosts.cdb is older than morercpthosts.\n"); else - if (stat("morercpthosts.cdb",&stmrhcdb) == -1) - buffer_puts(buffer_1,"Oops! morercpthosts exists but morercpthosts.cdb doesn't.\n"); - else - if (stmrh.st_mtime > stmrhcdb.st_mtime) - buffer_puts(buffer_1,"Oops! morercpthosts.cdb is older than morercpthosts.\n"); - else - buffer_puts(buffer_1,"Modified recently enough; hopefully up to date.\n"); - do_lst("recipients","SMTP clients may send messages to any recipient.","SMTP clients may send messages to local recipients listed in ","."); - do_str("smtpgreeting",1,"smtpgreeting","SMTP greeting: 220 "); - do_lst("qmtproutes","No additional QMTP routes.","QMTP route: ",""); - do_lst("smtproutes","No artificial SMTP routes.","SMTP route: ",""); - do_str("spfexplain",0,SPF_DEFEXP,"SPF default explanation is: 550 "); - do_str("spflocalrules",0,"(None)","Defined local SPF rules are: "); - do_lst("srsrdomains","No SRS fowarding rules.","SRS rules: ",""); - do_int("timeoutconnect","60","SMTP client connection timeout is "," seconds"); - do_int("timeoutremote","1200","SMTP client data timeout is "," seconds"); - do_int("timeoutsmtpd","1200","SMTP server data timeout is "," seconds"); - do_lst("tlsdestinations","No TLS destinations defined.","TLS destination: ",""); - do_lst("virtualdomains","No virtual domains.","Virtual domain: ",""); + buffer_puts(buffer_1, "Modified recently enough; hopefully up to date.\n"); + do_lst( + "recipients", + "SMTP clients may send messages to any recipient.", + "SMTP clients may send messages to local recipients listed in ", + "."); + do_str("smtpgreeting", 1, "smtpgreeting", "SMTP greeting: 220 "); + do_lst("qmtproutes", "No additional QMTP routes.", "QMTP route: ", ""); + do_lst("smtproutes", "No artificial SMTP routes.", "SMTP route: ", ""); + do_str("spfexplain", 0, SPF_DEFEXP, "SPF default explanation is: 550 "); + do_str("spflocalrules", 0, "(None)", "Defined local SPF rules are: "); + do_lst("srsrdomains", "No SRS fowarding rules.", "SRS rules: ", ""); + do_int("timeoutconnect", "60", "SMTP client connection timeout is ", " seconds"); + do_int("timeoutremote", "1200", "SMTP client data timeout is ", " seconds"); + do_int("timeoutsmtpd", "1200", "SMTP server data timeout is ", " seconds"); + do_lst("tlsdestinations", "No TLS destinations defined.", "TLS destination: ", ""); + do_lst("virtualdomains", "No virtual domains.", "Virtual domain: ", ""); while ((d = readdir(dir))) { - if (str_equal(d->d_name,".")) continue; - if (str_equal(d->d_name,"..")) continue; - if (str_equal(d->d_name,"authsenders")) continue; - if (str_equal(d->d_name,"badhelo")) continue; - if (str_equal(d->d_name,"badrcptto")) continue; - if (str_equal(d->d_name,"badmailfrom")) continue; - if (str_equal(d->d_name,"badloadertypes")) continue; - if (str_equal(d->d_name,"badloadertypes.cdb")) continue; - if (str_equal(d->d_name,"badmimetypes")) continue; - if (str_equal(d->d_name,"badmimetypes.cdb")) continue; - if (str_equal(d->d_name,"bouncefrom")) continue; - if (str_equal(d->d_name,"bouncehost")) continue; - if (str_equal(d->d_name,"bouncemaxbytes")) continue; - if (str_equal(d->d_name,"concurrencylocal")) continue; - if (str_equal(d->d_name,"concurrencyremote")) continue; - if (str_equal(d->d_name,"databytes")) continue; - if (str_equal(d->d_name,"defaultdomain")) continue; - if (str_equal(d->d_name,"defaulthost")) continue; - if (str_equal(d->d_name,"dkimdomains")) continue; - if (str_equal(d->d_name,"domainips")) continue; - if (str_equal(d->d_name,"domaincerts")) continue; - if (str_equal(d->d_name,"doublebouncehost")) continue; - if (str_equal(d->d_name,"doublebounceto")) continue; - if (str_equal(d->d_name,"envnoathost")) continue; - if (str_equal(d->d_name,"helohost")) continue; - if (str_equal(d->d_name,"idhost")) continue; - if (str_equal(d->d_name,"localiphost")) continue; - if (str_equal(d->d_name,"locals")) continue; - if (str_equal(d->d_name,"me")) continue; - if (str_equal(d->d_name,"mailfromrules")) continue; - if (str_equal(d->d_name,"mailfromrules.cdb")) continue; - if (str_equal(d->d_name,"morercpthosts")) continue; - if (str_equal(d->d_name,"morercpthosts.cdb")) continue; - if (str_equal(d->d_name,"percenthack")) continue; - if (str_equal(d->d_name,"plusdomain")) continue; - if (str_equal(d->d_name,"qmqpservers")) continue; - if (str_equal(d->d_name,"queuelifetime")) continue; - if (str_equal(d->d_name,"rcpthosts")) continue; - if (str_equal(d->d_name,"recipients")) continue; - if (str_equal(d->d_name,"smtpgreeting")) continue; - if (str_equal(d->d_name,"qmtproutes")) continue; - if (str_equal(d->d_name,"smtproutes")) continue; - if (str_equal(d->d_name,"spfexplain")) continue; - if (str_equal(d->d_name,"spflocalrules")) continue; - if (str_equal(d->d_name,"srsdomains")) continue; - if (str_equal(d->d_name,"timeoutconnect")) continue; - if (str_equal(d->d_name,"timeoutremote")) continue; - if (str_equal(d->d_name,"timeoutsmtpd")) continue; - if (str_equal(d->d_name,"tlsdestinations")) continue; - if (str_equal(d->d_name,"virtualdomains")) continue; - buffer_puts(buffer_1,"\n"); - buffer_puts(buffer_1,d->d_name); - buffer_puts(buffer_1,": I have no idea what this file does.\n"); + if (str_equal(d->d_name, ".")) continue; + if (str_equal(d->d_name, "..")) continue; + if (str_equal(d->d_name, "authsenders")) continue; + if (str_equal(d->d_name, "badhelo")) continue; + if (str_equal(d->d_name, "badrcptto")) continue; + if (str_equal(d->d_name, "badmailfrom")) continue; + if (str_equal(d->d_name, "badloadertypes")) continue; + if (str_equal(d->d_name, "badloadertypes.cdb")) continue; + if (str_equal(d->d_name, "badmimetypes")) continue; + if (str_equal(d->d_name, "badmimetypes.cdb")) continue; + if (str_equal(d->d_name, "bouncefrom")) continue; + if (str_equal(d->d_name, "bouncehost")) continue; + if (str_equal(d->d_name, "bouncemaxbytes")) continue; + if (str_equal(d->d_name, "concurrencylocal")) continue; + if (str_equal(d->d_name, "concurrencyremote")) continue; + if (str_equal(d->d_name, "databytes")) continue; + if (str_equal(d->d_name, "defaultdomain")) continue; + if (str_equal(d->d_name, "defaulthost")) continue; + if (str_equal(d->d_name, "dkimdomains")) continue; + if (str_equal(d->d_name, "domainips")) continue; + if (str_equal(d->d_name, "domaincerts")) continue; + if (str_equal(d->d_name, "doublebouncehost")) continue; + if (str_equal(d->d_name, "doublebounceto")) continue; + if (str_equal(d->d_name, "envnoathost")) continue; + if (str_equal(d->d_name, "helohost")) continue; + if (str_equal(d->d_name, "idhost")) continue; + if (str_equal(d->d_name, "localiphost")) continue; + if (str_equal(d->d_name, "locals")) continue; + if (str_equal(d->d_name, "me")) continue; + if (str_equal(d->d_name, "mailfromrules")) continue; + if (str_equal(d->d_name, "mailfromrules.cdb")) continue; + if (str_equal(d->d_name, "morercpthosts")) continue; + if (str_equal(d->d_name, "morercpthosts.cdb")) continue; + if (str_equal(d->d_name, "percenthack")) continue; + if (str_equal(d->d_name, "plusdomain")) continue; + if (str_equal(d->d_name, "qmqpservers")) continue; + if (str_equal(d->d_name, "queuelifetime")) continue; + if (str_equal(d->d_name, "rcpthosts")) continue; + if (str_equal(d->d_name, "recipients")) continue; + if (str_equal(d->d_name, "smtpgreeting")) continue; + if (str_equal(d->d_name, "qmtproutes")) continue; + if (str_equal(d->d_name, "smtproutes")) continue; + if (str_equal(d->d_name, "spfexplain")) continue; + if (str_equal(d->d_name, "spflocalrules")) continue; + if (str_equal(d->d_name, "srsdomains")) continue; + if (str_equal(d->d_name, "timeoutconnect")) continue; + if (str_equal(d->d_name, "timeoutremote")) continue; + if (str_equal(d->d_name, "timeoutsmtpd")) continue; + if (str_equal(d->d_name, "tlsdestinations")) continue; + if (str_equal(d->d_name, "virtualdomains")) continue; + buffer_puts(buffer_1, "\n"); + buffer_puts(buffer_1, d->d_name); + buffer_puts(buffer_1, ": I have no idea what this file does.\n"); } buffer_flush(buffer_1); diff --git a/src/qmail-smtpam.c b/src/qmail-smtpam.c index c0c6550..89105c3 100644 --- a/src/qmail-smtpam.c +++ b/src/qmail-smtpam.c @@ -1,65 +1,67 @@ -#include <sys/types.h> +#include <arpa/inet.h> +#include <netinet/in.h> #include <sys/socket.h> #include <sys/stat.h> -#include <netinet/in.h> -#include <arpa/inet.h> +#include <sys/types.h> #include <unistd.h> -#include "sig.h" -#include "genalloc.h" -#include "stralloc.h" + +#include "alloc.h" #include "buffer.h" -#include "scan.h" -#include "case.h" #include "byte.h" +#include "case.h" +#include "constmap.h" #include "error.h" +#include "exit.h" +#include "genalloc.h" +#include "ip.h" +#include "scan.h" +#include "sig.h" +#include "socket_if.h" +#include "str.h" +#include "stralloc.h" +#include "timeout.h" +#include "timeoutconn.h" +#include "uint_t.h" + #include "auto_qmail.h" #include "control.h" #include "dns.h" -#include "alloc.h" -#include "quote.h" -#include "ip.h" #include "ipalloc.h" #include "ipme.h" -#include "str.h" #include "now.h" -#include "exit.h" -#include "constmap.h" +#include "quote.h" #include "tcpto.h" -#include "socket_if.h" -#include "ucspissl.h" -#include "timeout.h" -#include "timeoutconn.h" -#include "tls_remote.h" #include "tls_errors.h" +#include "tls_remote.h" #include "tls_timeoutio.h" -#include "uint_t.h" +#include "ucspissl.h" -#define MAX_SIZE 200000000 +#define MAX_SIZE 200000000 #define HUGESMTPTEXT 5000 -#define PORT_SMTP 25 /* silly rabbit, /etc/services is for users */ -#define PORT_SMTPS 465 -#define VERIFYDEPTH 1 -#define FDPAM 3 +#define PORT_SMTP 25 /* silly rabbit, /etc/services is for users */ +#define PORT_SMTPS 465 +#define VERIFYDEPTH 1 +#define FDPAM 3 #define WHO "qmail-smtpam" /** @file qmail-smtpam.c -- TLS enabled SMTP PAM to check mailbox at remote MX */ -int flagauth = 0; /* 1 = login; 2 = plain; 3 =crammd5 */ -int flagsmtps = 0; /* RFC 8314 - 'implicit TLS' */ -int flagtls = 0; /* -2 = rejected; -1 = not; 0 = no, default; +int flagauth = 0; /* 1 = login; 2 = plain; 3 =crammd5 */ +int flagsmtps = 0; /* RFC 8314 - 'implicit TLS' */ +int flagtls = 0; /* -2 = rejected; -1 = not; 0 = no, default; > 0 see tls_remote.c +10 = SMTPS; +20 = QMTPS; 100 = active TLS connection */ -int flagverify = 0; /* 1 = verify Cert against CA ; -1 = Cert pinning */ +int flagverify = 0; /* 1 = verify Cert against CA ; -1 = Cert pinning */ int flagutf8mail = 0; unsigned long port = PORT_SMTP; -GEN_ALLOC_typedef(saa,stralloc,sa,len,a) -GEN_ALLOC_readyplus(saa,stralloc,sa,len,a,i,n,x,10,saa_readyplus) +GEN_ALLOC_typedef(saa, stralloc, sa, len, a) + GEN_ALLOC_readyplus(saa, stralloc, sa, len, a, i, n, x, 10, saa_readyplus) -stralloc helohost = {0}; + stralloc helohost = {0}; stralloc host = {0}; stralloc ports = {0}; stralloc remotehost = {0}; @@ -83,44 +85,55 @@ struct ip_mx partner; SSL *ssl; SSL_CTX *ctx; -void out(char *s) { if (buffer_puts(buffer_1small,s) == -1) _exit(111); } -void zero() { if (buffer_put(buffer_1small,"\0",1) == -1) _exit(111); } -void zerodie() { zero(); buffer_flush(buffer_1small); _exit(111); } -void outsafe(stralloc *sa) -{ - int i; +void out(char *s) +{ + if (buffer_puts(buffer_1small, s) == -1) _exit(111); +} +void zero() +{ + if (buffer_put(buffer_1small, "\0", 1) == -1) _exit(111); +} +void zerodie() +{ + zero(); + buffer_flush(buffer_1small); + _exit(111); +} +void outsafe(stralloc *sa) +{ + int i; char ch; for (i = 0; i < sa->len; ++i) { - ch = sa->s[i]; - if (ch < 33) ch = '?'; + ch = sa->s[i]; + if (ch < 33) ch = '?'; if (ch > 126) ch = '?'; - if (buffer_put(buffer_1small,&ch,1) == -1) _exit(111); - } + if (buffer_put(buffer_1small, &ch, 1) == -1) _exit(111); + } } -void temp_noip() +void temp_noip() { out("Zinvalid ipaddr in control/domainips (#4.3.0)\n"); zerodie(); } -void temp_nomem() +void temp_nomem() { out("ZOut of memory. (#4.3.0)\n"); zerodie(); } -void temp_oserr() +void temp_oserr() { out("ZSystem resources temporarily unavailable. (#4.3.0)\n"); zerodie(); } -void temp_osip() +void temp_osip() { out("ZCan't bind to local ip address: "); outsafe(&sendip); out(". (#4.3.0)\n"); zerodie(); } -void temp_noconn() +void temp_noconn() { out("ZSorry, I wasn't able to establish an SMTP connection. (#4.4.1)\n"); zerodie(); @@ -132,29 +145,29 @@ void temp_dnscanon() out(". (#4.4.3)\n"); zerodie(); } -void temp_dns() +void temp_dns() { out("ZSorry, I couldn't find any host named: "); outsafe(&host); out(". (#4.1.2)\n"); zerodie(); } -void temp_chdir() +void temp_chdir() { out("ZUnable to switch to home directory. (#4.3.0)\n"); zerodie(); } -void temp_control() +void temp_control() { out("ZUnable to read control files. (#4.3.0)\n"); zerodie(); } -void perm_usage() +void perm_usage() { out("Dqmail-smtpam was invoked improperly. (#5.3.5)\n"); zerodie(); } -void perm_dns() +void perm_dns() { out("DSorry, I couldn't find any host named: "); outsafe(&host); @@ -167,17 +180,15 @@ void outhost() int len; switch (partner.af) { - case AF_INET: - len = ip4_fmt(ipaddr,(char *)&partner.addr.ip4.d); break; - case AF_INET6: - len = ip6_fmt(ipaddr,(char *)&partner.addr.ip6.d); break; + case AF_INET: len = ip4_fmt(ipaddr, (char *)&partner.addr.ip4.d); break; + case AF_INET6: len = ip6_fmt(ipaddr, (char *)&partner.addr.ip6.d); break; } - if (buffer_put(buffer_1small,ipaddr,len) == -1) _exit(0); + if (buffer_put(buffer_1small, ipaddr, len) == -1) _exit(0); } int flagcritical = 0; -void dropped() +void dropped() { out("ZConnected to "); outhost(); @@ -191,35 +202,35 @@ int timeoutconnect = 60; int smtpfd; int timeout = 1200; -ssize_t saferead(int fd,char *buf,int len) +ssize_t saferead(int fd, char *buf, int len) { int r; - r = timeoutread(timeout,smtpfd,buf,len); + r = timeoutread(timeout, smtpfd, buf, len); if (r <= 0) dropped(); return r; } -ssize_t safewrite(int fd,char *buf,int len) +ssize_t safewrite(int fd, char *buf, int len) { int r; - r = timeoutwrite(timeout,smtpfd,buf,len); + r = timeoutwrite(timeout, smtpfd, buf, len); if (r <= 0) dropped(); return r; } char outbuf[1450]; -buffer bo = BUFFER_INIT(safewrite,-1,outbuf,sizeof(outbuf)); +buffer bo = BUFFER_INIT(safewrite, -1, outbuf, sizeof(outbuf)); char frombuf[128]; -buffer bi = BUFFER_INIT(saferead,-1,frombuf,sizeof(frombuf)); +buffer bi = BUFFER_INIT(saferead, -1, frombuf, sizeof(frombuf)); stralloc smtptext = {0}; void get(char *ch) { - buffer_get(&bi,ch,1); + buffer_get(&bi, ch, 1); if (*ch != '\r') if (smtptext.len < HUGESMTPTEXT) - if (!stralloc_append(&smtptext,ch)) temp_nomem(); + if (!stralloc_append(&smtptext, ch)) temp_nomem(); } unsigned long smtpcode() @@ -227,11 +238,14 @@ unsigned long smtpcode() unsigned char ch; unsigned long code; - if (!stralloc_copys(&smtptext,"")) temp_nomem(); + if (!stralloc_copys(&smtptext, "")) temp_nomem(); - get(&ch); code = ch - '0'; - get(&ch); code = code * 10 + (ch - '0'); - get(&ch); code = code * 10 + (ch - '0'); + get(&ch); + code = ch - '0'; + get(&ch); + code = code * 10 + (ch - '0'); + get(&ch); + code = code * 10 + (ch - '0'); for (;;) { get(&ch); if (ch != '-') break; @@ -247,19 +261,20 @@ unsigned long smtpcode() void outsmtptext() { - int i; - if (smtptext.s) if (smtptext.len) { - out("Remote host said: "); - for (i = 0; i < smtptext.len; ++i) - if (!smtptext.s[i]) smtptext.s[i] = '?'; - if (buffer_put(buffer_1small,smtptext.s,smtptext.len) == -1) _exit(111); - smtptext.len = 0; - } + int i; + if (smtptext.s) + if (smtptext.len) { + out("Remote host said: "); + for (i = 0; i < smtptext.len; ++i) + if (!smtptext.s[i]) smtptext.s[i] = '?'; + if (buffer_put(buffer_1small, smtptext.s, smtptext.len) == -1) _exit(111); + smtptext.len = 0; + } } -void quit(char *prepend,char *append) +void quit(char *prepend, char *append) { - buffer_putsflush(&bo,"QUIT\r\n"); + buffer_putsflush(&bo, "QUIT\r\n"); /* waiting for remote side is just too ridiculous */ out(prepend); outhost(); @@ -292,43 +307,42 @@ unsigned long verifydepth = VERIFYDEPTH; void tls_init() { -/* Client CTX */ + /* Client CTX */ ctx = ssl_client(); ssl_errstr(); if (!ctx) temp_tlsctx(); -/* Fetch CA infos for dest */ + /* Fetch CA infos for dest */ if (flagverify > 0) if (cafile.len || cadir.len) - if (!ssl_ca(ctx,cafile.s,cadir.s,(int) verifydepth)) temp_tlsca(); + if (!ssl_ca(ctx, cafile.s, cadir.s, (int)verifydepth)) temp_tlsca(); if (ciphers.len) - if (!ssl_ciphers(ctx,ciphers.s)) temp_tlscipher(); + if (!ssl_ciphers(ctx, ciphers.s)) temp_tlscipher(); -/* Set SSL Context */ + /* Set SSL Context */ - ssl = ssl_new(ctx,smtpfd); + ssl = ssl_new(ctx, smtpfd); if (!ssl) temp_tlsctx(); -/* Setup SSL FDs */ + /* Setup SSL FDs */ - if (!tls_conn(ssl,smtpfd)) temp_tlscon(); + if (!tls_conn(ssl, smtpfd)) temp_tlscon(); -/* Go on in none-blocking mode */ + /* Go on in none-blocking mode */ - if (tls_timeoutconn(timeout,smtpfd,smtpfd,ssl) <= 0) - temp_tlserr(); + if (tls_timeoutconn(timeout, smtpfd, smtpfd, ssl) <= 0) temp_tlserr(); } int starttls_peer() { int i = 0; - while ( (i += str_chr(smtptext.s + i,'\n') + 1) && - (i < smtptext.len - 8) ) { - if (!str_diffn(smtptext.s + i + 4,"STARTTLS",8)) return 1; } + while ((i += str_chr(smtptext.s + i, '\n') + 1) && (i < smtptext.len - 8)) { + if (!str_diffn(smtptext.s + i + 4, "STARTTLS", 8)) return 1; + } return 0; } @@ -336,37 +350,40 @@ int starttls_peer() void tls_peercheck() { X509 *cert; - + cert = SSL_get_peer_certificate(ssl); - if (!cert) { flagtls = 100; return; } + if (!cert) { + flagtls = 100; + return; + } if (flagverify < 0) { - if (cafile.len) case_lowerb(cafile.s,cafile.len); - switch (tls_fingerprint(cert,cafile.s + 1,cafile.len - 1)) { + if (cafile.len) case_lowerb(cafile.s, cafile.len); + switch (tls_fingerprint(cert, cafile.s + 1, cafile.len - 1)) { case -1: temp_tlspeercert(); case -2: temp_tlsdigest(); case -3: temp_invaliddigest(); - case 1: temp_tlscertfp(); + case 1: temp_tlscertfp(); } } else { - switch (tls_checkpeer(ssl,cert,remotehost,flagtls,flagverify)) { + switch (tls_checkpeer(ssl, cert, remotehost, flagtls, flagverify)) { case -1: temp_tlspeercert(); case -2: temp_tlspeerverify(); case -3: temp_tlspeervalid(); - case 1: flagtls = 101; break; - case 2: flagtls = 102; break; - case 3: flagtls = 103; break; + case 1: flagtls = 101; break; + case 2: flagtls = 102; break; + case 3: flagtls = 103; break; } } if (flagtls < 100) flagtls = 100; - + X509_free(cert); return; } -int utf8flag(unsigned char *ch,int len) +int utf8flag(unsigned char *ch, int len) { int i = 0; while (i < len) @@ -380,25 +397,25 @@ unsigned long code; void smtp_greeting() { - buffer_puts(&bo,"EHLO "); - buffer_put(&bo,helohost.s,helohost.len); - buffer_puts(&bo,"\r\n"); + buffer_puts(&bo, "EHLO "); + buffer_put(&bo, helohost.s, helohost.len); + buffer_puts(&bo, "\r\n"); buffer_flush(&bo); if (smtpcode() != 250) { - buffer_puts(&bo,"HELO "); - buffer_put(&bo,helohost.s,helohost.len); - buffer_puts(&bo,"\r\n"); + buffer_puts(&bo, "HELO "); + buffer_put(&bo, helohost.s, helohost.len); + buffer_puts(&bo, "\r\n"); buffer_flush(&bo); code = smtpcode(); - if (code >= 500) quit("DConnected to"," but my name was rejected"); - if (code != 250) quit("ZConnected to"," but my name was rejected"); + if (code >= 500) quit("DConnected to", " but my name was rejected"); + if (code != 250) quit("ZConnected to", " but my name was rejected"); } } void smtp_starttls() { - buffer_puts(&bo,"STARTTLS\r\n"); + buffer_puts(&bo, "STARTTLS\r\n"); buffer_flush(&bo); if (smtpcode() == 220) { tls_init(); @@ -406,45 +423,44 @@ void smtp_starttls() smtp_greeting(); } else { flagtls = -2; - quit("ZConnected to"," but STARTTLS was rejected"); + quit("ZConnected to", " but STARTTLS was rejected"); } } void smtp() { - if (flagtls > 10 && flagtls < 20) { /* SMTPS */ + if (flagtls > 10 && flagtls < 20) { /* SMTPS */ tls_init(); tls_peercheck(); } code = smtpcode(); - if (code >= 500) quit("DConnected to "," but sender was rejected"); - if (code >= 400) quit("ZConnected to "," but sender was probably greylisted"); + if (code >= 500) quit("DConnected to ", " but sender was rejected"); + if (code >= 400) quit("ZConnected to ", " but sender was probably greylisted"); smtp_greeting(); - if (flagutf8mail) buffer_puts(&bo," SMTPUTF8"); + if (flagutf8mail) buffer_puts(&bo, " SMTPUTF8"); - if (flagtls > 0 && flagtls < 10) /* STARTTLS */ + if (flagtls > 0 && flagtls < 10) /* STARTTLS */ if (starttls_peer()) { - smtp_starttls(); + smtp_starttls(); } else if (flagtls > 2) { - temp_tlshost(); + temp_tlshost(); } - buffer_puts(&bo,"MAIL FROM:<>"); - if (flagutf8mail) - buffer_puts(&bo," SMTPUTF8"); - buffer_puts(&bo,"\r\n"); + buffer_puts(&bo, "MAIL FROM:<>"); + if (flagutf8mail) buffer_puts(&bo, " SMTPUTF8"); + buffer_puts(&bo, "\r\n"); buffer_flush(&bo); code = smtpcode(); - if (code >= 500) quit("DConnected to "," but sender was rejected"); - if (code >= 400) quit("ZConnected to "," but sender was rejected"); - - buffer_puts(&bo,"RCPT TO:<"); - buffer_put(&bo,recipient.s,recipient.len); - buffer_puts(&bo,">\r\n"); + if (code >= 500) quit("DConnected to ", " but sender was rejected"); + if (code >= 400) quit("ZConnected to ", " but sender was rejected"); + + buffer_puts(&bo, "RCPT TO:<"); + buffer_put(&bo, recipient.s, recipient.len); + buffer_puts(&bo, ">\r\n"); buffer_flush(&bo); code = smtpcode(); close(smtpfd); @@ -455,28 +471,33 @@ void smtp() void getcontrols() { if (control_init() == -1) temp_control(); - if (control_readint(&timeout,"control/timeoutremote") == -1) temp_control(); - if (control_readint(&timeoutconnect,"control/timeoutconnect") == -1) - temp_control(); - if (control_rldef(&helohost,"control/helohost",1,(char *) 0) != 1) - temp_control(); - switch (control_readfile(&domainips,"control/domainips",0)) { + if (control_readint(&timeout, "control/timeoutremote") == -1) temp_control(); + if (control_readint(&timeoutconnect, "control/timeoutconnect") == -1) temp_control(); + if (control_rldef(&helohost, "control/helohost", 1, (char *)0) != 1) temp_control(); + switch (control_readfile(&domainips, "control/domainips", 0)) { case -1: temp_control(); - case 0: if (!constmap_init(&mapdomainips,"",0,1)) temp_nomem(); break; - case 1: if (!constmap_init(&mapdomainips,domainips.s,domainips.len,1)) temp_nomem(); break; + case 0: + if (!constmap_init(&mapdomainips, "", 0, 1)) temp_nomem(); + break; + case 1: + if (!constmap_init(&mapdomainips, domainips.s, domainips.len, 1)) temp_nomem(); + break; } - switch (control_readfile(&tlsdestinations,"control/tlsdestinations",0)) { + switch (control_readfile(&tlsdestinations, "control/tlsdestinations", 0)) { case -1: temp_control(); - case 0: if (!constmap_init(&maptlsdestinations,"",0,1)) temp_nomem(); break; - case 1: if (!constmap_init(&maptlsdestinations,tlsdestinations.s,tlsdestinations.len,1)) temp_nomem(); break; + case 0: + if (!constmap_init(&maptlsdestinations, "", 0, 1)) temp_nomem(); + break; + case 1: + if (!constmap_init(&maptlsdestinations, tlsdestinations.s, tlsdestinations.len, 1)) temp_nomem(); + break; } - } char up[513]; int uplen; -int main(int argc,char **argv) +int main(int argc, char **argv) { static ipalloc ip = {0}; stralloc netif = {0}; @@ -485,83 +506,88 @@ int main(int argc,char **argv) int p; /* reserved for port */ char *localip = 0; char *tlsdestinfo = 0; - + sig_pipeignore(); if (argc < 2) perm_usage(); if (chdir(auto_qmail) == -1) temp_chdir(); getcontrols(); - - if (!stralloc_copys(&host,argv[1])) temp_nomem(); + + if (!stralloc_copys(&host, argv[1])) temp_nomem(); if (argv[2]) { - if (!stralloc_copys(&ports,argv[2])) temp_nomem(); - if (*ports.s == 's') { ports.s++; flagsmtps = 1; } - scan_ulong(ports.s,&port); + if (!stralloc_copys(&ports, argv[2])) temp_nomem(); + if (*ports.s == 's') { + ports.s++; + flagsmtps = 1; + } + scan_ulong(ports.s, &port); } if (ipme_init() != 1) temp_oserr(); -/* this file is too long -------------------------------------- set domain ip + helohost */ + /* this file is too long -------------------------------------- set domain ip + helohost */ - if (!localip) - localip = constmap(&mapdomainips,"*",1); /* one for all */ + if (!localip) localip = constmap(&mapdomainips, "*", 1); /* one for all */ if (localip) { - j = str_chr(localip,'%'); + j = str_chr(localip, '%'); if (localip[j] != '%') j = 0; - k = str_chr(localip,'|'); + k = str_chr(localip, '|'); if (localip[k] != '|') k = 0; - if (k) { /* helohost */ - if (!stralloc_copys(&helohost,localip + k + 1)) temp_nomem(); + if (k) { /* helohost */ + if (!stralloc_copys(&helohost, localip + k + 1)) temp_nomem(); localip[k] = 0; } - if (j) { /* if index */ + if (j) { /* if index */ localip[j] = 0; - if (!stralloc_copys(&netif,localip + j + 1)) temp_nomem(); + if (!stralloc_copys(&netif, localip + j + 1)) temp_nomem(); if (!stralloc_0(&netif)) temp_nomem(); } } -/* this file is too long -------------------------------------- TLS destinations */ + /* this file is too long -------------------------------------- TLS destinations */ - flagtls = tls_destination((const stralloc) host); // un-terminated + flagtls = tls_destination((const stralloc)host); // un-terminated if (flagtls > 0) { if (tlsdestinfo) { - i = str_chr(tlsdestinfo,'|'); /* ca file or cert fingerprint */ + i = str_chr(tlsdestinfo, '|'); /* ca file or cert fingerprint */ if (tlsdestinfo[i] == '|') { tlsdestinfo[i] = 0; - j = str_chr(tlsdestinfo+i+1,'|'); /* cipher */ + j = str_chr(tlsdestinfo + i + 1, '|'); /* cipher */ if (tlsdestinfo[i + j + 1] == '|') { tlsdestinfo[i + j + 1] = 0; - k = str_chr(tlsdestinfo + i + j + 2,'|'); /* cone domain */ + k = str_chr(tlsdestinfo + i + j + 2, '|'); /* cone domain */ if (tlsdestinfo[i + j + k + 2] == '|') { tlsdestinfo[i + j + k + 2] = 0; - if (str_diffn(tlsdestinfo + j + k + 3,canonhost.s,canonhost.len)) flagtls = 0; + if (str_diffn(tlsdestinfo + j + k + 3, canonhost.s, canonhost.len)) flagtls = 0; } - p = str_chr(tlsdestinfo + i + j + 2,';'); /* verifydepth;port */ + p = str_chr(tlsdestinfo + i + j + 2, ';'); /* verifydepth;port */ if (tlsdestinfo[i + j + p + 2] == ';') { - if (tlsdestinfo[i + j + p + 3] == 's') { flagsmtps = 1; p++; } + if (tlsdestinfo[i + j + p + 3] == 's') { + flagsmtps = 1; + p++; + } tlsdestinfo[i + j + p + 2] = 0; - if (p > 0) scan_ulong(tlsdestinfo+i+j + 2,&verifydepth); - scan_ulong(tlsdestinfo+i+j + p + 3,&port); + if (p > 0) scan_ulong(tlsdestinfo + i + j + 2, &verifydepth); + scan_ulong(tlsdestinfo + i + j + p + 3, &port); } } - if (!stralloc_copys(&ciphers,tlsdestinfo + i + 1)) temp_nomem(); + if (!stralloc_copys(&ciphers, tlsdestinfo + i + 1)) temp_nomem(); } - if (!stralloc_copys(&cafile,tlsdestinfo)) temp_nomem(); + if (!stralloc_copys(&cafile, tlsdestinfo)) temp_nomem(); } -/* cafile starts with '=' => it is a fingerprint + /* cafile starts with '=' => it is a fingerprint cafile ends with '/' => consider it as cadir */ - if (cafile.len) { + if (cafile.len) { flagverify = 1; if (cafile.s[cafile.len] == '/') { cafile.len = 0; flagverify = 2; - if (!stralloc_copys(&cadir,tlsdestinfo)) temp_nomem(); + if (!stralloc_copys(&cadir, tlsdestinfo)) temp_nomem(); if (!stralloc_0(&cadir)) temp_nomem(); } else { if (cafile.s[0] == '%') flagverify = -1; @@ -574,12 +600,11 @@ int main(int argc,char **argv) if (port == PORT_SMTPS || flagsmtps) flagtls = flagtls + 10; } -/* this file is too long -------------------------------------- Setup connection */ + /* this file is too long -------------------------------------- Setup connection */ uplen = 0; for (;;) { - do - r = read(FDPAM,up + uplen,sizeof(up) - uplen); + do r = read(FDPAM, up + uplen, sizeof(up) - uplen); while ((r == -1) && (errno == EINTR)); if (r == -1) _exit(111); if (r == 0) break; @@ -588,44 +613,45 @@ int main(int argc,char **argv) } close(FDPAM); - if (!stralloc_copyb(&recipient,up,uplen)) temp_nomem(); - if (!stralloc_0(&recipient)) temp_nomem(); - if (!stralloc_0(&host)) temp_nomem(); - if (!stralloc_copys(&remotehost,host.s)) temp_nomem(); + if (!stralloc_copyb(&recipient, up, uplen)) temp_nomem(); + if (!stralloc_0(&recipient)) temp_nomem(); + if (!stralloc_0(&host)) temp_nomem(); + if (!stralloc_copys(&remotehost, host.s)) temp_nomem(); - flagutf8mail = utf8flag(recipient.s,recipient.len); + flagutf8mail = utf8flag(recipient.s, recipient.len); - switch (dns_ip(&ip,&remotehost)) { - case DNS_MEM: temp_nomem(); + switch (dns_ip(&ip, &remotehost)) { + case DNS_MEM: temp_nomem(); case DNS_ERR: temp_dns(); case DNS_COM: temp_dnscanon(); - default: if (ip.len <= 0) perm_dns(); + default: + if (ip.len <= 0) perm_dns(); } - smtpfd = socket(ip.ix[i].af,SOCK_STREAM,0); + smtpfd = socket(ip.ix[i].af, SOCK_STREAM, 0); if (smtpfd == -1) temp_oserr(); - - if (localip) { /* set domain ip */ - if (!stralloc_copyb(&sendip,localip,str_len(localip))) temp_nomem(); - j = str_chr(localip,':'); - if (j && localip[j] == ':') { /* IPv6 */ - if (!ip6_scan(localip,ip6)) temp_noip(); - ifidx = socket_getifidx(netif.s); - if (socket_bind6(smtpfd,ip6,0,ifidx) < 0) temp_osip(); - } else { /* IPv4 */ - if (!ip4_scan(localip,ip4)) temp_noip(); - if (socket_bind4(smtpfd,ip4,0) < 0) temp_osip(); - } - } - - r = timeoutconn(smtpfd,&ip.ix[i].addr,(unsigned int) port,timeoutconnect,ifidx); - if (r == 0) { - tcpto_err(&ip.ix[i],0); + + if (localip) { /* set domain ip */ + if (!stralloc_copyb(&sendip, localip, str_len(localip))) temp_nomem(); + j = str_chr(localip, ':'); + if (j && localip[j] == ':') { /* IPv6 */ + if (!ip6_scan(localip, ip6)) temp_noip(); + ifidx = socket_getifidx(netif.s); + if (socket_bind6(smtpfd, ip6, 0, ifidx) < 0) temp_osip(); + } else { /* IPv4 */ + if (!ip4_scan(localip, ip4)) temp_noip(); + if (socket_bind4(smtpfd, ip4, 0) < 0) temp_osip(); + } + } + + r = timeoutconn(smtpfd, &ip.ix[i].addr, (unsigned int)port, timeoutconnect, ifidx); + if (r == 0) { + tcpto_err(&ip.ix[i], 0); partner = ip.ix[i]; smtp(); /* does not return */ } - tcpto_err(&ip.ix[i],errno == ETIMEDOUT); + tcpto_err(&ip.ix[i], errno == ETIMEDOUT); close(smtpfd); - + temp_noconn(); } diff --git a/src/qmail-smtpd.c b/src/qmail-smtpd.c index f312c11..6d2d0d4 100644 --- a/src/qmail-smtpd.c +++ b/src/qmail-smtpd.c @@ -1,93 +1,102 @@ #include <unistd.h> -#include "wildmat.h" -#include "buffer.h" -#include "stralloc.h" -#include "genalloc.h" + #include "alloc.h" -#include "auto_qmail.h" -#include "control.h" -#include "received.h" +#include "base64.h" +#include "buffer.h" +#include "byte.h" +#include "case.h" +#include "cdbread.h" +#include "close.h" #include "constmap.h" -#include "logmsg.h" -#include "ipme.h" +#include "env.h" +#include "exit.h" #include "fd.h" -#include "ip.h" -#include "qmail.h" -#include "str.h" #include "fmt.h" +#include "genalloc.h" +#include "ip.h" +#include "logmsg.h" +#include "open.h" #include "scan.h" -#include "byte.h" -#include "case.h" -#include "env.h" +#include "sig.h" +#include "str.h" +#include "stralloc.h" +#include "timeout.h" +#include "wait.h" + +#include "auto_qmail.h" +#include "commands.h" +#include "control.h" +#include "dns.h" +#include "ipme.h" +#include "mfrules.h" #include "now.h" -#include "exit.h" +#include "qmail.h" #include "rcpthosts.h" +#include "received.h" #include "recipients.h" -#include "mfrules.h" -#include "tls_start.h" #include "smtpdlog.h" -#include "timeout.h" -#include "commands.h" -#include "cdbread.h" -#include "dns.h" -#include "wait.h" -#include "sig.h" -#include "close.h" -#include "open.h" -#include "base64.h" #include "spf.h" +#include "tls_start.h" +#include "wildmat.h" /** @file qmail-smtpd.c -- authenticating ESMTP/ESMTPS server @brief requires sslserver or tcpserver */ #define PAM111421 -#define AUTHSLEEP 5 +#define AUTHSLEEP 5 #define PORT_SMTPS "465" -#define MIMETYPE_LEN 9 -#define LOADER_LEN 5 +#define MIMETYPE_LEN 9 +#define LOADER_LEN 5 #define BASE64MESSAGE "content-transfer-encoding: base64" -#define FDIN 0 -#define FDOUT 1 -#define FDLOG 2 -#define FDAUTH 3 +#define FDIN 0 +#define FDOUT 1 +#define FDLOG 2 +#define FDAUTH 3 #define BUFFER_SIZE 1024 -#define MAXHOPS 100 +#define MAXHOPS 100 unsigned long databytes = 0; int timeout = 1200; int modssl_info(); -ssize_t safewrite(int fd,char *buf,int len) +ssize_t safewrite(int fd, char *buf, int len) { int r; - r = timeoutwrite(timeout,fd,buf,len); + r = timeoutwrite(timeout, fd, buf, len); if (r <= 0) _exit(1); return r; } -ssize_t saferead(int fd,char *buf,int len) +ssize_t saferead(int fd, char *buf, int len) { int r; flush(); - r = timeoutread(timeout,fd,buf,len); - if (r == -1) if (errno == ETIMEDOUT) die_alarm(); + r = timeoutread(timeout, fd, buf, len); + if (r == -1) + if (errno == ETIMEDOUT) die_alarm(); if (r <= 0) die_read(); return r; } char inbuf[BUFFER_SIZE]; -buffer bi = BUFFER_INIT(saferead,FDIN,inbuf,sizeof(inbuf)); +buffer bi = BUFFER_INIT(saferead, FDIN, inbuf, sizeof(inbuf)); -char outbuf[BUFFER_SIZE/2]; -buffer bo = BUFFER_INIT(safewrite,FDOUT,outbuf,sizeof(outbuf)); +char outbuf[BUFFER_SIZE / 2]; +buffer bo = BUFFER_INIT(safewrite, FDOUT, outbuf, sizeof(outbuf)); char logbuf[256]; -buffer bl = BUFFER_INIT(write,FDLOG,logbuf,sizeof(logbuf)); +buffer bl = BUFFER_INIT(write, FDLOG, logbuf, sizeof(logbuf)); -void flush() { buffer_flush(&bo); } // this triggers writing to STDIO -void out(char *s) { buffer_puts(&bo,s); } +void flush() +{ + buffer_flush(&bo); +} // this triggers writing to STDIO +void out(char *s) +{ + buffer_puts(&bo, s); +} stralloc sa = {0}; ipalloc ia = {0}; @@ -96,7 +105,7 @@ int bhelocheck(void); /* this file is too long -------------------------------------- DNS helper */ -int dnsq(char *arg,char type) +int dnsq(char *arg, char type) { unsigned int random; int at; @@ -106,26 +115,27 @@ int dnsq(char *arg,char type) len = str_len(arg); if (len < 1) return r; - if (arg[len-1] == ' ') len--; /* trailing blank */ + if (arg[len - 1] == ' ') len--; /* trailing blank */ if (len < 1) return r; - at = byte_rchr(arg,len,'@'); + at = byte_rchr(arg, len, '@'); if (at < len) { - if (!stralloc_copyb(&sa,arg + at + 1,len - at - 1)) die_nomem(); - } else - if (!stralloc_copyb(&sa,arg,len)) die_nomem(); + if (!stralloc_copyb(&sa, arg + at + 1, len - at - 1)) die_nomem(); + } else if (!stralloc_copyb(&sa, arg, len)) + die_nomem(); random = now() + (getpid() << 16); - switch (type) { /* Common for IPv4 and IPv6 */ - case 'A': r = dns_ip(&ia,&sa); break; - case 'M': r = dns_mxip(&ia,&sa,random); break; + switch (type) { /* Common for IPv4 and IPv6 */ + case 'A': r = dns_ip(&ia, &sa); break; + case 'M': r = dns_mxip(&ia, &sa, random); break; } switch (r) { case DNS_ERR: out("451 DNS temporary failure (#4.3.0)\r\n"); return -1; case DNS_MEM: die_nomem(); - default: if (ia.len) return 0; + default: + if (ia.len) return 0; } return 1; @@ -137,8 +147,8 @@ static stralloc greeting = {0}; void smtp_greet(char *code) { - buffer_puts(&bo,code); - buffer_put(&bo,greeting.s,greeting.len); + buffer_puts(&bo, code); + buffer_put(&bo, greeting.s, greeting.len); } void smtp_help() { @@ -146,7 +156,10 @@ void smtp_help() } void smtp_quit() { - smtp_greet("221 "); out("\r\n"); flush(); _exit(0); + smtp_greet("221 "); + out("\r\n"); + flush(); + _exit(0); } char *remoteip; @@ -172,33 +185,47 @@ char *badhelocond; void dohelo(char *helo) { - if (!stralloc_copys(&helohost,helo)) die_nomem(); + if (!stralloc_copys(&helohost, helo)) die_nomem(); if (!stralloc_0(&helohost)) die_nomem(); - fakehelo = case_diffs(remotehost,helohost.s) ? helohost.s : 0; + fakehelo = case_diffs(remotehost, helohost.s) ? helohost.s : 0; if (helocheck) { if (str_len(helocheck) == 1) { switch (*helocheck) { - case '=': flagbadhelo = bhelocheck(); - if (fakehelo) { flagdnshelo = 1; badhelocond = "="; } - break; - case 'A': flagbadhelo = bhelocheck(); - if (flagbadhelo == 0) { flagdnshelo = dnsq(helohost.s,'A'); badhelocond = "A"; } - break; - case 'M': flagbadhelo = bhelocheck(); - if (flagbadhelo == 0) { flagdnshelo = dnsq(helohost.s,'M'); badhelocond = "M"; } - break; - case '.': flagbadhelo = bhelocheck(); - if (!str_len(helo)) flagbadhelo = -2; - break; - case '!': if (!str_len(helo)) flagbadhelo = -2; - break; + case '=': + flagbadhelo = bhelocheck(); + if (fakehelo) { + flagdnshelo = 1; + badhelocond = "="; + } + break; + case 'A': + flagbadhelo = bhelocheck(); + if (flagbadhelo == 0) { + flagdnshelo = dnsq(helohost.s, 'A'); + badhelocond = "A"; + } + break; + case 'M': + flagbadhelo = bhelocheck(); + if (flagbadhelo == 0) { + flagdnshelo = dnsq(helohost.s, 'M'); + badhelocond = "M"; + } + break; + case '.': + flagbadhelo = bhelocheck(); + if (!str_len(helo)) flagbadhelo = -2; + break; + case '!': + if (!str_len(helo)) flagbadhelo = -2; + break; } } else { - flagbadhelo = bhelocheck(); + flagbadhelo = bhelocheck(); } if (flagbadhelo == -3) flagbadhelo = 0; } - if (!env_put("HELOHOST",helohost.s)) die_nomem(); + if (!env_put("HELOHOST", helohost.s)) die_nomem(); } int liphostok = 0; @@ -208,7 +235,7 @@ int bmfok = 0; stralloc bmf = {0}; struct constmap mapbmf; -int brtok= 0; +int brtok = 0; stralloc brt = {0}; struct constmap mapbrt; @@ -220,16 +247,17 @@ static struct cdb cdbm; static struct cdb cdbl; static int fdbmt; -int flagmimetype = 0; /* 1: white; 2: cdb; 3: white+cdb; 4: !relay+white; 6: !relay+white+cdb; -1: found in cdb; -2: found white */ +int flagmimetype = + 0; /* 1: white; 2: cdb; 3: white+cdb; 4: !relay+white; 6: !relay+white+cdb; -1: found in cdb; -2: found white */ char *badmimeinit; static int fdblt; -int flagloadertype = 0; /* 1: cdb; 2: !relay+cdb; -1: found in cdb */ +int flagloadertype = 0; /* 1: cdb; 2: !relay+cdb; -1: found in cdb */ char *badloaderinit; static int fdmav; int flagmav = 0; -int localmf = 0; /* 1: domainpart->rcpthosts; 2: ->mailformrules; 3: ->remoteinfo; 4: ->DN(Email) */ +int localmf = 0; /* 1: domainpart->rcpthosts; 2: ->mailformrules; 3: ->remoteinfo; 4: ->DN(Email) */ char *localmfcheck; char *mfdnscheck; @@ -247,12 +275,14 @@ int greylist = 0; stralloc pgbind = {0}; char *auth; -int smtpauth = 0; /* -1:Cert 0:none 1:login/plain 2:cram 3:login/plain/cram 11:must_login/plain 12:must_2 13:must_3 */ +int smtpauth = + 0; /* -1:Cert 0:none 1:login/plain 2:cram 3:login/plain/cram 11:must_login/plain 12:must_2 13:must_3 */ int seenauth = 0; /* 1:ESMTPA 2:~CLIENTDN */ stralloc authmethod = {0}; -int starttls = 0; /* -1:TLS 0:none 1:STARTTLS 2:require_STARTTLS 3:relay_if_CLIENTDN 4:require_+_relay_if_CLIENTDN */ -int seentls = 0; /* 1:~STARTTLS 2:~TLS 3:~CLIENTDN */ +int starttls = + 0; /* -1:TLS 0:none 1:STARTTLS 2:require_STARTTLS 3:relay_if_CLIENTDN 4:require_+_relay_if_CLIENTDN */ +int seentls = 0; /* 1:~STARTTLS 2:~TLS 3:~CLIENTDN */ char *ucspitls = 0; char *tlsversion; char *cipher; @@ -279,39 +309,47 @@ void setup() flagip6 = 1; // GCC 10 implicit int declarition if global var if (control_init() == -1) die_control(); - if (control_rldef(&greeting,"control/smtpgreeting",1,(char *) 0) != 1) - die_control(); - liphostok = control_rldef(&liphost,"control/localiphost",1,(char *) 0); + if (control_rldef(&greeting, "control/smtpgreeting", 1, (char *)0) != 1) die_control(); + liphostok = control_rldef(&liphost, "control/localiphost", 1, (char *)0); if (liphostok == -1) die_control(); - if (control_readint(&timeout,"control/timeoutsmtpd") == -1) die_control(); + if (control_readint(&timeout, "control/timeoutsmtpd") == -1) die_control(); if (timeout <= 0) timeout = 1; if (rcpthosts_init() == -1) die_control(); if (recipients_init() == -1) die_control(); - bmfok = control_readfile(&bmf,"control/badmailfrom",0); + bmfok = control_readfile(&bmf, "control/badmailfrom", 0); if (bmfok == -1) die_control(); if (bmfok) - if (!constmap_init(&mapbmf,bmf.s,bmf.len,0)) die_nomem(); + if (!constmap_init(&mapbmf, bmf.s, bmf.len, 0)) die_nomem(); - brtok = control_readfile(&brt,"control/badrcptto",0); + brtok = control_readfile(&brt, "control/badrcptto", 0); if (brtok == -1) die_control(); if (brtok) - if (!constmap_init(&mapbrt,brt.s,brt.len,0)) die_nomem(); + if (!constmap_init(&mapbrt, brt.s, brt.len, 0)) die_nomem(); - if (control_readint(&databytes,"control/databytes") == -1) die_control(); + if (control_readint(&databytes, "control/databytes") == -1) die_control(); x = env_get("DATABYTES"); - if (x) { scan_ulong(x,&u); databytes = u; } + if (x) { + scan_ulong(x, &u); + databytes = u; + } if (!(databytes + 1)) --databytes; - if (!stralloc_copys(&protocol,"ESMTP")) die_nomem(); /* RFC 3848 */ - remoteip = env_get("TCP6REMOTEIP"); /* compactified IPv6 */ - if (!remoteip) remoteip = env_get("TCPREMOTEIP"); /* allow other tcpserver/sslserver */ - if (remoteip && (str_chr(remoteip,':') < str_len(remoteip))) { - if (byte_equal(remoteip,7,V4MAPPREFIX)) - { remoteip = remoteip + 7; flagip6 = 0; } - } else flagip6 = 0; - if (!remoteip) { remoteip = "unknown"; flagip6 = -1; } + if (!stralloc_copys(&protocol, "ESMTP")) die_nomem(); /* RFC 3848 */ + remoteip = env_get("TCP6REMOTEIP"); /* compactified IPv6 */ + if (!remoteip) remoteip = env_get("TCPREMOTEIP"); /* allow other tcpserver/sslserver */ + if (remoteip && (str_chr(remoteip, ':') < str_len(remoteip))) { + if (byte_equal(remoteip, 7, V4MAPPREFIX)) { + remoteip = remoteip + 7; + flagip6 = 0; + } + } else + flagip6 = 0; + if (!remoteip) { + remoteip = "unknown"; + flagip6 = -1; + } local = env_get("TCP6LOCALHOST"); if (!local) local = env_get("TCPLOCALHOST"); if (!local) local = env_get("TCP6LOCALIP"); @@ -327,35 +365,44 @@ void setup() if (!remoteinfo) remoteinfo = env_get("TCPREMOTEINFO"); relayclient = env_get("RELAYCLIENT"); - if (!case_diffs(localport,PORT_SMTPS)) { + if (!case_diffs(localport, PORT_SMTPS)) { if (!modssl_info()) die_starttls(); starttls = -1; } mfdnscheck = env_get("MFDNSCHECK"); x = env_get("MAXRECIPIENTS"); - if (x) { scan_ulong(x,&u); maxrcptcount = u; }; + if (x) { + scan_ulong(x, &u); + maxrcptcount = u; + }; if (!(maxrcptcount + 1)) --maxrcptcount; helocheck = env_get("HELOCHECK"); if (helocheck) { - badhelook = control_readfile(&badhelo,"control/badhelo",0); + badhelook = control_readfile(&badhelo, "control/badhelo", 0); if (badhelook == -1) die_control(); if (badhelook) - if (!constmap_init(&mapbhlo,badhelo.s,badhelo.len,0)) die_nomem(); + if (!constmap_init(&mapbhlo, badhelo.s, badhelo.len, 0)) die_nomem(); } x = env_get("TARPITCOUNT"); - if (x) { scan_ulong(x,&u); tarpitcount = u; }; + if (x) { + scan_ulong(x, &u); + tarpitcount = u; + }; x = env_get("TARPITDELAY"); - if (x) { scan_ulong(x,&u); tarpitdelay = u; }; + if (x) { + scan_ulong(x, &u); + tarpitdelay = u; + }; x = env_get("POSTGREY"); // RFC 6647 if (x) { - if (case_diffs(x,"-")) { + if (case_diffs(x, "-")) { greylist = 1; - if (!stralloc_copys(&pgbind,x)) die_nomem(); - if (!stralloc_append(&pgbind,"")) die_nomem(); + if (!stralloc_copys(&pgbind, x)) die_nomem(); + if (!stralloc_append(&pgbind, "")) die_nomem(); } } @@ -365,7 +412,7 @@ void setup() if (str_len(localmfcheck) == 1 && *localmfcheck == '!') { localmf = 2; fdmav = open_read("control/mailfromrules.cdb"); - if (fdmav == -1 ) localmf = 1; + if (fdmav == -1) localmf = 1; } if (str_len(localmfcheck) == 1 && *localmfcheck == '=') { localmf = 3; @@ -386,7 +433,7 @@ void setup() if (*badmimeinit == '+') flagmimetype = 4; } } - if (fdbmt != -1 ) flagmimetype = flagmimetype + 2; + if (fdbmt != -1) flagmimetype = flagmimetype + 2; } badloaderinit = env_get("BADLOADERTYPE"); @@ -398,7 +445,7 @@ void setup() flagloadertype = 1; if (*badloaderinit == '+') flagloadertype = 2; fdblt = open_read("control/badloadertypes.cdb"); - if (fdblt == -1 ) flagloadertype = 0; + if (fdblt == -1) flagloadertype = 0; } } } @@ -408,67 +455,69 @@ void setup() auth = env_get("SMTPAUTH"); if (auth) { smtpauth = 1; - if (!case_diffs(auth,"-")) smtpauth = 0; - if (!case_diffs(auth,"!")) smtpauth = 11; - if (case_starts(auth,"cram")) smtpauth = 2; - if (case_starts(auth,"+cram")) smtpauth = 3; - if (case_starts(auth,"!cram")) smtpauth = 12; - if (case_starts(auth,"!+cram")) smtpauth = 13; + if (!case_diffs(auth, "-")) smtpauth = 0; + if (!case_diffs(auth, "!")) smtpauth = 11; + if (case_starts(auth, "cram")) smtpauth = 2; + if (case_starts(auth, "+cram")) smtpauth = 3; + if (case_starts(auth, "!cram")) smtpauth = 12; + if (case_starts(auth, "!+cram")) smtpauth = 13; } if (!seentls) { ucspitls = env_get("UCSPITLS"); if (ucspitls) { starttls = 1; - if (!case_diffs(ucspitls,"-")) starttls = 0; - if (!case_diffs(ucspitls,"!")) starttls = 2; - if (!case_diffs(ucspitls,"?")) starttls = 3; - if (!case_diffs(ucspitls,"@")) starttls = 4; + if (!case_diffs(ucspitls, "-")) starttls = 0; + if (!case_diffs(ucspitls, "!")) starttls = 2; + if (!case_diffs(ucspitls, "?")) starttls = 3; + if (!case_diffs(ucspitls, "@")) starttls = 4; } } delivermailto = env_get("DELIVERTO"); if (delivermailto) { - if (!stralloc_cats(&mailto,delivermailto)) die_nomem(); - if (!stralloc_cats(&mailto," ")) die_nomem(); + if (!stralloc_cats(&mailto, delivermailto)) die_nomem(); + if (!stralloc_cats(&mailto, " ")) die_nomem(); } rblsmtpd = env_get("RBLSMTPD"); if (rblsmtpd) { - if (!stralloc_cats(&rblinfo,rblsmtpd)) die_nomem(); + if (!stralloc_cats(&rblinfo, rblsmtpd)) die_nomem(); if (!stralloc_0(&rblinfo)) die_nomem(); } x = env_get("SPF"); - if (x) { scan_ulong(x,&u); flagspf = u; } + if (x) { + scan_ulong(x, &u); + flagspf = u; + } if (flagspf < 0 || flagspf > 6) die_control(); if (flagspf) { - r = control_readline(&spflocalrules,"control/spflocalrules"); + r = control_readline(&spflocalrules, "control/spflocalrules"); if (r == -1) die_control(); if (!stralloc_0(&spflocalrules)) die_nomem(); - if (control_rldef(&spfexplain,"control/spfexplain",0,SPF_DEFEXP) == -1) die_control(); + if (control_rldef(&spfexplain, "control/spfexplain", 0, SPF_DEFEXP) == -1) die_control(); if (!stralloc_0(&spfexplain)) die_nomem(); } x = env_get("UTF8"); if (x) flagutf8 = 1; - if (!stralloc_copys(&helohost,"")) die_nomem(); // helohost is initially empty + if (!stralloc_copys(&helohost, "")) die_nomem(); // helohost is initially empty if (!stralloc_0(&helohost)) die_nomem(); fakehelo = 0; - } void auth_info(char *method) { - if (!env_put("AUTHPROTOCOL",method)) die_nomem(); - if (!env_put("AUTHUSER",remoteinfo)) die_nomem(); - if (!env_unset("TCPREMOTEINFO")) die_read(); - if (!env_put("TCPREMOTEINFO",remoteinfo)) die_nomem(); - if (!env_unset("TCP6REMOTEINFO")) die_read(); - if (!env_put("TCP6REMOTEINFO",remoteinfo)) die_nomem(); - - if (!stralloc_append(&protocol,"A")) die_nomem(); + if (!env_put("AUTHPROTOCOL", method)) die_nomem(); + if (!env_put("AUTHUSER", remoteinfo)) die_nomem(); + if (!env_unset("TCPREMOTEINFO")) die_read(); + if (!env_put("TCPREMOTEINFO", remoteinfo)) die_nomem(); + if (!env_unset("TCP6REMOTEINFO")) die_read(); + if (!env_put("TCP6REMOTEINFO", remoteinfo)) die_nomem(); + + if (!stralloc_append(&protocol, "A")) die_nomem(); } int modssl_info() @@ -483,7 +532,8 @@ int modssl_info() cipherused = env_get("SSL_CIPHER_USEKEYSIZE"); if (!cipherused) cipherused = "unknown"; clientdn = env_get("SSL_CLIENT_S_DN"); - if (!clientdn) clientdn = "none"; + if (!clientdn) + clientdn = "none"; else { seentls = 3; seenauth = 2; @@ -491,26 +541,26 @@ int modssl_info() relayclient = ""; } - if (!stralloc_copys(&tlsinfo,tlsversion)) die_nomem(); - if (!stralloc_cats(&tlsinfo,": ")) die_nomem(); - if (!stralloc_cats(&tlsinfo,cipher)) die_nomem(); - if (!stralloc_cats(&tlsinfo," [")) die_nomem(); - if (!stralloc_cats(&tlsinfo,cipherused)) die_nomem(); - if (!stralloc_cats(&tlsinfo,"/")) die_nomem(); - if (!stralloc_cats(&tlsinfo,cipherperm)) die_nomem(); - if (!stralloc_cats(&tlsinfo,"] \n")) die_nomem(); - if (!stralloc_cats(&tlsinfo," DN=")) die_nomem(); - if (!stralloc_cats(&tlsinfo,clientdn)) die_nomem(); + if (!stralloc_copys(&tlsinfo, tlsversion)) die_nomem(); + if (!stralloc_cats(&tlsinfo, ": ")) die_nomem(); + if (!stralloc_cats(&tlsinfo, cipher)) die_nomem(); + if (!stralloc_cats(&tlsinfo, " [")) die_nomem(); + if (!stralloc_cats(&tlsinfo, cipherused)) die_nomem(); + if (!stralloc_cats(&tlsinfo, "/")) die_nomem(); + if (!stralloc_cats(&tlsinfo, cipherperm)) die_nomem(); + if (!stralloc_cats(&tlsinfo, "] \n")) die_nomem(); + if (!stralloc_cats(&tlsinfo, " DN=")) die_nomem(); + if (!stralloc_cats(&tlsinfo, clientdn)) die_nomem(); if (!stralloc_0(&tlsinfo)) die_nomem(); - if (!stralloc_append(&protocol,"S")) die_nomem(); + if (!stralloc_append(&protocol, "S")) die_nomem(); if (seentls == 3 && starttls == 4) { clientcn = env_get("SSL_CLIENT_S_DN_CN"); - remoteinfo = clientcn ? clientcn : clientdn; + remoteinfo = clientcn ? clientcn : clientdn; dnemail = env_get("SSL_CLIENT_S_DN_Email"); if (!dnemail) dnemail = "unknown"; - if (!stralloc_cats(&authmethod,"cert")) die_nomem(); + if (!stralloc_cats(&authmethod, "cert")) die_nomem(); auth_info(authmethod.s); } return 1; @@ -545,50 +595,52 @@ int addrparse(char *arg) int flagquoted; terminator = '>'; - i = str_chr(arg,'<'); + i = str_chr(arg, '<'); if (arg[i]) arg += i + 1; else return 0; /* strip source route */ - if (*arg == '@') while (*arg) if (*arg++ == ':') break; + if (*arg == '@') + while (*arg) + if (*arg++ == ':') break; - if (!stralloc_copys(&addr,"")) die_nomem(); + if (!stralloc_copys(&addr, "")) die_nomem(); flagesc = 0; flagquoted = 0; for (i = 0; ch = arg[i]; ++i) { /* copy arg to addr, stripping quotes */ if (flagesc) { - if (!stralloc_append(&addr,&ch)) die_nomem(); + if (!stralloc_append(&addr, &ch)) die_nomem(); flagesc = 0; - } - else { + } else { if (!flagquoted && (ch == terminator)) break; switch (ch) { case '\\': flagesc = 1; break; - case '"': flagquoted = !flagquoted; break; - default: if (!stralloc_append(&addr,&ch)) die_nomem(); + case '"': flagquoted = !flagquoted; break; + default: + if (!stralloc_append(&addr, &ch)) die_nomem(); } } } /* could check for termination failure here, but why bother? */ - if (!stralloc_append(&addr,"")) die_nomem(); + if (!stralloc_append(&addr, "")) die_nomem(); if (liphostok) { - i = byte_rchr(addr.s,addr.len,'@'); + i = byte_rchr(addr.s, addr.len, '@'); if (i < addr.len) /* if not, partner should go read rfc 821 */ if (addr.s[i + 1] == '[') { - if (byte_rchr(addr.s + i + 2,addr.len - i - 2,':') < str_len(addr.s)) { /* @[IPv6::] */ - if (!addr.s[i + 1 + ip6_scanbracket(addr.s + i + 1,(char *)&ip6s.d)]) - if (ipme_is6(&ip6s)) { - addr.len = i + 1; - if (!stralloc_cat(&addr,&liphost)) die_nomem(); - } - } else { /* @[IPv4] */ - if (!addr.s[i + 1 + ip4_scanbracket(addr.s + i + 1,(char *)&ip4s.d)]) + if (byte_rchr(addr.s + i + 2, addr.len - i - 2, ':') < str_len(addr.s)) { /* @[IPv6::] */ + if (!addr.s[i + 1 + ip6_scanbracket(addr.s + i + 1, (char *)&ip6s.d)]) + if (ipme_is6(&ip6s)) { + addr.len = i + 1; + if (!stralloc_cat(&addr, &liphost)) die_nomem(); + } + } else { /* @[IPv4] */ + if (!addr.s[i + 1 + ip4_scanbracket(addr.s + i + 1, (char *)&ip4s.d)]) if (ipme_is4(&ip4s)) { addr.len = i + 1; - if (!stralloc_cat(&addr,&liphost)) die_nomem(); + if (!stralloc_cat(&addr, &liphost)) die_nomem(); } } } @@ -607,19 +659,19 @@ int bhelocheck() char subvalue; if (badhelook && helohost.len > 1) { /* helohost! */ - if (!stralloc_copyb(&eddr,helohost.s,helohost.len - 1)) die_nomem(); - if (!stralloc_append(&eddr,"!")) die_nomem(); + if (!stralloc_copyb(&eddr, helohost.s, helohost.len - 1)) die_nomem(); + if (!stralloc_append(&eddr, "!")) die_nomem(); if (!stralloc_0(&eddr)) die_nomem(); - if (constmap(&mapbhlo,eddr.s,eddr.len - 1)) return -3; + if (constmap(&mapbhlo, eddr.s, eddr.len - 1)) return -3; - if (constmap(&mapbhlo,helohost.s,helohost.len - 1)) return -1; + if (constmap(&mapbhlo, helohost.s, helohost.len - 1)) return -1; i = 0; for (j = 0; j < badhelo.len; ++j) if (!badhelo.s[j]) { subvalue = badhelo.s[i] != '!'; if (!subvalue) i++; - if ((k != subvalue) && wildmat(helohost.s,badhelo.s + i)) k = subvalue; + if ((k != subvalue) && wildmat(helohost.s, badhelo.s + i)) k = subvalue; i = j + 1; } return k; @@ -639,74 +691,75 @@ int bmfcheck() if (bmfok && mailfrom.len > 1) { rlen = str_len(remotehost); - at = byte_rchr(mailfrom.s,mailfrom.len,'@'); + at = byte_rchr(mailfrom.s, mailfrom.len, '@'); -/* '?' enhanced address to skip all other tests including MFDNSCHECK */ + /* '?' enhanced address to skip all other tests including MFDNSCHECK */ - if (!stralloc_copys(&eddr,"?")) die_nomem(); - if (!stralloc_cat(&eddr,&mailfrom)) die_nomem(); - case_lowerb(eddr.s,eddr.len); - if (constmap(&mapbmf,eddr.s,eddr.len - 1)) return -110; + if (!stralloc_copys(&eddr, "?")) die_nomem(); + if (!stralloc_cat(&eddr, &mailfrom)) die_nomem(); + case_lowerb(eddr.s, eddr.len); + if (constmap(&mapbmf, eddr.s, eddr.len - 1)) return -110; -/* '+' extended address for none-RELAYCLIENTS */ + /* '+' extended address for none-RELAYCLIENTS */ if (at && !relayclient) { - if (!stralloc_copyb(&eddr,mailfrom.s,mailfrom.len - 1)) die_nomem(); - if (!stralloc_append(&eddr,"+")) die_nomem(); + if (!stralloc_copyb(&eddr, mailfrom.s, mailfrom.len - 1)) die_nomem(); + if (!stralloc_append(&eddr, "+")) die_nomem(); if (!stralloc_0(&eddr)) die_nomem(); - case_lowerb(eddr.s,eddr.len); - if (constmap(&mapbmf,eddr.s + at,eddr.len - at - 1)) return -5; + case_lowerb(eddr.s, eddr.len); + if (constmap(&mapbmf, eddr.s + at, eddr.len - at - 1)) return -5; } -/* '-' extended address from UNKNOWN */ + /* '-' extended address from UNKNOWN */ - if (at && !case_diffs(remotehost,"unknown")) { - if (!stralloc_copyb(&eddr,mailfrom.s,mailfrom.len - 1)) die_nomem(); - if (!stralloc_append(&eddr,"-")) die_nomem(); + if (at && !case_diffs(remotehost, "unknown")) { + if (!stralloc_copyb(&eddr, mailfrom.s, mailfrom.len - 1)) die_nomem(); + if (!stralloc_append(&eddr, "-")) die_nomem(); if (!stralloc_0(&eddr)) die_nomem(); - case_lowerb(eddr.s,eddr.len); - if (constmap(&mapbmf,eddr.s + at,eddr.len - at - 1)) return -4; + case_lowerb(eddr.s, eddr.len); + if (constmap(&mapbmf, eddr.s + at, eddr.len - at - 1)) return -4; } -/* '=' extended address for WELLKNOWN senders */ + /* '=' extended address for WELLKNOWN senders */ - else if (at && rlen >= mailfrom.len - at - 1) { + else if (at && rlen >= mailfrom.len - at - 1) + { dlen = mailfrom.len - at - 2; - if (!stralloc_copyb(&eddr,mailfrom.s,mailfrom.len - 1)) die_nomem(); - if (!stralloc_append(&eddr,"=")) die_nomem(); + if (!stralloc_copyb(&eddr, mailfrom.s, mailfrom.len - 1)) die_nomem(); + if (!stralloc_append(&eddr, "=")) die_nomem(); if (!stralloc_0(&eddr)) die_nomem(); - case_lowerb(eddr.s,eddr.len); - if (str_diffn(remotehost + rlen - dlen,eddr.s + at + 1,dlen)) - if (constmap(&mapbmf,eddr.s + at,eddr.len - at - 1)) return -3; + case_lowerb(eddr.s, eddr.len); + if (str_diffn(remotehost + rlen - dlen, eddr.s + at + 1, dlen)) + if (constmap(&mapbmf, eddr.s + at, eddr.len - at - 1)) return -3; -/* '~' extended address for MISMATCHED Domains */ + /* '~' extended address for MISMATCHED Domains */ - if (case_diffrs(remotehost,mailfrom.s + at + 1)) { + if (case_diffrs(remotehost, mailfrom.s + at + 1)) { j = 0; do { - if (!stralloc_copys(&eddr,"~")) die_nomem(); - if (!stralloc_cats(&eddr,remotehost + j)) die_nomem(); + if (!stralloc_copys(&eddr, "~")) die_nomem(); + if (!stralloc_cats(&eddr, remotehost + j)) die_nomem(); if (!stralloc_0(&eddr)) die_nomem(); - if (constmap(&mapbmf,eddr.s,eddr.len - 1)) return -2; - j = byte_chr(remotehost + j,rlen - j,'.') + j + 1; + if (constmap(&mapbmf, eddr.s, eddr.len - 1)) return -2; + j = byte_chr(remotehost + j, rlen - j, '.') + j + 1; } while (j > 0 && rlen - j > 0); } } -/* Standard */ + /* Standard */ - if (constmap(&mapbmf,mailfrom.s,mailfrom.len - 1)) return -1; + if (constmap(&mapbmf, mailfrom.s, mailfrom.len - 1)) return -1; if (at && at < mailfrom.len) - if (constmap(&mapbmf,mailfrom.s + at,mailfrom.len - at - 1)) return -1; + if (constmap(&mapbmf, mailfrom.s + at, mailfrom.len - at - 1)) return -1; -/* Wildmating */ + /* Wildmating */ i = k = 0; for (j = 0; j < bmf.len; ++j) { if (!bmf.s[j]) { subvalue = bmf.s[i] != '!'; if (!subvalue) i++; - if ((k != subvalue) && wildmat(mailfrom.s,bmf.s + i)) k = subvalue; + if ((k != subvalue) && wildmat(mailfrom.s, bmf.s + i)) k = subvalue; i = j + 1; } } @@ -724,20 +777,20 @@ int brtcheck() char subvalue; if (brtok) { - if (constmap(&mapbrt,addr.s,addr.len - 1)) return -2; + if (constmap(&mapbrt, addr.s, addr.len - 1)) return -2; - int at = byte_rchr(addr.s,addr.len,'@'); + int at = byte_rchr(addr.s, addr.len, '@'); if (at < addr.len) - if (constmap(&mapbrt,addr.s + at,addr.len - at - j)) return -1; + if (constmap(&mapbrt, addr.s + at, addr.len - at - j)) return -1; -/* '#' enhanced address to consider invalid rcptto addresses for none-relayclients */ + /* '#' enhanced address to consider invalid rcptto addresses for none-relayclients */ if (!relayclient) { - if (!stralloc_copys(&eddr,"+")) die_nomem(); - if (!stralloc_cat(&eddr,&addr)) die_nomem(); + if (!stralloc_copys(&eddr, "+")) die_nomem(); + if (!stralloc_cat(&eddr, &addr)) die_nomem(); if (!stralloc_0(&eddr)) die_nomem(); - case_lowerb(eddr.s,eddr.len); - if (constmap(&mapbmf,eddr.s,eddr.len - 1)) return 110; + case_lowerb(eddr.s, eddr.len); + if (constmap(&mapbmf, eddr.s, eddr.len - 1)) return 110; } i = 0; @@ -745,7 +798,7 @@ int brtcheck() if (!brt.s[j]) { subvalue = brt.s[i] != '!'; if (!subvalue) i++; - if ((k != subvalue) && wildmat(addr.s,brt.s + i)) k = subvalue; + if ((k != subvalue) && wildmat(addr.s, brt.s + i)) k = subvalue; i = j + 1; } return k; @@ -756,7 +809,7 @@ int brtcheck() int addrallowed(char *arg) { int r; - r = rcpthosts(arg,str_len(arg)); + r = rcpthosts(arg, str_len(arg)); if (r == -1) die_control(); return r; } @@ -764,7 +817,7 @@ int addrallowed(char *arg) int rcptallowed() { int r; - r = recipients(addr.s,str_len(addr.s)); + r = recipients(addr.s, str_len(addr.s)); #ifdef PAM111421 if (r == 111) die_recipients(); #endif @@ -780,24 +833,23 @@ int localaddr(char *mf) int mflen; mflen = str_len(mf); - if (mflen < 1 ) return 0; + if (mflen < 1) return 0; if (localmf == 4) { - if (!case_diffs(dnemail,mf)) return 2; + if (!case_diffs(dnemail, mf)) return 2; return -4; } if (localmf == 3) { - if (!case_diffs(remoteinfo,mf)) return 2; + if (!case_diffs(remoteinfo, mf)) return 2; return -3; - } - else if (localmf == 2) - return mfrules(fdmav,remoteip,remotehost,remoteinfo,mf); + } else if (localmf == 2) + return mfrules(fdmav, remoteip, remotehost, remoteinfo, mf); else { if (str_len(localmfcheck) > 1) { - case_lowerb(localmfcheck,str_len(localmfcheck)); - at = byte_rchr(mf,mflen,'@'); + case_lowerb(localmfcheck, str_len(localmfcheck)); + at = byte_rchr(mf, mflen, '@'); if (at < mflen) - if (!str_diffn(localmfcheck,mf + at + 1,mflen - at - 1)) return 2; + if (!str_diffn(localmfcheck, mf + at + 1, mflen - at - 1)) return 2; } if (addrallowed(mf)) return 3; return -2; @@ -808,47 +860,59 @@ int spf_check(int flag6) { int r; - if (mailfrom.len <= 1) { flagspf = 0; return 0; } + if (mailfrom.len <= 1) { + flagspf = 0; + return 0; + } DNS_INIT - r = spf_query(remoteip,helohost.s,mailfrom.s,local,flag6); + r = spf_query(remoteip, helohost.s, mailfrom.s, local, flag6); if (r == SPF_NOMEM) die_nomem(); if (!stralloc_0(&spfinfo)) die_nomem(); switch (r) { case SPF_ME: - case SPF_OK: - if (!env_put("SPFRESULT","pass")) die_nomem(); + case SPF_OK: + if (!env_put("SPFRESULT", "pass")) die_nomem(); flagspf = 10; break; case SPF_LOOP: - case SPF_ERROR: + case SPF_ERROR: case SPF_SYNTAX: case SPF_EXHAUST: - if (!env_put("SPFRESULT","error")) die_nomem(); - if (flagspf < 2) { flagspf = 0; break; } + if (!env_put("SPFRESULT", "error")) die_nomem(); + if (flagspf < 2) { + flagspf = 0; + break; + } out("451 SPF lookup failure (#4.3.0)\r\n"); return -1; - case SPF_NONE: - if (!env_put("SPFRESULT","none")) die_nomem(); + case SPF_NONE: + if (!env_put("SPFRESULT", "none")) die_nomem(); flagspf = 0; break; case SPF_UNKNOWN: - if (!env_put("SPFRESULT","unknown")) die_nomem(); - if (flagspf < 6) break; - else return 4; + if (!env_put("SPFRESULT", "unknown")) die_nomem(); + if (flagspf < 6) + break; + else + return 4; case SPF_NEUTRAL: - if (!env_put("SPFRESULT","neutral")) die_nomem(); - if (flagspf < 5) break; - else return 3; + if (!env_put("SPFRESULT", "neutral")) die_nomem(); + if (flagspf < 5) + break; + else + return 3; case SPF_SOFTFAIL: - if (!env_put("SPFRESULT","softfail")) die_nomem(); - if (flagspf < 4) break; - else return 2; + if (!env_put("SPFRESULT", "softfail")) die_nomem(); + if (flagspf < 4) + break; + else + return 2; case SPF_FAIL: - if (!env_put("SPFRESULT","fail")) die_nomem(); + if (!env_put("SPFRESULT", "fail")) die_nomem(); if (flagspf < 3) break; - if (!spf_parse(&spfbounce,spfexpmsg.s,expdomain.s)) die_nomem(); + if (!spf_parse(&spfbounce, spfexpmsg.s, expdomain.s)) die_nomem(); return 1; } @@ -862,32 +926,36 @@ int mailfrom_size(char *arg) unsigned long r; unsigned long sizebytes = 0; - scan_ulong(arg,&r); + scan_ulong(arg, &r); sizebytes = r; - if (databytes) if (sizebytes > databytes) return 1; + if (databytes) + if (sizebytes > databytes) return 1; return 0; } -void mailfrom_auth(char *arg,int len) +void mailfrom_auth(char *arg, int len) { - if (!stralloc_copys(&fuser,"")) die_nomem(); - if (case_starts(arg,"<>")) { - if (!stralloc_cats(&fuser,"unknown")) die_nomem(); + if (!stralloc_copys(&fuser, "")) die_nomem(); + if (case_starts(arg, "<>")) { + if (!stralloc_cats(&fuser, "unknown")) die_nomem(); } else { while (len) { if (*arg == '+') { - if (case_starts(arg,"+3D")) { - arg = arg + 2; len = len - 2; - if (!stralloc_cats(&fuser,"=")) die_nomem(); + if (case_starts(arg, "+3D")) { + arg = arg + 2; + len = len - 2; + if (!stralloc_cats(&fuser, "=")) die_nomem(); } - if (case_starts(arg,"+2B")) { - arg = arg + 2; len = len - 2; - if (!stralloc_cats(&fuser,"+")) die_nomem(); + if (case_starts(arg, "+2B")) { + arg = arg + 2; + len = len - 2; + if (!stralloc_cats(&fuser, "+")) die_nomem(); } } else { - if (!stralloc_catb(&fuser,arg,1)) die_nomem(); + if (!stralloc_catb(&fuser, arg, 1)) die_nomem(); } - arg++; len--; + arg++; + len--; } } @@ -895,9 +963,9 @@ void mailfrom_auth(char *arg,int len) if (!remoteinfo) { remoteinfo = fuser.s; if (!env_unset("TCPREMOTEINFO")) die_read(); - if (!env_put("TCPREMOTEINFO",remoteinfo)) die_nomem(); + if (!env_put("TCPREMOTEINFO", remoteinfo)) die_nomem(); if (!env_unset("TCP6REMOTEINFO")) die_read(); - if (!env_put("TCP6REMOTEINFO",remoteinfo)) die_nomem(); + if (!env_put("TCP6REMOTEINFO", remoteinfo)) die_nomem(); } } @@ -906,18 +974,23 @@ void mailfrom_parms(char *arg) int len; if ((len = str_len(arg))) { - if (!stralloc_copys(&mfparms,"")) die_nomem(); - while (len) { - arg++; len--; - if (*arg == ' ' || *arg == '\0' ) { - if (flagutf8) if (case_starts(mfparms.s,"SMTPUTF8")) flagutf8 = 2; - if (case_starts(mfparms.s,"SIZE=")) if (mailfrom_size(mfparms.s + 5)) { flagsize = 1; return; } - if (case_starts(mfparms.s,"AUTH=")) mailfrom_auth(mfparms.s + 5,mfparms.len - 5); - if (!stralloc_copys(&mfparms,"")) die_nomem(); - } - else - if (!stralloc_catb(&mfparms,arg,1)) die_nomem(); - } + if (!stralloc_copys(&mfparms, "")) die_nomem(); + while (len) { + arg++; + len--; + if (*arg == ' ' || *arg == '\0') { + if (flagutf8) + if (case_starts(mfparms.s, "SMTPUTF8")) flagutf8 = 2; + if (case_starts(mfparms.s, "SIZE=")) + if (mailfrom_size(mfparms.s + 5)) { + flagsize = 1; + return; + } + if (case_starts(mfparms.s, "AUTH=")) mailfrom_auth(mfparms.s + 5, mfparms.len - 5); + if (!stralloc_copys(&mfparms, "")) die_nomem(); + } else if (!stralloc_catb(&mfparms, arg, 1)) + die_nomem(); + } } } @@ -925,8 +998,12 @@ void mailfrom_parms(char *arg) void smtp_helo(char *arg) { - smtp_greet("250 "); out("\r\n"); flush(); - seenmail = 0; rcptcount = 0; seenhelo++; + smtp_greet("250 "); + out("\r\n"); + flush(); + seenmail = 0; + rcptcount = 0; + seenhelo++; dohelo(arg); } @@ -934,30 +1011,39 @@ void smtp_ehlo(char *arg) { char size[FMT_ULONG]; - smtp_greet("250-"); out("\r\n"); + smtp_greet("250-"); + out("\r\n"); out("250-PIPELINING\r\n250-8BITMIME\r\n"); if (flagutf8) out("250-SMTPUTF8\r\n"); if (starttls > 0 && !seentls) out("250-STARTTLS\r\n"); - + switch (smtpauth) { - case 1: case 11: out("250-AUTH LOGIN PLAIN\r\n"); break; - case 2: case 12: out("250-AUTH CRAM-MD5\r\n"); break; - case 3: case 13: out("250-AUTH LOGIN PLAIN CRAM-MD5\r\n"); break; + case 1: + case 11: out("250-AUTH LOGIN PLAIN\r\n"); break; + case 2: + case 12: out("250-AUTH CRAM-MD5\r\n"); break; + case 3: + case 13: out("250-AUTH LOGIN PLAIN CRAM-MD5\r\n"); break; } - size[fmt_ulong(size,(unsigned long) databytes)] = 0; - out("250 SIZE "); out(size); out("\r\n"); + size[fmt_ulong(size, (unsigned long)databytes)] = 0; + out("250 SIZE "); + out(size); + out("\r\n"); - seenhelo++; seenmail = 0; rcptcount = 0; + seenhelo++; + seenmail = 0; + rcptcount = 0; dohelo(arg); } void smtp_rset(void) { - seenmail = 0; rcptcount = 0; /* RFC 5321: seenauth + seentls stay */ + seenmail = 0; + rcptcount = 0; /* RFC 5321: seenauth + seentls stay */ - if (!stralloc_copys(&mailfrom,"")) die_nomem(); - if (!stralloc_copys(&rcptto,"")) die_nomem(); + if (!stralloc_copys(&mailfrom, "")) die_nomem(); + if (!stralloc_copys(&rcptto, "")) die_nomem(); out("250 flushed\r\n"); } @@ -969,36 +1055,42 @@ void smtp_starttls() flush(); if (!starttls_init()) die_starttls(); - buffer_init(&bi,saferead,FDIN,inbuf,sizeof(inbuf)); + buffer_init(&bi, saferead, FDIN, inbuf, sizeof(inbuf)); seentls = 2; if (!starttls_info()) die_starttls(); if (!modssl_info()) die_starttls(); -/* reset SMTP state */ + /* reset SMTP state */ - seenhelo = 0; seenmail = 0; rcptcount = 0; - if (!stralloc_copys(&addr,"")) die_nomem(); - if (!stralloc_copys(&helohost,"")) die_nomem(); - if (!stralloc_copys(&mailfrom,"")) die_nomem(); - if (!stralloc_copys(&rcptto,"")) die_nomem(); + seenhelo = 0; + seenmail = 0; + rcptcount = 0; + if (!stralloc_copys(&addr, "")) die_nomem(); + if (!stralloc_copys(&helohost, "")) die_nomem(); + if (!stralloc_copys(&mailfrom, "")) die_nomem(); + if (!stralloc_copys(&rcptto, "")) die_nomem(); if (seenauth == 1) seenauth = 0; /* Otherwise Auth by client Cert */ } void smtp_mail(char *arg) { - if (flagutf8) if (!stralloc_cats(&protocol,"UTF8")) die_nomem(); + if (flagutf8) + if (!stralloc_cats(&protocol, "UTF8")) die_nomem(); if (!stralloc_0(&protocol)) die_nomem(); - if ((starttls > 1) && !seentls) { - err_tlsreq("Reject::TLS::missing",protocol.s,remoteip,remotehost,helohost.s); + if ((starttls > 1) && !seentls) { + err_tlsreq("Reject::TLS::missing", protocol.s, remoteip, remotehost, helohost.s); + return; + } + if (smtpauth > 10 && !seenauth) { + err_authreq("Reject::AUTH::missing", protocol.s, remoteip, remotehost, helohost.s); return; } - if (smtpauth > 10 && !seenauth) { - err_authreq("Reject::AUTH::missing",protocol.s,remoteip,remotehost,helohost.s); + if (!addrparse(arg)) { + err_syntax(); return; } - if (!addrparse(arg)) { err_syntax(); return; } flagsize = 0; rcptcount = 0; @@ -1006,16 +1098,17 @@ void smtp_mail(char *arg) seenmail++; if (relayclient && localmf) { flagmav = localaddr(addr.s); - if (flagmav > 0) if (!stralloc_append(&protocol,"M")) die_nomem(); + if (flagmav > 0) + if (!stralloc_append(&protocol, "M")) die_nomem(); } - if (!stralloc_copys(&rcptto,"")) die_nomem(); - if (!stralloc_copys(&mailfrom,addr.s)) die_nomem(); + if (!stralloc_copys(&rcptto, "")) die_nomem(); + if (!stralloc_copys(&mailfrom, addr.s)) die_nomem(); if (!stralloc_0(&mailfrom)) die_nomem(); - if (!env_put("MAILFROM",mailfrom.s)) die_nomem(); + if (!env_put("MAILFROM", mailfrom.s)) die_nomem(); flagaddr = bmfcheck(); - if (flagaddr != -110) - if (mfdnscheck) flagdnsmf = dnsq(mailfrom.s,'M'); + if (flagaddr != -110) + if (mfdnscheck) flagdnsmf = dnsq(mailfrom.s, 'M'); out("250 ok\r\n"); } @@ -1027,17 +1120,15 @@ int postgrey_scanner() int child; int wstat; - char *postgrey_scannerarg[] = {"bin/qmail-postgrey",pgbind.s,mailfrom.s,addr.s,remoteip,remotehost,0}; + char *postgrey_scannerarg[] = { + "bin/qmail-postgrey", pgbind.s, mailfrom.s, addr.s, remoteip, remotehost, 0}; switch (child = fork()) { - case -1: - return err_forkgl(); - case 0: - execv(*postgrey_scannerarg,postgrey_scannerarg); - _exit(1); + case -1: return err_forkgl(); + case 0: execv(*postgrey_scannerarg, postgrey_scannerarg); _exit(1); } - wait_pid(&wstat,child); + wait_pid(&wstat, child); if (wait_crashed(wstat)) return err_postgl(); switch (wait_exitcode(wstat)) { @@ -1049,89 +1140,164 @@ int postgrey_scanner() void smtp_rcpt(char *arg) { char *rcptok = 0; - if (!seenmail) { err_wantmail(); return; } - if (!addrparse(arg)) { err_syntax(); return; } + if (!seenmail) { + err_wantmail(); + return; + } + if (!addrparse(arg)) { + err_syntax(); + return; + } rcptcount++; -/* this file is too long --------------------------------- Split Horizon envelope checks */ + /* this file is too long --------------------------------- Split Horizon envelope checks */ if (!relayclient) { - if (!seenhelo && helocheck) /* Helo rejects */ - if (str_len(helocheck) == 1) { - err_helo("Reject::SNDR::Bad_Helo",protocol.s,remoteip,remotehost,helohost.s,mailfrom.s,addr.s,"0"); - return; - } + if (!seenhelo && helocheck) /* Helo rejects */ + if (str_len(helocheck) == 1) { + err_helo( + "Reject::SNDR::Bad_Helo", + protocol.s, + remoteip, + remotehost, + helohost.s, + mailfrom.s, + addr.s, + "0"); + return; + } if (flagbadhelo) { - switch (flagbadhelo) { - case -2: badhelocond = "!"; break; - case -1: badhelocond = "."; break; - default: badhelocond = "*"; break; - } - err_helo("Reject::SNDR::Bad_Helo",protocol.s,remoteip,remotehost,helohost.s,mailfrom.s,addr.s,badhelocond); - return; + switch (flagbadhelo) { + case -2: badhelocond = "!"; break; + case -1: badhelocond = "."; break; + default: badhelocond = "*"; break; + } + err_helo( + "Reject::SNDR::Bad_Helo", + protocol.s, + remoteip, + remotehost, + helohost.s, + mailfrom.s, + addr.s, + badhelocond); + return; } if (flagdnshelo > 0) { - err_helo("Reject::SNDR::DNS_Helo",protocol.s,remoteip,remotehost,helohost.s,mailfrom.s,addr.s,badhelocond); + err_helo( + "Reject::SNDR::DNS_Helo", + protocol.s, + remoteip, + remotehost, + helohost.s, + mailfrom.s, + addr.s, + badhelocond); return; } - if (flagdnsmf > 0) { /* Mail from rejects */ - err_mfdns("Reject::ORIG::DNS_MF",protocol.s,remoteip,remotehost,helohost.s,mailfrom.s,addr.s); + if (flagdnsmf > 0) { /* Mail from rejects */ + err_mfdns( + "Reject::ORIG::DNS_MF", protocol.s, remoteip, remotehost, helohost.s, mailfrom.s, addr.s); return; } - - if (!addrallowed(addr.s)) { /* Relaying rejects */ - err_nogateway("Reject::SNDR::Invalid_Relay",protocol.s,remoteip,remotehost,helohost.s,mailfrom.s,addr.s); + + if (!addrallowed(addr.s)) { /* Relaying rejects */ + err_nogateway( + "Reject::SNDR::Invalid_Relay", + protocol.s, + remoteip, + remotehost, + helohost.s, + mailfrom.s, + addr.s); return; } - - if (greylist && (postgrey_scanner() == 1)) { /* Greylisting */ - postgrey("Deferred::SNDR::Grey_Listed",protocol.s,remoteip,remotehost,helohost.s,mailfrom.s,addr.s); + + if (greylist && (postgrey_scanner() == 1)) { /* Greylisting */ + postgrey( + "Deferred::SNDR::Grey_Listed", + protocol.s, + remoteip, + remotehost, + helohost.s, + mailfrom.s, + addr.s); return; } - if (tarpitcount && flagerrcpts >= tarpitcount) { /* Tarpitting et al. */ + if (tarpitcount && flagerrcpts >= tarpitcount) { /* Tarpitting et al. */ if (tarpitdelay == 999) flagnotorious++; - err_rcpts("Reject::RCPT::Toomany_Rcptto",protocol.s,remoteip,remotehost,helohost.s,mailfrom.s,addr.s); + err_rcpts( + "Reject::RCPT::Toomany_Rcptto", + protocol.s, + remoteip, + remotehost, + helohost.s, + mailfrom.s, + addr.s); return; } if (tarpitcount && rcptcount >= tarpitcount) if (tarpitdelay > 0 && tarpitdelay < 999) sleep(tarpitdelay); - flagrcpt = rcptallowed(); /* Rcpt to rejects */ + flagrcpt = rcptallowed(); /* Rcpt to rejects */ if (!flagrcpt) { - err_recipient("Reject::RCPT::Failed_Rcptto",protocol.s,remoteip,remotehost,helohost.s,mailfrom.s,addr.s); + err_recipient( + "Reject::RCPT::Failed_Rcptto", + protocol.s, + remoteip, + remotehost, + helohost.s, + mailfrom.s, + addr.s); flagerrcpts++; return; } - if (flagspf) /* SPF rejects */ + if (flagspf) /* SPF rejects */ if (spf_check(flagip6) > 0) { if (!stralloc_0(&spfbounce)) die_nomem(); // spfbounce is 0-terminated in any case - err_spf("Reject::SPF::Fail",protocol.s,remoteip,remotehost,helohost.s,mailfrom.s,addr.s,spfbounce.s); + err_spf( + "Reject::SPF::Fail", + protocol.s, + remoteip, + remotehost, + helohost.s, + mailfrom.s, + addr.s, + spfbounce.s); return; } } -/* this file is too long --------------------------------- Local checks */ + /* this file is too long --------------------------------- Local checks */ - else { + else + { if (flagmimetype == 4 || flagmimetype == 6) flagmimetype = 0; if (flagloadertype == 2) flagloadertype = 0; if (flagmav < 0) { - err_mav("Reject::ORIG::Invalid_Mailfrom",protocol.s,remoteip,remotehost,helohost.s,mailfrom.s,addr.s); + err_mav( + "Reject::ORIG::Invalid_Mailfrom", + protocol.s, + remoteip, + remotehost, + helohost.s, + mailfrom.s, + addr.s); return; } --addr.len; - if (!stralloc_cats(&addr,relayclient)) die_nomem(); + if (!stralloc_cats(&addr, relayclient)) die_nomem(); if (!stralloc_0(&addr)) die_nomem(); } -/* this file is too long --------------------------------- Common checks */ + /* this file is too long --------------------------------- Common checks */ - if (flagmimetype == 2 || flagmimetype == 3 || flagmimetype == 6) cdb_init(&cdbm,fdbmt); - if (flagloadertype == 1) cdb_init(&cdbl,fdblt); + if (flagmimetype == 2 || flagmimetype == 3 || flagmimetype == 6) cdb_init(&cdbm, fdbmt); + if (flagloadertype == 1) cdb_init(&cdbl, fdblt); if (flagaddr && flagaddr != -110) { switch (flagaddr) { @@ -1142,60 +1308,96 @@ void smtp_rcpt(char *arg) case -5: badmailcond = "+"; break; default: badmailcond = "*"; break; } - err_bmf("Reject::ORIG::Bad_Mailfrom",protocol.s,remoteip,remotehost,helohost.s,mailfrom.s,addr.s,badmailcond); + err_bmf( + "Reject::ORIG::Bad_Mailfrom", + protocol.s, + remoteip, + remotehost, + helohost.s, + mailfrom.s, + addr.s, + badmailcond); return; } - + flagrcpt = brtcheck(); if (flagrcpt == 110) { - err_brt("Reject::RCPT::Invalid_Rcptto",protocol.s,remoteip,remotehost,helohost.s,mailfrom.s,addr.s); + err_brt( + "Reject::RCPT::Invalid_Rcptto", + protocol.s, + remoteip, + remotehost, + helohost.s, + mailfrom.s, + addr.s); return; } else if (flagrcpt > 0) { - err_brt("Reject::RCPT::Bad_Rcptto",protocol.s,remoteip,remotehost,helohost.s,mailfrom.s,addr.s); + err_brt( + "Reject::RCPT::Bad_Rcptto", protocol.s, remoteip, remotehost, helohost.s, mailfrom.s, addr.s); return; } if (flagsize) { - err_size("Reject::DATA::Invalid_Size",protocol.s,remoteip,remotehost,helohost.s,mailfrom.s,addr.s); + err_size( + "Reject::DATA::Invalid_Size", protocol.s, remoteip, remotehost, helohost.s, mailfrom.s, addr.s); return; } if (maxrcptcount && rcptcount > maxrcptcount) { - err_rcpts("Reject::RCPT::Toomany_Rcptto",protocol.s,remoteip,remotehost,helohost.s,mailfrom.s,addr.s); + err_rcpts( + "Reject::RCPT::Toomany_Rcptto", + protocol.s, + remoteip, + remotehost, + helohost.s, + mailfrom.s, + addr.s); return; } -/* this file is too long --------------------------------- Checks done; mailfrom/recipient accepted */ + /* this file is too long --------------------------------- Checks done; mailfrom/recipient accepted */ - if (!stralloc_cats(&rcptto,"T")) die_nomem(); - if (!stralloc_cats(&rcptto,addr.s)) die_nomem(); + if (!stralloc_cats(&rcptto, "T")) die_nomem(); + if (!stralloc_cats(&rcptto, addr.s)) die_nomem(); if (!stralloc_0(&rcptto)) die_nomem(); - if (!stralloc_cats(&mailto,addr.s)) die_nomem(); - if (!stralloc_cats(&mailto," ")) die_nomem(); - if (!stralloc_copys(&deliverto,mailto.s)) die_nomem(); + if (!stralloc_cats(&mailto, addr.s)) die_nomem(); + if (!stralloc_cats(&mailto, " ")) die_nomem(); + if (!stralloc_copys(&deliverto, mailto.s)) die_nomem(); if (!stralloc_0(&deliverto)) die_nomem(); - if (!env_put("RCPTTO",deliverto.s)) die_nomem(); + if (!env_put("RCPTTO", deliverto.s)) die_nomem(); -/* this file is too long --------------------------------- Additional logging */ + /* this file is too long --------------------------------- Additional logging */ switch (flagrcpt) { - case 1: rcptok = "Recipients_Cdb"; break; - case 2: rcptok = "Recipients_Pam"; break; - case 3: rcptok = "Recipients_Users"; break; - case 4: rcptok = "Recipients_Wild"; break; - default: rcptok = "Rcpthosts_Rcptto"; break; + case 1: rcptok = "Recipients_Cdb"; break; + case 2: rcptok = "Recipients_Pam"; break; + case 3: rcptok = "Recipients_Users"; break; + case 4: rcptok = "Recipients_Wild"; break; + default: rcptok = "Rcpthosts_Rcptto"; break; } if (seenauth) - smtp_loga("Accept::AUTH::",protocol.s,remoteip,remotehost,helohost.s,mailfrom.s,addr.s,remoteinfo,authmethod.s); + smtp_loga( + "Accept::AUTH::", + protocol.s, + remoteip, + remotehost, + helohost.s, + mailfrom.s, + addr.s, + remoteinfo, + authmethod.s); else if (flagmav > 0) - smtp_logg("Accept::ORIG::Local_Sender",protocol.s,remoteip,remotehost,helohost.s,mailfrom.s,addr.s); + smtp_logg( + "Accept::ORIG::Local_Sender", protocol.s, remoteip, remotehost, helohost.s, mailfrom.s, addr.s); else if (relayclient) - smtp_logg("Accept::SNDR::Relay_Client",protocol.s,remoteip,remotehost,helohost.s,mailfrom.s,addr.s); + smtp_logg( + "Accept::SNDR::Relay_Client", protocol.s, remoteip, remotehost, helohost.s, mailfrom.s, addr.s); else if (flagspf == 10) - smtp_logr("Accept::SPF::",rcptok,protocol.s,remoteip,remotehost,helohost.s,mailfrom.s,addr.s); + smtp_logr("Accept::SPF::", rcptok, protocol.s, remoteip, remotehost, helohost.s, mailfrom.s, addr.s); else - smtp_logr("Accept::RCPT::",rcptok,protocol.s,remoteip,remotehost,helohost.s,mailfrom.s,addr.s); + smtp_logr( + "Accept::RCPT::", rcptok, protocol.s, remoteip, remotehost, helohost.s, mailfrom.s, addr.s); out("250 ok\r\n"); } @@ -1209,37 +1411,44 @@ stralloc badmimetype = {0}; stralloc badloadertype = {0}; unsigned int nolines = 0; -unsigned int flagb64 = 0; /* lineno with BASE64MESSAGE */ -unsigned int flagbase = 0; /* lineno with actual base64 content */ +unsigned int flagb64 = 0; /* lineno with BASE64MESSAGE */ +unsigned int flagbase = 0; /* lineno with actual base64 content */ unsigned int flagblank = 0; static void queue_put(char *ch) { int i; - if (flagmimetype > 0 || flagloadertype > 0 ) { + if (flagmimetype > 0 || flagloadertype > 0) { if (line.len <= BUFFER_SIZE) - if (!stralloc_catb(&line,ch,1)) die_nomem(); /* Reassamble chars to line; prepend with 'L' */ + if (!stralloc_catb(&line, ch, 1)) die_nomem(); /* Reassamble chars to line; prepend with 'L' */ if (*ch == '\n') { nolines++; - if (line.len == 2) { flagblank = nolines; flagbase = 0; } + if (line.len == 2) { + flagblank = nolines; + flagbase = 0; + } if (*(line.s + 1) == 'C' || *(line.s + 1) == 'c') - if (case_startb(line.s + 1,line.len - 2,BASE64MESSAGE)) flagb64 = nolines; + if (case_startb(line.s + 1, line.len - 2, BASE64MESSAGE)) flagb64 = nolines; if (flagb64 && nolines == flagblank + 1 && line.len > MIMETYPE_LEN + 2) flagbase = nolines; - if (*(line.s + 1) == '-') { flagb64 = 0; flagbase = 0; } + if (*(line.s + 1) == '-') { + flagb64 = 0; + flagbase = 0; + } - if (flagmimetype > 0 && flagbase == nolines) { /* badmimetype */ - if (!stralloc_catb(&base64types,line.s + 1,MIMETYPE_LEN)) die_nomem(); + if (flagmimetype > 0 && flagbase == nolines) { /* badmimetype */ + if (!stralloc_catb(&base64types, line.s + 1, MIMETYPE_LEN)) die_nomem(); if (!stralloc_0(&base64types)) die_nomem(); if (flagmimetype == 2 || flagmimetype == 3 || flagmimetype == 6) { - if (cdb_find(&cdbm,line.s + 1,MIMETYPE_LEN)) { - cdb_free(&cdbm); close(fdbmt); - if (!stralloc_copyb(&badmimetype,line.s + 1,MIMETYPE_LEN)) die_nomem(); + if (cdb_find(&cdbm, line.s + 1, MIMETYPE_LEN)) { + cdb_free(&cdbm); + close(fdbmt); + if (!stralloc_copyb(&badmimetype, line.s + 1, MIMETYPE_LEN)) die_nomem(); if (!stralloc_0(&badmimetype)) die_nomem(); - if (!stralloc_cats(&rcptto,"M")) die_nomem(); + if (!stralloc_cats(&rcptto, "M")) die_nomem(); if (!stralloc_0(&rcptto)) die_nomem(); qmail_fail(&qqt); flagmimetype = -1; @@ -1250,22 +1459,23 @@ static void queue_put(char *ch) if (flagbase && line.len > LOADER_LEN + 2) { if (flagloadertype >= 1 || flagmimetype >= 1) { for (i = 0; i < line.len - LOADER_LEN; ++i) { - if (flagloadertype == 1 && *(line.s+i) == *badloaderinit) { /* badloadertype */ - if (cdb_find(&cdbl,line.s + i,LOADER_LEN)) { - cdb_free(&cdbl); close(fdbmt); - if (!stralloc_copyb(&badloadertype,line.s + i,LOADER_LEN)) die_nomem(); + if (flagloadertype == 1 && *(line.s + i) == *badloaderinit) { /* badloadertype */ + if (cdb_find(&cdbl, line.s + i, LOADER_LEN)) { + cdb_free(&cdbl); + close(fdbmt); + if (!stralloc_copyb(&badloadertype, line.s + i, LOADER_LEN)) die_nomem(); if (!stralloc_0(&badloadertype)) die_nomem(); - if (!stralloc_cats(&rcptto,"L")) die_nomem(); + if (!stralloc_cats(&rcptto, "L")) die_nomem(); if (!stralloc_0(&rcptto)) die_nomem(); qmail_fail(&qqt); flagloadertype = -1; } } if (flagmimetype == 1 || flagmimetype == 3 || flagmimetype == 4) { - if (*(line.s + i) == ' ' || *(line.s + i) == '\t') { /* white spaces */ - if (!stralloc_copyb(&badmimetype,line.s + i - 2,MIMETYPE_LEN)) die_nomem(); + if (*(line.s + i) == ' ' || *(line.s + i) == '\t') { /* white spaces */ + if (!stralloc_copyb(&badmimetype, line.s + i - 2, MIMETYPE_LEN)) die_nomem(); if (!stralloc_0(&badmimetype)) die_nomem(); - if (!stralloc_cats(&rcptto,"M")) die_nomem(); + if (!stralloc_cats(&rcptto, "M")) die_nomem(); if (!stralloc_0(&rcptto)) die_nomem(); qmail_fail(&qqt); flagmimetype = -2; @@ -1275,14 +1485,13 @@ static void queue_put(char *ch) } } line.len = 0; - if (!stralloc_copys(&line,"L")) die_nomem(); + if (!stralloc_copys(&line, "L")) die_nomem(); } } if (bytestooverflow) - if (!--bytestooverflow) - qmail_fail(&qqt); - qmail_put(&qqt,ch,1); + if (!--bytestooverflow) qmail_fail(&qqt); + qmail_put(&qqt, ch, 1); } void blast(int *hops) @@ -1291,7 +1500,7 @@ void blast(int *hops) int state; int seencr; int flaginheader; - int pos; /* number of bytes since most recent \n, if fih */ + int pos; /* number of bytes since most recent \n, if fih */ int flagmaybex; /* 1 if this line might match RECEIVED, if fih */ int flagmaybey; /* 1 if this line might match \r\n, if fih */ int flagmaybez; /* 1 if this line might match DELIVERED, if fih */ @@ -1299,52 +1508,90 @@ void blast(int *hops) state = 1; *hops = 0; flaginheader = 1; - pos = 0; flagmaybex = flagmaybey = flagmaybez = 1; seencr = 0; + pos = 0; + flagmaybex = flagmaybey = flagmaybez = 1; + seencr = 0; for (;;) { - buffer_get(&bi,&ch,1); + buffer_get(&bi, &ch, 1); if (ch == '\n') { - if (seencr == 0) { buffer_seek(&bi,-1); ch = '\r'; } + if (seencr == 0) { + buffer_seek(&bi, -1); + ch = '\r'; + } } - if (ch == '\r') seencr = 1; else seencr = 0; + if (ch == '\r') + seencr = 1; + else + seencr = 0; if (flaginheader) { if (pos < 9) { - if (ch != "delivered"[pos]) if (ch != "DELIVERED"[pos]) flagmaybez = 0; - if (flagmaybez) if (pos == 8) ++*hops; + if (ch != "delivered"[pos]) + if (ch != "DELIVERED"[pos]) flagmaybez = 0; + if (flagmaybez) + if (pos == 8) ++*hops; if (pos < 8) - if (ch != "received"[pos]) if (ch != "RECEIVED"[pos]) flagmaybex = 0; - if (flagmaybex) if (pos == 7) ++*hops; - if (pos < 2) if (ch != "\r\n"[pos]) flagmaybey = 0; - if (flagmaybey) if (pos == 1) flaginheader = 0; + if (ch != "received"[pos]) + if (ch != "RECEIVED"[pos]) flagmaybex = 0; + if (flagmaybex) + if (pos == 7) ++*hops; + if (pos < 2) + if (ch != "\r\n"[pos]) flagmaybey = 0; + if (flagmaybey) + if (pos == 1) flaginheader = 0; ++pos; } - if (ch == '\n') { pos = 0; flagmaybex = flagmaybey = flagmaybez = 1; } + if (ch == '\n') { + pos = 0; + flagmaybex = flagmaybey = flagmaybez = 1; + } } switch (state) { case 0: if (ch == '\n') straynewline(); - if (ch == '\r') { state = 4; continue; } + if (ch == '\r') { + state = 4; + continue; + } break; case 1: /* \r\n */ if (ch == '\n') straynewline(); - if (ch == '.') { state = 2; continue; } - if (ch == '\r') { state = 4; continue; } + if (ch == '.') { + state = 2; + continue; + } + if (ch == '\r') { + state = 4; + continue; + } state = 0; break; case 2: /* \r\n + . */ if (ch == '\n') straynewline(); - if (ch == '\r') { state = 3; continue; } + if (ch == '\r') { + state = 3; + continue; + } state = 0; break; case 3: /* \r\n + .\r */ if (ch == '\n') return; queue_put("."); queue_put("\r"); - if (ch == '\r') { state = 4; continue; } + if (ch == '\r') { + state = 4; + continue; + } state = 0; break; case 4: /* + \r */ - if (ch == '\n') { state = 1; break; } - if (ch != '\r') { queue_put("\r"); state = 0; } + if (ch == '\n') { + state = 1; + break; + } + if (ch != '\r') { + queue_put("\r"); + state = 0; + } } queue_put(&ch); } @@ -1357,10 +1604,10 @@ void acceptmessage(unsigned long qp) datetime_sec when; when = now(); out("250 ok "); - accept_buf[fmt_ulong(accept_buf,(unsigned long) when)] = 0; + accept_buf[fmt_ulong(accept_buf, (unsigned long)when)] = 0; out(accept_buf); out(" qp "); - accept_buf[fmt_ulong(accept_buf,qp)] = 0; + accept_buf[fmt_ulong(accept_buf, qp)] = 0; out(accept_buf); out("\r\n"); } @@ -1371,69 +1618,152 @@ void smtp_data() unsigned long qp; char *qqx; - if (!seenmail) { err_wantmail(); return; } - if (!rcptto.len) { err_wantrcpt(); return; } - if (flagnotorious) { err_notorious(); } + if (!seenmail) { + err_wantmail(); + return; + } + if (!rcptto.len) { + err_wantrcpt(); + return; + } + if (flagnotorious) { + err_notorious(); + } seenmail = 0; if (databytes) bytestooverflow = databytes + 1; - if (!stralloc_copys(&addr,"")) die_nomem(); - if (!stralloc_cats(&addr,rcptto.s + 1)) die_nomem(); + if (!stralloc_copys(&addr, "")) die_nomem(); + if (!stralloc_cats(&addr, rcptto.s + 1)) die_nomem(); if (!stralloc_0(&addr)) die_nomem(); - if (qmail_open(&qqt) == -1) { err_qqt(); return; } + if (qmail_open(&qqt) == -1) { + err_qqt(); + return; + } qp = qmail_qp(&qqt); out("354 go ahead\r\n"); - if (flagspf && !relayclient) spfheader(&qqt,spfinfo.s,local,remoteip,helohost.s,mailfrom.s); - received(&qqt,protocol.s,local,remoteip,remotehost,remoteinfo,fakehelo,tlsinfo.s,rblinfo.s); + if (flagspf && !relayclient) spfheader(&qqt, spfinfo.s, local, remoteip, helohost.s, mailfrom.s); + received(&qqt, protocol.s, local, remoteip, remotehost, remoteinfo, fakehelo, tlsinfo.s, rblinfo.s); blast(&hops); hops = (hops >= MAXHOPS); if (hops) qmail_fail(&qqt); if (base64 && base64types.len == 0) { - if (!stralloc_cats(&rcptto,"Q")) die_nomem(); + if (!stralloc_cats(&rcptto, "Q")) die_nomem(); if (!stralloc_0(&rcptto)) die_nomem(); } - qmail_from(&qqt,mailfrom.s); - qmail_put(&qqt,rcptto.s,rcptto.len); + qmail_from(&qqt, mailfrom.s); + qmail_put(&qqt, rcptto.s, rcptto.len); qqx = qmail_close(&qqt); - if (!*qqx) { acceptmessage(qp); return; } - if (hops) { out("554 too many hops, this message is looping (#5.4.6)\r\n"); return; } + if (!*qqx) { + acceptmessage(qp); + return; + } + if (hops) { + out("554 too many hops, this message is looping (#5.4.6)\r\n"); + return; + } if (databytes) if (!bytestooverflow) { - err_size("Reject::DATA::Invalid_Size",protocol.s,remoteip,remotehost,helohost.s,mailfrom.s,addr.s); - return; + err_size( + "Reject::DATA::Invalid_Size", + protocol.s, + remoteip, + remotehost, + helohost.s, + mailfrom.s, + addr.s); + return; } if (flagmimetype < 0) { - err_data("Reject::DATA::Bad_MIME",protocol.s,remoteip,remotehost,helohost.s,mailfrom.s,addr.s,badmimetype.s); + err_data( + "Reject::DATA::Bad_MIME", + protocol.s, + remoteip, + remotehost, + helohost.s, + mailfrom.s, + addr.s, + badmimetype.s); return; } if (flagloadertype < 0) { - err_data("Reject::DATA::Bad_Loader",protocol.s,remoteip,remotehost,helohost.s,mailfrom.s,addr.s,badloadertype.s); + err_data( + "Reject::DATA::Bad_Loader", + protocol.s, + remoteip, + remotehost, + helohost.s, + mailfrom.s, + addr.s, + badloadertype.s); return; } if (*qqx == 'I') { - err_data("Reject::DKIM::Signature",protocol.s,remoteip,remotehost,helohost.s,mailfrom.s,addr.s,"fail"); + err_data( + "Reject::DKIM::Signature", + protocol.s, + remoteip, + remotehost, + helohost.s, + mailfrom.s, + addr.s, + "fail"); return; } if (*qqx == 'S') { - err_data("Reject::DATA::Spam_Message",protocol.s,remoteip,remotehost,helohost.s,mailfrom.s,addr.s,"spam"); + err_data( + "Reject::DATA::Spam_Message", + protocol.s, + remoteip, + remotehost, + helohost.s, + mailfrom.s, + addr.s, + "spam"); return; } if (*qqx == 'A') { - err_data("Reject::DATA::MIME_Attach",protocol.s,remoteip,remotehost,helohost.s,mailfrom.s,addr.s,"MIME"); + err_data( + "Reject::DATA::MIME_Attach", + protocol.s, + remoteip, + remotehost, + helohost.s, + mailfrom.s, + addr.s, + "MIME"); return; } if (*qqx == 'V') { if (qhpsi) - err_data("Reject::DATA::Virus_Infected",protocol.s,remoteip,remotehost,helohost.s,mailfrom.s,addr.s,qhpsi); + err_data( + "Reject::DATA::Virus_Infected", + protocol.s, + remoteip, + remotehost, + helohost.s, + mailfrom.s, + addr.s, + qhpsi); else - err_data("Reject::DATA::Virus_Infected",protocol.s,remoteip,remotehost,helohost.s,mailfrom.s,addr.s,"AV scanner"); + err_data( + "Reject::DATA::Virus_Infected", + protocol.s, + remoteip, + remotehost, + helohost.s, + mailfrom.s, + addr.s, + "AV scanner"); return; } - if (*qqx == 'D') out("554 "); else out("451 "); + if (*qqx == 'D') + out("554 "); + else + out("451 "); out(qqx + 1); out("\r\n"); } @@ -1441,33 +1771,34 @@ void smtp_data() /* this file is too long --------------------------------- SMTP Auth */ char unique[FMT_ULONG + FMT_ULONG + 3]; -static stralloc authin = {0}; /* input from SMTP client */ -static stralloc pass = {0}; /* plain passwd or digest */ -static stralloc resp = {0}; /* b64 response */ -static stralloc chal = {0}; /* CRAM-MD5 plain challenge */ -static stralloc slop = {0}; /* CRAM-MD5 b64 challenge */ +static stralloc authin = {0}; /* input from SMTP client */ +static stralloc pass = {0}; /* plain passwd or digest */ +static stralloc resp = {0}; /* b64 response */ +static stralloc chal = {0}; /* CRAM-MD5 plain challenge */ +static stralloc slop = {0}; /* CRAM-MD5 b64 challenge */ char **childargs; char authbuf[512]; -buffer ba = BUFFER_INIT(safewrite,FDAUTH,authbuf,sizeof(authbuf)); +buffer ba = BUFFER_INIT(safewrite, FDAUTH, authbuf, sizeof(authbuf)); int authgetl(void) { int i; - if (!stralloc_copys(&authin,"")) die_nomem(); + if (!stralloc_copys(&authin, "")) die_nomem(); for (;;) { - if (!stralloc_readyplus(&authin,1)) die_nomem(); /* XXX */ - i = buffer_get(&bi,authin.s + authin.len,1); + if (!stralloc_readyplus(&authin, 1)) die_nomem(); /* XXX */ + i = buffer_get(&bi, authin.s + authin.len, 1); if (i != 1) die_read(); if (authin.s[authin.len] == '\n') break; ++authin.len; } - if (authin.len > 0) if (authin.s[authin.len - 1] == '\r') --authin.len; + if (authin.len > 0) + if (authin.s[authin.len - 1] == '\r') --authin.len; authin.s[authin.len] = 0; if (*authin.s == '*' && *(authin.s + 1) == 0) return err_authabort(); - if (authin.len == 0) return err_authinput(); + if (authin.len == 0) return err_authinput(); return authin.len; } @@ -1480,35 +1811,37 @@ int authenticate(void) if (!stralloc_0(&user)) die_nomem(); if (!stralloc_0(&pass)) die_nomem(); if (!stralloc_0(&chal)) die_nomem(); - if (!env_put("AUTHUSER",user.s)) die_nomem(); + if (!env_put("AUTHUSER", user.s)) die_nomem(); if (pipe(pi) == -1) return err_pipe(); switch (child = fork()) { - case -1: - return err_fork(); + case -1: return err_fork(); case 0: close(pi[1]); - if (fd_copy(FDAUTH,pi[0]) == -1) return err_pipe(); + if (fd_copy(FDAUTH, pi[0]) == -1) return err_pipe(); sig_pipedefault(); - execvp(*childargs,childargs); + execvp(*childargs, childargs); _exit(1); } close(pi[0]); - buffer_init(&ba,write,pi[1],authbuf,sizeof(authbuf)); - if (buffer_put(&ba,user.s,user.len) == -1) return err_write(); - if (buffer_put(&ba,pass.s,pass.len) == -1) return err_write(); + buffer_init(&ba, write, pi[1], authbuf, sizeof(authbuf)); + if (buffer_put(&ba, user.s, user.len) == -1) return err_write(); + if (buffer_put(&ba, pass.s, pass.len) == -1) return err_write(); if (smtpauth == 2 || smtpauth == 3 || smtpauth == 12 || smtpauth == 13) - if (buffer_put(&ba,chal.s,chal.len) == -1) return err_write(); + if (buffer_put(&ba, chal.s, chal.len) == -1) return err_write(); if (buffer_flush(&ba) == -1) return err_write(); close(pi[1]); - if (!stralloc_copys(&chal,"")) die_nomem(); - if (!stralloc_copys(&slop,"")) die_nomem(); - byte_zero(authbuf,sizeof(authbuf)); - if (wait_pid(&wstat,child) == -1) return err_child(); + if (!stralloc_copys(&chal, "")) die_nomem(); + if (!stralloc_copys(&slop, "")) die_nomem(); + byte_zero(authbuf, sizeof(authbuf)); + if (wait_pid(&wstat, child) == -1) return err_child(); if (wait_crashed(wstat)) return err_child(); - if (wait_exitcode(wstat)) { sleep(AUTHSLEEP); return 1; } /* no */ + if (wait_exitcode(wstat)) { + sleep(AUTHSLEEP); + return 1; + } /* no */ return 0; /* yes */ } @@ -1518,19 +1851,20 @@ int auth_login(char *arg) if (smtpauth == 2 || smtpauth == 12) return 1; /* only login/plain */ if (*arg) { - if ((r = b64decode((unsigned char *)arg,str_len(arg),&user)) == 1) return err_authinput(); - } - else { - out("334 VXNlcm5hbWU6\r\n"); flush(); /* Username: */ + if ((r = b64decode((unsigned char *)arg, str_len(arg), &user)) == 1) return err_authinput(); + } else { + out("334 VXNlcm5hbWU6\r\n"); + flush(); /* Username: */ if (authgetl() < 0) return -1; - if ((r = b64decode((unsigned char *)authin.s,authin.len,&user)) == 1) return err_authinput(); + if ((r = b64decode((unsigned char *)authin.s, authin.len, &user)) == 1) return err_authinput(); } if (r == -1) die_nomem(); - out("334 UGFzc3dvcmQ6\r\n"); flush(); /* Password: */ + out("334 UGFzc3dvcmQ6\r\n"); + flush(); /* Password: */ if (authgetl() < 0) return -1; - if ((r = b64decode((unsigned char *)authin.s,authin.len,&pass)) == 1) return err_authinput(); + if ((r = b64decode((unsigned char *)authin.s, authin.len, &pass)) == 1) return err_authinput(); if (r == -1) die_nomem(); if (!user.len || !pass.len) return err_authinput(); @@ -1543,20 +1877,20 @@ int auth_plain(char *arg) if (smtpauth == 2 || smtpauth == 12) return 1; /* only login/plain */ if (*arg) { - if ((r = b64decode((unsigned char *)arg,str_len(arg),&resp)) == 1) return err_authinput(); - } - else { - out("334 \r\n"); flush(); + if ((r = b64decode((unsigned char *)arg, str_len(arg), &resp)) == 1) return err_authinput(); + } else { + out("334 \r\n"); + flush(); if (authgetl() < 0) return -1; - if ((r = b64decode((unsigned char *)authin.s,authin.len,&resp)) == 1) return err_authinput(); + if ((r = b64decode((unsigned char *)authin.s, authin.len, &resp)) == 1) return err_authinput(); } if (r == -1 || !stralloc_0(&resp)) die_nomem(); - while (resp.s[id]) id++; /* "authorize-id\0userid\0passwd\0" */ + while (resp.s[id]) id++; /* "authorize-id\0userid\0passwd\0" */ if (resp.len > id + 1) - if (!stralloc_copys(&user,resp.s + id + 1)) die_nomem(); + if (!stralloc_copys(&user, resp.s + id + 1)) die_nomem(); if (resp.len > id + user.len + 2) - if (!stralloc_copys(&pass,resp.s + id + user.len + 2)) die_nomem(); + if (!stralloc_copys(&pass, resp.s + id + user.len + 2)) die_nomem(); if (!user.len || !pass.len) return err_authinput(); return authenticate(); @@ -1568,34 +1902,34 @@ int auth_cram() char *s; if (smtpauth == 1 || smtpauth == 11) return 1; /* no challenge if login/plain */ - s = unique; /* generate challenge */ - s += fmt_uint(s,getpid()); + s = unique; /* generate challenge */ + s += fmt_uint(s, getpid()); *s++ = '.'; - s += fmt_ulong(s,(unsigned long) now()); + s += fmt_ulong(s, (unsigned long)now()); *s++ = '@'; *s++ = 0; - if (!stralloc_copys(&chal,"<")) die_nomem(); - if (!stralloc_cats(&chal,unique)) die_nomem(); - if (!stralloc_cats(&chal,local)) die_nomem(); - if (!stralloc_cats(&chal,">")) die_nomem(); - if (b64encode(&chal,&slop) < 0) die_nomem(); + if (!stralloc_copys(&chal, "<")) die_nomem(); + if (!stralloc_cats(&chal, unique)) die_nomem(); + if (!stralloc_cats(&chal, local)) die_nomem(); + if (!stralloc_cats(&chal, ">")) die_nomem(); + if (b64encode(&chal, &slop) < 0) die_nomem(); if (!stralloc_0(&slop)) die_nomem(); - out("334 "); /* "334 base64_challenge \r\n" */ + out("334 "); /* "334 base64_challenge \r\n" */ out(slop.s); out("\r\n"); flush(); - if (authgetl() < 0) return -1; /* got response */ - if ((r = b64decode((unsigned char *)authin.s,authin.len,&resp)) == 1) return err_authinput(); + if (authgetl() < 0) return -1; /* got response */ + if ((r = b64decode((unsigned char *)authin.s, authin.len, &resp)) == 1) return err_authinput(); if (r == -1 || !stralloc_0(&resp)) die_nomem(); - i = str_rchr(resp.s,' '); + i = str_rchr(resp.s, ' '); s = resp.s + i; while (*s == ' ') ++s; resp.s[i] = 0; - if (!stralloc_copys(&user,resp.s)) die_nomem();/* userid */ - if (!stralloc_copys(&pass,s)) die_nomem(); /* digest */ + if (!stralloc_copys(&user, resp.s)) die_nomem(); /* userid */ + if (!stralloc_copys(&pass, s)) die_nomem(); /* digest */ if (!user.len || !pass.len) return err_authinput(); return authenticate(); @@ -1605,10 +1939,10 @@ struct authcmd { char *text; int (*fun)(); } authcmds[] = { - { "login", auth_login } -, { "plain", auth_plain } -, { "cram-md5", auth_cram } -, { 0, err_noauth } + { "login", auth_login}, + { "plain", auth_plain}, + {"cram-md5", auth_cram}, + { 0, err_noauth} }; void smtp_auth(char *arg) @@ -1618,49 +1952,60 @@ void smtp_auth(char *arg) /* prevent users to expose userid + password over unencrypted connection */ - if ((starttls > 1) && !seentls) { - if (!stralloc_append(&protocol,"A")) die_nomem(); + if ((starttls > 1) && !seentls) { + if (!stralloc_append(&protocol, "A")) die_nomem(); if (!stralloc_0(&protocol)) die_nomem(); - err_authsetup("Reject::TLS::required",protocol.s,remoteip,remotehost,helohost.s); + err_authsetup("Reject::TLS::required", protocol.s, remoteip, remotehost, helohost.s); return; } - if ((starttls > 1) && !seenhelo) { - if (!stralloc_append(&protocol,"A")) die_nomem(); + if ((starttls > 1) && !seenhelo) { + if (!stralloc_append(&protocol, "A")) die_nomem(); if (!stralloc_0(&protocol)) die_nomem(); - err_tlsreq("Reject::AUTH::invalid",protocol.s,remoteip,remotehost,helohost.s); + err_tlsreq("Reject::AUTH::invalid", protocol.s, remoteip, remotehost, helohost.s); return; } - if (!smtpauth) { out("503 auth not available (#5.3.3)\r\n"); flush(); _exit(0); } + if (!smtpauth) { + out("503 auth not available (#5.3.3)\r\n"); + flush(); + _exit(0); + } if (smtpauth && !*childargs) { - err_authsetup("Reject::AUTH::invalid",protocol.s,remoteip,remotehost,helohost.s); - flush(); _exit(1); + err_authsetup("Reject::AUTH::invalid", protocol.s, remoteip, remotehost, helohost.s); + flush(); + _exit(1); + } + if (seenauth) { + err_authd(); + return; + } + if (seenmail) { + err_authmail(); + return; } - if (seenauth) { err_authd(); return; } - if (seenmail) { err_authmail(); return; } - if (!stralloc_copys(&user,"")) die_nomem(); - if (!stralloc_copys(&pass,"")) die_nomem(); - if (!stralloc_copys(&resp,"")) die_nomem(); - if (!stralloc_copys(&chal,"")) die_nomem(); /* only needed for CRAM-MD5 */ + if (!stralloc_copys(&user, "")) die_nomem(); + if (!stralloc_copys(&pass, "")) die_nomem(); + if (!stralloc_copys(&resp, "")) die_nomem(); + if (!stralloc_copys(&chal, "")) die_nomem(); /* only needed for CRAM-MD5 */ - i = str_chr(cmd,' '); /* get AUTH type */ + i = str_chr(cmd, ' '); /* get AUTH type */ arg = cmd + i; while (*arg == ' ') ++arg; cmd[i] = 0; for (i = 0; authcmds[i].text; ++i) - if (case_equals(authcmds[i].text,cmd)) break; + if (case_equals(authcmds[i].text, cmd)) break; - if (!authcmds[i].text) { /* invalid auth cmd */ - if (!stralloc_append(&protocol,"A")) die_nomem(); + if (!authcmds[i].text) { /* invalid auth cmd */ + if (!stralloc_append(&protocol, "A")) die_nomem(); if (!stralloc_0(&protocol)) die_nomem(); - err_authinvalid("Reject::AUTH::Method",protocol.s,remoteip,remotehost,helohost.s); + err_authinvalid("Reject::AUTH::Method", protocol.s, remoteip, remotehost, helohost.s); return; } - if (!stralloc_copys(&authmethod,authcmds[i].text)) die_nomem(); + if (!stralloc_copys(&authmethod, authcmds[i].text)) die_nomem(); if (!stralloc_0(&authmethod)) die_nomem(); switch (authcmds[i].fun(arg)) { @@ -1672,9 +2017,9 @@ void smtp_auth(char *arg) out("235 ok, go ahead (#2.0.0)\r\n"); break; case 1: - if (!stralloc_append(&protocol,"A")) die_nomem(); + if (!stralloc_append(&protocol, "A")) die_nomem(); if (!stralloc_0(&protocol)) die_nomem(); - err_authfail("Reject::AUTH::",protocol.s,remoteip,remotehost,helohost.s,user.s,authmethod.s); + err_authfail("Reject::AUTH::", protocol.s, remoteip, remotehost, helohost.s, user.s, authmethod.s); return; } } @@ -1682,20 +2027,20 @@ void smtp_auth(char *arg) /* this file is too long --------------------------------- GO ON */ struct commands smtpcommands[] = { - { "rcpt", smtp_rcpt, 0 } -, { "mail", smtp_mail, 0 } -, { "data", smtp_data, flush } -, { "auth", smtp_auth, flush } -, { "quit", smtp_quit, flush } -, { "helo", smtp_helo, flush } -, { "ehlo", smtp_ehlo, flush } -, { "rset", smtp_rset, flush } -, { "help", smtp_help, flush } -, { "noop", err_noop, flush } -, { "vrfy", err_vrfy, flush } -, { "starttls", smtp_starttls, flush } -, { 0, err_unimpl, flush } -} ; + { "rcpt", smtp_rcpt, 0}, + { "mail", smtp_mail, 0}, + { "data", smtp_data, flush}, + { "auth", smtp_auth, flush}, + { "quit", smtp_quit, flush}, + { "helo", smtp_helo, flush}, + { "ehlo", smtp_ehlo, flush}, + { "rset", smtp_rset, flush}, + { "help", smtp_help, flush}, + { "noop", err_noop, flush}, + { "vrfy", err_vrfy, flush}, + {"starttls", smtp_starttls, flush}, + { 0, err_unimpl, flush} +}; int main(int argc, char **argv) { @@ -1708,7 +2053,7 @@ int main(int argc, char **argv) smtp_greet("220 "); out(" ESMTP\r\n"); flush(); - if (commands(&bi,&smtpcommands) == 0) die_read(); + if (commands(&bi, &smtpcommands) == 0) die_read(); die_nomem(); return 0; diff --git a/src/qmail-start.c b/src/qmail-start.c index 7a7342c..4ad250e 100644 --- a/src/qmail-start.c +++ b/src/qmail-start.c @@ -1,17 +1,22 @@ -#include <unistd.h> #include <sys/stat.h> +#include <unistd.h> + +#include "exit.h" #include "fd.h" #include "prot.h" -#include "exit.h" + #include "auto_uids.h" -char *(qsargs[]) = { "qmail-send", 0 }; -char *(qcargs[]) = { "qmail-clean", 0 }; -char *(qlargs[]) = { "qmail-lspawn", "./Mailbox", 0 }; -char *(qrargs[]) = { "qmail-rspawn", 0 }; -char *(qtargs[]) = { "qmail-todo", 0}; +char *(qsargs[]) = {"qmail-send", 0}; +char *(qcargs[]) = {"qmail-clean", 0}; +char *(qlargs[]) = {"qmail-lspawn", "./Mailbox", 0}; +char *(qrargs[]) = {"qmail-rspawn", 0}; +char *(qtargs[]) = {"qmail-todo", 0}; -void die() { _exit(111); } +void die() +{ + _exit(111); +} int pi0[2]; int pi1[2]; @@ -25,34 +30,54 @@ int pi8[2]; int pi9[2]; int pi10[2]; -void closefds() -{ - close(2); close(3); close(4); close(5); close(6); - close(7); close(8); +void closefds() +{ + close(2); + close(3); + close(4); + close(5); + close(6); + close(7); + close(8); } -void closepipes() +void closepipes() { - close(pi1[0]); close(pi1[1]); close(pi2[0]); close(pi2[1]); - close(pi3[0]); close(pi3[1]); close(pi4[0]); close(pi4[1]); - close(pi5[0]); close(pi5[1]); close(pi6[0]); close(pi6[1]); - close(pi7[0]); close(pi7[1]); close(pi8[0]); close(pi8[1]); - close(pi9[0]); close(pi9[1]); close(pi10[0]); close(pi10[1]); + close(pi1[0]); + close(pi1[1]); + close(pi2[0]); + close(pi2[1]); + close(pi3[0]); + close(pi3[1]); + close(pi4[0]); + close(pi4[1]); + close(pi5[0]); + close(pi5[1]); + close(pi6[0]); + close(pi6[1]); + close(pi7[0]); + close(pi7[1]); + close(pi8[0]); + close(pi8[1]); + close(pi9[0]); + close(pi9[1]); + close(pi10[0]); + close(pi10[1]); } -int main(int argc,char **argv) +int main(int argc, char **argv) { if (chdir("/") == -1) die(); umask(077); if (prot_gid(auto_gidq) == -1) die(); - if (fd_copy(2,0) == -1) die(); - if (fd_copy(3,0) == -1) die(); - if (fd_copy(4,0) == -1) die(); - if (fd_copy(5,0) == -1) die(); - if (fd_copy(6,0) == -1) die(); - if (fd_copy(7,0) == -1) die(); - if (fd_copy(8,0) == -1) die(); + if (fd_copy(2, 0) == -1) die(); + if (fd_copy(3, 0) == -1) die(); + if (fd_copy(4, 0) == -1) die(); + if (fd_copy(5, 0) == -1) die(); + if (fd_copy(6, 0) == -1) die(); + if (fd_copy(7, 0) == -1) die(); + if (fd_copy(8, 0) == -1) die(); if (argv[1]) { qlargs[1] = argv[1]; @@ -62,21 +87,20 @@ int main(int argc,char **argv) if (argv[1]) { if (pipe(pi0) == -1) die(); switch (fork()) { - case -1: - die(); + case -1: die(); case 0: if (prot_gid(auto_gidn) == -1) die(); if (prot_uid(auto_uidl) == -1) die(); close(pi0[1]); - if (fd_move(0,pi0[0]) == -1) die(); + if (fd_move(0, pi0[0]) == -1) die(); closefds(); - execvp(argv[1],argv + 1); + execvp(argv[1], argv + 1); die(); } close(pi0[0]); - if (fd_move(1,pi0[1]) == -1) die(); + if (fd_move(1, pi0[1]) == -1) die(); } - + if (pipe(pi1) == -1) die(); if (pipe(pi2) == -1) die(); if (pipe(pi3) == -1) die(); @@ -87,39 +111,39 @@ int main(int argc,char **argv) if (pipe(pi8) == -1) die(); if (pipe(pi9) == -1) die(); if (pipe(pi10) == -1) die(); - + switch (fork()) { case -1: die(); case 0: - if (fd_copy(0,pi1[0]) == -1) die(); - if (fd_copy(1,pi2[1]) == -1) die(); + if (fd_copy(0, pi1[0]) == -1) die(); + if (fd_copy(1, pi2[1]) == -1) die(); closefds(); closepipes(); - execvp(*qlargs,qlargs); + execvp(*qlargs, qlargs); die(); } - + switch (fork()) { case -1: die(); case 0: if (prot_uid(auto_uidr) == -1) die(); - if (fd_copy(0,pi3[0]) == -1) die(); - if (fd_copy(1,pi4[1]) == -1) die(); + if (fd_copy(0, pi3[0]) == -1) die(); + if (fd_copy(1, pi4[1]) == -1) die(); closefds(); closepipes(); - execvp(*qrargs,qrargs); + execvp(*qrargs, qrargs); die(); } - + switch (fork()) { case -1: die(); case 0: if (prot_uid(auto_uidq) == -1) die(); - if (fd_copy(0,pi5[0]) == -1) die(); - if (fd_copy(1,pi6[1]) == -1) die(); + if (fd_copy(0, pi5[0]) == -1) die(); + if (fd_copy(1, pi6[1]) == -1) die(); closefds(); closepipes(); - execvp(*qcargs,qcargs); + execvp(*qcargs, qcargs); die(); } @@ -127,13 +151,13 @@ int main(int argc,char **argv) case -1: die(); case 0: if (prot_uid(auto_uids) == -1) die(); - if (fd_copy(0,pi7[0]) == -1) die(); - if (fd_copy(1,pi8[1]) == -1) die(); + if (fd_copy(0, pi7[0]) == -1) die(); + if (fd_copy(1, pi8[1]) == -1) die(); closefds(); - if (fd_copy(2,pi9[1]) == -1) die(); - if (fd_copy(3,pi10[0]) == -1) die(); + if (fd_copy(2, pi9[1]) == -1) die(); + if (fd_copy(3, pi10[0]) == -1) die(); closepipes(); - execvp(*qtargs,qtargs); + execvp(*qtargs, qtargs); die(); } @@ -141,25 +165,25 @@ int main(int argc,char **argv) case -1: die(); case 0: if (prot_uid(auto_uidq) == -1) die(); - if (fd_copy(0,pi9[0]) == -1) die(); - if (fd_copy(1,pi10[1]) == -1) die(); + if (fd_copy(0, pi9[0]) == -1) die(); + if (fd_copy(1, pi10[1]) == -1) die(); closefds(); closepipes(); - execvp(*qcargs,qcargs); + execvp(*qcargs, qcargs); die(); } - + if (prot_uid(auto_uids) == -1) die(); - if (fd_copy(0,1) == -1) die(); - if (fd_copy(1,pi1[1]) == -1) die(); - if (fd_copy(2,pi2[0]) == -1) die(); - if (fd_copy(3,pi3[1]) == -1) die(); - if (fd_copy(4,pi4[0]) == -1) die(); - if (fd_copy(5,pi5[1]) == -1) die(); - if (fd_copy(6,pi6[0]) == -1) die(); - if (fd_copy(7,pi7[1]) == -1) die(); - if (fd_copy(8,pi8[0]) == -1) die(); + if (fd_copy(0, 1) == -1) die(); + if (fd_copy(1, pi1[1]) == -1) die(); + if (fd_copy(2, pi2[0]) == -1) die(); + if (fd_copy(3, pi3[1]) == -1) die(); + if (fd_copy(4, pi4[0]) == -1) die(); + if (fd_copy(5, pi5[1]) == -1) die(); + if (fd_copy(6, pi6[0]) == -1) die(); + if (fd_copy(7, pi7[1]) == -1) die(); + if (fd_copy(8, pi8[0]) == -1) die(); closepipes(); - execvp(*qsargs,qsargs); + execvp(*qsargs, qsargs); die(); } diff --git a/src/qmail-tcpok.c b/src/qmail-tcpok.c index 2935f17..db37274 100644 --- a/src/qmail-tcpok.c +++ b/src/qmail-tcpok.c @@ -1,10 +1,12 @@ -#include "logmsg.h" +#include <unistd.h> + #include "buffer.h" +#include "exit.h" #include "lock.h" +#include "logmsg.h" #include "open.h" -#include <unistd.h> + #include "auto_qmail.h" -#include "exit.h" #define WHO "qmail-tcpok" @@ -16,21 +18,18 @@ int main() int fd; int i; - if (chdir(auto_qmail) == -1) - logmsg(WHO,111,FATAL,B("unable to chdir to: ",auto_qmail)); + if (chdir(auto_qmail) == -1) logmsg(WHO, 111, FATAL, B("unable to chdir to: ", auto_qmail)); if (chdir("queue/lock") == -1) - logmsg(WHO,111,FATAL,B("unable to chdir to ",auto_qmail,"/queue/lock: ")); + logmsg(WHO, 111, FATAL, B("unable to chdir to ", auto_qmail, "/queue/lock: ")); fd = open_write("tcpto"); - if (fd == -1) - logmsg(WHO,111,FATAL,B("unable to write ",auto_qmail,"/queue/lock/tcpto: ")); + if (fd == -1) logmsg(WHO, 111, FATAL, B("unable to write ", auto_qmail, "/queue/lock/tcpto: ")); if (lock_ex(fd) == -1) - logmsg(WHO,111,FATAL,B("unable to lock ",auto_qmail,"/queue/lock/tcpto: ")); + logmsg(WHO, 111, FATAL, B("unable to lock ", auto_qmail, "/queue/lock/tcpto: ")); - buffer_init(&bo,write,fd,buf,sizeof(buf)); - for (i = 0; i < sizeof(buf); ++i) - buffer_put(&bo,"",1); + buffer_init(&bo, write, fd, buf, sizeof(buf)); + for (i = 0; i < sizeof(buf); ++i) buffer_put(&bo, "", 1); if (buffer_flush(&bo) == -1) - logmsg(WHO,111,FATAL,B("unable to clear ",auto_qmail,"/queue/lock/tcpto: ")); + logmsg(WHO, 111, FATAL, B("unable to clear ", auto_qmail, "/queue/lock/tcpto: ")); _exit(0); } diff --git a/src/qmail-tcpto.c b/src/qmail-tcpto.c index e148c55..b5100ef 100644 --- a/src/qmail-tcpto.c +++ b/src/qmail-tcpto.c @@ -2,37 +2,55 @@ #include <sys/socket.h> #include <unistd.h> + #include "buffer.h" -#include "auto_qmail.h" +#include "error.h" +#include "exit.h" #include "fmt.h" #include "ip.h" #include "lock.h" -#include "error.h" -#include "exit.h" +#include "logmsg.h" +#include "open.h" +#include "stralloc.h" + +#include "auto_qmail.h" #include "datetime.h" #include "now.h" -#include "stralloc.h" -#include "open.h" -#include "logmsg.h" #define WHO "qmail-tcpto" -void die(n) int n; { buffer_flush(buffer_1); _exit(n); } +void die(n) int n; +{ + buffer_flush(buffer_1); + _exit(n); +} void warn(s) char *s; { char *x; x = error_str(errno); - buffer_puts(buffer_1,s); - buffer_puts(buffer_1,": "); - buffer_puts(buffer_1,x); - buffer_puts(buffer_1,"\n"); + buffer_puts(buffer_1, s); + buffer_puts(buffer_1, ": "); + buffer_puts(buffer_1, x); + buffer_puts(buffer_1, "\n"); } -void die_chdir() { logmsg(WHO,110,FATAL,"unable to chdir"); } -void die_open() { logmsg(WHO,112,FATAL,"unable to open tcpto"); } -void die_lock() { logmsg(WHO,112,FATAL,"unable to lock tcpto"); } -void die_read() { logmsg(WHO,112,FATAL,"unable to read tcpto"); } +void die_chdir() +{ + logmsg(WHO, 110, FATAL, "unable to chdir"); +} +void die_open() +{ + logmsg(WHO, 112, FATAL, "unable to open tcpto"); +} +void die_lock() +{ + logmsg(WHO, 112, FATAL, "unable to lock tcpto"); +} +void die_read() +{ + logmsg(WHO, 112, FATAL, "unable to read tcpto"); +} char tcpto_buf[1024]; @@ -58,38 +76,38 @@ int main(void) fd = open_read("tcpto"); if (fd == -1) die_open(); if (lock_ex(fdlock) == -1) die_lock(); - r = read(fd,tcpto_buf,sizeof(tcpto_buf)); + r = read(fd, tcpto_buf, sizeof(tcpto_buf)); close(fd); close(fdlock); if (r == -1) die_read(); - r >>= 5; /* 32 bit read */ + r >>= 5; /* 32 bit read */ start = now(); record = tcpto_buf; for (i = 0; i < r; ++i) { if (record[4] >= 1) { - when = (unsigned long) (unsigned char) record[11]; - when = (when << 8) + (unsigned long) (unsigned char) record[10]; - when = (when << 8) + (unsigned long) (unsigned char) record[9]; - when = (when << 8) + (unsigned long) (unsigned char) record[8]; + when = (unsigned long)(unsigned char)record[11]; + when = (when << 8) + (unsigned long)(unsigned char)record[10]; + when = (when << 8) + (unsigned long)(unsigned char)record[9]; + when = (when << 8) + (unsigned long)(unsigned char)record[8]; if (record[0] == AF_INET) { - byte_copy(&ip4,4,record + 16); - buffer_put(buffer_1,tmp,ip4_fmt(tmp,ip4)); + byte_copy(&ip4, 4, record + 16); + buffer_put(buffer_1, tmp, ip4_fmt(tmp, ip4)); } else { - byte_copy(&ip6,16,record + 16); - buffer_put(buffer_1,tmp,ip6_fmt(tmp,ip6)); + byte_copy(&ip6, 16, record + 16); + buffer_put(buffer_1, tmp, ip6_fmt(tmp, ip6)); } - buffer_puts(buffer_1," timed out "); - buffer_put(buffer_1,tmp,fmt_ulong(tmp,(unsigned long) (start - when))); - buffer_puts(buffer_1," seconds ago; # recent timeouts: "); - buffer_put(buffer_1,tmp,fmt_ulong(tmp,(unsigned long) (unsigned char) record[4])); - buffer_puts(buffer_1,"\n"); - } - record += 32; + buffer_puts(buffer_1, " timed out "); + buffer_put(buffer_1, tmp, fmt_ulong(tmp, (unsigned long)(start - when))); + buffer_puts(buffer_1, " seconds ago; # recent timeouts: "); + buffer_put(buffer_1, tmp, fmt_ulong(tmp, (unsigned long)(unsigned char)record[4])); + buffer_puts(buffer_1, "\n"); + } + record += 32; } - die(0); + die(0); } diff --git a/src/qmail-todo.c b/src/qmail-todo.c index 6e19272..34be551 100644 --- a/src/qmail-todo.c +++ b/src/qmail-todo.c @@ -1,30 +1,32 @@ -#include <sys/types.h> #include <sys/stat.h> +#include <sys/types.h> #include <unistd.h> + #include "alloc.h" -#include "auto_qmail.h" +#include "buffer.h" #include "byte.h" #include "constmap.h" -#include "control.h" #include "direntry.h" #include "error.h" #include "exit.h" #include "fmt.h" -#include "fmtqfn.h" #include "getln.h" -#include "open.h" #include "ndelay.h" -#include "now.h" -#include "readsubdir.h" -#include "buffer.h" +#include "open.h" #include "scan.h" #include "select.h" -#include "str.h" #include "sig.h" +#include "str.h" #include "stralloc.h" -#include "trigger.h" + +#include "auto_qmail.h" +#include "control.h" +#include "fmtqfn.h" +#include "now.h" #include "qsutil.h" +#include "readsubdir.h" #include "sendtodo.h" +#include "trigger.h" stralloc percenthack = {0}; struct constmap mappercenthack; @@ -38,26 +40,33 @@ char strnum[FMT_ULONG]; /* XXX not good, if qmail-send.c changes this has to be updated */ #define CHANNELS 2 -char *chanaddr[CHANNELS] = { "local/", "remote/" }; +char *chanaddr[CHANNELS] = {"local/", "remote/"}; datetime_sec recent; int flagquitasap = 0; void sendlog1(char *x); -void sendlog3(char *x,char *y,char *z); +void sendlog3(char *x, char *y, char *z); void sigterm(void) { - if (flagquitasap == 0) - sendlog1("status: qmail-todo stop processing asap\n"); + if (flagquitasap == 0) sendlog1("status: qmail-todo stop processing asap\n"); flagquitasap = 1; } -int flagreadasap = 0; void sighup(void) { flagreadasap = 1; } -int flagsendalive = 1; void senddied(void) { flagsendalive = 0; } +int flagreadasap = 0; +void sighup(void) +{ + flagreadasap = 1; +} +int flagsendalive = 1; +void senddied(void) +{ + flagsendalive = 0; +} void cleandied() -{ +{ sendlog1("alert: qmail-todo lost connection to qmail-clean ... exiting\n"); flagquitasap = 1; } @@ -69,13 +78,25 @@ stralloc fn = {0}; void fnmake_init(void) { - while (!stralloc_ready(&fn,FMTQFN)) nomem(); + while (!stralloc_ready(&fn, FMTQFN)) nomem(); } -void fnmake_info(unsigned long id) { fn.len = fmtqfn(fn.s,"info/",id,1); } -void fnmake_todo(unsigned long id) { fn.len = fmtqfn(fn.s,"todo/",id,1); } -void fnmake_mess(unsigned long id) { fn.len = fmtqfn(fn.s,"mess/",id,1); } -void fnmake_chanaddr(unsigned long id,int c) { fn.len = fmtqfn(fn.s,chanaddr[c],id,1); } +void fnmake_info(unsigned long id) +{ + fn.len = fmtqfn(fn.s, "info/", id, 1); +} +void fnmake_todo(unsigned long id) +{ + fn.len = fmtqfn(fn.s, "todo/", id, 1); +} +void fnmake_mess(unsigned long id) +{ + fn.len = fmtqfn(fn.s, "mess/", id, 1); +} +void fnmake_chanaddr(unsigned long id, int c) +{ + fn.len = fmtqfn(fn.s, chanaddr[c], id, 1); +} /* this file is not so long ------------------------------------- REWRITING */ @@ -93,100 +114,101 @@ int rewrite(char *recip) static stralloc addr = {0}; int at; - if (!stralloc_copys(&rwline,"T")) return 0; - if (!stralloc_copys(&addr,recip)) return 0; + if (!stralloc_copys(&rwline, "T")) return 0; + if (!stralloc_copys(&addr, recip)) return 0; - i = byte_rchr(addr.s,addr.len,'@'); + i = byte_rchr(addr.s, addr.len, '@'); if (i == addr.len) { - if (!stralloc_cats(&addr,"@")) return 0; - if (!stralloc_cat(&addr,&envnoathost)) return 0; + if (!stralloc_cats(&addr, "@")) return 0; + if (!stralloc_cat(&addr, &envnoathost)) return 0; } - while (constmap(&mappercenthack,addr.s + i + 1,addr.len - i - 1)) { - j = byte_rchr(addr.s,i,'%'); + while (constmap(&mappercenthack, addr.s + i + 1, addr.len - i - 1)) { + j = byte_rchr(addr.s, i, '%'); if (j == i) break; addr.len = i; i = j; addr.s[i] = '@'; } - at = byte_rchr(addr.s,addr.len,'@'); + at = byte_rchr(addr.s, addr.len, '@'); - if (constmap(&maplocals,addr.s + at + 1,addr.len - at - 1)) { - if (!stralloc_cat(&rwline,&addr)) return 0; + if (constmap(&maplocals, addr.s + at + 1, addr.len - at - 1)) { + if (!stralloc_cat(&rwline, &addr)) return 0; if (!stralloc_0(&rwline)) return 0; return 1; } for (i = 0; i <= addr.len; ++i) if (!i || (i == at + 1) || (i == addr.len) || ((i > at) && (addr.s[i] == '.'))) - if ((x = constmap(&mapvdoms,addr.s + i,addr.len - i))) { + if ((x = constmap(&mapvdoms, addr.s + i, addr.len - i))) { if (!*x) break; - if (!stralloc_cats(&rwline,x)) return 0; - if (!stralloc_cats(&rwline,"-")) return 0; - if (!stralloc_cat(&rwline,&addr)) return 0; + if (!stralloc_cats(&rwline, x)) return 0; + if (!stralloc_cats(&rwline, "-")) return 0; + if (!stralloc_cat(&rwline, &addr)) return 0; if (!stralloc_0(&rwline)) return 0; return 1; } - - if (!stralloc_cat(&rwline,&addr)) return 0; + + if (!stralloc_cat(&rwline, &addr)) return 0; if (!stralloc_0(&rwline)) return 0; return 2; } /* this file is not so long --------------------------------- COMMUNICATION */ -buffer toqc; char toqcbuf[1024]; -buffer fromqc; char fromqcbuf[1024]; +buffer toqc; +char toqcbuf[1024]; +buffer fromqc; +char fromqcbuf[1024]; stralloc comm_buf = {0}; int comm_pos; int fdout = -1; int fdin = -1; -void sendlog1(char* x) +void sendlog1(char *x) { int pos; pos = comm_buf.len; - if (!stralloc_cats(&comm_buf,"L")) goto FAIL; - if (!stralloc_cats(&comm_buf,x)) goto FAIL; + if (!stralloc_cats(&comm_buf, "L")) goto FAIL; + if (!stralloc_cats(&comm_buf, x)) goto FAIL; if (!stralloc_0(&comm_buf)) goto FAIL; return; - FAIL: +FAIL: /* either all or nothing */ comm_buf.len = pos; } -void sendlog3(char* x, char *y, char *z) +void sendlog3(char *x, char *y, char *z) { int pos; pos = comm_buf.len; - if (!stralloc_cats(&comm_buf,"L")) goto FAIL; - if (!stralloc_cats(&comm_buf,x)) goto FAIL; - if (!stralloc_cats(&comm_buf,y)) goto FAIL; - if (!stralloc_cats(&comm_buf,z)) goto FAIL; + if (!stralloc_cats(&comm_buf, "L")) goto FAIL; + if (!stralloc_cats(&comm_buf, x)) goto FAIL; + if (!stralloc_cats(&comm_buf, y)) goto FAIL; + if (!stralloc_cats(&comm_buf, z)) goto FAIL; if (!stralloc_0(&comm_buf)) goto FAIL; return; - FAIL: +FAIL: /* either all or nothing */ comm_buf.len = pos; } void comm_init(void) { - buffer_init(&toqc,write,2,toqcbuf,sizeof(toqcbuf)); - buffer_init(&fromqc,read,3,fromqcbuf,sizeof(fromqcbuf)); + buffer_init(&toqc, write, 2, toqcbuf, sizeof(toqcbuf)); + buffer_init(&fromqc, read, 3, fromqcbuf, sizeof(fromqcbuf)); - fdout = 1; /* stdout */ - fdin = 0; /* stdin */ - if (ndelay_on(fdout) == -1) - /* this is so stupid: NDELAY semantics should be default on write */ - senddied(); /* drastic, but better than risking deadlock */ + fdout = 1; /* stdout */ + fdin = 0; /* stdin */ + if (ndelay_on(fdout) == -1) /* this is so stupid: NDELAY semantics should be default on write */ + senddied(); /* drastic, but better than risking deadlock */ - while (!stralloc_ready(&comm_buf,1024)) nomem(); + while (!stralloc_ready(&comm_buf, 1024)) nomem(); } int comm_canwrite(void) @@ -202,59 +224,62 @@ void comm_write(unsigned long id, int local, int remote) { int pos; char *s; - - if (local && remote) s="B"; - else if (local) s="L"; - else if (remote) s="R"; - else s="X"; - + + if (local && remote) + s = "B"; + else if (local) + s = "L"; + else if (remote) + s = "R"; + else + s = "X"; + pos = comm_buf.len; - strnum[fmt_ulong(strnum,id)] = 0; - if (!stralloc_cats(&comm_buf,"D")) goto FAIL; - if (!stralloc_cats(&comm_buf,s)) goto FAIL; - if (!stralloc_cats(&comm_buf,strnum)) goto FAIL; + strnum[fmt_ulong(strnum, id)] = 0; + if (!stralloc_cats(&comm_buf, "D")) goto FAIL; + if (!stralloc_cats(&comm_buf, s)) goto FAIL; + if (!stralloc_cats(&comm_buf, strnum)) goto FAIL; if (!stralloc_0(&comm_buf)) goto FAIL; return; - - FAIL: + +FAIL: /* either all or nothing */ comm_buf.len = pos; } -void comm_info(unsigned long id, unsigned long size, char* from, unsigned long pid, unsigned long uid) +void comm_info(unsigned long id, unsigned long size, char *from, unsigned long pid, unsigned long uid) { int pos; int i; pos = comm_buf.len; - if (!stralloc_cats(&comm_buf,"Linfo msg ")) goto FAIL; - strnum[fmt_ulong(strnum,id)] = 0; - if (!stralloc_cats(&comm_buf,strnum)) goto FAIL; - if (!stralloc_cats(&comm_buf,": bytes ")) goto FAIL; - strnum[fmt_ulong(strnum,size)] = 0; - if (!stralloc_cats(&comm_buf,strnum)) goto FAIL; - if (!stralloc_cats(&comm_buf," from <")) goto FAIL; + if (!stralloc_cats(&comm_buf, "Linfo msg ")) goto FAIL; + strnum[fmt_ulong(strnum, id)] = 0; + if (!stralloc_cats(&comm_buf, strnum)) goto FAIL; + if (!stralloc_cats(&comm_buf, ": bytes ")) goto FAIL; + strnum[fmt_ulong(strnum, size)] = 0; + if (!stralloc_cats(&comm_buf, strnum)) goto FAIL; + if (!stralloc_cats(&comm_buf, " from <")) goto FAIL; i = comm_buf.len; - if (!stralloc_cats(&comm_buf,from)) goto FAIL; - + if (!stralloc_cats(&comm_buf, from)) goto FAIL; + for (; i < comm_buf.len; ++i) if (comm_buf.s[i] == '\n') comm_buf.s[i] = '/'; - else - if (!issafe(comm_buf.s[i])) - comm_buf.s[i] = '_'; - - if (!stralloc_cats(&comm_buf,"> qp ")) goto FAIL; - strnum[fmt_ulong(strnum,pid)] = 0; - if (!stralloc_cats(&comm_buf,strnum)) goto FAIL; - if (!stralloc_cats(&comm_buf," uid ")) goto FAIL; - strnum[fmt_ulong(strnum,uid)] = 0; - if (!stralloc_cats(&comm_buf,strnum)) goto FAIL; - if (!stralloc_cats(&comm_buf,"\n")) goto FAIL; + else if (!issafe(comm_buf.s[i])) + comm_buf.s[i] = '_'; + + if (!stralloc_cats(&comm_buf, "> qp ")) goto FAIL; + strnum[fmt_ulong(strnum, pid)] = 0; + if (!stralloc_cats(&comm_buf, strnum)) goto FAIL; + if (!stralloc_cats(&comm_buf, " uid ")) goto FAIL; + strnum[fmt_ulong(strnum, uid)] = 0; + if (!stralloc_cats(&comm_buf, strnum)) goto FAIL; + if (!stralloc_cats(&comm_buf, "\n")) goto FAIL; if (!stralloc_0(&comm_buf)) goto FAIL; return; - FAIL: +FAIL: /* either all or nothing */ comm_buf.len = pos; } @@ -262,23 +287,20 @@ void comm_info(unsigned long id, unsigned long size, char* from, unsigned long p void comm_exit(void) { /* if it FAILs exit, we have already stoped */ - if (!stralloc_cats(&comm_buf,"X")) _exit(1); + if (!stralloc_cats(&comm_buf, "X")) _exit(1); if (!stralloc_0(&comm_buf)) _exit(1); } void comm_selprep(int *nfds, fd_set *wfds, fd_set *rfds) { if (flagsendalive) { - if (flagquitasap && comm_canwrite() == 0) - comm_exit(); + if (flagquitasap && comm_canwrite() == 0) comm_exit(); if (comm_canwrite()) { - FD_SET(fdout,wfds); - if (*nfds <= fdout) - *nfds = fdout + 1; + FD_SET(fdout, wfds); + if (*nfds <= fdout) *nfds = fdout + 1; } - FD_SET(fdin,rfds); - if (*nfds <= fdin) - *nfds = fdin + 1; + FD_SET(fdin, rfds); + if (*nfds <= fdin) *nfds = fdin + 1; } } @@ -287,43 +309,35 @@ void comm_do(fd_set *wfds, fd_set *rfds) /* first write then read */ if (flagsendalive) if (comm_canwrite()) - if (FD_ISSET(fdout,wfds)) { - int w; - int len; - len = comm_buf.len; - w = write(fdout,comm_buf.s + comm_pos,len - comm_pos); - if (w <= 0) { - if ((w == -1) && (errno == EPIPE)) - senddied(); - } else { - comm_pos += w; - if (comm_pos == len) { - comm_buf.len = 0; - comm_pos = 0; - } - } + if (FD_ISSET(fdout, wfds)) { + int w; + int len; + len = comm_buf.len; + w = write(fdout, comm_buf.s + comm_pos, len - comm_pos); + if (w <= 0) { + if ((w == -1) && (errno == EPIPE)) senddied(); + } else { + comm_pos += w; + if (comm_pos == len) { + comm_buf.len = 0; + comm_pos = 0; + } + } } if (flagsendalive) - if (FD_ISSET(fdin,rfds)) { + if (FD_ISSET(fdin, rfds)) { /* there are only two messages 'H' and 'X' */ char c; int r; r = read(fdin, &c, 1); if (r <= 0) { - if ((r == -1) && (errno != EINTR)) - senddied(); + if ((r == -1) && (errno != EINTR)) senddied(); } else { - switch (c) { - case 'H': - sighup(); - break; - case 'X': - sigterm(); - break; - default: - sendlog1("warning: qmail-todo: qmail-send speaks an obscure dialect\n"); - break; - } + switch (c) { + case 'H': sighup(); break; + case 'X': sigterm(); break; + default: sendlog1("warning: qmail-todo: qmail-send speaks an obscure dialect\n"); break; + } } } } @@ -348,7 +362,7 @@ void todo_init(void) void todo_selprep(int *nfds, fd_set *rfds, datetime_sec *wakeup) { if (flagquitasap) return; - trigger_selprep(nfds,rfds); + trigger_selprep(nfds, rfds); if (flagtododir) *wakeup = 0; if (*wakeup > nexttodorun) *wakeup = nexttodorun; } @@ -356,9 +370,9 @@ void todo_selprep(int *nfds, fd_set *rfds, datetime_sec *wakeup) void todo_do(fd_set *rfds) { struct stat st; - buffer bi; + buffer bi; int fd; - buffer bo; + buffer bo; int fdnumber; buffer bchan[CHANNELS]; int fdchan[CHANNELS]; @@ -372,8 +386,7 @@ void todo_do(fd_set *rfds) fd = -1; fdnumber = -1; - for (c = 0; c < CHANNELS; ++c) - fdchan[c] = -1; + for (c = 0; c < CHANNELS; ++c) fdchan[c] = -1; if (flagquitasap) return; @@ -382,12 +395,12 @@ void todo_do(fd_set *rfds) if (recent < nexttodorun) return; } trigger_set(); - readsubdir_init(&todosubdir,"todo",pausedir); + readsubdir_init(&todosubdir, "todo", pausedir); flagtododir = 1; nexttodorun = recent + SLEEP_TODO; } - switch (readsubdir_next(&todosubdir,&id)) { + switch (readsubdir_next(&todosubdir, &id)) { case 1: break; case 0: flagtododir = 0; default: return; @@ -396,114 +409,149 @@ void todo_do(fd_set *rfds) fnmake_todo(id); fd = open_read(fn.s); - if (fd == -1) { sendlog3("warning: qmail-todo: unable to open ",fn.s,"\n"); return; } + if (fd == -1) { + sendlog3("warning: qmail-todo: unable to open ", fn.s, "\n"); + return; + } fnmake_mess(id); /* just for the statistics */ - if (stat(fn.s,&st) == -1) - { sendlog3("warning: qmail-todo: unable to stat ",fn.s," for mess\n"); goto FAIL; } + if (stat(fn.s, &st) == -1) { + sendlog3("warning: qmail-todo: unable to stat ", fn.s, " for mess\n"); + goto FAIL; + } for (c = 0; c < CHANNELS; ++c) { - fnmake_chanaddr(id,c); - if (unlink(fn.s) == -1) if (errno != ENOENT) - { sendlog3("warning: qmail-todo: unable to unlink ",fn.s," for mess\n"); goto FAIL; } + fnmake_chanaddr(id, c); + if (unlink(fn.s) == -1) + if (errno != ENOENT) { + sendlog3("warning: qmail-todo: unable to unlink ", fn.s, " for mess\n"); + goto FAIL; + } } fnmake_info(id); - if (unlink(fn.s) == -1) if (errno != ENOENT) - { sendlog3("warning: qmail-todo: unable to unlink ",fn.s," for info\n"); goto FAIL; } + if (unlink(fn.s) == -1) + if (errno != ENOENT) { + sendlog3("warning: qmail-todo: unable to unlink ", fn.s, " for info\n"); + goto FAIL; + } fdnumber = open_excl(fn.s); - if (fdnumber == -1) - { sendlog3("warning: qmail-todo: unable to create ",fn.s," for info\n"); goto FAIL; } + if (fdnumber == -1) { + sendlog3("warning: qmail-todo: unable to create ", fn.s, " for info\n"); + goto FAIL; + } - strnum[fmt_ulong(strnum,id)] = 0; - sendlog3("new msg ",strnum,"\n"); + strnum[fmt_ulong(strnum, id)] = 0; + sendlog3("new msg ", strnum, "\n"); - for (c = 0; c < CHANNELS; ++c) - flagchan[c] = 0; + for (c = 0; c < CHANNELS; ++c) flagchan[c] = 0; - buffer_init(&bi,read,fd,todobuf,sizeof(todobuf)); - buffer_init(&bo,write,fdnumber,todobufinfo,sizeof(todobufinfo)); + buffer_init(&bi, read, fd, todobuf, sizeof(todobuf)); + buffer_init(&bo, write, fdnumber, todobufinfo, sizeof(todobufinfo)); uid = 0; pid = 0; for (;;) { - if (getln(&bi,&todoline,&match,'\0') == -1) { + if (getln(&bi, &todoline, &match, '\0') == -1) { /* perhaps we're out of memory, perhaps an I/O error */ fnmake_todo(id); - sendlog3("warning: qmail-todo: trouble reading ",fn.s,"\n"); goto FAIL; + sendlog3("warning: qmail-todo: trouble reading ", fn.s, "\n"); + goto FAIL; } if (!match) break; switch (todoline.s[0]) { - case 'u': - scan_ulong(todoline.s + 1,&uid); break; - case 'p': - scan_ulong(todoline.s + 1,&pid); break; - case 'F': - if (buffer_putflush(&bo,todoline.s,todoline.len) == -1) { - fnmake_info(id); - sendlog3("warning: qmail-todo: trouble writing to ",fn.s," for todo\n"); goto FAIL; - } - comm_info(id,(unsigned long) st.st_size,todoline.s + 1,pid,uid); - break; - case 'T': - switch (rewrite(todoline.s + 1)) { - case 0: nomem(); goto FAIL; - case 2: c = 1; break; - default: c = 0; break; - } - if (fdchan[c] == -1) { - fnmake_chanaddr(id,c); - fdchan[c] = open_excl(fn.s); - if (fdchan[c] == -1) - { sendlog3("warning: qmail-todo: unable to create ",fn.s," for delivery\n"); goto FAIL; } - buffer_init(&bchan[c],write,fdchan[c],todobufchan[c],sizeof(todobufchan[c])); - flagchan[c] = 1; - } - if (buffer_put(&bchan[c],rwline.s,rwline.len) == -1) { - fnmake_chanaddr(id,c); - sendlog3("warning: qmail-todo: trouble writing to ",fn.s," for delivery\n"); goto FAIL; - } - break; - default: - fnmake_todo(id); - sendlog3("warning: qmail-todo: unknown record type in ",fn.s,"\n"); goto FAIL; + case 'u': scan_ulong(todoline.s + 1, &uid); break; + case 'p': scan_ulong(todoline.s + 1, &pid); break; + case 'F': + if (buffer_putflush(&bo, todoline.s, todoline.len) == -1) { + fnmake_info(id); + sendlog3("warning: qmail-todo: trouble writing to ", fn.s, " for todo\n"); + goto FAIL; + } + comm_info(id, (unsigned long)st.st_size, todoline.s + 1, pid, uid); + break; + case 'T': + switch (rewrite(todoline.s + 1)) { + case 0: nomem(); goto FAIL; + case 2: c = 1; break; + default: c = 0; break; + } + if (fdchan[c] == -1) { + fnmake_chanaddr(id, c); + fdchan[c] = open_excl(fn.s); + if (fdchan[c] == -1) { + sendlog3("warning: qmail-todo: unable to create ", fn.s, " for delivery\n"); + goto FAIL; + } + buffer_init(&bchan[c], write, fdchan[c], todobufchan[c], sizeof(todobufchan[c])); + flagchan[c] = 1; + } + if (buffer_put(&bchan[c], rwline.s, rwline.len) == -1) { + fnmake_chanaddr(id, c); + sendlog3("warning: qmail-todo: trouble writing to ", fn.s, " for delivery\n"); + goto FAIL; + } + break; + default: + fnmake_todo(id); + sendlog3("warning: qmail-todo: unknown record type in ", fn.s, "\n"); + goto FAIL; } } - close(fd); fd = -1; + close(fd); + fd = -1; fnmake_info(id); - if (buffer_flush(&bo) == -1) - { sendlog3("warning: qmail-todo: trouble writing to ",fn.s," for info\n"); goto FAIL; } - if (fsync(fdnumber) == -1) - { sendlog3("warning: qmail-todo: trouble fsyncing ",fn.s," for info\n"); goto FAIL; } - close(fdnumber); fdnumber = -1; + if (buffer_flush(&bo) == -1) { + sendlog3("warning: qmail-todo: trouble writing to ", fn.s, " for info\n"); + goto FAIL; + } + if (fsync(fdnumber) == -1) { + sendlog3("warning: qmail-todo: trouble fsyncing ", fn.s, " for info\n"); + goto FAIL; + } + close(fdnumber); + fdnumber = -1; for (c = 0; c < CHANNELS; ++c) if (fdchan[c] != -1) { - fnmake_chanaddr(id,c); - if (buffer_flush(&bchan[c]) == -1) { sendlog3("warning: qmail-todo: trouble writing to ",fn.s," in channel\n"); goto FAIL; } - if (fsync(fdchan[c]) == -1) { sendlog3("warning: qmail-todo: trouble fsyncing ",fn.s," in channel\n"); goto FAIL; } - close(fdchan[c]); fdchan[c] = -1; + fnmake_chanaddr(id, c); + if (buffer_flush(&bchan[c]) == -1) { + sendlog3("warning: qmail-todo: trouble writing to ", fn.s, " in channel\n"); + goto FAIL; + } + if (fsync(fdchan[c]) == -1) { + sendlog3("warning: qmail-todo: trouble fsyncing ", fn.s, " in channel\n"); + goto FAIL; + } + close(fdchan[c]); + fdchan[c] = -1; } fnmake_todo(id); - if (buffer_putflush(&toqc,fn.s,fn.len) == -1) { cleandied(); return; } - if (buffer_get(&fromqc,&ch,1) != 1) { cleandied(); return; } + if (buffer_putflush(&toqc, fn.s, fn.len) == -1) { + cleandied(); + return; + } + if (buffer_get(&fromqc, &ch, 1) != 1) { + cleandied(); + return; + } if (ch != '+') { - sendlog3("warning: qmail-clean unable to clean up ",fn.s,"\n"); + sendlog3("warning: qmail-clean unable to clean up ", fn.s, "\n"); return; } - comm_write(id,flagchan[0],flagchan[1]); + comm_write(id, flagchan[0], flagchan[1]); return; - - FAIL: + +FAIL: if (fd != -1) close(fd); if (fdnumber != -1) close(fdnumber); for (c = 0; c < CHANNELS; ++c) @@ -515,18 +563,26 @@ void todo_do(fd_set *rfds) int getcontrols(void) { if (control_init() == -1) return 0; - if (control_rldef(&envnoathost,"control/envnoathost",1,"envnoathost") != 1) return 0; - if (control_readfile(&locals,"control/locals",1) != 1) return 0; - if (!constmap_init(&maplocals,locals.s,locals.len,0)) return 0; - switch (control_readfile(&percenthack,"control/percenthack",0)) { + if (control_rldef(&envnoathost, "control/envnoathost", 1, "envnoathost") != 1) return 0; + if (control_readfile(&locals, "control/locals", 1) != 1) return 0; + if (!constmap_init(&maplocals, locals.s, locals.len, 0)) return 0; + switch (control_readfile(&percenthack, "control/percenthack", 0)) { case -1: return 0; - case 0: if (!constmap_init(&mappercenthack,"",0,0)) return 0; break; - case 1: if (!constmap_init(&mappercenthack,percenthack.s,percenthack.len,0)) return 0; break; + case 0: + if (!constmap_init(&mappercenthack, "", 0, 0)) return 0; + break; + case 1: + if (!constmap_init(&mappercenthack, percenthack.s, percenthack.len, 0)) return 0; + break; } - switch (control_readfile(&vdoms,"control/virtualdomains",0)) { + switch (control_readfile(&vdoms, "control/virtualdomains", 0)) { case -1: return 0; - case 0: if (!constmap_init(&mapvdoms,"",0,1)) return 0; break; - case 1: if (!constmap_init(&mapvdoms,vdoms.s,vdoms.len,1)) return 0; break; + case 0: + if (!constmap_init(&mapvdoms, "", 0, 1)) return 0; + break; + case 1: + if (!constmap_init(&mapvdoms, vdoms.s, vdoms.len, 1)) return 0; + break; } return 1; } @@ -538,24 +594,27 @@ void regetcontrols(void) { int r; - if (control_readfile(&newlocals,"control/locals",1) != 1) - { sendlog1("alert: qmail-todo: unable to reread control/locals\n"); return; } - r = control_readfile(&newvdoms,"control/virtualdomains",0); - if (r == -1) - { sendlog1("alert: qmail-todo: unable to reread control/virtualdomains\n"); return; } + if (control_readfile(&newlocals, "control/locals", 1) != 1) { + sendlog1("alert: qmail-todo: unable to reread control/locals\n"); + return; + } + r = control_readfile(&newvdoms, "control/virtualdomains", 0); + if (r == -1) { + sendlog1("alert: qmail-todo: unable to reread control/virtualdomains\n"); + return; + } constmap_free(&maplocals); constmap_free(&mapvdoms); - while (!stralloc_copy(&locals,&newlocals)) nomem(); - while (!constmap_init(&maplocals,locals.s,locals.len,0)) nomem(); + while (!stralloc_copy(&locals, &newlocals)) nomem(); + while (!constmap_init(&maplocals, locals.s, locals.len, 0)) nomem(); if (r) { - while (!stralloc_copy(&vdoms,&newvdoms)) nomem(); - while (!constmap_init(&mapvdoms,vdoms.s,vdoms.len,1)) nomem(); - } - else - while (!constmap_init(&mapvdoms,"",0,1)) nomem(); + while (!stralloc_copy(&vdoms, &newvdoms)) nomem(); + while (!constmap_init(&mapvdoms, vdoms.s, vdoms.len, 1)) nomem(); + } else + while (!constmap_init(&mapvdoms, "", 0, 1)) nomem(); } void reread(void) @@ -582,28 +641,36 @@ int main() int r; char c; - if (chdir(auto_qmail) == -1) - { sendlog1("alert: qmail-todo: cannot start: unable to switch to home directory\n"); _exit(110); } - if (!getcontrols()) - { sendlog1("alert: qmail-todo: cannot start: unable to read controls\n"); _exit(112); } - if (chdir("queue") == -1) - { sendlog1("alert: qmail-todo: cannot start: unable to switch to queue directory\n"); _exit(110); } + if (chdir(auto_qmail) == -1) { + sendlog1("alert: qmail-todo: cannot start: unable to switch to home directory\n"); + _exit(110); + } + if (!getcontrols()) { + sendlog1("alert: qmail-todo: cannot start: unable to read controls\n"); + _exit(112); + } + if (chdir("queue") == -1) { + sendlog1("alert: qmail-todo: cannot start: unable to switch to queue directory\n"); + _exit(110); + } sig_pipeignore(); umask(077); fnmake_init(); todo_init(); comm_init(); - + do { r = read(fdin, &c, 1); - if ((r == -1) && (errno != EINTR)) - _exit(100); /* read failed probably qmail-send died */ - } while ((r =! 1)); /* we assume it is a 'S' */ - + if ((r == -1) && (errno != EINTR)) _exit(100); /* read failed probably qmail-send died */ + } while ((r = !1)); /* we assume it is a 'S' */ + for (;;) { recent = now(); - if (flagreadasap) { flagreadasap = 0; reread(); } + if (flagreadasap) { + flagreadasap = 0; + reread(); + } if (!flagsendalive) { /* qmail-send finaly exited, so do the same. */ if (flagquitasap) _exit(0); @@ -616,14 +683,16 @@ int main() FD_ZERO(&wfds); nfds = 1; - todo_selprep(&nfds,&rfds,&wakeup); - comm_selprep(&nfds,&wfds,&rfds); + todo_selprep(&nfds, &rfds, &wakeup); + comm_selprep(&nfds, &wfds, &rfds); - if (wakeup <= recent) tv.tv_sec = 0; - else tv.tv_sec = wakeup - recent + SLEEP_FUZZ; + if (wakeup <= recent) + tv.tv_sec = 0; + else + tv.tv_sec = wakeup - recent + SLEEP_FUZZ; tv.tv_usec = 0; - if (select(nfds,&rfds,&wfds,(fd_set *) 0,&tv) == -1) + if (select(nfds, &rfds, &wfds, (fd_set *)0, &tv) == -1) if (errno == EINTR) ; else @@ -638,4 +707,3 @@ int main() /* NOTREACHED */ _exit(1); } - diff --git a/src/qmail-vmailuser.c b/src/qmail-vmailuser.c index 27dc85d..82cb243 100644 --- a/src/qmail-vmailuser.c +++ b/src/qmail-vmailuser.c @@ -1,24 +1,25 @@ -#include <sys/types.h> #include <sys/stat.h> +#include <sys/types.h> #include <unistd.h> -#include "global.h" -#include "auto_qmail.h" -#include "stralloc.h" + +#include "byte.h" #include "case.h" -#include "control.h" #include "constmap.h" #include "direntry.h" #include "error.h" -#include "str.h" #include "fmt.h" #include "open.h" -#include "byte.h" #include "scan.h" #include "str.h" +#include "stralloc.h" + +#include "auto_qmail.h" +#include "control.h" +#include "global.h" -#define FDAUTH 3 +#define FDAUTH 3 #define RESPECT_CASE "-C" -#define BUFFER_SIZE 128 +#define BUFFER_SIZE 128 /** @file qmail-vmailuser.c @return 0: virtual user exists @@ -44,10 +45,10 @@ void pam_exit(int fail) _exit(fail); } -int main(int argc,char **argv) +int main(int argc, char **argv) { DIR *dir; - char *vdomuser; + char *vdomuser; char *domain = 0; int buflen = 0; int domlen = 0; @@ -56,29 +57,30 @@ int main(int argc,char **argv) char ch; char *homedir = "/home"; - if (argv[1]) - if (!case_diffs(argv[1],RESPECT_CASE)) { - flagrespect = 1; + if (argv[1]) + if (!case_diffs(argv[1], RESPECT_CASE)) { + flagrespect = 1; } else { homedir = argv[1]; dir = opendir(homedir); if (!dir) pam_exit(2); } - if (argv[2]) - if (!case_diffs(argv[2],RESPECT_CASE)) flagrespect = 1; + if (argv[2]) + if (!case_diffs(argv[2], RESPECT_CASE)) flagrespect = 1; if (chdir(auto_qmail) == -1) pam_exit(110); - - switch (control_readfile(&vdoms,"control/virtualdomains",0)) { + + switch (control_readfile(&vdoms, "control/virtualdomains", 0)) { case -1: pam_exit(110); - case 0: if (!constmap_init(&mapvdoms,"",0,1)) pam_exit(111); - case 1: if (!constmap_init(&mapvdoms,vdoms.s,vdoms.len,1)) pam_exit(111); + case 0: + if (!constmap_init(&mapvdoms, "", 0, 1)) pam_exit(111); + case 1: + if (!constmap_init(&mapvdoms, vdoms.s, vdoms.len, 1)) pam_exit(111); } - for (;;) { /* read input */ - do - r = read(FDAUTH,inputbuf + buflen,sizeof(inputbuf) - buflen); + for (;;) { /* read input */ + do r = read(FDAUTH, inputbuf + buflen, sizeof(inputbuf) - buflen); while ((r == -1) && (errno == EINTR)); if (r == -1) pam_exit(111); if (r == 0) break; @@ -87,62 +89,62 @@ int main(int argc,char **argv) } close(FDAUTH); - if ((r = byte_rchr(inputbuf,buflen,'@'))) /* @domain */ + if ((r = byte_rchr(inputbuf, buflen, '@'))) /* @domain */ if (r < buflen && inputbuf[r] == '@') { domain = inputbuf + r + 1; domlen = str_len(domain); if (!flagrespect) - case_lowerb(inputbuf,buflen); + case_lowerb(inputbuf, buflen); else - case_lowerb(domain,domlen); + case_lowerb(domain, domlen); } - vdomuser = constmap(&mapvdoms,domain,domlen); + vdomuser = constmap(&mapvdoms, domain, domlen); if (!vdomuser) pam_exit(1); - if (!stralloc_copys(&vuser,"")) pam_exit(111); /* user */ + if (!stralloc_copys(&vuser, "")) pam_exit(111); /* user */ for (i = 0; i < r; ++i) { ch = inputbuf[i]; if (ch == '.') ch = ':'; - if (!stralloc_append(&vuser,&ch)) pam_exit(111); + if (!stralloc_append(&vuser, &ch)) pam_exit(111); } - if (!stralloc_0(&vuser)) pam_exit(111); + if (!stralloc_0(&vuser)) pam_exit(111); - if (!stralloc_copys(&vdomdir,homedir)) pam_exit(111); /* vpopmail */ - if (!stralloc_cats(&vdomdir,"/")) pam_exit(111); - if (!stralloc_cats(&vdomdir,"vpopmail")) pam_exit(111); - if (!stralloc_copy(&vuserdir,&vdomdir)) pam_exit(111); - if (!stralloc_cats(&vuserdir,"/domains/")) pam_exit(111); - if (!stralloc_cats(&vuserdir,vdomuser)) pam_exit(111); - if (!stralloc_copy(&vdomdir,&vuserdir)) pam_exit(111); - if (!stralloc_0(&vdomdir)) pam_exit(111); + if (!stralloc_copys(&vdomdir, homedir)) pam_exit(111); /* vpopmail */ + if (!stralloc_cats(&vdomdir, "/")) pam_exit(111); + if (!stralloc_cats(&vdomdir, "vpopmail")) pam_exit(111); + if (!stralloc_copy(&vuserdir, &vdomdir)) pam_exit(111); + if (!stralloc_cats(&vuserdir, "/domains/")) pam_exit(111); + if (!stralloc_cats(&vuserdir, vdomuser)) pam_exit(111); + if (!stralloc_copy(&vdomdir, &vuserdir)) pam_exit(111); + if (!stralloc_0(&vdomdir)) pam_exit(111); dir = opendir(vdomdir.s); if (dir) { - if (!stralloc_cats(&vuserdir,"/")) pam_exit(111); - if (!stralloc_cat(&vuserdir,&vuser)) pam_exit(111); - if (!stralloc_0(&vuserdir)) pam_exit(111); + if (!stralloc_cats(&vuserdir, "/")) pam_exit(111); + if (!stralloc_cat(&vuserdir, &vuser)) pam_exit(111); + if (!stralloc_0(&vuserdir)) pam_exit(111); dir = opendir(vuserdir.s); if (dir) pam_exit(0); } - if (!stralloc_copys(&vdomdir,homedir)) pam_exit(111); /* vmailmgr */ - if (!stralloc_cats(&vdomdir,"/")) pam_exit(111); - if (!stralloc_copy(&vuserdir,&vdomdir)) pam_exit(111); - if (!stralloc_cats(&vuserdir,vdomuser)) pam_exit(111); - if (!stralloc_cats(&vuserdir,"/users")) pam_exit(111); - if (!stralloc_copy(&vdomdir,&vuserdir)) pam_exit(111); - if (!stralloc_0(&vdomdir)) pam_exit(111); + if (!stralloc_copys(&vdomdir, homedir)) pam_exit(111); /* vmailmgr */ + if (!stralloc_cats(&vdomdir, "/")) pam_exit(111); + if (!stralloc_copy(&vuserdir, &vdomdir)) pam_exit(111); + if (!stralloc_cats(&vuserdir, vdomuser)) pam_exit(111); + if (!stralloc_cats(&vuserdir, "/users")) pam_exit(111); + if (!stralloc_copy(&vdomdir, &vuserdir)) pam_exit(111); + if (!stralloc_0(&vdomdir)) pam_exit(111); dir = opendir(vdomdir.s); if (dir) { - if (!stralloc_cats(&vuserdir,"/")) pam_exit(111); - if (!stralloc_cat(&vuserdir,&vuser)) pam_exit(111); - if (!stralloc_0(&vuserdir)) pam_exit(111); + if (!stralloc_cats(&vuserdir, "/")) pam_exit(111); + if (!stralloc_cat(&vuserdir, &vuser)) pam_exit(111); + if (!stralloc_0(&vuserdir)) pam_exit(111); dir = opendir(vuserdir.s); if (dir) pam_exit(0); } - + pam_exit(1); } diff --git a/src/qmail.c b/src/qmail.c index 7bdfd29..f143af1 100644 --- a/src/qmail.c +++ b/src/qmail.c @@ -1,21 +1,22 @@ +#include "qmail.h" + #include <unistd.h> -#include "readwrite.h" + #include "buffer.h" -#include "wait.h" +#include "env.h" #include "exit.h" #include "fd.h" -#include "qmail.h" +#include "readwrite.h" +#include "wait.h" + #include "auto_qmail.h" -#include "env.h" -static char *binqqargs[2] = { 0, 0 } ; +static char *binqqargs[2] = {0, 0}; static void setup_qqargs() { - if (!binqqargs[0]) - binqqargs[0] = env_get("QMAILQUEUE"); - if (!binqqargs[0]) - binqqargs[0] = "bin/qmail-queue"; + if (!binqqargs[0]) binqqargs[0] = env_get("QMAILQUEUE"); + if (!binqqargs[0]) binqqargs[0] = "bin/qmail-queue"; } int qmail_open(struct qmail *qq) @@ -26,26 +27,34 @@ int qmail_open(struct qmail *qq) setup_qqargs(); if (pipe(pim) == -1) return -1; - if (pipe(pie) == -1) { close(pim[0]); close(pim[1]); return -1; } - + if (pipe(pie) == -1) { + close(pim[0]); + close(pim[1]); + return -1; + } + switch (qq->pid = vfork()) { case -1: - close(pim[0]); close(pim[1]); - close(pie[0]); close(pie[1]); + close(pim[0]); + close(pim[1]); + close(pie[0]); + close(pie[1]); return -1; case 0: close(pim[1]); close(pie[1]); - if (fd_move(0,pim[0]) == -1) _exit(120); - if (fd_move(1,pie[0]) == -1) _exit(120); + if (fd_move(0, pim[0]) == -1) _exit(120); + if (fd_move(1, pie[0]) == -1) _exit(120); if (chdir(auto_qmail) == -1) _exit(61); - execv(*binqqargs,binqqargs); + execv(*binqqargs, binqqargs); _exit(120); } - qq->fdm = pim[1]; close(pim[0]); - qq->fde = pie[1]; close(pie[0]); - buffer_init(&qq->ss,write,qq->fdm,qq->buf,sizeof(qq->buf)); + qq->fdm = pim[1]; + close(pim[0]); + qq->fde = pie[1]; + close(pie[0]); + buffer_init(&qq->ss, write, qq->fdm, qq->buf, sizeof(qq->buf)); qq->flagerr = 0; return 0; } @@ -60,31 +69,33 @@ void qmail_fail(struct qmail *qq) qq->flagerr = 1; } -void qmail_put(struct qmail *qq,char *s,int len) +void qmail_put(struct qmail *qq, char *s, int len) { - if (!qq->flagerr) if (buffer_put(&qq->ss,s,len) == -1) qq->flagerr = 1; + if (!qq->flagerr) + if (buffer_put(&qq->ss, s, len) == -1) qq->flagerr = 1; } -void qmail_puts(struct qmail *qq,char *s) +void qmail_puts(struct qmail *qq, char *s) { - if (!qq->flagerr) if (buffer_puts(&qq->ss,s) == -1) qq->flagerr = 1; + if (!qq->flagerr) + if (buffer_puts(&qq->ss, s) == -1) qq->flagerr = 1; } -void qmail_from(struct qmail *qq,char *s) +void qmail_from(struct qmail *qq, char *s) { if (buffer_flush(&qq->ss) == -1) qq->flagerr = 1; close(qq->fdm); - buffer_init(&qq->ss,write,qq->fde,qq->buf,sizeof(qq->buf)); - qmail_put(qq,"F",1); - qmail_puts(qq,s); - qmail_put(qq,"",1); + buffer_init(&qq->ss, write, qq->fde, qq->buf, sizeof(qq->buf)); + qmail_put(qq, "F", 1); + qmail_puts(qq, s); + qmail_put(qq, "", 1); } -void qmail_to(struct qmail *qq,char *s) +void qmail_to(struct qmail *qq, char *s) { - qmail_put(qq,"T",1); - qmail_puts(qq,s); - qmail_put(qq,"",1); + qmail_put(qq, "T", 1); + qmail_puts(qq, s); + qmail_put(qq, "", 1); } char *qmail_close(struct qmail *qq) @@ -92,48 +103,46 @@ char *qmail_close(struct qmail *qq) int wstat; int exitcode; - qmail_put(qq,"",1); - if (!qq->flagerr) + qmail_put(qq, "", 1); + if (!qq->flagerr) if (buffer_flush(&qq->ss) == -1) qq->flagerr = 1; close(qq->fde); - if (wait_pid(&wstat,qq->pid) != qq->pid) - return "Zqq waitpid surprise (#4.3.0)"; - if (wait_crashed(wstat)) - return "Zqq crashed (#4.3.0)"; + if (wait_pid(&wstat, qq->pid) != qq->pid) return "Zqq waitpid surprise (#4.3.0)"; + if (wait_crashed(wstat)) return "Zqq crashed (#4.3.0)"; exitcode = wait_exitcode(wstat); switch (exitcode) { - case 0: if (!qq->flagerr) return ""; /* fall through */ - case 11: return "Denvelope address too long for qq (#5.1.3)"; - case 31: return "Dmail server permanently rejected message (#5.3.0)"; - case 32: return "Vmail server does not accept message (#5.3.0)"; - case 33: return "Smail server does not accept message (#5.3.0)"; - case 34: return "Amail server does not accept message (#5.3.0)"; - case 35: return "Imail server fails to verify DKIM signed message (#5.3.0)"; - case 51: return "Zqq out of memory (#4.3.0)"; - case 52: return "Zqq timeout (#4.3.0)"; - case 53: return "Zqq write error or disk full (#4.3.0)"; - case 54: return "Zqq read error (#4.3.0)"; - case 55: return "Zqq unable to read configuration (#4.3.0)"; - case 56: return "Zqq trouble making network connection (#4.3.0)"; - case 61: return "Zqq trouble in home directory (#4.3.0)"; - case 62: return "Zqq trouble creating files in queue (#4.3.0)"; - case 63: /* qmail-queue: fstat/unlinking problem */ - case 64: /* qmail-queue: linking pidfn -> messfn */ - case 65: /* qmail-queue: exclusive open failed */ - case 66: /* qmail-queue: linking intdfn -> todofn */ - case 71: return "Zmail server temporarily rejected message (#4.3.0)"; - case 72: return "Zconnection to mail server timed out (#4.4.1)"; - case 73: return "Zconnection to mail server rejected (#4.4.1)"; - case 74: return "Zcommunication with mail server failed (#4.4.2)"; - case 91: /* fall through */ - case 81: return "Zqq internal bug (#4.3.0)"; + case 0: + if (!qq->flagerr) return ""; /* fall through */ + case 11: return "Denvelope address too long for qq (#5.1.3)"; + case 31: return "Dmail server permanently rejected message (#5.3.0)"; + case 32: return "Vmail server does not accept message (#5.3.0)"; + case 33: return "Smail server does not accept message (#5.3.0)"; + case 34: return "Amail server does not accept message (#5.3.0)"; + case 35: return "Imail server fails to verify DKIM signed message (#5.3.0)"; + case 51: return "Zqq out of memory (#4.3.0)"; + case 52: return "Zqq timeout (#4.3.0)"; + case 53: return "Zqq write error or disk full (#4.3.0)"; + case 54: return "Zqq read error (#4.3.0)"; + case 55: return "Zqq unable to read configuration (#4.3.0)"; + case 56: return "Zqq trouble making network connection (#4.3.0)"; + case 61: return "Zqq trouble in home directory (#4.3.0)"; + case 62: return "Zqq trouble creating files in queue (#4.3.0)"; + case 63: /* qmail-queue: fstat/unlinking problem */ + case 64: /* qmail-queue: linking pidfn -> messfn */ + case 65: /* qmail-queue: exclusive open failed */ + case 66: /* qmail-queue: linking intdfn -> todofn */ + case 71: return "Zmail server temporarily rejected message (#4.3.0)"; + case 72: return "Zconnection to mail server timed out (#4.4.1)"; + case 73: return "Zconnection to mail server rejected (#4.4.1)"; + case 74: return "Zcommunication with mail server failed (#4.4.2)"; + case 91: /* fall through */ + case 81: return "Zqq internal bug (#4.3.0)"; case 115: /* compatibility */ case 120: return "Zunable to exec qq (#4.3.0)"; default: - if ((exitcode >= 11) && (exitcode <= 40)) - return "Dqq permanent problem (#5.3.0)"; + if ((exitcode >= 11) && (exitcode <= 40)) return "Dqq permanent problem (#5.3.0)"; return "Zqq temporary problem (#4.3.0)"; } } diff --git a/src/qreceipt.c b/src/qreceipt.c index 8dacf40..6b629ba 100644 --- a/src/qreceipt.c +++ b/src/qreceipt.c @@ -1,43 +1,76 @@ #include <unistd.h> -#include "sig.h" + +#include "alloc.h" +#include "buffer.h" #include "env.h" #include "error.h" -#include "buffer.h" -#include "stralloc.h" +#include "exit.h" #include "getln.h" -#include "alloc.h" +#include "open.h" +#include "sig.h" #include "str.h" -#include "hfield.h" -#include "token822.h" +#include "stralloc.h" + #include "headerbody.h" -#include "exit.h" -#include "open.h" -#include "quote.h" +#include "hfield.h" #include "qmail.h" +#include "quote.h" +#include "token822.h" #define WHO "qreceipt" -void die_noreceipt() { _exit(0); } -void die() { _exit(100); } -void die_temp() { _exit(111); } -void die_nomem() { - buffer_putsflush(buffer_2,"qreceipt: fatal: out of memory\n"); die_temp(); } -void die_fork() { - buffer_putsflush(buffer_2,"qreceipt: fatal: unable to fork\n"); die_temp(); } -void die_qqperm() { - buffer_putsflush(buffer_2,"qreceipt: fatal: permanent qmail-queue error\n"); die(); } -void die_qqtemp() { - buffer_putsflush(buffer_2,"qreceipt: fatal: temporary qmail-queue error\n"); die_temp(); } -void die_usage() { - buffer_putsflush(buffer_2, - "qreceipt: usage: qreceipt deliveryaddress\n"); die(); } -void die_read() { - if (errno == ENOMEM) die_nomem(); - buffer_putsflush(buffer_2,"qreceipt: fatal: read error\n"); die_temp(); } -void doordie(sa,r) stralloc *sa; int r; { - if (r == 1) return; if (r == -1) die_nomem(); - buffer_putsflush(buffer_2,"qreceipt: fatal: unable to parse this: "); - buffer_putflush(buffer_2,sa->s,sa->len); die(); } +void die_noreceipt() +{ + _exit(0); +} +void die() +{ + _exit(100); +} +void die_temp() +{ + _exit(111); +} +void die_nomem() +{ + buffer_putsflush(buffer_2, "qreceipt: fatal: out of memory\n"); + die_temp(); +} +void die_fork() +{ + buffer_putsflush(buffer_2, "qreceipt: fatal: unable to fork\n"); + die_temp(); +} +void die_qqperm() +{ + buffer_putsflush(buffer_2, "qreceipt: fatal: permanent qmail-queue error\n"); + die(); +} +void die_qqtemp() +{ + buffer_putsflush(buffer_2, "qreceipt: fatal: temporary qmail-queue error\n"); + die_temp(); +} +void die_usage() +{ + buffer_putsflush(buffer_2, "qreceipt: usage: qreceipt deliveryaddress\n"); + die(); +} +void die_read() +{ + if (errno == ENOMEM) die_nomem(); + buffer_putsflush(buffer_2, "qreceipt: fatal: read error\n"); + die_temp(); +} +void doordie(sa, r) stralloc *sa; +int r; +{ + if (r == 1) return; + if (r == -1) die_nomem(); + buffer_putsflush(buffer_2, "qreceipt: fatal: unable to parse this: "); + buffer_putflush(buffer_2, sa->s, sa->len); + die(); +} char *target; @@ -50,12 +83,11 @@ stralloc sanotice = {0}; int rwnotice(token822_alloc *addr) { token822_reverse(addr); - if (token822_unquote(&sanotice,addr) != 1) die_nomem(); + if (token822_unquote(&sanotice, addr) != 1) die_nomem(); if (sanotice.len == str_len(target)) - if (!str_diffn(sanotice.s,target,sanotice.len)) - flagreceipt = 1; - token822_reverse(addr); - return 1; + if (!str_diffn(sanotice.s, target, sanotice.len)) flagreceipt = 1; + token822_reverse(addr); + return 1; } struct qmail qqt; @@ -67,37 +99,41 @@ void finishheader() char *qqx; if (!flagreceipt) die_noreceipt(); - if (str_equal(returnpath,"")) die_noreceipt(); - if (str_equal(returnpath,"#@[]")) die_noreceipt(); + if (str_equal(returnpath, "")) die_noreceipt(); + if (str_equal(returnpath, "#@[]")) die_noreceipt(); - if (!quote2("ed,returnpath)) die_nomem(); + if (!quote2("ed, returnpath)) die_nomem(); if (qmail_open(&qqt) == -1) die_fork(); - qmail_puts(&qqt,"From: DELIVERY NOTICE SYSTEM <"); - qmail_put(&qqt,quoted.s,quoted.len); - qmail_puts(&qqt,">\n"); - qmail_puts(&qqt,"To: <"); - qmail_put(&qqt,quoted.s,quoted.len); - qmail_puts(&qqt,">\n"); - qmail_puts(&qqt,"Subject: success notice\n\ + qmail_puts(&qqt, "From: DELIVERY NOTICE SYSTEM <"); + qmail_put(&qqt, quoted.s, quoted.len); + qmail_puts(&qqt, ">\n"); + qmail_puts(&qqt, "To: <"); + qmail_put(&qqt, quoted.s, quoted.len); + qmail_puts(&qqt, ">\n"); + qmail_puts( + &qqt, + "Subject: success notice\n\ \n\ Hi! This is the qreceipt program. Your message was delivered to the\n\ following address: "); - qmail_puts(&qqt,target); - qmail_puts(&qqt,". Thanks for asking.\n"); + qmail_puts(&qqt, target); + qmail_puts(&qqt, ". Thanks for asking.\n"); if (messageid.s) { - qmail_puts(&qqt,"Your "); - qmail_put(&qqt,messageid.s,messageid.len); + qmail_puts(&qqt, "Your "); + qmail_put(&qqt, messageid.s, messageid.len); } - qmail_from(&qqt,""); - qmail_to(&qqt,returnpath); + qmail_from(&qqt, ""); + qmail_to(&qqt, returnpath); qqx = qmail_close(&qqt); if (*qqx) - if (*qqx == 'D') die_qqperm(); - else die_qqtemp(); + if (*qqx == 'D') + die_qqperm(); + else + die_qqtemp(); } stralloc hfbuf = {0}; @@ -107,24 +143,27 @@ token822_alloc hfaddr = {0}; void doheaderfield(stralloc *h) { - switch (hfield_known(h->s,h->len)) { + switch (hfield_known(h->s, h->len)) { case H_MESSAGEID: - if (!stralloc_copy(&messageid,h)) die_nomem(); + if (!stralloc_copy(&messageid, h)) die_nomem(); break; case H_NOTICEREQUESTEDUPONDELIVERYTO: - doordie(h,token822_parse(&hfin,h,&hfbuf)); - doordie(h,token822_addrlist(&hfrewrite,&hfaddr,&hfin,rwnotice)); + doordie(h, token822_parse(&hfin, h, &hfbuf)); + doordie(h, token822_addrlist(&hfrewrite, &hfaddr, &hfin, rwnotice)); break; } } -void dobody(stralloc *h) { ; } +void dobody(stralloc *h) +{ + ; +} int main(int argc, char **argv) { sig_pipeignore(); if (!(target = argv[1])) die_usage(); if (!(returnpath = env_get("SENDER"))) die_usage(); - if (headerbody(buffer_0,doheaderfield,finishheader,dobody) == -1) die_read(); + if (headerbody(buffer_0, doheaderfield, finishheader, dobody) == -1) die_read(); die_noreceipt(); } diff --git a/src/qsutil.c b/src/qsutil.c index 9c438ea..1d3c9c3 100644 --- a/src/qsutil.c +++ b/src/qsutil.c @@ -1,64 +1,66 @@ +#include "qsutil.h" + #include <unistd.h> -#include "stralloc.h" + #include "buffer.h" -#include "qsutil.h" +#include "stralloc.h" static stralloc foo = {0}; static char errbuf[1]; -static struct buffer be = BUFFER_INIT(write,0,errbuf,1); +static struct buffer be = BUFFER_INIT(write, 0, errbuf, 1); -void logsa(stralloc *sa) +void logsa(stralloc *sa) { - buffer_putflush(&be,sa->s,sa->len); + buffer_putflush(&be, sa->s, sa->len); } -void log1s(char *s1) +void log1s(char *s1) { - buffer_putsflush(&be,s1); + buffer_putsflush(&be, s1); } -void log2s(char *s1,char *s2) +void log2s(char *s1, char *s2) { - buffer_putsflush(&be,s1); - buffer_putsflush(&be,s2); + buffer_putsflush(&be, s1); + buffer_putsflush(&be, s2); } -void log3s(char *s1,char *s2,char *s3) +void log3s(char *s1, char *s2, char *s3) { - buffer_putsflush(&be,s1); - buffer_putsflush(&be,s2); - buffer_putsflush(&be,s3); + buffer_putsflush(&be, s1); + buffer_putsflush(&be, s2); + buffer_putsflush(&be, s3); } -void log4s(char *s1,char *s2,char *s3,char *s4) +void log4s(char *s1, char *s2, char *s3, char *s4) { - buffer_putsflush(&be,s1); - buffer_putsflush(&be,s2); - buffer_putsflush(&be,s3); - buffer_putsflush(&be,s4); + buffer_putsflush(&be, s1); + buffer_putsflush(&be, s2); + buffer_putsflush(&be, s3); + buffer_putsflush(&be, s4); } -void log5s(char *s1,char *s2,char *s3,char *s4,char *s5) +void log5s(char *s1, char *s2, char *s3, char *s4, char *s5) { - buffer_putsflush(&be,s1); - buffer_putsflush(&be,s2); - buffer_putsflush(&be,s3); - buffer_putsflush(&be,s4); - buffer_putsflush(&be,s5); + buffer_putsflush(&be, s1); + buffer_putsflush(&be, s2); + buffer_putsflush(&be, s3); + buffer_putsflush(&be, s4); + buffer_putsflush(&be, s5); } -void nomem() -{ - log1s("alert: out of memory, sleeping...\n"); - sleep(10); +void nomem() +{ + log1s("alert: out of memory, sleeping...\n"); + sleep(10); } void pausedir(char *dir) -{ - log3s("alert: unable to opendir ",dir,", sleeping...\n"); - sleep(10); +{ + log3s("alert: unable to opendir ", dir, ", sleeping...\n"); + sleep(10); } int issafe(char ch) @@ -73,13 +75,12 @@ void logsafe(char *s) { int i; - while (!stralloc_copys(&foo,s)) nomem(); + while (!stralloc_copys(&foo, s)) nomem(); for (i = 0; i < foo.len; ++i) if (foo.s[i] == '\n') foo.s[i] = '/'; - else - if (!issafe(foo.s[i])) - foo.s[i] = '_'; + else if (!issafe(foo.s[i])) + foo.s[i] = '_'; logsa(&foo); } diff --git a/src/quote.c b/src/quote.c index ef1bf45..0a3efc3 100644 --- a/src/quote.c +++ b/src/quote.c @@ -1,7 +1,8 @@ -#include "stralloc.h" -#include "str.h" #include "quote.h" +#include "str.h" +#include "stralloc.h" + /* quote() encodes a box as per rfc 821 and rfc 822, while trying to do as little quoting as possible. @@ -10,26 +11,24 @@ no special encoding here for bytes above 127. */ static char ok[128] = { - 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 -,0,7,0,7,7,7,7,7,0,0,7,7,0,7,7,7 ,7,7,7,7,7,7,7,7,7,7,0,0,0,7,0,7 -,0,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7 ,7,7,7,7,7,7,7,7,7,7,7,0,0,0,7,7 -,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7 ,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,0 -} ; + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 7, 0, 7, 7, 7, 7, 7, 0, 0, 7, 7, 0, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 0, 0, 0, 7, 0, 7, + 0, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 0, 0, 0, 7, 7, + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 0}; -static int doit(stralloc *saout,stralloc *sain) +static int doit(stralloc *saout, stralloc *sain) { char ch; int i; int j; - if (!stralloc_ready(saout,sain->len * 2 + 2)) return 0; + if (!stralloc_ready(saout, sain->len * 2 + 2)) return 0; j = 0; saout->s[j++] = '"'; for (i = 0; i < sain->len; ++i) { ch = sain->s[i]; - if ((ch == '\r') || (ch == '\n') || (ch == '"') || (ch == '\\')) - saout->s[j++] = '\\'; + if ((ch == '\r') || (ch == '\n') || (ch == '"') || (ch == '\\')) saout->s[j++] = '\\'; saout->s[j++] = ch; } saout->s[j++] = '"'; @@ -38,7 +37,7 @@ static int doit(stralloc *saout,stralloc *sain) return 1; } -int quote_need(char *s,unsigned int n) +int quote_need(char *s, unsigned int n) { unsigned char uch; int i; @@ -52,30 +51,30 @@ int quote_need(char *s,unsigned int n) if (s[0] == '.') return 1; if (s[n - 1] == '.') return 1; - for (i = 0; i < n - 1; ++i) - if (s[i] == '.') + for (i = 0; i < n - 1; ++i) + if (s[i] == '.') if (s[i + 1] == '.') return 1; return 0; } -int quote(stralloc *saout,stralloc *sain) +int quote(stralloc *saout, stralloc *sain) { - if (quote_need(sain->s,sain->len)) return doit(saout,sain); - return stralloc_copy(saout,sain); + if (quote_need(sain->s, sain->len)) return doit(saout, sain); + return stralloc_copy(saout, sain); } static stralloc foo = {0}; -int quote2(stralloc *sa,char *s) +int quote2(stralloc *sa, char *s) { int j; - if (!*s) return stralloc_copys(sa,s); - j = str_rchr(s,'@'); - if (!stralloc_copys(&foo,s)) return 0; - if (!s[j]) return quote(sa,&foo); + if (!*s) return stralloc_copys(sa, s); + j = str_rchr(s, '@'); + if (!stralloc_copys(&foo, s)) return 0; + if (!s[j]) return quote(sa, &foo); foo.len = j; - if (!quote(sa,&foo)) return 0; + if (!quote(sa, &foo)) return 0; - return stralloc_cats(sa,s + j); + return stralloc_cats(sa, s + j); } diff --git a/src/rcpthosts.c b/src/rcpthosts.c index 2242249..f69f4f9 100644 --- a/src/rcpthosts.c +++ b/src/rcpthosts.c @@ -1,15 +1,17 @@ -#include "cdbread.h" +#include "rcpthosts.h" + #include "byte.h" -#include "open.h" -#include "error.h" -#include "exit.h" -#include "control.h" -#include "constmap.h" -#include "stralloc.h" #include "case.h" +#include "cdbread.h" #include "close.h" +#include "constmap.h" +#include "error.h" +#include "exit.h" #include "fd.h" -#include "rcpthosts.h" +#include "open.h" +#include "stralloc.h" + +#include "control.h" static int flagrh = 0; static int flagmrh = 0; @@ -21,11 +23,12 @@ static struct cdb cdb; int rcpthosts_init() { - flagrh = control_readfile(&rh,"control/rcpthosts",0); + flagrh = control_readfile(&rh, "control/rcpthosts", 0); if (flagrh != 1) return flagrh; - if (!constmap_init(&maprh,rh.s,rh.len,0)) return flagrh = -1; + if (!constmap_init(&maprh, rh.s, rh.len, 0)) return flagrh = -1; fdmrh = open_read("control/morercpthosts.cdb"); - if (fdmrh == -1) if (errno != ENOENT) return flagmrh = -1; + if (fdmrh == -1) + if (errno != ENOENT) return flagmrh = -1; if (fdmrh > 0) flagmrh = 1; return 0; } @@ -39,28 +42,35 @@ int rcpthosts(char *buf, int len) if (flagrh != 1) return 1; - j = byte_rchr(buf,len,'@'); + j = byte_rchr(buf, len, '@'); if (j >= len) return 1; /* presumably envnoathost is acceptable */ - ++j; buf += j; len -= j; + ++j; + buf += j; + len -= j; - if (!stralloc_copyb(&host,buf,len)) return -1; + if (!stralloc_copyb(&host, buf, len)) return -1; buf = host.s; - case_lowerb(buf,len); + case_lowerb(buf, len); for (j = 0; j < len; ++j) if (!j || (buf[j] == '.')) - if (constmap(&maprh,buf + j,len - j)) return 1; + if (constmap(&maprh, buf + j, len - j)) return 1; if (flagmrh == 1) { fdmrh = open_read("control/morercpthosts.cdb"); - if (fdmrh == -1) if (errno == ENOENT) return 0; - cdb_init(&cdb,fdmrh); + if (fdmrh == -1) + if (errno == ENOENT) return 0; + cdb_init(&cdb, fdmrh); - for (j = 0; j < len ;++j) + for (j = 0; j < len; ++j) if (!j || (buf[j] == '.')) { - r = cdb_find(&cdb,buf + j,len - j); - if (r) { cdb_free(&cdb); close(fdmrh); return r; } + r = cdb_find(&cdb, buf + j, len - j); + if (r) { + cdb_free(&cdb); + close(fdmrh); + return r; + } } cdb_free(&cdb); close(fdmrh); diff --git a/src/readsubdir.c b/src/readsubdir.c index 754aa36..72b83e6 100644 --- a/src/readsubdir.c +++ b/src/readsubdir.c @@ -1,7 +1,9 @@ #include "readsubdir.h" + #include "fmt.h" #include "scan.h" #include "str.h" + #include "auto_split.h" void readsubdir_init(readsubdir *rs, char *name, void (*pause)()) @@ -21,11 +23,14 @@ int readsubdir_next(readsubdir *rs, unsigned long *id) if (!rs->dir) { if (rs->pos >= auto_split) return 0; - if (str_len(rs->name) > READSUBDIR_NAMELEN) { rs->pos++; return -1; } + if (str_len(rs->name) > READSUBDIR_NAMELEN) { + rs->pos++; + return -1; + } len = 0; - len += fmt_str(namepos + len,rs->name); + len += fmt_str(namepos + len, rs->name); namepos[len++] = '/'; - len += fmt_ulong(namepos + len,(unsigned long) rs->pos); + len += fmt_ulong(namepos + len, (unsigned long)rs->pos); namepos[len] = 0; while (!(rs->dir = opendir(namepos))) rs->pause(namepos); rs->pos++; @@ -33,11 +38,15 @@ int readsubdir_next(readsubdir *rs, unsigned long *id) } d = readdir(rs->dir); - if (!d) { closedir(rs->dir); rs->dir = 0; return -1; } + if (!d) { + closedir(rs->dir); + rs->dir = 0; + return -1; + } - if (str_equal(d->d_name,".")) return -1; - if (str_equal(d->d_name,"..")) return -1; - len = scan_ulong(d->d_name,id); + if (str_equal(d->d_name, ".")) return -1; + if (str_equal(d->d_name, "..")) return -1; + len = scan_ulong(d->d_name, id); if (!len || d->d_name[len]) return -2; return 1; diff --git a/src/received.c b/src/received.c index 51339a9..b564fd4 100644 --- a/src/received.c +++ b/src/received.c @@ -1,17 +1,18 @@ -#include "fmt.h" -#include "qmail.h" -#include "now.h" -#include "datetime.h" -#include "date822fmt.h" #include "received.h" -#include "str.h" -#include "stralloc.h" + #include "byte.h" +#include "fmt.h" #include "str.h" +#include "stralloc.h" + +#include "date822fmt.h" +#include "datetime.h" +#include "now.h" +#include "qmail.h" static int issafe(char ch) { - if (ch == ' ') return 1; /* accept empty spaces */ + if (ch == ' ') return 1; /* accept empty spaces */ if (ch == '.') return 1; if (ch == '@') return 1; if (ch == '%') return 1; @@ -26,12 +27,12 @@ static int issafe(char ch) return 0; } -void safeput(struct qmail *qqt,char *s) +void safeput(struct qmail *qqt, char *s) { char ch; while ((ch = *s++)) { if (!issafe(ch)) ch = '?'; - qmail_put(qqt,&ch,1); + qmail_put(qqt, &ch, 1); } } @@ -42,44 +43,53 @@ static char buf[DATE822FMT]; /* " by silverton.berkeley.edu with [UTF8][E]SMTP[SA]; 26 Sep 1995 04:46:54 -0000" */ /* "X-RBL-Info: http://www.spamhaus.org/query/bl?ip=127.0.0.2 */ -void received(struct qmail *qqt,char *protocol,char *local,char *remoteip,char *remotehost,char *remoteinfo,char *helo,char *tlsinfo,char *rblinfo) +void received( + struct qmail *qqt, + char *protocol, + char *local, + char *remoteip, + char *remotehost, + char *remoteinfo, + char *helo, + char *tlsinfo, + char *rblinfo) { struct datetime dt; int i; - qmail_puts(qqt,"Received: from "); - safeput(qqt,remotehost); + qmail_puts(qqt, "Received: from "); + safeput(qqt, remotehost); if (helo) { - qmail_puts(qqt," (HELO "); - safeput(qqt,helo); - qmail_puts(qqt,")"); + qmail_puts(qqt, " (HELO "); + safeput(qqt, helo); + qmail_puts(qqt, ")"); } - qmail_puts(qqt," ("); + qmail_puts(qqt, " ("); if (remoteinfo) { - safeput(qqt,remoteinfo); - qmail_puts(qqt,"@"); + safeput(qqt, remoteinfo); + qmail_puts(qqt, "@"); } - safeput(qqt,remoteip); - qmail_puts(qqt,")"); + safeput(qqt, remoteip); + qmail_puts(qqt, ")"); if (tlsinfo) { - qmail_puts(qqt,"\n de/crypted with "); - qmail_puts(qqt,tlsinfo); + qmail_puts(qqt, "\n de/crypted with "); + qmail_puts(qqt, tlsinfo); } - qmail_puts(qqt,"\n by "); - safeput(qqt,local); - qmail_puts(qqt," with "); - qmail_puts(qqt,protocol); - qmail_puts(qqt,"; "); - datetime_tai(&dt,now()); - qmail_put(qqt,buf,date822fmt(buf,&dt)); + qmail_puts(qqt, "\n by "); + safeput(qqt, local); + qmail_puts(qqt, " with "); + qmail_puts(qqt, protocol); + qmail_puts(qqt, "; "); + datetime_tai(&dt, now()); + qmail_put(qqt, buf, date822fmt(buf, &dt)); if (rblinfo) { - i = str_chr(rblinfo,']'); - if (rblinfo[i] == ']') { - qmail_puts(qqt,"X-RBL-Info: "); - safeput(qqt,rblinfo + i + 2); - qmail_puts(qqt,"\n"); + i = str_chr(rblinfo, ']'); + if (rblinfo[i] == ']') { + qmail_puts(qqt, "X-RBL-Info: "); + safeput(qqt, rblinfo + i + 2); + qmail_puts(qqt, "\n"); } } } @@ -88,7 +98,8 @@ void received(struct qmail *qqt,char *protocol,char *local,char *remoteip,char * /* " designates Clientip as permitted sender) receiver=Hostname " */ /* " client-ip=Clientip; envelope-from=Mailfrom; " */ -void spfheader(struct qmail *qqt,char *spfinfo,char *local,char *remoteip,char *helohost,char *mailfrom) +void spfheader( + struct qmail *qqt, char *spfinfo, char *local, char *remoteip, char *helohost, char *mailfrom) { char *result = 0; char *identity = 0; @@ -105,21 +116,23 @@ void spfheader(struct qmail *qqt,char *spfinfo,char *local,char *remoteip,char * if (!len) return; for (i = 0; i < len; i++) - if (spfinfo[i] == ' ') spfinfo[i] = '\0'; - + if (spfinfo[i] == ' ') spfinfo[i] = '\0'; + for (i = 0; i < len; i++) { if (spfinfo[i] == '\0') { - switch (spfinfo[i + 1]) { + switch (spfinfo[i + 1]) { case 'S': clientip = spfinfo + i + 3; break; case 'O': envelopefrom = spfinfo + i + 3; break; case 'C': identity = spfinfo + i + 3; break; case 'H': helo = spfinfo + i + 3; break; case 'T': receiver = spfinfo + i + 3; break; case 'P': problem = spfinfo + i + 3; break; - case 'M': if ((j = str_chr(spfinfo + i,'='))) spfinfo[i + j] = '\0'; - mechanism = spfinfo + i + 1; break; + case 'M': + if ((j = str_chr(spfinfo + i, '='))) spfinfo[i + j] = '\0'; + mechanism = spfinfo + i + 1; + break; case 'R': result = spfinfo + i + 3; break; - default: break; + default: break; } } } @@ -132,41 +145,102 @@ void spfheader(struct qmail *qqt,char *spfinfo,char *local,char *remoteip,char * if (!problem || *problem == 0) problem = "unknown"; if (!mechanism || *mechanism == 0) mechanism = "unknown"; if (!identity || *identity == 0) { - j = str_rchr(envelopefrom,'@'); - if (envelopefrom[j] == '@') identity = envelopefrom + j + 1; - else identity = "unknown"; + j = str_rchr(envelopefrom, '@'); + if (envelopefrom[j] == '@') + identity = envelopefrom + j + 1; + else + identity = "unknown"; } - qmail_puts(qqt,"Received-SPF: "); + qmail_puts(qqt, "Received-SPF: "); switch (*result) { - case '+': qmail_puts(qqt," pass ("); safeput(qqt,helo); - qmail_puts(qqt,": domain of "); safeput(qqt,identity); qmail_puts(qqt,"\n"); - qmail_puts(qqt," designates "); safeput(qqt,clientip); qmail_puts(qqt," as permitted sender)\n"); - qmail_puts(qqt," receiver="); safeput(qqt,receiver); - qmail_puts(qqt,"; client-ip="); safeput(qqt,clientip); qmail_puts(qqt,"\n"); - qmail_puts(qqt," envelope-from="); safeput(qqt,envelopefrom); qmail_puts(qqt,";\n"); break; - case '-': qmail_puts(qqt," fail ("); safeput(qqt,helo); - qmail_puts(qqt,": domain of "); safeput(qqt,identity); qmail_puts(qqt,"\n"); - qmail_puts(qqt," does not designate "); safeput(qqt,clientip); qmail_puts(qqt," as permitted sender)\n"); break; - case '~': qmail_puts(qqt," softfail ("); safeput(qqt,helo); - qmail_puts(qqt,": domain of transitioning "); safeput(qqt,identity); qmail_puts(qqt,"\n"); - qmail_puts(qqt," does not designate "); safeput(qqt,clientip); qmail_puts(qqt," as permitted sender)\n"); break; - case '?': qmail_puts(qqt," neutral ("); safeput(qqt,helo); qmail_puts(qqt,"; client-ip="); safeput(qqt,clientip); - qmail_puts(qqt," is neither permitted \n"); qmail_puts(qqt," nor denied by domain of "); safeput(qqt,identity), - qmail_puts(qqt,")\n"); break; - case 'o': qmail_puts(qqt," none ("); safeput(qqt,helo); - qmail_puts(qqt,": domain of "); safeput(qqt,identity); qmail_puts(qqt," does\n"); - qmail_puts(qqt," not designate permitted sender hosts)\n"); break; - case 't': qmail_puts(qqt," temperror ("); safeput(qqt,helo); - qmail_puts(qqt,": domain of "); safeput(qqt,identity); qmail_puts(qqt," evaluated\n"); - qmail_puts(qqt," with error: "); safeput(qqt,problem); qmail_puts(qqt," for mechanism: "); safeput(qqt,mechanism); - qmail_puts(qqt,")\n"); break; - case 'e': qmail_puts(qqt," permerror ("); safeput(qqt,helo); - qmail_puts(qqt,": domain of "); safeput(qqt,identity); qmail_puts(qqt," evaluated\n"); - qmail_puts(qqt," with error: "); safeput(qqt,problem); qmail_puts(qqt," for mechanism: "); safeput(qqt,mechanism); - qmail_puts(qqt,")\n"); break; - default: qmail_puts(qqt," unknown (results for "); safeput(qqt,helo); - qmail_puts(qqt,": domain of "); safeput(qqt,identity); - qmail_puts(qqt," follow an unknown mechanism: "); safeput(qqt,mechanism); qmail_puts(qqt,")\n"); break; + case '+': + qmail_puts(qqt, " pass ("); + safeput(qqt, helo); + qmail_puts(qqt, ": domain of "); + safeput(qqt, identity); + qmail_puts(qqt, "\n"); + qmail_puts(qqt, " designates "); + safeput(qqt, clientip); + qmail_puts(qqt, " as permitted sender)\n"); + qmail_puts(qqt, " receiver="); + safeput(qqt, receiver); + qmail_puts(qqt, "; client-ip="); + safeput(qqt, clientip); + qmail_puts(qqt, "\n"); + qmail_puts(qqt, " envelope-from="); + safeput(qqt, envelopefrom); + qmail_puts(qqt, ";\n"); + break; + case '-': + qmail_puts(qqt, " fail ("); + safeput(qqt, helo); + qmail_puts(qqt, ": domain of "); + safeput(qqt, identity); + qmail_puts(qqt, "\n"); + qmail_puts(qqt, " does not designate "); + safeput(qqt, clientip); + qmail_puts(qqt, " as permitted sender)\n"); + break; + case '~': + qmail_puts(qqt, " softfail ("); + safeput(qqt, helo); + qmail_puts(qqt, ": domain of transitioning "); + safeput(qqt, identity); + qmail_puts(qqt, "\n"); + qmail_puts(qqt, " does not designate "); + safeput(qqt, clientip); + qmail_puts(qqt, " as permitted sender)\n"); + break; + case '?': + qmail_puts(qqt, " neutral ("); + safeput(qqt, helo); + qmail_puts(qqt, "; client-ip="); + safeput(qqt, clientip); + qmail_puts(qqt, " is neither permitted \n"); + qmail_puts(qqt, " nor denied by domain of "); + safeput(qqt, identity), qmail_puts(qqt, ")\n"); + break; + case 'o': + qmail_puts(qqt, " none ("); + safeput(qqt, helo); + qmail_puts(qqt, ": domain of "); + safeput(qqt, identity); + qmail_puts(qqt, " does\n"); + qmail_puts(qqt, " not designate permitted sender hosts)\n"); + break; + case 't': + qmail_puts(qqt, " temperror ("); + safeput(qqt, helo); + qmail_puts(qqt, ": domain of "); + safeput(qqt, identity); + qmail_puts(qqt, " evaluated\n"); + qmail_puts(qqt, " with error: "); + safeput(qqt, problem); + qmail_puts(qqt, " for mechanism: "); + safeput(qqt, mechanism); + qmail_puts(qqt, ")\n"); + break; + case 'e': + qmail_puts(qqt, " permerror ("); + safeput(qqt, helo); + qmail_puts(qqt, ": domain of "); + safeput(qqt, identity); + qmail_puts(qqt, " evaluated\n"); + qmail_puts(qqt, " with error: "); + safeput(qqt, problem); + qmail_puts(qqt, " for mechanism: "); + safeput(qqt, mechanism); + qmail_puts(qqt, ")\n"); + break; + default: + qmail_puts(qqt, " unknown (results for "); + safeput(qqt, helo); + qmail_puts(qqt, ": domain of "); + safeput(qqt, identity); + qmail_puts(qqt, " follow an unknown mechanism: "); + safeput(qqt, mechanism); + qmail_puts(qqt, ")\n"); + break; } } diff --git a/src/recipients.c b/src/recipients.c index ce29e19..bf70fd7 100644 --- a/src/recipients.c +++ b/src/recipients.c @@ -1,18 +1,21 @@ +#include "recipients.h" + #include <unistd.h> -#include "cdbread.h" + +#include "buffer.h" #include "byte.h" -#include "open.h" -#include "control.h" +#include "case.h" +#include "cdbread.h" #include "constmap.h" -#include "stralloc.h" -#include "recipients.h" -#include "wait.h" -#include "str.h" #include "fd.h" +#include "open.h" #include "sig.h" -#include "case.h" -#include "buffer.h" +#include "str.h" +#include "stralloc.h" +#include "wait.h" + #include "auto_break.h" +#include "control.h" #include "qmail.h" #define FDAUTH 3 @@ -47,21 +50,21 @@ static struct cdb cdb; int recipients_init() { - flagrcpts = control_readfile(&rcptline,"control/recipients",0); + flagrcpts = control_readfile(&rcptline, "control/recipients", 0); if (flagrcpts != 1) return flagrcpts; return 0; } char rcptbuf[512]; -buffer br = BUFFER_INIT(safewrite,FDAUTH,rcptbuf,sizeof(rcptbuf)); +buffer br = BUFFER_INIT(safewrite, FDAUTH, rcptbuf, sizeof(rcptbuf)); -int callapam(char *pam,char *addr) +int callapam(char *pam, char *addr) { int i; - int j=0; + int j = 0; int wstat; int pi[2]; - int child; + int child; char ch; static stralloc mailaddress = {0}; @@ -74,73 +77,82 @@ int callapam(char *pam,char *addr) stralloc pamarg4 = {0}; stralloc pamarg5 = {0}; - for (i = 0; (ch = pam[i]); i++) { + for (i = 0; (ch = pam[i]); i++) { if (j < 6) { - if (ch != ' ') - if (!stralloc_append(&pamarg,&ch)) return -2; + if (ch != ' ') + if (!stralloc_append(&pamarg, &ch)) return -2; if (ch == ' ' || ch == '\n' || i == str_len(pam) - 1) { if (!stralloc_0(&pamarg)) return -2; switch (j) { case 0: - if (!stralloc_copy(&pamname,&pamarg)) return -2; + if (!stralloc_copy(&pamname, &pamarg)) return -2; childargs[0] = pamname.s; case 1: - if (!stralloc_copy(&pamarg1,&pamarg)) return -2; + if (!stralloc_copy(&pamarg1, &pamarg)) return -2; childargs[1] = pamarg1.s; case 2: - if (!stralloc_copy(&pamarg2,&pamarg)) return -2; + if (!stralloc_copy(&pamarg2, &pamarg)) return -2; childargs[2] = pamarg2.s; case 3: - if (!stralloc_copy(&pamarg3,&pamarg)) return -2; - childargs[3] = pamarg3.s; + if (!stralloc_copy(&pamarg3, &pamarg)) return -2; + childargs[3] = pamarg3.s; case 4: - if (!stralloc_copy(&pamarg4,&pamarg)) return -2; - childargs[4] = pamarg4.s; + if (!stralloc_copy(&pamarg4, &pamarg)) return -2; + childargs[4] = pamarg4.s; case 5: - if (!stralloc_copy(&pamarg5,&pamarg)) return -2; - childargs[5] = pamarg5.s; + if (!stralloc_copy(&pamarg5, &pamarg)) return -2; + childargs[5] = pamarg5.s; } j++; - if (!stralloc_copys(&pamarg,"")) return -2; - } + if (!stralloc_copys(&pamarg, "")) return -2; + } } } - childargs[j] = 0; + childargs[j] = 0; close(FDAUTH); if (pipe(pi) == -1) return -3; if (pi[0] != FDAUTH) return -3; switch (child = fork()) { - case -1: - return -3; + case -1: return -3; case 0: close(pi[1]); - if (fd_copy(FDAUTH,pi[0]) == -1) return -3; + if (fd_copy(FDAUTH, pi[0]) == -1) return -3; sig_pipedefault(); - execvp(childargs[0],childargs); + execvp(childargs[0], childargs); return 111; } close(pi[0]); -/* checkpassword compliant form: address\0\0\0 */ - - if (!stralloc_copys(&mailaddress,addr)) return -2; + /* checkpassword compliant form: address\0\0\0 */ + + if (!stralloc_copys(&mailaddress, addr)) return -2; if (!stralloc_0(&mailaddress)) return -2; if (!stralloc_0(&mailaddress)) return -2; if (!stralloc_0(&mailaddress)) return -2; - buffer_init(&br,write,pi[1],rcptbuf,sizeof(rcptbuf)); - if (buffer_put(&br,mailaddress.s,mailaddress.len) == -1) return -3; + buffer_init(&br, write, pi[1], rcptbuf, sizeof(rcptbuf)); + if (buffer_put(&br, mailaddress.s, mailaddress.len) == -1) return -3; if (buffer_flush(&br) == -1) return -3; close(pi[1]); - - if (wait_pid(&wstat,child) == -1) return -3; + + if (wait_pid(&wstat, child) == -1) return -3; if (wait_crashed(wstat)) return -3; return wait_exitcode(wstat); } -int recipients_parse(char *rhost,int rlen,char *addr,char *rkey,int klen,char *vaddr,char *vkey,int vlen,char *ukey,int ulen) +int recipients_parse( + char *rhost, + int rlen, + char *addr, + char *rkey, + int klen, + char *vaddr, + char *vkey, + int vlen, + char *ukey, + int ulen) { int i; int r; @@ -150,92 +162,89 @@ int recipients_parse(char *rhost,int rlen,char *addr,char *rkey,int klen,char *v static stralloc line = {0}; int seenhost = 0; - if (!stralloc_copys(&line,"")) return -2; - if (!stralloc_copys(&wildhost,"!")) return -2; - if (!stralloc_cats(&wildhost,rhost)) return -2; + if (!stralloc_copys(&line, "")) return -2; + if (!stralloc_copys(&wildhost, "!")) return -2; + if (!stralloc_cats(&wildhost, rhost)) return -2; if (!stralloc_0(&wildhost)) return -2; - - for (i = 0; i < rcptline.len; ++i) { - if (!stralloc_append(&line,&rcptline.s[i])) return -2; + + for (i = 0; i < rcptline.len; ++i) { + if (!stralloc_append(&line, &rcptline.s[i])) return -2; if (rcptline.s[i] == '\0') { if (!stralloc_0(&line)) return -2; - j = byte_chr(line.s,line.len,':'); /* cdb */ - k = byte_chr(line.s,line.len,'|'); /* pam */ - u = byte_chr(line.s,line.len,'='); /* assign users */ + j = byte_chr(line.s, line.len, ':'); /* cdb */ + k = byte_chr(line.s, line.len, '|'); /* pam */ + u = byte_chr(line.s, line.len, '='); /* assign users */ - if (!str_diffn(line.s,wildhost.s,wildhost.len - 1)) return 4; /* wilddomain */ + if (!str_diffn(line.s, wildhost.s, wildhost.len - 1)) return 4; /* wilddomain */ if ((j && j < line.len) || (k && k < line.len) || (u && u < line.len)) - if (!str_diffn(line.s,"@",1)) /* exact */ - if (!str_diffn(line.s + 1,rhost,rlen - 1)) seenhost = 1; + if (!str_diffn(line.s, "@", 1)) /* exact */ + if (!str_diffn(line.s + 1, rhost, rlen - 1)) seenhost = 1; - if (!seenhost) { /* domain */ + if (!seenhost) { /* domain */ if (j && rlen >= j) - if (!str_diffn(line.s,rhost + rlen - j - 1,j - 1)) seenhost = 2; + if (!str_diffn(line.s, rhost + rlen - j - 1, j - 1)) seenhost = 2; if (k && rlen >= k) - if (!str_diffn(line.s,rhost + rlen - k - 1,k - 1)) seenhost = 3; + if (!str_diffn(line.s, rhost + rlen - k - 1, k - 1)) seenhost = 3; if (u && rlen >= u) - if (!str_diffn(line.s,rhost + rlen - u - 1,u - 1)) seenhost = 4; + if (!str_diffn(line.s, rhost + rlen - u - 1, u - 1)) seenhost = 4; } - if (!seenhost) /* pass-thru */ - if (!str_diffn(line.s,"!*",2)) return 5; - - if (k && k < line.len) /* pam */ - if (seenhost || !str_diffn(line.s,"*",1)) { - r = callapam(line.s + k + 1,addr); - if (vlen > 0 && r != 0) - r = callapam(line.s + k + 1,vaddr); + if (!seenhost) /* pass-thru */ + if (!str_diffn(line.s, "!*", 2)) return 5; + + if (k && k < line.len) /* pam */ + if (seenhost || !str_diffn(line.s, "*", 1)) { + r = callapam(line.s + k + 1, addr); + if (vlen > 0 && r != 0) r = callapam(line.s + k + 1, vaddr); if (r == 0) return 2; if (r == 111) return r; } - if (u && u < line.len) /* qmail-users */ - if (seenhost || !str_diffn(line.s,"*",1)) { + if (u && u < line.len) /* qmail-users */ + if (seenhost || !str_diffn(line.s, "*", 1)) { fdrcps = open_read("users/assign.cdb"); if (fdrcps != -1) { - cdb_init(&cdb,fdrcps); - r = cdb_find(&cdb,ukey,ulen - 1); + cdb_init(&cdb, fdrcps); + r = cdb_find(&cdb, ukey, ulen - 1); cdb_free(&cdb); close(fdrcps); if (r) return 3; - } + } } - - if (j && j < line.len) /* cdb */ - if (seenhost || !str_diffn(line.s,"*",1)) { + + if (j && j < line.len) /* cdb */ + if (seenhost || !str_diffn(line.s, "*", 1)) { fdrcps = open_read(line.s + j + 1); if (fdrcps != -1) { - cdb_init(&cdb,fdrcps); - r = cdb_find(&cdb,rkey,klen - 2); - if (vlen > 0 && r == 0) - r = cdb_find(&cdb,vkey,vlen - 2); + cdb_init(&cdb, fdrcps); + r = cdb_find(&cdb, rkey, klen - 2); + if (vlen > 0 && r == 0) r = cdb_find(&cdb, vkey, vlen - 2); cdb_free(&cdb); close(fdrcps); if (r) return 1; - } + } } - + if (!seenhost) { - fdrcps = open_read(line.s); /* legacy cdb */ + fdrcps = open_read(line.s); /* legacy cdb */ if (fdrcps != -1) { - cdb_init(&cdb,fdrcps); - r = cdb_find(&cdb,rkey,klen - 2); - if (vlen > 0 && r == 0) - r = cdb_find(&cdb,vkey,vlen - 2); + cdb_init(&cdb, fdrcps); + r = cdb_find(&cdb, rkey, klen - 2); + if (vlen > 0 && r == 0) r = cdb_find(&cdb, vkey, vlen - 2); cdb_free(&cdb); close(fdrcps); if (r) return 1; } } - if (!stralloc_copys(&line,"")) return -2; + if (!stralloc_copys(&line, "")) return -2; } } return 0; } -int recipients(char *buf,int len) +int recipients(char *buf, int len) { int at; int i; @@ -243,48 +252,49 @@ int recipients(char *buf,int len) if (flagrcpts != 1) return 10; - at = byte_rchr(buf,len,'@'); + at = byte_rchr(buf, len, '@'); if (at && at < len) { - if (!stralloc_copyb(&domain,buf + at + 1,len - at - 1)) return -2; - if (!stralloc_copyb(&address,buf,len)) return -2; + if (!stralloc_copyb(&domain, buf + at + 1, len - at - 1)) return -2; + if (!stralloc_copyb(&address, buf, len)) return -2; } else { - if (!stralloc_copyb(&address,buf,len)) return -2; - if (!stralloc_append(&address,"@")) return -2; - if (!stralloc_copys(&domain,"localhost")) return -2; - if (!stralloc_cat(&address,&domain)) return -2; + if (!stralloc_copyb(&address, buf, len)) return -2; + if (!stralloc_append(&address, "@")) return -2; + if (!stralloc_copys(&domain, "localhost")) return -2; + if (!stralloc_cat(&address, &domain)) return -2; } - if (!stralloc_copyb(&user,buf,at - 1)) return -2; + if (!stralloc_copyb(&user, buf, at - 1)) return -2; if (!stralloc_0(&user)) return -2; if (!stralloc_0(&address)) return -2; if (!stralloc_0(&domain)) return -2; - if (!stralloc_copys(&key,":")) return -2; - if (!stralloc_cat(&key,&address)) return -2; - if (!stralloc_0(&key)) return -2; /* \0\0 terminated */ - case_lowerb(key.s,key.len); - case_lowerb(domain.s,domain.len); + if (!stralloc_copys(&key, ":")) return -2; + if (!stralloc_cat(&key, &address)) return -2; + if (!stralloc_0(&key)) return -2; /* \0\0 terminated */ + case_lowerb(key.s, key.len); + case_lowerb(domain.s, domain.len); - if (!stralloc_copys(&ukey,"!")) return -2; - if (!stralloc_cat(&ukey,&user)) return -2; - if (!stralloc_0(&ukey)) return -2; /* \0 terminated */ - case_lowerb(ukey.s,ukey.len); + if (!stralloc_copys(&ukey, "!")) return -2; + if (!stralloc_cat(&ukey, &user)) return -2; + if (!stralloc_0(&ukey)) return -2; /* \0 terminated */ + case_lowerb(ukey.s, ukey.len); - for (i = 0; i < at; i++) { /* VERP addresses */ + for (i = 0; i < at; i++) { /* VERP addresses */ if (buf[i] == *auto_break || buf[i] == '=' || buf[i] == '+') { /* SRS delimiter */ - if (!stralloc_copyb(&verp,buf,i + 1)) return -2; - if (!stralloc_append(&verp,"@")) return -2; - if (!stralloc_cat(&verp,&domain)) return -2; - if (!stralloc_copys(&vkey,":")) return -2; - if (!stralloc_cat(&vkey,&verp)) return -2; - if (!stralloc_0(&vkey)) return -2; /* \0\0 terminated */ - case_lowerb(vkey.s,vkey.len); + if (!stralloc_copyb(&verp, buf, i + 1)) return -2; + if (!stralloc_append(&verp, "@")) return -2; + if (!stralloc_cat(&verp, &domain)) return -2; + if (!stralloc_copys(&vkey, ":")) return -2; + if (!stralloc_cat(&vkey, &verp)) return -2; + if (!stralloc_0(&vkey)) return -2; /* \0\0 terminated */ + case_lowerb(vkey.s, vkey.len); break; - } + } } - r = recipients_parse(domain.s,domain.len,address.s,key.s,key.len,verp.s,vkey.s,vkey.len,ukey.s,ukey.len); + r = recipients_parse( + domain.s, domain.len, address.s, key.s, key.len, verp.s, vkey.s, vkey.len, ukey.s, ukey.len); if (r) return r; return 0; -} +} diff --git a/src/sendmail.c b/src/sendmail.c index 69971e3..801c019 100644 --- a/src/sendmail.c +++ b/src/sendmail.c @@ -1,26 +1,28 @@ #include <unistd.h> -#include "getoptb.h" -#include "buffer.h" + #include "alloc.h" -#include "auto_qmail.h" -#include "exit.h" +#include "buffer.h" #include "env.h" -#include "str.h" +#include "exit.h" +#include "getoptb.h" #include "logmsg.h" +#include "str.h" + +#include "auto_qmail.h" #define WHO "sendmail" void nomem() { - logmsg(WHO,111,FATAL,"out of memory\n"); + logmsg(WHO, 111, FATAL, "out of memory\n"); } void die_usage() { - logmsg(WHO,100,USAGE,"sendmail [ -t ] [ -fsender ] [ -Fname ] [ -bp ] [ -bs ] [ arg ... ]\n"); + logmsg(WHO, 100, USAGE, "sendmail [ -t ] [ -fsender ] [ -Fname ] [ -bp ] [ -bs ] [ arg ... ]\n"); } -char *smtpdarg[] = { "bin/qmail-smtpd", 0 }; +char *smtpdarg[] = {"bin/qmail-smtpd", 0}; void smtpd() { @@ -36,15 +38,15 @@ void smtpd() if (!env_puts("TCPREMOTEHOST=localhost")) nomem(); if (!env_puts("TCPREMOTEINFO=sendmail-bs")) nomem(); } - execv(*smtpdarg,smtpdarg); - logmsg(WHO,111,FATAL,"unable to run qmail-smtpd\n"); + execv(*smtpdarg, smtpdarg); + logmsg(WHO, 111, FATAL, "unable to run qmail-smtpd\n"); } -char *qreadarg[] = { "bin/qmail-qread", 0 }; +char *qreadarg[] = {"bin/qmail-qread", 0}; void mailq() { - execv(*qreadarg,qreadarg); - logmsg(WHO,111,FATAL,"unable to run qmail-qread\n"); + execv(*qreadarg, qreadarg); + logmsg(WHO, 111, FATAL, "unable to run qmail-qread\n"); } void do_sender(const char *s) @@ -61,20 +63,18 @@ void do_sender(const char *s) env_unset("MAILHOST"); n = str_len(s); - a = str_rchr(s,'@'); - if (a == n) - { - env_put("QMAILUSER",s); + a = str_rchr(s, '@'); + if (a == n) { + env_put("QMAILUSER", s); return; } - env_put("QMAILHOST",s + a + 1); + env_put("QMAILHOST", s + a + 1); - x = (char *) alloc((a + 1) * sizeof(char)); + x = (char *)alloc((a + 1) * sizeof(char)); if (!x) nomem(); - for (i = 0; i < a; i++) - x[i] = s[i]; + for (i = 0; i < a; i++) x[i] = s[i]; x[i] = 0; - env_put("QMAILUSER",x); + env_put("QMAILUSER", x); alloc_free(x); } @@ -87,21 +87,23 @@ int main(int argc, char **argv) char **qiargv; char **arg; int i; - + if (chdir(auto_qmail) == -1) { - buffer_putsflush(buffer_2,"sendmail: fatal: unable to switch to qmail home directory\n"); + buffer_putsflush(buffer_2, "sendmail: fatal: unable to switch to qmail home directory\n"); _exit(111); } flagh = 0; sender = 0; - while ((opt = getopt(argc,argv,"vimte:f:p:o:B:F:EJxb:")) != opteof) { + while ((opt = getopt(argc, argv, "vimte:f:p:o:B:F:EJxb:")) != opteof) { switch (opt) { case 'N': break; /* ignore DSN option */ case 'B': break; case 't': flagh = 1; break; case 'f': sender = optarg; break; - case 'F': if (!env_put("MAILNAME",optarg)) nomem(); break; + case 'F': + if (!env_put("MAILNAME", optarg)) nomem(); + break; case 'p': break; /* could generate a Received line from optarg */ case 'v': break; case 'i': break; /* what an absurd concept */ @@ -116,7 +118,8 @@ int main(int argc, char **argv) case 'm': break; /* see 'm' above */ } break; - case 'E': case 'J': /* Sony NEWS-OS */ + case 'E': + case 'J': /* Sony NEWS-OS */ while (argv[optind][optpos]) ++optpos; /* skip optional argument */ break; case 'b': @@ -124,26 +127,24 @@ int main(int argc, char **argv) case 'm': break; case 'p': mailq(); case 's': smtpd(); - default: die_usage(); + default: die_usage(); } break; - default: - die_usage(); + default: die_usage(); } } argc -= optind; argv += optind; - - if (str_equal(optprogname,"mailq")) - mailq(); - if (str_equal(optprogname,"newaliases")) { - logmsg(WHO,100,FATAL,"please use fastforward/newaliases instead\n"); + if (str_equal(optprogname, "mailq")) mailq(); + + if (str_equal(optprogname, "newaliases")) { + logmsg(WHO, 100, FATAL, "please use fastforward/newaliases instead\n"); } - qiargv = (char **) alloc((argc + 10) * sizeof(char *)); + qiargv = (char **)alloc((argc + 10) * sizeof(char *)); if (!qiargv) nomem(); - + arg = qiargv; *arg++ = "bin/qmail-inject"; *arg++ = (flagh ? "-H" : "-a"); @@ -155,7 +156,7 @@ int main(int argc, char **argv) *arg++ = "--"; for (i = 0; i < argc; ++i) *arg++ = argv[i]; *arg = 0; - - execv(*qiargv,qiargv); - logmsg(WHO,111,FATAL,"unable to run qmail-inject\n"); + + execv(*qiargv, qiargv); + logmsg(WHO, 111, FATAL, "unable to run qmail-inject\n"); } diff --git a/src/setforward.c b/src/setforward.c index fe17f74..356ec78 100644 --- a/src/setforward.c +++ b/src/setforward.c @@ -1,43 +1,43 @@ #include <unistd.h> + #include "buffer.h" -#include "logmsg.h" -#include "stralloc.h" -#include "open.h" #include "case.h" #include "cdbmake.h" #include "logmsg.h" +#include "open.h" +#include "stralloc.h" #define WHO "setforward" -int rename(const char *,const char *); // stdio.h +int rename(const char *, const char *); // stdio.h void usage() { - logmsg(WHO,100,USAGE,"setforward data.cdb data.tmp"); + logmsg(WHO, 100, USAGE, "setforward data.cdb data.tmp"); } void nomem() { - logmsg(WHO,111,FATAL,"out of memory"); + logmsg(WHO, 111, FATAL, "out of memory"); } void missingsemicolon() { - logmsg(WHO,100,FATAL,"final instruction must end with semicolon"); + logmsg(WHO, 100, FATAL, "final instruction must end with semicolon"); } void extracolon() { - logmsg(WHO,100,FATAL,"double colons are not permitted"); + logmsg(WHO, 100, FATAL, "double colons are not permitted"); } void extracomma() { - logmsg(WHO,100,FATAL,"commas are not permitted before colons"); + logmsg(WHO, 100, FATAL, "commas are not permitted before colons"); } void nulbyte() { - logmsg(WHO,100,FATAL,"NUL bytes are not permitted"); + logmsg(WHO, 100, FATAL, "NUL bytes are not permitted"); } void longaddress() { - logmsg(WHO,100,FATAL,"addresses over 800 bytes are not permitted"); + logmsg(WHO, 100, FATAL, "addresses over 800 bytes are not permitted"); } char *fncdb; @@ -46,9 +46,9 @@ int fd; struct cdb_make cdb; stralloc key = {0}; -stralloc target = {0}; /* always initialized; no NUL */ +stralloc target = {0}; /* always initialized; no NUL */ stralloc command = {0}; /* always initialized; no NUL */ -stralloc instr = {0}; /* always initialized */ +stralloc instr = {0}; /* always initialized */ int flagtarget = 0; /* 0: reading target; command is empty; instr is empty */ @@ -56,19 +56,17 @@ int flagtarget = 0; void writeerr() { - logmsg(WHO,111,FATAL,B("unable to write to: ",fntmp)); + logmsg(WHO, 111, FATAL, B("unable to write to: ", fntmp)); } -void doit(prepend,data,datalen) -char *prepend; +void doit(prepend, data, datalen) char *prepend; char *data; int datalen; { - if (!stralloc_copys(&key,prepend)) nomem(); - if (!stralloc_cat(&key,&target)) nomem(); - case_lowerb(key.s,key.len); - if (cdb_make_add(&cdb,key.s,key.len,data,datalen) == -1) - writeerr(); + if (!stralloc_copys(&key, prepend)) nomem(); + if (!stralloc_cat(&key, &target)) nomem(); + case_lowerb(key.s, key.len); + if (cdb_make_add(&cdb, key.s, key.len, data, datalen) == -1) writeerr(); } int getch(ch) @@ -76,9 +74,8 @@ char *ch; { int r; - r = buffer_get(buffer_0small,ch,1); - if (r == -1) - logmsg(WHO,111,FATAL,"unable to read input: "); + r = buffer_get(buffer_0small, ch, 1); + if (r == -1) logmsg(WHO, 111, FATAL, "unable to read input: "); return r; } @@ -86,24 +83,26 @@ int main(int argc, char **argv) { char ch; - if (!stralloc_copys(&target,"")) nomem(); - if (!stralloc_copys(&command,"")) nomem(); - if (!stralloc_copys(&instr,"")) nomem(); + if (!stralloc_copys(&target, "")) nomem(); + if (!stralloc_copys(&command, "")) nomem(); + if (!stralloc_copys(&instr, "")) nomem(); - fncdb = argv[1]; if (!fncdb) usage(); - fntmp = argv[2]; if (!fntmp) usage(); + fncdb = argv[1]; + if (!fncdb) usage(); + fntmp = argv[2]; + if (!fntmp) usage(); fd = open_trunc(fntmp); - if (fd == -1) - logmsg(WHO,111,FATAL,B("unable to create: ",fntmp)); + if (fd == -1) logmsg(WHO, 111, FATAL, B("unable to create: ", fntmp)); - if (cdb_make_start(&cdb,fd) == -1) writeerr(); + if (cdb_make_start(&cdb, fd) == -1) writeerr(); for (;;) { if (!getch(&ch)) goto EOF; if (ch == '#') { - while (ch != '\n') if (!getch(&ch)) goto EOF; + while (ch != '\n') + if (!getch(&ch)) goto EOF; continue; } @@ -121,53 +120,48 @@ int main(int argc, char **argv) if (!flagtarget) extracomma(); if (command.len) { if (command.s[0] == '?') { - doit("?",command.s + 1,command.len - 1); - } - else if ((command.s[0] == '|') || (command.s[0] == '!')) { - if (!stralloc_cat(&instr,&command)) nomem(); + doit("?", command.s + 1, command.len - 1); + } else if ((command.s[0] == '|') || (command.s[0] == '!')) { + if (!stralloc_cat(&instr, &command)) nomem(); if (!stralloc_0(&instr)) nomem(); - } - else if ((command.s[0] == '.') || (command.s[0] == '/')) { - if (!stralloc_cat(&instr,&command)) nomem(); + } else if ((command.s[0] == '.') || (command.s[0] == '/')) { + if (!stralloc_cat(&instr, &command)) nomem(); if (!stralloc_0(&instr)) nomem(); - } - else { + } else { if (command.len > 800) longaddress(); if (command.s[0] != '&') - if (!stralloc_cats(&instr,"&")) nomem(); - if (!stralloc_cat(&instr,&command)) nomem(); + if (!stralloc_cats(&instr, "&")) nomem(); + if (!stralloc_cat(&instr, &command)) nomem(); if (!stralloc_0(&instr)) nomem(); } } - if (!stralloc_copys(&command,"")) nomem(); + if (!stralloc_copys(&command, "")) nomem(); if (ch == ';') { - if (instr.len) - doit(":",instr.s,instr.len); + if (instr.len) doit(":", instr.s, instr.len); - if (!stralloc_copys(&target,"")) nomem(); - if (!stralloc_copys(&instr,"")) nomem(); + if (!stralloc_copys(&target, "")) nomem(); + if (!stralloc_copys(&instr, "")) nomem(); flagtarget = 0; } continue; } - if (ch == '\\') if (!getch(&ch)) goto EOF; + if (ch == '\\') + if (!getch(&ch)) goto EOF; if (ch == 0) nulbyte(); - if (!stralloc_append(flagtarget ? &command : &target,&ch)) nomem(); + if (!stralloc_append(flagtarget ? &command : &target, &ch)) nomem(); } - EOF: - if (flagtarget || target.len) - missingsemicolon(); +EOF: + if (flagtarget || target.len) missingsemicolon(); if (cdb_make_finish(&cdb) == -1) writeerr(); if (fsync(fd) == -1) writeerr(); if (close(fd) == -1) writeerr(); /* NFS stupidity */ - if (rename(fntmp,fncdb) == -1) - logmsg(WHO,111,FATAL,B("unable to move ",fntmp," to: ",fncdb)); - + if (rename(fntmp, fncdb) == -1) logmsg(WHO, 111, FATAL, B("unable to move ", fntmp, " to: ", fncdb)); + _exit(0); } diff --git a/src/setmaillist.c b/src/setmaillist.c index f7ac89b..a32e86b 100644 --- a/src/setmaillist.c +++ b/src/setmaillist.c @@ -1,19 +1,20 @@ -#include <unistd.h> #include <sys/stat.h> +#include <unistd.h> + #include "buffer.h" -#include "logmsg.h" -#include "stralloc.h" +#include "byte.h" #include "getln.h" +#include "logmsg.h" #include "open.h" -#include "byte.h" +#include "stralloc.h" #define WHO "setmaillist" -int rename(const char *,const char *); // stdio.h +int rename(const char *, const char *); // stdio.h void usage() { - logmsg(WHO,100,USAGE,"setmaillist list.bin list.tmp"); + logmsg(WHO, 100, USAGE, "setmaillist list.bin list.tmp"); } stralloc line = {0}; @@ -27,56 +28,52 @@ buffer bo; void writeerr() { - logmsg(WHO,111,FATAL,B("unable to write to: ",fntmp)); + logmsg(WHO, 111, FATAL, B("unable to write to: ", fntmp)); } -static void out(char *s,int len) +static void out(char *s, int len) { - if (buffer_put(&bo,s,len) == -1) writeerr(); + if (buffer_put(&bo, s, len) == -1) writeerr(); } -int main(int argc,char **argv) +int main(int argc, char **argv) { umask(033); - fnbin = argv[1]; if (!fnbin) usage(); - fntmp = argv[2]; if (!fntmp) usage(); + fnbin = argv[1]; + if (!fnbin) usage(); + fntmp = argv[2]; + if (!fntmp) usage(); fd = open_trunc(fntmp); - if (fd == -1) - logmsg(WHO,111,FATAL,B("unable to create: ",fntmp)); + if (fd == -1) logmsg(WHO, 111, FATAL, B("unable to create: ", fntmp)); - buffer_init(&bo,write,fd,buf,sizeof(buf)); + buffer_init(&bo, write, fd, buf, sizeof(buf)); do { - if (getln(buffer_0small,&line,&match,'\n') == -1) - logmsg(WHO,111,FATAL,"unable to read input: "); + if (getln(buffer_0small, &line, &match, '\n') == -1) + logmsg(WHO, 111, FATAL, "unable to read input: "); while (line.len) { if (line.s[line.len - 1] != '\n') if (line.s[line.len - 1] != ' ') - if (line.s[line.len - 1] != '\t') - break; + if (line.s[line.len - 1] != '\t') break; --line.len; } - if (byte_chr(line.s,line.len,'\0') != line.len) - logmsg(WHO,111,FATAL,"NUL in input"); + if (byte_chr(line.s, line.len, '\0') != line.len) logmsg(WHO, 111, FATAL, "NUL in input"); if (line.len) if (line.s[0] != '#') { if ((line.s[0] == '.') || (line.s[0] == '/')) { - out(line.s,line.len); - out("",1); - } - else { - if (line.len > 800) - logmsg(WHO,111,FATAL,"addresses must be under 800 bytes"); - if (line.s[0] != '&') - out("&",1); - out(line.s,line.len); - out("",1); + out(line.s, line.len); + out("", 1); + } else { + if (line.len > 800) logmsg(WHO, 111, FATAL, "addresses must be under 800 bytes"); + if (line.s[0] != '&') out("&", 1); + out(line.s, line.len); + out("", 1); } } @@ -86,8 +83,7 @@ int main(int argc,char **argv) if (fsync(fd) == -1) writeerr(); if (close(fd) == -1) writeerr(); /* NFS stupidity */ - if (rename(fntmp,fnbin) == -1) - logmsg(WHO,111,FATAL,B("unable to move ",fntmp," to: ",fnbin)); - + if (rename(fntmp, fnbin) == -1) logmsg(WHO, 111, FATAL, B("unable to move ", fntmp, " to: ", fnbin)); + _exit(0); } @@ -15,8 +15,10 @@ Still 100% Public Domain; though requiring fehQlibs-14 */ -#include <string.h> #include "sha1.h" + +#include <string.h> + #include "byte.h" // #define SHA1HANDSOFF @@ -27,39 +29,51 @@ Still 100% Public Domain; though requiring fehQlibs-14 /* I got the idea of expanding during the round function from SSLeay */ /* FIXME: can we do this in an endian-proof way? */ #ifndef WORDS_BIGENDIAN -#define blk0(i) (block->l[i] = (rol(block->l[i],24) & 0xFF00FF00) \ - | (rol(block->l[i],8) & 0x00FF00FF)) + #define blk0(i) \ + (block->l[i] = (rol(block->l[i], 24) & 0xFF00FF00) | (rol(block->l[i], 8) & 0x00FF00FF)) #else -#define blk0(i) block->l[i] + #define blk0(i) block->l[i] #endif -#define blk(i) (block->l[i&15] = rol(block->l[(i+13)&15]^block->l[(i+8)&15] \ - ^block->l[(i+2)&15]^block->l[i&15],1)) +#define blk(i) \ + (block->l[i & 15] = rol( \ + block->l[(i + 13) & 15] ^ block->l[(i + 8) & 15] ^ block->l[(i + 2) & 15] ^ block->l[i & 15], \ + 1)) /* (R0+R1), R2, R3, R4 are the different operations used in SHA1 */ -#define R0(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk0(i)+0x5A827999+rol(v,5);w=rol(w,30); -#define R1(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk(i)+0x5A827999+rol(v,5);w=rol(w,30); -#define R2(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0x6ED9EBA1+rol(v,5);w=rol(w,30); -#define R3(v,w,x,y,z,i) z+=(((w|x)&y)|(w&x))+blk(i)+0x8F1BBCDC+rol(v,5);w=rol(w,30); -#define R4(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=rol(w,30); +#define R0(v, w, x, y, z, i) \ + z += ((w & (x ^ y)) ^ y) + blk0(i) + 0x5A827999 + rol(v, 5); \ + w = rol(w, 30); +#define R1(v, w, x, y, z, i) \ + z += ((w & (x ^ y)) ^ y) + blk(i) + 0x5A827999 + rol(v, 5); \ + w = rol(w, 30); +#define R2(v, w, x, y, z, i) \ + z += (w ^ x ^ y) + blk(i) + 0x6ED9EBA1 + rol(v, 5); \ + w = rol(w, 30); +#define R3(v, w, x, y, z, i) \ + z += (((w | x) & y) | (w & x)) + blk(i) + 0x8F1BBCDC + rol(v, 5); \ + w = rol(w, 30); +#define R4(v, w, x, y, z, i) \ + z += (w ^ x ^ y) + blk(i) + 0xCA62C1D6 + rol(v, 5); \ + w = rol(w, 30); /* Hash a single 512-bit block. This is the core of the algorithm. */ -void sha1_transform(uint32_t state[5],const uint8_t buffer[SHA1_BLOCKSIZE]) +void sha1_transform(uint32_t state[5], const uint8_t buffer[SHA1_BLOCKSIZE]) { uint32_t a, b, c, d, e; typedef union { - uint8_t c[SHA1_BLOCKSIZE]; - uint32_t l[16]; + uint8_t c[SHA1_BLOCKSIZE]; + uint32_t l[16]; } CHAR64LONG16; CHAR64LONG16 *block; #ifdef SHA1HANDSOFF static uint8_t workspace[SHA1_BLOCKSIZE]; - block = (CHAR64LONG16 *) workspace; - byte_copy(block,SHA1_BLOCKSIZE,buffer); + block = (CHAR64LONG16 *)workspace; + byte_copy(block, SHA1_BLOCKSIZE, buffer); #else - block = (CHAR64LONG16 *) buffer; + block = (CHAR64LONG16 *)buffer; #endif /* Copy context->state[] to working vars */ @@ -70,26 +84,86 @@ void sha1_transform(uint32_t state[5],const uint8_t buffer[SHA1_BLOCKSIZE]) e = state[4]; /* 4 rounds of 20 operations each. Loop unrolled. */ - R0(a,b,c,d,e, 0); R0(e,a,b,c,d, 1); R0(d,e,a,b,c, 2); R0(c,d,e,a,b, 3); - R0(b,c,d,e,a, 4); R0(a,b,c,d,e, 5); R0(e,a,b,c,d, 6); R0(d,e,a,b,c, 7); - R0(c,d,e,a,b, 8); R0(b,c,d,e,a, 9); R0(a,b,c,d,e,10); R0(e,a,b,c,d,11); - R0(d,e,a,b,c,12); R0(c,d,e,a,b,13); R0(b,c,d,e,a,14); R0(a,b,c,d,e,15); - R1(e,a,b,c,d,16); R1(d,e,a,b,c,17); R1(c,d,e,a,b,18); R1(b,c,d,e,a,19); - R2(a,b,c,d,e,20); R2(e,a,b,c,d,21); R2(d,e,a,b,c,22); R2(c,d,e,a,b,23); - R2(b,c,d,e,a,24); R2(a,b,c,d,e,25); R2(e,a,b,c,d,26); R2(d,e,a,b,c,27); - R2(c,d,e,a,b,28); R2(b,c,d,e,a,29); R2(a,b,c,d,e,30); R2(e,a,b,c,d,31); - R2(d,e,a,b,c,32); R2(c,d,e,a,b,33); R2(b,c,d,e,a,34); R2(a,b,c,d,e,35); - R2(e,a,b,c,d,36); R2(d,e,a,b,c,37); R2(c,d,e,a,b,38); R2(b,c,d,e,a,39); - R3(a,b,c,d,e,40); R3(e,a,b,c,d,41); R3(d,e,a,b,c,42); R3(c,d,e,a,b,43); - R3(b,c,d,e,a,44); R3(a,b,c,d,e,45); R3(e,a,b,c,d,46); R3(d,e,a,b,c,47); - R3(c,d,e,a,b,48); R3(b,c,d,e,a,49); R3(a,b,c,d,e,50); R3(e,a,b,c,d,51); - R3(d,e,a,b,c,52); R3(c,d,e,a,b,53); R3(b,c,d,e,a,54); R3(a,b,c,d,e,55); - R3(e,a,b,c,d,56); R3(d,e,a,b,c,57); R3(c,d,e,a,b,58); R3(b,c,d,e,a,59); - R4(a,b,c,d,e,60); R4(e,a,b,c,d,61); R4(d,e,a,b,c,62); R4(c,d,e,a,b,63); - R4(b,c,d,e,a,64); R4(a,b,c,d,e,65); R4(e,a,b,c,d,66); R4(d,e,a,b,c,67); - R4(c,d,e,a,b,68); R4(b,c,d,e,a,69); R4(a,b,c,d,e,70); R4(e,a,b,c,d,71); - R4(d,e,a,b,c,72); R4(c,d,e,a,b,73); R4(b,c,d,e,a,74); R4(a,b,c,d,e,75); - R4(e,a,b,c,d,76); R4(d,e,a,b,c,77); R4(c,d,e,a,b,78); R4(b,c,d,e,a,79); + R0(a, b, c, d, e, 0); + R0(e, a, b, c, d, 1); + R0(d, e, a, b, c, 2); + R0(c, d, e, a, b, 3); + R0(b, c, d, e, a, 4); + R0(a, b, c, d, e, 5); + R0(e, a, b, c, d, 6); + R0(d, e, a, b, c, 7); + R0(c, d, e, a, b, 8); + R0(b, c, d, e, a, 9); + R0(a, b, c, d, e, 10); + R0(e, a, b, c, d, 11); + R0(d, e, a, b, c, 12); + R0(c, d, e, a, b, 13); + R0(b, c, d, e, a, 14); + R0(a, b, c, d, e, 15); + R1(e, a, b, c, d, 16); + R1(d, e, a, b, c, 17); + R1(c, d, e, a, b, 18); + R1(b, c, d, e, a, 19); + R2(a, b, c, d, e, 20); + R2(e, a, b, c, d, 21); + R2(d, e, a, b, c, 22); + R2(c, d, e, a, b, 23); + R2(b, c, d, e, a, 24); + R2(a, b, c, d, e, 25); + R2(e, a, b, c, d, 26); + R2(d, e, a, b, c, 27); + R2(c, d, e, a, b, 28); + R2(b, c, d, e, a, 29); + R2(a, b, c, d, e, 30); + R2(e, a, b, c, d, 31); + R2(d, e, a, b, c, 32); + R2(c, d, e, a, b, 33); + R2(b, c, d, e, a, 34); + R2(a, b, c, d, e, 35); + R2(e, a, b, c, d, 36); + R2(d, e, a, b, c, 37); + R2(c, d, e, a, b, 38); + R2(b, c, d, e, a, 39); + R3(a, b, c, d, e, 40); + R3(e, a, b, c, d, 41); + R3(d, e, a, b, c, 42); + R3(c, d, e, a, b, 43); + R3(b, c, d, e, a, 44); + R3(a, b, c, d, e, 45); + R3(e, a, b, c, d, 46); + R3(d, e, a, b, c, 47); + R3(c, d, e, a, b, 48); + R3(b, c, d, e, a, 49); + R3(a, b, c, d, e, 50); + R3(e, a, b, c, d, 51); + R3(d, e, a, b, c, 52); + R3(c, d, e, a, b, 53); + R3(b, c, d, e, a, 54); + R3(a, b, c, d, e, 55); + R3(e, a, b, c, d, 56); + R3(d, e, a, b, c, 57); + R3(c, d, e, a, b, 58); + R3(b, c, d, e, a, 59); + R4(a, b, c, d, e, 60); + R4(e, a, b, c, d, 61); + R4(d, e, a, b, c, 62); + R4(c, d, e, a, b, 63); + R4(b, c, d, e, a, 64); + R4(a, b, c, d, e, 65); + R4(e, a, b, c, d, 66); + R4(d, e, a, b, c, 67); + R4(c, d, e, a, b, 68); + R4(b, c, d, e, a, 69); + R4(a, b, c, d, e, 70); + R4(e, a, b, c, d, 71); + R4(d, e, a, b, c, 72); + R4(c, d, e, a, b, 73); + R4(b, c, d, e, a, 74); + R4(a, b, c, d, e, 75); + R4(e, a, b, c, d, 76); + R4(d, e, a, b, c, 77); + R4(c, d, e, a, b, 78); + R4(b, c, d, e, a, 79); /* Add the working vars back into context.state[] */ state[0] += a; @@ -102,7 +176,7 @@ void sha1_transform(uint32_t state[5],const uint8_t buffer[SHA1_BLOCKSIZE]) a = b = c = d = e = 0; #ifdef SHA1HANDSOFF - byte_zero(block,64); + byte_zero(block, 64); #endif } @@ -121,68 +195,65 @@ void sha1_init(sha1_ctx *context) /* Run your data through this. */ -void sha1_update(sha1_ctx *context,const uint8_t *data,uint32_t len) +void sha1_update(sha1_ctx *context, const uint8_t *data, uint32_t len) { uint32_t i, j; j = (context->count[0] >> 3) & 63; - if ((context->count[0] += len << 3) < (len << 3)) - context->count[1]++; + if ((context->count[0] += len << 3) < (len << 3)) context->count[1]++; context->count[1] += (len >> 29); if ((j + len) > 63) { - byte_copy(&context->buffer[j],(i = 64 - j),data); - sha1_transform(context->state,context->buffer); + byte_copy(&context->buffer[j], (i = 64 - j), data); + sha1_transform(context->state, context->buffer); for (; i + 63 < len; i += 64) { - sha1_transform(context->state,data + i); + sha1_transform(context->state, data + i); } j = 0; } else i = 0; - byte_copy(&context->buffer[j],len - i,&data[i]); + byte_copy(&context->buffer[j], len - i, &data[i]); } /* Add padding and return the message digest. */ -void sha1_final(uint8_t digest[SHA1_DIGESTSIZE],sha1_ctx *context) +void sha1_final(uint8_t digest[SHA1_DIGESTSIZE], sha1_ctx *context) { uint32_t i; uint8_t finalcount[8]; for (i = 0; i < 8; i++) { - finalcount[i] = (uint8_t)((context->count[(i >= 4 ? 0 : 1)] - >> ((3 - (i & 3)) * 8)) & 255); /* Endian independent */ + finalcount[i] = (uint8_t)((context->count[(i >= 4 ? 0 : 1)] >> ((3 - (i & 3)) * 8)) + & 255); /* Endian independent */ } - sha1_update(context,(uint8_t *) "\200",1); + sha1_update(context, (uint8_t *)"\200", 1); + + while ((context->count[0] & 504) != 448) sha1_update(context, (uint8_t *)"\0", 1); - while ((context->count[0] & 504) != 448) - sha1_update(context,(uint8_t *) "\0",1); - - sha1_update(context,finalcount,8); /* Should cause a SHA1_Transform() */ + sha1_update(context, finalcount, 8); /* Should cause a SHA1_Transform() */ - for (i = 0; i < SHA1_DIGESTSIZE; i++) - digest[i] = (uint8_t) ((context->state[i >> 2] >> ((3 - (i & 3)) * 8)) & 255); + for (i = 0; i < SHA1_DIGESTSIZE; i++) + digest[i] = (uint8_t)((context->state[i >> 2] >> ((3 - (i & 3)) * 8)) & 255); /* Wipe variables */ i = 0; - byte_zero(context->buffer,64); - byte_zero(context->state,20); - byte_zero(context->count,8); - byte_zero(finalcount,8); + byte_zero(context->buffer, 64); + byte_zero(context->state, 20); + byte_zero(context->count, 8); + byte_zero(finalcount, 8); -#ifdef SHA1HANDSOFF /* make SHA1Transform overwrite its own static vars */ - sha1_transform(context->state,context->buffer); +#ifdef SHA1HANDSOFF /* make SHA1Transform overwrite its own static vars */ + sha1_transform(context->state, context->buffer); #endif } -void sha1_hash(char *hash,const char *str,uint32_t len) +void sha1_hash(char *hash, const char *str, uint32_t len) { sha1_ctx context; int i; sha1_init(&context); - for (i = 0; i < len; i++) - sha1_update(&context,(uint8_t *)str + i,1); + for (i = 0; i < len; i++) sha1_update(&context, (uint8_t *)str + i, 1); - sha1_final((uint8_t *)hash,&context); + sha1_final((uint8_t *)hash, &context); hash[20] = '\0'; } diff --git a/src/sha256.c b/src/sha256.c index e5ba5dd..0bb29af 100644 --- a/src/sha256.c +++ b/src/sha256.c @@ -6,46 +6,43 @@ * clean-up and argument order for sha256_hash (feh). */ +#include "sha256.h" + #include <stdint.h> #include <string.h> -#include "sha256.h" // DBL_INT_ADD treats two unsigned ints a and b as one 64-bit integer and adds c to it -#define DBL_INT_ADD(a,b,c) if (a > 0xffffffff - (c)) ++b; a += c; -#define ROTLEFT(a,b) (((a) << (b)) | ((a) >> (32-(b)))) -#define ROTRIGHT(a,b) (((a) >> (b)) | ((a) << (32-(b)))) +#define DBL_INT_ADD(a, b, c) \ + if (a > 0xffffffff - (c)) ++b; \ + a += c; +#define ROTLEFT(a, b) (((a) << (b)) | ((a) >> (32 - (b)))) +#define ROTRIGHT(a, b) (((a) >> (b)) | ((a) << (32 - (b)))) -#define CH(x,y,z) (((x) & (y)) ^ (~(x) & (z))) -#define MAJ(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z))) -#define EP0(x) (ROTRIGHT(x,2) ^ ROTRIGHT(x,13) ^ ROTRIGHT(x,22)) -#define EP1(x) (ROTRIGHT(x,6) ^ ROTRIGHT(x,11) ^ ROTRIGHT(x,25)) -#define SIG0(x) (ROTRIGHT(x,7) ^ ROTRIGHT(x,18) ^ ((x) >> 3)) -#define SIG1(x) (ROTRIGHT(x,17) ^ ROTRIGHT(x,19) ^ ((x) >> 10)) +#define CH(x, y, z) (((x) & (y)) ^ (~(x) & (z))) +#define MAJ(x, y, z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z))) +#define EP0(x) (ROTRIGHT(x, 2) ^ ROTRIGHT(x, 13) ^ ROTRIGHT(x, 22)) +#define EP1(x) (ROTRIGHT(x, 6) ^ ROTRIGHT(x, 11) ^ ROTRIGHT(x, 25)) +#define SIG0(x) (ROTRIGHT(x, 7) ^ ROTRIGHT(x, 18) ^ ((x) >> 3)) +#define SIG1(x) (ROTRIGHT(x, 17) ^ ROTRIGHT(x, 19) ^ ((x) >> 10)) -uint32_t k[64] = -{ - 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, - 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, - 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786, - 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, - 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, - 0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, - 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b, - 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, - 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, - 0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, - 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2 -}; +uint32_t k[64] = { + 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, + 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, + 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, + 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, + 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, + 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, + 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, + 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2}; static void sha256_transform(sha256_ctx *ctx, uint8_t *data) -{ +{ uint32_t a, b, c, d, e, f, g, h, i, j, t1, t2, m[64]; - + for (i = 0, j = 0; i < 16; ++i, j += 4) - m[i] = (data[j] << 24) | (data[j+1] << 16) | (data[j+2] << 8) | (data[j+3]); - for (; i < 64; ++i) - m[i] = SIG1(m[i-2]) + m[i-7] + SIG0(m[i-15]) + m[i-16]; + m[i] = (data[j] << 24) | (data[j + 1] << 16) | (data[j + 2] << 8) | (data[j + 3]); + for (; i < 64; ++i) m[i] = SIG1(m[i - 2]) + m[i - 7] + SIG0(m[i - 15]) + m[i - 16]; a = ctx->state[0]; b = ctx->state[1]; @@ -55,10 +52,10 @@ static void sha256_transform(sha256_ctx *ctx, uint8_t *data) f = ctx->state[5]; g = ctx->state[6]; h = ctx->state[7]; - + for (i = 0; i < 64; ++i) { - t1 = h + EP1(e) + CH(e,f,g) + k[i] + m[i]; - t2 = EP0(a) + MAJ(a,b,c); + t1 = h + EP1(e) + CH(e, f, g) + k[i] + m[i]; + t2 = EP0(a) + MAJ(a, b, c); h = g; g = f; f = e; @@ -68,7 +65,7 @@ static void sha256_transform(sha256_ctx *ctx, uint8_t *data) b = a; a = t1 + t2; } - + ctx->state[0] += a; ctx->state[1] += b; ctx->state[2] += c; @@ -80,10 +77,10 @@ static void sha256_transform(sha256_ctx *ctx, uint8_t *data) } static void sha256_init(sha256_ctx *ctx) -{ - ctx->datalen = 0; - ctx->bitlen[0] = 0; - ctx->bitlen[1] = 0; +{ + ctx->datalen = 0; + ctx->bitlen[0] = 0; + ctx->bitlen[1] = 0; ctx->state[0] = 0x6a09e667; ctx->state[1] = 0xbb67ae85; ctx->state[2] = 0x3c6ef372; @@ -95,73 +92,70 @@ static void sha256_init(sha256_ctx *ctx) } static void sha256_update(sha256_ctx *ctx, uint8_t *data, uint32_t len) -{ +{ uint32_t i; - - for (i=0; i < len; ++i) { - ctx->data[ctx->datalen] = data[i]; - ctx->datalen++; - if (ctx->datalen == 64) { - sha256_transform(ctx,ctx->data); - DBL_INT_ADD(ctx->bitlen[0],ctx->bitlen[1],512); - ctx->datalen = 0; - } - } -} - -static void sha256_final(uint8_t *hash,sha256_ctx *ctx) -{ - uint32_t i; - - i = ctx->datalen; - -// Pad whatever data is left in the buffer. - - if (ctx->datalen < 56) { - ctx->data[i++] = 0x80; - while (i < 56) - ctx->data[i++] = 0x00; + + for (i = 0; i < len; ++i) { + ctx->data[ctx->datalen] = data[i]; + ctx->datalen++; + if (ctx->datalen == 64) { + sha256_transform(ctx, ctx->data); + DBL_INT_ADD(ctx->bitlen[0], ctx->bitlen[1], 512); + ctx->datalen = 0; + } } - else { - ctx->data[i++] = 0x80; - while (i < 64) - ctx->data[i++] = 0x00; - sha256_transform(ctx,ctx->data); - memset(ctx->data,0,56); - } - -// Append to the padding the total message's length in bits and transform. - - DBL_INT_ADD(ctx->bitlen[0],ctx->bitlen[1],ctx->datalen * 8); - ctx->data[63] = ctx->bitlen[0]; - ctx->data[62] = ctx->bitlen[0] >> 8; - ctx->data[61] = ctx->bitlen[0] >> 16; - ctx->data[60] = ctx->bitlen[0] >> 24; - ctx->data[59] = ctx->bitlen[1]; - ctx->data[58] = ctx->bitlen[1] >> 8; - ctx->data[57] = ctx->bitlen[1] >> 16; - ctx->data[56] = ctx->bitlen[1] >> 24; - sha256_transform(ctx,ctx->data); - -// Since this implementation uses little endian byte ordering and SHA uses -// big endian, reverse all the bytes when copying the final state to the output hash. - - for (i = 0; i < 4; ++i) { - hash[i] = (ctx->state[0] >> (24-i*8)) & 0x000000ff; - hash[i+4] = (ctx->state[1] >> (24-i*8)) & 0x000000ff; - hash[i+8] = (ctx->state[2] >> (24-i*8)) & 0x000000ff; - hash[i+12] = (ctx->state[3] >> (24-i*8)) & 0x000000ff; - hash[i+16] = (ctx->state[4] >> (24-i*8)) & 0x000000ff; - hash[i+20] = (ctx->state[5] >> (24-i*8)) & 0x000000ff; - hash[i+24] = (ctx->state[6] >> (24-i*8)) & 0x000000ff; - hash[i+28] = (ctx->state[7] >> (24-i*8)) & 0x000000ff; +} + +static void sha256_final(uint8_t *hash, sha256_ctx *ctx) +{ + uint32_t i; + + i = ctx->datalen; + + // Pad whatever data is left in the buffer. + + if (ctx->datalen < 56) { + ctx->data[i++] = 0x80; + while (i < 56) ctx->data[i++] = 0x00; + } else { + ctx->data[i++] = 0x80; + while (i < 64) ctx->data[i++] = 0x00; + sha256_transform(ctx, ctx->data); + memset(ctx->data, 0, 56); + } + + // Append to the padding the total message's length in bits and transform. + + DBL_INT_ADD(ctx->bitlen[0], ctx->bitlen[1], ctx->datalen * 8); + ctx->data[63] = ctx->bitlen[0]; + ctx->data[62] = ctx->bitlen[0] >> 8; + ctx->data[61] = ctx->bitlen[0] >> 16; + ctx->data[60] = ctx->bitlen[0] >> 24; + ctx->data[59] = ctx->bitlen[1]; + ctx->data[58] = ctx->bitlen[1] >> 8; + ctx->data[57] = ctx->bitlen[1] >> 16; + ctx->data[56] = ctx->bitlen[1] >> 24; + sha256_transform(ctx, ctx->data); + + // Since this implementation uses little endian byte ordering and SHA uses + // big endian, reverse all the bytes when copying the final state to the output hash. + + for (i = 0; i < 4; ++i) { + hash[i] = (ctx->state[0] >> (24 - i * 8)) & 0x000000ff; + hash[i + 4] = (ctx->state[1] >> (24 - i * 8)) & 0x000000ff; + hash[i + 8] = (ctx->state[2] >> (24 - i * 8)) & 0x000000ff; + hash[i + 12] = (ctx->state[3] >> (24 - i * 8)) & 0x000000ff; + hash[i + 16] = (ctx->state[4] >> (24 - i * 8)) & 0x000000ff; + hash[i + 20] = (ctx->state[5] >> (24 - i * 8)) & 0x000000ff; + hash[i + 24] = (ctx->state[6] >> (24 - i * 8)) & 0x000000ff; + hash[i + 28] = (ctx->state[7] >> (24 - i * 8)) & 0x000000ff; } } -extern void sha256_hash(char *hash,const char *data,size_t len) +extern void sha256_hash(char *hash, const char *data, size_t len) { sha256_ctx ctx; sha256_init(&ctx); - sha256_update(&ctx,(uint8_t *)data,(int)len); - sha256_final((uint8_t *)hash,&ctx); + sha256_update(&ctx, (uint8_t *)data, (int)len); + sha256_final((uint8_t *)hash, &ctx); } diff --git a/src/smtpdlog.c b/src/smtpdlog.c index 1b44af1..c7505af 100755..100644 --- a/src/smtpdlog.c +++ b/src/smtpdlog.c @@ -1,11 +1,13 @@ +#include "smtpdlog.h" + #include <unistd.h> + #include "buffer.h" -#include "str.h" #include "byte.h" #include "env.h" -#include "fmt.h" #include "exit.h" -#include "smtpdlog.h" +#include "fmt.h" +#include "str.h" #define FDLOG 2 char *reply421pgl; @@ -21,7 +23,7 @@ char *reply554cnt; static char strnum[FMT_ULONG]; static char logbuf[512]; -buffer bo2 = BUFFER_INIT(write,FDLOG,logbuf,sizeof(logbuf)); +buffer bo2 = BUFFER_INIT(write, FDLOG, logbuf, sizeof(logbuf)); void smtpdlog_init() { @@ -37,150 +39,317 @@ void smtpdlog_init() reply554cnt = env_get("REPLY_CONTENT"); } -static void logs(char *s) { if (buffer_puts(&bo2,s) == -1) _exit(1); } /* single string */ -static void logp(char *s) { logs(" P:"); logs(s); } /* protocol */ -static void logh(char *s1,char *s2,char *s3) { logs(" S:"); logs(s1); logs(":"); logs(s2); logs(" H:"); logs(s3); } /* host */ -static void logm(char *s) { logs(" F:"); logs(s); } /* mailfrom */ -static void logt(char *s) { logs(" T:"); logs(s); } /* rcptto */ -static void logi(char *s) { logs(" '"); logs(s); logs("'"); } /* information */ -static void logn(char *s) { if (buffer_puts(&bo2,s) == -1 ) _exit(1); if (buffer_flush(&bo2) == -1) _exit(1); } /* end */ -static void logpid() { strnum[fmt_ulong(strnum,getpid())] = 0; logs("qmail-smtpd: pid "); logs(strnum); logs(" "); } +static void logs(char *s) +{ + if (buffer_puts(&bo2, s) == -1) _exit(1); +} /* single string */ +static void logp(char *s) +{ + logs(" P:"); + logs(s); +} /* protocol */ +static void logh(char *s1, char *s2, char *s3) +{ + logs(" S:"); + logs(s1); + logs(":"); + logs(s2); + logs(" H:"); + logs(s3); +} /* host */ +static void logm(char *s) +{ + logs(" F:"); + logs(s); +} /* mailfrom */ +static void logt(char *s) +{ + logs(" T:"); + logs(s); +} /* rcptto */ +static void logi(char *s) +{ + logs(" '"); + logs(s); + logs("'"); +} /* information */ +static void logn(char *s) +{ + if (buffer_puts(&bo2, s) == -1) _exit(1); + if (buffer_flush(&bo2) == -1) _exit(1); +} /* end */ +static void logpid() +{ + strnum[fmt_ulong(strnum, getpid())] = 0; + logs("qmail-smtpd: pid "); + logs(strnum); + logs(" "); +} -void smtp_loga(char *s1,char *s2,char *s3,char *s4,char *s5,char *s6,char *s7,char *s8,char *s9) - { logpid(); logs(s1); logs(s9); logp(s2); logh(s3,s4,s5); logm(s6); logt(s7), logs(" ?~"); logi(s8); logn("\n"); } /* Auth info */ -void smtp_logb(char *s1,char *s2,char *s3,char *s4,char *s5,char *s6,char *s7) - { logpid(); logs(s1); logs(s7); logp(s2); logh(s3,s4,s5); logs(" ?~"); logi(s6); logn("\n"); } /* Auth info */ -void smtp_logg(char *s1,char *s2,char *s3,char *s4,char *s5,char *s6,char *s7) - { logpid(); logs(s1); logp(s2); logh(s3,s4,s5); logm(s6); logt(s7); logn("\n"); } /* Generic */ -void smtp_logh(char *s1,char *s2,char *s3,char *s4,char *s5) - { logpid(); logs(s1); logp(s2); logh(s3,s4,s5); logn("\n"); } /* Host */ -void smtp_logi(char *s1,char *s2,char *s3,char *s4,char *s5,char *s6,char *s7,char *s8) - { logpid(); logs(s1); logp(s2); logh(s3,s4,s5); logm(s6); logt(s7); logi(s8); logn("\n"); } /* Generic + Info */ -void smtp_logr(char *s1,char *s2,char *s3,char *s4,char *s5,char *s6,char *s7,char *s8) - { logpid(); logs(s1); logs(s2); logp(s3); logh(s4,s5,s6); logm(s7); logt(s8); logn("\n"); } /* Recipient */ +void smtp_loga(char *s1, char *s2, char *s3, char *s4, char *s5, char *s6, char *s7, char *s8, char *s9) +{ + logpid(); + logs(s1); + logs(s9); + logp(s2); + logh(s3, s4, s5); + logm(s6); + logt(s7), logs(" ?~"); + logi(s8); + logn("\n"); +} /* Auth info */ +void smtp_logb(char *s1, char *s2, char *s3, char *s4, char *s5, char *s6, char *s7) +{ + logpid(); + logs(s1); + logs(s7); + logp(s2); + logh(s3, s4, s5); + logs(" ?~"); + logi(s6); + logn("\n"); +} /* Auth info */ +void smtp_logg(char *s1, char *s2, char *s3, char *s4, char *s5, char *s6, char *s7) +{ + logpid(); + logs(s1); + logp(s2); + logh(s3, s4, s5); + logm(s6); + logt(s7); + logn("\n"); +} /* Generic */ +void smtp_logh(char *s1, char *s2, char *s3, char *s4, char *s5) +{ + logpid(); + logs(s1); + logp(s2); + logh(s3, s4, s5); + logn("\n"); +} /* Host */ +void smtp_logi(char *s1, char *s2, char *s3, char *s4, char *s5, char *s6, char *s7, char *s8) +{ + logpid(); + logs(s1); + logp(s2); + logh(s3, s4, s5); + logm(s6); + logt(s7); + logi(s8); + logn("\n"); +} /* Generic + Info */ +void smtp_logr(char *s1, char *s2, char *s3, char *s4, char *s5, char *s6, char *s7, char *s8) +{ + logpid(); + logs(s1); + logs(s2); + logp(s3); + logh(s4, s5, s6); + logm(s7); + logt(s8); + logn("\n"); +} /* Recipient */ -void die_read() { _exit(1); } -void die_alarm() { out("451 timeout (#4.4.2)\r\n"); flush(); _exit(1); } -void die_nomem() { out("421 out of memory (#4.3.0)\r\n"); flush(); _exit(1); } -void die_control() { out("421 unable to read controls (#4.3.0)\r\n"); flush(); _exit(1); } -void die_ipme() { out("421 unable to figure out my IP addresses (#4.3.0)\r\n"); flush(); _exit(1); } -void die_starttls() { out("454 TLS not available due to temporary reason (#5.7.3)\r\n"); flush(); _exit(1); } -void die_recipients() { out("421 unable to check recipients (#4.3.0)\r\n"); flush(); _exit(1); } +void die_read() +{ + _exit(1); +} +void die_alarm() +{ + out("451 timeout (#4.4.2)\r\n"); + flush(); + _exit(1); +} +void die_nomem() +{ + out("421 out of memory (#4.3.0)\r\n"); + flush(); + _exit(1); +} +void die_control() +{ + out("421 unable to read controls (#4.3.0)\r\n"); + flush(); + _exit(1); +} +void die_ipme() +{ + out("421 unable to figure out my IP addresses (#4.3.0)\r\n"); + flush(); + _exit(1); +} +void die_starttls() +{ + out("454 TLS not available due to temporary reason (#5.7.3)\r\n"); + flush(); + _exit(1); +} +void die_recipients() +{ + out("421 unable to check recipients (#4.3.0)\r\n"); + flush(); + _exit(1); +} -void err_unimpl() { out("500 unimplemented (#5.5.1)\r\n"); } -void err_syntax() { out("555 syntax error (#5.5.4)\r\n"); } -void err_noop() { out("250 ok\r\n"); } -void err_vrfy() { out("252 send some mail, i'll try my best\r\n"); } -void err_qqt() { out("451 qqt failure (#4.3.0)\r\n"); } +void err_unimpl() +{ + out("500 unimplemented (#5.5.1)\r\n"); +} +void err_syntax() +{ + out("555 syntax error (#5.5.4)\r\n"); +} +void err_noop() +{ + out("250 ok\r\n"); +} +void err_vrfy() +{ + out("252 send some mail, i'll try my best\r\n"); +} +void err_qqt() +{ + out("451 qqt failure (#4.3.0)\r\n"); +} -int err_child() { out("454 problem with child and I can't auth (#4.3.0)\r\n"); return -1; } -int err_fork() { out("454 child won't start and I can't auth (#4.3.0)\r\n"); return -1; } -int err_pipe() { out("454 unable to open pipe and I can't auth (#4.3.0)\r\n"); return -1; } -int err_write() { out("454 unable to write pipe and I can't auth (#4.3.0)\r\n"); return -1; } +int err_child() +{ + out("454 problem with child and I can't auth (#4.3.0)\r\n"); + return -1; +} +int err_fork() +{ + out("454 child won't start and I can't auth (#4.3.0)\r\n"); + return -1; +} +int err_pipe() +{ + out("454 unable to open pipe and I can't auth (#4.3.0)\r\n"); + return -1; +} +int err_write() +{ + out("454 unable to write pipe and I can't auth (#4.3.0)\r\n"); + return -1; +} -int err_postgl() { out("454 problem with child and I can't greylist (#4.3.0)\r\n"); return -1; } -int err_forkgl() { out("454 problem with child and I can't greylist (#4.3.0)\r\n"); return -1; } +int err_postgl() +{ + out("454 problem with child and I can't greylist (#4.3.0)\r\n"); + return -1; +} +int err_forkgl() +{ + out("454 problem with child and I can't greylist (#4.3.0)\r\n"); + return -1; +} /* TLS */ -int err_starttls() -{ +int err_starttls() +{ out("454 TLS not available due to temporary reason (#5.7.3)\r\n"); - _exit(1); + _exit(1); } -void err_tlsreq(char *s1,char *s2,char *s3,char *s4,char *s5) +void err_tlsreq(char *s1, char *s2, char *s3, char *s4, char *s5) { - out("535 STARTTLS required (#5.7.1)\r\n"); - smtp_logh(s1,s2,s3,s4,s5); + out("535 STARTTLS required (#5.7.1)\r\n"); + smtp_logh(s1, s2, s3, s4, s5); } /* Helo */ -void err_helo(char *s1,char *s2,char *s3,char *s4,char *s5,char *s6,char *s7,char *s8) +void err_helo(char *s1, char *s2, char *s3, char *s4, char *s5, char *s6, char *s7, char *s8) { out("550 sorry, invalid HELO/EHLO greeting "); if (reply550hlo) out(reply550hlo); out(" (#5.7.1)\r\n"); - smtp_logi(s1,s2,s3,s4,s5,s6,s7,s8); - } + smtp_logi(s1, s2, s3, s4, s5, s6, s7, s8); +} /* Auth */ -void err_authsetup(char *s1,char *s2,char *s3,char *s4,char *s5) +void err_authsetup(char *s1, char *s2, char *s3, char *s4, char *s5) +{ + out("530 Auth not available (#5.7.1)\r\n"); + smtp_logh(s1, s2, s3, s4, s5); +} +void err_authd() { - out("530 Auth not available (#5.7.1)\r\n"); - smtp_logh(s1,s2,s3,s4,s5); -} -void err_authd() -{ out("503 you're already authenticated (#5.5.0)\r\n"); } -void err_authmail() -{ +void err_authmail() +{ out("503 no auth during mail transaction (#5.5.0)\r\n"); } -void err_authfail(char *s1,char *s2,char *s3,char *s4,char *s5,char *s6,char *s7) +void err_authfail(char *s1, char *s2, char *s3, char *s4, char *s5, char *s6, char *s7) { - out("535 authentication failed (#5.7.1)\r\n"); smtp_logb(s1,s2,s3,s4,s5,s6,s7); + out("535 authentication failed (#5.7.1)\r\n"); + smtp_logb(s1, s2, s3, s4, s5, s6, s7); } -void err_authreq(char *s1,char *s2,char *s3,char *s4,char *s5) +void err_authreq(char *s1, char *s2, char *s3, char *s4, char *s5) { - out("535 authentication required (#5.7.1)\r\n"); smtp_logh(s1,s2,s3,s4,s5); + out("535 authentication required (#5.7.1)\r\n"); + smtp_logh(s1, s2, s3, s4, s5); } -void err_submission(char *s1,char *s2,char *s3,char *s4,char *s5) -{ - out("530 Authorization required (#5.7.1) \r\n"); smtp_logh(s1,s2,s3,s4,s5); +void err_submission(char *s1, char *s2, char *s3, char *s4, char *s5) +{ + out("530 Authorization required (#5.7.1) \r\n"); + smtp_logh(s1, s2, s3, s4, s5); } -int err_authabort() -{ +int err_authabort() +{ out("501 auth exchange canceled (#5.0.0)\r\n"); return -1; } -int err_authinput() -{ +int err_authinput() +{ out("501 malformed auth input (#5.5.4)\r\n"); return -1; } -void err_authinvalid(char *s1,char *s2,char *s3,char *s4,char *s5) -{ +void err_authinvalid(char *s1, char *s2, char *s3, char *s4, char *s5) +{ out("504 auth type unimplemented (#5.5.1)\r\n"); - smtp_logh(s1,s2,s3,s4,s5); + smtp_logh(s1, s2, s3, s4, s5); } -int err_noauth() -{ +int err_noauth() +{ out("504 auth type unimplemented (#5.5.1)\r\n"); return -1; } /* Mail From: */ -void err_wantmail() { out("503 MAIL first (#5.5.1)\r\n"); } +void err_wantmail() +{ + out("503 MAIL first (#5.5.1)\r\n"); +} -void err_mav(char *s1,char *s2,char *s3,char *s4,char *s5,char *s6,char *s7) +void err_mav(char *s1, char *s2, char *s3, char *s4, char *s5, char *s6, char *s7) { out("553 sorry, invalid sender address specified "); if (reply553inv) out(reply553inv); out(" (#5.7.1)\r\n"); - smtp_logg(s1,s2,s3,s4,s5,s6,s7); + smtp_logg(s1, s2, s3, s4, s5, s6, s7); } -void err_bmf(char *s1,char *s2,char *s3,char *s4,char *s5,char *s6,char *s7,char *s8) +void err_bmf(char *s1, char *s2, char *s3, char *s4, char *s5, char *s6, char *s7, char *s8) { out("553 sorry, your envelope sender is in my badmailfrom list "); if (reply553bmf) out(reply553bmf); out(" (#5.7.1)\r\n"); - smtp_logi(s1,s2,s3,s4,s5,s6,s7,s8); + smtp_logi(s1, s2, s3, s4, s5, s6, s7, s8); } -void err_mfdns(char *s1,char *s2,char *s3,char *s4,char *s5,char *s6,char *s7) +void err_mfdns(char *s1, char *s2, char *s3, char *s4, char *s5, char *s6, char *s7) { out("553 sorry, your envelope sender must exist "); if (reply553env) out(reply553env); out(" (#5.7.1)\r\n"); - smtp_logg(s1,s2,s3,s4,s5,s6,s7); + smtp_logg(s1, s2, s3, s4, s5, s6, s7); } /* SPF */ -void err_spf(char *s1,char *s2,char *s3,char *s4,char *s5,char *s6,char *s7,char *msg) +void err_spf(char *s1, char *s2, char *s3, char *s4, char *s5, char *s6, char *s7, char *msg) { int i, j; int len = str_len(msg); @@ -196,76 +365,80 @@ void err_spf(char *s1,char *s2,char *s3,char *s4,char *s5,char *s6,char *s7,char out("550 "); out(msg); } - } + } out(" (#5.7.1)\r\n"); - smtp_logg(s1,s2,s3,s4,s5,s6,s7); + smtp_logg(s1, s2, s3, s4, s5, s6, s7); } /* Rcpt To: */ -void err_wantrcpt() { out("503 RCPT first (#5.5.1)\r\n"); } +void err_wantrcpt() +{ + out("503 RCPT first (#5.5.1)\r\n"); +} -void postgrey(char *s1,char *s2,char *s3,char *s4,char *s5,char *s6,char *s7) +void postgrey(char *s1, char *s2, char *s3, char *s4, char *s5, char *s6, char *s7) { out("421 greylisted"); if (reply421pgl) out(reply421pgl); out(" (#4.3.0)\r\n"); - smtp_logg(s1,s2,s3,s4,s5,s6,s7); + smtp_logg(s1, s2, s3, s4, s5, s6, s7); } -void err_nogateway(char *s1,char *s2,char *s3,char *s4,char *s5,char *s6,char *s7) +void err_nogateway(char *s1, char *s2, char *s3, char *s4, char *s5, char *s6, char *s7) { out("553 sorry, that domain isn't in my list of allowed rcpthosts "); if (reply553ngw) out(reply553ngw); out(" (#5.7.1)\r\n"); - smtp_logg(s1,s2,s3,s4,s5,s6,s7); + smtp_logg(s1, s2, s3, s4, s5, s6, s7); } -void err_brt(char *s1,char *s2,char *s3,char *s4,char *s5,char *s6,char *s7) +void err_brt(char *s1, char *s2, char *s3, char *s4, char *s5, char *s6, char *s7) { out("553 sorry, your envelope recipient is in my badrcptto list "); if (reply553brt) out(reply553brt); out(" (#5.7.1)\r\n"); - smtp_logg(s1,s2,s3,s4,s5,s6,s7); + smtp_logg(s1, s2, s3, s4, s5, s6, s7); } -void err_rcpts(char *s1,char *s2,char *s3,char *s4,char *s5,char *s6,char *s7) +void err_rcpts(char *s1, char *s2, char *s3, char *s4, char *s5, char *s6, char *s7) { - out("452 sorry, too many recipients (#4.5.3)\r\n"); /* RFC 5321 */ - smtp_logg(s1,s2,s3,s4,s5,s6,s7); + out("452 sorry, too many recipients (#4.5.3)\r\n"); /* RFC 5321 */ + smtp_logg(s1, s2, s3, s4, s5, s6, s7); } -void err_recipient(char *s1,char *s2,char *s3,char *s4,char *s5,char *s6,char *s7) +void err_recipient(char *s1, char *s2, char *s3, char *s4, char *s5, char *s6, char *s7) { if (env_get("RECIPIENTS450")) out("450 sorry, mailbox currently unavailable (#4.2.1)\r\n"); else { out("550 sorry, no mailbox by that name "); - if (reply550mbx) out(reply550mbx); out(" (#5.7.1)\r\n"); + if (reply550mbx) out(reply550mbx); + out(" (#5.7.1)\r\n"); } - smtp_logg(s1,s2,s3,s4,s5,s6,s7); + smtp_logg(s1, s2, s3, s4, s5, s6, s7); } /* Data */ -void straynewline() +void straynewline() { out("451 Bare Line Feeds (LF) are not accepted in SMTP; CRLF is required according to RFC 2822.\r\n"); - flush(); - _exit(1); + flush(); + _exit(1); } -void err_notorious() -{ +void err_notorious() +{ out("503 DATA command not accepted at this time (#5.5.1)\r\n"); flush(); _exit(1); } -void err_size(char *s1,char *s2,char *s3,char *s4,char *s5,char *s6,char *s7) +void err_size(char *s1, char *s2, char *s3, char *s4, char *s5, char *s6, char *s7) { out("552 sorry, that message size exceeds my databytes limit (#5.3.4)\r\n"); - smtp_logg(s1,s2,s3,s4,s5,s6,s7); + smtp_logg(s1, s2, s3, s4, s5, s6, s7); } -void err_data(char *s1,char *s2,char *s3,char *s4,char *s5,char *s6,char *s7,char *s8) +void err_data(char *s1, char *s2, char *s3, char *s4, char *s5, char *s6, char *s7, char *s8) { out("554 sorry, invalid message content "); if (reply554cnt) out(reply554cnt); out(" (#5.3.2)\r\n"); - smtp_logi(s1,s2,s3,s4,s5,s6,s7,s8); + smtp_logi(s1, s2, s3, s4, s5, s6, s7, s8); } diff --git a/src/spawn.c b/src/spawn.c index effcb26..5315637 100644 --- a/src/spawn.c +++ b/src/spawn.c @@ -1,37 +1,37 @@ -#include <sys/types.h> #include <sys/stat.h> +#include <sys/types.h> #include <unistd.h> + #include "alloc.h" -#include "sig.h" -#include "wait.h" #include "buffer.h" #include "byte.h" -#include "str.h" -#include "stralloc.h" -#include "select.h" +#include "error.h" #include "exit.h" #include "fd.h" #include "open.h" -#include "error.h" +#include "select.h" +#include "sig.h" +#include "str.h" +#include "stralloc.h" +#include "wait.h" + #include "auto_qmail.h" -#include "auto_uids.h" #include "auto_spawn.h" +#include "auto_uids.h" extern int truncreport; extern int spawn(); extern void report(); extern void initialize(); -struct delivery - { +struct delivery { int used; - int fdin; /* pipe input */ - int pid; /* zero if child is dead */ + int fdin; /* pipe input */ + int pid; /* zero if child is dead */ int wstat; /* if !pid: status of child */ int fdout; /* pipe output, -1 if !pid; delays eof until after death */ stralloc output; - } -; +}; struct delivery *d; @@ -42,32 +42,35 @@ void sigchld() int i; while ((pid = wait_nohang(&wstat)) > 0) - for (i = 0; i < auto_spawn; ++i) + for (i = 0; i < auto_spawn; ++i) if (d[i].used) if (d[i].pid == pid) { - close(d[i].fdout); d[i].fdout = -1; - d[i].wstat = wstat; d[i].pid = 0; + close(d[i].fdout); + d[i].fdout = -1; + d[i].wstat = wstat; + d[i].pid = 0; } -} +} int flagwriting = 1; -ssize_t okwrite(int fd,char *buf,int n) +ssize_t okwrite(int fd, char *buf, int n) { int w; if (!flagwriting) return n; - w = write(fd,buf,n); + w = write(fd, buf, n); if (w != -1) return w; if (errno == EINTR) return -1; - flagwriting = 0; close(fd); + flagwriting = 0; + close(fd); return n; } int flagreading = 1; -char outbuf[1024]; +char outbuf[1024]; buffer bo; -int stage = 0; /* reading 0:delnum 1:messid 2:sender 3:recip */ +int stage = 0; /* reading 0:delnum 1:messid 2:sender 3:recip */ int flagabort = 0; /* if 1, everything except delnum is garbage */ int delnum; stralloc messid = {0}; @@ -76,12 +79,12 @@ stralloc recip = {0}; void err(char *s) { - char ch; - - ch = delnum; - buffer_put(&bo,&ch,1); - buffer_puts(&bo,s); - buffer_putflush(&bo,"",1); + char ch; + + ch = delnum; + buffer_put(&bo, &ch, 1); + buffer_puts(&bo, s); + buffer_putflush(&bo, "", 1); } void docmd() @@ -93,57 +96,105 @@ void docmd() int pi[2]; struct stat st; - if (flagabort) { err("Zqmail-spawn: Out of memory. (#4.3.0)\n"); return; } - if (delnum < 0) { err("Zqmail-spawn: Internal error: delnum negative. (#4.3.5)\n"); return; } - if (delnum >= auto_spawn) { err("Zqmail-spawn: Internal error: delnum too big. (#4.3.5)\n"); return; } - if (d[delnum].used) { err("Zqmail-spawn: Internal error: delnum in use. (#4.3.5)\n"); return; } - + if (flagabort) { + err("Zqmail-spawn: Out of memory. (#4.3.0)\n"); + return; + } + if (delnum < 0) { + err("Zqmail-spawn: Internal error: delnum negative. (#4.3.5)\n"); + return; + } + if (delnum >= auto_spawn) { + err("Zqmail-spawn: Internal error: delnum too big. (#4.3.5)\n"); + return; + } + if (d[delnum].used) { + err("Zqmail-spawn: Internal error: delnum in use. (#4.3.5)\n"); + return; + } + for (i = 0; i < messid.len; ++i) if (messid.s[i]) if (!i || (messid.s[i] != '/')) - if ((unsigned char) (messid.s[i] - '0') > 9) - { err("Dqmail-spawn: Internal error: messid has nonnumerics. (#5.3.5)\n"); return; } + if ((unsigned char)(messid.s[i] - '0') > 9) { + err("Dqmail-spawn: Internal error: messid has nonnumerics. (#5.3.5)\n"); + return; + } - if (messid.len > 100) { err("Dqmail-spawn: Internal error: messid too long. (#5.3.5)\n"); return; } - if (!messid.s[0]) { err("Dqmail-spawn: Internal error: messid too short. (#5.3.5)\n"); return; } + if (messid.len > 100) { + err("Dqmail-spawn: Internal error: messid too long. (#5.3.5)\n"); + return; + } + if (!messid.s[0]) { + err("Dqmail-spawn: Internal error: messid too short. (#5.3.5)\n"); + return; + } - if (!stralloc_copys(&d[delnum].output,"")) - { err("Zqmail-spawn: Out of memory. (#4.3.0)\n"); return; } + if (!stralloc_copys(&d[delnum].output, "")) { + err("Zqmail-spawn: Out of memory. (#4.3.0)\n"); + return; + } - j = byte_rchr(recip.s,recip.len,'@'); - if (j >= recip.len) { err("DSorry, address must include host name. (#5.1.3)\n"); return; } + j = byte_rchr(recip.s, recip.len, '@'); + if (j >= recip.len) { + err("DSorry, address must include host name. (#5.1.3)\n"); + return; + } fdmess = open_read(messid.s); - if (fdmess == -1) { err("Zqmail-spawn: Unable to open message. (#4.3.0)\n"); return; } + if (fdmess == -1) { + err("Zqmail-spawn: Unable to open message. (#4.3.0)\n"); + return; + } - if (fstat(fdmess,&st) == -1) - { close(fdmess); err("Zqmail-spawn: Unable to fstat message. (#4.3.0)\n"); return; } - if ((st.st_mode & S_IFMT) != S_IFREG) - { close(fdmess); err("ZSorry, message has wrong type. (#4.3.5)\n"); return; } + if (fstat(fdmess, &st) == -1) { + close(fdmess); + err("Zqmail-spawn: Unable to fstat message. (#4.3.0)\n"); + return; + } + if ((st.st_mode & S_IFMT) != S_IFREG) { + close(fdmess); + err("ZSorry, message has wrong type. (#4.3.5)\n"); + return; + } if (st.st_uid != auto_uidq) /* aaack! qmailq has to be trusted! */ - /* your security is already toast at this point. damage control... */ - { close(fdmess); err("ZSorry, message has wrong owner. (#4.3.5)\n"); return; } + /* your security is already toast at this point. damage control... */ + { + close(fdmess); + err("ZSorry, message has wrong owner. (#4.3.5)\n"); + return; + } if (pipe(pi) == -1) { - if (errno == EFAULT) err("Zqmail-spawn: Unable to create pipe (wrong fildes). (#4.3.0)\n"); - else if (errno == EMFILE) err("Zqmail-spawn: Unable to create pipe (too many FDS). (#4.3.0)\n"); - else if (errno == ENFILE) err("Zqmail-spawn: Unable to create pipe (system file table full). (#4.3.0)\n"); - else if (errno == ENOMEM) err("Zqmail-spawn: Unable to create pipe (out of memory). (#4.3.0)\n"); - else err("Zqmail-spawn: Unable to create pipe (unkown reason). (#4.3.0)\n"); - close(fdmess); - return; + if (errno == EFAULT) + err("Zqmail-spawn: Unable to create pipe (wrong fildes). (#4.3.0)\n"); + else if (errno == EMFILE) + err("Zqmail-spawn: Unable to create pipe (too many FDS). (#4.3.0)\n"); + else if (errno == ENFILE) + err("Zqmail-spawn: Unable to create pipe (system file table full). (#4.3.0)\n"); + else if (errno == ENOMEM) + err("Zqmail-spawn: Unable to create pipe (out of memory). (#4.3.0)\n"); + else + err("Zqmail-spawn: Unable to create pipe (unkown reason). (#4.3.0)\n"); + close(fdmess); + return; } fd_coe(pi[0]); - f = spawn(fdmess,pi[1],sender.s,recip.s,j); + f = spawn(fdmess, pi[1], sender.s, recip.s, j); close(fdmess); - if (f == -1) - { close(pi[0]); close(pi[1]); err("Zqmail-spawn: Unable to fork. (#4.3.0)\n"); return; } + if (f == -1) { + close(pi[0]); + close(pi[1]); + err("Zqmail-spawn: Unable to fork. (#4.3.0)\n"); + return; + } d[delnum].fdin = pi[0]; - d[delnum].fdout = pi[1]; fd_coe(pi[1]); + d[delnum].fdout = pi[1]; + fd_coe(pi[1]); d[delnum].pid = f; d[delnum].used = 1; } @@ -156,41 +207,50 @@ void getcmd() int r; char ch; - r = read(0,cmdbuf,sizeof(cmdbuf)); - if (r == 0) - { flagreading = 0; return; } + r = read(0, cmdbuf, sizeof(cmdbuf)); + if (r == 0) { + flagreading = 0; + return; + } if (r == -1) { - if (errno != EINTR) - flagreading = 0; + if (errno != EINTR) flagreading = 0; return; } - + for (i = 0; i < r; ++i) { ch = cmdbuf[i]; switch (stage) { case 0: - delnum = (unsigned int) (unsigned char) ch; - messid.len = 0; stage = 1; break; + delnum = (unsigned int)(unsigned char)ch; + messid.len = 0; + stage = 1; + break; case 1: - if (!stralloc_append(&messid,&ch)) flagabort = 1; + if (!stralloc_append(&messid, &ch)) flagabort = 1; if (ch) break; - sender.len = 0; stage = 2; break; + sender.len = 0; + stage = 2; + break; case 2: - if (!stralloc_append(&sender,&ch)) flagabort = 1; + if (!stralloc_append(&sender, &ch)) flagabort = 1; if (ch) break; - recip.len = 0; stage = 3; break; + recip.len = 0; + stage = 3; + break; case 3: - if (!stralloc_append(&recip,&ch)) flagabort = 1; + if (!stralloc_append(&recip, &ch)) flagabort = 1; if (ch) break; docmd(); - flagabort = 0; stage = 0; break; + flagabort = 0; + stage = 0; + break; } } } char inbuf[128]; -int main(int argc,char **argv) +int main(int argc, char **argv) { char ch; int i; @@ -200,77 +260,78 @@ int main(int argc,char **argv) if (chdir(auto_qmail) == -1) _exit(110); if (chdir("queue/mess") == -1) _exit(110); - if (!stralloc_copys(&messid,"")) _exit(111); - if (!stralloc_copys(&sender,"")) _exit(111); - if (!stralloc_copys(&recip,"")) _exit(111); + if (!stralloc_copys(&messid, "")) _exit(111); + if (!stralloc_copys(&sender, "")) _exit(111); + if (!stralloc_copys(&recip, "")) _exit(111); - d = (struct delivery *) alloc((auto_spawn + 10) * sizeof(struct delivery)); + d = (struct delivery *)alloc((auto_spawn + 10) * sizeof(struct delivery)); if (!d) _exit(111); - buffer_init(&bo,okwrite,1,outbuf,sizeof(outbuf)); + buffer_init(&bo, okwrite, 1, outbuf, sizeof(outbuf)); sig_pipeignore(); sig_childcatch(sigchld); - initialize(argc,argv); + initialize(argc, argv); - ch = auto_spawn; - buffer_putflush(&bo,&ch,1); + ch = auto_spawn; + buffer_putflush(&bo, &ch, 1); - for (i = 0; i < auto_spawn; ++i) - { d[i].used = 0; d[i].output.s = 0; } + for (i = 0; i < auto_spawn; ++i) { + d[i].used = 0; + d[i].output.s = 0; + } for (;;) { if (!flagreading) { - for (i = 0; i < auto_spawn; ++i) if (d[i].used) break; + for (i = 0; i < auto_spawn; ++i) + if (d[i].used) break; if (i >= auto_spawn) _exit(0); } sig_childunblock(); FD_ZERO(&rfds); - if (flagreading) FD_SET(0,&rfds); + if (flagreading) FD_SET(0, &rfds); nfds = 1; - for (i = 0; i < auto_spawn; ++i) - if (d[i].used) { - FD_SET(d[i].fdin,&rfds); - if (d[i].fdin >= nfds) - nfds = d[i].fdin + 1; + for (i = 0; i < auto_spawn; ++i) + if (d[i].used) { + FD_SET(d[i].fdin, &rfds); + if (d[i].fdin >= nfds) nfds = d[i].fdin + 1; } - r = select(nfds,&rfds,(fd_set *) 0,(fd_set *) 0,(struct timeval *) 0); + r = select(nfds, &rfds, (fd_set *)0, (fd_set *)0, (struct timeval *)0); sig_childblock(); if (r != -1) { if (flagreading) - if (FD_ISSET(0,&rfds)) getcmd(); - for (i = 0; i < auto_spawn; ++i) + if (FD_ISSET(0, &rfds)) getcmd(); + for (i = 0; i < auto_spawn; ++i) if (d[i].used) - if (FD_ISSET(d[i].fdin,&rfds)) { - r = read(d[i].fdin,inbuf,128); - if (r == -1) - continue; /* read error on a readable pipe? be serious */ - if (r == 0) { - ch = i; - buffer_put(&bo,&ch,1); - report(&bo,d[i].wstat,d[i].output.s,d[i].output.len); - buffer_put(&bo,"",1); - buffer_flush(&bo); - close(d[i].fdin); d[i].used = 0; - continue; - } - while (!stralloc_readyplus(&d[i].output,r)) - sleep(10); /*XXX*/ - byte_copy(d[i].output.s + d[i].output.len,r,inbuf); - d[i].output.len += r; - if (truncreport > 100) - if (d[i].output.len > truncreport) { - char *truncmess = "\nError report too long, sorry.\n"; - d[i].output.len = truncreport - str_len(truncmess) - 3; - stralloc_cats(&d[i].output,truncmess); + if (FD_ISSET(d[i].fdin, &rfds)) { + r = read(d[i].fdin, inbuf, 128); + if (r == -1) continue; /* read error on a readable pipe? be serious */ + if (r == 0) { + ch = i; + buffer_put(&bo, &ch, 1); + report(&bo, d[i].wstat, d[i].output.s, d[i].output.len); + buffer_put(&bo, "", 1); + buffer_flush(&bo); + close(d[i].fdin); + d[i].used = 0; + continue; + } + while (!stralloc_readyplus(&d[i].output, r)) sleep(10); /*XXX*/ + byte_copy(d[i].output.s + d[i].output.len, r, inbuf); + d[i].output.len += r; + if (truncreport > 100) + if (d[i].output.len > truncreport) { + char *truncmess = "\nError report too long, sorry.\n"; + d[i].output.len = truncreport - str_len(truncmess) - 3; + stralloc_cats(&d[i].output, truncmess); + } } - } } } } @@ -1,34 +1,36 @@ -#include "stralloc.h" +#include "spf.h" + #include "alloc.h" +#include "byte.h" +#include "case.h" +#include "fmt.h" #include "ip.h" +#include "scan.h" +#include "str.h" +#include "stralloc.h" + +#include "dns.h" #include "ipalloc.h" #include "ipme.h" -#include "str.h" -#include "fmt.h" -#include "scan.h" -#include "byte.h" #include "now.h" -#include "dns.h" -#include "case.h" -#include "spf.h" /* long lived SPF variables (output) */ -stralloc spfinfo = {0}; /* SPF results - see spf.h */ -stralloc spfrecord = {0}; /* Used for diagnostics */ +stralloc spfinfo = {0}; /* SPF results - see spf.h */ +stralloc spfrecord = {0}; /* Used for diagnostics */ /* s/qmail control SPF variables (input) */ -stralloc spflocalrules; /* Local rules provided here */ -stralloc spfexplain; /* Default SPF_EXPMSG in spf.h */ +stralloc spflocalrules; /* Local rules provided here */ +stralloc spfexplain; /* Default SPF_EXPMSG in spf.h */ -stralloc spfmf = {0}; /* aka envelopefrom = clientid */ -stralloc spfhelo = {0}; /* helo or domain part for spfmf */ -stralloc dnsname = {0}; /* FQDN of client host in DNS */ -stralloc spflocal = {0}; /* Receiving host */ +stralloc spfmf = {0}; /* aka envelopefrom = clientid */ +stralloc spfhelo = {0}; /* helo or domain part for spfmf */ +stralloc dnsname = {0}; /* FQDN of client host in DNS */ +stralloc spflocal = {0}; /* Receiving host */ -stralloc spfexpmsg = {0}; /* additional explanation given as 5xx SMTP response */ -stralloc expdomain = {0}; /* the domain, for which explanation is given */ +stralloc spfexpmsg = {0}; /* additional explanation given as 5xx SMTP response */ +stralloc expdomain = {0}; /* the domain, for which explanation is given */ int flagip6; stralloc domain = {0}; @@ -55,75 +57,81 @@ Exists+Expand: exists.com TXT "v=spf1 exists:%{ir}.%{l1r+-}._spf.%{d} -all" */ -int spf_query(const char *remoteip,const char *helo,const char *mf,const char *local,const int flagip) +int spf_query( + const char *remoteip, const char *helo, const char *mf, const char *local, const int flagip) { int at; int r = SPF_INIT; flagip6 = flagip; - if (!stralloc_copys(&spfinfo," ")) return SPF_NOMEM; - + if (!stralloc_copys(&spfinfo, " ")) return SPF_NOMEM; + switch (flagip6) { - case -1: if (!spf_info("MLocal=",remoteip)) return SPF_NOMEM; - if (!spf_info("R:","+")) return SPF_NOMEM; - break; - case 0: if (!ip4_scan(remoteip,ip4remote)) return SPF_SYNTAX; - if (ipme_is4(ip4remote) == 1) { - if (!spf_info("MLocal=",remoteip)) return SPF_NOMEM; - if (!spf_info("R:","+")) return SPF_NOMEM; - return SPF_ME; - } break; - case 1: if (!ip6_scan(remoteip,ip6remote)) return SPF_SYNTAX; - if (ipme_is6(ip6remote) == 1) { - if (!spf_info("MLocal=",remoteip)) return SPF_NOMEM; - if (!spf_info("R:","+")) return SPF_NOMEM; - return SPF_ME; - } break; + case -1: + if (!spf_info("MLocal=", remoteip)) return SPF_NOMEM; + if (!spf_info("R:", "+")) return SPF_NOMEM; + break; + case 0: + if (!ip4_scan(remoteip, ip4remote)) return SPF_SYNTAX; + if (ipme_is4(ip4remote) == 1) { + if (!spf_info("MLocal=", remoteip)) return SPF_NOMEM; + if (!spf_info("R:", "+")) return SPF_NOMEM; + return SPF_ME; + } + break; + case 1: + if (!ip6_scan(remoteip, ip6remote)) return SPF_SYNTAX; + if (ipme_is6(ip6remote) == 1) { + if (!spf_info("MLocal=", remoteip)) return SPF_NOMEM; + if (!spf_info("R:", "+")) return SPF_NOMEM; + return SPF_ME; + } + break; } if (helo && str_len(helo)) { - if (!stralloc_copys(&spfhelo,helo)) return SPF_NOMEM; + if (!stralloc_copys(&spfhelo, helo)) return SPF_NOMEM; } else { - if (!stralloc_copys(&spfhelo,"unknown")) return SPF_NOMEM; + if (!stralloc_copys(&spfhelo, "unknown")) return SPF_NOMEM; } if (!stralloc_0(&spfhelo)) return SPF_NOMEM; if (mf && str_len(mf)) { - if (!stralloc_copys(&spfmf,mf)) return SPF_NOMEM; + if (!stralloc_copys(&spfmf, mf)) return SPF_NOMEM; if (!stralloc_0(&spfmf)) return SPF_NOMEM; - at = str_rchr(spfmf.s,'@'); + at = str_rchr(spfmf.s, '@'); if (spfmf.s[at] == '@') { - if (!stralloc_copys(&domain,spfmf.s + at + 1)) return SPF_NOMEM; + if (!stralloc_copys(&domain, spfmf.s + at + 1)) return SPF_NOMEM; } else { -// if (!stralloc_0(&spfhelo)) return SPF_NOMEM; - if (!stralloc_copys(&domain,&spfhelo)) return SPF_NOMEM; + // if (!stralloc_0(&spfhelo)) return SPF_NOMEM; + if (!stralloc_copys(&domain, &spfhelo)) return SPF_NOMEM; } - if (!stralloc_copy(&identity,&domain)) return SPF_NOMEM; + if (!stralloc_copy(&identity, &domain)) return SPF_NOMEM; } if (!stralloc_0(&identity)) return SPF_NOMEM; if (local && str_len(local)) { - if (!stralloc_copys(&spflocal,local)) return SPF_NOMEM; + if (!stralloc_copys(&spflocal, local)) return SPF_NOMEM; } else { - if (!stralloc_copys(&spflocal,"localhost")) return SPF_NOMEM; + if (!stralloc_copys(&spflocal, "localhost")) return SPF_NOMEM; } if (!stralloc_0(&spflocal)) return SPF_NOMEM; - if (!spf_info("S=",remoteip)) return SPF_NOMEM; - if (!spf_info("O=",spfmf.s)) return SPF_NOMEM; - if (!spf_info("C=",identity.s)) return SPF_NOMEM; - if (!spf_info("H=",spfhelo.s)) return SPF_NOMEM; + if (!spf_info("S=", remoteip)) return SPF_NOMEM; + if (!spf_info("O=", spfmf.s)) return SPF_NOMEM; + if (!spf_info("C=", identity.s)) return SPF_NOMEM; + if (!spf_info("H=", spfhelo.s)) return SPF_NOMEM; - if (!stralloc_copy(&spfexpmsg,&spfexplain)) return SPF_NOMEM; - if (!stralloc_0(&spfexpmsg)) return SPF_NOMEM; + if (!stralloc_copy(&spfexpmsg, &spfexplain)) return SPF_NOMEM; + if (!stralloc_0(&spfexpmsg)) return SPF_NOMEM; recursion = 0; dnsname.len = 0; if (r == SPF_INIT) r = spf_lookup(&domain); if (r == SPF_LOOP) { - if (!spf_info("P=","Maximum nesting level exceeded; possible loop")) return SPF_NOMEM; - if (!spf_info("R:","e")) return SPF_NOMEM; + if (!spf_info("P=", "Maximum nesting level exceeded; possible loop")) return SPF_NOMEM; + if (!spf_info("R:", "e")) return SPF_NOMEM; } if (r < 0) r = SPF_UNKNOWN; /* return 2main */ @@ -136,14 +144,14 @@ static struct spf_aliases { char *alias; int defrc; } spf_aliases[] = { - { "allow", SPF_OK } -, { "pass", SPF_OK } -, { "deny", SPF_FAIL } -, { "softdeny",SPF_SOFTFAIL } -, { "fail", SPF_FAIL } -, { "softfail",SPF_SOFTFAIL } -, { "unknown", SPF_NEUTRAL } -, { 0, SPF_UNKNOWN } + { "allow", SPF_OK}, + { "pass", SPF_OK}, + { "deny", SPF_FAIL}, + {"softdeny", SPF_SOFTFAIL}, + { "fail", SPF_FAIL}, + {"softfail", SPF_SOFTFAIL}, + { "unknown", SPF_NEUTRAL}, + { 0, SPF_UNKNOWN} }; /** @@ -155,7 +163,7 @@ static struct spf_aliases { @return int r = SPF return code */ -int spf_lookup(stralloc *domain) +int spf_lookup(stralloc *domain) { stralloc spfdata = {0}; stralloc sa = {0}; @@ -164,7 +172,7 @@ int spf_lookup(stralloc *domain) int local_pos = -1; int localrules = 0; int q = -1; - int i, r; + int i, r; int begin, pos; int spfrc; int done; @@ -172,64 +180,64 @@ int spf_lookup(stralloc *domain) /* Fallthrough result */ - REDIRECT: - if (++recursion > LOOKUP_LIMIT) return SPF_EXHAUST; +REDIRECT: + if (++recursion > LOOKUP_LIMIT) return SPF_EXHAUST; - if (!stralloc_copys(&expdomain,domain->s)) return SPF_NOMEM; // *FIXME */ + if (!stralloc_copys(&expdomain, domain->s)) return SPF_NOMEM; // *FIXME */ - if (!stralloc_copys(&spfdata,"")) return SPF_NOMEM; - r = spf_records(&spfdata,domain); + if (!stralloc_copys(&spfdata, "")) return SPF_NOMEM; + r = spf_records(&spfdata, domain); if (!stralloc_0(domain)) return SPF_NOMEM; - if (first) if (!stralloc_copys(&spfrecord,"")) return SPF_NOMEM; - if (!stralloc_cats(&spfrecord,"(")) return SPF_NOMEM; - if (!stralloc_cat(&spfrecord,domain)) return SPF_NOMEM; - if (!stralloc_cats(&spfrecord,")")) return SPF_NOMEM; - if (!stralloc_cats(&spfrecord," => ")) return SPF_NOMEM; - if (!stralloc_cat(&spfrecord,&spfdata)) return SPF_NOMEM; - if (!stralloc_cats(&spfrecord,"\n")) return SPF_NOMEM; + if (first) + if (!stralloc_copys(&spfrecord, "")) return SPF_NOMEM; + if (!stralloc_cats(&spfrecord, "(")) return SPF_NOMEM; + if (!stralloc_cat(&spfrecord, domain)) return SPF_NOMEM; + if (!stralloc_cats(&spfrecord, ")")) return SPF_NOMEM; + if (!stralloc_cats(&spfrecord, " => ")) return SPF_NOMEM; + if (!stralloc_cat(&spfrecord, &spfdata)) return SPF_NOMEM; + if (!stralloc_cats(&spfrecord, "\n")) return SPF_NOMEM; if (!stralloc_0(&spfrecord)) return SPF_NOMEM; /* In spite of none-existing SPF data, use local rules as substitude */ - if (r == SPF_NONE) { /* No SPF records published */ - if (!first) { - return r; + if (r == SPF_NONE) { /* No SPF records published */ + if (!first) { + return r; } else { spfdata.len = 0; } - if (localrules) { /* append local ruleset */ + if (localrules) { /* append local ruleset */ local_pos = spfdata.len; - if (!stralloc_cats(&spfdata,spflocalrules.s)) return SPF_NOMEM; + if (!stralloc_cats(&spfdata, spflocalrules.s)) return SPF_NOMEM; } if (!stralloc_0(&spfdata)) return SPF_NOMEM; - if (!stralloc_copys(&expdomain,"")) return SPF_NOMEM; - - } else if (r == SPF_OK) { /* SPF records published */ + if (!stralloc_copys(&expdomain, "")) return SPF_NOMEM; + + } else if (r == SPF_OK) { /* SPF records published */ if (!stralloc_0(&spfdata)) return SPF_NOMEM; r = SPF_NEUTRAL; if (first && localrules) { /* try to add local rules before failure of all mechs */ pos = 0; - p = (char *) 0; + p = (char *)0; while (pos < spfdata.len) { - NXTOK(begin,pos,&spfdata); + NXTOK(begin, pos, &spfdata); if (!spfdata.s[begin]) continue; - if (p && spfdata.s[begin] != *p) p = (char *) 0; - if (!p && (spfdata.s[begin] == '-' || - spfdata.s[begin] == '~' || - spfdata.s[begin] == '?')) p = &spfdata.s[begin]; + if (p && spfdata.s[begin] != *p) p = (char *)0; + if (!p && (spfdata.s[begin] == '-' || spfdata.s[begin] == '~' || spfdata.s[begin] == '?')) + p = &spfdata.s[begin]; - if (p && p > spfdata.s && case_equals(spfdata.s + begin + 1,"all")) { + if (p && p > spfdata.s && case_equals(spfdata.s + begin + 1, "all")) { /* ok, we can insert the local rules at p */ local_pos = p - spfdata.s; - if (!stralloc_readyplus(&spfdata,spflocalrules.len)) return 0; + if (!stralloc_readyplus(&spfdata, spflocalrules.len)) return 0; p = spfdata.s + local_pos; - byte_copyr(p + spflocalrules.len,spfdata.len - local_pos,p); - byte_copy(p,spflocalrules.len,spflocalrules.s); + byte_copyr(p + spflocalrules.len, spfdata.len - local_pos, p); + byte_copy(p, spflocalrules.len, spflocalrules.s); spfdata.len += spflocalrules.len; pos += spflocalrules.len; @@ -241,8 +249,8 @@ int spf_lookup(stralloc *domain) for (i = 0; i < pos; i++) if (!spfdata.s[i]) spfdata.s[i] = ' '; } - - } else { /* Any other SPF return code */ + + } else { /* Any other SPF return code */ return r; } @@ -251,15 +259,15 @@ int spf_lookup(stralloc *domain) pos = 0; done = 0; while (pos < spfdata.len) { - NXTOK(begin,pos,&spfdata); + NXTOK(begin, pos, &spfdata); if (!spfdata.s[begin]) continue; - if (!done && localrules) { /* in local ruleset? */ + if (!done && localrules) { /* in local ruleset? */ if (local_pos >= 0 && begin >= local_pos) { if (begin < (local_pos + spflocalrules.len)) { - if (!stralloc_copys(&expdomain,"")) return SPF_NOMEM; + if (!stralloc_copys(&expdomain, "")) return SPF_NOMEM; } else { - if (!stralloc_copy(&expdomain,domain)) return SPF_NOMEM; + if (!stralloc_copy(&expdomain, domain)) return SPF_NOMEM; } } } @@ -270,88 +278,116 @@ int spf_lookup(stralloc *domain) if (*p == '=') { *p++ = 0; - if (case_equals(spfdata.s + begin,"redirect")) { /* modifiers are simply handled here */ + if (case_equals(spfdata.s + begin, "redirect")) { /* modifiers are simply handled here */ if (done) continue; - -// if (!stralloc_0(domain)) return SPF_NOMEM; - if (!spf_parse(&sa,p,domain->s)) return SPF_NOMEM; - if (!stralloc_copy(domain,&sa)) return SPF_NOMEM; - if (!spf_info("D=",p)) return SPF_NOMEM; + + // if (!stralloc_0(domain)) return SPF_NOMEM; + if (!spf_parse(&sa, p, domain->s)) return SPF_NOMEM; + if (!stralloc_copy(domain, &sa)) return SPF_NOMEM; + if (!spf_info("D=", p)) return SPF_NOMEM; r = SPF_UNKNOWN; goto REDIRECT; - } else if (case_equals(spfdata.s + begin,"default")) { /* we don't need those anymore */ - if (done) continue; + } else if (case_equals(spfdata.s + begin, "default")) { /* we don't need those anymore */ + if (done) continue; - for (da = spf_aliases; da->alias; ++da) - if (case_equals(da->alias,p)) break; + for (da = spf_aliases; da->alias; ++da) + if (case_equals(da->alias, p)) break; - r = da->defrc; - } else if (case_equals(spfdata.s + begin,"exp")) { /* exp= only on top level */ + r = da->defrc; + } else if (case_equals(spfdata.s + begin, "exp")) { /* exp= only on top level */ stralloc out = {0}; if (!first) continue; - if (!stralloc_copys(&sa,p)) return SPF_NOMEM; - - switch (dns_txt(&out,&sa)) { + if (!stralloc_copys(&sa, p)) return SPF_NOMEM; + + switch (dns_txt(&out, &sa)) { case -1: return SPF_NOMEM; - case 0: continue; /* nobody @home */ + case 0: continue; /* nobody @home */ } - if (!stralloc_copys(&spfexpmsg,out.s)) return SPF_NOMEM; - if (!stralloc_append(&spfexpmsg,"\n")) return SPF_NOMEM; + if (!stralloc_copys(&spfexpmsg, out.s)) return SPF_NOMEM; + if (!stralloc_append(&spfexpmsg, "\n")) return SPF_NOMEM; if (!stralloc_0(&spfexpmsg)) return SPF_NOMEM; } - } else if (!done) { /* and unknown modifiers are ignored */ - if (!stralloc_copys(&sa,spfdata.s + begin)) return SPF_NOMEM; + } else if (!done) { /* and unknown modifiers are ignored */ + if (!stralloc_copys(&sa, spfdata.s + begin)) return SPF_NOMEM; if (!stralloc_0(&sa)) return SPF_NOMEM; - + switch (spfdata.s[begin]) { - case '-': begin++; spfrc = SPF_FAIL; break; - case '~': begin++; spfrc = SPF_SOFTFAIL; break; - case '+': begin++; spfrc = SPF_OK; break; - case '?': begin++; spfrc = SPF_NEUTRAL; break; - default: spfrc = SPF_OK; + case '-': + begin++; + spfrc = SPF_FAIL; + break; + case '~': + begin++; + spfrc = SPF_SOFTFAIL; + break; + case '+': + begin++; + spfrc = SPF_OK; + break; + case '?': + begin++; + spfrc = SPF_NEUTRAL; + break; + default: spfrc = SPF_OK; } if (*p == '/') { *p++ = 0; - q = spf_mechanism(spfdata.s + begin,0,p,domain->s); + q = spf_mechanism(spfdata.s + begin, 0, p, domain->s); } else { if (*p) *p++ = 0; - i = str_chr(p,'/'); + i = str_chr(p, '/'); if (p[i] == '/') { p[i++] = 0; - q = spf_mechanism(spfdata.s + begin,p,p + i,domain->s); + q = spf_mechanism(spfdata.s + begin, p, p + i, domain->s); } else if (i > 0) { - q = spf_mechanism(spfdata.s + begin,p,0,domain->s); + q = spf_mechanism(spfdata.s + begin, p, 0, domain->s); } else { - q = spf_mechanism(spfdata.s + begin,0,0,domain->s); + q = spf_mechanism(spfdata.s + begin, 0, 0, domain->s); } } if (q == SPF_OK) q = spfrc; switch (q) { - case SPF_OK: if (!spf_info("R:","+")) return SPF_NOMEM; break; - case SPF_NEUTRAL: if (!spf_info("R:","?")) return SPF_NOMEM; break; - case SPF_SYNTAX: if (!spf_info("P=","Unknown parse error")) return SPF_NOMEM; - if (!spf_info("R:","e")) return SPF_NOMEM; break; - case SPF_SOFTFAIL: if (!spf_info("R:","~")) return SPF_NOMEM; break; - case SPF_FAIL: if (!spf_info("R:","-")) return SPF_NOMEM; break; - case SPF_EXT: if (!spf_info("P=","Unknown SPF mechanism")) return SPF_NOMEM; break; - case SPF_ERROR: if (localrules) if (local_pos >= 0 && begin >= local_pos) break; - if (!spf_info("R:","o")) return SPF_NOMEM; q = SPF_NONE; break; - case SPF_NONE: continue; + case SPF_OK: + if (!spf_info("R:", "+")) return SPF_NOMEM; + break; + case SPF_NEUTRAL: + if (!spf_info("R:", "?")) return SPF_NOMEM; + break; + case SPF_SYNTAX: + if (!spf_info("P=", "Unknown parse error")) return SPF_NOMEM; + if (!spf_info("R:", "e")) return SPF_NOMEM; + break; + case SPF_SOFTFAIL: + if (!spf_info("R:", "~")) return SPF_NOMEM; + break; + case SPF_FAIL: + if (!spf_info("R:", "-")) return SPF_NOMEM; + break; + case SPF_EXT: + if (!spf_info("P=", "Unknown SPF mechanism")) return SPF_NOMEM; + break; + case SPF_ERROR: + if (localrules) + if (local_pos >= 0 && begin >= local_pos) break; + if (!spf_info("R:", "o")) return SPF_NOMEM; + q = SPF_NONE; + break; + case SPF_NONE: continue; } r = q; - done = 1; /* we're done, no more mechanisms */ + done = 1; /* we're done, no more mechanisms */ } } /* we fell through, no local rule applied */ if (!done) - if (!stralloc_copy(&expdomain,domain)) return SPF_NOMEM; + if (!stralloc_copy(&expdomain, domain)) return SPF_NOMEM; return r; } @@ -360,23 +396,23 @@ int spf_lookup(stralloc *domain) static struct mechanisms { char *mechanism; - int (*func)(char *spfspec,char *prefix); + int (*func)(char *spfspec, char *prefix); unsigned int use_spfspec : 1; - unsigned int use_prefix : 1; - unsigned int expands : 1; - unsigned int filldomain : 1; - int defresult : 4; + unsigned int use_prefix : 1; + unsigned int expands : 1; + unsigned int filldomain : 1; + int defresult : 4; } mechanisms[] = { - { "all", 0, 0,0,0,0,SPF_OK } -, { "include", spf_include,1,0,1,0,0 } -, { "a", spf_a, 1,1,1,1,0 } -, { "mx", spf_mx, 1,1,1,1,0 } -, { "ptr", spf_ptr, 1,0,1,1,0 } -, { "ip4", spf_ip4, 1,1,0,0,0 } -, { "ip6", spf_ip6, 1,1,0,0,0 } -, { "exists", spf_exists, 1,0,1,0,0 } -, { "extension",0, 1,1,0,0,SPF_EXT } -, { 0, 0, 1,1,0,0,SPF_EXT } + { "all", 0, 0, 0, 0, 0, SPF_OK}, + { "include", spf_include, 1, 0, 1, 0, 0}, + { "a", spf_a, 1, 1, 1, 1, 0}, + { "mx", spf_mx, 1, 1, 1, 1, 0}, + { "ptr", spf_ptr, 1, 0, 1, 1, 0}, + { "ip4", spf_ip4, 1, 1, 0, 0, 0}, + { "ip6", spf_ip6, 1, 1, 0, 0, 0}, + { "exists", spf_exists, 1, 0, 1, 0, 0}, + {"extension", 0, 1, 1, 0, 0, SPF_EXT}, + { 0, 0, 1, 1, 0, 0, SPF_EXT} }; /** @@ -388,7 +424,7 @@ static struct mechanisms { @return int r */ -int spf_mechanism(char *mechanism,char *spfspec,char *prefix,char *domain) +int spf_mechanism(char *mechanism, char *spfspec, char *prefix, char *domain) { struct mechanisms *mech; stralloc sa = {0}; @@ -396,28 +432,28 @@ int spf_mechanism(char *mechanism,char *spfspec,char *prefix,char *domain) int pos; for (mech = mechanisms; mech->mechanism; mech++) - if (case_equals(mech->mechanism,mechanism)) break; + if (case_equals(mech->mechanism, mechanism)) break; if (mech->use_spfspec && !spfspec && mech->filldomain) spfspec = domain; if (!mech->use_spfspec != !spfspec) return SPF_SYNTAX; if (mech->use_prefix && !get_prefix(prefix)) return SPF_SYNTAX; if (!mech->func) return mech->defresult; - if (!stralloc_readyplus(&sa,1)) return SPF_NOMEM; + if (!stralloc_readyplus(&sa, 1)) return SPF_NOMEM; - if (mech->expands && case_diffs(spfspec,domain)) { - if (!spf_parse(&sa,spfspec,domain)) return SPF_NOMEM; + if (mech->expands && case_diffs(spfspec, domain)) { + if (!spf_parse(&sa, spfspec, domain)) return SPF_NOMEM; for (pos = 0; (sa.len - pos) > 255;) { - pos += byte_chr(sa.s + pos,sa.len - pos,'.'); + pos += byte_chr(sa.s + pos, sa.len - pos, '.'); if (pos < sa.len) pos++; } sa.len -= pos; - if (pos > 0) byte_copy(sa.s,sa.len,sa.s + pos); + if (pos > 0) byte_copy(sa.s, sa.len, sa.s + pos); if (!stralloc_0(&sa)) return SPF_NOMEM; spfspec = sa.s; } - r = mech->func(spfspec,prefix); + r = mech->func(spfspec, prefix); return r; } @@ -428,12 +464,12 @@ int spf_mechanism(char *mechanism,char *spfspec,char *prefix,char *domain) @return int r = 1 ok; 0 failure */ -int spf_include(char *spfspec,char *prefix) +int spf_include(char *spfspec, char *prefix) { stralloc sa = {0}; int r; - if (!stralloc_copys(&sa,spfspec)) return SPF_NOMEM; + if (!stralloc_copys(&sa, spfspec)) return SPF_NOMEM; r = spf_lookup(&sa); switch (r) { @@ -444,8 +480,8 @@ int spf_include(char *spfspec,char *prefix) case SPF_FAIL: r = SPF_NONE; break; } if (!stralloc_0(&sa)) return SPF_NOMEM; - if (!spf_info("I=",sa.s)) return SPF_NOMEM; - + if (!spf_info("I=", sa.s)) return SPF_NOMEM; + return r; } @@ -458,34 +494,39 @@ int spf_include(char *spfspec,char *prefix) @return int r = 1 ok; 0 failure */ -int spf_parse(stralloc *sa,char *spfspec,char *domain) +int spf_parse(stralloc *sa, char *spfspec, char *domain) { char *p; int pos; char append; - if (!stralloc_readyplus(sa,3)) return 0; - if (!stralloc_copys(sa,"")) return 0; + if (!stralloc_readyplus(sa, 3)) return 0; + if (!stralloc_copys(sa, "")) return 0; for (p = spfspec; *p; ++p) { append = *p; - if (byte_equal(p,1,"%")) { + if (byte_equal(p, 1, "%")) { p++; switch (*p) { case '%': break; case '_': append = ' '; break; - case '-': if (!stralloc_cats(sa,"%20")) return 0; continue; + case '-': + if (!stralloc_cats(sa, "%20")) return 0; + continue; case '{': - pos = str_chr(p,'}'); - if (p[pos] != '}') { p--; break; } - p[pos] = '\0'; - if (!spf_macros(sa,p + 1,domain)) return 0; + pos = str_chr(p, '}'); + if (p[pos] != '}') { + p--; + break; + } + p[pos] = '\0'; + if (!spf_macros(sa, p + 1, domain)) return 0; p += pos; continue; default: p--; } } - if (!stralloc_append(sa,&append)) return 0; + if (!stralloc_append(sa, &append)) return 0; } return 1; @@ -499,7 +540,7 @@ int spf_parse(stralloc *sa,char *spfspec,char *domain) @return int r = 1 ok; 0 failure */ -int spf_macros(stralloc *expand,char *macro,char *domain) +int spf_macros(stralloc *expand, char *macro, char *domain) { static const char hextab[] = "0123456789abcdef"; stralloc sa = {0}; @@ -509,139 +550,167 @@ int spf_macros(stralloc *expand,char *macro,char *domain) unsigned long u; char ch = {0}; char ascii; - int pos, i, n; - int start = expand->len; + int pos, i, n; + int start = expand->len; /* URL encoding - hidden in RFC 7208 Sec. 7.3 */ - if (*macro == 'x') { urlencode = -1; ++macro; } else urlencode = 0; + if (*macro == 'x') { + urlencode = -1; + ++macro; + } else + urlencode = 0; ch = *macro; - if (!ch) { return 1; } - if (ch >= 'A' && ch <= 'Z') { ch += 32; urlencode = 1; } + if (!ch) { + return 1; + } + if (ch >= 'A' && ch <= 'Z') { + ch += 32; + urlencode = 1; + } if (urlencode == -1) ch -= 32; /* No. digits determine number of printed labels */ - + i = 0; while (*macro) { i++; if (*macro == '}') break; if (*macro >= '0' && *macro <= '9') { - scan_ulong(macro,&u); ndigits = u; - } else if (i > 1 && *macro == 'r') { reverse = 1; break; } /* Reverse representation */ + scan_ulong(macro, &u); + ndigits = u; + } else if (i > 1 && *macro == 'r') { + reverse = 1; + break; + } /* Reverse representation */ macro++; } - switch (ch) { /* see RFC7208 sec. 7.2 */ - case 's': case 'S': - if (!stralloc_readyplus(&sa,spfmf.len)) return 0; - if (!stralloc_copys(&sa,spfmf.s)) return 0; + switch (ch) { /* see RFC7208 sec. 7.2 */ + case 's': + case 'S': + if (!stralloc_readyplus(&sa, spfmf.len)) return 0; + if (!stralloc_copys(&sa, spfmf.s)) return 0; break; - case 'l': case 'L': - i = byte_rchr(spfmf.s,spfmf.len,'@'); + case 'l': + case 'L': + i = byte_rchr(spfmf.s, spfmf.len, '@'); if (i < spfmf.len) { - if (!stralloc_copyb(&sa,spfmf.s,i)) return 0; + if (!stralloc_copyb(&sa, spfmf.s, i)) return 0; } else { - if (!stralloc_copys(&sa,"postmaster")) return 0; + if (!stralloc_copys(&sa, "postmaster")) return 0; } break; - case 'o': case 'O': - i = byte_rchr(spfmf.s,spfmf.len,'@') + 1; + case 'o': + case 'O': + i = byte_rchr(spfmf.s, spfmf.len, '@') + 1; if (i > spfmf.len) break; - if (!stralloc_copys(&sa,spfmf.s + i)) return 0; + if (!stralloc_copys(&sa, spfmf.s + i)) return 0; break; - case 'd': case 'D': - if (!stralloc_copys(&sa,domain)) return 0; /* the hack for 'Z'; Russions everywhere ;-) */ + case 'd': + case 'D': + if (!stralloc_copys(&sa, domain)) return 0; /* the hack for 'Z'; Russions everywhere ;-) */ break; - case 'i': case 'c': case 'I': case 'C': - if (!stralloc_ready(&sa,IPFMT)) return 0; + case 'i': + case 'c': + case 'I': + case 'C': + if (!stralloc_ready(&sa, IPFMT)) return 0; if (flagip6) { - sa.len = ip6_fmt(sa.s,ip6remote); + sa.len = ip6_fmt(sa.s, ip6remote); } else { - sa.len = ip4_fmt(sa.s,ip4remote); + sa.len = ip4_fmt(sa.s, ip4remote); } break; - case 'p': case 'P': - if (!dnsname.len) spf_ptr(domain,0); + case 'p': + case 'P': + if (!dnsname.len) spf_ptr(domain, 0); if (dnsname.len) { - if (!stralloc_copys(&sa,dnsname.s)) return 0; + if (!stralloc_copys(&sa, dnsname.s)) return 0; } else { - if (!stralloc_copys(&sa,"unknown")) return 0; + if (!stralloc_copys(&sa, "unknown")) return 0; } break; - case 'h': case 'H': - if (!stralloc_copys(&sa,spfhelo.s)) return 0; /* FIXME: FQDN? */ + case 'h': + case 'H': + if (!stralloc_copys(&sa, spfhelo.s)) return 0; /* FIXME: FQDN? */ break; - case 't': case 'T': - if (!stralloc_ready(&sa,FMT_ULONG)) return 0; - sa.len = fmt_ulong(sa.s,(unsigned long)now()); + case 't': + case 'T': + if (!stralloc_ready(&sa, FMT_ULONG)) return 0; + sa.len = fmt_ulong(sa.s, (unsigned long)now()); break; - case 'v': case 'V': + case 'v': + case 'V': if (flagip6) { - if (!stralloc_copys(&sa,"ip6")) return 0; + if (!stralloc_copys(&sa, "ip6")) return 0; } else { - if (!stralloc_copys(&sa,"in-addr")) return 0; + if (!stralloc_copys(&sa, "in-addr")) return 0; } break; - case 'r': case 'R': - if (!stralloc_copy(&sa,&spflocal)) return 0; + case 'r': + case 'R': + if (!stralloc_copy(&sa, &spflocal)) return 0; break; default: break; } - if (!stralloc_0(&sa)) return 0; // XXX + if (!stralloc_0(&sa)) return 0; // XXX if (reverse) { n = 0; for (i = 1; i <= sa.len; i++) { if ((ndigits == -1) || (n < ndigits)) { - if (!byte_diff(sa.s + sa.len - i - 1,1,".") || (i == sa.len)) { + if (!byte_diff(sa.s + sa.len - i - 1, 1, ".") || (i == sa.len)) { n++; - if (!stralloc_cats(expand,sa.s + sa.len - i)) return 0; + if (!stralloc_cats(expand, sa.s + sa.len - i)) return 0; if (i < sa.len) { sa.s[sa.len - i - 1] = 0; - if (!stralloc_cats(expand,".")) return 0; - } + if (!stralloc_cats(expand, ".")) return 0; + } } - } + } } } else if (ndigits != -1) { n = pos = 0; for (i = 1; i <= sa.len; i++) { if (n < ndigits) { - if (!byte_diff(sa.s + i,1,".")) { n++; pos = i; } + if (!byte_diff(sa.s + i, 1, ".")) { + n++; + pos = i; + } } } - if (!stralloc_catb(expand,sa.s,pos)) return 0; - } else - if (!stralloc_cats(expand,sa.s)) return 0; + if (!stralloc_catb(expand, sa.s, pos)) return 0; + } else if (!stralloc_cats(expand, sa.s)) + return 0; if (urlencode) { - stralloc_copyb(&sa,expand->s + start,expand->len - start); + stralloc_copyb(&sa, expand->s + start, expand->len - start); expand->len = start; for (i = 0; i < sa.len; ++i) { ch = sa.s[i]; if (urlchr_table[(unsigned char)ch]) { - if (!stralloc_readyplus(expand,3)) return 0; - if (!stralloc_append(expand,"%")) return 0; + if (!stralloc_readyplus(expand, 3)) return 0; + if (!stralloc_append(expand, "%")) return 0; ascii = hextab[(unsigned char)ch >> 4]; - if (!stralloc_append(expand,&ascii)) return 0; + if (!stralloc_append(expand, &ascii)) return 0; ascii = hextab[(unsigned char)ch & 0x0f]; - if (!stralloc_append(expand,&ascii)) return 0; + if (!stralloc_append(expand, &ascii)) return 0; } else { - if (!stralloc_append(expand,&ch)) return 0; + if (!stralloc_append(expand, &ch)) return 0; } } - } + } return 1; } -int spf_info(char *s,const char *t) +int spf_info(char *s, const char *t) { - if (!stralloc_cats(&spfinfo,s)) return 0; - if (!stralloc_cats(&spfinfo,t)) return 0; - if (!stralloc_cats(&spfinfo," ")) return 0; + if (!stralloc_cats(&spfinfo, s)) return 0; + if (!stralloc_cats(&spfinfo, t)) return 0; + if (!stralloc_cats(&spfinfo, " ")) return 0; return 1; } diff --git a/src/spfdnsip.c b/src/spfdnsip.c index e9cf9ee..9012f69 100644 --- a/src/spfdnsip.c +++ b/src/spfdnsip.c @@ -1,24 +1,26 @@ #include <unistd.h> -#include "stralloc.h" + #include "alloc.h" +#include "byte.h" +#include "case.h" +#include "fmt.h" #include "ip.h" +#include "scan.h" +#include "str.h" +#include "stralloc.h" + +#include "dns.h" #include "ipalloc.h" #include "ipme.h" -#include "str.h" -#include "fmt.h" -#include "scan.h" -#include "byte.h" #include "now.h" -#include "dns.h" -#include "case.h" #include "spf.h" // shared by spf.c + spfdnsip.c extern stralloc dnsname; extern char ip4remote[4]; -extern char ip6remote[16]; -extern int flagip6; +extern char ip6remote[16]; +extern int flagip6; /** @brief match_ip @@ -27,30 +29,30 @@ extern int flagip6; @return 1 ok; 0 failure */ -int match_ip4(unsigned char ip1[4],int prefix,char ip2[4]) +int match_ip4(unsigned char ip1[4], int prefix, char ip2[4]) { stralloc iptest1 = {0}; stralloc iptest2 = {0}; if (flagip6) return 0; - if (ip4_bytestring(&iptest1,ip1,prefix) == prefix) - if (ip4_bytestring(&iptest2,ip2,prefix) == prefix) - if (byte_diff(iptest1.s,prefix,iptest2.s)) return 0; + if (ip4_bytestring(&iptest1, ip1, prefix) == prefix) + if (ip4_bytestring(&iptest2, ip2, prefix) == prefix) + if (byte_diff(iptest1.s, prefix, iptest2.s)) return 0; return 1; } -int match_ip6(unsigned char ip1[16],int prefix,char ip2[16]) +int match_ip6(unsigned char ip1[16], int prefix, char ip2[16]) { stralloc iptest1 = {0}; stralloc iptest2 = {0}; if (!flagip6) return 0; - if (ip6_bytestring(&iptest1,ip1,prefix) == prefix) - if (ip6_bytestring(&iptest2,ip2,prefix) == prefix) - if (byte_diff(iptest1.s,prefix,iptest2.s)) return 0; + if (ip6_bytestring(&iptest1, ip1, prefix) == prefix) + if (ip6_bytestring(&iptest2, ip2, prefix) == prefix) + if (byte_diff(iptest1.s, prefix, iptest2.s)) return 0; return 1; } @@ -72,7 +74,7 @@ int get_prefix(char *prefix) if (flagip6 == 1) return 128; } - pos = scan_ulong(prefix,&r); + pos = scan_ulong(prefix, &r); if (!pos || (prefix[pos] && !(prefix[pos] == '/'))) return SPF_SYNTAX; if (flagip6 == 0 && r > 32) return SPF_SYNTAX; if (flagip6 == 1 && r > 128) return SPF_SYNTAX; @@ -90,42 +92,42 @@ int get_prefix(char *prefix) @return SPF_OK, SPF_NONE; SPF_MULTIRR, SPF_DNSSOFT, SPF_NOMEM */ -int spf_records(stralloc *spfrec,stralloc *domain) +int spf_records(stralloc *spfrec, stralloc *domain) { static stralloc out = {0}; static stralloc spf = {0}; - int i, k; - int begin; + int i, k; + int begin; int r = 0; begin = -1; DNS_INIT - r = dns_txt(&out,(const stralloc *)domain); + r = dns_txt(&out, (const stralloc *)domain); switch (r) { case DNS_MEM: return SPF_NOMEM; case DNS_ERR: return SPF_DNSSOFT; /* return 2main */ - case DNS_NXD: return SPF_NONE; + case DNS_NXD: return SPF_NONE; } r = SPF_NONE; for (k = 0; k < out.len; ++k) { - if (case_starts(out.s + k,"v=spf1")) { + if (case_starts(out.s + k, "v=spf1")) { begin = k; break; - } + } } if (begin >= 0) { - if (case_starts(out.s + k + 6,"v=spf1")) return SPF_MULTIRR; /* return 2main */ + if (case_starts(out.s + k + 6, "v=spf1")) return SPF_MULTIRR; /* return 2main */ - if (!stralloc_copys(&spf,"")) return SPF_NOMEM; + if (!stralloc_copys(&spf, "")) return SPF_NOMEM; for (i = begin; i < out.len; ++i) { if (out.s[i] == '\r' || out.s[i] == '\n' || out.s[i] == '\0') break; - if (!stralloc_append(&spf,out.s + i)) return SPF_NOMEM; - } + if (!stralloc_append(&spf, out.s + i)) return SPF_NOMEM; + } if (!stralloc_0(&spf)) return SPF_NOMEM; - if (!stralloc_copys(spfrec,spf.s)) return SPF_NOMEM; + if (!stralloc_copys(spfrec, spf.s)) return SPF_NOMEM; r = SPF_OK; } @@ -142,7 +144,7 @@ int spf_records(stralloc *spfrec,stralloc *domain) @return SPF_OK, SPF_NONE; SPF_DNSSOFT, SPF_NOMEM */ -int spf_a(char *spfspec,char *prefix) +int spf_a(char *spfspec, char *prefix) { stralloc sa = {0}; stralloc ip = {0}; @@ -151,32 +153,30 @@ int spf_a(char *spfspec,char *prefix) ipprefix = get_prefix(prefix); if (ipprefix < 0) return SPF_SYNTAX; - if (!stralloc_copys(&sa,spfspec)) return SPF_NOMEM; - if (!stralloc_readyplus(&ip,0)) return SPF_NOMEM; - if (!spf_info("MA/AAAA=",spfspec)) return SPF_NOMEM; + if (!stralloc_copys(&sa, spfspec)) return SPF_NOMEM; + if (!stralloc_readyplus(&ip, 0)) return SPF_NOMEM; + if (!spf_info("MA/AAAA=", spfspec)) return SPF_NOMEM; DNS_INIT - switch (dns_ip4(&ip,&sa)) { + switch (dns_ip4(&ip, &sa)) { case DNS_MEM: return SPF_NOMEM; case DNS_ERR: r = SPF_DNSSOFT; break; case DNS_NXD: r = SPF_NONE; break; - default: + default: r = SPF_NONE; - for (j = 0; j + 4 <= ip.len; j += 4) - if (match_ip4(ip.s + j,ipprefix,ip4remote)) - return SPF_OK; + for (j = 0; j + 4 <= ip.len; j += 4) + if (match_ip4(ip.s + j, ipprefix, ip4remote)) return SPF_OK; } - switch (dns_ip6(&ip,&sa)) { + switch (dns_ip6(&ip, &sa)) { case DNS_MEM: return SPF_NOMEM; case DNS_ERR: r = SPF_DNSSOFT; break; case DNS_NXD: r = SPF_NONE; break; - default: + default: r = SPF_NONE; - for (j = 0; j + 16 <= ip.len; j += 16) - if (match_ip6(ip.s + j,ipprefix,ip6remote)) - return SPF_OK; + for (j = 0; j + 16 <= ip.len; j += 16) + if (match_ip6(ip.s + j, ipprefix, ip6remote)) return SPF_OK; } return r; @@ -189,7 +189,7 @@ int spf_a(char *spfspec,char *prefix) @return SPF_OK, SPF_NONE; SPF_DNSSOFT, SPF_NOMEM */ -int spf_mx(char *spfspec,char *prefix) +int spf_mx(char *spfspec, char *prefix) { stralloc sa = {0}; ipalloc ia = {0}; @@ -201,23 +201,21 @@ int spf_mx(char *spfspec,char *prefix) if (ipprefix < 0) return SPF_SYNTAX; random = now() + (getpid() << 16); - - if (!stralloc_copys(&sa,spfspec)) return SPF_NOMEM; - if (!spf_info("MMX=",spfspec)) return SPF_NOMEM; - switch (dns_mxip(&ia,&sa,random)) { + if (!stralloc_copys(&sa, spfspec)) return SPF_NOMEM; + if (!spf_info("MMX=", spfspec)) return SPF_NOMEM; + + switch (dns_mxip(&ia, &sa, random)) { case DNS_MEM: return SPF_NOMEM; - case DNS_ERR: return SPF_DNSSOFT; + case DNS_ERR: return SPF_DNSSOFT; default: r = SPF_NONE; for (j = 0; j < ia.len; ++j) { - if (byte_diff(ip6remote,16,V6localnet) && !ip6_isv4mapped(ip6remote)) { - if (match_ip6(&ia.ix[j].addr.ip6.d,ipprefix,ip6remote)) - return SPF_OK; - } - if (byte_diff(ip4remote,4,V4localnet)) { - if (match_ip4(&ia.ix[j].addr.ip4.d,ipprefix,ip4remote)) - return SPF_OK; + if (byte_diff(ip6remote, 16, V6localnet) && !ip6_isv4mapped(ip6remote)) { + if (match_ip6(&ia.ix[j].addr.ip6.d, ipprefix, ip6remote)) return SPF_OK; + } + if (byte_diff(ip4remote, 4, V4localnet)) { + if (match_ip4(&ia.ix[j].addr.ip4.d, ipprefix, ip4remote)) return SPF_OK; } } } @@ -232,7 +230,7 @@ int spf_mx(char *spfspec,char *prefix) @return SPF_OK, SPF_NONE; SPF_DNSSOFT, SPF_NOMEM */ -int spf_ptr(char *spfspec,char *prefix) +int spf_ptr(char *spfspec, char *prefix) { stralloc fqdn = {0}; stralloc out = {0}; @@ -244,17 +242,16 @@ int spf_ptr(char *spfspec,char *prefix) int l = 0; /* we didn't find host with the matching IP before */ - if (dnsname.len == 7 && str_equal(dnsname.s,"unknown")) - return SPF_NONE; + if (dnsname.len == 7 && str_equal(dnsname.s, "unknown")) return SPF_NONE; - if (!spf_info("MPTR=",spfspec)) return SPF_NOMEM; + if (!spf_info("MPTR=", spfspec)) return SPF_NOMEM; /* the hostname found will probably be the same as before */ while (dnsname.len) { pos = dnsname.len - slen; if (pos < 0) break; if (pos > 0 && dnsname.s[pos - 1] != '.') break; - if (case_diffb(dnsname.s + pos,slen,spfspec)) break; + if (case_diffb(dnsname.s + pos, slen, spfspec)) break; return SPF_OK; } @@ -265,72 +262,90 @@ int spf_ptr(char *spfspec,char *prefix) */ - if (!stralloc_readyplus(&fqdn,255)) return SPF_NOMEM; - if (!stralloc_readyplus(&out,255)) return SPF_NOMEM; - if (!stralloc_readyplus(&ip,32)) return SPF_NOMEM; + if (!stralloc_readyplus(&fqdn, 255)) return SPF_NOMEM; + if (!stralloc_readyplus(&out, 255)) return SPF_NOMEM; + if (!stralloc_readyplus(&ip, 32)) return SPF_NOMEM; - if (flagip6) { - rc = dns_name6(&out,ip6remote); // usually: 2. . .ip6.addr => only one + if (flagip6) { + rc = dns_name6(&out, ip6remote); // usually: 2. . .ip6.addr => only one switch (rc) { case DNS_MEM: return SPF_NOMEM; case DNS_COM: r = SPF_DNSSOFT; break; case DNS_ERR: r = SPF_NONE; break; case DNS_NXD: r = SPF_NONE; break; - default: r = SPF_NONE; l++; - if (l > LOOKUP_LIMIT) { r = SPF_ERROR; break; } - switch (dns_ip6(&ip,&out)) { // theoretical more IPs cound be retrieved - case DNS_MEM: return SPF_NOMEM; - case DNS_ERR: r = SPF_DNSSOFT; break; - case DNS_NXD: r = SPF_NONE; break; - default: r = SPF_NONE; - for (k = 0; k + 16 <= ip.len; k += 16) { - if (k > 32 * LOOKUP_LIMIT) { r = SPF_ERROR; break; } - if (match_ip6(ip.s + k,128,ip6remote)) { - if (!dnsname.len) - if (!stralloc_copy(&dnsname,&out)) return SPF_NOMEM; - pos = out.len - slen; - if (pos < 0) continue; - if (pos > 0 && out.s[pos - 1] != '.') continue; - if (case_diffb(out.s + pos,slen,spfspec)) continue; - - if (!stralloc_copy(&dnsname,&out)) return SPF_NOMEM; - r = SPF_OK; - } - } - } - } - } else { // IP4 branch - rc = dns_name4(&out,ip4remote); // usual answer: d.c.b.e.in-arpa.addr for IP4 a.b.c.d => only one + default: + r = SPF_NONE; + l++; + if (l > LOOKUP_LIMIT) { + r = SPF_ERROR; + break; + } + switch (dns_ip6(&ip, &out)) { // theoretical more IPs cound be retrieved + case DNS_MEM: return SPF_NOMEM; + case DNS_ERR: r = SPF_DNSSOFT; break; + case DNS_NXD: r = SPF_NONE; break; + default: + r = SPF_NONE; + for (k = 0; k + 16 <= ip.len; k += 16) { + if (k > 32 * LOOKUP_LIMIT) { + r = SPF_ERROR; + break; + } + if (match_ip6(ip.s + k, 128, ip6remote)) { + if (!dnsname.len) + if (!stralloc_copy(&dnsname, &out)) return SPF_NOMEM; + pos = out.len - slen; + if (pos < 0) continue; + if (pos > 0 && out.s[pos - 1] != '.') continue; + if (case_diffb(out.s + pos, slen, spfspec)) continue; + + if (!stralloc_copy(&dnsname, &out)) return SPF_NOMEM; + r = SPF_OK; + } + } + } + } + } else { // IP4 branch + rc = dns_name4(&out, ip4remote); // usual answer: d.c.b.e.in-arpa.addr for IP4 a.b.c.d => only one switch (rc) { case DNS_MEM: return SPF_NOMEM; case DNS_ERR: r = SPF_DNSSOFT; break; case DNS_NXD: r = SPF_NONE; break; - default: r = SPF_NONE; l++; - if (l > LOOKUP_LIMIT) { r = SPF_ERROR; break; } - switch (dns_ip4(&ip,&out)) { - case DNS_MEM: return SPF_NOMEM; - case DNS_ERR: r = SPF_DNSSOFT; break; - case DNS_NXD: r = SPF_NONE; break; - default: r = SPF_NONE; - for (k = 0; k + 4 <= ip.len; k += 4) { - if (k > 32 * LOOKUP_LIMIT) { r = SPF_ERROR; break; } - if (match_ip4(ip.s + k,32,ip4remote)) { - if (!dnsname.len) - if (!stralloc_copy(&dnsname,&out)) return SPF_NOMEM; - pos = out.len - slen; - if (pos < 0) continue; - if (pos > 0 && out.s[pos - 1] != '.') continue; - if (case_diffb(out.s + pos,slen,spfspec)) continue; - - if (!stralloc_copy(&dnsname,&out)) return SPF_NOMEM; - r = SPF_OK; - } - } - } + default: + r = SPF_NONE; + l++; + if (l > LOOKUP_LIMIT) { + r = SPF_ERROR; + break; + } + switch (dns_ip4(&ip, &out)) { + case DNS_MEM: return SPF_NOMEM; + case DNS_ERR: r = SPF_DNSSOFT; break; + case DNS_NXD: r = SPF_NONE; break; + default: + r = SPF_NONE; + for (k = 0; k + 4 <= ip.len; k += 4) { + if (k > 32 * LOOKUP_LIMIT) { + r = SPF_ERROR; + break; + } + if (match_ip4(ip.s + k, 32, ip4remote)) { + if (!dnsname.len) + if (!stralloc_copy(&dnsname, &out)) return SPF_NOMEM; + pos = out.len - slen; + if (pos < 0) continue; + if (pos > 0 && out.s[pos - 1] != '.') continue; + if (case_diffb(out.s + pos, slen, spfspec)) continue; + + if (!stralloc_copy(&dnsname, &out)) return SPF_NOMEM; + r = SPF_OK; + } + } + } } } if (!dnsname.len) - if (!stralloc_copys(&dnsname,"unknown")) return SPF_NOMEM; + if (!stralloc_copys(&dnsname, "unknown")) return SPF_NOMEM; return r; } @@ -342,7 +357,7 @@ int spf_ptr(char *spfspec,char *prefix) @return SPF_OK, SPF_NONE; SPF_DNSSOFT, SPF_NOMEM */ -int spf_ip4(char *spfspec,char *prefix) +int spf_ip4(char *spfspec, char *prefix) { char spfip[4]; @@ -350,10 +365,10 @@ int spf_ip4(char *spfspec,char *prefix) int ipprefix = get_prefix(prefix); if (ipprefix < 0) return SPF_SYNTAX; - if (!ip4_scan(spfspec,spfip)) return SPF_SYNTAX; + if (!ip4_scan(spfspec, spfip)) return SPF_SYNTAX; - if (!spf_info("MIPv4=",spfspec)) return SPF_NOMEM; - if (!match_ip4(spfip,ipprefix,ip4remote)) return SPF_NONE; + if (!spf_info("MIPv4=", spfspec)) return SPF_NOMEM; + if (!match_ip4(spfip, ipprefix, ip4remote)) return SPF_NONE; return SPF_OK; } @@ -365,7 +380,7 @@ int spf_ip4(char *spfspec,char *prefix) @return SPF_OK, SPF_NONE; SPF_DNSSOFT, SPF_NOMEM */ -int spf_ip6(char *spfspec,char *prefix) +int spf_ip6(char *spfspec, char *prefix) { char spfip[16]; @@ -373,10 +388,10 @@ int spf_ip6(char *spfspec,char *prefix) int ipprefix = get_prefix(prefix); if (ipprefix < 0) return SPF_SYNTAX; - if (!ip6_scan(spfspec,spfip)) return SPF_SYNTAX; + if (!ip6_scan(spfspec, spfip)) return SPF_SYNTAX; - if (!spf_info("MIPv6=",spfspec)) return SPF_NOMEM; - if (!match_ip6(spfip,ipprefix,ip6remote)) return SPF_NONE; + if (!spf_info("MIPv6=", spfspec)) return SPF_NOMEM; + if (!match_ip6(spfip, ipprefix, ip6remote)) return SPF_NONE; return SPF_OK; } @@ -388,19 +403,18 @@ int spf_ip6(char *spfspec,char *prefix) @return SPF_OK, SPF_NONE; SPF_DNSSOFT, SPF_NOMEM */ -int spf_exists(char *spfspec,char *prefix) +int spf_exists(char *spfspec, char *prefix) { stralloc sa = {0}; stralloc ip = {0}; - if (!stralloc_copys(&sa,spfspec)) return SPF_NOMEM; - if (!spf_info("MExists=",spfspec)) return SPF_NOMEM; + if (!stralloc_copys(&sa, spfspec)) return SPF_NOMEM; + if (!spf_info("MExists=", spfspec)) return SPF_NOMEM; - switch (dns_ip4(&ip,&sa)) { + switch (dns_ip4(&ip, &sa)) { case DNS_MEM: return SPF_NOMEM; - case DNS_ERR: return SPF_DNSSOFT; - case DNS_NXD: return SPF_NONE; - default: return SPF_OK; + case DNS_ERR: return SPF_DNSSOFT; + case DNS_NXD: return SPF_NONE; + default: return SPF_OK; } - } diff --git a/src/spfquery.c b/src/spfquery.c index 8c642ee..68a5158 100644 --- a/src/spfquery.c +++ b/src/spfquery.c @@ -1,26 +1,36 @@ -#include <string.h> #include <unistd.h> -#include "buffer.h" -#include "stralloc.h" + +#include <string.h> + #include "alloc.h" -#include "spf.h" -#include "exit.h" -#include "dns.h" -#include "str.h" +#include "buffer.h" #include "byte.h" +#include "exit.h" #include "logmsg.h" +#include "str.h" +#include "stralloc.h" + +#include "dns.h" +#include "spf.h" #define WHO "spfquery" -void die(int e,char *s) { buffer_putsflush(buffer_2,s); _exit(e); } -void die_nomem() { die(111,"fatal: out of memory\n"); } +void die(int e, char *s) +{ + buffer_putsflush(buffer_2, s); + _exit(e); +} +void die_nomem() +{ + die(111, "fatal: out of memory\n"); +} static stralloc heloin = {0}; static stralloc mfin = {0}; static stralloc spflocal = {0}; static stralloc spfbounce = {0}; -int main(int argc,char **argv) +int main(int argc, char **argv) { stralloc spfip = {0}; int flag = 0; @@ -28,70 +38,75 @@ int main(int argc,char **argv) int verbose = 0; flagip6 = 1; - if (argc < 4) - logmsg(WHO,100,USAGE,"spfquery <sender-ip> <sender-helo/ehlo> <envelope-from> [<local rules>] [-v(erbose) ]\n"); + if (argc < 4) + logmsg( + WHO, + 100, + USAGE, + "spfquery <sender-ip> <sender-helo/ehlo> <envelope-from> [<local rules>] [-v(erbose) ]\n"); - if (!stralloc_copys(&spfip,argv[1])) die_nomem(); + if (!stralloc_copys(&spfip, argv[1])) die_nomem(); if (!stralloc_0(&spfip)) die_nomem(); - r = byte_chr(spfip.s,spfip.len,':'); + r = byte_chr(spfip.s, spfip.len, ':'); if (r < spfip.len) flag = 1; - if (!stralloc_copys(&heloin,argv[2])) die_nomem(); + if (!stralloc_copys(&heloin, argv[2])) die_nomem(); if (!stralloc_0(&heloin)) die_nomem(); - if (!stralloc_copys(&mfin,argv[3])) die_nomem(); + if (!stralloc_copys(&mfin, argv[3])) die_nomem(); if (!stralloc_0(&mfin)) die_nomem(); if (argc > 4) { - if (!byte_diff(argv[4],2,"-v")) verbose = 1; + if (!byte_diff(argv[4], 2, "-v")) + verbose = 1; else { - if (!stralloc_copys(&spflocal,argv[4])) die_nomem(); + if (!stralloc_copys(&spflocal, argv[4])) die_nomem(); if (spflocal.len && !stralloc_0(&spflocal)) die_nomem(); } } if (argc > 5) { - if (!byte_diff(argv[5],2,"-v")) verbose = 1; + if (!byte_diff(argv[5], 2, "-v")) verbose = 1; } - if (!stralloc_copys(&spfexplain,SPF_DEFEXP)) die_nomem(); + if (!stralloc_copys(&spfexplain, SPF_DEFEXP)) die_nomem(); if (!stralloc_0(&spfexplain)) die_nomem(); DNS_INIT - r = spf_query(spfip.s,heloin.s,mfin.s,"localhost",flag); + r = spf_query(spfip.s, heloin.s, mfin.s, "localhost", flag); if (r == SPF_NOMEM) die_nomem(); - buffer_puts(buffer_1,"result="); + buffer_puts(buffer_1, "result="); switch (r) { - case SPF_ME: buffer_puts(buffer_1,"loopback"); break; - case SPF_OK: buffer_puts(buffer_1,"pass"); break; - case SPF_NONE: buffer_puts(buffer_1,"none"); break; - case SPF_UNKNOWN: buffer_puts(buffer_1,"unknown"); break; - case SPF_NEUTRAL: buffer_puts(buffer_1,"neutral"); break; - case SPF_SOFTFAIL: buffer_puts(buffer_1,"softfail"); break; - case SPF_FAIL: buffer_puts(buffer_1,"fail"); break; - case SPF_ERROR: buffer_puts(buffer_1,"error"); break; - case SPF_SYNTAX: buffer_puts(buffer_1,"IP address syntax error"); break; - default: buffer_puts(buffer_1,"undefined"); break; + case SPF_ME: buffer_puts(buffer_1, "loopback"); break; + case SPF_OK: buffer_puts(buffer_1, "pass"); break; + case SPF_NONE: buffer_puts(buffer_1, "none"); break; + case SPF_UNKNOWN: buffer_puts(buffer_1, "unknown"); break; + case SPF_NEUTRAL: buffer_puts(buffer_1, "neutral"); break; + case SPF_SOFTFAIL: buffer_puts(buffer_1, "softfail"); break; + case SPF_FAIL: buffer_puts(buffer_1, "fail"); break; + case SPF_ERROR: buffer_puts(buffer_1, "error"); break; + case SPF_SYNTAX: buffer_puts(buffer_1, "IP address syntax error"); break; + default: buffer_puts(buffer_1, "undefined"); break; } - buffer_putsflush(buffer_1,"\n"); + buffer_putsflush(buffer_1, "\n"); if (r == SPF_SYNTAX) _exit(1); if (verbose) { - buffer_puts(buffer_1,"SPF records read: \n"); - buffer_put(buffer_1,spfrecord.s,spfrecord.len); + buffer_puts(buffer_1, "SPF records read: \n"); + buffer_put(buffer_1, spfrecord.s, spfrecord.len); } - buffer_puts(buffer_1,"SPF information evaluated: "); - buffer_put(buffer_1,spfinfo.s,spfinfo.len); - buffer_putsflush(buffer_1,"\n"); + buffer_puts(buffer_1, "SPF information evaluated: "); + buffer_put(buffer_1, spfinfo.s, spfinfo.len); + buffer_putsflush(buffer_1, "\n"); if (r == SPF_FAIL) { - buffer_puts(buffer_1,"SPF results returned: "); - if (!spf_parse(&spfbounce,spfexpmsg.s,expdomain.s)) die_nomem(); - buffer_put(buffer_1,spfbounce.s,spfbounce.len); - buffer_putsflush(buffer_1,"\n"); + buffer_puts(buffer_1, "SPF results returned: "); + if (!spf_parse(&spfbounce, spfexpmsg.s, expdomain.s)) die_nomem(); + buffer_put(buffer_1, spfbounce.s, spfbounce.len); + buffer_putsflush(buffer_1, "\n"); } _exit(0); diff --git a/src/splogger.c b/src/splogger.c index 4e64590..2dcfe2a 100644 --- a/src/splogger.c +++ b/src/splogger.c @@ -1,29 +1,30 @@ -#include <sys/types.h> #include <sys/time.h> +#include <sys/types.h> #include <syslog.h> #include <unistd.h> -#include "error.h" + #include "buffer.h" +#include "error.h" #include "exit.h" -#include "str.h" -#include "scan.h" #include "fmt.h" +#include "scan.h" +#include "str.h" -char buf[800]; /* syslog truncates long lines (or crashes); GPACIC */ +char buf[800]; /* syslog truncates long lines (or crashes); GPACIC */ int bufpos = 0; /* 0 <= bufpos < sizeof(buf) */ int flagcont = 0; -int priority; /* defined if flagcont */ +int priority; /* defined if flagcont */ char stamp[FMT_ULONG + FMT_ULONG + 3]; /* defined if flagcont */ void stamp_make() { struct timeval tv; char *s; - gettimeofday(&tv,(struct timezone *) 0); + gettimeofday(&tv, (struct timezone *)0); s = stamp; - s += fmt_ulong(s,(unsigned long) tv.tv_sec); + s += fmt_ulong(s, (unsigned long)tv.tv_sec); *s++ = '.'; - s += fmt_uint0(s,(unsigned int) tv.tv_usec,6); + s += fmt_uint0(s, (unsigned int)tv.tv_usec, 6); *s = 0; } @@ -32,37 +33,40 @@ void flush() if (bufpos) { buf[bufpos] = 0; if (flagcont) - syslog(priority,"%s+%s",stamp,buf); /* logger folds invisibly; GPACIC */ + syslog(priority, "%s+%s", stamp, buf); /* logger folds invisibly; GPACIC */ else { stamp_make(); priority = LOG_INFO; - if (str_start(buf,"warning:")) priority = LOG_WARNING; - if (str_start(buf,"alert:")) priority = LOG_ALERT; - syslog(priority,"%s %s",stamp,buf); + if (str_start(buf, "warning:")) priority = LOG_WARNING; + if (str_start(buf, "alert:")) priority = LOG_ALERT; + syslog(priority, "%s %s", stamp, buf); flagcont = 1; } } bufpos = 0; } -int main(int argc,char **argv) +int main(int argc, char **argv) { char ch; if (argv[1]) if (argv[2]) { unsigned long facility; - scan_ulong(argv[2],&facility); - openlog(argv[1],0,facility << 3); - } - else - openlog(argv[1],0,LOG_MAIL); + scan_ulong(argv[2], &facility); + openlog(argv[1], 0, facility << 3); + } else + openlog(argv[1], 0, LOG_MAIL); else - openlog("splogger",0,LOG_MAIL); + openlog("splogger", 0, LOG_MAIL); for (;;) { - if (buffer_get(buffer_0,&ch,1) < 1) _exit(0); - if (ch == '\n') { flush(); flagcont = 0; continue; } + if (buffer_get(buffer_0, &ch, 1) < 1) _exit(0); + if (ch == '\n') { + flush(); + flagcont = 0; + continue; + } if (bufpos == sizeof(buf) - 1) flush(); if ((ch < 32) || (ch > 126)) ch = '?'; /* logger truncates at 0; GPACIC */ buf[bufpos++] = ch; @@ -21,150 +21,154 @@ e) Reduce code by 50% */ -#include <stdio.h> -#include <stdlib.h> +#include "srs2.h" + +#include <sys/time.h> /* timeval / timezone struct */ +#include <sys/types.h> /* tyepdefs */ + #include <ctype.h> #include <stdarg.h> -#include <time.h> /* time */ -#include <sys/types.h> /* tyepdefs */ -#include <sys/time.h> /* timeval / timezone struct */ -#include <string.h> /* memcpy, strcpy, memset */ -#include "srs2.h" +#include <stdio.h> +#include <stdlib.h> +#include <string.h> /* memcpy, strcpy, memset */ +#include <time.h> /* time */ + #include "sha1.h" #ifndef HAVE_STRCASECMP -# ifdef HAVE__STRICMP -# define strcasecmp _stricmp -# endif + #ifdef HAVE__STRICMP + #define strcasecmp _stricmp + #endif #endif #ifndef HAVE_STRNCASECMP -# ifdef HAVE__STRNICMP -# define strncasecmp _strnicmp -# endif + #ifdef HAVE__STRNICMP + #define strncasecmp _strnicmp + #endif #endif - /* Use this */ +/* Use this */ #define STRINGP(s) ((s != NULL) && (*(s) != '\0')) static const char *srs_separators = "=-+"; -static srs_malloc_t srs_f_malloc = malloc; -static srs_realloc_t srs_f_realloc = realloc; -static srs_free_t srs_f_free = free; +static srs_malloc_t srs_f_malloc = malloc; +static srs_realloc_t srs_f_realloc = realloc; +static srs_free_t srs_f_free = free; -int srs_set_malloc(srs_malloc_t m, srs_realloc_t r, srs_free_t f) +int srs_set_malloc(srs_malloc_t m, srs_realloc_t r, srs_free_t f) { - srs_f_malloc = m; - srs_f_realloc = r; - srs_f_free = f; - return SRS_SUCCESS; + srs_f_malloc = m; + srs_f_realloc = r; + srs_f_free = f; + return SRS_SUCCESS; } -#define X(e,s) if (code == e) return s; +#define X(e, s) \ + if (code == e) return s; const char *srs_strerror(int code) { - X(0,"") + X(0, "") /* Simple errors */ - X(SRS_SUCCESS,"Success") - X(SRS_ENOTSRSADDRESS,"Not an SRS address.") + X(SRS_SUCCESS, "Success") + X(SRS_ENOTSRSADDRESS, "Not an SRS address.") /* Config errors */ - X(SRS_ENOSECRETS,"No secrets in SRS configuration.") - X(SRS_ESEPARATORINVALID,"Invalid separator suggested.") + X(SRS_ENOSECRETS, "No secrets in SRS configuration.") + X(SRS_ESEPARATORINVALID, "Invalid separator suggested.") /* Input errors */ - X(SRS_ENOSENDERATSIGN,"No at sign in sender address") - X(SRS_EBUFTOOSMALL,"Buffer too small.") + X(SRS_ENOSENDERATSIGN, "No at sign in sender address") + X(SRS_EBUFTOOSMALL, "Buffer too small.") /* Syntax errors */ - X(SRS_ENOSRS0HOST,"No host in SRS0 address.") - X(SRS_ENOSRS0USER,"No user in SRS0 address.") - X(SRS_ENOSRS0HASH,"No hash in SRS0 address.") - X(SRS_ENOSRS0STAMP,"No timestamp in SRS0 address.") - X(SRS_ENOSRS1HOST,"No host in SRS1 address.") - X(SRS_ENOSRS1USER,"No user in SRS1 address.") - X(SRS_ENOSRS1HASH,"No hash in SRS1 address.") - X(SRS_EBADTIMESTAMPCHAR,"Bad base32 character in timestamp.") - X(SRS_EHASHTOOSHORT,"Hash too short in SRS address.") + X(SRS_ENOSRS0HOST, "No host in SRS0 address.") + X(SRS_ENOSRS0USER, "No user in SRS0 address.") + X(SRS_ENOSRS0HASH, "No hash in SRS0 address.") + X(SRS_ENOSRS0STAMP, "No timestamp in SRS0 address.") + X(SRS_ENOSRS1HOST, "No host in SRS1 address.") + X(SRS_ENOSRS1USER, "No user in SRS1 address.") + X(SRS_ENOSRS1HASH, "No hash in SRS1 address.") + X(SRS_EBADTIMESTAMPCHAR, "Bad base32 character in timestamp.") + X(SRS_EHASHTOOSHORT, "Hash too short in SRS address.") /* SRS errors */ - X(SRS_ETIMESTAMPOUTOFDATE,"Time stamp out of date.") - X(SRS_EHASHINVALID,"Hash invalid in SRS address.") + X(SRS_ETIMESTAMPOUTOFDATE, "Time stamp out of date.") + X(SRS_EHASHINVALID, "Hash invalid in SRS address.") - return "Unknown SRS error."; + return "Unknown SRS error."; } srs_t *srs_new() { - srs_t *srs = (srs_t *)srs_f_malloc(sizeof(srs_t)); - srs_init(srs); - return srs; + srs_t *srs = (srs_t *)srs_f_malloc(sizeof(srs_t)); + srs_init(srs); + return srs; } void srs_init(srs_t *srs) { - memset(srs, 0, sizeof(srs_t)); - srs->secrets = NULL; - srs->numsecrets = 0; - srs->separator = '='; - srs->maxage = 21; - srs->hashlen = 4; - srs->hashmin = srs->hashlen; - srs->alwaysrewrite = FALSE; + memset(srs, 0, sizeof(srs_t)); + srs->secrets = NULL; + srs->numsecrets = 0; + srs->separator = '='; + srs->maxage = 21; + srs->hashlen = 4; + srs->hashmin = srs->hashlen; + srs->alwaysrewrite = FALSE; } void srs_free(srs_t *srs) { - int i; - for (i = 0; i < srs->numsecrets; i++) { - memset(srs->secrets[i], 0, strlen(srs->secrets[i])); - srs_f_free(srs->secrets[i]); - srs->secrets[i] = '\0'; - } - srs_f_free(srs); + int i; + for (i = 0; i < srs->numsecrets; i++) { + memset(srs->secrets[i], 0, strlen(srs->secrets[i])); + srs_f_free(srs->secrets[i]); + srs->secrets[i] = '\0'; + } + srs_f_free(srs); } int srs_add_secret(srs_t *srs, const char *secret) { - int newlen = (srs->numsecrets + 1) * sizeof(char *); - srs->secrets = (char **)srs_f_realloc(srs->secrets, newlen); - srs->secrets[srs->numsecrets++] = strdup(secret); - return SRS_SUCCESS; + int newlen = (srs->numsecrets + 1) * sizeof(char *); + srs->secrets = (char **)srs_f_realloc(srs->secrets, newlen); + srs->secrets[srs->numsecrets++] = strdup(secret); + return SRS_SUCCESS; } const char *srs_get_secret(srs_t *srs, int idx) { - if (idx < srs->numsecrets) - return srs->secrets[idx]; - return NULL; + if (idx < srs->numsecrets) return srs->secrets[idx]; + return NULL; } -#define SRS_PARAM_DEFINE(n, t) \ - int srs_set_ ## n (srs_t *srs, t value) { \ - srs->n = value; \ - return SRS_SUCCESS; \ - } \ - t srs_get_ ## n (srs_t *srs) { \ - return srs->n; \ - } +#define SRS_PARAM_DEFINE(n, t) \ + int srs_set_##n(srs_t *srs, t value) \ + { \ + srs->n = value; \ + return SRS_SUCCESS; \ + } \ + t srs_get_##n(srs_t *srs) \ + { \ + return srs->n; \ + } int srs_set_separator(srs_t *srs, char value) { - if (strchr(srs_separators, value) == NULL) - return SRS_ESEPARATORINVALID; - srs->separator = value; - return SRS_SUCCESS; + if (strchr(srs_separators, value) == NULL) return SRS_ESEPARATORINVALID; + srs->separator = value; + return SRS_SUCCESS; } char srs_get_separator(srs_t *srs) { - return srs->separator; + return srs->separator; } SRS_PARAM_DEFINE(maxage, int) - /* XXX Check hashlen >= hashmin */ +/* XXX Check hashlen >= hashmin */ SRS_PARAM_DEFINE(hashlen, int) SRS_PARAM_DEFINE(hashmin, int) SRS_PARAM_DEFINE(alwaysrewrite, srs_bool) @@ -175,467 +179,421 @@ SRS_PARAM_DEFINE(noreverse, srs_bool) * enough to rewrite the timestamp functions. These are based on * a 2 character timestamp. Changing these in the wild is probably * a bad idea. */ -#define SRS_TIME_PRECISION (60 * 60 * 24) /* One day */ -#define SRS_TIME_BASEBITS 5 /* 2^5 = 32 = strlen(CHARS) */ +#define SRS_TIME_PRECISION (60 * 60 * 24) /* One day */ +#define SRS_TIME_BASEBITS 5 /* 2^5 = 32 = strlen(CHARS) */ /* This had better be a real variable since we do arithmethic * with it. */ const char *SRS_TIME_BASECHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"; -#define SRS_TIME_SIZE 2 -#define SRS_TIME_SLOTS (1<<(SRS_TIME_BASEBITS<<(SRS_TIME_SIZE-1))) +#define SRS_TIME_SIZE 2 +#define SRS_TIME_SLOTS (1 << (SRS_TIME_BASEBITS << (SRS_TIME_SIZE - 1))) int srs_timestamp_create(srs_t *srs, char *buf, time_t now) { - now = now / SRS_TIME_PRECISION; - buf[1] = SRS_TIME_BASECHARS[now & ((1 << SRS_TIME_BASEBITS) - 1)]; - now = now >> SRS_TIME_BASEBITS; - buf[0] = SRS_TIME_BASECHARS[now & ((1 << SRS_TIME_BASEBITS) - 1)]; - buf[2] = '\0'; - return SRS_SUCCESS; + now = now / SRS_TIME_PRECISION; + buf[1] = SRS_TIME_BASECHARS[now & ((1 << SRS_TIME_BASEBITS) - 1)]; + now = now >> SRS_TIME_BASEBITS; + buf[0] = SRS_TIME_BASECHARS[now & ((1 << SRS_TIME_BASEBITS) - 1)]; + buf[2] = '\0'; + return SRS_SUCCESS; } int srs_timestamp_check(srs_t *srs, const char *stamp) { - const char *sp; - char *bp; - int off; - time_t now; - time_t then; - - /* We had better go around this loop exactly twice! */ - then = 0; - for (sp = stamp; *sp; sp++) { - bp = strchr(SRS_TIME_BASECHARS, toupper(*sp)); - if (bp == NULL) - return SRS_EBADTIMESTAMPCHAR; - off = bp - SRS_TIME_BASECHARS; - then = (then << SRS_TIME_BASEBITS) | off; - } - - time(&now); - now = (now / SRS_TIME_PRECISION) % SRS_TIME_SLOTS; - while (now < then) - now = now + SRS_TIME_SLOTS; - - if (now <= then + srs->maxage) - return SRS_SUCCESS; - return SRS_ETIMESTAMPOUTOFDATE; + const char *sp; + char *bp; + int off; + time_t now; + time_t then; + + /* We had better go around this loop exactly twice! */ + then = 0; + for (sp = stamp; *sp; sp++) { + bp = strchr(SRS_TIME_BASECHARS, toupper(*sp)); + if (bp == NULL) return SRS_EBADTIMESTAMPCHAR; + off = bp - SRS_TIME_BASECHARS; + then = (then << SRS_TIME_BASEBITS) | off; + } + + time(&now); + now = (now / SRS_TIME_PRECISION) % SRS_TIME_SLOTS; + while (now < then) now = now + SRS_TIME_SLOTS; + + if (now <= then + srs->maxage) return SRS_SUCCESS; + return SRS_ETIMESTAMPOUTOFDATE; } -const char *SRS_HASH_BASECHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" - "abcdefghijklmnopqrstuvwxyz" - "0123456789+/"; +const char *SRS_HASH_BASECHARS = + "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz" + "0123456789+/"; static void srs_hash_create_v(srs_t *srs, int idx, char *buf, int nargs, va_list ap) { - sha1_ctx ctx; - char srshash[SHA1_DIGESTSIZE + 1]; - char *secret; - char *data; - int len; - char *lcdata; - unsigned char *hp; - char *bp; - int i; - int j; - - secret = srs->secrets[idx]; - sha1_init(&ctx); - sha1_update(&ctx, secret, strlen(secret)); - - for (i = 0; i < nargs; i++) { - data = va_arg(ap, char *); - len = strlen(data); - lcdata = alloca(len + 1); - for (j = 0; j < len; j++) { - if (isupper(data[j])) - lcdata[j] = tolower(data[j]); - else - lcdata[j] = data[j]; - } - sha1_update(&ctx, lcdata, len); - } - - sha1_final(srshash, &ctx); /* args inverted */ - srshash[SHA1_DIGESTSIZE] = '\0'; - - /* A little base64 encoding. Just a little. */ - hp = (unsigned char *)srshash; - bp = buf; - for (i = 0; i < srs->hashlen; i++) { - switch (i & 0x03) { - default: /* NOTREACHED */ - case 0: - j = (*hp >> 2); - break; - case 1: - j = ((*hp & 0x03) << 4) | - ((*(hp + 1) & 0xF0) >> 4); - hp++; - break; - case 2: - j = ((*hp & 0x0F) << 2) | - ((*(hp + 1) & 0xC0) >> 6); - hp++; - break; - case 3: - j = (*hp++ & 0x3F); - break; - } - *bp++ = SRS_HASH_BASECHARS[j]; - } - - *bp = '\0'; - buf[srs->hashlen] = '\0'; + sha1_ctx ctx; + char srshash[SHA1_DIGESTSIZE + 1]; + char *secret; + char *data; + int len; + char *lcdata; + unsigned char *hp; + char *bp; + int i; + int j; + + secret = srs->secrets[idx]; + sha1_init(&ctx); + sha1_update(&ctx, secret, strlen(secret)); + + for (i = 0; i < nargs; i++) { + data = va_arg(ap, char *); + len = strlen(data); + lcdata = alloca(len + 1); + for (j = 0; j < len; j++) { + if (isupper(data[j])) + lcdata[j] = tolower(data[j]); + else + lcdata[j] = data[j]; + } + sha1_update(&ctx, lcdata, len); + } + + sha1_final(srshash, &ctx); /* args inverted */ + srshash[SHA1_DIGESTSIZE] = '\0'; + + /* A little base64 encoding. Just a little. */ + hp = (unsigned char *)srshash; + bp = buf; + for (i = 0; i < srs->hashlen; i++) { + switch (i & 0x03) { + default: /* NOTREACHED */ + case 0: j = (*hp >> 2); break; + case 1: + j = ((*hp & 0x03) << 4) | ((*(hp + 1) & 0xF0) >> 4); + hp++; + break; + case 2: + j = ((*hp & 0x0F) << 2) | ((*(hp + 1) & 0xC0) >> 6); + hp++; + break; + case 3: j = (*hp++ & 0x3F); break; + } + *bp++ = SRS_HASH_BASECHARS[j]; + } + + *bp = '\0'; + buf[srs->hashlen] = '\0'; } int srs_hash_create(srs_t *srs, char *buf, int nargs, ...) { - va_list ap; + va_list ap; - if (srs->numsecrets == 0) - return SRS_ENOSECRETS; - if (srs->secrets == NULL) - return SRS_ENOSECRETS; - if (srs->secrets[0] == NULL) - return SRS_ENOSECRETS; + if (srs->numsecrets == 0) return SRS_ENOSECRETS; + if (srs->secrets == NULL) return SRS_ENOSECRETS; + if (srs->secrets[0] == NULL) return SRS_ENOSECRETS; - va_start(ap, nargs); - srs_hash_create_v(srs, 0, buf, nargs, ap); - va_end(ap); + va_start(ap, nargs); + srs_hash_create_v(srs, 0, buf, nargs, ap); + va_end(ap); - return SRS_SUCCESS; + return SRS_SUCCESS; } int srs_hash_check(srs_t *srs, char *hash, int nargs, ...) { - va_list ap; - char *srshash; - char *tmp; - int len; - int i; - - len = strlen(hash); - if (len < srs->hashmin) - return SRS_EHASHTOOSHORT; - if (len < srs->hashlen) { - tmp = alloca(srs->hashlen + 1); - strncpy(tmp, hash, srs->hashlen); - tmp[srs->hashlen] = '\0'; - hash = tmp; - len = srs->hashlen; - } - - for (i = 0; i < srs->numsecrets; i++) { - va_start(ap, nargs); - srshash = alloca(srs->hashlen + 1); - srs_hash_create_v(srs, i, srshash, nargs, ap); - va_end(ap); - if (strncasecmp(hash, srshash, len) == 0) - return SRS_SUCCESS; - } - - return SRS_EHASHINVALID; + va_list ap; + char *srshash; + char *tmp; + int len; + int i; + + len = strlen(hash); + if (len < srs->hashmin) return SRS_EHASHTOOSHORT; + if (len < srs->hashlen) { + tmp = alloca(srs->hashlen + 1); + strncpy(tmp, hash, srs->hashlen); + tmp[srs->hashlen] = '\0'; + hash = tmp; + len = srs->hashlen; + } + + for (i = 0; i < srs->numsecrets; i++) { + va_start(ap, nargs); + srshash = alloca(srs->hashlen + 1); + srs_hash_create_v(srs, i, srshash, nargs, ap); + va_end(ap); + if (strncasecmp(hash, srshash, len) == 0) return SRS_SUCCESS; + } + + return SRS_EHASHINVALID; } -int srs_compile_shortcut(srs_t *srs, - char *buf, int buflen, - char *sendhost, char *senduser, - const char *aliashost) { - char *srshash; - char srsstamp[SRS_TIME_SIZE + 1]; - int len; - int ret; - - /* This never happens if we get called from guarded() */ - if ((strncasecmp(senduser, SRS0TAG, 4) == 0) && - (strchr(srs_separators, senduser[4]) != NULL)) { - sendhost = senduser + 5; - if (*sendhost == '\0') - return SRS_ENOSRS0HOST; - senduser = strchr(sendhost, SRSSEP); - if ((senduser == NULL) || (*senduser == '\0')) - return SRS_ENOSRS0USER; - } - - len = strlen(SRS0TAG) + 1 + - srs->hashlen + 1 + - SRS_TIME_SIZE + 1 + - strlen(sendhost) + 1 + strlen(senduser) - + 1 + strlen(aliashost); - if (len >= buflen) - return SRS_EBUFTOOSMALL; - - ret = srs_timestamp_create(srs, srsstamp, time(NULL)); - if (ret != SRS_SUCCESS) - return ret; - srshash = alloca(srs->hashlen + 1); - ret = srs_hash_create(srs, srshash,3, srsstamp, sendhost, senduser); - if (ret != SRS_SUCCESS) - return ret; - - sprintf(buf, SRS0TAG "%c%s%c%s%c%s%c%s@%s", srs->separator, - srshash, SRSSEP, srsstamp, SRSSEP, - sendhost, SRSSEP, senduser, - aliashost); - - return SRS_SUCCESS; +int srs_compile_shortcut( + srs_t *srs, char *buf, int buflen, char *sendhost, char *senduser, const char *aliashost) +{ + char *srshash; + char srsstamp[SRS_TIME_SIZE + 1]; + int len; + int ret; + + /* This never happens if we get called from guarded() */ + if ((strncasecmp(senduser, SRS0TAG, 4) == 0) && (strchr(srs_separators, senduser[4]) != NULL)) { + sendhost = senduser + 5; + if (*sendhost == '\0') return SRS_ENOSRS0HOST; + senduser = strchr(sendhost, SRSSEP); + if ((senduser == NULL) || (*senduser == '\0')) return SRS_ENOSRS0USER; + } + + len = strlen(SRS0TAG) + 1 + srs->hashlen + 1 + SRS_TIME_SIZE + 1 + strlen(sendhost) + 1 + + strlen(senduser) + 1 + strlen(aliashost); + if (len >= buflen) return SRS_EBUFTOOSMALL; + + ret = srs_timestamp_create(srs, srsstamp, time(NULL)); + if (ret != SRS_SUCCESS) return ret; + srshash = alloca(srs->hashlen + 1); + ret = srs_hash_create(srs, srshash, 3, srsstamp, sendhost, senduser); + if (ret != SRS_SUCCESS) return ret; + + sprintf( + buf, + SRS0TAG "%c%s%c%s%c%s%c%s@%s", + srs->separator, + srshash, + SRSSEP, + srsstamp, + SRSSEP, + sendhost, + SRSSEP, + senduser, + aliashost); + + return SRS_SUCCESS; } -int srs_compile_guarded(srs_t *srs, - char *buf, int buflen, - char *sendhost, char *senduser, - const char *aliashost) { - char *srshost; - char *srsuser; - char *srshash; - int len; - int ret; - - if ((strncasecmp(senduser, SRS1TAG, 4) == 0) && - (strchr(srs_separators, senduser[4]) != NULL)) { - /* Used as a temporary convenience var */ - srshash = senduser + 5; - if (*srshash == '\0') - return SRS_ENOSRS1HASH; - /* Used as a temporary convenience var */ - srshost = strchr(srshash, SRSSEP); - if (!STRINGP(srshost)) - return SRS_ENOSRS1HOST; - *srshost++ = '\0'; - srsuser = strchr(srshost, SRSSEP); - if (!STRINGP(srsuser)) - return SRS_ENOSRS1USER; - *srsuser++ = '\0'; - srshash = alloca(srs->hashlen + 1); - ret = srs_hash_create(srs, srshash, 2, srshost, srsuser); - if (ret != SRS_SUCCESS) - return ret; - len = strlen(SRS1TAG) + 1 + - srs->hashlen + 1 + - strlen(srshost) + 1 + strlen(srsuser) - + 1 + strlen(aliashost); - if (len >= buflen) - return SRS_EBUFTOOSMALL; - sprintf(buf, SRS1TAG "%c%s%c%s%c%s@%s", srs->separator, - srshash, SRSSEP, - srshost, SRSSEP, srsuser, - aliashost); - return SRS_SUCCESS; - } - else if ((strncasecmp(senduser, SRS0TAG, 4) == 0) && - (strchr(srs_separators, senduser[4]) != NULL)) { - srsuser = senduser + 4; - srshost = sendhost; - srshash = alloca(srs->hashlen + 1); - ret = srs_hash_create(srs, srshash, 2, srshost, srsuser); - if (ret != SRS_SUCCESS) - return ret; - len = strlen(SRS1TAG) + 1 + - srs->hashlen + 1 + - strlen(srshost) + 1 + strlen(srsuser) - + 1 + strlen(aliashost); - if (len >= buflen) - return SRS_EBUFTOOSMALL; - sprintf(buf, SRS1TAG "%c%s%c%s%c%s@%s", srs->separator, - srshash, SRSSEP, - srshost, SRSSEP, srsuser, - aliashost); - } - else { - return srs_compile_shortcut(srs, buf, buflen, - sendhost, senduser, aliashost); - } - - return SRS_SUCCESS; +int srs_compile_guarded( + srs_t *srs, char *buf, int buflen, char *sendhost, char *senduser, const char *aliashost) +{ + char *srshost; + char *srsuser; + char *srshash; + int len; + int ret; + + if ((strncasecmp(senduser, SRS1TAG, 4) == 0) && (strchr(srs_separators, senduser[4]) != NULL)) { + /* Used as a temporary convenience var */ + srshash = senduser + 5; + if (*srshash == '\0') return SRS_ENOSRS1HASH; + /* Used as a temporary convenience var */ + srshost = strchr(srshash, SRSSEP); + if (!STRINGP(srshost)) return SRS_ENOSRS1HOST; + *srshost++ = '\0'; + srsuser = strchr(srshost, SRSSEP); + if (!STRINGP(srsuser)) return SRS_ENOSRS1USER; + *srsuser++ = '\0'; + srshash = alloca(srs->hashlen + 1); + ret = srs_hash_create(srs, srshash, 2, srshost, srsuser); + if (ret != SRS_SUCCESS) return ret; + len = strlen(SRS1TAG) + 1 + srs->hashlen + 1 + strlen(srshost) + 1 + strlen(srsuser) + 1 + + strlen(aliashost); + if (len >= buflen) return SRS_EBUFTOOSMALL; + sprintf( + buf, + SRS1TAG "%c%s%c%s%c%s@%s", + srs->separator, + srshash, + SRSSEP, + srshost, + SRSSEP, + srsuser, + aliashost); + return SRS_SUCCESS; + } else if ((strncasecmp(senduser, SRS0TAG, 4) == 0) && (strchr(srs_separators, senduser[4]) != NULL)) { + srsuser = senduser + 4; + srshost = sendhost; + srshash = alloca(srs->hashlen + 1); + ret = srs_hash_create(srs, srshash, 2, srshost, srsuser); + if (ret != SRS_SUCCESS) return ret; + len = strlen(SRS1TAG) + 1 + srs->hashlen + 1 + strlen(srshost) + 1 + strlen(srsuser) + 1 + + strlen(aliashost); + if (len >= buflen) return SRS_EBUFTOOSMALL; + sprintf( + buf, + SRS1TAG "%c%s%c%s%c%s@%s", + srs->separator, + srshash, + SRSSEP, + srshost, + SRSSEP, + srsuser, + aliashost); + } else { + return srs_compile_shortcut(srs, buf, buflen, sendhost, senduser, aliashost); + } + + return SRS_SUCCESS; } int srs_parse_shortcut(srs_t *srs, char *buf, int buflen, char *senduser) { - char *srshash; - char *srsstamp; - char *srshost; - char *srsuser; - int ret; - - if (strncasecmp(senduser, SRS0TAG, 4) == 0) { - srshash = senduser + 5; - if (!STRINGP(srshash)) - return SRS_ENOSRS0HASH; - srsstamp = strchr(srshash, SRSSEP); - if (!STRINGP(srsstamp)) - return SRS_ENOSRS0STAMP; - *srsstamp++ = '\0'; - srshost = strchr(srsstamp, SRSSEP); - if (!STRINGP(srshost)) - return SRS_ENOSRS0HOST; - *srshost++ = '\0'; - srsuser = strchr(srshost, SRSSEP); - if (!STRINGP(srsuser)) - return SRS_ENOSRS0USER; - *srsuser++ = '\0'; - ret = srs_timestamp_check(srs, srsstamp); - if (ret != SRS_SUCCESS) - return ret; - ret = srs_hash_check(srs, srshash, 3, srsstamp, - srshost, srsuser); - if (ret != SRS_SUCCESS) - return ret; - sprintf(buf, "%s@%s", srsuser, srshost); - return SRS_SUCCESS; - } - - return SRS_ENOTSRSADDRESS; + char *srshash; + char *srsstamp; + char *srshost; + char *srsuser; + int ret; + + if (strncasecmp(senduser, SRS0TAG, 4) == 0) { + srshash = senduser + 5; + if (!STRINGP(srshash)) return SRS_ENOSRS0HASH; + srsstamp = strchr(srshash, SRSSEP); + if (!STRINGP(srsstamp)) return SRS_ENOSRS0STAMP; + *srsstamp++ = '\0'; + srshost = strchr(srsstamp, SRSSEP); + if (!STRINGP(srshost)) return SRS_ENOSRS0HOST; + *srshost++ = '\0'; + srsuser = strchr(srshost, SRSSEP); + if (!STRINGP(srsuser)) return SRS_ENOSRS0USER; + *srsuser++ = '\0'; + ret = srs_timestamp_check(srs, srsstamp); + if (ret != SRS_SUCCESS) return ret; + ret = srs_hash_check(srs, srshash, 3, srsstamp, srshost, srsuser); + if (ret != SRS_SUCCESS) return ret; + sprintf(buf, "%s@%s", srsuser, srshost); + return SRS_SUCCESS; + } + + return SRS_ENOTSRSADDRESS; } int srs_parse_guarded(srs_t *srs, char *buf, int buflen, char *senduser) { - char *srshash; - char *srshost; - char *srsuser; - int ret; - - if (strncasecmp(senduser, SRS1TAG, 4) == 0) { - srshash = senduser + 5; - if (!STRINGP(srshash)) - return SRS_ENOSRS1HASH; - srshost = strchr(srshash, SRSSEP); - if (!STRINGP(srshost)) - return SRS_ENOSRS1HOST; - *srshost++ = '\0'; - srsuser = strchr(srshost, SRSSEP); - if (!STRINGP(srsuser)) - return SRS_ENOSRS1USER; - *srsuser++ = '\0'; - ret = srs_hash_check(srs, srshash, 2, srshost, srsuser); - if (ret != SRS_SUCCESS) - return ret; - sprintf(buf, SRS0TAG "%s@%s", srsuser, srshost); - return SRS_SUCCESS; - } - else { - return srs_parse_shortcut(srs, buf, buflen, senduser); - } + char *srshash; + char *srshost; + char *srsuser; + int ret; + + if (strncasecmp(senduser, SRS1TAG, 4) == 0) { + srshash = senduser + 5; + if (!STRINGP(srshash)) return SRS_ENOSRS1HASH; + srshost = strchr(srshash, SRSSEP); + if (!STRINGP(srshost)) return SRS_ENOSRS1HOST; + *srshost++ = '\0'; + srsuser = strchr(srshost, SRSSEP); + if (!STRINGP(srsuser)) return SRS_ENOSRS1USER; + *srsuser++ = '\0'; + ret = srs_hash_check(srs, srshash, 2, srshost, srsuser); + if (ret != SRS_SUCCESS) return ret; + sprintf(buf, SRS0TAG "%s@%s", srsuser, srshost); + return SRS_SUCCESS; + } else { + return srs_parse_shortcut(srs, buf, buflen, senduser); + } } -int srs_forward(srs_t *srs, char *buf, int buflen, - const char *sender, const char *alias) +int srs_forward(srs_t *srs, char *buf, int buflen, const char *sender, const char *alias) { - char *senduser; - char *sendhost; - char *tmp; - int len; - - if (srs->noforward) - return SRS_ENOTREWRITTEN; - - /* This is allowed to be a plain domain */ - while ((tmp = strchr(alias, '@')) != NULL) - alias = tmp + 1; - - tmp = strchr(sender, '@'); - if (tmp == NULL) - return SRS_ENOSENDERATSIGN; - sendhost = tmp + 1; - - len = strlen(sender); - - if (! srs->alwaysrewrite) { - if (strcasecmp(sendhost, alias) == 0) { - if (strlen(sender) >= buflen) - return SRS_EBUFTOOSMALL; - strcpy(buf, sender); - return SRS_SUCCESS; - } - } - - /* Reconstruct the whole show into our alloca() buffer. */ - senduser = alloca(len + 1); - strcpy(senduser, sender); - tmp = (senduser + (tmp - sender)); - sendhost = tmp + 1; - *tmp = '\0'; - - return srs_compile_guarded(srs, buf, buflen, - sendhost, senduser, alias); + char *senduser; + char *sendhost; + char *tmp; + int len; + + if (srs->noforward) return SRS_ENOTREWRITTEN; + + /* This is allowed to be a plain domain */ + while ((tmp = strchr(alias, '@')) != NULL) alias = tmp + 1; + + tmp = strchr(sender, '@'); + if (tmp == NULL) return SRS_ENOSENDERATSIGN; + sendhost = tmp + 1; + + len = strlen(sender); + + if (!srs->alwaysrewrite) { + if (strcasecmp(sendhost, alias) == 0) { + if (strlen(sender) >= buflen) return SRS_EBUFTOOSMALL; + strcpy(buf, sender); + return SRS_SUCCESS; + } + } + + /* Reconstruct the whole show into our alloca() buffer. */ + senduser = alloca(len + 1); + strcpy(senduser, sender); + tmp = (senduser + (tmp - sender)); + sendhost = tmp + 1; + *tmp = '\0'; + + return srs_compile_guarded(srs, buf, buflen, sendhost, senduser, alias); } -int srs_forward_alloc(srs_t *srs, char **sptr, - const char *sender, const char *alias) +int srs_forward_alloc(srs_t *srs, char **sptr, const char *sender, const char *alias) { - char *buf; - int slen; - int alen; - int len; - int ret; + char *buf; + int slen; + int alen; + int len; + int ret; - if (srs->noforward) - return SRS_ENOTREWRITTEN; + if (srs->noforward) return SRS_ENOTREWRITTEN; - slen = strlen(sender); - alen = strlen(alias); + slen = strlen(sender); + alen = strlen(alias); - /* strlen(SRSxTAG) + strlen("====+@") < 64 */ - len = slen + alen + srs->hashlen + SRS_TIME_SIZE + 64; - buf = (char *)srs_f_malloc(len); + /* strlen(SRSxTAG) + strlen("====+@") < 64 */ + len = slen + alen + srs->hashlen + SRS_TIME_SIZE + 64; + buf = (char *)srs_f_malloc(len); - ret = srs_forward(srs, buf, len, sender, alias); + ret = srs_forward(srs, buf, len, sender, alias); - if (ret == SRS_SUCCESS) - *sptr = buf; - else - srs_f_free(buf); + if (ret == SRS_SUCCESS) + *sptr = buf; + else + srs_f_free(buf); - return ret; + return ret; } int srs_reverse(srs_t *srs, char *buf, int buflen, const char *sender) { - char *senduser; - char *tmp; - int len; - - if (!SRS_IS_SRS_ADDRESS(sender)) - return SRS_ENOTSRSADDRESS; - - if (srs->noreverse) - return SRS_ENOTREWRITTEN; - - len = strlen(sender); - if (len >= buflen) - return SRS_EBUFTOOSMALL; - senduser = alloca(len + 1); - strcpy(senduser, sender); - - /* We don't really care about the host for reversal. */ - tmp = strchr(senduser, '@'); - if (tmp != NULL) - *tmp = '\0'; - return srs_parse_guarded(srs, buf, buflen, senduser); + char *senduser; + char *tmp; + int len; + + if (!SRS_IS_SRS_ADDRESS(sender)) return SRS_ENOTSRSADDRESS; + + if (srs->noreverse) return SRS_ENOTREWRITTEN; + + len = strlen(sender); + if (len >= buflen) return SRS_EBUFTOOSMALL; + senduser = alloca(len + 1); + strcpy(senduser, sender); + + /* We don't really care about the host for reversal. */ + tmp = strchr(senduser, '@'); + if (tmp != NULL) *tmp = '\0'; + return srs_parse_guarded(srs, buf, buflen, senduser); } int srs_reverse_alloc(srs_t *srs, char **sptr, const char *sender) { - char *buf; - int len; - int ret; + char *buf; + int len; + int ret; - *sptr = NULL; + *sptr = NULL; - if (!SRS_IS_SRS_ADDRESS(sender)) - return SRS_ENOTSRSADDRESS; + if (!SRS_IS_SRS_ADDRESS(sender)) return SRS_ENOTSRSADDRESS; - if (srs->noreverse) - return SRS_ENOTREWRITTEN; + if (srs->noreverse) return SRS_ENOTREWRITTEN; - len = strlen(sender) + 1; - buf = (char *)srs_f_malloc(len); + len = strlen(sender) + 1; + buf = (char *)srs_f_malloc(len); - ret = srs_reverse(srs, buf, len, sender); + ret = srs_reverse(srs, buf, len, sender); - if (ret == SRS_SUCCESS) - *sptr = buf; - else - srs_f_free(buf); + if (ret == SRS_SUCCESS) + *sptr = buf; + else + srs_f_free(buf); - return ret; + return ret; } diff --git a/src/srsforward.c b/src/srsforward.c index c8aefc9..c855d36 100644 --- a/src/srsforward.c +++ b/src/srsforward.c @@ -1,24 +1,32 @@ -#include <unistd.h> #include <sys/types.h> -#include "control.h" -#include "sig.h" +#include <unistd.h> + +#include "buffer.h" #include "constmap.h" -#include "readwrite.h" -#include "exit.h" #include "env.h" -#include "qmail.h" -#include "auto_qmail.h" -#include "buffer.h" -#include "str.h" +#include "exit.h" #include "fmt.h" -#include "stralloc.h" #include "logmsg.h" +#include "readwrite.h" +#include "sig.h" +#include "str.h" +#include "stralloc.h" + +#include "auto_qmail.h" +#include "control.h" +#include "qmail.h" #include "srs2.h" #define WHO "srsforward" -void die_nomem() { logmsg(WHO,111,FATAL,"out of memory"); } -void die_control() { logmsg(WHO,110,FATAL,"Unable to read control files"); } +void die_nomem() +{ + logmsg(WHO, 111, FATAL, "out of memory"); +} +void die_control() +{ + logmsg(WHO, 110, FATAL, "Unable to read control files"); +} struct qmail qqt; char *srsdomaininfo = 0; @@ -35,27 +43,28 @@ stralloc srserror = {0}; 110 control file not readable */ -static int srserror_str(int code) { - if (!stralloc_copys(&srserror,"SRS: ")) die_nomem(); - if (!stralloc_cats(&srserror,srs_strerror(code))) die_nomem(); +static int srserror_str(int code) +{ + if (!stralloc_copys(&srserror, "SRS: ")) die_nomem(); + if (!stralloc_cats(&srserror, srs_strerror(code))) die_nomem(); if (!stralloc_0(&srserror)) die_nomem(); return -3; } -ssize_t mywrite(int fd,char *buf,int len) +ssize_t mywrite(int fd, char *buf, int len) { - qmail_put(&qqt,buf,len); + qmail_put(&qqt, buf, len); return len; } char inbuf[BUFFER_INSIZE]; char outbuf[1]; -buffer bi = BUFFER_INIT(read,0,inbuf,sizeof(inbuf)); -buffer bo = BUFFER_INIT(mywrite,-1,outbuf,sizeof(outbuf)); +buffer bi = BUFFER_INIT(read, 0, inbuf, sizeof(inbuf)); +buffer bo = BUFFER_INIT(mywrite, -1, outbuf, sizeof(outbuf)); char num[FMT_ULONG]; -int main(int argc,char **argv) +int main(int argc, char **argv) { int i, j, r; char *qqx; @@ -70,57 +79,59 @@ int main(int argc,char **argv) int alwaysrewrite = 0; sig_pipeignore(); - + sender = env_get("NEWSENDER"); - if (!sender) - logmsg(WHO,100,FATAL,"NEWSENDER not set"); + if (!sender) logmsg(WHO, 100, FATAL, "NEWSENDER not set"); host = env_get("HOST"); - if (!host) - logmsg(WHO,100,FATAL,"HOST not set"); + if (!host) logmsg(WHO, 100, FATAL, "HOST not set"); dtline = env_get("DTLINE"); - if (!dtline) - logmsg(WHO,100,FATAL,"DTLINE not set"); + if (!dtline) logmsg(WHO, 100, FATAL, "DTLINE not set"); - if (chdir(auto_qmail) == -1) - logmsg(WHO,111,FATAL,B("unable to chdir to: ",auto_qmail)); + if (chdir(auto_qmail) == -1) logmsg(WHO, 111, FATAL, B("unable to chdir to: ", auto_qmail)); - if (!stralloc_cats(&srshost,"!")) die_nomem(); - if (!stralloc_cats(&srshost,host)) die_nomem(); + if (!stralloc_cats(&srshost, "!")) die_nomem(); + if (!stralloc_cats(&srshost, host)) die_nomem(); - switch (control_readfile(&srsdomains,"control/srsdomains",0)) { + switch (control_readfile(&srsdomains, "control/srsdomains", 0)) { case -1: die_control(); - case 0: if (!constmap_init(&mapsrsdomains,"",0,1)) die_nomem(); break; - case 1: if (!constmap_init(&mapsrsdomains,srsdomains.s,srsdomains.len,1)) die_nomem(); break; + case 0: + if (!constmap_init(&mapsrsdomains, "", 0, 1)) die_nomem(); + break; + case 1: + if (!constmap_init(&mapsrsdomains, srsdomains.s, srsdomains.len, 1)) die_nomem(); + break; } - if (constmap(&mapsrsdomains,srshost.s,srshost.len)) return 0; // domain blacklisted - if ((srsdomaininfo = constmap(&mapsrsdomains,host,str_len(host))) == 0) { - if ((srsdomaininfo = constmap(&mapsrsdomains,"*",1)) == 0) return 0; // '*' means always SRS - else alwaysrewrite = 1; + if (constmap(&mapsrsdomains, srshost.s, srshost.len)) return 0; // domain blacklisted + if ((srsdomaininfo = constmap(&mapsrsdomains, host, str_len(host))) == 0) { + if ((srsdomaininfo = constmap(&mapsrsdomains, "*", 1)) == 0) + return 0; // '*' means always SRS + else + alwaysrewrite = 1; } if (*srsdomaininfo) { - i = str_chr(srsdomaininfo,'|'); // multiple cookies; separated by ' ' + i = str_chr(srsdomaininfo, '|'); // multiple cookies; separated by ' ' if (srsdomaininfo[i] == '|') { srsdomaininfo[i] = 0; - j = str_chr(srsdomaininfo + i + 1,'|'); + j = str_chr(srsdomaininfo + i + 1, '|'); if (srsdomaininfo[i + j + 1] == '|') { srsdomaininfo[i + j + 1] = 0; - sendhost = srsdomaininfo + i + j + 2; // separator: - + = + sendhost = srsdomaininfo + i + j + 2; // separator: - + = } separator = srsdomaininfo[i + 1]; } - if (!stralloc_copys(&cookie,srsdomaininfo)) die_nomem(); + if (!stralloc_copys(&cookie, srsdomaininfo)) die_nomem(); if (!stralloc_0(&cookie)) die_nomem(); - if (!stralloc_copys(&srshost,"")) die_nomem(); + if (!stralloc_copys(&srshost, "")) die_nomem(); if (*sendhost) { j = str_len(sendhost); if (sendhost[j - 1] == '.') { - if (!stralloc_copys(&srshost,sendhost)) die_nomem(); - if (!stralloc_cats(&srshost,host)) die_nomem(); - } else - if (!stralloc_copys(&srshost,sendhost)) die_nomem(); - } else - if (!stralloc_copys(&srshost,host)) die_nomem(); + if (!stralloc_copys(&srshost, sendhost)) die_nomem(); + if (!stralloc_cats(&srshost, host)) die_nomem(); + } else if (!stralloc_copys(&srshost, sendhost)) + die_nomem(); + } else if (!stralloc_copys(&srshost, host)) + die_nomem(); if (!stralloc_0(&srshost)) die_nomem(); } else die_control(); @@ -129,41 +140,41 @@ int main(int argc,char **argv) srs = srs_new(); - if (separator == '-' || separator == '+' || separator == '=') { // '=' is default - r = srs_set_separator(srs,separator); + if (separator == '-' || separator == '+' || separator == '=') { // '=' is default + r = srs_set_separator(srs, separator); if (r != SRS_SUCCESS) return srserror_str(r); } if (alwaysrewrite) { - r = srs_set_alwaysrewrite(srs,alwaysrewrite); + r = srs_set_alwaysrewrite(srs, alwaysrewrite); if (r != SRS_SUCCESS) return srserror_str(r); } for (j = 0, i = 0; j < cookie.len; j++) { - if (cookie.s[j] == ' ' || cookie.s[j] == '\0' ) { - cookie.s[j] = '\0'; - r = srs_add_secret(srs,cookie.s + i); + if (cookie.s[j] == ' ' || cookie.s[j] == '\0') { + cookie.s[j] = '\0'; + r = srs_add_secret(srs, cookie.s + i); if (r != SRS_SUCCESS) return srserror_str(r); - i = j + 1; - if (cookie.s[i] == ' ') { j++; continue; } + i = j + 1; + if (cookie.s[i] == ' ') { + j++; + continue; + } } } - if ((r = srs_forward(srs,srssender,sizeof(srssender),sender,srshost.s)) != SRS_SUCCESS) - logmsg(WHO,100,FATAL,B("Unable to forward: ",sender," ",srs_strerror(r))); + if ((r = srs_forward(srs, srssender, sizeof(srssender), sender, srshost.s)) != SRS_SUCCESS) + logmsg(WHO, 100, FATAL, B("Unable to forward: ", sender, " ", srs_strerror(r))); - if (qmail_open(&qqt) == -1) - logmsg(WHO,111,FATAL,"unable to fork: "); - qmail_puts(&qqt,dtline); - if (buffer_copy(&bo,&bi) != 0) - logmsg(WHO,111,FATAL,"unable to read message: "); + if (qmail_open(&qqt) == -1) logmsg(WHO, 111, FATAL, "unable to fork: "); + qmail_puts(&qqt, dtline); + if (buffer_copy(&bo, &bi) != 0) logmsg(WHO, 111, FATAL, "unable to read message: "); buffer_flush(&bo); - num[fmt_ulong(num,qmail_qp(&qqt))] = 0; - - qmail_from(&qqt,srssender); - while (*++argv) qmail_to(&qqt,*argv); - qqx = qmail_close(&qqt); - if (*qqx) logmsg(WHO,*qqx == 'D' ? 100 : 111,FATAL,qqx + 1); - logmsg(WHO,0,LOG,B(srssender,": qp ",num)); + num[fmt_ulong(num, qmail_qp(&qqt))] = 0; + qmail_from(&qqt, srssender); + while (*++argv) qmail_to(&qqt, *argv); + qqx = qmail_close(&qqt); + if (*qqx) logmsg(WHO, *qqx == 'D' ? 100 : 111, FATAL, qqx + 1); + logmsg(WHO, 0, LOG, B(srssender, ": qp ", num)); } diff --git a/src/srsreverse.c b/src/srsreverse.c index dc2171d..33d0e5a 100644 --- a/src/srsreverse.c +++ b/src/srsreverse.c @@ -1,26 +1,34 @@ -#include <unistd.h> #include <sys/types.h> -#include "control.h" -#include "sig.h" -#include "constmap.h" -#include "readwrite.h" -#include "exit.h" -#include "env.h" -#include "qmail.h" -#include "auto_qmail.h" -#include "auto_break.h" +#include <unistd.h> + #include "buffer.h" #include "case.h" -#include "str.h" +#include "constmap.h" +#include "env.h" +#include "exit.h" #include "fmt.h" -#include "stralloc.h" #include "logmsg.h" +#include "readwrite.h" +#include "sig.h" +#include "str.h" +#include "stralloc.h" + +#include "auto_break.h" +#include "auto_qmail.h" +#include "control.h" +#include "qmail.h" #include "srs2.h" #define WHO "srsreverse" -void die_nomem() { logmsg(WHO,111,FATAL,"out of memory"); } -void die_control() { logmsg(WHO,110,FATAL,"Unable to read control files"); } +void die_nomem() +{ + logmsg(WHO, 111, FATAL, "out of memory"); +} +void die_control() +{ + logmsg(WHO, 110, FATAL, "Unable to read control files"); +} struct qmail qqt; char *vdomainuser = 0; @@ -40,23 +48,24 @@ stralloc srshost = {0}; 110 control file not readable */ -static int srserror_str(int code) { - if (!stralloc_copys(&srserror,"SRS: ")) die_nomem(); - if (!stralloc_cats(&srserror,srs_strerror(code))) die_nomem(); +static int srserror_str(int code) +{ + if (!stralloc_copys(&srserror, "SRS: ")) die_nomem(); + if (!stralloc_cats(&srserror, srs_strerror(code))) die_nomem(); if (!stralloc_0(&srserror)) die_nomem(); return -3; } -ssize_t mywrite(int fd,char *buf,int len) +ssize_t mywrite(int fd, char *buf, int len) { - qmail_put(&qqt,buf,len); + qmail_put(&qqt, buf, len); return len; } char inbuf[BUFFER_INSIZE]; char outbuf[1]; -buffer bi = BUFFER_INIT(read,0,inbuf,sizeof(inbuf)); -buffer bo = BUFFER_INIT(mywrite,-1,outbuf,sizeof(outbuf)); +buffer bi = BUFFER_INIT(read, 0, inbuf, sizeof(inbuf)); +buffer bo = BUFFER_INIT(mywrite, -1, outbuf, sizeof(outbuf)); char num[FMT_ULONG]; @@ -73,100 +82,104 @@ int main() char *host = 0; sig_pipeignore(); - + recipient = env_get("RECIPIENT"); - if (!recipient) - logmsg(WHO,100,FATAL,"RECIPIENT not set"); + if (!recipient) logmsg(WHO, 100, FATAL, "RECIPIENT not set"); dtline = env_get("DTLINE"); - if (!dtline) - logmsg(WHO,100,FATAL,"DTLINE not set"); + if (!dtline) logmsg(WHO, 100, FATAL, "DTLINE not set"); host = env_get("HOST"); - if (!host) - logmsg(WHO,100,FATAL,"HOST not set"); - - if (chdir(auto_qmail) == -1) - logmsg(WHO,111,FATAL,B("unable to chdir to: ",auto_qmail)); + if (!host) logmsg(WHO, 100, FATAL, "HOST not set"); + + if (chdir(auto_qmail) == -1) logmsg(WHO, 111, FATAL, B("unable to chdir to: ", auto_qmail)); /* Check for particular virtual SRS domain user */ - switch (control_readfile(&vdomains,"control/virtualdomains",0)) { + switch (control_readfile(&vdomains, "control/virtualdomains", 0)) { case -1: die_control(); - case 0: if (!constmap_init(&mapvdomains,"",0,1)) die_nomem(); break; - case 1: if (!constmap_init(&mapvdomains,vdomains.s,vdomains.len,1)) die_nomem(); break; + case 0: + if (!constmap_init(&mapvdomains, "", 0, 1)) die_nomem(); + break; + case 1: + if (!constmap_init(&mapvdomains, vdomains.s, vdomains.len, 1)) die_nomem(); + break; } j = str_len(host); - for (i = 0; i <= j; ++i) + for (i = 0; i <= j; ++i) if ((i == 0) || (host[i] == '.')) { - if (!stralloc_copys(&srshost,"")) die_nomem(); - if (!stralloc_catb(&srshost,host + i,j - i)) die_nomem(); - if ((srsdomaininfo = constmap(&mapvdomains,srshost.s,srshost.len)) != 0) goto SRSDOMAINS; + if (!stralloc_copys(&srshost, "")) die_nomem(); + if (!stralloc_catb(&srshost, host + i, j - i)) die_nomem(); + if ((srsdomaininfo = constmap(&mapvdomains, srshost.s, srshost.len)) != 0) goto SRSDOMAINS; } - if (!stralloc_copys(&srshost,host)) die_nomem(); + if (!stralloc_copys(&srshost, host)) die_nomem(); - SRSDOMAINS: +SRSDOMAINS: - switch (control_readfile(&srsdomains,"control/srsdomains",0)) { + switch (control_readfile(&srsdomains, "control/srsdomains", 0)) { case -1: die_control(); - case 0: if (!constmap_init(&mapsrsdomains,"",0,1)) die_nomem(); break; - case 1: if (!constmap_init(&mapsrsdomains,srsdomains.s,srsdomains.len,1)) die_nomem(); break; + case 0: + if (!constmap_init(&mapsrsdomains, "", 0, 1)) die_nomem(); + break; + case 1: + if (!constmap_init(&mapsrsdomains, srsdomains.s, srsdomains.len, 1)) die_nomem(); + break; } - if ((srsdomaininfo = constmap(&mapsrsdomains,srshost.s,srshost.len)) == 0) - if ((srsdomaininfo = constmap(&mapsrsdomains,"*",1)) == 0) return 0; + if ((srsdomaininfo = constmap(&mapsrsdomains, srshost.s, srshost.len)) == 0) + if ((srsdomaininfo = constmap(&mapsrsdomains, "*", 1)) == 0) return 0; if (*srsdomaininfo) { - i = str_chr(srsdomaininfo,'|'); // multiple cookies; separated by ' ' + i = str_chr(srsdomaininfo, '|'); // multiple cookies; separated by ' ' if (srsdomaininfo[i] == '|') { - srsdomaininfo[i] = 0; - separator = srsdomaininfo[i+1]; + srsdomaininfo[i] = 0; + separator = srsdomaininfo[i + 1]; } - if (!stralloc_copys(&cookie,srsdomaininfo)) die_nomem(); + if (!stralloc_copys(&cookie, srsdomaininfo)) die_nomem(); if (!stralloc_0(&cookie)) die_nomem(); } /* strip virtual user from recipient */ - if ((vdomainuser = constmap(&mapvdomains,host,j))) { - i = str_chr(recipient,*auto_break); - if (!case_diffb(recipient,i - 1,vdomainuser)) recipient += i + 1; + if ((vdomainuser = constmap(&mapvdomains, host, j))) { + i = str_chr(recipient, *auto_break); + if (!case_diffb(recipient, i - 1, vdomainuser)) recipient += i + 1; } - + /* Let's go SRS reverse */ srs = srs_new(); - if (separator == '-' || separator == '+' || separator == '=') { // '=' is default - r = srs_set_separator(srs,separator); + if (separator == '-' || separator == '+' || separator == '=') { // '=' is default + r = srs_set_separator(srs, separator); if (r != SRS_SUCCESS) return srserror_str(r); } for (j = 0, i = 0; j < cookie.len; j++) { - if (cookie.s[j] == ' ' || cookie.s[j] == '\0' ) { - cookie.s[j] = '\0'; - r = srs_add_secret(srs,cookie.s + i); + if (cookie.s[j] == ' ' || cookie.s[j] == '\0') { + cookie.s[j] = '\0'; + r = srs_add_secret(srs, cookie.s + i); if (r != SRS_SUCCESS) return srserror_str(r); - i = j + 1; - if (cookie.s[i] == ' ') { j++; continue; } + i = j + 1; + if (cookie.s[i] == ' ') { + j++; + continue; + } } } - if ((r = srs_reverse(srs,srsrecipient,sizeof(srsrecipient),recipient)) != SRS_SUCCESS) { - logmsg(WHO,100,FATAL,B("unable to reverse: ",recipient," ",srs_strerror(r))); + if ((r = srs_reverse(srs, srsrecipient, sizeof(srsrecipient), recipient)) != SRS_SUCCESS) { + logmsg(WHO, 100, FATAL, B("unable to reverse: ", recipient, " ", srs_strerror(r))); } - if (qmail_open(&qqt) == -1) - logmsg(WHO,111,FATAL,"unable to fork: "); - qmail_puts(&qqt,dtline); - if (buffer_copy(&bo,&bi) != 0) - logmsg(WHO,111,FATAL,"unable to read message: "); + if (qmail_open(&qqt) == -1) logmsg(WHO, 111, FATAL, "unable to fork: "); + qmail_puts(&qqt, dtline); + if (buffer_copy(&bo, &bi) != 0) logmsg(WHO, 111, FATAL, "unable to read message: "); buffer_flush(&bo); - num[fmt_ulong(num,qmail_qp(&qqt))] = 0; - - qmail_from(&qqt,""); - qmail_to(&qqt,srsrecipient); - qqx = qmail_close(&qqt); - if (*qqx) logmsg(WHO,*qqx == 'D' ? 100 : 111,FATAL,qqx + 1); - logmsg(WHO,0,LOG,B(srsrecipient,": qp ",num)); + num[fmt_ulong(num, qmail_qp(&qqt))] = 0; + qmail_from(&qqt, ""); + qmail_to(&qqt, srsrecipient); + qqx = qmail_close(&qqt); + if (*qqx) logmsg(WHO, *qqx == 'D' ? 100 : 111, FATAL, qqx + 1); + logmsg(WHO, 0, LOG, B(srsrecipient, ": qp ", num)); } diff --git a/src/strset.c b/src/strset.c index 8f3ffe8..3f3a855 100644 --- a/src/strset.c +++ b/src/strset.c @@ -1,7 +1,8 @@ #include "strset.h" -#include "str.h" -#include "byte.h" + #include "alloc.h" +#include "byte.h" +#include "str.h" uint32 strset_hash(char *s) { @@ -24,20 +25,26 @@ int strset_init(strset *set) set->n = 0; set->a = 10; - set->first = (int *) alloc(sizeof(int) * (set->mask + 1)); + set->first = (int *)alloc(sizeof(int) * (set->mask + 1)); if (!set->first) return 0; - set->p = (strset_list *) alloc(sizeof(strset_list) * set->a); - if (!set->p) { alloc_free(set->first); return 0; } - set->x = (char **) alloc(sizeof(char *) * set->a); - if (!set->x) { alloc_free(set->p); alloc_free(set->first); return 0; } + set->p = (strset_list *)alloc(sizeof(strset_list) * set->a); + if (!set->p) { + alloc_free(set->first); + return 0; + } + set->x = (char **)alloc(sizeof(char *) * set->a); + if (!set->x) { + alloc_free(set->p); + alloc_free(set->first); + return 0; + } - for (h = 0; h <= set->mask; ++h) - set->first[h] = -1; + for (h = 0; h <= set->mask; ++h) set->first[h] = -1; return 1; } -char *strset_in(strset *set,char *s) +char *strset_in(strset *set, char *s) { uint32 h; strset_list *sl; @@ -51,14 +58,14 @@ char *strset_in(strset *set,char *s) sl = set->p + i; if (sl->h == h) { xi = set->x[i]; - if (!str_diff(xi,s)) return xi; + if (!str_diff(xi, s)) return xi; } i = sl->next; } return 0; } -int strset_add(strset *set,char *s) +int strset_add(strset *set, char *s) { uint32 h; int n; @@ -72,13 +79,16 @@ int strset_add(strset *set,char *s) char **newx; newa = n + 10 + (n >> 3); - newp = (strset_list *) alloc(sizeof(strset_list) * newa); + newp = (strset_list *)alloc(sizeof(strset_list) * newa); if (!newp) return 0; - newx = (char **) alloc(sizeof(char *) * newa); - if (!newx) { alloc_free(newp); return 0; } + newx = (char **)alloc(sizeof(char *) * newa); + if (!newx) { + alloc_free(newp); + return 0; + } - byte_copy(newp,sizeof(strset_list) * n,set->p); - byte_copy(newx,sizeof(char *) * n,set->x); + byte_copy(newp, sizeof(strset_list) * n, set->p); + byte_copy(newx, sizeof(char *) * n, set->x); alloc_free(set->p); alloc_free(set->x); set->p = newp; @@ -90,13 +100,12 @@ int strset_add(strset *set,char *s) int *newfirst; int i; uint32 h; - + newmask = set->mask + set->mask + 1; - newfirst = (int *) alloc(sizeof(int) * (newmask + 1)); + newfirst = (int *)alloc(sizeof(int) * (newmask + 1)); if (!newfirst) return 0; - for (h = 0; h <= newmask; ++h) - newfirst[h] = -1; + for (h = 0; h <= newmask; ++h) newfirst[h] = -1; for (i = 0; i < n; ++i) { sl = set->p + i; diff --git a/src/tai64nfrac.c b/src/tai64nfrac.c index f3db977..bb29b44 100644 --- a/src/tai64nfrac.c +++ b/src/tai64nfrac.c @@ -1,11 +1,11 @@ #include "buffer.h" -#include "stralloc.h" #include "exit.h" -#include "readwrite.h" -#include "open.h" -#include "scan.h" #include "fmt.h" #include "getln.h" +#include "open.h" +#include "readwrite.h" +#include "scan.h" +#include "stralloc.h" #define TAI64NLEN 24 @@ -16,11 +16,11 @@ */ char outbuf[64]; -buffer bo = BUFFER_INIT(write,1,outbuf,sizeof(outbuf)); +buffer bo = BUFFER_INIT(write, 1, outbuf, sizeof(outbuf)); static void outs(char *s) { - if (buffer_puts(&bo,s) == -1) _exit(1); + if (buffer_puts(&bo, s) == -1) _exit(1); if (buffer_flush(&bo) == -1) _exit(1); } @@ -28,12 +28,12 @@ static void outi(int i) { char num[FMT_ULONG]; - if (buffer_put(&bo,num,fmt_ulong(num,(unsigned long) i)) == -1) _exit(1); + if (buffer_put(&bo, num, fmt_ulong(num, (unsigned long)i)) == -1) _exit(1); if (buffer_flush(&bo) == -1) _exit(1); } char inbuf[1024]; -buffer bi = BUFFER_INIT(read,0,inbuf,sizeof(inbuf)); +buffer bi = BUFFER_INIT(read, 0, inbuf, sizeof(inbuf)); int main(void) { @@ -41,23 +41,23 @@ int main(void) int i; int match; unsigned long u; - unsigned long seconds; + unsigned long seconds; unsigned long nanoseconds; stralloc line = {0}; -/* Read from stdin */ + /* Read from stdin */ - buffer_init(&bi,read,0,inbuf,sizeof(inbuf)); + buffer_init(&bi, read, 0, inbuf, sizeof(inbuf)); for (;;) { - if (getln(&bi,&line,&match,'\n') != 0) _exit(1); + if (getln(&bi, &line, &match, '\n') != 0) _exit(1); if (!match) break; if (!stralloc_0(&line)) _exit(1); seconds = 0; nanoseconds = 0; - - if (line.s[0] == '@') { /* tai64 timestamp */ + + if (line.s[0] == '@') { /* tai64 timestamp */ for (i = 1; i <= TAI64NLEN; i++) { c = (int)line.s[i]; u = c - '0'; @@ -74,9 +74,14 @@ int main(void) } seconds -= 4611686018427387914ULL; seconds = seconds > 0 ? seconds : 0; - outi(seconds); outs("."); outi(nanoseconds); outs(line.s + i); outs("\n"); + outi(seconds); + outs("."); + outi(nanoseconds); + outs(line.s + i); + outs("\n"); } else { - outs("tai64nfrac: fatal: Wrong TAI64N input format."); outs("\n"); + outs("tai64nfrac: fatal: Wrong TAI64N input format."); + outs("\n"); _exit(1); } } diff --git a/src/tcpto.c b/src/tcpto.c index 92c33ea..77e565c 100644 --- a/src/tcpto.c +++ b/src/tcpto.c @@ -1,21 +1,24 @@ +#include "tcpto.h" + #include <sys/socket.h> #include <unistd.h> -#include "tcpto.h" -#include "open.h" + +#include "byte.h" +#include "ip.h" #include "lock.h" +#include "open.h" #include "seek.h" -#include "now.h" -#include "ip.h" -#include "ipalloc.h" -#include "byte.h" + #include "datetime.h" +#include "ipalloc.h" +#include "now.h" char tcpto_buf[1024]; static int flagwasthere; static int fdlock; -static int getbuf() +static int getbuf() { int r; int fd; @@ -23,17 +26,27 @@ static int getbuf() fdlock = open_write("queue/lock/tcpto"); if (fdlock == -1) return 0; fd = open_read("queue/lock/tcpto"); - if (fd == -1) { close(fdlock); return 0; } - if (lock_ex(fdlock) == -1) { close(fdlock); close(fd); return 0; } - r = read(fd,tcpto_buf,sizeof(tcpto_buf)); + if (fd == -1) { + close(fdlock); + return 0; + } + if (lock_ex(fdlock) == -1) { + close(fdlock); + close(fd); + return 0; + } + r = read(fd, tcpto_buf, sizeof(tcpto_buf)); close(fd); - if (r < 0) { close(fdlock); return 0; } + if (r < 0) { + close(fdlock); + return 0; + } r >>= 5; if (!r) close(fdlock); return r; } -int tcpto(struct ip_mx *ix) +int tcpto(struct ip_mx *ix) { int af = ix->af; struct ip_address *ip = &ix->addr; @@ -51,13 +64,13 @@ int tcpto(struct ip_mx *ix) record = tcpto_buf; for (i = 0; i < n; ++i) { - if (af == record[0] && byte_equal(ip->d,af == AF_INET ? 4 : 16,record + 16)) { + if (af == record[0] && byte_equal(ip->d, af == AF_INET ? 4 : 16, record + 16)) { flagwasthere = 1; if (record[4] >= 2) { - when = (unsigned long) (unsigned char) record[11]; - when = (when << 8) + (unsigned long) (unsigned char) record[10]; - when = (when << 8) + (unsigned long) (unsigned char) record[9]; - when = (when << 8) + (unsigned long) (unsigned char) record[8]; + when = (unsigned long)(unsigned char)record[11]; + when = (when << 8) + (unsigned long)(unsigned char)record[10]; + when = (when << 8) + (unsigned long)(unsigned char)record[9]; + when = (when << 8) + (unsigned long)(unsigned char)record[8]; if (now() - when < ((60 + (getpid() & 31)) << 6)) return 1; } @@ -68,7 +81,7 @@ int tcpto(struct ip_mx *ix) return 0; } -void tcpto_err(struct ip_mx *ix,int flagerr) +void tcpto_err(struct ip_mx *ix, int flagerr) { int af = ix->af; struct ip_address *ip = &ix->addr; @@ -81,8 +94,7 @@ void tcpto_err(struct ip_mx *ix,int flagerr) datetime_sec lastwhen; if (!flagerr) - if (!flagwasthere) - return; /* could have been added, but not worth the effort to check */ + if (!flagwasthere) return; /* could have been added, but not worth the effort to check */ n = getbuf(); if (!n) return; @@ -90,26 +102,32 @@ void tcpto_err(struct ip_mx *ix,int flagerr) record = tcpto_buf; for (i = 0; i < n; ++i) { - if (af == record[0] && byte_equal(ip->d,af == AF_INET ? 4 : 16,record + 16)) { + if (af == record[0] && byte_equal(ip->d, af == AF_INET ? 4 : 16, record + 16)) { if (!flagerr) - record[4] = 0; + record[4] = 0; else { - lastwhen = (unsigned long) (unsigned char) record[11]; - lastwhen = (lastwhen << 8) + (unsigned long) (unsigned char) record[10]; - lastwhen = (lastwhen << 8) + (unsigned long) (unsigned char) record[9]; - lastwhen = (lastwhen << 8) + (unsigned long) (unsigned char) record[8]; + lastwhen = (unsigned long)(unsigned char)record[11]; + lastwhen = (lastwhen << 8) + (unsigned long)(unsigned char)record[10]; + lastwhen = (lastwhen << 8) + (unsigned long)(unsigned char)record[9]; + lastwhen = (lastwhen << 8) + (unsigned long)(unsigned char)record[8]; when = now(); - if (record[4] && (when < 120 + lastwhen)) { close(fdlock); return; } + if (record[4] && (when < 120 + lastwhen)) { + close(fdlock); + return; + } if (++record[4] > 10) record[4] = 10; - record[8] = when; when >>= 8; - record[9] = when; when >>= 8; - record[10] = when; when >>= 8; + record[8] = when; + when >>= 8; + record[9] = when; + when >>= 8; + record[10] = when; + when >>= 8; record[11] = when; } - if (seek_set(fdlock,i << 5) == 0) - if (write(fdlock,record,32) < 32) + if (seek_set(fdlock, i << 5) == 0) + if (write(fdlock, record, 32) < 32) ; /*XXX*/ close(fdlock); return; @@ -117,7 +135,10 @@ void tcpto_err(struct ip_mx *ix,int flagerr) record += 32; } - if (!flagerr) { close(fdlock); return; } + if (!flagerr) { + close(fdlock); + return; + } record = tcpto_buf; @@ -131,10 +152,10 @@ void tcpto_err(struct ip_mx *ix,int flagerr) record = tcpto_buf; for (i = 0; i < n; ++i) { - when = (unsigned long) (unsigned char) record[11]; - when = (when << 8) + (unsigned long) (unsigned char) record[10]; - when = (when << 8) + (unsigned long) (unsigned char) record[9]; - when = (when << 8) + (unsigned long) (unsigned char) record[8]; + when = (unsigned long)(unsigned char)record[11]; + when = (when << 8) + (unsigned long)(unsigned char)record[10]; + when = (when << 8) + (unsigned long)(unsigned char)record[9]; + when = (when << 8) + (unsigned long)(unsigned char)record[8]; when += (record[4] << 10); if ((firstpos < 0) || (when < firstwhen)) { firstpos = i; @@ -148,20 +169,23 @@ void tcpto_err(struct ip_mx *ix,int flagerr) if (i >= 0) { record = tcpto_buf + (i << 5); record[0] = af; - if (af == AF_INET6) - byte_copy(record + 16,16,ip->d); + if (af == AF_INET6) + byte_copy(record + 16, 16, ip->d); else { - byte_copy(record + 16,4,ip->d); - byte_copy(record + 20,12,"............"); + byte_copy(record + 16, 4, ip->d); + byte_copy(record + 20, 12, "............"); } when = now(); - record[8] = when; when >>= 8; - record[9] = when; when >>= 8; - record[10] = when; when >>= 8; + record[8] = when; + when >>= 8; + record[9] = when; + when >>= 8; + record[10] = when; + when >>= 8; record[11] = when; record[4] = 1; - if (seek_set(fdlock,i << 5) == 0) - if (write(fdlock,record,32) < 32) + if (seek_set(fdlock, i << 5) == 0) + if (write(fdlock, record, 32) < 32) ; /*XXX*/ } diff --git a/src/tcpto_clean.c b/src/tcpto_clean.c index e0b6969..92df9bd 100644 --- a/src/tcpto_clean.c +++ b/src/tcpto_clean.c @@ -1,7 +1,9 @@ #include <unistd.h> -#include "tcpto.h" -#include "open.h" + #include "buffer.h" +#include "open.h" + +#include "tcpto.h" char tcpto_cleanbuf[1024]; @@ -13,9 +15,8 @@ void tcpto_clean() /* running from queue/mess */ fd = open_write("../lock/tcpto"); if (fd == -1) return; - buffer_init(&bo,write,fd,tcpto_cleanbuf,sizeof(tcpto_cleanbuf)); - for (i = 0; i < sizeof(tcpto_cleanbuf); ++i) - buffer_put(&bo,"",1); + buffer_init(&bo, write, fd, tcpto_cleanbuf, sizeof(tcpto_cleanbuf)); + for (i = 0; i < sizeof(tcpto_cleanbuf); ++i) buffer_put(&bo, "", 1); buffer_flush(&bo); /* if it fails, bummer */ close(fd); } diff --git a/src/tls_errors.c b/src/tls_errors.c index 5c30236..3d24784 100644 --- a/src/tls_errors.c +++ b/src/tls_errors.c @@ -1,7 +1,9 @@ -#include <unistd.h> -#include "stralloc.h" #include "tls_errors.h" + +#include <unistd.h> + #include "error.h" +#include "stralloc.h" /** @file tls_errors.c @brief temp_tls* routines are used for error messges @@ -9,41 +11,41 @@ /* TLS error messages: A) Setup */ -void temp_tlscert() -{ - out("ZCan't load X.509 certificate: "); - outsafe(&certfile); - out(". (#4.4.1)\n"); - zerodie(); +void temp_tlscert() +{ + out("ZCan't load X.509 certificate: "); + outsafe(&certfile); + out(". (#4.4.1)\n"); + zerodie(); } -void temp_tlskey() -{ - out("ZCan't load X.509 private key: "); - outsafe(&keyfile); - out(". (#4.4.1)\n"); - zerodie(); +void temp_tlskey() +{ + out("ZCan't load X.509 private key: "); + outsafe(&keyfile); + out(". (#4.4.1)\n"); + zerodie(); } -void temp_tlschk() -{ - out("ZKeyfile does not match X.509 certificate: "); - outsafe(&keypwd); - out(". (#4.4.1)\n"); - zerodie(); +void temp_tlschk() +{ + out("ZKeyfile does not match X.509 certificate: "); + outsafe(&keypwd); + out(". (#4.4.1)\n"); + zerodie(); } -void temp_tlsca() -{ - out("ZI wasn't able to set up CAFILE: "); - outsafe(&cafile); +void temp_tlsca() +{ + out("ZI wasn't able to set up CAFILE: "); + outsafe(&cafile); out(" or CADIR: "); - outsafe(&cadir); - out(" for TLS. (#4.4.1)\n"); - zerodie(); + outsafe(&cadir); + out(" for TLS. (#4.4.1)\n"); + zerodie(); } -void temp_tlscipher() +void temp_tlscipher() { out("ZI wasn't able to process the TLS ciphers: "); outsafe(&ciphers); @@ -53,106 +55,128 @@ void temp_tlscipher() /* TLS error messages: B) Connection related */ -void temp_tlsctx() -{ +void temp_tlsctx() +{ out("ZI wasn't able to create TLS context for: "); - outsafe(&host); out(" at "); out(remotehost.s); - out(". (#4.4.1)\n"); + outsafe(&host); + out(" at "); + out(remotehost.s); + out(". (#4.4.1)\n"); zerodie(); } -void temp_tlscon() -{ +void temp_tlscon() +{ errno = EPROTO; - out("ZI wasn't able to establish a TLS connection with: "); - out(remotehost.s); out(" for "); outsafe(&host); - out(". (#4.4.1)\n"); - zerodie(); + out("ZI wasn't able to establish a TLS connection with: "); + out(remotehost.s); + out(" for "); + outsafe(&host); + out(". (#4.4.1)\n"); + zerodie(); } -void temp_tlserr() +void temp_tlserr() { errno = EPROTO; out("ZTLS connection/protocol error with: "); - out(remotehost.s); out(" for "); outsafe(&host); + out(remotehost.s); + out(" for "); + outsafe(&host); out(". (#4.4.1)\n"); zerodie(); } -void temp_tlshost() -{ - out("ZI wasn't able to negotiate a StartTLS connection with: "); - out(remotehost.s); out(" for "); outsafe(&host); - out(". (#4.4.1)\n"); - zerodie(); +void temp_tlshost() +{ + out("ZI wasn't able to negotiate a StartTLS connection with: "); + out(remotehost.s); + out(" for "); + outsafe(&host); + out(". (#4.4.1)\n"); + zerodie(); } /* TLS error messages: C) Verification related */ -void temp_tlspeercert() -{ - out("ZUnable to obtain X.509 certificate from: "); - out(remotehost.s); out(" for "); outsafe(&host); - out(". (#4.4.1)\n"); - zerodie(); +void temp_tlspeercert() +{ + out("ZUnable to obtain X.509 certificate from: "); + out(remotehost.s); + out(" for "); + outsafe(&host); + out(". (#4.4.1)\n"); + zerodie(); } -void temp_tlspeerverify() -{ - out("ZUnable to verify X.509 certificate from: "); - out(remotehost.s); out(" for "); outsafe(&host); - out(". (#4.4.1)\n"); - zerodie(); +void temp_tlspeerverify() +{ + out("ZUnable to verify X.509 certificate from: "); + out(remotehost.s); + out(" for "); + outsafe(&host); + out(". (#4.4.1)\n"); + zerodie(); } -void temp_tlspeervalid() -{ - out("ZUnable to validate X.509 certificate Subject for: "); - outsafe(&host); out(" at "); out(remotehost.s); - out(". (#4.4.1)\n"); - zerodie(); +void temp_tlspeervalid() +{ + out("ZUnable to validate X.509 certificate Subject for: "); + outsafe(&host); + out(" at "); + out(remotehost.s); + out(". (#4.4.1)\n"); + zerodie(); } -void temp_tlscertfp() -{ +void temp_tlscertfp() +{ out("ZReceived X.509 certificate from: "); - out(remotehost.s); out(" for "); outsafe(&host); - out(" does not match fingerprint: "); - outsafe(&cafile); - out(". (#4.4.1)\n"); - zerodie(); + out(remotehost.s); + out(" for "); + outsafe(&host); + out(" does not match fingerprint: "); + outsafe(&cafile); + out(". (#4.4.1)\n"); + zerodie(); } -void temp_invaliddigest() -{ +void temp_invaliddigest() +{ out("ZInvalid digest length provided given for: "); - out(remotehost.s); out(" for "); outsafe(&host); - out(". (#4.4.1)\n"); - zerodie(); + out(remotehost.s); + out(" for "); + outsafe(&host); + out(". (#4.4.1)\n"); + zerodie(); } -void temp_tlsamissing() -{ +void temp_tlsamissing() +{ out("ZTLSA X.509 cert required but missing from: "); - out(remotehost.s); out(" for "); outsafe(&host); - out(". (#4.4.1)\n"); - zerodie(); + out(remotehost.s); + out(" for "); + outsafe(&host); + out(". (#4.4.1)\n"); + zerodie(); } -void temp_tlsainvalid() -{ +void temp_tlsainvalid() +{ out("ZTLSA fingerprint matching error for: "); - out(remotehost.s); - out(". (#4.4.1)\n"); - zerodie(); + out(remotehost.s); + out(". (#4.4.1)\n"); + zerodie(); } -void temp_tlsdigest() -{ +void temp_tlsdigest() +{ out("ZReceived X.509 certificate from: "); - out(remotehost.s); out(" for "); outsafe(&host); - out(" posses an unknown digest method"); - out(". (#4.4.1)\n"); - zerodie(); + out(remotehost.s); + out(" for "); + outsafe(&host); + out(" posses an unknown digest method"); + out(". (#4.4.1)\n"); + zerodie(); } diff --git a/src/tls_remote.c b/src/tls_remote.c index 1318b4e..7986077 100644 --- a/src/tls_remote.c +++ b/src/tls_remote.c @@ -1,14 +1,17 @@ +#include "tls_remote.h" + #include <unistd.h> -#include "ucspissl.h" -#include "fmt.h" -#include "stralloc.h" -#include "str.h" + #include "byte.h" #include "case.h" -#include "dns.h" #include "constmap.h" -#include "tls_remote.h" +#include "fmt.h" +#include "str.h" +#include "stralloc.h" + +#include "dns.h" #include "tls_errors.h" +#include "ucspissl.h" /** @file tls_remote.c -- TLS client functions @brief connection functions: tls_conn, tls_exit; @@ -26,43 +29,40 @@ #define X509_cert_digest X509_digest -int tls_certkey(SSL_CTX *ctx,const char *cert,const char *key,char *ppwd) +int tls_certkey(SSL_CTX *ctx, const char *cert, const char *key, char *ppwd) { if (!cert) return 0; - if (SSL_CTX_use_certificate_chain_file(ctx,cert) != 1) - return -1; + if (SSL_CTX_use_certificate_chain_file(ctx, cert) != 1) return -1; if (!key) key = cert; - if (ppwd) SSL_CTX_set_default_passwd_cb_userdata(ctx,ppwd); + if (ppwd) SSL_CTX_set_default_passwd_cb_userdata(ctx, ppwd); - if (SSL_CTX_use_PrivateKey_file(ctx,key,SSL_FILETYPE_PEM) != 1) - return -2; + if (SSL_CTX_use_PrivateKey_file(ctx, key, SSL_FILETYPE_PEM) != 1) return -2; - if (SSL_CTX_check_private_key(ctx) != 1) - return -3; + if (SSL_CTX_check_private_key(ctx) != 1) return -3; return 0; } -int tls_conn(SSL *ssl,int smtpfd) +int tls_conn(SSL *ssl, int smtpfd) { - SSL_set_options(ssl,SSL_OP_NO_SSLv2); - SSL_set_options(ssl,SSL_OP_NO_SSLv3); - return SSL_set_fd(ssl,smtpfd); + SSL_set_options(ssl, SSL_OP_NO_SSLv2); + SSL_set_options(ssl, SSL_OP_NO_SSLv3); + return SSL_set_fd(ssl, smtpfd); } -int tls_checkpeer(SSL *ssl,X509 *cert,const stralloc host,const int flag,const int verify) +int tls_checkpeer(SSL *ssl, X509 *cert, const stralloc host, const int flag, const int verify) { - STACK_OF(GENERAL_NAME) *extensions; + STACK_OF(GENERAL_NAME) * extensions; const GENERAL_NAME *ext; char buf[SSL_NAME_LEN]; char *dnsname = 0; int dname = 0; int num; int len; - int fflag; + int fflag; int i; int rc = 0; @@ -73,58 +73,64 @@ int tls_checkpeer(SSL *ssl,X509 *cert,const stralloc host,const int flag,const i /* X.509 CA DN/SAN name validation against DNS */ if (host.len && fflag > 4) { - extensions = (GENERAL_NAME *)X509_get_ext_d2i(cert,NID_subject_alt_name,0,0); - num = sk_GENERAL_NAME_num(extensions); /* num = 0, if no SAN extensions */ + extensions = (GENERAL_NAME *)X509_get_ext_d2i(cert, NID_subject_alt_name, 0, 0); + num = sk_GENERAL_NAME_num(extensions); /* num = 0, if no SAN extensions */ for (i = 0; i < num; ++i) { - ext = sk_GENERAL_NAME_value(extensions,i); + ext = sk_GENERAL_NAME_value(extensions, i); if (ext->type == GEN_DNS) { - #if (OPENSSL_VERSION_NUMBER < 0x10100000L) // 0xmnnffppsL +#if (OPENSSL_VERSION_NUMBER < 0x10100000L) // 0xmnnffppsL if (ASN1_STRING_type(ext->d.ia5) != V_ASN1_IA5STRING) continue; dnsname = (char *)ASN1_STRING_data(ext->d.ia5); - #else - if (OBJ_sn2nid((const char*)ext->d.ia5) != V_ASN1_IA5STRING) continue; +#else + if (OBJ_sn2nid((const char *)ext->d.ia5) != V_ASN1_IA5STRING) continue; dnsname = (char *)ASN1_STRING_get0_data(ext->d.ia5); - #endif +#endif len = ASN1_STRING_length(ext->d.ia5); dname = 1; } } if (!dname) { - X509_NAME_get_text_by_NID(X509_get_subject_name(cert),NID_commonName,buf,sizeof(buf)); + X509_NAME_get_text_by_NID(X509_get_subject_name(cert), NID_commonName, buf, sizeof(buf)); buf[SSL_NAME_LEN - 1] = 0; dnsname = buf; len = SSL_NAME_LEN - 1; } switch (fflag) { - case 5: if (dnsname[0] == '*' && dnsname[1] == '.') - if (case_diffrs(dnsname + 1,host.s)) return -3; - if (case_diffrs(dnsname,host.s)) return -3; - rc = 3; break; - case 6: if (case_diffs(dnsname,host.s)) return -3; - rc = 2; break; + case 5: + if (dnsname[0] == '*' && dnsname[1] == '.') + if (case_diffrs(dnsname + 1, host.s)) return -3; + if (case_diffrs(dnsname, host.s)) return -3; + rc = 3; + break; + case 6: + if (case_diffs(dnsname, host.s)) return -3; + rc = 2; + break; } } /* X.509 CA Verification: root CA must be available */ - if (fflag > 3 && verify > -2) { - if (SSL_get_verify_result(ssl) != X509_V_OK) return -2; - else rc = 1; + if (fflag > 3 && verify > -2) { + if (SSL_get_verify_result(ssl) != X509_V_OK) + return -2; + else + rc = 1; } return rc; } -int tls_checkcrl(SSL *ssl) // not implemented yet +int tls_checkcrl(SSL *ssl) // not implemented yet { return 0; } -int dig_ascii(char *digascii,const char *digest,const int len) +int dig_ascii(char *digascii, const char *digest, const int len) { static const char hextab[] = "0123456789abcdef"; int j; @@ -143,25 +149,25 @@ int dig_ascii(char *digascii,const char *digest,const int len) Subjects keys are restricted to 2048 byte in size. Return codes: 1: sucess, 0: failed. */ -int X509_pkey_digest(const X509 *cert,const EVP_MD *type,unsigned char *md,unsigned int *dlen) +int X509_pkey_digest(const X509 *cert, const EVP_MD *type, unsigned char *md, unsigned int *dlen) { unsigned int len = 0; unsigned int size = 2048; unsigned char *buf; unsigned char *buf2; - unsigned char buffer[size]; // avoid malloc + unsigned char buffer[size]; // avoid malloc -/* Following Viktor's suggestion */ + /* Following Viktor's suggestion */ if (!X509_get0_pubkey_bitstr(cert)) return 0; // no Subject public key - len = i2d_X509_PUBKEY(X509_get_X509_PUBKEY(cert),0); + len = i2d_X509_PUBKEY(X509_get_X509_PUBKEY(cert), 0); if (len > size) return 0; buf2 = buf = buffer; - i2d_X509_PUBKEY(X509_get_X509_PUBKEY(cert),(unsigned char **)&buf2); + i2d_X509_PUBKEY(X509_get_X509_PUBKEY(cert), (unsigned char **)&buf2); if (buf2 - buf != len) return 0; - if (!EVP_Digest(buf,len,md,dlen,type,0)) return 0; // OpenSSL voodoo + if (!EVP_Digest(buf, len, md, dlen, type, 0)) return 0; // OpenSSL voodoo return 1; } @@ -169,7 +175,7 @@ int X509_pkey_digest(const X509 *cert,const EVP_MD *type,unsigned char *md,unsig -2: unsupported type, -1: weird TLSA record 0: No X.509 cert; seen: usage++; */ -int tlsa_check(const STACK_OF(X509) *certs,const stralloc host,const unsigned long p) +int tlsa_check(const STACK_OF(X509) * certs, const stralloc host, const unsigned long p) { const EVP_MD *methodsha256 = EVP_sha256(); const EVP_MD *methodsha512 = EVP_sha512(); @@ -180,25 +186,27 @@ int tlsa_check(const STACK_OF(X509) *certs,const stralloc host,const unsigned lo unsigned int dlen = 0; unsigned int n = 0; int i = 0; - int r; + int r; char port[FMT_ULONG]; uint16 type; uint16 selector; uint16 usage; -// construct TLSA FQDN -- simple procedure; returning Usage + // construct TLSA FQDN -- simple procedure; returning Usage if (host.len < 2) return 0; - if (!stralloc_copyb(&sa,"_",1)) temp_nomem(); - port[fmt_ulong(port,p)] = 0; - if (!stralloc_cats(&sa,port)) temp_nomem(); - if (!stralloc_cats(&sa,"._tcp.")) temp_nomem(); - if (!stralloc_cats(&sa,host.s)) temp_nomem(); - - if (dns_cname(&cn,&sa) > 0) // query name could be a cname - { if (dns_tlsa(&out,&cn) <= 0) return 0; } - else - { if (dns_tlsa(&out,&sa) <= 0) return 0; } + if (!stralloc_copyb(&sa, "_", 1)) temp_nomem(); + port[fmt_ulong(port, p)] = 0; + if (!stralloc_cats(&sa, port)) temp_nomem(); + if (!stralloc_cats(&sa, "._tcp.")) temp_nomem(); + if (!stralloc_cats(&sa, host.s)) temp_nomem(); + + if (dns_cname(&cn, &sa) > 0) // query name could be a cname + { + if (dns_tlsa(&out, &cn) <= 0) return 0; + } else { + if (dns_tlsa(&out, &sa) <= 0) return 0; + } if (out.len < 5) return -1; /* https://www.openssl.org/docs/man3.0/man3/X509_digest.html (1.1.1): @@ -207,23 +215,23 @@ int tlsa_check(const STACK_OF(X509) *certs,const stralloc host,const unsigned lo */ do { - usage = (unsigned char) out.s[i]; // Usage: PKIX-TA [0], PKIX-EE [1], DANE-TA [2], DANE-EE [3] - selector = (unsigned char) out.s[i + 1]; // Selector: 0 = Cert, 1 = SPKI - type = (unsigned char) out.s[i + 2]; // Type: 0/1/2 = [Cert|SPKI]/SHA256/SHA512 + usage = (unsigned char)out.s[i]; // Usage: PKIX-TA [0], PKIX-EE [1], DANE-TA [2], DANE-EE [3] + selector = (unsigned char)out.s[i + 1]; // Selector: 0 = Cert, 1 = SPKI + type = (unsigned char)out.s[i + 2]; // Type: 0/1/2 = [Cert|SPKI]/SHA256/SHA512 unsigned len = sk_X509_num(certs); - for (n = 0; n < len; n++) { - X509 *cert = sk_X509_value(certs,n); + for (n = 0; n < len; n++) { + X509 *cert = sk_X509_value(certs, n); if (type == 1) { - if (selector == 0) r = X509_cert_digest(cert,methodsha256,digest,&dlen); - if (selector == 1) r = X509_pkey_digest(cert,methodsha256,digest,&dlen); + if (selector == 0) r = X509_cert_digest(cert, methodsha256, digest, &dlen); + if (selector == 1) r = X509_pkey_digest(cert, methodsha256, digest, &dlen); } else if (type == 2) { - if (selector == 0) r = X509_cert_digest(cert,methodsha512,digest,&dlen); - if (selector == 1) r = X509_pkey_digest(cert,methodsha512,digest,&dlen); - } else + if (selector == 0) r = X509_cert_digest(cert, methodsha512, digest, &dlen); + if (selector == 1) r = X509_pkey_digest(cert, methodsha512, digest, &dlen); + } else return -2; - if (!byte_diff(digest,dlen,out.s + i + 3)) return ++usage; + if (!byte_diff(digest, dlen, out.s + i + 3)) return ++usage; } i += (dlen + 3); @@ -232,7 +240,7 @@ int tlsa_check(const STACK_OF(X509) *certs,const stralloc host,const unsigned lo return -3; } -int tls_fingerprint(X509 *cert,const char *fingerprint,int dlen) +int tls_fingerprint(X509 *cert, const char *fingerprint, int dlen) { const EVP_MD *methodsha1 = EVP_sha1(); const EVP_MD *methodsha224 = EVP_sha224(); @@ -242,24 +250,27 @@ int tls_fingerprint(X509 *cert,const char *fingerprint,int dlen) unsigned char digascii[257]; unsigned int len; - switch (dlen) { /* fetch digest from cert; len = bitlength/8 */ - case 40: if (!X509_digest(cert,methodsha1,digest,&len)) return -2; - case 56: if (!X509_digest(cert,methodsha224,digest,&len)) return -2; - case 64: if (!X509_digest(cert,methodsha256,digest,&len)) return -2; - case 128: if (!X509_digest(cert,methodsha512,digest,&len)) return -2; - default: return -3; + switch (dlen) { /* fetch digest from cert; len = bitlength/8 */ + case 40: + if (!X509_digest(cert, methodsha1, digest, &len)) return -2; + case 56: + if (!X509_digest(cert, methodsha224, digest, &len)) return -2; + case 64: + if (!X509_digest(cert, methodsha256, digest, &len)) return -2; + case 128: + if (!X509_digest(cert, methodsha512, digest, &len)) return -2; + default: return -3; } - len = dig_ascii(digascii,digest,len); - if (!str_diffn(digascii,fingerprint,len)) return 1; + len = dig_ascii(digascii, digest, len); + if (!str_diffn(digascii, fingerprint, len)) return 1; return 0; } int tls_exit(SSL *ssl) { - if (SSL_shutdown(ssl) == 0) - SSL_shutdown(ssl); + if (SSL_shutdown(ssl) == 0) SSL_shutdown(ssl); return 0; } @@ -283,86 +294,86 @@ int tls_destination(const stralloc hostname) stralloc tlshost = {0}; stralloc tlsdest = {0}; - if (!stralloc_copy(&tlshost,&hostname)) temp_nomem(); + if (!stralloc_copy(&tlshost, &hostname)) temp_nomem(); if (!stralloc_0(&tlshost)) temp_nomem(); -// Host rules + // Host rules + + if (!stralloc_copys(&tlsdest, "!")) temp_nomem(); + if (!stralloc_cats(&tlsdest, tlshost.s)) temp_nomem(); + if ((tlsdestinfo = constmap(&maptlsdestinations, tlsdest.s, tlsdest.len))) return -1; - if (!stralloc_copys(&tlsdest,"!")) temp_nomem(); - if (!stralloc_cats(&tlsdest,tlshost.s)) temp_nomem(); - if ((tlsdestinfo = constmap(&maptlsdestinations,tlsdest.s,tlsdest.len))) return -1; + if (!stralloc_copys(&tlsdest, "?")) temp_nomem(); + if (!stralloc_cats(&tlsdest, tlshost.s)) temp_nomem(); + if ((tlsdestinfo = constmap(&maptlsdestinations, tlsdest.s, tlsdest.len))) return 9; - if (!stralloc_copys(&tlsdest,"?")) temp_nomem(); - if (!stralloc_cats(&tlsdest,tlshost.s)) temp_nomem(); - if ((tlsdestinfo = constmap(&maptlsdestinations,tlsdest.s,tlsdest.len))) return 9; + if (!stralloc_copys(&tlsdest, "/")) temp_nomem(); + if (!stralloc_cats(&tlsdest, tlshost.s)) temp_nomem(); + if ((tlsdestinfo = constmap(&maptlsdestinations, tlsdest.s, tlsdest.len))) return 8; - if (!stralloc_copys(&tlsdest,"/")) temp_nomem(); - if (!stralloc_cats(&tlsdest,tlshost.s)) temp_nomem(); - if ((tlsdestinfo = constmap(&maptlsdestinations,tlsdest.s,tlsdest.len))) return 8; + if (!stralloc_copys(&tlsdest, "%")) temp_nomem(); // CERT + hash + if (!stralloc_cats(&tlsdest, tlshost.s)) temp_nomem(); + if ((tlsdestinfo = constmap(&maptlsdestinations, tlsdest.s, tlsdest.len))) return 7; - if (!stralloc_copys(&tlsdest,"%")) temp_nomem(); // CERT + hash - if (!stralloc_cats(&tlsdest,tlshost.s)) temp_nomem(); - if ((tlsdestinfo = constmap(&maptlsdestinations,tlsdest.s,tlsdest.len))) return 7; + if (!stralloc_copys(&tlsdest, "=")) temp_nomem(); // CERT + FQDN + if (!stralloc_cats(&tlsdest, tlshost.s)) temp_nomem(); + if ((tlsdestinfo = constmap(&maptlsdestinations, tlsdest.s, tlsdest.len))) return 6; - if (!stralloc_copys(&tlsdest,"=")) temp_nomem(); // CERT + FQDN - if (!stralloc_cats(&tlsdest,tlshost.s)) temp_nomem(); - if ((tlsdestinfo = constmap(&maptlsdestinations,tlsdest.s,tlsdest.len))) return 6; - - if (!stralloc_copys(&tlsdest,"~")) temp_nomem(); // CERT + Wild - if (!stralloc_cats(&tlsdest,tlshost.s)) temp_nomem(); - if ((tlsdestinfo = constmap(&maptlsdestinations,tlsdest.s,tlsdest.len))) return 5; - -// Domain rules + if (!stralloc_copys(&tlsdest, "~")) temp_nomem(); // CERT + Wild + if (!stralloc_cats(&tlsdest, tlshost.s)) temp_nomem(); + if ((tlsdestinfo = constmap(&maptlsdestinations, tlsdest.s, tlsdest.len))) return 5; + + // Domain rules for (i = 0; i < tlshost.len; ++i) // TLS fallthru if ((i == 0) || (tlshost.s[i] == '.')) { - if (!stralloc_copys(&tlsdest,"?")) temp_nomem(); - if (!stralloc_cats(&tlsdest,tlshost.s + i)) temp_nomem(); - if ((tlsdestinfo = constmap(&maptlsdestinations,tlsdest.s,tlsdest.len))) return 9; + if (!stralloc_copys(&tlsdest, "?")) temp_nomem(); + if (!stralloc_cats(&tlsdest, tlshost.s + i)) temp_nomem(); + if ((tlsdestinfo = constmap(&maptlsdestinations, tlsdest.s, tlsdest.len))) return 9; } for (i = 0; i < tlshost.len; ++i) // no TLSA if ((i == 0) || (tlshost.s[i] == '.')) { - if (!stralloc_copys(&tlsdest,"/")) temp_nomem(); - if (!stralloc_cats(&tlsdest,tlshost.s + i)) temp_nomem(); - if ((tlsdestinfo = constmap(&maptlsdestinations,tlsdest.s,tlsdest.len))) return 8; + if (!stralloc_copys(&tlsdest, "/")) temp_nomem(); + if (!stralloc_cats(&tlsdest, tlshost.s + i)) temp_nomem(); + if ((tlsdestinfo = constmap(&maptlsdestinations, tlsdest.s, tlsdest.len))) return 8; } for (i = 0; i < tlshost.len; ++i) // CERT + Wild if ((i == 0) || (tlshost.s[i] == '.')) { - if (!stralloc_copys(&tlsdest,"~")) temp_nomem(); - if (!stralloc_cats(&tlsdest,tlshost.s + i)) temp_nomem(); - if ((tlsdestinfo = constmap(&maptlsdestinations,tlsdest.s,tlsdest.len))) return 5; + if (!stralloc_copys(&tlsdest, "~")) temp_nomem(); + if (!stralloc_cats(&tlsdest, tlshost.s + i)) temp_nomem(); + if ((tlsdestinfo = constmap(&maptlsdestinations, tlsdest.s, tlsdest.len))) return 5; } - for (i = 0; i < tlshost.len; ++i) // CERT - generic + for (i = 0; i < tlshost.len; ++i) // CERT - generic if ((i == 0) || (tlshost.s[i] == '.')) { - if (!stralloc_copys(&tlsdest,"")) temp_nomem(); - if (!stralloc_cats(&tlsdest,tlshost.s + i)) temp_nomem(); - if ((tlsdestinfo = constmap(&maptlsdestinations,tlsdest.s,tlsdest.len))) return 4; + if (!stralloc_copys(&tlsdest, "")) temp_nomem(); + if (!stralloc_cats(&tlsdest, tlshost.s + i)) temp_nomem(); + if ((tlsdestinfo = constmap(&maptlsdestinations, tlsdest.s, tlsdest.len))) return 4; } for (i = 0; i < tlshost.len; ++i) // ADH per host/domain if ((i == 0) || (tlshost.s[i] == '.')) { - if (!stralloc_copys(&tlsdest,"-")) temp_nomem(); - if (!stralloc_cats(&tlsdest,tlshost.s + i)) temp_nomem(); - if ((tlsdestinfo = constmap(&maptlsdestinations,tlsdest.s,tlsdest.len))) return 2; + if (!stralloc_copys(&tlsdest, "-")) temp_nomem(); + if (!stralloc_cats(&tlsdest, tlshost.s + i)) temp_nomem(); + if ((tlsdestinfo = constmap(&maptlsdestinations, tlsdest.s, tlsdest.len))) return 2; } -// General rules (mandatory TLS) + // General rules (mandatory TLS) tlsdestinfo = 0; - if (constmap(&maptlsdestinations,"/*",2)) return 8; // no TLSA - if (constmap(&maptlsdestinations,"=*",2)) return 6; // CERT + FQDN - if (constmap(&maptlsdestinations,"~*",2)) return 5; // CERT + Wild - if (constmap(&maptlsdestinations,"+*",2)) return 4; // CERT - if (constmap(&maptlsdestinations,"-*",2)) return 2; // ADH + if (constmap(&maptlsdestinations, "/*", 2)) return 8; // no TLSA + if (constmap(&maptlsdestinations, "=*", 2)) return 6; // CERT + FQDN + if (constmap(&maptlsdestinations, "~*", 2)) return 5; // CERT + Wild + if (constmap(&maptlsdestinations, "+*", 2)) return 4; // CERT + if (constmap(&maptlsdestinations, "-*", 2)) return 2; // ADH -// Fall thru rules (optional TLS) + // Fall thru rules (optional TLS) - if (constmap(&maptlsdestinations,"?",1)) return 9; // fallback to no TLS - if (constmap(&maptlsdestinations,"*",1)) return 3; // CERT - if (constmap(&maptlsdestinations,"-",1)) return 1; // ADH + if (constmap(&maptlsdestinations, "?", 1)) return 9; // fallback to no TLS + if (constmap(&maptlsdestinations, "*", 1)) return 3; // CERT + if (constmap(&maptlsdestinations, "-", 1)) return 1; // ADH return 0; } @@ -370,18 +381,18 @@ int tls_destination(const stralloc hostname) int tls_domaincerts(const stralloc domainname) { int i; - tlsdomaininfo = 0; // extern + tlsdomaininfo = 0; // extern -/* Our Certs - per domain */ + /* Our Certs - per domain */ if (domainname.len) for (i = 0; i < domainname.len; ++i) if ((i == 0) || (domainname.s[i] == '.')) - if ((tlsdomaininfo = constmap(&mapdomaincerts,domainname.s + i,domainname.len - i))) return 2; + if ((tlsdomaininfo = constmap(&mapdomaincerts, domainname.s + i, domainname.len - i))) return 2; + + /* Standard Cert (if any) */ -/* Standard Cert (if any) */ - - if ((tlsdomaininfo = constmap(&mapdomaincerts,"*",1))) return 1; + if ((tlsdomaininfo = constmap(&mapdomaincerts, "*", 1))) return 1; return 0; } diff --git a/src/tls_start.c b/src/tls_start.c index 77c5155..12c8e75 100644 --- a/src/tls_start.c +++ b/src/tls_start.c @@ -1,35 +1,42 @@ #include <unistd.h> -#include "scan.h" + #include "env.h" -#include "open.h" -#include "stralloc.h" #include "fd.h" #include "logmsg.h" +#include "open.h" +#include "scan.h" +#include "stralloc.h" #define WHO "tls_start" #define BUFSIZE 8192 -static void die_nomem() { logmsg(WHO,111,FATAL,"out of memory"); } -static void die_tlsenv() { logmsg(WHO,111,FATAL,"no UCSPITLS environment to read"); } +static void die_nomem() +{ + logmsg(WHO, 111, FATAL, "out of memory"); +} +static void die_tlsenv() +{ + logmsg(WHO, 111, FATAL, "no UCSPITLS environment to read"); +} int starttls_init(void) { unsigned long fd; char *fdstr; - + if (!(fdstr = env_get("SSLCTLFD"))) return 0; - if (!scan_ulong(fdstr,&fd)) return 0; - if (write((int)fd,"Y",1) < 1) return 0; - + if (!scan_ulong(fdstr, &fd)) return 0; + if (write((int)fd, "Y", 1) < 1) return 0; + if (!(fdstr = env_get("SSLREADFD"))) return 0; - if (!scan_ulong(fdstr,&fd)) return 0; - if (fd_move(0,(int)fd) == -1) return 0; - - if (!(fdstr = env_get("SSLWRITEFD"))) return 0; - if (!scan_ulong(fdstr,&fd)) return 0; - if (fd_move(1,(int)fd) == -1) return 0; - + if (!scan_ulong(fdstr, &fd)) return 0; + if (fd_move(0, (int)fd) == -1) return 0; + + if (!(fdstr = env_get("SSLWRITEFD"))) return 0; + if (!scan_ulong(fdstr, &fd)) return 0; + if (fd_move(1, (int)fd) == -1) return 0; + return 1; } @@ -41,41 +48,43 @@ int starttls_info(void) char *x; int j; - stralloc ssl_env = {0}; - stralloc ssl_parm = {0}; + stralloc ssl_env = {0}; + stralloc ssl_parm = {0}; stralloc ssl_value = {0}; if (!(fdstr = env_get("SSLCTLFD"))) return 0; - if (!scan_ulong(fdstr,&fd)) return 0; + if (!scan_ulong(fdstr, &fd)) return 0; - while ((j = read(fd,envbuf,BUFSIZE)) > 0 ) { - if (!stralloc_catb(&ssl_env,envbuf,j)) die_nomem(); - if (ssl_env.len >= 2 && ssl_env.s[ssl_env.len - 2] == 0 && ssl_env.s[ssl_env.len - 1] == 0) - break; + while ((j = read(fd, envbuf, BUFSIZE)) > 0) { + if (!stralloc_catb(&ssl_env, envbuf, j)) die_nomem(); + if (ssl_env.len >= 2 && ssl_env.s[ssl_env.len - 2] == 0 && ssl_env.s[ssl_env.len - 1] == 0) break; } - if (j <= 0) { die_tlsenv(); return 0; } // nothing to read + if (j <= 0) { + die_tlsenv(); + return 0; + } // nothing to read - x = ssl_env.s; + x = ssl_env.s; for (j = 0; j < ssl_env.len - 1; ++j) { - if ( *x != '=' ) { - if (!stralloc_catb(&ssl_parm,x,1)) die_nomem(); - x++; + if (*x != '=') { + if (!stralloc_catb(&ssl_parm, x, 1)) die_nomem(); + x++; } else { if (!stralloc_0(&ssl_parm)) die_nomem(); x++; for (; j < ssl_env.len - j - 1; ++j) { - if ( *x != '\0' ) { - if (!stralloc_catb(&ssl_value,x,1)) die_nomem(); - x++; + if (*x != '\0') { + if (!stralloc_catb(&ssl_value, x, 1)) die_nomem(); + x++; } else { if (!stralloc_0(&ssl_value)) die_nomem(); x++; - if (!env_put(ssl_parm.s,ssl_value.s)) die_nomem(); - ssl_parm.len = 0; + if (!env_put(ssl_parm.s, ssl_value.s)) die_nomem(); + ssl_parm.len = 0; ssl_value.len = 0; - break; + break; } } } diff --git a/src/tls_timeoutio.c b/src/tls_timeoutio.c index c5f40a2..3a2936b 100644 --- a/src/tls_timeoutio.c +++ b/src/tls_timeoutio.c @@ -1,14 +1,15 @@ /* This is essentially taken from Eric Vermeulen's TLS patch */ -#include "select.h" +#include "tls_timeoutio.h" + #include "error.h" +#include "logmsg.h" #include "ndelay.h" +#include "select.h" + #include "now.h" -#include "logmsg.h" #include "ucspissl.h" -#include "tls_timeoutio.h" -int tls_timeoutio(int (*fun)(), - int t, int rfd, int wfd, SSL *ssl, char *buf, int len) +int tls_timeoutio(int (*fun)(), int t, int rfd, int wfd, SSL *ssl, char *buf, int len) { int n; const datetime_sec end = (datetime_sec)t + now(); @@ -17,22 +18,25 @@ int tls_timeoutio(int (*fun)(), fd_set fds; struct timeval tv; - const int r = buf ? fun(ssl,buf,len) : fun(ssl); + const int r = buf ? fun(ssl, buf, len) : fun(ssl); if (r > 0) return r; t = end - now(); if (t < 0) break; - tv.tv_sec = (time_t)t; tv.tv_usec = 0; + tv.tv_sec = (time_t)t; + tv.tv_usec = 0; FD_ZERO(&fds); - switch (SSL_get_error(ssl,r)) { + switch (SSL_get_error(ssl, r)) { default: return r; /* some other error */ - case SSL_ERROR_WANT_READ: - FD_SET(rfd,&fds); n = select(rfd + 1,&fds,NULL,NULL,&tv); - break; - case SSL_ERROR_WANT_WRITE: - FD_SET(wfd,&fds); n = select(wfd + 1,NULL,&fds,NULL,&tv); - break; + case SSL_ERROR_WANT_READ: + FD_SET(rfd, &fds); + n = select(rfd + 1, &fds, NULL, NULL, &tv); + break; + case SSL_ERROR_WANT_WRITE: + FD_SET(wfd, &fds); + n = select(wfd + 1, NULL, &fds, NULL, &tv); + break; } /* n is the number of descriptors that changed status */ @@ -42,58 +46,64 @@ int tls_timeoutio(int (*fun)(), return -1; } -int tls_timeoutaccept(int t,int rfd,int wfd,SSL *ssl) +int tls_timeoutaccept(int t, int rfd, int wfd, SSL *ssl) { int r; /* if connection is established, keep NDELAY */ if (ndelay_on(rfd) == -1 || ndelay_on(wfd) == -1) return -1; - r = tls_timeoutio(SSL_accept,t,rfd,wfd,ssl,NULL,0); + r = tls_timeoutio(SSL_accept, t, rfd, wfd, ssl, NULL, 0); - if (r <= 0) { ndelay_off(rfd); ndelay_off(wfd); } - else SSL_set_mode(ssl,SSL_MODE_ENABLE_PARTIAL_WRITE); + if (r <= 0) { + ndelay_off(rfd); + ndelay_off(wfd); + } else + SSL_set_mode(ssl, SSL_MODE_ENABLE_PARTIAL_WRITE); return r; } -int tls_timeoutconn(int t,int rfd,int wfd,SSL *ssl) +int tls_timeoutconn(int t, int rfd, int wfd, SSL *ssl) { int r; /* if connection is established, keep NDELAY */ if (ndelay_on(rfd) == -1 || ndelay_on(wfd) == -1) return -1; - r = tls_timeoutio(SSL_connect,t,rfd,wfd,ssl,NULL,0); + r = tls_timeoutio(SSL_connect, t, rfd, wfd, ssl, NULL, 0); - if (r <= 0) { ndelay_off(rfd); ndelay_off(wfd); } - else SSL_set_mode(ssl,SSL_MODE_ENABLE_PARTIAL_WRITE); + if (r <= 0) { + ndelay_off(rfd); + ndelay_off(wfd); + } else + SSL_set_mode(ssl, SSL_MODE_ENABLE_PARTIAL_WRITE); return r; } -int tls_timeoutrehandshake(int t,int rfd,int wfd,SSL *ssl) +int tls_timeoutrehandshake(int t, int rfd, int wfd, SSL *ssl) { int r; SSL_renegotiate(ssl); - r = tls_timeoutio(SSL_do_handshake,t,rfd,wfd,ssl,NULL,0); + r = tls_timeoutio(SSL_do_handshake, t, rfd, wfd, ssl, NULL, 0); if (r <= 0) return r; if (SSL_get_state(ssl) & SSL_ST_CONNECT) return -2; /* now a macro in ssl.h */ /* this is for the client only */ SSL_set_connect_state(ssl); - return tls_timeoutio(SSL_do_handshake,t,rfd,wfd,ssl,NULL,0); + return tls_timeoutio(SSL_do_handshake, t, rfd, wfd, ssl, NULL, 0); } -int tls_timeoutread(int t,int rfd,int wfd,SSL *ssl,char *buf,int len) +int tls_timeoutread(int t, int rfd, int wfd, SSL *ssl, char *buf, int len) { if (!buf) return 0; - if (SSL_pending(ssl)) return SSL_read(ssl,buf,len); - return tls_timeoutio(SSL_read,t,rfd,wfd,ssl,buf,len); + if (SSL_pending(ssl)) return SSL_read(ssl, buf, len); + return tls_timeoutio(SSL_read, t, rfd, wfd, ssl, buf, len); } -int tls_timeoutwrite(int t,int rfd,int wfd,SSL *ssl,char *buf,int len) +int tls_timeoutwrite(int t, int rfd, int wfd, SSL *ssl, char *buf, int len) { if (!buf) return 0; - return tls_timeoutio(SSL_write,t,rfd,wfd,ssl,buf,len); + return tls_timeoutio(SSL_write, t, rfd, wfd, ssl, buf, len); } diff --git a/src/token822.c b/src/token822.c index 239887c..e1048c6 100644 --- a/src/token822.c +++ b/src/token822.c @@ -1,10 +1,11 @@ -#include "stralloc.h" +#include "token822.h" + #include "alloc.h" #include "genalloc.h" #include "str.h" -#include "token822.h" +#include "stralloc.h" -static struct token822 comma = { TOKEN822_COMMA }; +static struct token822 comma = {TOKEN822_COMMA}; void token822_reverse(token822_alloc *ta) { @@ -17,14 +18,15 @@ void token822_reverse(token822_alloc *ta) temp = ta->t[i]; ta->t[i] = ta->t[n - i]; ta->t[n - i] = temp; - } + } } -GEN_ALLOC_ready(token822_alloc,struct token822,t,len,a,i,n,x,30,token822_ready) -GEN_ALLOC_readyplus(token822_alloc,struct token822,t,len,a,i,n,x,30,token822_readyplus) -GEN_ALLOC_append(token822_alloc,struct token822,t,len,a,i,n,x,30,token822_readyplus,token822_append) +GEN_ALLOC_ready(token822_alloc, struct token822, t, len, a, i, n, x, 30, token822_ready) + GEN_ALLOC_readyplus(token822_alloc, struct token822, t, len, a, i, n, x, 30, token822_readyplus) + GEN_ALLOC_append( + token822_alloc, struct token822, t, len, a, i, n, x, 30, token822_readyplus, token822_append) -static int needspace(int t1,int t2) + static int needspace(int t1, int t2) { if (!t1) return 0; if (t1 == TOKEN822_COLON) return 1; @@ -32,12 +34,15 @@ static int needspace(int t1,int t2) if (t2 == TOKEN822_LEFT) return 1; switch (t1) { - case TOKEN822_ATOM: case TOKEN822_LITERAL: - case TOKEN822_QUOTE: case TOKEN822_COMMENT: + case TOKEN822_ATOM: + case TOKEN822_LITERAL: + case TOKEN822_QUOTE: + case TOKEN822_COMMENT: switch (t2) { - case TOKEN822_ATOM: case TOKEN822_LITERAL: - case TOKEN822_QUOTE: case TOKEN822_COMMENT: - return 1; + case TOKEN822_ATOM: + case TOKEN822_LITERAL: + case TOKEN822_QUOTE: + case TOKEN822_COMMENT: return 1; } } return 0; @@ -46,11 +51,20 @@ static int needspace(int t1,int t2) static int atomok(char ch) { switch (ch) { - case ' ': case '\t': case '\r': case '\n': - case '(': case '[': case '"': - case '<': case '>': case ';': case ':': - case '@': case ',': case '.': - return 0; + case ' ': + case '\t': + case '\r': + case '\n': + case '(': + case '[': + case '"': + case '<': + case '>': + case ';': + case ':': + case '@': + case ',': + case '.': return 0; } return 1; } @@ -59,7 +73,7 @@ static void atomcheck(struct token822 *t) { int i; char ch; - + for (i = 0; i < t->slen; ++i) { ch = t->s[i]; if ((ch < 32) || (ch > 126) || (ch == ')') || (ch == ']') || (ch == '\\')) { @@ -69,7 +83,7 @@ static void atomcheck(struct token822 *t) } } -int token822_unparse(stralloc *sa,token822_alloc *ta,unsigned int linelen) +int token822_unparse(stralloc *sa, token822_alloc *ta, unsigned int linelen) { struct token822 *t; int len; @@ -88,29 +102,40 @@ int token822_unparse(stralloc *sa,token822_alloc *ta,unsigned int linelen) for (i = 0; i < ta->len; ++i) { t = ta->t + i; newtype = t->type; - if (needspace(lasttype,newtype)) ++len; + if (needspace(lasttype, newtype)) ++len; lasttype = newtype; switch (newtype) { - case TOKEN822_COMMA: - len += 3; break; - case TOKEN822_AT: case TOKEN822_DOT: case TOKEN822_LEFT: case TOKEN822_RIGHT: - case TOKEN822_SEMI: case TOKEN822_COLON: - ++len; break; - case TOKEN822_ATOM: case TOKEN822_QUOTE: case TOKEN822_LITERAL: case TOKEN822_COMMENT: + case TOKEN822_COMMA: len += 3; break; + case TOKEN822_AT: + case TOKEN822_DOT: + case TOKEN822_LEFT: + case TOKEN822_RIGHT: + case TOKEN822_SEMI: + case TOKEN822_COLON: ++len; break; + case TOKEN822_ATOM: + case TOKEN822_QUOTE: + case TOKEN822_LITERAL: + case TOKEN822_COMMENT: if (t->type != TOKEN822_ATOM) len += 2; - for (j = 0; j < t->slen; ++j) - switch (ch = t->s[j]) { - case '"': case '[': case ']': case '(': case ')': - case '\\': case '\r': case '\n': ++len; - default: ++len; + for (j = 0; j < t->slen; ++j) switch (ch = t->s[j]) + { + case '"': + case '[': + case ']': + case '(': + case ')': + case '\\': + case '\r': + case '\n': ++len; + default: ++len; } - break; + break; } } len += 2; - if (!stralloc_ready(sa,len)) return -1; + if (!stralloc_ready(sa, len)) return -1; s = sa->s; lineb = s; @@ -121,49 +146,64 @@ int token822_unparse(stralloc *sa,token822_alloc *ta,unsigned int linelen) for (i = 0; i < ta->len; ++i) { t = ta->t + i; newtype = t->type; - if (needspace(lasttype,newtype)) *s++ = ' '; + if (needspace(lasttype, newtype)) *s++ = ' '; lasttype = newtype; switch (newtype) { - case TOKEN822_COMMA: - *s++ = ','; -#define NSUW \ - s[0] = '\n'; s[1] = ' '; \ - if (linee && (!linelen || (s - lineb <= linelen))) \ - { while (linee < s) { linee[0] = linee[2]; ++linee; } linee -= 2; } \ - else { if (linee) lineb = linee + 1; linee = s; s += 2; } - NSUW - break; - case TOKEN822_AT: *s++ = '@'; break; - case TOKEN822_DOT: *s++ = '.'; break; - case TOKEN822_LEFT: *s++ = '<'; break; - case TOKEN822_RIGHT: *s++ = '>'; break; - case TOKEN822_SEMI: *s++ = ';'; break; - case TOKEN822_COLON: *s++ = ':'; break; - case TOKEN822_ATOM: case TOKEN822_QUOTE: case TOKEN822_LITERAL: case TOKEN822_COMMENT: - if (t->type == TOKEN822_QUOTE) *s++ = '"'; - if (t->type == TOKEN822_LITERAL) *s++ = '['; - if (t->type == TOKEN822_COMMENT) *s++ = '('; - - for (j = 0; j < t->slen; ++j) - switch (ch = t->s[j]) { - case '"': case '[': case ']': case '(': case ')': - case '\\': case '\r': case '\n': *s++ = '\\'; - default: *s++ = ch; - } - if (t->type == TOKEN822_QUOTE) *s++ = '"'; - if (t->type == TOKEN822_LITERAL) *s++ = ']'; - if (t->type == TOKEN822_COMMENT) *s++ = ')'; - break; + case TOKEN822_COMMA: *s++ = ','; +#define NSUW \ + s[0] = '\n'; \ + s[1] = ' '; \ + if (linee && (!linelen || (s - lineb <= linelen))) { \ + while (linee < s) { \ + linee[0] = linee[2]; \ + ++linee; \ + } \ + linee -= 2; \ + } else { \ + if (linee) lineb = linee + 1; \ + linee = s; \ + s += 2; \ + } + NSUW break; + case TOKEN822_AT: *s++ = '@'; break; + case TOKEN822_DOT: *s++ = '.'; break; + case TOKEN822_LEFT: *s++ = '<'; break; + case TOKEN822_RIGHT: *s++ = '>'; break; + case TOKEN822_SEMI: *s++ = ';'; break; + case TOKEN822_COLON: *s++ = ':'; break; + case TOKEN822_ATOM: + case TOKEN822_QUOTE: + case TOKEN822_LITERAL: + case TOKEN822_COMMENT: + if (t->type == TOKEN822_QUOTE) *s++ = '"'; + if (t->type == TOKEN822_LITERAL) *s++ = '['; + if (t->type == TOKEN822_COMMENT) *s++ = '('; + + for (j = 0; j < t->slen; ++j) switch (ch = t->s[j]) + { + case '"': + case '[': + case ']': + case '(': + case ')': + case '\\': + case '\r': + case '\n': *s++ = '\\'; + default: *s++ = ch; + } + if (t->type == TOKEN822_QUOTE) *s++ = '"'; + if (t->type == TOKEN822_LITERAL) *s++ = ']'; + if (t->type == TOKEN822_COMMENT) *s++ = ')'; + break; } } - NSUW - --s; - sa->len = s - sa->s; - return 1; + NSUW-- s; + sa->len = s - sa->s; + return 1; } -int token822_unquote(stralloc *sa,token822_alloc *ta) +int token822_unquote(stralloc *sa, token822_alloc *ta) { struct token822 *t; int len; @@ -176,17 +216,20 @@ int token822_unquote(stralloc *sa,token822_alloc *ta) for (i = 0; i < ta->len; ++i) { t = ta->t + i; switch (t->type) { - case TOKEN822_COMMA: case TOKEN822_AT: case TOKEN822_DOT: case TOKEN822_LEFT: - case TOKEN822_RIGHT: case TOKEN822_SEMI: case TOKEN822_COLON: - ++len; break; - case TOKEN822_LITERAL: - len += 2; - case TOKEN822_ATOM: case TOKEN822_QUOTE: - len += t->slen; + case TOKEN822_COMMA: + case TOKEN822_AT: + case TOKEN822_DOT: + case TOKEN822_LEFT: + case TOKEN822_RIGHT: + case TOKEN822_SEMI: + case TOKEN822_COLON: ++len; break; + case TOKEN822_LITERAL: len += 2; + case TOKEN822_ATOM: + case TOKEN822_QUOTE: len += t->slen; } } - if (!stralloc_ready(sa,len)) return -1; + if (!stralloc_ready(sa, len)) return -1; s = sa->s; @@ -194,16 +237,17 @@ int token822_unquote(stralloc *sa,token822_alloc *ta) t = ta->t + i; switch (t->type) { case TOKEN822_COMMA: *s++ = ','; break; - case TOKEN822_AT: *s++ = '@'; break; - case TOKEN822_DOT: *s++ = '.'; break; - case TOKEN822_LEFT: *s++ = '<'; break; + case TOKEN822_AT: *s++ = '@'; break; + case TOKEN822_DOT: *s++ = '.'; break; + case TOKEN822_LEFT: *s++ = '<'; break; case TOKEN822_RIGHT: *s++ = '>'; break; - case TOKEN822_SEMI: *s++ = ';'; break; + case TOKEN822_SEMI: *s++ = ';'; break; case TOKEN822_COLON: *s++ = ':'; break; - case TOKEN822_ATOM: case TOKEN822_QUOTE: case TOKEN822_LITERAL: + case TOKEN822_ATOM: + case TOKEN822_QUOTE: + case TOKEN822_LITERAL: if (t->type == TOKEN822_LITERAL) *s++ = '['; - for (j = 0; j < t->slen; ++j) - *s++ = t->s[j]; + for (j = 0; j < t->slen; ++j) *s++ = t->s[j]; if (t->type == TOKEN822_LITERAL) *s++ = ']'; break; case TOKEN822_COMMENT: break; @@ -213,7 +257,7 @@ int token822_unquote(stralloc *sa,token822_alloc *ta) return 1; } -int token822_parse(token822_alloc *ta,stralloc *sa,stralloc *buf) +int token822_parse(token822_alloc *ta, stralloc *sa, stralloc *buf) { int i; int salen; @@ -228,13 +272,23 @@ int token822_parse(token822_alloc *ta,stralloc *sa,stralloc *buf) numchars = 0; numtoks = 0; - for (i = 0; i < salen; ++i) - switch (sa->s[i]) { - case '.': case ',': case '@': case '<': case '>': case ':': case ';': - ++numtoks; break; - case ' ': case '\t': case '\r': case '\n': break; - case ')': case ']': return 0; - /* other control chars and non-ASCII chars are also bad, in theory */ + for (i = 0; i < salen; ++i) switch (sa->s[i]) + { + case '.': + case ',': + case '@': + case '<': + case '>': + case ':': + case ';': ++numtoks; break; + case ' ': + case '\t': + case '\r': + case '\n': break; + case ')': + case ']': + return 0; + /* other control chars and non-ASCII chars are also bad, in theory */ case '(': level = 1; while (level) { @@ -242,8 +296,9 @@ int token822_parse(token822_alloc *ta,stralloc *sa,stralloc *buf) switch (sa->s[i]) { case '(': ++level; break; case ')': --level; break; - case '\\': if (++i >= salen) return 0; - default: ++numchars; + case '\\': + if (++i >= salen) return 0; + default: ++numchars; } } ++numtoks; @@ -254,7 +309,8 @@ int token822_parse(token822_alloc *ta,stralloc *sa,stralloc *buf) if (++i >= salen) return 0; switch (sa->s[i]) { case '"': --level; break; - case '\\': if (++i >= salen) return 0; + case '\\': + if (++i >= salen) return 0; default: ++numchars; } } @@ -266,84 +322,120 @@ int token822_parse(token822_alloc *ta,stralloc *sa,stralloc *buf) if (++i >= salen) return 0; switch (sa->s[i]) { case ']': --level; break; - case '\\': if (++i >= salen) return 0; - default: ++numchars; + case '\\': + if (++i >= salen) return 0; + default: ++numchars; } } ++numtoks; break; default: do { - if (sa->s[i] == '\\') if (++i >= salen) break; + if (sa->s[i] == '\\') + if (++i >= salen) break; ++numchars; - if (++i >= salen) break; + if (++i >= salen) break; } while (atomok(sa->s[i])); --i; ++numtoks; } - if (!token822_ready(ta,numtoks)) return -1; - if (!stralloc_ready(buf,numchars)) return -1; + if (!token822_ready(ta, numtoks)) return -1; + if (!stralloc_ready(buf, numchars)) return -1; cbuf = buf->s; ta->len = numtoks; t = ta->t; - for (i = 0; i < salen; ++i) - switch (sa->s[i]) { - case '.': t->type = TOKEN822_DOT; ++t; break; - case ',': t->type = TOKEN822_COMMA; ++t; break; - case '@': t->type = TOKEN822_AT; ++t; break; - case '<': t->type = TOKEN822_LEFT; ++t; break; - case '>': t->type = TOKEN822_RIGHT; ++t; break; - case ':': t->type = TOKEN822_COLON; ++t; break; - case ';': t->type = TOKEN822_SEMI; ++t; break; - case ' ': case '\t': case '\r': case '\n': break; + for (i = 0; i < salen; ++i) switch (sa->s[i]) + { + case '.': + t->type = TOKEN822_DOT; + ++t; + break; + case ',': + t->type = TOKEN822_COMMA; + ++t; + break; + case '@': + t->type = TOKEN822_AT; + ++t; + break; + case '<': + t->type = TOKEN822_LEFT; + ++t; + break; + case '>': + t->type = TOKEN822_RIGHT; + ++t; + break; + case ':': + t->type = TOKEN822_COLON; + ++t; + break; + case ';': + t->type = TOKEN822_SEMI; + ++t; + break; + case ' ': + case '\t': + case '\r': + case '\n': break; case '(': - t->type = TOKEN822_COMMENT; t->s = cbuf; t->slen = 0; + t->type = TOKEN822_COMMENT; + t->s = cbuf; + t->slen = 0; level = 1; while (level) { ++i; /* assert: < salen */ switch (sa->s[i]) { - case '(': ++level; break; - case ')': --level; break; + case '(': ++level; break; + case ')': --level; break; case '\\': ++i; /* assert: < salen */ - default: *cbuf++ = sa->s[i]; ++t->slen; + default: *cbuf++ = sa->s[i]; ++t->slen; } } ++t; break; case '"': - t->type = TOKEN822_QUOTE; t->s = cbuf; t->slen = 0; + t->type = TOKEN822_QUOTE; + t->s = cbuf; + t->slen = 0; level = 1; while (level) { ++i; /* assert: < salen */ switch (sa->s[i]) { - case '"': --level; break; + case '"': --level; break; case '\\': ++i; /* assert: < salen */ - default: *cbuf++ = sa->s[i]; ++t->slen; + default: *cbuf++ = sa->s[i]; ++t->slen; } } ++t; break; case '[': - t->type = TOKEN822_LITERAL; t->s = cbuf; t->slen = 0; + t->type = TOKEN822_LITERAL; + t->s = cbuf; + t->slen = 0; level = 1; while (level) { ++i; /* assert: < salen */ switch (sa->s[i]) { - case ']': --level; break; + case ']': --level; break; case '\\': ++i; /* assert: < salen */ - default: *cbuf++ = sa->s[i]; ++t->slen; + default: *cbuf++ = sa->s[i]; ++t->slen; } } ++t; break; default: - t->type = TOKEN822_ATOM; t->s = cbuf; t->slen = 0; + t->type = TOKEN822_ATOM; + t->s = cbuf; + t->slen = 0; do { - if (sa->s[i] == '\\') if (++i >= salen) break; - *cbuf++ = sa->s[i]; ++t->slen; + if (sa->s[i] == '\\') + if (++i >= salen) break; + *cbuf++ = sa->s[i]; + ++t->slen; if (++i >= salen) break; } while (atomok(sa->s[i])); atomcheck(t); @@ -353,24 +445,22 @@ int token822_parse(token822_alloc *ta,stralloc *sa,stralloc *buf) return 1; } -static int gotaddr(token822_alloc *taout,token822_alloc *taaddr,int (*callback)()) +static int gotaddr(token822_alloc *taout, token822_alloc *taaddr, int (*callback)()) { int i; - if (callback(taaddr) != 1) - return 0; + if (callback(taaddr) != 1) return 0; + + if (!token822_readyplus(taout, taaddr->len)) return 0; - if (!token822_readyplus(taout,taaddr->len)) - return 0; - - for (i = 0; i < taaddr->len; ++i) - taout->t[taout->len++] = taaddr->t[i]; + for (i = 0; i < taaddr->len; ++i) taout->t[taout->len++] = taaddr->t[i]; taaddr->len = 0; return 1; } -int token822_addrlist(token822_alloc *taout,token822_alloc *taaddr,token822_alloc *ta,int (*callback)()) +int token822_addrlist( + token822_alloc *taout, token822_alloc *taaddr, token822_alloc *ta, int (*callback)()) { struct token822 *t; struct token822 *beginning; @@ -380,23 +470,29 @@ int token822_addrlist(token822_alloc *taout,token822_alloc *taaddr,token822_allo taout->len = 0; taaddr->len = 0; - if (!token822_readyplus(taout,1)) return -1; - if (!token822_readyplus(taaddr,1)) return -1; - + if (!token822_readyplus(taout, 1)) return -1; + if (!token822_readyplus(taaddr, 1)) return -1; + ingroup = 0; wordok = 1; beginning = ta->t + 2; t = ta->t + ta->len - 1; - /* rfc 822 address lists are easy to parse from right to left */ + /* rfc 822 address lists are easy to parse from right to left */ -#define FLUSH if (taaddr->len) if (!gotaddr(taout,taaddr,callback)) return -1; -#define FLUSHCOMMA if (taaddr->len) { \ -if (!gotaddr(taout,taaddr,callback)) return -1; \ -if (!token822_append(taout,&comma)) return -1; } -#define ADDRLEFT if (!token822_append(taaddr,t--)) return -1; -#define OUTLEFT if (!token822_append(taout,t--)) return -1; +#define FLUSH \ + if (taaddr->len) \ + if (!gotaddr(taout, taaddr, callback)) return -1; +#define FLUSHCOMMA \ + if (taaddr->len) { \ + if (!gotaddr(taout, taaddr, callback)) return -1; \ + if (!token822_append(taout, &comma)) return -1; \ + } +#define ADDRLEFT \ + if (!token822_append(taaddr, t--)) return -1; +#define OUTLEFT \ + if (!token822_append(taout, t--)) return -1; while (t >= beginning) { switch (t->type) { @@ -410,35 +506,34 @@ if (!token822_append(taout,&comma)) return -1; } FLUSH if (!ingroup) return 0; ingroup = 0; - while ((t >= beginning) && (t->type != TOKEN822_COMMA)) - OUTLEFT - if (t >= beginning) - OUTLEFT + while ((t >= beginning) && (t->type != TOKEN822_COMMA)) OUTLEFT + if (t >= beginning) OUTLEFT wordok = 1; continue; case TOKEN822_RIGHT: FLUSHCOMMA OUTLEFT - while ((t >= beginning) && (t->type != TOKEN822_LEFT)) - ADDRLEFT - /* important to use address here even if it's empty: <> */ - if (!gotaddr(taout,taaddr,callback)) return -1; + while ((t >= beginning) && (t->type != TOKEN822_LEFT)) ADDRLEFT + /* important to use address here even if it's empty: <> */ + if (!gotaddr(taout, taaddr, callback)) return -1; if (t < beginning) return 0; OUTLEFT - while ((t >= beginning) && ((t->type == TOKEN822_COMMENT) || - (t->type == TOKEN822_ATOM) || (t->type == TOKEN822_QUOTE) || - (t->type == TOKEN822_AT) || (t->type == TOKEN822_DOT))) - OUTLEFT + while ( + (t >= beginning) + && ((t->type == TOKEN822_COMMENT) || (t->type == TOKEN822_ATOM) + || (t->type == TOKEN822_QUOTE) || (t->type == TOKEN822_AT) || (t->type == TOKEN822_DOT))) + OUTLEFT wordok = 0; continue; - case TOKEN822_ATOM: case TOKEN822_QUOTE: case TOKEN822_LITERAL: - if (!wordok) - FLUSHCOMMA + case TOKEN822_ATOM: + case TOKEN822_QUOTE: + case TOKEN822_LITERAL: + if (!wordok) FLUSHCOMMA wordok = 0; ADDRLEFT continue; case TOKEN822_COMMENT: - /* comment is lexically a space; shouldn't affect wordok */ + /* comment is lexically a space; shouldn't affect wordok */ break; case TOKEN822_COMMA: FLUSH @@ -448,13 +543,13 @@ if (!token822_append(taout,&comma)) return -1; } wordok = 1; ADDRLEFT continue; - } - OUTLEFT - } + } + OUTLEFT + } FLUSH ++t; while (t > ta->t) - if (!token822_append(taout,--t)) return -1; + if (!token822_append(taout, --t)) return -1; token822_reverse(taout); return 1; diff --git a/src/trigger.c b/src/trigger.c index ec22e11..4b7cc49 100644 --- a/src/trigger.c +++ b/src/trigger.c @@ -1,41 +1,43 @@ -#include "select.h" -#include "ndelay.h" -#include "open.h" #include "trigger.h" + #include "close.h" +#include "ndelay.h" +#include "open.h" +#include "select.h" static int fd = -1; -void trigger_set() +void trigger_set() { if (fd != -1) close(fd); fd = open_read("lock/trigger"); } -void trigger_selprep(int *nfds,fd_set *rfds) +void trigger_selprep(int *nfds, fd_set *rfds) { if (fd != -1) { - FD_SET(fd,rfds); + FD_SET(fd, rfds); if (*nfds < fd + 1) *nfds = fd + 1; } } -int trigger_pulled(fd_set *rfds) +int trigger_pulled(fd_set *rfds) { - if (fd != -1) if (FD_ISSET(fd,rfds)) return 1; + if (fd != -1) + if (FD_ISSET(fd, rfds)) return 1; return 0; } -void write(); /* compiler warning (temp) */ +void write(); /* compiler warning (temp) */ -void triggerpull() +void triggerpull() { int fd; fd = open_write("lock/trigger"); if (fd >= 0) { ndelay_on(fd); - write(fd,"",1); /* if it fails, bummer */ + write(fd, "", 1); /* if it fails, bummer */ close(fd); } } diff --git a/src/triggerpull.c b/src/triggerpull.c index 3691c5a..a55797f 100644 --- a/src/triggerpull.c +++ b/src/triggerpull.c @@ -1,7 +1,9 @@ +#include "triggerpull.h" + #include <unistd.h> + #include "ndelay.h" #include "open.h" -#include "triggerpull.h" void triggerpull(void) { @@ -10,7 +12,7 @@ void triggerpull(void) fd = open_write("lock/trigger"); if (fd >= 0) { ndelay_on(fd); - write(fd,"",1); /* if it fails, bummer */ + write(fd, "", 1); /* if it fails, bummer */ close(fd); } } diff --git a/src/trycpp.c b/src/trycpp.c index 690f2f3..5fdf435 100644 --- a/src/trycpp.c +++ b/src/trycpp.c @@ -1,7 +1,9 @@ int main() { #ifdef NeXT - printf("nextstep\n"); exit(0); + printf("nextstep\n"); + exit(0); #endif - printf("unknown\n"); exit(0); + printf("unknown\n"); + exit(0); } diff --git a/src/trydrent.c b/src/trydrent.c index c778176..c1967e6 100644 --- a/src/trydrent.c +++ b/src/trydrent.c @@ -1,5 +1,5 @@ -#include <sys/types.h> #include <dirent.h> +#include <sys/types.h> void foo() { diff --git a/src/tryflock.c b/src/tryflock.c index b18743a..e15acca 100644 --- a/src/tryflock.c +++ b/src/tryflock.c @@ -1,8 +1,8 @@ -#include <sys/types.h> -#include <sys/file.h> #include <fcntl.h> +#include <sys/file.h> +#include <sys/types.h> int main() { - flock(0,LOCK_EX | LOCK_UN | LOCK_NB); + flock(0, LOCK_EX | LOCK_UN | LOCK_NB); } diff --git a/src/trymkffo.c b/src/trymkffo.c index e832a31..573c891 100644 --- a/src/trymkffo.c +++ b/src/trymkffo.c @@ -1,7 +1,7 @@ -#include <sys/types.h> #include <sys/stat.h> +#include <sys/types.h> int main() { - mkfifo("temp-trymkffo",0); + mkfifo("temp-trymkffo", 0); } diff --git a/src/trynpbg1.c b/src/trynpbg1.c index 01a152e..da3e47e 100644 --- a/src/trynpbg1.c +++ b/src/trynpbg1.c @@ -1,6 +1,6 @@ -#include "select.h" -#include "open.h" #include "fifo.h" +#include "open.h" +#include "select.h" #define FN "temp-trynpbg1.fifo" @@ -9,16 +9,15 @@ int main() int flagbug; struct timeval instant; fd_set rfds; - + flagbug = 0; - if (fifo_make(FN,0600) != -1) { + if (fifo_make(FN, 0600) != -1) { close(0); if (open_read(FN) == 0) { FD_ZERO(&rfds); - FD_SET(0,&rfds); + FD_SET(0, &rfds); instant.tv_sec = instant.tv_usec = 0; - if (select(1,&rfds,(fd_set *) 0,(fd_set *) 0,&instant) > 0) - flagbug = 1; + if (select(1, &rfds, (fd_set *)0, (fd_set *)0, &instant) > 0) flagbug = 1; } unlink(FN); } diff --git a/src/trysalen.c b/src/trysalen.c index 731a109..05a0b13 100644 --- a/src/trysalen.c +++ b/src/trysalen.c @@ -1,8 +1,8 @@ -#include <sys/types.h> -#include <sys/param.h> -#include <sys/time.h> #include <sys/ioctl.h> +#include <sys/param.h> #include <sys/socket.h> +#include <sys/time.h> +#include <sys/types.h> void foo() { diff --git a/src/trysgact.c b/src/trysgact.c index 1471ecc..c9714ff 100644 --- a/src/trysgact.c +++ b/src/trysgact.c @@ -6,5 +6,5 @@ int main() sa.sa_handler = 0; sa.sa_flags = 0; sigemptyset(&sa.sa_mask); - sigaction(0,&sa,(struct sigaction *) 0); + sigaction(0, &sa, (struct sigaction *)0); } diff --git a/src/trysgprm.c b/src/trysgprm.c index 3d73f86..7a2c6b6 100644 --- a/src/trysgprm.c +++ b/src/trysgprm.c @@ -3,8 +3,8 @@ int main() { sigset_t ss; - + sigemptyset(&ss); - sigaddset(&ss,SIGCHLD); - sigprocmask(SIG_SETMASK,&ss,(sigset_t *) 0); + sigaddset(&ss, SIGCHLD); + sigprocmask(SIG_SETMASK, &ss, (sigset_t *)0); } diff --git a/src/tryshsgr.c b/src/tryshsgr.c index 81b395c..ea1bf66 100644 --- a/src/tryshsgr.c +++ b/src/tryshsgr.c @@ -1,14 +1,15 @@ int main() { short x[4]; - + x[0] = x[1] = 1; - if (getgroups(1,x) == 0) if (setgroups(1,x) == -1) _exit(1); - - if (getgroups(1,x) == -1) _exit(1); + if (getgroups(1, x) == 0) + if (setgroups(1, x) == -1) _exit(1); + + if (getgroups(1, x) == -1) _exit(1); if (x[1] != 1) _exit(1); x[1] = 2; - if (getgroups(1,x) == -1) _exit(1); + if (getgroups(1, x) == -1) _exit(1); if (x[1] != 2) _exit(1); _exit(0); } diff --git a/src/trysysel.c b/src/trysysel.c index abebad5..e974364 100644 --- a/src/trysysel.c +++ b/src/trysysel.c @@ -1,11 +1,13 @@ -#include <sys/types.h> -#include <sys/time.h> #include <sys/select.h> /* SVR4 silliness */ +#include <sys/time.h> +#include <sys/types.h> + #include <stdio.h> + #include "select.h" int main() { - printf("FD_SETSIZE:%d\n",FD_SETSIZE); - return 0; + printf("FD_SETSIZE:%d\n", FD_SETSIZE); + return 0; } diff --git a/src/trysyslog.c b/src/trysyslog.c index 4b99afc..5408436 100644 --- a/src/trysyslog.c +++ b/src/trysyslog.c @@ -1,9 +1,9 @@ -#include <sys/types.h> #include <sys/time.h> +#include <sys/types.h> #include <syslog.h> main() { - openlog("foo",0,LOG_MAIL); - syslog(0,"foo"); + openlog("foo", 0, LOG_MAIL); + syslog(0, "foo"); } diff --git a/src/tryulong32.c b/src/tryulong32.c index 20683d6..43951e9 100644 --- a/src/tryulong32.c +++ b/src/tryulong32.c @@ -2,10 +2,38 @@ int main() { unsigned long u; u = 1; - u += u; u += u; u += u; u += u; u += u; u += u; u += u; u += u; - u += u; u += u; u += u; u += u; u += u; u += u; u += u; u += u; - u += u; u += u; u += u; u += u; u += u; u += u; u += u; u += u; - u += u; u += u; u += u; u += u; u += u; u += u; u += u; u += u; + u += u; + u += u; + u += u; + u += u; + u += u; + u += u; + u += u; + u += u; + u += u; + u += u; + u += u; + u += u; + u += u; + u += u; + u += u; + u += u; + u += u; + u += u; + u += u; + u += u; + u += u; + u += u; + u += u; + u += u; + u += u; + u += u; + u += u; + u += u; + u += u; + u += u; + u += u; + u += u; if (!u) _exit(0); _exit(1); } diff --git a/src/tryutmp.c b/src/tryutmp.c index 2a25e5d..7c8deb3 100644 --- a/src/tryutmp.c +++ b/src/tryutmp.c @@ -1,5 +1,6 @@ +#include <utmp.h> + #include <sys/types.h> -#include <utmp.h> int main() { diff --git a/src/trywaitp.c b/src/trywaitp.c index 0380358..c6d50e8 100644 --- a/src/trywaitp.c +++ b/src/trywaitp.c @@ -3,5 +3,5 @@ int main() { - waitpid(0,0,0); + waitpid(0, 0, 0); } diff --git a/src/wildmat.c b/src/wildmat.c index 739f943..363c118 100644 --- a/src/wildmat.c +++ b/src/wildmat.c @@ -37,8 +37,8 @@ ** on. */ -#define TRUE 1 -#define FALSE 0 +#define TRUE 1 +#define FALSE 0 #define ABORT -1 /* What character marks an inverted character class? */ @@ -53,57 +53,50 @@ */ static int DoMatch(register char *text, register char *p) { - register int last; - register int matched; - register int reverse; + register int last; + register int matched; + register int reverse; for (; *p; text++, p++) { - if (*text == '\0' && *p != '*') - return ABORT; + if (*text == '\0' && *p != '*') return ABORT; switch (*p) { - case '\\': /* Literal match with following character. */ - p++; - case '?': /* Match anything. */ - continue; - case '*': /* Consecutive stars act just like one. */ - while (*++p == '*') - continue; - if (*p == '\0') return TRUE; /* Trailing star matches everything. */ - while (*text) - if ((matched = DoMatch(text++, p)) != FALSE) return matched; - return ABORT; - case '[': - reverse = p[1] == NEGATE_CLASS ? TRUE : FALSE; - if (reverse) p++; /* Inverted character class. */ - matched = FALSE; - if (p[1] == ']' || p[1] == '-') - if (*++p == *text) matched = TRUE; - for (last = *p; *++p && *p != ']'; last = *p) /* This next line requires a good C compiler. */ - if (*p == '-' && p[1] != ']' ? *text <= *++p && *text >= last : *text == *p) - matched = TRUE; - if (matched == reverse) return FALSE; - continue; - default: /* FALLTHROUGH */ - if (*text != *p) return FALSE; - continue; + case '\\': /* Literal match with following character. */ p++; + case '?': /* Match anything. */ continue; + case '*': /* Consecutive stars act just like one. */ + while (*++p == '*') continue; + if (*p == '\0') return TRUE; /* Trailing star matches everything. */ + while (*text) + if ((matched = DoMatch(text++, p)) != FALSE) return matched; + return ABORT; + case '[': + reverse = p[1] == NEGATE_CLASS ? TRUE : FALSE; + if (reverse) p++; /* Inverted character class. */ + matched = FALSE; + if (p[1] == ']' || p[1] == '-') + if (*++p == *text) matched = TRUE; + for (last = *p; *++p && *p != ']'; last = *p) /* This next line requires a good C compiler. */ + if (*p == '-' && p[1] != ']' ? *text <= *++p && *text >= last : *text == *p) matched = TRUE; + if (matched == reverse) return FALSE; + continue; + default: /* FALLTHROUGH */ + if (*text != *p) return FALSE; + continue; } } -#ifdef MATCH_TAR_PATTERN - if (*text == '/') - return TRUE; -#endif /* MATCH_TAR_ATTERN */ +#ifdef MATCH_TAR_PATTERN + if (*text == '/') return TRUE; +#endif /* MATCH_TAR_ATTERN */ return *text == '\0'; } /* ** User-level routine. Returns TRUE or FALSE. */ -int wildmat(char *text,char *p) +int wildmat(char *text, char *p) { -#ifdef OPTIMIZE_JUST_STAR - if (p[0] == '*' && p[1] == '\0') - return TRUE; -#endif /* OPTIMIZE_JUST_STAR */ +#ifdef OPTIMIZE_JUST_STAR + if (p[0] == '*' && p[1] == '\0') return TRUE; +#endif /* OPTIMIZE_JUST_STAR */ return DoMatch(text, p) == TRUE; } |