]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/userprocess.cpp
Auto loading of commands as shared objects via dlsym (very lightweight interface...
[user/henk/code/inspircd.git] / src / userprocess.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
6  *                       E-mail:
7  *                <brain@chatspike.net>
8  *                <Craig@chatspike.net>
9  *
10  * Written by Craig Edwards, Craig McLure, and others.
11  * This program is free but copyrighted software; see
12  *            the file COPYING for details.
13  *
14  * ---------------------------------------------------
15  */
16
17 /* Now with added unF! ;) */
18
19 using namespace std;
20
21 #include "configreader.h"
22 #include "users.h"
23 #include "modules.h"
24 #include "wildcard.h"
25 #include "xline.h"
26 #include "socketengine.h"
27 #include "userprocess.h"
28 #include "inspircd.h"
29 #include "command_parse.h"
30 #include "cull_list.h"
31
32 void InspIRCd::ProcessUser(userrec* cu)
33 {
34         int result = EAGAIN;
35
36         if (cu->GetFd() == FD_MAGIC_NUMBER)
37                 return;
38
39         if (this->Config->GetIOHook(cu->GetPort()))
40         {
41                 int result2 = 0;
42                 int MOD_RESULT = 0;
43
44                 try
45                 {
46                         MOD_RESULT = this->Config->GetIOHook(cu->GetPort())->OnRawSocketRead(cu->GetFd(),ReadBuffer,sizeof(ReadBuffer),result2);
47                         this->Log(DEBUG,"Data result returned by module: %d",MOD_RESULT);
48                 }
49                 catch (ModuleException& modexcept)
50                 {
51                         this->Log(DEBUG,"Module exception caught: %s",modexcept.GetReason());
52                 }
53
54                 if (MOD_RESULT < 0)
55                 {
56                         result = -EAGAIN;
57                 }
58                 else
59                 {
60                         result = result2;
61                 }
62         }
63         else
64         {
65                 result = cu->ReadData(ReadBuffer, sizeof(ReadBuffer));
66         }
67
68         this->Log(DEBUG,"Read result: %d",result);
69
70         if ((result) && (result != -EAGAIN))
71         {
72                 userrec *current;
73                 int currfd;
74                 int floodlines = 0;
75
76                 this->stats->statsRecv += result;
77                 /*
78                  * perform a check on the raw buffer as an array (not a string!) to remove
79                  * character 0 which is illegal in the RFC - replace them with spaces.
80                  * XXX - no garauntee there's not \0's in the middle of the data,
81                  *       and no reason for it to be terminated either. -- Om
82                  */
83
84                 for (int checker = 0; checker < result; checker++)
85                 {
86                         if (ReadBuffer[checker] == 0)
87                                 ReadBuffer[checker] = ' ';
88                 }
89
90                 if (result > 0)
91                         ReadBuffer[result] = '\0';
92
93                 current = cu;
94                 currfd = current->GetFd();
95
96                 // add the data to the users buffer
97                 if (result > 0)
98                 {
99                         if (!current->AddBuffer(ReadBuffer))
100                         {
101                                 // AddBuffer returned false, theres too much data in the user's buffer and theyre up to no good.
102                                 if (current->registered == REG_ALL)
103                                 {
104                                         // Make sure they arn't flooding long lines.
105                                         if (TIME > current->reset_due)
106                                         {
107                                                 current->reset_due = TIME + current->threshold;
108                                                 current->lines_in = 0;
109                                         }
110
111                                         current->lines_in++;
112
113                                         if (current->lines_in > current->flood)
114                                         {
115                                                 this->Log(DEFAULT,"Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
116                                                 this->SNO->WriteToSnoMask('f',"Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
117                                                 userrec::QuitUser(this, current,"Excess flood");
118                                                 return;
119                                         }
120                                         else
121                                         {
122                                                 current->WriteServ("NOTICE %s :Your previous line was too long and was not delivered (Over 512chars) Please shorten it.", current->nick);
123                                                 current->recvq = "";
124                                         }
125                                 }
126                                 else
127                                 {
128                                         this->WriteOpers("*** Excess flood from %s",current->GetIPString());
129                                         this->SNO->WriteToSnoMask('f',"Excess flood from: %s",current->GetIPString());
130                                         XLines->add_zline(120,this->Config->ServerName,"Flood from unregistered connection",current->GetIPString());
131                                         XLines->apply_lines(APPLY_ZLINES);
132                                 }
133
134                                 return;
135                         }
136
137                         if (current->recvq.length() > (unsigned)this->Config->NetBufferSize)
138                         {
139                                 if (current->registered == REG_ALL)
140                                 {
141                                         userrec::QuitUser(this, current,"RecvQ exceeded");
142                                 }
143                                 else
144                                 {
145                                         this->WriteOpers("*** Excess flood from %s",current->GetIPString());
146                                         this->SNO->WriteToSnoMask('f',"Excess flood from: %s",current->GetIPString());
147                                         XLines->add_zline(120,this->Config->ServerName,"Flood from unregistered connection",current->GetIPString());
148                                         XLines->apply_lines(APPLY_ZLINES);
149                                 }
150
151                                 return;
152                         }
153
154                         // while there are complete lines to process...
155                         while (current->BufferIsReady())
156                         {
157                                 if (TIME > current->reset_due)
158                                 {
159                                         current->reset_due = TIME + current->threshold;
160                                         current->lines_in = 0;
161                                 }
162
163                                 if (++current->lines_in > current->flood)
164                                 {
165                                         this->Log(DEFAULT,"Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
166                                         this->SNO->WriteToSnoMask('f',"Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
167                                         userrec::QuitUser(this, current,"Excess flood");
168                                         return;
169                                 }
170
171                                 if ((++floodlines > current->flood) && (current->flood != 0))
172                                 {
173                                         if (current->registered == REG_ALL)
174                                         {
175                                                 this->Log(DEFAULT,"Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
176                                                 SNO->WriteToSnoMask('f',"Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
177                                                 userrec::QuitUser(this, current,"Excess flood");
178                                         }
179                                         else
180                                         {
181                                                 XLines->add_zline(120,this->Config->ServerName,"Flood from unregistered connection",current->GetIPString());
182                                                 XLines->apply_lines(APPLY_ZLINES);
183                                         }
184
185                                         return;
186                                 }
187
188                                 // use GetBuffer to copy single lines into the sanitized string
189                                 std::string single_line = current->GetBuffer();
190                                 current->bytes_in += single_line.length();
191                                 current->cmds_in++;
192                                 if (single_line.length() > 512)
193                                         single_line.resize(512);
194
195                                 EventHandler* old_comp = this->SE->GetRef(currfd);
196
197                                 this->Parser->ProcessBuffer(single_line,current);
198                                 /*
199                                  * look for the user's record in case it's changed... if theyve quit,
200                                  * we cant do anything more with their buffer, so bail.
201                                  * there used to be an ugly, slow loop here. Now we have a reference
202                                  * table, life is much easier (and FASTER)
203                                  */
204                                 EventHandler* new_comp = this->SE->GetRef(currfd);
205                                 if (old_comp != new_comp)
206                                 {
207                                         return;
208                                 }
209                                 else
210                                 {
211                                         /* The user is still here, flush their buffer */
212                                         current->FlushWriteBuf();
213                                 }
214                         }
215
216                         return;
217                 }
218
219                 if ((result == -1) && (errno != EAGAIN) && (errno != EINTR))
220                 {
221                         this->Log(DEBUG,"killing: %s",cu->nick);
222                         userrec::QuitUser(this,cu,strerror(errno));
223                         return;
224                 }
225         }
226
227         // result EAGAIN means nothing read
228         else if ((result == EAGAIN) || (result == -EAGAIN))
229         {
230                 /* do nothing */
231         }
232         else if (result == 0)
233         {
234                 this->Log(DEBUG,"InspIRCd: Exited: %s",cu->nick);
235                 userrec::QuitUser(this,cu,"Client exited");
236                 this->Log(DEBUG,"Bailing from client exit");
237                 return;
238         }
239 }
240
241 /**
242  * This function is called once a second from the mainloop.
243  * It is intended to do background checking on all the user structs, e.g.
244  * stuff like ping checks, registration timeouts, etc. This function is
245  * also responsible for checking if InspSocket derived classes are timed out.
246  */
247 void InspIRCd::DoBackgroundUserStuff(time_t TIME)
248 {
249         CullList GlobalGoners(this);
250
251         /* XXX: IT IS NOT SAFE TO USE AN ITERATOR HERE. DON'T EVEN THINK ABOUT IT. */
252         for (unsigned long count2 = 0; count2 != this->local_users.size(); count2++)
253         {
254                 if (count2 >= this->local_users.size())
255                         break;
256
257                 userrec* curr = this->local_users[count2];
258
259                 if (curr)
260                 {
261                         /*
262                          * registration timeout -- didnt send USER/NICK/HOST
263                          * in the time specified in their connection class.
264                          */
265                         if (((unsigned)TIME > (unsigned)curr->timeout) && (curr->registered != REG_ALL))
266                         {
267                                 this->Log(DEBUG,"InspIRCd: registration timeout: %s",curr->nick);
268                                 //ZapThisDns(curr->fd);
269                                 GlobalGoners.AddItem(curr,"Registration timeout");
270                                 continue;
271                         }
272                         /*
273                          * user has signed on with USER/NICK/PASS, and dns has completed, all the modules
274                          * say this user is ok to proceed, fully connect them.
275                          */
276                         if ((TIME > curr->signon) && (curr->registered == REG_NICKUSER) && (AllModulesReportReady(curr)))
277                         {
278                                 curr->dns_done = true;
279                                 //ZapThisDns(curr->fd);
280                                 this->stats->statsDnsBad++;
281                                 curr->FullConnect(&GlobalGoners);
282                                 continue;
283                         }
284                         if ((curr->dns_done) && (curr->registered == REG_NICKUSER) && (AllModulesReportReady(curr)))
285                         {
286                                 this->Log(DEBUG,"dns done, registered=3, and modules ready, OK");
287                                 curr->FullConnect(&GlobalGoners);
288                                 //ZapThisDns(curr->fd);
289                                 continue;
290                         }
291                         // It's time to PING this user. Send them a ping.
292                         if ((TIME > curr->nping) && (curr->registered == REG_ALL))
293                         {
294                                 // This user didn't answer the last ping, remove them
295                                 if (!curr->lastping)
296                                 {
297                                         GlobalGoners.AddItem(curr,"Ping timeout");
298                                         curr->lastping = 1;
299                                         curr->nping = TIME+curr->pingmax;
300                                         continue;
301                                 }
302                                 curr->Write("PING :%s",this->Config->ServerName);
303                                 curr->lastping = 0;
304                                 curr->nping = TIME+curr->pingmax;
305                         }
306
307                         /*
308                          * We can flush the write buffer as the last thing we do, because if they
309                          * match any of the above conditions its no use flushing their buffer anyway.
310                          */
311         
312                         curr->FlushWriteBuf();
313                         if (*curr->GetWriteError())
314                         {
315                                 GlobalGoners.AddItem(curr,curr->GetWriteError());
316                                 continue;
317                         }
318                 }
319
320         }
321
322
323         /* Remove all the queued users who are due to be quit, free memory used. */
324         GlobalGoners.Apply();
325 }
326