Example #1
0
    def test1(self):
        """test successful write"""

        filename = _tmpdir + "/safefile"

        out = safefile.open(filename, "w", codec="utf-8")
        tmp = out.get_tempfile()

        out.write(u"\u2022 hello\n")
        out.write(u"there")
        out.close()

        self.assertEquals(safefile.open(filename, codec="utf-8").read(),
                          u"\u2022 hello\nthere")
        self.assertEquals(os.path.exists(tmp), False)
Example #2
0
    def save(self, textbuffer, filename, title=None, stream=None):
        """
        Save buffer contents to file

        textbuffer -- richtextbuffer to save
        filename   -- HTML filename to save to (optional if stream given)
        title      -- title of HTML file (optional)
        stream     -- output stream for HTML file (optional)
        """
        
        self._save_images(textbuffer, filename)
        
        try:
            buffer_contents = iter_buffer_contents(
                textbuffer, None, None, ignore_tag)
            
            if stream:
                out = stream
            else:
                out = safefile.open(filename, "wb", codec="utf-8")
            self._html_buffer.set_output(out)
            self._html_buffer.write(buffer_contents,
                                    textbuffer.tag_table,
                                    title=title)
            out.close()
        except IOError, e:
            raise RichTextError("Could not save '%s'." % filename, e)
Example #3
0
    def load(self, textview, textbuffer, filename):
        """Load buffer with data from file"""
        
        # unhook expensive callbacks
        textbuffer.block_signals()
        if textview:
            spell = textview.is_spell_check_enabled()
            textview.enable_spell_check(False)
            textview.set_buffer(None)


        # clear buffer        
        textbuffer.clear()
        
        err = None
        try:
            if hasattr(filename, "read"):
                infile = filename
            else:
                infile = safefile.open(filename, "r", codec="utf-8")
            buffer_contents = self._html_buffer.read(infile)
            textbuffer.insert_contents(buffer_contents,
                                       textbuffer.get_start_iter())
            infile.close()

            # put cursor at begining
            textbuffer.place_cursor(textbuffer.get_start_iter())
            
        except (HtmlError, IOError, Exception), e:
            err = e
            textbuffer.clear()
            if textview:
                textview.set_buffer(textbuffer)
            ret = False            
Example #4
0
    def write(self, filename, node, notebook_attrs):

        try:
            out = safefile.open(filename, "w", codec="utf-8")
            out.write(XML_HEADER)
            out.write("<node>\n"
                      "<version>%s</version>\n" % node.get_version())

            for key, val in node.iter_attr():
                attr = notebook_attrs.get(key, None)

                if attr is not None:
                    out.write('<attr key="%s">%s</attr>\n' %
                              (key, escape(attr.write(val))))
                elif key == "version":
                    # skip version attr
                    pass
                elif isinstance(val, UnknownAttr):
                    # write unknown attrs if they are strings
                    out.write('<attr key="%s">%s</attr>\n' %
                              (key, escape(val.value)))
                else:
                    # drop attribute
                    pass

            out.write("</node>\n")
            out.close()
        except Exception as e:
            print(e)
            raise NoteBookError("Cannot write meta data", e)
    def _write_attr(self, filename, attr, attr_defs):
        """Write a node meta data file"""
        
        try:
            out = safefile.open(filename, "w", codec="utf-8")
            out.write(XML_HEADER)
            out.write(u"<node>\n"
                      u"<version>%s</version>\n" % 
                      keepnote.compat.notebook_v4.NOTEBOOK_FORMAT_VERSION)
            
            for key, val in attr.iteritems():
                if key in self._attr_suppress:
                    continue

                attr_def = attr_defs.get(key, None)
                
                if attr_def is not None:
                    out.write(u'<attr key="%s">%s</attr>\n' %
                              (key, escape(attr_def.write(val))))
                elif key == "version":
                    # skip version attr
                    pass
                elif isinstance(val, keepnote.compat.notebook_v4.UnknownAttr):
                    # write unknown attrs if they are strings
                    out.write(u'<attr key="%s">%s</attr>\n' %
                              (key, escape(val.value)))
                else:
                    # drop attribute
                    pass
                
            out.write(u"</node>\n")
            out.close()
        except Exception, e:
            raise keepnote.compat.notebook_v4.NoteBookError(
                _("Cannot write meta data"), e)
Example #6
0
    def make_page(self, parent_node, title, text):
        """
        """

        child = parent_node.new_child(notebooklib.CONTENT_TYPE_PAGE, title, None)
        #child.set_attr("title", l) # remove for 0.6.4


        # TODO: handle spaces correctly


        #child = node.new_child(notebooklib.CONTENT_TYPE_PAGE, os.path.basename(filename), index)
        #child.set_attr("title", os.path.basename(filename)) # remove for 0.6.4

        #text = open(filename).read()
        
        out = safefile.open(child.get_data_file(), "w", codec="utf-8")
        out.write(u"""<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml"><body>""")

        #text = escape(text)
        text = text.replace(u"\n", u"<br/>")
        text = text.replace(u"\r", u"")

        out.write(text)
        out.write(u"</body></html>")

        out.close()
Example #7
0
    def _write_attr(self, filename, attr, attr_defs):
        """Write a node meta data file"""

        try:
            out = safefile.open(filename, "w", codec="utf-8")
            out.write(XML_HEADER)
            out.write(u"<node>\n"
                      u"<version>%s</version>\n" %
                      keepnote.compat.notebook_v4.NOTEBOOK_FORMAT_VERSION)

            for key, val in attr.iteritems():
                if key in self._attr_suppress:
                    continue

                attr_def = attr_defs.get(key, None)

                if attr_def is not None:
                    out.write(u'<attr key="%s">%s</attr>\n' %
                              (key, escape(attr_def.write(val))))
                elif key == "version":
                    # skip version attr
                    pass
                elif isinstance(val, keepnote.compat.notebook_v4.UnknownAttr):
                    # write unknown attrs if they are strings
                    out.write(u'<attr key="%s">%s</attr>\n' %
                              (key, escape(val.value)))
                else:
                    # drop attribute
                    pass

            out.write(u"</node>\n")
            out.close()
        except Exception as e:
            raise keepnote.compat.notebook_v4.NoteBookError(
                _("Cannot write meta data"), e)
Example #8
0
    def load(self, textview, textbuffer, filename):
        """Load buffer with data from file"""

        # unhook expensive callbacks
        textbuffer.block_signals()
        if textview:
            spell = textview.is_spell_check_enabled()
            textview.enable_spell_check(False)
            textview.set_buffer(None)

        # clear buffer
        textbuffer.clear()

        err = None
        try:
            if hasattr(filename, "read"):
                infile = filename
            else:
                infile = safefile.open(filename, "r", codec="utf-8")
            buffer_contents = self._html_buffer.read(infile)
            textbuffer.insert_contents(buffer_contents,
                                       textbuffer.get_start_iter())
            infile.close()

            # put cursor at begining
            textbuffer.place_cursor(textbuffer.get_start_iter())

        except (HtmlError, IOError, Exception), e:
            err = e
            textbuffer.clear()
            if textview:
                textview.set_buffer(textbuffer)
            ret = False
Example #9
0
def write_attr(filename, nodeid, attr):
    """
    Write a node meta file

    filename -- a filename or stream
    attr     -- attribute dict
    """
    if isinstance(filename, basestring):
        out = safefile.open(filename, "w", codec="utf-8")

    # Ensure nodeid is consistent if given.
    nodeid2 = attr.get('nodeid')
    if nodeid2:
        assert nodeid == nodeid2, (nodeid, nodeid2)

    version = attr.get('version',
                       keepnote.notebook.NOTEBOOK_FORMAT_VERSION)
    out.write(u'<?xml version="1.0" encoding="UTF-8"?>\n'
              u'<node>\n'
              u'<version>%d</version>\n'
              u'<id>%s</id>\n' % (version, nodeid))
    plist.dump(attr, out, indent=2, depth=0)
    out.write(u'</node>\n')

    if isinstance(filename, basestring):
        out.close()
