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