841d897155ee79062bac08a942338342875796f6
[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.5 2005-01-15 19:47:14 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 #include <yaz/yconfig.h>
16 #include <yaz/comstack.h>
17 #include <yaz/statserv.h>
18 #include <yaz/log.h>
19
20
21 #if HAVE_TCPD_H
22 #include <syslog.h>
23 #include <tcpd.h>
24
25
26 int allow_severity = LOG_INFO;  /* not YLOG !! */
27 int deny_severity = LOG_WARNING;
28
29 #ifdef LOG_DEBUG
30 #undef LOG_DEBUG
31 #endif
32 #ifdef LOG_WARN
33 #undef LOG_WARN
34 #endif
35
36 #endif
37
38 int check_ip_tcpd(void *cd, const char *addr, int len, int type)
39 {
40     const char *daemon_name = (const char *) cd;
41
42     if (type == AF_INET)
43     {
44         if (daemon_name && *daemon_name)
45         {
46 #if HAVE_TCPD_H
47             struct request_info request_info;
48             int i;
49 #endif
50             char *host_name = 0, *host_addr = 0;
51             struct hostent *host;
52
53             struct sockaddr_in *addr_in = (struct sockaddr_in *) addr;
54             
55             if ((host = gethostbyaddr((char*)&addr_in->sin_addr,
56                                       sizeof(addr_in->sin_addr),
57                                       AF_INET)))
58                 host_name = (char*) host->h_name;
59             host_addr = inet_ntoa(addr_in->sin_addr);
60 #if HAVE_TCPD_H
61             if (host_addr)
62                 request_init(&request_info, RQ_DAEMON, daemon_name,
63                              RQ_CLIENT_NAME, host_name,
64                              RQ_CLIENT_SIN, addr_in,
65                              RQ_CLIENT_ADDR, host_addr, 0);
66             else
67                 request_init(&request_info, RQ_DAEMON, daemon_name,
68                              RQ_CLIENT_SIN, addr_in,
69                              RQ_CLIENT_ADDR, host_addr, 0);
70             i = hosts_access(&request_info);
71             if (!i)
72             {
73                 yaz_log (YLOG_DEBUG, "access denied from %s",
74                          host_name ? host_name : host_addr);
75                 return 1;
76             }
77             yaz_log (YLOG_DEBUG, "access granted from %s",
78                      host_name ? host_name : host_addr);
79 #endif
80         }
81     }
82     return 0;
83 }
84