mirror of
https://github.com/jwinterm/modoboa.git
synced 2026-10-20 06:08:35 +02:00
End of ticket #26
This commit is contained in:
+9
-41
@@ -1,42 +1,18 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
import popen2
|
||||
import os
|
||||
import sys
|
||||
import time
|
||||
from django.conf import settings
|
||||
from django.template import RequestContext
|
||||
from django.shortcuts import render_to_response
|
||||
from django.template.loader import render_to_string
|
||||
from mailng.lib import parameters
|
||||
|
||||
def exec_pipe(cmd):
|
||||
"""Exécute une commande et récupère la sortie générée
|
||||
def exec_cmd(cmd):
|
||||
import subprocess
|
||||
|
||||
Le module popen2 est utilisé pour l'exécution de la commande. Un
|
||||
objet Popen4 est créé car il regroupe la sortie standard et la sortie
|
||||
d'erreur dans le même fd.
|
||||
De plus, popen2 est le seul à pouvoir capturer retourner le code de
|
||||
sortie de la commande.
|
||||
|
||||
cmd -- commande à exécuter
|
||||
|
||||
Retourne un couple (code de sortie, texte de sortie)
|
||||
"""
|
||||
child = popen2.Popen4(cmd)
|
||||
child.tochild.close()
|
||||
while True:
|
||||
ret = child.poll()
|
||||
if ret != -1:
|
||||
break
|
||||
time.sleep(0.001)
|
||||
output = child.fromchild.read()
|
||||
if not ret:
|
||||
code = 0
|
||||
else:
|
||||
if os.WIFEXITED(ret):
|
||||
code = os.WEXITSTATUS(ret), output
|
||||
else:
|
||||
code = -1
|
||||
return code, output
|
||||
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
|
||||
output = p.communicate()[0]
|
||||
return p.returncode, output
|
||||
|
||||
def _render(request, tpl, user_context):
|
||||
return render_to_response(tpl, user_context,
|
||||
@@ -47,9 +23,10 @@ def _render_error(request, user_context):
|
||||
context_instance=RequestContext(request))
|
||||
|
||||
def exec_as_vuser(cmd):
|
||||
code, output = exec_pipe("sudo -u %s %s" % (settings.VIRTUAL_UID, cmd))
|
||||
code, output = exec_cmd("sudo -u %s %s" \
|
||||
% (parameters.get("admin", "VIRTUAL_UID"), cmd))
|
||||
if code:
|
||||
os.system("echo '%s' >> /tmp/vmail.log" % output)
|
||||
exec_cmd("echo '%s' >> /tmp/vmail.log" % output)
|
||||
return False
|
||||
return True
|
||||
|
||||
@@ -77,15 +54,6 @@ def decode(s, encodings=('utf8', 'latin1', 'windows-1252', 'ascii')):
|
||||
pass
|
||||
return s.decode('ascii', 'ignore')
|
||||
|
||||
def getoption(name, default=None):
|
||||
res = None
|
||||
try:
|
||||
res = getattr(settings, name)
|
||||
except AttributeError:
|
||||
res = default
|
||||
return res
|
||||
|
||||
|
||||
class Singleton(type):
|
||||
def __init__(cls, name, bases, dict):
|
||||
super(Singleton, cls).__init__(name, bases, dict)
|
||||
|
||||
+3
-10
@@ -1,10 +1,10 @@
|
||||
from django.contrib.auth.models import User
|
||||
from mailng.admin.models import Mailbox
|
||||
from django.conf import settings
|
||||
import hashlib
|
||||
import crypt
|
||||
import string
|
||||
from random import Random
|
||||
from mailng.lib import parameters
|
||||
|
||||
class SimpleBackend:
|
||||
def authenticate(self, username=None, password=None):
|
||||
@@ -26,15 +26,8 @@ class SimpleBackend:
|
||||
except User.DoesNotExist:
|
||||
return None
|
||||
|
||||
def __get_password_scheme():
|
||||
try:
|
||||
scheme = getattr(settings, "PASSWORD_SCHEME")
|
||||
except AttributeError:
|
||||
scheme = "crypt"
|
||||
return scheme
|
||||
|
||||
def check_password(password, crypted):
|
||||
scheme = __get_password_scheme()
|
||||
scheme = parameters.get("admin", "PASSWORD_SCHEME")
|
||||
if scheme == "crypt":
|
||||
return crypt.crypt(password, crypted) == crypted
|
||||
if scheme == "md5":
|
||||
@@ -42,7 +35,7 @@ def check_password(password, crypted):
|
||||
return password
|
||||
|
||||
def crypt_password(password):
|
||||
scheme = __get_password_scheme()
|
||||
scheme = parameters.get("admin", "PASSWORD_SCHEME")
|
||||
if scheme == "crypt":
|
||||
salt = ''.join(Random().sample(string.letters + string.digits, 2))
|
||||
return crypt.crypt(password, salt)
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
from django.db import models
|
||||
|
||||
class Parameter(models.Model):
|
||||
name = models.CharField(max_length=255)
|
||||
value = models.CharField(max_length=255)
|
||||
+6
-4
@@ -1,7 +1,7 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import string
|
||||
from mailng.admin.models import Parameter
|
||||
from mailng.lib.models import Parameter
|
||||
|
||||
"""
|
||||
This interface provides a simple way to declare and store parameters
|
||||
@@ -10,7 +10,7 @@ in MailNG's database.
|
||||
Core components or extensions can register their own parameters, which
|
||||
will be available and modifiable directly from the web interface.
|
||||
|
||||
Only super users will be able to acces this part of the web interface.
|
||||
Only super users will be able to access this part of the web interface.
|
||||
"""
|
||||
|
||||
_params = {}
|
||||
@@ -21,8 +21,8 @@ class NotDefined(Exception):
|
||||
self.name = name
|
||||
|
||||
def __str__(self):
|
||||
return "Application '%s' and/or parameter '%s' not defined" % (self.app,
|
||||
self.name)
|
||||
return "Application '%s' and/or parameter '%s' not defined" \
|
||||
% (self.app, self.name)
|
||||
|
||||
def register(app, name, type="string", deflt=None, help=None, **kwargs):
|
||||
"""Register a new parameter.
|
||||
@@ -32,6 +32,8 @@ def register(app, name, type="string", deflt=None, help=None, **kwargs):
|
||||
if not app in _params.keys():
|
||||
_params[app] = {}
|
||||
_params[app][name] = {"type" : type, "default" : deflt, "help" : help}
|
||||
for k, v in kwargs.iteritems():
|
||||
_params[app][name][k] = v
|
||||
|
||||
def save(app, name, value):
|
||||
if not app in _params.keys() or not name in _params[app].keys():
|
||||
|
||||
Reference in New Issue
Block a user