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 last IP received from */
39 struct in_addr lastip;
41 /* the last port received from */
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)
55 return(getsockopt(fd, SOL_SOCKET, SO_TYPE, (char *)&v, &l) == 0);
59 enum SOCK_OPT_TYPES {OPT_BOOL,OPT_INT,OPT_ON};
68 } socket_options[] = {
69 {"SO_KEEPALIVE", SOL_SOCKET, SO_KEEPALIVE, 0, OPT_BOOL},
70 {"SO_REUSEADDR", SOL_SOCKET, SO_REUSEADDR, 0, OPT_BOOL},
71 {"SO_BROADCAST", SOL_SOCKET, SO_BROADCAST, 0, OPT_BOOL},
73 {"TCP_NODELAY", IPPROTO_TCP, TCP_NODELAY, 0, OPT_BOOL},
76 {"IPTOS_LOWDELAY", IPPROTO_IP, IP_TOS, IPTOS_LOWDELAY, OPT_ON},
78 #ifdef IPTOS_THROUGHPUT
79 {"IPTOS_THROUGHPUT", IPPROTO_IP, IP_TOS, IPTOS_THROUGHPUT, OPT_ON},
82 {"SO_SNDBUF", SOL_SOCKET, SO_SNDBUF, 0, OPT_INT},
85 {"SO_RCVBUF", SOL_SOCKET, SO_RCVBUF, 0, OPT_INT},
88 {"SO_SNDLOWAT", SOL_SOCKET, SO_SNDLOWAT, 0, OPT_INT},
91 {"SO_RCVLOWAT", SOL_SOCKET, SO_RCVLOWAT, 0, OPT_INT},
94 {"SO_SNDTIMEO", SOL_SOCKET, SO_SNDTIMEO, 0, OPT_INT},
97 {"SO_RCVTIMEO", SOL_SOCKET, SO_RCVTIMEO, 0, OPT_INT},
103 /****************************************************************************
104 set user socket options
105 ****************************************************************************/
106 void set_socket_options(int fd, char *options)
110 while (next_token(&options,tok," \t,", sizeof(tok)))
115 BOOL got_value = False;
117 if ((p = strchr(tok,'=')))
124 for (i=0;socket_options[i].name;i++)
125 if (strequal(socket_options[i].name,tok))
128 if (!socket_options[i].name)
130 DEBUG(0,("Unknown socket option %s\n",tok));
134 switch (socket_options[i].opttype)
138 ret = setsockopt(fd,socket_options[i].level,
139 socket_options[i].option,(char *)&value,sizeof(int));
144 DEBUG(0,("syntax error - %s does not take a value\n",tok));
147 int on = socket_options[i].value;
148 ret = setsockopt(fd,socket_options[i].level,
149 socket_options[i].option,(char *)&on,sizeof(int));
155 DEBUG(0,("Failed to set socket option %s\n",tok));
161 /****************************************************************************
162 close the socket communication
163 ****************************************************************************/
164 void close_sockets(void )
167 sslutil_disconnect(Client);
168 #endif /* WITH_SSL */
176 /****************************************************************************
178 ****************************************************************************/
179 ssize_t write_socket(int fd,char *buf,size_t len)
185 DEBUG(6,("write_socket(%d,%d)\n",fd,len));
186 ret = write_data(fd,buf,len);
188 DEBUG(6,("write_socket(%d,%d) wrote %d\n",fd,len,ret));
190 DEBUG(0,("write_socket: Error writing %d bytes to socket %d: ERRNO = %s\n",
191 len, fd, strerror(errno) ));
196 /****************************************************************************
198 ****************************************************************************/
199 ssize_t read_udp_socket(int fd,char *buf,size_t len)
202 struct sockaddr_in sock;
205 socklen = sizeof(sock);
206 bzero((char *)&sock,socklen);
207 bzero((char *)&lastip,sizeof(lastip));
208 ret = (ssize_t)recvfrom(fd,buf,len,0,(struct sockaddr *)&sock,&socklen);
210 DEBUG(2,("read socket failed. ERRNO=%s\n",strerror(errno)));
214 lastip = sock.sin_addr;
215 lastport = ntohs(sock.sin_port);
217 DEBUG(10,("read_udp_socket: lastip %s lastport %d read: %d\n",
218 inet_ntoa(lastip), lastport, ret));
223 /****************************************************************************
224 read data from a device with a timout in msec.
225 mincount = if timeout, minimum to read before returning
226 maxcount = number to be read.
227 time_out = timeout in milliseconds
228 ****************************************************************************/
230 ssize_t read_with_timeout(int fd,char *buf,size_t mincnt,size_t maxcnt,unsigned int time_out)
236 struct timeval timeout;
238 /* just checking .... */
239 if (maxcnt <= 0) return(0);
245 if (mincnt == 0) mincnt = maxcnt;
247 while (nread < mincnt) {
250 readret = SSL_read(ssl, buf + nread, maxcnt - nread);
252 readret = read(fd, buf + nread, maxcnt - nread);
255 readret = read(fd, buf + nread, maxcnt - nread);
256 #endif /* WITH_SSL */
259 smb_read_error = READ_EOF;
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; )
287 selrtn = sys_select(fd+1,&fds,&timeout);
291 /* something is wrong. Maybe the socket is dead? */
292 smb_read_error = READ_ERROR;
296 /* Did we timeout ? */
298 smb_read_error = READ_TIMEOUT;
304 readret = SSL_read(ssl, buf + nread, maxcnt - nread);
306 readret = read(fd, buf + nread, maxcnt - nread);
309 readret = read(fd, buf+nread, maxcnt-nread);
310 #endif /* WITH_SSL */
313 /* we got EOF on the file descriptor */
314 smb_read_error = READ_EOF;
319 /* the descriptor is probably dead */
320 smb_read_error = READ_ERROR;
327 /* Return the number we got */
328 return((ssize_t)nread);
332 /****************************************************************************
333 send a keepalive packet (rfc1002)
334 ****************************************************************************/
335 BOOL send_keepalive(int client)
337 unsigned char buf[4];
340 buf[1] = buf[2] = buf[3] = 0;
342 return(write_data(client,(char *)buf,4) == 4);
347 /****************************************************************************
348 read data from the client, reading exactly N bytes.
349 ****************************************************************************/
350 ssize_t read_data(int fd,char *buffer,size_t N)
361 ret = SSL_read(ssl, buffer + total, N - total);
363 ret = read(fd,buffer + total,N - total);
366 ret = read(fd,buffer + total,N - total);
367 #endif /* WITH_SSL */
371 smb_read_error = READ_EOF;
376 smb_read_error = READ_ERROR;
381 return (ssize_t)total;
385 /****************************************************************************
387 ****************************************************************************/
388 ssize_t write_data(int fd,char *buffer,size_t N)
397 ret = SSL_write(ssl,buffer + total,N - total);
399 ret = write(fd,buffer + total,N - total);
402 ret = write(fd,buffer + total,N - total);
403 #endif /* WITH_SSL */
405 if (ret == -1) return -1;
406 if (ret == 0) return total;
410 return (ssize_t)total;
415 /****************************************************************************
416 read 4 bytes of a smb packet and return the smb length of the packet
417 store the result in the buffer
418 This version of the function will return a length of zero on receiving
420 timeout is in milliseconds.
421 ****************************************************************************/
422 static ssize_t read_smb_length_return_keepalive(int fd,char *inbuf,unsigned int timeout)
431 ok = (read_with_timeout(fd,inbuf,4,4,timeout) == 4);
433 ok = (read_data(fd,inbuf,4) == 4);
438 len = smb_len(inbuf);
439 msg_type = CVAL(inbuf,0);
441 if (msg_type == 0x85)
442 DEBUG(5,("Got keepalive packet\n"));
445 DEBUG(10,("got smb length of %d\n",len));
450 /****************************************************************************
451 read 4 bytes of a smb packet and return the smb length of the packet
452 store the result in the buffer. This version of the function will
453 never return a session keepalive (length of zero).
454 timeout is in milliseconds.
455 ****************************************************************************/
456 ssize_t read_smb_length(int fd,char *inbuf,unsigned int timeout)
462 len = read_smb_length_return_keepalive(fd, inbuf, timeout);
467 /* Ignore session keepalives. */
468 if(CVAL(inbuf,0) != 0x85)
475 /****************************************************************************
476 read an smb from a fd. Note that the buffer *MUST* be of size
477 BUFFER_SIZE+SAFETY_MARGIN.
478 The timeout is in milliseconds.
479 This function will return on a
480 receipt of a session keepalive packet.
481 ****************************************************************************/
482 BOOL receive_smb(int fd,char *buffer, unsigned int timeout)
488 bzero(buffer,smb_size + 100);
490 len = read_smb_length_return_keepalive(fd,buffer,timeout);
493 DEBUG(10,("receive_smb: length < 0!\n"));
497 if (len > BUFFER_SIZE) {
498 DEBUG(0,("Invalid packet length! (%d bytes).\n",len));
499 if (len > BUFFER_SIZE + (SAFETY_MARGIN/2))
506 ret = read_data(fd,buffer+4,len);
508 smb_read_error = READ_ERROR;
515 /****************************************************************************
516 read an smb from a fd ignoring all keepalive packets. Note that the buffer
517 *MUST* be of size BUFFER_SIZE+SAFETY_MARGIN.
518 The timeout is in milliseconds
520 This is exactly the same as receive_smb except that it never returns
521 a session keepalive packet (just as receive_smb used to do).
522 receive_smb was changed to return keepalives as the oplock processing means this call
523 should never go into a blocking read.
524 ****************************************************************************/
526 BOOL client_receive_smb(int fd,char *buffer, unsigned int timeout)
532 ret = receive_smb(fd, buffer, timeout);
536 DEBUG(10,("client_receive_smb failed\n"));
541 /* Ignore session keepalive packets. */
542 if(CVAL(buffer,0) != 0x85)
549 /****************************************************************************
551 ****************************************************************************/
552 BOOL send_smb(int fd,char *buffer)
557 len = smb_len(buffer) + 4;
559 while (nwritten < len)
561 ret = write_socket(fd,buffer+nwritten,len - nwritten);
564 DEBUG(0,("Error writing %d bytes to client. %d. Exiting\n",len,ret));
576 /****************************************************************************
577 send a single packet to a port on another machine
578 ****************************************************************************/
579 BOOL send_one_packet(char *buf,int len,struct in_addr ip,int port,int type)
583 struct sockaddr_in sock_out;
588 /* create a socket to write to */
589 out_fd = socket(AF_INET, type, 0);
592 DEBUG(0,("socket failed"));
596 /* set the address and port */
597 bzero((char *)&sock_out,sizeof(sock_out));
598 putip((char *)&sock_out.sin_addr,(char *)&ip);
599 sock_out.sin_port = htons( port );
600 sock_out.sin_family = AF_INET;
603 DEBUG(3,("sending a packet of len %d to (%s) on port %d of type %s\n",
604 len,inet_ntoa(ip),port,type==SOCK_DGRAM?"DGRAM":"STREAM"));
607 ret = (sendto(out_fd,buf,len,0,(struct sockaddr *)&sock_out,sizeof(sock_out)) >= 0);
610 DEBUG(0,("Packet send to %s(%d) failed ERRNO=%s\n",
611 inet_ntoa(ip),port,strerror(errno)));
618 /****************************************************************************
619 open a socket of the specified type, port and address for incoming data
620 ****************************************************************************/
621 int open_socket_in(int type, int port, int dlevel,uint32 socket_addr)
624 struct sockaddr_in sock;
628 /* get my host name */
629 if (gethostname(host_name, MAXHOSTNAMELEN) == -1)
630 { DEBUG(0,("gethostname failed\n")); return -1; }
633 if ((hp = Get_Hostbyname(host_name)) == 0)
635 DEBUG(0,( "Get_Hostbyname: Unknown host %s\n",host_name));
639 bzero((char *)&sock,sizeof(sock));
640 memcpy((char *)&sock.sin_addr,(char *)hp->h_addr, hp->h_length);
642 #ifdef HAVE_SOCK_SIN_LEN
643 sock.sin_len = sizeof(sock);
645 sock.sin_port = htons( port );
646 sock.sin_family = hp->h_addrtype;
647 sock.sin_addr.s_addr = socket_addr;
648 res = socket(hp->h_addrtype, type, 0);
650 { DEBUG(0,("socket failed\n")); return -1; }
654 setsockopt(res,SOL_SOCKET,SO_REUSEADDR,(char *)&one,sizeof(one));
657 /* now we've got a socket - we need to bind it */
658 if (bind(res, (struct sockaddr * ) &sock,sizeof(sock)) < 0)
661 if (port == SMB_PORT || port == NMB_PORT)
662 DEBUG(dlevel,("bind failed on port %d socket_addr=%s (%s)\n",
663 port,inet_ntoa(sock.sin_addr),strerror(errno)));
666 if (dlevel > 0 && port < 1000)
669 if (port >= 1000 && port < 9000)
670 return(open_socket_in(type,port+1,dlevel,socket_addr));
675 DEBUG(3,("bind succeeded on port %d\n",port));
681 /****************************************************************************
682 create an outgoing socket
683 **************************************************************************/
684 int open_socket_out(int type, struct in_addr *addr, int port ,int timeout)
686 struct sockaddr_in sock_out;
688 int connect_loop = 250; /* 250 milliseconds */
689 int loops = (timeout * 1000) / connect_loop;
691 /* create a socket to write to */
692 res = socket(PF_INET, type, 0);
694 { DEBUG(0,("socket error\n")); return -1; }
696 if (type != SOCK_STREAM) return(res);
698 bzero((char *)&sock_out,sizeof(sock_out));
699 putip((char *)&sock_out.sin_addr,(char *)addr);
701 sock_out.sin_port = htons( port );
702 sock_out.sin_family = PF_INET;
704 /* set it non-blocking */
705 set_blocking(res,False);
707 DEBUG(3,("Connecting to %s at port %d\n",inet_ntoa(*addr),port));
709 /* and connect it to the destination */
711 ret = connect(res,(struct sockaddr *)&sock_out,sizeof(sock_out));
713 /* Some systems return EAGAIN when they mean EINPROGRESS */
714 if (ret < 0 && (errno == EINPROGRESS || errno == EALREADY ||
715 errno == EAGAIN) && loops--) {
716 msleep(connect_loop);
720 if (ret < 0 && (errno == EINPROGRESS || errno == EALREADY ||
722 DEBUG(1,("timeout connecting to %s:%d\n",inet_ntoa(*addr),port));
728 if (ret < 0 && errno == EISCONN) {
735 DEBUG(1,("error connecting to %s:%d (%s)\n",
736 inet_ntoa(*addr),port,strerror(errno)));
741 /* set it blocking again */
742 set_blocking(res,True);
748 /*******************************************************************
749 Reset the 'done' variables so after a client process is created
750 from a fork call these calls will be re-done. This should be
751 expanded if more variables need reseting.
752 ******************************************************************/
754 static BOOL global_client_name_done = False;
755 static BOOL global_client_addr_done = False;
757 void reset_globals_after_fork(void)
759 global_client_name_done = False;
760 global_client_addr_done = False;
763 * Re-seed the random crypto generator, so all smbd's
764 * started from the same parent won't generate the same
769 generate_random_buffer( &dummy, 1, True);
773 /*******************************************************************
774 return the DNS name of the client
775 ******************************************************************/
776 char *client_name(int fd)
779 struct sockaddr_in *sockin = (struct sockaddr_in *) (&sa);
780 int length = sizeof(sa);
781 static pstring name_buf;
783 static int last_fd=-1;
785 if (global_client_name_done && last_fd == fd)
789 global_client_name_done = False;
791 pstrcpy(name_buf,"UNKNOWN");
797 if (getpeername(fd, &sa, &length) < 0) {
798 DEBUG(0,("getpeername failed. Error was %s\n", strerror(errno) ));
802 /* Look up the remote host name. */
803 if ((hp = gethostbyaddr((char *) &sockin->sin_addr,
804 sizeof(sockin->sin_addr),
806 DEBUG(1,("Gethostbyaddr failed for %s\n",client_addr(fd)));
807 StrnCpy(name_buf,client_addr(fd),sizeof(name_buf) - 1);
809 StrnCpy(name_buf,(char *)hp->h_name,sizeof(name_buf) - 1);
810 if (!matchname(name_buf, sockin->sin_addr)) {
811 DEBUG(0,("Matchname failed on %s %s\n",name_buf,client_addr(fd)));
812 pstrcpy(name_buf,"UNKNOWN");
815 global_client_name_done = True;
819 /*******************************************************************
820 return the IP addr of the client as a string
821 ******************************************************************/
822 char *client_addr(int fd)
825 struct sockaddr_in *sockin = (struct sockaddr_in *) (&sa);
826 int length = sizeof(sa);
827 static fstring addr_buf;
828 static int last_fd = -1;
830 if (global_client_addr_done && fd == last_fd)
834 global_client_addr_done = False;
836 fstrcpy(addr_buf,"0.0.0.0");
842 if (getpeername(fd, &sa, &length) < 0) {
843 DEBUG(0,("getpeername failed. Error was %s\n", strerror(errno) ));
847 fstrcpy(addr_buf,(char *)inet_ntoa(sockin->sin_addr));
849 global_client_addr_done = True;