commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mt...@apache.org
Subject svn commit: r1092182 - in /commons/sandbox/runtime/trunk/src/main/native: Makefile.msc.in os/win32/winapi.c
Date Thu, 14 Apr 2011 12:29:57 GMT
Author: mturk
Date: Thu Apr 14 12:29:57 2011
New Revision: 1092182

URL: http://svn.apache.org/viewvc?rev=1092182&view=rev
Log:
Start adding win32 direct api functions

Added:
    commons/sandbox/runtime/trunk/src/main/native/os/win32/winapi.c   (with props)
Modified:
    commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in

Modified: commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in?rev=1092182&r1=1092181&r2=1092182&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in (original)
+++ commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in Thu Apr 14 12:29:57 2011
@@ -81,7 +81,8 @@ WIN32_SOURCES=\
 	$(TOPDIR)\os\win32\platform.c \
 	$(TOPDIR)\os\win32\semaphore.c \
 	$(TOPDIR)\os\win32\time.c \
-	$(TOPDIR)\os\win32\util.c
+	$(TOPDIR)\os\win32\util.c \
+	$(TOPDIR)\os\win32\winapi.c
 
 
 LIBSOURCES=\

Added: commons/sandbox/runtime/trunk/src/main/native/os/win32/winapi.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/win32/winapi.c?rev=1092182&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/win32/winapi.c (added)
+++ commons/sandbox/runtime/trunk/src/main/native/os/win32/winapi.c Thu Apr 14 12:29:57 2011
@@ -0,0 +1,154 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "acr/string.h"
+#include "acr/debug.h"
+#include "acr/memory.h"
+#include "acr/port.h"
+#include "acr/clazz.h"
+#include "acr/misc.h"
+#include "arch_defs.h"
+
+#include <sddl.h>
+
+/* Closest we can get to directly using Win32 API
+ */
+
+static PSECURITY_ATTRIBUTES _null_SA[2] = { 0, 0 };
+static SECURITY_ATTRIBUTES  _zero_SA[2];
+
+static PSECURITY_ATTRIBUTES GetSaWithNullDacl(JNI_STDENV, jboolean inherit)
+{
+    DWORD rc = 0;
+    PSECURITY_DESCRIPTOR pSD = 0;
+    int   si = inherit ? 1 : 0;
+
+    if (_null_SA[si] != 0) {
+        /* Return cached entry */
+        return _null_SA[si];
+    }
+    else {
+        _zero_SA[si].nLength = TSIZEOF(DWORD, SECURITY_ATTRIBUTES);
+        _zero_SA[si].lpSecurityDescriptor = 0;
+    }
+
+    if (!(_null_SA[si] = LocalAlloc(LPTR, sizeof(SECURITY_ATTRIBUTES)))) {
+        rc = GetLastError();
+        ACR_THROW(ACR_EX_ENOMEM, 0);
+        goto cleanup;
+    }
+    _null_SA[si]->nLength = TSIZEOF(DWORD, SECURITY_ATTRIBUTES);
+
+    pSD = LocalAlloc(LPTR, SECURITY_DESCRIPTOR_MIN_LENGTH);
+    if (pSD == 0) {
+        rc = GetLastError();
+        ACR_THROW(ACR_EX_ENOMEM, 0);
+        goto cleanup;
+    }
+    _null_SA[si]->lpSecurityDescriptor = pSD;
+    if (!InitializeSecurityDescriptor(pSD, SECURITY_DESCRIPTOR_REVISION)) {
+        rc = GetLastError();
+        goto cleanup;
+    }
+    if (!SetSecurityDescriptorDacl(pSD, TRUE, (PACL)0, FALSE)) {
+        rc = GetLastError();
+        goto cleanup;
+    }
+    _null_SA[si]->lpSecurityDescriptor = pSD;
+    _null_SA[si]->bInheritHandle       = inherit;
+
+    return _null_SA[si];
+
+cleanup:
+    if (pSD != 0)
+        LocalFree(pSD);
+    if (_null_SA[si] != 0)
+        LocalFree(_null_SA[si]);
+
+    _null_SA[si] = &_zero_SA[si];
+    _null_SA[si]->bInheritHandle = inherit;
+
+    SetLastError(rc);
+    return _null_SA[si];
+}
+
+ACR_WIN_EXPORT(jlong, Api, GetSaWithNullDacl)(JNI_STDARGS, jboolean inherit)
+{
+    return P2J(GetSaWithNullDacl(env, inherit));
+}
+
+ACR_WIN_EXPORT(jlong, Api, ConvertStringSecurityDescriptorToSecurityDescriptor)
+              (JNI_STDARGS, jstring desc, jint rev)
+{
+    PSECURITY_DESCRIPTOR pSD = 0;
+
+    WITH_WSTR(desc) {
+        if (!ConvertStringSecurityDescriptorToSecurityDescriptorW(J2S(desc),
+                                                    SDDL_REVISION_1, &pSD, 0)) {
+            ACR_THROW_SYS_ERROR();
+        }
+    } DONE_WITH_STR(desc);
+
+    return P2J(pSD);
+}
+
+ACR_WIN_EXPORT(jint, Api, CloseHandle)(JNI_STDARGS, jlong handle)
+{
+    if (!CloseHandle(J2P(handle, HANDLE)))
+        return ACR_GET_OS_ERROR();
+    else
+        return 0;
+}
+
+ACR_WIN_EXPORT(void, Api, LocalFree)(JNI_STDARGS, jlong ptr)
+{
+    if (ptr != 0LL)
+        LocalFree(J2P(ptr, LPVOID));
+}
+
+ACR_WIN_EXPORT(jlong, Api, GetCurrentProcessToken)(JNI_STDARGS)
+{
+    HANDLE ptoken = 0;
+    HANDLE dtoken = 0;
+
+    if (!OpenProcessToken(GetCurrentProcess(),
+                          MAXIMUM_ALLOWED,
+                          &ptoken))
+        return 0;
+    if (!DuplicateTokenEx(ptoken, MAXIMUM_ALLOWED, GetSaWithNullDacl(env, JNI_FALSE),
+                          SecurityIdentification, TokenPrimary,
+                          &dtoken)) {
+        return 0;
+    }
+    CloseHandle(ptoken);
+    return P2J(dtoken);
+}
+
+ACR_WIN_EXPORT(jlong, Api, AllocateWellKnownSid)(JNI_STDARGS, jint type)
+{
+    DWORD ss = SECURITY_MAX_SID_SIZE;
+    PSID sid;
+
+    if (!(sid = (PSID)LocalAlloc(LMEM_FIXED, ss))) {
+        ACR_THROW(ACR_EX_ENOMEM, 0);
+        return 0;
+    }
+    if (!CreateWellKnownSid(type, 0, sid, &ss)) {
+        LocalFree(sid);
+        sid = 0;
+    }
+    return P2J(sid);
+}

Propchange: commons/sandbox/runtime/trunk/src/main/native/os/win32/winapi.c
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message