Alternc  3.2
Alternc logiel libre pour l'hébergement
 All Data Structures Namespaces Files Functions Variables Pages
0.9.2.php
Go to the documentation of this file.
1 #!/usr/bin/php -q
2 <?php
3 
4 // Set the available memory to a large enough variable to be able to handle quite busy servers :)
5 ini_set("memory_limit","128M");
6 
7 /**
8  * Le but de ce script est deux choses:
9  *
10  * - migration des données LDAP vers les bases MYSQL de mx/imap
11  * - conversion des mots de passe en "crypt"
12  *
13  * dépendances de ce script:
14  * php4-cgi + php4-ldap + php4-mysql pour la migration ldap=>mysql du mail
15  * donc le script "0.9.1_migrationldap.php"
16  *
17  */
18 
19 /**
20  * for _md5cr()
21  */
22 require_once('/var/alternc/bureau/class/functions.php');
23 require_once('/var/alternc/bureau/class/config_nochk.php');
24 
25 $config = "/var/alternc/bureau/class/local.php";
26 $bar = @include($config);
27 if ($bar === FALSE) {
28  echo "cannot find the PHP config file: $config, aborting\n";
29  exit(0);
30 }
31 
32 /**
33  * sortir sans erreur et avec des instructions pour l'usager
34  */
35 function graceful_failure() {
36  global $config;
37  echo "assuming accounts have already been transfered\n";
38  echo "if that is not the case:\n";
39  echo " - make sure the LDAP server is running\n";
40  echo " - make sure the login information is correct (in $config)\n";
41  echo " - restart this script (".$_SERVER['argv'][0].")\n";
42  exit(0);
43 }
44 
45 // premiere etape
46 echo "Step 1: converting the LDAP database to MySQL, hold on\n";
47 echo " a dot (.) is printed for each 10 successful request\n";
48 echo " a X is printed for each failed request.\n";
49 echo " Usually, those failed requests occur when an entry is already present\n";
50 echo " in the database and can generally be ignored\n";
51 
52 // On a chargé un fichier de local.php version antérieure, donc AVEC LDAP et SANS mysql_host (surement)
53 
54 // Connect to the ldap server
55 
56 if (function_exists("ldap_connect")) {
57 if (!($ds=ldap_connect($L_LDAP_HOST))) {
58  echo "cannot connect to ldap server \"$L_LDAP_HOST\"\n";
60 }
61 
62 if (!(ldap_bind($ds,$L_LDAP_ROOT,$L_LDAP_ROOTPWD))) {
63  ldap_close($ds);
64  echo "cannot bind to ldap server \"$L_LDAP_HOST\" with user \"$L_LDAP_ROOT\"\n";
66 }
67 
68 // Connect to the mysql server
69 // errors here are fatal
70 if (!mysql_connect($L_MYSQL_HOST,$L_MYSQL_LOGIN,$L_MYSQL_PWD)) {
71  echo "cannot connect to mysql server\n";
72  return 1;
73 }
74 if (!mysql_select_db($L_MYSQL_DATABASE)) {
75  echo "cannot connect to mysql database\n";
76  return 1;
77 }
78 
79 // Now enumerate the data for each base.
80 $sr=ldap_search($ds,"dc=domains,".$L_LDAP_POSTFIX,"(objectclass=mail)",
81  array("mail","uid","account","pop","type"));
82 $info = ldap_get_entries($ds, $sr);
83 if ($info["count"]==0) {
84  echo "INFO : Aucun mail dans la base DOMAINS \n";
85 }
86 
87 echo "Transferring ".$info["count"]." Entries from domains ";
88 for($i=0;$i<$info["count"];$i++) {
89  if ($info[$i]["type"][0]=="mail") $type=0; else $type=1;
90 
91  if (count($info[$i]["account"]) > 1) {
92  unset($info[$i]["account"]['count']);
93  $accounts = join("\n", $info[$i]["account"]);
94  }
95  mysql_query("INSERT INTO mail_domain (mail,alias,uid,pop,type) VALUES ('".
96  addslashes($info[$i]["mail"][0])."','".
97  addslashes($accounts)."','".
98  addslashes($info[$i]["uid"][0])."','".
99  addslashes($info[$i]["pop"][0])."','$type');") || print "X";
100  if (($i/10.0)==intval($i/10)) { echo "."; flush(); }
101 }
102 echo " done\n";
103 
104 $sr=ldap_search($ds,"dc=aliases,".$L_LDAP_POSTFIX,"(objectClass=alias)",
105  array("mail","alias"));
106 $info = ldap_get_entries($ds, $sr);
107 if ($info["count"]==0) {
108  echo "INFO : Aucun mail dans la base ALIASES \n";
109 }
110 
111 echo "Transferring ".$info["count"]." Entries from aliases ";
112 for($i=0;$i<$info["count"];$i++) {
113  mysql_query("INSERT INTO mail_alias (mail,alias) VALUES ('".
114  addslashes($info[$i]["mail"][0])."','".
115  addslashes($info[$i]["alias"][0])."');") || print "X";
116  if (($i/10.0)==intval($i/10)) { echo "."; flush(); }
117 }
118 echo " done\n";
119 
120 
121 $sr=ldap_search($ds,"dc=users,".$L_LDAP_POSTFIX,"(objectClass=posixAccount)",
122  array("uid","gidNumber","homeDirectory","userPassword"));
123 $info = ldap_get_entries($ds, $sr);
124 if ($info["count"]==0) {
125  echo "INFO : Aucun mail dans la base USERS \n";
126 }
127 
128 
129 echo "Transferring ".$info["count"]." Entries from users ";
130 for($i=0;$i<$info["count"];$i++) {
131  // echo serialize($info[$i])."\n";
132  $pass=substr($info[$i]["userpassword"][0],7);
133  mysql_query("INSERT INTO mail_users (uid,alias,path,password) VALUES ('".
134  addslashes($info[$i]["gidnumber"][0])."','".
135  addslashes($info[$i]["uid"][0])."','".
136  addslashes($info[$i]["homedirectory"][0])."','".
137  addslashes($pass)."');") || print "X";
138  if (($i/10.0)==intval($i/10)) { echo "."; flush(); }
139 }
140 echo " done\n";
141 ldap_close($ds);
142 
143 } else {
144  echo "ldap module not loaded into php, skipping LDAP conversion\n";
145 }
146 
147 echo "Step 2: encrypting user passwords ";
148 
149 if (!mysql_query("use $L_MYSQL_DATABASE")) {
150  echo "can't select database $L_MYSQL_DATABASE\n";
151 }
152 
153 if ($q = mysql_query("SELECT LENGTH(`pass`) AS len FROM `membres` GROUP BY len ORDER BY len ASC;")) {
154  if ($res = mysql_fetch_array($q)) {
155  if ($res['len'] == 34) {
156  print "(already encrypted)";
157  } else {
158  if (!($q = mysql_query("SELECT uid,pass FROM membres;"))) {
159  echo "SELECT failed: " . mysql_error() . "\n";
160  }
161 
162  while ($c = mysql_fetch_array($q)) {
163  $pass=_md5cr($c['pass']);
164  $id=$c['uid'];
165  echo "membre $id\n";
166  if (!mysql_query("UPDATE membres SET pass='$pass' WHERE uid='$id';")) {
167  echo "UPDATE failed: " . mysql_error() . "\n";
168  } else {
169  echo "."; flush();
170  }
171  }
172  }
173  } else {
174  echo "fetch_array() failed: ". mysql_error()."\n";
175  }
176 } else {
177  echo "query failed: ". mysql_error()."\n";
178 }
179 echo "\n";
180 
181 mysql_close();
182 
183 ?>