]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/resolve.c
Only log "IDENT ... no result" when IDENT was looked up
[ngircd-alex.git] / src / ngircd / resolve.c
1 /*
2  * ngIRCd -- The Next Generation IRC Daemon
3  * Copyright (c)2001-2011 Alexander Barton (alex@barton.de) and Contributors.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  * Please read the file COPYING, README and AUTHORS for more information.
10  */
11
12 #define RESOLVER_TIMEOUT (Conf_PongTimeout*3)/4
13
14 #include "portab.h"
15
16 /**
17  * @file
18  * Asynchronous resolver
19  */
20
21 #include "imp.h"
22 #include <assert.h>
23 #include <errno.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <sys/socket.h>
28 #include <netinet/in.h>
29 #include <netdb.h>
30 #include <signal.h>
31
32 #ifdef IDENTAUTH
33 #ifdef HAVE_IDENT_H
34 #include <ident.h>
35 #endif
36 #endif
37
38 #include "array.h"
39 #include "conn.h"
40 #include "conf.h"
41 #include "defines.h"
42 #include "log.h"
43 #include "ng_ipaddr.h"
44
45 #include "exp.h"
46 #include "resolve.h"
47 #include "io.h"
48
49
50 static void Do_ResolveAddr PARAMS(( const ng_ipaddr_t *Addr, int Sock, int w_fd ));
51 static void Do_ResolveName PARAMS(( const char *Host, int w_fd ));
52
53 #ifdef WANT_IPV6
54 extern bool Conf_ConnectIPv4;
55 extern bool Conf_ConnectIPv6;
56 #endif
57
58
59 /**
60  * Resolve IP (asynchronous!).
61  */
62 GLOBAL bool
63 Resolve_Addr(PROC_STAT * s, const ng_ipaddr_t *Addr, int identsock,
64              void (*cbfunc) (int, short))
65 {
66         int pipefd[2];
67         pid_t pid;
68
69         assert(s != NULL);
70
71         pid = Proc_Fork(s, pipefd, cbfunc, RESOLVER_TIMEOUT);
72         if (pid > 0) {
73                 LogDebug("Resolver for %s created (PID %d).", ng_ipaddr_tostr(Addr), pid);
74                 return true;
75         } else if( pid == 0 ) {
76                 /* Sub process */
77                 Log_Init_Subprocess("Resolver");
78                 Conn_CloseAllSockets(identsock);
79                 Do_ResolveAddr(Addr, identsock, pipefd[1]);
80                 Log_Exit_Subprocess("Resolver");
81                 exit(0);
82         }
83         return false;
84 } /* Resolve_Addr */
85
86
87 /**
88  * Resolve hostname (asynchronous!).
89  */
90 GLOBAL bool
91 Resolve_Name( PROC_STAT *s, const char *Host, void (*cbfunc)(int, short))
92 {
93         int pipefd[2];
94         pid_t pid;
95
96         assert(s != NULL);
97
98         pid = Proc_Fork(s, pipefd, cbfunc, RESOLVER_TIMEOUT);
99         if (pid > 0) {
100                 /* Main process */
101 #ifdef DEBUG
102                 Log( LOG_DEBUG, "Resolver for \"%s\" created (PID %d).", Host, pid );
103 #endif
104                 return true;
105         } else if( pid == 0 ) {
106                 /* Sub process */
107                 Log_Init_Subprocess("Resolver");
108                 Conn_CloseAllSockets(NONE);
109                 Do_ResolveName(Host, pipefd[1]);
110                 Log_Exit_Subprocess("Resolver");
111                 exit(0);
112         }
113         return false;
114 } /* Resolve_Name */
115
116
117 #if !defined(HAVE_GETADDRINFO) || !defined(HAVE_GETNAMEINFO)
118 #if !defined(WANT_IPV6) && defined(h_errno)
119 static char *
120 Get_Error( int H_Error )
121 {
122         /* Get error message for H_Error */
123         switch( H_Error ) {
124         case HOST_NOT_FOUND:
125                 return "host not found";
126         case NO_DATA:
127                 return "name valid but no IP address defined";
128         case NO_RECOVERY:
129                 return "name server error";
130         case TRY_AGAIN:
131                 return "name server temporary not available";
132         }
133         return "unknown error";
134 }
135 #endif
136 #endif
137
138
139 /* Do "IDENT" (aka "AUTH") lookup and append result to resolved_addr array */
140 static void
141 Do_IdentQuery(int identsock, array *resolved_addr)
142 {
143 #ifdef IDENTAUTH
144         char *res;
145
146         if (identsock < 0)
147                 return;
148
149 #ifdef DEBUG
150         Log_Subprocess(LOG_DEBUG, "Doing IDENT lookup on socket %d ...",
151                        identsock);
152 #endif
153         res = ident_id( identsock, 10 );
154 #ifdef DEBUG
155         Log_Subprocess(LOG_DEBUG, "Ok, IDENT lookup on socket %d done: \"%s\"",
156                        identsock, res ? res : "(NULL)");
157 #endif
158         if (!res) /* no result */
159                 return;
160         if (!array_cats(resolved_addr, res))
161                 Log_Subprocess(LOG_WARNING,
162                                "Resolver: Cannot copy IDENT result: %s!",
163                                strerror(errno));
164
165         free(res);
166 #else
167         (void) identsock;
168         (void) resolved_addr;
169 #endif
170 }
171
172
173 /**
174  * perform reverse DNS lookup and put result string into resbuf.
175  * If no hostname could be obtained, this function stores the string representation of
176  * the IP address in resbuf and returns false.
177  * @param IpAddr ip address to resolve
178  * @param resbuf result buffer to store DNS name/string representation of ip address
179  * @param reslen size of result buffer (must be >= NGT_INET_ADDRSTRLEN)
180  * @return true if reverse lookup successful, false otherwise
181  */
182 static bool
183 ReverseLookup(const ng_ipaddr_t *IpAddr, char *resbuf, size_t reslen)
184 {
185         char tmp_ip_str[NG_INET_ADDRSTRLEN];
186         const char *errmsg;
187 #ifdef HAVE_GETNAMEINFO
188         static const char funcname[]="getnameinfo";
189         int res;
190
191         *resbuf = 0;
192
193         res = getnameinfo((struct sockaddr *) IpAddr, ng_ipaddr_salen(IpAddr),
194                           resbuf, (socklen_t)reslen, NULL, 0, NI_NAMEREQD);
195         if (res == 0)
196                 return true;
197
198         if (res == EAI_SYSTEM)
199                 errmsg = strerror(errno);
200         else
201                 errmsg = gai_strerror(res);
202 #else
203         const struct sockaddr_in *Addr = (const struct sockaddr_in *) IpAddr;
204         struct hostent *h;
205         static const char funcname[]="gethostbyaddr";
206
207         h = gethostbyaddr((char *)&Addr->sin_addr, sizeof(Addr->sin_addr), AF_INET);
208         if (h) {
209                 if (strlcpy(resbuf, h->h_name, reslen) < reslen)
210                         return true;
211                 errmsg = "hostname too long";
212         } else {
213 # ifdef h_errno
214                 errmsg = Get_Error(h_errno);
215 # else
216                 errmsg = "unknown error";
217 # endif /* h_errno */
218         }
219 #endif  /* HAVE_GETNAMEINFO */
220
221         assert(errmsg);
222         assert(reslen >= NG_INET_ADDRSTRLEN);
223         ng_ipaddr_tostr_r(IpAddr, tmp_ip_str);
224
225         Log_Subprocess(LOG_WARNING, "%s: Can't resolve address \"%s\": %s",
226                                 funcname, tmp_ip_str, errmsg);
227         strlcpy(resbuf, tmp_ip_str, reslen);
228         return false;
229 }
230
231
232 /**
233  * perform DNS lookup of given host name and fill IpAddr with a list of
234  * ip addresses associated with that name.
235  * ip addresses found are stored in the "array *IpAddr" argument (type ng_ipaddr_t)
236  * @param hostname The domain name to look up.
237  * @param IpAddr pointer to empty and initialized array to store results
238  * @return true if lookup successful, false if domain name not found
239  */
240 static bool
241 ForwardLookup(const char *hostname, array *IpAddr, int af)
242 {
243         ng_ipaddr_t addr;
244
245 #ifdef HAVE_GETADDRINFO
246         int res;
247         struct addrinfo *a, *ai_results;
248         static struct addrinfo hints;
249
250 #ifdef AI_ADDRCONFIG    /* glibc has this, but not e.g. netbsd 4.0 */
251         hints.ai_flags = AI_ADDRCONFIG;
252 #endif
253         hints.ai_socktype = SOCK_STREAM;
254         hints.ai_protocol = IPPROTO_TCP;
255         hints.ai_family = af;
256
257         memset(&addr, 0, sizeof(addr));
258
259         res = getaddrinfo(hostname, NULL, &hints, &ai_results);
260         switch (res) {
261         case 0: break;
262         case EAI_SYSTEM:
263                 Log_Subprocess(LOG_WARNING, "Can't resolve \"%s\": %s", hostname, strerror(errno));
264                 return false;
265         default:
266                 Log_Subprocess(LOG_WARNING, "Can't resolve \"%s\": %s", hostname, gai_strerror(res));
267                 return false;
268         }
269
270         for (a = ai_results; a != NULL; a = a->ai_next) {
271                 assert((size_t)a->ai_addrlen <= sizeof(addr));
272
273                 if ((size_t)a->ai_addrlen > sizeof(addr))
274                         continue;
275
276                 memcpy(&addr, a->ai_addr, a->ai_addrlen);
277
278                 if (!array_catb(IpAddr, (char *)&addr, sizeof(addr)))
279                         break;
280         }
281
282         freeaddrinfo(ai_results);
283         return a == NULL;
284 #else
285         struct hostent *h = gethostbyname(hostname);
286
287         if (!h) {
288 #ifdef h_errno
289                 Log_Subprocess(LOG_WARNING, "Can't resolve \"%s\": %s",
290                                hostname, Get_Error(h_errno));
291 #else
292                 Log_Subprocess(LOG_WARNING, "Can't resolve \"%s\"", hostname);
293 #endif
294                 return false;
295         }
296         memset(&addr, 0, sizeof(addr));
297
298         addr.sin4.sin_family = AF_INET;
299         memcpy(&addr.sin4.sin_addr, h->h_addr, sizeof(struct in_addr));
300
301         return array_copyb(IpAddr, (char *)&addr, sizeof(addr));
302 #endif /* HAVE_GETADDRINFO */
303 }
304
305
306 static bool
307 Addr_in_list(const array *resolved_addr, const ng_ipaddr_t *Addr)
308 {
309         char tmp_ip_str[NG_INET_ADDRSTRLEN];
310         const ng_ipaddr_t *tmpAddrs = array_start(resolved_addr);
311         size_t len = array_length(resolved_addr, sizeof(*tmpAddrs));
312
313         assert(len > 0);
314         assert(tmpAddrs);
315
316         while (len > 0) {
317                 if (ng_ipaddr_ipequal(Addr, tmpAddrs))
318                         return true;
319                 tmpAddrs++;
320                 len--;
321         }
322         /* failed; print list of addresses */
323         ng_ipaddr_tostr_r(Addr, tmp_ip_str);
324         len = array_length(resolved_addr, sizeof(*tmpAddrs));
325         tmpAddrs = array_start(resolved_addr);
326
327         while (len > 0) {
328                 Log_Subprocess(LOG_WARNING, "Address mismatch: %s != %s",
329                         tmp_ip_str, ng_ipaddr_tostr(tmpAddrs));
330                 tmpAddrs++;
331                 len--;
332         }
333
334         return false;
335 }
336
337
338 static void
339 Log_Forgery_NoIP(const char *ip, const char *host)
340 {
341         Log_Subprocess(LOG_WARNING,
342                 "Possible forgery: %s resolved to %s (which has no ip address)", ip, host);
343 }
344
345 static void
346 Log_Forgery_WrongIP(const char *ip, const char *host)
347 {
348         Log_Subprocess(LOG_WARNING,
349                 "Possible forgery: %s resolved to %s (which points to different address)", ip, host);
350 }
351
352
353 static void
354 ArrayWrite(int fd, const array *a)
355 {
356         size_t len = array_bytes(a);
357         const char *data = array_start(a);
358
359         assert(data);
360
361         if( (size_t)write(fd, data, len) != len )
362                 Log_Subprocess( LOG_CRIT, "Resolver: Can't write to parent: %s!",
363                                                         strerror(errno));
364 }
365
366
367 static void
368 Do_ResolveAddr(const ng_ipaddr_t *Addr, int identsock, int w_fd)
369 {
370         /* Resolver sub-process: resolve IP address and write result into
371          * pipe to parent. */
372         char hostname[CLIENT_HOST_LEN];
373         char tmp_ip_str[NG_INET_ADDRSTRLEN];
374         size_t len;
375         array resolved_addr;
376
377         array_init(&resolved_addr);
378         ng_ipaddr_tostr_r(Addr, tmp_ip_str);
379 #ifdef DEBUG
380         Log_Subprocess(LOG_DEBUG, "Now resolving %s ...", tmp_ip_str);
381 #endif
382         if (!ReverseLookup(Addr, hostname, sizeof(hostname)))
383                 goto dns_done;
384
385         if (ForwardLookup(hostname, &resolved_addr, ng_ipaddr_af(Addr))) {
386                 if (!Addr_in_list(&resolved_addr, Addr)) {
387                         Log_Forgery_WrongIP(tmp_ip_str, hostname);
388                         strlcpy(hostname, tmp_ip_str, sizeof(hostname));
389                 }
390         } else {
391                 Log_Forgery_NoIP(tmp_ip_str, hostname);
392                 strlcpy(hostname, tmp_ip_str, sizeof(hostname));
393         }
394 #ifdef DEBUG
395         Log_Subprocess(LOG_DEBUG, "Ok, translated %s to \"%s\".", tmp_ip_str, hostname);
396 #endif
397  dns_done:
398         len = strlen(hostname);
399         hostname[len] = '\n';
400         if (!array_copyb(&resolved_addr, hostname, ++len)) {
401                 Log_Subprocess(LOG_CRIT,
402                                "Resolver: Can't copy resolved name: %s!",
403                                strerror(errno));
404                 array_free(&resolved_addr);
405                 return;
406         }
407
408         Do_IdentQuery(identsock, &resolved_addr);
409
410         ArrayWrite(w_fd, &resolved_addr);
411
412         array_free(&resolved_addr);
413 } /* Do_ResolveAddr */
414
415
416 static void
417 Do_ResolveName( const char *Host, int w_fd )
418 {
419         /* Resolver sub-process: resolve name and write result into pipe
420          * to parent. */
421         array IpAddrs;
422         int af;
423 #ifdef DEBUG
424         ng_ipaddr_t *addr;
425         size_t len;
426 #endif
427         Log_Subprocess(LOG_DEBUG, "Now resolving \"%s\" ...", Host);
428
429         array_init(&IpAddrs);
430
431 #ifdef WANT_IPV6
432         af = AF_UNSPEC;
433         assert(Conf_ConnectIPv6 || Conf_ConnectIPv4);
434
435         if (!Conf_ConnectIPv6)
436                 af = AF_INET;
437         if (!Conf_ConnectIPv4)
438                 af = AF_INET6;
439 #else
440         af = AF_INET;
441 #endif
442         if (!ForwardLookup(Host, &IpAddrs, af)) {
443                 close(w_fd);
444                 return;
445         }
446 #ifdef DEBUG
447         len = array_length(&IpAddrs, sizeof(*addr));
448         assert(len > 0);
449         addr = array_start(&IpAddrs);
450         assert(addr);
451         for (; len > 0; --len,addr++) {
452                 Log_Subprocess(LOG_DEBUG, "translated \"%s\" to %s.",
453                                         Host, ng_ipaddr_tostr(addr));
454         }
455 #endif
456         /* Write result into pipe to parent */
457         ArrayWrite(w_fd, &IpAddrs);
458
459         array_free(&IpAddrs);
460 } /* Do_ResolveName */
461
462
463 /* -eof- */