]> git.decadent.org.uk Git - nfs-utils.git/blob - support/misc/tcpwrapper.c
tcpwrapper: Eliminated shadowed declaration warnings
[nfs-utils.git] / support / misc / tcpwrapper.c
1 /* This is copied from portmap 4.0-29 in RedHat. */
2
3  /*
4   * pmap_check - additional portmap security.
5   * 
6   * Always reject non-local requests to update the portmapper tables.
7   * 
8   * Refuse to forward mount requests to the nfs mount daemon. Otherwise, the
9   * requests would appear to come from the local system, and nfs export
10   * restrictions could be bypassed.
11   * 
12   * Refuse to forward requests to the nfsd process.
13   * 
14   * Refuse to forward requests to NIS (YP) daemons; The only exception is the
15   * YPPROC_DOMAIN_NONACK broadcast rpc call that is used to establish initial
16   * contact with the NIS server.
17   * 
18   * Always allocate an unprivileged port when forwarding a request.
19   * 
20   * If compiled with -DCHECK_PORT, require that requests to register or
21   * unregister a privileged port come from a privileged port. This makes it
22   * more difficult to replace a critical service by a trojan.
23   * 
24   * If compiled with -DHOSTS_ACCESS, reject requests from hosts that are not
25   * authorized by the /etc/hosts.{allow,deny} files. The local system is
26   * always treated as an authorized host. The access control tables are never
27   * consulted for requests from the local system, and are always consulted
28   * for requests from other hosts.
29   * 
30   * Author: Wietse Venema (wietse@wzv.win.tue.nl), dept. of Mathematics and
31   * Computing Science, Eindhoven University of Technology, The Netherlands.
32   */
33
34 #ifdef HAVE_CONFIG_H
35 #include <config.h>
36 #endif
37
38 #ifdef HAVE_LIBWRAP
39 #include <unistd.h>
40 #include <string.h>
41 #include <rpc/rpc.h>
42 #include <rpc/pmap_prot.h>
43 #include <netdb.h>
44 #include <pwd.h>
45 #include <sys/types.h>
46 #include <sys/signal.h>
47 #include <sys/queue.h>
48 #include <sys/stat.h>
49 #include <tcpd.h>
50
51 #include "tcpwrapper.h"
52 #include "xlog.h"
53
54 #ifdef SYSV40
55 #include <netinet/in.h>
56 #include <rpc/rpcent.h>
57 #endif
58
59 static int check_files(void);
60
61 #define ALLOW 1
62 #define DENY 0
63
64 typedef struct _haccess_t {
65         TAILQ_ENTRY(_haccess_t) list;
66         int                     allowed;
67         struct in_addr          addr;
68 } haccess_t;
69
70 #define HASH_TABLE_SIZE 1021
71 typedef struct _hash_head {
72         TAILQ_HEAD(host_list, _haccess_t) h_head;
73 } hash_head;
74 hash_head haccess_tbl[HASH_TABLE_SIZE];
75 static haccess_t *haccess_lookup(struct sockaddr_in *addr, u_long);
76
77 static unsigned long
78 strtoint(const char *str)
79 {
80         unsigned long i, n = 0;
81         size_t len = strlen(str);
82
83         for (i = 0; i < len; i++)
84                 n += (unsigned char)str[i] * i;
85
86         return n;
87 }
88
89 static unsigned int
90 hashint(const unsigned long num)
91 {
92         return (unsigned int)(num % HASH_TABLE_SIZE);
93 }
94
95 static unsigned int
96 HASH(const char *addr, const unsigned long program)
97 {
98         return hashint(strtoint(addr) + program);
99 }
100
101 static void
102 haccess_add(struct sockaddr_in *addr, u_long prog, int allowed)
103 {
104         hash_head *head;
105         haccess_t *hptr;
106         unsigned int hash;
107
108         hptr = (haccess_t *)malloc(sizeof(haccess_t));
109         if (hptr == NULL)
110                 return;
111
112         hash = HASH(inet_ntoa(addr->sin_addr), prog);
113         head = &(haccess_tbl[hash]);
114
115         hptr->allowed = allowed;
116         hptr->addr.s_addr = addr->sin_addr.s_addr;
117
118         if (TAILQ_EMPTY(&head->h_head))
119                 TAILQ_INSERT_HEAD(&head->h_head, hptr, list);
120         else
121                 TAILQ_INSERT_TAIL(&head->h_head, hptr, list);
122 }
123 haccess_t *haccess_lookup(struct sockaddr_in *addr, u_long prog)
124 {
125         hash_head *head;
126         haccess_t *hptr;
127         unsigned int hash;
128
129         hash = HASH(inet_ntoa(addr->sin_addr), prog);
130         head = &(haccess_tbl[hash]);
131
132         TAILQ_FOREACH(hptr, &head->h_head, list) {
133                 if (hptr->addr.s_addr == addr->sin_addr.s_addr)
134                         return hptr;
135         }
136         return NULL;
137 }
138
139 static void
140 logit(const struct sockaddr_in *sin)
141 {
142         char buf[INET_ADDRSTRLEN];
143
144         xlog_warn("connect from %s denied: request from unauthorized host",
145                         inet_ntop(AF_INET, &sin->sin_addr, buf, sizeof(buf)));
146                 
147 }
148
149 static int
150 good_client(char *name, struct sockaddr_in *addr)
151 {
152         struct request_info req;
153
154         request_init(&req, RQ_DAEMON, name, RQ_CLIENT_SIN, addr, 0);
155         sock_methods(&req);
156
157         if (hosts_access(&req)) 
158                 return ALLOW;
159
160         return DENY;
161 }
162
163 /* check_files - check to see if either access files have changed */
164
165 static int check_files()
166 {
167         static time_t allow_mtime, deny_mtime;
168         struct stat astat, dstat;
169         int changed = 0;
170
171         if (stat("/etc/hosts.allow", &astat) < 0)
172                 astat.st_mtime = 0;
173         if (stat("/etc/hosts.deny", &dstat) < 0)
174                 dstat.st_mtime = 0;
175
176         if(!astat.st_mtime || !dstat.st_mtime)
177                 return changed;
178
179         if (astat.st_mtime != allow_mtime)
180                 changed = 1;
181         else if (dstat.st_mtime != deny_mtime)
182                 changed = 1;
183
184         allow_mtime = astat.st_mtime;
185         deny_mtime = dstat.st_mtime;
186
187         return changed;
188 }
189
190 /**
191  * check_default - additional checks for NULL, DUMP, GETPORT and unknown
192  * @name: pointer to '\0'-terminated ASCII string containing name of the
193  *              daemon requesting the access check
194  * @addr: pointer to socket address containing address of caller
195  * @prog: RPC program number caller is attempting to access
196  *
197  * Returns TRUE if the caller is allowed access; otherwise FALSE is returned.
198  */
199 int
200 check_default(char *name, struct sockaddr_in *addr, u_long prog)
201 {
202         haccess_t *acc = NULL;
203         int changed = check_files();
204
205         acc = haccess_lookup(addr, prog);
206         if (acc && changed == 0)
207                 return acc->allowed;
208
209         if (!(from_local((struct sockaddr *)addr) || good_client(name, addr))) {
210                 logit(addr);
211                 if (acc)
212                         acc->allowed = FALSE;
213                 else 
214                         haccess_add(addr, prog, FALSE);
215                 return (FALSE);
216         }
217
218         if (acc)
219                 acc->allowed = TRUE;
220         else 
221                 haccess_add(addr, prog, TRUE);
222
223     return (TRUE);
224 }
225
226 #endif  /* HAVE_LIBWRAP */