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 ($this->config->current['BASE']);
404 $ldap->search ("(&(objectClass=gosaAccount)(!(objectClass=gosaUserTemplate))(!(uid=*$)))",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]." [".$attrs["uid"][0]."]";
410 } else {
411 $this->allusers[$attrs["uid"][0]]= $attrs['uid'][0];
412 }
413 }
414 natcasesort ($this->allusers);
415 reset ($this->allusers);
417 /* Fill memberlist */
418 $this->members= array();
419 foreach ($this->memberUid as $value){
420 if (isset($this->allusers[$value])){
421 $this->members[$value]= $this->allusers[$value];
422 } else {
423 $this->members[$value]= "[$value]";
424 }
425 }
426 asort($this->members);
427 reset($this->members);
428 }
432 function remove_from_parent()
433 {
434 plugin::remove_from_parent();
436 $ldap= $this->config->get_ldap_link();
437 $ldap->rmdir($this->dn);
438 show_ldap_error($ldap->get_error());
440 /* Delete references to object groups */
441 $ldap->cd ($this->config->current['BASE']);
442 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
443 while ($ldap->fetch()){
444 $og= new ogroup($this->config, $ldap->getDN());
445 unset($og->member[$this->dn]);
446 $og->save ();
447 }
449 /* Send signal to the world that we've done */
450 $this->handle_post_events("remove");
451 }
454 /* Save data to object */
455 function save_object()
456 {
457 /* Save additional values for possible next step */
458 if (isset($_POST['groupedit'])){
459 plugin::save_object();
461 $this->force_gid= 0;
462 $this->smbgroup= 0;
463 foreach (array("force_gid", "department", "base", "smbgroup") as $val) {
464 if (chkacl ($this->acl, "$val") == "" && isset($_POST["$val"])){
465 $this->$val= $_POST["$val"];
466 }
467 }
469 /* Save sambaDomain attribute */
470 if (chkacl ($this->acl, "sambaDomainName") == "" && $this->samba3 &&
471 isset ($_POST['sambaDomainName'])){
473 $this->sambaDomainName= $_POST['sambaDomainName'];
474 $this->groupType= $_POST['groupType'];
475 }
477 /* Save fon attribute */
478 if (chkacl ($this->acl, "fon_group") == ""){
479 if (isset ($_POST['fon_group'])){
480 $this->fon_group= TRUE;
481 } else {
482 $this->fon_group= FALSE;
483 }
484 }
485 if (chkacl ($this->acl, "nagios_group") == ""){
486 if (isset ($_POST['nagios_group'])){
487 $this->nagios_group= TRUE;
488 } else {
489 $this->nagios_group= FALSE;
490 }
491 }
492 }
493 }
496 /* Save to LDAP */
497 function save()
498 {
499 /* ID handling */
500 if ($this->force_gid == 0){
501 if ($this->saved_gidNumber != ""){
502 $this->gidNumber= $this->saved_gidNumber;
503 } else {
504 /* Calculate new, lock uids */
505 $wait= 10;
506 while (get_lock("uidnumber") != ""){
507 sleep (1);
509 /* timed out? */
510 if ($wait-- == 0){
511 break;
512 }
513 }
514 add_lock ("uidnumber", "gosa");
515 $this->gidNumber= $this->get_next_id("gidNumber");
516 }
517 }
519 plugin::save();
521 /* Remove objectClass for samba/phone support */
522 $tmp= array();
523 for ($i= 0; $i<count($this->attrs["objectClass"]); $i++){
524 if ($this->attrs['objectClass'][$i] != 'sambaGroupMapping' &&
525 $this->attrs['objectClass'][$i] != 'sambaIdmapEntry' &&
526 $this->attrs['objectClass'][$i] != 'goFonPickupGroup' &&
527 $this->attrs['objectClass'][$i] != 'nagiosContactGroup'){
528 $tmp[]= $this->attrs['objectClass'][$i];
529 }
530 }
531 $this->attrs['objectClass']= $tmp;
532 $ldap= $this->config->get_ldap_link();
534 /* Add samba group functionality */
535 if ($this->samba3 && $this->smbgroup){
536 $this->SID= $this->config->data['SERVERS']['SAMBA'][$this->sambaDomainName]['SID'];
537 $this->ridBase= $this->config->data['SERVERS']['SAMBA'][$this->sambaDomainName]['RIDBASE'];
539 $this->attrs['objectClass'][]= 'sambaGroupMapping';
540 $this->attrs['sambaGroupType']= "2";
542 /* Check if we need to create a special entry */
543 if ($this->groupType == 0){
545 if ($this->sambaSID == "" || $this->oldgroupType != $this->groupType){
546 $gidNumber= $this->gidNumber;
547 while(TRUE){
548 $sid= $this->SID."-".($gidNumber*2 + $this->ridBase+1);
549 $ldap->cd($this->config->current['BASE']);
550 $ldap->search("(sambaSID=$sid)",array("sambaSID"));
551 if ($ldap->count() == 0){
552 break;
553 }
554 $gidNumber++;
555 }
556 $this->attrs['sambaSID']= $sid;
557 $this->sambaSID= $sid;
558 }
560 } else {
561 $this->attrs['sambaSID']=$this->SID."-".$this->groupType;
562 }
564 /* User wants me to fake the idMappings? This is useful for
565 making winbind resolve the group names in a reasonable amount
566 of time in combination with larger databases. */
567 if (isset($this->config->current['SAMBAIDMAPPING']) &&
568 preg_match('/true/i', $this->config->current['SAMBAIDMAPPING'])){
569 $this->attrs['objectClass'][]= "sambaIdmapEntry";
570 }
572 }
574 /* Add phone functionality */
575 if ($this->fon_group){
576 $this->attrs['objectClass'][]= "goFonPickupGroup";
577 }
579 /* Add nagios functionality */
580 if ($this->nagios_group){
581 $this->attrs['objectClass'][]= "nagiosContactGroup";
582 }
584 /* Take members array */
585 if (count ($this->memberUid)){
586 $this->attrs['memberUid']= array_unique($this->memberUid);
587 }
589 /* New accounts need proper 'dn', propagate it to remaining objects */
590 if ($this->dn == 'new'){
591 $this->dn= 'cn='.$this->cn.','.get_groups_ou().$this->base;
592 }
594 /* Save data. Using 'modify' implies that the entry is already present, use 'add' for
595 new entries. So do a check first... */
596 $ldap->cat ($this->dn);
597 if ($ldap->fetch()){
598 /* Modify needs array() to remove values :-( */
599 if (!count ($this->memberUid)){
600 $this->attrs['memberUid']= array();
601 }
602 if ($this->samba3){
603 if (!$this->smbgroup){
604 $this->attrs['sambaGroupType']= array();
605 $this->attrs['sambaSID']= array();
606 }
607 }
608 $mode= "modify";
609 } else {
610 $mode= "add";
611 $ldap->cd($this->config->current['BASE']);
612 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
613 }
615 /* Write back to ldap */
616 $ldap->cd($this->dn);
617 $this->cleanup();
618 $ldap->$mode($this->attrs);
620 $ret= 0;
621 if (show_ldap_error($ldap->get_error())){
622 $ret= 1;
623 }
625 /* Remove uid lock */
626 del_lock ("uidnumber");
628 /* Post that we've done*/
629 $this->handle_post_events($mode);
631 return ($ret);
632 }
634 function check()
635 {
636 $message= array();
638 /* Permissions for that base? */
639 if ($this->base != ""){
640 $new_dn= 'cn='.$this->cn.','.get_groups_ou().$this->base;
641 } else {
642 $new_dn= $this->dn;
643 }
645 $ui= get_userinfo();
646 $acl= get_permissions ($ui->dn, $ui->subtreeACL);
647 $acl= get_module_permission($acl, "group", $ui->dn);
648 if (chkacl($this->acl, "create") != ""){
649 $message[]= _("You have no permissions to create a group on this 'Base'.");
650 }
652 /* must: cn */
653 if ($this->cn == "" && chkacl ($this->acl, "cn") == ""){
654 $message[]= "The required field 'Name' is not set.";
655 }
657 /* Check for valid input */
658 if (!is_uid($this->cn)){
659 $message[]= _("The field 'Name' contains invalid characters. Lowercase, numbers and dashes are allowed.");
660 }
662 /* Check for used 'cn' */
663 $ldap= $this->config->get_ldap_link();
664 $ldap->cd($this->config->current['BASE']);
665 $ldap->search("(&(|(objectClass=gosaGroupOfNames)(objectClass=posixGroup))(cn=$this->cn))",array("cn"));
666 if ($ldap->count() != 0){
668 /* New entry? */
669 if ($this->dn == 'new'){
670 $message[]= _("Value specified as 'Name' is already used.");
671 }
673 /* Moved? */
674 elseif ($new_dn != $this->orig_dn){
675 $ldap->fetch();
676 if ($ldap->getDN() != $this->orig_dn){
677 $message[]= _("Value specified as 'Name' is already used.");
678 }
679 }
680 }
682 /* Check ID */
683 if ($this->force_gid == "1"){
684 if (!is_id($this->gidNumber)){
685 $message[]= _("Value specified as 'GID' is not valid.");
686 } else {
687 if ($this->gidNumber < $this->config->current['MINID']){
688 $message[]= _("Value specified as 'GID' is too small.");
689 }
691 }
692 }
694 return ($message);
695 }
697 function get_next_id($attrib)
698 {
699 $ids= array();
700 $ldap= $this->config->get_ldap_link();
702 $ldap->cd ($this->config->current['BASE']);
703 if (preg_match('/gidNumber/i', $attrib)){
704 $oc= "posixGroup";
705 } else {
706 $oc= "posixAccount";
707 }
708 $ldap->search ("(&(objectClass=$oc)($attrib=*))", array("$attrib"));
710 /* Get list of ids */
711 while ($attrs= $ldap->fetch()){
712 $ids[]= (int)$attrs["$attrib"][0];
713 }
715 /* Find out next free id near to UID_BASE */
716 for ($id= $this->config->current['UIDBASE']; $id++; $id<65000){
717 if (!in_array($id, $ids)){
718 return ($id);
719 }
720 }
722 /* Should not happen */
723 if ($id == 65000){
724 print_red(_("Too many users, can't allocate a free ID!"));
725 exit;
726 }
727 }
729 }
731 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
732 ?>