13 #include <sys/types.h>
21 #include "sysdep_decls.h"
22 #include "citserver.h"
25 #include "dynloader.h"
32 #include "internet_addressing.h"
36 struct usersupp vrfy_buffer;
48 #define SMTP ((struct citsmtp *)CtdlGetUserData(SYM_SMTP))
54 * Here's where our SMTP session begins its happy day.
56 void smtp_greeting(void) {
58 strcpy(CC->cs_clientname, "Citadel SMTP");
59 CtdlAllocUserData(SYM_SMTP, sizeof(struct citsmtp));
61 cprintf("220 Welcome to the Citadel/UX ESMTP server at %s\n",
67 * Implement HELO and EHLO commands.
69 void smtp_hello(char *argbuf, int is_esmtp) {
72 cprintf("250 Greetings and joyous salutations.\n");
75 cprintf("250-Greetings and joyous salutations.\n");
76 cprintf("250-HELP\n");
77 cprintf("250-SIZE %ld\n", config.c_maxmsglen);
78 cprintf("250 AUTH=LOGIN\n");
84 * Implement HELP command.
86 void smtp_help(void) {
87 cprintf("214-Here's the frequency, Kenneth:\n");
88 cprintf("214- EHLO\n");
89 cprintf("214- EXPN\n");
90 cprintf("214- HELO\n");
91 cprintf("214- HELP\n");
92 cprintf("214- MAIL\n");
93 cprintf("214- NOOP\n");
94 cprintf("214- QUIT\n");
95 cprintf("214- RSET\n");
96 cprintf("214- VRFY\n");
97 cprintf("214 I could tell you more, but then I'd have to kill you.\n");
104 void smtp_get_user(char *argbuf) {
108 decode_base64(username, argbuf);
109 lprintf(9, "Trying <%s>\n", username);
110 if (CtdlLoginExistingUser(username) == login_ok) {
111 encode_base64(buf, "Password:");
112 cprintf("334 %s\n", buf);
113 SMTP->command_state = smtp_password;
116 cprintf("500 No such user.\n");
117 SMTP->command_state = smtp_command;
125 void smtp_get_pass(char *argbuf) {
128 decode_base64(password, argbuf);
129 lprintf(9, "Trying <%s>\n", password);
130 if (CtdlTryPassword(password) == pass_ok) {
131 cprintf("235 Authentication successful.\n");
132 lprintf(9, "SMTP auth login successful\n");
135 cprintf("500 Authentication failed.\n");
137 SMTP->command_state = smtp_command;
144 void smtp_auth(char *argbuf) {
147 if (strncasecmp(argbuf, "login", 5) ) {
148 cprintf("550 We only support LOGIN authentication.\n");
152 if (strlen(argbuf) >= 7) {
153 smtp_get_user(&argbuf[6]);
157 encode_base64(buf, "Username:");
158 cprintf("334 %s\n", buf);
159 SMTP->command_state = smtp_user;
165 * Back end for smtp_vrfy() command
167 void smtp_vrfy_backend(struct usersupp *us) {
169 if (!fuzzy_match(us, SMTP->vrfy_match)) {
171 memcpy(&SMTP->vrfy_buffer, us, sizeof(struct usersupp));
177 * Implements the VRFY (verify user name) command.
178 * Performs fuzzy match on full user names.
180 void smtp_vrfy(char *argbuf) {
181 SMTP->vrfy_count = 0;
182 strcpy(SMTP->vrfy_match, argbuf);
183 ForEachUser(smtp_vrfy_backend);
185 if (SMTP->vrfy_count < 1) {
186 cprintf("550 String does not match anything.\n");
188 else if (SMTP->vrfy_count == 1) {
189 cprintf("250 %s <cit%ld@%s>\n",
190 SMTP->vrfy_buffer.fullname,
191 SMTP->vrfy_buffer.usernum,
194 else if (SMTP->vrfy_count > 1) {
195 cprintf("553 Request ambiguous: %d users matched.\n",
204 * Back end for smtp_expn() command
206 void smtp_expn_backend(struct usersupp *us) {
208 if (!fuzzy_match(us, SMTP->vrfy_match)) {
210 if (SMTP->vrfy_count >= 1) {
211 cprintf("250-%s <cit%ld@%s>\n",
212 SMTP->vrfy_buffer.fullname,
213 SMTP->vrfy_buffer.usernum,
218 memcpy(&SMTP->vrfy_buffer, us, sizeof(struct usersupp));
224 * Implements the EXPN (expand user name) command.
225 * Performs fuzzy match on full user names.
227 void smtp_expn(char *argbuf) {
228 SMTP->vrfy_count = 0;
229 strcpy(SMTP->vrfy_match, argbuf);
230 ForEachUser(smtp_expn_backend);
232 if (SMTP->vrfy_count < 1) {
233 cprintf("550 String does not match anything.\n");
235 else if (SMTP->vrfy_count >= 1) {
236 cprintf("250 %s <cit%ld@%s>\n",
237 SMTP->vrfy_buffer.fullname,
238 SMTP->vrfy_buffer.usernum,
245 * Implements the RSET (reset state) command.
246 * Currently this just zeroes out the state buffer. If pointers to data
247 * allocated with mallok() are ever placed in the state buffer, we have to
248 * be sure to phree() them first!
250 void smtp_rset(void) {
251 memset(SMTP, 0, sizeof(struct citsmtp));
252 if (CC->logged_in) logout(CC);
253 cprintf("250 Zap!\n");
259 * Implements the "MAIL From:" command
261 void smtp_mail(char *argbuf) {
266 if (strlen(SMTP->from) != 0) {
267 cprintf("503 Only one sender permitted\n");
271 if (strncasecmp(argbuf, "From:", 5)) {
272 cprintf("501 Syntax error\n");
276 strcpy(SMTP->from, &argbuf[5]);
279 if (strlen(SMTP->from) == 0) {
280 cprintf("501 Empty sender name is not permitted\n");
285 /* If this SMTP connection is from a logged-in user, make sure that
286 * the user only sends email from his/her own address.
289 lprintf(9, "Me-checking <%s>\n", SMTP->from);
290 cvt = convert_internet_address(user, node, SMTP->from);
291 lprintf(9, "cvt=%d, citaddr=<%s@%s>\n", cvt, user, node);
292 if ( (cvt != 0) || (strcasecmp(user, CC->usersupp.fullname))) {
293 cprintf("550 <%s> is not your address.\n", SMTP->from);
294 strcpy(SMTP->from, "");
299 cprintf("250 Sender ok. Groovy.\n");
305 * Main command loop for SMTP sessions.
307 void smtp_command_loop(void) {
311 memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
312 if (client_gets(cmdbuf) < 1) {
313 lprintf(3, "SMTP socket is broken. Ending session.\n");
317 lprintf(5, "citserver[%3d]: %s\n", CC->cs_pid, cmdbuf);
318 while (strlen(cmdbuf) < 5) strcat(cmdbuf, " ");
320 if (SMTP->command_state == smtp_user) {
321 smtp_get_user(cmdbuf);
324 else if (SMTP->command_state == smtp_password) {
325 smtp_get_pass(cmdbuf);
328 else if (!strncasecmp(cmdbuf, "AUTH", 4)) {
329 smtp_auth(&cmdbuf[5]);
332 else if (!strncasecmp(cmdbuf, "EHLO", 4)) {
333 smtp_hello(&cmdbuf[5], 1);
336 else if (!strncasecmp(cmdbuf, "EXPN", 4)) {
337 smtp_expn(&cmdbuf[5]);
340 else if (!strncasecmp(cmdbuf, "HELO", 4)) {
341 smtp_hello(&cmdbuf[5], 0);
344 else if (!strncasecmp(cmdbuf, "HELP", 4)) {
348 else if (!strncasecmp(cmdbuf, "MAIL", 4)) {
349 smtp_mail(&cmdbuf[5]);
352 else if (!strncasecmp(cmdbuf, "NOOP", 4)) {
353 cprintf("250 This command successfully did nothing.\n");
356 else if (!strncasecmp(cmdbuf, "QUIT", 4)) {
357 cprintf("221 Goodbye...\n");
362 else if (!strncasecmp(cmdbuf, "RSET", 4)) {
366 else if (!strncasecmp(cmdbuf, "VRFY", 4)) {
367 smtp_vrfy(&cmdbuf[5]);
371 cprintf("500 I'm afraid I can't do that, Dave.\n");
378 char *Dynamic_Module_Init(void)
380 SYM_SMTP = CtdlGetDynamicSymbol();
381 CtdlRegisterServiceHook(SMTP_PORT,