Removed away include of system headers in comstack.h. Instead
[yaz-moved-to-github.git] / src / tcpdchk.c
1 /*
2  * Copyright (C) 1995-2005, Index Data ApS
3  * See the file LICENSE for details.
4  *
5  * $Id: tcpdchk.c,v 1.6 2005-01-16 21:51:50 adam Exp $
6  */
7 /**
8  * \file tcpdchk.c
9  * \brief Implements TCP WRAPPER check.
10  */
11
12 #include <stdio.h>
13 #include <string.h>
14
15 #ifdef WIN32
16 #include <winsock.h>
17 #else
18 #include <netdb.h>
19 #include <arpa/inet.h>
20 #endif
21
22 #include <yaz/yconfig.h>
23 #include <yaz/comstack.h>
24 #include <yaz/statserv.h>
25 #include <yaz/log.h>
26
27
28 #if HAVE_TCPD_H
29 #include <syslog.h>
30 #include <tcpd.h>
31
32 int allow_severity = LOG_INFO;  /* not YLOG !! */
33 int deny_severity = LOG_WARNING;
34
35 #ifdef LOG_DEBUG
36 #undef LOG_DEBUG
37 #endif
38 #ifdef LOG_WARN
39 #undef LOG_WARN
40 #endif
41
42 #endif
43
44 int check_ip_tcpd(void *cd, const char *addr, int len, int type)
45 {
46     const char *daemon_name = (const char *) cd;
47
48     if (type == AF_INET)
49     {
50         if (daemon_name && *daemon_name)
51         {
52 #if HAVE_TCPD_H
53             struct request_info request_info;
54             int i;
55 #endif
56             char *host_name = 0, *host_addr = 0;
57             struct hostent *host;
58
59             struct sockaddr_in *addr_in = (struct sockaddr_in *) addr;
60             
61             if ((host = gethostbyaddr((char*)&addr_in->sin_addr,
62                                       sizeof(addr_in->sin_addr),
63                                       AF_INET)))
64                 host_name = (char*) host->h_name;
65             host_addr = inet_ntoa(addr_in->sin_addr);
66 #if HAVE_TCPD_H
67             if (host_addr)
68                 request_init(&request_info, RQ_DAEMON, daemon_name,
69                              RQ_CLIENT_NAME, host_name,
70                              RQ_CLIENT_SIN, addr_in,
71                              RQ_CLIENT_ADDR, host_addr, 0);
72             else
73                 request_init(&request_info, RQ_DAEMON, daemon_name,
74                              RQ_CLIENT_SIN, addr_in,
75                              RQ_CLIENT_ADDR, host_addr, 0);
76             i = hosts_access(&request_info);
77             if (!i)
78             {
79                 yaz_log (YLOG_DEBUG, "access denied from %s",
80                          host_name ? host_name : host_addr);
81                 return 1;
82             }
83             yaz_log (YLOG_DEBUG, "access granted from %s",
84                      host_name ? host_name : host_addr);
85 #endif
86         }
87     }
88     return 0;
89 }
90