]> code.citadel.org Git - citadel.git/blob - citadel/serv_smtp.c
* serv_smtp: implemented RFC821 "VRFY" and "EXPN" commands
[citadel.git] / citadel / serv_smtp.c
1 /* $Id$ */
2 #include "sysdep.h"
3 #include <stdlib.h>
4 #include <unistd.h>
5 #include <stdio.h>
6 #include <fcntl.h>
7 #include <signal.h>
8 #include <pwd.h>
9 #include <errno.h>
10 #include <sys/types.h>
11 #include <sys/time.h>
12 #include <sys/wait.h>
13 #include <string.h>
14 #include <limits.h>
15 #include "citadel.h"
16 #include "server.h"
17 #include <time.h>
18 #include "sysdep_decls.h"
19 #include "citserver.h"
20 #include "support.h"
21 #include "config.h"
22 #include "dynloader.h"
23 #include "room_ops.h"
24 #include "user_ops.h"
25 #include "policy.h"
26 #include "database.h"
27 #include "msgbase.h"
28 #include "tools.h"
29
30 struct citsmtp {
31         int command_state;
32         struct usersupp vrfy_buffer;
33         int vrfy_count;
34         char vrfy_match[256];
35 };
36
37 enum {
38         smtp_command,
39         smtp_user,
40         smtp_password
41 };
42
43 #define SMTP ((struct citsmtp *)CtdlGetUserData(SYM_SMTP))
44
45 long SYM_SMTP;
46
47
48 /*
49  * Here's where our SMTP session begins its happy day.
50  */
51 void smtp_greeting(void) {
52
53         strcpy(CC->cs_clientname, "Citadel SMTP");
54         CtdlAllocUserData(SYM_SMTP, sizeof(struct citsmtp));
55
56         cprintf("220 Welcome to the Citadel/UX ESMTP server at %s\n",
57                 config.c_fqdn);
58 }
59
60
61 /*
62  * Implement HELO and EHLO commands.
63  */
64 void smtp_hello(char *argbuf, int is_esmtp) {
65
66         if (!is_esmtp) {
67                 cprintf("250 Greetings and joyous salutations.\n");
68         }
69         else {
70                 cprintf("250-Greetings and joyous salutations.\n");
71                 cprintf("250-HELP\n");
72                 cprintf("250-SIZE %ld\n", config.c_maxmsglen);
73                 cprintf("250 AUTH=LOGIN\n");
74         }
75 }
76
77
78 /*
79  * Implement HELP command.
80  */
81 void smtp_help(void) {
82         cprintf("214-Here's the frequency, Kenneth:\n");
83         cprintf("214-    EHLO\n");
84         cprintf("214-    EXPN\n");
85         cprintf("214-    HELO\n");
86         cprintf("214-    HELP\n");
87         cprintf("214-    NOOP\n");
88         cprintf("214-    QUIT\n");
89         cprintf("214-    VRFY\n");
90         cprintf("214 I could tell you more, but then I'd have to kill you.\n");
91 }
92
93
94 /*
95  *
96  */
97 void smtp_get_user(char *argbuf) {
98         char buf[256];
99         char username[256];
100
101         decode_base64(username, argbuf);
102         lprintf(9, "Trying <%s>\n", username);
103         if (CtdlLoginExistingUser(username) == login_ok) {
104                 encode_base64(buf, "Password:");
105                 cprintf("334 %s\n", buf);
106                 SMTP->command_state = smtp_password;
107         }
108         else {
109                 cprintf("500 No such user.\n");
110                 SMTP->command_state = smtp_command;
111         }
112 }
113
114
115 /*
116  *
117  */
118 void smtp_get_pass(char *argbuf) {
119         char password[256];
120
121         decode_base64(password, argbuf);
122         lprintf(9, "Trying <%s>\n", password);
123         if (CtdlTryPassword(password) == pass_ok) {
124                 cprintf("235 Authentication successful.\n");
125                 lprintf(9, "SMTP auth login successful\n");
126         }
127         else {
128                 cprintf("500 Authentication failed.\n");
129         }
130         SMTP->command_state = smtp_command;
131 }
132
133
134 /*
135  *
136  */
137 void smtp_auth(char *argbuf) {
138         char buf[256];
139
140         if (strncasecmp(argbuf, "login", 5) ) {
141                 cprintf("500 We only support LOGIN authentication.\n");
142                 return;
143         }
144
145         if (strlen(argbuf) >= 7) {
146                 smtp_get_user(&argbuf[6]);
147         }
148
149         else {
150                 encode_base64(buf, "Username:");
151                 cprintf("334 %s\n", buf);
152                 SMTP->command_state = smtp_user;
153         }
154 }
155
156
157 /*
158  * Return 0 if a given string fuzzy-matches a Citadel user account
159  *
160  * FIX ... this needs to be updated to match any and all ways of addressing
161  *         a user.  It may even be appropriate to move this out of SMTP and
162  *         into the server core.
163  */
164 int fuzzy_match(struct usersupp *us, char *matchstring) {
165         int a;
166
167         for (a=0; a<strlen(us->fullname); ++a) {
168                 if (!strncasecmp(&us->fullname[a],
169                    matchstring, strlen(matchstring))) {
170                         return 0;
171                 }
172         }
173         return -1;
174 }
175
176
177 /*
178  * Back end for smtp_vrfy() command
179  */
180 void smtp_vrfy_backend(struct usersupp *us) {
181
182         if (!fuzzy_match(us, SMTP->vrfy_match)) {
183                 ++SMTP->vrfy_count;
184                 memcpy(&SMTP->vrfy_buffer, us, sizeof(struct usersupp));
185         }
186 }
187
188
189 /* 
190  * Implements the VRFY (verify user name) command.
191  * Performs fuzzy match on full user names.
192  */
193 void smtp_vrfy(char *argbuf) {
194         SMTP->vrfy_count = 0;
195         strcpy(SMTP->vrfy_match, argbuf);
196         ForEachUser(smtp_vrfy_backend);
197
198         if (SMTP->vrfy_count < 1) {
199                 cprintf("550 String does not match anything.\n");
200         }
201         else if (SMTP->vrfy_count == 1) {
202                 cprintf("250 %s <cit%ld@%s>\n",
203                         SMTP->vrfy_buffer.fullname,
204                         SMTP->vrfy_buffer.usernum,
205                         config.c_fqdn);
206         }
207         else if (SMTP->vrfy_count > 1) {
208                 cprintf("553 Request ambiguous: %d users matched.\n",
209                         SMTP->vrfy_count);
210         }
211
212 }
213
214
215
216 /*
217  * Back end for smtp_expn() command
218  */
219 void smtp_expn_backend(struct usersupp *us) {
220
221         if (!fuzzy_match(us, SMTP->vrfy_match)) {
222
223                 if (SMTP->vrfy_count >= 1) {
224                         cprintf("250-%s <cit%ld@%s>\n",
225                                 SMTP->vrfy_buffer.fullname,
226                                 SMTP->vrfy_buffer.usernum,
227                                 config.c_fqdn);
228                 }
229
230                 ++SMTP->vrfy_count;
231                 memcpy(&SMTP->vrfy_buffer, us, sizeof(struct usersupp));
232         }
233 }
234
235
236 /* 
237  * Implements the EXPN (expand user name) command.
238  * Performs fuzzy match on full user names.
239  */
240 void smtp_expn(char *argbuf) {
241         SMTP->vrfy_count = 0;
242         strcpy(SMTP->vrfy_match, argbuf);
243         ForEachUser(smtp_expn_backend);
244
245         if (SMTP->vrfy_count < 1) {
246                 cprintf("550 String does not match anything.\n");
247         }
248         else if (SMTP->vrfy_count >= 1) {
249                 cprintf("250 %s <cit%ld@%s>\n",
250                         SMTP->vrfy_buffer.fullname,
251                         SMTP->vrfy_buffer.usernum,
252                         config.c_fqdn);
253         }
254 }
255
256
257
258 /* 
259  * Main command loop for SMTP sessions.
260  */
261 void smtp_command_loop(void) {
262         char cmdbuf[256];
263
264         time(&CC->lastcmd);
265         memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
266         if (client_gets(cmdbuf) < 1) {
267                 lprintf(3, "SMTP socket is broken.  Ending session.\n");
268                 CC->kill_me = 1;
269                 return;
270         }
271         lprintf(5, "citserver[%3d]: %s\n", CC->cs_pid, cmdbuf);
272         while (strlen(cmdbuf) < 5) strcat(cmdbuf, " ");
273
274         if (SMTP->command_state == smtp_user) {
275                 smtp_get_user(cmdbuf);
276         }
277
278         else if (SMTP->command_state == smtp_password) {
279                 smtp_get_pass(cmdbuf);
280         }
281
282         else if (!strncasecmp(cmdbuf, "AUTH", 4)) {
283                 smtp_auth(&cmdbuf[5]);
284         }
285
286         else if (!strncasecmp(cmdbuf, "EHLO", 4)) {
287                 smtp_hello(&cmdbuf[5], 1);
288         }
289
290         else if (!strncasecmp(cmdbuf, "EXPN", 4)) {
291                 smtp_expn(&cmdbuf[5]);
292         }
293
294         else if (!strncasecmp(cmdbuf, "HELO", 4)) {
295                 smtp_hello(&cmdbuf[5], 0);
296         }
297
298         else if (!strncasecmp(cmdbuf, "HELP", 4)) {
299                 smtp_help();
300         }
301
302         else if (!strncasecmp(cmdbuf, "NOOP", 4)) {
303                 cprintf("250 This command successfully did nothing.\n");
304         }
305
306         else if (!strncasecmp(cmdbuf, "QUIT", 4)) {
307                 cprintf("221 Goodbye...\n");
308                 CC->kill_me = 1;
309                 return;
310                 }
311
312         else if (!strncasecmp(cmdbuf, "VRFY", 4)) {
313                 smtp_vrfy(&cmdbuf[5]);
314         }
315
316         else {
317                 cprintf("500 I'm afraid I can't do that, Dave.\n");
318         }
319
320 }
321
322
323
324 char *Dynamic_Module_Init(void)
325 {
326         SYM_SMTP = CtdlGetDynamicSymbol();
327         CtdlRegisterServiceHook(25,
328                                 smtp_greeting,
329                                 smtp_command_loop);
330         return "$Id$";
331 }