UserStorageMacOSX.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784
  1. <?php
  2. Lib::loadClass('UserStorageBase');
  3. Lib::loadClass('ObjectUserLdap');
  4. Lib::loadClass('ObjectGroupLdap');
  5. Lib::loadClass('UsersLdapHelper');
  6. Lib::loadClass('LDAP');
  7. /**
  8. * Test remove user:
  9. * $ dscl /Local/Default -list /Groups GroupMembership | grep test13
  10. * ... for all groups:
  11. * $ dscl -u diradmin -p /Local/Default -delete /Groups/workgroup GroupMembership test13
  12. * $ dscl /LDAPv3/127.0.0.1 -list /Groups GroupMembership | grep test13
  13. * ... for all groups:
  14. * $ dscl -u diradmin -p /LDAPv3/127.0.0.1 -delete /Groups/workgroup GroupMembership test13
  15. * $ dscl -u diradmin -p /Local/Default -delete /Users/test13
  16. */
  17. class UserStorageMacOSX extends UserStorageBase {
  18. private $_rootUser;
  19. private $_rootPass;
  20. private $_host;
  21. private $_ldapRoot;
  22. public function __construct($rootUser, $rootPass, $host) {
  23. $this->_rootUser = $rootUser;
  24. $this->_rootPass = $rootPass;
  25. $this->_host = $host;
  26. }
  27. /**
  28. * @return ObjectUserLdap
  29. */
  30. public function getUser($userName) {
  31. $usrLdap = UsersLdapHelper::getUser($userName, true);
  32. if (empty($usrLdap[0])) return null;
  33. $user = $this->_buildUserFromLdap($usrLdap[0]);
  34. return $user;
  35. }
  36. private function _buildUserFromLdap($usrLdap) {
  37. $user = new ObjectUserLdap($this);
  38. $user->primaryKey = V::get('uidNumber', '', $usrLdap);
  39. $user->login = V::get('uid', '', $usrLdap);
  40. $user->password = '';
  41. $user->name = V::get('cn', '', $usrLdap);
  42. $user->email = V::get('mail', '', $usrLdap);
  43. $user->phone = V::get('telephoneNumber', '', $usrLdap);
  44. $user->homeEmail = V::get('carLicense', '', $usrLdap);
  45. $user->homePhone = V::get('homePhone', '', $usrLdap);
  46. $user->employeeType = V::get('employeeType', '', $usrLdap);
  47. return $user;
  48. }
  49. /**
  50. * @return ObjectGroupLdap $group
  51. */
  52. public function getGroup($groupID) {
  53. return $this->_getGroup($groupID, $fetchNested = true);
  54. }
  55. private function _getGroup($groupID, $fetchNested = false) {
  56. if ($groupID <= 0) return false;
  57. $group = null;
  58. $groups = UsersLdapHelper::getGroupsByID($groupID);
  59. if (count($groups) == 1) {
  60. $group = reset($groups);
  61. if(V::get('DBG_SU', 0, $_GET, 'int') > 2){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">groupLdap (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($group);echo'</pre>';}
  62. $group = $this->_buildGroupFromLdap($group, $fetchNested);
  63. if(V::get('DBG_SU', 0, $_GET, 'int') > 2){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">group (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($group);echo'</pre>';}
  64. } else if (count($groups) > 1) {
  65. if(V::get('DBG_SU', 0, $_GET, 'int') > 0){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">'."Too much groups in ldap by ID {$groupID}".' (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($groups);echo'</pre>';}
  66. trigger_error("Too much groups in ldap by ID {$groupID}", E_USER_WARNING);
  67. }
  68. return $group;
  69. }
  70. public function getParentGroups(ObjectGroup $group) {
  71. $parentGroups = array();
  72. $parentGroupsLdap = UsersLdapHelper::getParentGroupsByAppleUID($group->getLdapUID());
  73. foreach ($parentGroupsLdap as $groupLdap) {
  74. $group = $this->_buildGroupFromLdap($groupLdap);
  75. if ($group->zasobID > 0) {
  76. $parentGroups[$group->zasobID] = $group;
  77. }
  78. }
  79. return $parentGroups;
  80. }
  81. /**
  82. * @return bool
  83. */
  84. public function isDisabled($usr) {
  85. if (null === $usr->isDisabled) {
  86. $allGroups = $this->_fetchAllUserGroups($usr->login);
  87. $usr->isDisabled = in_array('com.apple.access_disabled', $allGroups);
  88. DBG::_('DBG_SU', '>1', "usr->isDisabled(" . (($usr->isDisabled)? 'true' : 'false') . ") ", null, __CLASS__, __FUNCTION__, __LINE__);
  89. }
  90. return $usr->isDisabled;
  91. }
  92. /**
  93. * @return bool
  94. */
  95. public function setDisabled($usrLogin, $isDisabled) {
  96. // pwpolicy -a diradmin -u t1 -disableuser
  97. // pwpolicy -a diradmin -u t1 -enableuser
  98. if (empty($usrLogin) || null === $isDisabled) {
  99. return false;
  100. }
  101. $cmdDisabled = ($isDisabled)? ' -disableuser' : ' -enableuser';
  102. $cmd = "pwpolicy -a {$this->_rootUser} -p {$this->_rootPass} -u {$usrLogin} {$cmdDisabled} 2>&1 ";
  103. $cmdOut = null; $cmdRet = null;
  104. exec($cmd, $cmdOut, $cmdRet);
  105. DBG::_('DBG_SU', '>1', "cmd(" . str_replace($this->_rootPass, '***', $cmd) . ") ret({$cmdRet}) ", $cmdOut, __CLASS__, __FUNCTION__, __LINE__);
  106. if ($cmdRet !== 0) {
  107. return false;
  108. }
  109. return true;
  110. }
  111. /**
  112. * @param $usr - user object @see UserStorageBase::getUser()
  113. * $usr->employeeType: Pracownik, Kandydat, Partner, Anonymous
  114. * Pracownik - all access
  115. * Kandydat - no access
  116. * Partner - access: smb/afp, TODO: calendar?, addressbook?
  117. * Anonymous - no access
  118. */
  119. public function createUser($usr) {
  120. DBG::_('DBG_SU', '>1', 'usr', $usr, __CLASS__, __FUNCTION__, __LINE__);
  121. $cmdDsclAuth = "dscl -u {$this->_rootUser} -P {$this->_rootPass} /LDAPv3/127.0.0.1 ";
  122. $login = $this->_cleanUid($usr->login);
  123. $name = $this->_cleanText($usr->name);
  124. $type = $usr->employeeType;
  125. $email = $usr->email;
  126. $pass = $usr->password;
  127. $uniqueID = 0;
  128. // test user login and pass by searching for $uniqueID for new user
  129. $cmd = "{$cmdDsclAuth} -list /Users > /dev/null && {$cmdDsclAuth} -list /Users UniqueID|awk '{print \$2}'|sort -n|tail -1 ";
  130. $cmdOut = null; $cmdRet = null;
  131. exec($cmd, $cmdOut, $cmdRet);
  132. if ($cmdRet == 0 && !empty($cmdOut[0])) {
  133. $uniqueID = intval($cmdOut[0]);
  134. if ($uniqueID > 0) {
  135. $uniqueID += 1;
  136. }
  137. }
  138. if ($uniqueID <= 0) {
  139. throw new Exception("Error: dscl auth - check login and password in ldap config");
  140. }
  141. if (empty($name)) {
  142. $name = $login;
  143. } else {
  144. // TODO: replace bad signs str_replace($_SESSION['CONFIG']['BAD_FILE_SIGNS_LETTERS'],$_SESSION['CONFIG']['OK_FILE_SIGNS_LETTERS'],$ADM_NAME)
  145. }
  146. if (empty($pass)) {
  147. $pass = $login;
  148. }
  149. $cmds = array();
  150. //$cmds[] = "{$cmdDsclAuth} -create /Users/{$login} HomeDirectory \"<home_dir><url>afp://{$this->_host}/Users</url><path>{$login}</path></home_dir>\" ";
  151. //$cmds[] = "{$cmdDsclAuth} -create /Users/{$login} NFSHomeDirectory /Network/Servers/{$this->_host}/Users/{$login} ";
  152. $cmds[] = "{$cmdDsclAuth} -create /Users/{$login} NFSHomeDirectory /Users/{$login} ";
  153. $cmds[] = "{$cmdDsclAuth} -create /Users/{$login} UserShell /bin/bash ";// TODO: bash?
  154. $cmds[] = "{$cmdDsclAuth} -create /Users/{$login} UniqueID {$uniqueID} ";
  155. $cmds[] = "{$cmdDsclAuth} -create /Users/{$login} PrimaryGroupID 20 ";// TODO: 20 maja domyslnie inne konta?
  156. $cmds[] = "{$cmdDsclAuth} -create /Users/{$login} RealName \"{$name}\" ";
  157. if (!empty($email)) $cmds[] = "{$cmdDsclAuth} -create /Users/{$login} EMailAddress {$email} ";
  158. $cmds[] = "{$cmdDsclAuth} -passwd /Users/{$login} \"{$pass}\" ";
  159. $cmds[] = "sudo /usr/sbin/createhomedir -c -u {$login} 2>&1 ";// TODO:INSTALATOR: add to sudoers _www ALL = NOPASSWD: /usr/sbin/createhomedir
  160. foreach ($cmds as $cmd) {
  161. $cmdOut = null; $cmdRet = null;
  162. exec($cmd, $cmdOut, $cmdRet);
  163. if ($cmdRet != 0) {
  164. DBG::_('DBG_SU', '>1', "cmd failed: " . str_replace($cmdDsclAuth, "dscl __auth__ ", $cmd), $cmdOut, __CLASS__, __FUNCTION__, __LINE__);
  165. throw new Exception("Wystąpił błąd podczas tworzenia użytwkonika '{$usrLogin}' w bazie Ldap");
  166. }
  167. }
  168. }
  169. private function _getAdminLdap() {
  170. if (!$this->_ldapRoot) {
  171. $this->_ldapRoot = LDAP::getInstance();
  172. if (!$this->_ldapRoot->bindDiradmin($errorMsg)) {
  173. // $errorMsg?
  174. $this->setError(1, "cant bind as diradmin", '(' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . ')');
  175. return null;
  176. }
  177. }
  178. return $this->_ldapRoot;
  179. }
  180. public function updateUser($userName, $updateData) {
  181. if (empty($updateData)) return true;
  182. foreach ($updateData as $fldName => $val) {
  183. switch ($fldName) {
  184. case 'email':
  185. $ldap = $this->_getAdminLdap();
  186. if ($ldap) {
  187. $attr = array();
  188. $attr['mail'] = $val;
  189. $ldap->mod_replace($userName, $attr);
  190. }
  191. break;
  192. case 'name':
  193. $ldap = $this->_getAdminLdap();
  194. if ($ldap) {
  195. $attr = array();
  196. $attr['cn'] = $val;
  197. $ldap->mod_replace($userName, $attr);
  198. }
  199. break;
  200. case 'phone':
  201. $ldap = $this->_getAdminLdap();
  202. if ($ldap) {
  203. $attr = array();
  204. $attr['telephoneNumber'] = $val;
  205. $ldap->mod_replace($userName, $attr);
  206. }
  207. break;
  208. case 'employeeType':
  209. $ldap = $this->_getAdminLdap();
  210. if ($ldap) {
  211. $attr = array();
  212. $attr['employeeType'] = $val;
  213. $ldap->mod_replace($userName, $attr);
  214. }
  215. break;
  216. case 'password':
  217. if (!empty($val) && !$this->changePassword($userName, $val)) {
  218. $this->setError(1, "Nie udało się zmienić hasła dla usera '{$userName}'", '(' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . ')');
  219. }
  220. break;
  221. case 'homeEmail':
  222. $ldap = $this->_getAdminLdap();
  223. if ($ldap) {
  224. $attr = array();
  225. $attr['carLicense'] = $val;
  226. if (!$ldap->mod_replace($userName, $attr)) {
  227. if (!$ldap->mod_add($userName, $attr)) {
  228. //$this->setError(1, "TODO: update homeEmail failed " . $ldap->error(), '(' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . ')');
  229. }
  230. }
  231. }
  232. break;
  233. default:
  234. $this->setError(1, "TODO: update user {$userName} field {$fldName} to value '{$val}'", '(' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . ')');
  235. }
  236. }
  237. if ($this->hasErrors()) {
  238. $this->setError(1, "Nie udało się zaktualizować danych usera '{$userName}'", '(' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . ')');
  239. return false;
  240. }
  241. return true;
  242. }
  243. public function updateGroup($group, $updateData) {
  244. if (!$group) return false;
  245. if (empty($updateData)) return true;
  246. foreach ($updateData as $fldName => $val) {
  247. switch ($fldName) {
  248. case 'realName':
  249. $ldap = $this->_getAdminLdap();
  250. if ($ldap) {
  251. $attr = array();
  252. $attr['apple-group-realname'] = $val;
  253. $ldap->groupAttrUpdate($group->primaryKey, $attr);
  254. }
  255. break;
  256. default:
  257. $this->setError(1, "Błąd podczas aktulizacji grupy '{$group->primaryKey}' - pole '{$fldName}' watość '{$val}'", '#L' . __LINE__);
  258. }
  259. }
  260. return true;
  261. }
  262. private function _getGroupIdFromUid($groupUid) {
  263. if (empty($groupUid)) return null;
  264. if (!is_numeric(substr($groupUid, 0, 1))) return null;
  265. $tmp = str_replace(array('-', '_'), '_', $groupUid);
  266. $tmp = explode('_', $tmp);
  267. $tmp = reset($tmp);
  268. if (!empty($tmp) && is_numeric($tmp)) {
  269. return $tmp;
  270. }
  271. return null;
  272. }
  273. /**
  274. * User group list by id.
  275. *
  276. * @param bool $fetchNested - contain all groups below connected groups and group PODMIOT from above.
  277. *
  278. * @return array with group objects @see getGroup
  279. */
  280. public function getUserGroups($usrLogin, $fetchNested = false) {
  281. $groups = array();
  282. $groupsNetwork = $this->_getUserGroupsNetwork($usrLogin);
  283. $groupsLocal = $this->_getUserGroupsLocal($usrLogin);
  284. foreach ($groupsLocal as $kGroupUid => $vGroup) {
  285. $groups[$kGroupUid] = $vGroup;
  286. }
  287. foreach ($groupsNetwork as $kGroupUid => $vGroupNetwork) {
  288. if ($vGroupNetwork->primaryKey == 'workgroup') {
  289. $groups[$vGroupNetwork->primaryKey] = $vGroupNetwork;
  290. }
  291. else if ($vGroupNetwork->zasobID > 0) {
  292. $groups[$vGroupNetwork->zasobID] = $vGroupNetwork;
  293. }
  294. }
  295. DBG::_('DBG_SU', '>2', "groupsNetwork", $groupsNetwork, __CLASS__, __FUNCTION__, __LINE__);
  296. DBG::_('DBG_SU', '>2', "groupsLocal", $groupsLocal, __CLASS__, __FUNCTION__, __LINE__);
  297. return $groups;
  298. }
  299. /**
  300. * Build network group object.
  301. *
  302. * @param object $groupDB {ID, DESC} @see _getUserGroupsAll
  303. * @return object $group @see getGroup
  304. *
  305. * Example: _buildGroupFromLdap($groupLdap) => {@see getGroup}
  306. */
  307. private function _buildGroupFromLdap($groupLdap, $fetchNested = false) {
  308. $group = new ObjectGroupLdap($this);
  309. $group->primaryKey = $groupLdap->cn;
  310. $group->realName = V::get('realName', '', $groupLdap);
  311. $group->zasobID = $this->_getGroupIdFromUid($groupLdap->cn);
  312. $group->type = 'unknown';// TODO: try to fetch from name or from ldap attribute
  313. if ($groupLdap->cn == 'workgroup') $group->type = 'network';
  314. if ($fetchNested && !empty($groupLdap->nestedGroups)) {
  315. $group->nestedGroups = $this->_fetchNestedGroupsByAppleUids($groupLdap->nestedGroups);
  316. }
  317. $group->setLdapUID($groupLdap->appleUID);
  318. return $group;
  319. }
  320. private function _fetchNestedGroupsByAppleUids($appleUids) {
  321. $groups = array();
  322. if (!is_array($appleUids)) $appleUids = array($appleUids);
  323. $groupsLdap = UsersLdapHelper::getGroupsByAppleUids($appleUids);
  324. foreach ($groupsLdap as $vGroupLdap) {
  325. $group = $this->_buildGroupFromLdap($vGroupLdap, $fetchNested = false);
  326. if ($group && $group->zasobID > 0) {
  327. $groups[$group->zasobID] = $group;
  328. }
  329. }
  330. return $groups;
  331. }
  332. /**
  333. * @param string $usrLogin - user login
  334. * @return array of group objects @see getGroup
  335. */
  336. private function _getUserGroupsNetwork($usrLogin) {
  337. $groups = array();
  338. $groupsNetwork = UsersLdapHelper::getUserGroups($usrLogin, 0);
  339. foreach ($groupsNetwork as $vGroupNetwork) {
  340. $groups[$vGroupNetwork->cn] = $this->_buildGroupFromLdap($vGroupNetwork);
  341. }
  342. return $groups;
  343. }
  344. /**
  345. * @param string $usrLogin - user login
  346. * @return array of group objects @see getGroup
  347. */
  348. private function _getUserGroupsLocal($usrLogin) {
  349. $groups = array();
  350. $allGroups = $this->_fetchAllUserGroups($usrLogin);
  351. foreach ($allGroups as $groupName) {
  352. if ($this->_isGroupLocal($groupName)) {
  353. $groups[$groupName] = $this->_buildGroupLocal($groupName);
  354. }
  355. }
  356. DBG::_('DBG_SU', '>1', "User '{$usrLogin}' GroupsLocal:", $groups, __CLASS__, __FUNCTION__, __LINE__);
  357. return $groups;
  358. }
  359. private function _fetchAllUserGroups($usrLogin) {
  360. $groups = array();
  361. $cmd = "groups {$usrLogin}";
  362. $cmdOut = null; $cmdRet = null;
  363. exec($cmd, $cmdOut, $cmdRet);
  364. if ($cmdRet == 0 && !empty($cmdOut[0])) {
  365. $groupsCmd = explode(' ', $cmdOut[0]);
  366. foreach ($groupsCmd as $groupName) {
  367. if (!empty($groupName)) {
  368. $groups[] = $groupName;
  369. }
  370. }
  371. }
  372. DBG::_('DBG_SU', '>1', "User '{$usrLogin}' all groups:", $groups, __CLASS__, __FUNCTION__, __LINE__);
  373. return $groups;
  374. }
  375. public function getUserGroupsWithNested($usrLogin) {// TODO: NOT USED
  376. $groups = array();
  377. $groupsAll = array();
  378. $cmd = "groups {$usrLogin}";
  379. $cmdOut = null; $cmdRet = null;
  380. exec($cmd, $cmdOut, $cmdRet);
  381. if ($cmdRet == 0 && !empty($cmdOut[0])) {
  382. $pominGrupy = array('staff','everyone','netaccounts');
  383. $groupsCmd = explode(' ', $cmdOut[0]);
  384. foreach ($groupsCmd as $group) {
  385. $groupsAll[] = $group;
  386. $groupID = $this->_getGroupIdFromUid($group);
  387. if (!empty($groupID)) {
  388. $groups[$groupID] = $group;
  389. }
  390. else if ('workgroup' == $group) {
  391. $groups[$group] = $group;
  392. }
  393. else if (substr($group, 0, strlen('com.apple.access_')) == 'com.apple.access_') {
  394. $groups[$group] = $group;
  395. }
  396. }
  397. }
  398. if (V::get('DBG_SU', 0, $_GET, 'int') > 1) {
  399. echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">groupsAll (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($groupsAll);echo'</pre>';
  400. echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">groups (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($groups);echo'</pre>';
  401. }
  402. return $groups;
  403. }
  404. private function _groupNameRemoveID($groupName) {
  405. if (substr($groupName, 0, 1) == '[' && strpos($groupName, ']')) {
  406. $groupName = substr($groupName, strpos($groupName, ']') + 1);
  407. $groupName = trim($groupName);
  408. }
  409. return $groupName;
  410. }
  411. private function _generateGroupName($id, $groupName) {
  412. $groupNameShort = $groupName;
  413. $groupNameShort = $this->_groupNameRemoveID($groupNameShort);
  414. // TODO: polish chars - replace to ascii?
  415. $groupNameShort = preg_replace('/[^a-zA-Z0-9_-]+/', '_', $groupNameShort);
  416. // TODO: skrócić nazwę bo nie widać w aplikacji Server, np.
  417. // RealName: [5] Typowe_stanowisko_obs_uguj_ce_Obieg_Dokument_w_do_implementacji_po_instalacji_systemu
  418. // w apliakcji Server pokauje tylko "[5] ", tak samo w edycji
  419. return "[{$id}] {$groupNameShort}";
  420. }
  421. private function _generateGroupUid($id, $groupName) {
  422. $groupNameShort = $groupName;
  423. $groupNameShort = $this->_groupNameRemoveID($groupNameShort);
  424. $groupNameShort = str_replace(' ', '_', $groupNameShort);
  425. $groupNameShort = preg_replace('/[^a-zA-Z0-9_-]+/', '_', $groupNameShort);
  426. if (strlen($groupNameShort) > 30) {
  427. $groupNameShort = substr($groupNameShort, 0, 30);
  428. }
  429. return "{$id}_{$groupNameShort}";
  430. }
  431. /**
  432. * Create group.
  433. *
  434. * @param object $group @see getGroup
  435. * @return bool
  436. *
  437. * @require $group->zasobID - Allowed only network group based on Zasob.
  438. */
  439. public function createGroup(ObjectGroup $group) {
  440. // TEST: $ dscl /LDAPv3/127.0.0.1 -list /Groups PrimaryGroupID
  441. if ($group->zasobID <= 0) {
  442. throw new Exception("Nie udało się utworzyć grupy sieciowej '{$group->primaryKey}' '{$group->realName}' - brak numery zasobu");
  443. }
  444. $groupName = $this->_generateGroupName($group->zasobID, $group->realName);
  445. $groupUidGenerated = $this->_generateGroupUid($group->zasobID, $group->realName);
  446. /*
  447. * dseditgroup -o create -n /LDAPv3/ldap.company.com -u {$this->_rootUser} -P {$this->_rootPass} -r "Extra Group" -c "a nice comment" -k "some keyword" extragroup
  448. * The group extragroup is created from the node /LDAPv3/ldap.company.com with the realname, comment,
  449. * timetolive (instead of default of 14400 = 4 hours), and keyword atttribute values given above if the user
  450. * myusername has supplied a correct password and has write access.
  451. *
  452. * -r realname
  453. * This is a simple text string.
  454. *
  455. * -t recordtype
  456. * The type of the record to be added to or deleted from the group specified by groupname. Valid values are user, computer, group, or computergroup.
  457. *
  458. */
  459. $cmd = "dseditgroup -o create -n /LDAPv3/127.0.0.1 -u {$this->_rootUser} -P {$this->_rootPass} -r \"{$groupName}\" {$groupUidGenerated}";
  460. $cmdOut = null; $cmdRet = null;
  461. exec($cmd, $cmdOut, $cmdRet);
  462. DBG::_('DBG_SU', '>1', "create group cmd(" . str_replace($this->_rootPass, '***', $cmd) . ") ret({$cmdRet})", $cmdOut, __CLASS__, __FUNCTION__, __LINE__);
  463. if ($cmdRet !== 0) {
  464. throw new Exception("Nie udało się utworzyć grupy sieciowej '{$group->primaryKey}' '{$group->realName}'");
  465. }
  466. //$command8 = "dscl -u {$user} -P {$pass} /LDAPv3/127.0.0.1 -append /Groups/{$groupUid} GroupMembership {$ACCOUNT} ";
  467. //$command8 = "dscl -u {$user} -P {$pass} /LDAPv3/127.0.0.1 -delete /Groups/{$groupUid} GroupMembership {$ACCOUNT} ";
  468. //$command1 = "dscl -u {$user} -P {$pass} /LDAPv3/127.0.0.1 -create /Groups/{$groupUid} PrimaryGroupID {$PrimaryGroupID} ";
  469. //$command2 = "dscl -u {$user} -P {$pass} /LDAPv3/127.0.0.1 -create /Groups/{$groupUid} RealName \"{$groupName}\" ";
  470. }
  471. private function _isGroupLocal($groupUid) {
  472. $localGroups = array();
  473. $localGroups[] = 'com.apple.access_mail';
  474. $localGroups[] = 'com.apple.access_addressbook';
  475. $localGroups[] = 'com.apple.access_calendar';
  476. $localGroups[] = 'com.apple.access_smb';
  477. $localGroups[] = 'com.apple.access_afp';
  478. $localGroups[] = 'com.apple.access_vpn';
  479. $localGroups[] = 'com.apple.access_chat';
  480. //$localGroups[] = 'workgroup'; - Network Group
  481. return in_array($groupUid, $localGroups);
  482. }
  483. /**
  484. * Add local group member.
  485. *
  486. * @param string $usrLogin - user login
  487. * @param object $group - @see getGroup
  488. * @return bool
  489. *
  490. * @require sudoers dla _www
  491. *
  492. * cat /etc/sudoers |grep "'.$ADMIN_USERNAME.' ALL = NOPASSWD: /usr/bin/su" || echo "'.$ADMIN_USERNAME.' ALL = NOPASSWD: /usr/bin/su " >> /etc/sudoers;
  493. * cat /etc/sudoers |grep "'.$ADMIN_USERNAME.' ALL = NOPASSWD: /usr/bin/su"
  494. * cat /etc/sudoers |grep "_www ALL = NOPASSWD: /Applications/Server.app/Contents/ServerRoot/usr/sbin/calendarserver_manage_principals" || echo "_www ALL = NOPASSWD: /Applications/Server.app/Contents/ServerRoot/usr/sbin/calendarserver_manage_principals " >> /etc/sudoers;
  495. * cat /etc/sudoers |grep "_www ALL = NOPASSWD: /Applications/Server.app/Contents/ServerRoot/usr/sbin/calendarserver_manage_principals"';
  496. * cat /etc/sudoers |grep "_www ALL = NOPASSWD: /usr/bin/dscl" || echo "_www ALL = NOPASSWD: /usr/bin/dscl " >> /etc/sudoers;
  497. * cat /etc/sudoers |grep "_www ALL = NOPASSWD: /usr/bin/dscl";
  498. * cat /etc/sudoers |grep "_www ALL = NOPASSWD: /usr/bin/pwpolicy" || echo "_www ALL = NOPASSWD: /usr/bin/pwpolicy" >> /etc/sudoers;
  499. * cat /etc/sudoers |grep "_www ALL = NOPASSWD: /usr/bin/pwpolicy";
  500. * cat /etc/sudoers |grep "_www ALL = NOPASSWD: /usr/sbin/createhomedir" || echo "_www ALL = NOPASSWD: /usr/sbin/createhomedir" >> /etc/sudoers;
  501. * cat /etc/sudoers |grep "_www ALL = NOPASSWD: /usr/sbin/createhomedir";
  502. *
  503. * cat /etc/sudoers |grep "_www ALL = NOPASSWD: /usr/sbin/dseditgroup" || echo "_www ALL = NOPASSWD: /usr/sbin/dseditgroup" >> /etc/sudoers;
  504. * cat /etc/sudoers |grep "_www ALL = NOPASSWD: /usr/sbin/dseditgroup";
  505. */
  506. private function _addUserGroupLocal($usrLogin, $group) {
  507. if (!$group || empty($group->primaryKey) || empty($usrLogin)) return false;
  508. $groupUid = $group->primaryKey;
  509. $cmd = "sudo dscl /Local/Default -append /Groups/{$groupUid} GroupMembership {$usrLogin} ";
  510. $cmdOut = null; $cmdRet = null;
  511. exec($cmd, $cmdOut, $cmdRet);
  512. if ($cmdRet != 0) {
  513. throw new Exception("Nie udało się dodać usera '{$usrLogin}' do grupy lokalnej '{$groupUid}'");
  514. }
  515. }
  516. /**
  517. * Remove local group member.
  518. *
  519. * @param string $usrLogin - user login
  520. * @param object $group - @see getGroup
  521. * @return bool
  522. */
  523. private function _removeUserGroupLocal($usrLogin, $group) {
  524. if (!$group || empty($group->primaryKey) || empty($usrLogin)) return false;
  525. $groupUid = $group->primaryKey;
  526. //$cmd = "sudo dscl /Local/Default -delete /Groups/{$groupUid} GroupMembership {$usrLogin} 2>&1 ";
  527. //$cmd = "dseditgroup -o edit -n /Local/Default -u diradmin -p ... -d username -t user {$groupUid} ";
  528. $cmd = "sudo dseditgroup -o edit -n /Local/Default -d {$usrLogin} -t user {$groupUid} 2>&1 ";
  529. // The group extragroup from the node /LDAPv3/ldap.company.com will have the username deleted if the correct
  530. // password is presented interactively for the user myusername which also need to have write access.
  531. // -t recordtype type of the record to add or delete
  532. // -d recordname name of the record to delete
  533. $cmdOut = null; $cmdRet = null;
  534. exec($cmd, $cmdOut, $cmdRet);
  535. DBG::_('DBG_SU', '>1', "cmd({$cmd}) ret({$cmdRet})", $cmdOut, __CLASS__, __FUNCTION__, __LINE__);
  536. if ($cmdRet != 0) {
  537. throw new Exception("Nie udało się dodać usera '{$usrLogin}' z grupy lokalnej '{$groupUid}'");
  538. }
  539. }
  540. public function findGroupUidDscl($groupUid) {// not used @see findGroupUid
  541. $groupRealUid = null;
  542. $cmd = "dscl /LDAPv3/127.0.0.1 -list /Groups | grep '^{$groupUid}' ";
  543. $cmdOut = null; $cmdRet = null;
  544. exec($cmd, $cmdOut, $cmdRet);
  545. if ($cmdRet != 0) {
  546. $this->setError(1, "cmd failed - search for group by uid", '(' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . ')');
  547. return false;
  548. }
  549. if (!empty($cmdOut)) {
  550. foreach ($cmdOut as $vGroupUid) {
  551. $vGroupID = $this->_getGroupIdFromUid($vGroupUid);
  552. if (!empty($vGroupID) && $vGroupID == $groupUid) {
  553. $groupRealUid = $vGroupUid;
  554. break;
  555. }
  556. }
  557. }
  558. return $groupRealUid;
  559. }
  560. public function findGroupUidLdap($groupUid) {
  561. $groupRealUid = null;
  562. $groups = UsersLdapHelper::getGroupsByID($groupUid);
  563. if (count($groups) == 1) {
  564. $groupRealUid = reset($groups)->cn;
  565. }
  566. return $groupRealUid;
  567. }
  568. public function findGroupUid($groupUid) {
  569. return $this->findGroupUidLdap($groupUid);
  570. }
  571. /**
  572. * Add network group member.
  573. *
  574. * @param string $usrLogin - user login
  575. * @param object $group - @see getGroup
  576. * @return bool
  577. */
  578. private function _addUserGroupNetwork($usrLogin, $group) {
  579. if (!$group || empty($group->primaryKey) || empty($usrLogin)) return false;
  580. $groupUid = $group->primaryKey;
  581. $groupName = $group->realName;
  582. $groupRealUid = '';
  583. if ($group->type == 'network') {
  584. $groupRealUid = $group->primaryKey;// workgroup
  585. }
  586. else if (is_numeric($groupUid)) {
  587. $groupRealUid = $this->findGroupUid($groupUid);
  588. }
  589. if (!$groupRealUid) {
  590. if ($group->type == 'network') {
  591. throw new Exception("Brak dostępu do utworzenia grupy sieciowej '{$group->primaryKey}'");
  592. } else if ($group->type == 'local') {
  593. throw new Exception("Brak dostępu do utworzenia grupy lokalnej '{$group->primaryKey}'");
  594. }
  595. $this->createGroup($group);
  596. }
  597. $cmdDsclAuth = "dscl -u {$this->_rootUser} -P {$this->_rootPass} /LDAPv3/127.0.0.1 ";
  598. $cmd = "{$cmdDsclAuth} -append /Groups/{$groupRealUid} GroupMembership {$usrLogin} ";
  599. $cmdOut = null; $cmdRet = null;
  600. exec($cmd, $cmdOut, $cmdRet);
  601. if ($cmdRet != 0) {// TODO: may return 62 - user already in this group
  602. throw new Exception("Nie udało się dodać usera '{$usrLogin}' do grupy sieciowej '{$groupUid}'");
  603. }
  604. }
  605. /**
  606. * Remove network group member.
  607. *
  608. * @param string $usrLogin - user login
  609. * @param object $group - @see getGroup
  610. * @return bool
  611. */
  612. private function _removeUserGroupNetwork($usrLogin, $group) {
  613. if (!$group || empty($group->primaryKey) || empty($usrLogin)) return false;
  614. $groupUid = $group->primaryKey;
  615. $cmdDsclAuth = "dscl -u {$this->_rootUser} -P {$this->_rootPass} /LDAPv3/127.0.0.1 ";
  616. $cmd = "{$cmdDsclAuth} -delete /Groups/{$groupUid} GroupMembership {$usrLogin} ";
  617. $cmdOut = null; $cmdRet = null;
  618. exec($cmd, $cmdOut, $cmdRet);
  619. DBG::_('DBG_SU', '>1', "cmd({$cmd}) ret({$cmdRet})", $cmdOut, __CLASS__, __FUNCTION__, __LINE__);
  620. if ($cmdRet != 0) {
  621. throw new Exception("Nie udało się dodać usera '{$usrLogin}' z grupy sieciowej '{$groupUid}'");
  622. }
  623. }
  624. /**
  625. * Add group member.
  626. *
  627. * @param string $usrLogin - user login
  628. * @param object $group - @see getGroup
  629. * @return bool
  630. */
  631. public function addUserGroup($usrLogin, $group) {
  632. // $groupUid, $groupName
  633. if ($group->type == 'local') {
  634. return $this->_addUserGroupLocal($usrLogin, $group);
  635. } else {
  636. return $this->_addUserGroupNetwork($usrLogin, $group);
  637. }
  638. }
  639. /**
  640. * Remove group member.
  641. *
  642. * @param string $usrLogin - user login
  643. * @param object $group - @see getGroup
  644. * @return bool
  645. */
  646. public function removeUserGroup($usrLogin, $group) {
  647. if ($group->type == 'local') {
  648. return $this->_removeUserGroupLocal($usrLogin, $group);
  649. } else {
  650. return $this->_removeUserGroupNetwork($usrLogin, $group);
  651. }
  652. }
  653. public function addNestedGroup($groupID, $nestedGroupID) {
  654. if ($groupID <= 0) return false;
  655. if ($nestedGroupID <= 0) return false;
  656. $group = $this->_getGroup($groupID);
  657. $groupNested = $this->_getGroup($nestedGroupID);
  658. if (!$group || !$groupNested) {
  659. return false;
  660. }
  661. $groupToAdd = $groupNested->primaryKey;
  662. $groupName = $group->primaryKey;
  663. // put a group called {$groupToAdd} into the {$groupName} group
  664. $cmd = "dseditgroup -o edit -n /LDAPv3/127.0.0.1 -u {$this->_rootUser} -P {$this->_rootPass} -a {$groupToAdd} -t group {$groupName}";
  665. $cmdOut = null; $cmdRet = null;
  666. exec($cmd, $cmdOut, $cmdRet);
  667. if ($cmdRet != 0) {
  668. DBG::_('DBG_SU', '>1', "cmd(" . str_replace($this->_rootPass, '***', $cmd) . ") ret({$cmdRet})", $cmdOut, __CLASS__, __FUNCTION__, __LINE__);
  669. $this->setError(1, "Nie udało się dodać grupy nadrzędnej '{$groupToAdd}' do grupy '{$groupName}' ", '(' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . ')');
  670. return false;
  671. }
  672. return true;
  673. }
  674. public function removeNestedGroup($groupID, $nestedGroupID) {
  675. if ($groupID <= 0) return false;
  676. if ($nestedGroupID <= 0) return false;
  677. $group = $this->_getGroup($groupID);
  678. $groupNested = $this->_getGroup($nestedGroupID);
  679. if (!$group || !$groupNested) {
  680. return false;
  681. }
  682. $groupToRemove = $groupNested->primaryKey;
  683. $groupName = $group->primaryKey;
  684. // put a group called {$groupToAdd} into the {$groupName} group
  685. $cmd = "dseditgroup -o edit -n /LDAPv3/127.0.0.1 -u {$this->_rootUser} -P {$this->_rootPass} -d {$groupToRemove} -t group {$groupName}";
  686. $cmdOut = null; $cmdRet = null;
  687. exec($cmd, $cmdOut, $cmdRet);
  688. if ($cmdRet != 0) {
  689. DBG::_('DBG_SU', '>1', "cmd(" . str_replace($this->_rootPass, '***', $cmd) . ") ret({$cmdRet})", $cmdOut, __CLASS__, __FUNCTION__, __LINE__);
  690. $this->setError(1, "Nie udało się usunąć grupy podrzędnej '{$groupToRemove}' z grupy '{$groupName}' ", '(' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . ')');
  691. return false;
  692. }
  693. return true;
  694. }
  695. public function changePassword($usrLogin, $passwd) {
  696. $cmdDsclAuth = "dscl -u {$this->_rootUser} -P {$this->_rootPass} /LDAPv3/127.0.0.1 ";
  697. $cmd = "{$cmdDsclAuth} -passwd /Users/{$usrLogin} \"{$passwd}\" ";
  698. $cmdOut = null; $cmdRet = null;
  699. exec($cmd, $cmdOut, $cmdRet);
  700. if ($cmdRet != 0) {
  701. return false;
  702. }
  703. return true;
  704. }
  705. }