Ejemplo n.º 1
0
    def MakeDeployableBinary(self, template_path, output_path):
        """This will add the config to the client template."""
        context = self.context + ["Client Context"]
        utils.EnsureDirExists(os.path.dirname(output_path))

        client_config_data = self.GetClientConfig(context)
        shutil.copyfile(template_path, output_path)
        zip_file = zipfile.ZipFile(output_path, mode="a")
        zip_info = zipfile.ZipInfo(filename="config.yaml")
        zip_file.writestr(zip_info, client_config_data)
        zip_file.close()
        return output_path
Ejemplo n.º 2
0
    def _GenerateInitConfigs(self, template_dir, rpm_build_dir):
        """Generates init-system configs."""
        client_name = config.CONFIG.Get("Client.name", context=self.context)
        initd_target_filename = os.path.join(rpm_build_dir, "etc/init.d",
                                             client_name)

        # Generate init.d
        utils.EnsureDirExists(os.path.dirname(initd_target_filename))
        self.GenerateFile(
            os.path.join(template_dir, "rpmbuild/grr-client.initd.in"),
            initd_target_filename)

        # Generate systemd unit
        if config.CONFIG["Template.version_numeric"] >= 3125:
            systemd_target_filename = os.path.join(rpm_build_dir,
                                                   "usr/lib/systemd/system/",
                                                   "%s.service" % client_name)

            utils.EnsureDirExists(os.path.dirname(systemd_target_filename))
            self.GenerateFile(
                os.path.join(template_dir, "rpmbuild/grr-client.service.in"),
                systemd_target_filename)
Ejemplo n.º 3
0
    def CopyFiles(self):
        """This sets up the template directory."""
        # Copy the nanny binary.
        shutil.copy(
            package.ResourcePath(
                "grr-response-core",
                "install_data/debian/dpkg_client/nanny.sh.in"),
            self.output_dir)

        dpkg_dir = config.CONFIG.Get("PyInstaller.dpkg_root",
                                     context=self.context)

        # Copy files needed for dpkg-buildpackage.
        shutil.copytree(
            config_lib.Resource().Filter(
                "install_data/debian/dpkg_client/debian"),
            os.path.join(dpkg_dir, "debian/debian.in"))

        # Copy upstart files
        outdir = os.path.join(dpkg_dir, "debian/upstart.in")
        utils.EnsureDirExists(outdir)
        shutil.copy(
            config_lib.Resource().Filter(
                "install_data/debian/dpkg_client/upstart/grr-client.conf"),
            outdir)

        # Copy init files
        outdir = os.path.join(dpkg_dir, "debian/initd.in")
        utils.EnsureDirExists(outdir)
        shutil.copy(
            config_lib.Resource().Filter(
                "install_data/debian/dpkg_client/initd/grr-client"), outdir)

        # Copy systemd unit file
        outdir = os.path.join(dpkg_dir, "debian/systemd.in")
        utils.EnsureDirExists(outdir)
        shutil.copy(
            config_lib.Resource().Filter(
                "install_data/systemd/client/grr-client.service"), outdir)
Ejemplo n.º 4
0
def _CopyNonFleetspeakDpkgFiles(dist_dir, package_dir):
    """Copies non-Fleetspeak-enabled DPKG files to template directory."""

    # Copy the nanny binary.
    shutil.copy(
        package.ResourcePath("grr-response-core",
                             "install_data/debian/dpkg_client/nanny.sh.in"),
        dist_dir)

    # Copy the wrapper script.
    shutil.copy(
        package.ResourcePath("grr-response-core",
                             "install_data/wrapper.sh.in"), dist_dir)

    # Copy files needed for dpkg-buildpackage.
    shutil.copytree(
        config_lib.Resource().Filter("install_data/debian/dpkg_client/debian"),
        os.path.join(package_dir, "debian/legacy-debian.in"))

    # Copy upstart files
    outdir = os.path.join(package_dir, "debian/upstart.in")
    utils.EnsureDirExists(outdir)
    shutil.copy(
        config_lib.Resource().Filter(
            "install_data/debian/dpkg_client/upstart/grr-client.conf"), outdir)

    # Copy init files
    outdir = os.path.join(package_dir, "debian/initd.in")
    utils.EnsureDirExists(outdir)
    shutil.copy(
        config_lib.Resource().Filter(
            "install_data/debian/dpkg_client/initd/grr-client"), outdir)

    # Copy systemd unit file
    outdir = os.path.join(package_dir, "debian/systemd.in")
    utils.EnsureDirExists(outdir)
    shutil.copy(
        config_lib.Resource().Filter(
            "install_data/systemd/client/grr-client.service"), outdir)
Ejemplo n.º 5
0
def _CopyNonFleetspeakRpmFiles(package_dir):
  """Copies non-Fleetspeak-enabled RPM files into the template folder."""

  utils.EnsureDirExists(os.path.join(package_dir, "legacy/rpmbuild"))

  shutil.copy(
      config_lib.Resource().Filter("install_data/centos/grr-client.initd.in"),
      os.path.join(package_dir, "legacy/rpmbuild/grr-client.initd.in"))
  shutil.copy(
      config_lib.Resource().Filter(
          "install_data/systemd/client/grr-client.service"),
      os.path.join(package_dir, "legacy/rpmbuild/grr-client.service.in"))

  shutil.copy(config_lib.Resource().Filter("install_data/centos/grr.spec.in"),
              os.path.join(package_dir, "legacy/rpmbuild/grr.spec.in"))
Ejemplo n.º 6
0
Archivo: linux.py Proyecto: x35029/grr
 def MakeExecutableTemplate(self, output_file=None):
     super(LinuxClientBuilder,
           self).MakeExecutableTemplate(output_file=output_file)
     self.MakeBuildDirectory()
     self.CleanDirectory(self.package_dir)
     self.BuildWithPyInstaller()
     self.CopyMissingModules()
     self.CopyFiles()
     if self.fleetspeak_enabled:
         # Include the Fleetspeak service config in the template.
         fleetspeak_dir = os.path.join(self.package_dir, "fleetspeak")
         utils.EnsureDirExists(fleetspeak_dir)
         shutil.copy(
             config.CONFIG.Get("ClientBuilder.fleetspeak_config_path",
                               context=self.context), fleetspeak_dir)
     self.MakeZip(self.package_dir, self.template_file)
