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
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/>.
24 /****************************************************************************
25 Return true if a string could be an IPv4 address.
26 ****************************************************************************/
28 bool is_ipaddress_v4(const char *str
)
33 ret
= inet_pton(AF_INET
, str
, &dest
);
40 /****************************************************************************
41 Return true if a string could be an IPv4 or IPv6 address.
42 ****************************************************************************/
44 bool is_ipaddress(const char *str
)
46 #if defined(HAVE_IPV6)
49 if (strchr_m(str
, ':')) {
50 char addr
[INET6_ADDRSTRLEN
];
51 struct in6_addr dest6
;
53 char *p
= strchr_m(str
, '%');
56 * Cope with link-local.
57 * This is IP:v6:addr%ifname.
60 if (p
&& (p
> str
) && (if_nametoindex(p
+1) != 0)) {
62 MIN(PTR_DIFF(p
,str
)+1,
66 ret
= inet_pton(AF_INET6
, sp
, &dest6
);
72 return is_ipaddress_v4(str
);
75 /****************************************************************************
76 Is a sockaddr_storage a broadcast address ?
77 ****************************************************************************/
79 bool is_broadcast_addr(const struct sockaddr_storage
*pss
)
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
);
88 if (pss
->ss_family
== AF_INET
) {
90 ntohl(((const struct sockaddr_in
*)pss
)->sin_addr
.s_addr
);
91 return addr
== INADDR_BROADCAST
;
96 /*******************************************************************
98 ******************************************************************/
100 static bool interpret_string_addr_internal(struct addrinfo
**ppres
,
101 const char *str
, int flags
)
104 struct addrinfo hints
;
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
;
111 /* Linux man page on getaddinfo() says port will be
112 uninitialized when service string in NULL */
114 ret
= getaddrinfo(str
, NULL
,
119 DEBUG(3,("interpret_string_addr_internal: getaddrinfo failed "
120 "for name %s [%s]\n",
122 gai_strerror(ret
) ));
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 ****************************************************************************/
133 uint32
interpret_addr(const char *str
)
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
)) {
142 if (inet_pton(AF_INET
, str
, &dest
) <= 0) {
143 /* Error - this shouldn't happen ! */
144 DEBUG(0,("interpret_addr: inet_pton failed "
149 ret
= dest
.s_addr
; /* NETWORK BYTE ORDER ! */
151 /* Otherwise assume it's a network name of some sort and use
153 struct addrinfo
*res
= NULL
;
154 struct addrinfo
*res_list
= NULL
;
155 if (!interpret_string_addr_internal(&res_list
,
158 DEBUG(3,("interpret_addr: Unknown host. %s\n",str
));
162 /* Find the first IPv4 address. */
163 for (res
= res_list
; res
; res
= res
->ai_next
) {
164 if (res
->ai_family
!= AF_INET
) {
167 if (res
->ai_addr
== NULL
) {
173 DEBUG(3,("interpret_addr: host address is "
174 "invalid for host %s\n",str
));
176 freeaddrinfo(res_list
);
181 &((struct sockaddr_in
*)res
->ai_addr
)->sin_addr
.s_addr
);
183 freeaddrinfo(res_list
);
187 /* This is so bogus - all callers need fixing... JRA. */
188 if (ret
== (uint32
)-1) {
195 /*******************************************************************
196 A convenient addition to interpret_addr().
197 ******************************************************************/
199 struct in_addr
*interpret_addr2(struct in_addr
*ip
, const char *str
)
201 uint32 a
= interpret_addr(str
);
206 /*******************************************************************
207 Map a text hostname or IP address (IPv4 or IPv6) into a
208 struct sockaddr_storage.
209 ******************************************************************/
211 bool interpret_string_addr(struct sockaddr_storage
*pss
,
215 struct addrinfo
*res
= NULL
;
216 #if defined(HAVE_IPV6)
217 char addr
[INET6_ADDRSTRLEN
];
218 unsigned int scope_id
= 0;
220 if (strchr_m(str
, ':')) {
221 char *p
= strchr_m(str
, '%');
224 * Cope with link-local.
225 * This is IP:v6:addr%ifname.
228 if (p
&& (p
> str
) && ((scope_id
= if_nametoindex(p
+1)) != 0)) {
230 MIN(PTR_DIFF(p
,str
)+1,
239 if (!interpret_string_addr_internal(&res
, str
, flags
|AI_ADDRCONFIG
)) {
245 /* Copy the first sockaddr. */
246 memcpy(pss
, res
->ai_addr
, res
->ai_addrlen
);
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
;
262 /*******************************************************************
263 Check if an IPv7 is 127.0.0.1
264 ******************************************************************/
266 bool is_loopback_ip_v4(struct in_addr ip
)
269 a
.s_addr
= htonl(INADDR_LOOPBACK
);
270 return(ip
.s_addr
== a
.s_addr
);
273 /*******************************************************************
274 Check if a struct sockaddr_storage is the loopback address.
275 ******************************************************************/
277 bool is_loopback_addr(const struct sockaddr_storage
*pss
)
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
);
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
);
293 /*******************************************************************
294 Check if an IPv4 is 0.0.0.0.
295 ******************************************************************/
297 bool is_zero_ip_v4(struct in_addr ip
)
300 putip((char *)&a
,(char *)&ip
);
304 /*******************************************************************
305 Check if a struct sockaddr_storage has an unspecified address.
306 ******************************************************************/
308 bool is_zero_addr(const struct sockaddr_storage
*pss
)
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
);
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
);
324 /*******************************************************************
325 Set an IP to 0.0.0.0.
326 ******************************************************************/
328 void zero_ip_v4(struct in_addr
*ip
)
330 memset(ip
, '\0', sizeof(struct in_addr
));
333 /*******************************************************************
334 Set an address to INADDR_ANY.
335 ******************************************************************/
337 void zero_sockaddr(struct sockaddr_storage
*pss
)
339 memset(pss
, '\0', sizeof(*pss
));
340 /* Ensure we're at least a valid sockaddr-storage. */
341 pss
->ss_family
= AF_INET
;
344 /*******************************************************************
345 Are two IPs on the same subnet - IPv4 version ?
346 ********************************************************************/
348 bool same_net_v4(struct in_addr ip1
,struct in_addr ip2
,struct in_addr mask
)
350 uint32 net1
,net2
,nmask
;
352 nmask
= ntohl(mask
.s_addr
);
353 net1
= ntohl(ip1
.s_addr
);
354 net2
= ntohl(ip2
.s_addr
);
356 return((net1
& nmask
) == (net2
& nmask
));
359 /*******************************************************************
360 Convert an IPv4 struct in_addr to a struct sockaddr_storage.
361 ********************************************************************/
363 void in_addr_to_sockaddr_storage(struct sockaddr_storage
*ss
,
366 struct sockaddr_in
*sa
= (struct sockaddr_in
*)ss
;
367 memset(ss
, '\0', sizeof(*ss
));
368 sa
->sin_family
= AF_INET
;
372 #if defined(HAVE_IPV6)
373 /*******************************************************************
374 Convert an IPv6 struct in_addr to a struct sockaddr_storage.
375 ********************************************************************/
377 void in6_addr_to_sockaddr_storage(struct sockaddr_storage
*ss
,
380 struct sockaddr_in6
*sa
= (struct sockaddr_in6
*)ss
;
381 memset(ss
, '\0', sizeof(*ss
));
382 sa
->sin6_family
= AF_INET6
;
387 /*******************************************************************
388 Are two IPs on the same subnet?
389 ********************************************************************/
391 bool same_net(const struct sockaddr_storage
*ip1
,
392 const struct sockaddr_storage
*ip2
,
393 const struct sockaddr_storage
*mask
)
395 if (ip1
->ss_family
!= ip2
->ss_family
) {
396 /* Never on the same net. */
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
;
410 for (i
= 0; i
< sizeof(struct in6_addr
); i
++) {
415 return (memcmp(&ip1_6
.sin6_addr
,
417 sizeof(struct in6_addr
)) == 0);
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
);
428 /*******************************************************************
429 Are two sockaddr_storage's the same family and address ? Ignore port etc.
430 ********************************************************************/
432 bool sockaddr_equal(const struct sockaddr_storage
*ip1
,
433 const struct sockaddr_storage
*ip2
)
435 if (ip1
->ss_family
!= ip2
->ss_family
) {
436 /* Never the same. */
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);
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);
455 /****************************************************************************
456 Is an IP address the INADDR_ANY or in6addr_any value ?
457 ****************************************************************************/
459 bool is_address_any(const struct sockaddr_storage
*psa
)
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
,
466 sizeof(in6addr_any
)) == 0) {
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
) {
482 /****************************************************************************
483 Get a port number in host byte order from a sockaddr_storage.
484 ****************************************************************************/
486 uint16_t get_sockaddr_port(const struct sockaddr_storage
*pss
)
490 if (pss
->ss_family
!= AF_INET
) {
491 #if defined(HAVE_IPV6)
493 const struct sockaddr_in6
*sa6
=
494 (const struct sockaddr_in6
*)pss
;
495 port
= ntohs(sa6
->sin6_port
);
498 const struct sockaddr_in
*sa
=
499 (const struct sockaddr_in
*)pss
;
500 port
= ntohs(sa
->sin_port
);
505 /****************************************************************************
506 Print out an IPv4 or IPv6 address from a struct sockaddr_storage.
507 ****************************************************************************/
509 static char *print_sockaddr_len(char *dest
,
511 const struct sockaddr_storage
*psa
,
517 (void)sys_getnameinfo((const struct sockaddr
*)psa
,
525 /****************************************************************************
526 Print out an IPv4 or IPv6 address from a struct sockaddr_storage.
527 ****************************************************************************/
529 char *print_sockaddr(char *dest
,
531 const struct sockaddr_storage
*psa
)
533 return print_sockaddr_len(dest
, destlen
, psa
,
534 sizeof(struct sockaddr_storage
));
537 /****************************************************************************
538 Print out a canonical IPv4 or IPv6 address from a struct sockaddr_storage.
539 ****************************************************************************/
541 char *print_canonical_sockaddr(TALLOC_CTX
*ctx
,
542 const struct sockaddr_storage
*pss
)
544 char addr
[INET6_ADDRSTRLEN
];
548 /* Linux getnameinfo() man pages says port is unitialized if
549 service name is NULL. */
551 ret
= sys_getnameinfo((const struct sockaddr
*)pss
,
552 sizeof(struct sockaddr_storage
),
560 if (pss
->ss_family
!= AF_INET
) {
561 #if defined(HAVE_IPV6)
562 dest
= talloc_asprintf(ctx
, "[%s]", addr
);
567 dest
= talloc_asprintf(ctx
, "%s", addr
);
573 /****************************************************************************
574 Return the string of an IP address (IPv4 or IPv6).
575 ****************************************************************************/
577 static const char *get_socket_addr(int fd
, char *addr_buf
, size_t addr_len
)
579 struct sockaddr_storage sa
;
580 socklen_t length
= sizeof(sa
);
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.
587 strlcpy(addr_buf
, "0.0.0.0", addr_len
);
593 if (getsockname(fd
, (struct sockaddr
*)&sa
, &length
) < 0) {
594 DEBUG(0,("getsockname failed. Error was %s\n",
599 return print_sockaddr_len(addr_buf
, addr_len
, &sa
, length
);
603 /* Not currently used. JRA. */
604 /****************************************************************************
605 Return the port number we've bound to on a socket.
606 ****************************************************************************/
608 static int get_socket_port(int fd
)
610 struct sockaddr_storage sa
;
611 socklen_t length
= sizeof(sa
);
617 if (getsockname(fd
, (struct sockaddr
*)&sa
, &length
) < 0) {
618 DEBUG(0,("getpeername failed. Error was %s\n",
623 #if defined(HAVE_IPV6)
624 if (sa
.ss_family
== AF_INET6
) {
625 return ntohs(((struct sockaddr_in6
*)&sa
)->sin6_port
);
628 if (sa
.ss_family
== AF_INET
) {
629 return ntohs(((struct sockaddr_in
*)&sa
)->sin_port
);
635 void set_sockaddr_port(struct sockaddr_storage
*psa
, uint16 port
)
637 #if defined(HAVE_IPV6)
638 if (psa
->ss_family
== AF_INET6
) {
639 ((struct sockaddr_in6
*)psa
)->sin6_port
= htons(port
);
642 if (psa
->ss_family
== AF_INET
) {
643 ((struct sockaddr_in
*)psa
)->sin_port
= htons(port
);
647 const char *client_name(int fd
)
649 return get_peer_name(fd
,false);
652 const char *client_addr(int fd
, char *addr
, size_t addrlen
)
654 return get_peer_addr(fd
,addr
,addrlen
);
657 const char *client_socket_addr(int fd
, char *addr
, size_t addr_len
)
659 return get_socket_addr(fd
, addr
, addr_len
);
663 /* Not currently used. JRA. */
664 int client_socket_port(int fd
)
666 return get_socket_port(fd
);
670 /****************************************************************************
671 Accessor functions to make thread-safe code easier later...
672 ****************************************************************************/
674 void set_smb_read_error(enum smb_read_errors
*pre
,
675 enum smb_read_errors newerr
)
682 void cond_set_smb_read_error(enum smb_read_errors
*pre
,
683 enum smb_read_errors newerr
)
685 if (pre
&& *pre
== SMB_READ_OK
) {
690 /****************************************************************************
691 Determine if a file descriptor is in fact a socket.
692 ****************************************************************************/
694 bool is_a_socket(int fd
)
699 return(getsockopt(fd
, SOL_SOCKET
, SO_TYPE
, (char *)&v
, &l
) == 0);
702 enum SOCK_OPT_TYPES
{OPT_BOOL
,OPT_INT
,OPT_ON
};
704 typedef struct smb_socket_option
{
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
},
717 {"TCP_NODELAY", IPPROTO_TCP
, TCP_NODELAY
, 0, OPT_BOOL
},
720 {"TCP_KEEPCNT", IPPROTO_TCP
, TCP_KEEPCNT
, 0, OPT_INT
},
723 {"TCP_KEEPIDLE", IPPROTO_TCP
, TCP_KEEPIDLE
, 0, OPT_INT
},
726 {"TCP_KEEPINTVL", IPPROTO_TCP
, TCP_KEEPINTVL
, 0, OPT_INT
},
728 #ifdef IPTOS_LOWDELAY
729 {"IPTOS_LOWDELAY", IPPROTO_IP
, IP_TOS
, IPTOS_LOWDELAY
, OPT_ON
},
731 #ifdef IPTOS_THROUGHPUT
732 {"IPTOS_THROUGHPUT", IPPROTO_IP
, IP_TOS
, IPTOS_THROUGHPUT
, OPT_ON
},
735 {"SO_REUSEPORT", SOL_SOCKET
, SO_REUSEPORT
, 0, OPT_BOOL
},
738 {"SO_SNDBUF", SOL_SOCKET
, SO_SNDBUF
, 0, OPT_INT
},
741 {"SO_RCVBUF", SOL_SOCKET
, SO_RCVBUF
, 0, OPT_INT
},
744 {"SO_SNDLOWAT", SOL_SOCKET
, SO_SNDLOWAT
, 0, OPT_INT
},
747 {"SO_RCVLOWAT", SOL_SOCKET
, SO_RCVLOWAT
, 0, OPT_INT
},
750 {"SO_SNDTIMEO", SOL_SOCKET
, SO_SNDTIMEO
, 0, OPT_INT
},
753 {"SO_RCVTIMEO", SOL_SOCKET
, SO_RCVTIMEO
, 0, OPT_INT
},
756 {"TCP_FASTACK", IPPROTO_TCP
, TCP_FASTACK
, 0, OPT_INT
},
760 /****************************************************************************
761 Print socket options.
762 ****************************************************************************/
764 static void print_socket_options(int s
)
768 const smb_socket_option
*p
= &socket_options
[0];
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",
780 DEBUG(5,("socket option %s = %d\n",
787 /****************************************************************************
788 Set user socket options.
789 ****************************************************************************/
791 void set_socket_options(int fd
, const char *options
)
793 TALLOC_CTX
*ctx
= talloc_stackframe();
796 while (next_token_talloc(ctx
, &options
, &tok
," \t,")) {
800 bool got_value
= false;
802 if ((p
= strchr_m(tok
,'='))) {
808 for (i
=0;socket_options
[i
].name
;i
++)
809 if (strequal(socket_options
[i
].name
,tok
))
812 if (!socket_options
[i
].name
) {
813 DEBUG(0,("Unknown socket option %s\n",tok
));
817 switch (socket_options
[i
].opttype
) {
820 ret
= setsockopt(fd
,socket_options
[i
].level
,
821 socket_options
[i
].option
,
822 (char *)&value
,sizeof(int));
827 DEBUG(0,("syntax error - %s "
828 "does not take a value\n",tok
));
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));
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
) ));
849 print_socket_options(fd
);
852 /****************************************************************************
854 ****************************************************************************/
856 ssize_t
read_udp_v4_socket(int fd
,
859 struct sockaddr_storage
*psa
)
862 socklen_t socklen
= sizeof(*psa
);
863 struct sockaddr_in
*si
= (struct sockaddr_in
*)psa
;
865 memset((char *)psa
,'\0',socklen
);
867 ret
= (ssize_t
)sys_recvfrom(fd
,buf
,len
,0,
868 (struct sockaddr
*)psa
,&socklen
);
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"));
874 DEBUG(2,("read_udp_v4_socket: failed. errno=%s\n",
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
));
886 DEBUG(10,("read_udp_v4_socket: ip %s port %d read: %lu\n",
887 inet_ntoa(si
->sin_addr
),
889 (unsigned long)ret
));
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 ****************************************************************************/
901 NTSTATUS
read_socket_with_timeout(int fd
, char *buf
,
902 size_t mincnt
, size_t maxcnt
,
903 unsigned int time_out
,
910 struct timeval timeout
;
911 char addr
[INET6_ADDRSTRLEN
];
913 /* just checking .... */
923 while (nread
< mincnt
) {
924 readret
= sys_read(fd
, buf
+ nread
, maxcnt
- nread
);
927 DEBUG(5,("read_socket_with_timeout: "
928 "blocking read. EOF from client.\n"));
929 return NT_STATUS_END_OF_FILE
;
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
)),
941 DEBUG(0,("read_socket_with_timeout: "
942 "read error = %s.\n",
945 return map_nt_error_from_unix(errno
);
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 */
958 /* Set initial timeout */
959 timeout
.tv_sec
= (time_t)(time_out
/ 1000);
960 timeout
.tv_usec
= (long)(1000 * (time_out
% 1000));
962 for (nread
=0; nread
< mincnt
; ) {
966 selrtn
= sys_select_intr(fd
+1,&fds
,NULL
,NULL
,&timeout
);
970 /* something is wrong. Maybe the socket is dead? */
971 if (fd
== get_client_fd()) {
972 /* Try and give an error message saying
973 * what client failed. */
974 DEBUG(0,("read_socket_with_timeout: timeout "
975 "read for client %s. select error = %s.\n",
976 get_peer_addr(fd
,addr
,sizeof(addr
)),
979 DEBUG(0,("read_socket_with_timeout: timeout "
980 "read. select error = %s.\n",
983 return map_nt_error_from_unix(errno
);
986 /* Did we timeout ? */
988 DEBUG(10,("read_socket_with_timeout: timeout read. "
989 "select timed out.\n"));
990 return NT_STATUS_IO_TIMEOUT
;
993 readret
= sys_read(fd
, buf
+nread
, maxcnt
-nread
);
996 /* we got EOF on the file descriptor */
997 DEBUG(5,("read_socket_with_timeout: timeout read. "
998 "EOF from client.\n"));
999 return NT_STATUS_END_OF_FILE
;
1002 if (readret
== -1) {
1003 /* the descriptor is probably dead */
1004 if (fd
== get_client_fd()) {
1005 /* Try and give an error message
1006 * saying what client failed. */
1007 DEBUG(0,("read_socket_with_timeout: timeout "
1008 "read to client %s. read error = %s.\n",
1009 get_peer_addr(fd
,addr
,sizeof(addr
)),
1012 DEBUG(0,("read_socket_with_timeout: timeout "
1013 "read. read error = %s.\n",
1016 return map_nt_error_from_unix(errno
);
1023 /* Return the number we got */
1027 return NT_STATUS_OK
;
1030 /****************************************************************************
1031 Read data from the client, reading exactly N bytes.
1032 ****************************************************************************/
1034 NTSTATUS
read_data(int fd
, char *buffer
, size_t N
)
1036 return read_socket_with_timeout(fd
, buffer
, N
, N
, 0, NULL
);
1039 /****************************************************************************
1040 Write all data from an iov array
1041 ****************************************************************************/
1043 ssize_t
write_data_iov(int fd
, const struct iovec
*orig_iov
, int iovcnt
)
1049 struct iovec
*iov_copy
, *iov
;
1052 for (i
=0; i
<iovcnt
; i
++) {
1053 to_send
+= orig_iov
[i
].iov_len
;
1056 thistime
= sys_writev(fd
, orig_iov
, iovcnt
);
1057 if ((thistime
<= 0) || (thistime
== to_send
)) {
1063 * We could not send everything in one call. Make a copy of iov that
1064 * we can mess with. We keep a copy of the array start in iov_copy for
1065 * the TALLOC_FREE, because we're going to modify iov later on,
1066 * discarding elements.
1069 iov_copy
= (struct iovec
*)TALLOC_MEMDUP(
1070 talloc_tos(), orig_iov
, sizeof(struct iovec
) * iovcnt
);
1072 if (iov_copy
== NULL
) {
1078 while (sent
< to_send
) {
1080 * We have to discard "thistime" bytes from the beginning
1081 * iov array, "thistime" contains the number of bytes sent
1084 while (thistime
> 0) {
1085 if (thistime
< iov
[0].iov_len
) {
1087 (char *)iov
[0].iov_base
+ thistime
;
1088 iov
[0].iov_base
= new_base
;
1089 iov
[0].iov_len
-= thistime
;
1092 thistime
-= iov
[0].iov_len
;
1097 thistime
= sys_writev(fd
, iov
, iovcnt
);
1098 if (thistime
<= 0) {
1104 TALLOC_FREE(iov_copy
);
1108 /****************************************************************************
1110 ****************************************************************************/
1112 /****************************************************************************
1114 ****************************************************************************/
1116 ssize_t
write_data(int fd
, const char *buffer
, size_t N
)
1121 iov
.iov_base
= CONST_DISCARD(char *, buffer
);
1124 ret
= write_data_iov(fd
, &iov
, 1);
1129 if (fd
== get_client_fd()) {
1130 char addr
[INET6_ADDRSTRLEN
];
1132 * Try and give an error message saying what client failed.
1134 DEBUG(0, ("write_data: write failure in writing to client %s. "
1135 "Error %s\n", get_peer_addr(fd
,addr
,sizeof(addr
)),
1138 DEBUG(0,("write_data: write failure. Error = %s\n",
1145 /****************************************************************************
1146 Send a keepalive packet (rfc1002).
1147 ****************************************************************************/
1149 bool send_keepalive(int client
)
1151 unsigned char buf
[4];
1153 buf
[0] = SMBkeepalive
;
1154 buf
[1] = buf
[2] = buf
[3] = 0;
1156 return(write_data(client
,(char *)buf
,4) == 4);
1159 /****************************************************************************
1160 Read 4 bytes of a smb packet and return the smb length of the packet.
1161 Store the result in the buffer.
1162 This version of the function will return a length of zero on receiving
1164 Timeout is in milliseconds.
1165 ****************************************************************************/
1167 NTSTATUS
read_smb_length_return_keepalive(int fd
, char *inbuf
,
1168 unsigned int timeout
,
1174 status
= read_socket_with_timeout(fd
, inbuf
, 4, 4, timeout
, NULL
);
1176 if (!NT_STATUS_IS_OK(status
)) {
1180 *len
= smb_len(inbuf
);
1181 msg_type
= CVAL(inbuf
,0);
1183 if (msg_type
== SMBkeepalive
) {
1184 DEBUG(5,("Got keepalive packet\n"));
1187 DEBUG(10,("got smb length of %lu\n",(unsigned long)(*len
)));
1189 return NT_STATUS_OK
;
1192 /****************************************************************************
1193 Read 4 bytes of a smb packet and return the smb length of the packet.
1194 Store the result in the buffer. This version of the function will
1195 never return a session keepalive (length of zero).
1196 Timeout is in milliseconds.
1197 ****************************************************************************/
1199 NTSTATUS
read_smb_length(int fd
, char *inbuf
, unsigned int timeout
,
1202 uint8_t msgtype
= SMBkeepalive
;
1204 while (msgtype
== SMBkeepalive
) {
1207 status
= read_smb_length_return_keepalive(fd
, inbuf
, timeout
,
1209 if (!NT_STATUS_IS_OK(status
)) {
1213 msgtype
= CVAL(inbuf
, 0);
1216 DEBUG(10,("read_smb_length: got smb length of %lu\n",
1217 (unsigned long)len
));
1219 return NT_STATUS_OK
;
1222 /****************************************************************************
1223 Read an smb from a fd.
1224 The timeout is in milliseconds.
1225 This function will return on receipt of a session keepalive packet.
1226 maxlen is the max number of bytes to return, not including the 4 byte
1227 length. If zero it means buflen limit.
1228 Doesn't check the MAC on signed packets.
1229 ****************************************************************************/
1231 NTSTATUS
receive_smb_raw(int fd
, char *buffer
, size_t buflen
, unsigned int timeout
,
1232 size_t maxlen
, size_t *p_len
)
1237 status
= read_smb_length_return_keepalive(fd
,buffer
,timeout
,&len
);
1239 if (!NT_STATUS_IS_OK(status
)) {
1240 DEBUG(10, ("receive_smb_raw: %s!\n", nt_errstr(status
)));
1245 DEBUG(0,("Invalid packet length! (%lu bytes).\n",
1246 (unsigned long)len
));
1247 return NT_STATUS_INVALID_PARAMETER
;
1252 len
= MIN(len
,maxlen
);
1255 status
= read_socket_with_timeout(
1256 fd
, buffer
+4, len
, len
, timeout
, &len
);
1258 if (!NT_STATUS_IS_OK(status
)) {
1262 /* not all of samba3 properly checks for packet-termination
1263 * of strings. This ensures that we don't run off into
1265 SSVAL(buffer
+4,len
, 0);
1269 return NT_STATUS_OK
;
1272 /****************************************************************************
1273 Open a socket of the specified type, port, and address for incoming data.
1274 ****************************************************************************/
1276 int open_socket_in(int type
,
1279 const struct sockaddr_storage
*psock
,
1282 struct sockaddr_storage sock
;
1284 socklen_t slen
= sizeof(struct sockaddr_in
);
1288 #if defined(HAVE_IPV6)
1289 if (sock
.ss_family
== AF_INET6
) {
1290 ((struct sockaddr_in6
*)&sock
)->sin6_port
= htons(port
);
1291 slen
= sizeof(struct sockaddr_in6
);
1294 if (sock
.ss_family
== AF_INET
) {
1295 ((struct sockaddr_in
*)&sock
)->sin_port
= htons(port
);
1298 res
= socket(sock
.ss_family
, type
, 0 );
1301 dbgtext( "open_socket_in(): socket() call failed: " );
1302 dbgtext( "%s\n", strerror( errno
) );
1307 /* This block sets/clears the SO_REUSEADDR and possibly SO_REUSEPORT. */
1309 int val
= rebind
? 1 : 0;
1310 if( setsockopt(res
,SOL_SOCKET
,SO_REUSEADDR
,
1311 (char *)&val
,sizeof(val
)) == -1 ) {
1312 if( DEBUGLVL( dlevel
) ) {
1313 dbgtext( "open_socket_in(): setsockopt: " );
1314 dbgtext( "SO_REUSEADDR = %s ",
1315 val
?"true":"false" );
1316 dbgtext( "on port %d failed ", port
);
1317 dbgtext( "with error = %s\n", strerror(errno
) );
1321 if( setsockopt(res
,SOL_SOCKET
,SO_REUSEPORT
,
1322 (char *)&val
,sizeof(val
)) == -1 ) {
1323 if( DEBUGLVL( dlevel
) ) {
1324 dbgtext( "open_socket_in(): setsockopt: ");
1325 dbgtext( "SO_REUSEPORT = %s ",
1326 val
?"true":"false");
1327 dbgtext( "on port %d failed ", port
);
1328 dbgtext( "with error = %s\n", strerror(errno
));
1331 #endif /* SO_REUSEPORT */
1334 /* now we've got a socket - we need to bind it */
1335 if (bind(res
, (struct sockaddr
*)&sock
, slen
) == -1 ) {
1336 if( DEBUGLVL(dlevel
) && (port
== SMB_PORT1
||
1337 port
== SMB_PORT2
|| port
== NMB_PORT
) ) {
1338 char addr
[INET6_ADDRSTRLEN
];
1339 print_sockaddr(addr
, sizeof(addr
),
1341 dbgtext( "bind failed on port %d ", port
);
1342 dbgtext( "socket_addr = %s.\n", addr
);
1343 dbgtext( "Error = %s\n", strerror(errno
));
1349 DEBUG( 10, ( "bind succeeded on port %d\n", port
) );
1353 /****************************************************************************
1354 Create an outgoing socket. timeout is in milliseconds.
1355 **************************************************************************/
1357 int open_socket_out(int type
,
1358 const struct sockaddr_storage
*pss
,
1362 char addr
[INET6_ADDRSTRLEN
];
1363 struct sockaddr_storage sock_out
= *pss
;
1365 int connect_loop
= 10;
1368 /* create a socket to write to */
1369 res
= socket(pss
->ss_family
, type
, 0);
1371 DEBUG(0,("socket error (%s)\n", strerror(errno
)));
1375 if (type
!= SOCK_STREAM
) {
1379 #if defined(HAVE_IPV6)
1380 if (pss
->ss_family
== AF_INET6
) {
1381 struct sockaddr_in6
*psa6
= (struct sockaddr_in6
*)&sock_out
;
1382 psa6
->sin6_port
= htons(port
);
1383 if (psa6
->sin6_scope_id
== 0 &&
1384 IN6_IS_ADDR_LINKLOCAL(&psa6
->sin6_addr
)) {
1385 setup_linklocal_scope_id(&sock_out
);
1389 if (pss
->ss_family
== AF_INET
) {
1390 struct sockaddr_in
*psa
= (struct sockaddr_in
*)&sock_out
;
1391 psa
->sin_port
= htons(port
);
1394 /* set it non-blocking */
1395 set_blocking(res
,false);
1397 print_sockaddr(addr
, sizeof(addr
), &sock_out
);
1398 DEBUG(3,("Connecting to %s at port %u\n",
1400 (unsigned int)port
));
1402 /* and connect it to the destination */
1405 ret
= sys_connect(res
, (struct sockaddr
*)&sock_out
);
1407 /* Some systems return EAGAIN when they mean EINPROGRESS */
1408 if (ret
< 0 && (errno
== EINPROGRESS
|| errno
== EALREADY
||
1409 errno
== EAGAIN
) && (connect_loop
< timeout
) ) {
1410 smb_msleep(connect_loop
);
1411 timeout
-= connect_loop
;
1412 connect_loop
+= increment
;
1413 if (increment
< 250) {
1414 /* After 8 rounds we end up at a max of 255 msec */
1420 if (ret
< 0 && (errno
== EINPROGRESS
|| errno
== EALREADY
||
1422 DEBUG(1,("timeout connecting to %s:%u\n",
1424 (unsigned int)port
));
1430 if (ret
< 0 && errno
== EISCONN
) {
1437 DEBUG(2,("error connecting to %s:%d (%s)\n",
1445 /* set it blocking again */
1446 set_blocking(res
,true);
1451 /*******************************************************************
1452 Create an outgoing TCP socket to the first addr that connects.
1454 This is for simultaneous connection attempts to port 445 and 139 of a host
1455 or for simultatneous connection attempts to multiple DCs at once. We return
1456 a socket fd of the first successful connection.
1458 @param[in] addrs list of Internet addresses and ports to connect to
1459 @param[in] num_addrs number of address/port pairs in the addrs list
1460 @param[in] timeout time after which we stop waiting for a socket connection
1461 to succeed, given in milliseconds
1462 @param[out] fd_index the entry in addrs which we successfully connected to
1463 @param[out] fd fd of the open and connected socket
1464 @return true on a successful connection, false if all connection attempts
1465 failed or we timed out
1466 *******************************************************************/
1468 bool open_any_socket_out(struct sockaddr_storage
*addrs
, int num_addrs
,
1469 int timeout
, int *fd_index
, int *fd
)
1471 int i
, resulting_index
, res
;
1475 fd_set r_fds
, wr_fds
;
1479 int connect_loop
= 10000; /* 10 milliseconds */
1481 timeout
*= 1000; /* convert to microseconds */
1483 sockets
= SMB_MALLOC_ARRAY(int, num_addrs
);
1485 if (sockets
== NULL
)
1488 resulting_index
= -1;
1490 for (i
=0; i
<num_addrs
; i
++)
1493 for (i
=0; i
<num_addrs
; i
++) {
1494 sockets
[i
] = socket(addrs
[i
].ss_family
, SOCK_STREAM
, 0);
1497 set_blocking(sockets
[i
], false);
1501 good_connect
= false;
1503 for (i
=0; i
<num_addrs
; i
++) {
1504 const struct sockaddr
* a
=
1505 (const struct sockaddr
*)&(addrs
[i
]);
1507 if (sockets
[i
] == -1)
1510 if (sys_connect(sockets
[i
], a
) == 0) {
1511 /* Rather unlikely as we are non-blocking, but it
1512 * might actually happen. */
1513 resulting_index
= i
;
1517 if (errno
== EINPROGRESS
|| errno
== EALREADY
||
1521 errno
== EAGAIN
|| errno
== EINTR
) {
1522 /* These are the error messages that something is
1524 good_connect
= true;
1525 } else if (errno
!= 0) {
1526 /* There was a direct error */
1532 if (!good_connect
) {
1533 /* All of the connect's resulted in real error conditions */
1537 /* Lets see if any of the connect attempts succeeded */
1543 for (i
=0; i
<num_addrs
; i
++) {
1544 if (sockets
[i
] == -1)
1546 FD_SET(sockets
[i
], &wr_fds
);
1547 FD_SET(sockets
[i
], &r_fds
);
1548 if (sockets
[i
]>maxfd
)
1553 tv
.tv_usec
= connect_loop
;
1555 res
= sys_select_intr(maxfd
+1, &r_fds
, &wr_fds
, NULL
, &tv
);
1563 for (i
=0; i
<num_addrs
; i
++) {
1565 if (sockets
[i
] == -1)
1568 /* Stevens, Network Programming says that if there's a
1569 * successful connect, the socket is only writable. Upon an
1570 * error, it's both readable and writable. */
1572 if (FD_ISSET(sockets
[i
], &r_fds
) &&
1573 FD_ISSET(sockets
[i
], &wr_fds
)) {
1574 /* readable and writable, so it's an error */
1580 if (!FD_ISSET(sockets
[i
], &r_fds
) &&
1581 FD_ISSET(sockets
[i
], &wr_fds
)) {
1582 /* Only writable, so it's connected */
1583 resulting_index
= i
;
1590 timeout
-= connect_loop
;
1593 connect_loop
*= 1.5;
1594 if (connect_loop
> timeout
)
1595 connect_loop
= timeout
;
1599 for (i
=0; i
<num_addrs
; i
++) {
1600 if (i
== resulting_index
)
1602 if (sockets
[i
] >= 0)
1606 if (resulting_index
>= 0) {
1607 *fd_index
= resulting_index
;
1608 *fd
= sockets
[*fd_index
];
1609 set_blocking(*fd
, true);
1614 return (resulting_index
>= 0);
1616 /****************************************************************************
1617 Open a connected UDP socket to host on port
1618 **************************************************************************/
1620 int open_udp_socket(const char *host
, int port
)
1622 int type
= SOCK_DGRAM
;
1623 struct sockaddr_in sock_out
;
1625 struct in_addr addr
;
1627 (void)interpret_addr2(&addr
, host
);
1629 res
= socket(PF_INET
, type
, 0);
1634 memset((char *)&sock_out
,'\0',sizeof(sock_out
));
1635 putip((char *)&sock_out
.sin_addr
,(char *)&addr
);
1636 sock_out
.sin_port
= htons(port
);
1637 sock_out
.sin_family
= PF_INET
;
1639 if (sys_connect(res
,(struct sockaddr
*)&sock_out
)) {
1647 /*******************************************************************
1648 Return the IP addr of the remote end of a socket as a string.
1649 Optionally return the struct sockaddr_storage.
1650 ******************************************************************/
1652 static const char *get_peer_addr_internal(int fd
,
1654 size_t addr_buf_len
,
1655 struct sockaddr_storage
*pss
,
1658 struct sockaddr_storage ss
;
1659 socklen_t length
= sizeof(ss
);
1661 strlcpy(addr_buf
,"0.0.0.0",addr_buf_len
);
1670 if (plength
== NULL
) {
1674 if (getpeername(fd
, (struct sockaddr
*)pss
, plength
) < 0) {
1675 DEBUG(0,("getpeername failed. Error was %s\n",
1680 print_sockaddr_len(addr_buf
,
1687 /*******************************************************************
1688 Matchname - determine if host name matches IP address. Used to
1689 confirm a hostname lookup to prevent spoof attacks.
1690 ******************************************************************/
1692 static bool matchname(const char *remotehost
,
1693 const struct sockaddr_storage
*pss
,
1696 struct addrinfo
*res
= NULL
;
1697 struct addrinfo
*ailist
= NULL
;
1698 char addr_buf
[INET6_ADDRSTRLEN
];
1699 bool ret
= interpret_string_addr_internal(&ailist
,
1701 AI_ADDRCONFIG
|AI_CANONNAME
);
1703 if (!ret
|| ailist
== NULL
) {
1704 DEBUG(3,("matchname: getaddrinfo failed for "
1707 gai_strerror(ret
) ));
1712 * Make sure that getaddrinfo() returns the "correct" host name.
1715 if (ailist
->ai_canonname
== NULL
||
1716 (!strequal(remotehost
, ailist
->ai_canonname
) &&
1717 !strequal(remotehost
, "localhost"))) {
1718 DEBUG(0,("matchname: host name/name mismatch: %s != %s\n",
1720 ailist
->ai_canonname
?
1721 ailist
->ai_canonname
: "(NULL)"));
1722 freeaddrinfo(ailist
);
1726 /* Look up the host address in the address list we just got. */
1727 for (res
= ailist
; res
; res
= res
->ai_next
) {
1728 if (!res
->ai_addr
) {
1731 if (sockaddr_equal((const struct sockaddr_storage
*)res
->ai_addr
,
1733 freeaddrinfo(ailist
);
1739 * The host name does not map to the original host address. Perhaps
1740 * someone has compromised a name server. More likely someone botched
1741 * it, but that could be dangerous, too.
1744 DEBUG(0,("matchname: host name/address mismatch: %s != %s\n",
1745 print_sockaddr_len(addr_buf
,
1749 ailist
->ai_canonname
? ailist
->ai_canonname
: "(NULL)"));
1752 freeaddrinfo(ailist
);
1757 /*******************************************************************
1758 Deal with the singleton cache.
1759 ******************************************************************/
1761 struct name_addr_pair
{
1762 struct sockaddr_storage ss
;
1766 /*******************************************************************
1767 Lookup a name/addr pair. Returns memory allocated from memcache.
1768 ******************************************************************/
1770 static bool lookup_nc(struct name_addr_pair
*nc
)
1776 if (!memcache_lookup(
1777 NULL
, SINGLETON_CACHE
,
1778 data_blob_string_const("get_peer_name"),
1783 memcpy(&nc
->ss
, tmp
.data
, sizeof(nc
->ss
));
1784 nc
->name
= (const char *)tmp
.data
+ sizeof(nc
->ss
);
1788 /*******************************************************************
1789 Save a name/addr pair.
1790 ******************************************************************/
1792 static void store_nc(const struct name_addr_pair
*nc
)
1795 size_t namelen
= strlen(nc
->name
);
1797 tmp
= data_blob(NULL
, sizeof(nc
->ss
) + namelen
+ 1);
1801 memcpy(tmp
.data
, &nc
->ss
, sizeof(nc
->ss
));
1802 memcpy(tmp
.data
+sizeof(nc
->ss
), nc
->name
, namelen
+1);
1804 memcache_add(NULL
, SINGLETON_CACHE
,
1805 data_blob_string_const("get_peer_name"),
1807 data_blob_free(&tmp
);
1810 /*******************************************************************
1811 Return the DNS name of the remote end of a socket.
1812 ******************************************************************/
1814 const char *get_peer_name(int fd
, bool force_lookup
)
1816 struct name_addr_pair nc
;
1817 char addr_buf
[INET6_ADDRSTRLEN
];
1818 struct sockaddr_storage ss
;
1819 socklen_t length
= sizeof(ss
);
1822 char name_buf
[MAX_DNS_NAME_LENGTH
];
1823 char tmp_name
[MAX_DNS_NAME_LENGTH
];
1825 /* reverse lookups can be *very* expensive, and in many
1826 situations won't work because many networks don't link dhcp
1827 with dns. To avoid the delay we avoid the lookup if
1829 if (!lp_hostname_lookups() && (force_lookup
== false)) {
1830 length
= sizeof(nc
.ss
);
1831 nc
.name
= get_peer_addr_internal(fd
, addr_buf
, sizeof(addr_buf
),
1835 return nc
.name
? nc
.name
: "UNKNOWN";
1840 memset(&ss
, '\0', sizeof(ss
));
1841 p
= get_peer_addr_internal(fd
, addr_buf
, sizeof(addr_buf
), &ss
, &length
);
1843 /* it might be the same as the last one - save some DNS work */
1844 if (sockaddr_equal(&ss
, &nc
.ss
)) {
1845 return nc
.name
? nc
.name
: "UNKNOWN";
1848 /* Not the same. We need to lookup. */
1853 /* Look up the remote host name. */
1854 ret
= sys_getnameinfo((struct sockaddr
*)&ss
,
1863 DEBUG(1,("get_peer_name: getnameinfo failed "
1864 "for %s with error %s\n",
1866 gai_strerror(ret
)));
1867 strlcpy(name_buf
, p
, sizeof(name_buf
));
1869 if (!matchname(name_buf
, &ss
, length
)) {
1870 DEBUG(0,("Matchname failed on %s %s\n",name_buf
,p
));
1871 strlcpy(name_buf
,"UNKNOWN",sizeof(name_buf
));
1875 /* can't pass the same source and dest strings in when you
1876 use --enable-developer or the clobber_region() call will
1879 strlcpy(tmp_name
, name_buf
, sizeof(tmp_name
));
1880 alpha_strcpy(name_buf
, tmp_name
, "_-.", sizeof(name_buf
));
1881 if (strstr(name_buf
,"..")) {
1882 strlcpy(name_buf
, "UNKNOWN", sizeof(name_buf
));
1890 return nc
.name
? nc
.name
: "UNKNOWN";
1893 /*******************************************************************
1894 Return the IP addr of the remote end of a socket as a string.
1895 ******************************************************************/
1897 const char *get_peer_addr(int fd
, char *addr
, size_t addr_len
)
1899 return get_peer_addr_internal(fd
, addr
, addr_len
, NULL
, NULL
);
1902 /*******************************************************************
1903 Create protected unix domain socket.
1905 Some unixes cannot set permissions on a ux-dom-sock, so we
1906 have to make sure that the directory contains the protection
1907 permissions instead.
1908 ******************************************************************/
1910 int create_pipe_sock(const char *socket_dir
,
1911 const char *socket_name
,
1914 #ifdef HAVE_UNIXSOCKET
1915 struct sockaddr_un sunaddr
;
1921 old_umask
= umask(0);
1923 /* Create the socket directory or reuse the existing one */
1925 if (lstat(socket_dir
, &st
) == -1) {
1926 if (errno
== ENOENT
) {
1927 /* Create directory */
1928 if (mkdir(socket_dir
, dir_perms
) == -1) {
1929 DEBUG(0, ("error creating socket directory "
1930 "%s: %s\n", socket_dir
,
1935 DEBUG(0, ("lstat failed on socket directory %s: %s\n",
1936 socket_dir
, strerror(errno
)));
1940 /* Check ownership and permission on existing directory */
1941 if (!S_ISDIR(st
.st_mode
)) {
1942 DEBUG(0, ("socket directory %s isn't a directory\n",
1946 if ((st
.st_uid
!= sec_initial_uid()) ||
1947 ((st
.st_mode
& 0777) != dir_perms
)) {
1948 DEBUG(0, ("invalid permissions on socket directory "
1949 "%s\n", socket_dir
));
1954 /* Create the socket file */
1956 sock
= socket(AF_UNIX
, SOCK_STREAM
, 0);
1959 DEBUG(0, ("create_pipe_sock: socket error %s\n",
1964 if (asprintf(&path
, "%s/%s", socket_dir
, socket_name
) == -1) {
1969 memset(&sunaddr
, 0, sizeof(sunaddr
));
1970 sunaddr
.sun_family
= AF_UNIX
;
1971 strlcpy(sunaddr
.sun_path
, path
, sizeof(sunaddr
.sun_path
));
1973 if (bind(sock
, (struct sockaddr
*)&sunaddr
, sizeof(sunaddr
)) == -1) {
1974 DEBUG(0, ("bind failed on pipe socket %s: %s\n", path
,
1979 if (listen(sock
, 5) == -1) {
1980 DEBUG(0, ("listen failed on pipe socket %s: %s\n", path
,
2000 DEBUG(0, ("create_pipe_sock: No Unix sockets on this system\n"));
2002 #endif /* HAVE_UNIXSOCKET */
2005 /****************************************************************************
2006 Get my own canonical name, including domain.
2007 ****************************************************************************/
2009 const char *get_mydnsfullname(void)
2011 struct addrinfo
*res
= NULL
;
2012 char my_hostname
[HOST_NAME_MAX
];
2016 if (memcache_lookup(NULL
, SINGLETON_CACHE
,
2017 data_blob_string_const("get_mydnsfullname"),
2019 SMB_ASSERT(tmp
.length
> 0);
2020 return (const char *)tmp
.data
;
2023 /* get my host name */
2024 if (gethostname(my_hostname
, sizeof(my_hostname
)) == -1) {
2025 DEBUG(0,("get_mydnsfullname: gethostname failed\n"));
2029 /* Ensure null termination. */
2030 my_hostname
[sizeof(my_hostname
)-1] = '\0';
2032 ret
= interpret_string_addr_internal(&res
,
2034 AI_ADDRCONFIG
|AI_CANONNAME
);
2036 if (!ret
|| res
== NULL
) {
2037 DEBUG(3,("get_mydnsfullname: getaddrinfo failed for "
2040 gai_strerror(ret
) ));
2045 * Make sure that getaddrinfo() returns the "correct" host name.
2048 if (res
->ai_canonname
== NULL
) {
2049 DEBUG(3,("get_mydnsfullname: failed to get "
2050 "canonical name for %s\n",
2056 /* This copies the data, so we must do a lookup
2057 * afterwards to find the value to return.
2060 memcache_add(NULL
, SINGLETON_CACHE
,
2061 data_blob_string_const("get_mydnsfullname"),
2062 data_blob_string_const(res
->ai_canonname
));
2064 if (!memcache_lookup(NULL
, SINGLETON_CACHE
,
2065 data_blob_string_const("get_mydnsfullname"),
2067 tmp
= data_blob_talloc(talloc_tos(), res
->ai_canonname
,
2068 strlen(res
->ai_canonname
) + 1);
2073 return (const char *)tmp
.data
;
2076 /************************************************************
2078 ************************************************************/
2080 bool is_myname_or_ipaddr(const char *s
)
2082 TALLOC_CTX
*ctx
= talloc_tos();
2083 char addr
[INET6_ADDRSTRLEN
];
2085 const char *dnsname
;
2086 char *servername
= NULL
;
2092 /* Santize the string from '\\name' */
2093 name
= talloc_strdup(ctx
, s
);
2098 servername
= strrchr_m(name
, '\\' );
2105 /* Optimize for the common case */
2106 if (strequal(servername
, global_myname())) {
2110 /* Check for an alias */
2111 if (is_myname(servername
)) {
2115 /* Check for loopback */
2116 if (strequal(servername
, "127.0.0.1") ||
2117 strequal(servername
, "::1")) {
2121 if (strequal(servername
, "localhost")) {
2125 /* Maybe it's my dns name */
2126 dnsname
= get_mydnsfullname();
2127 if (dnsname
&& strequal(servername
, dnsname
)) {
2131 /* Handle possible CNAME records - convert to an IP addr. */
2132 if (!is_ipaddress(servername
)) {
2133 /* Use DNS to resolve the name, but only the first address */
2134 struct sockaddr_storage ss
;
2135 if (interpret_string_addr(&ss
, servername
, 0)) {
2136 print_sockaddr(addr
,
2143 /* Maybe its an IP address? */
2144 if (is_ipaddress(servername
)) {
2145 struct sockaddr_storage ss
;
2146 struct iface_struct
*nics
;
2149 if (!interpret_string_addr(&ss
, servername
, AI_NUMERICHOST
)) {
2153 if (is_zero_addr(&ss
) || is_loopback_addr(&ss
)) {
2157 nics
= TALLOC_ARRAY(ctx
, struct iface_struct
,
2162 n
= get_interfaces(nics
, MAX_INTERFACES
);
2163 for (i
=0; i
<n
; i
++) {
2164 if (sockaddr_equal(&nics
[i
].ip
, &ss
)) {