2012-10-06 17:10:19 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2012-10-08 18:16:43 +02:00
|
|
|
import os
|
2012-10-07 17:57:57 +02:00
|
|
|
import sys
|
2012-10-06 17:57:08 +02:00
|
|
|
import ldap
|
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-10 19:47:57 +02:00
|
|
|
from yunohost import YunoHostError, win_msg, colorize, validate
|
2012-10-07 16:20:20 +02:00
|
|
|
|
2012-10-12 19:00:41 +02:00
|
|
|
def user_list(args, connections): # TODO : fix
|
|
|
|
print(args)
|
2012-10-07 17:57:57 +02:00
|
|
|
|
|
|
|
|
2012-10-12 19:00:41 +02:00
|
|
|
def user_create(args, connections):
|
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-12 19:00:41 +02:00
|
|
|
yldap = connections['ldap']
|
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]:
|
2012-10-08 18:16:43 +02:00
|
|
|
if os.isatty(1):
|
2012-10-10 20:53:42 +02:00
|
|
|
args[arg] = raw_input(colorize(arg.capitalize()+': ', 'cyan'))
|
2012-10-08 18:16:43 +02:00
|
|
|
else:
|
|
|
|
raise Exception
|
|
|
|
# Password
|
2012-10-07 17:57:57 +02:00
|
|
|
if not args['password']:
|
2012-10-08 18:16:43 +02:00
|
|
|
if os.isatty(1):
|
2012-10-10 20:53:42 +02:00
|
|
|
args['password'] = getpass.getpass(colorize('Password: ', 'cyan'))
|
|
|
|
pwd2 = getpass.getpass(colorize('Retype password:', 'cyan'))
|
2012-10-08 18:16:43 +02:00
|
|
|
if args['password'] != pwd2:
|
|
|
|
raise YunoHostError(22, _("Passwords doesn't match"))
|
|
|
|
else:
|
|
|
|
raise YunoHostError(22, _("Missing arguments"))
|
2012-10-07 17:57:57 +02:00
|
|
|
except KeyboardInterrupt, EOFError:
|
2012-10-08 18:16:43 +02:00
|
|
|
raise YunoHostError(125, _("Interrupted, user not created"))
|
2012-10-07 23:51:40 +02:00
|
|
|
|
2012-10-24 17:30:28 +02:00
|
|
|
# Get random UID/GID
|
|
|
|
uid_check = gid_check = 0
|
|
|
|
while uid_check == 0 and gid_check == 0:
|
|
|
|
uid = str(random.randint(200, 99999))
|
|
|
|
uid_check = os.system("getent passwd " + uid)
|
|
|
|
gid_check = os.system("getent group " + uid)
|
|
|
|
|
2012-10-07 23:51:40 +02:00
|
|
|
# Manage values
|
|
|
|
fullname = args['firstname'] + ' ' + args['lastname']
|
2012-10-24 18:36:23 +02:00
|
|
|
rdn = 'uid=' + args['username'] + ',ou=users'
|
2012-10-07 23:51:40 +02:00
|
|
|
char_set = string.ascii_uppercase + string.digits
|
|
|
|
salt = ''.join(random.sample(char_set,8))
|
|
|
|
salt = '$1$' + salt + '$'
|
2012-10-08 18:16:43 +02:00
|
|
|
pwd = '{CRYPT}' + crypt.crypt(str(args['password']), salt)
|
2012-10-07 23:51:40 +02:00
|
|
|
attr_dict = {
|
2012-10-24 17:30:28 +02:00
|
|
|
'objectClass' : ['mailAccount', 'inetOrgPerson', 'posixAccount'],
|
2012-10-07 23:51:40 +02:00
|
|
|
'givenName' : args['firstname'],
|
|
|
|
'sn' : args['lastname'],
|
|
|
|
'displayName' : fullname,
|
|
|
|
'cn' : fullname,
|
|
|
|
'uid' : args['username'],
|
|
|
|
'mail' : args['mail'],
|
2012-10-24 17:30:28 +02:00
|
|
|
'userPassword' : pwd,
|
|
|
|
'gidNumber' : uid,
|
|
|
|
'uidNumber' : uid,
|
|
|
|
'homeDirectory' : '/home/' + args['username'],
|
|
|
|
'loginShell' : '/bin/bash'
|
2012-10-07 23:51:40 +02:00
|
|
|
}
|
2012-10-07 17:57:57 +02:00
|
|
|
|
2012-10-24 18:36:23 +02:00
|
|
|
# Validate password length
|
|
|
|
if len(args['password']) < 4:
|
|
|
|
raise YunoHostError(22, _("Password is too short"))
|
|
|
|
|
|
|
|
# Validate other values TODO: validate other values
|
2012-10-10 19:47:57 +02:00
|
|
|
validate({
|
2012-10-07 23:51:40 +02:00
|
|
|
args['username'] : r'^[a-z0-9_]+$',
|
2012-10-24 18:36:23 +02:00
|
|
|
args['mail'] : r'^[\w.-]+@[\w.-]+\.[a-zA-Z]{2,6}$',
|
2012-10-07 23:51:40 +02:00
|
|
|
})
|
2012-10-07 17:57:57 +02:00
|
|
|
|
2012-10-07 23:51:40 +02:00
|
|
|
yldap.validate_uniqueness({
|
|
|
|
'uid' : args['username'],
|
|
|
|
'mail' : args['mail'],
|
|
|
|
'mailalias' : args['mail']
|
|
|
|
})
|
2012-10-07 17:57:57 +02:00
|
|
|
|
2012-10-24 18:36:23 +02:00
|
|
|
# Check if unix user already exists
|
|
|
|
if not os.system("getent passwd " + args['username']):
|
|
|
|
raise YunoHostError(17, _("Username not available"))
|
|
|
|
|
2012-10-10 20:53:42 +02:00
|
|
|
#TODO: check if mail belongs to a domain
|
|
|
|
|
2012-10-07 23:51:40 +02:00
|
|
|
if yldap.add(rdn, attr_dict):
|
2012-10-24 17:30:28 +02:00
|
|
|
# Create user /home directory by switching user
|
2012-10-24 18:36:23 +02:00
|
|
|
os.system("su - " + args['username'] + " -c ''")
|
2012-10-14 21:48:16 +02:00
|
|
|
#TODO: Send a welcome mail to user
|
2012-10-24 17:30:28 +02:00
|
|
|
win_msg(_("User successfully created"))
|
2012-10-10 20:53:42 +02:00
|
|
|
return { _("Fullname") : fullname, _("Username") : args['username'], _("Mail") : args['mail'] }
|
2012-10-07 23:51:40 +02:00
|
|
|
else:
|
2012-10-24 18:36:23 +02:00
|
|
|
raise YunoHostError(169, _("An error occured during user creation"))
|