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))
62 * Here's where our SMTP session begins its happy day.
64 void smtp_greeting(void) {
66 strcpy(CC->cs_clientname, "SMTP session");
68 CC->cs_flags |= CS_STEALTH;
69 CtdlAllocUserData(SYM_SMTP, sizeof(struct citsmtp));
70 CtdlAllocUserData(SYM_SMTP_RECP, 256);
71 sprintf(SMTP_RECP, "%s", "");
73 cprintf("220 Welcome to the Citadel/UX ESMTP server at %s\n",
79 * Implement HELO and EHLO commands.
81 void smtp_hello(char *argbuf, int is_esmtp) {
84 cprintf("250 Greetings and joyous salutations.\n");
87 cprintf("250-Greetings and joyous salutations.\n");
88 cprintf("250-HELP\n");
89 cprintf("250-SIZE %ld\n", config.c_maxmsglen);
90 cprintf("250 AUTH=LOGIN\n");
96 * Implement HELP command.
98 void smtp_help(void) {
99 cprintf("214-Here's the frequency, Kenneth:\n");
100 cprintf("214- DATA\n");
101 cprintf("214- EHLO\n");
102 cprintf("214- EXPN\n");
103 cprintf("214- HELO\n");
104 cprintf("214- HELP\n");
105 cprintf("214- MAIL\n");
106 cprintf("214- NOOP\n");
107 cprintf("214- QUIT\n");
108 cprintf("214- RCPT\n");
109 cprintf("214- RSET\n");
110 cprintf("214- VRFY\n");
111 cprintf("214 I could tell you more, but then I'd have to kill you.\n");
118 void smtp_get_user(char *argbuf) {
122 decode_base64(username, argbuf);
123 lprintf(9, "Trying <%s>\n", username);
124 if (CtdlLoginExistingUser(username) == login_ok) {
125 encode_base64(buf, "Password:");
126 cprintf("334 %s\n", buf);
127 SMTP->command_state = smtp_password;
130 cprintf("500 No such user.\n");
131 SMTP->command_state = smtp_command;
139 void smtp_get_pass(char *argbuf) {
142 decode_base64(password, argbuf);
143 lprintf(9, "Trying <%s>\n", password);
144 if (CtdlTryPassword(password) == pass_ok) {
145 cprintf("235 Authentication successful.\n");
146 lprintf(9, "SMTP auth login successful\n");
147 CC->internal_pgm = 0;
148 CC->cs_flags &= ~CS_STEALTH;
151 cprintf("500 Authentication failed.\n");
153 SMTP->command_state = smtp_command;
160 void smtp_auth(char *argbuf) {
163 if (strncasecmp(argbuf, "login", 5) ) {
164 cprintf("550 We only support LOGIN authentication.\n");
168 if (strlen(argbuf) >= 7) {
169 smtp_get_user(&argbuf[6]);
173 encode_base64(buf, "Username:");
174 cprintf("334 %s\n", buf);
175 SMTP->command_state = smtp_user;
181 * Back end for smtp_vrfy() command
183 void smtp_vrfy_backend(struct usersupp *us) {
185 if (!fuzzy_match(us, SMTP->vrfy_match)) {
187 memcpy(&SMTP->vrfy_buffer, us, sizeof(struct usersupp));
193 * Implements the VRFY (verify user name) command.
194 * Performs fuzzy match on full user names.
196 void smtp_vrfy(char *argbuf) {
197 SMTP->vrfy_count = 0;
198 strcpy(SMTP->vrfy_match, argbuf);
199 ForEachUser(smtp_vrfy_backend);
201 if (SMTP->vrfy_count < 1) {
202 cprintf("550 String does not match anything.\n");
204 else if (SMTP->vrfy_count == 1) {
205 cprintf("250 %s <cit%ld@%s>\n",
206 SMTP->vrfy_buffer.fullname,
207 SMTP->vrfy_buffer.usernum,
210 else if (SMTP->vrfy_count > 1) {
211 cprintf("553 Request ambiguous: %d users matched.\n",
220 * Back end for smtp_expn() command
222 void smtp_expn_backend(struct usersupp *us) {
224 if (!fuzzy_match(us, SMTP->vrfy_match)) {
226 if (SMTP->vrfy_count >= 1) {
227 cprintf("250-%s <cit%ld@%s>\n",
228 SMTP->vrfy_buffer.fullname,
229 SMTP->vrfy_buffer.usernum,
234 memcpy(&SMTP->vrfy_buffer, us, sizeof(struct usersupp));
240 * Implements the EXPN (expand user name) command.
241 * Performs fuzzy match on full user names.
243 void smtp_expn(char *argbuf) {
244 SMTP->vrfy_count = 0;
245 strcpy(SMTP->vrfy_match, argbuf);
246 ForEachUser(smtp_expn_backend);
248 if (SMTP->vrfy_count < 1) {
249 cprintf("550 String does not match anything.\n");
251 else if (SMTP->vrfy_count >= 1) {
252 cprintf("250 %s <cit%ld@%s>\n",
253 SMTP->vrfy_buffer.fullname,
254 SMTP->vrfy_buffer.usernum,
261 * Implements the RSET (reset state) command.
262 * Currently this just zeroes out the state buffer. If pointers to data
263 * allocated with mallok() are ever placed in the state buffer, we have to
264 * be sure to phree() them first!
266 void smtp_rset(void) {
267 memset(SMTP, 0, sizeof(struct citsmtp));
268 if (CC->logged_in) logout(CC);
269 cprintf("250 Zap!\n");
275 * Implements the "MAIL From:" command
277 void smtp_mail(char *argbuf) {
282 if (strlen(SMTP->from) != 0) {
283 cprintf("503 Only one sender permitted\n");
287 if (strncasecmp(argbuf, "From:", 5)) {
288 cprintf("501 Syntax error\n");
292 strcpy(SMTP->from, &argbuf[5]);
295 if (strlen(SMTP->from) == 0) {
296 cprintf("501 Empty sender name is not permitted\n");
301 /* If this SMTP connection is from a logged-in user, make sure that
302 * the user only sends email from his/her own address.
305 cvt = convert_internet_address(user, node, SMTP->from);
306 lprintf(9, "cvt=%d, citaddr=<%s@%s>\n", cvt, user, node);
307 if ( (cvt != 0) || (strcasecmp(user, CC->usersupp.fullname))) {
308 cprintf("550 <%s> is not your address.\n", SMTP->from);
309 strcpy(SMTP->from, "");
314 /* Otherwise, make sure outsiders aren't trying to forge mail from
318 cvt = convert_internet_address(user, node, SMTP->from);
319 lprintf(9, "cvt=%d, citaddr=<%s@%s>\n", cvt, user, node);
320 if (!strcasecmp(node, config.c_nodename)) { /* FIX use fcn */
321 cprintf("550 You must log in to send mail from %s\n",
323 strcpy(SMTP->from, "");
328 cprintf("250 Sender ok. Groovy.\n");
334 * Implements the "RCPT To:" command
336 void smtp_rcpt(char *argbuf) {
341 int is_spam = 0; /* FIX implement anti-spamming */
343 if (strlen(SMTP->from) == 0) {
344 cprintf("503 MAIL first, then RCPT. Duh.\n");
348 if (strncasecmp(argbuf, "To:", 3)) {
349 cprintf("501 Syntax error\n");
353 strcpy(recp, &argbuf[3]);
357 cvt = convert_internet_address(user, node, recp);
358 sprintf(recp, "%s@%s", user, node);
362 case rfc822_address_locally_validated:
363 cprintf("250 %s is a valid recipient.\n", user);
364 ++SMTP->number_of_recipients;
365 CtdlReallocUserData(SYM_SMTP_RECP,
366 strlen(SMTP_RECP) + 1024 );
367 strcat(SMTP_RECP, "local|");
368 strcat(SMTP_RECP, user);
369 strcat(SMTP_RECP, "|0\n");
372 case rfc822_no_such_user:
373 cprintf("550 %s: no such user\n", recp);
376 case rfc822_address_invalid:
378 cprintf("551 Away with thee, spammer!\n");
381 cprintf("250 Remote recipient %s ok\n", recp);
382 ++SMTP->number_of_recipients;
383 CtdlReallocUserData(SYM_SMTP_RECP,
384 strlen(SMTP_RECP) + 1024 );
385 strcat(SMTP_RECP, "remote|");
386 strcat(SMTP_RECP, recp);
387 strcat(SMTP_RECP, "|0\n");
393 cprintf("599 Unknown error\n");
401 * Back end for smtp_data() ... this does the actual delivery of the message
402 * Returns 0 on success, nonzero on failure
404 int smtp_message_delivery(struct CtdlMessage *msg) {
411 int successful_saves = 0; /* number of successful local saves */
412 int failed_saves = 0; /* number of failed deliveries */
413 int remote_spools = 0; /* number of copies to send out */
416 struct usersupp userbuf;
417 char *instr; /* Remote delivery instructions */
418 struct CtdlMessage *imsg;
420 lprintf(9, "smtp_message_delivery() called\n");
422 /* Fill in 'from' fields with envelope information if missing */
423 process_rfc822_addr(SMTP->from, user, node, name);
424 if (msg->cm_fields['A']==NULL) msg->cm_fields['A'] = strdoop(user);
425 if (msg->cm_fields['N']==NULL) msg->cm_fields['N'] = strdoop(node);
426 if (msg->cm_fields['H']==NULL) msg->cm_fields['H'] = strdoop(name);
428 /* Save the message in the queue */
429 msgid = CtdlSaveMsg(msg,
436 instr = mallok(1024);
437 sprintf(instr, "Content-type: %s\n\nmsgid|%ld\n",
440 for (i=0; i<SMTP->number_of_recipients; ++i) {
441 extract_token(buf, SMTP_RECP, i, '\n');
442 extract(dtype, buf, 0);
444 /* Stuff local mailboxes */
445 if (!strcasecmp(dtype, "local")) {
446 extract(user, buf, 1);
447 if (getuser(&userbuf, user) == 0) {
448 MailboxName(room, &userbuf, MAILROOM);
449 CtdlSaveMsgPointerInRoom(room, msgid, 0);
457 /* Remote delivery */
458 if (!strcasecmp(dtype, "remote")) {
459 extract(user, buf, 1);
460 instr = reallok(instr, strlen(instr) + 1024);
461 sprintf(&instr[strlen(instr)],
469 /* If there are remote spools to be done, save the instructions */
470 if (remote_spools > 0) {
471 imsg = mallok(sizeof(struct CtdlMessage));
472 memset(imsg, 0, sizeof(struct CtdlMessage));
473 imsg->cm_magic = CTDLMESSAGE_MAGIC;
474 imsg->cm_anon_type = MES_NORMAL;
475 imsg->cm_format_type = FMT_RFC822;
476 imsg->cm_fields['M'] = instr;
477 CtdlSaveMsg(imsg, "", SMTP_SPOOLOUT_ROOM, MES_LOCAL, 1);
478 CtdlFreeMessage(imsg);
481 /* If there are no remote spools, delete the message */
483 phree(instr); /* only needed here, because CtdlSaveMsg()
484 * would free this buffer otherwise */
485 CtdlDeleteMessages(SMTP_SPOOLOUT_ROOM, msgid, NULL);
488 return(failed_saves);
494 * Implements the DATA command
496 void smtp_data(void) {
498 struct CtdlMessage *msg;
502 if (strlen(SMTP->from) == 0) {
503 cprintf("503 Need MAIL command first.\n");
507 if (SMTP->number_of_recipients < 1) {
508 cprintf("503 Need RCPT command first.\n");
512 cprintf("354 Transmit message now; terminate with '.' by itself\n");
514 generate_rfc822_datestamp(nowstamp, time(NULL));
516 if (body != NULL) sprintf(body,
517 "Received: from %s\n"
524 body = CtdlReadMessageBody(".", config.c_maxmsglen, body);
526 cprintf("550 Unable to save message text: internal error.\n");
530 lprintf(9, "Converting message...\n");
531 msg = convert_internet_message(body);
533 /* If the user is locally authenticated, FORCE the From: header to
534 * show up as the real sender
537 if (msg->cm_fields['A'] != NULL) phree(msg->cm_fields['A']);
538 if (msg->cm_fields['N'] != NULL) phree(msg->cm_fields['N']);
539 if (msg->cm_fields['H'] != NULL) phree(msg->cm_fields['H']);
540 msg->cm_fields['A'] = strdoop(CC->usersupp.fullname);
541 msg->cm_fields['N'] = strdoop(config.c_nodename);
542 msg->cm_fields['H'] = strdoop(config.c_humannode);
545 retval = smtp_message_delivery(msg);
546 CtdlFreeMessage(msg);
549 cprintf("250 Message accepted for delivery.\n");
552 cprintf("550 Internal delivery errors: %d\n", retval);
560 * Main command loop for SMTP sessions.
562 void smtp_command_loop(void) {
566 memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
567 if (client_gets(cmdbuf) < 1) {
568 lprintf(3, "SMTP socket is broken. Ending session.\n");
572 lprintf(5, "citserver[%3d]: %s\n", CC->cs_pid, cmdbuf);
573 while (strlen(cmdbuf) < 5) strcat(cmdbuf, " ");
575 if (SMTP->command_state == smtp_user) {
576 smtp_get_user(cmdbuf);
579 else if (SMTP->command_state == smtp_password) {
580 smtp_get_pass(cmdbuf);
583 else if (!strncasecmp(cmdbuf, "AUTH", 4)) {
584 smtp_auth(&cmdbuf[5]);
587 else if (!strncasecmp(cmdbuf, "DATA", 4)) {
591 else if (!strncasecmp(cmdbuf, "EHLO", 4)) {
592 smtp_hello(&cmdbuf[5], 1);
595 else if (!strncasecmp(cmdbuf, "EXPN", 4)) {
596 smtp_expn(&cmdbuf[5]);
599 else if (!strncasecmp(cmdbuf, "HELO", 4)) {
600 smtp_hello(&cmdbuf[5], 0);
603 else if (!strncasecmp(cmdbuf, "HELP", 4)) {
607 else if (!strncasecmp(cmdbuf, "MAIL", 4)) {
608 smtp_mail(&cmdbuf[5]);
611 else if (!strncasecmp(cmdbuf, "NOOP", 4)) {
612 cprintf("250 This command successfully did nothing.\n");
615 else if (!strncasecmp(cmdbuf, "QUIT", 4)) {
616 cprintf("221 Goodbye...\n");
621 else if (!strncasecmp(cmdbuf, "RCPT", 4)) {
622 smtp_rcpt(&cmdbuf[5]);
625 else if (!strncasecmp(cmdbuf, "RSET", 4)) {
629 else if (!strncasecmp(cmdbuf, "VRFY", 4)) {
630 smtp_vrfy(&cmdbuf[5]);
634 cprintf("502 I'm sorry Dave, I'm afraid I can't do that.\n");
640 char *Dynamic_Module_Init(void)
642 SYM_SMTP = CtdlGetDynamicSymbol();
643 SYM_SMTP_RECP = CtdlGetDynamicSymbol();
644 CtdlRegisterServiceHook(SMTP_PORT,
647 create_room(SMTP_SPOOLOUT_ROOM, 3, "", 0);