Example #1
0
    def Run(self, arg):
        """Does the actual work."""
        try:
            if self.grr_worker.client.FleetspeakEnabled():
                raise ValueError(
                    "Not supported on Fleetspeak enabled clients.")
        except AttributeError:
            pass

        smart_arg = {
            utils.SmartStr(field): value
            for field, value in iteritems(arg)
        }

        disallowed_fields = [
            field for field in smart_arg
            if field not in UpdateConfiguration.UPDATABLE_FIELDS
        ]

        if disallowed_fields:
            raise ValueError(
                "Received an update request for restricted field(s) %s." %
                ",".join(disallowed_fields))

        if platform.system() != "Windows":
            # Check config validity before really applying the changes. This isn't
            # implemented for our Windows clients though, whose configs are stored in
            # the registry, as opposed to in the filesystem.

            canary_config = config.CONFIG.CopyConfig()

            # Prepare a temporary file we'll write changes to.
            with tempfiles.CreateGRRTempFile(mode="w+") as temp_fd:
                temp_filename = temp_fd.name

            # Write canary_config changes to temp_filename.
            canary_config.SetWriteBack(temp_filename)
            self._UpdateConfig(smart_arg, canary_config)

            try:
                # Assert temp_filename is usable by loading it.
                canary_config.SetWriteBack(temp_filename)
            # Wide exception handling passed here from config_lib.py...
            except Exception:  # pylint: disable=broad-except
                logging.warning("Updated config file %s is not usable.",
                                temp_filename)
                raise

            # If temp_filename works, remove it (if not, it's useful for debugging).
            os.unlink(temp_filename)

        # The changes seem to work, so push them to the real config.
        self._UpdateConfig(smart_arg, config.CONFIG)
Example #2
0
    def testCreateAndDelete(self):
        fd = tempfiles.CreateGRRTempFile(filename="process.42.exe", mode="wb")
        fd.close()
        self.assertTrue(os.path.exists(fd.name))
        self.assertEqual(os.path.basename(fd.name), "process.42.exe")
        tempfiles.DeleteGRRTempFile(fd.name)
        self.assertFalse(os.path.exists(fd.name))

        with io.open(os.path.join(self.temp_dir, "notatmpfile"), "wb") as fd:
            fd.write(b"something")
        self.assertTrue(os.path.exists(fd.name))
        self.assertRaises(tempfiles.ErrorNotTempFile,
                          tempfiles.DeleteGRRTempFile, fd.name)
        self.assertTrue(os.path.exists(fd.name))
Example #3
0
  def setUp(self):
    super(DeleteGRRTempFiles, self).setUp()
    filename = "%s_blah" % config.CONFIG["Client.tempfile_prefix"]
    self.tempfile = utils.JoinPath(self.temp_dir, "delete_test", filename)
    self.dirname = os.path.dirname(self.tempfile)
    os.makedirs(self.dirname)
    self.tempdir_overrider = test_lib.ConfigOverrider({
        "Client.tempdir_roots": [os.path.dirname(self.dirname)],
        "Client.grr_tempdir": os.path.basename(self.dirname)
    })
    self.tempdir_overrider.Start()

    self.not_tempfile = os.path.join(self.temp_dir, "notatempfile")
    open(self.not_tempfile, "wb").write("something")

    self.temp_fd = tempfiles.CreateGRRTempFile(filename="file1")
    self.temp_fd2 = tempfiles.CreateGRRTempFile(filename="file2")
    self.assertTrue(os.path.exists(self.not_tempfile))
    self.assertTrue(os.path.exists(self.temp_fd.name))
    self.assertTrue(os.path.exists(self.temp_fd2.name))

    self.pathspec = rdf_paths.PathSpec(
        path=self.dirname, pathtype=rdf_paths.PathSpec.PathType.OS)
Example #4
0
    def testWrongOwnerGetsFixed(self):
        def mystat(filename):
            stat_info = os.lstat.old_target(filename)
            stat_list = list(stat_info)
            # Adjust the UID.
            stat_list[4] += 1
            return os.stat_result(stat_list)

        # Place a malicious file in the temp dir. This needs to be deleted
        # before we can use the temp dir.
        fd = tempfiles.CreateGRRTempFile(filename="maliciousfile", mode="wb")
        fd.close()

        self.assertTrue(os.path.exists(fd.name))

        with utils.Stubber(os, "lstat", mystat):
            fd2 = tempfiles.CreateGRRTempFile(filename="temptemp", mode="wb")
            fd2.close()

        # Old file is gone.
        self.assertFalse(os.path.exists(fd.name))

        # Cleanup.
        tempfiles.DeleteGRRTempFile(fd2.name)
Example #5
0
    def WriteBlobToFile(self, request):
        """Writes the blob to a file and returns its path."""
        # First chunk truncates the file, later chunks append.
        if request.offset == 0:
            mode = "w+b"
        else:
            mode = "r+b"

        temp_file = tempfiles.CreateGRRTempFile(filename=request.write_path,
                                                mode=mode)
        with temp_file:
            path = temp_file.name
            temp_file.seek(0, 2)
            if temp_file.tell() != request.offset:
                raise IOError("Chunks out of order Error.")

            # Write the new chunk.
            temp_file.write(request.executable.data)

        return path