Migrate from 2.4 code repo
[platform/core/context/context-service.git] / src / access_control / config_loader.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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 #include <libxml/xmlmemory.h>
18 #include <libxml/parser.h>
19
20 #include <types_internal.h>
21
22 #include "privilege.h"
23 #include "config_loader.h"
24
25 #define CONFIG_PATH "/opt/data/context-service/access-config.xml"
26 #define ELM_ROOT "ContextAccessConfig"
27 #define ELM_PRIV "Privilege"
28 #define ELM_LINK "Link"
29 #define ELM_ALLOW "Allow"
30 #define ATTR_NAME "name"
31 #define ATTR_SUBJ "subject"
32 #define IS_NODE_OF(node, element) (xmlStrcmp((node)->name, (const xmlChar *)(element)) == 0)
33
34 static void parse_priv_node(xmlNodePtr cursor)
35 {
36         char *prop = (char*)(xmlGetProp(cursor, (const xmlChar*)ATTR_NAME));
37         IF_FAIL_VOID_TAG(prop, _E, "Failed to get '%s'", ATTR_NAME);
38
39         std::string name = prop;
40         free(prop);
41
42         cursor = cursor->xmlChildrenNode;
43         IF_FAIL_VOID_TAG(cursor, _E, "No child node exists");
44
45         while (cursor) {
46                 if (!IS_NODE_OF(cursor, ELM_ALLOW)) {
47                         _D("Skipping a node '%s'", (const char*)cursor->name);
48                         cursor = cursor->next;
49                         continue;
50                 }
51
52                 prop = (char*)(xmlGetProp(cursor, (const xmlChar*)ATTR_SUBJ));
53                 if (prop == NULL) {
54                         _E("Failed to get '%s'", ATTR_SUBJ);
55                         cursor = cursor->next;
56                         continue;
57                 }
58
59                 _SI("Set Privilege: %s <- %s", prop, name.c_str());
60                 ctx::privilege_manager::set(prop, name.c_str());
61
62                 free(prop);
63                 cursor = cursor->next;
64         }
65 }
66
67 static void parse_link_node(xmlNodePtr cursor)
68 {
69         //TODO: Not supported yet
70         _D("Skipping a link node\n");
71 }
72
73 bool ctx::access_config_loader::load()
74 {
75         xmlDocPtr doc = xmlParseFile(CONFIG_PATH);
76         IF_FAIL_RETURN_TAG(doc, false, _E, "XML parsing failed");
77
78         xmlNodePtr cursor = xmlDocGetRootElement(doc);
79         IF_FAIL_CATCH_TAG(cursor, _E, "No root node");
80         IF_FAIL_CATCH_TAG(IS_NODE_OF(cursor, ELM_ROOT), _E, "Invalid root node");
81
82         cursor = cursor->xmlChildrenNode;
83
84         while (cursor) {
85                 if (IS_NODE_OF(cursor, ELM_PRIV)) {
86                         parse_priv_node(cursor);
87                 } else if (IS_NODE_OF(cursor, ELM_LINK)) {
88                         parse_link_node(cursor);
89                 }
90
91                 cursor = cursor->next;
92         }
93
94         xmlFreeDoc(doc);
95         return true;
96
97 CATCH:
98         xmlFreeDoc(doc);
99         return false;
100 }