* Author:
* Sebastien Pouliot <sebastien@ximian.com>
*
- * (C) 2004 Novell (http://www.novell.com)
+ * Copyright 2004-2009 Novell, Inc (http://www.novell.com)
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
+#include <mono/metadata/assembly.h>
#include <mono/metadata/appdomain.h>
+#include <mono/metadata/image.h>
#include <mono/metadata/exception.h>
+#include <mono/metadata/object-internals.h>
+#include <mono/metadata/metadata-internals.h>
#include <mono/metadata/security.h>
#include <mono/io-layer/io-layer.h>
#include <mono/utils/strenc.h>
-#ifdef PLATFORM_WIN32
+#ifdef HOST_WIN32
#include <aclapi.h>
#include <accctrl.h>
#endif /* defined(__GNUC__) */
-#endif /* not PLATFORM_WIN32 */
+#endif /* not HOST_WIN32 */
/* internal functions - reuse driven */
-#ifdef PLATFORM_WIN32
+#ifdef HOST_WIN32
/* ask a server to translate a SID into a textual representation */
static gunichar2*
}
-#else /* not PLATFORM_WIN32 */
+#else /* not HOST_WIN32 */
+
+#define MONO_SYSCONF_DEFAULT_SIZE ((size_t) 1024)
+
+/*
+ * Ensure we always get a valid (usable) value from sysconf.
+ * In case of error, we return the default value.
+ */
+static size_t mono_sysconf (int name)
+{
+ size_t size = (size_t) sysconf (name);
+ /* default value */
+ return (size == -1) ? MONO_SYSCONF_DEFAULT_SIZE : size;
+}
static gchar*
#ifdef HAVE_GETPWUID_R
#ifdef _SC_GETPW_R_SIZE_MAX
- fbufsize = (size_t) sysconf (_SC_GETPW_R_SIZE_MAX);
+ fbufsize = mono_sysconf (_SC_GETPW_R_SIZE_MAX);
#else
- fbufsize = (size_t) 1024;
+ fbufsize = MONO_SYSCONF_DEFAULT_SIZE;
#endif
fbuf = g_malloc0 (fbufsize);
retval = getpwuid_r (uid, &pwd, fbuf, fbufsize, &p);
#ifdef HAVE_GETPWUID_R
#ifdef _SC_GETPW_R_SIZE_MAX
- fbufsize = (size_t) sysconf (_SC_GETPW_R_SIZE_MAX);
+ fbufsize = mono_sysconf (_SC_GETPW_R_SIZE_MAX);
#else
- fbufsize = (size_t) 1024;
+ fbufsize = MONO_SYSCONF_DEFAULT_SIZE;
#endif
fbuf = g_malloc0 (fbufsize);
/* ICALLS */
-/* System.Environment */
-
-
-MonoString*
-ves_icall_System_Environment_get_UserName (void)
-{
- MONO_ARCH_SAVE_REGS;
-
- /* using glib is more portable */
- return mono_string_new (mono_domain_get (), g_get_user_name ());
-}
-
-
/* System.Security.Principal.WindowsIdentity */
MONO_ARCH_SAVE_REGS;
-#ifdef PLATFORM_WIN32
+#ifdef HOST_WIN32
/* Note: This isn't a copy of the Token - we must not close it!!!
* http://www.develop.com/kbrown/book/html/whatis_windowsprincipal.html
*/
OpenProcessToken (GetCurrentProcess (), TOKEN_QUERY, &token);
}
#else
- token = (gpointer) geteuid ();
+ token = GINT_TO_POINTER (geteuid ());
#endif
return token;
}
gunichar2 *uniname = NULL;
gint32 size = 0;
-#ifdef PLATFORM_WIN32
+#ifdef HOST_WIN32
MONO_ARCH_SAVE_REGS;
GetTokenInformation (token, TokenUser, NULL, size, (PDWORD)&size);
g_free (tu);
}
#else
- gchar *uname = GetTokenName ((uid_t) token);
+ gchar *uname = GetTokenName ((uid_t) GPOINTER_TO_INT (token));
MONO_ARCH_SAVE_REGS;
uniname = g_utf8_to_utf16 (uname, size, NULL, NULL, NULL);
g_free (uname);
}
-#endif /* PLATFORM_WIN32 */
+#endif /* HOST_WIN32 */
if (size > 0) {
result = mono_string_new_utf16 (mono_domain_get (), uniname, size);
gpointer
ves_icall_System_Security_Principal_WindowsIdentity_GetUserToken (MonoString *username)
{
-#ifdef PLATFORM_WIN32
+#ifdef HOST_WIN32
gpointer token = NULL;
MONO_ARCH_SAVE_REGS;
*/
g_warning ("Unsupported on Win32 (anyway requires W2K3 minimum)");
-#else /* PLATFORM_WIN32*/
+#else /* HOST_WIN32*/
#ifdef HAVE_GETPWNAM_R
struct passwd pwd;
#ifdef HAVE_GETPWNAM_R
#ifdef _SC_GETPW_R_SIZE_MAX
- fbufsize = (size_t) sysconf (_SC_GETPW_R_SIZE_MAX);
+ fbufsize = mono_sysconf (_SC_GETPW_R_SIZE_MAX);
#else
- fbufsize = (size_t) 1024;
+ fbufsize = MONO_SYSCONF_DEFAULT_SIZE;
#endif
fbuf = g_malloc0 (fbufsize);
#endif
if (result) {
- token = (gpointer) p->pw_uid;
+ token = GINT_TO_POINTER (p->pw_uid);
}
#ifdef HAVE_GETPWNAM_R
{
MonoArray *array = NULL;
MonoDomain *domain = mono_domain_get ();
-#ifdef PLATFORM_WIN32
+#ifdef HOST_WIN32
gint32 size = 0;
MONO_ARCH_SAVE_REGS;
if (uniname) {
MonoString *str = mono_string_new_utf16 (domain, uniname, size);
- mono_array_set (array, MonoString *, i, str);
+ mono_array_setref (array, i, str);
g_free (uniname);
}
}
MONO_ARCH_SAVE_REGS;
-#ifdef PLATFORM_WIN32
+#ifdef HOST_WIN32
result = (CloseHandle (token) != 0);
#endif
return result;
MONO_ARCH_SAVE_REGS;
-#ifdef PLATFORM_WIN32
+#ifdef HOST_WIN32
if (DuplicateToken (token, SecurityImpersonation, &dupe) == 0) {
dupe = NULL;
}
{
gboolean result = FALSE;
-#ifdef PLATFORM_WIN32
+#ifdef HOST_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 */
+#else /* HOST_WIN32 */
#ifdef HAVE_GETGRGID_R
struct group grp;
#ifdef HAVE_GETGRGID_R
#ifdef _SC_GETGR_R_SIZE_MAX
- fbufsize = (size_t) sysconf (_SC_GETGR_R_SIZE_MAX);
+ fbufsize = mono_sysconf (_SC_GETGR_R_SIZE_MAX);
#else
- fbufsize = (size_t) 1024;
+ fbufsize = MONO_SYSCONF_DEFAULT_SIZE;
#endif
fbuf = g_malloc0 (fbufsize);
- retval = getgrgid_r ((gid_t) group, &grp, fbuf, fbufsize, &g);
+ 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) group);
+ g = getgrgid ((gid_t) GPOINTER_TO_INT (group));
result = (g != NULL);
#endif
if (result) {
- result = IsMemberOf ((uid_t) user, g);
+ result = IsMemberOf ((uid_t) GPOINTER_TO_INT (user), g);
}
#ifdef HAVE_GETGRGID_R
g_free (fbuf);
#endif
-#endif /* PLATFORM_WIN32 */
+#endif /* HOST_WIN32 */
return result;
}
{
gboolean result = FALSE;
-#ifdef PLATFORM_WIN32
+#ifdef HOST_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 */
+#else /* HOST_WIN32 */
gchar *utf8_groupname;
MONO_ARCH_SAVE_REGS;
gchar *fbuf;
gint32 retval;
#ifdef _SC_GETGR_R_SIZE_MAX
- size_t fbufsize = (size_t) sysconf (_SC_GETGR_R_SIZE_MAX);
+ size_t fbufsize = mono_sysconf (_SC_GETGR_R_SIZE_MAX);
#else
- size_t fbufsize = (size_t) 1024;
+ size_t fbufsize = MONO_SYSCONF_DEFAULT_SIZE;
#endif
fbuf = g_malloc0 (fbufsize);
retval = getgrnam_r (utf8_groupname, &grp, fbuf, fbufsize, &g);
#endif
if (result) {
- result = IsMemberOf ((uid_t) user, g);
+ result = IsMemberOf ((uid_t) GPOINTER_TO_INT (user), g);
}
#ifdef HAVE_GETGRNAM_R
#endif
g_free (utf8_groupname);
}
-#endif /* PLATFORM_WIN32 */
+#endif /* HOST_WIN32 */
return result;
}
/* Mono.Security.Cryptography IO related internal calls */
-#ifdef PLATFORM_WIN32
+#ifdef HOST_WIN32
static PSID
GetAdministratorsSid (void)
return result;
}
-#endif /* not PLATFORM_WIN32 */
+#endif /* not HOST_WIN32 */
MonoBoolean
ves_icall_Mono_Security_Cryptography_KeyPairPersistence_CanSecure (MonoString *root)
{
-#if PLATFORM_WIN32
+#if HOST_WIN32
gint32 flags;
MONO_ARCH_SAVE_REGS;
MONO_ARCH_SAVE_REGS;
/* no one, but the owner, should have write access to the directory */
-#ifdef PLATFORM_WIN32
+#ifdef HOST_WIN32
ret = IsMachineProtected (mono_string_chars (path));
#else
ret = IsProtected (path, (S_IWGRP | S_IWOTH));
MONO_ARCH_SAVE_REGS;
/* no one, but the user, should have access to the directory */
-#ifdef PLATFORM_WIN32
+#ifdef HOST_WIN32
ret = IsUserProtected (mono_string_chars (path));
#else
ret = IsProtected (path, (S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IWOTH | S_IXOTH));
MONO_ARCH_SAVE_REGS;
/* read/write to owner, read to everyone else */
-#ifdef PLATFORM_WIN32
+#ifdef HOST_WIN32
ret = ProtectMachine (mono_string_chars (path));
#else
ret = Protect (path, (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH), (S_IXUSR | S_IXGRP | S_IXOTH));
MONO_ARCH_SAVE_REGS;
/* read/write to user, no access to everyone else */
-#ifdef PLATFORM_WIN32
+#ifdef HOST_WIN32
ret = ProtectUser (mono_string_chars (path));
#else
ret = Protect (path, (S_IRUSR | S_IWUSR), S_IXUSR);
#endif
return ret;
}
+
+
+/*
+ * Returns TRUE if there is "something" where the Authenticode signature is
+ * normally located. Returns FALSE is data directory is empty.
+ *
+ * Note: Neither the structure nor the signature is verified by this function.
+ */
+MonoBoolean
+ves_icall_System_Security_Policy_Evidence_IsAuthenticodePresent (MonoReflectionAssembly *refass)
+{
+ if (refass && refass->assembly && refass->assembly->image) {
+ return mono_image_has_authenticode_entry (refass->assembly->image);
+ }
+ return FALSE;
+}
+
+
+/* System.Security.SecureString related internal calls */
+
+static MonoImage *system_security_assembly = NULL;
+
+void
+ves_icall_System_Security_SecureString_DecryptInternal (MonoArray *data, MonoObject *scope)
+{
+ invoke_protected_memory_method (data, scope, FALSE);
+}
+void
+ves_icall_System_Security_SecureString_EncryptInternal (MonoArray* data, MonoObject *scope)
+{
+ invoke_protected_memory_method (data, scope, TRUE);
+}
+
+void invoke_protected_memory_method (MonoArray *data, MonoObject *scope, gboolean encrypt)
+{
+ MonoClass *klass;
+ MonoMethod *method;
+ void *params [2];
+
+ MONO_ARCH_SAVE_REGS;
+
+ if (system_security_assembly == NULL) {
+ system_security_assembly = mono_image_loaded ("System.Security");
+ if (!system_security_assembly) {
+ MonoAssembly *sa = mono_assembly_open ("System.Security.dll", NULL);
+ if (!sa)
+ g_assert_not_reached ();
+ system_security_assembly = mono_assembly_get_image (sa);
+ }
+ }
+
+ klass = mono_class_from_name (system_security_assembly,
+ "System.Security.Cryptography", "ProtectedMemory");
+ method = mono_class_get_method_from_name (klass, encrypt ? "Protect" : "Unprotect", 2);
+ params [0] = data;
+ params [1] = scope; /* MemoryProtectionScope.SameProcess */
+ mono_runtime_invoke (method, NULL, params, NULL);
+}