Windows: pioctl use GetFileAttributes
authorJeffrey Altman <jaltman@your-file-system.com>
Sat, 14 Apr 2012 13:43:48 +0000 (09:43 -0400)
committerJeffrey Altman <jaltman@secure-endpoints.com>
Sun, 15 Apr 2012 13:56:57 +0000 (06:56 -0700)
Instead of attempting to CreateFile(OPEN_EXISTING) the _._AFS_IOCTL_._
file, try using GetFileAttributes() to obtain the attributes of the
file.  The file doesn't exist in the directory listing but the
afs redirector and the afs smb server will respond to the request.
Perhaps the SMB servers in the broken printers will fail it.

Change-Id: I792bcc89c63871043ce6835a971de284fd754d54
Reviewed-on: http://gerrit.openafs.org/7210
Tested-by: BuildBot <buildbot@rampaginggeek.com>
Reviewed-by: Jeffrey Altman <jaltman@secure-endpoints.com>
Tested-by: Jeffrey Altman <jaltman@secure-endpoints.com>

src/sys/pioctl_nt.c

index 13431b1..c2f73af 100644 (file)
@@ -665,6 +665,7 @@ GetIoctlHandle(char *fileNamep, HANDLE * handlep)
     DWORD res;
     DWORD ioctlDebug = IoctlDebug();
     DWORD gle;
+    DWORD dwAttrib;
     DWORD dwSize = sizeof(szUser);
     BOOL  usingRDR = FALSE;
     int saveerrno;
@@ -828,21 +829,14 @@ GetIoctlHandle(char *fileNamep, HANDLE * handlep)
     }
 
     fflush(stdout);
-    /* now open the file */
-    sharingViolation = 0;
-    do {
-        if (sharingViolation)
-            Sleep(100);
-        fh = CreateFile(tbuffer, FILE_READ_DATA | FILE_WRITE_DATA,
-                        FILE_SHARE_READ, NULL, OPEN_EXISTING,
-                        FILE_FLAG_WRITE_THROUGH, NULL);
-        sharingViolation++;
-    } while (fh == INVALID_HANDLE_VALUE &&
-             GetLastError() == ERROR_SHARING_VIOLATION &&
-             sharingViolation < 100);
-    fflush(stdout);
 
