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