- got fed up of vuser_db spewing 150 lines of trash, made it possible
[Samba.git] / source / lib / util_sock.c
blob3a789ed533a5e0ae0405ca1bfacea4aa93b34286
1 /*
2 Unix SMB/Netbios implementation.
3 Version 1.9.
4 Samba utility functions
5 Copyright (C) Andrew Tridgell 1992-1998
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 #ifdef WITH_SSL
25 #include <ssl.h>
26 #undef Realloc /* SSLeay defines this and samba has a function of this name */
27 extern SSL *ssl;
28 extern int sslFd;
29 #endif /* WITH_SSL */
31 extern int DEBUGLEVEL;
33 BOOL passive = False;
35 /* the port, where client connected */
36 int ClientPort = 0;
38 /* the last IP received from */
39 struct in_addr lastip;
41 /* the last port received from */
42 int lastport = 0;
45 int smb_read_error = 0;
48 /****************************************************************************
49 Determine if a file descriptor is in fact a socket.
50 ****************************************************************************/
51 BOOL is_a_socket(int fd)
53 int v, l;
54 l = sizeof(int);
55 return (getsockopt(fd, SOL_SOCKET, SO_TYPE, (char *)&v, &l) == 0);
59 enum SOCK_OPT_TYPES
60 { OPT_BOOL, OPT_INT, OPT_ON };
62 struct
64 char *name;
65 int level;
66 int option;
67 int value;
68 int opttype;
70 socket_options[] =
73 "SO_KEEPALIVE", SOL_SOCKET, SO_KEEPALIVE, 0, OPT_BOOL}
76 "SO_REUSEADDR", SOL_SOCKET, SO_REUSEADDR, 0, OPT_BOOL}
79 "SO_BROADCAST", SOL_SOCKET, SO_BROADCAST, 0, OPT_BOOL}
81 #ifdef TCP_NODELAY
83 "TCP_NODELAY", IPPROTO_TCP, TCP_NODELAY, 0, OPT_BOOL}
85 #endif
86 #ifdef IPTOS_LOWDELAY
88 "IPTOS_LOWDELAY", IPPROTO_IP, IP_TOS, IPTOS_LOWDELAY, OPT_ON}
90 #endif
91 #ifdef IPTOS_THROUGHPUT
93 "IPTOS_THROUGHPUT", IPPROTO_IP, IP_TOS, IPTOS_THROUGHPUT,
94 OPT_ON}
96 #endif
97 #ifdef SO_REUSEPORT
99 "SO_REUSEPORT", SOL_SOCKET, SO_REUSEPORT, 0, OPT_BOOL}
101 #endif
102 #ifdef SO_SNDBUF
104 "SO_SNDBUF", SOL_SOCKET, SO_SNDBUF, 0, OPT_INT}
106 #endif
107 #ifdef SO_RCVBUF
109 "SO_RCVBUF", SOL_SOCKET, SO_RCVBUF, 0, OPT_INT}
111 #endif
112 #ifdef SO_SNDLOWAT
114 "SO_SNDLOWAT", SOL_SOCKET, SO_SNDLOWAT, 0, OPT_INT}
116 #endif
117 #ifdef SO_RCVLOWAT
119 "SO_RCVLOWAT", SOL_SOCKET, SO_RCVLOWAT, 0, OPT_INT}
121 #endif
122 #ifdef SO_SNDTIMEO
124 "SO_SNDTIMEO", SOL_SOCKET, SO_SNDTIMEO, 0, OPT_INT}
126 #endif
127 #ifdef SO_RCVTIMEO
129 "SO_RCVTIMEO", SOL_SOCKET, SO_RCVTIMEO, 0, OPT_INT}
131 #endif
133 NULL, 0, 0, 0, 0}
138 /****************************************************************************
139 Set user socket options.
140 ****************************************************************************/
141 void set_socket_options(int fd, char *options)
143 fstring tok;
145 while (next_token(&options, tok, " \t,", sizeof(tok)))
147 int ret = 0, i;
148 int value = 1;
149 char *p;
150 BOOL got_value = False;
152 if ((p = strchr(tok, '=')))
154 *p = 0;
155 value = atoi(p + 1);
156 got_value = True;
159 for (i = 0; socket_options[i].name; i++)
160 if (strequal(socket_options[i].name, tok))
161 break;
163 if (!socket_options[i].name)
165 DEBUG(0, ("Unknown socket option %s\n", tok));
166 continue;
169 switch (socket_options[i].opttype)
171 case OPT_BOOL:
172 case OPT_INT:
173 ret = setsockopt(fd, socket_options[i].level,
174 socket_options[i].option,
175 (char *)&value, sizeof(int));
176 break;
178 case OPT_ON:
179 if (got_value)
180 DEBUG(0,
181 ("syntax error - %s does not take a value\n",
182 tok));
185 int on = socket_options[i].value;
186 ret =
187 setsockopt(fd,
188 socket_options
189 [i].level,
190 socket_options
191 [i].option,
192 (char *)&on,
193 sizeof(int));
195 break;
198 if (ret != 0)
199 DEBUG(0,
200 ("Failed to set socket option %s (Error %s)\n",
201 tok, strerror(errno)));
207 /****************************************************************************
208 Read from a socket.
209 ****************************************************************************/
212 /*******************************************************************
213 checks if write data is outstanding.
214 ********************************************************************/
215 int write_data_outstanding(int fd, unsigned int time_out, BOOL *more)
217 int selrtn;
218 fd_set fds;
219 struct timeval timeout;
221 FD_ZERO(&fds);
222 FD_SET(fd, &fds);
224 timeout.tv_sec = (time_t) (time_out / 1000);
225 timeout.tv_usec = (long)(1000 * (time_out % 1000));
227 selrtn = sys_select(fd + 1, NULL, &fds, &timeout);
229 if (selrtn <= 0)
231 return selrtn;
233 (*more) = FD_ISSET(fd, &fds);
234 return selrtn;
237 /*******************************************************************
238 checks if read data is outstanding.
239 ********************************************************************/
240 int read_data_outstanding(int fd, unsigned int time_out)
242 int selrtn;
243 fd_set fds;
244 struct timeval timeout;
246 FD_ZERO(&fds);
247 FD_SET(fd, &fds);
249 timeout.tv_sec = (time_t) (time_out / 1000);
250 timeout.tv_usec = (long)(1000 * (time_out % 1000));
252 selrtn = sys_select(fd + 1, &fds, NULL, &timeout);
254 if (selrtn <= 0)
256 return selrtn;
258 return FD_ISSET(fd, &fds) ? 1 : 0;
261 /****************************************************************************
262 read from a socket
263 ****************************************************************************/
264 ssize_t read_udp_socket(int fd, char *buf, size_t len)
266 ssize_t ret;
267 struct sockaddr_in sock;
268 int socklen;
270 socklen = sizeof(sock);
271 memset((char *)&sock, 0, socklen);
272 ZERO_STRUCT(lastip);
273 ret =
274 (ssize_t) recvfrom(fd, buf, len, 0, (struct sockaddr *)&sock,
275 &socklen);
276 if (ret <= 0)
278 DEBUG(2, ("read socket failed. ERRNO=%s\n", strerror(errno)));
279 return (0);
282 lastip = sock.sin_addr;
283 lastport = ntohs(sock.sin_port);
285 DEBUG(10, ("read_udp_socket: lastip %s lastport %d read: %d\n",
286 inet_ntoa(lastip), lastport, ret));
288 return (ret);
291 /****************************************************************************
292 Read data from a socket with a timout in msec.
293 mincount = if timeout, minimum to read before returning
294 maxcount = number to be read.
295 time_out = timeout in milliseconds
296 ****************************************************************************/
298 ssize_t read_socket_with_timeout(int fd, char *buf, size_t mincnt,
299 size_t maxcnt, unsigned int time_out)
301 ssize_t readret;
302 size_t nread = 0;
304 /* just checking .... */
305 if (maxcnt <= 0)
306 return (0);
308 smb_read_error = 0;
310 /* Blocking read */
311 if (time_out <= 0)
313 if (mincnt == 0)
314 mincnt = maxcnt;
316 while (nread < mincnt)
318 #ifdef WITH_SSL
319 if (fd == sslFd)
321 readret =
322 SSL_read(ssl, buf + nread,
323 maxcnt - nread);
325 else
327 readret =
328 read(fd, buf + nread, maxcnt - nread);
330 #else /* WITH_SSL */
331 readret = read(fd, buf + nread, maxcnt - nread);
332 #endif /* WITH_SSL */
334 if (readret == 0)
336 DEBUG(5,
337 ("read_socket_with_timeout: blocking read. EOF from client.\n"));
338 smb_read_error = READ_EOF;
339 return -1;
342 if (readret == -1)
344 DEBUG(0,
345 ("read_socket_with_timeout: read error = %s.\n",
346 strerror(errno)));
347 smb_read_error = READ_ERROR;
348 return -1;
350 nread += readret;
352 return ((ssize_t) nread);
355 /* Most difficult - timeout read */
356 /* If this is ever called on a disk file and
357 mincnt is greater then the filesize then
358 system performance will suffer severely as
359 select always returns true on disk files */
361 for (nread = 0; nread < mincnt;)
363 int selrtn = read_data_outstanding(fd, time_out);
365 /* Check if error */
366 if (selrtn == -1)
368 /* something is wrong. Maybe the socket is dead? */
369 DEBUG(0,
370 ("read_socket_with_timeout: timeout read. select error = %s.\n",
371 strerror(errno)));
372 smb_read_error = READ_ERROR;
373 return -1;
376 /* Did we timeout ? */
377 if (selrtn == 0)
379 DEBUG(10,
380 ("read_socket_with_timeout: timeout read. select timed out.\n"));
381 smb_read_error = READ_TIMEOUT;
382 return -1;
385 #ifdef WITH_SSL
386 if (fd == sslFd)
388 readret = SSL_read(ssl, buf + nread, maxcnt - nread);
390 else
392 readret = read(fd, buf + nread, maxcnt - nread);
394 #else /* WITH_SSL */
395 readret = read(fd, buf + nread, maxcnt - nread);
396 #endif /* WITH_SSL */
398 if (readret == 0)
400 /* we got EOF on the file descriptor */
401 DEBUG(5,
402 ("read_socket_with_timeout: timeout read. EOF from client.\n"));
403 smb_read_error = READ_EOF;
404 return -1;
407 if (readret == -1)
409 /* the descriptor is probably dead */
410 DEBUG(0,
411 ("read_socket_with_timeout: timeout read. read error = %s.\n",
412 strerror(errno)));
413 smb_read_error = READ_ERROR;
414 return -1;
416 nread += readret;
419 /* Return the number we got */
420 return ((ssize_t) nread);
423 /****************************************************************************
424 Read data from a fd with a timout in msec.
425 mincount = if timeout, minimum to read before returning
426 maxcount = number to be read.
427 time_out = timeout in milliseconds
428 ****************************************************************************/
430 ssize_t read_with_timeout(int fd, char *buf, size_t mincnt, size_t maxcnt,
431 unsigned int time_out)
433 ssize_t readret;
434 size_t nread = 0;
436 /* just checking .... */
437 if (maxcnt <= 0)
438 return (0);
440 /* Blocking read */
441 if (time_out <= 0)
443 if (mincnt == 0)
444 mincnt = maxcnt;
446 while (nread < mincnt)
448 #ifdef WITH_SSL
449 if (fd == sslFd)
451 readret =
452 SSL_read(ssl, buf + nread,
453 maxcnt - nread);
455 else
457 readret =
458 read(fd, buf + nread, maxcnt - nread);
460 #else /* WITH_SSL */
461 readret = read(fd, buf + nread, maxcnt - nread);
462 #endif /* WITH_SSL */
464 if (readret <= 0)
465 return readret;
467 nread += readret;
469 return ((ssize_t) nread);
472 /* Most difficult - timeout read */
473 /* If this is ever called on a disk file and
474 mincnt is greater then the filesize then
475 system performance will suffer severely as
476 select always returns true on disk files */
478 for (nread = 0; nread < mincnt;)
480 int selrtn = read_data_outstanding(fd, time_out);
482 if (selrtn <= 0)
483 return selrtn;
485 #ifdef WITH_SSL
486 if (fd == sslFd)
488 readret = SSL_read(ssl, buf + nread, maxcnt - nread);
490 else
492 readret = read(fd, buf + nread, maxcnt - nread);
494 #else /* WITH_SSL */
495 readret = read(fd, buf + nread, maxcnt - nread);
496 #endif /* WITH_SSL */
498 if (readret <= 0)
499 return readret;
501 nread += readret;
504 /* Return the number we got */
505 return ((ssize_t) nread);
508 /****************************************************************************
509 send a keepalive packet (rfc1002)
510 ****************************************************************************/
511 BOOL send_keepalive(int client)
513 unsigned char buf[4];
515 buf[0] = 0x85;
516 buf[1] = buf[2] = buf[3] = 0;
518 return (write_socket_data(client, (char *)buf, 4) == 4);
523 /****************************************************************************
524 read data from the client, reading exactly N bytes.
525 ****************************************************************************/
526 ssize_t read_data(int fd, char *buffer, size_t N)
528 ssize_t ret;
529 size_t total = 0;
531 smb_read_error = 0;
533 while (total < N)
535 #ifdef WITH_SSL
536 if (fd == sslFd)
538 ret = SSL_read(ssl, buffer + total, N - total);
540 else
542 ret = read(fd, buffer + total, N - total);
544 #else /* WITH_SSL */
545 ret = read(fd, buffer + total, N - total);
546 #endif /* WITH_SSL */
548 if (ret == 0)
550 DEBUG(10,
551 ("read_data: read of %d returned 0. Error = %s\n",
552 (int)(N - total), strerror(errno)));
553 smb_read_error = READ_EOF;
554 return 0;
556 if (ret == -1)
558 DEBUG(0,
559 ("read_data: read failure for %d. Error = %s\n",
560 (int)(N - total), strerror(errno)));
561 smb_read_error = READ_ERROR;
562 return -1;
564 total += ret;
566 return (ssize_t) total;
569 /****************************************************************************
570 Read data from a socket, reading exactly N bytes.
571 ****************************************************************************/
573 static ssize_t read_socket_data(int fd, char *buffer, size_t N)
575 ssize_t ret;
576 size_t total = 0;
578 smb_read_error = 0;
580 while (total < N)
582 #ifdef WITH_SSL
583 if (fd == sslFd)
585 ret = SSL_read(ssl, buffer + total, N - total);
587 else
589 ret = read(fd, buffer + total, N - total);
591 #else /* WITH_SSL */
592 ret = read(fd, buffer + total, N - total);
593 #endif /* WITH_SSL */
595 if (ret == 0)
597 DEBUG(10,
598 ("read_socket_data: recv of %d returned 0. Error = %s\n",
599 (int)(N - total), strerror(errno)));
600 smb_read_error = READ_EOF;
601 return 0;
603 if (ret == -1)
605 DEBUG(0,
606 ("read_socket_data: recv failure for %d. Error = %s\n",
607 (int)(N - total), strerror(errno)));
608 smb_read_error = READ_ERROR;
609 return -1;
611 total += ret;
613 return (ssize_t) total;
616 /****************************************************************************
617 Write data to a fd.
618 ****************************************************************************/
619 ssize_t write_data(int fd, char *buffer, size_t N)
621 size_t total = 0;
622 ssize_t ret;
624 while (total < N)
626 #ifdef WITH_SSL
627 if (fd == sslFd)
629 ret = SSL_write(ssl, buffer + total, N - total);
631 else
633 ret = write(fd, buffer + total, N - total);
635 #else /* WITH_SSL */
636 ret = write(fd, buffer + total, N - total);
637 #endif /* WITH_SSL */
639 if (ret == -1)
641 DEBUG(0,
642 ("write_data: write failure. Error = %s\n",
643 strerror(errno)));
644 return -1;
646 if (ret == 0)
647 return total;
649 total += ret;
651 return (ssize_t) total;
654 /****************************************************************************
655 Write data to a socket - use send rather than write.
656 ****************************************************************************/
658 ssize_t write_socket_data(int fd, char *buffer, size_t N)
660 size_t total = 0;
661 ssize_t ret;
663 while (total < N)
665 #ifdef WITH_SSL
666 if (fd == sslFd)
668 ret = SSL_write(ssl, buffer + total, N - total);
670 else
672 ret = send(fd, buffer + total, N - total, 0);
674 #else /* WITH_SSL */
675 ret = send(fd, buffer + total, N - total, 0);
676 #endif /* WITH_SSL */
678 if (ret == -1)
680 DEBUG(0,
681 ("write_socket_data: write failure. Error = %s\n",
682 strerror(errno)));
683 return -1;
685 if (ret == 0)
686 return total;
688 total += ret;
690 return (ssize_t) total;
693 /****************************************************************************
694 write to a socket
695 ****************************************************************************/
696 ssize_t write_socket(int fd, char *buf, size_t len)
698 ssize_t ret = 0;
700 if (passive)
701 return (len);
702 DEBUG(6, ("write_socket(%d,%d)\n", fd, (int)len));
703 ret = write_socket_data(fd, buf, len);
705 DEBUG(6, ("write_socket(%d,%d) wrote %d\n", fd, (int)len, (int)ret));
706 if (ret <= 0)
707 DEBUG(0,
708 ("write_socket: Error writing %d bytes to socket %d: ERRNO = %s\n",
709 (int)len, fd, strerror(errno)));
711 return (ret);
715 /****************************************************************************
716 read 4 bytes of a smb packet and return the smb length of the packet
717 store the result in the buffer
718 This version of the function will return a length of zero on receiving
719 a keepalive packet.
720 timeout is in milliseconds.
721 ****************************************************************************/
722 static ssize_t read_smb_length_return_keepalive(int fd, char *inbuf,
723 unsigned int timeout)
725 ssize_t len = 0;
726 int msg_type;
727 BOOL ok = False;
729 while (!ok)
731 if (timeout > 0)
732 ok =
733 (read_socket_with_timeout
734 (fd, inbuf, 4, 4, timeout) == 4);
735 else
736 ok = (read_socket_data(fd, inbuf, 4) == 4);
738 if (!ok)
739 return (-1);
741 len = smb_len(inbuf);
742 msg_type = CVAL(inbuf, 0);
744 if (msg_type == 0x85)
745 DEBUG(5, ("Got keepalive packet\n"));
748 DEBUG(10, ("got smb length of %d\n", len));
750 return (len);
753 /****************************************************************************
754 read 4 bytes of a smb packet and return the smb length of the packet
755 store the result in the buffer. This version of the function will
756 never return a session keepalive (length of zero).
757 timeout is in milliseconds.
758 ****************************************************************************/
759 ssize_t read_smb_length(int fd, char *inbuf, unsigned int timeout)
761 ssize_t len;
763 for (;;)
765 len = read_smb_length_return_keepalive(fd, inbuf, timeout);
767 if (len < 0)
768 return len;
770 /* Ignore session keepalives. */
771 if (CVAL(inbuf, 0) != 0x85)
772 break;
775 DEBUG(10, ("read_smb_length: got smb length of %d\n", len));
777 return len;
780 /****************************************************************************
781 read an smb from a fd. Note that the buffer *MUST* be of size
782 BUFFER_SIZE+SAFETY_MARGIN.
783 The timeout is in milliseconds.
784 This function will return on a
785 receipt of a session keepalive packet.
786 ****************************************************************************/
787 BOOL receive_smb(int fd, char *buffer, unsigned int timeout)
789 ssize_t len, ret;
791 smb_read_error = 0;
793 if (buffer == NULL)
795 DEBUG(1, ("receive_smb: buffer==NULL\n"));
796 return False;
799 memset(buffer, 0, smb_size + 100);
801 len = read_smb_length_return_keepalive(fd, buffer, timeout);
802 if (len < 0)
804 DEBUG(10, ("receive_smb: length < 0!\n"));
805 return (False);
808 if (len > BUFFER_SIZE)
810 DEBUG(0, ("Invalid packet length! (%d bytes).\n", len));
811 if (len > BUFFER_SIZE + (SAFETY_MARGIN / 2))
813 exit(1);
817 if (len > 0)
819 ret = read_socket_data(fd, buffer + 4, len);
820 if (ret != len)
822 smb_read_error = READ_ERROR;
823 return False;
826 return (True);
829 /****************************************************************************
830 read an smb from a fd ignoring all keepalive packets. Note that the buffer
831 *MUST* be of size BUFFER_SIZE+SAFETY_MARGIN.
832 The timeout is in milliseconds
834 This is exactly the same as receive_smb except that it never returns
835 a session keepalive packet (just as receive_smb used to do).
836 receive_smb was changed to return keepalives as the oplock processing means this call
837 should never go into a blocking read.
838 ****************************************************************************/
840 BOOL client_receive_smb(int fd, char *buffer, unsigned int timeout)
842 BOOL ret;
844 for (;;)
846 ret = receive_smb(fd, buffer, timeout);
848 if (!ret)
850 DEBUG(10, ("client_receive_smb failed\n"));
851 show_msg(buffer);
852 return ret;
855 /* Ignore session keepalive packets. */
856 if (CVAL(buffer, 0) != 0x85)
857 break;
859 show_msg(buffer);
860 return ret;
863 /****************************************************************************
864 send an null session message to a fd
865 ****************************************************************************/
867 BOOL send_null_session_msg(int fd)
869 ssize_t ret;
870 uint32 blank = 0;
871 size_t len = 4;
872 size_t nwritten = 0;
873 char *buffer = (char *)&blank;
875 while (nwritten < len)
877 ret = write_socket(fd, buffer + nwritten, len - nwritten);
878 if (ret <= 0)
880 DEBUG(0,
881 ("send_null_session_msg: Error writing %d bytes to client. %d. Exiting\n",
882 (int)len, (int)ret));
883 exit(1);
885 nwritten += ret;
888 DEBUG(10, ("send_null_session_msg: sent 4 null bytes to client.\n"));
889 return True;
892 /****************************************************************************
893 send an smb to a fd
894 ****************************************************************************/
895 BOOL send_smb(int fd, char *buffer)
897 size_t len;
898 size_t nwritten = 0;
899 ssize_t ret;
900 len = smb_len(buffer) + 4;
902 while (nwritten < len)
904 ret = write_socket(fd, buffer + nwritten, len - nwritten);
905 if (ret <= 0)
907 DEBUG(0,
908 ("Error writing %d bytes to client. %d. Exiting\n",
909 (int)len, (int)ret));
910 exit(1);
912 nwritten += ret;
915 return True;
920 /****************************************************************************
921 send a single packet to a port on another machine
922 ****************************************************************************/
923 BOOL send_one_packet(char *buf, int len, struct in_addr ip, int port,
924 int type)
926 BOOL ret;
927 int out_fd;
928 struct sockaddr_in sock_out;
930 if (passive)
931 return (True);
933 /* create a socket to write to */
934 out_fd = socket(AF_INET, type, 0);
935 if (out_fd == -1)
937 DEBUG(0, ("socket failed"));
938 return False;
941 /* set the address and port */
942 ZERO_STRUCT(sock_out);
943 putip((char *)&sock_out.sin_addr, (char *)&ip);
944 sock_out.sin_port = htons(port);
945 sock_out.sin_family = AF_INET;
947 if (DEBUGLEVEL > 0)
948 DEBUG(3,
949 ("sending a packet of len %d to (%s) on port %d of type %s\n",
950 len, inet_ntoa(ip), port,
951 type == SOCK_DGRAM ? "DGRAM" : "STREAM"));
953 /* send it */
954 ret =
955 (sendto
956 (out_fd, buf, len, 0, (struct sockaddr *)&sock_out,
957 sizeof(sock_out)) >= 0);
959 if (!ret)
960 DEBUG(0, ("Packet send to %s(%d) failed ERRNO=%s\n",
961 inet_ntoa(ip), port, strerror(errno)));
963 close(out_fd);
964 return (ret);
968 /****************************************************************************
969 open a socket of the specified type, port and address for incoming data
970 ****************************************************************************/
971 int open_socket_in(int type, int port, int dlevel, uint32 socket_addr,
972 BOOL rebind)
974 struct hostent *hp;
975 struct sockaddr_in sock;
976 pstring host_name;
977 int res;
979 /* get my host name */
980 if (gethostname(host_name, MAXHOSTNAMELEN) == -1)
982 DEBUG(0, ("gethostname failed\n"));
983 return -1;
986 /* get host info */
987 if ((hp = Get_Hostbyname(host_name)) == 0)
989 DEBUG(0, ("Get_Hostbyname: Unknown host %s\n", host_name));
990 return -1;
993 ZERO_STRUCT(sock);
994 memcpy((char *)&sock.sin_addr, (char *)hp->h_addr, hp->h_length);
996 #ifdef HAVE_SOCK_SIN_LEN
997 sock.sin_len = sizeof(sock);
998 #endif
999 sock.sin_port = htons(port);
1000 sock.sin_family = hp->h_addrtype;
1001 sock.sin_addr.s_addr = socket_addr;
1002 res = socket(hp->h_addrtype, type, 0);
1003 if (res == -1)
1005 DEBUG(0, ("socket failed\n"));
1006 return -1;
1010 int val = 1;
1011 if (rebind)
1012 val = 1;
1013 else
1014 val = 0;
1015 if (setsockopt
1016 (res, SOL_SOCKET, SO_REUSEADDR, (char *)&val,
1017 sizeof(val)) == -1)
1018 DEBUG(dlevel,
1019 ("setsockopt: SO_REUSEADDR=%d on port %d failed with error = %s\n",
1020 val, port, strerror(errno)));
1021 #ifdef SO_REUSEPORT
1022 if (setsockopt
1023 (res, SOL_SOCKET, SO_REUSEPORT, (char *)&val,
1024 sizeof(val)) == -1)
1025 DEBUG(dlevel,
1026 ("setsockopt: SO_REUSEPORT=%d on port %d failed with error = %s\n",
1027 val, port, strerror(errno)));
1028 #endif /* SO_REUSEPORT */
1031 /* now we've got a socket - we need to bind it */
1032 if (bind(res, (struct sockaddr *)&sock, sizeof(sock)) < 0)
1034 if (port)
1036 if (port == SMB_PORT || port == NMB_PORT)
1037 DEBUG(dlevel,
1038 ("bind failed on port %d socket_addr=%s (%s)\n",
1039 port, inet_ntoa(sock.sin_addr),
1040 strerror(errno)));
1041 close(res);
1043 if (dlevel > 0 && port < 1000)
1044 port = 7999;
1046 if (port >= 1000 && port < 9000)
1047 return (open_socket_in
1048 (type, port + 1, dlevel, socket_addr,
1049 rebind));
1052 return (-1);
1054 DEBUG(3, ("bind succeeded on port %d\n", port));
1056 return res;
1060 /****************************************************************************
1061 create an outgoing socket. timeout is in milliseconds.
1062 **************************************************************************/
1063 int open_socket_out(int type, struct in_addr *addr, int port, int timeout)
1065 struct sockaddr_in sock_out;
1066 int res, ret;
1067 int connect_loop = 250; /* 250 milliseconds */
1068 int loops = (timeout) / connect_loop;
1070 /* create a socket to write to */
1071 res = socket(PF_INET, type, 0);
1072 if (res == -1)
1074 DEBUG(0, ("socket error\n"));
1075 return -1;
1078 if (type != SOCK_STREAM)
1079 return (res);
1081 ZERO_STRUCT(sock_out);
1082 putip((char *)&sock_out.sin_addr, (char *)addr);
1084 sock_out.sin_port = htons(port);
1085 sock_out.sin_family = PF_INET;
1087 /* set it non-blocking */
1088 set_blocking(res, False);
1090 DEBUG(3, ("Connecting to %s at port %d\n", inet_ntoa(*addr), port));
1092 /* and connect it to the destination */
1093 connect_again:
1094 ret = connect(res, (struct sockaddr *)&sock_out, sizeof(sock_out));
1096 /* Some systems return EAGAIN when they mean EINPROGRESS */
1097 if (ret < 0 && (errno == EINPROGRESS || errno == EALREADY ||
1098 errno == EAGAIN) && loops--)
1100 msleep(connect_loop);
1101 goto connect_again;
1104 if (ret < 0 && (errno == EINPROGRESS || errno == EALREADY ||
1105 errno == EAGAIN))
1107 DEBUG(1,
1108 ("timeout connecting to %s:%d\n", inet_ntoa(*addr),
1109 port));
1110 close(res);
1111 return -1;
1114 #ifdef EISCONN
1115 if (ret < 0 && errno == EISCONN)
1117 errno = 0;
1118 ret = 0;
1120 #endif
1122 if (ret < 0)
1124 DEBUG(1, ("error connecting to %s:%d (%s)\n",
1125 inet_ntoa(*addr), port, strerror(errno)));
1126 close(res);
1127 return -1;
1130 /* set it blocking again */
1131 set_blocking(res, True);
1133 return res;
1137 /*******************************************************************
1138 Reset the 'done' variables so after a client process is created
1139 from a fork call these calls will be re-done. This should be
1140 expanded if more variables need reseting.
1141 ******************************************************************/
1144 void reset_globals_after_fork(void)
1147 * Re-seed the random crypto generator, so all smbd's
1148 * started from the same parent won't generate the same
1149 * sequence.
1152 unsigned char dummy;
1153 generate_random_buffer(&dummy, 1, True);
1157 /* the following 3 client_*() functions are nasty ways of allowing
1158 some generic functions to get info that really should be hidden in
1159 particular modules */
1160 static int client_fd = -1;
1162 void client_setfd(int fd)
1164 client_fd = fd;
1167 char *client_name(void)
1169 return get_socket_name(client_fd);
1172 char *client_addr(void)
1174 return get_socket_addr(client_fd);
1177 /*******************************************************************
1178 matchname - determine if host name matches IP address. Used to
1179 confirm a hostname lookup to prevent spoof attacks
1180 ******************************************************************/
1181 static BOOL matchname(char *remotehost, struct in_addr addr)
1183 struct hostent *hp;
1184 int i;
1186 if ((hp = Get_Hostbyname(remotehost)) == 0)
1188 DEBUG(0,
1189 ("Get_Hostbyname(%s): lookup failure.\n", remotehost));
1190 return False;
1194 * Make sure that gethostbyname() returns the "correct" host name.
1195 * Unfortunately, gethostbyname("localhost") sometimes yields
1196 * "localhost.domain". Since the latter host name comes from the
1197 * local DNS, we just have to trust it (all bets are off if the local
1198 * DNS is perverted). We always check the address list, though.
1201 if (strcasecmp(remotehost, hp->h_name)
1202 && strcasecmp(remotehost, "localhost"))
1204 DEBUG(0, ("host name/name mismatch: %s != %s\n",
1205 remotehost, hp->h_name));
1206 return False;
1209 /* Look up the host address in the address list we just got. */
1210 for (i = 0; hp->h_addr_list[i]; i++)
1212 if (memcmp(hp->h_addr_list[i], (caddr_t) & addr, sizeof(addr))
1213 == 0)
1214 return True;
1218 * The host name does not map to the original host address. Perhaps
1219 * someone has compromised a name server. More likely someone botched
1220 * it, but that could be dangerous, too.
1223 DEBUG(0, ("host name/address mismatch: %s != %s\n",
1224 inet_ntoa(addr), hp->h_name));
1225 return False;
1228 /*******************************************************************
1229 return the DNS name of the remote end of a socket
1230 ******************************************************************/
1231 char *get_socket_name(int fd)
1233 static pstring name_buf;
1234 static fstring addr_buf;
1235 struct hostent *hp;
1236 struct in_addr addr;
1237 char *p;
1239 p = get_socket_addr(fd);
1241 /* it might be the same as the last one - save some DNS work */
1242 if (strcmp(p, addr_buf) == 0)
1243 return name_buf;
1245 pstrcpy(name_buf, "UNKNOWN");
1246 if (fd == -1)
1247 return name_buf;
1249 fstrcpy(addr_buf, p);
1251 if (inet_aton(p, &addr) == 0)
1252 return name_buf;
1254 /* Look up the remote host name. */
1255 if (
1256 (hp =
1257 gethostbyaddr((char *)&addr.s_addr, sizeof(addr.s_addr),
1258 AF_INET)) == 0)
1260 DEBUG(1, ("Gethostbyaddr failed for %s\n", p));
1261 pstrcpy(name_buf, p);
1263 else
1265 pstrcpy(name_buf, (char *)hp->h_name);
1266 if (!matchname(name_buf, addr))
1268 DEBUG(0,
1269 ("Matchname failed on %s %s\n", name_buf, p));
1270 pstrcpy(name_buf, "UNKNOWN");
1273 return name_buf;
1276 /*******************************************************************
1277 return the IP addr of the remote end of a socket as a string
1278 ******************************************************************/
1279 char *get_socket_addr(int fd)
1281 struct sockaddr sa;
1282 struct sockaddr_in *sockin = (struct sockaddr_in *)(&sa);
1283 int length = sizeof(sa);
1284 static fstring addr_buf;
1286 fstrcpy(addr_buf, "0.0.0.0");
1288 if (fd == -1)
1290 return addr_buf;
1293 if (getpeername(fd, &sa, &length) < 0)
1295 DEBUG(0,
1296 ("getpeername failed. Error was %s\n",
1297 strerror(errno)));
1298 return addr_buf;
1301 fstrcpy(addr_buf, (char *)inet_ntoa(sockin->sin_addr));
1303 return addr_buf;
1306 /*******************************************************************
1307 opens and connects to a unix pipe socket
1308 ******************************************************************/
1309 int open_pipe_sock(char *path)
1311 int sock;
1312 struct sockaddr_un sa;
1314 sock = socket(AF_UNIX, SOCK_STREAM, 0);
1316 if (sock < 0)
1318 DEBUG(0, ("unix socket open failed\n"));
1319 return sock;
1322 ZERO_STRUCT(sa);
1323 sa.sun_family = AF_UNIX;
1324 safe_strcpy(sa.sun_path, path, sizeof(sa.sun_path) - 1);
1326 DEBUG(10, ("socket open succeeded. file name: %s\n", sa.sun_path));
1328 if (connect(sock, (struct sockaddr *)&sa, sizeof(sa)) < 0)
1330 DEBUG(2, ("socket connect to %s failed: %s\n",
1331 sa.sun_path, strerror(errno)));
1332 close(sock);
1333 return -1;
1336 return sock;
1339 int create_pipe_socket(char *dir, int dir_perms, char *path, int path_perms)
1341 int s;
1342 struct sockaddr_un sa;
1344 DEBUG(0, ("create_pipe_socket: %s perms=%d %s perms=%d\n",
1345 dir, dir_perms, path, path_perms));
1347 DEBUG(0,
1348 ("*** Please someone examine create_pipe_socket and fix it ***\n"));
1349 DEBUG(0, ("*** if used other than for exclusive root access ***\n"));
1350 DEBUG(0, ("*** (see perms, which should be 0700 and 0600) ***\n"));
1351 DEBUG(0, ("*** there is a race condition to be exploited. ***\n"));
1353 mkdir(dir, dir_perms);
1355 if (chmod(dir, dir_perms) < 0)
1357 DEBUG(0, ("chmod on %s failed\n", dir));
1358 return -1;
1361 if (!remove(path))
1363 DEBUG(0, ("remove on %s failed\n", path));
1366 /* start listening on unix socket */
1367 s = socket(AF_UNIX, SOCK_STREAM, 0);
1369 if (s < 0)
1371 DEBUG(0, ("socket open failed\n"));
1372 return -1;
1375 ZERO_STRUCT(sa);
1376 sa.sun_family = AF_UNIX;
1377 safe_strcpy(sa.sun_path, path, sizeof(sa.sun_path) - 1);
1379 if (bind(s, (struct sockaddr *)&sa, sizeof(sa)) < 0)
1381 DEBUG(0, ("socket bind to %s failed\n", sa.sun_path));
1382 close(s);
1383 remove(path);
1384 return -1;
1387 if (s == -1)
1389 DEBUG(0, ("bind failed\n"));
1390 remove(path);
1391 return -1;
1394 if (path_perms != 0)
1396 chmod(path, path_perms);
1399 if (listen(s, 5) == -1)
1401 DEBUG(0, ("listen failed\n"));
1402 return -1;
1405 DEBUG(5, ("unix socket opened: %s\n", path));
1407 return s;