4 * This file contains functions which handle the mapping of Internet addresses
5 * to users on the Citadel system.
21 #include <sys/types.h>
23 #if TIME_WITH_SYS_TIME
24 # include <sys/time.h>
28 # include <sys/time.h>
39 #include "dynloader.h"
40 #include "sysdep_decls.h"
41 #include "citserver.h"
46 #include "internet_addressing.h"
49 #include "parsedate.h"
62 * Return nonzero if the supplied name is an alias for this host.
64 int CtdlHostAlias(char *fqdn) {
68 char host[SIZ], type[SIZ];
70 if (!strcasecmp(fqdn, config.c_fqdn)) return(hostalias_localhost);
71 if (!strcasecmp(fqdn, config.c_nodename)) return(hostalias_localhost);
72 if (inetcfg == NULL) return(hostalias_nomatch);
74 config_lines = num_tokens(inetcfg, '\n');
75 for (i=0; i<config_lines; ++i) {
76 extract_token(buf, inetcfg, i, '\n');
77 extract_token(host, buf, 0, '|');
78 extract_token(type, buf, 1, '|');
80 if ( (!strcasecmp(type, "localhost"))
81 && (!strcasecmp(fqdn, host)))
82 return(hostalias_localhost);
84 if ( (!strcasecmp(type, "gatewaydomain"))
85 && (!strcasecmp(&fqdn[strlen(fqdn)-strlen(host)], host)))
86 return(hostalias_gatewaydomain);
90 return(hostalias_nomatch);
100 * Return 0 if a given string fuzzy-matches a Citadel user account
102 * FIXME ... this needs to be updated to handle aliases.
104 int fuzzy_match(struct usersupp *us, char *matchstring) {
107 if ( (!strncasecmp(matchstring, "cit", 3))
108 && (atol(&matchstring[3]) == us->usernum)) {
113 for (a=0; a<strlen(us->fullname); ++a) {
114 if (!strncasecmp(&us->fullname[a],
115 matchstring, strlen(matchstring))) {
124 * Unfold a multi-line field into a single line, removing multi-whitespaces
126 void unfold_rfc822_field(char *field) {
130 striplt(field); /* remove leading/trailing whitespace */
132 /* convert non-space whitespace to spaces, and remove double blanks */
133 for (i=0; i<strlen(field); ++i) {
134 if (field[i]=='\"') quote = 1 - quote;
136 if (isspace(field[i])) field[i] = ' ';
137 while (isspace(field[i]) && isspace(field[i+1])) {
138 strcpy(&field[i+1], &field[i+2]);
147 * Split an RFC822-style address into userid, host, and full name
150 void process_rfc822_addr(char *rfc822, char *user, char *node, char *name)
155 strcpy(node, config.c_fqdn);
158 /* extract full name - first, it's From minus <userid> */
159 strcpy(name, rfc822);
160 stripout(name, '<', '>');
162 /* strip anything to the left of a bang */
163 while ((strlen(name) > 0) && (haschar(name, '!') > 0))
164 strcpy(name, &name[1]);
166 /* and anything to the right of a @ or % */
167 for (a = 0; a < strlen(name); ++a) {
174 /* but if there are parentheses, that changes the rules... */
175 if ((haschar(rfc822, '(') == 1) && (haschar(rfc822, ')') == 1)) {
176 strcpy(name, rfc822);
177 stripallbut(name, '(', ')');
180 /* but if there are a set of quotes, that supersedes everything */
181 if (haschar(rfc822, 34) == 2) {
182 strcpy(name, rfc822);
183 while ((strlen(name) > 0) && (name[0] != 34)) {
184 strcpy(&name[0], &name[1]);
186 strcpy(&name[0], &name[1]);
187 for (a = 0; a < strlen(name); ++a)
191 /* extract user id */
192 strcpy(user, rfc822);
194 /* first get rid of anything in parens */
195 stripout(user, '(', ')');
197 /* if there's a set of angle brackets, strip it down to that */
198 if ((haschar(user, '<') == 1) && (haschar(user, '>') == 1)) {
199 stripallbut(user, '<', '>');
202 /* strip anything to the left of a bang */
203 while ((strlen(user) > 0) && (haschar(user, '!') > 0))
204 strcpy(user, &user[1]);
206 /* and anything to the right of a @ or % */
207 for (a = 0; a < strlen(user); ++a) {
215 /* extract node name */
216 strcpy(node, rfc822);
218 /* first get rid of anything in parens */
219 stripout(node, '(', ')');
221 /* if there's a set of angle brackets, strip it down to that */
222 if ((haschar(node, '<') == 1) && (haschar(node, '>') == 1)) {
223 stripallbut(node, '<', '>');
226 /* If no node specified, tack ours on instead */
228 (haschar(node, '@')==0)
229 && (haschar(node, '%')==0)
230 && (haschar(node, '!')==0)
232 strcpy(node, config.c_nodename);
237 /* strip anything to the left of a @ */
238 while ((strlen(node) > 0) && (haschar(node, '@') > 0))
239 strcpy(node, &node[1]);
241 /* strip anything to the left of a % */
242 while ((strlen(node) > 0) && (haschar(node, '%') > 0))
243 strcpy(node, &node[1]);
245 /* reduce multiple system bang paths to node!user */
246 while ((strlen(node) > 0) && (haschar(node, '!') > 1))
247 strcpy(node, &node[1]);
249 /* now get rid of the user portion of a node!user string */
250 for (a = 0; a < strlen(node); ++a)
255 /* strip leading and trailing spaces in all strings */
264 * Back end for convert_internet_address()
265 * (Compares an internet name [buffer1] and stores in [buffer2] if found)
267 void try_name(struct usersupp *us, void *data) {
269 struct trynamebuf *tnb;
270 tnb = (struct trynamebuf *)data;
272 if (!strncasecmp(tnb->buffer1, "cit", 3))
273 if (atol(&tnb->buffer1[3]) == us->usernum)
274 strcpy(tnb->buffer2, us->fullname);
276 if (!collapsed_strcmp(tnb->buffer1, us->fullname))
277 strcpy(tnb->buffer2, us->fullname);
279 if (us->uid != BBSUID) {
280 pw = getpwuid(us->uid);
282 if (!strcasecmp(tnb->buffer1, pw->pw_name)) {
283 strcpy(tnb->buffer2, us->fullname);
291 * Convert an Internet email address to a Citadel user/host combination
293 int convert_internet_address(char *destuser, char *desthost, char *source)
298 struct quickroom qrbuf;
301 struct trynamebuf tnb;
304 char sourcealias[1024];
306 safestrncpy(sourcealias, source, sizeof(sourcealias) );
311 process_rfc822_addr(sourcealias, user, node, name);
312 lprintf(9, "process_rfc822_addr() converted to <%s@%s> (%s)\n",
315 /* Map the FQDN to a Citadel node name
317 hostalias = CtdlHostAlias(node);
319 case hostalias_localhost:
320 strcpy(node, config.c_nodename);
323 case hostalias_gatewaydomain:
324 extract_token(buf, node, 0, '.');
325 safestrncpy(node, buf, sizeof buf);
328 /* Now try to resolve the name
329 * FIXME ... do the multiple-addresses thing
331 if (!strcasecmp(node, config.c_nodename)) {
334 /* First, see if we hit an alias. Don't do this more than
335 * a few times, in case we accidentally hit an alias loop
337 strcpy(sourcealias, user);
339 if ( (strcasecmp(user, sourcealias)) && (++passes < 3) )
342 /* Try all local rooms */
343 if (!strncasecmp(user, "room_", 5)) {
344 strcpy(name, &user[5]);
345 for (i=0; i<strlen(name); ++i)
346 if (name[i]=='_') name[i]=' ';
347 if (getroom(&qrbuf, name) == 0) {
348 strcpy(destuser, qrbuf.QRname);
349 strcpy(desthost, config.c_nodename);
350 return rfc822_room_delivery;
354 /* Try all local users */
355 strcpy(destuser, user);
356 strcpy(desthost, config.c_nodename);
357 strcpy(tnb.buffer1, user);
358 strcpy(tnb.buffer2, "");
359 ForEachUser(try_name, &tnb);
360 if (strlen(tnb.buffer2) == 0) return(rfc822_no_such_user);
361 strcpy(destuser, tnb.buffer2);
362 return(rfc822_address_locally_validated);
365 strcpy(destuser, user);
366 strcpy(desthost, node);
367 if (hostalias == hostalias_gatewaydomain)
368 return(rfc822_address_on_citadel_network);
369 return(rfc822_address_nonlocal);
376 * convert_field() is a helper function for convert_internet_message().
377 * Given start/end positions for an rfc822 field, it converts it to a Citadel
378 * field if it wants to, and unfolds it if necessary.
380 * Returns 1 if the field was converted and inserted into the Citadel message
381 * structure, implying that the source field should be removed from the
384 int convert_field(struct CtdlMessage *msg, int beg, int end) {
397 rfc822 = msg->cm_fields['M']; /* M field contains rfc822 text */
398 for (i = end; i >= beg; --i) {
399 if (rfc822[i] == ':') colonpos = i;
402 if (colonpos < 0) return(0); /* no colon? not a valid header line */
404 key = mallok((end - beg) + 2);
405 safestrncpy(key, &rfc822[beg], (end-beg)+1);
406 key[colonpos - beg] = 0;
407 value = &key[(colonpos - beg) + 1];
408 unfold_rfc822_field(value);
411 * Here's the big rfc822-to-citadel loop.
414 /* Date/time is converted into a unix timestamp. If the conversion
415 * fails, we replace it with the time the message arrived locally.
417 if (!strcasecmp(key, "Date")) {
418 parsed_date = parsedate(value);
419 if (parsed_date < 0L) parsed_date = time(NULL);
420 snprintf(buf, sizeof buf, "%ld", parsed_date );
421 if (msg->cm_fields['T'] == NULL)
422 msg->cm_fields['T'] = strdoop(buf);
426 else if (!strcasecmp(key, "From")) {
427 process_rfc822_addr(value, user, node, name);
428 lprintf(9, "Converted to <%s@%s> (%s)\n", user, node, name);
429 snprintf(addr, sizeof addr, "%s@%s", user, node);
430 if (msg->cm_fields['A'] == NULL)
431 msg->cm_fields['A'] = strdoop(name);
433 if (msg->cm_fields['F'] == NULL)
434 msg->cm_fields['F'] = strdoop(addr);
438 else if (!strcasecmp(key, "Subject")) {
439 if (msg->cm_fields['U'] == NULL)
440 msg->cm_fields['U'] = strdoop(value);
444 else if (!strcasecmp(key, "Message-ID")) {
445 if (msg->cm_fields['I'] != NULL) {
446 lprintf(5, "duplicate message id\n");
449 if (msg->cm_fields['I'] == NULL) {
450 msg->cm_fields['I'] = strdoop(value);
452 /* Strip angle brackets */
453 while (haschar(msg->cm_fields['I'], '<') > 0) {
454 strcpy(&msg->cm_fields['I'][0],
455 &msg->cm_fields['I'][1]);
457 for (i = 0; i<strlen(msg->cm_fields['I']); ++i)
458 if (msg->cm_fields['I'][i] == '>')
459 msg->cm_fields['I'][i] = 0;
465 /* Clean up and move on. */
466 phree(key); /* Don't free 'value', it's actually the same buffer */
472 * Convert an RFC822 message (headers + body) to a CtdlMessage structure.
473 * NOTE: the supplied buffer becomes part of the CtdlMessage structure, and
474 * will be deallocated when CtdlFreeMessage() is called. Therefore, the
475 * supplied buffer should be DEREFERENCED. It should not be freed or used
478 struct CtdlMessage *convert_internet_message(char *rfc822) {
480 struct CtdlMessage *msg;
481 int pos, beg, end, msglen;
486 msg = mallok(sizeof(struct CtdlMessage));
487 if (msg == NULL) return msg;
489 memset(msg, 0, sizeof(struct CtdlMessage));
490 msg->cm_magic = CTDLMESSAGE_MAGIC; /* self check */
491 msg->cm_anon_type = 0; /* never anonymous */
492 msg->cm_format_type = FMT_RFC822; /* internet message */
493 msg->cm_fields['M'] = rfc822;
495 lprintf(9, "Unconverted RFC822 message length = %d\n", strlen(rfc822));
501 /* Locate beginning and end of field, keeping in mind that
502 * some fields might be multiline
507 msglen = strlen(rfc822);
508 while ( (end < 0) && (done == 0) ) {
510 if ((rfc822[pos]=='\n')
511 && (!isspace(rfc822[pos+1]))) {
515 /* done with headers? */
516 if ( ((rfc822[pos]=='\n')
517 ||(rfc822[pos]=='\r') )
518 && ( (rfc822[pos+1]=='\n')
519 ||(rfc822[pos+1]=='\r')) ) {
524 if (pos >= (msglen-1) ) {
533 /* At this point we have a field. Are we interested in it? */
534 converted = convert_field(msg, beg, end);
536 /* Strip the field out of the RFC822 header if we used it */
538 strcpy(&rfc822[beg], &rfc822[pos]);
542 /* If we've hit the end of the message, bail out */
543 if (pos > strlen(rfc822)) done = 1;
546 /* Follow-up sanity checks... */
548 /* If there's no timestamp on this message, set it to now. */
549 if (msg->cm_fields['T'] == NULL) {
550 snprintf(buf, sizeof buf, "%ld", time(NULL));
551 msg->cm_fields['T'] = strdoop(buf);
554 lprintf(9, "RFC822 length remaining after conversion = %d\n",
562 * Look for a particular header field in an RFC822 message text. If the
563 * requested field is found, it is unfolded (if necessary) and returned to
564 * the caller. The field name is stripped out, leaving only its contents.
565 * The caller is responsible for freeing the returned buffer. If the requested
566 * field is not present, or anything else goes wrong, it returns NULL.
568 char *rfc822_fetch_field(char *rfc822, char *fieldname) {
573 char *fieldbuf = NULL;
575 /* Should never happen, but sometimes we get stupid */
576 if (rfc822 == NULL) return(NULL);
577 if (fieldname == NULL) return(NULL);
581 /* Locate beginning and end of field, keeping in mind that
582 * some fields might be multiline
586 for (pos=beg; ((pos<=strlen(rfc822))&&(end<0)); ++pos) {
587 if ((rfc822[pos]=='\n')
588 && (!isspace(rfc822[pos+1]))) {
591 if ( (rfc822[pos]=='\n') /* done w. headers? */
592 && ( (rfc822[pos+1]=='\n')
593 ||(rfc822[pos+1]=='\r'))) {
600 /* At this point we have a field. Is it The One? */
602 fieldbuf = mallok((end-beg)+3);
603 if (fieldbuf == NULL) return(NULL);
604 safestrncpy(fieldbuf, &rfc822[beg], (end-beg)+1);
605 unfold_rfc822_field(fieldbuf);
607 for (i = strlen(fieldbuf); i >= 0; --i) {
608 if (fieldbuf[i] == ':') colonpos = i;
611 fieldbuf[colonpos] = 0;
612 if (!strcasecmp(fieldbuf, fieldname)) {
613 strcpy(fieldbuf, &fieldbuf[colonpos+1]);
621 /* If we've hit the end of the message, bail out */
622 if (pos > strlen(rfc822)) done = 1;