Ejemplo n.º 7
0
Archivo: build.py Proyecto: x35029/grr
 def _GenerateFleetspeakConfig(self, template_dir, rpm_build_dir):
   """Generates a Fleetspeak config for GRR."""
   source_config = os.path.join(
       template_dir, "fleetspeak",
       os.path.basename(
           config.CONFIG.Get("ClientBuilder.fleetspeak_config_path")))
   fleetspeak_service_dir = config.CONFIG.Get(
       "ClientBuilder.fleetspeak_service_dir", context=self.context)
   dest_config_dir = os.path.join(rpm_build_dir, fleetspeak_service_dir[1:])
   utils.EnsureDirExists(dest_config_dir)
   dest_config_path = os.path.join(
       dest_config_dir,
       config.CONFIG.Get(
           "Client.fleetspeak_unsigned_config_fname", context=self.context))
   self.GenerateFile(
       input_filename=source_config, output_filename=dest_config_path)
Ejemplo n.º 8
0
    def GenerateDirectory(self,
                          input_dir=None,
                          output_dir=None,
                          replacements=None):
        input_dir = utils.NormalizePath(input_dir)
        output_dir = utils.NormalizePath(output_dir)
        replacements = replacements or []

        for (root, _, files) in os.walk(input_dir):
            for filename in files:
                in_file = utils.JoinPath(root, filename)
                out_file = in_file.replace(input_dir, output_dir)
                for (s, replacement) in replacements:
                    out_file = out_file.replace(s, replacement)
                utils.EnsureDirExists(os.path.dirname(out_file))
                self.GenerateFile(in_file, out_file)
Ejemplo n.º 9
0
def _CopyFleetspeakDpkgFiles(package_dir, context=None):
    """Copies Fleetspeak-enabled DPKG files to template directory."""
    if context is None:
        raise ValueError("context can't be None")

    # Copy files needed for dpkg-buildpackage.
    shutil.copytree(
        config_lib.Resource().Filter(
            "install_data/debian/dpkg_client/fleetspeak-debian"),
        os.path.join(package_dir, "debian/fleetspeak-debian.in"))

    # Include the Fleetspeak service config in the template.
    fleetspeak_dir = os.path.join(package_dir, "fleetspeak")
    utils.EnsureDirExists(fleetspeak_dir)
    shutil.copy(
        config.CONFIG.Get("ClientBuilder.fleetspeak_config_path",
                          context=context), fleetspeak_dir)
Ejemplo n.º 10
0
def _CreateServiceConfig(config_dir: str) -> None:
    """Creates a fleetspeak service config in the config directory."""
    service_config_path = config.CONFIG["ClientBuilder.fleetspeak_config_path"]
    with open(service_config_path, "r") as f:
        data = config.CONFIG.InterpolateValue(f.read())
        service_config = text_format.Parse(data,
                                           fs_system_pb2.ClientServiceConfig())
    daemon_config = fs_daemon_config_pb2.Config()
    service_config.config.Unpack(daemon_config)
    del daemon_config.argv[:]
    daemon_config.argv.extend([
        "grr_client",
    ])
    service_config.config.Pack(daemon_config)
    utils.EnsureDirExists(os.path.join(config_dir, "textservices"))
    with open(os.path.join(config_dir, "textservices", "GRR.textproto"),
              "w") as f:
        f.write(text_format.MessageToString(service_config))
Ejemplo n.º 11
0
    def MakeExecutableTemplate(self, output_file=None):
        """Create the executable template.

    The client is built in two phases. First an executable template is created
    with the client binaries contained inside a zip file. Then the installation
    package is created by appending the SFX extractor to this template and
    writing a config file into the zip file.  This technique allows the
    client build to be carried out once on the supported platform (e.g.
    windows with MSVS), but the deployable installer can be build on any
    platform which supports python.  Subclasses for each OS do the actual
    work, we just make sure the output directory is set up correctly here.

    Args:
      output_file: string filename where we will write the template.
    """
        self.template_file = output_file or config.CONFIG.Get(
            "ClientBuilder.template_path", context=self.context)
        utils.EnsureDirExists(os.path.dirname(self.template_file))
Ejemplo n.º 12
0
def GenerateDirectory(input_dir = None,
                      output_dir = None,
                      replacements = None,
                      context = None):
  """Copies an a directory rewriting file names according to spec."""
  if context is None:
    raise ValueError("context must be provided")

  input_dir = utils.NormalizePath(input_dir)
  output_dir = utils.NormalizePath(output_dir)
  replacements = replacements or []

  for (root, _, files) in os.walk(input_dir):
    for filename in files:
      in_file = utils.JoinPath(root, filename)
      out_file = in_file.replace(input_dir, output_dir)
      for (s, replacement) in replacements:
        out_file = out_file.replace(s, replacement)
      utils.EnsureDirExists(os.path.dirname(out_file))
      GenerateFile(in_file, out_file, context=context)
Ejemplo n.º 13
0
 def _GenerateFleetspeakConfig(self, build_dir):
   """Generates a Fleetspeak config for GRR in the debian build dir."""
   source_config = os.path.join(
       build_dir, "dist", "fleetspeak",
       os.path.basename(
           config.CONFIG.Get(
               "ClientBuilder.fleetspeak_config_path", context=self.context)))
   fleetspeak_service_dir = config.CONFIG.Get(
       "ClientBuilder.fleetspeak_service_dir", context=self.context)
   package_name = config.CONFIG.Get(
       "ClientBuilder.package_name", context=self.context)
   dest_config_dir = os.path.join(build_dir, "dist", "debian", package_name,
                                  fleetspeak_service_dir[1:])
   utils.EnsureDirExists(dest_config_dir)
   dest_config_path = os.path.join(
       dest_config_dir,
       config.CONFIG.Get(
           "Client.fleetspeak_unsigned_config_fname", context=self.context))
   self.GenerateFile(
       input_filename=source_config, output_filename=dest_config_path)
Ejemplo n.º 14
0
 def _GenerateFleetspeakConfig(self, build_dir, dest_config_dir):
     """Generates a Fleetspeak config for GRR in the debian build dir."""
     # We need to strip leading /'s or .join will ignore everything that comes
     # before it.
     dest_config_dir = dest_config_dir.lstrip("/")
     source_config = os.path.join(
         build_dir, "dist", "fleetspeak",
         os.path.basename(
             config.CONFIG.Get("ClientBuilder.fleetspeak_config_path",
                               context=self.context)))
     dest_config = os.path.join(
         build_dir, "dist", "debian",
         config.CONFIG.Get("ClientBuilder.package_name",
                           context=self.context), dest_config_dir,
         config.CONFIG.Get("Client.fleetspeak_unsigned_config_fname",
                           context=self.context))
     utils.EnsureDirExists(os.path.dirname(dest_config))
     build_helpers.GenerateFile(input_filename=source_config,
                                output_filename=dest_config,
                                context=self.context)
