comparison common/util.c @ 644:c7a35fa302ec

Check sudo group membership if user to determine if he can elevate privileges
author Andre Heinecke <andre.heinecke@intevation.de>
date Tue, 24 Jun 2014 18:10:10 +0200
parents 78959fd970b0
children 175370634226
comparison
equal deleted inserted replaced
637:be30d50bc4f0 644:c7a35fa302ec
5 * and comes with ABSOLUTELY NO WARRANTY! 5 * and comes with ABSOLUTELY NO WARRANTY!
6 * See LICENSE.txt for details. 6 * See LICENSE.txt for details.
7 */ 7 */
8 #include "util.h" 8 #include "util.h"
9 #include "logging.h" 9 #include "logging.h"
10 #include "strhelp.h"
10 11
11 #ifndef _WIN32 12 #ifndef _WIN32
12 #include <unistd.h> 13 #include <unistd.h>
13 #include <sys/types.h> 14 #include <sys/types.h>
15 #include <pwd.h>
16 #include <grp.h>
17 #include <string.h>
14 #else 18 #else
15 #include <windows.h> 19 #include <windows.h>
16 #endif 20 #endif
17 21
18 bool 22 bool
40 } 44 }
41 45
42 bool is_admin() 46 bool is_admin()
43 { 47 {
44 #ifndef _WIN32 48 #ifndef _WIN32
45 /* TODO implement */ 49 struct passwd *current_user = getpwuid (geteuid());
50 int ngroups = 0,
51 ret = 0,
52 i = 0;
53 gid_t * groups = NULL;
54
55 if (current_user == NULL)
56 {
57 ERRORPRINTF ("Failed to obtain user information.");
58 return false;
59 }
60
61 ret = getgrouplist (current_user->pw_name, current_user->pw_gid, NULL,
62 &ngroups);
63
64 if (ret != -1 || ngroups <= 0)
65 {
66 ERRORPRINTF ("Unknown error in getgrouplist call");
67 return false;
68 }
69
70 groups = xmalloc (((unsigned int)ngroups) * sizeof (gid_t));
71
72 ret = getgrouplist (current_user->pw_name, current_user->pw_gid, groups,
73 &ngroups);
74
75 if (ret != ngroups)
76 {
77 ERRORPRINTF ("Group length mismatch.");
78 xfree (groups);
79 return false;
80 }
81
82 for (i = 0; i < ngroups; i++)
83 {
84 struct group *gr = getgrgid (groups[i]);
85 if (gr == NULL)
86 {
87 ERRORPRINTF ("Error in group enumeration");
88 xfree (groups);
89 return false;
90 }
91 if (strcmp("sudo", gr->gr_name) == 0)
92 {
93 DEBUGPRINTF ("User is in sudo group \n");
94 xfree (groups);
95 return true;
96 }
97 }
98
99 DEBUGPRINTF ("User is not in sudo group");
100
46 return false; 101 return false;
47 #else 102 #else
48 bool retval = false; 103 bool retval = false;
49 BOOL in_admin_group = FALSE; 104 BOOL in_admin_group = FALSE;
50 HANDLE hToken = NULL; 105 HANDLE hToken = NULL;

http://wald.intevation.org/projects/trustbridge/