root/branch/NetworkInventory/umitCore/UmitDB.py @ 3878

Revision 3878, 9.2 kB (checked in by gpolo, 4 years ago)

Merged revisions 3858,3876-3877 via svnmerge from
http://svn.umitproject.org/svnroot/umit/trunk

........

r3858 | nopper | 2008-12-21 10:52:40 -0200 (Sun, 21 Dec 2008) | 1 line


Fixing horror

........

r3876 | getxsick | 2008-12-25 19:31:37 -0200 (Thu, 25 Dec 2008) | 1 line


#191: psyco is off if UMIT_DEVELOPMENT was set

........

r3877 | getxsick | 2008-12-25 20:08:50 -0200 (Thu, 25 Dec 2008) | 1 line


Added missing import of I18N

........

Line 
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3#
4# Copyright (C) 2005-2006 Insecure.Com LLC.
5# Copyright (C) 2007-2008 Adriano Monteiro Marques
6#
7# Author: Adriano Monteiro Marques <adriano@umitproject.org>
8#
9# This program is free software; you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation; either version 2 of the License, or
12# (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program; if not, write to the Free Software
21# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22
23import md5
24
25from umitCore.I18N import _
26
27sqlite = None
28OperationalError = None
29try:
30    from pysqlite2 import dbapi2 as sqlite
31    from pysqlite2.dbapi2 import OperationalError
32except ImportError:
33    try:
34        # In case this script is been running under python2.5 with sqlite3
35        import sqlite3 as sqlite
36    except ImportError:
37        raise ImportError(_("No module named dbapi2.pysqlite2 or sqlite3"))
38    from sqlite3 import OperationalError
39
40from time import time
41
42from umitCore.Paths import Path
43from umitCore.UmitLogging import log
44
45
46umitdb = ""
47
48try:
49    umitdb = Path.umitdb
50except:
51    import os.path
52    from BasePaths import base_paths
53   
54    umitdb = os.path.join(Path.config_dir, base_paths["umitdb"])
55    Path.umitdb = umitdb
56
57
58from os.path import exists, dirname
59from os import access, R_OK, W_OK
60
61
62using_memory = False
63connection = None
64try:
65    connection = sqlite.connect(umitdb)
66except OperationalError:
67    using_memory = True
68    connection = sqlite.connect(":memory:")
69
70
71class Table(object):
72    def __init__(self, table_name):
73        self.table_name = table_name
74        self.table_id = "%s_id" % table_name
75       
76        self.cursor = connection.cursor()
77   
78    def get_item(self, item_name):
79        if self.__getattribute__("_%s" % item_name):
80            return self.__getattribute__("_%s" % item_name)
81
82        sql = "SELECT %s FROM %s WHERE %s_id = %s" % (item_name,
83                                                      self.table_name,
84                                                      self.table_name,
85                                           self.__getattribute__(self.table_id))
86
87        self.cursor.execute(sql)
88       
89        self.__setattr__("_%s" % item_name, self.cursor.fetchall()[0][0])
90        return self.__getattribute__("_%s" % item_name)
91
92    def set_item(self, item_name, item_value):
93        if item_value == self.__getattribute__("_%s" % item_name):
94            return None
95       
96        sql = "UPDATE %s SET %s = ? WHERE %s_id = %s" % (self.table_name,
97                                                         item_name,
98                                                         self.table_name,
99                                           self.__getattribute__(self.table_id))
100        self.cursor.execute(sql, (item_value,))
101        connection.commit()
102        self.__setattr__("_%s" % item_name, item_value)
103
104    def insert(self, **kargs):
105        sql = "INSERT INTO %s ("
106        for k in kargs.keys():
107            sql += k
108            sql += ", "
109        else:
110            sql = sql[:][:-2]
111            sql += ") VALUES ("
112           
113        for v in xrange(len(kargs.values())):
114            sql += "?, "
115        else:
116            sql = sql[:][:-2]
117            sql += ")"
118
119        sql %= self.table_name
120       
121        self.cursor.execute(sql, tuple(kargs.values()))
122        connection.commit()
123
124        sql = "SELECT MAX(%s_id) FROM %s;" % (self.table_name, self.table_name)
125        self.cursor.execute(sql)
126        return self.cursor.fetchall()[0][0]
127
128class UmitDB(object):
129    def __init__(self):
130        self.cursor = connection.cursor()
131
132    def create_db(self):
133        drop_string = ("DROP TABLE scans;",)
134       
135        try:
136            for d in drop_string:
137                self.cursor.execute(d)
138        except:
139            connection.rollback()
140        else:
141            connection.commit()
142
143       
144        creation_string = ("""CREATE TABLE scans (scans_id INTEGER \
145                                                  PRIMARY KEY AUTOINCREMENT,
146                                                  scan_name TEXT,
147                                                  nmap_xml_output TEXT,
148                                                  digest TEXT,
149                                                  date INTEGER)""",)
150
151        for c in creation_string:
152            self.cursor.execute(c)
153            connection.commit()
154
155    def add_scan(self, **kargs):
156        return Scans(**kargs)
157
158    def get_scans_ids(self):
159        sql = "SELECT scans_id FROM scans;"
160        self.cursor.execute(sql)
161        return [sid[0] for sid in self.cursor.fetchall()]
162
163    def get_scans(self):
164        scans_ids = self.get_scans_ids()
165        for sid in scans_ids:
166            yield Scans(scans_id=sid)
167
168    def cleanup(self, save_time):
169        log.debug(">>> Cleanning up data base.")
170        log.debug(">>> Removing results olders than %s seconds" % save_time)
171        self.cursor.execute("SELECT scans_id FROM scans WHERE date < ?",
172                            (time() - save_time,))
173       
174        for sid in [sid[0] for sid in self.cursor.fetchall()]:
175            log.debug(">>> Removing results with scans_id %s" % sid)
176            self.cursor.execute("DELETE FROM scans WHERE scans_id = ?", (sid, ))
177        else:
178            connection.commit()
179            log.debug(">>> Data base sucessfully cleanned up!")
180       
181
182class Scans(Table, object):
183    def __init__(self, **kargs):
184        Table.__init__(self, "scans")
185        if "scans_id" in kargs.keys():
186            self.scans_id = kargs["scans_id"]
187        else:
188            log.debug(">>>Ceating new scan result entry at data base")
189            fields = ["scan_name", "nmap_xml_output", "date"]
190           
191            for k in kargs.keys():
192                if k not in fields:
193                    raise Exception("Wrong table field passed to creation \
194method. '%s'" % k)
195
196            if "nmap_xml_output" not in kargs.keys() or \
197               not kargs["nmap_xml_output"]:
198                raise Exception("Can't save result without xml output")
199
200            if not self.verify_digest(md5.new(kargs["nmap_xml_output"]).\
201                                      hexdigest()):
202                raise Exception("XML output registered already!")
203           
204            self.scans_id = self.insert(**kargs)
205
206    def verify_digest(self, digest):
207        self.cursor.execute("SELECT scans_id FROM scans WHERE digest = ?",
208                            (digest, ))
209        result = self.cursor.fetchall()
210        if result:
211            return False
212        return True
213
214    def add_host(self, **kargs):
215        kargs.update({self.table_id:self.scans_id})
216        return Hosts(**kargs)
217
218    def get_hosts(self):
219        sql = "SELECT hosts_id FROM hosts WHERE scans_id= %s" % self.scans_id
220       
221        self.cursor.execute(sql)
222        result = self.cursor.fetchall()
223       
224        for h in result:
225            yield Hosts(hosts_id=h[0])
226
227    def get_scans_id(self):
228        return self._scans_id
229
230    def set_scans_id(self, scans_id):
231        if scans_id != self._scans_id:
232            self._scans_id = scans_id
233
234    def get_scan_name(self):
235        return self.get_item("scan_name")
236
237    def set_scan_name(self, scan_name):
238        self.set_item("scan_name", scan_name)
239
240    def get_nmap_xml_output(self):
241        return self.get_item("nmap_xml_output")
242
243    def set_nmap_xml_output(self, nmap_xml_output):
244        self.set_item("nmap_xml_output", nmap_xml_output)
245        self.set_item("digest", md5.new(nmap_xml_output).hexdigest())
246
247    def get_date(self):
248        return self.get_item("date")
249
250    def set_date(self, date):
251        self.set_item("date", date)
252   
253    scans_id = property(get_scans_id, set_scans_id)
254    scan_name = property(get_scan_name, set_scan_name)
255    nmap_xml_output = property(get_nmap_xml_output, set_nmap_xml_output)
256    date = property(get_date, set_date)
257   
258    _scans_id = None
259    _scan_name = None
260    _nmap_xml_output = None
261    _date = None
262
263
264######################################################################
265# Verify if data base exists and if it does have the required tables.
266# If something is wrong, re-create table
267def verify_db():
268    cursor = connection.cursor()
269    try:
270        cursor.execute("SELECT scans_id FROM scans WHERE date = 0")
271    except sqlite.OperationalError:
272        u = UmitDB()
273        u.create_db()
274verify_db()
275
276######################################################################
277
278if __name__ == "__main__":
279    from pprint import pprint
280    import psyco
281    psyco.profile()
282   
283    u = UmitDB()
284
285    #print "Creating Data Base"
286    #u.create_db()
287
288    #print "Creating new scan"
289    #s = u.add_scan(scan_name="Fake scan", nmap_xml_output="", date="007")
290
291    #s = Scans(scans_id=2)
292    #print s.scans_id
293    #print s.scan_name
294    #print s.nmap_xml_output
295    #print s.date
296   
297    sql = "SELECT * FROM scans;"
298    u.cursor.execute(sql)
299    print "Scans:",
300    pprint(u.cursor.fetchall())
Note: See TracBrowser for help on using the browser.