2 Unix SMB/CIFS implementation.
3 Samba utility functions
4 Copyright (C) Andrew Tridgell 1992-1998
5 Copyright (C) Tim Potter 2000-2001
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include <openssl/ssl.h>
26 #undef Realloc /* SSLeay defines this and samba has a function of this name */
31 /* the last IP received from */
32 struct in_addr lastip;
34 /* the last port received from */
37 int smb_read_error = 0;
39 /****************************************************************************
40 Determine if a file descriptor is in fact a socket.
41 ****************************************************************************/
43 BOOL is_a_socket(int fd)
47 return(getsockopt(fd, SOL_SOCKET, SO_TYPE, (char *)&v, &l) == 0);
50 enum SOCK_OPT_TYPES {OPT_BOOL,OPT_INT,OPT_ON};
52 typedef struct smb_socket_option
61 smb_socket_option socket_options[] = {
62 {"SO_KEEPALIVE", SOL_SOCKET, SO_KEEPALIVE, 0, OPT_BOOL},
63 {"SO_REUSEADDR", SOL_SOCKET, SO_REUSEADDR, 0, OPT_BOOL},
64 {"SO_BROADCAST", SOL_SOCKET, SO_BROADCAST, 0, OPT_BOOL},
66 {"TCP_NODELAY", IPPROTO_TCP, TCP_NODELAY, 0, OPT_BOOL},
69 {"IPTOS_LOWDELAY", IPPROTO_IP, IP_TOS, IPTOS_LOWDELAY, OPT_ON},
71 #ifdef IPTOS_THROUGHPUT
72 {"IPTOS_THROUGHPUT", IPPROTO_IP, IP_TOS, IPTOS_THROUGHPUT, OPT_ON},
75 {"SO_REUSEPORT", SOL_SOCKET, SO_REUSEPORT, 0, OPT_BOOL},
78 {"SO_SNDBUF", SOL_SOCKET, SO_SNDBUF, 0, OPT_INT},
81 {"SO_RCVBUF", SOL_SOCKET, SO_RCVBUF, 0, OPT_INT},
84 {"SO_SNDLOWAT", SOL_SOCKET, SO_SNDLOWAT, 0, OPT_INT},
87 {"SO_RCVLOWAT", SOL_SOCKET, SO_RCVLOWAT, 0, OPT_INT},
90 {"SO_SNDTIMEO", SOL_SOCKET, SO_SNDTIMEO, 0, OPT_INT},
93 {"SO_RCVTIMEO", SOL_SOCKET, SO_RCVTIMEO, 0, OPT_INT},
97 /****************************************************************************
99 ****************************************************************************/
100 static void print_socket_options(int s)
103 smb_socket_option *p = &socket_options[0];
105 for (; p->name != NULL; p++) {
106 if (getsockopt(s, p->level, p->option, (void *)&value, &vlen) == -1) {
107 DEBUG(5,("Could not test socket option %s.\n", p->name));
109 DEBUG(5,("socket option %s = %d\n",p->name,value));
114 /****************************************************************************
115 Set user socket options.
116 ****************************************************************************/
118 void set_socket_options(int fd, char *options)
122 while (next_token(&options,tok," \t,", sizeof(tok))) {
126 BOOL got_value = False;
128 if ((p = strchr_m(tok,'='))) {
134 for (i=0;socket_options[i].name;i++)
135 if (strequal(socket_options[i].name,tok))
138 if (!socket_options[i].name) {
139 DEBUG(0,("Unknown socket option %s\n",tok));
143 switch (socket_options[i].opttype) {
146 ret = setsockopt(fd,socket_options[i].level,
147 socket_options[i].option,(char *)&value,sizeof(int));
152 DEBUG(0,("syntax error - %s does not take a value\n",tok));
155 int on = socket_options[i].value;
156 ret = setsockopt(fd,socket_options[i].level,
157 socket_options[i].option,(char *)&on,sizeof(int));
163 DEBUG(0,("Failed to set socket option %s (Error %s)\n",tok, strerror(errno) ));
166 print_socket_options(fd);
169 /****************************************************************************
171 ****************************************************************************/
173 ssize_t read_udp_socket(int fd,char *buf,size_t len)
176 struct sockaddr_in sock;
177 socklen_t socklen = sizeof(sock);
179 memset((char *)&sock,'\0',socklen);
180 memset((char *)&lastip,'\0',sizeof(lastip));
181 ret = (ssize_t)recvfrom(fd,buf,len,0,(struct sockaddr *)&sock,&socklen);
183 DEBUG(2,("read socket failed. ERRNO=%s\n",strerror(errno)));
187 lastip = sock.sin_addr;
188 lastport = ntohs(sock.sin_port);
190 DEBUG(10,("read_udp_socket: lastip %s lastport %d read: %d\n",
191 inet_ntoa(lastip), lastport, ret));
196 /*******************************************************************
197 checks if read data is outstanding.
198 ********************************************************************/
199 int read_data_outstanding(int fd, unsigned int time_out)
203 struct timeval timeout;
208 timeout.tv_sec = (time_t) (time_out / 1000);
209 timeout.tv_usec = (long)(1000 * (time_out % 1000));
211 selrtn = sys_select_intr(fd + 1, &fds, NULL, NULL, &timeout);
217 return FD_ISSET(fd, &fds) ? 1 : 0;
220 /****************************************************************************
221 Read data from a socket with a timout in msec.
222 mincount = if timeout, minimum to read before returning
223 maxcount = number to be read.
224 time_out = timeout in milliseconds
225 ****************************************************************************/
227 static ssize_t read_socket_with_timeout(int fd,char *buf,size_t mincnt,size_t maxcnt,unsigned int time_out)
233 struct timeval timeout;
235 /* just checking .... */
243 if (mincnt == 0) mincnt = maxcnt;
245 while (nread < mincnt) {
248 readret = SSL_read(ssl, buf + nread, maxcnt - nread);
250 readret = read(fd, buf + nread, maxcnt - nread);
253 readret = read(fd, buf + nread, maxcnt - nread);
254 #endif /* WITH_SSL */
257 DEBUG(5,("read_socket_with_timeout: blocking read. EOF from client.\n"));
258 smb_read_error = READ_EOF;
263 DEBUG(0,("read_socket_with_timeout: read error = %s.\n", strerror(errno) ));
264 smb_read_error = READ_ERROR;
269 return((ssize_t)nread);
272 /* Most difficult - timeout read */
273 /* If this is ever called on a disk file and
274 mincnt is greater then the filesize then
275 system performance will suffer severely as
276 select always returns true on disk files */
278 /* Set initial timeout */
279 timeout.tv_sec = (time_t)(time_out / 1000);
280 timeout.tv_usec = (long)(1000 * (time_out % 1000));
282 for (nread=0; nread < mincnt; ) {
286 selrtn = sys_select_intr(fd+1,&fds,NULL,NULL,&timeout);
290 /* something is wrong. Maybe the socket is dead? */
291 DEBUG(0,("read_socket_with_timeout: timeout read. select error = %s.\n", strerror(errno) ));
292 smb_read_error = READ_ERROR;
296 /* Did we timeout ? */
298 DEBUG(10,("read_socket_with_timeout: timeout read. select timed out.\n"));
299 smb_read_error = READ_TIMEOUT;
305 readret = SSL_read(ssl, buf + nread, maxcnt - nread);
307 readret = read(fd, buf + nread, maxcnt - nread);
310 readret = read(fd, buf+nread, maxcnt-nread);
311 #endif /* WITH_SSL */
314 /* we got EOF on the file descriptor */
315 DEBUG(5,("read_socket_with_timeout: timeout read. EOF from client.\n"));
316 smb_read_error = READ_EOF;
321 /* the descriptor is probably dead */
322 DEBUG(0,("read_socket_with_timeout: timeout read. read error = %s.\n", strerror(errno) ));
323 smb_read_error = READ_ERROR;
330 /* Return the number we got */
331 return (ssize_t)nread;
334 /****************************************************************************
335 Read data from a fd with a timout in msec.
336 mincount = if timeout, minimum to read before returning
337 maxcount = number to be read.
338 time_out = timeout in milliseconds
339 ****************************************************************************/
341 ssize_t read_with_timeout(int fd, char *buf, size_t mincnt, size_t maxcnt,
342 unsigned int time_out)
347 /* just checking .... */
353 if (mincnt == 0) mincnt = maxcnt;
355 while (nread < mincnt) {
358 readret = SSL_read(ssl, buf + nread, maxcnt - nread);
360 readret = read(fd, buf + nread, maxcnt - nread);
363 readret = read(fd, buf + nread, maxcnt - nread);
364 #endif /* WITH_SSL */
371 return((ssize_t)nread);
374 /* Most difficult - timeout read */
375 /* If this is ever called on a disk file and
376 mincnt is greater then the filesize then
377 system performance will suffer severely as
378 select always returns true on disk files */
380 for (nread=0; nread < mincnt; ) {
381 int selrtn = read_data_outstanding(fd, time_out);
388 readret = SSL_read(ssl, buf + nread, maxcnt - nread);
390 readret = read(fd, buf + nread, maxcnt - nread);
393 readret = read(fd, buf+nread, maxcnt-nread);
394 #endif /* WITH_SSL */
402 /* Return the number we got */
403 return((ssize_t)nread);
406 /****************************************************************************
407 send a keepalive packet (rfc1002)
408 ****************************************************************************/
410 BOOL send_keepalive(int client)
412 unsigned char buf[4];
414 buf[0] = SMBkeepalive;
415 buf[1] = buf[2] = buf[3] = 0;
417 return(write_socket_data(client,(char *)buf,4) == 4);
420 /****************************************************************************
421 read data from the client, reading exactly N bytes.
422 ****************************************************************************/
424 ssize_t read_data(int fd,char *buffer,size_t N)
435 ret = SSL_read(ssl, buffer + total, N - total);
437 ret = read(fd,buffer + total,N - total);
440 ret = read(fd,buffer + total,N - total);
441 #endif /* WITH_SSL */
445 DEBUG(10,("read_data: read of %d returned 0. Error = %s\n", (int)(N - total), strerror(errno) ));
446 smb_read_error = READ_EOF;
451 DEBUG(0,("read_data: read failure for %d. Error = %s\n", (int)(N - total), strerror(errno) ));
452 smb_read_error = READ_ERROR;
457 return (ssize_t)total;
460 /****************************************************************************
461 Read data from a socket, reading exactly N bytes.
462 ****************************************************************************/
464 static ssize_t read_socket_data(int fd,char *buffer,size_t N)
475 ret = SSL_read(ssl, buffer + total, N - total);
477 ret = read(fd,buffer + total,N - total);
480 ret = read(fd,buffer + total,N - total);
481 #endif /* WITH_SSL */
485 DEBUG(10,("read_socket_data: recv of %d returned 0. Error = %s\n", (int)(N - total), strerror(errno) ));
486 smb_read_error = READ_EOF;
491 DEBUG(0,("read_socket_data: recv failure for %d. Error = %s\n", (int)(N - total), strerror(errno) ));
492 smb_read_error = READ_ERROR;
497 return (ssize_t)total;
500 /****************************************************************************
502 ****************************************************************************/
504 ssize_t write_data(int fd,char *buffer,size_t N)
513 ret = SSL_write(ssl,buffer + total,N - total);
515 ret = write(fd,buffer + total,N - total);
518 ret = write(fd,buffer + total,N - total);
519 #endif /* WITH_SSL */
522 DEBUG(0,("write_data: write failure. Error = %s\n", strerror(errno) ));
525 if (ret == 0) return total;
529 return (ssize_t)total;
532 /****************************************************************************
533 Write data to a socket - use send rather than write.
534 ****************************************************************************/
536 ssize_t write_socket_data(int fd,char *buffer,size_t N)
545 ret = SSL_write(ssl,buffer + total,N - total);
547 ret = send(fd,buffer + total,N - total, 0);
550 ret = send(fd,buffer + total,N - total,0);
551 #endif /* WITH_SSL */
554 DEBUG(0,("write_socket_data: write failure. Error = %s\n", strerror(errno) ));
557 if (ret == 0) return total;
561 return (ssize_t)total;
564 /****************************************************************************
566 ****************************************************************************/
568 ssize_t write_socket(int fd,char *buf,size_t len)
572 DEBUG(6,("write_socket(%d,%d)\n",fd,(int)len));
573 ret = write_socket_data(fd,buf,len);
575 DEBUG(6,("write_socket(%d,%d) wrote %d\n",fd,(int)len,(int)ret));
577 DEBUG(0,("write_socket: Error writing %d bytes to socket %d: ERRNO = %s\n",
578 (int)len, fd, strerror(errno) ));
583 /****************************************************************************
584 read 4 bytes of a smb packet and return the smb length of the packet
585 store the result in the buffer
586 This version of the function will return a length of zero on receiving
588 timeout is in milliseconds.
589 ****************************************************************************/
591 static ssize_t read_smb_length_return_keepalive(int fd,char *inbuf,unsigned int timeout)
600 ok = (read_socket_with_timeout(fd,inbuf,4,4,timeout) == 4);
602 ok = (read_socket_data(fd,inbuf,4) == 4);
607 len = smb_len(inbuf);
608 msg_type = CVAL(inbuf,0);
610 if (msg_type == SMBkeepalive)
611 DEBUG(5,("Got keepalive packet\n"));
614 DEBUG(10,("got smb length of %d\n",len));
619 /****************************************************************************
620 read 4 bytes of a smb packet and return the smb length of the packet
621 store the result in the buffer. This version of the function will
622 never return a session keepalive (length of zero).
623 timeout is in milliseconds.
624 ****************************************************************************/
626 ssize_t read_smb_length(int fd,char *inbuf,unsigned int timeout)
632 len = read_smb_length_return_keepalive(fd, inbuf, timeout);
637 /* Ignore session keepalives. */
638 if(CVAL(inbuf,0) != SMBkeepalive)
642 DEBUG(10,("read_smb_length: got smb length of %d\n",len));
647 /****************************************************************************
648 read an smb from a fd. Note that the buffer *MUST* be of size
649 BUFFER_SIZE+SAFETY_MARGIN.
650 The timeout is in milliseconds.
651 This function will return on a
652 receipt of a session keepalive packet.
653 ****************************************************************************/
655 BOOL receive_smb(int fd,char *buffer, unsigned int timeout)
661 memset(buffer,'\0',smb_size + 100);
663 len = read_smb_length_return_keepalive(fd,buffer,timeout);
665 DEBUG(10,("receive_smb: length < 0!\n"));
666 /* XXX: You might think that we ought to set
667 * smb_read_error here, but apparently that breaks the
668 * recursive main loop in oplock.c. Global variables
674 * A WRITEX with CAP_LARGE_WRITEX can be 64k worth of data plus 65 bytes
675 * of header. Don't print the error if this fits.... JRA.
678 if (len > (BUFFER_SIZE + LARGE_WRITEX_HDR_SIZE)) {
679 DEBUG(0,("Invalid packet length! (%d bytes).\n",len));
680 if (len > BUFFER_SIZE + (SAFETY_MARGIN/2)) {
681 smb_read_error = READ_ERROR;
687 ret = read_socket_data(fd,buffer+4,len);
689 smb_read_error = READ_ERROR;
697 /****************************************************************************
698 read an smb from a fd ignoring all keepalive packets. Note that the buffer
699 *MUST* be of size BUFFER_SIZE+SAFETY_MARGIN.
700 The timeout is in milliseconds
702 This is exactly the same as receive_smb except that it never returns
703 a session keepalive packet (just as receive_smb used to do).
704 receive_smb was changed to return keepalives as the oplock processing means this call
705 should never go into a blocking read.
706 ****************************************************************************/
708 BOOL client_receive_smb(int fd,char *buffer, unsigned int timeout)
714 ret = receive_smb(fd, buffer, timeout);
718 DEBUG(10,("client_receive_smb failed\n"));
723 /* Ignore session keepalive packets. */
724 if(CVAL(buffer,0) != SMBkeepalive)
731 /****************************************************************************
733 ****************************************************************************/
735 BOOL send_smb(int fd,char *buffer)
740 len = smb_len(buffer) + 4;
742 while (nwritten < len) {
743 ret = write_socket(fd,buffer+nwritten,len - nwritten);
745 DEBUG(0,("Error writing %d bytes to client. %d. (%s)\n",
746 (int)len,(int)ret, strerror(errno) ));
755 /****************************************************************************
756 send a single packet to a port on another machine
757 ****************************************************************************/
759 BOOL send_one_packet(char *buf,int len,struct in_addr ip,int port,int type)
763 struct sockaddr_in sock_out;
765 /* create a socket to write to */
766 out_fd = socket(AF_INET, type, 0);
769 DEBUG(0,("socket failed"));
773 /* set the address and port */
774 memset((char *)&sock_out,'\0',sizeof(sock_out));
775 putip((char *)&sock_out.sin_addr,(char *)&ip);
776 sock_out.sin_port = htons( port );
777 sock_out.sin_family = AF_INET;
780 DEBUG(3,("sending a packet of len %d to (%s) on port %d of type %s\n",
781 len,inet_ntoa(ip),port,type==SOCK_DGRAM?"DGRAM":"STREAM"));
784 ret = (sendto(out_fd,buf,len,0,(struct sockaddr *)&sock_out,sizeof(sock_out)) >= 0);
787 DEBUG(0,("Packet send to %s(%d) failed ERRNO=%s\n",
788 inet_ntoa(ip),port,strerror(errno)));
794 /****************************************************************************
795 Open a socket of the specified type, port, and address for incoming data.
796 ****************************************************************************/
798 int open_socket_in( int type, int port, int dlevel, uint32 socket_addr, BOOL rebind )
800 struct sockaddr_in sock;
803 memset( (char *)&sock, '\0', sizeof(sock) );
805 #ifdef HAVE_SOCK_SIN_LEN
806 sock.sin_len = sizeof(sock);
808 sock.sin_port = htons( port );
809 sock.sin_family = AF_INET;
810 sock.sin_addr.s_addr = socket_addr;
812 res = socket( AF_INET, type, 0 );
815 dbgtext( "open_socket_in(): socket() call failed: " );
816 dbgtext( "%s\n", strerror( errno ) );
821 /* This block sets/clears the SO_REUSEADDR and possibly SO_REUSEPORT. */
823 int val = rebind ? 1 : 0;
824 if( setsockopt(res,SOL_SOCKET,SO_REUSEADDR,(char *)&val,sizeof(val)) == -1 ) {
825 if( DEBUGLVL( dlevel ) ) {
826 dbgtext( "open_socket_in(): setsockopt: " );
827 dbgtext( "SO_REUSEADDR = %s ", val?"True":"False" );
828 dbgtext( "on port %d failed ", port );
829 dbgtext( "with error = %s\n", strerror(errno) );
833 if( setsockopt(res,SOL_SOCKET,SO_REUSEPORT,(char *)&val,sizeof(val)) == -1 ) {
834 if( DEBUGLVL( dlevel ) ) {
835 dbgtext( "open_socket_in(): setsockopt: ");
836 dbgtext( "SO_REUSEPORT = %s ", val?"True":"False" );
837 dbgtext( "on port %d failed ", port );
838 dbgtext( "with error = %s\n", strerror(errno) );
841 #endif /* SO_REUSEPORT */
844 /* now we've got a socket - we need to bind it */
845 if( bind( res, (struct sockaddr *)&sock, sizeof(sock) ) == -1 ) {
846 if( DEBUGLVL(dlevel) && (port == SMB_PORT || port == NMB_PORT) ) {
847 dbgtext( "bind failed on port %d ", port );
848 dbgtext( "socket_addr = %s.\n", inet_ntoa( sock.sin_addr ) );
849 dbgtext( "Error = %s\n", strerror(errno) );
855 DEBUG( 3, ( "bind succeeded on port %d\n", port ) );
860 /****************************************************************************
861 create an outgoing socket. timeout is in milliseconds.
862 **************************************************************************/
864 int open_socket_out(int type, struct in_addr *addr, int port ,int timeout)
866 struct sockaddr_in sock_out;
868 int connect_loop = 250; /* 250 milliseconds */
869 int loops = (timeout) / connect_loop;
871 /* create a socket to write to */
872 res = socket(PF_INET, type, 0);
874 { DEBUG(0,("socket error\n")); return -1; }
876 if (type != SOCK_STREAM) return(res);
878 memset((char *)&sock_out,'\0',sizeof(sock_out));
879 putip((char *)&sock_out.sin_addr,(char *)addr);
881 sock_out.sin_port = htons( port );
882 sock_out.sin_family = PF_INET;
884 /* set it non-blocking */
885 set_blocking(res,False);
887 DEBUG(3,("Connecting to %s at port %d\n",inet_ntoa(*addr),port));
889 /* and connect it to the destination */
891 ret = connect(res,(struct sockaddr *)&sock_out,sizeof(sock_out));
893 /* Some systems return EAGAIN when they mean EINPROGRESS */
894 if (ret < 0 && (errno == EINPROGRESS || errno == EALREADY ||
895 errno == EAGAIN) && loops--) {
896 msleep(connect_loop);
900 if (ret < 0 && (errno == EINPROGRESS || errno == EALREADY ||
902 DEBUG(1,("timeout connecting to %s:%d\n",inet_ntoa(*addr),port));
908 if (ret < 0 && errno == EISCONN) {
915 DEBUG(2,("error connecting to %s:%d (%s)\n",
916 inet_ntoa(*addr),port,strerror(errno)));
921 /* set it blocking again */
922 set_blocking(res,True);
928 open a connected UDP socket to host on port
930 int open_udp_socket(const char *host, int port)
932 int type = SOCK_DGRAM;
933 struct sockaddr_in sock_out;
935 struct in_addr *addr;
937 addr = interpret_addr2(host);
939 res = socket(PF_INET, type, 0);
944 memset((char *)&sock_out,'\0',sizeof(sock_out));
945 putip((char *)&sock_out.sin_addr,(char *)addr);
946 sock_out.sin_port = htons(port);
947 sock_out.sin_family = PF_INET;
949 if (connect(res,(struct sockaddr *)&sock_out,sizeof(sock_out))) {
958 /* the following 3 client_*() functions are nasty ways of allowing
959 some generic functions to get info that really should be hidden in
960 particular modules */
961 static int client_fd = -1;
963 void client_setfd(int fd)
968 char *client_name(void)
970 return get_socket_name(client_fd);
973 char *client_addr(void)
975 return get_socket_addr(client_fd);
978 /*******************************************************************
979 matchname - determine if host name matches IP address. Used to
980 confirm a hostname lookup to prevent spoof attacks
981 ******************************************************************/
982 static BOOL matchname(char *remotehost,struct in_addr addr)
987 if ((hp = sys_gethostbyname(remotehost)) == 0) {
988 DEBUG(0,("sys_gethostbyname(%s): lookup failure.\n", remotehost));
993 * Make sure that gethostbyname() returns the "correct" host name.
994 * Unfortunately, gethostbyname("localhost") sometimes yields
995 * "localhost.domain". Since the latter host name comes from the
996 * local DNS, we just have to trust it (all bets are off if the local
997 * DNS is perverted). We always check the address list, though.
1000 if (strcasecmp(remotehost, hp->h_name)
1001 && strcasecmp(remotehost, "localhost")) {
1002 DEBUG(0,("host name/name mismatch: %s != %s\n",
1003 remotehost, hp->h_name));
1007 /* Look up the host address in the address list we just got. */
1008 for (i = 0; hp->h_addr_list[i]; i++) {
1009 if (memcmp(hp->h_addr_list[i], (caddr_t) & addr, sizeof(addr)) == 0)
1014 * The host name does not map to the original host address. Perhaps
1015 * someone has compromised a name server. More likely someone botched
1016 * it, but that could be dangerous, too.
1019 DEBUG(0,("host name/address mismatch: %s != %s\n",
1020 inet_ntoa(addr), hp->h_name));
1025 /*******************************************************************
1026 return the DNS name of the remote end of a socket
1027 ******************************************************************/
1028 char *get_socket_name(int fd)
1030 static pstring name_buf;
1031 static fstring addr_buf;
1033 struct in_addr addr;
1036 /* reverse lookups can be *very* expensive, and in many
1037 situations won't work because many networks don't link dhcp
1038 with dns. To avoid the delay we avoid the lookup if
1040 if (!lp_hostname_lookups()) {
1041 return get_socket_addr(fd);
1044 p = get_socket_addr(fd);
1046 /* it might be the same as the last one - save some DNS work */
1047 if (strcmp(p, addr_buf) == 0) return name_buf;
1049 pstrcpy(name_buf,"UNKNOWN");
1050 if (fd == -1) return name_buf;
1052 fstrcpy(addr_buf, p);
1054 addr = *interpret_addr2(p);
1056 /* Look up the remote host name. */
1057 if ((hp = gethostbyaddr((char *)&addr.s_addr, sizeof(addr.s_addr), AF_INET)) == 0) {
1058 DEBUG(1,("Gethostbyaddr failed for %s\n",p));
1059 pstrcpy(name_buf, p);
1061 pstrcpy(name_buf,(char *)hp->h_name);
1062 if (!matchname(name_buf, addr)) {
1063 DEBUG(0,("Matchname failed on %s %s\n",name_buf,p));
1064 pstrcpy(name_buf,"UNKNOWN");
1068 alpha_strcpy(name_buf, name_buf, "_-.", sizeof(name_buf));
1069 if (strstr(name_buf,"..")) {
1070 pstrcpy(name_buf, "UNKNOWN");
1076 /*******************************************************************
1077 return the IP addr of the remote end of a socket as a string
1078 ******************************************************************/
1079 char *get_socket_addr(int fd)
1082 struct sockaddr_in *sockin = (struct sockaddr_in *) (&sa);
1083 int length = sizeof(sa);
1084 static fstring addr_buf;
1086 fstrcpy(addr_buf,"0.0.0.0");
1092 if (getpeername(fd, &sa, &length) < 0) {
1093 DEBUG(0,("getpeername failed. Error was %s\n", strerror(errno) ));
1097 fstrcpy(addr_buf,(char *)inet_ntoa(sockin->sin_addr));
1102 /*******************************************************************
1103 opens and connects to a unix pipe socket
1104 ******************************************************************/
1105 int open_pipe_sock(char *path)
1108 struct sockaddr_un sa;
1110 sock = socket(AF_UNIX, SOCK_STREAM, 0);
1114 DEBUG(0, ("unix socket open failed\n"));
1119 sa.sun_family = AF_UNIX;
1120 safe_strcpy(sa.sun_path, path, sizeof(sa.sun_path)-1);
1122 DEBUG(10, ("socket open succeeded. file name: %s\n", sa.sun_path));
1124 if (connect(sock, (struct sockaddr*) &sa, sizeof(sa)) < 0)
1126 DEBUG(0,("socket connect to %s failed\n", sa.sun_path));
1134 /*******************************************************************
1135 Create protected unix domain socket.
1137 some unixen cannot set permissions on a ux-dom-sock, so we
1138 have to make sure that the directory contains the protection
1139 permissions, instead.
1140 ******************************************************************/
1141 int create_pipe_sock(const char *socket_dir,
1142 const char *socket_name,
1145 struct sockaddr_un sunaddr;
1151 /* Create the socket directory or reuse the existing one */
1153 if (lstat(socket_dir, &st) == -1) {
1155 if (errno == ENOENT) {
1157 /* Create directory */
1159 if (mkdir(socket_dir, dir_perms) == -1) {
1160 DEBUG(0, ("error creating socket directory "
1161 "%s: %s\n", socket_dir,
1168 DEBUG(0, ("lstat failed on socket directory %s: %s\n",
1169 socket_dir, strerror(errno)));
1175 /* Check ownership and permission on existing directory */
1177 if (!S_ISDIR(st.st_mode)) {
1178 DEBUG(0, ("socket directory %s isn't a directory\n",
1183 if ((st.st_uid != sec_initial_uid()) ||
1184 ((st.st_mode & 0777) != dir_perms)) {
1185 DEBUG(0, ("invalid permissions on socket directory "
1186 "%s\n", socket_dir));
1191 /* Create the socket file */
1193 old_umask = umask(0);
1195 sock = socket(AF_UNIX, SOCK_STREAM, 0);
1203 snprintf(path, sizeof(path), "%s/%s", socket_dir, socket_name);
1206 memset(&sunaddr, 0, sizeof(sunaddr));
1207 sunaddr.sun_family = AF_UNIX;
1208 safe_strcpy(sunaddr.sun_path, path, sizeof(sunaddr.sun_path)-1);
1210 if (bind(sock, (struct sockaddr *)&sunaddr, sizeof(sunaddr)) == -1) {
1211 DEBUG(0, ("bind failed on pipe socket %s: %s\n",
1219 if (listen(sock, 5) == -1) {
1220 DEBUG(0, ("listen failed on pipe socket %s: %s\n",
1235 /*******************************************************************
1236 this is like socketpair but uses tcp. It is used by the Samba
1237 regression test code
1238 The function guarantees that nobody else can attach to the socket,
1239 or if they do that this function fails and the socket gets closed
1240 returns 0 on success, -1 on failure
1241 the resulting file descriptors are symmetrical
1242 ******************************************************************/
1243 static int socketpair_tcp(int fd[2])
1246 struct sockaddr_in sock;
1247 struct sockaddr_in sock2;
1248 socklen_t socklen = sizeof(sock);
1249 int connect_done = 0;
1251 fd[0] = fd[1] = listener = -1;
1253 memset(&sock, 0, sizeof(sock));
1255 if ((listener = socket(PF_INET, SOCK_STREAM, 0)) == -1) goto failed;
1257 memset(&sock2, 0, sizeof(sock2));
1258 #ifdef HAVE_SOCK_SIN_LEN
1259 sock2.sin_len = sizeof(sock2);
1261 sock2.sin_family = PF_INET;
1263 bind(listener, (struct sockaddr *)&sock2, sizeof(sock2));
1265 if (listen(listener, 1) != 0) goto failed;
1267 if (getsockname(listener, (struct sockaddr *)&sock, &socklen) != 0) goto failed;
1269 if ((fd[1] = socket(PF_INET, SOCK_STREAM, 0)) == -1) goto failed;
1271 set_blocking(fd[1], 0);
1273 sock.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
1275 if (connect(fd[1],(struct sockaddr *)&sock,sizeof(sock)) == -1) {
1276 if (errno != EINPROGRESS) goto failed;
1281 if ((fd[0] = accept(listener, (struct sockaddr *)&sock, &socklen)) == -1) goto failed;
1284 if (connect_done == 0) {
1285 if (connect(fd[1],(struct sockaddr *)&sock,sizeof(sock)) != 0
1286 && errno != EISCONN) goto failed;
1289 set_blocking(fd[1], 1);
1295 if (fd[0] != -1) close(fd[0]);
1296 if (fd[1] != -1) close(fd[1]);
1297 if (listener != -1) close(listener);
1302 /*******************************************************************
1303 run a program on a local tcp socket, this is used to launch smbd
1304 when regression testing
1305 the return value is a socket which is attached to a subprocess
1306 running "prog". stdin and stdout are attached. stderr is left
1307 attached to the original stderr
1308 ******************************************************************/
1309 int sock_exec(const char *prog)
1312 if (socketpair_tcp(fd) != 0) {
1313 DEBUG(0,("socketpair_tcp failed (%s)\n", strerror(errno)));