File: qmail-pop3d.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 (397 lines) | stat: -rw-r--r-- 7,951 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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
#include <sys/types.h>
#include <sys/stat.h>
#include "direntry.h"
#include "sig.h"
#include "getln.h"
#include "stralloc.h"
#include "substdio.h"
#include "alloc.h"
#include "datetime.h"
#include "prot.h"
#include "open.h"
#include "prioq.h"
#include "scan.h"
#include "fmt.h"
#include "error.h"
#include "str.h"
#include "exit.h"
#include "now.h"
#include "readwrite.h"

int timeout = 1200;

char ssoutbuf[1024];
substdio ssout = SUBSTDIO_FDBUF(write,1,ssoutbuf,sizeof(ssoutbuf));

int timeoutread(fd,buf,n) int fd; char *buf; int n;
{
 int r; int saveerrno;
 alarm(timeout);
 r = read(fd,buf,n); saveerrno = errno;
 alarm(0);
 errno = saveerrno; return r;
}

char ssinbuf[128];
substdio ssin = SUBSTDIO_FDBUF(timeoutread,0,ssinbuf,sizeof(ssinbuf));


void die() { _exit(0); }
void puts(s) char *s;
{
 if (substdio_puts(&ssout,s) == -1) die();
}
void flush()
{
 if (substdio_flush(&ssout) == -1) die();
}
void err(s) char *s;
{
 puts("-ERR ");
 puts(s);
 puts("\r\n");
 if (substdio_flush(&ssout) == -1) die();
}
void die_nomem() { err("out of memory"); die(); }
void die_prot() { err("protection problem"); die(); }
void die_nomaildir() { err("this user has no $HOME/Maildir"); die(); }

void err_syntax() { err("syntax error"); }
void err_unimpl() { err("unimplemented"); }
void err_deleted() { err("already deleted"); }
void err_nozero() { err("messages are counted from 1"); }
void err_toobig() { err("not that many messages"); }
void err_nosuch() { err("unable to open that message"); }
void err_nounlink() { err("unable to unlink all deleted messages"); }

void okay() { puts("+OK \r\n"); flush(); }
void pop3_last() { puts("+OK 0\r\n"); flush(); }


stralloc dataline = {0};

stralloc filenames = {0};
prioq pq = {0};
stralloc newname = {0};

struct message
 {
  int flagdeleted;
  unsigned long size;
  char *fn;
 }
*m;
int numm;

substdio ssmsg; char ssmsgbuf[1024];


void blast(ssfrom,limit)
substdio *ssfrom;
unsigned long limit;
{
 int match;
 int inheaders = 1;

 for (;;)
  {
   if (getln(ssfrom,&dataline,&match,'\n') != 0) die();
   if (!match && !dataline.len) break;
   if (match) --dataline.len; /* no way to pass this info over POP */
   if (limit) if (!inheaders) if (!--limit) break;
   if (!dataline.len)
     inheaders = 0;
   else
     if (dataline.s[0] == '.')
       substdio_put(&ssout,".",1);
   if (substdio_put(&ssout,dataline.s,dataline.len) == -1) die();
   if (substdio_put(&ssout,"\r\n",2) == -1) die();
   if (!match) break;
  }
 if (substdio_put(&ssout,"\r\n.\r\n",5) == -1) die();
 if (substdio_flush(&ssout) == -1) die();
}

void getlist()
{
 unsigned long pos;
 datetime_sec time;
 DIR *dir;
 direntry *d;
 struct prioq_elt pe;
 struct stat st;
 int i;

 numm = 0;

 time = now();

 if (dir = opendir("tmp"))
  {
   while (d = readdir(dir))
    {
     if (str_equal(d->d_name,".")) continue;
     if (str_equal(d->d_name,"..")) continue;
     if (!stralloc_copys(&newname,"tmp/")) die_nomem();
     if (!stralloc_cats(&newname,d->d_name)) die_nomem();
     if (!stralloc_0(&newname)) die_nomem();
     if (stat(newname.s,&st) == 0)
       if (time > st.st_atime + 129600)
	 unlink(newname.s);
    }
   closedir(dir);
  }

 if (!stralloc_copys(&filenames,"")) die_nomem();

 if (dir = opendir("new"))
  {
   while (d = readdir(dir))
    {
     if (str_equal(d->d_name,".")) continue;
     if (str_equal(d->d_name,"..")) continue;
     pos = filenames.len;
     if (!stralloc_cats(&filenames,"new/")) die_nomem();
     if (!stralloc_cats(&filenames,d->d_name)) die_nomem();
     if (!stralloc_0(&filenames)) die_nomem();
     if (stat(filenames.s + pos,&st) == 0)
      {
       pe.dt = st.st_mtime;
       pe.id = pos;
       if (!prioq_insert(&pq,&pe)) die_nomem();
       ++numm;
      }
    }
   closedir(dir);
  }

 if (dir = opendir("cur"))
  {
   while (d = readdir(dir))
    {
     if (str_equal(d->d_name,".")) continue;
     if (str_equal(d->d_name,"..")) continue;
     pos = filenames.len;
     if (!stralloc_cats(&filenames,"cur/")) die_nomem();
     if (!stralloc_cats(&filenames,d->d_name)) die_nomem();
     if (!stralloc_0(&filenames)) die_nomem();
     if (stat(filenames.s + pos,&st) == 0)
      {
       pe.dt = st.st_mtime;
       pe.id = pos;
       if (!prioq_insert(&pq,&pe)) die_nomem();
       ++numm;
      }
    }
   closedir(dir);
  }

 m = (struct message *) alloc(numm * sizeof(struct message));
 if (!m) die_nomem();

 for (i = 0;i < numm;++i)
  {
   if (!prioq_min(&pq,&pe)) { numm = i; break; }
   prioq_delmin(&pq);
   m[i].fn = filenames.s + pe.id;
   m[i].flagdeleted = 0;
   if (stat(m[i].fn,&st) == -1)
     m[i].size = 0;
   else
     m[i].size = st.st_size;
  }
}

