2 Unix SMB/Netbios implementation.
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.
26 #undef Realloc /* SSLeay defines this and samba has a function of this name */
31 extern int DEBUGLEVEL;
35 /* the client file descriptor */
38 /* the port, where client connected */
41 /* the last IP received from */
42 struct in_addr lastip;
44 /* the last port received from */
48 int smb_read_error = 0;
51 /****************************************************************************
52 determine if a file descriptor is in fact a socket
53 ****************************************************************************/
54 BOOL is_a_socket(int fd)
58 return(getsockopt(fd, SOL_SOCKET, SO_TYPE, (char *)&v, &l) == 0);
62 enum SOCK_OPT_TYPES {OPT_BOOL,OPT_INT,OPT_ON};
71 } socket_options[] = {
72 {"SO_KEEPALIVE", SOL_SOCKET, SO_KEEPALIVE, 0, OPT_BOOL},
73 {"SO_REUSEADDR", SOL_SOCKET, SO_REUSEADDR, 0, OPT_BOOL},
74 {"SO_BROADCAST", SOL_SOCKET, SO_BROADCAST, 0, OPT_BOOL},
76 {"TCP_NODELAY", IPPROTO_TCP, TCP_NODELAY, 0, OPT_BOOL},
79 {"IPTOS_LOWDELAY", IPPROTO_IP, IP_TOS, IPTOS_LOWDELAY, OPT_ON},
81 #ifdef IPTOS_THROUGHPUT
82 {"IPTOS_THROUGHPUT", IPPROTO_IP, IP_TOS, IPTOS_THROUGHPUT, OPT_ON},
85 {"SO_SNDBUF", SOL_SOCKET, SO_SNDBUF, 0, OPT_INT},
88 {"SO_RCVBUF", SOL_SOCKET, SO_RCVBUF, 0, OPT_INT},
91 {"SO_SNDLOWAT", SOL_SOCKET, SO_SNDLOWAT, 0, OPT_INT},
94 {"SO_RCVLOWAT", SOL_SOCKET, SO_RCVLOWAT, 0, OPT_INT},
97 {"SO_SNDTIMEO", SOL_SOCKET, SO_SNDTIMEO, 0, OPT_INT},
100 {"SO_RCVTIMEO", SOL_SOCKET, SO_RCVTIMEO, 0, OPT_INT},
106 /****************************************************************************
107 set user socket options
108 ****************************************************************************/
109 void set_socket_options(int fd, char *options)
113 while (next_token(&options,tok," \t,", sizeof(tok)))
118 BOOL got_value = False;
120 if ((p = strchr(tok,'=')))
127 for (i=0;socket_options[i].name;i++)
128 if (strequal(socket_options[i].name,tok))
131 if (!socket_options[i].name)
133 DEBUG(0,("Unknown socket option %s\n",tok));
137 switch (socket_options[i].opttype)
141 ret = setsockopt(fd,socket_options[i].level,
142 socket_options[i].option,(char *)&value,sizeof(int));
147 DEBUG(0,("syntax error - %s does not take a value\n",tok));
150 int on = socket_options[i].value;
151 ret = setsockopt(fd,socket_options[i].level,
152 socket_options[i].option,(char *)&on,sizeof(int));
158 DEBUG(0,("Failed to set socket option %s\n",tok));
164 /****************************************************************************
165 close the socket communication
166 ****************************************************************************/
167 void close_sockets(void )
170 sslutil_disconnect(Client);
171 #endif /* WITH_SSL */
179 /****************************************************************************
181 ****************************************************************************/
182 ssize_t write_socket(int fd,char *buf,size_t len)
188 DEBUG(6,("write_socket(%d,%d)\n",fd,len));
189 ret = write_data(fd,buf,len);
191 DEBUG(6,("write_socket(%d,%d) wrote %d\n",fd,len,ret));
193 DEBUG(0,("write_socket: Error writing %d bytes to socket %d: ERRNO = %s\n",
194 len, fd, strerror(errno) ));
199 /****************************************************************************
201 ****************************************************************************/
202 ssize_t read_udp_socket(int fd,char *buf,size_t len)
205 struct sockaddr_in sock;
208 socklen = sizeof(sock);
209 bzero((char *)&sock,socklen);
210 bzero((char *)&lastip,sizeof(lastip));
211 ret = (ssize_t)recvfrom(fd,buf,len,0,(struct sockaddr *)&sock,&socklen);
213 DEBUG(2,("read socket failed. ERRNO=%s\n",strerror(errno)));
217 lastip = sock.sin_addr;
218 lastport = ntohs(sock.sin_port);
220 DEBUG(10,("read_udp_socket: lastip %s lastport %d read: %d\n",
221 inet_ntoa(lastip), lastport, ret));
226 /****************************************************************************
227 read data from a device with a timout in msec.
228 mincount = if timeout, minimum to read before returning
229 maxcount = number to be read.
230 time_out = timeout in milliseconds
231 ****************************************************************************/
233 ssize_t read_with_timeout(int fd,char *buf,size_t mincnt,size_t maxcnt,unsigned int time_out)
239 struct timeval timeout;
241 /* just checking .... */
242 if (maxcnt <= 0) return(0);
248 if (mincnt == 0) mincnt = maxcnt;
250 while (nread < mincnt) {
253 readret = SSL_read(ssl, buf + nread, maxcnt - nread);
255 readret = read(fd, buf + nread, maxcnt - nread);
258 readret = read(fd, buf + nread, maxcnt - nread);
259 #endif /* WITH_SSL */
262 smb_read_error = READ_EOF;
267 smb_read_error = READ_ERROR;
272 return((ssize_t)nread);
275 /* Most difficult - timeout read */
276 /* If this is ever called on a disk file and
277 mincnt is greater then the filesize then
278 system performance will suffer severely as
279 select always returns true on disk files */
281 /* Set initial timeout */
282 timeout.tv_sec = (time_t)(time_out / 1000);
283 timeout.tv_usec = (long)(1000 * (time_out % 1000));
285 for (nread=0; nread < mincnt; )
290 selrtn = sys_select(fd+1,&fds,NULL, &timeout);
294 /* something is wrong. Maybe the socket is dead? */
295 smb_read_error = READ_ERROR;
299 /* Did we timeout ? */
301 smb_read_error = READ_TIMEOUT;
307 readret = SSL_read(ssl, buf + nread, maxcnt - nread);
309 readret = read(fd, buf + nread, maxcnt - nread);
312 readret = read(fd, buf+nread, maxcnt-nread);
313 #endif /* WITH_SSL */
316 /* we got EOF on the file descriptor */
317 smb_read_error = READ_EOF;
322 /* the descriptor is probably dead */
323 smb_read_error = READ_ERROR;
330 /* Return the number we got */
331 return((ssize_t)nread);
335 /****************************************************************************
336 send a keepalive packet (rfc1002)
337 ****************************************************************************/
338 BOOL send_keepalive(int client)
340 unsigned char buf[4];
343 buf[1] = buf[2] = buf[3] = 0;
345 return(write_data(client,(char *)buf,4) == 4);
350 /****************************************************************************
351 read data from the client, reading exactly N bytes.
352 ****************************************************************************/
353 ssize_t read_data(int fd,char *buffer,size_t N)
364 ret = SSL_read(ssl, buffer + total, N - total);
366 ret = read(fd,buffer + total,N - total);
369 ret = read(fd,buffer + total,N - total);
370 #endif /* WITH_SSL */
374 smb_read_error = READ_EOF;
379 smb_read_error = READ_ERROR;
384 return (ssize_t)total;
388 /****************************************************************************
390 ****************************************************************************/
391 ssize_t write_data(int fd,char *buffer,size_t N)
400 ret = SSL_write(ssl,buffer + total,N - total);
402 ret = write(fd,buffer + total,N - total);
405 ret = write(fd,buffer + total,N - total);
406 #endif /* WITH_SSL */
408 if (ret == -1) return -1;
409 if (ret == 0) return total;
413 return (ssize_t)total;
418 /****************************************************************************
419 read 4 bytes of a smb packet and return the smb length of the packet
420 store the result in the buffer
421 This version of the function will return a length of zero on receiving
423 timeout is in milliseconds.
424 ****************************************************************************/
425 static ssize_t read_smb_length_return_keepalive(int fd,char *inbuf,unsigned int timeout)
434 ok = (read_with_timeout(fd,inbuf,4,4,timeout) == 4);
436 ok = (read_data(fd,inbuf,4) == 4);
441 len = smb_len(inbuf);
442 msg_type = CVAL(inbuf,0);
444 if (msg_type == 0x85)
445 DEBUG(5,("Got keepalive packet\n"));
448 DEBUG(10,("got smb length of %d\n",len));
453 /****************************************************************************
454 read 4 bytes of a smb packet and return the smb length of the packet
455 store the result in the buffer. This version of the function will
456 never return a session keepalive (length of zero).
457 timeout is in milliseconds.
458 ****************************************************************************/
459 ssize_t read_smb_length(int fd,char *inbuf,unsigned int timeout)
465 len = read_smb_length_return_keepalive(fd, inbuf, timeout);
470 /* Ignore session keepalives. */
471 if(CVAL(inbuf,0) != 0x85)
478 /****************************************************************************
479 read an smb from a fd. Note that the buffer *MUST* be of size
480 BUFFER_SIZE+SAFETY_MARGIN.
481 The timeout is in milliseconds.
482 This function will return on a
483 receipt of a session keepalive packet.
484 ****************************************************************************/
485 BOOL receive_smb(int fd,char *buffer, unsigned int timeout)
491 bzero(buffer,smb_size + 100);
493 len = read_smb_length_return_keepalive(fd,buffer,timeout);
496 DEBUG(10,("receive_smb: length < 0!\n"));
500 if (len > BUFFER_SIZE) {
501 DEBUG(0,("Invalid packet length! (%d bytes).\n",len));
502 if (len > BUFFER_SIZE + (SAFETY_MARGIN/2))
509 ret = read_data(fd,buffer+4,len);
511 smb_read_error = READ_ERROR;
518 /****************************************************************************
519 read an smb from a fd ignoring all keepalive packets. Note that the buffer
520 *MUST* be of size BUFFER_SIZE+SAFETY_MARGIN.
521 The timeout is in milliseconds
523 This is exactly the same as receive_smb except that it never returns
524 a session keepalive packet (just as receive_smb used to do).
525 receive_smb was changed to return keepalives as the oplock processing means this call
526 should never go into a blocking read.
527 ****************************************************************************/
529 BOOL client_receive_smb(int fd,char *buffer, unsigned int timeout)
536 ret = receive_smb(fd, buffer, timeout);
540 DEBUG(10,("client_receive_smb failed\n"));
545 /* Ignore session keepalive packets. */
546 msg_type = CVAL(buffer,0);
547 if (msg_type != 0x85)
556 dump_data(10, buffer, smb_len(buffer) + 4);
562 /****************************************************************************
564 ****************************************************************************/
565 BOOL send_smb(int fd,char *buffer)
570 len = smb_len(buffer) + 4;
572 while (nwritten < len)
574 ret = write_socket(fd,buffer+nwritten,len - nwritten);
577 DEBUG(0,("Error writing %d bytes to client. %d. Exiting\n",len,ret));
589 /****************************************************************************
590 send a single packet to a port on another machine
591 ****************************************************************************/
592 BOOL send_one_packet(char *buf,int len,struct in_addr ip,int port,int type)
596 struct sockaddr_in sock_out;
601 /* create a socket to write to */
602 out_fd = socket(AF_INET, type, 0);
605 DEBUG(0,("socket failed"));
609 /* set the address and port */
610 bzero((char *)&sock_out,sizeof(sock_out));
611 putip((char *)&sock_out.sin_addr,(char *)&ip);
612 sock_out.sin_port = htons( port );
613 sock_out.sin_family = AF_INET;
616 DEBUG(3,("sending a packet of len %d to (%s) on port %d of type %s\n",
617 len,inet_ntoa(ip),port,type==SOCK_DGRAM?"DGRAM":"STREAM"));
620 ret = (sendto(out_fd,buf,len,0,(struct sockaddr *)&sock_out,sizeof(sock_out)) >= 0);
623 DEBUG(0,("Packet send to %s(%d) failed ERRNO=%s\n",
624 inet_ntoa(ip),port,strerror(errno)));
631 /****************************************************************************
632 open a socket of the specified type, port and address for incoming data
633 ****************************************************************************/
634 int open_socket_in(int type, int port, int dlevel,uint32 socket_addr)
637 struct sockaddr_in sock;
641 /* get my host name */
642 if (gethostname(host_name, MAXHOSTNAMELEN) == -1)
643 { DEBUG(0,("gethostname failed\n")); return -1; }
646 if ((hp = Get_Hostbyname(host_name)) == 0)
648 DEBUG(0,( "Get_Hostbyname: Unknown host %s\n",host_name));
652 bzero((char *)&sock,sizeof(sock));
653 memcpy((char *)&sock.sin_addr,(char *)hp->h_addr, hp->h_length);
655 #ifdef HAVE_SOCK_SIN_LEN
656 sock.sin_len = sizeof(sock);
658 sock.sin_port = htons( port );
659 sock.sin_family = hp->h_addrtype;
660 sock.sin_addr.s_addr = socket_addr;
661 res = socket(hp->h_addrtype, type, 0);
663 { DEBUG(0,("socket failed\n")); return -1; }
667 setsockopt(res,SOL_SOCKET,SO_REUSEADDR,(char *)&one,sizeof(one));
670 /* now we've got a socket - we need to bind it */
671 if (bind(res, (struct sockaddr * ) &sock,sizeof(sock)) < 0)
674 if (port == SMB_PORT || port == NMB_PORT)
675 DEBUG(dlevel,("bind failed on port %d socket_addr=%s (%s)\n",
676 port,inet_ntoa(sock.sin_addr),strerror(errno)));
679 if (dlevel > 0 && port < 1000)
682 if (port >= 1000 && port < 9000)
683 return(open_socket_in(type,port+1,dlevel,socket_addr));
688 DEBUG(3,("bind succeeded on port %d\n",port));
694 /****************************************************************************
695 create an outgoing socket
696 **************************************************************************/
697 int open_socket_out(int type, struct in_addr *addr, int port ,int timeout)
699 struct sockaddr_in sock_out;
701 int connect_loop = 250; /* 250 milliseconds */
702 int loops = (timeout * 1000) / connect_loop;
704 /* create a socket to write to */
705 res = socket(PF_INET, type, 0);
707 { DEBUG(0,("socket error\n")); return -1; }
709 if (type != SOCK_STREAM) return(res);
711 bzero((char *)&sock_out,sizeof(sock_out));
712 putip((char *)&sock_out.sin_addr,(char *)addr);
714 sock_out.sin_port = htons( port );
715 sock_out.sin_family = PF_INET;
717 /* set it non-blocking */
718 set_blocking(res,False);
720 DEBUG(3,("Connecting to %s at port %d\n",inet_ntoa(*addr),port));
722 /* and connect it to the destination */
724 ret = connect(res,(struct sockaddr *)&sock_out,sizeof(sock_out));
726 /* Some systems return EAGAIN when they mean EINPROGRESS */
727 if (ret < 0 && (errno == EINPROGRESS || errno == EALREADY ||
728 errno == EAGAIN) && loops--) {
729 msleep(connect_loop);
733 if (ret < 0 && (errno == EINPROGRESS || errno == EALREADY ||
735 DEBUG(1,("timeout connecting to %s:%d\n",inet_ntoa(*addr),port));
741 if (ret < 0 && errno == EISCONN) {
748 DEBUG(1,("error connecting to %s:%d (%s)\n",
749 inet_ntoa(*addr),port,strerror(errno)));
754 /* set it blocking again */
755 set_blocking(res,True);
761 /*******************************************************************
762 Reset the 'done' variables so after a client process is created
763 from a fork call these calls will be re-done. This should be
764 expanded if more variables need reseting.
765 ******************************************************************/
767 static BOOL global_client_name_done = False;
768 static BOOL global_client_addr_done = False;
770 void reset_globals_after_fork(void)
772 global_client_name_done = False;
773 global_client_addr_done = False;
776 * Re-seed the random crypto generator, so all smbd's
777 * started from the same parent won't generate the same
782 generate_random_buffer( &dummy, 1, True);
786 /*******************************************************************
787 return the DNS name of the client
788 ******************************************************************/
789 char *client_name(int fd)
792 struct sockaddr_in *sockin = (struct sockaddr_in *) (&sa);
793 int length = sizeof(sa);
794 static pstring name_buf;
796 static int last_fd=-1;
798 if (global_client_name_done && last_fd == fd)
802 global_client_name_done = False;
804 pstrcpy(name_buf,"UNKNOWN");
810 if (getpeername(fd, &sa, &length) < 0) {
811 DEBUG(0,("getpeername failed. Error was %s\n", strerror(errno) ));
815 /* Look up the remote host name. */
816 if ((hp = gethostbyaddr((char *) &sockin->sin_addr,
817 sizeof(sockin->sin_addr),
819 DEBUG(1,("Gethostbyaddr failed for %s\n",client_addr(fd)));
820 StrnCpy(name_buf,client_addr(fd),sizeof(name_buf) - 1);
822 StrnCpy(name_buf,(char *)hp->h_name,sizeof(name_buf) - 1);
823 if (!matchname(name_buf, sockin->sin_addr)) {
824 DEBUG(0,("Matchname failed on %s %s\n",name_buf,client_addr(fd)));
825 pstrcpy(name_buf,"UNKNOWN");
828 global_client_name_done = True;
832 /*******************************************************************
833 return the IP addr of the client as a string
834 ******************************************************************/
835 char *client_addr(int fd)
838 struct sockaddr_in *sockin = (struct sockaddr_in *) (&sa);
839 int length = sizeof(sa);
840 static fstring addr_buf;
841 static int last_fd = -1;
843 if (global_client_addr_done && fd == last_fd)
847 global_client_addr_done = False;
849 fstrcpy(addr_buf,"0.0.0.0");
855 if (getpeername(fd, &sa, &length) < 0) {
856 DEBUG(0,("getpeername failed. Error was %s\n", strerror(errno) ));
860 fstrcpy(addr_buf,(char *)inet_ntoa(sockin->sin_addr));
862 global_client_addr_done = True;
866 /*******************************************************************
867 opens and connects to a unix pipe socket
868 ******************************************************************/
869 int open_pipe_sock(char *path)
872 struct sockaddr_un sa;
874 sock = socket(AF_UNIX, SOCK_STREAM, 0);
878 DEBUG(0, ("unix socket open failed\n"));
883 sa.sun_family = AF_UNIX;
884 safe_strcpy(sa.sun_path, path, sizeof(sa.sun_path)-1);
886 DEBUG(10, ("socket open succeeded. file name: %s\n", sa.sun_path));
888 if (connect(sock, (struct sockaddr*) &sa, sizeof(sa)) < 0)
890 DEBUG(0,("socket connect to %s failed\n", sa.sun_path));