Esempio n. 1
0
def main(unused_argv):
    """Main."""
    token = GetToken()
    grr_config.CONFIG.AddContext(contexts.COMMAND_LINE_CONTEXT)
    grr_config.CONFIG.AddContext(contexts.CONFIG_UPDATER_CONTEXT)

    if flags.FLAGS.subparser_name == "initialize":
        config_lib.ParseConfigCommandLine()
        if flags.FLAGS.noprompt:
            InitializeNoPrompt(grr_config.CONFIG, token=token)
        else:
            Initialize(grr_config.CONFIG, token=token)
        return

    server_startup.Init()

    try:
        print "Using configuration %s" % grr_config.CONFIG
    except AttributeError:
        raise RuntimeError("No valid config specified.")

    if flags.FLAGS.subparser_name == "generate_keys":
        try:
            GenerateKeys(grr_config.CONFIG,
                         overwrite_keys=flags.FLAGS.overwrite_keys)
        except RuntimeError, e:
            # GenerateKeys will raise if keys exist and overwrite_keys is not set.
            print "ERROR: %s" % e
            sys.exit(1)
        grr_config.CONFIG.Write()
Esempio n. 2
0
def main(unused_argv):
    """Main."""
    config_lib.CONFIG.AddContext("Worker Context",
                                 "Context applied when running a worker.")

    # Initialise flows
    server_startup.Init()
    token = access_control.ACLToken(username="******").SetUID()
    worker_obj = worker.GRRWorker(token=token)
    worker_obj.Run()
Esempio n. 3
0
def main(unused_argv):
    """Main."""
    config_lib.CONFIG.AddContext("HTTPServer Context")

    server_startup.Init()

    httpd = CreateServer()

    server_startup.DropPrivileges()

    try:
        httpd.serve_forever()
    except KeyboardInterrupt:
        print "Caught keyboard interrupt, stopping"
Esempio n. 4
0
def main(unused_argv):
    """Main."""
    config_lib.CONFIG.AddContext("Commandline Context",
                                 "Context applied for all command line tools")
    config_lib.CONFIG.AddContext("ExportTool Context",
                                 "Context applied to the export tool.")
    server_startup.Init()

    data_store.default_token = access_control.ACLToken(
        username=flags.FLAGS.username or getpass.getuser(),
        reason=flags.FLAGS.reason)

    # If subcommand was specified by the user in the command line,
    # corresponding subparser should have initialized "func" argument
    # with a corresponding export plugin's Run() function.
    flags.FLAGS.func(flags.FLAGS)
Esempio n. 5
0
def main(unused_argv):
    """Main."""

    config.CONFIG.AddContext(contexts.DATA_SERVER_CONTEXT)
    server_startup.Init()

    manager = Manager()
    if not manager.Start():
        print "Failed to start manager"
        return

    atexit.register(manager.SaveHistory)
    try:
        manager.Run()
    except (EOFError, KeyboardInterrupt):
        print
Esempio n. 6
0
def main(unused_argv):
    """Main."""
    server_startup.Init()

    filename = flags.FLAGS.filename
    if not os.path.exists(filename):
        print "File %s does not exist" % filename
        return

    with aff4.FACTORY.Create(filestore.NSRLFileStore.PATH,
                             filestore.NSRLFileStore,
                             mode="rw",
                             token=aff4.FACTORY.root_token) as store:
        imported = ImportFile(store, filename, flags.FLAGS.start)
        data_store.DB.Flush()
        print "Imported %d hashes" % imported
Esempio n. 7
0
def main(unused_argv):
    """Main."""
    banner = ("\nWelcome to the GRR console\n")

    config_lib.CONFIG.AddContext(contexts.COMMAND_LINE_CONTEXT)
    config_lib.CONFIG.AddContext(
        contexts.CONSOLE_CONTEXT,
        "Context applied when running the console binary.")
    server_startup.Init()

    # To make the console easier to use, we make a default token which will be
    # used in StartFlow operations.
    data_store.default_token = access_control.ACLToken(
        username=getpass.getuser(), reason=flags.FLAGS.reason)

    locals_vars = {
        "__name__": "GRR Console",
        "l": Lister,
        "lc": GetChildrenList,
        "o": aff4.FACTORY.Open,

        # Bring some symbols from other modules into the console's
        # namespace.
        "StartFlowAndWait": flow_utils.StartFlowAndWait,
        "StartFlowAndWorker": debugging.StartFlowAndWorker,
        "RunEndToEndTests": end_to_end_tests.RunEndToEndTests,
    }

    locals_vars.update(globals())  # add global variables to console
    if flags.FLAGS.client is not None:
        locals_vars["client"], locals_vars["token"] = console_utils.OpenClient(
            client_id=flags.FLAGS.client)

    if flags.FLAGS.code_to_execute:
        logging.info("Running code from flag: %s", flags.FLAGS.code_to_execute)
        exec(flags.FLAGS.code_to_execute)  # pylint: disable=exec-used
    elif flags.FLAGS.command_file:
        logging.info("Running code from file: %s", flags.FLAGS.command_file)
        execfile(flags.FLAGS.command_file)

    if (flags.FLAGS.exit_on_complete
            and (flags.FLAGS.code_to_execute or flags.FLAGS.command_file)):
        return

    else:  # We want the normal shell.
        locals_vars.update(globals())  # add global variables to console
        ipshell.IPShell(argv=[], user_ns=locals_vars, banner=banner)
