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"
31 #include "internet_addressing.h"
34 #include "parsedate.h"
47 * Return nonzero if the supplied name is an alias for this host.
49 int CtdlHostAlias(char *fqdn) {
53 char host[256], type[256];
55 if (!strcasecmp(fqdn, config.c_fqdn)) return(hostalias_localhost);
56 if (!strcasecmp(fqdn, config.c_nodename)) return(hostalias_localhost);
57 if (inetcfg == NULL) return(hostalias_nomatch);
59 config_lines = num_tokens(inetcfg, '\n');
60 for (i=0; i<config_lines; ++i) {
61 extract_token(buf, inetcfg, i, '\n');
62 extract_token(host, buf, 0, '|');
63 extract_token(type, buf, 1, '|');
65 if ( (!strcasecmp(type, "localhost"))
66 && (!strcasecmp(fqdn, host)))
67 return(hostalias_localhost);
69 if ( (!strcasecmp(type, "gatewaydomain"))
70 && (!strcasecmp(&fqdn[strlen(fqdn)-strlen(host)], host)))
71 return(hostalias_gatewaydomain);
75 return(hostalias_nomatch);
85 * Return 0 if a given string fuzzy-matches a Citadel user account
87 * FIXME ... this needs to be updated to handle aliases.
89 int fuzzy_match(struct usersupp *us, char *matchstring) {
92 if ( (!strncasecmp(matchstring, "cit", 3))
93 && (atol(&matchstring[3]) == us->usernum)) {
98 for (a=0; a<strlen(us->fullname); ++a) {
99 if (!strncasecmp(&us->fullname[a],
100 matchstring, strlen(matchstring))) {
109 * Unfold a multi-line field into a single line, removing multi-whitespaces
111 void unfold_rfc822_field(char *field) {
115 striplt(field); /* remove leading/trailing whitespace */
117 /* convert non-space whitespace to spaces, and remove double blanks */
118 for (i=0; i<strlen(field); ++i) {
119 if (field[i]=='\"') quote = 1 - quote;
121 if (isspace(field[i])) field[i] = ' ';
122 while (isspace(field[i]) && isspace(field[i+1])) {
123 strcpy(&field[i+1], &field[i+2]);
132 * Split an RFC822-style address into userid, host, and full name
135 void process_rfc822_addr(char *rfc822, char *user, char *node, char *name)
140 strcpy(node, config.c_fqdn);
143 /* extract full name - first, it's From minus <userid> */
144 strcpy(name, rfc822);
145 for (a = 0; a < strlen(name); ++a) {
146 if (name[a] == '<') {
148 strcpy(&name[a], &name[a + 1]);
149 } while ((strlen(name) > 0) && (name[a] != '>'));
150 strcpy(&name[a], &name[a + 1]);
153 /* strip anything to the left of a bang */
154 while ((strlen(name) > 0) && (haschar(name, '!') > 0))
155 strcpy(name, &name[1]);
157 /* and anything to the right of a @ or % */
158 for (a = 0; a < strlen(name); ++a) {
165 /* but if there are parentheses, that changes the rules... */
166 if ((haschar(rfc822, '(') == 1) && (haschar(rfc822, ')') == 1)) {
167 strcpy(name, rfc822);
168 while ((strlen(name) > 0) && (name[0] != '(')) {
169 strcpy(&name[0], &name[1]);
171 strcpy(&name[0], &name[1]);
172 for (a = 0; a < strlen(name); ++a) {
173 if (name[a] == ')') {
178 /* but if there are a set of quotes, that supersedes everything */
179 if (haschar(rfc822, 34) == 2) {
180 strcpy(name, rfc822);
181 while ((strlen(name) > 0) && (name[0] != 34)) {
182 strcpy(&name[0], &name[1]);
184 strcpy(&name[0], &name[1]);
185 for (a = 0; a < strlen(name); ++a)
189 /* extract user id */
190 strcpy(user, rfc822);
192 /* first get rid of anything in parens */
193 for (a = 0; a < strlen(user); ++a)
194 if (user[a] == '(') {
196 strcpy(&user[a], &user[a + 1]);
197 } while ((strlen(user) > 0) && (user[a] != ')'));
198 strcpy(&user[a], &user[a + 1]);
200 /* if there's a set of angle brackets, strip it down to that */
201 if ((haschar(user, '<') == 1) && (haschar(user, '>') == 1)) {
202 while ((strlen(user) > 0) && (user[0] != '<')) {
203 strcpy(&user[0], &user[1]);
205 strcpy(&user[0], &user[1]);
206 for (a = 0; a < strlen(user); ++a)
210 /* strip anything to the left of a bang */
211 while ((strlen(user) > 0) && (haschar(user, '!') > 0))
212 strcpy(user, &user[1]);
214 /* and anything to the right of a @ or % */
215 for (a = 0; a < strlen(user); ++a) {
223 /* extract node name */
224 strcpy(node, rfc822);
226 /* first get rid of anything in parens */
227 for (a = 0; a < strlen(node); ++a)
228 if (node[a] == '(') {
230 strcpy(&node[a], &node[a + 1]);
231 } while ((strlen(node) > 0) && (node[a] != ')'));
232 strcpy(&node[a], &node[a + 1]);
234 /* if there's a set of angle brackets, strip it down to that */
235 if ((haschar(node, '<') == 1) && (haschar(node, '>') == 1)) {
236 while ((strlen(node) > 0) && (node[0] != '<')) {
237 strcpy(&node[0], &node[1]);
239 strcpy(&node[0], &node[1]);
240 for (a = 0; a < strlen(node); ++a)
245 /* If no node specified, tack ours on instead */
247 (haschar(node, '@')==0)
248 && (haschar(node, '%')==0)
249 && (haschar(node, '!')==0)
251 strcpy(node, config.c_nodename);
256 /* strip anything to the left of a @ */
257 while ((strlen(node) > 0) && (haschar(node, '@') > 0))
258 strcpy(node, &node[1]);
260 /* strip anything to the left of a % */
261 while ((strlen(node) > 0) && (haschar(node, '%') > 0))
262 strcpy(node, &node[1]);
264 /* reduce multiple system bang paths to node!user */
265 while ((strlen(node) > 0) && (haschar(node, '!') > 1))
266 strcpy(node, &node[1]);
268 /* now get rid of the user portion of a node!user string */
269 for (a = 0; a < strlen(node); ++a)
274 /* strip leading and trailing spaces in all strings */
283 * Back end for convert_internet_address()
284 * (Compares an internet name [buffer1] and stores in [buffer2] if found)
286 void try_name(struct usersupp *us, void *data) {
287 struct trynamebuf *tnb;
288 tnb = (struct trynamebuf *)data;
290 lprintf(9, "Trying <%s>\n", us->fullname);
292 if (!strncasecmp(tnb->buffer1, "cit", 3))
293 if (atol(&tnb->buffer1[3]) == us->usernum)
294 strcpy(tnb->buffer2, us->fullname);
296 if (!collapsed_strcmp(tnb->buffer1, us->fullname))
297 strcpy(tnb->buffer2, us->fullname);
299 if (us->uid != BBSUID)
300 if (!strcasecmp(tnb->buffer1, getpwuid(us->uid)->pw_name))
301 strcpy(tnb->buffer2, us->fullname);
306 * Convert an Internet email address to a Citadel user/host combination
308 int convert_internet_address(char *destuser, char *desthost, char *source)
313 struct quickroom qrbuf;
316 struct trynamebuf tnb;
319 char sourcealias[1024];
321 safestrncpy(sourcealias, source, sizeof(sourcealias) );
326 process_rfc822_addr(sourcealias, user, node, name);
327 lprintf(9, "process_rfc822_addr() converted to <%s@%s> (%s)\n",
330 /* Map the FQDN to a Citadel node name
332 hostalias = CtdlHostAlias(node);
334 case hostalias_localhost:
335 strcpy(node, config.c_nodename);
338 case hostalias_gatewaydomain:
339 extract_token(buf, node, 0, '.');
340 safestrncpy(node, buf, sizeof buf);
343 /* Now try to resolve the name
344 * FIXME ... do the multiple-addresses thing
346 if (!strcasecmp(node, config.c_nodename)) {
349 /* First, see if we hit an alias. Don't do this more than
350 * a few times, in case we accidentally hit an alias loop
352 strcpy(sourcealias, user);
354 if ( (strcasecmp(user, sourcealias)) && (++passes < 3) )
357 /* Try all local rooms */
358 if (!strncasecmp(user, "room_", 5)) {
359 strcpy(name, &user[5]);
360 for (i=0; i<strlen(name); ++i)
361 if (name[i]=='_') name[i]=' ';
362 if (getroom(&qrbuf, name) == 0) {
363 strcpy(destuser, qrbuf.QRname);
364 strcpy(desthost, config.c_nodename);
365 return rfc822_room_delivery;
369 /* Try all local users */
370 strcpy(destuser, user);
371 strcpy(desthost, config.c_nodename);
372 strcpy(tnb.buffer1, user);
373 strcpy(tnb.buffer2, "");
374 ForEachUser(try_name, &tnb);
375 if (strlen(tnb.buffer2) == 0) return(rfc822_no_such_user);
376 strcpy(destuser, tnb.buffer2);
377 return(rfc822_address_locally_validated);
380 strcpy(destuser, user);
381 strcpy(desthost, node);
382 if (hostalias == hostalias_gatewaydomain)
383 return(rfc822_address_on_citadel_network);
384 return(rfc822_address_nonlocal);
391 * convert_field() is a helper function for convert_internet_message().
392 * Given start/end positions for an rfc822 field, it converts it to a Citadel
393 * field if it wants to, and unfolds it if necessary.
395 * Returns 1 if the field was converted and inserted into the Citadel message
396 * structure, implying that the source field should be removed from the
399 int convert_field(struct CtdlMessage *msg, int beg, int end) {
412 rfc822 = msg->cm_fields['M']; /* M field contains rfc822 text */
413 for (i = end; i >= beg; --i) {
414 if (rfc822[i] == ':') colonpos = i;
417 if (colonpos < 0) return(0); /* no colon? not a valid header line */
419 key = mallok((end - beg) + 2);
420 safestrncpy(key, &rfc822[beg], (end-beg)+1);
421 key[colonpos - beg] = 0;
422 value = &key[(colonpos - beg) + 1];
423 unfold_rfc822_field(value);
425 lprintf(9, "Key=<%s> Value=<%s>\n", key, value);
428 * Here's the big rfc822-to-citadel loop.
431 /* Date/time is converted into a unix timestamp. If the conversion
432 * fails, we replace it with the time the message arrived locally.
434 if (!strcasecmp(key, "Date")) {
435 parsed_date = parsedate(value);
436 if (parsed_date < 0L) parsed_date = time(NULL);
437 lprintf(9, "Parsed date is %s",
438 asctime(localtime(&parsed_date)));
439 snprintf(buf, sizeof buf, "%ld", parsed_date );
440 if (msg->cm_fields['T'] == NULL)
441 msg->cm_fields['T'] = strdoop(buf);
445 else if (!strcasecmp(key, "From")) {
446 process_rfc822_addr(value, user, node, name);
447 lprintf(9, "Converted to <%s@%s> (%s)\n", user, node, name);
448 snprintf(addr, sizeof addr, "%s@%s", user, node);
449 if (msg->cm_fields['A'] == NULL)
450 msg->cm_fields['A'] = strdoop(name);
452 if (msg->cm_fields['F'] == NULL)
453 msg->cm_fields['F'] = strdoop(addr);
457 else if (!strcasecmp(key, "Subject")) {
458 if (msg->cm_fields['U'] == NULL)
459 msg->cm_fields['U'] = strdoop(value);
463 else if (!strcasecmp(key, "Message-ID")) {
464 if (msg->cm_fields['I'] != NULL) {
465 lprintf(5, "duplicate message id\n");
468 if (msg->cm_fields['I'] == NULL) {
469 msg->cm_fields['I'] = strdoop(value);
471 /* Strip angle brackets */
472 if ((haschar(msg->cm_fields['I'], '<') == 1)
473 && (haschar(msg->cm_fields['I'], '>') == 1)) {
474 while ((strlen(msg->cm_fields['I']) > 0)
475 && (msg->cm_fields['I'][0] != '<')) {
476 strcpy(&msg->cm_fields['I'][0],
477 &msg->cm_fields['I'][1]);
479 strcpy(&msg->cm_fields['I'][0],
480 &msg->cm_fields['I'][1]);
481 for (i = 0; i<strlen(msg->cm_fields['I']); ++i)
482 if (msg->cm_fields['I'][i] == '>')
483 msg->cm_fields['I'][i] = 0;
487 if (msg->cm_fields['I'] != NULL) {
488 lprintf(9, "Converted message id <%s>\n",
489 msg->cm_fields['I'] );
494 /* Clean up and move on. */
495 phree(key); /* Don't free 'value', it's actually the same buffer */
501 * Convert an RFC822 message (headers + body) to a CtdlMessage structure.
503 struct CtdlMessage *convert_internet_message(char *rfc822) {
505 struct CtdlMessage *msg;
511 msg = mallok(sizeof(struct CtdlMessage));
512 if (msg == NULL) return msg;
514 memset(msg, 0, sizeof(struct CtdlMessage));
515 msg->cm_magic = CTDLMESSAGE_MAGIC; /* self check */
516 msg->cm_anon_type = 0; /* never anonymous */
517 msg->cm_format_type = FMT_RFC822; /* internet message */
518 msg->cm_fields['M'] = rfc822;
520 lprintf(9, "Unconverted RFC822 message length = %d\n", strlen(rfc822));
526 TRACE; lprintf(9, "I field is %s\n", msg->cm_fields['I']);
527 /* Locate beginning and end of field, keeping in mind that
528 * some fields might be multiline
532 for (pos=beg; ((pos<=strlen(rfc822))&&(end<0)); ++pos) {
533 if ((rfc822[pos]=='\n')
534 && (!isspace(rfc822[pos+1]))) {
537 if ( (rfc822[pos]=='\n') /* done w. headers? */
538 && ( (rfc822[pos+1]=='\n')
539 ||(rfc822[pos+1]=='\r'))) {
546 /* At this point we have a field. Are we interested in it? */
547 converted = convert_field(msg, beg, end);
549 /* Strip the field out of the RFC822 header if we used it */
551 strcpy(&rfc822[beg], &rfc822[pos]);
555 /* If we've hit the end of the message, bail out */
556 if (pos > strlen(rfc822)) done = 1;
559 /* Follow-up sanity checks... */
560 TRACE; lprintf(9, "I field is %s\n", msg->cm_fields['I']);
562 /* If there's no timestamp on this message, set it to now. */
563 if (msg->cm_fields['T'] == NULL) {
564 snprintf(buf, sizeof buf, "%ld", time(NULL));
565 msg->cm_fields['T'] = strdoop(buf);
568 TRACE; lprintf(9, "I field is %s\n", msg->cm_fields['I']);
569 lprintf(9, "RFC822 length remaining after conversion = %d\n",