mkdir: support more objectclasses
authorPeter Marschall <peter@adpm.de>
Sun, 06 Mar 2011 14:53:39 +0100
changeset 29 bd95c3aea253
parent 28 d42bd1b087a1
child 30 e4b4b0968107
mkdir: support more objectclasses Depending on the naming attribute given, support the objectclasses 'country' and 'organization' in addition to the default 'organizationalUnit'.
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<mkdir>
 
-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<move>
 
@@ -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 );