template-db2index.pl.in 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. die "The '-w -' option requires an extension library (Term::ReadKey) which is not\n",
  125. "part of the standard perl distribution. If you want to use it, you must\n",
  126. "download and install the module. You can find it at\n",
  127. "http://www.perl.com/CPAN/CPAN.html\n";
  128. # Remove the previous line and uncomment the following 6 lines once you have installed Term::ReadKey module.
  129. # use Term::ReadKey;
  130. # print "Bind Password: ";
  131. # ReadMode('noecho');
  132. # $passwd = ReadLine(0);
  133. # chomp($passwd);
  134. # ReadMode('normal');
  135. }
  136. if ( $rootdn eq "" || $passwd eq "" )
  137. {
  138. &usage;
  139. exit(1);
  140. }
  141. $vstr = "";
  142. if ($verbose != 0)
  143. {
  144. $vstr = "-v";
  145. }
  146. ($s, $m, $h, $dy, $mn, $yr, $wdy, $ydy, $r) = localtime(time);
  147. $mn++; $yr += 1900;
  148. $taskname = "db2index_${yr}_${mn}_${dy}_${h}_${m}_${s}";
  149. if ( $instance eq "" )
  150. {
  151. &usage;
  152. exit(1);
  153. }
  154. # No attribute name has been specified: let's get them from the configuration
  155. $attribute="";
  156. $indexes_list="";
  157. $vlvattribute="";
  158. $vlvindexes_list="";
  159. chdir("$prefix{{SEP}}usr{{SEP}}bin");
  160. if ( $attribute_arg eq "" && $vlvattribute_arg eq "" )
  161. {
  162. # Get the list of indexes from the entry
  163. $indexes_list="ldapsearch $vstr -h {{SERVER-NAME}} -p {{SERVER-PORT}} -D \"$rootdn\" -w \"$passwd\" -s one " .
  164. "-b \"cn=index,cn=\"$instance\", cn=ldbm database,cn=plugins,cn=config\" \"(&(objectclass=*)(nsSystemIndex=false))\" cn";
  165. # build the values of the attribute nsIndexAttribute
  166. open(LDAP1, "$indexes_list |");
  167. while (<LDAP1>) {
  168. s/\n //g;
  169. if (/^cn: (.*)\n/) {
  170. $IndexAttribute="nsIndexAttribute";
  171. $attribute="$attribute$IndexAttribute: $1\n";
  172. }
  173. }
  174. close(LDAP1);
  175. if ( $attribute eq "" )
  176. {
  177. # No attribute to index, just exit
  178. exit(0);
  179. }
  180. # Get the list of indexes from the entry
  181. $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";
  182. # build the values of the attribute nsIndexVlvAttribute
  183. open(LDAP1, "$vlvindexes_list |");
  184. while (<LDAP1>) {
  185. s/\n //g;
  186. if (/^cn: (.*)\n/) {
  187. $vlvIndexAttribute="nsIndexVlvAttribute";
  188. $vlvattribute="$vlvattribute$vlvIndexAttribute: $1\n";
  189. }
  190. }
  191. close(LDAP1);
  192. }
  193. else
  194. {
  195. if ( $attribute_arg ne "" )
  196. {
  197. $attribute="nsIndexAttribute: $attribute_arg\n";
  198. }
  199. if ( $vlvattribute_arg ne "" )
  200. {
  201. $vlvattribute="nsIndexVlvAttribute: $vlvattribute_arg\n";
  202. }
  203. }
  204. # Build the task entry to add
  205. $dn = "dn: cn=$taskname, cn=index, cn=tasks, cn=config\n";
  206. $misc = "changetype: add\nobjectclass: top\nobjectclass: extensibleObject\n";
  207. $cn = "cn: $taskname\n";
  208. $nsinstance = "nsInstance: ${instance}\n";
  209. $entry = "${dn}${misc}${cn}${nsinstance}${attribute}${vlvattribute}";
  210. open(FOO, "| ldapmodify $vstr -h {{SERVER-NAME}} -p {{SERVER-PORT}} -D \"$rootdn\" -w \"$passwd\" -a" );
  211. print(FOO "$entry");
  212. close(FOO);