2 * This file contains functions which handle the mapping of Internet addresses
3 * to users on the Citadel system.
15 #include <sys/types.h>
20 #include <libcitadel.h>
23 #include "sysdep_decls.h"
24 #include "citserver.h"
28 #include "internet_addressing.h"
31 #include "parsedate.h"
33 #include "ctdl_module.h"
38 /* This is the non-define version in case of s.b. needing to debug */
39 inline void FindNextEnd (char *bptr, char *end)
41 /* Find the next ?Q? */
42 end = strchr(bptr + 2, '?');
43 if (end == NULL) return NULL;
44 if (((*(end + 1) == 'B') || (*(end + 1) == 'Q')) &&
45 (*(end + 2) == '?')) {
46 /* skip on to the end of the cluster, the next ?= */
47 end = strstr(end + 3, "?=");
50 /* sort of half valid encoding, try to find an end. */
51 end = strstr(bptr, "?=");
55 #define FindNextEnd(bptr, end) { \
56 end = strchr(bptr + 2, '?'); \
58 if (((*(end + 1) == 'B') || (*(end + 1) == 'Q')) && (*(end + 2) == '?')) { \
59 end = strstr(end + 3, "?="); \
60 } else end = strstr(bptr, "?="); \
65 * Handle subjects with RFC2047 encoding such as:
66 * =?koi8-r?B?78bP0s3Mxc7JxSDXz9rE1dvO2c3JINvB0sHNySDP?=
68 void utf8ify_rfc822_string(char *buf) {
69 char *start, *end, *next, *nextend, *ptr;
74 iconv_t ic = (iconv_t)(-1) ;
75 char *ibuf; /**< Buffer of characters to be converted */
76 char *obuf; /**< Buffer for converted characters */
77 size_t ibuflen; /**< Length of input buffer */
78 size_t obuflen; /**< Length of output buffer */
79 char *isav; /**< Saved pointer to input buffer */
80 char *osav; /**< Saved pointer to output buffer */
83 int illegal_non_rfc2047_encoding = 0;
85 /* Sometimes, badly formed messages contain strings which were simply
86 * written out directly in some foreign character set instead of
87 * using RFC2047 encoding. This is illegal but we will attempt to
88 * handle it anyway by converting from a user-specified default
89 * charset to UTF-8 if we see any nonprintable characters.
92 for (i=0; i<len; ++i) {
93 if ((buf[i] < 32) || (buf[i] > 126)) {
94 illegal_non_rfc2047_encoding = 1;
95 i = len; ///< take a shortcut, it won't be more than one.
98 if (illegal_non_rfc2047_encoding) {
99 const char *default_header_charset = "iso-8859-1";
100 if ( (strcasecmp(default_header_charset, "UTF-8")) && (strcasecmp(default_header_charset, "us-ascii")) ) {
101 ctdl_iconv_open("UTF-8", default_header_charset, &ic);
102 if (ic != (iconv_t)(-1) ) {
105 safestrncpy(ibuf, buf, 1024);
106 ibuflen = strlen(ibuf);
108 obuf = (char *) malloc(obuflen);
110 iconv(ic, &ibuf, &ibuflen, &obuf, &obuflen);
111 osav[1024-obuflen] = 0;
120 /* pre evaluate the first pair */
121 nextend = end = NULL;
123 start = strstr(buf, "=?");
125 FindNextEnd (start, end);
127 while ((start != NULL) && (end != NULL))
129 next = strstr(end, "=?");
131 FindNextEnd(next, nextend);
135 /* did we find two partitions */
136 if ((next != NULL) &&
140 while ((ptr < next) &&
146 /* did we find a gab just filled with blanks? */
151 len - (next - start));
153 /* now terminate the gab at the end */
154 delta = (next - end) - 2;
158 /* move next to its new location. */
163 /* our next-pair is our new first pair now. */
168 /* Now we handle foreign character sets properly encoded
171 start = strstr(buf, "=?");
172 FindNextEnd((start != NULL)? start : buf, end);
173 while (start != NULL && end != NULL && end > start)
175 extract_token(charset, start, 1, '?', sizeof charset);
176 extract_token(encoding, start, 2, '?', sizeof encoding);
177 extract_token(istr, start, 3, '?', sizeof istr);
181 if (!strcasecmp(encoding, "B")) { /**< base64 */
182 ibuflen = CtdlDecodeBase64(ibuf, istr, strlen(istr));
184 else if (!strcasecmp(encoding, "Q")) { /**< quoted-printable */
192 if (istr[pos] == '_') istr[pos] = ' ';
196 ibuflen = CtdlDecodeQuotedPrintable(ibuf, istr, len);
199 strcpy(ibuf, istr); /**< unknown encoding */
200 ibuflen = strlen(istr);
203 ctdl_iconv_open("UTF-8", charset, &ic);
204 if (ic != (iconv_t)(-1) ) {
206 obuf = (char *) malloc(obuflen);
208 iconv(ic, &ibuf, &ibuflen, &obuf, &obuflen);
209 osav[1024-obuflen] = 0;
214 remove_token(end, 0, '?');
215 remove_token(end, 0, '?');
216 remove_token(end, 0, '?');
217 remove_token(end, 0, '?');
218 strcpy(end, &end[1]);
220 snprintf(newbuf, sizeof newbuf, "%s%s%s", buf, osav, end);
229 remove_token(end, 0, '?');
230 remove_token(end, 0, '?');
231 remove_token(end, 0, '?');
232 remove_token(end, 0, '?');
233 strcpy(end, &end[1]);
235 snprintf(newbuf, sizeof newbuf, "%s(unreadable)%s", buf, end);
242 * Since spammers will go to all sorts of absurd lengths to get their
243 * messages through, there are LOTS of corrupt headers out there.
244 * So, prevent a really badly formed RFC2047 header from throwing
245 * this function into an infinite loop.
248 if (passes > 20) return;
250 start = strstr(buf, "=?");
251 FindNextEnd((start != NULL)? start : buf, end);
256 inline void utf8ify_rfc822_string(char *a){};
267 char *inetcfg = NULL;
268 struct spamstrings_t *spamstrings = NULL;
272 * Return nonzero if the supplied name is an alias for this host.
274 int CtdlHostAlias(char *fqdn) {
278 char host[256], type[256];
281 if (fqdn == NULL) return(hostalias_nomatch);
282 if (IsEmptyStr(fqdn)) return(hostalias_nomatch);
283 if (!strcasecmp(fqdn, "localhost")) return(hostalias_localhost);
284 if (!strcasecmp(fqdn, config.c_fqdn)) return(hostalias_localhost);
285 if (!strcasecmp(fqdn, config.c_nodename)) return(hostalias_localhost);
286 if (inetcfg == NULL) return(hostalias_nomatch);
288 config_lines = num_tokens(inetcfg, '\n');
289 for (i=0; i<config_lines; ++i) {
290 extract_token(buf, inetcfg, i, '\n', sizeof buf);
291 extract_token(host, buf, 0, '|', sizeof host);
292 extract_token(type, buf, 1, '|', sizeof type);
296 /* Process these in a specific order, in case there are multiple matches.
297 * We want directory to override masq, for example.
300 if ( (!strcasecmp(type, "masqdomain")) && (!strcasecmp(fqdn, host))) {
301 found = hostalias_masq;
303 if ( (!strcasecmp(type, "localhost")) && (!strcasecmp(fqdn, host))) {
304 found = hostalias_localhost;
306 if ( (!strcasecmp(type, "directory")) && (!strcasecmp(fqdn, host))) {
307 found = hostalias_directory;
310 if (found) return(found);
313 return(hostalias_nomatch);
319 * Determine whether a given Internet address belongs to the current user
321 int CtdlIsMe(char *addr, int addr_buf_len)
326 recp = validate_recipients(addr, NULL, 0);
327 if (recp == NULL) return(0);
329 if (recp->num_local == 0) {
330 free_recipients(recp);
334 for (i=0; i<recp->num_local; ++i) {
335 extract_token(addr, recp->recp_local, i, '|', addr_buf_len);
336 if (!strcasecmp(addr, CC->user.fullname)) {
337 free_recipients(recp);
342 free_recipients(recp);
347 /* If the last item in a list of recipients was truncated to a partial address,
348 * remove it completely in order to avoid choking libSieve
350 void sanitize_truncated_recipient(char *str)
353 if (num_tokens(str, ',') < 2) return;
355 int len = strlen(str);
356 if (len < 900) return;
357 if (len > 998) str[998] = 0;
359 char *cptr = strrchr(str, ',');
362 char *lptr = strchr(cptr, '<');
363 char *rptr = strchr(cptr, '>');
365 if ( (lptr) && (rptr) && (rptr > lptr) ) return;
375 * This function is self explanatory.
376 * (What can I say, I'm in a weird mood today...)
378 void remove_any_whitespace_to_the_left_or_right_of_at_symbol(char *name)
382 for (i = 0; i < strlen(name); ++i) {
383 if (name[i] == '@') {
384 while (isspace(name[i - 1]) && i > 0) {
385 strcpy(&name[i - 1], &name[i]);
388 while (isspace(name[i + 1])) {
389 strcpy(&name[i + 1], &name[i + 2]);
397 * Aliasing for network mail.
398 * (Error messages have been commented out, because this is a server.)
400 int alias(char *name)
401 { /* process alias and routing info for mail */
402 struct CitContext *CCC = CC;
405 char aaa[SIZ], bbb[SIZ];
406 char *ignetcfg = NULL;
407 char *ignetmap = NULL;
413 char original_name[256];
414 safestrncpy(original_name, name, sizeof original_name);
417 remove_any_whitespace_to_the_left_or_right_of_at_symbol(name);
418 stripallbut(name, '<', '>');
420 fp = fopen(file_mail_aliases, "r");
422 fp = fopen("/dev/null", "r");
429 while (fgets(aaa, sizeof aaa, fp) != NULL) {
430 while (isspace(name[0]))
431 strcpy(name, &name[1]);
432 aaa[strlen(aaa) - 1] = 0;
434 for (a = 0; aaa[a] != '\0'; ++a) {
436 strcpy(bbb, &aaa[a + 1]);
441 if (!strcasecmp(name, aaa))
446 /* Hit the Global Address Book */
447 if (CtdlDirectoryLookup(aaa, name, sizeof aaa) == 0) {
451 if (strcasecmp(original_name, name)) {
452 MSG_syslog(LOG_INFO, "%s is being forwarded to %s\n", original_name, name);
455 /* Change "user @ xxx" to "user" if xxx is an alias for this host */
456 for (a=0; name[a] != '\0'; ++a) {
457 if (name[a] == '@') {
458 if (CtdlHostAlias(&name[a+1]) == hostalias_localhost) {
460 MSG_syslog(LOG_INFO, "Changed to <%s>\n", name);
466 /* determine local or remote type, see citadel.h */
467 at = haschar(name, '@');
468 if (at == 0) return(MES_LOCAL); /* no @'s - local address */
469 if (at > 1) return(MES_ERROR); /* >1 @'s - invalid address */
470 remove_any_whitespace_to_the_left_or_right_of_at_symbol(name);
472 /* figure out the delivery mode */
473 extract_token(node, name, 1, '@', sizeof node);
475 /* If there are one or more dots in the nodename, we assume that it
476 * is an FQDN and will attempt SMTP delivery to the Internet.
478 if (haschar(node, '.') > 0) {
479 return(MES_INTERNET);
482 /* Otherwise we look in the IGnet maps for a valid Citadel node.
483 * Try directly-connected nodes first...
485 ignetcfg = CtdlGetSysConfig(IGNETCFG);
486 for (i=0; i<num_tokens(ignetcfg, '\n'); ++i) {
487 extract_token(buf, ignetcfg, i, '\n', sizeof buf);
488 extract_token(testnode, buf, 0, '|', sizeof testnode);
489 if (!strcasecmp(node, testnode)) {
497 * Then try nodes that are two or more hops away.
499 ignetmap = CtdlGetSysConfig(IGNETMAP);
500 for (i=0; i<num_tokens(ignetmap, '\n'); ++i) {
501 extract_token(buf, ignetmap, i, '\n', sizeof buf);
502 extract_token(testnode, buf, 0, '|', sizeof testnode);
503 if (!strcasecmp(node, testnode)) {
510 /* If we get to this point it's an invalid node name */
517 * Validate recipients, count delivery types and errors, and handle aliasing
518 * FIXME check for dupes!!!!!
520 * Returns 0 if all addresses are ok, ret->num_error = -1 if no addresses
521 * were specified, or the number of addresses found invalid.
523 * Caller needs to free the result using free_recipients()
525 recptypes *validate_recipients(const char *supplied_recipients,
526 const char *RemoteIdentifier,
528 struct CitContext *CCC = CC;
530 char *recipients = NULL;
533 char this_recp_cooked[256];
540 struct ctdluser tempUS;
541 struct ctdlroom tempQR;
542 struct ctdlroom tempQR2;
548 ret = (recptypes *) malloc(sizeof(recptypes));
549 if (ret == NULL) return(NULL);
551 /* Set all strings to null and numeric values to zero */
552 memset(ret, 0, sizeof(recptypes));
554 if (supplied_recipients == NULL) {
555 recipients = strdup("");
558 recipients = strdup(supplied_recipients);
561 /* Allocate some memory. Yes, this allocates 500% more memory than we will
562 * actually need, but it's healthier for the heap than doing lots of tiny
563 * realloc() calls instead.
565 len = strlen(recipients) + 1024;
566 ret->errormsg = malloc(len);
567 ret->recp_local = malloc(len);
568 ret->recp_internet = malloc(len);
569 ret->recp_ignet = malloc(len);
570 ret->recp_room = malloc(len);
571 ret->display_recp = malloc(len);
572 ret->recp_orgroom = malloc(len);
573 org_recp = malloc(len);
575 ret->errormsg[0] = 0;
576 ret->recp_local[0] = 0;
577 ret->recp_internet[0] = 0;
578 ret->recp_ignet[0] = 0;
579 ret->recp_room[0] = 0;
580 ret->recp_orgroom[0] = 0;
581 ret->display_recp[0] = 0;
583 ret->recptypes_magic = RECPTYPES_MAGIC;
585 /* Change all valid separator characters to commas */
586 for (i=0; !IsEmptyStr(&recipients[i]); ++i) {
587 if ((recipients[i] == ';') || (recipients[i] == '|')) {
592 /* Now start extracting recipients... */
594 while (!IsEmptyStr(recipients)) {
595 for (i=0; i<=strlen(recipients); ++i) {
596 if (recipients[i] == '\"') in_quotes = 1 - in_quotes;
597 if ( ( (recipients[i] == ',') && (!in_quotes) ) || (recipients[i] == 0) ) {
598 safestrncpy(this_recp, recipients, i+1);
600 if (recipients[i] == ',') {
601 strcpy(recipients, &recipients[i+1]);
604 strcpy(recipients, "");
611 if (IsEmptyStr(this_recp))
613 MSG_syslog(LOG_DEBUG, "Evaluating recipient #%d: %s\n", num_recps, this_recp);
616 strcpy(org_recp, this_recp);
619 mailtype = alias(this_recp);
621 for (j = 0; !IsEmptyStr(&this_recp[j]); ++j) {
622 if (this_recp[j]=='_') {
623 this_recp_cooked[j] = ' ';
626 this_recp_cooked[j] = this_recp[j];
629 this_recp_cooked[j] = '\0';
634 if (!strcasecmp(this_recp, "sysop")) {
636 strcpy(this_recp, config.c_aideroom);
637 if (!IsEmptyStr(ret->recp_room)) {
638 strcat(ret->recp_room, "|");
640 strcat(ret->recp_room, this_recp);
642 else if ( (!strncasecmp(this_recp, "room_", 5))
643 && (!CtdlGetRoom(&tempQR, &this_recp_cooked[5])) ) {
645 /* Save room so we can restore it later */
649 /* Check permissions to send mail to this room */
650 err = CtdlDoIHavePermissionToPostInThisRoom(
655 0 /* 0 = not a reply */
664 if (!IsEmptyStr(ret->recp_room)) {
665 strcat(ret->recp_room, "|");
667 strcat(ret->recp_room, &this_recp_cooked[5]);
669 if (!IsEmptyStr(ret->recp_orgroom)) {
670 strcat(ret->recp_orgroom, "|");
672 strcat(ret->recp_orgroom, org_recp);
676 /* Restore room in case something needs it */
680 else if (CtdlGetUser(&tempUS, this_recp) == 0) {
682 strcpy(this_recp, tempUS.fullname);
683 if (!IsEmptyStr(ret->recp_local)) {
684 strcat(ret->recp_local, "|");
686 strcat(ret->recp_local, this_recp);
688 else if (CtdlGetUser(&tempUS, this_recp_cooked) == 0) {
690 strcpy(this_recp, tempUS.fullname);
691 if (!IsEmptyStr(ret->recp_local)) {
692 strcat(ret->recp_local, "|");
694 strcat(ret->recp_local, this_recp);
702 /* Yes, you're reading this correctly: if the target
703 * domain points back to the local system or an attached
704 * Citadel directory, the address is invalid. That's
705 * because if the address were valid, we would have
706 * already translated it to a local address by now.
708 if (IsDirectory(this_recp, 0)) {
714 if (!IsEmptyStr(ret->recp_internet)) {
715 strcat(ret->recp_internet, "|");
717 strcat(ret->recp_internet, this_recp);
722 if (!IsEmptyStr(ret->recp_ignet)) {
723 strcat(ret->recp_ignet, "|");
725 strcat(ret->recp_ignet, this_recp);
733 if (IsEmptyStr(errmsg)) {
734 snprintf(append, sizeof append, "Invalid recipient: %s", this_recp);
737 snprintf(append, sizeof append, "%s", errmsg);
739 if ( (strlen(ret->errormsg) + strlen(append) + 3) < SIZ) {
740 if (!IsEmptyStr(ret->errormsg)) {
741 strcat(ret->errormsg, "; ");
743 strcat(ret->errormsg, append);
747 if (IsEmptyStr(ret->display_recp)) {
748 strcpy(append, this_recp);
751 snprintf(append, sizeof append, ", %s", this_recp);
753 if ( (strlen(ret->display_recp)+strlen(append)) < SIZ) {
754 strcat(ret->display_recp, append);
760 if ((ret->num_local + ret->num_internet + ret->num_ignet +
761 ret->num_room + ret->num_error) == 0) {
762 ret->num_error = (-1);
763 strcpy(ret->errormsg, "No recipients specified.");
766 MSGM_syslog(LOG_DEBUG, "validate_recipients()\n");
767 MSG_syslog(LOG_DEBUG, " local: %d <%s>\n", ret->num_local, ret->recp_local);
768 MSG_syslog(LOG_DEBUG, " room: %d <%s>\n", ret->num_room, ret->recp_room);
769 MSG_syslog(LOG_DEBUG, " inet: %d <%s>\n", ret->num_internet, ret->recp_internet);
770 MSG_syslog(LOG_DEBUG, " ignet: %d <%s>\n", ret->num_ignet, ret->recp_ignet);
771 MSG_syslog(LOG_DEBUG, " error: %d <%s>\n", ret->num_error, ret->errormsg);
779 * Destructor for recptypes
781 void free_recipients(recptypes *valid) {
787 if (valid->recptypes_magic != RECPTYPES_MAGIC) {
788 struct CitContext *CCC = CC;
789 MSGM_syslog(LOG_EMERG, "Attempt to call free_recipients() on some other data type!\n");
793 if (valid->errormsg != NULL) free(valid->errormsg);
794 if (valid->recp_local != NULL) free(valid->recp_local);
795 if (valid->recp_internet != NULL) free(valid->recp_internet);
796 if (valid->recp_ignet != NULL) free(valid->recp_ignet);
797 if (valid->recp_room != NULL) free(valid->recp_room);
798 if (valid->recp_orgroom != NULL) free(valid->recp_orgroom);
799 if (valid->display_recp != NULL) free(valid->display_recp);
800 if (valid->bounce_to != NULL) free(valid->bounce_to);
801 if (valid->envelope_from != NULL) free(valid->envelope_from);
802 if (valid->sending_room != NULL) free(valid->sending_room);
807 char *qp_encode_email_addrs(char *source)
809 struct CitContext *CCC = CC;
810 char *user, *node, *name;
811 const char headerStr[] = "=?UTF-8?Q?";
815 int need_to_encode = 0;
821 long nAddrPtrMax = 50;
826 if (source == NULL) return source;
827 if (IsEmptyStr(source)) return source;
828 if (MessageDebugEnabled != 0) cit_backtrace();
829 MSG_syslog(LOG_DEBUG, "qp_encode_email_addrs: [%s]\n", source);
831 AddrPtr = malloc (sizeof (long) * nAddrPtrMax);
832 AddrUtf8 = malloc (sizeof (long) * nAddrPtrMax);
833 memset(AddrUtf8, 0, sizeof (long) * nAddrPtrMax);
836 while (!IsEmptyStr (&source[i])) {
837 if (nColons >= nAddrPtrMax){
840 ptr = (long *) malloc(sizeof (long) * nAddrPtrMax * 2);
841 memcpy (ptr, AddrPtr, sizeof (long) * nAddrPtrMax);
842 free (AddrPtr), AddrPtr = ptr;
844 ptr = (long *) malloc(sizeof (long) * nAddrPtrMax * 2);
845 memset(&ptr[nAddrPtrMax], 0,
846 sizeof (long) * nAddrPtrMax);
848 memcpy (ptr, AddrUtf8, sizeof (long) * nAddrPtrMax);
849 free (AddrUtf8), AddrUtf8 = ptr;
852 if (((unsigned char) source[i] < 32) ||
853 ((unsigned char) source[i] > 126)) {
855 AddrUtf8[nColons] = 1;
857 if (source[i] == '"')
858 InQuotes = !InQuotes;
859 if (!InQuotes && source[i] == ',') {
860 AddrPtr[nColons] = i;
865 if (need_to_encode == 0) {
872 EncodedMaxLen = nColons * (sizeof(headerStr) + 3) + SourceLen * 3;
873 Encoded = (char*) malloc (EncodedMaxLen);
875 for (i = 0; i < nColons; i++)
876 source[AddrPtr[i]++] = '\0';
877 /* TODO: if libidn, this might get larger*/
878 user = malloc(SourceLen + 1);
879 node = malloc(SourceLen + 1);
880 name = malloc(SourceLen + 1);
884 for (i = 0; i < nColons && nPtr != NULL; i++) {
885 nmax = EncodedMaxLen - (nPtr - Encoded);
887 process_rfc822_addr(&source[AddrPtr[i]],
891 /* TODO: libIDN here ! */
892 if (IsEmptyStr(name)) {
893 n = snprintf(nPtr, nmax,
894 (i==0)?"%s@%s" : ",%s@%s",
898 EncodedName = rfc2047encode(name, strlen(name));
899 n = snprintf(nPtr, nmax,
900 (i==0)?"%s <%s@%s>" : ",%s <%s@%s>",
901 EncodedName, user, node);
906 n = snprintf(nPtr, nmax,
908 &source[AddrPtr[i]]);
914 ptr = (char*) malloc(EncodedMaxLen * 2);
915 memcpy(ptr, Encoded, EncodedMaxLen);
916 nnPtr = ptr + (nPtr - Encoded), nPtr = nnPtr;
917 free(Encoded), Encoded = ptr;
919 i--; /* do it once more with properly lengthened buffer */
922 for (i = 0; i < nColons; i++)
923 source[--AddrPtr[i]] = ',';
935 * Return 0 if a given string fuzzy-matches a Citadel user account
937 * FIXME ... this needs to be updated to handle aliases.
939 int fuzzy_match(struct ctdluser *us, char *matchstring) {
943 if ( (!strncasecmp(matchstring, "cit", 3))
944 && (atol(&matchstring[3]) == us->usernum)) {
948 len = strlen(matchstring);
949 for (a=0; !IsEmptyStr(&us->fullname[a]); ++a) {
950 if (!strncasecmp(&us->fullname[a],
960 * Unfold a multi-line field into a single line, removing multi-whitespaces
962 void unfold_rfc822_field(char **field, char **FieldEnd)
965 char *pField = *field;
967 char *pFieldEnd = *FieldEnd;
969 while (isspace(*pField))
971 /* remove leading/trailing whitespace */
974 while (isspace(*pFieldEnd))
977 *FieldEnd = pFieldEnd;
978 /* convert non-space whitespace to spaces, and remove double blanks */
979 for (sField = *field = pField;
983 if ((*sField=='\r') || (*sField=='\n'))
986 while (((*(sField + Offset) == '\r') ||
987 (*(sField + Offset) == '\n') ||
988 (isspace(*(sField + Offset)))) &&
989 (sField + Offset < pFieldEnd))
995 if (*sField=='\"') quote = 1 - quote;
997 if (isspace(*sField))
1003 while ((sField < pFieldEnd) &&
1008 else *pField = *sField;
1010 else *pField = *sField;
1014 *FieldEnd = pField - 1;
1020 * Split an RFC822-style address into userid, host, and full name
1023 void process_rfc822_addr(const char *rfc822, char *user, char *node, char *name)
1028 strcpy(node, config.c_fqdn);
1031 if (rfc822 == NULL) return;
1033 /* extract full name - first, it's From minus <userid> */
1034 strcpy(name, rfc822);
1035 stripout(name, '<', '>');
1037 /* strip anything to the left of a bang */
1038 while ((!IsEmptyStr(name)) && (haschar(name, '!') > 0))
1039 strcpy(name, &name[1]);
1041 /* and anything to the right of a @ or % */
1042 for (a = 0; name[a] != '\0'; ++a) {
1043 if (name[a] == '@') {
1047 if (name[a] == '%') {
1053 /* but if there are parentheses, that changes the rules... */
1054 if ((haschar(rfc822, '(') == 1) && (haschar(rfc822, ')') == 1)) {
1055 strcpy(name, rfc822);
1056 stripallbut(name, '(', ')');
1059 /* but if there are a set of quotes, that supersedes everything */
1060 if (haschar(rfc822, 34) == 2) {
1061 strcpy(name, rfc822);
1062 while ((!IsEmptyStr(name)) && (name[0] != 34)) {
1063 strcpy(&name[0], &name[1]);
1065 strcpy(&name[0], &name[1]);
1066 for (a = 0; name[a] != '\0'; ++a)
1067 if (name[a] == 34) {
1072 /* extract user id */
1073 strcpy(user, rfc822);
1075 /* first get rid of anything in parens */
1076 stripout(user, '(', ')');
1078 /* if there's a set of angle brackets, strip it down to that */
1079 if ((haschar(user, '<') == 1) && (haschar(user, '>') == 1)) {
1080 stripallbut(user, '<', '>');
1083 /* strip anything to the left of a bang */
1084 while ((!IsEmptyStr(user)) && (haschar(user, '!') > 0))
1085 strcpy(user, &user[1]);
1087 /* and anything to the right of a @ or % */
1088 for (a = 0; user[a] != '\0'; ++a) {
1089 if (user[a] == '@') {
1093 if (user[a] == '%') {
1100 /* extract node name */
1101 strcpy(node, rfc822);
1103 /* first get rid of anything in parens */
1104 stripout(node, '(', ')');
1106 /* if there's a set of angle brackets, strip it down to that */
1107 if ((haschar(node, '<') == 1) && (haschar(node, '>') == 1)) {
1108 stripallbut(node, '<', '>');
1111 /* If no node specified, tack ours on instead */
1113 (haschar(node, '@')==0)
1114 && (haschar(node, '%')==0)
1115 && (haschar(node, '!')==0)
1117 strcpy(node, config.c_nodename);
1122 /* strip anything to the left of a @ */
1123 while ((!IsEmptyStr(node)) && (haschar(node, '@') > 0))
1124 strcpy(node, &node[1]);
1126 /* strip anything to the left of a % */
1127 while ((!IsEmptyStr(node)) && (haschar(node, '%') > 0))
1128 strcpy(node, &node[1]);
1130 /* reduce multiple system bang paths to node!user */
1131 while ((!IsEmptyStr(node)) && (haschar(node, '!') > 1))
1132 strcpy(node, &node[1]);
1134 /* now get rid of the user portion of a node!user string */
1135 for (a = 0; node[a] != '\0'; ++a)
1136 if (node[a] == '!') {
1142 /* strip leading and trailing spaces in all strings */
1147 /* If we processed a string that had the address in angle brackets
1148 * but no name outside the brackets, we now have an empty name. In
1149 * this case, use the user portion of the address as the name.
1151 if ((IsEmptyStr(name)) && (!IsEmptyStr(user))) {
1159 * convert_field() is a helper function for convert_internet_message().
1160 * Given start/end positions for an rfc822 field, it converts it to a Citadel
1161 * field if it wants to, and unfolds it if necessary.
1163 * Returns 1 if the field was converted and inserted into the Citadel message
1164 * structure, implying that the source field should be removed from the
1167 int convert_field(struct CtdlMessage *msg, const char *beg, const char *end) {
1168 char *key, *value, *valueend;
1172 const char *colonpos = NULL;
1181 for (pos = end; pos >= beg; pos--) {
1182 if (*pos == ':') colonpos = pos;
1185 if (colonpos == NULL) return(0); /* no colon? not a valid header line */
1188 key = malloc(len + 2);
1189 memcpy(key, beg, len + 1);
1191 valueend = key + len;
1192 * ( key + (colonpos - beg) ) = '\0';
1193 value = &key[(colonpos - beg) + 1];
1194 /* printf("Header: [%s]\nValue: [%s]\n", key, value); */
1195 unfold_rfc822_field(&value, &valueend);
1196 valuelen = valueend - value + 1;
1197 /* printf("UnfoldedValue: [%s]\n", value); */
1200 * Here's the big rfc822-to-citadel loop.
1203 /* Date/time is converted into a unix timestamp. If the conversion
1204 * fails, we replace it with the time the message arrived locally.
1206 if (!strcasecmp(key, "Date")) {
1207 parsed_date = parsedate(value);
1208 if (parsed_date < 0L) parsed_date = time(NULL);
1210 if (CM_IsEmpty(msg, eTimestamp))
1211 CM_SetFieldLONG(msg, eTimestamp, parsed_date);
1215 else if (!strcasecmp(key, "From")) {
1216 process_rfc822_addr(value, user, node, name);
1217 syslog(LOG_DEBUG, "Converted to <%s@%s> (%s)\n", user, node, name);
1218 snprintf(addr, sizeof(addr), "%s@%s", user, node);
1219 if (CM_IsEmpty(msg, eAuthor))
1220 CM_SetField(msg, eAuthor, name, strlen(name));
1221 if (CM_IsEmpty(msg, erFc822Addr))
1222 CM_SetField(msg, erFc822Addr, addr, strlen(addr));
1226 else if (!strcasecmp(key, "Subject")) {
1227 if (CM_IsEmpty(msg, eMsgSubject))
1228 CM_SetField(msg, eMsgSubject, value, valuelen);
1232 else if (!strcasecmp(key, "List-ID")) {
1233 if (CM_IsEmpty(msg, eListID))
1234 CM_SetField(msg, eListID, value, valuelen);
1238 else if (!strcasecmp(key, "To")) {
1239 if (CM_IsEmpty(msg, eRecipient))
1240 CM_SetField(msg, eRecipient, value, valuelen);
1244 else if (!strcasecmp(key, "CC")) {
1245 if (CM_IsEmpty(msg, eCarbonCopY))
1246 CM_SetField(msg, eCarbonCopY, value, valuelen);
1250 else if (!strcasecmp(key, "Message-ID")) {
1251 if (!CM_IsEmpty(msg, emessageId)) {
1252 syslog(LOG_WARNING, "duplicate message id\n");
1259 pValueLen = valuelen;
1260 /* Strip angle brackets */
1261 while (haschar(pValue, '<') > 0) {
1266 for (i = 0; i <= pValueLen; ++i)
1267 if (pValue[i] == '>') {
1272 CM_SetField(msg, emessageId, pValue, pValueLen);
1278 else if (!strcasecmp(key, "Return-Path")) {
1279 if (CM_IsEmpty(msg, eMessagePath))
1280 CM_SetField(msg, eMessagePath, value, valuelen);
1284 else if (!strcasecmp(key, "Envelope-To")) {
1285 if (CM_IsEmpty(msg, eenVelopeTo))
1286 CM_SetField(msg, eenVelopeTo, value, valuelen);
1290 else if (!strcasecmp(key, "References")) {
1291 CM_SetField(msg, eWeferences, value, valuelen);
1295 else if (!strcasecmp(key, "Reply-To")) {
1296 CM_SetField(msg, eReplyTo, value, valuelen);
1300 else if (!strcasecmp(key, "In-reply-to")) {
1301 if (CM_IsEmpty(msg, eWeferences)) /* References: supersedes In-reply-to: */
1302 CM_SetField(msg, eWeferences, value, valuelen);
1308 /* Clean up and move on. */
1309 free(key); /* Don't free 'value', it's actually the same buffer */
1315 * Convert RFC822 references format (References) to Citadel references format (Weferences)
1317 void convert_references_to_wefewences(char *str) {
1318 int bracket_nesting = 0;
1320 char *moveptr = NULL;
1327 if (bracket_nesting < 0) bracket_nesting = 0;
1329 if ((ch == '>') && (bracket_nesting == 0) && (*(ptr+1)) && (ptr>str) ) {
1333 else if (bracket_nesting > 0) {
1339 *moveptr = *(moveptr+1);
1343 if (ch == '<') ++bracket_nesting;
1350 * Convert an RFC822 message (headers + body) to a CtdlMessage structure.
1351 * NOTE: the supplied buffer becomes part of the CtdlMessage structure, and
1352 * will be deallocated when CM_Free() is called. Therefore, the
1353 * supplied buffer should be DEREFERENCED. It should not be freed or used
1356 struct CtdlMessage *convert_internet_message(char *rfc822) {
1357 StrBuf *RFCBuf = NewStrBufPlain(rfc822, -1);
1359 return convert_internet_message_buf(&RFCBuf);
1364 struct CtdlMessage *convert_internet_message_buf(StrBuf **rfc822)
1366 struct CtdlMessage *msg;
1367 const char *pos, *beg, *end, *totalend;
1368 int done, alldone = 0;
1370 StrBuf *OtherHeaders;
1372 msg = malloc(sizeof(struct CtdlMessage));
1373 if (msg == NULL) return msg;
1375 memset(msg, 0, sizeof(struct CtdlMessage));
1376 msg->cm_magic = CTDLMESSAGE_MAGIC; /* self check */
1377 msg->cm_anon_type = 0; /* never anonymous */
1378 msg->cm_format_type = FMT_RFC822; /* internet message */
1380 pos = ChrPtr(*rfc822);
1381 totalend = pos + StrLength(*rfc822);
1383 OtherHeaders = NewStrBufPlain(NULL, StrLength(*rfc822));
1387 /* Locate beginning and end of field, keeping in mind that
1388 * some fields might be multiline
1392 while ((end < totalend) &&
1397 if ( (*pos=='\n') && ((*(pos+1))!=0x20) && ((*(pos+1))!=0x09) )
1402 /* done with headers? */
1404 ( (*(pos+1)=='\n') ||
1410 if (pos >= (totalend - 1) )
1420 /* At this point we have a field. Are we interested in it? */
1421 converted = convert_field(msg, beg, end);
1423 /* Strip the field out of the RFC822 header if we used it */
1425 StrBufAppendBufPlain(OtherHeaders, beg, end - beg, 0);
1426 StrBufAppendBufPlain(OtherHeaders, HKEY("\n"), 0);
1429 /* If we've hit the end of the message, bail out */
1430 if (pos >= totalend)
1433 StrBufAppendBufPlain(OtherHeaders, HKEY("\n"), 0);
1435 StrBufAppendBufPlain(OtherHeaders, pos, totalend - pos, 0);
1437 CM_SetAsFieldSB(msg, eMesageText, &OtherHeaders);
1439 /* Follow-up sanity checks... */
1441 /* If there's no timestamp on this message, set it to now. */
1442 if (CM_IsEmpty(msg, eTimestamp)) {
1443 CM_SetFieldLONG(msg, eTimestamp, time(NULL));
1446 /* If a W (references, or rather, Wefewences) field is present, we
1447 * have to convert it from RFC822 format to Citadel format.
1449 if (!CM_IsEmpty(msg, eWeferences)) {
1451 convert_references_to_wefewences(msg->cm_fields[eWeferences]);
1460 * Look for a particular header field in an RFC822 message text. If the
1461 * requested field is found, it is unfolded (if necessary) and returned to
1462 * the caller. The field name is stripped out, leaving only its contents.
1463 * The caller is responsible for freeing the returned buffer. If the requested
1464 * field is not present, or anything else goes wrong, it returns NULL.
1466 char *rfc822_fetch_field(const char *rfc822, const char *fieldname) {
1467 char *fieldbuf = NULL;
1468 const char *end_of_headers;
1469 const char *field_start;
1474 /* Should never happen, but sometimes we get stupid */
1475 if (rfc822 == NULL) return(NULL);
1476 if (fieldname == NULL) return(NULL);
1478 snprintf(fieldhdr, sizeof fieldhdr, "%s:", fieldname);
1480 /* Locate the end of the headers, so we don't run past that point */
1481 end_of_headers = cbmstrcasestr(rfc822, "\n\r\n");
1482 if (end_of_headers == NULL) {
1483 end_of_headers = cbmstrcasestr(rfc822, "\n\n");
1485 if (end_of_headers == NULL) return (NULL);
1487 field_start = cbmstrcasestr(rfc822, fieldhdr);
1488 if (field_start == NULL) return(NULL);
1489 if (field_start > end_of_headers) return(NULL);
1491 fieldbuf = malloc(SIZ);
1492 strcpy(fieldbuf, "");
1495 ptr = cmemreadline(ptr, fieldbuf, SIZ-strlen(fieldbuf) );
1496 while ( (isspace(ptr[0])) && (ptr < end_of_headers) ) {
1497 strcat(fieldbuf, " ");
1498 cont = &fieldbuf[strlen(fieldbuf)];
1499 ptr = cmemreadline(ptr, cont, SIZ-strlen(fieldbuf) );
1503 strcpy(fieldbuf, &fieldbuf[strlen(fieldhdr)]);
1511 /*****************************************************************************
1512 * DIRECTORY MANAGEMENT FUNCTIONS *
1513 *****************************************************************************/
1516 * Generate the index key for an Internet e-mail address to be looked up
1519 void directory_key(char *key, char *addr) {
1523 for (i=0; !IsEmptyStr(&addr[i]); ++i) {
1524 if (!isspace(addr[i])) {
1525 key[keylen++] = tolower(addr[i]);
1530 syslog(LOG_DEBUG, "Directory key is <%s>\n", key);
1535 /* Return nonzero if the supplied address is in a domain we keep in
1538 int IsDirectory(char *addr, int allow_masq_domains) {
1542 extract_token(domain, addr, 1, '@', sizeof domain);
1545 h = CtdlHostAlias(domain);
1547 if ( (h == hostalias_masq) && allow_masq_domains)
1550 if ( (h == hostalias_localhost) || (h == hostalias_directory) ) {
1560 * Initialize the directory database (erasing anything already there)
1562 void CtdlDirectoryInit(void) {
1563 cdb_trunc(CDB_DIRECTORY);
1568 * Add an Internet e-mail address to the directory for a user
1570 int CtdlDirectoryAddUser(char *internet_addr, char *citadel_addr) {
1573 if (IsDirectory(internet_addr, 0) == 0)
1575 syslog(LOG_DEBUG, "Create directory entry: %s --> %s\n", internet_addr, citadel_addr);
1576 directory_key(key, internet_addr);
1577 cdb_store(CDB_DIRECTORY, key, strlen(key), citadel_addr, strlen(citadel_addr)+1 );
1583 * Delete an Internet e-mail address from the directory.
1585 * (NOTE: we don't actually use or need the citadel_addr variable; it's merely
1586 * here because the callback API expects to be able to send it.)
1588 int CtdlDirectoryDelUser(char *internet_addr, char *citadel_addr) {
1591 syslog(LOG_DEBUG, "Delete directory entry: %s --> %s\n", internet_addr, citadel_addr);
1592 directory_key(key, internet_addr);
1593 return cdb_delete(CDB_DIRECTORY, key, strlen(key) ) == 0;
1598 * Look up an Internet e-mail address in the directory.
1599 * On success: returns 0, and Citadel address stored in 'target'
1600 * On failure: returns nonzero
1602 int CtdlDirectoryLookup(char *target, char *internet_addr, size_t targbuflen) {
1603 struct cdbdata *cdbrec;
1606 /* Dump it in there unchanged, just for kicks */
1607 safestrncpy(target, internet_addr, targbuflen);
1609 /* Only do lookups for addresses with hostnames in them */
1610 if (num_tokens(internet_addr, '@') != 2) return(-1);
1612 /* Only do lookups for domains in the directory */
1613 if (IsDirectory(internet_addr, 0) == 0) return(-1);
1615 directory_key(key, internet_addr);
1616 cdbrec = cdb_fetch(CDB_DIRECTORY, key, strlen(key) );
1617 if (cdbrec != NULL) {
1618 safestrncpy(target, cdbrec->ptr, targbuflen);
1628 * Harvest any email addresses that someone might want to have in their
1629 * "collected addresses" book.
1631 char *harvest_collected_addresses(struct CtdlMessage *msg) {
1634 char user[256], node[256], name[256];
1637 eMsgField field = 0;
1639 if (msg == NULL) return(NULL);
1643 if (!CM_IsEmpty(msg, eAuthor)) {
1644 strcat(addr, msg->cm_fields[eAuthor]);
1646 if (!CM_IsEmpty(msg, erFc822Addr)) {
1648 strcat(addr, msg->cm_fields[erFc822Addr]);
1650 if (IsDirectory(msg->cm_fields[erFc822Addr], 0)) {
1655 if (is_harvestable) {
1656 coll = strdup(addr);
1662 if (coll == NULL) return(NULL);
1664 /* Scan both the R (To) and Y (CC) fields */
1665 for (i = 0; i < 2; ++i) {
1666 if (i == 0) field = eRecipient;
1667 if (i == 1) field = eCarbonCopY;
1669 if (!CM_IsEmpty(msg, field)) {
1670 for (j=0; j<num_tokens(msg->cm_fields[field], ','); ++j) {
1671 extract_token(addr, msg->cm_fields[field], j, ',', sizeof addr);
1672 if (strstr(addr, "=?") != NULL)
1673 utf8ify_rfc822_string(addr);
1674 process_rfc822_addr(addr, user, node, name);
1675 h = CtdlHostAlias(node);
1676 if ( (h != hostalias_localhost) && (h != hostalias_directory) ) {
1677 coll = realloc(coll, strlen(coll) + strlen(addr) + 4);
1678 if (coll == NULL) return(NULL);
1679 if (!IsEmptyStr(coll)) {
1689 if (IsEmptyStr(coll)) {