def rootResource(self): try: from twistedcaldav.config import config rootResource = getRootResource(config, self.store) except OSError, e: if e.errno == ENOENT: # Trying to re-write resources.xml but its parent directory does # not exist. The server's never been started, so we're missing # state required to do any work. raise ConfigurationError( "It appears that the server has never been started.\n" "Please start it at least once before running this tool.") elif e.errno == EACCES: # Trying to re-write resources.xml but it is not writable by the # current user. This most likely means we're in a system # configuration and the user doesn't have sufficient privileges # to do the other things the tool might need to do either. raise ConfigurationError("You must run this tool as root.") else: raise
def loadConfig(configFileName): """ Helper method for command-line utilities to load configuration plist and override certain values. """ if configFileName is None: configFileName = DEFAULT_CONFIG_FILE if not os.path.isfile(configFileName): raise ConfigurationError("No config file: %s" % (configFileName, )) config.load(configFileName) # Command-line utilities always want these enabled: config.EnableCalDAV = True config.EnableCardDAV = True return config
def validConfiguration(config): if config.Scheduling.iSchedule.DKIM.Enabled: if not config.Scheduling.iSchedule.DKIM.Domain and not config.ServerHostName: msg = "DKIM: No domain specified" log.error(msg) raise ConfigurationError(msg) if not config.Scheduling.iSchedule.DKIM.KeySelector: msg = "DKIM: No selector specified" log.error(msg) raise ConfigurationError(msg) if config.Scheduling.iSchedule.DKIM.SignatureAlgorithm not in ( RSA1, RSA256): msg = "DKIM: Invalid algorithm: %s" % ( config.Scheduling.iSchedule.SignatureAlgorithm, ) log.error(msg) raise ConfigurationError(msg) try: with open( config.Scheduling.iSchedule.DKIM.PrivateKeyFile) as f: key_data = f.read() except IOError, e: msg = "DKIM: Cannot read private key file: %s %s" % ( config.Scheduling.iSchedule.DKIM.PrivateKeyFile, e, ) log.error(msg) raise ConfigurationError(msg) try: RSA.importKey(key_data) except: msg = "DKIM: Invalid private key file: %s" % ( config.Scheduling.iSchedule.DKIM.PrivateKeyFile, ) log.error(msg) raise ConfigurationError(msg) try: with open(config.Scheduling.iSchedule.DKIM.PublicKeyFile) as f: key_data = f.read() except IOError, e: msg = "DKIM: Cannot read public key file: %s %s" % ( config.Scheduling.iSchedule.DKIM.PublicKeyFile, e, ) log.error(msg) raise ConfigurationError(msg)
def checkDirectory(dirpath, description, access=None, create=None, wait=False): """ Make sure dirpath is an existing directory, and optionally ensure it has the expected permissions. Alternatively the function can create the directory or can wait for someone else to create it. @param dirpath: The directory path we're checking @type dirpath: string @param description: A description of what the directory path represents, used in log messages @type description: string @param access: The type of access we're expecting, either os.W_OK or os.R_OK @param create: A tuple of (file permissions mode, username, groupname) to use when creating the directory. If create=None then no attempt will be made to create the directory. @type create: tuple @param wait: Wether the function should wait in a loop for the directory to be created by someone else (or mounted, etc.) @type wait: boolean """ # Note: we have to use print here because the logging mechanism has not # been set up yet. if not os.path.exists(dirpath) or ( diagnose.detectPhantomVolume(dirpath) == diagnose.EXIT_CODE_PHANTOM_DATA_VOLUME): if wait: # If we're being told to wait, post an alert that we can't continue # until the volume is mounted if not os.path.exists(dirpath) or ( diagnose.detectPhantomVolume(dirpath) == diagnose.EXIT_CODE_PHANTOM_DATA_VOLUME): from calendarserver.tap.util import AlertPoster AlertPoster.postAlert("MissingDataVolumeAlert", 0, ["volumePath", dirpath]) while not os.path.exists(dirpath) or ( diagnose.detectPhantomVolume(dirpath) == diagnose.EXIT_CODE_PHANTOM_DATA_VOLUME): if not os.path.exists(dirpath): print("Path does not exist: %s" % (dirpath, )) else: print("Path is not a real volume: %s" % (dirpath, )) sleep(5) else: try: mode, username, groupname = create except TypeError: raise ConfigurationError("%s does not exist: %s" % (description, dirpath)) try: os.mkdir(dirpath) except (OSError, IOError), e: print("Could not create %s: %s" % (dirpath, e)) raise ConfigurationError( "%s does not exist and cannot be created: %s" % (description, dirpath)) if username: uid = getpwnam(username).pw_uid else: uid = -1 if groupname: gid = getgrnam(groupname).gr_gid else: gid = -1 try: os.chmod(dirpath, mode) os.chown(dirpath, uid, gid) except (OSError, IOError), e: print("Unable to change mode/owner of %s: %s" % (dirpath, e)) print("Created directory: %s" % (dirpath, ))
if groupname: gid = getgrnam(groupname).gr_gid else: gid = -1 try: os.chmod(dirpath, mode) os.chown(dirpath, uid, gid) except (OSError, IOError), e: print("Unable to change mode/owner of %s: %s" % (dirpath, e)) print("Created directory: %s" % (dirpath, )) if not os.path.isdir(dirpath): raise ConfigurationError("%s is not a directory: %s" % (description, dirpath)) if access and not os.access(dirpath, access): raise ConfigurationError( "Insufficient permissions for server on %s directory: %s" % (description, dirpath)) @inlineCallbacks def principalForPrincipalID(principalID, checkOnly=False, directory=None): # Allow a directory parameter to be passed in, but default to config.directory # But config.directory isn't set right away, so only use it when we're doing more # than checking. if not checkOnly and not directory: directory = config.directory
class DKIMUtils(object): """ Some useful functions. """ @staticmethod def validConfiguration(config): if config.Scheduling.iSchedule.DKIM.Enabled: if not config.Scheduling.iSchedule.DKIM.Domain and not config.ServerHostName: msg = "DKIM: No domain specified" log.error(msg) raise ConfigurationError(msg) if not config.Scheduling.iSchedule.DKIM.KeySelector: msg = "DKIM: No selector specified" log.error(msg) raise ConfigurationError(msg) if config.Scheduling.iSchedule.DKIM.SignatureAlgorithm not in (RSA1, RSA256): msg = "DKIM: Invalid algorithm: %s" % (config.Scheduling.iSchedule.SignatureAlgorithm,) log.error(msg) raise ConfigurationError(msg) try: with open(config.Scheduling.iSchedule.DKIM.PrivateKeyFile) as f: key_data = f.read() except IOError, e: msg = "DKIM: Cannot read private key file: %s %s" % (config.Scheduling.iSchedule.DKIM.PrivateKeyFile, e,) log.error(msg) raise ConfigurationError(msg) try: RSA.importKey(key_data) except: msg = "DKIM: Invalid private key file: %s" % (config.Scheduling.iSchedule.DKIM.PrivateKeyFile,) log.error(msg) raise ConfigurationError(msg) try: with open(config.Scheduling.iSchedule.DKIM.PublicKeyFile) as f: key_data = f.read() except IOError, e: msg = "DKIM: Cannot read public key file: %s %s" % (config.Scheduling.iSchedule.DKIM.PublicKeyFile, e,) log.error(msg) raise ConfigurationError(msg) try: RSA.importKey(key_data) except: msg = "DKIM: Invalid public key file: %s" % (config.Scheduling.iSchedule.DKIM.PublicKeyFile,) log.error(msg) raise ConfigurationError(msg) if config.Scheduling.iSchedule.DKIM.PrivateExchanges: if not os.path.exists(config.Scheduling.iSchedule.DKIM.PrivateExchanges): try: os.makedirs(config.Scheduling.iSchedule.DKIM.PrivateExchanges) except IOError, e: msg = "DKIM: Cannot create public key private exchange directory: %s" % (config.Scheduling.iSchedule.DKIM.PrivateExchanges,) log.error(msg) raise ConfigurationError(msg) if not os.path.isdir(config.Scheduling.iSchedule.DKIM.PrivateExchanges): msg = "DKIM: Invalid public key private exchange directory: %s" % (config.Scheduling.iSchedule.DKIM.PrivateExchanges,) log.error(msg) raise ConfigurationError(msg) PublicKeyLookup_PrivateExchange.directory = config.Scheduling.iSchedule.DKIM.PrivateExchanges
def __init__(self): raise ConfigurationError("PostgreSQL module not available.")
def checkDirectory(dirpath, description, access=None, create=None, wait=False): """ Make sure dirpath is an existing directory, and optionally ensure it has the expected permissions. Alternatively the function can create the directory or can wait for someone else to create it. @param dirpath: The directory path we're checking @type dirpath: string @param description: A description of what the directory path represents, used in log messages @type description: string @param access: The type of access we're expecting, either os.W_OK or os.R_OK @param create: A tuple of (file permissions mode, username, groupname) to use when creating the directory. If create=None then no attempt will be made to create the directory. @type create: tuple @param wait: Wether the function should wait in a loop for the directory to be created by someone else (or mounted, etc.) @type wait: boolean """ # Note: we have to use print here because the logging mechanism has not # been set up yet. if not os.path.exists(dirpath): if wait: while not os.path.exists(dirpath): print("Path does not exist: %s" % (dirpath, )) sleep(1) else: try: mode, username, groupname = create except TypeError: raise ConfigurationError("%s does not exist: %s" % (description, dirpath)) try: os.mkdir(dirpath) except (OSError, IOError), e: print("Could not create %s: %s" % (dirpath, e)) raise ConfigurationError( "%s does not exist and cannot be created: %s" % (description, dirpath)) if username: uid = getpwnam(username).pw_uid else: uid = -1 if groupname: gid = getgrnam(groupname).gr_gid else: gid = -1 try: os.chmod(dirpath, mode) os.chown(dirpath, uid, gid) except (OSError, IOError), e: print("Unable to change mode/owner of %s: %s" % (dirpath, e)) print("Created directory: %s" % (dirpath, ))