Ejemplo n.º 15
0
    def _MakeDeployableBinaryV2(self, template_path, output_path):
        context = self.context + ["Client Context"]
        utils.EnsureDirExists(os.path.dirname(output_path))

        fleetspeak_enabled = config.CONFIG.Get("Client.fleetspeak_enabled",
                                               context=self.context)
        fleetspeak_bundled = config.CONFIG.Get(
            "ClientBuilder.fleetspeak_bundled", context=self.context)

        with contextlib.ExitStack() as stack:
            tmp_dir = stack.enter_context(utils.TempDirectory())
            shutil.unpack_archive(template_path, tmp_dir, format="zip")

            if fleetspeak_bundled:
                variant = "fleetspeak-bundled"
            elif fleetspeak_enabled:
                variant = "fleetspeak-enabled"
            else:
                variant = "legacy"

            pkg_utils.JoinPkg(os.path.join(tmp_dir, variant),
                              os.path.join(tmp_dir, "blocks"), output_path)

            zf = stack.enter_context(zipfile.ZipFile(output_path, mode="a"))
            with open(os.path.join(tmp_dir, "build.yaml"),
                      "r") as build_yaml_file:
                zf.writestr("build.yaml", build_yaml_file.read())

            client_config_data = build_helpers.GetClientConfig(context)
            zf.writestr("config.yaml", client_config_data)

            if fleetspeak_bundled:
                fleetspeak_client_config = config.CONFIG.Get(
                    "ClientBuilder.fleetspeak_client_config",
                    context=self.context)
                with open(fleetspeak_client_config,
                          "r") as fleetspeak_client_config_file:
                    zf.writestr("client.config",
                                fleetspeak_client_config_file.read())

        return output_path
Ejemplo n.º 16
0
    def MakeDeployableBinary(self, template_path: str,
                             output_path: str) -> str:
        context = self.context + ["Client Context"]
        utils.EnsureDirExists(os.path.dirname(output_path))

        def GetConfig(name: str) -> Any:
            return config.CONFIG.Get(name, context=self.context)

        fleetspeak_enabled = GetConfig("Client.fleetspeak_enabled")
        fleetspeak_bundled = GetConfig("ClientBuilder.fleetspeak_bundled")

        legacy = not (fleetspeak_enabled or fleetspeak_bundled)

        with contextlib.ExitStack() as stack:
            tmp_dir = stack.enter_context(utils.TempDirectory())
            shutil.unpack_archive(template_path, tmp_dir, format="zip")
            msi_file = MsiFile(os.path.join(tmp_dir, "installer.msi"))

            def EnableFeature(name: str) -> None:
                msi_file.EnableFeature(name.encode("utf-8"))

            def ReplaceString(src: str, dst: str) -> None:
                msi_file.ReplaceString(src.encode("utf-8"),
                                       dst.encode("utf-8"))

            def RenameFile(src: str, dst: str) -> None:
                msi_file.RenameFile(src.encode("utf-8"), dst.encode("utf-8"))

            def ReplaceStringConfig(src: str, dst: str) -> None:
                ReplaceString(src, GetConfig(dst))

            def RenameFileConfig(src: str, dst: str) -> None:
                RenameFile(src, GetConfig(dst))

            # Set product information

            ReplaceStringConfig("__ProductName", "Client.name")
            ReplaceStringConfig("__ProductManufacturer", "Client.company_name")

            # Enable features

            if GetConfig("ClientBuilder.console"):
                EnableFeature("DbgGrrExe")
            else:
                EnableFeature("GrrExe")

            if legacy:
                if GetConfig("ClientBuilder.console"):
                    EnableFeature("DbgNanny")
                else:
                    EnableFeature("Nanny")

            if fleetspeak_bundled:
                EnableFeature("FleetspeakClient")

            if fleetspeak_enabled or fleetspeak_bundled:
                EnableFeature("FleetspeakServiceRegistryEntry")

            # Rename directories

            RenameFileConfig("__GrrDirectory", "Client.name")
            RenameFileConfig("__GrrVersion", "Source.version_string")

            # Rename files

            if GetConfig("ClientBuilder.console"):
                RenameFileConfig("__dbg_grr-client.exe", "Client.binary_name")
                RenameFileConfig("__dbg_GRRService.exe",
                                 "Nanny.service_binary_name")
            else:
                RenameFileConfig("__grr-client.exe", "Client.binary_name")
                RenameFileConfig("__GRRService.exe",
                                 "Nanny.service_binary_name")

            # Write Configs

            if fleetspeak_bundled:
                with open(GetConfig("ClientBuilder.fleetspeak_client_config"),
                          "rb") as f:
                    msi_file.SetFleetspeakConfig(f.read())

            RenameFileConfig("grr-config.yaml",
                             "ClientBuilder.config_filename")
            msi_file.SetGrrConfig(
                build_helpers.GetClientConfig(context).encode("utf-8"))

            # Write Fleetspeak service registry data

            if fleetspeak_enabled or fleetspeak_bundled:
                key_name = GetConfig(
                    "Client.fleetspeak_unsigned_services_regkey")
                key_name = key_name.replace("HKEY_LOCAL_MACHINE\\", "")
                ReplaceString("__FleetspeakServiceRegistryKey", key_name)
                ReplaceStringConfig("__FleetspeakServiceRegistryName",
                                    "Client.name")
                ReplaceString(
                    "__FleetspeakServiceRegistryValue",
                    f"[INSTALLDIR]{GetConfig('Client.fleetspeak_unsigned_config_fname')}"
                )

            if fleetspeak_bundled:
                ReplaceStringConfig("FleetspeakClientService",
                                    "Client.fleetspeak_service_name")

            # Write Fleetspeak service config

            # If we don't need to re-write the file after installation, just run
            # a dummy command.
            gen_fleespeak_service_file_cmd = "cmd.exe /c exit"

            if fleetspeak_enabled or fleetspeak_bundled:
                path = GetConfig("ClientBuilder.fleetspeak_config_path")
                with open(path, "rb") as f:
                    msi_file.SetFleetspeakServiceConfig(f.read())
                RenameFileConfig("fleetspeak-service-config.txt",
                                 "Client.fleetspeak_unsigned_config_fname")
                if path.endswith(".in"):
                    args = [
                        "[INSTALLDIR]" + GetConfig("Client.binary_name"),
                        "--config",
                        "[INSTALLDIR]" +
                        GetConfig("ClientBuilder.config_filename"),
                        "-p",
                        "Client.install_path=[INSTALLDIR]",
                        "--install",
                        "--interpolate_fleetspeak_service_config",
                        "[INSTALLDIR]" +
                        GetConfig("Client.fleetspeak_unsigned_config_fname"),
                    ]
                    gen_fleespeak_service_file_cmd = subprocess.list2cmdline(
                        args)

            ReplaceString("__GenFleetspeakServiceFileCmd",
                          gen_fleespeak_service_file_cmd)

            # Configure nanny service

            if legacy:
                nanny_args = ["--service_key", GetConfig("Client.config_key")]
                ReplaceString("__NannyArguments",
                              subprocess.list2cmdline(nanny_args))
                ReplaceStringConfig("__NannyServiceDescription",
                                    "Nanny.service_description")
                if GetConfig("ClientBuilder.console"):
                    ReplaceStringConfig("__DbgNannyRegistryKey",
                                        "Client.config_key")
                    ReplaceStringConfig("__DbgNannyServiceName",
                                        "Nanny.service_name")

                else:
                    ReplaceStringConfig("__NannyRegistryKey",
                                        "Client.config_key")
                    ReplaceStringConfig("__NannyServiceName",
                                        "Nanny.service_name")
                grr_binary = GetConfig("Client.binary_name")
                grr_config = GetConfig("ClientBuilder.config_filename")
                ReplaceString("__NannyChildBinary",
                              f"[INSTALLDIR]{grr_binary}")
                child_args = [
                    f"[INSTALLDIR]{grr_binary}", "--config",
                    f"[INSTALLDIR]{grr_config}"
                ]
                ReplaceString("__NannyChildCommandLine",
                              subprocess.list2cmdline(child_args))

            msi_file.Write()
            msi_file.Close()

            if os.path.exists(output_path):
                os.remove(output_path)
            shutil.move(os.path.join(tmp_dir, "installer.msi"), output_path)

        return output_path
