gpointer ves_icall_System_Security_Principal_WindowsIdentity_GetUserToken (MonoString *username) { #ifdef HOST_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 /* HOST_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; }
gboolean ves_icall_System_Security_Principal_WindowsPrincipal_IsMemberOfGroupName (gpointer user, MonoString *group) { gboolean result = FALSE; #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 /* HOST_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 /* HOST_WIN32 */ return result; }
gboolean ves_icall_System_Security_Principal_WindowsPrincipal_IsMemberOfGroupId (gpointer user, gpointer group) { gboolean result = FALSE; #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 /* HOST_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 /* HOST_WIN32 */ return result; }
gpointer ves_icall_System_Security_Principal_WindowsIdentity_GetUserToken (MonoStringHandle username, MonoError *error) { gpointer token = (gpointer)-2; error_init (error); #if defined (HAVE_PWD_H) && !defined (HOST_WASM) #ifdef HAVE_GETPWNAM_R struct passwd pwd; size_t fbufsize; gchar *fbuf; gint32 retval; #endif struct passwd *p; gchar *utf8_name; gboolean result; utf8_name = mono_string_handle_to_utf8 (username, error); return_val_if_nok (error, NULL); #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 = (gchar *)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 /* HAVE_PWD_H */ return token; }
static gchar* GetTokenName (uid_t uid) { gchar *uname = NULL; #ifdef HAVE_PWD_H #ifdef HAVE_GETPWUID_R struct passwd pwd; size_t fbufsize; gchar *fbuf; gint32 retval; #endif struct passwd *p = NULL; gboolean result; #ifdef HAVE_GETPWUID_R #ifdef _SC_GETPW_R_SIZE_MAX fbufsize = mono_sysconf (_SC_GETPW_R_SIZE_MAX); #else fbufsize = MONO_SYSCONF_DEFAULT_SIZE; #endif fbuf = (gchar *)g_malloc0 (fbufsize); retval = getpwuid_r (uid, &pwd, fbuf, fbufsize, &p); result = ((retval == 0) && (p == &pwd)); #else /* default to non thread-safe but posix compliant function */ p = getpwuid (uid); result = (p != NULL); #endif if (result) { uname = g_strdup (p->pw_name); } #ifdef HAVE_GETPWUID_R g_free (fbuf); #endif #endif /* HAVE_PWD_H */ return uname; }
static gboolean IsDefaultGroup (uid_t user, gid_t group) { gboolean result = FALSE; #ifdef HAVE_PWD_H #ifdef HAVE_GETPWUID_R struct passwd pwd; size_t fbufsize; gchar *fbuf; gint32 retval; #endif struct passwd *p = NULL; #ifdef HAVE_GETPWUID_R #ifdef _SC_GETPW_R_SIZE_MAX fbufsize = mono_sysconf (_SC_GETPW_R_SIZE_MAX); #else fbufsize = MONO_SYSCONF_DEFAULT_SIZE; #endif fbuf = (gchar *)g_malloc0 (fbufsize); retval = getpwuid_r (user, &pwd, fbuf, fbufsize, &p); result = ((retval == 0) && (p == &pwd)); #else /* default to non thread-safe but posix compliant function */ p = getpwuid (user); result = (p != NULL); #endif if (result) { result = (p->pw_gid == group); } #ifdef HAVE_GETPWUID_R g_free (fbuf); #endif #endif /* HAVE_PWD_H */ return result; }
MonoBoolean ves_icall_System_Security_Principal_WindowsPrincipal_IsMemberOfGroupId (gpointer user, gpointer group, MonoError *error) { gboolean result = FALSE; #ifdef HAVE_GRP_H #ifdef HAVE_GETGRGID_R struct group grp; size_t fbufsize; gchar *fbuf; gint32 retval; #endif struct group *g = NULL; #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 = (gchar *)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 /* HAVE_GRP_H */ return result; }