4 * This file contains functions which handle the mapping of Internet addresses
5 * to users on the Citadel system.
17 #include <sys/types.h>
25 #include "sysdep_decls.h"
26 #include "citserver.h"
30 #include "internet_addressing.h"
33 #include "parsedate.h"
43 * Return 0 if a given string fuzzy-matches a Citadel user account
45 * FIX ... this needs to be updated to match any and all address syntaxes.
47 int fuzzy_match(struct usersupp *us, char *matchstring) {
50 for (a=0; a<strlen(us->fullname); ++a) {
51 if (!strncasecmp(&us->fullname[a],
52 matchstring, strlen(matchstring))) {
61 * Unfold a multi-line field into a single line, removing multi-whitespaces
63 void unfold_rfc822_field(char *field) {
67 striplt(field); /* remove leading/trailing whitespace */
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;
73 if (isspace(field[i])) field[i] = ' ';
74 while (isspace(field[i]) && isspace(field[i+1])) {
75 strcpy(&field[i+1], &field[i+2]);
84 * Split an RFC822-style address into userid, host, and full name
85 * (Originally from citmail.c, and unchanged so far)
88 void process_rfc822_addr(char *rfc822, char *user, char *node, char *name)
93 strcpy(node, config.c_fqdn);
96 /* extract full name - first, it's From minus <userid> */
98 for (a = 0; a < strlen(name); ++a) {
101 strcpy(&name[a], &name[a + 1]);
102 } while ((strlen(name) > 0) && (name[a] != '>'));
103 strcpy(&name[a], &name[a + 1]);
106 /* strip anything to the left of a bang */
107 while ((strlen(name) > 0) && (haschar(name, '!') > 0))
108 strcpy(name, &name[1]);
110 /* and anything to the right of a @ or % */
111 for (a = 0; a < strlen(name); ++a) {
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]);
124 strcpy(&name[0], &name[1]);
125 for (a = 0; a < strlen(name); ++a) {
126 if (name[a] == ')') {
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]);
137 strcpy(&name[0], &name[1]);
138 for (a = 0; a < strlen(name); ++a)
142 /* extract user id */
143 strcpy(user, rfc822);
145 /* first get rid of anything in parens */
146 for (a = 0; a < strlen(user); ++a)
147 if (user[a] == '(') {
149 strcpy(&user[a], &user[a + 1]);
150 } while ((strlen(user) > 0) && (user[a] != ')'));
151 strcpy(&user[a], &user[a + 1]);
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]);
158 strcpy(&user[0], &user[1]);
159 for (a = 0; a < strlen(user); ++a)
163 /* strip anything to the left of a bang */
164 while ((strlen(user) > 0) && (haschar(user, '!') > 0))
165 strcpy(user, &user[1]);
167 /* and anything to the right of a @ or % */
168 for (a = 0; a < strlen(user); ++a) {
176 /* extract node name */
177 strcpy(node, rfc822);
179 /* first get rid of anything in parens */
180 for (a = 0; a < strlen(node); ++a)
181 if (node[a] == '(') {
183 strcpy(&node[a], &node[a + 1]);
184 } while ((strlen(node) > 0) && (node[a] != ')'));
185 strcpy(&node[a], &node[a + 1]);
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]);
192 strcpy(&node[0], &node[1]);
193 for (a = 0; a < strlen(node); ++a)
197 /* strip anything to the left of a @ */
198 while ((strlen(node) > 0) && (haschar(node, '@') > 0))
199 strcpy(node, &node[1]);
201 /* strip anything to the left of a % */
202 while ((strlen(node) > 0) && (haschar(node, '%') > 0))
203 strcpy(node, &node[1]);
205 /* reduce multiple system bang paths to node!user */
206 while ((strlen(node) > 0) && (haschar(node, '!') > 1))
207 strcpy(node, &node[1]);
209 /* now get rid of the user portion of a node!user string */
210 for (a = 0; a < strlen(node); ++a)
214 /* strip leading and trailing spaces in all strings */
223 * Back end for convert_internet_address()
224 * (Compares an internet name [buffer1] and stores in [buffer2] if found)
226 void try_name(struct usersupp *us, void *data) {
227 struct trynamebuf *tnb;
228 tnb = (struct trynamebuf *)data;
230 if (!strncasecmp(tnb->buffer1, "cit", 3))
231 if (atol(&tnb->buffer1[3]) == us->usernum)
232 strcpy(tnb->buffer2, us->fullname);
234 if (!collapsed_strcmp(tnb->buffer1, us->fullname))
235 strcpy(tnb->buffer2, us->fullname);
237 if (us->uid != BBSUID)
238 if (!strcasecmp(tnb->buffer1, getpwuid(us->uid)->pw_name))
239 strcpy(tnb->buffer2, us->fullname);
244 * Convert an Internet email address to a Citadel user/host combination
246 int convert_internet_address(char *destuser, char *desthost, char *source)
251 struct quickroom qrbuf;
253 struct trynamebuf tnb;
256 process_rfc822_addr(source, user, node, name);
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.
262 if (!strcasecmp(node, config.c_fqdn)) {
263 strcpy(node, config.c_nodename);
266 /* Now try to resolve the name
267 * FIX ... do the multiple-addresses thing
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;
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);
293 strcpy(destuser, user);
294 strcpy(desthost, node);
295 return(rfc822_address_invalid); /* unknown error */
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.
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
310 int convert_field(struct CtdlMessage *msg, int beg, int end) {
317 char *user, *node, *name;
319 rfc822 = msg->cm_fields['M']; /* M field contains rfc822 text */
320 for (i = end; i >= beg; --i) {
321 if (rfc822[i] == ':') colonpos = i;
324 if (colonpos < 0) return(0); /* no colon? not a valid header line */
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);
332 lprintf(9, "Key=<%s> Value=<%s>\n", key, value);
334 /* Here's the big rfc822-to-citadel loop. */
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);
343 else if (!strcasecmp(key, "From")) {
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;
353 if (msg->cm_fields['N'] == NULL)
354 msg->cm_fields['N'] = node;
357 if (msg->cm_fields['H'] == NULL)
358 msg->cm_fields['H'] = name;
364 else if (!strcasecmp(key, "Subject")) {
365 if (msg->cm_fields['U'] == NULL)
366 msg->cm_fields['U'] = strdoop(value);
370 /* Clean up and move on. */
371 phree(key); /* Don't free 'value', it's actually the same buffer */
377 * Convert an RFC822 message (headers + body) to a CtdlMessage structure.
379 struct CtdlMessage *convert_internet_message(char *rfc822) {
381 struct CtdlMessage *msg;
387 msg = mallok(sizeof(struct CtdlMessage));
388 if (msg == NULL) return msg;
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;
396 lprintf(9, "Unconverted RFC822 message length = %d\n", strlen(rfc822));
402 /* Locate beginning and end of field, keeping in mind that
403 * some fields might be multiline
407 for (pos=beg; ((pos<=strlen(rfc822))&&(end<0)); ++pos) {
408 if ((rfc822[pos]=='\n')
409 && (!isspace(rfc822[pos+1]))) {
412 if ( (rfc822[pos]=='\n') /* done w. headers? */
413 && ( (rfc822[pos+1]=='\n')
414 ||(rfc822[pos+1]=='\r'))) {
421 /* At this point we have a field. Are we interested in it? */
422 converted = convert_field(msg, beg, end);
426 strcpy(&rfc822[beg], &rfc822[pos]);
431 /* If we've hit the end of the message, bail out */
432 if (pos > strlen(rfc822)) done = 1;
435 /* Follow-up sanity checks... */
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);
443 lprintf(9, "RFC822 length remaining after conversion = %d\n",