1 | /*
|
---|
2 | Unix SMB/CIFS implementation.
|
---|
3 | Samba utility functions
|
---|
4 | Copyright (C) Andrew Tridgell 1992-1998
|
---|
5 | Copyright (C) Tim Potter 2000-2001
|
---|
6 | Copyright (C) Jeremy Allison 1992-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 | #include "includes.h"
|
---|
23 |
|
---|
24 | /****************************************************************************
|
---|
25 | Return true if a string could be an IPv4 address.
|
---|
26 | ****************************************************************************/
|
---|
27 |
|
---|
28 | bool is_ipaddress_v4(const char *str)
|
---|
29 | {
|
---|
30 | int ret = -1;
|
---|
31 | struct in_addr dest;
|
---|
32 |
|
---|
33 | ret = inet_pton(AF_INET, str, &dest);
|
---|
34 | if (ret > 0) {
|
---|
35 | return true;
|
---|
36 | }
|
---|
37 | return false;
|
---|
38 | }
|
---|
39 |
|
---|
40 | /****************************************************************************
|
---|
41 | Return true if a string could be an IPv4 or IPv6 address.
|
---|
42 | ****************************************************************************/
|
---|
43 |
|
---|
44 | bool is_ipaddress(const char *str)
|
---|
45 | {
|
---|
46 | #if defined(HAVE_IPV6)
|
---|
47 | int ret = -1;
|
---|
48 |
|
---|
49 | if (strchr_m(str, ':')) {
|
---|
50 | char addr[INET6_ADDRSTRLEN];
|
---|
51 | struct in6_addr dest6;
|
---|
52 | const char *sp = str;
|
---|
53 | char *p = strchr_m(str, '%');
|
---|
54 |
|
---|
55 | /*
|
---|
56 | * Cope with link-local.
|
---|
57 | * This is IP:v6:addr%ifname.
|
---|
58 | */
|
---|
59 |
|
---|
60 | if (p && (p > str) && (if_nametoindex(p+1) != 0)) {
|
---|
61 | strlcpy(addr, str,
|
---|
62 | MIN(PTR_DIFF(p,str)+1,
|
---|
63 | sizeof(addr)));
|
---|
64 | sp = addr;
|
---|
65 | }
|
---|
66 | ret = inet_pton(AF_INET6, sp, &dest6);
|
---|
67 | if (ret > 0) {
|
---|
68 | return true;
|
---|
69 | }
|
---|
70 | }
|
---|
71 | #endif
|
---|
72 | return is_ipaddress_v4(str);
|
---|
73 | }
|
---|
74 |
|
---|
75 | /****************************************************************************
|
---|
76 | Is a sockaddr_storage a broadcast address ?
|
---|
77 | ****************************************************************************/
|
---|
78 |
|
---|
79 | bool is_broadcast_addr(const struct sockaddr_storage *pss)
|
---|
80 | {
|
---|
81 | #if defined(HAVE_IPV6)
|
---|
82 | if (pss->ss_family == AF_INET6) {
|
---|
83 | const struct in6_addr *sin6 =
|
---|
84 | &((const struct sockaddr_in6 *)pss)->sin6_addr;
|
---|
85 | return IN6_IS_ADDR_MULTICAST(sin6);
|
---|
86 | }
|
---|
87 | #endif
|
---|
88 | if (pss->ss_family == AF_INET) {
|
---|
89 | uint32_t addr =
|
---|
90 | ntohl(((const struct sockaddr_in *)pss)->sin_addr.s_addr);
|
---|
91 | return addr == INADDR_BROADCAST;
|
---|
92 | }
|
---|
93 | return false;
|
---|
94 | }
|
---|
95 |
|
---|
96 | /*******************************************************************
|
---|
97 | Wrap getaddrinfo...
|
---|
98 | ******************************************************************/
|
---|
99 |
|
---|
100 | static bool interpret_string_addr_internal(struct addrinfo **ppres,
|
---|
101 | const char *str, int flags)
|
---|
102 | {
|
---|
103 | int ret;
|
---|
104 | struct addrinfo hints;
|
---|
105 |
|
---|
106 | memset(&hints, '\0', sizeof(hints));
|
---|
107 | /* By default make sure it supports TCP. */
|
---|
108 | hints.ai_socktype = SOCK_STREAM;
|
---|
109 | hints.ai_flags = flags;
|
---|
110 |
|
---|
111 | /* Linux man page on getaddinfo() says port will be
|
---|
112 | uninitialized when service string in NULL */
|
---|
113 |
|
---|
114 | ret = getaddrinfo(str, NULL,
|
---|
115 | &hints,
|
---|
116 | ppres);
|
---|
117 |
|
---|
118 | if (ret) {
|
---|
119 | DEBUG(3,("interpret_string_addr_internal: getaddrinfo failed "
|
---|
120 | "for name %s [%s]\n",
|
---|
121 | str,
|
---|
122 | gai_strerror(ret) ));
|
---|
123 | return false;
|
---|
124 | }
|
---|
125 | return true;
|
---|
126 | }
|
---|
127 |
|
---|
128 | /****************************************************************************
|
---|
129 | Interpret an internet address or name into an IP address in 4 byte form.
|
---|
130 | RETURNS IN NETWORK BYTE ORDER (big endian).
|
---|
131 | ****************************************************************************/
|
---|
132 |
|
---|
133 | uint32 interpret_addr(const char *str)
|
---|
134 | {
|
---|
135 | uint32 ret;
|
---|
136 |
|
---|
137 | /* If it's in the form of an IP address then
|
---|
138 | * get the lib to interpret it */
|
---|
139 | if (is_ipaddress_v4(str)) {
|
---|
140 | struct in_addr dest;
|
---|
141 |
|
---|
142 | if (inet_pton(AF_INET, str, &dest) <= 0) {
|
---|
143 | /* Error - this shouldn't happen ! */
|
---|
144 | DEBUG(0,("interpret_addr: inet_pton failed "
|
---|
145 | "host %s\n",
|
---|
146 | str));
|
---|
147 | return 0;
|
---|
148 | }
|
---|
149 | ret = dest.s_addr; /* NETWORK BYTE ORDER ! */
|
---|
150 | } else {
|
---|
151 | /* Otherwise assume it's a network name of some sort and use
|
---|
152 | getadddrinfo. */
|
---|
153 | struct addrinfo *res = NULL;
|
---|
154 | struct addrinfo *res_list = NULL;
|
---|
155 | if (!interpret_string_addr_internal(&res_list,
|
---|
156 | str,
|
---|
157 | AI_ADDRCONFIG)) {
|
---|
158 | DEBUG(3,("interpret_addr: Unknown host. %s\n",str));
|
---|
159 | return 0;
|
---|
160 | }
|
---|
161 |
|
---|
162 | /* Find the first IPv4 address. */
|
---|
163 | for (res = res_list; res; res = res->ai_next) {
|
---|
164 | if (res->ai_family != AF_INET) {
|
---|
165 | continue;
|
---|
166 | }
|
---|
167 | if (res->ai_addr == NULL) {
|
---|
168 | continue;
|
---|
169 | }
|
---|
170 | break;
|
---|
171 | }
|
---|
172 | if(res == NULL) {
|
---|
173 | DEBUG(3,("interpret_addr: host address is "
|
---|
174 | "invalid for host %s\n",str));
|
---|
175 | if (res_list) {
|
---|
176 | freeaddrinfo(res_list);
|
---|
177 | }
|
---|
178 | return 0;
|
---|
179 | }
|
---|
180 | putip((char *)&ret,
|
---|
181 | &((struct sockaddr_in *)res->ai_addr)->sin_addr.s_addr);
|
---|
182 | if (res_list) {
|
---|
183 | freeaddrinfo(res_list);
|
---|
184 | }
|
---|
185 | }
|
---|
186 |
|
---|
187 | /* This is so bogus - all callers need fixing... JRA. */
|
---|
188 | if (ret == (uint32)-1) {
|
---|
189 | return 0;
|
---|
190 | }
|
---|
191 |
|
---|
192 | return ret;
|
---|
193 | }
|
---|
194 |
|
---|
195 | /*******************************************************************
|
---|
196 | A convenient addition to interpret_addr().
|
---|
197 | ******************************************************************/
|
---|
198 |
|
---|
199 | struct in_addr *interpret_addr2(struct in_addr *ip, const char *str)
|
---|
200 | {
|
---|
201 | uint32 a = interpret_addr(str);
|
---|
202 | ip->s_addr = a;
|
---|
203 | return ip;
|
---|
204 | }
|
---|
205 |
|
---|
206 | /*******************************************************************
|
---|
207 | Map a text hostname or IP address (IPv4 or IPv6) into a
|
---|
208 | struct sockaddr_storage.
|
---|
209 | ******************************************************************/
|
---|
210 |
|
---|
211 | bool interpret_string_addr(struct sockaddr_storage *pss,
|
---|
212 | const char *str,
|
---|
213 | int flags)
|
---|
214 | {
|
---|
215 | struct addrinfo *res = NULL;
|
---|
216 | #if defined(HAVE_IPV6)
|
---|
217 | char addr[INET6_ADDRSTRLEN];
|
---|
218 | unsigned int scope_id = 0;
|
---|
219 |
|
---|
220 | if (strchr_m(str, ':')) {
|
---|
221 | char *p = strchr_m(str, '%');
|
---|
222 |
|
---|
223 | /*
|
---|
224 | * Cope with link-local.
|
---|
225 | * This is IP:v6:addr%ifname.
|
---|
226 | */
|
---|
227 |
|
---|
228 | if (p && (p > str) && ((scope_id = if_nametoindex(p+1)) != 0)) {
|
---|
229 | strlcpy(addr, str,
|
---|
230 | MIN(PTR_DIFF(p,str)+1,
|
---|
231 | sizeof(addr)));
|
---|
232 | str = addr;
|
---|
233 | }
|
---|
234 | }
|
---|
235 | #endif
|
---|
236 |
|
---|
237 | zero_sockaddr(pss);
|
---|
238 |
|
---|
239 | if (!interpret_string_addr_internal(&res, str, flags|AI_ADDRCONFIG)) {
|
---|
240 | return false;
|
---|
241 | }
|
---|
242 | if (!res) {
|
---|
243 | return false;
|
---|
244 | }
|
---|
245 | /* Copy the first sockaddr. */
|
---|
246 | memcpy(pss, res->ai_addr, res->ai_addrlen);
|
---|
247 |
|
---|
248 | #if defined(HAVE_IPV6)
|
---|
249 | if (pss->ss_family == AF_INET6 && scope_id) {
|
---|
250 | struct sockaddr_in6 *ps6 = (struct sockaddr_in6 *)pss;
|
---|
251 | if (IN6_IS_ADDR_LINKLOCAL(&ps6->sin6_addr) &&
|
---|
252 | ps6->sin6_scope_id == 0) {
|
---|
253 | ps6->sin6_scope_id = scope_id;
|
---|
254 | }
|
---|
255 | }
|
---|
256 | #endif
|
---|
257 |
|
---|
258 | freeaddrinfo(res);
|
---|
259 | return true;
|
---|
260 | }
|
---|
261 |
|
---|
262 | /*******************************************************************
|
---|
263 | Check if an IPv7 is 127.0.0.1
|
---|
264 | ******************************************************************/
|
---|
265 |
|
---|
266 | bool is_loopback_ip_v4(struct in_addr ip)
|
---|
267 | {
|
---|
268 | struct in_addr a;
|
---|
269 | a.s_addr = htonl(INADDR_LOOPBACK);
|
---|
270 | return(ip.s_addr == a.s_addr);
|
---|
271 | }
|
---|
272 |
|
---|
273 | /*******************************************************************
|
---|
274 | Check if a struct sockaddr_storage is the loopback address.
|
---|
275 | ******************************************************************/
|
---|
276 |
|
---|
277 | bool is_loopback_addr(const struct sockaddr_storage *pss)
|
---|
278 | {
|
---|
279 | #if defined(HAVE_IPV6)
|
---|
280 | if (pss->ss_family == AF_INET6) {
|
---|
281 | struct in6_addr *pin6 =
|
---|
282 | &((struct sockaddr_in6 *)pss)->sin6_addr;
|
---|
283 | return IN6_IS_ADDR_LOOPBACK(pin6);
|
---|
284 | }
|
---|
285 | #endif
|
---|
286 | if (pss->ss_family == AF_INET) {
|
---|
287 | struct in_addr *pin = &((struct sockaddr_in *)pss)->sin_addr;
|
---|
288 | return is_loopback_ip_v4(*pin);
|
---|
289 | }
|
---|
290 | return false;
|
---|
291 | }
|
---|
292 |
|
---|
293 | /*******************************************************************
|
---|
294 | Check if an IPv4 is 0.0.0.0.
|
---|
295 | ******************************************************************/
|
---|
296 |
|
---|
297 | bool is_zero_ip_v4(struct in_addr ip)
|
---|
298 | {
|
---|
299 | uint32 a;
|
---|
300 | putip((char *)&a,(char *)&ip);
|
---|
301 | return(a == 0);
|
---|
302 | }
|
---|
303 |
|
---|
304 | /*******************************************************************
|
---|
305 | Check if a struct sockaddr_storage has an unspecified address.
|
---|
306 | ******************************************************************/
|
---|
307 |
|
---|
308 | bool is_zero_addr(const struct sockaddr_storage *pss)
|
---|
309 | {
|
---|
310 | #if defined(HAVE_IPV6)
|
---|
311 | if (pss->ss_family == AF_INET6) {
|
---|
312 | struct in6_addr *pin6 =
|
---|
313 | &((struct sockaddr_in6 *)pss)->sin6_addr;
|
---|
314 | return IN6_IS_ADDR_UNSPECIFIED(pin6);
|
---|
315 | }
|
---|
316 | #endif
|
---|
317 | if (pss->ss_family == AF_INET) {
|
---|
318 | struct in_addr *pin = &((struct sockaddr_in *)pss)->sin_addr;
|
---|
319 | return is_zero_ip_v4(*pin);
|
---|
320 | }
|
---|
321 | return false;
|
---|
322 | }
|
---|
323 |
|
---|
324 | /*******************************************************************
|
---|
325 | Set an IP to 0.0.0.0.
|
---|
326 | ******************************************************************/
|
---|
327 |
|
---|
328 | void zero_ip_v4(struct in_addr *ip)
|
---|
329 | {
|
---|
330 | memset(ip, '\0', sizeof(struct in_addr));
|
---|
331 | }
|
---|
332 |
|
---|
333 | /*******************************************************************
|
---|
334 | Set an address to INADDR_ANY.
|
---|
335 | ******************************************************************/
|
---|
336 |
|
---|
337 | void zero_sockaddr(struct sockaddr_storage *pss)
|
---|
338 | {
|
---|
339 | memset(pss, '\0', sizeof(*pss));
|
---|
340 | /* Ensure we're at least a valid sockaddr-storage. */
|
---|
341 | pss->ss_family = AF_INET;
|
---|
342 | }
|
---|
343 |
|
---|
344 | /*******************************************************************
|
---|
345 | Are two IPs on the same subnet - IPv4 version ?
|
---|
346 | ********************************************************************/
|
---|
347 |
|
---|
348 | bool same_net_v4(struct in_addr ip1,struct in_addr ip2,struct in_addr mask)
|
---|
349 | {
|
---|
350 | uint32 net1,net2,nmask;
|
---|
351 |
|
---|
352 | nmask = ntohl(mask.s_addr);
|
---|
353 | net1 = ntohl(ip1.s_addr);
|
---|
354 | net2 = ntohl(ip2.s_addr);
|
---|
355 |
|
---|
356 | return((net1 & nmask) == (net2 & nmask));
|
---|
357 | }
|
---|
358 |
|
---|
359 | /*******************************************************************
|
---|
360 | Convert an IPv4 struct in_addr to a struct sockaddr_storage.
|
---|
361 | ********************************************************************/
|
---|
362 |
|
---|
363 | void in_addr_to_sockaddr_storage(struct sockaddr_storage *ss,
|
---|
364 | struct in_addr ip)
|
---|
365 | {
|
---|
366 | struct sockaddr_in *sa = (struct sockaddr_in *)ss;
|
---|
367 | memset(ss, '\0', sizeof(*ss));
|
---|
368 | sa->sin_family = AF_INET;
|
---|
369 | sa->sin_addr = ip;
|
---|
370 | }
|
---|
371 |
|
---|
372 | #if defined(HAVE_IPV6)
|
---|
373 | /*******************************************************************
|
---|
374 | Convert an IPv6 struct in_addr to a struct sockaddr_storage.
|
---|
375 | ********************************************************************/
|
---|
376 |
|
---|
377 | void in6_addr_to_sockaddr_storage(struct sockaddr_storage *ss,
|
---|
378 | struct in6_addr ip)
|
---|
379 | {
|
---|
380 | struct sockaddr_in6 *sa = (struct sockaddr_in6 *)ss;
|
---|
381 | memset(ss, '\0', sizeof(*ss));
|
---|
382 | sa->sin6_family = AF_INET6;
|
---|
383 | sa->sin6_addr = ip;
|
---|
384 | }
|
---|
385 | #endif
|
---|
386 |
|
---|
387 | /*******************************************************************
|
---|
388 | Are two IPs on the same subnet?
|
---|
389 | ********************************************************************/
|
---|
390 |
|
---|
391 | bool same_net(const struct sockaddr_storage *ip1,
|
---|
392 | const struct sockaddr_storage *ip2,
|
---|
393 | const struct sockaddr_storage *mask)
|
---|
394 | {
|
---|
395 | if (ip1->ss_family != ip2->ss_family) {
|
---|
396 | /* Never on the same net. */
|
---|
397 | return false;
|
---|
398 | }
|
---|
399 |
|
---|
400 | #if defined(HAVE_IPV6)
|
---|
401 | if (ip1->ss_family == AF_INET6) {
|
---|
402 | struct sockaddr_in6 ip1_6 = *(struct sockaddr_in6 *)ip1;
|
---|
403 | struct sockaddr_in6 ip2_6 = *(struct sockaddr_in6 *)ip2;
|
---|
404 | struct sockaddr_in6 mask_6 = *(struct sockaddr_in6 *)mask;
|
---|
405 | char *p1 = (char *)&ip1_6.sin6_addr;
|
---|
406 | char *p2 = (char *)&ip2_6.sin6_addr;
|
---|
407 | char *m = (char *)&mask_6.sin6_addr;
|
---|
408 | int i;
|
---|
409 |
|
---|
410 | for (i = 0; i < sizeof(struct in6_addr); i++) {
|
---|
411 | *p1++ &= *m;
|
---|
412 | *p2++ &= *m;
|
---|
413 | m++;
|
---|
414 | }
|
---|
415 | return (memcmp(&ip1_6.sin6_addr,
|
---|
416 | &ip2_6.sin6_addr,
|
---|
417 | sizeof(struct in6_addr)) == 0);
|
---|
418 | }
|
---|
419 | #endif
|
---|
420 | if (ip1->ss_family == AF_INET) {
|
---|
421 | return same_net_v4(((const struct sockaddr_in *)ip1)->sin_addr,
|
---|
422 | ((const struct sockaddr_in *)ip2)->sin_addr,
|
---|
423 | ((const struct sockaddr_in *)mask)->sin_addr);
|
---|
424 | }
|
---|
425 | return false;
|
---|
426 | }
|
---|
427 |
|
---|
428 | /*******************************************************************
|
---|
429 | Are two sockaddr_storage's the same family and address ? Ignore port etc.
|
---|
430 | ********************************************************************/
|
---|
431 |
|
---|
432 | bool sockaddr_equal(const struct sockaddr_storage *ip1,
|
---|
433 | const struct sockaddr_storage *ip2)
|
---|
434 | {
|
---|
435 | if (ip1->ss_family != ip2->ss_family) {
|
---|
436 | /* Never the same. */
|
---|
437 | return false;
|
---|
438 | }
|
---|
439 |
|
---|
440 | #if defined(HAVE_IPV6)
|
---|
441 | if (ip1->ss_family == AF_INET6) {
|
---|
442 | return (memcmp(&((const struct sockaddr_in6 *)ip1)->sin6_addr,
|
---|
443 | &((const struct sockaddr_in6 *)ip2)->sin6_addr,
|
---|
444 | sizeof(struct in6_addr)) == 0);
|
---|
445 | }
|
---|
446 | #endif
|
---|
447 | if (ip1->ss_family == AF_INET) {
|
---|
448 | return (memcmp(&((const struct sockaddr_in *)ip1)->sin_addr,
|
---|
449 | &((const struct sockaddr_in *)ip2)->sin_addr,
|
---|
450 | sizeof(struct in_addr)) == 0);
|
---|
451 | }
|
---|
452 | return false;
|
---|
453 | }
|
---|
454 |
|
---|
455 | /****************************************************************************
|
---|
456 | Is an IP address the INADDR_ANY or in6addr_any value ?
|
---|
457 | ****************************************************************************/
|
---|
458 |
|
---|
459 | bool is_address_any(const struct sockaddr_storage *psa)
|
---|
460 | {
|
---|
461 | #if defined(HAVE_IPV6)
|
---|
462 | if (psa->ss_family == AF_INET6) {
|
---|
463 | struct sockaddr_in6 *si6 = (struct sockaddr_in6 *)psa;
|
---|
464 | if (memcmp(&in6addr_any,
|
---|
465 | &si6->sin6_addr,
|
---|
466 | sizeof(in6addr_any)) == 0) {
|
---|
467 | return true;
|
---|
468 | }
|
---|
469 | return false;
|
---|
470 | }
|
---|
471 | #endif
|
---|
472 | if (psa->ss_family == AF_INET) {
|
---|
473 | struct sockaddr_in *si = (struct sockaddr_in *)psa;
|
---|
474 | if (si->sin_addr.s_addr == INADDR_ANY) {
|
---|
475 | return true;
|
---|
476 | }
|
---|
477 | return false;
|
---|
478 | }
|
---|
479 | return false;
|
---|
480 | }
|
---|
481 |
|
---|
482 | /****************************************************************************
|
---|
483 | Get a port number in host byte order from a sockaddr_storage.
|
---|
484 | ****************************************************************************/
|
---|
485 |
|
---|
486 | uint16_t get_sockaddr_port(const struct sockaddr_storage *pss)
|
---|
487 | {
|
---|
488 | uint16_t port = 0;
|
---|
489 |
|
---|
490 | if (pss->ss_family != AF_INET) {
|
---|
491 | #if defined(HAVE_IPV6)
|
---|
492 | /* IPv6 */
|
---|
493 | const struct sockaddr_in6 *sa6 =
|
---|
494 | (const struct sockaddr_in6 *)pss;
|
---|
495 | port = ntohs(sa6->sin6_port);
|
---|
496 | #endif
|
---|
497 | } else {
|
---|
498 | const struct sockaddr_in *sa =
|
---|
499 | (const struct sockaddr_in *)pss;
|
---|
500 | port = ntohs(sa->sin_port);
|
---|
501 | }
|
---|
502 | return port;
|
---|
503 | }
|
---|
504 |
|
---|
505 | /****************************************************************************
|
---|
506 | Print out an IPv4 or IPv6 address from a struct sockaddr_storage.
|
---|
507 | ****************************************************************************/
|
---|
508 |
|
---|
509 | static char *print_sockaddr_len(char *dest,
|
---|
510 | size_t destlen,
|
---|
511 | const struct sockaddr_storage *psa,
|
---|
512 | socklen_t psalen)
|
---|
513 | {
|
---|
514 | if (destlen > 0) {
|
---|
515 | dest[0] = '\0';
|
---|
516 | }
|
---|
517 | (void)sys_getnameinfo((const struct sockaddr *)psa,
|
---|
518 | psalen,
|
---|
519 | dest, destlen,
|
---|
520 | NULL, 0,
|
---|
521 | NI_NUMERICHOST);
|
---|
522 | return dest;
|
---|
523 | }
|
---|
524 |
|
---|
525 | /****************************************************************************
|
---|
526 | Print out an IPv4 or IPv6 address from a struct sockaddr_storage.
|
---|
527 | ****************************************************************************/
|
---|
528 |
|
---|
529 | char *print_sockaddr(char *dest,
|
---|
530 | size_t destlen,
|
---|
531 | const struct sockaddr_storage *psa)
|
---|
532 | {
|
---|
533 | return print_sockaddr_len(dest, destlen, psa,
|
---|
534 | sizeof(struct sockaddr_storage));
|
---|
535 | }
|
---|
536 |
|
---|
537 | /****************************************************************************
|
---|
538 | Print out a canonical IPv4 or IPv6 address from a struct sockaddr_storage.
|
---|
539 | ****************************************************************************/
|
---|
540 |
|
---|
541 | char *print_canonical_sockaddr(TALLOC_CTX *ctx,
|
---|
542 | const struct sockaddr_storage *pss)
|
---|
543 | {
|
---|
544 | char addr[INET6_ADDRSTRLEN];
|
---|
545 | char *dest = NULL;
|
---|
546 | int ret;
|
---|
547 |
|
---|
548 | /* Linux getnameinfo() man pages says port is unitialized if
|
---|
549 | service name is NULL. */
|
---|
550 |
|
---|
551 | ret = sys_getnameinfo((const struct sockaddr *)pss,
|
---|
552 | sizeof(struct sockaddr_storage),
|
---|
553 | addr, sizeof(addr),
|
---|
554 | NULL, 0,
|
---|
555 | NI_NUMERICHOST);
|
---|
556 | if (ret != 0) {
|
---|
557 | return NULL;
|
---|
558 | }
|
---|
559 |
|
---|
560 | if (pss->ss_family != AF_INET) {
|
---|
561 | #if defined(HAVE_IPV6)
|
---|
562 | dest = talloc_asprintf(ctx, "[%s]", addr);
|
---|
563 | #else
|
---|
564 | return NULL;
|
---|
565 | #endif
|
---|
566 | } else {
|
---|
567 | dest = talloc_asprintf(ctx, "%s", addr);
|
---|
568 | }
|
---|
569 |
|
---|
570 | return dest;
|
---|
571 | }
|
---|
572 |
|
---|
573 | /****************************************************************************
|
---|
574 | Return the string of an IP address (IPv4 or IPv6).
|
---|
575 | ****************************************************************************/
|
---|
576 |
|
---|
577 | static const char *get_socket_addr(int fd, char *addr_buf, size_t addr_len)
|
---|
578 | {
|
---|
579 | struct sockaddr_storage sa;
|
---|
580 | socklen_t length = sizeof(sa);
|
---|
581 |
|
---|
582 | /* Ok, returning a hard coded IPv4 address
|
---|
583 | * is bogus, but it's just as bogus as a
|
---|
584 | * zero IPv6 address. No good choice here.
|
---|
585 | */
|
---|
586 |
|
---|
587 | strlcpy(addr_buf, "0.0.0.0", addr_len);
|
---|
588 |
|
---|
589 | if (fd == -1) {
|
---|
590 | return addr_buf;
|
---|
591 | }
|
---|
592 |
|
---|
593 | if (getsockname(fd, (struct sockaddr *)&sa, &length) < 0) {
|
---|
594 | DEBUG(0,("getsockname failed. Error was %s\n",
|
---|
595 | strerror(errno) ));
|
---|
596 | return addr_buf;
|
---|
597 | }
|
---|
598 |
|
---|
599 | return print_sockaddr_len(addr_buf, addr_len, &sa, length);
|
---|
600 | }
|
---|
601 |
|
---|
602 | #if 0
|
---|
603 | /* Not currently used. JRA. */
|
---|
604 | /****************************************************************************
|
---|
605 | Return the port number we've bound to on a socket.
|
---|
606 | ****************************************************************************/
|
---|
607 |
|
---|
608 | static int get_socket_port(int fd)
|
---|
609 | {
|
---|
610 | struct sockaddr_storage sa;
|
---|
611 | socklen_t length = sizeof(sa);
|
---|
612 |
|
---|
613 | if (fd == -1) {
|
---|
614 | return -1;
|
---|
615 | }
|
---|
616 |
|
---|
617 | if (getsockname(fd, (struct sockaddr *)&sa, &length) < 0) {
|
---|
618 | DEBUG(0,("getpeername failed. Error was %s\n",
|
---|
619 | strerror(errno) ));
|
---|
620 | return -1;
|
---|
621 | }
|
---|
622 |
|
---|
623 | #if defined(HAVE_IPV6)
|
---|
624 | if (sa.ss_family == AF_INET6) {
|
---|
625 | return ntohs(((struct sockaddr_in6 *)&sa)->sin6_port);
|
---|
626 | }
|
---|
627 | #endif
|
---|
628 | if (sa.ss_family == AF_INET) {
|
---|
629 | return ntohs(((struct sockaddr_in *)&sa)->sin_port);
|
---|
630 | }
|
---|
631 | return -1;
|
---|
632 | }
|
---|
633 | #endif
|
---|
634 |
|
---|
635 | void set_sockaddr_port(struct sockaddr_storage *psa, uint16 port)
|
---|
636 | {
|
---|
637 | #if defined(HAVE_IPV6)
|
---|
638 | if (psa->ss_family == AF_INET6) {
|
---|
639 | ((struct sockaddr_in6 *)psa)->sin6_port = htons(port);
|
---|
640 | }
|
---|
641 | #endif
|
---|
642 | if (psa->ss_family == AF_INET) {
|
---|
643 | ((struct sockaddr_in *)psa)->sin_port = htons(port);
|
---|
644 | }
|
---|
645 | }
|
---|
646 |
|
---|
647 | const char *client_name(int fd)
|
---|
648 | {
|
---|
649 | return get_peer_name(fd,false);
|
---|
650 | }
|
---|
651 |
|
---|
652 | const char *client_addr(int fd, char *addr, size_t addrlen)
|
---|
653 | {
|
---|
654 | return get_peer_addr(fd,addr,addrlen);
|
---|
655 | }
|
---|
656 |
|
---|
657 | const char *client_socket_addr(int fd, char *addr, size_t addr_len)
|
---|
658 | {
|
---|
659 | return get_socket_addr(fd, addr, addr_len);
|
---|
660 | }
|
---|
661 |
|
---|
662 | #if 0
|
---|
663 | /* Not currently used. JRA. */
|
---|
664 | int client_socket_port(int fd)
|
---|
665 | {
|
---|
666 | return get_socket_port(fd);
|
---|
667 | }
|
---|
668 | #endif
|
---|
669 |
|
---|
670 | /****************************************************************************
|
---|
671 | Accessor functions to make thread-safe code easier later...
|
---|
672 | ****************************************************************************/
|
---|
673 |
|
---|
674 | void set_smb_read_error(enum smb_read_errors *pre,
|
---|
675 | enum smb_read_errors newerr)
|
---|
676 | {
|
---|
677 | if (pre) {
|
---|
678 | *pre = newerr;
|
---|
679 | }
|
---|
680 | }
|
---|
681 |
|
---|
682 | void cond_set_smb_read_error(enum smb_read_errors *pre,
|
---|
683 | enum smb_read_errors newerr)
|
---|
684 | {
|
---|
685 | if (pre && *pre == SMB_READ_OK) {
|
---|
686 | *pre = newerr;
|
---|
687 | }
|
---|
688 | }
|
---|
689 |
|
---|
690 | /****************************************************************************
|
---|
691 | Determine if a file descriptor is in fact a socket.
|
---|
692 | ****************************************************************************/
|
---|
693 |
|
---|
694 | bool is_a_socket(int fd)
|
---|
695 | {
|
---|
696 | int v;
|
---|
697 | socklen_t l;
|
---|
698 | l = sizeof(int);
|
---|
699 | return(getsockopt(fd, SOL_SOCKET, SO_TYPE, (char *)&v, &l) == 0);
|
---|
700 | }
|
---|
701 |
|
---|
702 | enum SOCK_OPT_TYPES {OPT_BOOL,OPT_INT,OPT_ON};
|
---|
703 |
|
---|
704 | typedef struct smb_socket_option {
|
---|
705 | const char *name;
|
---|
706 | int level;
|
---|
707 | int option;
|
---|
708 | int value;
|
---|
709 | int opttype;
|
---|
710 | } smb_socket_option;
|
---|
711 |
|
---|
712 | static const smb_socket_option socket_options[] = {
|
---|
713 | {"SO_KEEPALIVE", SOL_SOCKET, SO_KEEPALIVE, 0, OPT_BOOL},
|
---|
714 | {"SO_REUSEADDR", SOL_SOCKET, SO_REUSEADDR, 0, OPT_BOOL},
|
---|
715 | {"SO_BROADCAST", SOL_SOCKET, SO_BROADCAST, 0, OPT_BOOL},
|
---|
716 | #ifdef TCP_NODELAY
|
---|
717 | {"TCP_NODELAY", IPPROTO_TCP, TCP_NODELAY, 0, OPT_BOOL},
|
---|
718 | #endif
|
---|
719 | #ifdef TCP_KEEPCNT
|
---|
720 | {"TCP_KEEPCNT", IPPROTO_TCP, TCP_KEEPCNT, 0, OPT_INT},
|
---|
721 | #endif
|
---|
722 | #ifdef TCP_KEEPIDLE
|
---|
723 | {"TCP_KEEPIDLE", IPPROTO_TCP, TCP_KEEPIDLE, 0, OPT_INT},
|
---|
724 | #endif
|
---|
725 | #ifdef TCP_KEEPINTVL
|
---|
726 | {"TCP_KEEPINTVL", IPPROTO_TCP, TCP_KEEPINTVL, 0, OPT_INT},
|
---|
727 | #endif
|
---|
728 | #ifdef IPTOS_LOWDELAY
|
---|
729 | {"IPTOS_LOWDELAY", IPPROTO_IP, IP_TOS, IPTOS_LOWDELAY, OPT_ON},
|
---|
730 | #endif
|
---|
731 | #ifdef IPTOS_THROUGHPUT
|
---|
732 | {"IPTOS_THROUGHPUT", IPPROTO_IP, IP_TOS, IPTOS_THROUGHPUT, OPT_ON},
|
---|
733 | #endif
|
---|
734 | #ifdef SO_REUSEPORT
|
---|
735 | {"SO_REUSEPORT", SOL_SOCKET, SO_REUSEPORT, 0, OPT_BOOL},
|
---|
736 | #endif
|
---|
737 | #ifdef SO_SNDBUF
|
---|
738 | {"SO_SNDBUF", SOL_SOCKET, SO_SNDBUF, 0, OPT_INT},
|
---|
739 | #endif
|
---|
740 | #ifdef SO_RCVBUF
|
---|
741 | {"SO_RCVBUF", SOL_SOCKET, SO_RCVBUF, 0, OPT_INT},
|
---|
742 | #endif
|
---|
743 | #ifdef SO_SNDLOWAT
|
---|
744 | {"SO_SNDLOWAT", SOL_SOCKET, SO_SNDLOWAT, 0, OPT_INT},
|
---|
745 | #endif
|
---|
746 | #ifdef SO_RCVLOWAT
|
---|
747 | {"SO_RCVLOWAT", SOL_SOCKET, SO_RCVLOWAT, 0, OPT_INT},
|
---|
748 | #endif
|
---|
749 | #ifdef SO_SNDTIMEO
|
---|
750 | {"SO_SNDTIMEO", SOL_SOCKET, SO_SNDTIMEO, 0, OPT_INT},
|
---|
751 | #endif
|
---|
752 | #ifdef SO_RCVTIMEO
|
---|
753 | {"SO_RCVTIMEO", SOL_SOCKET, SO_RCVTIMEO, 0, OPT_INT},
|
---|
754 | #endif
|
---|
755 | #ifdef TCP_FASTACK
|
---|
756 | {"TCP_FASTACK", IPPROTO_TCP, TCP_FASTACK, 0, OPT_INT},
|
---|
757 | #endif
|
---|
758 | {NULL,0,0,0,0}};
|
---|
759 |
|
---|
760 | /****************************************************************************
|
---|
761 | Print socket options.
|
---|
762 | ****************************************************************************/
|
---|
763 |
|
---|
764 | static void print_socket_options(int s)
|
---|
765 | {
|
---|
766 | int value;
|
---|
767 | socklen_t vlen = 4;
|
---|
768 | const smb_socket_option *p = &socket_options[0];
|
---|
769 |
|
---|
770 | /* wrapped in if statement to prevent streams
|
---|
771 | * leak in SCO Openserver 5.0 */
|
---|
772 | /* reported on samba-technical --jerry */
|
---|
773 | if ( DEBUGLEVEL >= 5 ) {
|
---|
774 | for (; p->name != NULL; p++) {
|
---|
775 | if (getsockopt(s, p->level, p->option,
|
---|
776 | (void *)&value, &vlen) == -1) {
|
---|
777 | DEBUG(5,("Could not test socket option %s.\n",
|
---|
778 | p->name));
|
---|
779 | } else {
|
---|
780 | DEBUG(5,("socket option %s = %d\n",
|
---|
781 | p->name,value));
|
---|
782 | }
|
---|
783 | }
|
---|
784 | }
|
---|
785 | }
|
---|
786 |
|
---|
787 | /****************************************************************************
|
---|
788 | Set user socket options.
|
---|
789 | ****************************************************************************/
|
---|
790 |
|
---|
791 | void set_socket_options(int fd, const char *options)
|
---|
792 | {
|
---|
793 | TALLOC_CTX *ctx = talloc_stackframe();
|
---|
794 | char *tok;
|
---|
795 |
|
---|
796 | while (next_token_talloc(ctx, &options, &tok," \t,")) {
|
---|
797 | int ret=0,i;
|
---|
798 | int value = 1;
|
---|
799 | char *p;
|
---|
800 | bool got_value = false;
|
---|
801 |
|
---|
802 | if ((p = strchr_m(tok,'='))) {
|
---|
803 | *p = 0;
|
---|
804 | value = atoi(p+1);
|
---|
805 | got_value = true;
|
---|
806 | }
|
---|
807 |
|
---|
808 | for (i=0;socket_options[i].name;i++)
|
---|
809 | if (strequal(socket_options[i].name,tok))
|
---|
810 | break;
|
---|
811 |
|
---|
812 | if (!socket_options[i].name) {
|
---|
813 | DEBUG(0,("Unknown socket option %s\n",tok));
|
---|
814 | continue;
|
---|
815 | }
|
---|
816 |
|
---|
817 | switch (socket_options[i].opttype) {
|
---|
818 | case OPT_BOOL:
|
---|
819 | case OPT_INT:
|
---|
820 | ret = setsockopt(fd,socket_options[i].level,
|
---|
821 | socket_options[i].option,
|
---|
822 | (char *)&value,sizeof(int));
|
---|
823 | break;
|
---|
824 |
|
---|
825 | case OPT_ON:
|
---|
826 | if (got_value)
|
---|
827 | DEBUG(0,("syntax error - %s "
|
---|
828 | "does not take a value\n",tok));
|
---|
829 |
|
---|
830 | {
|
---|
831 | int on = socket_options[i].value;
|
---|
832 | ret = setsockopt(fd,socket_options[i].level,
|
---|
833 | socket_options[i].option,
|
---|
834 | (char *)&on,sizeof(int));
|
---|
835 | }
|
---|
836 | break;
|
---|
837 | }
|
---|
838 |
|
---|
839 | if (ret != 0) {
|
---|
840 | /* be aware that some systems like Solaris return
|
---|
841 | * EINVAL to a setsockopt() call when the client
|
---|
842 | * sent a RST previously - no need to worry */
|
---|
843 | DEBUG(2,("Failed to set socket option %s (Error %s)\n",
|
---|
844 | tok, strerror(errno) ));
|
---|
845 | }
|
---|
846 | }
|
---|
847 |
|
---|
848 | TALLOC_FREE(ctx);
|
---|
849 | print_socket_options(fd);
|
---|
850 | }
|
---|
851 |
|
---|
852 | /****************************************************************************
|
---|
853 | Read from a socket.
|
---|
854 | ****************************************************************************/
|
---|
855 |
|
---|
856 | ssize_t read_udp_v4_socket(int fd,
|
---|
857 | char *buf,
|
---|
858 | size_t len,
|
---|
859 | struct sockaddr_storage *psa)
|
---|
860 | {
|
---|
861 | ssize_t ret;
|
---|
862 | socklen_t socklen = sizeof(*psa);
|
---|
863 | struct sockaddr_in *si = (struct sockaddr_in *)psa;
|
---|
864 |
|
---|
865 | memset((char *)psa,'\0',socklen);
|
---|
866 |
|
---|
867 | ret = (ssize_t)sys_recvfrom(fd,buf,len,0,
|
---|
868 | (struct sockaddr *)psa,&socklen);
|
---|
869 | if (ret <= 0) {
|
---|
870 | /* Don't print a low debug error for a non-blocking socket. */
|
---|
871 | if (errno == EAGAIN) {
|
---|
872 | DEBUG(10,("read_udp_v4_socket: returned EAGAIN\n"));
|
---|
873 | } else {
|
---|
874 | DEBUG(2,("read_udp_v4_socket: failed. errno=%s\n",
|
---|
875 | strerror(errno)));
|
---|
876 | }
|
---|
877 | return 0;
|
---|
878 | }
|
---|
879 |
|
---|
880 | if (psa->ss_family != AF_INET) {
|
---|
881 | DEBUG(2,("read_udp_v4_socket: invalid address family %d "
|
---|
882 | "(not IPv4)\n", (int)psa->ss_family));
|
---|
883 | return 0;
|
---|
884 | }
|
---|
885 |
|
---|
886 | DEBUG(10,("read_udp_v4_socket: ip %s port %d read: %lu\n",
|
---|
887 | inet_ntoa(si->sin_addr),
|
---|
888 | si->sin_port,
|
---|
889 | (unsigned long)ret));
|
---|
890 |
|
---|
891 | return ret;
|
---|
892 | }
|
---|
893 |
|
---|
894 | /****************************************************************************
|
---|
895 | Read data from a socket with a timout in msec.
|
---|
896 | mincount = if timeout, minimum to read before returning
|
---|
897 | maxcount = number to be read.
|
---|
898 | time_out = timeout in milliseconds
|
---|
899 | ****************************************************************************/
|
---|
900 |
|
---|
901 | NTSTATUS read_socket_with_timeout(int fd, char *buf,
|
---|
902 | size_t mincnt, size_t maxcnt,
|
---|
903 | unsigned int time_out,
|
---|
904 | size_t *size_ret)
|
---|
905 | {
|
---|
906 | fd_set fds;
|
---|
907 | int selrtn;
|
---|
908 | ssize_t readret;
|
---|
909 | size_t nread = 0;
|
---|
910 | struct timeval timeout;
|
---|
911 | char addr[INET6_ADDRSTRLEN];
|
---|
912 |
|
---|
913 | /* just checking .... */
|
---|
914 | if (maxcnt <= 0)
|
---|
915 | return NT_STATUS_OK;
|
---|
916 |
|
---|
917 | /* Blocking read */
|
---|
918 | if (time_out == 0) {
|
---|
919 | if (mincnt == 0) {
|
---|
920 | mincnt = maxcnt;
|
---|
921 | }
|
---|
922 |
|
---|
923 | while (nread < mincnt) {
|
---|
924 | readret = sys_read(fd, buf + nread, maxcnt - nread);
|
---|
925 |
|
---|
926 | if (readret == 0) {
|
---|
927 | DEBUG(5,("read_socket_with_timeout: "
|
---|
928 | "blocking read. EOF from client.\n"));
|
---|
929 | return NT_STATUS_END_OF_FILE;
|
---|
930 | }
|
---|
931 |
|
---|
932 | if (readret == -1) {
|
---|
933 | if (fd == get_client_fd()) {
|
---|
934 | /* Try and give an error message
|
---|
935 | * saying what client failed. */
|
---|
936 | DEBUG(0,("read_socket_with_timeout: "
|
---|
937 | "client %s read error = %s.\n",
|
---|
938 | get_peer_addr(fd,addr,sizeof(addr)),
|
---|
939 | strerror(errno) ));
|
---|
940 | } else {
|
---|
941 | DEBUG(0,("read_socket_with_timeout: "
|
---|
942 | "read error = %s.\n",
|
---|
943 | strerror(errno) ));
|
---|
944 | }
|
---|
945 | return map_nt_error_from_unix(errno);
|
---|
946 | }
|
---|
947 | nread += readret;
|
---|
948 | }
|
---|
949 | goto done;
|
---|
950 | }
|
---|
951 |
|
---|
952 | /* Most difficult - timeout read */
|
---|
953 | /* If this is ever called on a disk file and
|
---|
954 | mincnt is greater then the filesize then
|
---|
955 | system performance will suffer severely as
|
---|
956 | select always returns true on disk files */
|
---|
957 |
|
---|
958 | /* Set initial timeout */
|
---|
959 | timeout.tv_sec = (time_t)(time_out / 1000);
|
---|
960 | timeout.tv_usec = (long)(1000 * (time_out % 1000));
|
---|
961 |
|
---|
962 | for (nread=0; nread < mincnt; ) {
|
---|
963 | if (fd < 0 || fd >= FD_SETSIZE) {
|
---|
964 | errno = EBADF;
|
---|
965 | return map_nt_error_from_unix(EBADF);
|
---|
966 | }
|
---|
967 |
|
---|
968 | FD_ZERO(&fds);
|
---|
969 | FD_SET(fd,&fds);
|
---|
970 |
|
---|
971 | selrtn = sys_select_intr(fd+1,&fds,NULL,NULL,&timeout);
|
---|
972 |
|
---|
973 | /* Check if error */
|
---|
974 | if (selrtn == -1) {
|
---|
975 | /* something is wrong. Maybe the socket is dead? */
|
---|
976 | if (fd == get_client_fd()) {
|
---|
977 | /* Try and give an error message saying
|
---|
978 | * what client failed. */
|
---|
979 | DEBUG(0,("read_socket_with_timeout: timeout "
|
---|
980 | "read for client %s. select error = %s.\n",
|
---|
981 | get_peer_addr(fd,addr,sizeof(addr)),
|
---|
982 | strerror(errno) ));
|
---|
983 | } else {
|
---|
984 | DEBUG(0,("read_socket_with_timeout: timeout "
|
---|
985 | "read. select error = %s.\n",
|
---|
986 | strerror(errno) ));
|
---|
987 | }
|
---|
988 | return map_nt_error_from_unix(errno);
|
---|
989 | }
|
---|
990 |
|
---|
991 | /* Did we timeout ? */
|
---|
992 | if (selrtn == 0) {
|
---|
993 | DEBUG(10,("read_socket_with_timeout: timeout read. "
|
---|
994 | "select timed out.\n"));
|
---|
995 | return NT_STATUS_IO_TIMEOUT;
|
---|
996 | }
|
---|
997 |
|
---|
998 | readret = sys_read(fd, buf+nread, maxcnt-nread);
|
---|
999 |
|
---|
1000 | if (readret == 0) {
|
---|
1001 | /* we got EOF on the file descriptor */
|
---|
1002 | DEBUG(5,("read_socket_with_timeout: timeout read. "
|
---|
1003 | "EOF from client.\n"));
|
---|
1004 | return NT_STATUS_END_OF_FILE;
|
---|
1005 | }
|
---|
1006 |
|
---|
1007 | if (readret == -1) {
|
---|
1008 | /* the descriptor is probably dead */
|
---|
1009 | if (fd == get_client_fd()) {
|
---|
1010 | /* Try and give an error message
|
---|
1011 | * saying what client failed. */
|
---|
1012 | DEBUG(0,("read_socket_with_timeout: timeout "
|
---|
1013 | "read to client %s. read error = %s.\n",
|
---|
1014 | get_peer_addr(fd,addr,sizeof(addr)),
|
---|
1015 | strerror(errno) ));
|
---|
1016 | } else {
|
---|
1017 | DEBUG(0,("read_socket_with_timeout: timeout "
|
---|
1018 | "read. read error = %s.\n",
|
---|
1019 | strerror(errno) ));
|
---|
1020 | }
|
---|
1021 | return map_nt_error_from_unix(errno);
|
---|
1022 | }
|
---|
1023 |
|
---|
1024 | nread += readret;
|
---|
1025 | }
|
---|
1026 |
|
---|
1027 | done:
|
---|
1028 | /* Return the number we got */
|
---|
1029 | if (size_ret) {
|
---|
1030 | *size_ret = nread;
|
---|
1031 | }
|
---|
1032 | return NT_STATUS_OK;
|
---|
1033 | }
|
---|
1034 |
|
---|
1035 | /****************************************************************************
|
---|
1036 | Read data from the client, reading exactly N bytes.
|
---|
1037 | ****************************************************************************/
|
---|
1038 |
|
---|
1039 | NTSTATUS read_data(int fd, char *buffer, size_t N)
|
---|
1040 | {
|
---|
1041 | return read_socket_with_timeout(fd, buffer, N, N, 0, NULL);
|
---|
1042 | }
|
---|
1043 |
|
---|
1044 | /****************************************************************************
|
---|
1045 | Write all data from an iov array
|
---|
1046 | ****************************************************************************/
|
---|
1047 |
|
---|
1048 | ssize_t write_data_iov(int fd, const struct iovec *orig_iov, int iovcnt)
|
---|
1049 | {
|
---|
1050 | int i;
|
---|
1051 | size_t to_send;
|
---|
1052 | ssize_t thistime;
|
---|
1053 | size_t sent;
|
---|
1054 | struct iovec *iov_copy, *iov;
|
---|
1055 |
|
---|
1056 | to_send = 0;
|
---|
1057 | for (i=0; i<iovcnt; i++) {
|
---|
1058 | to_send += orig_iov[i].iov_len;
|
---|
1059 | }
|
---|
1060 |
|
---|
1061 | thistime = sys_writev(fd, orig_iov, iovcnt);
|
---|
1062 | if ((thistime <= 0) || (thistime == to_send)) {
|
---|
1063 | return thistime;
|
---|
1064 | }
|
---|
1065 | sent = thistime;
|
---|
1066 |
|
---|
1067 | /*
|
---|
1068 | * We could not send everything in one call. Make a copy of iov that
|
---|
1069 | * we can mess with. We keep a copy of the array start in iov_copy for
|
---|
1070 | * the TALLOC_FREE, because we're going to modify iov later on,
|
---|
1071 | * discarding elements.
|
---|
1072 | */
|
---|
1073 |
|
---|
1074 | iov_copy = (struct iovec *)TALLOC_MEMDUP(
|
---|
1075 | talloc_tos(), orig_iov, sizeof(struct iovec) * iovcnt);
|
---|
1076 |
|
---|
1077 | if (iov_copy == NULL) {
|
---|
1078 | errno = ENOMEM;
|
---|
1079 | return -1;
|
---|
1080 | }
|
---|
1081 | iov = iov_copy;
|
---|
1082 |
|
---|
1083 | while (sent < to_send) {
|
---|
1084 | /*
|
---|
1085 | * We have to discard "thistime" bytes from the beginning
|
---|
1086 | * iov array, "thistime" contains the number of bytes sent
|
---|
1087 | * via writev last.
|
---|
1088 | */
|
---|
1089 | while (thistime > 0) {
|
---|
1090 | if (thistime < iov[0].iov_len) {
|
---|
1091 | char *new_base =
|
---|
1092 | (char *)iov[0].iov_base + thistime;
|
---|
1093 | iov[0].iov_base = new_base;
|
---|
1094 | iov[0].iov_len -= thistime;
|
---|
1095 | break;
|
---|
1096 | }
|
---|
1097 | thistime -= iov[0].iov_len;
|
---|
1098 | iov += 1;
|
---|
1099 | iovcnt -= 1;
|
---|
1100 | }
|
---|
1101 |
|
---|
1102 | thistime = sys_writev(fd, iov, iovcnt);
|
---|
1103 | if (thistime <= 0) {
|
---|
1104 | break;
|
---|
1105 | }
|
---|
1106 | sent += thistime;
|
---|
1107 | }
|
---|
1108 |
|
---|
1109 | TALLOC_FREE(iov_copy);
|
---|
1110 | return sent;
|
---|
1111 | }
|
---|
1112 |
|
---|
1113 | /****************************************************************************
|
---|
1114 | Write data to a fd.
|
---|
1115 | ****************************************************************************/
|
---|
1116 |
|
---|
1117 | /****************************************************************************
|
---|
1118 | Write data to a fd.
|
---|
1119 | ****************************************************************************/
|
---|
1120 |
|
---|
1121 | ssize_t write_data(int fd, const char *buffer, size_t N)
|
---|
1122 | {
|
---|
1123 | ssize_t ret;
|
---|
1124 | struct iovec iov;
|
---|
1125 |
|
---|
1126 | iov.iov_base = CONST_DISCARD(char *, buffer);
|
---|
1127 | iov.iov_len = N;
|
---|
1128 |
|
---|
1129 | ret = write_data_iov(fd, &iov, 1);
|
---|
1130 | if (ret >= 0) {
|
---|
1131 | return ret;
|
---|
1132 | }
|
---|
1133 |
|
---|
1134 | if (fd == get_client_fd()) {
|
---|
1135 | char addr[INET6_ADDRSTRLEN];
|
---|
1136 | /*
|
---|
1137 | * Try and give an error message saying what client failed.
|
---|
1138 | */
|
---|
1139 | DEBUG(0, ("write_data: write failure in writing to client %s. "
|
---|
1140 | "Error %s\n", get_peer_addr(fd,addr,sizeof(addr)),
|
---|
1141 | strerror(errno)));
|
---|
1142 | } else {
|
---|
1143 | DEBUG(0,("write_data: write failure. Error = %s\n",
|
---|
1144 | strerror(errno) ));
|
---|
1145 | }
|
---|
1146 |
|
---|
1147 | return -1;
|
---|
1148 | }
|
---|
1149 |
|
---|
1150 | /****************************************************************************
|
---|
1151 | Send a keepalive packet (rfc1002).
|
---|
1152 | ****************************************************************************/
|
---|
1153 |
|
---|
1154 | bool send_keepalive(int client)
|
---|
1155 | {
|
---|
1156 | unsigned char buf[4];
|
---|
1157 |
|
---|
1158 | buf[0] = SMBkeepalive;
|
---|
1159 | buf[1] = buf[2] = buf[3] = 0;
|
---|
1160 |
|
---|
1161 | return(write_data(client,(char *)buf,4) == 4);
|
---|
1162 | }
|
---|
1163 |
|
---|
1164 | /****************************************************************************
|
---|
1165 | Read 4 bytes of a smb packet and return the smb length of the packet.
|
---|
1166 | Store the result in the buffer.
|
---|
1167 | This version of the function will return a length of zero on receiving
|
---|
1168 | a keepalive packet.
|
---|
1169 | Timeout is in milliseconds.
|
---|
1170 | ****************************************************************************/
|
---|
1171 |
|
---|
1172 | NTSTATUS read_smb_length_return_keepalive(int fd, char *inbuf,
|
---|
1173 | unsigned int timeout,
|
---|
1174 | size_t *len)
|
---|
1175 | {
|
---|
1176 | int msg_type;
|
---|
1177 | NTSTATUS status;
|
---|
1178 |
|
---|
1179 | status = read_socket_with_timeout(fd, inbuf, 4, 4, timeout, NULL);
|
---|
1180 |
|
---|
1181 | if (!NT_STATUS_IS_OK(status)) {
|
---|
1182 | return status;
|
---|
1183 | }
|
---|
1184 |
|
---|
1185 | *len = smb_len(inbuf);
|
---|
1186 | msg_type = CVAL(inbuf,0);
|
---|
1187 |
|
---|
1188 | if (msg_type == SMBkeepalive) {
|
---|
1189 | DEBUG(5,("Got keepalive packet\n"));
|
---|
1190 | }
|
---|
1191 |
|
---|
1192 | DEBUG(10,("got smb length of %lu\n",(unsigned long)(*len)));
|
---|
1193 |
|
---|
1194 | return NT_STATUS_OK;
|
---|
1195 | }
|
---|
1196 |
|
---|
1197 | /****************************************************************************
|
---|
1198 | Read 4 bytes of a smb packet and return the smb length of the packet.
|
---|
1199 | Store the result in the buffer. This version of the function will
|
---|
1200 | never return a session keepalive (length of zero).
|
---|
1201 | Timeout is in milliseconds.
|
---|
1202 | ****************************************************************************/
|
---|
1203 |
|
---|
1204 | NTSTATUS read_smb_length(int fd, char *inbuf, unsigned int timeout,
|
---|
1205 | size_t *len)
|
---|
1206 | {
|
---|
1207 | uint8_t msgtype = SMBkeepalive;
|
---|
1208 |
|
---|
1209 | while (msgtype == SMBkeepalive) {
|
---|
1210 | NTSTATUS status;
|
---|
1211 |
|
---|
1212 | status = read_smb_length_return_keepalive(fd, inbuf, timeout,
|
---|
1213 | len);
|
---|
1214 | if (!NT_STATUS_IS_OK(status)) {
|
---|
1215 | return status;
|
---|
1216 | }
|
---|
1217 |
|
---|
1218 | msgtype = CVAL(inbuf, 0);
|
---|
1219 | }
|
---|
1220 |
|
---|
1221 | DEBUG(10,("read_smb_length: got smb length of %lu\n",
|
---|
1222 | (unsigned long)len));
|
---|
1223 |
|
---|
1224 | return NT_STATUS_OK;
|
---|
1225 | }
|
---|
1226 |
|
---|
1227 | /****************************************************************************
|
---|
1228 | Read an smb from a fd.
|
---|
1229 | The timeout is in milliseconds.
|
---|
1230 | This function will return on receipt of a session keepalive packet.
|
---|
1231 | maxlen is the max number of bytes to return, not including the 4 byte
|
---|
1232 | length. If zero it means buflen limit.
|
---|
1233 | Doesn't check the MAC on signed packets.
|
---|
1234 | ****************************************************************************/
|
---|
1235 |
|
---|
1236 | NTSTATUS receive_smb_raw(int fd, char *buffer, size_t buflen, unsigned int timeout,
|
---|
1237 | size_t maxlen, size_t *p_len)
|
---|
1238 | {
|
---|
1239 | size_t len;
|
---|
1240 | NTSTATUS status;
|
---|
1241 |
|
---|
1242 | status = read_smb_length_return_keepalive(fd,buffer,timeout,&len);
|
---|
1243 |
|
---|
1244 | if (!NT_STATUS_IS_OK(status)) {
|
---|
1245 | DEBUG(10, ("receive_smb_raw: %s!\n", nt_errstr(status)));
|
---|
1246 | return status;
|
---|
1247 | }
|
---|
1248 |
|
---|
1249 | if (len > buflen) {
|
---|
1250 | DEBUG(0,("Invalid packet length! (%lu bytes).\n",
|
---|
1251 | (unsigned long)len));
|
---|
1252 | return NT_STATUS_INVALID_PARAMETER;
|
---|
1253 | }
|
---|
1254 |
|
---|
1255 | if(len > 0) {
|
---|
1256 | if (maxlen) {
|
---|
1257 | len = MIN(len,maxlen);
|
---|
1258 | }
|
---|
1259 |
|
---|
1260 | status = read_socket_with_timeout(
|
---|
1261 | fd, buffer+4, len, len, timeout, &len);
|
---|
1262 |
|
---|
1263 | if (!NT_STATUS_IS_OK(status)) {
|
---|
1264 | return status;
|
---|
1265 | }
|
---|
1266 |
|
---|
1267 | /* not all of samba3 properly checks for packet-termination
|
---|
1268 | * of strings. This ensures that we don't run off into
|
---|
1269 | * empty space. */
|
---|
1270 | SSVAL(buffer+4,len, 0);
|
---|
1271 | }
|
---|
1272 |
|
---|
1273 | *p_len = len;
|
---|
1274 | return NT_STATUS_OK;
|
---|
1275 | }
|
---|
1276 |
|
---|
1277 | /****************************************************************************
|
---|
1278 | Open a socket of the specified type, port, and address for incoming data.
|
---|
1279 | ****************************************************************************/
|
---|
1280 |
|
---|
1281 | int open_socket_in(int type,
|
---|
1282 | uint16_t port,
|
---|
1283 | int dlevel,
|
---|
1284 | const struct sockaddr_storage *psock,
|
---|
1285 | bool rebind)
|
---|
1286 | {
|
---|
1287 | struct sockaddr_storage sock;
|
---|
1288 | int res;
|
---|
1289 | socklen_t slen = sizeof(struct sockaddr_in);
|
---|
1290 |
|
---|
1291 | sock = *psock;
|
---|
1292 |
|
---|
1293 | #if defined(HAVE_IPV6)
|
---|
1294 | if (sock.ss_family == AF_INET6) {
|
---|
1295 | ((struct sockaddr_in6 *)&sock)->sin6_port = htons(port);
|
---|
1296 | slen = sizeof(struct sockaddr_in6);
|
---|
1297 | }
|
---|
1298 | #endif
|
---|
1299 | if (sock.ss_family == AF_INET) {
|
---|
1300 | ((struct sockaddr_in *)&sock)->sin_port = htons(port);
|
---|
1301 | }
|
---|
1302 |
|
---|
1303 | res = socket(sock.ss_family, type, 0 );
|
---|
1304 | if( res == -1 ) {
|
---|
1305 | if( DEBUGLVL(0) ) {
|
---|
1306 | dbgtext( "open_socket_in(): socket() call failed: " );
|
---|
1307 | dbgtext( "%s\n", strerror( errno ) );
|
---|
1308 | }
|
---|
1309 | return -1;
|
---|
1310 | }
|
---|
1311 |
|
---|
1312 | /* This block sets/clears the SO_REUSEADDR and possibly SO_REUSEPORT. */
|
---|
1313 | {
|
---|
1314 | int val = rebind ? 1 : 0;
|
---|
1315 | if( setsockopt(res,SOL_SOCKET,SO_REUSEADDR,
|
---|
1316 | (char *)&val,sizeof(val)) == -1 ) {
|
---|
1317 | if( DEBUGLVL( dlevel ) ) {
|
---|
1318 | dbgtext( "open_socket_in(): setsockopt: " );
|
---|
1319 | dbgtext( "SO_REUSEADDR = %s ",
|
---|
1320 | val?"true":"false" );
|
---|
1321 | dbgtext( "on port %d failed ", port );
|
---|
1322 | dbgtext( "with error = %s\n", strerror(errno) );
|
---|
1323 | }
|
---|
1324 | }
|
---|
1325 | #ifdef SO_REUSEPORT
|
---|
1326 | if( setsockopt(res,SOL_SOCKET,SO_REUSEPORT,
|
---|
1327 | (char *)&val,sizeof(val)) == -1 ) {
|
---|
1328 | if( DEBUGLVL( dlevel ) ) {
|
---|
1329 | dbgtext( "open_socket_in(): setsockopt: ");
|
---|
1330 | dbgtext( "SO_REUSEPORT = %s ",
|
---|
1331 | val?"true":"false");
|
---|
1332 | dbgtext( "on port %d failed ", port);
|
---|
1333 | dbgtext( "with error = %s\n", strerror(errno));
|
---|
1334 | }
|
---|
1335 | }
|
---|
1336 | #endif /* SO_REUSEPORT */
|
---|
1337 | }
|
---|
1338 |
|
---|
1339 | /* now we've got a socket - we need to bind it */
|
---|
1340 | if (bind(res, (struct sockaddr *)&sock, slen) == -1 ) {
|
---|
1341 | if( DEBUGLVL(dlevel) && (port == SMB_PORT1 ||
|
---|
1342 | port == SMB_PORT2 || port == NMB_PORT) ) {
|
---|
1343 | char addr[INET6_ADDRSTRLEN];
|
---|
1344 | print_sockaddr(addr, sizeof(addr),
|
---|
1345 | &sock);
|
---|
1346 | dbgtext( "bind failed on port %d ", port);
|
---|
1347 | dbgtext( "socket_addr = %s.\n", addr);
|
---|
1348 | dbgtext( "Error = %s\n", strerror(errno));
|
---|
1349 | }
|
---|
1350 | close(res);
|
---|
1351 | return -1;
|
---|
1352 | }
|
---|
1353 |
|
---|
1354 | DEBUG( 10, ( "bind succeeded on port %d\n", port ) );
|
---|
1355 | return( res );
|
---|
1356 | }
|
---|
1357 |
|
---|
1358 | /****************************************************************************
|
---|
1359 | Create an outgoing socket. timeout is in milliseconds.
|
---|
1360 | **************************************************************************/
|
---|
1361 |
|
---|
1362 | int open_socket_out(int type,
|
---|
1363 | const struct sockaddr_storage *pss,
|
---|
1364 | uint16_t port,
|
---|
1365 | int timeout)
|
---|
1366 | {
|
---|
1367 | char addr[INET6_ADDRSTRLEN];
|
---|
1368 | struct sockaddr_storage sock_out = *pss;
|
---|
1369 | int res,ret;
|
---|
1370 | int connect_loop = 10;
|
---|
1371 | int increment = 10;
|
---|
1372 |
|
---|
1373 | /* create a socket to write to */
|
---|
1374 | res = socket(pss->ss_family, type, 0);
|
---|
1375 | if (res == -1) {
|
---|
1376 | DEBUG(0,("socket error (%s)\n", strerror(errno)));
|
---|
1377 | return -1;
|
---|
1378 | }
|
---|
1379 |
|
---|
1380 | if (type != SOCK_STREAM) {
|
---|
1381 | return res;
|
---|
1382 | }
|
---|
1383 |
|
---|
1384 | #if defined(HAVE_IPV6)
|
---|
1385 | if (pss->ss_family == AF_INET6) {
|
---|
1386 | struct sockaddr_in6 *psa6 = (struct sockaddr_in6 *)&sock_out;
|
---|
1387 | psa6->sin6_port = htons(port);
|
---|
1388 | if (psa6->sin6_scope_id == 0 &&
|
---|
1389 | IN6_IS_ADDR_LINKLOCAL(&psa6->sin6_addr)) {
|
---|
1390 | setup_linklocal_scope_id(&sock_out);
|
---|
1391 | }
|
---|
1392 | }
|
---|
1393 | #endif
|
---|
1394 | if (pss->ss_family == AF_INET) {
|
---|
1395 | struct sockaddr_in *psa = (struct sockaddr_in *)&sock_out;
|
---|
1396 | psa->sin_port = htons(port);
|
---|
1397 | }
|
---|
1398 |
|
---|
1399 | /* set it non-blocking */
|
---|
1400 | set_blocking(res,false);
|
---|
1401 |
|
---|
1402 | print_sockaddr(addr, sizeof(addr), &sock_out);
|
---|
1403 | DEBUG(3,("Connecting to %s at port %u\n",
|
---|
1404 | addr,
|
---|
1405 | (unsigned int)port));
|
---|
1406 |
|
---|
1407 | /* and connect it to the destination */
|
---|
1408 | connect_again:
|
---|
1409 |
|
---|
1410 | ret = sys_connect(res, (struct sockaddr *)&sock_out);
|
---|
1411 |
|
---|
1412 | /* Some systems return EAGAIN when they mean EINPROGRESS */
|
---|
1413 | if (ret < 0 && (errno == EINPROGRESS || errno == EALREADY ||
|
---|
1414 | errno == EAGAIN) && (connect_loop < timeout) ) {
|
---|
1415 | smb_msleep(connect_loop);
|
---|
1416 | timeout -= connect_loop;
|
---|
1417 | connect_loop += increment;
|
---|
1418 | if (increment < 250) {
|
---|
1419 | /* After 8 rounds we end up at a max of 255 msec */
|
---|
1420 | increment *= 1.5;
|
---|
1421 | }
|
---|
1422 | goto connect_again;
|
---|
1423 | }
|
---|
1424 |
|
---|
1425 | if (ret < 0 && (errno == EINPROGRESS || errno == EALREADY ||
|
---|
1426 | errno == EAGAIN)) {
|
---|
1427 | DEBUG(1,("timeout connecting to %s:%u\n",
|
---|
1428 | addr,
|
---|
1429 | (unsigned int)port));
|
---|
1430 | close(res);
|
---|
1431 | return -1;
|
---|
1432 | }
|
---|
1433 |
|
---|
1434 | #ifdef EISCONN
|
---|
1435 | if (ret < 0 && errno == EISCONN) {
|
---|
1436 | errno = 0;
|
---|
1437 | ret = 0;
|
---|
1438 | }
|
---|
1439 | #endif
|
---|
1440 |
|
---|
1441 | if (ret < 0) {
|
---|
1442 | DEBUG(2,("error connecting to %s:%d (%s)\n",
|
---|
1443 | addr,
|
---|
1444 | (unsigned int)port,
|
---|
1445 | strerror(errno)));
|
---|
1446 | close(res);
|
---|
1447 | return -1;
|
---|
1448 | }
|
---|
1449 |
|
---|
1450 | /* set it blocking again */
|
---|
1451 | set_blocking(res,true);
|
---|
1452 |
|
---|
1453 | return res;
|
---|
1454 | }
|
---|
1455 |
|
---|
1456 | /*******************************************************************
|
---|
1457 | Create an outgoing TCP socket to the first addr that connects.
|
---|
1458 |
|
---|
1459 | This is for simultaneous connection attempts to port 445 and 139 of a host
|
---|
1460 | or for simultatneous connection attempts to multiple DCs at once. We return
|
---|
1461 | a socket fd of the first successful connection.
|
---|
1462 |
|
---|
1463 | @param[in] addrs list of Internet addresses and ports to connect to
|
---|
1464 | @param[in] num_addrs number of address/port pairs in the addrs list
|
---|
1465 | @param[in] timeout time after which we stop waiting for a socket connection
|
---|
1466 | to succeed, given in milliseconds
|
---|
1467 | @param[out] fd_index the entry in addrs which we successfully connected to
|
---|
1468 | @param[out] fd fd of the open and connected socket
|
---|
1469 | @return true on a successful connection, false if all connection attempts
|
---|
1470 | failed or we timed out
|
---|
1471 | *******************************************************************/
|
---|
1472 |
|
---|
1473 | bool open_any_socket_out(struct sockaddr_storage *addrs, int num_addrs,
|
---|
1474 | int timeout, int *fd_index, int *fd)
|
---|
1475 | {
|
---|
1476 | int i, resulting_index, res;
|
---|
1477 | int *sockets;
|
---|
1478 | bool good_connect;
|
---|
1479 |
|
---|
1480 | fd_set r_fds, wr_fds;
|
---|
1481 | struct timeval tv;
|
---|
1482 | int maxfd;
|
---|
1483 |
|
---|
1484 | int connect_loop = 10000; /* 10 milliseconds */
|
---|
1485 |
|
---|
1486 | timeout *= 1000; /* convert to microseconds */
|
---|
1487 |
|
---|
1488 | sockets = SMB_MALLOC_ARRAY(int, num_addrs);
|
---|
1489 |
|
---|
1490 | if (sockets == NULL)
|
---|
1491 | return false;
|
---|
1492 |
|
---|
1493 | resulting_index = -1;
|
---|
1494 |
|
---|
1495 | for (i=0; i<num_addrs; i++)
|
---|
1496 | sockets[i] = -1;
|
---|
1497 |
|
---|
1498 | for (i=0; i<num_addrs; i++) {
|
---|
1499 | sockets[i] = socket(addrs[i].ss_family, SOCK_STREAM, 0);
|
---|
1500 | if (sockets[i] < 0 || sockets[i] >= FD_SETSIZE)
|
---|
1501 | goto done;
|
---|
1502 | set_blocking(sockets[i], false);
|
---|
1503 | }
|
---|
1504 |
|
---|
1505 | connect_again:
|
---|
1506 | good_connect = false;
|
---|
1507 |
|
---|
1508 | for (i=0; i<num_addrs; i++) {
|
---|
1509 | const struct sockaddr * a =
|
---|
1510 | (const struct sockaddr *)&(addrs[i]);
|
---|
1511 |
|
---|
1512 | if (sockets[i] == -1)
|
---|
1513 | continue;
|
---|
1514 |
|
---|
1515 | if (sys_connect(sockets[i], a) == 0) {
|
---|
1516 | /* Rather unlikely as we are non-blocking, but it
|
---|
1517 | * might actually happen. */
|
---|
1518 | resulting_index = i;
|
---|
1519 | goto done;
|
---|
1520 | }
|
---|
1521 |
|
---|
1522 | if (errno == EINPROGRESS || errno == EALREADY ||
|
---|
1523 | #ifdef EISCONN
|
---|
1524 | errno == EISCONN ||
|
---|
1525 | #endif
|
---|
1526 | errno == EAGAIN || errno == EINTR) {
|
---|
1527 | /* These are the error messages that something is
|
---|
1528 | progressing. */
|
---|
1529 | good_connect = true;
|
---|
1530 | } else if (errno != 0) {
|
---|
1531 | /* There was a direct error */
|
---|
1532 | close(sockets[i]);
|
---|
1533 | sockets[i] = -1;
|
---|
1534 | }
|
---|
1535 | }
|
---|
1536 |
|
---|
1537 | if (!good_connect) {
|
---|
1538 | /* All of the connect's resulted in real error conditions */
|
---|
1539 | goto done;
|
---|
1540 | }
|
---|
1541 |
|
---|
1542 | /* Lets see if any of the connect attempts succeeded */
|
---|
1543 |
|
---|
1544 | maxfd = 0;
|
---|
1545 | FD_ZERO(&wr_fds);
|
---|
1546 | FD_ZERO(&r_fds);
|
---|
1547 |
|
---|
1548 | for (i=0; i<num_addrs; i++) {
|
---|
1549 | if (sockets[i] < 0 || sockets[i] >= FD_SETSIZE) {
|
---|
1550 | /* This cannot happen - ignore if so. */
|
---|
1551 | continue;
|
---|
1552 | }
|
---|
1553 | FD_SET(sockets[i], &wr_fds);
|
---|
1554 | FD_SET(sockets[i], &r_fds);
|
---|
1555 | if (sockets[i]>maxfd)
|
---|
1556 | maxfd = sockets[i];
|
---|
1557 | }
|
---|
1558 |
|
---|
1559 | tv.tv_sec = 0;
|
---|
1560 | tv.tv_usec = connect_loop;
|
---|
1561 |
|
---|
1562 | res = sys_select_intr(maxfd+1, &r_fds, &wr_fds, NULL, &tv);
|
---|
1563 |
|
---|
1564 | if (res < 0)
|
---|
1565 | goto done;
|
---|
1566 |
|
---|
1567 | if (res == 0)
|
---|
1568 | goto next_round;
|
---|
1569 |
|
---|
1570 | for (i=0; i<num_addrs; i++) {
|
---|
1571 |
|
---|
1572 | if (sockets[i] == -1)
|
---|
1573 | continue;
|
---|
1574 |
|
---|
1575 | /* Stevens, Network Programming says that if there's a
|
---|
1576 | * successful connect, the socket is only writable. Upon an
|
---|
1577 | * error, it's both readable and writable. */
|
---|
1578 |
|
---|
1579 | if (FD_ISSET(sockets[i], &r_fds) &&
|
---|
1580 | FD_ISSET(sockets[i], &wr_fds)) {
|
---|
1581 | /* readable and writable, so it's an error */
|
---|
1582 | close(sockets[i]);
|
---|
1583 | sockets[i] = -1;
|
---|
1584 | continue;
|
---|
1585 | }
|
---|
1586 |
|
---|
1587 | if (!FD_ISSET(sockets[i], &r_fds) &&
|
---|
1588 | FD_ISSET(sockets[i], &wr_fds)) {
|
---|
1589 | /* Only writable, so it's connected */
|
---|
1590 | resulting_index = i;
|
---|
1591 | goto done;
|
---|
1592 | }
|
---|
1593 | }
|
---|
1594 |
|
---|
1595 | next_round:
|
---|
1596 |
|
---|
1597 | timeout -= connect_loop;
|
---|
1598 | if (timeout <= 0)
|
---|
1599 | goto done;
|
---|
1600 | connect_loop *= 1.5;
|
---|
1601 | if (connect_loop > timeout)
|
---|
1602 | connect_loop = timeout;
|
---|
1603 | goto connect_again;
|
---|
1604 |
|
---|
1605 | done:
|
---|
1606 | for (i=0; i<num_addrs; i++) {
|
---|
1607 | if (i == resulting_index)
|
---|
1608 | continue;
|
---|
1609 | if (sockets[i] >= 0)
|
---|
1610 | close(sockets[i]);
|
---|
1611 | }
|
---|
1612 |
|
---|
1613 | if (resulting_index >= 0) {
|
---|
1614 | *fd_index = resulting_index;
|
---|
1615 | *fd = sockets[*fd_index];
|
---|
1616 | set_blocking(*fd, true);
|
---|
1617 | }
|
---|
1618 |
|
---|
1619 | free(sockets);
|
---|
1620 |
|
---|
1621 | return (resulting_index >= 0);
|
---|
1622 | }
|
---|
1623 | /****************************************************************************
|
---|
1624 | Open a connected UDP socket to host on port
|
---|
1625 | **************************************************************************/
|
---|
1626 |
|
---|
1627 | int open_udp_socket(const char *host, int port)
|
---|
1628 | {
|
---|
1629 | int type = SOCK_DGRAM;
|
---|
1630 | struct sockaddr_in sock_out;
|
---|
1631 | int res;
|
---|
1632 | struct in_addr addr;
|
---|
1633 |
|
---|
1634 | (void)interpret_addr2(&addr, host);
|
---|
1635 |
|
---|
1636 | res = socket(PF_INET, type, 0);
|
---|
1637 | if (res == -1) {
|
---|
1638 | return -1;
|
---|
1639 | }
|
---|
1640 |
|
---|
1641 | memset((char *)&sock_out,'\0',sizeof(sock_out));
|
---|
1642 | putip((char *)&sock_out.sin_addr,(char *)&addr);
|
---|
1643 | sock_out.sin_port = htons(port);
|
---|
1644 | sock_out.sin_family = PF_INET;
|
---|
1645 |
|
---|
1646 | if (sys_connect(res,(struct sockaddr *)&sock_out)) {
|
---|
1647 | close(res);
|
---|
1648 | return -1;
|
---|
1649 | }
|
---|
1650 |
|
---|
1651 | return res;
|
---|
1652 | }
|
---|
1653 |
|
---|
1654 | /*******************************************************************
|
---|
1655 | Return the IP addr of the remote end of a socket as a string.
|
---|
1656 | Optionally return the struct sockaddr_storage.
|
---|
1657 | ******************************************************************/
|
---|
1658 |
|
---|
1659 | static const char *get_peer_addr_internal(int fd,
|
---|
1660 | char *addr_buf,
|
---|
1661 | size_t addr_buf_len,
|
---|
1662 | struct sockaddr_storage *pss,
|
---|
1663 | socklen_t *plength)
|
---|
1664 | {
|
---|
1665 | struct sockaddr_storage ss;
|
---|
1666 | socklen_t length = sizeof(ss);
|
---|
1667 |
|
---|
1668 | strlcpy(addr_buf,"0.0.0.0",addr_buf_len);
|
---|
1669 |
|
---|
1670 | if (fd == -1) {
|
---|
1671 | return addr_buf;
|
---|
1672 | }
|
---|
1673 |
|
---|
1674 | if (pss == NULL) {
|
---|
1675 | pss = &ss;
|
---|
1676 | }
|
---|
1677 | if (plength == NULL) {
|
---|
1678 | plength = &length;
|
---|
1679 | }
|
---|
1680 |
|
---|
1681 | if (getpeername(fd, (struct sockaddr *)pss, plength) < 0) {
|
---|
1682 | DEBUG(0,("getpeername failed. Error was %s\n",
|
---|
1683 | strerror(errno) ));
|
---|
1684 | return addr_buf;
|
---|
1685 | }
|
---|
1686 |
|
---|
1687 | print_sockaddr_len(addr_buf,
|
---|
1688 | addr_buf_len,
|
---|
1689 | pss,
|
---|
1690 | *plength);
|
---|
1691 | return addr_buf;
|
---|
1692 | }
|
---|
1693 |
|
---|
1694 | /*******************************************************************
|
---|
1695 | Matchname - determine if host name matches IP address. Used to
|
---|
1696 | confirm a hostname lookup to prevent spoof attacks.
|
---|
1697 | ******************************************************************/
|
---|
1698 |
|
---|
1699 | static bool matchname(const char *remotehost,
|
---|
1700 | const struct sockaddr_storage *pss,
|
---|
1701 | socklen_t len)
|
---|
1702 | {
|
---|
1703 | struct addrinfo *res = NULL;
|
---|
1704 | struct addrinfo *ailist = NULL;
|
---|
1705 | char addr_buf[INET6_ADDRSTRLEN];
|
---|
1706 | bool ret = interpret_string_addr_internal(&ailist,
|
---|
1707 | remotehost,
|
---|
1708 | AI_ADDRCONFIG|AI_CANONNAME);
|
---|
1709 |
|
---|
1710 | if (!ret || ailist == NULL) {
|
---|
1711 | DEBUG(3,("matchname: getaddrinfo failed for "
|
---|
1712 | "name %s [%s]\n",
|
---|
1713 | remotehost,
|
---|
1714 | gai_strerror(ret) ));
|
---|
1715 | return false;
|
---|
1716 | }
|
---|
1717 |
|
---|
1718 | /*
|
---|
1719 | * Make sure that getaddrinfo() returns the "correct" host name.
|
---|
1720 | */
|
---|
1721 |
|
---|
1722 | if (ailist->ai_canonname == NULL ||
|
---|
1723 | (!strequal(remotehost, ailist->ai_canonname) &&
|
---|
1724 | !strequal(remotehost, "localhost"))) {
|
---|
1725 | DEBUG(0,("matchname: host name/name mismatch: %s != %s\n",
|
---|
1726 | remotehost,
|
---|
1727 | ailist->ai_canonname ?
|
---|
1728 | ailist->ai_canonname : "(NULL)"));
|
---|
1729 | freeaddrinfo(ailist);
|
---|
1730 | return false;
|
---|
1731 | }
|
---|
1732 |
|
---|
1733 | /* Look up the host address in the address list we just got. */
|
---|
1734 | for (res = ailist; res; res = res->ai_next) {
|
---|
1735 | if (!res->ai_addr) {
|
---|
1736 | continue;
|
---|
1737 | }
|
---|
1738 | if (sockaddr_equal((const struct sockaddr_storage *)res->ai_addr,
|
---|
1739 | pss)) {
|
---|
1740 | freeaddrinfo(ailist);
|
---|
1741 | return true;
|
---|
1742 | }
|
---|
1743 | }
|
---|
1744 |
|
---|
1745 | /*
|
---|
1746 | * The host name does not map to the original host address. Perhaps
|
---|
1747 | * someone has compromised a name server. More likely someone botched
|
---|
1748 | * it, but that could be dangerous, too.
|
---|
1749 | */
|
---|
1750 |
|
---|
1751 | DEBUG(0,("matchname: host name/address mismatch: %s != %s\n",
|
---|
1752 | print_sockaddr_len(addr_buf,
|
---|
1753 | sizeof(addr_buf),
|
---|
1754 | pss,
|
---|
1755 | len),
|
---|
1756 | ailist->ai_canonname ? ailist->ai_canonname : "(NULL)"));
|
---|
1757 |
|
---|
1758 | if (ailist) {
|
---|
1759 | freeaddrinfo(ailist);
|
---|
1760 | }
|
---|
1761 | return false;
|
---|
1762 | }
|
---|
1763 |
|
---|
1764 | /*******************************************************************
|
---|
1765 | Deal with the singleton cache.
|
---|
1766 | ******************************************************************/
|
---|
1767 |
|
---|
1768 | struct name_addr_pair {
|
---|
1769 | struct sockaddr_storage ss;
|
---|
1770 | const char *name;
|
---|
1771 | };
|
---|
1772 |
|
---|
1773 | /*******************************************************************
|
---|
1774 | Lookup a name/addr pair. Returns memory allocated from memcache.
|
---|
1775 | ******************************************************************/
|
---|
1776 |
|
---|
1777 | static bool lookup_nc(struct name_addr_pair *nc)
|
---|
1778 | {
|
---|
1779 | DATA_BLOB tmp;
|
---|
1780 |
|
---|
1781 | ZERO_STRUCTP(nc);
|
---|
1782 |
|
---|
1783 | if (!memcache_lookup(
|
---|
1784 | NULL, SINGLETON_CACHE,
|
---|
1785 | data_blob_string_const("get_peer_name"),
|
---|
1786 | &tmp)) {
|
---|
1787 | return false;
|
---|
1788 | }
|
---|
1789 |
|
---|
1790 | memcpy(&nc->ss, tmp.data, sizeof(nc->ss));
|
---|
1791 | nc->name = (const char *)tmp.data + sizeof(nc->ss);
|
---|
1792 | return true;
|
---|
1793 | }
|
---|
1794 |
|
---|
1795 | /*******************************************************************
|
---|
1796 | Save a name/addr pair.
|
---|
1797 | ******************************************************************/
|
---|
1798 |
|
---|
1799 | static void store_nc(const struct name_addr_pair *nc)
|
---|
1800 | {
|
---|
1801 | DATA_BLOB tmp;
|
---|
1802 | size_t namelen = strlen(nc->name);
|
---|
1803 |
|
---|
1804 | tmp = data_blob(NULL, sizeof(nc->ss) + namelen + 1);
|
---|
1805 | if (!tmp.data) {
|
---|
1806 | return;
|
---|
1807 | }
|
---|
1808 | memcpy(tmp.data, &nc->ss, sizeof(nc->ss));
|
---|
1809 | memcpy(tmp.data+sizeof(nc->ss), nc->name, namelen+1);
|
---|
1810 |
|
---|
1811 | memcache_add(NULL, SINGLETON_CACHE,
|
---|
1812 | data_blob_string_const("get_peer_name"),
|
---|
1813 | tmp);
|
---|
1814 | data_blob_free(&tmp);
|
---|
1815 | }
|
---|
1816 |
|
---|
1817 | /*******************************************************************
|
---|
1818 | Return the DNS name of the remote end of a socket.
|
---|
1819 | ******************************************************************/
|
---|
1820 |
|
---|
1821 | const char *get_peer_name(int fd, bool force_lookup)
|
---|
1822 | {
|
---|
1823 | struct name_addr_pair nc;
|
---|
1824 | char addr_buf[INET6_ADDRSTRLEN];
|
---|
1825 | struct sockaddr_storage ss;
|
---|
1826 | socklen_t length = sizeof(ss);
|
---|
1827 | const char *p;
|
---|
1828 | int ret;
|
---|
1829 | char name_buf[MAX_DNS_NAME_LENGTH];
|
---|
1830 | char tmp_name[MAX_DNS_NAME_LENGTH];
|
---|
1831 |
|
---|
1832 | /* reverse lookups can be *very* expensive, and in many
|
---|
1833 | situations won't work because many networks don't link dhcp
|
---|
1834 | with dns. To avoid the delay we avoid the lookup if
|
---|
1835 | possible */
|
---|
1836 | if (!lp_hostname_lookups() && (force_lookup == false)) {
|
---|
1837 | length = sizeof(nc.ss);
|
---|
1838 | nc.name = get_peer_addr_internal(fd, addr_buf, sizeof(addr_buf),
|
---|
1839 | &nc.ss, &length);
|
---|
1840 | store_nc(&nc);
|
---|
1841 | lookup_nc(&nc);
|
---|
1842 | return nc.name ? nc.name : "UNKNOWN";
|
---|
1843 | }
|
---|
1844 |
|
---|
1845 | lookup_nc(&nc);
|
---|
1846 |
|
---|
1847 | memset(&ss, '\0', sizeof(ss));
|
---|
1848 | p = get_peer_addr_internal(fd, addr_buf, sizeof(addr_buf), &ss, &length);
|
---|
1849 |
|
---|
1850 | /* it might be the same as the last one - save some DNS work */
|
---|
1851 | if (sockaddr_equal(&ss, &nc.ss)) {
|
---|
1852 | return nc.name ? nc.name : "UNKNOWN";
|
---|
1853 | }
|
---|
1854 |
|
---|
1855 | /* Not the same. We need to lookup. */
|
---|
1856 | if (fd == -1) {
|
---|
1857 | return "UNKNOWN";
|
---|
1858 | }
|
---|
1859 |
|
---|
1860 | /* Look up the remote host name. */
|
---|
1861 | ret = sys_getnameinfo((struct sockaddr *)&ss,
|
---|
1862 | length,
|
---|
1863 | name_buf,
|
---|
1864 | sizeof(name_buf),
|
---|
1865 | NULL,
|
---|
1866 | 0,
|
---|
1867 | 0);
|
---|
1868 |
|
---|
1869 | if (ret) {
|
---|
1870 | DEBUG(1,("get_peer_name: getnameinfo failed "
|
---|
1871 | "for %s with error %s\n",
|
---|
1872 | p,
|
---|
1873 | gai_strerror(ret)));
|
---|
1874 | strlcpy(name_buf, p, sizeof(name_buf));
|
---|
1875 | } else {
|
---|
1876 | if (!matchname(name_buf, &ss, length)) {
|
---|
1877 | DEBUG(0,("Matchname failed on %s %s\n",name_buf,p));
|
---|
1878 | strlcpy(name_buf,"UNKNOWN",sizeof(name_buf));
|
---|
1879 | }
|
---|
1880 | }
|
---|
1881 |
|
---|
1882 | /* can't pass the same source and dest strings in when you
|
---|
1883 | use --enable-developer or the clobber_region() call will
|
---|
1884 | get you */
|
---|
1885 |
|
---|
1886 | strlcpy(tmp_name, name_buf, sizeof(tmp_name));
|
---|
1887 | alpha_strcpy(name_buf, tmp_name, "_-.", sizeof(name_buf));
|
---|
1888 | if (strstr(name_buf,"..")) {
|
---|
1889 | strlcpy(name_buf, "UNKNOWN", sizeof(name_buf));
|
---|
1890 | }
|
---|
1891 |
|
---|
1892 | nc.name = name_buf;
|
---|
1893 | nc.ss = ss;
|
---|
1894 |
|
---|
1895 | store_nc(&nc);
|
---|
1896 | lookup_nc(&nc);
|
---|
1897 | return nc.name ? nc.name : "UNKNOWN";
|
---|
1898 | }
|
---|
1899 |
|
---|
1900 | /*******************************************************************
|
---|
1901 | Return the IP addr of the remote end of a socket as a string.
|
---|
1902 | ******************************************************************/
|
---|
1903 |
|
---|
1904 | const char *get_peer_addr(int fd, char *addr, size_t addr_len)
|
---|
1905 | {
|
---|
1906 | return get_peer_addr_internal(fd, addr, addr_len, NULL, NULL);
|
---|
1907 | }
|
---|
1908 |
|
---|
1909 | /*******************************************************************
|
---|
1910 | Create protected unix domain socket.
|
---|
1911 |
|
---|
1912 | Some unixes cannot set permissions on a ux-dom-sock, so we
|
---|
1913 | have to make sure that the directory contains the protection
|
---|
1914 | permissions instead.
|
---|
1915 | ******************************************************************/
|
---|
1916 |
|
---|
1917 | int create_pipe_sock(const char *socket_dir,
|
---|
1918 | const char *socket_name,
|
---|
1919 | mode_t dir_perms)
|
---|
1920 | {
|
---|
1921 | #ifdef HAVE_UNIXSOCKET
|
---|
1922 | struct sockaddr_un sunaddr;
|
---|
1923 | struct stat st;
|
---|
1924 | int sock;
|
---|
1925 | mode_t old_umask;
|
---|
1926 | char *path = NULL;
|
---|
1927 |
|
---|
1928 | old_umask = umask(0);
|
---|
1929 |
|
---|
1930 | /* Create the socket directory or reuse the existing one */
|
---|
1931 |
|
---|
1932 | #ifndef __OS2__
|
---|
1933 | if (lstat(socket_dir, &st) == -1) {
|
---|
1934 | if (errno == ENOENT) {
|
---|
1935 | /* Create directory */
|
---|
1936 | if (mkdir(socket_dir, dir_perms) == -1) {
|
---|
1937 | DEBUG(0, ("error creating socket directory "
|
---|
1938 | "%s: %s\n", socket_dir,
|
---|
1939 | strerror(errno)));
|
---|
1940 | goto out_umask;
|
---|
1941 | }
|
---|
1942 | } else {
|
---|
1943 | DEBUG(0, ("lstat failed on socket directory %s: %s\n",
|
---|
1944 | socket_dir, strerror(errno)));
|
---|
1945 | goto out_umask;
|
---|
1946 | }
|
---|
1947 | } else {
|
---|
1948 | /* Check ownership and permission on existing directory */
|
---|
1949 | if (!S_ISDIR(st.st_mode)) {
|
---|
1950 | DEBUG(0, ("socket directory %s isn't a directory\n",
|
---|
1951 | socket_dir));
|
---|
1952 | goto out_umask;
|
---|
1953 | }
|
---|
1954 | if ((st.st_uid != sec_initial_uid()) ||
|
---|
1955 | ((st.st_mode & 0777) != dir_perms)) {
|
---|
1956 | DEBUG(0, ("invalid permissions on socket directory "
|
---|
1957 | "%s\n", socket_dir));
|
---|
1958 | goto out_umask;
|
---|
1959 | }
|
---|
1960 | }
|
---|
1961 | #endif
|
---|
1962 |
|
---|
1963 | /* Create the socket file */
|
---|
1964 |
|
---|
1965 | sock = socket(AF_UNIX, SOCK_STREAM, 0);
|
---|
1966 |
|
---|
1967 | if (sock == -1) {
|
---|
1968 | DEBUG(0, ("create_pipe_sock: socket error %s\n",
|
---|
1969 | strerror(errno) ));
|
---|
1970 | goto out_close;
|
---|
1971 | }
|
---|
1972 |
|
---|
1973 | #ifdef __OS2__
|
---|
1974 | if (asprintf(&path, "\\socket\\samba\\%s\\%s", socket_dir, socket_name) == -1) {
|
---|
1975 | #else
|
---|
1976 | if (asprintf(&path, "%s/%s", socket_dir, socket_name) == -1) {
|
---|
1977 | #endif
|
---|
1978 | goto out_close;
|
---|
1979 | }
|
---|
1980 |
|
---|
1981 | unlink(path);
|
---|
1982 | memset(&sunaddr, 0, sizeof(sunaddr));
|
---|
1983 | sunaddr.sun_family = AF_UNIX;
|
---|
1984 | strlcpy(sunaddr.sun_path, path, sizeof(sunaddr.sun_path));
|
---|
1985 |
|
---|
1986 | if (bind(sock, (struct sockaddr *)&sunaddr, sizeof(sunaddr)) == -1) {
|
---|
1987 | DEBUG(0, ("bind failed on pipe socket %s: %s\n", path,
|
---|
1988 | strerror(errno)));
|
---|
1989 | goto out_close;
|
---|
1990 | }
|
---|
1991 |
|
---|
1992 | if (listen(sock, 5) == -1) {
|
---|
1993 | DEBUG(0, ("listen failed on pipe socket %s: %s\n", path,
|
---|
1994 | strerror(errno)));
|
---|
1995 | goto out_close;
|
---|
1996 | }
|
---|
1997 |
|
---|
1998 | SAFE_FREE(path);
|
---|
1999 |
|
---|
2000 | umask(old_umask);
|
---|
2001 | return sock;
|
---|
2002 |
|
---|
2003 | out_close:
|
---|
2004 | SAFE_FREE(path);
|
---|
2005 | if (sock != -1)
|
---|
2006 | close(sock);
|
---|
2007 |
|
---|
2008 | out_umask:
|
---|
2009 | umask(old_umask);
|
---|
2010 | return -1;
|
---|
2011 |
|
---|
2012 | #else
|
---|
2013 | DEBUG(0, ("create_pipe_sock: No Unix sockets on this system\n"));
|
---|
2014 | return -1;
|
---|
2015 | #endif /* HAVE_UNIXSOCKET */
|
---|
2016 | }
|
---|
2017 |
|
---|
2018 | /****************************************************************************
|
---|
2019 | Get my own canonical name, including domain.
|
---|
2020 | ****************************************************************************/
|
---|
2021 |
|
---|
2022 | const char *get_mydnsfullname(void)
|
---|
2023 | {
|
---|
2024 | struct addrinfo *res = NULL;
|
---|
2025 | char my_hostname[HOST_NAME_MAX];
|
---|
2026 | bool ret;
|
---|
2027 | DATA_BLOB tmp;
|
---|
2028 |
|
---|
2029 | if (memcache_lookup(NULL, SINGLETON_CACHE,
|
---|
2030 | data_blob_string_const("get_mydnsfullname"),
|
---|
2031 | &tmp)) {
|
---|
2032 | SMB_ASSERT(tmp.length > 0);
|
---|
2033 | return (const char *)tmp.data;
|
---|
2034 | }
|
---|
2035 |
|
---|
2036 | /* get my host name */
|
---|
2037 | if (gethostname(my_hostname, sizeof(my_hostname)) == -1) {
|
---|
2038 | DEBUG(0,("get_mydnsfullname: gethostname failed\n"));
|
---|
2039 | return NULL;
|
---|
2040 | }
|
---|
2041 |
|
---|
2042 | /* Ensure null termination. */
|
---|
2043 | my_hostname[sizeof(my_hostname)-1] = '\0';
|
---|
2044 |
|
---|
2045 | ret = interpret_string_addr_internal(&res,
|
---|
2046 | my_hostname,
|
---|
2047 | AI_ADDRCONFIG|AI_CANONNAME);
|
---|
2048 |
|
---|
2049 | if (!ret || res == NULL) {
|
---|
2050 | DEBUG(3,("get_mydnsfullname: getaddrinfo failed for "
|
---|
2051 | "name %s [%s]\n",
|
---|
2052 | my_hostname,
|
---|
2053 | gai_strerror(ret) ));
|
---|
2054 | return NULL;
|
---|
2055 | }
|
---|
2056 |
|
---|
2057 | /*
|
---|
2058 | * Make sure that getaddrinfo() returns the "correct" host name.
|
---|
2059 | */
|
---|
2060 |
|
---|
2061 | if (res->ai_canonname == NULL) {
|
---|
2062 | DEBUG(3,("get_mydnsfullname: failed to get "
|
---|
2063 | "canonical name for %s\n",
|
---|
2064 | my_hostname));
|
---|
2065 | freeaddrinfo(res);
|
---|
2066 | return NULL;
|
---|
2067 | }
|
---|
2068 |
|
---|
2069 | /* This copies the data, so we must do a lookup
|
---|
2070 | * afterwards to find the value to return.
|
---|
2071 | */
|
---|
2072 |
|
---|
2073 | memcache_add(NULL, SINGLETON_CACHE,
|
---|
2074 | data_blob_string_const("get_mydnsfullname"),
|
---|
2075 | data_blob_string_const(res->ai_canonname));
|
---|
2076 |
|
---|
2077 | if (!memcache_lookup(NULL, SINGLETON_CACHE,
|
---|
2078 | data_blob_string_const("get_mydnsfullname"),
|
---|
2079 | &tmp)) {
|
---|
2080 | tmp = data_blob_talloc(talloc_tos(), res->ai_canonname,
|
---|
2081 | strlen(res->ai_canonname) + 1);
|
---|
2082 | }
|
---|
2083 |
|
---|
2084 | freeaddrinfo(res);
|
---|
2085 |
|
---|
2086 | return (const char *)tmp.data;
|
---|
2087 | }
|
---|
2088 |
|
---|
2089 | /************************************************************
|
---|
2090 | Is this my name ?
|
---|
2091 | ************************************************************/
|
---|
2092 |
|
---|
2093 | bool is_myname_or_ipaddr(const char *s)
|
---|
2094 | {
|
---|
2095 | TALLOC_CTX *ctx = talloc_tos();
|
---|
2096 | char addr[INET6_ADDRSTRLEN];
|
---|
2097 | char *name = NULL;
|
---|
2098 | const char *dnsname;
|
---|
2099 | char *servername = NULL;
|
---|
2100 |
|
---|
2101 | if (!s) {
|
---|
2102 | return false;
|
---|
2103 | }
|
---|
2104 |
|
---|
2105 | /* Santize the string from '\\name' */
|
---|
2106 | name = talloc_strdup(ctx, s);
|
---|
2107 | if (!name) {
|
---|
2108 | return false;
|
---|
2109 | }
|
---|
2110 |
|
---|
2111 | servername = strrchr_m(name, '\\' );
|
---|
2112 | if (!servername) {
|
---|
2113 | servername = name;
|
---|
2114 | } else {
|
---|
2115 | servername++;
|
---|
2116 | }
|
---|
2117 |
|
---|
2118 | /* Optimize for the common case */
|
---|
2119 | if (strequal(servername, global_myname())) {
|
---|
2120 | return true;
|
---|
2121 | }
|
---|
2122 |
|
---|
2123 | /* Check for an alias */
|
---|
2124 | if (is_myname(servername)) {
|
---|
2125 | return true;
|
---|
2126 | }
|
---|
2127 |
|
---|
2128 | /* Check for loopback */
|
---|
2129 | if (strequal(servername, "127.0.0.1") ||
|
---|
2130 | strequal(servername, "::1")) {
|
---|
2131 | return true;
|
---|
2132 | }
|
---|
2133 |
|
---|
2134 | if (strequal(servername, "localhost")) {
|
---|
2135 | return true;
|
---|
2136 | }
|
---|
2137 |
|
---|
2138 | /* Maybe it's my dns name */
|
---|
2139 | dnsname = get_mydnsfullname();
|
---|
2140 | if (dnsname && strequal(servername, dnsname)) {
|
---|
2141 | return true;
|
---|
2142 | }
|
---|
2143 |
|
---|
2144 | /* Handle possible CNAME records - convert to an IP addr. */
|
---|
2145 | if (!is_ipaddress(servername)) {
|
---|
2146 | /* Use DNS to resolve the name, but only the first address */
|
---|
2147 | struct sockaddr_storage ss;
|
---|
2148 | if (interpret_string_addr(&ss, servername, 0)) {
|
---|
2149 | print_sockaddr(addr,
|
---|
2150 | sizeof(addr),
|
---|
2151 | &ss);
|
---|
2152 | servername = addr;
|
---|
2153 | }
|
---|
2154 | }
|
---|
2155 |
|
---|
2156 | /* Maybe its an IP address? */
|
---|
2157 | if (is_ipaddress(servername)) {
|
---|
2158 | struct sockaddr_storage ss;
|
---|
2159 | struct iface_struct *nics;
|
---|
2160 | int i, n;
|
---|
2161 |
|
---|
2162 | if (!interpret_string_addr(&ss, servername, AI_NUMERICHOST)) {
|
---|
2163 | return false;
|
---|
2164 | }
|
---|
2165 |
|
---|
2166 | if (is_zero_addr(&ss) || is_loopback_addr(&ss)) {
|
---|
2167 | return false;
|
---|
2168 | }
|
---|
2169 |
|
---|
2170 | nics = TALLOC_ARRAY(ctx, struct iface_struct,
|
---|
2171 | MAX_INTERFACES);
|
---|
2172 | if (!nics) {
|
---|
2173 | return false;
|
---|
2174 | }
|
---|
2175 | n = get_interfaces(nics, MAX_INTERFACES);
|
---|
2176 | for (i=0; i<n; i++) {
|
---|
2177 | if (sockaddr_equal(&nics[i].ip, &ss)) {
|
---|
2178 | TALLOC_FREE(nics);
|
---|
2179 | return true;
|
---|
2180 | }
|
---|
2181 | }
|
---|
2182 | TALLOC_FREE(nics);
|
---|
2183 | }
|
---|
2184 |
|
---|
2185 | /* No match */
|
---|
2186 | return false;
|
---|
2187 | }
|
---|