Add support for app uninstallation in cmd 70/112970/5
authorKrzysztof Jackiewicz <k.jackiewicz@samsung.com>
Wed, 1 Feb 2017 15:55:01 +0000 (16:55 +0100)
committerKrzysztof Jackiewicz <k.jackiewicz@samsung.com>
Wed, 1 Mar 2017 09:55:20 +0000 (10:55 +0100)
Change-Id: Ib4b072e30c117a10400aa0e0267522a318a52518

src/cmd/security-manager-cmd.cpp

index b7edf47de6c6fc225512a5461bf4a694f8f93bfe..017d79cc57098aa9af86e657da121518eaa1bfb0 100644 (file)
@@ -28,6 +28,7 @@
 #include <vector>
 #include <map>
 #include <string>
+#include <memory>
 
 #include <dpl/log/log.h>
 #include <dpl/singleton.h>
@@ -66,6 +67,7 @@ static po::options_description getGenericOptions()
     opts.add_options()
          ("help,h", "produce help message")
          ("install,i", "install an application")
+         ("uninstall,r", "uninstall an application")
          ("manage-users,m", po::value<std::string>(), "add or remove user, parameter is 'a' or 'add' (for add) and 'r' or 'remove' (for remove)")
          ;
     return opts;
@@ -108,6 +110,18 @@ static po::options_description getInstallOptions()
     return opts;
 }
 
+static po::options_description getUninstallOptions()
+{
+    po::options_description opts("Uninstall options");
+    opts.add_options()
+         ("app,a", po::value<std::string>()->required(),
+          "application name (required)")
+         ("uid,u", po::value<uid_t>()->required(),
+          "user identifier number (required)")
+         ;
+    return opts;
+}
+
 static po::options_description getUserOptions()
 {
     po::options_description opts("User management options");
@@ -125,6 +139,7 @@ static po::options_description getAllOptions()
     po::options_description opts("Allowed options");
     opts.add(getGenericOptions());
     opts.add(getInstallOptions());
+    opts.add(getUninstallOptions());
     opts.add(getUserOptions());
 
     return opts;
@@ -254,6 +269,18 @@ static void parseInstallOptions(int argc, char *argv[],
 
 }
 
+static void parseUninstallOptions(int argc, char *argv[],
+                                  struct app_inst_req &req,
+                                  po::variables_map &vm)
+{
+    parseCommandOptions(argc, argv, getUninstallOptions(), vm);
+
+    if (vm.count("app"))
+        req.appName = vm["app"].as<std::string>();
+    if (vm.count("uid"))
+        req.uid = vm["uid"].as<uid_t>();
+}
+
 static void parseUserOptions(int argc, char *argv[],
                              struct user_req &req,
                              po::variables_map &vm)
@@ -293,6 +320,27 @@ static int installApp(const struct app_inst_req &req)
     return ret;
 }
 
+static int uninstallApp(const struct app_inst_req &req)
+{
+    int ret = EXIT_FAILURE;
+
+    ret = security_manager_app_uninstall(&req);
+    if (SECURITY_MANAGER_SUCCESS == ret) {
+        std::cout << "Application " << req.appName <<
+                  " uninstalled successfully." << std::endl;
+        LogDebug("Application " << req.appName <<
+                 " uninstalled successfully.");
+    } else {
+        std::cout << "Failed to uninstall " << req.appName << " application: " <<
+                  security_manager_strerror(static_cast<lib_retcode>(ret)) <<
+                  " (" << ret << ")." << std::endl;
+        LogError("Failed to uninstall " << req.appName << " application: " <<
+                 security_manager_strerror(static_cast<lib_retcode>(ret)) <<
+                 " (" << ret << ")." << std::endl);
+    }
+    return ret;
+}
+
 static int manageUserOperation(const struct user_req &req, std::string operation)
 {
     int ret = EXIT_FAILURE;
@@ -360,6 +408,15 @@ int main(int argc, char *argv[])
                 return EXIT_FAILURE;
             parseInstallOptions(argc, argv, *req, vm);
             return installApp(*req);
+        } else if (vm.count("uninstall")) {
+            struct app_inst_req *req = nullptr;
+            LogDebug("Uninstall command.");
+            if (security_manager_app_inst_req_new(&req) != SECURITY_MANAGER_SUCCESS)
+                return EXIT_FAILURE;
+            std::unique_ptr<app_inst_req, void(*)(app_inst_req*)> req_ptr(
+                    req, security_manager_app_inst_req_free);
+            parseUninstallOptions(argc, argv, *req, vm);
+            return uninstallApp(*req);
         } else if (vm.count("manage-users")) {
             std::string operation = vm["manage-users"].as<std::string>();
             struct user_req *req = nullptr;