-    if (fh == INVALID_HANDLE_VALUE) {
+    /*
+     * Try to find the correct path and authentication
+     */
+    dwAttrib = GetFileAttributes(tbuffer);
+    if (dwAttrib == INVALID_FILE_ATTRIBUTES) {
+        int  gonext = 0;
+
         gle = GetLastError();
         if (gle && ioctlDebug ) {
             char buf[4096];
@@ -857,17 +851,12 @@ GetIoctlHandle(char *fileNamep, HANDLE * handlep)
                                (va_list *) NULL
                                ) )
             {
-                fprintf(stderr,"pioctl CreateFile(%s) failed: 0x%X\r\n\t[%s]\r\n",
+                fprintf(stderr,"pioctl GetFileAttributes(%s) failed: 0x%X\r\n\t[%s]\r\n",
                         tbuffer,gle,buf);
             }
             errno = saveerrno;
             SetLastError(gle);
         }
-    }
-
-    if (fh == INVALID_HANDLE_VALUE &&
-        GetLastError() != ERROR_SHARING_VIOLATION) {
-        int  gonext = 0;
 
         /* with the redirector interface, fail immediately.  there is nothing to retry */
         if (usingRDR)
@@ -921,19 +910,8 @@ GetIoctlHandle(char *fileNamep, HANDLE * handlep)
             if (gonext)
                 goto try_lsa_principal;
 
-            sharingViolation = 0;
-            do {
-                if (sharingViolation)
-                    Sleep(100);
-                fh = CreateFile(tbuffer, FILE_READ_DATA | FILE_WRITE_DATA,
-                                FILE_SHARE_READ, NULL, OPEN_EXISTING,
-                                FILE_FLAG_WRITE_THROUGH, NULL);
-                sharingViolation++;
-            } while (fh == INVALID_HANDLE_VALUE &&
-                     GetLastError() == ERROR_SHARING_VIOLATION &&
-                     sharingViolation < 100);
-            fflush(stdout);
-            if (fh == INVALID_HANDLE_VALUE) {
+            dwAttrib = GetFileAttributes(tbuffer);
+            if (dwAttrib == INVALID_FILE_ATTRIBUTES) {
                 gle = GetLastError();
                 if (gle && ioctlDebug ) {
                     char buf[4096];
@@ -948,7 +926,7 @@ GetIoctlHandle(char *fileNamep, HANDLE * handlep)
                                         (va_list *) NULL
                                         ) )
                     {
-                        fprintf(stderr,"pioctl CreateFile(%s) failed: 0x%X\r\n\t[%s]\r\n",
+                        fprintf(stderr,"pioctl GetFileAttributes(%s) failed: 0x%X\r\n\t[%s]\r\n",
                                  tbuffer,gle,buf);
                     }
                     errno = saveerrno;
@@ -960,8 +938,7 @@ GetIoctlHandle(char *fileNamep, HANDLE * handlep)
 
   try_lsa_principal:
     if (!usingRDR &&
-        fh == INVALID_HANDLE_VALUE &&
-        GetLastError() != ERROR_SHARING_VIOLATION) {
+        dwAttrib == INVALID_FILE_ATTRIBUTES) {
         int  gonext = 0;
 
         dwSize = sizeof(szUser);
@@ -1002,19 +979,8 @@ GetIoctlHandle(char *fileNamep, HANDLE * handlep)
             if (gonext)
                 goto try_sam_compat;
 
-            sharingViolation = 0;
-            do {
-                if (sharingViolation)
-                    Sleep(100);
-                fh = CreateFile(tbuffer, FILE_READ_DATA | FILE_WRITE_DATA,
-                                FILE_SHARE_READ, NULL, OPEN_EXISTING,
-                                FILE_FLAG_WRITE_THROUGH, NULL);
-                sharingViolation++;
-            } while (fh == INVALID_HANDLE_VALUE &&
-                     GetLastError() == ERROR_SHARING_VIOLATION &&
-                     sharingViolation < 100);
-            fflush(stdout);
-            if (fh == INVALID_HANDLE_VALUE) {
+            dwAttrib = GetFileAttributes(tbuffer);
+            if (dwAttrib == INVALID_FILE_ATTRIBUTES) {
                 gle = GetLastError();
                 if (gle && ioctlDebug ) {
                     char buf[4096];
@@ -1029,7 +995,7 @@ GetIoctlHandle(char *fileNamep, HANDLE * handlep)
                                         (va_list *) NULL
                                         ) )
                     {
-                        fprintf(stderr,"pioctl CreateFile(%s) failed: 0x%X\r\n\t[%s]\r\n",
+                        fprintf(stderr,"pioctl GetFileAttributes(%s) failed: 0x%X\r\n\t[%s]\r\n",
                                  tbuffer,gle,buf);
                     }
                     errno = saveerrno;
@@ -1041,8 +1007,7 @@ GetIoctlHandle(char *fileNamep, HANDLE * handlep)
 
   try_sam_compat:
     if (!usingRDR &&
-        fh == INVALID_HANDLE_VALUE &&
-        GetLastError() != ERROR_SHARING_VIOLATION) {
+        dwAttrib == INVALID_FILE_ATTRIBUTES) {
         dwSize = sizeof(szUser);
         if (GetUserNameEx(NameSamCompatible, szUser, &dwSize)) {
             if ( ioctlDebug ) {
@@ -1077,19 +1042,8 @@ GetIoctlHandle(char *fileNamep, HANDLE * handlep)
                 return -1;
             }
 
-            sharingViolation = 0;
-            do {
-                if (sharingViolation)
-                    Sleep(100);
-                fh = CreateFile(tbuffer, FILE_READ_DATA | FILE_WRITE_DATA,
-                                FILE_SHARE_READ, NULL, OPEN_EXISTING,
-                                FILE_FLAG_WRITE_THROUGH, NULL);
-                sharingViolation++;
-            } while (fh == INVALID_HANDLE_VALUE &&
-                     GetLastError() == ERROR_SHARING_VIOLATION &&
-                     sharingViolation < 100);
-            fflush(stdout);
-            if (fh == INVALID_HANDLE_VALUE) {
+            dwAttrib = GetFileAttributes(tbuffer);
+            if (dwAttrib == INVALID_FILE_ATTRIBUTES) {
                 gle = GetLastError();
                 if (gle && ioctlDebug ) {
                     char buf[4096];
@@ -1104,7 +1058,7 @@ GetIoctlHandle(char *fileNamep, HANDLE * handlep)
                                         (va_list *) NULL
                                         ) )
                     {
-                        fprintf(stderr,"pioctl CreateFile(%s) failed: 0x%X\r\n\t[%s]\r\n",
+                        fprintf(stderr,"pioctl GetFileAttributes(%s) failed: 0x%X\r\n\t[%s]\r\n",
                                  tbuffer,gle,buf);
                     }
                     errno = saveerrno;
@@ -1117,6 +1071,26 @@ GetIoctlHandle(char *fileNamep, HANDLE * handlep)
         }
     }
 
+    if ( dwAttrib != (FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM)) {
+        fprintf(stderr, "GetFileAttributes(%s) returned: 0x%08X\r\n",
+                tbuffer, dwAttrib);
+        return -1;
+    }
+
+    /* tbuffer now contains the correct path; now open the file */
+    sharingViolation = 0;
+    do {
+        if (sharingViolation)
+            Sleep(100);
+        fh = CreateFile(tbuffer, FILE_READ_DATA | FILE_WRITE_DATA,
+                        FILE_SHARE_READ, NULL, OPEN_EXISTING,
+                        FILE_FLAG_WRITE_THROUGH, NULL);
+        sharingViolation++;
+    } while (fh == INVALID_HANDLE_VALUE &&
+             GetLastError() == ERROR_SHARING_VIOLATION &&
+             sharingViolation < 100);
+    fflush(stdout);
+
     if (fh == INVALID_HANDLE_VALUE)
         return -1;