| 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 | |
|---|
| 23 | import os |
|---|
| 24 | import sys |
|---|
| 25 | |
|---|
| 26 | from umitCore.UmitLogging import log |
|---|
| 27 | from umitCore.UmitConfigParser import UmitConfigParser |
|---|
| 28 | from umitCore.TempConf import create_temp_conf_dir |
|---|
| 29 | from umitCore.Version import VERSION |
|---|
| 30 | from umitCore.BasePaths import base_paths, HOME |
|---|
| 31 | from umitCore.BasePaths import CONFIG_DIR, LOCALE_DIR, MISC_DIR |
|---|
| 32 | from umitCore.BasePaths import ICONS_DIR, PIXMAPS_DIR, DOCS_DIR |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | def root_dir(): |
|---|
| 36 | """Retrieves root dir on current filesystem""" |
|---|
| 37 | curr_dir = os.getcwd() |
|---|
| 38 | while True: |
|---|
| 39 | splited = os.path.split(curr_dir)[0] |
|---|
| 40 | if curr_dir == splited: |
|---|
| 41 | break |
|---|
| 42 | curr_dir = splited |
|---|
| 43 | |
|---|
| 44 | log.debug(">>> Root dir: %s" % curr_dir) |
|---|
| 45 | return curr_dir |
|---|
| 46 | |
|---|
| 47 | ####### |
|---|
| 48 | # Paths |
|---|
| 49 | class Paths(object): |
|---|
| 50 | """Paths |
|---|
| 51 | """ |
|---|
| 52 | config_parser = UmitConfigParser() |
|---|
| 53 | paths_section = "paths" |
|---|
| 54 | |
|---|
| 55 | hardcoded = ["locale_dir", |
|---|
| 56 | "pixmaps_dir", |
|---|
| 57 | "icons_dir", |
|---|
| 58 | "misc_dir", |
|---|
| 59 | "config_dir", |
|---|
| 60 | "docs_dir"] |
|---|
| 61 | |
|---|
| 62 | config_files_list = ["config_file", |
|---|
| 63 | "profile_editor", |
|---|
| 64 | "wizard", |
|---|
| 65 | "scan_profile", |
|---|
| 66 | "options", |
|---|
| 67 | "umitdb_ng", |
|---|
| 68 | "tl_conf", |
|---|
| 69 | "tl_colors_std", |
|---|
| 70 | "sched_schemas", |
|---|
| 71 | "sched_profiles", |
|---|
| 72 | "sched_log", |
|---|
| 73 | "smtp_schemas", |
|---|
| 74 | "umit_version"] |
|---|
| 75 | |
|---|
| 76 | empty_config_files_list = ["target_list", |
|---|
| 77 | "recent_scans", |
|---|
| 78 | "umitdb"] |
|---|
| 79 | |
|---|
| 80 | share_files_list = ["umit_opt", |
|---|
| 81 | "umit_opf"] |
|---|
| 82 | |
|---|
| 83 | misc_files_list = ["services_dump", |
|---|
| 84 | "os_dump", |
|---|
| 85 | "os_classification", |
|---|
| 86 | "sched_running"] |
|---|
| 87 | |
|---|
| 88 | other_settings = ["nmap_command_path"] |
|---|
| 89 | |
|---|
| 90 | config_file_set = False |
|---|
| 91 | |
|---|
| 92 | def _parse_and_set_dirs(self, config_file, config_dir): |
|---|
| 93 | """Parse the given config_file and then set the directories.""" |
|---|
| 94 | # Parsing the umit main config file |
|---|
| 95 | self.config_parser.read(config_file) |
|---|
| 96 | |
|---|
| 97 | # Should make the following only after reading the umit.conf file |
|---|
| 98 | self.config_dir = config_dir |
|---|
| 99 | self.config_file = config_file |
|---|
| 100 | self.config_file_set = True |
|---|
| 101 | self.locale_dir = LOCALE_DIR |
|---|
| 102 | self.pixmaps_dir = PIXMAPS_DIR |
|---|
| 103 | self.icons_dir = ICONS_DIR |
|---|
| 104 | self.misc_dir = MISC_DIR |
|---|
| 105 | self.docs_dir = DOCS_DIR |
|---|
| 106 | |
|---|
| 107 | def get_running_path(self): |
|---|
| 108 | return self.__runpath |
|---|
| 109 | |
|---|
| 110 | def set_running_path(self, path): |
|---|
| 111 | """ |
|---|
| 112 | Sets path for current umit instance. |
|---|
| 113 | """ |
|---|
| 114 | self.__runpath = path |
|---|
| 115 | |
|---|
| 116 | def get_umit_conf(self): |
|---|
| 117 | """ |
|---|
| 118 | Returns umit conf file being used. |
|---|
| 119 | """ |
|---|
| 120 | if self.config_file_set: |
|---|
| 121 | return self.config_file |
|---|
| 122 | |
|---|
| 123 | def force_set_umit_conf(self, base_dir): |
|---|
| 124 | if not os.path.exists(base_dir): |
|---|
| 125 | return |
|---|
| 126 | |
|---|
| 127 | main_config_dir = base_dir |
|---|
| 128 | config_dir = base_dir |
|---|
| 129 | config_file = os.path.join(base_dir, 'umit.conf') |
|---|
| 130 | |
|---|
| 131 | self._parse_and_set_dirs(config_file, config_dir) |
|---|
| 132 | |
|---|
| 133 | def set_umit_conf(self, base_dir, force=False): |
|---|
| 134 | main_config_dir = "" |
|---|
| 135 | main_config_file = "" |
|---|
| 136 | |
|---|
| 137 | if (os.path.exists(CONFIG_DIR) and |
|---|
| 138 | os.path.exists(os.path.join(CONFIG_DIR, |
|---|
| 139 | base_paths['config_file']))): |
|---|
| 140 | main_config_dir = CONFIG_DIR |
|---|
| 141 | |
|---|
| 142 | elif ( |
|---|
| 143 | os.path.exists(os.path.join(base_dir, CONFIG_DIR)) and |
|---|
| 144 | os.path.exists(os.path.join( |
|---|
| 145 | base_dir, CONFIG_DIR, base_paths['config_file']))): |
|---|
| 146 | main_config_dir = os.path.join(base_dir, CONFIG_DIR) |
|---|
| 147 | |
|---|
| 148 | elif ( |
|---|
| 149 | os.path.exists(os.path.join(os.path.split(base_dir)[0], |
|---|
| 150 | CONFIG_DIR)) and |
|---|
| 151 | os.path.exists(os.path.join(os.path.split(base_dir)[0], |
|---|
| 152 | CONFIG_DIR, base_paths['config_file']))): |
|---|
| 153 | main_config_dir = ( |
|---|
| 154 | os.path.join(os.path.split(base_dir)[0], CONFIG_DIR)) |
|---|
| 155 | |
|---|
| 156 | else: |
|---|
| 157 | main_config_dir = create_temp_conf_dir(VERSION) |
|---|
| 158 | |
|---|
| 159 | # Main config file, based on the main_config_dir got above |
|---|
| 160 | main_config_file = os.path.join(main_config_dir, |
|---|
| 161 | base_paths['config_file']) |
|---|
| 162 | |
|---|
| 163 | # This is the expected place in which umit.conf should be placed |
|---|
| 164 | supposed_file = os.path.join(base_paths['user_dir'], |
|---|
| 165 | base_paths['config_file']) |
|---|
| 166 | config_dir = "" |
|---|
| 167 | config_file = "" |
|---|
| 168 | |
|---|
| 169 | if os.path.exists(supposed_file)\ |
|---|
| 170 | and check_access(supposed_file, os.R_OK and os.W_OK): |
|---|
| 171 | config_dir = base_paths['user_dir'] |
|---|
| 172 | config_file = supposed_file |
|---|
| 173 | log.debug(">>> Using config files in user home directory: %s" \ |
|---|
| 174 | % config_file) |
|---|
| 175 | |
|---|
| 176 | elif not os.path.exists(supposed_file)\ |
|---|
| 177 | and not check_access(base_paths['user_dir'], |
|---|
| 178 | os.R_OK and os.W_OK): |
|---|
| 179 | try: |
|---|
| 180 | result = create_user_dir(os.path.join(main_config_dir, |
|---|
| 181 | base_paths['config_file']), |
|---|
| 182 | HOME) |
|---|
| 183 | if type(result) == type({}): |
|---|
| 184 | config_dir = result['config_dir'] |
|---|
| 185 | config_file = result['config_file'] |
|---|
| 186 | log.debug(">>> Using recently created config files in \ |
|---|
| 187 | user home: %s" % config_file) |
|---|
| 188 | else: |
|---|
| 189 | raise Exception() |
|---|
| 190 | except: |
|---|
| 191 | log.debug(">>> Failed to create user home") |
|---|
| 192 | |
|---|
| 193 | if config_dir and config_file: |
|---|
| 194 | # Checking if the version of the configuration files are the same |
|---|
| 195 | # as this Umit's version |
|---|
| 196 | if not self.check_version(config_dir): |
|---|
| 197 | log.debug(">>> The config files versions are different!") |
|---|
| 198 | log.debug(">>> Running update scripts...") |
|---|
| 199 | self.update_config_dir(config_dir) |
|---|
| 200 | |
|---|
| 201 | else: |
|---|
| 202 | log.debug(">>> There is no way to create nor use home connfigs.") |
|---|
| 203 | log.debug(">>> Trying to use main configuration files...") |
|---|
| 204 | |
|---|
| 205 | config_dir = main_config_dir |
|---|
| 206 | config_file = main_config_file |
|---|
| 207 | |
|---|
| 208 | self._parse_and_set_dirs(config_file, config_dir) |
|---|
| 209 | |
|---|
| 210 | for plug_path in ('plugins', 'plugins-download', 'plugins-temp'): |
|---|
| 211 | dir_path = os.path.join(config_dir, plug_path) |
|---|
| 212 | try: |
|---|
| 213 | if not os.path.exists(dir_path): |
|---|
| 214 | os.makedirs(dir_path) |
|---|
| 215 | except: |
|---|
| 216 | pass |
|---|
| 217 | |
|---|
| 218 | log.debug(">>> Config file: %s" % config_file) |
|---|
| 219 | log.debug(">>> Locale: %s" % self.locale_dir) |
|---|
| 220 | log.debug(">>> Pixmaps: %s" % self.pixmaps_dir) |
|---|
| 221 | log.debug(">>> Icons: %s" % self.icons_dir) |
|---|
| 222 | log.debug(">>> Misc: %s" % self.misc_dir) |
|---|
| 223 | log.debug(">>> Docs: %s" % self.docs_dir) |
|---|
| 224 | |
|---|
| 225 | def update_config_dir(self, config_dir): |
|---|
| 226 | # Do any updates of configuration files. Not yet implemented. |
|---|
| 227 | pass |
|---|
| 228 | |
|---|
| 229 | def check_version(self, config_dir): |
|---|
| 230 | version_file = os.path.join(config_dir, base_paths['umit_version']) |
|---|
| 231 | |
|---|
| 232 | if os.path.exists(version_file): |
|---|
| 233 | ver = open(version_file).readline().strip() |
|---|
| 234 | |
|---|
| 235 | log.debug(">>> This Umit Version: %s" % VERSION) |
|---|
| 236 | log.debug(">>> Version of the files in %s: %s" % (config_dir, ver)) |
|---|
| 237 | |
|---|
| 238 | if VERSION == ver: |
|---|
| 239 | return True |
|---|
| 240 | return False |
|---|
| 241 | |
|---|
| 242 | def __getattr__(self, name): |
|---|
| 243 | if self.config_file_set: |
|---|
| 244 | if name in self.other_settings: |
|---|
| 245 | return self.config_parser.get(self.paths_section, name) |
|---|
| 246 | |
|---|
| 247 | elif name in self.hardcoded: |
|---|
| 248 | return self.__dict__[name] |
|---|
| 249 | |
|---|
| 250 | elif name in self.config_files_list: |
|---|
| 251 | return return_if_exists(os.path.join( |
|---|
| 252 | self.__dict__['config_dir'], base_paths[name])) |
|---|
| 253 | |
|---|
| 254 | elif name in self.empty_config_files_list: |
|---|
| 255 | return return_if_exists(os.path.join( |
|---|
| 256 | self.__dict__['config_dir'], base_paths[name]), |
|---|
| 257 | True) |
|---|
| 258 | |
|---|
| 259 | elif name in self.share_files_list: |
|---|
| 260 | return return_if_exists(os.path.join( |
|---|
| 261 | self.__dict__['pixmaps_dir'], base_paths[name])) |
|---|
| 262 | |
|---|
| 263 | elif name in self.misc_files_list: |
|---|
| 264 | return return_if_exists(os.path.join( |
|---|
| 265 | self.__dict__["misc_dir"], base_paths[name])) |
|---|
| 266 | |
|---|
| 267 | try: |
|---|
| 268 | return self.__dict__[name] |
|---|
| 269 | except: |
|---|
| 270 | raise NameError(name) |
|---|
| 271 | else: |
|---|
| 272 | raise Exception("Must set config file location first") |
|---|
| 273 | |
|---|
| 274 | def __setattr__(self, name, value): |
|---|
| 275 | if name in self.other_settings: |
|---|
| 276 | self.config_parser.set(self.paths_section, name, value) |
|---|
| 277 | else: |
|---|
| 278 | self.__dict__[name] = value |
|---|
| 279 | |
|---|
| 280 | #################################### |
|---|
| 281 | # Functions for directories creation |
|---|
| 282 | |
|---|
| 283 | def create_user_dir(config_file, user_home): |
|---|
| 284 | log.debug(">>> Create user dir at given home: %s" % user_home) |
|---|
| 285 | log.debug(">>> Using %s as source" % config_file) |
|---|
| 286 | |
|---|
| 287 | main_umit_conf = UmitConfigParser() |
|---|
| 288 | main_umit_conf.read(config_file) |
|---|
| 289 | |
|---|
| 290 | user_dir = os.path.join(user_home, base_paths['config_dir']) |
|---|
| 291 | |
|---|
| 292 | if os.path.exists(user_home)\ |
|---|
| 293 | and os.access(user_home, os.R_OK and os.W_OK)\ |
|---|
| 294 | and not os.path.exists(user_dir): |
|---|
| 295 | os.mkdir(user_dir) |
|---|
| 296 | os.mkdir(os.path.join(user_dir, "plugins")) |
|---|
| 297 | os.mkdir(os.path.join(user_dir, "plugins-download")) |
|---|
| 298 | os.mkdir(os.path.join(user_dir, "plugins-temp")) |
|---|
| 299 | log.debug(">>> Umit user dir successfully created! %s" % user_dir) |
|---|
| 300 | else: |
|---|
| 301 | log.warning(">>> No permissions to create user dir!") |
|---|
| 302 | return False |
|---|
| 303 | |
|---|
| 304 | main_dir = os.path.split(config_file)[0] |
|---|
| 305 | copy_config_file("options.xml", main_dir, user_dir) |
|---|
| 306 | copy_config_file("profile_editor.xml", main_dir, user_dir) |
|---|
| 307 | copy_config_file("scan_profile.usp", main_dir, user_dir) |
|---|
| 308 | copy_config_file("umit_version", main_dir, user_dir) |
|---|
| 309 | copy_config_file("umitng.db", main_dir, user_dir) |
|---|
| 310 | copy_config_file("timeline-settings.conf", main_dir, user_dir) |
|---|
| 311 | copy_config_file("tl_colors_evt_std.conf", main_dir, user_dir) |
|---|
| 312 | copy_config_file("scheduler-schemas.conf", main_dir, user_dir) |
|---|
| 313 | copy_config_file("scheduler-profiles.conf", main_dir, user_dir) |
|---|
| 314 | copy_config_file("scheduler.log", main_dir, user_dir) |
|---|
| 315 | copy_config_file("smtp-schemas.conf", main_dir, user_dir) |
|---|
| 316 | copy_config_file("wizard.xml", main_dir, user_dir) |
|---|
| 317 | |
|---|
| 318 | return dict(user_dir = user_dir, |
|---|
| 319 | config_dir = user_dir, |
|---|
| 320 | config_file = copy_config_file("umit.conf", |
|---|
| 321 | os.path.split(config_file)[0], |
|---|
| 322 | user_dir)) |
|---|
| 323 | |
|---|
| 324 | def copy_config_file(filename, dir_origin, dir_destiny): |
|---|
| 325 | log.debug(">>> copy_config_file %s to %s" % (filename, dir_destiny)) |
|---|
| 326 | |
|---|
| 327 | origin = os.path.join(dir_origin, filename) |
|---|
| 328 | destiny = os.path.join(dir_destiny, filename) |
|---|
| 329 | |
|---|
| 330 | if not os.path.exists(destiny): |
|---|
| 331 | # Quick copy |
|---|
| 332 | origin_file = open(origin, 'rb') |
|---|
| 333 | destiny_file = open(destiny, 'wb') |
|---|
| 334 | destiny_file.write(origin_file.read()) |
|---|
| 335 | origin_file.close() |
|---|
| 336 | destiny_file.close() |
|---|
| 337 | return destiny |
|---|
| 338 | |
|---|
| 339 | def check_access(path, permission): |
|---|
| 340 | if isinstance(path, basestring): |
|---|
| 341 | return os.path.exists(path) and os.access(path, permission) |
|---|
| 342 | return False |
|---|
| 343 | |
|---|
| 344 | def return_if_exists(path, create=False): |
|---|
| 345 | path = os.path.abspath(path) |
|---|
| 346 | if os.path.exists(path): |
|---|
| 347 | return path |
|---|
| 348 | elif create: |
|---|
| 349 | f = open(path, "w") |
|---|
| 350 | f.close() |
|---|
| 351 | return path |
|---|
| 352 | raise Exception("File '%s' does not exist or could not be found!" % path) |
|---|
| 353 | |
|---|
| 354 | ############ |
|---|
| 355 | # Singleton! |
|---|
| 356 | Path = Paths() |
|---|
| 357 | |
|---|
| 358 | if __name__ == '__main__': |
|---|
| 359 | Path.set_umit_conf(os.path.split(sys.argv[0])[0]) |
|---|
| 360 | |
|---|
| 361 | print ">>> SAVED DIRECTORIES:" |
|---|
| 362 | print ">>> LOCALE DIR:", Path.locale_dir |
|---|
| 363 | print ">>> PIXMAPS DIR:", Path.pixmaps_dir |
|---|
| 364 | print ">>> CONFIG DIR:", Path.config_dir |
|---|
| 365 | print |
|---|
| 366 | print ">>> FILES:" |
|---|
| 367 | print ">>> CONFIG FILE:", Path.config_file |
|---|
| 368 | print ">>> TARGET_LIST:", Path.target_list |
|---|
| 369 | print ">>> PROFILE_EDITOR:", Path.profile_editor |
|---|
| 370 | print ">>> WIZARD:", Path.wizard |
|---|
| 371 | print ">>> SCAN_PROFILE:", Path.scan_profile |
|---|
| 372 | print ">>> RECENT_SCANS:", Path.recent_scans |
|---|
| 373 | print ">>> OPTIONS:", Path.options |
|---|
| 374 | print |
|---|
| 375 | print ">>> UMIT_OPT:", Path.umit_opt |
|---|
| 376 | print ">>> UMIT_OPF:", Path.umit_opf |
|---|
| 377 | print ">>> UMITDB:", Path.umitdb |
|---|
| 378 | print ">>> UMITDB (New generation):", Path.umitdb_ng |
|---|
| 379 | print ">>> SERVICES DUMP:", Path.services_dump |
|---|
| 380 | print ">>> OS DB DUMP:", Path.os_dump |
|---|
| 381 | print ">>> UMIT VERSION:", Path.umit_version |
|---|
| 382 | print ">>> OS CLASSIFICATION DUMP:", Path.os_classification |
|---|
| 383 | print ">>> NMAP COMMAND PATH:", Path.nmap_command_path |
|---|