+
+ if (uniname)
+ g_free (uniname);
+
+ return result;
+}
+
+
+gpointer
+ves_icall_System_Security_Principal_WindowsIdentity_GetUserToken (MonoString *username)
+{
+#ifdef PLATFORM_WIN32
+ gpointer token = NULL;
+
+ MONO_ARCH_SAVE_REGS;
+
+ /* TODO: MS has something like this working in Windows 2003 (client and
+ * server) but works only for domain accounts (so it's quite limiting).
+ * http://www.develop.com/kbrown/book/html/howto_logonuser.html
+ */
+ g_warning ("Unsupported on Win32 (anyway requires W2K3 minimum)");
+
+#else /* PLATFORM_WIN32*/
+
+#ifdef HAVE_GETPWNAM_R
+ struct passwd pwd;
+ size_t fbufsize;
+ gchar *fbuf;
+ gint32 retval;
+#endif
+ gpointer token = (gpointer) -2;
+ struct passwd *p;
+ gchar *utf8_name;
+ gboolean result;
+
+ MONO_ARCH_SAVE_REGS;
+
+ utf8_name = mono_unicode_to_external (mono_string_chars (username));
+
+#ifdef HAVE_GETPWNAM_R
+#ifdef _SC_GETPW_R_SIZE_MAX
+ fbufsize = mono_sysconf (_SC_GETPW_R_SIZE_MAX);
+#else
+ fbufsize = MONO_SYSCONF_DEFAULT_SIZE;
+#endif
+
+ fbuf = g_malloc0 (fbufsize);
+ retval = getpwnam_r (utf8_name, &pwd, fbuf, fbufsize, &p);
+ result = ((retval == 0) && (p == &pwd));
+#else
+ /* default to non thread-safe but posix compliant function */
+ p = getpwnam (utf8_name);
+ result = (p != NULL);
+#endif
+
+ if (result) {
+ token = GINT_TO_POINTER (p->pw_uid);
+ }
+
+#ifdef HAVE_GETPWNAM_R
+ g_free (fbuf);
+#endif
+ g_free (utf8_name);
+#endif
+ return token;
+}
+
+
+/* http://www.dotnet247.com/247reference/msgs/39/195403.aspx
+// internal static string[] WindowsIdentity._GetRoles (IntPtr token)
+*/
+MonoArray*
+ves_icall_System_Security_Principal_WindowsIdentity_GetRoles (gpointer token)
+{
+ MonoArray *array = NULL;
+ MonoDomain *domain = mono_domain_get ();
+#ifdef PLATFORM_WIN32
+ gint32 size = 0;
+
+ MONO_ARCH_SAVE_REGS;
+
+ GetTokenInformation (token, TokenGroups, NULL, size, (PDWORD)&size);
+ if (size > 0) {
+ TOKEN_GROUPS *tg = g_malloc0 (size);
+ if (GetTokenInformation (token, TokenGroups, tg, size, (PDWORD)&size)) {
+ int i=0;
+ int num = tg->GroupCount;
+
+ array = mono_array_new (domain, mono_get_string_class (), num);
+
+ for (i=0; i < num; i++) {
+ gint32 size = 0;
+ gunichar2 *uniname = GetSidName (NULL, tg->Groups [i].Sid, &size);
+
+ if (uniname) {
+ MonoString *str = mono_string_new_utf16 (domain, uniname, size);
+ mono_array_setref (array, i, str);
+ g_free (uniname);
+ }
+ }
+ }
+ g_free (tg);
+ }
+#else
+ /* POSIX-compliant systems should use IsMemberOfGroupId or IsMemberOfGroupName */
+ g_warning ("WindowsIdentity._GetRoles should never be called on POSIX");
+#endif
+ if (!array) {
+ /* return empty array of string, i.e. string [0] */
+ array = mono_array_new (domain, mono_get_string_class (), 0);
+ }
+ return array;
+}
+
+
+/* System.Security.Principal.WindowsImpersonationContext */
+
+
+gboolean
+ves_icall_System_Security_Principal_WindowsImpersonationContext_CloseToken (gpointer token)
+{
+ gboolean result = TRUE;
+
+ MONO_ARCH_SAVE_REGS;
+
+#ifdef PLATFORM_WIN32
+ result = (CloseHandle (token) != 0);
+#endif
+ return result;
+}
+
+
+gpointer
+ves_icall_System_Security_Principal_WindowsImpersonationContext_DuplicateToken (gpointer token)
+{
+ gpointer dupe = NULL;
+
+ MONO_ARCH_SAVE_REGS;
+
+#ifdef PLATFORM_WIN32
+ if (DuplicateToken (token, SecurityImpersonation, &dupe) == 0) {
+ dupe = NULL;
+ }
+#else
+ dupe = token;
+#endif
+ return dupe;
+}
+
+
+gboolean
+ves_icall_System_Security_Principal_WindowsImpersonationContext_SetCurrentToken (gpointer token)
+{
+ MONO_ARCH_SAVE_REGS;
+
+ /* Posix version implemented in /mono/mono/io-layer/security.c */
+ return (ImpersonateLoggedOnUser (token) != 0);
+}
+
+
+gboolean
+ves_icall_System_Security_Principal_WindowsImpersonationContext_RevertToSelf (void)
+{
+ MONO_ARCH_SAVE_REGS;
+
+ /* Posix version implemented in /mono/mono/io-layer/security.c */
+ return (RevertToSelf () != 0);
+}
+
+
+/* System.Security.Principal.WindowsPrincipal */
+
+gboolean
+ves_icall_System_Security_Principal_WindowsPrincipal_IsMemberOfGroupId (gpointer user, gpointer group)
+{
+ gboolean result = FALSE;
+
+#ifdef PLATFORM_WIN32
+ MONO_ARCH_SAVE_REGS;
+
+ /* The convertion from an ID to a string is done in managed code for Windows */
+ g_warning ("IsMemberOfGroupId should never be called on Win32");
+
+#else /* PLATFORM_WIN32 */
+
+#ifdef HAVE_GETGRGID_R
+ struct group grp;
+ size_t fbufsize;
+ gchar *fbuf;
+ gint32 retval;
+#endif
+ struct group *g = NULL;
+
+ MONO_ARCH_SAVE_REGS;
+
+#ifdef HAVE_GETGRGID_R
+#ifdef _SC_GETGR_R_SIZE_MAX
+ fbufsize = mono_sysconf (_SC_GETGR_R_SIZE_MAX);
+#else
+ fbufsize = MONO_SYSCONF_DEFAULT_SIZE;
+#endif
+ fbuf = g_malloc0 (fbufsize);
+ retval = getgrgid_r ((gid_t) GPOINTER_TO_INT (group), &grp, fbuf, fbufsize, &g);
+ result = ((retval == 0) && (g == &grp));
+#else
+ /* default to non thread-safe but posix compliant function */
+ g = getgrgid ((gid_t) GPOINTER_TO_INT (group));
+ result = (g != NULL);
+#endif
+
+ if (result) {
+ result = IsMemberOf ((uid_t) GPOINTER_TO_INT (user), g);
+ }
+
+#ifdef HAVE_GETGRGID_R
+ g_free (fbuf);
+#endif
+
+#endif /* PLATFORM_WIN32 */
+
+ return result;
+}
+
+
+gboolean
+ves_icall_System_Security_Principal_WindowsPrincipal_IsMemberOfGroupName (gpointer user, MonoString *group)
+{
+ gboolean result = FALSE;
+
+#ifdef PLATFORM_WIN32
+
+ MONO_ARCH_SAVE_REGS;
+
+ /* Windows version use a cache built using WindowsIdentity._GetRoles */
+ g_warning ("IsMemberOfGroupName should never be called on Win32");
+
+#else /* PLATFORM_WIN32 */
+ gchar *utf8_groupname;
+
+ MONO_ARCH_SAVE_REGS;
+
+ utf8_groupname = mono_unicode_to_external (mono_string_chars (group));
+ if (utf8_groupname) {
+ struct group *g = NULL;
+#ifdef HAVE_GETGRNAM_R
+ struct group grp;
+ gchar *fbuf;
+ gint32 retval;
+#ifdef _SC_GETGR_R_SIZE_MAX
+ size_t fbufsize = mono_sysconf (_SC_GETGR_R_SIZE_MAX);
+#else
+ size_t fbufsize = MONO_SYSCONF_DEFAULT_SIZE;
+#endif
+ fbuf = g_malloc0 (fbufsize);
+ retval = getgrnam_r (utf8_groupname, &grp, fbuf, fbufsize, &g);
+ result = ((retval == 0) && (g == &grp));
+#else
+ /* default to non thread-safe but posix compliant function */
+ g = getgrnam (utf8_groupname);
+ result = (g != NULL);
+#endif
+
+ if (result) {
+ result = IsMemberOf ((uid_t) GPOINTER_TO_INT (user), g);
+ }
+
+#ifdef HAVE_GETGRNAM_R
+ g_free (fbuf);
+#endif
+ g_free (utf8_groupname);
+ }
+#endif /* PLATFORM_WIN32 */
+
+ return result;
+}
+
+
+/* Mono.Security.Cryptography IO related internal calls */
+
+#ifdef PLATFORM_WIN32
+
+static PSID
+GetAdministratorsSid (void)
+{
+ SID_IDENTIFIER_AUTHORITY admins = SECURITY_NT_AUTHORITY;
+ PSID pSid = NULL;
+ if (!AllocateAndInitializeSid (&admins, 2, SECURITY_BUILTIN_DOMAIN_RID,
+ DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0, &pSid))
+ return NULL;
+ /* Note: this SID must be freed with FreeSid () */
+ return pSid;
+}
+
+
+static PSID
+GetEveryoneSid (void)
+{
+ SID_IDENTIFIER_AUTHORITY everyone = SECURITY_WORLD_SID_AUTHORITY;
+ PSID pSid = NULL;
+ if (!AllocateAndInitializeSid (&everyone, 1, SECURITY_WORLD_RID, 0, 0, 0, 0, 0, 0, 0, &pSid))
+ return NULL;
+ /* Note: this SID must be freed with FreeSid () */
+ return pSid;
+}
+
+
+static PSID
+GetCurrentUserSid (void)
+{
+ PSID sid = NULL;
+ guint32 size = 0;
+ gpointer token = ves_icall_System_Security_Principal_WindowsIdentity_GetCurrentToken ();
+
+ GetTokenInformation (token, TokenUser, NULL, size, (PDWORD)&size);
+ if (size > 0) {
+ TOKEN_USER *tu = g_malloc0 (size);
+ if (GetTokenInformation (token, TokenUser, tu, size, (PDWORD)&size)) {
+ DWORD length = GetLengthSid (tu->User.Sid);
+ sid = (PSID) g_malloc0 (length);
+ if (!CopySid (length, sid, tu->User.Sid)) {
+ g_free (sid);
+ sid = NULL;
+ }
+ }
+ g_free (tu);
+ }
+ /* Note: this SID must be freed with g_free () */
+ return sid;
+}
+
+
+static ACCESS_MASK
+GetRightsFromSid (PSID sid, PACL acl)
+{
+ ACCESS_MASK rights = 0;
+ TRUSTEE trustee;
+
+ BuildTrusteeWithSidW (&trustee, sid);
+ if (GetEffectiveRightsFromAcl (acl, &trustee, &rights) != ERROR_SUCCESS)
+ return 0;
+
+ return rights;
+}
+
+
+static gboolean
+IsMachineProtected (gunichar2 *path)
+{
+ gboolean success = FALSE;
+ PACL pDACL = NULL;
+ PSID pEveryoneSid = NULL;
+
+ DWORD dwRes = GetNamedSecurityInfoW (path, SE_FILE_OBJECT, DACL_SECURITY_INFORMATION, NULL, NULL, &pDACL, NULL, NULL);
+ if (dwRes != ERROR_SUCCESS)
+ return FALSE;
+
+ /* We check that Everyone is still limited to READ-ONLY -
+ but not if new entries have been added by an Administrator */
+
+ pEveryoneSid = GetEveryoneSid ();
+ if (pEveryoneSid) {
+ ACCESS_MASK rights = GetRightsFromSid (pEveryoneSid, pDACL);
+ /* http://msdn.microsoft.com/library/en-us/security/security/generic_access_rights.asp?frame=true */
+ success = (rights == (READ_CONTROL | SYNCHRONIZE | FILE_READ_DATA | FILE_READ_EA | FILE_READ_ATTRIBUTES));
+ FreeSid (pEveryoneSid);
+ }
+ /* Note: we don't need to check our own access -
+ we'll know soon enough when reading the file */
+
+ if (pDACL)
+ LocalFree (pDACL);
+
+ return success;
+}
+
+
+static gboolean
+IsUserProtected (gunichar2 *path)
+{
+ gboolean success = FALSE;
+ PACL pDACL = NULL;
+ PSID pEveryoneSid = NULL;
+
+ DWORD dwRes = GetNamedSecurityInfoW (path, SE_FILE_OBJECT,
+ DACL_SECURITY_INFORMATION, NULL, NULL, &pDACL, NULL, NULL);
+ if (dwRes != ERROR_SUCCESS)
+ return FALSE;
+
+ /* We check that our original entries in the ACL are in place -
+ but not if new entries have been added by the user */
+
+ /* Everyone should be denied */
+ pEveryoneSid = GetEveryoneSid ();
+ if (pEveryoneSid) {
+ ACCESS_MASK rights = GetRightsFromSid (pEveryoneSid, pDACL);
+ success = (rights == 0);
+ FreeSid (pEveryoneSid);
+ }
+ /* Note: we don't need to check our own access -
+ we'll know soon enough when reading the file */
+
+ if (pDACL)
+ LocalFree (pDACL);
+
+ return success;
+}
+
+
+static gboolean
+ProtectMachine (gunichar2 *path)
+{
+ PSID pEveryoneSid = GetEveryoneSid ();
+ PSID pAdminsSid = GetAdministratorsSid ();
+ DWORD retval = -1;
+
+ if (pEveryoneSid && pAdminsSid) {
+ PACL pDACL = NULL;
+ EXPLICIT_ACCESS ea [2];
+ ZeroMemory (&ea, 2 * sizeof (EXPLICIT_ACCESS));
+
+ /* grant all access to the BUILTIN\Administrators group */
+ BuildTrusteeWithSidW (&ea [0].Trustee, pAdminsSid);
+ ea [0].grfAccessPermissions = GENERIC_ALL;
+ ea [0].grfAccessMode = SET_ACCESS;
+ ea [0].grfInheritance = SUB_CONTAINERS_AND_OBJECTS_INHERIT;
+ ea [0].Trustee.TrusteeForm = TRUSTEE_IS_SID;
+ ea [0].Trustee.TrusteeType = TRUSTEE_IS_WELL_KNOWN_GROUP;
+
+ /* read-only access everyone */
+ BuildTrusteeWithSidW (&ea [1].Trustee, pEveryoneSid);
+ ea [1].grfAccessPermissions = GENERIC_READ;
+ ea [1].grfAccessMode = SET_ACCESS;
+ ea [1].grfInheritance = SUB_CONTAINERS_AND_OBJECTS_INHERIT;
+ ea [1].Trustee.TrusteeForm = TRUSTEE_IS_SID;
+ ea [1].Trustee.TrusteeType = TRUSTEE_IS_WELL_KNOWN_GROUP;
+
+ retval = SetEntriesInAcl (2, ea, NULL, &pDACL);
+ if (retval == ERROR_SUCCESS) {
+ /* with PROTECTED_DACL_SECURITY_INFORMATION we */
+ /* remove any existing ACL (like inherited ones) */
+ retval = SetNamedSecurityInfo (path, SE_FILE_OBJECT,
+ DACL_SECURITY_INFORMATION | PROTECTED_DACL_SECURITY_INFORMATION,
+ NULL, NULL, pDACL, NULL);
+ }
+ if (pDACL)
+ LocalFree (pDACL);
+ }
+
+ if (pEveryoneSid)
+ FreeSid (pEveryoneSid);
+ if (pAdminsSid)
+ FreeSid (pAdminsSid);
+ return (retval == ERROR_SUCCESS);
+}
+
+
+static gboolean
+ProtectUser (gunichar2 *path)
+{
+ DWORD retval = -1;
+
+ PSID pCurrentSid = GetCurrentUserSid ();
+ if (pCurrentSid) {
+ PACL pDACL = NULL;
+ EXPLICIT_ACCESS ea;
+ ZeroMemory (&ea, sizeof (EXPLICIT_ACCESS));
+
+ /* grant exclusive access to the current user */
+ BuildTrusteeWithSidW (&ea.Trustee, pCurrentSid);
+ ea.grfAccessPermissions = GENERIC_ALL;
+ ea.grfAccessMode = SET_ACCESS;
+ ea.grfInheritance = SUB_CONTAINERS_AND_OBJECTS_INHERIT;
+ ea.Trustee.TrusteeForm = TRUSTEE_IS_SID;
+ ea.Trustee.TrusteeType = TRUSTEE_IS_USER;
+
+ retval = SetEntriesInAcl (1, &ea, NULL, &pDACL);
+ if (retval == ERROR_SUCCESS) {
+ /* with PROTECTED_DACL_SECURITY_INFORMATION we
+ remove any existing ACL (like inherited ones) */
+ retval = SetNamedSecurityInfo (path, SE_FILE_OBJECT,
+ DACL_SECURITY_INFORMATION | PROTECTED_DACL_SECURITY_INFORMATION,
+ NULL, NULL, pDACL, NULL);
+ }
+
+ if (pDACL)
+ LocalFree (pDACL);
+ g_free (pCurrentSid); /* g_malloc0 */