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