]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/userprocess.cpp
Fix segfault due to invalidated iterator
[user/henk/code/inspircd.git] / src / userprocess.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2009 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 /* $Core */
15
16 #include "inspircd.h"
17 #include "xline.h"
18 #include "socketengine.h"
19 #include "command_parse.h"
20
21 void FloodQuitUserHandler::Call(User* current)
22 {
23         Server->Logs->Log("USERS",DEFAULT,"Excess flood from: %s@%s", current->ident.c_str(), current->host.c_str());
24         Server->SNO->WriteToSnoMask('f',"Excess flood from: %s%s%s@%s",
25                         current->registered == REG_ALL ? current->nick.c_str() : "",
26                         current->registered == REG_ALL ? "!" : "", current->ident.c_str(), current->host.c_str());
27         Server->Users->QuitUser(current, "Excess flood");
28
29         if (current->registered != REG_ALL)
30         {
31                 ZLine* zl = new ZLine(Server, Server->Time(), 0, Server->Config->ServerName, "Flood from unregistered connection", current->GetIPString());
32                 if (Server->XLines->AddLine(zl,NULL))
33                         Server->XLines->ApplyLines();
34                 else
35                         delete zl;
36         }
37 }
38
39 void ProcessUserHandler::Call(User* cu)
40 {
41         int result = EAGAIN;
42
43         if (cu->GetFd() == FD_MAGIC_NUMBER)
44                 return;
45
46         char* ReadBuffer = Server->GetReadBuffer();
47
48         if (cu->GetIOHook())
49         {
50                 int result2 = 0;
51                 int MOD_RESULT = 0;
52
53                 try
54                 {
55                         MOD_RESULT = cu->GetIOHook()->OnRawSocketRead(cu->GetFd(), ReadBuffer, Server->Config->NetBufferSize, result2);
56                 }
57                 catch (CoreException& modexcept)
58                 {
59                         Server->Logs->Log("USERS",DEBUG, "%s threw an exception: %s", modexcept.GetSource(), modexcept.GetReason());
60                 }
61
62                 if (MOD_RESULT < 0)
63                 {
64                         result = -EAGAIN;
65                 }
66                 else
67                 {
68                         result = result2;
69                 }
70         }
71         else
72         {
73                 result = cu->ReadData(ReadBuffer, Server->Config->NetBufferSize);
74         }
75
76         if ((result) && (result != -EAGAIN))
77         {
78                 User *current;
79                 int currfd;
80
81                 Server->stats->statsRecv += result;
82                 /*
83                  * perform a check on the raw buffer as an array (not a string!) to remove
84                  * character 0 which is illegal in the RFC - replace them with spaces.
85                  */
86
87                 for (int checker = 0; checker < result; checker++)
88                 {
89                         if (ReadBuffer[checker] == 0)
90                                 ReadBuffer[checker] = ' ';
91                 }
92
93                 if (result > 0)
94                         ReadBuffer[result] = '\0';
95
96                 current = cu;
97                 currfd = current->GetFd();
98
99                 // add the data to the users buffer
100                 if (result > 0)
101                 {
102                         if (!current->AddBuffer(ReadBuffer))
103                         {
104                                 // AddBuffer returned false, theres too much data in the user's buffer and theyre up to no good.
105                                 Server->FloodQuitUser(current);
106                                 return;
107                         }
108
109                         /* If user is over penalty, dont process here, just build up */
110                         if (current->Penalty < 10)
111                                 Server->Parser->DoLines(current);
112
113                         return;
114                 }
115
116                 if ((result == -1) && (errno != EAGAIN) && (errno != EINTR))
117                 {
118                         Server->Users->QuitUser(cu, errno ? strerror(errno) : "EOF from client");
119                         return;
120                 }
121         }
122
123         // result EAGAIN means nothing read
124         else if ((result == EAGAIN) || (result == -EAGAIN))
125         {
126                 /* do nothing */
127         }
128         else if (result == 0)
129         {
130                 Server->Users->QuitUser(cu, "Connection closed");
131                 return;
132         }
133 }
134
135 /**
136  * This function is called once a second from the mainloop.
137  * It is intended to do background checking on all the user structs, e.g.
138  * stuff like ping checks, registration timeouts, etc.
139  */
140 void InspIRCd::DoBackgroundUserStuff()
141 {
142         /*
143          * loop over all local users..
144          */
145         std::vector<User*>::reverse_iterator count2 = this->Users->local_users.rbegin();
146         while (count2 != this->Users->local_users.rend())
147         {
148                 User *curr = *count2;
149                 count2++;
150
151                 if (curr->quitting)
152                         continue;
153
154                 if (curr->Penalty)
155                 {
156                         curr->Penalty--;
157                         if (curr->Penalty < 10)
158                                 Parser->DoLines(curr, true);
159                 }
160
161                 switch (curr->registered)
162                 {
163                         case REG_ALL:
164                                 if (TIME > curr->nping)
165                                 {
166                                         // This user didn't answer the last ping, remove them
167                                         if (!curr->lastping)
168                                         {
169                                                 time_t time = this->Time() - (curr->nping - curr->MyClass->GetPingTime());
170                                                 char message[MAXBUF];
171                                                 snprintf(message, MAXBUF, "Ping timeout: %ld second%s", (long)time, time > 1 ? "s" : "");
172                                                 curr->lastping = 1;
173                                                 curr->nping = TIME + curr->MyClass->GetPingTime();
174                                                 this->Users->QuitUser(curr, message);
175                                                 continue;
176                                         }
177
178                                         curr->Write("PING :%s",this->Config->ServerName);
179                                         curr->lastping = 0;
180                                         curr->nping = TIME  +curr->MyClass->GetPingTime();
181                                 }
182                                 break;
183                         case REG_NICKUSER:
184                                 if (AllModulesReportReady(curr) && curr->dns_done)
185                                 {
186                                         /* User has sent NICK/USER, modules are okay, DNS finished. */
187                                         curr->FullConnect();
188                                         continue;
189                                 }
190                                 break;
191                 }
192
193                 if (curr->registered != REG_ALL && (TIME > (curr->age + curr->MyClass->GetRegTimeout())))
194                 {
195                         /*
196                          * registration timeout -- didnt send USER/NICK/HOST
197                          * in the time specified in their connection class.
198                          */
199                         this->Users->QuitUser(curr, "Registration timeout");
200                         continue;
201                 }
202         }
203 }
204