1 /* This is copied from portmap 4.0-29 in RedHat. */
4 * pmap_check - additional portmap security.
6 * Always reject non-local requests to update the portmapper tables.
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.
12 * Refuse to forward requests to the nfsd process.
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.
18 * Always allocate an unprivileged port when forwarding a request.
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.
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.
30 * Author: Wietse Venema (wietse@wzv.win.tue.nl), dept. of Mathematics and
31 * Computing Science, Eindhoven University of Technology, The Netherlands.
42 #include <rpc/pmap_prot.h>
45 #include <sys/types.h>
46 #include <sys/signal.h>
47 #include <sys/queue.h>
51 #include "tcpwrapper.h"
55 #include <netinet/in.h>
56 #include <rpc/rpcent.h>
59 static int check_files(void);
64 typedef struct _haccess_t {
65 TAILQ_ENTRY(_haccess_t) list;
70 #define HASH_TABLE_SIZE 1021
71 typedef struct _hash_head {
72 TAILQ_HEAD(host_list, _haccess_t) h_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);
78 inline unsigned int strtoint(char *str)
81 int len = strlen(str);
84 for (i=0; i < len; i++)
89 static inline int hashint(unsigned int num)
91 return num % HASH_TABLE_SIZE;
93 #define HASH(_addr, _prog) \
94 hashint((strtoint((_addr))+(_prog)))
96 void haccess_add(struct sockaddr_in *addr, u_long prog, int access)
102 hptr = (haccess_t *)malloc(sizeof(haccess_t));
106 hash = HASH(inet_ntoa(addr->sin_addr), prog);
107 head = &(haccess_tbl[hash]);
109 hptr->access = access;
110 hptr->addr.s_addr = addr->sin_addr.s_addr;
112 if (TAILQ_EMPTY(&head->h_head))
113 TAILQ_INSERT_HEAD(&head->h_head, hptr, list);
115 TAILQ_INSERT_TAIL(&head->h_head, hptr, list);
117 haccess_t *haccess_lookup(struct sockaddr_in *addr, u_long prog)
123 hash = HASH(inet_ntoa(addr->sin_addr), prog);
124 head = &(haccess_tbl[hash]);
126 TAILQ_FOREACH(hptr, &head->h_head, list) {
127 if (hptr->addr.s_addr == addr->sin_addr.s_addr)
134 logit(const struct sockaddr_in *sin)
136 char buf[INET_ADDRSTRLEN];
138 xlog_warn("connect from %s denied: request from unauthorized host",
139 inet_ntop(AF_INET, &sin->sin_addr, buf, sizeof(buf)));
144 good_client(daemon, addr)
146 struct sockaddr_in *addr;
148 struct request_info req;
150 request_init(&req, RQ_DAEMON, daemon, RQ_CLIENT_SIN, addr, 0);
153 if (hosts_access(&req))
159 /* check_files - check to see if either access files have changed */
161 static int check_files()
163 static time_t allow_mtime, deny_mtime;
164 struct stat astat, dstat;
167 if (stat("/etc/hosts.allow", &astat) < 0)
169 if (stat("/etc/hosts.deny", &dstat) < 0)
172 if(!astat.st_mtime || !dstat.st_mtime)
175 if (astat.st_mtime != allow_mtime)
177 else if (dstat.st_mtime != deny_mtime)
180 allow_mtime = astat.st_mtime;
181 deny_mtime = dstat.st_mtime;
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
193 * Returns TRUE if the caller is allowed access; otherwise FALSE is returned.
196 check_default(char *daemon, struct sockaddr_in *addr, u_long prog)
198 haccess_t *acc = NULL;
199 int changed = check_files();
201 acc = haccess_lookup(addr, prog);
202 if (acc && changed == 0)
203 return (acc->access);
205 if (!(from_local((struct sockaddr *)addr) || good_client(daemon, addr))) {
210 haccess_add(addr, prog, FALSE);
217 haccess_add(addr, prog, TRUE);
222 #endif /* HAVE_LIBWRAP */