File: qmail-qmtpd.c

package info (click to toggle)
qmail 1.01-5
  • links: PTS
  • area: non-free
  • in suites: hamm
  • size: 2,188 kB
  • ctags: 1,711
  • sloc: ansic: 13,993; makefile: 1,914; perl: 448; sh: 377
file content (281 lines) | stat: -rw-r--r-- 6,773 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#include "stralloc.h"
#include "substdio.h"
#include "subfd.h"
#include "qmail.h"
#include "now.h"
#include "str.h"
#include "fmt.h"
#include "env.h"
#include "sig.h"
#include "auto_qmail.h"
#include "now.h"
#include "datetime.h"
#include "date822fmt.h"
#include "readwrite.h"
#include "control.h"
#include "constmap.h"
#include "received.h"

struct qmail qqt;

void dropped() { _exit(0); }
void badproto() { _exit(100); }
void resources() { _exit(111); }
void sigalrm() { _exit(111); }

unsigned long getlen()
{
 unsigned long len;
 char ch;

 len = 0;
 for (;;)
  {
   if (substdio_get(subfdinsmall,&ch,1) < 1) dropped();
   if (ch == ':') return len;
   if (len > 200000000) resources();
   len = 10 * len + (ch - '0');
  }
}

void getcomma()
{
 char ch;

 if (substdio_get(subfdinsmall,&ch,1) < 1) dropped();
 if (ch != ',') badproto();
}

struct datetime dt;
char buf[1000];
char buf2[100];

char *remotehost;
char *remoteinfo;
char *remoteip;
char *local;

stralloc failure = {0};

int flagrcpthosts;
stralloc rcpthosts = {0};
struct constmap maprcpthosts;
char *relayclient;
int relayclientlen;

int addrallowed(buf,len) char *buf; int len;
{
 int j;
 if (!flagrcpthosts) return 1;
 j = byte_rchr(buf,len,'@');
 if (j >= len) return 1;
 if (constmap(&maprcpthosts,buf + j + 1,len - j - 1)) return 1;
 for (;j < len;++j)
   if (buf[j] == '.')
     if (constmap(&maprcpthosts,buf + j,len - j)) return 1;
 return 0;
}

