First well-working version.
[platform/core/security/ima-evm-reference-utils.git] / src / im-get-policy.c
1 /**
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *    http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16 /*
17  * @file        im-get-policy.c
18  * @author      Janusz Kozerski (j.kozerski@samsung.com)
19  * @version     1.0
20  * @brief
21  */
22
23 #include "im-uigadget.h"
24 #include "im-common.h"
25 #include "im-get-policy.h"
26
27 /************************ Genlist Item Class ************************/
28 static void _gl_del(void *data, Evas_Object *obj)
29 {
30     // Cleaning formated policy string
31     (void)obj;
32     if (data)
33         free(data);
34 }
35
36 static char * _item_label_get(void *data, Evas_Object *obj, const char *part)
37 {
38     (void)obj;
39     (void)part;
40     return strdup(data);
41 }
42
43 // Needed because of GCC "missing initializer" error
44 #pragma GCC diagnostic ignored "-Wmissing-field-initializers"
45 static Elm_Genlist_Item_Class _itc_policy_content = {
46         .item_style       = "multiline/1text",
47         .func.text_get    = _item_label_get,
48         .func.content_get = NULL,
49         .func.state_get   = NULL,
50         .func.del         = _gl_del
51 };
52 #pragma GCC diagnostic pop // Restoring GCC command line parameters
53 /********************************************************************/
54
55 // This function will format policy to EFL usable string ("<br>" as a new line
56 // sign). You have to free() the memory that's allocated by this function.
57 static char* _convert_policy (const char *const *const policy)
58 {
59     LOGD("Enter function: %s", __func__);
60     char *new_policy = NULL;
61     char *tmp = NULL;
62     const char* const new_line_sign = "<br>";
63     size_t nls_size = strlen(new_line_sign);
64     size_t size = 0;
65     int i = 0;
66
67     // Counting size for policy rules
68     while (policy[i] != NULL) {
69         size += strlen(policy[i]);
70         ++i;
71     }
72     // Size of new line signs after every policy rule except the last one.
73     size += nls_size * (i - 1);
74
75     // NULL sign at the end
76     size += 1;
77
78     new_policy = malloc(size * sizeof(char));
79     if (!new_policy)
80         return NULL;
81
82     i = 0;
83     tmp = new_policy;
84     while (policy[i] != NULL) {
85         LOGD("Policy rule no. %d: %s", i, policy[i]);
86         strcpy(tmp, policy[i]);
87         tmp += strlen(policy[i]);
88         if (policy[i+1] != NULL) { //Appending new_line_sign only if it's not the last rule
89             strcpy(tmp, new_line_sign);
90             tmp += nls_size;
91         }
92         ++i;
93     }
94     new_policy[size] = '\0';
95
96     LOGD("Formated Policy: %s", new_policy);
97     return new_policy;
98 }
99
100 void im_get_policy_cb(void *data, Evas_Object *obj, void *event_info)
101 {
102     LOGD("Enter function: %s", __func__);
103     (void)obj;
104     (void)event_info;
105
106     struct ug_data *ad = (struct ug_data*) data;
107     int i;
108     Evas_Object *genlist = NULL;
109     Elm_Object_Item *nf_it = NULL;
110     char **policy = NULL;
111     char *formated_policy = NULL;
112
113     // TODO: Call function from libIMA to get currently loaded policy
114     // int res = ima_get_ima_policy(&policy);
115     // Check error code: if (res) {return;}
116     // Temporary allocation - will be removed when function from libIMA will be used
117     policy = calloc(4, sizeof(char*));
118     policy[0] = strdup("MEASURE something");
119     policy[1] = strdup("MEASUER something else");
120     policy[2] = strdup("DONT_MEASURE file");
121     // ------------
122
123     formated_policy = _convert_policy((const char *const *const) policy);
124     i = 0;
125     while (policy[i] != NULL) {
126         free(policy[i]);
127         policy[i] = NULL;
128         ++i;
129     }
130     free(policy);
131     policy = NULL;
132
133     if (!formated_policy)
134         return;
135
136     genlist = elm_genlist_add(ad->navi_bar);
137     if (!genlist)
138         return;
139
140     elm_genlist_mode_set(genlist, ELM_LIST_COMPRESS);
141     evas_object_smart_callback_add(genlist, "selected", genlist_clicked_cb, NULL);
142
143     nf_it = elm_genlist_item_append(
144             genlist,               // Genlist object
145             &_itc_policy_content,  // Item class
146             formated_policy,       // Item data
147             NULL,                  // Parrent
148             ELM_GENLIST_ITEM_NONE, // Item type
149             genlist_clicked_cb,    // select callback
150             NULL);                 // callback data
151     elm_genlist_item_select_mode_set(nf_it, ELM_OBJECT_SELECT_MODE_DISPLAY_ONLY);
152
153     nf_it = elm_naviframe_item_push(ad->navi_bar, dgettext(PACKAGE, "IDS_ST_CURRENT_POLICY"), NULL, NULL, genlist, NULL);
154     elm_object_item_domain_text_translatable_set(nf_it, PACKAGE, EINA_TRUE);
155
156     elm_naviframe_prev_btn_auto_pushed_set(ad->navi_bar, EINA_FALSE);
157     elm_naviframe_item_pop_cb_set(nf_it, NULL, NULL);
158
159     ea_object_event_callback_add(ad->navi_bar, EA_CALLBACK_BACK, ea_naviframe_back_cb, NULL);
160     ea_object_event_callback_add(ad->navi_bar, EA_CALLBACK_MORE, ea_naviframe_more_cb, NULL);
161 }