4 * This file contains functions which handle the mapping of Internet addresses
5 * to users on the Citadel system.
21 #include <sys/types.h>
23 #if TIME_WITH_SYS_TIME
24 # include <sys/time.h>
28 # include <sys/time.h>
39 #include "serv_extensions.h"
40 #include "sysdep_decls.h"
41 #include "citserver.h"
46 #include "internet_addressing.h"
49 #include "parsedate.h"
64 struct spamstrings_t *spamstrings = NULL;
68 * Return nonzero if the supplied name is an alias for this host.
70 int CtdlHostAlias(char *fqdn) {
74 char host[SIZ], type[SIZ];
76 if (fqdn == NULL) return(hostalias_nomatch);
77 if (strlen(fqdn) == 0) return(hostalias_nomatch);
78 if (!strcasecmp(fqdn, config.c_fqdn)) return(hostalias_localhost);
79 if (!strcasecmp(fqdn, config.c_nodename)) return(hostalias_localhost);
80 if (inetcfg == NULL) return(hostalias_nomatch);
82 config_lines = num_tokens(inetcfg, '\n');
83 for (i=0; i<config_lines; ++i) {
84 extract_token(buf, inetcfg, i, '\n');
85 extract_token(host, buf, 0, '|');
86 extract_token(type, buf, 1, '|');
88 if ( (!strcasecmp(type, "localhost"))
89 && (!strcasecmp(fqdn, host)))
90 return(hostalias_localhost);
92 if ( (!strcasecmp(type, "gatewaydomain"))
93 && (!strcasecmp(&fqdn[strlen(fqdn)-strlen(host)], host)))
94 return(hostalias_gatewaydomain);
96 if ( (!strcasecmp(type, "directory"))
97 && (!strcasecmp(&fqdn[strlen(fqdn)-strlen(host)], host)))
98 return(hostalias_directory);
102 return(hostalias_nomatch);
112 * Return 0 if a given string fuzzy-matches a Citadel user account
114 * FIXME ... this needs to be updated to handle aliases.
116 int fuzzy_match(struct usersupp *us, char *matchstring) {
119 if ( (!strncasecmp(matchstring, "cit", 3))
120 && (atol(&matchstring[3]) == us->usernum)) {
125 for (a=0; a<strlen(us->fullname); ++a) {
126 if (!strncasecmp(&us->fullname[a],
127 matchstring, strlen(matchstring))) {
136 * Unfold a multi-line field into a single line, removing multi-whitespaces
138 void unfold_rfc822_field(char *field) {
142 striplt(field); /* remove leading/trailing whitespace */
144 /* convert non-space whitespace to spaces, and remove double blanks */
145 for (i=0; i<strlen(field); ++i) {
146 if (field[i]=='\"') quote = 1 - quote;
148 if (isspace(field[i])) field[i] = ' ';
149 while (isspace(field[i]) && isspace(field[i+1])) {
150 strcpy(&field[i+1], &field[i+2]);
159 * Split an RFC822-style address into userid, host, and full name
162 void process_rfc822_addr(const char *rfc822, char *user, char *node, char *name)
167 strcpy(node, config.c_fqdn);
170 /* extract full name - first, it's From minus <userid> */
171 strcpy(name, rfc822);
172 stripout(name, '<', '>');
174 /* strip anything to the left of a bang */
175 while ((strlen(name) > 0) && (haschar(name, '!') > 0))
176 strcpy(name, &name[1]);
178 /* and anything to the right of a @ or % */
179 for (a = 0; a < strlen(name); ++a) {
186 /* but if there are parentheses, that changes the rules... */
187 if ((haschar(rfc822, '(') == 1) && (haschar(rfc822, ')') == 1)) {
188 strcpy(name, rfc822);
189 stripallbut(name, '(', ')');
192 /* but if there are a set of quotes, that supersedes everything */
193 if (haschar(rfc822, 34) == 2) {
194 strcpy(name, rfc822);
195 while ((strlen(name) > 0) && (name[0] != 34)) {
196 strcpy(&name[0], &name[1]);
198 strcpy(&name[0], &name[1]);
199 for (a = 0; a < strlen(name); ++a)
203 /* extract user id */
204 strcpy(user, rfc822);
206 /* first get rid of anything in parens */
207 stripout(user, '(', ')');
209 /* if there's a set of angle brackets, strip it down to that */
210 if ((haschar(user, '<') == 1) && (haschar(user, '>') == 1)) {
211 stripallbut(user, '<', '>');
214 /* strip anything to the left of a bang */
215 while ((strlen(user) > 0) && (haschar(user, '!') > 0))
216 strcpy(user, &user[1]);
218 /* and anything to the right of a @ or % */
219 for (a = 0; a < strlen(user); ++a) {
227 /* extract node name */
228 strcpy(node, rfc822);
230 /* first get rid of anything in parens */
231 stripout(node, '(', ')');
233 /* if there's a set of angle brackets, strip it down to that */
234 if ((haschar(node, '<') == 1) && (haschar(node, '>') == 1)) {
235 stripallbut(node, '<', '>');
238 /* If no node specified, tack ours on instead */
240 (haschar(node, '@')==0)
241 && (haschar(node, '%')==0)
242 && (haschar(node, '!')==0)
244 strcpy(node, config.c_nodename);
249 /* strip anything to the left of a @ */
250 while ((strlen(node) > 0) && (haschar(node, '@') > 0))
251 strcpy(node, &node[1]);
253 /* strip anything to the left of a % */
254 while ((strlen(node) > 0) && (haschar(node, '%') > 0))
255 strcpy(node, &node[1]);
257 /* reduce multiple system bang paths to node!user */
258 while ((strlen(node) > 0) && (haschar(node, '!') > 1))
259 strcpy(node, &node[1]);
261 /* now get rid of the user portion of a node!user string */
262 for (a = 0; a < strlen(node); ++a)
267 /* strip leading and trailing spaces in all strings */
276 * convert_field() is a helper function for convert_internet_message().
277 * Given start/end positions for an rfc822 field, it converts it to a Citadel
278 * field if it wants to, and unfolds it if necessary.
280 * Returns 1 if the field was converted and inserted into the Citadel message
281 * structure, implying that the source field should be removed from the
284 int convert_field(struct CtdlMessage *msg, int beg, int end) {
297 rfc822 = msg->cm_fields['M']; /* M field contains rfc822 text */
298 for (i = end; i >= beg; --i) {
299 if (rfc822[i] == ':') colonpos = i;
302 if (colonpos < 0) return(0); /* no colon? not a valid header line */
304 key = mallok((end - beg) + 2);
305 safestrncpy(key, &rfc822[beg], (end-beg)+1);
306 key[colonpos - beg] = 0;
307 value = &key[(colonpos - beg) + 1];
308 unfold_rfc822_field(value);
311 * Here's the big rfc822-to-citadel loop.
314 /* Date/time is converted into a unix timestamp. If the conversion
315 * fails, we replace it with the time the message arrived locally.
317 if (!strcasecmp(key, "Date")) {
318 parsed_date = parsedate(value);
319 if (parsed_date < 0L) parsed_date = time(NULL);
320 snprintf(buf, sizeof buf, "%ld", (long)parsed_date );
321 if (msg->cm_fields['T'] == NULL)
322 msg->cm_fields['T'] = strdoop(buf);
326 else if (!strcasecmp(key, "From")) {
327 process_rfc822_addr(value, user, node, name);
328 lprintf(9, "Converted to <%s@%s> (%s)\n", user, node, name);
329 snprintf(addr, sizeof addr, "%s@%s", user, node);
330 if (msg->cm_fields['A'] == NULL)
331 msg->cm_fields['A'] = strdoop(name);
333 if (msg->cm_fields['F'] == NULL)
334 msg->cm_fields['F'] = strdoop(addr);
338 else if (!strcasecmp(key, "Subject")) {
339 if (msg->cm_fields['U'] == NULL)
340 msg->cm_fields['U'] = strdoop(value);
344 else if (!strcasecmp(key, "Message-ID")) {
345 if (msg->cm_fields['I'] != NULL) {
346 lprintf(5, "duplicate message id\n");
349 if (msg->cm_fields['I'] == NULL) {
350 msg->cm_fields['I'] = strdoop(value);
352 /* Strip angle brackets */
353 while (haschar(msg->cm_fields['I'], '<') > 0) {
354 strcpy(&msg->cm_fields['I'][0],
355 &msg->cm_fields['I'][1]);
357 for (i = 0; i<strlen(msg->cm_fields['I']); ++i)
358 if (msg->cm_fields['I'][i] == '>')
359 msg->cm_fields['I'][i] = 0;
365 /* Clean up and move on. */
366 phree(key); /* Don't free 'value', it's actually the same buffer */
372 * Convert an RFC822 message (headers + body) to a CtdlMessage structure.
373 * NOTE: the supplied buffer becomes part of the CtdlMessage structure, and
374 * will be deallocated when CtdlFreeMessage() is called. Therefore, the
375 * supplied buffer should be DEREFERENCED. It should not be freed or used
378 struct CtdlMessage *convert_internet_message(char *rfc822) {
380 struct CtdlMessage *msg;
381 int pos, beg, end, msglen;
386 msg = mallok(sizeof(struct CtdlMessage));
387 if (msg == NULL) return msg;
389 memset(msg, 0, sizeof(struct CtdlMessage));
390 msg->cm_magic = CTDLMESSAGE_MAGIC; /* self check */
391 msg->cm_anon_type = 0; /* never anonymous */
392 msg->cm_format_type = FMT_RFC822; /* internet message */
393 msg->cm_fields['M'] = rfc822;
395 lprintf(9, "Unconverted RFC822 message length = %ld\n", (long)strlen(rfc822));
401 /* Locate beginning and end of field, keeping in mind that
402 * some fields might be multiline
407 msglen = strlen(rfc822);
408 while ( (end < 0) && (done == 0) ) {
410 if ((rfc822[pos]=='\n')
411 && (!isspace(rfc822[pos+1]))) {
415 /* done with headers? (commented out; see below)
416 if ( ((rfc822[pos]=='\n')
417 ||(rfc822[pos]=='\r') )
418 && ( (rfc822[pos+1]=='\n')
419 ||(rfc822[pos+1]=='\r')) ) {
425 /* done with headers? (try this way instead) */
426 if ( (rfc822[pos]=='\n')
427 && ( (rfc822[pos+1]=='\n')
428 ||(rfc822[pos+1]=='\r')) ) {
433 if (pos >= (msglen-1) ) {
442 /* At this point we have a field. Are we interested in it? */
443 converted = convert_field(msg, beg, end);
445 /* Strip the field out of the RFC822 header if we used it */
447 strcpy(&rfc822[beg], &rfc822[pos]);
451 /* If we've hit the end of the message, bail out */
452 if (pos > strlen(rfc822)) done = 1;
455 /* Follow-up sanity checks... */
457 /* If there's no timestamp on this message, set it to now. */
458 if (msg->cm_fields['T'] == NULL) {
459 snprintf(buf, sizeof buf, "%ld", (long)time(NULL));
460 msg->cm_fields['T'] = strdoop(buf);
463 lprintf(9, "RFC822 length remaining after conversion = %ld\n",
464 (long)strlen(rfc822));
471 * Look for a particular header field in an RFC822 message text. If the
472 * requested field is found, it is unfolded (if necessary) and returned to
473 * the caller. The field name is stripped out, leaving only its contents.
474 * The caller is responsible for freeing the returned buffer. If the requested
475 * field is not present, or anything else goes wrong, it returns NULL.
477 char *rfc822_fetch_field(char *rfc822, char *fieldname) {
482 char *fieldbuf = NULL;
484 /* Should never happen, but sometimes we get stupid */
485 if (rfc822 == NULL) return(NULL);
486 if (fieldname == NULL) return(NULL);
490 /* Locate beginning and end of field, keeping in mind that
491 * some fields might be multiline
495 for (pos=beg; ((pos<=strlen(rfc822))&&(end<0)); ++pos) {
496 if ((rfc822[pos]=='\n')
497 && (!isspace(rfc822[pos+1]))) {
500 if ( (rfc822[pos]=='\n') /* done w. headers? */
501 && ( (rfc822[pos+1]=='\n')
502 ||(rfc822[pos+1]=='\r'))) {
509 /* At this point we have a field. Is it The One? */
511 fieldbuf = mallok((end-beg)+3);
512 if (fieldbuf == NULL) return(NULL);
513 safestrncpy(fieldbuf, &rfc822[beg], (end-beg)+1);
514 unfold_rfc822_field(fieldbuf);
516 for (i = strlen(fieldbuf); i >= 0; --i) {
517 if (fieldbuf[i] == ':') colonpos = i;
520 fieldbuf[colonpos] = 0;
521 if (!strcasecmp(fieldbuf, fieldname)) {
522 strcpy(fieldbuf, &fieldbuf[colonpos+1]);
530 /* If we've hit the end of the message, bail out */
531 if (pos > strlen(rfc822)) done = 1;
538 /*****************************************************************************
539 * DIRECTORY MANAGEMENT FUNCTIONS *
540 *****************************************************************************/
543 * Generate the index key for an Internet e-mail address to be looked up
546 void directory_key(char *key, char *addr) {
550 for (i=0; i<strlen(addr); ++i) {
551 if (!isspace(addr[i])) {
552 key[keylen++] = tolower(addr[i]);
557 lprintf(9, "Directory key is <%s>\n", key);
562 /* Return nonzero if the supplied address is in a domain we keep in
565 int IsDirectory(char *addr) {
569 extract_token(domain, addr, 1, '@');
572 h = CtdlHostAlias(domain);
573 lprintf(9, "IsDirectory(%s)\n", domain);
575 if ( (h == hostalias_localhost) || (h == hostalias_directory) ) {
576 lprintf(9, " ...yes\n");
580 lprintf(9, " ...no\n");
587 * Initialize the directory database (erasing anything already there)
589 void CtdlDirectoryInit(void) {
590 cdb_trunc(CDB_DIRECTORY);
595 * Add an Internet e-mail address to the directory for a user
597 void CtdlDirectoryAddUser(char *internet_addr, char *citadel_addr) {
600 lprintf(9, "Dir: %s --> %s\n",
601 internet_addr, citadel_addr);
602 if (IsDirectory(internet_addr) == 0) return;
604 directory_key(key, internet_addr);
606 cdb_store(CDB_DIRECTORY, key, strlen(key),
607 citadel_addr, strlen(citadel_addr)+1 );
612 * Delete an Internet e-mail address from the directory.
614 * (NOTE: we don't actually use or need the citadel_addr variable; it's merely
615 * here because the callback API expects to be able to send it.)
617 void CtdlDirectoryDelUser(char *internet_addr, char *citadel_addr) {
620 directory_key(key, internet_addr);
621 cdb_delete(CDB_DIRECTORY, key, strlen(key) );
626 * Look up an Internet e-mail address in the directory.
627 * On success: returns 0, and Citadel address stored in 'target'
628 * On failure: returns nonzero
630 int CtdlDirectoryLookup(char *target, char *internet_addr) {
631 struct cdbdata *cdbrec;
634 /* Dump it in there unchanged, just for kicks */
635 strcpy(target, internet_addr);
637 /* Only do lookups for addresses with hostnames in them */
638 if (num_tokens(internet_addr, '@') != 2) return(-1);
640 /* Only do lookups for domains in the directory */
641 if (IsDirectory(internet_addr) == 0) return(-1);
643 directory_key(key, internet_addr);
644 cdbrec = cdb_fetch(CDB_DIRECTORY, key, strlen(key) );
645 if (cdbrec != NULL) {
646 safestrncpy(target, cdbrec->ptr, SIZ);