e0a793d9fbd0d5c2e51626d88b1222ac0b3d4790
[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.2  1999-04-20 09:56:48  adam
8  * Added 'name' paramter to encoder/decoder routines (typedef Odr_fun).
9  * Modified all encoders/decoders to reflect this change.
10  *
11  * Revision 1.1  1999/04/16 14:45:55  adam
12  * Added interface for tcpd wrapper for access control.
13  *
14  */
15
16 #include <stdio.h>
17 #include <string.h>
18
19 #if HAVE_TCPD_H
20 #include <syslog.h>
21 #include <tcpd.h>
22
23 int allow_severity = LOG_INFO;
24 int deny_severity = LOG_WARNING;
25
26 #ifdef LOG_DEBUG
27 #undef LOG_DEBUG
28 #endif
29 #ifdef LOG_WARN
30 #undef LOG_WARN
31 #endif
32
33 #endif
34
35 #ifdef WIN32
36 #include <winsock.h>
37 #else
38 #include <unistd.h>
39 #endif
40
41 #include <comstack.h>
42 #include <statserv.h>
43 #include <log.h>
44
45 int check_ip_tcpd(void *cd, const char *addr, int len, int type)
46 {
47     const char *daemon_name = (const char *) cd;
48
49     if (type == AF_INET)
50     {
51         if (daemon_name && *daemon_name)
52         {
53 #if HAVE_TCPD_H
54             struct request_info request_info;
55             int i;
56 #endif
57             char *host_name = 0, *host_addr = 0;
58             struct hostent *host;
59
60             struct sockaddr_in *addr_in = (struct sockaddr_in *) addr;
61             
62             if ((host = gethostbyaddr((char*)&addr_in->sin_addr,
63                                       sizeof(addr_in->sin_addr),
64                                       AF_INET)))
65                 host_name = (char*) host->h_name;
66             host_addr = inet_ntoa(addr_in->sin_addr);
67 #if HAVE_TCPD_H
68             if (host_addr)
69                 request_init(&request_info, RQ_DAEMON, daemon_name,
70                              RQ_CLIENT_NAME, host_name,
71                              RQ_CLIENT_SIN, addr_in,
72                              RQ_CLIENT_ADDR, host_addr, 0);
73             else
74                 request_init(&request_info, RQ_DAEMON, daemon_name,
75                              RQ_CLIENT_SIN, addr_in,
76                              RQ_CLIENT_ADDR, host_addr, 0);
77             i = hosts_access(&request_info);
78             if (!i)
79             {
80                 logf (LOG_DEBUG, "access denied from %s",
81                       host_name ? host_name : host_addr);
82                 return 1;
83             }
84             logf (LOG_DEBUG, "access granted from %s",
85                   host_name ? host_name : host_addr);
86 #endif
87         }
88     }
89     return 0;
90 }
91