]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_ident.cpp
d1a399df3d664c164076568104697a3ce6b9b504
[user/henk/code/inspircd.git] / src / modules / m_ident.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 using namespace std;
18
19 #include <stdio.h>
20 #include <string>
21 #include "users.h"
22 #include "channels.h"
23 #include "modules.h"
24 #include "inspircd.h"
25
26 /* $ModDesc: Provides support for RFC 1413 ident lookups */
27
28 // Version 1.5.0.0 - Updated to use InspSocket, faster and neater.
29
30 class RFC1413 : public InspSocket
31 {
32  protected:
33         Server* Srv;             // Server* class used for core communications
34         sockaddr_in sock_us;     // our port number
35         sockaddr_in sock_them;   // their port number
36         socklen_t uslen;         // length of our port number
37         socklen_t themlen;       // length of their port number
38         char ident_request[128]; // buffer used to make up the request string
39  public:
40
41         userrec* u;              // user record that the lookup is associated with
42
43         RFC1413(userrec* user, int maxtime, Server* S) : InspSocket((char*)inet_ntoa(user->ip4), 113, false, maxtime), Srv(S), u(user)
44         {
45                 Srv->Log(DEBUG,"Ident: associated.");
46         }
47
48         virtual void OnTimeout()
49         {
50                 // When we timeout, the connection failed within the allowed timeframe,
51                 // so we just display a notice, and tidy off the ident_data.
52                 if (u)
53                 {
54                         u->Shrink("ident_data");
55                         Srv->SendServ(u->fd,"NOTICE "+std::string(u->nick)+" :*** Could not find your ident, using "+std::string(u->ident)+" instead.");
56                 }
57         }
58
59         virtual bool OnDataReady()
60         {
61                 char* ibuf = this->Read();
62                 if (ibuf)
63                 {
64                         char* savept;
65                         char* section = strtok_r(ibuf,":",&savept);
66                         while (section)
67                         {
68                                 if (strstr(section,"USERID"))
69                                 {
70                                         section = strtok_r(NULL,":",&savept);
71                                         if (section)
72                                         {
73                                                 // ID type, usually UNIX or OTHER... we dont want it, so read the next token
74                                                 section = strtok_r(NULL,":",&savept);
75                                                 if (section)
76                                                 {
77                                                         while (*section == ' ') section++; // strip leading spaces
78                                                         for (char* j = section; *j; j++)
79                                                         if ((*j < 33) || (*j > 126))
80                                                                 *j = '\0'; // truncate at invalid chars
81                                                         if (*section)
82                                                         {
83                                                                 if (u)
84                                                                 {
85                                                                         strlcpy(u->ident,section,IDENTMAX);
86                                                                         Srv->Log(DEBUG,"IDENT SET: "+std::string(u->ident));
87                                                                         Srv->SendServ(u->fd,"NOTICE "+std::string(u->nick)+" :*** Found your ident: "+std::string(u->ident));
88                                                                 }
89                                                         }
90                                                         return false;
91                                                 }
92                                         }
93                                 }
94                                 section = strtok_r(NULL,":",&savept);
95                         }
96                 }
97                 return false;
98         }
99
100         virtual void OnClose()
101         {
102                 // tidy up after ourselves when the connection is done.
103                 // We receive this event straight after a timeout, too.
104                 if (u)
105                 {
106                         u->Shrink("ident_data");
107                 }
108         }
109
110         virtual void OnError(InspSocketError e)
111         {
112                 if (u)
113                 {
114                         u->Shrink("ident_data");
115                 }
116         }
117
118         virtual bool OnConnected()
119         {
120                 if (u)
121                 {
122                         uslen = sizeof(sock_us);
123                         themlen = sizeof(sock_them);
124                         if ((getsockname(this->u->fd,(sockaddr*)&sock_us,&uslen) || getpeername(this->u->fd, (sockaddr*)&sock_them, &themlen)))
125                         {
126                                 Srv->Log(DEBUG,"Ident: failed to get socket names, bailing");
127                                 return false;
128                         }
129                         else
130                         {
131                                 // send the request in the following format: theirsocket,oursocket
132                                 snprintf(ident_request,127,"%d,%d\r\n",ntohs(sock_them.sin_port),ntohs(sock_us.sin_port));
133                                 this->Write(ident_request);
134                                 Srv->Log(DEBUG,"Sent ident request, waiting for reply");
135                                 return true;
136                         }
137                 }
138                 else
139                 {
140                         return true;
141                 }
142         }
143 };
144
145 class ModuleIdent : public Module
146 {
147
148         ConfigReader* Conf;
149         Server* Srv;
150         int IdentTimeout;
151
152  public:
153         void ReadSettings()
154         {
155                 Conf = new ConfigReader;
156                 IdentTimeout = Conf->ReadInteger("ident","timeout",0,true);
157                 if (!IdentTimeout)
158                         IdentTimeout = 1;
159                 DELETE(Conf);
160         }
161
162         ModuleIdent(Server* Me)
163                 : Module::Module(Me)
164         {
165                 Srv = Me;
166                 ReadSettings();
167         }
168
169         void Implements(char* List)
170         {
171                 List[I_OnCleanup] = List[I_OnRehash] = List[I_OnUserRegister] = List[I_OnCheckReady] = List[I_OnUserDisconnect] = 1;
172         }
173
174         virtual void OnRehash(const std::string &parameter)
175         {
176                 ReadSettings();
177         }
178
179         virtual void OnUserRegister(userrec* user)
180         {
181                 /*
182                  * when the new user connects, before they authenticate with USER/NICK/PASS, we do
183                  * their ident lookup. We do this by instantiating an object of type RFC1413, which
184                  * is derived from InspSocket, and inserting it into the socket engine using the
185                  * Server::AddSocket() call.
186                  */
187                 Srv->SendServ(user->fd,"NOTICE "+std::string(user->nick)+" :*** Looking up your ident...");
188                 RFC1413* ident = new RFC1413(user, IdentTimeout, Srv);
189                 if (ident->GetState() != I_ERROR)
190                 {
191                         user->Extend("ident_data", (char*)ident);
192                         Srv->AddSocket(ident);
193                 }
194                 else
195                 {
196                         Srv->SendServ(user->fd,"NOTICE "+std::string(user->nick)+" :*** Could not find your ident, using "+std::string(user->ident)+" instead.");
197                         DELETE(ident);
198                 }
199         }
200
201         virtual bool OnCheckReady(userrec* user)
202         {
203                 /*
204                  * The socket engine will clean up their ident request for us when it completes,
205                  * either due to timeout or due to closing, so, we just hold them until they dont
206                  * have an ident field any more.
207                  */
208                 RFC1413* ident = (RFC1413*)user->GetExt("ident_data");
209                 return (!ident);
210         }
211
212         virtual void OnCleanup(int target_type, void* item)
213         {
214                 if (target_type == TYPE_USER)
215                 {
216                         userrec* user = (userrec*)item;
217                         RFC1413* ident = (RFC1413*)user->GetExt("ident_data");
218                         if (ident)
219                         {
220                                 // FIX: If the user record is deleted, the socket wont be removed
221                                 // immediately so there is chance of the socket trying to write to
222                                 // a user which has now vanished! To prevent this, set ident::u
223                                 // to NULL and check it so that we dont write users who have gone away.
224                                 ident->u = NULL;
225                                 Srv->RemoveSocket(ident);
226                         }
227                 }
228         }
229
230         virtual void OnUserDisconnect(userrec* user)
231         {
232                 /*
233                  * when the user quits tidy up any ident lookup they have pending to keep things tidy.
234                  * When we call RemoveSocket, the abstractions tied into the system evnetually work their
235                  * way to RFC1459::OnClose(), which shrinks off the ident_data for us, so we dont need
236                  * to do it here. If we don't tidy this up, there may still be lingering idents for users
237                  * who have quit, as class RFC1459 is only loosely bound to userrec* via a pair of pointers
238                  * and this would leave at least one of the invalid ;)
239                  */
240                 RFC1413* ident = (RFC1413*)user->GetExt("ident_data");
241                 if (ident)
242                 {
243                         ident->u = NULL;
244                         Srv->RemoveSocket(ident);
245                 }
246         }
247         
248         virtual ~ModuleIdent()
249         {
250         }
251         
252         virtual Version GetVersion()
253         {
254                 return Version(1,5,0,0,VF_VENDOR);
255         }
256         
257 };
258
259 class ModuleIdentFactory : public ModuleFactory
260 {
261  public:
262         ModuleIdentFactory()
263         {
264         }
265         
266         ~ModuleIdentFactory()
267         {
268         }
269         
270         virtual Module * CreateModule(Server* Me)
271         {
272                 return new ModuleIdent(Me);
273         }
274         
275 };
276
277
278 extern "C" void * init_module( void )
279 {
280         return new ModuleIdentFactory;
281 }
282