]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/resolve.c
Don't abort startup when setgid/setuid() fails with EINVAL
[ngircd-alex.git] / src / ngircd / resolve.c
1 /*
2  * ngIRCd -- The Next Generation IRC Daemon
3  * Copyright (c)2001-2014 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 <assert.h>
22 #include <errno.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include <sys/stat.h>
27 #include <sys/types.h>
28 #include <sys/socket.h>
29 #include <netinet/in.h>
30 #include <netdb.h>
31
32 #ifdef IDENTAUTH
33 #ifdef HAVE_IDENT_H
34 #include <ident.h>
35 #endif
36 #endif
37
38 #include "conn.h"
39 #include "conf.h"
40 #include "log.h"
41 #include "ng_ipaddr.h"
42
43 #include "resolve.h"
44
45 static void Do_ResolveAddr PARAMS(( const ng_ipaddr_t *Addr, int Sock, int w_fd ));
46 static void Do_ResolveName PARAMS(( const char *Host, int w_fd ));
47
48 #ifdef WANT_IPV6
49 extern bool Conf_ConnectIPv4;
50 extern bool Conf_ConnectIPv6;
51 #endif
52
53
54 /**
55  * Resolve IP (asynchronous!).
56  */
57 GLOBAL bool
58 Resolve_Addr(PROC_STAT * s, const ng_ipaddr_t *Addr, int identsock,
59              void (*cbfunc) (int, short))
60 {
61         int pipefd[2];
62         pid_t pid;
63
64         assert(s != NULL);
65
66         pid = Proc_Fork(s, pipefd, cbfunc, RESOLVER_TIMEOUT);
67         if (pid > 0) {
68                 LogDebug("Resolver for %s created (PID %d).", ng_ipaddr_tostr(Addr), pid);
69                 return true;
70         } else if( pid == 0 ) {
71                 /* Sub process */
72                 Log_Init_Subprocess("Resolver");
73                 Conn_CloseAllSockets(identsock);
74                 Do_ResolveAddr(Addr, identsock, pipefd[1]);
75                 Log_Exit_Subprocess("Resolver");
76                 exit(0);
77         }
78         return false;
79 } /* Resolve_Addr */
80
81
82 /**
83  * Resolve hostname (asynchronous!).
84  */
85 GLOBAL bool
86 Resolve_Name( PROC_STAT *s, const char *Host, void (*cbfunc)(int, short))
87 {
88         int pipefd[2];
89         pid_t pid;
90
91         assert(s != NULL);
92
93         pid = Proc_Fork(s, pipefd, cbfunc, RESOLVER_TIMEOUT);
94         if (pid > 0) {
95                 /* Main process */
96 #ifdef DEBUG
97                 Log( LOG_DEBUG, "Resolver for \"%s\" created (PID %d).", Host, pid );
98 #endif
99                 return true;
100         } else if( pid == 0 ) {
101                 /* Sub process */
102                 Log_Init_Subprocess("Resolver");
103                 Conn_CloseAllSockets(NONE);
104                 Do_ResolveName(Host, pipefd[1]);
105                 Log_Exit_Subprocess("Resolver");
106                 exit(0);
107         }
108         return false;
109 } /* Resolve_Name */
110
111 #if !defined(HAVE_WORKING_GETADDRINFO) || !defined(HAVE_GETNAMEINFO)
112 #ifdef h_errno
113 static char *
114 Get_Error( int H_Error )
115 {
116         /* Get error message for H_Error */
117         switch( H_Error ) {
118         case HOST_NOT_FOUND:
119                 return "host not found";
120         case NO_DATA:
121                 return "name valid but no IP address defined";
122         case NO_RECOVERY:
123                 return "name server error";
124         case TRY_AGAIN:
125                 return "name server temporary not available";
126         }
127         return "unknown error";
128 }
129 #endif
130 #endif
131
132
133 /* Do "IDENT" (aka "AUTH") lookup and append result to resolved_addr array */
134 static void
135 Do_IdentQuery(int identsock, array *resolved_addr)
136 {
137 #ifdef IDENTAUTH
138         char *res;
139
140         if (identsock < 0)
141                 return;
142
143 #ifdef DEBUG
144         Log_Subprocess(LOG_DEBUG, "Doing IDENT lookup on socket %d ...",
145                        identsock);
146 #endif
147         res = ident_id( identsock, 10 );
148 #ifdef DEBUG
149         Log_Subprocess(LOG_DEBUG, "Ok, IDENT lookup on socket %d done: \"%s\"",
150                        identsock, res ? res : "(NULL)");
151 #endif
152         if (!res) /* no result */
153                 return;
154         if (!array_cats(resolved_addr, res))
155                 Log_Subprocess(LOG_WARNING,
156                                "Resolver: Cannot copy IDENT result: %s!",
157                                strerror(errno));
158
159         free(res);
160 #else
161         (void) identsock;
162         (void) resolved_addr;
163 #endif
164 }
165
166
167 /**
168  * perform reverse DNS lookup and put result string into resbuf.
169  * If no hostname could be obtained, this function stores the string representation of
170  * the IP address in resbuf and returns false.
171  * @param IpAddr ip address to resolve
172  * @param resbuf result buffer to store DNS name/string representation of ip address
173  * @param reslen size of result buffer (must be >= NGT_INET_ADDRSTRLEN)
174  * @return true if reverse lookup successful, false otherwise
175  */
176 static bool
177 ReverseLookup(const ng_ipaddr_t *IpAddr, char *resbuf, size_t reslen)
178 {
179         char tmp_ip_str[NG_INET_ADDRSTRLEN];
180         const char *errmsg;
181 #ifdef HAVE_GETNAMEINFO
182         static const char funcname[]="getnameinfo";
183         int res;
184
185         *resbuf = 0;
186
187         res = getnameinfo((struct sockaddr *) IpAddr, ng_ipaddr_salen(IpAddr),
188                           resbuf, (socklen_t)reslen, NULL, 0, NI_NAMEREQD);
189         if (res == 0)
190                 return true;
191
192         if (res == EAI_SYSTEM)
193                 errmsg = strerror(errno);
194         else
195                 errmsg = gai_strerror(res);
196 #else
197         const struct sockaddr_in *Addr = (const struct sockaddr_in *) IpAddr;
198         struct hostent *h;
199         static const char funcname[]="gethostbyaddr";
200
201         h = gethostbyaddr((char *)&Addr->sin_addr, sizeof(Addr->sin_addr), AF_INET);
202         if (h) {
203                 if (strlcpy(resbuf, h->h_name, reslen) < reslen)
204                         return true;
205                 errmsg = "hostname too long";
206         } else {
207 # ifdef h_errno
208                 errmsg = Get_Error(h_errno);
209 # else
210                 errmsg = "unknown error";
211 # endif /* h_errno */
212         }
213 #endif  /* HAVE_GETNAMEINFO */
214
215         assert(errmsg);
216         assert(reslen >= NG_INET_ADDRSTRLEN);
217         ng_ipaddr_tostr_r(IpAddr, tmp_ip_str);
218
219         Log_Subprocess(LOG_WARNING, "Can't resolve address \"%s\": %s [%s].",
220                        tmp_ip_str, errmsg, funcname);
221         strlcpy(resbuf, tmp_ip_str, reslen);
222         return false;
223 }
224
225
226 /**
227  * perform DNS lookup of given host name and fill IpAddr with a list of
228  * ip addresses associated with that name.
229  * ip addresses found are stored in the "array *IpAddr" argument (type ng_ipaddr_t)
230  * @param hostname The domain name to look up.
231  * @param IpAddr pointer to empty and initialized array to store results
232  * @return true if lookup successful, false if domain name not found
233  */
234 static bool
235 #ifdef HAVE_WORKING_GETADDRINFO
236 ForwardLookup(const char *hostname, array *IpAddr, int af)
237 #else
238 ForwardLookup(const char *hostname, array *IpAddr, UNUSED int af)
239 #endif
240 {
241         ng_ipaddr_t addr;
242
243 #ifdef HAVE_WORKING_GETADDRINFO
244         int res;
245         struct addrinfo *a, *ai_results;
246         static struct addrinfo hints;
247
248 #ifdef AI_ADDRCONFIG    /* glibc has this, but not e.g. netbsd 4.0 */
249         hints.ai_flags = AI_ADDRCONFIG;
250 #endif
251         hints.ai_socktype = SOCK_STREAM;
252         hints.ai_protocol = IPPROTO_TCP;
253         hints.ai_family = af;
254
255         memset(&addr, 0, sizeof(addr));
256
257         res = getaddrinfo(hostname, NULL, &hints, &ai_results);
258         switch (res) {
259         case 0: break;
260         case EAI_SYSTEM:
261                 Log_Subprocess(LOG_WARNING, "Can't resolve \"%s\": %s", hostname, strerror(errno));
262                 return false;
263         default:
264                 Log_Subprocess(LOG_WARNING, "Can't resolve \"%s\": %s", hostname, gai_strerror(res));
265                 return false;
266         }
267
268         for (a = ai_results; a != NULL; a = a->ai_next) {
269                 assert((size_t)a->ai_addrlen <= sizeof(addr));
270
271                 if ((size_t)a->ai_addrlen > sizeof(addr))
272                         continue;
273
274                 memcpy(&addr, a->ai_addr, a->ai_addrlen);
275
276                 if (!array_catb(IpAddr, (char *)&addr, sizeof(addr)))
277                         break;
278         }
279
280         freeaddrinfo(ai_results);
281         return a == NULL;
282 #else
283         struct hostent *h = gethostbyname(hostname);
284
285         if (!h) {
286 #ifdef h_errno
287                 Log_Subprocess(LOG_WARNING, "Can't resolve \"%s\": %s",
288                                hostname, Get_Error(h_errno));
289 #else
290                 Log_Subprocess(LOG_WARNING, "Can't resolve \"%s\"", hostname);
291 #endif
292                 return false;
293         }
294         memset(&addr, 0, sizeof(addr));
295
296         addr.sin4.sin_family = AF_INET;
297         memcpy(&addr.sin4.sin_addr, h->h_addr, sizeof(struct in_addr));
298
299         return array_copyb(IpAddr, (char *)&addr, sizeof(addr));
300 #endif /* HAVE_GETADDRINFO */
301 }
302
303
304 static bool
305 Addr_in_list(const array *resolved_addr, const ng_ipaddr_t *Addr)
306 {
307         char tmp_ip_str[NG_INET_ADDRSTRLEN];
308         const ng_ipaddr_t *tmpAddrs = array_start(resolved_addr);
309         size_t len = array_length(resolved_addr, sizeof(*tmpAddrs));
310
311         assert(len > 0);
312         assert(tmpAddrs);
313
314         while (len > 0) {
315                 if (ng_ipaddr_ipequal(Addr, tmpAddrs))
316                         return true;
317                 tmpAddrs++;
318                 len--;
319         }
320         /* failed; print list of addresses */
321         ng_ipaddr_tostr_r(Addr, tmp_ip_str);
322         len = array_length(resolved_addr, sizeof(*tmpAddrs));
323         tmpAddrs = array_start(resolved_addr);
324
325         while (len > 0) {
326                 Log_Subprocess(LOG_WARNING, "Address mismatch: %s != %s",
327                         tmp_ip_str, ng_ipaddr_tostr(tmpAddrs));
328                 tmpAddrs++;
329                 len--;
330         }
331
332         return false;
333 }
334
335
336 static void
337 Log_Forgery_NoIP(const char *ip, const char *host)
338 {
339         Log_Subprocess(LOG_WARNING,
340                 "Possible forgery: %s resolved to \"%s\", which has no IP address!",
341                 ip, host);
342 }
343
344 static void
345 Log_Forgery_WrongIP(const char *ip, const char *host)
346 {
347         Log_Subprocess(LOG_WARNING,
348                 "Possible forgery: %s resolved to \"%s\", which points to a different address!",
349                 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- */