Added interface for tcpd wrapper for access control.
[yaz-moved-to-github.git] / server / tcpdchk.c
1 /*
2  * Copyright (c) 1995-1999, Index Data
3  * See the file LICENSE for details.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: tcpdchk.c,v $
7  * Revision 1.1  1999-04-16 14:45:55  adam
8  * Added interface for tcpd wrapper for access control.
9  *
10  */
11
12 #include <stdio.h>
13 #include <string.h>
14
15 #if HAVE_TCPD_H
16 #include <syslog.h>
17 #include <tcpd.h>
18
19 int allow_severity = LOG_INFO;
20 int deny_severity = LOG_WARNING;
21
22 #ifdef LOG_DEBUG
23 #undef LOG_DEBUG
24 #endif
25 #ifdef LOG_WARN
26 #undef LOG_WARN
27 #endif
28
29 #endif
30
31 #ifdef WIN32
32 #include <winsock.h>
33 #else
34 #include <unistd.h>
35 #endif
36
37 #include <comstack.h>
38 #include <statserv.h>
39 #include <log.h>
40
41 int check_ip_tcpd(void *cd, const char *addr, int len, int type)
42 {
43     const char *daemon_name = cd;
44
45     if (type == AF_INET)
46     {
47         if (daemon_name && *daemon_name)
48         {
49 #if HAVE_TCPD_H
50             struct request_info request_info;
51 #endif
52             int i;
53             char *host_name = 0, *host_addr = 0;
54             struct hostent *host;
55
56             struct sockaddr_in *addr_in = (struct sockaddr_in *) addr;
57             
58             if ((host = gethostbyaddr((char*)&addr_in->sin_addr,
59                                       sizeof(addr_in->sin_addr),
60                                       AF_INET)))
61                 host_name = (char*) host->h_name;
62             host_addr = inet_ntoa(addr_in->sin_addr);
63 #if HAVE_TCPD_H
64             if (host_addr)
65                 request_init(&request_info, RQ_DAEMON, daemon_name,
66                              RQ_CLIENT_NAME, host_name,
67                              RQ_CLIENT_SIN, addr_in,
68                              RQ_CLIENT_ADDR, host_addr, 0);
69             else
70                 request_init(&request_info, RQ_DAEMON, daemon_name,
71                              RQ_CLIENT_SIN, addr_in,
72                              RQ_CLIENT_ADDR, host_addr, 0);
73             i = hosts_access(&request_info);
74             if (!i)
75             {
76                 logf (LOG_DEBUG, "access denied from %s",
77                       host_name ? host_name : host_addr);
78                 return 1;
79             }
80             logf (LOG_DEBUG, "access granted from %s",
81                   host_name ? host_name : host_addr);
82 #endif
83         }
84     }
85     return 0;
86 }
87