Skip to content

Instantly share code, notes, and snippets.

@alanorth
Created November 11, 2022 07:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alanorth/c1f46028ff84bc8841a755c8778ac42c to your computer and use it in GitHub Desktop.
Save alanorth/c1f46028ff84bc8841a755c8778ac42c to your computer and use it in GitHub Desktop.
Migrate an LDIF from RFC 2307 to RFC 2307bis, ie the schema used in 389-ds 1.3.x to 1.4+.
BEGIN {}
/dn: uid=.*,ou=People,dc=ilri,dc=cgiar,dc=org/ {
print "# User migrated to RFC 2307bis";
# Keep getting the next line until we have a blank one (which means this
# user's LDIF entry is finished).
while ($0 !~ /^$/) {
# Lines to update or remove. I comment them out by printing a hash and
# "&", which is a special awk syntax to print the pattern that matched.
switch($0) {
case /^objectClass: person/:
sub(/^objectClass: person/, "objectClass: nsPerson");
# Print the line as it is after substitution
print;
# Break out of the switch statement
break;
case /^objectClass: organizationalPerson/:
sub(/^objectClass: organizationalPerson/, "objectClass: nsAccount");
print;
break;
case /^objectClass: inetorgperson/:
sub(/^objectClass: inetorgperson/, "objectClass: nsOrgPerson");
print;
break;
# givenName is not allowed
case /^givenName: .*/:
sub(/^givenName: .*/, "#&");
print;
break;
# sn is not allowed
case /^sn: .*/:
sub(/^sn: .*/, "#&");
print;
break;
# When we see a cn, copy the value to displayName and then print both
case /^cn: .*/:
displayName = gensub(/^cn: (.*)/, "displayName: \\1", "g", $0);
print;
print displayName;
break;
default:
print;
}
# Read the next record (aka line) immediately, which effectively runs the line
# through the switch cases again. Exit if we have reached the end of the file.
if (getline <= 0) {
exit;
}
}
}
# TODO: memberUid to member
/dn: cn=.*,ou=Groups,dc=ilri,dc=cgiar,dc=org/ {
print "# Group migrated to RFC 2307bis";
while ($0 !~ /^$/) {
switch($0) {
case /^objectClass: groupofuniquenames/:
sub(/^objectClass: groupofuniquenames/, "objectClass: groupOfNames");
print;
break;
case /^objectClass: posixgroup/:
sub(/^objectClass: posixgroup/, "objectClass: posixGroup");
print;
print "objectClass: nsMemberOf";
break;
default:
print;
}
if (getline <= 0) {
exit;
}
}
}
# Match and print all other lines in the LDIF
{ print }
END {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment