2012-10-06 17:10:19 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2012-10-07 17:57:57 +02:00
|
|
|
import sys
|
2012-10-06 17:57:08 +02:00
|
|
|
import ldap
|
2012-10-07 16:20:20 +02:00
|
|
|
import yunohost_ldap
|
2012-10-07 17:57:57 +02:00
|
|
|
import yunohost_messages as msg
|
2012-10-07 23:51:40 +02:00
|
|
|
import crypt
|
|
|
|
import random
|
|
|
|
import string
|
2012-10-07 17:57:57 +02:00
|
|
|
import getpass
|
2012-10-07 16:20:20 +02:00
|
|
|
|
|
|
|
# Initialize LDAP
|
|
|
|
yldap = yunohost_ldap.YunoHostLDAP()
|
2012-10-06 17:57:08 +02:00
|
|
|
|
2012-10-07 17:57:57 +02:00
|
|
|
def user_list(args): # TODO : fix
|
|
|
|
result = yldap.search()
|
2012-10-07 16:20:20 +02:00
|
|
|
print(result)
|
2012-10-07 17:57:57 +02:00
|
|
|
|
|
|
|
|
|
|
|
def user_add(args):
|
2012-10-07 23:51:40 +02:00
|
|
|
"""
|
|
|
|
Add user to LDAP
|
|
|
|
|
|
|
|
Keyword argument:
|
|
|
|
args -- Dictionnary of values (can be empty)
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Boolean
|
|
|
|
"""
|
2012-10-07 17:57:57 +02:00
|
|
|
required_args = ['username', 'mail', 'firstname', 'lastname']
|
|
|
|
|
2012-10-07 23:51:40 +02:00
|
|
|
# Input missing values
|
2012-10-07 17:57:57 +02:00
|
|
|
try:
|
|
|
|
for arg in required_args:
|
|
|
|
if not args[arg]:
|
|
|
|
args[arg] = raw_input(arg.capitalize()+': ')
|
|
|
|
|
|
|
|
if not args['password']:
|
|
|
|
args['password'] = getpass.getpass()
|
|
|
|
pwd2 = getpass.getpass('Retype password:')
|
|
|
|
if args['password'] != pwd2:
|
|
|
|
print(msg.error + _("Passwords doesn't match"))
|
2012-10-07 23:51:40 +02:00
|
|
|
sys.exit(msg.EINVAL)
|
2012-10-07 17:57:57 +02:00
|
|
|
except KeyboardInterrupt, EOFError:
|
|
|
|
print("\n" + msg.interrupt + _("User not created"))
|
2012-10-07 23:51:40 +02:00
|
|
|
sys.exit(msg.ECANCELED)
|
|
|
|
|
|
|
|
# Manage values
|
|
|
|
fullname = args['firstname'] + ' ' + args['lastname']
|
|
|
|
rdn = 'cn=' + fullname + ',ou=users'
|
|
|
|
char_set = string.ascii_uppercase + string.digits
|
|
|
|
salt = ''.join(random.sample(char_set,8))
|
|
|
|
salt = '$1$' + salt + '$'
|
|
|
|
pwd = "{CRYPT}" + crypt.crypt(str(args['password']), salt)
|
|
|
|
attr_dict = {
|
|
|
|
'objectClass' : ['mailAccount', 'inetOrgPerson'],
|
|
|
|
'givenName' : args['firstname'],
|
|
|
|
'sn' : args['lastname'],
|
|
|
|
'displayName' : fullname,
|
|
|
|
'cn' : fullname,
|
|
|
|
'uid' : args['username'],
|
|
|
|
'mail' : args['mail'],
|
|
|
|
'userPassword' : pwd
|
|
|
|
}
|
2012-10-07 17:57:57 +02:00
|
|
|
|
2012-10-07 23:51:40 +02:00
|
|
|
# Validate values
|
|
|
|
yldap.validate({
|
|
|
|
args['username'] : r'^[a-z0-9_]+$',
|
|
|
|
args['mail'] : r'^[\w.-]+@[\w.-]+\.[a-zA-Z]{2,6}$'
|
|
|
|
})
|
2012-10-07 17:57:57 +02:00
|
|
|
|
2012-10-07 23:51:40 +02:00
|
|
|
yldap.validate_uniqueness({
|
|
|
|
'uid' : args['username'],
|
|
|
|
'cn' : fullname,
|
|
|
|
'mail' : args['mail'],
|
|
|
|
'mailalias' : args['mail']
|
|
|
|
})
|
2012-10-07 17:57:57 +02:00
|
|
|
|
2012-10-07 23:51:40 +02:00
|
|
|
if yldap.add(rdn, attr_dict):
|
|
|
|
print('\n ' + msg.success + _('User successfully created') + '\n')
|
|
|
|
for attr, value in attr_dict.items():
|
|
|
|
if attr != 'objectClass':
|
|
|
|
print('\033[35m\033[1m ' + attr + ': \033[m' + value)
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
print(msg.error + _('An error occured during user creation'))
|
|
|
|
return False
|