]> code.citadel.org Git - citadel.git/blob - citadel/serv_smtp.c
* Implemented most of MAIL FROM:
[citadel.git] / citadel / serv_smtp.c
1 /* $Id$ */
2
3 #define SMTP_PORT       2525
4
5 #include "sysdep.h"
6 #include <stdlib.h>
7 #include <unistd.h>
8 #include <stdio.h>
9 #include <fcntl.h>
10 #include <signal.h>
11 #include <pwd.h>
12 #include <errno.h>
13 #include <sys/types.h>
14 #include <sys/time.h>
15 #include <sys/wait.h>
16 #include <string.h>
17 #include <limits.h>
18 #include "citadel.h"
19 #include "server.h"
20 #include <time.h>
21 #include "sysdep_decls.h"
22 #include "citserver.h"
23 #include "support.h"
24 #include "config.h"
25 #include "dynloader.h"
26 #include "room_ops.h"
27 #include "user_ops.h"
28 #include "policy.h"
29 #include "database.h"
30 #include "msgbase.h"
31 #include "tools.h"
32 #include "internet_addressing.h"
33
34 struct citsmtp {
35         int command_state;
36         struct usersupp vrfy_buffer;
37         int vrfy_count;
38         char vrfy_match[256];
39         char from[256];
40 };
41
42 enum {
43         smtp_command,
44         smtp_user,
45         smtp_password
46 };
47
48 #define SMTP ((struct citsmtp *)CtdlGetUserData(SYM_SMTP))
49
50 long SYM_SMTP;
51
52
53 /*
54  * Here's where our SMTP session begins its happy day.
55  */
56 void smtp_greeting(void) {
57
58         strcpy(CC->cs_clientname, "Citadel SMTP");
59         CtdlAllocUserData(SYM_SMTP, sizeof(struct citsmtp));
60
61         cprintf("220 Welcome to the Citadel/UX ESMTP server at %s\n",
62                 config.c_fqdn);
63 }
64
65
66 /*
67  * Implement HELO and EHLO commands.
68  */
69 void smtp_hello(char *argbuf, int is_esmtp) {
70
71         if (!is_esmtp) {
72                 cprintf("250 Greetings and joyous salutations.\n");
73         }
74         else {
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");
79         }
80 }
81
82
83 /*
84  * Implement HELP command.
85  */
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");
98 }
99
100
101 /*
102  *
103  */
104 void smtp_get_user(char *argbuf) {
105         char buf[256];
106         char username[256];
107
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;
114         }
115         else {
116                 cprintf("500 No such user.\n");
117                 SMTP->command_state = smtp_command;
118         }
119 }
120
121
122 /*
123  *
124  */
125 void smtp_get_pass(char *argbuf) {
126         char password[256];
127
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");
133         }
134         else {
135                 cprintf("500 Authentication failed.\n");
136         }
137         SMTP->command_state = smtp_command;
138 }
139
140
141 /*
142  *
143  */
144 void smtp_auth(char *argbuf) {
145         char buf[256];
146
147         if (strncasecmp(argbuf, "login", 5) ) {
148                 cprintf("550 We only support LOGIN authentication.\n");
149                 return;
150         }
151
152         if (strlen(argbuf) >= 7) {
153                 smtp_get_user(&argbuf[6]);
154         }
155
156         else {
157                 encode_base64(buf, "Username:");
158                 cprintf("334 %s\n", buf);
159                 SMTP->command_state = smtp_user;
160         }
161 }
162
163
164 /*
165  * Back end for smtp_vrfy() command
166  */
167 void smtp_vrfy_backend(struct usersupp *us) {
168
169         if (!fuzzy_match(us, SMTP->vrfy_match)) {
170                 ++SMTP->vrfy_count;
171                 memcpy(&SMTP->vrfy_buffer, us, sizeof(struct usersupp));
172         }
173 }
174
175
176 /* 
177  * Implements the VRFY (verify user name) command.
178  * Performs fuzzy match on full user names.
179  */
180 void smtp_vrfy(char *argbuf) {
181         SMTP->vrfy_count = 0;
182         strcpy(SMTP->vrfy_match, argbuf);
183         ForEachUser(smtp_vrfy_backend);
184
185         if (SMTP->vrfy_count < 1) {
186                 cprintf("550 String does not match anything.\n");
187         }
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,
192                         config.c_fqdn);
193         }
194         else if (SMTP->vrfy_count > 1) {
195                 cprintf("553 Request ambiguous: %d users matched.\n",
196                         SMTP->vrfy_count);
197         }
198
199 }
200
201
202
203 /*
204  * Back end for smtp_expn() command
205  */
206 void smtp_expn_backend(struct usersupp *us) {
207
208         if (!fuzzy_match(us, SMTP->vrfy_match)) {
209
210                 if (SMTP->vrfy_count >= 1) {
211                         cprintf("250-%s <cit%ld@%s>\n",
212                                 SMTP->vrfy_buffer.fullname,
213                                 SMTP->vrfy_buffer.usernum,
214                                 config.c_fqdn);
215                 }
216
217                 ++SMTP->vrfy_count;
218                 memcpy(&SMTP->vrfy_buffer, us, sizeof(struct usersupp));
219         }
220 }
221
222
223 /* 
224  * Implements the EXPN (expand user name) command.
225  * Performs fuzzy match on full user names.
226  */
227 void smtp_expn(char *argbuf) {
228         SMTP->vrfy_count = 0;
229         strcpy(SMTP->vrfy_match, argbuf);
230         ForEachUser(smtp_expn_backend);
231
232         if (SMTP->vrfy_count < 1) {
233                 cprintf("550 String does not match anything.\n");
234         }
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,
239                         config.c_fqdn);
240         }
241 }
242
243
244 /*
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!
249  */
250 void smtp_rset(void) {
251         memset(SMTP, 0, sizeof(struct citsmtp));
252         if (CC->logged_in) logout(CC);
253         cprintf("250 Zap!\n");
254 }
255
256
257
258 /*
259  * Implements the "MAIL From:" command
260  */
261 void smtp_mail(char *argbuf) {
262         char user[256];
263         char node[256];
264         int cvt;
265
266         if (strlen(SMTP->from) != 0) {
267                 cprintf("503 Only one sender permitted\n");
268                 return;
269         }
270
271         if (strncasecmp(argbuf, "From:", 5)) {
272                 cprintf("501 Syntax error\n");
273                 return;
274         }
275
276         strcpy(SMTP->from, &argbuf[5]);
277         striplt(SMTP->from);
278
279         if (strlen(SMTP->from) == 0) {
280                 cprintf("501 Empty sender name is not permitted\n");
281                 return;
282         }
283
284
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.
287          */
288         if (CC->logged_in) {
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, "");
295                         return;
296                 }
297         }
298
299         cprintf("250 Sender ok.  Groovy.\n");
300 }
301
302
303
304 /* 
305  * Main command loop for SMTP sessions.
306  */
307 void smtp_command_loop(void) {
308         char cmdbuf[256];
309
310         time(&CC->lastcmd);
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");
314                 CC->kill_me = 1;
315                 return;
316         }
317         lprintf(5, "citserver[%3d]: %s\n", CC->cs_pid, cmdbuf);
318         while (strlen(cmdbuf) < 5) strcat(cmdbuf, " ");
319
320         if (SMTP->command_state == smtp_user) {
321                 smtp_get_user(cmdbuf);
322         }
323
324         else if (SMTP->command_state == smtp_password) {
325                 smtp_get_pass(cmdbuf);
326         }
327
328         else if (!strncasecmp(cmdbuf, "AUTH", 4)) {
329                 smtp_auth(&cmdbuf[5]);
330         }
331
332         else if (!strncasecmp(cmdbuf, "EHLO", 4)) {
333                 smtp_hello(&cmdbuf[5], 1);
334         }
335
336         else if (!strncasecmp(cmdbuf, "EXPN", 4)) {
337                 smtp_expn(&cmdbuf[5]);
338         }
339
340         else if (!strncasecmp(cmdbuf, "HELO", 4)) {
341                 smtp_hello(&cmdbuf[5], 0);
342         }
343
344         else if (!strncasecmp(cmdbuf, "HELP", 4)) {
345                 smtp_help();
346         }
347
348         else if (!strncasecmp(cmdbuf, "MAIL", 4)) {
349                 smtp_mail(&cmdbuf[5]);
350         }
351
352         else if (!strncasecmp(cmdbuf, "NOOP", 4)) {
353                 cprintf("250 This command successfully did nothing.\n");
354         }
355
356         else if (!strncasecmp(cmdbuf, "QUIT", 4)) {
357                 cprintf("221 Goodbye...\n");
358                 CC->kill_me = 1;
359                 return;
360                 }
361
362         else if (!strncasecmp(cmdbuf, "RSET", 4)) {
363                 smtp_rset();
364         }
365
366         else if (!strncasecmp(cmdbuf, "VRFY", 4)) {
367                 smtp_vrfy(&cmdbuf[5]);
368         }
369
370         else {
371                 cprintf("500 I'm afraid I can't do that, Dave.\n");
372         }
373
374 }
375
376
377
378 char *Dynamic_Module_Init(void)
379 {
380         SYM_SMTP = CtdlGetDynamicSymbol();
381         CtdlRegisterServiceHook(SMTP_PORT,
382                                 smtp_greeting,
383                                 smtp_command_loop);
384         return "$Id$";
385 }