Ejemplo n.º 17
0
def SplitPkg(pkg_path: str, dst_dir: str, blocks_dir: str) -> None:
    """Decomposes a pkg file into a pair of directories.

  Args:
    pkg_path: Path to input .pkg file.
    dst_dir: Destination directory.
    blocks_dir: Directory to write blocks into.

  Raises:
    RuntimeError: if called on a system different than OSX.

  A pkg file is decomposed and stored into 2 directories (`dst_dir`,
  `blocks_dir`):

   * `dst_dir/header`: XAR binary header
   * `dst_dir/toc`: XAR XML table of contents
   * `dst_dir/files/`: all the files contained in the XAR EXCEPT payload,
      directories flattened.
   * `dst_dir/payload`: a list of files in the `blocks_dir/` directory, which
      if concatenated, produce the `Payload` file
   * `blocks_dir/`: contains fragments of the gunzip'ed `Payload` file

  The `Payload` file is decompressed, decomposed into blocks and stored as
  `blocks_dir/<sha1 of block>`. For each file in the CPIO archive 2 blocks are
  created, one for the ASCI header and another for the (file name,
  file contents) part.

  The `blocks_dir/` directory can be shared by multiple packages, resulting in
  common files being stored once only.

  SplitPkg is implemented using OSX command line tools and will thus run on OSX
  only.
  """

    if platform.system() != "Darwin":
        raise RuntimeError("JoinPkg works only on Mac OSX.")

    utils.EnsureDirExists(dst_dir)
    utils.EnsureDirExists(blocks_dir)

    files_root = os.path.join(dst_dir, "files")
    utils.EnsureDirExists(files_root)

    tmp_files_root = os.path.join(dst_dir, "_files")
    utils.EnsureDirExists(tmp_files_root)

    _ExtractXarHeader(pkg_path, os.path.join(dst_dir, "header"))

    command = ["xar", "--dump-toc=toc", "-f", pkg_path]
    subprocess.check_call(command, cwd=dst_dir)

    command = ["xar", "-x", "-f", pkg_path]
    subprocess.check_call(command, cwd=tmp_files_root)

    _FlattenFiles(tmp_files_root, files_root)

    _ExtractPayload(os.path.join(files_root, "Payload"),
                    os.path.join(dst_dir, "payload"), blocks_dir)
    os.unlink(os.path.join(files_root, "Payload"))

    with open(os.path.join(dst_dir, "name"), "w") as f:
        f.write(os.path.basename(pkg_path))
Ejemplo n.º 18
0
 def TmpPath(*components):
   result = os.path.join(self.tmp_dir, *components)
   utils.EnsureDirExists(os.path.dirname(result))
   return result
