def __init__(self, *args, **kwargs): super(AdminParametersForm, self).__init__(*args, **kwargs) self.field_widths = {"default_domain_quota": 2} hide_fields = False dpath = None code, output = exec_cmd("which dovecot") if not code: dpath = output.strip() else: known_paths = getattr( settings, "DOVECOT_LOOKUP_PATH", ("/usr/sbin/dovecot", "/usr/local/sbin/dovecot")) for fpath in known_paths: if os.path.isfile(fpath) and os.access(fpath, os.X_OK): dpath = fpath if dpath: try: code, version = exec_cmd("%s --version" % dpath) except OSError: hide_fields = True else: if code or not version.strip().startswith("2"): hide_fields = True else: hide_fields = True if hide_fields: del self.fields["handle_mailboxes"] del self.fields["mailboxes_owner"]
def create_new_dkim_key(self, domain): """Create a new DKIM key.""" storage_dir = param_tools.get_global_parameter("dkim_keys_storage_dir") pkey_path = os.path.join(storage_dir, "{}.pem".format(domain.name)) key_size = ( domain.dkim_key_length if domain.dkim_key_length else self.default_key_length) code, output = sysutils.exec_cmd( "openssl genrsa -out {} {}".format(pkey_path, key_size)) if code: print("Failed to generate DKIM private key for domain {}: {}" .format(domain.name, smart_text(output))) domain.dkim_private_key_path = pkey_path code, output = sysutils.exec_cmd( "openssl rsa -in {} -pubout".format(pkey_path)) if code: print("Failed to generate DKIM public key for domain {}: {}" .format(domain.name, smart_text(output))) public_key = "" for cpt, line in enumerate(smart_text(output).splitlines()): if cpt == 0 or line.startswith("-----"): continue public_key += line domain.dkim_public_key = public_key domain.save(update_fields=["dkim_public_key", "dkim_private_key_path"])
def __init__(self, *args, **kwargs): super(AdminParametersForm, self).__init__(*args, **kwargs) self.field_widths = { "default_domain_quota": 2 } hide_fields = False dpath = None code, output = exec_cmd("which dovecot") if not code: dpath = output.strip() else: known_paths = getattr( settings, "DOVECOT_LOOKUP_PATH", ("/usr/sbin/dovecot", "/usr/local/sbin/dovecot") ) for fpath in known_paths: if os.path.isfile(fpath) and os.access(fpath, os.X_OK): dpath = fpath if dpath: try: code, version = exec_cmd("%s --version" % dpath) except OSError: hide_fields = True else: if code or not version.strip().startswith("2"): hide_fields = True else: hide_fields = True if hide_fields: del self.fields["handle_mailboxes"] del self.fields["mailboxes_owner"]
def create_new_dkim_key(self, domain): """Create a new DKIM key.""" storage_dir = param_tools.get_global_parameter("dkim_keys_storage_dir") pkey_path = os.path.join(storage_dir, "{}.pem".format(domain.name)) key_size = (domain.dkim_key_length if domain.dkim_key_length else self.default_key_length) code, output = sysutils.exec_cmd("openssl genrsa -out {} {}".format( pkey_path, key_size)) if code: print( "Failed to generate DKIM private key for domain {}: {}".format( domain.name, smart_text(output))) domain.dkim_private_key_path = pkey_path code, output = sysutils.exec_cmd( "openssl rsa -in {} -pubout".format(pkey_path)) if code: print( "Failed to generate DKIM public key for domain {}: {}".format( domain.name, smart_text(output))) public_key = "" for cpt, line in enumerate(smart_text(output).splitlines()): if cpt == 0 or line.startswith("-----"): continue public_key += line domain.dkim_public_key = public_key domain.save(update_fields=["dkim_public_key", "dkim_private_key_path"])
def handle(self, parsed_args): management.call_command( 'startproject', parsed_args.name, verbosity=False ) if os.path.exists("%(name)s/%(name)s" % {'name': parsed_args.name}): # Django 1.4+ path = "%(name)s/%(name)s" % {'name': parsed_args.name} sys.path.append(parsed_args.name) django14 = True else: path = parsed_args.name sys.path.append(".") django14 = False t = Template(dbconn_tpl) default_conn = t.render(Context(self.ask_db_info())) amavis_conn = t.render(Context(self.ask_db_info('amavis'))) \ if parsed_args.with_amavis else None allowed_host = raw_input( 'Under which domain do you want to deploy modoboa? ' ) mod = __import__(parsed_args.name, globals(), locals(), ['settings']) tpl = self._render_template( "%s/settings.py.tpl" % self._templates_dir, { 'default_conn': default_conn, 'amavis_conn': amavis_conn, 'secret_key': mod.settings.SECRET_KEY, 'name': parsed_args.name, 'django14': django14, 'allowed_host': allowed_host } ) fp = open("%s/settings.py" % path, "w") fp.write(tpl) fp.close() shutil.copyfile( "%s/urls.py.tpl" % self._templates_dir, "%s/urls.py" % path ) os.mkdir("%s/media" % path) if parsed_args.syncdb: self._exec_django_command( "syncdb", parsed_args.name, '--noinput' ) exec_cmd('sed -ri "s|^#(\s+\'south)|\\1|" %s/settings.py' % path) self._exec_django_command( "syncdb", parsed_args.name, ) self._exec_django_command( 'migrate', parsed_args.name, '--fake' ) self._exec_django_command( "loaddata", parsed_args.name, 'initial_users.json' ) if parsed_args.collectstatic: self._exec_django_command( "collectstatic", parsed_args.name, '--noinput' )
def handle(self, parsed_args): management.call_command('startproject', parsed_args.name, verbosity=False) if os.path.exists("%(name)s/%(name)s" % {'name': parsed_args.name}): # Django 1.4+ path = "%(name)s/%(name)s" % {'name': parsed_args.name} sys.path.append(parsed_args.name) django14 = True else: path = parsed_args.name sys.path.append(".") django14 = False t = Template(dbconn_tpl) default_conn = t.render(Context(self.ask_db_info())) amavis_conn = t.render(Context(self.ask_db_info('amavis'))) \ if parsed_args.with_amavis else None allowed_host = raw_input( 'Under which domain do you want to deploy modoboa? ') mod = __import__(parsed_args.name, globals(), locals(), ['settings']) tpl = self._render_template( "%s/settings.py.tpl" % self._templates_dir, { 'default_conn': default_conn, 'amavis_conn': amavis_conn, 'secret_key': mod.settings.SECRET_KEY, 'name': parsed_args.name, 'django14': django14, 'allowed_host': allowed_host }) fp = open("%s/settings.py" % path, "w") fp.write(tpl) fp.close() shutil.copyfile("%s/urls.py.tpl" % self._templates_dir, "%s/urls.py" % path) os.mkdir("%s/media" % path) if parsed_args.syncdb: self._exec_django_command("syncdb", parsed_args.name, '--noinput') exec_cmd('sed -ri "s|^#(\s+\'south)|\\1|" %s/settings.py' % path) self._exec_django_command( "syncdb", parsed_args.name, ) self._exec_django_command('migrate', parsed_args.name, '--fake') self._exec_django_command("loaddata", parsed_args.name, 'initial_users.json') if parsed_args.collectstatic: self._exec_django_command("collectstatic", parsed_args.name, '--noinput')
def on(self): self.enabled = True self.save() self.__get_ext_instance() self.instance.load() self.instance.init() if self.instance.needs_media: path = os.path.join(settings.MEDIA_ROOT, self.name) exec_cmd("mkdir %s" % path) events.raiseEvent("ExtEnabled", self)
def off(self): self.__get_ext_instance() if self.instance is None: return self.instance.destroy() self.enabled = False self.save() if self.instance.needs_media: path = os.path.join(settings.MEDIA_ROOT, self.name) exec_cmd("rm -r %s" % path) events.raiseEvent("ExtDisabled", self)
def on(self, update_db=True): """Activate this extension.""" if update_db: self.enabled = True self.save() self.__get_ext_instance() self.instance.load() self.instance.init() if self.instance.needs_media: path = os.path.join(settings.MEDIA_ROOT, self.name) exec_cmd("mkdir %s" % path) events.raiseEvent("ExtEnabled", self)
def export(self, rrdfile, start, end): """Export data to XML using rrdtool and convert it to JSON.""" result = [] cmdargs = [] for curve in self._curves: result += [{ "name": str(curve.legend), "color": curve.color, "data": [] }] cmdargs += curve.to_rrd_command_args(rrdfile) code = 0 cmd = "{} xport --start {} --end {} ".format(self.rrdtool_binary, str(start), str(end)) cmd += " ".join(cmdargs) code, output = exec_cmd(smart_bytes(cmd)) if code: return [] tree = etree.fromstring(output) timestamp = int(tree.xpath('/xport/meta/start')[0].text) step = int(tree.xpath('/xport/meta/step')[0].text) for row in tree.xpath('/xport/data/row'): for vindex, value in enumerate(row.findall('v')): if value.text == 'NaN': result[vindex]['data'].append({'x': timestamp, 'y': 0}) else: result[vindex]['data'].append({ 'x': timestamp, 'y': float(value.text) }) timestamp += step return result
def _learn(self, rcpt, msg, mtype): """Internal method to call the learning command.""" if self._username is None: if self._recipient_db == "global": username = self._default_username else: mbox = self._get_mailbox_from_rcpt(rcpt) if mbox is None: username = self._default_username if self._recipient_db == "domain": username = mbox.domain.name if username not in self._setup_cache and \ setup_manual_learning_for_domain(mbox.domain): self._setup_cache[username] = True else: username = mbox.full_address if username not in self._setup_cache and \ setup_manual_learning_for_mbox(mbox): self._setup_cache[username] = True else: username = self._username if username not in self._username_cache: self._username_cache.append(username) cmd = self._learn_cmd.format(mtype, username) code, output = exec_cmd(cmd, pinput=msg, **self._learn_cmd_kwargs) if code in self._expected_exit_codes: return True self.error = output return False
def mail_home(self): """Retrieve the home directory of this mailbox. The home directory refers to the place on the file system where the mailbox data is stored. We ask dovecot to give us this information because there are several patterns to understand and we don't want to implement them. """ hm = parameters.get_admin("HANDLE_MAILBOXES", raise_error=False) if hm is None or hm == "no": return None if self.__mail_home is None: curuser = pwd.getpwuid(os.getuid()).pw_name mbowner = parameters.get_admin("MAILBOXES_OWNER") options = {} if curuser != mbowner: options['sudo_user'] = mbowner code, output = exec_cmd( "doveadm user %s -f home" % self.full_address, **options) if code: raise lib_exceptions.InternalError( _("Failed to retrieve mailbox location (%s)" % output)) self.__mail_home = output.strip() return self.__mail_home
def migrate_mailboxes(domain, options): print "\tMigrating mailboxes" old_mboxes = pf_models.Mailbox.objects.using( options._from).filter(domain=domain.name) for old_mb in old_mboxes: new_mb = md_models.Mailbox() new_mb.address = old_mb.local_part new_mb.domain = domain new_mb.dates = migrate_dates(old_mb) if old_mb.quota: new_mb.quota = old_mb.quota / 1024000 new_mb.path = "%s/" % old_mb.local_part if options.rename_dirs: oldpath = os.path.join(options.mboxes_path, domain.name, old_mb.maildir) newpath = os.path.join(options.mboxes_path, domain.name, new_mb.path) code, output = exec_cmd("mv %s %s" % (oldpath, newpath)) if code: print "Error: cannot rename mailbox directory\n%s" % output sys.exit(1) new_mb.save(name=old_mb.name, password=old_mb.password, using=options.to)
def mail_home(self): """Retrieve the home directory of this mailbox. The home directory refers to the place on the file system where the mailbox data is stored. We ask dovecot to give us this information because there are several patterns to understand and we don't want to implement them. """ admin_params = dict(param_tools.get_global_parameters("admin")) if not admin_params.get("handle_mailboxes"): return None if self.__mail_home is None: curuser = pwd.getpwuid(os.getuid()).pw_name mbowner = admin_params["mailboxes_owner"] options = {} if curuser != mbowner: options["sudo_user"] = mbowner code, output = exec_cmd( "doveadm user %s -f home" % self.full_address, **options ) if code: raise lib_exceptions.InternalError( _(u"Failed to retrieve mailbox location (%s)") % output) self.__mail_home = output.strip() return self.__mail_home
def export(self, rrdfile, start, end): """ """ result = [] cmdargs = [] for curve in self._curves: result += [{ "name": curve.legend, "color": curve.color, "data": [] }] cmdargs += curve.to_rrd_command_args(rrdfile) cmd = "rrdtool xport --start %s --end %s " % (str(start), str(end)) cmd += " ".join(cmdargs) code, output = exec_cmd(cmd) if code: return [] tree = etree.fromstring(output) for row in tree.xpath('/xport/data/row'): timestamp = int(row.find('t').text) for vindex, value in enumerate(row.findall('v')): if value.text == 'NaN': result[vindex]['data'].append({'x': timestamp, 'y': 0}) else: result[vindex]['data'].append( {'x': timestamp, 'y': float(value.text)} ) return result
def mail_home(self): """Retrieve the home directory of this mailbox. The home directory refers to the place on the file system where the mailbox data is stored. We ask dovecot to give us this information because there are several patterns to understand and we don't want to implement them. """ hm = parameters.get_admin("HANDLE_MAILBOXES", raise_error=False) if hm is None or hm == "no": return None if self.__mail_home is None: curuser = pwd.getpwuid(os.getuid()).pw_name mbowner = parameters.get_admin("MAILBOXES_OWNER") options = {} if curuser != mbowner: options['sudo_user'] = mbowner code, output = exec_cmd( "doveadm user %s -f home" % self.full_address, **options ) if code: raise lib_exceptions.InternalError( _("Failed to retrieve mailbox location (%s)" % output)) self.__mail_home = output.strip() return self.__mail_home
def export(self, rrdfile, start, end): """Export data to XML using rrdtool and convert it to JSON.""" result = [] cmdargs = [] for curve in self._curves: result += [{ "name": curve.legend, "color": curve.color, "data": [] }] cmdargs += curve.to_rrd_command_args(rrdfile) code = 0 for extra_args in [" --showtime", ""]: cmd = "{} xport --start {} --end {}{} ".format( self.rrdtool_binary, str(start), str(end), extra_args) cmd += " ".join(cmdargs) if isinstance(cmd, unicode): cmd = cmd.encode("utf-8") code, output = exec_cmd(cmd) if code: continue if code: return [] tree = etree.fromstring(output) for row in tree.xpath('/xport/data/row'): timestamp = int(row.find('t').text) for vindex, value in enumerate(row.findall('v')): if value.text == 'NaN': result[vindex]['data'].append({'x': timestamp, 'y': 0}) else: result[vindex]['data'].append( {'x': timestamp, 'y': float(value.text)} ) return result
def export(self, rrdfile, start, end): """ """ result = [] cmdargs = [] for curve in self._curves: result += [{ "name": curve.legend, "color": curve.color, "data": [] }] cmdargs += curve.to_rrd_command_args(rrdfile) cmd = "rrdtool xport --start %s --end %s " % (str(start), str(end)) cmd += " ".join(cmdargs) code, output = exec_cmd(cmd) if code: return [] tree = etree.fromstring(output) for row in tree.xpath('/xport/data/row'): timestamp = int(row.find('t').text) for vindex, value in enumerate(row.findall('v')): if value.text == 'NaN': result[vindex]['data'].append({'x': timestamp, 'y': 0}) else: result[vindex]['data'].append({ 'x': timestamp, 'y': float(value.text) }) return result
def mail_home(self): """Retrieve the home directory of this mailbox. The home directory refers to the place on the file system where the mailbox data is stored. We ask dovecot to give us this information because there are several patterns to understand and we don't want to implement them. """ admin_params = dict(param_tools.get_global_parameters("admin")) if not admin_params.get("handle_mailboxes"): return None if self.__mail_home is None: curuser = pwd.getpwuid(os.getuid()).pw_name mbowner = admin_params["mailboxes_owner"] options = {} if curuser != mbowner: options["sudo_user"] = mbowner code, output = exec_cmd( "doveadm user -f home %s" % self.full_address, **options) if code: raise lib_exceptions.InternalError( _("Failed to retrieve mailbox location (%s)") % output) self.__mail_home = output.strip() return self.__mail_home
def export(self, rrdfile, start, end): """Export data to JSON using rrdtool.""" result = [] cmdargs = [] for curve in self._curves: result += [{ "name": str(curve.legend), "backgroundColor": curve.color, "data": [] }] cmdargs += curve.to_rrd_command_args(rrdfile) code = 0 cmd = "{} xport --json -t --start {} --end {} ".format( self.rrdtool_binary, str(start), str(end)) cmd += " ".join(cmdargs) code, output = exec_cmd(smart_bytes(cmd)) if code: return [] xport = json.loads(output) for row in xport['data']: timestamp = int(row[0]) date = datetime.datetime.fromtimestamp(timestamp).isoformat( sep=' ') for (vindex, value) in enumerate(row[1:]): result[vindex]['data'].append({ 'x': date, 'y': value, 'timestamp': timestamp }) return result
def tearDown(self): path = os.path.join(self.workdir, self.projname) code, output = exec_cmd( "python manage.py test core lib admin limits postfix_relay_domains radicale", capture_output=False, cwd=path ) self.assertEqual(code, 0)
def test_silent(self): dburl = "%s://%s:%s@%s/%s" \ % (self.dbtype, self.dbuser, self.dbpassword, self.dbhost, self.projname) cmd = "modoboa-admin.py deploy --syncdb --collectstatic --dburl %s --domain %s %s" \ % (dburl, 'localhost', self.projname) code, output = exec_cmd(cmd, cwd=self.workdir) self.assertEqual(code, 0)
def delete_mailbox(self, operation): if not os.path.exists(operation.argument): return code, output = exec_cmd( "rm -r %s" % operation.argument ) if code: raise OperationError(output)
def __init__(self, *args, **kwargs): super(GeneralParametersForm, self).__init__(*args, **kwargs) self.fields["default_top_redirection"].choices = enabled_applications() hide_fields = False try: code, version = exec_cmd("dovecot --version") except OSError, e: hide_fields = True
def tearDown(self): path = os.path.join(self.workdir, self.projname) core_apps = ["modoboa.core", "modoboa.lib"] cmd = "python manage.py test {0}".format( " ".join(core_apps), ) code, output = exec_cmd(cmd, capture_output=False, cwd=path) self.assertEqual(code, 0)
def test_silent(self): dburl = "default:%s://%s:%s@%s:%s/%s" \ % (self.dbtype, self.dbuser, self.dbpassword, self.dbhost, self.dbport, self.projname) cmd = ("modoboa-admin.py deploy --collectstatic " "--dburl %s --domain %s --admin-username admin %s" % (dburl, "localhost", self.projname)) code, output = exec_cmd(cmd, cwd=self.workdir) self.assertEqual(code, 0)
def __init__(self, *args, **kwargs): super(AdminParametersForm, self).__init__(*args, **kwargs) hide_fields = False code, output = exec_cmd("which dovecot") if not code: dpath = output.strip() try: code, version = exec_cmd("%s --version" % dpath) except OSError: hide_fields = True else: if code or not version.strip().startswith("2"): hide_fields = True else: hide_fields = True if hide_fields: del self.fields["handle_mailboxes"] del self.fields["mailboxes_owner"]
def validate_dkim_keys_storage_dir(self, value): """Check that directory exists.""" if value: if not os.path.isdir(value): raise serializers.ValidationError(_("Directory not found.")) code, output = exec_cmd("which openssl") if code: raise serializers.ValidationError( _("openssl not found, please make sure it is installed.")) return value
def delete_dir(self): hm = parameters.get_admin("HANDLE_MAILBOXES", raise_error=False) if hm is None or hm == "no": return if not os.path.exists(self.mail_home): return code, output = exec_cmd( "rm -r %s" % self.mail_home, sudo_user=parameters.get_admin("MAILBOXES_OWNER")) if code: raise AdminError(_("Failed to remove mailbox: %s" % output))
def test_silent(self): dburl = "default:%s://%s:%s@%s/%s" \ % (self.dbtype, self.dbuser, self.dbpassword, self.dbhost, self.projname) cmd = ( "modoboa-admin.py deploy --collectstatic " "--dburl %s --domain %s --admin-username admin %s" % (dburl, "localhost", self.projname) ) code, output = exec_cmd(cmd, cwd=self.workdir) self.assertEqual(code, 0)
def _find_binary(self, name): """Find path to binary.""" code, output = exec_cmd("which {}".format(name)) if not code: return smart_text(output).strip() known_paths = getattr(settings, "SA_LOOKUP_PATH", ("/usr/bin", )) for path in known_paths: bpath = os.path.join(path, name) if os.path.isfile(bpath) and os.access(bpath, os.X_OK): return bpath raise InternalError(_("Failed to find {} binary").format(name))
def rename_dir(self, old_mail_home): hm = parameters.get_admin("HANDLE_MAILBOXES", raise_error=False) if hm is None or hm == "no": return self.__mail_home = None if not os.path.exists(old_mail_home): return code, output = exec_cmd( "mv %s %s" % (old_mail_home, self.mail_home), sudo_user=parameters.get_admin("MAILBOXES_OWNER")) if code: raise AdminError(_("Failed to rename mailbox: %s" % output))
def delete_dir(self): hm = parameters.get_admin("HANDLE_MAILBOXES", raise_error=False) if hm is None or hm == "no": return if not os.path.exists(self.mail_home): return code, output = exec_cmd( "rm -r %s" % self.mail_home, sudo_user=parameters.get_admin("MAILBOXES_OWNER") ) if code: raise AdminError(_("Failed to remove mailbox: %s" % output))
def rename_dir(self, old_mail_home): hm = parameters.get_admin("HANDLE_MAILBOXES", raise_error=False) if hm is None or hm == "no": return self.__mail_home = None if not os.path.exists(old_mail_home): return code, output = exec_cmd( "mv %s %s" % (old_mail_home, self.mail_home), sudo_user=parameters.get_admin("MAILBOXES_OWNER") ) if code: raise AdminError(_("Failed to rename mailbox: %s" % output))
def __init__(self, *args, **kwargs): super(AdminParametersForm, self).__init__(*args, **kwargs) hide_fields = False try: code, version = exec_cmd("dovecot --version") except OSError: hide_fields = True else: if code or not version.strip().startswith("2"): hide_fields = True if hide_fields: del self.fields["handle_mailboxes"] del self.fields["mailboxes_owner"]
def tearDown(self): path = os.path.join(self.workdir, self.projname) core_apps = ["modoboa.core", "modoboa.lib"] extensions = [ "admin", "limits", "postfix_relay_domains", "radicale", "postfix_autoreply" ] cmd = "python manage.py test {0} {1}".format( " ".join(core_apps), " ".join(["modoboa.extensions.{0}".format(extension) for extension in extensions]) ) code, output = exec_cmd(cmd, capture_output=False, cwd=path) self.assertEqual(code, 0)
def clean_dkim_keys_storage_dir(self): """Check that directory exists.""" storage_dir = self.cleaned_data.get("dkim_keys_storage_dir", "") if storage_dir: if not os.path.isdir(storage_dir): raise forms.ValidationError( ugettext_lazy("Directory not found.")) code, output = exec_cmd("which openssl") if code: raise forms.ValidationError( ugettext_lazy( "openssl not found, please make sure it is installed.") ) return storage_dir
def mail_home(self): """ """ hm = parameters.get_admin("HANDLE_MAILBOXES", raise_error=False) if hm is None or hm == "no": return None if self.__mail_home is None: code, output = exec_cmd("doveadm user %s -f home" % self.full_address, sudo_user=parameters.get_admin("MAILBOXES_OWNER")) if code: raise AdminError(_("Failed to retrieve mailbox location (%s)" % output)) self.__mail_home = output.strip() return self.__mail_home
def rename_mailbox(self, operation): if not os.path.exists(operation.argument): return new_mail_home = operation.mailbox.mail_home dirname = os.path.dirname(new_mail_home) if not os.path.exists(dirname): try: os.makedirs(dirname) except os.error as e: raise OperationError(str(e)) code, output = exec_cmd("mv %s %s" % (operation.argument, new_mail_home)) if code: raise OperationError(output)
def tearDown(self): path = os.path.join(self.workdir, self.projname) core_apps = ["modoboa.core", "modoboa.lib"] extensions = [ "admin", "limits", "postfix_relay_domains", "radicale", "postfix_autoreply" ] cmd = "python manage.py test {0} {1}".format( " ".join(core_apps), " ".join([ "modoboa.extensions.{0}".format(extension) for extension in extensions ])) code, output = exec_cmd(cmd, capture_output=False, cwd=path) self.assertEqual(code, 0)
def clean_dkim_keys_storage_dir(self): """Check that directory exists.""" storage_dir = self.cleaned_data.get("dkim_keys_storage_dir", "") if storage_dir: if not os.path.isdir(storage_dir): raise forms.ValidationError( ugettext_lazy("Directory not found.") ) code, output = exec_cmd("which openssl") if code: raise forms.ValidationError( ugettext_lazy( "openssl not found, please make sure it is installed.") ) return storage_dir
def rename_mailbox(self, operation): if not os.path.exists(operation.argument): return new_mail_home = operation.mailbox.mail_home dirname = os.path.dirname(new_mail_home) if not os.path.exists(dirname): try: os.makedirs(dirname) except os.error as e: raise OperationError(str(e)) code, output = exec_cmd( "mv %s %s" % (operation.argument, new_mail_home) ) if code: raise OperationError(output)
def check_pidfile(self, path): """Check if this command is already running :param str path: path to the file containing the PID :return: a boolean, True means we can go further """ if os.path.exists(path): with open(path) as fp: pid = fp.read().strip() code, output = exec_cmd( "grep handle_mailbox_operations /proc/%s/cmdline" % pid) if not code: return False with open(path, 'w') as fp: print(os.getpid(), file=fp) return True
def check_pidfile(self, path): """Check if this command is already running :param str path: path to the file containing the PID :return: a boolean, True means we can go further """ if os.path.exists(path): with open(path) as fp: pid = fp.read().strip() code, output = exec_cmd( "grep handle_mailbox_operations /proc/%s/cmdline" % pid ) if not code: return False with open(path, 'w') as fp: print >> fp, os.getpid() return True
def rrdtool_binary(self): """Return path to rrdtool binary.""" dpath = None code, output = exec_cmd("which rrdtool") if not code: dpath = output.strip() else: known_paths = getattr( settings, "RRDTOOL_LOOKUP_PATH", ("/usr/bin/rrdtool", "/usr/local/bin/rrdtool")) for fpath in known_paths: if os.path.isfile(fpath) and os.access(fpath, os.X_OK): dpath = fpath if dpath is None: raise exceptions.InternalError( _("Failed to locate rrdtool binary.")) return dpath
def rrdtool_binary(self): """Return path to rrdtool binary.""" dpath = None code, output = exec_cmd("which rrdtool") if not code: dpath = output.strip() else: known_paths = getattr( settings, "RRDTOOL_LOOKUP_PATH", ("/usr/bin/rrdtool", "/usr/local/bin/rrdtool") ) for fpath in known_paths: if os.path.isfile(fpath) and os.access(fpath, os.X_OK): dpath = fpath if dpath is None: raise exceptions.InternalError( _("Failed to locate rrdtool binary.")) return dpath
def _learn(self, rcpt, msg, mtype): """Internal method to call the learning command.""" if self._username is None: if self._recipient_db == "global": username = self._default_username elif self._recipient_db == "domain": domain = self._get_domain_from_rcpt(rcpt) username = domain.name condition = (username not in self._setup_cache and setup_manual_learning_for_domain(domain)) if condition: self._setup_cache[username] = True else: mbox = self._get_mailbox_from_rcpt(rcpt) if mbox is None: username = self._default_username else: if isinstance(mbox, admin_models.Mailbox): username = mbox.full_address elif isinstance(mbox, admin_models.AliasRecipient): username = mbox.address else: username = None condition = (username is not None and username not in self._setup_cache and setup_manual_learning_for_mbox(mbox)) if condition: self._setup_cache[username] = True else: username = self._username if username not in self._setup_cache: mbox = self._get_mailbox_from_rcpt(username) if mbox and setup_manual_learning_for_mbox(mbox): self._setup_cache[username] = True if username not in self._username_cache: self._username_cache.append(username) cmd = self._learn_cmd.format(mtype, username) code, output = exec_cmd(cmd, pinput=smart_bytes(msg), **self._learn_cmd_kwargs) if code in self._expected_exit_codes: return True self.error = smart_text(output) return False
def migrate_mailboxes(domain, options): print "\tMigrating mailboxes" old_mboxes = pf_models.Mailbox.objects.using(options._from).filter(domain=domain.name) for old_mb in old_mboxes: new_mb = md_models.Mailbox() new_mb.address = old_mb.local_part new_mb.domain = domain new_mb.dates = migrate_dates(old_mb) if old_mb.quota: new_mb.quota = old_mb.quota / 1024000 new_mb.path = "%s/" % old_mb.local_part if options.rename_dirs: oldpath = os.path.join(options.mboxes_path, domain.name, old_mb.maildir) newpath = os.path.join(options.mboxes_path, domain.name, new_mb.path) code, output = exec_cmd("mv %s %s" % (oldpath, newpath)) if code: print "Error: cannot rename mailbox directory\n%s" % output sys.exit(1) new_mb.save(name=old_mb.name, password=old_mb.password, using=options.to)
def _test_maps_generation(self, engine, categories=None): if categories is None: categories = ["std", "autoreply", "relaydomains"] dburl = "{0}://user:password@localhost/testdb".format(engine) code, output = exec_cmd( "modoboa-admin.py postfix_maps --categories {0} --dburl {1} {2}".format( " ".join(categories), dburl, self.workdir ) ) self.assertEqual(code, 0) for category in categories: for mapfile in self.MAP_FILES[category]: path = "{0}/{1}".format(self.workdir, mapfile) self.assertTrue(os.path.exists(path)) with open(path) as fpo: content = fpo.read() if engine != "sqlite": self.assertIn("user = user", content) self.assertIn("password = password", content) self.assertIn("dbname = testdb", content) self.assertIn("hosts = localhost", content) else: self.assertIn("dbpath = testdb", content)
def done(self): """Call this method at the end of the processing.""" if self._sa_is_local: for username in self._username_cache: exec_cmd("sa-learn -u {0} --sync".format(username), **self._learn_cmd_kwargs)