11 #include <sys/types.h>
19 #include "sysdep_decls.h"
20 #include "citserver.h"
23 #include "dynloader.h"
30 #include "internet_addressing.h"
34 struct citsmtp { /* Information about the current session */
36 struct usersupp vrfy_buffer;
40 int number_of_recipients;
44 enum { /* Command states for login authentication */
50 enum { /* Delivery modes */
55 #define SMTP ((struct citsmtp *)CtdlGetUserData(SYM_SMTP))
56 #define SMTP_RECP ((char *)CtdlGetUserData(SYM_SMTP_RECP))
63 /*****************************************************************************/
64 /* SMTP SERVER (INBOUND) STUFF */
65 /*****************************************************************************/
69 * Here's where our SMTP session begins its happy day.
71 void smtp_greeting(void) {
73 strcpy(CC->cs_clientname, "SMTP session");
75 CC->cs_flags |= CS_STEALTH;
76 CtdlAllocUserData(SYM_SMTP, sizeof(struct citsmtp));
77 CtdlAllocUserData(SYM_SMTP_RECP, 256);
78 sprintf(SMTP_RECP, "%s", "");
80 cprintf("220 Welcome to the Citadel/UX ESMTP server at %s\r\n",
86 * Implement HELO and EHLO commands.
88 void smtp_hello(char *argbuf, int is_esmtp) {
91 cprintf("250 Greetings and joyous salutations.\r\n");
94 cprintf("250-Greetings and joyous salutations.\r\n");
95 cprintf("250-HELP\r\n");
96 cprintf("250-SIZE %ld\r\n", config.c_maxmsglen);
97 cprintf("250 AUTH=LOGIN\r\n");
103 * Implement HELP command.
105 void smtp_help(void) {
106 cprintf("214-Here's the frequency, Kenneth:\r\n");
107 cprintf("214- DATA\r\n");
108 cprintf("214- EHLO\r\n");
109 cprintf("214- EXPN\r\n");
110 cprintf("214- HELO\r\n");
111 cprintf("214- HELP\r\n");
112 cprintf("214- MAIL\r\n");
113 cprintf("214- NOOP\r\n");
114 cprintf("214- QUIT\r\n");
115 cprintf("214- RCPT\r\n");
116 cprintf("214- RSET\r\n");
117 cprintf("214- VRFY\r\n");
118 cprintf("214 I could tell you more, but then I'd have to kill you.\r\n");
125 void smtp_get_user(char *argbuf) {
129 decode_base64(username, argbuf);
130 lprintf(9, "Trying <%s>\n", username);
131 if (CtdlLoginExistingUser(username) == login_ok) {
132 encode_base64(buf, "Password:");
133 cprintf("334 %s\r\n", buf);
134 SMTP->command_state = smtp_password;
137 cprintf("500 No such user.\r\n");
138 SMTP->command_state = smtp_command;
146 void smtp_get_pass(char *argbuf) {
149 decode_base64(password, argbuf);
150 lprintf(9, "Trying <%s>\n", password);
151 if (CtdlTryPassword(password) == pass_ok) {
152 cprintf("235 Authentication successful.\r\n");
153 lprintf(9, "SMTP authenticated login successful\n");
154 CC->internal_pgm = 0;
155 CC->cs_flags &= ~CS_STEALTH;
158 cprintf("500 Authentication failed.\r\n");
160 SMTP->command_state = smtp_command;
167 void smtp_auth(char *argbuf) {
170 if (strncasecmp(argbuf, "login", 5) ) {
171 cprintf("550 We only support LOGIN authentication.\r\n");
175 if (strlen(argbuf) >= 7) {
176 smtp_get_user(&argbuf[6]);
180 encode_base64(buf, "Username:");
181 cprintf("334 %s\r\n", buf);
182 SMTP->command_state = smtp_user;
188 * Back end for smtp_vrfy() command
190 void smtp_vrfy_backend(struct usersupp *us, void *data) {
192 if (!fuzzy_match(us, SMTP->vrfy_match)) {
194 memcpy(&SMTP->vrfy_buffer, us, sizeof(struct usersupp));
200 * Implements the VRFY (verify user name) command.
201 * Performs fuzzy match on full user names.
203 void smtp_vrfy(char *argbuf) {
204 SMTP->vrfy_count = 0;
205 strcpy(SMTP->vrfy_match, argbuf);
206 ForEachUser(smtp_vrfy_backend, NULL);
208 if (SMTP->vrfy_count < 1) {
209 cprintf("550 String does not match anything.\r\n");
211 else if (SMTP->vrfy_count == 1) {
212 cprintf("250 %s <cit%ld@%s>\r\n",
213 SMTP->vrfy_buffer.fullname,
214 SMTP->vrfy_buffer.usernum,
217 else if (SMTP->vrfy_count > 1) {
218 cprintf("553 Request ambiguous: %d users matched.\r\n",
227 * Back end for smtp_expn() command
229 void smtp_expn_backend(struct usersupp *us, void *data) {
231 if (!fuzzy_match(us, SMTP->vrfy_match)) {
233 if (SMTP->vrfy_count >= 1) {
234 cprintf("250-%s <cit%ld@%s>\r\n",
235 SMTP->vrfy_buffer.fullname,
236 SMTP->vrfy_buffer.usernum,
241 memcpy(&SMTP->vrfy_buffer, us, sizeof(struct usersupp));
247 * Implements the EXPN (expand user name) command.
248 * Performs fuzzy match on full user names.
250 void smtp_expn(char *argbuf) {
251 SMTP->vrfy_count = 0;
252 strcpy(SMTP->vrfy_match, argbuf);
253 ForEachUser(smtp_expn_backend, NULL);
255 if (SMTP->vrfy_count < 1) {
256 cprintf("550 String does not match anything.\r\n");
258 else if (SMTP->vrfy_count >= 1) {
259 cprintf("250 %s <cit%ld@%s>\r\n",
260 SMTP->vrfy_buffer.fullname,
261 SMTP->vrfy_buffer.usernum,
268 * Implements the RSET (reset state) command.
269 * Currently this just zeroes out the state buffer. If pointers to data
270 * allocated with mallok() are ever placed in the state buffer, we have to
271 * be sure to phree() them first!
273 void smtp_rset(void) {
274 memset(SMTP, 0, sizeof(struct citsmtp));
275 if (CC->logged_in) logout(CC);
276 cprintf("250 Zap!\r\n");
282 * Implements the "MAIL From:" command
284 void smtp_mail(char *argbuf) {
289 if (strlen(SMTP->from) != 0) {
290 cprintf("503 Only one sender permitted\r\n");
294 if (strncasecmp(argbuf, "From:", 5)) {
295 cprintf("501 Syntax error\r\n");
299 strcpy(SMTP->from, &argbuf[5]);
302 if (strlen(SMTP->from) == 0) {
303 cprintf("501 Empty sender name is not permitted\r\n");
308 /* If this SMTP connection is from a logged-in user, make sure that
309 * the user only sends email from his/her own address.
312 cvt = convert_internet_address(user, node, SMTP->from);
313 lprintf(9, "cvt=%d, citaddr=<%s@%s>\n", cvt, user, node);
314 if ( (cvt != 0) || (strcasecmp(user, CC->usersupp.fullname))) {
315 cprintf("550 <%s> is not your address.\r\n", SMTP->from);
316 strcpy(SMTP->from, "");
321 /* Otherwise, make sure outsiders aren't trying to forge mail from
325 cvt = convert_internet_address(user, node, SMTP->from);
326 lprintf(9, "cvt=%d, citaddr=<%s@%s>\n", cvt, user, node);
327 if (!strcasecmp(node, config.c_nodename)) { /* FIX use fcn */
328 cprintf("550 You must log in to send mail from %s\r\n",
330 strcpy(SMTP->from, "");
335 cprintf("250 Sender ok. Groovy.\r\n");
341 * Implements the "RCPT To:" command
343 void smtp_rcpt(char *argbuf) {
348 int is_spam = 0; /* FIX implement anti-spamming */
350 if (strlen(SMTP->from) == 0) {
351 cprintf("503 MAIL first, then RCPT. Duh.\r\n");
355 if (strncasecmp(argbuf, "To:", 3)) {
356 cprintf("501 Syntax error\r\n");
360 strcpy(recp, &argbuf[3]);
364 cvt = convert_internet_address(user, node, recp);
365 sprintf(recp, "%s@%s", user, node);
369 case rfc822_address_locally_validated:
370 cprintf("250 %s is a valid recipient.\r\n", user);
371 ++SMTP->number_of_recipients;
372 CtdlReallocUserData(SYM_SMTP_RECP,
373 strlen(SMTP_RECP) + 1024 );
374 strcat(SMTP_RECP, "local|");
375 strcat(SMTP_RECP, user);
376 strcat(SMTP_RECP, "|0\n");
379 case rfc822_room_delivery:
380 cprintf("250 Delivering to room '%s'\r\n", user);
381 ++SMTP->number_of_recipients;
382 CtdlReallocUserData(SYM_SMTP_RECP,
383 strlen(SMTP_RECP) + 1024 );
384 strcat(SMTP_RECP, "room|");
385 strcat(SMTP_RECP, user);
386 strcat(SMTP_RECP, "|0|\n");
389 case rfc822_no_such_user:
390 cprintf("550 %s: no such user\r\n", recp);
393 case rfc822_address_invalid:
395 cprintf("551 Away with thee, spammer!\r\n");
398 cprintf("250 Remote recipient %s ok\r\n", recp);
399 ++SMTP->number_of_recipients;
400 CtdlReallocUserData(SYM_SMTP_RECP,
401 strlen(SMTP_RECP) + 1024 );
402 strcat(SMTP_RECP, "remote|");
403 strcat(SMTP_RECP, recp);
404 strcat(SMTP_RECP, "|0|\n");
410 cprintf("599 Unknown error\r\n");
418 * Back end for smtp_data() ... this does the actual delivery of the message
419 * Returns 0 on success, nonzero on failure
421 int smtp_message_delivery(struct CtdlMessage *msg) {
428 int successful_saves = 0; /* number of successful local saves */
429 int failed_saves = 0; /* number of failed deliveries */
430 int remote_spools = 0; /* number of copies to send out */
433 struct usersupp userbuf;
434 char *instr; /* Remote delivery instructions */
435 struct CtdlMessage *imsg;
437 lprintf(9, "smtp_message_delivery() called\n");
439 /* Fill in 'from' fields with envelope information if missing */
440 process_rfc822_addr(SMTP->from, user, node, name);
441 if (msg->cm_fields['A']==NULL) msg->cm_fields['A'] = strdoop(user);
442 if (msg->cm_fields['N']==NULL) msg->cm_fields['N'] = strdoop(node);
443 if (msg->cm_fields['H']==NULL) msg->cm_fields['H'] = strdoop(name);
445 /* Save the message in the queue */
446 msgid = CtdlSaveMsg(msg,
453 instr = mallok(1024);
454 sprintf(instr, "Content-type: %s\n\nmsgid|%ld\nsubmitted|%ld\n",
455 SPOOLMIME, msgid, time(NULL) );
457 for (i=0; i<SMTP->number_of_recipients; ++i) {
458 extract_token(buf, SMTP_RECP, i, '\n');
459 extract(dtype, buf, 0);
461 /* Stuff local mailboxes */
462 if (!strcasecmp(dtype, "local")) {
463 extract(user, buf, 1);
464 if (getuser(&userbuf, user) == 0) {
465 MailboxName(room, &userbuf, MAILROOM);
466 CtdlSaveMsgPointerInRoom(room, msgid, 0);
474 /* Delivery to local non-mailbox rooms */
475 if (!strcasecmp(dtype, "room")) {
476 extract(room, buf, 1);
477 CtdlSaveMsgPointerInRoom(room, msgid, 0);
481 /* Remote delivery */
482 if (!strcasecmp(dtype, "remote")) {
483 extract(user, buf, 1);
484 instr = reallok(instr, strlen(instr) + 1024);
485 sprintf(&instr[strlen(instr)],
493 /* If there are remote spools to be done, save the instructions */
494 if (remote_spools > 0) {
495 imsg = mallok(sizeof(struct CtdlMessage));
496 memset(imsg, 0, sizeof(struct CtdlMessage));
497 imsg->cm_magic = CTDLMESSAGE_MAGIC;
498 imsg->cm_anon_type = MES_NORMAL;
499 imsg->cm_format_type = FMT_RFC822;
500 imsg->cm_fields['M'] = instr;
501 CtdlSaveMsg(imsg, "", SMTP_SPOOLOUT_ROOM, MES_LOCAL, 1);
502 CtdlFreeMessage(imsg);
505 /* If there are no remote spools, delete the message */
507 phree(instr); /* only needed here, because CtdlSaveMsg()
508 * would free this buffer otherwise */
509 CtdlDeleteMessages(SMTP_SPOOLOUT_ROOM, msgid, NULL);
512 return(failed_saves);
518 * Implements the DATA command
520 void smtp_data(void) {
522 struct CtdlMessage *msg;
526 if (strlen(SMTP->from) == 0) {
527 cprintf("503 Need MAIL command first.\r\n");
531 if (SMTP->number_of_recipients < 1) {
532 cprintf("503 Need RCPT command first.\r\n");
536 cprintf("354 Transmit message now; terminate with '.' by itself\r\n");
538 generate_rfc822_datestamp(nowstamp, time(NULL));
540 if (body != NULL) sprintf(body,
541 "Received: from %s\n"
548 body = CtdlReadMessageBody(".", config.c_maxmsglen, body);
550 cprintf("550 Unable to save message text: internal error.\r\n");
554 lprintf(9, "Converting message...\n");
555 msg = convert_internet_message(body);
557 /* If the user is locally authenticated, FORCE the From: header to
558 * show up as the real sender
561 if (msg->cm_fields['A'] != NULL) phree(msg->cm_fields['A']);
562 if (msg->cm_fields['N'] != NULL) phree(msg->cm_fields['N']);
563 if (msg->cm_fields['H'] != NULL) phree(msg->cm_fields['H']);
564 msg->cm_fields['A'] = strdoop(CC->usersupp.fullname);
565 msg->cm_fields['N'] = strdoop(config.c_nodename);
566 msg->cm_fields['H'] = strdoop(config.c_humannode);
569 retval = smtp_message_delivery(msg);
570 CtdlFreeMessage(msg);
573 cprintf("250 Message accepted for delivery.\r\n");
576 cprintf("550 Internal delivery errors: %d\r\n", retval);
584 * Main command loop for SMTP sessions.
586 void smtp_command_loop(void) {
590 memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
591 if (client_gets(cmdbuf) < 1) {
592 lprintf(3, "SMTP socket is broken. Ending session.\n");
596 lprintf(5, "citserver[%3d]: %s\n", CC->cs_pid, cmdbuf);
597 while (strlen(cmdbuf) < 5) strcat(cmdbuf, " ");
599 if (SMTP->command_state == smtp_user) {
600 smtp_get_user(cmdbuf);
603 else if (SMTP->command_state == smtp_password) {
604 smtp_get_pass(cmdbuf);
607 else if (!strncasecmp(cmdbuf, "AUTH", 4)) {
608 smtp_auth(&cmdbuf[5]);
611 else if (!strncasecmp(cmdbuf, "DATA", 4)) {
615 else if (!strncasecmp(cmdbuf, "EHLO", 4)) {
616 smtp_hello(&cmdbuf[5], 1);
619 else if (!strncasecmp(cmdbuf, "EXPN", 4)) {
620 smtp_expn(&cmdbuf[5]);
623 else if (!strncasecmp(cmdbuf, "HELO", 4)) {
624 smtp_hello(&cmdbuf[5], 0);
627 else if (!strncasecmp(cmdbuf, "HELP", 4)) {
631 else if (!strncasecmp(cmdbuf, "MAIL", 4)) {
632 smtp_mail(&cmdbuf[5]);
635 else if (!strncasecmp(cmdbuf, "NOOP", 4)) {
636 cprintf("250 This command successfully did nothing.\r\n");
639 else if (!strncasecmp(cmdbuf, "QUIT", 4)) {
640 cprintf("221 Goodbye...\r\n");
645 else if (!strncasecmp(cmdbuf, "RCPT", 4)) {
646 smtp_rcpt(&cmdbuf[5]);
649 else if (!strncasecmp(cmdbuf, "RSET", 4)) {
653 else if (!strncasecmp(cmdbuf, "VRFY", 4)) {
654 smtp_vrfy(&cmdbuf[5]);
658 cprintf("502 I'm sorry Dave, I'm afraid I can't do that.\r\n");
666 /*****************************************************************************/
667 /* SMTP CLIENT (OUTBOUND PROCESSING) STUFF */
668 /*****************************************************************************/
675 * Called by smtp_do_procmsg() to attempt delivery to one SMTP host
678 void smtp_try(char *key, char *addr, int *status, char *dsn) {
681 strcpy(dsn, "smtp_try() is not finished yet");
689 * Called by smtp_do_queue() to handle an individual message.
691 void smtp_do_procmsg(long msgnum) {
692 struct CtdlMessage *msg;
694 char *results = NULL;
702 long text_msgid = (-1);
704 msg = CtdlFetchMessage(msgnum);
706 lprintf(3, "SMTP: tried %ld but no such message!\n", msgnum);
710 instr = strdoop(msg->cm_fields['M']);
711 CtdlFreeMessage(msg);
713 /* Strip out the headers amd any other non-instruction line */
714 lines = num_tokens(instr, '\n');
715 for (i=0; i<lines; ++i) {
716 extract_token(buf, instr, i, '\n');
717 if (num_tokens(buf, '|') < 2) {
718 lprintf(9, "removing <%s>\n", buf);
719 remove_token(instr, i, '|');
725 /* Learn the message ID */
726 lines = num_tokens(instr, '\n');
727 for (i=0; i<lines; ++i) {
728 extract_token(buf, instr, i, '\n');
729 extract(key, buf, 0);
730 if (!strcasecmp(key, "msgid")) {
731 text_msgid = extract_long(buf, 1);
735 if (text_msgid < 0L) {
736 lprintf(3, "SMTP: no 'msgid' directive found!\n", msgnum);
741 /* Plow through the instructions looking for 'remote' directives and
742 * a status of 0 (no delivery yet attempted) or 3 (transient errors
743 * were experienced and it's time to try again)
745 lines = num_tokens(instr, '\n');
746 for (i=0; i<lines; ++i) {
747 extract_token(buf, instr, i, '\n');
748 extract(key, buf, 0);
749 extract(addr, buf, 1);
750 status = extract_int(buf, 2);
751 extract(dsn, buf, 3);
752 if ( (!strcasecmp(key, "remote"))
753 && ((status==0)||(status==3)) ) {
754 remove_token(instr, i, '\n');
757 lprintf(9, "SMTP: Trying <%s>\n", addr);
758 smtp_try(key, addr, &status, dsn);
760 if (results == NULL) {
761 results = mallok(1024);
762 memset(results, 0, 1024);
765 results = reallok(results,
766 strlen(results) + 1024);
768 sprintf(&results[strlen(results)],
770 key, addr, status, dsn);
775 if (results != NULL) {
776 instr = reallok(instr, strlen(instr) + strlen(results) + 2);
777 strcat(instr, results);
781 /* Delete the instructions and replace with the updated ones */
782 CtdlDeleteMessages(SMTP_SPOOLOUT_ROOM, msgnum, NULL);
783 msg = mallok(sizeof(struct CtdlMessage));
784 memset(msg, 0, sizeof(struct CtdlMessage));
785 msg->cm_magic = CTDLMESSAGE_MAGIC;
786 msg->cm_anon_type = MES_NORMAL;
787 msg->cm_format_type = FMT_RFC822;
788 msg->cm_fields['M'] = instr;
789 CtdlSaveMsg(msg, "", SMTP_SPOOLOUT_ROOM, MES_LOCAL, 1);
790 CtdlFreeMessage(msg);
798 * Run through the queue sending out messages.
800 void smtp_do_queue(void) {
801 lprintf(5, "SMTP: processing outbound queue\n");
803 if (getroom(&CC->quickroom, SMTP_SPOOLOUT_ROOM) != 0) {
804 lprintf(3, "Cannot find room <%s>\n", SMTP_SPOOLOUT_ROOM);
807 CtdlForEachMessage(MSGS_ALL, 0L, SPOOLMIME, NULL, smtp_do_procmsg);
809 lprintf(5, "SMTP: queue run completed\n");
813 /**** FIX temporary hack to run the queue *****/
814 void cmd_qqqq(char *argbuf) {
816 cprintf("%d ok\n", OK);
821 /*****************************************************************************/
822 /* MODULE INITIALIZATION STUFF */
823 /*****************************************************************************/
826 char *Dynamic_Module_Init(void)
828 SYM_SMTP = CtdlGetDynamicSymbol();
829 SYM_SMTP_RECP = CtdlGetDynamicSymbol();
830 CtdlRegisterServiceHook(SMTP_PORT,
834 /**** FIX ... temporary hack to run the queue ******/
835 CtdlRegisterProtoHook(cmd_qqqq, "QQQQ", "run the queue");
837 create_room(SMTP_SPOOLOUT_ROOM, 3, "", 0);