Example #10
0
    def open_file(self, nodeid, filename, mode="r", codec=None, _path=None):
        """Open a node file"""
        if mode not in "rwa":
            raise FileError("mode must be 'r', 'w', or 'a'")

        if filename.endswith("/"):
            raise FileError("filename '%s' cannot end with '/'" % filename)

        path = self.get_node_path(nodeid) if _path is None else _path
        fullname = get_node_filename(path, filename)
        dirpath = os.path.dirname(fullname)

        try:
            if not os.path.exists(dirpath):
                os.makedirs(dirpath)

            # NOTE: always use binary mode to ensure no
            # Window-specific line ending conversion
            stream = safefile.open(fullname, mode + "b", codec=codec)
        except Exception as e:
            raise FileError(
                "cannot open file '%s' '%s': %s" % (nodeid, filename, str(e)),
                e)

        return stream
Example #11
0
def write_attr(filename, nodeid, attr):
    """
    Write a node meta file

    filename -- a filename or stream
    attr     -- attribute dict
    """
    if isinstance(filename, basestring):
        out = safefile.open(filename, "w", codec="utf-8")

    # Ensure nodeid is consistent if given.
    nodeid2 = attr.get('nodeid')
    if nodeid2:
        assert nodeid == nodeid2, (nodeid, nodeid2)

    version = attr.get('version', keepnote.notebook.NOTEBOOK_FORMAT_VERSION)
    out.write(u'<?xml version="1.0" encoding="UTF-8"?>\n'
              u'<node>\n'
              u'<version>%d</version>\n'
              u'<id>%s</id>\n' % (version, nodeid))
    plist.dump(attr, out, indent=2, depth=0)
    out.write(u'</node>\n')

    if isinstance(filename, basestring):
        out.close()
Example #12
0
    def test4(self):

        filename = _tmpdir + "/safefile"

        out = safefile.open(filename, "w", codec="utf-8")

        out.writelines([u"\u2022 hello\n",
                        u"there\n",
                        u"again\n"])
        out.close()

        lines = safefile.open(filename, codec="utf-8").readlines()

        self.assertEquals(lines, [u"\u2022 hello\n",
                                  u"there\n",
                                  u"again\n"])
Example #13
0
    def open_file(self, nodeid, filename, mode="r", codec=None, _path=None):
        """Open a node file"""

        if mode not in "rwa":
            raise FileError("mode must be 'r', 'w', or 'a'")
        
        path = self._get_node_path(nodeid) if _path is None else _path
        fullname = get_node_filename(path, filename)
        dirpath = os.path.dirname(fullname)
        
        try:
            if not os.path.exists(dirpath):
                os.makedirs(dirpath)
            
            # NOTE: always use binary mode to ensure no 
            # Window-specific line ending conversion
            stream = safefile.open(fullname, mode + "b", codec=codec)
        except Exception as e:
            raise FileError(
                "cannot open file '%s' '%s': %s" % (nodeid, filename, str(e)), e)
        
        # TODO: this should check and update the mtime
        # but only update if it was previously consistent (before the open)
        # update mtime since file creation causes directory mtime to change
        if self._index:
            self._index.set_node_mtime(nodeid, os.stat(path).st_mtime)
        
        return stream
Example #14
0
def write_attr_v6(filename, attr):
    out = safefile.open(filename, "w", codec="utf-8")
    out.write(u'<?xml version="1.0" encoding="UTF-8"?>\n'
              u'<node>\n'
              u'<version>%d</version>\n' % attr["version"])
    plist.dump(attr, out, indent=2, depth=0)
    out.write(u'</node>\n')
    out.close()
Example #15
0
def write_attr_v6(filename, attr):
    out = safefile.open(filename, "w", codec="utf-8")
    out.write(u'<?xml version="1.0" encoding="UTF-8"?>\n'
              u'<node>\n'
              u'<version>%d</version>\n' % attr["version"])
    plist.dump(attr, out, indent=2, depth=0)
    out.write(u'</node>\n')
    out.close()
Example #16
0
 def write_empty_data_file(self):
     """Initializes an empty data file on file-system"""
     datafile = self.get_data_file()
     
     try:
         out = safefile.open(datafile, "w")
         out.write(BLANK_NOTE)
         out.close()
     except IOError, e:
         raise NoteBookError("Cannot initialize richtext file '%s'" % datafile, e)
Example #17
0
 def write_empty_data_file(self):
     """Initializes an empty data file on file-system"""
     datafile = self.get_data_file()
     
     try:
         out = safefile.open(datafile, "w")
         out.write(BLANK_NOTE)
         out.close()
     except IOError, e:
         raise NoteBookError("Cannot initialize richtext file '%s'" % datafile, e)
Example #18
0
    def test2(self):
        """test unsuccessful write"""

        filename = _tmpdir + "/safefile"

        # make file
        self.test1()

        try:
            out = safefile.open(filename, "w")

            out.write("hello2\n")
            raise Exception("oops")
            out.write("there2")
            out.close()
        except:
            pass

        self.assertEquals(safefile.open(filename, codec="utf-8").read(),
                          u"\u2022 hello\nthere")
        self.assertEquals(os.path.exists(out.get_tempfile()), True)
Example #19
0
 def write(self, obj, filename):
     if isinstance(filename, basestring):
         #out = codecs.open(filename, "w", "utf-8")
         out = safefile.open(filename, "w", codec="utf-8")
         #out = file(filename, "w")
         need_close = True
     else:
         out = filename
         need_close = False
     
     out.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>")
     self._root_tag.write(obj, out)
     out.write("\n")
     if need_close:
         out.close()
Example #20
0
    def write(self, obj, filename):
        if isinstance(filename, basestring):
            #out = codecs.open(filename, "w", "utf-8")
            out = safefile.open(filename, "w", codec="utf-8")
            #out = file(filename, "w")
            need_close = True
        else:
            out = filename
            need_close = False

        out.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>")
        self._root_tag.write(obj, out)
        out.write("\n")
        if need_close:
            out.close()
Example #21
0
    def open_file(self, nodeid, filename, mode="r", codec=None, _path=None):
        """Open a node file"""
        path = self._get_node_path(nodeid) if _path is None else _path
        fullname = get_node_filename(path, filename)
        dirpath = os.path.dirname(fullname)
        if not os.path.exists(dirpath):
            os.makedirs(dirpath)

        stream = safefile.open(fullname, mode, codec=codec)

        # TODO: this should check and update the mtime
        # but only update if it was previously consistent (before the open)
        # update mtime since file creation causes directory mtime to change
        if self._index:
            self._index.set_node_mtime(nodeid, os.stat(path).st_mtime)

        return stream
Example #22
0
    def write_meta_data2(self):
        try:
            out = safefile.open(self.get_meta_file(), "w")
            out.write(XML_HEADER)
            out.write("<node>\n" "<version>2</version>\n")

            for key, val in self._attr.iteritems():
                attr = self._notebook.notebook_attrs.get(key, None)

                if attr is not None:
                    out.write('<attr key="%s">%s</attr>\n' %
                              (key, attr.write(val)))

            out.write("</node>\n")
            out.close()
        except Exception, e:
            raise NoteBookError("Cannot write meta data", e)
Example #23
0
    def save(self, textbuffer, filename, title=None):
        """Save buffer contents to file"""

        self._save_images(textbuffer, filename)

        try:
            buffer_contents = iter_buffer_contents(textbuffer, None, None,
                                                   ignore_tag)

            out = safefile.open(filename, "wb", codec="utf-8")
            self._html_buffer.set_output(out)
            self._html_buffer.write(buffer_contents,
                                    textbuffer.tag_table,
                                    title=title)
            out.close()
        except IOError, e:
            raise RichTextError("Could not save '%s'." % filename, e)
