]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_ident.cpp
Made m_ident bind ident requests to the same IP the user is connected on, which is...
[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  public:
150         ModuleIdent(InspIRCd *Me)
151                 : Module(Me)
152         {
153                 OnRehash(NULL, "");
154         }
155         
156         virtual ~ModuleIdent()
157         {
158         }
159         
160         virtual Version GetVersion()
161         {
162                 return Version(1, 1, 1, 0, VF_VENDOR, API_VERSION);
163         }
164         
165         virtual void Implements(char *List)
166         {
167                 List[I_OnRehash] = List[I_OnUserRegister] = List[I_OnCheckReady] = List[I_OnCleanup] = List[I_OnUserDisconnect] = 1;
168         }
169         
170         virtual void OnRehash(userrec *user, const std::string &param)
171         {
172                 ConfigReader MyConf(ServerInstance);
173                 
174                 RequestTimeout = MyConf.ReadInteger("ident", "timeout", 0, true);
175                 if (!RequestTimeout)
176                         RequestTimeout = 5;
177         }
178         
179         virtual int OnUserRegister(userrec *user)
180         {
181                 /* userrec::ident is currently the username field from USER; with m_ident loaded, that
182                  * should be preceded by a ~. The field is actually IDENTMAX+2 characters wide. */
183                 memmove(user->ident + 1, user->ident, IDENTMAX);
184                 user->ident[0] = '~';
185                 // Ensure that it is null terminated
186                 user->ident[IDENTMAX + 1] = '\0';
187                 
188                 user->WriteServ("NOTICE Auth :*** Looking up your ident...");
189                 
190                 // Get the IP that the user is connected to, and bind to that for the outgoing connection
191                 #ifndef IPV6
192                 sockaddr_in laddr;
193                 #else
194                 sockaddr_in6 laddr;
195                 #endif
196                 socklen_t laddrsz = sizeof(laddr);
197                 
198                 if (getsockname(user->GetFd(), (sockaddr*) &laddr, &laddrsz) != 0)
199                 {
200                         user->WriteServ("NOTICE Auth :*** Could not find your ident, using %s instead.", user->ident);
201                         return 0;
202                 }
203                 
204                 #ifndef IPV6
205                 const char *ip = inet_ntoa(laddr.sin_addr);
206                 #else
207                 char ip[INET6_ADDRSTRLEN + 1];
208                 inet_ntop(laddr.sin6_family, laddr.sin6_addr, ip, INET6_ADDRSTRLEN);
209                 #endif
210                 
211                 IdentRequestSocket *isock = new IdentRequestSocket(ServerInstance, user, RequestTimeout, ip);
212                 user->Extend("ident_socket", isock);
213                 return 0;
214         }
215         
216         virtual bool OnCheckReady(userrec *user)
217         {
218                 return (!user->GetExt("ident_socket"));
219         }
220         
221         virtual void OnCleanup(int target_type, void *item)
222         {
223                 if (target_type == TYPE_USER)
224                 {
225                         IdentRequestSocket *isock;
226                         userrec *user = (userrec*)item;
227                         if (user->GetExt("ident_socket", isock))
228                                 isock->Close();
229                 }
230         }
231         
232         virtual void OnUserDisconnect(userrec *user)
233         {
234                 IdentRequestSocket *isock;
235                 if (user->GetExt("ident_socket", isock))
236                         isock->Close();
237         }
238 };
239
240 MODULE_INIT(ModuleIdent);