s/qmail 4.3.20
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
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("Possible duplicate! ");
286 out("(#4.4.2)\n");
287 zerodie();
288}
289
291unsigned long timeout = SMTP_TIMEOUT;
293
294ssize_t saferead(int fd,char *buf,int 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,int 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 unsigned char ch;
341 unsigned long code;
342
343 if (!stralloc_copys(&smtptext,"")) temp_nomem();
344
345 get(&ch); code = ch - '0';
346 get(&ch); code = code * 10 + (ch - '0');
347 get(&ch); code = code * 10 + (ch - '0');
348 for (;;) {
349 get(&ch);
350 if (ch != '-') break;
351 while (ch != '\n') get(&ch);
352 get(&ch);
353 get(&ch);
354 get(&ch);
355 }
356 while (ch != '\n') get(&ch);
357
358 return code;
359}
360
362{
363 int i;
364 if (smtptext.s) if (smtptext.len) {
365 out("Remote host said: ");
366 for (i = 0; i < smtptext.len; ++i)
367 if (!smtptext.s[i]) smtptext.s[i] = '?';
368 if (buffer_put(&bs,smtptext.s,smtptext.len) == -1) _exit(0);
369 smtptext.len = 0;
370 }
371}
372
373void quit(char *prepend,char *append)
374{
375 buffer_putsflush(&bo,"QUIT\r\n");
376 /* waiting for remote side is just too ridiculous */
377 out(prepend);
378 outhost();
379 out(append);
380 out(".\n");
381 outsmtptext();
382 zerodie();
383}
384
385void blast()
386{
387 int r;
388 int in;
389 int out;
390 int eom = 1; // end-of-message <CRLF>.<CRLF>
391 char tmpbuf[BUFSIZE_MESS]; // intermediate write buffer
392
393// New blast code; inspired by Bruce Guenter's 'fastremote patch' (2005)
394
395 while ((r = buffer_get(&bi,inbuf,sizeof(inbuf)))) { // read into buffer
396 if (r == -1) temp_read();
397
398 for (in = out = 0; in < r;) {
399 if (eom && inbuf[in] == '.') {
400 tmpbuf[out++] = '.';
401 tmpbuf[out++] = inbuf[in++];
402 }
403 eom = 0;
404 while (in < r) {
405 if (inbuf[in] == '\r') { in++; continue; } // CR is DKIM input
406 if (inbuf[in] == '\n') {
407 eom = 1;
408 ++in;
409 tmpbuf[out++] = '\r';
410 tmpbuf[out++] = '\n';
411 break;
412 }
413 tmpbuf[out++] = inbuf[in++];
414 }
415 }
416 if (out) buffer_put(&bo,tmpbuf,out);
417 }
418
419 if (!eom) perm_partialline();
420 flagcritical = 1;
421 buffer_put(&bo,".\r\n",3); // LF seen; finish with .<CRLF>
422 buffer_flush(&bo);
423}
424
425/* this file is too long -------------------------------------- client TLS */
426
427stralloc cafile = {0};
428stralloc cadir = {0};
429stralloc certfile = {0};
430stralloc keyfile = {0};
431stralloc keypwd = {0};
432stralloc ciphers = {0};
433
434char *tlsdestinfo = 0;
436
437stralloc domaincerts = {0};
439stralloc tlsdestinations = {0};
441unsigned long verifydepth = VERIFYDEPTH;
442
444{
445 ctx = ssl_client();
446 ssl_errstr();
447 if (!ctx) temp_tlsctx();
448
449/* Fetch CA infos for dest */
450
451 if (flagverify > 0)
452 if (cafile.len || cadir.len)
453 if (!ssl_ca(ctx,cafile.s,cadir.s,(int) verifydepth)) temp_tlsca();
454
455 if (ciphers.len)
457
458/* Prepare for Certificate Request */
459
460 if (flagtlsdomain == 2)
461 switch (tls_certkey(ctx,certfile.s,keyfile.s,keypwd.s)) {
462 case 0: break;
463 case -1: temp_tlscert();
464 case -2: temp_tlskey();
465 case -3: temp_tlschk();
466 }
467
468/* Set SSL Context */
469
471 if (!ssl) temp_tlsctx();
472
473/* Setup SSL FDs */
474
475 if (!tls_conn(ssl,smtpfd)) temp_tlscon();
476
477/* Go on in none-blocking mode */
478
480 temp_tlserr();
481}
482
484{
485 int i = 0;
486
487 do {
488 if (case_startb(smtptext.s + i + 4,8,"STARTTLS")) return 1;
489 } while ((i += str_chr(smtptext.s + i,'\n') + 1) &&
490 (i < smtptext.len - 12));
491
492 return 0;
493}
494
496{
497 X509 *cert;
498 STACK_OF(X509) *certs;
499 int ncerts = 0;
500
501 cert = SSL_get_peer_certificate(ssl);
502 if (!cert) { flagtls = 100; return; }
503
504 if ((certs = SSL_get_peer_cert_chain(ssl)) == NULL) {
505 certs = sk_X509_new_null();
506 ncerts++;
507 sk_X509_push(certs,cert);
508 }
509
510 if (flagverify == -2) { // fingerprinting is silent
511 if (cafile.len) case_lowerb(cafile.s,cafile.len);
512 switch (tls_fingerprint(cert,cafile.s + 1,cafile.len - 2)) {
513 case -1: temp_tlspeercert();
514 case -2: temp_tlsdigest();
515 case -3: temp_invaliddigest();
516 case 0: temp_tlscertfp();
517 case 1: flagtls = 104; break;
518 }
519 }
520
521 if (flagverify >= 0) { // TLSA is default
522 switch (tlsa_check(certs,remotehost,port)) {
523 case -4: temp_tlsamissing(); break; /* FIXME */
524 case -3: temp_tlsainvalid(); break;
525 case -2: break; // unsupported type; may happen
526 case -1: break; // weird TLSA record
527 case 0: break; // no TLSA record given
528 case 1: case 2: flagtls = 107; flagverify = 3; break; // full certchain available (-PKIX)
529 case 3: flagtls = 106; flagverify = 0; break; // TA-CA; verify wont work
530 case 4: flagtls = 105; flagverify = 0; break; // Endpoint only
531 }
532 }
533
534 if (flagverify > 0) {
536 case -1: temp_tlspeercert();
537 case -2: temp_tlspeerverify();
538 case -3: temp_tlspeervalid();
539 case 1: flagtls = 101; break;
540 case 2: flagtls = 102; break;
541 case 3: flagtls = 103; break;
542 }
543 }
544
545 if (flagtls < 100) flagtls = 100;
546
547 if (ncerts) sk_X509_free(certs);
548 X509_free(cert);
549
550 return;
551}
552
553/* this file is too long --------------------------------------- smtp UTF8 */
554
555int utf8string(unsigned char *ch,int len)
556{
557 int i = 0;
558 while (i < len)
559 if (ch[i++] > 127) return 1;
560 return 0;
561}
562
564{
565 int r;
566 int i;
567 int received = 0;
568 char ch;
569 stralloc receivedline = {0};
570
571/* we consider only our own last written header */
572
573 for (;;) {
574 r = buffer_get(&bi,&ch,1);
575 if (r == 0) break;
576 if (r == -1) temp_read();
577 if (ch == '\r') continue; // DKIM
578
579 if (ch == '\n') {
580 if (!stralloc_append(&header,"\r")) temp_nomem(); /* received.c does not add '\r' */
581 if (!stralloc_append(&header,"\n")) temp_nomem();
582 if (case_starts(receivedline.s,"Date:")) return 0; /* header to quit asap */
583 if (case_starts(receivedline.s,"Received: from")) received++; /* found Received header */
584 if (received) {
585 if (case_starts(receivedline.s," by ")) {
586 for (i = 6; i < receivedline.len - 6; ++i)
587 if (*(receivedline.s + i) == ' ')
588 if (case_starts(receivedline.s + i + 1,"with UTF8")) return 1;
589 return 0;
590 }
591 }
592 if (!stralloc_copys(&receivedline,"")) temp_nomem();
593 } else {
594 if (!stralloc_append(&header,&ch)) temp_nomem();
595 if (!stralloc_catb(&receivedline,&ch,1)) temp_nomem();
596 }
597 }
598 return 0;
599}
600
601/* this file is too long -------------------------------------- smtp client */
602
603unsigned long code;
604int flagsize = 0;
605
607{
608 int i = 0;
609
610 do {
611 if (case_startb(smtptext.s + i + 4,4,"SIZE")) return 1;
612 } while ((i += str_chr(smtptext.s + i,'\n') + 1) &&
613 (i < smtptext.len - 8));
614
615 return 0;;
616}
617
619{
620 buffer_puts(&bo,"EHLO ");
621 buffer_put(&bo,helohost.s,helohost.len);
622 buffer_puts(&bo,"\r\n");
623 buffer_flush(&bo);
624
625 if (smtpcode() != 250) {
626 buffer_puts(&bo,"HELO ");
627 buffer_put(&bo,helohost.s,helohost.len);
628 buffer_puts(&bo,"\r\n");
629 buffer_flush(&bo);
630
631 code = smtpcode();
632 authsender = 0;
633 if (code >= 500) quit("DConnected to "," but my name was rejected");
634 if (code != 250) quit("ZConnected to "," but my name was rejected");
635 }
637}
638
640{
641 buffer_puts(&bo,"STARTTLS\r\n");
642 buffer_flush(&bo);
643
644 if (smtpcode() == 220) {
645 tls_init();
648 }
649 else {
650 flagtls = -2;
651 quit("ZConnected to "," but STARTTLS was rejected");
652 }
653}
654
656{
657 buffer_puts(&bo,"MAIL FROM:<");
658 buffer_put(&bo,sender.s,sender.len);
659 buffer_puts(&bo,">");
660 if (flagutf8 || utf8received())
661 buffer_puts(&bo," SMTPUTF8");
662 if (flagsize && msize) {
663 buffer_puts(&bo," SIZE=");
664 buffer_puts(&bo,msgsize);
665 }
666 buffer_puts(&bo,"\r\n");
667 buffer_flush(&bo);
668}
669
670/* this file is too long -------------------------------------- client auth */
671
672stralloc authsenders = {0};
674
675stralloc user = {0};
676stralloc pass = {0};
677stralloc auth = {0};
678stralloc chal = {0};
679stralloc slop = {0};
680stralloc plain = {0};
681stralloc xuser = {0};
682
683static const char hextab[] = "0123456789abcdef";
684
685int xtext(stralloc *sa,char *s,int len)
686{
687 int i;
688 unsigned char c;
689 char xch[2];
690
691 if (!stralloc_copys(sa,"")) temp_nomem();
692
693 for (i = 0; i < len; i++) {
694 c = s[i];
695 if (c < 33 || c > 126 || c == '=' || c == '+') {
696 xch[0] = hextab[(c >> 4) & 0x0f];
697 xch[1] = hextab[c & 0x0f];
698 if (!stralloc_catb(sa,xch,2)) temp_nomem();
699 } else
700 if (!stralloc_catb(sa,s + i,1)) temp_nomem();
701 }
702
703 return sa->len;
704}
705
707{
708 if (!xtext(&xuser,user.s,user.len)) temp_nomem();
709 buffer_puts(&bo,"MAIL FROM:<");
710 buffer_put(&bo,sender.s,sender.len);
711 buffer_puts(&bo,"> AUTH=");
712 buffer_put(&bo,xuser.s,xuser.len);
713 if (flagutf8 || utf8received())
714 buffer_puts(&bo," SMTPUTF8");
715 if (flagsize && msize) {
716 buffer_puts(&bo," SIZE=");
717 buffer_puts(&bo,msgsize);
718 }
719 buffer_puts(&bo,"\r\n");
720 buffer_flush(&bo);
721}
722
724{
725 buffer_puts(&bo,"AUTH PLAIN\r\n");
726 buffer_flush(&bo);
727
728 if (smtpcode() != 334) quit("ZConnected to "," but authentication was rejected (AUTH PLAIN)");
729
730 if (!stralloc_cats(&plain,"")) temp_nomem(); /* RFC 4616 section 2 */
731 if (!stralloc_0(&plain)) temp_nomem();
732 if (!stralloc_cat(&plain,&user)) temp_nomem(); /* user-id */
733 if (!stralloc_0(&plain)) temp_nomem();
734 if (!stralloc_cat(&plain,&pass)) temp_nomem(); /* password */
735 if (b64encode(&plain,&auth)) quit("ZConnected to "," but unable to base64encode (plain)");
736 buffer_put(&bo,auth.s,auth.len);
737 buffer_puts(&bo,"\r\n");
738 buffer_flush(&bo);
739
740 switch (smtpcode()) {
741 case 235: mailfrom_xtext(); break;
742 case 432: quit("DConnected to "," but password expired");
743 case 534: quit("ZConnected to "," but authentication mechamism too weak (plain)");
744 default: quit("ZConnected to "," but authentication was rejected (plain)");
745 }
746 return 0;
747}
748
750{
751 buffer_puts(&bo,"AUTH LOGIN\r\n");
752 buffer_flush(&bo);
753
754 if (smtpcode() != 334) quit("ZConnected to "," but authentication was rejected (AUTH LOGIN)");
755 if (!stralloc_copys(&auth,"")) temp_nomem();
756 if (b64encode(&user,&auth)) quit("ZConnected to "," but unable to base64encode user");
757
758 buffer_put(&bo,auth.s,auth.len);
759 buffer_puts(&bo,"\r\n");
760 buffer_flush(&bo);
761
762 if (smtpcode() != 334) quit("ZConnected to "," but authentication was rejected (username)");
763
764 if (!stralloc_copys(&auth,"")) temp_nomem();
765 if (b64encode(&pass,&auth)) quit("ZConnected to "," but unable to base64encode pass");
766 buffer_put(&bo,auth.s,auth.len);
767 buffer_puts(&bo,"\r\n");
768 buffer_flush(&bo);
769
770 switch (smtpcode()) {
771 case 235: mailfrom_xtext(); break;
772 case 432: quit("DConnected to "," but password expired");
773 case 534: quit("ZConnected to "," but authentication mechanism is too weak (login)");
774 default: quit("ZConnected to "," but authentication was rejected (login)");
775 }
776 return 0;
777}
778
780{
781 int j;
782 unsigned char digest[16];
783 unsigned char digascii[33];
784
785 buffer_puts(&bo,"AUTH CRAM-MD5\r\n");
786 buffer_flush(&bo);
787
788 if (smtpcode() != 334) quit("ZConnected to "," but authentication was rejected (AUTH CRAM-MD5)");
789 if (str_chr(smtptext.s + 4,' ')) { /* Challenge */
790 if (!stralloc_copys(&slop,"")) temp_nomem();
791 if (!stralloc_copyb(&slop,smtptext.s + 4,smtptext.len - 5)) temp_nomem();
792 if (b64decode(slop.s,slop.len,&chal)) quit("ZConnected to "," but unable to base64decode challenge");
793 }
794
795 hmac_md5((unsigned char *)chal.s,chal.len,pass.s,pass.len,digest);
796
797 for (j = 0; j < 16; j++) { /* HEX => ASCII */
798 digascii[2 * j] = hextab[digest[j] >> 4];
799 digascii[2 * j + 1] = hextab[digest[j] & 0x0f];
800 }
801 digascii[32]=0;
802
803 if (!stralloc_copys(&slop,"")) temp_nomem();
804 if (!stralloc_cat(&slop,&user)) temp_nomem(); /* user-id */
805 if (!stralloc_cats(&slop," ")) temp_nomem();
806 if (!stralloc_catb(&slop,digascii,32)) temp_nomem(); /* digest */
807
808 if (!stralloc_copys(&auth,"")) temp_nomem();
809 if (b64encode(&slop,&auth)) quit("ZConnected to "," but unable to base64encode username+digest");
810
811 buffer_put(&bo,auth.s,auth.len);
812 buffer_puts(&bo,"\r\n");
813 buffer_flush(&bo);
814
815 switch (smtpcode()) {
816 case 235: mailfrom_xtext(); break;
817 case 432: quit("DConnected to "," but password expired");
818 case 534: quit("ZConnected to "," but authentication mechamism too weak (cram)");
819 default: quit("ZConnected to "," but authentication was rejected (cram)");
820 }
821 return 0;
822}
823
825{
826 int i = 0;
827
828 do {
829 if (case_startb(smtptext.s + i + 4,4,"AUTH"))
830 for (i = 4; i < smtptext.len - 5; ++i) {
831 if (case_startb(smtptext.s + i,4,"CRAM"))
832 if (mailfrom_cram() >= 0) return;
833 if (case_startb(smtptext.s + i,5,"LOGIN"))
834 if (mailfrom_login() >= 0) return;
835 if (case_startb(smtptext.s + i,5,"PLAIN"))
836 if (mailfrom_plain() >= 0) return;
837 }
838 } while ((i += str_chr(smtptext.s + i,'\n') + 1) &&
839 (i < smtptext.len - 12));
840
841 err_authprot();
842 mailfrom();
843}
844
845/* this file is too long ------------------------------------------- GO ON */
846
847void smtp()
848{
849 int flagbother;
850 int i;
851
852 if (flagtls > 10 && flagtls < 20) { /* SMTPS */
853 tls_init();
854 tls_peercheck();
855 }
856
857 code = smtpcode();
858 if (code >= 500) quit("DConnected to "," but sender was rejected");
859 if (code == 421 || code == 450) quit("ZConnected to "," but probably greylisted"); /* RFC 6647 */
860 if (code >= 400) quit("ZConnected to "," but sender was rejected");
861 if (code != 220) quit("ZConnected to "," but greeting failed");
862
864
865 if (flagtls > 0 && flagtls < 10) { /* STARTTLS */
866 if (ehlo_starttls())
868 else if (flagtls > 3 && flagtls != 9) {
869 if (!stralloc_0(&host)) temp_nomem();
870 temp_tlshost();
871 }
872 }
873 if (user.len && pass.len) /* AUTH */
874 ehlo_auth();
875 else
876 mailfrom(); /* Mail From */
877
878 code = smtpcode();
879 if (code >= 500) quit("DConnected to "," but sender was rejected");
880 if (code >= 400) quit("ZConnected to "," but sender was probably greylisted");
881
882 flagbother = 0; /* Rcpt To */
883 for (i = 0; i < reciplist.len; ++i) {
884 buffer_puts(&bo,"RCPT TO:<");
885 buffer_put(&bo,reciplist.sa[i].s,reciplist.sa[i].len);
886 buffer_puts(&bo,">\r\n");
887 buffer_flush(&bo);
888
889 code = smtpcode(); /* Data */
890 if (flagsize) {
891 if (code == 552) quit("DConnected to "," but message size is too large");
892 if (code == 452) quit("ZConnected to "," however insufficient storage space available");
893 }
894 if (code == 421 || code == 450) { // Postfix merde ;-)
895 out("s"); outhost(); out(" sender is greylisting.\n");
896 outsmtptext(); zero();
897 } else if (code >= 500) {
898 out("h"); outhost(); out(" does not like recipient.\n");
899 outsmtptext(); zero();
900 } else if (code >= 400) {
901 out("s"); outhost(); out(" does not like recipient.\n");
902 outsmtptext(); zero();
903 } else {
904 out("r"); zero();
905 flagbother = 1;
906 }
907 }
908 if (!flagbother) quit("DGiving up on ","");
909
910 buffer_putsflush(&bo,"DATA\r\n");
911
912 code = smtpcode();
913 if (code >= 500) quit("D"," failed on DATA command");
914 if (code >= 400) quit("Z"," failed on DATA command");
915
916 buffer_putflush(&bo,header.s,header.len);
917
918 blast();
919 code = smtpcode();
920 flagcritical = 0;
921 if (code >= 500) quit("D"," failed after I sent the message");
922 if (code >= 400) quit("Z"," failed after I sent the message");
923 switch (flagtls) { // StartTLS + SMTPS
924 case 100: case 110: quit("K"," TLS transmitted message accepted"); break;
925 case 101: case 111: quit("K"," TLS (verified CA) transmitted message accepted"); break;
926 case 102: case 112: quit("K"," TLS (validated CA+DN*) transmitted message accepted"); break;
927 case 103: case 113: quit("K"," TLS (validated CA+DN) transmitted message accepted"); break;
928 case 104: case 114: quit("K"," TLS (CERT pinning) transmitted message accepted"); break;
929 case 105: case 115: quit("K"," TLS (TLSA EE validated) transmitted message accepted"); break;
930 case 106: case 116: quit("K"," TLS (TLSA TA validated) transmitted message accepted"); break;
931 case 107: case 117: quit("K"," TLS (TLSA PKIX verified) transmitted message accepted"); break;
932 default: quit("K"," accepted message"); break;
933 }
934}
935
936/* this file is too long -------------------------------------- qmtp client */
937
938int qmtpsend = 0;
939
940void qmtp()
941{
942 unsigned long len = 0;
943 char *x;
944 int i = 0;
945 int n = 0;
946 unsigned char ch;
947 char num[FMT_ULONG];
948 int flagallok;
949
950 if (qmtpsend == 2) { /* QMTPS */
951 tls_init();
953 }
954
955/* the following code was substantially taken from serialmail's serialqmtp.c */
956
957 if (msize)
958 len = msize;
959 else {
960 out("DMessage for: "); outhost(); out(" has zero bytes. Giving up.\n");
961 }
962
963 buffer_put(&bo,num,fmt_ulong(num,len + 1));
964 buffer_put(&bo,":\n",2);
965 while (len > 0) {
966 n = buffer_feed(&bi);
967 if (n <= 0) _exit(1); /* wise guy again */
968 x = buffer_PEEK(&bi);
969 buffer_put(&bo,x,n);
970 buffer_SEEK(&bi,n);
971 len -= n;
972 }
973 buffer_put(&bo,",",1);
974
975 len = sender.len;
976 buffer_put(&bo,num,fmt_ulong(num,len));
977 buffer_put(&bo,":",1);
978 buffer_put(&bo,sender.s,sender.len);
979 buffer_put(&bo,",",1);
980
981 len = 0;
982 for (i = 0; i < reciplist.len; ++i)
983 len += fmt_ulong(num,reciplist.sa[i].len) + 1 + reciplist.sa[i].len + 1;
984 buffer_put(&bo,num,fmt_ulong(num,len));
985 buffer_put(&bo,":",1);
986 for (i = 0; i < reciplist.len; ++i) {
987 buffer_put(&bo,num,fmt_ulong(num,reciplist.sa[i].len));
988 buffer_put(&bo,":",1);
989 buffer_put(&bo,reciplist.sa[i].s,reciplist.sa[i].len);
990 buffer_put(&bo,",",1);
991 }
992 buffer_put(&bo,",",1);
993 buffer_flush(&bo);
994
995 flagallok = 1;
996
997 for (i = 0; i < reciplist.len; ++i) {
998 len = 0;
999 for (;;) {
1000 get(&ch);
1001 if (ch == ':') break;
1002 if (len > QMTP_MAX) temp_proto();
1003 if (ch - '0' > 9) temp_proto();
1004 len = 10 * len + (ch - '0');
1005 }
1006 if (!len) temp_proto();
1007 get(&ch); --len;
1008 if ((ch != 'Z') && (ch != 'D') && (ch != 'K')) temp_proto();
1009
1010 if (!stralloc_copyb(&smtptext,&ch,1)) temp_nomem();
1011 if (flagtls >= 100) {
1012 if (!stralloc_cats(&smtptext,"qmtps:")) temp_nomem();
1013 } else {
1014 if (!stralloc_cats(&smtptext,"qmtp:")) temp_nomem();
1015 }
1016
1017 while (len > 0) {
1018 get(&ch);
1019 --len;
1020 }
1021
1022 for (len = 0; len < smtptext.len; ++len) {
1023 ch = smtptext.s[len];
1024 if ((ch < 32) || (ch > 126)) smtptext.s[len] = '?';
1025 }
1026 get(&ch); // Read response
1027 if (ch != ',') temp_proto();
1028 smtptext.s[smtptext.len - 1] = '\n';
1029
1030 if (smtptext.s[0] == 'K') out("r");
1031 else if (smtptext.s[0] == 'D') {
1032 out("h");
1033 flagallok = 0;
1034 }
1035 else { /* if (smtptext.s[0] == 'Z') */
1036 out("s");
1037 flagallok = 0;
1038 }
1039 if (buffer_put(&bs,smtptext.s + 1,smtptext.len - 1) == -1) temp_qmtpnoc();
1040 zero();
1041 }
1042
1043 if (!flagallok) {
1044 out("DGiving up on "); outhost(); out("\n");
1045 } else {
1046 out("KAll received okay by "); outhost(); out("\n");
1047 }
1048 zerodie();
1049}
1050
1051/* this file is too long -------------------------------------- common */
1052
1053/* host has to be canonical [A/AAAA record], box has to be quoted */
1054
1055void addrmangle(stralloc *saout,char *address,int *flagalias,int flagcname)
1056{
1057 int at;
1058 int r = 0;
1059 stralloc cn = {0};
1060
1061 *flagalias = flagcname; /* saout + flagalias are output */
1062 if (!flagutf8)
1063 flagutf8 = utf8string(address,str_len(address));
1064
1065 at = str_rchr(address,'@');
1066 if (!address[at]) {
1067 if (!stralloc_copys(saout,address)) temp_nomem();
1068 return;
1069 }
1070
1072 canonbox.len = at;
1073 if (!quote(saout,&canonbox)) temp_nomem(); /* saout = 'inbox' name without quotes ;-) */
1074 if (!stralloc_cats(saout,"@")) temp_nomem();
1075
1076 if (!stralloc_copys(&canonhost,address + at + 1)) temp_nomem();
1077 if (flagcname) { /* no relayhost */
1078 DNS_INIT
1079 switch ((r = dns_cname(&cn,&canonhost))) {
1080 case DNS_MEM: temp_nomem();
1081 case DNS_SOFT: temp_dnscanon();
1082 case DNS_HARD: ; /* alias loop, not our problem */
1083 default: if (r > 0) *flagalias = 0;
1084 }
1085 }
1086 if (!stralloc_cat(saout,&canonhost)) temp_nomem();
1087}
1088
1090{
1091 if (control_init() == -1) temp_control();
1092 if (control_readint(&timeout,"control/timeoutremote") == -1) temp_control();
1093 if (control_readint(&timeoutconnect,"control/timeoutconnect") == -1)
1094 temp_control();
1095 if (control_rldef(&helohost,"control/helohost",1,(char *) 0) != 1)
1096 temp_control();
1097 switch (control_readfile(&smtproutes,"control/smtproutes",0)) {
1098 case -1: temp_control();
1099 case 0: if (!constmap_init(&mapsmtproutes,"",0,1)) temp_nomem(); break;
1100 case 1: if (!constmap_init(&mapsmtproutes,smtproutes.s,smtproutes.len,1)) temp_nomem(); break;
1101 }
1102 switch (control_readfile(&domainips,"control/domainips",0)) {
1103 case -1: temp_control();
1104 case 0: if (!constmap_init(&mapdomainips,"",0,1)) temp_nomem(); break;
1105 case 1: if (!constmap_init(&mapdomainips,domainips.s,domainips.len,1)) temp_nomem(); break;
1106 }
1107 switch (control_readfile(&authsenders,"control/authsenders",0)) {
1108 case -1: temp_control();
1109 case 0: if (!constmap_init(&mapauthsenders,"",0,1)) temp_nomem(); break;
1110 case 1: if (!constmap_init(&mapauthsenders,authsenders.s,authsenders.len,1)) temp_nomem(); break;
1111 }
1112 switch (control_readfile(&qmtproutes,"control/qmtproutes",0)) {
1113 case -1: temp_control();
1114 case 0: if (!constmap_init(&mapqmtproutes,"",0,1)) temp_nomem(); break;
1115 case 1: if (!constmap_init(&mapqmtproutes,qmtproutes.s,qmtproutes.len,1)) temp_nomem(); break;
1116 }
1117 switch (control_readfile(&domaincerts,"control/domaincerts",0)) {
1118 case -1: temp_control();
1119 case 0: if (!constmap_init(&mapdomaincerts,"",0,1)) temp_nomem(); break;
1120 case 1: if (!constmap_init(&mapdomaincerts,domaincerts.s,domaincerts.len,1)) temp_nomem(); break;
1121 }
1122 switch (control_readfile(&tlsdestinations,"control/tlsdestinations",0)) {
1123 case -1: temp_control();
1124 case 0: if (!constmap_init(&maptlsdestinations,"",0,1)) temp_nomem(); break;
1126 }
1127}
1128
1129int main(int argc,char * const *argv)
1130{
1131 static ipalloc ip = {0};
1132 stralloc netif = {0};
1133 struct stat st;
1134 int i, j, k;
1135 int p; /* reserved for port */
1136 int r; /* reserved for return code */
1137 unsigned long random;
1138 char **recips;
1139 unsigned long prefme;
1140 int flagallaliases;
1141 int flagalias;
1142 char *relayhost;
1143 char *localip;
1144 int ip6flag = 0;
1145
1146 sig_pipeignore();
1147 if (argc < 4) perm_usage();
1148 if (chdir(auto_qmail) == -1) temp_chdir();
1149
1150 getcontrols();
1151 if (!stralloc_copys(&host,argv[1])) temp_nomem();
1152
1153 authsender = 0;
1154 relayhost = 0;
1155
1156 addrmangle(&sender,argv[2],&flagalias,0);
1157
1158 if (sender.len > 1) {
1159 i = str_chr(sender.s,'@');
1160 if (sender.s[i] == '@')
1161 if (!stralloc_copyb(&senddomain,sender.s + i + 1,sender.len - i - 1)) temp_nomem(); // un-terminated
1162 }
1163
1164/* this file is too long -------------------------------------- set domain ip + helohost */
1165
1166 localip = 0;
1167
1168 for (i = 0; i <= senddomain.len; ++i)
1169 if ((i == 0) || (senddomain.s[i] == '.'))
1170 if ((localip = constmap(&mapdomainips,senddomain.s + i,senddomain.len - i)))
1171 break;
1172
1173 if (!localip)
1174 localip = constmap(&mapdomainips,"*",1); /* one for all */
1175
1176 if (localip) {
1177 j = str_chr(localip,'%');
1178 if (localip[j] != '%') j = 0;
1179 k = str_chr(localip,'|');
1180 if (localip[k] != '|') k = 0;
1181 if (k) { /* helohost */
1182 if (!stralloc_copys(&helohost,localip + k + 1)) temp_nomem();
1183 if (!stralloc_0(&helohost)) temp_nomem();
1184 localip[k] = 0;
1185 }
1186 if (j) { /* IF index */
1187 localip[j] = 0;
1188 if (!stralloc_copys(&netif,localip + j + 1)) temp_nomem();
1189 if (!stralloc_0(&netif)) temp_nomem();
1190 }
1191 }
1192
1193/* this file is too long -------------------------------------- authsender routes */
1194
1195 for (i = 0; i <= sender.len; ++i)
1196 if ((i == 0) || (i == sender.len) || (sender.s[i] == '.') || (sender.s[i] == '@'))
1197 if ((authsender = constmap(&mapauthsenders,sender.s + i,sender.len - i)))
1198 break;
1199
1200 if (authsender && !*authsender) authsender = 0;
1201
1202 if (authsender) {
1203 i = str_chr(authsender,'|');
1204 if (authsender[i] == '|') {
1205 j = str_chr(authsender + i + 1,'|');
1206 if (authsender[i + j + 1] == '|') {
1207 authsender[i] = 0;
1208 authsender[i + j + 1] = 0;
1209 if (!stralloc_copys(&user,"")) temp_nomem();
1210 if (!stralloc_copys(&user,authsender + i + 1)) temp_nomem();
1211 if (!stralloc_copys(&pass,"")) temp_nomem();
1212 if (!stralloc_copys(&pass,authsender + i + j + 2)) temp_nomem();
1213 }
1214 }
1215 p = str_chr(authsender,';');
1216 if (authsender[p] == ';') {
1217 if (authsender[p + 1] == 's') { flagsmtps = 1, p++; }
1218 scan_ulong(authsender + p + 1,&port);
1219 authsender[p] = 0;
1220 }
1221 relayhost = authsender;
1223 }
1224
1225/* this file is too long -------------------------------------- standard routes */
1226
1227 if (!authsender) {
1228 if (sender.len == 0) { /* bounce routes */
1229 if ((relayhost = constmap(&mapqmtproutes,"!@",2))) {
1230 qmtpsend = 1; port = PORT_QMTP;
1231 } else
1232 relayhost = constmap(&mapsmtproutes,"!@",2);
1233 }
1234
1235 if (relayhost && !*relayhost) relayhost = 0;
1236
1237 if (!relayhost) {
1238 for (i = 0; i <= host.len; ++i) { /* qmtproutes */
1239 if ((i == 0) || (i == host.len) || (host.s[i] == '.'))
1240 if ((relayhost = constmap(&mapqmtproutes,host.s + i,host.len - i))) {
1241 qmtpsend = 1; port = PORT_QMTP;
1242 break;
1243 } /* default smtproutes */
1244 if ((relayhost = constmap(&mapsmtproutes,host.s + i,host.len - i)))
1245 break;
1246 }
1247 }
1248 if (relayhost && !*relayhost) relayhost = 0;
1249
1250 if (relayhost) { /* default smtproutes -- authenticated */
1251 i = str_chr(relayhost,'|');
1252 if (relayhost[i] == '|') {
1253 j = str_chr(relayhost + i + 1,'|'); // authenticate
1254 if (relayhost[i + j + 1] == '|') {
1255 relayhost[i] = 0;
1256 relayhost[i + j + 1] = 0;
1257 if (!stralloc_copys(&user,"")) temp_nomem();
1258 if (!stralloc_copys(&user,relayhost + i + 1)) temp_nomem();
1259 if (!stralloc_copys(&pass,"")) temp_nomem();
1260 k = str_chr(relayhost + i + j + 2,'|'); // local ip
1261 if (relayhost[i + j + k + 2] == '|') {
1262 relayhost[i + j + k + 2] = 0;
1263 localip = relayhost + i + j + k + 3;
1264 }
1265 if (!stralloc_copys(&pass,relayhost + i + j + 2)) temp_nomem();
1266 }
1267 }
1268 p = str_chr(relayhost,';');
1269 if (relayhost[p] == ';') {
1270 if (relayhost[p + 1] == 's') { flagsmtps = 1; p++; } // RFC 8314
1271 scan_ulong(relayhost + p + 1,&port);
1272 if (qmtpsend && port == PORT_QMTPS) qmtpsend = 2;
1273 relayhost[p] = 0;
1274 }
1275 if (!stralloc_copys(&host,relayhost)) temp_nomem();
1276#ifdef IDN2
1277 } else {
1278 char *asciihost = 0;
1279 if (!stralloc_0(&host)) temp_nomem();
1280 switch (idn2_lookup_u8(host.s,(uint8_t**)&asciihost,IDN2_NFC_INPUT)) {
1281 case IDN2_OK: break;
1282 case IDN2_MALLOC: temp_nomem();
1283 default: perm_dns();
1284 }
1285 if (!stralloc_copys(&idnhost,asciihost)) temp_nomem();
1286#endif
1287 }
1288 }
1289
1290/* this file is too long -------------------------------------- TLS destinations */
1291
1292
1293 flagtls = tls_destination((const stralloc) host); // host may not be 0-terminated
1294
1295 if (flagtls > 0) {
1296 if (tlsdestinfo) {
1297 i = str_chr(tlsdestinfo,'|'); /* ca file/dir or cert fingerprint */
1298 if (tlsdestinfo[i] == '|') {
1299 tlsdestinfo[i] = 0;
1300 j = str_chr(tlsdestinfo + i + 1,'|'); /* cipher */
1301 if (tlsdestinfo[i + j + 1] == '|') {
1302 tlsdestinfo[i + j + 1] = 0;
1303 k = str_chr(tlsdestinfo + i + j + 2,'|'); /* cone domain */
1304 if (tlsdestinfo[i + j + k + 2] == '|') {
1305 tlsdestinfo[i + j + k + 2] = 0;
1306 if (str_diffn(tlsdestinfo + j + k + 3,canonhost.s,canonhost.len)) flagtls = 0;
1307 }
1308 p = str_chr(tlsdestinfo + i + j + 2,';'); /* verifydepth;port */
1309 if (tlsdestinfo[i + j + p + 2] == ';') {
1310 tlsdestinfo[i + j + p + 2] = 0;
1311 if (p > 0) scan_ulong(tlsdestinfo + i + j + 2,&verifydepth);
1312 if (tlsdestinfo[i + j + p + 3] == 's') { flagsmtps = 1; p++; } /* RFC 8314 */
1313 scan_ulong(tlsdestinfo + i + j + p + 3,&port);
1314 }
1315 }
1316 if (j)
1317 if (!stralloc_copys(&ciphers,tlsdestinfo + i + 1)) temp_nomem();
1318 }
1319
1320 /* either ':[=]cafile/cadir' -or- ':;port' */
1321
1322 if (tlsdestinfo[0] == ';')
1323 scan_ulong(tlsdestinfo + 1,&port);
1324 else
1326 }
1327
1328/* cafile starts with '=' => it is a fingerprint
1329 cafile ends with '/' => consider it as cadir
1330 cafile and cadir are now 0-terminated
1331 ciphers are alway 0-terminated if given */
1332
1333 if (cafile.len > 2) {
1334 flagverify = 1;
1335 if (cafile.s[cafile.len] == '/') {
1336 cafile.len = 0;
1337 flagverify = 2;
1339 if (!stralloc_0(&cadir)) temp_nomem();
1340 } else {
1341 if (cafile.s[0] == '=') flagverify = -2;
1342 if (!stralloc_0(&cafile)) temp_nomem();
1343 }
1344 } else
1345 cafile.len = cadir.len = 0;
1346
1347 if (ciphers.len > 4) /* otherwise garbage */
1348 if (!stralloc_0(&ciphers)) temp_nomem();
1349 else
1350 ciphers.len = 0;
1351
1352 if (port == PORT_SMTPS || flagsmtps) flagtls += 10;
1353 if (port == PORT_QMTPS) flagtls += 20;
1354 }
1355
1356 if (flagtls == 8 || flagtls == 18 || flagtls == 28) flagverify = -1; /* no TLSA */
1357 if (!flagtls && qmtpsend == 2) flagtls = 20; /* QMTPS */
1358
1359
1360/* this file is too long -------------------------------------- Our Certs - per senddomain */
1361
1362 if (flagtls > 0) {
1363 flagtlsdomain = tls_domaincerts((const stralloc) senddomain); // senddomain un-terminated
1364
1366 i = str_chr(tlsdomaininfo,'|');
1367 if (tlsdomaininfo[i] == '|') {
1368 tlsdomaininfo[i] = 0;
1369 j = str_chr(tlsdomaininfo + i + 1,'|');
1370 if (tlsdomaininfo[i + j + 1] == '|') {
1371 tlsdomaininfo[i + j + 1] = 0;
1372 if (!stralloc_copys(&keypwd,"")) temp_nomem();
1373 if (!stralloc_copys(&keypwd,tlsdomaininfo + i + j + 2)) temp_nomem();
1374 if (!stralloc_0(&keypwd)) temp_nomem();
1375 }
1377 if (!stralloc_0(&keyfile)) temp_nomem();
1378 }
1380 if (!stralloc_0(&certfile)) temp_nomem();
1381 flagtlsdomain = 2;
1382 }
1383 }
1384
1385/* this file is too long -------------------------------------- work thru reciplist */
1386
1387 if (!saa_readyplus(&reciplist,0)) temp_nomem();
1388 if (ipme_init() != 1) temp_oserr();
1389
1390 flagallaliases = 1;
1391 recips = argv + 3;
1392
1393 if (fstat(0,&st) == -1) quit("Z", " unable to fstat stdin");
1394 msize = st.st_size;
1395 fmt_ulong(msgsize,msize);
1396
1397 while (*recips) {
1398 if (!saa_readyplus(&reciplist,1)) temp_nomem();
1399 reciplist.sa[reciplist.len] = sauninit;
1400 addrmangle(reciplist.sa + reciplist.len,*recips,&flagalias,!relayhost);
1401 if (!flagalias) flagallaliases = 0;
1402 ++reciplist.len;
1403 ++recips;
1404 }
1405
1406 random = now() + (getpid() << 16);
1407#ifdef IDN2
1408 switch (relayhost ? dns_ip(&ip,&host) : dns_mxip(&ip,&idnhost,random)) {
1409#else
1410 switch (relayhost ? dns_ip(&ip,&host) : dns_mxip(&ip,&host,random)) {
1411#endif
1412 case DNS_MEM: temp_nomem();
1413 case DNS_ERR: temp_dns();
1414 case DNS_COM: temp_dns();
1415 case DNS_SOFT: temp_dns();
1416#ifdef DEFERREDBOUNCES
1417 default: if (!ip.len) temp_nomx();
1418#else
1419 default: if (!ip.len) perm_nomx();
1420#endif
1421 }
1422
1423 prefme = 100000;
1424 for (i = 0; i < ip.len; ++i)
1425 if (ipme_is(&ip.ix[i])) {
1426#ifdef NULLMX
1427 if (ip.ix[i].pref == 0 && ip.ix[i].mxh[0] == '.')
1428 perm_nullmx(); /* RFC 7505 MX = 0 */
1429#endif
1430 if (ip.ix[i].pref < prefme)
1431 prefme = ip.ix[i].pref;
1432 }
1433
1434 if (relayhost) prefme = 300000;
1435 if (flagallaliases) prefme = 500000;
1436
1437 if (localip) {
1438 i = str_chr(localip,':');
1439 if (localip[i] == ':') ip6flag = 1;
1440 else ip6flag = -1;
1441 }
1442
1443 for (i = 0; i < ip.len; ++i) { /* MX with smallest distance */
1444 if (ip6flag == -1 && ip.ix[i].af == AF_INET6) continue;
1445 if (ip6flag == 1 && ip.ix[i].af == AF_INET) continue;
1446 if (ip.ix[i].pref < prefme) break;
1447 }
1448
1449 if (i >= ip.len) perm_ambigmx();
1450
1451 if (!stralloc_copys(&remotehost,ip.ix[i].mxh)) temp_nomem(); /* take MX hostname for TLSA */
1452 if (!stralloc_0(&remotehost)) temp_nomem();
1453
1454 for (i = 0; i < ip.len; ++i) {
1455 if (ip.ix[i].pref < prefme) {
1456 if (ip6flag == -1 && ip.ix[i].af == AF_INET6) continue; /* explicit binding */
1457 if (ip6flag == 1 && ip.ix[i].af == AF_INET) continue;
1458 if (tcpto(&ip.ix[i])) continue;
1459
1460 smtpfd = socket(ip.ix[i].af,SOCK_STREAM,0);
1461 if (smtpfd == -1) continue;
1462
1463 if (localip) { /* set domain ip */
1464 if (!stralloc_copyb(&sendip,localip,str_len(localip))) temp_nomem();
1465 j = str_chr(localip,':');
1466 if (localip[j] == ':') {
1467 if (!ip6_scan(localip,ip6)) temp_noip(); /* IPv6 */
1468 if (byte_equal(ip.ix[i].addr.ip6.d,16,ip6)) continue;
1469 ifidx = socket_getifidx(netif.s);
1470 if (socket_bind6(smtpfd,ip6,0,ifidx) < 0) temp_osip();
1471 } else {
1472 if (!ip4_scan(localip,ip4)) temp_noip(); /* IPv4 */
1473 if (byte_equal(ip.ix[i].addr.ip4.d,4,ip4)) continue;
1474 if (socket_bind4(smtpfd,ip4,0) < 0) temp_osip();
1475 }
1476 }
1477
1478
1479 AGAIN:
1480 if (ip.ix[i].af == AF_INET6)
1481 r = timeoutconn6(smtpfd,(char *)&ip.ix[i].addr.ip6.d,(unsigned int) port,timeoutconnect,ifidx);
1482 else
1483 r = timeoutconn4(smtpfd,(char *)&ip.ix[i].addr.ip4.d,(unsigned int) port,timeoutconnect);
1484 if (r == 0) {
1485 tcpto_err(&ip.ix[i],0);
1486 partner = ip.ix[i];
1487 if (qmtpsend)
1488 qmtp();
1489 else
1490 smtp(); /* read qmail/THOUGHTS; section 6 */
1491 }
1492 if (flagtls == 9 && errno == EPROTO) {
1493 flagtls = 0; goto AGAIN;
1494 }
1495 if (errno == ETIMEDOUT || errno == ECONNREFUSED || errno == EPROTO)
1496 tcpto_err(&ip.ix[i],1);
1497 close(smtpfd);
1498 }
1499 }
1500 temp_noconn();
1501}
char auto_qmail[]
int b64encode(stralloc *in, stralloc *out)
Definition: base64.c:88
int b64decode(const unsigned char *in, int l, stralloc *out)
Definition: base64.c:11
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:67
void p(char *, char *, int, int, int)
Definition: install.c:49
void hmac_md5(unsigned char *text, int text_len, unsigned char *key, int key_len, unsigned 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:69
stralloc canonhost
Definition: qmail-remote.c:82
unsigned long code
Definition: qmail-remote.c:603
stralloc idnhost
Definition: qmail-remote.c:80
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
ssize_t saferead(int fd, char *buf, int len)
Definition: qmail-remote.c:294
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:673
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:685
void temp_qmtpnoc()
Definition: qmail-remote.c:173
int flagverify
Definition: qmail-remote.c:69
stralloc certfile
Definition: qmail-remote.c:429
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:779
void smtp_greeting()
Definition: qmail-remote.c:618
void getcontrols()
stralloc authsenders
Definition: qmail-remote.c:672
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:435
void blast()
Definition: qmail-remote.c:385
void temp_dnscanon()
Definition: qmail-remote.c:185
void ehlo_auth()
Definition: qmail-remote.c:824
#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:675
struct constmap maptlsdestinations
Definition: qmail-remote.c:440
int qmtpsend
Definition: qmail-remote.c:938
stralloc sendip
Definition: qmail-remote.c:86
void quit(char *prepend, char *append)
Definition: qmail-remote.c:373
stralloc pass
Definition: qmail-remote.c:676
unsigned long verifydepth
Definition: qmail-remote.c:441
void smtp()
Definition: qmail-remote.c:847
void outsafe(stralloc *sa)
Definition: qmail-remote.c:131
stralloc keyfile
Definition: qmail-remote.c:430
#define TCP_TIMEOUT
Definition: qmail-remote.c:53
void outsmtptext()
Definition: qmail-remote.c:361
struct constmap mapdomaincerts
Definition: qmail-remote.c:438
void perm_partialline()
Definition: qmail-remote.c:216
void get(char *ch)
Definition: qmail-remote.c:330
int utf8string(unsigned char *ch, int len)
Definition: qmail-remote.c:555
int utf8received()
Definition: qmail-remote.c:563
stralloc cadir
Definition: qmail-remote.c:428
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:679
stralloc keypwd
Definition: qmail-remote.c:431
#define PORT_QMTP
Definition: qmail-remote.c:48
void temp_nomem()
Definition: qmail-remote.c:149
char * tlsdestinfo
Definition: qmail-remote.c:434
int ehlo_starttls()
Definition: qmail-remote.c:483
stralloc ciphers
Definition: qmail-remote.c:432
stralloc xuser
Definition: qmail-remote.c:681
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
ssize_t safewrite(int fd, char *buf, int len)
Definition: qmail-remote.c:307
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:749
buffer bf
Definition: qmail-remote.c:325
int ehlo_size()
Definition: qmail-remote.c:606
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:495
void temp_proto()
Definition: qmail-remote.c:221
stralloc auth
Definition: qmail-remote.c:677
int mailfrom_plain()
Definition: qmail-remote.c:723
stralloc cafile
Definition: qmail-remote.c:427
buffer bo
Definition: qmail-remote.c:323
SSL_CTX * ctx
Definition: qmail-remote.c:108
void qmtp()
Definition: qmail-remote.c:940
void tls_init()
Definition: qmail-remote.c:443
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:639
void perm_nomx()
Definition: qmail-remote.c:238
stralloc tlsdestinations
Definition: qmail-remote.c:439
stralloc canonbox
Definition: qmail-remote.c:84
void mailfrom_xtext()
Definition: qmail-remote.c:706
stralloc domaincerts
Definition: qmail-remote.c:437
void perm_nullmx()
Definition: qmail-remote.c:245
int flagtls
Definition: qmail-remote.c:63
stralloc chal
Definition: qmail-remote.c:678
int flagsize
Definition: qmail-remote.c:604
stralloc plain
Definition: qmail-remote.c:680
void mailfrom()
Definition: qmail-remote.c:655
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:26
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:168
int tls_exit(SSL *)
Definition: tls_remote.c:255
int tls_conn(SSL *, int)
Definition: tls_remote.c:49
int tls_domaincerts(const stralloc)
Definition: tls_remote.c:366
int tls_destination(const stralloc)
tls_destination
Definition: tls_remote.c:276
int tls_fingerprint(X509 *, const char *, const int)
Definition: tls_remote.c:231
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_timeoutwrite(int t, int rfd, int wfd, SSL *tls, char *buf, int len)
Definition: tls_timeoutio.c:96
int tls_timeoutread(int t, int rfd, int wfd, SSL *tls, char *buf, int len)
Definition: tls_timeoutio.c:89
int tls_timeoutconn(int t, int rfd, int wfd, SSL *tls)
Definition: tls_timeoutio.c:60
#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)