# HG changeset patch # User Peter Marschall # Date 1299419619 -3600 # Node ID bd95c3aea25303572f836c038aa6bffbebd5a6bb # Parent d42bd1b087a1dea47654fa693a1a7caa252d1cf8 mkdir: support more objectclasses Depending on the naming attribute given, support the objectclasses 'country' and 'organization' in addition to the default 'organizationalUnit'. diff -r d42bd1b087a1 -r bd95c3aea253 shelldap --- a/shelldap Sat Mar 05 22:22:47 2011 +0100 +++ b/shelldap Sun Mar 06 14:53:39 2011 +0100 @@ -304,10 +304,16 @@ =item B -Creates a new 'organizationalUnit' entry. +Creates a new entry. The type of object created depends on +the naming attribute given, and defaults to 'organizationalUnit' +if none is given. +Supported naming attributes and corresponding object classes are: + c - country + o - organization + ou - organizationalUnit - mkdir containername - mkdir ou=whatever + mkdir myNewOrgUnit + mkdir o=myNewOrg =item B @@ -1546,24 +1552,47 @@ { my $self = shift; my $dir = join ' ', @_; + my %ClassMap = ( c => 'country', + o => 'organization', + ou => 'organizationalUnit' ); + my %class; unless ( $dir ) { print "No 'directory' provided.\n"; return; } - # normalize ou name, then pull uniq val back out. - $dir = "ou=$dir" unless $dir =~ /^ou=/i; + # ToDo: path_to_dn $self->rdn_to_dn( \$dir ); - my $ou = $1 - if $dir =~ /^[\.\w]+(?:\s+)?=(?:\s+)?([\.\-\s\w]+),?/; + # normalize name, if it is not yet a legal DN + $dir = 'ou='.$dir if (!canonical_dn($dir)); + + # get RDN: naming attributes (lower-case) and their values + my %rdn = %{ shift(@{ ldap_explode_dn($dir, casefold => 'lower') }) }; + + # without RDN, return error + unless ( %rdn ) { + print "Illegal DN: $dir\n"; + return; + } - # add - my $r = $self->ldap()->add( $dir, attr => [ - objectClass => [ 'top', 'organizationalUnit' ], - ou => $ou, - ]); + # get objectclass from naming attributes + foreach my $attr (keys(%rdn)) { + map { $class{$ClassMap{$_}} = 1 if ($attr =~ /^\Q$_\E$/); } + keys(%ClassMap); + } + + # fail if we did not get a unique objectclass + unless (scalar(keys(%class)) == 1) { + print "Unsupported DN: $dir\n"; + return; + } + + # create the new object + my $r = $self->ldap()->add($dir, + attr => [ objectClass => [ 'top', keys(%class) ], + %rdn ]); print $r->error(), "\n"; $self->update_entries( clearcache => 1 );