1 /* $NetBSD: security.c,v 1.5 2000/06/08 09:01:05 fvdl Exp $ */
2 /* $FreeBSD: src/usr.sbin/rpcbind/security.c,v 1.6 2002/12/16 22:24:26 mbr Exp $ */
6 #include <sys/socket.h>
7 #include <netinet/in.h>
10 #include <rpc/rpcb_prot.h>
11 #include <rpc/pmap_prot.h>
22 * XXX for special case checks in check_callit.
24 #include <rpcsvc/mount.h>
25 #include <rpcsvc/rquota.h>
26 #include <rpcsvc/nfs_prot.h>
27 #include <rpcsvc/yp.h>
28 #include <rpcsvc/ypclnt.h>
29 #include <rpcsvc/yppasswd.h>
35 #ifndef LIBWRAP_ALLOW_FACILITY
36 # define LIBWRAP_ALLOW_FACILITY LOG_AUTH
38 #ifndef LIBWRAP_ALLOW_SEVERITY
39 # define LIBWRAP_ALLOW_SEVERITY LOG_INFO
41 #ifndef LIBWRAP_DENY_FACILITY
42 # define LIBWRAP_DENY_FACILITY LOG_AUTH
44 #ifndef LIBWRAP_DENY_SEVERITY
45 # define LIBWRAP_DENY_SEVERITY LOG_WARNING
47 int allow_severity = LIBWRAP_ALLOW_FACILITY|LIBWRAP_ALLOW_SEVERITY;
48 int deny_severity = LIBWRAP_DENY_FACILITY|LIBWRAP_DENY_SEVERITY;
51 #ifndef PORTMAP_LOG_FACILITY
52 # define PORTMAP_LOG_FACILITY LOG_AUTH
54 #ifndef PORTMAP_LOG_SEVERITY
55 # define PORTMAP_LOG_SEVERITY LOG_INFO
57 int log_severity = PORTMAP_LOG_FACILITY|PORTMAP_LOG_SEVERITY;
59 extern int verboselog;
62 check_access(SVCXPRT *xprt, rpcproc_t proc, void *args, unsigned int rpcbvers)
64 struct netbuf *caller = svc_getrpccaller(xprt);
65 struct sockaddr *addr = (struct sockaddr *)caller->buf;
67 struct request_info req;
74 * The older PMAP_* equivalents have the same numbers, so
75 * they are accounted for here as well.
78 case RPCBPROC_GETADDR:
81 if (rpcbvers > PMAPVERS) {
85 pmap = (struct pmap *)args;
88 if (proc == RPCBPROC_GETADDR)
90 if (!insecure && !is_loopback(caller)) {
92 logit(log_severity, addr, proc, prog,
93 " declined (non-loopback sender)");
98 case RPCBPROC_INDIRECT:
100 case RPCBPROC_GETTIME:
101 case RPCBPROC_UADDR2TADDR:
102 case RPCBPROC_TADDR2UADDR:
103 case RPCBPROC_GETVERSADDR:
104 case RPCBPROC_GETADDRLIST:
105 case RPCBPROC_GETSTAT:
111 if (addr->sa_family == AF_LOCAL)
113 request_init(&req, RQ_DAEMON, "rpcbind", RQ_CLIENT_SIN, addr, 0);
115 if(!hosts_access(&req)) {
116 logit(deny_severity, addr, proc, prog, ": request from unauthorized host");
121 logit(log_severity, addr, proc, prog, "");
126 is_loopback(struct netbuf *nbuf)
128 struct sockaddr *addr = (struct sockaddr *)nbuf->buf;
129 struct sockaddr_in *sin;
131 struct sockaddr_in6 *sin6;
134 switch (addr->sa_family) {
138 sin = (struct sockaddr_in *)addr;
139 return ((sin->sin_addr.s_addr == htonl(INADDR_LOOPBACK)) &&
140 (ntohs(sin->sin_port) < IPPORT_RESERVED));
145 sin6 = (struct sockaddr_in6 *)addr;
146 return (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr) &&
147 (ntohs(sin6->sin6_port) < IPV6PORT_RESERVED));
159 /* logit - report events of interest via the syslog daemon */
161 logit(int severity, struct sockaddr *addr, rpcproc_t procnum, rpcprog_t prognum,
164 const char *procname;
168 char fromname[NI_MAXHOST];
170 static const char *procmap[] = {
171 /* RPCBPROC_NULL */ "null",
172 /* RPCBPROC_SET */ "set",
173 /* RPCBPROC_UNSET */ "unset",
174 /* RPCBPROC_GETADDR */ "getport/addr",
175 /* RPCBPROC_DUMP */ "dump",
176 /* RPCBPROC_CALLIT */ "callit",
177 /* RPCBPROC_GETTIME */ "gettime",
178 /* RPCBPROC_UADDR2TADDR */ "uaddr2taddr",
179 /* RPCBPROC_TADDR2UADDR */ "taddr2uaddr",
180 /* RPCBPROC_GETVERSADDR */ "getversaddr",
181 /* RPCBPROC_INDIRECT */ "indirect",
182 /* RPCBPROC_GETADDRLIST */ "getaddrlist",
183 /* RPCBPROC_GETSTAT */ "getstat"
187 * Fork off a process or the portmap daemon might hang while
188 * getrpcbynumber() or syslog() does its thing.
192 setproctitle("logit");
194 /* Try to map program number to name. */
198 } else if ((rpc = getrpcbynumber((int) prognum))) {
199 progname = rpc->r_name;
201 snprintf(progname = progbuf, sizeof(progbuf), "%u",
205 /* Try to map procedure number to name. */
207 if (procnum >= (sizeof procmap / sizeof (char *))) {
208 snprintf(procbuf, sizeof procbuf, "%u",
212 procname = procmap[procnum];
214 /* Write syslog record. */
216 if (addr->sa_family == AF_LOCAL)
217 strcpy(fromname, "local");
219 getnameinfo(addr, addr->sa_len, fromname,
220 sizeof fromname, NULL, 0, NI_NUMERICHOST);
222 syslog(severity, "connect from %s to %s(%s)%s",
223 fromname, procname, progname, text);
229 check_callit(SVCXPRT *xprt, struct r_rmtcall_args *args, int versnum __unused)
231 struct sockaddr *sa = (struct sockaddr *)svc_getrpccaller(xprt)->buf;
234 * Always allow calling NULLPROC
236 if (args->rmt_proc == 0)
240 * XXX - this special casing sucks.
242 switch (args->rmt_prog) {
245 * Allow indirect calls to ourselves in insecure mode.
246 * The is_loopback checks aren't useful then anyway.
252 if (args->rmt_proc != MOUNTPROC_MNT &&
253 args->rmt_proc != MOUNTPROC_UMNT)
257 if (args->rmt_proc != YPBINDPROC_SETDOM)
265 switch (args->rmt_proc) {
281 logit(deny_severity, sa, args->rmt_proc, args->rmt_prog,
282 ": indirect call not allowed");
284 logit(0, sa, args->rmt_proc, args->rmt_prog,
285 ": indirect call not allowed");