Merge "add tzplatform_has_users_group() api" into tizen
[platform/core/system/tizen-platform-wrapper.git] / src / groups.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 "groups.h"
40 #include "tzplatform_config.h"
41
42 int _has_specified_group_static_(uid_t uid, enum tzplatform_variable group) {
43         
44         struct passwd *userinfo = NULL;
45         struct group *groupinfo = NULL;
46         const char *grpname = NULL;
47         uid_t myuid = 0;
48         gid_t gid = 0;
49         gid_t *groups = NULL;
50         int i, nbgroups = 0;
51         
52         if (group != TZ_SYS_USER_GROUP || group != TZ_SYS_ADMIN_GROUP) {
53                 fprintf( stderr, "groups ERROR: group is not valid \n");
54                 return -1;
55         }
56
57         if(uid == -1)
58                 /* Get current uid */
59                 myuid = getuid();
60         else
61                 myuid = uid;
62         
63         /* Get the gid of the group */
64         grpname = tzplatform_getname(group);
65         if(grpname == NULL) {
66                 fprintf( stderr, "groups ERROR: variable TZ_SYS_ADMIN_GROUP is NULL");
67                 return -1;
68         }
69         groupinfo = getgrnam(grpname);
70         if(groupinfo == NULL) {
71                 fprintf( stderr, "groups ERROR: cannot find group named \"%s\"\n", grpname);
72                 return -1;
73         }
74         
75         gid = groupinfo->gr_gid;
76         
77         /* Get all the gid of the given uid */
78         
79         userinfo = getpwuid(myuid);
80         
81         /* Need to call this function now to get the number of group to make the
82            malloc correctly sized */
83         if (getgrouplist(userinfo->pw_name, userinfo->pw_gid, groups, &nbgroups) != -1) {
84                 fprintf( stderr, "groups ERROR: cannot get number of groups\n");
85                 return -1;
86         }
87         
88         groups = malloc(nbgroups * sizeof (gid_t));
89         if (groups == NULL) {
90                 fprintf( stderr, "groups ERROR: malloc cannot allocate memory\n");
91                 return -1;
92         }
93         
94         if (getgrouplist(userinfo->pw_name, userinfo->pw_gid, groups, &nbgroups) == -1) {
95                 free(groups);
96                 fprintf( stderr, "groups ERROR: cannot get groups\n");
97                 return -1;
98         }
99         
100         /* Check if the given uid is in the specified group */
101         
102         for(i = 0 ; i < nbgroups ; i++) {
103                 if(groups[i] == gid) {
104                         free(groups);
105                         return 1;
106                 }
107         }
108         free(groups);
109         return 0;
110 }
111