1a80e12774b0076849625fbd1f9810bfbc0a6e6c
[platform/core/security/krate.git] / server / packman.cpp
1 /*
2  *  Copyright (c) 2015 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 #include <klay/exception.h>
17 #include <klay/audit/logger.h>
18
19 #include "packman.h"
20
21 namespace {
22
23 int PackageEventCallback(uid_t uid, int id, const char* type, const char* name,
24                                                  const char* key, const char* val, const void* msg, void* data)
25 {
26         return 0;
27 }
28
29 int AppListCallback(pkgmgrinfo_appinfo_h handle, void *data)
30 {
31         std::vector<ApplicationInfo>* appList = static_cast<std::vector<ApplicationInfo>*>(data);
32         appList->push_back(ApplicationInfo(handle));
33
34         return 0;
35 }
36
37 int PackageListCallback(pkgmgrinfo_pkginfo_h handle, void *data)
38 {
39         char* pkgid = nullptr;
40         ::pkgmgrinfo_pkginfo_get_pkgid(handle, &pkgid);
41
42         std::vector<std::string>* packageList = static_cast<std::vector<std::string>*>(data);
43         packageList->push_back(pkgid);
44
45         return 0;
46 }
47
48 } // namespace
49
50 PackageInfo::PackageInfo(const std::string& pkgid, uid_t uid) :
51         user(uid), handle(nullptr)
52 {
53         if (uid == 0) {
54                 uid = getuid();
55         }
56
57         if (::pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgid.c_str(), user, &handle) != PMINFO_R_OK) {
58                 throw runtime::Exception("Invalid Package Id");
59         }
60 }
61
62 PackageInfo::~PackageInfo()
63 {
64         ::pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
65 }
66
67 std::vector<ApplicationInfo> PackageInfo::getAppList() const
68 {
69         std::vector<ApplicationInfo> appList;
70
71         if (::pkgmgrinfo_appinfo_get_usr_list(handle, PMINFO_ALL_APP, AppListCallback, &appList, user) != PMINFO_R_OK) {
72                 ERROR("Error in pkgmgrinfo_appinfo_get_usr_list");
73         }
74
75         return appList;
76 }
77
78 std::string PackageInfo::getType() const
79 {
80         char *pkgtype;
81         if (::pkgmgrinfo_pkginfo_get_type(handle, &pkgtype) != PMINFO_R_OK) {
82                 throw runtime::Exception("Invalid operation");
83         }
84
85         return pkgtype;
86 }
87
88 std::string PackageInfo::getLabel() const
89 {
90         char *label;
91         if (::pkgmgrinfo_pkginfo_get_label(handle, &label) != PMINFO_R_OK) {
92                 throw runtime::Exception("Invalid operation");
93         }
94
95         return label;
96 }
97
98 std::string PackageInfo::getIcon() const
99 {
100         char *icon;
101         if (::pkgmgrinfo_pkginfo_get_icon(handle, &icon) != PMINFO_R_OK) {
102                 throw runtime::Exception("Invalid operation");
103         }
104
105         return icon;
106 }
107
108 std::string PackageInfo::getDescription() const
109 {
110         char *desc;
111         if (::pkgmgrinfo_pkginfo_get_description(handle, &desc) != PMINFO_R_OK) {
112                 throw runtime::Exception("Invalid operation");
113         }
114
115         if (desc == NULL)
116                 return "";
117
118         return desc;
119 }
120
121 std::string PackageInfo::getAuthorName() const
122 {
123         char *name;
124         if (::pkgmgrinfo_pkginfo_get_author_name(handle, &name) != PMINFO_R_OK) {
125                 throw runtime::Exception("Invalid operation");
126         }
127
128         return name;
129 }
130
131 std::string PackageInfo::getAuthorEmail() const
132 {
133         char *email;
134         if (::pkgmgrinfo_pkginfo_get_author_email(handle, &email) != PMINFO_R_OK) {
135                 throw runtime::Exception("Invalid operation");
136         }
137
138         return email;
139 }
140
141 std::string PackageInfo::getAuthorHref() const
142 {
143         char *href;
144         if (::pkgmgrinfo_pkginfo_get_author_name(handle, &href) != PMINFO_R_OK) {
145                 throw runtime::Exception("Invalid operation");
146         }
147
148         return href;
149 }
150
151 std::string PackageInfo::getVersion() const
152 {
153         char *version;
154         if (::pkgmgrinfo_pkginfo_get_version(handle, &version) != PMINFO_R_OK) {
155                 throw runtime::Exception("Invalid operation");
156         }
157
158         return version;
159 }
160
161 std::string PackageInfo::getApiVersion() const
162 {
163         char *api;
164         if (::pkgmgrinfo_pkginfo_get_api_version(handle, &api) != PMINFO_R_OK) {
165                 throw runtime::Exception("Invalid operation");
166         }
167
168         return api;
169 }
170
171 std::string PackageInfo::getMainAppId() const
172 {
173         char *mainappid;
174         if (::pkgmgrinfo_pkginfo_get_mainappid(handle, &mainappid) != PMINFO_R_OK) {
175                 throw runtime::Exception("Invalid operation");
176         }
177
178         return mainappid;
179 }
180
181 bool PackageInfo::isSystem() const
182 {
183         bool ret;
184         if (::pkgmgrinfo_pkginfo_is_system(handle, &ret) != PMINFO_R_OK) {
185                 throw runtime::Exception("Invalid operation");
186         }
187
188         return ret;
189 }
190
191 bool PackageInfo::isRemovable() const
192 {
193         bool ret;
194         if (::pkgmgrinfo_pkginfo_is_removable(handle, &ret) != PMINFO_R_OK) {
195                 throw runtime::Exception("Invalid operation");
196         }
197
198         return ret;
199 }
200
201 bool PackageInfo::isPreload() const
202 {
203         bool ret;
204         if (::pkgmgrinfo_pkginfo_is_preload(handle, &ret) != PMINFO_R_OK) {
205                 throw runtime::Exception("Invalid operation");
206         }
207
208         return ret;
209 }
210
211 ApplicationInfo::ApplicationInfo(const std::string& aid, uid_t uid)
212 {
213         pkgmgrinfo_appinfo_h handle;
214
215         if (::pkgmgrinfo_appinfo_get_usr_appinfo(aid.c_str(), uid, &handle) != PMINFO_R_OK) {
216                 throw runtime::Exception("Invalid App Id");
217         }
218
219         load(handle);
220
221         ::pkgmgrinfo_appinfo_destroy_appinfo(handle);
222 }
223
224 ApplicationInfo::ApplicationInfo(pkgmgrinfo_appinfo_h handle)
225 {
226         load(handle);
227 }
228
229
230 ApplicationInfo::~ApplicationInfo()
231 {
232 }
233
234 void ApplicationInfo::load(pkgmgrinfo_appinfo_h handle) {
235         pkgmgrinfo_app_component comp;
236         char* str;
237         int ret;
238
239         ret = ::pkgmgrinfo_appinfo_get_appid(handle, &str);
240         if (ret == PMINFO_R_OK) {
241                 id = str;
242         }
243
244         ret = ::pkgmgrinfo_appinfo_get_pkgid(handle, &str);
245         if (ret == PMINFO_R_OK) {
246                 package = str;
247         }
248
249         ret = ::pkgmgrinfo_appinfo_get_apptype(handle, &str);
250         if (ret == PMINFO_R_OK) {
251                 type = str;
252         }
253
254         ret = ::pkgmgrinfo_appinfo_get_icon(handle, &str);
255         if (ret == PMINFO_R_OK) {
256                 icon = str;
257         }
258
259         ret = ::pkgmgrinfo_appinfo_get_label(handle, &str);
260         if (ret == PMINFO_R_OK) {
261                 label = str;
262         }
263
264         ret = ::pkgmgrinfo_appinfo_get_component(handle, &comp);
265         if (ret == PMINFO_R_OK) {
266                 componentType = static_cast<int>(comp);
267         }
268
269         ::pkgmgrinfo_appinfo_is_nodisplay(handle, &noDisplayed);
270         ::pkgmgrinfo_appinfo_is_taskmanage(handle, &taskManaged);
271 }
272
273 const std::string& ApplicationInfo::getId() const
274 {
275         return id;
276 }
277
278 const std::string& ApplicationInfo::getPackage() const
279 {
280         return package;
281 }
282
283 const std::string& ApplicationInfo::getType() const
284 {
285         return type;
286 }
287
288 const std::string& ApplicationInfo::getIcon() const
289 {
290         return icon;
291 }
292
293 const std::string& ApplicationInfo::getLabel() const
294 {
295         return label;
296 }
297
298 int ApplicationInfo::getComponentType() const
299 {
300         return componentType;
301 }
302
303 bool ApplicationInfo::isNoDisplayed() const
304 {
305         return noDisplayed;
306 }
307
308 bool ApplicationInfo::isTaskManaged() const
309 {
310         return taskManaged;
311 }
312
313 PackageManager::PackageManager() :
314         nativeRequestHandle(nullptr), nativeListenHandle(nullptr)
315 {
316         nativeRequestHandle = ::pkgmgr_client_new(PC_REQUEST);
317         if (nativeRequestHandle == nullptr) {
318                 throw runtime::Exception("No package manager request instance");
319         }
320
321         nativeListenHandle = ::pkgmgr_client_new(PC_LISTENING);
322         if (nativeListenHandle == nullptr) {
323                 ::pkgmgr_client_free(nativeRequestHandle);
324                 throw runtime::Exception("No package manager listening instance");
325         }
326 }
327
328 PackageManager::~PackageManager()
329 {
330         ::pkgmgr_client_free(nativeRequestHandle);
331         ::pkgmgr_client_free(nativeListenHandle);
332 }
333
334 PackageManager& PackageManager::instance()
335 {
336         static PackageManager __instance__;
337         return __instance__;
338 }
339
340 void PackageManager::setEventCallback(pkgmgrinfo_handler callback, void* user_data)
341 {
342         int ret;
343         ret = ::pkgmgr_client_listen_status(nativeListenHandle, callback, user_data);
344         if (ret < 0) {
345                 throw runtime::Exception("Failed to set package event callback");
346         }
347 }
348
349 void PackageManager::unsetEventCallback()
350 {
351         int ret;
352         ret = ::pkgmgr_client_remove_listen_status(nativeListenHandle);
353         if (ret < 0) {
354                 throw runtime::Exception("Failed to unset package event callback");
355         }
356 }
357
358 void PackageManager::installPackage(const std::string& pkgpath, const uid_t user)
359 {
360         int ret = ::pkgmgr_client_usr_install(nativeRequestHandle, NULL, NULL, pkgpath.c_str(), NULL, PM_QUIET, PackageEventCallback, nullptr, user);
361         if (ret < PKGMGR_R_OK) {
362                 throw runtime::Exception("Package installation failed");
363         }
364 }
365
366 void PackageManager::uninstallPackage(const std::string& pkgid, const uid_t user)
367 {
368         std::string pkgtype;
369
370         PackageInfo pkgInfo(pkgid, user);
371         pkgtype = pkgInfo.getType();
372
373         int ret = ::pkgmgr_client_usr_uninstall(nativeRequestHandle, pkgtype.c_str(), pkgid.c_str(), PM_QUIET, PackageEventCallback, nullptr, user);
374         if (ret < PKGMGR_R_OK) {
375                 ERROR("Error in pkgmgr_client_uninstall");
376                 throw runtime::Exception("Package manager exception");
377         }
378 }
379
380 std::vector<std::string> PackageManager::getPackageList(const uid_t user)
381 {
382         std::vector<std::string> packageList;
383
384         if (::pkgmgrinfo_pkginfo_get_usr_list(PackageListCallback, &packageList, user) != PMINFO_R_OK) {
385                 ERROR("Error in pkgmgrinfo_pkginfo_get_list");
386         }
387
388         return packageList;
389 }
390
391 std::vector<ApplicationInfo> PackageManager::getAppList(const uid_t user)
392 {
393         std::vector<ApplicationInfo> appList;
394
395         if (::pkgmgrinfo_appinfo_get_usr_installed_list(AppListCallback, user, &appList) != PMINFO_R_OK) {
396                 ERROR("Error in pkgmgrinfo_appinfo_get_installed_list");
397         }
398
399         return appList;
400 }
401
402 void PackageManager::setModeRestriction(int mode, uid_t user)
403 {
404         if (::pkgmgr_client_usr_set_restriction_mode(nativeRequestHandle,
405                                                                                                  mode,
406                                                                                                  user) != PKGMGR_R_OK) {
407                 throw runtime::Exception("Failed to set package restriction mode");
408         }
409 }
410
411 void PackageManager::unsetModeRestriction(int mode, uid_t user)
412 {
413         if (::pkgmgr_client_usr_unset_restriction_mode(nativeRequestHandle,
414                                                                                                    mode,
415                                                                                                    user) != PKGMGR_R_OK) {
416                 throw runtime::Exception("Failed to unset package mode restriction");
417         }
418 }
419
420 int PackageManager::getModeRestriction(uid_t user)
421 {
422         int mode;
423         if (::pkgmgr_client_usr_get_restriction_mode(nativeRequestHandle,
424                                                                                                  &mode,
425                                                                                                  user) != PKGMGR_R_OK) {
426                 throw runtime::Exception("Failed to get package restriction mode");
427         }
428
429         return mode;
430 }