Call setguid function with the APP group ID retreives from /etc/group through getgrou... 92/5892/4 accepted/tizen/20130730.200453 submit/tizen/20130730.080821
authorBaptiste DURAND <baptiste.durand@eurogiciel.fr>
Tue, 23 Jul 2013 17:37:21 +0000 (19:37 +0200)
committerBaptiste DURAND <baptiste.durand@eurogiciel.fr>
Tue, 23 Jul 2013 17:37:21 +0000 (19:37 +0200)
packaging/libprivilege-control.changes
src/privilege-control.c

index 94ac09f..878a61e 100644 (file)
@@ -1,3 +1,6 @@
+* Tue Jul 23 2013 Baptiste DURAND <baptiste.durand@eurogiciel.fr> accepted/tizen/20130711.205247@88318d6
+- Call setguid function with the APP group ID retreives from /etc/group through getgrouplists function call.
+
 * Thu Jul 11 2013 Rusty Lynch <rusty.lynch@intel.com> accepted/tizen/20130711.163636@43b59bc
 - Before applied a label on the process context, check if the label is previously set.
 
index a361968..433ee2b 100644 (file)
@@ -205,14 +205,45 @@ API int control_privilege(void)
                return PC_ERR_NOT_PERMITTED;
 }
 
+
+
+static int get_user_groups(uid_t user_id, int *nbgroup, gid_t **groups_list)
+{
+       gid_t *groups = NULL;
+       struct passwd * pw;
+       C_LOGD("Enter function: %s", __func__);
+
+       if ((!groups_list) || (!nbgroup))
+               return PC_ERR_INVALID_OPERATION;
+       pw = getpwuid(user_id);
+       if(!pw) {
+               C_LOGE("getgrouplist fails : Invalid User ID %d",user_id);
+               return PC_ERR_INVALID_OPERATION;
+       }
+       *nbgroup = 0;
+       //First call is done with *ngroup = 0 to get the number of groups found for the user (Usefull for next malloc operation). It should return -1 in this case.
+       if (getgrouplist(pw->pw_name,  pw->pw_gid, groups, nbgroup) != -1)
+               return PC_ERR_INVALID_OPERATION;
+
+       C_LOGD("getgrouplist %s user is member of %d groups ",pw->pw_name,*nbgroup);
+       groups = malloc(*nbgroup * sizeof (gid_t));
+       if (!groups)
+               return PC_ERR_INVALID_OPERATION;
+       //Second call is done with the suitable ngroup value and structure groups allocated.
+       if (getgrouplist(pw->pw_name,  pw->pw_gid, groups, nbgroup) == -1) {
+               free(groups);
+               C_LOGE("getgrouplist fails %d",nbgroup);
+               return PC_ERR_INVALID_OPERATION;
+       }
+       *groups_list = groups;
+       return  PC_OPERATION_SUCCESS;
+}
+
 static int set_dac(const char *smack_label, const char *pkg_name)
 {
        C_LOGD("Enter function: %s", __func__);
-       FILE* fp_group = NULL;  // /etc/group
        uid_t t_uid = -1;               // uid of current process
        gid_t *glist = NULL;    // group list
-       gid_t temp_gid = -1;    // for group list
-       char buf[10] = {0, };           // contents in group_list file
        int glist_cnt = 0;              // for group list
        int result;
        int i;
@@ -248,44 +279,16 @@ static int set_dac(const char *smack_label, const char *pkg_name)
                        usr.uid = APP_UID;
                        usr.gid = APP_GID;
                        strncpy(usr.home_dir, APP_HOME_DIR, sizeof(usr.home_dir));
-                       strncpy(usr.group_list, APP_GROUP_PATH, sizeof(usr.group_list));
                }
 
                /*
                 * get group information
                 */
                C_LOGD("get group information");
-               if(!(fp_group = fopen(usr.group_list, "r")))
-               {
-                       C_LOGE("[ERR] file open error: [%s]\n", usr.group_list);
+               if (get_user_groups(usr.uid, &glist_cnt, &glist)) {
                        result = PC_ERR_FILE_OPERATION; // return -1
                        goto error;
                }
-
-               while(fgets(buf, 10, fp_group) != NULL)
-               {
-                       errno = 0;
-                       temp_gid = strtoul(buf, 0, 10);
-                       if(errno != 0)  // error occured during strtoul()
-                       {
-                               C_LOGE("[ERR] cannot change string to integer: [%s]", buf);
-                               result = PC_ERR_INVALID_OPERATION;
-                               goto error;
-                       }
-
-                       glist = (gid_t*)realloc(glist, sizeof(gid_t) * (glist_cnt + 1));
-                       if(!glist)
-                       {
-                               result = PC_ERR_MEM_OPERATION;  // return -2
-                               C_LOGE("Cannot allocate memory");
-                               goto error;
-                       }
-                       glist[glist_cnt] = temp_gid;
-                       glist_cnt++;
-               }
-               fclose(fp_group);
-               fp_group = NULL;
-
                {
                        gid_t *glist_new;
                        int i, cnt;
@@ -370,8 +373,6 @@ static int set_dac(const char *smack_label, const char *pkg_name)
        result = PC_OPERATION_SUCCESS;
 
 error:
-       if(fp_group != NULL)
-               fclose(fp_group);
        if(glist != NULL)
                free(glist);
        free(additional_gids);