| 1 | #!/usr/bin/env python |
|---|
| 2 | # -*- coding: utf-8 -*- |
|---|
| 3 | |
|---|
| 4 | # Copyright (C) 2005 Insecure.Com LLC. |
|---|
| 5 | # |
|---|
| 6 | # Author: Adriano Monteiro Marques <py.adriano@gmail.com> |
|---|
| 7 | # |
|---|
| 8 | # This program is free software; you can redistribute it and/or modify |
|---|
| 9 | # it under the terms of the GNU General Public License as published by |
|---|
| 10 | # the Free Software Foundation; either version 2 of the License, or |
|---|
| 11 | # (at your option) any later version. |
|---|
| 12 | # |
|---|
| 13 | # This program is distributed in the hope that it will be useful, |
|---|
| 14 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 15 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 16 | # GNU General Public License for more details. |
|---|
| 17 | # |
|---|
| 18 | # You should have received a copy of the GNU General Public License |
|---|
| 19 | # along with this program; if not, write to the Free Software |
|---|
| 20 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|---|
| 21 | |
|---|
| 22 | import os |
|---|
| 23 | import os.path |
|---|
| 24 | import re |
|---|
| 25 | |
|---|
| 26 | from umitCore.Logging import log |
|---|
| 27 | from umitCore.UmitConfigParser import UmitConfigParser |
|---|
| 28 | from umitCore.BasePaths import base_paths, HOME |
|---|
| 29 | from umitCore.I18N import _ |
|---|
| 30 | |
|---|
| 31 | |
|---|
| 32 | ####### |
|---|
| 33 | # Paths |
|---|
| 34 | class Paths(object): |
|---|
| 35 | """Paths |
|---|
| 36 | """ |
|---|
| 37 | config_parser = UmitConfigParser() |
|---|
| 38 | paths_section = "paths" |
|---|
| 39 | |
|---|
| 40 | directories_list = ["locale_dir", |
|---|
| 41 | "pixmaps_dir", |
|---|
| 42 | "config_dir", |
|---|
| 43 | "misc_dir", |
|---|
| 44 | "docs_dir"] |
|---|
| 45 | |
|---|
| 46 | config_files_list = ["config_file", |
|---|
| 47 | "target_list", |
|---|
| 48 | "profile_editor", |
|---|
| 49 | "wizard", |
|---|
| 50 | "scan_profile", |
|---|
| 51 | "recent_scans", |
|---|
| 52 | "options", |
|---|
| 53 | "umitdb", |
|---|
| 54 | "umit_version"] |
|---|
| 55 | |
|---|
| 56 | share_files_list = ["umit_op", |
|---|
| 57 | "umit_opi", |
|---|
| 58 | "umit_opt", |
|---|
| 59 | "umit_opf"] |
|---|
| 60 | |
|---|
| 61 | misc_files_list = ["services_dump", |
|---|
| 62 | "os_dump", |
|---|
| 63 | "os_classification"] |
|---|
| 64 | |
|---|
| 65 | other_settings = ["nmap_command_path"] |
|---|
| 66 | |
|---|
| 67 | config_file_set = False |
|---|
| 68 | |
|---|
| 69 | def set_umit_conf(self, umit_conf): |
|---|
| 70 | # Place supposed to have the user's config file |
|---|
| 71 | supposed_user_conf = os.path.join(base_paths['user_dir'], |
|---|
| 72 | base_paths['config_file']) |
|---|
| 73 | config_file = supposed_user_conf |
|---|
| 74 | parsed = False |
|---|
| 75 | |
|---|
| 76 | if os.path.exists(supposed_user_conf)\ |
|---|
| 77 | and check_access(supposed_user_conf, os.R_OK): |
|---|
| 78 | try: |
|---|
| 79 | self.config_parser.read(config_file) |
|---|
| 80 | log.debug(">>> Using config files in user home \ |
|---|
| 81 | directory: %s" % config_file) |
|---|
| 82 | parsed = True |
|---|
| 83 | except: |
|---|
| 84 | log.debug(">>> Failed to load config file from \ |
|---|
| 85 | user home directory") |
|---|
| 86 | |
|---|
| 87 | if not parsed and not os.path.exists(supposed_user_conf)\ |
|---|
| 88 | and not check_access(base_paths['user_dir'], os.R_OK and os.W_OK): |
|---|
| 89 | try: |
|---|
| 90 | result = create_user_dir(umit_conf, HOME) |
|---|
| 91 | config_file = result['config_file'] |
|---|
| 92 | self.config_parser.read(config_file) |
|---|
| 93 | [self.__setattr__(opt, result[opt]) for opt in result] |
|---|
| 94 | log.debug(">>> Using recently created config files in \ |
|---|
| 95 | user home: %s" % config_file) |
|---|
| 96 | parsed = True |
|---|
| 97 | except: |
|---|
| 98 | log.debug(">>> Failed to create user home") |
|---|
| 99 | |
|---|
| 100 | if not parsed and type(umit_conf) == type([]): |
|---|
| 101 | config_file = self.config_parser.read(umit_conf) |
|---|
| 102 | if config_file != None and len(config_file) >= 1: |
|---|
| 103 | config_file = config_file[0] |
|---|
| 104 | else: |
|---|
| 105 | raise Exception("Couldn't load umit config file!") |
|---|
| 106 | |
|---|
| 107 | # Should make the following only after reading the umit.conf file |
|---|
| 108 | self.config_file = config_file |
|---|
| 109 | self.config_file_set = True |
|---|
| 110 | |
|---|
| 111 | log.debug(">>> Config file: %s" % config_file) |
|---|
| 112 | |
|---|
| 113 | def root_dir(self): |
|---|
| 114 | """Retrieves root dir on current filesystem""" |
|---|
| 115 | curr_dir = os.getcwd() |
|---|
| 116 | while True: |
|---|
| 117 | splited = os.path.split(curr_dir)[0] |
|---|
| 118 | if curr_dir == splited: |
|---|
| 119 | break |
|---|
| 120 | curr_dir = splited |
|---|
| 121 | |
|---|
| 122 | log.debug(">>> Root dir: %s" % curr_dir) |
|---|
| 123 | return curr_dir |
|---|
| 124 | |
|---|
| 125 | |
|---|
| 126 | def __getattr__(self, name): |
|---|
| 127 | if self.config_file_set: |
|---|
| 128 | if name in self.directories_list or name in self.other_settings: |
|---|
| 129 | return return_if_exists(self.config_parser.get(self.paths_section, name)) |
|---|
| 130 | elif name in self.config_files_list: |
|---|
| 131 | return return_if_exists(os.path.join(self.config_parser.get(self.paths_section, "config_dir"), |
|---|
| 132 | base_paths[name])) |
|---|
| 133 | elif name in self.share_files_list: |
|---|
| 134 | return return_if_exists(os.path.join(self.config_parser.get(self.paths_section, "pixmaps_dir"), |
|---|
| 135 | base_paths[name])) |
|---|
| 136 | elif name in self.misc_files_list: |
|---|
| 137 | return return_if_exists(os.path.join(self.config_parser.get(self.paths_section, "misc_dir"), |
|---|
| 138 | base_paths[name])) |
|---|
| 139 | |
|---|
| 140 | try: |
|---|
| 141 | return self.__dict__[name] |
|---|
| 142 | except: |
|---|
| 143 | raise NameError(name) |
|---|
| 144 | else: |
|---|
| 145 | raise Exception("Must set config file location first") |
|---|
| 146 | |
|---|
| 147 | def __setattr__(self, name, value): |
|---|
| 148 | if name in self.directories_list or name in self.other_settings: |
|---|
| 149 | self.config_parser.set(self.paths_section, name, value) |
|---|
| 150 | else: |
|---|
| 151 | self.__dict__[name] = value |
|---|
| 152 | |
|---|
| 153 | |
|---|
| 154 | #################################### |
|---|
| 155 | # Functions for directories creation |
|---|
| 156 | |
|---|
| 157 | def create_user_dir(config_list, user_home): |
|---|
| 158 | main_config = None |
|---|
| 159 | for config in config_list: |
|---|
| 160 | if os.path.exists(config): |
|---|
| 161 | main_config = config |
|---|
| 162 | break |
|---|
| 163 | else: |
|---|
| 164 | log.critical(">>> No useful configuration file found in this list: %s"\ |
|---|
| 165 | % config_list) |
|---|
| 166 | raise Exception(">>> No usefull configuration file found!") |
|---|
| 167 | |
|---|
| 168 | log.debug(">>> Create user dir at given home: %s" % user_home) |
|---|
| 169 | log.debug(">>> Using %s as source" % main_config) |
|---|
| 170 | main_umit_conf = UmitConfigParser() |
|---|
| 171 | main_umit_conf.read(main_config) |
|---|
| 172 | paths_section = "paths" |
|---|
| 173 | |
|---|
| 174 | user_dir = os.path.join(user_home, base_paths['config_dir']) |
|---|
| 175 | |
|---|
| 176 | if os.path.exists(user_home)\ |
|---|
| 177 | and os.access(user_home, os.R_OK and os.W_OK)\ |
|---|
| 178 | and not os.path.exists(user_dir): |
|---|
| 179 | os.mkdir(user_dir) |
|---|
| 180 | log.debug(">>> Umit user dir successfully created! %s" % user_dir) |
|---|
| 181 | else: |
|---|
| 182 | log.warning(">>> No permissions to create user dir!") |
|---|
| 183 | return False |
|---|
| 184 | |
|---|
| 185 | main_dir = os.path.split(main_config)[0] |
|---|
| 186 | copy_config_file("options.xml", main_dir, user_dir) |
|---|
| 187 | copy_config_file("profile_editor.xml", main_dir, user_dir) |
|---|
| 188 | copy_config_file("recent_scans.txt", main_dir, user_dir) |
|---|
| 189 | copy_config_file("scan_profile.usp", main_dir, user_dir) |
|---|
| 190 | copy_config_file("target_list.txt", main_dir, user_dir) |
|---|
| 191 | copy_config_file("umit_version", main_dir, user_dir) |
|---|
| 192 | copy_config_file("umit.db", main_dir, user_dir) |
|---|
| 193 | copy_config_file("wizard.xml", main_dir, user_dir) |
|---|
| 194 | |
|---|
| 195 | return dict(user_dir = user_dir, |
|---|
| 196 | config_dir = user_dir, |
|---|
| 197 | config_file = copy_config_file("umit.conf", os.path.split(main_config)[0], user_dir)) |
|---|
| 198 | |
|---|
| 199 | def copy_config_file(filename, dir_origin, dir_destiny): |
|---|
| 200 | log.debug(">>> copy_config_file %s to %s" % (filename, dir_destiny)) |
|---|
| 201 | |
|---|
| 202 | origin = os.path.join(dir_origin, filename) |
|---|
| 203 | destiny = os.path.join(dir_destiny, filename) |
|---|
| 204 | |
|---|
| 205 | if not os.path.exists(destiny): |
|---|
| 206 | # Quick copy |
|---|
| 207 | open(destiny, 'w').write(open(origin).read()) |
|---|
| 208 | return destiny |
|---|
| 209 | |
|---|
| 210 | def check_access(path, permission): |
|---|
| 211 | return os.path.exists(path) and os.access(path, permission) |
|---|
| 212 | |
|---|
| 213 | def return_if_exists(path): |
|---|
| 214 | if os.path.exists(path): |
|---|
| 215 | return os.path.abspath(path) |
|---|
| 216 | raise Exception("File '%s' does not exist or could not be found!" % path) |
|---|
| 217 | |
|---|
| 218 | ######### |
|---|
| 219 | # Singleton! |
|---|
| 220 | Path = Paths() |
|---|
| 221 | |
|---|
| 222 | if __name__ == '__main__': |
|---|
| 223 | #create_user_dir(os.path.expanduser("~")) |
|---|
| 224 | __file__ = ".." |
|---|
| 225 | Path.set_umit_conf(os.path.join(os.path.split(__file__)[0], "config", "umit.conf")) |
|---|
| 226 | |
|---|
| 227 | print ">>> SAVED DIRECTORIES:" |
|---|
| 228 | print ">>> LOCALE DIR:", Path.locale_dir |
|---|
| 229 | print ">>> PIXMAPS DIR:", Path.pixmaps_dir |
|---|
| 230 | print ">>> CONFIG DIR:", Path.config_dir |
|---|
| 231 | print |
|---|
| 232 | print ">>> FILES:" |
|---|
| 233 | print ">>> CONFIG FILE:", Path.config_file |
|---|
| 234 | print ">>> TARGET_LIST:", Path.target_list |
|---|
| 235 | print ">>> PROFILE_EDITOR:", Path.profile_editor |
|---|
| 236 | print ">>> WIZARD:", Path.wizard |
|---|
| 237 | print ">>> SCAN_PROFILE:", Path.scan_profile |
|---|
| 238 | print ">>> RECENT_SCANS:", Path.recent_scans |
|---|
| 239 | print ">>> OPTIONS:", Path.options |
|---|
| 240 | print |
|---|
| 241 | print ">>> UMIT_OP:", Path.umit_op |
|---|
| 242 | print ">>> UMIT_OPI:", Path.umit_opi |
|---|
| 243 | print ">>> UMIT_OPT:", Path.umit_opt |
|---|
| 244 | print ">>> UMIT_OPF:", Path.umit_opf |
|---|
| 245 | print ">>> UMITDB:", Path.umitdb |
|---|
| 246 | print ">>> SERVICES DUMP:", Path.services_dump |
|---|
| 247 | print ">>> OS DB DUMP:", Path.os_dump |
|---|
| 248 | print ">>> UMIT VERSION:", Path.umit_version |
|---|
| 249 | print ">>> OS CLASSIFICATION DUMP:", Path.os_classification |
|---|
| 250 | print ">>> NMAP COMMAND PATH:", Path.nmap_command_path |
|---|