]> jfr.im git - irc/evilnet/x3.git/blame - tools/ldap/ldap_convert.php
mod-python: generalised the setting of the PYTHONPATH environment variable
[irc/evilnet/x3.git] / tools / ldap / ldap_convert.php
CommitLineData
bf754776
AS
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
25echo "------------------------------------------\n";
26echo "X3 to LDAP dump script\n";
27echo "Copyright (C) 2007 evilnet development\n";
28echo "------------------------------------------\n\n";
29
30if (!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;
03fa9256 35$bs = 0;
bf754776
AS
36$add = 0;
37$parse = 0;
38
39if ($handle) {
40 echo "Connecting to ldap server\n";
41 $ds=ldap_connect($ldap_server);
42
43 if (!$ds)
44 die("Couldnt connect to ldap server\n");
45
46 echo "Switching to ldap protocol 3\n";
47 ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
48
49 echo "Binding to ldap server\n";
50 $r=ldap_bind($ds, $ldap_bind, $ldap_pass);
51 if (!$r)
52 die("LDAP bind error - ". ldap_error($ds) ."\n");
53
54 echo "Attempting to read $db\n";
55 while (!feof($handle)) {
56 $line = fgets($handle, 4096);
57 $line = trim($line);
58 $gotpass = 0;
59 $gotemail = 0;
60 $user = NULL;
61 $pass = NULL;
62 $email = NULL;
03fa9256 63 if (($line == "\"NickServ\" {") && ($bs == 0)) {
64 echo "SSTARTT\n";
bf754776
AS
65 $ns = 1;
66 continue;
67 }
68
03fa9256 69 if ($line == "\"ChanServ\" {") {
70 $bs = 1;
bf754776 71 $ns = 0;
03fa9256 72 }
bf754776
AS
73
74 if ($ns == 1) {
75 $parse++;
76 $space = " ";
77 $exp = explode($space, $line);
78 $i = sizeof($exp);
79 $i--;
80 while ($exp[$i] != NULL) {
81 if (($exp[$i] == "\"passwd\"") && ($gotpass == 0)) {
82 $pass = $exp[$i+1];
83 $gotpass = 1;
84 }
85
86 if (($exp[$i] == "\"email_addr\"") && ($gotemail == 0)) {
87 $email = $exp[$i+1];
88 $gotemail = 1;
89 }
90 $i--;
91 }
92
93 $user = $exp[0];
94
95 $user = trim($user, "\";");
96 $pass = trim($pass, "\";");
97 $email = trim($email, "\";");
98 if ($user && $pass && $email && ($user != "}")) {
99 unset($info);
100
101 $info["objectclass"][] = "top";
102 $info["objectclass"][] = "inetOrgAnonAccount";
103 $info["uid"]=$user;
104 $info["mail"]=$email;
6cba6239 105 if ($pass[0] == "$") {
106 $info["userPassword"] = "";
107 echo "ALERT: $user ADDED WITH NO PASSWORD (old crypt style)\n";
6cba6239 108 } else
109 $info["userPassword"]='{MD5}'.base64_encode(pack('H*',$pass));
110
111 $r=@ldap_add($ds, "uid=".$user.",$ldap_add", $info);
bf754776
AS
112 if ($r) {
113 $add++;
114 echo "Added $user (email: $email) (pass: $pass)\n";
bf754776
AS
115 } else
116 echo "Failed adding $user (email: $email) (pass: $pass) - ". ldap_error($ds) ."\n";
117
118 } else if (!$user || !$pass || !$email) {
119 if (!$user && !$pass && !$email)
120 continue; /* misc bits after entries */
121
122 if (($user == "}") && !$pass && !$email)
123 continue; /* misc bits after entries */
124
125 echo "Missing fields from $db (User: $user Pass: $pass Email: $email)\n";
126 }
127
128 }
129 }
130} else
131 die("Couldnt read $db\n");
132
133echo "Disconnecting from ldap server\n";
134ldap_close($ds);
135$parse--;
136$parse--;
137echo "Processed $parse accounts.\n";
138echo "Added $add accounts to the ldap server\n";
03fa9256 139
bf754776 140?>