]> scripts.mit.edu Git - wizard.git/blobdiff - wizard/command/__init__.py
Fix kernel buffer overflow by avoiding passing --debug to subprocesses.
[wizard.git] / wizard / command / __init__.py
index 74b6e5eb41e9f249dc808bcc3ed69fadb87cb680..e3558f6dfb3fdbe82dd015ddaa5c1f529f2fdfd0 100644 (file)
 import logging
+import traceback
 import os
 import sys
 import optparse
 import errno
+import pwd
+import shutil
+import cStringIO
 
 import wizard
+from wizard import util, shell
 
-logging_setup = False
-
-class Error(wizard.Error):
-    """Base error class for all command errors"""
-    pass
-
-class PermissionsError(Error):
-    def __init__(self, dir):
-        self.dir = dir
-    def __str__(self):
-        return """
-
-ERROR: You don't have permissions to access this directory.
-Do you have tokens for AFS with your root instance, and
-is your root instance on scripts-security-upd?
-
-You can check by running the commands 'klist' and
-'blanche scripts-security-upd'.
-"""
-
-class NoSuchDirectoryError(Error):
-    def __init__(self, dir):
-        self.dir = dir
-    def __str__(self):
-        return """
-
-ERROR: No such directory... check your typing
-"""
+def chdir_to_production():
+    if os.path.exists(".git/WIZARD_UPGRADE_VERSION"): # XXX do something more robust
+        util.chdir(shell.eval("git", "config", "remote.origin.url"))
+        return True
+    return False
 
-def boolish(val):
-    """
-    Parse the contents of an environment variable as a boolean.
-    This recognizes more values as ``False`` than :func:`bool` would.
-
-        >>> boolish("0")
-        False
-        >>> boolish("no")
-        False
-        >>> boolish("1")
-        True
-    """
-    try:
-        return bool(int(val))
-    except (ValueError, TypeError):
-        if val == "No" or val == "no" or val == "false" or val == "False":
-            return False
-        return bool(val)
-
-def chdir(dir):
-    try:
-        os.chdir(dir)
-    except OSError as e:
-        if e.errno == errno.EACCES:
-            raise PermissionsError(dir)
-        elif e.errno == errno.ENOENT:
-            raise NoSuchDirectoryError(dir)
-        else: raise e
+logging_setup = False
+debug = True # This will get overwritten with the real value early on
 
-def makeLogger(options, numeric_args):
+def setup_logger(options, numeric_args):
     global logging_setup
     if logging_setup: return logging.getLogger()
-    context = " ".join(numeric_args)
     logger = logging.getLogger()
+    logger.handlers = [] # under certain cases, a spurious stream handler is set. We don't know why
     logger.setLevel(logging.INFO)
     stderr = logging.StreamHandler(sys.stderr)
-    stderr.setFormatter(logging.Formatter(" " * int(options.indent) + '%(levelname)s: %(message)s'))
-    dateFormat = "%H:%M:%S"
-    if options.context:
-        logformatter = logging.Formatter("%(asctime)s %(levelname)s -- " + context + ": %(message)s", dateFormat)
+    stderr.setFormatter(logging.Formatter('%(levelname)s: %(message)s'))
+    if not options.quiet:
+        logger.addHandler(stderr)
     else:
-        logformatter = logging.Formatter("%(asctime)s %(levelname)s: %(message)s", dateFormat)
-    if not options.quiet: logger.addHandler(stderr)
-    else: logger.addHandler(NullLogHandler()) # prevent default
+        logger.addHandler(NullLogHandler()) # prevent default
     if options.log_file:
-        file = logging.FileHandler(options.log_file)
-        file.setFormatter(logformatter)
-        logger.addHandler(file)
-        if options.log_file_chmod:
-            os.chmod(options.log_file, int(options.log_file_chmod, 8))
+        setup_file_logger(options.log_file, options.debug)
     if options.debug:
         logger.setLevel(logging.DEBUG)
     else:
         stderr.setLevel(logging.WARNING)
-        if options.verbose or hasattr(options, "dry_run"):
+        if options.verbose:
             stderr.setLevel(logging.INFO)
-        if options.log_file:
-            file.setLevel(logging.INFO)
     logging_setup = True
     return logger
 
-def makeBaseArgs(options, **grab):
+def setup_file_logger(log_file, debug):
+    logger = logging.getLogger()
+    file = logging.FileHandler(log_file)
+    logformatter = logging.Formatter("%(asctime)s %(levelname)s: %(message)s", "%Y-%m-%d %H:%M")
+    file.setFormatter(logformatter)
+    logger.addHandler(file)
+    if not debug:
+        file.setLevel(logging.INFO)
+    return file
+
+def make_base_args(options, **grab):
     """Takes parsed options, and breaks them back into a command
     line string that we can pass into a subcommand"""
     args = []
-    grab["log_file"]= "--log-file"
-    grab["debug"]   = "--debug"
-    grab["verbose"] = "--verbose"
-    grab["indent"]  = "--indent"
-    grab["quiet"]   = "--quiet"
-    #grab["log_db"] = "--log-db"
+    # Note: you can override these by passing in debug = None
+    # in your kwargs.
+    grab.setdefault("debug", "--debug")
+    grab.setdefault("verbose", "--verbose")
+    grab.setdefault("quiet", "--quiet")
+    #grab.setdefault("log_db", "--log-db")
     for k,flag in grab.items():
+        if not flag: continue
         value = getattr(options, k)
-        if not value and k != "indent": continue
+        if not value: continue
         args.append(flag)
         if type(value) is not bool:
-            if k == "indent":
-                value += 4
             args.append(str(value))
