File: rem1.c

package info (click to toggle)
mixmaster 3.0.0-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 1,272 kB
  • ctags: 1,031
  • sloc: ansic: 18,669; sh: 1,448; yacc: 698; perl: 314; makefile: 160
file content (599 lines) | stat: -rw-r--r-- 15,397 bytes parent folder | download | duplicates (5)
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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
/* Mixmaster version 3.0  --  (C) 1999 - 2006 Anonymizer Inc. and others.

   Mixmaster may be redistributed and modified under certain conditions.
   This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF
   ANY KIND, either express or implied. See the file COPYRIGHT for
   details.

   Process Cypherpunk remailer messages
   $Id: rem1.c 934 2006-06-24 13:40:39Z rabbi $ */


#include "mix3.h"
#include <ctype.h>
#include <time.h>
#include <string.h>
#include <assert.h>
#include <stdlib.h>

static int t1msg(BUFFER *in, int hdr);

int isline(BUFFER *line, char *text)
{
  int i;

  if (!bufileft(line, text))
    return (0);

    for (i = strlen(text); i < line->length; i++)
      if (!isspace(line->data[i]))
	return(0);
    return(1);
}

int t1_decrypt(BUFFER *in)
{
  int ret;

  buf_rewind(in);
  if (TYPE1[0] == '\0')
    ret = t1msg(in, 1);
  else {
    FILE *f;

    f = openpipe(TYPE1);
    if (f == NULL)
      return -1;
    buf_write(in, f);
    ret = closepipe(f);
  }
  if (ret == 0)
    stats_log(1);
  return (ret);
}

#ifdef USE_IDEA
void t1_esub(BUFFER *esub, BUFFER *subject)
{
  BUFFER *iv, *out;
  char hex[33];

  iv = buf_new();
  out = buf_new();

  buf_appendrnd(iv, 8);
  id_encode(iv->data, hex);
  buf_append(out, hex, 16);

  digest_md5(esub, esub);
  digest_md5(subject, subject);
  buf_ideacrypt(subject, esub, iv, ENCRYPT);
  id_encode(subject->data, hex);
  buf_appends(out, hex);
  buf_move(subject, out);
  buf_free(iv);
  buf_free(out);
}
#endif /* USE_IDEA */

#define N(X) (isdigit(X) ? (X)-'0' : 0)

static int readnum(BUFFER *b, int f)
{
  int num = 0;

  if (b->length > 0)
    sscanf(b->data, "%d", &num);
  num *= f;
  if (strchr(b->data, 'r'))
    num = rnd_number(num) + 1;
  return (num);
}

static int readdate(BUFFER *b)
{
  int num = -1;

  if (b->length > 0)
    num = parsedate(b->data);
  return (num);
}

static int reached_maxcount(BUFFER *md, int maxcount)
{
  FILE *f;
  char temp[LINELEN];
  int count = 0;
  int err = 0;
  long then;
  time_t now = time(NULL);

  assert(md->length > 0);

  encode(md, 0);

  f = mix_openfile(PGPMAXCOUNT, "a+"); /* create file if it does not exist */
  fseek(f,0,SEEK_SET);
  if (f == NULL) {
    errlog(ERRORMSG, "Can't open %s!\n", PGPMAXCOUNT);
    return (-1);
  }
  lock(f);
  while (fgets(temp, sizeof(temp), f) != NULL)
    if (sscanf(temp, "%ld", &then) &&
	(then >= now - SECONDSPERDAY) &&
	strstr (temp, md->data))
      count++;

  if (count > maxcount)
    err = 1;
  else
    fprintf(f, "%ld %s\n", (long) time(NULL), md->data);

  unlock(f);
  fclose(f);
  return (err);
}

