xref: /netbsd-src/usr.sbin/rpcbind/security.c (revision f525b84e77cf010301c631222b01ed509b311a6a)
1*f525b84eSchristos /*	$NetBSD: security.c,v 1.14 2021/03/07 00:23:06 christos Exp $	*/
234b7ffd9Schristos /*	$FreeBSD: head/usr.sbin/rpcbind/security.c 262860 2014-03-06 17:33:27Z mav $ */
3d687de29Sfvdl 
4d687de29Sfvdl #include <sys/types.h>
5d687de29Sfvdl #include <sys/time.h>
6d687de29Sfvdl #include <sys/socket.h>
7d687de29Sfvdl #include <netinet/in.h>
8d687de29Sfvdl #include <arpa/inet.h>
9d687de29Sfvdl #include <rpc/rpc.h>
10d687de29Sfvdl #include <rpc/rpcb_prot.h>
11d687de29Sfvdl #include <rpc/pmap_prot.h>
12d687de29Sfvdl #include <err.h>
1334b7ffd9Schristos #include <stdio.h>
14d687de29Sfvdl #include <stdlib.h>
15b6219f38Sthorpej #include <string.h>
16d687de29Sfvdl #include <unistd.h>
17d687de29Sfvdl #include <util.h>
18d687de29Sfvdl #include <syslog.h>
19d687de29Sfvdl #include <netdb.h>
20d687de29Sfvdl 
21d687de29Sfvdl /*
22d687de29Sfvdl  * XXX for special case checks in check_callit.
23d687de29Sfvdl  */
24d687de29Sfvdl #include <rpcsvc/mount.h>
25d687de29Sfvdl #include <rpcsvc/rquota.h>
26d687de29Sfvdl #include <rpcsvc/nfs_prot.h>
274a483217Sroy 
284a483217Sroy #ifdef YP
29d687de29Sfvdl #include <rpcsvc/yp.h>
30d687de29Sfvdl #include <rpcsvc/ypclnt.h>
31d687de29Sfvdl #include <rpcsvc/yppasswd.h>
324a483217Sroy #else
334a483217Sroy /* Define enough to compile. */
344a483217Sroy #define	YPBINDPROG		((unsigned long)100007)
354a483217Sroy #define	YPBINDPROC_SETDOM	((unsigned long)2)
364a483217Sroy #define	YPPROG			((unsigned long)100004)
374a483217Sroy #define	YPPROC_FIRST		((unsigned long)4)
384a483217Sroy #define	YPPROC_NEXT		((unsigned long)5)
394a483217Sroy #define	YPPROC_MATCH		((unsigned long)3)
404a483217Sroy #define	YPPROC_ALL		((unsigned long)8)
414a483217Sroy #define	YPPASSWDPROG		((unsigned long)100009)
424a483217Sroy #endif
43d687de29Sfvdl 
44d687de29Sfvdl #include "rpcbind.h"
45d687de29Sfvdl 
46d687de29Sfvdl #ifdef LIBWRAP
47d687de29Sfvdl # include <tcpd.h>
48d687de29Sfvdl #ifndef LIBWRAP_ALLOW_FACILITY
49d687de29Sfvdl # define LIBWRAP_ALLOW_FACILITY LOG_AUTH
50d687de29Sfvdl #endif
51d687de29Sfvdl #ifndef LIBWRAP_ALLOW_SEVERITY
52d687de29Sfvdl # define LIBWRAP_ALLOW_SEVERITY LOG_INFO
53d687de29Sfvdl #endif
54d687de29Sfvdl #ifndef LIBWRAP_DENY_FACILITY
55d687de29Sfvdl # define LIBWRAP_DENY_FACILITY LOG_AUTH
56d687de29Sfvdl #endif
57d687de29Sfvdl #ifndef LIBWRAP_DENY_SEVERITY
58d687de29Sfvdl # define LIBWRAP_DENY_SEVERITY LOG_WARNING
59d687de29Sfvdl #endif
60d687de29Sfvdl int allow_severity = LIBWRAP_ALLOW_FACILITY|LIBWRAP_ALLOW_SEVERITY;
61d687de29Sfvdl int deny_severity = LIBWRAP_DENY_FACILITY|LIBWRAP_DENY_SEVERITY;
62d687de29Sfvdl #endif
63d687de29Sfvdl 
64d687de29Sfvdl #ifndef PORTMAP_LOG_FACILITY
65d687de29Sfvdl # define PORTMAP_LOG_FACILITY LOG_AUTH
66d687de29Sfvdl #endif
67d687de29Sfvdl #ifndef PORTMAP_LOG_SEVERITY
68d687de29Sfvdl # define PORTMAP_LOG_SEVERITY LOG_INFO
69d687de29Sfvdl #endif
70d687de29Sfvdl int log_severity = PORTMAP_LOG_FACILITY|PORTMAP_LOG_SEVERITY;
71d687de29Sfvdl 
72d687de29Sfvdl extern int verboselog;
73d687de29Sfvdl 
74d687de29Sfvdl int
check_access(SVCXPRT * xprt,rpcproc_t proc,void * args,unsigned int rpcbvers)7534b7ffd9Schristos check_access(SVCXPRT *xprt, rpcproc_t proc, void *args, unsigned int rpcbvers)
76d687de29Sfvdl {
77d687de29Sfvdl 	struct netbuf *caller = svc_getrpccaller(xprt);
78d687de29Sfvdl 	struct sockaddr *addr = (struct sockaddr *)caller->buf;
79d687de29Sfvdl #ifdef LIBWRAP
80d687de29Sfvdl 	struct request_info req;
81d687de29Sfvdl #endif
82d687de29Sfvdl 	rpcprog_t prog = 0;
83d687de29Sfvdl 	rpcb *rpcbp;
84d687de29Sfvdl 	struct pmap *pmap;
85d687de29Sfvdl 
86d687de29Sfvdl 	/*
87d687de29Sfvdl 	 * The older PMAP_* equivalents have the same numbers, so
88d687de29Sfvdl 	 * they are accounted for here as well.
89d687de29Sfvdl 	 */
90d687de29Sfvdl 	switch (proc) {
91d687de29Sfvdl 	case RPCBPROC_GETADDR:
92d687de29Sfvdl 	case RPCBPROC_SET:
93d687de29Sfvdl 	case RPCBPROC_UNSET:
94d687de29Sfvdl 		if (rpcbvers > PMAPVERS) {
95d687de29Sfvdl 			rpcbp = (rpcb *)args;
96d687de29Sfvdl 			prog = rpcbp->r_prog;
97d687de29Sfvdl 		} else {
98d687de29Sfvdl 			pmap = (struct pmap *)args;
99d687de29Sfvdl 			prog = pmap->pm_prog;
100d687de29Sfvdl 		}
101d687de29Sfvdl 		if (proc == RPCBPROC_GETADDR)
102d687de29Sfvdl 			break;
103d687de29Sfvdl 		if (!insecure && !is_loopback(caller)) {
104d687de29Sfvdl 			if (verboselog)
105d687de29Sfvdl 				logit(log_severity, addr, proc, prog,
106d687de29Sfvdl 				    " declined (non-loopback sender)");
107d687de29Sfvdl 			return 0;
108d687de29Sfvdl 		}
109d687de29Sfvdl 		break;
110d687de29Sfvdl 	case RPCBPROC_CALLIT:
111d687de29Sfvdl 	case RPCBPROC_INDIRECT:
112d687de29Sfvdl 	case RPCBPROC_DUMP:
113d687de29Sfvdl 	case RPCBPROC_GETTIME:
114d687de29Sfvdl 	case RPCBPROC_UADDR2TADDR:
115d687de29Sfvdl 	case RPCBPROC_TADDR2UADDR:
116d687de29Sfvdl 	case RPCBPROC_GETVERSADDR:
117d687de29Sfvdl 	case RPCBPROC_GETADDRLIST:
118d687de29Sfvdl 	case RPCBPROC_GETSTAT:
119d687de29Sfvdl 	default:
120fdfcf196Scgd 		break;
121d687de29Sfvdl 	}
122d687de29Sfvdl 
123d687de29Sfvdl #ifdef LIBWRAP
12434b7ffd9Schristos 	if (libwrap && addr->sa_family != AF_LOCAL) {
12534b7ffd9Schristos 		request_init(&req, RQ_DAEMON, "rpcbind", RQ_CLIENT_SIN, addr,
126*f525b84eSchristos 		    RQ_FILE, xprt->xp_fd, NULL);
127d687de29Sfvdl 		sock_methods(&req);
128d687de29Sfvdl 		if(!hosts_access(&req)) {
12934b7ffd9Schristos 			logit(deny_severity, addr, proc, prog,
13034b7ffd9Schristos 			    ": request from unauthorized host");
131d687de29Sfvdl 			return 0;
132d687de29Sfvdl 		}
13334b7ffd9Schristos 	}
134d687de29Sfvdl #endif
135d687de29Sfvdl 	if (verboselog)
136d687de29Sfvdl 		logit(log_severity, addr, proc, prog, "");
137d687de29Sfvdl     	return 1;
138d687de29Sfvdl }
139d687de29Sfvdl 
140d687de29Sfvdl int
is_loopback(struct netbuf * nbuf)141d687de29Sfvdl is_loopback(struct netbuf *nbuf)
142d687de29Sfvdl {
143d687de29Sfvdl 	struct sockaddr *addr = (struct sockaddr *)nbuf->buf;
144d687de29Sfvdl 	struct sockaddr_in *sin;
145d687de29Sfvdl #ifdef INET6
146d687de29Sfvdl 	struct sockaddr_in6 *sin6;
147d687de29Sfvdl #endif
148d687de29Sfvdl 
149d687de29Sfvdl 	switch (addr->sa_family) {
150d687de29Sfvdl 	case AF_INET:
151d687de29Sfvdl 		if (!oldstyle_local)
152d687de29Sfvdl 			return 0;
153d687de29Sfvdl 		sin = (struct sockaddr_in *)addr;
154d687de29Sfvdl         	return ((sin->sin_addr.s_addr == htonl(INADDR_LOOPBACK)) &&
1554810a01fSfvdl 		    (ntohs(sin->sin_port) < IPPORT_RESERVED));
156d687de29Sfvdl #ifdef INET6
157d687de29Sfvdl 	case AF_INET6:
158d687de29Sfvdl 		if (!oldstyle_local)
159d687de29Sfvdl 			return 0;
160d687de29Sfvdl 		sin6 = (struct sockaddr_in6 *)addr;
161d687de29Sfvdl 		return (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr) &&
1624810a01fSfvdl 		    (ntohs(sin6->sin6_port) < IPV6PORT_RESERVED));
163d687de29Sfvdl #endif
164d687de29Sfvdl 	case AF_LOCAL:
165d687de29Sfvdl 		return 1;
166d687de29Sfvdl 	default:
167fdfcf196Scgd 		break;
168d687de29Sfvdl 	}
169d687de29Sfvdl 
170d687de29Sfvdl 	return 0;
171d687de29Sfvdl }
172d687de29Sfvdl 
173d687de29Sfvdl 
174d687de29Sfvdl /* logit - report events of interest via the syslog daemon */
175d687de29Sfvdl void
logit(int severity,struct sockaddr * addr,rpcproc_t procnum,rpcprog_t prognum,const char * text)176d687de29Sfvdl logit(int severity, struct sockaddr *addr, rpcproc_t procnum, rpcprog_t prognum,
177d687de29Sfvdl       const char *text)
178d687de29Sfvdl {
179d687de29Sfvdl 	const char *procname;
180d687de29Sfvdl 	char	procbuf[32];
181d687de29Sfvdl 	char   *progname;
182d687de29Sfvdl 	char	progbuf[32];
183d687de29Sfvdl 	char fromname[NI_MAXHOST];
184d687de29Sfvdl 	struct rpcent *rpc;
185d687de29Sfvdl 	static const char *procmap[] = {
186d687de29Sfvdl 	/* RPCBPROC_NULL */		"null",
187d687de29Sfvdl 	/* RPCBPROC_SET */		"set",
188d687de29Sfvdl 	/* RPCBPROC_UNSET */		"unset",
189d687de29Sfvdl 	/* RPCBPROC_GETADDR */		"getport/addr",
190d687de29Sfvdl 	/* RPCBPROC_DUMP */		"dump",
191d687de29Sfvdl 	/* RPCBPROC_CALLIT */		"callit",
192d687de29Sfvdl 	/* RPCBPROC_GETTIME */		"gettime",
193d687de29Sfvdl 	/* RPCBPROC_UADDR2TADDR */	"uaddr2taddr",
194d687de29Sfvdl 	/* RPCBPROC_TADDR2UADDR */	"taddr2uaddr",
195d687de29Sfvdl 	/* RPCBPROC_GETVERSADDR */	"getversaddr",
196d687de29Sfvdl 	/* RPCBPROC_INDIRECT */		"indirect",
197d687de29Sfvdl 	/* RPCBPROC_GETADDRLIST */	"getaddrlist",
198d687de29Sfvdl 	/* RPCBPROC_GETSTAT */		"getstat"
199d687de29Sfvdl 	};
200d687de29Sfvdl 
201d687de29Sfvdl 	/*
202d687de29Sfvdl 	 * Fork off a process or the portmap daemon might hang while
203d687de29Sfvdl 	 * getrpcbynumber() or syslog() does its thing.
204d687de29Sfvdl 	 */
205d687de29Sfvdl 
206d687de29Sfvdl 	if (fork() == 0) {
207d687de29Sfvdl 		setproctitle("logit");
208d687de29Sfvdl 
209d687de29Sfvdl 		/* Try to map program number to name. */
210d687de29Sfvdl 
211d687de29Sfvdl 		if (prognum == 0) {
212de327a01Schristos 			progname = __UNCONST("");
213d687de29Sfvdl 		} else if ((rpc = getrpcbynumber((int) prognum))) {
214d687de29Sfvdl 			progname = rpc->r_name;
215d687de29Sfvdl 		} else {
216d687de29Sfvdl 			snprintf(progname = progbuf, sizeof(progbuf), "%u",
217d687de29Sfvdl 			    (unsigned)prognum);
218d687de29Sfvdl 		}
219d687de29Sfvdl 
220d687de29Sfvdl 		/* Try to map procedure number to name. */
221d687de29Sfvdl 
222fb41d909Selad 		if (procnum >= (sizeof procmap / sizeof (char *))) {
223d687de29Sfvdl 			snprintf(procbuf, sizeof procbuf, "%u",
224d687de29Sfvdl 			    (unsigned)procnum);
225d687de29Sfvdl 			procname = procbuf;
226d687de29Sfvdl 		} else
227d687de29Sfvdl 			procname = procmap[procnum];
228d687de29Sfvdl 
229d687de29Sfvdl 		/* Write syslog record. */
230d687de29Sfvdl 
231d687de29Sfvdl 		if (addr->sa_family == AF_LOCAL)
232d9f2774cSitojun 			strlcpy(fromname, "local", sizeof(fromname));
233d687de29Sfvdl 		else
234d687de29Sfvdl 			getnameinfo(addr, addr->sa_len, fromname,
235d687de29Sfvdl 			    sizeof fromname, NULL, 0, NI_NUMERICHOST);
236d687de29Sfvdl 
237d687de29Sfvdl 		syslog(severity, "connect from %s to %s(%s)%s",
238d687de29Sfvdl 			fromname, procname, progname, text);
239d687de29Sfvdl 		_exit(0);
240d687de29Sfvdl 	}
241d687de29Sfvdl }
242d687de29Sfvdl 
243d687de29Sfvdl int
check_callit(SVCXPRT * xprt,struct r_rmtcall_args * args,int versnum __unused)24434b7ffd9Schristos check_callit(SVCXPRT *xprt, struct r_rmtcall_args *args, int versnum __unused)
245d687de29Sfvdl {
246d687de29Sfvdl 	struct sockaddr *sa = (struct sockaddr *)svc_getrpccaller(xprt)->buf;
247d687de29Sfvdl 
248d687de29Sfvdl 	/*
249d687de29Sfvdl 	 * Always allow calling NULLPROC
250d687de29Sfvdl 	 */
251d687de29Sfvdl 	if (args->rmt_proc == 0)
252d687de29Sfvdl 		return 1;
253d687de29Sfvdl 
254d687de29Sfvdl 	/*
255d687de29Sfvdl 	 * XXX - this special casing sucks.
256d687de29Sfvdl 	 */
257d687de29Sfvdl 	switch (args->rmt_prog) {
258d687de29Sfvdl 	case RPCBPROG:
259d687de29Sfvdl 		/*
260d687de29Sfvdl 		 * Allow indirect calls to ourselves in insecure mode.
261d687de29Sfvdl 		 * The is_loopback checks aren't useful then anyway.
262d687de29Sfvdl 		 */
263d687de29Sfvdl 		if (!insecure)
264d687de29Sfvdl 			goto deny;
265d687de29Sfvdl 		break;
266d687de29Sfvdl 	case MOUNTPROG:
267d687de29Sfvdl 		if (args->rmt_proc != MOUNTPROC_MNT &&
268d687de29Sfvdl 		    args->rmt_proc != MOUNTPROC_UMNT)
269d687de29Sfvdl 			break;
270d687de29Sfvdl 		goto deny;
271d687de29Sfvdl 	case YPBINDPROG:
272d687de29Sfvdl 		if (args->rmt_proc != YPBINDPROC_SETDOM)
273d687de29Sfvdl 			break;
274d687de29Sfvdl 		/* FALLTHROUGH */
275d687de29Sfvdl 	case YPPASSWDPROG:
276d687de29Sfvdl 	case NFS_PROGRAM:
277d687de29Sfvdl 	case RQUOTAPROG:
278d687de29Sfvdl 		goto deny;
279d687de29Sfvdl 	case YPPROG:
280d687de29Sfvdl 		switch (args->rmt_proc) {
281d687de29Sfvdl 		case YPPROC_ALL:
282d687de29Sfvdl 		case YPPROC_MATCH:
283d687de29Sfvdl 		case YPPROC_FIRST:
284d687de29Sfvdl 		case YPPROC_NEXT:
285d687de29Sfvdl 			goto deny;
286d687de29Sfvdl 		default:
287fdfcf196Scgd 			break;
288d687de29Sfvdl 		}
289d687de29Sfvdl 	default:
290fdfcf196Scgd 		break;
291d687de29Sfvdl 	}
292d687de29Sfvdl 
293d687de29Sfvdl 	return 1;
294d687de29Sfvdl deny:
29534b7ffd9Schristos #ifdef LIBWRAP
296d687de29Sfvdl 	logit(deny_severity, sa, args->rmt_proc, args->rmt_prog,
297d687de29Sfvdl 	    ": indirect call not allowed");
29834b7ffd9Schristos #else
29934b7ffd9Schristos 	logit(0, sa, args->rmt_proc, args->rmt_prog,
30034b7ffd9Schristos 	    ": indirect call not allowed");
30134b7ffd9Schristos #endif
302d687de29Sfvdl 	return 0;
303d687de29Sfvdl }
304