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