[LDAPsh-cvs] ldapsh ldapsh,1.44,1.45
Status: Beta
Brought to you by:
rcorvalan
From: <j-d...@us...> - 2004-02-09 08:13:08
|
Update of /cvsroot/ldapsh/ldapsh In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv6894 Modified Files: ldapsh Log Message: * Added -p option for the 'create' command to 'prune' out attributes with empty values (actually, it comments them out). * Added -X option for the 'create' command to easily 'resume' using the most recent LDIF file. * Added $LASTFILE to support the -X option. * Modified some system(...) calls so that $EDITOR may contain command-line arguments. Index: ldapsh =================================================================== RCS file: /cvsroot/ldapsh/ldapsh/ldapsh,v retrieving revision 1.44 retrieving revision 1.45 diff -C2 -d -r1.44 -r1.45 *** ldapsh 3 Feb 2004 01:09:42 -0000 1.44 --- ldapsh 9 Feb 2004 08:09:54 -0000 1.45 *************** *** 326,329 **** --- 326,330 ---- LDAPCONN => {VALUE => undef, RIGHTS => 'RW'}, DNSEP => {VALUE => "; ", RIGHTS => 'RWL'}, + LASTFILE => {VALUE => undef, RIGHTS => 'RWL'}, ENTRIES => {VALUE => [], RIGHTS => 'RW'}, COLUMNS => {VALUE => *************** *** 355,359 **** }; ! our ($COLUMNS, $CONNPARAMS, $CWD, $DNSEP, $EDITOR, $ENTRIES, $G, $LDAPCONN, $OLDWD, $PROMPT); our ($LDAPSH_PARSER, $PERL_RL); # read-only values my ($Term, $TermAttribs, $OffLine); --- 356,360 ---- }; ! our ($COLUMNS, $CONNPARAMS, $CWD, $DNSEP, $EDITOR, $ENTRIES, $G, $LDAPCONN, $OLDWD, $PROMPT, $LASTFILE); our ($LDAPSH_PARSER, $PERL_RL); # read-only values my ($Term, $TermAttribs, $OffLine); *************** *** 2029,2033 **** =head3 create ! B<Synopsis>: C<create ['-d',|'-k',|'-m',|'-n',|'-q',|'-v',] 'E<lt>RDN|DNE<gt>' [,'E<lt>objectclassE<gt>' ...]> Create a new LDIF file based on the specified distinguished named and object --- 2030,2036 ---- =head3 create ! B<Synopsis>: C<create ['-d',|'-k',|'-m',|'-n',|'-p',|'-q',|'-v',] 'E<lt>RDN|DNE<gt>' [,'E<lt>objectclassE<gt>' ...]> ! ! B<Synopsis>: C<create ['-d',|'-k',|'-p',] '-X'> Create a new LDIF file based on the specified distinguished named and object *************** *** 2036,2042 **** on the LDIF file. In the file is subsequently edited, a new LDAP entry will be created. Upon success, the LDIF file is deleted. Note: you must be sure to ! create a valid LDIF file from the template that is provided to you. If you do ! not, an entry cannot be created. If this error occurs, your invalid LDIF file ! will be preserved for your convenience. By default, the LDIF file is populated with all attributes specified in the schema --- 2039,2047 ---- on the LDIF file. In the file is subsequently edited, a new LDAP entry will be created. Upon success, the LDIF file is deleted. Note: you must be sure to ! create a valid LDIF file from the template that is provided to you (use the C<-p> ! option to allow B<ldapsh> to prune empty attributes from your LDIF file). If ! you do not, an entry cannot be created. If this error occurs, your invalid LDIF ! file will be preserved for your convenience (you can resume your attempt by ! using the C<-X> option). By default, the LDIF file is populated with all attributes specified in the schema *************** *** 2049,2052 **** --- 2054,2059 ---- while C<-d> forces deletion (regardless of failure). + Note: the effectiveness and correctness of the C<-p> option cannot be guaranteed. + =cut *************** *** 2054,2058 **** my @entries; my @successes; ! my %localArgs = ('d' => 0, 'k' => 0, 'm' => 0, 'n' => 0, 'q' => 0, 'v' => 0); @ARGV = @_; --- 2061,2065 ---- my @entries; my @successes; ! my %localArgs = ('d' => 0, 'k' => 0, 'm' => 0, 'n' => 0, 'p' => 0, 'q' => 0, 'v' => 0, 'X' => 0); @ARGV = @_; *************** *** 2060,2066 **** my $dn = shift(@ARGV); ! unless ($dn) { ! print STDERR "A distinguished name is required.\n"; ! return undef; } --- 2067,2081 ---- my $dn = shift(@ARGV); ! if ($localArgs{X}) { ! if ($dn) { ! print STDERR "No distinguished name allowed with -X.\n"; ! return undef; ! } ! } ! else { ! unless ($dn) { ! print STDERR "A distinguished name is required.\n"; ! return undef; ! } } *************** *** 2071,2122 **** require File::Temp; ! my ($fh, $tempfile) = File::Temp::tempfile('LDAPShell_vi_XXXXXXXXXX', SUFFIX => '.ldif'); ! ! # Print DN ! my @parts = _splitdn($dn,2); ! if (scalar(@parts) == 1) { ! print $fh "dn: $dn, $CWD\n"; } else { ! print $fh "dn: $dn\n"; ! } ! @parts = split('=',$parts[0]); ! # Deal with objectclasses ! my %attrs = (); ! if (@ARGV > 0) { ! $attrs{"objectclass"} = 1; ! foreach (@ARGV) { ! print $fh "objectclass: $_\n"; # caution: user might have supplied bad data } ! if (!$localArgs{n}) { ! my $schema = $Globals->{LDAPCONN}{VALUE}->schema(dn=>"cn=Subschema"); ! if ($schema) { ! foreach (@ARGV) { ! my $objectclass = $_; ! my @must = $schema->must($objectclass); ! foreach (@must) { ! my %attr = %$_; ! my $name = lc($attr{name}); ! if (!$attrs{$name}) { ! if ($localArgs{v}) { ! print $fh "# MUST $objectclass \"$attr{desc}\" ($name)\n"; ! } ! elsif (!$localArgs{q}) { ! print $fh "# MUST\n"; ! } ! if ("$name" eq "$parts[0]") { ! print $fh "$name: $parts[1]\n"; ! } ! else { ! print $fh "$name: \n"; ! } ! $attrs{$name} = 1; ! } ! } ! } ! if (!$localArgs{m}) { foreach (@ARGV) { my $objectclass = $_; ! my @must = $schema->may($objectclass); foreach (@must) { my %attr = %$_; --- 2086,2125 ---- require File::Temp; ! my ($fh, $tempfile); ! if ($localArgs{X}) { ! if (defined($LASTFILE)) { ! $tempfile = $LASTFILE; ! $fh = undef; ! } ! else { ! print STDERR "Cannot use -X: no file available.\n"; ! return undef; ! } } else { ! ($fh, $tempfile) = File::Temp::tempfile('LDAPShell_vi_XXXXXXXXXX', SUFFIX => '.ldif'); ! ! # Print DN ! my @parts = _splitdn($dn,2); ! if (scalar(@parts) == 1) { ! print $fh "dn: $dn, $CWD\n"; } ! else { ! print $fh "dn: $dn\n"; ! } ! @parts = split('=',$parts[0]); ! # Deal with objectclasses ! my %attrs = (); ! if (@ARGV > 0) { ! $attrs{"objectclass"} = 1; ! foreach (@ARGV) { ! print $fh "objectclass: $_\n"; # caution: user might have supplied bad data ! } ! if (!$localArgs{n}) { ! my $schema = $Globals->{LDAPCONN}{VALUE}->schema(dn=>"cn=Subschema"); ! if ($schema) { foreach (@ARGV) { my $objectclass = $_; ! my @must = $schema->must($objectclass); foreach (@must) { my %attr = %$_; *************** *** 2124,2131 **** if (!$attrs{$name}) { if ($localArgs{v}) { ! print $fh "# MAY $objectclass \"$attr{desc}\" ($name)\n"; } elsif (!$localArgs{q}) { ! print $fh "# MAY\n"; } if ("$name" eq "$parts[0]") { --- 2127,2134 ---- if (!$attrs{$name}) { if ($localArgs{v}) { ! print $fh "# MUST $objectclass \"$attr{desc}\" ($name)\n"; } elsif (!$localArgs{q}) { ! print $fh "# MUST\n"; } if ("$name" eq "$parts[0]") { *************** *** 2139,2160 **** } } } - } - else { - print $fh "# Server did not provide a schema (do you have permission?).\n"; - print $fh "$parts[0]: $parts[1]\n"; } } ! } ! if (!$attrs{$parts[0]}) { ! print $fh "$parts[0]: $parts[1]\n"; } ! close $fh; my $mtime = (stat($tempfile))[9]; my $cleanup = 1; # success => unlink $tempfile by default ! system($Globals->{EDITOR}{VALUE}, $tempfile); if ( (stat($tempfile))[9] > $mtime ) { my $ldif = Net::LDAP::LDIF->new($tempfile, "r", onerror => undef); while (not $ldif->eof()) { --- 2142,2214 ---- } } + if (!$localArgs{m}) { + foreach (@ARGV) { + my $objectclass = $_; + my @must = $schema->may($objectclass); + foreach (@must) { + my %attr = %$_; + my $name = lc($attr{name}); + if (!$attrs{$name}) { + if ($localArgs{v}) { + print $fh "# MAY $objectclass \"$attr{desc}\" ($name)\n"; + } + elsif (!$localArgs{q}) { + print $fh "# MAY\n"; + } + if ("$name" eq "$parts[0]") { + print $fh "$name: $parts[1]\n"; + } + else { + print $fh "$name: \n"; + } + $attrs{$name} = 1; + } + } + } + } + } + else { + print $fh "# Server did not provide a schema (do you have permission?).\n"; + print $fh "$parts[0]: $parts[1]\n"; } } } ! if (!$attrs{$parts[0]}) { ! print $fh "$parts[0]: $parts[1]\n"; ! } ! ! close $fh; } ! $LASTFILE = undef; ! my $mtime = (stat($tempfile))[9]; my $cleanup = 1; # success => unlink $tempfile by default ! system("$Globals->{EDITOR}{VALUE} $tempfile"); if ( (stat($tempfile))[9] > $mtime ) { + if ($localArgs{p}) { + if (open(LDIF, "<", $tempfile)) { + my ($fh, $secondfile) = File::Temp::tempfile('LDAPShell_vi_XXXXXXXXXX', SUFFIX => '.ldif'); + while (<LDIF>) { + # Simplification of RFC 2849 + if (/^[0-9a-zA-Z][^:<]*(:|::|<)\W*$/) { + # is an attribute with an "empty" value + printf $fh "#%s", $_; + } + else { + printf $fh $_; + } + } + close(LDIF); + unlink $tempfile; + + close($fh); + $tempfile = $secondfile; + } + else { + printf STDERR "Warning: could not open file %s for pruning.\n", $tempfile; + } + } my $ldif = Net::LDAP::LDIF->new($tempfile, "r", onerror => undef); while (not $ldif->eof()) { *************** *** 2167,2170 **** --- 2221,2225 ---- } else { + $LASTFILE = $tempfile; print STDERR "LDIF stored in $tempfile\n\n"; } *************** *** 2194,2197 **** --- 2249,2253 ---- if ($cleanup) { if ($localArgs{k}) { + $LASTFILE = $tempfile; print STDERR "LDIF stored in $tempfile\n\n"; } *************** *** 2205,2208 **** --- 2261,2265 ---- } else { + $LASTFILE = $tempfile; print STDERR "LDIF stored in $tempfile\n\n"; } *************** *** 2458,2462 **** my $mtime = (stat($tempfile))[9]; ! system($Globals->{EDITOR}{VALUE}, $tempfile); # If modified, load as LDIF entry and compare to the same DNs in our directory (if they exist). --- 2515,2519 ---- my $mtime = (stat($tempfile))[9]; ! system("$Globals->{EDITOR}{VALUE} $tempfile"); # If modified, load as LDIF entry and compare to the same DNs in our directory (if they exist). *************** *** 2941,2944 **** --- 2998,3005 ---- B<Example>: C<$G = {ENTRIES => $ENTRIES, PWD => $PWD}; $G->{TIME} = time();> + =head2 $LASTFILE + + The name of the last file edited unsuccessfully (facilitates C<create -X>). + =head2 $LDAPCONN |