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"
67 * Return nonzero if the supplied name is an alias for this host.
69 int CtdlHostAlias(char *fqdn) {
73 char host[SIZ], type[SIZ];
75 if (!strcasecmp(fqdn, config.c_fqdn)) return(hostalias_localhost);
76 if (!strcasecmp(fqdn, config.c_nodename)) return(hostalias_localhost);
77 if (inetcfg == NULL) return(hostalias_nomatch);
79 config_lines = num_tokens(inetcfg, '\n');
80 for (i=0; i<config_lines; ++i) {
81 extract_token(buf, inetcfg, i, '\n');
82 extract_token(host, buf, 0, '|');
83 extract_token(type, buf, 1, '|');
85 if ( (!strcasecmp(type, "localhost"))
86 && (!strcasecmp(fqdn, host)))
87 return(hostalias_localhost);
89 if ( (!strcasecmp(type, "gatewaydomain"))
90 && (!strcasecmp(&fqdn[strlen(fqdn)-strlen(host)], host)))
91 return(hostalias_gatewaydomain);
95 return(hostalias_nomatch);
105 * Return 0 if a given string fuzzy-matches a Citadel user account
107 * FIXME ... this needs to be updated to handle aliases.
109 int fuzzy_match(struct usersupp *us, char *matchstring) {
112 if ( (!strncasecmp(matchstring, "cit", 3))
113 && (atol(&matchstring[3]) == us->usernum)) {
118 for (a=0; a<strlen(us->fullname); ++a) {
119 if (!strncasecmp(&us->fullname[a],
120 matchstring, strlen(matchstring))) {
129 * Unfold a multi-line field into a single line, removing multi-whitespaces
131 void unfold_rfc822_field(char *field) {
135 striplt(field); /* remove leading/trailing whitespace */
137 /* convert non-space whitespace to spaces, and remove double blanks */
138 for (i=0; i<strlen(field); ++i) {
139 if (field[i]=='\"') quote = 1 - quote;
141 if (isspace(field[i])) field[i] = ' ';
142 while (isspace(field[i]) && isspace(field[i+1])) {
143 strcpy(&field[i+1], &field[i+2]);
152 * Split an RFC822-style address into userid, host, and full name
155 void process_rfc822_addr(char *rfc822, char *user, char *node, char *name)
160 strcpy(node, config.c_fqdn);
163 /* extract full name - first, it's From minus <userid> */
164 strcpy(name, rfc822);
165 stripout(name, '<', '>');
167 /* strip anything to the left of a bang */
168 while ((strlen(name) > 0) && (haschar(name, '!') > 0))
169 strcpy(name, &name[1]);
171 /* and anything to the right of a @ or % */
172 for (a = 0; a < strlen(name); ++a) {
179 /* but if there are parentheses, that changes the rules... */
180 if ((haschar(rfc822, '(') == 1) && (haschar(rfc822, ')') == 1)) {
181 strcpy(name, rfc822);
182 stripallbut(name, '(', ')');
185 /* but if there are a set of quotes, that supersedes everything */
186 if (haschar(rfc822, 34) == 2) {
187 strcpy(name, rfc822);
188 while ((strlen(name) > 0) && (name[0] != 34)) {
189 strcpy(&name[0], &name[1]);
191 strcpy(&name[0], &name[1]);
192 for (a = 0; a < strlen(name); ++a)
196 /* extract user id */
197 strcpy(user, rfc822);
199 /* first get rid of anything in parens */
200 stripout(user, '(', ')');
202 /* if there's a set of angle brackets, strip it down to that */
203 if ((haschar(user, '<') == 1) && (haschar(user, '>') == 1)) {
204 stripallbut(user, '<', '>');
207 /* strip anything to the left of a bang */
208 while ((strlen(user) > 0) && (haschar(user, '!') > 0))
209 strcpy(user, &user[1]);
211 /* and anything to the right of a @ or % */
212 for (a = 0; a < strlen(user); ++a) {
220 /* extract node name */
221 strcpy(node, rfc822);
223 /* first get rid of anything in parens */
224 stripout(node, '(', ')');
226 /* if there's a set of angle brackets, strip it down to that */
227 if ((haschar(node, '<') == 1) && (haschar(node, '>') == 1)) {
228 stripallbut(node, '<', '>');
231 /* If no node specified, tack ours on instead */
233 (haschar(node, '@')==0)
234 && (haschar(node, '%')==0)
235 && (haschar(node, '!')==0)
237 strcpy(node, config.c_nodename);
242 /* strip anything to the left of a @ */
243 while ((strlen(node) > 0) && (haschar(node, '@') > 0))
244 strcpy(node, &node[1]);
246 /* strip anything to the left of a % */
247 while ((strlen(node) > 0) && (haschar(node, '%') > 0))
248 strcpy(node, &node[1]);
250 /* reduce multiple system bang paths to node!user */
251 while ((strlen(node) > 0) && (haschar(node, '!') > 1))
252 strcpy(node, &node[1]);
254 /* now get rid of the user portion of a node!user string */
255 for (a = 0; a < strlen(node); ++a)
260 /* strip leading and trailing spaces in all strings */
269 * Back end for convert_internet_address()
270 * (Compares an internet name [buffer1] and stores in [buffer2] if found)
272 void try_name(struct usersupp *us, void *data) {
274 struct trynamebuf *tnb;
275 tnb = (struct trynamebuf *)data;
277 if (!strncasecmp(tnb->buffer1, "cit", 3))
278 if (atol(&tnb->buffer1[3]) == us->usernum)
279 strcpy(tnb->buffer2, us->fullname);
281 if (!collapsed_strcmp(tnb->buffer1, us->fullname))
282 strcpy(tnb->buffer2, us->fullname);
284 if (us->uid != BBSUID) {
285 pw = getpwuid(us->uid);
287 if (!strcasecmp(tnb->buffer1, pw->pw_name)) {
288 strcpy(tnb->buffer2, us->fullname);
296 * Convert an Internet email address to a Citadel user/host combination
298 int convert_internet_address(char *destuser, char *desthost, char *source)
303 struct quickroom qrbuf;
306 struct trynamebuf tnb;
309 char sourcealias[1024];
311 safestrncpy(sourcealias, source, sizeof(sourcealias) );
316 process_rfc822_addr(sourcealias, user, node, name);
317 lprintf(9, "process_rfc822_addr() converted to <%s@%s> (%s)\n",
320 /* Map the FQDN to a Citadel node name
322 hostalias = CtdlHostAlias(node);
324 case hostalias_localhost:
325 strcpy(node, config.c_nodename);
328 case hostalias_gatewaydomain:
329 extract_token(buf, node, 0, '.');
330 safestrncpy(node, buf, sizeof buf);
333 /* Now try to resolve the name
334 * FIXME ... do the multiple-addresses thing
336 if (!strcasecmp(node, config.c_nodename)) {
339 /* First, see if we hit an alias. Don't do this more than
340 * a few times, in case we accidentally hit an alias loop
342 strcpy(sourcealias, user);
344 if ( (strcasecmp(user, sourcealias)) && (++passes < 3) )
347 /* Try all local rooms */
348 if (!strncasecmp(user, "room_", 5)) {
349 strcpy(name, &user[5]);
350 for (i=0; i<strlen(name); ++i)
351 if (name[i]=='_') name[i]=' ';
352 if (getroom(&qrbuf, name) == 0) {
353 strcpy(destuser, qrbuf.QRname);
354 strcpy(desthost, config.c_nodename);
355 return rfc822_room_delivery;
359 /* Try all local users */
360 strcpy(destuser, user);
361 strcpy(desthost, config.c_nodename);
362 strcpy(tnb.buffer1, user);
363 strcpy(tnb.buffer2, "");
364 ForEachUser(try_name, &tnb);
365 if (strlen(tnb.buffer2) == 0) return(rfc822_no_such_user);
366 strcpy(destuser, tnb.buffer2);
367 return(rfc822_address_locally_validated);
370 strcpy(destuser, user);
371 strcpy(desthost, node);
372 if (hostalias == hostalias_gatewaydomain)
373 return(rfc822_address_on_citadel_network);
374 return(rfc822_address_nonlocal);
381 * convert_field() is a helper function for convert_internet_message().
382 * Given start/end positions for an rfc822 field, it converts it to a Citadel
383 * field if it wants to, and unfolds it if necessary.
385 * Returns 1 if the field was converted and inserted into the Citadel message
386 * structure, implying that the source field should be removed from the
389 int convert_field(struct CtdlMessage *msg, int beg, int end) {
402 rfc822 = msg->cm_fields['M']; /* M field contains rfc822 text */
403 for (i = end; i >= beg; --i) {
404 if (rfc822[i] == ':') colonpos = i;
407 if (colonpos < 0) return(0); /* no colon? not a valid header line */
409 key = mallok((end - beg) + 2);
410 safestrncpy(key, &rfc822[beg], (end-beg)+1);
411 key[colonpos - beg] = 0;
412 value = &key[(colonpos - beg) + 1];
413 unfold_rfc822_field(value);
416 * Here's the big rfc822-to-citadel loop.
419 /* Date/time is converted into a unix timestamp. If the conversion
420 * fails, we replace it with the time the message arrived locally.
422 if (!strcasecmp(key, "Date")) {
423 parsed_date = parsedate(value);
424 if (parsed_date < 0L) parsed_date = time(NULL);
425 snprintf(buf, sizeof buf, "%ld", (long)parsed_date );
426 if (msg->cm_fields['T'] == NULL)
427 msg->cm_fields['T'] = strdoop(buf);
431 else if (!strcasecmp(key, "From")) {
432 process_rfc822_addr(value, user, node, name);
433 lprintf(9, "Converted to <%s@%s> (%s)\n", user, node, name);
434 snprintf(addr, sizeof addr, "%s@%s", user, node);
435 if (msg->cm_fields['A'] == NULL)
436 msg->cm_fields['A'] = strdoop(name);
438 if (msg->cm_fields['F'] == NULL)
439 msg->cm_fields['F'] = strdoop(addr);
443 else if (!strcasecmp(key, "Subject")) {
444 if (msg->cm_fields['U'] == NULL)
445 msg->cm_fields['U'] = strdoop(value);
449 else if (!strcasecmp(key, "Message-ID")) {
450 if (msg->cm_fields['I'] != NULL) {
451 lprintf(5, "duplicate message id\n");
454 if (msg->cm_fields['I'] == NULL) {
455 msg->cm_fields['I'] = strdoop(value);
457 /* Strip angle brackets */
458 while (haschar(msg->cm_fields['I'], '<') > 0) {
459 strcpy(&msg->cm_fields['I'][0],
460 &msg->cm_fields['I'][1]);
462 for (i = 0; i<strlen(msg->cm_fields['I']); ++i)
463 if (msg->cm_fields['I'][i] == '>')
464 msg->cm_fields['I'][i] = 0;
470 /* Clean up and move on. */
471 phree(key); /* Don't free 'value', it's actually the same buffer */
477 * Convert an RFC822 message (headers + body) to a CtdlMessage structure.
478 * NOTE: the supplied buffer becomes part of the CtdlMessage structure, and
479 * will be deallocated when CtdlFreeMessage() is called. Therefore, the
480 * supplied buffer should be DEREFERENCED. It should not be freed or used
483 struct CtdlMessage *convert_internet_message(char *rfc822) {
485 struct CtdlMessage *msg;
486 int pos, beg, end, msglen;
491 msg = mallok(sizeof(struct CtdlMessage));
492 if (msg == NULL) return msg;
494 memset(msg, 0, sizeof(struct CtdlMessage));
495 msg->cm_magic = CTDLMESSAGE_MAGIC; /* self check */
496 msg->cm_anon_type = 0; /* never anonymous */
497 msg->cm_format_type = FMT_RFC822; /* internet message */
498 msg->cm_fields['M'] = rfc822;
500 lprintf(9, "Unconverted RFC822 message length = %ld\n", (long)strlen(rfc822));
506 /* Locate beginning and end of field, keeping in mind that
507 * some fields might be multiline
512 msglen = strlen(rfc822);
513 while ( (end < 0) && (done == 0) ) {
515 if ((rfc822[pos]=='\n')
516 && (!isspace(rfc822[pos+1]))) {
520 /* done with headers? */
521 if ( ((rfc822[pos]=='\n')
522 ||(rfc822[pos]=='\r') )
523 && ( (rfc822[pos+1]=='\n')
524 ||(rfc822[pos+1]=='\r')) ) {
529 if (pos >= (msglen-1) ) {
538 /* At this point we have a field. Are we interested in it? */
539 converted = convert_field(msg, beg, end);
541 /* Strip the field out of the RFC822 header if we used it */
543 strcpy(&rfc822[beg], &rfc822[pos]);
547 /* If we've hit the end of the message, bail out */
548 if (pos > strlen(rfc822)) done = 1;
551 /* Follow-up sanity checks... */
553 /* If there's no timestamp on this message, set it to now. */
554 if (msg->cm_fields['T'] == NULL) {
555 snprintf(buf, sizeof buf, "%ld", (long)time(NULL));
556 msg->cm_fields['T'] = strdoop(buf);
559 lprintf(9, "RFC822 length remaining after conversion = %ld\n",
560 (long)strlen(rfc822));
567 * Look for a particular header field in an RFC822 message text. If the
568 * requested field is found, it is unfolded (if necessary) and returned to
569 * the caller. The field name is stripped out, leaving only its contents.
570 * The caller is responsible for freeing the returned buffer. If the requested
571 * field is not present, or anything else goes wrong, it returns NULL.
573 char *rfc822_fetch_field(char *rfc822, char *fieldname) {
578 char *fieldbuf = NULL;
580 /* Should never happen, but sometimes we get stupid */
581 if (rfc822 == NULL) return(NULL);
582 if (fieldname == NULL) return(NULL);
586 /* Locate beginning and end of field, keeping in mind that
587 * some fields might be multiline
591 for (pos=beg; ((pos<=strlen(rfc822))&&(end<0)); ++pos) {
592 if ((rfc822[pos]=='\n')
593 && (!isspace(rfc822[pos+1]))) {
596 if ( (rfc822[pos]=='\n') /* done w. headers? */
597 && ( (rfc822[pos+1]=='\n')
598 ||(rfc822[pos+1]=='\r'))) {
605 /* At this point we have a field. Is it The One? */
607 fieldbuf = mallok((end-beg)+3);
608 if (fieldbuf == NULL) return(NULL);
609 safestrncpy(fieldbuf, &rfc822[beg], (end-beg)+1);
610 unfold_rfc822_field(fieldbuf);
612 for (i = strlen(fieldbuf); i >= 0; --i) {
613 if (fieldbuf[i] == ':') colonpos = i;
616 fieldbuf[colonpos] = 0;
617 if (!strcasecmp(fieldbuf, fieldname)) {
618 strcpy(fieldbuf, &fieldbuf[colonpos+1]);
626 /* If we've hit the end of the message, bail out */
627 if (pos > strlen(rfc822)) done = 1;