Changes include:
[samba.git] / source3 / smbd / server.c
index e290027d0a482afb7edda19550357f53c4bcf600..c4db5536590e231287ff3a1366ca6b610949f00c 100644 (file)
@@ -101,7 +101,7 @@ extern fstring remote_machine;
 extern pstring OriginalDir;
 
 /* these can be set by some functions to override the error codes */
-int unix_ERR_class=SUCCESS;
+int unix_ERR_class=SMB_SUCCESS;
 int unix_ERR_code=0;
 
 
@@ -464,7 +464,7 @@ static BOOL scan_directory(char *path, char *name,int cnum,BOOL docache)
    * (JRA).
    */
   if (mangled)
-    mangled = !check_mangled_stack(name);
+    mangled = !check_mangled_cache( name );
 
   /* open the directory */
   if (!(cur_dir = OpenDir(cnum, path, True))) 
@@ -659,7 +659,7 @@ BOOL unix_convert(char *name,int cnum,pstring saved_last_component, BOOL *bad_pa
              /* check on the mangled stack to see if we can recover the 
                 base of the filename */
              if (is_mangled(start))
-               check_mangled_stack(start);
+               check_mangled_cache( start );
 
              DEBUG(5,("New file %s\n",start));
              return(True); 
@@ -1060,7 +1060,7 @@ static file_fd_struct *fd_get_already_open(struct stat *sbuf)
 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()
+static file_fd_struct *fd_get_new(void)
 {
   int i;
   file_fd_struct *fd_ptr;
@@ -1514,6 +1514,13 @@ void close_file(int fnum, BOOL normal_close)
   DEBUG(2,("%s %s closed file %s (numopen=%d)\n",
           timestring(),Connections[cnum].user,fs_p->name,
           Connections[cnum].num_files_open));
+
+  if (fs_p->name) {
+         string_free(&fs_p->name);
+  }
+
+  /* we will catch bugs faster by zeroing this structure */
+  memset(fs_p, 0, sizeof(*fs_p));
 }
 
 enum {AFAIL,AREAD,AWRITE,AALL};
@@ -1790,7 +1797,7 @@ void open_file_shared(int fnum,int cnum,char *fname,int share_mode,int ofun,
   {
     unix_ERR_class = ERRDOS;
     /* OS/2 Workplace shell fix may be main code stream in a later release. */ 
-#ifdef OS2_WPS_FIX
+#if 1 /* OS2_WPS_FIX - Recent versions of OS/2 need this. */
     unix_ERR_code = ERRcannotopen;
 #else /* OS2_WPS_FIX */
     unix_ERR_code = ERROR_EAS_NOT_SUPPORTED;
@@ -1829,6 +1836,10 @@ void open_file_shared(int fnum,int cnum,char *fname,int share_mode,int ofun,
       flags = O_RDONLY;
       break;
   }
+
+  if (share_mode&(1<<14)) {
+         flags2 |= O_SYNC;
+  }
   
   if (flags != O_RDONLY && file_existed && 
       (!CAN_WRITE(cnum) || IS_DOS_READONLY(dos_mode(cnum,fname,&sbuf)))) 
@@ -2058,7 +2069,8 @@ int read_file(int fnum,char *data,uint32 pos,int n)
 #if USE_MMAP
   if (Files[fnum].mmap_ptr)
     {
-      int num = MIN(n,(int)(Files[fnum].mmap_size-pos));
+      int num = (Files[fnum].mmap_size > pos) ? (Files[fnum].mmap_size - pos) : -1;
+      num = MIN(n,num);
       if (num > 0)
        {
          memcpy(data,Files[fnum].mmap_ptr+pos,num);
@@ -2213,8 +2225,16 @@ int find_service(char *service)
    /* just possibly it's a default service? */
    if (iService < 0) 
      {
-       char *defservice = lp_defaultservice();
-       if (defservice && *defservice && !strequal(defservice,service)) {
+       char *pdefservice = lp_defaultservice();
+       if (pdefservice && *pdefservice && !strequal(pdefservice,service)) {
+         /*
+          * We need to do a local copy here as lp_defaultservice() 
+          * returns one of the rotating lp_string buffers that
+          * could get overwritten by the recursive find_service() call
+          * below. Fix from Josef Hinteregger <joehtg@joehtg.co.at>.
+          */
+         pstring defservice;
+         pstrcpy(defservice, pdefservice);
         iService = find_service(defservice);
         if (iService >= 0) {
           string_sub(service,"_","/");
@@ -2294,11 +2314,11 @@ int unix_error_packet(char *inbuf,char *outbuf,int def_class,uint32 def_code,int
   int ecode=def_code;
   int i=0;
 
-  if (unix_ERR_class != SUCCESS)
+  if (unix_ERR_class != SMB_SUCCESS)
     {
       eclass = unix_ERR_class;
       ecode = unix_ERR_code;
-      unix_ERR_class = SUCCESS;
+      unix_ERR_class = SMB_SUCCESS;
       unix_ERR_code = 0;
     }
   else
@@ -2325,13 +2345,21 @@ int unix_error_packet(char *inbuf,char *outbuf,int def_class,uint32 def_code,int
 int error_packet(char *inbuf,char *outbuf,int error_class,uint32 error_code,int line)
 {
   int outsize = set_message(outbuf,0,0,True);
-  int cmd;
-  cmd = CVAL(inbuf,smb_com);
-  
-  CVAL(outbuf,smb_rcls) = error_class;
-  SSVAL(outbuf,smb_err,error_code);  
-  
-  DEBUG(3,("%s error packet at line %d cmd=%d (%s) eclass=%d ecode=%d\n",
+  int cmd = CVAL(inbuf,smb_com);
+  int flgs2 = SVAL(outbuf,smb_flg2); 
+
+  if ((flgs2 & FLAGS2_32_BIT_ERROR_CODES) == FLAGS2_32_BIT_ERROR_CODES)
+  {
+    SIVAL(outbuf,smb_rcls,error_code);
+    
+    DEBUG(3,("%s 32 bit error packet at line %d cmd=%d (%s) eclass=%08x [%s]\n",
+           timestring(), line, cmd, smb_fn_name(cmd), error_code, smb_errstr(outbuf)));
+  }
+  else
+  {
+    CVAL(outbuf,smb_rcls) = error_class;
+    SSVAL(outbuf,smb_err,error_code);  
+    DEBUG(3,("%s error packet at line %d cmd=%d (%s) eclass=%d ecode=%d\n",
           timestring(),
           line,
           (int)CVAL(inbuf,smb_com),
@@ -2339,6 +2367,8 @@ int error_packet(char *inbuf,char *outbuf,int error_class,uint32 error_code,int
           error_class,
           error_code));
 
+  }
+  
   if (errno != 0)
     DEBUG(3,("error string = %s\n",strerror(errno)));
   
@@ -2350,7 +2380,7 @@ int error_packet(char *inbuf,char *outbuf,int error_class,uint32 error_code,int
 /****************************************************************************
 this prevents zombie child processes
 ****************************************************************************/
-static int sig_cld()
+static int sig_cld(void)
 {
   static int depth = 0;
   if (depth != 0)
@@ -2395,7 +2425,7 @@ static int sig_cld()
 /****************************************************************************
   this is called when the client exits abruptly
   **************************************************************************/
-static int sig_pipe()
+static int sig_pipe(void)
 {
        struct cli_state *cli;
        BlockSignals(True,SIGPIPE);
@@ -2625,7 +2655,7 @@ static void process_smb(char *inbuf, char *outbuf)
                   name" */
                  static unsigned char buf[5] = {0x83, 0, 0, 1, 0x81};
                  DEBUG(1,("%s Connection denied from %s\n",
-                          timestring(),client_addr()));
+                          timestring(),client_addr(Client)));
                  send_smb(Client,(char *)buf);
                  exit_server("connection denied");
          }
@@ -2668,7 +2698,7 @@ static void process_smb(char *inbuf, char *outbuf)
 /****************************************************************************
   open the oplock IPC socket communication
 ****************************************************************************/
-static BOOL open_oplock_ipc()
+static BOOL open_oplock_ipc(void)
 {
   struct sockaddr_in sock_name;
   int len = sizeof(sock_name);
@@ -3257,7 +3287,7 @@ BOOL reload_services(BOOL test)
 
   lp_killunused(snum_used);
 
-  ret = lp_load(servicesf,False);
+  ret = lp_load(servicesf,False,False,True);
 
   /* perhaps the config filename is now set */
   if (!test)
@@ -3275,7 +3305,7 @@ BOOL reload_services(BOOL test)
     }
   }
 
-  reset_mangled_stack( lp_mangledstack() );
+  reset_mangled_cache();
 
   /* this forces service parameters to be flushed */
   become_service(-1,True);
@@ -3288,7 +3318,7 @@ BOOL reload_services(BOOL test)
 /****************************************************************************
 this prevents zombie child processes
 ****************************************************************************/
-static int sig_hup()
+static int sig_hup(void)
 {
   BlockSignals(True,SIGHUP);
   DEBUG(0,("Got SIGHUP\n"));
@@ -3300,89 +3330,6 @@ static int sig_hup()
   return(0);
 }
 
-/****************************************************************************
-Setup the groups a user belongs to.
-****************************************************************************/
-int setup_groups(char *user, int uid, int gid, int *p_ngroups, 
-                int **p_igroups, gid_t **p_groups,
-         int **p_attrs)
-{
-  if (-1 == initgroups(user,gid))
-    {
-      if (getuid() == 0)
-       {
-         DEBUG(0,("Unable to initgroups!\n"));
-         if (gid < 0 || gid > 16000 || uid < 0 || uid > 16000)
-           DEBUG(0,("This is probably a problem with the account %s\n",user));
-       }
-    }
-  else
-    {
-      int i,ngroups;
-      int *igroups;
-      int *attrs;
-      gid_t grp = 0;
-      ngroups = getgroups(0,&grp);
-      if (ngroups <= 0)
-        ngroups = 32;
-      igroups = (int *)malloc(sizeof(int)*ngroups);
-      attrs   = (int *)malloc(sizeof(int)*ngroups);
-      for (i=0;i<ngroups;i++)
-      {
-        attrs  [i] = 0x7; /* XXXX don't know what NT user attributes are yet! */
-        igroups[i] = 0x42424242;
-      }
-      ngroups = getgroups(ngroups,(gid_t *)igroups);
-
-      if (igroups[0] == 0x42424242)
-        ngroups = 0;
-
-      *p_ngroups = ngroups;
-      *p_attrs   = attrs;
-
-      /* The following bit of code is very strange. It is due to the
-         fact that some OSes use int* and some use gid_t* for
-         getgroups, and some (like SunOS) use both, one in prototypes,
-         and one in man pages and the actual code. Thus we detect it
-         dynamically using some very ugly code */
-      if (ngroups > 0)
-        {
-         /* does getgroups return ints or gid_t ?? */
-         static BOOL groups_use_ints = True;
-
-         if (groups_use_ints && 
-             ngroups == 1 && 
-             SVAL(igroups,2) == 0x4242)
-           groups_use_ints = False;
-         
-          for (i=0;groups_use_ints && i<ngroups;i++)
-            if (igroups[i] == 0x42424242)
-             groups_use_ints = False;
-             
-          if (groups_use_ints)
-          {
-             *p_igroups = igroups;
-             *p_groups = (gid_t *)igroups;       
-          }
-          else
-          {
-             gid_t *groups = (gid_t *)igroups;
-             igroups = (int *)malloc(sizeof(int)*ngroups);
-             for (i=0;i<ngroups;i++)
-          {
-               igroups[i] = groups[i];
-          }
-             *p_igroups = igroups;
-             *p_groups = (gid_t *)groups;
-           }
-       }
-      DEBUG(3,("%s is in %d groups\n",user,ngroups));
-      for (i=0;i<ngroups;i++)
-        DEBUG(3,("%d ",igroups[i]));
-      DEBUG(3,("\n"));
-    }
-  return 0;
-}
 
 /****************************************************************************
   make a connection to a service
@@ -3395,20 +3342,20 @@ int make_connection(char *service,char *user,char *password, int pwlen, char *de
   connection_struct *pcon;
   BOOL guest = False;
   BOOL force = False;
-  static BOOL first_connection = True;
 
   strlower(service);
 
   snum = find_service(service);
   if (snum < 0)
     {
+      extern int Client;
       if (strequal(service,"IPC$"))
        {         
          DEBUG(3,("%s refusing IPC connection\n",timestring()));
          return(-3);
        }
 
-      DEBUG(0,("%s couldn't find service %s\n",timestring(),service));      
+      DEBUG(0,("%s %s (%s) couldn't find service %s\n",timestring(),remote_machine,client_addr(Client),service));      
       return(-2);
     }
 
@@ -3417,11 +3364,25 @@ int make_connection(char *service,char *user,char *password, int pwlen, char *de
       if (*user && Get_Pwnam(user,True))
        return(make_connection(user,user,password,pwlen,dev,vuid));
 
-      if (validated_username(vuid))
-       {
-         strcpy(user,validated_username(vuid));
-         return(make_connection(user,user,password,pwlen,dev,vuid));
-       }
+      if(lp_security() != SEC_SHARE)
+      {
+        if (validated_username(vuid))
+        {
+          strcpy(user,validated_username(vuid));
+          return(make_connection(user,user,password,pwlen,dev,vuid));
+        }
+      }
+      else
+      {
+        /*
+         * Security = share. Try with sesssetup_user as the username.
+         */
+        if(*sesssetup_user)
+        {
+          strcpy(user,sesssetup_user);
+          return(make_connection(user,user,password,pwlen,dev,vuid));
+        }
+      }
     }
 
   if (!lp_snum_ok(snum) || !check_access(snum)) {    
@@ -3601,9 +3562,7 @@ int make_connection(char *service,char *user,char *password, int pwlen, char *de
        }  
 
       if (lp_status(SNUM(cnum)))
-       claim_connection(cnum,"STATUS.",MAXSTATUS,first_connection);
-
-      first_connection = False;
+       claim_connection(cnum,"STATUS.",MAXSTATUS,False);
     } /* IS_IPC */
 
   pcon->open = True;
@@ -3683,10 +3642,11 @@ int make_connection(char *service,char *user,char *password, int pwlen, char *de
   }
 
   {
+    extern int Client;
     DEBUG(IS_IPC(cnum)?3:1,("%s %s (%s) connect to service %s as user %s (uid=%d,gid=%d) (pid %d)\n",
                            timestring(),
                            remote_machine,
-                           client_addr(),
+                           client_addr(Client),
                            lp_servicename(SNUM(cnum)),user,
                            pcon->uid,
                            pcon->gid,
@@ -3955,7 +3915,7 @@ reply for the nt protocol
 int reply_nt1(char *outbuf)
 {
   /* dual names + lock_and_read + nt SMBs + remote API calls */
-  int capabilities = CAP_NT_FIND|CAP_LOCK_AND_READ;
+  int capabilities = CAP_NT_FIND|CAP_LOCK_AND_READ|CAP_RPC_REMOTE_APIS;
 /*
   other valid capabilities which we may support at some time...
                      CAP_LARGE_FILES|CAP_NT_SMBS|CAP_RPC_REMOTE_APIS;
@@ -4014,7 +3974,7 @@ int reply_nt1(char *outbuf)
   SSVAL(outbuf,smb_vwv1+1,lp_maxmux()); /* maxmpx */
   SSVAL(outbuf,smb_vwv2+1,1); /* num vcs */
   SIVAL(outbuf,smb_vwv3+1,0xffff); /* max buffer. LOTS! */
-  SIVAL(outbuf,smb_vwv5+1,0xffff); /* raw size. LOTS! */
+  SIVAL(outbuf,smb_vwv5+1,0x10000); /* raw size. full 64k */
   SIVAL(outbuf,smb_vwv7+1,getpid()); /* session key */
   SIVAL(outbuf,smb_vwv9+1,capabilities); /* capabilities */
   put_long_date(outbuf+smb_vwv11+1,t);
@@ -4106,7 +4066,7 @@ struct {
 /****************************************************************************
   reply to a negprot
 ****************************************************************************/
-static int reply_negprot(char *inbuf,char *outbuf)
+static int reply_negprot(char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
 {
   int outsize = set_message(outbuf,1,0,True);
   int Index=0;
@@ -4229,6 +4189,7 @@ close a cnum
 ****************************************************************************/
 void close_cnum(int cnum, uint16 vuid)
 {
+  extern int Client;
   DirCacheFlush(SNUM(cnum));
 
   unbecome_user();
@@ -4241,7 +4202,7 @@ void close_cnum(int cnum, uint16 vuid)
 
   DEBUG(IS_IPC(cnum)?3:1,("%s %s (%s) closed connection to service %s\n",
                          timestring(),
-                         remote_machine,client_addr(),
+                         remote_machine,client_addr(Client),
                          lp_servicename(SNUM(cnum))));
 
   yield_connection(cnum,
@@ -4296,183 +4257,6 @@ void close_cnum(int cnum, uint16 vuid)
 }
 
 
-/****************************************************************************
-simple routines to do connection counting
-****************************************************************************/
-BOOL yield_connection(int cnum,char *name,int max_connections)
-{
-  struct connect_record crec;
-  pstring fname;
-  FILE *f;
-  int mypid = getpid();
-  int i;
-
-  DEBUG(3,("Yielding connection to %d %s\n",cnum,name));
-
-  if (max_connections <= 0)
-    return(True);
-
-  bzero(&crec,sizeof(crec));
-
-  pstrcpy(fname,lp_lockdir());
-  standard_sub(cnum,fname);
-  trim_string(fname,"","/");
-
-  strcat(fname,"/");
-  strcat(fname,name);
-  strcat(fname,".LCK");
-
-  f = fopen(fname,"r+");
-  if (!f)
-    {
-      DEBUG(2,("Couldn't open lock file %s (%s)\n",fname,strerror(errno)));
-      return(False);
-    }
-
-  fseek(f,0,SEEK_SET);
-
-  /* find a free spot */
-  for (i=0;i<max_connections;i++)
-    {
-      if (fread(&crec,sizeof(crec),1,f) != 1)
-       {
-         DEBUG(2,("Entry not found in lock file %s\n",fname));
-         fclose(f);
-         return(False);
-       }
-      if (crec.pid == mypid && crec.cnum == cnum)
-       break;
-    }
-
-  if (crec.pid != mypid || crec.cnum != cnum)
-    {
-      fclose(f);
-      DEBUG(2,("Entry not found in lock file %s\n",fname));
-      return(False);
-    }
-
-  bzero((void *)&crec,sizeof(crec));
-  
-  /* remove our mark */
-  if (fseek(f,i*sizeof(crec),SEEK_SET) != 0 ||
-      fwrite(&crec,sizeof(crec),1,f) != 1)
-    {
-      DEBUG(2,("Couldn't update lock file %s (%s)\n",fname,strerror(errno)));
-      fclose(f);
-      return(False);
-    }
-
-  DEBUG(3,("Yield successful\n"));
-
-  fclose(f);
-  return(True);
-}
-
-
-/****************************************************************************
-simple routines to do connection counting
-****************************************************************************/
-BOOL claim_connection(int cnum,char *name,int max_connections,BOOL Clear)
-{
-  struct connect_record crec;
-  pstring fname;
-  FILE *f;
-  int snum = SNUM(cnum);
-  int i,foundi= -1;
-  int total_recs;
-
-  if (max_connections <= 0)
-    return(True);
-
-  DEBUG(5,("trying claim %s %s %d\n",lp_lockdir(),name,max_connections));
-
-  pstrcpy(fname,lp_lockdir());
-  standard_sub(cnum,fname);
-  trim_string(fname,"","/");
-
-  if (!directory_exist(fname,NULL))
-    mkdir(fname,0755);
-
-  strcat(fname,"/");
-  strcat(fname,name);
-  strcat(fname,".LCK");
-
-  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);
-
-  f = fopen(fname,"r+");
-
-  if (!f)
-    {
-      DEBUG(1,("couldn't open lock file %s\n",fname));
-      return(False);
-    }
-
-  /* find a free spot */
-  for (i=0;i<max_connections;i++)
-    {
-
-      if (i>=total_recs || 
-         fseek(f,i*sizeof(crec),SEEK_SET) != 0 ||
-         fread(&crec,sizeof(crec),1,f) != 1)
-       {
-         if (foundi < 0) foundi = i;
-         break;
-       }
-
-      if (Clear && crec.pid && !process_exists(crec.pid))
-       {
-         fseek(f,i*sizeof(crec),SEEK_SET);
-         bzero((void *)&crec,sizeof(crec));
-         fwrite(&crec,sizeof(crec),1,f);
-         if (foundi < 0) foundi = i;
-         continue;
-       }
-      if (foundi < 0 && (!crec.pid || !process_exists(crec.pid)))
-       {
-         foundi=i;
-         if (!Clear) break;
-       }
-    }  
-
-  if (foundi < 0)
-    {
-      DEBUG(3,("no free locks in %s\n",fname));
-      fclose(f);
-      return(False);
-    }      
-
-  /* fill in the crec */
-  bzero((void *)&crec,sizeof(crec));
-  crec.magic = 0x280267;
-  crec.pid = getpid();
-  crec.cnum = cnum;
-  crec.uid = Connections[cnum].uid;
-  crec.gid = Connections[cnum].gid;
-  StrnCpy(crec.name,lp_servicename(snum),sizeof(crec.name)-1);
-  crec.start = time(NULL);
-
-  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 ||
-      fwrite(&crec,sizeof(crec),1,f) != 1)
-    {
-      fclose(f);
-      return(False);
-    }
-
-  fclose(f);
-  return(True);
-}
 
 #if DUMP_CORE
 /*******************************************************************
@@ -4608,7 +4392,7 @@ struct smb_message_struct
 {
   int code;
   char *name;
-  int (*fn)();
+  int (*fn)(char *, char *, int, int);
   int flags;
 #if PROFILING
   unsigned long time;
@@ -4684,7 +4468,7 @@ struct smb_message_struct
    {SMBmove,"SMBmove",NULL,AS_USER | NEED_WRITE | QUEUE_IN_OPLOCK },
    
    {SMBopenX,"SMBopenX",reply_open_and_X,AS_USER | CAN_IPC | QUEUE_IN_OPLOCK },
-   {SMBreadX,"SMBreadX",reply_read_and_X,AS_USER},
+   {SMBreadX,"SMBreadX",reply_read_and_X,AS_USER | CAN_IPC },
    {SMBwriteX,"SMBwriteX",reply_write_and_X,AS_USER},
    {SMBlockingX,"SMBlockingX",reply_lockingX,AS_USER},
    
@@ -4695,7 +4479,7 @@ struct smb_message_struct
    /* LANMAN2.0 PROTOCOL FOLLOWS */
    {SMBfindnclose, "SMBfindnclose", reply_findnclose, AS_USER},
    {SMBfindclose, "SMBfindclose", reply_findclose,AS_USER},
-   {SMBtrans2, "SMBtrans2", reply_trans2, AS_USER | QUEUE_IN_OPLOCK },
+   {SMBtrans2, "SMBtrans2", reply_trans2, AS_USER },
    {SMBtranss2, "SMBtranss2", reply_transs2, AS_USER},
 
    /* messaging routines */
@@ -4927,12 +4711,12 @@ int chain_reply(char *inbuf,char *outbuf,int size,int bufsize)
   CVAL(outbuf2,smb_com) = CVAL(inbuf2,smb_com);
   
   memcpy(outbuf2+4,inbuf2+4,4);
-  CVAL(outbuf2,smb_rcls) = SUCCESS;
+  CVAL(outbuf2,smb_rcls) = SMB_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_err,SMB_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));
@@ -4989,12 +4773,12 @@ int construct_reply(char *inbuf,char *outbuf,int size,int bufsize)
   set_message(outbuf,0,0,True);
   
   memcpy(outbuf+4,inbuf+4,4);
-  CVAL(outbuf,smb_rcls) = SUCCESS;
+  CVAL(outbuf,smb_rcls) = SMB_SUCCESS;
   CVAL(outbuf,smb_reh) = 0;
   CVAL(outbuf,smb_flg) = 0x80 | (CVAL(inbuf,smb_flg) & 0x8); /* bit 7 set 
                                                             means a reply */
   SSVAL(outbuf,smb_flg2,1); /* say we support long filenames */
-  SSVAL(outbuf,smb_err,SUCCESS);
+  SSVAL(outbuf,smb_err,SMB_SUCCESS);
   SSVAL(outbuf,smb_tid,SVAL(inbuf,smb_tid));
   SSVAL(outbuf,smb_pid,SVAL(inbuf,smb_pid));
   SSVAL(outbuf,smb_uid,SVAL(inbuf,smb_uid));
@@ -5172,7 +4956,6 @@ static void init_structs(void )
     {
       Files[i].open = False;
       string_init(&Files[i].name,"");
-
     }
 
   for (i=0;i<MAX_OPEN_FILES;i++)
@@ -5190,10 +4973,8 @@ static void init_structs(void )
   /* for RPC pipes */
   init_rpc_pipe_hnd();
 
-#ifdef NTDOMAIN
   /* for LSA handles */
   init_lsa_policy_hnd();
-#endif
 
   init_dptrs();
 }
@@ -5229,9 +5010,6 @@ static void usage(char *pname)
   int port = SMB_PORT;
   int opt;
   extern char *optarg;
-  char pidFile[100];
-
-  *pidFile = '\0';
 
 #ifdef NEED_AUTH_PARAMETERS
   set_auth_parameters(argc,argv);
@@ -5264,7 +5042,7 @@ static void usage(char *pname)
   seteuid(0);
 #endif
 
-  fault_setup(exit_server);
+  fault_setup((void (*)(void *))exit_server);
   signal(SIGTERM , SIGNAL_CAST dflt_sig);
 
   /* we want total control over the permissions on created files,
@@ -5285,9 +5063,6 @@ static void usage(char *pname)
   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;
@@ -5413,33 +5188,9 @@ static void usage(char *pname)
          mkdir(lp_lockdir(), 0755);
   }
 
-  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 (is_daemon) {
+         pidfile_create("smbd");
+  }
 
   if (!open_sockets(is_daemon,port))
     exit(1);