Fix passing uid in pkg_initdb 44/85544/2
authorTomasz Iwanek <t.iwanek@samsung.com>
Thu, 25 Aug 2016 14:17:17 +0000 (16:17 +0200)
committerTomasz Iwanek <t.iwanek@samsung.com>
Fri, 26 Aug 2016 14:04:33 +0000 (07:04 -0700)
To verify, run as root:
 pkg_initdb -u 5001
tool should process package installed for owner user.

Change-Id: Ie95ce4966386cb98acc2175359b1a9306b627146

src/pkg_initdb/pkg_initdb.cc

index 0df326b..9402ec3 100644 (file)
@@ -27,8 +27,9 @@ namespace ci = common_installer;
 
 namespace {
 
-const uid_t kUserRoot = 0;
-const uid_t kGlobalUser = tzplatform_getuid(TZ_SYS_GLOBALAPP_USER);
+const uid_t kRootUserUid = 0;
+const uid_t kAppfwUserUid = 301;
+const uid_t kGlobalUserUid = tzplatform_getuid(TZ_SYS_GLOBALAPP_USER);
 const char kPkgInstallManifestPath[] = "/usr/bin/pkg-install-manifest";
 const char kBackendDirectoryPath[] = "/etc/package-manager/backend/";
 
@@ -39,16 +40,22 @@ int InstallManifestOffline(const std::string& pkgid,
   bf::path backend_path(kBackendDirectoryPath);
   backend_path /= type;
   ci::Subprocess backend(backend_path.string());
-  backend.set_uid(uid);
-  if (preload)
+  if (preload) {
+    if (uid != kRootUserUid) {
+      std::cerr << "Preload request for non-root user" << std::endl;
+      return -1;
+    }
     backend.Run("-y", pkgid.c_str(), "--preload");
-  else
-    backend.Run("-y", pkgid.c_str());
+  } else {
+    backend.set_uid(kAppfwUserUid);
+    std::string str_uid = std::to_string(uid);
+    backend.Run("-y", pkgid.c_str(), "-u", str_uid.c_str());
+  }
   return backend.Wait();
 }
 
 bool IsGlobal(uid_t uid) {
-  return uid == kUserRoot || uid == kGlobalUser;
+  return uid == kRootUserUid || uid == kGlobalUserUid;
 }
 
 void InitdbLoadDirectory(uid_t uid, const bf::path& directory, bool preload) {
@@ -113,7 +120,7 @@ void RemoveOldDatabases(uid_t uid) {
 }  // namespace
 
 int main(int argc, char *argv[]) {
-  if (getuid() != kUserRoot) {
+  if (getuid() != kRootUserUid) {
     std::cerr << "This binary should be run as root user" << std::endl;
     return -1;
   }
@@ -123,7 +130,7 @@ int main(int argc, char *argv[]) {
   uid_t uid;
   try {
     options.add_options()
-        ("uid,u", bpo::value<int>()->default_value(kUserRoot), "user id")
+        ("uid,u", bpo::value<int>()->default_value(kRootUserUid), "user id")
         ("help,h", "display this help message");
     bpo::store(bpo::parse_command_line(argc, argv, options), opt_map);
     if (opt_map.count("help")) {