Add path registration to cmd line tool 34/116634/7
authorKrzysztof Jackiewicz <k.jackiewicz@samsung.com>
Wed, 22 Feb 2017 13:41:52 +0000 (14:41 +0100)
committerKrzysztof Jackiewicz <k.jackiewicz@samsung.com>
Thu, 2 Mar 2017 14:13:09 +0000 (15:13 +0100)
Change-Id: Id90a02eb8561976aa4f895838966d6104868923a

src/cmd/security-manager-cmd.cpp

index 017d79cc57098aa9af86e657da121518eaa1bfb0..aeec5875dcc52786ae4b5a4e10af2e29b9f2e73e 100644 (file)
@@ -69,6 +69,7 @@ static po::options_description getGenericOptions()
          ("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)")
+         ("paths,q", "register package paths")
          ;
     return opts;
 }
@@ -134,6 +135,36 @@ static po::options_description getUserOptions()
     return opts;
 }
 
+static po::options_description getPathsOptions()
+{
+    po::options_description opts("Paths options");
+    opts.add_options()
+         ("pkg,g", po::value<std::string>()->required(),
+          "package name for the application (required)")
+         /*
+          * multitoken: Specifies that the value can span multiple tokens.
+          *             So it is possible to pass values to an option like
+          *             this:
+          *             --path=/home/user dirtype
+          *             --path /home/user dirtype
+          *             --path="/home/user" dirtype
+          */
+         ("path,p", po::value< std::vector<std::string> >()->multitoken(),
+          "path for setting smack labels and, in case of rw_sensitive, registering as a sensitive directory\n"
+          "(may occur more than once).\n"
+          "Format: --path <path> <path type>\n"
+          "  where <path type> is: \trw, ro, public_ro, rw_others_ro, trusted_rw, rw_sensitive\n"
+          "  ('trusted_rw' requires author id)\n"
+          "example:\n"
+          "        \t--path=/home/user/app rw")
+         ("uid,u", po::value<uid_t>()->required(),
+          "user identifier number (required)")
+         ("install-type", po::value<std::string>(),
+          "type of installation (local, global, preloaded)")
+         ;
+    return opts;
+}
+
 static po::options_description getAllOptions()
 {
     po::options_description opts("Allowed options");
@@ -141,6 +172,7 @@ static po::options_description getAllOptions()
     opts.add(getInstallOptions());
     opts.add(getUninstallOptions());
     opts.add(getUserOptions());
+    opts.add(getPathsOptions());
 
     return opts;
 }
@@ -199,8 +231,8 @@ void parseCommandOptions(int argc, char *argv[],
     po::notify(vm);
 }
 
-static bool loadPaths(const std::vector<std::string> &paths,
-                      struct app_inst_req &req)
+template <typename Req>
+static bool loadPaths(const std::vector<std::string> &paths, Req &req)
 {
     if (paths.size() & 1) {
         std::cout << "Wrong number of tokens was given for path option." <<
@@ -299,6 +331,28 @@ static void parseUserOptions(int argc, char *argv[],
     }
 }
 
+static void parsePathsOptions(int argc, char *argv[],
+                              struct path_req &req,
+                              po::variables_map &vm)
+{
+    parseCommandOptions(argc, argv, getPathsOptions(), vm);
+
+    if (vm.count("pkg"))
+        req.pkgName = vm["pkg"].as<std::string>();
+    if (vm.count("path")) {
+        const std::vector<std::string> paths =
+            vm["path"].as<std::vector<std::string> >();
+        if (!loadPaths(paths, req)) {
+            po::error e("Error in parsing path arguments.");
+            throw e;
+        }
+    }
+    if (vm.count("uid"))
+        req.uid = vm["uid"].as<uid_t>();
+    if (vm.count("install-type"))
+        req.installationType = install_type_map.at(vm["install-type"].as<std::string>());
+}
+
 static int installApp(const struct app_inst_req &req)
 {
     int ret = EXIT_FAILURE;
@@ -375,6 +429,26 @@ static int manageUserOperation(const struct user_req &req, std::string operation
     return ret;
 }
 
+static int registerPaths(const struct path_req &req)
+{
+    int ret = EXIT_FAILURE;
+
+    ret = security_manager_paths_register(&req);
+    if (SECURITY_MANAGER_SUCCESS == ret) {
+        std::cout << "Paths registration for package " << req.pkgName <<
+                  " succeeded." << std::endl;
+        LogDebug("Paths registration for package " << req.pkgName << " succeeded.");
+    } else {
+        std::cout << "Failed to register paths for " << req.pkgName << ". " <<
+                  security_manager_strerror(static_cast<lib_retcode>(ret)) <<
+                  " (" << ret << ")." << std::endl;
+        LogError("Failed to register paths for package " << req.pkgName << ". " <<
+                 security_manager_strerror(static_cast<lib_retcode>(ret)) <<
+                 " (" << ret << ")." << std::endl);
+    }
+    return ret;
+}
+
 int main(int argc, char *argv[])
 {
     po::variables_map vm;
@@ -425,6 +499,15 @@ int main(int argc, char *argv[])
                 return EXIT_FAILURE;
             parseUserOptions(argc, argv, *req, vm);
             return manageUserOperation(*req, operation);
+        } else if (vm.count("paths")) {
+            struct path_req *req = nullptr;
+            LogDebug("Path registration command.");
+            if (security_manager_path_req_new(&req) != SECURITY_MANAGER_SUCCESS)
+                return EXIT_FAILURE;
+            std::unique_ptr<path_req, void(*)(path_req*)> req_ptr(
+                    req, security_manager_path_req_free);
+            parsePathsOptions(argc, argv, *req, vm);
+            return registerPaths(*req);
         } else {
             std::cout << "No command argument was given." << std::endl;
             usage(std::string(argv[0]));