Example #24
0
    def _write_attr(self, filename, attr):
        """Write a node meta data file"""

        self._attr_mask.set_dict(attr)

        try:
            out = safefile.open(filename, "w", codec="utf-8")
            out.write(u'<?xml version="1.0" encoding="UTF-8"?>\n'
                      u'<node>\n'
                      u'<version>%d</version>\n' % 
                      attr.get("version", 
                               keepnote.notebook.NOTEBOOK_FORMAT_VERSION))
            plist.dump(self._attr_mask, out, indent=2, depth=0)
            out.write(u'</node>\n')
            out.close()
        except Exception, e:
            raise ConnectionError(
                _("Cannot write meta data" + " " + filename + ":" + str(e)), e)
Example #25
0
    def write_meta_data2(self):
        try:
            out = safefile.open(self.get_meta_file(), "w")
            out.write(XML_HEADER)
            out.write("<node>\n"
                      "<version>2</version>\n")

            for key, val in self._attr.iteritems():
                attr = self._notebook.notebook_attrs.get(key, None)

                if attr is not None:
                    out.write('<attr key="%s">%s</attr>\n' %
                              (key, attr.write(val)))

            out.write("</node>\n")
            out.close()
        except Exception, e:
            raise NoteBookError("Cannot write meta data", e)
Example #26
0
    def _write_attr(self, filename, attr):
        """Write a node meta data file"""

        self._attr_mask.set_dict(attr)

        try:
            out = safefile.open(filename, "w", codec="utf-8")
            out.write(u'<?xml version="1.0" encoding="UTF-8"?>\n'
                      u'<node>\n'
                      u'<version>%d</version>\n' % 
                      attr.get("version", 
                               keepnote.notebook.NOTEBOOK_FORMAT_VERSION))
            plist.dump(self._attr_mask, out, indent=2, depth=0)
            out.write(u'</node>\n')
            out.close()
        except Exception as e:
            raise ConnectionError(
                _("Cannot write meta data" + " " + filename + ":" + str(e)), e)
 def open_file(self, nodeid, filename, mode="r", 
                     codec=None, _path=None):
     """Open a node file"""        
     path = self._get_node_path(nodeid) if _path is None else _path
     fullname = get_node_filename(path, filename)
     dirpath = os.path.dirname(fullname)
     if not os.path.exists(dirpath):
         os.makedirs(dirpath)
     
     stream = safefile.open(fullname, mode, codec=codec)
     
     # TODO: this should check and update the mtime
     # but only update if it was previously consistent (before the open)
     # update mtime since file creation causes directory mtime to change
     if self._index:
         self._index.set_node_mtime(nodeid, os.stat(path).st_mtime)
     
     return stream
Example #28
0
 def save(self, textbuffer, filename, title=None):
     """Save buffer contents to file"""
     
     self._save_images(textbuffer, filename)
     
     try:
         buffer_contents = iter_buffer_contents(textbuffer,
                                                None,
                                                None,
                                                ignore_tag)
         
         out = safefile.open(filename, "wb", codec="utf-8")
         self._html_buffer.set_output(out)
         self._html_buffer.write(buffer_contents,
                                 textbuffer.tag_table,
                                 title=title)
         out.close()
     except IOError, e:
         raise RichTextError("Could not save '%s'." % filename, e)
Example #29
0
    def open_file(self, nodeid, filename, mode="r", codec=None, _path=None):
        """Open a node file"""

        if mode not in "rwa":
            raise FileError("mode must be 'r', 'w', or 'a'")
        
        path = self._get_node_path(nodeid) if _path is None else _path
        fullname = get_node_filename(path, filename)
        dirpath = os.path.dirname(fullname)
        
        try:
            if not os.path.exists(dirpath):
                os.makedirs(dirpath)
            
            # NOTE: always use binary mode to ensure no 
            # Window-specific line ending conversion
            stream = safefile.open(fullname, mode + "b", codec=codec)
        except Exception, e:
            raise FileError(
                "cannot open file '%s' '%s': %s" % (nodeid, filename, str(e)), e)
Example #30
0
    def write(self):
        """Write preferences to file"""        

        try:
            if not os.path.exists(self._pref_dir):
                init_user_pref_dir(self._pref_dir)
            
            out = safefile.open(get_user_pref_file(self._pref_dir), "w", 
                                codec="utf-8")
            out.write(u'<?xml version="1.0" encoding="UTF-8"?>\n'
                      u'<keepnote>\n'
                      u'<pref>\n')
            plist.dump(self._data, out, indent=4, depth=4)
            out.write(u'</pref>\n'
                      u'</keepnote>\n')
            
            out.close()
                                         
        except (IOError, OSError), e:
            log_error(e, sys.exc_info()[2])
            raise NoteBookError(_("Cannot save preferences"), e)
Example #31
0
    def write(self):
        """Write preferences to file"""

        try:
            if not os.path.exists(self._pref_dir):
                init_user_pref_dir(self._pref_dir)

            out = safefile.open(get_user_pref_file(self._pref_dir),
                                "w",
                                codec="utf-8")
            out.write(u'<?xml version="1.0" encoding="UTF-8"?>\n'
                      u'<keepnote>\n'
                      u'<pref>\n')
            plist.dump(self._data, out, indent=4, depth=4)
            out.write(u'</pref>\n' u'</keepnote>\n')

            out.close()

        except (IOError, OSError), e:
            log_error(e, sys.exc_info()[2])
            raise NoteBookError(_("Cannot save preferences"), e)
def write_meta_data(node):

    try:
        filename = notebooklib.get_node_meta_file(node.get_path())
        out = safefile.open(filename, "w")
        out.write(notebooklib.XML_HEADER)
        out.write("<node>\n" "<version>2</version>\n")

        for key, val in node._attr.iteritems():
            attr = node._notebook.notebook_attrs.get(key, None)

            if attr is not None:
                out.write('<attr key="%s">%s</attr>\n' %
                          (key, escape(attr.write(val))))

            elif key == "content_type":
                out.write('<attr key="content_type">%s</attr>\n' % escape(val))

        out.write("</node>\n")
        out.close()
    except Exception as e:
        raise notebooklib.NoteBookError("Cannot write meta data", e)
Example #33
0
def import_txt(node, filename, index=None, task=None):
    """
    Import a text file into the notebook

    node     -- node to attach folder to
    filename -- filename of text file to import
    task     -- Task object to track progress
    """

    # TODO: handle spaces correctly

    if task is None:
        # create dummy task if needed
        task = tasklib.Task()
    

    child = node.new_child(notebooklib.CONTENT_TYPE_PAGE, 
                           os.path.basename(filename), index)
    child.set_attr("title", os.path.basename(filename)) # remove for 0.6.4

    lines = open(filename).readlines()
    
    out = safefile.open(child.get_data_file(), "w", codec="utf-8")
    out.write(u"""<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><body>""")

    lines = [escape_whitespace(escape(line)) for line in lines]
    text = "".join(lines)

    # replace newlines
    text = text.replace(u"\n", u"<br/>")
    text = text.replace(u"\r", u"")

    out.write(text)
    out.write(u"</body></html>")

    out.close()
    task.finish()
Example #34
0
def import_txt(node, filename, index=None, task=None):
    """
    Import a text file into the notebook

    node     -- node to attach folder to
    filename -- filename of text file to import
    task     -- Task object to track progress
    """

    # TODO: handle spaces correctly

    if task is None:
        # create dummy task if needed
        task = tasklib.Task()

    child = node.new_child(notebooklib.CONTENT_TYPE_PAGE,
                           os.path.basename(filename), index)
    child.set_attr("title", os.path.basename(filename))  # remove for 0.6.4

    lines = open(filename).readlines()

    out = safefile.open(child.get_data_file(), "w", codec="utf-8")
    out.write(
        u"""<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><body>""")

    lines = [escape_whitespace(escape(line)) for line in lines]
    text = "".join(lines)

    # replace newlines
    text = text.replace(u"\n", u"<br/>")
    text = text.replace(u"\r", u"")

    out.write(text)
    out.write(u"</body></html>")

    out.close()
    task.finish()
