ldap.inc.php 12 KB

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