]> git.decadent.org.uk Git - nfs-utils.git/blob - support/misc/tcpwrapper.c
tcp_wrapper: Clean up logit()
[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 access;
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 static void haccess_add(struct sockaddr_in *addr, u_long, int);
77
78 inline unsigned int strtoint(char *str)
79 {
80         unsigned int n = 0;
81         int len = strlen(str);
82         int i;
83
84         for (i=0; i < len; i++)
85                 n+=((int)str[i])*i;
86
87         return n;
88 }
89 static inline int hashint(unsigned int num)
90 {
91         return num % HASH_TABLE_SIZE;
92 }
93 #define HASH(_addr, _prog) \
94         hashint((strtoint((_addr))+(_prog)))
95
96 void haccess_add(struct sockaddr_in *addr, u_long prog, int access)
97 {
98         hash_head *head;
99         haccess_t *hptr;
100         int hash;
101
102         hptr = (haccess_t *)malloc(sizeof(haccess_t));
103         if (hptr == NULL)
104                 return;
105
106         hash = HASH(inet_ntoa(addr->sin_addr), prog);
107         head = &(haccess_tbl[hash]);
108
109         hptr->access = access;
110         hptr->addr.s_addr = addr->sin_addr.s_addr;
111
112         if (TAILQ_EMPTY(&head->h_head))
113                 TAILQ_INSERT_HEAD(&head->h_head, hptr, list);
114         else
115                 TAILQ_INSERT_TAIL(&head->h_head, hptr, list);
116 }
117 haccess_t *haccess_lookup(struct sockaddr_in *addr, u_long prog)
118 {
119         hash_head *head;
120         haccess_t *hptr;
121         int hash;
122
123         hash = HASH(inet_ntoa(addr->sin_addr), prog);
124         head = &(haccess_tbl[hash]);
125
126         TAILQ_FOREACH(hptr, &head->h_head, list) {
127                 if (hptr->addr.s_addr == addr->sin_addr.s_addr)
128                         return hptr;
129         }
130         return NULL;
131 }
132
133 static void
134 logit(const struct sockaddr_in *sin)
135 {
136         char buf[INET_ADDRSTRLEN];
137
138         xlog_warn("connect from %s denied: request from unauthorized host",
139                         inet_ntop(AF_INET, &sin->sin_addr, buf, sizeof(buf)));
140                 
141 }
142
143 int
144 good_client(daemon, addr)
145 char *daemon;
146 struct sockaddr_in *addr;
147 {
148         struct request_info req;
149
150         request_init(&req, RQ_DAEMON, daemon, RQ_CLIENT_SIN, addr, 0);
151         sock_methods(&req);
152
153         if (hosts_access(&req)) 
154                 return ALLOW;
155
156         return DENY;
157 }
158
159 /* check_files - check to see if either access files have changed */
160
161 static int check_files()
162 {
163         static time_t allow_mtime, deny_mtime;
164         struct stat astat, dstat;
165         int changed = 0;
166
167         if (stat("/etc/hosts.allow", &astat) < 0)
168                 astat.st_mtime = 0;
169         if (stat("/etc/hosts.deny", &dstat) < 0)
170                 dstat.st_mtime = 0;
171
172         if(!astat.st_mtime || !dstat.st_mtime)
173                 return changed;
174
175         if (astat.st_mtime != allow_mtime)
176                 changed = 1;
177         else if (dstat.st_mtime != deny_mtime)
178                 changed = 1;
179
180         allow_mtime = astat.st_mtime;
181         deny_mtime = dstat.st_mtime;
182
183         return changed;
184 }
185
186 /**
187  * check_default - additional checks for NULL, DUMP, GETPORT and unknown
188  * @daemon: pointer to '\0'-terminated ASCII string containing name of the
189  *              daemon requesting the access check
190  * @addr: pointer to socket address containing address of caller
191  * @prog: RPC program number caller is attempting to access
192  *
193  * Returns TRUE if the caller is allowed access; otherwise FALSE is returned.
194  */
195 int
196 check_default(char *daemon, struct sockaddr_in *addr, u_long prog)
197 {
198         haccess_t *acc = NULL;
199         int changed = check_files();
200
201         acc = haccess_lookup(addr, prog);
202         if (acc && changed == 0)
203                 return (acc->access);
204
205         if (!(from_local((struct sockaddr *)addr) || good_client(daemon, addr))) {
206                 logit(addr);
207                 if (acc)
208                         acc->access = FALSE;
209                 else 
210                         haccess_add(addr, prog, FALSE);
211                 return (FALSE);
212         }
213
214         if (acc)
215                 acc->access = TRUE;
216         else 
217                 haccess_add(addr, prog, TRUE);
218
219     return (TRUE);
220 }
221
222 #endif  /* HAVE_LIBWRAP */