4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Jayoun Lee <airjany@samsung.com>, Sewook Park <sewook7.park@samsung.com>,
7 * Jaeho Lee <jaeho81.lee@samsung.com>, Shobhit Srivastava <shobhit.s@samsung.com>
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
34 #include <pkgmgr-info.h>
36 static int __pkg_list_cb (const pkgmgrinfo_pkginfo_h handle, void *user_data);
42 #define _E(fmt, arg...) fprintf(stderr, "[PKG_SMACK][E][%s,%d] "fmt"\n", __FUNCTION__, __LINE__, ##arg);
47 #define _D(fmt, arg...) fprintf(stderr, "[PKG_SMACK][D][%s,%d] "fmt"\n", __FUNCTION__, __LINE__, ##arg);
49 #define LIB_PRIVILEGE_CONTROL "libprivilege-control.so.0"
50 #define LIB_SMACK "libsmack.so.1"
53 #define APP_OWNER_ID 5000
54 #define APP_GROUP_ID 5000
64 static int __is_dir(char *dirname)
66 struct stat stFileInfo;
67 stat(dirname, &stFileInfo);
68 if (S_ISDIR(stFileInfo.st_mode)) {
74 int __pkg_smack_register_package(const char *pkgid)
79 int (*app_install)(const char*) = NULL;
84 handle = dlopen(LIB_PRIVILEGE_CONTROL, RTLD_LAZY | RTLD_GLOBAL);
86 _E( "register package: dlopen() failed. [%s]", dlerror());
90 app_install = dlsym(handle, "app_install");
92 if ((errmsg != NULL) || (app_install == NULL)) {
93 _E( "register package: dlsym() failed. [%s]", errmsg);
98 _E( "[smack] app_install(%s)", pkgid);
99 ret = app_install(pkgid);
100 _E( "[smack] app_install(%s), result = [%d]", pkgid, ret);
106 int __pkg_smack_unregister_package(const char *pkgid)
111 int (*app_uninstall)(const char*) = NULL;
116 handle = dlopen(LIB_PRIVILEGE_CONTROL, RTLD_LAZY | RTLD_GLOBAL);
118 _E( "unregister package: dlopen() failed. [%s]", dlerror());
122 app_uninstall = dlsym(handle, "app_uninstall");
124 if ((errmsg != NULL) || (app_uninstall == NULL)) {
125 _E( "unregister package: dlsym() failed. [%s]", errmsg);
130 _E( "[smack] app_uninstall(%s)", pkgid);
131 ret = app_uninstall(pkgid);
132 _E( "[smack] app_uninstall(%s), result = [%d]", pkgid, ret);
138 int __pkg_smack_revoke_permissions(const char *pkgid)
143 int (*app_revoke_permissions)(const char*) = NULL;
148 handle = dlopen(LIB_PRIVILEGE_CONTROL, RTLD_LAZY | RTLD_GLOBAL);
150 _E( "revoke permissions: dlopen() failed. [%s][%s]", pkgid, dlerror());
154 app_revoke_permissions = dlsym(handle, "app_revoke_permissions");
156 if ((errmsg != NULL) || (app_revoke_permissions == NULL)) {
157 _E( "revoke permissions(): dlsym() failed. [%s][%s]", pkgid, errmsg);
162 _E( "[smack] app_revoke_permissions(%s)", pkgid);
163 ret = app_revoke_permissions(pkgid);
164 _E( "[smack] app_revoke_permissions(%s), result = [%d]", pkgid, ret);
170 int __pkg_smack_enable_permissions(const char *pkgid, int apptype,
171 const char **perms, int persistent)
176 int (*app_enable_permissions)(const char*, int, const char**, bool) = NULL;
181 handle = dlopen(LIB_PRIVILEGE_CONTROL, RTLD_LAZY | RTLD_GLOBAL);
183 _E( "enable permissions(): dlopen() failed. [%s]", dlerror());
187 app_enable_permissions = dlsym(handle, "app_enable_permissions");
189 if ((errmsg != NULL) || (app_enable_permissions == NULL)) {
190 _E( "enable permissions(): dlsym() failed. [%s]", errmsg);
195 _E( "[smack] app_enable_permissions(%s, %d)", pkgid, apptype);
196 ret = app_enable_permissions(pkgid, apptype, perms, persistent);
197 _E( "[smack] app_enable_permissions(%s, %d), result = [%d]", pkgid, apptype, ret);
203 int __pkg_smack_setup_path(const char *pkgid, const char *dirpath,
204 int apppathtype, const char *groupid)
209 int (*app_setup_path)(const char*, const char*, int, ...) = NULL;
211 if (pkgid == NULL || dirpath == NULL)
214 handle = dlopen(LIB_PRIVILEGE_CONTROL, RTLD_LAZY | RTLD_GLOBAL);
216 _E( "setup path: dlopen() failed. [%s]", dlerror());
220 app_setup_path = dlsym(handle, "app_setup_path");
222 if ((errmsg != NULL) || (app_setup_path == NULL)) {
223 _E( "setup path: dlsym() failed. [%s]", errmsg);
228 if (groupid == NULL) {
229 _E( "[smack] app_setup_path(%s, %s, %d)", pkgid, dirpath, apppathtype);
230 ret = app_setup_path(pkgid, dirpath, apppathtype);
231 _E( "[smack] app_setup_path(), result = [%d]", ret);
233 _E( "[smack] app_setup_path(%s, %s, %d, %s)", pkgid, dirpath, apppathtype, groupid);
234 ret = app_setup_path(pkgid, dirpath, apppathtype, groupid);
235 _E( "[smack] app_setup_path(), result = [%d]", ret);
242 int __pkg_smack_add_friend(const char *pkgid1, const char *pkgid2)
247 int (*app_add_friend)(const char*, const char*) = NULL;
249 if (pkgid1 == NULL || pkgid2 == NULL)
252 handle = dlopen(LIB_PRIVILEGE_CONTROL, RTLD_LAZY | RTLD_GLOBAL);
254 _E( "add friend: dlopen() failed. [%s]", dlerror());
258 app_add_friend = dlsym(handle, "app_add_friend");
260 if ((errmsg != NULL) || (app_add_friend == NULL)) {
261 _E( "add friend: dlsym() failed. [%s]", errmsg);
266 _E( "[smack] app_add_friend(%s, %s)", pkgid1, pkgid2);
267 ret = app_add_friend(pkgid1, pkgid2);
268 _E( "[smack] app_add_friend(%s, %s), result = [%d]", pkgid1, pkgid2, ret);
274 int __pkg_smack_change_smack_label(const char *path, const char *label,
277 if (path == NULL || label == NULL)
282 int (*smack_lsetlabel)(const char*, const char*, int) = NULL;
284 handle = dlopen(LIB_SMACK, RTLD_LAZY | RTLD_GLOBAL);
286 _E( "change smack label: dlopen() failed. [%s]", dlerror());
290 smack_lsetlabel = dlsym(handle, "smack_lsetlabel");
292 if ((errmsg != NULL) || (smack_lsetlabel == NULL)) {
293 _E( "change smack label: dlsym() failed. [%s]", errmsg);
298 _E( "[smack] smack_lsetlabel(%s, %s, %d)", path, label, label_type);
299 ret = smack_lsetlabel(path, label, label_type);
300 _E( "[smack] smack_lsetlabel(%s, %s, %d), result = [%d]", path, label, label_type, ret);
306 static void __apply_shared_privileges(char *pkgname, int flag)
308 char dirpath[BUFF_SIZE] = {'\0'};
309 /*execute privilege APIs. The APIs should not fail*/
310 __pkg_smack_register_package(pkgname);
313 /*home dir. Dont setup path but change smack access to "_" */
314 snprintf(dirpath, BUFF_SIZE, "/usr/apps/%s", pkgname);
315 if (__is_dir(dirpath))
316 __pkg_smack_change_smack_label(dirpath, "_", 0);/*0 is SMACK_LABEL_ACCESS*/
317 memset(dirpath, '\0', BUFF_SIZE);
318 snprintf(dirpath, BUFF_SIZE, "/opt/usr/apps/%s", pkgname);
319 if (__is_dir(dirpath))
320 __pkg_smack_change_smack_label(dirpath, "_", 0);/*0 is SMACK_LABEL_ACCESS*/
321 memset(dirpath, '\0', BUFF_SIZE);
323 /*/shared dir. Dont setup path but change smack access to "_" */
324 snprintf(dirpath, BUFF_SIZE, "/usr/apps/%s/shared", pkgname);
325 if (__is_dir(dirpath))
326 __pkg_smack_change_smack_label(dirpath, "_", 0);/*0 is SMACK_LABEL_ACCESS*/
327 memset(dirpath, '\0', BUFF_SIZE);
328 snprintf(dirpath, BUFF_SIZE, "/opt/usr/apps/%s/shared", pkgname);
329 if (__is_dir(dirpath))
330 __pkg_smack_change_smack_label(dirpath, "_", 0);/*0 is SMACK_LABEL_ACCESS*/
331 memset(dirpath, '\0', BUFF_SIZE);
333 /*/shared/res dir. Dont setup path but change smack access to "_" */
335 snprintf(dirpath, BUFF_SIZE, "/usr/apps/%s/shared/res", pkgname);
337 snprintf(dirpath, BUFF_SIZE, "/opt/usr/apps/%s/shared/res", pkgname);
338 if (__is_dir(dirpath))
339 __pkg_smack_setup_path(pkgname, dirpath, RPM_PATH_ANY_LABEL, "_");
340 memset(dirpath, '\0', BUFF_SIZE);
342 /*/shared/data dir. setup path and change group to 'app'*/
344 snprintf(dirpath, BUFF_SIZE, "/usr/apps/%s/shared/data", pkgname);
346 snprintf(dirpath, BUFF_SIZE, "/opt/usr/apps/%s/shared/data", pkgname);
347 if (__is_dir(dirpath)) {
348 chown(dirpath, APP_OWNER_ID, APP_GROUP_ID);
349 __pkg_smack_setup_path(pkgname, dirpath, RPM_PATH_PUBLIC_RO, NULL);
351 memset(dirpath, '\0', BUFF_SIZE);
353 snprintf(dirpath, BUFF_SIZE, "/opt/usr/apps/%s/shared/data", pkgname);
355 snprintf(dirpath, BUFF_SIZE, "/usr/apps/%s/shared/data", pkgname);
356 if (__is_dir(dirpath))
357 chown(dirpath, APP_OWNER_ID, APP_GROUP_ID);
358 __pkg_smack_setup_path(pkgname, dirpath, RPM_PATH_PUBLIC_RO, NULL);
363 static int __is_authorized()
365 /* pkg_init db should be called by as root privilege. */
367 uid_t uid = getuid();
368 if ((uid_t) 0 == uid)
374 static int __pkg_list_cb (const pkgmgrinfo_pkginfo_h handle, void *user_data)
379 ret = pkgmgrinfo_pkginfo_get_pkgid(handle, &pkgid);
381 printf("pkgmgrinfo_pkginfo_get_pkgid() failed\n");
384 __apply_shared_privileges(pkgid, 0);
389 static int __additional_rpm_for_smack()
391 char *pkgid = "ui-gadget::client";
392 char *perm[] = {"http://tizen.org/privilege/appsetting", NULL};
394 __apply_shared_privileges(pkgid, 0);
395 __pkg_smack_enable_permissions(pkgid, 1, perm, 1);
399 static int __find_rpm_for_smack()
402 pkgmgrinfo_pkginfo_filter_h handle = NULL;
404 ret = pkgmgrinfo_pkginfo_filter_create(&handle);
406 printf("pkginfo filter handle create failed\n");
410 ret = pkgmgrinfo_pkginfo_filter_add_string(handle,
411 PMINFO_PKGINFO_PROP_PACKAGE_TYPE, "rpm");
413 printf("pkgmgrinfo_pkginfo_filter_add_string() failed\n");
417 ret = pkgmgrinfo_pkginfo_filter_foreach_pkginfo(handle, __pkg_list_cb, NULL);
419 printf("pkgmgrinfo_pkginfo_filter_foreach_pkginfo() failed\n");
423 pkgmgrinfo_pkginfo_filter_destroy(handle);
427 int main(int argc, char *argv[])
431 if (!__is_authorized()) {
432 _E("You are not an authorized user!\n");
436 ret = __find_rpm_for_smack();
438 printf("__find_rpm_for_smack() failed\n");
440 ret = __additional_rpm_for_smack();
442 printf("__additional_rpm_for_smack() failed\n");