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