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- RCPT\n");
96 cprintf("214- RSET\n");
97 cprintf("214- VRFY\n");
98 cprintf("214 I could tell you more, but then I'd have to kill you.\n");
105 void smtp_get_user(char *argbuf) {
109 decode_base64(username, argbuf);
110 lprintf(9, "Trying <%s>\n", username);
111 if (CtdlLoginExistingUser(username) == login_ok) {
112 encode_base64(buf, "Password:");
113 cprintf("334 %s\n", buf);
114 SMTP->command_state = smtp_password;
117 cprintf("500 No such user.\n");
118 SMTP->command_state = smtp_command;
126 void smtp_get_pass(char *argbuf) {
129 decode_base64(password, argbuf);
130 lprintf(9, "Trying <%s>\n", password);
131 if (CtdlTryPassword(password) == pass_ok) {
132 cprintf("235 Authentication successful.\n");
133 lprintf(9, "SMTP auth login successful\n");
136 cprintf("500 Authentication failed.\n");
138 SMTP->command_state = smtp_command;
145 void smtp_auth(char *argbuf) {
148 if (strncasecmp(argbuf, "login", 5) ) {
149 cprintf("550 We only support LOGIN authentication.\n");
153 if (strlen(argbuf) >= 7) {
154 smtp_get_user(&argbuf[6]);
158 encode_base64(buf, "Username:");
159 cprintf("334 %s\n", buf);
160 SMTP->command_state = smtp_user;
166 * Back end for smtp_vrfy() command
168 void smtp_vrfy_backend(struct usersupp *us) {
170 if (!fuzzy_match(us, SMTP->vrfy_match)) {
172 memcpy(&SMTP->vrfy_buffer, us, sizeof(struct usersupp));
178 * Implements the VRFY (verify user name) command.
179 * Performs fuzzy match on full user names.
181 void smtp_vrfy(char *argbuf) {
182 SMTP->vrfy_count = 0;
183 strcpy(SMTP->vrfy_match, argbuf);
184 ForEachUser(smtp_vrfy_backend);
186 if (SMTP->vrfy_count < 1) {
187 cprintf("550 String does not match anything.\n");
189 else if (SMTP->vrfy_count == 1) {
190 cprintf("250 %s <cit%ld@%s>\n",
191 SMTP->vrfy_buffer.fullname,
192 SMTP->vrfy_buffer.usernum,
195 else if (SMTP->vrfy_count > 1) {
196 cprintf("553 Request ambiguous: %d users matched.\n",
205 * Back end for smtp_expn() command
207 void smtp_expn_backend(struct usersupp *us) {
209 if (!fuzzy_match(us, SMTP->vrfy_match)) {
211 if (SMTP->vrfy_count >= 1) {
212 cprintf("250-%s <cit%ld@%s>\n",
213 SMTP->vrfy_buffer.fullname,
214 SMTP->vrfy_buffer.usernum,
219 memcpy(&SMTP->vrfy_buffer, us, sizeof(struct usersupp));
225 * Implements the EXPN (expand user name) command.
226 * Performs fuzzy match on full user names.
228 void smtp_expn(char *argbuf) {
229 SMTP->vrfy_count = 0;
230 strcpy(SMTP->vrfy_match, argbuf);
231 ForEachUser(smtp_expn_backend);
233 if (SMTP->vrfy_count < 1) {
234 cprintf("550 String does not match anything.\n");
236 else if (SMTP->vrfy_count >= 1) {
237 cprintf("250 %s <cit%ld@%s>\n",
238 SMTP->vrfy_buffer.fullname,
239 SMTP->vrfy_buffer.usernum,
246 * Implements the RSET (reset state) command.
247 * Currently this just zeroes out the state buffer. If pointers to data
248 * allocated with mallok() are ever placed in the state buffer, we have to
249 * be sure to phree() them first!
251 void smtp_rset(void) {
252 memset(SMTP, 0, sizeof(struct citsmtp));
253 if (CC->logged_in) logout(CC);
254 cprintf("250 Zap!\n");
260 * Implements the "MAIL From:" command
262 void smtp_mail(char *argbuf) {
267 if (strlen(SMTP->from) != 0) {
268 cprintf("503 Only one sender permitted\n");
272 if (strncasecmp(argbuf, "From:", 5)) {
273 cprintf("501 Syntax error\n");
277 strcpy(SMTP->from, &argbuf[5]);
280 if (strlen(SMTP->from) == 0) {
281 cprintf("501 Empty sender name is not permitted\n");
286 /* If this SMTP connection is from a logged-in user, make sure that
287 * the user only sends email from his/her own address.
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 /* Otherwise, make sure outsiders aren't trying to forge mail from
303 cvt = convert_internet_address(user, node, SMTP->from);
304 lprintf(9, "cvt=%d, citaddr=<%s@%s>\n", cvt, user, node);
305 if (!strcasecmp(node, config.c_nodename)) { /* FIX use fcn */
306 cprintf("550 You must log in to send mail from %s\n",
312 cprintf("250 Sender ok. Groovy.\n");
318 * Implements the "RCPT To:" command
320 void smtp_rcpt(char *argbuf) {
322 if (strlen(SMTP->from) == 0) {
323 cprintf("503 MAIL first, then RCPT. Duh.\n");
327 if (strncasecmp(argbuf, "To:", 3)) {
328 cprintf("501 Syntax error\n");
332 cprintf("599 this is unfinished\n");
339 * Main command loop for SMTP sessions.
341 void smtp_command_loop(void) {
345 memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
346 if (client_gets(cmdbuf) < 1) {
347 lprintf(3, "SMTP socket is broken. Ending session.\n");
351 lprintf(5, "citserver[%3d]: %s\n", CC->cs_pid, cmdbuf);
352 while (strlen(cmdbuf) < 5) strcat(cmdbuf, " ");
354 if (SMTP->command_state == smtp_user) {
355 smtp_get_user(cmdbuf);
358 else if (SMTP->command_state == smtp_password) {
359 smtp_get_pass(cmdbuf);
362 else if (!strncasecmp(cmdbuf, "AUTH", 4)) {
363 smtp_auth(&cmdbuf[5]);
366 else if (!strncasecmp(cmdbuf, "EHLO", 4)) {
367 smtp_hello(&cmdbuf[5], 1);
370 else if (!strncasecmp(cmdbuf, "EXPN", 4)) {
371 smtp_expn(&cmdbuf[5]);
374 else if (!strncasecmp(cmdbuf, "HELO", 4)) {
375 smtp_hello(&cmdbuf[5], 0);
378 else if (!strncasecmp(cmdbuf, "HELP", 4)) {
382 else if (!strncasecmp(cmdbuf, "MAIL", 4)) {
383 smtp_mail(&cmdbuf[5]);
386 else if (!strncasecmp(cmdbuf, "NOOP", 4)) {
387 cprintf("250 This command successfully did nothing.\n");
390 else if (!strncasecmp(cmdbuf, "QUIT", 4)) {
391 cprintf("221 Goodbye...\n");
396 else if (!strncasecmp(cmdbuf, "RCPT", 4)) {
397 smtp_rcpt(&cmdbuf[5]);
400 else if (!strncasecmp(cmdbuf, "RSET", 4)) {
404 else if (!strncasecmp(cmdbuf, "VRFY", 4)) {
405 smtp_vrfy(&cmdbuf[5]);
409 cprintf("500 I'm afraid I can't do that, Dave.\n");
416 char *Dynamic_Module_Init(void)
418 SYM_SMTP = CtdlGetDynamicSymbol();
419 CtdlRegisterServiceHook(SMTP_PORT,