* Renamed "dynloader" to "serv_extensions" globally. We don't want people
[citadel.git] / citadel / locate_host.c
1 /*
2  * $Id$
3  *
4  * locate the originating host
5  *
6  */
7
8 #ifdef DLL_EXPORT
9 #define IN_LIBCIT
10 #endif
11
12 #include "sysdep.h"
13 #include <stdlib.h>
14 #include <unistd.h>
15 #include <stdio.h>
16 #include <signal.h>
17 #include <sys/types.h>
18 #include <sys/socket.h>
19 #include <netinet/in.h>
20 #include <limits.h>
21 #include <netdb.h>
22 #include <string.h>
23 #include "citadel.h"
24 #include "server.h"
25 #include "serv_extensions.h"
26 #include "locate_host.h"
27 #include "sysdep_decls.h"
28 #include "config.h"
29 #include "tools.h"
30
31 void locate_host(char *tbuf, size_t n, const struct in_addr *addr)
32 {
33         struct hostent *ch;
34         const char *i;
35         char *j;
36         int a1, a2, a3, a4;
37
38         lprintf(9, "locate_host() called\n");
39
40 #ifdef HAVE_NONREENTRANT_NETDB
41         begin_critical_section(S_NETDB);
42 #endif
43
44         if ((ch = gethostbyaddr((const char *) addr, sizeof(*addr), AF_INET)) ==
45             NULL) {
46               bad_dns:
47                 i = (const char *) addr;
48                 a1 = ((*i++) & 0xff);
49                 a2 = ((*i++) & 0xff);
50                 a3 = ((*i++) & 0xff);
51                 a4 = ((*i++) & 0xff);
52                 snprintf(tbuf, n, "%d.%d.%d.%d", a1, a2, a3, a4);
53                 goto end;       /* because we might need to end the critical
54                                    section */
55         }
56         /* check if the forward DNS agrees; if not, they're spoofing */
57         j = strdoop(ch->h_name);
58         ch = gethostbyname(j);
59         phree(j);
60         if (ch == NULL)
61                 goto bad_dns;
62
63         /* check address for consistency */
64         for (; *ch->h_addr_list; ch->h_addr_list++)
65                 if (!memcmp(*ch->h_addr_list, addr,
66                             sizeof *addr)) {
67                         safestrncpy(tbuf, ch->h_name, 63);
68                         goto end;
69                 }
70         goto bad_dns;           /* they were spoofing. report a numeric IP
71                                    address. */
72
73       end:
74
75 #ifdef HAVE_NONREENTRANT_NETDB
76         end_critical_section(S_NETDB);
77 #endif
78
79         tbuf[63] = 0;
80         lprintf(9, "locate_host() exiting\n");
81 }