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 */
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 /* Do we represent a valid group? */
162 if (!$this->is_account && $this->parent == NULL){
163 $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\"> <b>".
164 _("This 'dn' is no group.")."</b>";
165 return ($display);
166 }
168 /* Delete user from group */
169 if (isset($_POST['del_users']) && isset($_POST['members'])){
170 foreach ($_POST['members'] as $value){
171 unset ($this->members["$value"]);
172 $this->removeUser($value);
173 }
174 $this->reload();
175 }
177 /* Add objects? */
178 if (isset($_POST["edit_membership"])){
179 $this->group_dialog= TRUE;
180 $this->dialog= TRUE;
181 }
183 /* Add objects finished? */
184 if (isset($_POST["add_users_finish"]) || isset($_POST["add_users_cancel"])){
185 $this->group_dialog= FALSE;
186 $this->dialog= FALSE;
187 }
189 /* Add user to group */
190 if (isset($_POST['add_users_finish']) && isset($_POST['users'])){
191 foreach ($_POST['users'] as $value){
192 $this->members["$value"]= $this->allusers[$value];
193 asort($this->members);
194 $this->addUser($value);
195 }
196 $this->reload();
197 }
199 /* Assign templating stuff */
200 $smarty= get_smarty();
201 if ($this->samba3){
202 $smarty->assign("samba3", "true");
203 } else {
204 $smarty->assign("samba3", "");
205 }
207 if(search_config($this->config->data['MENU'], "nagiosaccount", "CLASS")){
208 $smarty->assign("nagios",true);
209 }else{
210 $smarty->assign("nagios",false);
211 }
213 if(search_config($this->config->data['MENU'], "phoneAccount", "CLASS")){
214 $smarty->assign("pickupGroup",true);
215 }else{
216 $smarty->assign("pickupGroup",false);
217 }
219 /* Manage object add dialog */
220 if ($this->group_dialog){
222 /* Save data */
223 $gufilter= get_global("gufilter");
224 foreach( array("dselect", "regex") as $type){
225 if (isset($_POST[$type])){
226 $gufilter[$type]= $_POST[$type];
227 }
228 }
229 if (isset($_GET['search'])){
230 $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
231 if ($s == "**"){
232 $s= "*";
233 }
234 $gufilter['regex']= $s;
235 }
236 $regex= preg_replace('/[*]/', ".*", $gufilter['regex']);
237 register_global("gufilter", $gufilter);
238 $this->reload();
240 /* Show dialog */
241 $smarty->assign("search_image", get_template_path('images/search.png'));
242 $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
243 $smarty->assign("tree_image", get_template_path('images/tree.png'));
244 $smarty->assign("deplist", $this->config->idepartments);
245 $smarty->assign("alphabet", generate_alphabet());
246 foreach( array("dselect", "regex") as $type){
247 $smarty->assign("$type", $gufilter[$type]);
248 }
249 $smarty->assign("hint", print_sizelimit_warning());
251 $users= array();
252 foreach ($this->allusers as $key => $value){
253 if (!array_key_exists($key, $this->members)){
254 if (preg_match("/^$regex/i", $key)){
255 $users[$key]= $value;
256 }
257 }
258 }
259 $smarty->assign("users", $users);
260 $smarty->assign("apply", apply_filter());
261 $display= $smarty->fetch (get_template_path('group_objects.tpl', TRUE, dirname(__FILE__)));
262 return ($display);
263 }
265 /* Bases / Departments */
266 if (isset($_POST['base'])){
267 $this->base= $_POST['base'];
268 }
270 $smarty->assign("bases", $this->config->idepartments);
271 $smarty->assign("base_select", $this->base);
272 $smarty->assign("department", $this->department);
274 if ($this->samba3){
275 $domains= array();
276 foreach($this->config->data['SERVERS']['SAMBA'] as $name => $content){
277 $domains[$name]= $name;
278 }
279 $smarty->assign("sambaDomains", $domains);
280 $smarty->assign("sambaDomainName", $this->sambaDomainName);
281 $groupTypes= array(0 => _("Samba group"), 512 => _("Domain admins"), 513 => _("Domain users"),
282 514 => _("Domain guests"));
284 /* Don't loose special groups! If not key'ed above, add it to
285 the combo box... */
286 if ($this->groupType >= 500 && $this->groupType <= 553 && !isset($groupTypes[$this->groupType])){
287 $groupTypes[$this->groupType]= sprintf(_("Special group (%d)"), $this->groupType);
288 }
290 $smarty->assign("groupTypes", $groupTypes);
291 $smarty->assign("groupType", $this->groupType);
292 }
294 /* Members and users */
295 $smarty->assign("members", $this->members);
297 /* Checkboxes */
298 foreach (array("force_gid", "smbgroup") as $val){
299 if ($this->$val == "1"){
300 $smarty->assign("$val", "checked");
301 } else {
302 $smarty->assign("$val", "");
303 }
304 }
305 if ($this->force_gid != "1"){
306 $smarty->assign("forceMode", "disabled");
307 }else{
308 $smarty->assign("forceMode", "");
309 }
310 $smarty->assign("force_gidACL", chkacl($this->acl, "gidNumber"));
311 $smarty->assign("sambaDomainNameACL", chkacl($this->acl, "sambaDomainName"));
312 if ($this->fon_group){
313 $smarty->assign("fon_group", "checked");
314 } else {
315 $smarty->assign("fon_group", "");
316 }
317 $smarty->assign("fon_groupACL", chkacl($this->acl, "fon_group"));
319 if ($this->nagios_group){
320 $smarty->assign("nagios_group", "checked");
321 } else {
322 $smarty->assign("nagios_group", "");
323 }
324 $smarty->assign("nagios_groupACL", chkacl($this->acl, "nagios_group"));
326 /* Fields */
327 foreach (array("cn", "description", "gidNumber") as $val){
328 $smarty->assign("$val", $this->$val);
329 $smarty->assign("$val"."ACL", chkacl($this->acl, "$val"));
330 }
332 /* Missing ACL's */
333 foreach (array("base", "smbgroup", "members") as $val){
334 $smarty->assign("$val"."ACL", chkacl($this->acl, "$val"));
335 }
337 /* Show main page */
338 $smarty->assign("alphabet", generate_alphabet(10));
339 $smarty->assign("search_image", get_template_path('images/search.png'));
340 $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
341 $smarty->assign("tree_image", get_template_path('images/tree.png'));
342 $smarty->assign("deplist", $this->config->idepartments);
343 return($smarty->fetch (get_template_path('generic.tpl', TRUE)));
344 }
346 function addUser($uid)
347 {
348 $this->memberUid[]= $uid;
349 $this->memberUid= array_unique($this->memberUid);
350 }
352 function removeUser($uid)
353 {
354 $temp= array();
355 foreach ($this->memberUid as $value){
356 if ($value != $uid){
357 $temp[]= $value;
358 }
359 }
360 $this->memberUid= $temp;
361 }
364 /* Reload data */
365 function reload()
366 {
367 /* Generate userlists */
368 $this->last_sorting= "invalid";
369 $this->users= array();
370 $ldap= $this->config->get_ldap_link();
371 $gufilter= get_global("gufilter");
372 $ldap->cd ($gufilter['dselect']);
373 $ldap->search ("(&(objectClass=gosaAccount)(!(objectClass=gosaUserTemplate))(!(uid=*$)))");
374 natcasesort ($this->users);
375 reset ($this->users);
377 $ldap->cd ($gufilter['dselect']);
378 $ldap->search ("(&(objectClass=gosaAccount)(!(objectClass=gosaUserTemplate))(!(uid=*$)))");
379 $this->allusers= array();
380 while ($attrs= $ldap->fetch()){
381 if (isset($attrs["givenName"][0]) && isset($attrs["sn"][0])){
382 $this->allusers[$attrs["uid"][0]]= $attrs["sn"][0].", ".
383 $attrs["givenName"][0].
384 " [".$attrs["uid"][0]."]";
385 } else {
386 $this->allusers[$attrs["uid"][0]]= $attrs['uid'][0];
387 }
388 }
389 natcasesort ($this->allusers);
390 reset ($this->allusers);
392 /* Fill memberlist */
393 $this->members= array();
394 foreach ($this->memberUid as $value){
395 if (isset($this->allusers[$value])){
396 $this->members[$value]= $this->allusers[$value];
397 } else {
398 $this->members[$value]= "[$value]";
399 }
400 }
401 asort($this->members);
402 reset($this->members);
403 }
407 function remove_from_parent()
408 {
409 plugin::remove_from_parent();
411 $ldap= $this->config->get_ldap_link();
412 $ldap->rmdir($this->dn);
413 show_ldap_error($ldap->get_error());
415 /* Delete references to object groups */
416 $ldap->cd ($this->config->current['BASE']);
417 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
418 while ($ldap->fetch()){
419 $og= new ogroup($this->config, $ldap->getDN());
420 unset($og->member[$this->dn]);
421 $og->save ();
422 }
424 /* Send signal to the world that we've done */
425 $this->handle_post_events("remove");
426 }
429 /* Save data to object */
430 function save_object()
431 {
432 /* Save additional values for possible next step */
433 if (isset($_POST['groupedit'])){
434 plugin::save_object();
436 $this->force_gid= 0;
437 $this->smbgroup= 0;
438 foreach (array("force_gid", "department", "base", "smbgroup") as $val) {
439 if (chkacl ($this->acl, "$val") == "" && isset($_POST["$val"])){
440 $this->$val= $_POST["$val"];
441 }
442 }
444 /* Save sambaDomain attribute */
445 if (chkacl ($this->acl, "sambaDomainName") == "" && $this->samba3 &&
446 isset ($_POST['sambaDomainName'])){
448 $this->sambaDomainName= $_POST['sambaDomainName'];
449 $this->groupType= $_POST['groupType'];
450 }
452 /* Save fon attribute */
453 if (chkacl ($this->acl, "fon_group") == ""){
454 if (isset ($_POST['fon_group'])){
455 $this->fon_group= TRUE;
456 } else {
457 $this->fon_group= FALSE;
458 }
459 }
460 if (chkacl ($this->acl, "nagios_group") == ""){
461 if (isset ($_POST['nagios_group'])){
462 $this->nagios_group= TRUE;
463 } else {
464 $this->nagios_group= FALSE;
465 }
466 }
467 }
468 }
471 /* Save to LDAP */
472 function save()
473 {
474 /* ID handling */
475 if ($this->force_gid == 0){
476 if ($this->saved_gidNumber != ""){
477 $this->gidNumber= $this->saved_gidNumber;
478 } else {
479 /* Calculate new, lock uids */
480 $wait= 10;
481 while (get_lock("uidnumber") != ""){
482 sleep (1);
484 /* timed out? */
485 if ($wait-- == 0){
486 break;
487 }
488 }
489 add_lock ("uidnumber", "gosa");
490 $this->gidNumber= $this->get_next_id("gidNumber");
491 }
492 }
494 plugin::save();
496 /* Remove objectClass for samba/phone support */
497 $tmp= array();
498 for ($i= 0; $i<count($this->attrs["objectClass"]); $i++){
499 if ($this->attrs['objectClass'][$i] != 'sambaGroupMapping' &&
500 $this->attrs['objectClass'][$i] != 'sambaIdmapEntry' &&
501 $this->attrs['objectClass'][$i] != 'goFonPickupGroup' &&
502 $this->attrs['objectClass'][$i] != 'nagiosContactGroup'){
503 $tmp[]= $this->attrs['objectClass'][$i];
504 }
505 }
506 $this->attrs['objectClass']= $tmp;
507 $ldap= $this->config->get_ldap_link();
509 /* Add samba group functionality */
510 if ($this->samba3 && $this->smbgroup){
511 $this->SID= $this->config->data['SERVERS']['SAMBA'][$this->sambaDomainName]['SID'];
512 $this->ridBase= $this->config->data['SERVERS']['SAMBA'][$this->sambaDomainName]['RIDBASE'];
514 $this->attrs['objectClass'][]= 'sambaGroupMapping';
515 $this->attrs['sambaGroupType']= "2";
517 /* Check if we need to create a special entry */
518 if ($this->groupType == 0){
520 if ($this->sambaSID == "" || $this->oldgroupType != $this->groupType){
521 $gidNumber= $this->gidNumber;
522 while(TRUE){
523 $sid= $this->SID."-".($gidNumber*2 + $this->ridBase+1);
524 $ldap->cd($this->config->current['BASE']);
525 $ldap->search("(sambaSID=$sid)");
526 if ($ldap->count() == 0){
527 break;
528 }
529 $gidNumber++;
530 }
531 $this->attrs['sambaSID']= $sid;
532 $this->sambaSID= $sid;
533 }
535 } else {
536 $this->attrs['sambaSID']=$this->SID."-".$this->groupType;
537 }
539 /* User wants me to fake the idMappings? This is useful for
540 making winbind resolve the group names in a reasonable amount
541 of time in combination with larger databases. */
542 if (isset($this->config->current['SAMBAIDMAPPING']) &&
543 preg_match('/true/i', $this->config->current['SAMBAIDMAPPING'])){
544 $this->attrs['objectClass'][]= "sambaIdmapEntry";
545 }
547 }
549 /* Add phone functionality */
550 if ($this->fon_group){
551 $this->attrs['objectClass'][]= "goFonPickupGroup";
552 }
554 /* Add nagios functionality */
555 if ($this->nagios_group){
556 $this->attrs['objectClass'][]= "nagiosContactGroup";
557 }
559 /* Take members array */
560 if (count ($this->memberUid)){
561 $this->attrs['memberUid']= array_unique($this->memberUid);
562 }
564 /* New accounts need proper 'dn', propagate it to remaining objects */
565 if ($this->dn == 'new'){
566 $this->dn= 'cn='.$this->cn.','.get_groups_ou().$this->base;
567 }
569 /* Save data. Using 'modify' implies that the entry is already present, use 'add' for
570 new entries. So do a check first... */
571 $ldap->cat ($this->dn);
572 if ($ldap->fetch()){
573 /* Modify needs array() to remove values :-( */
574 if (!count ($this->memberUid)){
575 $this->attrs['memberUid']= array();
576 }
577 if ($this->samba3){
578 if (!$this->smbgroup){
579 $this->attrs['sambaGroupType']= array();
580 $this->attrs['sambaSID']= array();
581 }
582 }
583 $mode= "modify";
584 } else {
585 $mode= "add";
586 $ldap->cd($this->config->current['BASE']);
587 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
588 }
590 /* Write back to ldap */
591 $ldap->cd($this->dn);
592 $ldap->$mode($this->attrs);
594 $ret= 0;
595 if (show_ldap_error($ldap->get_error())){
596 $ret= 1;
597 }
599 /* Remove uid lock */
600 del_lock ("uidnumber");
602 /* Post that we've done*/
603 $this->handle_post_events($mode);
605 return ($ret);
606 }
608 function check()
609 {
610 $message= array();
612 /* Permissions for that base? */
613 if ($this->base != ""){
614 $new_dn= 'cn='.$this->cn.','.get_groups_ou().$this->base;
615 } else {
616 $new_dn= $this->dn;
617 }
619 $ui= get_userinfo();
620 $acl= get_permissions ($new_dn, $ui->subtreeACL);
621 $acl= get_module_permission($acl, "group", $new_dn);
622 if (chkacl($acl, "create") != ""){
623 $message[]= _("You have no permissions to create a group on this 'Base'.");
624 }
626 /* must: cn */
627 if ($this->cn == "" && chkacl ($this->acl, "cn") == ""){
628 $message[]= "The required field 'Name' is not set.";
629 }
631 /* Check for valid input */
632 if (!is_uid($this->cn)){
633 $message[]= _("The field 'Name' contains invalid characters. Lowercase, numbers and dashes are allowed.");
634 }
636 /* Check for used 'cn' */
637 $ldap= $this->config->get_ldap_link();
638 $ldap->cd($this->config->current['BASE']);
639 $ldap->search("(&(|(objectClass=gosaGroupOfNames)(objectClass=posixGroup))(cn=$this->cn))");
640 if ($ldap->count() != 0){
642 /* New entry? */
643 if ($this->dn == 'new'){
644 $message[]= _("Value specified as 'Name' is already used.");
645 }
647 /* Moved? */
648 elseif ($new_dn != $this->orig_dn){
649 $ldap->fetch();
650 if ($ldap->getDN() != $this->orig_dn){
651 $message[]= _("Value specified as 'Name' is already used.");
652 }
653 }
654 }
656 /* Check ID */
657 if ($this->force_gid == "1"){
658 if (!is_id($this->gidNumber)){
659 $message[]= _("Value specified as 'GID' is not valid.");
660 } else {
661 if ($this->gidNumber < $this->config->current['MINID']){
662 $message[]= _("Value specified as 'GID' is too small.");
663 }
665 }
666 }
668 return ($message);
669 }
671 function get_next_id($attrib)
672 {
673 $ids= array();
674 $ldap= $this->config->get_ldap_link();
676 $ldap->cd ($this->config->current['BASE']);
677 $ldap->search ("($attrib=*)");
679 /* Get list of ids */
680 while ($attrs= $ldap->fetch()){
681 $ids[]= (int)$attrs["$attrib"][0];
682 }
684 /* Find out next free id near to UID_BASE */
685 for ($id= $this->config->current['UIDBASE']; $id++; $id<65000){
686 if (!in_array($id, $ids)){
687 return ($id);
688 }
689 }
691 /* Should not happen */
692 if ($id == 65000){
693 print_red(_("Too many users, can't allocate a free ID!"));
694 exit;
695 }
696 }
698 }
700 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
701 ?>