9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov# LDAP modlist generation
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov# Copyright (c) 2015 Red Hat, Inc.
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov# Author: Nikolai Kondrashov <Nikolai.Kondrashov@redhat.com>
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov# This is free software; you can redistribute it and/or modify it
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov# under the terms of the GNU General Public License as published by
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov# the Free Software Foundation; version 2 only
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov# This program is distributed in the hope that it will be useful, but
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov# WITHOUT ANY WARRANTY; without even the implied warranty of
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov# General Public License for more details.
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov# You should have received a copy of the GNU General Public License
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov# along with this program. If not, see <http://www.gnu.org/licenses/>.
c423ad75a56b199083463a2714c8fbfd6e8edcc8Nikolai Kondrashovdef user(base_dn, uid, uidNumber, gidNumber,
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov Generate an RFC2307(bis) user add-modlist for passing to ldap.add*
1097a61a8d4a892e126d14631c1b80fc1a5ce976Lukas Slebodnik ('objectClass', [b'top', b'inetOrgPerson', b'posixAccount']),
1097a61a8d4a892e126d14631c1b80fc1a5ce976Lukas Slebodnik ('cn', [uidNumber if cn is None else cn.encode('utf-8')]),
1097a61a8d4a892e126d14631c1b80fc1a5ce976Lukas Slebodnik ('sn', [b'User' if sn is None else sn.encode('utf-8')]),
1097a61a8d4a892e126d14631c1b80fc1a5ce976Lukas Slebodnik ('homeDirectory', [b'/home/' + uid.encode('utf-8')
c423ad75a56b199083463a2714c8fbfd6e8edcc8Nikolai Kondrashov if gecos is not None:
1097a61a8d4a892e126d14631c1b80fc1a5ce976Lukas Slebodnik user[1].append(('gecos', [gecos.encode('utf-8')]))
1097a61a8d4a892e126d14631c1b80fc1a5ce976Lukas Slebodnikdef group(base_dn, cn, gidNumber, member_uids=()):
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov Generate an RFC2307 group add-modlist for passing to ldap.add*.
1097a61a8d4a892e126d14631c1b80fc1a5ce976Lukas Slebodnik mem_uids = [member.encode('utf-8') for member in member_uids]
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov return ("cn=" + cn + ",ou=Groups," + base_dn, attr_list)
1097a61a8d4a892e126d14631c1b80fc1a5ce976Lukas Slebodnikdef group_bis(base_dn, cn, gidNumber, member_uids=(), member_gids=()):
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov Generate an RFC2307bis group add-modlist for passing to ldap.add*.
1097a61a8d4a892e126d14631c1b80fc1a5ce976Lukas Slebodnik ('objectClass', [b'top', b'extensibleObject', b'groupOfNames']),
c65de71bc38753320b9fd6f6fe1386244a2ff54aNikolai Kondrashov member_list.append("uid=" + uid + ",ou=Users," + base_dn)
c65de71bc38753320b9fd6f6fe1386244a2ff54aNikolai Kondrashov member_list.append("cn=" + gid + ",ou=Groups," + base_dn)
1097a61a8d4a892e126d14631c1b80fc1a5ce976Lukas Slebodnik mem_list = [member.encode('utf-8') for member in member_list]
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov return ("cn=" + cn + ",ou=Groups," + base_dn, attr_list)
1cba321946084231c220e9561487555671b944c3Petr Cech Generate an RFC2307bis netgroup add-modlist for passing to ldap.add*.
1097a61a8d4a892e126d14631c1b80fc1a5ce976Lukas Slebodnik triples = [triple.encode('utf-8') for triple in triples]
1097a61a8d4a892e126d14631c1b80fc1a5ce976Lukas Slebodnik members = [member.encode('utf-8') for member in members]
1cba321946084231c220e9561487555671b944c3Petr Cech return ("cn=" + cn + ",ou=Netgroups," + base_dn, attr_list)
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov """LDAP add-modlist list"""
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov def add_user(self, uid, uidNumber, gidNumber,
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov """Add an RFC2307(bis) user add-modlist."""
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov self.append(user(base_dn or self.base_dn,
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov def add_group(self, cn, gidNumber, member_uids=[],
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov """Add an RFC2307 group add-modlist."""
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov self.append(group(base_dn or self.base_dn,
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov """Add an RFC2307bis group add-modlist."""
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov self.append(group_bis(base_dn or self.base_dn,
1cba321946084231c220e9561487555671b944c3Petr Cech def add_netgroup(self, cn, triples=(), members=(), base_dn=None):
1cba321946084231c220e9561487555671b944c3Petr Cech """Add an RFC2307bis netgroup add-modlist."""