char foo[FMT_ULONG];

void printint(u) unsigned int u;
{
 foo[fmt_uint(foo,u)] = 0;
 puts(foo);
 puts(" ");
}

void printlong(u) unsigned long u;
{
 foo[fmt_uint(foo,u)] = 0;
 puts(foo);
 puts("\r\n");
}

void printfn(fn) char *fn;
{
 puts(fn + 4);
 puts("\r\n");
}

void pop3_stat()
{
 int i;
 unsigned long total;

 total = 0;
 for (i = 0;i < numm;++i) if (!m[i].flagdeleted) total += m[i].size;
 puts("+OK ");
 printint(numm);
 printlong(total);
 flush();
}

void pop3_rset()
{
 int i;
 for (i = 0;i < numm;++i) m[i].flagdeleted = 0;
 okay();
}

void pop3_quit()
{
 int i;
 for (i = 0;i < numm;++i)
   if (m[i].flagdeleted)
     if (unlink(m[i].fn) == -1) err_nounlink();
 okay();
 die();
}

int msgno(arg) char *arg;
{
 unsigned long u;
 if (!arg) { err_syntax(); return -1; }
 if (!scan_ulong(arg,&u)) { err_syntax(); return -1; }
 if (!u) { err_nozero(); return -1; }
 --u;
 if (u >= numm) { err_toobig(); return -1; }
 if (m[u].flagdeleted) { err_deleted(); return -1; }
 return u;
}

void pop3_dele(arg) char *arg;
{
 int i;

 i = msgno(arg);
 if (i == -1) return;
 m[i].flagdeleted = 1;
 okay();
}

void dolisting(arg,flaguidl) char *arg; int flaguidl;
{
 unsigned int i;

 if (arg)
  {
   i = msgno(arg);
   if (i == -1) return;
   puts("+OK ");
   printint(i + 1);
   if (flaguidl) printfn(m[i].fn); else printlong(m[i].size);
  }
 else
  {
   okay();

   for (i = 0;i < numm;++i)
     if (!m[i].flagdeleted)
      {
       printint(i + 1);
       if (flaguidl) printfn(m[i].fn); else printlong(m[i].size);
      }
   puts(".\r\n");
  }
 flush();
}

void pop3_uidl(arg) char *arg; { dolisting(arg,1); }
void pop3_list(arg) char *arg; { dolisting(arg,0); }

void pop3_top(arg) char *arg;
{
 int i;
 unsigned long limit;
 int fd;

 i = msgno(arg);
 if (i == -1) return;

 arg += scan_ulong(arg,&limit);
 while (*arg == ' ') ++arg;
 if (scan_ulong(arg,&limit)) ++limit; else limit = 0;

 fd = open_read(m[i].fn);
 if (fd == -1) { err_nosuch(); return; }
 okay();
 substdio_fdbuf(&ssmsg,read,fd,ssmsgbuf,sizeof(ssmsgbuf));
 blast(&ssmsg,limit);
 close(fd);
}

static struct { void (*fun)(); char *text; } pop3cmd[] = {
  { pop3_quit, "quit" }
, { pop3_stat, "stat" }
, { pop3_list, "list" }
, { pop3_uidl, "uidl" }
, { pop3_dele, "dele" }
, { pop3_top, "retr" }
, { pop3_rset, "rset" }
, { pop3_last, "last" }
, { pop3_top, "top" }
, { okay, "noop" }
, { 0, 0 }
};

void doit(cmd)
char *cmd;
{
 int i;
 int j;
 char ch;

 for (i = 0;pop3cmd[i].fun;++i)
  {
   for (j = 0;ch = pop3cmd[i].text[j];++j)
     if ((cmd[j] != ch) && (cmd[j] != ch - 32))
       break;
   if (!ch)
     if (!cmd[j] || (cmd[j] == ' '))
      {
       while (cmd[j] == ' ') ++j;
       if (!cmd[j])
         pop3cmd[i].fun((char *) 0);
       else
         pop3cmd[i].fun(cmd + j);
       return;
      }
  }
 err_unimpl();
}

void main(argc,argv)
int argc;
char **argv;
{
 static stralloc cmd = {0};
 int match;

 sig_alarmcatch(die);
 sig_pipeignore();

 if (!argv[1]) die_nomaildir();
 if (chdir(argv[1]) == -1) die_nomaildir();

 getlist();

 okay();

 for (;;)
  {
   if (getln(&ssin,&cmd,&match,'\n') == -1) die();
   if (!match) die();
   if (cmd.len == 0) die();
   if (cmd.s[--cmd.len] != '\n') die();
   if ((cmd.len > 0) && (cmd.s[cmd.len - 1] == '\r')) --cmd.len;
   cmd.s[cmd.len++] = 0;
   doit(cmd.s);
  }
}