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 "dynloader.h"
40 #include "sysdep_decls.h"
41 #include "citserver.h"
46 #include "internet_addressing.h"
49 #include "parsedate.h"
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 (!strcasecmp(fqdn, config.c_fqdn)) return(hostalias_localhost);
77 if (!strcasecmp(fqdn, config.c_nodename)) return(hostalias_localhost);
78 if (inetcfg == NULL) return(hostalias_nomatch);
80 config_lines = num_tokens(inetcfg, '\n');
81 for (i=0; i<config_lines; ++i) {
82 extract_token(buf, inetcfg, i, '\n');
83 extract_token(host, buf, 0, '|');
84 extract_token(type, buf, 1, '|');
86 if ( (!strcasecmp(type, "localhost"))
87 && (!strcasecmp(fqdn, host)))
88 return(hostalias_localhost);
90 if ( (!strcasecmp(type, "gatewaydomain"))
91 && (!strcasecmp(&fqdn[strlen(fqdn)-strlen(host)], host)))
92 return(hostalias_gatewaydomain);
94 if ( (!strcasecmp(type, "directory"))
95 && (!strcasecmp(&fqdn[strlen(fqdn)-strlen(host)], host)))
96 return(hostalias_directory);
100 return(hostalias_nomatch);
110 * Return 0 if a given string fuzzy-matches a Citadel user account
112 * FIXME ... this needs to be updated to handle aliases.
114 int fuzzy_match(struct usersupp *us, char *matchstring) {
117 if ( (!strncasecmp(matchstring, "cit", 3))
118 && (atol(&matchstring[3]) == us->usernum)) {
123 for (a=0; a<strlen(us->fullname); ++a) {
124 if (!strncasecmp(&us->fullname[a],
125 matchstring, strlen(matchstring))) {
134 * Unfold a multi-line field into a single line, removing multi-whitespaces
136 void unfold_rfc822_field(char *field) {
140 striplt(field); /* remove leading/trailing whitespace */
142 /* convert non-space whitespace to spaces, and remove double blanks */
143 for (i=0; i<strlen(field); ++i) {
144 if (field[i]=='\"') quote = 1 - quote;
146 if (isspace(field[i])) field[i] = ' ';
147 while (isspace(field[i]) && isspace(field[i+1])) {
148 strcpy(&field[i+1], &field[i+2]);
157 * Split an RFC822-style address into userid, host, and full name
160 void process_rfc822_addr(char *rfc822, char *user, char *node, char *name)
165 strcpy(node, config.c_fqdn);
168 /* extract full name - first, it's From minus <userid> */
169 strcpy(name, rfc822);
170 stripout(name, '<', '>');
172 /* strip anything to the left of a bang */
173 while ((strlen(name) > 0) && (haschar(name, '!') > 0))
174 strcpy(name, &name[1]);
176 /* and anything to the right of a @ or % */
177 for (a = 0; a < strlen(name); ++a) {
184 /* but if there are parentheses, that changes the rules... */
185 if ((haschar(rfc822, '(') == 1) && (haschar(rfc822, ')') == 1)) {
186 strcpy(name, rfc822);
187 stripallbut(name, '(', ')');
190 /* but if there are a set of quotes, that supersedes everything */
191 if (haschar(rfc822, 34) == 2) {
192 strcpy(name, rfc822);
193 while ((strlen(name) > 0) && (name[0] != 34)) {
194 strcpy(&name[0], &name[1]);
196 strcpy(&name[0], &name[1]);
197 for (a = 0; a < strlen(name); ++a)
201 /* extract user id */
202 strcpy(user, rfc822);
204 /* first get rid of anything in parens */
205 stripout(user, '(', ')');
207 /* if there's a set of angle brackets, strip it down to that */
208 if ((haschar(user, '<') == 1) && (haschar(user, '>') == 1)) {
209 stripallbut(user, '<', '>');
212 /* strip anything to the left of a bang */
213 while ((strlen(user) > 0) && (haschar(user, '!') > 0))
214 strcpy(user, &user[1]);
216 /* and anything to the right of a @ or % */
217 for (a = 0; a < strlen(user); ++a) {
225 /* extract node name */
226 strcpy(node, rfc822);
228 /* first get rid of anything in parens */
229 stripout(node, '(', ')');
231 /* if there's a set of angle brackets, strip it down to that */
232 if ((haschar(node, '<') == 1) && (haschar(node, '>') == 1)) {
233 stripallbut(node, '<', '>');
236 /* If no node specified, tack ours on instead */
238 (haschar(node, '@')==0)
239 && (haschar(node, '%')==0)
240 && (haschar(node, '!')==0)
242 strcpy(node, config.c_nodename);
247 /* strip anything to the left of a @ */
248 while ((strlen(node) > 0) && (haschar(node, '@') > 0))
249 strcpy(node, &node[1]);
251 /* strip anything to the left of a % */
252 while ((strlen(node) > 0) && (haschar(node, '%') > 0))
253 strcpy(node, &node[1]);
255 /* reduce multiple system bang paths to node!user */
256 while ((strlen(node) > 0) && (haschar(node, '!') > 1))
257 strcpy(node, &node[1]);
259 /* now get rid of the user portion of a node!user string */
260 for (a = 0; a < strlen(node); ++a)
265 /* strip leading and trailing spaces in all strings */
274 * convert_field() is a helper function for convert_internet_message().
275 * Given start/end positions for an rfc822 field, it converts it to a Citadel
276 * field if it wants to, and unfolds it if necessary.
278 * Returns 1 if the field was converted and inserted into the Citadel message
279 * structure, implying that the source field should be removed from the
282 int convert_field(struct CtdlMessage *msg, int beg, int end) {
295 rfc822 = msg->cm_fields['M']; /* M field contains rfc822 text */
296 for (i = end; i >= beg; --i) {
297 if (rfc822[i] == ':') colonpos = i;
300 if (colonpos < 0) return(0); /* no colon? not a valid header line */
302 key = mallok((end - beg) + 2);
303 safestrncpy(key, &rfc822[beg], (end-beg)+1);
304 key[colonpos - beg] = 0;
305 value = &key[(colonpos - beg) + 1];
306 unfold_rfc822_field(value);
309 * Here's the big rfc822-to-citadel loop.
312 /* Date/time is converted into a unix timestamp. If the conversion
313 * fails, we replace it with the time the message arrived locally.
315 if (!strcasecmp(key, "Date")) {
316 parsed_date = parsedate(value);
317 if (parsed_date < 0L) parsed_date = time(NULL);
318 snprintf(buf, sizeof buf, "%ld", (long)parsed_date );
319 if (msg->cm_fields['T'] == NULL)
320 msg->cm_fields['T'] = strdoop(buf);
324 else if (!strcasecmp(key, "From")) {
325 process_rfc822_addr(value, user, node, name);
326 lprintf(9, "Converted to <%s@%s> (%s)\n", user, node, name);
327 snprintf(addr, sizeof addr, "%s@%s", user, node);
328 if (msg->cm_fields['A'] == NULL)
329 msg->cm_fields['A'] = strdoop(name);
331 if (msg->cm_fields['F'] == NULL)
332 msg->cm_fields['F'] = strdoop(addr);
336 else if (!strcasecmp(key, "Subject")) {
337 if (msg->cm_fields['U'] == NULL)
338 msg->cm_fields['U'] = strdoop(value);
342 else if (!strcasecmp(key, "Message-ID")) {
343 if (msg->cm_fields['I'] != NULL) {
344 lprintf(5, "duplicate message id\n");
347 if (msg->cm_fields['I'] == NULL) {
348 msg->cm_fields['I'] = strdoop(value);
350 /* Strip angle brackets */
351 while (haschar(msg->cm_fields['I'], '<') > 0) {
352 strcpy(&msg->cm_fields['I'][0],
353 &msg->cm_fields['I'][1]);
355 for (i = 0; i<strlen(msg->cm_fields['I']); ++i)
356 if (msg->cm_fields['I'][i] == '>')
357 msg->cm_fields['I'][i] = 0;
363 /* Clean up and move on. */
364 phree(key); /* Don't free 'value', it's actually the same buffer */
370 * Convert an RFC822 message (headers + body) to a CtdlMessage structure.
371 * NOTE: the supplied buffer becomes part of the CtdlMessage structure, and
372 * will be deallocated when CtdlFreeMessage() is called. Therefore, the
373 * supplied buffer should be DEREFERENCED. It should not be freed or used
376 struct CtdlMessage *convert_internet_message(char *rfc822) {
378 struct CtdlMessage *msg;
379 int pos, beg, end, msglen;
384 msg = mallok(sizeof(struct CtdlMessage));
385 if (msg == NULL) return msg;
387 memset(msg, 0, sizeof(struct CtdlMessage));
388 msg->cm_magic = CTDLMESSAGE_MAGIC; /* self check */
389 msg->cm_anon_type = 0; /* never anonymous */
390 msg->cm_format_type = FMT_RFC822; /* internet message */
391 msg->cm_fields['M'] = rfc822;
393 lprintf(9, "Unconverted RFC822 message length = %ld\n", (long)strlen(rfc822));
399 /* Locate beginning and end of field, keeping in mind that
400 * some fields might be multiline
405 msglen = strlen(rfc822);
406 while ( (end < 0) && (done == 0) ) {
408 if ((rfc822[pos]=='\n')
409 && (!isspace(rfc822[pos+1]))) {
413 /* done with headers? */
414 if ( ((rfc822[pos]=='\n')
415 ||(rfc822[pos]=='\r') )
416 && ( (rfc822[pos+1]=='\n')
417 ||(rfc822[pos+1]=='\r')) ) {
422 if (pos >= (msglen-1) ) {
431 /* At this point we have a field. Are we interested in it? */
432 converted = convert_field(msg, beg, end);
434 /* Strip the field out of the RFC822 header if we used it */
436 strcpy(&rfc822[beg], &rfc822[pos]);
440 /* If we've hit the end of the message, bail out */
441 if (pos > strlen(rfc822)) done = 1;
444 /* Follow-up sanity checks... */
446 /* If there's no timestamp on this message, set it to now. */
447 if (msg->cm_fields['T'] == NULL) {
448 snprintf(buf, sizeof buf, "%ld", (long)time(NULL));
449 msg->cm_fields['T'] = strdoop(buf);
452 lprintf(9, "RFC822 length remaining after conversion = %ld\n",
453 (long)strlen(rfc822));
460 * Look for a particular header field in an RFC822 message text. If the
461 * requested field is found, it is unfolded (if necessary) and returned to
462 * the caller. The field name is stripped out, leaving only its contents.
463 * The caller is responsible for freeing the returned buffer. If the requested
464 * field is not present, or anything else goes wrong, it returns NULL.
466 char *rfc822_fetch_field(char *rfc822, char *fieldname) {
471 char *fieldbuf = NULL;
473 /* Should never happen, but sometimes we get stupid */
474 if (rfc822 == NULL) return(NULL);
475 if (fieldname == NULL) return(NULL);
479 /* Locate beginning and end of field, keeping in mind that
480 * some fields might be multiline
484 for (pos=beg; ((pos<=strlen(rfc822))&&(end<0)); ++pos) {
485 if ((rfc822[pos]=='\n')
486 && (!isspace(rfc822[pos+1]))) {
489 if ( (rfc822[pos]=='\n') /* done w. headers? */
490 && ( (rfc822[pos+1]=='\n')
491 ||(rfc822[pos+1]=='\r'))) {
498 /* At this point we have a field. Is it The One? */
500 fieldbuf = mallok((end-beg)+3);
501 if (fieldbuf == NULL) return(NULL);
502 safestrncpy(fieldbuf, &rfc822[beg], (end-beg)+1);
503 unfold_rfc822_field(fieldbuf);
505 for (i = strlen(fieldbuf); i >= 0; --i) {
506 if (fieldbuf[i] == ':') colonpos = i;
509 fieldbuf[colonpos] = 0;
510 if (!strcasecmp(fieldbuf, fieldname)) {
511 strcpy(fieldbuf, &fieldbuf[colonpos+1]);
519 /* If we've hit the end of the message, bail out */
520 if (pos > strlen(rfc822)) done = 1;
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(9, "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(9, "IsDirectory(%s)\n", domain);
564 if ( (h == hostalias_localhost) || (h == hostalias_directory) ) {
565 lprintf(9, " ...yes\n");
569 lprintf(9, " ...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(9, "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 lprintf(9, "CtdlDirectoryLookup(%s)\n", internet_addr);
625 if (IsDirectory(internet_addr) == 0) return(-1);
627 directory_key(key, internet_addr);
628 cdbrec = cdb_fetch(CDB_DIRECTORY, key, strlen(key) );
629 if (cdbrec != NULL) {
630 safestrncpy(target, cdbrec->ptr, SIZ);
632 lprintf(9, "Looked up as <%s>\n", target);
636 lprintf(9, "Lookup failed\n");