]> code.citadel.org Git - citadel.git/blob - citadel/serv_smtp.c
434e23c0a4584749b282d0d61c361f36af2d273f
[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
35 struct citsmtp {                /* Information about the current session */
36         int command_state;
37         struct usersupp vrfy_buffer;
38         int vrfy_count;
39         char vrfy_match[256];
40         char from[256];
41         char recipient[256];
42         int number_of_recipients;
43 };
44
45 enum {                          /* Command states for login authentication */
46         smtp_command,
47         smtp_user,
48         smtp_password
49 };
50
51 #define SMTP ((struct citsmtp *)CtdlGetUserData(SYM_SMTP))
52
53 long SYM_SMTP;
54
55
56 /*
57  * Here's where our SMTP session begins its happy day.
58  */
59 void smtp_greeting(void) {
60
61         strcpy(CC->cs_clientname, "Citadel SMTP");
62         CC->internal_pgm = 1;
63         CC->cs_flags |= CS_STEALTH;
64         CtdlAllocUserData(SYM_SMTP, sizeof(struct citsmtp));
65
66         cprintf("220 Welcome to the Citadel/UX ESMTP server at %s\n",
67                 config.c_fqdn);
68 }
69
70
71 /*
72  * Implement HELO and EHLO commands.
73  */
74 void smtp_hello(char *argbuf, int is_esmtp) {
75
76         if (!is_esmtp) {
77                 cprintf("250 Greetings and joyous salutations.\n");
78         }
79         else {
80                 cprintf("250-Greetings and joyous salutations.\n");
81                 cprintf("250-HELP\n");
82                 cprintf("250-SIZE %ld\n", config.c_maxmsglen);
83                 cprintf("250 AUTH=LOGIN\n");
84         }
85 }
86
87
88 /*
89  * Implement HELP command.
90  */
91 void smtp_help(void) {
92         cprintf("214-Here's the frequency, Kenneth:\n");
93         cprintf("214-    DATA\n");
94         cprintf("214-    EHLO\n");
95         cprintf("214-    EXPN\n");
96         cprintf("214-    HELO\n");
97         cprintf("214-    HELP\n");
98         cprintf("214-    MAIL\n");
99         cprintf("214-    NOOP\n");
100         cprintf("214-    QUIT\n");
101         cprintf("214-    RCPT\n");
102         cprintf("214-    RSET\n");
103         cprintf("214-    VRFY\n");
104         cprintf("214 I could tell you more, but then I'd have to kill you.\n");
105 }
106
107
108 /*
109  *
110  */
111 void smtp_get_user(char *argbuf) {
112         char buf[256];
113         char username[256];
114
115         decode_base64(username, argbuf);
116         lprintf(9, "Trying <%s>\n", username);
117         if (CtdlLoginExistingUser(username) == login_ok) {
118                 encode_base64(buf, "Password:");
119                 cprintf("334 %s\n", buf);
120                 SMTP->command_state = smtp_password;
121         }
122         else {
123                 cprintf("500 No such user.\n");
124                 SMTP->command_state = smtp_command;
125         }
126 }
127
128
129 /*
130  *
131  */
132 void smtp_get_pass(char *argbuf) {
133         char password[256];
134
135         decode_base64(password, argbuf);
136         lprintf(9, "Trying <%s>\n", password);
137         if (CtdlTryPassword(password) == pass_ok) {
138                 cprintf("235 Authentication successful.\n");
139                 lprintf(9, "SMTP auth login successful\n");
140                 CC->internal_pgm = 0;
141                 CC->cs_flags &= ~CS_STEALTH;
142         }
143         else {
144                 cprintf("500 Authentication failed.\n");
145         }
146         SMTP->command_state = smtp_command;
147 }
148
149
150 /*
151  *
152  */
153 void smtp_auth(char *argbuf) {
154         char buf[256];
155
156         if (strncasecmp(argbuf, "login", 5) ) {
157                 cprintf("550 We only support LOGIN authentication.\n");
158                 return;
159         }
160
161         if (strlen(argbuf) >= 7) {
162                 smtp_get_user(&argbuf[6]);
163         }
164
165         else {
166                 encode_base64(buf, "Username:");
167                 cprintf("334 %s\n", buf);
168                 SMTP->command_state = smtp_user;
169         }
170 }
171
172
173 /*
174  * Back end for smtp_vrfy() command
175  */
176 void smtp_vrfy_backend(struct usersupp *us) {
177
178         if (!fuzzy_match(us, SMTP->vrfy_match)) {
179                 ++SMTP->vrfy_count;
180                 memcpy(&SMTP->vrfy_buffer, us, sizeof(struct usersupp));
181         }
182 }
183
184
185 /* 
186  * Implements the VRFY (verify user name) command.
187  * Performs fuzzy match on full user names.
188  */
189 void smtp_vrfy(char *argbuf) {
190         SMTP->vrfy_count = 0;
191         strcpy(SMTP->vrfy_match, argbuf);
192         ForEachUser(smtp_vrfy_backend);
193
194         if (SMTP->vrfy_count < 1) {
195                 cprintf("550 String does not match anything.\n");
196         }
197         else if (SMTP->vrfy_count == 1) {
198                 cprintf("250 %s <cit%ld@%s>\n",
199                         SMTP->vrfy_buffer.fullname,
200                         SMTP->vrfy_buffer.usernum,
201                         config.c_fqdn);
202         }
203         else if (SMTP->vrfy_count > 1) {
204                 cprintf("553 Request ambiguous: %d users matched.\n",
205                         SMTP->vrfy_count);
206         }
207
208 }
209
210
211
212 /*
213  * Back end for smtp_expn() command
214  */
215 void smtp_expn_backend(struct usersupp *us) {
216
217         if (!fuzzy_match(us, SMTP->vrfy_match)) {
218
219                 if (SMTP->vrfy_count >= 1) {
220                         cprintf("250-%s <cit%ld@%s>\n",
221                                 SMTP->vrfy_buffer.fullname,
222                                 SMTP->vrfy_buffer.usernum,
223                                 config.c_fqdn);
224                 }
225
226                 ++SMTP->vrfy_count;
227                 memcpy(&SMTP->vrfy_buffer, us, sizeof(struct usersupp));
228         }
229 }
230
231
232 /* 
233  * Implements the EXPN (expand user name) command.
234  * Performs fuzzy match on full user names.
235  */
236 void smtp_expn(char *argbuf) {
237         SMTP->vrfy_count = 0;
238         strcpy(SMTP->vrfy_match, argbuf);
239         ForEachUser(smtp_expn_backend);
240
241         if (SMTP->vrfy_count < 1) {
242                 cprintf("550 String does not match anything.\n");
243         }
244         else if (SMTP->vrfy_count >= 1) {
245                 cprintf("250 %s <cit%ld@%s>\n",
246                         SMTP->vrfy_buffer.fullname,
247                         SMTP->vrfy_buffer.usernum,
248                         config.c_fqdn);
249         }
250 }
251
252
253 /*
254  * Implements the RSET (reset state) command.
255  * Currently this just zeroes out the state buffer.  If pointers to data
256  * allocated with mallok() are ever placed in the state buffer, we have to
257  * be sure to phree() them first!
258  */
259 void smtp_rset(void) {
260         memset(SMTP, 0, sizeof(struct citsmtp));
261         if (CC->logged_in) logout(CC);
262         cprintf("250 Zap!\n");
263 }
264
265
266
267 /*
268  * Implements the "MAIL From:" command
269  */
270 void smtp_mail(char *argbuf) {
271         char user[256];
272         char node[256];
273         int cvt;
274
275         if (strlen(SMTP->from) != 0) {
276                 cprintf("503 Only one sender permitted\n");
277                 return;
278         }
279
280         if (strncasecmp(argbuf, "From:", 5)) {
281                 cprintf("501 Syntax error\n");
282                 return;
283         }
284
285         strcpy(SMTP->from, &argbuf[5]);
286         striplt(SMTP->from);
287
288         if (strlen(SMTP->from) == 0) {
289                 cprintf("501 Empty sender name is not permitted\n");
290                 return;
291         }
292
293
294         /* If this SMTP connection is from a logged-in user, make sure that
295          * the user only sends email from his/her own address.
296          */
297         if (CC->logged_in) {
298                 cvt = convert_internet_address(user, node, SMTP->from);
299                 lprintf(9, "cvt=%d, citaddr=<%s@%s>\n", cvt, user, node);
300                 if ( (cvt != 0) || (strcasecmp(user, CC->usersupp.fullname))) {
301                         cprintf("550 <%s> is not your address.\n", SMTP->from);
302                         strcpy(SMTP->from, "");
303                         return;
304                 }
305         }
306
307         /* Otherwise, make sure outsiders aren't trying to forge mail from
308          * this system.
309          */
310         else {
311                 cvt = convert_internet_address(user, node, SMTP->from);
312                 lprintf(9, "cvt=%d, citaddr=<%s@%s>\n", cvt, user, node);
313                 if (!strcasecmp(node, config.c_nodename)) { /* FIX use fcn */
314                         cprintf("550 You must log in to send mail from %s\n",
315                                 config.c_fqdn);
316                         strcpy(SMTP->from, "");
317                         return;
318                 }
319         }
320
321         cprintf("250 Sender ok.  Groovy.\n");
322 }
323
324
325
326 /*
327  * Implements the "RCPT To:" command
328  */
329 void smtp_rcpt(char *argbuf) {
330         int cvt;
331         char user[256];
332         char node[256];
333
334         if (strlen(SMTP->from) == 0) {
335                 cprintf("503 MAIL first, then RCPT.  Duh.\n");
336                 return;
337         }
338
339         if (strlen(SMTP->recipient) > 0) {
340                 cprintf("550 Only one recipient allowed (FIX)\n");
341                 return;
342         }
343
344         if (strncasecmp(argbuf, "To:", 3)) {
345                 cprintf("501 Syntax error\n");
346                 return;
347         }
348
349         strcpy(SMTP->recipient, &argbuf[3]);
350         striplt(SMTP->recipient);
351
352         cvt = convert_internet_address(user, node, SMTP->recipient);
353         switch(cvt) {
354                 case rfc822_address_locally_validated:
355                         cprintf("250 %s is a valid recipient.\n", user);
356                         return;
357                 case rfc822_no_such_user:
358                         cprintf("550 %s: no such user\n", SMTP->recipient);
359                         strcpy(SMTP->recipient, "");
360                         return;
361         }
362
363         strcpy(SMTP->recipient, "");
364         cprintf("599 Unknown error (FIX)\n");
365 }
366
367
368
369
370 /*
371  * Implements the DATA command
372  */
373 void smtp_data(void) {
374         char *body;
375         struct CtdlMessage *msg;
376
377 /*
378         if (strlen(SMTP->from) == 0) {
379                 cprintf("503 Need MAIL command first.\n");
380                 return;
381         }
382
383         if (SMTP->number_of_recipients < 1) {
384                 cprintf("503 Need RCPT command first.\n");
385                 return;
386         }
387
388 */
389
390         cprintf("354 Transmit message now; terminate with '.' by itself\n");
391         body = CtdlReadMessageBody(".", config.c_maxmsglen);
392         if (body == NULL) {
393                 cprintf("550 Unable to save message text: internal error.\n");
394                 return;
395         }
396
397         fprintf(stderr, "Converting message...\n");
398         msg = convert_internet_message(body);
399         phree(body);
400
401         CtdlSaveMsg(msg, "", BASEROOM, MES_LOCAL, 1);   /* FIX temporary */
402         CtdlFreeMessage(msg);
403
404         cprintf("599 command unfinished but message saved\n");
405 }
406
407
408
409
410 /* 
411  * Main command loop for SMTP sessions.
412  */
413 void smtp_command_loop(void) {
414         char cmdbuf[256];
415
416         time(&CC->lastcmd);
417         memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
418         if (client_gets(cmdbuf) < 1) {
419                 lprintf(3, "SMTP socket is broken.  Ending session.\n");
420                 CC->kill_me = 1;
421                 return;
422         }
423         lprintf(5, "citserver[%3d]: %s\n", CC->cs_pid, cmdbuf);
424         while (strlen(cmdbuf) < 5) strcat(cmdbuf, " ");
425
426         if (SMTP->command_state == smtp_user) {
427                 smtp_get_user(cmdbuf);
428         }
429
430         else if (SMTP->command_state == smtp_password) {
431                 smtp_get_pass(cmdbuf);
432         }
433
434         else if (!strncasecmp(cmdbuf, "AUTH", 4)) {
435                 smtp_auth(&cmdbuf[5]);
436         }
437
438         else if (!strncasecmp(cmdbuf, "DATA", 4)) {
439                 smtp_data();
440         }
441
442         else if (!strncasecmp(cmdbuf, "EHLO", 4)) {
443                 smtp_hello(&cmdbuf[5], 1);
444         }
445
446         else if (!strncasecmp(cmdbuf, "EXPN", 4)) {
447                 smtp_expn(&cmdbuf[5]);
448         }
449
450         else if (!strncasecmp(cmdbuf, "HELO", 4)) {
451                 smtp_hello(&cmdbuf[5], 0);
452         }
453
454         else if (!strncasecmp(cmdbuf, "HELP", 4)) {
455                 smtp_help();
456         }
457
458         else if (!strncasecmp(cmdbuf, "MAIL", 4)) {
459                 smtp_mail(&cmdbuf[5]);
460         }
461
462         else if (!strncasecmp(cmdbuf, "NOOP", 4)) {
463                 cprintf("250 This command successfully did nothing.\n");
464         }
465
466         else if (!strncasecmp(cmdbuf, "QUIT", 4)) {
467                 cprintf("221 Goodbye...\n");
468                 CC->kill_me = 1;
469                 return;
470                 }
471
472         else if (!strncasecmp(cmdbuf, "RCPT", 4)) {
473                 smtp_rcpt(&cmdbuf[5]);
474         }
475
476         else if (!strncasecmp(cmdbuf, "RSET", 4)) {
477                 smtp_rset();
478         }
479
480         else if (!strncasecmp(cmdbuf, "VRFY", 4)) {
481                 smtp_vrfy(&cmdbuf[5]);
482         }
483
484         else {
485                 cprintf("500 I'm afraid I can't do that, Dave.\n");
486         }
487
488 }
489
490
491
492 char *Dynamic_Module_Init(void)
493 {
494         SYM_SMTP = CtdlGetDynamicSymbol();
495         CtdlRegisterServiceHook(SMTP_PORT,
496                                 smtp_greeting,
497                                 smtp_command_loop);
498         return "$Id$";
499 }