| 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 | from os import R_OK, W_OK, access, mkdir, getcwd |
|---|
| 23 | from os.path import exists, join, split, abspath |
|---|
| 24 | import sys |
|---|
| 25 | |
|---|
| 26 | from umitCore.UmitLogging import log |
|---|
| 27 | from umitCore.UmitConfigParser import UmitConfigParser |
|---|
| 28 | from umitCore.BasePaths import base_paths, HOME |
|---|
| 29 | from umitCore.I18N import _ |
|---|
| 30 | |
|---|
| 31 | VERSION = "0.9.4" |
|---|
| 32 | REVISION = "1288" |
|---|
| 33 | |
|---|
| 34 | CONFIG_DIR = join("share", "umit", "config") |
|---|
| 35 | UMIT_ICON = join("share", "icons", "umit_48.ico") |
|---|
| 36 | LOCALE_DIR = join("share", "umit", "locale") |
|---|
| 37 | MISC_DIR = join("share", "umit", "misc") |
|---|
| 38 | ICONS_DIR = join("share", "icons") |
|---|
| 39 | PIXMAPS_DIR = join("share", "pixmaps") |
|---|
| 40 | DOCS_DIR = join("share", "umit", "docs") |
|---|
| 41 | |
|---|
| 42 | |
|---|
| 43 | ####### |
|---|
| 44 | # Paths |
|---|
| 45 | class Paths(object): |
|---|
| 46 | """Paths |
|---|
| 47 | """ |
|---|
| 48 | config_parser = UmitConfigParser() |
|---|
| 49 | paths_section = "paths" |
|---|
| 50 | |
|---|
| 51 | hardcoded = ["locale_dir", |
|---|
| 52 | "pixmaps_dir", |
|---|
| 53 | "icons_dir", |
|---|
| 54 | "misc_dir", |
|---|
| 55 | "docs_dir", |
|---|
| 56 | "umit_icon"] |
|---|
| 57 | |
|---|
| 58 | config_files_list = ["config_file", |
|---|
| 59 | "target_list", |
|---|
| 60 | "profile_editor", |
|---|
| 61 | "wizard", |
|---|
| 62 | "scan_profile", |
|---|
| 63 | "recent_scans", |
|---|
| 64 | "options", |
|---|
| 65 | "umitdb", |
|---|
| 66 | "umit_version"] |
|---|
| 67 | |
|---|
| 68 | share_files_list = ["umit_op", |
|---|
| 69 | "umit_opi", |
|---|
| 70 | "umit_opt", |
|---|
| 71 | "umit_opf"] |
|---|
| 72 | |
|---|
| 73 | misc_files_list = ["services_dump", |
|---|
| 74 | "os_dump", |
|---|
| 75 | "os_classification"] |
|---|
| 76 | |
|---|
| 77 | other_settings = ["nmap_command_path"] |
|---|
| 78 | |
|---|
| 79 | config_file_set = False |
|---|
| 80 | |
|---|
| 81 | def set_umit_conf(self, base_dir): |
|---|
| 82 | main_config_dir = "" |
|---|
| 83 | main_config_file = "" |
|---|
| 84 | if exists(CONFIG_DIR) and \ |
|---|
| 85 | exists(join(CONFIG_DIR, base_paths['config_file'])): |
|---|
| 86 | main_config_dir = CONFIG_DIR |
|---|
| 87 | |
|---|
| 88 | elif exists(join(base_dir, CONFIG_DIR)) and\ |
|---|
| 89 | exists(join(base_dir, |
|---|
| 90 | CONFIG_DIR, |
|---|
| 91 | base_paths['config_file'])): |
|---|
| 92 | main_config_dir = join(base_dir, CONFIG_DIR) |
|---|
| 93 | |
|---|
| 94 | elif exists(join(split(base_dir)[0], CONFIG_DIR)) and \ |
|---|
| 95 | exists(join(split(base_dir)[0], |
|---|
| 96 | CONFIG_DIR, |
|---|
| 97 | base_paths['config_file'])): |
|---|
| 98 | main_config_dir = join(split(base_dir)[0], CONFIG_DIR) |
|---|
| 99 | |
|---|
| 100 | else: |
|---|
| 101 | raise Exception("Couldn't find the Umit's Config Directory! \ |
|---|
| 102 | Aborting...") |
|---|
| 103 | |
|---|
| 104 | # Main config file, based on the main_config_dir got above |
|---|
| 105 | main_config_file = join(main_config_dir, base_paths['config_file']) |
|---|
| 106 | |
|---|
| 107 | # This is the expected place in which umit.conf should be placed |
|---|
| 108 | supposed_file = join(base_paths['user_dir'], base_paths['config_file']) |
|---|
| 109 | config_dir = "" |
|---|
| 110 | config_file = "" |
|---|
| 111 | |
|---|
| 112 | if exists(supposed_file)\ |
|---|
| 113 | and check_access(supposed_file, R_OK and W_OK): |
|---|
| 114 | config_dir = base_paths['user_dir'] |
|---|
| 115 | config_file = supposed_file |
|---|
| 116 | log.debug(">>> Using config files in user home \ |
|---|
| 117 | directory: %s" % config_file) |
|---|
| 118 | |
|---|
| 119 | elif not exists(supposed_file)\ |
|---|
| 120 | and not check_access(base_paths['user_dir'], |
|---|
| 121 | R_OK and W_OK): |
|---|
| 122 | try: |
|---|
| 123 | result = create_user_dir(join(main_config_dir, |
|---|
| 124 | base_paths['config_file']), |
|---|
| 125 | HOME) |
|---|
| 126 | if type(result) == type({}): |
|---|
| 127 | config_dir = result['config_dir'] |
|---|
| 128 | config_file = result['config_file'] |
|---|
| 129 | log.debug(">>> Using recently created config files in \ |
|---|
| 130 | user home: %s" % config_file) |
|---|
| 131 | else: |
|---|
| 132 | raise Exception() |
|---|
| 133 | except: |
|---|
| 134 | log.debug(">>> Failed to create user home") |
|---|
| 135 | |
|---|
| 136 | if config_dir and config_file: |
|---|
| 137 | # Checking if the version of the configuration files are the same |
|---|
| 138 | # as this Umit's version |
|---|
| 139 | if not self.check_version(config_dir): |
|---|
| 140 | log.debug(">>> The config files versions are different!") |
|---|
| 141 | log.debug(">>> Running update scripts...") |
|---|
| 142 | self.update_config_dir(config_dir) |
|---|
| 143 | |
|---|
| 144 | else: |
|---|
| 145 | log.debug(">>> There is no way to create nor use home connfigs.") |
|---|
| 146 | log.debug(">>> Trying to use main configuration files...") |
|---|
| 147 | |
|---|
| 148 | config_dir = main_config_dir |
|---|
| 149 | config_file = main_config_file |
|---|
| 150 | |
|---|
| 151 | # Parsing the umit main config file |
|---|
| 152 | self.config_parser.read(config_file) |
|---|
| 153 | |
|---|
| 154 | # Should make the following only after reading the umit.conf file |
|---|
| 155 | self.config_dir = config_dir |
|---|
| 156 | self.config_file = config_file |
|---|
| 157 | self.config_file_set = True |
|---|
| 158 | self.locale_dir = LOCALE_DIR |
|---|
| 159 | self.pixmaps_dir = PIXMAPS_DIR |
|---|
| 160 | self.icons_dir = ICONS_DIR |
|---|
| 161 | self.misc_dir = MISC_DIR |
|---|
| 162 | self.docs_dir = DOCS_DIR |
|---|
| 163 | self.umit_icon = UMIT_ICON |
|---|
| 164 | |
|---|
| 165 | log.debug(">>> Config file: %s" % config_file) |
|---|
| 166 | |
|---|
| 167 | def update_config_dir(self, config_dir): |
|---|
| 168 | pass |
|---|
| 169 | |
|---|
| 170 | def check_version(self, config_dir): |
|---|
| 171 | ver = open(join(config_dir, |
|---|
| 172 | base_paths['umit_version'])).readlines()[0].\ |
|---|
| 173 | split("\n")[0].\ |
|---|
| 174 | split("\r")[0] |
|---|
| 175 | |
|---|
| 176 | log.debug(">>> This Umit Version: %s" % VERSION) |
|---|
| 177 | log.debug(">>> Version of the files in %s: %s" % (config_dir, ver)) |
|---|
| 178 | |
|---|
| 179 | if VERSION == ver: |
|---|
| 180 | return True |
|---|
| 181 | return False |
|---|
| 182 | |
|---|
| 183 | def root_dir(self): |
|---|
| 184 | """Retrieves root dir on current filesystem""" |
|---|
| 185 | curr_dir = getcwd() |
|---|
| 186 | while True: |
|---|
| 187 | splited = split(curr_dir)[0] |
|---|
| 188 | if curr_dir == splited: |
|---|
| 189 | break |
|---|
| 190 | curr_dir = splited |
|---|
| 191 | |
|---|
| 192 | log.debug(">>> Root dir: %s" % curr_dir) |
|---|
| 193 | return curr_dir |
|---|
| 194 | |
|---|
| 195 | def __getattr__(self, name): |
|---|
| 196 | if self.config_file_set: |
|---|
| 197 | if name in self.other_settings: |
|---|
| 198 | return self.config_parser.get(self.paths_section, name) |
|---|
| 199 | |
|---|
| 200 | elif name in self.hardcoded: |
|---|
| 201 | return self.__dict__[name] |
|---|
| 202 | |
|---|
| 203 | elif name in self.config_files_list: |
|---|
| 204 | return return_if_exists(join(self.__dict__['config_dir'], |
|---|
| 205 | base_paths[name])) |
|---|
| 206 | |
|---|
| 207 | elif name in self.share_files_list: |
|---|
| 208 | return return_if_exists(join(self.__dict__['pixmaps_dir'], |
|---|
| 209 | base_paths[name])) |
|---|
| 210 | |
|---|
| 211 | elif name in self.misc_files_list: |
|---|
| 212 | return return_if_exists(join(self.__dict__["misc_dir"], |
|---|
| 213 | base_paths[name])) |
|---|
| 214 | |
|---|
| 215 | try: |
|---|
| 216 | return self.__dict__[name] |
|---|
| 217 | except: |
|---|
| 218 | raise NameError(name) |
|---|
| 219 | else: |
|---|
| 220 | raise Exception("Must set config file location first") |
|---|
| 221 | |
|---|
| 222 | def __setattr__(self, name, value): |
|---|
| 223 | if name in self.other_settings: |
|---|
| 224 | self.config_parser.set(self.paths_section, name, value) |
|---|
| 225 | else: |
|---|
| 226 | self.__dict__[name] = value |
|---|
| 227 | |
|---|
| 228 | #################################### |
|---|
| 229 | # Functions for directories creation |
|---|
| 230 | |
|---|
| 231 | def create_user_dir(config_file, user_home): |
|---|
| 232 | log.debug(">>> Create user dir at given home: %s" % user_home) |
|---|
| 233 | log.debug(">>> Using %s as source" % config_file) |
|---|
| 234 | |
|---|
| 235 | main_umit_conf = UmitConfigParser() |
|---|
| 236 | main_umit_conf.read(config_file) |
|---|
| 237 | paths_section = "paths" |
|---|
| 238 | |
|---|
| 239 | user_dir = join(user_home, base_paths['config_dir']) |
|---|
| 240 | |
|---|
| 241 | if exists(user_home)\ |
|---|
| 242 | and access(user_home, R_OK and W_OK)\ |
|---|
| 243 | and not exists(user_dir): |
|---|
| 244 | mkdir(user_dir) |
|---|
| 245 | log.debug(">>> Umit user dir successfully created! %s" % user_dir) |
|---|
| 246 | else: |
|---|
| 247 | log.warning(">>> No permissions to create user dir!") |
|---|
| 248 | return False |
|---|
| 249 | |
|---|
| 250 | main_dir = split(config_file)[0] |
|---|
| 251 | copy_config_file("options.xml", main_dir, user_dir) |
|---|
| 252 | copy_config_file("profile_editor.xml", main_dir, user_dir) |
|---|
| 253 | copy_config_file("recent_scans.txt", main_dir, user_dir) |
|---|
| 254 | copy_config_file("scan_profile.usp", main_dir, user_dir) |
|---|
| 255 | copy_config_file("target_list.txt", main_dir, user_dir) |
|---|
| 256 | copy_config_file("umit_version", main_dir, user_dir) |
|---|
| 257 | copy_config_file("umit.db", main_dir, user_dir) |
|---|
| 258 | copy_config_file("wizard.xml", main_dir, user_dir) |
|---|
| 259 | |
|---|
| 260 | return dict(user_dir = user_dir, |
|---|
| 261 | config_dir = user_dir, |
|---|
| 262 | config_file = copy_config_file("umit.conf", |
|---|
| 263 | split(config_file)[0], |
|---|
| 264 | user_dir)) |
|---|
| 265 | |
|---|
| 266 | def copy_config_file(filename, dir_origin, dir_destiny): |
|---|
| 267 | log.debug(">>> copy_config_file %s to %s" % (filename, dir_destiny)) |
|---|
| 268 | |
|---|
| 269 | origin = join(dir_origin, filename) |
|---|
| 270 | destiny = join(dir_destiny, filename) |
|---|
| 271 | |
|---|
| 272 | if not exists(destiny): |
|---|
| 273 | # Quick copy |
|---|
| 274 | open(destiny, 'w').write(open(origin).read()) |
|---|
| 275 | return destiny |
|---|
| 276 | |
|---|
| 277 | def check_access(path, permission): |
|---|
| 278 | return exists(path) and access(path, permission) |
|---|
| 279 | |
|---|
| 280 | def return_if_exists(path): |
|---|
| 281 | if exists(path): |
|---|
| 282 | return abspath(path) |
|---|
| 283 | raise Exception("File '%s' does not exist or could not be found!" % path) |
|---|
| 284 | |
|---|
| 285 | ############ |
|---|
| 286 | # Singleton! |
|---|
| 287 | Path = Paths() |
|---|
| 288 | |
|---|
| 289 | if __name__ == '__main__': |
|---|
| 290 | Path.set_umit_conf(split(sys.argv[0])[0]) |
|---|
| 291 | |
|---|
| 292 | print ">>> SAVED DIRECTORIES:" |
|---|
| 293 | print ">>> LOCALE DIR:", Path.locale_dir |
|---|
| 294 | print ">>> PIXMAPS DIR:", Path.pixmaps_dir |
|---|
| 295 | print ">>> CONFIG DIR:", Path.config_dir |
|---|
| 296 | print |
|---|
| 297 | print ">>> FILES:" |
|---|
| 298 | print ">>> CONFIG FILE:", Path.config_file |
|---|
| 299 | print ">>> TARGET_LIST:", Path.target_list |
|---|
| 300 | print ">>> PROFILE_EDITOR:", Path.profile_editor |
|---|
| 301 | print ">>> WIZARD:", Path.wizard |
|---|
| 302 | print ">>> SCAN_PROFILE:", Path.scan_profile |
|---|
| 303 | print ">>> RECENT_SCANS:", Path.recent_scans |
|---|
| 304 | print ">>> OPTIONS:", Path.options |
|---|
| 305 | print |
|---|
| 306 | print ">>> UMIT_OP:", Path.umit_op |
|---|
| 307 | print ">>> UMIT_OPI:", Path.umit_opi |
|---|
| 308 | print ">>> UMIT_OPT:", Path.umit_opt |
|---|
| 309 | print ">>> UMIT_OPF:", Path.umit_opf |
|---|
| 310 | print ">>> UMITDB:", Path.umitdb |
|---|
| 311 | print ">>> SERVICES DUMP:", Path.services_dump |
|---|
| 312 | print ">>> OS DB DUMP:", Path.os_dump |
|---|
| 313 | print ">>> UMIT VERSION:", Path.umit_version |
|---|
| 314 | print ">>> OS CLASSIFICATION DUMP:", Path.os_classification |
|---|
| 315 | print ">>> NMAP COMMAND PATH:", Path.nmap_command_path |
|---|