4 * This file contains functions which handle the mapping of Internet addresses
5 * to users on the Citadel system.
17 #include <sys/types.h>
19 #if TIME_WITH_SYS_TIME
20 # include <sys/time.h>
24 # include <sys/time.h>
35 #include "sysdep_decls.h"
36 #include "citserver.h"
41 #include "internet_addressing.h"
44 #include "parsedate.h"
57 * Return nonzero if the supplied name is an alias for this host.
59 int CtdlHostAlias(char *fqdn) {
63 char host[SIZ], type[SIZ];
65 if (!strcasecmp(fqdn, config.c_fqdn)) return(hostalias_localhost);
66 if (!strcasecmp(fqdn, config.c_nodename)) return(hostalias_localhost);
67 if (inetcfg == NULL) return(hostalias_nomatch);
69 config_lines = num_tokens(inetcfg, '\n');
70 for (i=0; i<config_lines; ++i) {
71 extract_token(buf, inetcfg, i, '\n');
72 extract_token(host, buf, 0, '|');
73 extract_token(type, buf, 1, '|');
75 if ( (!strcasecmp(type, "localhost"))
76 && (!strcasecmp(fqdn, host)))
77 return(hostalias_localhost);
79 if ( (!strcasecmp(type, "gatewaydomain"))
80 && (!strcasecmp(&fqdn[strlen(fqdn)-strlen(host)], host)))
81 return(hostalias_gatewaydomain);
85 return(hostalias_nomatch);
95 * Return 0 if a given string fuzzy-matches a Citadel user account
97 * FIXME ... this needs to be updated to handle aliases.
99 int fuzzy_match(struct usersupp *us, char *matchstring) {
102 if ( (!strncasecmp(matchstring, "cit", 3))
103 && (atol(&matchstring[3]) == us->usernum)) {
108 for (a=0; a<strlen(us->fullname); ++a) {
109 if (!strncasecmp(&us->fullname[a],
110 matchstring, strlen(matchstring))) {
119 * Unfold a multi-line field into a single line, removing multi-whitespaces
121 void unfold_rfc822_field(char *field) {
125 striplt(field); /* remove leading/trailing whitespace */
127 /* convert non-space whitespace to spaces, and remove double blanks */
128 for (i=0; i<strlen(field); ++i) {
129 if (field[i]=='\"') quote = 1 - quote;
131 if (isspace(field[i])) field[i] = ' ';
132 while (isspace(field[i]) && isspace(field[i+1])) {
133 strcpy(&field[i+1], &field[i+2]);
142 * Split an RFC822-style address into userid, host, and full name
145 void process_rfc822_addr(char *rfc822, char *user, char *node, char *name)
150 strcpy(node, config.c_fqdn);
153 /* extract full name - first, it's From minus <userid> */
154 strcpy(name, rfc822);
155 for (a = 0; a < strlen(name); ++a) {
156 if (name[a] == '<') {
158 strcpy(&name[a], &name[a + 1]);
159 } while ((strlen(name) > 0) && (name[a] != '>'));
160 strcpy(&name[a], &name[a + 1]);
163 /* strip anything to the left of a bang */
164 while ((strlen(name) > 0) && (haschar(name, '!') > 0))
165 strcpy(name, &name[1]);
167 /* and anything to the right of a @ or % */
168 for (a = 0; a < strlen(name); ++a) {
175 /* but if there are parentheses, that changes the rules... */
176 if ((haschar(rfc822, '(') == 1) && (haschar(rfc822, ')') == 1)) {
177 strcpy(name, rfc822);
178 while ((strlen(name) > 0) && (name[0] != '(')) {
179 strcpy(&name[0], &name[1]);
181 strcpy(&name[0], &name[1]);
182 for (a = 0; a < strlen(name); ++a) {
183 if (name[a] == ')') {
188 /* but if there are a set of quotes, that supersedes everything */
189 if (haschar(rfc822, 34) == 2) {
190 strcpy(name, rfc822);
191 while ((strlen(name) > 0) && (name[0] != 34)) {
192 strcpy(&name[0], &name[1]);
194 strcpy(&name[0], &name[1]);
195 for (a = 0; a < strlen(name); ++a)
199 /* extract user id */
200 strcpy(user, rfc822);
202 /* first get rid of anything in parens */
203 for (a = 0; a < strlen(user); ++a)
204 if (user[a] == '(') {
206 strcpy(&user[a], &user[a + 1]);
207 } while ((strlen(user) > 0) && (user[a] != ')'));
208 strcpy(&user[a], &user[a + 1]);
210 /* if there's a set of angle brackets, strip it down to that */
211 if ((haschar(user, '<') == 1) && (haschar(user, '>') == 1)) {
212 while ((strlen(user) > 0) && (user[0] != '<')) {
213 strcpy(&user[0], &user[1]);
215 strcpy(&user[0], &user[1]);
216 for (a = 0; a < strlen(user); ++a)
220 /* strip anything to the left of a bang */
221 while ((strlen(user) > 0) && (haschar(user, '!') > 0))
222 strcpy(user, &user[1]);
224 /* and anything to the right of a @ or % */
225 for (a = 0; a < strlen(user); ++a) {
233 /* extract node name */
234 strcpy(node, rfc822);
236 /* first get rid of anything in parens */
237 for (a = 0; a < strlen(node); ++a)
238 if (node[a] == '(') {
240 strcpy(&node[a], &node[a + 1]);
241 } while ((strlen(node) > 0) && (node[a] != ')'));
242 strcpy(&node[a], &node[a + 1]);
244 /* if there's a set of angle brackets, strip it down to that */
245 if ((haschar(node, '<') == 1) && (haschar(node, '>') == 1)) {
246 while ((strlen(node) > 0) && (node[0] != '<')) {
247 strcpy(&node[0], &node[1]);
249 strcpy(&node[0], &node[1]);
250 for (a = 0; a < strlen(node); ++a)
255 /* If no node specified, tack ours on instead */
257 (haschar(node, '@')==0)
258 && (haschar(node, '%')==0)
259 && (haschar(node, '!')==0)
261 strcpy(node, config.c_nodename);
266 /* strip anything to the left of a @ */
267 while ((strlen(node) > 0) && (haschar(node, '@') > 0))
268 strcpy(node, &node[1]);
270 /* strip anything to the left of a % */
271 while ((strlen(node) > 0) && (haschar(node, '%') > 0))
272 strcpy(node, &node[1]);
274 /* reduce multiple system bang paths to node!user */
275 while ((strlen(node) > 0) && (haschar(node, '!') > 1))
276 strcpy(node, &node[1]);
278 /* now get rid of the user portion of a node!user string */
279 for (a = 0; a < strlen(node); ++a)
284 /* strip leading and trailing spaces in all strings */
293 * Back end for convert_internet_address()
294 * (Compares an internet name [buffer1] and stores in [buffer2] if found)
296 void try_name(struct usersupp *us, void *data) {
297 struct trynamebuf *tnb;
298 tnb = (struct trynamebuf *)data;
300 if (!strncasecmp(tnb->buffer1, "cit", 3))
301 if (atol(&tnb->buffer1[3]) == us->usernum)
302 strcpy(tnb->buffer2, us->fullname);
304 if (!collapsed_strcmp(tnb->buffer1, us->fullname))
305 strcpy(tnb->buffer2, us->fullname);
307 if (us->uid != BBSUID)
308 if (!strcasecmp(tnb->buffer1, getpwuid(us->uid)->pw_name))
309 strcpy(tnb->buffer2, us->fullname);
314 * Convert an Internet email address to a Citadel user/host combination
316 int convert_internet_address(char *destuser, char *desthost, char *source)
321 struct quickroom qrbuf;
324 struct trynamebuf tnb;
327 char sourcealias[1024];
329 safestrncpy(sourcealias, source, sizeof(sourcealias) );
334 process_rfc822_addr(sourcealias, user, node, name);
335 lprintf(9, "process_rfc822_addr() converted to <%s@%s> (%s)\n",
338 /* Map the FQDN to a Citadel node name
340 hostalias = CtdlHostAlias(node);
342 case hostalias_localhost:
343 strcpy(node, config.c_nodename);
346 case hostalias_gatewaydomain:
347 extract_token(buf, node, 0, '.');
348 safestrncpy(node, buf, sizeof buf);
351 /* Now try to resolve the name
352 * FIXME ... do the multiple-addresses thing
354 if (!strcasecmp(node, config.c_nodename)) {
357 /* First, see if we hit an alias. Don't do this more than
358 * a few times, in case we accidentally hit an alias loop
360 strcpy(sourcealias, user);
362 if ( (strcasecmp(user, sourcealias)) && (++passes < 3) )
365 /* Try all local rooms */
366 if (!strncasecmp(user, "room_", 5)) {
367 strcpy(name, &user[5]);
368 for (i=0; i<strlen(name); ++i)
369 if (name[i]=='_') name[i]=' ';
370 if (getroom(&qrbuf, name) == 0) {
371 strcpy(destuser, qrbuf.QRname);
372 strcpy(desthost, config.c_nodename);
373 return rfc822_room_delivery;
377 /* Try all local users */
378 strcpy(destuser, user);
379 strcpy(desthost, config.c_nodename);
380 strcpy(tnb.buffer1, user);
381 strcpy(tnb.buffer2, "");
382 ForEachUser(try_name, &tnb);
383 if (strlen(tnb.buffer2) == 0) return(rfc822_no_such_user);
384 strcpy(destuser, tnb.buffer2);
385 return(rfc822_address_locally_validated);
388 strcpy(destuser, user);
389 strcpy(desthost, node);
390 if (hostalias == hostalias_gatewaydomain)
391 return(rfc822_address_on_citadel_network);
392 return(rfc822_address_nonlocal);
399 * convert_field() is a helper function for convert_internet_message().
400 * Given start/end positions for an rfc822 field, it converts it to a Citadel
401 * field if it wants to, and unfolds it if necessary.
403 * Returns 1 if the field was converted and inserted into the Citadel message
404 * structure, implying that the source field should be removed from the
407 int convert_field(struct CtdlMessage *msg, int beg, int end) {
420 rfc822 = msg->cm_fields['M']; /* M field contains rfc822 text */
421 for (i = end; i >= beg; --i) {
422 if (rfc822[i] == ':') colonpos = i;
425 if (colonpos < 0) return(0); /* no colon? not a valid header line */
427 key = mallok((end - beg) + 2);
428 safestrncpy(key, &rfc822[beg], (end-beg)+1);
429 key[colonpos - beg] = 0;
430 value = &key[(colonpos - beg) + 1];
431 unfold_rfc822_field(value);
434 * Here's the big rfc822-to-citadel loop.
437 /* Date/time is converted into a unix timestamp. If the conversion
438 * fails, we replace it with the time the message arrived locally.
440 if (!strcasecmp(key, "Date")) {
441 parsed_date = parsedate(value);
442 if (parsed_date < 0L) parsed_date = time(NULL);
443 snprintf(buf, sizeof buf, "%ld", parsed_date );
444 if (msg->cm_fields['T'] == NULL)
445 msg->cm_fields['T'] = strdoop(buf);
449 else if (!strcasecmp(key, "From")) {
450 process_rfc822_addr(value, user, node, name);
451 lprintf(9, "Converted to <%s@%s> (%s)\n", user, node, name);
452 snprintf(addr, sizeof addr, "%s@%s", user, node);
453 if (msg->cm_fields['A'] == NULL)
454 msg->cm_fields['A'] = strdoop(name);
456 if (msg->cm_fields['F'] == NULL)
457 msg->cm_fields['F'] = strdoop(addr);
461 else if (!strcasecmp(key, "Subject")) {
462 if (msg->cm_fields['U'] == NULL)
463 msg->cm_fields['U'] = strdoop(value);
467 else if (!strcasecmp(key, "Message-ID")) {
468 if (msg->cm_fields['I'] != NULL) {
469 lprintf(5, "duplicate message id\n");
472 if (msg->cm_fields['I'] == NULL) {
473 msg->cm_fields['I'] = strdoop(value);
475 /* Strip angle brackets */
476 while (haschar(msg->cm_fields['I'], '<') > 0) {
477 strcpy(&msg->cm_fields['I'][0],
478 &msg->cm_fields['I'][1]);
480 for (i = 0; i<strlen(msg->cm_fields['I']); ++i)
481 if (msg->cm_fields['I'][i] == '>')
482 msg->cm_fields['I'][i] = 0;
488 /* Clean up and move on. */
489 phree(key); /* Don't free 'value', it's actually the same buffer */
495 * Convert an RFC822 message (headers + body) to a CtdlMessage structure.
496 * NOTE: the supplied buffer becomes part of the CtdlMessage structure, and
497 * will be deallocated when CtdlFreeMessage() is called. Therefore, the
498 * supplied buffer should be DEREFERENCED. It should not be freed or used
501 struct CtdlMessage *convert_internet_message(char *rfc822) {
503 struct CtdlMessage *msg;
504 int pos, beg, end, msglen;
509 msg = mallok(sizeof(struct CtdlMessage));
510 if (msg == NULL) return msg;
512 memset(msg, 0, sizeof(struct CtdlMessage));
513 msg->cm_magic = CTDLMESSAGE_MAGIC; /* self check */
514 msg->cm_anon_type = 0; /* never anonymous */
515 msg->cm_format_type = FMT_RFC822; /* internet message */
516 msg->cm_fields['M'] = rfc822;
518 lprintf(9, "Unconverted RFC822 message length = %d\n", strlen(rfc822));
524 /* Locate beginning and end of field, keeping in mind that
525 * some fields might be multiline
530 msglen = strlen(rfc822);
531 while ( (end < 0) && (done == 0) ) {
533 if ((rfc822[pos]=='\n')
534 && (!isspace(rfc822[pos+1]))) {
538 /* done with headers? */
539 if ( ((rfc822[pos]=='\n')
540 ||(rfc822[pos]=='\r') )
541 && ( (rfc822[pos+1]=='\n')
542 ||(rfc822[pos+1]=='\r')) ) {
547 if (pos >= (msglen-1) ) {
556 /* At this point we have a field. Are we interested in it? */
557 converted = convert_field(msg, beg, end);
559 /* Strip the field out of the RFC822 header if we used it */
561 strcpy(&rfc822[beg], &rfc822[pos]);
565 /* If we've hit the end of the message, bail out */
566 if (pos > strlen(rfc822)) done = 1;
569 /* Follow-up sanity checks... */
571 /* If there's no timestamp on this message, set it to now. */
572 if (msg->cm_fields['T'] == NULL) {
573 snprintf(buf, sizeof buf, "%ld", time(NULL));
574 msg->cm_fields['T'] = strdoop(buf);
577 lprintf(9, "RFC822 length remaining after conversion = %d\n",
585 * Look for a particular header field in an RFC822 message text. If the
586 * requested field is found, it is unfolded (if necessary) and returned to
587 * the caller. The field name is stripped out, leaving only its contents.
588 * The caller is responsible for freeing the returned buffer. If the requested
589 * field is not present, or anything else goes wrong, it returns NULL.
591 char *rfc822_fetch_field(char *rfc822, char *fieldname) {
596 char *fieldbuf = NULL;
598 /* Should never happen, but sometimes we get stupid */
599 if (rfc822 == NULL) return(NULL);
600 if (fieldname == NULL) return(NULL);
604 /* Locate beginning and end of field, keeping in mind that
605 * some fields might be multiline
609 for (pos=beg; ((pos<=strlen(rfc822))&&(end<0)); ++pos) {
610 if ((rfc822[pos]=='\n')
611 && (!isspace(rfc822[pos+1]))) {
614 if ( (rfc822[pos]=='\n') /* done w. headers? */
615 && ( (rfc822[pos+1]=='\n')
616 ||(rfc822[pos+1]=='\r'))) {
623 /* At this point we have a field. Is it The One? */
625 fieldbuf = mallok((end-beg)+3);
626 if (fieldbuf == NULL) return(NULL);
627 safestrncpy(fieldbuf, &rfc822[beg], (end-beg)+1);
628 unfold_rfc822_field(fieldbuf);
630 for (i = strlen(fieldbuf); i >= 0; --i) {
631 if (fieldbuf[i] == ':') colonpos = i;
634 fieldbuf[colonpos] = 0;
635 if (!strcasecmp(fieldbuf, fieldname)) {
636 strcpy(fieldbuf, &fieldbuf[colonpos+1]);
644 /* If we've hit the end of the message, bail out */
645 if (pos > strlen(rfc822)) done = 1;