def write_meta_data(node):
    
    try:
        filename = notebooklib.get_node_meta_file(node.get_path())
        out = safefile.open(filename, "w")
        out.write(notebooklib.XML_HEADER)
        out.write("<node>\n"
                  "<version>2</version>\n")

        for key, val in node._attr.iteritems():
            attr = node._notebook.notebook_attrs.get(key, None)

            if attr is not None:
                out.write('<attr key="%s">%s</attr>\n' %
                          (key, escape(attr.write(val))))
                
            elif key == "content_type":
                out.write('<attr key="content_type">%s</attr>\n' % escape(val))

        out.write("</node>\n")
        out.close()
    except Exception, e:
        raise notebooklib.NoteBookError("Cannot write meta data", e)
Example #36
0
def make_catalog_page(child,text,task) :
        #node, filename, index=None, task=None):
    """
    Insert a listing of files into current page

    child     -- write into this node
    text -- formatted listing of files or content to insert into page
    task     -- Task object to track progress

    filename -- filename of text file to import
    """

    # TODO: handle spaces correctly

    if task is None:
        # create dummy task if needed
        task = tasklib.Task()
    

    #child = node.new_child(notebooklib.CONTENT_TYPE_PAGE, os.path.basename(filename), index)
    #child.set_attr("title", os.path.basename(filename)) # remove for 0.6.4

    #text = open(filename).read()
    
    out = safefile.open(child.get_data_file(), "w", codec="utf-8")
    out.write(u"""<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><body>""")

    #text = escape(text)
    text = text.replace(u"\n", u"<br/>")
    text = text.replace(u"\r", u"")

    out.write(text)
    out.write(u"</body></html>")

    out.close()
    task.finish()      
Example #37
0
def make_catalog_page(child,text,task) :
        #node, filename, index=None, task=None):
    """
    Insert a listing of files into current page

    child     -- write into this node
    text -- formatted listing of files or content to insert into page
    task     -- Task object to track progress

    filename -- filename of text file to import
    """

    # TODO: handle spaces correctly

    if task is None:
        # create dummy task if needed
        task = tasklib.Task()
    

    #child = node.new_child(notebooklib.CONTENT_TYPE_PAGE, os.path.basename(filename), index)
    #child.set_attr("title", os.path.basename(filename)) # remove for 0.6.4

    #text = open(filename).read()
    
    out = safefile.open(child.get_data_file(), "w", codec="utf-8")
    out.write(u"""<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><body>""")

    #text = escape(text)
    text = text.replace(u"\n", u"<br/>")
    text = text.replace(u"\r", u"")

    out.write(text)
    out.write(u"</body></html>")

    out.close()
    task.finish()      
