1 <?php
2 /*
3 * This code is part of GOsa (http://www.gosa-project.org)
4 * Copyright (C) 2003-2008 GONICUS GmbH
5 *
6 * ID: $$Id$$
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
23 /*!
24 \brief posixAccount plugin
25 \author Cajus Pollmeier <pollmeier@gonicus.de>
26 \version 2.00
27 \date 24.07.2003
29 This class provides the functionality to read and write all attributes
30 relevant for posixAccounts and shadowAccounts from/to the LDAP. It
31 does syntax checking and displays the formulars required.
32 */
34 class posixAccount extends plugin
35 {
36 /* Definitions */
37 var $plHeadline= "UNIX";
38 var $plDescription= "Edit users POSIX extensions";
40 /* Plugin specific values */
41 var $homeDirectory= "";
42 var $loginShell= "/bin/bash";
43 var $uidNumber= "";
44 var $gidNumber= "";
45 var $gecos= "";
46 var $shadowMin= "0";
47 var $shadowMax= "0";
48 var $shadowWarning= "0";
49 var $shadowLastChange= "0";
50 var $shadowInactive= "0";
51 var $shadowExpire= "0";
52 var $gosaDefaultPrinter= "";
53 var $accessTo= array();
54 var $trustModel= "";
56 var $glist=array();
57 var $status= "";
58 var $loginShellList= array();
59 var $groupMembership= array();
60 var $savedGroupMembership= array();
61 var $savedUidNumber= "";
62 var $savedGidNumber= "";
63 var $activate_shadowMin= "0";
64 var $activate_shadowMax= "0";
65 var $activate_shadowWarning= "0";
66 var $activate_shadowInactive= "0";
67 var $activate_shadowExpire= "0";
68 var $mustchangepassword= "0";
69 var $force_ids= 0;
70 var $group_dialog= FALSE;
71 var $show_ws_dialog= FALSE;
72 var $secondaryGroups= array();
73 var $primaryGroup= 0;
74 var $was_trust_account= FALSE;
75 var $memberGroup = array();
76 var $grouplist = array();
77 var $ui = array();
79 var $GroupRegex = "*";
80 var $GroupUserRegex = "*";
81 var $SubSearch = false;
83 var $view_logged = FALSE;
85 /* attribute list for save action */
86 var $CopyPasteVars =
87 array("grouplist","groupMembership","activate_shadowMin",
88 "activate_shadowMax","activate_shadowWarning","activate_shadowInactive","activate_shadowExpire",
89 "must_change_password","printerList","grouplist","savedGidNumber","savedUidNumber");
91 var $attributes = array("homeDirectory", "loginShell", "uidNumber", "gidNumber", "gecos",
92 "shadowMin", "shadowMax", "shadowWarning", "shadowInactive", "shadowLastChange",
93 "shadowExpire", "gosaDefaultPrinter", "uid","accessTo","trustModel");
95 var $objectclasses= array("posixAccount", "shadowAccount");
97 var $uid= "";
98 var $multiple_support = TRUE;
99 var $groupMembership_some = array();
101 /* constructor, if 'dn' is set, the node loads the given
102 'dn' from LDAP */
103 function posixAccount (&$config, $dn= NULL)
104 {
105 /* Configuration is fine, allways */
106 $this->config= $config;
108 /* Load bases attributes */
109 plugin::plugin($config, $dn);
111 /* Setting uid to default */
112 if(isset($this->attrs['uid'][0])){
113 $this->uid = $this->attrs['uid'][0];
114 }
116 $ldap= $this->config->get_ldap_link();
118 if ($dn !== NULL){
120 /* Correct is_account. shadowAccount is not required. */
121 if (isset($this->attrs['objectClass']) &&
122 in_array ('posixAccount', $this->attrs['objectClass'])){
124 $this->is_account= TRUE;
125 }
127 /* Is this account a trustAccount? */
128 if ($this->is_account && isset($this->attrs['trustModel'])){
129 $this->trustModel= $this->attrs['trustModel'][0];
130 $this->was_trust_account= TRUE;
131 } else {
132 $this->was_trust_account= FALSE;
133 $this->trustModel= "";
134 }
136 $this->accessTo = array();
137 if ($this->is_account && isset($this->attrs['accessTo'])){
138 for ($i= 0; $i<$this->attrs['accessTo']['count']; $i++){
139 $tmp= $this->attrs['accessTo'][$i];
140 $this->accessTo[$tmp]= $tmp;
141 }
142 }
143 $this->initially_was_account= $this->is_account;
145 /* Fill group */
146 $this->primaryGroup= $this->gidNumber;
148 /* Generate status text */
149 $current= date("U");
151 $current= floor($current / 60 /60 / 24);
153 if (($current >= $this->shadowExpire) && $this->shadowExpire){
154 $this->status= _("expired");
155 if (($current - $this->shadowExpire) < $this->shadowInactive){
156 $this->status.= ", "._("grace time active");
157 }
158 } elseif (($this->shadowLastChange + $this->shadowMin) >= $current){
159 $this->status= _("active, password not changable");
160 } elseif (($this->shadowLastChange + $this->shadowMax) >= $current){
161 $this->status= _("active, password expired");
162 } else {
163 $this->status= _("active");
164 }
166 /* Get group membership */
167 $ldap->cd($this->config->current['BASE']);
168 $ldap->search("(&(objectClass=posixGroup)(memberUid=".$this->uid."))", array("cn", "description"));
170 while ($attrs= $ldap->fetch()){
171 if (!isset($attrs["description"][0])){
172 $entry= $attrs["cn"][0];
173 } else {
174 $entry= $attrs["cn"][0]." [".$attrs["description"][0]."]";
175 }
176 $this->groupMembership[$ldap->getDN()]= $entry;
177 }
178 asort($this->groupMembership);
179 reset($this->groupMembership);
180 $this->savedGroupMembership= $this->groupMembership;
181 $this->savedUidNumber= $this->uidNumber;
182 $this->savedGidNumber= $this->gidNumber;
183 }
185 /* Adjust shadow checkboxes */
186 foreach (array("shadowMin", "shadowMax", "shadowWarning", "shadowInactive",
187 "shadowExpire") as $val){
189 if ($this->$val != 0){
190 $oval= "activate_".$val;
191 $this->$oval= "1";
192 }
193 }
195 /* Convert to seconds */
196 $this->shadowExpire= $this->convertToSeconds($this->shadowExpire);
198 /* Generate shell list from CONFIG_DIR./shells */
199 if (file_exists(CONFIG_DIR.'/shells')){
200 $shells = file (CONFIG_DIR.'/shells');
201 foreach ($shells as $line){
202 if (!preg_match ("/^#/", $line)){
203 $this->loginShellList[]= trim($line);
204 }
205 }
206 } else {
207 if ($this->loginShell == ""){
208 $this->loginShellList[]= _("unconfigured");
209 }
210 }
212 /* Insert possibly missing loginShell */
213 if ($this->loginShell != "" && !in_array($this->loginShell, $this->loginShellList)){
214 $this->loginShellList[]= $this->loginShell;
215 }
217 /* Generate group list */
218 $this->ui = get_userinfo();
219 $this->secondaryGroups[]= "- "._("automatic")." -";
220 $ldap->cd($this->config->current['BASE']);
221 $ldap->search("(objectClass=posixGroup)", array("cn", "gidNumber"));
222 while($attrs = $ldap->fetch()){
223 $this->secondaryGroups[$attrs['gidNumber'][0]]= $attrs['cn'][0];
224 }
225 asort ($this->secondaryGroups);
227 /* Get global filter config */
228 if (!session::is_set("sysfilter")){
229 $ui= get_userinfo();
230 $base= get_base_from_people($ui->dn);
231 $sysfilter= array( "depselect" => $base,
232 "regex" => "*");
233 session::set("sysfilter", $sysfilter);
234 }
235 $this->ui = get_userinfo();
236 }
239 /* execute generates the html output for this node */
240 function execute($isCopyPaste = false)
241 {
242 /* Call parent execute */
243 plugin::execute();
244 $display= "";
246 /* Log view */
247 if($this->is_account && !$this->view_logged){
248 $this->view_logged = TRUE;
249 new log("view","users/".get_class($this),$this->dn);
250 }
252 /* Department has changed? */
253 if(isset($_POST['depselect'])){
254 session::set('CurrentMainBase',validate($_POST['depselect']));
255 }
257 if($this->multiple_support_active){
258 $this->is_account = TRUE;
259 }
261 if(!$isCopyPaste && ! $this->multiple_support_active){
263 /* Do we need to flip is_account state? */
264 if(isset($_POST['modify_state'])){
265 if($this->is_account && $this->acl_is_removeable()){
266 $this->is_account= FALSE;
267 }elseif(!$this->is_account && $this->acl_is_createable()){
268 $this->is_account= TRUE;
269 }
270 }
272 /* Do we represent a valid posixAccount? */
273 if (!$this->is_account && $this->parent === NULL ){
274 $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\"> <b>".
275 _("This account has no unix extensions.")."</b>";
276 $display.= back_to_main();
277 return ($display);
278 }
281 /* Show tab dialog headers */
282 if ($this->parent !== NULL){
283 if ($this->is_account){
284 if (isset($this->parent->by_object['sambaAccount'])){
285 $obj= $this->parent->by_object['sambaAccount'];
286 }
287 if (isset($obj) && $obj->is_account == TRUE &&
288 ((isset($this->parent->by_object['sambaAccount']))&&($this->parent->by_object['sambaAccount']->is_account))
289 ||(isset($this->parent->by_object['environment'] ))&&($this->parent->by_object['environment'] ->is_account)){
291 /* Samba3 dependency on posix accounts are enabled
292 in the moment, because I need to rely on unique
293 uidNumbers. There'll be a better solution later
294 on. */
295 $display= $this->show_disable_header(_("Remove posix account"),
296 _("This account has unix features enabled. To disable them, you'll need to remove the samba / environment account first."), TRUE);
297 } else {
298 $display= $this->show_disable_header(_("Remove posix account"),
299 _("This account has posix features enabled. You can disable them by clicking below."));
300 }
301 } else {
302 $display= $this->show_enable_header(_("Create posix account"),
303 _("This account has posix features disabled. You can enable them by clicking below."));
304 return($display);
305 }
306 }
307 }
308 /* Trigger group edit? */
309 if (isset($_POST['edit_groupmembership'])){
310 $this->group_dialog= TRUE;
311 $this->dialog= TRUE;
312 }
314 /* Cancel group edit? */
315 if (isset($_POST['add_groups_cancel']) ||
316 isset($_POST['add_groups_finish'])){
317 $this->group_dialog= FALSE;
318 $this->dialog= FALSE;
319 }
321 /* Add selected groups */
322 if (isset($_POST['add_groups_finish']) && isset($_POST['groups']) &&
323 count($_POST['groups'])){
325 $this->addGroup ($_POST['groups']);
326 }
328 /* Delete selected groups */
329 if (isset($_POST['delete_groupmembership']) &&
330 isset($_POST['group_list']) && count($_POST['group_list'])){
332 $this->delGroup ($_POST['group_list']);
333 }
335 /* Add user workstation? */
336 if (isset($_POST["add_ws"])){
337 $this->show_ws_dialog= TRUE;
338 $this->dialog= TRUE;
339 }
341 /* Add user workstation? */
342 if (isset($_POST["add_ws_finish"]) && isset($_POST['wslist'])){
343 foreach($_POST['wslist'] as $ws){
344 $this->accessTo[$ws]= $ws;
345 }
346 ksort($this->accessTo);
347 $this->is_modified= TRUE;
348 }
350 /* Remove user workstations? */
351 if (isset($_POST["delete_ws"]) && isset($_POST['workstation_list'])){
352 foreach($_POST['workstation_list'] as $name){
353 unset ($this->accessTo[$name]);
354 }
355 $this->is_modified= TRUE;
356 }
358 /* Add user workstation finished? */
359 if (isset($_POST["add_ws_finish"]) || isset($_POST["add_ws_cancel"])){
360 $this->show_ws_dialog= FALSE;
361 $this->dialog= FALSE;
362 }
364 /* Templates now! */
365 $smarty= get_smarty();
367 /* Show ws dialog */
368 if ($this->show_ws_dialog){
369 /* Save data */
370 $sysfilter= session::get("sysfilter");
371 foreach( array("depselect", "regex") as $type){
372 if (isset($_POST[$type])){
373 $sysfilter[$type]= $_POST[$type];
374 }
375 }
376 if (isset($_GET['search'])){
377 $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
378 if ($s == "**"){
379 $s= "*";
380 }
381 $sysfilter['regex']= $s;
382 }
383 session::set("sysfilter", $sysfilter);
385 /* Get workstation list */
386 $exclude= "";
387 foreach($this->accessTo as $ws){
388 $exclude.= "(cn=$ws)";
389 }
390 if ($exclude != ""){
391 $exclude= "(!(|$exclude))";
392 }
393 $regex= $sysfilter['regex'];
394 $filter= "(&(|(objectClass=goServer)(objectClass=gotoWorkstation)(objectClass=gotoTerminal))$exclude(cn=*)(cn=$regex))";
396 $deps_a = array(get_ou("serverou"),
397 get_ou("terminalou"),
398 get_ou("workstationou"));
400 $res= get_sub_list($filter, array("terminal","server","workstation"), $deps_a, get_ou("systemsou").$sysfilter['depselect'], array("cn"), GL_SUBSEARCH | GL_SIZELIMIT);
401 $wslist= array();
402 foreach ($res as $attrs){
403 $wslist[]= preg_replace('/\$/', '', $attrs['cn'][0]);
404 }
405 asort($wslist);
406 $smarty->assign("search_image", get_template_path('images/search.png'));
407 $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
408 $smarty->assign("tree_image", get_template_path('images/tree.png'));
409 $smarty->assign("deplist", $this->config->idepartments);
410 $smarty->assign("alphabet", generate_alphabet());
411 foreach( array("depselect", "regex") as $type){
412 $smarty->assign("$type", $sysfilter[$type]);
413 }
414 $smarty->assign("hint", print_sizelimit_warning());
415 $smarty->assign("wslist", $wslist);
416 $smarty->assign("apply", apply_filter());
417 $display= $smarty->fetch (get_template_path('trust_machines.tpl', TRUE, dirname(__FILE__)));
418 return ($display);
419 }
421 /* Manage group add dialog */
422 if ($this->group_dialog){
424 /* Get global filter config */
425 $this->reload();
427 /* remove already assigned groups */
428 $glist= array();
429 foreach ($this->grouplist as $key => $value){
430 if (!isset($this->groupMembership[$key]) && obj_is_writable($key,"groups/group","memberUid")){
431 $glist[$key]= $value;
432 }
433 }
435 if($this->SubSearch){
436 $smarty->assign("SubSearchCHK"," checked ");
437 }else{
438 $smarty->assign("SubSearchCHK","");
439 }
441 $smarty->assign("regex",$this->GroupRegex);
442 $smarty->assign("guser",$this->GroupUserRegex);
443 $smarty->assign("groups", $glist);
444 $smarty->assign("search_image", get_template_path('images/search.png'));
445 $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
446 $smarty->assign("tree_image", get_template_path('images/tree.png'));
447 $smarty->assign("deplist", $this->config->idepartments);
448 $smarty->assign("alphabet", generate_alphabet());
449 $smarty->assign("depselect", session::get('CurrentMainBase'));
450 $smarty->assign("hint", print_sizelimit_warning());
452 $smarty->assign("apply", apply_filter());
453 $display.= $smarty->fetch (get_template_path('posix_groups.tpl', TRUE, dirname(__FILE__)));
454 return ($display);
455 }
457 /* Show main page */
458 $smarty= get_smarty();
460 /* In 'MyAccount' mode, we must remove write acls if we are not in editing mode. */
461 $SkipWrite = (!isset($this->parent) || !$this->parent) && !session::is_set('edit');
463 /* Depending on pwmode, currently hardcoded because there are no other methods */
464 if ( 1 == 1 ){
465 $smarty->assign("pwmode", dirname(__FILE__)."/posix_shadow");
467 $shadowMinACL = $this->getacl("shadowMin",$SkipWrite);
468 $smarty->assign("shadowmins", sprintf(_("Password can't be changed up to %s days after last change"),
469 "<input name=\"shadowMin\" size=3 maxlength=4 value=\"".$this->shadowMin."\">"));
471 $shadowMaxACL = $this->getacl("shadowMax",$SkipWrite);
472 $smarty->assign("shadowmaxs", sprintf(_("Password must be changed after %s days"),
473 "<input name=\"shadowMax\" size=3 maxlength=4 value=\"".$this->shadowMax."\">"));
475 $shadowInactiveACL= $this->getacl("shadowInactive",$SkipWrite);
476 $smarty->assign("shadowinactives", sprintf(_("Disable account after %s days of inactivity after password expiery"),
477 "<input name=\"shadowInactive\" size=3 maxlength=4 value=\"".$this->shadowInactive."\">"));
479 $shadowWarningACL = $this->getacl("shadowWarning",$SkipWrite);
480 $smarty->assign("shadowwarnings", sprintf(_("Warn user %s days before password expiery"),
481 "<input name=\"shadowWarning\" size=3 maxlength=4 value=\"".$this->shadowWarning."\">"));
483 foreach( array("activate_shadowMin", "activate_shadowMax",
484 "activate_shadowExpire", "activate_shadowInactive","activate_shadowWarning") as $val){
485 if ($this->$val == 1){
486 $smarty->assign("$val", "checked");
487 } else {
488 $smarty->assign("$val", "");
489 }
490 $smarty->assign("$val"."ACL", $this->getacl($val,$SkipWrite));
491 }
493 $smarty->assign("mustchangepasswordACL", $this->getacl("mustchangepassword",$SkipWrite));
494 }
496 /* Fill calendar */
497 /* If this $this->shadowExpire is empty
498 use current date as base for calculating selectbox values.
499 (This attribute is empty if this is a new user )*/
500 if(empty($this->shadowExpire)){
501 $date= getdate(time());
502 }else{
503 $date= getdate($this->shadowExpire);
504 }
506 $days= array();
507 for($d= 1; $d<32; $d++){
508 $days[$d]= $d;
509 }
510 $years= array();
511 for($y= $date['year']-10; $y<$date['year']+10; $y++){
512 $years[]= $y;
513 }
514 $months= array(_("January"), _("February"), _("March"), _("April"),
515 _("May"), _("June"), _("July"), _("August"), _("September"),
516 _("October"), _("November"), _("December"));
517 $smarty->assign("day", $date["mday"]);
518 $smarty->assign("days", $days);
519 $smarty->assign("months", $months);
520 $smarty->assign("month", $date["mon"]-1);
521 $smarty->assign("years", $years);
522 $smarty->assign("year", $date["year"]);
524 /* Fill arrays */
525 $smarty->assign("shells", $this->loginShellList);
526 $smarty->assign("secondaryGroups", $this->secondaryGroups);
527 $smarty->assign("primaryGroup", $this->primaryGroup);
528 if(!$this->multiple_support_active){
529 if (!count($this->groupMembership)){
530 $smarty->assign("groupMembership", array(" "));
531 } else {
532 $smarty->assign("groupMembership", $this->groupMembership);
533 }
534 }else{
535 $smarty->assign("groupMembership", $this->groupMembership);
536 $smarty->assign("groupMembership_some", $this->groupMembership_some);
537 }
538 if (count($this->groupMembership) > 16){
539 $smarty->assign("groups", "too_many_for_nfs");
540 } else {
541 $smarty->assign("groups", "");
542 }
544 /* Avoid "Undefined index: forceMode" */
545 $smarty->assign("forceMode", "");
547 /* Checkboxes */
548 if ($this->force_ids == 1){
549 $smarty->assign("force_ids", "checked");
550 if (session::get('js')){
551 $smarty->assign("forceMode", "");
552 }
553 } else {
554 if (session::get('js')){
555 $smarty->assign("forceMode", "disabled");
556 }
557 $smarty->assign("force_ids", "");
558 }
561 $smarty->assign("force_idsACL", $this->getacl("uidNumber",$SkipWrite).$this->getacl("gidNumber",$SkipWrite));
563 foreach(array("primaryGroup","trustmode","activate_shadowWarning","activate_shadowInactive","activate_shadowMin","activate_shadowMax","activate_shadowExpire","mustchangepassword") as $val){
564 if(in_array($val,$this->multi_boxes)){
565 $smarty->assign("use_".$val,TRUE);
566 }else{
567 $smarty->assign("use_".$val,FALSE);
568 }
569 }
572 /* Load attributes and acl's */
573 foreach($this->attributes as $val){
574 if(in_array($val,$this->multi_boxes)){
575 $smarty->assign("use_".$val,TRUE);
576 }else{
577 $smarty->assign("use_".$val,FALSE);
578 }
580 if((session::get("js"))&&(($val=="uidNumber")||($val=="gidNumber")))
581 {
582 $smarty->assign("$val"."ACL",$this->getacl($val,$SkipWrite));
583 $smarty->assign("$val", $this->$val);
584 continue;
585 }
586 $smarty->assign("$val", $this->$val);
587 $smarty->assign("$val"."ACL", $this->getacl($val,$SkipWrite));
588 }
589 if($SkipWrite){
590 $smarty->assign("groupMembershipACL","r");
591 }else{
592 $smarty->assign("groupMembershipACL","rw");
593 }
594 $smarty->assign("status", $this->status);
596 /* Work on trust modes */
597 $smarty->assign("trusthide", " disabled ");
598 $smarty->assign("trustmodeACL", $this->getacl("trustModel",$SkipWrite));
599 if ($this->trustModel == "fullaccess"){
600 $trustmode= 1;
601 // pervent double disable tag in html code, this will disturb our clean w3c html
602 $smarty->assign("trustmode", $this->getacl("trustModel",$SkipWrite));
604 } elseif ($this->trustModel == "byhost"){
605 $trustmode= 2;
606 $smarty->assign("trusthide", "");
607 } else {
608 // pervent double disable tag in html code, this will disturb our clean w3c html
609 $smarty->assign("trustmode", $this->getacl("trustModel",$SkipWrite));
610 $trustmode= 0;
611 }
612 $smarty->assign("trustmode", $trustmode);
613 $smarty->assign("trustmodes", array( 0 => _("disabled"), 1 => _("full access"),
614 2 => _("allow access to these hosts")));
618 if((count($this->accessTo))==0)
619 $smarty->assign("emptyArrAccess",true);
620 else
621 $smarty->assign("emptyArrAccess",false);
623 if($this->mustchangepassword){
624 $smarty->assign("mustchangepassword", " checked ");
625 } else {
626 $smarty->assign("mustchangepassword", "");
627 }
629 $smarty->assign("workstations", $this->accessTo);
631 $smarty->assign("apply", apply_filter());
632 $smarty->assign("multiple_support" , $this->multiple_support_active);
633 $display.= $smarty->fetch (get_template_path('generic.tpl', TRUE, dirname(__FILE__)));
634 return($display);
635 }
638 /* remove object from parent */
639 function remove_from_parent()
640 {
641 /* Cancel if there's nothing to do here */
642 if ((!$this->initially_was_account) || (!$this->acl_is_removeable())){
643 return;
644 }
646 /* include global link_info */
647 $ldap= $this->config->get_ldap_link();
649 /* Remove and write to LDAP */
650 plugin::remove_from_parent();
652 /* Zero out array */
653 $this->attrs['gosaHostACL']= array();
655 /* Keep uid, because we need it for authentification! */
656 unset($this->attrs['uid']);
657 unset($this->attrs['trustModel']);
659 @DEBUG (DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__,
660 $this->attributes, "Save");
661 $ldap->cd($this->dn);
662 $this->cleanup();
663 $ldap->modify ($this->attrs);
665 new log("remove","users/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
667 show_ldap_error($ldap->get_error(), sprintf(_("Removing of user/posix account with dn '%s' failed."),$this->dn));
669 /* Delete group only if cn is uid and there are no other
670 members inside */
671 $ldap->cd ($this->config->current['BASE']);
672 $ldap->search ("(&(objectClass=posixGroup)(gidNumber=".$this->gidNumber."))", array("cn", "memberUid"));
673 if ($ldap->count() != 0){
674 $attrs= $ldap->fetch();
675 if ($attrs['cn'][0] == $this->uid &&
676 !isset($this->attrs['memberUid'])){
678 $ldap->rmDir($ldap->getDN());
679 }
680 }
682 /* Optionally execute a command after we're done */
683 $this->handle_post_events("remove",array("uid" => $this->uid));
684 }
687 function save_object()
688 {
689 if (isset($_POST['posixTab'])){
690 /* Save values to object */
691 plugin::save_object();
694 /* Save force GID checkbox */
695 if($this->acl_is_writeable("gidNumber") || $this->acl_is_writeable("uidNumber")){
696 if (isset ($_POST['force_ids'])){
697 $data= 1;
698 } else {
699 $data= 0;
700 }
701 if ($this->force_ids != $data){
702 $this->is_modified= TRUE;
703 }
704 $this->force_ids= $data;
705 }
707 /*Save primary group settings */
708 if($this->acl_is_writeable("primaryGroup") && isset($_POST['primaryGroup'])){
709 $data= $_POST['primaryGroup'];
710 if ($this->primaryGroup != $data){
711 $this->is_modified= TRUE;
712 }
713 $this->primaryGroup= $_POST['primaryGroup'];
714 }
716 /* Get seelcted shadow checkboxes */
717 foreach(array("shadowMin","shadowMax","shadowExpire","shadowInactive","shadowWarning") as $var) {
718 if($this->acl_is_writeable($var)){
719 $activate_var = "activate_".$var;
720 if(isset($_POST['activate_'.$var])){
721 $this->$activate_var = true;
722 $this->$var = $_POST[$var];
723 }else{
724 $this->$activate_var = false;
725 $this->$var = 0;
726 }
727 }
728 }
730 /* Force change password ? */
731 if(isset($_POST['mustchangepassword'])){
732 $this->mustchangepassword = TRUE;
733 }else{
734 $this->mustchangepassword = FALSE;
735 }
737 /* Trust mode - special handling */
738 if($this->acl_is_writeable("trustModel")){
739 if (isset($_POST['trustmode'])){
740 $saved= $this->trustModel;
741 if ($_POST['trustmode'] == "1"){
742 $this->trustModel= "fullaccess";
743 } elseif ($_POST['trustmode'] == "2"){
744 $this->trustModel= "byhost";
745 } else {
746 $this->trustModel= "";
747 }
748 if ($this->trustModel != $saved){
749 $this->is_modified= TRUE;
750 }
751 }
752 }
753 }
755 /* Get regex from alphabet */
756 if(isset($_GET['search'])){
757 $this->GroupRegex = $_GET['search']."*";
758 }
760 /* Check checkboxes and regexes */
761 if(isset($_POST["PosixGroupDialogPosted"])){
763 if(isset($_POST['SubSearch']) && ($_POST['SubSearch'])){
764 $this->SubSearch = true;
765 }else{
766 $this->SubSearch = false;
767 }
768 if(isset($_POST['guser'])){
769 $this->GroupUserRegex = $_POST['guser'];
770 }
771 if(isset($_POST['regex'])){
772 $this->GroupRegex = $_POST['regex'];
773 }
774 }
775 $this->GroupRegex = preg_replace("/\*\**/","*",$this->GroupRegex);
776 $this->GroupUserRegex = preg_replace("/\*\**/","*",$this->GroupUserRegex);
777 }
780 /* Save data to LDAP, depending on is_account we save or delete */
781 function save()
782 {
784 /* include global link_info */
785 $ldap= $this->config->get_ldap_link();
787 /* Adapt shadow values */
788 if (!$this->activate_shadowExpire){
789 $this->shadowExpire= "0";
790 } else {
791 /* Transform seconds to days here */
792 $this->shadowExpire= (int)($this->shadowExpire / (60 * 60 * 24)) ;
793 }
794 if (!$this->activate_shadowMax){
795 $this->shadowMax= "0";
796 }
797 if ($this->mustchangepassword){
798 $this->shadowLastChange= (int)(date("U") / 86400) - $this->shadowMax - 1;
799 } else {
800 $this->shadowLastChange= (int)(date("U") / 86400);
801 }
802 if (!$this->activate_shadowWarning){
803 $this->shadowWarning= "0";
804 }
806 /* Check what to do with ID's */
807 if ($this->force_ids == 0){
809 /* Use id's that are already set */
810 if ($this->savedUidNumber != ""){
811 $this->uidNumber= $this->savedUidNumber;
812 $this->gidNumber= $this->savedGidNumber;
813 } else {
815 /* Calculate new id's. We need to place a lock before calling get_next_id
816 to get real unique values. */
817 $wait= 10;
818 while (get_lock("uidnumber") != ""){
819 sleep (1);
821 /* Oups - timed out */
822 if ($wait-- == 0){
823 msg_dialog::display(_("Warning"), _("Timeout while waiting for lock! Ignoring lock."), WARNING_DIALOG);
824 break;
825 }
826 }
828 add_lock ("uidnumber", "gosa");
829 $this->uidNumber= $this->get_next_id("uidNumber", $this->dn);
830 if ($this->savedGidNumber != ""){
831 $this->gidNumber= $this->savedGidNumber;
832 } else {
833 $this->gidNumber= $this->get_next_id("gidNumber", $this->dn);
834 }
835 }
837 if ($this->primaryGroup != 0){
838 $this->gidNumber= $this->primaryGroup;
839 }
840 }
842 if ($this->activate_shadowMin != "1" ) {
843 $this->shadowMin = "";
844 }
846 if (($this->activate_shadowMax != "1") && ($this->mustchangepassword != "1")) {
847 $this->shadowMax = "";
848 }
850 if ($this->activate_shadowWarning != "1" ) {
851 $this->shadowWarning = "";
852 }
854 if ($this->activate_shadowInactive != "1" ) {
855 $this->shadowInactive = "";
856 }
858 if ($this->activate_shadowExpire != "1" ) {
859 $this->shadowExpire = "";
860 }
862 /* Fill gecos */
863 if (isset($this->parent) && $this->parent !== NULL){
864 $this->gecos= rewrite($this->parent->by_object['user']->cn);
865 if (!preg_match('/^[a-z0-9 -]+$/i', $this->gecos)){
866 $this->gecos= "";
867 }
868 }
870 foreach(array("shadowMin","shadowMax","shadowWarning","shadowInactive","shadowExpire") as $attr){
871 $this->$attr = (int) $this->$attr;
872 }
873 /* Call parents save to prepare $this->attrs */
874 plugin::save();
876 /* Trust accounts */
877 $objectclasses= array();
878 foreach ($this->attrs['objectClass'] as $key => $class){
879 if (preg_match('/trustAccount/i', $class)){
880 continue;
881 }
882 $objectclasses[]= $this->attrs['objectClass'][$key];
883 }
884 $this->attrs['objectClass']= $objectclasses;
885 if ($this->trustModel != ""){
886 $this->attrs['objectClass'][]= "trustAccount";
887 $this->attrs['trustModel']= $this->trustModel;
888 $this->attrs['accessTo']= array();
889 if ($this->trustModel == "byhost"){
890 foreach ($this->accessTo as $host){
891 $this->attrs['accessTo'][]= $host;
892 }
893 }
894 } else {
895 if ($this->was_trust_account){
896 $this->attrs['accessTo']= array();
897 $this->attrs['trustModel']= array();
898 }
899 }
901 if(empty($this->attrs['gosaDefaultPrinter'])){
902 $thid->attrs['gosaDefaultPrinter']=array();
903 }
906 /* Save data to LDAP */
907 $ldap->cd($this->dn);
908 $this->cleanup();
910 /* This is just a test, we have had duplicated ids
911 in the past when copy & paste was used.
912 Normaly this should not happen.
913 */
914 if(isset($this->attrs['uidNumber']) && !$this->force_ids){
915 $used = $this->get_used_uid_numbers();
916 if(isset($used[$this->attrs['uidNumber']]) && $used[$this->attrs['uidNumber']] != $this->dn){
917 msg_dialog::display(_("Uid number"),_("A duplicated uid number was written for this user, if this was not intended please verify all used uidNumbers."), WARNING_DIALOG);
918 }
919 }
921 unset($this->attrs['uid']);
922 $ldap->modify ($this->attrs);
924 /* Log last action */
925 if($this->initially_was_account){
926 new log("modify","users/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
927 }else{
928 new log("create","users/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
929 }
931 show_ldap_error($ldap->get_error(), sprintf(_("Saving of user/posix account with dn '%s' failed."),$this->dn));
933 /* Remove lock needed for unique id generation */
934 del_lock ("uidnumber");
936 /* Posix accounts have group interrelationship,
937 take care about these here if this is a new user without forced gidNumber. */
938 if ($this->force_ids == 0 && $this->primaryGroup == 0 && !$this->initially_was_account){
939 $ldap->cd($this->config->current['BASE']);
940 $ldap->search("(&(objectClass=posixGroup)(gidNumber=".$this->gidNumber."))", array("cn"));
942 /* Create group if it doesn't exist */
943 if ($ldap->count() == 0){
944 $groupdn= preg_replace ('/^'.$this->config->current['DNMODE'].'=[^,]+,'.get_people_ou().'/i', 'cn='.$this->uid.','.get_groups_ou(), $this->dn);
946 $g= new group($this->config, $groupdn);
947 $g->cn= $this->uid;
948 $g->force_gid= 1;
949 $g->gidNumber= $this->gidNumber;
950 $g->description= _("Group of user")." ".$this->givenName." ".$this->sn;
951 $g->save ();
952 }
953 }
955 /* Take care about groupMembership values: add to groups */
956 foreach ($this->groupMembership as $key => $value){
957 if (!isset($this->savedGroupMembership[$key])){
958 $g= new grouptabs($this->config,$this->config->data['TABS']['GROUPTABS'], $key,"groups");
959 $g->set_acl_base($key);
960 $g->by_object['group']->addUser($this->uid);
961 $g->save();
962 }
963 }
965 /* Remove from groups not listed in groupMembership */
966 foreach ($this->savedGroupMembership as $key => $value){
967 if (!isset($this->groupMembership[$key])){
968 $g= new grouptabs($this->config,$this->config->data['TABS']['GROUPTABS'], $key,"groups");
969 $g->set_acl_base($key);
970 $g->by_object['group']->removeUser ($this->uid);
971 $g->save();
972 }
973 }
975 /* Optionally execute a command after we're done */
976 if ($this->initially_was_account == $this->is_account){
977 if ($this->is_modified){
978 $this->handle_post_events("modify",array("uid" => $this->uid));
979 }
980 } else {
981 $this->handle_post_events("add" ,array("uid"=> $this->uid));
982 }
983 }
985 /* Check formular input */
986 function check()
987 {
988 /* Include global link_info */
989 $ldap= $this->config->get_ldap_link();
991 /* Append groups as memberGroup: to check hook
992 */
993 $tmp_attributes = $this->attributes;
994 $this->attributes[] = "memberGroup";
995 $this->memberGroup = array();
996 foreach($this->groupMembership as $dn => $name){
997 $this->memberGroup[] = $name;
998 }
1000 /* Call common method to give check the hook */
1001 $message= plugin::check();
1002 $this->attributes = $tmp_attributes;
1004 /* must: homeDirectory */
1005 if ($this->homeDirectory == ""){
1006 $message[]= msgPool::required(_("Home directory"));
1007 }
1008 if (!tests::is_path($this->homeDirectory)){
1009 $message[]= msgPool::invalid(_("Home directory"), "", "", "/home/yourname" );
1010 }
1012 /* Check ID's if they are forced by user */
1013 if ($this->force_ids == "1"){
1015 /* Valid uid/gid? */
1016 if (!tests::is_id($this->uidNumber)){
1017 $message[]= msgPool::invalid(_("UID"), $this->uidNumber, "/[0-9]/");
1018 } else {
1019 if ($this->uidNumber < $this->config->current['MINID']){
1020 $message[]= msgPool::toosmall(_("UID"), $this->config->current['MINID']);
1021 }
1022 }
1023 if (!tests::is_id($this->gidNumber)){
1024 $message[]= msgPool::invalid(_("GID"), $this->gidNumber, "/[0-9]/");
1025 } else {
1026 if ($this->gidNumber < $this->config->current['MINID']){
1027 $message[]= msgPool::toosmall(_("GID"), $this->config->current['MINID']);
1028 }
1029 }
1030 }
1032 /* Check shadow settings, well I like spaghetties... */
1033 if ($this->activate_shadowMin){
1034 if (!tests::is_id($this->shadowMin)){
1035 $message[]= msgPool::invalid(_("shadowMin"), $this->shadowMin, "/[0-9]/");
1036 }
1037 }
1038 if ($this->activate_shadowMax){
1039 if (!tests::is_id($this->shadowMax)){
1040 $message[]= msgPool::invalid(_("shadowMax"), $this->shadowMax, "/[0-9]/");
1041 }
1042 }
1043 if ($this->activate_shadowWarning){
1044 if (!tests::is_id($this->shadowWarning)){
1045 $message[]= msgPool::invalid(_("shadowWarning"), $this->shadowWarning, "/[0-9]/");
1046 }
1047 if (!$this->activate_shadowMax){
1048 $message[]= msgPool::depends("shadowWarning", "shadowMax");
1049 }
1050 if ($this->shadowWarning > $this->shadowMax){
1051 $message[]= msgPool::smaller("shadowWarning", "shadowMax");
1052 }
1053 if ($this->activate_shadowMin && $this->shadowWarning < $this->shadowMin){
1054 $message[]= msgPool::bigger("shadowWarning", "shadowMin");
1055 }
1056 }
1057 if ($this->activate_shadowInactive){
1058 if (!tests::is_id($this->shadowInactive)){
1059 $message[]= msgPool::invalid(_("shadowInactive"), $this->shadowInactive, "/[0-9]/");
1060 }
1061 if (!$this->activate_shadowMax){
1062 $message[]= msgPool::depends("shadowInactive", "shadowMax");
1063 }
1064 }
1065 if ($this->activate_shadowMin && $this->activate_shadowMax){
1066 if ($this->shadowMin > $this->shadowMax){
1067 $message[]= msgPool::smaller("shadowMin", "shadowMax");
1068 }
1069 }
1071 return ($message);
1072 }
1075 function multiple_check()
1076 {
1077 $message = plugin::multiple_check();
1078 if ($this->homeDirectory == "" && in_array("homeDirectory",$this->multi_boxes)){
1079 $message[]= msgPool::required(_("Home directory"));
1080 }
1081 if (!tests::is_path($this->homeDirectory) && in_array("homeDirectory",$this->multi_boxes)){
1082 $message[]= msgPool::invalid(_("Home directory"), "", "", "/home/yourname" );
1083 }
1085 /* Check shadow settings, well I like spaghetties... */
1086 if ($this->activate_shadowMin && in_array("activate_shadowMin",$this->multi_boxes)){
1087 if (!tests::is_id($this->shadowMin)){
1088 $message[]= msgPool::invalid(_("shadowMin"), $this->shadowMin, "/[0-9]/");
1089 }
1090 }
1091 if ($this->activate_shadowMax && in_array("activate_shadowMax",$this->multi_boxes)){
1092 if (!tests::is_id($this->shadowMax)){
1093 $message[]= msgPool::invalid(_("shadowMax"), $this->shadowMax, "/[0-9]/");
1094 }
1095 }
1096 if ($this->activate_shadowWarning && in_array("activate_shadowWarning",$this->multi_boxes)){
1097 if (!tests::is_id($this->shadowWarning)){
1098 $message[]= msgPool::invalid(_("shadowWarning"), $this->shadowWarning, "/[0-9]/");
1099 }
1100 if (!$this->activate_shadowMax && in_array("activate_shadowMax",$this->multi_boxes)){
1101 $message[]= msgPool::depends("shadowWarning", "shadowMax");
1102 }
1103 if ($this->shadowWarning > $this->shadowMax && in_array("activate_shadowWarning",$this->multi_boxes)){
1104 $message[]= msgPool::smaller("shadowWarning", "shadowMax");
1105 }
1106 if ($this->activate_shadowMin && $this->shadowWarning < $this->shadowMin && in_array("activate_shadowMin",$this->multi_boxes)){
1107 $message[]= msgPool::bigger("shadowWarning", "shadowMin");
1108 }
1109 }
1110 if ($this->activate_shadowInactive && in_array("activate_shadowInactive",$this->multi_boxes)){
1111 if (!tests::is_id($this->shadowInactive)){
1112 $message[]= msgPool::invalid(_("shadowInactive"), $this->shadowInactive, "/[0-9]/");
1113 }
1114 if (!$this->activate_shadowMax && in_array("activate_shadowMax",$this->multi_boxes)){
1115 $message[]= msgPool::depends("shadowInactive", "shadowMax");
1116 }
1117 }
1118 if ($this->activate_shadowMin && $this->activate_shadowMax && in_array("activate_shadowMin",$this->multi_boxes)){
1119 if ($this->shadowMin > $this->shadowMax){
1120 $message[]= msgPool::smaller("shadowMin", "shadowMax");
1121 }
1122 }
1124 return($message);
1125 }
1128 function addGroup ($groups)
1129 {
1130 /* include global link_info */
1131 $ldap= $this->config->get_ldap_link();
1133 /* Walk through groups and add the descriptive entry if not exists */
1134 foreach ($groups as $value){
1136 if (!array_key_exists($value, $this->groupMembership)){
1137 $ldap->cat($value, array('cn', 'description', 'dn'));
1138 $attrs= $ldap->fetch();
1139 error_reporting (0);
1140 if (!isset($attrs['description'][0])){
1141 $entry= $attrs["cn"][0];
1142 } else {
1143 $dsc= preg_replace ('/^Group of user/', _("Group of user"), $attrs["description"][0]);
1144 $entry= $attrs["cn"][0]." [$dsc]";
1145 }
1146 error_reporting (E_ALL | E_STRICT);
1148 if(obj_is_writable($attrs['dn'],"groups/group","memberUid")){
1149 $this->groupMembership[$attrs['dn']]= $entry;
1150 if($this->multiple_support_active && isset($this->groupMembership_some[$attrs['dn']])){
1151 unset($this->groupMembership_some[$attrs['dn']]);
1152 }
1153 }
1154 }
1155 }
1157 /* Sort groups */
1158 asort ($this->groupMembership);
1159 reset ($this->groupMembership);
1160 }
1163 /* Del posix user from some groups */
1164 function delGroup ($groups)
1165 {
1166 $dest= array();
1167 foreach($groups as $dn_to_del){
1168 if(isset($this->groupMembership[$dn_to_del]) && obj_is_writable($dn_to_del,"groups/group","memberUid")){
1169 unset($this->groupMembership[$dn_to_del]);
1170 }
1171 if($this->multiple_support_active){
1172 if(isset($this->groupMembership_some[$dn_to_del]) && obj_is_writable($dn_to_del,"groups/group","memberUid")){
1173 unset($this->groupMembership_some[$dn_to_del]);
1174 }
1175 }
1176 }
1177 }
1180 /* Adapt from template, using 'dn' */
1181 function adapt_from_template($dn)
1182 {
1183 /* Include global link_info */
1184 $ldap= $this->config->get_ldap_link();
1186 plugin::adapt_from_template($dn);
1187 $template= $this->attrs['uid'][0];
1189 /* Adapt group membership */
1190 $ldap->cd($this->config->current['BASE']);
1191 $ldap->search("(&(objectClass=posixGroup)(memberUid=".$this->attrs["uid"][0]."))", array("description", "cn"));
1193 while ($this->attrs= $ldap->fetch()){
1194 if (!isset($this->attrs["description"][0])){
1195 $entry= $this->attrs["cn"][0];
1196 } else {
1197 $entry= $this->attrs["cn"][0]." [".$this->attrs["description"][0]."]";
1198 }
1199 $this->groupMembership[$ldap->getDN()]= $entry;
1200 }
1202 /* Fix primary group settings */
1203 $ldap->cd($this->config->current['BASE']);
1204 $ldap->search("(&(objectClass=posixGroup)(cn=$template)(gidNumber=".$this->gidNumber."))", array("cn"));
1205 if ($ldap->count() != 1){
1206 $this->primaryGroup= $this->gidNumber;
1207 }
1209 $ldap->cd($this->config->current['BASE']);
1210 $ldap->search("(&(objectClass=gosaUserTemplate)(uid=".$template.")(accessTo=*))", array("cn","accessTo"));
1211 while($attr = $ldap->fetch()){
1212 $tmp = $attr['accessTo'];
1213 unset ($tmp['count']);
1214 $this->accessTo = $tmp;
1215 }
1217 /* Adjust shadow checkboxes */
1218 foreach (array("shadowMin", "shadowMax", "shadowWarning", "shadowInactive") as $val){
1219 if ($this->$val != 0){
1220 $oval= "activate_".$val;
1221 $this->$oval= "1";
1222 }
1223 }
1225 /* FIXME: NEED review of this section */
1226 /* Need to check shadowExpire separately */
1228 /*
1229 * If shadowExpire is not enabled in the template, it's a UNIX timestamp - so don't convert it to seconds.
1230 * The check is a hack - if difference between timestamp generated above and here is max 1 day.
1231 */
1232 if(abs($this->shadowExpire - time())>86400) {
1233 $this->shadowExpire= $this->convertToSeconds($this->shadowExpire);
1234 }
1236 /* Only enable checkbox, if shadowExpire is in the future */
1237 if($this->shadowExpire > time()) {
1238 $this->activate_shadowExpire= "1";
1239 }
1240 }
1242 function convertToSeconds($val)
1243 {
1244 if ($val != 0){
1245 $val*= 60 * 60 * 24;
1246 } else {
1247 $date= getdate();
1248 $val= floor($date[0] / (60*60*24)) * 60 * 60 * 24;
1249 }
1250 return($val);
1251 }
1254 function get_used_uid_numbers()
1255 {
1256 $ids= array();
1257 $ldap= $this->config->get_ldap_link();
1259 $ldap->cd ($this->config->current['BASE']);
1260 $ldap->search ("(&(objectClass=posixAccount)(uidNumber=*))", array("uidNumber"));
1262 /* Get list of ids */
1263 while ($attrs= $ldap->fetch()){
1264 $ids[$attrs['uidNumber'][0]] = $attrs['dn'];
1265 }
1266 return($ids);
1267 }
1270 function get_next_id($attrib, $dn)
1271 {
1272 $ids= array();
1273 $ldap= $this->config->get_ldap_link();
1275 $ldap->cd ($this->config->current['BASE']);
1276 if (preg_match('/gidNumber/i', $attrib)){
1277 $oc= "posixGroup";
1278 } else {
1279 $oc= "posixAccount";
1280 }
1281 $ldap->search ("(&(objectClass=$oc)($attrib=*))", array("$attrib"));
1283 /* Get list of ids */
1284 while ($attrs= $ldap->fetch()){
1285 $ids[]= (int)$attrs["$attrib"][0];
1286 }
1288 /* Add the nobody id */
1289 $ids[]= 65534;
1291 /* get the ranges */
1292 $tmp = array('0'=> 1000);
1293 if (preg_match('/posixAccount/', $oc) && isset($this->config->current['UIDBASE'])) {
1294 $tmp= split('-',$this->config->current['UIDBASE']);
1295 } elseif(isset($this->config->current['GIDBASE'])){
1296 $tmp= split('-',$this->config->current['GIDBASE']);
1297 }
1299 /* Set hwm to max if not set - for backward compatibility */
1300 $lwm= $tmp[0];
1301 if (isset($tmp[1])){
1302 $hwm= $tmp[1];
1303 } else {
1304 $hwm= pow(2,32);
1305 }
1307 /* Find out next free id near to UID_BASE */
1308 if (!isset($this->config->current['BASE_HOOK'])){
1309 $base= $lwm;
1310 } else {
1311 /* Call base hook */
1312 $base= get_base_from_hook($dn, $attrib);
1313 }
1314 for ($id= $base; $id++; $id < pow(2,32)){
1315 if (!in_array($id, $ids)){
1316 return ($id);
1317 }
1318 }
1320 /* Should not happen */
1321 if ($id == $hwm){
1322 msg_dialog::display(_("Error"), _("Cannot allocate a free ID: too many users!"), ERROR_DIALOG);
1323 exit;
1324 }
1326 }
1328 function reload()
1329 {
1330 /* Set base for all searches */
1331 $base = session::get('CurrentMainBase');
1332 $base = $base;
1333 $ldap = $this->config->get_ldap_link();
1334 $attrs = array("cn", "description", "gidNumber");
1335 $Flags = GL_SIZELIMIT;
1337 /* Get groups */
1338 if ($this->GroupUserRegex == '*'){
1339 $filter = "(&(objectClass=posixGroup)(cn=".$this->GroupRegex."))";
1340 } else {
1341 $filter= "(&(objectClass=posixGroup)(cn=".$this->GroupRegex.")(memberUid=".$this->GroupUserRegex."))";
1342 }
1343 if($this->SubSearch){
1344 $Flags |= GL_SUBSEARCH;
1345 }else{
1346 $base = get_groups_ou().$base;
1347 }
1349 $res= get_list($filter, "groups", $base,$attrs, $Flags);
1351 /* check sizelimit */
1352 if (preg_match("/size limit/i", $ldap->error)){
1353 session::set('limit_exceeded',TRUE);
1354 }
1356 /* Create a list of users */
1357 $this->grouplist = array();
1358 foreach ($res as $value){
1359 $this->grouplist[$value['gidNumber'][0]]= $value;
1360 }
1362 $tmp=array();
1363 foreach($this->grouplist as $tkey => $val ){
1364 $tmp[strtolower($val['cn'][0]).$val['cn'][0]]=$val;
1365 }
1367 /* Sort index */
1368 ksort($tmp);
1370 /* Recreate index array[dn]=cn[description]*/
1371 $this->grouplist=array();
1372 foreach($tmp as $val){
1373 if(isset($val['description'])){
1374 $this->grouplist[$val['dn']]=$val['cn'][0]." [".$val['description'][0]."]";
1375 }else{
1376 $this->grouplist[$val['dn']]=$val['cn'][0];
1377 }
1378 }
1380 reset ($this->grouplist);
1381 }
1384 /* Get posts from copy & paste dialog */
1385 function saveCopyDialog()
1386 {
1387 if(isset($_POST['homeDirectory'])){
1388 $this->homeDirectory = $_POST['homeDirectory'];
1389 if (isset ($_POST['force_ids'])){
1390 $data= 1;
1391 $this->gidNumber = $_POST['gidNumber'];
1392 $this->uidNumber = $_POST['uidNumber'];
1393 } else {
1394 $data= 0;
1395 }
1396 if ($this->force_ids != $data){
1397 $this->is_modified= TRUE;
1398 }
1399 $this->force_ids= $data;
1400 }
1401 }
1404 /* Create the posix dialog part for copy & paste */
1405 function getCopyDialog()
1406 {
1407 /* Skip dialog creation if this is not a valid account*/
1408 if(!$this->is_account) return("");
1409 if ($this->force_ids == 1){
1410 $force_ids = "checked";
1411 if (session::get('js')){
1412 $forceMode = "";
1413 }
1414 } else {
1415 if (session::get('js')){
1416 if($this->acl != "#none#")
1417 $forceMode ="disabled";
1418 }
1419 $force_ids = "";
1420 }
1422 $sta = "";
1424 /* Open group add dialog */
1425 if(isset($_POST['edit_groupmembership'])){
1426 $this->group_dialog = TRUE;
1427 $sta = "SubDialog";
1428 }
1430 /* If the group-add dialog is closed, call execute
1431 to ensure that the membership is updatd */
1432 if(isset($_POST['add_groups_finish']) || isset($_POST['add_groups_cancel'])){
1433 $this->execute();
1434 $this->group_dialog =FALSE;
1435 }
1437 if($this->group_dialog){
1438 $str = $this->execute(true);
1439 $ret = array();
1440 $ret['string'] = $str;
1441 $ret['status'] = $sta;
1442 return($ret);
1443 }
1445 /* If a group member should be deleted, simply call execute */
1446 if(isset($_POST['delete_groupmembership'])){
1447 $this->execute();
1448 }
1450 /* Assigned informations to smarty */
1451 $smarty = get_smarty();
1452 $smarty->assign("homeDirectory",$this->homeDirectory);
1453 $smarty->assign("uidNumber",$this->uidNumber);
1454 $smarty->assign("gidNumber",$this->gidNumber);
1455 $smarty->assign("forceMode",$forceMode);
1456 $smarty->assign("force_ids",$force_ids);
1457 if (!count($this->groupMembership)){
1458 $smarty->assign("groupMembership", array(" "));
1459 } else {
1460 $smarty->assign("groupMembership", $this->groupMembership);
1461 }
1463 /* Display wars message if there are more than 16 group members */
1464 if (count($this->groupMembership) > 16){
1465 $smarty->assign("groups", "too_many_for_nfs");
1466 } else {
1467 $smarty->assign("groups", "");
1468 }
1469 $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE,dirname(__FILE__)));
1471 $ret = array();
1472 $ret['string'] = $str;
1473 $ret['status'] = $sta;
1474 return($ret);
1475 }
1478 function PrepareForCopyPaste($source)
1479 {
1480 plugin::PrepareForCopyPaste($source);
1482 /* Avoid using the same gid/uid number as source user */
1483 $this->savedUidNumber = $this->get_next_id("uidNumber", $this->dn);
1484 $this->savedGidNumber = $this->get_next_id("gidNumber", $this->dn);
1485 }
1488 function multiple_execute()
1489 {
1490 return($this->execute());
1491 }
1494 static function plInfo()
1495 {
1496 return (array(
1497 "plDescription" => _("POSIX account"),
1498 "plSelfModify" => TRUE,
1499 "plDepends" => array("user"),
1500 "plPriority" => 2,
1501 "plSection" => array("personal" => _("My account")),
1502 "plCategory" => array("users"),
1503 "plOptions" => array(),
1505 "plProvidedAcls" => array(
1507 "homeDirectory" => _("Home directory"),
1508 "loginShell" => _("Shell"),
1509 "uidNumber" => _("User ID"),
1510 "gidNumber" => _("Group ID"),
1512 "mustchangepassword"=> _("Force password change on login"),
1513 "shadowMin" => _("Shadow min"),
1514 "shadowMax" => _("Shadow max"),
1515 "shadowWarning" => _("Shadow warning"),
1516 "shadowInactive" => _("Shadow inactive"),
1517 "shadowExpire" => _("Shadow expire"),
1518 "trustModel" => _("System trust model")))
1519 );
1520 }
1523 /* Return selected values for multiple edit */
1524 function get_multi_edit_values()
1525 {
1526 $ret = plugin::get_multi_edit_values();
1527 $ret['groupMembership'] = $this->groupMembership;
1528 $ret['groupMembership_some']= $this->groupMembership_some;
1530 if(in_array("primaryGroup",$this->multi_boxes)){
1531 $ret['primaryGroup'] = $this->primaryGroup;
1532 }
1533 if(in_array("trustmode",$this->multi_boxes)){
1534 $ret['trustModel'] = $this->trustModel;
1535 $ret['accessTo'] = $this->accessTo;
1536 }
1537 foreach(array("shadowWarning","shadowInactive","shadowMin","shadowMax", "shadowExpire") as $entry){
1538 $active = "activate_".$entry;
1539 if(in_array($active,$this->multi_boxes)){
1540 $ret[$entry] = $this->$entry;
1541 $ret[$active] = $this->$active;
1542 }
1543 }
1544 if(in_array("mustchangepassword",$this->multi_boxes)){
1545 $ret['mustchangepassword'] = $this->mustchangepassword;
1546 }
1547 return($ret);
1548 }
1551 /* Save posts for multiple edit
1552 */
1553 function multiple_save_object()
1554 {
1555 if(isset($_POST['posix_mulitple_edit'])){
1557 /* Backup expire value */
1558 $expire_tmp = $this->shadowExpire;
1560 /* Update all values */
1561 plugin::multiple_save_object();
1563 /* Get selected checkboxes */
1564 foreach(array("primaryGroup","trustmode","mustchangepassword","activate_shadowWarning","activate_shadowInactive","activate_shadowMin", "activate_shadowMax","activate_shadowExpire") as $val){
1565 if(isset($_POST["use_".$val])){
1566 $this->multi_boxes[] = $val;
1567 }
1568 }
1570 /* Update special values, checkboxes for posixShadow */
1571 foreach(array("shadowMin","shadowMax","shadowExpire","shadowInactive","shadowWarning") as $var) {
1572 if($this->acl_is_writeable($var)){
1573 $activate_var = "activate_".$var;
1574 if(in_array($activate_var, $this->multi_boxes)){
1575 if(isset($_POST['activate_'.$var])){
1576 $this->$activate_var = true;
1577 $this->$var = $_POST[$var];
1578 }else{
1579 $this->$activate_var = false;
1580 $this->$var = 0;
1581 }
1582 }
1583 }
1584 }
1586 /* Restore shadow value, if the shadow attribute isn't used */
1587 if(!in_array("activate_shadowExpire",$this->multi_boxes)){
1588 $this->shadowExpire = $expire_tmp;
1589 }
1591 /* Force change password ? */
1592 if(isset($_POST['mustchangepassword'])){
1593 $this->mustchangepassword = TRUE;
1594 }else{
1595 $this->mustchangepassword = FALSE;
1596 }
1598 /* Trust mode - special handling */
1599 if($this->acl_is_writeable("trustModel")){
1600 if (isset($_POST['trustmode'])){
1601 $saved= $this->trustModel;
1602 if ($_POST['trustmode'] == "1"){
1603 $this->trustModel= "fullaccess";
1604 } elseif ($_POST['trustmode'] == "2"){
1605 $this->trustModel= "byhost";
1606 } else {
1607 $this->trustModel= "";
1608 }
1609 if ($this->trustModel != $saved){
1610 $this->is_modified= TRUE;
1611 }
1612 }
1613 }
1615 /* Save primary group settings */
1616 if($this->acl_is_writeable("primaryGroup") && isset($_POST['primaryGroup'])){
1617 $data= $_POST['primaryGroup'];
1618 if ($this->primaryGroup != $data){
1619 $this->is_modified= TRUE;
1620 }
1621 $this->primaryGroup= $_POST['primaryGroup'];
1622 }
1623 }
1624 }
1627 /* Initialize plugin with given atribute arrays
1628 */
1629 function init_multiple_support($attrs,$all)
1630 {
1631 plugin::init_multiple_support($attrs,$all);
1633 /* Some dummy values */
1634 $groups_some = array();
1635 $groups_all = array();
1636 $groups_uid = array();
1637 $uids = array();
1638 $first = TRUE;
1640 /* Get all groups used by currently edited users */
1641 $uid_filter="";
1642 for($i =0; $i < $this->multi_attrs_all['uid']['count'] ; $i ++){
1643 $uid = $this->multi_attrs_all['uid'][$i];
1644 $uids[] = $uid;
1645 $uid_filter.= "(memberUid=".$uid.")";
1646 }
1647 $uid_filter = "(&(objectClass=posixGroup)(|".$uid_filter."))";
1648 $ldap = $this->config->get_ldap_link();
1649 $ldap->cd($this->config->current['BASE']);
1650 $ldap->search($uid_filter,array("dn","cn","memberUid"));
1651 while($group = $ldap->fetch()){
1652 $groups_some[$group['dn']] = $group['cn'][0];
1653 for($i = 0 ; $i < $group['memberUid']['count'] ; $i++){
1654 $groups_uid[$group['dn']][] = $group['memberUid'][$i];
1655 }
1656 }
1658 /* Create an array, containing all used groups */
1659 $groups_all = $groups_some;
1660 foreach($groups_all as $id => $group){
1661 foreach($uids as $uid){
1662 if(!in_array($uid,$groups_uid[$id])){
1663 unset($groups_all[$id]);
1664 break;
1665 }
1666 }
1667 }
1669 /* Assign group array */
1670 $this->groupMembership = $groups_all;
1672 /* Create an array of all grouops used by all users */
1673 foreach( $groups_all as $dn => $cn){
1674 if(isset($groups_some[$dn])){
1675 unset($groups_some[$dn]);
1676 }
1677 }
1678 $this->groupMembership_some = $groups_some;
1679 $this->primaryGroup = $this->gidNumber;
1681 /* Is this account a trustAccount? */
1682 if (isset($this->multi_attrs['trustModel'])){
1683 $this->trustModel= $this->multi_attrs['trustModel'][0];
1684 $this->was_trust_account= TRUE;
1685 $this->multi_boxes[] = "trustmode";
1686 } else {
1687 $this->was_trust_account= FALSE;
1688 $this->trustModel= "";
1689 }
1691 /* Create access informations */
1692 $this->accessTo = array();
1693 if (isset($this->multi_attrs['accessTo'])){
1694 for ($i= 0; $i<$this->multi_attrs['accessTo']['count']; $i++){
1695 $tmp= $this->multi_attrs['accessTo'][$i];
1696 $this->accessTo[$tmp]= $tmp;
1697 }
1698 }
1700 /* Adjust shadow checkboxes */
1701 foreach (array("shadowMin", "shadowMax", "shadowWarning", "shadowInactive",
1702 "shadowExpire") as $val){
1703 if ($this->$val != 0){
1704 $oval= "activate_".$val;
1705 $this->$oval= "1";
1706 }
1707 }
1709 /* Convert to seconds */
1710 if(isset($this->multi_attrs['shadowExpire'])){
1711 $this->shadowExpire = $this->convertToSeconds($this->multi_attrs['shadowExpire'][0]);
1712 }else{
1713 $this->activate_shadowExpire = FALSE;
1714 }
1715 }
1718 function set_multi_edit_values($attrs)
1719 {
1720 $groups = array();
1722 /* Update groupMembership, keep optinal group */
1723 foreach($attrs['groupMembership_some'] as $dn => $cn){
1724 if(isset($this->groupMembership[$dn])){
1725 $groups[$dn] = $cn;
1726 }
1727 }
1728 /* Update groupMembership, add forced groups */
1729 foreach($attrs['groupMembership'] as $dn => $cn){
1730 $groups[$dn] = $cn;
1731 }
1732 plugin::set_multi_edit_values($attrs);
1733 $this->groupMembership = $groups;
1734 }
1735 }
1737 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1738 ?>