* More header parsing stuff. Still needs work.
[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
33
34 /*
35  * Return 0 if a given string fuzzy-matches a Citadel user account
36  *
37  * FIX ... this needs to be updated to match any and all address syntaxes.
38  */
39 int fuzzy_match(struct usersupp *us, char *matchstring) {
40         int a;
41
42         for (a=0; a<strlen(us->fullname); ++a) {
43                 if (!strncasecmp(&us->fullname[a],
44                    matchstring, strlen(matchstring))) {
45                         return 0;
46                 }
47         }
48         return -1;
49 }
50
51
52
53
54 /*
55  * Split an RFC822-style address into userid, host, and full name
56  * (Originally from citmail.c, and unchanged so far)
57  *
58  */
59 void process_rfc822_addr(char *rfc822, char *user, char *node, char *name)
60 {
61         int a;
62
63         strcpy(user, "");
64         strcpy(node, config.c_fqdn);
65         strcpy(name, "");
66
67         /* extract full name - first, it's From minus <userid> */
68         strcpy(name, rfc822);
69         for (a = 0; a < strlen(name); ++a) {
70                 if (name[a] == '<') {
71                         do {
72                                 strcpy(&name[a], &name[a + 1]);
73                         } while ((strlen(name) > 0) && (name[a] != '>'));
74                         strcpy(&name[a], &name[a + 1]);
75                 }
76         }
77         /* strip anything to the left of a bang */
78         while ((strlen(name) > 0) && (haschar(name, '!') > 0))
79                 strcpy(name, &name[1]);
80
81         /* and anything to the right of a @ or % */
82         for (a = 0; a < strlen(name); ++a) {
83                 if (name[a] == '@')
84                         name[a] = 0;
85                 if (name[a] == '%')
86                         name[a] = 0;
87         }
88
89         /* but if there are parentheses, that changes the rules... */
90         if ((haschar(rfc822, '(') == 1) && (haschar(rfc822, ')') == 1)) {
91                 strcpy(name, rfc822);
92                 while ((strlen(name) > 0) && (name[0] != '(')) {
93                         strcpy(&name[0], &name[1]);
94                 }
95                 strcpy(&name[0], &name[1]);
96                 for (a = 0; a < strlen(name); ++a) {
97                         if (name[a] == ')') {
98                                 name[a] = 0;
99                         }
100                 }
101         }
102         /* but if there are a set of quotes, that supersedes everything */
103         if (haschar(rfc822, 34) == 2) {
104                 strcpy(name, rfc822);
105                 while ((strlen(name) > 0) && (name[0] != 34)) {
106                         strcpy(&name[0], &name[1]);
107                 }
108                 strcpy(&name[0], &name[1]);
109                 for (a = 0; a < strlen(name); ++a)
110                         if (name[a] == 34)
111                                 name[a] = 0;
112         }
113         /* extract user id */
114         strcpy(user, rfc822);
115
116         /* first get rid of anything in parens */
117         for (a = 0; a < strlen(user); ++a)
118                 if (user[a] == '(') {
119                         do {
120                                 strcpy(&user[a], &user[a + 1]);
121                         } while ((strlen(user) > 0) && (user[a] != ')'));
122                         strcpy(&user[a], &user[a + 1]);
123                 }
124         /* if there's a set of angle brackets, strip it down to that */
125         if ((haschar(user, '<') == 1) && (haschar(user, '>') == 1)) {
126                 while ((strlen(user) > 0) && (user[0] != '<')) {
127                         strcpy(&user[0], &user[1]);
128                 }
129                 strcpy(&user[0], &user[1]);
130                 for (a = 0; a < strlen(user); ++a)
131                         if (user[a] == '>')
132                                 user[a] = 0;
133         }
134         /* strip anything to the left of a bang */
135         while ((strlen(user) > 0) && (haschar(user, '!') > 0))
136                 strcpy(user, &user[1]);
137
138         /* and anything to the right of a @ or % */
139         for (a = 0; a < strlen(user); ++a) {
140                 if (user[a] == '@')
141                         user[a] = 0;
142                 if (user[a] == '%')
143                         user[a] = 0;
144         }
145
146
147         /* extract node name */
148         strcpy(node, rfc822);
149
150         /* first get rid of anything in parens */
151         for (a = 0; a < strlen(node); ++a)
152                 if (node[a] == '(') {
153                         do {
154                                 strcpy(&node[a], &node[a + 1]);
155                         } while ((strlen(node) > 0) && (node[a] != ')'));
156                         strcpy(&node[a], &node[a + 1]);
157                 }
158         /* if there's a set of angle brackets, strip it down to that */
159         if ((haschar(node, '<') == 1) && (haschar(node, '>') == 1)) {
160                 while ((strlen(node) > 0) && (node[0] != '<')) {
161                         strcpy(&node[0], &node[1]);
162                 }
163                 strcpy(&node[0], &node[1]);
164                 for (a = 0; a < strlen(node); ++a)
165                         if (node[a] == '>')
166                                 node[a] = 0;
167         }
168         /* strip anything to the left of a @ */
169         while ((strlen(node) > 0) && (haschar(node, '@') > 0))
170                 strcpy(node, &node[1]);
171
172         /* strip anything to the left of a % */
173         while ((strlen(node) > 0) && (haschar(node, '%') > 0))
174                 strcpy(node, &node[1]);
175
176         /* reduce multiple system bang paths to node!user */
177         while ((strlen(node) > 0) && (haschar(node, '!') > 1))
178                 strcpy(node, &node[1]);
179
180         /* now get rid of the user portion of a node!user string */
181         for (a = 0; a < strlen(node); ++a)
182                 if (node[a] == '!')
183                         node[a] = 0;
184
185         /* strip leading and trailing spaces in all strings */
186         striplt(user);
187         striplt(node);
188         striplt(name);
189 }
190
191
192
193 /*
194  * Back end for convert_internet_address()
195  * (Compares an internet name [buffer1] and stores in [buffer2] if found)
196  */
197 void try_name(struct usersupp *us) {
198         
199         if (!strncasecmp(CC->buffer1, "cit", 3))
200                 if (atol(&CC->buffer1[3]) == us->usernum)
201                         strcpy(CC->buffer2, us->fullname);
202
203         if (!collapsed_strcmp(CC->buffer1, us->fullname)) 
204                         strcpy(CC->buffer2, us->fullname);
205
206         if (us->uid != BBSUID)
207                 if (!strcasecmp(CC->buffer1, getpwuid(us->uid)->pw_name))
208                         strcpy(CC->buffer2, us->fullname);
209 }
210
211
212
213 /*
214  * Convert an Internet email address to a Citadel user/host combination
215  */
216 int convert_internet_address(char *destuser, char *desthost, char *source)
217 {
218         char user[256];
219         char node[256];
220         char name[256];
221
222         /* Split it up */
223         process_rfc822_addr(source, user, node, name);
224
225         /* Map the FQDN to a Citadel node name
226          * FIX ... we have to check for all known aliases for the local
227          *         system, and also handle gateway domains, etc. etc.
228          */
229         if (!strcasecmp(node, config.c_fqdn)) {
230                 strcpy(node, config.c_nodename);
231         }
232
233         /* Return an error condition if the node is not known.
234          * FIX ... make this work for non-local systems
235          */
236         if (strcasecmp(node, config.c_nodename)) {
237                 return(rfc822_address_invalid);
238         }
239         
240         /* Now try to resolve the name
241          * FIX ... do the multiple-addresses thing
242          */
243         if (!strcasecmp(node, config.c_nodename)) {
244                 strcpy(destuser, user);
245                 strcpy(desthost, config.c_nodename);
246                 strcpy(CC->buffer1, user);
247                 strcpy(CC->buffer2, "");
248                 ForEachUser(try_name);
249                 if (strlen(CC->buffer2) == 0) return(rfc822_no_such_user);
250                 strcpy(destuser, CC->buffer2);
251                 return(rfc822_address_locally_validated);
252         }
253
254         return(rfc822_address_invalid); /* unknown error */
255 }
256
257
258
259
260
261 /*
262  * Convert an RFC822 message (headers + body) to a CtdlMessage structure.
263  */
264 struct CtdlMessage *convert_internet_message(char *rfc822) {
265
266         struct CtdlMessage *msg;
267         int pos, beg, end;
268         int msglen;
269         int done;
270         char buf[256];
271         char field[256];
272         int i;
273
274         msg = mallok(sizeof(struct CtdlMessage));
275         if (msg == NULL) return msg;
276
277         memset(msg, 0, sizeof(struct CtdlMessage));
278         msg->cm_magic = CTDLMESSAGE_MAGIC;      /* self check */
279         msg->cm_anon_type = 0;                  /* never anonymous */
280         msg->cm_format_type = 4;                /* always MIME */
281         msg->cm_fields['M'] = rfc822;
282
283         /* FIX   there's plenty to do here. */
284         msglen = strlen(rfc822);
285         pos = 0;
286         done = 0;
287
288         while (!done) {
289
290                 /* Locate beginning and end of field, keeping in mind that
291                  * some fields might be multiline
292                  */
293                 beg = pos;
294                 end = (-1);
295                 for (pos=beg; ((pos<=strlen(rfc822))&&(end<0)); ++pos) {
296                         if ((rfc822[pos]=='\n')
297                            && (!isspace(rfc822[pos+1]))) {
298                                 end = pos;
299                         }
300                         if ( (rfc822[pos]=='\n')        /* done w. headers? */
301                            && ( (rfc822[pos+1]=='\n')
302                               ||(rfc822[pos+1]=='\r'))) {
303                                 end = pos;
304                                 done = 1;
305                         }
306
307                 }
308
309                 /* At this point we have a field.  Are we interested in it? */
310                 strcpy(field, "");
311                 for (i = beg; i <= end; ++i) {
312                         if ((rfc822[i] == ':') && ((i-beg)<sizeof(field))) {
313                                 safestrncpy(field, &rfc822[beg], i-beg+1);
314                         }
315                 }
316                 fprintf(stderr,
317                         "Field: %6d .. %-6d ... <%s>\n",
318                         beg, end, field);
319
320
321                 /* If we've hit the end of the message, bail out */
322                 if (pos > strlen(rfc822)) done = 1;
323         }
324
325
326         /* Follow-up sanity check. */
327
328         /* If there's no timestamp on this message, set it to now. */
329         if (msg->cm_fields['T'] == NULL) {
330                 sprintf(buf, "%ld", time(NULL));
331                 msg->cm_fields['T'] = strdoop(buf);
332         }
333
334         return msg;
335 }