s/qmail 4.4.13
Next generation secure email transport
Loading...
Searching...
No Matches
qmail-remote.c
Go to the documentation of this file.
1#ifdef IDN2
2#include <idn2.h>
3#endif
4#include <sys/types.h>
5#include <sys/stat.h>
6#include <sys/socket.h>
7#include <netinet/in.h>
8#include <arpa/inet.h>
9#include <unistd.h>
10#include "sig.h"
11#include "stralloc.h"
12#include "buffer.h"
13#include "scan.h"
14#include "case.h"
15#include "byte.h"
16#include "qmail.h"
17#include "auto_qmail.h"
18#include "control.h"
19#include "dns.h"
20#include "alloc.h"
21#include "genalloc.h"
22#include "quote.h"
23#include "fmt.h"
24#include "ip.h"
25#include "ipalloc.h"
26#include "ipme.h"
27#include "str.h"
28#include "now.h"
29#include "exit.h"
30#include "constmap.h"
31#include "tcpto.h"
32#include "timeout.h"
33#include "timeoutconn.h"
34#include "base64.h"
35#include "socket_if.h"
36#include "ucspissl.h"
37#include "tls_remote.h"
38#include "tls_errors.h"
39#include "tls_timeoutio.h"
40#include "uint_t.h"
41#include "hmac_md5.h"
42
43#define WHO "qmail-remote"
44
45#define QMTP_MAX 200000000 /* 190 MB for QMTP */
46#define HUGESMTPTEXT 1000 /* RFC 5322; was 5000 chars/line */
47#define PORT_SMTP 25 /* silly rabbit, /etc/services is for users */
48#define PORT_QMTP 209
49#define PORT_SMTPS 465
50#define PORT_SUBMISSION 587
51#define PORT_QMTPS 6209
52#define VERIFYDEPTH 1
53#define TCP_TIMEOUT 60
54#define SMTP_TIMEOUT 1200
55
56unsigned long port = PORT_SMTP;
57
60int flagauth = 0; /* 1 = login; 2 = plain; 3 = crammd5 */
61int flagsmtps = 0; /* RFC 8314 - 'implicit TLS' */
62int flagtlsdomain = 0; /* 0 = no; 1 = yes; 2 = cert */
63int flagtls = 0; /* flagtls: XYZ (set for remote MTA by tls_destination(host) - from tls_remote.c)
64 (mode) Z: -2 = rejected; -1 = not; 0 = no, default; Z > 0 see tls_remote.c
65 (prot) Y: 0 = StartTLS; 1 = SMTPS; 2 = QMTPS
66 (active) X: 1 = running TLS connection (after DNS lookup)
67 (done) Z: 1: CA chain; 2: Cert wildname; 3: Cert exactname;
68 4: Cert fingerprint; 5: TLSA record */
69int flagverify = 0; /* 1 = verify Cert against CA; 2 = verify against Dir; 3 = triggerd by TLSA;
70 -2 = Cert pinning; -1 = no TLSA validation */
71int flagutf8 = 0;
72
73GEN_ALLOC_typedef(saa,stralloc,sa,len,a)
74GEN_ALLOC_readyplus(saa,stralloc,sa,len,a,i,n,x,10,saa_readyplus)
75static stralloc sauninit = {0};
76
77stralloc helohost = {0};
78stralloc eaihost = {0};
79stralloc host = {0};
80stralloc idnhost = {0};
81stralloc sender = {0};
82stralloc canonhost = {0};
83stralloc remotehost = {0};
84stralloc canonbox = {0};
85stralloc senddomain = {0};
86stralloc sendip = {0};
87
88stralloc domainips = {0};
90char ip4[4];
91char ip6[16];
92uint32 ifidx = 0;
93char *authsender = 0;
94
95stralloc smtproutes = {0};
97stralloc qmtproutes = {0};
99
100saa reciplist = {0};
101stralloc recip = {0};
102
103char msgsize[FMT_ULONG];
104unsigned long msize = 0;
106
107SSL *ssl;
108SSL_CTX *ctx;
109
110char bufsmall[BUFFER_SMALL];
111buffer bs = BUFFER_INIT(buffer_unixwrite,1,bufsmall,sizeof(bufsmall));
112
113void out(char *s)
114{
115 if (buffer_puts(&bs,s) == -1)
116 _exit(0);
117 }
118void zero()
119{
120 if (buffer_put(&bs,"\0",1) == -1)
121 _exit(0);
122}
123void zerodie()
124{
125 zero();
126 buffer_flush(&bs);
127 if (ssl) tls_exit(ssl);
128 _exit(0);
129}
130
131void outsafe(stralloc *sa)
132{
133 int i;
134 char ch;
135 for (i = 0; i < sa->len; ++i) {
136 ch = sa->s[i];
137 if (ch == 0) continue;
138 if (ch < 33) ch = '?';
139 if (ch > 126) ch = '?';
140 if (buffer_put(&bs,&ch,1) == -1) _exit(0);
141 }
142}
143
145{
146 out("ZInvalid ipaddr in control/domainips (#4.3.0)\n");
147 zerodie();
148}
150{
151 out("ZOut of memory. (#4.3.0)\n");
152 zerodie();
153}
155{
156 out("ZSystem resources temporarily unavailable. (#4.3.0)\n");
157 zerodie();
158}
160{
161 out("ZCan't bind to local ip address: ");
162 outsafe(&sendip);
163 out(". (#4.3.0)\n");
164 zerodie();
165}
167{
168 out("ZSorry, I wasn't able to establish a SMTP connection: ");
170 out(". (#4.3.0)\n");
171 zerodie();
172}
174{
175 out("ZSorry, I wasn't able to establish a QMTP connection: ");
177 out(". (#4.3.1)\n");
178 zerodie();
179}
181{
182 out("ZUnable to read message. (#4.3.0)\n");
183 zerodie();
184}
186{
187 out("ZCNAME lookup failed temporarily for: ");
189 out(". (#4.4.3)\n");
190 zerodie();
191}
192void temp_dns()
193{
194 out("ZSorry, I couldn't find any host named: ");
195 outsafe(&host);
196 out(". (#4.1.2)\n");
197 zerodie();
198}
200{
201 out("ZSorry, I couldn't find a mail exchanger or IP address for: ");
202 outsafe(&host);
203 out(". Will try again. (#4.1.2)\n");
204 zerodie();
205}
207{
208 out("ZUnable to switch to home directory. (#4.3.0)\n");
209 zerodie();
210}
212{
213 out("ZUnable to read control files. (#4.3.0)\n");
214 zerodie();
215}
217{
218 out("DSMTP cannot transfer messages with partial final line. (#5.6.2)\n");
219 zerodie();
220}
222{
223 out("ZRecipient did not talk proper QMTP. (#4.3.0)\n");
224 zerodie();
225}
227{
228 out("Dqmail-remote was invoked improperly. (#5.3.5)\n");
229 zerodie();
230}
231void perm_dns()
232{
233 out("DSorry, I couldn't find any host named: ");
234 outsafe(&host);
235 out(". (#5.1.2)\n");
236 zerodie();
237}
239{
240 out("DSorry, I couldn't find a mail exchanger or IP address for: ");
241 outsafe(&host);
242 out(". (#5.4.4)\n");
243 zerodie();
244}
246{
247 out("DSorry, I could no deliver mail to MX: ");
248 outsafe(&host);
249 out(" ; domain does not accept mails. (#5.1.10)\n");
250 zerodie();
251}
253{
254 out("DSorry. Although I'm listed as a best-preference MX or A for that host,\n\
255it isn't in my control/locals file, so I don't treat it as local. (#5.4.6)\n");
256 zerodie();
257}
259{
260 out("ZSorry, no supported AUTH method found, trying later again. (#4.7.1)\n");
261 zerodie();
262}
263
265{
266 char ipaddr[IPFMT];
267 int len = 0;
268
269 switch (partner.af) {
270 case AF_INET:
271 len = ip4_fmt(ipaddr,(char *)&partner.addr.ip4.d); break;
272 case AF_INET6:
273 len = ip6_fmt(ipaddr,(char *)&partner.addr.ip6.d); break;
274 }
275 if (buffer_put(&bs,ipaddr,len) == -1) _exit(0);
276}
277
279
280void dropped()
281{
282 out("ZConnected to ");
283 outhost();
284 out(" but connection died. ");
285 if (flagcritical) out("TLS insufficiency or potential duplicate! ");
286 out("(#4.4.2)\n");
287 zerodie();
288}
289
291unsigned long timeout = SMTP_TIMEOUT;
293
294ssize_t saferead(int fd,char *buf,size_t len)
295{
296 int r;
297 if (ssl) {
299 if (r < 0) temp_tlserr();
300 } else {
301 r = timeoutread(timeout,smtpfd,buf,len);
302 }
303 if (r <= 0) dropped();
304 return r;
305}
306
307ssize_t safewrite(int fd,char *buf,size_t len)
308{
309 int r;
310 if (ssl) {
312 if (r < 0) temp_tlserr();
313 } else {
314 r = timeoutwrite(timeout,smtpfd,buf,len);
315 }
316 if (r <= 0) dropped();
317 return r;
318}
319
320char inbuf[BUFFER_MTUSIZE];
321buffer bi = BUFFER_INIT(buffer_unixread,0,inbuf,sizeof(inbuf));
322char outbuf[BUFFER_MTUSIZE];
323buffer bo = BUFFER_INIT(safewrite,-1,outbuf,sizeof(outbuf));
324char frombuf[BUFFER_SMALL];
325buffer bf = BUFFER_INIT(saferead,-1,frombuf,sizeof(frombuf));
326
327static stralloc smtptext = {0};
328static stralloc header = {0};
329
330void get(char *ch)
331{
332 buffer_get(&bf,ch,1);
333 if (*ch != '\r')
334 if (smtptext.len < HUGESMTPTEXT)
335 if (!stralloc_append(&smtptext,ch)) temp_nomem();
336}
337
338unsigned long smtpcode()
339{
340 char ch;
341 long code;
342
343 if (!stralloc_ready(&smtptext,HUGESMTPTEXT)) temp_nomem();
344 if (!stralloc_copys(&smtptext,"")) temp_nomem();
345
346 get(&ch); code = ch - '0';
347 get(&ch); code = code * 10 + (ch - '0');
348 get(&ch); code = code * 10 + (ch - '0');
349 for (;;) {
350 get(&ch);
351 if (ch != '-') break;
352 while (ch != '\n') get(&ch);
353 get(&ch);
354 get(&ch);
355 get(&ch);
356 }
357 while (ch != '\n') get(&ch);
358
359 return code;
360}
361
363{
364 int i;
365 if (smtptext.s) if (smtptext.len) {
366 out("Remote host said: ");
367 for (i = 0; i < smtptext.len; ++i)
368 if (!smtptext.s[i]) smtptext.s[i] = '?';
369 if (buffer_put(&bs,smtptext.s,smtptext.len) == -1) _exit(0);
370 smtptext.len = 0;
371 }
372}
373
374void quit(char *prepend,char *append)
375{
376 buffer_putsflush(&bo,"QUIT\r\n");
377 /* waiting for remote side is just too ridiculous */
378 out(prepend);
379 outhost();
380 out(append);
381 out(".\n");
382 outsmtptext();
383 zerodie();
384}
385
386void blast()
387{
388 int r;
389 int in;
390 int out;
391 int eom = 1; // end-of-message <CRLF>.<CRLF>
392 char tmpbuf[BUFSIZE_MESS]; // intermediate write buffer
393
394// New blast code; inspired by Bruce Guenter's 'fastremote patch' (2005)
395
396 while ((r = buffer_get(&bi,inbuf,sizeof(inbuf)))) { // read into buffer
397 if (r == -1) temp_read();
398
399 for (in = out = 0; in < r;) {
400 if (eom && inbuf[in] == '.') {
401 tmpbuf[out++] = '.';
402 tmpbuf[out++] = inbuf[in++];
403 }
404 eom = 0;
405 while (in < r) {
406 if (inbuf[in] == '\r') { in++; continue; } // CR is DKIM input
407 if (inbuf[in] == '\n') {
408 eom = 1;
409 ++in;
410 tmpbuf[out++] = '\r';
411 tmpbuf[out++] = '\n';
412 break;
413 }
414 tmpbuf[out++] = inbuf[in++];
415 }
416 }
417 if (out) buffer_put(&bo,tmpbuf,out);
418 }
419
420 if (!eom) perm_partialline();
421 flagcritical = 1;
422 buffer_put(&bo,".\r\n",3); // LF seen; finish with .<CRLF>
423 buffer_flush(&bo);
424}
425
426/* this file is too long -------------------------------------- client TLS */
427
428stralloc cafile = {0};
429stralloc cadir = {0};
430stralloc certfile = {0};
431stralloc keyfile = {0};
432stralloc keypwd = {0};
433stralloc ciphers = {0};
434
435char *tlsdestinfo = 0;
437
438stralloc domaincerts = {0};
440stralloc tlsdestinations = {0};
442unsigned long verifydepth = VERIFYDEPTH;
443
445{
446 ctx = ssl_client();
447 ssl_errstr();
448 if (!ctx) temp_tlsctx();
449
450/* Fetch CA infos for dest */
451
452 if (flagverify > 0)
453 if (cafile.len || cadir.len)
454 if (!ssl_ca(ctx,cafile.s,cadir.s,(int) verifydepth)) temp_tlsca();
455
456 if (ciphers.len)
458
459/* Prepare for Certificate Request */
460
461 if (flagtlsdomain == 2)
462 switch (tls_certkey(ctx,certfile.s,keyfile.s,keypwd.s)) {
463 case 0: break;
464 case -1: temp_tlscert();
465 case -2: temp_tlskey();
466 case -3: temp_tlschk();
467 }
468
469/* Set SSL Context */
470
472 if (!ssl) temp_tlsctx();
473
474/* Setup SSL FDs */
475
476 if (!tls_conn(ssl,smtpfd)) { temp_tlscon(); }
477
478/* Go on in none-blocking mode */
479
481 temp_tlserr();
482}
483
485{
486 int n = 0;
487
488 if (smtptext.len <= 12) return 0;
489 while (n < smtptext.len - 12) {
490 if (case_startb(smtptext.s + n + 4,8,"STARTTLS")) return 1;
491 n += str_chr(smtptext.s + n,'\n') + 1; // next line
492 }
493
494 return 0;
495}
496
498{
499 X509 *cert;
500 STACK_OF(X509) *certs;
501 int ncerts = 0;
502
503 cert = SSL_get_peer_certificate(ssl);
504 if (!cert) { flagtls = 100; return; }
505
506 if ((certs = SSL_get_peer_cert_chain(ssl)) == NULL) {
507 certs = sk_X509_new_null();
508 ncerts++;
509 sk_X509_push(certs,cert);
510 }
511
512 if (flagverify == -2) { // fingerprinting is silent
513 if (cafile.len) case_lowerb(cafile.s,cafile.len);
514 switch (tls_fingerprint(cert,cafile.s + 1,cafile.len - 2)) {
515 case -1: temp_tlspeercert();
516 case -2: temp_tlsdigest();
517 case -3: temp_invaliddigest();
518 case 0: temp_tlscertfp();
519 case 1: flagtls = 104; break;
520 }
521 }
522
523 if (flagverify >= 0) { // TLSA is default
524 switch (tlsa_check(certs,remotehost,port)) {
525 case -4: temp_tlsamissing(); break; /* FIXME */
526 case -3: temp_tlsainvalid(); break;
527 case -2: break; // unsupported type; may happen
528 case -1: break; // weird TLSA record
529 case 0: break; // no TLSA record given
530 case 1: case 2: flagtls = 107; flagverify = 3; break; // full certchain available (-PKIX)
531 case 3: flagtls = 106; flagverify = 0; break; // TA-CA; verify wont work
532 case 4: flagtls = 105; flagverify = 0; break; // Endpoint only
533 }
534 }
535
536 if (flagverify > 0) {
538 case -1: temp_tlspeercert();
539 case -2: temp_tlspeerverify();
540 case -3: temp_tlspeervalid();
541 case 1: flagtls = 101; break;
542 case 2: flagtls = 102; break;
543 case 3: flagtls = 103; break;
544 }
545 }
546
547 if (flagtls < 100) flagtls = 100;
548
549 if (ncerts) sk_X509_free(certs);
550 X509_free(cert);
551
552 return;
553}
554
555/* this file is too long --------------------------------------- smtp UTF8 */
556
557int utf8string(char *ch,int len)
558{
559 int i = 0;
560 while (i < len)
561 if ((unsigned char) ch[i++] > 127) return 1;
562 return 0;
563}
564
566{
567 int r;
568 int i;
569 int received = 0;
570 char ch;
571 stralloc receivedline = {0};
572
573/* we consider only our own last written header */
574
575 for (;;) {
576 r = buffer_get(&bi,&ch,1);
577 if (r == 0) break;
578 if (r == -1) temp_read();
579 if (ch == '\r') continue; // DKIM
580
581 if (ch == '\n') {
582 if (!stralloc_append(&header,"\r")) temp_nomem(); /* received.c does not add '\r' */
583 if (!stralloc_append(&header,"\n")) temp_nomem();
584 if (case_starts(receivedline.s,"Date:")) return 0; /* header to quit asap */
585 if (case_starts(receivedline.s,"Received: from")) received++; /* found Received header */
586 if (received) {
587 if (case_starts(receivedline.s," by ")) {
588 for (i = 6; i < receivedline.len - 6; ++i)
589 if (*(receivedline.s + i) == ' ')
590 if (case_starts(receivedline.s + i + 1,"with UTF8")) return 1;
591 return 0;
592 }
593 }
594 if (!stralloc_copys(&receivedline,"")) temp_nomem();
595 } else {
596 if (!stralloc_append(&header,&ch)) temp_nomem();
597 if (!stralloc_catb(&receivedline,&ch,1)) temp_nomem();
598 }
599 }
600 return 0;
601}
602
603/* this file is too long -------------------------------------- smtp client */
604
605unsigned long code;
606int flagsize = 0;
607
609{
610 int n = 0;
611
612 if (smtptext.len <= 8) return 0;
613 while (n < smtptext.len - 8) {
614 if (case_startb(smtptext.s + n + 4,4,"SIZE")) return 1;
615 n += str_chr(smtptext.s + n,'\n') + 1; // next line
616 }
617
618 return 0;
619}
620
622{
623 buffer_puts(&bo,"EHLO ");
624 buffer_put(&bo,helohost.s,helohost.len);
625 buffer_puts(&bo,"\r\n");
626 buffer_flush(&bo);
627
628 if (smtpcode() != 250) {
629 buffer_puts(&bo,"HELO ");
630 buffer_put(&bo,helohost.s,helohost.len);
631 buffer_puts(&bo,"\r\n");
632 buffer_flush(&bo);
633
634 code = smtpcode();
635 authsender = 0;
636 if (code >= 500) quit("DConnected to "," but my name was rejected");
637 if (code != 250) quit("ZConnected to "," but my name was rejected");
638 }
640}
641
643{
644 buffer_puts(&bo,"STARTTLS\r\n");
645 buffer_flush(&bo);
646
647 if (smtpcode() == 220) {
648 tls_init();
651 }
652 else {
653 flagtls = -2;
654 quit("ZConnected to "," but STARTTLS was rejected");
655 }
656}
657
659{
660 buffer_puts(&bo,"MAIL FROM:<");
661 buffer_put(&bo,sender.s,sender.len);
662 buffer_puts(&bo,">");
663 if (flagutf8 || utf8received())
664 buffer_puts(&bo," SMTPUTF8");
665 if (flagsize && msize) {
666 buffer_puts(&bo," SIZE=");
667 buffer_puts(&bo,msgsize);
668 }
669 buffer_puts(&bo,"\r\n");
670 buffer_flush(&bo);
671}
672
673/* this file is too long -------------------------------------- client auth */
674
675stralloc authsenders = {0};
677
678stralloc user = {0};
679stralloc pass = {0};
680stralloc auth = {0};
681stralloc chal = {0};
682stralloc slop = {0};
683stralloc plain = {0};
684stralloc xuser = {0};
685
686static const char hextab[] = "0123456789abcdef";
687
688int xtext(stralloc *sa,char *s,int len)
689{
690 int i;
691 unsigned char c;
692 char xch[2];
693
694 if (!stralloc_copys(sa,"")) temp_nomem();
695
696 for (i = 0; i < len; i++) {
697 c = s[i];
698 if (c < 33 || c > 126 || c == '=' || c == '+') {
699 xch[0] = hextab[(c >> 4) & 0x0f];
700 xch[1] = hextab[c & 0x0f];
701 if (!stralloc_catb(sa,xch,2)) temp_nomem();
702 } else
703 if (!stralloc_catb(sa,s + i,1)) temp_nomem();
704 }
705
706 return sa->len;
707}
708
710{
711 if (!xtext(&xuser,user.s,user.len)) temp_nomem();
712 buffer_puts(&bo,"MAIL FROM:<");
713 buffer_put(&bo,sender.s,sender.len);
714 buffer_puts(&bo,"> AUTH=");
715 buffer_put(&bo,xuser.s,xuser.len);
716 if (flagutf8 || utf8received())
717 buffer_puts(&bo," SMTPUTF8");
718 if (flagsize && msize) {
719 buffer_puts(&bo," SIZE=");
720 buffer_puts(&bo,msgsize);
721 }
722 buffer_puts(&bo,"\r\n");
723 buffer_flush(&bo);
724}
725
727{
728 buffer_puts(&bo,"AUTH PLAIN\r\n");
729 buffer_flush(&bo);
730
731 if (smtpcode() != 334) quit("ZConnected to "," but authentication was rejected (AUTH PLAIN)");
732
733 if (!stralloc_cats(&plain,"")) temp_nomem(); /* RFC 4616 section 2 */
734 if (!stralloc_0(&plain)) temp_nomem();
735 if (!stralloc_cat(&plain,&user)) temp_nomem(); /* user-id */
736 if (!stralloc_0(&plain)) temp_nomem();
737 if (!stralloc_cat(&plain,&pass)) temp_nomem(); /* password */
738 if (b64encode(&plain,&auth)) quit("ZConnected to "," but unable to base64encode (plain)");
739 buffer_put(&bo,auth.s,auth.len);
740 buffer_puts(&bo,"\r\n");
741 buffer_flush(&bo);
742
743 switch (smtpcode()) {
744 case 235: mailfrom_xtext(); break;
745 case 432: quit("DConnected to "," but password expired");
746 case 534: quit("ZConnected to "," but authentication mechamism too weak (plain)");
747 default: quit("ZConnected to "," but authentication was rejected (plain)");
748 }
749 return 0;
750}
751
753{
754 buffer_puts(&bo,"AUTH LOGIN\r\n");
755 buffer_flush(&bo);
756
757 if (smtpcode() != 334) quit("ZConnected to "," but authentication was rejected (AUTH LOGIN)");
758 if (!stralloc_copys(&auth,"")) temp_nomem();
759 if (b64encode(&user,&auth)) quit("ZConnected to "," but unable to base64encode user");
760
761 buffer_put(&bo,auth.s,auth.len);
762 buffer_puts(&bo,"\r\n");
763 buffer_flush(&bo);
764
765 if (smtpcode() != 334) quit("ZConnected to "," but authentication was rejected (username)");
766
767 if (!stralloc_copys(&auth,"")) temp_nomem();
768 if (b64encode(&pass,&auth)) quit("ZConnected to "," but unable to base64encode pass");
769 buffer_put(&bo,auth.s,auth.len);
770 buffer_puts(&bo,"\r\n");
771 buffer_flush(&bo);
772
773 switch (smtpcode()) {
774 case 235: mailfrom_xtext(); break;
775 case 432: quit("DConnected to "," but password expired");
776 case 534: quit("ZConnected to "," but authentication mechanism is too weak (login)");
777 default: quit("ZConnected to "," but authentication was rejected (login)");
778 }
779 return 0;
780}
781
783{
784 int j;
785 char digest[16]; /* signed is ok */
786 char digascii[33];
787
788 buffer_puts(&bo,"AUTH CRAM-MD5\r\n");
789 buffer_flush(&bo);
790
791 if (smtpcode() != 334) quit("ZConnected to "," but authentication was rejected (AUTH CRAM-MD5)");
792 if (str_chr(smtptext.s + 4,' ')) { /* Challenge */
793 if (!stralloc_copys(&slop,"")) temp_nomem();
794 if (!stralloc_copyb(&slop,smtptext.s + 4,smtptext.len - 5)) temp_nomem();
795 if (b64decode((unsigned char *) slop.s,slop.len,&chal))
796 quit("ZConnected to "," but unable to base64decode challenge");
797 }
798
799 hmac_md5(chal.s,chal.len,pass.s,pass.len,digest);
800
801 for (j = 0; j < 16; j++) { /* HEX => ASCII */
802 digascii[2 * j] = hextab[(unsigned char) digest[j] >> 4];
803 digascii[2 * j + 1] = hextab[(unsigned char) digest[j] & 0x0f];
804 }
805 digascii[32] = 0;
806
807 if (!stralloc_copys(&slop,"")) temp_nomem();
808 if (!stralloc_cat(&slop,&user)) temp_nomem(); /* user-id */
809 if (!stralloc_cats(&slop," ")) temp_nomem();
810 if (!stralloc_catb(&slop,digascii,32)) temp_nomem(); /* digest */
811
812 if (!stralloc_copys(&auth,"")) temp_nomem();
813 if (b64encode(&slop,&auth)) quit("ZConnected to "," but unable to base64encode username+digest");
814
815 buffer_put(&bo,auth.s,auth.len);
816 buffer_puts(&bo,"\r\n");
817 buffer_flush(&bo);
818
819 switch (smtpcode()) {
820 case 235: mailfrom_xtext(); break;
821 case 432: quit("DConnected to "," but password expired");
822 case 534: quit("ZConnected to "," but authentication mechamism too weak (cram)");
823 default: quit("ZConnected to "," but authentication was rejected (cram)");
824 }
825 return 0;
826}
827
829{
830 int n = 0;
831
832 if (smtptext.len <= 12) return;
833 while (n < smtptext.len - 12) {
834 if (case_startb(smtptext.s + n + 4,4,"AUTH")) {
835 for (int i = n + 4; i < smtptext.len - 5; ++i) {
836 if (case_startb(smtptext.s + i,4,"CRAM"))
837 if (mailfrom_cram() >= 0) return;
838 if (case_startb(smtptext.s + i,5,"LOGIN"))
839 if (mailfrom_login() >= 0) return;
840 if (case_startb(smtptext.s + i,5,"PLAIN"))
841 if (mailfrom_plain() >= 0) return;
842 }
843 }
844 n += str_chr(smtptext.s + n,'\n') + 1; // next line
845 }
846
847 err_authprot();
848 mailfrom();
849}
850
851/* this file is too long ------------------------------------------- GO ON */
852
853void smtp()
854{
855 int flagbother;
856 int i;
857
858 if (flagtls > 10 && flagtls < 20) { /* SMTPS */
859 tls_init();
860 tls_peercheck();
861 }
862
863 code = smtpcode();
864 if (code >= 500) quit("DConnected to "," but sender was rejected");
865 if (code == 421 || code == 450) quit("ZConnected to "," but probably greylisted"); /* RFC 6647 */
866 if (code >= 400) quit("ZConnected to "," but sender was rejected");
867 if (code != 220) quit("ZConnected to "," but greeting failed");
868
870
871 if (flagtls > 0 && flagtls < 10) { /* STARTTLS */
872 if (ehlo_starttls())
874 else if (flagtls > 3) {
875 if (!stralloc_0(&host)) temp_nomem();
876 temp_tlshost();
877 }
878 }
879 if (user.len && pass.len) /* AUTH */
880 ehlo_auth();
881 else
882 mailfrom(); /* Mail From */
883
884 code = smtpcode();
885 if (code >= 500) quit("DConnected to "," but sender was rejected");
886 if (code >= 400) quit("ZConnected to "," but sender was probably greylisted");
887
888 flagbother = 0; /* Rcpt To */
889 for (i = 0; i < reciplist.len; ++i) {
890 buffer_puts(&bo,"RCPT TO:<");
891 buffer_put(&bo,reciplist.sa[i].s,reciplist.sa[i].len);
892 buffer_puts(&bo,">\r\n");
893 buffer_flush(&bo);
894
895 code = smtpcode(); /* Data */
896 if (flagsize) {
897 if (code == 552) quit("DConnected to "," but message size is too large");
898 if (code == 452) quit("ZConnected to "," however insufficient storage space available");
899 }
900 if (code == 421 || code == 450) { // Postfix merde ;-)
901 out("s"); outhost(); out(" sender is greylisting.\n");
902 outsmtptext(); zero();
903 } else if (code >= 500) {
904 out("h"); outhost(); out(" does not like recipient.\n");
905 outsmtptext(); zero();
906 } else if (code >= 400) {
907 out("s"); outhost(); out(" does not like recipient.\n");
908 outsmtptext(); zero();
909 } else {
910 out("r"); zero();
911 flagbother = 1;
912 }
913 }
914 if (!flagbother) quit("DGiving up on ","");
915
916 buffer_putsflush(&bo,"DATA\r\n");
917
918 code = smtpcode();
919 if (code >= 500) quit("D"," failed on DATA command");
920 if (code >= 400) quit("Z"," failed on DATA command");
921
922 buffer_putflush(&bo,header.s,header.len);
923
924 blast();
925 code = smtpcode();
926 flagcritical = 0;
927 if (code >= 500) quit("D"," failed after I sent the message");
928 if (code >= 400) quit("Z"," failed after I sent the message");
929 switch (flagtls) { // StartTLS + SMTPS
930 case 100: case 110: quit("K"," TLS transmitted message accepted"); break;
931 case 101: case 111: quit("K"," TLS (verified CA) transmitted message accepted"); break;
932 case 102: case 112: quit("K"," TLS (validated CA+DN*) transmitted message accepted"); break;
933 case 103: case 113: quit("K"," TLS (validated CA+DN) transmitted message accepted"); break;
934 case 104: case 114: quit("K"," TLS (CERT pinning) transmitted message accepted"); break;
935 case 105: case 115: quit("K"," TLS (TLSA EE validated) transmitted message accepted"); break;
936 case 106: case 116: quit("K"," TLS (TLSA TA validated) transmitted message accepted"); break;
937 case 107: case 117: quit("K"," TLS (TLSA PKIX verified) transmitted message accepted"); break;
938 default: quit("K"," accepted message"); break;
939 }
940}
941
942/* this file is too long -------------------------------------- qmtp client */
943
944int qmtpsend = 0;
945
946void qmtp()
947{
948 unsigned long len = 0;
949 char *x;
950 int i = 0;
951 int n = 0;
952 char ch;
953 char num[FMT_ULONG];
954 int flagallok;
955
956 if (qmtpsend == 2) { /* QMTPS */
957 tls_init();
959 }
960
961/* the following code was substantially taken from serialmail's serialqmtp.c */
962
963 if (msize)
964 len = msize;
965 else {
966 out("DMessage for: "); outhost(); out(" has zero bytes. Giving up.\n");
967 }
968
969 buffer_put(&bo,num,fmt_ulong(num,len + 1));
970 buffer_put(&bo,":\n",2);
971 while (len > 0) {
972 n = buffer_feed(&bi);
973 if (n <= 0) _exit(1); /* wise guy again */
974 x = buffer_PEEK(&bi);
975 buffer_put(&bo,x,n);
976 buffer_SEEK(&bi,n);
977 len -= n;
978 }
979 buffer_put(&bo,",",1);
980
981 len = sender.len;
982 buffer_put(&bo,num,fmt_ulong(num,len));
983 buffer_put(&bo,":",1);
984 buffer_put(&bo,sender.s,sender.len);
985 buffer_put(&bo,",",1);
986
987 len = 0;
988 for (i = 0; i < reciplist.len; ++i)
989 len += fmt_ulong(num,reciplist.sa[i].len) + 1 + reciplist.sa[i].len + 1;
990 buffer_put(&bo,num,fmt_ulong(num,len));
991 buffer_put(&bo,":",1);
992 for (i = 0; i < reciplist.len; ++i) {
993 buffer_put(&bo,num,fmt_ulong(num,reciplist.sa[i].len));
994 buffer_put(&bo,":",1);
995 buffer_put(&bo,reciplist.sa[i].s,reciplist.sa[i].len);
996 buffer_put(&bo,",",1);
997 }
998 buffer_put(&bo,",",1);
999 buffer_flush(&bo);
1000
1001 flagallok = 1;
1002
1003 for (i = 0; i < reciplist.len; ++i) {
1004 len = 0;
1005 for (;;) {
1006 get(&ch);
1007 if (ch == ':') break;
1008 if (len > QMTP_MAX) temp_proto();
1009 if (ch - '0' > 9) temp_proto();
1010 len = 10 * len + (ch - '0');
1011 }
1012 if (!len) temp_proto();
1013 get(&ch); --len;
1014 if ((ch != 'Z') && (ch != 'D') && (ch != 'K')) temp_proto();
1015
1016 if (!stralloc_copyb(&smtptext,&ch,1)) temp_nomem();
1017 if (flagtls >= 100) {
1018 if (!stralloc_cats(&smtptext,"qmtps:")) temp_nomem();
1019 } else {
1020 if (!stralloc_cats(&smtptext,"qmtp:")) temp_nomem();
1021 }
1022
1023 while (len > 0) {
1024 get(&ch);
1025 --len;
1026 }
1027
1028 for (len = 0; len < smtptext.len; ++len) {
1029 ch = smtptext.s[len];
1030 if ((ch < 32) || (ch > 126)) smtptext.s[len] = '?';
1031 }
1032 get(&ch); // Read response
1033 if (ch != ',') temp_proto();
1034 smtptext.s[smtptext.len - 1] = '\n';
1035
1036 if (smtptext.s[0] == 'K') out("r");
1037 else if (smtptext.s[0] == 'D') {
1038 out("h");
1039 flagallok = 0;
1040 }
1041 else { /* if (smtptext.s[0] == 'Z') */
1042 out("s");
1043 flagallok = 0;
1044 }
1045 if (buffer_put(&bs,smtptext.s + 1,smtptext.len - 1) == -1) temp_qmtpnoc();
1046 zero();
1047 }
1048
1049 if (!flagallok) {
1050 out("DGiving up on "); outhost(); out("\n");
1051 } else {
1052 out("KAll received okay by "); outhost(); out("\n");
1053 }
1054 zerodie();
1055}
1056
1057/* this file is too long -------------------------------------- common */
1058
1059/* host has to be canonical [A/AAAA record], box has to be quoted */
1060
1061void addrmangle(stralloc *saout,char *address,int *flagalias,int flagcname)
1062{
1063 int at;
1064 int r = 0;
1065 stralloc cn = {0};
1066
1067 *flagalias = flagcname; /* saout + flagalias are output */
1068 if (!flagutf8)
1069 flagutf8 = utf8string(address,str_len(address));
1070
1071 at = str_rchr(address,'@');
1072 if (!address[at]) {
1073 if (!stralloc_copys(saout,address)) temp_nomem();
1074 return;
1075 }
1076
1078 canonbox.len = at;
1079 if (!quote(saout,&canonbox)) temp_nomem(); /* saout = 'inbox' name without quotes ;-) */
1080 if (!stralloc_cats(saout,"@")) temp_nomem();
1081
1082 if (!stralloc_copys(&canonhost,address + at + 1)) temp_nomem();
1083 if (flagcname) { /* no relayhost */
1084 DNS_INIT
1085 switch ((r = dns_cname(&cn,&canonhost))) {
1086 case DNS_MEM: temp_nomem();
1087 case DNS_SOFT: temp_dnscanon();
1088 case DNS_HARD: ; /* alias loop, not our problem */
1089 default: if (r > 0) *flagalias = 0;
1090 }
1091 }
1092 if (!stralloc_cat(saout,&canonhost)) temp_nomem();
1093}
1094
1096{
1097 if (control_init() == -1) temp_control();
1098 if (control_readint(&timeout,"control/timeoutremote") == -1) temp_control();
1099 if (control_readint(&timeoutconnect,"control/timeoutconnect") == -1)
1100 temp_control();
1101 if (control_rldef(&helohost,"control/helohost",1,(char *) 0) != 1)
1102 temp_control();
1103 switch (control_readfile(&smtproutes,"control/smtproutes",0)) {
1104 case -1: temp_control();
1105 case 0: if (!constmap_init(&mapsmtproutes,"",0,1)) temp_nomem(); break;
1106 case 1: if (!constmap_init(&mapsmtproutes,smtproutes.s,smtproutes.len,1)) temp_nomem(); break;
1107 }
1108 switch (control_readfile(&domainips,"control/domainips",0)) {
1109 case -1: temp_control();
1110 case 0: if (!constmap_init(&mapdomainips,"",0,1)) temp_nomem(); break;
1111 case 1: if (!constmap_init(&mapdomainips,domainips.s,domainips.len,1)) temp_nomem(); break;
1112 }
1113 switch (control_readfile(&authsenders,"control/authsenders",0)) {
1114 case -1: temp_control();
1115 case 0: if (!constmap_init(&mapauthsenders,"",0,1)) temp_nomem(); break;
1116 case 1: if (!constmap_init(&mapauthsenders,authsenders.s,authsenders.len,1)) temp_nomem(); break;
1117 }
1118 switch (control_readfile(&qmtproutes,"control/qmtproutes",0)) {
1119 case -1: temp_control();
1120 case 0: if (!constmap_init(&mapqmtproutes,"",0,1)) temp_nomem(); break;
1121 case 1: if (!constmap_init(&mapqmtproutes,qmtproutes.s,qmtproutes.len,1)) temp_nomem(); break;
1122 }
1123 switch (control_readfile(&domaincerts,"control/domaincerts",0)) {
1124 case -1: temp_control();
1125 case 0: if (!constmap_init(&mapdomaincerts,"",0,1)) temp_nomem(); break;
1126 case 1: if (!constmap_init(&mapdomaincerts,domaincerts.s,domaincerts.len,1)) temp_nomem(); break;
1127 }
1128 switch (control_readfile(&tlsdestinations,"control/tlsdestinations",0)) {
1129 case -1: temp_control();
1130 case 0: if (!constmap_init(&maptlsdestinations,"",0,1)) temp_nomem(); break;
1132 }
1133}
1134
1135int main(int argc,char * const argv[])
1136{
1137 static ipalloc ip = {0};
1138 stralloc netif = {0};
1139 struct stat st;
1140 int i, j, k;
1141 int p; /* reserved for port */
1142 int r; /* reserved for return code */
1143 unsigned long random;
1144 char **recips;
1145 unsigned long prefme;
1146 int flagallaliases;
1147 int flagalias;
1148 char *relayhost;
1149 char *localip;
1150 int ip6flag = 0;
1151
1152 sig_pipeignore();
1153 if (argc < 4) perm_usage();
1154 if (chdir(auto_qmail) == -1) temp_chdir();
1155
1156 getcontrols();
1157 if (!stralloc_copys(&host,argv[1])) temp_nomem();
1158
1159 authsender = 0;
1160 relayhost = 0;
1161
1162 addrmangle(&sender,argv[2],&flagalias,0);
1163
1164 if (sender.len > 1) {
1165 i = str_chr(sender.s,'@');
1166 if (sender.s[i] == '@')
1167 if (!stralloc_copyb(&senddomain,sender.s + i + 1,sender.len - i - 1)) temp_nomem(); // un-terminated
1168 }
1169
1170/* this file is too long -------------------------------------- set domain ip + helohost */
1171
1172 localip = 0;
1173
1174 for (i = 0; i <= senddomain.len; ++i)
1175 if ((i == 0) || (senddomain.s[i] == '.'))
1176 if ((localip = constmap(&mapdomainips,senddomain.s + i,senddomain.len - i)))
1177 break;
1178
1179 if (!localip)
1180 localip = constmap(&mapdomainips,"*",1); /* one for all */
1181
1182 if (localip) {
1183 j = str_chr(localip,'%');
1184 if (localip[j] != '%') j = 0;
1185 k = str_chr(localip,'|');
1186 if (localip[k] != '|') k = 0;
1187 if (k) { /* helohost */
1188 if (!stralloc_copys(&helohost,localip + k + 1)) temp_nomem();
1189 localip[k] = 0;
1190 }
1191 if (j) { /* IF index */
1192 localip[j] = 0;
1193 if (!stralloc_copys(&netif,localip + j + 1)) temp_nomem();
1194 if (!stralloc_0(&netif)) temp_nomem();
1195 }
1196 }
1197
1198/* this file is too long -------------------------------------- authsender routes */
1199
1200 for (i = 0; i <= sender.len; ++i)
1201 if ((i == 0) || (i == sender.len) || (sender.s[i] == '.') || (sender.s[i] == '@'))
1202 if ((authsender = constmap(&mapauthsenders,sender.s + i,sender.len - i)))
1203 break;
1204
1205 if (authsender && !*authsender) authsender = 0;
1206
1207 if (authsender) {
1208 i = str_chr(authsender,'|');
1209 if (authsender[i] == '|') {
1210 j = str_chr(authsender + i + 1,'|');
1211 if (authsender[i + j + 1] == '|') {
1212 authsender[i] = 0;
1213 authsender[i + j + 1] = 0;
1214 if (!stralloc_copys(&user,"")) temp_nomem();
1215 if (!stralloc_copys(&user,authsender + i + 1)) temp_nomem();
1216 if (!stralloc_copys(&pass,"")) temp_nomem();
1217 if (!stralloc_copys(&pass,authsender + i + j + 2)) temp_nomem();
1218 }
1219 }
1220 p = str_chr(authsender,';');
1221 if (authsender[p] == ';') {
1222 if (authsender[p + 1] == 's') { flagsmtps = 1, p++; }
1223 scan_ulong(authsender + p + 1,&port);
1224 authsender[p] = 0;
1225 }
1226 relayhost = authsender;
1228 }
1229
1230/* this file is too long -------------------------------------- standard routes */
1231
1232 if (!authsender) {
1233 if (sender.len == 0) { /* bounce routes */
1234 if ((relayhost = constmap(&mapqmtproutes,"!@",2))) {
1235 qmtpsend = 1; port = PORT_QMTP;
1236 } else
1237 relayhost = constmap(&mapsmtproutes,"!@",2);
1238 }
1239
1240 if (relayhost && !*relayhost) relayhost = 0;
1241
1242 if (!relayhost) {
1243 for (i = 0; i <= host.len; ++i) { /* qmtproutes */
1244 if ((i == 0) || (i == host.len) || (host.s[i] == '.'))
1245 if ((relayhost = constmap(&mapqmtproutes,host.s + i,host.len - i))) {
1246 qmtpsend = 1; port = PORT_QMTP;
1247 break;
1248 } /* default smtproutes */
1249 if ((relayhost = constmap(&mapsmtproutes,host.s + i,host.len - i)))
1250 break;
1251 }
1252 }
1253 if (relayhost && !*relayhost) relayhost = 0;
1254
1255 if (relayhost) { /* default smtproutes -- authenticated */
1256 i = str_chr(relayhost,'|');
1257 if (relayhost[i] == '|') {
1258 j = str_chr(relayhost + i + 1,'|'); // authenticate
1259 if (relayhost[i + j + 1] == '|') {
1260 relayhost[i] = 0;
1261 relayhost[i + j + 1] = 0;
1262 if (!stralloc_copys(&user,"")) temp_nomem();
1263 if (!stralloc_copys(&user,relayhost + i + 1)) temp_nomem();
1264 if (!stralloc_copys(&pass,"")) temp_nomem();
1265 k = str_chr(relayhost + i + j + 2,'|'); // local ip
1266 if (relayhost[i + j + k + 2] == '|') {
1267 relayhost[i + j + k + 2] = 0;
1268 localip = relayhost + i + j + k + 3;
1269 }
1270 if (!stralloc_copys(&pass,relayhost + i + j + 2)) temp_nomem();
1271 }
1272 }
1273 p = str_chr(relayhost,';');
1274 if (relayhost[p] == ';') {
1275 if (relayhost[p + 1] == 's') { flagsmtps = 1; p++; } // RFC 8314
1276 scan_ulong(relayhost + p + 1,&port);
1277 if (qmtpsend && port == PORT_QMTPS) qmtpsend = 2;
1278 relayhost[p] = 0;
1279 }
1280 if (!stralloc_copys(&host,relayhost)) temp_nomem();
1281#ifdef IDN2
1282 } else {
1283 char *asciihost = 0;
1284 if (!stralloc_0(&host)) temp_nomem();
1285 switch (idn2_lookup_u8(host.s,(uint8_t**)&asciihost,IDN2_NFC_INPUT)) {
1286 case IDN2_OK: break;
1287 case IDN2_MALLOC: temp_nomem();
1288 default: perm_dns();
1289 }
1290 if (!stralloc_copys(&idnhost,asciihost)) temp_nomem();
1291#endif
1292 }
1293 }
1294
1295/* this file is too long -------------------------------------- TLS destinations */
1296
1297
1298 flagtls = tls_destination((const stralloc) host); // host may not be 0-terminated
1299
1300 if (flagtls > 0) {
1301 if (tlsdestinfo) {
1302 i = str_chr(tlsdestinfo,'|'); /* ca file/dir or cert fingerprint */
1303 if (tlsdestinfo[i] == '|') {
1304 tlsdestinfo[i] = 0;
1305 j = str_chr(tlsdestinfo + i + 1,'|'); /* cipher */
1306 if (tlsdestinfo[i + j + 1] == '|') {
1307 tlsdestinfo[i + j + 1] = 0;
1308 k = str_chr(tlsdestinfo + i + j + 2,'|'); /* cone domain */
1309 if (tlsdestinfo[i + j + k + 2] == '|') {
1310 tlsdestinfo[i + j + k + 2] = 0;
1311 if (str_diffn(tlsdestinfo + j + k + 3,canonhost.s,canonhost.len)) flagtls = 0;
1312 }
1313 p = str_chr(tlsdestinfo + i + j + 2,';'); /* verifydepth;port */
1314 if (tlsdestinfo[i + j + p + 2] == ';') {
1315 tlsdestinfo[i + j + p + 2] = 0;
1316 if (p > 0) scan_ulong(tlsdestinfo + i + j + 2,&verifydepth);
1317 if (tlsdestinfo[i + j + p + 3] == 's') { flagsmtps = 1; p++; } /* RFC 8314 */
1318 scan_ulong(tlsdestinfo + i + j + p + 3,&port);
1319 }
1320 }
1321 if (j)
1322 if (!stralloc_copys(&ciphers,tlsdestinfo + i + 1)) temp_nomem();
1323 }
1324
1325 /* either ':[=]cafile/cadir' -or- ':;port' */
1326
1327 if (tlsdestinfo[0] == ';')
1328 scan_ulong(tlsdestinfo + 1,&port);
1329 else
1331 }
1332
1333/* cafile starts with '=' => it is a fingerprint
1334 cafile ends with '/' => consider it as cadir
1335 cafile and cadir are now 0-terminated
1336 ciphers are alway 0-terminated if given */
1337
1338 if (cafile.len > 2) {
1339 flagverify = 1;
1340 if (cafile.s[cafile.len] == '/') {
1341 cafile.len = 0;
1342 flagverify = 2;
1344 if (!stralloc_0(&cadir)) temp_nomem();
1345 } else {
1346 if (cafile.s[0] == '=') flagverify = -2;
1347 if (!stralloc_0(&cafile)) temp_nomem();
1348 }
1349 } else
1350 cafile.len = cadir.len = 0;
1351
1352 if (ciphers.len > 4) /* otherwise garbage */
1353 if (!stralloc_0(&ciphers)) temp_nomem();
1354 else
1355 ciphers.len = 0;
1356
1357 if (port == PORT_SMTPS || flagsmtps) flagtls += 10;
1358 if (port == PORT_QMTPS) flagtls += 20;
1359 }
1360
1361 if (flagtls == 8 || flagtls == 18 || flagtls == 28) flagverify = -1; /* no TLSA */
1362 if (!flagtls && qmtpsend == 2) flagtls = 20; /* QMTPS */
1363
1364
1365/* this file is too long -------------------------------------- Our Certs - per senddomain */
1366
1367 if (flagtls > 0) {
1368 flagtlsdomain = tls_domaincerts((const stralloc) senddomain); // senddomain un-terminated
1369
1371 i = str_chr(tlsdomaininfo,'|');
1372 if (tlsdomaininfo[i] == '|') {
1373 tlsdomaininfo[i] = 0;
1374 j = str_chr(tlsdomaininfo + i + 1,'|');
1375 if (tlsdomaininfo[i + j + 1] == '|') {
1376 tlsdomaininfo[i + j + 1] = 0;
1377 if (!stralloc_copys(&keypwd,"")) temp_nomem();
1378 if (!stralloc_copys(&keypwd,tlsdomaininfo + i + j + 2)) temp_nomem();
1379 if (!stralloc_0(&keypwd)) temp_nomem();
1380 }
1382 if (!stralloc_0(&keyfile)) temp_nomem();
1383 }
1385 if (!stralloc_0(&certfile)) temp_nomem();
1386 flagtlsdomain = 2;
1387 }
1388 }
1389
1390/* this file is too long -------------------------------------- work thru reciplist */
1391
1392 if (!saa_readyplus(&reciplist,0)) temp_nomem();
1393 if (ipme_init() != 1) temp_oserr();
1394
1395 flagallaliases = 1;
1396 recips = argv + 3;
1397
1398 if (fstat(0,&st) == -1) quit("Z", " unable to fstat stdin");
1399 msize = st.st_size;
1400 fmt_ulong(msgsize,msize);
1401
1402 while (*recips) {
1403 if (!saa_readyplus(&reciplist,1)) temp_nomem();
1404 reciplist.sa[reciplist.len] = sauninit;
1405 addrmangle(reciplist.sa + reciplist.len,*recips,&flagalias,!relayhost);
1406 if (!flagalias) flagallaliases = 0;
1407 ++reciplist.len;
1408 ++recips;
1409 }
1410
1411 random = now() + (getpid() << 16);
1412#ifdef IDN2
1413 switch (relayhost ? dns_ip(&ip,&host) : dns_mxip(&ip,&idnhost,random)) {
1414#else
1415 switch (relayhost ? dns_ip(&ip,&host) : dns_mxip(&ip,&host,random)) {
1416#endif
1417 case DNS_MEM: temp_nomem();
1418 case DNS_ERR: temp_dns();
1419 case DNS_COM: temp_dns();
1420 case DNS_SOFT: temp_dns();
1421#ifdef DEFERREDBOUNCES
1422 default: if (!ip.len) temp_nomx();
1423#else
1424 default: if (!ip.len) perm_nomx();
1425#endif
1426 }
1427
1428 prefme = 100000;
1429 for (i = 0; i < ip.len; ++i)
1430 if (ipme_is(&ip.ix[i])) {
1431#ifdef NULLMX
1432 if (ip.ix[i].pref == 0 && ip.ix[i].mxh[0] == '.')
1433 perm_nullmx(); /* RFC 7505 MX = 0 */
1434#endif
1435 if (ip.ix[i].pref < prefme)
1436 prefme = ip.ix[i].pref;
1437 }
1438
1439 if (relayhost) prefme = 300000;
1440 if (flagallaliases) prefme = 500000;
1441
1442 if (localip) {
1443 i = str_chr(localip,':');
1444 if (localip[i] == ':') ip6flag = 1;
1445 else ip6flag = -1;
1446 }
1447
1448 for (i = 0; i < ip.len; ++i) { /* MX with smallest distance */
1449 if (ip6flag == -1 && ip.ix[i].af == AF_INET6) continue;
1450 if (ip6flag == 1 && ip.ix[i].af == AF_INET) continue;
1451 if (ip.ix[i].pref < prefme) break;
1452 }
1453
1454 if (i >= ip.len) perm_ambigmx();
1455
1456 if (!stralloc_copys(&remotehost,ip.ix[i].mxh)) temp_nomem(); /* take MX hostname for TLSA */
1457 if (!stralloc_0(&remotehost)) temp_nomem();
1458
1459 for (i = 0; i < ip.len; ++i) {
1460 if (ip.ix[i].pref < prefme) {
1461 if (ip6flag == -1 && ip.ix[i].af == AF_INET6) continue; /* explicit binding */
1462 if (ip6flag == 1 && ip.ix[i].af == AF_INET) continue;
1463 if (tcpto(&ip.ix[i])) continue;
1464
1465 smtpfd = socket(ip.ix[i].af,SOCK_STREAM,0);
1466 if (smtpfd == -1) continue;
1467
1468 if (localip) { /* set domain ip */
1469 if (!stralloc_copyb(&sendip,localip,str_len(localip))) temp_nomem();
1470 j = str_chr(localip,':');
1471 if (localip[j] == ':') {
1472 if (!ip6_scan(localip,ip6)) temp_noip(); /* IPv6 */
1473 if (byte_equal((char *)ip.ix[i].addr.ip6.d,16,ip6)) continue;
1474 ifidx = socket_getifidx(netif.s);
1475 if (socket_bind6(smtpfd,ip6,0,ifidx) < 0) temp_osip();
1476 } else {
1477 if (!ip4_scan(localip,ip4)) temp_noip(); /* IPv4 */
1478 if (byte_equal((char *)ip.ix[i].addr.ip4.d,4,ip4)) continue;
1479 if (socket_bind4(smtpfd,ip4,0) < 0) temp_osip();
1480 }
1481 }
1482
1483
1484 AGAIN:
1485 if (ip.ix[i].af == AF_INET6)
1486 r = timeoutconn6(smtpfd,(char *)&ip.ix[i].addr.ip6.d,(unsigned int) port,timeoutconnect,ifidx);
1487 else
1488 r = timeoutconn4(smtpfd,(char *)&ip.ix[i].addr.ip4.d,(unsigned int) port,timeoutconnect);
1489 if (r == 0) {
1490 tcpto_err(&ip.ix[i],0);
1491 partner = ip.ix[i];
1492 if (qmtpsend)
1493 qmtp();
1494 else
1495 smtp(); /* read qmail/THOUGHTS; section 6 */
1496 }
1497 if (flagtls == 9 && errno == EPROTO) {
1498 flagtls = -2; goto AGAIN;
1499 }
1500 if (errno == ETIMEDOUT || errno == ECONNREFUSED || errno == EPROTO)
1501 tcpto_err(&ip.ix[i],1);
1502 close(smtpfd);
1503 }
1504 }
1505 temp_noconn();
1506}
char auto_qmail[]
int main()
Definition: chkshsgr.c:6
char num[FMT_ULONG]
Definition: chkspawn.c:8
int constmap_init(struct constmap *cm, char *s, int len, int flagcolon)
Definition: constmap.c:35
int control_readint(unsigned long *i, char *fn)
Definition: control.c:72
int control_rldef(stralloc *sa, char *fn, int flagme, char *def)
Definition: control.c:42
int control_readfile(stralloc *sa, char *fn, int flagme)
Definition: control.c:87
int control_init(void)
Definition: control.c:33
int stralloc_copys(stralloc *, char const *)
int dns_mxip(ipalloc *ia, stralloc *sa, unsigned long random)
Definition: dns.c:105
int dns_ip(ipalloc *ia, stralloc *sa)
Definition: dns.c:194
#define DNS_SOFT
Definition: dns.h:14
#define DNS_HARD
Definition: dns.h:15
#define DNS_INIT
Definition: dns.h:12
stralloc out
Definition: dnscname.c:12
stralloc sa
Definition: dnscname.c:11
void _exit(int)
char buf[100+FMT_ULONG]
Definition: hier.c:11
void c(char *, char *, char *, int, int, int)
Definition: install.c:70
void p(char *, char *, int, int, int)
Definition: install.c:52
void hmac_md5(char *text, int text_len, char *key, int key_len, char *digest)
Definition: hmac_md5.c:16
int ipme_init(void)
Definition: ipme.c:61
int ipme_is(struct ip_mx *)
Definition: ipme.c:50
char ipaddr[IPFMT]
Definition: ipmeprint.c:11
stralloc address
Definition: newaliases.c:90
char tmpbuf[BUFSIZE_LINE]
Definition: newinclude.c:38
datetime_sec now()
Definition: now.c:5
GEN_ALLOC_readyplus(prioq, struct prioq_elt, p, len, a, i, n, x, 100, prioq_readyplus)
Definition: prioq.c:5
int fd
buffer in
Definition: qmail-pw2u.c:240
int flagalias
Definition: qmail-pw2u.c:82
char * received
Definition: qmail-queue.c:91
stralloc canonhost
Definition: qmail-remote.c:82
unsigned long code
Definition: qmail-remote.c:605
stralloc idnhost
Definition: qmail-remote.c:80
int utf8string(char *ch, int len)
Definition: qmail-remote.c:557
ssize_t safewrite(int fd, char *buf, size_t len)
Definition: qmail-remote.c:307
char inbuf[BUFFER_MTUSIZE]
Definition: qmail-remote.c:320
buffer bs
Definition: qmail-remote.c:111
void outhost()
Definition: qmail-remote.c:264
void zero()
Definition: qmail-remote.c:118
stralloc recip
Definition: qmail-remote.c:101
int flagcritical
Definition: qmail-remote.c:278
char * authsender
Definition: qmail-remote.c:93
void temp_read()
Definition: qmail-remote.c:180
int flagutf8
Definition: qmail-remote.c:71
struct constmap mapauthsenders
Definition: qmail-remote.c:676
void temp_noip()
Definition: qmail-remote.c:144
stralloc qmtproutes
Definition: qmail-remote.c:97
void temp_dns()
Definition: qmail-remote.c:192
stralloc remotehost
Definition: qmail-remote.c:83
#define QMTP_MAX
Definition: qmail-remote.c:45
struct constmap mapqmtproutes
Definition: qmail-remote.c:98
void temp_chdir()
Definition: qmail-remote.c:206
#define PORT_QMTPS
Definition: qmail-remote.c:51
int xtext(stralloc *sa, char *s, int len)
Definition: qmail-remote.c:688
void temp_qmtpnoc()
Definition: qmail-remote.c:173
int flagverify
Definition: qmail-remote.c:69
stralloc certfile
Definition: qmail-remote.c:430
char frombuf[BUFFER_SMALL]
Definition: qmail-remote.c:324
void perm_usage()
Definition: qmail-remote.c:226
void addrmangle(stralloc *saout, char *address, int *flagalias, int flagcname)
int mailfrom_cram()
Definition: qmail-remote.c:782
void smtp_greeting()
Definition: qmail-remote.c:621
void getcontrols()
stralloc authsenders
Definition: qmail-remote.c:675
uint32 ifidx
Definition: qmail-remote.c:92
void perm_ambigmx()
Definition: qmail-remote.c:252
void temp_control()
Definition: qmail-remote.c:211
int flagtlsdomain
Definition: qmail-remote.c:62
stralloc helohost
Definition: qmail-remote.c:77
unsigned long port
Definition: qmail-remote.c:56
stralloc host
Definition: qmail-remote.c:79
void dropped()
Definition: qmail-remote.c:280
int flagsmtps
Definition: qmail-remote.c:61
stralloc sender
Definition: qmail-remote.c:81
struct constmap mapdomainips
Definition: qmail-remote.c:89
char * tlsdomaininfo
Definition: qmail-remote.c:436
void blast()
Definition: qmail-remote.c:386
void temp_dnscanon()
Definition: qmail-remote.c:185
void ehlo_auth()
Definition: qmail-remote.c:828
#define SMTP_TIMEOUT
Definition: qmail-remote.c:54
stralloc domainips
Definition: qmail-remote.c:88
void temp_nomx()
Definition: qmail-remote.c:199
struct ip_mx partner
Definition: qmail-remote.c:105
char ip6[16]
Definition: qmail-remote.c:91
void perm_dns()
Definition: qmail-remote.c:231
char msgsize[FMT_ULONG]
Definition: qmail-remote.c:103
unsigned long timeout
Definition: qmail-remote.c:291
void temp_noconn()
Definition: qmail-remote.c:166
GEN_ALLOC_typedef(GEN_ALLOC_readyplus(saa, GEN_ALLOC_readyplus(stralloc, GEN_ALLOC_readyplus(sa, GEN_ALLOC_readyplus(len, GEN_ALLOC_readyplus(a)
Definition: qmail-remote.c:73
void temp_oserr()
Definition: qmail-remote.c:154
#define PORT_SMTP
Definition: qmail-remote.c:47
stralloc user
Definition: qmail-remote.c:678
struct constmap maptlsdestinations
Definition: qmail-remote.c:441
int qmtpsend
Definition: qmail-remote.c:944
stralloc sendip
Definition: qmail-remote.c:86
void quit(char *prepend, char *append)
Definition: qmail-remote.c:374
stralloc pass
Definition: qmail-remote.c:679
unsigned long verifydepth
Definition: qmail-remote.c:442
void smtp()
Definition: qmail-remote.c:853
void outsafe(stralloc *sa)
Definition: qmail-remote.c:131
stralloc keyfile
Definition: qmail-remote.c:431
#define TCP_TIMEOUT
Definition: qmail-remote.c:53
void outsmtptext()
Definition: qmail-remote.c:362
struct constmap mapdomaincerts
Definition: qmail-remote.c:439
void perm_partialline()
Definition: qmail-remote.c:216
void get(char *ch)
Definition: qmail-remote.c:330
int utf8received()
Definition: qmail-remote.c:565
stralloc cadir
Definition: qmail-remote.c:429
int smtpfd
Definition: qmail-remote.c:292
#define HUGESMTPTEXT
Definition: qmail-remote.c:46
int flagauth
Definition: qmail-remote.c:60
stralloc slop
Definition: qmail-remote.c:682
stralloc keypwd
Definition: qmail-remote.c:432
#define PORT_QMTP
Definition: qmail-remote.c:48
void temp_nomem()
Definition: qmail-remote.c:149
char * tlsdestinfo
Definition: qmail-remote.c:435
int ehlo_starttls()
Definition: qmail-remote.c:484
stralloc ciphers
Definition: qmail-remote.c:433
stralloc xuser
Definition: qmail-remote.c:684
char bufsmall[BUFFER_SMALL]
Definition: qmail-remote.c:110
unsigned long timeoutconnect
Definition: qmail-remote.c:290
void temp_osip()
Definition: qmail-remote.c:159
buffer bi
Definition: qmail-remote.c:321
char ip4[4]
Definition: qmail-remote.c:90
#define VERIFYDEPTH
Definition: qmail-remote.c:52
struct constmap mapsmtproutes
Definition: qmail-remote.c:96
int mailfrom_login()
Definition: qmail-remote.c:752
buffer bf
Definition: qmail-remote.c:325
int ehlo_size()
Definition: qmail-remote.c:608
saa reciplist
Definition: qmail-remote.c:100
unsigned long msize
Definition: qmail-remote.c:104
stralloc smtproutes
Definition: qmail-remote.c:95
void tls_peercheck()
Definition: qmail-remote.c:497
void temp_proto()
Definition: qmail-remote.c:221
stralloc auth
Definition: qmail-remote.c:680
int mailfrom_plain()
Definition: qmail-remote.c:726
stralloc cafile
Definition: qmail-remote.c:428
buffer bo
Definition: qmail-remote.c:323
SSL_CTX * ctx
Definition: qmail-remote.c:108
void qmtp()
Definition: qmail-remote.c:946
void tls_init()
Definition: qmail-remote.c:444
stralloc eaihost
Definition: qmail-remote.c:78
stralloc senddomain
Definition: qmail-remote.c:85
void err_authprot()
Definition: qmail-remote.c:258
void zerodie()
Definition: qmail-remote.c:123
#define PORT_SMTPS
Definition: qmail-remote.c:49
unsigned long smtpcode()
Definition: qmail-remote.c:338
SSL * ssl
Definition: qmail-remote.c:107
void smtp_starttls()
Definition: qmail-remote.c:642
void perm_nomx()
Definition: qmail-remote.c:238
stralloc tlsdestinations
Definition: qmail-remote.c:440
ssize_t saferead(int fd, char *buf, size_t len)
Definition: qmail-remote.c:294
stralloc canonbox
Definition: qmail-remote.c:84
void mailfrom_xtext()
Definition: qmail-remote.c:709
stralloc domaincerts
Definition: qmail-remote.c:438
void perm_nullmx()
Definition: qmail-remote.c:245
int flagtls
Definition: qmail-remote.c:63
stralloc chal
Definition: qmail-remote.c:681
int flagsize
Definition: qmail-remote.c:606
stralloc plain
Definition: qmail-remote.c:683
void mailfrom()
Definition: qmail-remote.c:658
char outbuf[BUFFER_MTUSIZE]
Definition: qmail-remote.c:322
int j
Definition: qmail-send.c:926
#define BUFSIZE_MESS
Definition: qmail.h:7
int quote(stralloc *, stralloc *)
Definition: quote.c:62
uint32_t k[64]
Definition: sha256.c:27
Definition: ipalloc.h:8
struct ip6_address ip6
Definition: ipalloc.h:12
struct ip4_address ip4
Definition: ipalloc.h:11
union ip_mx::@0 addr
unsigned short af
Definition: ipalloc.h:9
void tcpto_err(struct ip_mx *, int)
Definition: tcpto.c:82
int tcpto(struct ip_mx *)
Definition: tcpto.c:42
void temp_tlspeercert()
Definition: tls_errors.c:93
void temp_tlsctx(void)
Definition: tls_errors.c:56
void temp_tlshost(void)
Definition: tls_errors.c:82
void temp_tlscipher(void)
Definition: tls_errors.c:46
void temp_tlscert(void)
Definition: tls_errors.c:12
void temp_tlskey(void)
Definition: tls_errors.c:20
void temp_tlschk(void)
Definition: tls_errors.c:28
void temp_tlspeervalid()
Definition: tls_errors.c:109
void temp_tlsdigest(void)
Definition: tls_errors.c:151
void temp_tlsca(void)
Definition: tls_errors.c:36
void temp_tlspeerverify()
Definition: tls_errors.c:101
void temp_tlsamissing(void)
Definition: tls_errors.c:135
void temp_tlscon()
Definition: tls_errors.c:64
void temp_tlsainvalid(void)
Definition: tls_errors.c:143
void temp_tlserr(void)
Definition: tls_errors.c:73
void temp_tlscertfp(void)
Definition: tls_errors.c:117
void temp_invaliddigest(void)
Definition: tls_errors.c:127
int tlsa_check(const STACK_OF(X509) *, const stralloc, const unsigned long)
Definition: tls_remote.c:165
int tls_destination(stralloc)
tls_destination
Definition: tls_remote.c:272
int tls_exit(SSL *)
Definition: tls_remote.c:251
int tls_conn(SSL *, int)
Definition: tls_remote.c:49
int tls_domaincerts(stralloc)
Definition: tls_remote.c:363
int tls_fingerprint(X509 *, const char *, const int)
Definition: tls_remote.c:227
int tls_certkey(SSL_CTX *, const char *, const char *, char *)
Definition: tls_remote.c:29
int tls_checkpeer(SSL *, X509 *, const stralloc, const int, const int)
Definition: tls_remote.c:56
int tls_timeoutconn(int, int, int, SSL *)
Definition: tls_timeoutio.c:67
int tls_timeoutwrite(int, int, int, SSL *, char *, int)
int tls_timeoutread(int, int, int, SSL *, char *, int)
Definition: tls_timeoutio.c:96
#define ssl_client()
Definition: ucspitls.h:16
int ssl_ciphers(SSL_CTX *, const char *)
#define ssl_errstr()
Definition: ucspitls.h:35
SSL * ssl_new(SSL_CTX *, int)
int ssl_ca(SSL_CTX *, const char *, const char *, int)