Ejemplo n.º 19
0
    def MakeDeployableBinary(self, template_path, output_path):
        """This will add the config to the client template and create a .rpm."""

        rpmbuild_binary = "/usr/bin/rpmbuild"
        if not os.path.exists(rpmbuild_binary):
            logging.error("rpmbuild not found, unable to repack client.")
            return

        with utils.TempDirectory() as tmp_dir:
            template_dir = os.path.join(tmp_dir, "dist")
            utils.EnsureDirExists(template_dir)

            zf = zipfile.ZipFile(template_path)
            for name in zf.namelist():
                dirname = os.path.dirname(name)
                utils.EnsureDirExists(os.path.join(template_dir, dirname))
                with open(os.path.join(template_dir, name), "wb") as fd:
                    fd.write(zf.read(name))

            # Set up a RPM building environment.

            rpm_root_dir = os.path.join(tmp_dir, "rpmbuild")

            rpm_build_dir = os.path.join(rpm_root_dir, "BUILD")
            utils.EnsureDirExists(rpm_build_dir)

            rpm_buildroot_dir = os.path.join(rpm_root_dir, "BUILDROOT")
            utils.EnsureDirExists(rpm_buildroot_dir)

            rpm_rpms_dir = os.path.join(rpm_root_dir, "RPMS")
            utils.EnsureDirExists(rpm_rpms_dir)

            rpm_specs_dir = os.path.join(rpm_root_dir, "SPECS")
            utils.EnsureDirExists(rpm_specs_dir)

            template_binary_dir = os.path.join(tmp_dir,
                                               "dist/rpmbuild/grr-client")

            target_binary_dir = "%s%s" % (rpm_build_dir,
                                          config.CONFIG.Get(
                                              "ClientBuilder.target_dir",
                                              context=self.context))

            utils.EnsureDirExists(os.path.dirname(target_binary_dir))
            try:
                shutil.rmtree(target_binary_dir)
            except OSError:
                pass
            shutil.move(template_binary_dir, target_binary_dir)
            client_name = config.CONFIG.Get("Client.name",
                                            context=self.context)
            client_binary_name = config.CONFIG.Get("Client.binary_name",
                                                   context=self.context)
            if client_binary_name != "grr-client":
                shutil.move(
                    os.path.join(target_binary_dir, "grr-client"),
                    os.path.join(target_binary_dir, client_binary_name))

            # Generate spec
            spec_filename = os.path.join(rpm_specs_dir,
                                         "%s.spec" % client_name)
            self.GenerateFile(
                os.path.join(tmp_dir, "dist/rpmbuild/grr.spec.in"),
                spec_filename)

            initd_target_filename = os.path.join(rpm_build_dir, "etc/init.d",
                                                 client_name)

            # Generate init.d
            utils.EnsureDirExists(os.path.dirname(initd_target_filename))
            self.GenerateFile(
                os.path.join(tmp_dir, "dist/rpmbuild/grr-client.initd.in"),
                initd_target_filename)

            # Generate systemd unit
            if config.CONFIG["Template.version_numeric"] >= 3125:
                systemd_target_filename = os.path.join(
                    rpm_build_dir, "usr/lib/systemd/system/",
                    "%s.service" % client_name)

                utils.EnsureDirExists(os.path.dirname(systemd_target_filename))
                self.GenerateFile(
                    os.path.join(tmp_dir,
                                 "dist/rpmbuild/grr-client.service.in"),
                    systemd_target_filename)

                # Generate prelinking blacklist file
                prelink_target_filename = os.path.join(rpm_build_dir,
                                                       "etc/prelink.conf.d",
                                                       "%s.conf" % client_name)

                utils.EnsureDirExists(os.path.dirname(prelink_target_filename))
                self.GenerateFile(
                    os.path.join(tmp_dir,
                                 "dist/rpmbuild/prelink_blacklist.conf.in"),
                    prelink_target_filename)

            # Create a client config.
            client_context = ["Client Context"] + self.context
            client_config_content = self.GetClientConfig(client_context)

            with open(
                    os.path.join(
                        target_binary_dir,
                        config.CONFIG.Get("ClientBuilder.config_filename",
                                          context=self.context)), "wb") as fd:
                fd.write(client_config_content)

            # Set the daemon to executable.
            os.chmod(os.path.join(target_binary_dir, client_binary_name),
                     0o755)

            client_arch = config.CONFIG.Get("Template.arch",
                                            context=self.context)
            if client_arch == "amd64":
                client_arch = "x86_64"

            command = [
                rpmbuild_binary, "--define", "_topdir " + rpm_root_dir,
                "--target", client_arch, "--buildroot", rpm_buildroot_dir,
                "-bb", spec_filename
            ]
            try:
                subprocess.check_output(command, stderr=subprocess.STDOUT)
            except subprocess.CalledProcessError as e:
                logging.error("Error calling %s.", command)
                logging.error(e.output)
                raise

            client_version = config.CONFIG.Get("Template.version_string",
                                               context=self.context)
            rpm_filename = os.path.join(
                rpm_rpms_dir, client_arch,
                "%s-%s-1.%s.rpm" % (client_name, client_version, client_arch))

            utils.EnsureDirExists(os.path.dirname(output_path))
            shutil.move(rpm_filename, output_path)

            logging.info("Created package %s", output_path)
            self.Sign(output_path)
            return output_path
Ejemplo n.º 20
0
    def MakeDeployableBinary(self, template_path, output_path):
        """This will add the config to the client template and create a .deb."""
        buildpackage_binary = "/usr/bin/dpkg-buildpackage"
        if not os.path.exists(buildpackage_binary):
            logging.error(
                "dpkg-buildpackage not found, unable to repack client.")
            return

        with utils.TempDirectory() as tmp_dir:
            template_dir = os.path.join(tmp_dir, "dist")
            utils.EnsureDirExists(template_dir)

            zf = zipfile.ZipFile(template_path)
            for name in zf.namelist():
                dirname = os.path.dirname(name)
                utils.EnsureDirExists(os.path.join(template_dir, dirname))
                with open(os.path.join(template_dir, name), "wb") as fd:
                    fd.write(zf.read(name))

            # Generate the dpkg files.
            self.GenerateDPKGFiles(tmp_dir)

            # Create a client config.
            client_context = ["Client Context"] + self.context
            client_config_content = self.GetClientConfig(client_context)

            # We need to strip leading /'s or .join will ignore everything that comes
            # before it.
            target_dir = config.CONFIG.Get("ClientBuilder.target_dir",
                                           context=self.context).lstrip("/")
            agent_dir = os.path.join(
                template_dir, "debian",
                config.CONFIG.Get("ClientBuilder.package_name",
                                  context=self.context), target_dir)

            with open(
                    os.path.join(
                        agent_dir,
                        config.CONFIG.Get("ClientBuilder.config_filename",
                                          context=self.context)), "wb") as fd:
                fd.write(client_config_content)

            # Set the daemon to executable.
            os.chmod(
                os.path.join(
                    agent_dir,
                    config.CONFIG.Get("Client.binary_name",
                                      context=self.context)), 0o755)

            arch = config.CONFIG.Get("Template.arch", context=self.context)

            try:
                old_working_dir = os.getcwd()
            except OSError:
                old_working_dir = os.environ.get("HOME", "/tmp")

            try:
                os.chdir(template_dir)
                command = [
                    buildpackage_binary, "-uc", "-d", "-b",
                    "-a%s" % arch
                ]

                try:
                    subprocess.check_output(command, stderr=subprocess.STDOUT)
                except subprocess.CalledProcessError as e:
                    if "Failed to sign" not in e.output:
                        logging.error("Error calling %s.", command)
                        logging.error(e.output)
                        raise

                filename_base = config.CONFIG.Get(
                    "ClientBuilder.debian_package_base", context=self.context)
                output_base = config.CONFIG.Get(
                    "ClientRepacker.output_basename", context=self.context)
            finally:
                try:
                    os.chdir(old_working_dir)
                except OSError:
                    pass

            utils.EnsureDirExists(os.path.dirname(output_path))

            for extension in [
                    ".changes",
                    config.CONFIG.Get("ClientBuilder.output_extension",
                                      context=self.context)
            ]:
                input_name = "%s%s" % (filename_base, extension)
                output_name = "%s%s" % (output_base, extension)

                shutil.move(
                    os.path.join(tmp_dir, input_name),
                    os.path.join(os.path.dirname(output_path), output_name))

            logging.info("Created package %s", output_path)
            return output_path
