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;
45 enum { /* Command states for login authentication */
51 enum { /* Delivery modes */
56 #define SMTP ((struct citsmtp *)CtdlGetUserData(SYM_SMTP))
57 #define SMTP_RECP ((char *)CtdlGetUserData(SYM_SMTP_RECP))
63 * Here's where our SMTP session begins its happy day.
65 void smtp_greeting(void) {
67 strcpy(CC->cs_clientname, "SMTP session");
69 CC->cs_flags |= CS_STEALTH;
70 CtdlAllocUserData(SYM_SMTP, sizeof(struct citsmtp));
71 CtdlAllocUserData(SYM_SMTP_RECP, 256);
72 sprintf(SMTP_RECP, "%s", "");
74 cprintf("220 Welcome to the Citadel/UX ESMTP server at %s\n",
80 * Implement HELO and EHLO commands.
82 void smtp_hello(char *argbuf, int is_esmtp) {
85 cprintf("250 Greetings and joyous salutations.\n");
88 cprintf("250-Greetings and joyous salutations.\n");
89 cprintf("250-HELP\n");
90 cprintf("250-SIZE %ld\n", config.c_maxmsglen);
91 cprintf("250 AUTH=LOGIN\n");
97 * Implement HELP command.
99 void smtp_help(void) {
100 cprintf("214-Here's the frequency, Kenneth:\n");
101 cprintf("214- DATA\n");
102 cprintf("214- EHLO\n");
103 cprintf("214- EXPN\n");
104 cprintf("214- HELO\n");
105 cprintf("214- HELP\n");
106 cprintf("214- MAIL\n");
107 cprintf("214- NOOP\n");
108 cprintf("214- QUIT\n");
109 cprintf("214- RCPT\n");
110 cprintf("214- RSET\n");
111 cprintf("214- VRFY\n");
112 cprintf("214 I could tell you more, but then I'd have to kill you.\n");
119 void smtp_get_user(char *argbuf) {
123 decode_base64(username, argbuf);
124 lprintf(9, "Trying <%s>\n", username);
125 if (CtdlLoginExistingUser(username) == login_ok) {
126 encode_base64(buf, "Password:");
127 cprintf("334 %s\n", buf);
128 SMTP->command_state = smtp_password;
131 cprintf("500 No such user.\n");
132 SMTP->command_state = smtp_command;
140 void smtp_get_pass(char *argbuf) {
143 decode_base64(password, argbuf);
144 lprintf(9, "Trying <%s>\n", password);
145 if (CtdlTryPassword(password) == pass_ok) {
146 cprintf("235 Authentication successful.\n");
147 lprintf(9, "SMTP auth login successful\n");
148 CC->internal_pgm = 0;
149 CC->cs_flags &= ~CS_STEALTH;
152 cprintf("500 Authentication failed.\n");
154 SMTP->command_state = smtp_command;
161 void smtp_auth(char *argbuf) {
164 if (strncasecmp(argbuf, "login", 5) ) {
165 cprintf("550 We only support LOGIN authentication.\n");
169 if (strlen(argbuf) >= 7) {
170 smtp_get_user(&argbuf[6]);
174 encode_base64(buf, "Username:");
175 cprintf("334 %s\n", buf);
176 SMTP->command_state = smtp_user;
182 * Back end for smtp_vrfy() command
184 void smtp_vrfy_backend(struct usersupp *us) {
186 if (!fuzzy_match(us, SMTP->vrfy_match)) {
188 memcpy(&SMTP->vrfy_buffer, us, sizeof(struct usersupp));
194 * Implements the VRFY (verify user name) command.
195 * Performs fuzzy match on full user names.
197 void smtp_vrfy(char *argbuf) {
198 SMTP->vrfy_count = 0;
199 strcpy(SMTP->vrfy_match, argbuf);
200 ForEachUser(smtp_vrfy_backend);
202 if (SMTP->vrfy_count < 1) {
203 cprintf("550 String does not match anything.\n");
205 else if (SMTP->vrfy_count == 1) {
206 cprintf("250 %s <cit%ld@%s>\n",
207 SMTP->vrfy_buffer.fullname,
208 SMTP->vrfy_buffer.usernum,
211 else if (SMTP->vrfy_count > 1) {
212 cprintf("553 Request ambiguous: %d users matched.\n",
221 * Back end for smtp_expn() command
223 void smtp_expn_backend(struct usersupp *us) {
225 if (!fuzzy_match(us, SMTP->vrfy_match)) {
227 if (SMTP->vrfy_count >= 1) {
228 cprintf("250-%s <cit%ld@%s>\n",
229 SMTP->vrfy_buffer.fullname,
230 SMTP->vrfy_buffer.usernum,
235 memcpy(&SMTP->vrfy_buffer, us, sizeof(struct usersupp));
241 * Implements the EXPN (expand user name) command.
242 * Performs fuzzy match on full user names.
244 void smtp_expn(char *argbuf) {
245 SMTP->vrfy_count = 0;
246 strcpy(SMTP->vrfy_match, argbuf);
247 ForEachUser(smtp_expn_backend);
249 if (SMTP->vrfy_count < 1) {
250 cprintf("550 String does not match anything.\n");
252 else if (SMTP->vrfy_count >= 1) {
253 cprintf("250 %s <cit%ld@%s>\n",
254 SMTP->vrfy_buffer.fullname,
255 SMTP->vrfy_buffer.usernum,
262 * Implements the RSET (reset state) command.
263 * Currently this just zeroes out the state buffer. If pointers to data
264 * allocated with mallok() are ever placed in the state buffer, we have to
265 * be sure to phree() them first!
267 void smtp_rset(void) {
268 memset(SMTP, 0, sizeof(struct citsmtp));
269 if (CC->logged_in) logout(CC);
270 cprintf("250 Zap!\n");
276 * Implements the "MAIL From:" command
278 void smtp_mail(char *argbuf) {
283 if (strlen(SMTP->from) != 0) {
284 cprintf("503 Only one sender permitted\n");
288 if (strncasecmp(argbuf, "From:", 5)) {
289 cprintf("501 Syntax error\n");
293 strcpy(SMTP->from, &argbuf[5]);
296 if (strlen(SMTP->from) == 0) {
297 cprintf("501 Empty sender name is not permitted\n");
302 /* If this SMTP connection is from a logged-in user, make sure that
303 * the user only sends email from his/her own address.
306 cvt = convert_internet_address(user, node, SMTP->from);
307 lprintf(9, "cvt=%d, citaddr=<%s@%s>\n", cvt, user, node);
308 if ( (cvt != 0) || (strcasecmp(user, CC->usersupp.fullname))) {
309 cprintf("550 <%s> is not your address.\n", SMTP->from);
310 strcpy(SMTP->from, "");
315 /* Otherwise, make sure outsiders aren't trying to forge mail from
319 cvt = convert_internet_address(user, node, SMTP->from);
320 lprintf(9, "cvt=%d, citaddr=<%s@%s>\n", cvt, user, node);
321 if (!strcasecmp(node, config.c_nodename)) { /* FIX use fcn */
322 cprintf("550 You must log in to send mail from %s\n",
324 strcpy(SMTP->from, "");
329 cprintf("250 Sender ok. Groovy.\n");
335 * Implements the "RCPT To:" command
337 void smtp_rcpt(char *argbuf) {
342 int is_spam = 0; /* FIX implement anti-spamming */
344 if (strlen(SMTP->from) == 0) {
345 cprintf("503 MAIL first, then RCPT. Duh.\n");
349 if (strncasecmp(argbuf, "To:", 3)) {
350 cprintf("501 Syntax error\n");
354 strcpy(recp, &argbuf[3]);
358 cvt = convert_internet_address(user, node, recp);
359 sprintf(recp, "%s@%s", user, node);
363 case rfc822_address_locally_validated:
364 cprintf("250 %s is a valid recipient.\n", user);
365 ++SMTP->number_of_recipients;
366 CtdlReallocUserData(SYM_SMTP_RECP,
367 strlen(SMTP_RECP) + 1024 );
368 strcat(SMTP_RECP, "local|");
369 strcat(SMTP_RECP, user);
370 strcat(SMTP_RECP, "|0\n");
373 case rfc822_no_such_user:
374 cprintf("550 %s: no such user\n", recp);
377 case rfc822_address_invalid:
379 cprintf("551 Away with thee, spammer!\n");
382 cprintf("250 Remote recipient %s ok\n", recp);
383 ++SMTP->number_of_recipients;
384 CtdlReallocUserData(SYM_SMTP_RECP,
385 strlen(SMTP_RECP) + 1024 );
386 strcat(SMTP_RECP, "remote|");
387 strcat(SMTP_RECP, recp);
388 strcat(SMTP_RECP, "|0\n");
394 cprintf("599 Unknown error\n");
402 * Back end for smtp_data() ... this does the actual delivery of the message
403 * Returns 0 on success, nonzero on failure
405 int smtp_message_delivery(struct CtdlMessage *msg) {
409 int successful_saves = 0;
410 int failed_saves = 0;
413 lprintf(9, "smtp_message_delivery() called\n");
415 /* Fill in 'from' fields with envelope information if missing */
416 process_rfc822_addr(SMTP->from, user, node, name);
417 if (msg->cm_fields['A']==NULL) msg->cm_fields['A'] = strdoop(user);
418 if (msg->cm_fields['N']==NULL) msg->cm_fields['N'] = strdoop(node);
419 if (msg->cm_fields['H']==NULL) msg->cm_fields['H'] = strdoop(name);
421 /* Save the message in the queue */
422 msgid = CtdlSaveMsg(msg,
429 /* FIX go thru each local user and save to boxes
430 then stuff remote users in queue list
431 then delete from queue if num remote users is 0
434 return(failed_saves);
440 * Implements the DATA command
442 void smtp_data(void) {
444 struct CtdlMessage *msg;
448 if (strlen(SMTP->from) == 0) {
449 cprintf("503 Need MAIL command first.\n");
453 if (SMTP->number_of_recipients < 1) {
454 cprintf("503 Need RCPT command first.\n");
458 cprintf("354 Transmit message now; terminate with '.' by itself\n");
460 generate_rfc822_datestamp(nowstamp, time(NULL));
462 if (body != NULL) sprintf(body,
463 "Received: from %s\n"
470 body = CtdlReadMessageBody(".", config.c_maxmsglen, body);
472 cprintf("550 Unable to save message text: internal error.\n");
476 lprintf(9, "Converting message...\n");
477 msg = convert_internet_message(body);
479 /* If the user is locally authenticated, FORCE the From: header to
480 * show up as the real sender
483 if (msg->cm_fields['A'] != NULL) phree(msg->cm_fields['A']);
484 if (msg->cm_fields['N'] != NULL) phree(msg->cm_fields['N']);
485 if (msg->cm_fields['H'] != NULL) phree(msg->cm_fields['H']);
486 msg->cm_fields['A'] = strdoop(CC->usersupp.fullname);
487 msg->cm_fields['N'] = strdoop(config.c_nodename);
488 msg->cm_fields['H'] = strdoop(config.c_humannode);
491 retval = smtp_message_delivery(msg);
492 CtdlFreeMessage(msg);
495 cprintf("250 Message accepted for delivery.\n");
498 cprintf("550 Internal error.\n");
506 * Main command loop for SMTP sessions.
508 void smtp_command_loop(void) {
512 memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
513 if (client_gets(cmdbuf) < 1) {
514 lprintf(3, "SMTP socket is broken. Ending session.\n");
518 lprintf(5, "citserver[%3d]: %s\n", CC->cs_pid, cmdbuf);
519 while (strlen(cmdbuf) < 5) strcat(cmdbuf, " ");
521 if (SMTP->command_state == smtp_user) {
522 smtp_get_user(cmdbuf);
525 else if (SMTP->command_state == smtp_password) {
526 smtp_get_pass(cmdbuf);
529 else if (!strncasecmp(cmdbuf, "AUTH", 4)) {
530 smtp_auth(&cmdbuf[5]);
533 else if (!strncasecmp(cmdbuf, "DATA", 4)) {
537 else if (!strncasecmp(cmdbuf, "EHLO", 4)) {
538 smtp_hello(&cmdbuf[5], 1);
541 else if (!strncasecmp(cmdbuf, "EXPN", 4)) {
542 smtp_expn(&cmdbuf[5]);
545 else if (!strncasecmp(cmdbuf, "HELO", 4)) {
546 smtp_hello(&cmdbuf[5], 0);
549 else if (!strncasecmp(cmdbuf, "HELP", 4)) {
553 else if (!strncasecmp(cmdbuf, "MAIL", 4)) {
554 smtp_mail(&cmdbuf[5]);
557 else if (!strncasecmp(cmdbuf, "NOOP", 4)) {
558 cprintf("250 This command successfully did nothing.\n");
561 else if (!strncasecmp(cmdbuf, "QUIT", 4)) {
562 cprintf("221 Goodbye...\n");
567 else if (!strncasecmp(cmdbuf, "RCPT", 4)) {
568 smtp_rcpt(&cmdbuf[5]);
571 else if (!strncasecmp(cmdbuf, "RSET", 4)) {
575 else if (!strncasecmp(cmdbuf, "VRFY", 4)) {
576 smtp_vrfy(&cmdbuf[5]);
580 cprintf("502 I'm afraid I can't do that, Dave.\n");
586 char *Dynamic_Module_Init(void)
588 SYM_SMTP = CtdlGetDynamicSymbol();
589 SYM_SMTP_RECP = CtdlGetDynamicSymbol();
590 CtdlRegisterServiceHook(SMTP_PORT,
593 create_room(SMTP_SPOOLOUT_ROOM, 3, "", 0);