Changed smtp for new worker classes
[fmail.git] / backends / auth / fsauth.cpp
blob7f92d61cb7387fd337a3e769239421b550486700
2 #include <libfmail.h>
3 #include <libfmail/socketipc.h>
5 int Server_onRun;
7 class InternalHandler : public ProtocolHandler{
8 public:
9 int Handle(Socket *s){
10 IPC *ipc;
11 IPCMessage *msg;
12 int onRun;
14 onRun = 1;
15 ipc = new SocketIPC(s);
17 while(onRun){
18 if (ipc->PeekMessage()){
19 msg = ipc->PopMessage();
21 if (!strcmp(msg->GetMessageName(), "shutdown")){
22 Server_onRun = 0;
23 onRun = 0;
26 delete msg;
30 return 0;
34 class FSAuth : public ProtocolHandler{
35 public:
36 int Handle(Socket *s){
37 IPCMessage *msg;
38 IPC *ipc;
39 int onRun;
40 char *user, *pass, buffer[255], auxbuffer[255];
41 FILE *fd;
43 printf("Got Client\n");
44 printf("Creating new IPC\n");
46 ipc = new SocketIPC(s);
47 onRun = 1;
49 while(onRun){
50 printf("Waiting for Message\n");
51 if (ipc->PeekMessage()){
52 printf("Got Message\n");
53 msg = ipc->PopMessage();
55 if (!strcmp(msg->GetMessageName(), "auth")){
56 printf("Got Auth Request\n");
57 user = msg->PopParam();
58 pass = msg->PopParam();
60 delete msg;
61 printf("Authenticating %s : %s\n", user, pass);
63 sprintf(buffer, "/tmp/fmail/%s.passwd", user);
64 fd = fopen(buffer, "r");
65 if (fd == NULL){
66 msg = new IPCMessage("failed");
67 }else{
68 memset(auxbuffer, 0, 255);
69 fread(auxbuffer, 1, 255, fd);
70 fclose(fd);
71 if (!strcmp(pass, auxbuffer)){
72 msg = new IPCMessage("failed");
73 }else{
74 msg = new IPCMessage("authok");
77 ipc->PushMessage(msg);
78 free(user);
79 free(pass);
80 onRun = 0;
84 ipc->CloseIPC();
85 delete ipc;
86 return 0;
90 class SimpleLoad : public LoadHandler{
91 public:
92 int Dispatch(Socket *sock, ProtocolHandler *ph){
93 printf("SimpleLoad: Dispatching\n");
94 if (ph)
95 return ph->Handle(sock);
96 return 0;
100 int main(){
101 BaseServer *serv, *serv_conf;
102 LoadHandler *lh;
103 FSAuth *fsauth;
104 InternalHandler *ih;
106 printf("Creating new FSAuth\n");
107 fsauth = new FSAuth();
108 ih = new InternalHandler();
110 printf("Creating new SimpleLoad\n");
111 lh = new SimpleLoad();
113 printf("Creating new BaseServer\n");
114 serv = new BaseServer(fsauth, 14000);
115 serv_conf = new BaseServer(ih, 13000);
117 printf("Setting Load Handler\n");
118 serv->SetLoadhandler(lh);
119 serv_conf->SetLoadhandler(lh);
121 printf("Listening\n");
122 serv->Listen();
123 Server_onRun = 1;
125 while(Server_onRun){
126 serv->Accept();
127 serv_conf->Accept();
128 printf("L");
131 return 0;