Makefile: Split definitions for SGI4,5,6.
[kai/samba.git] / source3 / smbd / server.c
index 334edf77d0e824368e912c91d3369c0b72a46aab..2b906a7641715d1c37bf5305e3ac703378eb65f0 100644 (file)
@@ -2,7 +2,7 @@
    Unix SMB/Netbios implementation.
    Version 1.9.
    Main SMB server routines
-   Copyright (C) Andrew Tridgell 1992-1995
+   Copyright (C) Andrew Tridgell 1992-1997
    
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
 pstring servicesf = CONFIGFILE;
 extern pstring debugf;
 extern pstring sesssetup_user;
+extern fstring myworkgroup;
 
 char *InBuffer = NULL;
 char *OutBuffer = NULL;
 char *last_inbuf = NULL;
 
-BOOL share_mode_pending = False;
+int am_parent = 1;
+int atexit_set = 0;
 
 /* the last message the was processed */
 int last_message = -1;
@@ -55,11 +57,26 @@ extern pstring user_socket_options;
 connection_struct Connections[MAX_CONNECTIONS];
 files_struct Files[MAX_OPEN_FILES];
 
-extern int Protocol;
+/*
+ * Indirection for file fd's. Needed as POSIX locking
+ * is based on file/process, not fd/process.
+ */
+file_fd_struct FileFd[MAX_OPEN_FILES];
+int max_file_fd_used = 0;
 
-int maxxmit = BUFFER_SIZE;
+extern int Protocol;
 
-int chain_size = 0;
+/* 
+ * Size of data we can send to client. Set
+ *  by the client for all protocols above CORE.
+ *  Set by us for CORE protocol.
+ */
+int max_send = BUFFER_SIZE;
+/*
+ * Size of the data we can receive. Set by us.
+ * Can be modified by the max xmit parameter.
+ */
+int max_recv = BUFFER_SIZE;
 
 /* a fnum to use when chaining */
 int chain_fnum = -1;
@@ -67,8 +84,17 @@ int chain_fnum = -1;
 /* number of open connections */
 static int num_connections_open = 0;
 
+/* Oplock ipc UDP socket. */
+int oplock_sock = -1;
+uint16 oplock_port = 0;
+/* Current number of oplocks we have outstanding. */
+int32 global_oplocks_open = 0;
+
+BOOL global_oplock_break = False;
+
 extern fstring remote_machine;
 
+pstring OriginalDir;
 
 /* these can be set by some functions to override the error codes */
 int unix_ERR_class=SUCCESS;
@@ -88,7 +114,21 @@ static int find_free_connection(int hash);
 #define IS_DOS_SYSTEM(test_mode) (((test_mode) & aSYSTEM) != 0)
 #define IS_DOS_HIDDEN(test_mode) (((test_mode) & aHIDDEN) != 0)
 
-
+/****************************************************************************
+  when exiting, take the whole family
+****************************************************************************/
+void  *dflt_sig(void)
+{
+  exit_server("caught signal");
+  return 0; /* Keep -Wall happy :-) */
+}
+/****************************************************************************
+  Send a SIGTERM to our process group.
+*****************************************************************************/
+void  killkids(void)
+{
+  if(am_parent) kill(0,SIGTERM);
+}
 
 /****************************************************************************
   change a dos mode to a unix mode
@@ -98,8 +138,12 @@ static int find_free_connection(int hash);
          dos archive is represented in unix by the user's execute bit
          dos system is represented in unix by the group's execute bit
          dos hidden is represented in unix by the other's execute bit
+         Then apply create mask,
+         then add force bits.
     base permission for directories:
          dos directory is represented in unix by unix's dir bit and the exec bit
+         Then apply create mask,
+         then add force bits.
 ****************************************************************************/
 mode_t unix_mode(int cnum,int dosmode)
 {
@@ -108,19 +152,29 @@ mode_t unix_mode(int cnum,int dosmode)
   if ( !IS_DOS_READONLY(dosmode) )
     result |= (S_IWUSR | S_IWGRP | S_IWOTH);
  
-  if (IS_DOS_DIR(dosmode))
+  if (IS_DOS_DIR(dosmode)) {
+    /* We never make directories read only for the owner as under DOS a user
+       can always create a file in a read-only directory. */
     result |= (S_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH | S_IWUSR);
+    /* Apply directory mask */
+    result &= lp_dir_mode(SNUM(cnum));
+    /* Add in force bits */
+    result |= lp_force_dir_mode(SNUM(cnum));
+  } else { 
+    if (MAP_ARCHIVE(cnum) && IS_DOS_ARCHIVE(dosmode))
+      result |= S_IXUSR;
+
+    if (MAP_SYSTEM(cnum) && IS_DOS_SYSTEM(dosmode))
+      result |= S_IXGRP;
  
-  if (MAP_ARCHIVE(cnum) && IS_DOS_ARCHIVE(dosmode))
-    result |= S_IXUSR;
-
-  if (MAP_SYSTEM(cnum) && IS_DOS_SYSTEM(dosmode))
-    result |= S_IXGRP;
-  if (MAP_HIDDEN(cnum) && IS_DOS_HIDDEN(dosmode))
-    result |= S_IXOTH;  
+    if (MAP_HIDDEN(cnum) && IS_DOS_HIDDEN(dosmode))
+      result |= S_IXOTH;  
  
-  result &= CREATE_MODE(cnum);
+    /* Apply mode mask */
+    result &= lp_create_mode(SNUM(cnum));
+    /* Add in force bits */
+    result |= lp_force_create_mode(SNUM(cnum));
+  }
   return(result);
 }
 
@@ -133,6 +187,8 @@ int dos_mode(int cnum,char *path,struct stat *sbuf)
   int result = 0;
   extern struct current_user current_user;
 
+  DEBUG(8,("dos_mode: %d %s\n", cnum, path));
+
   if (CAN_WRITE(cnum) && !lp_alternate_permissions(SNUM(cnum))) {
     if (!((sbuf->st_mode & S_IWOTH) ||
          Connections[cnum].admin_user ||
@@ -146,7 +202,7 @@ int dos_mode(int cnum,char *path,struct stat *sbuf)
       result |= aRONLY;
   }
 
-  if ((sbuf->st_mode & S_IXUSR) != 0)
+  if (MAP_ARCHIVE(cnum) && ((sbuf->st_mode & S_IXUSR) != 0))
     result |= aARCH;
 
   if (MAP_SYSTEM(cnum) && ((sbuf->st_mode & S_IXGRP) != 0))
@@ -176,6 +232,23 @@ int dos_mode(int cnum,char *path,struct stat *sbuf)
        result |= aHIDDEN;
     }
 
+  /* Optimization : Only call is_hidden_path if it's not already
+     hidden. */
+  if (!(result & aHIDDEN) && IS_HIDDEN_PATH(cnum,path))
+  {
+    result |= aHIDDEN;
+  }
+
+  DEBUG(8,("dos_mode returning "));
+
+  if (result & aHIDDEN) DEBUG(8, ("h"));
+  if (result & aRONLY ) DEBUG(8, ("r"));
+  if (result & aSYSTEM) DEBUG(8, ("s"));
+  if (result & aDIR   ) DEBUG(8, ("d"));
+  if (result & aARCH  ) DEBUG(8, ("a"));
+
+  DEBUG(8,("\n"));
+
   return(result);
 }
 
