moulinette/yunohost_user.py

297 lines
10 KiB
Python
Raw Normal View History

2012-10-06 17:10:19 +02:00
# -*- coding: utf-8 -*-
2013-07-06 09:42:26 +02:00
""" License
Copyright (C) 2013 YunoHost
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program; if not, see http://www.gnu.org/licenses
"""
""" yunohost_user.py
2013-07-06 10:17:16 +02:00
Manage users
2013-07-06 09:42:26 +02:00
"""
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-11-08 19:20:13 +01:00
from yunohost import YunoHostError, YunoHostLDAP, win_msg, colorize, validate, get_required_args
from yunohost_domain import domain_list
2012-10-07 16:20:20 +02:00
def user_list(fields=None, filter=None, limit=None, offset=None):
2012-10-28 17:27:47 +01:00
"""
2013-07-06 10:17:16 +02:00
List users
2012-10-28 17:27:47 +01:00
Keyword argument:
2013-07-06 10:17:16 +02:00
fields -- fields to fetch
offset -- Starting number for user fetching
filter -- LDAP filter used to search
2013-07-06 12:59:06 +02:00
limit -- Maximum number of user fetched
2012-10-28 17:27:47 +01:00
"""
2012-11-08 19:20:13 +01:00
with YunoHostLDAP() as yldap:
2013-06-08 09:50:43 +02:00
user_attrs = ['uid', 'mail', 'cn', 'maildrop']
2012-11-08 19:20:13 +01:00
attrs = []
2013-03-15 18:17:19 +01:00
result_list = []
if offset: offset = int(offset)
2012-11-08 19:20:13 +01:00
else: offset = 0
if limit: limit = int(limit)
2012-11-08 19:20:13 +01:00
else: limit = 1000
if not filter: filter = 'uid=*'
if fields:
2012-12-01 15:33:56 +01:00
for attr in fields.items():
2012-11-08 19:20:13 +01:00
if attr in user_attrs:
attrs.append(attr)
continue
else:
2012-12-01 15:33:56 +01:00
raise YunoHostError(22, _("Invalid field : ") + attr)
2012-11-08 19:20:13 +01:00
else:
attrs = user_attrs
2012-10-28 17:27:47 +01:00
2012-11-08 19:20:13 +01:00
result = yldap.search('ou=users,dc=yunohost,dc=org', filter, attrs)
2012-12-01 15:33:56 +01:00
2012-11-08 19:20:13 +01:00
if result and len(result) > (0 + offset) and limit > 0:
i = 0 + offset
for user in result[i:]:
2013-03-15 18:17:19 +01:00
if i - offset < limit:
2012-11-08 19:20:13 +01:00
entry = {
2013-03-13 13:13:48 +01:00
'Username': user['uid'][0],
'Fullname': user['cn'][0],
2012-11-08 19:20:13 +01:00
'Mail': user['mail'][0]
}
if len(user['mail']) > 1:
2013-06-08 09:50:43 +02:00
entry['Mail Aliases'] = user['mail'][1:]
if 'maildrop' in user:
entry['Mail Forward'] = user['maildrop']
2012-12-01 15:33:56 +01:00
2013-03-15 19:12:48 +01:00
result_list.append(entry)
2012-11-08 19:20:13 +01:00
i += 1
else:
raise YunoHostError(167, _("No user found"))
2012-10-28 17:27:47 +01:00
2013-03-15 18:17:19 +01:00
return { 'Users' : result_list }
2012-10-07 17:57:57 +02:00
def user_create(username, firstname, lastname, mail, password):
2012-10-07 23:51:40 +02:00
"""
2013-07-06 10:17:16 +02:00
Create user
2012-10-07 23:51:40 +02:00
Keyword argument:
2013-07-06 12:59:06 +02:00
username -- Must be unique
lastname
firstname
2013-07-06 12:59:06 +02:00
password
2013-07-06 10:17:16 +02:00
mail -- Main mail address must be unique
2012-10-07 23:51:40 +02:00
"""
2012-11-08 19:20:13 +01:00
with YunoHostLDAP() as yldap:
# Validate password length
if len(password) < 4:
2012-11-08 19:20:13 +01:00
raise YunoHostError(22, _("Password is too short"))
yldap.validate_uniqueness({
'uid' : username,
2013-06-08 09:50:43 +02:00
'mail' : mail
2012-11-08 19:20:13 +01:00
})
2013-06-08 10:35:26 +02:00
if mail[mail.find('@')+1:] not in domain_list()['Domains']:
raise YunoHostError(22, _("Domain not found : ")+ mail[mail.find('@')+1:])
2012-11-08 19:20:13 +01:00
2013-07-06 13:15:59 +02:00
user_added = os.system('/usr/sbin/smbldap-useradd -a -A 1 -m -M "'+ mail +'" -N "'+ firstname +'" -S "'+ lastname +'" -Z "objectclass=mailAccount,maildrop='+ username +'" -p '+ username)
2013-07-06 12:57:47 +02:00
if user_added == 0:
char_set = string.ascii_uppercase + string.digits
salt = ''.join(random.sample(char_set,8))
salt = '$1$' + salt + '$'
2013-07-06 13:15:59 +02:00
attr_dict = {'userPassword': '{CRYPT}' + crypt.crypt(str(password), salt)}
2013-07-06 12:57:47 +02:00
if yldap.update('uid=' + username + ',ou=users', attr_dict):
#TODO: Send a welcome mail to user
win_msg(_("User successfully created"))
return { _("Fullname") : fullname, _("Username") : username, _("Mail") : mail }
2012-11-08 19:20:13 +01:00
else:
raise YunoHostError(169, _("An error occured during user creation"))
def user_delete(users, purge=None):
2012-10-29 12:35:29 +01:00
"""
2013-07-06 10:17:16 +02:00
Delete user
2012-10-29 12:35:29 +01:00
Keyword argument:
2013-07-06 10:17:16 +02:00
users -- Username of users to delete
purge
2012-10-29 12:35:29 +01:00
"""
2012-11-08 19:20:13 +01:00
with YunoHostLDAP() as yldap:
result = { 'Users' : [] }
if not isinstance(users, list):
users = [ users ]
2012-11-08 19:20:13 +01:00
for user in users:
2012-11-08 19:20:13 +01:00
if yldap.remove('uid=' + user+ ',ou=users'):
if purge:
2012-11-08 19:20:13 +01:00
os.system('rm -rf /home/' + user)
result['Users'].append(user)
continue
else:
raise YunoHostError(169, _("An error occured during user deletion"))
2012-10-29 12:35:29 +01:00
2012-11-08 19:20:13 +01:00
win_msg(_("User(s) successfully deleted"))
2012-12-01 15:33:56 +01:00
return result
2013-07-06 13:15:59 +02:00
def user_update(username, firstname=None, lastname=None, mail=None, change_password=None, add_mailforward=None, remove_mailforward=None, add_mailalias=None, remove_mailalias=None):
2012-10-29 15:43:43 +01:00
"""
Update user informations
Keyword argument:
2013-07-06 10:17:16 +02:00
username -- Username of user to update
firstname
2013-07-06 10:17:16 +02:00
add_mailalias -- Mail aliases to add
mail
2013-07-06 10:17:16 +02:00
lastname
2013-07-06 12:59:06 +02:00
change_password -- New password to set
remove_mailalias -- Mail aliases to remove
add_mailforward -- Mailforward addresses to add
remove_mailforward -- Mailforward addresses to remove
2013-07-06 10:17:16 +02:00
2012-10-29 15:43:43 +01:00
"""
2012-11-08 19:20:13 +01:00
with YunoHostLDAP() as yldap:
2013-06-08 09:50:43 +02:00
attrs_to_fetch = ['givenName', 'sn', 'mail', 'maildrop']
2012-11-08 19:20:13 +01:00
new_attr_dict = {}
2013-06-08 19:46:15 +02:00
domains = domain_list()['Domains']
2012-11-08 19:20:13 +01:00
# Populate user informations
result = yldap.search(base='ou=users,dc=yunohost,dc=org', filter='uid=' + username, attrs=attrs_to_fetch)
2012-11-08 19:20:13 +01:00
if not result:
raise YunoHostError(167, _("No user found"))
user = result[0]
# Get modifications from arguments
if firstname:
new_attr_dict['givenName'] = firstname # TODO: Validate
new_attr_dict['cn'] = new_attr_dict['displayName'] = firstname + ' ' + user['sn'][0]
2012-11-08 19:20:13 +01:00
if lastname:
new_attr_dict['sn'] = lastname # TODO: Validate
new_attr_dict['cn'] = new_attr_dict['displayName'] = user['givenName'][0] + ' ' + lastname
2012-11-08 19:20:13 +01:00
if lastname and firstname:
new_attr_dict['cn'] = new_attr_dict['displayName'] = firstname + ' ' + lastname
2012-11-08 19:20:13 +01:00
if change_password:
2012-11-08 19:20:13 +01:00
char_set = string.ascii_uppercase + string.digits
salt = ''.join(random.sample(char_set,8))
salt = '$1$' + salt + '$'
new_attr_dict['userPassword'] = '{CRYPT}' + crypt.crypt(str(change_password), salt)
2012-11-08 19:20:13 +01:00
if mail:
2013-06-08 09:50:43 +02:00
yldap.validate_uniqueness({ 'mail': mail })
2013-06-08 10:35:26 +02:00
if mail[mail.find('@')+1:] not in domains:
raise YunoHostError(22, _("Domain not found : ")+ mail[mail.find('@')+1:])
2012-11-08 19:20:13 +01:00
del user['mail'][0]
new_attr_dict['mail'] = [mail] + user['mail']
2012-11-08 19:20:13 +01:00
if add_mailalias:
if not isinstance(add_mailalias, list):
add_mailalias = [ add_mailalias ]
for mail in add_mailalias:
2013-06-08 09:50:43 +02:00
yldap.validate_uniqueness({ 'mail': mail })
2013-06-08 10:35:26 +02:00
if mail[mail.find('@')+1:] not in domains:
raise YunoHostError(22, _("Domain not found : ")+ mail[mail.find('@')+1:])
2013-06-08 09:50:43 +02:00
user['mail'].append(mail)
new_attr_dict['mail'] = user['mail']
2012-11-08 19:20:13 +01:00
if remove_mailalias:
if not isinstance(remove_mailalias, list):
remove_mailalias = [ remove_mailalias ]
for mail in remove_mailalias:
2013-06-08 09:50:43 +02:00
if len(user['mail']) > 1 and mail in user['mail'][1:]:
user['mail'].remove(mail)
2012-11-08 19:20:13 +01:00
else:
2012-12-01 15:33:56 +01:00
raise YunoHostError(22, _("Invalid mail alias : ") + mail)
2013-06-08 09:50:43 +02:00
new_attr_dict['mail'] = user['mail']
if add_mailforward:
if not isinstance(add_mailforward, list):
add_mailforward = [ add_mailforward ]
for mail in add_mailforward:
if mail in user['maildrop'][1:]:
continue
user['maildrop'].append(mail)
new_attr_dict['maildrop'] = user['maildrop']
if remove_mailforward:
if not isinstance(remove_mailforward, list):
remove_mailforward = [ remove_mailforward ]
for mail in remove_mailforward:
if len(user['maildrop']) > 1 and mail in user['maildrop'][1:]:
user['maildrop'].remove(mail)
else:
raise YunoHostError(22, _("Invalid mail forward : ") + mail)
new_attr_dict['maildrop'] = user['maildrop']
2012-11-08 19:20:13 +01:00
if yldap.update('uid=' + username + ',ou=users', new_attr_dict):
2012-11-08 19:20:13 +01:00
win_msg(_("User successfully updated"))
2012-12-01 15:33:56 +01:00
return user_info(username)
2012-11-08 19:20:13 +01:00
else:
raise YunoHostError(169, _("An error occured during user update"))
2012-10-29 15:43:43 +01:00
2012-12-01 15:33:56 +01:00
2013-06-30 18:13:26 +02:00
def user_info(username):
"""
2013-07-06 10:17:16 +02:00
Get user informations
Keyword argument:
2013-07-06 10:17:16 +02:00
username -- Username or mail to get informations
"""
2012-11-08 19:20:13 +01:00
with YunoHostLDAP() as yldap:
2013-06-08 09:50:43 +02:00
user_attrs = ['cn', 'mail', 'uid', 'maildrop']
2012-11-08 19:20:13 +01:00
2013-06-30 18:13:26 +02:00
if len(username.split('@')) is 2:
filter = 'mail='+ username
2012-11-08 19:20:13 +01:00
else:
2013-06-30 18:13:26 +02:00
filter = 'uid='+ username
2012-11-08 19:20:13 +01:00
result = yldap.search('ou=users,dc=yunohost,dc=org', filter, user_attrs)
2012-12-01 15:33:56 +01:00
if result:
user = result[0]
else:
2013-06-30 18:13:26 +02:00
raise YunoHostError(22, _("Unknown user/mail : ") + username)
2012-12-01 15:33:56 +01:00
2012-11-08 19:20:13 +01:00
result_dict = {
2013-05-25 16:52:10 +02:00
'Username': user['uid'][0],
'Fullname': user['cn'][0],
2012-11-08 19:20:13 +01:00
'Mail': user['mail'][0]
}
if len(user['mail']) > 1:
2013-06-08 09:50:43 +02:00
result_dict['Mail Aliases'] = user['mail'][1:]
2012-11-08 19:20:13 +01:00
2013-06-08 09:50:43 +02:00
if len(user['maildrop']) > 1:
result_dict['Mail Forward'] = user['maildrop'][1:]
2012-11-08 19:20:13 +01:00
if result:
return result_dict
else:
raise YunoHostError(167, _("No user found"))
2012-12-01 15:33:56 +01:00