chiark / gitweb /
Merge branches 'idx/verh' and 'idx/qmqpc'
[qmail] / qmail-remote.c
CommitLineData
2117e02e
MW
1#include <sys/types.h>
2#include <sys/socket.h>
3#include <netinet/in.h>
4#include <arpa/inet.h>
5#include "sig.h"
2117e02e
MW
6#include "stralloc.h"
7#include "substdio.h"
8#include "subfd.h"
9#include "scan.h"
10#include "case.h"
11#include "error.h"
12#include "auto_qmail.h"
13#include "control.h"
14#include "dns.h"
15#include "alloc.h"
16#include "quote.h"
17#include "ip.h"
18#include "ipalloc.h"
19#include "ipme.h"
20#include "gen_alloc.h"
21#include "gen_allocdefs.h"
22#include "str.h"
23#include "now.h"
24#include "exit.h"
25#include "constmap.h"
26#include "tcpto.h"
212b6f5d 27#include "readwrite.h"
2117e02e
MW
28#include "timeoutconn.h"
29#include "timeoutread.h"
30#include "timeoutwrite.h"
31
32#define HUGESMTPTEXT 5000
33
34#define PORT_SMTP 25 /* silly rabbit, /etc/services is for users */
35unsigned long port = PORT_SMTP;
36
37GEN_ALLOC_typedef(saa,stralloc,sa,len,a)
38GEN_ALLOC_readyplus(saa,stralloc,sa,len,a,i,n,x,10,saa_readyplus)
39static stralloc sauninit = {0};
40
41stralloc helohost = {0};
42stralloc routes = {0};
43struct constmap maproutes;
44stralloc host = {0};
45stralloc sender = {0};
46
47saa reciplist = {0};
48
49struct ip_address partner;
50
212b6f5d
MW
51void out(s) char *s; { if (substdio_puts(subfdoutsmall,s) == -1) _exit(0); }
52void zero() { if (substdio_put(subfdoutsmall,"\0",1) == -1) _exit(0); }
53void zerodie() { zero(); substdio_flush(subfdoutsmall); _exit(0); }
2117e02e
MW
54void outsafe(sa) stralloc *sa; { int i; char ch;
55for (i = 0;i < sa->len;++i) {
56ch = sa->s[i]; if (ch < 33) ch = '?'; if (ch > 126) ch = '?';
212b6f5d 57if (substdio_put(subfdoutsmall,&ch,1) == -1) _exit(0); } }
2117e02e
MW
58
59void temp_nomem() { out("ZOut of memory. (#4.3.0)\n"); zerodie(); }
60void temp_oserr() { out("Z\
61System resources temporarily unavailable. (#4.3.0)\n"); zerodie(); }
62void temp_noconn() { out("Z\
63Sorry, I wasn't able to establish an SMTP connection. (#4.4.1)\n"); zerodie(); }
64void temp_read() { out("ZUnable to read message. (#4.3.0)\n"); zerodie(); }
65void temp_dnscanon() { out("Z\
66CNAME lookup failed temporarily. (#4.4.3)\n"); zerodie(); }
67void temp_dns() { out("Z\
68Sorry, I couldn't find any host by that name. (#4.1.2)\n"); zerodie(); }
69void temp_chdir() { out("Z\
70Unable to switch to home directory. (#4.3.0)\n"); zerodie(); }
71void temp_control() { out("Z\
72Unable to read control files. (#4.3.0)\n"); zerodie(); }
73void perm_partialline() { out("D\
74SMTP cannot transfer messages with partial final lines. (#5.6.2)\n"); zerodie(); }
75void perm_usage() { out("D\
76I (qmail-remote) was invoked improperly. (#5.3.5)\n"); zerodie(); }
77void perm_dns() { out("D\
78Sorry, I couldn't find any host named ");
79outsafe(&host);
80out(". (#5.1.2)\n"); zerodie(); }
81void perm_nomx() { out("D\
82Sorry, I couldn't find a mail exchanger or IP address. (#5.4.4)\n");
83zerodie(); }
84void perm_ambigmx() { out("D\
85Sorry. Although I'm listed as a best-preference MX or A for that host,\n\
86it isn't in my control/locals file, so I don't treat it as local. (#5.4.6)\n");
87zerodie(); }
88
212b6f5d
MW
89void outhost()
90{
91 char x[IPFMT];
92 if (substdio_put(subfdoutsmall,x,ip_fmt(x,&partner)) == -1) _exit(0);
93}
94
95int flagcritical = 0;
96
97void dropped() {
98 out("ZConnected to ");
99 outhost();
100 out(" but connection died. ");
101 if (flagcritical) out("Possible duplicate! ");
102 out("(#4.4.2)\n");
103 zerodie();
104}
105
2117e02e 106int timeoutconnect = 60;
212b6f5d
MW
107int smtpfd;
108int timeout = 1200;
2117e02e 109
212b6f5d 110int saferead(fd,buf,len) int fd; char *buf; int len;
2117e02e 111{
212b6f5d
MW
112 int r;
113 r = timeoutread(timeout,smtpfd,buf,len);
114 if (r <= 0) dropped();
115 return r;
116}
117int safewrite(fd,buf,len) int fd; char *buf; int len;
118{
119 int r;
120 r = timeoutwrite(timeout,smtpfd,buf,len);
121 if (r <= 0) dropped();
122 return r;
2117e02e
MW
123}
124
212b6f5d
MW
125char inbuf[1024];
126substdio ssin = SUBSTDIO_FDBUF(read,0,inbuf,sizeof inbuf);
2117e02e 127char smtptobuf[1024];
212b6f5d 128substdio smtpto = SUBSTDIO_FDBUF(safewrite,-1,smtptobuf,sizeof smtptobuf);
2117e02e 129char smtpfrombuf[128];
212b6f5d
MW
130substdio smtpfrom = SUBSTDIO_FDBUF(saferead,-1,smtpfrombuf,sizeof smtpfrombuf);
131
2117e02e
MW
132stralloc smtptext = {0};
133
212b6f5d
MW
134void get(ch)
135char *ch;
2117e02e 136{
212b6f5d
MW
137 substdio_get(&smtpfrom,ch,1);
138 if (*ch != '\r')
139 if (smtptext.len < HUGESMTPTEXT)
140 if (!stralloc_append(&smtptext,ch)) temp_nomem();
2117e02e
MW
141}
142
212b6f5d 143unsigned long smtpcode()
2117e02e 144{
212b6f5d
MW
145 unsigned char ch;
146 unsigned long code;
147
148 if (!stralloc_copys(&smtptext,"")) temp_nomem();
149
150 get(&ch); code = ch - '0';
151 get(&ch); code = code * 10 + (ch - '0');
152 get(&ch); code = code * 10 + (ch - '0');
153 for (;;) {
154 get(&ch);
155 if (ch != '-') break;
156 while (ch != '\n') get(&ch);
157 get(&ch);
158 get(&ch);
159 get(&ch);
2117e02e 160 }
212b6f5d 161 while (ch != '\n') get(&ch);
2117e02e 162
212b6f5d 163 return code;
2117e02e
MW
164}
165
212b6f5d 166void outsmtptext()
2117e02e 167{
212b6f5d
MW
168 int i;
169 if (smtptext.s) if (smtptext.len) {
170 out("Remote host said: ");
171 for (i = 0;i < smtptext.len;++i)
172 if (!smtptext.s[i]) smtptext.s[i] = '?';
173 if (substdio_put(subfdoutsmall,smtptext.s,smtptext.len) == -1) _exit(0);
174 smtptext.len = 0;
175 }
2117e02e
MW
176}
177
212b6f5d
MW
178void quit(prepend,append)
179char *prepend;
180char *append;
2117e02e 181{
212b6f5d
MW
182 substdio_putsflush(&smtpto,"QUIT\r\n");
183 /* waiting for remote side is just too ridiculous */
184 out(prepend);
185 outhost();
186 out(append);
187 out(".\n");
188 outsmtptext();
189 zerodie();
2117e02e
MW
190}
191
34c57ee0
MW
192stralloc verh = {0}; /* quoted recipient */
193int flagverh; /* argc */
194char *vp; /* argv[3] */
195
212b6f5d 196void blast()
2117e02e 197{
34c57ee0
MW
198 unsigned int posat, i;
199 int flagdobody,flagheader;
212b6f5d
MW
200 int r;
201 char ch;
202
34c57ee0
MW
203 posat = 0; /* stays 0 if no VERH */
204 flagdobody = 0; /* => 0 at first blank line */
205 flagheader = 1;
206 if (flagverh == 4) { /* only if single recipient */
207 if (!quote2(&verh,vp)) temp_nomem(); /* non-canonicalized */
208 for (i = 0; i < verh.len; i++) /* \n would destroy message */
209 if (verh.s[i] == '\n') verh.s[i] = '_';
210 posat = byte_rchr(verh.s,verh.len,'@'); /* posat=0 if no VERH */
211 if (posat == verh.len) posat = 0;
212 }
212b6f5d
MW
213 for (;;) {
214 r = substdio_get(&ssin,&ch,1);
215 if (r == 0) break;
216 if (r == -1) temp_read();
217 if (ch == '.')
218 substdio_put(&smtpto,".",1);
34c57ee0
MW
219 if (flagheader) {
220 if (ch == '\n') { /* header ends */
221 flagheader = 0;
222 if (!flagdobody) posat = 0;
223 } else if (ch == '#') { /* # starting line => VERH ... */
224 flagdobody = 1; /* continues in body and ... */
225 continue; /* character is suppressed. */
226 }
227 }
212b6f5d 228 while (ch != '\n') {
34c57ee0
MW
229 if (ch == '#' && posat) { /* ... # */
230 r = substdio_get(&ssin,&ch,1);
231 if (r == 0) perm_partialline();
232 if (r == -1) temp_read();
233 if (ch == '#') { /* ... ## */
234 register char ch1;
235 ch1 = *substdio_peek(&ssin);
236 if (ch1 != 'L' && ch1 != 'H') { /* ... ##x x!=L x!=H */
237 substdio_put(&smtpto,"#",1);
238 continue;
239 }
240 r = substdio_get(&ssin,&ch,1);
241 if (r == 0) perm_partialline();
242 if (r == -1) temp_read();
243 if (ch == 'L') /* ... ##L */
244 substdio_put(&smtpto,verh.s,posat);
245 else /* ... ##H */
246 substdio_put(&smtpto,verh.s + posat + 1,verh.len - posat - 1);
247 } else {
248 substdio_put(&smtpto,"#",1);
249 if (ch == '\n') break;
250 substdio_put(&smtpto,&ch,1);
251 }
252 } else
253 substdio_put(&smtpto,&ch,1);
212b6f5d
MW
254 r = substdio_get(&ssin,&ch,1);
255 if (r == 0) perm_partialline();
256 if (r == -1) temp_read();
257 }
258 substdio_put(&smtpto,"\r\n",2);
2117e02e 259 }
212b6f5d
MW
260
261 flagcritical = 1;
262 substdio_put(&smtpto,".\r\n",3);
263 substdio_flush(&smtpto);
2117e02e
MW
264}
265
266stralloc recip = {0};
267
212b6f5d 268void smtp()
2117e02e 269{
212b6f5d
MW
270 unsigned long code;
271 int flagbother;
272 int i;
273
274 if (smtpcode() != 220) quit("ZConnected to "," but greeting failed");
275
276 substdio_puts(&smtpto,"HELO ");
277 substdio_put(&smtpto,helohost.s,helohost.len);
278 substdio_puts(&smtpto,"\r\n");
279 substdio_flush(&smtpto);
280 if (smtpcode() != 250) quit("ZConnected to "," but my name was rejected");
281
282 substdio_puts(&smtpto,"MAIL FROM:<");
283 substdio_put(&smtpto,sender.s,sender.len);
284 substdio_puts(&smtpto,">\r\n");
285 substdio_flush(&smtpto);
286 code = smtpcode();
287 if (code >= 500) quit("DConnected to "," but sender was rejected");
288 if (code >= 400) quit("ZConnected to "," but sender was rejected");
289
290 flagbother = 0;
291 for (i = 0;i < reciplist.len;++i) {
292 substdio_puts(&smtpto,"RCPT TO:<");
293 substdio_put(&smtpto,reciplist.sa[i].s,reciplist.sa[i].len);
294 substdio_puts(&smtpto,">\r\n");
295 substdio_flush(&smtpto);
296 code = smtpcode();
297 if (code >= 500) {
298 out("h"); outhost(); out(" does not like recipient.\n");
299 outsmtptext(); zero();
2117e02e 300 }
212b6f5d
MW
301 else if (code >= 400) {
302 out("s"); outhost(); out(" does not like recipient.\n");
303 outsmtptext(); zero();
2117e02e 304 }
212b6f5d
MW
305 else {
306 out("r"); zero();
307 flagbother = 1;
2117e02e
MW
308 }
309 }
212b6f5d
MW
310 if (!flagbother) quit("DGiving up on ","");
311
312 substdio_putsflush(&smtpto,"DATA\r\n");
313 code = smtpcode();
314 if (code >= 500) quit("D"," failed on DATA command");
315 if (code >= 400) quit("Z"," failed on DATA command");
316
317 blast();
318 code = smtpcode();
319 flagcritical = 0;
320 if (code >= 500) quit("D"," failed after I sent the message");
321 if (code >= 400) quit("Z"," failed after I sent the message");
322 quit("K"," accepted message");
2117e02e
MW
323}
324
325stralloc canonhost = {0};
326stralloc canonbox = {0};
327
328void addrmangle(saout,s,flagalias,flagcname)
329stralloc *saout; /* host has to be canonical, box has to be quoted */
330char *s;
331int *flagalias;
332int flagcname;
333{
212b6f5d
MW
334 int j;
335
336 *flagalias = flagcname;
337
338 j = str_rchr(s,'@');
339 if (!s[j]) {
340 if (!stralloc_copys(saout,s)) temp_nomem();
341 return;
2117e02e 342 }
212b6f5d
MW
343 if (!stralloc_copys(&canonbox,s)) temp_nomem();
344 canonbox.len = j;
345 if (!quote(saout,&canonbox)) temp_nomem();
346 if (!stralloc_cats(saout,"@")) temp_nomem();
347
348 if (!stralloc_copys(&canonhost,s + j + 1)) temp_nomem();
349 if (flagcname)
350 switch(dns_cname(&canonhost)) {
351 case 0: *flagalias = 0; break;
352 case DNS_MEM: temp_nomem();
353 case DNS_SOFT: temp_dnscanon();
354 case DNS_HARD: ; /* alias loop, not our problem */
2117e02e
MW
355 }
356
212b6f5d
MW
357 if (!stralloc_cat(saout,&canonhost)) temp_nomem();
358}
359
360void getcontrols()
361{
362 if (control_init() == -1) temp_control();
363 if (control_readint(&timeout,"control/timeoutremote") == -1) temp_control();
364 if (control_readint(&timeoutconnect,"control/timeoutconnect") == -1)
365 temp_control();
366 if (control_rldef(&helohost,"control/helohost",1,(char *) 0) != 1)
367 temp_control();
368 switch(control_readfile(&routes,"control/smtproutes",0)) {
369 case -1:
370 temp_control();
371 case 0:
372 if (!constmap_init(&maproutes,"",0,1)) temp_nomem(); break;
373 case 1:
374 if (!constmap_init(&maproutes,routes.s,routes.len,1)) temp_nomem(); break;
375 }
2117e02e
MW
376}
377
378void main(argc,argv)
379int argc;
380char **argv;
381{
212b6f5d
MW
382 static ipalloc ip = {0};
383 int i;
384 unsigned long random;
385 char **recips;
386 unsigned long prefme;
387 int flagallaliases;
388 int flagalias;
389 char *relayhost;
390
391 sig_pipeignore();
392 if (argc < 4) perm_usage();
34c57ee0
MW
393 flagverh = argc;
394 vp = argv[3];
212b6f5d
MW
395 if (chdir(auto_qmail) == -1) temp_chdir();
396 getcontrols();
397
398
399 if (!stralloc_copys(&host,argv[1])) temp_nomem();
400
401 relayhost = 0;
402 for (i = 0;i <= host.len;++i)
403 if ((i == 0) || (i == host.len) || (host.s[i] == '.'))
404 if (relayhost = constmap(&maproutes,host.s + i,host.len - i))
405 break;
406 if (relayhost && !*relayhost) relayhost = 0;
407
408 if (relayhost) {
409 i = str_chr(relayhost,':');
410 if (relayhost[i]) {
411 scan_ulong(relayhost + i + 1,&port);
412 relayhost[i] = 0;
2117e02e 413 }
212b6f5d 414 if (!stralloc_copys(&host,relayhost)) temp_nomem();
2117e02e
MW
415 }
416
417
212b6f5d
MW
418 addrmangle(&sender,argv[2],&flagalias,0);
419
420 if (!saa_readyplus(&reciplist,0)) temp_nomem();
421 if (ipme_init() != 1) temp_oserr();
422
423 flagallaliases = 1;
424 recips = argv + 3;
425 while (*recips) {
426 if (!saa_readyplus(&reciplist,1)) temp_nomem();
427 reciplist.sa[reciplist.len] = sauninit;
428 addrmangle(reciplist.sa + reciplist.len,*recips,&flagalias,!relayhost);
429 if (!flagalias) flagallaliases = 0;
430 ++reciplist.len;
431 ++recips;
2117e02e
MW
432 }
433
212b6f5d
MW
434
435 random = now() + (getpid() << 16);
436 switch (relayhost ? dns_ip(&ip,&host) : dns_mxip(&ip,&host,random)) {
437 case DNS_MEM: temp_nomem();
438 case DNS_SOFT: temp_dns();
439 case DNS_HARD: perm_dns();
440 case 1:
441 if (ip.len <= 0) temp_dns();
2117e02e 442 }
212b6f5d
MW
443
444 if (ip.len <= 0) perm_nomx();
445
446 prefme = 100000;
447 for (i = 0;i < ip.len;++i)
448 if (ipme_is(&ip.ix[i].ip))
449 if (ip.ix[i].pref < prefme)
450 prefme = ip.ix[i].pref;
451
452 if (relayhost) prefme = 300000;
453 if (flagallaliases) prefme = 500000;
454
455 for (i = 0;i < ip.len;++i)
456 if (ip.ix[i].pref < prefme)
457 break;
458
459 if (i >= ip.len)
460 perm_ambigmx();
461
462 for (i = 0;i < ip.len;++i) if (ip.ix[i].pref < prefme) {
463 if (tcpto(&ip.ix[i].ip)) continue;
464
465 smtpfd = socket(AF_INET,SOCK_STREAM,0);
466 if (smtpfd == -1) temp_oserr();
467
468 if (timeoutconn(smtpfd,&ip.ix[i].ip,(unsigned int) port,timeoutconnect) == 0) {
469 tcpto_err(&ip.ix[i].ip,0);
470 partner = ip.ix[i].ip;
471 smtp(); /* does not return */
2117e02e 472 }
212b6f5d
MW
473 tcpto_err(&ip.ix[i].ip,errno == error_timeout);
474 close(smtpfd);
2117e02e 475 }
212b6f5d
MW
476
477 temp_noconn();
2117e02e 478}