Fix xattr
[platform/core/security/ima-evm-reference-utils.git] / src / im-get-xattrs.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-xattrs.c
18  * @author      Janusz Kozerski (j.kozerski@samsung.com)
19  * @version     1.0
20  * @brief
21  */
22
23 #include <imaevm.h>
24
25 #include "im-common.h"
26 #include "im-uigadget.h"
27 #include "im-get-xattrs.h"
28
29 #define GET_XATTRS_DEFAULT_PATH   "/root/ima-test/"
30 #define MESSAGE_BEGIN            "IDS_ST_GET_XATTRS_MSG_BEGIN" // "Xattrs of:"
31 #define MESSAGE_IMA              ":<br>security.ima: "
32 #define MESSAGE_EVM              "<br>security.evm: "
33 #define FILE_SELECTOR_TITLE      "IDS_ST_GET_XATTRS_TITLE"
34
35 void im_get_xattrs_cb (void *data, Evas_Object *obj, void *event_info)
36 {
37     LOGD("Enter function: %s", __func__);
38     // Unused param warning
39     (void)obj;
40     (void)event_info;
41
42     struct ug_data *ad = (struct ug_data*) data;
43     ad->file_action = im_get_xattrs_wrapper;
44     ad->fileselector_default_path = GET_XATTRS_DEFAULT_PATH;
45
46     show_file_selector(ad, dgettext(PACKAGE, FILE_SELECTOR_TITLE));
47     // FIXME: For testing:
48     //im_get_xattrs_wrapper (ad, "/home/root/file");
49 }
50
51 static void im_print_xattr (char* xattr, char** out)
52 {
53     if (!xattr || !out)
54         return;
55
56     int i, j;
57     int len = strlen(xattr);
58     *out = calloc(sizeof(char), (len*2 + 1 ));
59
60     for (i = 0, j = 0; i < len; ++i) {
61         sprintf(&((*out)[j]), "%x", (unsigned char) xattr[i]);
62         if ((unsigned char) xattr[i] < 0x10)
63             j += 1;
64         else
65             j += 2;
66     }
67 }
68
69 int im_get_xattrs_wrapper (struct ug_data *ad, const char* const file_path)
70 {
71     LOGD("Enter function: %s", __func__);
72
73     int ret;
74     char* tmp = NULL;
75     char* ima_xattr = NULL;
76     char* evm_xattr = NULL;
77
78     ret = ima_get_xattr(file_path, &tmp);
79     if (ret != LIB_SUCCESS) {
80         LOGE("ima_get_xattr (%s) failed (%d)", file_path, ret);
81         goto out;
82     }
83     //LOGD("IMA xattr: %x", tmp);
84     im_print_xattr(tmp, &ima_xattr);
85     free(tmp);
86     tmp = NULL;
87
88     ret = evm_get_xattr(file_path, &tmp);
89     if (ret != LIB_SUCCESS) {
90         LOGE("evm_get_xattr (%s) failed (%d)", file_path, ret);
91         goto out;
92     }
93     //LOGD("EVM xattr: %x", tmp);
94     im_print_xattr(tmp, &evm_xattr);
95     free(tmp);
96     tmp = NULL;
97
98     size_t size =  strlen(dgettext(PACKAGE, "IDS_ST_GET_XATTRS_MSG_BEGIN")) +
99                    strlen(file_path)   +
100                    strlen(MESSAGE_IMA) +
101                    strlen(ima_xattr)   +
102                    strlen(MESSAGE_EVM) +
103                    strlen(evm_xattr)   + 1;  // Use asprintf?
104     ad->popup_content = malloc(sizeof(char) * size);  // One more char to add NULL at the end
105     snprintf(ad->popup_content, size, "%s%s%s%s%s%s",
106             dgettext(PACKAGE, "IDS_ST_GET_XATTRS_MSG_BEGIN"),
107             file_path,
108             MESSAGE_IMA,
109             ima_xattr,
110             MESSAGE_EVM,
111             evm_xattr);
112     ad->popup_content[size-1] = '\0';
113
114     create_ok_popup(ad);
115
116 out:
117     free(ima_xattr);
118     free(evm_xattr);
119     return 0;
120 }