4 * This file contains functions which handle the mapping of Internet addresses
5 * to users on the Citadel system.
17 #include <sys/types.h>
25 #include "sysdep_decls.h"
26 #include "citserver.h"
30 #include "internet_addressing.h"
33 #include "parsedate.h"
46 * Return nonzero if the supplied name is an alias for this host.
48 int CtdlHostAlias(char *fqdn) {
52 char host[256], type[256];
54 if (!strcasecmp(fqdn, config.c_fqdn)) return(hostalias_localhost);
55 if (inetcfg == NULL) return(hostalias_nomatch);
57 config_lines = num_tokens(inetcfg, '\n');
58 for (i=0; i<config_lines; ++i) {
59 extract_token(buf, inetcfg, i, '\n');
60 extract_token(host, buf, 0, '|');
61 extract_token(type, buf, 1, '|');
63 if ( (!strcasecmp(type, "localhost"))
64 && (!strcasecmp(fqdn, host)))
65 return(hostalias_localhost);
67 if ( (!strcasecmp(type, "gatewaydomain"))
68 && (!strcasecmp(&fqdn[strlen(fqdn)-strlen(host)], host)))
69 return(hostalias_gatewaydomain);
73 return(hostalias_nomatch);
83 * Return 0 if a given string fuzzy-matches a Citadel user account
85 * FIX ... this needs to be updated to handle aliases.
87 int fuzzy_match(struct usersupp *us, char *matchstring) {
90 if ( (!strncasecmp(matchstring, "cit", 3))
91 && (atol(&matchstring[3]) == us->usernum)) {
96 for (a=0; a<strlen(us->fullname); ++a) {
97 if (!strncasecmp(&us->fullname[a],
98 matchstring, strlen(matchstring))) {
107 * Unfold a multi-line field into a single line, removing multi-whitespaces
109 void unfold_rfc822_field(char *field) {
113 striplt(field); /* remove leading/trailing whitespace */
115 /* convert non-space whitespace to spaces, and remove double blanks */
116 for (i=0; i<strlen(field); ++i) {
117 if (field[i]=='\"') quote = 1 - quote;
119 if (isspace(field[i])) field[i] = ' ';
120 while (isspace(field[i]) && isspace(field[i+1])) {
121 strcpy(&field[i+1], &field[i+2]);
130 * Split an RFC822-style address into userid, host, and full name
131 * (Originally from citmail.c, and unchanged so far)
134 void process_rfc822_addr(char *rfc822, char *user, char *node, char *name)
139 strcpy(node, config.c_fqdn);
142 /* extract full name - first, it's From minus <userid> */
143 strcpy(name, rfc822);
144 for (a = 0; a < strlen(name); ++a) {
145 if (name[a] == '<') {
147 strcpy(&name[a], &name[a + 1]);
148 } while ((strlen(name) > 0) && (name[a] != '>'));
149 strcpy(&name[a], &name[a + 1]);
152 /* strip anything to the left of a bang */
153 while ((strlen(name) > 0) && (haschar(name, '!') > 0))
154 strcpy(name, &name[1]);
156 /* and anything to the right of a @ or % */
157 for (a = 0; a < strlen(name); ++a) {
164 /* but if there are parentheses, that changes the rules... */
165 if ((haschar(rfc822, '(') == 1) && (haschar(rfc822, ')') == 1)) {
166 strcpy(name, rfc822);
167 while ((strlen(name) > 0) && (name[0] != '(')) {
168 strcpy(&name[0], &name[1]);
170 strcpy(&name[0], &name[1]);
171 for (a = 0; a < strlen(name); ++a) {
172 if (name[a] == ')') {
177 /* but if there are a set of quotes, that supersedes everything */
178 if (haschar(rfc822, 34) == 2) {
179 strcpy(name, rfc822);
180 while ((strlen(name) > 0) && (name[0] != 34)) {
181 strcpy(&name[0], &name[1]);
183 strcpy(&name[0], &name[1]);
184 for (a = 0; a < strlen(name); ++a)
188 /* extract user id */
189 strcpy(user, rfc822);
191 /* first get rid of anything in parens */
192 for (a = 0; a < strlen(user); ++a)
193 if (user[a] == '(') {
195 strcpy(&user[a], &user[a + 1]);
196 } while ((strlen(user) > 0) && (user[a] != ')'));
197 strcpy(&user[a], &user[a + 1]);
199 /* if there's a set of angle brackets, strip it down to that */
200 if ((haschar(user, '<') == 1) && (haschar(user, '>') == 1)) {
201 while ((strlen(user) > 0) && (user[0] != '<')) {
202 strcpy(&user[0], &user[1]);
204 strcpy(&user[0], &user[1]);
205 for (a = 0; a < strlen(user); ++a)
209 /* strip anything to the left of a bang */
210 while ((strlen(user) > 0) && (haschar(user, '!') > 0))
211 strcpy(user, &user[1]);
213 /* and anything to the right of a @ or % */
214 for (a = 0; a < strlen(user); ++a) {
222 /* extract node name */
223 strcpy(node, rfc822);
225 /* first get rid of anything in parens */
226 for (a = 0; a < strlen(node); ++a)
227 if (node[a] == '(') {
229 strcpy(&node[a], &node[a + 1]);
230 } while ((strlen(node) > 0) && (node[a] != ')'));
231 strcpy(&node[a], &node[a + 1]);
233 /* if there's a set of angle brackets, strip it down to that */
234 if ((haschar(node, '<') == 1) && (haschar(node, '>') == 1)) {
235 while ((strlen(node) > 0) && (node[0] != '<')) {
236 strcpy(&node[0], &node[1]);
238 strcpy(&node[0], &node[1]);
239 for (a = 0; a < strlen(node); ++a)
243 /* strip anything to the left of a @ */
244 while ((strlen(node) > 0) && (haschar(node, '@') > 0))
245 strcpy(node, &node[1]);
247 /* strip anything to the left of a % */
248 while ((strlen(node) > 0) && (haschar(node, '%') > 0))
249 strcpy(node, &node[1]);
251 /* reduce multiple system bang paths to node!user */
252 while ((strlen(node) > 0) && (haschar(node, '!') > 1))
253 strcpy(node, &node[1]);
255 /* now get rid of the user portion of a node!user string */
256 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) {
273 struct trynamebuf *tnb;
274 tnb = (struct trynamebuf *)data;
276 if (!strncasecmp(tnb->buffer1, "cit", 3))
277 if (atol(&tnb->buffer1[3]) == us->usernum)
278 strcpy(tnb->buffer2, us->fullname);
280 if (!collapsed_strcmp(tnb->buffer1, us->fullname))
281 strcpy(tnb->buffer2, us->fullname);
283 if (us->uid != BBSUID)
284 if (!strcasecmp(tnb->buffer1, getpwuid(us->uid)->pw_name))
285 strcpy(tnb->buffer2, us->fullname);
290 * Convert an Internet email address to a Citadel user/host combination
292 int convert_internet_address(char *destuser, char *desthost, char *source)
297 struct quickroom qrbuf;
300 struct trynamebuf tnb;
304 process_rfc822_addr(source, user, node, name);
306 /* Map the FQDN to a Citadel node name
308 hostalias = CtdlHostAlias(node);
310 case hostalias_localhost:
311 strcpy(node, config.c_nodename);
314 case hostalias_gatewaydomain:
315 extract_token(buf, node, 0, '.');
316 safestrncpy(node, buf, sizeof buf);
319 /* Now try to resolve the name
320 * FIX ... do the multiple-addresses thing
322 if (!strcasecmp(node, config.c_nodename)) {
323 /* Try all local rooms */
324 if (!strncasecmp(user, "room_", 5)) {
325 strcpy(name, &user[5]);
326 for (i=0; i<strlen(name); ++i)
327 if (name[i]=='_') name[i]=' ';
328 if (getroom(&qrbuf, name) == 0) {
329 strcpy(destuser, qrbuf.QRname);
330 strcpy(desthost, config.c_nodename);
331 return rfc822_room_delivery;
335 /* Try all local users */
336 strcpy(destuser, user);
337 strcpy(desthost, config.c_nodename);
338 strcpy(tnb.buffer1, user);
339 strcpy(tnb.buffer2, "");
340 ForEachUser(try_name, &tnb);
341 if (strlen(tnb.buffer2) == 0) return(rfc822_no_such_user);
342 strcpy(destuser, tnb.buffer2);
343 return(rfc822_address_locally_validated);
346 strcpy(destuser, user);
347 strcpy(desthost, node);
348 return(rfc822_address_invalid); /* unknown error */
355 * convert_field() is a helper function for convert_internet_message().
356 * Given start/end positions for an rfc822 field, it converts it to a Citadel
357 * field if it wants to, and unfolds it if necessary.
359 * Returns 1 if the field was converted and inserted into the Citadel message
360 * structure, implying that the source field should be removed from the
363 int convert_field(struct CtdlMessage *msg, int beg, int end) {
370 char *user, *node, *name;
372 rfc822 = msg->cm_fields['M']; /* M field contains rfc822 text */
373 for (i = end; i >= beg; --i) {
374 if (rfc822[i] == ':') colonpos = i;
377 if (colonpos < 0) return(0); /* no colon? not a valid header line */
379 key = mallok((end - beg) + 2);
380 safestrncpy(key, &rfc822[beg], (end-beg)+1);
381 key[colonpos - beg] = 0;
382 value = &key[(colonpos - beg) + 1];
383 unfold_rfc822_field(value);
385 lprintf(9, "Key=<%s> Value=<%s>\n", key, value);
387 /* Here's the big rfc822-to-citadel loop. */
389 if (!strcasecmp(key, "Date")) {
390 sprintf(buf, "%ld", parsedate(value) );
391 if (msg->cm_fields['T'] == NULL)
392 msg->cm_fields['T'] = strdoop(buf);
396 else if (!strcasecmp(key, "From")) {
400 process_rfc822_addr(value, user, node, name);
401 lprintf(9, "Converted to <%s@%s> (%s)\n", user, node, name);
402 if (msg->cm_fields['A'] == NULL)
403 msg->cm_fields['A'] = user;
406 if (msg->cm_fields['N'] == NULL)
407 msg->cm_fields['N'] = node;
410 if (msg->cm_fields['H'] == NULL)
411 msg->cm_fields['H'] = name;
417 else if (!strcasecmp(key, "Subject")) {
418 if (msg->cm_fields['U'] == NULL)
419 msg->cm_fields['U'] = strdoop(value);
423 /* Clean up and move on. */
424 phree(key); /* Don't free 'value', it's actually the same buffer */
430 * Convert an RFC822 message (headers + body) to a CtdlMessage structure.
432 struct CtdlMessage *convert_internet_message(char *rfc822) {
434 struct CtdlMessage *msg;
440 msg = mallok(sizeof(struct CtdlMessage));
441 if (msg == NULL) return msg;
443 memset(msg, 0, sizeof(struct CtdlMessage));
444 msg->cm_magic = CTDLMESSAGE_MAGIC; /* self check */
445 msg->cm_anon_type = 0; /* never anonymous */
446 msg->cm_format_type = FMT_RFC822; /* internet message */
447 msg->cm_fields['M'] = rfc822;
449 lprintf(9, "Unconverted RFC822 message length = %d\n", strlen(rfc822));
455 /* Locate beginning and end of field, keeping in mind that
456 * some fields might be multiline
460 for (pos=beg; ((pos<=strlen(rfc822))&&(end<0)); ++pos) {
461 if ((rfc822[pos]=='\n')
462 && (!isspace(rfc822[pos+1]))) {
465 if ( (rfc822[pos]=='\n') /* done w. headers? */
466 && ( (rfc822[pos+1]=='\n')
467 ||(rfc822[pos+1]=='\r'))) {
474 /* At this point we have a field. Are we interested in it? */
475 converted = convert_field(msg, beg, end);
479 strcpy(&rfc822[beg], &rfc822[pos]);
484 /* If we've hit the end of the message, bail out */
485 if (pos > strlen(rfc822)) done = 1;
488 /* Follow-up sanity checks... */
490 /* If there's no timestamp on this message, set it to now. */
491 if (msg->cm_fields['T'] == NULL) {
492 sprintf(buf, "%ld", time(NULL));
493 msg->cm_fields['T'] = strdoop(buf);
496 lprintf(9, "RFC822 length remaining after conversion = %d\n",