template-db2ldif.pl.in 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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}* | {-s include}* [{-x exclude}*] \n");
  44. print(STDERR " [-m] [-M] [-u] [-C] [-N] [-U] [-a filename]\n");
  45. print(STDERR " Opts: -D rootdn - Directory Manager\n");
  46. print(STDERR " : -w password - Directory Manager's password\n");
  47. print(STDERR " : -w - - Prompt for Directory Manager's password\n");
  48. print(STDERR " : -j filename - Read Directory Manager's password from file\n");
  49. print(STDERR " : -n instance - instance to be exported\n");
  50. print(STDERR " : -a filename - output ldif file\n");
  51. print(STDERR " : -s include - included suffix(es)\n");
  52. print(STDERR " : -x exclude - excluded suffix(es)\n");
  53. print(STDERR " : -m - minimal base64 encoding\n");
  54. print(STDERR " : -M - output ldif is stored in multiple files\n");
  55. print(STDERR " these files are named : <instance>_<filename>\n");
  56. print(STDERR " by default, all instances are stored in <filename>\n");
  57. print(STDERR " : -r - export replica\n");
  58. print(STDERR " : -u - do not export unique id\n");
  59. print(STDERR " : -C - use main db file only\n");
  60. print(STDERR " : -N - suppress printing sequential number\n");
  61. print(STDERR " : -U - output ldif is not folded\n");
  62. print(STDERR " : -E - Decrypt encrypted data when exporting\n");
  63. print(STDERR " : -1 - do not print version line\n");
  64. print(STDERR " : -v - verbose\n");
  65. }
  66. @instances = (
  67. "", "", "", "", "", "", "", "", "", "",
  68. "", "", "", "", "", "", "", "", "", "",
  69. "", "", "", "", "", "", "", "", "", "",
  70. "", "", "", "", "", "", "", "", "", "",
  71. "", "", "", "", "", "", "", "", "", "",
  72. ""
  73. );
  74. @included = (
  75. "", "", "", "", "", "", "", "", "", "",
  76. "", "", "", "", "", "", "", "", "", "",
  77. "", "", "", "", "", "", "", "", "", "",
  78. "", "", "", "", "", "", "", "", "", "",
  79. "", "", "", "", "", "", "", "", "", "",
  80. ""
  81. );
  82. @excluded = (
  83. "", "", "", "", "", "", "", "", "", "",
  84. "", "", "", "", "", "", "", "", "", "",
  85. "", "", "", "", "", "", "", "", "", "",
  86. "", "", "", "", "", "", "", "", "", "",
  87. "", "", "", "", "", "", "", "", "", "",
  88. ""
  89. );
  90. $maxidx = 50;
  91. $nowrap = 0;
  92. $nobase64 = 0;
  93. $noversion = 0;
  94. $nouniqueid = 0;
  95. $useid2entry = 0;
  96. $onefile = 1;
  97. $printkey = 1;
  98. $taskname = "";
  99. $ldiffile = "";
  100. $doreplica = 0;
  101. $prefix = "{{DS-ROOT}}";
  102. $ldifdir = "{{LDIF-DIR}}";
  103. $servid = "{{SERV-ID}}";
  104. $verbose = 0;
  105. $rootdn = "";
  106. $passwd = "";
  107. $passwdfile = "";
  108. $i = 0;
  109. $insti = 0;
  110. $incli = 0;
  111. $excli = 0;
  112. $decrypt_on_export = 0;
  113. while ($i <= $#ARGV) {
  114. if ( "$ARGV[$i]" eq "-n" ) { # instances
  115. $i++;
  116. if ($insti < $maxidx) {
  117. $instances[$insti] = $ARGV[$i]; $insti++;
  118. } else {
  119. &usage; exit(1);
  120. }
  121. } elsif ("$ARGV[$i]" eq "-s") { # included suffix
  122. $i++;
  123. if ($incli < $maxidx) {
  124. $included[$incli] = $ARGV[$i]; $incli++;
  125. } else {
  126. &usage; exit(1);
  127. }
  128. } elsif ("$ARGV[$i]" eq "-x") { # excluded suffix
  129. $i++;
  130. if ($excli < $maxidx) {
  131. $excluded[$excli] = $ARGV[$i]; $excli++;
  132. } else {
  133. &usage; exit(1);
  134. }
  135. } elsif ("$ARGV[$i]" eq "-a") { # ldif file
  136. $i++; $ldiffile = $ARGV[$i];
  137. } elsif ("$ARGV[$i]" eq "-D") { # Directory Manager
  138. $i++; $rootdn = $ARGV[$i];
  139. } elsif ("$ARGV[$i]" eq "-w") { # Directory Manager's password
  140. $i++; $passwd = $ARGV[$i];
  141. } elsif ("$ARGV[$i]" eq "-j") { # Read Directory Manager's password from a file
  142. $i++; $passwdfile = $ARGV[$i];
  143. } elsif ("$ARGV[$i]" eq "-M") { # multiple ldif file
  144. $onefile = 0;
  145. } elsif ("$ARGV[$i]" eq "-o") { # one ldif file
  146. $onefile = 1;
  147. } elsif ("$ARGV[$i]" eq "-u") { # no dump unique id
  148. $nouniqueid = 1;
  149. } elsif ("$ARGV[$i]" eq "-C") { # use id2entry
  150. $useid2entry = 1;
  151. } elsif ("$ARGV[$i]" eq "-N") { # does not print key
  152. $printkey = 0;
  153. } elsif ("$ARGV[$i]" eq "-r") { # export replica
  154. $doreplica = 1;
  155. } elsif ("$ARGV[$i]" eq "-m") { # no base64
  156. $nobase64 = 1;
  157. } elsif ("$ARGV[$i]" eq "-U") { # no wrap
  158. $nowrap = 1;
  159. } elsif ("$ARGV[$i]" eq "-1") { # no version line
  160. $noversion = 1;
  161. } elsif ("$ARGV[$i]" eq "-E") { # decrypt
  162. $decrypt_on_export = 1;
  163. } elsif ("$ARGV[$i]" eq "-v") { # verbose
  164. $verbose = 1;
  165. } else {
  166. &usage; exit(1);
  167. }
  168. $i++;
  169. }
  170. if ($passwdfile ne ""){
  171. # Open file and get the password
  172. unless (open (RPASS, $passwdfile)) {
  173. die "Error, cannot open password file $passwdfile\n";
  174. }
  175. $passwd = <RPASS>;
  176. chomp($passwd);
  177. close(RPASS);
  178. } elsif ($passwd eq "-"){
  179. # Read the password from terminal
  180. print "Bind Password: ";
  181. # Disable console echo
  182. system("stty -echo");
  183. # read the answer
  184. $passwd = <STDIN>;
  185. # Enable console echo
  186. system("stty echo");
  187. print "\n";
  188. chop($passwd); # trim trailing newline
  189. }
  190. if (($instances[0] eq "" && $included[0] eq "") || $rootdn eq "" || $passwd eq "") { &usage; exit(1); }
  191. ($s, $m, $h, $dy, $mn, $yr, $wdy, $ydy, $r) = localtime(time);
  192. $mn++; $yr += 1900;
  193. $taskname = "export_${yr}_${mn}_${dy}_${h}_${m}_${s}";
  194. $dn = "dn: cn=$taskname, cn=export, cn=tasks, cn=config\n";
  195. $misc = "changetype: add\nobjectclass: top\nobjectclass: extensibleObject\n";
  196. $cn = "cn: $taskname\n";
  197. $i = 0;
  198. $be = "";
  199. $nsinstance = "";
  200. while ("" ne "$instances[$i]") {
  201. $nsinstance = "${nsinstance}nsInstance: $instances[$i]\n";
  202. if ( "" eq "$be" ) {
  203. $be = "$instances[$i]";
  204. } else {
  205. $be = "${be}-$instances[$i]";
  206. }
  207. $i++;
  208. }
  209. $i = 0;
  210. $nsincluded = "";
  211. while ("" ne "$included[$i]") {
  212. $nsincluded = "${nsincluded}nsIncludeSuffix: $included[$i]\n";
  213. my ($rdn, $rest) = split(/,/, $included[$i]);
  214. my ($rest, $tmpbe) = split(/=/, $rdn);
  215. if ( "" eq "$be" ) {
  216. $be = "$tmpbe";
  217. } else {
  218. $be = "${be}-$tmpbe";
  219. }
  220. $i++;
  221. }
  222. $i = 0;
  223. $nsexcluded = "";
  224. while ("" ne "$excluded[$i]") {
  225. $nsexcluded = "${nsexcluded}nsExcludeSuffix: $excluded[$i]\n";
  226. $i++;
  227. }
  228. if ($ldiffile eq "") {
  229. if ($onefile == 0) {
  230. $ldiffile = "${ldifdir}{{SEP}}${servid}-${yr}_${mn}_${dy}_${h}_${m}_${s}.ldif";
  231. } else {
  232. $ldiffile = "${ldifdir}{{SEP}}${servid}-${be}-${yr}_${mn}_${dy}_${h}_${m}_${s}.ldif";
  233. }
  234. }
  235. $nsreplica = "";
  236. if ($doreplica != 0) { $nsreplica = "nsExportReplica: true\n"; }
  237. $nsnobase64 = "";
  238. if ($nobase64 != 0) { $nsnobase64 = "nsMinimalEncoding: true\n"; }
  239. $nsnowrap = "";
  240. if ($nowrap != 0) { $nsnowrap = "nsNoWrap: true\n"; }
  241. $nsnoversion = "";
  242. if ($noversion != 0) { $nsnoversion = "nsNoVersionLine: true\n"; }
  243. $nsnouniqueid = "";
  244. if ($nouniqueid != 0) { $nsnouniqueid = "nsDumpUniqId: false\n"; }
  245. $nsuseid2entry = "";
  246. if ($useid2entry != 0) { $nsuseid2entry = "nsUseId2Entry: true\n"; }
  247. $nsonefile = "";
  248. if ($onefile != 0) { $nsonefile = "nsUseOneFile: true\n"; }
  249. if ($onefile == 0) { $nsonefile = "nsUseOneFile: false\n"; }
  250. $nsexportdecrypt = "";
  251. if ($decrypt_on_export != 0) { $nsexportdecrypt = "nsExportDecrypt: true\n"; }
  252. $nsprintkey = "";
  253. if ($printkey == 0) { $nsprintkey = "nsPrintKey: false\n"; }
  254. $nsldiffile = "nsFilename: ${ldiffile}\n";
  255. $entry = "${dn}${misc}${cn}${nsinstance}${nsincluded}${nsexcluded}${nsreplica}${nsnobase64}${nsnowrap}${nsnoversion}${nsnouniqueid}${nsuseid2entry}${nsonefile}${nsexportdecrypt}${nsprintkey}${nsldiffile}";
  256. $vstr = "";
  257. if ($verbose != 0) { $vstr = "-v"; }
  258. $ENV{'PATH'} = "$prefix@ldapsdk_bindir@:$prefix/usr/bin:@ldapsdk_bindir@:/usr/bin";
  259. $ENV{'LD_LIBRARY_PATH'} = "$prefix@nss_libdir@:$prefix/usr/lib:@nss_libdir@:/usr/lib";
  260. $ENV{'SHLIB_PATH'} = "$prefix@nss_libdir@:$prefix/usr/lib:@nss_libdir@:/usr/lib";
  261. print("Exporting to ldif file: ${ldiffile}\n");
  262. open(FOO, "| ldapmodify $vstr -h {{SERVER-NAME}} -p {{SERVER-PORT}} -D \"$rootdn\" -w \"$passwd\" -a" );
  263. print(FOO "$entry");
  264. close(FOO);