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;
41 int number_of_recipients;
45 enum { /* Command states for login authentication */
51 enum { /* Delivery modes */
56 #define SMTP ((struct citsmtp *)CtdlGetUserData(SYM_SMTP))
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));
71 cprintf("220 Welcome to the Citadel/UX ESMTP server at %s\n",
77 * Implement HELO and EHLO commands.
79 void smtp_hello(char *argbuf, int is_esmtp) {
82 cprintf("250 Greetings and joyous salutations.\n");
85 cprintf("250-Greetings and joyous salutations.\n");
86 cprintf("250-HELP\n");
87 cprintf("250-SIZE %ld\n", config.c_maxmsglen);
88 cprintf("250 AUTH=LOGIN\n");
94 * Implement HELP command.
96 void smtp_help(void) {
97 cprintf("214-Here's the frequency, Kenneth:\n");
98 cprintf("214- DATA\n");
99 cprintf("214- EHLO\n");
100 cprintf("214- EXPN\n");
101 cprintf("214- HELO\n");
102 cprintf("214- HELP\n");
103 cprintf("214- MAIL\n");
104 cprintf("214- NOOP\n");
105 cprintf("214- QUIT\n");
106 cprintf("214- RCPT\n");
107 cprintf("214- RSET\n");
108 cprintf("214- VRFY\n");
109 cprintf("214 I could tell you more, but then I'd have to kill you.\n");
116 void smtp_get_user(char *argbuf) {
120 decode_base64(username, argbuf);
121 lprintf(9, "Trying <%s>\n", username);
122 if (CtdlLoginExistingUser(username) == login_ok) {
123 encode_base64(buf, "Password:");
124 cprintf("334 %s\n", buf);
125 SMTP->command_state = smtp_password;
128 cprintf("500 No such user.\n");
129 SMTP->command_state = smtp_command;
137 void smtp_get_pass(char *argbuf) {
140 decode_base64(password, argbuf);
141 lprintf(9, "Trying <%s>\n", password);
142 if (CtdlTryPassword(password) == pass_ok) {
143 cprintf("235 Authentication successful.\n");
144 lprintf(9, "SMTP auth login successful\n");
145 CC->internal_pgm = 0;
146 CC->cs_flags &= ~CS_STEALTH;
149 cprintf("500 Authentication failed.\n");
151 SMTP->command_state = smtp_command;
158 void smtp_auth(char *argbuf) {
161 if (strncasecmp(argbuf, "login", 5) ) {
162 cprintf("550 We only support LOGIN authentication.\n");
166 if (strlen(argbuf) >= 7) {
167 smtp_get_user(&argbuf[6]);
171 encode_base64(buf, "Username:");
172 cprintf("334 %s\n", buf);
173 SMTP->command_state = smtp_user;
179 * Back end for smtp_vrfy() command
181 void smtp_vrfy_backend(struct usersupp *us) {
183 if (!fuzzy_match(us, SMTP->vrfy_match)) {
185 memcpy(&SMTP->vrfy_buffer, us, sizeof(struct usersupp));
191 * Implements the VRFY (verify user name) command.
192 * Performs fuzzy match on full user names.
194 void smtp_vrfy(char *argbuf) {
195 SMTP->vrfy_count = 0;
196 strcpy(SMTP->vrfy_match, argbuf);
197 ForEachUser(smtp_vrfy_backend);
199 if (SMTP->vrfy_count < 1) {
200 cprintf("550 String does not match anything.\n");
202 else if (SMTP->vrfy_count == 1) {
203 cprintf("250 %s <cit%ld@%s>\n",
204 SMTP->vrfy_buffer.fullname,
205 SMTP->vrfy_buffer.usernum,
208 else if (SMTP->vrfy_count > 1) {
209 cprintf("553 Request ambiguous: %d users matched.\n",
218 * Back end for smtp_expn() command
220 void smtp_expn_backend(struct usersupp *us) {
222 if (!fuzzy_match(us, SMTP->vrfy_match)) {
224 if (SMTP->vrfy_count >= 1) {
225 cprintf("250-%s <cit%ld@%s>\n",
226 SMTP->vrfy_buffer.fullname,
227 SMTP->vrfy_buffer.usernum,
232 memcpy(&SMTP->vrfy_buffer, us, sizeof(struct usersupp));
238 * Implements the EXPN (expand user name) command.
239 * Performs fuzzy match on full user names.
241 void smtp_expn(char *argbuf) {
242 SMTP->vrfy_count = 0;
243 strcpy(SMTP->vrfy_match, argbuf);
244 ForEachUser(smtp_expn_backend);
246 if (SMTP->vrfy_count < 1) {
247 cprintf("550 String does not match anything.\n");
249 else if (SMTP->vrfy_count >= 1) {
250 cprintf("250 %s <cit%ld@%s>\n",
251 SMTP->vrfy_buffer.fullname,
252 SMTP->vrfy_buffer.usernum,
259 * Implements the RSET (reset state) command.
260 * Currently this just zeroes out the state buffer. If pointers to data
261 * allocated with mallok() are ever placed in the state buffer, we have to
262 * be sure to phree() them first!
264 void smtp_rset(void) {
265 memset(SMTP, 0, sizeof(struct citsmtp));
266 if (CC->logged_in) logout(CC);
267 cprintf("250 Zap!\n");
273 * Implements the "MAIL From:" command
275 void smtp_mail(char *argbuf) {
280 if (strlen(SMTP->from) != 0) {
281 cprintf("503 Only one sender permitted\n");
285 if (strncasecmp(argbuf, "From:", 5)) {
286 cprintf("501 Syntax error\n");
290 strcpy(SMTP->from, &argbuf[5]);
293 if (strlen(SMTP->from) == 0) {
294 cprintf("501 Empty sender name is not permitted\n");
299 /* If this SMTP connection is from a logged-in user, make sure that
300 * the user only sends email from his/her own address.
303 cvt = convert_internet_address(user, node, SMTP->from);
304 lprintf(9, "cvt=%d, citaddr=<%s@%s>\n", cvt, user, node);
305 if ( (cvt != 0) || (strcasecmp(user, CC->usersupp.fullname))) {
306 cprintf("550 <%s> is not your address.\n", SMTP->from);
307 strcpy(SMTP->from, "");
312 /* Otherwise, make sure outsiders aren't trying to forge mail from
316 cvt = convert_internet_address(user, node, SMTP->from);
317 lprintf(9, "cvt=%d, citaddr=<%s@%s>\n", cvt, user, node);
318 if (!strcasecmp(node, config.c_nodename)) { /* FIX use fcn */
319 cprintf("550 You must log in to send mail from %s\n",
321 strcpy(SMTP->from, "");
326 cprintf("250 Sender ok. Groovy.\n");
332 * Implements the "RCPT To:" command
334 void smtp_rcpt(char *argbuf) {
339 if (strlen(SMTP->from) == 0) {
340 cprintf("503 MAIL first, then RCPT. Duh.\n");
344 if (SMTP->number_of_recipients > 0) {
345 cprintf("552 Only one recipient allowed (FIX)\n");
349 if (strncasecmp(argbuf, "To:", 3)) {
350 cprintf("501 Syntax error\n");
354 strcpy(SMTP->recipient, &argbuf[3]);
355 striplt(SMTP->recipient);
356 alias(SMTP->recipient);
358 cvt = convert_internet_address(user, node, SMTP->recipient);
360 case rfc822_address_locally_validated:
361 cprintf("250 %s is a valid recipient.\n", user);
362 ++SMTP->number_of_recipients;
364 case rfc822_no_such_user:
365 cprintf("550 %s: no such user\n", SMTP->recipient);
366 strcpy(SMTP->recipient, "");
370 strcpy(SMTP->recipient, "");
371 cprintf("599 Unknown error (FIX)\n");
379 * Back end for smtp_data() ... this does the actual delivery of the message
380 * Returns 0 on success, nonzero on failure
382 int smtp_message_delivery(struct CtdlMessage *msg) {
386 int successful_saves = 0;
387 int failed_saves = 0;
391 lprintf(9, "smtp_message_delivery() called\n");
393 /* Fill in 'from' fields with envelope information if missing */
394 process_rfc822_addr(SMTP->from, user, node, name);
395 if (msg->cm_fields['A']==NULL) msg->cm_fields['A'] = strdoop(user);
396 if (msg->cm_fields['N']==NULL) msg->cm_fields['N'] = strdoop(node);
397 if (msg->cm_fields['H']==NULL) msg->cm_fields['H'] = strdoop(name);
399 /* Stuff the boxes */
400 /* FIX modify to handle multiple recipients */
401 cvt = convert_internet_address(user, node, SMTP->recipient);
403 case rfc822_address_locally_validated:
404 lprintf(9, "Delivering to %s\n", user);
414 /* FIX copy the msgid to another room */
420 case rfc822_no_such_user:
425 return(failed_saves);
431 * Implements the DATA command
433 void smtp_data(void) {
435 struct CtdlMessage *msg;
439 if (strlen(SMTP->from) == 0) {
440 cprintf("503 Need MAIL command first.\n");
444 if (SMTP->number_of_recipients < 1) {
445 cprintf("503 Need RCPT command first.\n");
449 cprintf("354 Transmit message now; terminate with '.' by itself\n");
451 generate_rfc822_datestamp(nowstamp, time(NULL));
453 if (body != NULL) sprintf(body,
454 "Received: from %s\n"
461 body = CtdlReadMessageBody(".", config.c_maxmsglen, body);
463 cprintf("550 Unable to save message text: internal error.\n");
467 lprintf(9, "Converting message...\n");
468 msg = convert_internet_message(body);
470 /* If the user is locally authenticated, FORCE the From: header to
471 * show up as the real sender
474 if (msg->cm_fields['A'] != NULL) phree(msg->cm_fields['A']);
475 if (msg->cm_fields['N'] != NULL) phree(msg->cm_fields['N']);
476 if (msg->cm_fields['H'] != NULL) phree(msg->cm_fields['H']);
477 msg->cm_fields['A'] = strdoop(CC->usersupp.fullname);
478 msg->cm_fields['N'] = strdoop(config.c_nodename);
479 msg->cm_fields['H'] = strdoop(config.c_humannode);
482 retval = smtp_message_delivery(msg);
483 CtdlFreeMessage(msg);
486 cprintf("250 Message accepted for delivery.\n");
489 cprintf("550 Internal error.\n");
497 * Main command loop for SMTP sessions.
499 void smtp_command_loop(void) {
503 memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
504 if (client_gets(cmdbuf) < 1) {
505 lprintf(3, "SMTP socket is broken. Ending session.\n");
509 lprintf(5, "citserver[%3d]: %s\n", CC->cs_pid, cmdbuf);
510 while (strlen(cmdbuf) < 5) strcat(cmdbuf, " ");
512 if (SMTP->command_state == smtp_user) {
513 smtp_get_user(cmdbuf);
516 else if (SMTP->command_state == smtp_password) {
517 smtp_get_pass(cmdbuf);
520 else if (!strncasecmp(cmdbuf, "AUTH", 4)) {
521 smtp_auth(&cmdbuf[5]);
524 else if (!strncasecmp(cmdbuf, "DATA", 4)) {
528 else if (!strncasecmp(cmdbuf, "EHLO", 4)) {
529 smtp_hello(&cmdbuf[5], 1);
532 else if (!strncasecmp(cmdbuf, "EXPN", 4)) {
533 smtp_expn(&cmdbuf[5]);
536 else if (!strncasecmp(cmdbuf, "HELO", 4)) {
537 smtp_hello(&cmdbuf[5], 0);
540 else if (!strncasecmp(cmdbuf, "HELP", 4)) {
544 else if (!strncasecmp(cmdbuf, "MAIL", 4)) {
545 smtp_mail(&cmdbuf[5]);
548 else if (!strncasecmp(cmdbuf, "NOOP", 4)) {
549 cprintf("250 This command successfully did nothing.\n");
552 else if (!strncasecmp(cmdbuf, "QUIT", 4)) {
553 cprintf("221 Goodbye...\n");
558 else if (!strncasecmp(cmdbuf, "RCPT", 4)) {
559 smtp_rcpt(&cmdbuf[5]);
562 else if (!strncasecmp(cmdbuf, "RSET", 4)) {
566 else if (!strncasecmp(cmdbuf, "VRFY", 4)) {
567 smtp_vrfy(&cmdbuf[5]);
571 cprintf("502 I'm afraid I can't do that, Dave.\n");
577 char *Dynamic_Module_Init(void)
579 SYM_SMTP = CtdlGetDynamicSymbol();
580 CtdlRegisterServiceHook(SMTP_PORT,
583 create_room(SMTP_SPOOLOUT_ROOM, 3, "", 0);