]> jfr.im git - irc/evilnet/x3.git/blob - tools/ldap/ldap_convert.php
adding some extra stuff we are using for ldap
[irc/evilnet/x3.git] / tools / ldap / ldap_convert.php
1 #!/usr/bin/php
2 <?
3 /*
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.
6 *
7 * Its expected you would modify and well-test this before running it on
8 * a production database. Use at your own risk!
9 *
10 * Edit the variables below first..
11 *
12 */
13 /* -------------------------------------------- */;
14 /* CONFIGURATION */
15 /* -------------------------------------------- */;
16
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 */
22
23 /* -------------------------------------------- */;
24
25 echo "------------------------------------------\n";
26 echo "X3 to LDAP dump script\n";
27 echo "Copyright (C) 2007 evilnet development\n";
28 echo "------------------------------------------\n\n";
29
30 if (!extension_loaded('ldap'))
31 die("PHP Extension LDAP MUST be loaded before using this script.\n");
32
33 $handle=fopen($db, r);
34 $ns = 0;
35 $add = 0;
36 $parse = 0;
37
38 if ($handle) {
39 echo "Connecting to ldap server\n";
40 $ds=ldap_connect($ldap_server);
41
42 if (!$ds)
43 die("Couldnt connect to ldap server\n");
44
45 echo "Switching to ldap protocol 3\n";
46 ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
47
48 echo "Binding to ldap server\n";
49 $r=ldap_bind($ds, $ldap_bind, $ldap_pass);
50 if (!$r)
51 die("LDAP bind error - ". ldap_error($ds) ."\n");
52
53 echo "Attempting to read $db\n";
54 while (!feof($handle)) {
55 $line = fgets($handle, 4096);
56 $line = trim($line);
57 $gotpass = 0;
58 $gotemail = 0;
59 $user = NULL;
60 $pass = NULL;
61 $email = NULL;
62 if ($line == "\"NickServ\" {") {
63 $ns = 1;
64 continue;
65 }
66
67 if ($line == "\"ChanServ\" {")
68 $ns = 0;
69
70 if ($ns == 1) {
71 $parse++;
72 $space = " ";
73 $exp = explode($space, $line);
74 $i = sizeof($exp);
75 $i--;
76 while ($exp[$i] != NULL) {
77 if (($exp[$i] == "\"passwd\"") && ($gotpass == 0)) {
78 $pass = $exp[$i+1];
79 $gotpass = 1;
80 }
81
82 if (($exp[$i] == "\"email_addr\"") && ($gotemail == 0)) {
83 $email = $exp[$i+1];
84 $gotemail = 1;
85 }
86 $i--;
87 }
88
89 $user = $exp[0];
90
91 $user = trim($user, "\";");
92 $pass = trim($pass, "\";");
93 $email = trim($email, "\";");
94 if ($user && $pass && $email && ($user != "}")) {
95 unset($info);
96
97 $info["objectclass"][] = "top";
98 $info["objectclass"][] = "inetOrgAnonAccount";
99 $info["uid"]=$user;
100 $info["mail"]=$email;
101 $info["userPassword"]='{MD5}'.base64_encode(pack('H*',$pass));
102
103 # $r=@ldap_add($ds, "uid=".$user.",$ldap_add", $info);
104 if ($r) {
105 $add++;
106 echo "Added $user (email: $email) (pass: $pass)\n";
107 print_r($info);
108 } else
109 echo "Failed adding $user (email: $email) (pass: $pass) - ". ldap_error($ds) ."\n";
110
111 } else if (!$user || !$pass || !$email) {
112 if (!$user && !$pass && !$email)
113 continue; /* misc bits after entries */
114
115 if (($user == "}") && !$pass && !$email)
116 continue; /* misc bits after entries */
117
118 echo "Missing fields from $db (User: $user Pass: $pass Email: $email)\n";
119 }
120
121 }
122 }
123 } else
124 die("Couldnt read $db\n");
125
126 echo "Disconnecting from ldap server\n";
127 ldap_close($ds);
128 $parse--;
129 $parse--;
130 echo "Processed $parse accounts.\n";
131 echo "Added $add accounts to the ldap server\n";
132
133 ?>