Example #38
0
def import_nmap(node, filename, index=None, task=None):
    """
    Import a nmap XML single file into the notebook

    node     -- node to attach folder to
    filename -- filename of text file to import
    task     -- Task object to track progress
    """

    # TODO: handle spaces correctly

    if task is None:
        # create dummy task if needed
        task = tasklib.Task()

    nmapxml = minidom.parse(filename)

    # Searching for up and down numbers
    nhostup = nmapxml.getElementsByTagName("hosts")[0].getAttribute("up")
    nhostdown = nmapxml.getElementsByTagName("hosts")[0].getAttribute("down")
    scanstats = "(%s up, %s down)" % (nhostup, nhostdown)
    originaltitle = node.get_attr("title")
    node.set_attr("title", "%s %s" % (originaltitle, scanstats))

    # Create a Up and Down folder
    uphostsfolder = node.new_child(notebooklib.CONTENT_TYPE_DIR, ("Up"), index)
    uphostsfolder.set_attr("title", ("Up (%s)" % nhostup))
    # Create a Up and Down folder
    downhostsfolder = node.new_child(notebooklib.CONTENT_TYPE_DIR, ("Down"),
                                     index)
    downhostsfolder.set_attr("title", ("Down (%s)" % nhostdown))

    noportsopenfolder = uphostsfolder.new_child(notebooklib.CONTENT_TYPE_DIR,
                                                ("No Ports Open"), index)
    noportsopenfolder.set_attr("title", ("No Ports Open"))
    withportsopenfolder = uphostsfolder.new_child(notebooklib.CONTENT_TYPE_DIR,
                                                  ("With Ports Open"), index)
    withportsopenfolder.set_attr("title", "With Ports Open")

    for hostnode in nmapxml.getElementsByTagName("host"):

        hstatus = "Unknown"
        hstatusreason = "Unknown"
        hstatusreasonttl = "Unknown"
        haddress = "Unknown"
        hosfirstmatch = None
        newhostnode = None
        icon = None
        detectedos = []

        if len(hostnode.getElementsByTagName("status")) > 0:
            hstatusnode = hostnode.getElementsByTagName("status")[0]
            hstatus = hstatusnode.getAttribute("state")
            hstatusreason = hstatusnode.getAttribute("reason")
            hstatusreasonttl = hstatusnode.getAttribute("reason_ttl")

        if (len(hostnode.getElementsByTagName("address")) > 0):
            haddress = hostnode.getElementsByTagName(
                "address")[0].getAttribute("addr")

        if len(hostnode.getElementsByTagName("os")) > 0:
            oscount = 0
            for osmatch in hostnode.getElementsByTagName(
                    "os")[0].getElementsByTagName("osmatch"):
                osname = osmatch.getAttribute("name")
                osaccuracy = osmatch.getAttribute("accuracy")
                osfamily = osmatch.getElementsByTagName(
                    "osclass")[0].getAttribute("osfamily")
                if osfamily is None:
                    osfamily = osmatch.getElementsByTagName(
                        "osclass")[0].getAttribute("family")
                osvendor = osmatch.getElementsByTagName(
                    "osclass")[0].getAttribute("vendor")
                ostype = osmatch.getElementsByTagName(
                    "osclass")[0].getAttribute("type")
                detectedos.append(
                    [osname, osaccuracy, ostype, osvendor, osfamily])
                if oscount == 0:
                    hosfirstmatch = osname
                    icon = get_os_icon(hosfirstmatch)
                oscount += 1

            # If no OS was identified by nmap
            if oscount == 0:
                mypath = os.path.dirname(os.path.abspath(__file__))
                icon = "%s/icons/question.png" % mypath

        # Create the folder with the first IP obtained and the fist hostname
        hnames = []
        if len(hostnode.getElementsByTagName("hostnames")) > 0:
            for hostname in hostnode.getElementsByTagName(
                    "hostnames")[0].getElementsByTagName("hostname"):
                hnames.append([
                    hostname.getAttribute("name"),
                    hostname.getAttribute("type")
                ])

        if len(hnames) == 0:
            mainhostname = haddress
        else:
            if hnames[0][0] is not None:
                mainhostname = hnames[0][0]
            else:
                mainhostname = haddress

        # New host node hanging from "Up" or "Down" folder
        if (hstatus == "up"):
            if len(hostnode.getElementsByTagName("ports")) > 0:
                newhostnode = withportsopenfolder.new_child(
                    notebooklib.CONTENT_TYPE_DIR,
                    ("%s - %s") % (haddress, mainhostname), index)
            else:
                newhostnode = noportsopenfolder.new_child(
                    notebooklib.CONTENT_TYPE_DIR,
                    ("%s - %s") % (haddress, mainhostname), index)
        else:
            newhostnode = downhostsfolder.new_child(
                notebooklib.CONTENT_TYPE_DIR,
                ("%s - %s") % (haddress, mainhostname), index)
        newhostnode.set_attr("title", ("%s - %s") % (haddress, mainhostname))

        # Create a page with status reason of the host and other information
        statusnode = newhostnode.new_child(notebooklib.CONTENT_TYPE_PAGE,
                                           "Status Information", None)
        statusout = safefile.open(statusnode.get_data_file(),
                                  "w",
                                  codec="utf-8")
        statusout.write(
            """<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><body>"""
        )
        statusout.write("<b>Status:</b> %s<br/>" % hstatus)
        statusout.write("<b>Status Reason:</b> %s<br/>" % hstatusreason)
        statusout.write("<b>Status Reason TTL:</b> %s<br/>" % hstatusreasonttl)
        statusout.write("</body></html>")
        statusout.close()

        if len(hostnode.getElementsByTagName("os")) > 0:
            osinfonode = newhostnode.new_child(notebooklib.CONTENT_TYPE_PAGE,
                                               "OS Information", None)
            osinfonode = safefile.open(osinfonode.get_data_file(),
                                       "w",
                                       codec="utf-8")
            osinfonode.write(
                """<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><body>"""
            )

            if icon is not None:
                osinfonode.write("<br/>")
                osinfonode.write("<img src=\"%s\"/><br/>" % icon)

            for detected_os in detectedos:
                osinfonode.write("-----------------------------------<br/>")
                osinfonode.write("<b>OS Name:</b> %s<br/>" % detected_os[0])
                osinfonode.write("<b>OS Accuracy:</b> %s<br/>" %
                                 detected_os[1])
                osinfonode.write("<b>OS Type:</b> %s<br/>" % detected_os[2])
                osinfonode.write("<b>OS Vendor:</b> %s<br/>" % detected_os[3])
                osinfonode.write("<b>OS Family:</b> %s<br/>" % detected_os[4])
                osinfonode.write("-----------------------------------<br/>")

            osinfonode.write("</body></html>")
            osinfonode.close()

        # Icon selection
        if icon is not None:
            print "Setting the icon for host %s to %s" % (haddress, icon)
            newhostnode.set_attr("icon", icon)
        else:
            # Change the color of the Host depending on the state (Up: Green, Dow: Red)
            if hstatus == "up":
                # Green
                newhostnode.set_attr("icon", "folder-green.png")
            else:
                # Red
                newhostnode.set_attr("icon", "folder-red.png")

        # Change the color of the Host depending on the state (Up: Green, Dow: Red)
        if hstatus == "up":
            # Green
            newhostnode.set_attr("title_fgcolor", "#00AA00")
        else:
            # Red
            newhostnode.set_attr("title_fgcolor", "#AA0000")

        # Create a page with multiple hostnames of this host
        if len(hnames) > 0:
            hostnamenode = newhostnode.new_child(notebooklib.CONTENT_TYPE_PAGE,
                                                 "Hostnames", None)
            hostnameout = safefile.open(hostnamenode.get_data_file(),
                                        "w",
                                        codec="utf-8")
            hostnameout.write(
                """<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><body>"""
            )
            for hnametype in hnames:
                hostnameout.write(
                    ("<b>Hostname:</b> %s. <b>Type:</b> %s<br/>") %
                    (hnametype[0], hnametype[1]))
            hostnameout.write("</body></html>")
            hostnameout.close()

        # If this host has any port information
        if len(hostnode.getElementsByTagName("ports")) > 0:
            n_udpopen = 0
            n_udpclosed = 0
            n_udpfiltered = 0
            n_tcpopen = 0
            n_tcpclosed = 0
            n_tcpfiltered = 0
            # Create a folder for TCP Ports
            tcpportfolder = newhostnode.new_child(notebooklib.CONTENT_TYPE_DIR,
                                                  ("TCP"), index)
            tcpportfolder.set_attr("title", ("TCP"))

            # Create a folder for UDP Ports
            udpportfolder = newhostnode.new_child(notebooklib.CONTENT_TYPE_DIR,
                                                  ("UDP"), index)
            udpportfolder.set_attr("title", ("UDP"))

            for port in hostnode.getElementsByTagName(
                    "ports")[0].getElementsByTagName("port"):

                pstate = "Unknown"
                pstatereason = "Unknown"
                pservicename = "Unknown"
                pserviceproduct = "Unknown"
                pserviceversion = "Unknown"
                pserviceostype = "Unknown"

                pnumber = port.getAttribute("portid")
                pprotocol = port.getAttribute("protocol")

                if len(port.getElementsByTagName("state")) > 0:
                    statenode = port.getElementsByTagName("state")[0]
                    pstate = statenode.getAttribute("state")
                    pstatereason = statenode.getAttribute("reason")

                if len(port.getElementsByTagName("service")) > 0:
                    servicenode = port.getElementsByTagName("service")[0]
                    pservicename = servicenode.getAttribute("name")
                    pserviceproduct = servicenode.getAttribute("product")
                    pserviceversion = servicenode.getAttribute("version")
                    pserviceostype = servicenode.getAttribute("ostype")

                newportchild = None
                if pprotocol.upper() == "TCP":
                    # Create the page node fot TCP
                    newportchild = tcpportfolder.new_child(
                        notebooklib.CONTENT_TYPE_PAGE, ("%s_%s - %s [%s]") %
                        (pnumber, pprotocol, pservicename, pstate))
                    newportchild.set_attr(
                        "title", ("%s_%s - %s [%s]") %
                        (pnumber, pprotocol, pservicename, pstate))
                    # Save port status stats
                    if (pstate.upper() == "OPEN"):
                        n_tcpopen += 1
                    elif (pstate.upper() == "CLOSED"):
                        n_tcpclosed += 1
                    else:
                        n_tcpfiltered += 1
                else:
                    # Create the page node fot UDP
                    newportchild = udpportfolder.new_child(
                        notebooklib.CONTENT_TYPE_PAGE, ("%s_%s - %s [%s]") %
                        (pnumber, pprotocol, pservicename, pstate))
                    newportchild.set_attr(
                        "title", ("%s_%s - %s [%s]") %
                        (pnumber, pprotocol, pservicename, pstate))
                    # Save port status stats
                    if (pstate.upper() == "OPEN"):
                        n_udpopen += 1
                    elif (pstate.upper() == "CLOSED"):
                        n_udpclosed += 1
                    else:
                        n_udpfiltered += 1

                portout = safefile.open(newportchild.get_data_file(),
                                        "w",
                                        codec="utf-8")
                portout.write(
                    """<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><body>"""
                )
                portout.write((
                    "<b>Port Number:</b> %s. <b>Protocol:</b> %s. <b>State:</b> %s. <b>State Reason:</b> %s<br/>"
                ) % (pnumber, pprotocol, pstate, pstatereason))
                portout.write((
                    "<b>Service:</b> %s. <b>Product:</b> %s. <b>version:</b> %s. <b>OS Type:</b> %s<br/>"
                ) % (pservicename, pserviceproduct, pserviceversion,
                     pserviceostype))
                portout.write("</body></html>")
                portout.close()

                # Change the color of the note depending on the state (Open: Green, Closed: Red, Filtered: Orange)
                if pstate == "open":
                    # Green
                    newportchild.set_attr("icon", "note-green.png")
                    newportchild.set_attr("title_fgcolor", "#00AA00")
                elif pstate == "filtered" or pstate == "open|filtered":
                    # Orange
                    newportchild.set_attr("icon", "note-orange.png")
                    newportchild.set_attr("title_fgcolor", "#ffa300")
                else:
                    # Red
                    newportchild.set_attr("icon", "note-red.png")
                    newportchild.set_attr("title_fgcolor", "#AA0000")

            # Update Ports UPD/TCP Stats in the title
            # For TCP
            portstats = ""
            if (n_tcpopen > 0):
                portstats += "%s open" % n_tcpopen
            if (n_tcpclosed > 0):
                if (len(portstats) > 0):
                    portstats += ","
                portstats += "%s closed" % n_tcpclosed
            if (n_tcpfiltered > 0):
                if (len(portstats) > 0):
                    portstats += ","
                portstats += "%s filtered" % n_tcpfiltered
            if (len(portstats) > 0):
                tcpportfolder.set_attr("title", "TCP (%s)" % portstats)
            # For UDP
            portstats = ""
            if (n_udpopen > 0):
                portstats += "%s open" % n_udpopen
            if (n_udpclosed > 0):
                if (len(portstats) > 0):
                    portstats += ","
                portstats += "%s closed" % n_udpclosed
            if (n_udpfiltered > 0):
                if (len(portstats) > 0):
                    portstats += ","
                portstats += "%s filtered" % n_udpfiltered
            if (len(portstats) > 0):
                udpportfolder.set_attr("title", "UDP (%s)" % portstats)

    task.finish()
