48e2c2bf2c77d59fbdc8dfc6152a23e21a428dda
[framework/security/security-server.git] / ace / dao / AceDAOReadOnly.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  * @file       AceDAOReadOnlyReadOnly.cpp
20  * @author     Krzysztof Jackiewicz (k.jackiewicz@samsung.com)
21  * @author     Grzegorz Krawczyk (g.krawczyk@samsung.com)
22  * @version    0.1
23  * @brief
24  */
25
26 #include <list>
27 #include <utility>
28
29 #include <ace-dao-ro/AceDAOReadOnly.h>
30 #include <ace-dao-ro/AceDAOUtilities.h>
31 #include <ace-dao-ro/AceDAOConversions.h>
32 #include <ace-dao-ro/AceDatabase.h>
33 #include <dpl/foreach.h>
34
35 using namespace DPL::DB::ORM;
36 using namespace DPL::DB::ORM::ace;
37 using namespace AceDB::AceDaoUtilities;
38 using namespace AceDB::AceDaoConversions;
39
40 namespace AceDB {
41
42 static const int DB_ALLOW_ALWAYS = 0;
43 static const int DB_ALLOW_FOR_SESSION = 1;
44 static const int DB_ALLOW_THIS_TIME = 2;
45 static const int DB_DENY_ALWAYS = 3;
46 static const int DB_DENY_FOR_SESSION = 4;
47 static const int DB_DENY_THIS_TIME = 5;
48
49 static const int DB_APP_UNKNOWN = 0;
50 static const int DB_APP_WAC20 = 1;
51 static const int DB_APP_TIZEN = 2;
52
53 int AceDAOReadOnly::promptDecisionToInt(PromptDecision decision)
54 {
55     if (PromptDecision::ALLOW_ALWAYS == decision) {
56         return DB_ALLOW_ALWAYS;
57     } else if (PromptDecision::DENY_ALWAYS == decision) {
58         return DB_DENY_ALWAYS;
59     } else if (PromptDecision::ALLOW_THIS_TIME == decision) {
60         return DB_ALLOW_THIS_TIME;
61     } else if (PromptDecision::DENY_THIS_TIME == decision) {
62         return DB_DENY_THIS_TIME;
63     } else if (PromptDecision::ALLOW_FOR_SESSION == decision) {
64         return DB_ALLOW_FOR_SESSION;
65     }
66     // DENY_FOR_SESSION
67     return DB_DENY_FOR_SESSION;
68 }
69
70 PromptDecision AceDAOReadOnly::intToPromptDecision(int dec) {
71     if (dec == DB_ALLOW_ALWAYS) {
72         return PromptDecision::ALLOW_ALWAYS;
73     } else if (dec == DB_DENY_ALWAYS) {
74         return PromptDecision::DENY_ALWAYS;
75     } else if (dec == DB_ALLOW_THIS_TIME) {
76         return PromptDecision::ALLOW_THIS_TIME;
77     } else if (dec == DB_DENY_THIS_TIME) {
78         return PromptDecision::DENY_THIS_TIME;
79     } else if (dec == DB_ALLOW_FOR_SESSION) {
80         return PromptDecision::ALLOW_FOR_SESSION;
81     }
82     // DB_DENY_FOR_SESSION
83     return PromptDecision::DENY_FOR_SESSION;
84 }
85
86 int AceDAOReadOnly::appTypeToInt(AppTypes app_type)
87 {
88     switch (app_type) {
89     case AppTypes::Unknown:
90         return DB_APP_UNKNOWN;
91     case AppTypes::WAC20:
92         return DB_APP_WAC20;
93     case AppTypes::Tizen:
94         return DB_APP_TIZEN;
95     default:
96         return DB_APP_UNKNOWN;
97     }
98
99 }
100
101 AppTypes AceDAOReadOnly::intToAppType(int app_type)
102 {
103     switch (app_type) {
104     case DB_APP_UNKNOWN:
105         return AppTypes::Unknown;
106     case DB_APP_WAC20:
107         return AppTypes::WAC20;
108     case DB_APP_TIZEN:
109         return AppTypes::Tizen;
110     default:
111         return AppTypes::Unknown;
112     }
113 }
114
115 void AceDAOReadOnly::attachToThreadRO()
116 {
117     AceDaoUtilities::m_databaseInterface.AttachToThread(
118         DPL::DB::SqlConnection::Flag::RO);
119 }
120
121 void AceDAOReadOnly::attachToThreadRW()
122 {
123     AceDaoUtilities::m_databaseInterface.AttachToThread(
124         DPL::DB::SqlConnection::Flag::RW);
125 }
126
127 void AceDAOReadOnly::detachFromThread()
128 {
129     AceDaoUtilities::m_databaseInterface.DetachFromThread();
130 }
131
132 OptionalCachedPromptDecision AceDAOReadOnly::getPromptDecision(
133     WidgetHandle widgetHandle,
134     int ruleId)
135 {
136     Try {
137         // get matching subject verdict
138         ACE_DB_SELECT(select, AcePromptDecision, &AceDaoUtilities::m_databaseInterface);
139
140         select->Where(
141             And(
142                 Equals<AcePromptDecision::rule_id>(ruleId),
143                 Equals<AcePromptDecision::app_id>(widgetHandle)));
144
145         std::list<AcePromptDecision::Row> rows = select->GetRowList();
146         if (rows.empty()) {
147             return OptionalCachedPromptDecision();
148         }
149
150         AcePromptDecision::Row row = rows.front();
151         CachedPromptDecision decision;
152         decision.decision = intToPromptDecision(row.Get_decision());
153         decision.session = row.Get_session();
154
155         return OptionalCachedPromptDecision(decision);
156     }
157     Catch(DPL::DB::SqlConnection::Exception::Base) {
158         ReThrowMsg(Exception::DatabaseError, "Failed to getPromptDecision");
159     }
160 }
161
162 void AceDAOReadOnly::getAttributes(BaseAttributeSet *attributes)
163 {
164     if (NULL == attributes) {
165         LogError("NULL pointer");
166         return;
167     }
168     attributes->clear();
169     std::string aname;
170     int type;
171     Try {
172         ACE_DB_SELECT(select, AceAttribute, &AceDaoUtilities::m_databaseInterface);
173         typedef std::list<AceAttribute::Row> RowList;
174         RowList list = select->GetRowList();
175
176         FOREACH(i, list) {
177             BaseAttributePtr attribute(new BaseAttribute());
178             DPL::String name = i->Get_name();
179             aname = DPL::ToUTF8String(name);
180             type = i->Get_type();
181
182             attribute->setName(&aname);
183             attribute->setType(intToAttributeType(type));
184             attributes->insert(attribute);
185         }
186     }
187     Catch(DPL::DB::SqlConnection::Exception::Base) {
188         ReThrowMsg(Exception::DatabaseError, "Failed to getAttributes");
189     }
190 }
191
192 OptionalExtendedPolicyResult AceDAOReadOnly::getPolicyResult(
193         const BaseAttributeSet &attributes)
194 {
195
196     auto attrHash = convertToHash(attributes);
197     return getPolicyResult(attrHash);
198 }
199
200 OptionalExtendedPolicyResult AceDAOReadOnly::getPolicyResult(
201     const DPL::String &attrHash)
202 {
203     Try {
204         // get matching subject verdict
205         ACE_DB_SELECT(select, AcePolicyResult, &AceDaoUtilities::m_databaseInterface);
206         Equals<AcePolicyResult::hash> e1(attrHash);
207         select->Where(e1);
208
209         std::list<AcePolicyResult::Row> rows = select->GetRowList();
210         if (rows.empty()) {
211             return OptionalExtendedPolicyResult();
212         }
213
214         AcePolicyResult::Row row = rows.front();
215         int decision = row.Get_decision();
216         ExtendedPolicyResult res;
217         res.policyResult = PolicyResult::deserialize(decision);
218         res.ruleId = row.Get_rule_id();
219         return OptionalExtendedPolicyResult(res);
220     }
221     Catch(DPL::DB::SqlConnection::Exception::Base) {
222         ReThrowMsg(Exception::DatabaseError, "Failed to getVerdict");
223     }
224 }
225
226 PreferenceTypes AceDAOReadOnly::getDevCapSetting(const std::string &resource)
227 {
228     Try {
229         AceDevCap::Row row;
230         if (!getResourceByUri(resource, row)) {
231             return PreferenceTypes::PREFERENCE_DEFAULT;
232         }
233         return intToPreference(row.Get_general_setting());
234     }
235     Catch(DPL::DB::SqlConnection::Exception::Base) {
236         ReThrowMsg(Exception::DatabaseError, "Failed to getResourceSetting");
237     }
238 }
239
240 void AceDAOReadOnly::getDevCapSettings(PreferenceTypesMap *globalSettingsMap)
241 {
242     if (NULL == globalSettingsMap) {
243         LogError("Null pointer");
244         return;
245     }
246     globalSettingsMap->clear();
247     Try {
248         ACE_DB_SELECT(select, AceDevCap, &AceDaoUtilities::m_databaseInterface);
249         typedef std::list<AceDevCap::Row> RowList;
250         RowList list = select->GetRowList();
251
252         FOREACH(i, list) {
253             PreferenceTypes p = intToPreference(i->Get_general_setting());
254             globalSettingsMap->insert(make_pair(DPL::ToUTF8String(
255                 i->Get_id_uri()), p));
256         }
257     }
258     Catch(DPL::DB::SqlConnection::Exception::Base) {
259         ReThrowMsg(Exception::DatabaseError, "Failed to getResourceSettings");
260     }
261 }
262
263 void AceDAOReadOnly::getWidgetDevCapSettings(BasePermissionList *outputList)
264 {
265     if (NULL == outputList) {
266         LogError("NULL pointer");
267         return;
268     }
269     outputList->clear();
270     Try {
271         std::string resourceName;
272         PreferenceTypes allowAccess;
273
274         ACE_DB_SELECT(select,
275                       AceWidgetDevCapSetting,
276                       &AceDaoUtilities::m_databaseInterface);
277
278         typedef std::list<AceWidgetDevCapSetting::Row> RowList;
279         RowList list = select->GetRowList();
280
281         // TODO JOIN
282         FOREACH(i, list) {
283             int app_id = i->Get_app_id();
284             int res_id = i->Get_resource_id();
285
286             ACE_DB_SELECT(resourceSelect, AceDevCap, &AceDaoUtilities::m_databaseInterface);
287             resourceSelect->Where(Equals<AceDevCap::resource_id>(res_id));
288             AceDevCap::Row rrow = resourceSelect->GetSingleRow();
289
290             resourceName = DPL::ToUTF8String(rrow.Get_id_uri());
291
292             if (!resourceName.empty()) {
293                 allowAccess = intToPreference(i->Get_access_value());
294                 outputList->push_back(
295                     BasePermission(app_id,
296                     resourceName,
297                     allowAccess));
298             }
299         }
300     }
301     Catch(DPL::DB::SqlConnection::Exception::Base) {
302         ReThrowMsg(Exception::DatabaseError, "Failed to findUserSettings");
303     }
304 }
305
306 PreferenceTypes AceDAOReadOnly::getWidgetDevCapSetting(
307         const std::string &resource,
308         WidgetHandle handler)
309 {
310     Try {
311         AceDevCap::Row rrow;
312         if (!getResourceByUri(resource, rrow)) {
313             return PreferenceTypes::PREFERENCE_DEFAULT;
314         }
315         int resourceId = rrow.Get_resource_id();
316
317         // get matching user setting
318         ACE_DB_SELECT(select, AceWidgetDevCapSetting, &AceDaoUtilities::m_databaseInterface);
319
320         select->Where(And(Equals<AceWidgetDevCapSetting::resource_id>(resourceId),
321                 Equals<AceWidgetDevCapSetting::app_id>(handler)));
322
323         std::list<int> values =
324             select->GetValueList<AceWidgetDevCapSetting::access_value>();
325         if (values.empty()) {
326             return PreferenceTypes::PREFERENCE_DEFAULT;
327         }
328         return intToPreference(values.front());
329     }
330     Catch(DPL::DB::SqlConnection::Exception::Base) {
331         ReThrowMsg(Exception::DatabaseError, "Failed in getUserSetting");
332     }
333 }
334
335 void AceDAOReadOnly::getRequestedDevCaps(
336     WidgetHandle widgetHandle,
337     RequestedDevCapsMap *permissions)
338 {
339     if (NULL == permissions) {
340         LogError("NULL pointer");
341         return;
342     }
343     permissions->clear();
344     Try {
345         ACE_DB_SELECT(select, AceRequestedDevCaps,
346                       &AceDaoUtilities::m_databaseInterface);
347         select->Where(
348             Equals<AceRequestedDevCaps::app_id>(widgetHandle));
349         std::list<AceRequestedDevCaps::Row> list = select->GetRowList();
350
351         FOREACH(i, list) {
352             permissions->insert(std::make_pair(i->Get_dev_cap(),
353                    i->Get_grant_smack() == 1));
354         }
355     } Catch(DPL::DB::SqlConnection::Exception::Base) {
356         ReThrowMsg(Exception::DatabaseError, "Failed to getRequestedDevCaps");
357     }
358 }
359
360 void AceDAOReadOnly::getAcceptedFeature(
361     WidgetHandle widgetHandle,
362     FeatureNameVector *fvector)
363 {
364     if (NULL == fvector) {
365         LogError("NULL pointer");
366         return;
367     }
368
369     fvector->clear();
370     Try {
371         ACE_DB_SELECT(select, AceAcceptedFeature,
372                       &AceDaoUtilities::m_databaseInterface);
373         select->Where(
374             Equals<AceAcceptedFeature::app_id>(widgetHandle));
375         std::list<AceAcceptedFeature::Row> list = select->GetRowList();
376
377         FOREACH(i, list) {
378             fvector->push_back(i->Get_feature());
379         }
380     } Catch(DPL::DB::SqlConnection::Exception::Base) {
381         ReThrowMsg(Exception::DatabaseError, "Failed to getRequestedDevCaps");
382     }
383 }
384
385 AppTypes AceDAOReadOnly::getWidgetType(WidgetHandle handle)
386 {
387     Try {
388         ACE_DB_SELECT(select, WidgetInfo, &AceDaoUtilities::m_databaseInterface);
389         select->Where(Equals<WidgetInfo::app_id>(handle));
390         WidgetInfo::Select::RowList rows = select->GetRowList();
391         DPL::OptionalInt res;
392         if (!rows.empty()) {
393             res = rows.front().Get_widget_type();
394             AppTypes retType = (res.IsNull() ? AppTypes::Unknown : static_cast<AppTypes>(*res));
395             return retType;
396         } else {
397             LogDebug("Can not find widget type");
398             return AppTypes::Unknown;
399         }
400     }
401     Catch(DPL::DB::SqlConnection::Exception::Base) {
402         ReThrowMsg(Exception::DatabaseError, "Failed to getWidgetType");
403     }
404 }
405
406 std::string AceDAOReadOnly::getVersion(WidgetHandle widgetHandle)
407 {
408     Try
409     {
410         ACE_DB_SELECT(select, WidgetInfo, &AceDaoUtilities::m_databaseInterface);
411         select->Where(Equals<WidgetInfo::app_id>(widgetHandle));
412         WidgetInfo::Select::RowList rows = select->GetRowList();
413         DPL::OptionalString res;
414         if(!rows.empty()) {
415             res = rows.front().Get_widget_version();
416             return (res.IsNull() ? "" : DPL::ToUTF8String(*res));
417         } else {
418             LogDebug("Widget not installed");
419             return "";
420         }
421     } Catch(DPL::DB::SqlConnection::Exception::Base) {
422         ReThrowMsg(Exception::DatabaseError, "Failed to getVersion");
423     }
424 }
425
426 std::string AceDAOReadOnly::getAuthorName(WidgetHandle widgetHandle)
427 {
428     Try
429     {
430         ACE_DB_SELECT(select, WidgetInfo, &AceDaoUtilities::m_databaseInterface);
431         select->Where(Equals<WidgetInfo::app_id>(widgetHandle));
432         WidgetInfo::Select::RowList rows = select->GetRowList();
433         DPL::OptionalString res;
434         if(!rows.empty()) {
435             res = rows.front().Get_author_name();
436             return (res.IsNull() ? "" : DPL::ToUTF8String(*res));
437         } else {
438             LogDebug("Widget not installed");
439             return "";
440         }
441     } Catch(DPL::DB::SqlConnection::Exception::Base) {
442         ReThrowMsg(Exception::DatabaseError, "Failed to getAuthorName");
443     }
444 }
445
446 std::string AceDAOReadOnly::getGUID(WidgetHandle widgetHandle)
447 {
448     Try
449     {
450         ACE_DB_SELECT(select, WidgetInfo, &AceDaoUtilities::m_databaseInterface);
451         select->Where(Equals<WidgetInfo::app_id>(widgetHandle));
452         WidgetInfo::Select::RowList rows = select->GetRowList();
453         DPL::OptionalString res;
454         if(!rows.empty()) {
455             res = rows.front().Get_widget_id();
456             return (res.IsNull() ? "" : DPL::ToUTF8String(*res));
457         } else {
458             LogDebug("Widget not installed");
459             return "";
460         }
461     } Catch(DPL::DB::SqlConnection::Exception::Base) {
462         ReThrowMsg(Exception::DatabaseError, "Failed to getGUID");
463     }
464 }
465
466 WidgetCertificateCNList AceDAOReadOnly::getKeyCommonNameList(
467         WidgetHandle widgetHandle,
468         WidgetCertificateData::Owner owner,
469         WidgetCertificateData::Type type)
470 {
471     Try {
472         ACE_DB_SELECT(select, WidgetCertificateFingerprint, &AceDaoUtilities::m_databaseInterface);
473         select->Where(And(And(
474             Equals<WidgetCertificateFingerprint::app_id>(widgetHandle),
475             Equals<WidgetCertificateFingerprint::owner>(owner)),
476             Equals<WidgetCertificateFingerprint::type>(type)));
477         WidgetCertificateFingerprint::Select::RowList rows = select->GetRowList();
478
479         WidgetCertificateCNList out;
480         FOREACH(it, rows)
481         {
482             DPL::Optional<DPL::String> cn = it->Get_common_name();
483             out.push_back(cn.IsNull() ? "" : DPL::ToUTF8String(*cn));
484         }
485         return out;
486     }
487     Catch(DPL::DB::SqlConnection::Exception::Base) {
488         ReThrowMsg(Exception::DatabaseError, "Failed to getKeyCommonNameList");
489     }
490 }
491
492 FingerPrintList AceDAOReadOnly::getKeyFingerprints(
493         WidgetHandle widgetHandle,
494         WidgetCertificateData::Owner owner,
495         WidgetCertificateData::Type type)
496 {
497     Try
498     {
499         ACE_DB_SELECT(select, WidgetCertificateFingerprint, &AceDaoUtilities::m_databaseInterface);
500         select->Where(And(And(
501             Equals<WidgetCertificateFingerprint::app_id>(widgetHandle),
502             Equals<WidgetCertificateFingerprint::owner>(owner)),
503             Equals<WidgetCertificateFingerprint::type>(type)));
504         WidgetCertificateFingerprint::Select::RowList rows = select->GetRowList();
505
506         FingerPrintList keys;
507         FOREACH(it, rows)
508         {
509             DPL::Optional<DPL::String> sha1 = it->Get_sha1_fingerprint();
510             if (!sha1.IsNull())
511                 keys.push_back(DPL::ToUTF8String(*sha1));
512             DPL::Optional<DPL::String> md5 = it->Get_md5_fingerprint();
513             if (!md5.IsNull())
514                 keys.push_back(DPL::ToUTF8String(*md5));
515         }
516         return keys;
517     }
518     Catch(DPL::DB::SqlConnection::Exception::Base) {
519         ReThrowMsg(Exception::DatabaseError, "Failed to getKeyFingerprints");
520     }
521 }
522
523 std::string AceDAOReadOnly::getShareHref(WidgetHandle widgetHandle)
524 {
525     Try
526     {
527         ACE_DB_SELECT(select, WidgetInfo, &AceDaoUtilities::m_databaseInterface);
528         select->Where(Equals<WidgetInfo::app_id>(widgetHandle));
529         WidgetInfo::Select::RowList rows = select->GetRowList();
530
531         if(rows.empty())
532             ThrowMsg(Exception::DatabaseError, "Cannot find widget. Handle: " << widgetHandle);
533
534         DPL::Optional<DPL::String> value = rows.front().Get_share_href();
535         std::string ret = "";
536         if(!value.IsNull())
537             ret = DPL::ToUTF8String(*value);
538         return ret;
539     }
540     Catch(DPL::DB::SqlConnection::Exception::Base) {
541         ReThrowMsg(Exception::DatabaseError, "Failed to getShareHref");
542     }
543 }
544
545 WidgetHandleList AceDAOReadOnly::getHandleList()
546 {
547     LogDebug("Getting DbWidgetHandle List");
548     Try
549     {
550         ACE_DB_SELECT(select, WidgetInfo, &AceDaoUtilities::m_databaseInterface);
551         return select->GetValueList<WidgetInfo::app_id>();
552     }
553     Catch(DPL::DB::SqlConnection::Exception::Base) {
554         ReThrowMsg(Exception::DatabaseError, "Failed to list of widget handles");
555     }
556 }
557
558 bool AceDAOReadOnly::isWidgetInstalled(WidgetHandle handle)
559 {
560     Try {
561         ACE_DB_SELECT(select, WidgetInfo, &AceDaoUtilities::m_databaseInterface);
562         select->Where(Equals<WidgetInfo::app_id>(handle));
563         WidgetInfo::Select::RowList rows = select->GetRowList();
564         return !rows.empty() ? true : false;
565     } Catch(DPL::DB::SqlConnection::Exception::Base) {
566         ReThrowMsg(Exception::DatabaseError, "Failed in isWidgetInstalled");
567     }
568 }
569
570 }