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