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"
35 * Return 0 if a given string fuzzy-matches a Citadel user account
37 * FIX ... this needs to be updated to match any and all address syntaxes.
39 int fuzzy_match(struct usersupp *us, char *matchstring) {
42 for (a=0; a<strlen(us->fullname); ++a) {
43 if (!strncasecmp(&us->fullname[a],
44 matchstring, strlen(matchstring))) {
55 * Split an RFC822-style address into userid, host, and full name
56 * (Originally from citmail.c, and unchanged so far)
59 void process_rfc822_addr(char *rfc822, char *user, char *node, char *name)
64 strcpy(node, config.c_fqdn);
67 /* extract full name - first, it's From minus <userid> */
69 for (a = 0; a < strlen(name); ++a) {
72 strcpy(&name[a], &name[a + 1]);
73 } while ((strlen(name) > 0) && (name[a] != '>'));
74 strcpy(&name[a], &name[a + 1]);
77 /* strip anything to the left of a bang */
78 while ((strlen(name) > 0) && (haschar(name, '!') > 0))
79 strcpy(name, &name[1]);
81 /* and anything to the right of a @ or % */
82 for (a = 0; a < strlen(name); ++a) {
89 /* but if there are parentheses, that changes the rules... */
90 if ((haschar(rfc822, '(') == 1) && (haschar(rfc822, ')') == 1)) {
92 while ((strlen(name) > 0) && (name[0] != '(')) {
93 strcpy(&name[0], &name[1]);
95 strcpy(&name[0], &name[1]);
96 for (a = 0; a < strlen(name); ++a) {
102 /* but if there are a set of quotes, that supersedes everything */
103 if (haschar(rfc822, 34) == 2) {
104 strcpy(name, rfc822);
105 while ((strlen(name) > 0) && (name[0] != 34)) {
106 strcpy(&name[0], &name[1]);
108 strcpy(&name[0], &name[1]);
109 for (a = 0; a < strlen(name); ++a)
113 /* extract user id */
114 strcpy(user, rfc822);
116 /* first get rid of anything in parens */
117 for (a = 0; a < strlen(user); ++a)
118 if (user[a] == '(') {
120 strcpy(&user[a], &user[a + 1]);
121 } while ((strlen(user) > 0) && (user[a] != ')'));
122 strcpy(&user[a], &user[a + 1]);
124 /* if there's a set of angle brackets, strip it down to that */
125 if ((haschar(user, '<') == 1) && (haschar(user, '>') == 1)) {
126 while ((strlen(user) > 0) && (user[0] != '<')) {
127 strcpy(&user[0], &user[1]);
129 strcpy(&user[0], &user[1]);
130 for (a = 0; a < strlen(user); ++a)
134 /* strip anything to the left of a bang */
135 while ((strlen(user) > 0) && (haschar(user, '!') > 0))
136 strcpy(user, &user[1]);
138 /* and anything to the right of a @ or % */
139 for (a = 0; a < strlen(user); ++a) {
147 /* extract node name */
148 strcpy(node, rfc822);
150 /* first get rid of anything in parens */
151 for (a = 0; a < strlen(node); ++a)
152 if (node[a] == '(') {
154 strcpy(&node[a], &node[a + 1]);
155 } while ((strlen(node) > 0) && (node[a] != ')'));
156 strcpy(&node[a], &node[a + 1]);
158 /* if there's a set of angle brackets, strip it down to that */
159 if ((haschar(node, '<') == 1) && (haschar(node, '>') == 1)) {
160 while ((strlen(node) > 0) && (node[0] != '<')) {
161 strcpy(&node[0], &node[1]);
163 strcpy(&node[0], &node[1]);
164 for (a = 0; a < strlen(node); ++a)
168 /* strip anything to the left of a @ */
169 while ((strlen(node) > 0) && (haschar(node, '@') > 0))
170 strcpy(node, &node[1]);
172 /* strip anything to the left of a % */
173 while ((strlen(node) > 0) && (haschar(node, '%') > 0))
174 strcpy(node, &node[1]);
176 /* reduce multiple system bang paths to node!user */
177 while ((strlen(node) > 0) && (haschar(node, '!') > 1))
178 strcpy(node, &node[1]);
180 /* now get rid of the user portion of a node!user string */
181 for (a = 0; a < strlen(node); ++a)
185 /* strip leading and trailing spaces in all strings */
194 * Back end for convert_internet_address()
195 * (Compares an internet name [buffer1] and stores in [buffer2] if found)
197 void try_name(struct usersupp *us) {
199 if (!strncasecmp(CC->buffer1, "cit", 3))
200 if (atol(&CC->buffer1[3]) == us->usernum)
201 strcpy(CC->buffer2, us->fullname);
203 if (!collapsed_strcmp(CC->buffer1, us->fullname))
204 strcpy(CC->buffer2, us->fullname);
206 if (us->uid != BBSUID)
207 if (!strcasecmp(CC->buffer1, getpwuid(us->uid)->pw_name))
208 strcpy(CC->buffer2, us->fullname);
214 * Convert an Internet email address to a Citadel user/host combination
216 int convert_internet_address(char *destuser, char *desthost, char *source)
223 process_rfc822_addr(source, user, node, name);
225 /* Map the FQDN to a Citadel node name
226 * FIX ... we have to check for all known aliases for the local
227 * system, and also handle gateway domains, etc. etc.
229 if (!strcasecmp(node, config.c_fqdn)) {
230 strcpy(node, config.c_nodename);
233 /* Return an error condition if the node is not known.
234 * FIX ... make this work for non-local systems
236 if (strcasecmp(node, config.c_nodename)) {
237 return(rfc822_address_invalid);
240 /* Now try to resolve the name
241 * FIX ... do the multiple-addresses thing
243 if (!strcasecmp(node, config.c_nodename)) {
244 strcpy(destuser, user);
245 strcpy(desthost, config.c_nodename);
246 strcpy(CC->buffer1, user);
247 strcpy(CC->buffer2, "");
248 ForEachUser(try_name);
249 if (strlen(CC->buffer2) == 0) return(rfc822_no_such_user);
250 strcpy(destuser, CC->buffer2);
251 return(rfc822_address_locally_validated);
254 return(rfc822_address_invalid); /* unknown error */
262 * Convert an RFC822 message (headers + body) to a CtdlMessage structure.
264 struct CtdlMessage *convert_internet_message(char *rfc822) {
266 struct CtdlMessage *msg;
274 msg = mallok(sizeof(struct CtdlMessage));
275 if (msg == NULL) return msg;
277 memset(msg, 0, sizeof(struct CtdlMessage));
278 msg->cm_magic = CTDLMESSAGE_MAGIC; /* self check */
279 msg->cm_anon_type = 0; /* never anonymous */
280 msg->cm_format_type = 4; /* always MIME */
281 msg->cm_fields['M'] = rfc822;
283 /* FIX there's plenty to do here. */
284 msglen = strlen(rfc822);
290 /* Locate beginning and end of field, keeping in mind that
291 * some fields might be multiline
295 for (pos=beg; ((pos<=strlen(rfc822))&&(end<0)); ++pos) {
296 if ((rfc822[pos]=='\n')
297 && (!isspace(rfc822[pos+1]))) {
300 if ( (rfc822[pos]=='\n') /* done w. headers? */
301 && ( (rfc822[pos+1]=='\n')
302 ||(rfc822[pos+1]=='\r'))) {
309 /* At this point we have a field. Are we interested in it? */
311 for (i = beg; i <= end; ++i) {
312 if ((rfc822[i] == ':') && ((i-beg)<sizeof(field))) {
313 safestrncpy(field, &rfc822[beg], i-beg+1);
317 "Field: %6d .. %-6d ... <%s>\n",
321 /* If we've hit the end of the message, bail out */
322 if (pos > strlen(rfc822)) done = 1;
326 /* Follow-up sanity check. */
328 /* If there's no timestamp on this message, set it to now. */
329 if (msg->cm_fields['T'] == NULL) {
330 sprintf(buf, "%ld", time(NULL));
331 msg->cm_fields['T'] = strdoop(buf);