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