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
|
2013-06-08 10:31:52 +02:00
|
|
|
from yunohost_domain import domain_list
|
2012-10-07 16:20:20 +02:00
|
|
|
|
2012-11-29 11:49:41 +01: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 = []
|
2012-11-29 11:49:41 +01:00
|
|
|
if offset: offset = int(offset)
|
2012-11-08 19:20:13 +01:00
|
|
|
else: offset = 0
|
2012-11-29 11:49:41 +01:00
|
|
|
if limit: limit = int(limit)
|
2012-11-08 19:20:13 +01:00
|
|
|
else: limit = 1000
|
2012-11-29 11:49:41 +01:00
|
|
|
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:
|
2013-09-23 10:33:15 +02:00
|
|
|
if user['uid'][0] == 'root' or user['uid'][0] == 'nobody':
|
|
|
|
continue
|
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
|
|
|
}
|
2013-07-24 10:02:42 +02:00
|
|
|
if 'mail' in user.keys():
|
|
|
|
entry['Mail'] = user['mail'][0]
|
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
|
|
|
|
|
|
|
|
2012-11-29 14:45:06 +01: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
|
2012-11-29 11:49:41 +01:00
|
|
|
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
|
2012-11-29 11:49:41 +01:00
|
|
|
if len(password) < 4:
|
2012-11-08 19:20:13 +01:00
|
|
|
raise YunoHostError(22, _("Password is too short"))
|
|
|
|
|
|
|
|
yldap.validate_uniqueness({
|
2012-11-29 11:49:41 +01:00
|
|
|
'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']:
|
2013-06-08 10:31:52 +02:00
|
|
|
raise YunoHostError(22, _("Domain not found : ")+ mail[mail.find('@')+1:])
|
2012-11-08 19:20:13 +01:00
|
|
|
|
2013-07-06 18:54:37 +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 19:01:11 +02:00
|
|
|
pwd_changed = os.system('echo "'+ password +'\n'+ password +'" | smbldap-passwd '+ username)
|
2013-07-06 18:54:37 +02:00
|
|
|
|
|
|
|
if user_added == pwd_changed == 0:
|
|
|
|
#TODO: Send a welcome mail to user
|
|
|
|
win_msg(_("User successfully created"))
|
|
|
|
return { _("Fullname") : firstname +' '+ lastname, _("Username") : username, _("Mail") : mail }
|
2012-11-08 19:20:13 +01:00
|
|
|
else:
|
|
|
|
raise YunoHostError(169, _("An error occured during user creation"))
|
|
|
|
|
|
|
|
|
2013-07-06 13:32:32 +02:00
|
|
|
def user_delete(users, purge=False):
|
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' : [] }
|
|
|
|
|
2012-11-29 11:49:41 +01:00
|
|
|
if not isinstance(users, list):
|
|
|
|
users = [ users ]
|
2012-11-08 19:20:13 +01:00
|
|
|
|
2012-11-29 11:49:41 +01:00
|
|
|
for user in users:
|
2013-07-06 13:32:32 +02:00
|
|
|
delete_command = '/usr/sbin/smbldap-userdel'
|
|
|
|
if purge:
|
|
|
|
delete_command = delete_command +' -r '+ user
|
|
|
|
else:
|
|
|
|
delete_command = delete_command +' '+ user
|
|
|
|
user_deleted = os.system(delete_command)
|
|
|
|
if user_deleted == 0:
|
2012-11-08 19:20:13 +01:00
|
|
|
result['Users'].append(user)
|
|
|
|
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
|
|
|
|
|
2012-10-29 13:02:55 +01:00
|
|
|
|
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
|
2012-11-29 11:49:41 +01:00
|
|
|
firstname
|
2013-07-06 10:17:16 +02:00
|
|
|
add_mailalias -- Mail aliases to add
|
2012-11-29 11:49:41 +01:00
|
|
|
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
|
2012-11-29 11:49:41 +01:00
|
|
|
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
|
2012-11-29 11:49:41 +01:00
|
|
|
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
|
|
|
|
2012-11-29 11:49:41 +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
|
|
|
|
2012-11-29 11:49:41 +01:00
|
|
|
if lastname and firstname:
|
|
|
|
new_attr_dict['cn'] = new_attr_dict['displayName'] = firstname + ' ' + lastname
|
2012-11-08 19:20:13 +01:00
|
|
|
|
2012-11-29 11:49:41 +01:00
|
|
|
if change_password:
|
2013-07-23 13:47:33 +02:00
|
|
|
pwd_changed = os.system('echo "'+ change_password +'\n'+ change_password +'" | smbldap-passwd '+ username)
|
2013-07-07 08:52:29 +02:00
|
|
|
if pwd_changed > 0:
|
|
|
|
raise YunoHostError(169, _("An error occured during password update"))
|
2012-11-08 19:20:13 +01:00
|
|
|
|
2012-11-29 11:49:41 +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:
|
2013-06-08 10:31:52 +02:00
|
|
|
raise YunoHostError(22, _("Domain not found : ")+ mail[mail.find('@')+1:])
|
2012-11-08 19:20:13 +01:00
|
|
|
del user['mail'][0]
|
2012-11-29 11:49:41 +01:00
|
|
|
new_attr_dict['mail'] = [mail] + user['mail']
|
2012-11-08 19:20:13 +01:00
|
|
|
|
2012-11-29 11:49:41 +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:
|
2013-06-08 10:31:52 +02:00
|
|
|
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
|
|
|
|
2012-11-29 11:49:41 +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
|
|
|
|
2012-11-29 11:49:41 +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):
|
2012-10-29 13:02:55 +01:00
|
|
|
"""
|
2013-07-06 10:17:16 +02:00
|
|
|
Get user informations
|
2012-10-29 13:02:55 +01:00
|
|
|
|
|
|
|
Keyword argument:
|
2013-07-06 10:17:16 +02:00
|
|
|
username -- Username or mail to get informations
|
2012-10-29 13:02:55 +01:00
|
|
|
|
|
|
|
"""
|
2012-11-08 19:20:13 +01:00
|
|
|
with YunoHostLDAP() as yldap:
|
2013-09-23 12:29:49 +02:00
|
|
|
user_attrs = ['cn', 'mail', 'uid', 'maildrop', 'givenName', 'sn']
|
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],
|
2013-09-23 12:29:49 +02:00
|
|
|
'Firstname': user['givenName'][0],
|
|
|
|
'Lastname': user['sn'][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
|
|
|
|