Esempio n. 8
0
def AddUsers(token=None):
    # Now initialize with our modified config.
    server_startup.Init()

    print "\nStep 3: Adding Admin User"
    try:
        maintenance_utils.AddUser("admin",
                                  labels=["admin"],
                                  token=token,
                                  password=flags.FLAGS.admin_password)
    except maintenance_utils.UserError:
        if flags.FLAGS.noprompt:
            maintenance_utils.UpdateUser("admin",
                                         password=flags.FLAGS.admin_password,
                                         add_labels=["admin"],
                                         token=token)
        else:
            if ((raw_input("User 'admin' already exists, do you want to "
                           "reset the password? [yN]: ").upper()
                 or "N") == "Y"):
                maintenance_utils.UpdateUser("admin",
                                             password=True,
                                             add_labels=["admin"],
                                             token=token)
Esempio n. 9
0
def main(unused_argv):
    config_lib.CONFIG.AddContext(contexts.COMMAND_LINE_CONTEXT,
                                 "Context applied for all command line tools")
    server_startup.Init()

    if fuse is None:
        logging.critical("""Could not start!
fusepy must be installed to run fuse_mount.py!
Try to run:
  pip install fusepy

inside your virtualenv.
""")
        sys.exit(1)

    if not flags.FLAGS.mountpoint:
        Usage()
        sys.exit(1)

    # We multiple inherit from GRRFuse and fuse.Operations. In the
    # case that fuse is present, we run the actual FUSE layer, since we have
    # fuse.Operations. In the case that fuse is not present, we have already
    # exited by now if we were run from the command line, and if we were not run
    # from the command line, we've been imported, and we run the tests using a
    # mock fuse.

    class FuseOperation(GRRFuse, fuse.Operations):
        pass

    root = flags.FLAGS.aff4path

    username = flags.FLAGS.username or getpass.getuser()
    data_store.default_token = access_control.ACLToken(
        username=username, reason=flags.FLAGS.reason or "fusemount")

    logging.info("fuse_mount.py is mounting %s at %s....", root,
                 flags.FLAGS.mountpoint)

    refresh_policy = flags.FLAGS.refresh_policy

    if refresh_policy == "always":
        max_age_before_refresh = datetime.timedelta(0)
    elif refresh_policy == "never":
        # Set the max age to be the maximum possible time difference.
        max_age_before_refresh = datetime.timedelta.max
    elif refresh_policy == "if_older_than_max_age":
        max_age_before_refresh = datetime.timedelta(
            seconds=flags.FLAGS.max_age_before_refresh)
    else:
        # Otherwise, a flag outside the enum was given and the flag validator threw
        # an execption.
        pass

    fuse_operation = FuseOperation(
        root=root,
        token=data_store.default_token,
        max_age_before_refresh=max_age_before_refresh,
        ignore_cache=flags.FLAGS.ignore_cache,
        force_sparse_image=flags.FLAGS.force_sparse_image,
        sparse_image_threshold=flags.FLAGS.sparse_image_threshold,
        timeout=flags.FLAGS.timeout)

    fuse.FUSE(fuse_operation,
              flags.FLAGS.mountpoint,
              foreground=not flags.FLAGS.background)
