]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_ident.cpp
* Fixed some incorrect declarations in IOCPEngine
[user/henk/code/inspircd.git] / src / modules / m_ident.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 "users.h"
16 #include "channels.h"
17 #include "modules.h"
18
19 /* $ModDesc: Provides support for RFC1413 ident lookups */
20
21 class IdentRequestSocket : public InspSocket
22 {
23  private:
24         userrec *user;
25  public:
26         IdentRequestSocket(InspIRCd *Server, userrec *user, int timeout, const std::string &bindip)
27                 : InspSocket(Server, user->GetIPString(), 113, false, timeout, bindip), user(user)
28         {
29                 
30         }
31         
32         virtual bool OnConnected()
33         {
34                 /* Both sockaddr_in and sockaddr_in6 can be safely casted to sockaddr, especially since the
35                  * only members we use are in a part of the struct that should always be identical (at the
36                  * byte level). */
37
38                 Instance->Log(DEBUG, "Sending ident request to %s", user->GetIPString());
39                 
40                 #ifndef IPV6
41                 sockaddr_in laddr, raddr;
42                 #else
43                 sockaddr_in6 laddr, raddr;
44                 #endif
45                 
46                 socklen_t laddrsz = sizeof(laddr);
47                 socklen_t raddrsz = sizeof(raddr);
48                 
49                 if ((getsockname(user->GetFd(), (sockaddr*) &laddr, &laddrsz) != 0) || (getpeername(user->GetFd(), (sockaddr*) &raddr, &raddrsz) != 0))
50                 {
51                         // Error
52                         return false;
53                 }
54                 
55                 char req[32];
56                 
57                 #ifndef IPV6
58                 snprintf(req, sizeof(req), "%d,%d\r\n", ntohs(raddr.sin_port), ntohs(laddr.sin_port));
59                 #else
60                 snprintf(req, sizeof(req), "%d,%d\r\n", ntohs(raddr.sin6_port), ntohs(laddr.sin6_port));
61                 #endif
62                 
63                 this->Write(req);
64                 
65                 return true;
66         }
67
68         virtual void OnClose()
69         {
70                 /* There used to be a check against the fd table here, to make sure the user hadn't been
71                  * deleted but not yet had their socket closed or something along those lines, dated june
72                  * 2006. Since we added the global cull list and such, I don't *think* that is necessary
73                  * anymore. If you're getting odd crashes here, that's probably why ;) -Special */
74
75                 if (*user->ident == '~' && user->GetExt("ident_socket"))
76                         user->WriteServ("NOTICE Auth :*** Could not find your ident, using %s instead", user->ident);
77
78                 user->Shrink("ident_socket");
79                 Instance->next_call = Instance->Time();
80         }
81         
82         virtual void OnError(InspSocketError e)
83         {
84                 // Quick check to make sure that this hasn't been sent ;)
85                 if (*user->ident == '~' && user->GetExt("ident_socket"))
86                         user->WriteServ("NOTICE Auth :*** Could not find your ident, using %s instead", user->ident);
87                 
88                 user->Shrink("ident_socket");
89                 Instance->next_call = Instance->Time();
90         }
91         
92         virtual bool OnDataReady()
93         {
94                 char *ibuf = this->Read();
95                 if (!ibuf)
96                         return false;
97                 
98                 // We don't really need to buffer for incomplete replies here, since IDENT replies are
99                 // extremely short - there is *no* sane reason it'd be in more than one packet
100                 
101                 irc::sepstream sep(ibuf, ':');
102                 std::string token;
103                 for (int i = 0; sep.GetToken(token); i++)
104                 {
105                         // We only really care about the 4th portion
106                         if (i < 3)
107                                 continue;
108                         
109                         char ident[IDENTMAX + 2];
110                         
111                         // Truncate the ident at any characters we don't like, skip leading spaces
112                         int k = 0;
113                         for (const char *j = token.c_str(); *j && (k < IDENTMAX + 1); j++)
114                         {
115                                 if (*j == ' ')
116                                         continue;
117                                 
118                                 // Rules taken from InspIRCd::IsIdent
119                                 if (((*j >= 'A') && (*j <= '}')) || ((*j >= '0') && (*j <= '9')) || (*j == '-') || (*j == '.'))
120                                 {
121                                         ident[k++] = *j;
122                                         continue;
123                                 }
124                                 
125                                 break;
126                         }
127                         
128                         ident[k] = '\0';
129                         
130                         // Redundant check with IsIdent, in case that changes and this doesn't (paranoia!)
131                         if (*ident && Instance->IsIdent(ident))
132                         {
133                                 strlcpy(user->ident, ident, IDENTMAX);
134                                 user->WriteServ("NOTICE Auth :*** Found your ident: %s", user->ident);
135                                 Instance->next_call = Instance->Time();
136                         }
137                         
138                         break;
139                 }
140                 
141                 return false;
142         }
143 };
144
145 class ModuleIdent : public Module
146 {
147  private:
148         int RequestTimeout;
149         std::string IdentBindIP;
150  public:
151         ModuleIdent(InspIRCd *Me)
152                 : Module(Me)
153         {
154                 OnRehash(NULL, "");
155         }
156         
157         virtual ~ModuleIdent()
158         {
159         }
160         
161         virtual Version GetVersion()
162         {
163                 return Version(1, 1, 1, 0, VF_VENDOR, API_VERSION);
164         }
165         
166         virtual void Implements(char *List)
167         {
168                 List[I_OnRehash] = List[I_OnUserRegister] = List[I_OnCheckReady] = List[I_OnCleanup] = List[I_OnUserDisconnect] = 1;
169         }
170         
171         virtual void OnRehash(userrec *user, const std::string &param)
172         {
173                 ConfigReader MyConf(ServerInstance);
174                 
175                 RequestTimeout = MyConf.ReadInteger("ident", "timeout", 0, true);
176                 if (!RequestTimeout)
177                         RequestTimeout = 5;
178                 IdentBindIP = MyConf.ReadValue("ident", "bind", 0);
179         }
180         
181         virtual int OnUserRegister(userrec *user)
182         {
183                 /* userrec::ident is currently the username field from USER; with m_ident loaded, that
184                  * should be preceded by a ~. The field is actually IDENTMAX+2 characters wide. */
185                 memmove(user->ident + 1, user->ident, IDENTMAX);
186                 user->ident[0] = '~';
187                 // Ensure that it is null terminated
188                 user->ident[IDENTMAX + 1] = '\0';
189                 
190                 user->WriteServ("NOTICE Auth :*** Looking up your ident...");
191                 
192                 IdentRequestSocket *isock = new IdentRequestSocket(ServerInstance, user, RequestTimeout, IdentBindIP);
193                 user->Extend("ident_socket", isock);
194                 return 0;
195         }
196         
197         virtual bool OnCheckReady(userrec *user)
198         {
199                 return (!user->GetExt("ident_socket"));
200         }
201         
202         virtual void OnCleanup(int target_type, void *item)
203         {
204                 if (target_type == TYPE_USER)
205                 {
206                         IdentRequestSocket *isock;
207                         userrec *user = (userrec*)item;
208                         if (user->GetExt("ident_socket", isock))
209                                 isock->Close();
210                 }
211         }
212         
213         virtual void OnUserDisconnect(userrec *user)
214         {
215                 IdentRequestSocket *isock;
216                 if (user->GetExt("ident_socket", isock))
217                         isock->Close();
218         }
219 };
220
221 MODULE_INIT(ModuleIdent);