UserStorageMacOSX.php 31 KB

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