예제 #1
0
파일: admin.py 프로젝트: dcampos00/aRAT
    def update_configurations_view(self, request):
        """
        Allows update the CentralLO Configurations from the clo_configs file
        """

        error = ''
        changes = []
        clo_confs = []

        header = None
        file_lines = open(settings.CONFIGURATION_DIR + 'clo_configs.cfg')
        file_lines = file_lines.readlines()

        for line_string in file_lines:
            line_string = line_string.strip()
            is_comment = line_string[0:2] == "//"

            if line_string and not is_comment:
                line_list = line_string.split()
                line_list[0:-1] = [x.replace('-', ' ')
                                   for x in line_list[0:-1]]
                if line_string[0] == "#":
                    line_list[0:-1] = [x.replace('#', '')
                                       for x in line_list[0:-1]]

                    # if the table header not exist this is created
                    if TableHeader.objects.filter(
                        resource=line_list[-1]):
                        header = TableHeader.objects.get(
                            resource=line_list[-1])
                        header.text = str(line_list[0:-1])
                        header.save()
                    else:
                        new_header = TableHeader(text=str(line_list[0:-1]),
                                                 resource=line_list[-1])
                        new_header.save()
                else:
                    # is used the element in the first column as identifier
                    identifier = line_list[0]
                    configuration = line_list[1:-1]
                    centrallo = line_list[-1]

                    header = TableHeader.objects.get(resource=centrallo)

                    if CentralloConfiguration.objects.filter(
                        identifier=identifier,
                        centrallo=centrallo):
                        clo_config = CentralloConfiguration.objects.get(
                            identifier=identifier,
                            centrallo=centrallo)

                        if clo_config.configuration != unicode(configuration):
                            clo_config.configuration = unicode(configuration)
                            clo_config.save()
                            changes.append("The %s was updated." % clo_config)
                        clo_confs.append(clo_config)
                    else:
                        new_clo_config = CentralloConfiguration(
                            identifier=identifier,
                            centrallo=centrallo)
                        new_clo_config.configuration = unicode(configuration)
                        new_clo_config.header = header
                        new_clo_config.save()
                        changes.append("The %s was added." % new_clo_config)
                        clo_confs.append(new_clo_config)

        # the configurations that not appears in the cfg file are deleted
        for clo_config in CentralloConfiguration.objects.all():
            if clo_config not in clo_confs:
                changes.append("The %s was deleted." % clo_config)
                clo_config.delete()

        consistent = checkConsistencyService.check() == "SUCCESS"

        ctx = {'title': 'Update CentralLO Configurations',
               'changes': changes,
               'error': error,
               'consistent': consistent
               }

        return TemplateResponse(request,
                                "admin/update_configuration.html",
                                ctx)
예제 #2
0
파일: admin.py 프로젝트: dcampos00/aRAT
    def update_configurations_view(self, request):
        "View that update the corr configs from corr_configs file"
        error = ''
        changes = []
        corr_confs = []

        header = None
        file_lines = open(settings.CONFIGURATION_DIR + 'corr_configs.cfg')
        file_lines = file_lines.readlines()
        for line_string in file_lines:
            line_string = line_string.strip()
            #the comments are ignored
            is_comment = line_string[0:2] == "//"

            if line_string and not is_comment:
                line_list = line_string.split()

                # the - are replaced to spaces
                line_list[0:-1] = [x.replace('-', ' ')
                                   for x in line_list[0:-1]]
                if line_string[0] == "#":
                    line_list[0:-1] = [x.replace('#', '')
                                       for x in line_list[0:-1]]

                    # if the table header exist is added to the object,
                    # if not is created
                    if TableHeader.objects.filter(resource=line_list[-1]):
                        header = TableHeader.objects.get(
                            resource=line_list[-1])
                        header.text = str(line_list[0:-1])
                        header.save()
                    else:
                        new_header = TableHeader(text=str(line_list[0:-1]),
                                                 resource=line_list[-1])
                        new_header.save()
                else:
                    caimap = line_list[0]
                    configuration = line_list[1:-1]
                    correlator = line_list[-1]

                    header = TableHeader.objects.get(resource=correlator)

                    if CorrelatorConfiguration.objects.filter(
                        caimap=caimap,
                        correlator=correlator):
                        corr_config = CorrelatorConfiguration.objects.get(
                            caimap=caimap,
                            correlator=correlator)

                        if corr_config.configuration != unicode(configuration):
                            corr_config.configuration = unicode(configuration)
                            corr_config.save()
                            changes.append("The %s was updated." % corr_config)
                        corr_confs.append(corr_config)
                    else:
                        new_corr_config = CorrelatorConfiguration(
                            caimap=caimap,
                            correlator=correlator)
                        new_corr_config.configuration = unicode(configuration)
                        new_corr_config.header = header
                        new_corr_config.save()
                        changes.append("The %s was added." % new_corr_config)
                        corr_confs.append(new_corr_config)

        for corr_config in CorrelatorConfiguration.objects.all():
            if corr_config not in corr_confs:
                changes.append("The %s was deleted." % corr_config)
                corr_config.delete()

        consistent = checkConsistencyService.check() == "SUCCESS"

        ctx = {'title': 'Update Correlator Configurations',
               'changes': changes,
               'error': error,
               'consistent': consistent
               }
        return TemplateResponse(request,
                                "admin/update_configuration.html",
                                ctx)