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