gdipdbcnv.pl 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #!/usr/bin/perl
  2. #####################################################
  3. # gdipdbcnv.pl
  4. #
  5. # This script generates mySQL statements needed
  6. # complete the conversion of an earlier GnuDIP
  7. # mySQL database.
  8. #
  9. # See COPYING for licensing information.
  10. #
  11. #####################################################
  12. # Perl modules
  13. use strict;
  14. use DBD::mysql;
  15. # process command line
  16. sub usage {
  17. print STDERR <<"EOQ";
  18. usage: gdipdbcnv.pl gnudipdatabase gnudipserver gnudipuser gnudippass
  19. EOQ
  20. }
  21. if (@ARGV ne 4) {
  22. usage();
  23. exit 1;
  24. }
  25. my $gnudipdatabase = $ARGV[0];
  26. my $gnudipserver = $ARGV[1];
  27. my $gnudipuser = $ARGV[2];
  28. my $gnudippass = $ARGV[3];
  29. # use GnuDIP database
  30. tpr(qq*
  31. ######################################
  32. # use GnuDIP database
  33. use $gnudipdatabase;
  34. *);
  35. # connect to mySQL
  36. my $dbh = DBI->connect(
  37. "DBI:mysql:$gnudipdatabase:$gnudipserver", $gnudipuser, $gnudippass)
  38. || die "Could not connect to database\n";
  39. my $sth;
  40. # get preferences from the database
  41. my $pref = getprefs($dbh);
  42. # get list of domains
  43. my @domains = ();
  44. # pre 2.3 with domain in globalprefs?
  45. if ($$pref{'GNUDIP_DOMAIN'}) {
  46. my %domain;
  47. $domain{'domain'} = $$pref{'GNUDIP_DOMAIN'};
  48. $domain{'changepass'} = $$pref{'ALLOW_CHANGE_PASS'};
  49. $domain{'addself'} = $$pref{'ADD_SELF'};
  50. push(@domains, (\%domain));
  51. }
  52. # get from domains table
  53. $sth = $dbh->prepare(
  54. "select domain, changepass, addself from domains order by id"
  55. );
  56. $sth->execute;
  57. while (my $domain = $sth->fetchrow_hashref) {
  58. push(@domains, ($domain));
  59. }
  60. $sth->finish;
  61. # need to fix pre 2.3 database with domain in globalprefs?
  62. if ($$pref{'GNUDIP_DOMAIN'}) {
  63. tpr(qq*
  64. ######################################
  65. # domains
  66. delete from domains;
  67. *);
  68. foreach my $domain (@domains) {
  69. tpr(qq*
  70. insert into domains set
  71. domain = '$$domain{'domain'}',
  72. changepass = '$$domain{'changepass'}',
  73. addself = '$$domain{'addself'}'
  74. *);
  75. }
  76. tpr(qq*
  77. *);
  78. }
  79. # need to fix pre 2.2 database with global domain?
  80. if ($$pref{'DOMAIN_TYPE'} and
  81. $$pref{'DOMAIN_TYPE'} eq 'GLOBAL' and
  82. $$pref{'GNUDIP_DOMAIN'}) {
  83. tpr(qq*
  84. ######################################
  85. # users
  86. *);
  87. # for each non-ADMIN user entry with an empty domain ...
  88. my $columns =
  89. 'id, username, password, email, createdate, ' .
  90. 'forwardurl, updated, currentip, autourlon, MXvalue, ' .
  91. 'MXbackup, wildcard, allowwild, allowmx';
  92. $sth = $dbh->prepare(
  93. "select $columns from users where domain = '' and level <> 'ADMIN'");
  94. $sth->execute;
  95. while (my (
  96. $id, $username, $password, $email, $createdate,
  97. $forwardurl, $updated, $currentip, $autourlon, $MXvalue,
  98. $MXbackup, $wildcard, $allowwild, $allowmx
  99. ) = $sth->fetchrow_array) {
  100. # the current row gets the "global" domain
  101. tpr(qq*
  102. update user where id = '$id' set
  103. domain = '$$pref{'GNUDIP_DOMAIN'}';
  104. *);
  105. # then generate another row for each additional domain
  106. foreach my $domain (@domains) {
  107. tpr(qq*
  108. insert into users set
  109. username = '$username',
  110. password = '$password',
  111. domain = '$$domain{'domain'}',
  112. email = '$email',
  113. createdate = '$createdate',
  114. forwardurl = '$forwardurl',
  115. updated = '$updated',
  116. level = 'USER',
  117. currentip = '$currentip',
  118. autourlon = '$autourlon',
  119. MXvalue = '$MXvalue',
  120. MXbackup = '$MXbackup',
  121. wildcard = '$wildcard',
  122. allowwild = '$allowwild',
  123. allowmx = '$allowmx';
  124. *);
  125. }
  126. }
  127. }
  128. exit;
  129. #####################################################
  130. # subroutines
  131. #####################################################
  132. #####################################################
  133. # get preferences from database
  134. #####################################################
  135. sub getprefs {
  136. my $dbh = shift;
  137. my %PREF;
  138. my $sth = $dbh->prepare("select param, value from globalprefs");
  139. $sth->execute;
  140. while (my ($param, $value) = $sth->fetchrow_array) {
  141. $PREF{$param} = $value;
  142. }
  143. $sth->finish;
  144. return \%PREF;
  145. }
  146. #######################################################################
  147. # strip leading blank line from string
  148. #######################################################################
  149. sub tst {
  150. my $str = shift;
  151. $str =~ s /^\n//;
  152. return $str;
  153. }
  154. #######################################################################
  155. # print string with leading blank line removed
  156. #######################################################################
  157. sub tpr {
  158. print tst(shift);
  159. }