You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by mt...@apache.org on 2009/04/28 09:46:59 UTC

svn commit: r769272 - in /commons/sandbox/runtime/trunk/src: main/native/include/ main/native/include/arch/windows/ main/native/os/win32/ test/org/apache/commons/runtime/

Author: mturk
Date: Tue Apr 28 07:46:58 2009
New Revision: 769272

URL: http://svn.apache.org/viewvc?rev=769272&view=rev
Log:
Implement group and user on windows

Modified:
    commons/sandbox/runtime/trunk/src/main/native/include/acr_private.h
    commons/sandbox/runtime/trunk/src/main/native/include/arch/windows/acr_arch_private.h
    commons/sandbox/runtime/trunk/src/main/native/os/win32/group.c
    commons/sandbox/runtime/trunk/src/main/native/os/win32/user.c
    commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestGroup.java
    commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestUser.java

Modified: commons/sandbox/runtime/trunk/src/main/native/include/acr_private.h
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/include/acr_private.h?rev=769272&r1=769271&r2=769272&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/include/acr_private.h (original)
+++ commons/sandbox/runtime/trunk/src/main/native/include/acr_private.h Tue Apr 28 07:46:58 2009
@@ -360,6 +360,13 @@
         (*_E)->DeleteLocalRef(_E, _str);                                    \
     } else (void)(0)
 
