2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <libxml/xmlmemory.h>
18 #include <libxml/parser.h>
20 #include <types_internal.h>
22 #include "privilege.h"
23 #include "config_loader.h"
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)
34 static void parse_priv_node(xmlNodePtr cursor)
36 char *prop = (char*)(xmlGetProp(cursor, (const xmlChar*)ATTR_NAME));
37 IF_FAIL_VOID_TAG(prop, _E, "Failed to get '%s'", ATTR_NAME);
39 std::string name = prop;
42 cursor = cursor->xmlChildrenNode;
43 IF_FAIL_VOID_TAG(cursor, _E, "No child node exists");
46 if (!IS_NODE_OF(cursor, ELM_ALLOW)) {
47 _D("Skipping a node '%s'", (const char*)cursor->name);
48 cursor = cursor->next;
52 prop = (char*)(xmlGetProp(cursor, (const xmlChar*)ATTR_SUBJ));
54 _E("Failed to get '%s'", ATTR_SUBJ);
55 cursor = cursor->next;
59 _SI("Set Privilege: %s <- %s", prop, name.c_str());
60 ctx::privilege_manager::set(prop, name.c_str());
63 cursor = cursor->next;
67 static void parse_link_node(xmlNodePtr cursor)
69 //TODO: Not supported yet
70 _D("Skipping a link node\n");
73 bool ctx::access_config_loader::load()
75 xmlDocPtr doc = xmlParseFile(CONFIG_PATH);
76 IF_FAIL_RETURN_TAG(doc, false, _E, "XML parsing failed");
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");
82 cursor = cursor->xmlChildrenNode;
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);
91 cursor = cursor->next;