Ejemplo n.º 21
0
    def GenerateDPKGFiles(self, template_path):
        """Generates the files needed by dpkg-buildpackage."""

        # Rename the generated binaries to the correct name.
        template_binary_dir = os.path.join(template_path,
                                           "dist/debian/grr-client")
        package_name = config.CONFIG.Get("ClientBuilder.package_name",
                                         context=self.context)
        target_binary_dir = os.path.join(
            template_path,
            "dist/debian/%s%s" % (package_name,
                                  config.CONFIG.Get("ClientBuilder.target_dir",
                                                    context=self.context)))
        if package_name == "grr-client":
            # Need to rename the template path or the move will fail.
            shutil.move(template_binary_dir,
                        "%s-template" % template_binary_dir)
            template_binary_dir = "%s-template" % template_binary_dir

        utils.EnsureDirExists(os.path.dirname(target_binary_dir))
        shutil.move(template_binary_dir, target_binary_dir)

        shutil.move(
            os.path.join(target_binary_dir, "grr-client"),
            os.path.join(
                target_binary_dir,
                config.CONFIG.Get("Client.binary_name", context=self.context)))

        deb_in_dir = os.path.join(template_path, "dist/debian/debian.in/")

        self.GenerateDirectory(deb_in_dir,
                               os.path.join(template_path, "dist/debian"),
                               [("grr-client", package_name)])

        # Generate directories for the /usr/sbin link.
        utils.EnsureDirExists(
            os.path.join(template_path,
                         "dist/debian/%s/usr/sbin" % package_name))

        # Generate the nanny template. This only exists from client version 3.1.2.5
        # onwards.
        if config.CONFIG["Template.version_numeric"] >= 3125:
            self.GenerateFile(os.path.join(target_binary_dir, "nanny.sh.in"),
                              os.path.join(target_binary_dir, "nanny.sh"))

        # Generate the upstart template.
        self.GenerateFile(
            os.path.join(template_path,
                         "dist/debian/upstart.in/grr-client.conf"),
            os.path.join(template_path,
                         "dist/debian/%s.upstart" % package_name))

        # Generate the initd template. The init will not run if it detects upstart
        # is present.
        self.GenerateFile(
            os.path.join(template_path, "dist/debian/initd.in/grr-client"),
            os.path.join(template_path, "dist/debian/%s.init" % package_name))

        # Generate the systemd unit file.
        self.GenerateFile(
            os.path.join(template_path,
                         "dist/debian/systemd.in/grr-client.service"),
            os.path.join(template_path,
                         "dist/debian/%s.service" % package_name))

        # Clean up the template dirs.
        shutil.rmtree(deb_in_dir)
        shutil.rmtree(os.path.join(template_path, "dist/debian/upstart.in"))
        shutil.rmtree(os.path.join(template_path, "dist/debian/initd.in"))
        shutil.rmtree(os.path.join(template_path, "dist/debian/systemd.in"))
Ejemplo n.º 22
0
 def TmpDir(*components):
   result = TmpPath(*components)
   utils.EnsureDirExists(result)
   return result
Ejemplo n.º 23
0
    def _MakeSelfExtractingZip(self, payload_data, output_path):
        """Repack the installer into the payload.

    Args:
      payload_data: data payload for zip file
      output_path: filename for the zip output

    Raises:
      RuntimeError: if the ClientBuilder.unzipsfx_stub doesn't require admin.
    Returns:
      output_path: filename string of zip output file
    """
        context = self.context + ["Client Context"]

        src_zip = zipfile.ZipFile(io.BytesIO(payload_data), mode="r")
        zip_data = io.BytesIO()
        output_zip = zipfile.ZipFile(zip_data,
                                     mode="w",
                                     compression=zipfile.ZIP_DEFLATED)

        config_file_name = config.CONFIG.Get("ClientBuilder.config_filename",
                                             context=context)
        # Copy the rest of the files from the package to the new zip.
        for template_file in src_zip.namelist():
            if template_file != config_file_name:
                # Avoid writing the config file twice if we're repacking a binary that
                # has already been run through deployment. We write it in the next step,
                # so no need to copy over from the original here.
                _CopyFileInZip(src_zip, template_file, output_zip)

        client_config_content = build_helpers.GetClientConfig(context)
        self._ValidateEndConfig(client_config_content)

        output_zip.writestr(
            config_file_name,
            client_config_content.encode("utf-8"),  # pytype: disable=attribute-error
            compress_type=zipfile.ZIP_STORED)

        # The zip file comment is used by the self extractor to run the installation
        # script. Comment has to be `bytes` object because `zipfile` module is not
        # smart enough to properly handle `unicode` objects.
        output_zip.comment = b"$AUTORUN$>%s" % config.CONFIG.Get(
            "ClientBuilder.autorun_command_line",
            context=context).encode("utf-8")

        output_zip.close()

        utils.EnsureDirExists(os.path.dirname(output_path))
        with io.open(output_path, "wb") as fd:
            # First write the installer stub
            stub_data = io.BytesIO()
            unzipsfx_stub = config.CONFIG.Get("ClientBuilder.unzipsfx_stub",
                                              context=context)
            stub_raw = io.open(unzipsfx_stub, "rb").read()

            # Check stub has been compiled with the requireAdministrator manifest.
            if b"level=\"requireAdministrator" not in stub_raw:
                raise RuntimeError(
                    "Bad unzip binary in use. Not compiled with the"
                    "requireAdministrator manifest option.")

            stub_data.write(stub_raw)

            # If in verbose mode, modify the unzip bins PE header to run in console
            # mode for easier debugging.
            build_helpers.SetPeSubsystem(stub_data,
                                         console=config.CONFIG.Get(
                                             "ClientBuilder.console",
                                             context=context))

            # Now patch up the .rsrc section to contain the payload.
            end_of_file = zip_data.tell() + stub_data.tell()

            # This is the IMAGE_SECTION_HEADER.Name which is also the start of
            # IMAGE_SECTION_HEADER.
            offset_to_rsrc = stub_data.getvalue().find(b".rsrc")

            # IMAGE_SECTION_HEADER.PointerToRawData is a 32 bit int.
            stub_data.seek(offset_to_rsrc + 20)
            start_of_rsrc_section = struct.unpack("<I", stub_data.read(4))[0]

            # Adjust IMAGE_SECTION_HEADER.SizeOfRawData to span from the old start to
            # the end of file.
            stub_data.seek(offset_to_rsrc + 16)
            stub_data.write(
                struct.pack("<I", end_of_file - start_of_rsrc_section))

            # Concatenate stub and zip file.
            out_data = io.BytesIO()
            out_data.write(stub_data.getvalue())
            out_data.write(zip_data.getvalue())

            # Then write the actual output file.
            fd.write(out_data.getvalue())

        if self.signer:
            self.signer.SignFile(output_path)

        logging.info("Deployable binary generated at %s", output_path)

        return output_path
