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, "Citadel SMTP");
62 CtdlAllocUserData(SYM_SMTP, sizeof(struct citsmtp));
64 cprintf("220 Welcome to the Citadel/UX ESMTP server at %s\n",
70 * Implement HELO and EHLO commands.
72 void smtp_hello(char *argbuf, int is_esmtp) {
75 cprintf("250 Greetings and joyous salutations.\n");
78 cprintf("250-Greetings and joyous salutations.\n");
79 cprintf("250-HELP\n");
80 cprintf("250-SIZE %ld\n", config.c_maxmsglen);
81 cprintf("250 AUTH=LOGIN\n");
87 * Implement HELP command.
89 void smtp_help(void) {
90 cprintf("214-Here's the frequency, Kenneth:\n");
91 cprintf("214- DATA\n");
92 cprintf("214- EHLO\n");
93 cprintf("214- EXPN\n");
94 cprintf("214- HELO\n");
95 cprintf("214- HELP\n");
96 cprintf("214- MAIL\n");
97 cprintf("214- NOOP\n");
98 cprintf("214- QUIT\n");
99 cprintf("214- RCPT\n");
100 cprintf("214- RSET\n");
101 cprintf("214- VRFY\n");
102 cprintf("214 I could tell you more, but then I'd have to kill you.\n");
109 void smtp_get_user(char *argbuf) {
113 decode_base64(username, argbuf);
114 lprintf(9, "Trying <%s>\n", username);
115 if (CtdlLoginExistingUser(username) == login_ok) {
116 encode_base64(buf, "Password:");
117 cprintf("334 %s\n", buf);
118 SMTP->command_state = smtp_password;
121 cprintf("500 No such user.\n");
122 SMTP->command_state = smtp_command;
130 void smtp_get_pass(char *argbuf) {
133 decode_base64(password, argbuf);
134 lprintf(9, "Trying <%s>\n", password);
135 if (CtdlTryPassword(password) == pass_ok) {
136 cprintf("235 Authentication successful.\n");
137 lprintf(9, "SMTP auth login successful\n");
140 cprintf("500 Authentication failed.\n");
142 SMTP->command_state = smtp_command;
149 void smtp_auth(char *argbuf) {
152 if (strncasecmp(argbuf, "login", 5) ) {
153 cprintf("550 We only support LOGIN authentication.\n");
157 if (strlen(argbuf) >= 7) {
158 smtp_get_user(&argbuf[6]);
162 encode_base64(buf, "Username:");
163 cprintf("334 %s\n", buf);
164 SMTP->command_state = smtp_user;
170 * Back end for smtp_vrfy() command
172 void smtp_vrfy_backend(struct usersupp *us) {
174 if (!fuzzy_match(us, SMTP->vrfy_match)) {
176 memcpy(&SMTP->vrfy_buffer, us, sizeof(struct usersupp));
182 * Implements the VRFY (verify user name) command.
183 * Performs fuzzy match on full user names.
185 void smtp_vrfy(char *argbuf) {
186 SMTP->vrfy_count = 0;
187 strcpy(SMTP->vrfy_match, argbuf);
188 ForEachUser(smtp_vrfy_backend);
190 if (SMTP->vrfy_count < 1) {
191 cprintf("550 String does not match anything.\n");
193 else if (SMTP->vrfy_count == 1) {
194 cprintf("250 %s <cit%ld@%s>\n",
195 SMTP->vrfy_buffer.fullname,
196 SMTP->vrfy_buffer.usernum,
199 else if (SMTP->vrfy_count > 1) {
200 cprintf("553 Request ambiguous: %d users matched.\n",
209 * Back end for smtp_expn() command
211 void smtp_expn_backend(struct usersupp *us) {
213 if (!fuzzy_match(us, SMTP->vrfy_match)) {
215 if (SMTP->vrfy_count >= 1) {
216 cprintf("250-%s <cit%ld@%s>\n",
217 SMTP->vrfy_buffer.fullname,
218 SMTP->vrfy_buffer.usernum,
223 memcpy(&SMTP->vrfy_buffer, us, sizeof(struct usersupp));
229 * Implements the EXPN (expand user name) command.
230 * Performs fuzzy match on full user names.
232 void smtp_expn(char *argbuf) {
233 SMTP->vrfy_count = 0;
234 strcpy(SMTP->vrfy_match, argbuf);
235 ForEachUser(smtp_expn_backend);
237 if (SMTP->vrfy_count < 1) {
238 cprintf("550 String does not match anything.\n");
240 else if (SMTP->vrfy_count >= 1) {
241 cprintf("250 %s <cit%ld@%s>\n",
242 SMTP->vrfy_buffer.fullname,
243 SMTP->vrfy_buffer.usernum,
250 * Implements the RSET (reset state) command.
251 * Currently this just zeroes out the state buffer. If pointers to data
252 * allocated with mallok() are ever placed in the state buffer, we have to
253 * be sure to phree() them first!
255 void smtp_rset(void) {
256 memset(SMTP, 0, sizeof(struct citsmtp));
257 if (CC->logged_in) logout(CC);
258 cprintf("250 Zap!\n");
264 * Implements the "MAIL From:" command
266 void smtp_mail(char *argbuf) {
271 if (strlen(SMTP->from) != 0) {
272 cprintf("503 Only one sender permitted\n");
276 if (strncasecmp(argbuf, "From:", 5)) {
277 cprintf("501 Syntax error\n");
281 strcpy(SMTP->from, &argbuf[5]);
284 if (strlen(SMTP->from) == 0) {
285 cprintf("501 Empty sender name is not permitted\n");
290 /* If this SMTP connection is from a logged-in user, make sure that
291 * the user only sends email from his/her own address.
294 cvt = convert_internet_address(user, node, SMTP->from);
295 lprintf(9, "cvt=%d, citaddr=<%s@%s>\n", cvt, user, node);
296 if ( (cvt != 0) || (strcasecmp(user, CC->usersupp.fullname))) {
297 cprintf("550 <%s> is not your address.\n", SMTP->from);
298 strcpy(SMTP->from, "");
303 /* Otherwise, make sure outsiders aren't trying to forge mail from
307 cvt = convert_internet_address(user, node, SMTP->from);
308 lprintf(9, "cvt=%d, citaddr=<%s@%s>\n", cvt, user, node);
309 if (!strcasecmp(node, config.c_nodename)) { /* FIX use fcn */
310 cprintf("550 You must log in to send mail from %s\n",
312 strcpy(SMTP->from, "");
317 cprintf("250 Sender ok. Groovy.\n");
323 * Implements the "RCPT To:" command
325 void smtp_rcpt(char *argbuf) {
330 if (strlen(SMTP->from) == 0) {
331 cprintf("503 MAIL first, then RCPT. Duh.\n");
335 if (strlen(SMTP->recipient) > 0) {
336 cprintf("550 Only one recipient allowed (FIX)\n");
340 if (strncasecmp(argbuf, "To:", 3)) {
341 cprintf("501 Syntax error\n");
345 strcpy(SMTP->recipient, &argbuf[3]);
346 striplt(SMTP->recipient);
348 cvt = convert_internet_address(user, node, SMTP->recipient);
350 case rfc822_address_locally_validated:
351 cprintf("250 %s is a valid recipient.\n", user);
353 case rfc822_no_such_user:
354 cprintf("550 %s: no such user\n", SMTP->recipient);
355 strcpy(SMTP->recipient, "");
359 strcpy(SMTP->recipient, "");
360 cprintf("599 Unknown error (FIX)\n");
367 * Implements the DATA command
369 void smtp_data(void) {
372 if (strlen(SMTP->from) == 0) {
373 cprintf("503 Need MAIL command first.\n");
377 if (SMTP->number_of_recipients < 1) {
378 cprintf("503 Need RCPT command first.\n");
382 cprintf("354 Transmit message now; terminate with '.' by itself\n");
383 body = CtdlReadMessageBody(".", config.c_maxmsglen);
385 cprintf("550 Unable to save message text: internal error.\n");
390 cprintf("599 command unfinished\n");
397 * Main command loop for SMTP sessions.
399 void smtp_command_loop(void) {
403 memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
404 if (client_gets(cmdbuf) < 1) {
405 lprintf(3, "SMTP socket is broken. Ending session.\n");
409 lprintf(5, "citserver[%3d]: %s\n", CC->cs_pid, cmdbuf);
410 while (strlen(cmdbuf) < 5) strcat(cmdbuf, " ");
412 if (SMTP->command_state == smtp_user) {
413 smtp_get_user(cmdbuf);
416 else if (SMTP->command_state == smtp_password) {
417 smtp_get_pass(cmdbuf);
420 else if (!strncasecmp(cmdbuf, "AUTH", 4)) {
421 smtp_auth(&cmdbuf[5]);
424 else if (!strncasecmp(cmdbuf, "DATA", 4)) {
428 else if (!strncasecmp(cmdbuf, "EHLO", 4)) {
429 smtp_hello(&cmdbuf[5], 1);
432 else if (!strncasecmp(cmdbuf, "EXPN", 4)) {
433 smtp_expn(&cmdbuf[5]);
436 else if (!strncasecmp(cmdbuf, "HELO", 4)) {
437 smtp_hello(&cmdbuf[5], 0);
440 else if (!strncasecmp(cmdbuf, "HELP", 4)) {
444 else if (!strncasecmp(cmdbuf, "MAIL", 4)) {
445 smtp_mail(&cmdbuf[5]);
448 else if (!strncasecmp(cmdbuf, "NOOP", 4)) {
449 cprintf("250 This command successfully did nothing.\n");
452 else if (!strncasecmp(cmdbuf, "QUIT", 4)) {
453 cprintf("221 Goodbye...\n");
458 else if (!strncasecmp(cmdbuf, "RCPT", 4)) {
459 smtp_rcpt(&cmdbuf[5]);
462 else if (!strncasecmp(cmdbuf, "RSET", 4)) {
466 else if (!strncasecmp(cmdbuf, "VRFY", 4)) {
467 smtp_vrfy(&cmdbuf[5]);
471 cprintf("500 I'm afraid I can't do that, Dave.\n");
478 char *Dynamic_Module_Init(void)
480 SYM_SMTP = CtdlGetDynamicSymbol();
481 CtdlRegisterServiceHook(SMTP_PORT,