13 #include <sys/types.h>
21 #include "sysdep_decls.h"
22 #include "citserver.h"
25 #include "dynloader.h"
32 #include "internet_addressing.h"
35 struct citsmtp { /* Information about the current session */
37 struct usersupp vrfy_buffer;
42 int number_of_recipients;
45 enum { /* Command states for login authentication */
51 #define SMTP ((struct citsmtp *)CtdlGetUserData(SYM_SMTP))
57 * Here's where our SMTP session begins its happy day.
59 void smtp_greeting(void) {
61 strcpy(CC->cs_clientname, "SMTP session");
63 CC->cs_flags |= CS_STEALTH;
64 CtdlAllocUserData(SYM_SMTP, sizeof(struct citsmtp));
66 cprintf("220 Welcome to the Citadel/UX ESMTP server at %s\n",
72 * Implement HELO and EHLO commands.
74 void smtp_hello(char *argbuf, int is_esmtp) {
77 cprintf("250 Greetings and joyous salutations.\n");
80 cprintf("250-Greetings and joyous salutations.\n");
81 cprintf("250-HELP\n");
82 cprintf("250-SIZE %ld\n", config.c_maxmsglen);
83 cprintf("250 AUTH=LOGIN\n");
89 * Implement HELP command.
91 void smtp_help(void) {
92 cprintf("214-Here's the frequency, Kenneth:\n");
93 cprintf("214- DATA\n");
94 cprintf("214- EHLO\n");
95 cprintf("214- EXPN\n");
96 cprintf("214- HELO\n");
97 cprintf("214- HELP\n");
98 cprintf("214- MAIL\n");
99 cprintf("214- NOOP\n");
100 cprintf("214- QUIT\n");
101 cprintf("214- RCPT\n");
102 cprintf("214- RSET\n");
103 cprintf("214- VRFY\n");
104 cprintf("214 I could tell you more, but then I'd have to kill you.\n");
111 void smtp_get_user(char *argbuf) {
115 decode_base64(username, argbuf);
116 lprintf(9, "Trying <%s>\n", username);
117 if (CtdlLoginExistingUser(username) == login_ok) {
118 encode_base64(buf, "Password:");
119 cprintf("334 %s\n", buf);
120 SMTP->command_state = smtp_password;
123 cprintf("500 No such user.\n");
124 SMTP->command_state = smtp_command;
132 void smtp_get_pass(char *argbuf) {
135 decode_base64(password, argbuf);
136 lprintf(9, "Trying <%s>\n", password);
137 if (CtdlTryPassword(password) == pass_ok) {
138 cprintf("235 Authentication successful.\n");
139 lprintf(9, "SMTP auth login successful\n");
140 CC->internal_pgm = 0;
141 CC->cs_flags &= ~CS_STEALTH;
144 cprintf("500 Authentication failed.\n");
146 SMTP->command_state = smtp_command;
153 void smtp_auth(char *argbuf) {
156 if (strncasecmp(argbuf, "login", 5) ) {
157 cprintf("550 We only support LOGIN authentication.\n");
161 if (strlen(argbuf) >= 7) {
162 smtp_get_user(&argbuf[6]);
166 encode_base64(buf, "Username:");
167 cprintf("334 %s\n", buf);
168 SMTP->command_state = smtp_user;
174 * Back end for smtp_vrfy() command
176 void smtp_vrfy_backend(struct usersupp *us) {
178 if (!fuzzy_match(us, SMTP->vrfy_match)) {
180 memcpy(&SMTP->vrfy_buffer, us, sizeof(struct usersupp));
186 * Implements the VRFY (verify user name) command.
187 * Performs fuzzy match on full user names.
189 void smtp_vrfy(char *argbuf) {
190 SMTP->vrfy_count = 0;
191 strcpy(SMTP->vrfy_match, argbuf);
192 ForEachUser(smtp_vrfy_backend);
194 if (SMTP->vrfy_count < 1) {
195 cprintf("550 String does not match anything.\n");
197 else if (SMTP->vrfy_count == 1) {
198 cprintf("250 %s <cit%ld@%s>\n",
199 SMTP->vrfy_buffer.fullname,
200 SMTP->vrfy_buffer.usernum,
203 else if (SMTP->vrfy_count > 1) {
204 cprintf("553 Request ambiguous: %d users matched.\n",
213 * Back end for smtp_expn() command
215 void smtp_expn_backend(struct usersupp *us) {
217 if (!fuzzy_match(us, SMTP->vrfy_match)) {
219 if (SMTP->vrfy_count >= 1) {
220 cprintf("250-%s <cit%ld@%s>\n",
221 SMTP->vrfy_buffer.fullname,
222 SMTP->vrfy_buffer.usernum,
227 memcpy(&SMTP->vrfy_buffer, us, sizeof(struct usersupp));
233 * Implements the EXPN (expand user name) command.
234 * Performs fuzzy match on full user names.
236 void smtp_expn(char *argbuf) {
237 SMTP->vrfy_count = 0;
238 strcpy(SMTP->vrfy_match, argbuf);
239 ForEachUser(smtp_expn_backend);
241 if (SMTP->vrfy_count < 1) {
242 cprintf("550 String does not match anything.\n");
244 else if (SMTP->vrfy_count >= 1) {
245 cprintf("250 %s <cit%ld@%s>\n",
246 SMTP->vrfy_buffer.fullname,
247 SMTP->vrfy_buffer.usernum,
254 * Implements the RSET (reset state) command.
255 * Currently this just zeroes out the state buffer. If pointers to data
256 * allocated with mallok() are ever placed in the state buffer, we have to
257 * be sure to phree() them first!
259 void smtp_rset(void) {
260 memset(SMTP, 0, sizeof(struct citsmtp));
261 if (CC->logged_in) logout(CC);
262 cprintf("250 Zap!\n");
268 * Implements the "MAIL From:" command
270 void smtp_mail(char *argbuf) {
275 if (strlen(SMTP->from) != 0) {
276 cprintf("503 Only one sender permitted\n");
280 if (strncasecmp(argbuf, "From:", 5)) {
281 cprintf("501 Syntax error\n");
285 strcpy(SMTP->from, &argbuf[5]);
288 if (strlen(SMTP->from) == 0) {
289 cprintf("501 Empty sender name is not permitted\n");
294 /* If this SMTP connection is from a logged-in user, make sure that
295 * the user only sends email from his/her own address.
298 cvt = convert_internet_address(user, node, SMTP->from);
299 lprintf(9, "cvt=%d, citaddr=<%s@%s>\n", cvt, user, node);
300 if ( (cvt != 0) || (strcasecmp(user, CC->usersupp.fullname))) {
301 cprintf("550 <%s> is not your address.\n", SMTP->from);
302 strcpy(SMTP->from, "");
307 /* Otherwise, make sure outsiders aren't trying to forge mail from
311 cvt = convert_internet_address(user, node, SMTP->from);
312 lprintf(9, "cvt=%d, citaddr=<%s@%s>\n", cvt, user, node);
313 if (!strcasecmp(node, config.c_nodename)) { /* FIX use fcn */
314 cprintf("550 You must log in to send mail from %s\n",
316 strcpy(SMTP->from, "");
321 cprintf("250 Sender ok. Groovy.\n");
327 * Implements the "RCPT To:" command
329 void smtp_rcpt(char *argbuf) {
334 if (strlen(SMTP->from) == 0) {
335 cprintf("503 MAIL first, then RCPT. Duh.\n");
339 if (SMTP->number_of_recipients > 0) {
340 cprintf("552 Only one recipient allowed (FIX)\n");
344 if (strncasecmp(argbuf, "To:", 3)) {
345 cprintf("501 Syntax error\n");
349 strcpy(SMTP->recipient, &argbuf[3]);
350 striplt(SMTP->recipient);
352 cvt = convert_internet_address(user, node, SMTP->recipient);
354 case rfc822_address_locally_validated:
355 cprintf("250 %s is a valid recipient.\n", user);
356 ++SMTP->number_of_recipients;
358 case rfc822_no_such_user:
359 cprintf("550 %s: no such user\n", SMTP->recipient);
360 strcpy(SMTP->recipient, "");
364 strcpy(SMTP->recipient, "");
365 cprintf("599 Unknown error (FIX)\n");
373 * Back end for smtp_data() ... this does the actual delivery of the message
374 * Returns 0 on success, nonzero on failure
376 int smtp_message_delivery(struct CtdlMessage *msg) {
380 int successful_saves = 0;
381 int failed_saves = 0;
385 lprintf(9, "smtp_message_delivery() called\n");
387 /* Fill in 'from' fields with envelope information if missing */
388 process_rfc822_addr(SMTP->from, user, node, name);
389 if (msg->cm_fields['A']==NULL) msg->cm_fields['A'] = strdoop(user);
390 if (msg->cm_fields['N']==NULL) msg->cm_fields['N'] = strdoop(node);
391 if (msg->cm_fields['H']==NULL) msg->cm_fields['H'] = strdoop(name);
393 /* Stuff the boxes */
394 /* FIX modify to handle multiple recipients */
395 cvt = convert_internet_address(user, node, SMTP->recipient);
397 case rfc822_address_locally_validated:
398 lprintf(9, "Delivering to %s\n", user);
408 /* FIX copy the msgid to another room */
414 case rfc822_no_such_user:
419 return(failed_saves);
425 * Implements the DATA command
427 void smtp_data(void) {
429 struct CtdlMessage *msg;
432 if (strlen(SMTP->from) == 0) {
433 cprintf("503 Need MAIL command first.\n");
437 if (SMTP->number_of_recipients < 1) {
438 cprintf("503 Need RCPT command first.\n");
442 cprintf("354 Transmit message now; terminate with '.' by itself\n");
443 body = CtdlReadMessageBody(".", config.c_maxmsglen);
445 cprintf("550 Unable to save message text: internal error.\n");
449 lprintf(9, "Converting message...\n");
450 msg = convert_internet_message(body);
452 /* If the user is locally authenticated, FORCE the From: header to
453 * show up as the real sender
456 if (msg->cm_fields['A'] != NULL) phree(msg->cm_fields['A']);
457 if (msg->cm_fields['N'] != NULL) phree(msg->cm_fields['N']);
458 if (msg->cm_fields['H'] != NULL) phree(msg->cm_fields['H']);
459 msg->cm_fields['A'] = strdoop(CC->usersupp.fullname);
460 msg->cm_fields['N'] = strdoop(config.c_nodename);
461 msg->cm_fields['H'] = strdoop(config.c_humannode);
464 retval = smtp_message_delivery(msg);
465 CtdlFreeMessage(msg);
468 cprintf("250 Message accepted for delivery.\n");
471 cprintf("550 Internal error.\n");
479 * Main command loop for SMTP sessions.
481 void smtp_command_loop(void) {
485 memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
486 if (client_gets(cmdbuf) < 1) {
487 lprintf(3, "SMTP socket is broken. Ending session.\n");
491 lprintf(5, "citserver[%3d]: %s\n", CC->cs_pid, cmdbuf);
492 while (strlen(cmdbuf) < 5) strcat(cmdbuf, " ");
494 if (SMTP->command_state == smtp_user) {
495 smtp_get_user(cmdbuf);
498 else if (SMTP->command_state == smtp_password) {
499 smtp_get_pass(cmdbuf);
502 else if (!strncasecmp(cmdbuf, "AUTH", 4)) {
503 smtp_auth(&cmdbuf[5]);
506 else if (!strncasecmp(cmdbuf, "DATA", 4)) {
510 else if (!strncasecmp(cmdbuf, "EHLO", 4)) {
511 smtp_hello(&cmdbuf[5], 1);
514 else if (!strncasecmp(cmdbuf, "EXPN", 4)) {
515 smtp_expn(&cmdbuf[5]);
518 else if (!strncasecmp(cmdbuf, "HELO", 4)) {
519 smtp_hello(&cmdbuf[5], 0);
522 else if (!strncasecmp(cmdbuf, "HELP", 4)) {
526 else if (!strncasecmp(cmdbuf, "MAIL", 4)) {
527 smtp_mail(&cmdbuf[5]);
530 else if (!strncasecmp(cmdbuf, "NOOP", 4)) {
531 cprintf("250 This command successfully did nothing.\n");
534 else if (!strncasecmp(cmdbuf, "QUIT", 4)) {
535 cprintf("221 Goodbye...\n");
540 else if (!strncasecmp(cmdbuf, "RCPT", 4)) {
541 smtp_rcpt(&cmdbuf[5]);
544 else if (!strncasecmp(cmdbuf, "RSET", 4)) {
548 else if (!strncasecmp(cmdbuf, "VRFY", 4)) {
549 smtp_vrfy(&cmdbuf[5]);
553 cprintf("502 I'm afraid I can't do that, Dave.\n");
560 char *Dynamic_Module_Init(void)
562 SYM_SMTP = CtdlGetDynamicSymbol();
563 CtdlRegisterServiceHook(SMTP_PORT,