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