windows-pioctl-20041204
[openafs.git] / src / sys / pioctl_nt.c
index 17a64b3..aa93298 100644 (file)
@@ -22,6 +22,8 @@ RCSID
 #include <string.h>
 #include <winioctl.h>
 #include <winsock2.h>
+#define SECURITY_WIN32
+#include <security.h>
 #include <nb30.h>
 
 #include <osi.h>
@@ -129,6 +131,15 @@ GetIoctlHandle(char *fileNamep, HANDLE * handlep)
     char netbiosName[MAX_NB_NAME_LENGTH];
     char tbuffer[256]="";
     HANDLE fh;
+    HKEY hk;
+    char szUser[128] = "";
+    char szClient[MAX_PATH] = "";
+    char szPath[MAX_PATH] = "";
+    NETRESOURCE nr;
+    DWORD res;
+    DWORD ioctlDebug = IoctlDebug();
+    DWORD gle;
+    DWORD dwSize = sizeof(szUser);
 
     if (fileNamep) {
         drivep = strchr(fileNamep, ':');
@@ -188,15 +199,6 @@ GetIoctlHandle(char *fileNamep, HANDLE * handlep)
                    FILE_FLAG_WRITE_THROUGH, NULL);
     fflush(stdout);
     if (fh == INVALID_HANDLE_VALUE) {
-        HKEY hk;
-        char szUser[64] = "";
-        char szClient[MAX_PATH] = "";
-        char szPath[MAX_PATH] = "";
-        NETRESOURCE nr;
-        DWORD res;
-        DWORD ioctlDebug = IoctlDebug();
-        DWORD gle;
-
         gle = GetLastError();
         if (gle && ioctlDebug ) {
             char buf[4096];
@@ -214,60 +216,127 @@ GetIoctlHandle(char *fileNamep, HANDLE * handlep)
                         tbuffer,gle,buf);
             }
         }
+#ifdef COMMENT
         if (gle != ERROR_DOWNGRADE_DETECTED)
             return -1;                                   
+#endif
 
         lana_GetNetbiosName(szClient, LANA_NETBIOS_NAME_FULL);
-        sprintf(szPath, "\\\\%s", szClient);
 
-        /* We should probably be using GetUserNameEx() for this */
         if (RegOpenKey (HKEY_CURRENT_USER, 
-                        TEXT("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer"), &hk) == 0)
+                         TEXT("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer"), &hk) == 0)
         {
-            DWORD dwSize = sizeof(szUser);
             DWORD dwType = REG_SZ;
             RegQueryValueEx (hk, TEXT("Logon User Name"), NULL, &dwType, (PBYTE)szUser, &dwSize);
             RegCloseKey (hk);
         }
-        if ( ioctlDebug )
-            fprintf(stderr, "pioctl logon user: [%s]\r\n",szUser);
-
-        memset (&nr, 0x00, sizeof(NETRESOURCE));
-        nr.dwType=RESOURCETYPE_DISK;
-        nr.lpLocalName=0;
-        nr.lpRemoteName=szPath;
-        res = WNetAddConnection2(&nr,NULL,szUser,0);
-        if (res) {
-            if ( ioctlDebug ) {
-                fprintf(stderr, "pioctl WNetAddConnection2(%s,%s) failed: 0x%X\r\n",
-                         szPath,szUser,res);
+
+        if ( szUser[0] ) {
+            if ( ioctlDebug )
+                fprintf(stderr, "pioctl logon user: [%s]\r\n",szUser);
+
+            sprintf(szPath, "\\\\%s", szClient);
+            memset (&nr, 0x00, sizeof(NETRESOURCE));
+            nr.dwType=RESOURCETYPE_DISK;
+            nr.lpLocalName=0;
+            nr.lpRemoteName=szPath;
+            res = WNetAddConnection2(&nr,NULL,szUser,0);
+            if (res) {
+                if ( ioctlDebug ) {
+                    fprintf(stderr, "pioctl WNetAddConnection2(%s,%s) failed: 0x%X\r\n",
+                             szPath,szUser,res);
+                }
+
+                sprintf(szPath, "\\\\%s\\all", szClient);
+                res = WNetAddConnection2(&nr,NULL,szUser,0);
+                if (res) {
+                    if ( ioctlDebug ) {
+                        fprintf(stderr, "pioctl WNetAddConnection2(%s,%s) failed: 0x%X\r\n",
+                                 szPath,szUser,res);
+                    }
+                    goto next_attempt;
+                }
+            }
+
+            fh = CreateFile(tbuffer, GENERIC_READ | GENERIC_WRITE,
+                             FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
+                             FILE_FLAG_WRITE_THROUGH, NULL);
+            fflush(stdout);
+            if (fh == INVALID_HANDLE_VALUE) {
+                gle = GetLastError();
+                if (gle && ioctlDebug ) {
+                    char buf[4096];
+
+                    if ( FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,
+                                        NULL,
+                                        gle,
+                                        MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US),
+                                        buf,
+                                        4096,
+                                        (va_list *) NULL
+                                        ) )
+                    {
+                        fprintf(stderr,"pioctl CreateFile(%s) failed: 0x%8X\r\n\t[%s]\r\n",
+                                 tbuffer,gle,buf);
+                    }
+                }
             }
-            return -1;
         }
