|
43 | 43 |
|
44 | 44 |
|
45 | 45 | class BaseCommand(metaclass=InitSubclassMeta):
|
46 |
| - |
47 | 46 | name = None
|
48 | 47 | description = None
|
49 | 48 | attributes = None
|
@@ -544,99 +543,104 @@ def handle_xml(self, xml: Element) -> bytes:
|
544 | 543 | Return:
|
545 | 544 | Response string for <start_scan> command.
|
546 | 545 | """
|
| 546 | + with self._daemon.scan_collection.scan_collection_lock: |
| 547 | + current_queued_scans = self._daemon.get_count_queued_scans() |
| 548 | + if ( |
| 549 | + self._daemon.max_queued_scans |
| 550 | + and current_queued_scans >= self._daemon.max_queued_scans |
| 551 | + ): |
| 552 | + logger.info( |
| 553 | + 'Maximum number of queued scans set to %d reached.', |
| 554 | + self._daemon.max_queued_scans, |
| 555 | + ) |
| 556 | + raise OspdCommandError( |
| 557 | + 'Maximum number of queued scans set to ' |
| 558 | + f'{str(self._daemon.max_queued_scans)} reached.', |
| 559 | + 'start_scan', |
| 560 | + ) |
547 | 561 |
|
548 |
| - current_queued_scans = self._daemon.get_count_queued_scans() |
549 |
| - if ( |
550 |
| - self._daemon.max_queued_scans |
551 |
| - and current_queued_scans >= self._daemon.max_queued_scans |
552 |
| - ): |
553 |
| - logger.info( |
554 |
| - 'Maximum number of queued scans set to %d reached.', |
555 |
| - self._daemon.max_queued_scans, |
556 |
| - ) |
557 |
| - raise OspdCommandError( |
558 |
| - 'Maximum number of queued scans set to ' |
559 |
| - f'{str(self._daemon.max_queued_scans)} reached.', |
560 |
| - 'start_scan', |
561 |
| - ) |
562 |
| - |
563 |
| - target_str = xml.get('target') |
564 |
| - ports_str = xml.get('ports') |
565 |
| - |
566 |
| - # For backward compatibility, if target and ports attributes are set, |
567 |
| - # <targets> element is ignored. |
568 |
| - if target_str is None or ports_str is None: |
569 |
| - target_element = xml.find('targets/target') |
570 |
| - if target_element is None: |
571 |
| - raise OspdCommandError('No targets or ports', 'start_scan') |
| 562 | + target_str = xml.get('target') |
| 563 | + ports_str = xml.get('ports') |
| 564 | + |
| 565 | + # For backward compatibility, if target and ports attributes |
| 566 | + # are set, <targets> element is ignored. |
| 567 | + if target_str is None or ports_str is None: |
| 568 | + target_element = xml.find('targets/target') |
| 569 | + if target_element is None: |
| 570 | + raise OspdCommandError('No targets or ports', 'start_scan') |
| 571 | + else: |
| 572 | + scan_target = OspRequest.process_target_element( |
| 573 | + target_element |
| 574 | + ) |
572 | 575 | else:
|
573 |
| - scan_target = OspRequest.process_target_element(target_element) |
574 |
| - else: |
575 |
| - scan_target = { |
576 |
| - 'hosts': target_str, |
577 |
| - 'ports': ports_str, |
578 |
| - 'credentials': {}, |
579 |
| - 'exclude_hosts': '', |
580 |
| - 'finished_hosts': '', |
581 |
| - 'options': {}, |
582 |
| - } |
583 |
| - logger.warning( |
584 |
| - "Legacy start scan command format is being used, which " |
585 |
| - "is deprecated since 20.08. Please read the documentation " |
586 |
| - "for start scan command." |
587 |
| - ) |
| 576 | + scan_target = { |
| 577 | + 'hosts': target_str, |
| 578 | + 'ports': ports_str, |
| 579 | + 'credentials': {}, |
| 580 | + 'exclude_hosts': '', |
| 581 | + 'finished_hosts': '', |
| 582 | + 'options': {}, |
| 583 | + } |
| 584 | + logger.warning( |
| 585 | + "Legacy start scan command format is being used, which " |
| 586 | + "is deprecated since 20.08. Please read the documentation " |
| 587 | + "for start scan command." |
| 588 | + ) |
588 | 589 |
|
589 |
| - scan_id = xml.get('scan_id') |
590 |
| - if scan_id is not None and scan_id != '' and not valid_uuid(scan_id): |
591 |
| - raise OspdCommandError('Invalid scan_id UUID', 'start_scan') |
| 590 | + scan_id = xml.get('scan_id') |
| 591 | + if ( |
| 592 | + scan_id is not None |
| 593 | + and scan_id != '' |
| 594 | + and not valid_uuid(scan_id) |
| 595 | + ): |
| 596 | + raise OspdCommandError('Invalid scan_id UUID', 'start_scan') |
| 597 | + |
| 598 | + if xml.get('parallel'): |
| 599 | + logger.warning( |
| 600 | + "parallel attribute of start_scan will be ignored, sice " |
| 601 | + "parallel scan is not supported by OSPd." |
| 602 | + ) |
592 | 603 |
|
593 |
| - if xml.get('parallel'): |
594 |
| - logger.warning( |
595 |
| - "parallel attribute of start_scan will be ignored, sice " |
596 |
| - "parallel scan is not supported by OSPd." |
| 604 | + scanner_params = xml.find('scanner_params') |
| 605 | + if scanner_params is None: |
| 606 | + scanner_params = {} |
| 607 | + |
| 608 | + # params are the parameters we got from the <scanner_params> XML. |
| 609 | + params = self._daemon.preprocess_scan_params(scanner_params) |
| 610 | + |
| 611 | + # VTS is an optional element. If present should not be empty. |
| 612 | + vt_selection = {} # type: Dict |
| 613 | + scanner_vts = xml.find('vt_selection') |
| 614 | + if scanner_vts is not None: |
| 615 | + if len(scanner_vts) == 0: |
| 616 | + raise OspdCommandError('VTs list is empty', 'start_scan') |
| 617 | + else: |
| 618 | + vt_selection = OspRequest.process_vts_params(scanner_vts) |
| 619 | + |
| 620 | + scan_params = self._daemon.process_scan_params(params) |
| 621 | + scan_id_aux = scan_id |
| 622 | + scan_id = self._daemon.create_scan( |
| 623 | + scan_id, scan_target, scan_params, vt_selection |
597 | 624 | )
|
598 | 625 |
|
599 |
| - scanner_params = xml.find('scanner_params') |
600 |
| - if scanner_params is None: |
601 |
| - scanner_params = {} |
602 |
| - |
603 |
| - # params are the parameters we got from the <scanner_params> XML. |
604 |
| - params = self._daemon.preprocess_scan_params(scanner_params) |
| 626 | + if not scan_id: |
| 627 | + id_ = Element('id') |
| 628 | + id_.text = scan_id_aux |
| 629 | + return simple_response_str('start_scan', 100, 'Continue', id_) |
605 | 630 |
|
606 |
| - # VTS is an optional element. If present should not be empty. |
607 |
| - vt_selection = {} # type: Dict |
608 |
| - scanner_vts = xml.find('vt_selection') |
609 |
| - if scanner_vts is not None: |
610 |
| - if len(scanner_vts) == 0: |
611 |
| - raise OspdCommandError('VTs list is empty', 'start_scan') |
612 |
| - else: |
613 |
| - vt_selection = OspRequest.process_vts_params(scanner_vts) |
614 |
| - |
615 |
| - scan_params = self._daemon.process_scan_params(params) |
616 |
| - scan_id_aux = scan_id |
617 |
| - scan_id = self._daemon.create_scan( |
618 |
| - scan_id, scan_target, scan_params, vt_selection |
619 |
| - ) |
| 631 | + logger.info( |
| 632 | + 'Scan %s added to the queue in position %d.', |
| 633 | + scan_id, |
| 634 | + self._daemon.get_count_queued_scans() + 1, |
| 635 | + ) |
620 | 636 |
|
621 |
| - if not scan_id: |
622 | 637 | id_ = Element('id')
|
623 |
| - id_.text = scan_id_aux |
624 |
| - return simple_response_str('start_scan', 100, 'Continue', id_) |
625 |
| - |
626 |
| - logger.info( |
627 |
| - 'Scan %s added to the queue in position %d.', |
628 |
| - scan_id, |
629 |
| - current_queued_scans + 1, |
630 |
| - ) |
631 |
| - |
632 |
| - id_ = Element('id') |
633 |
| - id_.text = scan_id |
| 638 | + id_.text = scan_id |
634 | 639 |
|
635 |
| - return simple_response_str('start_scan', 200, 'OK', id_) |
| 640 | + return simple_response_str('start_scan', 200, 'OK', id_) |
636 | 641 |
|
637 | 642 |
|
638 | 643 | class GetMemoryUsage(BaseCommand):
|
639 |
| - |
640 | 644 | name = "get_memory_usage"
|
641 | 645 | description = "print the memory consumption of all processes"
|
642 | 646 | attributes = {
|
|
0 commit comments