-    args.append("--context") # always have context for a subcommand
     return args
 
+def security_check_homedir(location):
+    """
+    Performs a check against a directory to determine if current
+    directory's owner has a home directory that is a parent directory.
+    This protects against malicious mountpoints, and is roughly equivalent
+    to the suexec checks.
+    """
+    # XXX: this is a smidge unfriendly to systems who haven't setup
+    # nswitch.
+    try:
+        uid = util.get_dir_uid(location)
+        real = os.path.realpath(location)
+        if not real.startswith(pwd.getpwuid(uid).pw_dir + "/"):
+            logging.error("Security check failed, owner of deployment and "
+                    "owner of home directory mismatch for %s" % location)
+            return False
+    except KeyError:
+        logging.error("Security check failed, could not look up "
+                "owner of %s (uid %d)" % (location, uid))
+        return False
+    except OSError as e:
+        logging.error("OSError: %s" % str(e))
+        return False
+    return True
+
+def calculate_log_name(log_dir, i):
+    """
+    Calculates a log entry given a numeric identifier, and
+    directory under operation.
+    """
+    return os.path.join(log_dir, "%04d.log" % i)
+
+def create_logdir(log_dir):
+    """
+    Creates a log directory and chmods it 777 to enable de-priviledged
+    processes to create files.
+    """
+    try:
+        os.mkdir(log_dir)
+    except OSError as e:
+        if e.errno != errno.EEXIST:
+            raise
+        #if create_subdirs:
+        #    log_dir = os.path.join(log_dir, str(int(time.time())))
+        #    os.mkdir(log_dir) # if fails, be fatal
+        #    # XXX: update last symlink
+    os.chmod(log_dir, 0o777)
+
 class NullLogHandler(logging.Handler):
     """Log handler that doesn't do anything"""
     def emit(self, record):
@@ -124,29 +127,37 @@ class NullLogHandler(logging.Handler):
 
 class WizardOptionParser(optparse.OptionParser):
     """Configures some default user-level options"""
+    store_help = False
     def __init__(self, *args, **kwargs):
         kwargs["add_help_option"] = False
+        if "store_help" in kwargs:
+            self.store_help = kwargs["store_help"]
+            del kwargs["store_help"]
         optparse.OptionParser.__init__(self, *args, **kwargs)
-    def parse_all(self, argv):
-        self.add_option("-h", "--help", action="help", help=optparse.SUPPRESS_HELP)
+    def parse_all(self, *args, **kwargs):
+        if self.store_help:
+            self.add_option("-h", "--help", action="store_true", default=False, dest="help", help=optparse.SUPPRESS_HELP)
+        else:
+            self.add_option("-h", "--help", action="help", help=optparse.SUPPRESS_HELP)
         group = optparse.OptionGroup(self, "Common Options")
         group.add_option("-v", "--verbose", dest="verbose", action="store_true",
-                default=boolish(os.getenv("WIZARD_VERBOSE")), help="Turns on verbose output.  Environment variable is WIZARD_VERBOSE")
+                default=util.boolish(os.getenv("WIZARD_VERBOSE")), help="Turns on verbose output.  Envvar is WIZARD_VERBOSE")
         group.add_option("--debug", dest="debug", action="store_true",
-                default=boolish(os.getenv("WIZARD_DEBUG")), help="Turns on debugging output.  Environment variable is WIZARD_DEBUG")
+                default=util.boolish(os.getenv("WIZARD_DEBUG")), help="Turns on debugging output.  Envvar is WIZARD_DEBUG")
         group.add_option("-q", "--quiet", dest="quiet", action="store_true",
-                default=boolish(os.getenv("WIZARD_QUIET")), help="Turns off output to stdout. Environment variable is WIZARD_QUIET")
+                default=util.boolish(os.getenv("WIZARD_QUIET")), help="Turns off output to stdout. Envvar is WIZARD_QUIET")
         group.add_option("--log-file", dest="log_file", metavar="FILE",
-                default=None, help="Logs verbose output to file")
-        group.add_option("--log-file-chmod", dest="log_file_chmod", metavar="CHMOD",
-                default=None, help="Chmod the log file after opening.  Number is octal. You must chmod the log file 666 and place the file in /tmp if subprocesses are running as different users.")
-        group.add_option("--indent", dest="indent", metavar="WIDTH",
-                default=0, help="Indents stdout, useful for nested calls")
-        group.add_option("--context", dest="context", action="store_true",
-                default=False, help="Adds context to logs, useful for parallel processing")
+                default=os.getenv("WIZARD_LOGFILE"), help="Logs verbose output to file")
+        group.add_option("--directory", dest="directory", metavar="PATH",
+                default=os.getenv("WIZARD_DIRECTORY", ".wizard"), help="Initialize this folder to store metadata.")
         self.add_option_group(group)
-        options, numeric_args = self.parse_args(argv)
-        makeLogger(options, numeric_args)
+        options, numeric_args = self.parse_args(*args, **kwargs)
+        setup_logger(options, numeric_args)
+        debug = options.debug
+        # we're going to process the global --log-dir/--seen dependency here
+        if hasattr(options, "seen") and hasattr(options, "log_dir"):
+            if not options.seen and options.log_dir:
+                options.seen = os.path.join(options.log_dir, "seen.txt")
         return options, numeric_args
 
 class OptionBaton(object):
@@ -164,3 +175,7 @@ class OptionBaton(object):
         """Hands off parameters to option parser"""
         for key in args:
             option_parser.add_option(self.store[key])
+
+class Error(wizard.Error):
+    """Base error class for all command errors"""
+    pass