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.
38 #include <tcpwrapper.h>
42 #include <rpc/pmap_prot.h>
46 #include <sys/types.h>
47 #include <sys/signal.h>
48 #include <sys/queue.h>
55 #include <netinet/in.h>
56 #include <rpc/rpcent.h>
59 static void logit(int severity, struct sockaddr_in *addr,
60 u_long procnum, u_long prognum, char *text);
61 static int check_files(void);
63 #define log_bad_host(addr, proc, prog) \
64 logit(LOG_WARNING, addr, proc, prog, "request from unauthorized host")
69 typedef struct _haccess_t {
70 TAILQ_ENTRY(_haccess_t) list;
75 #define HASH_TABLE_SIZE 1021
76 typedef struct _hash_head {
77 TAILQ_HEAD(host_list, _haccess_t) h_head;
79 hash_head haccess_tbl[HASH_TABLE_SIZE];
80 static haccess_t *haccess_lookup(struct sockaddr_in *addr, u_long);
81 static void haccess_add(struct sockaddr_in *addr, u_long, int);
83 inline unsigned int strtoint(char *str)
86 int len = strlen(str);
89 for (i=0; i < len; i++)
94 static inline int hashint(unsigned int num)
96 return num % HASH_TABLE_SIZE;
98 #define HASH(_addr, _prog) \
99 hashint((strtoint((_addr))+(_prog)))
101 void haccess_add(struct sockaddr_in *addr, u_long prog, int access)
107 hptr = (haccess_t *)malloc(sizeof(haccess_t));
111 hash = HASH(inet_ntoa(addr->sin_addr), prog);
112 head = &(haccess_tbl[hash]);
114 hptr->access = access;
115 hptr->addr.s_addr = addr->sin_addr.s_addr;
117 if (TAILQ_EMPTY(&head->h_head))
118 TAILQ_INSERT_HEAD(&head->h_head, hptr, list);
120 TAILQ_INSERT_TAIL(&head->h_head, hptr, list);
122 haccess_t *haccess_lookup(struct sockaddr_in *addr, u_long prog)
128 hash = HASH(inet_ntoa(addr->sin_addr), prog);
129 head = &(haccess_tbl[hash]);
131 TAILQ_FOREACH(hptr, &head->h_head, list) {
132 if (hptr->addr.s_addr == addr->sin_addr.s_addr)
139 good_client(daemon, addr)
141 struct sockaddr_in *addr;
143 struct request_info req;
145 request_init(&req, RQ_DAEMON, daemon, RQ_CLIENT_SIN, addr, 0);
148 if (hosts_access(&req))
154 /* check_files - check to see if either access files have changed */
156 static int check_files()
158 static time_t allow_mtime, deny_mtime;
159 struct stat astat, dstat;
162 if (stat("/etc/hosts.allow", &astat) < 0)
164 if (stat("/etc/hosts.deny", &dstat) < 0)
167 if(!astat.st_mtime || !dstat.st_mtime)
170 if (astat.st_mtime != allow_mtime)
172 else if (dstat.st_mtime != deny_mtime)
175 allow_mtime = astat.st_mtime;
176 deny_mtime = dstat.st_mtime;
181 /* check_default - additional checks for NULL, DUMP, GETPORT and unknown */
184 check_default(daemon, addr, proc, prog)
186 struct sockaddr_in *addr;
190 haccess_t *acc = NULL;
191 int changed = check_files();
193 acc = haccess_lookup(addr, prog);
194 if (acc && changed == 0)
195 return (acc->access);
197 if (!(from_local(addr) || good_client(daemon, addr))) {
198 log_bad_host(addr, proc, prog);
202 haccess_add(addr, prog, FALSE);
209 haccess_add(addr, prog, TRUE);
214 /* logit - report events of interest via the syslog daemon */
216 static void logit(int severity, struct sockaddr_in *addr,
217 u_long procnum, u_long prognum, char *text)
219 syslog(severity, "connect from %s denied: %s",
220 inet_ntoa(addr->sin_addr), text);