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