Revert "add tzplatform_has_users_group() api"
[platform/core/system/tizen-platform-wrapper.git] / src / isadmin.c
1 /*
2  * Copyright (C) 2013 Intel Corporation.
3  * 
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
17  *
18  * Authors:
19  *   Stephen Clymans <stephen.clymans@open.eurogiciel.org>
20  */
21  
22  /*
23  * DISCLAIMER :
24  *  This source file and its associated header are present to maintain a 
25  * temporary solution. We need to know if an user have the privilege for
26  * a particular action.
27  * 
28  * At the end, this feature will be managed by Cynara
29  * 
30  */
31
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <sys/types.h>
35 #include <unistd.h>
36 #include <grp.h>
37 #include <pwd.h>
38
39 #include "isadmin.h"
40 #include "tzplatform_variables.h"
41 #include "tzplatform_config.h"
42
43 int _has_system_group_static_(uid_t uid) {
44         
45         struct passwd *userinfo = NULL;
46         struct group *systemgroupinfo = NULL;
47         const char *sysgrpname = NULL;
48         uid_t myuid = 0;
49         gid_t system_gid = 0;
50         gid_t *groups = NULL;
51         int i, nbgroups = 0;
52         
53         
54         if(uid == -1)
55                 /* Get current uid */
56                 myuid = getuid();
57         else
58                 myuid = uid;
59         
60         /* Get the gid of the group named "system" */
61         sysgrpname = tzplatform_getname(TZ_SYS_ADMIN_GROUP);
62         if(sysgrpname == NULL) {
63                 fprintf( stderr, "isadmin ERROR: variable TZ_SYS_ADMIN_GROUP is NULL");
64                 return -1;
65         }
66         systemgroupinfo = getgrnam(sysgrpname);
67         if(systemgroupinfo == NULL) {
68                 fprintf( stderr, "isadmin ERROR: cannot find group named \"%s\"\n", sysgrpname);
69                 return -1;
70         }
71         
72         system_gid = systemgroupinfo->gr_gid;
73         
74         /* Get all the gid of the given uid */
75         
76         userinfo = getpwuid(myuid);
77         
78         /* Need to call this function now to get the number of group to make the
79            malloc correctly sized */
80         if (getgrouplist(userinfo->pw_name, userinfo->pw_gid, groups, &nbgroups) != -1) {
81                 fprintf( stderr, "isadmin ERROR: cannot get number of groups\n");
82                 return -1;
83         }
84         
85         groups = malloc(nbgroups * sizeof (gid_t));
86         if (groups == NULL) {
87                 fprintf( stderr, "isadmin ERROR: malloc cannot allocate memory\n");
88                 return -1;
89         }
90         
91         if (getgrouplist(userinfo->pw_name, userinfo->pw_gid, groups, &nbgroups) == -1) {
92                 free(groups);
93                 fprintf( stderr, "isadmin ERROR: cannot get groups\n");
94                 return -1;
95         }
96         
97         /* Check if the given uid is in the system group */
98         
99         for(i = 0 ; i < nbgroups ; i++) {
100                 if(groups[i] == system_gid) {
101                         free(groups);
102                         return 1;
103                 }
104         }
105         free(groups);
106         return 0;
107 }
108