/[pkg-mixmaster]/branches/mixmaster_2_9_STABLE/Mix/Src/rem.c
ViewVC logotype

Contents of /branches/mixmaster_2_9_STABLE/Mix/Src/rem.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 62 - (hide annotations) (download)
Thu Jan 10 23:59:16 2002 UTC (11 years, 4 months ago) by rabbi
Original Path: trunk/Mix/Src/rem.c
File MIME type: text/plain
File size: 12695 byte(s)
Patches to support the Encrypt-To directive (by Disastry).
1 rabbi 1 /* Mixmaster version 3 -- (C) 1999 Anonymizer Inc.
2    
3     Mixmaster may be redistributed and modified under certain conditions.
4     This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF
5     ANY KIND, either express or implied. See the file COPYRIGHT for
6     details.
7    
8     Process remailer messages
9 rabbi 62 $Id: rem.c,v 1.12 2002/01/10 23:59:16 rabbi Exp $ */
10 rabbi 1
11    
12     #include "mix3.h"
13     #include <stdlib.h>
14     #include <string.h>
15     #include <time.h>
16     #include <ctype.h>
17     #include <sys/types.h>
18     #include <sys/stat.h>
19     #ifdef POSIX
20     #include <unistd.h>
21     #else
22     #include <io.h>
23     #endif
24     #ifndef _MSC
25     #include <dirent.h>
26     #endif
27     #include <assert.h>
28    
29     int blockrequest(BUFFER *message);
30    
31     #define REQUESTHELP 100
32     #define REQUESTSTATS 101
33     #define REQUESTKEY 200
34     #define REQUESTCONF 201
35     #define REQUESTOPKEY 202
36     #define BLOCKREQUEST 666
37     #define DISABLED 99
38    
39     #define CPUNKMSG 1
40     #define MIXMSG 2
41    
42     int mix_decrypt(BUFFER *message)
43     {
44     int type = 0;
45     BUFFER *field, *content;
46     BUFFER *to, *subject, *replyto, *reply;
47     FILE *f;
48     BUFFER *block;
49     int err = 0;
50 rabbi 41 int quoted_printable = 0; /* is this message quoted printable encoded */
51 rabbi 1
52     mix_init(NULL);
53     field = buf_new();
54     content = buf_new();
55     to = buf_new();
56     replyto = buf_new();
57     reply = buf_new();
58     block = buf_new();
59     subject = buf_new();
60     buf_sets(subject, "Subject: Re: your mail");
61    
62     buf_rewind(message);
63    
64     f = mix_openfile(SOURCEBLOCK, "r");
65     if (f != NULL) {
66     buf_read(block, f);
67     fclose(f);
68     }
69     for (;;) {
70     err = buf_getheader(message, field, content);
71     if (err == 1) {
72     /* "::" marks for additional header lines */
73     while (buf_lookahead(message, field) == 1)
74     buf_getheader(message, field, content);
75     if (isline(field, HDRMARK))
76     continue;
77     else
78     goto hdrend;
79     }
80     if (err == -1)
81     goto hdrend;
82    
83     if ((bufieq(field, "from") || bufieq(field, "sender") || bufieq(field,"received")) &&
84     doblock(content, block, 1) != 0)
85     goto end;
86    
87     if (bufieq(field, "to"))
88     buf_cat(to, content);
89     else if (bufieq(field, "from") && replyto->length == 0)
90     /* reply to From address if no Reply-To header present */
91     buf_set(replyto, content);
92     else if (bufieq(field, "reply-to"))
93     buf_set(replyto, content);
94     else if (MIX && bufieq(field, "remailer-type") &&
95     bufileft(content, "mixmaster"))
96     type = MIXMSG;
97     else if (bufieq(field, "subject")) {
98     if (bufieq(content, "help") || bufieq(content, "remailer-help"))
99     type = REQUESTHELP;
100     else if (bufieq(content, "remailer-stats"))
101     type = REQUESTSTATS;
102     else if (bufieq(content, "remailer-key"))
103     type = REQUESTKEY;
104     else if (bufieq(content, "remailer-adminkey"))
105     type = REQUESTOPKEY;
106     else if (bufieq(content, "remailer-conf"))
107     type = REQUESTCONF;
108     else if (bufileft(content, "destination-block"))
109     type = BLOCKREQUEST;
110     else {
111     buf_sets(subject, "Subject: ");
112     if (!bufileft(content, "re:"))
113     buf_appends(subject, "Re: ");
114     buf_cat(subject, content);
115     }
116     } else if (bufieq(field, "test-to") || bufieq(field, "encrypted") ||
117     bufieq(field, "anon-to") ||
118     bufieq(field, "request-remailing-to") ||
119     bufieq(field, "remail-to") || bufieq(field, "anon-post-to") ||
120     bufieq(field, "post-to") || bufieq(field, "anon-send-to") ||
121 rabbi 62 bufieq(field, "send-to") || bufieq(field, "remix-to") ||
122     bufieq(field, "encrypt-to"))
123 rabbi 1 type = CPUNKMSG;
124 rabbi 41 else if (bufieq(field, "content-transfer-encoding")
125     && bufieq(content, "quoted-printable")) {
126     quoted_printable = 1;
127     }
128    
129 rabbi 1 }
130     hdrend:
131 rabbi 41 if (quoted_printable)
132     qp_decode_message(message);
133    
134 rabbi 1 if (type > 0 && REMAIL == 0)
135     type = DISABLED;
136     switch (type) {
137     case REQUESTHELP:
138     if (sendinfofile(HELPFILE, NULL, replyto, NULL) == -1)
139     errlog(WARNING, "No help file available.\n");
140     break;
141     case REQUESTKEY:
142     err = key(reply);
143     if (err == 0)
144     err = sendmail(reply, REMAILERNAME, replyto);
145     break;
146     case REQUESTOPKEY:
147     err = adminkey(reply);
148     if (err == 0)
149     err = sendmail(reply, REMAILERNAME, replyto);
150     break;
151     case REQUESTSTATS:
152     err = stats(reply);
153     if (err == 0)
154     err = sendmail(reply, REMAILERNAME, replyto);
155     break;
156     case REQUESTCONF:
157     err = conf(reply);
158     if (err == 0)
159     err = sendmail(reply, REMAILERNAME, replyto);
160     break;
161     case CPUNKMSG:
162     err = t1_decrypt(message);
163     if (err != 0) {
164     errlog(LOG, "Invalid type 1 message from %b\n", replyto);
165     sendinfofile(USAGEFILE, USAGELOG, replyto, NULL);
166     logmail(err == -2 ? MAILUSAGE : MAILERROR, message);
167     }
168     break;
169     case MIXMSG:
170     err = t2_decrypt(message);
171     if (err == -1) {
172     errlog(LOG, "Invalid type 2 message from %b\n", replyto);
173     sendinfofile(USAGEFILE, USAGELOG, replyto, NULL);
174     logmail(MAILERROR, message);
175     }
176     break;
177     case BLOCKREQUEST:
178     blockrequest(message);
179     logmail(MAILBLOCK, message);
180     break;
181     case DISABLED:
182     errlog(ERRORMSG, "Remailer is disabled.\n");
183     buf_sets(reply, "Subject: remailer error\n\nThe remailer is disabled.\n");
184     sendmail(reply, REMAILERNAME, replyto);
185     logmail(MAILERROR, message);
186     break;
187     default:
188     if (strifind
189     (replyto->data, "mailer-daemon")) {
190     errlog(LOG, "Bounce mail from %b\n", replyto);
191     logmail(MAILBOUNCE, message);
192     } else if (bufifind(to, REMAILERADDR) && blockrequest(message))
193     logmail(MAILBLOCK, message);
194     else if (!AUTOREPLY)
195     logmail(MAILBOX, message);
196     else if (bufifind(to, REMAILERADDR)) {
197     errlog(LOG, "Non-remailer message from %b\n", replyto);
198     sendinfofile(USAGEFILE, USAGELOG, replyto, NULL);
199     logmail(MAILUSAGE, message);
200     } else if (bufifind(to, COMPLAINTS)) {
201     errlog(WARNING, "Abuse complaint from %b\n", replyto);
202     sendinfofile(ABUSEFILE, NULL, replyto, subject);
203     logmail(MAILABUSE, message);
204     } else if (ANONADDR[0] && bufifind(to, ANONADDR)) {
205     errlog(LOG, "Reply to anonymous message from %b\n", replyto);
206     sendinfofile(REPLYFILE, NULL, replyto, subject);
207     logmail(MAILANON, message);
208     } else {
209     errlog(DEBUGINFO, "Mail from %b\n", replyto);
210     logmail(MAILBOX, message);
211     }
212     err = 1;
213     }
214     end:
215     buf_free(field);
216     buf_free(content);
217     buf_free(to);
218     buf_free(replyto);
219     buf_free(reply);
220     buf_free(block);
221     buf_free(subject);
222     return (err);
223     }
224    
225     int t2_decrypt(BUFFER *in)
226     {
227     int err = 0;
228     BUFFER *msg;
229    
230     msg = buf_new();
231     do {
232     err = mix_dearmor(in, msg);
233     if (err != -1) {
234     if (v3_magic(msg->data))
235     err = mix3_decrypt(msg);
236     else
237     err = mix2_decrypt(msg);
238     }
239     }
240     while (in->ptr + 1000 < in->length); /* accept several packets in one message */
241    
242     buf_free(msg);
243     return (err);
244     }
245    
246     int mix_pool(BUFFER *msg, int type, long latent)
247     {
248     char path[PATHMAX], pathtmp[PATHMAX];
249     FILE *f;
250     int err = -1;
251    
252     f = pool_new(latent > 0 ? "lat" : "msg", pathtmp, path);
253     if (f != NULL) {
254     if (latent > 0)
255     fprintf(f, "%d %ld\n", type, latent + time(NULL));
256     else
257     fprintf(f, "%d 0\n", type);
258     err = buf_write(msg, f);
259     fclose(f);
260     }
261     if (err == 0) {
262     rename(pathtmp, path);
263     errlog(DEBUGINFO, "Added message to pool.\n");
264     }
265     return (err);
266     }
267    
268     int pool_packetfile(char *fname, BUFFER *mid, int packetnum)
269     /* create a filename */
270     {
271     #ifdef SHORTNAMES
272 rabbi 11 sprintf(fname, "%s%cp%02x%02x%02x%01x.%02x", POOLDIR, DIRSEP,
273 rabbi 1 mid->data[0], mid->data[1], mid->data[2], mid->data[3] & 15,
274     packetnum);
275     #else
276 rabbi 11 sprintf(fname, "%s%cp%02x%02x%02x%02x%02x%02x%01x", POOLDIR, DIRSEP,
277 rabbi 1 packetnum, mid->data[0], mid->data[1], mid->data[2], mid->data[3],
278     mid->data[4], mid->data[5] & 15);
279     #endif
280     return (0);
281     }
282    
283     void pool_packetexp(void)
284     {
285     DIR *d;
286     struct dirent *e;
287     struct stat sb;
288    
289     d = opendir(POOLDIR);
290     if (d != NULL)
291     for (;;) {
292     e = readdir(d);
293     if (e == NULL)
294     break;
295     if (e->d_name[0] == 'p') {
296     if (stat(e->d_name, &sb) == 0 &&
297     time(NULL) - sb.st_mtime > PACKETEXP) {
298     errlog(NOTICE, "Expiring partial message %s.\n",
299     e->d_name);
300     unlink(e->d_name);
301     }
302     }
303     }
304     closedir(d);
305     }
306    
307     void logmail(char *mailbox, BUFFER *message)
308     {
309 rabbi 6 time_t t;
310     struct tm *tc;
311     char line[LINELEN];
312    
313 rabbi 1 /* mailbox is "|program", "user@host", "stdout" or "filename" */
314     buf_rewind(message);
315     if (mailbox[0] == '\0') /* default action */
316     mailbox = MAILBOX;
317     if (strieq(mailbox, "stdout"))
318     buf_write(message, stdout);
319     else if (mailbox[0] == '|') {
320     FILE *p;
321    
322     errlog(DEBUGINFO, "Piping message to %s.", mailbox + 1);
323     p = openpipe(mailbox + 1);
324     if (p != NULL) {
325     buf_write(message, p);
326     closepipe(p);
327     }
328     } else if (strchr(mailbox, '@')) {
329     BUFFER *field, *content;
330    
331     field = buf_new();
332     content = buf_new();
333     while (buf_getheader(message, field, content) == 0)
334     if (bufieq(field, "x-loop") && bufifind(content, REMAILERADDR)) {
335     errlog(WARNING, "Loop detected! Message not sent to %s.\n", mailbox);
336     goto isloop;
337     }
338     buf_sets(content, mailbox);
339     sendmail(message, NULL, content);
340     isloop:
341     buf_free(field);
342     buf_free(content);
343     } else {
344     FILE *mbox;
345    
346     mbox = mix_openfile(mailbox, "a");
347     if (mbox == NULL) {
348     errlog(ERRORMSG, "Can't write to mail folder %s\n", mailbox);
349     return;
350     }
351     lock(mbox);
352 rabbi 6 if (!bufileft(message, "From ")) {
353     t = time(NULL);
354     tc = localtime(&t);
355     strftime(line, LINELEN, "From Mixmaster %a %b %d %H:%M:%S %Y\n", tc);
356     fprintf(mbox, line);
357     }
358 rabbi 1 buf_write(message, mbox);
359     fprintf(mbox, "\n");
360     unlock(mbox);
361     fclose(mbox);
362     }
363     }
364    
365     int blockrequest(BUFFER *message)
366     {
367 rabbi 59 int request = 0;
368 rabbi 1 BUFFER *from, *line, *field, *content, *addr;
369     FILE *f;
370     char *destblklst = (char *)malloc( strlen(DESTBLOCK)+1 );
371     char *destblk;
372    
373     from = buf_new();
374     line = buf_new();
375     field = buf_new();
376     content = buf_new();
377     addr = buf_new();
378    
379     buf_rewind(message);
380     while (buf_getheader(message, field, content) == 0)
381     if (bufieq(field, "from"))
382     buf_set(from, content);
383     else if (bufieq(field, "subject"))
384 rabbi 30 buf_cat(message, content);
385     /* Append the subject to the message body so destination block requests
386     in the subject line work too (we process the body a few lines down) */
387 rabbi 1 while (buf_getline(message, line) != -1)
388     if (bufifind(line, "destination-block")) {
389     buf_clear(addr);
390     request = 1;
391 rabbi 52 {
392 rabbi 1 int c = 0;
393    
394     while (!strileft(line->data + line->ptr, "block"))
395     line->ptr++;
396 rabbi 52 while (c != ' ' && c != -1)
397 rabbi 1 c = tolower(buf_getc(line));
398     while (c == ' ')
399     c = buf_getc(line);
400 rabbi 52 if (c != -1)
401     do {
402     buf_appendc(addr, c);
403     c = buf_getc(line);
404     } while (c > ' ');
405 rabbi 48 }
406 rabbi 55 if (addr->length == 0) {
407 rabbi 54 rfc822_addr (from, addr);
408 rabbi 55 buf_chop(addr);
409     }
410 rabbi 1 if (bufieq(addr, REMAILERADDR)) {
411     errlog(LOG, "Ignoring blocking request for %b from %b.\n", addr, from);
412     return (2);
413     }
414     if (buf_ieq(addr, from))
415     errlog(NOTICE, "Blocking request for %b\n", addr);
416     else
417     errlog(NOTICE, "Blocking request for %b from %b\n", addr, from);
418     if (AUTOBLOCK) {
419     buf_clear(line);
420     rfc822_addr(addr, line);
421 rabbi 59 if (line->length == 0) {
422     errlog(LOG, "Nothing to block after rfc822_addr().\n");
423 rabbi 1 } else
424 rabbi 59 if (bufleft(line, "/")) {
425     errlog(LOG, "Ignoring blocking request: %b is a regex.\n", addr);
426     } else {
427     if (strchr(line->data, '@') && strchr(strchr(line->data, '@'), '.')) {
428     strcpy( destblklst, DESTBLOCK );
429     destblk = strtok( destblklst, " " );
430     f = mix_openfile( destblk, "a" );
431     free( destblklst );
432     if (f != NULL) {
433     lock(f);
434    
435     buf_chop(line);
436     sendinfofile(BLOCKFILE, NULL, line, NULL);
437     if (line->length) {
438     fprintf(f, "%s\n", line->data);
439     } else
440     errlog(NOTICE, "%b already blocked.\n", addr);
441     unlock(f);
442     fclose(f);
443     } else
444     errlog(ERRORMSG, "Can't write to %s.\n", DESTBLOCK);
445     } else
446     errlog(WARNING, "Invalid address not added to %s: %b\n", DESTBLOCK,
447     addr);
448     }
449 rabbi 1 }
450     }
451     return (request);
452     }
453    
454    
455     int idexp(void)
456     {
457     FILE *f;
458     BUFFER *b;
459     long now, then;
460     char line[LINELEN];
461     LOCK *i;
462    
463     if (IDEXP == 0)
464     return (0);
465    
466     b = buf_new();
467     f = mix_openfile(IDLOG, "r");
468     if (f == NULL)
469     return (-1);
470     i = lockfile(IDLOG);
471     now = time(NULL);
472     fgets(line, sizeof(line), f); /* replace first line */
473     while (fgets(line, sizeof(line), f) != NULL) {
474     sscanf(line, "%*s %ld", &then);
475     if (now - then < IDEXP)
476     buf_appends(b, line);
477     }
478     fclose(f);
479     f = mix_openfile(IDLOG, "w");
480     if (f != NULL) {
481     fprintf(f, "expired %ld\n", now - IDEXP);
482     buf_write(b, f);
483     fclose(f);
484     }
485     unlockfile(i);
486     return (0);
487     }

  ViewVC Help
Powered by ViewVC 1.1.5