]> git.decadent.org.uk Git - nfs-utils.git/blob - support/misc/from_local.c
libexport.a: Allow client_init() to fail instead of exit
[nfs-utils.git] / support / misc / from_local.c
1  /*
2   * Check if an address belongs to the local system. Adapted from:
3   * 
4   * @(#)pmap_svc.c 1.32 91/03/11 Copyright 1984,1990 Sun Microsystems, Inc.
5   * @(#)get_myaddress.c  2.1 88/07/29 4.0 RPCSRC.
6   */
7
8 /*
9  * Copyright (c) 2009, Sun Microsystems, Inc.
10  * All rights reserved.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions are met:
14  * - Redistributions of source code must retain the above copyright notice,
15  *   this list of conditions and the following disclaimer.
16  * - Redistributions in binary form must reproduce the above copyright notice,
17  *   this list of conditions and the following disclaimer in the documentation
18  *   and/or other materials provided with the distribution.
19  * - Neither the name of Sun Microsystems, Inc. nor the names of its
20  *   contributors may be used to endorse or promote products derived
21  *   from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  */
35
36 #if 0
37 static char sccsid[] = "@(#) from_local.c 1.3 96/05/31 15:52:57";
38 #endif
39
40 #ifdef HAVE_CONFIG_H
41 #include <config.h>
42 #endif
43
44 #include <sys/types.h>
45 #include <sys/socket.h>
46 #include <stdbool.h>
47 #include <stdio.h>
48 #include <unistd.h>
49 #include <netdb.h>
50 #include <netinet/in.h>
51 #include <net/if.h>
52 #include <sys/ioctl.h>
53 #include <stdlib.h>
54 #include <string.h>
55
56 #include "sockaddr.h"
57 #include "tcpwrapper.h"
58 #include "xlog.h"
59
60 #ifndef TRUE
61 #define TRUE    1
62 #define FALSE   0
63 #endif
64
65 #ifdef HAVE_GETIFADDRS
66
67 #include <ifaddrs.h>
68 #include <time.h>
69
70 /**
71  * from_local - determine whether request comes from the local system
72  * @sap: pointer to socket address to check
73  *
74  * With virtual hosting, each hardware network interface can have
75  * multiple network addresses. On such machines the number of machine
76  * addresses can be surprisingly large.
77  *
78  * We also expect the local network configuration to change over time,
79  * so call getifaddrs(3) more than once, but not too often.
80  *
81  * Returns TRUE if the sockaddr contains an address of one of the local
82  * network interfaces.  Otherwise FALSE is returned.
83  */
84 int
85 from_local(const struct sockaddr *sap)
86 {
87         static struct ifaddrs *ifaddr = NULL;
88         static time_t last_update = 0;
89         struct ifaddrs *ifa;
90         unsigned int count;
91         time_t now;
92
93         if (time(&now) == ((time_t)-1)) {
94                 xlog(L_ERROR, "%s: time(2): %m", __func__);
95
96                 /* If we don't know what time it is, use the
97                  * existing ifaddr list, if one exists  */
98                 now = last_update;
99                 if (ifaddr == NULL)
100                         now++;
101         }
102         if (now != last_update) {
103                 xlog(D_GENERAL, "%s: updating local if addr list", __func__);
104
105                 if (ifaddr)
106                         freeifaddrs(ifaddr);
107
108                 if (getifaddrs(&ifaddr) == -1) {
109                         xlog(L_ERROR, "%s: getifaddrs(3): %m", __func__);
110                         return FALSE;
111                 }
112
113                 last_update = now;
114         }
115
116         count = 0;
117         for (ifa = ifaddr; ifa; ifa = ifa->ifa_next) {
118                 if ((ifa->ifa_flags & IFF_UP) &&
119                     nfs_compare_sockaddr(sap, ifa->ifa_addr)) {
120                         xlog(D_GENERAL, "%s: incoming address matches "
121                                         "local interface address", __func__);
122                         return TRUE;
123                 } else
124                         count++;
125         }
126
127         xlog(D_GENERAL, "%s: checked %u local if addrs; "
128                         "incoming address not found", __func__, count);
129         return FALSE;
130 }
131
132 #else   /* !HAVE_GETIFADDRS */
133
134 static int num_local;
135 static int num_addrs;
136 static struct in_addr *addrs;
137
138 /* grow_addrs - extend list of local interface addresses */
139
140 static int grow_addrs(void)
141 {
142     struct in_addr *new_addrs;
143     int     new_num;
144
145     /*
146      * Keep the previous result if we run out of memory. The system would
147      * really get hosed if we simply give up.
148      */
149     new_num = (addrs == 0) ? 1 : num_addrs + num_addrs;
150     new_addrs = (struct in_addr *) malloc(sizeof(*addrs) * new_num);
151     if (new_addrs == 0) {
152         xlog_warn("%s: out of memory", __func__);
153         return (0);
154     } else {
155         if (addrs != 0) {
156             memcpy((char *) new_addrs, (char *) addrs,
157                    sizeof(*addrs) * num_addrs);
158             free((char *) addrs);
159         }
160         num_addrs = new_num;
161         addrs = new_addrs;
162         return (1);
163     }
164 }
165
166 /* find_local - find all IP addresses for this host */
167 static int
168 find_local(void)
169 {
170     struct ifconf ifc;
171     struct ifreq ifreq;
172     struct ifreq *ifr;
173     struct ifreq *the_end;
174     int     sock;
175     char    buf[BUFSIZ];
176
177     /*
178      * Get list of network interfaces. We use a huge buffer to allow for the
179      * presence of non-IP interfaces.
180      */
181
182     if ((sock = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
183         xlog_warn("%s: socket(2): %m", __func__);
184         return (0);
185     }
186     ifc.ifc_len = sizeof(buf);
187     ifc.ifc_buf = buf;
188     if (ioctl(sock, SIOCGIFCONF, (char *) &ifc) < 0) {
189         xlog_warn("%s: ioctl(SIOCGIFCONF): %m", __func__);
190         (void) close(sock);
191         return (0);
192     }
193     /* Get IP address of each active IP network interface. */
194
195     the_end = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
196     num_local = 0;
197     for (ifr = ifc.ifc_req; ifr < the_end; ifr++) {
198         if (ifr->ifr_addr.sa_family == AF_INET) {       /* IP net interface */
199             ifreq = *ifr;
200             if (ioctl(sock, SIOCGIFFLAGS, (char *) &ifreq) < 0) {
201                 xlog_warn("%s: ioctl(SIOCGIFFLAGS): %m", __func__);
202             } else if (ifreq.ifr_flags & IFF_UP) {      /* active interface */
203                 if (ioctl(sock, SIOCGIFADDR, (char *) &ifreq) < 0) {
204                     xlog_warn("%s: ioctl(SIOCGIFADDR): %m", __func__);
205                 } else {
206                     if (num_local >= num_addrs)
207                         if (grow_addrs() == 0)
208                             break;
209                     addrs[num_local++] = ((struct sockaddr_in *)
210                                           & ifreq.ifr_addr)->sin_addr;
211                 }
212             }
213         }
214         /* Support for variable-length addresses. */
215 #ifdef HAS_SA_LEN
216         ifr = (struct ifreq *) ((caddr_t) ifr
217                       + ifr->ifr_addr.sa_len - sizeof(struct sockaddr));
218 #endif
219     }
220     (void) close(sock);
221     return (num_local);
222 }
223
224 /**
225  * from_local - determine whether request comes from the local system
226  * @sap: pointer to socket address to check
227  *
228  * With virtual hosting, each hardware network interface can have
229  * multiple network addresses. On such machines the number of machine
230  * addresses can be surprisingly large.
231  *
232  * Returns TRUE if the sockaddr contains an address of one of the local
233  * network interfaces.  Otherwise FALSE is returned.
234  */
235 int
236 from_local(const struct sockaddr *sap)
237 {
238     const struct sockaddr_in *addr = (const struct sockaddr_in *)sap;
239     int     i;
240
241     if (sap->sa_family != AF_INET)
242         return (FALSE);
243
244     if (addrs == 0 && find_local() == 0)
245         xlog(L_ERROR, "Cannot find any active local network interfaces");
246
247     for (i = 0; i < num_local; i++) {
248         if (memcmp((char *) &(addr->sin_addr), (char *) &(addrs[i]),
249                    sizeof(struct in_addr)) == 0)
250             return (TRUE);
251     }
252     return (FALSE);
253 }
254
255 #ifdef TEST
256
257 int main(void)
258 {
259     int     i;
260
261     find_local();
262     for (i = 0; i < num_local; i++)
263         printf("%s\n", inet_ntoa(addrs[i]));
264 }
265
266 #endif  /* TEST */
267
268 #endif  /* !HAVE_GETIFADDRS */