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