template-db2index.pl.in 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. #{{PERL-EXEC}}
  2. #
  3. # BEGIN COPYRIGHT BLOCK
  4. # This Program is free software; you can redistribute it and/or modify it under
  5. # the terms of the GNU General Public License as published by the Free Software
  6. # Foundation; version 2 of the License.
  7. #
  8. # This Program is distributed in the hope that it will be useful, but WITHOUT
  9. # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  10. # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  11. #
  12. # You should have received a copy of the GNU General Public License along with
  13. # this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  14. # Place, Suite 330, Boston, MA 02111-1307 USA.
  15. #
  16. # In addition, as a special exception, Red Hat, Inc. gives You the additional
  17. # right to link the code of this Program with code not covered under the GNU
  18. # General Public License ("Non-GPL Code") and to distribute linked combinations
  19. # including the two, subject to the limitations in this paragraph. Non-GPL Code
  20. # permitted under this exception must only link to the code of this Program
  21. # through those well defined interfaces identified in the file named EXCEPTION
  22. # found in the source code files (the "Approved Interfaces"). The files of
  23. # Non-GPL Code may instantiate templates or use macros or inline functions from
  24. # the Approved Interfaces without causing the resulting work to be covered by
  25. # the GNU General Public License. Only Red Hat, Inc. may make changes or
  26. # additions to the list of Approved Interfaces. You must obey the GNU General
  27. # Public License in all respects for all of the Program code and other code used
  28. # in conjunction with the Program except the Non-GPL Code covered by this
  29. # exception. If you modify this file, you may extend this exception to your
  30. # version of the file, but you are not obligated to do so. If you do not wish to
  31. # provide this exception without modification, you must delete this exception
  32. # statement from your version and license this file solely under the GPL without
  33. # exception.
  34. #
  35. #
  36. # Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  37. # Copyright (C) 2005 Red Hat, Inc.
  38. # All rights reserved.
  39. # END COPYRIGHT BLOCK
  40. #
  41. sub usage {
  42. print(STDERR "Usage: $0 [-v] -D rootdn { -w password | -w - | -j filename } \n");
  43. print(STDERR " -n instance [-t attributeName[:indextypes[:matchingrules]]]\n");
  44. print(STDERR " Opts: -D rootdn - Directory Manager\n");
  45. print(STDERR " : -w password - Directory Manager's password\n");
  46. print(STDERR " : -w - - Prompt for Directory Manager's password\n");
  47. print(STDERR " : -j filename - Read Directory Manager's password from file\n");
  48. print(STDERR " : -n instance - instance to be indexed\n");
  49. print(STDERR " : -t attributeName[:indextypes[:matchingrules]]\n");
  50. print(STDERR " - attributeName: name of the attribute to be indexed\n");
  51. print(STDERR " If omitted, all the indexes defined \n");
  52. print(STDERR " for that instance are generated.\n");
  53. print(STDERR " - indextypes: comma separated index types\n");
  54. print(STDERR " - matchingrules: comma separated matrules\n");
  55. print(STDERR " Example: -t foo:eq,pres\n");
  56. print(STDERR " : -T vlvAttributeName - vlvAttributeName: name of the vlv attribute to be indexed\n");
  57. print(STDERR " : -v - verbose\n");
  58. }
  59. $instance = "";
  60. $rootdn = "";
  61. $passwd = "";
  62. $passwdfile = "";
  63. $attribute_arg = "";
  64. $vlvattribute_arg = "";
  65. $verbose = 0;
  66. $prefix = "{{DS-ROOT}}";
  67. $ENV{'PATH'} = "$prefix@ldapsdk_bindir@:$prefix/usr/bin:@ldapsdk_bindir@:/usr/bin";
  68. $ENV{'LD_LIBRARY_PATH'} = "$prefix@nss_libdir@:$prefix/usr/lib:@nss_libdir@:/usr/lib";
  69. $ENV{'SHLIB_PATH'} = "$prefix@nss_libdir@:$prefix/usr/lib:@nss_libdir@:/usr/lib";
  70. $i = 0;
  71. while ($i <= $#ARGV)
  72. {
  73. if ("$ARGV[$i]" eq "-n")
  74. {
  75. # instance
  76. $i++; $instance = $ARGV[$i];
  77. }
  78. elsif ("$ARGV[$i]" eq "-D")
  79. {
  80. # Directory Manager
  81. $i++; $rootdn = $ARGV[$i];
  82. }
  83. elsif ("$ARGV[$i]" eq "-w")
  84. {
  85. # Directory Manager's password
  86. $i++; $passwd = $ARGV[$i];
  87. }
  88. elsif ("$ARGV[$i]" eq "-j")
  89. {
  90. # Read Directory Manager's password from a file
  91. $i++; $passwdfile = $ARGV[$i];
  92. }
  93. elsif ("$ARGV[$i]" eq "-t")
  94. {
  95. # Attribute to index
  96. $i++; $attribute_arg = $ARGV[$i];
  97. }
  98. elsif ("$ARGV[$i]" eq "-T")
  99. {
  100. # Vlvattribute to index
  101. $i++; $vlvattribute_arg = $ARGV[$i];
  102. }
  103. elsif ("$ARGV[$i]" eq "-v")
  104. {
  105. # verbose
  106. $verbose = 1;
  107. }
  108. else
  109. {
  110. &usage; exit(1);
  111. }
  112. $i++;
  113. }
  114. if ($passwdfile ne ""){
  115. # Open file and get the password
  116. unless (open (RPASS, $passwdfile)) {
  117. die "Error, cannot open password file $passwdfile\n";
  118. }
  119. $passwd = <RPASS>;
  120. chomp($passwd);
  121. close(RPASS);
  122. } elsif ($passwd eq "-"){
  123. # Read the password from terminal
  124. print "Bind Password: ";
  125. # Disable console echo
  126. system("stty -echo");
  127. # read the answer
  128. $passwd = <STDIN>;
  129. # Enable console echo
  130. system("stty echo");
  131. print "\n";
  132. chop($passwd); # trim trailing newline
  133. }
  134. if ( $rootdn eq "" || $passwd eq "" )
  135. {
  136. &usage;
  137. exit(1);
  138. }
  139. $vstr = "";
  140. if ($verbose != 0)
  141. {
  142. $vstr = "-v";
  143. }
  144. ($s, $m, $h, $dy, $mn, $yr, $wdy, $ydy, $r) = localtime(time);
  145. $mn++; $yr += 1900;
  146. $taskname = "db2index_${yr}_${mn}_${dy}_${h}_${m}_${s}";
  147. if ( $instance eq "" )
  148. {
  149. &usage;
  150. exit(1);
  151. }
  152. # No attribute name has been specified: let's get them from the configuration
  153. $attribute="";
  154. $indexes_list="";
  155. $vlvattribute="";
  156. $vlvindexes_list="";
  157. chdir("$prefix{{SEP}}usr{{SEP}}bin");
  158. if ( $attribute_arg eq "" && $vlvattribute_arg eq "" )
  159. {
  160. # Get the list of indexes from the entry
  161. $indexes_list="ldapsearch $vstr -h {{SERVER-NAME}} -p {{SERVER-PORT}} -D \"$rootdn\" -w \"$passwd\" -s one " .
  162. "-b \"cn=index,cn=\"$instance\", cn=ldbm database,cn=plugins,cn=config\" \"(&(objectclass=*)(nsSystemIndex=false))\" cn";
  163. # build the values of the attribute nsIndexAttribute
  164. open(LDAP1, "$indexes_list |");
  165. while (<LDAP1>) {
  166. s/\n //g;
  167. if (/^cn: (.*)\n/) {
  168. $IndexAttribute="nsIndexAttribute";
  169. $attribute="$attribute$IndexAttribute: $1\n";
  170. }
  171. }
  172. close(LDAP1);
  173. if ( $attribute eq "" )
  174. {
  175. # No attribute to index, just exit
  176. exit(0);
  177. }
  178. # Get the list of indexes from the entry
  179. $vlvindexes_list="ldapsearch $vstr -h {{SERVER-NAME}} -p {{SERVER-PORT}} -D \"$rootdn\" -w \"$passwd\" -s sub -b \"cn=\"$instance\", cn=ldbm database,cn=plugins,cn=config\" \"objectclass=vlvIndex\" cn";
  180. # build the values of the attribute nsIndexVlvAttribute
  181. open(LDAP1, "$vlvindexes_list |");
  182. while (<LDAP1>) {
  183. s/\n //g;
  184. if (/^cn: (.*)\n/) {
  185. $vlvIndexAttribute="nsIndexVlvAttribute";
  186. $vlvattribute="$vlvattribute$vlvIndexAttribute: $1\n";
  187. }
  188. }
  189. close(LDAP1);
  190. }
  191. else
  192. {
  193. if ( $attribute_arg ne "" )
  194. {
  195. $attribute="nsIndexAttribute: $attribute_arg\n";
  196. }
  197. if ( $vlvattribute_arg ne "" )
  198. {
  199. $vlvattribute="nsIndexVlvAttribute: $vlvattribute_arg\n";
  200. }
  201. }
  202. # Build the task entry to add
  203. $dn = "dn: cn=$taskname, cn=index, cn=tasks, cn=config\n";
  204. $misc = "changetype: add\nobjectclass: top\nobjectclass: extensibleObject\n";
  205. $cn = "cn: $taskname\n";
  206. $nsinstance = "nsInstance: ${instance}\n";
  207. $entry = "${dn}${misc}${cn}${nsinstance}${attribute}${vlvattribute}";
  208. open(FOO, "| ldapmodify $vstr -h {{SERVER-NAME}} -p {{SERVER-PORT}} -D \"$rootdn\" -w \"$passwd\" -a" );
  209. print(FOO "$entry");
  210. close(FOO);