Esempio n. 10
0
def RunEndToEndTests():
    runner = unittest.TextTestRunner()

    # We are running a test so let the config system know that.
    config_lib.CONFIG.AddContext(contexts.TEST_CONTEXT,
                                 "Context applied when we run tests.")
    server_startup.Init()

    token = access_control.ACLToken(username="******",
                                    reason="Running end to end client tests.")

    # We need this for the launchbinary test
    with aff4.FACTORY.Create("aff4:/users/GRREndToEndTest",
                             aff4_users.GRRUser,
                             mode="rw",
                             token=token) as test_user:
        test_user.AddLabels("admin")

    client_id_set = base.GetClientTestTargets(
        client_ids=flags.FLAGS.client_ids,
        hostnames=flags.FLAGS.hostnames,
        checkin_duration_threshold="1h",
        token=token)

    for cls in base.ClientTestBase.classes.values():
        for p in cls.platforms:
            if p not in set(["Linux", "Darwin", "Windows"]):
                raise ValueError("Unsupported platform: %s in class %s" %
                                 (p, cls.__name__))

    if not client_id_set:
        print(
            "No clients to test on.  Define Test.end_to_end_client* config "
            "options, or pass them as parameters.")

    results_by_client = {}
    for client in aff4.FACTORY.MultiOpen(client_id_set, token=token):
        client_summary = client.GetSummary()

        if hasattr(client_summary, "system_info"):
            sysinfo = client_summary.system_info
        else:
            raise RuntimeError(
                "Unknown system type, likely waiting on interrogate"
                " to complete.")

        results = {}
        results_by_client[client.urn] = results
        for cls in base.ClientTestBase.classes.values():
            if flags.FLAGS.testnames and (cls.__name__
                                          not in flags.FLAGS.testnames):
                continue

            if not aff4.issubclass(cls, base.ClientTestBase):
                continue

            if cls.__name__.startswith("Abstract"):
                continue

            # Fix the call method so we can use the test runner.  See doco in
            # base.ClientTestBase
            def _RealCall(testcase, *args, **kwds):
                return testcase.run(*args, **kwds)

            cls.__call__ = _RealCall

            if sysinfo.system in cls.platforms:
                print "Running %s on %s (%s: %s, %s, %s)" % (
                    cls.__name__, client_summary.client_id, sysinfo.fqdn,
                    sysinfo.system, sysinfo.version, sysinfo.machine)

                try:
                    # Mixin the unittest framework so we can use the test runner to run
                    # the test and get nice output.  We don't want to depend on unitttest
                    # code in the tests themselves.
                    testcase = cls(client_id=client_summary.client_id,
                                   platform=sysinfo.system,
                                   token=token,
                                   local_client=flags.FLAGS.local_client,
                                   local_worker=flags.FLAGS.local_worker)
                    results[cls.__name__] = runner.run(testcase)
                except Exception:  # pylint: disable=broad-except
                    logging.exception("Failed to run test %s", cls)

        # Print a little summary.

        for client, results in results_by_client.iteritems():
            print "Results for %s:" % client
            for testcase, result in sorted(results.items()):
                res = "[  OK  ]"
                if result.errors or result.failures:
                    res = "[ FAIL ]"
                print "%45s: %s" % (testcase, res)
Esempio n. 11
0
def main(_):
    """Run the main test harness."""
    config_lib.CONFIG.AddContext(
        contexts.ADMIN_UI_CONTEXT,
        "Context applied when running the admin user interface GUI.")
    server_startup.Init()

    if (not os.path.exists(
            os.path.join(config_lib.CONFIG["AdminUI.document_root"],
                         "dist/grr-ui.bundle.js"))
            or not os.path.exists(
                os.path.join(config_lib.CONFIG["AdminUI.document_root"],
                             "dist/grr-ui.bundle.css"))):
        raise RuntimeError("Can't find compiled JS/CSS bundles. "
                           "Please reinstall the PIP package using "
                           "\"pip install -e .\" to rebuild the bundles.")

    # Start up a server in another thread
    bind_address = config_lib.CONFIG["AdminUI.bind"]
    ip = ipaddr.IPAddress(bind_address)
    if ip.version == 4:
        # Address looks like an IPv4 address.
        ThreadedServer.address_family = socket.AF_INET

    max_port = config_lib.CONFIG.Get("AdminUI.port_max",
                                     config_lib.CONFIG["AdminUI.port"])

    for port in range(config_lib.CONFIG["AdminUI.port"], max_port + 1):
        # Make a simple reference implementation WSGI server
        try:
            server = simple_server.make_server(
                bind_address,
                port,
                wsgiapp.AdminUIApp().WSGIHandler(),
                server_class=ThreadedServer)
            break
        except socket.error as e:
            if e.errno == socket.errno.EADDRINUSE and port < max_port:
                logging.info("Port %s in use, trying %s", port, port + 1)
            else:
                raise

    proto = "HTTP"

    if config_lib.CONFIG["AdminUI.enable_ssl"]:
        cert_file = config_lib.CONFIG["AdminUI.ssl_cert_file"]
        if not cert_file:
            raise ValueError("Need a valid cert file to enable SSL.")

        key_file = config_lib.CONFIG["AdminUI.ssl_key_file"]
        server.socket = ssl.wrap_socket(server.socket,
                                        certfile=cert_file,
                                        keyfile=key_file,
                                        server_side=True)
        proto = "HTTPS"

        # SSL errors are swallowed by the WSGIServer so if your configuration does
        # not work, uncomment the line below, point your browser at the gui and look
        # at the log file to see why SSL complains:
        # server.socket.accept()

    sa = server.socket.getsockname()
    logging.info("Serving %s on %s port %d ...", proto, sa[0], sa[1])
    server_startup.DropPrivileges()

    server.serve_forever()