13 #include <sys/types.h>
21 #include "sysdep_decls.h"
22 #include "citserver.h"
25 #include "dynloader.h"
35 struct usersupp vrfy_buffer;
46 #define SMTP ((struct citsmtp *)CtdlGetUserData(SYM_SMTP))
52 * Here's where our SMTP session begins its happy day.
54 void smtp_greeting(void) {
56 strcpy(CC->cs_clientname, "Citadel SMTP");
57 CtdlAllocUserData(SYM_SMTP, sizeof(struct citsmtp));
59 cprintf("220 Welcome to the Citadel/UX ESMTP server at %s\n",
65 * Implement HELO and EHLO commands.
67 void smtp_hello(char *argbuf, int is_esmtp) {
70 cprintf("250 Greetings and joyous salutations.\n");
73 cprintf("250-Greetings and joyous salutations.\n");
74 cprintf("250-HELP\n");
75 cprintf("250-SIZE %ld\n", config.c_maxmsglen);
76 cprintf("250 AUTH=LOGIN\n");
82 * Implement HELP command.
84 void smtp_help(void) {
85 cprintf("214-Here's the frequency, Kenneth:\n");
86 cprintf("214- EHLO\n");
87 cprintf("214- EXPN\n");
88 cprintf("214- HELO\n");
89 cprintf("214- HELP\n");
90 cprintf("214- NOOP\n");
91 cprintf("214- QUIT\n");
92 cprintf("214- VRFY\n");
93 cprintf("214 I could tell you more, but then I'd have to kill you.\n");
100 void smtp_get_user(char *argbuf) {
104 decode_base64(username, argbuf);
105 lprintf(9, "Trying <%s>\n", username);
106 if (CtdlLoginExistingUser(username) == login_ok) {
107 encode_base64(buf, "Password:");
108 cprintf("334 %s\n", buf);
109 SMTP->command_state = smtp_password;
112 cprintf("500 No such user.\n");
113 SMTP->command_state = smtp_command;
121 void smtp_get_pass(char *argbuf) {
124 decode_base64(password, argbuf);
125 lprintf(9, "Trying <%s>\n", password);
126 if (CtdlTryPassword(password) == pass_ok) {
127 cprintf("235 Authentication successful.\n");
128 lprintf(9, "SMTP auth login successful\n");
131 cprintf("500 Authentication failed.\n");
133 SMTP->command_state = smtp_command;
140 void smtp_auth(char *argbuf) {
143 if (strncasecmp(argbuf, "login", 5) ) {
144 cprintf("550 We only support LOGIN authentication.\n");
148 if (strlen(argbuf) >= 7) {
149 smtp_get_user(&argbuf[6]);
153 encode_base64(buf, "Username:");
154 cprintf("334 %s\n", buf);
155 SMTP->command_state = smtp_user;
161 * Return 0 if a given string fuzzy-matches a Citadel user account
163 * FIX ... this needs to be updated to match any and all ways of addressing
164 * a user. It may even be appropriate to move this out of SMTP and
165 * into the server core.
167 int fuzzy_match(struct usersupp *us, char *matchstring) {
170 for (a=0; a<strlen(us->fullname); ++a) {
171 if (!strncasecmp(&us->fullname[a],
172 matchstring, strlen(matchstring))) {
181 * Back end for smtp_vrfy() command
183 void smtp_vrfy_backend(struct usersupp *us) {
185 if (!fuzzy_match(us, SMTP->vrfy_match)) {
187 memcpy(&SMTP->vrfy_buffer, us, sizeof(struct usersupp));
193 * Implements the VRFY (verify user name) command.
194 * Performs fuzzy match on full user names.
196 void smtp_vrfy(char *argbuf) {
197 SMTP->vrfy_count = 0;
198 strcpy(SMTP->vrfy_match, argbuf);
199 ForEachUser(smtp_vrfy_backend);
201 if (SMTP->vrfy_count < 1) {
202 cprintf("550 String does not match anything.\n");
204 else if (SMTP->vrfy_count == 1) {
205 cprintf("250 %s <cit%ld@%s>\n",
206 SMTP->vrfy_buffer.fullname,
207 SMTP->vrfy_buffer.usernum,
210 else if (SMTP->vrfy_count > 1) {
211 cprintf("553 Request ambiguous: %d users matched.\n",
220 * Back end for smtp_expn() command
222 void smtp_expn_backend(struct usersupp *us) {
224 if (!fuzzy_match(us, SMTP->vrfy_match)) {
226 if (SMTP->vrfy_count >= 1) {
227 cprintf("250-%s <cit%ld@%s>\n",
228 SMTP->vrfy_buffer.fullname,
229 SMTP->vrfy_buffer.usernum,
234 memcpy(&SMTP->vrfy_buffer, us, sizeof(struct usersupp));
240 * Implements the EXPN (expand user name) command.
241 * Performs fuzzy match on full user names.
243 void smtp_expn(char *argbuf) {
244 SMTP->vrfy_count = 0;
245 strcpy(SMTP->vrfy_match, argbuf);
246 ForEachUser(smtp_expn_backend);
248 if (SMTP->vrfy_count < 1) {
249 cprintf("550 String does not match anything.\n");
251 else if (SMTP->vrfy_count >= 1) {
252 cprintf("250 %s <cit%ld@%s>\n",
253 SMTP->vrfy_buffer.fullname,
254 SMTP->vrfy_buffer.usernum,
262 * Main command loop for SMTP sessions.
264 void smtp_command_loop(void) {
268 memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
269 if (client_gets(cmdbuf) < 1) {
270 lprintf(3, "SMTP socket is broken. Ending session.\n");
274 lprintf(5, "citserver[%3d]: %s\n", CC->cs_pid, cmdbuf);
275 while (strlen(cmdbuf) < 5) strcat(cmdbuf, " ");
277 if (SMTP->command_state == smtp_user) {
278 smtp_get_user(cmdbuf);
281 else if (SMTP->command_state == smtp_password) {
282 smtp_get_pass(cmdbuf);
285 else if (!strncasecmp(cmdbuf, "AUTH", 4)) {
286 smtp_auth(&cmdbuf[5]);
289 else if (!strncasecmp(cmdbuf, "EHLO", 4)) {
290 smtp_hello(&cmdbuf[5], 1);
293 else if (!strncasecmp(cmdbuf, "EXPN", 4)) {
294 smtp_expn(&cmdbuf[5]);
297 else if (!strncasecmp(cmdbuf, "HELO", 4)) {
298 smtp_hello(&cmdbuf[5], 0);
301 else if (!strncasecmp(cmdbuf, "HELP", 4)) {
305 else if (!strncasecmp(cmdbuf, "NOOP", 4)) {
306 cprintf("250 This command successfully did nothing.\n");
309 else if (!strncasecmp(cmdbuf, "QUIT", 4)) {
310 cprintf("221 Goodbye...\n");
315 else if (!strncasecmp(cmdbuf, "VRFY", 4)) {
316 smtp_vrfy(&cmdbuf[5]);
320 cprintf("500 I'm afraid I can't do that, Dave.\n");
327 char *Dynamic_Module_Init(void)
329 SYM_SMTP = CtdlGetDynamicSymbol();
330 CtdlRegisterServiceHook(SMTP_PORT,