Fix Libxml2 error handling under Windows.
[yaz-moved-to-github.git] / src / tcpdchk.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2008 Index Data
3  * See the file LICENSE for details.
4  */
5 /**
6  * \file tcpdchk.c
7  * \brief Implements TCP WRAPPER check.
8  */
9
10 #include <stdio.h>
11 #include <string.h>
12
13 #if HAVE_SYS_TYPES_H
14 #include <sys/types.h>
15 #endif
16
17 #ifdef WIN32
18 #include <winsock.h>
19 #else
20 #include <netinet/in.h>
21 #include <netdb.h>
22 #include <arpa/inet.h>
23 #include <netinet/tcp.h>
24 #endif
25
26 #if HAVE_SYS_SOCKET_H
27 #include <sys/socket.h>
28 #endif
29
30 #include <yaz/yconfig.h>
31 #include <yaz/comstack.h>
32 #include <yaz/statserv.h>
33 #include <yaz/log.h>
34
35
36 #if HAVE_TCPD_H
37 #include <syslog.h>
38 #include <tcpd.h>
39
40 int allow_severity = LOG_INFO;  /* not YLOG !! */
41 int deny_severity = LOG_WARNING;
42
43 #ifdef LOG_DEBUG
44 #undef LOG_DEBUG
45 #endif
46 #ifdef LOG_WARN
47 #undef LOG_WARN
48 #endif
49
50 #endif
51
52 int check_ip_tcpd(void *cd, const char *addr, int len, int type)
53 {
54     const char *daemon_name = (const char *) cd;
55
56     if (type == AF_INET)
57     {
58         if (daemon_name && *daemon_name)
59         {
60 #if HAVE_TCPD_H
61             struct request_info request_info;
62             int i;
63 #endif
64             char *host_name = 0, *host_addr = 0;
65             struct hostent *host;
66
67             struct sockaddr_in *addr_in = (struct sockaddr_in *) addr;
68             
69             if ((host = gethostbyaddr((char*)&addr_in->sin_addr,
70                                       sizeof(addr_in->sin_addr),
71                                       AF_INET)))
72                 host_name = (char*) host->h_name;
73             host_addr = inet_ntoa(addr_in->sin_addr);
74 #if HAVE_TCPD_H
75             if (host_addr)
76                 request_init(&request_info, RQ_DAEMON, daemon_name,
77                              RQ_CLIENT_NAME, host_name,
78                              RQ_CLIENT_SIN, addr_in,
79                              RQ_CLIENT_ADDR, host_addr, 0);
80             else
81                 request_init(&request_info, RQ_DAEMON, daemon_name,
82                              RQ_CLIENT_SIN, addr_in,
83                              RQ_CLIENT_ADDR, host_addr, 0);
84             i = hosts_access(&request_info);
85             if (!i)
86             {
87                 yaz_log (YLOG_DEBUG, "access denied from %s",
88                          host_name ? host_name : host_addr);
89                 return 1;
90             }
91             yaz_log (YLOG_DEBUG, "access granted from %s",
92                      host_name ? host_name : host_addr);
93 #endif
94         }
95     }
96     return 0;
97 }
98
99 /*
100  * Local variables:
101  * c-basic-offset: 4
102  * indent-tabs-mode: nil
103  * End:
104  * vim: shiftwidth=4 tabstop=8 expandtab
105  */
106