}
+#define TRUNCATE_NETBIOS_NAME 1
+
+/*******************************************************************
+ convert, possibly using a stupid microsoft-ism which has destroyed
+ the transport independence of netbios (for CIFS vendors that usually
+ use the Win95-type methods, not for NT to NT communication, which uses
+ DCE/RPC and therefore full-length unicode strings...) a dns name into
+ a netbios name.
+
+ the netbios name (NOT necessarily null-terminated) is truncated to 15
+ characters.
+
+ ******************************************************************/
+char *dns_to_netbios_name(char *dns_name)
+{
+ static char netbios_name[16];
+ int i;
+ StrnCpy(netbios_name, dns_name, 15);
+ netbios_name[15] = 0;
+
+#ifdef TRUNCATE_NETBIOS_NAME
+ /* ok. this is because of a stupid microsoft-ism. if the called host
+ name contains a '.', microsoft clients expect you to truncate the
+ netbios name up to and including the '.' this even applies, by
+ mistake, to workgroup (domain) names, which is _really_ daft.
+ */
+ for (i = 15; i >= 0; i--)
+ {
+ if (netbios_name[i] == '.')
+ {
+ netbios_name[i] = 0;
+ break;
+ }
+ }
+#endif /* TRUNCATE_NETBIOS_NAME */
+
+ return netbios_name;
+}
+
+
/****************************************************************************
interpret the weird netbios "name". Return the name type
****************************************************************************/
****************************************************************************/
void unix_format(char *fname)
{
- pstring namecopy;
string_replace(fname,'\\','/');
-
- if (*fname == '/')
- {
- pstrcpy(namecopy,fname);
- pstrcpy(fname,".");
- pstrcat(fname,namecopy);
- }
}
/****************************************************************************
BOOL trim_string(char *s,char *front,char *back)
{
BOOL ret = False;
- while (front && *front && strncmp(s,front,strlen(front)) == 0)
- {
- char *p = s;
- ret = True;
- while (1)
- {
- if (!(*p = p[strlen(front)]))
- break;
- p++;
- }
- }
- while (back && *back && strlen(s) >= strlen(back) &&
- (strncmp(s+strlen(s)-strlen(back),back,strlen(back))==0))
+ size_t front_len = (front && *front) ? strlen(front) : 0;
+ size_t back_len = (back && *back) ? strlen(back) : 0;
+ size_t s_len;
+
+ while (front_len && strncmp(s, front, front_len) == 0)
+ {
+ char *p = s;
+ ret = True;
+ while (1)
{
- ret = True;
- s[strlen(s)-strlen(back)] = 0;
+ if (!(*p = p[front_len]))
+ break;
+ p++;
}
+ }
+
+ s_len = strlen(s);
+ while (back_len && s_len >= back_len &&
+ (strncmp(s + s_len - back_len, back, back_len)==0))
+ {
+ ret = True;
+ s[s_len - back_len] = 0;
+ s_len = strlen(s);
+ }
return(ret);
}
/****************************************************************************
write to a socket
****************************************************************************/
-int write_socket(int fd,char *buf,int len)
+ssize_t write_socket(int fd,char *buf,size_t len)
{
- int ret=0;
+ ssize_t ret=0;
if (passive)
return(len);
/****************************************************************************
read from a socket
****************************************************************************/
-int read_udp_socket(int fd,char *buf,int len)
+ssize_t read_udp_socket(int fd,char *buf,size_t len)
{
- int ret;
+ ssize_t ret;
struct sockaddr_in sock;
int socklen;
socklen = sizeof(sock);
bzero((char *)&sock,socklen);
bzero((char *)&lastip,sizeof(lastip));
- ret = recvfrom(fd,buf,len,0,(struct sockaddr *)&sock,&socklen);
+ ret = (ssize_t)recvfrom(fd,buf,len,0,(struct sockaddr *)&sock,&socklen);
if (ret <= 0) {
DEBUG(2,("read socket failed. ERRNO=%s\n",strerror(errno)));
return(0);
read data from a device with a timout in msec.
mincount = if timeout, minimum to read before returning
maxcount = number to be read.
+time_out = timeout in milliseconds
****************************************************************************/
-int read_with_timeout(int fd,char *buf,int mincnt,int maxcnt,long time_out)
+
+ssize_t read_with_timeout(int fd,char *buf,size_t mincnt,size_t maxcnt,unsigned int time_out)
{
fd_set fds;
int selrtn;
- int readret;
- int nread = 0;
+ ssize_t readret;
+ size_t nread = 0;
struct timeval timeout;
/* just checking .... */
#endif /* WITH_SSL */
if (readret == 0) {
- smb_read_error = READ_EOF;
- return -1;
+ smb_read_error = READ_EOF;
+ return -1;
}
if (readret == -1) {
- smb_read_error = READ_ERROR;
- return -1;
+ smb_read_error = READ_ERROR;
+ return -1;
}
nread += readret;
}
- return(nread);
+ return((ssize_t)nread);
}
/* Most difficult - timeout read */
/* If this is ever called on a disk file and
- mincnt is greater then the filesize then
- system performance will suffer severely as
- select always return true on disk files */
+ mincnt is greater then the filesize then
+ system performance will suffer severely as
+ select always returns true on disk files */
/* Set initial timeout */
timeout.tv_sec = time_out / 1000;
timeout.tv_usec = 1000 * (time_out % 1000);
- for (nread=0; nread<mincnt; )
- {
- FD_ZERO(&fds);
- FD_SET(fd,&fds);
+ for (nread=0; nread < mincnt; )
+ {
+ FD_ZERO(&fds);
+ FD_SET(fd,&fds);
- selrtn = sys_select(fd+1,&fds,&timeout);
+ selrtn = sys_select(fd+1,&fds,&timeout);
- /* Check if error */
- if(selrtn == -1) {
- /* something is wrong. Maybe the socket is dead? */
- smb_read_error = READ_ERROR;
- return -1;
- }
+ /* Check if error */
+ if(selrtn == -1) {
+ /* something is wrong. Maybe the socket is dead? */
+ smb_read_error = READ_ERROR;
+ return -1;
+ }
- /* Did we timeout ? */
- if (selrtn == 0) {
- smb_read_error = READ_TIMEOUT;
- return -1;
- }
+ /* Did we timeout ? */
+ if (selrtn == 0) {
+ smb_read_error = READ_TIMEOUT;
+ return -1;
+ }
#ifdef WITH_SSL
if(fd == sslFd){
readret = read(fd, buf+nread, maxcnt-nread);
#endif /* WITH_SSL */
- if (readret == 0) {
- /* we got EOF on the file descriptor */
- smb_read_error = READ_EOF;
- return -1;
- }
+ if (readret == 0) {
+ /* we got EOF on the file descriptor */
+ smb_read_error = READ_EOF;
+ return -1;
+ }
- if (readret == -1) {
- /* the descriptor is probably dead */
- smb_read_error = READ_ERROR;
- return -1;
- }
-
- nread += readret;
+ if (readret == -1) {
+ /* the descriptor is probably dead */
+ smb_read_error = READ_ERROR;
+ return -1;
}
+
+ nread += readret;
+ }
/* Return the number we got */
- return(nread);
+ return((ssize_t)nread);
}
/*******************************************************************
/****************************************************************************
read data from the client, reading exactly N bytes.
****************************************************************************/
-int read_data(int fd,char *buffer,int N)
+ssize_t read_data(int fd,char *buffer,size_t N)
{
- int ret;
- int total=0;
+ ssize_t ret;
+ size_t total=0;
smb_read_error = 0;
}
total += ret;
}
- return total;
+ return (ssize_t)total;
}
/****************************************************************************
write data to a fd
****************************************************************************/
-int write_data(int fd,char *buffer,int N)
+ssize_t write_data(int fd,char *buffer,size_t N)
{
- int total=0;
- int ret;
+ size_t total=0;
+ ssize_t ret;
while (total < N)
{
total += ret;
}
- return total;
+ return (ssize_t)total;
}
store the result in the buffer
This version of the function will return a length of zero on receiving
a keepalive packet.
+timeout is in milliseconds.
****************************************************************************/
-static int read_smb_length_return_keepalive(int fd,char *inbuf,int timeout)
+static ssize_t read_smb_length_return_keepalive(int fd,char *inbuf,unsigned int timeout)
{
- int len=0, msg_type;
- BOOL ok=False;
+ ssize_t len=0;
+ int msg_type;
+ BOOL ok = False;
while (!ok)
- {
- if (timeout > 0)
- ok = (read_with_timeout(fd,inbuf,4,4,timeout) == 4);
- else
- ok = (read_data(fd,inbuf,4) == 4);
+ {
+ if (timeout > 0)
+ ok = (read_with_timeout(fd,inbuf,4,4,timeout) == 4);
+ else
+ ok = (read_data(fd,inbuf,4) == 4);
- if (!ok)
- return(-1);
+ if (!ok)
+ return(-1);
- len = smb_len(inbuf);
- msg_type = CVAL(inbuf,0);
+ len = smb_len(inbuf);
+ msg_type = CVAL(inbuf,0);
- if (msg_type == 0x85)
- DEBUG(5,("Got keepalive packet\n"));
- }
+ if (msg_type == 0x85)
+ DEBUG(5,("Got keepalive packet\n"));
+ }
DEBUG(10,("got smb length of %d\n",len));
read 4 bytes of a smb packet and return the smb length of the packet
store the result in the buffer. This version of the function will
never return a session keepalive (length of zero).
+timeout is in milliseconds.
****************************************************************************/
-int read_smb_length(int fd,char *inbuf,int timeout)
+ssize_t read_smb_length(int fd,char *inbuf,unsigned int timeout)
{
- int len;
+ ssize_t len;
for(;;)
{
/****************************************************************************
read an smb from a fd. Note that the buffer *MUST* be of size
BUFFER_SIZE+SAFETY_MARGIN.
- The timeout is in milli seconds.
-
+ The timeout is in milliseconds.
This function will return on a
receipt of a session keepalive packet.
****************************************************************************/
-BOOL receive_smb(int fd,char *buffer, int timeout)
+BOOL receive_smb(int fd,char *buffer, unsigned int timeout)
{
- int len,ret;
+ ssize_t len,ret;
smb_read_error = 0;
/****************************************************************************
read an smb from a fd ignoring all keepalive packets. Note that the buffer
*MUST* be of size BUFFER_SIZE+SAFETY_MARGIN.
- The timeout is in milli seconds
+ The timeout is in milliseconds
This is exactly the same as receive_smb except that it never returns
a session keepalive packet (just as receive_smb used to do).
should never go into a blocking read.
****************************************************************************/
-BOOL client_receive_smb(int fd,char *buffer, int timeout)
+BOOL client_receive_smb(int fd,char *buffer, unsigned int timeout)
{
BOOL ret;
return ret;
}
-/****************************************************************************
- read a message from a udp fd.
-The timeout is in milli seconds
-****************************************************************************/
-BOOL receive_local_message(int fd, char *buffer, int buffer_len, int timeout)
-{
- struct sockaddr_in from;
- int fromlen = sizeof(from);
- int32 msg_len = 0;
-
- smb_read_error = 0;
-
- if(timeout != 0)
- {
- struct timeval to;
- fd_set fds;
- int selrtn;
-
- FD_ZERO(&fds);
- FD_SET(fd,&fds);
-
- to.tv_sec = timeout / 1000;
- to.tv_usec = (timeout % 1000) * 1000;
-
- selrtn = sys_select(fd+1,&fds,&to);
-
- /* Check if error */
- if(selrtn == -1)
- {
- /* something is wrong. Maybe the socket is dead? */
- smb_read_error = READ_ERROR;
- return False;
- }
-
- /* Did we timeout ? */
- if (selrtn == 0)
- {
- smb_read_error = READ_TIMEOUT;
- return False;
- }
- }
-
- /*
- * Read a loopback udp message.
- */
- msg_len = recvfrom(fd, &buffer[UDP_CMD_HEADER_LEN],
- buffer_len - UDP_CMD_HEADER_LEN, 0,
- (struct sockaddr *)&from, &fromlen);
-
- if(msg_len < 0)
- {
- DEBUG(0,("receive_local_message. Error in recvfrom. (%s).\n",strerror(errno)));
- return False;
- }
-
- /* Validate message length. */
- if(msg_len > (buffer_len - UDP_CMD_HEADER_LEN))
- {
- DEBUG(0,("receive_local_message: invalid msg_len (%d) max can be %d\n",
- msg_len,
- buffer_len - UDP_CMD_HEADER_LEN));
- return False;
- }
-
- /* Validate message from address (must be localhost). */
- if(from.sin_addr.s_addr != htonl(INADDR_LOOPBACK))
- {
- DEBUG(0,("receive_local_message: invalid 'from' address \
-(was %x should be 127.0.0.1\n", from.sin_addr.s_addr));
- return False;
- }
-
- /* Setup the message header */
- SIVAL(buffer,UDP_CMD_LEN_OFFSET,msg_len);
- SSVAL(buffer,UDP_CMD_PORT_OFFSET,ntohs(from.sin_port));
-
- return True;
-}
-
-/****************************************************************************
- structure to hold a linked list of local messages.
- for processing.
-****************************************************************************/
-
-typedef struct {
- ubi_slNode msg_next;
- char *msg_buf;
- int msg_len;
-} pending_message_list;
-
-static ubi_slList smb_oplock_queue = { NULL, (ubi_slNodePtr)&smb_oplock_queue, 0};
-
-/****************************************************************************
- Function to push a message onto the tail of a linked list of smb messages ready
- for processing.
-****************************************************************************/
-
-static BOOL push_local_message(ubi_slList *list_head, char *buf, int msg_len)
-{
- pending_message_list *msg = (pending_message_list *)
- malloc(sizeof(pending_message_list));
-
- if(msg == NULL)
- {
- DEBUG(0,("push_local_message: malloc fail (1)\n"));
- return False;
- }
-
- msg->msg_buf = (char *)malloc(msg_len);
- if(msg->msg_buf == NULL)
- {
- DEBUG(0,("push_local_message: malloc fail (2)\n"));
- free((char *)msg);
- return False;
- }
-
- memcpy(msg->msg_buf, buf, msg_len);
- msg->msg_len = msg_len;
-
- ubi_slAddTail( list_head, msg);
-
- return True;
-}
-
-/****************************************************************************
- Function to push a smb message onto a linked list of local smb messages ready
- for processing.
-****************************************************************************/
-
-BOOL push_oplock_pending_smb_message(char *buf, int msg_len)
-{
- return push_local_message(&smb_oplock_queue, buf, msg_len);
-}
-
-/****************************************************************************
- Do a select on an two fd's - with timeout.
-
- If a local udp message has been pushed onto the
- queue (this can only happen during oplock break
- processing) return this first.
-
- If a pending smb message has been pushed onto the
- queue (this can only happen during oplock break
- processing) return this next.
-
- If the first smbfd is ready then read an smb from it.
- if the second (loopback UDP) fd is ready then read a message
- from it and setup the buffer header to identify the length
- and from address.
- Returns False on timeout or error.
- Else returns True.
-
-The timeout is in milli seconds
-****************************************************************************/
-BOOL receive_message_or_smb(int smbfd, int oplock_fd,
- char *buffer, int buffer_len,
- int timeout, BOOL *got_smb)
-{
- fd_set fds;
- int selrtn;
- struct timeval to;
-
- smb_read_error = 0;
-
- *got_smb = False;
-
- /*
- * Check to see if we already have a message on the smb queue.
- * If so - copy and return it.
- */
-
- if(ubi_slCount(&smb_oplock_queue) != 0)
- {
- pending_message_list *msg = (pending_message_list *)ubi_slRemHead(&smb_oplock_queue);
- memcpy(buffer, msg->msg_buf, MIN(buffer_len, msg->msg_len));
-
- /* Free the message we just copied. */
- free((char *)msg->msg_buf);
- free((char *)msg);
- *got_smb = True;
-
- DEBUG(5,("receive_message_or_smb: returning queued smb message.\n"));
- return True;
- }
-
- FD_ZERO(&fds);
- FD_SET(smbfd,&fds);
- FD_SET(oplock_fd,&fds);
-
- to.tv_sec = timeout / 1000;
- to.tv_usec = (timeout % 1000) * 1000;
-
- selrtn = sys_select(MAX(smbfd,oplock_fd)+1,&fds,timeout>0?&to:NULL);
-
- /* Check if error */
- if(selrtn == -1) {
- /* something is wrong. Maybe the socket is dead? */
- smb_read_error = READ_ERROR;
- return False;
- }
-
- /* Did we timeout ? */
- if (selrtn == 0) {
- smb_read_error = READ_TIMEOUT;
- return False;
- }
-
- if (FD_ISSET(smbfd,&fds))
- {
- *got_smb = True;
- return receive_smb(smbfd, buffer, 0);
- }
- else
- {
- return receive_local_message(oplock_fd, buffer, buffer_len, 0);
- }
-}
-
/****************************************************************************
send an smb to a fd
****************************************************************************/
BOOL send_smb(int fd,char *buffer)
{
- int len;
- int ret,nwritten=0;
+ size_t len;
+ size_t nwritten=0;
+ ssize_t ret;
len = smb_len(buffer) + 4;
while (nwritten < len)
+ {
+ ret = write_socket(fd,buffer+nwritten,len - nwritten);
+ if (ret <= 0)
{
- ret = write_socket(fd,buffer+nwritten,len - nwritten);
- if (ret <= 0)
- {
- DEBUG(0,("Error writing %d bytes to client. %d. Exiting\n",len,ret));
- close_sockets();
- exit(1);
- }
- nwritten += ret;
+ DEBUG(0,("Error writing %d bytes to client. %d. Exiting\n",len,ret));
+ close_sockets();
+ exit(1);
}
-
+ nwritten += ret;
+ }
return True;
}
return the total storage length of a mangled name
****************************************************************************/
int name_len( char *s )
- {
+{
int len;
/* If the two high bits of the byte are set, return 2. */
}
return( len );
- } /* name_len */
+} /* name_len */
/****************************************************************************
send a single packet to a port on another machine
if (!list) return(False);
- while (next_token(&p,tok,LIST_SEP,sizeof(tok)))
- {
- if (casesensitive) {
- if (strcmp(tok,s) == 0)
- return(True);
- } else {
- if (StrCaseCmp(tok,s) == 0)
- return(True);
- }
+ while (next_token(&p,tok,LIST_SEP,sizeof(tok))) {
+ if (casesensitive) {
+ if (strcmp(tok,s) == 0)
+ return(True);
+ } else {
+ if (StrCaseCmp(tok,s) == 0)
+ return(True);
}
+ }
return(False);
}
/****************************************************************************
expand a pointer to be a particular size
****************************************************************************/
-void *Realloc(void *p,int size)
+void *Realloc(void *p,size_t size)
{
void *ret=NULL;
int ret;
if(lp_ole_locking_compat()) {
-#ifdef LARGE_SMB_OFF_T
- SMB_OFF_T mask = 0xC000000000000000LL;
-#else
- SMB_OFF_T mask = 0xC0000000;
-#endif
+ SMB_OFF_T mask = ((SMB_OFF_T)0xC) << (SMB_OFF_T_BITS-4);
+ SMB_OFF_T mask2= ((SMB_OFF_T)0x3) << (SMB_OFF_T_BITS-4);
/* make sure the count is reasonable, we might kill the lockd otherwise */
count &= ~mask;
still allows OLE2 apps to operate, but should stop lockd from
dieing */
if ((offset & mask) != 0)
-#ifdef LARGE_SMB_OFF_T
- offset = (offset & ~mask) | (((offset & mask) >> 2) & 0x3000000000000000LL);
-#else
- offset = (offset & ~mask) | (((offset & mask) >> 2) & 0x30000000);
-#endif
+ offset = (offset & ~mask) | (((offset & mask) >> 2) & mask2);
} else {
-#ifdef LARGE_SMB_OFF_T
- SMB_OFF_T mask = 0x8000000000000000LL;
-#else
- SMB_OFF_T mask = 0x80000000;
-#endif
+ SMB_OFF_T mask = ((SMB_OFF_T)0x8) << (SMB_OFF_T_BITS-4);
SMB_OFF_T neg_mask = ~mask;
/* interpret negative counts as large numbers */
errno = 0;
ret = fcntl(fd,op,&lock);
+ if (errno == EFBIG)
+ {
+ if( DEBUGLVL( 0 ))
+ {
+ dbgtext("fcntl_lock: WARNING: lock request at offset %.0f, length %.0f returned\n", (double)offset,(double)count);
+ dbgtext("a 'file too large' error. This can happen when using 64 bit lock offsets\n");
+ dbgtext("on 32 bit NFS mounted file systems. Retrying with 32 bit truncated length.\n");
+ }
+ /* 32 bit NFS file system, retry with smaller offset */
+ errno = 0;
+ lock.l_len = count & 0xffffffff;
+ ret = fcntl(fd,op,&lock);
+ }
if (errno != 0)
DEBUG(3,("fcntl lock gave errno %d (%s)\n",errno,strerror(errno)));
/*****************************************************************
zero a memory area then free it. Used to catch bugs faster
*****************************************************************/
-void zero_free(void *p, int size)
+void zero_free(void *p, size_t size)
{
memset(p, 0, size);
free(p);