UserStorageMacOSX.php 30 KB

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