1 // This file contains functions which handle the mapping of Internet addresses
2 // to users on the Citadel system.
4 // Copyright (c) 1987-2024 by the citadel.org team
6 // This program is open source software. Use, duplication, or disclosure
7 // is subject to the terms of the GNU General Public License, version 3.
18 #include <sys/types.h>
23 #include <libcitadel.h>
24 #include "citadel_defs.h"
26 #include "sysdep_decls.h"
27 #include "citserver.h"
31 #include "internet_addressing.h"
34 #include "parsedate.h"
36 #include "ctdl_module.h"
41 // Return nonzero if the supplied name is an alias for this host.
42 int CtdlHostAlias(char *fqdn) {
46 char host[256], type[256];
49 if (fqdn == NULL) return(hostalias_nomatch);
50 if (IsEmptyStr(fqdn)) return(hostalias_nomatch);
51 if (!strcasecmp(fqdn, "localhost")) return(hostalias_localhost);
52 if (!strcasecmp(fqdn, CtdlGetConfigStr("c_fqdn"))) return(hostalias_localhost);
53 if (!strcasecmp(fqdn, CtdlGetConfigStr("c_nodename"))) return(hostalias_localhost);
54 if (inetcfg == NULL) return(hostalias_nomatch);
56 config_lines = num_tokens(inetcfg, '\n');
57 for (i=0; i<config_lines; ++i) {
58 extract_token(buf, inetcfg, i, '\n', sizeof buf);
59 extract_token(host, buf, 0, '|', sizeof host);
60 extract_token(type, buf, 1, '|', sizeof type);
64 // Process these in a specific order, in case there are multiple matches.
65 // We want localhost to override masq, for example.
67 if ( (!strcasecmp(type, "masqdomain")) && (!strcasecmp(fqdn, host))) {
68 found = hostalias_masq;
71 if ( (!strcasecmp(type, "localhost")) && (!strcasecmp(fqdn, host))) {
72 found = hostalias_localhost;
75 // "directory" used to be a distributed version of "localhost" but they're both the same now
76 if ( (!strcasecmp(type, "directory")) && (!strcasecmp(fqdn, host))) {
77 found = hostalias_localhost;
80 if (found) return(found);
82 return(hostalias_nomatch);
86 // Determine whether a given Internet address belongs to the current user
87 int CtdlIsMe(char *addr, int addr_buf_len) {
88 struct recptypes *recp;
91 recp = validate_recipients(addr, NULL, 0);
92 if (recp == NULL) return(0);
94 if (recp->num_local == 0) {
95 free_recipients(recp);
99 for (i=0; i<recp->num_local; ++i) {
100 extract_token(addr, recp->recp_local, i, '|', addr_buf_len);
101 if (!strcasecmp(addr, CC->user.fullname)) {
102 free_recipients(recp);
107 free_recipients(recp);
112 // If the last item in a list of recipients was truncated to a partial address,
113 // remove it completely in order to avoid choking library functions.
114 void sanitize_truncated_recipient(char *str) {
116 if (num_tokens(str, ',') < 2) return;
118 int len = strlen(str);
119 if (len < 900) return;
120 if (len > 998) str[998] = 0;
122 char *cptr = strrchr(str, ',');
125 char *lptr = strchr(cptr, '<');
126 char *rptr = strchr(cptr, '>');
128 if ( (lptr) && (rptr) && (rptr > lptr) ) return;
134 // This function is self explanatory.
135 // (What can I say, I'm in a weird mood today...)
136 void remove_any_whitespace_to_the_left_or_right_of_at_symbol(char *name) {
140 for (ptr=name; *ptr; ++ptr) {
141 while ( (isspace(*ptr)) && (*(ptr+1)=='@') ) {
143 if (ptr > name) --ptr;
145 while ( (*ptr=='@') && (*(ptr+1)!=0) && (isspace(*(ptr+1))) ) {
146 strcpy(ptr+1, ptr+2);
152 // values that can be returned by expand_aliases()
154 EA_ERROR, // Can't send message due to bad address
155 EA_MULTIPLE, // Alias expanded into multiple recipients -- run me again!
156 EA_LOCAL, // Local message, do no network processing
157 EA_INTERNET, // Convert msg and send as Internet mail
158 EA_SKIP // This recipient has been invalidated -- skip it!
162 // Process alias and routing info for email addresses
163 int expand_aliases(char *name, char *aliases) {
169 int num_aliases = num_tokens(aliases, '\n');
170 for (a=0; a<num_aliases; ++a) {
171 extract_token(aaa, aliases, a, '\n', sizeof aaa);
172 char *bar = strchr(aaa, '|');
178 if ( (!IsEmptyStr(aaa)) && (!strcasecmp(name, aaa)) ) {
179 syslog(LOG_DEBUG, "internet_addressing: global alias <%s> to <%s>", name, bar);
184 if (strchr(name, ',')) {
189 char original_name[256]; // Now go for the regular aliases
190 safestrncpy(original_name, name, sizeof original_name);
192 // should these checks still be here, or maybe move them to split_recps() ?
194 remove_any_whitespace_to_the_left_or_right_of_at_symbol(name);
195 stripallbut(name, '<', '>');
197 // Hit the email address directory
198 if (CtdlDirectoryLookup(aaa, name, sizeof aaa) == 0) {
202 if (strcasecmp(original_name, name)) {
203 syslog(LOG_INFO, "internet_addressing: directory alias <%s> to <%s>", original_name, name);
206 // Change "user @ xxx" to "user" if xxx is an alias for this host
207 for (a=0; name[a] != '\0'; ++a) {
208 if (name[a] == '@') {
209 if (CtdlHostAlias(&name[a+1]) == hostalias_localhost) {
211 syslog(LOG_DEBUG, "internet_addressing: host is local, recipient is <%s>", name);
217 // Is this a local or remote recipient?
218 at = haschar(name, '@');
220 return(EA_LOCAL); // no @'s = local address
223 return(EA_INTERNET); // one @ = internet address
226 return(EA_ERROR); // more than one @ = badly formed address
231 // Return a supplied list of email addresses as an array, removing superfluous information and syntax.
232 // If an existing Array is supplied as "append_to" it will do so; otherwise a new Array is allocated.
233 Array *split_recps(char *addresses, Array *append_to) {
235 if (IsEmptyStr(addresses)) { // nothing supplied, nothing returned
239 // Copy the supplied address list into our own memory space, because we are going to modify it.
240 char *a = strdup(addresses);
242 syslog(LOG_ERR, "internet_addressing: malloc() failed: %m");
246 // Strip out anything in double quotes
251 r = strrchr(a, '\"');
257 // Transform all qualifying delimiters to commas
259 for (t=a; t[0]; ++t) {
260 if ((t[0]==';') || (t[0]=='|')) {
265 // Tokenize the recipients into an array. No single recipient should be larger than 256 bytes.
266 Array *recipients_array = NULL;
268 recipients_array = append_to; // Append to an existing array of recipients
271 recipients_array = array_new(256); // This is a new array of recipients
274 int num_addresses = num_tokens(a, ',');
276 for (i=0; i<num_addresses; ++i) {
277 char this_address[256];
278 extract_token(this_address, a, i, ',', sizeof this_address);
279 string_trim(this_address); // strip leading and trailing whitespace
280 stripout(this_address, '(', ')'); // remove any portion in parentheses
281 stripallbut(this_address, '<', '>'); // if angle brackets are present, keep only what is inside them
282 if (!IsEmptyStr(this_address)) {
283 array_append(recipients_array, this_address);
287 free(a); // We don't need this buffer anymore.
288 return(recipients_array); // Return the completed array to the caller.
292 // Validate recipients, count delivery types and errors, and handle aliasing
294 // Returns 0 if all addresses are ok, ret->num_error = -1 if no addresses
295 // were specified, or the number of addresses found invalid.
297 // Caller needs to free the result using free_recipients()
299 struct recptypes *validate_recipients(char *supplied_recipients, const char *RemoteIdentifier, int Flags) {
300 struct recptypes *ret;
301 char *recipients = NULL;
306 struct ctdluser tempUS;
307 struct ctdlroom original_room;
313 ret = (struct recptypes *) malloc(sizeof(struct recptypes)); // Initialize
314 if (ret == NULL) return(NULL);
315 memset(ret, 0, sizeof(struct recptypes)); // set all values to null/zero
317 if (supplied_recipients == NULL) {
318 recipients = strdup("");
321 recipients = strdup(supplied_recipients);
324 len = strlen(recipients) + 1024; // allocate memory
325 ret->errormsg = malloc(len);
326 ret->recp_local = malloc(len);
327 ret->recp_internet = malloc(len);
328 ret->recp_room = malloc(len);
329 ret->display_recp = malloc(len);
330 ret->recp_orgroom = malloc(len);
332 ret->errormsg[0] = 0;
333 ret->recp_local[0] = 0;
334 ret->recp_internet[0] = 0;
335 ret->recp_room[0] = 0;
336 ret->recp_orgroom[0] = 0;
337 ret->display_recp[0] = 0;
338 ret->recptypes_magic = RECPTYPES_MAGIC;
340 Array *recp_array = split_recps(supplied_recipients, NULL);
342 char *aliases = CtdlGetSysConfig(GLOBAL_ALIASES); // First hit the Global Alias Table
345 for (r=0; (recp_array && r<array_len(recp_array)); ++r) {
346 org_recp = (char *)array_get_element_at(recp_array, r);
347 strncpy(this_recp, org_recp, sizeof this_recp);
350 for (i=0; i<3; ++i) { // pass three times through the aliaser
351 mailtype = expand_aliases(this_recp, aliases);
353 // If an alias expanded to multiple recipients, strip off those recipients and append them
354 // to the end of the array. This loop will hit those again when it gets there.
355 if (mailtype == EA_MULTIPLE) {
356 recp_array = split_recps(this_recp, recp_array);
360 // This loop searches for duplicate recipients in the final list and marks them to be skipped.
362 for (j=0; j<r; ++j) {
363 if (!strcasecmp(this_recp, (char *)array_get_element_at(recp_array, j) )) {
368 syslog(LOG_DEBUG, "Recipient #%d of type %d is <%s>", r, mailtype, this_recp);
372 case EA_LOCAL: // There are several types of "local" recipients.
374 // Old BBS conventions require mail to "sysop" to go somewhere. Send it to the admin room.
375 if (!strcasecmp(this_recp, "sysop")) {
377 strcpy(this_recp, CtdlGetConfigStr("c_aideroom"));
378 if (!IsEmptyStr(ret->recp_room)) {
379 strcat(ret->recp_room, "|");
381 strcat(ret->recp_room, this_recp);
384 // This handles rooms which can receive posts via email.
385 else if (!strncasecmp(this_recp, "room_", 5)) {
386 original_room = CC->room; // Remember where we parked
388 char mail_to_room[ROOMNAMELEN];
390 strncpy(mail_to_room, &this_recp[5], sizeof mail_to_room);
391 for (m = mail_to_room; *m; ++m) {
392 if (m[0] == '_') m[0]=' ';
394 if (!CtdlGetRoom(&CC->room, mail_to_room)) { // Find the room they asked for
396 err = CtdlDoIHavePermissionToPostInThisRoom( // check for write permissions to room
400 0 // 0 means "this is not a reply"
408 if (!IsEmptyStr(ret->recp_room)) {
409 strcat(ret->recp_room, "|");
411 strcat(ret->recp_room, CC->room.QRname);
413 if (!IsEmptyStr(ret->recp_orgroom)) {
414 strcat(ret->recp_orgroom, "|");
416 strcat(ret->recp_orgroom, this_recp);
420 else { // no such room exists
425 // Restore this session's original room location.
426 CC->room = original_room;
430 // This handles the most common case, which is mail to a user's inbox.
431 // (the next line depends on left-to-right evaluation)
432 else if ((CtdlGetUser(&tempUS, this_recp) == 0) && (tempUS.usernum > 0)) {
434 strcpy(this_recp, tempUS.fullname);
435 if (!IsEmptyStr(ret->recp_local)) {
436 strcat(ret->recp_local, "|");
438 strcat(ret->recp_local, this_recp);
441 // No match for this recipient
448 // Yes, you're reading this correctly: if the target domain points back to the local system,
449 // the address is invalid. That's because if the address were valid, we would have
450 // already translated it to a local address by now.
451 if (IsDirectory(this_recp, 0)) {
457 if (!IsEmptyStr(ret->recp_internet)) {
458 strcat(ret->recp_internet, "|");
460 strcat(ret->recp_internet, this_recp);
465 // no action required, anything in this slot has already been processed elsewhere
473 if (IsEmptyStr(errmsg)) {
474 snprintf(append, sizeof append, "Invalid recipient: %s", this_recp);
477 snprintf(append, sizeof append, "%s", errmsg);
479 if ( (strlen(ret->errormsg) + strlen(append) + 3) < SIZ) {
480 if (!IsEmptyStr(ret->errormsg)) {
481 strcat(ret->errormsg, "; ");
483 strcat(ret->errormsg, append);
487 if (IsEmptyStr(ret->display_recp)) {
488 strcpy(append, this_recp);
491 snprintf(append, sizeof append, ", %s", this_recp);
493 if ( (strlen(ret->display_recp)+strlen(append)) < SIZ) {
494 strcat(ret->display_recp, append);
499 if (aliases != NULL) { // ok, we're done with the global alias list now
503 if ( (ret->num_local + ret->num_internet + ret->num_room + ret->num_error) == 0) {
504 ret->num_error = (-1);
505 strcpy(ret->errormsg, "No recipients specified.");
508 syslog(LOG_DEBUG, "internet_addressing: validate_recipients() = %d local, %d room, %d SMTP, %d error",
509 ret->num_local, ret->num_room, ret->num_internet, ret->num_error
514 array_free(recp_array);
521 // Destructor for recptypes
522 void free_recipients(struct recptypes *valid) {
528 if (valid->recptypes_magic != RECPTYPES_MAGIC) {
529 syslog(LOG_ERR, "internet_addressing: attempt to call free_recipients() on some other data type!");
530 exit(CTDLEXIT_BAD_MAGIC);
533 if (valid->errormsg != NULL) free(valid->errormsg);
534 if (valid->recp_local != NULL) free(valid->recp_local);
535 if (valid->recp_internet != NULL) free(valid->recp_internet);
536 if (valid->recp_room != NULL) free(valid->recp_room);
537 if (valid->recp_orgroom != NULL) free(valid->recp_orgroom);
538 if (valid->display_recp != NULL) free(valid->display_recp);
539 if (valid->bounce_to != NULL) free(valid->bounce_to);
540 if (valid->envelope_from != NULL) free(valid->envelope_from);
541 if (valid->sending_room != NULL) free(valid->sending_room);
546 char *qp_encode_email_addrs(char *source) {
547 char *user, *node, *name;
548 const char headerStr[] = "=?UTF-8?Q?";
552 int need_to_encode = 0;
558 long nAddrPtrMax = 50;
563 if (source == NULL) return source;
564 if (IsEmptyStr(source)) return source;
565 syslog(LOG_DEBUG, "internet_addressing: qp_encode_email_addrs <%s>", source);
567 AddrPtr = malloc (sizeof (long) * nAddrPtrMax);
568 AddrUtf8 = malloc (sizeof (long) * nAddrPtrMax);
569 memset(AddrUtf8, 0, sizeof (long) * nAddrPtrMax);
572 while (!IsEmptyStr (&source[i])) {
573 if (nColons >= nAddrPtrMax){
576 ptr = (long *) malloc(sizeof (long) * nAddrPtrMax * 2);
577 memcpy (ptr, AddrPtr, sizeof (long) * nAddrPtrMax);
578 free (AddrPtr), AddrPtr = ptr;
580 ptr = (long *) malloc(sizeof (long) * nAddrPtrMax * 2);
581 memset(&ptr[nAddrPtrMax], 0, sizeof (long) * nAddrPtrMax);
583 memcpy (ptr, AddrUtf8, sizeof (long) * nAddrPtrMax);
584 free (AddrUtf8), AddrUtf8 = ptr;
587 if (((unsigned char) source[i] < 32) || ((unsigned char) source[i] > 126)) {
589 AddrUtf8[nColons] = 1;
591 if (source[i] == '"') {
592 InQuotes = !InQuotes;
594 if (!InQuotes && source[i] == ',') {
595 AddrPtr[nColons] = i;
600 if (need_to_encode == 0) {
607 EncodedMaxLen = nColons * (sizeof(headerStr) + 3) + SourceLen * 3;
608 Encoded = (char*) malloc (EncodedMaxLen);
610 for (i = 0; i < nColons; i++) {
611 source[AddrPtr[i]++] = '\0';
613 // TODO: if libidn, this might get larger
614 user = malloc(SourceLen + 1);
615 node = malloc(SourceLen + 1);
616 name = malloc(SourceLen + 1);
620 for (i = 0; i < nColons && nPtr != NULL; i++) {
621 nmax = EncodedMaxLen - (nPtr - Encoded);
623 process_rfc822_addr(&source[AddrPtr[i]], user, node, name);
624 // TODO: libIDN here !
625 if (IsEmptyStr(name)) {
626 n = snprintf(nPtr, nmax, (i==0)?"%s@%s" : ",%s@%s", user, node);
629 EncodedName = rfc2047encode(name, strlen(name));
630 n = snprintf(nPtr, nmax, (i==0)?"%s <%s@%s>" : ",%s <%s@%s>", EncodedName, user, node);
635 n = snprintf(nPtr, nmax, (i==0)?"%s" : ",%s", &source[AddrPtr[i]]);
641 ptr = (char*) malloc(EncodedMaxLen * 2);
642 memcpy(ptr, Encoded, EncodedMaxLen);
643 nnPtr = ptr + (nPtr - Encoded), nPtr = nnPtr;
644 free(Encoded), Encoded = ptr;
646 i--; // do it once more with properly lengthened buffer
649 for (i = 0; i < nColons; i++)
650 source[--AddrPtr[i]] = ',';
661 // Unfold a multi-line field into a single line, removing multi-whitespaces
662 void unfold_rfc822_field(char **field, char **FieldEnd)
665 char *pField = *field;
667 char *pFieldEnd = *FieldEnd;
669 while (isspace(*pField))
671 // remove leading/trailing whitespace
674 while (isspace(*pFieldEnd))
677 *FieldEnd = pFieldEnd;
678 // convert non-space whitespace to spaces, and remove double blanks
679 for (sField = *field = pField;
683 if ((*sField=='\r') || (*sField=='\n'))
686 while ( ( (*(sField + offset) == '\r') || (*(sField + offset) == '\n' )) && (sField + offset < pFieldEnd) ) {
693 if (*sField=='\"') quote = 1 - quote;
695 if (isspace(*sField)) {
700 while ((sField < pFieldEnd) &&
705 else *pField = *sField;
707 else *pField = *sField;
711 *FieldEnd = pField - 1;
715 // Split an RFC822-style address into userid, host, and full name
716 void process_rfc822_addr(const char *rfc822, char *user, char *node, char *name) {
720 strcpy(node, CtdlGetConfigStr("c_fqdn"));
723 if (rfc822 == NULL) return;
725 // extract full name - first, it's From minus <userid>
726 strcpy(name, rfc822);
727 stripout(name, '<', '>');
729 // and anything to the right of a @
730 for (a = 0; name[a] != '\0'; ++a) {
731 if (name[a] == '@') {
737 // but if there are parentheses, that changes the rules...
738 if ((haschar(rfc822, '(') == 1) && (haschar(rfc822, ')') == 1)) {
739 strcpy(name, rfc822);
740 stripallbut(name, '(', ')');
743 // but if there are a set of quotes, that supersedes everything
744 if (haschar(rfc822, 34) == 2) {
745 strcpy(name, rfc822);
746 while ((!IsEmptyStr(name)) && (name[0] != 34)) {
747 strcpy(&name[0], &name[1]);
749 strcpy(&name[0], &name[1]);
750 for (a = 0; name[a] != '\0'; ++a)
757 strcpy(user, rfc822);
759 // first get rid of anything in parens
760 stripout(user, '(', ')');
762 // if there's a set of angle brackets, strip it down to that
763 if ((haschar(user, '<') == 1) && (haschar(user, '>') == 1)) {
764 stripallbut(user, '<', '>');
767 // and anything to the right of a @
768 for (a = 0; user[a] != '\0'; ++a) {
769 if (user[a] == '@') {
776 strcpy(node, rfc822);
778 // first get rid of anything in parens
779 stripout(node, '(', ')');
781 // if there's a set of angle brackets, strip it down to that
782 if ((haschar(node, '<') == 1) && (haschar(node, '>') == 1)) {
783 stripallbut(node, '<', '>');
786 // If no node specified, tack ours on instead
787 if (haschar(node, '@') == 0) {
788 strcpy(node, CtdlGetConfigStr("c_nodename"));
791 // strip anything to the left of a @
792 while ((!IsEmptyStr(node)) && (haschar(node, '@') > 0)) {
793 strcpy(node, &node[1]);
797 // strip leading and trailing spaces in all strings
802 // If we processed a string that had the address in angle brackets
803 // but no name outside the brackets, we now have an empty name. In
804 // this case, use the user portion of the address as the name.
805 if ((IsEmptyStr(name)) && (!IsEmptyStr(user))) {
811 // convert_field() is a helper function for convert_internet_message().
812 // Given start/end positions for an rfc822 field, it converts it to a Citadel
813 // field if it wants to, and unfolds it if necessary.
815 // Returns 1 if the field was converted and inserted into the Citadel message
816 // structure, implying that the source field should be removed from the
818 int convert_field(struct CtdlMessage *msg, const char *beg, const char *end) {
819 char *key, *value, *valueend;
823 const char *colonpos = NULL;
832 for (pos = end; pos >= beg; pos--) {
833 if (*pos == ':') colonpos = pos;
836 if (colonpos == NULL) return(0); // no colon? not a valid header line
839 key = malloc(len + 2);
840 memcpy(key, beg, len + 1);
842 valueend = key + len;
843 * ( key + (colonpos - beg) ) = '\0';
844 value = &key[(colonpos - beg) + 1];
845 // printf("Header: [%s]\nValue: [%s]\n", key, value);
846 unfold_rfc822_field(&value, &valueend);
847 valuelen = valueend - value + 1;
848 // printf("UnfoldedValue: [%s]\n", value);
850 // Here's the big rfc822-to-citadel loop.
852 // Date/time is converted into a unix timestamp. If the conversion
853 // fails, we replace it with the time the message arrived locally.
854 if (!strcasecmp(key, "Date")) {
855 parsed_date = parsedate(value);
856 if (parsed_date < 0L) parsed_date = time(NULL);
858 if (CM_IsEmpty(msg, eTimestamp))
859 CM_SetFieldLONG(msg, eTimestamp, parsed_date);
863 else if (!strcasecmp(key, "From")) {
864 process_rfc822_addr(value, user, node, name);
865 syslog(LOG_DEBUG, "internet_addressing: converted to <%s@%s> (%s)", user, node, name);
866 snprintf(addr, sizeof(addr), "%s@%s", user, node);
867 if (CM_IsEmpty(msg, eAuthor) && !IsEmptyStr(name)) {
868 CM_SetField(msg, eAuthor, name);
870 if (CM_IsEmpty(msg, erFc822Addr) && !IsEmptyStr(addr)) {
871 CM_SetField(msg, erFc822Addr, addr);
876 else if (!strcasecmp(key, "Subject")) {
877 if (CM_IsEmpty(msg, eMsgSubject))
878 CM_SetField(msg, eMsgSubject, value);
882 else if (!strcasecmp(key, "List-ID")) {
883 if (CM_IsEmpty(msg, eListID))
884 CM_SetField(msg, eListID, value);
888 else if (!strcasecmp(key, "To")) {
889 if (CM_IsEmpty(msg, eRecipient))
890 CM_SetField(msg, eRecipient, value);
894 else if (!strcasecmp(key, "CC")) {
895 if (CM_IsEmpty(msg, eCarbonCopY))
896 CM_SetField(msg, eCarbonCopY, value);
900 else if (!strcasecmp(key, "Message-ID")) {
901 if (!CM_IsEmpty(msg, emessageId)) {
902 syslog(LOG_WARNING, "internet_addressing: duplicate message id");
909 pValueLen = valuelen;
910 // Strip angle brackets
911 while (haschar(pValue, '<') > 0) {
916 for (i = 0; i <= pValueLen; ++i)
917 if (pValue[i] == '>') {
922 CM_SetField(msg, emessageId, pValue);
928 else if (!strcasecmp(key, "Return-Path")) {
929 if (CM_IsEmpty(msg, eMessagePath))
930 CM_SetField(msg, eMessagePath, value);
934 else if (!strcasecmp(key, "Envelope-To")) {
935 if (CM_IsEmpty(msg, eenVelopeTo))
936 CM_SetField(msg, eenVelopeTo, value);
940 else if (!strcasecmp(key, "References")) {
941 CM_SetField(msg, eWeferences, value);
945 else if (!strcasecmp(key, "Reply-To")) {
946 CM_SetField(msg, eReplyTo, value);
950 else if (!strcasecmp(key, "In-reply-to")) {
951 if (CM_IsEmpty(msg, eWeferences)) // References: supersedes In-reply-to:
952 CM_SetField(msg, eWeferences, value);
958 // Clean up and move on.
959 free(key); // Don't free 'value', it's actually the same buffer
964 // Convert RFC822 references format (References) to Citadel references format (Weferences)
965 void convert_references_to_wefewences(char *str) {
966 int bracket_nesting = 0;
968 char *moveptr = NULL;
975 if (bracket_nesting < 0) bracket_nesting = 0;
977 if ((ch == '>') && (bracket_nesting == 0) && (*(ptr+1)) && (ptr>str) ) {
981 else if (bracket_nesting > 0) {
987 *moveptr = *(moveptr+1);
991 if (ch == '<') ++bracket_nesting;
997 // Convert an RFC822 message (headers + body) to a CtdlMessage structure.
998 // NOTE: the supplied buffer becomes part of the CtdlMessage structure, and
999 // will be deallocated when CM_Free() is called. Therefore, the
1000 // supplied buffer should be DEREFERENCED. It should not be freed or used
1002 struct CtdlMessage *convert_internet_message(char *rfc822) {
1003 StrBuf *RFCBuf = NewStrBufPlain(rfc822, -1);
1005 return convert_internet_message_buf(&RFCBuf);
1009 struct CtdlMessage *convert_internet_message_buf(StrBuf **rfc822) {
1010 struct CtdlMessage *msg;
1011 const char *pos, *beg, *end, *totalend;
1012 int done, alldone = 0;
1014 StrBuf *OtherHeaders;
1016 msg = malloc(sizeof(struct CtdlMessage));
1017 if (msg == NULL) return msg;
1019 memset(msg, 0, sizeof(struct CtdlMessage));
1020 msg->cm_magic = CTDLMESSAGE_MAGIC; // self check
1021 msg->cm_anon_type = 0; // never anonymous
1022 msg->cm_format_type = FMT_RFC822; // internet message
1024 pos = ChrPtr(*rfc822);
1025 totalend = pos + StrLength(*rfc822);
1027 OtherHeaders = NewStrBufPlain(NULL, StrLength(*rfc822));
1031 // Locate beginning and end of field, keeping in mind that
1032 // some fields might be multiline
1035 while ((end < totalend) && (end == beg) && (done == 0) ) {
1037 if ( (*pos=='\n') && ((*(pos+1))!=0x20) && ((*(pos+1))!=0x09) ) {
1041 // done with headers?
1042 if ((*pos=='\n') && ( (*(pos+1)=='\n') || (*(pos+1)=='\r')) ) {
1046 if (pos >= (totalend - 1) ) {
1055 // At this point we have a field. Are we interested in it?
1056 converted = convert_field(msg, beg, end);
1058 // Strip the field out of the RFC822 header if we used it
1060 StrBufAppendBufPlain(OtherHeaders, beg, end - beg, 0);
1061 StrBufAppendBufPlain(OtherHeaders, HKEY("\n"), 0);
1064 // If we've hit the end of the message, bail out
1065 if (pos >= totalend)
1068 StrBufAppendBufPlain(OtherHeaders, HKEY("\n"), 0);
1070 StrBufAppendBufPlain(OtherHeaders, pos, totalend - pos, 0);
1072 CM_SetAsFieldSB(msg, eMessageText, &OtherHeaders);
1074 // Follow-up sanity checks...
1076 // If there's no timestamp on this message, set it to now.
1077 if (CM_IsEmpty(msg, eTimestamp)) {
1078 CM_SetFieldLONG(msg, eTimestamp, time(NULL));
1081 // If a W (references, or rather, Wefewences) field is present, we
1082 // have to convert it from RFC822 format to Citadel format.
1083 if (!CM_IsEmpty(msg, eWeferences)) {
1085 convert_references_to_wefewences(msg->cm_fields[eWeferences]);
1092 // Look for a particular header field in an RFC822 message text. If the
1093 // requested field is found, it is unfolded (if necessary) and returned to
1094 // the caller. The field name is stripped out, leaving only its contents.
1095 // The caller is responsible for freeing the returned buffer. If the requested
1096 // field is not present, or anything else goes wrong, it returns NULL.
1097 char *rfc822_fetch_field(const char *rfc822, const char *fieldname) {
1098 char *fieldbuf = NULL;
1099 const char *end_of_headers;
1100 const char *field_start;
1105 // Should never happen, but sometimes we get stupid
1106 if (rfc822 == NULL) return(NULL);
1107 if (fieldname == NULL) return(NULL);
1109 snprintf(fieldhdr, sizeof fieldhdr, "%s:", fieldname);
1111 // Locate the end of the headers, so we don't run past that point
1112 end_of_headers = cbmstrcasestr(rfc822, "\n\r\n");
1113 if (end_of_headers == NULL) {
1114 end_of_headers = cbmstrcasestr(rfc822, "\n\n");
1116 if (end_of_headers == NULL) return (NULL);
1118 field_start = cbmstrcasestr(rfc822, fieldhdr);
1119 if (field_start == NULL) return(NULL);
1120 if (field_start > end_of_headers) return(NULL);
1122 fieldbuf = malloc(SIZ);
1123 strcpy(fieldbuf, "");
1126 ptr = cmemreadline(ptr, fieldbuf, SIZ-strlen(fieldbuf) );
1127 while ( (isspace(ptr[0])) && (ptr < end_of_headers) ) {
1128 strcat(fieldbuf, " ");
1129 cont = &fieldbuf[strlen(fieldbuf)];
1130 ptr = cmemreadline(ptr, cont, SIZ-strlen(fieldbuf) );
1134 strcpy(fieldbuf, &fieldbuf[strlen(fieldhdr)]);
1135 string_trim(fieldbuf);
1141 // *****************************************************************************
1142 // DIRECTORY MANAGEMENT FUNCTIONS
1143 // *****************************************************************************
1145 // Generate the index key for an Internet e-mail address to be looked up
1147 void directory_key(char *key, char *addr) {
1151 for (i=0; !IsEmptyStr(&addr[i]); ++i) {
1152 if (!isspace(addr[i])) {
1153 key[keylen++] = tolower(addr[i]);
1157 syslog(LOG_DEBUG, "internet_addressing: directory key is <%s>", key);
1161 // Return nonzero if the supplied address is in one of "our" domains
1162 int IsDirectory(char *addr, int allow_masq_domains) {
1166 extract_token(domain, addr, 1, '@', sizeof domain);
1167 string_trim(domain);
1169 h = CtdlHostAlias(domain);
1171 if ( (h == hostalias_masq) && allow_masq_domains)
1174 if (h == hostalias_localhost) {
1183 // Add an Internet e-mail address to the directory for a user
1184 int CtdlDirectoryAddUser(char *internet_addr, char *citadel_addr) {
1187 if (IsDirectory(internet_addr, 0) == 0) {
1190 syslog(LOG_DEBUG, "internet_addressing: create directory entry: %s --> %s", internet_addr, citadel_addr);
1191 directory_key(key, internet_addr);
1192 cdb_store(CDB_DIRECTORY, key, strlen(key), citadel_addr, strlen(citadel_addr)+1 );
1197 // Delete an Internet e-mail address from the directory.
1199 // (NOTE: we don't actually use or need the citadel_addr variable; it's merely
1200 // here because the callback API expects to be able to send it.)
1201 int CtdlDirectoryDelUser(char *internet_addr, char *citadel_addr) {
1204 syslog(LOG_DEBUG, "internet_addressing: delete directory entry: %s --> %s", internet_addr, citadel_addr);
1205 directory_key(key, internet_addr);
1206 return cdb_delete(CDB_DIRECTORY, key, strlen(key) ) == 0;
1210 // Look up an Internet e-mail address in the directory.
1211 // On success: returns 0, and Citadel address stored in 'target'
1212 // On failure: returns nonzero
1213 int CtdlDirectoryLookup(char *target, char *internet_addr, size_t targbuflen) {
1214 struct cdbdata cdbrec;
1217 // Dump it in there unchanged, just for kicks
1218 if (target != NULL) {
1219 safestrncpy(target, internet_addr, targbuflen);
1222 // Only do lookups for addresses with hostnames in them
1223 if (num_tokens(internet_addr, '@') != 2) return(-1);
1225 // Only do lookups for domains in the directory
1226 if (IsDirectory(internet_addr, 0) == 0) return(-1);
1228 directory_key(key, internet_addr);
1229 cdbrec = cdb_fetch(CDB_DIRECTORY, key, strlen(key));
1230 if (cdbrec.ptr != NULL) {
1231 if (target != NULL) {
1232 safestrncpy(target, cdbrec.ptr, targbuflen);
1241 // Harvest any email addresses that someone might want to have in their
1242 // "collected addresses" book.
1243 char *harvest_collected_addresses(struct CtdlMessage *msg) {
1246 char user[256], node[256], name[256];
1249 eMsgField field = 0;
1251 if (msg == NULL) return(NULL);
1255 if (!CM_IsEmpty(msg, eAuthor)) {
1256 strcat(addr, msg->cm_fields[eAuthor]);
1258 if (!CM_IsEmpty(msg, erFc822Addr)) {
1260 strcat(addr, msg->cm_fields[erFc822Addr]);
1262 if (IsDirectory(msg->cm_fields[erFc822Addr], 0)) {
1267 if (is_harvestable) {
1268 coll = strdup(addr);
1274 if (coll == NULL) return(NULL);
1276 // Scan both the R (To) and Y (CC) fields
1277 for (i = 0; i < 2; ++i) {
1278 if (i == 0) field = eRecipient;
1279 if (i == 1) field = eCarbonCopY;
1281 if (!CM_IsEmpty(msg, field)) {
1282 for (j=0; j<num_tokens(msg->cm_fields[field], ','); ++j) {
1283 extract_token(addr, msg->cm_fields[field], j, ',', sizeof addr);
1284 if (strstr(addr, "=?") != NULL) {
1285 utf8ify_rfc822_string(addr);
1287 process_rfc822_addr(addr, user, node, name);
1288 h = CtdlHostAlias(node);
1289 if (h != hostalias_localhost) {
1290 coll = realloc(coll, strlen(coll) + strlen(addr) + 4);
1291 if (coll == NULL) return(NULL);
1292 if (!IsEmptyStr(coll)) {
1302 if (IsEmptyStr(coll)) {
1310 // Helper function for CtdlRebuildDirectoryIndex()
1311 void CtdlRebuildDirectoryIndex_backend(char *username, void *data) {
1314 struct ctdluser usbuf;
1316 if (CtdlGetUser(&usbuf, username) != 0) {
1320 if ( (!IsEmptyStr(usbuf.fullname)) && (!IsEmptyStr(usbuf.emailaddrs)) ) {
1321 for (j=0; j<num_tokens(usbuf.emailaddrs, '|'); ++j) {
1322 char one_email[512];
1323 extract_token(one_email, usbuf.emailaddrs, j, '|', sizeof one_email);
1324 CtdlDirectoryAddUser(one_email, usbuf.fullname);
1330 // Initialize the directory database (erasing anything already there)
1331 void CtdlRebuildDirectoryIndex(void) {
1332 syslog(LOG_INFO, "internet_addressing: rebuilding email address directory index");
1333 cdb_trunc(CDB_DIRECTORY);
1334 ForEachUser(CtdlRebuildDirectoryIndex_backend, NULL);
1338 // Configure Internet email addresses for a user account, updating the Directory Index in the process
1339 void CtdlSetEmailAddressesForUser(char *requested_user, char *new_emailaddrs) {
1340 struct ctdluser usbuf;
1344 if (CtdlGetUserLock(&usbuf, requested_user) != 0) { // We can lock because the DirectoryIndex functions don't lock.
1345 return; // Silently fail here if the specified user does not exist.
1348 syslog(LOG_DEBUG, "internet_addressing: setting email addresses for <%s> to <%s>", usbuf.fullname, new_emailaddrs);
1350 // Delete all of the existing directory index records for the user (easier this way)
1351 for (i=0; i<num_tokens(usbuf.emailaddrs, '|'); ++i) {
1352 extract_token(buf, usbuf.emailaddrs, i, '|', sizeof buf);
1353 CtdlDirectoryDelUser(buf, requested_user);
1356 strcpy(usbuf.emailaddrs, new_emailaddrs); // make it official.
1358 // Index all of the new email addresses (they've already been sanitized)
1359 for (i=0; i<num_tokens(usbuf.emailaddrs, '|'); ++i) {
1360 extract_token(buf, usbuf.emailaddrs, i, '|', sizeof buf);
1361 CtdlDirectoryAddUser(buf, requested_user);
1364 CtdlPutUserLock(&usbuf);
1368 // Auto-generate an Internet email address for a user account
1369 void AutoGenerateEmailAddressForUser(struct ctdluser *user) {
1370 char synthetic_email_addr[1024];
1374 for (i=0; u==0; ++i) {
1376 // first try just converting the user name to lowercase and replacing spaces with underscores
1377 snprintf(synthetic_email_addr, sizeof synthetic_email_addr, "%s@%s", user->fullname, CtdlGetConfigStr("c_fqdn"));
1378 for (j=0; ((synthetic_email_addr[j] != '\0')&&(synthetic_email_addr[j] != '@')); j++) {
1379 synthetic_email_addr[j] = tolower(synthetic_email_addr[j]);
1380 if (!isalnum(synthetic_email_addr[j])) {
1381 synthetic_email_addr[j] = '_';
1386 // then try 'ctdl' followed by the user number
1387 snprintf(synthetic_email_addr, sizeof synthetic_email_addr, "ctdl%08lx@%s", user->usernum, CtdlGetConfigStr("c_fqdn"));
1390 // oof. just keep trying other numbers until we find one
1391 snprintf(synthetic_email_addr, sizeof synthetic_email_addr, "ctdl%08x@%s", i, CtdlGetConfigStr("c_fqdn"));
1393 u = CtdlDirectoryLookup(NULL, synthetic_email_addr, 0);
1394 syslog(LOG_DEBUG, "user_ops: address <%s> lookup returned <%d>", synthetic_email_addr, u);
1397 CtdlSetEmailAddressesForUser(user->fullname, synthetic_email_addr);
1398 strncpy(CC->user.emailaddrs, synthetic_email_addr, sizeof(user->emailaddrs));
1399 syslog(LOG_DEBUG, "user_ops: auto-generated email address <%s> for <%s>", synthetic_email_addr, user->fullname);
1403 // Determine whether the supplied email address is subscribed to the supplied room's mailing list service.
1404 int is_email_subscribed_to_list(char *email, char *room_name) {
1405 struct ctdlroom room;
1407 char *roomnetconfig;
1410 if (CtdlGetRoom(&room, room_name)) {
1411 return(0); // room not found, so definitely not subscribed
1414 // If this room has the QR2_SMTP_PUBLIC flag set, anyone may email a post to this room, even non-subscribers.
1415 if (room.QRflags2 & QR2_SMTP_PUBLIC) {
1419 roomnum = room.QRnumber;
1420 roomnetconfig = LoadRoomNetConfigFile(roomnum);
1421 if (roomnetconfig == NULL) {
1425 // We're going to do a very sloppy match here and simply search for the specified email address
1426 // anywhere in the room's netconfig. If you don't like this, fix it yourself.
1427 if (bmstrcasestr(roomnetconfig, email)) {
1434 free(roomnetconfig);