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, "localhost")) return(hostalias_localhost);
79 if (!strcasecmp(fqdn, config.c_fqdn)) return(hostalias_localhost);
80 if (!strcasecmp(fqdn, config.c_nodename)) return(hostalias_localhost);
81 if (inetcfg == NULL) return(hostalias_nomatch);
83 config_lines = num_tokens(inetcfg, '\n');
84 for (i=0; i<config_lines; ++i) {
85 extract_token(buf, inetcfg, i, '\n');
86 extract_token(host, buf, 0, '|');
87 extract_token(type, buf, 1, '|');
89 if ( (!strcasecmp(type, "localhost"))
90 && (!strcasecmp(fqdn, host)))
91 return(hostalias_localhost);
93 if ( (!strcasecmp(type, "gatewaydomain"))
94 && (!strcasecmp(&fqdn[strlen(fqdn)-strlen(host)], host)))
95 return(hostalias_gatewaydomain);
97 if ( (!strcasecmp(type, "directory"))
98 && (!strcasecmp(&fqdn[strlen(fqdn)-strlen(host)], host)))
99 return(hostalias_directory);
103 return(hostalias_nomatch);
113 * Return 0 if a given string fuzzy-matches a Citadel user account
115 * FIXME ... this needs to be updated to handle aliases.
117 int fuzzy_match(struct ctdluser *us, char *matchstring) {
120 if ( (!strncasecmp(matchstring, "cit", 3))
121 && (atol(&matchstring[3]) == us->usernum)) {
126 for (a=0; a<strlen(us->fullname); ++a) {
127 if (!strncasecmp(&us->fullname[a],
128 matchstring, strlen(matchstring))) {
137 * Unfold a multi-line field into a single line, removing multi-whitespaces
139 void unfold_rfc822_field(char *field) {
143 striplt(field); /* remove leading/trailing whitespace */
145 /* convert non-space whitespace to spaces, and remove double blanks */
146 for (i=0; i<strlen(field); ++i) {
147 if (field[i]=='\"') quote = 1 - quote;
149 if (isspace(field[i])) field[i] = ' ';
150 while (isspace(field[i]) && isspace(field[i+1])) {
151 strcpy(&field[i+1], &field[i+2]);
160 * Split an RFC822-style address into userid, host, and full name
163 void process_rfc822_addr(const char *rfc822, char *user, char *node, char *name)
168 strcpy(node, config.c_fqdn);
171 /* extract full name - first, it's From minus <userid> */
172 strcpy(name, rfc822);
173 stripout(name, '<', '>');
175 /* strip anything to the left of a bang */
176 while ((strlen(name) > 0) && (haschar(name, '!') > 0))
177 strcpy(name, &name[1]);
179 /* and anything to the right of a @ or % */
180 for (a = 0; a < strlen(name); ++a) {
187 /* but if there are parentheses, that changes the rules... */
188 if ((haschar(rfc822, '(') == 1) && (haschar(rfc822, ')') == 1)) {
189 strcpy(name, rfc822);
190 stripallbut(name, '(', ')');
193 /* but if there are a set of quotes, that supersedes everything */
194 if (haschar(rfc822, 34) == 2) {
195 strcpy(name, rfc822);
196 while ((strlen(name) > 0) && (name[0] != 34)) {
197 strcpy(&name[0], &name[1]);
199 strcpy(&name[0], &name[1]);
200 for (a = 0; a < strlen(name); ++a)
204 /* extract user id */
205 strcpy(user, rfc822);
207 /* first get rid of anything in parens */
208 stripout(user, '(', ')');
210 /* if there's a set of angle brackets, strip it down to that */
211 if ((haschar(user, '<') == 1) && (haschar(user, '>') == 1)) {
212 stripallbut(user, '<', '>');
215 /* strip anything to the left of a bang */
216 while ((strlen(user) > 0) && (haschar(user, '!') > 0))
217 strcpy(user, &user[1]);
219 /* and anything to the right of a @ or % */
220 for (a = 0; a < strlen(user); ++a) {
228 /* extract node name */
229 strcpy(node, rfc822);
231 /* first get rid of anything in parens */
232 stripout(node, '(', ')');
234 /* if there's a set of angle brackets, strip it down to that */
235 if ((haschar(node, '<') == 1) && (haschar(node, '>') == 1)) {
236 stripallbut(node, '<', '>');
239 /* If no node specified, tack ours on instead */
241 (haschar(node, '@')==0)
242 && (haschar(node, '%')==0)
243 && (haschar(node, '!')==0)
245 strcpy(node, config.c_nodename);
250 /* strip anything to the left of a @ */
251 while ((strlen(node) > 0) && (haschar(node, '@') > 0))
252 strcpy(node, &node[1]);
254 /* strip anything to the left of a % */
255 while ((strlen(node) > 0) && (haschar(node, '%') > 0))
256 strcpy(node, &node[1]);
258 /* reduce multiple system bang paths to node!user */
259 while ((strlen(node) > 0) && (haschar(node, '!') > 1))
260 strcpy(node, &node[1]);
262 /* now get rid of the user portion of a node!user string */
263 for (a = 0; a < strlen(node); ++a)
268 /* strip leading and trailing spaces in all strings */
273 /* If we processed a string that had the address in angle brackets
274 * but no name outside the brackets, we now have an empty name. In
275 * this case, use the user portion of the address as the name.
277 if ((strlen(name) == 0) && (strlen(user) > 0)) {
285 * convert_field() is a helper function for convert_internet_message().
286 * Given start/end positions for an rfc822 field, it converts it to a Citadel
287 * field if it wants to, and unfolds it if necessary.
289 * Returns 1 if the field was converted and inserted into the Citadel message
290 * structure, implying that the source field should be removed from the
293 int convert_field(struct CtdlMessage *msg, int beg, int end) {
306 rfc822 = msg->cm_fields['M']; /* M field contains rfc822 text */
307 for (i = end; i >= beg; --i) {
308 if (rfc822[i] == ':') colonpos = i;
311 if (colonpos < 0) return(0); /* no colon? not a valid header line */
313 key = malloc((end - beg) + 2);
314 safestrncpy(key, &rfc822[beg], (end-beg)+1);
315 key[colonpos - beg] = 0;
316 value = &key[(colonpos - beg) + 1];
317 unfold_rfc822_field(value);
320 * Here's the big rfc822-to-citadel loop.
323 /* Date/time is converted into a unix timestamp. If the conversion
324 * fails, we replace it with the time the message arrived locally.
326 if (!strcasecmp(key, "Date")) {
327 parsed_date = parsedate(value);
328 if (parsed_date < 0L) parsed_date = time(NULL);
329 snprintf(buf, sizeof buf, "%ld", (long)parsed_date );
330 if (msg->cm_fields['T'] == NULL)
331 msg->cm_fields['T'] = strdup(buf);
335 else if (!strcasecmp(key, "From")) {
336 process_rfc822_addr(value, user, node, name);
337 lprintf(CTDL_DEBUG, "Converted to <%s@%s> (%s)\n", user, node, name);
338 snprintf(addr, sizeof addr, "%s@%s", user, node);
339 if (msg->cm_fields['A'] == NULL)
340 msg->cm_fields['A'] = strdup(name);
342 if (msg->cm_fields['F'] == NULL)
343 msg->cm_fields['F'] = strdup(addr);
347 else if (!strcasecmp(key, "Subject")) {
348 if (msg->cm_fields['U'] == NULL)
349 msg->cm_fields['U'] = strdup(value);
353 else if (!strcasecmp(key, "To")) {
354 if (msg->cm_fields['R'] == NULL)
355 msg->cm_fields['R'] = strdup(value);
359 else if (!strcasecmp(key, "Message-ID")) {
360 if (msg->cm_fields['I'] != NULL) {
361 lprintf(CTDL_WARNING, "duplicate message id\n");
364 if (msg->cm_fields['I'] == NULL) {
365 msg->cm_fields['I'] = strdup(value);
367 /* Strip angle brackets */
368 while (haschar(msg->cm_fields['I'], '<') > 0) {
369 strcpy(&msg->cm_fields['I'][0],
370 &msg->cm_fields['I'][1]);
372 for (i = 0; i<strlen(msg->cm_fields['I']); ++i)
373 if (msg->cm_fields['I'][i] == '>')
374 msg->cm_fields['I'][i] = 0;
380 /* Clean up and move on. */
381 free(key); /* Don't free 'value', it's actually the same buffer */
387 * Convert an RFC822 message (headers + body) to a CtdlMessage structure.
388 * NOTE: the supplied buffer becomes part of the CtdlMessage structure, and
389 * will be deallocated when CtdlFreeMessage() is called. Therefore, the
390 * supplied buffer should be DEREFERENCED. It should not be freed or used
393 struct CtdlMessage *convert_internet_message(char *rfc822) {
395 struct CtdlMessage *msg;
396 int pos, beg, end, msglen;
401 msg = malloc(sizeof(struct CtdlMessage));
402 if (msg == NULL) return msg;
404 memset(msg, 0, sizeof(struct CtdlMessage));
405 msg->cm_magic = CTDLMESSAGE_MAGIC; /* self check */
406 msg->cm_anon_type = 0; /* never anonymous */
407 msg->cm_format_type = FMT_RFC822; /* internet message */
408 msg->cm_fields['M'] = rfc822;
410 lprintf(CTDL_DEBUG, "Unconverted RFC822 message length = %ld\n", (long)strlen(rfc822));
416 /* Locate beginning and end of field, keeping in mind that
417 * some fields might be multiline
422 msglen = strlen(rfc822);
423 while ( (end < 0) && (done == 0) ) {
425 if ((rfc822[pos]=='\n')
426 && (!isspace(rfc822[pos+1]))) {
430 /* done with headers? */
431 if ( (rfc822[pos]=='\n')
432 && ( (rfc822[pos+1]=='\n')
433 ||(rfc822[pos+1]=='\r')) ) {
438 if (pos >= (msglen-1) ) {
447 /* At this point we have a field. Are we interested in it? */
448 converted = convert_field(msg, beg, end);
450 /* Strip the field out of the RFC822 header if we used it */
452 strcpy(&rfc822[beg], &rfc822[pos]);
456 /* If we've hit the end of the message, bail out */
457 if (pos > strlen(rfc822)) done = 1;
460 /* Follow-up sanity checks... */
462 /* If there's no timestamp on this message, set it to now. */
463 if (msg->cm_fields['T'] == NULL) {
464 snprintf(buf, sizeof buf, "%ld", (long)time(NULL));
465 msg->cm_fields['T'] = strdup(buf);
468 lprintf(CTDL_DEBUG, "RFC822 length remaining after conversion = %ld\n",
469 (long)strlen(rfc822));
476 * Look for a particular header field in an RFC822 message text. If the
477 * requested field is found, it is unfolded (if necessary) and returned to
478 * the caller. The field name is stripped out, leaving only its contents.
479 * The caller is responsible for freeing the returned buffer. If the requested
480 * field is not present, or anything else goes wrong, it returns NULL.
482 char *rfc822_fetch_field(char *rfc822, char *fieldname) {
483 char *fieldbuf = NULL;
484 char *end_of_headers;
490 /* Should never happen, but sometimes we get stupid */
491 if (rfc822 == NULL) return(NULL);
492 if (fieldname == NULL) return(NULL);
494 snprintf(fieldhdr, sizeof fieldhdr, "%s:", fieldname);
496 /* Locate the end of the headers, so we don't run past that point */
497 end_of_headers = bmstrstr(rfc822, "\n\r\n", strncmp);
498 if (end_of_headers == NULL) {
499 end_of_headers = bmstrstr(rfc822, "\n\n", strncmp);
501 if (end_of_headers == NULL) return (NULL);
503 field_start = bmstrstr(rfc822, fieldhdr, strncasecmp);
504 if (field_start == NULL) return(NULL);
505 if (field_start > end_of_headers) return(NULL);
507 fieldbuf = malloc(SIZ);
508 strcpy(fieldbuf, "");
511 ptr = memreadline(ptr, fieldbuf, SIZ-strlen(fieldbuf) );
512 while ( (isspace(ptr[0])) && (ptr < end_of_headers) ) {
513 strcat(fieldbuf, " ");
514 cont = &fieldbuf[strlen(fieldbuf)];
515 ptr = memreadline(ptr, cont, SIZ-strlen(fieldbuf) );
519 strcpy(fieldbuf, &fieldbuf[strlen(fieldhdr)]);
527 /*****************************************************************************
528 * DIRECTORY MANAGEMENT FUNCTIONS *
529 *****************************************************************************/
532 * Generate the index key for an Internet e-mail address to be looked up
535 void directory_key(char *key, char *addr) {
539 for (i=0; i<strlen(addr); ++i) {
540 if (!isspace(addr[i])) {
541 key[keylen++] = tolower(addr[i]);
546 lprintf(CTDL_DEBUG, "Directory key is <%s>\n", key);
551 /* Return nonzero if the supplied address is in a domain we keep in
554 int IsDirectory(char *addr) {
558 extract_token(domain, addr, 1, '@');
561 h = CtdlHostAlias(domain);
562 lprintf(CTDL_DEBUG, "IsDirectory(%s)\n", domain);
564 if ( (h == hostalias_localhost) || (h == hostalias_directory) ) {
565 lprintf(CTDL_DEBUG, " ...yes\n");
569 lprintf(CTDL_DEBUG, " ...no\n");
576 * Initialize the directory database (erasing anything already there)
578 void CtdlDirectoryInit(void) {
579 cdb_trunc(CDB_DIRECTORY);
584 * Add an Internet e-mail address to the directory for a user
586 void CtdlDirectoryAddUser(char *internet_addr, char *citadel_addr) {
589 lprintf(CTDL_DEBUG, "Dir: %s --> %s\n",
590 internet_addr, citadel_addr);
591 if (IsDirectory(internet_addr) == 0) return;
593 directory_key(key, internet_addr);
595 cdb_store(CDB_DIRECTORY, key, strlen(key),
596 citadel_addr, strlen(citadel_addr)+1 );
601 * Delete an Internet e-mail address from the directory.
603 * (NOTE: we don't actually use or need the citadel_addr variable; it's merely
604 * here because the callback API expects to be able to send it.)
606 void CtdlDirectoryDelUser(char *internet_addr, char *citadel_addr) {
609 directory_key(key, internet_addr);
610 cdb_delete(CDB_DIRECTORY, key, strlen(key) );
615 * Look up an Internet e-mail address in the directory.
616 * On success: returns 0, and Citadel address stored in 'target'
617 * On failure: returns nonzero
619 int CtdlDirectoryLookup(char *target, char *internet_addr) {
620 struct cdbdata *cdbrec;
623 /* Dump it in there unchanged, just for kicks */
624 strcpy(target, internet_addr);
626 /* Only do lookups for addresses with hostnames in them */
627 if (num_tokens(internet_addr, '@') != 2) return(-1);
629 /* Only do lookups for domains in the directory */
630 if (IsDirectory(internet_addr) == 0) return(-1);
632 directory_key(key, internet_addr);
633 cdbrec = cdb_fetch(CDB_DIRECTORY, key, strlen(key) );
634 if (cdbrec != NULL) {
635 safestrncpy(target, cdbrec->ptr, SIZ);