tizen 2.3 release
[framework/web/wearable/wrt-security.git] / ace / engine / PolicyInformationPoint.cpp
1 /*
2  * Copyright (c) 2011 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 //
18 //
19 //
20 //  @ Project : Access Control Engine
21 //  @ File Name : PolicyInformationPoint.cpp
22 //  @ Date : 2009-05-06
23 //  @ Author : Samsung
24 //
25 //
26 #include <map>
27 #include <string>
28 #include <list>
29
30 #include <ace/PolicyInformationPoint.h>
31 #include <ace/ConfigurationManager.h>
32
33 #include <dpl/log/log.h>
34 #include <dpl/assert.h>
35 #include <dpl/foreach.h>
36
37 #include <ace/Attribute.h>
38 #include <ace-dao-ro/BaseAttribute.h>
39 #include <ace-dao-ro/AceDAOReadOnly.h>
40
41 using namespace AceDB;
42
43 PolicyInformationPoint::PolicyInformationPoint(IWebRuntime *wrt,
44         IResourceInformation *resource,
45         IOperationSystem *system) : wrtInterface(wrt),
46     resourceInformation(resource),
47     operationSystem(system)
48 {
49     AceDB::AceDAOReadOnly::attachToThreadRO();
50 }
51
52 PolicyInformationPoint::~PolicyInformationPoint()
53 {
54     AceDB::AceDAOReadOnly::detachFromThread();
55 }
56
57 /* gather attributes values from adequate interfaces */
58 PipResponse PolicyInformationPoint::getAttributesValues(const Request* request,
59         AttributeSet* attributes)
60 {
61     int subjectReturn = 0;
62     int resourceReturn = 0;
63     int operationReturn = 0;
64     int functionReturn = 0;
65     /* create query lists  */
66     createQueries(attributes);
67
68     /* check if subject attributes query has any elements*/
69     if (!subjectAttributesQuery.empty()) {
70         /* get Subject Attributes */
71         subjectReturn = wrtInterface->getAttributesValues(
72                 *request,
73                 &subjectAttributesQuery);
74     }
75
76     AttributeSet::const_iterator iter2;
77     FOREACH(iter, subjectAttributesQuery)
78     {
79         if (iter->second == NULL) {
80             Attribute attr(*(iter->first));
81             attr.setType(Attribute::Type::Subject);
82             iter2 = std::find_if(attributes->begin(),
83                                  attributes->end(),
84                                  BaseAttribute::UnaryPredicate(&attr));
85             Assert(iter2 != attributes->end() && "This should not happen, "
86                    "the attribute MUST be in attribute set");
87             (*iter2)->setUndetermind(true);
88         }
89     }
90
91     /* check if resource  attributes query has any elements*/
92     if (!resourceAttributesQuery.empty()) {
93         /* get Resource Attributes */
94         resourceReturn = resourceInformation->getAttributesValues(
95                 *request,
96                 &resourceAttributesQuery);
97         /* error analyzys*/
98         resourceReturn <<= ERROR_SHIFT_RESOURCE;
99     }
100
101     FOREACH(iter, resourceAttributesQuery)
102     {
103         if (iter->second == NULL) {
104             LogInfo("Found undetermined attribute");
105             Attribute attr(*(iter->first));
106             attr.setType(Attribute::Type::Resource);
107             iter2 = std::find_if(attributes->begin(),
108                                  attributes->end(),
109                                  BaseAttribute::UnaryPredicate(&attr));
110             Assert(iter2 != attributes->end() && "This should not happen, "
111                    "the attribute MUST be in attribute set");
112             (*iter2)->setUndetermind(true);
113         }
114     }
115
116     /* check if resource  attributes query has any elements*/
117     if (!environmentAttributesQuery.empty()) {
118         /* get enviroment attributes  */
119         operationReturn = operationSystem->getAttributesValues(
120                 *request,
121                 &environmentAttributesQuery);
122         /* error analyzys*/
123         operationReturn <<= ERROR_SHIFT_OS;
124     }
125
126     FOREACH(iter, environmentAttributesQuery)
127     {
128         if (iter->second == NULL) {
129             //it doesnt change uniqueness of a set element so we can const_cast
130             Attribute attr(*(iter->first));
131             attr.setType(Attribute::Type::Environment);
132             iter2 = find_if(attributes->begin(),
133                             attributes->end(),
134                             BaseAttribute::UnaryPredicate(&attr));
135             Assert(iter2 != attributes->end() && "This should not happen, "
136                    "the attribute MUST be in attribute set");
137             (*iter2)->setUndetermind(true);
138         }
139     }
140
141     /* check if functionParam attributes query has any elements*/
142     if (!functionParamAttributesQuery.empty() && request->getFunctionParam()) {
143         /* get params attributes  */
144         functionReturn = request->getFunctionParam()->getAttributesValues(
145                 *request,
146                 &functionParamAttributesQuery);
147         /* error analyzys*/
148         functionReturn <<= ERROR_SHIFT_FP;
149     }
150
151     FOREACH(iter, functionParamAttributesQuery)
152     {
153         if (iter->second == NULL) {
154             //it doesnt change uniqueness of a set element so we can const_cast
155             Attribute attr(*(iter->first));
156             attr.setType(Attribute::Type::FunctionParam);
157             iter2 = find_if(attributes->begin(),
158                             attributes->end(),
159                             BaseAttribute::UnaryPredicate(&attr));
160             Assert(iter2 != attributes->end() && "This should not happen, "
161                    "the attribute MUST be in attribute set");
162             (*iter2)->setUndetermind(true);
163         }
164     }
165
166     // Here we must add to attributes proper marking of policy type
167     // (Tizen or WAC widget)
168     /**
169      * This part of code seems odd here, but we don't want to keep it in
170      * attribute fascade, as it is maintained by ACE clients and we are not
171      * sure if this kind of distinction between different policies will be ok
172      * as final solution.
173      *
174      * This is somehow private part of ACE, so it may be moved into
175      * separate ACEAttributeFascade kind of a class in (already planned)
176      * refactoring, when moving to new, C-only API for ACE.
177      */
178     if (widgetParamAttributesQuery.empty()) {
179         LogError("No attrbutes of WidgetParam type present - "
180                  "should be widget type at least");
181     } else {
182         LogDebug("WidgetParam type atributes present, searching for widget type");
183         FOREACH(iter, widgetParamAttributesQuery) {
184             const std::string *name = iter->first;
185             if (POLICY_WIDGET_TYPE_ATTRIBUTE_NAME == *name) {
186                 LogDebug("Widget type attribute found");
187
188                 // Extracting widget type
189                 std::list<std::string> attrValue;
190                 Try {
191                     AceDB::AppTypes appType =
192                         AceDB::AceDAOReadOnly::getWidgetType(
193                             request->getWidgetHandle());
194                     switch (appType) {
195                     case AceDB::AppTypes::Tizen : {
196                         attrValue.push_back(POLICY_NAME_TIZEN);
197                         LogDebug("==== Using Tizen policy in PIP ====");
198                         break;}
199                     case AceDB::AppTypes::WAC20 : {
200                         attrValue.push_back(POLICY_NAME_WAC2_0);
201                         LogDebug("==== Using WAC policy in PIP ====");
202                         break;}
203                     default: {
204                         LogError("Invalid widget type");
205                         }
206                     }
207                 } Catch (AceDB::AceDAOReadOnly::Exception::DatabaseError)
208                 {
209                     LogError("Couldn't find widget for handle "
210                              << request->getWidgetHandle());
211                 }
212
213                 // Setting real attribute value
214                 Attribute attr(*(iter->first));
215                 attr.setType(Attribute::Type::WidgetParam);
216                 iter2 = find_if(attributes->begin(),
217                                 attributes->end(),
218                                 BaseAttribute::UnaryPredicate(&attr));
219                 Assert(iter2 != attributes->end() && "This should not happen, "
220                        "the attribute MUST be in attribute set");
221                 (*iter2)->setUndetermind(false);
222                 (*iter2)->setValue(attrValue);
223             }
224         }
225     }
226
227     /** clear query lists*/
228     resourceAttributesQuery.clear();
229     environmentAttributesQuery.clear();
230     subjectAttributesQuery.clear();
231     functionParamAttributesQuery.clear();
232     widgetParamAttributesQuery.clear();
233
234     return subjectReturn | resourceReturn | operationReturn | functionReturn;
235 }
236
237 /** create query lists */
238 void PolicyInformationPoint::createQueries(AttributeSet* attributes)
239 {
240     AttributeSet::const_iterator it;
241
242     enum Attribute::Type type;
243
244     /**iterate  all attributes and split them into adequate query  */
245     FOREACH (it, *attributes) {
246         type = (*it)->getType();
247
248         switch (type) {
249         case Attribute::Type::Subject:
250             subjectAttributesQuery.push_back(ATTRIBUTE((*it)->getName(),
251                                                        (*it)->getValue()));
252             break;
253
254         case Attribute::Type::Environment:
255             environmentAttributesQuery.push_back(ATTRIBUTE((*it)->getName(),
256                                                            (*it)->getValue()));
257             break;
258
259         case Attribute::Type::Resource:
260             resourceAttributesQuery.push_back(ATTRIBUTE((*it)->getName(),
261                                                         (*it)->getValue()));
262             break;
263
264         case Attribute::Type::FunctionParam:
265             functionParamAttributesQuery.push_back(ATTRIBUTE((*it)->getName(),
266                                                              (*it)->getValue()));
267             break;
268
269         case Attribute::Type::WidgetParam:
270             widgetParamAttributesQuery.push_back(ATTRIBUTE((*it)->getName(),
271                                                          (*it)->getValue()));
272             break;
273         default:
274             break;
275         }
276     }
277 }
278