* Fixed up the "Date:" headers to be RFC822-compliant
[citadel.git] / citadel / internet_addressing.c
1 /*
2  * $Id$
3  *
4  * This file contains functions which handle the mapping of Internet addresses
5  * to users on the Citadel system.
6  */
7
8 #include "sysdep.h"
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <stdio.h>
12 #include <fcntl.h>
13 #include <ctype.h>
14 #include <signal.h>
15 #include <pwd.h>
16 #include <errno.h>
17 #include <sys/types.h>
18 #include <sys/time.h>
19 #include <sys/wait.h>
20 #include <string.h>
21 #include <limits.h>
22 #include "citadel.h"
23 #include "server.h"
24 #include <time.h>
25 #include "sysdep_decls.h"
26 #include "citserver.h"
27 #include "support.h"
28 #include "config.h"
29 #include "tools.h"
30 #include "internet_addressing.h"
31 #include "user_ops.h"
32 #include "parsedate.h"
33
34
35
36 /*
37  * Return 0 if a given string fuzzy-matches a Citadel user account
38  *
39  * FIX ... this needs to be updated to match any and all address syntaxes.
40  */
41 int fuzzy_match(struct usersupp *us, char *matchstring) {
42         int a;
43
44         for (a=0; a<strlen(us->fullname); ++a) {
45                 if (!strncasecmp(&us->fullname[a],
46                    matchstring, strlen(matchstring))) {
47                         return 0;
48                 }
49         }
50         return -1;
51 }
52
53
54 /*
55  * Unfold a multi-line field into a single line, removing multi-whitespaces
56  */
57 void unfold_rfc822_field(char *field) {
58         int i;
59         int quote = 0;
60
61         striplt(field);         /* remove leading/trailing whitespace */
62
63         /* convert non-space whitespace to spaces, and remove double blanks */
64         for (i=0; i<strlen(field); ++i) {
65                 if (field[i]=='\"') quote = 1 - quote;
66                 if (!quote) {
67                         if (isspace(field[i])) field[i] = ' ';
68                         while (isspace(field[i]) && isspace(field[i+1])) {
69                                 strcpy(&field[i+1], &field[i+2]);
70                         }
71                 }
72         }
73 }
74
75
76
77 /*
78  * Split an RFC822-style address into userid, host, and full name
79  * (Originally from citmail.c, and unchanged so far)
80  *
81  */
82 void process_rfc822_addr(char *rfc822, char *user, char *node, char *name)
83 {
84         int a;
85
86         strcpy(user, "");
87         strcpy(node, config.c_fqdn);
88         strcpy(name, "");
89
90         /* extract full name - first, it's From minus <userid> */
91         strcpy(name, rfc822);
92         for (a = 0; a < strlen(name); ++a) {
93                 if (name[a] == '<') {
94                         do {
95                                 strcpy(&name[a], &name[a + 1]);
96                         } while ((strlen(name) > 0) && (name[a] != '>'));
97                         strcpy(&name[a], &name[a + 1]);
98                 }
99         }
100         /* strip anything to the left of a bang */
101         while ((strlen(name) > 0) && (haschar(name, '!') > 0))
102                 strcpy(name, &name[1]);
103
104         /* and anything to the right of a @ or % */
105         for (a = 0; a < strlen(name); ++a) {
106                 if (name[a] == '@')
107                         name[a] = 0;
108                 if (name[a] == '%')
109                         name[a] = 0;
110         }
111
112         /* but if there are parentheses, that changes the rules... */
113         if ((haschar(rfc822, '(') == 1) && (haschar(rfc822, ')') == 1)) {
114                 strcpy(name, rfc822);
115                 while ((strlen(name) > 0) && (name[0] != '(')) {
116                         strcpy(&name[0], &name[1]);
117                 }
118                 strcpy(&name[0], &name[1]);
119                 for (a = 0; a < strlen(name); ++a) {
120                         if (name[a] == ')') {
121                                 name[a] = 0;
122                         }
123                 }
124         }
125         /* but if there are a set of quotes, that supersedes everything */
126         if (haschar(rfc822, 34) == 2) {
127                 strcpy(name, rfc822);
128                 while ((strlen(name) > 0) && (name[0] != 34)) {
129                         strcpy(&name[0], &name[1]);
130                 }
131                 strcpy(&name[0], &name[1]);
132                 for (a = 0; a < strlen(name); ++a)
133                         if (name[a] == 34)
134                                 name[a] = 0;
135         }
136         /* extract user id */
137         strcpy(user, rfc822);
138
139         /* first get rid of anything in parens */
140         for (a = 0; a < strlen(user); ++a)
141                 if (user[a] == '(') {
142                         do {
143                                 strcpy(&user[a], &user[a + 1]);
144                         } while ((strlen(user) > 0) && (user[a] != ')'));
145                         strcpy(&user[a], &user[a + 1]);
146                 }
147         /* if there's a set of angle brackets, strip it down to that */
148         if ((haschar(user, '<') == 1) && (haschar(user, '>') == 1)) {
149                 while ((strlen(user) > 0) && (user[0] != '<')) {
150                         strcpy(&user[0], &user[1]);
151                 }
152                 strcpy(&user[0], &user[1]);
153                 for (a = 0; a < strlen(user); ++a)
154                         if (user[a] == '>')
155                                 user[a] = 0;
156         }
157         /* strip anything to the left of a bang */
158         while ((strlen(user) > 0) && (haschar(user, '!') > 0))
159                 strcpy(user, &user[1]);
160
161         /* and anything to the right of a @ or % */
162         for (a = 0; a < strlen(user); ++a) {
163                 if (user[a] == '@')
164                         user[a] = 0;
165                 if (user[a] == '%')
166                         user[a] = 0;
167         }
168
169
170         /* extract node name */
171         strcpy(node, rfc822);
172
173         /* first get rid of anything in parens */
174         for (a = 0; a < strlen(node); ++a)
175                 if (node[a] == '(') {
176                         do {
177                                 strcpy(&node[a], &node[a + 1]);
178                         } while ((strlen(node) > 0) && (node[a] != ')'));
179                         strcpy(&node[a], &node[a + 1]);
180                 }
181         /* if there's a set of angle brackets, strip it down to that */
182         if ((haschar(node, '<') == 1) && (haschar(node, '>') == 1)) {
183                 while ((strlen(node) > 0) && (node[0] != '<')) {
184                         strcpy(&node[0], &node[1]);
185                 }
186                 strcpy(&node[0], &node[1]);
187                 for (a = 0; a < strlen(node); ++a)
188                         if (node[a] == '>')
189                                 node[a] = 0;
190         }
191         /* strip anything to the left of a @ */
192         while ((strlen(node) > 0) && (haschar(node, '@') > 0))
193                 strcpy(node, &node[1]);
194
195         /* strip anything to the left of a % */
196         while ((strlen(node) > 0) && (haschar(node, '%') > 0))
197                 strcpy(node, &node[1]);
198
199         /* reduce multiple system bang paths to node!user */
200         while ((strlen(node) > 0) && (haschar(node, '!') > 1))
201                 strcpy(node, &node[1]);
202
203         /* now get rid of the user portion of a node!user string */
204         for (a = 0; a < strlen(node); ++a)
205                 if (node[a] == '!')
206                         node[a] = 0;
207
208         /* strip leading and trailing spaces in all strings */
209         striplt(user);
210         striplt(node);
211         striplt(name);
212 }
213
214
215
216 /*
217  * Back end for convert_internet_address()
218  * (Compares an internet name [buffer1] and stores in [buffer2] if found)
219  */
220 void try_name(struct usersupp *us) {
221         
222         if (!strncasecmp(CC->buffer1, "cit", 3))
223                 if (atol(&CC->buffer1[3]) == us->usernum)
224                         strcpy(CC->buffer2, us->fullname);
225
226         if (!collapsed_strcmp(CC->buffer1, us->fullname)) 
227                         strcpy(CC->buffer2, us->fullname);
228
229         if (us->uid != BBSUID)
230                 if (!strcasecmp(CC->buffer1, getpwuid(us->uid)->pw_name))
231                         strcpy(CC->buffer2, us->fullname);
232 }
233
234
235
236 /*
237  * Convert an Internet email address to a Citadel user/host combination
238  */
239 int convert_internet_address(char *destuser, char *desthost, char *source)
240 {
241         char user[256];
242         char node[256];
243         char name[256];
244
245         /* Split it up */
246         process_rfc822_addr(source, user, node, name);
247
248         /* Map the FQDN to a Citadel node name
249          * FIX ... we have to check for all known aliases for the local
250          *         system, and also handle gateway domains, etc. etc.
251          */
252         if (!strcasecmp(node, config.c_fqdn)) {
253                 strcpy(node, config.c_nodename);
254         }
255
256         /* Return an error condition if the node is not known.
257          * FIX ... make this work for non-local systems
258          */
259         if (strcasecmp(node, config.c_nodename)) {
260                 return(rfc822_address_invalid);
261         }
262         
263         /* Now try to resolve the name
264          * FIX ... do the multiple-addresses thing
265          */
266         if (!strcasecmp(node, config.c_nodename)) {
267                 strcpy(destuser, user);
268                 strcpy(desthost, config.c_nodename);
269                 strcpy(CC->buffer1, user);
270                 strcpy(CC->buffer2, "");
271                 ForEachUser(try_name);
272                 if (strlen(CC->buffer2) == 0) return(rfc822_no_such_user);
273                 strcpy(destuser, CC->buffer2);
274                 return(rfc822_address_locally_validated);
275         }
276
277         return(rfc822_address_invalid); /* unknown error */
278 }
279
280
281
282
283 /*
284  * convert_field() is a helper function for convert_internet_message().
285  * Given start/end positions for an rfc822 field, it converts it to a Citadel
286  * field if it wants to, and unfolds it if necessary.
287  *
288  * Returns 1 if the field was converted and inserted into the Citadel message
289  * structure, implying that the source field should be removed from the
290  * message text.
291  */
292 int convert_field(struct CtdlMessage *msg, int beg, int end) {
293         char *rfc822;
294         char *key, *value;
295         int i;
296         int colonpos = (-1);
297         int processed = 0;
298         char buf[256];
299         char *user, *node, *name;
300
301         rfc822 = msg->cm_fields['M'];   /* M field contains rfc822 text */
302         for (i = end; i >= beg; --i) {
303                 if (rfc822[i] == ':') colonpos = i;
304         }
305
306         if (colonpos < 0) return(0);    /* no colon? not a valid header line */
307
308         value = mallok((end - beg) + 2);
309         safestrncpy(value, &rfc822[beg], (end-beg)+1);
310         key = value;
311         key[colonpos - beg] = 0;
312         value = &key[(colonpos - beg) + 1];
313         unfold_rfc822_field(value);
314
315         /* Here's the big rfc822-to-citadel loop. */
316
317         if (!strcasecmp(key, "Date")) {
318                 lprintf(9, "converting date <%s>\n", value);
319                 sprintf(buf, "%ld", parsedate(value) );
320                 lprintf(9, "parsed value is <%s>\n", buf);
321                 if (msg->cm_fields['T'] != NULL)
322                         msg->cm_fields['T'] = strdoop(buf);
323                 processed = 1;
324         }
325
326         else if (!strcasecmp(key, "From")) {
327                 user = mallok(strlen(value));
328                 node = mallok(strlen(value));
329                 name = mallok(strlen(value));
330                 process_rfc822_addr(value, user, node, name);
331                 if (msg->cm_fields['A'] != NULL)
332                         msg->cm_fields['A'] = strdoop(user);
333                 if (msg->cm_fields['N'] != NULL)
334                         msg->cm_fields['N'] = strdoop(node);
335                 if (msg->cm_fields['H'] != NULL)
336                         msg->cm_fields['H'] = strdoop(name);
337                 phree(user);
338                 phree(node);
339                 phree(name);
340                 processed = 1;
341         }
342
343         else if (!strcasecmp(key, "Subject")) {
344                 if (msg->cm_fields['U'] != NULL)
345                         msg->cm_fields['U'] = strdoop(key);
346                 processed = 1;
347         }
348
349         /* Clean up and move on. */
350         lprintf(9, "Field: key=<%s> value=<%s> processed=%d\n",
351                 key, value, processed);
352         phree(key);     /* Don't free 'value', it's actually the same buffer */
353         return(processed);
354 }
355
356
357 /*
358  * Convert an RFC822 message (headers + body) to a CtdlMessage structure.
359  */
360 struct CtdlMessage *convert_internet_message(char *rfc822) {
361
362         struct CtdlMessage *msg;
363         int pos, beg, end;
364         int msglen;
365         int done;
366         char buf[256];
367         int converted;
368
369         msg = mallok(sizeof(struct CtdlMessage));
370         if (msg == NULL) return msg;
371
372         memset(msg, 0, sizeof(struct CtdlMessage));
373         msg->cm_magic = CTDLMESSAGE_MAGIC;      /* self check */
374         msg->cm_anon_type = 0;                  /* never anonymous */
375         msg->cm_format_type = 4;                /* always MIME */
376         msg->cm_fields['M'] = rfc822;
377
378         /* FIX   there's plenty to do here. */
379         msglen = strlen(rfc822);
380         pos = 0;
381         done = 0;
382
383         while (!done) {
384
385                 /* Locate beginning and end of field, keeping in mind that
386                  * some fields might be multiline
387                  */
388                 beg = pos;
389                 end = (-1);
390                 for (pos=beg; ((pos<=strlen(rfc822))&&(end<0)); ++pos) {
391                         if ((rfc822[pos]=='\n')
392                            && (!isspace(rfc822[pos+1]))) {
393                                 end = pos;
394                         }
395                         if ( (rfc822[pos]=='\n')        /* done w. headers? */
396                            && ( (rfc822[pos+1]=='\n')
397                               ||(rfc822[pos+1]=='\r'))) {
398                                 end = pos;
399                                 done = 1;
400                         }
401
402                 }
403
404                 /* At this point we have a field.  Are we interested in it? */
405                 converted = convert_field(msg, beg, end);
406                 if (converted) {
407                         strcpy(&rfc822[beg], &rfc822[pos]);
408                         pos = beg;
409                 }
410
411                 /* If we've hit the end of the message, bail out */
412                 if (pos > strlen(rfc822)) done = 1;
413         }
414
415
416         /* Follow-up sanity checks... */
417
418         /* If there's no timestamp on this message, set it to now. */
419         if (msg->cm_fields['T'] == NULL) {
420                 sprintf(buf, "%ld", time(NULL));
421                 msg->cm_fields['T'] = strdoop(buf);
422         }
423
424         return msg;
425 }
426