From bc375de861791649d9dce67476d9d634824e5694 Mon Sep 17 00:00:00 2001 From: Noriko Hosoi Date: Wed, 3 Oct 2012 13:56:11 -0700 Subject: [PATCH] Trac Ticket #453 - db2index with -tattrname:type,type fails https://fedorahosted.org/389/ticket/453 Fix Description: 1) db2index and db2index.pl did not handle options with no space between the option type and the value. This patch properly parses them. 2) db2index_add_indexed_attr now converts the comma delimited list of index types into multiple values for the nsIndexType and nsMatchingRule. --- ldap/admin/src/scripts/template-db2index.in | 2 +- ldap/admin/src/scripts/template-db2index.pl.in | 89 +++++++++-------------- ldap/servers/slapd/back-ldbm/ldif2ldbm.c | 44 ++++++++++-- 3 files changed, 73 insertions(+), 62 deletions(-) diff --git a/ldap/admin/src/scripts/template-db2index.in b/ldap/admin/src/scripts/template-db2index.in index 3dc4740..ba39c17 100755 --- a/ldap/admin/src/scripts/template-db2index.in +++ b/ldap/admin/src/scripts/template-db2index.in @@ -23,7 +23,7 @@ if [ $# -eq 0 ] then bak_dir={{BAK-DIR}}/reindex_`date +%Y_%m_%d_%H_%M_%S` ./ns-slapd upgradedb -D {{CONFIG-DIR}} -f -a "$bak_dir" -elif [ $# -lt 4 ] +elif [ $# -lt 2 ] then echo "Usage: db2index [-n backend_instance | {-s includesuffix}* -t attribute[:indextypes[:matchingrules]] -T vlvattribute]" exit 1 diff --git a/ldap/admin/src/scripts/template-db2index.pl.in b/ldap/admin/src/scripts/template-db2index.pl.in index e598b05..46a7ee5 100644 --- a/ldap/admin/src/scripts/template-db2index.pl.in +++ b/ldap/admin/src/scripts/template-db2index.pl.in @@ -39,6 +39,13 @@ # END COPYRIGHT BLOCK # +use Getopt::Std; + +$opt_D = "{{ROOT-DN}}"; +$opt_p = "{{SERVER-PORT}}"; +$opt_h = "{{SERVER-NAME}}"; +$opt_v = 0; + sub usage { print(STDERR "Usage: $0 [-v] -D rootdn { -w password | -w - | -j filename } \n"); print(STDERR " -n instance [-t attributeName[:indextypes[:matchingrules]]]\n"); @@ -56,6 +63,8 @@ sub usage { print(STDERR " Example: -t foo:eq,pres\n"); print(STDERR " : -T vlvAttributeName - vlvAttributeName: name of the vlv attribute to be indexed\n"); print(STDERR " : -v - verbose\n"); + + exit 1; } $instance = ""; @@ -77,51 +86,25 @@ libpath_add("/usr/lib"); $ENV{'SHLIB_PATH'} = "$ENV{'LD_LIBRARY_PATH'}"; -$i = 0; -while ($i <= $#ARGV) -{ - if ("$ARGV[$i]" eq "-n") - { - # instance - $i++; $instance = $ARGV[$i]; - } - elsif ("$ARGV[$i]" eq "-D") - { - # Directory Manager - $i++; $rootdn = $ARGV[$i]; - } - elsif ("$ARGV[$i]" eq "-w") - { - # Directory Manager's password - $i++; $passwd = $ARGV[$i]; - } - elsif ("$ARGV[$i]" eq "-j") - { - # Read Directory Manager's password from a file - $i++; $passwdfile = $ARGV[$i]; - } - elsif ("$ARGV[$i]" eq "-t") - { - # Attribute to index - $i++; $attribute_arg = $ARGV[$i]; - } - elsif ("$ARGV[$i]" eq "-T") - { - # Vlvattribute to index - $i++; $vlvattribute_arg = $ARGV[$i]; - } - elsif ("$ARGV[$i]" eq "-v") - { - # verbose - $verbose = 1; - } - else - { - &usage; exit(1); - } - $i++; +usage() if (!getopts('n:D:w:T:t:j:v')); + +if ($opt_j ne ""){ + die "Error, cannot open password file $opt_j\n" unless (open (RPASS, $opt_j)); + $opt_w = ; + chomp($opt_w); + close(RPASS); } +usage() if( $opt_w eq "" ); +usage() if( $#ARGV ge 0 ); + +$instance = $opt_n; +$rootdn = $opt_D; +$passwd = $opt_w; +$attribute_arg = $opt_t; +$vlvattribute_arg = $opt_T; +$verbose = $opt_v; + if ($passwdfile ne ""){ # Open file and get the password unless (open (RPASS, $passwdfile)) { @@ -132,21 +115,20 @@ if ($passwdfile ne ""){ close(RPASS); } elsif ($passwd eq "-"){ # Read the password from terminal - print "Bind Password: "; - # Disable console echo - system("@sttyexec@ -echo") if -t STDIN; - # read the answer - $passwd = ; - # Enable console echo - system("@sttyexec@ echo") if -t STDIN; - print "\n"; - chop($passwd); # trim trailing newline + print "Bind Password: "; + # Disable console echo + system("@sttyexec@ -echo") if -t STDIN; + # read the answer + $passwd = ; + # Enable console echo + system("@sttyexec@ echo") if -t STDIN; + print "\n"; + chop($passwd); # trim trailing newline } if ( $rootdn eq "" || $passwd eq "" ) { &usage; - exit(1); } $vstr = ""; @@ -162,7 +144,6 @@ $taskname = "db2index_${yr}_${mn}_${dy}_${h}_${m}_${s}"; if ( $instance eq "" ) { &usage; - exit(1); } # No attribute name has been specified: let's get them from the configuration diff --git a/ldap/servers/slapd/back-ldbm/ldif2ldbm.c b/ldap/servers/slapd/back-ldbm/ldif2ldbm.c index c802ff2..d005c8b 100644 --- a/ldap/servers/slapd/back-ldbm/ldif2ldbm.c +++ b/ldap/servers/slapd/back-ldbm/ldif2ldbm.c @@ -2494,6 +2494,8 @@ db2index_add_indexed_attr(backend *be, char *attrString) Slapi_Entry *e; struct berval *vals[2]; struct berval val; + char *ptr, *q; + char *next, *last; vals[0] = &val; vals[1] = NULL; @@ -2508,6 +2510,7 @@ db2index_add_indexed_attr(backend *be, char *attrString) /* set the index name */ val.bv_val = attrString+1; val.bv_len = strlen(attrString); + /* bv_val is copied into the entry. */ slapi_entry_add_values(e,"cn",vals); if (NULL != (mptr = strchr(iptr, ':'))) { @@ -2516,15 +2519,42 @@ db2index_add_indexed_attr(backend *be, char *attrString) } /* set the index type */ - val.bv_val = iptr; - val.bv_len = strlen(iptr); - slapi_entry_add_values(e,"nsIndexType",vals); + last = iptr + strlen(iptr); + for (ptr = strtok_r(iptr, ",", &next); + ptr; + ptr = strtok_r(NULL, ",", &next)) { + while (isblank(*ptr) && (ptr < last)) { /* trim leading blanks */ + ptr++; + } + q = ptr + strlen(ptr) - 1; + while (isblank(*q) && (q > ptr)) { /* trim trailing blanks */ + q--; + } + *(q + 1) = '\0'; + val.bv_val = ptr; + val.bv_len = strlen(ptr); + /* bv_val is copied into the entry. */ + slapi_entry_add_values(e,"nsIndexType",vals); + } if (NULL != mptr) { - /* set the matching rule */ - val.bv_val = mptr; - val.bv_len = strlen(mptr); - slapi_entry_add_values(e,"nsMatchingRule",vals); + last = mptr + strlen(mptr); + for (ptr = strtok_r(mptr, ",", &next); + ptr; + ptr = strtok_r(NULL, ",", &next)) { + while (isblank(*ptr) && (ptr < last)) { /* trim leading blanks */ + ptr++; + } + q = ptr + strlen(ptr) - 1; + while (isblank(*q) && (q > ptr)) { /* trim trailing blanks */ + q--; + } + *(q + 1) = '\0'; + val.bv_val = ptr; + val.bv_len = strlen(ptr); + /* bv_val is copied into the entry. */ + slapi_entry_add_values(e,"nsMatchingRule",vals); + } } attr_index_config(be, "from db2index()", 0, e, 0, 0); -- 1.7.7.6