f8dcb2fd2aeeb1f68d7fa25effd0cdb3f1e6de7f
[citadel.git] / citadel / serv_pop3.c
1 /* $Id$ */
2
3 #define POP3_PORT       1110
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 citpop3 {                /* Information about the current session */
36         char FIX[3];
37 };
38
39 #define POP3 ((struct citpop3 *)CtdlGetUserData(SYM_POP3))
40
41 long SYM_POP3;
42
43
44 /*
45  * Here's where our POP3 session begins its happy day.
46  */
47 void pop3_greeting(void) {
48
49         strcpy(CC->cs_clientname, "POP3 session");
50         CC->internal_pgm = 1;
51         CtdlAllocUserData(SYM_POP3, sizeof(struct citpop3));
52
53         cprintf("+OK Welcome to the Citadel/UX POP3 server at %s\r\n",
54                 config.c_fqdn);
55 }
56
57
58 /*
59  * Specify user name (implements POP3 "USER" command)
60  */
61 void pop3_user(char *argbuf) {
62         char username[256];
63
64         if (CC->logged_in) {
65                 cprintf("-ERR You are already logged in.\r\n");
66                 return;
67         }
68
69         strcpy(username, argbuf);
70         striplt(username);
71
72         lprintf(9, "Trying <%s>\n", username);
73         if (CtdlLoginExistingUser(username) == login_ok) {
74                 cprintf("+OK Password required for %s\r\n", username);
75         }
76         else {
77                 cprintf("-ERR No such user.\r\n");
78         }
79 }
80
81
82 /*
83  * Authorize with password (implements POP3 "PASS" command)
84  */
85 void pop3_pass(char *argbuf) {
86         char password[256];
87
88         strcpy(password, argbuf);
89         striplt(password);
90
91         lprintf(9, "Trying <%s>\n", password);
92         if (CtdlTryPassword(password) == pass_ok) {
93                 cprintf("+OK %s is logged in!\r\n", CC->usersupp.fullname);
94                 lprintf(9, "POP3 password login successful\n");
95         }
96         else {
97                 cprintf("-ERR That is NOT the password!  Go away!\r\n");
98         }
99 }
100
101
102
103 /* 
104  * Main command loop for POP3 sessions.
105  */
106 void pop3_command_loop(void) {
107         char cmdbuf[256];
108
109         time(&CC->lastcmd);
110         memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
111         if (client_gets(cmdbuf) < 1) {
112                 lprintf(3, "POP3 socket is broken.  Ending session.\r\n");
113                 CC->kill_me = 1;
114                 return;
115         }
116         lprintf(5, "citserver[%3d]: %s\r\n", CC->cs_pid, cmdbuf);
117         while (strlen(cmdbuf) < 5) strcat(cmdbuf, " ");
118
119         if (!strncasecmp(cmdbuf, "NOOP", 4)) {
120                 cprintf("+OK This command successfully did nothing.\r\n");
121         }
122
123         else if (!strncasecmp(cmdbuf, "QUIT", 4)) {
124                 cprintf("+OK Goodbye...\r\n");
125                 CC->kill_me = 1;
126                 return;
127         }
128
129         else if (!strncasecmp(cmdbuf, "USER", 4)) {
130                 pop3_user(&cmdbuf[5]);
131         }
132
133         else if (!strncasecmp(cmdbuf, "PASS", 4)) {
134                 pop3_pass(&cmdbuf[5]);
135         }
136
137         else if (!CC->logged_in) {
138                 cprintf("-ERR Not logged in.\r\n");
139         }
140
141         else {
142                 cprintf("500 I'm afraid I can't do that, Dave.\r\n");
143         }
144
145 }
146
147
148
149 char *Dynamic_Module_Init(void)
150 {
151         SYM_POP3 = CtdlGetDynamicSymbol();
152         CtdlRegisterServiceHook(POP3_PORT,
153                                 pop3_greeting,
154                                 pop3_command_loop);
155         return "$Id$";
156 }