Ejemplo n.º 24
0
Archivo: osx.py Proyecto: thetraker/grr
 def RenamePkgToTemplate(self, output_file):
     print("Copying output to templates location: %s -> %s" %
           (self.prodbuild_out_binary, output_file))
     utils.EnsureDirExists(os.path.dirname(output_file))
     shutil.copyfile(self.prodbuild_out_binary, output_file)
Ejemplo n.º 25
0
    def MakeDeployableBinary(self, template_path, output_path):
        """This will add the config to the client template and create a .rpm."""

        rpmbuild_binary = "/usr/bin/rpmbuild"
        if not os.path.exists(rpmbuild_binary):
            logging.error("rpmbuild not found, unable to repack client.")
            return None

        with utils.TempDirectory() as tmp_dir:
            template_dir = os.path.join(tmp_dir, "dist")
            utils.EnsureDirExists(template_dir)

            zf = zipfile.ZipFile(template_path)
            for name in zf.namelist():
                dirname = os.path.dirname(name)
                utils.EnsureDirExists(os.path.join(template_dir, dirname))
                with io.open(os.path.join(template_dir, name), "wb") as fd:
                    fd.write(zf.read(name))

            self._ProcessUniversalTemplate(template_dir)

            # Set up a RPM building environment.

            rpm_root_dir = os.path.join(tmp_dir, "rpmbuild")

            rpm_build_dir = os.path.join(rpm_root_dir, "BUILD")
            utils.EnsureDirExists(rpm_build_dir)

            rpm_buildroot_dir = os.path.join(rpm_root_dir, "BUILDROOT")
            utils.EnsureDirExists(rpm_buildroot_dir)

            rpm_rpms_dir = os.path.join(rpm_root_dir, "RPMS")
            utils.EnsureDirExists(rpm_rpms_dir)

            rpm_specs_dir = os.path.join(rpm_root_dir, "SPECS")
            utils.EnsureDirExists(rpm_specs_dir)

            template_binary_dir = os.path.join(tmp_dir,
                                               "dist/rpmbuild/grr-client")

            target_binary_dir = "%s%s" % (rpm_build_dir,
                                          config.CONFIG.Get(
                                              "ClientBuilder.target_dir",
                                              context=self.context))

            utils.EnsureDirExists(os.path.dirname(target_binary_dir))
            try:
                shutil.rmtree(target_binary_dir)
            except OSError:
                pass
            # TODO(user):pytype: incorrect move() definition in typeshed.
            # pytype: disable=wrong-arg-types
            shutil.move(template_binary_dir, target_binary_dir)
            # pytype: enable=wrong-arg-types

            client_name = config.CONFIG.Get("Client.name",
                                            context=self.context)
            client_binary_name = config.CONFIG.Get("Client.binary_name",
                                                   context=self.context)
            if client_binary_name != "grr-client":
                # TODO(user):pytype: incorrect move() definition in typeshed.
                # pytype: disable=wrong-arg-types
                shutil.move(
                    os.path.join(target_binary_dir, "grr-client"),
                    os.path.join(target_binary_dir, client_binary_name))
                # pytype: enable=wrong-arg-types

            if config.CONFIG.Get("Client.fleetspeak_enabled",
                                 context=self.context):
                self._GenerateFleetspeakConfig(template_dir, rpm_build_dir)
                if not config.CONFIG.Get("Client.fleetspeak_service_name",
                                         context=self.context):
                    # The Fleetspeak service name is required when generating the RPM
                    # spec file.
                    raise build.BuildError(
                        "Client.fleetspeak_service_name is not set.")
                if config.CONFIG.Get("ClientBuilder.fleetspeak_bundled",
                                     context=self.context):
                    self._GenerateBundledFleetspeakFiles(
                        os.path.join(template_dir, "bundled-fleetspeak"),
                        rpm_build_dir)
                    shutil.copy(
                        config.CONFIG.Get(
                            "ClientBuilder.fleetspeak_client_config",
                            context=self.context),
                        os.path.join(rpm_build_dir,
                                     "etc/fleetspeak-client/client.config"))
            else:
                self._GenerateInitConfigs(template_dir, rpm_build_dir)

            # Generate spec
            spec_filename = os.path.join(rpm_specs_dir,
                                         "%s.spec" % client_name)
            build_helpers.GenerateFile(os.path.join(
                tmp_dir, "dist/rpmbuild/grr.spec.in"),
                                       spec_filename,
                                       context=self.context)

            # Generate prelinking blacklist file
            prelink_target_filename = os.path.join(rpm_build_dir,
                                                   "etc/prelink.conf.d",
                                                   "%s.conf" % client_name)

            utils.EnsureDirExists(os.path.dirname(prelink_target_filename))
            build_helpers.GenerateFile(os.path.join(
                tmp_dir, "dist/rpmbuild/prelink_blacklist.conf.in"),
                                       prelink_target_filename,
                                       context=self.context)

            # Create a client config.
            client_context = ["Client Context"] + self.context
            client_config_content = build_helpers.GetClientConfig(
                client_context)

            with io.open(os.path.join(
                    target_binary_dir,
                    config.CONFIG.Get("ClientBuilder.config_filename",
                                      context=self.context)),
                         "w",
                         encoding="utf-8") as fd:
                fd.write(client_config_content)

            # Set the daemon to executable.
            os.chmod(os.path.join(target_binary_dir, client_binary_name),
                     0o755)

            client_arch = config.CONFIG.Get("Template.arch",
                                            context=self.context)
            if client_arch == "amd64":
                client_arch = "x86_64"

            # Create wrapper script
            if os.path.exists(os.path.join(target_binary_dir,
                                           "wrapper.sh.in")):
                build_helpers.GenerateFile(
                    os.path.join(target_binary_dir, "wrapper.sh.in"),
                    os.path.join(target_binary_dir, "wrapper.sh"),
                    context=self.context)
                os.chmod(os.path.join(target_binary_dir, "wrapper.sh"), 0o755)

            command = [
                rpmbuild_binary, "--define", "_topdir " + rpm_root_dir,
                "--target", client_arch, "--buildroot", rpm_buildroot_dir,
                "-bb", spec_filename
            ]
            try:
                subprocess.check_output(command, stderr=subprocess.STDOUT)
            except subprocess.CalledProcessError as e:
                logging.error("Error calling %s.", command)
                logging.error(e.output)
                raise

            client_version = config.CONFIG.Get("Template.version_string",
                                               context=self.context)
            rpm_filename = os.path.join(
                rpm_rpms_dir, client_arch,
                "%s-%s-1.%s.rpm" % (client_name, client_version, client_arch))

            utils.EnsureDirExists(os.path.dirname(output_path))
            shutil.move(rpm_filename, output_path)

            logging.info("Created package %s", output_path)
            self._Sign(output_path)
            return output_path