Example #39
0
def import_nmap(node, filename, index=None, task=None):
    """
    Import a nmap XML single file into the notebook

    node     -- node to attach folder to
    filename -- filename of text file to import
    task     -- Task object to track progress
    """

    if task is None:
        # create dummy task if needed
        task = tasklib.Task()

    nmapxml = minidom.parse(filename)

    for hostnode in nmapxml.getElementsByTagName("host"):
        ip = ''
        mac = ''
        dns_a = []
        dns_ptr = []

        if len(hostnode.getElementsByTagName("address")) > 0:
            ip = hostnode.getElementsByTagName("address")[0].getAttribute(
                "addr")
            if len(hostnode.getElementsByTagName("address")) > 1:
                mac = hostnode.getElementsByTagName("address")[1].getAttribute(
                    "addr")
                mac += " " + hostnode.getElementsByTagName(
                    "address")[1].getAttribute("vendor")

        if len(hostnode.getElementsByTagName("hostnames")) > 0:
            for hostname in hostnode.getElementsByTagName(
                    "hostnames")[0].getElementsByTagName("hostname"):
                if hostname.getAttribute("type") == "user":
                    dns_a.append(hostname.getAttribute("name"))
                elif hostname.getAttribute("type") == "PTR":
                    dns_ptr.append(hostname.getAttribute("name"))

        hostinfo = "{ip} {hostnames} {mac}".format(ip=ip,
                                                   hostnames=','.join(dns_a +
                                                                      dns_ptr),
                                                   mac=mac)
        host = node.new_child(notebooklib.CONTENT_TYPE_PAGE, hostinfo)
        host.set_attr("title", hostinfo)
        with safefile.open(host.get_data_file(), "w", codec="utf-8") as o:
            o.write(
                """<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><body>"""
            )
            if mac:
                o.write('<span>%s</span><br/>' % mac)
            for hostname in dns_a:
                o.write('<span style="color: #00ff00">%s A</span><br/>' %
                        hostname)
            for hostname in dns_ptr:
                o.write('<span style="color: #00ffff">%s PTR</span><br/>' %
                        hostname)
            o.write("</body></html>")
            o.close()

        #host.set_attr("icon","note-green.png")

        if len(hostnode.getElementsByTagName("ports")) > 0:
            for portnode in hostnode.getElementsByTagName(
                    "ports")[0].getElementsByTagName("port"):
                pnumber = portnode.getAttribute("portid")
                pprotocol = portnode.getAttribute("protocol")

                if len(portnode.getElementsByTagName("state")) > 0:
                    statenode = portnode.getElementsByTagName("state")[0]
                    pstate = statenode.getAttribute("state")
                    pstatereason = statenode.getAttribute("reason")
                    pttl = statenode.getAttribute("reason_ttl")

                if len(portnode.getElementsByTagName("service")) > 0:
                    servicenode = portnode.getElementsByTagName("service")[0]
                    pservicename = servicenode.getAttribute("name")
                    pserviceproduct = servicenode.getAttribute("product")
                    pserviceversion = servicenode.getAttribute("version")
                    pextrainfo = servicenode.getAttribute("extrainfo")

                serviceinfo = "{service} {ver}".format(
                    service=pserviceproduct,
                    ver=pserviceversion) if pserviceproduct else pservicename
                portinfo = "{port}/{proto} ttl={ttl} {service}".format(
                    port=pnumber,
                    proto=pprotocol,
                    ttl=pttl,
                    service=serviceinfo)
                port = host.new_child(notebooklib.CONTENT_TYPE_PAGE, portinfo)
                port.set_attr("title", portinfo)
                with safefile.open(port.get_data_file(), "w",
                                   codec="utf-8") as o:
                    o.write(
                        """<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><body>"""
                    )
                    o.write(
                        "Service: %s<br/>Product: %s<br/>Version: %s<br/>Info: %s<br/>"
                        % (pservicename, pserviceproduct, pserviceversion,
                           pextrainfo))
                    o.write("</body></html>")
                    o.close()
                if pstate == 'open':
                    port.set_attr("title_fgcolor", "#00AA00")
                elif pstate == 'filtered':
                    port.set_attr("title_fgcolor", "#555555")
                elif pstate == 'closed':
                    port.set_attr("title_fgcolor", "#000000")

    task.finish()
