ldap.inc.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. <?php
  2. // External login module : LDAP
  3. /**
  4. * This files is included by newUser.ldap.php and login.ldap.php
  5. * It implements the functions nedded by both files.
  6. * */
  7. require_once __DIR__.'/../../inc/global.inc.php';
  8. $debug = false;
  9. /**
  10. * Returns a transcoded and trimmed string.
  11. *
  12. * @param string
  13. *
  14. * @return string
  15. *
  16. * @author ndiechburg <noel@cblue.be>
  17. * */
  18. function extldap_purify_string($string)
  19. {
  20. global $extldap_config;
  21. if (isset($extldap_config['encoding'])) {
  22. return trim(api_to_system_encoding($string, $extldap_config['encoding']));
  23. } else {
  24. return trim($string);
  25. }
  26. }
  27. /**
  28. * Establishes a connection to the LDAP server and sets the protocol version.
  29. *
  30. * @return bool ldap link identifier or false
  31. *
  32. * @author ndiechburg <noel@cblue.be>
  33. * */
  34. function extldap_connect()
  35. {
  36. global $extldap_config, $debug;
  37. if (!is_array($extldap_config['host'])) {
  38. $extldap_config['host'] = [$extldap_config['host']];
  39. }
  40. foreach ($extldap_config['host'] as $host) {
  41. //Trying to connect
  42. if (isset($extldap_config['port'])) {
  43. $ds = ldap_connect($host, $extldap_config['port']);
  44. } else {
  45. $ds = ldap_connect($host);
  46. }
  47. if (!$ds) {
  48. $port = isset($extldap_config['port']) ? $extldap_config['port'] : 389;
  49. if ($debug) {
  50. error_log(
  51. 'EXTLDAP ERROR : cannot connect to '.$extldap_config['host'].':'.$port
  52. );
  53. }
  54. } else {
  55. break;
  56. }
  57. }
  58. if (!$ds) {
  59. if ($debug) {
  60. error_log('EXTLDAP ERROR : no valid server found');
  61. }
  62. return false;
  63. }
  64. // Setting protocol version
  65. if (isset($extldap_config['protocol_version'])) {
  66. if (!ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, $extldap_config['protocol_version'])) {
  67. ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 2);
  68. }
  69. }
  70. // Setting protocol version
  71. if (isset($extldap_config['referrals'])) {
  72. if (!ldap_set_option($ds, LDAP_OPT_REFERRALS, $extldap_config['referrals'])) {
  73. ldap_set_option($ds, LDAP_OPT_REFERRALS, $extldap_config['referrals']);
  74. }
  75. }
  76. return $ds;
  77. }
  78. /**
  79. * Authenticate user on external ldap server and return user ldap entry if that succeeds.
  80. *
  81. * @param string $password
  82. *
  83. * @return mixed false if user cannot authenticate on ldap, user ldap entry if tha succeeds
  84. *
  85. * @author ndiechburg <noel@cblue.be>
  86. * Modified by hubert.borderiou@grenet.fr
  87. * Add possibility to get user info from LDAP without check password (if CAS auth and LDAP profil update)
  88. *
  89. * */
  90. function extldap_authenticate($username, $password, $in_auth_with_no_password = false)
  91. {
  92. global $extldap_config, $debug;
  93. if (empty($username) || empty($password)) {
  94. return false;
  95. }
  96. $ds = extldap_connect();
  97. if (!$ds) {
  98. return false;
  99. }
  100. // Connection as admin to search dn of user
  101. $ldapbind = @ldap_bind($ds, $extldap_config['admin_dn'], $extldap_config['admin_password']);
  102. if ($ldapbind === false) {
  103. if ($debug) {
  104. error_log(
  105. 'EXTLDAP ERROR : cannot connect with admin login/password'
  106. );
  107. }
  108. return false;
  109. }
  110. $user_search = extldap_get_user_search_string($username);
  111. // Search distinguish name of user
  112. $sr = ldap_search($ds, $extldap_config['base_dn'], $user_search);
  113. if (!$sr) {
  114. if ($debug) {
  115. error_log(
  116. 'EXTLDAP ERROR : ldap_search('.$ds.', '.$extldap_config['base_dn'].", $user_search) failed"
  117. );
  118. }
  119. return false;
  120. }
  121. $entries_count = ldap_count_entries($ds, $sr);
  122. if ($entries_count > 1) {
  123. if ($debug) {
  124. error_log(
  125. 'EXTLDAP ERROR : more than one entry for that user ( ldap_search(ds, '.$extldap_config['base_dn'].", $user_search) )"
  126. );
  127. }
  128. return false;
  129. }
  130. if ($entries_count < 1) {
  131. if ($debug) {
  132. error_log(
  133. 'EXTLDAP ERROR : No entry for that user ( ldap_search(ds, '.$extldap_config['base_dn'].", $user_search) )"
  134. );
  135. }
  136. return false;
  137. }
  138. $users = ldap_get_entries($ds, $sr);
  139. $user = $users[0];
  140. // If we just want to have user info from LDAP and not to check password
  141. if ($in_auth_with_no_password) {
  142. return $user;
  143. }
  144. // now we try to autenthicate the user in the ldap
  145. $ubind = @ldap_bind($ds, $user['dn'], $password);
  146. if ($ubind !== false) {
  147. return $user;
  148. } else {
  149. if ($debug) {
  150. error_log('EXTLDAP : Wrong password for '.$user['dn']);
  151. }
  152. return false;
  153. }
  154. }
  155. /**
  156. * Return an array with userinfo compatible with chamilo using $extldap_user_correspondance
  157. * configuration array declared in ldap.conf.php file.
  158. *
  159. * @param array ldap user
  160. * @param array correspondance array (if not set use extldap_user_correspondance declared in auth.conf.php
  161. *
  162. * @return array userinfo array
  163. *
  164. * @author ndiechburg <noel@cblue.be>
  165. * */
  166. function extldap_get_chamilo_user($ldap_user, $cor = null)
  167. {
  168. global $extldap_user_correspondance, $debug;
  169. if (is_null($cor)) {
  170. $cor = $extldap_user_correspondance;
  171. }
  172. $chamilo_user = [];
  173. foreach ($cor as $chamilo_field => $ldap_field) {
  174. if (is_array($ldap_field)) {
  175. $chamilo_user[$chamilo_field] = extldap_get_chamilo_user($ldap_user, $ldap_field);
  176. continue;
  177. }
  178. switch ($ldap_field) {
  179. case 'func':
  180. $func = "extldap_get_$chamilo_field";
  181. if (function_exists($func)) {
  182. $chamilo_user[$chamilo_field] = extldap_purify_string($func($ldap_user));
  183. } else {
  184. if ($debug) {
  185. error_log(
  186. "EXTLDAP WARNING : You forgot to declare $func"
  187. );
  188. }
  189. }
  190. break;
  191. default:
  192. //if string begins with "!", then this is a constant
  193. if ($ldap_field[0] === '!') {
  194. $chamilo_user[$chamilo_field] = trim($ldap_field, "!\t\n\r\0");
  195. break;
  196. }
  197. if (isset($ldap_user[$ldap_field][0])) {
  198. $chamilo_user[$chamilo_field] = extldap_purify_string($ldap_user[$ldap_field][0]);
  199. } else {
  200. if ($debug) {
  201. error_log(
  202. 'EXTLDAP WARNING : '.$ldap_field.'[0] field is not set in ldap array'
  203. );
  204. }
  205. }
  206. break;
  207. }
  208. }
  209. return $chamilo_user;
  210. }
  211. /**
  212. * Please declare here all the function you use in extldap_user_correspondance
  213. * All these functions must have an $ldap_user parameter. This parameter is the
  214. * array returned by the ldap for the user.
  215. * */
  216. function extldap_get_status($ldap_user)
  217. {
  218. return STUDENT;
  219. }
  220. function extldap_get_admin($ldap_user)
  221. {
  222. return false;
  223. }
  224. /**
  225. * return the string used to search a user in ldap.
  226. *
  227. * @param string username
  228. *
  229. * @return string the serach string
  230. *
  231. * @author ndiechburg <noel@cblue.be>
  232. * */
  233. function extldap_get_user_search_string($username)
  234. {
  235. global $extldap_config;
  236. // init
  237. $filter = '('.$extldap_config['user_search'].')';
  238. // replacing %username% by the actual username
  239. $filter = str_replace('%username%', $username, $filter);
  240. // append a global filter if needed
  241. if (isset($extldap_config['filter']) && $extldap_config['filter'] != "") {
  242. $filter = '(&'.$filter.'('.$extldap_config['filter'].'))';
  243. }
  244. return $filter;
  245. }
  246. /**
  247. * Imports all LDAP users into Chamilo.
  248. *
  249. * @return false|null false on error, true otherwise
  250. */
  251. function extldap_import_all_users()
  252. {
  253. global $extldap_config, $debug;
  254. //echo "Connecting...\n";
  255. $ds = extldap_connect();
  256. if (!$ds) {
  257. return false;
  258. }
  259. //echo "Binding...\n";
  260. $ldapbind = false;
  261. //Connection as admin to search dn of user
  262. $ldapbind = @ldap_bind($ds, $extldap_config['admin_dn'], $extldap_config['admin_password']);
  263. if ($ldapbind === false) {
  264. if ($debug) {
  265. error_log(
  266. 'EXTLDAP ERROR : cannot connect with admin login/password'
  267. );
  268. }
  269. return false;
  270. }
  271. //browse ASCII values from a to z to avoid 1000 results limit of LDAP
  272. $count = 0;
  273. $alphanum = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
  274. for ($a = 97; $a <= 122; $a++) {
  275. $alphanum[] = chr($a);
  276. }
  277. foreach ($alphanum as $char1) {
  278. foreach ($alphanum as $char2) {
  279. //$user_search = "uid=*";
  280. $user_search = "sAMAccountName=$char1$char2*";
  281. //Search distinguish name of user
  282. $sr = ldap_search($ds, $extldap_config['base_dn'], $user_search);
  283. if (!$sr) {
  284. if ($debug) {
  285. error_log(
  286. 'EXTLDAP ERROR : ldap_search('.$ds.', '.$extldap_config['base_dn'].", $user_search) failed"
  287. );
  288. }
  289. return false;
  290. }
  291. //echo "Getting entries\n";
  292. $users = ldap_get_entries($ds, $sr);
  293. //echo "Entries: ".$users['count']."\n";
  294. for ($key = 0; $key < $users['count']; $key++) {
  295. $user_id = extldap_add_user_by_array($users[$key], true);
  296. $count++;
  297. }
  298. }
  299. }
  300. //echo "Found $count users in total\n";
  301. @ldap_close($ds);
  302. }
  303. /**
  304. * Insert users from an array of user fields.
  305. */
  306. function extldap_add_user_by_array($data, $update_if_exists = true)
  307. {
  308. global $extldap_user_correspondance;
  309. $lastname = api_convert_encoding($data[$extldap_user_correspondance['lastname']][0], api_get_system_encoding(), 'UTF-8');
  310. $firstname = api_convert_encoding($data[$extldap_user_correspondance['firstname']][0], api_get_system_encoding(), 'UTF-8');
  311. $email = $data[$extldap_user_correspondance['email']][0];
  312. $username = $data[$extldap_user_correspondance['username']][0];
  313. // TODO the password, if encrypted at the source, will be encrypted twice, which makes it useless. Try to fix that.
  314. $passwordKey = isset($extldap_user_correspondance['password']) ? $extldap_user_correspondance['password'] : 'userPassword';
  315. $password = $data[$passwordKey][0];
  316. // To ease management, we add the step-year (etape-annee) code
  317. //$official_code = $etape."-".$annee;
  318. $official_code = api_convert_encoding($data[$extldap_user_correspondance['official_code']][0], api_get_system_encoding(), 'UTF-8');
  319. $auth_source = 'ldap';
  320. // No expiration date for students (recover from LDAP's shadow expiry)
  321. $expiration_date = '';
  322. $active = 1;
  323. $status = 5;
  324. $phone = '';
  325. $picture_uri = '';
  326. // Adding user
  327. $user_id = 0;
  328. if (UserManager::is_username_available($username)) {
  329. //echo "$username\n";
  330. $user_id = UserManager::create_user(
  331. $firstname,
  332. $lastname,
  333. $status,
  334. $email,
  335. $username,
  336. $password,
  337. $official_code,
  338. api_get_setting('platformLanguage'),
  339. $phone,
  340. $picture_uri,
  341. $auth_source,
  342. $expiration_date,
  343. $active
  344. );
  345. } else {
  346. if ($update_if_exists) {
  347. $user = api_get_user_info($username);
  348. $user_id = $user['user_id'];
  349. //echo "$username\n";
  350. UserManager::update_user(
  351. $user_id,
  352. $firstname,
  353. $lastname,
  354. $username,
  355. null,
  356. null,
  357. $email,
  358. $status,
  359. $official_code,
  360. $phone,
  361. $picture_uri,
  362. $expiration_date,
  363. $active
  364. );
  365. }
  366. }
  367. return $user_id;
  368. }