| 1 | # Copyright (C) 2007 Adriano Monteiro Marques |
|---|
| 2 | # |
|---|
| 3 | # Author: Guilherme Polo <ggpolo@gmail.com> |
|---|
| 4 | # |
|---|
| 5 | # This program is free software; you can redistribute it and/or modify |
|---|
| 6 | # it under the terms of the GNU General Public License as published by |
|---|
| 7 | # the Free Software Foundation; either version 2 of the License, or |
|---|
| 8 | # (at your option) any later version. |
|---|
| 9 | # |
|---|
| 10 | # This program is distributed in the hope that it will be useful, |
|---|
| 11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 13 | # GNU General Public License for more details. |
|---|
| 14 | # |
|---|
| 15 | # You should have received a copy of the GNU General Public License |
|---|
| 16 | # along with this program; if not, write to the Free Software |
|---|
| 17 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
|---|
| 18 | # USA |
|---|
| 19 | |
|---|
| 20 | from umit.db.Utils import empty |
|---|
| 21 | from umit.db.Utils import log_debug |
|---|
| 22 | from umit.db.Utils import normalize |
|---|
| 23 | from umit.db.Connection import ConnectDB |
|---|
| 24 | |
|---|
| 25 | """ |
|---|
| 26 | Missing methods for: |
|---|
| 27 | Traceroute retrieval. |
|---|
| 28 | """ |
|---|
| 29 | |
|---|
| 30 | debug = log_debug('umit.db.Retrieve') |
|---|
| 31 | |
|---|
| 32 | class RawRetrieve: |
|---|
| 33 | """ |
|---|
| 34 | Retrieve raw data from database. |
|---|
| 35 | """ |
|---|
| 36 | |
|---|
| 37 | def __init__(self, conn, cursor): |
|---|
| 38 | """ |
|---|
| 39 | Expects a conn and cursor from database connection. |
|---|
| 40 | """ |
|---|
| 41 | self.conn = conn |
|---|
| 42 | self.cursor = cursor |
|---|
| 43 | |
|---|
| 44 | |
|---|
| 45 | def get_service_info_id_from_db(self, info, service_name_id): |
|---|
| 46 | """ |
|---|
| 47 | Get service_info id based on data from info and service_name_id. |
|---|
| 48 | """ |
|---|
| 49 | debug("Getting pk for service_info..") |
|---|
| 50 | |
|---|
| 51 | normalize(info) |
|---|
| 52 | |
|---|
| 53 | info["ostype"] = empty() # ToFix: Parser isnt storing this |
|---|
| 54 | |
|---|
| 55 | data = ( |
|---|
| 56 | info["product"], info["version"], |
|---|
| 57 | info["extrainfo"], info["method"], |
|---|
| 58 | info["conf"], service_name_id) |
|---|
| 59 | |
|---|
| 60 | s_id = self.cursor.execute("SELECT pk FROM service_info " |
|---|
| 61 | "WHERE product = ? AND version = ? AND extrainfo = ? AND " |
|---|
| 62 | "method = ? AND conf = ? AND fk_service_name = ?", data).fetchone() |
|---|
| 63 | |
|---|
| 64 | if s_id: |
|---|
| 65 | return s_id[0] |
|---|
| 66 | |
|---|
| 67 | |
|---|
| 68 | def get_port_id_from_db(self, portid, fk_service_info, fk_protocol, |
|---|
| 69 | fk_port_state): |
|---|
| 70 | """ |
|---|
| 71 | Get port id from database. |
|---|
| 72 | """ |
|---|
| 73 | debug("Getting pk for port..") |
|---|
| 74 | |
|---|
| 75 | p_id = self.cursor.execute("SELECT pk FROM port " |
|---|
| 76 | "WHERE portid = ? AND fk_service_info = ? AND fk_protocol = ? AND " |
|---|
| 77 | "fk_port_state = ?", (portid, fk_service_info, fk_protocol, |
|---|
| 78 | fk_port_state)).fetchone() |
|---|
| 79 | |
|---|
| 80 | if p_id: |
|---|
| 81 | return p_id[0] |
|---|
| 82 | |
|---|
| 83 | |
|---|
| 84 | def get_service_name_id_from_db(self, service_name): |
|---|
| 85 | """ |
|---|
| 86 | Get id from service_name for service_name. |
|---|
| 87 | """ |
|---|
| 88 | debug("Getting pk for service_name..") |
|---|
| 89 | |
|---|
| 90 | s_id = self.cursor.execute("SELECT pk FROM service_name " |
|---|
| 91 | "WHERE name = ?", (service_name, )).fetchone() |
|---|
| 92 | |
|---|
| 93 | if s_id: |
|---|
| 94 | return s_id[0] |
|---|
| 95 | |
|---|
| 96 | |
|---|
| 97 | def get_hostname_id_from_db(self, hostname): |
|---|
| 98 | """ |
|---|
| 99 | Return hostname id from database based on type and name. |
|---|
| 100 | """ |
|---|
| 101 | debug("Getting pk for hostname..") |
|---|
| 102 | |
|---|
| 103 | h_id = self.cursor.execute("SELECT pk FROM hostname " |
|---|
| 104 | "WHERE type = ? AND name = ?", (hostname["type"], |
|---|
| 105 | hostname["name"])).fetchone() |
|---|
| 106 | |
|---|
| 107 | if h_id: |
|---|
| 108 | return h_id[0] |
|---|
| 109 | |
|---|
| 110 | |
|---|
| 111 | def get_address_id_from_db(self, address, a_type, vendor): |
|---|
| 112 | """ |
|---|
| 113 | Return address id from database based on address, type and vendor. |
|---|
| 114 | """ |
|---|
| 115 | debug("Getting pk for address..") |
|---|
| 116 | |
|---|
| 117 | a_id = self.cursor.execute("SELECT pk FROM address " |
|---|
| 118 | "WHERE address = ? AND type = ? AND fk_vendor = ?", (address, |
|---|
| 119 | a_type, vendor)).fetchone() |
|---|
| 120 | |
|---|
| 121 | if a_id: |
|---|
| 122 | return a_id[0] |
|---|
| 123 | |
|---|
| 124 | |
|---|
| 125 | def get_address_id_for_address_from_db(self, address): |
|---|
| 126 | """ |
|---|
| 127 | Return address id from database based on address only. |
|---|
| 128 | """ |
|---|
| 129 | a_id = self.cursor.execute("SELECT pk FROM address " |
|---|
| 130 | "WHERE address=?", (address, )).fetchone() |
|---|
| 131 | |
|---|
| 132 | if a_id: |
|---|
| 133 | return a_id[0] |
|---|
| 134 | |
|---|
| 135 | |
|---|
| 136 | def get_address_for_address_id_from_db(self, fk_address): |
|---|
| 137 | """ |
|---|
| 138 | Return address from database based on address id. |
|---|
| 139 | """ |
|---|
| 140 | addr = self.cursor.execute("SELECT address FROM address WHERE pk=?", |
|---|
| 141 | (fk_address, )).fetchone() |
|---|
| 142 | |
|---|
| 143 | if addr: |
|---|
| 144 | return addr[0] |
|---|
| 145 | |
|---|
| 146 | |
|---|
| 147 | def get_address_pk_for_host_from_db(self, fk_host): |
|---|
| 148 | """ |
|---|
| 149 | Return address pk from database based on host id. |
|---|
| 150 | """ |
|---|
| 151 | self.cursor.execute("SELECT address.pk FROM address " |
|---|
| 152 | "JOIN _host_address ON (_host_address.fk_address=address.pk) " |
|---|
| 153 | "WHERE _host_address.fk_host=?", (fk_host, )) |
|---|
| 154 | addr = self.cursor.fetchone() |
|---|
| 155 | |
|---|
| 156 | if addr: |
|---|
| 157 | return addr[0] |
|---|
| 158 | |
|---|
| 159 | |
|---|
| 160 | def get_vendor_id_from_db(self, name): |
|---|
| 161 | """ |
|---|
| 162 | Return vendor id from database based on name. |
|---|
| 163 | """ |
|---|
| 164 | debug("Getting pk for vendor..") |
|---|
| 165 | |
|---|
| 166 | v_id = self.cursor.execute("SELECT pk FROM vendor WHERE name = ?", |
|---|
| 167 | (name, )).fetchone() |
|---|
| 168 | |
|---|
| 169 | if v_id: |
|---|
| 170 | return v_id[0] |
|---|
| 171 | |
|---|
| 172 | |
|---|
| 173 | def get_host_state_id_from_db(self, state): |
|---|
| 174 | """ |
|---|
| 175 | Return state id from database based on state description. |
|---|
| 176 | """ |
|---|
| 177 | debug("Getting pk for host_state..") |
|---|
| 178 | |
|---|
| 179 | h_id = self.cursor.execute("SELECT pk FROM host_state " |
|---|
| 180 | "WHERE state = ?", (state, )).fetchone() |
|---|
| 181 | |
|---|
| 182 | if h_id: |
|---|
| 183 | return h_id[0] |
|---|
| 184 | |
|---|
| 185 | |
|---|
| 186 | def get_tcp_sequence_id_from_db(self, tcpseq_dict): |
|---|
| 187 | """ |
|---|
| 188 | Return tcp_sequence id from database based on tcpsequence values. |
|---|
| 189 | """ |
|---|
| 190 | debug("Getting pk for tcp_sequence..") |
|---|
| 191 | |
|---|
| 192 | t_id = self.cursor.execute("SELECT pk FROM tcp_sequence " |
|---|
| 193 | "WHERE tcp_values = ?", (tcpseq_dict["values"], )).fetchone() |
|---|
| 194 | |
|---|
| 195 | if t_id: |
|---|
| 196 | return t_id[0] |
|---|
| 197 | |
|---|
| 198 | |
|---|
| 199 | def get_tcp_ts_sequence_id_from_db(self, tcptsseq_dict): |
|---|
| 200 | """ |
|---|
| 201 | Return tcp_sequence id from database based on tcptssequence values. |
|---|
| 202 | """ |
|---|
| 203 | debug("Getting pk for tcp_ts_sequence..") |
|---|
| 204 | |
|---|
| 205 | t_id = self.cursor.execute("SELECT pk FROM tcp_ts_sequence " |
|---|
| 206 | "WHERE tcp_ts_values = ?", (tcptsseq_dict["values"], )).fetchone() |
|---|
| 207 | |
|---|
| 208 | if t_id: |
|---|
| 209 | return t_id[0] |
|---|
| 210 | |
|---|
| 211 | |
|---|
| 212 | def get_ip_id_sequence_id_from_db(self, ipidseq_dict): |
|---|
| 213 | """ |
|---|
| 214 | Return ip_id_sequence id from database based on ipidseq values. |
|---|
| 215 | """ |
|---|
| 216 | debug("Getting pk for ip_id_sequence..") |
|---|
| 217 | |
|---|
| 218 | t_id = self.cursor.execute("SELECT pk FROM ip_id_sequence " |
|---|
| 219 | "WHERE ip_id_values = ?", (ipidseq_dict["values"], )).fetchone() |
|---|
| 220 | |
|---|
| 221 | if t_id: |
|---|
| 222 | return t_id[0] |
|---|
| 223 | |
|---|
| 224 | |
|---|
| 225 | def get_scan_type_id_from_db(self, name): |
|---|
| 226 | """ |
|---|
| 227 | Return scan_type id from database based on name. |
|---|
| 228 | """ |
|---|
| 229 | debug("Getting pk for scan_type..") |
|---|
| 230 | |
|---|
| 231 | s_id = self.cursor.execute("SELECT pk FROM scan_type " |
|---|
| 232 | "WHERE name = ?", (name, )).fetchone() |
|---|
| 233 | |
|---|
| 234 | if s_id: |
|---|
| 235 | return s_id[0] |
|---|
| 236 | |
|---|
| 237 | |
|---|
| 238 | def get_port_state_id_from_db(self, state): |
|---|
| 239 | """ |
|---|
| 240 | Return port_state id from database based on state.. |
|---|
| 241 | """ |
|---|
| 242 | debug("Getting pk for port_state..") |
|---|
| 243 | |
|---|
| 244 | p_id = self.cursor.execute("SELECT pk FROM port_state " |
|---|
| 245 | "WHERE state = ?", (state, )).fetchone() |
|---|
| 246 | |
|---|
| 247 | if p_id: |
|---|
| 248 | return p_id[0] |
|---|
| 249 | |
|---|
| 250 | |
|---|
| 251 | def get_protocol_id_from_db(self, name): |
|---|
| 252 | """ |
|---|
| 253 | Return protocol id from database based on name. |
|---|
| 254 | """ |
|---|
| 255 | debug("Getting pk for protocol..") |
|---|
| 256 | |
|---|
| 257 | p_id = self.cursor.execute("SELECT pk FROM protocol " |
|---|
| 258 | "WHERE name = ?", (name, )).fetchone() |
|---|
| 259 | |
|---|
| 260 | if p_id: |
|---|
| 261 | return p_id[0] |
|---|
| 262 | |
|---|
| 263 | |
|---|
| 264 | def get_scanner_id_from_db(self, name, version): |
|---|
| 265 | """ |
|---|
| 266 | Return scanner id from database based on scanner name and version |
|---|
| 267 | """ |
|---|
| 268 | debug("Getting pk for scanner..") |
|---|
| 269 | |
|---|
| 270 | s_id = self.cursor.execute("SELECT pk FROM scanner " |
|---|
| 271 | "WHERE name = ? AND version = ?", (name, version)).fetchone() |
|---|
| 272 | |
|---|
| 273 | if s_id: |
|---|
| 274 | return s_id[0] |
|---|
| 275 | |
|---|
| 276 | |
|---|
| 277 | def get_osgen_id_from_db(self, osgen): |
|---|
| 278 | """ |
|---|
| 279 | Get id from osgen table for osgen. |
|---|
| 280 | """ |
|---|
| 281 | debug("Getting pk for osgen..") |
|---|
| 282 | |
|---|
| 283 | o_id = self.cursor.execute("SELECT pk FROM osgen WHERE gen = ?", |
|---|
| 284 | (osgen, )).fetchone() |
|---|
| 285 | |
|---|
| 286 | if o_id: |
|---|
| 287 | return o_id[0] |
|---|
| 288 | |
|---|
| 289 | |
|---|
| 290 | def get_osfamily_id_from_db(self, osfamily): |
|---|
| 291 | """ |
|---|
| 292 | Get id from osfamily table for osfamily. |
|---|
| 293 | """ |
|---|
| 294 | debug("Getting pk for osfamily..") |
|---|
| 295 | |
|---|
| 296 | o_id = self.cursor.execute("SELECT pk FROM osfamily " |
|---|
| 297 | "WHERE family = ?", (osfamily, )).fetchone() |
|---|
| 298 | |
|---|
| 299 | if o_id: |
|---|
| 300 | return o_id[0] |
|---|
| 301 | |
|---|
| 302 | |
|---|
| 303 | def get_osvendor_id_from_db(self, osvendor): |
|---|
| 304 | """ |
|---|
| 305 | Get id from osvendor table for osvendor. |
|---|
| 306 | """ |
|---|
| 307 | debug("Getting pk for osvendor..") |
|---|
| 308 | |
|---|
| 309 | o_id = self.cursor.execute("SELECT pk FROM osvendor " |
|---|
| 310 | "WHERE vendor = ?", (osvendor, )).fetchone() |
|---|
| 311 | |
|---|
| 312 | if o_id: |
|---|
| 313 | return o_id[0] |
|---|
| 314 | |
|---|
| 315 | |
|---|
| 316 | def get_ostype_id_from_db(self, ostype): |
|---|
| 317 | """ |
|---|
| 318 | Get id from ostype table for ostype. |
|---|
| 319 | """ |
|---|
| 320 | debug("Getting pk for ostype..") |
|---|
| 321 | |
|---|
| 322 | o_id = self.cursor.execute("SELECT pk FROM ostype WHERE type = ?", |
|---|
| 323 | (ostype, )).fetchone() |
|---|
| 324 | |
|---|
| 325 | if o_id: |
|---|
| 326 | return o_id[0] |
|---|
| 327 | |
|---|
| 328 | |
|---|
| 329 | def get_inventory_id_from_db(self, inventory): |
|---|
| 330 | """ |
|---|
| 331 | Get id from inventory for inventory. |
|---|
| 332 | """ |
|---|
| 333 | debug("Getting pk for inventory..") |
|---|
| 334 | |
|---|
| 335 | i_id = self.cursor.execute("SELECT pk FROM inventory WHERE name = ?", |
|---|
| 336 | (inventory, )).fetchone() |
|---|
| 337 | |
|---|
| 338 | if i_id: |
|---|
| 339 | return i_id[0] |
|---|
| 340 | |
|---|
| 341 | |
|---|
| 342 | class CompositeRetrieve(RawRetrieve): |
|---|
| 343 | """ |
|---|
| 344 | Retrieve composite data from database. |
|---|
| 345 | """ |
|---|
| 346 | |
|---|
| 347 | def __init__(self, conn, cursor): |
|---|
| 348 | """ |
|---|
| 349 | Expects a conn and cursor from database connection. |
|---|
| 350 | """ |
|---|
| 351 | RawRetrieve.__init__(self, conn, cursor) |
|---|
| 352 | |
|---|
| 353 | |
|---|
| 354 | def get_hosts_id_for_scan_from_db(self, fk_scan): |
|---|
| 355 | """ |
|---|
| 356 | Get all hosts from database that are in fk_scan. |
|---|
| 357 | """ |
|---|
| 358 | debug("Getting hosts from scan id %d..", fk_scan) |
|---|
| 359 | |
|---|
| 360 | ids = self.cursor.execute("SELECT pk FROM host WHERE fk_scan = ?", |
|---|
| 361 | (fk_scan, )).fetchall() |
|---|
| 362 | |
|---|
| 363 | return ids |
|---|
| 364 | |
|---|
| 365 | |
|---|
| 366 | def get_addrtype_for_host_from_db(self, fk_host, addrtype): |
|---|
| 367 | """ |
|---|
| 368 | Get IPV4, IPV6 or MAC for a host. |
|---|
| 369 | """ |
|---|
| 370 | debug("Getting %s address for host id %d..", addrtype, fk_host) |
|---|
| 371 | |
|---|
| 372 | fk_address = self.cursor.execute("SELECT fk_address " |
|---|
| 373 | "FROM _host_address WHERE fk_host = ?", (fk_host, )).fetchall() |
|---|
| 374 | |
|---|
| 375 | address = None |
|---|
| 376 | for fk in fk_address: |
|---|
| 377 | address = self.cursor.execute("SELECT address FROM address " |
|---|
| 378 | "WHERE type = ? AND pk = ?", (addrtype, fk[0], )).fetchone() |
|---|
| 379 | if address: |
|---|
| 380 | break |
|---|
| 381 | |
|---|
| 382 | if address: |
|---|
| 383 | return address[0] |
|---|
| 384 | |
|---|
| 385 | |
|---|
| 386 | def get_ipv4_for_host_from_db(self, fk_host): |
|---|
| 387 | """ |
|---|
| 388 | Get IPv4 address for a host. |
|---|
| 389 | """ |
|---|
| 390 | return self.get_addrtype_for_host_from_db(fk_host, "ipv4") |
|---|
| 391 | |
|---|
| 392 | |
|---|
| 393 | def get_ipv6_for_host_from_db(self, fk_host): |
|---|
| 394 | """ |
|---|
| 395 | Get IPV6 address for a host. |
|---|
| 396 | """ |
|---|
| 397 | return self.get_addrtype_for_host_from_db(fk_host, "ipv6") |
|---|
| 398 | |
|---|
| 399 | |
|---|
| 400 | def get_mac_for_host_from_db(self, fk_host): |
|---|
| 401 | """ |
|---|
| 402 | Get MAC address associated with a host. |
|---|
| 403 | """ |
|---|
| 404 | return self.get_addrtype_for_host_from_db(fk_host, "mac") |
|---|
| 405 | |
|---|
| 406 | |
|---|
| 407 | def get_hostnames_for_host_from_db(self, fk_host): |
|---|
| 408 | """ |
|---|
| 409 | Get hostnames associated with a host. |
|---|
| 410 | """ |
|---|
| 411 | debug("Getting hostnames for host id %d..", fk_host) |
|---|
| 412 | |
|---|
| 413 | fk_hostname = self.cursor.execute("SELECT fk_hostname " |
|---|
| 414 | "FROM _host_hostname WHERE fk_host = ?", (fk_host, )).fetchall() |
|---|
| 415 | |
|---|
| 416 | hostnames = [ ] |
|---|
| 417 | for fk in fk_hostname: |
|---|
| 418 | hostname = self.cursor.execute("SELECT name FROM hostname " |
|---|
| 419 | "WHERE pk = ?", (fk[0], )).fetchone() |
|---|
| 420 | if hostname: |
|---|
| 421 | hostnames.append(hostname[0]) |
|---|
| 422 | |
|---|
| 423 | return tuple(hostnames) |
|---|
| 424 | |
|---|
| 425 | |
|---|
| 426 | def get_os_for_host_from_db(self, fk_host): |
|---|
| 427 | """ |
|---|
| 428 | Get OS for a host from database. |
|---|
| 429 | """ |
|---|
| 430 | os_name = self.cursor.execute("SELECT name FROM osmatch WHERE " |
|---|
| 431 | "fk_host=?", (fk_host, )).fetchone() |
|---|
| 432 | |
|---|
| 433 | if os_name: |
|---|
| 434 | return os_name[0] |
|---|
| 435 | |
|---|
| 436 | |
|---|
| 437 | def get_osshort_for_host_from_db(self, fk_host): |
|---|
| 438 | """ |
|---|
| 439 | Gets result from get_os_for_host_from_db and make it shorter. |
|---|
| 440 | """ |
|---|
| 441 | os_name = self.get_os_for_host_from_db(fk_host) |
|---|
| 442 | |
|---|
| 443 | if os_name: |
|---|
| 444 | os_name = os_name.split()[0] |
|---|
| 445 | |
|---|
| 446 | return os_name |
|---|
| 447 | |
|---|
| 448 | |
|---|
| 449 | def get_finish_timestamp_for_scan_from_db(self, scan): |
|---|
| 450 | """ |
|---|
| 451 | Get finish timestamp for a scan. |
|---|
| 452 | """ |
|---|
| 453 | debug("Getting finish timestamp for scan id %d..", scan) |
|---|
| 454 | |
|---|
| 455 | fts = self.cursor.execute("SELECT finish 'as finish [timestamp]' " |
|---|
| 456 | "FROM scan WHERE pk = ?", (scan, )).fetchone()[0] |
|---|
| 457 | |
|---|
| 458 | return fts |
|---|
| 459 | |
|---|
| 460 | |
|---|
| 461 | def get_scan_details_for_scan_from_db(self, scan): |
|---|
| 462 | """ |
|---|
| 463 | Get scan details for a scan. |
|---|
| 464 | """ |
|---|
| 465 | debug("Getting scan details for scan id %d..", scan) |
|---|
| 466 | |
|---|
| 467 | details = self.cursor.execute("SELECT args, xmloutputversion, " |
|---|
| 468 | "verbose, debugging, scanner.name, scanner.version " |
|---|
| 469 | "FROM scan JOIN scanner ON (scan.fk_scanner = scanner.pk) " |
|---|
| 470 | "WHERE scan.pk = ?", (scan, )).fetchone() |
|---|
| 471 | |
|---|
| 472 | return details |
|---|
| 473 | |
|---|
| 474 | |
|---|
| 475 | """ |
|---|
| 476 | (c1) Missing for port data retrieve: ostype in service_info. |
|---|
| 477 | Reason: NmapParser doesn't handle this yet. |
|---|
| 478 | """ |
|---|
| 479 | |
|---|
| 480 | def get_portid_and_state_for_host_from_db(self, host): |
|---|
| 481 | """ |
|---|
| 482 | Get only portid and port state from port table, for a host. |
|---|
| 483 | """ |
|---|
| 484 | debug("Getting portid and state for host id %d from table " |
|---|
| 485 | "port..", host) |
|---|
| 486 | |
|---|
| 487 | pst = self.cursor.execute("SELECT port.portid, port_state.state " |
|---|
| 488 | "FROM port " |
|---|
| 489 | "JOIN _host_port ON (_host_port.fk_port=port.pk) " |
|---|
| 490 | "JOIN port_state ON (port.fk_port_state=port_state.pk) " |
|---|
| 491 | " WHERE _host_port.fk_host=?", (host, )).fetchall() |
|---|
| 492 | |
|---|
| 493 | return pst |
|---|
| 494 | |
|---|
| 495 | |
|---|
| 496 | def get_portid_and_fks_for_host_from_db(self, host): |
|---|
| 497 | """ |
|---|
| 498 | Get portid and fks from port table, for a host. |
|---|
| 499 | """ |
|---|
| 500 | debug("Getting portid and foreign keys for host id %d from " |
|---|
| 501 | "table port..", host) |
|---|
| 502 | |
|---|
| 503 | pdata = self.cursor.execute("SELECT portid, fk_service_info, " |
|---|
| 504 | "fk_protocol, fk_port_state FROM port JOIN _host_port ON " |
|---|
| 505 | "(_host_port.fk_port=port.pk) WHERE _host_port.fk_host=?", |
|---|
| 506 | (host, )).fetchall() |
|---|
| 507 | |
|---|
| 508 | return pdata |
|---|
| 509 | |
|---|
| 510 | |
|---|
| 511 | def get_port_data_for_pdata_from_db(self, protocol_id, port_state_id, |
|---|
| 512 | service_info_id): |
|---|
| 513 | """ |
|---|
| 514 | Get port data based on data returned from |
|---|
| 515 | get_portid_and_fks_for_host_from_db |
|---|
| 516 | """ |
|---|
| 517 | debug("Getting port data for pdata..") |
|---|
| 518 | |
|---|
| 519 | fullpdata = self.cursor.execute("SELECT protocol.name as protocol, " |
|---|
| 520 | "port_state.state, service_info.product, " |
|---|
| 521 | "service_info.version, service_info.extrainfo, " |
|---|
| 522 | "service_info.method, service_info.conf, service_name.name " |
|---|
| 523 | "FROM protocol, port_state, service_info, service_name " |
|---|
| 524 | "WHERE protocol.pk = ? AND port_state.pk = ? AND " |
|---|
| 525 | "service_info.pk = ? AND " |
|---|
| 526 | "service_name.pk = service_info.fk_service_name", (protocol_id, |
|---|
| 527 | port_state_id, service_info_id)).fetchall()[0] |
|---|
| 528 | |
|---|
| 529 | return fullpdata |
|---|
| 530 | |
|---|
| 531 | |
|---|
| 532 | """ |
|---|
| 533 | End (c1) |
|---|
| 534 | """ |
|---|
| 535 | |
|---|
| 536 | def get_extraports_count_for_host_from_db(self, host_id): |
|---|
| 537 | """ |
|---|
| 538 | Get extraports Count for host id. |
|---|
| 539 | """ |
|---|
| 540 | debug("Getting extraports count for host id %d", host_id) |
|---|
| 541 | |
|---|
| 542 | epcount = self.cursor.execute("SELECT extraports.count " |
|---|
| 543 | "FROM extraports WHERE extraports.fk_host = ?", |
|---|
| 544 | (host_id, )).fetchall() |
|---|
| 545 | |
|---|
| 546 | return epcount |
|---|
| 547 | |
|---|
| 548 | |
|---|
| 549 | def get_extraports_data_for_host_from_db(self, host_id): |
|---|
| 550 | """ |
|---|
| 551 | Get extraport data for host id (returns Count and State). |
|---|
| 552 | """ |
|---|
| 553 | debug("Getting extraports data for host id %d", host_id) |
|---|
| 554 | |
|---|
| 555 | epdata = self.cursor.execute("SELECT extraports.count, " |
|---|
| 556 | "port_state.state FROM extraports, port_state " |
|---|
| 557 | "WHERE extraports.fk_host = ? AND " |
|---|
| 558 | "port_state.pk = extraports.fk_port_state", (host_id, )).fetchall() |
|---|
| 559 | |
|---|
| 560 | return epdata |
|---|
| 561 | |
|---|
| 562 | |
|---|
| 563 | def get_fingerprint_info_for_host_from_db(self, host_id): |
|---|
| 564 | """ |
|---|
| 565 | Get fingerprinto info for a host id. |
|---|
| 566 | """ |
|---|
| 567 | debug("Getting fingerprinto info for host id %d", host_id) |
|---|
| 568 | |
|---|
| 569 | # W: Not using signature field for now. |
|---|
| 570 | |
|---|
| 571 | fpinfo = self.cursor.execute("SELECT uptime, lastboot, " |
|---|
| 572 | "tcp_sequence_class, tcp_sequence_index, tcp_sequence_value, " |
|---|
| 573 | "tcp_sequence_difficulty, tcp_ts_sequence_class, " |
|---|
| 574 | "tcp_ts_sequence_value, ip_id_sequence_class, " |
|---|
| 575 | "ip_id_sequence_value FROM fingerprint_info " |
|---|
| 576 | "WHERE fk_host = ?", (host_id, )).fetchone() |
|---|
| 577 | |
|---|
| 578 | return fpinfo |
|---|
| 579 | |
|---|
| 580 | |
|---|
| 581 | def get_osmatch_for_host_from_db(self, host_id): |
|---|
| 582 | """ |
|---|
| 583 | Get osmatch data for a host id. |
|---|
| 584 | """ |
|---|
| 585 | debug("Getting osmatch for host id %d", host_id) |
|---|
| 586 | |
|---|
| 587 | match = self.cursor.execute("SELECT name, accuracy, line FROM osmatch " |
|---|
| 588 | "WHERE fk_host = ?", (host_id, )).fetchone() |
|---|
| 589 | |
|---|
| 590 | return match |
|---|
| 591 | |
|---|
| 592 | def get_osclasses_for_host_from_db(self, host_id): |
|---|
| 593 | """ |
|---|
| 594 | Get osclasses for a host id. |
|---|
| 595 | """ |
|---|
| 596 | debug("Getting osclasses for host id %d", host_id) |
|---|
| 597 | |
|---|
| 598 | classes = self.cursor.execute("SELECT osclass.accuracy, osgen.gen, " |
|---|
| 599 | "osfamily.family, osvendor.vendor, ostype.type FROM osclass " |
|---|
| 600 | "JOIN osgen ON (osclass.fk_osgen = osgen.pk) " |
|---|
| 601 | "JOIN osfamily ON (osclass.fk_osfamily = osfamily.pk) " |
|---|
| 602 | "JOIN osvendor ON (osclass.fk_osvendor = osvendor.pk) " |
|---|
| 603 | "JOIN ostype ON (osclass.fk_ostype = ostype.pk) " |
|---|
| 604 | "WHERE osclass.fk_host = ?", (host_id, )).fetchall() |
|---|
| 605 | |
|---|
| 606 | return classes |
|---|
| 607 | |
|---|
| 608 | |
|---|
| 609 | class InventoryRetrieve(CompositeRetrieve): |
|---|
| 610 | """ |
|---|
| 611 | Retrieves inventory data from database. |
|---|
| 612 | """ |
|---|
| 613 | |
|---|
| 614 | def __init__(self, conn, cursor): |
|---|
| 615 | CompositeRetrieve.__init__(self, conn, cursor) |
|---|
| 616 | |
|---|
| 617 | |
|---|
| 618 | def get_inventories_ids(self): |
|---|
| 619 | """ |
|---|
| 620 | Returns all inventories ids from database. |
|---|
| 621 | """ |
|---|
| 622 | debug("Getting all inventories ids..") |
|---|
| 623 | |
|---|
| 624 | ids = self.cursor.execute("SELECT pk FROM inventory").fetchall() |
|---|
| 625 | |
|---|
| 626 | return ids |
|---|
| 627 | |
|---|
| 628 | |
|---|
| 629 | def get_inventories_names(self): |
|---|
| 630 | """ |
|---|
| 631 | Returns all inventories names from database. |
|---|
| 632 | """ |
|---|
| 633 | debug("Getting all inventories name..") |
|---|
| 634 | |
|---|
| 635 | names = self.cursor.execute("SELECT name FROM inventory").fetchall() |
|---|
| 636 | |
|---|
| 637 | return names |
|---|
| 638 | |
|---|
| 639 | |
|---|
| 640 | def get_inventories_ids_names(self): |
|---|
| 641 | """ |
|---|
| 642 | Returns all inventories ids and names from database. |
|---|
| 643 | """ |
|---|
| 644 | debug("Getting all inventories ids and names..") |
|---|
| 645 | |
|---|
| 646 | id_names = self.cursor.execute("SELECT pk, name FROM \ |
|---|
| 647 | inventory").fetchall() |
|---|
| 648 | |
|---|
| 649 | return id_names |
|---|
| 650 | |
|---|
| 651 | |
|---|
| 652 | def get_inventory_name_for_id(self, inv_id): |
|---|
| 653 | """ |
|---|
| 654 | Returns inventory name for id. |
|---|
| 655 | """ |
|---|
| 656 | debug("Getting inventory name for id %d..", inv_id) |
|---|
| 657 | |
|---|
| 658 | name = self.cursor.execute("SELECT name FROM inventory WHERE pk = ?", |
|---|
| 659 | (inv_id, )).fetchone() |
|---|
| 660 | |
|---|
| 661 | if name: |
|---|
| 662 | return name[0] |
|---|
| 663 | |
|---|
| 664 | |
|---|
| 665 | def get_inventory_id_for_name(self, name): |
|---|
| 666 | """ |
|---|
| 667 | Returns inventory id for name. |
|---|
| 668 | """ |
|---|
| 669 | debug("Getting inventory id for name %s..", name) |
|---|
| 670 | |
|---|
| 671 | i_id = self.cursor.execute("SELECT pk FROM inventory WHERE name = ?", |
|---|
| 672 | (name, )).fetchone() |
|---|
| 673 | |
|---|
| 674 | if i_id: |
|---|
| 675 | return i_id[0] |
|---|
| 676 | |
|---|
| 677 | |
|---|
| 678 | def get_scan_args_for_inventory_id(self, fk_inventory): |
|---|
| 679 | """ |
|---|
| 680 | Return scan arguments for an inventory id. |
|---|
| 681 | """ |
|---|
| 682 | debug("Getting scan arguments for inventory id %d..", fk_inventory) |
|---|
| 683 | |
|---|
| 684 | args = self.cursor.execute("SELECT args FROM scan " |
|---|
| 685 | "JOIN _inventory_scan ON (scan.pk = _inventory_scan.fk_scan) " |
|---|
| 686 | "WHERE _inventory_scan.fk_inventory=? LIMIT 1", |
|---|
| 687 | (fk_inventory, )).fetchone() |
|---|
| 688 | |
|---|
| 689 | if args: |
|---|
| 690 | return args[0] |
|---|
| 691 | |
|---|
| 692 | |
|---|
| 693 | def get_scans_id_for_inventory(self, fk_inventory): |
|---|
| 694 | """ |
|---|
| 695 | Returns all pks from table scan, where scan is in an inventory. |
|---|
| 696 | """ |
|---|
| 697 | debug("Getting scans for inventory id %d..", fk_inventory) |
|---|
| 698 | |
|---|
| 699 | ids = self.cursor.execute("SELECT fk_scan FROM _inventory_scan " |
|---|
| 700 | "WHERE fk_inventory = ?", (fk_inventory, )).fetchall() |
|---|
| 701 | |
|---|
| 702 | return ids |
|---|
| 703 | |
|---|
| 704 | |
|---|
| 705 | def get_hosts_id_for_scan_from_db(self, fk_scan): |
|---|
| 706 | """ |
|---|
| 707 | Get all pks from table host, where host is in fk_scan and host in an |
|---|
| 708 | inventory. |
|---|
| 709 | """ |
|---|
| 710 | debug("Getting hosts from scan id %d..", fk_scan) |
|---|
| 711 | |
|---|
| 712 | ids = self.cursor.execute("SELECT pk FROM host WHERE fk_scan = ? " |
|---|
| 713 | "AND fk_scan IN (SELECT fk_scan FROM _inventory_scan)", |
|---|
| 714 | (fk_scan, )).fetchall() |
|---|
| 715 | |
|---|
| 716 | return ids |
|---|
| 717 | |
|---|
| 718 | |
|---|
| 719 | def get_hosts_base_data_for_inventory_from_db(self, host_address, |
|---|
| 720 | fk_inventory): |
|---|
| 721 | """ |
|---|
| 722 | Get all pks and fk_scans from table host, where: host has an especified |
|---|
| 723 | host_address and host is in an especified fk_inventory. |
|---|
| 724 | """ |
|---|
| 725 | debug("Getting pks in host with host_address %s and " |
|---|
| 726 | "fk_inventory %d", host_address, fk_inventory) |
|---|
| 727 | |
|---|
| 728 | ids = self.cursor.execute("SELECT host.fk_scan, host.pk " |
|---|
| 729 | "FROM host " |
|---|
| 730 | "JOIN _host_address ON (host.pk = _host_address.fk_host) " |
|---|
| 731 | "WHERE _host_address.fk_address = (SELECT " |
|---|
| 732 | "address.pk FROM address WHERE address = ?) " |
|---|
| 733 | "AND host.fk_scan IN (SELECT fk_scan FROM " |
|---|
| 734 | "_inventory_scan WHERE fk_inventory = ?) ORDER BY host.pk DESC", |
|---|
| 735 | (host_address, fk_inventory)).fetchall() |
|---|
| 736 | |
|---|
| 737 | return ids |
|---|
| 738 | |
|---|
| 739 | |
|---|
| 740 | def get_finish_data_for_inventory_from_db(self, fk_inventory): |
|---|
| 741 | """ |
|---|
| 742 | Get all finish timestamps and scan id from scans that are in an |
|---|
| 743 | inventory. |
|---|
| 744 | """ |
|---|
| 745 | debug("Getting finish timestamps for fk_inventory %d", fk_inventory) |
|---|
| 746 | |
|---|
| 747 | data = self.cursor.execute("SELECT scan.pk, " |
|---|
| 748 | "scan.finish as 'finish [timestamp]' FROM scan " |
|---|
| 749 | "JOIN _inventory_scan ON (_inventory_scan.fk_scan = scan.pk) " |
|---|
| 750 | "WHERE _inventory_scan.fk_inventory = ? ORDER BY scan.pk DESC", |
|---|
| 751 | (fk_inventory, )).fetchall() |
|---|
| 752 | |
|---|
| 753 | return data |
|---|
| 754 | |
|---|
| 755 | |
|---|
| 756 | def get_inventory_change_category_id(self, name): |
|---|
| 757 | """ |
|---|
| 758 | Get inventory_change_category pk based on category name. |
|---|
| 759 | """ |
|---|
| 760 | debug("Getting change_category id for name %r", name) |
|---|
| 761 | |
|---|
| 762 | pk = self.cursor.execute("SELECT pk FROM inventory_change_category " |
|---|
| 763 | "WHERE name=?", (name, )).fetchone() |
|---|
| 764 | |
|---|
| 765 | if pk: |
|---|
| 766 | return pk[0] |
|---|
| 767 | |
|---|
| 768 | |
|---|
| 769 | def get_inventory_comparison(self, old_hid, new_hid, date, fk_inventory): |
|---|
| 770 | """ |
|---|
| 771 | Returns entry in _inventory_changes if there is a comparison stored |
|---|
| 772 | for old_hid against new_hid in a date for fk_inventory. |
|---|
| 773 | """ |
|---|
| 774 | debug("Checking if there is a comparison for old hostid %d against " |
|---|
| 775 | "new hostid %d @ %s for Inventory id %d", |
|---|
| 776 | old_hid, new_hid, date, fk_inventory) |
|---|
| 777 | |
|---|
| 778 | pk = self.cursor.execute("SELECT pk FROM _inventory_changes " |
|---|
| 779 | "WHERE old_hostid=? AND new_hostid=? AND entry_date=? AND " |
|---|
| 780 | "fk_inventory=?", (old_hid, new_hid, date, fk_inventory)).fetchall() |
|---|
| 781 | |
|---|
| 782 | if pk: |
|---|
| 783 | return True |
|---|
| 784 | |
|---|
| 785 | def get_inventory_changes(self, fk_inventory, fk_address): |
|---|
| 786 | """ |
|---|
| 787 | Returns changes in _inventory_changes for fk_inventory and |
|---|
| 788 | fk_address |
|---|
| 789 | """ |
|---|
| 790 | debug("Getting changes for Inventory id %d and Address id %d", |
|---|
| 791 | fk_inventory, fk_address) |
|---|
| 792 | |
|---|
| 793 | |
|---|
| 794 | changes = self.cursor.execute("SELECT old_hostid, new_hostid, " |
|---|
| 795 | "entry_date, short_description, inventory_change_category.name " |
|---|
| 796 | "FROM _inventory_changes JOIN inventory_change_category ON " |
|---|
| 797 | "(_inventory_changes.fk_category=inventory_change_category.pk) " |
|---|
| 798 | "WHERE fk_inventory=? AND fk_address=? ORDER BY entry_date DESC", |
|---|
| 799 | (fk_inventory, fk_address)).fetchall() |
|---|
| 800 | |
|---|
| 801 | return changes |
|---|
| 802 | |
|---|
| 803 | |
|---|
| 804 | def get_inventory_changes_for_category(self, fk_inventory, fk_address, |
|---|
| 805 | fk_category): |
|---|
| 806 | """ |
|---|
| 807 | Returns changes in _inventory_changes for fk_inventory, fk_address and |
|---|
| 808 | a especific fk_category. |
|---|
| 809 | """ |
|---|
| 810 | debug("Getting changes for Inventory id %d, Address id %d and " |
|---|
| 811 | "Category id %d", fk_inventory, fk_address, fk_category) |
|---|
| 812 | |
|---|
| 813 | changes = self.cursor.execute("SELECT old_hostid, new_hostid, " |
|---|
| 814 | "entry_date, short_description FROM _inventory_changes " |
|---|
| 815 | "WHERE fk_category=? and fk_inventory=? AND fk_address=? " |
|---|
| 816 | "ORDER BY entry_date DESC", (fk_category, fk_inventory, |
|---|
| 817 | fk_address)).fetchall() |
|---|
| 818 | |
|---|
| 819 | return changes |
|---|
| 820 | |
|---|
| 821 | |
|---|
| 822 | def get_inventory_changes_for_category_in_range(self, fk_inventory, |
|---|
| 823 | fk_address, fk_category, start, end): |
|---|
| 824 | """ |
|---|
| 825 | Returns changes in _inventory_changes for fk_inventory, fk_address and |
|---|
| 826 | a especific fk_category in a time range. |
|---|
| 827 | """ |
|---|
| 828 | debug("Getting changes for Inventory id %d, Address id %d and " |
|---|
| 829 | "Category id %d from %s to %s", fk_inventory, fk_address, |
|---|
| 830 | fk_category, start, end) |
|---|
| 831 | |
|---|
| 832 | changes = self.cursor.execute("SELECT old_hostid, new_hostid, " |
|---|
| 833 | "entry_date, short_description FROM _inventory_changes " |
|---|
| 834 | "WHERE entry_date >= ? AND entry_date < ? AND fk_category=? AND " |
|---|
| 835 | "fk_inventory=? AND fk_address=? ORDER BY entry_date DESC", |
|---|
| 836 | (start, end, fk_category, fk_inventory, fk_address)).fetchall() |
|---|
| 837 | |
|---|
| 838 | return changes |
|---|
| 839 | |
|---|
| 840 | |
|---|
| 841 | class ConnectInventoryDB(ConnectDB, InventoryRetrieve): |
|---|
| 842 | """ |
|---|
| 843 | This replaces the previous UpdateChanges class in InventoryChanges module. |
|---|
| 844 | ToDo: Write a better doc =) |
|---|
| 845 | """ |
|---|
| 846 | |
|---|
| 847 | def __init__(self, db): |
|---|
| 848 | ConnectDB.__init__(self, db) |
|---|
| 849 | InventoryRetrieve.__init__(self, self.conn, self.cursor) |
|---|