root/trunk/umitCore/UmitConf.py @ 3953

Revision 3953, 20.2 kB (checked in by gpolo, 4 years ago)

Merged NetworkInventory?

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 re
24import os
25
26from types import StringTypes
27from ConfigParser import NoSectionError, NoOptionError
28
29from umitCore.Paths import Path
30from umitCore.ScanProfileConf import scan_profile_file
31from umitCore.UmitLogging import log
32from umitCore.UmitConfigParser import UmitConfigParser
33from umitCore.I18N import _
34
35class UmitConf(object):
36    def __init__(self):
37        self.parser = Path.config_parser
38
39    def save_changes(self):
40        self.parser.save_changes()
41
42    def get_colored_diff(self):
43        try:
44            cd = self.parser.get('diff', 'colored_diff')
45            if cd == "False" or \
46                cd == "false" or \
47                cd == "0" or \
48                cd == "" or \
49                cd == False:
50                return False
51            return True
52        except:
53            return True
54
55    def set_colored_diff(self, enable):
56        if not self.parser.has_section('diff'):
57            self.parser.add_section('diff')
58
59        self.parser.set('diff', 'colored_diff', str(enable))
60
61    def get_diff_mode(self):
62        try: return self.parser.get('diff', 'diff_mode')
63        except: return "compare"
64
65    def set_diff_mode(self, diff_mode):
66        if not self.parser.has_section('diff'):
67            self.parser.add_section('diff')
68       
69        self.parser.set('diff', 'diff_mode', diff_mode)
70
71    colored_diff = property(get_colored_diff, set_colored_diff)
72    diff_mode = property(get_diff_mode, set_diff_mode)
73
74
75class SearchConfig(UmitConfigParser, object):
76    def __init__(self):
77        self.parser = Path.config_parser
78
79        self.section_name = "search"
80        if not self.parser.has_section(self.section_name):
81            self.create_section()
82
83    def save_changes(self):
84        self.parser.save_changes()
85
86    def create_section(self):
87        self.parser.add_section(self.section_name)
88        self.directory = ""
89        self.file_extension = "usr"
90        self.save_time = "60;Days"
91        self.store_results = True
92        self.search_db = True
93
94    def _get_it(self, p_name, default):
95        return self.parser.get(self.section_name, p_name, default)
96
97    def _set_it(self, p_name, value):
98        self.parser.set(self.section_name, p_name, value)
99       
100    def boolean_sanity(self, attr):
101        if attr == True or \
102           attr == "True" or \
103           attr == "true" or \
104           attr == "1":
105
106            return 1
107
108        return 0
109
110    def get_directory(self):
111        return self._get_it("directory", "")
112
113    def set_directory(self, directory):
114        self._set_it("directory", directory)
115
116    def get_file_extension(self):
117        return self._get_it("file_extension", "usr").split(";")
118
119    def set_file_extension(self, file_extension):
120        if type(file_extension) == type([]):
121            self._set_it("file_extension", ";".join(file_extension))
122        elif type(file_extension) in StringTypes:
123            self._set_it("file_extension", file_extension)
124
125    def get_save_time(self):
126        return self._get_it("save_time", "60;Days").split(";")
127
128    def set_save_time(self, save_time):
129        if type(save_time) == type([]):
130            self._set_it("save_time", ";".join(save_time))
131        elif type(save_time) in StringTypes:
132            self._set_it("save_time", save_time)
133
134    def get_store_results(self):
135        return self.boolean_sanity(self._get_it("store_results", True))
136
137    def set_store_results(self, store_results):
138        self._set_it("store_results", self.boolean_sanity(store_results))
139
140    def get_search_db(self):
141        return self.boolean_sanity(self._get_it("search_db", True))
142
143    def set_search_db(self, search_db):
144        self._set_it("search_db", self.boolean_sanity(search_db))
145
146    def get_converted_save_time(self):
147        try:
148            return int(self.save_time[0]) * self.time_list[self.save_time[1]]
149        except:
150            # If something goes wrong, return a save time of 60 days
151            return 60 * 60 * 24 * 60
152
153    def get_time_list(self):
154        # Time as key, seconds a value
155        return {_("Hours"): 60 * 60,
156                _("Days"): 60 * 60 * 24,
157                _("Weeks"): 60 * 60 * 24 * 7,
158                _("Months"): 60 * 60 * 24 * 7 * 30,
159                _("Years"): 60 * 60 * 24 * 7 * 30 * 12,
160                _("Minutes"): 60,
161                _("Seconds"): 1}
162   
163    directory = property(get_directory, set_directory)
164    file_extension = property(get_file_extension, set_file_extension)
165    save_time = property(get_save_time, set_save_time)
166    store_results = property(get_store_results, set_store_results)
167    search_db = property(get_search_db, set_search_db)
168    converted_save_time = property(get_converted_save_time)
169    time_list = property(get_time_list)
170
171
172class Profile(UmitConfigParser, object):
173    def __init__(self, user_profile=None, *args):
174        UmitConfigParser.__init__(self, *args)
175
176        if not user_profile:
177            user_profile = scan_profile_file
178
179        fconf = open(user_profile, 'r')
180        self.readfp(fconf, user_profile)
181
182        fconf.close()
183        del(fconf)
184
185        self.attributes = {}
186
187    def _get_it(self, profile, attribute):
188        if self._verify_profile(profile):
189            return self.get(profile, attribute)
190        return ""
191
192    def _set_it(self, profile, attribute, value=''):
193        if self._verify_profile(profile):
194            return self.set(profile, attribute, value)
195
196    def add_profile(self, profile_name, **attributes):
197        log.debug(">>> Add Profile '%s': %s" % (profile_name, attributes))
198        try: self.add_section(profile_name)
199        except: return None
200       
201        [self._set_it(profile_name, attr, attributes[attr]) \
202         for attr in attributes if attr != "options"]
203        options = attributes["options"]
204        if type(options) in StringTypes:
205            self._set_it(profile_name, "options", options)
206        elif type(options) == type({}):
207            self._set_it(profile_name, "options", ",".join(options.keys()))
208
209        for opt in options:
210            if options[opt]:
211                self._set_it(profile_name, opt, options[opt])
212        self.save_changes()
213
214    def remove_profile(self, profile_name):
215        try: self.remove_section(profile_name)
216        except: pass
217        self.save_changes()
218
219    def _verify_profile(self, profile_name):
220        if profile_name not in self.sections():
221            return False
222        return True
223
224class CommandProfile (Profile, object):
225    def __init__(self, user_profile=''):
226        if not user_profile:
227            user_profile = scan_profile_file
228       
229        Profile.__init__(self, user_profile)
230       
231    def get_command(self, profile):
232        return self._get_it(profile, 'command')
233
234    def get_hint(self, profile):
235        return self._get_it(profile, 'hint')
236
237    def get_description(self, profile):
238        return self._get_it(profile, 'description')
239   
240    def get_annotation(self, profile):
241        return self._get_it(profile, 'annotation')
242
243    def get_options(self, profile):
244        dic = {}
245        options_result = self._get_it(profile, 'options')
246        if options_result.strip()=='':
247            return dic
248       
249        for opt in options_result.split(','):
250            try:
251                dic[unicode(opt.strip())] = self._get_it(profile, opt)
252            except NoOptionError:
253                dic[unicode(opt.strip())] = None
254        return dic
255
256    def set_command(self, profile, command=''):
257        self._set_it(profile, 'command', command)
258
259    def set_hint(self, profile, hint=''):
260        self._set_it(profile, 'hint', hint)
261   
262    def set_description(self, profile, description=''):
263        self._set_it(profile, 'description', description)
264   
265    def set_annotation (self, profile, annotation=''):
266        self._set_it(profile, 'annotation', annotation)
267   
268    def set_options(self, profile, options={}):
269        for opt in options:
270            if options[opt]:
271                self._set_it(profile, opt, options[opt])
272        self._set_it(profile, 'options', ",".join(options.keys()))
273
274    def get_profile(self, profile_name):
275        return {'profile':profile_name, \
276                'command':self.get_command(profile_name), \
277                'hint':self.get_hint(profile_name), \
278                'description':self.get_description(profile_name), \
279                'annotation':self.get_annotation(profile_name),\
280                'options':self.get_options(profile_name)}
281
282
283class NmapOutputHighlight(object):
284    setts = ["bold", "italic", "underline", "text", "highlight", "regex"]
285   
286    def __init__(self):
287        self.parser = Path.config_parser
288
289    def save_changes(self):
290        self.parser.save_changes()
291
292    def __get_it(self, p_name):
293        property_name = "%s_highlight" % p_name
294
295        try:
296            return self.sanity_settings([self.parser.get(property_name,
297                                                         prop,
298                                                         True) \
299                                         for prop in self.setts])
300        except:
301            settings = []
302            prop_settings = self.default_highlights[p_name]
303            settings.append(prop_settings["bold"])
304            settings.append(prop_settings["italic"])
305            settings.append(prop_settings["underline"])
306            settings.append(prop_settings["text"])
307            settings.append(prop_settings["highlight"])
308            settings.append(prop_settings["regex"])
309
310            self.__set_it(p_name, settings)
311
312            return self.sanity_settings(settings)
313
314    def __set_it(self, property_name, settings):
315        property_name = "%s_highlight" % property_name
316        settings = self.sanity_settings(list(settings))
317
318        [self.parser.set(property_name, self.setts[pos], settings[pos]) \
319         for pos in xrange(len(settings))]
320
321    def sanity_settings(self, settings):
322        """This method tries to convert insane settings to sanity ones ;-)
323        If user send a True, "True" or "true" value, for example, it tries to
324        convert then to the integer 1.
325        Same to False, "False", etc.
326
327        Sequence: [bold, italic, underline, text, highlight, regex]
328        """
329        #log.debug(">>> Sanitize %s" % str(settings))
330       
331        settings[0] = self.boolean_sanity(settings[0])
332        settings[1] = self.boolean_sanity(settings[1])
333        settings[2] = self.boolean_sanity(settings[2])
334
335        tuple_regex = "[\(\[]\s?(\d+)\s?,\s?(\d+)\s?,\s?(\d+)\s?[\)\]]"
336        if type(settings[3]) == type(""):
337            settings[3] = [int(t) \
338                           for t in re.findall(tuple_regex, settings[3])[0]]
339
340        if type(settings[4]) == type(""):
341            settings[4]= [int(h) \
342                          for h in re.findall(tuple_regex, settings[4])[0]]
343
344        return settings
345
346    def boolean_sanity(self, attr):
347        if attr == True or attr == "True" or attr == "true" or attr == "1":
348            return 1
349        return 0
350
351    def get_date(self):
352        return self.__get_it("date")
353
354    def set_date(self, settings):
355        self.__set_it("date", settings)
356
357    def get_hostname(self):
358        return self.__get_it("hostname")
359
360    def set_hostname(self, settings):
361        self.__set_it("hostname", settings)
362
363    def get_ip(self):
364        return self.__get_it("ip")
365
366    def set_ip(self, settings):
367        self.__set_it("ip", settings)
368
369    def get_port_list(self):
370        return self.__get_it("port_list")
371
372    def set_port_list(self, settings):
373        self.__set_it("port_list", settings)
374
375    def get_open_port(self):
376        return self.__get_it("open_port")
377
378    def set_open_port(self, settings):
379        self.__set_it("open_port", settings)
380
381    def get_closed_port(self):
382        return self.__get_it("closed_port")
383
384    def set_closed_port(self, settings):
385        self.__set_it("closed_port", settings)
386
387    def get_filtered_port(self):
388        return self.__get_it("filtered_port")
389
390    def set_filtered_port(self, settings):
391        self.__set_it("filtered_port", settings)
392
393    def get_details(self):
394        return self.__get_it("details")
395
396    def set_details(self, settings):
397        self.__set_it("details", settings)
398
399    def get_enable(self):
400        enable = True
401        try:
402            enable = self.parser.get("output_highlight", "enable_highlight")
403        except NoSectionError:
404            self.parser.set("output_highlight", "enable_highlight", str(True))
405       
406        if enable == "False" or enable == "0" or enable == "":
407            return False
408        return True
409
410    def set_enable(self, enable):
411        if enable == False or enable == "0" or enable is None or enable == "":
412            self.parser.set("output_highlight", "enable_highlight", str(False))
413        else:
414            self.parser.set("output_highlight", "enable_highlight", str(True))
415
416    date = property(get_date, set_date)
417    hostname = property(get_hostname, set_hostname)
418    ip = property(get_ip, set_ip)
419    port_list = property(get_port_list, set_port_list)
420    open_port = property(get_open_port, set_open_port)
421    closed_port = property(get_closed_port, set_closed_port)
422    filtered_port = property(get_filtered_port, set_filtered_port)
423    details = property(get_details, set_details)
424    enable = property(get_enable, set_enable)
425
426    # These settings are made when there is nothing set yet. They set
427    # the "factory" default to highlight colors
428    default_highlights = {"date":{"bold":str(True),
429                            "italic":str(False),
430                            "underline":str(False),
431                            "text":[0, 0, 0],
432                            "highlight":[65535, 65535, 65535],
433                            "regex":"\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}\s.{1,4}"},
434                          "hostname":{"bold":str(True),
435                            "italic":str(True),
436                            "underline":str(True),
437                            "text":[0, 111, 65535],
438                            "highlight":[65535, 65535, 65535],
439                "regex":"(\w{2,}://)*\w{2,}\.\w{2,}(\.\w{2,})*(/[\w{2,}]*)*"},
440                          "ip":{"bold":str(True),
441                            "italic":str(False),
442                            "underline":str(False),
443                            "text":[0, 0, 0],
444                            "highlight":[65535, 65535, 65535],
445                            "regex":"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}"},
446                          "port_list":{"bold":str(True),
447                            "italic":str(False),
448                            "underline":str(False),
449                            "text":[0, 1272, 28362],
450                            "highlight":[65535, 65535, 65535],
451                        "regex":"PORT\s+STATE\s+SERVICE(\s+VERSION)?[^\n]*"},
452                          "open_port":{"bold":str(True),
453                            "italic":str(False),
454                            "underline":str(False),
455                            "text":[0, 41036, 2396],
456                            "highlight":[65535, 65535, 65535],
457                            "regex":"\d{1,5}/.{1,5}\s+open\s+.*"},
458                          "closed_port":{"bold":str(False),
459                            "italic":str(False),
460                            "underline":str(False),
461                            "text":[65535, 0, 0],
462                            "highlight":[65535, 65535, 65535],
463                            "regex":"\d{1,5}/.{1,5}\s+closed\s+.*"},
464                          "filtered_port":{"bold":str(False),
465                            "italic":str(False),
466                            "underline":str(False),
467                            "text":[38502, 39119, 0],
468                            "highlight":[65535, 65535, 65535],
469                            "regex":"\d{1,5}/.{1,5}\s+filtered\s+.*"},
470                          "details":{"bold":str(True),
471                            "italic":str(False),
472                            "underline":str(True),
473                            "text":[0, 0, 0],
474                            "highlight":[65535, 65535, 65535],
475                            "regex":"^(\w{2,}[\s]{,3}){,4}:"}}
476
477class DiffColors(object):
478    def __init__(self):
479        self.parser = Path.config_parser
480        self.section_name = "diff_colors"
481
482    def save_changes(self):
483        self.parser.save_changes()
484
485    def __get_it(self, p_name):
486        return self.sanity_settings(self.parser.get(self.section_name, p_name))
487
488    def __set_it(self, property_name, settings):
489        settings = self.sanity_settings(settings)
490        self.parser.set(self.section_name, property_name, settings)
491
492    def sanity_settings(self, settings):
493        log.debug(">>> Sanitize %s" % str(settings))
494       
495        tuple_regex = "[\(\[]\s?(\d+)\s?,\s?(\d+)\s?,\s?(\d+)\s?[\)\]]"
496        if type(settings) == type(""):
497            settings = [int(t) for t in re.findall(tuple_regex, settings)[0]]
498
499        return settings
500
501    def get_unchanged(self):
502        return self.__get_it("unchanged")
503
504    def set_unchanged(self, settings):
505        self.__set_it("unchanged", settings)
506
507    def get_added(self):
508        return self.__get_it("added")
509
510    def set_added(self, settings):
511        self.__set_it("added", settings)
512
513    def get_modified(self):
514        return self.__get_it("modified")
515
516    def set_modified(self, settings):
517        self.__set_it("modified", settings)
518
519    def get_not_present(self):
520        return self.__get_it("not_present")
521
522    def set_not_present(self, settings):
523        self.__set_it("not_present", settings)
524
525    unchanged = property(get_unchanged, set_unchanged)
526    added = property(get_added, set_added)
527    modified = property(get_modified, set_modified)
528    not_present = property(get_not_present, set_not_present)
529
530class Plugins(object):
531    def __init__(self):
532        self.parser = Path.config_parser
533        self.section_name = "plugins"
534        self.separator = os.pathsep
535
536        if not self.parser.has_section(self.section_name):
537            self.create_section()
538
539    def save_changes(self):
540        self.parser.save_changes()
541
542    def create_section(self):
543        from os.path import join
544        self.paths = [join(Path.config_dir, "plugins")]
545        self.plugins = ""
546
547    def __get_it(self, p_name):
548        value = None
549
550        try:
551            try:
552                value = self.parser.get(self.section_name, p_name)
553            except:
554                pass
555        finally:
556            return self.sanity_settings(value)
557
558    def __set_it(self, property_name, settings):
559        settings = self.sanity_settings(settings)
560        self.parser.set(self.section_name, property_name, settings)
561
562    def sanity_settings(self, settings):
563        # FIXME: more sensed :D
564        if not settings:
565            return ""
566        return settings
567
568    def get_paths(self):
569        return filter(None, self.__get_it("paths").split(self.separator))
570
571    def set_paths(self, settings):
572        self.__set_it("paths", self.separator.join(settings))
573
574    def get_plugins(self):
575        return filter(None, self.__get_it("plugins").split(self.separator))
576
577    def set_plugins(self, settings):
578        self.__set_it("plugins", self.separator.join(settings))
579
580    paths = property(get_paths, set_paths)
581    plugins = property(get_plugins, set_plugins)
582
583# Exceptions
584class ProfileNotFound:
585    def __init__ (self, profile):
586        self.profile = profile
587    def __str__ (self):
588        return "No profile named '"+self.profile+"' found!"
589
590class ProfileCouldNotBeSaved:
591    def __init__ (self, profile):
592        self.profile = profile
593    def __str__ (self):
594        return "Profile named '"+self.profile+"' could not be saved!"
595
596
597if __name__ == "__main__":
598    pass
Note: See TracBrowser for help on using the browser.