| 1 | #!/usr/bin/php4 -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 | // Script de migration des données LDAP vers les bases MYSQL de mx/imap |
|---|
| 8 | # dépendances de ce script: |
|---|
| 9 | # php4-cgi + php4-ldap + php4-mysql pour la migration ldap=>mysql du mail |
|---|
| 10 | # donc le script "0.9.1_migrationldap.php" |
|---|
| 11 | |
|---|
| 12 | $config = "/var/alternc/bureau/class/local.php"; |
|---|
| 13 | $bar = @include($config); |
|---|
| 14 | if ($bar === FALSE) { |
|---|
| 15 | echo "cannot find the PHP config file: $config, aborting\n"; |
|---|
| 16 | exit(0); |
|---|
| 17 | } |
|---|
| 18 | |
|---|
| 19 | function graceful_failure() { |
|---|
| 20 | echo "assuming accounts have already been transfered\n"; |
|---|
| 21 | echo "if that is not the case:\n"; |
|---|
| 22 | echo " - make sure the LDAP server is running\n"; |
|---|
| 23 | echo " - make sure the login information is correct (in $config)\n"; |
|---|
| 24 | echo " - restart this script (".$_SERVER['argv'][0].")\n"; |
|---|
| 25 | exit(0); |
|---|
| 26 | } |
|---|
| 27 | |
|---|
| 28 | // On a chargé un fichier de local.php version antérieure, donc AVEC LDAP et SANS mysql_host (surement) |
|---|
| 29 | |
|---|
| 30 | // Connect to the ldap server |
|---|
| 31 | |
|---|
| 32 | if (!($ds=ldap_connect($L_LDAP_HOST))) { |
|---|
| 33 | echo "cannot connect to ldap server \"$L_LDAP_HOST\"\n"; |
|---|
| 34 | graceful_failure(); |
|---|
| 35 | } |
|---|
| 36 | |
|---|
| 37 | if (!(ldap_bind($ds,$L_LDAP_ROOT,$L_LDAP_ROOTPWD))) { |
|---|
| 38 | ldap_close($ds); |
|---|
| 39 | echo "cannot bind to ldap server \"$L_LDAP_HOST\" with user \"$L_LDAP_ROOT\"\n"; |
|---|
| 40 | graceful_failure(); |
|---|
| 41 | } |
|---|
| 42 | |
|---|
| 43 | // Connect to the mysql server |
|---|
| 44 | if (!mysql_connect($L_MYSQL_HOST,$L_MYSQL_LOGIN,$L_MYSQL_PWD)) { |
|---|
| 45 | echo "cannot connect to mysql server\n"; |
|---|
| 46 | return 1; |
|---|
| 47 | } |
|---|
| 48 | if (!mysql_select_db($L_MYSQL_DATABASE)) { |
|---|
| 49 | echo "cannot connect to mysql database\n"; |
|---|
| 50 | return 1; |
|---|
| 51 | } |
|---|
| 52 | |
|---|
| 53 | // Now enumerate the data for each base. |
|---|
| 54 | $sr=ldap_search($ds,"dc=domains,".$L_LDAP_POSTFIX,"(objectclass=mail)", |
|---|
| 55 | array("mail","uid","account","pop","type")); |
|---|
| 56 | $info = ldap_get_entries($ds, $sr); |
|---|
| 57 | if ($info["count"]==0) { |
|---|
| 58 | echo "INFO : Aucun mail dans la base DOMAINS \n"; |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | echo "Transferring ".$info["count"]." Entries from domains "; |
|---|
| 62 | for($i=0;$i<$info["count"];$i++) { |
|---|
| 63 | if ($info[$i]["type"][0]=="mail") $type=0; else $type=1; |
|---|
| 64 | |
|---|
| 65 | mysql_query("INSERT INTO mail_domain (mail,alias,uid,pop,type) VALUES ('". |
|---|
| 66 | addslashes($info[$i]["mail"][0])."','". |
|---|
| 67 | addslashes(join("\n", $info[$i]["account"]))."','". |
|---|
| 68 | addslashes($info[$i]["uid"][0])."','". |
|---|
| 69 | addslashes($info[$i]["pop"][0])."','$type');"); |
|---|
| 70 | if (($i/10.0)==intval($i/10)) { echo "."; flush(); } |
|---|
| 71 | } |
|---|
| 72 | echo " done\n"; |
|---|
| 73 | |
|---|
| 74 | $sr=ldap_search($ds,"dc=aliases,".$L_LDAP_POSTFIX,"(objectClass=alias)", |
|---|
| 75 | array("mail","alias")); |
|---|
| 76 | $info = ldap_get_entries($ds, $sr); |
|---|
| 77 | if ($info["count"]==0) { |
|---|
| 78 | echo "INFO : Aucun mail dans la base ALIASES \n"; |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | echo "Transferring ".$info["count"]." Entries from aliases "; |
|---|
| 82 | for($i=0;$i<$info["count"];$i++) { |
|---|
| 83 | mysql_query("INSERT INTO mail_alias (mail,alias) VALUES ('". |
|---|
| 84 | addslashes($info[$i]["mail"][0])."','". |
|---|
| 85 | addslashes($info[$i]["alias"][0])."');"); |
|---|
| 86 | if (($i/10.0)==intval($i/10)) { echo "."; flush(); } |
|---|
| 87 | } |
|---|
| 88 | echo " done\n"; |
|---|
| 89 | |
|---|
| 90 | |
|---|
| 91 | $sr=ldap_search($ds,"dc=users,".$L_LDAP_POSTFIX,"(objectClass=posixAccount)", |
|---|
| 92 | array("uid","gidNumber","homeDirectory","userPassword")); |
|---|
| 93 | $info = ldap_get_entries($ds, $sr); |
|---|
| 94 | if ($info["count"]==0) { |
|---|
| 95 | echo "INFO : Aucun mail dans la base USERS \n"; |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | |
|---|
| 99 | echo "Transferring ".$info["count"]." Entries from users "; |
|---|
| 100 | for($i=0;$i<$info["count"];$i++) { |
|---|
| 101 | // echo serialize($info[$i])."\n"; |
|---|
| 102 | $pass=substr($info[$i]["userpassword"][0],7); |
|---|
| 103 | mysql_query("INSERT INTO mail_users (uid,alias,path,password) VALUES ('". |
|---|
| 104 | addslashes($info[$i]["gidnumber"][0])."','". |
|---|
| 105 | addslashes($info[$i]["uid"][0])."','". |
|---|
| 106 | addslashes($info[$i]["homedirectory"][0])."','". |
|---|
| 107 | addslashes($pass)."');"); |
|---|
| 108 | if (($i/10.0)==intval($i/10)) { echo "."; flush(); } |
|---|
| 109 | } |
|---|
| 110 | echo " done\n"; |
|---|
| 111 | |
|---|
| 112 | mysql_close(); |
|---|
| 113 | ldap_close($ds); |
|---|
| 114 | |
|---|
| 115 | ?> |
|---|