+#define SET_IFIELD_C(I, O, V)  \
+    if (_f##I##n.i && (V) && *(V)) {                                        \
+        jstring _str = ACR_NewJavaStringW(_E, (V));                         \
+        (*_E)->SetObjectField(_E, (O), _f##I##n.i, _str);                   \
+        (*_E)->DeleteLocalRef(_E, _str);                                    \
+    } else (void)(0)
+
 #define SET_IFIELD_O(I, O, V)  \
     if (_f##I##n.i) {                                                       \
         (*_E)->SetObjectField(_E, (O), _f##I##n.i, (jobject)(V));           \

Modified: commons/sandbox/runtime/trunk/src/main/native/include/arch/windows/acr_arch_private.h
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/include/arch/windows/acr_arch_private.h?rev=769272&r1=769271&r2=769272&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/include/arch/windows/acr_arch_private.h (original)
+++ commons/sandbox/runtime/trunk/src/main/native/include/arch/windows/acr_arch_private.h Tue Apr 28 07:46:58 2009
@@ -59,6 +59,11 @@
 #define ACR_JNI_PLATFORM_DECLARE(RT, CL, FN)  \
     JNIEXPORT RT JNICALL Java_org_apache_commons_runtime_platform_windows_##CL##_##FN
 
+/**
+ * Local functions from user.c
+ */
+PSID ACR_GetSidFromAccountName(LPCWSTR name, PSID_NAME_USE sidtype);
+SID_NAME_USE ACR_GetAccountNameFromSid(LPWSTR buf, size_t blen, PSID psid);
 
 
 #ifdef __cplusplus

Modified: commons/sandbox/runtime/trunk/src/main/native/os/win32/group.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/win32/group.c?rev=769272&r1=769271&r2=769272&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/win32/group.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/win32/group.c Tue Apr 28 07:46:58 2009
@@ -88,14 +88,83 @@
         return NULL;
 }
 
-ACR_JNI_EXPORT_DECLARE(jobject, Hroup, get0)(ACR_JNISTDARGS,
+static int sid_descriptor_handler(ACR_JNISTDARGS, acr_descriptor_cb_type_e t,
+                                  acr_descriptor_cb_t *cb)
+{
+    if (t == ACR_DESC_CLOSE) {
+        if (cb->dp) {
+            free(cb->dp);
+            return ACR_SUCCESS;
+        }
+        return ACR_EINCOMPLETE;
+    }
+    else {
+        /* We don't handle anything but close. */
+        return ACR_ENOTIMPL;
+    }
+}
+
+static jobject grp_object_create(JNIEnv *_E, LPCWSTR name)
+{
+    jobject grp;
+    jobject gid;
+    int err;
+    DWORD rc;
+    SID_NAME_USE sidtype;
+    PSID  sid;
+    PLOCALGROUP_INFO_1 pb = NULL;
+    LPWSTR ssid = NULL;
+
+    sid =  ACR_GetSidFromAccountName(name, &sidtype);
+    if (!sid) {
+        err = ACR_GET_OS_ERROR();
+        if  (ACR_STATUS_IS_EACCES(err))
+            ACR_ThrowException(_E, THROW_NMARK, ACR_EX_ESECURITY, 0);
+        else if (ACR_TO_OS_ERROR(err) != ERROR_NONE_MAPPED)
+            ACR_ThrowException(_E, THROW_NMARK, ACR_EX_EIO, err);
+
+        /* TODO: Throw exception */
+        return NULL;
+    }
+
+    gid = ACR_DescriptorCreate(_E, 0, -1, sid, sid_descriptor_handler);
+    if (!gid) {
+        free(gid);
+        return NULL;
+    }
+    grp = (*_E)->NewObject(_E, _clazzn.i, J4MID(0000), gid);
+    if (!grp) {
+        ACR_DescriptorCleanup(_E, grp);
+        return NULL;
+    }
+
+    rc =  NetGroupGetInfo(NULL, name, 1, (LPBYTE *)&pb);
+    if (rc == ERROR_SUCCESS) {
+        SET_IFIELD_W(0000, grp, pb->lgrpi1_name);
+        SET_IFIELD_C(0001, grp, pb->lgrpi1_comment);            
+        NetApiBufferFree(pb);
+    }
+    else {
+        SET_IFIELD_W(0000, grp, name);
+    }
+    if (ConvertSidToStringSidW(sid, &ssid)) {
+
+        SET_IFIELD_W(0002, grp, ssid);
+        LocalFree(ssid);
+    }
+    SET_IFIELD_Z(0003, grp, JNI_TRUE);
+
+    return grp;
+}
+
+ACR_JNI_EXPORT_DECLARE(jobject, Group, get0)(ACR_JNISTDARGS,
                                              jstring name)
 {
     jobject grp = NULL;
     UNREFERENCED_O;
 
     WITH_WSTR(name) {
-        int rc;
+        grp = grp_object_create(_E, J2W(name));
 
     } END_WITH_WSTR(name);
 

Modified: commons/sandbox/runtime/trunk/src/main/native/os/win32/user.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/win32/user.c?rev=769272&r1=769271&r2=769272&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/win32/user.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/win32/user.c Tue Apr 28 07:46:58 2009
@@ -101,6 +101,165 @@
         return NULL;
 }
 
+PSID ACR_GetSidFromAccountName(LPCWSTR name, PSID_NAME_USE sidtype)
+{
+    WCHAR domain[MAX_PATH];
+    DWORD domlen = MAX_PATH;
+    DWORD sidlen = 0;
+    PSID  sid = NULL;
+
+    *sidtype = -1;
+    LookupAccountNameW(NULL, name, NULL, &sidlen,
+                       NULL, &domlen, sidtype);
+    if (sidlen) {
+        /* Give it back on the second pass with proper sidlen
+         */
+        if (!(sid = malloc(sidlen)))
+            return NULL;
+        domlen = MAX_PATH;
+        if (!LookupAccountNameW(NULL, name, sid, &sidlen,
+                                domain, &domlen, sidtype)) {
+            free(sid);
+            sid = NULL;
+        }
+    }
+    return sid;
+}
+
+SID_NAME_USE ACR_GetAccountNameFromSid(LPWSTR buf, size_t blen, PSID psid)
+{
+    WCHAR domain[MAX_PATH];
+    DWORD domlen = MAX_PATH;
+    DWORD acclen = (DWORD)blen;
+    SID_NAME_USE sidtype;
+
+    if (IsWellKnownSid(psid, WinBuiltinAdministratorsSid)) {
+        wcscpy(buf, L"SYSTEM");
+        return SidTypeWellKnownGroup;
+    }
+    if (!LookupAccountSidW(NULL, psid,
+                           buf, &acclen,
+                           domain, &domlen,
+                           &sidtype)) {
+
+        sidtype = -1;
+        buf[0]  = L'\0';
+    }
+    return sidtype;
+}
+
+static void GetUserHomePath(LPWSTR buf, DWORD blen, PSID sid)
+{
+    LPWSTR ssid = NULL;
+    WCHAR regk[ACR_MBUFF_SIZ];
+    HKEY  key;
+    DWORD rv, type;
+    DWORD keylen;
+
+    buf[0] = L'\0';
+
+    if (!ConvertSidToStringSidW(sid, &ssid)) {
+        return;
+    }
+
+    wcscpy(regk, L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\ProfileList\\");
+    wcscat(regk, ssid);
+    LocalFree(ssid);
+
+    if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, regk, 0,
+                     KEY_QUERY_VALUE, &key) == ERROR_SUCCESS) {
+        keylen = sizeof(regk);
+        rv = RegQueryValueExW(key, L"ProfileImagePath", NULL, &type,
+                                   (void *)regk, &keylen);
+        RegCloseKey(key);
+        if (rv != ERROR_SUCCESS)
+            return;
+        if (type == REG_SZ)
+            wcsncpy(buf, regk, blen);
+        else if (type == REG_EXPAND_SZ)
+            ExpandEnvironmentStringsW(regk, buf, blen);
+        buf[blen] = L'\0';
+    }
+}
+
+static int sid_descriptor_handler(ACR_JNISTDARGS, acr_descriptor_cb_type_e t,
+                                  acr_descriptor_cb_t *cb)
+{
+    if (t == ACR_DESC_CLOSE) {
+        if (cb->dp) {
+            free(cb->dp);
+            return ACR_SUCCESS;
+        }
+        return ACR_EINCOMPLETE;
+    }
+    else {
+        /* We don't handle anything but close. */
+        return ACR_ENOTIMPL;
+    }
+}
+
+static jobject usr_object_create(JNIEnv *_E, LPCWSTR name)
+{
+    jobject usr;
+    jobject uid;
+    int err;
+    DWORD rc;
+    SID_NAME_USE sidtype;
+    PSID  sid;
+    PUSER_INFO_2 pb = NULL;
+    LPWSTR ssid = NULL;
+    WCHAR home[MAX_PATH+1];
+
+    sid =  ACR_GetSidFromAccountName(name, &sidtype);
+    if (!sid) {
+        err = ACR_GET_OS_ERROR();
+        if  (ACR_STATUS_IS_EACCES(err))
+            ACR_ThrowException(_E, THROW_NMARK, ACR_EX_ESECURITY, 0);
+        else if (ACR_TO_OS_ERROR(err) != ERROR_NONE_MAPPED)
+            ACR_ThrowException(_E, THROW_NMARK, ACR_EX_EIO, err);
+
+        /* TODO: Throw exception */
+        return NULL;
+    }
+
+    uid = ACR_DescriptorCreate(_E, 0, -1, sid, sid_descriptor_handler);
+    if (!uid) {
+        free(sid);
+        return NULL;
+    }
+    usr = (*_E)->NewObject(_E, _clazzn.i, J4MID(0000), uid);
+    if (!usr) {
+        ACR_DescriptorCleanup(_E, uid);
+        return NULL;
+    }
+
+    rc = NetUserGetInfo(NULL, name, 2, (LPBYTE *)&pb);
+    if (rc == ERROR_SUCCESS) {
+        SET_IFIELD_W(0000, usr, pb->usri2_name);
+        SET_IFIELD_C(0001, usr, pb->usri2_full_name);
+        SET_IFIELD_C(0002, usr, pb->usri2_comment);
+        if (pb->usri2_home_dir && *pb->usri2_home_dir)
+            SET_IFIELD_W(0003, usr, pb->usri2_home_dir);
+        else {
+            GetUserHomePath(home, MAX_PATH, sid);
+            SET_IFIELD_C(0003, usr, home);
+        }
+        NetApiBufferFree(pb);
+    }
+    else {
+        SET_IFIELD_W(0000, usr, name);
+        SET_IFIELD_W(0001, usr, name);
+        GetUserHomePath(home, MAX_PATH, sid);
+        SET_IFIELD_C(0003, usr, home);
+    }
+    if (ConvertSidToStringSidW(sid, &ssid)) {
+
+        SET_IFIELD_W(0005, usr, ssid);
+        LocalFree(ssid);
+    }
+
+    return usr;
+}
 
 ACR_JNI_EXPORT_DECLARE(jobject, User, get0)(ACR_JNISTDARGS,
                                             jstring name)
@@ -109,8 +268,7 @@
     UNREFERENCED_O;
 
     WITH_WSTR(name) {
-        int rc;
-
+        usr = usr_object_create(_E, J2W(name));
     } END_WITH_WSTR(name);
 
     return usr;

Modified: commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestGroup.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestGroup.java?rev=769272&r1=769271&r2=769272&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestGroup.java (original)
+++ commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestGroup.java Tue Apr 28 07:46:58 2009
@@ -44,9 +44,15 @@
     {
         Group g;
 
-        g = Group.get("bin");
+        if (OS.TYPE.contains(OsType.WINDOWS))
+            g = Group.get("Users");
+        else
+            g = Group.get("bin");
         assertNotNull("Group" + g);
         assertTrue("Local",  g.IsLocal());
+        System.out.println("Group      " + g.getName());
+        System.out.println("Comment    " + g.getComment());
+        System.out.println("Id         " + g);
     }
 
     public void testUnknownGroup()

Modified: commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestUser.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestUser.java?rev=769272&r1=769271&r2=769272&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestUser.java (original)
+++ commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestUser.java Tue Apr 28 07:46:58 2009
@@ -43,11 +43,16 @@
         throws Exception
     {
         User u;
-
-        u = User.get("root");
+        
+        if (OS.TYPE.contains(OsType.WINDOWS))
+            u = User.get("Guest");
+        else
+            u = User.get("root");
+        
         assertNotNull("User" + u);
         System.out.println();
-        System.out.println("User       " + u.getFullName());
+        System.out.println("User       " + u.getName());
+        System.out.println("Full Name  " + u.getFullName());
         System.out.println("Home       " + u.getHome());
         System.out.println("Comment    " + u.getComment());
         System.out.println("Shell      " + u.getShell());