]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/userprocess.cpp
In the grand tradition of huge fucking commits:
[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 "wildcard.h"
16 #include "xline.h"
17 #include "socketengine.h"
18 #include "command_parse.h"
19
20 void FloodQuitUserHandler::Call(User* current)
21 {
22         Server->Log(DEFAULT,"Excess flood from: %s@%s", current->ident, current->host);
23         Server->SNO->WriteToSnoMask('f',"Excess flood from: %s%s%s@%s",
24                         current->registered == REG_ALL ? current->nick : "",
25                         current->registered == REG_ALL ? "!" : "", current->ident, current->host);
26         User::QuitUser(Server, current, "Excess flood");
27         if (current->registered != REG_ALL)
28         {
29                 Server->XLines->add_zline(120, Server->Config->ServerName, "Flood from unregistered connection", current->GetIPString());
30                 Server->XLines->apply_lines(APPLY_ZLINES);
31         }
32 }
33
34 void ProcessUserHandler::Call(User* cu)
35 {
36         int result = EAGAIN;
37
38         if (cu->GetFd() == FD_MAGIC_NUMBER)
39                 return;
40
41         char* ReadBuffer = Server->GetReadBuffer();
42
43         if (Server->Config->GetIOHook(cu->GetPort()))
44         {
45                 int result2 = 0;
46                 int MOD_RESULT = 0;
47
48                 try
49                 {
50                         MOD_RESULT = Server->Config->GetIOHook(cu->GetPort())->OnRawSocketRead(cu->GetFd(),ReadBuffer,sizeof(ReadBuffer),result2);
51                 }
52                 catch (CoreException& modexcept)
53                 {
54                         Server->Log(DEBUG, "%s threw an exception: %s", modexcept.GetSource(), modexcept.GetReason());
55                 }
56
57                 if (MOD_RESULT < 0)
58                 {
59                         result = -EAGAIN;
60                 }
61                 else
62                 {
63                         result = result2;
64                 }
65         }
66         else
67         {
68                 result = cu->ReadData(ReadBuffer, sizeof(ReadBuffer));
69         }
70
71         if ((result) && (result != -EAGAIN))
72         {
73                 User *current;
74                 int currfd;
75                 int floodlines = 0;
76
77                 Server->stats->statsRecv += result;
78                 /*
79                  * perform a check on the raw buffer as an array (not a string!) to remove
80                  * character 0 which is illegal in the RFC - replace them with spaces.
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 (Server->Time() > current->reset_due)
105                                         {
106                                                 current->reset_due = Server->Time() + current->threshold;
107                                                 current->lines_in = 0;
108                                         }
109
110                                         current->lines_in++;
111
112                                         if (current->flood && current->lines_in > current->flood)
113                                                 Server->FloodQuitUser(current);
114                                         else
115                                         {
116                                                 current->WriteServ("NOTICE %s :Your previous line was too long and was not delivered (Over %d chars) Please shorten it.", current->nick, MAXBUF-2);
117                                                 current->recvq.clear();
118                                         }
119                                 }
120                                 else
121                                         Server->FloodQuitUser(current);
122
123                                 return;
124                         }
125
126                         // while there are complete lines to process...
127                         while (current->BufferIsReady())
128                         {
129                                 if (Server->Time() > current->reset_due)
130                                 {
131                                         current->reset_due = Server->Time() + current->threshold;
132                                         current->lines_in = 0;
133                                 }
134
135                                 if (++current->lines_in > current->flood && current->flood)
136                                 {
137                                         Server->FloodQuitUser(current);
138                                         return;
139                                 }
140
141                                 if ((++floodlines > current->flood) && (current->flood != 0))
142                                 {
143                                         Server->FloodQuitUser(current);
144                                         return;
145                                 }
146
147                                 // use GetBuffer to copy single lines into the sanitized string
148                                 std::string single_line = current->GetBuffer();
149                                 current->bytes_in += single_line.length();
150                                 current->cmds_in++;
151                                 if (single_line.length() > MAXBUF - 2)  /* MAXBUF is 514 to allow for neccessary line terminators */
152                                         single_line.resize(MAXBUF - 2); /* So to trim to 512 here, we use MAXBUF - 2 */
153
154                                 Server->Parser->ProcessBuffer(single_line, current);
155                         }
156
157                         return;
158                 }
159
160                 if ((result == -1) && (errno != EAGAIN) && (errno != EINTR))
161                 {
162                         User::QuitUser(Server, cu, errno ? strerror(errno) : "EOF from client");
163                         return;
164                 }
165         }
166
167         // result EAGAIN means nothing read
168         else if ((result == EAGAIN) || (result == -EAGAIN))
169         {
170                 /* do nothing */
171         }
172         else if (result == 0)
173         {
174                 User::QuitUser(Server, cu, "Connection closed");
175                 return;
176         }
177 }
178
179 /**
180  * This function is called once a second from the mainloop.
181  * It is intended to do background checking on all the user structs, e.g.
182  * stuff like ping checks, registration timeouts, etc.
183  */
184 void InspIRCd::DoBackgroundUserStuff(time_t TIME)
185 {
186         /* Is it time yet? */
187         if (TIME < next_call)
188                 return;
189         else
190         {
191                 /* Time we actually need to call this again */
192                 const time_t DUMMY_VALUE = 32768;
193                 next_call = TIME + DUMMY_VALUE;
194
195                 for (std::vector<User*>::iterator count2 = local_users.begin(); count2 != local_users.end(); count2++)
196                 {
197                         User* curr = *count2;
198
199                         /*
200                          * registration timeout -- didnt send USER/NICK/HOST
201                          * in the time specified in their connection class.
202                          */
203                         if ((TIME > curr->timeout) && (curr->registered != REG_ALL))
204                         {
205                                 curr->muted = true;
206                                 User::QuitUser(this, curr, "Registration timeout");
207                                 continue;
208                         }
209                         else
210                         {
211                                 if ((curr->registered != REG_ALL) && (next_call > (time_t)curr->timeout))
212                                         next_call = curr->timeout;
213                         }
214                         /*
215                          * user has signed on with USER/NICK/PASS, and dns has completed, all the modules
216                          * say this user is ok to proceed, fully connect them.
217                          */
218                         bool ready = AllModulesReportReady(curr);
219                         if ((TIME > curr->signon) && (curr->registered == REG_NICKUSER) && (ready))
220                         {
221                                 if (!curr->dns_done)
222                                 {
223                                         curr->WriteServ("NOTICE Auth :*** Could not resolve your hostname: Request timed out; using your IP address (%s) instead.", curr->GetIPString());
224                                         curr->dns_done = true;
225                                 }
226                                 this->stats->statsDnsBad++;
227                                 curr->FullConnect();
228                                 continue;
229                         }
230                         else
231                         {
232                                 if ((curr->registered == REG_NICKUSER) && (ready) && (next_call > curr->signon))
233                                         next_call = curr->signon;
234                         }
235
236                         if ((curr->dns_done) && (curr->registered == REG_NICKUSER) && (ready))
237                         {
238                                 curr->FullConnect();
239                                 continue;
240                         }
241                         else
242                         {
243                                 if ((curr->registered == REG_NICKUSER) && (ready) && (next_call > curr->signon + this->Config->dns_timeout))
244                                         next_call = curr->signon + this->Config->dns_timeout;
245                         }
246
247                         // It's time to PING this user. Send them a ping.
248                         if ((TIME > curr->nping) && (curr->registered == REG_ALL))
249                         {
250                                 // This user didn't answer the last ping, remove them
251                                 if (!curr->lastping)
252                                 {
253                                         /* Everybody loves boobies. */
254                                         time_t time = this->Time(false) - (curr->nping - curr->pingmax);
255                                         char message[MAXBUF];
256                                         snprintf(message, MAXBUF, "Ping timeout: %ld second%s", (long)time, time > 1 ? "s" : "");
257                                         curr->muted = true;
258                                         curr->lastping = 1;
259                                         curr->nping = TIME+curr->pingmax;
260                                         User::QuitUser(this, curr, message);
261                                         continue;
262                                 }
263                                 curr->Write("PING :%s",this->Config->ServerName);
264                                 curr->lastping = 0;
265                                 curr->nping = TIME+curr->pingmax;
266                         }
267                         else
268                         {
269                                 if ((curr->registered == REG_ALL) && (next_call > curr->nping))
270                                         next_call = curr->nping;
271                         }
272                 }
273
274                 /* If theres nothing to do, trigger in the next second, something might come up */
275                 time_t delta = next_call - TIME;
276                 if (delta == DUMMY_VALUE)
277                 {
278                         next_call = TIME + 1;
279                         delta = 1;
280                 }
281         }
282 }
283