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

Contents of /trunk/Mix/Src/rem.c

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.5