1 /* $NetBSD: security.c,v 1.1 2010/07/26 15:53:01 pooka Exp $ */
5 #include <sys/socket.h>
6 #include <netinet/in.h>
9 #include <rpc/rpcb_prot.h>
10 #include <rpc/pmap_prot.h>
20 * XXX for special case checks in check_callit.
22 #include <rpcsvc/mount.h>
23 #include <rpcsvc/rquota.h>
24 #include <rpcsvc/nfs_prot.h>
25 #include <rpcsvc/yp.h>
26 #include <rpcsvc/ypclnt.h>
27 #include <rpcsvc/yppasswd.h>
33 #ifndef LIBWRAP_ALLOW_FACILITY
34 # define LIBWRAP_ALLOW_FACILITY LOG_AUTH
36 #ifndef LIBWRAP_ALLOW_SEVERITY
37 # define LIBWRAP_ALLOW_SEVERITY LOG_INFO
39 #ifndef LIBWRAP_DENY_FACILITY
40 # define LIBWRAP_DENY_FACILITY LOG_AUTH
42 #ifndef LIBWRAP_DENY_SEVERITY
43 # define LIBWRAP_DENY_SEVERITY LOG_WARNING
45 int allow_severity = LIBWRAP_ALLOW_FACILITY|LIBWRAP_ALLOW_SEVERITY;
46 int deny_severity = LIBWRAP_DENY_FACILITY|LIBWRAP_DENY_SEVERITY;
49 #ifndef PORTMAP_LOG_FACILITY
50 # define PORTMAP_LOG_FACILITY LOG_AUTH
52 #ifndef PORTMAP_LOG_SEVERITY
53 # define PORTMAP_LOG_SEVERITY LOG_INFO
55 int log_severity = PORTMAP_LOG_FACILITY|PORTMAP_LOG_SEVERITY;
57 extern int verboselog;
60 check_access(SVCXPRT *xprt, rpcproc_t proc, void *args, int rpcbvers)
62 struct netbuf *caller = svc_getrpccaller(xprt);
63 struct sockaddr *addr = (struct sockaddr *)caller->buf;
65 struct request_info req;
72 * The older PMAP_* equivalents have the same numbers, so
73 * they are accounted for here as well.
76 case RPCBPROC_GETADDR:
79 if (rpcbvers > PMAPVERS) {
83 pmap = (struct pmap *)args;
86 if (proc == RPCBPROC_GETADDR)
88 if (!insecure && !is_loopback(caller)) {
90 logit(log_severity, addr, proc, prog,
91 " declined (non-loopback sender)");
96 case RPCBPROC_INDIRECT:
98 case RPCBPROC_GETTIME:
99 case RPCBPROC_UADDR2TADDR:
100 case RPCBPROC_TADDR2UADDR:
101 case RPCBPROC_GETVERSADDR:
102 case RPCBPROC_GETADDRLIST:
103 case RPCBPROC_GETSTAT:
109 if (addr->sa_family == AF_LOCAL)
111 request_init(&req, RQ_DAEMON, "rpcbind", RQ_CLIENT_SIN, addr, 0);
113 if(!hosts_access(&req)) {
114 logit(deny_severity, addr, proc, prog, ": request from unauthorized host");
119 logit(log_severity, addr, proc, prog, "");
124 is_loopback(struct netbuf *nbuf)
126 struct sockaddr *addr = (struct sockaddr *)nbuf->buf;
127 struct sockaddr_in *sin;
129 struct sockaddr_in6 *sin6;
132 switch (addr->sa_family) {
136 sin = (struct sockaddr_in *)addr;
137 return ((sin->sin_addr.s_addr == htonl(INADDR_LOOPBACK)) &&
138 (ntohs(sin->sin_port) < IPPORT_RESERVED));
143 sin6 = (struct sockaddr_in6 *)addr;
144 return (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr) &&
145 (ntohs(sin6->sin6_port) < IPV6PORT_RESERVED));
157 /* logit - report events of interest via the syslog daemon */
159 logit(int severity, struct sockaddr *addr, rpcproc_t procnum, rpcprog_t prognum,
162 const char *procname;
166 char fromname[NI_MAXHOST];
168 static const char *procmap[] = {
169 /* RPCBPROC_NULL */ "null",
170 /* RPCBPROC_SET */ "set",
171 /* RPCBPROC_UNSET */ "unset",
172 /* RPCBPROC_GETADDR */ "getport/addr",
173 /* RPCBPROC_DUMP */ "dump",
174 /* RPCBPROC_CALLIT */ "callit",
175 /* RPCBPROC_GETTIME */ "gettime",
176 /* RPCBPROC_UADDR2TADDR */ "uaddr2taddr",
177 /* RPCBPROC_TADDR2UADDR */ "taddr2uaddr",
178 /* RPCBPROC_GETVERSADDR */ "getversaddr",
179 /* RPCBPROC_INDIRECT */ "indirect",
180 /* RPCBPROC_GETADDRLIST */ "getaddrlist",
181 /* RPCBPROC_GETSTAT */ "getstat"
185 * Fork off a process or the portmap daemon might hang while
186 * getrpcbynumber() or syslog() does its thing.
190 setproctitle("logit");
192 /* Try to map program number to name. */
195 progname = __UNCONST("");
196 } else if ((rpc = getrpcbynumber((int) prognum))) {
197 progname = rpc->r_name;
199 snprintf(progname = progbuf, sizeof(progbuf), "%u",
203 /* Try to map procedure number to name. */
205 if (procnum >= (sizeof procmap / sizeof (char *))) {
206 snprintf(procbuf, sizeof procbuf, "%u",
210 procname = procmap[procnum];
212 /* Write syslog record. */
214 if (addr->sa_family == AF_LOCAL)
215 strlcpy(fromname, "local", sizeof(fromname));
217 getnameinfo(addr, addr->sa_len, fromname,
218 sizeof fromname, NULL, 0, NI_NUMERICHOST);
220 syslog(severity, "connect from %s to %s(%s)%s",
221 fromname, procname, progname, text);
227 check_callit(SVCXPRT *xprt, struct r_rmtcall_args *args, int versnum)
229 struct sockaddr *sa = (struct sockaddr *)svc_getrpccaller(xprt)->buf;
232 * Always allow calling NULLPROC
234 if (args->rmt_proc == 0)
238 * XXX - this special casing sucks.
240 switch (args->rmt_prog) {
243 * Allow indirect calls to ourselves in insecure mode.
244 * The is_loopback checks aren't useful then anyway.
250 if (args->rmt_proc != MOUNTPROC_MNT &&
251 args->rmt_proc != MOUNTPROC_UMNT)
255 if (args->rmt_proc != YPBINDPROC_SETDOM)
263 switch (args->rmt_proc) {
278 logit(deny_severity, sa, args->rmt_proc, args->rmt_prog,
279 ": indirect call not allowed");