cvs: TSRM(PHP_5_3) / tsrm_win32.c tsrm_win32.h

[email protected] ("Pierre-Alain Joye") Sun, 17 May 2009 19:42:53 -0000
Newsgroups php.zend-engine.cvs
Message-ID <cvspajoye1242589373@cvsserver>
pajoye		Sun May 17 19:42:53 2009 UTC

  Modified files:              (Branch: PHP_5_3)
    /TSRM	tsrm_win32.c tsrm_win32.h 
  Log:
  - #44859, fixed support for windows ACL, drop win9x code
  
http://cvs.php.net/viewvc.cgi/TSRM/tsrm_win32.c?r1=1.27.2.1.2.7.2.4&r2=1.27.2.1.2.7.2.5&diff_format=u
Index: TSRM/tsrm_win32.c
diff -u TSRM/tsrm_win32.c:1.27.2.1.2.7.2.4 TSRM/tsrm_win32.c:1.27.2.1.2.7.2.5
--- TSRM/tsrm_win32.c:1.27.2.1.2.7.2.4	Fri May 15 17:48:34 2009
+++ TSRM/tsrm_win32.c	Sun May 17 19:42:53 2009
@@ -16,13 +16,14 @@
    +----------------------------------------------------------------------+
 */
 
-/* $Id: tsrm_win32.c,v 1.27.2.1.2.7.2.4 2009/05/15 17:48:34 pajoye Exp $ */
+/* $Id: tsrm_win32.c,v 1.27.2.1.2.7.2.5 2009/05/17 19:42:53 pajoye Exp $ */
 
 #include <stdio.h>
 #include <fcntl.h>
 #include <io.h>
 #include <process.h>
 #include <time.h>
+#include <errno.h>
 
 #define TSRM_INCLUDE_FULL_WINDOWS_HEADERS
 
@@ -45,6 +46,7 @@
 	globals->process_size = 0;
 	globals->shm_size	  = 0;
 	globals->comspec = _strdup((GetVersion()<0x80000000)?"cmd.exe":"command.com");
+	globals->impersonation_token = NULL;
 }
 
 static void tsrm_win32_dtor(tsrm_win32_globals *globals TSRMLS_DC)
@@ -86,21 +88,82 @@
 
 TSRM_API int tsrm_win32_access(const char *pathname, int mode)
 {
+	SECURITY_INFORMATION sec_info = OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION;
+	GENERIC_MAPPING gen_map = { FILE_GENERIC_READ, FILE_GENERIC_WRITE, FILE_GENERIC_EXECUTE, FILE_ALL_ACCESS };
+	DWORD priv_set_length = sizeof(PRIVILEGE_SET);
+
+	PRIVILEGE_SET privilege_set = {0};
+	DWORD sec_desc_length = 0, desired_access = 0, granted_access = 0;
+	BYTE * psec_desc = NULL;
+	BOOL fAccess = FALSE;
+	HANDLE process_token = NULL;
+	TSRMLS_FETCH();
+
 	if (mode == 1 /*X_OK*/) {
-#if 1
-		/* This code is not supported by Windows 98,
-		 * but we don't support it anymore */
 		DWORD type;
+		return GetBinaryType(pathname, &type) ? 0 : -1;
+	} else {
+		if(access(pathname, mode)) {
+				return errno;
+		}
 
-		return GetBinaryType(pathname, &type)?0:-1;
-#else
-		SHFILEINFO sfi;
+		/* Do a full access check because access() will only check read-only attribute */
+		if(mode == 0 || mode > 6) {
+			desired_access = FILE_GENERIC_READ;
+		} else if(mode <= 2) {
+				desired_access = FILE_GENERIC_WRITE;
+		} else if(mode <= 4) {
+				desired_access = FILE_GENERIC_READ;
+		} else { // if(mode <= 6)
+				desired_access = FILE_GENERIC_READ | FILE_GENERIC_WRITE;
+		}
 
-		return access(pathname, 0) == 0 &&
-			SHGetFileInfo(pathname, 0, &sfi, sizeof(SHFILEINFO), SHGFI_EXETYPE) != 0 ? 0 : -1;
-#endif
-	} else {
-		return access(pathname, mode);
+		/* Get size of security buffer. Call is expected to fail */
+		if(GetFileSecurity(pathname, sec_info, NULL, 0, &sec_desc_length)) {
+			goto Finished;
+		}
+
+		psec_desc = (BYTE *)malloc(sec_desc_length);
+		if(psec_desc == NULL ||
+			 !GetFileSecurity(pathname, sec_info, (PSECURITY_DESCRIPTOR)psec_desc, sec_desc_length, &sec_desc_length)) {
+			goto Finished;
+		}
+
+		if(TWG(impersonation_token) == NULL) {
+
+			if(!OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE | TOKEN_QUERY, &process_token)) {
+				goto Finished;
+			}
+
+			/* Access check requires impersonation token. Create a duplicate token. */
+			if(!DuplicateToken(process_token, SecurityImpersonation, &TWG(impersonation_token))) {
+				goto Finished;
+			}
+		}
+
+		if(!AccessCheck((PSECURITY_DESCRIPTOR)psec_desc, TWG(impersonation_token), desired_access, &gen_map, &privilege_set, &priv_set_length, &granted_access, &fAccess)) {
+				goto Finished;
+		}
+
+Finished:
+
+		/* impersonation_token will be closed when the process dies */
+		if(process_token != NULL) {
+			CloseHandle(process_token);
+			process_token = NULL;
+		}
+
+		if(psec_desc != NULL) {
+			free(psec_desc);
+			psec_desc = NULL;
+		}
+
+		if(fAccess == FALSE) {
+			errno = EACCES;
+			return errno;
+		} else {
+			return 0;
+		}
 	}
 }
 
http://cvs.php.net/viewvc.cgi/TSRM/tsrm_win32.h?r1=1.19.2.1.2.1.2.2&r2=1.19.2.1.2.1.2.3&diff_format=u
Index: TSRM/tsrm_win32.h
diff -u TSRM/tsrm_win32.h:1.19.2.1.2.1.2.2 TSRM/tsrm_win32.h:1.19.2.1.2.1.2.3
--- TSRM/tsrm_win32.h:1.19.2.1.2.1.2.2	Wed Dec 31 11:15:31 2008
+++ TSRM/tsrm_win32.h	Sun May 17 19:42:53 2009
@@ -16,7 +16,7 @@
    +----------------------------------------------------------------------+
 */
 
-/* $Id: tsrm_win32.h,v 1.19.2.1.2.1.2.2 2008/12/31 11:15:31 sebastian Exp $ */
+/* $Id: tsrm_win32.h,v 1.19.2.1.2.1.2.3 2009/05/17 19:42:53 pajoye Exp $ */
 
 #ifndef TSRM_WIN32_H
 #define TSRM_WIN32_H
@@ -63,6 +63,7 @@
 	int				process_size;
 	int				shm_size;
 	char			*comspec;
+	HANDLE impersonation_token;
 } tsrm_win32_globals;
 
 #ifdef ZTS