avoid-long-windows-shell-timeouts-20040105
[openafs.git] / src / WINNT / afsd / smb.c
index b2a95af..0be9cff 100644 (file)
@@ -57,15 +57,15 @@ long ongoingOps = 0;
 
 unsigned int sessionGen = 0;
 
-void afsi_log();
+extern void afsi_log(char *pattern, ...);
 
 osi_hyper_t hzero = {0, 0};
 osi_hyper_t hones = {0xFFFFFFFF, -1};
 
-osi_log_t *smb_logp;
+osi_log_t *  smb_logp;
 osi_rwlock_t smb_globalLock;
 osi_rwlock_t smb_rctLock;
-osi_rwlock_t smb_ListenerLock;
+osi_mutex_t  smb_ListenerLock;
  
 char smb_LANadapter;
 unsigned char smb_sharename[NCBNAMSZ+1] = {0};
@@ -157,8 +157,8 @@ smb_waitingLock_t *smb_allWaitingLocks;
 void smb_DispatchPacket(smb_vc_t *vcp, smb_packet_t *inp, smb_packet_t *outp,
        NCB *ncbp, raw_write_cont_t *rwcp);
 void smb_NetbiosInit();
-extern char cm_HostName[];
 #ifdef DJGPP
+extern char cm_HostName[];
 extern char cm_confDir[];
 #endif
 
@@ -780,7 +780,7 @@ smb_user_t *smb_FindUID(smb_vc_t *vcp, unsigned short uid, int flags)
        for(uidp = vcp->usersp; uidp; uidp = uidp->nextp) {
                if (uid == uidp->userID) {
                        uidp->refCount++;
-                       osi_LogEvent("AFS smb_FindUID (Find by UID)",NULL," VCP[%x] found-uid[%d] name[%s]",vcp,uidp->userID,(uidp->unp) ? uidp->unp->name : "");
+                       osi_LogEvent("AFS smb_FindUID (Find by UID)",NULL," VCP[%x] found-uid[%d] name[%s]",(int)vcp,uidp->userID,(uidp->unp) ? uidp->unp->name : "");
                break;
                }
         }
@@ -793,7 +793,7 @@ smb_user_t *smb_FindUID(smb_vc_t *vcp, unsigned short uid, int flags)
                 vcp->usersp = uidp;
                 lock_InitializeMutex(&uidp->mx, "uid_t mutex");
                 uidp->userID = uid;
-                               osi_LogEvent("AFS smb_FindUID (Find by UID)",NULL,"VCP[%x] new-uid[%d] name[%s]",vcp,uidp->userID,(uidp->unp ? uidp->unp->name : ""));
+                               osi_LogEvent("AFS smb_FindUID (Find by UID)",NULL,"VCP[%x] new-uid[%d] name[%s]",(int)vcp,uidp->userID,(uidp->unp ? uidp->unp->name : ""));
         }
         lock_ReleaseWrite(&smb_rctLock);
         return uidp;
@@ -834,7 +834,7 @@ smb_user_t *smb_FindUserByNameThisSession(smb_vc_t *vcp, char *usern)
             continue;
           if (stricmp(uidp->unp->name, usern) == 0) {
             uidp->refCount++;
-                       osi_LogEvent("AFS smb_FindUserByNameThisSession",NULL,"VCP[%x] uid[%d] match-name[%s]",vcp,uidp->userID,usern);
+                       osi_LogEvent("AFS smb_FindUserByNameThisSession",NULL,"VCP[%x] uid[%d] match-name[%s]",(int)vcp,uidp->userID,usern);
             break;
           } else
             continue;
@@ -1057,7 +1057,7 @@ int smb_ListShares()
         fprintf(stderr, "The following shares are available:\n");
         fprintf(stderr, "Share Name (AFS Path)\n");
         fprintf(stderr, "---------------------\n");
-        fprintf(stderr, "\\\\%s\\%-16s (/afs)\n", smb_localNamep, "ALL");
+        fprintf(stderr, "\\\\%s\\%-16s (%s)\n", smb_localNamep, "ALL", cm_mountRoot);
 
 #ifndef DJGPP
        code = GetWindowsDirectory(sbmtpath, sizeof(sbmtpath));
