windows-misc-20040803
[openafs.git] / src / WINNT / client_config / drivemap.cpp
index 240cfdd..350004b 100644 (file)
@@ -21,17 +21,13 @@ extern "C" {
 #include "drivemap.h"
 #include <time.h>
 #include <adssts.h>
+#ifdef DEBUG
 #define DEBUG_VERBOSE
+#endif
 #include <osilog.h>
+#include <lanahelper.h>
 
 extern void Config_GetLanAdapter (ULONG *pnLanAdapter);
-void GetNetbiosName(LPTSTR pszName, int type);
-extern BOOL Config_ReadNum (LPCTSTR pszLHS, DWORD *pdwRHS);
-extern BOOL Config_ReadString (LPCTSTR pszLHS, LPTSTR pszRHS, size_t cchMax);
-extern void GetUncServerName(int lanaNumber, BOOL isGateway, TCHAR* name, int type);
-
-#define NETBIOS_NAME_FULL 0
-#define NETBIOS_NAME_SUFFIX 1
 
 /*
  * REGISTRY ___________________________________________________________________
@@ -49,15 +45,82 @@ const TCHAR sAFSConfigKeyName[] = TEXT("SYSTEM\\CurrentControlSet\\Services\\Tra
 
 #define cREALLOC_SUBMOUNTS   4
 
-static TCHAR cszINIFILE[] = TEXT("afsdsbmt.ini");
-static TCHAR cszSECTION_SUBMOUNTS[] = TEXT("AFS Submounts");
-static TCHAR cszSECTION_MAPPINGS[] = TEXT("AFS Mappings");
-static TCHAR cszSECTION_ACTIVE[] = TEXT("AFS Active");
+static TCHAR cszSECTION_SUBMOUNTS[] = TEXT("SOFTWARE\\OpenAFS\\Client\\Submounts");
+static TCHAR cszSECTION_MAPPINGS[] = TEXT("SOFTWARE\\OpenAFS\\Client\\Mappings");
+static TCHAR cszSECTION_ACTIVE[] = TEXT("SOFTWARE\\OpenAFS\\Client\\Active Maps");
 
 static TCHAR cszAUTOSUBMOUNT[] = TEXT("Auto");
 static TCHAR cszLANMANDEVICE[] = TEXT("\\Device\\LanmanRedirector\\");
 
 
+static BOOL 
+WriteRegistryString(HKEY key, TCHAR * subkey, LPTSTR lhs, LPTSTR rhs)
+{
+    HKEY hkSub = NULL;
+    RegCreateKeyEx( key,
+                    subkey,
+                    0,
+                    NULL,
+                    REG_OPTION_NON_VOLATILE,
+                    KEY_WRITE,
+                    NULL,
+                    &hkSub,
+                    NULL);
+
+    DWORD status = RegSetValueEx( hkSub, lhs, 0, REG_SZ, (const BYTE *)rhs, strlen(rhs)+1 );
+
+    if ( hkSub )
+        RegCloseKey( hkSub );
+
+    return (status == ERROR_SUCCESS);
+}
+
+static BOOL 
+ReadRegistryString(HKEY key, TCHAR * subkey, LPTSTR lhs, LPTSTR rhs, DWORD * size)
+{
+    HKEY hkSub = NULL;
+    RegCreateKeyEx( key,
+                    subkey,
+                    0,
+                    NULL,
+                    REG_OPTION_NON_VOLATILE,
+                    KEY_READ,
+                    NULL,
+                    &hkSub,
+                    NULL);
+
+    DWORD dwType;
+    DWORD status = RegQueryValueEx( hkSub, lhs, 0, &dwType, (LPBYTE)rhs, size );
+
+    if ( hkSub )
+        RegCloseKey( hkSub );
+
+    return (status == ERROR_SUCCESS);
+}
+
+static BOOL 
+DeleteRegistryString(HKEY key, TCHAR * subkey, LPTSTR lhs)
+{
+    HKEY hkSub = NULL;
+    RegCreateKeyEx( key,
+                    subkey,
+                    0,
+                    NULL,
+                    REG_OPTION_NON_VOLATILE,
+                    KEY_READ,
+                    NULL,
+                    &hkSub,
+                    NULL);
+
+    DWORD dwType;
+    DWORD status = RegDeleteValue( hkSub, lhs );
+
+    if ( hkSub )
+        RegCloseKey( hkSub );
+
+    return (status == ERROR_SUCCESS);
+}
+
 /*
  * STRINGS ____________________________________________________________________
  *
@@ -161,7 +224,8 @@ static BOOL IsWindowsNT (void)
    return fIsWinNT;
 }
 
-
+/* Check if the OS is Windows 2000 or higher.
+*/
 BOOL IsWindows2000 (void)
 {
    static BOOL fChecked = FALSE;
@@ -193,8 +257,12 @@ BOOL IsWindows2000 (void)
 
 void GetClientNetbiosName (LPTSTR pszName)
 {
-    *pszName = TEXT('\0');
-    GetNetbiosName(pszName, NETBIOS_NAME_FULL);
+    static TCHAR szNetbiosName[32] = "";
+
+    if ( szNetbiosName[0] == 0 ) {
+        lana_GetNetbiosName(szNetbiosName, LANA_NETBIOS_NAME_FULL);
+    }
+    _tcscpy(pszName, szNetbiosName);
 }
 
 
@@ -238,14 +306,18 @@ BOOL IsValidSubmountName (LPTSTR pszSubmount)
       return FALSE;
 
    for ( ; *pszSubmount; ++pszSubmount)
-      {
-      if (!isprint(*pszSubmount))
-         return FALSE;
-      if (*pszSubmount == TEXT(' '))
-         return FALSE;
-      if (*pszSubmount == TEXT('\t'))
-         return FALSE;
-      }
+   {
+       if (!isprint(*pszSubmount))
+           return FALSE;
+       if (*pszSubmount == TEXT(' '))
+           return FALSE;
+       if (*pszSubmount == TEXT('/'))
+           return FALSE;
+       if (*pszSubmount == TEXT('\\'))
+           return FALSE;
+       if (*pszSubmount == TEXT('\t'))
+           return FALSE;
+   }
 
    return TRUE;
 }
@@ -308,92 +380,128 @@ BOOL fCanIssuePIOCTL (void)
 
 void QueryDriveMapList_ReadSubmounts (PDRIVEMAPLIST pList)
 {
-   if (IsWindowsNT())
-      {
-      size_t cchLHS = 1024;
-      LPTSTR mszLHS = AllocateStringMemory (cchLHS);
-
-      for (int iRetry = 0; iRetry < 5; ++iRetry)
-         {
-         DWORD rc = GetPrivateProfileString (cszSECTION_SUBMOUNTS, NULL, TEXT(""), mszLHS, cchLHS, cszINIFILE);
-         if ((rc != cchLHS-1) && (rc != cchLHS-2))
-            break;
-
-         FreeStringMemory (mszLHS);
-         cchLHS *= 2;
-         mszLHS = AllocateStringMemory (cchLHS);
-         }
-
-      for (LPTSTR psz = mszLHS; psz && *psz; psz += 1+lstrlen(psz))
-         {
-         SUBMOUNT Submount;
-         memset (&Submount, 0x00, sizeof(SUBMOUNT));
-         lstrcpy (Submount.szSubmount, psz);
-
-         TCHAR szMapping[ MAX_PATH ] = TEXT("");
-         GetPrivateProfileString (cszSECTION_SUBMOUNTS, Submount.szSubmount, TEXT(""), szMapping, MAX_PATH, cszINIFILE);
-         if (szMapping[0] != TEXT('\0'))
-            {
-            AdjustAfsPath (Submount.szMapping, szMapping, FALSE, TRUE);
-
-            for (size_t ii = 0; ii < pList->cSubmounts; ++ii)
-               {
-               if (!pList->aSubmounts[ii].szSubmount[0])
-                  break;
-               }
-            if (REALLOC (pList->aSubmounts, pList->cSubmounts, 1+ii, cREALLOC_SUBMOUNTS))
-               {
-               memcpy (&pList->aSubmounts[ii], &Submount, sizeof(SUBMOUNT));
-               }
+    if (IsWindowsNT())
+    {
+        HKEY hkSubmounts;
+
+        RegCreateKeyEx( HKEY_LOCAL_MACHINE, 
+                        "SOFTWARE\\OpenAFS\\Client\\Submounts",
+                        0, 
+                        "AFS", 
+                        REG_OPTION_NON_VOLATILE,
+                        KEY_READ|KEY_QUERY_VALUE,
+                        NULL, 
+                        &hkSubmounts,
+                        NULL );
+
+        DWORD dwSubmounts;
+        RegQueryInfoKey( hkSubmounts,
+                         NULL,  /* lpClass */
+                         NULL,  /* lpcClass */
+                         NULL,  /* lpReserved */
+                         NULL,  /* lpcSubKeys */
+                         NULL,  /* lpcMaxSubKeyLen */
+                         NULL,  /* lpcMaxClassLen */
+                         &dwSubmounts, /* lpcValues */
+                         NULL,  /* lpcMaxValueNameLen */
+                         NULL,  /* lpcMaxValueLen */
+                         NULL,  /* lpcbSecurityDescriptor */
+                         NULL   /* lpftLastWriteTime */
+                         );
+
+        for ( DWORD dwIndex = 0; dwIndex < dwSubmounts; dwIndex ++ ) {
+            TCHAR submountPath[MAX_PATH] = "";
+            DWORD submountPathLen = MAX_PATH;
+            TCHAR submountName[MAX_PATH];
+            DWORD submountNameLen = MAX_PATH;
+            DWORD dwType;
+
+            RegEnumValue( hkSubmounts, dwIndex, submountName, &submountNameLen, NULL,
+                          &dwType, (LPBYTE)submountPath, &submountPathLen);
+
+            SUBMOUNT Submount;
+            memset (&Submount, 0x00, sizeof(SUBMOUNT));
+            lstrcpy (Submount.szSubmount, submountName);
+
+            if ( submountPath[0] != TEXT('\0') ) {
+                AdjustAfsPath (Submount.szMapping, submountPath, FALSE, TRUE);
+
+                for (size_t ii = 0; ii < pList->cSubmounts; ++ii)
+                {
+                    if (!pList->aSubmounts[ii].szSubmount[0])
+                        break;
+                }
+                if (REALLOC (pList->aSubmounts, pList->cSubmounts, 1+ii, cREALLOC_SUBMOUNTS))
+                {
+                    memcpy (&pList->aSubmounts[ii], &Submount, sizeof(SUBMOUNT));
+                }
             }
-         }
 
-      FreeStringMemory (mszLHS);
-      }
+        }
+        RegCloseKey(hkSubmounts);
+    }
 }
 
 
 void QueryDriveMapList_ReadMappings (PDRIVEMAPLIST pList)
 {
-   size_t cchLHS = 1024;
-   LPTSTR mszLHS = AllocateStringMemory (cchLHS);
-
-   for (int iRetry = 0; iRetry < 5; ++iRetry)
-      {
-      DWORD rc = GetPrivateProfileString (cszSECTION_MAPPINGS, NULL, TEXT(""), mszLHS, cchLHS, cszINIFILE);
-      if ((rc != cchLHS-1) && (rc != cchLHS-2))
-         break;
-
-      FreeStringMemory (mszLHS);
-      cchLHS *= 2;
-      mszLHS = AllocateStringMemory (cchLHS);
-      }
-
-   for (LPTSTR psz = mszLHS; psz && *psz; psz += 1+lstrlen(psz))
-      {
-      DRIVEMAP DriveMap;
-      memset (&DriveMap, 0x00, sizeof(DRIVEMAP));
-      DriveMap.chDrive = toupper(*psz);
-      DriveMap.fPersistent = TRUE;
-      if ((DriveMap.chDrive < chDRIVE_A) || (DriveMap.chDrive > chDRIVE_Z))
-         continue;
-
-      TCHAR szMapping[ MAX_PATH ] = TEXT("");
-      GetPrivateProfileString (cszSECTION_MAPPINGS, psz, TEXT(""), szMapping, MAX_PATH, cszINIFILE);
-      if (szMapping[0] != TEXT('\0'))
-         {
-         AdjustAfsPath (DriveMap.szMapping, szMapping, TRUE, TRUE);
-         if (DriveMap.szMapping[ lstrlen(DriveMap.szMapping)-1 ] == TEXT('*'))
-            {
-            DriveMap.fPersistent = FALSE;
-            DriveMap.szMapping[ lstrlen(DriveMap.szMapping)-1 ] = TEXT('\0');
-            }
-         size_t iDrive = DriveMap.chDrive - chDRIVE_A;
-         memcpy (&pList->aDriveMap[ iDrive ], &DriveMap, sizeof(DRIVEMAP));
-         }
-      }
+    HKEY hkMappings;
+    RegCreateKeyEx( HKEY_CURRENT_USER,
+                    "SOFTWARE\\OpenAFS\\Client\\Mappings",
+                    0, 
+                    "AFS", 
+                    REG_OPTION_NON_VOLATILE,
+                    KEY_READ|KEY_QUERY_VALUE,
+                    NULL, 
+                    &hkMappings,
+                    NULL );
+
+    DWORD dwMappings;
+    RegQueryInfoKey( hkMappings,
+                     NULL,  /* lpClass */
+                     NULL,  /* lpcClass */
+                     NULL,  /* lpReserved */
+                     NULL,  /* lpcSubKeys */
+                     NULL,  /* lpcMaxSubKeyLen */
+                     NULL,  /* lpcMaxClassLen */
+                     &dwMappings, /* lpcValues */
+                     NULL,  /* lpcMaxValueNameLen */
+                     NULL,  /* lpcMaxValueLen */
+                     NULL,  /* lpcbSecurityDescriptor */
+                     NULL   /* lpftLastWriteTime */
+                     );
+
+    for ( DWORD dwIndex = 0; dwIndex < dwMappings; dwIndex ++ ) {
+        TCHAR mapping[MAX_PATH] = "";
+        DWORD mappingLen = MAX_PATH;
+        TCHAR drive[MAX_PATH];
+        DWORD driveLen = MAX_PATH;
+        DWORD dwType;
+
+        RegEnumValue( hkMappings, dwIndex, drive, &driveLen, NULL,
+                      &dwType, (LPBYTE)mapping, &mappingLen);
+
+       DRIVEMAP DriveMap;
+       memset (&DriveMap, 0x00, sizeof(DRIVEMAP));
+       DriveMap.chDrive = toupper(*drive);
+       DriveMap.fPersistent = TRUE;
+       if ((DriveMap.chDrive < chDRIVE_A) || (DriveMap.chDrive > chDRIVE_Z))
+           continue;
+
+       if (mapping[0] != TEXT('\0'))
+       {
+           AdjustAfsPath (DriveMap.szMapping, mapping, TRUE, TRUE);
+           if (DriveMap.szMapping[ lstrlen(DriveMap.szMapping)-1 ] == TEXT('*'))
+           {
+               DriveMap.fPersistent = FALSE;
+               DriveMap.szMapping[ lstrlen(DriveMap.szMapping)-1 ] = TEXT('\0');
+           }
+           size_t iDrive = DriveMap.chDrive - chDRIVE_A;
+           memcpy (&pList->aDriveMap[ iDrive ], &DriveMap, sizeof(DRIVEMAP));
+       }
+    }
 
-   FreeStringMemory (mszLHS);
+    RegCloseKey(hkMappings);
 }
 
 BOOL ForceMapActive (TCHAR chDrive)
@@ -404,7 +512,8 @@ BOOL ForceMapActive (TCHAR chDrive)
     szDrive[0] = chDrive;
     szDrive[1] = 0;
 
-    GetPrivateProfileString (cszSECTION_ACTIVE, szDrive, TEXT("0"), szActive, sizeof(szActive), cszINIFILE);
+    DWORD len = sizeof(szActive);
+    ReadRegistryString( HKEY_CURRENT_USER, cszSECTION_ACTIVE, szDrive, szActive, &len);
 
     if ( !lstrcmp(szActive,"1") || !lstrcmpi(szActive,"true") || !lstrcmpi(szActive,"on") || !lstrcmpi(szActive,"yes") )
         return TRUE;
@@ -419,34 +528,69 @@ void WriteActiveMap (TCHAR chDrive, BOOL on)
     szDrive[0] = chDrive;
     szDrive[1] = 0;
 
-    WritePrivateProfileString (cszSECTION_ACTIVE, szDrive, on ? "1" : "0", cszINIFILE);
+    WriteRegistryString(HKEY_CURRENT_USER, cszSECTION_ACTIVE, szDrive, on ? "1" : "0");
 }
 
 void QueryDriveMapList_WriteMappings (PDRIVEMAPLIST pList)
 {
-   WriteDriveMappings (pList);
+    WriteDriveMappings (pList);
 }
 
 
 void WriteDriveMappings (PDRIVEMAPLIST pList)
 {
-   WritePrivateProfileString (cszSECTION_MAPPINGS, NULL, NULL, cszINIFILE);
+    HKEY hkMappings;
+    RegCreateKeyEx( HKEY_CURRENT_USER, 
+                    "SOFTWARE\\OpenAFS\\Client\\Mappings",
+                    0, 
+                    "AFS", 
+                    REG_OPTION_NON_VOLATILE,
+                    KEY_READ|KEY_QUERY_VALUE|KEY_WRITE,
+                    NULL, 
+                    &hkMappings,
+                    NULL );
+
+    DWORD dwMappings;
+    RegQueryInfoKey( hkMappings,
+                     NULL,  /* lpClass */
+                     NULL,  /* lpcClass */
+                     NULL,  /* lpReserved */
+                     NULL,  /* lpcSubKeys */
+                     NULL,  /* lpcMaxSubKeyLen */
+                     NULL,  /* lpcMaxClassLen */
+                     &dwMappings, /* lpcValues */
+                     NULL,  /* lpcMaxValueNameLen */
+                     NULL,  /* lpcMaxValueLen */
+                     NULL,  /* lpcbSecurityDescriptor */
+                     NULL   /* lpftLastWriteTime */
+                     );
+
+    if ( dwMappings > 0 ) {
+        for ( long dwIndex = dwMappings - 1; dwIndex >= 0; dwIndex-- ) {
+            TCHAR drive[MAX_PATH];
+            DWORD driveLen = MAX_PATH;
+
+            RegEnumValue( hkMappings, dwIndex, drive, &driveLen, NULL, NULL, NULL, NULL);
+            RegDeleteValue( hkMappings, drive );
+        }
+    }
 
    for (size_t iDrive = 0; iDrive < 26; ++iDrive)
-      {
-      if (pList->aDriveMap[iDrive].szMapping[0] != TEXT('\0'))
-         {
-         TCHAR szLHS[] = TEXT("*");
-         szLHS[0] = pList->aDriveMap[iDrive].chDrive;
-
-         TCHAR szRHS[MAX_PATH];
-         AdjustAfsPath (szRHS, pList->aDriveMap[iDrive].szMapping, TRUE, TRUE);
-         if (!pList->aDriveMap[iDrive].fPersistent)
-            lstrcat (szRHS, TEXT("*"));
-
-         WritePrivateProfileString (cszSECTION_MAPPINGS, szLHS, szRHS, cszINIFILE);
-         }
-      }
+   {
+       if (pList->aDriveMap[iDrive].szMapping[0] != TEXT('\0'))
+       {
+           TCHAR szLHS[] = TEXT("*");
+           szLHS[0] = pList->aDriveMap[iDrive].chDrive;
+
+           TCHAR szRHS[MAX_PATH];
+           AdjustAfsPath (szRHS, pList->aDriveMap[iDrive].szMapping, TRUE, TRUE);
+           if (!pList->aDriveMap[iDrive].fPersistent)
+               lstrcat (szRHS, TEXT("*"));
+
+           RegSetValueEx( hkMappings, szLHS, 0, REG_SZ, (const BYTE *)szRHS, lstrlen(szRHS) + 1);
+       }
+   }
+   RegCloseKey( hkMappings );
 }
 
 BOOL DriveIsGlobalAfsDrive(TCHAR chDrive)
@@ -625,17 +769,7 @@ BOOL ActivateDriveMap (TCHAR chDrive, LPTSTR pszMapping, LPTSTR pszSubmountReq,
       }
 
    // We now have a submount name and drive letter--map the network drive.
-   //
-   TCHAR szClient[ MAX_PATH ];
-   GetClientNetbiosName (szClient);
-
-   TCHAR szLocal[ MAX_PATH ] = TEXT("*:");
-   szLocal[0] = chDrive;
-
-   TCHAR szRemote[ MAX_PATH ];
-   wsprintf (szRemote, TEXT("\\\\%s\\%s"), szClient, szSubmount);
-
-   DWORD rc=MountDOSDrive(chDrive,szSubmount,fPersistent);
+   DWORD rc=MountDOSDrive(chDrive,szSubmount,fPersistent,NULL);
    if (rc == NO_ERROR)
       return TRUE;
 
@@ -647,29 +781,30 @@ BOOL ActivateDriveMap (TCHAR chDrive, LPTSTR pszMapping, LPTSTR pszSubmountReq,
 
 BOOL InactivateDriveMap (TCHAR chDrive, DWORD *pdwStatus)
 {
-   DWORD rc = DisMountDOSDrive(chDrive, FALSE);
-   if (rc == NO_ERROR)
-      return TRUE;
+    DWORD rc = DisMountDOSDrive(chDrive, FALSE);
+    if (rc == NO_ERROR)
+        return TRUE;
 
-   if (pdwStatus)
-      *pdwStatus = rc;
-   return FALSE;
+    if (pdwStatus)
+        *pdwStatus = rc;
+    return FALSE;
 }
 
 
 void AddSubMount (LPTSTR pszSubmount, LPTSTR pszMapping)
 {
-   TCHAR szRHS[ MAX_PATH ];
-   AdjustAfsPath (szRHS, pszMapping, FALSE, TRUE);
-   if (!szRHS[0])
-      lstrcpy (szRHS, TEXT("/"));
-   WritePrivateProfileString (cszSECTION_SUBMOUNTS, pszSubmount, szRHS, cszINIFILE);
+    TCHAR szRHS[ MAX_PATH ];
+    AdjustAfsPath (szRHS, pszMapping, FALSE, TRUE);
+    if (!szRHS[0])
+        lstrcpy (szRHS, TEXT("/"));
+
+    WriteRegistryString(HKEY_LOCAL_MACHINE, cszSECTION_SUBMOUNTS, pszSubmount, szRHS);
 }
 
 
 void RemoveSubMount (LPTSTR pszSubmount)
 {
-   WritePrivateProfileString (cszSECTION_SUBMOUNTS, pszSubmount, NULL, cszINIFILE);
+    DeleteRegistryString(HKEY_LOCAL_MACHINE, cszSECTION_SUBMOUNTS, pszSubmount);
 }
 
 
@@ -711,38 +846,48 @@ void AdjustAfsPath (LPTSTR pszTarget, LPCTSTR pszSource, BOOL fWantAFS, BOOL fWa
       }
 }
 
-
 BOOL GetDriveSubmount (TCHAR chDrive, LPTSTR pszSubmountNow)
 {
-    TCHAR szDrive[] = TEXT("*:");
+       BOOL isWinNT = IsWindowsNT();
+
+       TCHAR szDrive[] = TEXT("*:");
     szDrive[0] = chDrive;
 
-    TCHAR szMapping[ MAX_PATH ] = TEXT("");
-    LPTSTR pszSubmount = szMapping;
-    TCHAR szNetBiosName[32];
+    TCHAR szMapping[ _MAX_PATH ] = TEXT("");
+
+    if (isWinNT && !QueryDosDevice (szDrive, szMapping, MAX_PATH))
+           return FALSE;
 
+    LPTSTR pszSubmount = szMapping;
+    
+       TCHAR szNetBiosName[32];
     memset(szNetBiosName, '\0', sizeof(szNetBiosName));
-    GetNetbiosName(szNetBiosName, NETBIOS_NAME_SUFFIX);
+    GetClientNetbiosName(szNetBiosName);
     _tcscat(szNetBiosName, TEXT("\\"));
 
-   if (IsWindowsNT())
-      {
-      QueryDosDevice (szDrive, szMapping, MAX_PATH);
-
+   if (isWinNT)
+   {
       // Now if this is an AFS network drive mapping, {szMapping} will be:
       //
-      //   \Device\LanmanRedirector\Q:\machine-afs\submount
+      //   \Device\LanmanRedirector\<Drive>:\<netbiosname>\submount
       //
       // on Windows NT. On Windows 2000, it will be:
       //
-      //   \Device\LanmanRedirector\;Q:0\machine-afs\submount
+      //   \Device\LanmanRedirector\;<Drive>:0\<netbiosname>\submount
       //
       // (This is presumably to support multiple drive mappings with
       // Terminal Server).
       //
-      if (lstrncmpi (szMapping, cszLANMANDEVICE, lstrlen(cszLANMANDEVICE)))
+      // on Windows XP and 2003, it will be :
+      //   \Device\LanmanRedirector\;<Drive>:<AuthID>\<netbiosname>\submount
+      //
+      //   where : <Drive> : DOS drive letter
+      //           <AuthID>: Authentication ID, 16 char hex.
+      //           <netbiosname>: Netbios name of server
+      //
+      if (_tcsnicmp(szMapping, cszLANMANDEVICE, _tcslen(cszLANMANDEVICE)))
          return FALSE;
-      pszSubmount = &szMapping[ lstrlen(cszLANMANDEVICE) ];
+      pszSubmount = &szMapping[ _tcslen(cszLANMANDEVICE) ];
 
       if (IsWindows2000())
          {
@@ -757,9 +902,12 @@ BOOL GetDriveSubmount (TCHAR chDrive, LPTSTR pszSubmountNow)
       if (*(++pszSubmount) != TEXT(':'))
          return FALSE;
 
+#ifdef COMMENT
+       // No longer a safe assumption on XP
       if (IsWindows2000())
           if (*(++pszSubmount) != TEXT('0'))
              return FALSE;
+#endif
 
       // scan for next "\"
       while (*(++pszSubmount) != TEXT('\\'))
@@ -767,12 +915,15 @@ BOOL GetDriveSubmount (TCHAR chDrive, LPTSTR pszSubmountNow)
          if (*pszSubmount==0)
               return FALSE;
       }
+
+       // note that szNetBiosName has a '\\' tagged in the end earlier
       for (++pszSubmount; *pszSubmount && (*pszSubmount != TEXT('\\')); ++pszSubmount)
-         if (!lstrncmpi (pszSubmount, szNetBiosName, lstrlen(szNetBiosName)))
+         if (!_tcsncicmp(pszSubmount, szNetBiosName, _tcslen(szNetBiosName)))
             break;
       if ((!*pszSubmount) || (*pszSubmount == TEXT('\\')))
          return FALSE;
-      pszSubmount += lstrlen(szNetBiosName);
+
+       pszSubmount += _tcslen(szNetBiosName);
       }
    else // (!IsWindowsNT())
       {
@@ -821,7 +972,7 @@ DWORD RWLogonOption(BOOL read,DWORD value)
    if (read)
    {
           rval=0;
-               if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, AFSLogonOptionName,0, KEY_QUERY_VALUE, &hk)==ERROR_SUCCESS)
+               if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, AFSLogonOptionName, 0, KEY_QUERY_VALUE, &hk)==ERROR_SUCCESS)
                {
                        LSPsize=sizeof(rval);
                        RegQueryValueEx(hk, "LogonOptions", NULL,
@@ -921,7 +1072,7 @@ void DoUnMapShare(BOOL drivemap)   //disconnect drivemap
        CHAR *pSubmount="";
 
     memset(szMachine, '\0', sizeof(szMachine));
-    GetNetbiosName(szMachine, NETBIOS_NAME_FULL);
+    GetClientNetbiosName(szMachine);
 
    // Initialize the data structure
        if ((res=WNetOpenEnum(RESOURCE_CONNECTED,RESOURCETYPE_DISK,RESOURCEUSAGE_CONNECTABLE,lpnr,&hEnum))!=NO_ERROR)
@@ -939,11 +1090,14 @@ void DoUnMapShare(BOOL drivemap) //disconnect drivemap
                                {
                                        if ((lpnrLocal[i].lpLocalName) && (strlen(lpnrLocal[i].lpLocalName)>0))
                                        {
-                                               if (drivemap)
+                                               if (drivemap) {
                                                    DisMountDOSDrive(*lpnrLocal[i].lpLocalName);
-                                       } else
+                            DEBUG_EVENT1("AFS DriveUnMap","UnMap-Local=%x",res);
+                        }
+                                       } else {
                                            DisMountDOSDriveFull(lpnrLocal[i].lpRemoteName);
-                                       DEBUG_EVENT1("AFS DriveUnMap","UnMap-Remote=%x",res);
+                        DEBUG_EVENT1("AFS DriveUnMap","UnMap-Remote=%x",res);
+                    }
                                }
                        }
                }
