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 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 ((strlen(name) > 0) && (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 ((strlen(name) > 0) && (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 ((strlen(user) > 0) && (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 ((strlen(node) > 0) && (haschar(node, '@') > 0))
250 strcpy(node, &node[1]);
252 /* strip anything to the left of a % */
253 while ((strlen(node) > 0) && (haschar(node, '%') > 0))
254 strcpy(node, &node[1]);
256 /* reduce multiple system bang paths to node!user */
257 while ((strlen(node) > 0) && (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 ((strlen(name) == 0) && (strlen(user) > 0)) {
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 lprintf(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 lprintf(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 /* Clean up and move on. */
397 free(key); /* Don't free 'value', it's actually the same buffer */
403 * Convert an RFC822 message (headers + body) to a CtdlMessage structure.
404 * NOTE: the supplied buffer becomes part of the CtdlMessage structure, and
405 * will be deallocated when CtdlFreeMessage() is called. Therefore, the
406 * supplied buffer should be DEREFERENCED. It should not be freed or used
409 struct CtdlMessage *convert_internet_message(char *rfc822) {
411 struct CtdlMessage *msg;
412 int pos, beg, end, msglen;
417 msg = malloc(sizeof(struct CtdlMessage));
418 if (msg == NULL) return msg;
420 memset(msg, 0, sizeof(struct CtdlMessage));
421 msg->cm_magic = CTDLMESSAGE_MAGIC; /* self check */
422 msg->cm_anon_type = 0; /* never anonymous */
423 msg->cm_format_type = FMT_RFC822; /* internet message */
424 msg->cm_fields['M'] = rfc822;
431 /* Locate beginning and end of field, keeping in mind that
432 * some fields might be multiline
437 msglen = strlen(rfc822);
438 while ( (end < 0) && (done == 0) ) {
440 if ((rfc822[pos]=='\n')
441 && (!isspace(rfc822[pos+1]))) {
445 /* done with headers? */
446 if ( (rfc822[pos]=='\n')
447 && ( (rfc822[pos+1]=='\n')
448 ||(rfc822[pos+1]=='\r')) ) {
453 if (pos >= (msglen-1) ) {
462 /* At this point we have a field. Are we interested in it? */
463 converted = convert_field(msg, beg, end);
465 /* Strip the field out of the RFC822 header if we used it */
467 strcpy(&rfc822[beg], &rfc822[pos]);
471 /* If we've hit the end of the message, bail out */
472 if (pos > strlen(rfc822)) done = 1;
475 /* Follow-up sanity checks... */
477 /* If there's no timestamp on this message, set it to now. */
478 if (msg->cm_fields['T'] == NULL) {
479 snprintf(buf, sizeof buf, "%ld", (long)time(NULL));
480 msg->cm_fields['T'] = strdup(buf);
489 * Look for a particular header field in an RFC822 message text. If the
490 * requested field is found, it is unfolded (if necessary) and returned to
491 * the caller. The field name is stripped out, leaving only its contents.
492 * The caller is responsible for freeing the returned buffer. If the requested
493 * field is not present, or anything else goes wrong, it returns NULL.
495 char *rfc822_fetch_field(char *rfc822, char *fieldname) {
496 char *fieldbuf = NULL;
497 char *end_of_headers;
503 /* Should never happen, but sometimes we get stupid */
504 if (rfc822 == NULL) return(NULL);
505 if (fieldname == NULL) return(NULL);
507 snprintf(fieldhdr, sizeof fieldhdr, "%s:", fieldname);
509 /* Locate the end of the headers, so we don't run past that point */
510 end_of_headers = bmstrcasestr(rfc822, "\n\r\n");
511 if (end_of_headers == NULL) {
512 end_of_headers = bmstrcasestr(rfc822, "\n\n");
514 if (end_of_headers == NULL) return (NULL);
516 field_start = bmstrcasestr(rfc822, fieldhdr);
517 if (field_start == NULL) return(NULL);
518 if (field_start > end_of_headers) return(NULL);
520 fieldbuf = malloc(SIZ);
521 strcpy(fieldbuf, "");
524 ptr = memreadline(ptr, fieldbuf, SIZ-strlen(fieldbuf) );
525 while ( (isspace(ptr[0])) && (ptr < end_of_headers) ) {
526 strcat(fieldbuf, " ");
527 cont = &fieldbuf[strlen(fieldbuf)];
528 ptr = memreadline(ptr, cont, SIZ-strlen(fieldbuf) );
532 strcpy(fieldbuf, &fieldbuf[strlen(fieldhdr)]);
540 /*****************************************************************************
541 * DIRECTORY MANAGEMENT FUNCTIONS *
542 *****************************************************************************/
545 * Generate the index key for an Internet e-mail address to be looked up
548 void directory_key(char *key, char *addr) {
552 for (i=0; i<strlen(addr); ++i) {
553 if (!isspace(addr[i])) {
554 key[keylen++] = tolower(addr[i]);
559 lprintf(CTDL_DEBUG, "Directory key is <%s>\n", key);
564 /* Return nonzero if the supplied address is in a domain we keep in
567 int IsDirectory(char *addr) {
571 extract_token(domain, addr, 1, '@', sizeof domain);
574 h = CtdlHostAlias(domain);
576 if ( (h == hostalias_localhost) || (h == hostalias_directory) ) {
586 * Initialize the directory database (erasing anything already there)
588 void CtdlDirectoryInit(void) {
589 cdb_trunc(CDB_DIRECTORY);
594 * Add an Internet e-mail address to the directory for a user
596 void CtdlDirectoryAddUser(char *internet_addr, char *citadel_addr) {
599 lprintf(CTDL_DEBUG, "Dir: %s --> %s\n",
600 internet_addr, citadel_addr);
601 if (IsDirectory(internet_addr) == 0) return;
603 directory_key(key, internet_addr);
605 cdb_store(CDB_DIRECTORY, key, strlen(key),
606 citadel_addr, strlen(citadel_addr)+1 );
611 * Delete an Internet e-mail address from the directory.
613 * (NOTE: we don't actually use or need the citadel_addr variable; it's merely
614 * here because the callback API expects to be able to send it.)
616 void CtdlDirectoryDelUser(char *internet_addr, char *citadel_addr) {
619 directory_key(key, internet_addr);
620 cdb_delete(CDB_DIRECTORY, key, strlen(key) );
625 * Look up an Internet e-mail address in the directory.
626 * On success: returns 0, and Citadel address stored in 'target'
627 * On failure: returns nonzero
629 int CtdlDirectoryLookup(char *target, char *internet_addr, size_t targbuflen) {
630 struct cdbdata *cdbrec;
633 /* Dump it in there unchanged, just for kicks */
634 safestrncpy(target, internet_addr, targbuflen);
636 /* Only do lookups for addresses with hostnames in them */
637 if (num_tokens(internet_addr, '@') != 2) return(-1);
639 /* Only do lookups for domains in the directory */
640 if (IsDirectory(internet_addr) == 0) return(-1);
642 directory_key(key, internet_addr);
643 cdbrec = cdb_fetch(CDB_DIRECTORY, key, strlen(key) );
644 if (cdbrec != NULL) {
645 safestrncpy(target, cdbrec->ptr, targbuflen);
655 * Harvest any email addresses that someone might want to have in their
656 * "collected addresses" book.
658 char *harvest_collected_addresses(struct CtdlMessage *msg) {
661 char user[256], node[256], name[256];
666 if (msg == NULL) return(NULL);
670 if (msg->cm_fields['A'] != NULL) {
671 strcat(addr, msg->cm_fields['A']);
673 if (msg->cm_fields['F'] != NULL) {
675 strcat(addr, msg->cm_fields['F']);
677 if (IsDirectory(msg->cm_fields['F'])) {
682 if (is_harvestable) {
689 if (coll == NULL) return(NULL);
691 /* Scan both the R (To) and Y (CC) fields */
692 for (i = 0; i < 2; ++i) {
693 if (i == 0) field = 'R' ;
694 if (i == 1) field = 'Y' ;
696 if (msg->cm_fields[field] != NULL) {
697 for (j=0; j<num_tokens(msg->cm_fields[field], ','); ++j) {
698 extract_token(addr, msg->cm_fields[field], j, ',', sizeof addr);
699 process_rfc822_addr(addr, user, node, name);
700 h = CtdlHostAlias(node);
701 if ( (h != hostalias_localhost) && (h != hostalias_directory) ) {
702 coll = realloc(coll, strlen(coll) + strlen(addr) + 4);
703 if (coll == NULL) return(NULL);
704 if (strlen(coll) > 0) {
714 if (strlen(coll) == 0) {