Simplify in a lot of places using odr_strdupn
[yaz-moved-to-github.git] / src / tcpdchk.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2013 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 #endif
55
56 int check_ip_tcpd(void *cd, const char *addr, int len, int type)
57 {
58     const char *daemon_name = (const char *) cd;
59
60     if (type == AF_INET)
61     {
62         if (daemon_name && *daemon_name)
63         {
64 #if HAVE_TCPD_H
65             struct request_info request_info;
66             int i;
67             char *host_name = 0, *host_addr = 0;
68             struct hostent *host;
69
70             struct sockaddr_in *addr_in = (struct sockaddr_in *) addr;
71
72             if ((host = gethostbyaddr((char*)&addr_in->sin_addr,
73                                       sizeof(addr_in->sin_addr),
74                                       AF_INET)))
75                 host_name = (char*) host->h_name;
76             host_addr = inet_ntoa(addr_in->sin_addr);
77             if (host_addr && host_name)
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, 0);
85             i = hosts_access(&request_info);
86             if (!i)
87             {
88                 yaz_log(YLOG_DEBUG, "access denied from %s",
89                         host_name ? host_name : host_addr);
90                 return 1;
91             }
92             yaz_log(YLOG_DEBUG, "access granted from %s",
93                     host_name ? host_name : host_addr);
94 #endif
95         }
96     }
97     return 0;
98 }
99
100 /*
101  * Local variables:
102  * c-basic-offset: 4
103  * c-file-style: "Stroustrup"
104  * indent-tabs-mode: nil
105  * End:
106  * vim: shiftwidth=4 tabstop=8 expandtab
107  */
108