static int t1msg(BUFFER *in, int hdr)
     /* hdr = 1: mail header, hdr = 2: pasted header, hdr = 0: ignore */
{
  BUFFER *field, *content, *line;
  BUFFER *cutmarks, *to, *newsgroups, *ek, *ekdes, *ekcast, *esub, *subject;
  BUFFER *temp, *header, *out;
  BUFFER *test, *testto, *remixto;
  BUFFER *digest;
  int err = 0;
  int encrypted = 0;
  int type = -1;
  int latent = 0;
  int remix = 0, repgp = 0;
  int inflate = 0;
  int maxsize = -1;
  int maxcount = -1;
  int maxdate = -2; /* -2 not used, -1 parse error */

  field = buf_new();
  content = buf_new();
  line = buf_new();
  to = buf_new();
  remixto = buf_new();
  cutmarks = buf_new();
  newsgroups = buf_new();
  ek = buf_new();
  ekdes = buf_new();
  ekcast = buf_new();
  esub = buf_new();
  subject = buf_new();
  temp = buf_new();
  header = buf_new();
  out = buf_new();
  test = buf_new();
  testto = buf_new();
  digest = buf_new();

  if (REMIX == 1)
    remix = 2;
  if (!UNENCRYPTED)
    encrypted = -1;

header:
  while (buf_getheader(in, field, content) == 0) {
    if (header->length == 0 && bufieq(content, ":"))	/* HDRMARK */
      hdr = 2;

    if (bufieq(field, "test-to"))
      buf_set(testto, content);
    else if (PGP && bufieq(field, "encrypted"))
      encrypted = 1;
    else if (bufieq(field, "remix-to")) {
      remix = 1; repgp = 0;
      buf_set(remixto, content);
      if (type == -1)
	type = MSG_MAIL;
    } else if (bufieq(field, "encrypt-to")) {
      repgp = remix = 1;
      buf_set(remixto, content);
      if (type == -1)
	type = MSG_MAIL;
    } else if (bufieq(field, "anon-to") ||
	       bufieq(field, "request-remailing-to") ||
	       bufieq(field, "remail-to") ||
	       bufieq(field, "anon-send-to")) {
      if (bufieq(field, "remail-to"))
	repgp = remix = 0;
      if (to->length > 0)
	buf_appendc(to, ',');
      buf_cat(to, content);
      if (type == -1)
	type = MSG_MAIL;
    } else if (bufieq(field, "anon-post-to") || bufieq(field, "post-to")) {
      if (newsgroups->length > 0)
	buf_appendc(newsgroups, ',');
      buf_cat(newsgroups, content);
      type = MSG_POST;
    } else if (bufieq(field, "cutmarks"))
      buf_set(cutmarks, content);
    else if (bufieq(field, "latent-time")) {
      byte *q;
      int l;

      q = content->data;
      l = strlen(q);
      latent = 0;
      if (q[0] == '+')
	q++;
      if (l >= 5 && q[2] == ':')
	latent = 600 * N(q[0]) + 60 * N(q[1]) + 10 * N(q[3]) + N(q[4]);
      else if (l >= 4 && q[1] == ':')
	latent = 60 * N(q[0]) + 10 * N(q[2]) + N(q[3]);
      else if (l >= 3 && q[0] == ':')
	latent = 10 * N(q[1]) + N(q[2]);
      if (!bufleft(content, "+")) {
	time_t now;

	time(&now);
	latent -= localtime(&now)->tm_hour * 60;
	if (latent < 0)
	  latent += 24 * 60;
      }
      if (q[l - 1] == 'r')
	latent = rnd_number(latent);
    } else if (bufieq(field, "null"))
      type = MSG_NULL;
#ifdef USE_IDEA
    else if (bufieq(field, "encrypt-key") || bufieq(field, "encrypt-idea"))
      buf_set(ek, content);
#else
    else if (bufieq(field, "encrypt-key") || bufieq(field, "encrypt-idea"))
      buf_set(ekdes, content);
#endif
    else if (bufieq(field, "encrypt-des") || bufieq(field, "encrypt-3des"))
      buf_set(ekdes, content);
    else if (bufieq(field, "encrypt-cast") || bufieq(field, "encrypt-cast5"))
      buf_set(ekcast, content);
    else if (bufieq(field, "encrypt-subject"))
      buf_set(esub, content);
    else if (bufieq(field, "inflate")) {
      inflate = readnum(content, 1024);
      if (inflate > INFLATEMAX * 1024)
	inflate = INFLATEMAX * 1024;
    } else if (bufieq(field, "rand-hop")) {
      int randhops, i;
      randhops = readnum(content, 1);
      if (randhops > MAXRANDHOPS)
	randhops = MAXRANDHOPS;
      buf_clear(temp);
      if (remixto->length)
	 buf_move(temp, remixto);
      for (i = 0; i < randhops; i++) {
	if (remixto->length > 0)
	  buf_appendc(remixto, ',');
	buf_appendc(remixto, '*');
      }
      if (temp->length) {
	buf_appendc(remixto, ',');
	buf_cat(remixto, temp);
      }
    } else if (bufieq(field, "max-size") || bufieq(field, "maxsize"))
      maxsize = readnum(content, 1024);
    else if (bufieq(field, "max-count") || bufieq(field, "maxcount"))
      maxcount = readnum(content, 1);
    else if (bufieq(field, "max-date") || bufieq(field, "maxdate"))
      maxdate = readdate(content);
#if USE_NSUB
    else if (bufieq(field, "subject"))
      buf_set(subject, content);
#endif /* USE_NSUB */
  }

  if (cutmarks->length > 0) {
    BUFFER *cut;

    cut = buf_new();
    buf_clear(temp);

    while ((err = buf_getline(in, line)) != -1 && !buf_eq(line, cutmarks)) {
      buf_cat(temp, line);
      buf_nl(temp);
    }
    while (err != -1) {
      err = buf_getline(in, line);
      if (err == -1 || buf_eq(line, cutmarks)) {
	t1msg(cut, 0);
	buf_clear(cut);
      } else {
	buf_cat(cut, line);
	buf_nl(cut);
      }
    }
    buf_move(in, temp);
    buf_clear(cutmarks);
  }
  if (encrypted == 1) {
#ifdef USE_PGP
    err = pgp_dearmor(in, temp);
    if (err == 0) {
      BUFFER *pass;
      digest_sha1(temp, digest);

      pass = buf_new();
      buf_sets(pass, PASSPHRASE);
      err = pgp_decrypt(temp, pass, NULL, NULL, NULL);
      buf_free(pass);
    }
    if (err != -1 && temp->length == 0) {
      errlog(ERRORMSG, "Empty PGP message.\n");
      err = -1;
      goto end;
    }
    if (err != -1) {
      buf_rest(temp, in);	/* dangerous, but required for reply blocks */
      buf_move(in, temp);
      encrypted = 0;
      hdr = 0;
      goto header;
    }
#endif /* USE_PGP */
    if (testto->length == 0)
      errlog(ERRORMSG, "Can't decrypt PGP message.\n");
    buf_appends(test, "Can't decrypt PGP message.\n");
  }
  while ((err = buf_lookahead(in, line)) == 1)
    buf_getline(in, line);
#if 0
  if (err == -1)
    goto end;
#endif /* 0 */

  if (isline(line, HDRMARK) && (hdr == 0 || hdr == 1)) {
    buf_getline(in, NULL);
    hdr = 2;
    goto header;
  } else if (isline(line, HASHMARK)) {
    buf_getline(in, NULL);
    for (;;) {
      if (buf_lookahead(in, line) == 0 && bufileft(line, "subject:")) {
	buf_getheader(in, field, content);
	buf_set(subject, content);
      }
      if (buf_getline(in, line) != 0)
	break;
      buf_cat(header, line);
      buf_nl(header);
    }
  }
  if (encrypted == -1) {
    if (testto->length == 0)
      errlog(LOG, "Unencrypted message detected.\n");
    buf_appends(test, "Unencrypted message detected.\n");
    err = -2;
    goto end;
  }
  if (maxdate == -1) {
    if (testto->length == 0)
      errlog(LOG, "Could not parse Max-Date: header.\n");
    buf_appends(test, "Could not parse Max-Date: header.\n");
    err = -2;
    goto end;
  } else if (maxdate >= 0 && maxdate <= time(NULL)) {
    if (testto->length == 0)
      errlog(LOG, "Message is expired.\n");
    buf_appends(test, "Message is expired.\n");
    err = -2;
    goto end;
  }
  if (maxsize >= 0 && in->length >= maxsize) {
    if (testto->length == 0)
      errlog(LOG, "Message Size exceeds Max-Size.\n");
    buf_appends(test, "Message Size exceeds Max-Size.\n");
    err = -2;
    goto end;
  }
  if (maxcount >= 0) {
    if (digest->length == 0) {
      if (testto->length == 0)
	errlog(LOG, "Max-Count yet not encrypted.\n");
      buf_appends(test, "Max-Count yet not encrypted.\n");
      err = -2;
      goto end;
    }
    if (reached_maxcount(digest, maxcount)) {
      if (testto->length == 0)
	errlog(LOG, "Max-Count reached - discarding message.\n");
      buf_appends(test, "Max-Count reached - discarding message.\n");
      err = -2;
      goto end;
    }
  }

  if (type == MSG_POST && subject->length == 0)
    buf_sets(subject, "(no subject)");

  if (to->length > 0)
    buf_appendf(out, "To: %b\n", to);
  else if (remixto->length > 0)
    buf_appendf(out, "To: %b\n", remixto);
  if (newsgroups->length > 0)
    buf_appendf(out, "Newsgroups: %b\n", newsgroups);
  if (subject->length > 0) {
#ifdef USE_IDEA
    if (esub->length > 0)
      t1_esub(esub, subject);
#endif /* USE_IDEA */
    buf_appendf(out, "Subject: %b\n", subject);
  }
  buf_cat(out, header);
  buf_nl(out);

#if 0
  inflate -= in->length;
#endif /* 0 */
  if (inflate > 0) {
    buf_setrnd(temp, inflate * 3 / 4);
    encode(temp, 64);
    buf_appends(in, "\n-----BEGIN GARBAGE-----\n");
    buf_cat(in, temp);
    buf_appends(in, "-----END GARBAGE-----\n");
  }

  if (!(ek->length || ekdes->length || ekcast->length))
    buf_rest(out, in);
  else {
    err = 0;
    buf_clear(temp);
    while (buf_getline(in, line) != -1) {
      if (isline(line, EKMARK)) {
	buf_cat(out, temp);
	buf_clear(temp);
	buf_rest(temp, in);
	break;
      }
      else {
	buf_cat(temp, line);
	buf_nl(temp);
      }
    }
#ifdef USE_PGP
    if (ekcast->length) {
      err = pgp_encrypt(PGP_CONVCAST | PGP_TEXT, temp, ekcast, NULL, NULL,
			NULL, NULL);
      buf_clear(ekcast);
    }
    if (ekdes->length) {
      err = pgp_encrypt(PGP_CONV3DES | PGP_TEXT, temp, ekdes, NULL, NULL,
			NULL, NULL);
      buf_clear(ekdes);
    }
    if (ek->length) {
      err = pgp_encrypt(PGP_CONVENTIONAL | PGP_TEXT, temp, ek, NULL, NULL,
			NULL, NULL);
      buf_clear(ek);
    }
    buf_appends(out, EKMARK);
    buf_nl(out);
    buf_cat(out, temp);
#else /* end of USE_PGP */
    err = -1;
#endif /* Else if not USE_PGP */
  }

  if (type == -1) {
    buf_appends(test, "No destination.\n");
    err = -1;
  }

end:
  if (testto->length) {
    BUFFER *report;
    int i;

    report = buf_new();
    buf_sets(report,
	     "Subject: remailer test report\n\nThis is an automated response to the test message you sent to ");
    buf_appends(report, SHORTNAME);
    buf_appends(report, ".\nYour test message results follow:\n\n");
    buf_appends(report, remailer_type);
    buf_appends(report, VERSION);
    buf_appends(report, "\n\n");
    if (err == 0) {
      err = filtermsg(out);
      if (err == -1)
	buf_appends(report, "This remailer cannot deliver the message.\n\n");
      else {
	buf_appends(report, "Valid ");
	buf_appends(report, type == MSG_POST ? "Usenet" : "mail");
	buf_appends(report, " message.\n");
	if (remixto->length) {
	  if (remix && MIX)
	    buf_appends(report, "Delivery via Mixmaster: ");
	  else if (remix)
	    buf_appends(report, "Error! Can't remix: ");
	  else
	    buf_appends(report, "Delivery via Cypherpunk remailer: ");
	  buf_cat(report, remixto);
	  buf_nl(report);
	}
	else if (type == MSG_POST && strchr(NEWS, '@') && !strchr(NEWS, ' ')) {
	  buf_appendf(report, "News gateway: %s\n", NEWS);
	}
	buf_appends(report,
		    "\n=========================================================================\nThe first 20 lines of the message follow:\n");
	if (err != 1)
	  buf_appendf(report, "From: %s\n", ANONNAME);
	if (type == MSG_POST && ORGANIZATION[0] != '\0')
	  buf_appendf(report, "Organization: %s\n", ORGANIZATION);
      }
      for (i = 0; i < 20 && buf_getline(out, test) != -1; i++)
	buf_cat(report, test), buf_nl(report);
    } else {
      buf_appends(report, "The remailer message is invalid.\n\n");
      if (test->length) {
	buf_appends(report, "The following error occurred: ");
	buf_cat(report, test);
	buf_nl(report);
      }
    }
    buf_appends(report,
		"=========================================================================\nThe first 20 lines of your message to the remailer follow:\n");
    buf_rewind(in);
    for (i = 0; i < 20 && buf_getline(in, test) != -1; i++)
      buf_cat(report, test), buf_nl(report);

    sendmail(report, REMAILERNAME, testto);
    err = 0;
    buf_free(report);
  } else if (err == 0 && type != MSG_NULL) {
    err = 1;
    if (bufieq(to, REMAILERADDR)) /* don't remix to ourselves */
      remix = 0;
    if (remix && remixto->length == 0)
      buf_set(remixto, to);
    if (remixto->length > 0) {
      /* check that the remix-to path isn't too long */
      int remixcount = 1;
      char *tmp = remixto->data;
      while ((tmp = strchr(tmp+1, ','))) {
	remixcount ++;
	if (remixcount > MAXRANDHOPS) {
	  *tmp = '\0';
	  break;
	}
      };
    }
    if (remix && !repgp && remixto->length != 0)
      err = mix_encrypt(type, out, remixto->data, 1, line);
    if (err != 0) {
      if (remix == 1 && !repgp)
	errlog(NOTICE, "Can't remix -- %b\n", line);
      else {
	if (remixto->length)
	  err = t1_encrypt(type, out, remixto->data, 0, 0, line);
	if (err != 0 && repgp)
	  errlog(NOTICE, "Can't repgp -- %b\n", line);
	else
	  err = mix_pool(out, type, latent * 60);
      }
    }
  }

  buf_free(field);
  buf_free(content);
  buf_free(line);
  buf_free(to);
  buf_free(remixto);
  buf_free(newsgroups);
  buf_free(subject);
  buf_free(ek);
  buf_free(ekcast);
  buf_free(ekdes);
  buf_free(esub);
  buf_free(cutmarks);
  buf_free(temp);
  buf_free(out);
  buf_free(header);
  buf_free(test);
  buf_free(testto);
  buf_free(digest);
  return (err);
}