10 #include "auto_qmail.h"
13 #include "date822fmt.h"
14 #include "readwrite.h"
21 void dropped() { _exit(0); }
22 void badproto() { _exit(100); }
23 void resources() { _exit(111); }
24 void sigalrm() { _exit(111); }
26 unsigned long getlen()
34 if (substdio_get(subfdinsmall,&ch,1) < 1) dropped();
35 if (ch == ':') return len;
36 if (len > 200000000) resources();
37 len = 10 * len + (ch - '0');
45 if (substdio_get(subfdinsmall,&ch,1) < 1) dropped();
46 if (ch != ',') badproto();
58 stralloc failure = {0};
61 stralloc rcpthosts = {0};
62 struct constmap maprcpthosts;
66 int addrallowed(buf,len) char *buf; int len;
69 if (!flagrcpthosts) return 1;
70 j = byte_rchr(buf,len,'@');
71 if (j >= len) return 1;
72 if (constmap(&maprcpthosts,buf + j + 1,len - j - 1)) return 1;
75 if (constmap(&maprcpthosts,buf + j,len - j)) return 1;
91 sig_alarmcatch(sigalrm);
94 if (chdir(auto_qmail) == -1) resources();
96 if (control_init() == -1) resources();
97 flagrcpthosts = control_readfile(&rcpthosts,"control/rcpthosts",0);
98 if (flagrcpthosts == -1) resources();
100 if (!constmap_init(&maprcpthosts,rcpthosts.s,rcpthosts.len,0)) resources();
101 relayclient = env_get("RELAYCLIENT");
102 relayclientlen = relayclient ? str_len(relayclient) : 0;
104 remotehost = env_get("TCPREMOTEHOST");
105 if (!remotehost) remotehost = "unknown";
106 remoteinfo = env_get("TCPREMOTEINFO");
107 remoteip = env_get("TCPREMOTEIP");
108 if (!remoteip) remoteip = "unknown";
109 local = env_get("TCPLOCALHOST");
110 if (!local) local = env_get("TCPLOCALIP");
111 if (!local) local = "unknown";
115 if (!stralloc_copys(&failure,"")) resources();
119 if (len == 0) badproto();
121 if (qmail_open(&qqt) == -1) resources();
124 if (substdio_get(subfdinsmall,&ch,1) < 1) dropped();
127 if (ch == 10) flagdos = 0;
128 else if (ch == 13) flagdos = 1;
131 received(&qqt,"QMTP",local,remoteip,remotehost,remoteinfo,(char *) 0);
133 /* XXX: check for loops? only if len is big? */
138 if (substdio_get(subfdinsmall,&ch,1) < 1) dropped();
140 while ((ch == 13) && len)
142 if (substdio_get(subfdinsmall,&ch,1) < 1) dropped();
145 qmail_put(&qqt,"\015",1);
147 qmail_put(&qqt,&ch,1);
150 while (len > 0) /* XXX: could speed this up, obviously */
152 if (substdio_get(subfdinsmall,&ch,1) < 1) dropped();
154 qmail_put(&qqt,&ch,1);
164 for (i = 0;i < len;++i)
165 if (substdio_get(subfdinsmall,&ch,1) < 1) dropped();
169 for (i = 0;i < len;++i)
171 if (substdio_get(subfdinsmall,buf + i,1) < 1) dropped();
172 if (!buf[i]) flagsenderok = 0;
178 qmail_from(&qqt,buf);
179 if (!flagsenderok) qmail_fail(&qqt);
184 if (!stralloc_append(&failure,"")) resources();
189 if (!biglen) badproto();
190 if (substdio_get(subfdinsmall,&ch,1) < 1) dropped();
192 if (ch == ':') break;
193 if (len > 200000000) resources();
194 len = 10 * len + (ch - '0');
196 if (len >= biglen) badproto();
197 if (len + relayclientlen >= 1000)
199 failure.s[failure.len - 1] = 'L';
200 for (i = 0;i < len;++i)
201 if (substdio_get(subfdinsmall,&ch,1) < 1) dropped();
205 for (i = 0;i < len;++i)
207 if (substdio_get(subfdinsmall,buf + i,1) < 1) dropped();
208 if (!buf[i]) failure.s[failure.len - 1] = 'N';
213 str_copy(buf + len,relayclient);
215 if (!addrallowed(buf,len)) failure.s[failure.len - 1] = 'D';
217 if (!failure.s[failure.len - 1])
225 switch(qmail_close(&qqt))
227 case 0: result = 0; break;
228 case QMAIL_WAITPID: result = "Zqq waitpid surprise (#4.3.0)"; break;
229 case QMAIL_CRASHED: result = "Zqq crashed (#4.3.0)"; break;
230 case QMAIL_USAGE: result = "Zqq usage surprise (#4.3.0)"; break;
231 case QMAIL_SYS: result = "Zqq system error (#4.3.0)"; break;
232 case QMAIL_READ: result = "Zqq read error (#4.3.0)"; break;
233 case QMAIL_WRITE: result = "Zqq write error or disk full (#4.3.0)"; break;
234 case QMAIL_NOMEM: result = "Zqq out of memory (#4.3.0)"; break;
235 case QMAIL_EXECSOFT: result = "Zcould not exec qq (#4.3.0)"; break;
236 case QMAIL_TIMEOUT: result = "Zqq timeout (#4.3.0)"; break;
237 case QMAIL_TOOLONG: result = "Dqq toolong surprise (#5.1.3)"; break;
238 default: result = "Zqq internal bug (#4.3.0)"; break;
241 if (!flagsenderok) result = "Dunacceptable sender (#5.1.7)";
244 len = str_len(result);
249 len += fmt_str(buf2 + len,"Kok ");
250 len += fmt_ulong(buf2 + len,(unsigned long) now());
251 len += fmt_str(buf2 + len," qp ");
252 len += fmt_ulong(buf2 + len,qp);
257 len = fmt_ulong(buf,len);
259 len += fmt_str(buf + len,result);
262 for (i = 0;i < failure.len;++i)
266 if (substdio_put(subfdoutsmall,buf,len) == -1)
270 if (substdio_puts(subfdoutsmall,"66:Dsorry, that domain isn't in my list of allowed rcpthosts (#5.7.1),") == -1)
274 if (substdio_puts(subfdoutsmall,"46:Dsorry, I can't handle that recipient (#5.1.3),") == -1)
279 /* subfdoutsmall will be flushed when we read from the network again */