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"
30 #include "../lib/tsocket/tsocket.h"
32 const char *client_name(int fd
)
34 return get_peer_name(fd
,false);
37 const char *client_addr(int fd
, char *addr
, size_t addrlen
)
39 return get_peer_addr(fd
,addr
,addrlen
);
43 /* Not currently used. JRA. */
44 int client_socket_port(int fd
)
46 return get_socket_port(fd
);
50 /****************************************************************************
51 Determine if a file descriptor is in fact a socket.
52 ****************************************************************************/
54 bool is_a_socket(int fd
)
59 return(getsockopt(fd
, SOL_SOCKET
, SO_TYPE
, (char *)&v
, &l
) == 0);
62 /****************************************************************************
64 ****************************************************************************/
66 ssize_t
read_udp_v4_socket(int fd
,
69 struct sockaddr_storage
*psa
)
72 socklen_t socklen
= sizeof(*psa
);
73 struct sockaddr_in
*si
= (struct sockaddr_in
*)psa
;
75 memset((char *)psa
,'\0',socklen
);
77 ret
= (ssize_t
)sys_recvfrom(fd
,buf
,len
,0,
78 (struct sockaddr
*)psa
,&socklen
);
80 /* Don't print a low debug error for a non-blocking socket. */
81 if (errno
== EAGAIN
) {
82 DEBUG(10,("read_udp_v4_socket: returned EAGAIN\n"));
84 DEBUG(2,("read_udp_v4_socket: failed. errno=%s\n",
90 if (psa
->ss_family
!= AF_INET
) {
91 DEBUG(2,("read_udp_v4_socket: invalid address family %d "
92 "(not IPv4)\n", (int)psa
->ss_family
));
96 DEBUG(10,("read_udp_v4_socket: ip %s port %d read: %lu\n",
97 inet_ntoa(si
->sin_addr
),
104 /****************************************************************************
105 Read data from a file descriptor with a timout in msec.
106 mincount = if timeout, minimum to read before returning
107 maxcount = number to be read.
108 time_out = timeout in milliseconds
109 NB. This can be called with a non-socket fd, don't change
110 sys_read() to sys_recv() or other socket call.
111 ****************************************************************************/
113 NTSTATUS
read_fd_with_timeout(int fd
, char *buf
,
114 size_t mincnt
, size_t maxcnt
,
115 unsigned int time_out
,
122 /* just checking .... */
132 while (nread
< mincnt
) {
133 readret
= sys_read(fd
, buf
+ nread
, maxcnt
- nread
);
136 DEBUG(5,("read_fd_with_timeout: "
137 "blocking read. EOF from client.\n"));
138 return NT_STATUS_END_OF_FILE
;
142 return map_nt_error_from_unix(errno
);
149 /* Most difficult - timeout read */
150 /* If this is ever called on a disk file and
151 mincnt is greater then the filesize then
152 system performance will suffer severely as
153 select always returns true on disk files */
155 for (nread
=0; nread
< mincnt
; ) {
158 pollrtn
= poll_intr_one_fd(fd
, POLLIN
|POLLHUP
, time_out
,
163 return map_nt_error_from_unix(errno
);
166 /* Did we timeout ? */
167 if ((pollrtn
== 0) ||
168 ((revents
& (POLLIN
|POLLHUP
|POLLERR
)) == 0)) {
169 DEBUG(10,("read_fd_with_timeout: timeout read. "
170 "select timed out.\n"));
171 return NT_STATUS_IO_TIMEOUT
;
174 readret
= sys_read(fd
, buf
+nread
, maxcnt
-nread
);
177 /* we got EOF on the file descriptor */
178 DEBUG(5,("read_fd_with_timeout: timeout read. "
179 "EOF from client.\n"));
180 return NT_STATUS_END_OF_FILE
;
184 return map_nt_error_from_unix(errno
);
191 /* Return the number we got */
198 /****************************************************************************
199 Read data from an fd, reading exactly N bytes.
200 NB. This can be called with a non-socket fd, don't add dependencies
202 ****************************************************************************/
204 NTSTATUS
read_data(int fd
, char *buffer
, size_t N
)
206 return read_fd_with_timeout(fd
, buffer
, N
, N
, 0, NULL
);
209 /****************************************************************************
210 Write all data from an iov array
211 NB. This can be called with a non-socket fd, don't add dependencies
213 ****************************************************************************/
215 ssize_t
write_data_iov(int fd
, const struct iovec
*orig_iov
, int iovcnt
)
221 struct iovec
*iov_copy
, *iov
;
224 for (i
=0; i
<iovcnt
; i
++) {
225 to_send
+= orig_iov
[i
].iov_len
;
228 thistime
= sys_writev(fd
, orig_iov
, iovcnt
);
229 if ((thistime
<= 0) || (thistime
== to_send
)) {
235 * We could not send everything in one call. Make a copy of iov that
236 * we can mess with. We keep a copy of the array start in iov_copy for
237 * the TALLOC_FREE, because we're going to modify iov later on,
238 * discarding elements.
241 iov_copy
= (struct iovec
*)talloc_memdup(
242 talloc_tos(), orig_iov
, sizeof(struct iovec
) * iovcnt
);
244 if (iov_copy
== NULL
) {
250 while (sent
< to_send
) {
252 * We have to discard "thistime" bytes from the beginning
253 * iov array, "thistime" contains the number of bytes sent
256 while (thistime
> 0) {
257 if (thistime
< iov
[0].iov_len
) {
259 (char *)iov
[0].iov_base
+ thistime
;
260 iov
[0].iov_base
= (void *)new_base
;
261 iov
[0].iov_len
-= thistime
;
264 thistime
-= iov
[0].iov_len
;
269 thistime
= sys_writev(fd
, iov
, iovcnt
);
276 TALLOC_FREE(iov_copy
);
280 /****************************************************************************
282 NB. This can be called with a non-socket fd, don't add dependencies
284 ****************************************************************************/
286 ssize_t
write_data(int fd
, const char *buffer
, size_t N
)
290 iov
.iov_base
= discard_const_p(void, buffer
);
292 return write_data_iov(fd
, &iov
, 1);
295 /****************************************************************************
296 Send a keepalive packet (rfc1002).
297 ****************************************************************************/
299 bool send_keepalive(int client
)
301 unsigned char buf
[4];
303 buf
[0] = NBSSkeepalive
;
304 buf
[1] = buf
[2] = buf
[3] = 0;
306 return(write_data(client
,(char *)buf
,4) == 4);
309 /****************************************************************************
310 Read 4 bytes of a smb packet and return the smb length of the packet.
311 Store the result in the buffer.
312 This version of the function will return a length of zero on receiving
314 Timeout is in milliseconds.
315 ****************************************************************************/
317 NTSTATUS
read_smb_length_return_keepalive(int fd
, char *inbuf
,
318 unsigned int timeout
,
324 status
= read_fd_with_timeout(fd
, inbuf
, 4, 4, timeout
, NULL
);
326 if (!NT_STATUS_IS_OK(status
)) {
330 *len
= smb_len(inbuf
);
331 msg_type
= CVAL(inbuf
,0);
333 if (msg_type
== NBSSkeepalive
) {
334 DEBUG(5,("Got keepalive packet\n"));
337 DEBUG(10,("got smb length of %lu\n",(unsigned long)(*len
)));
342 /****************************************************************************
343 Read an smb from a fd.
344 The timeout is in milliseconds.
345 This function will return on receipt of a session keepalive packet.
346 maxlen is the max number of bytes to return, not including the 4 byte
347 length. If zero it means buflen limit.
348 Doesn't check the MAC on signed packets.
349 ****************************************************************************/
351 NTSTATUS
receive_smb_raw(int fd
, char *buffer
, size_t buflen
, unsigned int timeout
,
352 size_t maxlen
, size_t *p_len
)
357 status
= read_smb_length_return_keepalive(fd
,buffer
,timeout
,&len
);
359 if (!NT_STATUS_IS_OK(status
)) {
360 DEBUG(0, ("read_fd_with_timeout failed, read "
361 "error = %s.\n", nt_errstr(status
)));
366 DEBUG(0,("Invalid packet length! (%lu bytes).\n",
367 (unsigned long)len
));
368 return NT_STATUS_INVALID_PARAMETER
;
373 len
= MIN(len
,maxlen
);
376 status
= read_fd_with_timeout(
377 fd
, buffer
+4, len
, len
, timeout
, &len
);
379 if (!NT_STATUS_IS_OK(status
)) {
380 DEBUG(0, ("read_fd_with_timeout failed, read error = "
381 "%s.\n", nt_errstr(status
)));
385 /* not all of samba3 properly checks for packet-termination
386 * of strings. This ensures that we don't run off into
388 SSVAL(buffer
+4,len
, 0);
395 /****************************************************************************
396 Open a socket of the specified type, port, and address for incoming data.
397 ****************************************************************************/
399 int open_socket_in(int type
,
402 const struct sockaddr_storage
*psock
,
405 struct sockaddr_storage sock
;
407 socklen_t slen
= sizeof(struct sockaddr_in
);
411 #if defined(HAVE_IPV6)
412 if (sock
.ss_family
== AF_INET6
) {
413 ((struct sockaddr_in6
*)&sock
)->sin6_port
= htons(port
);
414 slen
= sizeof(struct sockaddr_in6
);
417 if (sock
.ss_family
== AF_INET
) {
418 ((struct sockaddr_in
*)&sock
)->sin_port
= htons(port
);
421 res
= socket(sock
.ss_family
, type
, 0 );
424 dbgtext( "open_socket_in(): socket() call failed: " );
425 dbgtext( "%s\n", strerror( errno
) );
430 /* This block sets/clears the SO_REUSEADDR and possibly SO_REUSEPORT. */
432 int val
= rebind
? 1 : 0;
433 if( setsockopt(res
,SOL_SOCKET
,SO_REUSEADDR
,
434 (char *)&val
,sizeof(val
)) == -1 ) {
435 if( DEBUGLVL( dlevel
) ) {
436 dbgtext( "open_socket_in(): setsockopt: " );
437 dbgtext( "SO_REUSEADDR = %s ",
438 val
?"true":"false" );
439 dbgtext( "on port %d failed ", port
);
440 dbgtext( "with error = %s\n", strerror(errno
) );
444 if( setsockopt(res
,SOL_SOCKET
,SO_REUSEPORT
,
445 (char *)&val
,sizeof(val
)) == -1 ) {
446 if( DEBUGLVL( dlevel
) ) {
447 dbgtext( "open_socket_in(): setsockopt: ");
448 dbgtext( "SO_REUSEPORT = %s ",
450 dbgtext( "on port %d failed ", port
);
451 dbgtext( "with error = %s\n", strerror(errno
));
454 #endif /* SO_REUSEPORT */
459 * As IPV6_V6ONLY is the default on some systems,
460 * we better try to be consistent and always use it.
462 * This also avoids using IPv4 via AF_INET6 sockets
463 * and makes sure %I never resolves to a '::ffff:192.168.0.1'
466 if (sock
.ss_family
== AF_INET6
) {
470 ret
= setsockopt(res
, IPPROTO_IPV6
, IPV6_V6ONLY
,
471 (const void *)&val
, sizeof(val
));
474 dbgtext("open_socket_in(): IPV6_ONLY failed: ");
475 dbgtext("%s\n", strerror(errno
));
483 /* now we've got a socket - we need to bind it */
484 if (bind(res
, (struct sockaddr
*)&sock
, slen
) == -1 ) {
485 if( DEBUGLVL(dlevel
) && (port
== SMB_PORT1
||
486 port
== SMB_PORT2
|| port
== NMB_PORT
) ) {
487 char addr
[INET6_ADDRSTRLEN
];
488 print_sockaddr(addr
, sizeof(addr
),
490 dbgtext( "bind failed on port %d ", port
);
491 dbgtext( "socket_addr = %s.\n", addr
);
492 dbgtext( "Error = %s\n", strerror(errno
));
498 DEBUG( 10, ( "bind succeeded on port %d\n", port
) );
502 struct open_socket_out_state
{
504 struct event_context
*ev
;
505 struct sockaddr_storage ss
;
511 static void open_socket_out_connected(struct tevent_req
*subreq
);
513 static int open_socket_out_state_destructor(struct open_socket_out_state
*s
)
521 /****************************************************************************
522 Create an outgoing socket. timeout is in milliseconds.
523 **************************************************************************/
525 struct tevent_req
*open_socket_out_send(TALLOC_CTX
*mem_ctx
,
526 struct event_context
*ev
,
527 const struct sockaddr_storage
*pss
,
531 char addr
[INET6_ADDRSTRLEN
];
532 struct tevent_req
*result
, *subreq
;
533 struct open_socket_out_state
*state
;
536 result
= tevent_req_create(mem_ctx
, &state
,
537 struct open_socket_out_state
);
538 if (result
== NULL
) {
544 state
->wait_usec
= 10000;
547 state
->fd
= socket(state
->ss
.ss_family
, SOCK_STREAM
, 0);
548 if (state
->fd
== -1) {
549 status
= map_nt_error_from_unix(errno
);
552 talloc_set_destructor(state
, open_socket_out_state_destructor
);
554 if (!tevent_req_set_endtime(
555 result
, ev
, timeval_current_ofs_msec(timeout
))) {
559 #if defined(HAVE_IPV6)
560 if (pss
->ss_family
== AF_INET6
) {
561 struct sockaddr_in6
*psa6
;
562 psa6
= (struct sockaddr_in6
*)&state
->ss
;
563 psa6
->sin6_port
= htons(port
);
564 if (psa6
->sin6_scope_id
== 0
565 && IN6_IS_ADDR_LINKLOCAL(&psa6
->sin6_addr
)) {
566 setup_linklocal_scope_id(
567 (struct sockaddr
*)&(state
->ss
));
569 state
->salen
= sizeof(struct sockaddr_in6
);
572 if (pss
->ss_family
== AF_INET
) {
573 struct sockaddr_in
*psa
;
574 psa
= (struct sockaddr_in
*)&state
->ss
;
575 psa
->sin_port
= htons(port
);
576 state
->salen
= sizeof(struct sockaddr_in
);
579 if (pss
->ss_family
== AF_UNIX
) {
580 state
->salen
= sizeof(struct sockaddr_un
);
583 print_sockaddr(addr
, sizeof(addr
), &state
->ss
);
584 DEBUG(3,("Connecting to %s at port %u\n", addr
, (unsigned int)port
));
586 subreq
= async_connect_send(state
, state
->ev
, state
->fd
,
587 (struct sockaddr
*)&state
->ss
,
590 || !tevent_req_set_endtime(
592 timeval_current_ofs(0, state
->wait_usec
))) {
595 tevent_req_set_callback(subreq
, open_socket_out_connected
, result
);
599 tevent_req_nterror(result
, status
);
600 return tevent_req_post(result
, ev
);
606 static void open_socket_out_connected(struct tevent_req
*subreq
)
608 struct tevent_req
*req
=
609 tevent_req_callback_data(subreq
, struct tevent_req
);
610 struct open_socket_out_state
*state
=
611 tevent_req_data(req
, struct open_socket_out_state
);
615 ret
= async_connect_recv(subreq
, &sys_errno
);
618 tevent_req_done(req
);
624 (sys_errno
== ETIMEDOUT
) ||
626 (sys_errno
== EINPROGRESS
) ||
627 (sys_errno
== EALREADY
) ||
628 (sys_errno
== EAGAIN
)) {
634 if (state
->wait_usec
< 250000) {
635 state
->wait_usec
*= 1.5;
638 subreq
= async_connect_send(state
, state
->ev
, state
->fd
,
639 (struct sockaddr
*)&state
->ss
,
641 if (tevent_req_nomem(subreq
, req
)) {
644 if (!tevent_req_set_endtime(
646 timeval_current_ofs_usec(state
->wait_usec
))) {
647 tevent_req_nterror(req
, NT_STATUS_NO_MEMORY
);
650 tevent_req_set_callback(subreq
, open_socket_out_connected
, req
);
655 if (sys_errno
== EISCONN
) {
656 tevent_req_done(req
);
662 tevent_req_nterror(req
, map_nt_error_from_unix(sys_errno
));
665 NTSTATUS
open_socket_out_recv(struct tevent_req
*req
, int *pfd
)
667 struct open_socket_out_state
*state
=
668 tevent_req_data(req
, struct open_socket_out_state
);
671 if (tevent_req_is_nterror(req
, &status
)) {
680 * @brief open a socket
682 * @param pss a struct sockaddr_storage defining the address to connect to
683 * @param port to connect to
684 * @param timeout in MILLISECONDS
685 * @param pfd file descriptor returned
687 * @return NTSTATUS code
689 NTSTATUS
open_socket_out(const struct sockaddr_storage
*pss
, uint16_t port
,
690 int timeout
, int *pfd
)
692 TALLOC_CTX
*frame
= talloc_stackframe();
693 struct event_context
*ev
;
694 struct tevent_req
*req
;
695 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
697 ev
= event_context_init(frame
);
702 req
= open_socket_out_send(frame
, ev
, pss
, port
, timeout
);
706 if (!tevent_req_poll(req
, ev
)) {
707 status
= NT_STATUS_INTERNAL_ERROR
;
710 status
= open_socket_out_recv(req
, pfd
);
716 struct open_socket_out_defer_state
{
717 struct event_context
*ev
;
718 struct sockaddr_storage ss
;
724 static void open_socket_out_defer_waited(struct tevent_req
*subreq
);
725 static void open_socket_out_defer_connected(struct tevent_req
*subreq
);
727 struct tevent_req
*open_socket_out_defer_send(TALLOC_CTX
*mem_ctx
,
728 struct event_context
*ev
,
729 struct timeval wait_time
,
730 const struct sockaddr_storage
*pss
,
734 struct tevent_req
*req
, *subreq
;
735 struct open_socket_out_defer_state
*state
;
737 req
= tevent_req_create(mem_ctx
, &state
,
738 struct open_socket_out_defer_state
);
745 state
->timeout
= timeout
;
747 subreq
= tevent_wakeup_send(
749 timeval_current_ofs(wait_time
.tv_sec
, wait_time
.tv_usec
));
750 if (subreq
== NULL
) {
753 tevent_req_set_callback(subreq
, open_socket_out_defer_waited
, req
);
760 static void open_socket_out_defer_waited(struct tevent_req
*subreq
)
762 struct tevent_req
*req
= tevent_req_callback_data(
763 subreq
, struct tevent_req
);
764 struct open_socket_out_defer_state
*state
= tevent_req_data(
765 req
, struct open_socket_out_defer_state
);
768 ret
= tevent_wakeup_recv(subreq
);
771 tevent_req_nterror(req
, NT_STATUS_INTERNAL_ERROR
);
775 subreq
= open_socket_out_send(state
, state
->ev
, &state
->ss
,
776 state
->port
, state
->timeout
);
777 if (tevent_req_nomem(subreq
, req
)) {
780 tevent_req_set_callback(subreq
, open_socket_out_defer_connected
, req
);
783 static void open_socket_out_defer_connected(struct tevent_req
*subreq
)
785 struct tevent_req
*req
= tevent_req_callback_data(
786 subreq
, struct tevent_req
);
787 struct open_socket_out_defer_state
*state
= tevent_req_data(
788 req
, struct open_socket_out_defer_state
);
791 status
= open_socket_out_recv(subreq
, &state
->fd
);
793 if (!NT_STATUS_IS_OK(status
)) {
794 tevent_req_nterror(req
, status
);
797 tevent_req_done(req
);
800 NTSTATUS
open_socket_out_defer_recv(struct tevent_req
*req
, int *pfd
)
802 struct open_socket_out_defer_state
*state
= tevent_req_data(
803 req
, struct open_socket_out_defer_state
);
806 if (tevent_req_is_nterror(req
, &status
)) {
814 /****************************************************************************
815 Open a connected UDP socket to host on port
816 **************************************************************************/
818 int open_udp_socket(const char *host
, int port
)
820 struct sockaddr_storage ss
;
823 if (!interpret_string_addr(&ss
, host
, 0)) {
824 DEBUG(10,("open_udp_socket: can't resolve name %s\n",
829 res
= socket(ss
.ss_family
, SOCK_DGRAM
, 0);
834 #if defined(HAVE_IPV6)
835 if (ss
.ss_family
== AF_INET6
) {
836 struct sockaddr_in6
*psa6
;
837 psa6
= (struct sockaddr_in6
*)&ss
;
838 psa6
->sin6_port
= htons(port
);
839 if (psa6
->sin6_scope_id
== 0
840 && IN6_IS_ADDR_LINKLOCAL(&psa6
->sin6_addr
)) {
841 setup_linklocal_scope_id(
842 (struct sockaddr
*)&ss
);
846 if (ss
.ss_family
== AF_INET
) {
847 struct sockaddr_in
*psa
;
848 psa
= (struct sockaddr_in
*)&ss
;
849 psa
->sin_port
= htons(port
);
852 if (sys_connect(res
,(struct sockaddr
*)&ss
)) {
860 /*******************************************************************
861 Return the IP addr of the remote end of a socket as a string.
862 Optionally return the struct sockaddr_storage.
863 ******************************************************************/
865 static const char *get_peer_addr_internal(int fd
,
868 struct sockaddr
*pss
,
871 struct sockaddr_storage ss
;
872 socklen_t length
= sizeof(ss
);
874 strlcpy(addr_buf
,"0.0.0.0",addr_buf_len
);
881 pss
= (struct sockaddr
*)&ss
;
885 if (getpeername(fd
, (struct sockaddr
*)pss
, plength
) < 0) {
886 int level
= (errno
== ENOTCONN
) ? 2 : 0;
887 DEBUG(level
, ("getpeername failed. Error was %s\n",
892 print_sockaddr_len(addr_buf
,
899 /*******************************************************************
900 Matchname - determine if host name matches IP address. Used to
901 confirm a hostname lookup to prevent spoof attacks.
902 ******************************************************************/
904 static bool matchname(const char *remotehost
,
905 const struct sockaddr
*pss
,
908 struct addrinfo
*res
= NULL
;
909 struct addrinfo
*ailist
= NULL
;
910 char addr_buf
[INET6_ADDRSTRLEN
];
911 bool ret
= interpret_string_addr_internal(&ailist
,
913 AI_ADDRCONFIG
|AI_CANONNAME
);
915 if (!ret
|| ailist
== NULL
) {
916 DEBUG(3,("matchname: getaddrinfo failed for "
919 gai_strerror(ret
) ));
924 * Make sure that getaddrinfo() returns the "correct" host name.
927 if (ailist
->ai_canonname
== NULL
||
928 (!strequal(remotehost
, ailist
->ai_canonname
) &&
929 !strequal(remotehost
, "localhost"))) {
930 DEBUG(0,("matchname: host name/name mismatch: %s != %s\n",
932 ailist
->ai_canonname
?
933 ailist
->ai_canonname
: "(NULL)"));
934 freeaddrinfo(ailist
);
938 /* Look up the host address in the address list we just got. */
939 for (res
= ailist
; res
; res
= res
->ai_next
) {
943 if (sockaddr_equal((const struct sockaddr
*)res
->ai_addr
,
944 (const struct sockaddr
*)pss
)) {
945 freeaddrinfo(ailist
);
951 * The host name does not map to the original host address. Perhaps
952 * someone has compromised a name server. More likely someone botched
953 * it, but that could be dangerous, too.
956 DEBUG(0,("matchname: host name/address mismatch: %s != %s\n",
957 print_sockaddr_len(addr_buf
,
961 ailist
->ai_canonname
? ailist
->ai_canonname
: "(NULL)"));
964 freeaddrinfo(ailist
);
969 /*******************************************************************
970 Deal with the singleton cache.
971 ******************************************************************/
973 struct name_addr_pair
{
974 struct sockaddr_storage ss
;
978 /*******************************************************************
979 Lookup a name/addr pair. Returns memory allocated from memcache.
980 ******************************************************************/
982 static bool lookup_nc(struct name_addr_pair
*nc
)
988 if (!memcache_lookup(
989 NULL
, SINGLETON_CACHE
,
990 data_blob_string_const_null("get_peer_name"),
995 memcpy(&nc
->ss
, tmp
.data
, sizeof(nc
->ss
));
996 nc
->name
= (const char *)tmp
.data
+ sizeof(nc
->ss
);
1000 /*******************************************************************
1001 Save a name/addr pair.
1002 ******************************************************************/
1004 static void store_nc(const struct name_addr_pair
*nc
)
1007 size_t namelen
= strlen(nc
->name
);
1009 tmp
= data_blob(NULL
, sizeof(nc
->ss
) + namelen
+ 1);
1013 memcpy(tmp
.data
, &nc
->ss
, sizeof(nc
->ss
));
1014 memcpy(tmp
.data
+sizeof(nc
->ss
), nc
->name
, namelen
+1);
1016 memcache_add(NULL
, SINGLETON_CACHE
,
1017 data_blob_string_const_null("get_peer_name"),
1019 data_blob_free(&tmp
);
1022 /*******************************************************************
1023 Return the DNS name of the remote end of a socket.
1024 ******************************************************************/
1026 const char *get_peer_name(int fd
, bool force_lookup
)
1028 struct name_addr_pair nc
;
1029 char addr_buf
[INET6_ADDRSTRLEN
];
1030 struct sockaddr_storage ss
;
1031 socklen_t length
= sizeof(ss
);
1034 char name_buf
[MAX_DNS_NAME_LENGTH
];
1035 char tmp_name
[MAX_DNS_NAME_LENGTH
];
1037 /* reverse lookups can be *very* expensive, and in many
1038 situations won't work because many networks don't link dhcp
1039 with dns. To avoid the delay we avoid the lookup if
1041 if (!lp_hostname_lookups() && (force_lookup
== false)) {
1042 length
= sizeof(nc
.ss
);
1043 nc
.name
= get_peer_addr_internal(fd
, addr_buf
, sizeof(addr_buf
),
1044 (struct sockaddr
*)&nc
.ss
, &length
);
1047 return nc
.name
? nc
.name
: "UNKNOWN";
1052 memset(&ss
, '\0', sizeof(ss
));
1053 p
= get_peer_addr_internal(fd
, addr_buf
, sizeof(addr_buf
), (struct sockaddr
*)&ss
, &length
);
1055 /* it might be the same as the last one - save some DNS work */
1056 if (sockaddr_equal((struct sockaddr
*)&ss
, (struct sockaddr
*)&nc
.ss
)) {
1057 return nc
.name
? nc
.name
: "UNKNOWN";
1060 /* Not the same. We need to lookup. */
1065 /* Look up the remote host name. */
1066 ret
= sys_getnameinfo((struct sockaddr
*)&ss
,
1075 DEBUG(1,("get_peer_name: getnameinfo failed "
1076 "for %s with error %s\n",
1078 gai_strerror(ret
)));
1079 strlcpy(name_buf
, p
, sizeof(name_buf
));
1081 if (!matchname(name_buf
, (struct sockaddr
*)&ss
, length
)) {
1082 DEBUG(0,("Matchname failed on %s %s\n",name_buf
,p
));
1083 strlcpy(name_buf
,"UNKNOWN",sizeof(name_buf
));
1087 strlcpy(tmp_name
, name_buf
, sizeof(tmp_name
));
1088 alpha_strcpy(name_buf
, tmp_name
, "_-.", sizeof(name_buf
));
1089 if (strstr(name_buf
,"..")) {
1090 strlcpy(name_buf
, "UNKNOWN", sizeof(name_buf
));
1098 return nc
.name
? nc
.name
: "UNKNOWN";
1101 /*******************************************************************
1102 Return the IP addr of the remote end of a socket as a string.
1103 ******************************************************************/
1105 const char *get_peer_addr(int fd
, char *addr
, size_t addr_len
)
1107 return get_peer_addr_internal(fd
, addr
, addr_len
, NULL
, NULL
);
1110 int get_remote_hostname(const struct tsocket_address
*remote_address
,
1112 TALLOC_CTX
*mem_ctx
)
1114 char name_buf
[MAX_DNS_NAME_LENGTH
];
1115 char tmp_name
[MAX_DNS_NAME_LENGTH
];
1116 struct name_addr_pair nc
;
1117 struct sockaddr_storage ss
;
1121 if (!lp_hostname_lookups()) {
1122 nc
.name
= tsocket_address_inet_addr_string(remote_address
,
1124 if (nc
.name
== NULL
) {
1128 len
= tsocket_address_bsd_sockaddr(remote_address
,
1129 (struct sockaddr
*) &nc
.ss
,
1130 sizeof(struct sockaddr_storage
));
1138 if (nc
.name
== NULL
) {
1139 *name
= talloc_strdup(mem_ctx
, "UNKNOWN");
1141 *name
= talloc_strdup(mem_ctx
, nc
.name
);
1150 len
= tsocket_address_bsd_sockaddr(remote_address
,
1151 (struct sockaddr
*) &ss
,
1152 sizeof(struct sockaddr_storage
));
1157 /* it might be the same as the last one - save some DNS work */
1158 if (sockaddr_equal((struct sockaddr
*)&ss
, (struct sockaddr
*)&nc
.ss
)) {
1159 if (nc
.name
== NULL
) {
1160 *name
= talloc_strdup(mem_ctx
, "UNKNOWN");
1162 *name
= talloc_strdup(mem_ctx
, nc
.name
);
1167 /* Look up the remote host name. */
1168 rc
= sys_getnameinfo((struct sockaddr
*) &ss
,
1178 p
= tsocket_address_inet_addr_string(remote_address
, mem_ctx
);
1183 DEBUG(1,("getnameinfo failed for %s with error %s\n",
1186 strlcpy(name_buf
, p
, sizeof(name_buf
));
1190 if (!matchname(name_buf
, (struct sockaddr
*)&ss
, len
)) {
1191 DEBUG(0,("matchname failed on %s\n", name_buf
));
1192 strlcpy(name_buf
, "UNKNOWN", sizeof(name_buf
));
1196 strlcpy(tmp_name
, name_buf
, sizeof(tmp_name
));
1197 alpha_strcpy(name_buf
, tmp_name
, "_-.", sizeof(name_buf
));
1198 if (strstr(name_buf
,"..")) {
1199 strlcpy(name_buf
, "UNKNOWN", sizeof(name_buf
));
1208 if (nc
.name
== NULL
) {
1209 *name
= talloc_strdup(mem_ctx
, "UNKOWN");
1211 *name
= talloc_strdup(mem_ctx
, nc
.name
);
1217 /*******************************************************************
1218 Create protected unix domain socket.
1220 Some unixes cannot set permissions on a ux-dom-sock, so we
1221 have to make sure that the directory contains the protection
1222 permissions instead.
1223 ******************************************************************/
1225 int create_pipe_sock(const char *socket_dir
,
1226 const char *socket_name
,
1229 #ifdef HAVE_UNIXSOCKET
1230 struct sockaddr_un sunaddr
;
1236 old_umask
= umask(0);
1238 /* Create the socket directory or reuse the existing one */
1240 if (lstat(socket_dir
, &st
) == -1) {
1241 if (errno
== ENOENT
) {
1242 /* Create directory */
1243 if (mkdir(socket_dir
, dir_perms
) == -1) {
1244 DEBUG(0, ("error creating socket directory "
1245 "%s: %s\n", socket_dir
,
1250 DEBUG(0, ("lstat failed on socket directory %s: %s\n",
1251 socket_dir
, strerror(errno
)));
1255 /* Check ownership and permission on existing directory */
1256 if (!S_ISDIR(st
.st_mode
)) {
1257 DEBUG(0, ("socket directory '%s' isn't a directory\n",
1261 if (st
.st_uid
!= sec_initial_uid()) {
1262 DEBUG(0, ("invalid ownership on directory "
1263 "'%s'\n", socket_dir
));
1267 if ((st
.st_mode
& 0777) != dir_perms
) {
1268 DEBUG(0, ("invalid permissions on directory "
1269 "'%s': has 0%o should be 0%o\n", socket_dir
,
1270 (st
.st_mode
& 0777), dir_perms
));
1276 /* Create the socket file */
1278 sock
= socket(AF_UNIX
, SOCK_STREAM
, 0);
1281 DEBUG(0, ("create_pipe_sock: socket error %s\n",
1286 if (asprintf(&path
, "%s/%s", socket_dir
, socket_name
) == -1) {
1291 memset(&sunaddr
, 0, sizeof(sunaddr
));
1292 sunaddr
.sun_family
= AF_UNIX
;
1293 strlcpy(sunaddr
.sun_path
, path
, sizeof(sunaddr
.sun_path
));
1295 if (bind(sock
, (struct sockaddr
*)&sunaddr
, sizeof(sunaddr
)) == -1) {
1296 DEBUG(0, ("bind failed on pipe socket %s: %s\n", path
,
1316 DEBUG(0, ("create_pipe_sock: No Unix sockets on this system\n"));
1318 #endif /* HAVE_UNIXSOCKET */
1321 /****************************************************************************
1322 Get my own canonical name, including domain.
1323 ****************************************************************************/
1325 const char *get_mydnsfullname(void)
1327 struct addrinfo
*res
= NULL
;
1328 char my_hostname
[HOST_NAME_MAX
];
1332 if (memcache_lookup(NULL
, SINGLETON_CACHE
,
1333 data_blob_string_const_null("get_mydnsfullname"),
1335 SMB_ASSERT(tmp
.length
> 0);
1336 return (const char *)tmp
.data
;
1339 /* get my host name */
1340 if (gethostname(my_hostname
, sizeof(my_hostname
)) == -1) {
1341 DEBUG(0,("get_mydnsfullname: gethostname failed\n"));
1345 /* Ensure null termination. */
1346 my_hostname
[sizeof(my_hostname
)-1] = '\0';
1348 ret
= interpret_string_addr_internal(&res
,
1350 AI_ADDRCONFIG
|AI_CANONNAME
);
1352 if (!ret
|| res
== NULL
) {
1353 DEBUG(3,("get_mydnsfullname: getaddrinfo failed for "
1356 gai_strerror(ret
) ));
1361 * Make sure that getaddrinfo() returns the "correct" host name.
1364 if (res
->ai_canonname
== NULL
) {
1365 DEBUG(3,("get_mydnsfullname: failed to get "
1366 "canonical name for %s\n",
1372 /* This copies the data, so we must do a lookup
1373 * afterwards to find the value to return.
1376 memcache_add(NULL
, SINGLETON_CACHE
,
1377 data_blob_string_const_null("get_mydnsfullname"),
1378 data_blob_string_const_null(res
->ai_canonname
));
1380 if (!memcache_lookup(NULL
, SINGLETON_CACHE
,
1381 data_blob_string_const_null("get_mydnsfullname"),
1383 tmp
= data_blob_talloc(talloc_tos(), res
->ai_canonname
,
1384 strlen(res
->ai_canonname
) + 1);
1389 return (const char *)tmp
.data
;
1392 /************************************************************
1393 Is this my ip address ?
1394 ************************************************************/
1396 static bool is_my_ipaddr(const char *ipaddr_str
)
1398 struct sockaddr_storage ss
;
1399 struct iface_struct
*nics
;
1402 if (!interpret_string_addr(&ss
, ipaddr_str
, AI_NUMERICHOST
)) {
1406 if (is_zero_addr(&ss
)) {
1410 if (ismyaddr((struct sockaddr
*)&ss
) ||
1411 is_loopback_addr((struct sockaddr
*)&ss
)) {
1415 n
= get_interfaces(talloc_tos(), &nics
);
1416 for (i
=0; i
<n
; i
++) {
1417 if (sockaddr_equal((struct sockaddr
*)&nics
[i
].ip
, (struct sockaddr
*)&ss
)) {
1426 /************************************************************
1428 ************************************************************/
1430 bool is_myname_or_ipaddr(const char *s
)
1432 TALLOC_CTX
*ctx
= talloc_tos();
1434 const char *dnsname
;
1435 char *servername
= NULL
;
1441 /* Santize the string from '\\name' */
1442 name
= talloc_strdup(ctx
, s
);
1447 servername
= strrchr_m(name
, '\\' );
1454 /* Optimize for the common case */
1455 if (strequal(servername
, lp_netbios_name())) {
1459 /* Check for an alias */
1460 if (is_myname(servername
)) {
1464 /* Check for loopback */
1465 if (strequal(servername
, "127.0.0.1") ||
1466 strequal(servername
, "::1")) {
1470 if (strequal(servername
, "localhost")) {
1474 /* Maybe it's my dns name */
1475 dnsname
= get_mydnsfullname();
1476 if (dnsname
&& strequal(servername
, dnsname
)) {
1480 /* Maybe its an IP address? */
1481 if (is_ipaddress(servername
)) {
1482 return is_my_ipaddr(servername
);
1485 /* Handle possible CNAME records - convert to an IP addr. list. */
1487 /* Use DNS to resolve the name, check all addresses. */
1488 struct addrinfo
*p
= NULL
;
1489 struct addrinfo
*res
= NULL
;
1491 if (!interpret_string_addr_internal(&res
,
1497 for (p
= res
; p
; p
= p
->ai_next
) {
1498 char addr
[INET6_ADDRSTRLEN
];
1499 struct sockaddr_storage ss
;
1502 memcpy(&ss
, p
->ai_addr
, p
->ai_addrlen
);
1503 print_sockaddr(addr
,
1506 if (is_my_ipaddr(addr
)) {
1518 struct getaddrinfo_state
{
1520 const char *service
;
1521 const struct addrinfo
*hints
;
1522 struct addrinfo
*res
;
1526 static void getaddrinfo_do(void *private_data
);
1527 static void getaddrinfo_done(struct tevent_req
*subreq
);
1529 struct tevent_req
*getaddrinfo_send(TALLOC_CTX
*mem_ctx
,
1530 struct tevent_context
*ev
,
1531 struct fncall_context
*ctx
,
1533 const char *service
,
1534 const struct addrinfo
*hints
)
1536 struct tevent_req
*req
, *subreq
;
1537 struct getaddrinfo_state
*state
;
1539 req
= tevent_req_create(mem_ctx
, &state
, struct getaddrinfo_state
);
1545 state
->service
= service
;
1546 state
->hints
= hints
;
1548 subreq
= fncall_send(state
, ev
, ctx
, getaddrinfo_do
, state
);
1549 if (tevent_req_nomem(subreq
, req
)) {
1550 return tevent_req_post(req
, ev
);
1552 tevent_req_set_callback(subreq
, getaddrinfo_done
, req
);
1556 static void getaddrinfo_do(void *private_data
)
1558 struct getaddrinfo_state
*state
=
1559 (struct getaddrinfo_state
*)private_data
;
1561 state
->ret
= getaddrinfo(state
->node
, state
->service
, state
->hints
,
1565 static void getaddrinfo_done(struct tevent_req
*subreq
)
1567 struct tevent_req
*req
= tevent_req_callback_data(
1568 subreq
, struct tevent_req
);
1571 ret
= fncall_recv(subreq
, &err
);
1572 TALLOC_FREE(subreq
);
1574 tevent_req_error(req
, err
);
1577 tevent_req_done(req
);
1580 int getaddrinfo_recv(struct tevent_req
*req
, struct addrinfo
**res
)
1582 struct getaddrinfo_state
*state
= tevent_req_data(
1583 req
, struct getaddrinfo_state
);
1586 if (tevent_req_is_unix_error(req
, &err
)) {
1594 if (state
->ret
== 0) {
1600 int poll_one_fd(int fd
, int events
, int timeout
, int *revents
)
1606 fds
= talloc_zero_array(talloc_tos(), struct pollfd
, 2);
1612 fds
[0].events
= events
;
1614 ret
= sys_poll(fds
, 1, timeout
);
1617 * Assign whatever poll did, even in the ret<=0 case.
1619 *revents
= fds
[0].revents
;
1620 saved_errno
= errno
;
1622 errno
= saved_errno
;
1627 int poll_intr_one_fd(int fd
, int events
, int timeout
, int *revents
)
1633 pfd
.events
= events
;
1635 ret
= sys_poll_intr(&pfd
, 1, timeout
);
1640 *revents
= pfd
.revents
;