* Replaced citmail.c with a new one that simply SMTP-forwards to Citadel
[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  * Convert an Internet email address to a Citadel user/host combination
237  */
238 int convert_internet_address(char *destuser, char *desthost, char *source)
239 {
240         char user[256];
241         char node[256];
242         char name[256];
243
244         /* Split it up */
245         process_rfc822_addr(source, user, node, name);
246
247         /* Map the FQDN to a Citadel node name
248          * FIX ... we have to check for all known aliases for the local
249          *         system, and also handle gateway domains, etc. etc.
250          */
251         if (!strcasecmp(node, config.c_fqdn)) {
252                 strcpy(node, config.c_nodename);
253         }
254
255         /* Return an error condition if the node is not known.
256          * FIX ... make this work for non-local systems
257          */
258         if (strcasecmp(node, config.c_nodename)) {
259                 return(rfc822_address_invalid);
260         }
261         
262         /* Now try to resolve the name
263          * FIX ... do the multiple-addresses thing
264          */
265         if (!strcasecmp(node, config.c_nodename)) {
266                 strcpy(destuser, user);
267                 strcpy(desthost, config.c_nodename);
268                 strcpy(CC->buffer1, user);
269                 strcpy(CC->buffer2, "");
270                 ForEachUser(try_name);
271                 if (strlen(CC->buffer2) == 0) return(rfc822_no_such_user);
272                 strcpy(destuser, CC->buffer2);
273                 return(rfc822_address_locally_validated);
274         }
275
276         return(rfc822_address_invalid); /* unknown error */
277 }
278
279
280
281
282 /*
283  * convert_field() is a helper function for convert_internet_message().
284  * Given start/end positions for an rfc822 field, it converts it to a Citadel
285  * field if it wants to, and unfolds it if necessary.
286  *
287  * Returns 1 if the field was converted and inserted into the Citadel message
288  * structure, implying that the source field should be removed from the
289  * message text.
290  */
291 int convert_field(struct CtdlMessage *msg, int beg, int end) {
292         char *rfc822;
293         char *key, *value;
294         int i;
295         int colonpos = (-1);
296         int processed = 0;
297         char buf[256];
298         char *user, *node, *name;
299
300         rfc822 = msg->cm_fields['M'];   /* M field contains rfc822 text */
301         for (i = end; i >= beg; --i) {
302                 if (rfc822[i] == ':') colonpos = i;
303         }
304
305         if (colonpos < 0) return(0);    /* no colon? not a valid header line */
306
307         key = mallok((end - beg) + 2);
308         safestrncpy(key, &rfc822[beg], (end-beg)+1);
309         key[colonpos - beg] = 0;
310         value = &key[(colonpos - beg) + 1];
311         unfold_rfc822_field(value);
312
313         lprintf(9, "Key=<%s> Value=<%s>\n", key, value);
314
315         /* Here's the big rfc822-to-citadel loop. */
316
317         if (!strcasecmp(key, "Date")) {
318                 sprintf(buf, "%ld", parsedate(value) );
319                 if (msg->cm_fields['T'] == NULL)
320                         msg->cm_fields['T'] = strdoop(buf);
321                 processed = 1;
322         }
323
324         else if (!strcasecmp(key, "From")) {
325                 user = mallok(strlen(value));
326                 node = mallok(strlen(value));
327                 name = mallok(strlen(value));
328                 process_rfc822_addr(value, user, node, name);
329                 lprintf(9, "Converted to <%s@%s> (%s)\n", user, node, name);
330                 if (msg->cm_fields['A'] == NULL)
331                         msg->cm_fields['A'] = user;
332                 else
333                         phree(user);
334                 if (msg->cm_fields['N'] == NULL)
335                         msg->cm_fields['N'] = node;
336                 else
337                         phree(node);
338                 if (msg->cm_fields['H'] == NULL)
339                         msg->cm_fields['H'] = name;
340                 else
341                         phree(name);
342                 processed = 1;
343         }
344
345         else if (!strcasecmp(key, "Subject")) {
346                 if (msg->cm_fields['U'] == NULL)
347                         msg->cm_fields['U'] = strdoop(value);
348                 processed = 1;
349         }
350
351         /* Clean up and move on. */
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 done;
365         char buf[256];
366         int converted;
367
368         msg = mallok(sizeof(struct CtdlMessage));
369         if (msg == NULL) return msg;
370
371         memset(msg, 0, sizeof(struct CtdlMessage));
372         msg->cm_magic = CTDLMESSAGE_MAGIC;      /* self check */
373         msg->cm_anon_type = 0;                  /* never anonymous */
374         msg->cm_format_type = FMT_RFC822;       /* internet message */
375         msg->cm_fields['M'] = rfc822;
376
377         lprintf(9, "Unconverted RFC822 message length = %d\n", strlen(rfc822));
378         pos = 0;
379         done = 0;
380
381         while (!done) {
382
383                 /* Locate beginning and end of field, keeping in mind that
384                  * some fields might be multiline
385                  */
386                 beg = pos;
387                 end = (-1);
388                 for (pos=beg; ((pos<=strlen(rfc822))&&(end<0)); ++pos) {
389                         if ((rfc822[pos]=='\n')
390                            && (!isspace(rfc822[pos+1]))) {
391                                 end = pos;
392                         }
393                         if ( (rfc822[pos]=='\n')        /* done w. headers? */
394                            && ( (rfc822[pos+1]=='\n')
395                               ||(rfc822[pos+1]=='\r'))) {
396                                 end = pos;
397                                 done = 1;
398                         }
399
400                 }
401
402                 /* At this point we have a field.  Are we interested in it? */
403                 converted = convert_field(msg, beg, end);
404
405                 /******
406                 if (converted) {
407                         strcpy(&rfc822[beg], &rfc822[pos]);
408                         pos = beg;
409                 }
410                 ********/
411
412                 /* If we've hit the end of the message, bail out */
413                 if (pos > strlen(rfc822)) done = 1;
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         lprintf(9, "RFC822 length remaining after conversion = %d\n",
425                 strlen(rfc822));
426         return msg;
427 }
428