4 * This script is used to export users from x3.db to an ldap server
5 * when initially converting to x3's ldap based authentication.
7 * Its expected you would modify and well-test this before running it on
8 * a production database. Use at your own risk!
10 * Edit the variables below first..
13 /* -------------------------------------------- */;
15 /* -------------------------------------------- */;
17 $db = "/home/you/x3/x3.db";
18 $ldap_server = "localhost";
19 $ldap_bind = "cn=admin,dc=afternet,dc=org";
20 $ldap_pass = "yourpassword";
21 $ldap_add = "ou=Users,dc=afternet,dc=org"; /* excludes the uid= part on purpose, dont add in */
23 /* -------------------------------------------- */;
25 echo "------------------------------------------\n";
26 echo "X3 to LDAP dump script\n";
27 echo "Copyright (C) 2007 evilnet development\n";
28 echo "------------------------------------------\n\n";
30 if (!extension_loaded('ldap'))
31 die("PHP Extension LDAP MUST be loaded before using this script.\n");
33 $handle=fopen($db, r
);
40 echo "Connecting to ldap server\n";
41 $ds=ldap_connect($ldap_server);
44 die("Couldnt connect to ldap server\n");
46 echo "Switching to ldap protocol 3\n";
47 ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION
, 3);
49 echo "Binding to ldap server\n";
50 $r=ldap_bind($ds, $ldap_bind, $ldap_pass);
52 die("LDAP bind error - ". ldap_error($ds) ."\n");
54 echo "Attempting to read $db\n";
55 while (!feof($handle)) {
56 $line = fgets($handle, 4096);
63 if ($line == "\"NickServ\" {") {
68 if ($line == "\"ChanServ\" {")
74 $exp = explode($space, $line);
77 while ($exp[$i] != NULL) {
78 if (($exp[$i] == "\"passwd\"") && ($gotpass == 0)) {
83 if (($exp[$i] == "\"email_addr\"") && ($gotemail == 0)) {
92 $user = trim($user, "\";");
93 $pass = trim($pass, "\";");
94 $email = trim($email, "\";");
95 if ($user && $pass && $email && ($user != "}")) {
98 $info["objectclass"][] = "top";
99 $info["objectclass"][] = "inetOrgAnonAccount";
101 $info["mail"]=$email;
102 if ($pass[0] == "$") {
103 $info["userPassword
"] = "";
104 echo "ALERT
: $user ADDED WITH NO
PASSWORD (old crypt style
)\n";
108 $info["userPassword
"]='{MD5}'.base64_encode(pack('H*',$pass));
110 $r=@ldap_add($ds, "uid
=".$user.",$ldap_add", $info);
113 echo "Added $user (email: $email) (pass: $pass)\n";
116 echo "Failed adding $user (email: $email) (pass: $pass) - ". ldap_error($ds) ."\n";
118 } else if (!$user || !$pass || !$email) {
119 if (!$user && !$pass && !$email)
120 continue; /* misc bits after entries */
122 if (($user == "}") && !$pass && !$email)
123 continue; /* misc bits after entries */
125 echo "Missing fields from $db (User: $user Pass: $pass Email: $email)\n";
131 die("Couldnt read $db\n");
133 echo "Disconnecting from ldap server\n";
137 echo "Processed $parse accounts.\n";
138 echo "Added $add accounts to the ldap server\n";
139 if (($alert == 1) && ($np > 0))
140 echo "ALERT: $np ACCOUNTS ADDED WITH NO PASSWORD (old crypt style)\n";