Osp-Installer merge changes from tizen_2.2
[platform/framework/native/installer.git] / src / Manager / SmackManager.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 /**
18  * @file        SmackManager.cpp
19  * @brief       This is the implementation file for %SmackManager class.
20  */
21
22 #include <unique_ptr.h>
23
24 #include <FAppPkgPackageInfo.h>
25 #include <FIoDirectory.h>
26 #include <FIoFile.h>
27 #include <FIoRegistry.h>
28 #include <FSecCryptoSha1Hash.h>
29 #include <FAppPkg_PackageManagerImpl.h>
30 #include <FBase_StringConverter.h>
31
32 #include "SmackManager.h"
33 #include "InstallerDefs.h"
34 #include "InstallerUtil.h"
35
36 using namespace Tizen::App;
37 using namespace Tizen::App::Package;
38 using namespace Tizen::Base;
39 using namespace Tizen::Base::Collection;
40 using namespace Tizen::Base::Utility;
41 using namespace Tizen::Security::Cert;
42 using namespace Tizen::Security::Crypto;
43 using namespace Tizen::Io;
44
45 SmackManager::SmackManager(void)
46 :__pContext(null)
47 {
48 }
49
50 SmackManager::~SmackManager(void)
51 {
52 }
53
54 bool
55 SmackManager::Construct(InstallationContext* pContext)
56 {
57         __pContext = pContext;
58
59         return true;
60 }
61
62 bool
63 SmackManager::Install(const PackageId& packageId)
64 {
65         TryReturn(__pContext, false, "__pContext is null");
66
67         int res = 0;
68
69         std::unique_ptr<char[]> pPackageId(_StringConverter::CopyToCharArrayN(packageId));
70         TryReturn(pPackageId, false, "pPackageId is null.");
71
72         res = Install(pPackageId.get());
73         TryReturn(res == 0, false, "Install() failed.");
74
75         return true;
76 }
77
78 bool
79 SmackManager::Uninstall(const PackageId& packageId)
80 {
81         int res = 0;
82
83         std::unique_ptr<char[]> pPackageId(_StringConverter::CopyToCharArrayN(packageId));
84         TryReturn(pPackageId, false, "pPackageId is null.");
85
86         res = Uninstall(pPackageId.get());
87
88         return true;
89 }
90
91 bool
92 SmackManager::AddLabelDir(const String& label, const String& dirPath, bool rootDirectory)
93 {
94         int res = 0;
95         String realPath;
96
97         std::unique_ptr<char[]> pPath(_StringConverter::CopyToCharArrayN(dirPath));
98         TryReturn(pPath, false, "pPath is null.");
99
100         if (InstallerUtil::IsSymlink(dirPath) == true)
101         {
102                 res = AddLabelDir("_", pPath.get());
103                 InstallerUtil::GetRealPath(dirPath, realPath);
104         }
105         else
106         {
107                 realPath = dirPath;
108         }
109
110         std::unique_ptr<char[]> pRealPath(_StringConverter::CopyToCharArrayN(realPath));
111         TryReturn(pRealPath, false, "pRealPath is null");
112
113         if ((rootDirectory == true) || (realPath.Contains(L"/shared/res") == true))
114         {
115                 res = AddLabelDir("_", pRealPath.get());
116         }
117         else
118         {
119                 std::unique_ptr<char[]> pLabel(_StringConverter::CopyToCharArrayN(label));
120                 TryReturn(pLabel, false, "pLabel is null.");
121
122                 res = AddLabelDir(pLabel.get(), pRealPath.get());
123         }
124
125         return true;
126 }
127
128 bool
129 SmackManager::AddLabelSymlink(const String& symlink)
130 {
131         int res = 0;
132
133         std::unique_ptr<char[]> pPath(_StringConverter::CopyToCharArrayN(symlink));
134         TryReturn(pPath, false, "pPath is null.");
135
136         if (InstallerUtil::IsSymlink(symlink) == true)
137         {
138                 res = AddLabelDir("_", pPath.get());
139         }
140         else
141         {
142                 AppLog("[%ls] is not symlink.", pPath.get());
143                 return false;
144         }
145
146         return true;
147 }
148
149 bool
150 SmackManager::EnablePermissions(const PackageId& packageId)
151 {
152         TryReturn(__pContext, false, "__pContext is null");
153
154         int res = 0;
155
156         std::unique_ptr<char[]> pPackageId(_StringConverter::CopyToCharArrayN(packageId));
157         TryReturn(pPackageId, false, "pPackageId is null.");
158
159         if (__pContext->__pPrivilegeList)
160         {
161                 int count = __pContext->__pPrivilegeList->GetCount();
162
163                 const char** pList = new (std::nothrow) const char*[count+1];
164                 TryReturn(pList, false, "pList is null.");
165
166                 for (int i = 0; i < count; i++)
167                 {
168                         String* pPrivilege = dynamic_cast < String* >(__pContext->__pPrivilegeList->GetAt(i));
169                         if (pPrivilege)
170                         {
171                                 char* pPrivilegeString = _StringConverter::CopyToCharArrayN(*pPrivilege);
172                                 if (pPrivilegeString)
173                                 {
174                                         pList[i] = pPrivilegeString;
175                                         AppLog("Privilege - [%s]", pList[i]);
176                                 }
177                         }
178                  }
179
180                 pList[count] = null;
181
182                 res = EnablePermissions(pPackageId.get(), APP_TYPE_OSP, pList, true);
183
184                 for (int i = 0; pList[i] != null; i++)
185                 {
186                         delete[] pList[i];
187                  }
188                 delete[] pList;
189         }
190         else
191         {
192                 const char* pNullList[1] = {null};
193                 res = EnablePermissions(pPackageId.get(), APP_TYPE_OSP, pNullList, true);
194         }
195
196         return true;
197 }
198
199 bool
200 SmackManager::RevokePermissions(const PackageId& packageId)
201 {
202         int res = 0;
203
204         std::unique_ptr<char[]> pPackageId(_StringConverter::CopyToCharArrayN(packageId));
205         TryReturn(pPackageId, false, "pPackageId is null.");
206
207         res = RevokePermissions(pPackageId.get());
208
209         return true;
210 }
211
212 bool
213 SmackManager::SetupPath(const PackageId& packageId, const String& dirPath, app_path_type_t type, const String& label)
214 {
215         int res = 0;
216         String realPath;
217
218         if (File::IsFileExist(dirPath) == false)
219         {
220                 AppLog("dirPath [%ls] is not existed. skip!", dirPath.GetPointer());
221                 return true;
222         }
223
224         std::unique_ptr<char[]> pPackageId(_StringConverter::CopyToCharArrayN(packageId));
225         TryReturn(pPackageId, false, "pPackageId is null.");
226
227         std::unique_ptr<char[]> pPath(_StringConverter::CopyToCharArrayN(dirPath));
228         TryReturn(pPath, false, "pPath is null.");
229
230         if (InstallerUtil::IsSymlink(dirPath) == true)
231         {
232                 res = SetupPath(pPackageId.get(), pPath.get(), APP_PATH_ANY_LABEL, "_");
233                 InstallerUtil::GetRealPath(dirPath, realPath);
234         }
235         else
236         {
237                 realPath = dirPath;
238         }
239
240         std::unique_ptr<char[]> pRealPath(_StringConverter::CopyToCharArrayN(realPath));
241         TryReturn(pRealPath, false, "pRealPath is null.");
242
243         if (type == APP_PATH_GROUP_RW)
244         {
245                 TryReturn(__pContext, false, "__pContext is null.");
246
247                 String groupId;
248
249                 if (__pContext->__isVerificationMode == true)
250                 {
251                         Sha1Hash hash;
252                         String base64Value;
253                         result r = E_SUCCESS;
254
255                         IListT<String *>* pAuthorCertList = __pContext->__pAuthorCertList;
256                         TryReturn(pAuthorCertList, false, "pAuthorCertList is null.");
257
258                         String* pEntity = null;
259                         r = pAuthorCertList->GetAt(0, pEntity);
260                         TryReturn(!IsFailed(r), false, "pAuthorCertList->GetAt() is failed.");
261                         TryReturn(pEntity, false, "pEntity is null.");
262
263                         std::unique_ptr<ByteBuffer> pEncodedData(StringUtil::DecodeBase64StringN(*pEntity));
264                         TryReturn(pEncodedData, false, "pEncodedData is null.");
265
266                         std::unique_ptr<ByteBuffer> pHashValue(hash.GetHashN(*pEncodedData.get()));
267                         TryReturn(pHashValue, false, "pHashValue is null.");
268
269                         r = StringUtil::EncodeToBase64String(*pHashValue, base64Value);
270                         TryReturn(!IsFailed(r), false, "EncodeToBase64String() is failed.");
271
272                         // in smack, '/' is not allowed for label.
273                         r = base64Value.Replace(L"/", L"#");
274                         TryReturn(!IsFailed(r), false, "base64Value.Replace() is failed.");
275
276                         std::unique_ptr<char[]> pHashEncodedValue(_StringConverter::CopyToCharArrayN(base64Value));
277                         TryReturn(pHashEncodedValue, false, "pHashEncodedValue is null.");
278
279                         groupId = pHashEncodedValue.get();
280                         AppLog("pHashEncodedValue = [%s]", pHashEncodedValue.get());
281                 }
282                 else
283                 {
284                         groupId = __pContext->__packageId;
285                 }
286
287                 std::unique_ptr<char[]> pGroupId(_StringConverter::CopyToCharArrayN(groupId));
288                 TryReturn(pGroupId, false, "pGroupId is null.");
289
290                 res = SetupPath(pPackageId.get(), pRealPath.get(), type, pGroupId.get());
291         }
292         else if (type == APP_PATH_ANY_LABEL)
293         {
294                 TryReturn(label.IsEmpty() == false, false, "label is empty.");
295
296                 std::unique_ptr<char[]> pAnyLabel(_StringConverter::CopyToCharArrayN(label));
297                 TryReturn(pAnyLabel, false, "pAnyLabel is null.");
298
299                 res = SetupPath(pPackageId.get(), pRealPath.get(), type, pAnyLabel.get());
300         }
301         else
302         {
303                 res = SetupPath(pPackageId.get(), pRealPath.get(), type);
304         }
305
306         return true;
307 }
308
309 bool
310 SmackManager::Begin(void)
311 {
312         AppLog("[smack] perm_begin()");
313         int ret = perm_begin();
314         AppLog("[smack] perm_begin(), result = [%d]", ret);
315
316         return true;
317 }
318
319 bool
320 SmackManager::End(void)
321 {
322         AppLog("[smack] perm_end()");
323         int ret = perm_end();
324         AppLog("[smack] perm_end(), result = [%d]", ret);
325
326         return true;
327 }
328
329 int
330 SmackManager::Install(const char* pPackageId)
331 {
332         AppLog("[smack] perm_app_install(%s)", pPackageId);
333         int ret = perm_app_install(pPackageId);
334         AppLog("[smack] perm_app_install(%s), result = [%d]", pPackageId, ret);
335
336         return ret;
337 }
338
339 int
340 SmackManager::Uninstall(const char* pPackageId)
341 {
342         AppLog("[smack] perm_app_uninstall(%s)", pPackageId);
343         int ret = perm_app_uninstall(pPackageId);
344         AppLog("[smack] perm_app_uninstall(%s), result = [%d]", pPackageId, ret);
345
346         return ret;
347 }
348
349 int
350 SmackManager::AddLabelDir(const char* pLabel, const char* pDirPath)
351 {
352         AppLog("[smack] app_label_dir(%s, %s)", pLabel, pDirPath);
353         int ret = app_label_dir(pLabel, pDirPath);
354         AppLog("[smack] app_label_dir(%s, %s), result = [%d]", pLabel, pDirPath, ret);
355
356         return ret;
357 }
358
359 int
360 SmackManager::EnablePermissions(const char* pPackageId, app_type_t appType, const char** ppPermissions, bool persistent)
361 {
362         AppLog("[smack] perm_app_enable_permissions(%s, %d)", pPackageId, appType);
363         int ret = perm_app_enable_permissions(pPackageId, appType, ppPermissions, persistent);
364         AppLog("[smack] perm_app_enable_permissions(%s, %d), result = [%d]", pPackageId, appType, ret);
365
366         return ret;
367 }
368
369 int
370 SmackManager::RevokePermissions(const char* pPackageId)
371 {
372         AppLog("[smack] perm_app_revoke_permissions(%s)", pPackageId);
373         int ret = perm_app_revoke_permissions(pPackageId);
374         AppLog("[smack] perm_app_revoke_permissions(%s), result = [%d]", pPackageId, ret);
375
376         return ret;
377 }
378
379 int
380 SmackManager::SetupPath(const char* pPackageId, const char* pDirPath, app_path_type_t appPathType, const char* pGroupId)
381 {
382         int ret = 0;
383
384         if (pGroupId == null)
385         {
386                 AppLog("[smack] perm_app_setup_path(%s, %s, %d)", pPackageId, pDirPath, appPathType);
387                 ret = perm_app_setup_path(pPackageId, pDirPath, appPathType);
388                 AppLog("[smack] perm_app_setup_path(), result = [%d]", ret);
389         }
390         else
391         {
392                 AppLog("[smack] perm_app_setup_path(%s, %s, %d, %s)", pPackageId, pDirPath, appPathType, pGroupId);
393                 ret = perm_app_setup_path(pPackageId, pDirPath, appPathType, pGroupId);
394                 AppLog("[smack] perm_app_setup_path(), result = [%d]", ret);
395         }
396
397         return ret;
398 }