Пример #1
0
    def _check_scans(self):
        """
        Check for finished scans.
        """
        todelete = [ ]

        for scan, inventory in self.running_scans.items():
            try:
                scan_state = scan.scan_state()
            except Exception, err:
                # scan failed to run.
                # probably a scan with args that requires root and a
                # normal user tried running.
                dlg = GenericAlert(_("Scan failed to run!"),
                        (_("You tried running scan for Inventory") +
                            (" %r,\n" % inventory) +
                            _("but it returned the following") +
                            (":\n\n%s" % err)),
                        buttons={1: (gtk.RESPONSE_OK, gtk.STOCK_OK)})
                dlg.run()
                dlg.destroy()
                todelete.append(scan)
                continue

            if not scan_state: # scan finished
                xmlstore = XMLStore(Path.umitdb_ng)
                try:
                    try:
                        xmlstore.store(scan.get_xml_output_file(),
                                inventory=inventory)
                    except Exception:
                        # failed while adding scan to the database
                        dlg = GenericAlert(_("Database couldn't be updated!"),
                                (_("The scan for the Inventory") +
                                    (" %r " % inventory) +
                                    _("finished but it couldn't be added to "
                                        "the database.") +
                                    ("\n\n%s" % traceback.format_exc())),
                                buttons={1: (gtk.RESPONSE_OK, gtk.STOCK_OK)})
                        dlg.run()
                        dlg.destroy()
                finally:
                    xmlstore.close() # close connection to the database
                    scan.close()
                    todelete.append(scan)
    def _check_scans(self):
        """
        Check for finished scans.
        """
        todelete = []

        for scan, inventory in self.running_scans.items():
            try:
                scan_state = scan.scan_state()
            except Exception, err:
                # scan failed to run.
                # probably a scan with args that requires root and a
                # normal user tried running.
                dlg = GenericAlert(
                    _("Scan failed to run!"),
                    (_("You tried running scan for Inventory") +
                     (" %r,\n" % inventory) +
                     _("but it returned the following") + (":\n\n%s" % err)),
                    buttons={1: (gtk.RESPONSE_OK, gtk.STOCK_OK)})
                dlg.run()
                dlg.destroy()
                todelete.append(scan)
                continue

            if not scan_state:  # scan finished
                xmlstore = XMLStore(Path.umitdb_ng)
                try:
                    try:
                        xmlstore.store(scan.get_xml_output_file(),
                                       inventory=inventory)
                    except Exception:
                        # failed while adding scan to the database
                        dlg = GenericAlert(
                            _("Database couldn't be updated!"),
                            (_("The scan for the Inventory") +
                             (" %r " % inventory) +
                             _("finished but it couldn't be added to "
                               "the database.") +
                             ("\n\n%s" % traceback.format_exc())),
                            buttons={1: (gtk.RESPONSE_OK, gtk.STOCK_OK)})
                        dlg.run()
                        dlg.destroy()
                finally:
                    xmlstore.close()  # close connection to the database
                    scan.close()
                    todelete.append(scan)
Пример #3
0
def run_scheduler(sched, winhndl=None):
    """
    Run scheduler forever.
    """
    next_time = calc_next_time()
    scount = 0  # number of scans running

    while 1:  # run forever and ever ;)
        current_time = time.time()

        if current_time < next_time:
            sleep_time = next_time - current_time + .1
            if winhndl:
                stopsignal = win32event.WaitForSingleObject(
                    winhndl, int(sleep_time * 1000))
                if stopsignal == win32event.WAIT_OBJECT_0:
                    break
            else:
                time.sleep(sleep_time)

        # check if time has changed by more than two minutes (clock changes)
        if abs(time.time() - next_time) > 120:
            # reset timer
            next_time = calc_next_time()

        sched.check_for_changes()

        for schema in sched.schemas:
            if schema.job_to_run():
                if not int(schema.enabled):  # schema disabled, neeexxt!
                    continue

                name = schema.schema_name
                log.info("Scheduled schema to run: %s" % name)

                if not schema.command:
                    log.warning("No command set for schema %s, \
skipping!" % name)
                    continue

                scan = NmapCommand(schema.command)
                scan.run_scan()

                running_scans[scount] = (scan, schema.saveto, schema.mailto,
                                         name, schema.addtoinv, schema.smtp)
                scount += 1  # more one scan running

        for running, opts in running_scans.items():
            scan = opts[0]

            try:
                scan_state = scan.scan_state()
            except Exception, err:
                log.critical("Scheduled schema '%s' failed to run. \
Reason: %s" % (schema.schema_name, err))
                continue

            if not scan_state:  # scan finished
                log.info("Scan finished: %s" % opts[3])

                if opts[1]:  # save xml output
                    saveto = decide_output(opts[1])
                    f = open(saveto, 'w')
                    f.write(open(scan.get_xml_output_file(), 'r').read())
                    f.close()
                    log.info("Scan output saved as: %s" % saveto)

                if opts[2]:  # mail output
                    recipients = opts[2].split(',')
                    log.info("Scan output will be mailed to: %s" % recipients)

                    smtp = load_smtp_profile(opts[5])

                    auth = int(smtp['auth'])
                    tls = int(smtp['tls'])
                    mailfrom = smtp['mailfrom']
                    user = smtp['user']
                    passwd = smtp['pass']
                    server = smtp['server']
                    port = smtp['port']
                    curr_time = format_asctime(datetime.now())
                    orig_output = scan.get_xml_output_file()
                    new_file_output = os.path.join(
                        os.path.dirname(orig_output),
                        "%s (%s).xml" % (curr_time, opts[3]))

                    fd_wcont = open(new_file_output, 'w')
                    for line in open(orig_output, 'r'):
                        fd_wcont.write(line)
                    fd_wcont.close()

                    email = Email(mailfrom, recipients, server, None, user,
                                  passwd, tls, port)
                    email.sendmail(
                        subject=(
                            _("UMIT: Status Report for scheduled schema") +
                            " %r " % opts[3]),
                        msg=(_("There was a scheduled job that just "
                               "finished:") + (" %s\n" % curr_time) +
                             _("Follows an attachment of the job output.")),
                        attach=new_file_output)
                    os.remove(new_file_output)

                if int(opts[4]):  # add to inventory
                    log.info("Scan result is being added to Inventory "
                             "%s" % opts[3])
                    xmlstore = XMLStore(Path.umitdb_ng, False)
                    try:
                        xmlstore.store(scan.get_xml_output_file(),
                                       inventory=opts[3])
                    finally:
                        xmlstore.close()  # close connection to the database
                    log.info("Scan finished insertion on Inventory "
                             "%s" % opts[3])

                scan.close()  # delete temporary files
                scount -= 1  # one scan finished
                del running_scans[running]

        next_time += 60
