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