def import_nmap(node, filename, index=None, task=None):
    """
    Import a nmap XML single file into the notebook

    node     -- node to attach folder to
    filename -- filename of text file to import
    task     -- Task object to track progress
    """

    # TODO: handle spaces correctly

    if task is None:
        # create dummy task if needed
        task = tasklib.Task()

    nmapxml = minidom.parse(filename)
    
    # Searching for up and down numbers
    nhostup=nmapxml.getElementsByTagName("hosts")[0].getAttribute("up")
    nhostdown=nmapxml.getElementsByTagName("hosts")[0].getAttribute("down")
    scanstats = "(%s up, %s down)" % (nhostup,nhostdown)
    originaltitle = node.get_attr("title")
    node.set_attr("title", "%s %s" % (originaltitle,scanstats))
    
    # Create a Up and Down folder
    uphostsfolder = node.new_child(notebooklib.CONTENT_TYPE_DIR,("Up"),index)
    uphostsfolder.set_attr("title", ("Up (%s)" % nhostup))
    # Create a Up and Down folder
    downhostsfolder = node.new_child(notebooklib.CONTENT_TYPE_DIR,("Down"),index)
    downhostsfolder.set_attr("title", ("Down (%s)" % nhostdown))
    
    noportsopenfolder = uphostsfolder.new_child(notebooklib.CONTENT_TYPE_DIR,("No Ports Open"),index)
    noportsopenfolder.set_attr("title",("No Ports Open"))
    withportsopenfolder = uphostsfolder.new_child(notebooklib.CONTENT_TYPE_DIR,("With Ports Open"),index)
    withportsopenfolder.set_attr("title","With Ports Open")
    
    for hostnode in nmapxml.getElementsByTagName("host"):
        
        hstatus = "Unknown"
        hstatusreason = "Unknown"
        hstatusreasonttl = "Unknown"
        haddress = "Unknown"
        hosfirstmatch = None
        newhostnode = None
        icon = None
        detectedos = []
        
        if len(hostnode.getElementsByTagName("status")) > 0:
            hstatusnode = hostnode.getElementsByTagName("status")[0]
            hstatus = hstatusnode.getAttribute("state")
            hstatusreason =  hstatusnode.getAttribute("reason")
            hstatusreasonttl =  hstatusnode.getAttribute("reason_ttl")
        
        if (len(hostnode.getElementsByTagName("address")) > 0):
            haddress = hostnode.getElementsByTagName("address")[0].getAttribute("addr")
        
        if len(hostnode.getElementsByTagName("os")) > 0:
            oscount = 0
            for osmatch in hostnode.getElementsByTagName("os")[0].getElementsByTagName("osmatch"):
                osname = osmatch.getAttribute("name")
                osaccuracy = osmatch.getAttribute("accuracy")
                osfamily = osmatch.getElementsByTagName("osclass")[0].getAttribute("family")
                osvendor = osmatch.getElementsByTagName("osclass")[0].getAttribute("vendor")
                ostype = osmatch.getElementsByTagName("osclass")[0].getAttribute("type")
                detectedos.append([osname,osaccuracy,ostype,osvendor,osfamily])
                if oscount == 0:
                    hosfirstmatch = osname
                    icon = get_os_icon(hosfirstmatch)
                oscount += 1
            
            # If no OS was identified by nmap
            if oscount == 0:
                mypath = os.path.dirname(os.path.abspath(__file__))
                icon = "%s/icons/question.png" % mypath
        
        # Create the folder with the first IP obtained and the fist hostname
        hnames = []
        if len(hostnode.getElementsByTagName("hostnames")) > 0:
            for hostname in hostnode.getElementsByTagName("hostnames")[0].getElementsByTagName("hostname"):
                hnames.append([hostname.getAttribute("name"),hostname.getAttribute("type")]) 
            
        if len(hnames)==0:
            mainhostname = haddress
        else:
            if hnames[0][0] is not None:
                mainhostname = hnames[0][0]
            else:
                mainhostname = haddress
        
        # New host node hanging from "Up" or "Down" folder
        if (hstatus == "up"):
            if len(hostnode.getElementsByTagName("ports")) > 0:
                newhostnode = withportsopenfolder.new_child(notebooklib.CONTENT_TYPE_DIR,("%s - %s") % (haddress,mainhostname),index)
            else:
                newhostnode = noportsopenfolder.new_child(notebooklib.CONTENT_TYPE_DIR,("%s - %s") % (haddress,mainhostname),index)
        else:
            newhostnode = downhostsfolder.new_child(notebooklib.CONTENT_TYPE_DIR,("%s - %s") % (haddress,mainhostname),index)
        newhostnode.set_attr("title",("%s - %s") % (haddress,mainhostname))
        
        # Create a page with status reason of the host and other information
        statusnode = newhostnode.new_child(notebooklib.CONTENT_TYPE_PAGE,"Status Information",None)
        statusout = safefile.open(statusnode.get_data_file(),"w",codec="utf-8")
        statusout.write("""<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><body>""")
        statusout.write("<b>Status:</b> %s<br/>" % hstatus)
        statusout.write("<b>Status Reason:</b> %s<br/>" % hstatusreason)
        statusout.write("<b>Status Reason TTL:</b> %s<br/>" % hstatusreasonttl)
        statusout.write("</body></html>")
        statusout.close()
        
        
        if len(hostnode.getElementsByTagName("os")) > 0:
            osinfonode = newhostnode.new_child(notebooklib.CONTENT_TYPE_PAGE,"OS Information",None)
            osinfonode = safefile.open(osinfonode.get_data_file(),"w",codec="utf-8")
            osinfonode.write("""<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><body>""")
            
            if icon is not None:
                osinfonode.write("<br/>")
                osinfonode.write("<img src=\"%s\"/><br/>" % icon)                
                
            for detected_os in detectedos:
                osinfonode.write("-----------------------------------<br/>")
                osinfonode.write("<b>OS Name:</b> %s<br/>" % detected_os[0])
                osinfonode.write("<b>OS Accuracy:</b> %s<br/>" % detected_os[1])
                osinfonode.write("<b>OS Type:</b> %s<br/>" % detected_os[2])
                osinfonode.write("<b>OS Vendor:</b> %s<br/>" % detected_os[3])
                osinfonode.write("<b>OS Family:</b> %s<br/>" % detected_os[4])
                osinfonode.write("-----------------------------------<br/>")
            
            osinfonode.write("</body></html>")
            osinfonode.close()
        
        # Icon selection
        if icon is not None:
            print "Setting the icon for host %s to %s" % (haddress,icon)
            newhostnode.set_attr("icon",icon)
        else:
            # Change the color of the Host depending on the state (Up: Green, Dow: Red)
            if hstatus == "up":
                # Green
                newhostnode.set_attr("icon","folder-green.png")
            else:
                # Red
                newhostnode.set_attr("icon","folder-red.png")
                
        # Change the color of the Host depending on the state (Up: Green, Dow: Red)
        if hstatus == "up":
            # Green
            newhostnode.set_attr("title_fgcolor","#00AA00")
        else:
            # Red
            newhostnode.set_attr("title_fgcolor","#AA0000")
            
        # Create a page with multiple hostnames of this host
        if len(hnames) > 0:
            hostnamenode = newhostnode.new_child(notebooklib.CONTENT_TYPE_PAGE,"Hostnames",None)
            hostnameout = safefile.open(hostnamenode.get_data_file(),"w",codec="utf-8")
            hostnameout.write("""<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><body>""")
            for hnametype in hnames:
                hostnameout.write(("<b>Hostname:</b> %s. <b>Type:</b> %s<br/>") % (hnametype[0],hnametype[1]))
            hostnameout.write("</body></html>")
            hostnameout.close()
            
        # If this host has any port information
        if len(hostnode.getElementsByTagName("ports")) > 0:
            n_udpopen = 0
            n_udpclosed = 0
            n_udpfiltered = 0
            n_tcpopen = 0
            n_tcpclosed = 0
            n_tcpfiltered = 0
            # Create a folder for TCP Ports
            tcpportfolder = newhostnode.new_child(notebooklib.CONTENT_TYPE_DIR,("TCP"),index)
            tcpportfolder.set_attr("title", ("TCP"))
            
            # Create a folder for UDP Ports
            udpportfolder = newhostnode.new_child(notebooklib.CONTENT_TYPE_DIR,("UDP"),index)
            udpportfolder.set_attr("title", ("UDP"))
            
            for port in hostnode.getElementsByTagName("ports")[0].getElementsByTagName("port"):
                
                pstate = "Unknown"
                pstatereason = "Unknown"
                pservicename = "Unknown"
                pserviceproduct = "Unknown"
                pserviceversion = "Unknown"
                pserviceostype = "Unknown"
                    
                pnumber = port.getAttribute("portid")
                pprotocol = port.getAttribute("protocol")
                
                if len(port.getElementsByTagName("state")) > 0:
                    statenode = port.getElementsByTagName("state")[0]
                    pstate = statenode.getAttribute("state")
                    pstatereason = statenode.getAttribute("reason")
                    
                if len(port.getElementsByTagName("service")) > 0:
                    servicenode = port.getElementsByTagName("service")[0]
                    pservicename = servicenode.getAttribute("name")
                    pserviceproduct = servicenode.getAttribute("product")
                    pserviceversion = servicenode.getAttribute("version")
                    pserviceostype = servicenode.getAttribute("ostype")
                
                newportchild = None
                if pprotocol.upper() == "TCP":
                    # Create the page node fot TCP
                    newportchild = tcpportfolder.new_child(notebooklib.CONTENT_TYPE_PAGE,("%s_%s - %s [%s]") % (pnumber,pprotocol,pservicename,pstate))
                    newportchild.set_attr("title",("%s_%s - %s [%s]") % (pnumber,pprotocol,pservicename,pstate))
                    # Save port status stats
                    if (pstate.upper() == "OPEN"):
                        n_tcpopen += 1
                    elif (pstate.upper() == "CLOSED"):
                        n_tcpclosed += 1
                    else:    
                        n_tcpfiltered += 1                    
                else:
                    # Create the page node fot UDP
                    newportchild = udpportfolder.new_child(notebooklib.CONTENT_TYPE_PAGE,("%s_%s - %s [%s]") % (pnumber,pprotocol,pservicename,pstate))
                    newportchild.set_attr("title",("%s_%s - %s [%s]") % (pnumber,pprotocol,pservicename,pstate))
                    # Save port status stats
                    if (pstate.upper() == "OPEN"):
                        n_udpopen += 1
                    elif (pstate.upper() == "CLOSED"):
                        n_udpclosed += 1
                    else:
                        n_udpfiltered += 1
                    
                portout = safefile.open(newportchild.get_data_file(),"w",codec="utf-8")
                portout.write("""<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><body>""")
                portout.write(("<b>Port Number:</b> %s. <b>Protocol:</b> %s. <b>State:</b> %s. <b>State Reason:</b> %s<br/>") % (pnumber,pprotocol,pstate,pstatereason))
                portout.write(("<b>Service:</b> %s. <b>Product:</b> %s. <b>version:</b> %s. <b>OS Type:</b> %s<br/>") % (pservicename,pserviceproduct,pserviceversion,pserviceostype))
                portout.write("</body></html>")
                portout.close()
                
                # Change the color of the note depending on the state (Open: Green, Closed: Red, Filtered: Orange)
                if pstate == "open":
                    # Green
                    newportchild.set_attr("icon","note-green.png")
                    newportchild.set_attr("title_fgcolor","#00AA00")
                elif pstate == "filtered" or pstate == "open|filtered":
                    # Orange
                    newportchild.set_attr("icon","note-orange.png")
                    newportchild.set_attr("title_fgcolor","#ffa300")
                else:
                    # Red
                    newportchild.set_attr("icon","note-red.png")
                    newportchild.set_attr("title_fgcolor","#AA0000")

            # Update Ports UPD/TCP Stats in the title
            # For TCP
            portstats = "" 
            if (n_tcpopen>0):
                portstats += "%s open" % n_tcpopen
            if (n_tcpclosed>0):
                if (len(portstats)>0):
                    portstats += ","
                portstats += "%s closed" % n_tcpclosed
            if (n_tcpfiltered>0):
                if (len(portstats)>0):
                    portstats += ","
                portstats += "%s filtered" % n_tcpfiltered
            if (len(portstats)>0):
                tcpportfolder.set_attr("title","TCP (%s)" % portstats)
            # For UDP
            portstats = "" 
            if (n_udpopen>0):
                portstats += "%s open" % n_udpopen
            if (n_udpclosed>0):
                if (len(portstats)>0):
                    portstats += ","
                portstats += "%s closed" % n_udpclosed
            if (n_udpfiltered>0):
                if (len(portstats)>0):
                    portstats += ","
                portstats += "%s filtered" % n_udpfiltered
            if (len(portstats)>0):
                udpportfolder.set_attr("title","UDP (%s)" % portstats)
        
                
    task.finish()
