Merge branch 'master' of ssh://git.indexdata.com/home/git/pub/yaz
[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_ARPA_INET_H
26 #include <arpa/inet.h>
27 #endif
28
29 #if HAVE_SYS_SOCKET_H
30 /* freebsd wants this for AF_INET */
31 #include <sys/socket.h>
32 #endif
33
34 #if HAVE_NETDB_H
35 #include <netdb.h>
36 #endif
37
38 #include <yaz/comstack.h>
39 #include <yaz/statserv.h>
40 #include <yaz/log.h>
41
42
43 #if HAVE_TCPD_H
44 #include <syslog.h>
45 #include <tcpd.h>
46
47 int allow_severity = LOG_INFO;  /* not YLOG !! */
48 int deny_severity = LOG_WARNING;
49
50 #ifdef LOG_DEBUG
51 #undef LOG_DEBUG
52 #endif
53 #ifdef LOG_WARN
54 #undef LOG_WARN
55 #endif
56
57 #endif
58
59 int check_ip_tcpd(void *cd, const char *addr, int len, int type)
60 {
61     const char *daemon_name = (const char *) cd;
62
63     if (type == AF_INET)
64     {
65         if (daemon_name && *daemon_name)
66         {
67 #if HAVE_TCPD_H
68             struct request_info request_info;
69             int i;
70 #endif
71             char *host_name = 0, *host_addr = 0;
72             struct hostent *host;
73
74             struct sockaddr_in *addr_in = (struct sockaddr_in *) addr;
75             
76             if ((host = gethostbyaddr((char*)&addr_in->sin_addr,
77                                       sizeof(addr_in->sin_addr),
78                                       AF_INET)))
79                 host_name = (char*) host->h_name;
80             host_addr = inet_ntoa(addr_in->sin_addr);
81 #if HAVE_TCPD_H
82             if (host_addr)
83                 request_init(&request_info, RQ_DAEMON, daemon_name,
84                              RQ_CLIENT_NAME, host_name,
85                              RQ_CLIENT_SIN, addr_in,
86                              RQ_CLIENT_ADDR, host_addr, 0);
87             else
88                 request_init(&request_info, RQ_DAEMON, daemon_name,
89                              RQ_CLIENT_SIN, addr_in,
90                              RQ_CLIENT_ADDR, host_addr, 0);
91             i = hosts_access(&request_info);
92             if (!i)
93             {
94                 yaz_log (YLOG_DEBUG, "access denied from %s",
95                          host_name ? host_name : host_addr);
96                 return 1;
97             }
98             yaz_log (YLOG_DEBUG, "access granted from %s",
99                      host_name ? host_name : host_addr);
100 #endif
101         }
102     }
103     return 0;
104 }
105
106 /*
107  * Local variables:
108  * c-basic-offset: 4
109  * indent-tabs-mode: nil
110  * End:
111  * vim: shiftwidth=4 tabstop=8 expandtab
112  */
113