Merge branches 'master' and 'suser_to_priv'
[dragonfly.git] / usr.sbin / rpcbind / util.c
1 /*-
2  * Copyright (c) 2000 The NetBSD Foundation, Inc.
3  * All rights reserved.
4  *
5  * This code is derived from software contributed to The NetBSD Foundation
6  * by Frank van der Linden.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *      This product includes software developed by the NetBSD
19  *      Foundation, Inc. and its contributors.
20  * 4. Neither the name of The NetBSD Foundation nor the names of its
21  *    contributors may be used to endorse or promote products derived
22  *    from this software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
25  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
26  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
27  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
28  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34  * POSSIBILITY OF SUCH DAMAGE.
35  *
36  * $NetBSD: util.c,v 1.4 2000/08/03 00:04:30 fvdl Exp $
37  * $FreeBSD: src/usr.sbin/rpcbind/util.c,v 1.6 2007/11/07 10:53:39 kevlo Exp $
38  * $DragonFly$
39  */
40
41 #include <sys/types.h>
42 #include <sys/socket.h>
43 #include <sys/queue.h>
44 #include <net/if.h>
45 #include <netinet/in.h>
46 #include <ifaddrs.h>
47 #include <sys/poll.h>
48 #include <rpc/rpc.h>
49 #include <errno.h>
50 #include <stdlib.h>
51 #include <string.h>
52 #include <unistd.h>
53 #include <netdb.h>
54 #include <netconfig.h>
55 #include <stdio.h>
56 #include <arpa/inet.h>
57
58 #include "rpcbind.h"
59
60 #define SA2SIN(sa)      ((struct sockaddr_in *)(sa))
61 #define SA2SINADDR(sa)  (SA2SIN(sa)->sin_addr)
62 #ifdef INET6
63 #define SA2SIN6(sa)     ((struct sockaddr_in6 *)(sa))
64 #define SA2SIN6ADDR(sa) (SA2SIN6(sa)->sin6_addr)
65 #endif
66
67 static struct sockaddr_in *local_in4;
68 #ifdef INET6
69 static struct sockaddr_in6 *local_in6;
70 #endif
71
72 static int bitmaskcmp(void *, void *, void *, int);
73 #ifdef INET6
74 static void in6_fillscopeid(struct sockaddr_in6 *);
75 #endif
76
77 /*
78  * For all bits set in "mask", compare the corresponding bits in
79  * "dst" and "src", and see if they match. Returns 0 if the addresses
80  * match.
81  */
82 static int
83 bitmaskcmp(void *dst, void *src, void *mask, int bytelen)
84 {
85         int i;
86         u_int8_t *p1 = dst, *p2 = src, *netmask = mask;
87
88         for (i = 0; i < bytelen; i++)
89                 if ((p1[i] & netmask[i]) != (p2[i] & netmask[i]))
90                         return (1);
91         return (0);
92 }
93
94 /*
95  * Similar to code in ifconfig.c. Fill in the scope ID for link-local
96  * addresses returned by getifaddrs().
97  */
98 #ifdef INET6
99 static void
100 in6_fillscopeid(struct sockaddr_in6 *sin6)
101 {
102         u_int16_t ifindex;
103
104         if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
105                 ifindex = ntohs(*(u_int16_t *)&sin6->sin6_addr.s6_addr[2]);
106                 if (sin6->sin6_scope_id == 0 && ifindex != 0) {
107                         sin6->sin6_scope_id = ifindex;
108                         *(u_int16_t *)&sin6->sin6_addr.s6_addr[2] = 0;
109                 }
110         }
111 }
112 #endif
113
114 /*
115  * Find a server address that can be used by `caller' to contact
116  * the local service specified by `serv_uaddr'. If `clnt_uaddr' is
117  * non-NULL, it is used instead of `caller' as a hint suggesting
118  * the best address (e.g. the `r_addr' field of an rpc, which
119  * contains the rpcbind server address that the caller used).
120  *
121  * Returns the best server address as a malloc'd "universal address"
122  * string which should be freed by the caller. On error, returns NULL.
123  */
124 char *
125 addrmerge(struct netbuf *caller, char *serv_uaddr, char *clnt_uaddr,
126           char *netid)
127 {
128         struct ifaddrs *ifap, *ifp = NULL, *bestif;
129         struct netbuf *serv_nbp = NULL, *hint_nbp = NULL, tbuf;
130         struct sockaddr *caller_sa, *hint_sa, *ifsa, *ifmasksa, *serv_sa;
131         struct sockaddr_storage ss;
132         struct netconfig *nconf;
133         char *caller_uaddr = NULL, *hint_uaddr = NULL;
134         char *ret = NULL;
135
136 #ifdef ND_DEBUG
137         if (debugging)
138                 fprintf(stderr, "addrmerge(caller, %s, %s, %s\n", serv_uaddr,
139                     clnt_uaddr == NULL ? "NULL" : clnt_uaddr, netid);
140 #endif
141         caller_sa = caller->buf;
142         if ((nconf = rpcbind_get_conf(netid)) == NULL)
143                 goto freeit;
144         if ((caller_uaddr = taddr2uaddr(nconf, caller)) == NULL)
145                 goto freeit;
146
147         /*
148          * Use `clnt_uaddr' as the hint if non-NULL, but ignore it if its
149          * address family is different from that of the caller.
150          */
151         hint_sa = NULL;
152         if (clnt_uaddr != NULL) {
153                 hint_uaddr = clnt_uaddr;
154                 if ((hint_nbp = uaddr2taddr(nconf, clnt_uaddr)) == NULL)
155                         goto freeit;
156                 hint_sa = hint_nbp->buf;
157         }
158         if (hint_sa == NULL || hint_sa->sa_family != caller_sa->sa_family) {
159                 hint_uaddr = caller_uaddr;
160                 hint_sa = caller->buf;
161         }
162
163 #ifdef ND_DEBUG
164         if (debugging)
165                 fprintf(stderr, "addrmerge: hint %s\n", hint_uaddr);
166 #endif
167         /* Local caller, just return the server address. */
168         if (strncmp(caller_uaddr, "0.0.0.0.", 8) == 0 ||
169             strncmp(caller_uaddr, "::.", 3) == 0 || caller_uaddr[0] == '/') {
170                 ret = strdup(serv_uaddr);
171                 goto freeit;
172         }
173
174         if (getifaddrs(&ifp) < 0)
175                 goto freeit;
176
177         /*
178          * Loop through all interfaces. For each interface, see if the
179          * network portion of its address is equal to that of the client.
180          * If so, we have found the interface that we want to use.
181          */
182         bestif = NULL;
183         for (ifap = ifp; ifap != NULL; ifap = ifap->ifa_next) {
184                 ifsa = ifap->ifa_addr;
185                 ifmasksa = ifap->ifa_netmask;
186
187                 if (ifsa == NULL || ifsa->sa_family != hint_sa->sa_family ||
188                     !(ifap->ifa_flags & IFF_UP))
189                         continue;
190
191                 switch (hint_sa->sa_family) {
192                 case AF_INET:
193                         /*
194                          * If the hint address matches this interface
195                          * address/netmask, then we're done.
196                          */
197                         if (!bitmaskcmp(&SA2SINADDR(ifsa),
198                             &SA2SINADDR(hint_sa), &SA2SINADDR(ifmasksa),
199                             sizeof(struct in_addr))) {
200                                 bestif = ifap;
201                                 goto found;
202                         }
203                         break;
204 #ifdef INET6
205                 case AF_INET6:
206                         /*
207                          * For v6 link local addresses, if the caller is on
208                          * a link-local address then use the scope id to see
209                          * which one.
210                          */
211                         in6_fillscopeid(SA2SIN6(ifsa));
212                         if (IN6_IS_ADDR_LINKLOCAL(&SA2SIN6ADDR(ifsa)) &&
213                             IN6_IS_ADDR_LINKLOCAL(&SA2SIN6ADDR(caller_sa)) &&
214                             IN6_IS_ADDR_LINKLOCAL(&SA2SIN6ADDR(hint_sa))) {
215                                 if (SA2SIN6(ifsa)->sin6_scope_id ==
216                                     SA2SIN6(caller_sa)->sin6_scope_id) {
217                                         bestif = ifap;
218                                         goto found;
219                                 }
220                         } else if (!bitmaskcmp(&SA2SIN6ADDR(ifsa),
221                             &SA2SIN6ADDR(hint_sa), &SA2SIN6ADDR(ifmasksa),
222                             sizeof(struct in6_addr))) {
223                                 bestif = ifap;
224                                 goto found;
225                         }
226                         break;
227 #endif
228                 default:
229                         continue;
230                 }
231
232                 /*
233                  * Remember the first possibly useful interface, preferring
234                  * "normal" to point-to-point and loopback ones.
235                  */
236                 if (bestif == NULL ||
237                     (!(ifap->ifa_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) &&
238                     (bestif->ifa_flags & (IFF_LOOPBACK | IFF_POINTOPOINT))))
239                         bestif = ifap;
240         }
241         if (bestif == NULL)
242                 goto freeit;
243
244 found:
245         /*
246          * Construct the new address using the the address from
247          * `bestif', and the port number from `serv_uaddr'.
248          */
249         serv_nbp = uaddr2taddr(nconf, serv_uaddr);
250         if (serv_nbp == NULL)
251                 goto freeit;
252         serv_sa = serv_nbp->buf;
253
254         memcpy(&ss, bestif->ifa_addr, bestif->ifa_addr->sa_len);
255         switch (ss.ss_family) {
256         case AF_INET:
257                 SA2SIN(&ss)->sin_port = SA2SIN(serv_sa)->sin_port;
258                 break;
259 #ifdef INET6
260         case AF_INET6:
261                 SA2SIN6(&ss)->sin6_port = SA2SIN6(serv_sa)->sin6_port;
262                 break;
263 #endif
264         }
265         tbuf.len = ss.ss_len;
266         tbuf.maxlen = sizeof(ss);
267         tbuf.buf = &ss;
268         ret = taddr2uaddr(nconf, &tbuf);
269
270 freeit:
271         if (caller_uaddr != NULL)
272                 free(caller_uaddr);
273         if (hint_nbp != NULL) {
274                 free(hint_nbp->buf);
275                 free(hint_nbp);
276         }
277         if (serv_nbp != NULL) {
278                 free(serv_nbp->buf);
279                 free(serv_nbp);
280         }
281         if (ifp != NULL)
282                 freeifaddrs(ifp);
283
284 #ifdef ND_DEBUG
285         if (debugging)
286                 fprintf(stderr, "addrmerge: returning %s\n", ret);
287 #endif
288         return ret;
289 }
290
291 void
292 network_init(void)
293 {
294 #ifdef INET6
295         struct ifaddrs *ifap, *ifp;
296         struct ipv6_mreq mreq6;
297         unsigned int ifindex;
298         int s;
299 #endif
300         int ecode;
301         struct addrinfo hints, *res;
302
303         memset(&hints, 0, sizeof hints);
304         hints.ai_family = AF_INET;
305         if ((ecode = getaddrinfo(NULL, "sunrpc", &hints, &res))) {
306                 if (debugging)
307                         fprintf(stderr, "can't get local ip4 address: %s\n",
308                             gai_strerror(ecode));
309         } else {
310                 local_in4 = (struct sockaddr_in *)malloc(sizeof *local_in4);
311                 if (local_in4 == NULL) {
312                         if (debugging)
313                                 fprintf(stderr, "can't alloc local ip4 addr\n");
314                 }
315                 memcpy(local_in4, res->ai_addr, sizeof *local_in4);
316         }
317
318 #ifdef INET6
319         hints.ai_family = AF_INET6;
320         if ((ecode = getaddrinfo(NULL, "sunrpc", &hints, &res))) {
321                 if (debugging)
322                         fprintf(stderr, "can't get local ip6 address: %s\n",
323                             gai_strerror(ecode));
324         } else {
325                 local_in6 = (struct sockaddr_in6 *)malloc(sizeof *local_in6);
326                 if (local_in6 == NULL) {
327                         if (debugging)
328                                 fprintf(stderr, "can't alloc local ip6 addr\n");
329                 }
330                 memcpy(local_in6, res->ai_addr, sizeof *local_in6);
331         }
332
333         /*
334          * Now join the RPC ipv6 multicast group on all interfaces.
335          */
336         if (getifaddrs(&ifp) < 0)
337                 return;
338
339         mreq6.ipv6mr_interface = 0;
340         inet_pton(AF_INET6, RPCB_MULTICAST_ADDR, &mreq6.ipv6mr_multiaddr);
341
342         s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
343
344         /*
345          * Loop through all interfaces. For each IPv6 multicast-capable
346          * interface, join the RPC multicast group on that interface.
347          */
348         for (ifap = ifp; ifap != NULL; ifap = ifap->ifa_next) {
349                 if (ifap->ifa_addr->sa_family != AF_INET6 ||
350                     !(ifap->ifa_flags & IFF_MULTICAST))
351                         continue;
352                 ifindex = if_nametoindex(ifap->ifa_name);
353                 if (ifindex == mreq6.ipv6mr_interface)
354                         /*
355                          * Already did this one.
356                          */
357                         continue;
358                 mreq6.ipv6mr_interface = ifindex;
359                 if (setsockopt(s, IPPROTO_IPV6, IPV6_JOIN_GROUP, &mreq6,
360                     sizeof mreq6) < 0)
361                         if (debugging)
362                                 perror("setsockopt v6 multicast");
363         }
364 #endif
365
366         /* close(s); */
367 }
368
369 struct sockaddr *
370 local_sa(int af)
371 {
372         switch (af) {
373         case AF_INET:
374                 return (struct sockaddr *)local_in4;
375 #ifdef INET6
376         case AF_INET6:
377                 return (struct sockaddr *)local_in6;
378 #endif
379         default:
380                 return NULL;
381         }
382 }