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