Ejemplo n.º 26
0
 def WriteFile(path, contents):
     utils.EnsureDirExists(os.path.dirname(path))
     with open(path, "w") as f:
         f.write(contents)
Ejemplo n.º 27
0
    def _GenerateDPKGFiles(self, template_path):
        """Generates the files needed by dpkg-buildpackage."""

        fleetspeak_enabled = config.CONFIG.Get("Client.fleetspeak_enabled",
                                               context=self.context)
        fleetspeak_bundled = config.CONFIG.Get(
            "ClientBuilder.fleetspeak_bundled", context=self.context)

        if fleetspeak_bundled and not fleetspeak_enabled:
            raise build.BuildError("ClientBuilder.fleetspeak_bundled requires "
                                   "Client.fleetspeak_enabled to be set.")

        # Rename the generated binaries to the correct name.
        template_binary_dir = os.path.join(template_path,
                                           "dist/debian/grr-client")
        package_name = config.CONFIG.Get("ClientBuilder.package_name",
                                         context=self.context)
        target_binary_dir = os.path.join(
            template_path,
            "dist/debian/%s%s" % (package_name,
                                  config.CONFIG.Get("ClientBuilder.target_dir",
                                                    context=self.context)))
        if package_name == "grr-client":
            # Need to rename the template path or the move will fail.
            shutil.move(template_binary_dir,
                        "%s-template" % template_binary_dir)
            template_binary_dir = "%s-template" % template_binary_dir

        utils.EnsureDirExists(os.path.dirname(target_binary_dir))
        shutil.move(template_binary_dir, target_binary_dir)

        shutil.move(
            os.path.join(target_binary_dir, "grr-client"),
            os.path.join(
                target_binary_dir,
                config.CONFIG.Get("Client.binary_name", context=self.context)))

        deb_in_dir = os.path.join(template_path, "dist/debian/debian.in/")

        if not os.path.isdir(deb_in_dir):
            # This is an universal (fleetspeak + legacy) template.
            # In prior versions, debian.in used to contain different files for a
            # fleetspeak-enabled and legacy template respectively.
            if fleetspeak_enabled:
                deb_in_dir = os.path.join(template_path,
                                          "dist/debian/fleetspeak-debian.in/")
            else:
                deb_in_dir = os.path.join(template_path,
                                          "dist/debian/legacy-debian.in/")

        build_helpers.GenerateDirectory(deb_in_dir,
                                        os.path.join(template_path,
                                                     "dist/debian"),
                                        [("grr-client", package_name)],
                                        context=self.context)

        # Generate directories for the /usr/sbin link.
        utils.EnsureDirExists(
            os.path.join(template_path,
                         "dist/debian/%s/usr/sbin" % package_name))

        if os.path.exists(os.path.join(target_binary_dir, "wrapper.sh.in")):
            build_helpers.GenerateFile(os.path.join(target_binary_dir,
                                                    "wrapper.sh.in"),
                                       os.path.join(target_binary_dir,
                                                    "wrapper.sh"),
                                       context=self.context)
            os.chmod(os.path.join(target_binary_dir, "wrapper.sh"), 0o755)

        if fleetspeak_enabled:
            if fleetspeak_bundled:
                self._GenerateFleetspeakConfig(
                    template_path, "/etc/fleetspeak-client/textservices")
                self._GenerateBundledFleetspeakFiles(
                    os.path.join(template_path, "dist/bundled-fleetspeak"),
                    os.path.join(template_path, "dist/debian", package_name))

                shutil.copy(
                    config.CONFIG.Get("ClientBuilder.fleetspeak_client_config",
                                      context=self.context),
                    os.path.join(template_path, "dist", "debian", package_name,
                                 "etc/fleetspeak-client/client.config"))

            else:
                fleetspeak_service_dir = config.CONFIG.Get(
                    "ClientBuilder.fleetspeak_service_dir",
                    context=self.context)
                self._GenerateFleetspeakConfig(template_path,
                                               fleetspeak_service_dir)
        else:
            # Generate the nanny template.
            # This exists from client version 3.1.2.5 onwards.
            build_helpers.GenerateFile(os.path.join(target_binary_dir,
                                                    "nanny.sh.in"),
                                       os.path.join(target_binary_dir,
                                                    "nanny.sh"),
                                       context=self.context)

            # Generate the upstart template.
            build_helpers.GenerateFile(
                os.path.join(template_path,
                             "dist/debian/upstart.in/grr-client.conf"),
                os.path.join(template_path,
                             "dist/debian/%s.upstart" % package_name),
                context=self.context)

            # Generate the initd template. The init will not run if it detects upstart
            # is present.
            build_helpers.GenerateFile(
                os.path.join(template_path, "dist/debian/initd.in/grr-client"),
                os.path.join(template_path,
                             "dist/debian/%s.init" % package_name),
                context=self.context)

            # Generate the systemd unit file.
            build_helpers.GenerateFile(
                os.path.join(template_path,
                             "dist/debian/systemd.in/grr-client.service"),
                os.path.join(template_path,
                             "dist/debian/%s.service" % package_name),
                context=self.context)

        # Clean up the template dirs.
        # Some of the dirs might be missing in older template versions, so removing
        # conditionally.
        self._RmTreeIfExists(
            os.path.join(template_path, "dist/debian/debian.in"))
        self._RmTreeIfExists(
            os.path.join(template_path, "dist/debian/fleetspeak-debian.in"))
        self._RmTreeIfExists(
            os.path.join(template_path, "dist/debian/legacy-debian.in"))
        self._RmTreeIfExists(
            os.path.join(template_path, "dist/debian/upstart.in"))
        self._RmTreeIfExists(
            os.path.join(template_path, "dist/debian/initd.in"))
        self._RmTreeIfExists(
            os.path.join(template_path, "dist/debian/systemd.in"))
        self._RmTreeIfExists(os.path.join(template_path, "dist/fleetspeak"))
        self._RmTreeIfExists(
            os.path.join(template_path, "dist/bundled-fleetspeak"))