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"
32 #include "parsedate.h"
37 * Return 0 if a given string fuzzy-matches a Citadel user account
39 * FIX ... this needs to be updated to match any and all address syntaxes.
41 int fuzzy_match(struct usersupp *us, char *matchstring) {
44 for (a=0; a<strlen(us->fullname); ++a) {
45 if (!strncasecmp(&us->fullname[a],
46 matchstring, strlen(matchstring))) {
55 * Unfold a multi-line field into a single line, removing multi-whitespaces
57 void unfold_rfc822_field(char *field) {
61 striplt(field); /* remove leading/trailing whitespace */
63 /* convert non-space whitespace to spaces, and remove double blanks */
64 for (i=0; i<strlen(field); ++i) {
65 if (field[i]=='\"') quote = 1 - quote;
67 if (isspace(field[i])) field[i] = ' ';
68 while (isspace(field[i]) && isspace(field[i+1])) {
69 strcpy(&field[i+1], &field[i+2]);
78 * Split an RFC822-style address into userid, host, and full name
79 * (Originally from citmail.c, and unchanged so far)
82 void process_rfc822_addr(char *rfc822, char *user, char *node, char *name)
87 strcpy(node, config.c_fqdn);
90 /* extract full name - first, it's From minus <userid> */
92 for (a = 0; a < strlen(name); ++a) {
95 strcpy(&name[a], &name[a + 1]);
96 } while ((strlen(name) > 0) && (name[a] != '>'));
97 strcpy(&name[a], &name[a + 1]);
100 /* strip anything to the left of a bang */
101 while ((strlen(name) > 0) && (haschar(name, '!') > 0))
102 strcpy(name, &name[1]);
104 /* and anything to the right of a @ or % */
105 for (a = 0; a < strlen(name); ++a) {
112 /* but if there are parentheses, that changes the rules... */
113 if ((haschar(rfc822, '(') == 1) && (haschar(rfc822, ')') == 1)) {
114 strcpy(name, rfc822);
115 while ((strlen(name) > 0) && (name[0] != '(')) {
116 strcpy(&name[0], &name[1]);
118 strcpy(&name[0], &name[1]);
119 for (a = 0; a < strlen(name); ++a) {
120 if (name[a] == ')') {
125 /* but if there are a set of quotes, that supersedes everything */
126 if (haschar(rfc822, 34) == 2) {
127 strcpy(name, rfc822);
128 while ((strlen(name) > 0) && (name[0] != 34)) {
129 strcpy(&name[0], &name[1]);
131 strcpy(&name[0], &name[1]);
132 for (a = 0; a < strlen(name); ++a)
136 /* extract user id */
137 strcpy(user, rfc822);
139 /* first get rid of anything in parens */
140 for (a = 0; a < strlen(user); ++a)
141 if (user[a] == '(') {
143 strcpy(&user[a], &user[a + 1]);
144 } while ((strlen(user) > 0) && (user[a] != ')'));
145 strcpy(&user[a], &user[a + 1]);
147 /* if there's a set of angle brackets, strip it down to that */
148 if ((haschar(user, '<') == 1) && (haschar(user, '>') == 1)) {
149 while ((strlen(user) > 0) && (user[0] != '<')) {
150 strcpy(&user[0], &user[1]);
152 strcpy(&user[0], &user[1]);
153 for (a = 0; a < strlen(user); ++a)
157 /* strip anything to the left of a bang */
158 while ((strlen(user) > 0) && (haschar(user, '!') > 0))
159 strcpy(user, &user[1]);
161 /* and anything to the right of a @ or % */
162 for (a = 0; a < strlen(user); ++a) {
170 /* extract node name */
171 strcpy(node, rfc822);
173 /* first get rid of anything in parens */
174 for (a = 0; a < strlen(node); ++a)
175 if (node[a] == '(') {
177 strcpy(&node[a], &node[a + 1]);
178 } while ((strlen(node) > 0) && (node[a] != ')'));
179 strcpy(&node[a], &node[a + 1]);
181 /* if there's a set of angle brackets, strip it down to that */
182 if ((haschar(node, '<') == 1) && (haschar(node, '>') == 1)) {
183 while ((strlen(node) > 0) && (node[0] != '<')) {
184 strcpy(&node[0], &node[1]);
186 strcpy(&node[0], &node[1]);
187 for (a = 0; a < strlen(node); ++a)
191 /* strip anything to the left of a @ */
192 while ((strlen(node) > 0) && (haschar(node, '@') > 0))
193 strcpy(node, &node[1]);
195 /* strip anything to the left of a % */
196 while ((strlen(node) > 0) && (haschar(node, '%') > 0))
197 strcpy(node, &node[1]);
199 /* reduce multiple system bang paths to node!user */
200 while ((strlen(node) > 0) && (haschar(node, '!') > 1))
201 strcpy(node, &node[1]);
203 /* now get rid of the user portion of a node!user string */
204 for (a = 0; a < strlen(node); ++a)
208 /* strip leading and trailing spaces in all strings */
217 * Back end for convert_internet_address()
218 * (Compares an internet name [buffer1] and stores in [buffer2] if found)
220 void try_name(struct usersupp *us) {
222 if (!strncasecmp(CC->buffer1, "cit", 3))
223 if (atol(&CC->buffer1[3]) == us->usernum)
224 strcpy(CC->buffer2, us->fullname);
226 if (!collapsed_strcmp(CC->buffer1, us->fullname))
227 strcpy(CC->buffer2, us->fullname);
229 if (us->uid != BBSUID)
230 if (!strcasecmp(CC->buffer1, getpwuid(us->uid)->pw_name))
231 strcpy(CC->buffer2, us->fullname);
237 * Convert an Internet email address to a Citadel user/host combination
239 int convert_internet_address(char *destuser, char *desthost, char *source)
246 process_rfc822_addr(source, user, node, name);
248 /* Map the FQDN to a Citadel node name
249 * FIX ... we have to check for all known aliases for the local
250 * system, and also handle gateway domains, etc. etc.
252 if (!strcasecmp(node, config.c_fqdn)) {
253 strcpy(node, config.c_nodename);
256 /* Return an error condition if the node is not known.
257 * FIX ... make this work for non-local systems
259 if (strcasecmp(node, config.c_nodename)) {
260 return(rfc822_address_invalid);
263 /* Now try to resolve the name
264 * FIX ... do the multiple-addresses thing
266 if (!strcasecmp(node, config.c_nodename)) {
267 strcpy(destuser, user);
268 strcpy(desthost, config.c_nodename);
269 strcpy(CC->buffer1, user);
270 strcpy(CC->buffer2, "");
271 ForEachUser(try_name);
272 if (strlen(CC->buffer2) == 0) return(rfc822_no_such_user);
273 strcpy(destuser, CC->buffer2);
274 return(rfc822_address_locally_validated);
277 return(rfc822_address_invalid); /* unknown error */
284 * convert_field() is a helper function for convert_internet_message().
285 * Given start/end positions for an rfc822 field, it converts it to a Citadel
286 * field if it wants to, and unfolds it if necessary.
288 * Returns 1 if the field was converted and inserted into the Citadel message
289 * structure, implying that the source field should be removed from the
292 int convert_field(struct CtdlMessage *msg, int beg, int end) {
299 char *user, *node, *name;
301 rfc822 = msg->cm_fields['M']; /* M field contains rfc822 text */
302 for (i = end; i >= beg; --i) {
303 if (rfc822[i] == ':') colonpos = i;
306 if (colonpos < 0) return(0); /* no colon? not a valid header line */
308 value = mallok((end - beg) + 2);
309 safestrncpy(value, &rfc822[beg], (end-beg)+1);
311 key[colonpos - beg] = 0;
312 value = &key[(colonpos - beg) + 1];
313 unfold_rfc822_field(value);
315 /* Here's the big rfc822-to-citadel loop. */
317 if (!strcasecmp(key, "Date")) {
318 lprintf(9, "converting date <%s>\n", value);
319 sprintf(buf, "%ld", parsedate(value) );
320 lprintf(9, "parsed value is <%s>\n", buf);
321 if (msg->cm_fields['T'] != NULL)
322 msg->cm_fields['T'] = strdoop(buf);
326 else if (!strcasecmp(key, "From")) {
327 user = mallok(strlen(value));
328 node = mallok(strlen(value));
329 name = mallok(strlen(value));
330 process_rfc822_addr(value, user, node, name);
331 if (msg->cm_fields['A'] != NULL)
332 msg->cm_fields['A'] = strdoop(user);
333 if (msg->cm_fields['N'] != NULL)
334 msg->cm_fields['N'] = strdoop(node);
335 if (msg->cm_fields['H'] != NULL)
336 msg->cm_fields['H'] = strdoop(name);
343 else if (!strcasecmp(key, "Subject")) {
344 if (msg->cm_fields['U'] != NULL)
345 msg->cm_fields['U'] = strdoop(key);
349 /* Clean up and move on. */
350 lprintf(9, "Field: key=<%s> value=<%s> processed=%d\n",
351 key, value, processed);
352 phree(key); /* Don't free 'value', it's actually the same buffer */
358 * Convert an RFC822 message (headers + body) to a CtdlMessage structure.
360 struct CtdlMessage *convert_internet_message(char *rfc822) {
362 struct CtdlMessage *msg;
369 msg = mallok(sizeof(struct CtdlMessage));
370 if (msg == NULL) return msg;
372 memset(msg, 0, sizeof(struct CtdlMessage));
373 msg->cm_magic = CTDLMESSAGE_MAGIC; /* self check */
374 msg->cm_anon_type = 0; /* never anonymous */
375 msg->cm_format_type = 4; /* always MIME */
376 msg->cm_fields['M'] = rfc822;
378 /* FIX there's plenty to do here. */
379 msglen = strlen(rfc822);
385 /* Locate beginning and end of field, keeping in mind that
386 * some fields might be multiline
390 for (pos=beg; ((pos<=strlen(rfc822))&&(end<0)); ++pos) {
391 if ((rfc822[pos]=='\n')
392 && (!isspace(rfc822[pos+1]))) {
395 if ( (rfc822[pos]=='\n') /* done w. headers? */
396 && ( (rfc822[pos+1]=='\n')
397 ||(rfc822[pos+1]=='\r'))) {
404 /* At this point we have a field. Are we interested in it? */
405 converted = convert_field(msg, beg, end);
407 strcpy(&rfc822[beg], &rfc822[pos]);
411 /* If we've hit the end of the message, bail out */
412 if (pos > strlen(rfc822)) done = 1;
416 /* Follow-up sanity checks... */
418 /* If there's no timestamp on this message, set it to now. */
419 if (msg->cm_fields['T'] == NULL) {
420 sprintf(buf, "%ld", time(NULL));
421 msg->cm_fields['T'] = strdoop(buf);