From 50dd441a11440201e1da9da81a7103677e2282ad Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jos=C3=A9=20Bollo?= Date: Thu, 4 Feb 2016 15:43:49 +0100 Subject: [PATCH] Adding uninstallation of widgets MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Change-Id: I8558a77312590181de5313c89ea4c9bdb9b477c7 Signed-off-by: José Bollo --- src/CMakeLists.txt | 1 + src/afm-system-daemon.c | 30 ++++++++++- src/secmgr-wrap.c | 11 ++++ src/secmgr-wrap.h | 17 ++++--- src/simulation/security-manager.h | 3 ++ src/wgtpkg-install.c | 2 +- src/wgtpkg-uninstall.c | 102 ++++++++++++++++++++++++++++++++++++++ src/wgtpkg-uninstall.h | 20 ++++++++ 8 files changed, 176 insertions(+), 10 deletions(-) create mode 100644 src/wgtpkg-uninstall.c create mode 100644 src/wgtpkg-uninstall.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 9379142..0b055e8 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -75,6 +75,7 @@ add_library(wgtpkg wgtpkg-files.c wgtpkg-install.c wgtpkg-permissions.c + wgtpkg-uninstall.c wgtpkg-workdir.c wgtpkg-xmlsec.c wgtpkg-zip.c diff --git a/src/afm-system-daemon.c b/src/afm-system-daemon.c index f02ff3d..ab87c97 100644 --- a/src/afm-system-daemon.c +++ b/src/afm-system-daemon.c @@ -31,6 +31,7 @@ #include "afm-db.h" #include "wgt-info.h" #include "wgtpkg-install.h" +#include "wgtpkg-uninstall.h" static const char appname[] = "afm-system-daemon"; static const char *rootdir = NULL; @@ -117,7 +118,34 @@ static void on_install(struct jreq *jreq, struct json_object *req) static void on_uninstall(struct jreq *jreq, struct json_object *req) { - jbus_reply_error_s(jreq, "\"not yet implemented\""); + const char *idaver; + const char *root; + int rc; + + /* scan the request */ + switch (json_object_get_type(req)) { + case json_type_string: + idaver = json_object_get_string(req); + root = rootdir; + break; + case json_type_object: + idaver = j_get_string(req, "id", NULL); + if (idaver != NULL) { + root = j_get_string(req, "root", rootdir); + break; + } + default: + jbus_reply_error_s(jreq, error_bad_request); + return; + } + + /* install the widget */ + rc = uninstall_widget(idaver, root); + if (rc) { + jbus_reply_error_s(jreq, "\"uninstallation had error\""); + return; + } + jbus_reply_s(jreq, "true"); } static int daemonize() diff --git a/src/secmgr-wrap.c b/src/secmgr-wrap.c index 78680a5..fee9d64 100644 --- a/src/secmgr-wrap.c +++ b/src/secmgr-wrap.c @@ -80,6 +80,17 @@ int secmgr_install() return retcode(rc); } +int secmgr_uninstall() +{ + int rc; + assert(request != NULL); + rc = security_manager_app_uninstall(request); + if (rc != SECURITY_MANAGER_SUCCESS) + ERROR("security_manager_app_uninstall failed"); + secmgr_cancel(); + return retcode(rc); +} + int secmgr_permit(const char *permission) { int rc; diff --git a/src/secmgr-wrap.h b/src/secmgr-wrap.h index 106b1b5..db5d0bb 100644 --- a/src/secmgr-wrap.h +++ b/src/secmgr-wrap.h @@ -16,12 +16,13 @@ limitations under the License. */ -int secmgr_init(const char *id); -void secmgr_cancel(); -int secmgr_install(); -int secmgr_permit(const char *permission); -int secmgr_path_public_read_only(const char *pathname); -int secmgr_path_read_only(const char *pathname); -int secmgr_path_read_write(const char *pathname); +extern int secmgr_init(const char *id); +extern void secmgr_cancel(); +extern int secmgr_install(); +extern int secmgr_uninstall(); +extern int secmgr_permit(const char *permission); +extern int secmgr_path_public_read_only(const char *pathname); +extern int secmgr_path_read_only(const char *pathname); +extern int secmgr_path_read_write(const char *pathname); -int secmgr_prepare_exec(const char *appid); +extern int secmgr_prepare_exec(const char *appid); diff --git a/src/simulation/security-manager.h b/src/simulation/security-manager.h index 6bc379a..59c356d 100644 --- a/src/simulation/security-manager.h +++ b/src/simulation/security-manager.h @@ -52,6 +52,9 @@ static int diese = 0; #define security_manager_app_install(r) \ (printf("security_manager_app_install(%p)\n",r), SECURITY_MANAGER_SUCCESS) +#define security_manager_app_uninstall(r) \ + (printf("security_manager_app_uninstall(%p)\n",r), SECURITY_MANAGER_SUCCESS) + #define security_manager_prepare_app(a) \ (printf("security_manager_prepare_app(%s)\n",a), SECURITY_MANAGER_SUCCESS) diff --git a/src/wgtpkg-install.c b/src/wgtpkg-install.c index b8646fa..deb2741 100644 --- a/src/wgtpkg-install.c +++ b/src/wgtpkg-install.c @@ -137,7 +137,7 @@ static int install_icon(const struct wgt_desc *desc) int rc; create_directory(FWK_ICON_DIR, 0755, 1); - rc = snprintf(link, sizeof link, "%s/%s@%s", FWK_ICON_DIR, desc->id, desc->ver); + rc = snprintf(link, sizeof link, "%s/%s", FWK_ICON_DIR, desc->idaver); if (rc >= sizeof link) { ERROR("link to long in install_icon"); errno = EINVAL; diff --git a/src/wgtpkg-uninstall.c b/src/wgtpkg-uninstall.c new file mode 100644 index 0000000..7516c3f --- /dev/null +++ b/src/wgtpkg-uninstall.c @@ -0,0 +1,102 @@ +/* + Copyright 2015 IoT.bzh + + author: José Bollo + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +#define _GNU_SOURCE + +#include +#include +#include +#include +#include +#include + +#include "verbose.h" +#include "utils-dir.h" +#include "secmgr-wrap.h" + +/* uninstall the widget of idaver */ +int uninstall_widget(const char *idaver, const char *root) +{ + char *id; + char *ver; + char path[PATH_MAX]; + const char *at; + int rc, rc2; + + NOTICE("-- UNINSTALLING widget of id %s from %s --", idaver, root); + + /* find the last '@' of the id */ + at = strrchr(idaver, '@'); + if (at == NULL) { + ERROR("bad widget id '%s', no @", idaver); + errno = EINVAL; + return -1; + } + id = strndupa(idaver, at - idaver); + ver = strdupa(at + 1); + + /* compute the path */ + rc = snprintf(path, sizeof path, "%s/%s/%s", root, id, ver); + if (rc >= sizeof path) { + ERROR("bad widget id '%s', too long", idaver); + errno = EINVAL; + return -1; + } + + /* removes the directory of the application */ + rc = remove_directory(path, 1); + if (rc < 0) { + ERROR("error while removing directory '%s': %m", path); + return -1; + } + + /* removes the icon of the application */ + rc = snprintf(path, sizeof path, "%s/%s", FWK_ICON_DIR, idaver); + assert(rc < sizeof path); + rc = unlink(path); + if (rc < 0) + ERROR("can't removing '%s': %m", path); + + /* removes the parent directory if empty */ + rc2 = snprintf(path, sizeof path, "%s/%s", root, id); + assert(rc2 < sizeof path); + rc2 = rmdir(path); + if (rc < 0 && errno == ENOTEMPTY) + return rc; + if (rc < 0) { + ERROR("error while removing directory '%s': %m", path); + return -1; + } + + /* + * parent directory removed: last occurrence of the application + * uninstall it for the security-manager + */ + rc2 = secmgr_init(id); + if (rc2) { + ERROR("can't init security manager context"); + return -1; + } + rc2 = secmgr_uninstall(); + if (rc2) { + ERROR("can't uninstall security manager context"); + return -1; + } + return rc; +} + diff --git a/src/wgtpkg-uninstall.h b/src/wgtpkg-uninstall.h new file mode 100644 index 0000000..f3b4c5b --- /dev/null +++ b/src/wgtpkg-uninstall.h @@ -0,0 +1,20 @@ +/* + Copyright 2015 IoT.bzh + + author: José Bollo + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +extern int uninstall_widget(const char *idaver, const char *root); + -- 2.16.6