Fix sample PQF
[yaz-moved-to-github.git] / server / tcpdchk.c
1 /*
2  * Copyright (c) 1995-2000, Index Data
3  * See the file LICENSE for details.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: tcpdchk.c,v $
7  * Revision 1.5  2000-02-28 11:20:06  adam
8  * Using autoconf. New definitions: YAZ_BEGIN_CDECL/YAZ_END_CDECL.
9  *
10  * Revision 1.4  1999/11/30 13:47:12  adam
11  * Improved installation. Moved header files to include/yaz.
12  *
13  * Revision 1.3  1999/08/27 09:40:32  adam
14  * Renamed logf function to yaz_log. Removed VC++ project files.
15  *
16  * Revision 1.2  1999/04/20 09:56:48  adam
17  * Added 'name' paramter to encoder/decoder routines (typedef Odr_fun).
18  * Modified all encoders/decoders to reflect this change.
19  *
20  * Revision 1.1  1999/04/16 14:45:55  adam
21  * Added interface for tcpd wrapper for access control.
22  *
23  */
24
25 #include <stdio.h>
26 #include <string.h>
27
28 #include <yaz/yconfig.h>
29 #include <yaz/comstack.h>
30
31 #if HAVE_TCPD_H
32 #include <syslog.h>
33 #include <tcpd.h>
34
35
36 int allow_severity = LOG_INFO;
37 int deny_severity = LOG_WARNING;
38
39 #ifdef LOG_DEBUG
40 #undef LOG_DEBUG
41 #endif
42 #ifdef LOG_WARN
43 #undef LOG_WARN
44 #endif
45
46 #endif
47
48 #include <yaz/statserv.h>
49 #include <yaz/log.h>
50
51 int check_ip_tcpd(void *cd, const char *addr, int len, int type)
52 {
53     const char *daemon_name = (const char *) cd;
54
55     if (type == AF_INET)
56     {
57         if (daemon_name && *daemon_name)
58         {
59 #if HAVE_TCPD_H
60             struct request_info request_info;
61             int i;
62 #endif
63             char *host_name = 0, *host_addr = 0;
64             struct hostent *host;
65
66             struct sockaddr_in *addr_in = (struct sockaddr_in *) addr;
67             
68             if ((host = gethostbyaddr((char*)&addr_in->sin_addr,
69                                       sizeof(addr_in->sin_addr),
70                                       AF_INET)))
71                 host_name = (char*) host->h_name;
72             host_addr = inet_ntoa(addr_in->sin_addr);
73 #if HAVE_TCPD_H
74             if (host_addr)
75                 request_init(&request_info, RQ_DAEMON, daemon_name,
76                              RQ_CLIENT_NAME, host_name,
77                              RQ_CLIENT_SIN, addr_in,
78                              RQ_CLIENT_ADDR, host_addr, 0);
79             else
80                 request_init(&request_info, RQ_DAEMON, daemon_name,
81                              RQ_CLIENT_SIN, addr_in,
82                              RQ_CLIENT_ADDR, host_addr, 0);
83             i = hosts_access(&request_info);
84             if (!i)
85             {
86                 yaz_log (LOG_DEBUG, "access denied from %s",
87                          host_name ? host_name : host_addr);
88                 return 1;
89             }
90             yaz_log (LOG_DEBUG, "access granted from %s",
91                      host_name ? host_name : host_addr);
92 #endif
93         }
94     }
95     return 0;
96 }
97