9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov#
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov# LDAP modlist generation
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov#
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov# Copyright (c) 2015 Red Hat, Inc.
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov# Author: Nikolai Kondrashov <Nikolai.Kondrashov@redhat.com>
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov#
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#
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#
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/>.
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov#
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov
60713f738cedb6e4239604baf6619a0ca986fa49Michal Židek
c423ad75a56b199083463a2714c8fbfd6e8edcc8Nikolai Kondrashovdef user(base_dn, uid, uidNumber, gidNumber,
c423ad75a56b199083463a2714c8fbfd6e8edcc8Nikolai Kondrashov userPassword=None,
c423ad75a56b199083463a2714c8fbfd6e8edcc8Nikolai Kondrashov gecos=None,
c423ad75a56b199083463a2714c8fbfd6e8edcc8Nikolai Kondrashov homeDirectory=None,
c423ad75a56b199083463a2714c8fbfd6e8edcc8Nikolai Kondrashov loginShell=None,
c423ad75a56b199083463a2714c8fbfd6e8edcc8Nikolai Kondrashov cn=None,
c423ad75a56b199083463a2714c8fbfd6e8edcc8Nikolai Kondrashov sn=None):
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov """
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov Generate an RFC2307(bis) user add-modlist for passing to ldap.add*
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov """
1097a61a8d4a892e126d14631c1b80fc1a5ce976Lukas Slebodnik uidNumber = str(uidNumber).encode('utf-8')
1097a61a8d4a892e126d14631c1b80fc1a5ce976Lukas Slebodnik gidNumber = str(gidNumber).encode('utf-8')
c423ad75a56b199083463a2714c8fbfd6e8edcc8Nikolai Kondrashov user = (
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov "uid=" + uid + ",ou=Users," + base_dn,
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov [
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')]),
c423ad75a56b199083463a2714c8fbfd6e8edcc8Nikolai Kondrashov ('uidNumber', [uidNumber]),
c423ad75a56b199083463a2714c8fbfd6e8edcc8Nikolai Kondrashov ('gidNumber', [gidNumber]),
1097a61a8d4a892e126d14631c1b80fc1a5ce976Lukas Slebodnik ('userPassword', [b'Password' + uidNumber
c423ad75a56b199083463a2714c8fbfd6e8edcc8Nikolai Kondrashov if userPassword is None
1097a61a8d4a892e126d14631c1b80fc1a5ce976Lukas Slebodnik else userPassword.encode('utf-8')]),
1097a61a8d4a892e126d14631c1b80fc1a5ce976Lukas Slebodnik ('homeDirectory', [b'/home/' + uid.encode('utf-8')
c423ad75a56b199083463a2714c8fbfd6e8edcc8Nikolai Kondrashov if homeDirectory is None
1097a61a8d4a892e126d14631c1b80fc1a5ce976Lukas Slebodnik else homeDirectory.encode('utf-8')]),
1097a61a8d4a892e126d14631c1b80fc1a5ce976Lukas Slebodnik ('loginShell', [b'/bin/bash'
c423ad75a56b199083463a2714c8fbfd6e8edcc8Nikolai Kondrashov if loginShell is None
1097a61a8d4a892e126d14631c1b80fc1a5ce976Lukas Slebodnik else loginShell.encode('utf-8')]),
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov ]
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov )
c423ad75a56b199083463a2714c8fbfd6e8edcc8Nikolai Kondrashov if gecos is not None:
1097a61a8d4a892e126d14631c1b80fc1a5ce976Lukas Slebodnik user[1].append(('gecos', [gecos.encode('utf-8')]))
c423ad75a56b199083463a2714c8fbfd6e8edcc8Nikolai Kondrashov return user
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov
60713f738cedb6e4239604baf6619a0ca986fa49Michal Židek
1097a61a8d4a892e126d14631c1b80fc1a5ce976Lukas Slebodnikdef group(base_dn, cn, gidNumber, member_uids=()):
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov """
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov Generate an RFC2307 group add-modlist for passing to ldap.add*.
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov """
1097a61a8d4a892e126d14631c1b80fc1a5ce976Lukas Slebodnik gidNumber = str(gidNumber).encode('utf-8')
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov attr_list = [
1097a61a8d4a892e126d14631c1b80fc1a5ce976Lukas Slebodnik ('objectClass', [b'top', b'posixGroup']),
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov ('gidNumber', [gidNumber])
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov ]
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov if len(member_uids) > 0:
1097a61a8d4a892e126d14631c1b80fc1a5ce976Lukas Slebodnik mem_uids = [member.encode('utf-8') for member in member_uids]
1097a61a8d4a892e126d14631c1b80fc1a5ce976Lukas Slebodnik attr_list.append(('memberUid', mem_uids))
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov return ("cn=" + cn + ",ou=Groups," + base_dn, attr_list)
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov
60713f738cedb6e4239604baf6619a0ca986fa49Michal Židek
1097a61a8d4a892e126d14631c1b80fc1a5ce976Lukas Slebodnikdef group_bis(base_dn, cn, gidNumber, member_uids=(), member_gids=()):
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov """
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov Generate an RFC2307bis group add-modlist for passing to ldap.add*.
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov """
1097a61a8d4a892e126d14631c1b80fc1a5ce976Lukas Slebodnik gidNumber = str(gidNumber).encode('utf-8')
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov attr_list = [
1097a61a8d4a892e126d14631c1b80fc1a5ce976Lukas Slebodnik ('objectClass', [b'top', b'extensibleObject', b'groupOfNames']),
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov ('gidNumber', [gidNumber])
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov ]
c65de71bc38753320b9fd6f6fe1386244a2ff54aNikolai Kondrashov member_list = []
c65de71bc38753320b9fd6f6fe1386244a2ff54aNikolai Kondrashov for uid in member_uids:
c65de71bc38753320b9fd6f6fe1386244a2ff54aNikolai Kondrashov member_list.append("uid=" + uid + ",ou=Users," + base_dn)
c65de71bc38753320b9fd6f6fe1386244a2ff54aNikolai Kondrashov for gid in member_gids:
c65de71bc38753320b9fd6f6fe1386244a2ff54aNikolai Kondrashov member_list.append("cn=" + gid + ",ou=Groups," + base_dn)
c65de71bc38753320b9fd6f6fe1386244a2ff54aNikolai Kondrashov if len(member_list) > 0:
1097a61a8d4a892e126d14631c1b80fc1a5ce976Lukas Slebodnik mem_list = [member.encode('utf-8') for member in member_list]
1097a61a8d4a892e126d14631c1b80fc1a5ce976Lukas Slebodnik attr_list.append(('member', mem_list))
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov return ("cn=" + cn + ",ou=Groups," + base_dn, attr_list)
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov
60713f738cedb6e4239604baf6619a0ca986fa49Michal Židek
1cba321946084231c220e9561487555671b944c3Petr Cechdef netgroup(base_dn, cn, triples=(), members=()):
1cba321946084231c220e9561487555671b944c3Petr Cech """
1cba321946084231c220e9561487555671b944c3Petr Cech Generate an RFC2307bis netgroup add-modlist for passing to ldap.add*.
1cba321946084231c220e9561487555671b944c3Petr Cech """
1cba321946084231c220e9561487555671b944c3Petr Cech attr_list = [
1097a61a8d4a892e126d14631c1b80fc1a5ce976Lukas Slebodnik ('objectClass', [b'top', b'nisNetgroup'])
1cba321946084231c220e9561487555671b944c3Petr Cech ]
1cba321946084231c220e9561487555671b944c3Petr Cech if triples:
1097a61a8d4a892e126d14631c1b80fc1a5ce976Lukas Slebodnik triples = [triple.encode('utf-8') for triple in triples]
1cba321946084231c220e9561487555671b944c3Petr Cech attr_list.append(('nisNetgroupTriple', triples))
1cba321946084231c220e9561487555671b944c3Petr Cech if members:
1097a61a8d4a892e126d14631c1b80fc1a5ce976Lukas Slebodnik members = [member.encode('utf-8') for member in members]
1cba321946084231c220e9561487555671b944c3Petr Cech attr_list.append(('memberNisNetgroup', members))
1cba321946084231c220e9561487555671b944c3Petr Cech return ("cn=" + cn + ",ou=Netgroups," + base_dn, attr_list)
1cba321946084231c220e9561487555671b944c3Petr Cech
1cba321946084231c220e9561487555671b944c3Petr Cech
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashovclass List(list):
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov """LDAP add-modlist list"""
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov def __init__(self, base_dn):
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov self.base_dn = base_dn
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov def add_user(self, uid, uidNumber, gidNumber,
c423ad75a56b199083463a2714c8fbfd6e8edcc8Nikolai Kondrashov base_dn=None,
c423ad75a56b199083463a2714c8fbfd6e8edcc8Nikolai Kondrashov userPassword=None,
c423ad75a56b199083463a2714c8fbfd6e8edcc8Nikolai Kondrashov gecos=None,
c423ad75a56b199083463a2714c8fbfd6e8edcc8Nikolai Kondrashov homeDirectory=None,
c423ad75a56b199083463a2714c8fbfd6e8edcc8Nikolai Kondrashov loginShell=None,
c423ad75a56b199083463a2714c8fbfd6e8edcc8Nikolai Kondrashov cn=None,
c423ad75a56b199083463a2714c8fbfd6e8edcc8Nikolai Kondrashov sn=None):
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov """Add an RFC2307(bis) user add-modlist."""
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov self.append(user(base_dn or self.base_dn,
c423ad75a56b199083463a2714c8fbfd6e8edcc8Nikolai Kondrashov uid, uidNumber, gidNumber,
c423ad75a56b199083463a2714c8fbfd6e8edcc8Nikolai Kondrashov userPassword=userPassword,
c423ad75a56b199083463a2714c8fbfd6e8edcc8Nikolai Kondrashov gecos=gecos,
c423ad75a56b199083463a2714c8fbfd6e8edcc8Nikolai Kondrashov homeDirectory=homeDirectory,
c423ad75a56b199083463a2714c8fbfd6e8edcc8Nikolai Kondrashov loginShell=loginShell,
c423ad75a56b199083463a2714c8fbfd6e8edcc8Nikolai Kondrashov cn=cn,
c423ad75a56b199083463a2714c8fbfd6e8edcc8Nikolai Kondrashov sn=sn))
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov def add_group(self, cn, gidNumber, member_uids=[],
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov base_dn=None):
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov """Add an RFC2307 group add-modlist."""
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov self.append(group(base_dn or self.base_dn,
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov cn, gidNumber, member_uids))
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov def add_group_bis(self, cn, gidNumber,
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov member_uids=[], member_gids=[],
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov base_dn=None):
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov """Add an RFC2307bis group add-modlist."""
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov self.append(group_bis(base_dn or self.base_dn,
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov cn, gidNumber,
9d453f1e8b28983b363b44c49b7cd701a994fd97Nikolai Kondrashov member_uids, member_gids))
1cba321946084231c220e9561487555671b944c3Petr Cech
1cba321946084231c220e9561487555671b944c3Petr Cech def add_netgroup(self, cn, triples=(), members=(), base_dn=None):
1cba321946084231c220e9561487555671b944c3Petr Cech """Add an RFC2307bis netgroup add-modlist."""
1cba321946084231c220e9561487555671b944c3Petr Cech self.append(netgroup(base_dn or self.base_dn,
1cba321946084231c220e9561487555671b944c3Petr Cech cn, triples, members))