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/>.
23 #include "system/filesys.h"
25 #include "../lib/async_req/async_sock.h"
26 #include "../lib/util/select.h"
27 #include "lib/socket/interfaces.h"
28 #include "../lib/util/tevent_unix.h"
29 #include "../lib/util/tevent_ntstatus.h"
31 const char *client_name(int fd
)
33 return get_peer_name(fd
,false);
36 const char *client_addr(int fd
, char *addr
, size_t addrlen
)
38 return get_peer_addr(fd
,addr
,addrlen
);
42 /* Not currently used. JRA. */
43 int client_socket_port(int fd
)
45 return get_socket_port(fd
);
49 /****************************************************************************
50 Accessor functions to make thread-safe code easier later...
51 ****************************************************************************/
53 void set_smb_read_error(enum smb_read_errors
*pre
,
54 enum smb_read_errors newerr
)
61 void cond_set_smb_read_error(enum smb_read_errors
*pre
,
62 enum smb_read_errors newerr
)
64 if (pre
&& *pre
== SMB_READ_OK
) {
69 /****************************************************************************
70 Determine if a file descriptor is in fact a socket.
71 ****************************************************************************/
73 bool is_a_socket(int fd
)
78 return(getsockopt(fd
, SOL_SOCKET
, SO_TYPE
, (char *)&v
, &l
) == 0);
81 /****************************************************************************
83 ****************************************************************************/
85 ssize_t
read_udp_v4_socket(int fd
,
88 struct sockaddr_storage
*psa
)
91 socklen_t socklen
= sizeof(*psa
);
92 struct sockaddr_in
*si
= (struct sockaddr_in
*)psa
;
94 memset((char *)psa
,'\0',socklen
);
96 ret
= (ssize_t
)sys_recvfrom(fd
,buf
,len
,0,
97 (struct sockaddr
*)psa
,&socklen
);
99 /* Don't print a low debug error for a non-blocking socket. */
100 if (errno
== EAGAIN
) {
101 DEBUG(10,("read_udp_v4_socket: returned EAGAIN\n"));
103 DEBUG(2,("read_udp_v4_socket: failed. errno=%s\n",
109 if (psa
->ss_family
!= AF_INET
) {
110 DEBUG(2,("read_udp_v4_socket: invalid address family %d "
111 "(not IPv4)\n", (int)psa
->ss_family
));
115 DEBUG(10,("read_udp_v4_socket: ip %s port %d read: %lu\n",
116 inet_ntoa(si
->sin_addr
),
118 (unsigned long)ret
));
123 /****************************************************************************
124 Read data from a file descriptor with a timout in msec.
125 mincount = if timeout, minimum to read before returning
126 maxcount = number to be read.
127 time_out = timeout in milliseconds
128 NB. This can be called with a non-socket fd, don't change
129 sys_read() to sys_recv() or other socket call.
130 ****************************************************************************/
132 NTSTATUS
read_fd_with_timeout(int fd
, char *buf
,
133 size_t mincnt
, size_t maxcnt
,
134 unsigned int time_out
,
141 /* just checking .... */
151 while (nread
< mincnt
) {
152 readret
= sys_read(fd
, buf
+ nread
, maxcnt
- nread
);
155 DEBUG(5,("read_fd_with_timeout: "
156 "blocking read. EOF from client.\n"));
157 return NT_STATUS_END_OF_FILE
;
161 return map_nt_error_from_unix(errno
);
168 /* Most difficult - timeout read */
169 /* If this is ever called on a disk file and
170 mincnt is greater then the filesize then
171 system performance will suffer severely as
172 select always returns true on disk files */
174 for (nread
=0; nread
< mincnt
; ) {
177 pollrtn
= poll_intr_one_fd(fd
, POLLIN
|POLLHUP
, time_out
,
182 return map_nt_error_from_unix(errno
);
185 /* Did we timeout ? */
186 if ((pollrtn
== 0) ||
187 ((revents
& (POLLIN
|POLLHUP
|POLLERR
)) == 0)) {
188 DEBUG(10,("read_fd_with_timeout: timeout read. "
189 "select timed out.\n"));
190 return NT_STATUS_IO_TIMEOUT
;
193 readret
= sys_read(fd
, buf
+nread
, maxcnt
-nread
);
196 /* we got EOF on the file descriptor */
197 DEBUG(5,("read_fd_with_timeout: timeout read. "
198 "EOF from client.\n"));
199 return NT_STATUS_END_OF_FILE
;
203 return map_nt_error_from_unix(errno
);
210 /* Return the number we got */
217 /****************************************************************************
218 Read data from an fd, reading exactly N bytes.
219 NB. This can be called with a non-socket fd, don't add dependencies
221 ****************************************************************************/
223 NTSTATUS
read_data(int fd
, char *buffer
, size_t N
)
225 return read_fd_with_timeout(fd
, buffer
, N
, N
, 0, NULL
);
228 /****************************************************************************
229 Write all data from an iov array
230 NB. This can be called with a non-socket fd, don't add dependencies
232 ****************************************************************************/
234 ssize_t
write_data_iov(int fd
, const struct iovec
*orig_iov
, int iovcnt
)
240 struct iovec
*iov_copy
, *iov
;
243 for (i
=0; i
<iovcnt
; i
++) {
244 to_send
+= orig_iov
[i
].iov_len
;
247 thistime
= sys_writev(fd
, orig_iov
, iovcnt
);
248 if ((thistime
<= 0) || (thistime
== to_send
)) {
254 * We could not send everything in one call. Make a copy of iov that
255 * we can mess with. We keep a copy of the array start in iov_copy for
256 * the TALLOC_FREE, because we're going to modify iov later on,
257 * discarding elements.
260 iov_copy
= (struct iovec
*)talloc_memdup(
261 talloc_tos(), orig_iov
, sizeof(struct iovec
) * iovcnt
);
263 if (iov_copy
== NULL
) {
269 while (sent
< to_send
) {
271 * We have to discard "thistime" bytes from the beginning
272 * iov array, "thistime" contains the number of bytes sent
275 while (thistime
> 0) {
276 if (thistime
< iov
[0].iov_len
) {
278 (char *)iov
[0].iov_base
+ thistime
;
279 iov
[0].iov_base
= (void *)new_base
;
280 iov
[0].iov_len
-= thistime
;
283 thistime
-= iov
[0].iov_len
;
288 thistime
= sys_writev(fd
, iov
, iovcnt
);
295 TALLOC_FREE(iov_copy
);
299 /****************************************************************************
301 NB. This can be called with a non-socket fd, don't add dependencies
303 ****************************************************************************/
305 ssize_t
write_data(int fd
, const char *buffer
, size_t N
)
309 iov
.iov_base
= discard_const_p(void, buffer
);
311 return write_data_iov(fd
, &iov
, 1);
314 /****************************************************************************
315 Send a keepalive packet (rfc1002).
316 ****************************************************************************/
318 bool send_keepalive(int client
)
320 unsigned char buf
[4];
322 buf
[0] = SMBkeepalive
;
323 buf
[1] = buf
[2] = buf
[3] = 0;
325 return(write_data(client
,(char *)buf
,4) == 4);
328 /****************************************************************************
329 Read 4 bytes of a smb packet and return the smb length of the packet.
330 Store the result in the buffer.
331 This version of the function will return a length of zero on receiving
333 Timeout is in milliseconds.
334 ****************************************************************************/
336 NTSTATUS
read_smb_length_return_keepalive(int fd
, char *inbuf
,
337 unsigned int timeout
,
343 status
= read_fd_with_timeout(fd
, inbuf
, 4, 4, timeout
, NULL
);
345 if (!NT_STATUS_IS_OK(status
)) {
349 *len
= smb_len(inbuf
);
350 msg_type
= CVAL(inbuf
,0);
352 if (msg_type
== SMBkeepalive
) {
353 DEBUG(5,("Got keepalive packet\n"));
356 DEBUG(10,("got smb length of %lu\n",(unsigned long)(*len
)));
361 /****************************************************************************
362 Read an smb from a fd.
363 The timeout is in milliseconds.
364 This function will return on receipt of a session keepalive packet.
365 maxlen is the max number of bytes to return, not including the 4 byte
366 length. If zero it means buflen limit.
367 Doesn't check the MAC on signed packets.
368 ****************************************************************************/
370 NTSTATUS
receive_smb_raw(int fd
, char *buffer
, size_t buflen
, unsigned int timeout
,
371 size_t maxlen
, size_t *p_len
)
376 status
= read_smb_length_return_keepalive(fd
,buffer
,timeout
,&len
);
378 if (!NT_STATUS_IS_OK(status
)) {
379 DEBUG(0, ("read_fd_with_timeout failed, read "
380 "error = %s.\n", nt_errstr(status
)));
385 DEBUG(0,("Invalid packet length! (%lu bytes).\n",
386 (unsigned long)len
));
387 return NT_STATUS_INVALID_PARAMETER
;
392 len
= MIN(len
,maxlen
);
395 status
= read_fd_with_timeout(
396 fd
, buffer
+4, len
, len
, timeout
, &len
);
398 if (!NT_STATUS_IS_OK(status
)) {
399 DEBUG(0, ("read_fd_with_timeout failed, read error = "
400 "%s.\n", nt_errstr(status
)));
404 /* not all of samba3 properly checks for packet-termination
405 * of strings. This ensures that we don't run off into
407 SSVAL(buffer
+4,len
, 0);
414 /****************************************************************************
415 Open a socket of the specified type, port, and address for incoming data.
416 ****************************************************************************/
418 int open_socket_in(int type
,
421 const struct sockaddr_storage
*psock
,
424 struct sockaddr_storage sock
;
426 socklen_t slen
= sizeof(struct sockaddr_in
);
430 #if defined(HAVE_IPV6)
431 if (sock
.ss_family
== AF_INET6
) {
432 ((struct sockaddr_in6
*)&sock
)->sin6_port
= htons(port
);
433 slen
= sizeof(struct sockaddr_in6
);
436 if (sock
.ss_family
== AF_INET
) {
437 ((struct sockaddr_in
*)&sock
)->sin_port
= htons(port
);
440 res
= socket(sock
.ss_family
, type
, 0 );
443 dbgtext( "open_socket_in(): socket() call failed: " );
444 dbgtext( "%s\n", strerror( errno
) );
449 /* This block sets/clears the SO_REUSEADDR and possibly SO_REUSEPORT. */
451 int val
= rebind
? 1 : 0;
452 if( setsockopt(res
,SOL_SOCKET
,SO_REUSEADDR
,
453 (char *)&val
,sizeof(val
)) == -1 ) {
454 if( DEBUGLVL( dlevel
) ) {
455 dbgtext( "open_socket_in(): setsockopt: " );
456 dbgtext( "SO_REUSEADDR = %s ",
457 val
?"true":"false" );
458 dbgtext( "on port %d failed ", port
);
459 dbgtext( "with error = %s\n", strerror(errno
) );
463 if( setsockopt(res
,SOL_SOCKET
,SO_REUSEPORT
,
464 (char *)&val
,sizeof(val
)) == -1 ) {
465 if( DEBUGLVL( dlevel
) ) {
466 dbgtext( "open_socket_in(): setsockopt: ");
467 dbgtext( "SO_REUSEPORT = %s ",
469 dbgtext( "on port %d failed ", port
);
470 dbgtext( "with error = %s\n", strerror(errno
));
473 #endif /* SO_REUSEPORT */
478 * As IPV6_V6ONLY is the default on some systems,
479 * we better try to be consistent and always use it.
481 * This also avoids using IPv4 via AF_INET6 sockets
482 * and makes sure %I never resolves to a '::ffff:192.168.0.1'
485 if (sock
.ss_family
== AF_INET6
) {
489 ret
= setsockopt(res
, IPPROTO_IPV6
, IPV6_V6ONLY
,
490 (const void *)&val
, sizeof(val
));
493 dbgtext("open_socket_in(): IPV6_ONLY failed: ");
494 dbgtext("%s\n", strerror(errno
));
502 /* now we've got a socket - we need to bind it */
503 if (bind(res
, (struct sockaddr
*)&sock
, slen
) == -1 ) {
504 if( DEBUGLVL(dlevel
) && (port
== SMB_PORT1
||
505 port
== SMB_PORT2
|| port
== NMB_PORT
) ) {
506 char addr
[INET6_ADDRSTRLEN
];
507 print_sockaddr(addr
, sizeof(addr
),
509 dbgtext( "bind failed on port %d ", port
);
510 dbgtext( "socket_addr = %s.\n", addr
);
511 dbgtext( "Error = %s\n", strerror(errno
));
517 DEBUG( 10, ( "bind succeeded on port %d\n", port
) );
521 struct open_socket_out_state
{
523 struct event_context
*ev
;
524 struct sockaddr_storage ss
;
530 static void open_socket_out_connected(struct tevent_req
*subreq
);
532 static int open_socket_out_state_destructor(struct open_socket_out_state
*s
)
540 /****************************************************************************
541 Create an outgoing socket. timeout is in milliseconds.
542 **************************************************************************/
544 struct tevent_req
*open_socket_out_send(TALLOC_CTX
*mem_ctx
,
545 struct event_context
*ev
,
546 const struct sockaddr_storage
*pss
,
550 char addr
[INET6_ADDRSTRLEN
];
551 struct tevent_req
*result
, *subreq
;
552 struct open_socket_out_state
*state
;
555 result
= tevent_req_create(mem_ctx
, &state
,
556 struct open_socket_out_state
);
557 if (result
== NULL
) {
563 state
->wait_usec
= 10000;
566 state
->fd
= socket(state
->ss
.ss_family
, SOCK_STREAM
, 0);
567 if (state
->fd
== -1) {
568 status
= map_nt_error_from_unix(errno
);
571 talloc_set_destructor(state
, open_socket_out_state_destructor
);
573 if (!tevent_req_set_endtime(
574 result
, ev
, timeval_current_ofs_msec(timeout
))) {
578 #if defined(HAVE_IPV6)
579 if (pss
->ss_family
== AF_INET6
) {
580 struct sockaddr_in6
*psa6
;
581 psa6
= (struct sockaddr_in6
*)&state
->ss
;
582 psa6
->sin6_port
= htons(port
);
583 if (psa6
->sin6_scope_id
== 0
584 && IN6_IS_ADDR_LINKLOCAL(&psa6
->sin6_addr
)) {
585 setup_linklocal_scope_id(
586 (struct sockaddr
*)&(state
->ss
));
588 state
->salen
= sizeof(struct sockaddr_in6
);
591 if (pss
->ss_family
== AF_INET
) {
592 struct sockaddr_in
*psa
;
593 psa
= (struct sockaddr_in
*)&state
->ss
;
594 psa
->sin_port
= htons(port
);
595 state
->salen
= sizeof(struct sockaddr_in
);
598 if (pss
->ss_family
== AF_UNIX
) {
599 state
->salen
= sizeof(struct sockaddr_un
);
602 print_sockaddr(addr
, sizeof(addr
), &state
->ss
);
603 DEBUG(3,("Connecting to %s at port %u\n", addr
, (unsigned int)port
));
605 subreq
= async_connect_send(state
, state
->ev
, state
->fd
,
606 (struct sockaddr
*)&state
->ss
,
609 || !tevent_req_set_endtime(
611 timeval_current_ofs(0, state
->wait_usec
))) {
614 tevent_req_set_callback(subreq
, open_socket_out_connected
, result
);
618 tevent_req_nterror(result
, status
);
619 return tevent_req_post(result
, ev
);
625 static void open_socket_out_connected(struct tevent_req
*subreq
)
627 struct tevent_req
*req
=
628 tevent_req_callback_data(subreq
, struct tevent_req
);
629 struct open_socket_out_state
*state
=
630 tevent_req_data(req
, struct open_socket_out_state
);
634 ret
= async_connect_recv(subreq
, &sys_errno
);
637 tevent_req_done(req
);
643 (sys_errno
== ETIMEDOUT
) ||
645 (sys_errno
== EINPROGRESS
) ||
646 (sys_errno
== EALREADY
) ||
647 (sys_errno
== EAGAIN
)) {
653 if (state
->wait_usec
< 250000) {
654 state
->wait_usec
*= 1.5;
657 subreq
= async_connect_send(state
, state
->ev
, state
->fd
,
658 (struct sockaddr
*)&state
->ss
,
660 if (tevent_req_nomem(subreq
, req
)) {
663 if (!tevent_req_set_endtime(
665 timeval_current_ofs_usec(state
->wait_usec
))) {
666 tevent_req_nterror(req
, NT_STATUS_NO_MEMORY
);
669 tevent_req_set_callback(subreq
, open_socket_out_connected
, req
);
674 if (sys_errno
== EISCONN
) {
675 tevent_req_done(req
);
681 tevent_req_nterror(req
, map_nt_error_from_unix(sys_errno
));
684 NTSTATUS
open_socket_out_recv(struct tevent_req
*req
, int *pfd
)
686 struct open_socket_out_state
*state
=
687 tevent_req_data(req
, struct open_socket_out_state
);
690 if (tevent_req_is_nterror(req
, &status
)) {
699 * @brief open a socket
701 * @param pss a struct sockaddr_storage defining the address to connect to
702 * @param port to connect to
703 * @param timeout in MILLISECONDS
704 * @param pfd file descriptor returned
706 * @return NTSTATUS code
708 NTSTATUS
open_socket_out(const struct sockaddr_storage
*pss
, uint16_t port
,
709 int timeout
, int *pfd
)
711 TALLOC_CTX
*frame
= talloc_stackframe();
712 struct event_context
*ev
;
713 struct tevent_req
*req
;
714 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
716 ev
= event_context_init(frame
);
721 req
= open_socket_out_send(frame
, ev
, pss
, port
, timeout
);
725 if (!tevent_req_poll(req
, ev
)) {
726 status
= NT_STATUS_INTERNAL_ERROR
;
729 status
= open_socket_out_recv(req
, pfd
);
735 struct open_socket_out_defer_state
{
736 struct event_context
*ev
;
737 struct sockaddr_storage ss
;
743 static void open_socket_out_defer_waited(struct tevent_req
*subreq
);
744 static void open_socket_out_defer_connected(struct tevent_req
*subreq
);
746 struct tevent_req
*open_socket_out_defer_send(TALLOC_CTX
*mem_ctx
,
747 struct event_context
*ev
,
748 struct timeval wait_time
,
749 const struct sockaddr_storage
*pss
,
753 struct tevent_req
*req
, *subreq
;
754 struct open_socket_out_defer_state
*state
;
756 req
= tevent_req_create(mem_ctx
, &state
,
757 struct open_socket_out_defer_state
);
764 state
->timeout
= timeout
;
766 subreq
= tevent_wakeup_send(
768 timeval_current_ofs(wait_time
.tv_sec
, wait_time
.tv_usec
));
769 if (subreq
== NULL
) {
772 tevent_req_set_callback(subreq
, open_socket_out_defer_waited
, req
);
779 static void open_socket_out_defer_waited(struct tevent_req
*subreq
)
781 struct tevent_req
*req
= tevent_req_callback_data(
782 subreq
, struct tevent_req
);
783 struct open_socket_out_defer_state
*state
= tevent_req_data(
784 req
, struct open_socket_out_defer_state
);
787 ret
= tevent_wakeup_recv(subreq
);
790 tevent_req_nterror(req
, NT_STATUS_INTERNAL_ERROR
);
794 subreq
= open_socket_out_send(state
, state
->ev
, &state
->ss
,
795 state
->port
, state
->timeout
);
796 if (tevent_req_nomem(subreq
, req
)) {
799 tevent_req_set_callback(subreq
, open_socket_out_defer_connected
, req
);
802 static void open_socket_out_defer_connected(struct tevent_req
*subreq
)
804 struct tevent_req
*req
= tevent_req_callback_data(
805 subreq
, struct tevent_req
);
806 struct open_socket_out_defer_state
*state
= tevent_req_data(
807 req
, struct open_socket_out_defer_state
);
810 status
= open_socket_out_recv(subreq
, &state
->fd
);
812 if (!NT_STATUS_IS_OK(status
)) {
813 tevent_req_nterror(req
, status
);
816 tevent_req_done(req
);
819 NTSTATUS
open_socket_out_defer_recv(struct tevent_req
*req
, int *pfd
)
821 struct open_socket_out_defer_state
*state
= tevent_req_data(
822 req
, struct open_socket_out_defer_state
);
825 if (tevent_req_is_nterror(req
, &status
)) {
833 /****************************************************************************
834 Open a connected UDP socket to host on port
835 **************************************************************************/
837 int open_udp_socket(const char *host
, int port
)
839 struct sockaddr_storage ss
;
842 if (!interpret_string_addr(&ss
, host
, 0)) {
843 DEBUG(10,("open_udp_socket: can't resolve name %s\n",
848 res
= socket(ss
.ss_family
, SOCK_DGRAM
, 0);
853 #if defined(HAVE_IPV6)
854 if (ss
.ss_family
== AF_INET6
) {
855 struct sockaddr_in6
*psa6
;
856 psa6
= (struct sockaddr_in6
*)&ss
;
857 psa6
->sin6_port
= htons(port
);
858 if (psa6
->sin6_scope_id
== 0
859 && IN6_IS_ADDR_LINKLOCAL(&psa6
->sin6_addr
)) {
860 setup_linklocal_scope_id(
861 (struct sockaddr
*)&ss
);
865 if (ss
.ss_family
== AF_INET
) {
866 struct sockaddr_in
*psa
;
867 psa
= (struct sockaddr_in
*)&ss
;
868 psa
->sin_port
= htons(port
);
871 if (sys_connect(res
,(struct sockaddr
*)&ss
)) {
879 /*******************************************************************
880 Return the IP addr of the remote end of a socket as a string.
881 Optionally return the struct sockaddr_storage.
882 ******************************************************************/
884 static const char *get_peer_addr_internal(int fd
,
887 struct sockaddr
*pss
,
890 struct sockaddr_storage ss
;
891 socklen_t length
= sizeof(ss
);
893 strlcpy(addr_buf
,"0.0.0.0",addr_buf_len
);
900 pss
= (struct sockaddr
*)&ss
;
904 if (getpeername(fd
, (struct sockaddr
*)pss
, plength
) < 0) {
905 int level
= (errno
== ENOTCONN
) ? 2 : 0;
906 DEBUG(level
, ("getpeername failed. Error was %s\n",
911 print_sockaddr_len(addr_buf
,
918 /*******************************************************************
919 Matchname - determine if host name matches IP address. Used to
920 confirm a hostname lookup to prevent spoof attacks.
921 ******************************************************************/
923 static bool matchname(const char *remotehost
,
924 const struct sockaddr
*pss
,
927 struct addrinfo
*res
= NULL
;
928 struct addrinfo
*ailist
= NULL
;
929 char addr_buf
[INET6_ADDRSTRLEN
];
930 bool ret
= interpret_string_addr_internal(&ailist
,
932 AI_ADDRCONFIG
|AI_CANONNAME
);
934 if (!ret
|| ailist
== NULL
) {
935 DEBUG(3,("matchname: getaddrinfo failed for "
938 gai_strerror(ret
) ));
943 * Make sure that getaddrinfo() returns the "correct" host name.
946 if (ailist
->ai_canonname
== NULL
||
947 (!strequal(remotehost
, ailist
->ai_canonname
) &&
948 !strequal(remotehost
, "localhost"))) {
949 DEBUG(0,("matchname: host name/name mismatch: %s != %s\n",
951 ailist
->ai_canonname
?
952 ailist
->ai_canonname
: "(NULL)"));
953 freeaddrinfo(ailist
);
957 /* Look up the host address in the address list we just got. */
958 for (res
= ailist
; res
; res
= res
->ai_next
) {
962 if (sockaddr_equal((const struct sockaddr
*)res
->ai_addr
,
963 (const struct sockaddr
*)pss
)) {
964 freeaddrinfo(ailist
);
970 * The host name does not map to the original host address. Perhaps
971 * someone has compromised a name server. More likely someone botched
972 * it, but that could be dangerous, too.
975 DEBUG(0,("matchname: host name/address mismatch: %s != %s\n",
976 print_sockaddr_len(addr_buf
,
980 ailist
->ai_canonname
? ailist
->ai_canonname
: "(NULL)"));
983 freeaddrinfo(ailist
);
988 /*******************************************************************
989 Deal with the singleton cache.
990 ******************************************************************/
992 struct name_addr_pair
{
993 struct sockaddr_storage ss
;
997 /*******************************************************************
998 Lookup a name/addr pair. Returns memory allocated from memcache.
999 ******************************************************************/
1001 static bool lookup_nc(struct name_addr_pair
*nc
)
1007 if (!memcache_lookup(
1008 NULL
, SINGLETON_CACHE
,
1009 data_blob_string_const_null("get_peer_name"),
1014 memcpy(&nc
->ss
, tmp
.data
, sizeof(nc
->ss
));
1015 nc
->name
= (const char *)tmp
.data
+ sizeof(nc
->ss
);
1019 /*******************************************************************
1020 Save a name/addr pair.
1021 ******************************************************************/
1023 static void store_nc(const struct name_addr_pair
*nc
)
1026 size_t namelen
= strlen(nc
->name
);
1028 tmp
= data_blob(NULL
, sizeof(nc
->ss
) + namelen
+ 1);
1032 memcpy(tmp
.data
, &nc
->ss
, sizeof(nc
->ss
));
1033 memcpy(tmp
.data
+sizeof(nc
->ss
), nc
->name
, namelen
+1);
1035 memcache_add(NULL
, SINGLETON_CACHE
,
1036 data_blob_string_const_null("get_peer_name"),
1038 data_blob_free(&tmp
);
1041 /*******************************************************************
1042 Return the DNS name of the remote end of a socket.
1043 ******************************************************************/
1045 const char *get_peer_name(int fd
, bool force_lookup
)
1047 struct name_addr_pair nc
;
1048 char addr_buf
[INET6_ADDRSTRLEN
];
1049 struct sockaddr_storage ss
;
1050 socklen_t length
= sizeof(ss
);
1053 char name_buf
[MAX_DNS_NAME_LENGTH
];
1054 char tmp_name
[MAX_DNS_NAME_LENGTH
];
1056 /* reverse lookups can be *very* expensive, and in many
1057 situations won't work because many networks don't link dhcp
1058 with dns. To avoid the delay we avoid the lookup if
1060 if (!lp_hostname_lookups() && (force_lookup
== false)) {
1061 length
= sizeof(nc
.ss
);
1062 nc
.name
= get_peer_addr_internal(fd
, addr_buf
, sizeof(addr_buf
),
1063 (struct sockaddr
*)&nc
.ss
, &length
);
1066 return nc
.name
? nc
.name
: "UNKNOWN";
1071 memset(&ss
, '\0', sizeof(ss
));
1072 p
= get_peer_addr_internal(fd
, addr_buf
, sizeof(addr_buf
), (struct sockaddr
*)&ss
, &length
);
1074 /* it might be the same as the last one - save some DNS work */
1075 if (sockaddr_equal((struct sockaddr
*)&ss
, (struct sockaddr
*)&nc
.ss
)) {
1076 return nc
.name
? nc
.name
: "UNKNOWN";
1079 /* Not the same. We need to lookup. */
1084 /* Look up the remote host name. */
1085 ret
= sys_getnameinfo((struct sockaddr
*)&ss
,
1094 DEBUG(1,("get_peer_name: getnameinfo failed "
1095 "for %s with error %s\n",
1097 gai_strerror(ret
)));
1098 strlcpy(name_buf
, p
, sizeof(name_buf
));
1100 if (!matchname(name_buf
, (struct sockaddr
*)&ss
, length
)) {
1101 DEBUG(0,("Matchname failed on %s %s\n",name_buf
,p
));
1102 strlcpy(name_buf
,"UNKNOWN",sizeof(name_buf
));
1106 strlcpy(tmp_name
, name_buf
, sizeof(tmp_name
));
1107 alpha_strcpy(name_buf
, tmp_name
, "_-.", sizeof(name_buf
));
1108 if (strstr(name_buf
,"..")) {
1109 strlcpy(name_buf
, "UNKNOWN", sizeof(name_buf
));
1117 return nc
.name
? nc
.name
: "UNKNOWN";
1120 /*******************************************************************
1121 Return the IP addr of the remote end of a socket as a string.
1122 ******************************************************************/
1124 const char *get_peer_addr(int fd
, char *addr
, size_t addr_len
)
1126 return get_peer_addr_internal(fd
, addr
, addr_len
, NULL
, NULL
);
1129 /*******************************************************************
1130 Create protected unix domain socket.
1132 Some unixes cannot set permissions on a ux-dom-sock, so we
1133 have to make sure that the directory contains the protection
1134 permissions instead.
1135 ******************************************************************/
1137 int create_pipe_sock(const char *socket_dir
,
1138 const char *socket_name
,
1141 #ifdef HAVE_UNIXSOCKET
1142 struct sockaddr_un sunaddr
;
1148 old_umask
= umask(0);
1150 /* Create the socket directory or reuse the existing one */
1152 if (lstat(socket_dir
, &st
) == -1) {
1153 if (errno
== ENOENT
) {
1154 /* Create directory */
1155 if (mkdir(socket_dir
, dir_perms
) == -1) {
1156 DEBUG(0, ("error creating socket directory "
1157 "%s: %s\n", socket_dir
,
1162 DEBUG(0, ("lstat failed on socket directory %s: %s\n",
1163 socket_dir
, strerror(errno
)));
1167 /* Check ownership and permission on existing directory */
1168 if (!S_ISDIR(st
.st_mode
)) {
1169 DEBUG(0, ("socket directory '%s' isn't a directory\n",
1173 if (st
.st_uid
!= sec_initial_uid()) {
1174 DEBUG(0, ("invalid ownership on directory "
1175 "'%s'\n", socket_dir
));
1179 if ((st
.st_mode
& 0777) != dir_perms
) {
1180 DEBUG(0, ("invalid permissions on directory "
1181 "'%s': has 0%o should be 0%o\n", socket_dir
,
1182 (st
.st_mode
& 0777), dir_perms
));
1188 /* Create the socket file */
1190 sock
= socket(AF_UNIX
, SOCK_STREAM
, 0);
1193 DEBUG(0, ("create_pipe_sock: socket error %s\n",
1198 if (asprintf(&path
, "%s/%s", socket_dir
, socket_name
) == -1) {
1203 memset(&sunaddr
, 0, sizeof(sunaddr
));
1204 sunaddr
.sun_family
= AF_UNIX
;
1205 strlcpy(sunaddr
.sun_path
, path
, sizeof(sunaddr
.sun_path
));
1207 if (bind(sock
, (struct sockaddr
*)&sunaddr
, sizeof(sunaddr
)) == -1) {
1208 DEBUG(0, ("bind failed on pipe socket %s: %s\n", path
,
1213 if (listen(sock
, 5) == -1) {
1214 DEBUG(0, ("listen failed on pipe socket %s: %s\n", path
,
1234 DEBUG(0, ("create_pipe_sock: No Unix sockets on this system\n"));
1236 #endif /* HAVE_UNIXSOCKET */
1239 /****************************************************************************
1240 Get my own canonical name, including domain.
1241 ****************************************************************************/
1243 const char *get_mydnsfullname(void)
1245 struct addrinfo
*res
= NULL
;
1246 char my_hostname
[HOST_NAME_MAX
];
1250 if (memcache_lookup(NULL
, SINGLETON_CACHE
,
1251 data_blob_string_const_null("get_mydnsfullname"),
1253 SMB_ASSERT(tmp
.length
> 0);
1254 return (const char *)tmp
.data
;
1257 /* get my host name */
1258 if (gethostname(my_hostname
, sizeof(my_hostname
)) == -1) {
1259 DEBUG(0,("get_mydnsfullname: gethostname failed\n"));
1263 /* Ensure null termination. */
1264 my_hostname
[sizeof(my_hostname
)-1] = '\0';
1266 ret
= interpret_string_addr_internal(&res
,
1268 AI_ADDRCONFIG
|AI_CANONNAME
);
1270 if (!ret
|| res
== NULL
) {
1271 DEBUG(3,("get_mydnsfullname: getaddrinfo failed for "
1274 gai_strerror(ret
) ));
1279 * Make sure that getaddrinfo() returns the "correct" host name.
1282 if (res
->ai_canonname
== NULL
) {
1283 DEBUG(3,("get_mydnsfullname: failed to get "
1284 "canonical name for %s\n",
1290 /* This copies the data, so we must do a lookup
1291 * afterwards to find the value to return.
1294 memcache_add(NULL
, SINGLETON_CACHE
,
1295 data_blob_string_const_null("get_mydnsfullname"),
1296 data_blob_string_const_null(res
->ai_canonname
));
1298 if (!memcache_lookup(NULL
, SINGLETON_CACHE
,
1299 data_blob_string_const_null("get_mydnsfullname"),
1301 tmp
= data_blob_talloc(talloc_tos(), res
->ai_canonname
,
1302 strlen(res
->ai_canonname
) + 1);
1307 return (const char *)tmp
.data
;
1310 /************************************************************
1311 Is this my ip address ?
1312 ************************************************************/
1314 static bool is_my_ipaddr(const char *ipaddr_str
)
1316 struct sockaddr_storage ss
;
1317 struct iface_struct
*nics
;
1320 if (!interpret_string_addr(&ss
, ipaddr_str
, AI_NUMERICHOST
)) {
1324 if (is_zero_addr(&ss
)) {
1328 if (ismyaddr((struct sockaddr
*)&ss
) ||
1329 is_loopback_addr((struct sockaddr
*)&ss
)) {
1333 n
= get_interfaces(talloc_tos(), &nics
);
1334 for (i
=0; i
<n
; i
++) {
1335 if (sockaddr_equal((struct sockaddr
*)&nics
[i
].ip
, (struct sockaddr
*)&ss
)) {
1344 /************************************************************
1346 ************************************************************/
1348 bool is_myname_or_ipaddr(const char *s
)
1350 TALLOC_CTX
*ctx
= talloc_tos();
1352 const char *dnsname
;
1353 char *servername
= NULL
;
1359 /* Santize the string from '\\name' */
1360 name
= talloc_strdup(ctx
, s
);
1365 servername
= strrchr_m(name
, '\\' );
1372 /* Optimize for the common case */
1373 if (strequal(servername
, lp_netbios_name())) {
1377 /* Check for an alias */
1378 if (is_myname(servername
)) {
1382 /* Check for loopback */
1383 if (strequal(servername
, "127.0.0.1") ||
1384 strequal(servername
, "::1")) {
1388 if (strequal(servername
, "localhost")) {
1392 /* Maybe it's my dns name */
1393 dnsname
= get_mydnsfullname();
1394 if (dnsname
&& strequal(servername
, dnsname
)) {
1398 /* Maybe its an IP address? */
1399 if (is_ipaddress(servername
)) {
1400 return is_my_ipaddr(servername
);
1403 /* Handle possible CNAME records - convert to an IP addr. list. */
1405 /* Use DNS to resolve the name, check all addresses. */
1406 struct addrinfo
*p
= NULL
;
1407 struct addrinfo
*res
= NULL
;
1409 if (!interpret_string_addr_internal(&res
,
1415 for (p
= res
; p
; p
= p
->ai_next
) {
1416 char addr
[INET6_ADDRSTRLEN
];
1417 struct sockaddr_storage ss
;
1420 memcpy(&ss
, p
->ai_addr
, p
->ai_addrlen
);
1421 print_sockaddr(addr
,
1424 if (is_my_ipaddr(addr
)) {
1436 struct getaddrinfo_state
{
1438 const char *service
;
1439 const struct addrinfo
*hints
;
1440 struct addrinfo
*res
;
1444 static void getaddrinfo_do(void *private_data
);
1445 static void getaddrinfo_done(struct tevent_req
*subreq
);
1447 struct tevent_req
*getaddrinfo_send(TALLOC_CTX
*mem_ctx
,
1448 struct tevent_context
*ev
,
1449 struct fncall_context
*ctx
,
1451 const char *service
,
1452 const struct addrinfo
*hints
)
1454 struct tevent_req
*req
, *subreq
;
1455 struct getaddrinfo_state
*state
;
1457 req
= tevent_req_create(mem_ctx
, &state
, struct getaddrinfo_state
);
1463 state
->service
= service
;
1464 state
->hints
= hints
;
1466 subreq
= fncall_send(state
, ev
, ctx
, getaddrinfo_do
, state
);
1467 if (tevent_req_nomem(subreq
, req
)) {
1468 return tevent_req_post(req
, ev
);
1470 tevent_req_set_callback(subreq
, getaddrinfo_done
, req
);
1474 static void getaddrinfo_do(void *private_data
)
1476 struct getaddrinfo_state
*state
=
1477 (struct getaddrinfo_state
*)private_data
;
1479 state
->ret
= getaddrinfo(state
->node
, state
->service
, state
->hints
,
1483 static void getaddrinfo_done(struct tevent_req
*subreq
)
1485 struct tevent_req
*req
= tevent_req_callback_data(
1486 subreq
, struct tevent_req
);
1489 ret
= fncall_recv(subreq
, &err
);
1490 TALLOC_FREE(subreq
);
1492 tevent_req_error(req
, err
);
1495 tevent_req_done(req
);
1498 int getaddrinfo_recv(struct tevent_req
*req
, struct addrinfo
**res
)
1500 struct getaddrinfo_state
*state
= tevent_req_data(
1501 req
, struct getaddrinfo_state
);
1504 if (tevent_req_is_unix_error(req
, &err
)) {
1512 if (state
->ret
== 0) {
1518 int poll_one_fd(int fd
, int events
, int timeout
, int *revents
)
1524 fds
= talloc_zero_array(talloc_tos(), struct pollfd
, 2);
1530 fds
[0].events
= events
;
1532 ret
= sys_poll(fds
, 1, timeout
);
1535 * Assign whatever poll did, even in the ret<=0 case.
1537 *revents
= fds
[0].revents
;
1538 saved_errno
= errno
;
1540 errno
= saved_errno
;
1545 int poll_intr_one_fd(int fd
, int events
, int timeout
, int *revents
)
1551 pfd
.events
= events
;
1553 ret
= sys_poll_intr(&pfd
, 1, timeout
);
1558 *revents
= pfd
.revents
;