def main():
    usage = "usage: %prog [options]"
    parser = optparse.OptionParser(usage=usage, description=__doc__)
    parser.add_option("-f",
                      "--filter",
                      help="resync objects from master found by this filter")
    parser.add_option("-r",
                      "--remove",
                      action="store_true",
                      help="remove objects in local database before resync")
    parser.add_option("-s",
                      "--simulate",
                      action="store_true",
                      help="dry run, do not remove or add")
    opts, args = parser.parse_args()

    ucr = univention.config_registry.ConfigRegistry()
    ucr.load()
    base = ucr.get("ldap/base")
    binddn = "cn=update,%s" % base
    with open("/etc/ldap/rootpw.conf", "r") as fh:
        for line in fh:
            line = line.strip()
            if line.startswith('rootpw '):
                bindpw = line[7:].strip('"')
                break
        else:
            exit(1)

    if not opts.filter:
        opts.filter = '(uid=%s$)' % ucr['hostname']

    # get local and master connection
    local = uldap.access(binddn=binddn,
                         bindpw=bindpw,
                         start_tls=0,
                         host="localhost",
                         port=389)
    if ucr.get("server/role", "") == "domaincontroller_backup":
        master = uldap.getAdminConnection()
    else:
        master = uldap.getMachineConnection(ldap_master=True)

    # delete local
    if opts.remove:
        res = local.search(base=base, filter=opts.filter)
        for dn, data in res:
            print("remove from local: %s" % (dn, ))
            if not opts.simulate:
                local.delete(dn)

    # resync from master
    res = master.search(base=base, filter=opts.filter)
    for dn, data in res:
        print("resync from master: %s" % (dn, ))
        if not opts.simulate:
            local.add(dn, ldap.modlist.addModlist(data))
示例#2
0
    def test_create_network(self, udm):
        """Test the base functionality to create and remove a network"""
        networkName = uts.random_name()
        network = udm.create_object('networks/network',
                                    name=networkName,
                                    network='10.20.30.0',
                                    netmask='24')

        ldap = uldap.getAdminConnection()
        ldapFilter = '(&(cn=%s)(objectClass=univentionNetworkClass))' % networkName
        assert network in ldap.searchDn(filter=ldapFilter)

        udm.remove_object('networks/network', dn=network)
        assert network not in ldap.searchDn(filter=ldapFilter)
def main():
    usage = "usage: %prog [options]"
    parser = optparse.OptionParser(usage=usage, description=__doc__)
    parser.add_option("-l",
                      "--ldif",
                      action="store_true",
                      help="Create LDIF file")
    parser.add_option("-s",
                      "--schema",
                      action="store_true",
                      help="Update LDAP schema [%s]" % SCHEMA)
    parser.add_option("-o",
                      "--outfile",
                      default=LDIF,
                      help="File to store gzip LDIF data [%default]")
    parser.add_option("-p",
                      "--pagesize",
                      type=int,
                      default=1000,
                      help="page size to use for LDAP paged search")
    parser.add_option("-v",
                      "--verbose",
                      action="count",
                      help="Increase verbosity")
    opts, args = parser.parse_args()

    logging.basicConfig(
        stream=sys.stderr,
        level=logging.DEBUG if opts.verbose else logging.WARNING)

    ucr = univention.config_registry.ConfigRegistry()
    ucr.load()
    base = ucr.get("ldap/base")
    if ucr.get("server/role", "") == "domaincontroller_backup":
        lo = uldap.getAdminConnection()
    else:
        lo = uldap.getMachineConnection(ldap_master=True)

    if opts.schema:
        update_schema(lo)

    if opts.ldif:
        create_ldif_from_master(lo, opts.outfile, base, opts.pagesize)
def main():
    usage = "usage: %prog [options]"
    parser = optparse.OptionParser(usage=usage, description=__doc__)
    parser.add_option(
        "-f",
        "--filter",
        help=
        "resync objects from master found by this filter. Default: (uid=<hostname>$)"
    )
    parser.add_option("-r",
                      "--remove",
                      action="store_true",
                      help="remove objects in local database before resync")
    parser.add_option("-s",
                      "--simulate",
                      action="store_true",
                      help="dry run, do not remove or add")
    parser.add_option("-u",
                      "--update",
                      action="store_true",
                      help="update/modify existing objects")
    opts, args = parser.parse_args()

    ucr = univention.config_registry.ConfigRegistry()
    ucr.load()
    base = ucr.get("ldap/base")
    server_role = ucr.get("server/role", "")
    if server_role == 'domaincontroller_master':
        print('local ldap is master server, nothing todo')
        return
    if server_role not in [
            'domaincontroller_backup', 'domaincontroller_slave'
    ]:
        print(
            'server role ("{}") has no ldap, nothing todo'.format(server_role))
        return

    if not opts.filter:
        opts.filter = '(uid=%s$)' % ucr['hostname']

    # get local and master connection
    local = uldap.getRootDnConnection()
    if server_role == "domaincontroller_backup":
        master = uldap.getAdminConnection()
    else:
        master = uldap.getMachineConnection(ldap_master=True)

    # delete local
    if opts.remove:
        res = local.search(base=base, filter=opts.filter)
        if not res:
            print('object does not exist local')
        for dn, data in res:
            print("remove from local: %s" % (dn, ))
            if not opts.simulate:
                local.delete(dn)

    # resync from master
    res = master.search(base=base, filter=opts.filter)
    if not res:
        print('object does not exist on master')
    for dn, data in res:
        print("resync from master: %s" % (dn, ))
        try:
            local_res = local.search(base=dn)
        except ldap.NO_SUCH_OBJECT:
            local_res = None
        if local_res and opts.remove and opts.simulate:
            local_res = None
        if not local_res and not opts.update:
            print('  ==> adding object')
            if not opts.simulate:
                local.add(dn, ldap.modlist.addModlist(data))
        elif not local_res and opts.update:
            print('  ==> object does not exist, can not update')
        elif local_res and opts.update:
            modlist = []
            local_data = local_res[0][1]
            for key in set(data.keys()) | set(local_data.keys()):
                if set(local_data.get(key, [])).symmetric_difference(
                        set(data.get(key, []))):
                    modlist.append(
                        [key, local_data.get(key, []),
                         data.get(key, [])])
            if not modlist:
                print('  ==> no change')
            else:
                print('  ==> modifying object')
                if not opts.simulate:
                    local.modify(dn, modlist)
        elif local_res and not opts.update:
            print('  ==> object does exist, can not create')