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.
37 #include <tcpwrapper.h>
41 #include <rpc/pmap_prot.h>
45 #include <sys/types.h>
46 #include <sys/signal.h>
47 #include <sys/queue.h>
49 #include <netinet/in.h>
50 #include <rpc/rpcent.h>
53 static void logit(int severity, struct sockaddr_in *addr,
54 u_long procnum, u_long prognum, char *text);
55 static void toggle_verboselog(int sig);
57 int allow_severity = LOG_INFO;
58 int deny_severity = LOG_WARNING;
60 /* A handful of macros for "readability". */
63 /* coming from libwrap.a (tcp_wrappers) */
64 extern int hosts_ctl(char *daemon, char *name, char *addr, char *user);
66 int hosts_ctl(char *daemon, char *name, char *addr, char *user)
72 #define legal_port(a,p) \
73 (ntohs((a)->sin_port) < IPPORT_RESERVED || (p) >= IPPORT_RESERVED)
75 #define log_bad_port(addr, proc, prog) \
76 logit(deny_severity, addr, proc, prog, ": request from unprivileged port")
78 #define log_bad_host(addr, proc, prog) \
79 logit(deny_severity, addr, proc, prog, ": request from unauthorized host")
81 #define log_bad_owner(addr, proc, prog) \
82 logit(deny_severity, addr, proc, prog, ": request from non-local host")
84 #define log_no_forward(addr, proc, prog) \
85 logit(deny_severity, addr, proc, prog, ": request not forwarded")
87 #define log_client(addr, proc, prog) \
88 logit(allow_severity, addr, proc, prog, "")
93 typedef struct _haccess_t {
94 TAILQ_ENTRY(_haccess_t) list;
99 #define HASH_TABLE_SIZE 1021
100 typedef struct _hash_head {
101 TAILQ_HEAD(host_list, _haccess_t) h_head;
103 hash_head haccess_tbl[HASH_TABLE_SIZE];
104 static haccess_t *haccess_lookup(struct sockaddr_in *addr, u_long, u_long);
105 static void haccess_add(struct sockaddr_in *addr, u_long, u_long, int);
107 inline unsigned int strtoint(char *str)
110 int len = strlen(str);
113 for (i=0; i < len; i++)
118 inline int hashint(unsigned int num)
120 return num % HASH_TABLE_SIZE;
122 #define HASH(_addr, _proc, _prog) \
123 hashint((strtoint((_addr))+(_proc)+(_prog)))
125 void haccess_add(struct sockaddr_in *addr, u_long proc,
126 u_long prog, int access)
132 hptr = (haccess_t *)malloc(sizeof(haccess_t));
136 hash = HASH(inet_ntoa(addr->sin_addr), proc, prog);
137 head = &(haccess_tbl[hash]);
139 hptr->access = access;
140 hptr->addr.s_addr = addr->sin_addr.s_addr;
142 if (TAILQ_EMPTY(&head->h_head))
143 TAILQ_INSERT_HEAD(&head->h_head, hptr, list);
145 TAILQ_INSERT_TAIL(&head->h_head, hptr, list);
147 haccess_t *haccess_lookup(struct sockaddr_in *addr, u_long proc, u_long prog)
153 hash = HASH(inet_ntoa(addr->sin_addr), proc, prog);
154 head = &(haccess_tbl[hash]);
156 TAILQ_FOREACH(hptr, &head->h_head, list) {
157 if (hptr->addr.s_addr == addr->sin_addr.s_addr)
164 good_client(daemon, addr)
166 struct sockaddr_in *addr;
172 /* First check the address. */
173 if (hosts_ctl(daemon, "", inet_ntoa(addr->sin_addr), "") == DENY)
176 /* Now do the hostname lookup */
177 hp = gethostbyaddr ((const char *) &(addr->sin_addr),
178 sizeof (addr->sin_addr), AF_INET);
180 return DENY; /* never heard of it. misconfigured DNS? */
182 /* Make sure the hostent is authorative. */
183 tmpname = strdup(hp->h_name);
186 hp = gethostbyname(tmpname);
189 return DENY; /* never heard of it. misconfigured DNS? */
191 /* Now make sure the address is on the list */
192 for (sp = hp->h_addr_list ; *sp ; sp++) {
193 if (memcmp(*sp, &(addr->sin_addr), hp->h_length) == 0)
197 return DENY; /* it was a FAKE. */
199 /* Check the official name and address. */
200 if (hosts_ctl(daemon, hp->h_name, inet_ntoa(addr->sin_addr), "") == DENY)
203 /* Now check aliases. */
204 for (sp = hp->h_aliases; *sp ; sp++) {
205 if (hosts_ctl(daemon, *sp, inet_ntoa(addr->sin_addr), "") == DENY)
212 /* check_startup - additional startup code */
214 void check_startup(void)
218 * Give up root privileges so that we can never allocate a privileged
219 * port when forwarding an rpc request.
221 * Fix 8/3/00 Philipp Knirsch: First lookup our rpc user. If we find it,
222 * switch to that uid, otherwise simply resue the old bin user and print
223 * out a warning in syslog.
226 struct passwd *pwent;
228 pwent = getpwnam("rpc");
230 syslog(LOG_WARNING, "user rpc not found, reverting to user bin");
231 if (setuid(1) == -1) {
232 syslog(LOG_ERR, "setuid(1) failed: %m");
237 if (setuid(pwent->pw_uid) == -1) {
238 syslog(LOG_WARNING, "setuid() to rpc user failed: %m");
239 if (setuid(1) == -1) {
240 syslog(LOG_ERR, "setuid(1) failed: %m");
246 (void) signal(SIGINT, toggle_verboselog);
249 /* check_default - additional checks for NULL, DUMP, GETPORT and unknown */
252 check_default(daemon, addr, proc, prog)
254 struct sockaddr_in *addr;
258 haccess_t *acc = NULL;
260 acc = haccess_lookup(addr, proc, prog);
262 return (acc->access);
264 if (!(from_local(addr) || good_client(daemon, addr))) {
265 log_bad_host(addr, proc, prog);
266 haccess_add(addr, proc, prog, FALSE);
270 log_client(addr, proc, prog);
272 haccess_add(addr, proc, prog, TRUE);
276 /* check_privileged_port - additional checks for privileged-port updates */
278 check_privileged_port(struct sockaddr_in *addr,
279 u_long proc, u_long prog, u_long port)
282 if (!legal_port(addr, port)) {
283 log_bad_port(addr, proc, prog);
290 /* toggle_verboselog - toggle verbose logging flag */
292 static void toggle_verboselog(int sig)
294 (void) signal(sig, toggle_verboselog);
295 verboselog = !verboselog;
298 /* logit - report events of interest via the syslog daemon */
300 static void logit(int severity, struct sockaddr_in *addr,
301 u_long procnum, u_long prognum, char *text)
304 char procbuf[16 + 4 * sizeof(u_long)];
306 char progbuf[16 + 4 * sizeof(u_long)];
310 * Fork off a process or the portmap daemon might hang while
311 * getrpcbynumber() or syslog() does its thing.
313 * Don't forget to wait for the children, too...
318 /* Try to map program number to name. */
322 } else if ((rpc = getrpcbynumber((int) prognum))) {
323 progname = rpc->r_name;
325 snprintf(progname = progbuf, sizeof (progbuf),
326 "prog (%lu)", prognum);
329 /* Try to map procedure number to name. */
331 snprintf(procname = procbuf, sizeof (procbuf),
332 "proc (%lu)", (u_long) procnum);
334 /* Write syslog record. */
336 syslog(severity, "connect from %s to %s in %s%s",
337 inet_ntoa(addr->sin_addr), procname, progname, text);