2010-01-07 Rodrigo Kumpera <rkumpera@novell.com>
[mono.git] / mono / io-layer / security.c
blob1d702d3188f274c02c319c8f3d59bc805d9ccbf4
1 /*
2 * security.c: Security
4 * Author:
5 * Sebastien Pouliot <sebastien@ximian.com>
7 * (C) 2004 Novell (http://www.novell.com)
8 */
10 #include <config.h>
11 #include <mono/io-layer/io-layer.h>
13 #ifdef HAVE_PWD_H
14 #include <pwd.h>
15 #endif
17 #include <string.h>
18 #include <sys/types.h>
19 #include <sys/stat.h>
20 #include <unistd.h>
23 /* Disclaimers */
25 #if defined(__GNUC__)
26 #ifndef HAVE_GETRESUID
27 #warning getresuid not supported. WindowsImpersonationContext wont work
28 #endif
29 #ifndef HAVE_SETRESUID
30 #warning setresuid not supported. WindowsImpersonationContext wont work
31 #endif
32 #endif
35 gboolean
36 ImpersonateLoggedOnUser (gpointer handle)
38 uid_t token = (uid_t) GPOINTER_TO_INT (handle);
39 #ifdef HAVE_SETRESUID
40 if (setresuid (-1, token, getuid ()) < 0)
41 return FALSE;
42 #endif
43 return (geteuid () == token);
47 gboolean RevertToSelf (void)
49 #ifdef HAVE_GETRESUID
50 uid_t ruid, euid;
51 #endif
52 uid_t suid = -1;
54 #ifdef HAVE_GETRESUID
55 if (getresuid (&ruid, &euid, &suid) < 0)
56 return FALSE;
57 #endif
58 #ifdef HAVE_SETRESUID
59 if (setresuid (-1, suid, -1) < 0)
60 return FALSE;
61 #else
62 return TRUE;
63 #endif
64 return (geteuid () == suid);