@@ -1084,7 +1084,7 @@ int smb_ListShares()
                                         sbmtpath);
           if (!len) return num_shares;
           p = pathName;
-          if (strncmp(p, "/afs", 4) != 0)
+          if (strncmp(p, cm_mountRoot, 4) != 0)
             print_afs = 1;
           while (*p) {
             if (*p == '\\') *p = '/';    /* change to / */
@@ -1092,12 +1092,12 @@ int smb_ListShares()
           }
 
           fprintf(stderr, "\\\\%s\\%-16s (%s%s)\n",
-                  smb_localNamep, this_share, (print_afs ? "/afs" : "\0"),
+                  smb_localNamep, this_share, (print_afs ? cm_mountRoot : "\0"),
                   pathName);
           num_shares++;
-          while (*this_share != NULL) this_share++;  /* find next NULL */
-          this_share++;   /* skip past the NULL */
-        } while (*this_share != NULL);  /* stop at final NULL */
+          while (*this_share != 0) this_share++;  /* find next NUL */
+          this_share++;   /* skip past the NUL */
+        } while (*this_share != 0);  /* stop at final NUL */
 
         return num_shares;
 }
@@ -1141,8 +1141,8 @@ int smb_FindShare(smb_vc_t *vcp, smb_packet_t *inp, char *shareName,
         /* We can accept either unix or PC style AFS pathnames.  Convert
            Unix-style to PC style here for internal use. */
         p = pathName;
-        if (strncmp(p, "/afs", 4) == 0)
-          p += 4;  /* skip /afs */
+        if (strncmp(p, cm_mountRoot, 4) == 0)
+          p += strlen(cm_mountRoot);  /* skip mount path */
         q = p;
         while (*q) {
           if (*q == '/') *q = '\\';    /* change to \ */
@@ -1372,13 +1372,13 @@ static smb_packet_t *GetPacket(void)
        tbp = smb_packetFreeListp;
         if (tbp) smb_packetFreeListp = tbp->nextp;
        lock_ReleaseWrite(&smb_globalLock);
-        if (!tbp) {
+    if (!tbp) {
 #ifndef DJGPP
-               tbp = GlobalAlloc(GMEM_FIXED, 65540);
+        tbp = calloc(65540,1);
 #else /* DJGPP */
-                tbp = malloc(sizeof(smb_packet_t));
+        tbp = malloc(sizeof(smb_packet_t));
 #endif /* !DJGPP */
-                tbp->magic = SMB_PACKETMAGIC;
+        tbp->magic = SMB_PACKETMAGIC;
                tbp->ncbp = NULL;
                tbp->vcp = NULL;
                tbp->resumeCode = 0;
@@ -1391,28 +1391,28 @@ static smb_packet_t *GetPacket(void)
                tbp->flags = 0;
         
 #ifdef DJGPP
-                npar = SMB_PACKETSIZE >> 4;  /* number of paragraphs */
-                {
-                  signed int retval =
-                    __dpmi_allocate_dos_memory(npar, &tb_sel); /* DOS segment */
-                  if (retval == -1) {
-                    afsi_log("Cannot allocate %d paragraphs of DOS memory",
-                             npar);
-                    osi_panic("",__FILE__,__LINE__);
-                  }
-                  else {
-                    afsi_log("Allocated %d paragraphs of DOS mem at 0x%X",
-                             npar, retval);
-                    seg = retval;
-                  }
-                }
-                tbp->dos_pkt = (seg * 16) + 0;  /* DOS physical address */
-                tbp->dos_pkt_sel = tb_sel;
+        npar = SMB_PACKETSIZE >> 4;  /* number of paragraphs */
+        {
+            signed int retval =
+                __dpmi_allocate_dos_memory(npar, &tb_sel); /* DOS segment */
+            if (retval == -1) {
+                afsi_log("Cannot allocate %d paragraphs of DOS memory",
+                          npar);
+                osi_panic("",__FILE__,__LINE__);
+            }
+            else {
+                afsi_log("Allocated %d paragraphs of DOS mem at 0x%X",
+                          npar, retval);
+                seg = retval;
+            }
+        }
+        tbp->dos_pkt = (seg * 16) + 0;  /* DOS physical address */
+        tbp->dos_pkt_sel = tb_sel;
 #endif /* DJGPP */
        }
-        osi_assert(tbp->magic == SMB_PACKETMAGIC);
+    osi_assert(tbp->magic == SMB_PACKETMAGIC);
 
-        return tbp;
+    return tbp;
 }
 
 smb_packet_t *smb_CopyPacket(smb_packet_t *pkt)
@@ -1420,63 +1420,63 @@ smb_packet_t *smb_CopyPacket(smb_packet_t *pkt)
        smb_packet_t *tbp;
        tbp = GetPacket();
        memcpy(tbp, pkt, sizeof(smb_packet_t));
-       tbp->wctp = tbp->data + ((unsigned int)pkt->wctp -
-                                 (unsigned int)pkt->data);
+       tbp->wctp = tbp->data + ((unsigned int)pkt->wctp - (unsigned int)pkt->data);
        return tbp;
 }
 
 static NCB *GetNCB(void)
 {
        smb_ncb_t *tbp;
-        NCB *ncbp;
+    NCB *ncbp;
 #ifdef DJGPP
-        unsigned int npar, seg, tb_sel;
+    unsigned int npar, seg, tb_sel;
 #endif /* DJGPP */
 
        lock_ObtainWrite(&smb_globalLock);
        tbp = smb_ncbFreeListp;
-        if (tbp) smb_ncbFreeListp = tbp->nextp;
+    if (tbp) 
+        smb_ncbFreeListp = tbp->nextp;
        lock_ReleaseWrite(&smb_globalLock);
-        if (!tbp) {
+    if (!tbp) {
 #ifndef DJGPP
-               tbp = GlobalAlloc(GMEM_FIXED, sizeof(*tbp));
+        tbp = calloc(sizeof(*tbp),1);
 #else /* DJGPP */
-                tbp = malloc(sizeof(*tbp));
-                npar = (sizeof(NCB)+15) >> 4;  /* number of paragraphs */
-                {
-                  signed int retval =
-                    __dpmi_allocate_dos_memory(npar, &tb_sel); /* DOS segment */
-                  if (retval == -1) {
-                    afsi_log("Cannot allocate %d paragraphs of DOS mem in GetNCB",
-                             npar);
-                    osi_panic("",__FILE__,__LINE__);
-                  } else {
-                    afsi_log("Allocated %d paragraphs of DOS mem at 0x%X in GetNCB",
-                             npar, retval);
-                    seg = retval;
-                  }
-                }
-                tbp->dos_ncb = (seg * 16) + 0;  /* DOS physical address */
-                tbp->dos_ncb_sel = tb_sel;
+        tbp = malloc(sizeof(*tbp));
+        npar = (sizeof(NCB)+15) >> 4;  /* number of paragraphs */
+        {
+            signed int retval =
+                __dpmi_allocate_dos_memory(npar, &tb_sel); /* DOS segment */
+            if (retval == -1) {
+                afsi_log("Cannot allocate %d paragraphs of DOS mem in GetNCB",
+                          npar);
+                osi_panic("",__FILE__,__LINE__);
+            } else {
+                afsi_log("Allocated %d paragraphs of DOS mem at 0x%X in GetNCB",
+                          npar, retval);
+                seg = retval;
+            }
+        }
+        tbp->dos_ncb = (seg * 16) + 0;  /* DOS physical address */
+        tbp->dos_ncb_sel = tb_sel;
 #endif /* !DJGPP */
-                tbp->magic = SMB_NCBMAGIC;
+        tbp->magic = SMB_NCBMAGIC;
        }
         
-        osi_assert(tbp->magic == SMB_NCBMAGIC);
+    osi_assert(tbp->magic == SMB_NCBMAGIC);
 
        memset(&tbp->ncb, 0, sizeof(NCB));
-        ncbp = &tbp->ncb;
+    ncbp = &tbp->ncb;
 #ifdef DJGPP
-        dos_memset(tbp->dos_ncb, 0, sizeof(NCB));
+    dos_memset(tbp->dos_ncb, 0, sizeof(NCB));
 #endif /* DJGPP */
-        return ncbp;
+    return ncbp;
 }
 
 void smb_FreePacket(smb_packet_t *tbp)
 {
-        osi_assert(tbp->magic == SMB_PACKETMAGIC);
+    osi_assert(tbp->magic == SMB_PACKETMAGIC);
         
-        lock_ObtainWrite(&smb_globalLock);
+    lock_ObtainWrite(&smb_globalLock);
        tbp->nextp = smb_packetFreeListp;
        smb_packetFreeListp = tbp;
        tbp->magic = SMB_PACKETMAGIC;
@@ -1490,39 +1490,39 @@ void smb_FreePacket(smb_packet_t *tbp)
        tbp->oddByte = 0;
        tbp->ncb_length = 0;
        tbp->flags = 0;
-        lock_ReleaseWrite(&smb_globalLock);
+    lock_ReleaseWrite(&smb_globalLock);
 }
 
 static void FreeNCB(NCB *bufferp)
 {
        smb_ncb_t *tbp;
         
-        tbp = (smb_ncb_t *) bufferp;
-        osi_assert(tbp->magic == SMB_NCBMAGIC);
+    tbp = (smb_ncb_t *) bufferp;
+    osi_assert(tbp->magic == SMB_NCBMAGIC);
         
-        lock_ObtainWrite(&smb_globalLock);
+    lock_ObtainWrite(&smb_globalLock);
        tbp->nextp = smb_ncbFreeListp;
        smb_ncbFreeListp = tbp;
-        lock_ReleaseWrite(&smb_globalLock);
+    lock_ReleaseWrite(&smb_globalLock);
 }
 
 /* get a ptr to the data part of a packet, and its count */
 unsigned char *smb_GetSMBData(smb_packet_t *smbp, int *nbytesp)
 {
-        int parmBytes;
-        int dataBytes;
-        unsigned char *afterParmsp;
+    int parmBytes;
+    int dataBytes;
+    unsigned char *afterParmsp;
 
-        parmBytes = *smbp->wctp << 1;
+    parmBytes = *smbp->wctp << 1;
        afterParmsp = smbp->wctp + parmBytes + 1;
         
-        dataBytes = afterParmsp[0] + (afterParmsp[1]<<8);
-        if (nbytesp) *nbytesp = dataBytes;
+    dataBytes = afterParmsp[0] + (afterParmsp[1]<<8);
+    if (nbytesp) *nbytesp = dataBytes;
         
        /* don't forget to skip the data byte count, since it follows
-         * the parameters; that's where the "2" comes from below.
-         */
-        return (unsigned char *) (afterParmsp + 2);
+     * the parameters; that's where the "2" comes from below.
+     */
+    return (unsigned char *) (afterParmsp + 2);
 }
 
 /* must set all the returned parameters before playing around with the
@@ -1811,7 +1811,7 @@ void smb_MapNTError(long code, unsigned long *NTStatusp)
                NTStatus = 0xC000000FL; /* No such file */
         }
         else if (code == CM_ERROR_TIMEDOUT) {
-               NTStatus = 0xC00000CFL; /* Paused */
+            NTStatus = 0xC00000CFL;    /* Sharing Paused */
         }
         else if (code == CM_ERROR_RETRY) {
                NTStatus = 0xC000022DL; /* Retry */
@@ -1862,7 +1862,11 @@ void smb_MapNTError(long code, unsigned long *NTStatusp)
                NTStatus = 0xC00000CCL; /* Bad network name */
        }
        else if (code == CM_ERROR_NOIPC) {
-               NTStatus = 0xC00000CCL; /* Bad network name */
+#ifdef COMMENT
+               NTStatus = 0xC0000022L; /* Access Denied */
+#else
+        NTStatus = 0xC000013DL; /* Remote Resources */
+#endif
        }
        else if (code == CM_ERROR_CLOCKSKEW) {
                NTStatus = 0xC0000133L; /* Time difference at DC */
@@ -1986,8 +1990,8 @@ void smb_MapCoreError(long code, smb_vc_t *vcp, unsigned short *scodep,
                error = 6;
        }
        else if (code == CM_ERROR_NOIPC) {
-               class = 1;
-               error = 66;
+               class = 2;
+               error = 4; /* bad access */
        }
        else if (code == CM_ERROR_CLOCKSKEW) {
                class = 1;      /* invalid function */
@@ -3831,6 +3835,7 @@ long smb_ReceiveCoreRename(smb_vc_t *vcp, smb_packet_t *inp, smb_packet_t *outp)
         smb_renameRock_t rock;
         cm_scache_t *oldDscp;
         cm_scache_t *newDscp;
+       cm_scache_t *tmpscp;
         char *oldLastNamep;
         char *newLastNamep;
         osi_hyper_t thyper;
@@ -3914,6 +3919,14 @@ long smb_ReceiveCoreRename(smb_vc_t *vcp, smb_packet_t *inp, smb_packet_t *outp)
          */
        thyper.LowPart = 0;             /* search dir from here */
         thyper.HighPart = 0;
+       /* search for file to already exhist, if so return error*/
+
+       code = cm_Lookup(newDscp,newLastNamep,CM_FLAG_CHECKPATH,userp,&req,&tmpscp);
+       if((code != CM_ERROR_NOSUCHFILE) && (code != CM_ERROR_NOSUCHPATH) && (code != CM_ERROR_NOSUCHVOLUME) ) {
+           cm_ReleaseSCache(tmpscp);
+           return CM_ERROR_EXISTS; /* file exist, do not rename, also 
+                                      fixes move*/
+       }
         code = cm_ApplyDir(oldDscp, smb_RenameProc, &rock, &thyper, userp, &req, NULL);
 
         if (code == CM_ERROR_STOPNOW)
@@ -5262,20 +5275,20 @@ long smb_ReceiveCoreSeek(smb_vc_t *vcp, smb_packet_t *inp, smb_packet_t *outp)
 void smb_DispatchPacket(smb_vc_t *vcp, smb_packet_t *inp, smb_packet_t *outp,
        NCB *ncbp, raw_write_cont_t *rwcp)
 {
-        static showErrors = 1;
-        smb_dispatch_t *dp;
-        smb_t *smbp;
-        unsigned long code;
-        unsigned char *outWctp;
-        int nparms;                    /* # of bytes of parameters */
-        char tbuffer[200];
-        int nbytes;                    /* bytes of data, excluding count */
-        int temp;
-        unsigned char *tp;
-        unsigned short errCode;
+    static showErrors = 0;
+    smb_dispatch_t *dp;
+    smb_t *smbp;
+    unsigned long code;
+    unsigned char *outWctp;
+    int nparms;                        /* # of bytes of parameters */
+    char tbuffer[200];
+    int nbytes;                        /* bytes of data, excluding count */
+    int temp;
+    unsigned char *tp;
+    unsigned short errCode;
        unsigned long NTStatus;
-        int noSend;
-        unsigned char errClass;
+    int noSend;
+    unsigned char errClass;
        unsigned int oldGen;
        DWORD oldTime, newTime;
 
@@ -5369,10 +5382,10 @@ void smb_DispatchPacket(smb_vc_t *vcp, smb_packet_t *inp, smb_packet_t *outp,
                                code = smb_ReceiveCoreWriteRaw (vcp, inp, outp,
                                                                rwcp);
                        else {
-                                       osi_LogEvent("AFS Dispatch %s",(myCrt_Dispatch(inp->inCom)),"vcp[%x] lana[%d] lsn[%d]",vcp,vcp->lana,vcp->lsn);
+                                       osi_LogEvent("AFS Dispatch %s",(myCrt_Dispatch(inp->inCom)),"vcp[%x] lana[%d] lsn[%d]",(int)vcp,vcp->lana,vcp->lsn);
                                        osi_Log4(afsd_logp,"Dispatch %s vcp[%x] lana[%d] lsn[%d]",(myCrt_Dispatch(inp->inCom)),vcp,vcp->lana,vcp->lsn);
                                        code = (*(dp->procp)) (vcp, inp, outp);
-                                       osi_LogEvent("AFS Dispatch return",NULL,"Code[%d]",(code==0)?0:code-CM_ERROR_BASE,"");
+                                       osi_LogEvent("AFS Dispatch return",NULL,"Code[%d]",(code==0)?0:code-CM_ERROR_BASE);
                                        osi_Log1(afsd_logp,"Dispatch return  code[%d]",(code==0)?0:code-CM_ERROR_BASE);
                                }
 
@@ -5872,17 +5885,17 @@ void InitNCBslot(int idx)
 void smb_Listener(void *parmp)
 {
        NCB *ncbp;
-        long code;
-        long len;
+    long code;
+    long len;
        long i, j;
-        smb_vc_t *vcp;
+    smb_vc_t *vcp;
        int flags = 0;
        char rname[NCBNAMSZ+1];
        char cname[MAX_COMPUTERNAME_LENGTH+1];
        int cnamelen = MAX_COMPUTERNAME_LENGTH+1;
 #ifdef DJGPP
-        dos_ptr dos_ncb;
-        time_t now;
+    dos_ptr dos_ncb;
+    time_t now;
 #endif /* DJGPP */
        int lana = (int) parmp;
 
@@ -5893,6 +5906,7 @@ void smb_Listener(void *parmp)
 
        while (1) {
                memset(ncbp, 0, sizeof(NCB));
+               flags = 0;
 #ifdef DJGPP
              /* terminate if shutdown flag is set */
              if (smbShutdownFlag == 1)
@@ -6049,6 +6063,28 @@ void smb_NetbiosInit()
     int delname_tried=0;
     int len;
     int lana_found = 0;
+    OSVERSIONINFO Version;
+
+    /*******************************************************************/
+    /*      ms loopback adapter scan                                   */
+    /*******************************************************************/
+    struct
+    {
+       ADAPTER_STATUS status;
+       NAME_BUFFER    NameBuff [30];
+    }       Adapter;
+    
+    int j;
+    BOOL wla_found;
+
+    /*      AFAIK, this is the default for the ms loopback adapter.*/
+    unsigned char kWLA_MAC[6] = { 0x02, 0x00, 0x4c, 0x4f, 0x4f, 0x50 };
+    /*******************************************************************/
+
+    /* Get the version of Windows */
+    memset(&Version, 0x00, sizeof(Version));
+    Version.dwOSVersionInfoSize = sizeof(Version);
+    GetVersionEx(&Version);
 
     /* setup the NCB system */
     ncbp = GetNCB();
@@ -6082,39 +6118,54 @@ void smb_NetbiosInit()
         ncbp->ncb_callname[2] = 100;
         ncbp->ncb_lana_num = lana_list.lana[i];
         code = Netbios(ncbp);
-        if (code == 0) code = ncbp->ncb_retcode;
+        if (code == 0) 
+            code = ncbp->ncb_retcode;
         if (code != 0) {
-           sprintf(s, "Netbios NCBRESET lana %d error code %d", lana_list.lana[i], code);
-           afsi_log(s);
-           lana_list.lana[i] = 255;  /* invalid lana */
+            sprintf(s, "Netbios NCBRESET lana %d error code %d", lana_list.lana[i], code);
+            afsi_log(s);
+            lana_list.lana[i] = 255;  /* invalid lana */
         } else {
             sprintf(s, "Netbios NCBRESET lana %d succeeded", lana_list.lana[i]);
             afsi_log(s);
-           /* check to see if this is the "Microsoft Loopback Adapter"        */
-           memset( ncbp, 0, sizeof (*ncbp) );
-           ncbp->ncb_command = NCBASTAT;
-           ncbp->ncb_lana_num = lana_list.lana[i];
-           strcpy( ncbp->ncb_callname,  "*               " );
-           ncbp->ncb_buffer = (char *) &Adapter;
-           ncbp->ncb_length = sizeof(Adapter);
-           code = Netbios( ncbp );
+            memset( ncbp, 0, sizeof (*ncbp) );
+            ncbp->ncb_command = NCBASTAT;
+            ncbp->ncb_lana_num = lana_list.lana[i];
+            strcpy( ncbp->ncb_callname,  "*               " );
+            ncbp->ncb_buffer = (char *) &Adapter;
+            ncbp->ncb_length = sizeof(Adapter);
+            code = Netbios( ncbp );
            
-           if ( code == 0 ) {
-               wla_found = TRUE;
-               for (j=0; wla_found && (j<6); j++)
-                   wla_found = ( Adapter.status.adapter_address[j] == kWLA_MAC[j] );
-               
-               if ( wla_found ) {
-                   sprintf(s, "Windows Loopback Adapter detected lana %d", lana_list.lana[i]);
-                   afsi_log(s);
-                   
-                   /* select this lana; no need to continue */
-                   lana_list.length = 1;
-                   lana_list.lana[0] = lana_list.lana[i];
-                   break;
-               }
-           }
-       }
+            if ( code == 0 ) {
+                wla_found = TRUE;
+                for (j=0; wla_found && (j<6); j++)
+                    wla_found = ( Adapter.status.adapter_address[j] == kWLA_MAC[j] );
+
+                if ( wla_found ) {
+                    /*
+                     * check to see if this is the Microsoft Loopback Adapter"
+                     * if we are running on Windows XP or higher
+                     */
+                    if ( Version.dwPlatformId == VER_PLATFORM_WIN32_NT &&
+                         ( Version.dwMajorVersion > 5 ||
+                           Version.dwMajorVersion == 5 &&
+                           Version.dwMinorVersion >= 1 )
+                 )  
+                    {
+                        sprintf(s, "Windows Loopback Adapter detected lana %d", lana_list.lana[i]);
+                        afsi_log(s);
+
+                        /* select this lana; no need to continue */
+                        lana_list.length = 1;
+                        lana_list.lana[0] = lana_list.lana[i];
+                        break;
+                    } else {
+                        sprintf(s, "Windows Loopback Adapter disabled lana %d", lana_list.lana[i]);
+                        afsi_log(s);
+                        lana_list.lana[i] = 255; /* invalid lana */
+                    }
+                }
+            }
+        }
     }
 #else
     /* for DJGPP, there is no NCBENUM and NCBRESET is a real reset.  so
@@ -6130,7 +6181,6 @@ void smb_NetbiosInit()
     }
 #endif /* !DJGPP */
 
- try_addname:
     /* and declare our name so we can receive connections */
     memset(ncbp, 0, sizeof(*ncbp));
     len=lstrlen(smb_localNamep);
@@ -6142,6 +6192,9 @@ void smb_NetbiosInit()
     len = strlen(smb_localNamep);
     for(i=len; i<NCBNAMSZ; i++) ncbp->ncb_name[i] = ' ';
 #endif
+    sprintf(s, "lana_list.length %d", lana_list.length);
+    afsi_log(s);
+
     /* Keep the name so we can unregister it later */
     for (l = 0; l < lana_list.length; l++) {
         lana = lana_list.lana[l];
@@ -6166,7 +6219,7 @@ void smb_NetbiosInit()
 
         if (code == 0) code = ncbp->ncb_retcode;
         if (code == 0) {
-            fprintf(stderr, "Netbios NCBADDNAME succeeded on lana %d\n", lana);
+            afsi_log("Netbios NCBADDNAME succeeded on lana %d", lana);
 #ifdef DJGPP
             /* we only use one LANA with djgpp */
             lana_list.lana[0] = lana;
@@ -6176,13 +6229,12 @@ void smb_NetbiosInit()
         else {
             sprintf(s, "Netbios NCBADDNAME lana %d error code %d", lana, code);
             afsi_log(s);
-            fprintf(stderr, "Netbios NCBADDNAME lana %d error code %d\n", lana, code);
             if (code == NRC_BRIDGE) {    /* invalid LANA num */
                 lana_list.lana[l] = 255;
                 continue;
             }
             else if (code == NRC_DUPNAME) {
-                /* Name already exists; try to delete it */
+                afsi_log("Name already exists; try to delete it");
                 memset(ncbp, 0, sizeof(*ncbp));
                 ncbp->ncb_command = NCBDELNAME;
                 memcpy(ncbp->ncb_name,smb_sharename,NCBNAMSZ);
@@ -6193,9 +6245,10 @@ void smb_NetbiosInit()
                 code = Netbios(ncbp, dos_ncb);
 #endif /* DJGPP */
                 if (code == 0) code = ncbp->ncb_retcode;
-                else
-                    fprintf(stderr, "Netbios NCBDELNAME lana %d error code %d\n", lana, code);
-                fflush(stderr);
+                else {
+                    sprintf(s, "Netbios NCBDELNAME lana %d error code %d\n", lana, code);
+                    afsi_log(s);
+                }
                 if (code != 0 || delname_tried) {
                     lana_list.lana[l] = 255;
                 }
@@ -6241,16 +6294,13 @@ void smb_Init(osi_log_t *logp, char *snamep, int useV3, int LANadapt,
 
 {
        thread_t phandle;
-        int lpid;
-        int i;
-        long code;
-        int len;
-        NCB *ncbp;
+    int lpid;
+    int i;
+    int len;
        struct tm myTime;
-       char s[100];
 #ifdef DJGPP
-        int npar, seg, sel;
-        dos_ptr rawBuf;
+    int npar, seg, sel;
+    dos_ptr rawBuf;
 #endif /* DJGPP */
 
 #ifndef DJGPP
@@ -6310,10 +6360,10 @@ void smb_Init(osi_log_t *logp, char *snamep, int useV3, int LANadapt,
        
        /* 4 Raw I/O buffers */
 #ifndef DJGPP
-       smb_RawBufs = GlobalAlloc(GMEM_FIXED, 65536);
+       smb_RawBufs = calloc(65536,1);
        *((char **)smb_RawBufs) = NULL;
        for (i=0; i<3; i++) {
-               char *rawBuf = GlobalAlloc(GMEM_FIXED, 65536);
+               char *rawBuf = calloc(65536,1);
                *((char **)rawBuf) = smb_RawBufs;
                smb_RawBufs = rawBuf;
        }
@@ -6560,3 +6610,40 @@ void smb_Shutdown(void)
 #endif
 }
 #endif /* DJGPP */
+
+int smb_DumpVCP(FILE *outputFile, char *cookie)
+{
+    int zilch;
+    char output[1024];
+    int i;
+  
+    smb_vc_t *vcp;
+  
+    lock_ObtainRead(&smb_rctLock);
+  
+    for(vcp = smb_allVCsp; vcp; vcp=vcp->nextp) 
+    {
+        smb_fid_t *fidp;
+      
+        sprintf(output, "%s vcp=0x%08X, refCount=%d, flags=%d, vcID=%d, lsn=%d, uidCounter=%d, tidCounter=%d, fidCounter=%d\n",
+                 cookie, vcp, vcp->refCount, vcp->flags, vcp->vcID, vcp->lsn, vcp->uidCounter, vcp->tidCounter, vcp->fidCounter);
+        WriteFile(outputFile, output, strlen(output), &zilch, NULL);
+      
+        for(fidp = vcp->fidsp; fidp; fidp = (smb_fid_t *) osi_QNext(&fidp->q)) 
+        {
+            sprintf(output, "%s -- fidp=0x%08X, refCount=%d, fid=%d, vcp=0x%08X, scp=0x%08X, ioctlp=0x%08X, NTopen_pathp=%s, NTopen_wholepathp=%s\n", 
+                     cookie, fidp, fidp->refCount, fidp->fid, fidp->vcp, fidp->scp, fidp->ioctlp, 
+                     fidp->NTopen_pathp ? fidp->NTopen_pathp : "NULL", 
+                     fidp->NTopen_wholepathp ? fidp->NTopen_wholepathp : "NULL");
+            WriteFile(outputFile, output, strlen(output), &zilch, NULL);
+        }
+      
+        sprintf(output, "done dumping fidsp\n");
+        WriteFile(outputFile, output, strlen(output), &zilch, NULL);
+    }       
+  
+    lock_ReleaseRead(&smb_rctLock);
+    return 0;
+}
+
+