add another registry rpc (opnum 0x14). Have no idea what it's real name
[Samba.git] / source / lib / util_sock.c
blob4f1f2a1470ad8f25a0f874732f5836fbc93785a6
1 /*
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.
22 #include "includes.h"
24 /* the last IP received from */
25 struct in_addr lastip;
27 /* the last port received from */
28 int lastport=0;
30 int smb_read_error = 0;
32 /****************************************************************************
33 Determine if a file descriptor is in fact a socket.
34 ****************************************************************************/
36 BOOL is_a_socket(int fd)
38 int v,l;
39 l = sizeof(int);
40 return(getsockopt(fd, SOL_SOCKET, SO_TYPE, (char *)&v, &l) == 0);
43 enum SOCK_OPT_TYPES {OPT_BOOL,OPT_INT,OPT_ON};
45 typedef struct smb_socket_option {
46 char *name;
47 int level;
48 int option;
49 int value;
50 int opttype;
51 } smb_socket_option;
53 static const smb_socket_option socket_options[] = {
54 {"SO_KEEPALIVE", SOL_SOCKET, SO_KEEPALIVE, 0, OPT_BOOL},
55 {"SO_REUSEADDR", SOL_SOCKET, SO_REUSEADDR, 0, OPT_BOOL},
56 {"SO_BROADCAST", SOL_SOCKET, SO_BROADCAST, 0, OPT_BOOL},
57 #ifdef TCP_NODELAY
58 {"TCP_NODELAY", IPPROTO_TCP, TCP_NODELAY, 0, OPT_BOOL},
59 #endif
60 #ifdef IPTOS_LOWDELAY
61 {"IPTOS_LOWDELAY", IPPROTO_IP, IP_TOS, IPTOS_LOWDELAY, OPT_ON},
62 #endif
63 #ifdef IPTOS_THROUGHPUT
64 {"IPTOS_THROUGHPUT", IPPROTO_IP, IP_TOS, IPTOS_THROUGHPUT, OPT_ON},
65 #endif
66 #ifdef SO_REUSEPORT
67 {"SO_REUSEPORT", SOL_SOCKET, SO_REUSEPORT, 0, OPT_BOOL},
68 #endif
69 #ifdef SO_SNDBUF
70 {"SO_SNDBUF", SOL_SOCKET, SO_SNDBUF, 0, OPT_INT},
71 #endif
72 #ifdef SO_RCVBUF
73 {"SO_RCVBUF", SOL_SOCKET, SO_RCVBUF, 0, OPT_INT},
74 #endif
75 #ifdef SO_SNDLOWAT
76 {"SO_SNDLOWAT", SOL_SOCKET, SO_SNDLOWAT, 0, OPT_INT},
77 #endif
78 #ifdef SO_RCVLOWAT
79 {"SO_RCVLOWAT", SOL_SOCKET, SO_RCVLOWAT, 0, OPT_INT},
80 #endif
81 #ifdef SO_SNDTIMEO
82 {"SO_SNDTIMEO", SOL_SOCKET, SO_SNDTIMEO, 0, OPT_INT},
83 #endif
84 #ifdef SO_RCVTIMEO
85 {"SO_RCVTIMEO", SOL_SOCKET, SO_RCVTIMEO, 0, OPT_INT},
86 #endif
87 {NULL,0,0,0,0}};
89 /****************************************************************************
90 Print socket options.
91 ****************************************************************************/
93 static void print_socket_options(int s)
95 int value, vlen = 4;
96 const smb_socket_option *p = &socket_options[0];
98 for (; p->name != NULL; p++) {
99 if (getsockopt(s, p->level, p->option, (void *)&value, &vlen) == -1) {
100 DEBUG(5,("Could not test socket option %s.\n", p->name));
101 } else {
102 DEBUG(5,("socket option %s = %d\n",p->name,value));
107 /****************************************************************************
108 Set user socket options.
109 ****************************************************************************/
111 void set_socket_options(int fd, char *options)
113 fstring tok;
115 while (next_token(&options,tok," \t,", sizeof(tok))) {
116 int ret=0,i;
117 int value = 1;
118 char *p;
119 BOOL got_value = False;
121 if ((p = strchr_m(tok,'='))) {
122 *p = 0;
123 value = atoi(p+1);
124 got_value = True;
127 for (i=0;socket_options[i].name;i++)
128 if (strequal(socket_options[i].name,tok))
129 break;
131 if (!socket_options[i].name) {
132 DEBUG(0,("Unknown socket option %s\n",tok));
133 continue;
136 switch (socket_options[i].opttype) {
137 case OPT_BOOL:
138 case OPT_INT:
139 ret = setsockopt(fd,socket_options[i].level,
140 socket_options[i].option,(char *)&value,sizeof(int));
141 break;
143 case OPT_ON:
144 if (got_value)
145 DEBUG(0,("syntax error - %s does not take a value\n",tok));
148 int on = socket_options[i].value;
149 ret = setsockopt(fd,socket_options[i].level,
150 socket_options[i].option,(char *)&on,sizeof(int));
152 break;
155 if (ret != 0)
156 DEBUG(0,("Failed to set socket option %s (Error %s)\n",tok, strerror(errno) ));
159 print_socket_options(fd);
162 /****************************************************************************
163 Read from a socket.
164 ****************************************************************************/
166 ssize_t read_udp_socket(int fd,char *buf,size_t len)
168 ssize_t ret;
169 struct sockaddr_in sock;
170 socklen_t socklen = sizeof(sock);
172 memset((char *)&sock,'\0',socklen);
173 memset((char *)&lastip,'\0',sizeof(lastip));
174 ret = (ssize_t)sys_recvfrom(fd,buf,len,0,(struct sockaddr *)&sock,&socklen);
175 if (ret <= 0) {
176 DEBUG(2,("read socket failed. ERRNO=%s\n",strerror(errno)));
177 return(0);
180 lastip = sock.sin_addr;
181 lastport = ntohs(sock.sin_port);
183 DEBUG(10,("read_udp_socket: lastip %s lastport %d read: %d\n",
184 inet_ntoa(lastip), lastport, ret));
186 return(ret);
189 /*******************************************************************
190 checks if read data is outstanding.
191 ********************************************************************/
192 static int read_data_outstanding(int fd, unsigned int time_out)
194 int selrtn;
195 fd_set fds;
196 struct timeval timeout;
198 FD_ZERO(&fds);
199 FD_SET(fd, &fds);
201 timeout.tv_sec = (time_t) (time_out / 1000);
202 timeout.tv_usec = (long)(1000 * (time_out % 1000));
204 selrtn = sys_select_intr(fd + 1, &fds, NULL, NULL, &timeout);
206 if (selrtn <= 0)
208 return selrtn;
210 return FD_ISSET(fd, &fds) ? 1 : 0;
213 /****************************************************************************
214 Read data from a socket with a timout in msec.
215 mincount = if timeout, minimum to read before returning
216 maxcount = number to be read.
217 time_out = timeout in milliseconds
218 ****************************************************************************/
220 static ssize_t read_socket_with_timeout(int fd,char *buf,size_t mincnt,size_t maxcnt,unsigned int time_out)
222 fd_set fds;
223 int selrtn;
224 ssize_t readret;
225 size_t nread = 0;
226 struct timeval timeout;
228 /* just checking .... */
229 if (maxcnt <= 0)
230 return(0);
232 smb_read_error = 0;
234 /* Blocking read */
235 if (time_out <= 0) {
236 if (mincnt == 0) mincnt = maxcnt;
238 while (nread < mincnt) {
239 readret = sys_read(fd, buf + nread, maxcnt - nread);
241 if (readret == 0) {
242 DEBUG(5,("read_socket_with_timeout: blocking read. EOF from client.\n"));
243 smb_read_error = READ_EOF;
244 return -1;
247 if (readret == -1) {
248 DEBUG(0,("read_socket_with_timeout: read error = %s.\n", strerror(errno) ));
249 smb_read_error = READ_ERROR;
250 return -1;
252 nread += readret;
254 return((ssize_t)nread);
257 /* Most difficult - timeout read */
258 /* If this is ever called on a disk file and
259 mincnt is greater then the filesize then
260 system performance will suffer severely as
261 select always returns true on disk files */
263 /* Set initial timeout */
264 timeout.tv_sec = (time_t)(time_out / 1000);
265 timeout.tv_usec = (long)(1000 * (time_out % 1000));
267 for (nread=0; nread < mincnt; ) {
268 FD_ZERO(&fds);
269 FD_SET(fd,&fds);
271 selrtn = sys_select_intr(fd+1,&fds,NULL,NULL,&timeout);
273 /* Check if error */
274 if (selrtn == -1) {
275 /* something is wrong. Maybe the socket is dead? */
276 DEBUG(0,("read_socket_with_timeout: timeout read. select error = %s.\n", strerror(errno) ));
277 smb_read_error = READ_ERROR;
278 return -1;
281 /* Did we timeout ? */
282 if (selrtn == 0) {
283 DEBUG(10,("read_socket_with_timeout: timeout read. select timed out.\n"));
284 smb_read_error = READ_TIMEOUT;
285 return -1;
288 readret = sys_read(fd, buf+nread, maxcnt-nread);
290 if (readret == 0) {
291 /* we got EOF on the file descriptor */
292 DEBUG(5,("read_socket_with_timeout: timeout read. EOF from client.\n"));
293 smb_read_error = READ_EOF;
294 return -1;
297 if (readret == -1) {
298 /* the descriptor is probably dead */
299 DEBUG(0,("read_socket_with_timeout: timeout read. read error = %s.\n", strerror(errno) ));
300 smb_read_error = READ_ERROR;
301 return -1;
304 nread += readret;
307 /* Return the number we got */
308 return (ssize_t)nread;
311 /****************************************************************************
312 Read data from a fd with a timout in msec.
313 mincount = if timeout, minimum to read before returning
314 maxcount = number to be read.
315 time_out = timeout in milliseconds
316 ****************************************************************************/
318 ssize_t read_with_timeout(int fd, char *buf, size_t mincnt, size_t maxcnt,
319 unsigned int time_out)
321 ssize_t readret;
322 size_t nread = 0;
324 /* just checking .... */
325 if (maxcnt <= 0)
326 return(0);
328 /* Blocking read */
329 if (time_out <= 0) {
330 if (mincnt == 0) mincnt = maxcnt;
332 while (nread < mincnt) {
333 readret = sys_read(fd, buf + nread, maxcnt - nread);
335 if (readret <= 0)
336 return readret;
338 nread += readret;
340 return((ssize_t)nread);
343 /* Most difficult - timeout read */
344 /* If this is ever called on a disk file and
345 mincnt is greater then the filesize then
346 system performance will suffer severely as
347 select always returns true on disk files */
349 for (nread=0; nread < mincnt; ) {
350 int selrtn = read_data_outstanding(fd, time_out);
352 if(selrtn <= 0)
353 return selrtn;
355 readret = sys_read(fd, buf+nread, maxcnt-nread);
357 if (readret <= 0)
358 return readret;
360 nread += readret;
363 /* Return the number we got */
364 return((ssize_t)nread);
367 /****************************************************************************
368 read data from the client, reading exactly N bytes.
369 ****************************************************************************/
371 ssize_t read_data(int fd,char *buffer,size_t N)
373 ssize_t ret;
374 size_t total=0;
376 smb_read_error = 0;
378 while (total < N) {
379 ret = sys_read(fd,buffer + total,N - total);
381 if (ret == 0) {
382 DEBUG(10,("read_data: read of %d returned 0. Error = %s\n", (int)(N - total), strerror(errno) ));
383 smb_read_error = READ_EOF;
384 return 0;
387 if (ret == -1) {
388 DEBUG(0,("read_data: read failure for %d. Error = %s\n", (int)(N - total), strerror(errno) ));
389 smb_read_error = READ_ERROR;
390 return -1;
392 total += ret;
394 return (ssize_t)total;
397 /****************************************************************************
398 Read data from a socket, reading exactly N bytes.
399 ****************************************************************************/
401 static ssize_t read_socket_data(int fd,char *buffer,size_t N)
403 ssize_t ret;
404 size_t total=0;
406 smb_read_error = 0;
408 while (total < N) {
409 ret = sys_read(fd,buffer + total,N - total);
411 if (ret == 0) {
412 DEBUG(10,("read_socket_data: recv of %d returned 0. Error = %s\n", (int)(N - total), strerror(errno) ));
413 smb_read_error = READ_EOF;
414 return 0;
417 if (ret == -1) {
418 DEBUG(0,("read_socket_data: recv failure for %d. Error = %s\n", (int)(N - total), strerror(errno) ));
419 smb_read_error = READ_ERROR;
420 return -1;
422 total += ret;
424 return (ssize_t)total;
427 /****************************************************************************
428 Write data to a fd.
429 ****************************************************************************/
431 ssize_t write_data(int fd,char *buffer,size_t N)
433 size_t total=0;
434 ssize_t ret;
436 while (total < N) {
437 ret = sys_write(fd,buffer + total,N - total);
439 if (ret == -1) {
440 DEBUG(0,("write_data: write failure. Error = %s\n", strerror(errno) ));
441 return -1;
443 if (ret == 0)
444 return total;
446 total += ret;
448 return (ssize_t)total;
451 /****************************************************************************
452 Write data to a socket - use send rather than write.
453 ****************************************************************************/
455 static ssize_t write_socket_data(int fd,char *buffer,size_t N)
457 size_t total=0;
458 ssize_t ret;
460 while (total < N) {
461 ret = sys_send(fd,buffer + total,N - total,0);
463 if (ret == -1) {
464 DEBUG(0,("write_socket_data: write failure. Error = %s\n", strerror(errno) ));
465 return -1;
467 if (ret == 0)
468 return total;
470 total += ret;
472 return (ssize_t)total;
475 /****************************************************************************
476 write to a socket
477 ****************************************************************************/
479 ssize_t write_socket(int fd,char *buf,size_t len)
481 ssize_t ret=0;
483 DEBUG(6,("write_socket(%d,%d)\n",fd,(int)len));
484 ret = write_socket_data(fd,buf,len);
486 DEBUG(6,("write_socket(%d,%d) wrote %d\n",fd,(int)len,(int)ret));
487 if(ret <= 0)
488 DEBUG(0,("write_socket: Error writing %d bytes to socket %d: ERRNO = %s\n",
489 (int)len, fd, strerror(errno) ));
491 return(ret);
494 /****************************************************************************
495 send a keepalive packet (rfc1002)
496 ****************************************************************************/
498 BOOL send_keepalive(int client)
500 unsigned char buf[4];
502 buf[0] = SMBkeepalive;
503 buf[1] = buf[2] = buf[3] = 0;
505 return(write_socket_data(client,(char *)buf,4) == 4);
509 /****************************************************************************
510 read 4 bytes of a smb packet and return the smb length of the packet
511 store the result in the buffer
512 This version of the function will return a length of zero on receiving
513 a keepalive packet.
514 timeout is in milliseconds.
515 ****************************************************************************/
517 static ssize_t read_smb_length_return_keepalive(int fd,char *inbuf,unsigned int timeout)
519 ssize_t len=0;
520 int msg_type;
521 BOOL ok = False;
523 while (!ok) {
524 if (timeout > 0)
525 ok = (read_socket_with_timeout(fd,inbuf,4,4,timeout) == 4);
526 else
527 ok = (read_socket_data(fd,inbuf,4) == 4);
529 if (!ok)
530 return(-1);
532 len = smb_len(inbuf);
533 msg_type = CVAL(inbuf,0);
535 if (msg_type == SMBkeepalive)
536 DEBUG(5,("Got keepalive packet\n"));
539 DEBUG(10,("got smb length of %d\n",len));
541 return(len);
544 /****************************************************************************
545 read 4 bytes of a smb packet and return the smb length of the packet
546 store the result in the buffer. This version of the function will
547 never return a session keepalive (length of zero).
548 timeout is in milliseconds.
549 ****************************************************************************/
551 ssize_t read_smb_length(int fd,char *inbuf,unsigned int timeout)
553 ssize_t len;
555 for(;;) {
556 len = read_smb_length_return_keepalive(fd, inbuf, timeout);
558 if(len < 0)
559 return len;
561 /* Ignore session keepalives. */
562 if(CVAL(inbuf,0) != SMBkeepalive)
563 break;
566 DEBUG(10,("read_smb_length: got smb length of %d\n",len));
568 return len;
571 /****************************************************************************
572 read an smb from a fd. Note that the buffer *MUST* be of size
573 BUFFER_SIZE+SAFETY_MARGIN.
574 The timeout is in milliseconds.
575 This function will return on a
576 receipt of a session keepalive packet.
577 ****************************************************************************/
579 BOOL receive_smb(int fd,char *buffer, unsigned int timeout)
581 ssize_t len,ret;
583 smb_read_error = 0;
585 memset(buffer,'\0',smb_size + 100);
587 len = read_smb_length_return_keepalive(fd,buffer,timeout);
588 if (len < 0) {
589 DEBUG(10,("receive_smb: length < 0!\n"));
592 * Correct fix. smb_read_error may have already been
593 * set. Only set it here if not already set. Global
594 * variables still suck :-). JRA.
597 if (smb_read_error == 0)
598 smb_read_error = READ_ERROR;
599 return False;
603 * A WRITEX with CAP_LARGE_WRITEX can be 64k worth of data plus 65 bytes
604 * of header. Don't print the error if this fits.... JRA.
607 if (len > (BUFFER_SIZE + LARGE_WRITEX_HDR_SIZE)) {
608 DEBUG(0,("Invalid packet length! (%d bytes).\n",len));
609 if (len > BUFFER_SIZE + (SAFETY_MARGIN/2)) {
612 * Correct fix. smb_read_error may have already been
613 * set. Only set it here if not already set. Global
614 * variables still suck :-). JRA.
617 if (smb_read_error == 0)
618 smb_read_error = READ_ERROR;
619 return False;
623 if(len > 0) {
624 ret = read_socket_data(fd,buffer+4,len);
625 if (ret != len) {
626 if (smb_read_error == 0)
627 smb_read_error = READ_ERROR;
628 return False;
632 return(True);
635 /****************************************************************************
636 send an smb to a fd
637 ****************************************************************************/
639 BOOL send_smb(int fd,char *buffer)
641 size_t len;
642 size_t nwritten=0;
643 ssize_t ret;
644 len = smb_len(buffer) + 4;
646 while (nwritten < len) {
647 ret = write_socket(fd,buffer+nwritten,len - nwritten);
648 if (ret <= 0) {
649 DEBUG(0,("Error writing %d bytes to client. %d. (%s)\n",
650 (int)len,(int)ret, strerror(errno) ));
651 return False;
653 nwritten += ret;
656 return True;
659 /****************************************************************************
660 Open a socket of the specified type, port, and address for incoming data.
661 ****************************************************************************/
663 int open_socket_in( int type, int port, int dlevel, uint32 socket_addr, BOOL rebind )
665 struct sockaddr_in sock;
666 int res;
668 memset( (char *)&sock, '\0', sizeof(sock) );
670 #ifdef HAVE_SOCK_SIN_LEN
671 sock.sin_len = sizeof(sock);
672 #endif
673 sock.sin_port = htons( port );
674 sock.sin_family = AF_INET;
675 sock.sin_addr.s_addr = socket_addr;
677 res = socket( AF_INET, type, 0 );
678 if( res == -1 ) {
679 if( DEBUGLVL(0) ) {
680 dbgtext( "open_socket_in(): socket() call failed: " );
681 dbgtext( "%s\n", strerror( errno ) );
683 return -1;
686 /* This block sets/clears the SO_REUSEADDR and possibly SO_REUSEPORT. */
688 int val = rebind ? 1 : 0;
689 if( setsockopt(res,SOL_SOCKET,SO_REUSEADDR,(char *)&val,sizeof(val)) == -1 ) {
690 if( DEBUGLVL( dlevel ) ) {
691 dbgtext( "open_socket_in(): setsockopt: " );
692 dbgtext( "SO_REUSEADDR = %s ", val?"True":"False" );
693 dbgtext( "on port %d failed ", port );
694 dbgtext( "with error = %s\n", strerror(errno) );
697 #ifdef SO_REUSEPORT
698 if( setsockopt(res,SOL_SOCKET,SO_REUSEPORT,(char *)&val,sizeof(val)) == -1 ) {
699 if( DEBUGLVL( dlevel ) ) {
700 dbgtext( "open_socket_in(): setsockopt: ");
701 dbgtext( "SO_REUSEPORT = %s ", val?"True":"False" );
702 dbgtext( "on port %d failed ", port );
703 dbgtext( "with error = %s\n", strerror(errno) );
706 #endif /* SO_REUSEPORT */
709 /* now we've got a socket - we need to bind it */
710 if( bind( res, (struct sockaddr *)&sock, sizeof(sock) ) == -1 ) {
711 if( DEBUGLVL(dlevel) && (port == SMB_PORT || port == NMB_PORT) ) {
712 dbgtext( "bind failed on port %d ", port );
713 dbgtext( "socket_addr = %s.\n", inet_ntoa( sock.sin_addr ) );
714 dbgtext( "Error = %s\n", strerror(errno) );
716 close( res );
717 return( -1 );
720 DEBUG( 10, ( "bind succeeded on port %d\n", port ) );
722 return( res );
725 /****************************************************************************
726 create an outgoing socket. timeout is in milliseconds.
727 **************************************************************************/
729 int open_socket_out(int type, struct in_addr *addr, int port ,int timeout)
731 struct sockaddr_in sock_out;
732 int res,ret;
733 int connect_loop = 250; /* 250 milliseconds */
734 int loops = (timeout) / connect_loop;
736 /* create a socket to write to */
737 res = socket(PF_INET, type, 0);
738 if (res == -1)
739 { DEBUG(0,("socket error\n")); return -1; }
741 if (type != SOCK_STREAM) return(res);
743 memset((char *)&sock_out,'\0',sizeof(sock_out));
744 putip((char *)&sock_out.sin_addr,(char *)addr);
746 sock_out.sin_port = htons( port );
747 sock_out.sin_family = PF_INET;
749 /* set it non-blocking */
750 set_blocking(res,False);
752 DEBUG(3,("Connecting to %s at port %d\n",inet_ntoa(*addr),port));
754 /* and connect it to the destination */
755 connect_again:
756 ret = connect(res,(struct sockaddr *)&sock_out,sizeof(sock_out));
758 /* Some systems return EAGAIN when they mean EINPROGRESS */
759 if (ret < 0 && (errno == EINPROGRESS || errno == EALREADY ||
760 errno == EAGAIN) && loops--) {
761 msleep(connect_loop);
762 goto connect_again;
765 if (ret < 0 && (errno == EINPROGRESS || errno == EALREADY ||
766 errno == EAGAIN)) {
767 DEBUG(1,("timeout connecting to %s:%d\n",inet_ntoa(*addr),port));
768 close(res);
769 return -1;
772 #ifdef EISCONN
773 if (ret < 0 && errno == EISCONN) {
774 errno = 0;
775 ret = 0;
777 #endif
779 if (ret < 0) {
780 DEBUG(2,("error connecting to %s:%d (%s)\n",
781 inet_ntoa(*addr),port,strerror(errno)));
782 close(res);
783 return -1;
786 /* set it blocking again */
787 set_blocking(res,True);
789 return res;
793 open a connected UDP socket to host on port
795 int open_udp_socket(const char *host, int port)
797 int type = SOCK_DGRAM;
798 struct sockaddr_in sock_out;
799 int res;
800 struct in_addr *addr;
802 addr = interpret_addr2(host);
804 res = socket(PF_INET, type, 0);
805 if (res == -1) {
806 return -1;
809 memset((char *)&sock_out,'\0',sizeof(sock_out));
810 putip((char *)&sock_out.sin_addr,(char *)addr);
811 sock_out.sin_port = htons(port);
812 sock_out.sin_family = PF_INET;
814 if (connect(res,(struct sockaddr *)&sock_out,sizeof(sock_out))) {
815 close(res);
816 return -1;
819 return res;
823 /* the following 3 client_*() functions are nasty ways of allowing
824 some generic functions to get info that really should be hidden in
825 particular modules */
826 static int client_fd = -1;
828 void client_setfd(int fd)
830 client_fd = fd;
833 char *client_name(void)
835 return get_socket_name(client_fd);
838 char *client_addr(void)
840 return get_socket_addr(client_fd);
843 /*******************************************************************
844 matchname - determine if host name matches IP address. Used to
845 confirm a hostname lookup to prevent spoof attacks
846 ******************************************************************/
847 static BOOL matchname(char *remotehost,struct in_addr addr)
849 struct hostent *hp;
850 int i;
852 if ((hp = sys_gethostbyname(remotehost)) == 0) {
853 DEBUG(0,("sys_gethostbyname(%s): lookup failure.\n", remotehost));
854 return False;
858 * Make sure that gethostbyname() returns the "correct" host name.
859 * Unfortunately, gethostbyname("localhost") sometimes yields
860 * "localhost.domain". Since the latter host name comes from the
861 * local DNS, we just have to trust it (all bets are off if the local
862 * DNS is perverted). We always check the address list, though.
865 if (strcasecmp(remotehost, hp->h_name)
866 && strcasecmp(remotehost, "localhost")) {
867 DEBUG(0,("host name/name mismatch: %s != %s\n",
868 remotehost, hp->h_name));
869 return False;
872 /* Look up the host address in the address list we just got. */
873 for (i = 0; hp->h_addr_list[i]; i++) {
874 if (memcmp(hp->h_addr_list[i], (caddr_t) & addr, sizeof(addr)) == 0)
875 return True;
879 * The host name does not map to the original host address. Perhaps
880 * someone has compromised a name server. More likely someone botched
881 * it, but that could be dangerous, too.
884 DEBUG(0,("host name/address mismatch: %s != %s\n",
885 inet_ntoa(addr), hp->h_name));
886 return False;
890 /*******************************************************************
891 return the DNS name of the remote end of a socket
892 ******************************************************************/
893 char *get_socket_name(int fd)
895 static pstring name_buf;
896 static fstring addr_buf;
897 struct hostent *hp;
898 struct in_addr addr;
899 char *p;
901 /* reverse lookups can be *very* expensive, and in many
902 situations won't work because many networks don't link dhcp
903 with dns. To avoid the delay we avoid the lookup if
904 possible */
905 if (!lp_hostname_lookups()) {
906 return get_socket_addr(fd);
909 p = get_socket_addr(fd);
911 /* it might be the same as the last one - save some DNS work */
912 if (strcmp(p, addr_buf) == 0) return name_buf;
914 pstrcpy(name_buf,"UNKNOWN");
915 if (fd == -1) return name_buf;
917 fstrcpy(addr_buf, p);
919 addr = *interpret_addr2(p);
921 /* Look up the remote host name. */
922 if ((hp = gethostbyaddr((char *)&addr.s_addr, sizeof(addr.s_addr), AF_INET)) == 0) {
923 DEBUG(1,("Gethostbyaddr failed for %s\n",p));
924 pstrcpy(name_buf, p);
925 } else {
926 pstrcpy(name_buf,(char *)hp->h_name);
927 if (!matchname(name_buf, addr)) {
928 DEBUG(0,("Matchname failed on %s %s\n",name_buf,p));
929 pstrcpy(name_buf,"UNKNOWN");
933 alpha_strcpy(name_buf, name_buf, "_-.", sizeof(name_buf));
934 if (strstr(name_buf,"..")) {
935 pstrcpy(name_buf, "UNKNOWN");
938 return name_buf;
941 /*******************************************************************
942 return the IP addr of the remote end of a socket as a string
943 ******************************************************************/
944 char *get_socket_addr(int fd)
946 struct sockaddr sa;
947 struct sockaddr_in *sockin = (struct sockaddr_in *) (&sa);
948 int length = sizeof(sa);
949 static fstring addr_buf;
951 fstrcpy(addr_buf,"0.0.0.0");
953 if (fd == -1) {
954 return addr_buf;
957 if (getpeername(fd, &sa, &length) < 0) {
958 DEBUG(0,("getpeername failed. Error was %s\n", strerror(errno) ));
959 return addr_buf;
962 fstrcpy(addr_buf,(char *)inet_ntoa(sockin->sin_addr));
964 return addr_buf;
968 /*******************************************************************
969 Create protected unix domain socket.
971 some unixen cannot set permissions on a ux-dom-sock, so we
972 have to make sure that the directory contains the protection
973 permissions, instead.
974 ******************************************************************/
975 int create_pipe_sock(const char *socket_dir,
976 const char *socket_name,
977 mode_t dir_perms)
979 struct sockaddr_un sunaddr;
980 struct stat st;
981 int sock;
982 mode_t old_umask;
983 pstring path;
985 /* Create the socket directory or reuse the existing one */
987 if (lstat(socket_dir, &st) == -1) {
989 if (errno == ENOENT) {
991 /* Create directory */
993 if (mkdir(socket_dir, dir_perms) == -1) {
994 DEBUG(0, ("error creating socket directory "
995 "%s: %s\n", socket_dir,
996 strerror(errno)));
997 return -1;
1000 } else {
1002 DEBUG(0, ("lstat failed on socket directory %s: %s\n",
1003 socket_dir, strerror(errno)));
1004 return -1;
1007 } else {
1009 /* Check ownership and permission on existing directory */
1011 if (!S_ISDIR(st.st_mode)) {
1012 DEBUG(0, ("socket directory %s isn't a directory\n",
1013 socket_dir));
1014 return -1;
1017 if ((st.st_uid != sec_initial_uid()) ||
1018 ((st.st_mode & 0777) != dir_perms)) {
1019 DEBUG(0, ("invalid permissions on socket directory "
1020 "%s\n", socket_dir));
1021 return -1;
1025 /* Create the socket file */
1027 old_umask = umask(0);
1029 sock = socket(AF_UNIX, SOCK_STREAM, 0);
1031 if (sock == -1) {
1032 perror("socket");
1033 umask(old_umask);
1034 return -1;
1037 snprintf(path, sizeof(path), "%s/%s", socket_dir, socket_name);
1039 unlink(path);
1040 memset(&sunaddr, 0, sizeof(sunaddr));
1041 sunaddr.sun_family = AF_UNIX;
1042 safe_strcpy(sunaddr.sun_path, path, sizeof(sunaddr.sun_path)-1);
1044 if (bind(sock, (struct sockaddr *)&sunaddr, sizeof(sunaddr)) == -1) {
1045 DEBUG(0, ("bind failed on pipe socket %s: %s\n",
1046 path,
1047 strerror(errno)));
1048 close(sock);
1049 umask(old_umask);
1050 return -1;
1053 if (listen(sock, 5) == -1) {
1054 DEBUG(0, ("listen failed on pipe socket %s: %s\n",
1055 path,
1056 strerror(errno)));
1057 close(sock);
1058 umask(old_umask);
1059 return -1;
1062 umask(old_umask);
1064 /* Success! */
1066 return sock;
1069 /*******************************************************************
1070 this is like socketpair but uses tcp. It is used by the Samba
1071 regression test code
1072 The function guarantees that nobody else can attach to the socket,
1073 or if they do that this function fails and the socket gets closed
1074 returns 0 on success, -1 on failure
1075 the resulting file descriptors are symmetrical
1076 ******************************************************************/
1077 static int socketpair_tcp(int fd[2])
1079 int listener;
1080 struct sockaddr_in sock;
1081 struct sockaddr_in sock2;
1082 socklen_t socklen = sizeof(sock);
1083 int connect_done = 0;
1085 fd[0] = fd[1] = listener = -1;
1087 memset(&sock, 0, sizeof(sock));
1089 if ((listener = socket(PF_INET, SOCK_STREAM, 0)) == -1) goto failed;
1091 memset(&sock2, 0, sizeof(sock2));
1092 #ifdef HAVE_SOCK_SIN_LEN
1093 sock2.sin_len = sizeof(sock2);
1094 #endif
1095 sock2.sin_family = PF_INET;
1097 bind(listener, (struct sockaddr *)&sock2, sizeof(sock2));
1099 if (listen(listener, 1) != 0) goto failed;
1101 if (getsockname(listener, (struct sockaddr *)&sock, &socklen) != 0) goto failed;
1103 if ((fd[1] = socket(PF_INET, SOCK_STREAM, 0)) == -1) goto failed;
1105 set_blocking(fd[1], 0);
1107 sock.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
1109 if (connect(fd[1],(struct sockaddr *)&sock,sizeof(sock)) == -1) {
1110 if (errno != EINPROGRESS) goto failed;
1111 } else {
1112 connect_done = 1;
1115 if ((fd[0] = accept(listener, (struct sockaddr *)&sock, &socklen)) == -1) goto failed;
1117 close(listener);
1118 if (connect_done == 0) {
1119 if (connect(fd[1],(struct sockaddr *)&sock,sizeof(sock)) != 0
1120 && errno != EISCONN) goto failed;
1123 set_blocking(fd[1], 1);
1125 /* all OK! */
1126 return 0;
1128 failed:
1129 if (fd[0] != -1) close(fd[0]);
1130 if (fd[1] != -1) close(fd[1]);
1131 if (listener != -1) close(listener);
1132 return -1;
1136 /*******************************************************************
1137 run a program on a local tcp socket, this is used to launch smbd
1138 when regression testing
1139 the return value is a socket which is attached to a subprocess
1140 running "prog". stdin and stdout are attached. stderr is left
1141 attached to the original stderr
1142 ******************************************************************/
1143 int sock_exec(const char *prog)
1145 int fd[2];
1146 if (socketpair_tcp(fd) != 0) {
1147 DEBUG(0,("socketpair_tcp failed (%s)\n", strerror(errno)));
1148 return -1;
1150 if (fork() == 0) {
1151 close(fd[0]);
1152 close(0);
1153 close(1);
1154 dup(fd[1]);
1155 dup(fd[1]);
1156 exit(system(prog));
1158 close(fd[1]);
1159 return fd[0];