4 * This file contains functions which handle the mapping of Internet addresses
5 * to users on the Citadel system.
17 #include <sys/types.h>
19 #if TIME_WITH_SYS_TIME
20 # include <sys/time.h>
24 # include <sys/time.h>
33 #include <libcitadel.h>
36 #include "sysdep_decls.h"
37 #include "citserver.h"
41 #include "internet_addressing.h"
44 #include "parsedate.h"
59 struct spamstrings_t *spamstrings = NULL;
63 * Return nonzero if the supplied name is an alias for this host.
65 int CtdlHostAlias(char *fqdn) {
69 char host[256], type[256];
71 if (fqdn == NULL) return(hostalias_nomatch);
72 if (IsEmptyStr(fqdn)) return(hostalias_nomatch);
73 if (!strcasecmp(fqdn, "localhost")) return(hostalias_localhost);
74 if (!strcasecmp(fqdn, config.c_fqdn)) return(hostalias_localhost);
75 if (!strcasecmp(fqdn, config.c_nodename)) return(hostalias_localhost);
76 if (inetcfg == NULL) return(hostalias_nomatch);
78 config_lines = num_tokens(inetcfg, '\n');
79 for (i=0; i<config_lines; ++i) {
80 extract_token(buf, inetcfg, i, '\n', sizeof buf);
81 extract_token(host, buf, 0, '|', sizeof host);
82 extract_token(type, buf, 1, '|', sizeof type);
84 if ( (!strcasecmp(type, "localhost"))
85 && (!strcasecmp(fqdn, host)))
86 return(hostalias_localhost);
88 if ( (!strcasecmp(type, "directory"))
89 && (!strcasecmp(&fqdn[strlen(fqdn)-strlen(host)], host)))
90 return(hostalias_directory);
92 if ( (!strcasecmp(type, "masqdomain"))
93 && (!strcasecmp(&fqdn[strlen(fqdn)-strlen(host)], host)))
94 return(hostalias_masq);
98 return(hostalias_nomatch);
108 * Return 0 if a given string fuzzy-matches a Citadel user account
110 * FIXME ... this needs to be updated to handle aliases.
112 int fuzzy_match(struct ctdluser *us, char *matchstring) {
116 if ( (!strncasecmp(matchstring, "cit", 3))
117 && (atol(&matchstring[3]) == us->usernum)) {
121 len = strlen(matchstring);
122 for (a=0; !IsEmptyStr(&us->fullname[a]); ++a) {
123 if (!strncasecmp(&us->fullname[a],
133 * Unfold a multi-line field into a single line, removing multi-whitespaces
135 void unfold_rfc822_field(char *field) {
139 striplt(field); /* remove leading/trailing whitespace */
141 /* convert non-space whitespace to spaces, and remove double blanks */
142 for (i=0; i<strlen(field); ++i) {
143 if (field[i]=='\"') quote = 1 - quote;
145 if (isspace(field[i])) field[i] = ' ';
146 while (isspace(field[i]) && isspace(field[i+1])) {
147 strcpy(&field[i+1], &field[i+2]);
156 * Split an RFC822-style address into userid, host, and full name
159 void process_rfc822_addr(const char *rfc822, char *user, char *node, char *name)
164 strcpy(node, config.c_fqdn);
167 if (rfc822 == NULL) return;
169 /* extract full name - first, it's From minus <userid> */
170 strcpy(name, rfc822);
171 stripout(name, '<', '>');
173 /* strip anything to the left of a bang */
174 while ((!IsEmptyStr(name)) && (haschar(name, '!') > 0))
175 strcpy(name, &name[1]);
177 /* and anything to the right of a @ or % */
178 for (a = 0; a < strlen(name); ++a) {
185 /* but if there are parentheses, that changes the rules... */
186 if ((haschar(rfc822, '(') == 1) && (haschar(rfc822, ')') == 1)) {
187 strcpy(name, rfc822);
188 stripallbut(name, '(', ')');
191 /* but if there are a set of quotes, that supersedes everything */
192 if (haschar(rfc822, 34) == 2) {
193 strcpy(name, rfc822);
194 while ((!IsEmptyStr(name)) && (name[0] != 34)) {
195 strcpy(&name[0], &name[1]);
197 strcpy(&name[0], &name[1]);
198 for (a = 0; a < strlen(name); ++a)
202 /* extract user id */
203 strcpy(user, rfc822);
205 /* first get rid of anything in parens */
206 stripout(user, '(', ')');
208 /* if there's a set of angle brackets, strip it down to that */
209 if ((haschar(user, '<') == 1) && (haschar(user, '>') == 1)) {
210 stripallbut(user, '<', '>');
213 /* strip anything to the left of a bang */
214 while ((!IsEmptyStr(user)) && (haschar(user, '!') > 0))
215 strcpy(user, &user[1]);
217 /* and anything to the right of a @ or % */
218 for (a = 0; a < strlen(user); ++a) {
226 /* extract node name */
227 strcpy(node, rfc822);
229 /* first get rid of anything in parens */
230 stripout(node, '(', ')');
232 /* if there's a set of angle brackets, strip it down to that */
233 if ((haschar(node, '<') == 1) && (haschar(node, '>') == 1)) {
234 stripallbut(node, '<', '>');
237 /* If no node specified, tack ours on instead */
239 (haschar(node, '@')==0)
240 && (haschar(node, '%')==0)
241 && (haschar(node, '!')==0)
243 strcpy(node, config.c_nodename);
248 /* strip anything to the left of a @ */
249 while ((!IsEmptyStr(node)) && (haschar(node, '@') > 0))
250 strcpy(node, &node[1]);
252 /* strip anything to the left of a % */
253 while ((!IsEmptyStr(node)) && (haschar(node, '%') > 0))
254 strcpy(node, &node[1]);
256 /* reduce multiple system bang paths to node!user */
257 while ((!IsEmptyStr(node)) && (haschar(node, '!') > 1))
258 strcpy(node, &node[1]);
260 /* now get rid of the user portion of a node!user string */
261 for (a = 0; a < strlen(node); ++a)
266 /* strip leading and trailing spaces in all strings */
271 /* If we processed a string that had the address in angle brackets
272 * but no name outside the brackets, we now have an empty name. In
273 * this case, use the user portion of the address as the name.
275 if ((IsEmptyStr(name)) && (!IsEmptyStr(user))) {
283 * convert_field() is a helper function for convert_internet_message().
284 * Given start/end positions for an rfc822 field, it converts it to a Citadel
285 * field if it wants to, and unfolds it if necessary.
287 * Returns 1 if the field was converted and inserted into the Citadel message
288 * structure, implying that the source field should be removed from the
291 int convert_field(struct CtdlMessage *msg, int beg, int end) {
304 rfc822 = msg->cm_fields['M']; /* M field contains rfc822 text */
305 for (i = end; i >= beg; --i) {
306 if (rfc822[i] == ':') colonpos = i;
309 if (colonpos < 0) return(0); /* no colon? not a valid header line */
311 key = malloc((end - beg) + 2);
312 safestrncpy(key, &rfc822[beg], (end-beg)+1);
313 key[colonpos - beg] = 0;
314 value = &key[(colonpos - beg) + 1];
315 unfold_rfc822_field(value);
318 * Here's the big rfc822-to-citadel loop.
321 /* Date/time is converted into a unix timestamp. If the conversion
322 * fails, we replace it with the time the message arrived locally.
324 if (!strcasecmp(key, "Date")) {
325 parsed_date = parsedate(value);
326 if (parsed_date < 0L) parsed_date = time(NULL);
327 snprintf(buf, sizeof buf, "%ld", (long)parsed_date );
328 if (msg->cm_fields['T'] == NULL)
329 msg->cm_fields['T'] = strdup(buf);
333 else if (!strcasecmp(key, "From")) {
334 process_rfc822_addr(value, user, node, name);
335 CtdlLogPrintf(CTDL_DEBUG, "Converted to <%s@%s> (%s)\n", user, node, name);
336 snprintf(addr, sizeof addr, "%s@%s", user, node);
337 if (msg->cm_fields['A'] == NULL)
338 msg->cm_fields['A'] = strdup(name);
340 if (msg->cm_fields['F'] == NULL)
341 msg->cm_fields['F'] = strdup(addr);
345 else if (!strcasecmp(key, "Subject")) {
346 if (msg->cm_fields['U'] == NULL)
347 msg->cm_fields['U'] = strdup(value);
351 else if (!strcasecmp(key, "To")) {
352 if (msg->cm_fields['R'] == NULL)
353 msg->cm_fields['R'] = strdup(value);
357 else if (!strcasecmp(key, "CC")) {
358 if (msg->cm_fields['Y'] == NULL)
359 msg->cm_fields['Y'] = strdup(value);
363 else if (!strcasecmp(key, "Message-ID")) {
364 if (msg->cm_fields['I'] != NULL) {
365 CtdlLogPrintf(CTDL_WARNING, "duplicate message id\n");
368 if (msg->cm_fields['I'] == NULL) {
369 msg->cm_fields['I'] = strdup(value);
371 /* Strip angle brackets */
372 while (haschar(msg->cm_fields['I'], '<') > 0) {
373 strcpy(&msg->cm_fields['I'][0],
374 &msg->cm_fields['I'][1]);
376 for (i = 0; i<strlen(msg->cm_fields['I']); ++i)
377 if (msg->cm_fields['I'][i] == '>')
378 msg->cm_fields['I'][i] = 0;
384 else if (!strcasecmp(key, "Return-Path")) {
385 if (msg->cm_fields['P'] == NULL)
386 msg->cm_fields['P'] = strdup(value);
390 else if (!strcasecmp(key, "Envelope-To")) {
391 if (msg->cm_fields['V'] == NULL)
392 msg->cm_fields['V'] = strdup(value);
396 else if (!strcasecmp(key, "References")) {
397 if (msg->cm_fields['W'] != NULL) {
398 free(msg->cm_fields['W']);
400 msg->cm_fields['W'] = strdup(value);
404 else if (!strcasecmp(key, "In-reply-to")) {
405 if (msg->cm_fields['W'] == NULL) { /* References: supersedes In-reply-to: */
406 msg->cm_fields['W'] = strdup(value);
413 /* Clean up and move on. */
414 free(key); /* Don't free 'value', it's actually the same buffer */
420 * Convert RFC822 references format (References) to Citadel references format (Weferences)
422 void convert_references_to_wefewences(char *str) {
423 int bracket_nesting = 0;
425 char *moveptr = NULL;
432 if (bracket_nesting < 0) bracket_nesting = 0;
434 if ((ch == '>') && (bracket_nesting == 0) && (*(ptr+1)) && (ptr>str) ) {
438 else if (bracket_nesting > 0) {
444 *moveptr = *(moveptr+1);
448 if (ch == '<') ++bracket_nesting;
455 * Convert an RFC822 message (headers + body) to a CtdlMessage structure.
456 * NOTE: the supplied buffer becomes part of the CtdlMessage structure, and
457 * will be deallocated when CtdlFreeMessage() is called. Therefore, the
458 * supplied buffer should be DEREFERENCED. It should not be freed or used
461 struct CtdlMessage *convert_internet_message(char *rfc822) {
463 struct CtdlMessage *msg;
464 int pos, beg, end, msglen;
469 msg = malloc(sizeof(struct CtdlMessage));
470 if (msg == NULL) return msg;
472 memset(msg, 0, sizeof(struct CtdlMessage));
473 msg->cm_magic = CTDLMESSAGE_MAGIC; /* self check */
474 msg->cm_anon_type = 0; /* never anonymous */
475 msg->cm_format_type = FMT_RFC822; /* internet message */
476 msg->cm_fields['M'] = rfc822;
483 /* Locate beginning and end of field, keeping in mind that
484 * some fields might be multiline
489 msglen = strlen(rfc822);
490 while ( (end < 0) && (done == 0) ) {
492 if ((rfc822[pos]=='\n')
493 && (!isspace(rfc822[pos+1]))) {
497 /* done with headers? */
498 if ( (rfc822[pos]=='\n')
499 && ( (rfc822[pos+1]=='\n')
500 ||(rfc822[pos+1]=='\r')) ) {
505 if (pos >= (msglen-1) ) {
514 /* At this point we have a field. Are we interested in it? */
515 converted = convert_field(msg, beg, end);
517 /* Strip the field out of the RFC822 header if we used it */
519 strcpy(&rfc822[beg], &rfc822[pos]);
523 /* If we've hit the end of the message, bail out */
524 if (pos > strlen(rfc822)) done = 1;
527 /* Follow-up sanity checks... */
529 /* If there's no timestamp on this message, set it to now. */
530 if (msg->cm_fields['T'] == NULL) {
531 snprintf(buf, sizeof buf, "%ld", (long)time(NULL));
532 msg->cm_fields['T'] = strdup(buf);
535 /* If a W (references, or rather, Wefewences) field is present, we
536 * have to convert it from RFC822 format to Citadel format.
538 if (msg->cm_fields['W'] != NULL) {
539 convert_references_to_wefewences(msg->cm_fields['W']);
548 * Look for a particular header field in an RFC822 message text. If the
549 * requested field is found, it is unfolded (if necessary) and returned to
550 * the caller. The field name is stripped out, leaving only its contents.
551 * The caller is responsible for freeing the returned buffer. If the requested
552 * field is not present, or anything else goes wrong, it returns NULL.
554 char *rfc822_fetch_field(char *rfc822, char *fieldname) {
555 char *fieldbuf = NULL;
556 char *end_of_headers;
562 /* Should never happen, but sometimes we get stupid */
563 if (rfc822 == NULL) return(NULL);
564 if (fieldname == NULL) return(NULL);
566 snprintf(fieldhdr, sizeof fieldhdr, "%s:", fieldname);
568 /* Locate the end of the headers, so we don't run past that point */
569 end_of_headers = bmstrcasestr(rfc822, "\n\r\n");
570 if (end_of_headers == NULL) {
571 end_of_headers = bmstrcasestr(rfc822, "\n\n");
573 if (end_of_headers == NULL) return (NULL);
575 field_start = bmstrcasestr(rfc822, fieldhdr);
576 if (field_start == NULL) return(NULL);
577 if (field_start > end_of_headers) return(NULL);
579 fieldbuf = malloc(SIZ);
580 strcpy(fieldbuf, "");
583 ptr = memreadline(ptr, fieldbuf, SIZ-strlen(fieldbuf) );
584 while ( (isspace(ptr[0])) && (ptr < end_of_headers) ) {
585 strcat(fieldbuf, " ");
586 cont = &fieldbuf[strlen(fieldbuf)];
587 ptr = memreadline(ptr, cont, SIZ-strlen(fieldbuf) );
591 strcpy(fieldbuf, &fieldbuf[strlen(fieldhdr)]);
599 /*****************************************************************************
600 * DIRECTORY MANAGEMENT FUNCTIONS *
601 *****************************************************************************/
604 * Generate the index key for an Internet e-mail address to be looked up
607 void directory_key(char *key, char *addr) {
611 for (i=0; !IsEmptyStr(&addr[i]); ++i) {
612 if (!isspace(addr[i])) {
613 key[keylen++] = tolower(addr[i]);
618 CtdlLogPrintf(CTDL_DEBUG, "Directory key is <%s>\n", key);
623 /* Return nonzero if the supplied address is in a domain we keep in
626 int IsDirectory(char *addr, int allow_masq_domains) {
630 extract_token(domain, addr, 1, '@', sizeof domain);
633 h = CtdlHostAlias(domain);
635 if ( (h == hostalias_masq) && allow_masq_domains)
638 if ( (h == hostalias_localhost) || (h == hostalias_directory) ) {
648 * Initialize the directory database (erasing anything already there)
650 void CtdlDirectoryInit(void) {
651 cdb_trunc(CDB_DIRECTORY);
656 * Add an Internet e-mail address to the directory for a user
658 void CtdlDirectoryAddUser(char *internet_addr, char *citadel_addr) {
661 CtdlLogPrintf(CTDL_DEBUG, "Dir: %s --> %s\n",
662 internet_addr, citadel_addr);
663 if (IsDirectory(internet_addr, 0) == 0) return;
665 directory_key(key, internet_addr);
667 cdb_store(CDB_DIRECTORY, key, strlen(key),
668 citadel_addr, strlen(citadel_addr)+1 );
673 * Delete an Internet e-mail address from the directory.
675 * (NOTE: we don't actually use or need the citadel_addr variable; it's merely
676 * here because the callback API expects to be able to send it.)
678 void CtdlDirectoryDelUser(char *internet_addr, char *citadel_addr) {
681 directory_key(key, internet_addr);
682 cdb_delete(CDB_DIRECTORY, key, strlen(key) );
687 * Look up an Internet e-mail address in the directory.
688 * On success: returns 0, and Citadel address stored in 'target'
689 * On failure: returns nonzero
691 int CtdlDirectoryLookup(char *target, char *internet_addr, size_t targbuflen) {
692 struct cdbdata *cdbrec;
695 /* Dump it in there unchanged, just for kicks */
696 safestrncpy(target, internet_addr, targbuflen);
698 /* Only do lookups for addresses with hostnames in them */
699 if (num_tokens(internet_addr, '@') != 2) return(-1);
701 /* Only do lookups for domains in the directory */
702 if (IsDirectory(internet_addr, 0) == 0) return(-1);
704 directory_key(key, internet_addr);
705 cdbrec = cdb_fetch(CDB_DIRECTORY, key, strlen(key) );
706 if (cdbrec != NULL) {
707 safestrncpy(target, cdbrec->ptr, targbuflen);
717 * Harvest any email addresses that someone might want to have in their
718 * "collected addresses" book.
720 char *harvest_collected_addresses(struct CtdlMessage *msg) {
723 char user[256], node[256], name[256];
728 if (msg == NULL) return(NULL);
732 if (msg->cm_fields['A'] != NULL) {
733 strcat(addr, msg->cm_fields['A']);
735 if (msg->cm_fields['F'] != NULL) {
737 strcat(addr, msg->cm_fields['F']);
739 if (IsDirectory(msg->cm_fields['F'], 0)) {
744 if (is_harvestable) {
751 if (coll == NULL) return(NULL);
753 /* Scan both the R (To) and Y (CC) fields */
754 for (i = 0; i < 2; ++i) {
755 if (i == 0) field = 'R' ;
756 if (i == 1) field = 'Y' ;
758 if (msg->cm_fields[field] != NULL) {
759 for (j=0; j<num_tokens(msg->cm_fields[field], ','); ++j) {
760 extract_token(addr, msg->cm_fields[field], j, ',', sizeof addr);
761 process_rfc822_addr(addr, user, node, name);
762 h = CtdlHostAlias(node);
763 if ( (h != hostalias_localhost) && (h != hostalias_directory) ) {
764 coll = realloc(coll, strlen(coll) + strlen(addr) + 4);
765 if (coll == NULL) return(NULL);
766 if (!IsEmptyStr(coll)) {
776 if (IsEmptyStr(coll)) {