Example #41
0
def import_nmap(node, filename, index=None, task=None):
    """
    Import a MSF XML single file into the notebook

    node     -- node to attach folder to
    filename -- filename of text file to import
    task     -- Task object to track progress
    """

    if task is None:
        task = tasklib.Task()

    msfxml = minidom.parse(filename)

    hosts = []
    for hostnode in msfxml.getElementsByTagName("host"):
        if not hostnode.getElementsByTagName("address"):
            continue
        host = Host(ip=hostnode.getElementsByTagName("address")
                    [0].childNodes[0].nodeValue)
        print "[*] parsed host %s" % host.ip
        host.mac = hostnode.getElementsByTagName(
            "mac")[0].childNodes[0].nodeValue if hostnode.getElementsByTagName(
                "mac")[0].childNodes else ''
        host.hostname = hostnode.getElementsByTagName(
            "name"
        )[0].childNodes[0].nodeValue if hostnode.getElementsByTagName(
            "name")[0].childNodes else ''
        host.os = hostnode.getElementsByTagName(
            "os-name"
        )[0].childNodes[0].nodeValue if hostnode.getElementsByTagName(
            "os-name")[0].childNodes else ''
        host.info = hostnode.getElementsByTagName(
            "info"
        )[0].childNodes[0].nodeValue if hostnode.getElementsByTagName(
            "info")[0].childNodes else ''
        host.comments = hostnode.getElementsByTagName(
            "comments"
        )[0].childNodes[0].nodeValue if hostnode.getElementsByTagName(
            "comments")[0].childNodes else ''
        host.vulns = int(
            hostnode.getElementsByTagName("vuln-count")
            [0].childNodes[0].nodeValue) if hostnode.getElementsByTagName(
                "vuln-count")[0].childNodes else 0
        for servicenode in hostnode.getElementsByTagName(
                "services")[0].getElementsByTagName("service"):
            service = Service(port=servicenode.getElementsByTagName("port")
                              [0].childNodes[0].nodeValue)
            service.proto = servicenode.getElementsByTagName(
                "proto")[0].childNodes[0].nodeValue
            service.state = servicenode.getElementsByTagName(
                "state"
            )[0].childNodes[0].nodeValue if servicenode.getElementsByTagName(
                "state")[0].childNodes else ''
            service.name = servicenode.getElementsByTagName(
                "name"
            )[0].childNodes[0].nodeValue if servicenode.getElementsByTagName(
                "name")[0].childNodes else ''
            service.info = servicenode.getElementsByTagName(
                "info"
            )[0].childNodes[0].nodeValue if servicenode.getElementsByTagName(
                "info")[0].childNodes else ''
            host.services.append(service)
        hosts.append(host)

    hosts.sort(key=lambda h: h._id)
    i = 0
    for host in hosts:
        i += 1
        subnetnode = get_subnet(where=node, ip=host.ip)
        if not subnetnode:
            cidr = str(IPNetwork("%s/24" % host.ip).cidr)
            subnetnode = node.new_child(notebooklib.CONTENT_TYPE_PAGE, cidr)
            subnetnode.set_attr("title", cidr)
            with safefile.open(subnetnode.get_data_file(), "w",
                               codec="utf-8") as o:
                o.write(
                    """<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><body>"""
                )
                o.close()
            subnetnode.set_attr("icon", get_net_icon(cidr))

        hostnode = get_hostnode(where=subnetnode, ip=host.ip)
        if not hostnode:
            hostnode = subnetnode.new_child(notebooklib.CONTENT_TYPE_PAGE,
                                            host.ip)
            print "[+] %d/%d added new host %s" % (i, len(hosts), host.ip)
        else:
            print "[+] %d/%d updated host %s" % (i, len(hosts), host.ip)

        #import pdb;pdb.set_trace()
        hostinfo = "{ip} {hostname} {mac}".format(ip=host.ip,
                                                  hostname=host.hostname,
                                                  mac=host.mac)
        hostnode.set_attr("title", hostinfo)
        with safefile.open(hostnode.get_data_file(), "w", codec="utf-8") as o:
            o.write(
                """<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><body>"""
            )
            if host.mac:
                o.write('<span>%s</span><br/>' % host.mac)
            if host.hostname:
                o.write('<span>%s</span><br/>' % host.hostname)
            o.write("</body></html>")
            o.close()

        if get_os_icon(host.os):
            hostnode.set_attr("icon", get_os_icon(host.os))

        if host.vulns > 0:
            hostnode.set_attr("title_fgcolor", "#770000")
            subnetnode.set_attr("title_fgcolor", "#770000")

        if host.comments.find('wned') != -1:
            hostnode.set_attr("title_fgcolor", "#FFFFFF")
            hostnode.set_attr("title_bgcolor", "#770000")
            subnetnode.set_attr("title_fgcolor", "#FFFFFF")
            subnetnode.set_attr("title_bgcolor", "#770000")

        host.services.sort(key=lambda s: s._id)
        for service in host.services:
            servicenode = get_servicenode(where=hostnode, port=service.port)
            if not servicenode:
                servicenode = hostnode.new_child(notebooklib.CONTENT_TYPE_PAGE,
                                                 service.port)
            if service.info and get_software_icon(service.info):
                servicenode.set_attr("icon", get_software_icon(service.info))

            serviceinfo = "{port}/{proto} {name} {info}".format(
                port=service.port,
                proto=service.proto,
                name=service.name,
                info=service.info)
            servicenode.set_attr("title", serviceinfo)
            with safefile.open(servicenode.get_data_file(), "w",
                               codec="utf-8") as o:
                o.write(
                    """<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><body>"""
                )
                o.write("<span>%s</span><br/>" % (service.info))
                o.write("</body></html>")
                o.close()

            if service.state == 'filtered':
                servicenode.set_attr("title_fgcolor", "#555555")
            elif service.state == 'closed':
                servicenode.set_attr("title_fgcolor", "#000000")

    task.finish()