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