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