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