Flush (work in progress)
[mono.git] / mono / io-layer / security.c
index e2135cf893ba9d2f97f3c6e8b033667d75c6eff3..1d702d3188f274c02c319c8f3d59bc805d9ccbf4 100644 (file)
 #include <config.h>
 #include <mono/io-layer/io-layer.h>
 
+#ifdef HAVE_PWD_H
 #include <pwd.h>
+#endif
+
 #include <string.h>
 #include <sys/types.h>
+#include <sys/stat.h>
 #include <unistd.h>
 
 
-gboolean
-GetUserName (gchar *buffer, gint32 *size) 
-{
-#ifdef HAVE_GETPWUID_R
-       struct passwd *pbuf;
-       size_t fbufsize;
-       gchar *fbuf;
+/* Disclaimers */
+
+#if defined(__GNUC__)
+#ifndef HAVE_GETRESUID
+       #warning getresuid not supported. WindowsImpersonationContext wont work
+#endif
+#ifndef HAVE_SETRESUID
+       #warning setresuid not supported. WindowsImpersonationContext wont work
+#endif
 #endif
-       struct passwd *p;
-       uid_t uid;
 
-       if (!size) {
-               SetLastError (ERROR_INVALID_PARAMETER);
-               return FALSE;
-       }
 
-       uid = getuid ();
-#ifdef HAVE_GETPWUID_R
-#ifdef _SC_GETPW_R_SIZE_MAX
-       fbufsize = (size_t) sysconf (_SC_GETPW_R_SIZE_MAX);
-#else
-       fbufsize = (size_t) 1024;
+gboolean 
+ImpersonateLoggedOnUser (gpointer handle)
+{
+       uid_t token = (uid_t) GPOINTER_TO_INT (handle);
+#ifdef HAVE_SETRESUID
+       if (setresuid (-1, token, getuid ()) < 0)
+               return FALSE;
 #endif
-       
-       fbuf = g_malloc0 (fbufsize);
-       pbuf = g_new0 (struct passwd, 1);
-       getpwuid_r (uid, pbuf, fbuf, fbufsize, &p);
-#else
-       p = getpwuid (uid);
+       return (geteuid () == token);
+}
+
+
+gboolean RevertToSelf (void)
+{
+#ifdef HAVE_GETRESUID
+       uid_t ruid, euid;
 #endif
-       if (p) {
-               gint32 sz = strlen (p->pw_name);
-               if (buffer) {
-                       if (sz > *size)
-                               sz = *size;
-                       strncpy (buffer, p->pw_name, sz);
-               }
-               *size = sz;
-               return TRUE;
-       }
+       uid_t suid = -1;
 
-#ifdef HAVE_GETPWUID_R
-       g_free (pbuf);
-       g_free (fbuf);
+#ifdef HAVE_GETRESUID
+       if (getresuid (&ruid, &euid, &suid) < 0)
+               return FALSE;
+#endif
+#ifdef HAVE_SETRESUID
+       if (setresuid (-1, suid, -1) < 0)
+               return FALSE;
+#else
+       return TRUE;
 #endif
-       *size = 0;
-       SetLastError (ERROR_INVALID_HANDLE);
-       return FALSE;
+       return (geteuid () == suid);
 }