]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/userprocess.cpp
Move all_opers into class InspIRCd
[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 "inspircd_config.h"
22 #include "configreader.h"
23 #include <unistd.h>
24 #include <fcntl.h>
25 #include <sys/errno.h>
26 #include <sys/ioctl.h>
27 #include <sys/utsname.h>
28 #include <time.h>
29 #include <string>
30 #include <ext/hash_map>
31 #include <map>
32 #include <sstream>
33 #include <vector>
34 #include <deque>
35 #include "users.h"
36 #include "ctables.h"
37 #include "globals.h"
38 #include "modules.h"
39 #include "dynamic.h"
40 #include "wildcard.h"
41 #include "mode.h"
42 #include "commands.h"
43 #include "xline.h"
44 #include "inspstring.h"
45 #include "helperfuncs.h"
46 #include "hashcomp.h"
47 #include "socketengine.h"
48 #include "userprocess.h"
49 #include "inspircd.h"
50 #include "typedefs.h"
51 #include "command_parse.h"
52 #include "cull_list.h"
53
54 extern struct sockaddr_in client,server;
55 extern socklen_t length;
56
57 extern time_t TIME;
58 extern time_t OLDTIME;
59 char data[65536];
60
61 void InspIRCd::ProcessUser(userrec* cu)
62 {
63         int result = EAGAIN;
64
65         if (cu->fd == FD_MAGIC_NUMBER)
66                 return;
67
68         log(DEBUG,"Processing user with fd %d",cu->fd);
69
70         if (this->Config->GetIOHook(cu->GetPort()))
71         {
72                 int result2 = 0;
73                 int MOD_RESULT = 0;
74
75                 try
76                 {
77                         MOD_RESULT = this->Config->GetIOHook(cu->GetPort())->OnRawSocketRead(cu->fd,data,65535,result2);
78                         log(DEBUG,"Data result returned by module: %d",MOD_RESULT);
79                 }
80                 catch (ModuleException& modexcept)
81                 {
82                         log(DEBUG,"Module exception caught: %s",modexcept.GetReason());
83                 }
84
85                 if (MOD_RESULT < 0)
86                 {
87                         result = -EAGAIN;
88                 }
89                 else
90                 {
91                         result = result2;
92                 }
93         }
94         else
95         {
96                 result = cu->ReadData(data, 65535);
97         }
98
99         log(DEBUG,"Read result: %d",result);
100
101         if ((result) && (result != -EAGAIN))
102         {
103                 userrec *current;
104                 int currfd;
105                 int floodlines = 0;
106
107                 this->stats->statsRecv += result;
108                 /*
109                  * perform a check on the raw buffer as an array (not a string!) to remove
110                  * character 0 which is illegal in the RFC - replace them with spaces.
111                  * XXX - no garauntee there's not \0's in the middle of the data,
112                  *       and no reason for it to be terminated either. -- Om
113                  */
114
115                 for (int checker = 0; checker < result; checker++)
116                 {
117                         if (data[checker] == 0)
118                                 data[checker] = ' ';
119                 }
120
121                 if (result > 0)
122                         data[result] = '\0';
123
124                 current = cu;
125                 currfd = current->fd;
126
127                 // add the data to the users buffer
128                 if (result > 0)
129                 {
130                         if (!current->AddBuffer(data))
131                         {
132                                 // AddBuffer returned false, theres too much data in the user's buffer and theyre up to no good.
133                                 if (current->registered == REG_ALL)
134                                 {
135                                         // Make sure they arn't flooding long lines.
136                                         if (TIME > current->reset_due)
137                                         {
138                                                 current->reset_due = TIME + current->threshold;
139                                                 current->lines_in = 0;
140                                         }
141
142                                         current->lines_in++;
143
144                                         if (current->lines_in > current->flood)
145                                         {
146                                                 log(DEFAULT,"Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
147                                                 this->WriteOpers("*** Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
148                                                 userrec::QuitUser(this, current,"Excess flood");
149                                                 return;
150                                         }
151                                         else
152                                         {
153                                                 current->WriteServ("NOTICE %s :Your previous line was too long and was not delivered (Over 512chars) Please shorten it.", current->nick);
154                                                 current->recvq = "";
155                                         }
156                                 }
157                                 else
158                                 {
159                                         this->WriteOpers("*** Excess flood from %s",current->GetIPString());
160                                         log(DEFAULT,"Excess flood from: %s",current->GetIPString());
161                                         add_zline(120,this->Config->ServerName,"Flood from unregistered connection",current->GetIPString());
162                                         apply_lines(APPLY_ZLINES);
163                                 }
164
165                                 return;
166                         }
167
168                         if (current->recvq.length() > (unsigned)this->Config->NetBufferSize)
169                         {
170                                 if (current->registered == REG_ALL)
171                                 {
172                                         userrec::QuitUser(this, current,"RecvQ exceeded");
173                                 }
174                                 else
175                                 {
176                                         this->WriteOpers("*** Excess flood from %s",current->GetIPString());
177                                         log(DEFAULT,"Excess flood from: %s",current->GetIPString());
178                                         add_zline(120,this->Config->ServerName,"Flood from unregistered connection",current->GetIPString());
179                                         apply_lines(APPLY_ZLINES);
180                                 }
181
182                                 return;
183                         }
184
185                         // while there are complete lines to process...
186                         while (current->BufferIsReady())
187                         {
188                                 if (TIME > current->reset_due)
189                                 {
190                                         current->reset_due = TIME + current->threshold;
191                                         current->lines_in = 0;
192                                 }
193
194                                 if (++current->lines_in > current->flood)
195                                 {
196                                         log(DEFAULT,"Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
197                                         WriteOpers("*** Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
198                                         userrec::QuitUser(this, current,"Excess flood");
199                                         return;
200                                 }
201
202                                 if ((++floodlines > current->flood) && (current->flood != 0))
203                                 {
204                                         if (current->registered == REG_ALL)
205                                         {
206                                                 log(DEFAULT,"Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
207                                                 WriteOpers("*** Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
208                                                 userrec::QuitUser(this, current,"Excess flood");
209                                         }
210                                         else
211                                         {
212                                                 add_zline(120,this->Config->ServerName,"Flood from unregistered connection",current->GetIPString());
213                                                 apply_lines(APPLY_ZLINES);
214                                         }
215
216                                         return;
217                                 }
218
219                                 // use GetBuffer to copy single lines into the sanitized string
220                                 std::string single_line = current->GetBuffer();
221                                 current->bytes_in += single_line.length();
222                                 current->cmds_in++;
223                                 if (single_line.length() > 512)
224                                         single_line.resize(512);
225
226                                 userrec* old_comp = this->fd_ref_table[currfd];
227
228                                 this->Parser->ProcessBuffer(single_line,current);
229                                 /*
230                                  * look for the user's record in case it's changed... if theyve quit,
231                                  * we cant do anything more with their buffer, so bail.
232                                  * there used to be an ugly, slow loop here. Now we have a reference
233                                  * table, life is much easier (and FASTER)
234                                  */
235                                 userrec* new_comp = this->fd_ref_table[currfd];
236                                 if ((currfd < 0) || (!this->fd_ref_table[currfd]) || (old_comp != new_comp))
237                                 {
238                                         return;
239                                 }
240                                 else
241                                 {
242                                         /* The user is still here, flush their buffer */
243                                         current->FlushWriteBuf();
244                                 }
245                         }
246
247                         return;
248                 }
249
250                 if ((result == -1) && (errno != EAGAIN) && (errno != EINTR))
251                 {
252                         log(DEBUG,"killing: %s",cu->nick);
253                         userrec::QuitUser(this,cu,strerror(errno));
254                         return;
255                 }
256         }
257
258         // result EAGAIN means nothing read
259         else if ((result == EAGAIN) || (result == -EAGAIN))
260         {
261                 /* do nothing */
262         }
263         else if (result == 0)
264         {
265                 log(DEBUG,"InspIRCd: Exited: %s",cu->nick);
266                 userrec::QuitUser(this,cu,"Client exited");
267                 log(DEBUG,"Bailing from client exit");
268                 return;
269         }
270 }
271
272 void InspIRCd::DoSocketTimeouts(time_t TIME)
273 {
274         unsigned int numsockets = this->module_sockets.size();
275         SocketEngine* SE = this->SE;
276
277         for (std::vector<InspSocket*>::iterator a = this->module_sockets.begin(); a < this->module_sockets.end(); a++)
278         {
279                 InspSocket* s = (InspSocket*)*a;
280                 if ((s) && (s->GetFd() >= 0) && (s->GetFd() < MAX_DESCRIPTORS) && (this->socket_ref[s->GetFd()] != NULL) && (s->Timeout(TIME)))
281                 {
282                         log(DEBUG,"userprocess.cpp: Socket poll returned false, close and bail");
283                         this->socket_ref[s->GetFd()] = NULL;
284                         SE->DelFd(s->GetFd());
285                         this->module_sockets.erase(a);
286                         s->Close();
287                         DELETE(s);
288                         break;
289                 }
290
291                 if (this->module_sockets.size() != numsockets)
292                         break;
293         }
294 }
295
296 /**
297  * This function is called once a second from the mainloop.
298  * It is intended to do background checking on all the user structs, e.g.
299  * stuff like ping checks, registration timeouts, etc. This function is
300  * also responsible for checking if InspSocket derived classes are timed out.
301  */
302 void InspIRCd::DoBackgroundUserStuff(time_t TIME)
303 {
304         CullList GlobalGoners;
305
306         /* XXX: IT IS NOT SAFE TO USE AN ITERATOR HERE. DON'T EVEN THINK ABOUT IT. */
307         for (unsigned long count2 = 0; count2 != this->local_users.size(); count2++)
308         {
309                 if (count2 >= this->local_users.size())
310                         break;
311
312                 userrec* curr = this->local_users[count2];
313
314                 if (curr)
315                 {
316                         /*
317                          * registration timeout -- didnt send USER/NICK/HOST
318                          * in the time specified in their connection class.
319                          */
320                         if (((unsigned)TIME > (unsigned)curr->timeout) && (curr->registered != REG_ALL))
321                         {
322                                 log(DEBUG,"InspIRCd: registration timeout: %s",curr->nick);
323                                 //ZapThisDns(curr->fd);
324                                 GlobalGoners.AddItem(curr,"Registration timeout");
325                                 continue;
326                         }
327                         /*
328                          * user has signed on with USER/NICK/PASS, and dns has completed, all the modules
329                          * say this user is ok to proceed, fully connect them.
330                          */
331                         if ((TIME > curr->signon) && (curr->registered == REG_NICKUSER) && (AllModulesReportReady(curr)))
332                         {
333                                 curr->dns_done = true;
334                                 //ZapThisDns(curr->fd);
335                                 this->stats->statsDnsBad++;
336                                 curr->FullConnect(&GlobalGoners);
337                                 continue;
338                         }
339                         if ((curr->dns_done) && (curr->registered == REG_NICKUSER) && (AllModulesReportReady(curr)))
340                         {
341                                 log(DEBUG,"dns done, registered=3, and modules ready, OK");
342                                 curr->FullConnect(&GlobalGoners);
343                                 //ZapThisDns(curr->fd);
344                                 continue;
345                         }
346                         // It's time to PING this user. Send them a ping.
347                         if ((TIME > curr->nping) && (curr->registered == REG_ALL))
348                         {
349                                 // This user didn't answer the last ping, remove them
350                                 if (!curr->lastping)
351                                 {
352                                         GlobalGoners.AddItem(curr,"Ping timeout");
353                                         curr->lastping = 1;
354                                         curr->nping = TIME+curr->pingmax;
355                                         continue;
356                                 }
357                                 curr->Write("PING :%s",this->Config->ServerName);
358                                 curr->lastping = 0;
359                                 curr->nping = TIME+curr->pingmax;
360                         }
361
362                         /*
363                          * We can flush the write buffer as the last thing we do, because if they
364                          * match any of the above conditions its no use flushing their buffer anyway.
365                          */
366         
367                         curr->FlushWriteBuf();
368                         if (*curr->GetWriteError())
369                         {
370                                 GlobalGoners.AddItem(curr,curr->GetWriteError());
371                                 continue;
372                         }
373                 }
374
375         }
376
377
378         /* Remove all the queued users who are due to be quit, free memory used. */
379         GlobalGoners.Apply();
380 }