s4:dsdb: Implement msDS-ManagedPassword attribute
[samba.git] / lib / socket / interfaces.c
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
6    Copyright (C) Jelmer Vernooij 2007
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22
23 #include "includes.h"
24 #include "system/network.h"
25 #include "interfaces.h"
26 #include "lib/util/tsort.h"
27
28 /****************************************************************************
29  Create a struct sockaddr_storage with the netmask bits set to 1.
30 ****************************************************************************/
31
32 bool make_netmask(struct sockaddr_storage *pss_out,
33                         const struct sockaddr_storage *pss_in,
34                         unsigned long masklen)
35 {
36         *pss_out = *pss_in;
37         /* Now apply masklen bits of mask. */
38 #if defined(HAVE_IPV6)
39         if (pss_in->ss_family == AF_INET6) {
40                 char *p = (char *)&((struct sockaddr_in6 *)pss_out)->sin6_addr;
41                 unsigned int i;
42
43                 if (masklen > 128) {
44                         return false;
45                 }
46                 for (i = 0; masklen >= 8; masklen -= 8, i++) {
47                         *p++ = 0xff;
48                 }
49                 /* Deal with the partial byte. */
50                 *p++ &= (0xff & ~(0xff>>masklen));
51                 i++;
52                 for (;i < sizeof(struct in6_addr); i++) {
53                         *p++ = '\0';
54                 }
55                 return true;
56         }
57 #endif
58         if (pss_in->ss_family == AF_INET) {
59                 if (masklen > 32) {
60                         return false;
61                 }
62                 ((struct sockaddr_in *)pss_out)->sin_addr.s_addr =
63                         htonl(((0xFFFFFFFFL >> masklen) ^ 0xFFFFFFFFL));
64                 return true;
65         }
66         return false;
67 }
68
69 /****************************************************************************
70  Create a struct sockaddr_storage set to the broadcast or network adress from
71  an incoming sockaddr_storage.
72 ****************************************************************************/
73
74 static void make_bcast_or_net(struct sockaddr_storage *pss_out,
75                         const struct sockaddr_storage *pss_in,
76                         const struct sockaddr_storage *nmask,
77                         bool make_bcast_p)
78 {
79         unsigned int i = 0, len = 0;
80         const char *pmask = NULL;
81         char *p = NULL;
82         *pss_out = *pss_in;
83
84         /* Set all zero netmask bits to 1. */
85 #if defined(HAVE_IPV6)
86         if (pss_in->ss_family == AF_INET6) {
87                 p = (char *)&((struct sockaddr_in6 *)pss_out)->sin6_addr;
88                 pmask = (const char *)&((const struct sockaddr_in6 *)nmask)->sin6_addr;
89                 len = 16;
90         }
91 #endif
92         if (pss_in->ss_family == AF_INET) {
93                 p = (char *)&((struct sockaddr_in *)pss_out)->sin_addr;
94                 pmask = (const char *)&((const struct sockaddr_in *)nmask)->sin_addr;
95                 len = 4;
96         }
97
98         for (i = 0; i < len; i++, p++, pmask++) {
99                 if (make_bcast_p) {
100                         *p = (*p & *pmask) | (*pmask ^ 0xff);
101                 } else {
102                         /* make_net */
103                         *p = (*p & *pmask);
104                 }
105         }
106 }
107
108 void make_bcast(struct sockaddr_storage *pss_out,
109                         const struct sockaddr_storage *pss_in,
110                         const struct sockaddr_storage *nmask)
111 {
112         make_bcast_or_net(pss_out, pss_in, nmask, true);
113 }
114
115 void make_net(struct sockaddr_storage *pss_out,
116                         const struct sockaddr_storage *pss_in,
117                         const struct sockaddr_storage *nmask)
118 {
119         make_bcast_or_net(pss_out, pss_in, nmask, false);
120 }
121
122
123 /****************************************************************************
124  Try the "standard" getifaddrs/freeifaddrs interfaces.
125  Also gets IPv6 interfaces.
126 ****************************************************************************/
127
128 /****************************************************************************
129  Get the netmask address for a local interface.
130 ****************************************************************************/
131
132 static int _get_interfaces(TALLOC_CTX *mem_ctx, struct iface_struct **pifaces)
133 {
134         struct iface_struct *ifaces;
135         struct ifaddrs *iflist = NULL;
136         struct ifaddrs *ifptr = NULL;
137         int count;
138         int total = 0;
139         size_t copy_size;
140
141         if (getifaddrs(&iflist) < 0) {
142                 return -1;
143         }
144
145         count = 0;
146         for (ifptr = iflist; ifptr != NULL; ifptr = ifptr->ifa_next) {
147                 if (!ifptr->ifa_addr || !ifptr->ifa_netmask) {
148                         continue;
149                 }
150                 if (!(ifptr->ifa_flags & IFF_UP)) {
151                         continue;
152                 }
153                 count += 1;
154         }
155
156         ifaces = talloc_array(mem_ctx, struct iface_struct, count);
157         if (ifaces == NULL) {
158                 errno = ENOMEM;
159                 return -1;
160         }
161
162         /* Loop through interfaces, looking for given IP address */
163         for (ifptr = iflist; ifptr != NULL; ifptr = ifptr->ifa_next) {
164
165                 if (!ifptr->ifa_addr || !ifptr->ifa_netmask) {
166                         continue;
167                 }
168
169                 /* Check the interface is up. */
170                 if (!(ifptr->ifa_flags & IFF_UP)) {
171                         continue;
172                 }
173
174                 memset(&ifaces[total], '\0', sizeof(ifaces[total]));
175
176                 copy_size = sizeof(struct sockaddr_in);
177
178                 ifaces[total].flags = ifptr->ifa_flags;
179
180 #if defined(HAVE_IPV6)
181                 if (ifptr->ifa_addr->sa_family == AF_INET6) {
182                         copy_size = sizeof(struct sockaddr_in6);
183                 }
184 #endif
185
186                 memcpy(&ifaces[total].ip, ifptr->ifa_addr, copy_size);
187                 memcpy(&ifaces[total].netmask, ifptr->ifa_netmask, copy_size);
188
189                 if (ifaces[total].flags & (IFF_BROADCAST|IFF_LOOPBACK)) {
190                         make_bcast(&ifaces[total].bcast,
191                                 &ifaces[total].ip,
192                                 &ifaces[total].netmask);
193                 } else if ((ifaces[total].flags & IFF_POINTOPOINT) &&
194                                ifptr->ifa_dstaddr ) {
195                         memcpy(&ifaces[total].bcast,
196                                 ifptr->ifa_dstaddr,
197                                 copy_size);
198 #if defined(HAVE_IPV6)
199                 } else if (ifptr->ifa_addr->sa_family == AF_INET6) {
200                         const struct sockaddr_in6 *sin6 =
201                                 (const struct sockaddr_in6 *)ifptr->ifa_addr;
202                         const struct in6_addr *in6 =
203                                 (const struct in6_addr *)&sin6->sin6_addr;
204
205                         if (IN6_IS_ADDR_LINKLOCAL(in6) || IN6_IS_ADDR_V4COMPAT(in6)) {
206                                 continue;
207                         }
208                         /* IPv6 does not have broadcast it uses multicast. */
209                         memset(&ifaces[total].bcast, '\0', copy_size);
210 #endif
211                 } else {
212                         continue;
213                 }
214
215                 if (strlcpy(ifaces[total].name, ifptr->ifa_name,
216                         sizeof(ifaces[total].name)) >=
217                                 sizeof(ifaces[total].name)) {
218                         /* Truncation ! Ignore. */
219                         continue;
220                 }
221                 total++;
222         }
223
224         freeifaddrs(iflist);
225
226         *pifaces = ifaces;
227         return total;
228 }
229
230 static int iface_comp(struct iface_struct *i1, struct iface_struct *i2)
231 {
232         int r;
233
234 #if defined(HAVE_IPV6)
235         /*
236          * If we have IPv6 - sort these interfaces lower
237          * than any IPv4 ones.
238          */
239         if (i1->ip.ss_family == AF_INET6 &&
240                         i2->ip.ss_family == AF_INET) {
241                 return -1;
242         } else if (i1->ip.ss_family == AF_INET &&
243                         i2->ip.ss_family == AF_INET6) {
244                 return 1;
245         }
246
247         if (i1->ip.ss_family == AF_INET6) {
248                 struct sockaddr_in6 *s1 = (struct sockaddr_in6 *)&i1->ip;
249                 struct sockaddr_in6 *s2 = (struct sockaddr_in6 *)&i2->ip;
250
251                 r = memcmp(&s1->sin6_addr,
252                                 &s2->sin6_addr,
253                                 sizeof(struct in6_addr));
254                 if (r) {
255                         return r;
256                 }
257
258                 s1 = (struct sockaddr_in6 *)&i1->netmask;
259                 s2 = (struct sockaddr_in6 *)&i2->netmask;
260
261                 r = memcmp(&s1->sin6_addr,
262                                 &s2->sin6_addr,
263                                 sizeof(struct in6_addr));
264                 if (r) {
265                         return r;
266                 }
267         }
268 #endif
269
270         /* AIX uses __ss_family instead of ss_family inside of
271            sockaddr_storage. Instead of trying to figure out which field to
272            use, we can just cast it to a sockaddr.
273          */
274
275         if (((struct sockaddr *)&i1->ip)->sa_family == AF_INET) {
276                 struct sockaddr_in *s1 = (struct sockaddr_in *)&i1->ip;
277                 struct sockaddr_in *s2 = (struct sockaddr_in *)&i2->ip;
278
279                 r = ntohl(s1->sin_addr.s_addr) -
280                         ntohl(s2->sin_addr.s_addr);
281                 if (r) {
282                         return r;
283                 }
284
285                 s1 = (struct sockaddr_in *)&i1->netmask;
286                 s2 = (struct sockaddr_in *)&i2->netmask;
287
288                 return ntohl(s1->sin_addr.s_addr) -
289                         ntohl(s2->sin_addr.s_addr);
290         }
291         return 0;
292 }
293
294 /* this wrapper is used to remove duplicates from the interface list generated
295    above */
296 int get_interfaces(TALLOC_CTX *mem_ctx, struct iface_struct **pifaces)
297 {
298         struct iface_struct *ifaces;
299         int total, i, j;
300
301         total = _get_interfaces(mem_ctx, &ifaces);
302         if (total <= 0) return total;
303
304         /* now we need to remove duplicates */
305         TYPESAFE_QSORT(ifaces, total, iface_comp);
306
307         for (i=1;i<total;) {
308                 if (iface_comp(&ifaces[i-1], &ifaces[i]) == 0) {
309                         for (j=i-1;j<total-1;j++) {
310                                 ifaces[j] = ifaces[j+1];
311                         }
312                         total--;
313                 } else {
314                         i++;
315                 }
316         }
317
318         *pifaces = ifaces;
319         return total;
320 }