]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/userprocess.cpp
41c5ae4cd73b78941dc8d5c5e16ad94e6d39a553
[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
76                 Server->stats->statsRecv += result;
77                 /*
78                  * perform a check on the raw buffer as an array (not a string!) to remove
79                  * character 0 which is illegal in the RFC - replace them with spaces.
80                  */
81
82                 for (int checker = 0; checker < result; checker++)
83                 {
84                         if (ReadBuffer[checker] == 0)
85                                 ReadBuffer[checker] = ' ';
86                 }
87
88                 if (result > 0)
89                         ReadBuffer[result] = '\0';
90
91                 current = cu;
92                 currfd = current->GetFd();
93
94                 // add the data to the users buffer
95                 if (result > 0)
96                 {
97                         if (!current->AddBuffer(ReadBuffer))
98                         {
99                                 // AddBuffer returned false, theres too much data in the user's buffer and theyre up to no good.
100                                 if (current->registered == REG_ALL)
101                                 {
102                                         // Make sure they arn't flooding long lines.
103                                         if (Server->Time() > current->reset_due)
104                                         {
105                                                 current->reset_due = Server->Time() + current->threshold;
106                                                 current->lines_in = 0;
107                                         }
108
109                                         current->lines_in++;
110
111                                         if (current->flood && current->lines_in > current->flood)
112                                                 Server->FloodQuitUser(current);
113                                         else
114                                         {
115                                                 current->WriteServ("NOTICE %s :Your previous line was too long and was not delivered (Over %d chars) Please shorten it.", current->nick, MAXBUF-2);
116                                                 current->recvq.clear();
117                                         }
118                                 }
119                                 else
120                                         Server->FloodQuitUser(current);
121
122                                 return;
123                         }
124
125                         /* If user is over penalty, dont process here, just build up */
126                         if (!current->OverPenalty)
127                                 Server->Parser->DoLines(current);
128
129                         return;
130                 }
131
132                 if ((result == -1) && (errno != EAGAIN) && (errno != EINTR))
133                 {
134                         User::QuitUser(Server, cu, errno ? strerror(errno) : "EOF from client");
135                         return;
136                 }
137         }
138
139         // result EAGAIN means nothing read
140         else if ((result == EAGAIN) || (result == -EAGAIN))
141         {
142                 /* do nothing */
143         }
144         else if (result == 0)
145         {
146                 User::QuitUser(Server, cu, "Connection closed");
147                 return;
148         }
149 }
150
151 /**
152  * This function is called once a second from the mainloop.
153  * It is intended to do background checking on all the user structs, e.g.
154  * stuff like ping checks, registration timeouts, etc.
155  */
156 void InspIRCd::DoBackgroundUserStuff()
157 {
158         /*
159          * loop over all local users..
160          */
161         for (std::vector<User*>::iterator count2 = local_users.begin(); count2 != local_users.end(); count2++)
162         {
163                 User *curr = *count2;
164
165                 if (curr->Penalty)
166                         curr->Penalty--;
167
168                 if (curr->OverPenalty)
169                 {
170                         if (curr->sendq.empty())
171                                 curr->OverPenalty = false;
172
173                         if (curr->Penalty < 10)
174                                 Parser->DoLines(curr, true);
175                 }
176
177                 if ((curr->registered != REG_ALL) && (TIME > curr->timeout))
178                 {
179                         /*
180                          * registration timeout -- didnt send USER/NICK/HOST
181                          * in the time specified in their connection class.
182                          */
183                         curr->muted = true;
184                         User::QuitUser(this, curr, "Registration timeout");
185                         continue;
186                 }
187
188                 /*
189                  * `ready` means that the user has provided NICK/USER(/PASS), and all modules agree
190                  * that the user is okay to proceed. The one thing we are then waiting for is DNS, which we do here...
191                  */
192                 bool ready = ((curr->registered == REG_NICKUSER) && AllModulesReportReady(curr));
193
194                 if (ready)
195                 {
196                         if (!curr->dns_done)
197                         {
198                                 /*
199                                  * DNS isn't done yet?
200                                  * Cool. Check for timeout.
201                                  */
202                                 if (TIME > curr->signon)
203                                 {
204                                         /* FZZZZZZZZT, timeout! */
205                                         curr->WriteServ("NOTICE Auth :*** Could not resolve your hostname: Request timed out; using your IP address (%s) instead.", curr->GetIPString());
206                                         curr->dns_done = true;
207                                         this->stats->statsDnsBad++;
208                                         curr->FullConnect();
209                                         continue;
210                                 }
211                         }
212                         else
213                         {
214                                 /* DNS passed, connect the user */
215                                 curr->FullConnect();
216                                 continue;
217                         }
218                 }
219
220                 // It's time to PING this user. Send them a ping.
221                 if ((TIME > curr->nping) && (curr->registered == REG_ALL))
222                 {
223                         // This user didn't answer the last ping, remove them
224                         if (!curr->lastping)
225                         {
226                                 time_t time = this->Time(false) - (curr->nping - curr->pingmax);
227                                 char message[MAXBUF];
228                                 snprintf(message, MAXBUF, "Ping timeout: %ld second%s", (long)time, time > 1 ? "s" : "");
229                                 curr->muted = true;
230                                 curr->lastping = 1;
231                                 curr->nping = TIME+curr->pingmax;
232                                 User::QuitUser(this, curr, message);
233                                 continue;
234                         }
235                         curr->Write("PING :%s",this->Config->ServerName);
236                         curr->lastping = 0;
237                         curr->nping = TIME+curr->pingmax;
238                 }
239         }
240 }
241