PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/www/ldap_create_emails.php

https://bitbucket.org/wayfarer/verse
PHP | 197 lines | 156 code | 29 blank | 12 comment | 20 complexity | 2d398ba117da6ee4d31dc591e8f8fb47 MD5 | raw file
Possible License(s): ISC, AGPL-3.0, LGPL-2.1, BSD-3-Clause, LGPL-3.0
  1. <?php
  2. // initial LDAP create script
  3. include("inc/verse.inc.php"); //main header - initializes Verse environment
  4. require_once("Net/LDAP2.php");
  5. ini_set("display_errors", 1);
  6. set_time_limit(0);
  7. if ($user->have_role(ROLE_EMAIL_MANAGEMENT)) {
  8. $action = @$_POST["action"];
  9. $myCacheConfig = array(
  10. 'path' => 'c:/temp/ldap/ttt.cache',
  11. 'max_age' => 1200
  12. );
  13. $myCacheObject = new Net_LDAP2_SimpleFileSchemaCache($myCacheConfig);
  14. // Connect using the configuration:
  15. $ldap = Net_LDAP2::connect($ldap_config);
  16. if (PEAR::isError($ldap)) {
  17. die('Could not connect to LDAP-server: ' . $ldap->getMessage());
  18. }
  19. $ldap->registerSchemaCache($myCacheObject);
  20. $baseDN = "o=domains,dc=twintierstech,dc=net";
  21. // ws0 data
  22. $emails_data = unserialize(file_get_contents("!devel/email_transfer/email_data.ws1"));
  23. $i = 0;
  24. foreach ($emails_data as $domain => $emails) {
  25. echo "<b>$domain</b> (", count($emails), ") - ", $domain_status = ensure_domain_name($domain), "<br>";
  26. if ($domain_status != "exists") {
  27. foreach ($emails as $email) {
  28. echo $email["name"], "@$domain - ", ($email["type"] == 0 ? "mailbox"
  29. : "forward (" . $email["forward"] . ")"), " - ", $email["password"], " - ";
  30. if ($email["type"] == 0) {
  31. echo ensure_email($email, $domain);
  32. }
  33. else {
  34. echo ensure_alias($email, $domain);
  35. }
  36. echo "<br>";
  37. }
  38. }
  39. else {
  40. echo "skipping emails<br>";
  41. }
  42. $i++;
  43. //if($i>4) break;
  44. }
  45. }
  46. function ensure_domain_name($domain_name) {
  47. global $ldap, $baseDN;
  48. $dsn = "domainName=$domain_name,$baseDN";
  49. if (!ldap_entry_exists($dsn)) {
  50. $attributes = array(
  51. 'objectClass' => 'mailDomain',
  52. 'domainName' => $domain_name,
  53. 'accountStatus' => 'active',
  54. 'cn' => $domain_name,
  55. 'enabledService' => array('mail', 'recipientbcc', 'senderbcc'),
  56. 'mtaTransport' => 'dovecot'
  57. );
  58. // create domain entry
  59. $entry = Net_LDAP2_Entry::createFresh($dsn, $attributes);
  60. // Add the entry to the directory:
  61. $ret = $ldap->add($entry);
  62. if (PEAR::isError($ret)) {
  63. return "failed - " . $ret->getMessage();
  64. }
  65. else {
  66. // create ou=Users and ou=Aliases entries under the domain
  67. $users_attributes = array(
  68. 'objectClass' => array('organizationalUnit', 'top'),
  69. 'ou' => "Users"
  70. );
  71. $aliases_attributes = array(
  72. 'objectClass' => array('organizationalUnit', 'top'),
  73. 'ou' => "Aliases"
  74. );
  75. $entry = Net_LDAP2_Entry::createFresh("ou=Users,$dsn", $users_attributes);
  76. $ret = $ldap->add($entry);
  77. if (PEAR::isError($ret)) {
  78. return "failed - " . $ret->getMessage();
  79. }
  80. $entry = Net_LDAP2_Entry::createFresh("ou=Aliases,$dsn", $aliases_attributes);
  81. $ret = $ldap->add($entry);
  82. if (PEAR::isError($ret)) {
  83. return "failed - " . $ret->getMessage();
  84. }
  85. return "created";
  86. }
  87. }
  88. return "exists";
  89. }
  90. function ensure_email($an_email, $domain_name) {
  91. global $ldap, $baseDN;
  92. $email = $an_email["name"] . "@$domain_name";
  93. $dsn = "mail=$email,ou=Users,domainName=$domain_name,$baseDN";
  94. if (!ldap_entry_exists($dsn)) {
  95. $username = $cn = $an_email["name"];
  96. $email_pass = $an_email["password"];
  97. $storagebasedir = "/home/vmail/vmail01";
  98. // generate mail message store
  99. $mailmessagestore = sprintf("%s/%s/%s/%s/%s-%s/", $domain_name, $username[0], substr($username, 0, 2), substr($username, 0, 3), $username, date("Y.m.d.H.i.s"));
  100. $homedir = $storagebasedir . "/" . $mailmessagestore;
  101. $attributes = array(
  102. 'objectClass' => array("inetOrgPerson", "mailUser", "shadowAccount"),
  103. 'cn' => $cn,
  104. 'mail' => $email,
  105. 'sn' => $username,
  106. 'uid' => $username,
  107. 'accountStatus' => "active",
  108. 'enabledService' => array('mail', 'smtp', 'pop3', 'imap', 'deliver', 'forward',
  109. 'senderbcc', 'recipientbcc', 'managesieve',
  110. 'displayedInGlobalAddressBook'),
  111. 'homeDirectory' => $homedir,
  112. 'mailMessageStore' => $mailmessagestore,
  113. 'mailQuota' => 1073741824,
  114. 'memberOfGroup' => "",
  115. 'mtaTransport' => "dovecot",
  116. 'storageBaseDirectory' => $storagebasedir,
  117. 'userPassword' => make_ssha_password($email_pass)
  118. );
  119. // create email entry
  120. $entry = Net_LDAP2_Entry::createFresh($dsn, $attributes);
  121. // Add the entry to the directory:
  122. $ret = $ldap->add($entry);
  123. if (PEAR::isError($ret)) {
  124. return "failed - " . $ret->getMessage();
  125. }
  126. else {
  127. return "created";
  128. }
  129. }
  130. return "exists";
  131. }
  132. function ensure_alias($an_email, $domain_name) {
  133. global $ldap, $baseDN;
  134. $email = $an_email["name"] . "@$domain_name";
  135. $dsn = "mail=$email,ou=Aliases,domainName=$domain_name,$baseDN";
  136. if (!ldap_entry_exists($dsn)) {
  137. $aliases = explode(",", $an_email["forward"]);
  138. $attributes = array(
  139. "objectClass" => array("mailAlias", "top"),
  140. "mail" => $email,
  141. "mailForwardingAddress" => $aliases,
  142. "accountStatus" => "active",
  143. "enabledService" => array("mail", "deliver")
  144. );
  145. // create alias entry
  146. $entry = Net_LDAP2_Entry::createFresh($dsn, $attributes);
  147. // Add the entry to the directory:
  148. $ret = $ldap->add($entry);
  149. if (PEAR::isError($ret)) {
  150. return "failed - " . $ret->getMessage();
  151. }
  152. else {
  153. return "created";
  154. }
  155. }
  156. return "exists";
  157. }
  158. function ldap_entry_exists($dsn) {
  159. global $ldap;
  160. $entry = $ldap->getEntry($dsn);
  161. return !PEAR::isError($entry);
  162. }
  163. function make_ssha_password($password) {
  164. mt_srand((double)microtime() * 1000000);
  165. $salt = pack("CCCC", mt_rand(), mt_rand(), mt_rand(), mt_rand());
  166. $hash = "{SSHA}" . base64_encode(pack("H*", sha1($password . $salt)) . $salt);
  167. return $hash;
  168. }