98bdc2b0ec0273582b2a50414cc35305dc53ba23
[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 #ifdef WIN32
14 #include <winsock.h>
15 #endif
16
17 #if HAVE_SYS_TYPES_H
18 #include <sys/types.h>
19 #endif
20
21 #if HAVE_NETINET_IN_H
22 #include <netinet/in.h>
23 #endif
24
25 #if HAVE_NETDB_H
26 #include <netdb.h>
27 #endif
28
29 #include <yaz/comstack.h>
30 #include <yaz/statserv.h>
31 #include <yaz/log.h>
32
33
34 #if HAVE_TCPD_H
35 #include <syslog.h>
36 #include <tcpd.h>
37
38 int allow_severity = LOG_INFO;  /* not YLOG !! */
39 int deny_severity = LOG_WARNING;
40
41 #ifdef LOG_DEBUG
42 #undef LOG_DEBUG
43 #endif
44 #ifdef LOG_WARN
45 #undef LOG_WARN
46 #endif
47
48 #endif
49
50 int check_ip_tcpd(void *cd, const char *addr, int len, int type)
51 {
52     const char *daemon_name = (const char *) cd;
53
54     if (type == AF_INET)
55     {
56         if (daemon_name && *daemon_name)
57         {
58 #if HAVE_TCPD_H
59             struct request_info request_info;
60             int i;
61 #endif
62             char *host_name = 0, *host_addr = 0;
63             struct hostent *host;
64
65             struct sockaddr_in *addr_in = (struct sockaddr_in *) addr;
66             
67             if ((host = gethostbyaddr((char*)&addr_in->sin_addr,
68                                       sizeof(addr_in->sin_addr),
69                                       AF_INET)))
70                 host_name = (char*) host->h_name;
71             host_addr = inet_ntoa(addr_in->sin_addr);
72 #if HAVE_TCPD_H
73             if (host_addr)
74                 request_init(&request_info, RQ_DAEMON, daemon_name,
75                              RQ_CLIENT_NAME, host_name,
76                              RQ_CLIENT_SIN, addr_in,
77                              RQ_CLIENT_ADDR, host_addr, 0);
78             else
79                 request_init(&request_info, RQ_DAEMON, daemon_name,
80                              RQ_CLIENT_SIN, addr_in,
81                              RQ_CLIENT_ADDR, host_addr, 0);
82             i = hosts_access(&request_info);
83             if (!i)
84             {
85                 yaz_log (YLOG_DEBUG, "access denied from %s",
86                          host_name ? host_name : host_addr);
87                 return 1;
88             }
89             yaz_log (YLOG_DEBUG, "access granted from %s",
90                      host_name ? host_name : host_addr);
91 #endif
92         }
93     }
94     return 0;
95 }
96
97 /*
98  * Local variables:
99  * c-basic-offset: 4
100  * indent-tabs-mode: nil
101  * End:
102  * vim: shiftwidth=4 tabstop=8 expandtab
103  */
104