CVE-2023-0614 ldb: Use binary search to check whether attribute is secret
[Samba.git] / source4 / lib / socket / interface.c
blob181893a9e7fcbad8e95b20ff4a0b500b59f169bd
1 /*
2 Unix SMB/CIFS implementation.
4 multiple interface handling
6 Copyright (C) Andrew Tridgell 1992-2005
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.
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.
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/>.
22 #include "includes.h"
23 #include "system/network.h"
24 #include "param/param.h"
25 #include "lib/socket/netif.h"
26 #include "../lib/util/util_net.h"
27 #include "../lib/util/dlinklist.h"
28 #include "lib/util/smb_strtox.h"
30 /* used for network interfaces */
31 struct interface {
32 struct interface *next, *prev;
33 char *name;
34 int flags;
35 struct sockaddr_storage ip;
36 struct sockaddr_storage netmask;
37 struct sockaddr_storage bcast;
38 const char *ip_s;
39 const char *bcast_s;
40 const char *nmask_s;
43 #define ALLONES ((uint32_t)0xFFFFFFFF)
45 address construction based on a patch from fred@datalync.com
47 #define MKBCADDR(_IP, _NM) ((_IP & _NM) | (_NM ^ ALLONES))
48 #define MKNETADDR(_IP, _NM) (_IP & _NM)
50 /****************************************************************************
51 Try and find an interface that matches an ip. If we cannot, return NULL
52 **************************************************************************/
53 static struct interface *iface_list_find(struct interface *interfaces,
54 const struct sockaddr *ip,
55 bool check_mask)
57 struct interface *i;
59 if (is_address_any(ip)) {
60 return interfaces;
63 for (i=interfaces;i;i=i->next) {
64 if (check_mask) {
65 if (same_net(ip, (struct sockaddr *)&i->ip, (struct sockaddr *)&i->netmask)) {
66 return i;
68 } else if (sockaddr_equal((struct sockaddr *)&i->ip, ip)) {
69 return i;
73 return NULL;
76 /****************************************************************************
77 add an interface to the linked list of interfaces
78 ****************************************************************************/
79 static void add_interface(TALLOC_CTX *mem_ctx, const struct iface_struct *ifs, struct interface **interfaces)
81 char addr[INET6_ADDRSTRLEN];
82 struct interface *iface;
84 if (iface_list_find(*interfaces, (const struct sockaddr *)&ifs->ip, false)) {
85 DEBUG(3,("add_interface: not adding duplicate interface %s\n",
86 print_sockaddr(addr, sizeof(addr), &ifs->ip) ));
87 return;
90 if (ifs->ip.ss_family == AF_INET &&
91 !(ifs->flags & (IFF_BROADCAST|IFF_LOOPBACK))) {
92 DEBUG(3,("not adding non-broadcast interface %s\n",
93 ifs->name ));
94 return;
97 iface = talloc(*interfaces == NULL ? mem_ctx : *interfaces, struct interface);
98 if (iface == NULL)
99 return;
101 ZERO_STRUCTPN(iface);
103 iface->name = talloc_strdup(iface, ifs->name);
104 if (!iface->name) {
105 SAFE_FREE(iface);
106 return;
108 iface->flags = ifs->flags;
109 iface->ip = ifs->ip;
110 iface->netmask = ifs->netmask;
111 iface->bcast = ifs->bcast;
113 /* keep string versions too, to avoid people tripping over the implied
114 static in inet_ntoa() */
115 print_sockaddr(addr, sizeof(addr), &iface->ip);
116 DEBUG(4,("added interface %s ip=%s ",
117 iface->name, addr));
118 iface->ip_s = talloc_strdup(iface, addr);
120 print_sockaddr(addr, sizeof(addr),
121 &iface->bcast);
122 DEBUG(4,("bcast=%s ", addr));
123 iface->bcast_s = talloc_strdup(iface, addr);
125 print_sockaddr(addr, sizeof(addr),
126 &iface->netmask);
127 DEBUG(4,("netmask=%s\n", addr));
128 iface->nmask_s = talloc_strdup(iface, addr);
131 this needs to be a ADD_END, as some tests (such as the
132 spoolss notify test) depend on the interfaces ordering
134 DLIST_ADD_END(*interfaces, iface);
138 interpret a single element from a interfaces= config line
140 This handles the following different forms:
142 1) wildcard interface name
143 2) DNS name
144 3) IP/masklen
145 4) ip/mask
146 5) bcast/mask
148 static void interpret_interface(TALLOC_CTX *mem_ctx,
149 const char *token,
150 struct iface_struct *probed_ifaces,
151 int total_probed,
152 struct interface **local_interfaces)
154 struct sockaddr_storage ss;
155 struct sockaddr_storage ss_mask;
156 struct sockaddr_storage ss_net;
157 struct sockaddr_storage ss_bcast;
158 struct iface_struct ifs;
159 char *p;
160 int i;
161 bool added=false;
162 bool goodaddr = false;
164 /* first check if it is an interface name */
165 for (i=0;i<total_probed;i++) {
166 if (gen_fnmatch(token, probed_ifaces[i].name) == 0) {
167 add_interface(mem_ctx, &probed_ifaces[i],
168 local_interfaces);
169 added = true;
172 if (added) {
173 return;
176 p = strchr_m(token, ';');
177 if (p != NULL) {
179 * skip smbd-specific extra data:
180 * link speed, capabilities, and interface index
182 *p = 0;
185 /* maybe it is a DNS name */
186 p = strchr_m(token,'/');
187 if (p == NULL) {
188 if (!interpret_string_addr(&ss, token, 0)) {
189 DEBUG(2, ("interpret_interface: Can't find address "
190 "for %s\n", token));
191 return;
194 for (i=0;i<total_probed;i++) {
195 if (sockaddr_equal((struct sockaddr *)&ss, (struct sockaddr *)&probed_ifaces[i].ip)) {
196 add_interface(mem_ctx, &probed_ifaces[i],
197 local_interfaces);
198 return;
201 DEBUG(2,("interpret_interface: "
202 "can't determine interface for %s\n",
203 token));
204 return;
207 /* parse it into an IP address/netmasklength pair */
208 *p = 0;
209 goodaddr = interpret_string_addr(&ss, token, 0);
210 *p++ = '/';
212 if (!goodaddr) {
213 DEBUG(2,("interpret_interface: "
214 "can't determine interface for %s\n",
215 token));
216 return;
219 if (strlen(p) > 2) {
220 goodaddr = interpret_string_addr(&ss_mask, p, 0);
221 if (!goodaddr) {
222 DEBUG(2,("interpret_interface: "
223 "can't determine netmask from %s\n",
224 p));
225 return;
227 } else {
228 int error = 0;
230 unsigned long val = smb_strtoul(p,
231 NULL,
233 &error,
234 SMB_STR_FULL_STR_CONV);
235 if (error != 0) {
236 DEBUG(2,("interpret_interface: "
237 "can't determine netmask value from %s\n",
238 p));
239 return;
241 if (!make_netmask(&ss_mask, &ss, val)) {
242 DEBUG(2,("interpret_interface: "
243 "can't apply netmask value %lu from %s\n",
244 val,
245 p));
246 return;
250 make_bcast(&ss_bcast, &ss, &ss_mask);
251 make_net(&ss_net, &ss, &ss_mask);
253 /* Maybe the first component was a broadcast address. */
254 if (sockaddr_equal((struct sockaddr *)&ss_bcast, (struct sockaddr *)&ss) ||
255 sockaddr_equal((struct sockaddr *)&ss_net, (struct sockaddr *)&ss)) {
256 for (i=0;i<total_probed;i++) {
257 if (same_net((struct sockaddr *)&ss,
258 (struct sockaddr *)&probed_ifaces[i].ip,
259 (struct sockaddr *)&ss_mask)) {
260 /* Temporarily replace netmask on
261 * the detected interface - user knows
262 * best.... */
263 struct sockaddr_storage saved_mask =
264 probed_ifaces[i].netmask;
265 probed_ifaces[i].netmask = ss_mask;
266 DEBUG(2,("interpret_interface: "
267 "using netmask value %s from "
268 "config file on interface %s\n",
270 probed_ifaces[i].name));
271 add_interface(mem_ctx, &probed_ifaces[i],
272 local_interfaces);
273 probed_ifaces[i].netmask = saved_mask;
274 return;
277 DEBUG(2,("interpret_interface: Can't determine ip for "
278 "broadcast address %s\n",
279 token));
280 return;
283 /* Just fake up the interface definition. User knows best. */
285 DEBUG(2,("interpret_interface: Adding interface %s\n",
286 token));
288 ZERO_STRUCT(ifs);
289 (void)strlcpy(ifs.name, token, sizeof(ifs.name));
290 ifs.flags = IFF_BROADCAST;
291 ifs.ip = ss;
292 ifs.netmask = ss_mask;
293 ifs.bcast = ss_bcast;
294 add_interface(mem_ctx, &ifs, local_interfaces);
299 load the list of network interfaces
301 void load_interface_list(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx, struct interface **local_interfaces)
303 const char **ptr = lpcfg_interfaces(lp_ctx);
304 int i;
305 struct iface_struct *ifaces = NULL;
306 int total_probed;
308 *local_interfaces = NULL;
310 /* probe the kernel for interfaces */
311 total_probed = get_interfaces(mem_ctx, &ifaces);
313 /* if we don't have a interfaces line then use all interfaces
314 except loopback */
315 if (!ptr || !*ptr || !**ptr) {
316 if (total_probed <= 0) {
317 DEBUG(0,("ERROR: Could not determine network interfaces, you must use a interfaces config line\n"));
319 for (i=0;i<total_probed;i++) {
320 if (!is_loopback_addr((struct sockaddr *)&ifaces[i].ip)) {
321 add_interface(mem_ctx, &ifaces[i], local_interfaces);
326 while (ptr && *ptr) {
327 interpret_interface(mem_ctx, *ptr, ifaces, total_probed, local_interfaces);
328 ptr++;
331 if (!*local_interfaces) {
332 DEBUG(0,("WARNING: no network interfaces found\n"));
334 talloc_free(ifaces);
338 how many interfaces do we have
340 int iface_list_count(struct interface *ifaces)
342 int ret = 0;
343 struct interface *i;
345 for (i=ifaces;i;i=i->next)
346 ret++;
347 return ret;
351 return IP of the Nth interface
353 const char *iface_list_n_ip(struct interface *ifaces, int n)
355 struct interface *i;
357 for (i=ifaces;i && n;i=i->next)
358 n--;
360 if (i) {
361 return i->ip_s;
363 return NULL;
368 return the first IPv4 interface address we have registered
370 const char *iface_list_first_v4(struct interface *ifaces)
372 struct interface *i;
374 for (i=ifaces; i; i=i->next) {
375 if (i->ip.ss_family == AF_INET) {
376 return i->ip_s;
379 return NULL;
383 return the first IPv6 interface address we have registered
385 static const char *iface_list_first_v6(struct interface *ifaces)
387 struct interface *i;
389 #ifdef HAVE_IPV6
390 for (i=ifaces; i; i=i->next) {
391 if (i->ip.ss_family == AF_INET6) {
392 return i->ip_s;
395 #endif
396 return NULL;
400 check if an interface is IPv4
402 bool iface_list_n_is_v4(struct interface *ifaces, int n)
404 struct interface *i;
406 for (i=ifaces;i && n;i=i->next)
407 n--;
409 if (i) {
410 return i->ip.ss_family == AF_INET;
412 return false;
416 return bcast of the Nth interface
418 const char *iface_list_n_bcast(struct interface *ifaces, int n)
420 struct interface *i;
422 for (i=ifaces;i && n;i=i->next)
423 n--;
425 if (i) {
426 return i->bcast_s;
428 return NULL;
432 return netmask of the Nth interface
434 const char *iface_list_n_netmask(struct interface *ifaces, int n)
436 struct interface *i;
438 for (i=ifaces;i && n;i=i->next)
439 n--;
441 if (i) {
442 return i->nmask_s;
444 return NULL;
448 return the local IP address that best matches a destination IP, or
449 our first interface if none match
451 const char *iface_list_best_ip(struct interface *ifaces, const char *dest)
453 struct interface *iface;
454 struct sockaddr_storage ss;
456 if (!interpret_string_addr(&ss, dest, AI_NUMERICHOST)) {
457 return iface_list_n_ip(ifaces, 0);
459 iface = iface_list_find(ifaces, (const struct sockaddr *)&ss, true);
460 if (iface) {
461 return iface->ip_s;
463 #ifdef HAVE_IPV6
464 if (ss.ss_family == AF_INET6) {
465 return iface_list_first_v6(ifaces);
467 #endif
468 return iface_list_first_v4(ifaces);
472 return true if an IP is one one of our local networks
474 bool iface_list_is_local(struct interface *ifaces, const char *dest)
476 struct sockaddr_storage ss;
478 if (!interpret_string_addr(&ss, dest, AI_NUMERICHOST)) {
479 return false;
481 if (iface_list_find(ifaces, (const struct sockaddr *)&ss, true)) {
482 return true;
484 return false;
488 return true if a IP matches a IP/netmask pair
490 bool iface_list_same_net(const char *ip1, const char *ip2, const char *netmask)
492 struct sockaddr_storage ip1_ss, ip2_ss, nm_ss;
494 if (!interpret_string_addr(&ip1_ss, ip1, AI_NUMERICHOST)) {
495 return false;
497 if (!interpret_string_addr(&ip2_ss, ip2, AI_NUMERICHOST)) {
498 return false;
500 if (!interpret_string_addr(&nm_ss, netmask, AI_NUMERICHOST)) {
501 return false;
504 return same_net((struct sockaddr *)&ip1_ss,
505 (struct sockaddr *)&ip2_ss,
506 (struct sockaddr *)&nm_ss);
510 return the list of wildcard interfaces
511 this will include the IPv4 0.0.0.0, and may include IPv6 ::
513 char **iface_list_wildcard(TALLOC_CTX *mem_ctx)
515 char **ret;
516 #ifdef HAVE_IPV6
517 ret = str_list_make(mem_ctx, "::,0.0.0.0", NULL);
518 #else
519 ret = str_list_make(mem_ctx, "0.0.0.0", NULL);
520 #endif
521 return ret;