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>
35 #include "serv_extensions.h"
36 #include "sysdep_decls.h"
37 #include "citserver.h"
42 #include "internet_addressing.h"
45 #include "parsedate.h"
60 struct spamstrings_t *spamstrings = NULL;
64 * Return nonzero if the supplied name is an alias for this host.
66 int CtdlHostAlias(char *fqdn) {
70 char host[256], type[256];
72 if (fqdn == NULL) return(hostalias_nomatch);
73 if (strlen(fqdn) == 0) return(hostalias_nomatch);
74 if (!strcasecmp(fqdn, "localhost")) return(hostalias_localhost);
75 if (!strcasecmp(fqdn, config.c_fqdn)) return(hostalias_localhost);
76 if (!strcasecmp(fqdn, config.c_nodename)) return(hostalias_localhost);
77 if (inetcfg == NULL) return(hostalias_nomatch);
79 config_lines = num_tokens(inetcfg, '\n');
80 for (i=0; i<config_lines; ++i) {
81 extract_token(buf, inetcfg, i, '\n', sizeof buf);
82 extract_token(host, buf, 0, '|', sizeof host);
83 extract_token(type, buf, 1, '|', sizeof type);
85 if ( (!strcasecmp(type, "localhost"))
86 && (!strcasecmp(fqdn, host)))
87 return(hostalias_localhost);
89 if ( (!strcasecmp(type, "gatewaydomain"))
90 && (!strcasecmp(&fqdn[strlen(fqdn)-strlen(host)], host)))
91 return(hostalias_gatewaydomain);
93 if ( (!strcasecmp(type, "directory"))
94 && (!strcasecmp(&fqdn[strlen(fqdn)-strlen(host)], host)))
95 return(hostalias_directory);
99 return(hostalias_nomatch);
109 * Return 0 if a given string fuzzy-matches a Citadel user account
111 * FIXME ... this needs to be updated to handle aliases.
113 int fuzzy_match(struct ctdluser *us, char *matchstring) {
116 if ( (!strncasecmp(matchstring, "cit", 3))
117 && (atol(&matchstring[3]) == us->usernum)) {
122 for (a=0; a<strlen(us->fullname); ++a) {
123 if (!strncasecmp(&us->fullname[a],
124 matchstring, strlen(matchstring))) {
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 /* extract full name - first, it's From minus <userid> */
168 strcpy(name, rfc822);
169 stripout(name, '<', '>');
171 /* strip anything to the left of a bang */
172 while ((strlen(name) > 0) && (haschar(name, '!') > 0))
173 strcpy(name, &name[1]);
175 /* and anything to the right of a @ or % */
176 for (a = 0; a < strlen(name); ++a) {
183 /* but if there are parentheses, that changes the rules... */
184 if ((haschar(rfc822, '(') == 1) && (haschar(rfc822, ')') == 1)) {
185 strcpy(name, rfc822);
186 stripallbut(name, '(', ')');
189 /* but if there are a set of quotes, that supersedes everything */
190 if (haschar(rfc822, 34) == 2) {
191 strcpy(name, rfc822);
192 while ((strlen(name) > 0) && (name[0] != 34)) {
193 strcpy(&name[0], &name[1]);
195 strcpy(&name[0], &name[1]);
196 for (a = 0; a < strlen(name); ++a)
200 /* extract user id */
201 strcpy(user, rfc822);
203 /* first get rid of anything in parens */
204 stripout(user, '(', ')');
206 /* if there's a set of angle brackets, strip it down to that */
207 if ((haschar(user, '<') == 1) && (haschar(user, '>') == 1)) {
208 stripallbut(user, '<', '>');
211 /* strip anything to the left of a bang */
212 while ((strlen(user) > 0) && (haschar(user, '!') > 0))
213 strcpy(user, &user[1]);
215 /* and anything to the right of a @ or % */
216 for (a = 0; a < strlen(user); ++a) {
224 /* extract node name */
225 strcpy(node, rfc822);
227 /* first get rid of anything in parens */
228 stripout(node, '(', ')');
230 /* if there's a set of angle brackets, strip it down to that */
231 if ((haschar(node, '<') == 1) && (haschar(node, '>') == 1)) {
232 stripallbut(node, '<', '>');
235 /* If no node specified, tack ours on instead */
237 (haschar(node, '@')==0)
238 && (haschar(node, '%')==0)
239 && (haschar(node, '!')==0)
241 strcpy(node, config.c_nodename);
246 /* strip anything to the left of a @ */
247 while ((strlen(node) > 0) && (haschar(node, '@') > 0))
248 strcpy(node, &node[1]);
250 /* strip anything to the left of a % */
251 while ((strlen(node) > 0) && (haschar(node, '%') > 0))
252 strcpy(node, &node[1]);
254 /* reduce multiple system bang paths to node!user */
255 while ((strlen(node) > 0) && (haschar(node, '!') > 1))
256 strcpy(node, &node[1]);
258 /* now get rid of the user portion of a node!user string */
259 for (a = 0; a < strlen(node); ++a)
264 /* strip leading and trailing spaces in all strings */
269 /* If we processed a string that had the address in angle brackets
270 * but no name outside the brackets, we now have an empty name. In
271 * this case, use the user portion of the address as the name.
273 if ((strlen(name) == 0) && (strlen(user) > 0)) {
281 * convert_field() is a helper function for convert_internet_message().
282 * Given start/end positions for an rfc822 field, it converts it to a Citadel
283 * field if it wants to, and unfolds it if necessary.
285 * Returns 1 if the field was converted and inserted into the Citadel message
286 * structure, implying that the source field should be removed from the
289 int convert_field(struct CtdlMessage *msg, int beg, int end) {
302 rfc822 = msg->cm_fields['M']; /* M field contains rfc822 text */
303 for (i = end; i >= beg; --i) {
304 if (rfc822[i] == ':') colonpos = i;
307 if (colonpos < 0) return(0); /* no colon? not a valid header line */
309 key = malloc((end - beg) + 2);
310 safestrncpy(key, &rfc822[beg], (end-beg)+1);
311 key[colonpos - beg] = 0;
312 value = &key[(colonpos - beg) + 1];
313 unfold_rfc822_field(value);
316 * Here's the big rfc822-to-citadel loop.
319 /* Date/time is converted into a unix timestamp. If the conversion
320 * fails, we replace it with the time the message arrived locally.
322 if (!strcasecmp(key, "Date")) {
323 parsed_date = parsedate(value);
324 if (parsed_date < 0L) parsed_date = time(NULL);
325 snprintf(buf, sizeof buf, "%ld", (long)parsed_date );
326 if (msg->cm_fields['T'] == NULL)
327 msg->cm_fields['T'] = strdup(buf);
331 else if (!strcasecmp(key, "From")) {
332 process_rfc822_addr(value, user, node, name);
333 lprintf(CTDL_DEBUG, "Converted to <%s@%s> (%s)\n", user, node, name);
334 snprintf(addr, sizeof addr, "%s@%s", user, node);
335 if (msg->cm_fields['A'] == NULL)
336 msg->cm_fields['A'] = strdup(name);
338 if (msg->cm_fields['F'] == NULL)
339 msg->cm_fields['F'] = strdup(addr);
343 else if (!strcasecmp(key, "Subject")) {
344 if (msg->cm_fields['U'] == NULL)
345 msg->cm_fields['U'] = strdup(value);
349 else if (!strcasecmp(key, "To")) {
350 if (msg->cm_fields['R'] == NULL)
351 msg->cm_fields['R'] = strdup(value);
355 else if (!strcasecmp(key, "CC")) {
356 if (msg->cm_fields['Y'] == NULL)
357 msg->cm_fields['Y'] = strdup(value);
361 else if (!strcasecmp(key, "Message-ID")) {
362 if (msg->cm_fields['I'] != NULL) {
363 lprintf(CTDL_WARNING, "duplicate message id\n");
366 if (msg->cm_fields['I'] == NULL) {
367 msg->cm_fields['I'] = strdup(value);
369 /* Strip angle brackets */
370 while (haschar(msg->cm_fields['I'], '<') > 0) {
371 strcpy(&msg->cm_fields['I'][0],
372 &msg->cm_fields['I'][1]);
374 for (i = 0; i<strlen(msg->cm_fields['I']); ++i)
375 if (msg->cm_fields['I'][i] == '>')
376 msg->cm_fields['I'][i] = 0;
382 /* Clean up and move on. */
383 free(key); /* Don't free 'value', it's actually the same buffer */
389 * Convert an RFC822 message (headers + body) to a CtdlMessage structure.
390 * NOTE: the supplied buffer becomes part of the CtdlMessage structure, and
391 * will be deallocated when CtdlFreeMessage() is called. Therefore, the
392 * supplied buffer should be DEREFERENCED. It should not be freed or used
395 struct CtdlMessage *convert_internet_message(char *rfc822) {
397 struct CtdlMessage *msg;
398 int pos, beg, end, msglen;
403 msg = malloc(sizeof(struct CtdlMessage));
404 if (msg == NULL) return msg;
406 memset(msg, 0, sizeof(struct CtdlMessage));
407 msg->cm_magic = CTDLMESSAGE_MAGIC; /* self check */
408 msg->cm_anon_type = 0; /* never anonymous */
409 msg->cm_format_type = FMT_RFC822; /* internet message */
410 msg->cm_fields['M'] = rfc822;
417 /* Locate beginning and end of field, keeping in mind that
418 * some fields might be multiline
423 msglen = strlen(rfc822);
424 while ( (end < 0) && (done == 0) ) {
426 if ((rfc822[pos]=='\n')
427 && (!isspace(rfc822[pos+1]))) {
431 /* done with headers? */
432 if ( (rfc822[pos]=='\n')
433 && ( (rfc822[pos+1]=='\n')
434 ||(rfc822[pos+1]=='\r')) ) {
439 if (pos >= (msglen-1) ) {
448 /* At this point we have a field. Are we interested in it? */
449 converted = convert_field(msg, beg, end);
451 /* Strip the field out of the RFC822 header if we used it */
453 strcpy(&rfc822[beg], &rfc822[pos]);
457 /* If we've hit the end of the message, bail out */
458 if (pos > strlen(rfc822)) done = 1;
461 /* Follow-up sanity checks... */
463 /* If there's no timestamp on this message, set it to now. */
464 if (msg->cm_fields['T'] == NULL) {
465 snprintf(buf, sizeof buf, "%ld", (long)time(NULL));
466 msg->cm_fields['T'] = strdup(buf);
475 * Look for a particular header field in an RFC822 message text. If the
476 * requested field is found, it is unfolded (if necessary) and returned to
477 * the caller. The field name is stripped out, leaving only its contents.
478 * The caller is responsible for freeing the returned buffer. If the requested
479 * field is not present, or anything else goes wrong, it returns NULL.
481 char *rfc822_fetch_field(char *rfc822, char *fieldname) {
482 char *fieldbuf = NULL;
483 char *end_of_headers;
489 /* Should never happen, but sometimes we get stupid */
490 if (rfc822 == NULL) return(NULL);
491 if (fieldname == NULL) return(NULL);
493 snprintf(fieldhdr, sizeof fieldhdr, "%s:", fieldname);
495 /* Locate the end of the headers, so we don't run past that point */
496 end_of_headers = bmstrcasestr(rfc822, "\n\r\n");
497 if (end_of_headers == NULL) {
498 end_of_headers = bmstrcasestr(rfc822, "\n\n");
500 if (end_of_headers == NULL) return (NULL);
502 field_start = bmstrcasestr(rfc822, fieldhdr);
503 if (field_start == NULL) return(NULL);
504 if (field_start > end_of_headers) return(NULL);
506 fieldbuf = malloc(SIZ);
507 strcpy(fieldbuf, "");
510 ptr = memreadline(ptr, fieldbuf, SIZ-strlen(fieldbuf) );
511 while ( (isspace(ptr[0])) && (ptr < end_of_headers) ) {
512 strcat(fieldbuf, " ");
513 cont = &fieldbuf[strlen(fieldbuf)];
514 ptr = memreadline(ptr, cont, SIZ-strlen(fieldbuf) );
518 strcpy(fieldbuf, &fieldbuf[strlen(fieldhdr)]);
526 /*****************************************************************************
527 * DIRECTORY MANAGEMENT FUNCTIONS *
528 *****************************************************************************/
531 * Generate the index key for an Internet e-mail address to be looked up
534 void directory_key(char *key, char *addr) {
538 for (i=0; i<strlen(addr); ++i) {
539 if (!isspace(addr[i])) {
540 key[keylen++] = tolower(addr[i]);
545 lprintf(CTDL_DEBUG, "Directory key is <%s>\n", key);
550 /* Return nonzero if the supplied address is in a domain we keep in
553 int IsDirectory(char *addr) {
557 extract_token(domain, addr, 1, '@', sizeof domain);
560 h = CtdlHostAlias(domain);
562 if ( (h == hostalias_localhost) || (h == hostalias_directory) ) {
572 * Initialize the directory database (erasing anything already there)
574 void CtdlDirectoryInit(void) {
575 cdb_trunc(CDB_DIRECTORY);
580 * Add an Internet e-mail address to the directory for a user
582 void CtdlDirectoryAddUser(char *internet_addr, char *citadel_addr) {
585 lprintf(CTDL_DEBUG, "Dir: %s --> %s\n",
586 internet_addr, citadel_addr);
587 if (IsDirectory(internet_addr) == 0) return;
589 directory_key(key, internet_addr);
591 cdb_store(CDB_DIRECTORY, key, strlen(key),
592 citadel_addr, strlen(citadel_addr)+1 );
597 * Delete an Internet e-mail address from the directory.
599 * (NOTE: we don't actually use or need the citadel_addr variable; it's merely
600 * here because the callback API expects to be able to send it.)
602 void CtdlDirectoryDelUser(char *internet_addr, char *citadel_addr) {
605 directory_key(key, internet_addr);
606 cdb_delete(CDB_DIRECTORY, key, strlen(key) );
611 * Look up an Internet e-mail address in the directory.
612 * On success: returns 0, and Citadel address stored in 'target'
613 * On failure: returns nonzero
615 int CtdlDirectoryLookup(char *target, char *internet_addr, size_t targbuflen) {
616 struct cdbdata *cdbrec;
619 /* Dump it in there unchanged, just for kicks */
620 safestrncpy(target, internet_addr, targbuflen);
622 /* Only do lookups for addresses with hostnames in them */
623 if (num_tokens(internet_addr, '@') != 2) return(-1);
625 /* Only do lookups for domains in the directory */
626 if (IsDirectory(internet_addr) == 0) return(-1);
628 directory_key(key, internet_addr);
629 cdbrec = cdb_fetch(CDB_DIRECTORY, key, strlen(key) );
630 if (cdbrec != NULL) {
631 safestrncpy(target, cdbrec->ptr, targbuflen);
641 * Harvest any email addresses that someone might want to have in their
642 * "collected addresses" book.
644 char *harvest_collected_addresses(struct CtdlMessage *msg) {
647 char user[256], node[256], name[256];
652 if (msg == NULL) return(NULL);
656 if (msg->cm_fields['A'] != NULL) {
657 strcat(addr, msg->cm_fields['A']);
659 if (msg->cm_fields['F'] != NULL) {
661 strcat(addr, msg->cm_fields['F']);
663 if (IsDirectory(msg->cm_fields['F'])) {
668 if (is_harvestable) {
675 if (coll == NULL) return(NULL);
677 /* Scan both the R (To) and Y (CC) fields */
678 for (i = 0; i < 2; ++i) {
679 if (i == 0) field = 'R' ;
680 if (i == 1) field = 'Y' ;
682 if (msg->cm_fields[field] != NULL) {
683 for (j=0; j<num_tokens(msg->cm_fields[field], ','); ++j) {
684 extract_token(addr, msg->cm_fields[field], j, ',', sizeof addr);
685 process_rfc822_addr(addr, user, node, name);
686 h = CtdlHostAlias(node);
687 if ( (h != hostalias_localhost) && (h != hostalias_directory) ) {
688 coll = realloc(coll, strlen(coll) + strlen(addr) + 4);
689 if (coll == NULL) return(NULL);
690 if (strlen(coll) > 0) {
700 if (strlen(coll) == 0) {