main()
{
 char ch;
 int i;
 unsigned long biglen;
 unsigned long len;
 int flagdos;
 int flagsenderok;
 unsigned long qp;
 char *result;

 sig_pipeignore();
 sig_alarmcatch(sigalrm);
 alarm(3600);

 if (chdir(auto_qmail) == -1) resources();

 if (control_init() == -1) resources();
 flagrcpthosts = control_readfile(&rcpthosts,"control/rcpthosts",0);
 if (flagrcpthosts == -1) resources();
 if (flagrcpthosts)
   if (!constmap_init(&maprcpthosts,rcpthosts.s,rcpthosts.len,0)) resources();
 relayclient = env_get("RELAYCLIENT");
 relayclientlen = relayclient ? str_len(relayclient) : 0;

 remotehost = env_get("TCPREMOTEHOST");
 if (!remotehost) remotehost = "unknown";
 remoteinfo = env_get("TCPREMOTEINFO");
 remoteip = env_get("TCPREMOTEIP");
 if (!remoteip) remoteip = "unknown";
 local = env_get("TCPLOCALHOST");
 if (!local) local = env_get("TCPLOCALIP");
 if (!local) local = "unknown";

 for (;;)
  {
   if (!stralloc_copys(&failure,"")) resources();
   flagsenderok = 1;

   len = getlen();
   if (len == 0) badproto();

   if (qmail_open(&qqt) == -1) resources();
   qp = qmail_qp(&qqt);

   if (substdio_get(subfdinsmall,&ch,1) < 1) dropped();
   --len;

   if (ch == 10) flagdos = 0;
   else if (ch == 13) flagdos = 1;
   else badproto();

   received(&qqt,"QMTP",local,remoteip,remotehost,remoteinfo,(char *) 0);

   /* XXX: check for loops? only if len is big? */

   if (flagdos)
     while (len > 0)
      {
       if (substdio_get(subfdinsmall,&ch,1) < 1) dropped();
       --len;
       while ((ch == 13) && len)
        {
         if (substdio_get(subfdinsmall,&ch,1) < 1) dropped();
         --len;
         if (ch == 10) break;
         qmail_put(&qqt,"\015",1);
        }
       qmail_put(&qqt,&ch,1);
      }
   else
     while (len > 0) /* XXX: could speed this up, obviously */
      {
       if (substdio_get(subfdinsmall,&ch,1) < 1) dropped();
       --len;
       qmail_put(&qqt,&ch,1);
      }
   getcomma();

   len = getlen();

   if (len >= 1000)
    {
     buf[0] = 0;
     flagsenderok = 0;
     for (i = 0;i < len;++i)
       if (substdio_get(subfdinsmall,&ch,1) < 1) dropped();
    }
   else
    {
     for (i = 0;i < len;++i)
      {
       if (substdio_get(subfdinsmall,buf + i,1) < 1) dropped();
       if (!buf[i]) flagsenderok = 0;
      }
     buf[len] = 0;
    }
   getcomma();

   qmail_from(&qqt,buf);
   if (!flagsenderok) qmail_fail(&qqt);

   biglen = getlen();
   while (biglen > 0)
    {
     if (!stralloc_append(&failure,"")) resources();

     len = 0;
     for (;;)
      {
       if (!biglen) badproto();
       if (substdio_get(subfdinsmall,&ch,1) < 1) dropped();
       --biglen;
       if (ch == ':') break;
       if (len > 200000000) resources();
       len = 10 * len + (ch - '0');
      }
     if (len >= biglen) badproto();
     if (len + relayclientlen >= 1000)
      {
       failure.s[failure.len - 1] = 'L';
       for (i = 0;i < len;++i)
         if (substdio_get(subfdinsmall,&ch,1) < 1) dropped();
      }
     else
      {
       for (i = 0;i < len;++i)
        {
         if (substdio_get(subfdinsmall,buf + i,1) < 1) dropped();
         if (!buf[i]) failure.s[failure.len - 1] = 'N';
        }
       buf[len] = 0;

       if (relayclient)
	 str_copy(buf + len,relayclient);
       else
	 if (!addrallowed(buf,len)) failure.s[failure.len - 1] = 'D';

       if (!failure.s[failure.len - 1])
	 qmail_to(&qqt,buf);
      }
     getcomma();
     biglen -= (len + 1);
    }
   getcomma();

   switch(qmail_close(&qqt))
    {
     case 0: result = 0; break;
     case QMAIL_WAITPID: result = "Zqq waitpid surprise (#4.3.0)"; break;
     case QMAIL_CRASHED: result = "Zqq crashed (#4.3.0)"; break;
     case QMAIL_USAGE: result = "Zqq usage surprise (#4.3.0)"; break;
     case QMAIL_SYS: result = "Zqq system error (#4.3.0)"; break;
     case QMAIL_READ: result = "Zqq read error (#4.3.0)"; break;
     case QMAIL_WRITE: result = "Zqq write error or disk full (#4.3.0)"; break;
     case QMAIL_NOMEM: result = "Zqq out of memory (#4.3.0)"; break;
     case QMAIL_EXECSOFT: result = "Zcould not exec qq (#4.3.0)"; break;
     case QMAIL_TIMEOUT: result = "Zqq timeout (#4.3.0)"; break;
     case QMAIL_TOOLONG: result = "Dqq toolong surprise (#5.1.3)"; break;
     default: result = "Zqq internal bug (#4.3.0)"; break;
    }

   if (!flagsenderok) result = "Dunacceptable sender (#5.1.7)";

   if (result)
     len = str_len(result);
   else
    {
     /* success! */
     len = 0;
     len += fmt_str(buf2 + len,"Kok ");
     len += fmt_ulong(buf2 + len,(unsigned long) now());
     len += fmt_str(buf2 + len," qp ");
     len += fmt_ulong(buf2 + len,qp);
     buf2[len] = 0;
     result = buf2;
    }
     
   len = fmt_ulong(buf,len);
   buf[len++] = ':';
   len += fmt_str(buf + len,result);
   buf[len++] = ',';

   for (i = 0;i < failure.len;++i)
     switch(failure.s[i])
      {
       case 0:
         if (substdio_put(subfdoutsmall,buf,len) == -1)
           dropped();
	 break;
       case 'D':
         if (substdio_puts(subfdoutsmall,"66:Dsorry, that domain isn't in my list of allowed rcpthosts (#5.7.1),") == -1)
	   dropped();
	 break;
       default:
         if (substdio_puts(subfdoutsmall,"46:Dsorry, I can't handle that recipient (#5.1.3),") == -1)
	   dropped();
	 break;
      }

   /* subfdoutsmall will be flushed when we read from the network again */
  }
}