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 (strlen(SMTP->recipient) > 0) {
340 cprintf("550 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);
357 case rfc822_no_such_user:
358 cprintf("550 %s: no such user\n", SMTP->recipient);
359 strcpy(SMTP->recipient, "");
363 strcpy(SMTP->recipient, "");
364 cprintf("599 Unknown error (FIX)\n");
371 * Implements the DATA command
373 void smtp_data(void) {
375 struct CtdlMessage *msg;
379 if (strlen(SMTP->from) == 0) {
380 cprintf("503 Need MAIL command first.\n");
384 if (SMTP->number_of_recipients < 1) {
385 cprintf("503 Need RCPT command first.\n");
391 cprintf("354 Transmit message now; terminate with '.' by itself\n");
392 body = CtdlReadMessageBody(".", config.c_maxmsglen);
394 cprintf("550 Unable to save message text: internal error.\n");
398 lprintf(9, "Converting message...\n");
399 msg = convert_internet_message(body);
401 /* FIX do something with it! */
402 lprintf(9, "Saving message...\n");
403 CtdlSaveMsg(msg, "", BASEROOM, MES_LOCAL, 1);
405 CtdlFreeMessage(msg);
407 cprintf("599 command unfinished but message saved\n");
414 * Main command loop for SMTP sessions.
416 void smtp_command_loop(void) {
420 memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
421 if (client_gets(cmdbuf) < 1) {
422 lprintf(3, "SMTP socket is broken. Ending session.\n");
426 lprintf(5, "citserver[%3d]: %s\n", CC->cs_pid, cmdbuf);
427 while (strlen(cmdbuf) < 5) strcat(cmdbuf, " ");
429 if (SMTP->command_state == smtp_user) {
430 smtp_get_user(cmdbuf);
433 else if (SMTP->command_state == smtp_password) {
434 smtp_get_pass(cmdbuf);
437 else if (!strncasecmp(cmdbuf, "AUTH", 4)) {
438 smtp_auth(&cmdbuf[5]);
441 else if (!strncasecmp(cmdbuf, "DATA", 4)) {
445 else if (!strncasecmp(cmdbuf, "EHLO", 4)) {
446 smtp_hello(&cmdbuf[5], 1);
449 else if (!strncasecmp(cmdbuf, "EXPN", 4)) {
450 smtp_expn(&cmdbuf[5]);
453 else if (!strncasecmp(cmdbuf, "HELO", 4)) {
454 smtp_hello(&cmdbuf[5], 0);
457 else if (!strncasecmp(cmdbuf, "HELP", 4)) {
461 else if (!strncasecmp(cmdbuf, "MAIL", 4)) {
462 smtp_mail(&cmdbuf[5]);
465 else if (!strncasecmp(cmdbuf, "NOOP", 4)) {
466 cprintf("250 This command successfully did nothing.\n");
469 else if (!strncasecmp(cmdbuf, "QUIT", 4)) {
470 cprintf("221 Goodbye...\n");
475 else if (!strncasecmp(cmdbuf, "RCPT", 4)) {
476 smtp_rcpt(&cmdbuf[5]);
479 else if (!strncasecmp(cmdbuf, "RSET", 4)) {
483 else if (!strncasecmp(cmdbuf, "VRFY", 4)) {
484 smtp_vrfy(&cmdbuf[5]);
488 cprintf("500 I'm afraid I can't do that, Dave.\n");
495 char *Dynamic_Module_Init(void)
497 SYM_SMTP = CtdlGetDynamicSymbol();
498 CtdlRegisterServiceHook(SMTP_PORT,