s3 swat: Add XSRF protection to password page
[Samba.git] / source3 / lib / interfaces.c
blobe40eaa91425b6b5c628c9cb4427fd32790b0248c
1 /*
2 Unix SMB/CIFS implementation.
3 return a list of network interfaces
4 Copyright (C) Andrew Tridgell 1998
5 Copyright (C) Jeremy Allison 2007
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "includes.h"
22 #include "interfaces.h"
24 /****************************************************************************
25 Create a struct sockaddr_storage with the netmask bits set to 1.
26 ****************************************************************************/
28 bool make_netmask(struct sockaddr_storage *pss_out,
29 const struct sockaddr_storage *pss_in,
30 unsigned long masklen)
32 *pss_out = *pss_in;
33 /* Now apply masklen bits of mask. */
34 #if defined(HAVE_IPV6)
35 if (pss_in->ss_family == AF_INET6) {
36 char *p = (char *)&((struct sockaddr_in6 *)pss_out)->sin6_addr;
37 unsigned int i;
39 if (masklen > 128) {
40 return false;
42 for (i = 0; masklen >= 8; masklen -= 8, i++) {
43 *p++ = 0xff;
45 /* Deal with the partial byte. */
46 *p++ &= (0xff & ~(0xff>>masklen));
47 i++;
48 for (;i < sizeof(struct in6_addr); i++) {
49 *p++ = '\0';
51 return true;
53 #endif
54 if (pss_in->ss_family == AF_INET) {
55 if (masklen > 32) {
56 return false;
58 ((struct sockaddr_in *)pss_out)->sin_addr.s_addr =
59 htonl(((0xFFFFFFFFL >> masklen) ^ 0xFFFFFFFFL));
60 return true;
62 return false;
65 /****************************************************************************
66 Create a struct sockaddr_storage set to the broadcast or network adress from
67 an incoming sockaddr_storage.
68 ****************************************************************************/
70 static void make_bcast_or_net(struct sockaddr_storage *pss_out,
71 const struct sockaddr_storage *pss_in,
72 const struct sockaddr_storage *nmask,
73 bool make_bcast_p)
75 unsigned int i = 0, len = 0;
76 char *pmask = NULL;
77 char *p = NULL;
78 *pss_out = *pss_in;
80 /* Set all zero netmask bits to 1. */
81 #if defined(HAVE_IPV6)
82 if (pss_in->ss_family == AF_INET6) {
83 p = (char *)&((struct sockaddr_in6 *)pss_out)->sin6_addr;
84 pmask = (char *)&((struct sockaddr_in6 *)nmask)->sin6_addr;
85 len = 16;
87 #endif
88 if (pss_in->ss_family == AF_INET) {
89 p = (char *)&((struct sockaddr_in *)pss_out)->sin_addr;
90 pmask = (char *)&((struct sockaddr_in *)nmask)->sin_addr;
91 len = 4;
94 for (i = 0; i < len; i++, p++, pmask++) {
95 if (make_bcast_p) {
96 *p = (*p & *pmask) | (*pmask ^ 0xff);
97 } else {
98 /* make_net */
99 *p = (*p & *pmask);
104 void make_bcast(struct sockaddr_storage *pss_out,
105 const struct sockaddr_storage *pss_in,
106 const struct sockaddr_storage *nmask)
108 make_bcast_or_net(pss_out, pss_in, nmask, true);
111 void make_net(struct sockaddr_storage *pss_out,
112 const struct sockaddr_storage *pss_in,
113 const struct sockaddr_storage *nmask)
115 make_bcast_or_net(pss_out, pss_in, nmask, false);
118 /****************************************************************************
119 Try the "standard" getifaddrs/freeifaddrs interfaces.
120 Also gets IPv6 interfaces.
121 ****************************************************************************/
123 /****************************************************************************
124 Get the netmask address for a local interface.
125 ****************************************************************************/
127 static int _get_interfaces(TALLOC_CTX *mem_ctx, struct iface_struct **pifaces)
129 struct iface_struct *ifaces;
130 struct ifaddrs *iflist = NULL;
131 struct ifaddrs *ifptr = NULL;
132 int count;
133 int total = 0;
134 size_t copy_size;
136 if (getifaddrs(&iflist) < 0) {
137 return -1;
140 count = 0;
141 for (ifptr = iflist; ifptr != NULL; ifptr = ifptr->ifa_next) {
142 if (!ifptr->ifa_addr || !ifptr->ifa_netmask) {
143 continue;
145 if (!(ifptr->ifa_flags & IFF_UP)) {
146 continue;
148 count += 1;
151 ifaces = talloc_array(mem_ctx, struct iface_struct, count);
152 if (ifaces == NULL) {
153 errno = ENOMEM;
154 return -1;
157 /* Loop through interfaces, looking for given IP address */
158 for (ifptr = iflist; ifptr != NULL; ifptr = ifptr->ifa_next) {
160 if (!ifptr->ifa_addr || !ifptr->ifa_netmask) {
161 continue;
164 /* Check the interface is up. */
165 if (!(ifptr->ifa_flags & IFF_UP)) {
166 continue;
169 memset(&ifaces[total], '\0', sizeof(ifaces[total]));
171 copy_size = sizeof(struct sockaddr_in);
173 ifaces[total].flags = ifptr->ifa_flags;
175 #if defined(HAVE_IPV6)
176 if (ifptr->ifa_addr->sa_family == AF_INET6) {
177 copy_size = sizeof(struct sockaddr_in6);
179 #endif
181 memcpy(&ifaces[total].ip, ifptr->ifa_addr, copy_size);
182 memcpy(&ifaces[total].netmask, ifptr->ifa_netmask, copy_size);
184 if (ifaces[total].flags & (IFF_BROADCAST|IFF_LOOPBACK)) {
185 make_bcast(&ifaces[total].bcast,
186 &ifaces[total].ip,
187 &ifaces[total].netmask);
188 } else if ((ifaces[total].flags & IFF_POINTOPOINT) &&
189 ifptr->ifa_dstaddr ) {
190 memcpy(&ifaces[total].bcast,
191 ifptr->ifa_dstaddr,
192 copy_size);
193 } else {
194 continue;
197 strlcpy(ifaces[total].name, ifptr->ifa_name,
198 sizeof(ifaces[total].name));
199 total++;
202 freeifaddrs(iflist);
204 *pifaces = ifaces;
205 return total;
208 static int iface_comp(struct iface_struct *i1, struct iface_struct *i2)
210 int r;
212 #if defined(HAVE_IPV6)
214 * If we have IPv6 - sort these interfaces lower
215 * than any IPv4 ones.
217 if (i1->ip.ss_family == AF_INET6 &&
218 i2->ip.ss_family == AF_INET) {
219 return -1;
220 } else if (i1->ip.ss_family == AF_INET &&
221 i2->ip.ss_family == AF_INET6) {
222 return 1;
225 if (i1->ip.ss_family == AF_INET6) {
226 struct sockaddr_in6 *s1 = (struct sockaddr_in6 *)&i1->ip;
227 struct sockaddr_in6 *s2 = (struct sockaddr_in6 *)&i2->ip;
229 r = memcmp(&s1->sin6_addr,
230 &s2->sin6_addr,
231 sizeof(struct in6_addr));
232 if (r) {
233 return r;
236 s1 = (struct sockaddr_in6 *)&i1->netmask;
237 s2 = (struct sockaddr_in6 *)&i2->netmask;
239 r = memcmp(&s1->sin6_addr,
240 &s2->sin6_addr,
241 sizeof(struct in6_addr));
242 if (r) {
243 return r;
246 #endif
248 /* AIX uses __ss_family instead of ss_family inside of
249 sockaddr_storage. Instead of trying to figure out which field to
250 use, we can just cast it to a sockaddr.
253 if (((struct sockaddr *)&i1->ip)->sa_family == AF_INET) {
254 struct sockaddr_in *s1 = (struct sockaddr_in *)&i1->ip;
255 struct sockaddr_in *s2 = (struct sockaddr_in *)&i2->ip;
257 r = ntohl(s1->sin_addr.s_addr) -
258 ntohl(s2->sin_addr.s_addr);
259 if (r) {
260 return r;
263 s1 = (struct sockaddr_in *)&i1->netmask;
264 s2 = (struct sockaddr_in *)&i2->netmask;
266 return ntohl(s1->sin_addr.s_addr) -
267 ntohl(s2->sin_addr.s_addr);
269 return 0;
272 /* this wrapper is used to remove duplicates from the interface list generated
273 above */
274 int get_interfaces(TALLOC_CTX *mem_ctx, struct iface_struct **pifaces)
276 struct iface_struct *ifaces;
277 int total, i, j;
279 total = _get_interfaces(mem_ctx, &ifaces);
280 if (total <= 0) return total;
282 /* now we need to remove duplicates */
283 TYPESAFE_QSORT(ifaces, total, iface_comp);
285 for (i=1;i<total;) {
286 if (iface_comp(&ifaces[i-1], &ifaces[i]) == 0) {
287 for (j=i-1;j<total-1;j++) {
288 ifaces[j] = ifaces[j+1];
290 total--;
291 } else {
292 i++;
296 *pifaces = ifaces;
297 return total;