@@ -964,7 +1118,7 @@ BOOL DoMapShareChange()
     DWORD cbBuffer=16384;
 
     memset(szMachine, '\0', sizeof(szMachine));
-    GetNetbiosName(szMachine, NETBIOS_NAME_FULL);
+    GetClientNetbiosName(szMachine);
 
     // Initialize the data structure
        if (!IsServiceActive())
@@ -1052,7 +1206,7 @@ BOOL DoMapShare()
 
        TCHAR szMachine[ MAX_PATH],szPath[MAX_PATH];
     memset(szMachine, '\0', sizeof(szMachine));
-    GetNetbiosName(szMachine, NETBIOS_NAME_FULL);
+    GetClientNetbiosName(szMachine);
     sprintf(szPath,"\\\\%s\\all",szMachine);
 
     // Lets connect all submounts that weren't connectd
@@ -1150,7 +1304,7 @@ BOOL GlobalMountDrive()
                break;
            }
        }
-       dwResult=MountDOSDrive(*szDriveToMapTo,(const char *)szSubMount,FALSE);
+       dwResult=MountDOSDrive(*szDriveToMapTo,(const char *)szSubMount,FALSE,NULL);
     }
     RegCloseKey(hKey);
     return TRUE;
@@ -1169,7 +1323,7 @@ DWORD MountDOSDrive(char chDrive,const char *szSubmount,BOOL bPersistent,const c
     nr.dwType=RESOURCETYPE_DISK;
     nr.lpLocalName=szDrive;
     nr.lpRemoteName=szPath;
-    nr.dwDisplayType = RESOURCEDISPLAYTYPE_SHARE;
+    nr.dwDisplayType = RESOURCEDISPLAYTYPE_SHARE; /* ignored parameter */
     DWORD res=WNetAddConnection2(&nr,NULL,pUsername,(bPersistent)?CONNECT_UPDATE_PROFILE:0);
     DEBUG_EVENT5("AFS DriveMap","Mount %s Local[%s] Remote[%s] User[%s]=%x",
                   (bPersistent)?"Persistant" : "NonPresistant",
@@ -1201,38 +1355,3 @@ DWORD DisMountDOSDrive(const char chDrive,BOOL bForce)
     sprintf(szPath,"%c:",chDrive);
     return DisMountDOSDriveFull(szPath,bForce);
 }
-
-void GetNetbiosName(LPTSTR pszName, int type)
-{
-    DWORD LanAdapter;
-    DWORD dwIsGateWay = 0;
-    TCHAR name[MAX_PATH];
-    TCHAR *Ptr;
-
-    memset(name, '\0', sizeof(name));
-    if (!IsWindowsNT())
-    {
-        if (type == NETBIOS_NAME_SUFFIX)
-        {
-            _tcscpy(pszName, TEXT("-afs"));
-            return;
-        }
-        Config_ReadString (TEXT("Gateway"), pszName, MAX_PATH);
-        if (strlen(pszName) == 0)
-        {
-            strcpy(pszName, TEXT("unknown"));
-            return;
-        }
-        _tcscat(pszName, TEXT("-afs"));
-        return;
-    }
-    if (!Config_ReadNum (TEXT("IsGateway"), &dwIsGateWay))
-        dwIsGateWay = 0;
-    memset(name, 0, sizeof(name));
-    if (!Config_ReadNum (TEXT("LANadapter"), &LanAdapter))
-        LanAdapter = -1;
-    GetUncServerName(LanAdapter, dwIsGateWay, name, type);
-    _tcscpy(pszName, name);
-    return;
-}
-