Solved bugs
This commit is contained in:
58
main.py
58
main.py
@@ -110,39 +110,35 @@ def main():
|
|||||||
except ldap.LDAPError as e:
|
except ldap.LDAPError as e:
|
||||||
print(f"Failed to search schema container: {e}", file=sys.stderr)
|
print(f"Failed to search schema container: {e}", file=sys.stderr)
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
# Determine existing indices
|
# Determine existing indices and detect if schema snippet already exists
|
||||||
idx_pattern = re.compile(r'\{(\d+)\}(.+)')
|
idx_re = re.compile(r'\{(\d+)\}([^,]+)')
|
||||||
indices = []
|
indices = []
|
||||||
existing_idx = None
|
existing_idx = None
|
||||||
for dn, _ in entries:
|
for dn, _ in entries:
|
||||||
m = idx_pattern.search(dn)
|
m = idx_re.search(dn)
|
||||||
if m:
|
if not m:
|
||||||
idx = int(m.group(1))
|
continue
|
||||||
name = m.group(2)
|
idx = int(m.group(1))
|
||||||
indices.append(idx)
|
name = m.group(2) # snippet name before the comma
|
||||||
if name == args.schema_name:
|
indices.append(idx)
|
||||||
existing_idx = idx
|
if name == args.schema_name:
|
||||||
|
existing_idx = idx
|
||||||
|
|
||||||
# Compute target index
|
# Compute which index to use
|
||||||
if existing_idx is not None:
|
if existing_idx is not None:
|
||||||
idx = existing_idx
|
idx = existing_idx
|
||||||
print(f"Using existing schema index {{idx}} for '{args.schema_name}'")
|
print(f"✔️ Using existing schema snippet {{{idx}}}{args.schema_name}")
|
||||||
else:
|
else:
|
||||||
idx = (max(indices) + 1) if indices else 0
|
idx = max(indices) + 1 if indices else 0
|
||||||
prefix = f'{{{idx}}}'
|
prefix = f'{{{idx}}}'
|
||||||
new_dn = f"cn={prefix}{args.schema_name},{base_dn}"
|
new_dn = f"cn={prefix}{args.schema_name},{base_dn}"
|
||||||
entry_attrs = {
|
entry_attrs = {
|
||||||
'objectClass': [b'top', b'olcSchemaConfig'],
|
'objectClass': [b'top', b'olcSchemaConfig'],
|
||||||
'cn': [f"{prefix}{args.schema_name}".encode()],
|
'cn': [f"{prefix}{args.schema_name}".encode()],
|
||||||
}
|
}
|
||||||
ldif = modlist.addModlist(entry_attrs)
|
conn.add_s(new_dn, ldap.modlist.addModlist(entry_attrs))
|
||||||
try:
|
print(f"✅ Created new schema snippet: {new_dn}")
|
||||||
conn.add_s(new_dn, ldif)
|
|
||||||
print(f"Created schema entry {new_dn}")
|
|
||||||
except ldap.LDAPError as e:
|
|
||||||
print(f"Failed to create schema entry: {e}", file=sys.stderr)
|
|
||||||
sys.exit(1)
|
|
||||||
|
|
||||||
# Final DN for modifications
|
# Final DN for modifications
|
||||||
prefix = f'{{{idx}}}'
|
prefix = f'{{{idx}}}'
|
||||||
@@ -155,12 +151,17 @@ def main():
|
|||||||
existing = result[0][1].get('olcAttributeTypes', [])
|
existing = result[0][1].get('olcAttributeTypes', [])
|
||||||
encoded = atdef.encode()
|
encoded = atdef.encode()
|
||||||
if encoded in existing:
|
if encoded in existing:
|
||||||
print(f"AttributeType already present: {atdef}")
|
print(f"ℹ️ AttributeType already present: {atdef}")
|
||||||
else:
|
else:
|
||||||
conn.modify_s(schema_dn, [(ldap.MOD_ADD, 'olcAttributeTypes', [encoded])])
|
conn.modify_s(schema_dn, [(ldap.MOD_ADD, 'olcAttributeTypes', [encoded])])
|
||||||
print(f"Added AttributeType: {atdef}")
|
print(f"➕ Added AttributeType: {atdef}")
|
||||||
except ldap.LDAPError as e:
|
except ldap.LDAPError as e:
|
||||||
print(f"Error adding AttributeType '{atdef}': {e}", file=sys.stderr)
|
info = getattr(e, 'info', '') or str(e)
|
||||||
|
if 'Duplicate attributeType' in info:
|
||||||
|
print(f"ℹ️ Duplicate AttributeType skipped: {atdef}")
|
||||||
|
else:
|
||||||
|
print(f"❌ Error adding AttributeType '{atdef}': {e}", file=sys.stderr)
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
# Add/update ObjectClasses
|
# Add/update ObjectClasses
|
||||||
for ocdef in args.object_class:
|
for ocdef in args.object_class:
|
||||||
@@ -169,12 +170,17 @@ def main():
|
|||||||
existing = result[0][1].get('olcObjectClasses', [])
|
existing = result[0][1].get('olcObjectClasses', [])
|
||||||
encoded = ocdef.encode()
|
encoded = ocdef.encode()
|
||||||
if encoded in existing:
|
if encoded in existing:
|
||||||
print(f"ObjectClass already present: {ocdef}")
|
print(f"ℹ️ ObjectClass already present: {ocdef}")
|
||||||
else:
|
else:
|
||||||
conn.modify_s(schema_dn, [(ldap.MOD_ADD, 'olcObjectClasses', [encoded])])
|
conn.modify_s(schema_dn, [(ldap.MOD_ADD, 'olcObjectClasses', [encoded])])
|
||||||
print(f"Added ObjectClass: {ocdef}")
|
print(f"➕ Added ObjectClass: {ocdef}")
|
||||||
except ldap.LDAPError as e:
|
except ldap.LDAPError as e:
|
||||||
print(f"Error adding ObjectClass '{ocdef}': {e}", file=sys.stderr)
|
info = getattr(e, 'info', '') or str(e)
|
||||||
|
if 'Duplicate objectClass' in info:
|
||||||
|
print(f"ℹ️ Duplicate ObjectClass skipped: {ocdef}")
|
||||||
|
else:
|
||||||
|
print(f"❌ Error adding ObjectClass '{ocdef}': {e}", file=sys.stderr)
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
conn.unbind_s()
|
conn.unbind_s()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user