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)
48 #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, addr, &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 ret = getaddrinfo(str, NULL,
115 DEBUG(3,("interpret_string_addr_internal: getaddrinfo failed "
116 "for name %s [%s]\n",
118 gai_strerror(ret) ));
124 /****************************************************************************
125 Interpret an internet address or name into an IP address in 4 byte form.
126 RETURNS IN NETWORK BYTE ORDER (big endian).
127 ****************************************************************************/
129 uint32 interpret_addr(const char *str)
133 /* If it's in the form of an IP address then
134 * get the lib to interpret it */
135 if (is_ipaddress_v4(str)) {
138 if (inet_pton(AF_INET, str, &dest) <= 0) {
139 /* Error - this shouldn't happen ! */
140 DEBUG(0,("interpret_addr: inet_pton failed "
145 ret = dest.s_addr; /* NETWORK BYTE ORDER ! */
147 /* Otherwise assume it's a network name of some sort and use
149 struct addrinfo *res = NULL;
150 struct addrinfo *res_list = NULL;
151 if (!interpret_string_addr_internal(&res_list,
154 DEBUG(3,("interpret_addr: Unknown host. %s\n",str));
158 /* Find the first IPv4 address. */
159 for (res = res_list; res; res = res->ai_next) {
160 if (res->ai_family != AF_INET) {
163 if (res->ai_addr == NULL) {
169 DEBUG(3,("interpret_addr: host address is "
170 "invalid for host %s\n",str));
172 freeaddrinfo(res_list);
177 &((struct sockaddr_in *)res->ai_addr)->sin_addr.s_addr);
179 freeaddrinfo(res_list);
183 /* This is so bogus - all callers need fixing... JRA. */
184 if (ret == (uint32)-1) {
191 /*******************************************************************
192 A convenient addition to interpret_addr().
193 ******************************************************************/
195 struct in_addr *interpret_addr2(struct in_addr *ip, const char *str)
197 uint32 a = interpret_addr(str);
202 /*******************************************************************
203 Map a text hostname or IP address (IPv4 or IPv6) into a
204 struct sockaddr_storage.
205 ******************************************************************/
207 bool interpret_string_addr(struct sockaddr_storage *pss,
211 char addr[INET6_ADDRSTRLEN];
212 struct addrinfo *res = NULL;
213 #if defined(HAVE_IPV6)
214 unsigned int scope_id = 0;
216 if (strchr_m(str, ':')) {
217 char *p = strchr_m(str, '%');
220 * Cope with link-local.
221 * This is IP:v6:addr%ifname.
224 if (p && (p > str) && ((scope_id = if_nametoindex(p+1)) != 0)) {
226 MIN(PTR_DIFF(p,str)+1,
235 if (!interpret_string_addr_internal(&res, str, flags|AI_ADDRCONFIG)) {
241 /* Copy the first sockaddr. */
242 memcpy(pss, res->ai_addr, res->ai_addrlen);
244 #if defined(HAVE_IPV6)
245 if (pss->ss_family == AF_INET6 && scope_id) {
246 struct sockaddr_in6 *ps6 = (struct sockaddr_in6 *)pss;
247 if (IN6_IS_ADDR_LINKLOCAL(&ps6->sin6_addr) &&
248 ps6->sin6_scope_id == 0) {
249 ps6->sin6_scope_id = scope_id;
258 /*******************************************************************
259 Check if an IPv7 is 127.0.0.1
260 ******************************************************************/
262 bool is_loopback_ip_v4(struct in_addr ip)
265 a.s_addr = htonl(INADDR_LOOPBACK);
266 return(ip.s_addr == a.s_addr);
269 /*******************************************************************
270 Check if a struct sockaddr_storage is the loopback address.
271 ******************************************************************/
273 bool is_loopback_addr(const struct sockaddr_storage *pss)
275 #if defined(HAVE_IPV6)
276 if (pss->ss_family == AF_INET6) {
277 struct in6_addr *pin6 =
278 &((struct sockaddr_in6 *)pss)->sin6_addr;
279 return IN6_IS_ADDR_LOOPBACK(pin6);
282 if (pss->ss_family == AF_INET) {
283 struct in_addr *pin = &((struct sockaddr_in *)pss)->sin_addr;
284 return is_loopback_ip_v4(*pin);
289 /*******************************************************************
290 Check if an IPv4 is 0.0.0.0.
291 ******************************************************************/
293 bool is_zero_ip_v4(struct in_addr ip)
296 putip((char *)&a,(char *)&ip);
300 /*******************************************************************
301 Check if a struct sockaddr_storage has an unspecified address.
302 ******************************************************************/
304 bool is_zero_addr(const struct sockaddr_storage *pss)
306 #if defined(HAVE_IPV6)
307 if (pss->ss_family == AF_INET6) {
308 struct in6_addr *pin6 =
309 &((struct sockaddr_in6 *)pss)->sin6_addr;
310 return IN6_IS_ADDR_UNSPECIFIED(pin6);
313 if (pss->ss_family == AF_INET) {
314 struct in_addr *pin = &((struct sockaddr_in *)pss)->sin_addr;
315 return is_zero_ip_v4(*pin);
320 /*******************************************************************
321 Set an IP to 0.0.0.0.
322 ******************************************************************/
324 void zero_ip_v4(struct in_addr *ip)
326 memset(ip, '\0', sizeof(struct in_addr));
329 /*******************************************************************
330 Set an address to INADDR_ANY.
331 ******************************************************************/
333 void zero_addr(struct sockaddr_storage *pss)
335 memset(pss, '\0', sizeof(*pss));
336 /* Ensure we're at least a valid sockaddr-storage. */
337 pss->ss_family = AF_INET;
340 /*******************************************************************
341 Are two IPs on the same subnet - IPv4 version ?
342 ********************************************************************/
344 bool same_net_v4(struct in_addr ip1,struct in_addr ip2,struct in_addr mask)
346 uint32 net1,net2,nmask;
348 nmask = ntohl(mask.s_addr);
349 net1 = ntohl(ip1.s_addr);
350 net2 = ntohl(ip2.s_addr);
352 return((net1 & nmask) == (net2 & nmask));
355 /*******************************************************************
356 Convert an IPv4 struct in_addr to a struct sockaddr_storage.
357 ********************************************************************/
359 void in_addr_to_sockaddr_storage(struct sockaddr_storage *ss,
362 struct sockaddr_in *sa = (struct sockaddr_in *)ss;
363 memset(ss, '\0', sizeof(*ss));
364 ss->ss_family = AF_INET;
368 #if defined(HAVE_IPV6)
369 /*******************************************************************
370 Convert an IPv6 struct in_addr to a struct sockaddr_storage.
371 ********************************************************************/
373 void in6_addr_to_sockaddr_storage(struct sockaddr_storage *ss,
376 struct sockaddr_in6 *sa = (struct sockaddr_in6 *)ss;
377 memset(ss, '\0', sizeof(*ss));
378 ss->ss_family = AF_INET6;
383 /*******************************************************************
384 Are two IPs on the same subnet?
385 ********************************************************************/
387 bool same_net(const struct sockaddr_storage *ip1,
388 const struct sockaddr_storage *ip2,
389 const struct sockaddr_storage *mask)
391 if (ip1->ss_family != ip2->ss_family) {
392 /* Never on the same net. */
396 #if defined(HAVE_IPV6)
397 if (ip1->ss_family == AF_INET6) {
398 struct sockaddr_in6 ip1_6 = *(struct sockaddr_in6 *)ip1;
399 struct sockaddr_in6 ip2_6 = *(struct sockaddr_in6 *)ip2;
400 struct sockaddr_in6 mask_6 = *(struct sockaddr_in6 *)mask;
401 char *p1 = (char *)&ip1_6.sin6_addr;
402 char *p2 = (char *)&ip2_6.sin6_addr;
403 char *m = (char *)&mask_6.sin6_addr;
406 for (i = 0; i < sizeof(struct in6_addr); i++) {
411 return (memcmp(&ip1_6.sin6_addr,
413 sizeof(struct in6_addr)) == 0);
416 if (ip1->ss_family == AF_INET) {
417 return same_net_v4(((const struct sockaddr_in *)ip1)->sin_addr,
418 ((const struct sockaddr_in *)ip2)->sin_addr,
419 ((const struct sockaddr_in *)mask)->sin_addr);
424 /*******************************************************************
425 Are two sockaddr_storage's the same family and address ? Ignore port etc.
426 ********************************************************************/
428 bool addr_equal(const struct sockaddr_storage *ip1,
429 const struct sockaddr_storage *ip2)
431 if (ip1->ss_family != ip2->ss_family) {
432 /* Never the same. */
436 #if defined(HAVE_IPV6)
437 if (ip1->ss_family == AF_INET6) {
438 return (memcmp(&((const struct sockaddr_in6 *)ip1)->sin6_addr,
439 &((const struct sockaddr_in6 *)ip2)->sin6_addr,
440 sizeof(struct in6_addr)) == 0);
443 if (ip1->ss_family == AF_INET) {
444 return (memcmp(&((const struct sockaddr_in *)ip1)->sin_addr,
445 &((const struct sockaddr_in *)ip2)->sin_addr,
446 sizeof(struct in_addr)) == 0);
451 /****************************************************************************
452 Is an IP address the INADDR_ANY or in6addr_any value ?
453 ****************************************************************************/
455 bool is_address_any(const struct sockaddr_storage *psa)
457 #if defined(HAVE_IPV6)
458 if (psa->ss_family == AF_INET6) {
459 struct sockaddr_in6 *si6 = (struct sockaddr_in6 *)psa;
460 if (memcmp(&in6addr_any,
462 sizeof(in6addr_any)) == 0) {
468 if (psa->ss_family == AF_INET) {
469 struct sockaddr_in *si = (struct sockaddr_in *)psa;
470 if (si->sin_addr.s_addr == INADDR_ANY) {
478 /****************************************************************************
479 Get a port number in host byte order from a sockaddr_storage.
480 ****************************************************************************/
482 uint16_t get_sockaddr_port(const struct sockaddr_storage *pss)
486 if (pss->ss_family != AF_INET) {
487 #if defined(HAVE_IPV6)
489 const struct sockaddr_in6 *sa6 =
490 (const struct sockaddr_in6 *)pss;
491 port = ntohs(sa6->sin6_port);
494 const struct sockaddr_in *sa =
495 (const struct sockaddr_in *)pss;
496 port = ntohs(sa->sin_port);
501 /****************************************************************************
502 Print out an IPv4 or IPv6 address from a struct sockaddr_storage.
503 ****************************************************************************/
505 static char *print_sockaddr_len(char *dest,
507 const struct sockaddr_storage *psa,
513 (void)sys_getnameinfo((const struct sockaddr *)psa,
521 /****************************************************************************
522 Print out an IPv4 or IPv6 address from a struct sockaddr_storage.
523 ****************************************************************************/
525 char *print_sockaddr(char *dest,
527 const struct sockaddr_storage *psa)
529 return print_sockaddr_len(dest, destlen, psa,
530 sizeof(struct sockaddr_storage));
533 /****************************************************************************
534 Print out a canonical IPv4 or IPv6 address from a struct sockaddr_storage.
535 ****************************************************************************/
537 char *print_canonical_sockaddr(TALLOC_CTX *ctx,
538 const struct sockaddr_storage *pss)
540 char addr[INET6_ADDRSTRLEN];
544 ret = sys_getnameinfo((const struct sockaddr *)pss,
545 sizeof(struct sockaddr_storage),
552 if (pss->ss_family != AF_INET) {
553 #if defined(HAVE_IPV6)
555 const struct sockaddr_in6 *sa6 =
556 (const struct sockaddr_in6 *)pss;
557 uint16_t port = ntohs(sa6->sin6_port);
560 dest = talloc_asprintf(ctx,
565 dest = talloc_asprintf(ctx,
573 const struct sockaddr_in *sa =
574 (const struct sockaddr_in *)pss;
575 uint16_t port = ntohs(sa->sin_port);
578 dest = talloc_asprintf(ctx,
583 dest = talloc_asprintf(ctx,
591 /****************************************************************************
592 Return the string of an IP address (IPv4 or IPv6).
593 ****************************************************************************/
595 static const char *get_socket_addr(int fd, char *addr_buf, size_t addr_len)
597 struct sockaddr_storage sa;
598 socklen_t length = sizeof(sa);
600 /* Ok, returning a hard coded IPv4 address
601 * is bogus, but it's just as bogus as a
602 * zero IPv6 address. No good choice here.
605 strlcpy(addr_buf, "0.0.0.0", addr_len);
611 if (getsockname(fd, (struct sockaddr *)&sa, &length) < 0) {
612 DEBUG(0,("getsockname failed. Error was %s\n",
617 return print_sockaddr_len(addr_buf, addr_len, &sa, length);
621 /* Not currently used. JRA. */
622 /****************************************************************************
623 Return the port number we've bound to on a socket.
624 ****************************************************************************/
626 static int get_socket_port(int fd)
628 struct sockaddr_storage sa;
629 socklen_t length = sizeof(sa);
635 if (getsockname(fd, (struct sockaddr *)&sa, &length) < 0) {
636 DEBUG(0,("getpeername failed. Error was %s\n",
641 #if defined(HAVE_IPV6)
642 if (sa.ss_family == AF_INET6) {
643 return ntohs(((struct sockaddr_in6 *)&sa)->sin6_port);
646 if (sa.ss_family == AF_INET) {
647 return ntohs(((struct sockaddr_in *)&sa)->sin_port);
653 void set_sockaddr_port(struct sockaddr_storage *psa, uint16 port)
655 #if defined(HAVE_IPV6)
656 if (psa->ss_family == AF_INET6) {
657 ((struct sockaddr_in6 *)psa)->sin6_port = htons(port);
660 if (psa->ss_family == AF_INET) {
661 ((struct sockaddr_in *)psa)->sin_port = htons(port);
665 const char *client_name(int fd)
667 return get_peer_name(fd,false);
670 const char *client_addr(int fd, char *addr, size_t addrlen)
672 return get_peer_addr(fd,addr,addrlen);
675 const char *client_socket_addr(int fd, char *addr, size_t addr_len)
677 return get_socket_addr(fd, addr, addr_len);
681 /* Not currently used. JRA. */
682 int client_socket_port(int fd)
684 return get_socket_port(fd);
688 /****************************************************************************
689 Accessor functions to make thread-safe code easier later...
690 ****************************************************************************/
692 void set_smb_read_error(enum smb_read_errors *pre,
693 enum smb_read_errors newerr)
700 void cond_set_smb_read_error(enum smb_read_errors *pre,
701 enum smb_read_errors newerr)
703 if (pre && *pre == SMB_READ_OK) {
708 /****************************************************************************
709 Determine if a file descriptor is in fact a socket.
710 ****************************************************************************/
712 bool is_a_socket(int fd)
717 return(getsockopt(fd, SOL_SOCKET, SO_TYPE, (char *)&v, &l) == 0);
720 enum SOCK_OPT_TYPES {OPT_BOOL,OPT_INT,OPT_ON};
722 typedef struct smb_socket_option {
730 static const smb_socket_option socket_options[] = {
731 {"SO_KEEPALIVE", SOL_SOCKET, SO_KEEPALIVE, 0, OPT_BOOL},
732 {"SO_REUSEADDR", SOL_SOCKET, SO_REUSEADDR, 0, OPT_BOOL},
733 {"SO_BROADCAST", SOL_SOCKET, SO_BROADCAST, 0, OPT_BOOL},
735 {"TCP_NODELAY", IPPROTO_TCP, TCP_NODELAY, 0, OPT_BOOL},
738 {"TCP_KEEPCNT", IPPROTO_TCP, TCP_KEEPCNT, 0, OPT_INT},
741 {"TCP_KEEPIDLE", IPPROTO_TCP, TCP_KEEPIDLE, 0, OPT_INT},
744 {"TCP_KEEPINTVL", IPPROTO_TCP, TCP_KEEPINTVL, 0, OPT_INT},
746 #ifdef IPTOS_LOWDELAY
747 {"IPTOS_LOWDELAY", IPPROTO_IP, IP_TOS, IPTOS_LOWDELAY, OPT_ON},
749 #ifdef IPTOS_THROUGHPUT
750 {"IPTOS_THROUGHPUT", IPPROTO_IP, IP_TOS, IPTOS_THROUGHPUT, OPT_ON},
753 {"SO_REUSEPORT", SOL_SOCKET, SO_REUSEPORT, 0, OPT_BOOL},
756 {"SO_SNDBUF", SOL_SOCKET, SO_SNDBUF, 0, OPT_INT},
759 {"SO_RCVBUF", SOL_SOCKET, SO_RCVBUF, 0, OPT_INT},
762 {"SO_SNDLOWAT", SOL_SOCKET, SO_SNDLOWAT, 0, OPT_INT},
765 {"SO_RCVLOWAT", SOL_SOCKET, SO_RCVLOWAT, 0, OPT_INT},
768 {"SO_SNDTIMEO", SOL_SOCKET, SO_SNDTIMEO, 0, OPT_INT},
771 {"SO_RCVTIMEO", SOL_SOCKET, SO_RCVTIMEO, 0, OPT_INT},
774 {"TCP_FASTACK", IPPROTO_TCP, TCP_FASTACK, 0, OPT_INT},
778 /****************************************************************************
779 Print socket options.
780 ****************************************************************************/
782 static void print_socket_options(int s)
786 const smb_socket_option *p = &socket_options[0];
788 /* wrapped in if statement to prevent streams
789 * leak in SCO Openserver 5.0 */
790 /* reported on samba-technical --jerry */
791 if ( DEBUGLEVEL >= 5 ) {
792 for (; p->name != NULL; p++) {
793 if (getsockopt(s, p->level, p->option,
794 (void *)&value, &vlen) == -1) {
795 DEBUG(5,("Could not test socket option %s.\n",
798 DEBUG(5,("socket option %s = %d\n",
805 /****************************************************************************
806 Set user socket options.
807 ****************************************************************************/
809 void set_socket_options(int fd, const char *options)
811 TALLOC_CTX *ctx = talloc_stackframe();
814 while (next_token_talloc(ctx, &options, &tok," \t,")) {
818 bool got_value = false;
820 if ((p = strchr_m(tok,'='))) {
826 for (i=0;socket_options[i].name;i++)
827 if (strequal(socket_options[i].name,tok))
830 if (!socket_options[i].name) {
831 DEBUG(0,("Unknown socket option %s\n",tok));
835 switch (socket_options[i].opttype) {
838 ret = setsockopt(fd,socket_options[i].level,
839 socket_options[i].option,
840 (char *)&value,sizeof(int));
845 DEBUG(0,("syntax error - %s "
846 "does not take a value\n",tok));
849 int on = socket_options[i].value;
850 ret = setsockopt(fd,socket_options[i].level,
851 socket_options[i].option,
852 (char *)&on,sizeof(int));
858 DEBUG(0,("Failed to set socket option %s (Error %s)\n",
859 tok, strerror(errno) ));
864 print_socket_options(fd);
867 /****************************************************************************
869 ****************************************************************************/
871 ssize_t read_udp_v4_socket(int fd,
874 struct sockaddr_storage *psa)
877 socklen_t socklen = sizeof(*psa);
878 struct sockaddr_in *si = (struct sockaddr_in *)psa;
880 memset((char *)psa,'\0',socklen);
882 ret = (ssize_t)sys_recvfrom(fd,buf,len,0,
883 (struct sockaddr *)psa,&socklen);
885 /* Don't print a low debug error for a non-blocking socket. */
886 if (errno == EAGAIN) {
887 DEBUG(10,("read_udp_v4_socket: returned EAGAIN\n"));
889 DEBUG(2,("read_udp_v4_socket: failed. errno=%s\n",
895 if (psa->ss_family != AF_INET) {
896 DEBUG(2,("read_udp_v4_socket: invalid address family %d "
897 "(not IPv4)\n", (int)psa->ss_family));
901 DEBUG(10,("read_udp_v4_socket: ip %s port %d read: %lu\n",
902 inet_ntoa(si->sin_addr),
904 (unsigned long)ret));
909 /****************************************************************************
910 Read data from a socket with a timout in msec.
911 mincount = if timeout, minimum to read before returning
912 maxcount = number to be read.
913 time_out = timeout in milliseconds
914 ****************************************************************************/
916 ssize_t read_socket_with_timeout(int fd,
920 unsigned int time_out,
921 enum smb_read_errors *pre)
927 struct timeval timeout;
928 char addr[INET6_ADDRSTRLEN];
930 /* just checking .... */
934 set_smb_read_error(pre,SMB_READ_OK);
942 while (nread < mincnt) {
943 readret = sys_read(fd, buf + nread, maxcnt - nread);
946 DEBUG(5,("read_socket_with_timeout: "
947 "blocking read. EOF from client.\n"));
948 set_smb_read_error(pre,SMB_READ_EOF);
953 if (fd == get_client_fd()) {
954 /* Try and give an error message
955 * saying what client failed. */
956 DEBUG(0,("read_socket_with_timeout: "
957 "client %s read error = %s.\n",
958 get_peer_addr(fd,addr,sizeof(addr)),
961 DEBUG(0,("read_socket_with_timeout: "
962 "read error = %s.\n",
965 set_smb_read_error(pre,SMB_READ_ERROR);
970 return((ssize_t)nread);
973 /* Most difficult - timeout read */
974 /* If this is ever called on a disk file and
975 mincnt is greater then the filesize then
976 system performance will suffer severely as
977 select always returns true on disk files */
979 /* Set initial timeout */
980 timeout.tv_sec = (time_t)(time_out / 1000);
981 timeout.tv_usec = (long)(1000 * (time_out % 1000));
983 for (nread=0; nread < mincnt; ) {
987 selrtn = sys_select_intr(fd+1,&fds,NULL,NULL,&timeout);
991 /* something is wrong. Maybe the socket is dead? */
992 if (fd == get_client_fd()) {
993 /* Try and give an error message saying
994 * what client failed. */
995 DEBUG(0,("read_socket_with_timeout: timeout "
996 "read for client %s. select error = %s.\n",
997 get_peer_addr(fd,addr,sizeof(addr)),
1000 DEBUG(0,("read_socket_with_timeout: timeout "
1001 "read. select error = %s.\n",
1004 set_smb_read_error(pre,SMB_READ_ERROR);
1008 /* Did we timeout ? */
1010 DEBUG(10,("read_socket_with_timeout: timeout read. "
1011 "select timed out.\n"));
1012 set_smb_read_error(pre,SMB_READ_TIMEOUT);
1016 readret = sys_read(fd, buf+nread, maxcnt-nread);
1019 /* we got EOF on the file descriptor */
1020 DEBUG(5,("read_socket_with_timeout: timeout read. "
1021 "EOF from client.\n"));
1022 set_smb_read_error(pre,SMB_READ_EOF);
1026 if (readret == -1) {
1027 /* the descriptor is probably dead */
1028 if (fd == get_client_fd()) {
1029 /* Try and give an error message
1030 * saying what client failed. */
1031 DEBUG(0,("read_socket_with_timeout: timeout "
1032 "read to client %s. read error = %s.\n",
1033 get_peer_addr(fd,addr,sizeof(addr)),
1036 DEBUG(0,("read_socket_with_timeout: timeout "
1037 "read. read error = %s.\n",
1040 set_smb_read_error(pre,SMB_READ_ERROR);
1047 /* Return the number we got */
1048 return (ssize_t)nread;
1051 /****************************************************************************
1052 Read data from the client, reading exactly N bytes.
1053 ****************************************************************************/
1055 ssize_t read_data(int fd,char *buffer,size_t N, enum smb_read_errors *pre)
1057 return read_socket_with_timeout(fd, buffer, N, N, 0, pre);
1060 /****************************************************************************
1062 ****************************************************************************/
1064 ssize_t write_data(int fd, const char *buffer, size_t N)
1068 char addr[INET6_ADDRSTRLEN];
1071 ret = sys_write(fd,buffer + total,N - total);
1074 if (fd == get_client_fd()) {
1075 /* Try and give an error message saying
1076 * what client failed. */
1077 DEBUG(0,("write_data: write failure in "
1078 "writing to client %s. Error %s\n",
1079 get_peer_addr(fd,addr,sizeof(addr)),
1082 DEBUG(0,("write_data: write failure. "
1083 "Error = %s\n", strerror(errno) ));
1094 return (ssize_t)total;
1097 /****************************************************************************
1098 Send a keepalive packet (rfc1002).
1099 ****************************************************************************/
1101 bool send_keepalive(int client)
1103 unsigned char buf[4];
1105 buf[0] = SMBkeepalive;
1106 buf[1] = buf[2] = buf[3] = 0;
1108 return(write_data(client,(char *)buf,4) == 4);
1111 /****************************************************************************
1112 Read 4 bytes of a smb packet and return the smb length of the packet.
1113 Store the result in the buffer.
1114 This version of the function will return a length of zero on receiving
1116 Timeout is in milliseconds.
1117 ****************************************************************************/
1119 ssize_t read_smb_length_return_keepalive(int fd,
1121 unsigned int timeout,
1122 enum smb_read_errors *pre)
1127 if (read_socket_with_timeout(fd, inbuf, 4, 4, timeout, pre) != 4) {
1131 len = smb_len(inbuf);
1132 msg_type = CVAL(inbuf,0);
1134 if (msg_type == SMBkeepalive) {
1135 DEBUG(5,("Got keepalive packet\n"));
1138 DEBUG(10,("got smb length of %lu\n",(unsigned long)len));
1143 /****************************************************************************
1144 Read 4 bytes of a smb packet and return the smb length of the packet.
1145 Store the result in the buffer. This version of the function will
1146 never return a session keepalive (length of zero).
1147 Timeout is in milliseconds.
1148 ****************************************************************************/
1150 ssize_t read_smb_length(int fd, char *inbuf, unsigned int timeout, enum smb_read_errors *pre)
1153 uint8_t msgtype = SMBkeepalive;
1155 while (msgtype == SMBkeepalive) {
1156 len = read_smb_length_return_keepalive(fd, inbuf, timeout,
1161 msgtype = CVAL(inbuf, 0);
1164 DEBUG(10,("read_smb_length: got smb length of %lu\n",
1165 (unsigned long)len));
1170 /****************************************************************************
1171 Read an smb from a fd. Note that the buffer *MUST* be of size
1172 BUFFER_SIZE+SAFETY_MARGIN.
1173 The timeout is in milliseconds.
1174 This function will return on receipt of a session keepalive packet.
1175 maxlen is the max number of bytes to return, not including the 4 byte
1176 length. If zero it means BUFFER_SIZE+SAFETY_MARGIN limit.
1177 Doesn't check the MAC on signed packets.
1178 ****************************************************************************/
1180 ssize_t receive_smb_raw(int fd,
1182 unsigned int timeout,
1184 enum smb_read_errors *pre)
1188 set_smb_read_error(pre,SMB_READ_OK);
1190 len = read_smb_length_return_keepalive(fd,buffer,timeout,pre);
1192 DEBUG(10,("receive_smb_raw: length < 0!\n"));
1195 * Correct fix. smb_read_error may have already been
1196 * set. Only set it here if not already set. Global
1197 * variables still suck :-). JRA.
1200 cond_set_smb_read_error(pre,SMB_READ_ERROR);
1205 * A WRITEX with CAP_LARGE_WRITEX can be 64k worth of data plus 65 bytes
1206 * of header. Don't print the error if this fits.... JRA.
1209 if (len > (BUFFER_SIZE + LARGE_WRITEX_HDR_SIZE)) {
1210 DEBUG(0,("Invalid packet length! (%lu bytes).\n",
1211 (unsigned long)len));
1212 if (len > BUFFER_SIZE + (SAFETY_MARGIN/2)) {
1215 * Correct fix. smb_read_error may have already been
1216 * set. Only set it here if not already set. Global
1217 * variables still suck :-). JRA.
1220 cond_set_smb_read_error(pre,SMB_READ_ERROR);
1227 len = MIN(len,maxlen);
1230 ret = read_socket_with_timeout(fd, buffer+4, len, len, timeout,
1234 cond_set_smb_read_error(pre,SMB_READ_ERROR);
1238 /* not all of samba3 properly checks for packet-termination
1239 * of strings. This ensures that we don't run off into
1241 SSVAL(buffer+4,len, 0);
1247 /****************************************************************************
1248 Open a socket of the specified type, port, and address for incoming data.
1249 ****************************************************************************/
1251 int open_socket_in(int type,
1254 const struct sockaddr_storage *psock,
1257 struct sockaddr_storage sock;
1259 socklen_t slen = sizeof(struct sockaddr_in);
1263 #if defined(HAVE_IPV6)
1264 if (sock.ss_family == AF_INET6) {
1265 ((struct sockaddr_in6 *)&sock)->sin6_port = htons(port);
1266 slen = sizeof(struct sockaddr_in6);
1269 if (sock.ss_family == AF_INET) {
1270 ((struct sockaddr_in *)&sock)->sin_port = htons(port);
1273 res = socket(sock.ss_family, type, 0 );
1276 dbgtext( "open_socket_in(): socket() call failed: " );
1277 dbgtext( "%s\n", strerror( errno ) );
1282 /* This block sets/clears the SO_REUSEADDR and possibly SO_REUSEPORT. */
1284 int val = rebind ? 1 : 0;
1285 if( setsockopt(res,SOL_SOCKET,SO_REUSEADDR,
1286 (char *)&val,sizeof(val)) == -1 ) {
1287 if( DEBUGLVL( dlevel ) ) {
1288 dbgtext( "open_socket_in(): setsockopt: " );
1289 dbgtext( "SO_REUSEADDR = %s ",
1290 val?"true":"false" );
1291 dbgtext( "on port %d failed ", port );
1292 dbgtext( "with error = %s\n", strerror(errno) );
1296 if( setsockopt(res,SOL_SOCKET,SO_REUSEPORT,
1297 (char *)&val,sizeof(val)) == -1 ) {
1298 if( DEBUGLVL( dlevel ) ) {
1299 dbgtext( "open_socket_in(): setsockopt: ");
1300 dbgtext( "SO_REUSEPORT = %s ",
1301 val?"true":"false");
1302 dbgtext( "on port %d failed ", port);
1303 dbgtext( "with error = %s\n", strerror(errno));
1306 #endif /* SO_REUSEPORT */
1309 /* now we've got a socket - we need to bind it */
1310 if (bind(res, (struct sockaddr *)&sock, slen) == -1 ) {
1311 if( DEBUGLVL(dlevel) && (port == SMB_PORT1 ||
1312 port == SMB_PORT2 || port == NMB_PORT) ) {
1313 char addr[INET6_ADDRSTRLEN];
1314 print_sockaddr(addr, sizeof(addr),
1316 dbgtext( "bind failed on port %d ", port);
1317 dbgtext( "socket_addr = %s.\n", addr);
1318 dbgtext( "Error = %s\n", strerror(errno));
1324 DEBUG( 10, ( "bind succeeded on port %d\n", port ) );
1328 /****************************************************************************
1329 Create an outgoing socket. timeout is in milliseconds.
1330 **************************************************************************/
1332 int open_socket_out(int type,
1333 const struct sockaddr_storage *pss,
1337 char addr[INET6_ADDRSTRLEN];
1338 struct sockaddr_storage sock_out = *pss;
1340 int connect_loop = 10;
1343 /* create a socket to write to */
1344 res = socket(pss->ss_family, type, 0);
1346 DEBUG(0,("socket error (%s)\n", strerror(errno)));
1350 if (type != SOCK_STREAM) {
1354 #if defined(HAVE_IPV6)
1355 if (pss->ss_family == AF_INET6) {
1356 struct sockaddr_in6 *psa6 = (struct sockaddr_in6 *)&sock_out;
1357 psa6->sin6_port = htons(port);
1358 if (psa6->sin6_scope_id == 0 &&
1359 IN6_IS_ADDR_LINKLOCAL(&psa6->sin6_addr)) {
1360 setup_linklocal_scope_id(&sock_out);
1364 if (pss->ss_family == AF_INET) {
1365 struct sockaddr_in *psa = (struct sockaddr_in *)&sock_out;
1366 psa->sin_port = htons(port);
1369 /* set it non-blocking */
1370 set_blocking(res,false);
1372 print_sockaddr(addr, sizeof(addr), &sock_out);
1373 DEBUG(3,("Connecting to %s at port %u\n",
1375 (unsigned int)port));
1377 /* and connect it to the destination */
1380 ret = sys_connect(res, (struct sockaddr *)&sock_out);
1382 /* Some systems return EAGAIN when they mean EINPROGRESS */
1383 if (ret < 0 && (errno == EINPROGRESS || errno == EALREADY ||
1384 errno == EAGAIN) && (connect_loop < timeout) ) {
1385 smb_msleep(connect_loop);
1386 timeout -= connect_loop;
1387 connect_loop += increment;
1388 if (increment < 250) {
1389 /* After 8 rounds we end up at a max of 255 msec */
1395 if (ret < 0 && (errno == EINPROGRESS || errno == EALREADY ||
1397 DEBUG(1,("timeout connecting to %s:%u\n",
1399 (unsigned int)port));
1405 if (ret < 0 && errno == EISCONN) {
1412 DEBUG(2,("error connecting to %s:%d (%s)\n",
1420 /* set it blocking again */
1421 set_blocking(res,true);
1426 /****************************************************************************
1427 Create an outgoing TCP socket to any of the addrs. This is for
1428 simultaneous connects to port 445 and 139 of a host or even a variety
1429 of DC's all of which are equivalent for our purposes.
1430 **************************************************************************/
1432 bool open_any_socket_out(struct sockaddr_storage *addrs, int num_addrs,
1433 int timeout, int *fd_index, int *fd)
1435 int i, resulting_index, res;
1439 fd_set r_fds, wr_fds;
1443 int connect_loop = 10000; /* 10 milliseconds */
1445 timeout *= 1000; /* convert to microseconds */
1447 sockets = SMB_MALLOC_ARRAY(int, num_addrs);
1449 if (sockets == NULL)
1452 resulting_index = -1;
1454 for (i=0; i<num_addrs; i++)
1457 for (i=0; i<num_addrs; i++) {
1458 sockets[i] = socket(addrs[i].ss_family, SOCK_STREAM, 0);
1461 set_blocking(sockets[i], false);
1465 good_connect = false;
1467 for (i=0; i<num_addrs; i++) {
1468 const struct sockaddr * a =
1469 (const struct sockaddr *)&(addrs[i]);
1471 if (sockets[i] == -1)
1474 if (sys_connect(sockets[i], a) == 0) {
1475 /* Rather unlikely as we are non-blocking, but it
1476 * might actually happen. */
1477 resulting_index = i;
1481 if (errno == EINPROGRESS || errno == EALREADY ||
1485 errno == EAGAIN || errno == EINTR) {
1486 /* These are the error messages that something is
1488 good_connect = true;
1489 } else if (errno != 0) {
1490 /* There was a direct error */
1496 if (!good_connect) {
1497 /* All of the connect's resulted in real error conditions */
1501 /* Lets see if any of the connect attempts succeeded */
1507 for (i=0; i<num_addrs; i++) {
1508 if (sockets[i] == -1)
1510 FD_SET(sockets[i], &wr_fds);
1511 FD_SET(sockets[i], &r_fds);
1512 if (sockets[i]>maxfd)
1517 tv.tv_usec = connect_loop;
1519 res = sys_select_intr(maxfd+1, &r_fds, &wr_fds, NULL, &tv);
1527 for (i=0; i<num_addrs; i++) {
1529 if (sockets[i] == -1)
1532 /* Stevens, Network Programming says that if there's a
1533 * successful connect, the socket is only writable. Upon an
1534 * error, it's both readable and writable. */
1536 if (FD_ISSET(sockets[i], &r_fds) &&
1537 FD_ISSET(sockets[i], &wr_fds)) {
1538 /* readable and writable, so it's an error */
1544 if (!FD_ISSET(sockets[i], &r_fds) &&
1545 FD_ISSET(sockets[i], &wr_fds)) {
1546 /* Only writable, so it's connected */
1547 resulting_index = i;
1554 timeout -= connect_loop;
1557 connect_loop *= 1.5;
1558 if (connect_loop > timeout)
1559 connect_loop = timeout;
1563 for (i=0; i<num_addrs; i++) {
1564 if (i == resulting_index)
1566 if (sockets[i] >= 0)
1570 if (resulting_index >= 0) {
1571 *fd_index = resulting_index;
1572 *fd = sockets[*fd_index];
1573 set_blocking(*fd, true);
1578 return (resulting_index >= 0);
1580 /****************************************************************************
1581 Open a connected UDP socket to host on port
1582 **************************************************************************/
1584 int open_udp_socket(const char *host, int port)
1586 int type = SOCK_DGRAM;
1587 struct sockaddr_in sock_out;
1589 struct in_addr addr;
1591 (void)interpret_addr2(&addr, host);
1593 res = socket(PF_INET, type, 0);
1598 memset((char *)&sock_out,'\0',sizeof(sock_out));
1599 putip((char *)&sock_out.sin_addr,(char *)&addr);
1600 sock_out.sin_port = htons(port);
1601 sock_out.sin_family = PF_INET;
1603 if (sys_connect(res,(struct sockaddr *)&sock_out)) {
1611 /*******************************************************************
1612 Return the IP addr of the remote end of a socket as a string.
1613 Optionally return the struct sockaddr_storage.
1614 ******************************************************************/
1616 static const char *get_peer_addr_internal(int fd,
1618 size_t addr_buf_len,
1619 struct sockaddr_storage *pss,
1622 struct sockaddr_storage ss;
1623 socklen_t length = sizeof(ss);
1625 strlcpy(addr_buf,"0.0.0.0",addr_buf_len);
1634 if (plength == NULL) {
1638 if (getpeername(fd, (struct sockaddr *)pss, plength) < 0) {
1639 DEBUG(0,("getpeername failed. Error was %s\n",
1644 print_sockaddr_len(addr_buf,
1651 /*******************************************************************
1652 Matchname - determine if host name matches IP address. Used to
1653 confirm a hostname lookup to prevent spoof attacks.
1654 ******************************************************************/
1656 static bool matchname(const char *remotehost,
1657 const struct sockaddr_storage *pss,
1660 struct addrinfo *res = NULL;
1661 struct addrinfo *ailist = NULL;
1662 char addr_buf[INET6_ADDRSTRLEN];
1663 bool ret = interpret_string_addr_internal(&ailist,
1665 AI_ADDRCONFIG|AI_CANONNAME);
1667 if (!ret || ailist == NULL) {
1668 DEBUG(3,("matchname: getaddrinfo failed for "
1671 gai_strerror(ret) ));
1676 * Make sure that getaddrinfo() returns the "correct" host name.
1679 if (ailist->ai_canonname == NULL ||
1680 (!strequal(remotehost, ailist->ai_canonname) &&
1681 !strequal(remotehost, "localhost"))) {
1682 DEBUG(0,("matchname: host name/name mismatch: %s != %s\n",
1684 ailist->ai_canonname ?
1685 ailist->ai_canonname : "(NULL)"));
1686 freeaddrinfo(ailist);
1690 /* Look up the host address in the address list we just got. */
1691 for (res = ailist; res; res = res->ai_next) {
1692 if (!res->ai_addr) {
1695 if (addr_equal((const struct sockaddr_storage *)res->ai_addr,
1697 freeaddrinfo(ailist);
1703 * The host name does not map to the original host address. Perhaps
1704 * someone has compromised a name server. More likely someone botched
1705 * it, but that could be dangerous, too.
1708 DEBUG(0,("matchname: host name/address mismatch: %s != %s\n",
1709 print_sockaddr_len(addr_buf,
1713 ailist->ai_canonname ? ailist->ai_canonname : "(NULL)"));
1716 freeaddrinfo(ailist);
1721 /*******************************************************************
1722 Deal with the singleton cache.
1723 ******************************************************************/
1725 struct name_addr_pair {
1726 struct sockaddr_storage ss;
1730 /*******************************************************************
1731 Lookup a name/addr pair. Returns memory allocated from memcache.
1732 ******************************************************************/
1734 static bool lookup_nc(struct name_addr_pair *nc)
1740 if (!memcache_lookup(
1741 NULL, SINGLETON_CACHE,
1742 data_blob_string_const("get_peer_name"),
1747 memcpy(&nc->ss, tmp.data, sizeof(nc->ss));
1748 nc->name = (const char *)tmp.data + sizeof(nc->ss);
1752 /*******************************************************************
1753 Save a name/addr pair.
1754 ******************************************************************/
1756 static void store_nc(const struct name_addr_pair *nc)
1759 size_t namelen = strlen(nc->name);
1761 tmp = data_blob(NULL, sizeof(nc->ss) + namelen + 1);
1765 memcpy(tmp.data, &nc->ss, sizeof(nc->ss));
1766 memcpy(tmp.data+sizeof(nc->ss), nc->name, namelen+1);
1768 memcache_add(NULL, SINGLETON_CACHE,
1769 data_blob_string_const("get_peer_name"),
1771 data_blob_free(&tmp);
1774 /*******************************************************************
1775 Return the DNS name of the remote end of a socket.
1776 ******************************************************************/
1778 const char *get_peer_name(int fd, bool force_lookup)
1780 struct name_addr_pair nc;
1781 char addr_buf[INET6_ADDRSTRLEN];
1782 struct sockaddr_storage ss;
1783 socklen_t length = sizeof(ss);
1786 char name_buf[MAX_DNS_NAME_LENGTH];
1787 char tmp_name[MAX_DNS_NAME_LENGTH];
1789 /* reverse lookups can be *very* expensive, and in many
1790 situations won't work because many networks don't link dhcp
1791 with dns. To avoid the delay we avoid the lookup if
1793 if (!lp_hostname_lookups() && (force_lookup == false)) {
1794 length = sizeof(nc.ss);
1795 nc.name = get_peer_addr_internal(fd, addr_buf, sizeof(addr_buf),
1799 return nc.name ? nc.name : "UNKNOWN";
1804 memset(&ss, '\0', sizeof(ss));
1805 p = get_peer_addr_internal(fd, addr_buf, sizeof(addr_buf), &ss, &length);
1807 /* it might be the same as the last one - save some DNS work */
1808 if (addr_equal(&ss, &nc.ss)) {
1809 return nc.name ? nc.name : "UNKNOWN";
1812 /* Not the same. We need to lookup. */
1817 /* Look up the remote host name. */
1818 ret = sys_getnameinfo((struct sockaddr *)&ss,
1827 DEBUG(1,("get_peer_name: getnameinfo failed "
1828 "for %s with error %s\n",
1830 gai_strerror(ret)));
1831 strlcpy(name_buf, p, sizeof(name_buf));
1833 if (!matchname(name_buf, &ss, length)) {
1834 DEBUG(0,("Matchname failed on %s %s\n",name_buf,p));
1835 strlcpy(name_buf,"UNKNOWN",sizeof(name_buf));
1839 /* can't pass the same source and dest strings in when you
1840 use --enable-developer or the clobber_region() call will
1843 strlcpy(tmp_name, name_buf, sizeof(tmp_name));
1844 alpha_strcpy(name_buf, tmp_name, "_-.", sizeof(name_buf));
1845 if (strstr(name_buf,"..")) {
1846 strlcpy(name_buf, "UNKNOWN", sizeof(name_buf));
1854 return nc.name ? nc.name : "UNKNOWN";
1857 /*******************************************************************
1858 Return the IP addr of the remote end of a socket as a string.
1859 ******************************************************************/
1861 const char *get_peer_addr(int fd, char *addr, size_t addr_len)
1863 return get_peer_addr_internal(fd, addr, addr_len, NULL, NULL);
1866 /*******************************************************************
1867 Create protected unix domain socket.
1869 Some unixes cannot set permissions on a ux-dom-sock, so we
1870 have to make sure that the directory contains the protection
1871 permissions instead.
1872 ******************************************************************/
1874 int create_pipe_sock(const char *socket_dir,
1875 const char *socket_name,
1878 #ifdef HAVE_UNIXSOCKET
1879 struct sockaddr_un sunaddr;
1885 old_umask = umask(0);
1887 /* Create the socket directory or reuse the existing one */
1889 if (lstat(socket_dir, &st) == -1) {
1890 if (errno == ENOENT) {
1891 /* Create directory */
1892 if (mkdir(socket_dir, dir_perms) == -1) {
1893 DEBUG(0, ("error creating socket directory "
1894 "%s: %s\n", socket_dir,
1899 DEBUG(0, ("lstat failed on socket directory %s: %s\n",
1900 socket_dir, strerror(errno)));
1904 /* Check ownership and permission on existing directory */
1905 if (!S_ISDIR(st.st_mode)) {
1906 DEBUG(0, ("socket directory %s isn't a directory\n",
1910 if ((st.st_uid != sec_initial_uid()) ||
1911 ((st.st_mode & 0777) != dir_perms)) {
1912 DEBUG(0, ("invalid permissions on socket directory "
1913 "%s\n", socket_dir));
1918 /* Create the socket file */
1920 sock = socket(AF_UNIX, SOCK_STREAM, 0);
1923 DEBUG(0, ("create_pipe_sock: socket error %s\n",
1928 asprintf(&path, "%s/%s", socket_dir, socket_name);
1934 memset(&sunaddr, 0, sizeof(sunaddr));
1935 sunaddr.sun_family = AF_UNIX;
1936 strlcpy(sunaddr.sun_path, path, sizeof(sunaddr.sun_path));
1938 if (bind(sock, (struct sockaddr *)&sunaddr, sizeof(sunaddr)) == -1) {
1939 DEBUG(0, ("bind failed on pipe socket %s: %s\n", path,
1944 if (listen(sock, 5) == -1) {
1945 DEBUG(0, ("listen failed on pipe socket %s: %s\n", path,
1964 DEBUG(0, ("create_pipe_sock: No Unix sockets on this system\n"));
1966 #endif /* HAVE_UNIXSOCKET */
1969 /****************************************************************************
1970 Get my own canonical name, including domain.
1971 ****************************************************************************/
1973 const char *get_mydnsfullname(void)
1975 struct addrinfo *res = NULL;
1976 char my_hostname[HOST_NAME_MAX];
1980 if (memcache_lookup(NULL, SINGLETON_CACHE,
1981 data_blob_string_const("get_mydnsfullname"),
1983 SMB_ASSERT(tmp.length > 0);
1984 return (const char *)tmp.data;
1987 /* get my host name */
1988 if (gethostname(my_hostname, sizeof(my_hostname)) == -1) {
1989 DEBUG(0,("get_mydnsfullname: gethostname failed\n"));
1993 /* Ensure null termination. */
1994 my_hostname[sizeof(my_hostname)-1] = '\0';
1996 ret = interpret_string_addr_internal(&res,
1998 AI_ADDRCONFIG|AI_CANONNAME);
2000 if (!ret || res == NULL) {
2001 DEBUG(3,("get_mydnsfullname: getaddrinfo failed for "
2004 gai_strerror(ret) ));
2009 * Make sure that getaddrinfo() returns the "correct" host name.
2012 if (res->ai_canonname == NULL) {
2013 DEBUG(3,("get_mydnsfullname: failed to get "
2014 "canonical name for %s\n",
2020 /* This copies the data, so we must do a lookup
2021 * afterwards to find the value to return.
2024 memcache_add(NULL, SINGLETON_CACHE,
2025 data_blob_string_const("get_mydnsfullname"),
2026 data_blob_string_const(res->ai_canonname));
2028 if (!memcache_lookup(NULL, SINGLETON_CACHE,
2029 data_blob_string_const("get_mydnsfullname"),
2031 tmp = data_blob_talloc(talloc_tos(), res->ai_canonname,
2032 strlen(res->ai_canonname) + 1);
2037 return (const char *)tmp.data;
2040 /************************************************************
2042 ************************************************************/
2044 bool is_myname_or_ipaddr(const char *s)
2046 TALLOC_CTX *ctx = talloc_tos();
2048 const char *dnsname;
2049 char *servername = NULL;
2055 /* Santize the string from '\\name' */
2056 name = talloc_strdup(ctx, s);
2061 servername = strrchr_m(name, '\\' );
2068 /* Optimize for the common case */
2069 if (strequal(servername, global_myname())) {
2073 /* Check for an alias */
2074 if (is_myname(servername)) {
2078 /* Check for loopback */
2079 if (strequal(servername, "127.0.0.1") ||
2080 strequal(servername, "::1")) {
2084 if (strequal(servername, "localhost")) {
2088 /* Maybe it's my dns name */
2089 dnsname = get_mydnsfullname();
2090 if (dnsname && strequal(servername, dnsname)) {
2094 /* Handle possible CNAME records - convert to an IP addr. */
2095 if (!is_ipaddress(servername)) {
2096 /* Use DNS to resolve the name, but only the first address */
2097 struct sockaddr_storage ss;
2098 if (interpret_string_addr(&ss, servername,0)) {
2099 print_sockaddr(name,
2106 /* Maybe its an IP address? */
2107 if (is_ipaddress(servername)) {
2108 struct sockaddr_storage ss;
2109 struct iface_struct *nics;
2112 if (!interpret_string_addr(&ss, servername, AI_NUMERICHOST)) {
2116 if (is_zero_addr(&ss) || is_loopback_addr(&ss)) {
2120 nics = TALLOC_ARRAY(ctx, struct iface_struct,
2125 n = get_interfaces(nics, MAX_INTERFACES);
2126 for (i=0; i<n; i++) {
2127 if (addr_equal(&nics[i].ip, &ss)) {