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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 262 - (hide annotations) (download)
Wed Sep 18 23:26:17 2002 UTC (10 years, 8 months ago) by rabbi
File MIME type: text/plain
File size: 8067 byte(s)
Added closing comments for all #ifdef statements. All #endif's, as well as
nested braces, should be commented to reference their start.

We need to provide comments before every function as well.
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     Encrypt message for Cypherpunk remailer chain
9 rabbi 262 $Id: chain1.c,v 1.3 2002/09/18 23:26:16 rabbi Exp $ */
10 rabbi 1
11    
12     #include "mix3.h"
13     #include "pgp.h"
14     #include <string.h>
15     #include <ctype.h>
16    
17     #define N(X) (isdigit(X) ? (X)-'0' : 0)
18    
19     int t1_rlist(REMAILER remailer[])
20     {
21     FILE *list;
22     int i, listed = 0;
23     int n = 0;
24     char line[2 * LINELEN], l2[LINELEN], name[LINELEN], *flags;
25    
26     list = mix_openfile(TYPE1LIST, "r");
27     if (list == NULL)
28     return (-1);
29     while (fgets(line, sizeof(line), list) != NULL && n < MAXREM) {
30     if (strleft(line, "$remailer") &&
31     strchr(line, '<') && strchr(line, '>') &&
32     strchr(line, '{') && strchr(line, '{') + 4 < strchr(line, '}')) {
33     if (line[strlen(line) - 1] == '\n')
34     line[strlen(line) - 1] = '\0';
35     if (line[strlen(line) - 1] == '\r')
36     line[strlen(line) - 1] = '\0';
37     while (line[strlen(line) - 1] == ' ')
38     line[strlen(line) - 1] = '\0';
39     if (line[strlen(line) - 1] != ';'
40     && fgets(l2, sizeof(l2), list) != NULL)
41     strcatn(line, l2, LINELEN);
42     flags = strchr(line, '>');
43     strncpy(name, strchr(line, '{') + 2,
44     strchr(line, '}') - strchr(line, '{') - 3);
45     name[strchr(line, '}') - strchr(line, '{') - 3] = '\0';
46     name[20] = '\0';
47    
48     for (i = 1; i <= n; i++)
49     if (streq(name, remailer[i].name))
50     break;
51     if (i > n) {
52     /* not in mix list */
53     n++;
54     strcpy(remailer[i].name, name);
55     strncpy(remailer[i].addr, strchr(line, '<') + 1,
56     strchr(line, '>') - strchr(line, '<'));
57     remailer[i].addr[strchr(line, '>') - strchr(line, '<') - 1]
58     = '\0';
59     remailer[i].flags.mix = 0;
60     remailer[i].flags.post = strifind(flags, " post");
61     }
62     remailer[i].flags.cpunk = strfind(flags, " cpunk");
63     remailer[i].flags.pgp = strfind(flags, " pgp");
64     remailer[i].flags.pgponly = strfind(flags, " pgponly");
65     remailer[i].flags.latent = strfind(flags, " latent");
66     remailer[i].flags.middle = strfind(flags, " middle");
67     remailer[i].flags.ek = strfind(flags, " ek");
68     remailer[i].flags.esub = strfind(flags, " esub");
69     remailer[i].flags.newnym = strfind(flags, " newnym");
70     remailer[i].flags.nym = strfind(flags, " nym");
71     remailer[i].info[1].reliability = 0;
72     remailer[i].info[1].latency = 0;
73     remailer[i].info[1].history[0] = '\0';
74     }
75     if (strleft(line,
76     "-----------------------------------------------------------------------"))
77     break;
78     }
79     n++; /* ?? */
80     while (fgets(line, sizeof(line), list) != NULL) {
81     if (strlen(line) >= 72 && strlen(line) <= 73)
82     for (i = 1; i < n; i++)
83     if (strleft(line, remailer[i].name) &&
84     line[strlen(remailer[i].name)] == ' ') {
85     strncpy(remailer[i].info[1].history, line + 42, 12);
86     remailer[i].info[1].history[12] = '\0';
87     remailer[i].info[1].reliability = 10000 * N(line[64])
88     + 1000 * N(line[65]) + 100 * N(line[66])
89     + 10 * N(line[68]) + N(line[69]);
90     remailer[i].info[1].latency = 36000 * N(line[55])
91     + 3600 * N(line[56]) + 600 * N(line[58])
92     + 60 * N(line[59]) + 10 * N(line[61])
93     + N(line[62]);
94     listed++;
95     }
96     }
97     fclose(list);
98     if (listed < 4) /* we have no valid reliability info */
99     for (i = 1; i < n; i++)
100     remailer[i].info[1].reliability = 10000;
101    
102     #ifdef USE_PGP
103     pgp_rlist(remailer, n);
104 rabbi 262 #endif /* USE_PGP */
105 rabbi 1 return (n);
106     }
107    
108     int t1_ek(BUFFER *key, BUFFER *seed, int num)
109     {
110     buf_reset(key);
111     buf_appendc(key, (byte) num);
112     buf_cat(key, seed);
113     digest_md5(key, key);
114     encode(key, 0);
115     #ifdef DEBUG
116     fprintf(stderr, "passphrase=%s (%2X%2X%2X%2X %d)\n", key->data,
117     seed->data[0], seed->data[1], seed->data[2], seed->data[3], num);
118 rabbi 262 #endif /* DEBUG */
119     return (0);
120 rabbi 1 }
121    
122     int t1_encrypt(int type, BUFFER *message, char *chainstr, int latency,
123     BUFFER *ek, BUFFER *feedback)
124     {
125     BUFFER *b, *rem, *dest, *line, *field, *content;
126     REMAILER remailer[MAXREM];
127     int maxrem, chainlen = 0;
128     int chain[20];
129     int hop;
130     int hashmark = 0;
131     int err = 0;
132    
133     b = buf_new();
134     rem = buf_new();
135     dest = buf_new();
136     line = buf_new();
137     field = buf_new();
138     content = buf_new();
139    
140     maxrem = t1_rlist(remailer);
141     if (maxrem < 1) {
142     clienterr(feedback, "No remailer list!");
143     err = -1;
144     goto end;
145     }
146     chainlen = chain_select(chain, chainstr, maxrem, remailer, 1, line);
147     if (chainlen < 1) {
148     if (line->length)
149     clienterr(feedback, line->data);
150     else
151     clienterr(feedback, "Invalid remailer chain!");
152     err = -1;
153     goto end;
154     }
155     if (chain[0] == 0)
156     chain[0] = chain_randfinal(type, remailer, maxrem, 1);
157    
158     if (chain[0] == -1) {
159     clienterr(feedback, "Invalid remailer chain!");
160     err = -1;
161     goto end;
162     }
163     if (chain_rand(remailer, maxrem, chain, chainlen, 1) == -1) {
164     clienterr(feedback, "No reliable remailers!");
165     err = -1;
166     goto end;
167     }
168     while (buf_getheader(message, field, content) == 0) {
169     hdr_encode(content, 0);
170     if (type == MSG_POST && bufieq(field, "newsgroups") &&
171     remailer[chain[0]].flags.post) {
172     buf_appendf(dest, "Anon-Post-To: %b\n", content);
173     } else if (type == MSG_MAIL && bufieq(field, "to")) {
174     buf_appendf(dest, "Anon-To: %b\n", content);
175     } else {
176     /* paste header */
177     if (type == MSG_POST && bufieq(field, "newsgroups"))
178     buf_appendf(dest, "Anon-To: %s\n", MAILtoNEWS);
179     if (hashmark == 0) {
180     buf_appends(b, "##\n");
181     hashmark = 1;
182     }
183     buf_appendheader(b, field, content);
184     }
185     }
186     buf_nl(b);
187     buf_rest(b, message);
188     buf_move(message, b);
189    
190     if (type != MSG_NULL && dest->length == 0) {
191     clienterr(feedback, "No destination address!");
192     err = -1;
193     goto end;
194     }
195     if (type == MSG_NULL) {
196     buf_sets(dest, "Null:\n");
197     }
198     for (hop = 0; hop < chainlen; hop++) {
199     if (hop == 0) {
200     buf_sets(b, "::\n");
201     buf_cat(b, dest);
202     } else {
203     buf_sets(b, "::\nAnon-To: ");
204     buf_appends(b, remailer[chain[hop - 1]].addr);
205     buf_nl(b);
206     }
207     if (remailer[chain[hop]].flags.latent && latency > 0)
208     buf_appendf(b, "Latent-Time: +%d:00r\n", latency);
209     if (ek && remailer[chain[hop]].flags.ek) {
210     t1_ek(line, ek, hop);
211     buf_appendf(b, "Encrypt-Key: %b\n", line);
212     }
213     buf_nl(b);
214     buf_cat(b, message);
215     #ifdef USE_PGP
216     if (remailer[chain[hop]].flags.pgp) {
217     buf_clear(message);
218     buf_clear(rem);
219     buf_setf(rem, "<%s>", remailer[chain[hop]].addr);
220     err = pgp_encrypt(PGP_ENCRYPT | PGP_REMAIL | PGP_TEXT, b, rem,
221     NULL, NULL, NULL, NULL);
222     if (err < 0) {
223     buf_setf(line, "No PGP key for remailer %s!\n",
224     remailer[chain[hop]].name);
225     clienterr(feedback, line->data);
226     goto end;
227     }
228     buf_appends(message, "::\nEncrypted: PGP\n\n");
229     buf_cat(message, b);
230     } else
231 rabbi 262 #endif /* USE_PGP */
232 rabbi 1 {
233     if (remailer[chain[hop]].flags.pgponly) {
234     buf_setf(line, "PGP encryption needed for remailer %s!\n",
235     remailer[chain[hop]].name);
236     clienterr(feedback, line->data);
237     goto end;
238     }
239     buf_move(message, b);
240     }
241     if (ek && remailer[chain[hop]].flags.ek)
242     buf_appends(message, "\n**\n");
243     }
244     buf_clear(b);
245     if (chainlen == 0) {
246     buf_appends(b, "::\n");
247     buf_cat(b, dest);
248     } else {
249 rabbi 40 buf_appendf(b, "%s: %s\n", ek ? "::\nAnon-To" : "To",
250 rabbi 1 remailer[chain[chainlen - 1]].addr);
251     }
252     buf_nl(b);
253     buf_cat(b, message);
254     buf_move(message, b);
255     end:
256     buf_free(b);
257     buf_free(rem);
258     buf_free(dest);
259     buf_free(line);
260     buf_free(field);
261     buf_free(content);
262     return (err);
263     }
264    
265     #ifdef USE_PGP
266     int t1_getreply(BUFFER *msg, BUFFER *ek, int len)
267     {
268     BUFFER *key, *decrypt;
269     int err = -1;
270     int hop = 0;
271    
272     key = buf_new();
273     decrypt = buf_new();
274    
275     do {
276     t1_ek(key, ek, hop);
277     buf_set(decrypt, msg);
278     if (pgp_decrypt(decrypt, key, NULL, NULL, NULL) == 0
279     && decrypt->data != NULL)
280     err = 0, buf_move(msg, decrypt);
281     }
282     while (hop++ < len);
283     return (err);
284     }
285    
286 rabbi 262 #endif /* USE_PGP */

  ViewVC Help
Powered by ViewVC 1.1.5