Modify no-ldap option

This commit is contained in:
Kload 2013-10-24 16:49:40 +00:00
parent 15f85746cd
commit 2c93b811c7
2 changed files with 35 additions and 25 deletions

View file

@ -64,7 +64,7 @@ def main():
admin_password_provided = False
json_print = False
no_ldap = False
write_ldap = True
for key, arg in enumerate(sys.argv):
if arg == '--admin-password':
@ -72,11 +72,9 @@ def main():
admin_password = sys.argv[key+1]
sys.argv.pop(key)
sys.argv.pop(key)
if arg == '--no-ldap':
no_ldap = True
write_ldap = False
sys.argv.pop(key)
if arg == '--json':
json_print = True
sys.argv.pop(key)
@ -104,7 +102,8 @@ def main():
if admin_password_provided:
with YunoHostLDAP(password=admin_password):
result = args.func(**args_dict)
elif os.isatty(1) and not no_ldap:
elif os.isatty(1) and write_ldap:
print('meh')
admin_password = getpass.getpass(colorize(_('Admin Password: '), 'yellow'))
with YunoHostLDAP(password=admin_password):
try:
@ -125,7 +124,8 @@ def main():
os.remove('/etc/yunohost/passwd')
os.remove('/var/run/yunohost.pid')
else:
result = args.func(**args_dict)
with YunoHostLDAP(anonymous=True):
result = args.func(**args_dict)
#except TypeError, error:
#if not __debug__ :
#traceback.print_exc()

View file

@ -251,6 +251,7 @@ class Singleton(object):
class YunoHostLDAP(Singleton):
""" Specific LDAP functions for YunoHost """
pwd = False
connected = False
conn = ldap.initialize('ldap://localhost:389')
base = 'dc=yunohost,dc=org'
level = 0
@ -258,33 +259,42 @@ class YunoHostLDAP(Singleton):
def __enter__(self):
return self
def __init__(self, password=False):
def __init__(self, password=False, anonymous=False):
"""
Connect to LDAP base
Initialize to localhost, base yunohost.org, prompt for password
"""
if password: self.pwd = password
elif self.pwd: pass
if anonymous:
self.conn.simple_bind_s()
self.connected = True
elif self.connected:
pass
else:
need_password = True
while need_password:
try:
self.pwd = getpass.getpass(colorize(_('Admin Password: '), 'yellow'))
self.conn.simple_bind_s('cn=admin,' + self.base, self.pwd)
except KeyboardInterrupt, EOFError:
raise YunoHostError(125, _("Interrupted"))
except ldap.INVALID_CREDENTIALS:
print(_('Invalid password... Try again'))
else:
need_password = False
if password:
self.pwd = password
elif self.pwd:
pass
else:
need_password = True
while need_password:
try:
self.pwd = getpass.getpass(colorize(_('Admin Password: '), 'yellow'))
self.conn.simple_bind_s('cn=admin,' + self.base, self.pwd)
except KeyboardInterrupt, EOFError:
raise YunoHostError(125, _("Interrupted"))
except ldap.INVALID_CREDENTIALS:
print(_('Invalid password... Try again'))
else:
need_password = False
self.level = self.level+1
try:
self.conn.simple_bind_s('cn=admin,' + self.base, self.pwd)
except ldap.INVALID_CREDENTIALS:
raise YunoHostError(13, _('Invalid credentials'))
self.level = self.level+1
try:
self.conn.simple_bind_s('cn=admin,' + self.base, self.pwd)
self.connected = True
except ldap.INVALID_CREDENTIALS:
raise YunoHostError(13, _('Invalid credentials'))
def __exit__(self, type, value, traceback):
self.level = self.level-1