2018-07-07 18:01:04 +00:00
|
|
|
# Copyright © 2018 Maël Kervella
|
2021-01-10 16:49:23 +00:00
|
|
|
# Copyright © 2021 Hugo Levy-Falk
|
2018-07-07 18:01:04 +00:00
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 2 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 General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License along
|
|
|
|
# with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
#
|
|
|
|
import subprocess
|
|
|
|
from base64 import decodebytes
|
|
|
|
|
|
|
|
from django.core.management.base import BaseCommand, CommandError
|
|
|
|
from django.conf import settings
|
|
|
|
|
|
|
|
from users.models import User, ListRight
|
2021-01-10 16:49:23 +00:00
|
|
|
from ldap_sync.models import synchronise_user, synchronise_serviceuser, synchronise_usergroup
|
2018-07-07 18:01:04 +00:00
|
|
|
|
|
|
|
|
2018-07-12 17:55:07 +00:00
|
|
|
def split_lines(lines):
|
|
|
|
"""
|
|
|
|
Split LDIF lines. They can span over multiple system lines if the
|
|
|
|
following system lines begins with a space.
|
|
|
|
"""
|
|
|
|
ret = []
|
2019-11-04 16:55:03 +00:00
|
|
|
for line in lines.split(b"\n"):
|
|
|
|
if line.startswith(b" ") and len(ret) > 1:
|
|
|
|
ret[-1] += line[len(b" ") :]
|
2018-07-12 17:55:07 +00:00
|
|
|
else:
|
|
|
|
ret.append(line)
|
|
|
|
return ret
|
|
|
|
|
|
|
|
|
2018-07-07 18:01:04 +00:00
|
|
|
def flush_ldap(binddn, bindpass, server, usersdn, groupsdn):
|
|
|
|
"""
|
|
|
|
Perform the python (and more understandable) equivalent of the following commands:
|
|
|
|
|
2018-07-12 17:55:07 +00:00
|
|
|
ldapsearch -LLL -s one -D $binddn -w $bindpass -H $server -b $usersdn dn \
|
2018-07-07 18:01:04 +00:00
|
|
|
| grep "dn: " | sed -e 's/dn: //g' \
|
|
|
|
| ldapdelete -v -D $binddn -w $bindpass -H $server --
|
2018-07-12 17:55:07 +00:00
|
|
|
ldapsearch -LLL -s one -D $binddn -w $bindpass -H $server -b $usersdn dn \
|
2018-07-07 18:01:04 +00:00
|
|
|
| grep "dn:: " | sed -e 's/dn:: //g' \
|
|
|
|
| while read x; do echo "$x" | base64 -d; echo ""; done \
|
|
|
|
| ldapdelete -v -D $binddn -w $bindpass -H $server --
|
2018-07-12 17:55:07 +00:00
|
|
|
ldapsearch -LLL -s one -D $binddn -w $bindpass -H $server -b $groupsdn dn \
|
2018-07-07 18:01:04 +00:00
|
|
|
| grep "dn: " | sed -e 's/dn: //g' \
|
|
|
|
| ldapdelete -v -D $binddn -w $bindpass -H $server --
|
2018-07-12 17:55:07 +00:00
|
|
|
ldapsearch -LLL -s one -D $binddn -w $bindpass -H $server -b $groupsdn dn \
|
2018-07-07 18:01:04 +00:00
|
|
|
| grep "dn:: " | sed -e 's/dn:: //g' \
|
|
|
|
| while read x; do echo "$x" | base64 -d; echo ""; done \
|
|
|
|
| ldapdelete -v -D $binddn -w $bindpass -H $server --
|
|
|
|
"""
|
|
|
|
|
|
|
|
to_remove = []
|
|
|
|
|
|
|
|
for lookup in (usersdn, groupsdn):
|
|
|
|
search_cmd = [
|
2019-11-04 16:55:03 +00:00
|
|
|
"ldapsearch",
|
|
|
|
"-LLL",
|
|
|
|
"-s",
|
|
|
|
"one",
|
|
|
|
"-D",
|
|
|
|
binddn,
|
|
|
|
"-w",
|
|
|
|
bindpass,
|
|
|
|
"-H",
|
|
|
|
server,
|
|
|
|
"-b",
|
|
|
|
lookup,
|
|
|
|
"dn",
|
2018-07-07 18:01:04 +00:00
|
|
|
]
|
2018-07-12 17:55:07 +00:00
|
|
|
for line in split_lines(subprocess.check_output(search_cmd)):
|
2019-11-04 16:55:03 +00:00
|
|
|
if line.startswith(b"dn: "):
|
|
|
|
to_remove.append(line[len(b"dn: ") :])
|
|
|
|
elif line.startswith(b"dn:: "):
|
2018-07-07 18:01:04 +00:00
|
|
|
# Non ASCII value ares are base64-encoded
|
2019-11-04 16:55:03 +00:00
|
|
|
to_remove.append(decodebytes(line[len(b"dn:: ") :]))
|
|
|
|
|
|
|
|
delete_cmd = ["ldapdelete", "-D", binddn, "-w", bindpass, "-H", server] + to_remove
|
2018-07-07 18:01:04 +00:00
|
|
|
subprocess.check_call(delete_cmd)
|
|
|
|
|
|
|
|
|
|
|
|
def sync_ldap():
|
|
|
|
"""Syncrhonize the whole LDAP with the DB."""
|
|
|
|
for u in User.objects.all():
|
2021-01-10 16:49:23 +00:00
|
|
|
synchronise_user(sender=User, instance=u)
|
2018-07-07 18:01:04 +00:00
|
|
|
for lr in ListRight.objects.all():
|
2021-01-10 16:49:23 +00:00
|
|
|
synchronise_usergroup(sender=ListRight, instance=lr)
|
2018-07-07 18:01:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Command(BaseCommand):
|
2019-11-04 16:55:03 +00:00
|
|
|
help = (
|
2019-11-16 14:11:57 +00:00
|
|
|
"Destroy the current LDAP data and rebuild it from the DB data."
|
|
|
|
" Use with caution."
|
2019-11-04 16:55:03 +00:00
|
|
|
)
|
2018-07-07 18:01:04 +00:00
|
|
|
|
|
|
|
def handle(self, *args, **options):
|
|
|
|
|
2019-11-04 16:55:03 +00:00
|
|
|
usersdn = settings.LDAP["base_user_dn"]
|
|
|
|
groupsdn = settings.LDAP["base_usergroup_dn"]
|
|
|
|
binddn = settings.DATABASES["ldap"]["USER"]
|
|
|
|
bindpass = settings.DATABASES["ldap"]["PASSWORD"]
|
|
|
|
server = settings.DATABASES["ldap"]["NAME"]
|
2018-07-07 18:01:04 +00:00
|
|
|
|
|
|
|
flush_ldap(binddn, bindpass, server, usersdn, groupsdn)
|
2019-11-16 14:11:57 +00:00
|
|
|
self.stdout.write("LDAP emptied.")
|
2018-07-07 18:01:04 +00:00
|
|
|
sync_ldap()
|
2019-11-16 14:11:57 +00:00
|
|
|
self.stdout.write("LDAP rebuilt.")
|