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

View file

@ -251,6 +251,7 @@ class Singleton(object):
class YunoHostLDAP(Singleton): class YunoHostLDAP(Singleton):
""" Specific LDAP functions for YunoHost """ """ Specific LDAP functions for YunoHost """
pwd = False pwd = False
connected = False
conn = ldap.initialize('ldap://localhost:389') conn = ldap.initialize('ldap://localhost:389')
base = 'dc=yunohost,dc=org' base = 'dc=yunohost,dc=org'
level = 0 level = 0
@ -258,15 +259,23 @@ class YunoHostLDAP(Singleton):
def __enter__(self): def __enter__(self):
return self return self
def __init__(self, password=False): def __init__(self, password=False, anonymous=False):
""" """
Connect to LDAP base Connect to LDAP base
Initialize to localhost, base yunohost.org, prompt for password Initialize to localhost, base yunohost.org, prompt for password
""" """
if password: self.pwd = password if anonymous:
elif self.pwd: pass self.conn.simple_bind_s()
self.connected = True
elif self.connected:
pass
else:
if password:
self.pwd = password
elif self.pwd:
pass
else: else:
need_password = True need_password = True
while need_password: while need_password:
@ -283,6 +292,7 @@ class YunoHostLDAP(Singleton):
self.level = self.level+1 self.level = self.level+1
try: try:
self.conn.simple_bind_s('cn=admin,' + self.base, self.pwd) self.conn.simple_bind_s('cn=admin,' + self.base, self.pwd)
self.connected = True
except ldap.INVALID_CREDENTIALS: except ldap.INVALID_CREDENTIALS:
raise YunoHostError(13, _('Invalid credentials')) raise YunoHostError(13, _('Invalid credentials'))