2 Unix SMB/Netbios implementation.
4 Samba utility functions
6 Copyright (C) Andrew Tridgell 1992-1998
9 The Free Software Foundation, Inc.
11 This file is part of the Midnight Commander.
13 The Midnight Commander is free software: you can redistribute it
14 and/or modify it under the terms of the GNU General Public License as
15 published by the Free Software Foundation, either version 3 of the License,
16 or (at your option) any later version.
18 The Midnight Commander is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program. If not, see <http://www.gnu.org/licenses/>.
29 const char *unix_error_string (int error_num
);
33 #undef Realloc /* SSLeay defines this and samba has a function of this name */
38 extern int DEBUGLEVEL
;
42 /* the client file descriptor */
45 /* the last IP received from */
46 struct in_addr lastip
;
48 /* the last port received from */
52 int smb_read_error
= 0;
55 /****************************************************************************
56 determine if a file descriptor is in fact a socket
57 ****************************************************************************/
64 return (getsockopt (fd
, SOL_SOCKET
, SO_TYPE
, (char *) &v
, &l
) == 0);
69 { OPT_BOOL
, OPT_INT
, OPT_ON
};
81 "SO_KEEPALIVE", SOL_SOCKET
, SO_KEEPALIVE
, 0, OPT_BOOL
},
83 "SO_REUSEADDR", SOL_SOCKET
, SO_REUSEADDR
, 0, OPT_BOOL
},
85 "SO_BROADCAST", SOL_SOCKET
, SO_BROADCAST
, 0, OPT_BOOL
},
88 "TCP_NODELAY", IPPROTO_TCP
, TCP_NODELAY
, 0, OPT_BOOL
},
92 "IPTOS_LOWDELAY", IPPROTO_IP
, IP_TOS
, IPTOS_LOWDELAY
, OPT_ON
},
94 #ifdef IPTOS_THROUGHPUT
96 "IPTOS_THROUGHPUT", IPPROTO_IP
, IP_TOS
, IPTOS_THROUGHPUT
, OPT_ON
},
100 "SO_SNDBUF", SOL_SOCKET
, SO_SNDBUF
, 0, OPT_INT
},
104 "SO_RCVBUF", SOL_SOCKET
, SO_RCVBUF
, 0, OPT_INT
},
108 "SO_SNDLOWAT", SOL_SOCKET
, SO_SNDLOWAT
, 0, OPT_INT
},
112 "SO_RCVLOWAT", SOL_SOCKET
, SO_RCVLOWAT
, 0, OPT_INT
},
116 "SO_SNDTIMEO", SOL_SOCKET
, SO_SNDTIMEO
, 0, OPT_INT
},
120 "SO_RCVTIMEO", SOL_SOCKET
, SO_RCVTIMEO
, 0, OPT_INT
},
127 /****************************************************************************
128 set user socket options
129 ****************************************************************************/
131 set_socket_options (int fd
, char *options
)
135 while (next_token (&options
, tok
, " \t,", sizeof (tok
)))
140 BOOL got_value
= False
;
142 if ((p
= strchr (tok
, '=')))
145 value
= atoi (p
+ 1);
149 for (i
= 0; socket_options
[i
].name
; i
++)
150 if (strequal (socket_options
[i
].name
, tok
))
153 if (!socket_options
[i
].name
)
155 DEBUG (0, ("Unknown socket option %s\n", tok
));
159 switch (socket_options
[i
].opttype
)
163 ret
= setsockopt (fd
, socket_options
[i
].level
,
164 socket_options
[i
].option
, (char *) &value
, sizeof (int));
169 DEBUG (0, ("syntax error - %s does not take a value\n", tok
));
172 int on
= socket_options
[i
].value
;
173 ret
= setsockopt (fd
, socket_options
[i
].level
,
174 socket_options
[i
].option
, (char *) &on
, sizeof (int));
180 DEBUG (0, ("Failed to set socket option %s\n", tok
));
186 /****************************************************************************
187 close the socket communication
188 ****************************************************************************/
193 sslutil_disconnect (Client
);
194 #endif /* WITH_SSL */
202 /****************************************************************************
204 ****************************************************************************/
206 write_socket (int fd
, char *buf
, size_t len
)
212 DEBUG (6, ("write_socket(%d,%d)\n", fd
, (int) len
));
213 ret
= write_data (fd
, buf
, len
);
215 DEBUG (6, ("write_socket(%d,%d) wrote %d\n", fd
, (int) len
, (int) ret
));
217 DEBUG (1, ("write_socket: Error writing %d bytes to socket %d: ERRNO = %s\n",
218 (int) len
, fd
, unix_error_string (errno
)));
223 /****************************************************************************
225 ****************************************************************************/
227 read_udp_socket (int fd
, char *buf
, size_t len
)
230 struct sockaddr_in sock
;
231 unsigned int socklen
;
233 socklen
= sizeof (sock
);
234 memset ((char *) &sock
, '\0', socklen
);
235 memset ((char *) &lastip
, '\0', sizeof (lastip
));
236 ret
= (ssize_t
) recvfrom (fd
, buf
, len
, 0, (struct sockaddr
*) &sock
, &socklen
);
239 DEBUG (2, ("read socket failed. ERRNO=%s\n", unix_error_string (errno
)));
243 lastip
= sock
.sin_addr
;
244 lastport
= ntohs (sock
.sin_port
);
246 DEBUG (10, ("read_udp_socket: lastip %s lastport %d read: %d\n",
247 inet_ntoa (lastip
), lastport
, (int) ret
));
253 /****************************************************************************
254 read data from a device with a timout in msec.
255 mincount = if timeout, minimum to read before returning
256 maxcount = number to be read.
257 time_out = timeout in milliseconds
258 ****************************************************************************/
261 read_with_timeout (int fd
, char *buf
, size_t mincnt
, size_t maxcnt
, unsigned int time_out
)
267 struct timeval timeout
;
269 /* just checking .... */
281 while (nread
< mincnt
)
286 readret
= SSL_read (ssl
, buf
+ nread
, maxcnt
- nread
);
290 readret
= read (fd
, buf
+ nread
, maxcnt
- nread
);
293 readret
= read (fd
, buf
+ nread
, maxcnt
- nread
);
294 #endif /* WITH_SSL */
298 DEBUG (5, ("read_with_timeout: blocking read. EOF from client.\n"));
299 smb_read_error
= READ_EOF
;
305 DEBUG (0, ("read_with_timeout: read error = %s.\n", unix_error_string (errno
)));
306 smb_read_error
= READ_ERROR
;
311 return ((ssize_t
) nread
);
314 /* Most difficult - timeout read */
315 /* If this is ever called on a disk file and
316 mincnt is greater then the filesize then
317 system performance will suffer severely as
318 select always returns true on disk files */
320 /* Set initial timeout */
321 timeout
.tv_sec
= (time_t) (time_out
/ 1000);
322 timeout
.tv_usec
= (long) (1000 * (time_out
% 1000));
324 for (nread
= 0; nread
< mincnt
;)
329 selrtn
= sys_select (fd
+ 1, &fds
, &timeout
);
334 /* something is wrong. Maybe the socket is dead? */
336 ("read_with_timeout: timeout read. select error = %s.\n",
337 unix_error_string (errno
)));
338 smb_read_error
= READ_ERROR
;
342 /* Did we timeout ? */
345 DEBUG (10, ("read_with_timeout: timeout read. select timed out.\n"));
346 smb_read_error
= READ_TIMEOUT
;
353 readret
= SSL_read (ssl
, buf
+ nread
, maxcnt
- nread
);
357 readret
= read (fd
, buf
+ nread
, maxcnt
- nread
);
360 readret
= read (fd
, buf
+ nread
, maxcnt
- nread
);
361 #endif /* WITH_SSL */
365 /* we got EOF on the file descriptor */
366 DEBUG (5, ("read_with_timeout: timeout read. EOF from client.\n"));
367 smb_read_error
= READ_EOF
;
373 /* the descriptor is probably dead */
375 ("read_with_timeout: timeout read. read error = %s.\n",
376 unix_error_string (errno
)));
377 smb_read_error
= READ_ERROR
;
384 /* Return the number we got */
385 return ((ssize_t
) nread
);
389 /****************************************************************************
390 send a keepalive packet (rfc1002)
391 ****************************************************************************/
393 send_keepalive (int client
)
395 unsigned char buf
[4];
398 buf
[1] = buf
[2] = buf
[3] = 0;
400 return (write_data (client
, (char *) buf
, 4) == 4);
405 /****************************************************************************
406 read data from the client, reading exactly N bytes.
407 ****************************************************************************/
409 read_data (int fd
, char *buffer
, size_t N
)
421 ret
= SSL_read (ssl
, buffer
+ total
, N
- total
);
425 ret
= read (fd
, buffer
+ total
, N
- total
);
428 ret
= read (fd
, buffer
+ total
, N
- total
);
429 #endif /* WITH_SSL */
434 ("read_data: read of %d returned 0. Error = %s\n", (int) (N
- total
),
435 unix_error_string (errno
)));
436 smb_read_error
= READ_EOF
;
442 ("read_data: read failure for %d. Error = %s\n", (int) (N
- total
),
443 unix_error_string (errno
)));
444 smb_read_error
= READ_ERROR
;
449 return (ssize_t
) total
;
453 /****************************************************************************
455 ****************************************************************************/
457 write_data (int fd
, char *buffer
, size_t N
)
467 ret
= SSL_write (ssl
, buffer
+ total
, N
- total
);
471 ret
= write (fd
, buffer
+ total
, N
- total
);
474 ret
= write (fd
, buffer
+ total
, N
- total
);
475 #endif /* WITH_SSL */
479 DEBUG (1, ("write_data: write failure. Error = %s\n", unix_error_string (errno
)));
487 return (ssize_t
) total
;
492 /****************************************************************************
493 read 4 bytes of a smb packet and return the smb length of the packet
494 store the result in the buffer
495 This version of the function will return a length of zero on receiving
497 timeout is in milliseconds.
498 ****************************************************************************/
500 read_smb_length_return_keepalive (int fd
, char *inbuf
, unsigned int timeout
)
509 ok
= (read_with_timeout (fd
, inbuf
, 4, 4, timeout
) == 4);
511 ok
= (read_data (fd
, inbuf
, 4) == 4);
516 len
= smb_len (inbuf
);
517 msg_type
= CVAL (inbuf
, 0);
519 if (msg_type
== 0x85)
520 DEBUG (5, ("Got keepalive packet\n"));
523 DEBUG (10, ("got smb length of %d\n", (int) len
));
529 /****************************************************************************
530 read 4 bytes of a smb packet and return the smb length of the packet
531 store the result in the buffer. This version of the function will
532 never return a session keepalive (length of zero).
533 timeout is in milliseconds.
534 ****************************************************************************/
536 read_smb_length (int fd
, char *inbuf
, unsigned int timeout
)
542 len
= read_smb_length_return_keepalive (fd
, inbuf
, timeout
);
547 /* Ignore session keepalives. */
548 if (CVAL (inbuf
, 0) != 0x85)
552 DEBUG (10, ("read_smb_length: got smb length of %d\n", len
));
557 /****************************************************************************
558 read an smb from a fd. Note that the buffer *MUST* be of size
559 BUFFER_SIZE+SAFETY_MARGIN.
560 The timeout is in milliseconds.
561 This function will return on a
562 receipt of a session keepalive packet.
563 ****************************************************************************/
565 receive_smb (int fd
, char *buffer
, unsigned int timeout
)
571 memset (buffer
, '\0', smb_size
+ 100);
573 len
= read_smb_length_return_keepalive (fd
, buffer
, timeout
);
576 DEBUG (10, ("receive_smb: length < 0!\n"));
580 if (len
> BUFFER_SIZE
)
582 DEBUG (0, ("Invalid packet length! (%d bytes).\n", (int) len
));
583 if (len
> BUFFER_SIZE
+ (SAFETY_MARGIN
/ 2))
591 ret
= read_data (fd
, buffer
+ 4, len
);
594 smb_read_error
= READ_ERROR
;
601 /****************************************************************************
602 read an smb from a fd ignoring all keepalive packets. Note that the buffer
603 *MUST* be of size BUFFER_SIZE+SAFETY_MARGIN.
604 The timeout is in milliseconds
606 This is exactly the same as receive_smb except that it never returns
607 a session keepalive packet (just as receive_smb used to do).
608 receive_smb was changed to return keepalives as the oplock processing means this call
609 should never go into a blocking read.
610 ****************************************************************************/
613 client_receive_smb (int fd
, char *buffer
, unsigned int timeout
)
619 ret
= receive_smb (fd
, buffer
, timeout
);
623 DEBUG (10, ("client_receive_smb failed\n"));
628 /* Ignore session keepalive packets. */
629 if (CVAL (buffer
, 0) != 0x85)
636 /****************************************************************************
637 send an null session message to a fd
638 ****************************************************************************/
641 send_null_session_msg (int fd
)
647 char *buffer
= (char *) &blank
;
649 while (nwritten
< len
)
651 ret
= write_socket (fd
, buffer
+ nwritten
, len
- nwritten
);
655 ("send_null_session_msg: Error writing %d bytes to client. %d. Exiting\n",
656 (int) len
, (int) ret
));
663 DEBUG (10, ("send_null_session_msg: sent 4 null bytes to client.\n"));
667 /****************************************************************************
669 ****************************************************************************/
671 send_smb (int fd
, char *buffer
)
676 len
= smb_len (buffer
) + 4;
678 while (nwritten
< len
)
680 ret
= write_socket (fd
, buffer
+ nwritten
, len
- nwritten
);
683 DEBUG (0, ("Error writing %d bytes to client. %d. Exiting\n", (int) len
, (int) ret
));
695 /****************************************************************************
696 send a single packet to a port on another machine
697 ****************************************************************************/
699 send_one_packet (char *buf
, int len
, struct in_addr ip
, int port
, int type
)
703 struct sockaddr_in sock_out
;
708 /* create a socket to write to */
709 out_fd
= socket (AF_INET
, type
, 0);
712 DEBUG (0, ("socket failed"));
716 /* set the address and port */
717 memset ((char *) &sock_out
, '\0', sizeof (sock_out
));
718 putip ((char *) &sock_out
.sin_addr
, (char *) &ip
);
719 sock_out
.sin_port
= htons (port
);
720 sock_out
.sin_family
= AF_INET
;
723 DEBUG (3, ("sending a packet of len %d to (%s) on port %d of type %s\n",
724 len
, inet_ntoa (ip
), port
, type
== SOCK_DGRAM
? "DGRAM" : "STREAM"));
727 ret
= (sendto (out_fd
, buf
, len
, 0, (struct sockaddr
*) &sock_out
, sizeof (sock_out
)) >= 0);
730 DEBUG (0, ("Packet send to %s(%d) failed ERRNO=%s\n",
731 inet_ntoa (ip
), port
, unix_error_string (errno
)));
738 /****************************************************************************
739 open a socket of the specified type, port and address for incoming data
740 ****************************************************************************/
742 open_socket_in (int type
, int port
, int dlevel
, uint32 socket_addr
, BOOL rebind
)
745 struct sockaddr_in sock
;
749 /* get my host name */
750 if (gethostname (host_name
, MAXHOSTNAMELEN
) == -1)
752 DEBUG (0, ("gethostname failed\n"));
757 if ((hp
= Get_Hostbyname (host_name
)) == 0)
759 DEBUG (0, ("Get_Hostbyname: Unknown host %s\n", host_name
));
763 memset ((char *) &sock
, '\0', sizeof (sock
));
764 memcpy ((char *) &sock
.sin_addr
, (char *) hp
->h_addr
, hp
->h_length
);
766 #ifdef HAVE_SOCK_SIN_LEN
767 sock
.sin_len
= sizeof (sock
);
769 sock
.sin_port
= htons (port
);
770 sock
.sin_family
= hp
->h_addrtype
;
771 sock
.sin_addr
.s_addr
= socket_addr
;
772 res
= socket (hp
->h_addrtype
, type
, 0);
775 DEBUG (0, ("socket failed\n"));
785 setsockopt (res
, SOL_SOCKET
, SO_REUSEADDR
, (char *) &val
, sizeof (val
));
788 /* now we've got a socket - we need to bind it */
789 if (bind (res
, (struct sockaddr
*) &sock
, sizeof (sock
)) < 0)
793 if (port
== SMB_PORT
|| port
== NMB_PORT
)
794 DEBUG (dlevel
, ("bind failed on port %d socket_addr=%s (%s)\n",
795 port
, inet_ntoa (sock
.sin_addr
), unix_error_string (errno
)));
798 if (dlevel
> 0 && port
< 1000)
801 if (port
>= 1000 && port
< 9000)
802 return (open_socket_in (type
, port
+ 1, dlevel
, socket_addr
, rebind
));
807 DEBUG (3, ("bind succeeded on port %d\n", port
));
813 /****************************************************************************
814 create an outgoing socket. timeout is in milliseconds.
815 **************************************************************************/
817 open_socket_out (int type
, struct in_addr
*addr
, int port
, int timeout
)
819 struct sockaddr_in sock_out
;
821 int connect_loop
= 250; /* 250 milliseconds */
822 int loops
= (timeout
) / connect_loop
;
824 /* create a socket to write to */
825 res
= socket (PF_INET
, type
, 0);
828 DEBUG (0, ("socket error\n"));
832 if (type
!= SOCK_STREAM
)
835 memset ((char *) &sock_out
, '\0', sizeof (sock_out
));
836 putip ((char *) &sock_out
.sin_addr
, (char *) addr
);
838 sock_out
.sin_port
= htons (port
);
839 sock_out
.sin_family
= PF_INET
;
841 /* set it non-blocking */
842 set_blocking (res
, False
);
844 DEBUG (3, ("Connecting to %s at port %d\n", inet_ntoa (*addr
), port
));
846 /* and connect it to the destination */
848 ret
= connect (res
, (struct sockaddr
*) &sock_out
, sizeof (sock_out
));
850 /* Some systems return EAGAIN when they mean EINPROGRESS */
851 if (ret
< 0 && (errno
== EINPROGRESS
|| errno
== EALREADY
|| errno
== EAGAIN
) && loops
--)
853 msleep (connect_loop
);
857 if (ret
< 0 && (errno
== EINPROGRESS
|| errno
== EALREADY
|| errno
== EAGAIN
))
859 DEBUG (1, ("timeout connecting to %s:%d\n", inet_ntoa (*addr
), port
));
865 if (ret
< 0 && errno
== EISCONN
)
874 DEBUG (1, ("error connecting to %s:%d (%s)\n",
875 inet_ntoa (*addr
), port
, unix_error_string (errno
)));
880 /* set it blocking again */
881 set_blocking (res
, True
);
887 /*******************************************************************
888 Reset the 'done' variables so after a client process is created
889 from a fork call these calls will be re-done. This should be
890 expanded if more variables need reseting.
891 ******************************************************************/
893 static BOOL global_client_name_done
= False
;
894 static BOOL global_client_addr_done
= False
;
896 /*******************************************************************
897 return the DNS name of the client
898 ******************************************************************/
903 struct sockaddr_in
*sockin
= (struct sockaddr_in
*) (&sa
);
904 unsigned int length
= sizeof (sa
);
905 static pstring name_buf
;
907 static int last_fd
= -1;
909 if (global_client_name_done
&& last_fd
== fd
)
913 global_client_name_done
= False
;
915 pstrcpy (name_buf
, "UNKNOWN");
922 if (getpeername (fd
, &sa
, &length
) < 0)
924 DEBUG (0, ("getpeername failed. Error was %s\n", unix_error_string (errno
)));
928 /* Look up the remote host name. */
929 if ((hp
= gethostbyaddr ((char *) &sockin
->sin_addr
, sizeof (sockin
->sin_addr
), AF_INET
)) == 0)
931 DEBUG (1, ("Gethostbyaddr failed for %s\n", client_addr (fd
)));
932 StrnCpy (name_buf
, client_addr (fd
), sizeof (name_buf
) - 1);
936 StrnCpy (name_buf
, (char *) hp
->h_name
, sizeof (name_buf
) - 1);
937 if (!matchname (name_buf
, sockin
->sin_addr
))
939 DEBUG (0, ("Matchname failed on %s %s\n", name_buf
, client_addr (fd
)));
940 pstrcpy (name_buf
, "UNKNOWN");
943 global_client_name_done
= True
;
947 /*******************************************************************
948 return the IP addr of the client as a string
949 ******************************************************************/
954 struct sockaddr_in
*sockin
= (struct sockaddr_in
*) (&sa
);
955 unsigned int length
= sizeof (sa
);
956 static fstring addr_buf
;
957 static int last_fd
= -1;
959 if (global_client_addr_done
&& fd
== last_fd
)
963 global_client_addr_done
= False
;
965 fstrcpy (addr_buf
, "0.0.0.0");
972 if (getpeername (fd
, &sa
, &length
) < 0)
974 DEBUG (0, ("getpeername failed. Error was %s\n", unix_error_string (errno
)));
978 fstrcpy (addr_buf
, (char *) inet_ntoa (sockin
->sin_addr
));
980 global_client_addr_done
= True
;