Flush (work in progress)
[mono.git] / mono / io-layer / security.c
index 4f736067d083650e6c108428a3ad44f12f071187..1d702d3188f274c02c319c8f3d59bc805d9ccbf4 100644 (file)
@@ -7,40 +7,60 @@
  * (C) 2004 Novell (http://www.novell.com)
  */
 
+#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>
 
 
-extern gboolean GetUserName (gchar *buffer, gint32 *size) 
+/* 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
+
+
+gboolean 
+ImpersonateLoggedOnUser (gpointer handle)
 {
-       struct passwd *p;
-       uid_t uid;
-
-       if (!size) {
-               SetLastError (ERROR_INVALID_PARAMETER);
-               return 0;
-       }
-
-       uid = getuid ();
-       p = getpwuid (uid);
-       if (p) {
-               gint32 sz = strlen (p->pw_name);
-               if (buffer) {
-                       if (sz > *size)
-                               sz = *size;
-                       strncpy (buffer, p->pw_name, sz);
-               }
-               *size = sz;
-               return 1;
-       }
-
-       // note: getpwuid return static data - no free here
-       *size = 0;
-       SetLastError (ERROR_INVALID_HANDLE);
-       return 0;
+       uid_t token = (uid_t) GPOINTER_TO_INT (handle);
+#ifdef HAVE_SETRESUID
+       if (setresuid (-1, token, getuid ()) < 0)
+               return FALSE;
+#endif
+       return (geteuid () == token);
+}
+
+
+gboolean RevertToSelf (void)
+{
+#ifdef HAVE_GETRESUID
+       uid_t ruid, euid;
+#endif
+       uid_t suid = -1;
+
+#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
+       return (geteuid () == suid);
 }