Code

8b2c2b6ae20e7e86ee88ca1ce3f2a2c9e17cdc7c
[gosa.git] / plugins / admin / groups / class_groupGeneric.inc
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;
37   /* attribute list for save action */
38   var $attributes= array("cn", "description", "gidNumber");
39   var $objectclasses= array("top", "posixGroup");
41   function group ($config, $dn= NULL)
42   {
43     plugin::plugin ($config, $dn);
45     /* Load attributes depending on the samba version */
46     $this->samba3= ($config->current['SAMBAVERSION'] == 3);
47     $this->orig_dn= $dn;
49     /* Get member list */
50     if (isset($this->attrs['memberUid'][0])){
51       $tmp= array();
52       for ($i= 0; $i<$this->attrs['memberUid']['count']; $i++){
53         $tmp[]= $this->attrs['memberUid'][$i];
54       }
55       $this->memberUid= $tmp;
56       sort ($this->memberUid);
57     }
59     /* Save gidNumber for later use */
60     if (isset($this->attrs['gidNumber'])){
61       $this->saved_gidNumber= $this->attrs['gidNumber'][0];
62     }
64     /* Is a samba group? */
65     if (isset($this->attrs['objectClass'])){
66       if (array_search ('sambaGroupMapping', $this->attrs['objectClass']) == NULL ){
67         $this->smbgroup= FALSE;
68       } else {
69         $this->smbgroup= TRUE;
70         if (isset($this->attrs['sambaSID'])){
71           $this->sambaSID= $this->attrs['sambaSID'][0];
72         }
73       }
74       if (array_search ('goFonPickupGroup', $this->attrs['objectClass']) == NULL ){
75         $this->fon_group= FALSE;
76       } else {
77         $this->fon_group= TRUE;
78       }
79       if (array_search ('nagiosContactGroup', $this->attrs['objectClass']) == NULL ){
80         $this->nagios_group= FALSE;
81       } else {
82         $this->nagios_group= TRUE;
83       }
84     }
86     /* Set mail flag */
87     if (isset($this->attrs['objectClass']) && in_array('gosaMailAccount', $this->attrs['objectClass'])){
88       $this->has_mailAccount= TRUE;
89     }
91     /* Get samba Domain in case of samba 3 */
92     if ($this->samba3 && $this->sambaSID != ""){
93       $this->SID= preg_replace ("/-[^-]+$/", "", $this->sambaSID);
94       $ldap= $this->config->get_ldap_link();
95       $ldap->cd($this->config->current['BASE']);
96       $ldap->search ("(&(objectClass=sambaDomain)(sambaSID=$this->SID))");
97       if ($ldap->count() != 0){
98         $attrs= $ldap->fetch();
99         $this->ridBase= $attrs['sambaAlgorithmicRidBase'][0];
101         /* Get domain name for SID */
102         $this->sambaDomainName= "DEFAULT";
103         foreach ($this->config->data['SERVERS']['SAMBA'] as $key => $val){
104           if ($val['SID'] == $this->SID){
105             $this->sambaDomainName= $key;
106             break;
107           }
108         }
109       } else {
110         if (isset($this->config->current['RIDBASE'])){
111           $this->sambaDomainName= "DEFAULT";
112           $this->ridBase= $this->config->current['RIDBASE'];
113           $this->SID= $this->config->current['SID'];
114         } else {
115           print_red(_("Can't find this groups SID in LDAP or in your configuration file!"));
116         }
117       }
119       /* Get group type */
120       $this->groupType= (int)substr(strrchr($this->sambaSID, "-"), 1);
121       if ($this->groupType < 500 || $this->groupType > 553){
122         $this->groupType= 0;
123       }
124       $this->oldgroupType= $this->groupType;
125     }
127     /* Get global filter config */
128     if (!is_global("gufilter")){
129       $ui= get_userinfo();
130       $base= get_base_from_people($ui->dn);
131       $gufilter= array( "dselect"       => $base,
132           "regex"           => "*");
133       register_global("gufilter", $gufilter);
134     }
135     $gufilter= get_global('gufilter');
137       /* Bases / Departments */
138       
139     if(isset($_SESSION['groupfilter']['depselect'])){
140       $this->base = $_SESSION['groupfilter']['depselect'];
141     }else{
142       if ($this->dn == "new"){
143         $ui= get_userinfo();
144         $this->base= dn2base($ui->dn);
145       } else {
146         $this->base= preg_replace ("/^[^,]+,[^,]+,/", "", $this->dn);
147       }
148     }
151     /* This is always an account */
152     $this->is_account= TRUE;
153     $this->reload();
154   }
156   function execute()
157   {
158         /* Call parent execute */
159         plugin::execute();
161   $ui= get_userinfo();
162   $acla= get_permissions ($ui->dn, $ui->subtreeACL);
163   $this->acl= get_module_permission($acla, "group", $ui->dn);
164   /* Do we represent a valid group? */
165     if (!$this->is_account && $this->parent == NULL){
166       $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\">&nbsp;<b>".
167         _("This 'dn' is no group.")."</b>";
168       return ($display);
169     }
171     /* Delete user from group */
172     if (isset($_POST['del_users']) && isset($_POST['members'])){
173       foreach ($_POST['members'] as $value){
174         unset ($this->members["$value"]);
175         $this->removeUser($value);
176       }
177       $this->reload();
178     }
180     /* Add objects? */
181     if (isset($_POST["edit_membership"])){
182       $this->group_dialog= TRUE;
183       $this->dialog= TRUE;
184     }
186     /* Add objects finished? */
187     if (isset($_POST["add_users_finish"]) || isset($_POST["add_users_cancel"])){
188       $this->group_dialog= FALSE;
189       $this->dialog= FALSE;
190     }
192     /* Add user to group */
193     if (isset($_POST['add_users_finish']) && isset($_POST['users'])){
194       foreach ($_POST['users'] as $value){
195         $this->members["$value"]= $this->allusers[$value];
196         asort($this->members);
197         $this->addUser($value);
198       }
199       $this->reload();
200     }
202     /* Assign templating stuff */
203     $smarty= get_smarty();
204     if ($this->samba3){
205       $smarty->assign("samba3", "true");
206     } else {
207       $smarty->assign("samba3", "");
208     }
210     if(search_config($this->config->data['MENU'], "nagiosaccount", "CLASS")){
211       $smarty->assign("nagios",true);
212     }else{
213       $smarty->assign("nagios",false);
214     }
215     
216     if(search_config($this->config->data['MENU'], "phoneAccount", "CLASS")){
217       $smarty->assign("pickupGroup",true);
218     }else{
219       $smarty->assign("pickupGroup",false);
220     }
222     /* Manage object add dialog */
223     if ($this->group_dialog){
225       /* Save data */
226       $gufilter= get_global("gufilter");
227       foreach( array("dselect", "regex") as $type){
228         if (isset($_POST[$type])){
229           $gufilter[$type]= $_POST[$type];
230         }
231       }
232       if (isset($_GET['search'])){
233         $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
234         if ($s == "**"){
235           $s= "*";
236         }
237         $gufilter['regex']= $s;
238       }
239       $regex= preg_replace('/[*]/', ".*", $gufilter['regex']);
240       register_global("gufilter", $gufilter);
241       $this->reload();
243       /* Show dialog */
244       $smarty->assign("search_image", get_template_path('images/search.png'));
245       $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
246       $smarty->assign("tree_image", get_template_path('images/tree.png'));
247       $smarty->assign("deplist", $this->config->idepartments);
248       $smarty->assign("alphabet", generate_alphabet());
249       foreach( array("dselect", "regex") as $type){
250         $smarty->assign("$type", $gufilter[$type]);
251       }
252       $smarty->assign("hint", print_sizelimit_warning());
254       $users= array();
255       foreach ($this->allusers as $key => $value){
256         if (!array_key_exists($key, $this->members)){
257           if (preg_match("/^$regex/i", $key)){
258             $users[$key]= $value;
259           }
260         }
261       }
262       $smarty->assign("users", $users);
263       $smarty->assign("apply", apply_filter());
264       $display= $smarty->fetch (get_template_path('group_objects.tpl', TRUE, dirname(__FILE__)));
265       return ($display);
266     }
268     /* Bases / Departments */
269     if (isset($_POST['base'])){
270       $this->base= $_POST['base'];
271     }
273     $smarty->assign("bases", $this->config->idepartments);
274     $smarty->assign("base_select", $this->base);
275     $smarty->assign("department", $this->department);
277     if ($this->samba3){
278       $domains= array();
279       foreach($this->config->data['SERVERS']['SAMBA'] as $name => $content){
280         $domains[$name]= $name;
281       }
282       $smarty->assign("sambaDomains", $domains);
283       $smarty->assign("sambaDomainName", $this->sambaDomainName);
284       $groupTypes= array(0 => _("Samba group"), 512 => _("Domain admins"), 513 => _("Domain users"),
285           514 => _("Domain guests"));
287       /* Don't loose special groups! If not key'ed above, add it to
288          the combo box... */    
289       if ($this->groupType >= 500 && $this->groupType <= 553 && !isset($groupTypes[$this->groupType])){
290         $groupTypes[$this->groupType]= sprintf(_("Special group (%d)"), $this->groupType);
291       }
293       $smarty->assign("groupTypes", $groupTypes);
294       $smarty->assign("groupType", $this->groupType);
295     }
297     /* Members and users */
298     $smarty->assign("members", $this->members);
300     /* Checkboxes */
301     foreach (array("force_gid", "smbgroup") as $val){
302       if ($this->$val == "1"){
303         $smarty->assign("$val", "checked");
304       } else {
305         $smarty->assign("$val", "");
306       }
307     }
308     if ($this->force_gid != "1"){
309       $smarty->assign("forceMode", "disabled");
310     }else{
311       $smarty->assign("forceMode", "");
312     }
313     $smarty->assign("force_gidACL", chkacl($this->acl, "gidNumber"));
314     $smarty->assign("sambaDomainNameACL", chkacl($this->acl, "sambaDomainName"));
315     if ($this->fon_group){
316       $smarty->assign("fon_group", "checked");
317     } else {
318       $smarty->assign("fon_group", "");
319     }
320     $smarty->assign("fon_groupACL", chkacl($this->acl, "fon_group"));
322     if ($this->nagios_group){
323       $smarty->assign("nagios_group", "checked");
324     } else {
325       $smarty->assign("nagios_group", "");
326     }
327     $smarty->assign("nagios_groupACL", chkacl($this->acl, "nagios_group"));
329     /* Fields */
330     foreach (array("cn", "description", "gidNumber") as $val){
331       $smarty->assign("$val", $this->$val);
332       $smarty->assign("$val"."ACL", chkacl($this->acl, "$val"));
333     }
335     /* Missing ACL's */
336     foreach (array("base", "smbgroup", "members") as $val){
337       $smarty->assign("$val"."ACL", chkacl($this->acl, "$val"));
338     }
340     /* Show main page */
341     $smarty->assign("alphabet", generate_alphabet(10));
342     $smarty->assign("search_image", get_template_path('images/search.png'));
343     $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
344     $smarty->assign("tree_image", get_template_path('images/tree.png'));
345     $smarty->assign("deplist", $this->config->idepartments);
346     return($smarty->fetch (get_template_path('generic.tpl', TRUE)));
347   }
349   function addUser($uid)
350   {
351     $this->memberUid[]= $uid;
352     $this->memberUid= array_unique($this->memberUid);
353   }
355   function removeUser($uid)
356   {
357     $temp= array();
358     foreach ($this->memberUid as $value){
359       if ($value != $uid){
360         $temp[]= $value;
361       }
362     }
363     $this->memberUid= $temp;
364   }
367   /* Reload data */
368   function reload()
369   {
370     /* Generate userlists */
371     $this->last_sorting= "invalid";
372     $this->users= array();
373     $ldap= $this->config->get_ldap_link();
374     $gufilter= get_global("gufilter");
375     $ldap->cd ($gufilter['dselect']);
376     $ldap->search ("(&(objectClass=gosaAccount)(!(objectClass=gosaUserTemplate))(!(uid=*$)))");
377     natcasesort ($this->users);
378     reset ($this->users);
380     $ldap->cd ($gufilter['dselect']);
381     $ldap->search ("(&(objectClass=gosaAccount)(!(objectClass=gosaUserTemplate))(!(uid=*$)))");
382     $this->allusers= array();
383     while ($attrs= $ldap->fetch()){
384       if (isset($attrs["givenName"][0]) && isset($attrs["sn"][0])){
385         $this->allusers[$attrs["uid"][0]]= $attrs["sn"][0].", ".
386           $attrs["givenName"][0].
387           " [".$attrs["uid"][0]."]";
388       } else {
389         $this->allusers[$attrs["uid"][0]]= $attrs['uid'][0];
390       }
391     }
392     natcasesort ($this->allusers);
393     reset ($this->allusers);
395     /* Fill memberlist */
396     $this->members= array();
397     foreach ($this->memberUid as $value){
398       if (isset($this->allusers[$value])){
399         $this->members[$value]= $this->allusers[$value];
400       } else {
401         $this->members[$value]= "[$value]";
402       }
403     }
404     asort($this->members);
405     reset($this->members);
406   }
410   function remove_from_parent()
411   {
412     plugin::remove_from_parent();
414     $ldap= $this->config->get_ldap_link();
415     $ldap->rmdir($this->dn);
416     show_ldap_error($ldap->get_error());
418     /* Delete references to object groups */
419     $ldap->cd ($this->config->current['BASE']);
420     $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
421     while ($ldap->fetch()){
422       $og= new ogroup($this->config, $ldap->getDN());
423       unset($og->member[$this->dn]);
424       $og->save ();
425     }
427     /* Send signal to the world that we've done */
428     $this->handle_post_events("remove");
429   }
432   /* Save data to object */
433   function save_object()
434   {
435     /* Save additional values for possible next step */
436     if (isset($_POST['groupedit'])){
437       plugin::save_object();
439       $this->force_gid= 0;
440       $this->smbgroup= 0;
441       foreach (array("force_gid", "department", "base", "smbgroup") as $val) {
442         if (chkacl ($this->acl, "$val") == "" && isset($_POST["$val"])){
443           $this->$val= $_POST["$val"];
444         }
445       }
447       /* Save sambaDomain attribute */
448       if (chkacl ($this->acl, "sambaDomainName") == "" && $this->samba3 &&
449           isset ($_POST['sambaDomainName'])){
451         $this->sambaDomainName= $_POST['sambaDomainName'];
452         $this->groupType= $_POST['groupType'];
453       }
455       /* Save fon attribute */
456       if (chkacl ($this->acl, "fon_group") == ""){
457         if (isset ($_POST['fon_group'])){
458           $this->fon_group= TRUE;
459         } else {
460           $this->fon_group= FALSE;
461         }
462       }
463          if (chkacl ($this->acl, "nagios_group") == ""){
464         if (isset ($_POST['nagios_group'])){
465           $this->nagios_group= TRUE;
466         } else {
467           $this->nagios_group= FALSE;
468         }
469       }
470     }
471   }
474   /* Save to LDAP */
475   function save()
476   {
477     /* ID handling */
478     if ($this->force_gid == 0){
479       if ($this->saved_gidNumber != ""){
480         $this->gidNumber= $this->saved_gidNumber;
481       } else {
482         /* Calculate new, lock uids */
483         $wait= 10;
484         while (get_lock("uidnumber") != ""){
485           sleep (1);
487           /* timed out? */
488           if ($wait-- == 0){
489             break;
490           }
491         }
492         add_lock ("uidnumber", "gosa");
493         $this->gidNumber= $this->get_next_id("gidNumber");
494       }
495     }
497     plugin::save(); 
499     /* Remove objectClass for samba/phone support */
500     $tmp= array();
501     for ($i= 0; $i<count($this->attrs["objectClass"]); $i++){
502       if ($this->attrs['objectClass'][$i] != 'sambaGroupMapping' &&
503           $this->attrs['objectClass'][$i] != 'sambaIdmapEntry' &&
504           $this->attrs['objectClass'][$i] != 'goFonPickupGroup' &&
505          $this->attrs['objectClass'][$i] != 'nagiosContactGroup'){
506         $tmp[]= $this->attrs['objectClass'][$i];
507       }
508     }
509     $this->attrs['objectClass']= $tmp;
510     $ldap= $this->config->get_ldap_link();
512     /* Add samba group functionality */
513     if ($this->samba3 && $this->smbgroup){
514       $this->SID= $this->config->data['SERVERS']['SAMBA'][$this->sambaDomainName]['SID'];
515       $this->ridBase= $this->config->data['SERVERS']['SAMBA'][$this->sambaDomainName]['RIDBASE'];
517       $this->attrs['objectClass'][]= 'sambaGroupMapping';
518       $this->attrs['sambaGroupType']= "2";
520       /* Check if we need to create a special entry */
521       if ($this->groupType == 0){
523         if ($this->sambaSID == "" || $this->oldgroupType != $this->groupType){
524           $gidNumber= $this->gidNumber;
525           while(TRUE){
526             $sid= $this->SID."-".($gidNumber*2 + $this->ridBase+1);
527             $ldap->cd($this->config->current['BASE']);
528             $ldap->search("(sambaSID=$sid)");
529             if ($ldap->count() == 0){
530               break;
531             }
532             $gidNumber++;
533           }
534           $this->attrs['sambaSID']= $sid;
535           $this->sambaSID= $sid;
536         }
538       } else {
539         $this->attrs['sambaSID']=$this->SID."-".$this->groupType;
540       }
542       /* User wants me to fake the idMappings? This is useful for
543          making winbind resolve the group names in a reasonable amount
544          of time in combination with larger databases. */
545       if (isset($this->config->current['SAMBAIDMAPPING']) &&
546           preg_match('/true/i', $this->config->current['SAMBAIDMAPPING'])){
547         $this->attrs['objectClass'][]= "sambaIdmapEntry";
548       }
550     }
552     /* Add phone functionality */
553     if ($this->fon_group){
554       $this->attrs['objectClass'][]= "goFonPickupGroup";
555     }
557     /* Add nagios functionality */
558     if ($this->nagios_group){
559         $this->attrs['objectClass'][]= "nagiosContactGroup";
560     }
562     /* Take members array */
563     if (count ($this->memberUid)){
564       $this->attrs['memberUid']= array_unique($this->memberUid);
565     }
567     /* New accounts need proper 'dn', propagate it to remaining objects */
568     if ($this->dn == 'new'){
569       $this->dn= 'cn='.$this->cn.','.get_groups_ou().$this->base;
570     }
572     /* Save data. Using 'modify' implies that the entry is already present, use 'add' for
573        new entries. So do a check first... */
574     $ldap->cat ($this->dn);
575     if ($ldap->fetch()){
576       /* Modify needs array() to remove values :-( */
577       if (!count ($this->memberUid)){
578         $this->attrs['memberUid']= array();
579       }
580       if ($this->samba3){
581         if (!$this->smbgroup){
582           $this->attrs['sambaGroupType']= array();
583           $this->attrs['sambaSID']= array();
584         }
585       }
586       $mode= "modify";
587     } else {
588       $mode= "add";
589       $ldap->cd($this->config->current['BASE']);
590       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
591     }
593     /* Write back to ldap */
594     $ldap->cd($this->dn);
595     $ldap->$mode($this->attrs);
597     $ret= 0;
598     if (show_ldap_error($ldap->get_error())){
599       $ret= 1;
600     }
602     /* Remove uid lock */
603     del_lock ("uidnumber");
605     /* Post that we've done*/
606     $this->handle_post_events($mode);
608     return ($ret);
609   }
611   function check()
612   {
613     $message= array();
615     /* Permissions for that base? */
616     if ($this->base != ""){
617       $new_dn= 'cn='.$this->cn.','.get_groups_ou().$this->base;
618     } else {
619       $new_dn= $this->dn;
620     }
622     $ui= get_userinfo();
623     $acl= get_permissions ($ui->dn, $ui->subtreeACL);
624     $acl= get_module_permission($acl, "group", $ui->dn);
625     if (chkacl($this->acl, "create") != ""){
626       $message[]= _("You have no permissions to create a group on this 'Base'.");
627     }
629     /* must: cn */
630     if ($this->cn == "" && chkacl ($this->acl, "cn") == ""){
631       $message[]= "The required field 'Name' is not set.";
632     }
634     /* Check for valid input */
635     if (!is_uid($this->cn)){
636       $message[]= _("The field 'Name' contains invalid characters. Lowercase, numbers and dashes are allowed.");
637     }
639     /* Check for used 'cn' */
640     $ldap= $this->config->get_ldap_link();
641     $ldap->cd($this->config->current['BASE']);
642     $ldap->search("(&(|(objectClass=gosaGroupOfNames)(objectClass=posixGroup))(cn=$this->cn))");
643     if ($ldap->count() != 0){
645       /* New entry? */
646       if ($this->dn == 'new'){
647         $message[]= _("Value specified as 'Name' is already used.");
648       }
649       
650       /* Moved? */
651       elseif ($new_dn != $this->orig_dn){
652         $ldap->fetch();
653         if ($ldap->getDN() != $this->orig_dn){
654           $message[]= _("Value specified as 'Name' is already used.");
655         }
656       }
657     }
659     /* Check ID */
660     if ($this->force_gid == "1"){
661       if (!is_id($this->gidNumber)){
662         $message[]= _("Value specified as 'GID' is not valid.");
663       } else {
664         if ($this->gidNumber < $this->config->current['MINID']){
665           $message[]= _("Value specified as 'GID' is too small.");
666         }
668       }
669     }
671     return ($message);
672   }
674   function get_next_id($attrib)
675   {
676     $ids= array();
677     $ldap= $this->config->get_ldap_link();
679     $ldap->cd ($this->config->current['BASE']);
680     $ldap->search ("($attrib=*)");
682     /* Get list of ids */
683     while ($attrs= $ldap->fetch()){
684       $ids[]= (int)$attrs["$attrib"][0];
685     }
687     /* Find out next free id near to UID_BASE */
688     for ($id= $this->config->current['UIDBASE']; $id++; $id<65000){
689       if (!in_array($id, $ids)){
690         return ($id);
691       }
692     }
694     /* Should not happen */
695     if ($id == 65000){
696       print_red(_("Too many users, can't allocate a free ID!"));
697       exit;
698     }
699   }
703 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
704 ?>