e1286d7e1b1344263e9783e717d637d24904a748
1 <?php
2 class group extends plugin
3 {
4 /* CLI vars */
5 var $cli_summary= "Handling of GOsa's base group object";
6 var $cli_description= "Some longer text\nfor help";
7 var $cli_parameters= array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
9 /* Group attributes */
10 var $cn= "";
11 var $description= "";
12 var $gidNumber= "";
13 var $memberUid= array();
15 /* Helpers */
16 var $base= "";
17 var $force_gid= FALSE;
18 var $fon_group= FALSE;
19 var $smbgroup= FALSE;
20 var $groupType= FALSE;
21 var $samba3= FALSE;
22 var $sambaSID= "";
23 var $sambaDomainName= "DEFAULT";
24 var $SID= "";
25 var $ridBase= 0;
26 var $members= array();
27 var $users= array();
28 var $allusers= array();
29 var $department= "";
30 var $saved_gidNumber= "";
31 var $oldgroupType= "";
32 var $orig_dn= "";
33 var $has_mailAccount= FALSE;
34 var $group_dialog= FALSE;
35 var $nagios_group =FALSE;
36 var $sambaGroupType;
37 var $dialog;
39 /* attribute list for save action */
40 var $attributes= array("cn", "description", "gidNumber","memberUid","sambaGroupType","sambaSID");
41 var $objectclasses= array("top", "posixGroup");
43 function group ($config, $dn= NULL)
44 {
45 plugin::plugin ($config, $dn);
47 /* Load attributes depending on the samba version */
48 $this->samba3= ($config->current['SAMBAVERSION'] == 3);
49 $this->orig_dn= $dn;
51 /* Get member list */
52 if (isset($this->attrs['memberUid'][0])){
53 $tmp= array();
54 for ($i= 0; $i<$this->attrs['memberUid']['count']; $i++){
55 $tmp[]= $this->attrs['memberUid'][$i];
56 }
57 $this->memberUid= $tmp;
58 sort ($this->memberUid);
59 }
61 /* Save gidNumber for later use */
62 if (isset($this->attrs['gidNumber'])){
63 $this->saved_gidNumber= $this->attrs['gidNumber'][0];
64 }
66 /* Is a samba group? */
67 if (isset($this->attrs['objectClass'])){
68 if (array_search ('sambaGroupMapping', $this->attrs['objectClass']) == NULL ){
69 $this->smbgroup= FALSE;
70 } else {
71 $this->smbgroup= TRUE;
72 if (isset($this->attrs['sambaSID'])){
73 $this->sambaSID= $this->attrs['sambaSID'][0];
74 }
75 }
76 if (array_search ('goFonPickupGroup', $this->attrs['objectClass']) == NULL ){
77 $this->fon_group= FALSE;
78 } else {
79 $this->fon_group= TRUE;
80 }
81 if (array_search ('nagiosContactGroup', $this->attrs['objectClass']) == NULL ){
82 $this->nagios_group= FALSE;
83 } else {
84 $this->nagios_group= TRUE;
85 }
86 }
88 /* Set mail flag */
89 if (isset($this->attrs['objectClass']) && in_array('gosaMailAccount', $this->attrs['objectClass'])){
90 $this->has_mailAccount= TRUE;
91 }
93 /* Get samba Domain in case of samba 3 */
94 if ($this->samba3 && $this->sambaSID != ""){
95 $this->SID= preg_replace ("/-[^-]+$/", "", $this->sambaSID);
96 $ldap= $this->config->get_ldap_link();
97 $ldap->cd($this->config->current['BASE']);
98 $ldap->search ("(&(objectClass=sambaDomain)(sambaSID=$this->SID))",array("sambaAlgorithmicRidBase"));
99 if ($ldap->count() != 0){
100 $attrs= $ldap->fetch();
101 $this->ridBase= $attrs['sambaAlgorithmicRidBase'][0];
103 /* Get domain name for SID */
104 $this->sambaDomainName= "DEFAULT";
105 foreach ($this->config->data['SERVERS']['SAMBA'] as $key => $val){
106 if ($val['SID'] == $this->SID){
107 $this->sambaDomainName= $key;
108 break;
109 }
110 }
111 } else {
112 if (isset($this->config->current['RIDBASE'])){
113 $this->sambaDomainName= "DEFAULT";
114 $this->ridBase= $this->config->current['RIDBASE'];
115 $this->SID= $this->config->current['SID'];
116 } else {
117 print_red(_("Can't find this groups SID in LDAP or in your configuration file!"));
118 }
119 }
121 /* Get group type */
122 $this->groupType= (int)substr(strrchr($this->sambaSID, "-"), 1);
123 if ($this->groupType < 500 || $this->groupType > 553){
124 $this->groupType= 0;
125 }
126 $this->oldgroupType= $this->groupType;
127 }
129 /* Get global filter config */
130 if (!is_global("gufilter")){
131 $ui= get_userinfo();
132 $base= get_base_from_people($ui->dn);
133 $gufilter= array( "dselect" => $base,
134 "regex" => "*");
135 register_global("gufilter", $gufilter);
136 }
137 $gufilter= get_global('gufilter');
139 /* Bases / Departments */
141 if(isset($_SESSION['groupfilter']['depselect'])){
142 $this->base = $_SESSION['groupfilter']['depselect'];
143 }else{
144 if ($this->dn == "new"){
145 $ui= get_userinfo();
146 $this->base= dn2base($ui->dn);
147 } else {
148 $this->base= preg_replace ("/^[^,]+,[^,]+,/", "", $this->dn);
149 }
150 }
153 /* This is always an account */
154 $this->is_account= TRUE;
155 $this->reload();
156 }
158 function execute()
159 {
160 /* Call parent execute */
161 plugin::execute();
163 $ui= get_userinfo();
164 $acla= get_permissions ($ui->dn, $ui->subtreeACL);
165 $this->acl= get_module_permission($acla, "group", $ui->dn);
166 /* Do we represent a valid group? */
167 if (!$this->is_account && $this->parent == NULL){
168 $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\"> <b>".
169 _("This 'dn' is no group.")."</b>";
170 return ($display);
171 }
173 /* Delete user from group */
174 if (isset($_POST['del_users']) && isset($_POST['members'])){
175 foreach ($_POST['members'] as $value){
176 unset ($this->members["$value"]);
177 $this->removeUser($value);
178 }
179 $this->reload();
180 }
182 /* Add objects? */
183 if (isset($_POST["edit_membership"])){
184 $this->group_dialog= TRUE;
185 $this->dialog= TRUE;
186 }
188 /* Add objects finished? */
189 if (isset($_POST["add_users_finish"]) || isset($_POST["add_users_cancel"])){
190 $this->group_dialog= FALSE;
191 $this->dialog= FALSE;
192 }
194 /* Add user to group */
195 if (isset($_POST['add_users_finish']) && isset($_POST['users'])){
196 foreach ($_POST['users'] as $value){
197 $this->members["$value"]= $this->allusers[$value];
198 asort($this->members);
199 $this->addUser($value);
200 }
201 $this->reload();
202 }
204 /* Base select dialog */
205 $once = true;
206 foreach($_POST as $name => $value){
207 if(preg_match("/^chooseBase/",$name) && $once){
208 $once = false;
209 $this->dialog = new baseSelectDialog($this->config);
210 $this->dialog->setCurrentBase($this->base);
211 }
212 }
214 /* Dialog handling */
215 if(is_object($this->dialog)){
216 /* Must be called before save_object */
217 $this->dialog->save_object();
219 if($this->dialog->isClosed()){
220 $this->dialog = false;
221 }elseif($this->dialog->isSelected()){
222 $this->base = $this->dialog->isSelected();
223 $this->dialog= false;
224 }else{
225 return($this->dialog->execute());
226 }
227 }
229 /* Assign templating stuff */
230 $smarty= get_smarty();
231 if ($this->samba3){
232 $smarty->assign("samba3", "true");
233 } else {
234 $smarty->assign("samba3", "");
235 }
237 if(search_config($this->config->data['MENU'], "nagiosaccount", "CLASS")){
238 $smarty->assign("nagios",true);
239 }else{
240 $smarty->assign("nagios",false);
241 }
243 if(search_config($this->config->data['MENU'], "phoneAccount", "CLASS")){
244 $smarty->assign("pickupGroup",true);
245 }else{
246 $smarty->assign("pickupGroup",false);
247 }
249 /* Manage object add dialog */
250 if ($this->group_dialog){
252 /* Save data */
253 $gufilter= get_global("gufilter");
254 foreach( array("dselect", "regex") as $type){
255 if (isset($_POST[$type])){
256 $gufilter[$type]= $_POST[$type];
257 }
258 }
259 if (isset($_GET['search'])){
260 $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
261 if ($s == "**"){
262 $s= "*";
263 }
264 $gufilter['regex']= $s;
265 }
266 $regex= preg_replace('/[*]/', ".*", $gufilter['regex']);
267 register_global("gufilter", $gufilter);
268 $this->reload();
270 /* Show dialog */
271 $smarty->assign("search_image", get_template_path('images/search.png'));
272 $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
273 $smarty->assign("tree_image", get_template_path('images/tree.png'));
274 $smarty->assign("deplist", $this->config->idepartments);
275 $smarty->assign("alphabet", generate_alphabet());
276 foreach( array("dselect", "regex") as $type){
277 $smarty->assign("$type", $gufilter[$type]);
278 }
279 $smarty->assign("hint", print_sizelimit_warning());
281 $users= array();
282 foreach ($this->allusers as $key => $value){
283 if (!array_key_exists($key, $this->members)){
284 if (preg_match("/^$regex/i", $key)){
285 $users[$key]= $value;
286 }
287 }
288 }
289 $smarty->assign("users", $users);
290 $smarty->assign("apply", apply_filter());
291 $display= $smarty->fetch (get_template_path('group_objects.tpl', TRUE, dirname(__FILE__)));
292 return ($display);
293 }
295 /* Bases / Departments */
296 if (isset($_POST['base'])){
297 $this->base= $_POST['base'];
298 }
300 $smarty->assign("bases", $this->config->idepartments);
301 $smarty->assign("base_select", $this->base);
302 $smarty->assign("department", $this->department);
304 if ($this->samba3){
305 $domains= array();
306 foreach($this->config->data['SERVERS']['SAMBA'] as $name => $content){
307 $domains[$name]= $name;
308 }
309 $smarty->assign("sambaDomains", $domains);
310 $smarty->assign("sambaDomainName", $this->sambaDomainName);
311 $groupTypes= array(0 => _("Samba group"), 512 => _("Domain admins"), 513 => _("Domain users"),
312 514 => _("Domain guests"));
314 /* Don't loose special groups! If not key'ed above, add it to
315 the combo box... */
316 if ($this->groupType >= 500 && $this->groupType <= 553 && !isset($groupTypes[$this->groupType])){
317 $groupTypes[$this->groupType]= sprintf(_("Special group (%d)"), $this->groupType);
318 }
320 $smarty->assign("groupTypes", $groupTypes);
321 $smarty->assign("groupType", $this->groupType);
322 }
324 /* Members and users */
325 $smarty->assign("members", $this->members);
327 /* Checkboxes */
328 foreach (array("force_gid", "smbgroup") as $val){
329 if ($this->$val == "1"){
330 $smarty->assign("$val", "checked");
331 } else {
332 $smarty->assign("$val", "");
333 }
334 }
335 if ($this->force_gid != "1"){
336 $smarty->assign("forceMode", "disabled");
337 }else{
338 $smarty->assign("forceMode", "");
339 }
340 $smarty->assign("force_gidACL", chkacl($this->acl, "gidNumber"));
341 $smarty->assign("sambaDomainNameACL", chkacl($this->acl, "sambaDomainName"));
342 if ($this->fon_group){
343 $smarty->assign("fon_group", "checked");
344 } else {
345 $smarty->assign("fon_group", "");
346 }
347 $smarty->assign("fon_groupACL", chkacl($this->acl, "fon_group"));
349 if ($this->nagios_group){
350 $smarty->assign("nagios_group", "checked");
351 } else {
352 $smarty->assign("nagios_group", "");
353 }
354 $smarty->assign("nagios_groupACL", chkacl($this->acl, "nagios_group"));
356 /* Fields */
357 foreach (array("cn", "description", "gidNumber") as $val){
358 $smarty->assign("$val", $this->$val);
359 $smarty->assign("$val"."ACL", chkacl($this->acl, "$val"));
360 }
362 /* Missing ACL's */
363 foreach (array("base", "smbgroup", "members") as $val){
364 $smarty->assign("$val"."ACL", chkacl($this->acl, "$val"));
365 }
367 /* Show main page */
368 $smarty->assign("alphabet", generate_alphabet(10));
369 $smarty->assign("search_image", get_template_path('images/search.png'));
370 $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
371 $smarty->assign("tree_image", get_template_path('images/tree.png'));
372 $smarty->assign("deplist", $this->config->idepartments);
373 return($smarty->fetch (get_template_path('generic.tpl', TRUE)));
374 }
376 function addUser($uid)
377 {
378 $this->memberUid[]= $uid;
379 $this->memberUid= array_unique($this->memberUid);
380 }
382 function removeUser($uid)
383 {
384 $temp= array();
385 foreach ($this->memberUid as $value){
386 if ($value != $uid){
387 $temp[]= $value;
388 }
389 }
390 $this->memberUid= $temp;
391 }
394 /* Reload data */
395 function reload()
396 {
397 /* Generate userlists */
398 $this->last_sorting= "invalid";
399 $this->users= array();
400 $ldap= $this->config->get_ldap_link();
401 $gufilter= get_global("gufilter");
403 $ldap->cd ($gufilter['dselect']);
404 $ldap->ls ("(&(objectClass=gosaAccount)(!(objectClass=gosaUserTemplate))(!(uid=*$)))",get_people_ou().$gufilter['dselect'],array("uid", "sn", "givenName"));
405 $this->allusers= array();
406 while ($attrs= $ldap->fetch()){
407 if (isset($attrs["givenName"][0]) && isset($attrs["sn"][0])){
408 $this->allusers[$attrs["uid"][0]]= $attrs["sn"][0].", ".
409 $attrs["givenName"][0].
410 " [".$attrs["uid"][0]."]";
411 } else {
412 $this->allusers[$attrs["uid"][0]]= $attrs['uid'][0];
413 }
414 }
415 natcasesort ($this->allusers);
416 reset ($this->allusers);
418 /* Fill memberlist */
419 $this->members= array();
420 foreach ($this->memberUid as $value){
421 if (isset($this->allusers[$value])){
422 $this->members[$value]= $this->allusers[$value];
423 } else {
424 $this->members[$value]= "[$value]";
425 }
426 }
427 asort($this->members);
428 reset($this->members);
429 }
433 function remove_from_parent()
434 {
435 plugin::remove_from_parent();
437 $ldap= $this->config->get_ldap_link();
438 $ldap->rmdir($this->dn);
439 show_ldap_error($ldap->get_error());
441 /* Delete references to object groups */
442 $ldap->cd ($this->config->current['BASE']);
443 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
444 while ($ldap->fetch()){
445 $og= new ogroup($this->config, $ldap->getDN());
446 unset($og->member[$this->dn]);
447 $og->save ();
448 }
450 /* Send signal to the world that we've done */
451 $this->handle_post_events("remove");
452 }
455 /* Save data to object */
456 function save_object()
457 {
458 /* Save additional values for possible next step */
459 if (isset($_POST['groupedit'])){
460 plugin::save_object();
462 $this->force_gid= 0;
463 $this->smbgroup= 0;
464 foreach (array("force_gid", "department", "base", "smbgroup") as $val) {
465 if (chkacl ($this->acl, "$val") == "" && isset($_POST["$val"])){
466 $this->$val= $_POST["$val"];
467 }
468 }
470 /* Save sambaDomain attribute */
471 if (chkacl ($this->acl, "sambaDomainName") == "" && $this->samba3 &&
472 isset ($_POST['sambaDomainName'])){
474 $this->sambaDomainName= $_POST['sambaDomainName'];
475 $this->groupType= $_POST['groupType'];
476 }
478 /* Save fon attribute */
479 if (chkacl ($this->acl, "fon_group") == ""){
480 if (isset ($_POST['fon_group'])){
481 $this->fon_group= TRUE;
482 } else {
483 $this->fon_group= FALSE;
484 }
485 }
486 if (chkacl ($this->acl, "nagios_group") == ""){
487 if (isset ($_POST['nagios_group'])){
488 $this->nagios_group= TRUE;
489 } else {
490 $this->nagios_group= FALSE;
491 }
492 }
493 }
494 }
497 /* Save to LDAP */
498 function save()
499 {
500 /* ID handling */
501 if ($this->force_gid == 0){
502 if ($this->saved_gidNumber != ""){
503 $this->gidNumber= $this->saved_gidNumber;
504 } else {
505 /* Calculate new, lock uids */
506 $wait= 10;
507 while (get_lock("uidnumber") != ""){
508 sleep (1);
510 /* timed out? */
511 if ($wait-- == 0){
512 break;
513 }
514 }
515 add_lock ("uidnumber", "gosa");
516 $this->gidNumber= $this->get_next_id("gidNumber");
517 }
518 }
520 plugin::save();
522 /* Remove objectClass for samba/phone support */
523 $tmp= array();
524 for ($i= 0; $i<count($this->attrs["objectClass"]); $i++){
525 if ($this->attrs['objectClass'][$i] != 'sambaGroupMapping' &&
526 $this->attrs['objectClass'][$i] != 'sambaIdmapEntry' &&
527 $this->attrs['objectClass'][$i] != 'goFonPickupGroup' &&
528 $this->attrs['objectClass'][$i] != 'nagiosContactGroup'){
529 $tmp[]= $this->attrs['objectClass'][$i];
530 }
531 }
532 $this->attrs['objectClass']= $tmp;
533 $ldap= $this->config->get_ldap_link();
535 /* Add samba group functionality */
536 if ($this->samba3 && $this->smbgroup){
537 $this->SID= $this->config->data['SERVERS']['SAMBA'][$this->sambaDomainName]['SID'];
538 $this->ridBase= $this->config->data['SERVERS']['SAMBA'][$this->sambaDomainName]['RIDBASE'];
540 $this->attrs['objectClass'][]= 'sambaGroupMapping';
541 $this->attrs['sambaGroupType']= "2";
543 /* Check if we need to create a special entry */
544 if ($this->groupType == 0){
546 if ($this->sambaSID == "" || $this->oldgroupType != $this->groupType){
547 $gidNumber= $this->gidNumber;
548 while(TRUE){
549 $sid= $this->SID."-".($gidNumber*2 + $this->ridBase+1);
550 $ldap->cd($this->config->current['BASE']);
551 $ldap->search("(sambaSID=$sid)",array("sambaSID"));
552 if ($ldap->count() == 0){
553 break;
554 }
555 $gidNumber++;
556 }
557 $this->attrs['sambaSID']= $sid;
558 $this->sambaSID= $sid;
559 }
561 } else {
562 $this->attrs['sambaSID']=$this->SID."-".$this->groupType;
563 }
565 /* User wants me to fake the idMappings? This is useful for
566 making winbind resolve the group names in a reasonable amount
567 of time in combination with larger databases. */
568 if (isset($this->config->current['SAMBAIDMAPPING']) &&
569 preg_match('/true/i', $this->config->current['SAMBAIDMAPPING'])){
570 $this->attrs['objectClass'][]= "sambaIdmapEntry";
571 }
573 }
575 /* Add phone functionality */
576 if ($this->fon_group){
577 $this->attrs['objectClass'][]= "goFonPickupGroup";
578 }
580 /* Add nagios functionality */
581 if ($this->nagios_group){
582 $this->attrs['objectClass'][]= "nagiosContactGroup";
583 }
585 /* Take members array */
586 if (count ($this->memberUid)){
587 $this->attrs['memberUid']= array_unique($this->memberUid);
588 }
590 /* New accounts need proper 'dn', propagate it to remaining objects */
591 if ($this->dn == 'new'){
592 $this->dn= 'cn='.$this->cn.','.get_groups_ou().$this->base;
593 }
595 /* Save data. Using 'modify' implies that the entry is already present, use 'add' for
596 new entries. So do a check first... */
597 $ldap->cat ($this->dn);
598 if ($ldap->fetch()){
599 /* Modify needs array() to remove values :-( */
600 if (!count ($this->memberUid)){
601 $this->attrs['memberUid']= array();
602 }
603 if ($this->samba3){
604 if (!$this->smbgroup){
605 $this->attrs['sambaGroupType']= array();
606 $this->attrs['sambaSID']= array();
607 }
608 }
609 $mode= "modify";
610 } else {
611 $mode= "add";
612 $ldap->cd($this->config->current['BASE']);
613 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
614 }
616 /* Write back to ldap */
617 $ldap->cd($this->dn);
618 $this->cleanup();
619 $ldap->$mode($this->attrs);
621 $ret= 0;
622 if (show_ldap_error($ldap->get_error())){
623 $ret= 1;
624 }
626 /* Remove uid lock */
627 del_lock ("uidnumber");
629 /* Post that we've done*/
630 $this->handle_post_events($mode);
632 return ($ret);
633 }
635 function check()
636 {
637 $message= array();
639 /* Permissions for that base? */
640 if ($this->base != ""){
641 $new_dn= 'cn='.$this->cn.','.get_groups_ou().$this->base;
642 } else {
643 $new_dn= $this->dn;
644 }
646 $ui= get_userinfo();
647 $acl= get_permissions ($ui->dn, $ui->subtreeACL);
648 $acl= get_module_permission($acl, "group", $ui->dn);
649 if (chkacl($this->acl, "create") != ""){
650 $message[]= _("You have no permissions to create a group on this 'Base'.");
651 }
653 /* must: cn */
654 if ($this->cn == "" && chkacl ($this->acl, "cn") == ""){
655 $message[]= "The required field 'Name' is not set.";
656 }
658 /* Check for valid input */
659 if (!is_uid($this->cn)){
660 $message[]= _("The field 'Name' contains invalid characters. Lowercase, numbers and dashes are allowed.");
661 }
663 /* Check for used 'cn' */
664 $ldap= $this->config->get_ldap_link();
665 $ldap->cd($this->config->current['BASE']);
666 $ldap->search("(&(|(objectClass=gosaGroupOfNames)(objectClass=posixGroup))(cn=$this->cn))",array("cn"));
667 if ($ldap->count() != 0){
669 /* New entry? */
670 if ($this->dn == 'new'){
671 $message[]= _("Value specified as 'Name' is already used.");
672 }
674 /* Moved? */
675 elseif ($new_dn != $this->orig_dn){
676 $ldap->fetch();
677 if ($ldap->getDN() != $this->orig_dn){
678 $message[]= _("Value specified as 'Name' is already used.");
679 }
680 }
681 }
683 /* Check ID */
684 if ($this->force_gid == "1"){
685 if (!is_id($this->gidNumber)){
686 $message[]= _("Value specified as 'GID' is not valid.");
687 } else {
688 if ($this->gidNumber < $this->config->current['MINID']){
689 $message[]= _("Value specified as 'GID' is too small.");
690 }
692 }
693 }
695 return ($message);
696 }
698 function get_next_id($attrib)
699 {
700 $ids= array();
701 $ldap= $this->config->get_ldap_link();
703 $ldap->cd ($this->config->current['BASE']);
704 if (preg_match('/gidNumber/i', $attrib)){
705 $oc= "posixGroup";
706 } else {
707 $oc= "posixAccount";
708 }
709 $ldap->search ("(&(objectClass=$oc)($attrib=*))", array("$attrib"));
711 /* Get list of ids */
712 while ($attrs= $ldap->fetch()){
713 $ids[]= (int)$attrs["$attrib"][0];
714 }
716 /* Find out next free id near to UID_BASE */
717 for ($id= $this->config->current['UIDBASE']; $id++; $id<65000){
718 if (!in_array($id, $ids)){
719 return ($id);
720 }
721 }
723 /* Should not happen */
724 if ($id == 65000){
725 print_red(_("Too many users, can't allocate a free ID!"));
726 exit;
727 }
728 }
730 }
732 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
733 ?>