@@ -278,7 +351,7 @@ static BOOL mangled_equal(char *name1, char *name2)
 {
   pstring tmpname;
 
-  if (is_8_3(name2))
+  if (is_8_3(name2, True))
     return(False);
 
   strcpy(tmpname,name2);
@@ -293,7 +366,7 @@ scan a directory to find a filename, matching without case sensitivity
 
 If the name looks like a mangled name then try via the mangling functions
 ****************************************************************************/
-static BOOL scan_directory(char *path, char *name,int snum,BOOL docache)
+static BOOL scan_directory(char *path, char *name,int cnum,BOOL docache)
 {
   void *cur_dir;
   char *dname;
@@ -306,7 +379,7 @@ static BOOL scan_directory(char *path, char *name,int snum,BOOL docache)
   if (*path == 0)
     path = ".";
 
-  if (docache && (dname = DirCacheCheck(path,name,snum))) {
+  if (docache && (dname = DirCacheCheck(path,name,SNUM(cnum)))) {
     strcpy(name, dname);       
     return(True);
   }      
@@ -315,7 +388,7 @@ static BOOL scan_directory(char *path, char *name,int snum,BOOL docache)
     check_mangled_stack(name);
 
   /* open the directory */
-  if (!(cur_dir = OpenDir(path))) 
+  if (!(cur_dir = OpenDir(cnum, path, True))) 
     {
       DEBUG(3,("scan dir didn't open dir [%s]\n",path));
       return(False);
@@ -328,14 +401,14 @@ static BOOL scan_directory(char *path, char *name,int snum,BOOL docache)
          (strequal(dname,".") || strequal(dname,"..")))
        continue;
 
-      strcpy(name2,dname);
-      if (!name_map_mangle(name2,False,snum)) continue;
+      pstrcpy(name2,dname);
+      if (!name_map_mangle(name2,False,SNUM(cnum))) continue;
 
       if ((mangled && mangled_equal(name,name2))
-         || fname_equal(name, name2))
+         || fname_equal(name, name2)) /* name2 here was changed to dname - since 1.9.16p2 - not sure of reason (jra) */
        {
          /* we've found the file, change it's name and return */
-         if (docache) DirCacheAdd(path,name,dname,snum);
+         if (docache) DirCacheAdd(path,name,dname,SNUM(cnum));
          strcpy(name, dname);
          CloseDir(cur_dir);
          return(True);
@@ -356,31 +429,57 @@ for this service.
 
 The function will return False if some part of the name except for the last
 part cannot be resolved
+
+If the saved_last_component != 0, then the unmodified last component
+of the pathname is returned there. This is used in an exceptional
+case in reply_mv (so far). If saved_last_component == 0 then nothing
+is returned there.
+
+The bad_path arg is set to True if the filename walk failed. This is
+used to pick the correct error code to return between ENOENT and ENOTDIR
+as Windows applications depend on ERRbadpath being returned if a component
+of a pathname does not exist.
 ****************************************************************************/
-BOOL unix_convert(char *name,int cnum)
+BOOL unix_convert(char *name,int cnum,pstring saved_last_component, BOOL *bad_path)
 {
   struct stat st;
   char *start, *end;
   pstring dirpath;
+  int saved_errno;
 
   *dirpath = 0;
+  *bad_path = False;
+
+  if(saved_last_component)
+    *saved_last_component = 0;
 
   /* convert to basic unix format - removing \ chars and cleaning it up */
   unix_format(name);
   unix_clean_name(name);
 
-  if (!case_sensitive && 
-      (!case_preserve || (is_8_3(name) && !short_case_preserve)))
-    strnorm(name);
-
   /* names must be relative to the root of the service - trim any leading /.
    also trim trailing /'s */
   trim_string(name,"/","/");
 
+  /*
+   * Ensure saved_last_component is valid even if file exists.
+   */
+  if(saved_last_component) {
+    end = strrchr(name, '/');
+    if(end)
+      strcpy(saved_last_component, end + 1);
+    else
+      strcpy(saved_last_component, name);
+  }
+
+  if (!case_sensitive && 
+      (!case_preserve || (is_8_3(name, False) && !short_case_preserve)))
+    strnorm(name);
+
   /* check if it's a printer file */
   if (Connections[cnum].printer)
     {
-      if ((! *name) || strchr(name,'/') || !is_8_3(name))
+      if ((! *name) || strchr(name,'/') || !is_8_3(name, True))
        {
          char *s;
          fstring name2;
@@ -397,12 +496,14 @@ BOOL unix_convert(char *name,int cnum)
   if (sys_stat(name,&st) == 0)
     return(True);
 
+  saved_errno = errno;
+
   DEBUG(5,("unix_convert(%s,%d)\n",name,cnum));
 
   /* a special case - if we don't have any mangling chars and are case
      sensitive then searching won't help */
   if (case_sensitive && !is_mangled(name) && 
-      !lp_strip_dot() && !use_mangled_map)
+      !lp_strip_dot() && !use_mangled_map && (saved_errno != ENOENT))
     return(False);
 
   /* now we need to recursively match the name against the real 
@@ -420,7 +521,10 @@ BOOL unix_convert(char *name,int cnum)
       end = strchr(start, '/');
 
       /* chop the name at this point */
-      if (end) *end = 0;
+      if (end)         *end = 0;
+
+      if(saved_last_component != 0)
+        strcpy(saved_last_component, end ? end + 1 : start);
 
       /* check if the name exists up to this point */
       if (sys_stat(name, &st) == 0) 
@@ -443,18 +547,24 @@ BOOL unix_convert(char *name,int cnum)
 
          /* remember the rest of the pathname so it can be restored
             later */
-         if (end) strcpy(rest,end+1);
-
+         if (end) pstrcpy(rest,end+1);
 
          /* try to find this part of the path in the directory */
          if (strchr(start,'?') || strchr(start,'*') ||
-             !scan_directory(dirpath, start, SNUM(cnum), end?True:False))
+             !scan_directory(dirpath, start, cnum, end?True:False))
            {
              if (end) 
                {
                  /* an intermediate part of the name can't be found */
                  DEBUG(5,("Intermediate not found %s\n",start));
                  *end = '/';
+                  /* We need to return the fact that the intermediate
+                     name resolution failed. This is used to return an
+                     error of ERRbadpath rather than ERRbadfile. Some
+                     Windows applications depend on the difference between
+                     these two errors.
+                   */
+                  *bad_path = True;
                  return(False);
                }
              
@@ -537,6 +647,14 @@ static void disk_norm(int *bsize,int *dfree,int *dsize)
 int disk_free(char *path,int *bsize,int *dfree,int *dsize)
 {
   char *df_command = lp_dfree_command();
+  int dfree_retval;
+#ifdef QUOTAS
+  int dfreeq_retval;
+  int dfreeq = 0;
+  int bsizeq = *bsize;
+  int dsizeq = *dsize;
+#endif
+
 #ifndef NO_STATFS
 #ifdef USE_STATVFS
   struct statvfs fs;
@@ -549,15 +667,6 @@ int disk_free(char *path,int *bsize,int *dfree,int *dsize)
 #endif
 #endif
 
-#ifdef QUOTAS
-  if (disk_quotas(path, bsize, dfree, dsize))
-    {
-      disk_norm(bsize,dfree,dsize);
-      return(((*bsize)/1024)*(*dfree));
-    }
-#endif
-
-
   /* possibly use system() to get the result */
   if (df_command && *df_command)
     {
@@ -565,30 +674,58 @@ int disk_free(char *path,int *bsize,int *dfree,int *dsize)
       pstring syscmd;
       pstring outfile;
          
-      sprintf(outfile,"/tmp/dfree.smb.%d",(int)getpid());
+      sprintf(outfile,"%s/dfree.smb.%d",tmpdir(),(int)getpid());
       sprintf(syscmd,"%s %s",df_command,path);
       standard_sub_basic(syscmd);
 
-      ret = smbrun(syscmd,outfile);
+      ret = smbrun(syscmd,outfile,False);
       DEBUG(3,("Running the command `%s' gave %d\n",syscmd,ret));
          
       {
-       FILE *f = fopen(outfile,"r");   
-       *dsize = 0;
-       *dfree = 0;
-       *bsize = 1024;
-       if (f)
-         {
-           fscanf(f,"%d %d %d",dsize,dfree,bsize);
-           fclose(f);
-         }
-       else
-         DEBUG(0,("Can't open %s\n",outfile));
+        FILE *f = fopen(outfile,"r");  
+        *dsize = 0;
+        *dfree = 0;
+        *bsize = 1024;
+        if (f)
+          {
+            fscanf(f,"%d %d %d",dsize,dfree,bsize);
+            fclose(f);
+          }
+        else
+          DEBUG(0,("Can't open %s\n",outfile));
       }
          
       unlink(outfile);
       disk_norm(bsize,dfree,dsize);
-      return(((*bsize)/1024)*(*dfree));
+      dfree_retval = ((*bsize)/1024)*(*dfree);
+#ifdef QUOTAS
+      /* Ensure we return the min value between the users quota and
+         what's free on the disk. Thanks to Albrecht Gebhardt 
+         <albrecht.gebhardt@uni-klu.ac.at> for this fix.
+      */
+      if (disk_quotas(path, &bsizeq, &dfreeq, &dsizeq))
+        {
+          disk_norm(&bsizeq, &dfreeq, &dsizeq);
+          dfreeq_retval = ((bsizeq)/1024)*(dfreeq);
+          dfree_retval =  ( dfree_retval < dfreeq_retval ) ? 
+                           dfree_retval : dfreeq_retval ;
+          /* maybe dfree and dfreeq are calculated using different bsizes 
+             so convert dfree from bsize into bsizeq */
+          /* avoid overflows due to multiplication, so do not:
+                *dfree = ((*dfree) * (*bsize)) / (bsizeq); 
+             bsize and bsizeq are powers of 2 so its better to
+             to divide them getting a multiplication or division factor
+             for dfree. Rene Nieuwenhuizen (07-10-1997) */
+          if (*bsize >= bsizeq) 
+            *dfree = *dfree * (*bsize / bsizeq);
+          else 
+            *dfree = *dfree / (bsizeq / *bsize);
+          *dfree = ( *dfree < dfreeq ) ? *dfree : dfreeq ; 
+          *bsize = bsizeq;
+          *dsize = dsizeq;
+        }
+#endif
+      return(dfree_retval);
     }
 
 #ifdef NO_STATFS
@@ -658,7 +795,35 @@ if ((*bsize) < 512 || (*bsize)>0xFFFF) *bsize = 1024;
       *dsize = 20*1024*1024/(*bsize);
       *dfree = MAX(1,*dfree);
     }
-  return(((*bsize)/1024)*(*dfree));
+  dfree_retval = ((*bsize)/1024)*(*dfree);
+#ifdef QUOTAS
+  /* Ensure we return the min value between the users quota and
+     what's free on the disk. Thanks to Albrecht Gebhardt 
+     <albrecht.gebhardt@uni-klu.ac.at> for this fix.
+  */
+  if (disk_quotas(path, &bsizeq, &dfreeq, &dsizeq))
+    {
+      disk_norm(&bsizeq, &dfreeq, &dsizeq);
+      dfreeq_retval = ((bsizeq)/1024)*(dfreeq);
+      dfree_retval = ( dfree_retval < dfreeq_retval ) ? 
+                       dfree_retval : dfreeq_retval ;
+      /* maybe dfree and dfreeq are calculated using different bsizes 
+         so convert dfree from bsize into bsizeq */
+      /* avoid overflows due to multiplication, so do not:
+              *dfree = ((*dfree) * (*bsize)) / (bsizeq); 
+       bsize and bsizeq are powers of 2 so its better to
+       to divide them getting a multiplication or division factor
+       for dfree. Rene Nieuwenhuizen (07-10-1997) */
+      if (*bsize >= bsizeq)
+        *dfree = *dfree * (*bsize / bsizeq);
+      else
+        *dfree = *dfree / (bsizeq / *bsize);
+      *dfree = ( *dfree < dfreeq ) ? *dfree : dfreeq ;
+      *bsize = bsizeq;
+      *dsize = dsizeq;
+    }
+#endif
+  return(dfree_retval);
 #endif
 }
 
@@ -686,7 +851,29 @@ BOOL check_name(char *name,int cnum)
 
   errno = 0;
 
+  if( IS_VETO_PATH(cnum, name)) 
+    {
+      DEBUG(5,("file path name %s vetoed\n",name));
+      return(0);
+    }
+
   ret = reduce_name(name,Connections[cnum].connectpath,lp_widelinks(SNUM(cnum)));
+
+  /* Check if we are allowing users to follow symlinks */
+  /* Patch from David Clerc <David.Clerc@cui.unige.ch>
+     University of Geneva */
+
+  if (!lp_symlinks(SNUM(cnum)))
+    {
+      struct stat statbuf;
+      if ( (sys_lstat(name,&statbuf) != -1) &&
+          (S_ISLNK(statbuf.st_mode)) )
+        {
+          DEBUG(3,("check_name: denied: file path name %s is a symlink\n",name));
+          ret=0; 
+        }
+    }
+
   if (!ret)
     DEBUG(5,("check_name on %s failed\n",name));
 
@@ -710,19 +897,185 @@ static void check_for_pipe(char *fname)
     }
 }
 
+/****************************************************************************
+fd support routines - attempt to do a sys_open
+****************************************************************************/
+static int fd_attempt_open(char *fname, int flags, int mode)
+{
+  int fd = sys_open(fname,flags,mode);
+
+  /* Fix for files ending in '.' */
+  if((fd == -1) && (errno == ENOENT) &&
+     (strchr(fname,'.')==NULL))
+    {
+      strcat(fname,".");
+      fd = sys_open(fname,flags,mode);
+    }
+
+#if (defined(ENAMETOOLONG) && defined(HAVE_PATHCONF))
+  if ((fd == -1) && (errno == ENAMETOOLONG))
+    {
+      int max_len;
+      char *p = strrchr(fname, '/');
+
+      if (p == fname)   /* name is "/xxx" */
+        {
+          max_len = pathconf("/", _PC_NAME_MAX);
+          p++;
+        }
+      else if ((p == NULL) || (p == fname))
+        {
+          p = fname;
+          max_len = pathconf(".", _PC_NAME_MAX);
+        }
+      else
+        {
+          *p = '\0';
+          max_len = pathconf(fname, _PC_NAME_MAX);
+          *p = '/';
+          p++;
+        }
+      if (strlen(p) > max_len)
+        {
+          char tmp = p[max_len];
+
+          p[max_len] = '\0';
+          if ((fd = sys_open(fname,flags,mode)) == -1)
+            p[max_len] = tmp;
+        }
+    }
+#endif
+  return fd;
+}
+
+/****************************************************************************
+fd support routines - attempt to find an already open file by dev
+and inode - increments the ref_count of the returned file_fd_struct *.
+****************************************************************************/
+static file_fd_struct *fd_get_already_open(struct stat *sbuf)
+{
+  int i;
+  file_fd_struct *fd_ptr;
+
+  if(sbuf == 0)
+    return 0;
+
+  for(i = 0; i <= max_file_fd_used; i++) {
+    fd_ptr = &FileFd[i];
+    if((fd_ptr->ref_count > 0) &&
+       (((uint32)sbuf->st_dev) == fd_ptr->dev) &&
+       (((uint32)sbuf->st_ino) == fd_ptr->inode)) {
+      fd_ptr->ref_count++;
+      DEBUG(3,
+       ("Re-used file_fd_struct %d, dev = %x, inode = %x, ref_count = %d\n",
+        i, fd_ptr->dev, fd_ptr->inode, fd_ptr->ref_count));
+      return fd_ptr;
+    }
+  }
+  return 0;
+}
+
+/****************************************************************************
+fd support routines - attempt to find a empty slot in the FileFd array.
+Increments the ref_count of the returned entry.
+****************************************************************************/
+static file_fd_struct *fd_get_new()
+{
+  int i;
+  file_fd_struct *fd_ptr;
+
+  for(i = 0; i < MAX_OPEN_FILES; i++) {
+    fd_ptr = &FileFd[i];
+    if(fd_ptr->ref_count == 0) {
+      fd_ptr->dev = (uint32)-1;
+      fd_ptr->inode = (uint32)-1;
+      fd_ptr->fd = -1;
+      fd_ptr->fd_readonly = -1;
+      fd_ptr->fd_writeonly = -1;
+      fd_ptr->real_open_flags = -1;
+      fd_ptr->ref_count++;
+      /* Increment max used counter if neccessary, cuts down
+        on search time when re-using */
+      if(i > max_file_fd_used)
+        max_file_fd_used = i;
+      DEBUG(3,("Allocated new file_fd_struct %d, dev = %x, inode = %x\n",
+               i, fd_ptr->dev, fd_ptr->inode));
+      return fd_ptr;
+    }
+  }
+  DEBUG(1,("ERROR! Out of file_fd structures - perhaps increase MAX_OPEN_FILES?\
+n"));
+  return 0;
+}
+
+/****************************************************************************
+fd support routines - attempt to re-open an already open fd as O_RDWR.
+Save the already open fd (we cannot close due to POSIX file locking braindamage.
+****************************************************************************/
+static void fd_attempt_reopen(char *fname, int mode, file_fd_struct *fd_ptr)
+{
+  int fd = sys_open( fname, O_RDWR, mode);
+
+  if(fd == -1)
+    return;
+
+  if(fd_ptr->real_open_flags == O_RDONLY)
+    fd_ptr->fd_readonly = fd_ptr->fd;
+  if(fd_ptr->real_open_flags == O_WRONLY)
+    fd_ptr->fd_writeonly = fd_ptr->fd;
+
+  fd_ptr->fd = fd;
+  fd_ptr->real_open_flags = O_RDWR;
+}
+
+/****************************************************************************
+fd support routines - attempt to close the file referenced by this fd.
+Decrements the ref_count and returns it.
+****************************************************************************/
+static int fd_attempt_close(file_fd_struct *fd_ptr)
+{
+  DEBUG(3,("fd_attempt_close on file_fd_struct %d, fd = %d, dev = %x, inode = %x, open_flags = %d, ref_count = %d.\n",
+          fd_ptr - &FileFd[0],
+          fd_ptr->fd, fd_ptr->dev, fd_ptr->inode,
+          fd_ptr->real_open_flags,
+          fd_ptr->ref_count));
+  if(fd_ptr->ref_count > 0) {
+    fd_ptr->ref_count--;
+    if(fd_ptr->ref_count == 0) {
+      if(fd_ptr->fd != -1)
+        close(fd_ptr->fd);
+      if(fd_ptr->fd_readonly != -1)
+       close(fd_ptr->fd_readonly);
+      if(fd_ptr->fd_writeonly != -1)
+       close(fd_ptr->fd_writeonly);
+      fd_ptr->fd = -1;
+      fd_ptr->fd_readonly = -1;
+      fd_ptr->fd_writeonly = -1;
+      fd_ptr->real_open_flags = -1;
+      fd_ptr->dev = (uint32)-1;
+      fd_ptr->inode = (uint32)-1;
+    }
+  } 
+ return fd_ptr->ref_count;
+}
 
 /****************************************************************************
 open a file
 ****************************************************************************/
-void open_file(int fnum,int cnum,char *fname1,int flags,int mode)
+static void open_file(int fnum,int cnum,char *fname1,int flags,int mode, struct stat *sbuf)
 {
+  extern struct current_user current_user;
   pstring fname;
+  struct stat statbuf;
+  file_fd_struct *fd_ptr;
+  files_struct *fsp = &Files[fnum];
 
-  Files[fnum].open = False;
-  Files[fnum].fd = -1;
+  fsp->open = False;
+  fsp->fd_ptr = 0;
+  fsp->granted_oplock = False;
   errno = EPERM;
 
-  strcpy(fname,fname1);
+  pstrcpy(fname,fname1);
 
   /* check permissions */
   if ((flags != O_RDONLY) && !CAN_WRITE(cnum) && !Connections[cnum].printer)
@@ -749,100 +1102,170 @@ void open_file(int fnum,int cnum,char *fname1,int flags,int mode)
     sys_unlink(fname);
 #endif
 
+  /*
+   * Ensure we have a valid struct stat so we can search the
+   * open fd table.
+   */
+  if(sbuf == 0) {
+    if(stat(fname, &statbuf) < 0) {
+      if(errno != ENOENT) {
+        DEBUG(3,("Error doing stat on file %s (%s)\n",
+                 fname,strerror(errno)));
+
+        check_for_pipe(fname);
+        return;
+      }
+      sbuf = 0;
+    } else {
+      sbuf = &statbuf;
+    }
+  }
+
+  /*
+   * Check to see if we have this file already
+   * open. If we do, just use the already open fd and increment the
+   * reference count (fd_get_already_open increments the ref_count).
+   */
+  if((fd_ptr = fd_get_already_open(sbuf))!= 0) {
+
+    int accmode = (flags & (O_RDONLY | O_WRONLY | O_RDWR));
+
+    /* File was already open. */
+    if((flags & O_CREAT) && (flags & O_EXCL)) {
+      fd_ptr->ref_count--;
+      errno = EEXIST;
+      return;
+    }
+
+    /* 
+     * If not opened O_RDWR try
+     * and do that here - a chmod may have been done
+     * between the last open and now. 
+     */
+    if(fd_ptr->real_open_flags != O_RDWR)
+      fd_attempt_reopen(fname, mode, fd_ptr);
+
+    /*
+     * Ensure that if we wanted write access
+     * it has been opened for write, and if we wanted read it
+     * was open for read. 
+     */
+    if(((accmode == O_WRONLY) && (fd_ptr->real_open_flags == O_RDONLY)) ||
+       ((accmode == O_RDONLY) && (fd_ptr->real_open_flags == O_WRONLY)) ||
+       ((accmode == O_RDWR) && (fd_ptr->real_open_flags != O_RDWR))) {
+      DEBUG(3,("Error opening (already open for flags=%d) file %s (%s) (flags=%d)\n",
+               fd_ptr->real_open_flags, fname,strerror(EACCES),flags));
+      check_for_pipe(fname);
+      fd_ptr->ref_count--;
+      return;
+    }
 
-  Files[fnum].fd = sys_open(fname,flags,mode);
+  } else {
+    int open_flags;
+    /* We need to allocate a new file_fd_struct (this increments the
+       ref_count). */
+    if((fd_ptr = fd_get_new()) == 0)
+      return;
+    /*
+     * Whatever the requested flags, attempt read/write access,
+     * as we don't know what flags future file opens may require.
+     * If this fails, try again with the required flags. 
+     * Even if we open read/write when only read access was 
+     * requested the setting of the can_write flag in
+     * the file_struct will protect us from errant
+     * write requests. We never need to worry about O_APPEND
+     * as this is not set anywhere in Samba.
+     */
+    fd_ptr->real_open_flags = O_RDWR;
+    /* Set the flags as needed without the read/write modes. */
+    open_flags = flags & ~(O_RDWR|O_WRONLY|O_RDONLY);
+    fd_ptr->fd = fd_attempt_open(fname, open_flags|O_RDWR, mode);
+    /*
+     * On some systems opening a file for R/W access on a read only
+     * filesystems sets errno to EROFS.
+     */
+#ifdef EROFS
+    if((fd_ptr->fd == -1) && ((errno == EACCES) || (errno == EROFS))) {
+#else /* No EROFS */
+    if((fd_ptr->fd == -1) && (errno == EACCES)) {
+#endif /* EROFS */
+      if(flags & O_WRONLY) {
+        fd_ptr->fd = fd_attempt_open(fname, open_flags|O_WRONLY, mode);
+        fd_ptr->real_open_flags = O_WRONLY;
+      } else {
+       fd_ptr->fd = fd_attempt_open(fname, open_flags|O_RDONLY, mode);
+        fd_ptr->real_open_flags = O_RDONLY;
+      }
+    }
+  }
 
-  if ((Files[fnum].fd>=0) && 
+  if ((fd_ptr->fd >=0) && 
       Connections[cnum].printer && lp_minprintspace(SNUM(cnum))) {
     pstring dname;
     int dum1,dum2,dum3;
     char *p;
-    strcpy(dname,fname);
+    pstrcpy(dname,fname);
     p = strrchr(dname,'/');
     if (p) *p = 0;
     if (sys_disk_free(dname,&dum1,&dum2,&dum3) < 
        lp_minprintspace(SNUM(cnum))) {
-      close(Files[fnum].fd);
-      Files[fnum].fd = -1;
-      sys_unlink(fname);
+      fd_attempt_close(fd_ptr);
+      fsp->fd_ptr = 0;
+      if(fd_ptr->ref_count == 0)
+        sys_unlink(fname);
       errno = ENOSPC;
       return;
     }
   }
     
-
-  /* Fix for files ending in '.' */
-  if((Files[fnum].fd == -1) && (errno == ENOENT) && 
-     (strchr(fname,'.')==NULL))
-    {
-      strcat(fname,".");
-      Files[fnum].fd = sys_open(fname,flags,mode);
-    }
-
-#if (defined(ENAMETOOLONG) && defined(HAVE_PATHCONF))
-  if ((Files[fnum].fd == -1) && (errno == ENAMETOOLONG))
-    {
-      int max_len;
-      char *p = strrchr(fname, '/');
-
-      if (p == fname)  /* name is "/xxx" */
-       {
-         max_len = pathconf("/", _PC_NAME_MAX);
-         p++;
-       }
-      else if ((p == NULL) || (p == fname))
-       {
-         p = fname;
-         max_len = pathconf(".", _PC_NAME_MAX);
-       }
-      else
-       {
-         *p = '\0';
-         max_len = pathconf(fname, _PC_NAME_MAX);
-         *p = '/';
-         p++;
-       }
-      if (strlen(p) > max_len)
-       {
-         char tmp = p[max_len];
-
-         p[max_len] = '\0';
-         if ((Files[fnum].fd = sys_open(fname,flags,mode)) == -1)
-           p[max_len] = tmp;
-       }
-    }
-#endif
-
-  if (Files[fnum].fd < 0)
+  if (fd_ptr->fd < 0)
     {
       DEBUG(3,("Error opening file %s (%s) (flags=%d)\n",
               fname,strerror(errno),flags));
+      /* Ensure the ref_count is decremented. */
+      fd_attempt_close(fd_ptr);
       check_for_pipe(fname);
       return;
     }
 
-  if (Files[fnum].fd >= 0)
+  if (fd_ptr->fd >= 0)
     {
-      struct stat st;
+      if(sbuf == 0) {
+        /* Do the fstat */
+        if(fstat(fd_ptr->fd, &statbuf) == -1) {
+          /* Error - backout !! */
+          DEBUG(3,("Error doing fstat on fd %d, file %s (%s)\n",
+                   fd_ptr->fd, fname,strerror(errno)));
+          /* Ensure the ref_count is decremented. */
+          fd_attempt_close(fd_ptr);
+          return;
+        }
+        sbuf = &statbuf;
+      }
+      /* Set the correct entries in fd_ptr. */
+      fd_ptr->dev = (uint32)sbuf->st_dev;
+      fd_ptr->inode = (uint32)sbuf->st_ino;
+
+      fsp->fd_ptr = fd_ptr;
       Connections[cnum].num_files_open++;
-      fstat(Files[fnum].fd,&st);
-      Files[fnum].mode = st.st_mode;
-      Files[fnum].open_time = time(NULL);
-      Files[fnum].size = 0;
-      Files[fnum].pos = -1;
-      Files[fnum].open = True;
-      Files[fnum].mmap_ptr = NULL;
-      Files[fnum].mmap_size = 0;
-      Files[fnum].can_lock = True;
-      Files[fnum].can_read = ((flags & O_WRONLY)==0);
-      Files[fnum].can_write = ((flags & (O_WRONLY|O_RDWR))!=0);
-      Files[fnum].share_mode = 0;
-      Files[fnum].share_pending = False;
-      Files[fnum].print_file = Connections[cnum].printer;
-      Files[fnum].modified = False;
-      Files[fnum].cnum = cnum;
-      string_set(&Files[fnum].name,fname);
-      Files[fnum].wbmpx_ptr = NULL;      
+      fsp->mode = sbuf->st_mode;
+      GetTimeOfDay(&fsp->open_time);
+      fsp->uid = current_user.id;
+      fsp->size = 0;
+      fsp->pos = -1;
+      fsp->open = True;
+      fsp->mmap_ptr = NULL;
+      fsp->mmap_size = 0;
+      fsp->can_lock = True;
+      fsp->can_read = ((flags & O_WRONLY)==0);
+      fsp->can_write = ((flags & (O_WRONLY|O_RDWR))!=0);
+      fsp->share_mode = 0;
+      fsp->print_file = Connections[cnum].printer;
+      fsp->modified = False;
+      fsp->granted_oplock = False;
+      fsp->cnum = cnum;
+      string_set(&fsp->name,dos_to_unix(fname,False));
+      fsp->wbmpx_ptr = NULL;      
 
       /*
        * If the printer is marked as postscript output a leading
@@ -851,8 +1274,8 @@ void open_file(int fnum,int cnum,char *fname1,int flags,int mode)
        * This has a similar effect as CtrlD=0 in WIN.INI file.
        * tim@fsg.com 09/06/94
        */
-      if (Files[fnum].print_file && POSTSCRIPT(cnum) && 
-         Files[fnum].can_write) 
+      if (fsp->print_file && POSTSCRIPT(cnum) && 
+         fsp->can_write) 
        {
          DEBUG(3,("Writing postscript line\n"));
          write_file(fnum,"%!\n",3);
@@ -860,23 +1283,23 @@ void open_file(int fnum,int cnum,char *fname1,int flags,int mode)
       
       DEBUG(2,("%s %s opened file %s read=%s write=%s (numopen=%d fnum=%d)\n",
               timestring(),Connections[cnum].user,fname,
-              BOOLSTR(Files[fnum].can_read),BOOLSTR(Files[fnum].can_write),
+              BOOLSTR(fsp->can_read),BOOLSTR(fsp->can_write),
               Connections[cnum].num_files_open,fnum));
 
     }
 
 #if USE_MMAP
   /* mmap it if read-only */
-  if (!Files[fnum].can_write)
+  if (!fsp->can_write)
     {
-      Files[fnum].mmap_size = file_size(fname);
-      Files[fnum].mmap_ptr = (char *)mmap(NULL,Files[fnum].mmap_size,
-                                         PROT_READ,MAP_SHARED,Files[fnum].fd,0);
+      fsp->mmap_size = file_size(fname);
+      fsp->mmap_ptr = (char *)mmap(NULL,fsp->mmap_size,
+                                         PROT_READ,MAP_SHARED,fsp->fd_ptr->fd,0);
 
-      if (Files[fnum].mmap_ptr == (char *)-1 || !Files[fnum].mmap_ptr)
+      if (fsp->mmap_ptr == (char *)-1 || !fsp->mmap_ptr)
        {
          DEBUG(3,("Failed to mmap() %s - %s\n",fname,strerror(errno)));
-         Files[fnum].mmap_ptr = NULL;
+         fsp->mmap_ptr = NULL;
        }
     }
 #endif
@@ -888,7 +1311,7 @@ sync a file
 void sync_file(int fnum)
 {
 #ifndef NO_FSYNC
-  fsync(Files[fnum].fd);
+  fsync(Files[fnum].fd_ptr->fd);
 #endif
 }
 
@@ -917,15 +1340,15 @@ static void check_magic(int fnum,int cnum)
     int ret;
     pstring magic_output;
     pstring fname;
-    strcpy(fname,Files[fnum].name);
+    pstrcpy(fname,Files[fnum].name);
 
     if (*lp_magicoutput(SNUM(cnum)))
-      strcpy(magic_output,lp_magicoutput(SNUM(cnum)));
+      pstrcpy(magic_output,lp_magicoutput(SNUM(cnum)));
     else
       sprintf(magic_output,"%s.out",fname);
 
     chmod(fname,0755);
-    ret = smbrun(fname,magic_output);
+    ret = smbrun(fname,magic_output,False);
     DEBUG(3,("Invoking magic command %s gave %d\n",fname,ret));
     unlink(fname);
   }
@@ -937,38 +1360,49 @@ close a file - possibly invalidating the read prediction
 ****************************************************************************/
 void close_file(int fnum)
 {
-  int cnum = Files[fnum].cnum;
-  invalidate_read_prediction(Files[fnum].fd);
-  Files[fnum].open = False;
+  files_struct *fs_p = &Files[fnum];
+  int cnum = fs_p->cnum;
+  uint32 dev = fs_p->fd_ptr->dev;
+  uint32 inode = fs_p->fd_ptr->inode;
+  share_lock_token token;
+
+  invalidate_read_prediction(fs_p->fd_ptr->fd);
+  fs_p->open = False;
   Connections[cnum].num_files_open--;
-  if(Files[fnum].wbmpx_ptr) 
-    {
-      free((char *)Files[fnum].wbmpx_ptr);
-      Files[fnum].wbmpx_ptr = NULL;
-    }
+  if(fs_p->wbmpx_ptr) 
+  {
+    free((char *)fs_p->wbmpx_ptr);
+    fs_p->wbmpx_ptr = NULL;
+  }
 
 #if USE_MMAP
-  if(Files[fnum].mmap_ptr) 
-    {
-      munmap(Files[fnum].mmap_ptr,Files[fnum].mmap_size);
-      Files[fnum].mmap_ptr = NULL;
-    }
+  if(fs_p->mmap_ptr) 
+  {
+    munmap(fs_p->mmap_ptr,fs_p->mmap_size);
+    fs_p->mmap_ptr = NULL;
+  }
 #endif
 
   if (lp_share_modes(SNUM(cnum)))
-    del_share_mode(fnum);
+  {
+    lock_share_entry( cnum, dev, inode, &token);
+    del_share_mode(token, fnum);
+  }
+
+  fd_attempt_close(fs_p->fd_ptr);
 
-  close(Files[fnum].fd);
+  if (lp_share_modes(SNUM(cnum)))
+    unlock_share_entry( cnum, dev, inode, token);
 
   /* NT uses smbclose to start a print - weird */
-  if (Files[fnum].print_file)
+  if (fs_p->print_file)
     print_file(fnum);
 
   /* check for magic scripts */
   check_magic(fnum,cnum);
 
   DEBUG(2,("%s %s closed file %s (numopen=%d)\n",
-          timestring(),Connections[cnum].user,Files[fnum].name,
+          timestring(),Connections[cnum].user,fs_p->name,
           Connections[cnum].num_files_open));
 }
 
@@ -983,7 +1417,8 @@ static int access_table(int new_deny,int old_deny,int old_mode,
   if (new_deny == DENY_ALL || old_deny == DENY_ALL) return(AFAIL);
 
   if (new_deny == DENY_DOS || old_deny == DENY_DOS) {
-    if (old_deny == new_deny && share_pid == getpid()) 
+    int pid = getpid();
+    if (old_deny == new_deny && share_pid == pid) 
        return(AALL);    
 
     if (old_mode == 0) return(AREAD);
@@ -1030,17 +1465,95 @@ return True if sharing doesn't prevent the operation
 ********************************************************************/
 BOOL check_file_sharing(int cnum,char *fname)
 {
-  int pid=0;
-  int share_mode = get_share_mode_byname(cnum,fname,&pid);
+  int i;
+  int ret = False;
+  min_share_mode_entry *old_shares = 0;
+  int num_share_modes;
+  struct stat sbuf;
+  share_lock_token token;
+  int pid = getpid();
+  uint32 dev, inode;
 
-  if (!pid || !share_mode) return(True);
-  if (share_mode == DENY_DOS)
-    return(pid == getpid());
+  if(!lp_share_modes(SNUM(cnum)))
+    return True;
+
+  if (stat(fname,&sbuf) == -1) return(True);
+
+  dev = (uint32)sbuf.st_dev;
+  inode = (uint32)sbuf.st_ino;
+
+  lock_share_entry(cnum, dev, inode, &token);
+  num_share_modes = get_share_modes(cnum, token, dev, inode, &old_shares);
+
+  /*
+   * Check if the share modes will give us access.
+   */
+
+  if(num_share_modes != 0)
+  {
+    BOOL broke_oplock;
+
+    do
+    {
+
+      broke_oplock = False;
+      for(i = 0; i < num_share_modes; i++)
+      {
+        min_share_mode_entry *share_entry = &old_shares[i];
+
+        /* 
+         * Break oplocks before checking share modes. See comment in
+         * open_file_shared for details. 
+         * Check if someone has an oplock on this file. If so we must 
+         * break it before continuing. 
+         */
+        if(share_entry->op_type & BATCH_OPLOCK)
+        {
+
+          DEBUG(5,("check_file_sharing: breaking oplock (%x) on file %s, \
+dev = %x, inode = %x\n", share_entry->op_type, fname, dev, inode));
+
+          /* Oplock break.... */
+          unlock_share_entry(cnum, dev, inode, token);
+          if(request_oplock_break(share_entry, dev, inode) == False)
+          {
+            free((char *)old_shares);
+            DEBUG(0,("check_file_sharing: FAILED when breaking oplock (%x) on file %s, \
+dev = %x, inode = %x\n", old_shares[i].op_type, fname, dev, inode));
+            return False;
+          }
+          lock_share_entry(cnum, dev, inode, &token);
+          broke_oplock = True;
+          break;
+        }
+
+        /* someone else has a share lock on it, check to see 
+           if we can too */
+        if ((share_entry->share_mode != DENY_DOS) || (share_entry->pid != pid))
+          goto free_and_exit;
+
+      } /* end for */
+
+      if(broke_oplock)
+      {
+        free((char *)old_shares);
+        num_share_modes = get_share_modes(cnum, token, dev, inode, &old_shares);
+      }
+    } while(broke_oplock);
+  }
 
   /* XXXX exactly what share mode combinations should be allowed for
      deleting/renaming? */
-  return(False);
+  /* If we got here then either there were no share modes or
+     all share modes were DENY_DOS and the pid == getpid() */
+  ret = True;
+
+free_and_exit:
+
+  unlock_share_entry(cnum, dev, inode, token);
+  if(old_shares != NULL)
+    free((char *)old_shares);
+  return(ret);
 }
 
 /****************************************************************************
@@ -1048,46 +1561,107 @@ BOOL check_file_sharing(int cnum,char *fname)
   Helper for open_file_shared. 
   Truncate a file after checking locking; close file if locked.
   **************************************************************************/
-static void truncate_unless_locked(int fnum, int cnum)
+static void truncate_unless_locked(int fnum, int cnum, share_lock_token token, 
+       BOOL *share_locked)
 {
   if (Files[fnum].can_write){
     if (is_locked(fnum,cnum,0x3FFFFFFF,0)){
+      /* If share modes are in force for this connection we
+         have the share entry locked. Unlock it before closing. */
+      if (*share_locked && lp_share_modes(SNUM(cnum)))
+        unlock_share_entry( cnum, Files[fnum].fd_ptr->dev, 
+                            Files[fnum].fd_ptr->inode, token);
       close_file(fnum);   
+      /* Share mode no longer locked. */
+      *share_locked = False;
       errno = EACCES;
       unix_ERR_class = ERRDOS;
       unix_ERR_code = ERRlock;
     }
     else
-      ftruncate(Files[fnum].fd,0); 
+      ftruncate(Files[fnum].fd_ptr->fd,0); 
   }
 }
 
+/****************************************************************************
+check if we can open a file with a share mode
+****************************************************************************/
+int check_share_mode( min_share_mode_entry *share, int deny_mode, char *fname,
+                      BOOL fcbopen, int *flags)
+{
+  int old_open_mode = share->share_mode &0xF;
+  int old_deny_mode = (share->share_mode >>4)&7;
+
+  if (old_deny_mode > 4 || old_open_mode > 2)
+  {
+    DEBUG(0,("Invalid share mode found (%d,%d,%d) on file %s\n",
+               deny_mode,old_deny_mode,old_open_mode,fname));
+    return False;
+  }
+
+  {
+    int access_allowed = access_table(deny_mode,old_deny_mode,old_open_mode,
+                                share->pid,fname);
+
+    if ((access_allowed == AFAIL) ||
+        (!fcbopen && (access_allowed == AREAD && *flags == O_RDWR)) ||
+        (access_allowed == AREAD && *flags == O_WRONLY) ||
+        (access_allowed == AWRITE && *flags == O_RDONLY))
+    {
+      DEBUG(2,("Share violation on file (%d,%d,%d,%d,%s) = %d\n",
+                deny_mode,old_deny_mode,old_open_mode,
+                share->pid,fname, access_allowed));
+      return False;
+    }
+
+    if (access_allowed == AREAD)
+      *flags = O_RDONLY;
+
+    if (access_allowed == AWRITE)
+      *flags = O_WRONLY;
+
+  }
+  return True;
+}
 
 /****************************************************************************
 open a file with a share mode
 ****************************************************************************/
 void open_file_shared(int fnum,int cnum,char *fname,int share_mode,int ofun,
-                     int mode,int *Access,int *action)
+                     int mode,int oplock_request, int *Access,int *action)
 {
+  files_struct *fs_p = &Files[fnum];
   int flags=0;
   int flags2=0;
   int deny_mode = (share_mode>>4)&7;
   struct stat sbuf;
   BOOL file_existed = file_exist(fname,&sbuf);
+  BOOL share_locked = False;
   BOOL fcbopen = False;
-  int share_pid=0;
+  share_lock_token token;
+  uint32 dev = 0;
+  uint32 inode = 0;
+  int num_share_modes = 0;
 
-  Files[fnum].open = False;
-  Files[fnum].fd = -1;
+  fs_p->open = False;
+  fs_p->fd_ptr = 0;
 
   /* this is for OS/2 EAs - try and say we don't support them */
-  if (strstr(fname,".+,;=[].")) {
+  if (strstr(fname,".+,;=[].")) 
+  {
     unix_ERR_class = ERRDOS;
+    /* OS/2 Workplace shell fix may be main code stream in a later release. */ 
+#ifdef OS2_WPS_FIX
+    unix_ERR_code = ERRcannotopen;
+#else /* OS2_WPS_FIX */
     unix_ERR_code = ERROR_EAS_NOT_SUPPORTED;
+#endif /* OS2_WPS_FIX */
+
     return;
   }
 
-  if ((ofun & 0x3) == 0 && file_existed) {
+  if ((ofun & 0x3) == 0 && file_existed)  
+  {
     errno = EEXIST;
     return;
   }
@@ -1101,7 +1675,7 @@ void open_file_shared(int fnum,int cnum,char *fname,int share_mode,int ofun,
      append does not mean the same thing under dos and unix */
 
   switch (share_mode&0xF)
-    {
+  {
     case 1: 
       flags = O_WRONLY; 
       break;
@@ -1115,18 +1689,21 @@ void open_file_shared(int fnum,int cnum,char *fname,int share_mode,int ofun,
     default:
       flags = O_RDONLY;
       break;
-    }
+  }
   
   if (flags != O_RDONLY && file_existed && 
-      (!CAN_WRITE(cnum) || IS_DOS_READONLY(dos_mode(cnum,fname,&sbuf)))) {
-    if (!fcbopen) {
+      (!CAN_WRITE(cnum) || IS_DOS_READONLY(dos_mode(cnum,fname,&sbuf)))) 
+  {
+    if (!fcbopen) 
+    {
       errno = EACCES;
       return;
     }
     flags = O_RDONLY;
   }
 
-  if (deny_mode > DENY_NONE && deny_mode!=DENY_FCB) {
+  if (deny_mode > DENY_NONE && deny_mode!=DENY_FCB) 
+  {
     DEBUG(2,("Invalid deny mode %d on file %s\n",deny_mode,fname));
     errno = EINVAL;
     return;
@@ -1134,144 +1711,201 @@ void open_file_shared(int fnum,int cnum,char *fname,int share_mode,int ofun,
 
   if (deny_mode == DENY_FCB) deny_mode = DENY_DOS;
 
-  if (lp_share_modes(SNUM(cnum))) {
-    int old_share=0;
+  if (lp_share_modes(SNUM(cnum))) 
+  {
+    int i;
+    min_share_mode_entry *old_shares = 0;
 
     if (file_existed)
-      old_share = get_share_mode(cnum,&sbuf,&share_pid);
-
-    if (share_pid) {
-      /* someone else has a share lock on it, check to see 
-        if we can too */
-      int old_open_mode = old_share&0xF;
-      int old_deny_mode = (old_share>>4)&7;
-
-      if (deny_mode > 4 || old_deny_mode > 4 || old_open_mode > 2) {
-       DEBUG(2,("Invalid share mode (%d,%d,%d) on file %s\n",
-                deny_mode,old_deny_mode,old_open_mode,fname));
-       errno = EACCES;
-       unix_ERR_class = ERRDOS;
-       unix_ERR_code = ERRbadshare;
-       return;
-      }
-
-      {
-       int access_allowed = access_table(deny_mode,old_deny_mode,old_open_mode,
-                                         share_pid,fname);
-
-       if ((access_allowed == AFAIL) ||
-           (access_allowed == AREAD && flags == O_WRONLY) ||
-           (access_allowed == AWRITE && flags == O_RDONLY)) {
-         DEBUG(2,("Share violation on file (%d,%d,%d,%d,%s) = %d\n",
-                  deny_mode,old_deny_mode,old_open_mode,
-                  share_pid,fname,
-                  access_allowed));
-         errno = EACCES;
-         unix_ERR_class = ERRDOS;
-         unix_ERR_code = ERRbadshare;
-         return;
-       }
-       
-       if (access_allowed == AREAD)
-         flags = O_RDONLY;
-       
-       if (access_allowed == AWRITE)
-         flags = O_WRONLY;
-      }
+    {
+      dev = (uint32)sbuf.st_dev;
+      inode = (uint32)sbuf.st_ino;
+      lock_share_entry(cnum, dev, inode, &token);
+      share_locked = True;
+      num_share_modes = get_share_modes(cnum, token, dev, inode, &old_shares);
     }
-  }
 
-  DEBUG(4,("calling open_file with flags=0x%X flags2=0x%X mode=0%o\n",
-          flags,flags2,mode));
+    /*
+     * Check if the share modes will give us access.
+     */
 
-  open_file(fnum,cnum,fname,flags|(flags2&~(O_TRUNC)),mode);
-  if (!Files[fnum].open && flags==O_RDWR && errno!=ENOENT && fcbopen) {
-    flags = O_RDONLY;
-    open_file(fnum,cnum,fname,flags,mode);
-  }
+    if(share_locked && (num_share_modes != 0))
+    {
+      BOOL broke_oplock;
+
+      do
+      {
+
+        broke_oplock = False;
+        for(i = 0; i < num_share_modes; i++)
+        {
+          min_share_mode_entry *share_entry = &old_shares[i];
+
+          /* 
+           * By observation of NetBench, oplocks are broken *before* share
+           * modes are checked. This allows a file to be closed by the client
+           * if the share mode would deny access and the client has an oplock. 
+           * Check if someone has an oplock on this file. If so we must break 
+           * it before continuing. 
+           */
+          if(share_entry->op_type & (EXCLUSIVE_OPLOCK|BATCH_OPLOCK))
+          {
+
+            DEBUG(5,("open_file_shared: breaking oplock (%x) on file %s, \
+dev = %x, inode = %x\n", share_entry->op_type, fname, dev, inode));
+
+            /* Oplock break.... */
+            unlock_share_entry(cnum, dev, inode, token);
+            if(request_oplock_break(share_entry, dev, inode) == False)
+            {
+              free((char *)old_shares);
+              DEBUG(0,("open_file_shared: FAILED when breaking oplock (%x) on file %s, \
+dev = %x, inode = %x\n", old_shares[i].op_type, fname, dev, inode));
+              errno = EACCES;
+              unix_ERR_class = ERRDOS;
+              unix_ERR_code = ERRbadshare;
+              return;
+            }
+            lock_share_entry(cnum, dev, inode, &token);
+            broke_oplock = True;
+            break;
+          }
+
+          /* someone else has a share lock on it, check to see 
+             if we can too */
+          if(check_share_mode(share_entry, deny_mode, fname, fcbopen, &flags) == False)
+          {
+            free((char *)old_shares);
+            unlock_share_entry(cnum, dev, inode, token);
+            errno = EACCES;
+            unix_ERR_class = ERRDOS;
+            unix_ERR_code = ERRbadshare;
+            return;
+          }
+
+        } /* end for */
+
+        if(broke_oplock)
+        {
+          free((char *)old_shares);
+          num_share_modes = get_share_modes(cnum, token, dev, inode, &old_shares);
+        }
+      } while(broke_oplock);
+    }
 
-  if (Files[fnum].open) {
+    if(old_shares != 0)
+      free((char *)old_shares);
+  }
+
+  DEBUG(4,("calling open_file with flags=0x%X flags2=0x%X mode=0%o\n",
+          flags,flags2,mode));
+
+  open_file(fnum,cnum,fname,flags|(flags2&~(O_TRUNC)),mode,file_existed ? &sbuf : 0);
+  if (!fs_p->open && flags==O_RDWR && errno!=ENOENT && fcbopen) 
+  {
+    flags = O_RDONLY;
+    open_file(fnum,cnum,fname,flags,mode,file_existed ? &sbuf : 0 );
+  }
+
+  if (fs_p->open) 
+  {
     int open_mode=0;
-    switch (flags) {
-    case O_RDONLY:
-      open_mode = 0;
-      break;
-    case O_RDWR:
-      open_mode = 2;
-      break;
-    case O_WRONLY:
-      open_mode = 1;
-      break;
+
+    if((share_locked == False) && lp_share_modes(SNUM(cnum)))
+    {
+      /* We created the file - thus we must now lock the share entry before creating it. */
+      dev = fs_p->fd_ptr->dev;
+      inode = fs_p->fd_ptr->inode;
+      lock_share_entry(cnum, dev, inode, &token);
+      share_locked = True;
     }
 
-    Files[fnum].share_mode = (deny_mode<<4) | open_mode;
-    Files[fnum].share_pending = True;
+    switch (flags) 
+    {
+      case O_RDONLY:
+        open_mode = 0;
+        break;
+      case O_RDWR:
+        open_mode = 2;
+        break;
+      case O_WRONLY:
+        open_mode = 1;
+        break;
+    }
 
-    if (Access) {
+    fs_p->share_mode = (deny_mode<<4) | open_mode;
+
+    if (Access)
       (*Access) = open_mode;
-    }
-    
-    if (action) {
+
+    if (action) 
+    {
       if (file_existed && !(flags2 & O_TRUNC)) *action = 1;
       if (!file_existed) *action = 2;
       if (file_existed && (flags2 & O_TRUNC)) *action = 3;
     }
+    /* We must create the share mode entry before truncate as
+       truncate can fail due to locking and have to close the
+       file (which expects the share_mode_entry to be there).
+     */
+    if (lp_share_modes(SNUM(cnum)))
+    {
+      uint16 port = 0;
+      /* JRA. Currently this only services Exlcusive and batch
+         oplocks (no other opens on this file). This needs to
+         be extended to level II oplocks (multiple reader
+         oplocks). */
 
-    if (!share_pid)
-      share_mode_pending = True;
-
-    if ((flags2&O_TRUNC) && file_existed)
-      truncate_unless_locked(fnum,cnum);
-  }
-}
-
+      if(oplock_request && (num_share_modes == 0) && lp_oplocks(SNUM(cnum)))
+      {
+        fs_p->granted_oplock = True;
+        global_oplocks_open++;
+        port = oplock_port;
 
+        DEBUG(5,("open_file_shared: granted oplock (%x) on file %s, \
+dev = %x, inode = %x\n", oplock_request, fname, dev, inode));
 
-/*******************************************************************
-check for files that we should now set our share modes on
-********************************************************************/
-static void check_share_modes(void)
-{
-  int i;
-  for (i=0;i<MAX_OPEN_FILES;i++)
-    if(Files[i].open && Files[i].share_pending) {
-      if (lp_share_modes(SNUM(Files[i].cnum))) {
-       int pid=0;
-       get_share_mode_by_fnum(Files[i].cnum,i,&pid);
-       if (!pid) {
-         set_share_mode(i,Files[i].share_mode);
-         Files[i].share_pending = False;
-       }
-      } else {
-       Files[i].share_pending = False; 
       }
+      else
+      {
+        port = 0;
+        oplock_request = 0;
+      }
+      set_share_mode(token, fnum, port, oplock_request);
     }
-}
 
+    if ((flags2&O_TRUNC) && file_existed)
+      truncate_unless_locked(fnum,cnum,token,&share_locked);
+  }
+
+  if (share_locked && lp_share_modes(SNUM(cnum)))
+    unlock_share_entry( cnum, dev, inode, token);
+}
 
 /****************************************************************************
 seek a file. Try to avoid the seek if possible
 ****************************************************************************/
-int seek_file(int fnum,int pos)
+int seek_file(int fnum,uint32 pos)
 {
-  int offset = 0;
+  uint32 offset = 0;
   if (Files[fnum].print_file && POSTSCRIPT(Files[fnum].cnum))
     offset = 3;
 
-  Files[fnum].pos = lseek(Files[fnum].fd,pos+offset,SEEK_SET) - offset;
+  Files[fnum].pos = (int)(lseek(Files[fnum].fd_ptr->fd,pos+offset,SEEK_SET) 
+                                  - offset);
   return(Files[fnum].pos);
 }
 
 /****************************************************************************
 read from a file
 ****************************************************************************/
-int read_file(int fnum,char *data,int pos,int n)
+int read_file(int fnum,char *data,uint32 pos,int n)
 {
   int ret=0,readret;
 
   if (!Files[fnum].can_write)
     {
-      ret = read_predict(Files[fnum].fd,pos,data,NULL,n);
+      ret = read_predict(Files[fnum].fd_ptr->fd,pos,data,NULL,n);
 
       data += ret;
       n -= ret;
@@ -1281,7 +1915,7 @@ int read_file(int fnum,char *data,int pos,int n)
 #if USE_MMAP
   if (Files[fnum].mmap_ptr)
     {
-      int num = MIN(n,Files[fnum].mmap_size-pos);
+      int num = MIN(n,(int)(Files[fnum].mmap_size-pos));
       if (num > 0)
        {
          memcpy(data,Files[fnum].mmap_ptr+pos,num);
@@ -1303,7 +1937,7 @@ int read_file(int fnum,char *data,int pos,int n)
     }
   
   if (n > 0) {
-    readret = read(Files[fnum].fd,data,n);
+    readret = read(Files[fnum].fd_ptr->fd,data,n);
     if (readret > 0) ret += readret;
   }
 
@@ -1324,7 +1958,7 @@ int write_file(int fnum,char *data,int n)
   if (!Files[fnum].modified) {
     struct stat st;
     Files[fnum].modified = True;
-    if (fstat(Files[fnum].fd,&st) == 0) {
+    if (fstat(Files[fnum].fd_ptr->fd,&st) == 0) {
       int dosmode = dos_mode(Files[fnum].cnum,Files[fnum].name,&st);
       if (MAP_ARCHIVE(Files[fnum].cnum) && !IS_DOS_ARCHIVE(dosmode)) { 
        dos_chmod(Files[fnum].cnum,Files[fnum].name,dosmode | aARCH,&st);
@@ -1332,7 +1966,7 @@ int write_file(int fnum,char *data,int n)
     }  
   }
 
-  return(write_data(Files[fnum].fd,data,n));
+  return(write_data(Files[fnum].fd_ptr->fd,data,n));
 }
 
 
@@ -1487,6 +2121,7 @@ struct
   {EPERM,ERRDOS,ERRnoaccess},
   {EACCES,ERRDOS,ERRnoaccess},
   {ENOENT,ERRDOS,ERRbadfile},
+  {ENOTDIR,ERRDOS,ERRbadpath},
   {EIO,ERRHRD,ERRgeneral},
   {EBADF,ERRSRV,ERRsrverror},
   {EINVAL,ERRSRV,ERRsrverror},
@@ -1507,7 +2142,6 @@ struct
   {0,0,0}
 };
 
-
 /****************************************************************************
   create an error packet from errno
 ****************************************************************************/
@@ -1527,15 +2161,15 @@ int unix_error_packet(char *inbuf,char *outbuf,int def_class,uint32 def_code,int
   else
     {
       while (unix_smb_errmap[i].smbclass != 0)
-       {
-         if (unix_smb_errmap[i].unixerror == errno)
+      {
+           if (unix_smb_errmap[i].unixerror == errno)
            {
              eclass = unix_smb_errmap[i].smbclass;
              ecode = unix_smb_errmap[i].smbcode;
              break;
            }
          i++;
-       }
+      }
     }
 
   return(error_packet(inbuf,outbuf,eclass,ecode,line));
@@ -1584,11 +2218,11 @@ static int sig_cld()
     }
   depth++;
 
-  BlockSignals(True);
+  BlockSignals(True,SIGCLD);
   DEBUG(5,("got SIGCLD\n"));
 
 #ifdef USE_WAITPID
-  while (waitpid((pid_t)-1,(int *)NULL, WNOHANG) > 0);
+  while (sys_waitpid((pid_t)-1,(int *)NULL, WNOHANG) > 0);
 #endif
 
   /* Stop zombies */
@@ -1610,117 +2244,677 @@ static int sig_cld()
   while (wait3(WAIT3_CAST1 NULL, WNOHANG, WAIT3_CAST2 NULL) > 0);
 #endif
   depth--;
-  BlockSignals(False);
+  BlockSignals(False,SIGCLD);
   return 0;
 }
 #endif
 
-/****************************************************************************
-  this is called when the client exits abruptly
-  **************************************************************************/
-static int sig_pipe()
-{
-  exit_server("Got sigpipe\n");
-  return(0);
+/****************************************************************************
+  this is called when the client exits abruptly
+  **************************************************************************/
+static int sig_pipe()
+{
+  extern int password_client;
+  BlockSignals(True,SIGPIPE);
+
+  if (password_client != -1) {
+    DEBUG(3,("lost connection to password server\n"));
+    close(password_client);
+    password_client = -1;
+#ifndef DONT_REINSTALL_SIG
+    signal(SIGPIPE, SIGNAL_CAST sig_pipe);
+#endif
+    BlockSignals(False,SIGPIPE);
+    return 0;
+  }
+
+  exit_server("Got sigpipe\n");
+  return(0);
+}
+
+/****************************************************************************
+  open the socket communication
+****************************************************************************/
+static BOOL open_sockets(BOOL is_daemon,int port)
+{
+  extern int Client;
+
+  if (is_daemon)
+    {
+      int s;
+      struct sockaddr addr;
+      int in_addrlen = sizeof(addr);
+       
+      /* Stop zombies */
+#ifdef SIGCLD_IGNORE
+      signal(SIGCLD, SIG_IGN);
+#else
+      signal(SIGCLD, SIGNAL_CAST sig_cld);
+#endif
+
+      /* open an incoming socket */
+      s = open_socket_in(SOCK_STREAM, port, 0,interpret_addr(lp_socket_address()));
+      if (s == -1)
+       return(False);
+
+      /* ready to listen */
+      if (listen(s, 5) == -1) 
+       {
+         DEBUG(0,("listen: %s\n",strerror(errno)));
+         close(s);
+         return False;
+       }
+      
+      if(atexit_set == 0)
+        atexit(killkids);
+
+      /* now accept incoming connections - forking a new process
+        for each incoming connection */
+      DEBUG(2,("waiting for a connection\n"));
+      while (1)
+       {
+         Client = accept(s,&addr,&in_addrlen);
+
+         if (Client == -1 && errno == EINTR)
+           continue;
+
+         if (Client == -1)
+           {
+             DEBUG(0,("accept: %s\n",strerror(errno)));
+             continue;
+           }
+
+#ifdef NO_FORK_DEBUG
+#ifndef NO_SIGNAL_TEST
+          signal(SIGPIPE, SIGNAL_CAST sig_pipe);
+          signal(SIGCLD, SIGNAL_CAST SIG_DFL);
+#endif
+         return True;
+#else
+         if (Client != -1 && fork()==0)
+           {
+              /* Child code ... */
+#ifndef NO_SIGNAL_TEST
+             signal(SIGPIPE, SIGNAL_CAST sig_pipe);
+             signal(SIGCLD, SIGNAL_CAST SIG_DFL);
+#endif
+             /* close the listening socket */
+             close(s);
+
+             /* close our standard file descriptors */
+             close_low_fds();
+              am_parent = 0;
+  
+             set_socket_options(Client,"SO_KEEPALIVE");
+             set_socket_options(Client,user_socket_options);
+
+              /* Reset global variables in util.c so that
+                 client substitutions will be done correctly
+                 in the process.
+               */
+              reset_globals_after_fork();
+             return True; 
+           }
+          close(Client); /* The parent doesn't need this socket */
+#endif
+       }
+    }
+  else
+    {
+      /* We will abort gracefully when the client or remote system 
+        goes away */
+#ifndef NO_SIGNAL_TEST
+      signal(SIGPIPE, SIGNAL_CAST sig_pipe);
+#endif
+      Client = dup(0);
+
+      /* close our standard file descriptors */
+      close_low_fds();
+
+      set_socket_options(Client,"SO_KEEPALIVE");
+      set_socket_options(Client,user_socket_options);
+    }
+
+  return True;
+}
+
+/****************************************************************************
+  process an smb from the client - split out from the process() code so
+  it can be used by the oplock break code.
+****************************************************************************/
+
+static void process_smb(char *inbuf, char *outbuf)
+{
+  extern int Client;
+  static int trans_num;
+  int msg_type = CVAL(inbuf,0);
+  int32 len = smb_len(inbuf);
+  int nread = len + 4;
+
+  if (trans_num == 0) {
+         /* on the first packet, check the global hosts allow/ hosts
+            deny parameters before doing any parsing of the packet
+            passed to us by the client.  This prevents attacks on our
+            parsing code from hosts not in the hosts allow list */
+         if (!check_access(-1)) {
+                 /* send a negative session response "not listining on calling
+                  name" */
+                 static unsigned char buf[5] = {0x83, 0, 0, 1, 0x81};
+                 DEBUG(1,("%s Connection denied from %s\n",
+                          timestring(),client_addr()));
+                 send_smb(Client,(char *)buf);
+                 exit_server("connection denied");
+         }
+  }
+
+  DEBUG(6,("got message type 0x%x of len 0x%x\n",msg_type,len));
+  DEBUG(3,("%s Transaction %d of length %d\n",timestring(),trans_num,nread));
+
+#ifdef WITH_VTP
+  if(trans_num == 1 && VT_Check(inbuf)) 
+  {
+    VT_Process();
+    return;
+  }
+#endif
+
+  if (msg_type == 0)
+    show_msg(inbuf);
+
+  nread = construct_reply(inbuf,outbuf,nread,max_send);
+      
+  if(nread > 0) 
+  {
+    if (CVAL(outbuf,0) == 0)
+      show_msg(outbuf);
+       
+    if (nread != smb_len(outbuf) + 4) 
+    {
+      DEBUG(0,("ERROR: Invalid message response size! %d %d\n",
+                 nread, smb_len(outbuf)));
+    }
+    else
+      send_smb(Client,outbuf);
+  }
+  trans_num++;
+}
+
+/****************************************************************************
+  open the oplock IPC socket communication
+****************************************************************************/
+static BOOL open_oplock_ipc()
+{
+  struct sockaddr_in sock_name;
+  int len = sizeof(sock_name);
+
+  DEBUG(3,("open_oplock_ipc: opening loopback UDP socket.\n"));
+
+  /* Open a lookback UDP socket on a random port. */
+  oplock_sock = open_socket_in(SOCK_DGRAM, 0, 0, htonl(INADDR_LOOPBACK));
+  if (oplock_sock == -1)
+  {
+    DEBUG(0,("open_oplock_ipc: Failed to get local UDP socket for \
+address %x. Error was %s\n", htonl(INADDR_LOOPBACK), strerror(errno)));
+    oplock_port = 0;
+    return(False);
+  }
+
+  /* Find out the transient UDP port we have been allocated. */
+  if(getsockname(oplock_sock, (struct sockaddr *)&sock_name, &len)<0)
+  {
+    DEBUG(0,("open_oplock_ipc: Failed to get local UDP port. Error was %s\n",
+            strerror(errno)));
+    close(oplock_sock);
+    oplock_sock = -1;
+    oplock_port = 0;
+    return False;
+  }
+  oplock_port = ntohs(sock_name.sin_port);
+
+  DEBUG(3,("open_oplock ipc: pid = %d, oplock_port = %u\n", 
+            getpid(), oplock_port));
+
+  return True;
+}
+
+/****************************************************************************
+  process an oplock break message.
+****************************************************************************/
+static BOOL process_local_message(int sock, char *buffer, int buf_size)
+{
+  int32 msg_len;
+  int16 from_port;
+  char *msg_start;
+
+  msg_len = IVAL(buffer,UDP_CMD_LEN_OFFSET);
+  from_port = SVAL(buffer,UDP_CMD_PORT_OFFSET);
+
+  msg_start = &buffer[UDP_CMD_HEADER_LEN];
+
+  DEBUG(5,("process_local_message: Got a message of length %d from port (%d)\n", 
+            msg_len, from_port));
+
+  /* Switch on message command - currently OPLOCK_BREAK_CMD is the
+     only valid request. */
+
+  switch(SVAL(msg_start,UDP_MESSAGE_CMD_OFFSET))
+  {
+    case OPLOCK_BREAK_CMD:
+      /* Ensure that the msg length is correct. */
+      if(msg_len != OPLOCK_BREAK_MSG_LEN)
+      {
+        DEBUG(0,("process_local_message: incorrect length for OPLOCK_BREAK_CMD (was %d, \
+should be %d).\n", msg_len, OPLOCK_BREAK_MSG_LEN));
+        return False;
+      }
+      {
+        uint32 remotepid = IVAL(msg_start,OPLOCK_BREAK_PID_OFFSET);
+        uint32 dev = IVAL(msg_start,OPLOCK_BREAK_DEV_OFFSET);
+        uint32 inode = IVAL(msg_start, OPLOCK_BREAK_INODE_OFFSET);
+        struct timeval tval;
+        struct sockaddr_in toaddr;
+
+        tval.tv_sec = IVAL(msg_start, OPLOCK_BREAK_SEC_OFFSET);
+        tval.tv_usec = IVAL(msg_start, OPLOCK_BREAK_USEC_OFFSET);
+
+        DEBUG(5,("process_local_message: oplock break request from \
+pid %d, port %d, dev = %x, inode = %x\n", remotepid, from_port, dev, inode));
+
+        /*
+         * If we have no record of any currently open oplocks,
+         * it's not an error, as a close command may have
+         * just been issued on the file that was oplocked.
+         * Just return success in this case.
+         */
+
+        if(global_oplocks_open != 0)
+        {
+          if(oplock_break(dev, inode, &tval) == False)
+          {
+            DEBUG(0,("process_local_message: oplock break failed - \
+not returning udp message.\n"));
+            return False;
+          }
+        }
+        else
+        {
+          DEBUG(3,("process_local_message: oplock break requested with no outstanding \
+oplocks. Returning success.\n"));
+        }
+
+        /* Send the message back after OR'ing in the 'REPLY' bit. */
+        SSVAL(msg_start,UDP_MESSAGE_CMD_OFFSET,OPLOCK_BREAK_CMD | CMD_REPLY);
+  
+        bzero((char *)&toaddr,sizeof(toaddr));
+        toaddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
+        toaddr.sin_port = htons(from_port);
+        toaddr.sin_family = AF_INET;
+
+        if(sendto( sock, msg_start, OPLOCK_BREAK_MSG_LEN, 0,
+                (struct sockaddr *)&toaddr, sizeof(toaddr)) < 0) 
+        {
+          DEBUG(0,("process_local_message: sendto process %d failed. Errno was %s\n",
+                    remotepid, strerror(errno)));
+          return False;
+        }
+
+        DEBUG(5,("process_local_message: oplock break reply sent to \
+pid %d, port %d, for file dev = %x, inode = %x\n", remotepid, 
+                from_port, dev, inode));
+
+      }
+      break;
+    /* 
+     * Keep this as a debug case - eventually we can remove it.
+     */
+    case 0x8001:
+      DEBUG(0,("process_local_message: Received unsolicited break \
+reply - dumping info.\n"));
+
+      if(msg_len != OPLOCK_BREAK_MSG_LEN)
+      {
+        DEBUG(0,("process_local_message: ubr: incorrect length for reply \
+(was %d, should be %d).\n", msg_len, OPLOCK_BREAK_MSG_LEN));
+        return False;
+      }
+
+      {
+        uint32 remotepid = IVAL(msg_start,OPLOCK_BREAK_PID_OFFSET);
+        uint32 dev = IVAL(msg_start,OPLOCK_BREAK_DEV_OFFSET);
+        uint32 inode = IVAL(msg_start, OPLOCK_BREAK_INODE_OFFSET);
+
+        DEBUG(0,("process_local_message: unsolicited oplock break reply from \
+pid %d, port %d, dev = %x, inode = %x\n", remotepid, from_port, dev, inode));
+
+       }
+       return False;
+
+    default:
+      DEBUG(0,("process_local_message: unknown UDP message command code (%x) - ignoring.\n",
+                (unsigned int)SVAL(msg_start,0)));
+      return False;
+  }
+  return True;
+}
+
+/****************************************************************************
+ Process an oplock break directly.
+****************************************************************************/
+BOOL oplock_break(uint32 dev, uint32 inode, struct timeval *tval)
+{
+  extern int Client;
+  static char *inbuf = NULL;
+  static char *outbuf = NULL;
+  files_struct *fsp = NULL;
+  int fnum;
+  time_t start_time;
+  BOOL shutdown_server = False;
+
+  DEBUG(5,("oplock_break: called for dev = %x, inode = %x. Current \
+global_oplocks_open = %d\n", dev, inode, global_oplocks_open));
+
+  if(inbuf == NULL)
+  {
+    inbuf = (char *)malloc(BUFFER_SIZE + SAFETY_MARGIN);
+    if(inbuf == NULL) {
+      DEBUG(0,("oplock_break: malloc fail for input buffer.\n"));
+      return False;
+    } 
+    outbuf = (char *)malloc(BUFFER_SIZE + SAFETY_MARGIN);
+    if(outbuf == NULL) {
+      DEBUG(0,("oplock_break: malloc fail for output buffer.\n"));
+      free(inbuf);
+      inbuf = NULL;
+      return False;
+    }
+  } 
+
+  /* We need to search the file open table for the
+     entry containing this dev and inode, and ensure
+     we have an oplock on it. */
+  for( fnum = 0; fnum < MAX_OPEN_FILES; fnum++)
+  {
+    if(OPEN_FNUM(fnum))
+    {
+      fsp = &Files[fnum];
+      if((fsp->fd_ptr->dev == dev) && (fsp->fd_ptr->inode == inode) &&
+         (fsp->open_time.tv_sec == tval->tv_sec) && 
+         (fsp->open_time.tv_usec == tval->tv_usec))
+        break;
+    }
+  }
+
+  if(fsp == NULL)
+  {
+    /* The file could have been closed in the meantime - return success. */
+    DEBUG(3,("oplock_break: cannot find open file with dev = %x, inode = %x (fnum = %d) \
+allowing break to succeed.\n", dev, inode, fnum));
+    return True;
+  }
+
+  /* Ensure we have an oplock on the file */
+
+  /* There is a potential race condition in that an oplock could
+     have been broken due to another udp request, and yet there are
+     still oplock break messages being sent in the udp message
+     queue for this file. So return true if we don't have an oplock,
+     as we may have just freed it.
+   */
+
+  if(!fsp->granted_oplock)
+  {
+    DEBUG(3,("oplock_break: file %s (fnum = %d, dev = %x, inode = %x) has no oplock. \
+Allowing break to succeed regardless.\n", fsp->name, fnum, dev, inode));
+    return True;
+  }
+
+  /* Now comes the horrid part. We must send an oplock break to the client,
+     and then process incoming messages until we get a close or oplock release.
+   */
+
+  /* Prepare the SMBlockingX message. */
+  bzero(outbuf,smb_size);
+  set_message(outbuf,8,0,True);
+
+  SCVAL(outbuf,smb_com,SMBlockingX);
+  SSVAL(outbuf,smb_tid,fsp->cnum);
+  SSVAL(outbuf,smb_pid,0xFFFF);
+  SSVAL(outbuf,smb_uid,0);
+  SSVAL(outbuf,smb_mid,0xFFFF);
+  SCVAL(outbuf,smb_vwv0,0xFF);
+  SSVAL(outbuf,smb_vwv2,fnum);
+  SCVAL(outbuf,smb_vwv3,LOCKING_ANDX_OPLOCK_RELEASE);
+  /* Change this when we have level II oplocks. */
+  SCVAL(outbuf,smb_vwv3+1,OPLOCKLEVEL_NONE);
+  send_smb(Client, outbuf);
+
+  global_oplock_break = True;
+  /* Process incoming messages. */
+
+  /* JRA - If we don't get a break from the client in OPLOCK_BREAK_TIMEOUT
+     seconds we should just die.... */
+
+  start_time = time(NULL);
+
+  while(OPEN_FNUM(fnum) && fsp->granted_oplock)
+  {
+    if(receive_smb(Client,inbuf,OPLOCK_BREAK_TIMEOUT * 1000) == False)
+    {
+      /*
+       * Die if we got an error.
+       */
+
+      if (smb_read_error == READ_EOF)
+        DEBUG(0,("oplock_break: end of file from client\n"));
+      if (smb_read_error == READ_ERROR)
+        DEBUG(0,("oplock_break: receive_smb error (%s)\n",
+                  strerror(errno)));
+
+      if (smb_read_error == READ_TIMEOUT)
+        DEBUG(0,("oplock_break: receive_smb timed out after %d seconds.\n",
+                  OPLOCK_BREAK_TIMEOUT));
+
+      DEBUG(0,("oplock_break failed for file %s (fnum = %d, dev = %x, \
+inode = %x).\n", fsp->name, fnum, dev, inode));
+      shutdown_server = True;
+      break;
+    }
+    process_smb(inbuf, outbuf);
+
+    /* We only need this in case a readraw crossed on the wire. */
+    if(global_oplock_break)
+      global_oplock_break = False;
+
+    /*
+     * Die if we go over the time limit.
+     */
+
+    if((time(NULL) - start_time) > OPLOCK_BREAK_TIMEOUT)
+    {
+      DEBUG(0,("oplock_break: no break received from client within \
+%d seconds.\n", OPLOCK_BREAK_TIMEOUT));
+      DEBUG(0,("oplock_break failed for file %s (fnum = %d, dev = %x, \
+inode = %x).\n", fsp->name, fnum, dev, inode));
+      shutdown_server = True;
+      break;
+    }
+  }
+
+  /*
+   * If the client did not respond we must die.
+   */
+
+  if(shutdown_server)
+  {
+    DEBUG(0,("oplock_break: client failure in break - shutting down this smbd.\n"));
+    close_sockets();
+    close(oplock_sock);
+    exit_server("oplock break failure");
+  }
+
+  if(OPEN_FNUM(fnum))
+  {
+    /* The lockingX reply will have removed the oplock flag 
+       from the sharemode. */
+    /* Paranoia.... */
+    fsp->granted_oplock = False;
+  }
+
+  global_oplocks_open--;
+
+  /* Santity check - remove this later. JRA */
+  if(global_oplocks_open < 0)
+  {
+    DEBUG(0,("oplock_break: global_oplocks_open < 0 (%d). PANIC ERROR\n",
+              global_oplocks_open));
+    abort();
+  }
+
+  DEBUG(5,("oplock_break: returning success for fnum = %d, dev = %x, inode = %x. Current \
+global_oplocks_open = %d\n", fnum, dev, inode, global_oplocks_open));
+
+  return True;
 }
 
 /****************************************************************************
-  open the socket communication
+Send an oplock break message to another smbd process. If the oplock is held 
+by the local smbd then call the oplock break function directly.
 ****************************************************************************/
-static BOOL open_sockets(BOOL is_daemon,int port)
+
+BOOL request_oplock_break(min_share_mode_entry *share_entry, 
+                          uint32 dev, uint32 inode)
 {
-  extern int Client;
+  char op_break_msg[OPLOCK_BREAK_MSG_LEN];
+  struct sockaddr_in addr_out;
+  int pid = getpid();
 
-  if (is_daemon)
+  if(pid == share_entry->pid)
+  {
+    /* We are breaking our own oplock, make sure it's us. */
+    if(share_entry->op_port != oplock_port)
     {
-      int s;
-      struct sockaddr addr;
-      int in_addrlen = sizeof(addr);
-       
-      /* Stop zombies */
-#ifdef SIGCLD_IGNORE
-      signal(SIGCLD, SIG_IGN);
-#else
-      signal(SIGCLD, SIGNAL_CAST sig_cld);
-#endif
+      DEBUG(0,("request_oplock_break: corrupt share mode entry - pid = %d, port = %d \
+should be %d\n", pid, share_entry->op_port, oplock_port));
+      return False;
+    }
 
-      /* open an incoming socket */
-      s = open_socket_in(SOCK_STREAM, port, 0);
-      if (s == -1)
-       return(False);
+    DEBUG(5,("request_oplock_break: breaking our own oplock\n"));
 
-      /* ready to listen */
-      if (listen(s, 5) == -1) 
-       {
-         DEBUG(0,("listen: %s",strerror(errno)));
-         close(s);
-         return False;
-       }
-      
-      /* now accept incoming connections - forking a new process
-        for each incoming connection */
-      DEBUG(2,("waiting for a connection\n"));
-      while (1)
-       {
-         Client = accept(s,&addr,&in_addrlen);
+    /* Call oplock break direct. */
+    return oplock_break(dev, inode, &share_entry->time);
+  }
 
-         if (Client == -1 && errno == EINTR)
-           continue;
+  /* We need to send a OPLOCK_BREAK_CMD message to the
+     port in the share mode entry. */
+
+  SSVAL(op_break_msg,UDP_MESSAGE_CMD_OFFSET,OPLOCK_BREAK_CMD);
+  SIVAL(op_break_msg,OPLOCK_BREAK_PID_OFFSET,pid);
+  SIVAL(op_break_msg,OPLOCK_BREAK_DEV_OFFSET,dev);
+  SIVAL(op_break_msg,OPLOCK_BREAK_INODE_OFFSET,inode);
+  SIVAL(op_break_msg,OPLOCK_BREAK_SEC_OFFSET,(uint32)share_entry->time.tv_sec);
+  SIVAL(op_break_msg,OPLOCK_BREAK_USEC_OFFSET,(uint32)share_entry->time.tv_usec);
+
+  /* set the address and port */
+  bzero((char *)&addr_out,sizeof(addr_out));
+  addr_out.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
+  addr_out.sin_port = htons( share_entry->op_port );
+  addr_out.sin_family = AF_INET;
+   
+  DEBUG(3,("request_oplock_break: sending a oplock break message to pid %d on port %d \
+for dev = %x, inode = %x\n", share_entry->pid, share_entry->op_port, dev, inode));
 
-         if (Client == -1)
-           {
-             DEBUG(0,("accept: %s",strerror(errno)));
-             return False;
-           }
+  if(sendto(oplock_sock,op_break_msg,OPLOCK_BREAK_MSG_LEN,0,
+         (struct sockaddr *)&addr_out,sizeof(addr_out)) < 0)
+  {
+    DEBUG(0,("request_oplock_break: failed when sending a oplock break message \
+to pid %d on port %d for dev = %x, inode = %x. Error was %s\n",
+         share_entry->pid, share_entry->op_port, dev, inode,
+         strerror(errno)));
+    return False;
+  }
 
-#ifdef NO_FORK_DEBUG
-#ifndef NO_SIGNAL_TEST
-          signal(SIGPIPE, SIGNAL_CAST sig_pipe);
-          signal(SIGCLD, SIGNAL_CAST SIG_DFL);
-#endif
-         return True;
-#else
-         if (Client != -1 && fork()==0)
-           {
-#ifndef NO_SIGNAL_TEST
-             signal(SIGPIPE, SIGNAL_CAST sig_pipe);
-             signal(SIGCLD, SIGNAL_CAST SIG_DFL);
-#endif
-             /* close the listening socket */
-             close(s);
+  /*
+   * Now we must await the oplock broken message coming back
+   * from the target smbd process. Timeout if it fails to
+   * return in OPLOCK_BREAK_TIMEOUT seconds.
+   * While we get messages that aren't ours, loop.
+   */
 
-             /* close our standard file descriptors */
-             close_low_fds();
-  
-             set_socket_options(Client,"SO_KEEPALIVE");
-             set_socket_options(Client,user_socket_options);
+  while(1)
+  {
+    char op_break_reply[UDP_CMD_HEADER_LEN+OPLOCK_BREAK_MSG_LEN];
+    int32 reply_msg_len;
+    int16 reply_from_port;
+    char *reply_msg_start;
 
-             return True; 
-           }
-          close(Client); /* The parent doesn't need this socket */
-#endif
-       }
-    }
-  else
+    if(receive_local_message(oplock_sock, op_break_reply, sizeof(op_break_reply),
+                             OPLOCK_BREAK_TIMEOUT * 1000) == False)
     {
-      /* We will abort gracefully when the client or remote system 
-        goes away */
-#ifndef NO_SIGNAL_TEST
-      signal(SIGPIPE, SIGNAL_CAST sig_pipe);
-#endif
-      Client = dup(0);
+      if(smb_read_error == READ_TIMEOUT)
+        DEBUG(0,("request_oplock_break: no response received to oplock break request to \
+pid %d on port %d for dev = %x, inode = %x\n", share_entry->pid, 
+                           share_entry->op_port, dev, inode));
+      else
+        DEBUG(0,("request_oplock_break: error in response received to oplock break request to \
+pid %d on port %d for dev = %x, inode = %x. Error was (%s).\n", share_entry->pid, 
+                         share_entry->op_port, dev, inode, strerror(errno)));
+      return False;
+    }
 
-      /* close our standard file descriptors */
-      close_low_fds();
+    /* 
+     * If the response we got was not an answer to our message, but
+     * was a completely different request, push it onto the pending
+     * udp message stack so that we can deal with it in the main loop.
+     * It may be another oplock break request to us.
+     */
+
+    /*
+     * Local note from JRA. There exists the possibility of a denial
+     * of service attack here by allowing non-root processes running
+     * on a local machine sending many of these pending messages to
+     * a smbd port. Currently I'm not sure how to restrict the messages
+     * I will queue (although I could add a limit to the queue) to
+     * those received by root processes only. There should be a 
+     * way to make this bulletproof....
+     */
+
+    reply_msg_len = IVAL(op_break_reply,UDP_CMD_LEN_OFFSET);
+    reply_from_port = SVAL(op_break_reply,UDP_CMD_PORT_OFFSET);
+
+    reply_msg_start = &op_break_reply[UDP_CMD_HEADER_LEN];
+
+    if(reply_msg_len != OPLOCK_BREAK_MSG_LEN)
+    {
+      /* Ignore it. */
+      DEBUG(0,("request_oplock_break: invalid message length received. Ignoring\n"));
+      continue;
+    }
 
-      set_socket_options(Client,"SO_KEEPALIVE");
-      set_socket_options(Client,user_socket_options);
+    if(((SVAL(reply_msg_start,UDP_MESSAGE_CMD_OFFSET) & CMD_REPLY) == 0) ||
+       (reply_from_port != share_entry->op_port) ||
+       (memcmp(&reply_msg_start[OPLOCK_BREAK_PID_OFFSET], 
+               &op_break_msg[OPLOCK_BREAK_PID_OFFSET],
+               OPLOCK_BREAK_MSG_LEN - OPLOCK_BREAK_PID_OFFSET) != 0))
+    {
+      DEBUG(3,("request_oplock_break: received other message whilst awaiting \
+oplock break response from pid %d on port %d for dev = %x, inode = %x.\n",
+             share_entry->pid, share_entry->op_port, dev, inode));
+      if(push_local_message(op_break_reply, sizeof(op_break_reply)) == False)
+        return False;
+      continue;
     }
 
+    break;
+  }
+
+  DEBUG(3,("request_oplock_break: broke oplock.\n"));
+
   return True;
 }
 
-
 /****************************************************************************
 check if a snum is in use
 ****************************************************************************/
@@ -1743,10 +2937,10 @@ BOOL reload_services(BOOL test)
   if (lp_loaded())
     {
       pstring fname;
-      strcpy(fname,lp_configfile());
+      pstrcpy(fname,lp_configfile());
       if (file_exist(fname,NULL) && !strcsequal(fname,servicesf))
        {
-         strcpy(servicesf,fname);
+         pstrcpy(servicesf,fname);
          test = False;
        }
     }
@@ -1791,13 +2985,13 @@ this prevents zombie child processes
 ****************************************************************************/
 static int sig_hup()
 {
-  BlockSignals(True);
+  BlockSignals(True,SIGHUP);
   DEBUG(0,("Got SIGHUP\n"));
   reload_services(False);
 #ifndef DONT_REINSTALL_SIG
   signal(SIGHUP,SIGNAL_CAST sig_hup);
 #endif
-  BlockSignals(False);
+  BlockSignals(False,SIGHUP);
   return(0);
 }
 
@@ -1879,7 +3073,7 @@ int setup_groups(char *user, int uid, int gid, int *p_ngroups,
 /****************************************************************************
   make a connection to a service
 ****************************************************************************/
-int make_connection(char *service,char *user,char *password, int pwlen, char *dev,int vuid)
+int make_connection(char *service,char *user,char *password, int pwlen, char *dev,uint16 vuid)
 {
   int cnum;
   int snum;
@@ -1989,8 +3183,17 @@ int make_connection(char *service,char *user,char *password, int pwlen, char *de
   }
 
   /* admin user check */
-  if (user_in_list(user,lp_admin_users(snum)) &&
-      !pcon->read_only)
+
+  /* JRA - original code denied admin user if the share was
+     marked read_only. Changed as I don't think this is needed,
+     but old code left in case there is a problem here.
+   */
+  if (user_in_list(user,lp_admin_users(snum)) 
+#if 0
+      && !pcon->read_only)
+#else
+      )
+#endif
     {
       pcon->admin_user = True;
       DEBUG(0,("%s logged in as admin user (root privileges)\n",user));
@@ -1999,6 +3202,7 @@ int make_connection(char *service,char *user,char *password, int pwlen, char *de
     pcon->admin_user = False;
     
   pcon->force_user = force;
+  pcon->vuid = vuid;
   pcon->uid = pass->pw_uid;
   pcon->gid = pass->pw_gid;
   pcon->num_files_open = 0;
@@ -2008,20 +3212,29 @@ int make_connection(char *service,char *user,char *password, int pwlen, char *de
   pcon->printer = (strncmp(dev,"LPT",3) == 0);
   pcon->ipc = (strncmp(dev,"IPC",3) == 0);
   pcon->dirptr = NULL;
+  pcon->veto_list = NULL;
+  pcon->hide_list = NULL;
   string_set(&pcon->dirpath,"");
   string_set(&pcon->user,user);
 
 #if HAVE_GETGRNAM 
   if (*lp_force_group(snum))
     {
-      struct group *gptr = (struct group *)getgrnam(lp_force_group(snum));
+      struct group *gptr;
+      pstring gname;
+
+      StrnCpy(gname,lp_force_group(snum),sizeof(pstring)-1);
+      /* default service may be a group name           */
+      string_sub(gname,"%S",service);
+      gptr = (struct group *)getgrnam(gname);
+
       if (gptr)
        {
          pcon->gid = gptr->gr_gid;
-         DEBUG(3,("Forced group %s\n",lp_force_group(snum)));
+         DEBUG(3,("Forced group %s\n",gname));
        }
       else
-       DEBUG(1,("Couldn't find group %s\n",lp_force_group(snum)));
+       DEBUG(1,("Couldn't find group %s\n",gname));
     }
 #endif
 
@@ -2029,13 +3242,13 @@ int make_connection(char *service,char *user,char *password, int pwlen, char *de
     {
       struct passwd *pass2;
       fstring fuser;
-      strcpy(fuser,lp_force_user(snum));
+      fstrcpy(fuser,lp_force_user(snum));
       pass2 = (struct passwd *)Get_Pwnam(fuser,True);
       if (pass2)
        {
          pcon->uid = pass2->pw_uid;
          string_set(&pcon->user,fuser);
-         strcpy(user,fuser);
+         fstrcpy(user,fuser);
          pcon->force_user = True;
          DEBUG(3,("Forced user %s\n",fuser));    
        }
@@ -2045,7 +3258,7 @@ int make_connection(char *service,char *user,char *password, int pwlen, char *de
 
   {
     pstring s;
-    strcpy(s,lp_pathname(snum));
+    pstrcpy(s,lp_pathname(snum));
     standard_sub(cnum,s);
     string_set(&pcon->connectpath,s);
     DEBUG(3,("Connect path is %s\n",s));
@@ -2081,13 +3294,13 @@ int make_connection(char *service,char *user,char *password, int pwlen, char *de
   if (*lp_rootpreexec(SNUM(cnum)))
     {
       pstring cmd;
-      strcpy(cmd,lp_rootpreexec(SNUM(cnum)));
+      pstrcpy(cmd,lp_rootpreexec(SNUM(cnum)));
       standard_sub(cnum,cmd);
       DEBUG(5,("cmd=%s\n",cmd));
-      smbrun(cmd,NULL);
+      smbrun(cmd,NULL,False);
     }
 
-  if (!become_user(cnum,pcon->uid))
+  if (!become_user(cnum,pcon->vuid))
     {
       DEBUG(0,("Can't become connected user!\n"));
       pcon->open = False;
@@ -2121,7 +3334,7 @@ int make_connection(char *service,char *user,char *password, int pwlen, char *de
   /* resolve any soft links early */
   {
     pstring s;
-    strcpy(s,pcon->connectpath);
+    pstrcpy(s,pcon->connectpath);
     GetWd(s);
     string_set(&pcon->connectpath,s);
     ChDir(pcon->connectpath);
@@ -2135,19 +3348,26 @@ int make_connection(char *service,char *user,char *password, int pwlen, char *de
   if (*lp_preexec(SNUM(cnum)))
     {
       pstring cmd;
-      strcpy(cmd,lp_preexec(SNUM(cnum)));
+      pstrcpy(cmd,lp_preexec(SNUM(cnum)));
       standard_sub(cnum,cmd);
-      smbrun(cmd,NULL);
+      smbrun(cmd,NULL,False);
     }
   
   /* we've finished with the sensitive stuff */
   unbecome_user();
 
+  /* Add veto/hide lists */
+  if (!IS_IPC(cnum) && !IS_PRINT(cnum))
+  {
+    set_namearray( &pcon->veto_list, lp_veto_files(SNUM(cnum)));
+    set_namearray( &pcon->hide_list, lp_hide_files(SNUM(cnum)));
+  }
+
   {
-    extern struct from_host Client_info;
     DEBUG(IS_IPC(cnum)?3:1,("%s %s (%s) connect to service %s as user %s (uid=%d,gid=%d) (pid %d)\n",
                            timestring(),
-                           Client_info.name,Client_info.addr,
+                           remote_machine,
+                           client_addr(),
                            lp_servicename(SNUM(cnum)),user,
                            pcon->uid,
                            pcon->gid,
@@ -2249,29 +3469,31 @@ int reply_lanman1(char *outbuf)
   int secword=0;
   BOOL doencrypt = SMBENCRYPT();
   time_t t = time(NULL);
+  /* We need to save and restore this as it can be destroyed
+     if we call another server if security=server
+     Thanks to Paul Nelson @ Thursby for pointing this out.
+   */
+  uint16 mid = SVAL(outbuf, smb_mid);
 
   if (lp_security()>=SEC_USER) secword |= 1;
   if (doencrypt) secword |= 2;
 
   set_message(outbuf,13,doencrypt?8:0,True);
   SSVAL(outbuf,smb_vwv1,secword); 
-#ifdef SMB_PASSWD
   /* Create a token value and add it to the outgoing packet. */
   if (doencrypt) 
     generate_next_challenge(smb_buf(outbuf));
-#endif
 
   Protocol = PROTOCOL_LANMAN1;
 
   if (lp_security() == SEC_SERVER && server_cryptkey(outbuf)) {
     DEBUG(3,("using password server validation\n"));
-#ifdef SMB_PASSWD
   if (doencrypt) set_challenge(smb_buf(outbuf));    
-#endif
   }
 
   CVAL(outbuf,smb_flg) = 0x81; /* Reply, SMBlockread, SMBwritelock supported */
-  SSVAL(outbuf,smb_vwv2,maxxmit);
+  SSVAL(outbuf,smb_mid,mid); /* Restore possibly corrupted mid */
+  SSVAL(outbuf,smb_vwv2,max_recv);
   SSVAL(outbuf,smb_vwv3,lp_maxmux()); /* maxmux */
   SSVAL(outbuf,smb_vwv4,1);
   SSVAL(outbuf,smb_vwv5,raw); /* tell redirector we support
@@ -2294,17 +3516,20 @@ int reply_lanman2(char *outbuf)
   int secword=0;
   BOOL doencrypt = SMBENCRYPT();
   time_t t = time(NULL);
+  /* We need to save and restore this as it can be destroyed
+     if we call another server if security=server
+     Thanks to Paul Nelson @ Thursby for pointing this out.
+   */
+  uint16 mid = SVAL(outbuf, smb_mid);
 
   if (lp_security()>=SEC_USER) secword |= 1;
   if (doencrypt) secword |= 2;
 
   set_message(outbuf,13,doencrypt?8:0,True);
   SSVAL(outbuf,smb_vwv1,secword); 
-#ifdef SMB_PASSWD
   /* Create a token value and add it to the outgoing packet. */
   if (doencrypt) 
     generate_next_challenge(smb_buf(outbuf));
-#endif
 
   SIVAL(outbuf,smb_vwv6,getpid());
 
@@ -2312,13 +3537,12 @@ int reply_lanman2(char *outbuf)
 
   if (lp_security() == SEC_SERVER && server_cryptkey(outbuf)) {
     DEBUG(3,("using password server validation\n"));
-#ifdef SMB_PASSWD
     if (doencrypt) set_challenge(smb_buf(outbuf));    
-#endif
   }
 
   CVAL(outbuf,smb_flg) = 0x81; /* Reply, SMBlockread, SMBwritelock supported */
-  SSVAL(outbuf,smb_vwv2,maxxmit);
+  SSVAL(outbuf,smb_mid,mid); /* Restore possibly corrupted mid */
+  SSVAL(outbuf,smb_vwv2,max_recv);
   SSVAL(outbuf,smb_vwv3,lp_maxmux()); 
   SSVAL(outbuf,smb_vwv4,1);
   SSVAL(outbuf,smb_vwv5,raw); /* readbraw and/or writebraw */
@@ -2328,56 +3552,91 @@ int reply_lanman2(char *outbuf)
   return (smb_len(outbuf)+4);
 }
 
+
 /****************************************************************************
 reply for the nt protocol
 ****************************************************************************/
 int reply_nt1(char *outbuf)
 {
-  int capabilities=0x300; /* has dual names + lock_and_read */
+  /* dual names + lock_and_read + nt SMBs + remote API calls */
+  int capabilities = CAP_NT_FIND|CAP_LOCK_AND_READ;
+/*
+  other valid capabilities which we may support at some time...
+                     CAP_LARGE_FILES|CAP_NT_SMBS|CAP_RPC_REMOTE_APIS;
+                     CAP_LARGE_FILES|CAP_LARGE_READX|
+                     CAP_STATUS32|CAP_LEVEL_II_OPLOCKS;
+ */
+
   int secword=0;
   BOOL doencrypt = SMBENCRYPT();
   time_t t = time(NULL);
+  int data_len;
+  int encrypt_len;
+  char challenge_len = 8;
+  /* We need to save and restore this as it can be destroyed
+     if we call another server if security=server
+     Thanks to Paul Nelson @ Thursby for pointing this out.
+   */
+  uint16 mid = SVAL(outbuf, smb_mid);
+
+  if (lp_readraw() && lp_writeraw())
+  {
+    capabilities |= CAP_RAW_MODE;
+  }
 
   if (lp_security()>=SEC_USER) secword |= 1;
   if (doencrypt) secword |= 2;
 
-  set_message(outbuf,17,doencrypt?8:0,True);
+  /* decide where (if) to put the encryption challenge, and
+     follow it with the OEM'd domain name
+   */
+  encrypt_len = doencrypt?challenge_len:0;
+#if UNICODE
+  data_len = encrypt_len + 2*(strlen(myworkgroup)+1);
+#else
+  data_len = encrypt_len + strlen(myworkgroup) + 1;
+#endif
+
+  set_message(outbuf,17,data_len,True);
+
+#if UNICODE
+  /* put the OEM'd domain name */
+  PutUniCode(smb_buf(outbuf)+encrypt_len,myworkgroup);
+#else
+  strcpy(smb_buf(outbuf)+encrypt_len, myworkgroup);
+#endif
+
   CVAL(outbuf,smb_vwv1) = secword;
-#ifdef SMB_PASSWD
   /* Create a token value and add it to the outgoing packet. */
-  if (doencrypt) {
+  if (doencrypt)
+  {
     generate_next_challenge(smb_buf(outbuf));
+
     /* Tell the nt machine how long the challenge is. */
-    SSVALS(outbuf,smb_vwv16+1,8);
+    SSVALS(outbuf,smb_vwv16+1,challenge_len);
   }
-#endif
-
-  SIVAL(outbuf,smb_vwv7+1,getpid()); /* session key */
 
   Protocol = PROTOCOL_NT1;
 
   if (lp_security() == SEC_SERVER && server_cryptkey(outbuf)) {
     DEBUG(3,("using password server validation\n"));
-#ifdef SMB_PASSWD
     if (doencrypt) set_challenge(smb_buf(outbuf));    
-#endif
   }
 
-  if (lp_readraw() && lp_writeraw())
-    capabilities |= 1;
-
+  SSVAL(outbuf,smb_mid,mid); /* Restore possibly corrupted mid */
   SSVAL(outbuf,smb_vwv1+1,lp_maxmux()); /* maxmpx */
   SSVAL(outbuf,smb_vwv2+1,1); /* num vcs */
-  SIVAL(outbuf,smb_vwv3+1,0xFFFF); /* max buffer */
-  SIVAL(outbuf,smb_vwv5+1,0xFFFF); /* raw size */
+  SIVAL(outbuf,smb_vwv3+1,0xffff); /* max buffer. LOTS! */
+  SIVAL(outbuf,smb_vwv5+1,0xffff); /* raw size. LOTS! */
+  SIVAL(outbuf,smb_vwv7+1,getpid()); /* session key */
   SIVAL(outbuf,smb_vwv9+1,capabilities); /* capabilities */
   put_long_date(outbuf+smb_vwv11+1,t);
   SSVALS(outbuf,smb_vwv15+1,TimeDiff(t)/60);
+  SSVAL(outbuf,smb_vwv17,data_len); /* length of challenge+domain strings */
 
   return (smb_len(outbuf)+4);
 }
 
-
 /* these are the protocol lists used for auto architecture detection:
 
 WinNT 3.51:
@@ -2462,7 +3721,6 @@ struct {
 ****************************************************************************/
 static int reply_negprot(char *inbuf,char *outbuf)
 {
-  extern fstring remote_arch;
   int outsize = set_message(outbuf,1,0,True);
   int Index=0;
   int choice= -1;
@@ -2502,22 +3760,22 @@ static int reply_negprot(char *inbuf,char *outbuf)
     
   switch ( arch ) {
   case ARCH_SAMBA:
-    strcpy(remote_arch,"Samba");
+    set_remote_arch(RA_SAMBA);
     break;
   case ARCH_WFWG:
-    strcpy(remote_arch,"WfWg");
+    set_remote_arch(RA_WFWG);
     break;
   case ARCH_WIN95:
-    strcpy(remote_arch,"Win95");
+    set_remote_arch(RA_WIN95);
     break;
   case ARCH_WINNT:
-    strcpy(remote_arch,"WinNT");
+    set_remote_arch(RA_WINNT);
     break;
   case ARCH_OS2:
-    strcpy(remote_arch,"OS2");
+    set_remote_arch(RA_OS2);
     break;
   default:
-    strcpy(remote_arch,"UNKNOWN");
+    set_remote_arch(RA_UNKNOWN);
     break;
   }
  
@@ -2549,7 +3807,7 @@ static int reply_negprot(char *inbuf,char *outbuf)
   SSVAL(outbuf,smb_vwv0,choice);
   if(choice != -1) {
     extern fstring remote_proto;
-    strcpy(remote_proto,supported_protocols[protocol].short_name);
+    fstrcpy(remote_proto,supported_protocols[protocol].short_name);
     reload_services(True);          
     outsize = supported_protocols[protocol].proto_reply_fn(outbuf);
     DEBUG(3,("Selected protocol %s\n",supported_protocols[protocol].proto_name));
@@ -2565,41 +3823,6 @@ static int reply_negprot(char *inbuf,char *outbuf)
 }
 
 
-/****************************************************************************
-  parse a connect packet
-****************************************************************************/
-void parse_connect(char *buf,char *service,char *user,char *password,int *pwlen,char *dev)
-{
-  char *p = smb_buf(buf) + 1;
-  char *p2;
-
-  DEBUG(4,("parsing connect string %s\n",p));
-    
-  p2 = strrchr(p,'\\');
-  if (p2 == NULL)
-    strcpy(service,p);
-  else
-    strcpy(service,p2+1);
-  
-  p += strlen(p) + 2;
-  
-  strcpy(password,p);
-  *pwlen = strlen(password);
-
-  p += strlen(p) + 2;
-
-  strcpy(dev,p);
-  
-  *user = 0;
-  p = strchr(service,'%');
-  if (p != NULL)
-    {
-      *p = 0;
-      strcpy(user,p+1);
-    }
-}
-
-
 /****************************************************************************
 close all open files for a connection
 ****************************************************************************/
@@ -2617,10 +3840,8 @@ static void close_open_files(int cnum)
 /****************************************************************************
 close a cnum
 ****************************************************************************/
-void close_cnum(int cnum, int uid)
+void close_cnum(int cnum, uint16 vuid)
 {
-  extern struct from_host Client_info;
-
   DirCacheFlush(SNUM(cnum));
 
   unbecome_user();
@@ -2633,7 +3854,7 @@ void close_cnum(int cnum, int uid)
 
   DEBUG(IS_IPC(cnum)?3:1,("%s %s (%s) closed connection to service %s\n",
                          timestring(),
-                         Client_info.name,Client_info.addr,
+                         remote_machine,client_addr(),
                          lp_servicename(SNUM(cnum))));
 
   yield_connection(cnum,
@@ -2647,12 +3868,12 @@ void close_cnum(int cnum, int uid)
   dptr_closecnum(cnum);
 
   /* execute any "postexec = " line */
-  if (*lp_postexec(SNUM(cnum)) && become_user(cnum,uid))
+  if (*lp_postexec(SNUM(cnum)) && become_user(cnum,vuid))
     {
       pstring cmd;
       strcpy(cmd,lp_postexec(SNUM(cnum)));
       standard_sub(cnum,cmd);
-      smbrun(cmd,NULL);
+      smbrun(cmd,NULL,False);
       unbecome_user();
     }
 
@@ -2663,7 +3884,7 @@ void close_cnum(int cnum, int uid)
       pstring cmd;
       strcpy(cmd,lp_rootpostexec(SNUM(cnum)));
       standard_sub(cnum,cmd);
-      smbrun(cmd,NULL);
+      smbrun(cmd,NULL,False);
     }
 
   Connections[cnum].open = False;
@@ -2678,6 +3899,9 @@ void close_cnum(int cnum, int uid)
       Connections[cnum].ngroups = 0;
     }
 
+  free_namearray(Connections[cnum].veto_list);
+  free_namearray(Connections[cnum].hide_list);
+
   string_set(&Connections[cnum].user,"");
   string_set(&Connections[cnum].dirpath,"");
   string_set(&Connections[cnum].connectpath,"");
@@ -2702,7 +3926,7 @@ BOOL yield_connection(int cnum,char *name,int max_connections)
 
   bzero(&crec,sizeof(crec));
 
-  strcpy(fname,lp_lockdir());
+  pstrcpy(fname,lp_lockdir());
   standard_sub(cnum,fname);
   trim_string(fname,"","/");
 
@@ -2713,7 +3937,7 @@ BOOL yield_connection(int cnum,char *name,int max_connections)
   f = fopen(fname,"r+");
   if (!f)
     {
-      DEBUG(2,("Coudn't open lock file %s (%s)\n",fname,strerror(errno)));
+      DEBUG(2,("Couldn't open lock file %s (%s)\n",fname,strerror(errno)));
       return(False);
     }
 
@@ -2774,7 +3998,7 @@ BOOL claim_connection(int cnum,char *name,int max_connections,BOOL Clear)
 
   DEBUG(5,("trying claim %s %s %d\n",lp_lockdir(),name,max_connections));
 
-  strcpy(fname,lp_lockdir());
+  pstrcpy(fname,lp_lockdir());
   standard_sub(cnum,fname);
   trim_string(fname,"","/");
 
@@ -2787,8 +4011,10 @@ BOOL claim_connection(int cnum,char *name,int max_connections,BOOL Clear)
 
   if (!file_exist(fname,NULL))
     {
+      int oldmask = umask(022);
       f = fopen(fname,"w");
       if (f) fclose(f);
+      umask(oldmask);
     }
 
   total_recs = file_size(fname) / sizeof(crec);
@@ -2845,11 +4071,8 @@ BOOL claim_connection(int cnum,char *name,int max_connections,BOOL Clear)
   StrnCpy(crec.name,lp_servicename(snum),sizeof(crec.name)-1);
   crec.start = time(NULL);
 
-  {
-    extern struct from_host Client_info;
-    StrnCpy(crec.machine,Client_info.name,sizeof(crec.machine)-1);
-    StrnCpy(crec.addr,Client_info.addr,sizeof(crec.addr)-1);
-  }
+  StrnCpy(crec.machine,remote_machine,sizeof(crec.machine)-1);
+  StrnCpy(crec.addr,client_addr(),sizeof(crec.addr)-1);
   
   /* make our mark */
   if (fseek(f,foundi*sizeof(crec),SEEK_SET) != 0 ||
@@ -2871,7 +4094,7 @@ static BOOL dump_core(void)
 {
   char *p;
   pstring dname;
-  strcpy(dname,debugf);
+  pstrcpy(dname,debugf);
   if ((p=strrchr(dname,'/'))) *p=0;
   strcat(dname,"/corefiles");
   mkdir(dname,0700);
@@ -2914,7 +4137,7 @@ void exit_server(char *reason)
   DEBUG(2,("Closing connections\n"));
   for (i=0;i<MAX_CONNECTIONS;i++)
     if (Connections[i].open)
-      close_cnum(i,-1);
+      close_cnum(i,(uint16)-1);
 #ifdef DFS_AUTH
   if (dcelogin_atmost_once)
     dfs_unlogin();
@@ -2931,6 +4154,11 @@ void exit_server(char *reason)
     if (dump_core()) return;
 #endif
   }    
+
+#ifdef FAST_SHARE_MODES
+  stop_share_mode_mgmt();
+#endif /* FAST_SHARE_MODES */
+
   DEBUG(3,("%s Server exit  (%s)\n",timestring(),reason?reason:""));
   exit(0);
 }
@@ -2938,22 +4166,28 @@ void exit_server(char *reason)
 /****************************************************************************
 do some standard substitutions in a string
 ****************************************************************************/
-void standard_sub(int cnum,char *s)
+void standard_sub(int cnum,char *str)
 {
-  if (!strchr(s,'%')) return;
-
-  if (VALID_CNUM(cnum))
-    {
-      string_sub(s,"%S",lp_servicename(Connections[cnum].service));
-      string_sub(s,"%P",Connections[cnum].connectpath);
-      string_sub(s,"%u",Connections[cnum].user);
-      if (strstr(s,"%H")) {
-       char *home = get_home_dir(Connections[cnum].user);
-       if (home) string_sub(s,"%H",home);
+  if (VALID_CNUM(cnum)) {
+    char *p, *s, *home;
+
+    for ( s=str ; (p=strchr(s, '%')) != NULL ; s=p ) {
+      switch (*(p+1)) {
+        case 'H' : if ((home = get_home_dir(Connections[cnum].user))!=NULL)
+                     string_sub(p,"%H",home);
+                   else
+                     p += 2;
+                   break;
+        case 'P' : string_sub(p,"%P",Connections[cnum].connectpath); break;
+        case 'S' : string_sub(p,"%S",lp_servicename(Connections[cnum].service)); break;
+        case 'g' : string_sub(p,"%g",gidtoname(Connections[cnum].gid)); break;
+        case 'u' : string_sub(p,"%u",Connections[cnum].user); break;
+        case '\0' : p++; break; /* don't run off the end of the string */
+        default  : p+=2; break;
       }
-      string_sub(s,"%g",gidtoname(Connections[cnum].gid));
     }
-  standard_sub_basic(s);
+  }
+  standard_sub_basic(str);
 }
 
 /*
@@ -2997,7 +4231,7 @@ struct smb_message_struct
    {SMBecho,"SMBecho",reply_echo,0},
    {SMBsesssetupX,"SMBsesssetupX",reply_sesssetup_and_X,0},
    {SMBtconX,"SMBtconX",reply_tcon_and_X,0},
-   {SMBulogoffX, "SMBulogoffX", reply_ulogoffX, 0}, 
+   {SMBulogoffX, "SMBulogoffX", reply_ulogoffX, 0}, /* ulogoff doesn't give a valid TID */
    {SMBgetatr,"SMBgetatr",reply_getatr,AS_USER},
    {SMBsetatr,"SMBsetatr",reply_setatr,AS_USER | NEED_WRITE},
    {SMBchkpth,"SMBchkpth",reply_chkpth,AS_USER},
@@ -3011,7 +4245,7 @@ struct smb_message_struct
    {SMBunlink,"SMBunlink",reply_unlink,AS_USER | NEED_WRITE},
    {SMBread,"SMBread",reply_read,AS_USER},
    {SMBwrite,"SMBwrite",reply_write,AS_USER},
-   {SMBclose,"SMBclose",reply_close,AS_USER},
+   {SMBclose,"SMBclose",reply_close,AS_USER | CAN_IPC},
    {SMBmkdir,"SMBmkdir",reply_mkdir,AS_USER | NEED_WRITE},
    {SMBrmdir,"SMBrmdir",reply_rmdir,AS_USER | NEED_WRITE},
    {SMBdskattr,"SMBdskattr",reply_dskattr,AS_USER},
@@ -3026,7 +4260,7 @@ struct smb_message_struct
    {SMBctemp,"SMBctemp",reply_ctemp,AS_USER},
    {SMBsplopen,"SMBsplopen",reply_printopen,AS_USER},
    {SMBsplclose,"SMBsplclose",reply_printclose,AS_USER},
-   {SMBsplretq,"SMBsplretq",reply_printqueue,AS_USER},
+   {SMBsplretq,"SMBsplretq",reply_printqueue,AS_USER|AS_GUEST},
    {SMBsplwr,"SMBsplwr",reply_printwrite,AS_USER},
    {SMBlock,"SMBlock",reply_lock,AS_USER},
    {SMBunlock,"SMBunlock",reply_unlock,AS_USER},
@@ -3054,7 +4288,7 @@ struct smb_message_struct
    {SMBcopy,"SMBcopy",reply_copy,AS_USER | NEED_WRITE},
    {SMBmove,"SMBmove",NULL,AS_USER | NEED_WRITE},
    
-   {SMBopenX,"SMBopenX",reply_open_and_X,AS_USER},
+   {SMBopenX,"SMBopenX",reply_open_and_X,AS_USER | CAN_IPC},
    {SMBreadX,"SMBreadX",reply_read_and_X,AS_USER},
    {SMBwriteX,"SMBwriteX",reply_write_and_X,AS_USER},
    {SMBlockingX,"SMBlockingX",reply_lockingX,AS_USER},
@@ -3152,15 +4386,24 @@ static int switch_message(int type,char *inbuf,char *outbuf,int size,int bufsize
        {
          int cnum = SVAL(inbuf,smb_tid);
          int flags = smb_messages[match].flags;
-         int uid = SVAL(inbuf,smb_uid);
+         uint16 session_tag = SVAL(inbuf,smb_uid);
 
          /* does this protocol need to be run as root? */
          if (!(flags & AS_USER))
            unbecome_user();
 
          /* does this protocol need to be run as the connected user? */
-         if ((flags & AS_USER) && !become_user(cnum,uid))
-           return(ERROR(ERRSRV,ERRinvnid));
+         if ((flags & AS_USER) && !become_user(cnum,session_tag)) {
+           if (flags & AS_GUEST) 
+             flags &= ~AS_USER;
+           else
+             return(ERROR(ERRSRV,ERRinvnid));
+         }
+         /* this code is to work around a bug is MS client 3 without
+            introducing a security hole - it needs to be able to do
+            print queue checks as guest if it isn't logged in properly */
+         if (flags & AS_USER)
+           flags &= ~AS_GUEST;
 
          /* does it need write permission? */
          if ((flags & NEED_WRITE) && !CAN_WRITE(cnum))
@@ -3212,103 +4455,99 @@ static int switch_message(int type,char *inbuf,char *outbuf,int size,int bufsize
 
 
 /****************************************************************************
-construct a chained reply and add it to the already made reply
-
-inbuf points to the original message start.
-inbuf2 points to the smb_wct part of the secondary message
-type is the type of the secondary message
-outbuf points to the original outbuffer
-outbuf2 points to the smb_wct field of the new outbuffer
-size is the total length of the incoming message (from inbuf1)
-bufsize is the total buffer size
-
-return how many bytes were added to the response
-****************************************************************************/
-int chain_reply(int type,char *inbuf,char *inbuf2,char *outbuf,char *outbuf2,int size,int bufsize)
+  construct a chained reply and add it to the already made reply
+  **************************************************************************/
+int chain_reply(char *inbuf,char *outbuf,int size,int bufsize)
 {
-  int outsize = 0;
-  char *ibuf,*obuf;
-  static BOOL in_chain = False;
-  static char *last_outbuf=NULL;
-  BOOL was_inchain = in_chain;
-  int insize_remaining;
-  static int insize_deleted;
-
-  chain_size += PTR_DIFF(outbuf2,outbuf) - smb_wct;
-  if (was_inchain)
-    outbuf = last_outbuf;
-  else
-    insize_deleted = 0;
+  static char *orig_inbuf;
+  static char *orig_outbuf;
+  int smb_com1, smb_com2 = CVAL(inbuf,smb_vwv0);
+  unsigned smb_off2 = SVAL(inbuf,smb_vwv1);
+  char *inbuf2, *outbuf2;
+  int outsize2;
+  char inbuf_saved[smb_wct];
+  char outbuf_saved[smb_wct];
+  extern int chain_size;
+  int wct = CVAL(outbuf,smb_wct);
+  int outsize = smb_size + 2*wct + SVAL(outbuf,smb_vwv0+2*wct);
+
+  /* maybe its not chained */
+  if (smb_com2 == 0xFF) {
+    CVAL(outbuf,smb_vwv0) = 0xFF;
+    return outsize;
+  }
 
+  if (chain_size == 0) {
+    /* this is the first part of the chain */
+    orig_inbuf = inbuf;
+    orig_outbuf = outbuf;
+  }
 
-  insize_deleted = 0;
-  inbuf2 -= insize_deleted;
-  insize_remaining = size - PTR_DIFF(inbuf2,inbuf);
-  insize_deleted += size - (insize_remaining + smb_wct);
+  /* we need to tell the client where the next part of the reply will be */
+  SSVAL(outbuf,smb_vwv1,smb_offset(outbuf+outsize,outbuf));
+  CVAL(outbuf,smb_vwv0) = smb_com2;
 
-  in_chain = True;
-  last_outbuf = outbuf;
+  /* remember how much the caller added to the chain, only counting stuff
+     after the parameter words */
+  chain_size += outsize - smb_wct;
 
+  /* work out pointers into the original packets. The
+     headers on these need to be filled in */
+  inbuf2 = orig_inbuf + smb_off2 + 4 - smb_wct;
+  outbuf2 = orig_outbuf + SVAL(outbuf,smb_vwv1) + 4 - smb_wct;
 
-  /* allocate some space for the in and out buffers of the chained message */
-  ibuf = (char *)malloc(size + SAFETY_MARGIN);
-  obuf = (char *)malloc(bufsize + SAFETY_MARGIN);
+  /* remember the original command type */
+  smb_com1 = CVAL(orig_inbuf,smb_com);
 
-  if (!ibuf || !obuf)
-    {
-      DEBUG(0,("Out of memory in chain reply\n"));
-      return(ERROR(ERRSRV,ERRnoresource));
-    }
+  /* save the data which will be overwritten by the new headers */
+  memcpy(inbuf_saved,inbuf2,smb_wct);
+  memcpy(outbuf_saved,outbuf2,smb_wct);
 
-  ibuf += SMB_ALIGNMENT;
-  obuf += SMB_ALIGNMENT;
+  /* give the new packet the same header as the last part of the SMB */
+  memmove(inbuf2,inbuf,smb_wct);
 
   /* create the in buffer */
-  memcpy(ibuf,inbuf,smb_wct);
-  memcpy(ibuf+smb_wct,inbuf2,insize_remaining);
-  CVAL(ibuf,smb_com) = type;
+  CVAL(inbuf2,smb_com) = smb_com2;
 
   /* create the out buffer */
-  bzero(obuf,smb_size);
-
-  set_message(obuf,0,0,True);
-  CVAL(obuf,smb_com) = CVAL(ibuf,smb_com);
+  bzero(outbuf2,smb_size);
+  set_message(outbuf2,0,0,True);
+  CVAL(outbuf2,smb_com) = CVAL(inbuf2,smb_com);
   
-  memcpy(obuf+4,ibuf+4,4);
-  CVAL(obuf,smb_rcls) = SUCCESS;
-  CVAL(obuf,smb_reh) = 0;
-  CVAL(obuf,smb_flg) = 0x80 | (CVAL(ibuf,smb_flg) & 0x8); /* bit 7 set 
-                                                            means a reply */
-  SSVAL(obuf,smb_flg2,1); /* say we support long filenames */
-  SSVAL(obuf,smb_err,SUCCESS);
-  SSVAL(obuf,smb_tid,SVAL(inbuf,smb_tid));
-  SSVAL(obuf,smb_pid,SVAL(inbuf,smb_pid));
-  SSVAL(obuf,smb_uid,SVAL(inbuf,smb_uid));
-  SSVAL(obuf,smb_mid,SVAL(inbuf,smb_mid));
+  memcpy(outbuf2+4,inbuf2+4,4);
+  CVAL(outbuf2,smb_rcls) = SUCCESS;
+  CVAL(outbuf2,smb_reh) = 0;
+  CVAL(outbuf2,smb_flg) = 0x80 | (CVAL(inbuf2,smb_flg) & 0x8); /* bit 7 set 
+                                                                 means a reply */
+  SSVAL(outbuf2,smb_flg2,1); /* say we support long filenames */
+  SSVAL(outbuf2,smb_err,SUCCESS);
+  SSVAL(outbuf2,smb_tid,SVAL(inbuf2,smb_tid));
+  SSVAL(outbuf2,smb_pid,SVAL(inbuf2,smb_pid));
+  SSVAL(outbuf2,smb_uid,SVAL(inbuf2,smb_uid));
+  SSVAL(outbuf2,smb_mid,SVAL(inbuf2,smb_mid));
 
   DEBUG(3,("Chained message\n"));
-  show_msg(ibuf);
+  show_msg(inbuf2);
 
   /* process the request */
-  outsize = switch_message(type,ibuf,obuf,smb_wct+insize_remaining,
-                          bufsize-chain_size);
+  outsize2 = switch_message(smb_com2,inbuf2,outbuf2,size-chain_size,
+                           bufsize-chain_size);
 
-  /* copy the new reply header over the old one, but preserve 
-     the smb_com field */
-  memcpy(outbuf+smb_com+1,obuf+smb_com+1,smb_wct-(smb_com+1));
+  /* copy the new reply and request headers over the old ones, but
+     preserve the smb_com field */
+  memmove(orig_outbuf,outbuf2,smb_wct);
+  CVAL(orig_outbuf,smb_com) = smb_com1;
 
-  /* and copy the data from the reply to the right spot */
-  memcpy(outbuf2,obuf+smb_wct,outsize - smb_wct);
-
-  /* free the allocated buffers */
-  if (ibuf) free(ibuf-SMB_ALIGNMENT);
-  if (obuf) free(obuf-SMB_ALIGNMENT);
-
-  in_chain = was_inchain;
+  /* restore the saved data, being careful not to overwrite any
+   data from the reply header */
+  memcpy(inbuf2,inbuf_saved,smb_wct);
+  {
+    int ofs = smb_wct - PTR_DIFF(outbuf2,orig_outbuf);
+    if (ofs < 0) ofs = 0;
+    memmove(outbuf2+ofs,outbuf_saved+ofs,smb_wct-ofs);
+  }
 
-  /* return how much extra has been added to the packet */
-  return(outsize - smb_wct);
+  return outsize2;
 }
 
 
@@ -3321,10 +4560,12 @@ int construct_reply(char *inbuf,char *outbuf,int size,int bufsize)
   int type = CVAL(inbuf,smb_com);
   int outsize = 0;
   int msg_type = CVAL(inbuf,0);
+  extern int chain_size;
 
   smb_last_time = time(NULL);
 
   chain_size = 0;
+  chain_fnum = -1;
 
   bzero(outbuf,smb_size);
 
@@ -3348,24 +4589,20 @@ int construct_reply(char *inbuf,char *outbuf,int size,int bufsize)
 
   outsize = switch_message(type,inbuf,outbuf,size,bufsize);
 
+  outsize += chain_size;
+
   if(outsize > 4)
     smb_setlen(outbuf,outsize - 4);
   return(outsize);
 }
 
-
 /****************************************************************************
   process commands from the client
 ****************************************************************************/
 static void process(void)
 {
-  static int trans_num = 0;
-  int nread;
-  extern struct from_host Client_info;
   extern int Client;
 
-  fromhost(Client,&Client_info);
-  
   InBuffer = (char *)malloc(BUFFER_SIZE + SAFETY_MARGIN);
   OutBuffer = (char *)malloc(BUFFER_SIZE + SAFETY_MARGIN);
   if ((InBuffer == NULL) || (OutBuffer == NULL)) 
@@ -3386,156 +4623,111 @@ static void process(void)
 #endif    
 
   while (True)
-    {
-      int32 len;      
-      int msg_type;
-      int msg_flags;
-      int type;
-      int deadtime = lp_deadtime()*60;
-      int counter;
-      int last_keepalive=0;
+  {
+    int deadtime = lp_deadtime()*60;
+    int counter;
+    int last_keepalive=0;
+    int service_load_counter = 0;
+    BOOL got_smb = False;
+
+    if (deadtime <= 0)
+      deadtime = DEFAULT_SMBD_TIMEOUT;
 
-      if (deadtime <= 0)
-       deadtime = DEFAULT_SMBD_TIMEOUT;
+    if (lp_readprediction())
+      do_read_prediction();
 
-      if (lp_readprediction())
-       do_read_prediction();
+    errno = 0;      
+
+    for (counter=SMBD_SELECT_LOOP; 
+          !receive_message_or_smb(Client,oplock_sock,
+                      InBuffer,BUFFER_SIZE,SMBD_SELECT_LOOP*1000,&got_smb); 
+          counter += SMBD_SELECT_LOOP)
+    {
+      int i;
+      time_t t;
+      BOOL allidle = True;
+      extern int keepalive;
 
+      if (counter > 365 * 3600) /* big number of seconds. */
       {
-       extern pstring share_del_pending;
-       if (*share_del_pending) {
-         unbecome_user();
-         if (!unlink(share_del_pending))
-           DEBUG(3,("Share file deleted %s\n",share_del_pending));
-         else
-           DEBUG(2,("Share del failed of %s\n",share_del_pending));
-         share_del_pending[0] = 0;
-       }
+        counter = 0;
+        service_load_counter = 0;
       }
 
-      if (share_mode_pending) {
-       unbecome_user();
-       check_share_modes();
-       share_mode_pending=False;
+      if (smb_read_error == READ_EOF) 
+      {
+        DEBUG(3,("end of file from client\n"));
+        return;
       }
 
-      errno = 0;      
-
-      for (counter=SMBD_SELECT_LOOP; 
-          !receive_smb(Client,InBuffer,SMBD_SELECT_LOOP*1000); 
-          counter += SMBD_SELECT_LOOP)
-       {
-         int i;
-         time_t t;
-         BOOL allidle = True;
-         extern int keepalive;
-
-         if (smb_read_error == READ_EOF) {
-           DEBUG(3,("end of file from client\n"));
-           return;
-         }
-
-         if (smb_read_error == READ_ERROR) {
-           DEBUG(3,("receive_smb error (%s) exiting\n",
-                    strerror(errno)));
-           return;
-         }
-
-         t = time(NULL);
-
-         /* become root again if waiting */
-         unbecome_user();
-
-         /* check for smb.conf reload */
-         if (!(counter%SMBD_RELOAD_CHECK))
-           reload_services(True);
-
-         /* check the share modes every 10 secs */
-         if (!(counter%SHARE_MODES_CHECK))
-           check_share_modes();
-
-         /* clean the share modes every 5 minutes */
-         if (!(counter%SHARE_MODES_CLEAN))
-           clean_share_files();
-
-         /* automatic timeout if all connections are closed */      
-         if (num_connections_open==0 && counter >= IDLE_CLOSED_TIMEOUT) {
-           DEBUG(2,("%s Closing idle connection\n",timestring()));
-           return;
-         }
-
-         if (keepalive && (counter-last_keepalive)>keepalive) {
-           extern int password_client;
-           if (!send_keepalive(Client)) {
-             DEBUG(2,("%s Keepalive failed - exiting\n",timestring()));
-             return;
-           }       
-           /* also send a keepalive to the password server if its still
-              connected */
-           if (password_client != -1)
-             send_keepalive(password_client);
-           last_keepalive = counter;
-         }
-
-         /* check for connection timeouts */
-         for (i=0;i<MAX_CONNECTIONS;i++)
-           if (Connections[i].open)
-             {
-               /* close dirptrs on connections that are idle */
-               if ((t-Connections[i].lastused)>DPTR_IDLE_TIMEOUT)
-                 dptr_idlecnum(i);
-
-               if (Connections[i].num_files_open > 0 ||
-                   (t-Connections[i].lastused)<deadtime)
-                 allidle = False;
-             }
-
-         if (allidle && num_connections_open>0) {
-           DEBUG(2,("%s Closing idle connection 2\n",timestring()));
-           return;
-         }
-       }
+      if (smb_read_error == READ_ERROR) 
+      {
+        DEBUG(3,("receive_smb error (%s) exiting\n",
+                  strerror(errno)));
+        return;
+      }
 
-      msg_type = CVAL(InBuffer,0);
-      msg_flags = CVAL(InBuffer,1);
-      type = CVAL(InBuffer,smb_com);
+      t = time(NULL);
 
-      len = smb_len(InBuffer);
+      /* become root again if waiting */
+      unbecome_user();
 
-      DEBUG(6,("got message type 0x%x of len 0x%x\n",msg_type,len));
+      /* check for smb.conf reload */
+      if (counter >= service_load_counter + SMBD_RELOAD_CHECK)
+      {
+        service_load_counter = counter;
 
-      nread = len + 4;
-      
-      DEBUG(3,("%s Transaction %d of length %d\n",timestring(),trans_num,nread));
+        /* reload services, if files have changed. */
+        reload_services(True);
+      }
 
-#ifdef WITH_VTP
-      if(trans_num == 1 && VT_Check(InBuffer)) {
-        VT_Process();
+      /* automatic timeout if all connections are closed */      
+      if (num_connections_open==0 && counter >= IDLE_CLOSED_TIMEOUT) 
+      {
+        DEBUG(2,("%s Closing idle connection\n",timestring()));
         return;
       }
-#endif
 
+      if (keepalive && (counter-last_keepalive)>keepalive) 
+      {
+        extern int password_client;
+        if (!send_keepalive(Client))
+        { 
+          DEBUG(2,("%s Keepalive failed - exiting\n",timestring()));
+          return;
+        }          
+        /* also send a keepalive to the password server if its still
+           connected */
+        if (password_client != -1)
+          send_keepalive(password_client);
+        last_keepalive = counter;
+      }
+
+      /* check for connection timeouts */
+      for (i=0;i<MAX_CONNECTIONS;i++)
+        if (Connections[i].open)
+        {
+          /* close dirptrs on connections that are idle */
+          if ((t-Connections[i].lastused)>DPTR_IDLE_TIMEOUT)
+            dptr_idlecnum(i);
 
-      if (msg_type == 0)
-       show_msg(InBuffer);
+          if (Connections[i].num_files_open > 0 ||
+                     (t-Connections[i].lastused)<deadtime)
+            allidle = False;
+        }
 
-      nread = construct_reply(InBuffer,OutBuffer,nread,maxxmit);
-      
-      if(nread > 0) {
-        if (CVAL(OutBuffer,0) == 0)
-         show_msg(OutBuffer);
-       
-        if (nread != smb_len(OutBuffer) + 4) 
-         {
-           DEBUG(0,("ERROR: Invalid message response size! %d %d\n",
-                    nread,
-                    smb_len(OutBuffer)));
-         }
-       else
-         send_smb(Client,OutBuffer);
+      if (allidle && num_connections_open>0) 
+      {
+        DEBUG(2,("%s Closing idle connection 2\n",timestring()));
+        return;
       }
-      trans_num++;
     }
+
+    if(got_smb)
+      process_smb(InBuffer, OutBuffer);
+    else
+      process_local_message(oplock_sock, InBuffer, BUFFER_SIZE);
+  }
 }
 
 
@@ -3563,6 +4755,19 @@ static void init_structs(void )
     {
       Files[i].open = False;
       string_init(&Files[i].name,"");
+
+    }
+
+  for (i=0;i<MAX_OPEN_FILES;i++)
+    {
+      file_fd_struct *fd_ptr = &FileFd[i];
+      fd_ptr->ref_count = 0;
+      fd_ptr->dev = (int32)-1;
+      fd_ptr->inode = (int32)-1;
+      fd_ptr->fd = -1;
+      fd_ptr->fd_readonly = -1;
+      fd_ptr->fd_writeonly = -1;
+      fd_ptr->real_open_flags = -1;
     }
 
   init_dptrs();
@@ -3599,6 +4804,7 @@ static void usage(char *pname)
   int port = SMB_PORT;
   int opt;
   extern char *optarg;
+  char pidFile[100] = { 0 };
 
 #ifdef NEED_AUTH_PARAMETERS
   set_auth_parameters(argc,argv);
@@ -3630,8 +4836,13 @@ static void usage(char *pname)
 #endif
 
   fault_setup(exit_server);
+  signal(SIGTERM , SIGNAL_CAST dflt_sig);
+
+  /* we want total control over the permissions on created files,
+     so set our umask to 0 */
+  umask(0);
 
-  umask(0777 & ~DEF_CREATE_MASK);
+  GetWd(OriginalDir);
 
   init_uid();
 
@@ -3642,9 +4853,12 @@ static void usage(char *pname)
       argc--;
     }
 
-  while ((opt = getopt(argc, argv, "O:i:l:s:d:Dp:hPa")) != EOF)
+  while ((opt = getopt(argc, argv, "O:i:l:s:d:Dp:hPaf:")) != EOF)
     switch (opt)
       {
+      case 'f':
+        strncpy(pidFile, optarg, sizeof(pidFile));
+        break;
       case 'O':
        strcpy(user_socket_options,optarg);
        break;
@@ -3693,7 +4907,7 @@ static void usage(char *pname)
   reopen_logs();
 
   DEBUG(2,("%s smbd version %s started\n",timestring(),VERSION));
-  DEBUG(2,("Copyright Andrew Tridgell 1992-1995\n"));
+  DEBUG(2,("Copyright Andrew Tridgell 1992-1997\n"));
 
 #ifndef NO_GETRLIMIT
 #ifdef RLIMIT_NOFILE
@@ -3723,6 +4937,10 @@ static void usage(char *pname)
   if (!reload_services(False))
     return(-1);        
 
+  codepage_initialise(lp_client_code_page());
+
+  strcpy(myworkgroup, lp_workgroup());
+
 #ifndef NO_SIGNAL_TEST
   signal(SIGHUP,SIGNAL_CAST sig_hup);
 #endif
@@ -3741,13 +4959,46 @@ static void usage(char *pname)
       become_daemon();
     }
 
+  if (*pidFile)
+    {
+      int     fd;
+      char    buf[20];
+
+      if ((fd = open(pidFile,
+#ifdef O_NONBLOCK
+         O_NONBLOCK | 
+#endif
+         O_CREAT | O_WRONLY | O_TRUNC, 0644)) < 0)
+        {
+           DEBUG(0,("ERROR: can't open %s: %s\n", pidFile, strerror(errno)));
+           exit(1);
+        }
+      if(fcntl_lock(fd,F_SETLK,0,1,F_WRLCK)==False)
+        {
+          DEBUG(0,("ERROR: smbd is already running\n"));
+          exit(1);
+        }
+      sprintf(buf, "%u\n", (unsigned int) getpid());
+      if (write(fd, buf, strlen(buf)) < 0)
+        {
+          DEBUG(0,("ERROR: can't write to %s: %s\n", pidFile, strerror(errno)));
+          exit(1);
+        }
+      /* Leave pid file open & locked for the duration... */
+    }
+
   if (!open_sockets(is_daemon,port))
     exit(1);
 
+#ifdef FAST_SHARE_MODES
+  if (!start_share_mode_mgmt())
+    exit(1);
+#endif /* FAST_SHARE_MODES */
+
   /* possibly reload the services file. */
   reload_services(True);
 
-  maxxmit = MIN(lp_maxxmit(),BUFFER_SIZE);
+  max_recv = MIN(lp_maxxmit(),BUFFER_SIZE);
 
   if (*lp_rootdir())
     {
@@ -3755,6 +5006,10 @@ static void usage(char *pname)
        DEBUG(2,("%s changed root to %s\n",timestring(),lp_rootdir()));
     }
 
+  /* Setup the oplock IPC socket. */
+  if(!open_oplock_ipc())
+    exit(1);
+
   process();
   close_sockets();