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