]> git.decadent.org.uk Git - nfs-utils.git/blob - support/misc/from_local.c
tcpwrappers: Use xlog() instead of perror(3) and syslog(2)
[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 <stdio.h>
47 #include <unistd.h>
48 #include <netdb.h>
49 #include <netinet/in.h>
50 #include <net/if.h>
51 #include <sys/ioctl.h>
52 #include <stdlib.h>
53 #include <string.h>
54
55 #include "tcpwrapper.h"
56 #include "xlog.h"
57
58 #ifndef TRUE
59 #define TRUE    1
60 #define FALSE   0
61 #endif
62
63  /*
64   * With virtual hosting, each hardware network interface can have multiple
65   * network addresses. On such machines the number of machine addresses can
66   * be surprisingly large.
67   */
68 static int num_local;
69 static int num_addrs;
70 static struct in_addr *addrs;
71
72 /* grow_addrs - extend list of local interface addresses */
73
74 static int grow_addrs(void)
75 {
76     struct in_addr *new_addrs;
77     int     new_num;
78
79     /*
80      * Keep the previous result if we run out of memory. The system would
81      * really get hosed if we simply give up.
82      */
83     new_num = (addrs == 0) ? 1 : num_addrs + num_addrs;
84     new_addrs = (struct in_addr *) malloc(sizeof(*addrs) * new_num);
85     if (new_addrs == 0) {
86         xlog_warn("%s: out of memory", __func__);
87         return (0);
88     } else {
89         if (addrs != 0) {
90             memcpy((char *) new_addrs, (char *) addrs,
91                    sizeof(*addrs) * num_addrs);
92             free((char *) addrs);
93         }
94         num_addrs = new_num;
95         addrs = new_addrs;
96         return (1);
97     }
98 }
99
100 /* find_local - find all IP addresses for this host */
101 static int
102 find_local(void)
103 {
104     struct ifconf ifc;
105     struct ifreq ifreq;
106     struct ifreq *ifr;
107     struct ifreq *the_end;
108     int     sock;
109     char    buf[BUFSIZ];
110
111     /*
112      * Get list of network interfaces. We use a huge buffer to allow for the
113      * presence of non-IP interfaces.
114      */
115
116     if ((sock = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
117         xlog_warn("%s: socket(2): %m", __func__);
118         return (0);
119     }
120     ifc.ifc_len = sizeof(buf);
121     ifc.ifc_buf = buf;
122     if (ioctl(sock, SIOCGIFCONF, (char *) &ifc) < 0) {
123         xlog_warn("%s: ioctl(SIOCGIFCONF): %m", __func__);
124         (void) close(sock);
125         return (0);
126     }
127     /* Get IP address of each active IP network interface. */
128
129     the_end = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
130     num_local = 0;
131     for (ifr = ifc.ifc_req; ifr < the_end; ifr++) {
132         if (ifr->ifr_addr.sa_family == AF_INET) {       /* IP net interface */
133             ifreq = *ifr;
134             if (ioctl(sock, SIOCGIFFLAGS, (char *) &ifreq) < 0) {
135                 xlog_warn("%s: ioctl(SIOCGIFFLAGS): %m", __func__);
136             } else if (ifreq.ifr_flags & IFF_UP) {      /* active interface */
137                 if (ioctl(sock, SIOCGIFADDR, (char *) &ifreq) < 0) {
138                     xlog_warn("%s: ioctl(SIOCGIFADDR): %m", __func__);
139                 } else {
140                     if (num_local >= num_addrs)
141                         if (grow_addrs() == 0)
142                             break;
143                     addrs[num_local++] = ((struct sockaddr_in *)
144                                           & ifreq.ifr_addr)->sin_addr;
145                 }
146             }
147         }
148         /* Support for variable-length addresses. */
149 #ifdef HAS_SA_LEN
150         ifr = (struct ifreq *) ((caddr_t) ifr
151                       + ifr->ifr_addr.sa_len - sizeof(struct sockaddr));
152 #endif
153     }
154     (void) close(sock);
155     return (num_local);
156 }
157
158 /* from_local - determine whether request comes from the local system */
159 int
160 from_local(struct sockaddr_in *addr)
161 {
162     int     i;
163
164     if (addrs == 0 && find_local() == 0)
165         xlog(L_ERROR, "Cannot find any active local network interfaces");
166
167     for (i = 0; i < num_local; i++) {
168         if (memcmp((char *) &(addr->sin_addr), (char *) &(addrs[i]),
169                    sizeof(struct in_addr)) == 0)
170             return (TRUE);
171     }
172     return (FALSE);
173 }
174
175 #ifdef TEST
176
177 int main(void)
178 {
179     int     i;
180
181     find_local();
182     for (i = 0; i < num_local; i++)
183         printf("%s\n", inet_ntoa(addrs[i]));
184 }
185
186 #endif  /* TEST */