+    }
+
+  next_attempt:
+    if ( fh == INVALID_HANDLE_VALUE ) {
+        if (GetUserNameEx(NameSamCompatible, szUser, &dwSize)) {
+            if ( ioctlDebug )
+                fprintf(stderr, "pioctl logon user: [%s]\r\n",szUser);
+
+            sprintf(szPath, "\\\\%s", szClient);
+            memset (&nr, 0x00, sizeof(NETRESOURCE));
+            nr.dwType=RESOURCETYPE_DISK;
+            nr.lpLocalName=0;
+            nr.lpRemoteName=szPath;
+            res = WNetAddConnection2(&nr,NULL,szUser,0);
+            if (res) {
+                if ( ioctlDebug ) {
+                    fprintf(stderr, "pioctl WNetAddConnection2(%s,%s) failed: 0x%X\r\n",
+                             szPath,szUser,res);
+                }
+
+                sprintf(szPath, "\\\\%s\\all", szClient);
+                res = WNetAddConnection2(&nr,NULL,szUser,0);
+                if (res) {
+                    if ( ioctlDebug ) {
+                        fprintf(stderr, "pioctl WNetAddConnection2(%s,%s) failed: 0x%X\r\n",
+                                 szPath,szUser,res);
+                    }
+                    return -1;
+                }
+            }
 
-        fh = CreateFile(tbuffer, GENERIC_READ | GENERIC_WRITE,
-                         FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
-                         FILE_FLAG_WRITE_THROUGH, NULL);
-        fflush(stdout);
-        if (fh == INVALID_HANDLE_VALUE) {
-            gle = GetLastError();
-            if (gle && ioctlDebug ) {
-                char buf[4096];
-
-                if ( FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,
-                                    NULL,
-                                    gle,
-                                    MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US),
-                                    buf,
-                                    4096,
-                                    (va_list *) NULL
-                                    ) )
-                {
-                    fprintf(stderr,"pioctl CreateFile(%s) failed: 0x%8X\r\n\t[%s]\r\n",
-                             tbuffer,gle,buf);
+            fh = CreateFile(tbuffer, GENERIC_READ | GENERIC_WRITE,
+                             FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
+                             FILE_FLAG_WRITE_THROUGH, NULL);
+            fflush(stdout);
+            if (fh == INVALID_HANDLE_VALUE) {
+                gle = GetLastError();
+                if (gle && ioctlDebug ) {
+                    char buf[4096];
+
+                    if ( FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,
+                                        NULL,
+                                        gle,
+                                        MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US),
+                                        buf,
+                                        4096,
+                                        (va_list *) NULL
+                                        ) )
+                    {
+                        fprintf(stderr,"pioctl CreateFile(%s) failed: 0x%8X\r\n\t[%s]\r\n",
+                                 tbuffer,gle,buf);
+                    }
                 }
+                return -1;
             }
-            return -1;
         }
     }