Пример #4
0
def run_scheduler(sched, winhndl=None):
    """
    Run scheduler forever.
    """
    next_time = calc_next_time()
    scount = 0 # number of scans running

    while 1: # run forever and ever ;)
        current_time = time.time()

        if current_time < next_time:
            sleep_time = next_time - current_time + .1
            if winhndl:
                stopsignal = win32event.WaitForSingleObject(winhndl,
                        int(sleep_time * 1000))
                if stopsignal == win32event.WAIT_OBJECT_0:
                    break
            else:
                time.sleep(sleep_time)

        # check if time has changed by more than two minutes (clock changes)
        if abs(time.time() - next_time) > 120:
            # reset timer
            next_time = calc_next_time()

        sched.check_for_changes()

        for schema in sched.schemas:
            if schema.job_to_run():
                if not int(schema.enabled): # schema disabled, neeexxt!
                    continue

                name = schema.schema_name
                log.info("Scheduled schema to run: %s" % name)

                if not schema.command:
                    log.warning("No command set for schema %s, \
skipping!" % name)
                    continue

                scan = NmapCommand(schema.command)
                scan.run_scan()

                running_scans[scount] = (scan, schema.saveto, schema.mailto,
                                         name, schema.addtoinv, schema.smtp)
                scount += 1 # more one scan running
        
        for running, opts in running_scans.items():
            scan = opts[0]

            try:
                scan_state = scan.scan_state()
            except Exception, err:
                log.critical("Scheduled schema '%s' failed to run. \
Reason: %s" % (schema.schema_name , err))
                continue

            if not scan_state: # scan finished
                log.info("Scan finished: %s" % opts[3])
            
                if opts[1]: # save xml output
                    saveto = decide_output(opts[1])
                    f = open(saveto, 'w')
                    f.write(open(scan.get_xml_output_file(), 'r').read())
                    f.close()
                    log.info("Scan output saved as: %s" % saveto)
                
                if opts[2]: # mail output
                    recipients = opts[2].split(',')
                    log.info("Scan output will be mailed to: %s" % recipients)

                    smtp = load_smtp_profile(opts[5])

                    auth = int(smtp['auth'])
                    tls = int(smtp['tls'])
                    mailfrom = smtp['mailfrom']
                    user = smtp['user']
                    passwd = smtp['pass']
                    server = smtp['server']
                    port = smtp['port']
                    curr_time = format_asctime(datetime.now())
                    orig_output = scan.get_xml_output_file()
                    new_file_output = os.path.join(
                            os.path.dirname(orig_output),
                            "%s (%s).xml" % (curr_time, opts[3]))

                    fd_wcont = open(new_file_output, 'w')
                    for line in open(orig_output, 'r'):
                        fd_wcont.write(line)
                    fd_wcont.close()

                    email = Email(mailfrom, recipients, server, None,
                                  user, passwd, tls, port)
                    email.sendmail(
                            subject=(
                                _("UMIT: Status Report for scheduled schema") +
                                " %r " % opts[3]),
                            msg=(
                                _("There was a scheduled job that just "
                                    "finished:") + (" %s\n" % curr_time) +
                                _("Follows an attachment of the job output.")),
                            attach=new_file_output)
                    os.remove(new_file_output)

                if int(opts[4]): # add to inventory
                    log.info("Scan result is being added to Inventory "
                            "%s" % opts[3])
                    xmlstore = XMLStore(Path.umitdb_ng, False)
                    try:
                        xmlstore.store(scan.get_xml_output_file(),
                                inventory=opts[3])
                    finally:
                        xmlstore.close() # close connection to the database
                    log.info("Scan finished insertion on Inventory "
                            "%s" % opts[3])

                scan.close() # delete temporary files
                scount -= 1 # one scan finished
                del running_scans[running]
            
        next_time += 60