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;
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 /* Assign templating stuff */
205 $smarty= get_smarty();
206 if ($this->samba3){
207 $smarty->assign("samba3", "true");
208 } else {
209 $smarty->assign("samba3", "");
210 }
212 if(search_config($this->config->data['MENU'], "nagiosaccount", "CLASS")){
213 $smarty->assign("nagios",true);
214 }else{
215 $smarty->assign("nagios",false);
216 }
218 if(search_config($this->config->data['MENU'], "phoneAccount", "CLASS")){
219 $smarty->assign("pickupGroup",true);
220 }else{
221 $smarty->assign("pickupGroup",false);
222 }
224 /* Manage object add dialog */
225 if ($this->group_dialog){
227 /* Save data */
228 $gufilter= get_global("gufilter");
229 foreach( array("dselect", "regex") as $type){
230 if (isset($_POST[$type])){
231 $gufilter[$type]= $_POST[$type];
232 }
233 }
234 if (isset($_GET['search'])){
235 $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
236 if ($s == "**"){
237 $s= "*";
238 }
239 $gufilter['regex']= $s;
240 }
241 $regex= preg_replace('/[*]/', ".*", $gufilter['regex']);
242 register_global("gufilter", $gufilter);
243 $this->reload();
245 /* Show dialog */
246 $smarty->assign("search_image", get_template_path('images/search.png'));
247 $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
248 $smarty->assign("tree_image", get_template_path('images/tree.png'));
249 $smarty->assign("deplist", $this->config->idepartments);
250 $smarty->assign("alphabet", generate_alphabet());
251 foreach( array("dselect", "regex") as $type){
252 $smarty->assign("$type", $gufilter[$type]);
253 }
254 $smarty->assign("hint", print_sizelimit_warning());
256 $users= array();
257 foreach ($this->allusers as $key => $value){
258 if (!array_key_exists($key, $this->members)){
259 if (preg_match("/^$regex/i", $key)){
260 $users[$key]= $value;
261 }
262 }
263 }
264 $smarty->assign("users", $users);
265 $smarty->assign("apply", apply_filter());
266 $display= $smarty->fetch (get_template_path('group_objects.tpl', TRUE, dirname(__FILE__)));
267 return ($display);
268 }
270 /* Bases / Departments */
271 if (isset($_POST['base'])){
272 $this->base= $_POST['base'];
273 }
275 $smarty->assign("bases", $this->config->idepartments);
276 $smarty->assign("base_select", $this->base);
277 $smarty->assign("department", $this->department);
279 if ($this->samba3){
280 $domains= array();
281 foreach($this->config->data['SERVERS']['SAMBA'] as $name => $content){
282 $domains[$name]= $name;
283 }
284 $smarty->assign("sambaDomains", $domains);
285 $smarty->assign("sambaDomainName", $this->sambaDomainName);
286 $groupTypes= array(0 => _("Samba group"), 512 => _("Domain admins"), 513 => _("Domain users"),
287 514 => _("Domain guests"));
289 /* Don't loose special groups! If not key'ed above, add it to
290 the combo box... */
291 if ($this->groupType >= 500 && $this->groupType <= 553 && !isset($groupTypes[$this->groupType])){
292 $groupTypes[$this->groupType]= sprintf(_("Special group (%d)"), $this->groupType);
293 }
295 $smarty->assign("groupTypes", $groupTypes);
296 $smarty->assign("groupType", $this->groupType);
297 }
299 /* Members and users */
300 $smarty->assign("members", $this->members);
302 /* Checkboxes */
303 foreach (array("force_gid", "smbgroup") as $val){
304 if ($this->$val == "1"){
305 $smarty->assign("$val", "checked");
306 } else {
307 $smarty->assign("$val", "");
308 }
309 }
310 if ($this->force_gid != "1"){
311 $smarty->assign("forceMode", "disabled");
312 }else{
313 $smarty->assign("forceMode", "");
314 }
315 $smarty->assign("force_gidACL", chkacl($this->acl, "gidNumber"));
316 $smarty->assign("sambaDomainNameACL", chkacl($this->acl, "sambaDomainName"));
317 if ($this->fon_group){
318 $smarty->assign("fon_group", "checked");
319 } else {
320 $smarty->assign("fon_group", "");
321 }
322 $smarty->assign("fon_groupACL", chkacl($this->acl, "fon_group"));
324 if ($this->nagios_group){
325 $smarty->assign("nagios_group", "checked");
326 } else {
327 $smarty->assign("nagios_group", "");
328 }
329 $smarty->assign("nagios_groupACL", chkacl($this->acl, "nagios_group"));
331 /* Fields */
332 foreach (array("cn", "description", "gidNumber") as $val){
333 $smarty->assign("$val", $this->$val);
334 $smarty->assign("$val"."ACL", chkacl($this->acl, "$val"));
335 }
337 /* Missing ACL's */
338 foreach (array("base", "smbgroup", "members") as $val){
339 $smarty->assign("$val"."ACL", chkacl($this->acl, "$val"));
340 }
342 /* Show main page */
343 $smarty->assign("alphabet", generate_alphabet(10));
344 $smarty->assign("search_image", get_template_path('images/search.png'));
345 $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
346 $smarty->assign("tree_image", get_template_path('images/tree.png'));
347 $smarty->assign("deplist", $this->config->idepartments);
348 return($smarty->fetch (get_template_path('generic.tpl', TRUE)));
349 }
351 function addUser($uid)
352 {
353 $this->memberUid[]= $uid;
354 $this->memberUid= array_unique($this->memberUid);
355 }
357 function removeUser($uid)
358 {
359 $temp= array();
360 foreach ($this->memberUid as $value){
361 if ($value != $uid){
362 $temp[]= $value;
363 }
364 }
365 $this->memberUid= $temp;
366 }
369 /* Reload data */
370 function reload()
371 {
372 /* Generate userlists */
373 $this->last_sorting= "invalid";
374 $this->users= array();
375 $ldap= $this->config->get_ldap_link();
376 $gufilter= get_global("gufilter");
378 $ldap->cd ($gufilter['dselect']);
379 $ldap->ls ("(&(objectClass=gosaAccount)(!(objectClass=gosaUserTemplate))(!(uid=*$)))",get_people_ou().$gufilter['dselect'],array("uid", "sn", "givenName"));
380 $this->allusers= array();
381 while ($attrs= $ldap->fetch()){
382 if (isset($attrs["givenName"][0]) && isset($attrs["sn"][0])){
383 $this->allusers[$attrs["uid"][0]]= $attrs["sn"][0].", ".
384 $attrs["givenName"][0].
385 " [".$attrs["uid"][0]."]";
386 } else {
387 $this->allusers[$attrs["uid"][0]]= $attrs['uid'][0];
388 }
389 }
390 natcasesort ($this->allusers);
391 reset ($this->allusers);
393 /* Fill memberlist */
394 $this->members= array();
395 foreach ($this->memberUid as $value){
396 if (isset($this->allusers[$value])){
397 $this->members[$value]= $this->allusers[$value];
398 } else {
399 $this->members[$value]= "[$value]";
400 }
401 }
402 asort($this->members);
403 reset($this->members);
404 }
408 function remove_from_parent()
409 {
410 plugin::remove_from_parent();
412 $ldap= $this->config->get_ldap_link();
413 $ldap->rmdir($this->dn);
414 show_ldap_error($ldap->get_error());
416 /* Delete references to object groups */
417 $ldap->cd ($this->config->current['BASE']);
418 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
419 while ($ldap->fetch()){
420 $og= new ogroup($this->config, $ldap->getDN());
421 unset($og->member[$this->dn]);
422 $og->save ();
423 }
425 /* Send signal to the world that we've done */
426 $this->handle_post_events("remove");
427 }
430 /* Save data to object */
431 function save_object()
432 {
433 /* Save additional values for possible next step */
434 if (isset($_POST['groupedit'])){
435 plugin::save_object();
437 $this->force_gid= 0;
438 $this->smbgroup= 0;
439 foreach (array("force_gid", "department", "base", "smbgroup") as $val) {
440 if (chkacl ($this->acl, "$val") == "" && isset($_POST["$val"])){
441 $this->$val= $_POST["$val"];
442 }
443 }
445 /* Save sambaDomain attribute */
446 if (chkacl ($this->acl, "sambaDomainName") == "" && $this->samba3 &&
447 isset ($_POST['sambaDomainName'])){
449 $this->sambaDomainName= $_POST['sambaDomainName'];
450 $this->groupType= $_POST['groupType'];
451 }
453 /* Save fon attribute */
454 if (chkacl ($this->acl, "fon_group") == ""){
455 if (isset ($_POST['fon_group'])){
456 $this->fon_group= TRUE;
457 } else {
458 $this->fon_group= FALSE;
459 }
460 }
461 if (chkacl ($this->acl, "nagios_group") == ""){
462 if (isset ($_POST['nagios_group'])){
463 $this->nagios_group= TRUE;
464 } else {
465 $this->nagios_group= FALSE;
466 }
467 }
468 }
469 }
472 /* Save to LDAP */
473 function save()
474 {
475 /* ID handling */
476 if ($this->force_gid == 0){
477 if ($this->saved_gidNumber != ""){
478 $this->gidNumber= $this->saved_gidNumber;
479 } else {
480 /* Calculate new, lock uids */
481 $wait= 10;
482 while (get_lock("uidnumber") != ""){
483 sleep (1);
485 /* timed out? */
486 if ($wait-- == 0){
487 break;
488 }
489 }
490 add_lock ("uidnumber", "gosa");
491 $this->gidNumber= $this->get_next_id("gidNumber");
492 }
493 }
495 plugin::save();
497 /* Remove objectClass for samba/phone support */
498 $tmp= array();
499 for ($i= 0; $i<count($this->attrs["objectClass"]); $i++){
500 if ($this->attrs['objectClass'][$i] != 'sambaGroupMapping' &&
501 $this->attrs['objectClass'][$i] != 'sambaIdmapEntry' &&
502 $this->attrs['objectClass'][$i] != 'goFonPickupGroup' &&
503 $this->attrs['objectClass'][$i] != 'nagiosContactGroup'){
504 $tmp[]= $this->attrs['objectClass'][$i];
505 }
506 }
507 $this->attrs['objectClass']= $tmp;
508 $ldap= $this->config->get_ldap_link();
510 /* Add samba group functionality */
511 if ($this->samba3 && $this->smbgroup){
512 $this->SID= $this->config->data['SERVERS']['SAMBA'][$this->sambaDomainName]['SID'];
513 $this->ridBase= $this->config->data['SERVERS']['SAMBA'][$this->sambaDomainName]['RIDBASE'];
515 $this->attrs['objectClass'][]= 'sambaGroupMapping';
516 $this->attrs['sambaGroupType']= "2";
518 /* Check if we need to create a special entry */
519 if ($this->groupType == 0){
521 if ($this->sambaSID == "" || $this->oldgroupType != $this->groupType){
522 $gidNumber= $this->gidNumber;
523 while(TRUE){
524 $sid= $this->SID."-".($gidNumber*2 + $this->ridBase+1);
525 $ldap->cd($this->config->current['BASE']);
526 $ldap->search("(sambaSID=$sid)",array("sambaSID"));
527 if ($ldap->count() == 0){
528 break;
529 }
530 $gidNumber++;
531 }
532 $this->attrs['sambaSID']= $sid;
533 $this->sambaSID= $sid;
534 }
536 } else {
537 $this->attrs['sambaSID']=$this->SID."-".$this->groupType;
538 }
540 /* User wants me to fake the idMappings? This is useful for
541 making winbind resolve the group names in a reasonable amount
542 of time in combination with larger databases. */
543 if (isset($this->config->current['SAMBAIDMAPPING']) &&
544 preg_match('/true/i', $this->config->current['SAMBAIDMAPPING'])){
545 $this->attrs['objectClass'][]= "sambaIdmapEntry";
546 }
548 }
550 /* Add phone functionality */
551 if ($this->fon_group){
552 $this->attrs['objectClass'][]= "goFonPickupGroup";
553 }
555 /* Add nagios functionality */
556 if ($this->nagios_group){
557 $this->attrs['objectClass'][]= "nagiosContactGroup";
558 }
560 /* Take members array */
561 if (count ($this->memberUid)){
562 $this->attrs['memberUid']= array_unique($this->memberUid);
563 }
565 /* New accounts need proper 'dn', propagate it to remaining objects */
566 if ($this->dn == 'new'){
567 $this->dn= 'cn='.$this->cn.','.get_groups_ou().$this->base;
568 }
570 /* Save data. Using 'modify' implies that the entry is already present, use 'add' for
571 new entries. So do a check first... */
572 $ldap->cat ($this->dn);
573 if ($ldap->fetch()){
574 /* Modify needs array() to remove values :-( */
575 if (!count ($this->memberUid)){
576 $this->attrs['memberUid']= array();
577 }
578 if ($this->samba3){
579 if (!$this->smbgroup){
580 $this->attrs['sambaGroupType']= array();
581 $this->attrs['sambaSID']= array();
582 }
583 }
584 $mode= "modify";
585 } else {
586 $mode= "add";
587 $ldap->cd($this->config->current['BASE']);
588 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
589 }
591 /* Write back to ldap */
592 $ldap->cd($this->dn);
593 $this->cleanup();
594 $ldap->$mode($this->attrs);
596 $ret= 0;
597 if (show_ldap_error($ldap->get_error())){
598 $ret= 1;
599 }
601 /* Remove uid lock */
602 del_lock ("uidnumber");
604 /* Post that we've done*/
605 $this->handle_post_events($mode);
607 return ($ret);
608 }
610 function check()
611 {
612 $message= array();
614 /* Permissions for that base? */
615 if ($this->base != ""){
616 $new_dn= 'cn='.$this->cn.','.get_groups_ou().$this->base;
617 } else {
618 $new_dn= $this->dn;
619 }
621 $ui= get_userinfo();
622 $acl= get_permissions ($ui->dn, $ui->subtreeACL);
623 $acl= get_module_permission($acl, "group", $ui->dn);
624 if (chkacl($this->acl, "create") != ""){
625 $message[]= _("You have no permissions to create a group on this 'Base'.");
626 }
628 /* must: cn */
629 if ($this->cn == "" && chkacl ($this->acl, "cn") == ""){
630 $message[]= "The required field 'Name' is not set.";
631 }
633 /* Check for valid input */
634 if (!is_uid($this->cn)){
635 $message[]= _("The field 'Name' contains invalid characters. Lowercase, numbers and dashes are allowed.");
636 }
638 /* Check for used 'cn' */
639 $ldap= $this->config->get_ldap_link();
640 $ldap->cd($this->config->current['BASE']);
641 $ldap->search("(&(|(objectClass=gosaGroupOfNames)(objectClass=posixGroup))(cn=$this->cn))",array("cn"));
642 if ($ldap->count() != 0){
644 /* New entry? */
645 if ($this->dn == 'new'){
646 $message[]= _("Value specified as 'Name' is already used.");
647 }
649 /* Moved? */
650 elseif ($new_dn != $this->orig_dn){
651 $ldap->fetch();
652 if ($ldap->getDN() != $this->orig_dn){
653 $message[]= _("Value specified as 'Name' is already used.");
654 }
655 }
656 }
658 /* Check ID */
659 if ($this->force_gid == "1"){
660 if (!is_id($this->gidNumber)){
661 $message[]= _("Value specified as 'GID' is not valid.");
662 } else {
663 if ($this->gidNumber < $this->config->current['MINID']){
664 $message[]= _("Value specified as 'GID' is too small.");
665 }
667 }
668 }
670 return ($message);
671 }
673 function get_next_id($attrib)
674 {
675 $ids= array();
676 $ldap= $this->config->get_ldap_link();
678 $ldap->cd ($this->config->current['BASE']);
679 if (preg_match('/gidNumber/i', $attrib)){
680 $oc= "posixGroup";
681 } else {
682 $oc= "posixAccount";
683 }
684 $ldap->search ("(&(objectClass=$oc)($attrib=*))", array("$attrib"));
686 /* Get list of ids */
687 while ($attrs= $ldap->fetch()){
688 $ids[]= (int)$attrs["$attrib"][0];
689 }
691 /* Find out next free id near to UID_BASE */
692 for ($id= $this->config->current['UIDBASE']; $id++; $id<65000){
693 if (!in_array($id, $ids)){
694 return ($id);
695 }
696 }
698 /* Should not happen */
699 if ($id == 65000){
700 print_red(_("Too many users, can't allocate a free ID!"));
701 exit;
702 }
703 }
705 }
707 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
708 ?>