UserStorageMacOSX.php 29 KB

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