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 settings";
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 changeable");
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/small-error.png\" align=\"middle\"> <b>".
275 msgPool::noValidExtension(_("POSIX"))."</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(msgPool::removeFeaturesButton(_("POSIX")), msgPool::featuresEnabled(_("POSIX"), array(_("Samba"), _("Environment"))), TRUE);
296 } else {
297 $display= $this->show_disable_header(msgPool::removeFeaturesButton(_("POSIX")), msgPool::featuresEnabled(_("POSIX")));
298 }
299 } else {
300 $display= $this->show_enable_header(msgPool::addFeaturesButton(_("POSIX")), msgPool::featuresDisabled(_("POSIX")));
301 return($display);
302 }
303 }
304 }
305 /* Trigger group edit? */
306 if (isset($_POST['edit_groupmembership'])){
307 $this->group_dialog= TRUE;
308 $this->dialog= TRUE;
309 }
311 /* Cancel group edit? */
312 if (isset($_POST['add_groups_cancel']) ||
313 isset($_POST['add_groups_finish'])){
314 $this->group_dialog= FALSE;
315 $this->dialog= FALSE;
316 }
318 /* Add selected groups */
319 if (isset($_POST['add_groups_finish']) && isset($_POST['groups']) &&
320 count($_POST['groups'])){
322 $this->addGroup ($_POST['groups']);
323 }
325 /* Delete selected groups */
326 if (isset($_POST['delete_groupmembership']) &&
327 isset($_POST['group_list']) && count($_POST['group_list'])){
329 $this->delGroup ($_POST['group_list']);
330 }
332 /* Add user workstation? */
333 if (isset($_POST["add_ws"])){
334 $this->show_ws_dialog= TRUE;
335 $this->dialog= TRUE;
336 }
338 /* Add user workstation? */
339 if (isset($_POST["add_ws_finish"]) && isset($_POST['wslist'])){
340 foreach($_POST['wslist'] as $ws){
341 $this->accessTo[$ws]= $ws;
342 }
343 ksort($this->accessTo);
344 $this->is_modified= TRUE;
345 }
347 /* Remove user workstations? */
348 if (isset($_POST["delete_ws"]) && isset($_POST['workstation_list'])){
349 foreach($_POST['workstation_list'] as $name){
350 unset ($this->accessTo[$name]);
351 }
352 $this->is_modified= TRUE;
353 }
355 /* Add user workstation finished? */
356 if (isset($_POST["add_ws_finish"]) || isset($_POST["add_ws_cancel"])){
357 $this->show_ws_dialog= FALSE;
358 $this->dialog= FALSE;
359 }
361 /* Templates now! */
362 $smarty= get_smarty();
364 /* Show ws dialog */
365 if ($this->show_ws_dialog){
366 /* Save data */
367 $sysfilter= session::get("sysfilter");
368 foreach( array("depselect", "regex") as $type){
369 if (isset($_POST[$type])){
370 $sysfilter[$type]= $_POST[$type];
371 }
372 }
373 if (isset($_GET['search'])){
374 $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
375 if ($s == "**"){
376 $s= "*";
377 }
378 $sysfilter['regex']= $s;
379 }
380 session::set("sysfilter", $sysfilter);
382 /* Get workstation list */
383 $exclude= "";
384 foreach($this->accessTo as $ws){
385 $exclude.= "(cn=$ws)";
386 }
387 if ($exclude != ""){
388 $exclude= "(!(|$exclude))";
389 }
390 $regex= $sysfilter['regex'];
392 $acls = array();
393 if(class_available("servgeneric")) $acls[] = "server";
394 if(class_available("workgeneric")) $acls[] = "worstation";
395 if(class_available("termgeneric")) $acls[] = "terminal";
397 $filter= "(&(|(objectClass=goServer)(objectClass=gotoWorkstation)(objectClass=gotoTerminal))$exclude(cn=*)(cn=$regex))";
399 $deps_a = array(get_ou("serverRDN"),
400 get_ou("terminalRDN"),
401 get_ou("workstationRDN"));
403 $res= get_sub_list($filter, $acls, $deps_a, get_ou("systemRDN").$sysfilter['depselect'], array("cn"), GL_SUBSEARCH | GL_SIZELIMIT);
404 $wslist= array();
405 foreach ($res as $attrs){
406 $wslist[]= preg_replace('/\$/', '', $attrs['cn'][0]);
407 }
408 asort($wslist);
409 $smarty->assign("search_image", get_template_path('images/lists/search.png'));
410 $smarty->assign("launchimage", get_template_path('images/lists/action.png'));
411 $smarty->assign("tree_image", get_template_path('images/lists/search-subtree.png'));
412 $smarty->assign("deplist", $this->config->idepartments);
413 $smarty->assign("alphabet", generate_alphabet());
414 foreach( array("depselect", "regex") as $type){
415 $smarty->assign("$type", $sysfilter[$type]);
416 }
417 $smarty->assign("hint", print_sizelimit_warning());
418 $smarty->assign("wslist", $wslist);
419 $smarty->assign("apply", apply_filter());
420 $display= $smarty->fetch (get_template_path('trust_machines.tpl', TRUE, dirname(__FILE__)));
421 return ($display);
422 }
424 /* Manage group add dialog */
425 if ($this->group_dialog){
427 /* Get global filter config */
428 $this->reload();
430 /* remove already assigned groups */
431 $glist= array();
432 foreach ($this->grouplist as $key => $value){
433 if (!isset($this->groupMembership[$key]) && obj_is_writable($key,"groups/group","memberUid")){
434 $glist[$key]= $value;
435 }
436 }
438 if($this->SubSearch){
439 $smarty->assign("SubSearchCHK"," checked ");
440 }else{
441 $smarty->assign("SubSearchCHK","");
442 }
444 $smarty->assign("regex",$this->GroupRegex);
445 $smarty->assign("guser",$this->GroupUserRegex);
446 $smarty->assign("groups", $glist);
447 $smarty->assign("search_image", get_template_path('images/lists/search.png'));
448 $smarty->assign("launchimage", get_template_path('images/lists/action.png'));
449 $smarty->assign("tree_image", get_template_path('images/lists/search-subtree.png'));
450 $smarty->assign("deplist", $this->config->idepartments);
451 $smarty->assign("alphabet", generate_alphabet());
452 $smarty->assign("depselect", session::get('CurrentMainBase'));
453 $smarty->assign("hint", print_sizelimit_warning());
455 $smarty->assign("apply", apply_filter());
456 $display.= $smarty->fetch (get_template_path('posix_groups.tpl', TRUE, dirname(__FILE__)));
457 return ($display);
458 }
460 /* Show main page */
461 $smarty= get_smarty();
463 /* In 'MyAccount' mode, we must remove write acls if we are not in editing mode. */
464 $SkipWrite = (!isset($this->parent) || !$this->parent) && !session::is_set('edit');
466 /* Depending on pwmode, currently hardcoded because there are no other methods */
467 if ( 1 == 1 ){
468 $smarty->assign("pwmode", dirname(__FILE__)."/posix_shadow");
470 $shadowMinACL = $this->getacl("shadowMin",$SkipWrite);
471 $smarty->assign("shadowmins", sprintf(_("Password can't be changed up to %s days after last change"),
472 "<input name=\"shadowMin\" size=3 maxlength=4 value=\"".$this->shadowMin."\">"));
474 $shadowMaxACL = $this->getacl("shadowMax",$SkipWrite);
475 $smarty->assign("shadowmaxs", sprintf(_("Password must be changed after %s days"),
476 "<input name=\"shadowMax\" size=3 maxlength=4 value=\"".$this->shadowMax."\">"));
478 $shadowInactiveACL= $this->getacl("shadowInactive",$SkipWrite);
479 $smarty->assign("shadowinactives", sprintf(_("Disable account after %s days of inactivity after password expiry"),
480 "<input name=\"shadowInactive\" size=3 maxlength=4 value=\"".$this->shadowInactive."\">"));
482 $shadowWarningACL = $this->getacl("shadowWarning",$SkipWrite);
483 $smarty->assign("shadowwarnings", sprintf(_("Warn user %s days before password expiry"),
484 "<input name=\"shadowWarning\" size=3 maxlength=4 value=\"".$this->shadowWarning."\">"));
486 foreach( array("activate_shadowMin", "activate_shadowMax",
487 "activate_shadowExpire", "activate_shadowInactive","activate_shadowWarning") as $val){
488 if ($this->$val == 1){
489 $smarty->assign("$val", "checked");
490 } else {
491 $smarty->assign("$val", "");
492 }
493 $smarty->assign("$val"."ACL", $this->getacl($val,$SkipWrite));
494 }
496 $smarty->assign("mustchangepasswordACL", $this->getacl("mustchangepassword",$SkipWrite));
497 }
499 /* Fill calendar */
500 /* If this $this->shadowExpire is empty
501 use current date as base for calculating selectbox values.
502 (This attribute is empty if this is a new user )*/
503 if(empty($this->shadowExpire)){
504 $date= getdate(time());
505 }else{
506 $date= getdate($this->shadowExpire);
507 }
509 $days= array();
510 for($d= 1; $d<32; $d++){
511 $days[$d]= $d;
512 }
513 $years= array();
514 for($y= $date['year']-10; $y<$date['year']+10; $y++){
515 $years[]= $y;
516 }
517 $months= msgPool::months();
518 $smarty->assign("day", $date["mday"]);
519 $smarty->assign("days", $days);
520 $smarty->assign("months", $months);
521 $smarty->assign("month", $date["mon"]-1);
522 $smarty->assign("years", $years);
523 $smarty->assign("year", $date["year"]);
525 /* Fill arrays */
526 $smarty->assign("shells", $this->loginShellList);
527 $smarty->assign("secondaryGroups", $this->secondaryGroups);
528 $smarty->assign("primaryGroup", $this->primaryGroup);
529 if(!$this->multiple_support_active){
530 if (!count($this->groupMembership)){
531 $smarty->assign("groupMembership", array(" "));
532 } else {
533 $smarty->assign("groupMembership", $this->groupMembership);
534 }
535 }else{
536 $smarty->assign("groupMembership", $this->groupMembership);
537 $smarty->assign("groupMembership_some", $this->groupMembership_some);
538 }
539 if (count($this->groupMembership) > 16){
540 $smarty->assign("groups", "too_many_for_nfs");
541 } else {
542 $smarty->assign("groups", "");
543 }
545 /* Avoid "Undefined index: forceMode" */
546 $smarty->assign("forceMode", "");
548 /* Checkboxes */
549 if ($this->force_ids == 1){
550 $smarty->assign("force_ids", "checked");
551 if (session::get('js')){
552 $smarty->assign("forceMode", "");
553 }
554 } else {
555 if (session::get('js')){
556 $smarty->assign("forceMode", "disabled");
557 }
558 $smarty->assign("force_ids", "");
559 }
561 /* Create onClick="" action string for the "Force UID/GID" option
562 */
563 $onClickIDS ="";
564 if(preg_match("/w/",$this->getacl("uidNumber",$SkipWrite))){
565 $onClickIDS .= "changeState('uidNumber');";
566 }
567 if(preg_match("/w/",$this->getacl("gidNumber",$SkipWrite))){
568 $onClickIDS .= "changeState('gidNumber');";
569 }
570 $smarty->assign("onClickIDS", $onClickIDS);
571 $smarty->assign("force_idsACL", $this->getacl("uidNumber",$SkipWrite).$this->getacl("gidNumber",$SkipWrite));
573 foreach(array("primaryGroup","trustmode","activate_shadowWarning","activate_shadowInactive","activate_shadowMin","activate_shadowMax","activate_shadowExpire","mustchangepassword") 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 }
579 }
582 /* Load attributes and acl's */
583 foreach($this->attributes as $val){
584 if(in_array($val,$this->multi_boxes)){
585 $smarty->assign("use_".$val,TRUE);
586 }else{
587 $smarty->assign("use_".$val,FALSE);
588 }
590 if((session::get("js"))&&(($val=="uidNumber")||($val=="gidNumber")))
591 {
592 $smarty->assign("$val"."ACL",$this->getacl($val,$SkipWrite));
593 $smarty->assign("$val", $this->$val);
594 continue;
595 }
596 $smarty->assign("$val", $this->$val);
597 $smarty->assign("$val"."ACL", $this->getacl($val,$SkipWrite));
598 }
599 if($SkipWrite){
600 $smarty->assign("groupMembershipACL","r");
601 }else{
602 $smarty->assign("groupMembershipACL","rw");
603 }
604 $smarty->assign("status", $this->status);
606 /* Work on trust modes */
607 $smarty->assign("trusthide", " disabled ");
608 $smarty->assign("trustmodeACL", $this->getacl("trustModel",$SkipWrite));
609 if ($this->trustModel == "fullaccess"){
610 $trustmode= 1;
611 // pervent double disable tag in html code, this will disturb our clean w3c html
612 $smarty->assign("trustmode", $this->getacl("trustModel",$SkipWrite));
614 } elseif ($this->trustModel == "byhost"){
615 $trustmode= 2;
616 $smarty->assign("trusthide", "");
617 } else {
618 // pervent double disable tag in html code, this will disturb our clean w3c html
619 $smarty->assign("trustmode", $this->getacl("trustModel",$SkipWrite));
620 $trustmode= 0;
621 }
622 $smarty->assign("trustmode", $trustmode);
623 $smarty->assign("trustmodes", array( 0 => _("disabled"), 1 => _("full access"),
624 2 => _("allow access to these hosts")));
628 if((count($this->accessTo))==0)
629 $smarty->assign("emptyArrAccess",true);
630 else
631 $smarty->assign("emptyArrAccess",false);
633 if($this->mustchangepassword){
634 $smarty->assign("mustchangepassword", " checked ");
635 } else {
636 $smarty->assign("mustchangepassword", "");
637 }
639 $smarty->assign("workstations", $this->accessTo);
641 $smarty->assign("apply", apply_filter());
642 $smarty->assign("multiple_support" , $this->multiple_support_active);
643 $display.= $smarty->fetch (get_template_path('generic.tpl', TRUE, dirname(__FILE__)));
644 return($display);
645 }
648 /* remove object from parent */
649 function remove_from_parent()
650 {
651 /* Cancel if there's nothing to do here */
652 if ((!$this->initially_was_account) || (!$this->acl_is_removeable())){
653 return;
654 }
657 /* Remove and write to LDAP */
658 plugin::remove_from_parent();
660 /* Zero out array */
661 $this->attrs['gosaHostACL']= array();
663 /* Keep uid, because we need it for authentification! */
664 unset($this->attrs['uid']);
665 unset($this->attrs['trustModel']);
667 @DEBUG (DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__,
668 /* include global link_info */
669 $this->attributes, "Save");
670 $ldap= $this->config->get_ldap_link();
671 $ldap->cd($this->dn);
672 $this->cleanup();
673 $ldap->modify ($this->attrs);
675 new log("remove","users/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
677 if (!$ldap->success()){
678 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_DEL, get_class()));
679 }
681 /* Delete group only if cn is uid and there are no other
682 members inside */
683 $ldap->cd ($this->config->current['BASE']);
684 $ldap->search ("(&(objectClass=posixGroup)(gidNumber=".$this->gidNumber."))", array("cn", "memberUid"));
685 if ($ldap->count() != 0){
686 $attrs= $ldap->fetch();
687 if ($attrs['cn'][0] == $this->uid &&
688 !isset($this->attrs['memberUid'])){
690 $ldap->rmDir($ldap->getDN());
691 }
692 }
694 /* Optionally execute a command after we're done */
695 $this->handle_post_events("remove",array("uid" => $this->uid));
696 }
699 function save_object()
700 {
701 if (isset($_POST['posixTab'])){
702 /* Save values to object */
703 plugin::save_object();
706 /* Save force GID checkbox */
707 if($this->acl_is_writeable("gidNumber") || $this->acl_is_writeable("uidNumber")){
708 if (isset ($_POST['force_ids'])){
709 $data= 1;
710 } else {
711 $data= 0;
712 }
713 if ($this->force_ids != $data){
714 $this->is_modified= TRUE;
715 }
716 $this->force_ids= $data;
717 }
719 /*Save primary group settings */
720 if($this->acl_is_writeable("primaryGroup") && isset($_POST['primaryGroup'])){
721 $data= $_POST['primaryGroup'];
722 if ($this->primaryGroup != $data){
723 $this->is_modified= TRUE;
724 }
725 $this->primaryGroup= $_POST['primaryGroup'];
726 }
728 /* Get seelcted shadow checkboxes */
729 foreach(array("shadowMin","shadowMax","shadowExpire","shadowInactive","shadowWarning") as $var) {
730 if($this->acl_is_writeable($var)){
731 $activate_var = "activate_".$var;
732 if(isset($_POST['activate_'.$var])){
733 $this->$activate_var = true;
734 $this->$var = $_POST[$var];
735 }else{
736 $this->$activate_var = false;
737 $this->$var = 0;
738 }
739 }
740 }
742 /* Force change password ? */
743 if(isset($_POST['mustchangepassword'])){
744 $this->mustchangepassword = TRUE;
745 }else{
746 $this->mustchangepassword = FALSE;
747 }
749 /* Trust mode - special handling */
750 if($this->acl_is_writeable("trustModel")){
751 if (isset($_POST['trustmode'])){
752 $saved= $this->trustModel;
753 if ($_POST['trustmode'] == "1"){
754 $this->trustModel= "fullaccess";
755 } elseif ($_POST['trustmode'] == "2"){
756 $this->trustModel= "byhost";
757 } else {
758 $this->trustModel= "";
759 }
760 if ($this->trustModel != $saved){
761 $this->is_modified= TRUE;
762 }
763 }
764 }
765 }
767 /* Get regex from alphabet */
768 if(isset($_GET['search'])){
769 $this->GroupRegex = $_GET['search']."*";
770 }
772 /* Check checkboxes and regexes */
773 if(isset($_POST["PosixGroupDialogPosted"])){
775 if(isset($_POST['SubSearch']) && ($_POST['SubSearch'])){
776 $this->SubSearch = true;
777 }else{
778 $this->SubSearch = false;
779 }
780 if(isset($_POST['guser'])){
781 $this->GroupUserRegex = $_POST['guser'];
782 }
783 if(isset($_POST['regex'])){
784 $this->GroupRegex = $_POST['regex'];
785 }
786 }
787 $this->GroupRegex = preg_replace("/\*\**/","*",$this->GroupRegex);
788 $this->GroupUserRegex = preg_replace("/\*\**/","*",$this->GroupUserRegex);
789 }
792 /* Save data to LDAP, depending on is_account we save or delete */
793 function save()
794 {
796 /* Adapt shadow values */
797 if (!$this->activate_shadowExpire){
798 $this->shadowExpire= "0";
799 } else {
800 /* Transform seconds to days here */
801 $this->shadowExpire= (int)($this->shadowExpire / (60 * 60 * 24)) ;
802 }
803 if (!$this->activate_shadowMax){
804 $this->shadowMax= "0";
805 }
806 if ($this->mustchangepassword){
807 $this->shadowLastChange= (int)(date("U") / 86400) - $this->shadowMax - 1;
808 } else {
809 $this->shadowLastChange= (int)(date("U") / 86400);
810 }
811 if (!$this->activate_shadowWarning){
812 $this->shadowWarning= "0";
813 }
815 /* Check what to do with ID's */
816 if ($this->force_ids == 0){
818 /* Use id's that are already set */
819 if ($this->savedUidNumber != ""){
820 $this->uidNumber= $this->savedUidNumber;
821 $this->gidNumber= $this->savedGidNumber;
822 } else {
824 /* Calculate new id's. We need to place a lock before calling get_next_id
825 to get real unique values. */
826 $wait= 10;
827 while (get_lock("uidnumber") != ""){
828 sleep (1);
830 /* Oups - timed out */
831 if ($wait-- == 0){
832 msg_dialog::display(_("Warning"), _("Timeout while waiting for lock. Ignoring lock!"), WARNING_DIALOG);
833 break;
834 }
835 }
837 add_lock ("uidnumber", "gosa");
838 $this->uidNumber= $this->get_next_id("uidNumber", $this->dn);
839 if ($this->savedGidNumber != ""){
840 $this->gidNumber= $this->savedGidNumber;
841 } else {
842 $this->gidNumber= $this->get_next_id("gidNumber", $this->dn);
843 }
844 }
846 if ($this->primaryGroup != 0){
847 $this->gidNumber= $this->primaryGroup;
848 }
849 }
851 if ($this->activate_shadowMin != "1" ) {
852 $this->shadowMin = "";
853 }
855 if (($this->activate_shadowMax != "1") && ($this->mustchangepassword != "1")) {
856 $this->shadowMax = "";
857 }
859 if ($this->activate_shadowWarning != "1" ) {
860 $this->shadowWarning = "";
861 }
863 if ($this->activate_shadowInactive != "1" ) {
864 $this->shadowInactive = "";
865 }
867 if ($this->activate_shadowExpire != "1" ) {
868 $this->shadowExpire = "";
869 }
871 /* Fill gecos */
872 if (isset($this->parent) && $this->parent !== NULL){
873 $this->gecos= rewrite($this->parent->by_object['user']->cn);
874 if (!preg_match('/^[a-z0-9 -]+$/i', $this->gecos)){
875 $this->gecos= "";
876 }
877 }
879 foreach(array("shadowMin","shadowMax","shadowWarning","shadowInactive","shadowExpire") as $attr){
880 $this->$attr = (int) $this->$attr;
881 }
882 /* Call parents save to prepare $this->attrs */
883 plugin::save();
885 /* Trust accounts */
886 $objectclasses= array();
887 foreach ($this->attrs['objectClass'] as $key => $class){
888 if (preg_match('/trustAccount/i', $class)){
889 continue;
890 }
891 $objectclasses[]= $this->attrs['objectClass'][$key];
892 }
893 $this->attrs['objectClass']= $objectclasses;
894 if ($this->trustModel != ""){
895 $this->attrs['objectClass'][]= "trustAccount";
896 $this->attrs['trustModel']= $this->trustModel;
897 $this->attrs['accessTo']= array();
898 if ($this->trustModel == "byhost"){
899 foreach ($this->accessTo as $host){
900 $this->attrs['accessTo'][]= $host;
901 }
902 }
903 } else {
904 if ($this->was_trust_account){
905 $this->attrs['accessTo']= array();
906 $this->attrs['trustModel']= array();
907 }
908 }
910 if(empty($this->attrs['gosaDefaultPrinter'])){
911 $thid->attrs['gosaDefaultPrinter']=array();
912 }
915 /* include global link_info */
916 $this->cleanup();
918 /* This is just a test, we have had duplicated ids
919 in the past when copy & paste was used.
920 Normaly this should not happen.
921 */
922 if(isset($this->attrs['uidNumber']) && !$this->force_ids){
923 $used = $this->get_used_uid_numbers();
924 if(isset($used[$this->attrs['uidNumber']]) && $used[$this->attrs['uidNumber']] != $this->dn){
925 msg_dialog::display(_("Warning"),_("A duplicated UID number was written for this user. If this was not intended please verify all used uidNumbers!"), WARNING_DIALOG);
926 }
927 }
929 $ldap= $this->config->get_ldap_link();
930 $ldap->cd($this->dn);
931 unset($this->attrs['uid']);
932 $ldap->modify ($this->attrs);
934 /* Log last action */
935 if($this->initially_was_account){
936 new log("modify","users/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
937 }else{
938 new log("create","users/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
939 }
941 if (!$ldap->success()){
942 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_MOD, get_class()));
943 }
945 /* Remove lock needed for unique id generation */
946 del_lock ("uidnumber");
948 /* Posix accounts have group interrelationship,
949 take care about these here if this is a new user without forced gidNumber. */
950 if ($this->force_ids == 0 && $this->primaryGroup == 0){
951 $ldap->cd($this->config->current['BASE']);
952 $ldap->search("(&(objectClass=posixGroup)(gidNumber=".$this->gidNumber."))", array("cn"));
954 /* Create group if it doesn't exist */
955 if ($ldap->count() == 0){
956 $groupdn= preg_replace ('/^'.$this->config->get_cfg_value("accountPrimaryAttribute").'=[^,]+,'.get_people_ou().'/i', 'cn='.$this->uid.','.get_groups_ou(), $this->dn);
958 $g= new group($this->config, $groupdn);
959 $g->cn= $this->uid;
960 $g->force_gid= 1;
961 $g->gidNumber= $this->gidNumber;
962 $g->description= _("Group of user")." ".$this->givenName." ".$this->sn;
963 $g->save ();
965 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
966 sprintf("Primary group '%s' created, using gidNumber '%s'.",$this->uid,$this->gidNumber),"");
967 }else{
968 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
969 sprintf("Primary group '%s' exists.",$this->uid),"");
970 }
971 }
973 /* Take care about groupMembership values: add to groups */
974 foreach ($this->groupMembership as $key => $value){
975 if (!isset($this->savedGroupMembership[$key])){
976 $g= new grouptabs($this->config,$this->config->data['TABS']['GROUPTABS'], $key,"groups");
977 $g->set_acl_base($key);
978 $g->by_object['group']->addUser($this->uid);
979 $g->save();
980 }
981 }
983 /* Remove from groups not listed in groupMembership */
984 foreach ($this->savedGroupMembership as $key => $value){
985 if (!isset($this->groupMembership[$key])){
986 $g= new grouptabs($this->config,$this->config->data['TABS']['GROUPTABS'], $key,"groups");
987 $g->set_acl_base($key);
988 $g->by_object['group']->removeUser ($this->uid);
989 $g->save();
990 }
991 }
993 /* Optionally execute a command after we're done */
994 if ($this->initially_was_account == $this->is_account){
995 if ($this->is_modified){
996 $this->handle_post_events("modify",array("uid" => $this->uid));
997 }
998 } else {
999 $this->handle_post_events("add" ,array("uid"=> $this->uid));
1000 }
1001 }
1003 /* Check formular input */
1004 function check()
1005 {
1006 /* Include global link_info */
1007 $ldap= $this->config->get_ldap_link();
1009 /* Append groups as memberGroup: to check hook
1010 */
1011 $tmp_attributes = $this->attributes;
1012 $this->attributes[] = "memberGroup";
1013 $this->memberGroup = array();
1014 foreach($this->groupMembership as $dn => $name){
1015 $this->memberGroup[] = $name;
1016 }
1018 /* Call common method to give check the hook */
1019 $message= plugin::check();
1020 $this->attributes = $tmp_attributes;
1022 /* must: homeDirectory */
1023 if ($this->homeDirectory == ""){
1024 $message[]= msgPool::required(_("Home directory"));
1025 }
1026 if (!tests::is_path($this->homeDirectory)){
1027 $message[]= msgPool::invalid(_("Home directory"), "", "", "/home/yourname" );
1028 }
1030 /* Check ID's if they are forced by user */
1031 if ($this->force_ids == "1"){
1033 /* Valid uid/gid? */
1034 if (!tests::is_id($this->uidNumber)){
1035 $message[]= msgPool::invalid(_("UID"), $this->uidNumber, "/[0-9]/");
1036 } else {
1037 if ($this->uidNumber < $this->config->get_cfg_value("minId")){
1038 $message[]= msgPool::toosmall(_("UID"), $this->config->get_cfg_value("minId"));
1039 }
1040 }
1041 if (!tests::is_id($this->gidNumber)){
1042 $message[]= msgPool::invalid(_("GID"), $this->gidNumber, "/[0-9]/");
1043 } else {
1044 if ($this->gidNumber < $this->config->get_cfg_value("minId")){
1045 $message[]= msgPool::toosmall(_("GID"), $this->config->get_cfg_value("minId"));
1046 }
1047 }
1048 }
1050 /* Check shadow settings, well I like spaghetties... */
1051 if ($this->activate_shadowMin){
1052 if (!tests::is_id($this->shadowMin)){
1053 $message[]= msgPool::invalid(_("shadowMin"), $this->shadowMin, "/[0-9]/");
1054 }
1055 }
1056 if ($this->activate_shadowMax){
1057 if (!tests::is_id($this->shadowMax)){
1058 $message[]= msgPool::invalid(_("shadowMax"), $this->shadowMax, "/[0-9]/");
1059 }
1060 }
1061 if ($this->activate_shadowWarning){
1062 if (!tests::is_id($this->shadowWarning)){
1063 $message[]= msgPool::invalid(_("shadowWarning"), $this->shadowWarning, "/[0-9]/");
1064 }
1065 if (!$this->activate_shadowMax){
1066 $message[]= msgPool::depends("shadowWarning", "shadowMax");
1067 }
1068 if ($this->shadowWarning > $this->shadowMax){
1069 $message[]= msgPool::toobig("shadowWarning", "shadowMax");
1070 }
1071 if ($this->activate_shadowMin && $this->shadowWarning < $this->shadowMin){
1072 $message[]= msgPool::toosmall("shadowWarning", "shadowMin");
1073 }
1074 }
1075 if ($this->activate_shadowInactive){
1076 if (!tests::is_id($this->shadowInactive)){
1077 $message[]= msgPool::invalid(_("shadowInactive"), $this->shadowInactive, "/[0-9]/");
1078 }
1079 if (!$this->activate_shadowMax){
1080 $message[]= msgPool::depends("shadowInactive", "shadowMax");
1081 }
1082 }
1083 if ($this->activate_shadowMin && $this->activate_shadowMax){
1084 if ($this->shadowMin > $this->shadowMax){
1085 $message[]= msgPool::toobig("shadowMin", "shadowMax");
1086 }
1087 }
1089 return ($message);
1090 }
1093 function multiple_check()
1094 {
1095 $message = plugin::multiple_check();
1096 if ($this->homeDirectory == "" && in_array("homeDirectory",$this->multi_boxes)){
1097 $message[]= msgPool::required(_("Home directory"));
1098 }
1099 if (!tests::is_path($this->homeDirectory) && in_array("homeDirectory",$this->multi_boxes)){
1100 $message[]= msgPool::invalid(_("Home directory"), "", "", "/home/yourname" );
1101 }
1103 /* Check shadow settings, well I like spaghetties... */
1104 if ($this->activate_shadowMin && in_array("activate_shadowMin",$this->multi_boxes)){
1105 if (!tests::is_id($this->shadowMin)){
1106 $message[]= msgPool::invalid(_("shadowMin"), $this->shadowMin, "/[0-9]/");
1107 }
1108 }
1109 if ($this->activate_shadowMax && in_array("activate_shadowMax",$this->multi_boxes)){
1110 if (!tests::is_id($this->shadowMax)){
1111 $message[]= msgPool::invalid(_("shadowMax"), $this->shadowMax, "/[0-9]/");
1112 }
1113 }
1114 if ($this->activate_shadowWarning && in_array("activate_shadowWarning",$this->multi_boxes)){
1115 if (!tests::is_id($this->shadowWarning)){
1116 $message[]= msgPool::invalid(_("shadowWarning"), $this->shadowWarning, "/[0-9]/");
1117 }
1118 if (!$this->activate_shadowMax && in_array("activate_shadowMax",$this->multi_boxes)){
1119 $message[]= msgPool::depends("shadowWarning", "shadowMax");
1120 }
1121 if ($this->shadowWarning > $this->shadowMax && in_array("activate_shadowWarning",$this->multi_boxes)){
1122 $message[]= msgPool::toobig("shadowWarning", "shadowMax");
1123 }
1124 if ($this->activate_shadowMin && $this->shadowWarning < $this->shadowMin && in_array("activate_shadowMin",$this->multi_boxes)){
1125 $message[]= msgPool::tosmall("shadowWarning", "shadowMin");
1126 }
1127 }
1128 if ($this->activate_shadowInactive && in_array("activate_shadowInactive",$this->multi_boxes)){
1129 if (!tests::is_id($this->shadowInactive)){
1130 $message[]= msgPool::invalid(_("shadowInactive"), $this->shadowInactive, "/[0-9]/");
1131 }
1132 if (!$this->activate_shadowMax && in_array("activate_shadowMax",$this->multi_boxes)){
1133 $message[]= msgPool::depends("shadowInactive", "shadowMax");
1134 }
1135 }
1136 if ($this->activate_shadowMin && $this->activate_shadowMax && in_array("activate_shadowMin",$this->multi_boxes)){
1137 if ($this->shadowMin > $this->shadowMax){
1138 $message[]= msgPool::toobig("shadowMin", "shadowMax");
1139 }
1140 }
1142 return($message);
1143 }
1146 function addGroup ($groups)
1147 {
1148 /* include global link_info */
1149 $ldap= $this->config->get_ldap_link();
1151 /* Walk through groups and add the descriptive entry if not exists */
1152 foreach ($groups as $value){
1154 if (!array_key_exists($value, $this->groupMembership)){
1155 $ldap->cat($value, array('cn', 'description', 'dn'));
1156 $attrs= $ldap->fetch();
1157 error_reporting (0);
1158 if (!isset($attrs['description'][0])){
1159 $entry= $attrs["cn"][0];
1160 } else {
1161 $dsc= preg_replace ('/^Group of user/', _("Group of user"), $attrs["description"][0]);
1162 $entry= $attrs["cn"][0]." [$dsc]";
1163 }
1164 error_reporting (E_ALL | E_STRICT);
1166 if(obj_is_writable($attrs['dn'],"groups/group","memberUid")){
1167 $this->groupMembership[$attrs['dn']]= $entry;
1168 if($this->multiple_support_active && isset($this->groupMembership_some[$attrs['dn']])){
1169 unset($this->groupMembership_some[$attrs['dn']]);
1170 }
1171 }
1172 }
1173 }
1175 /* Sort groups */
1176 asort ($this->groupMembership);
1177 reset ($this->groupMembership);
1178 }
1181 /* Del posix user from some groups */
1182 function delGroup ($groups)
1183 {
1184 $dest= array();
1185 foreach($groups as $dn_to_del){
1186 if(isset($this->groupMembership[$dn_to_del]) && obj_is_writable($dn_to_del,"groups/group","memberUid")){
1187 unset($this->groupMembership[$dn_to_del]);
1188 }
1189 if($this->multiple_support_active){
1190 if(isset($this->groupMembership_some[$dn_to_del]) && obj_is_writable($dn_to_del,"groups/group","memberUid")){
1191 unset($this->groupMembership_some[$dn_to_del]);
1192 }
1193 }
1194 }
1195 }
1198 /* Adapt from template, using 'dn' */
1199 function adapt_from_template($dn, $skip= array())
1200 {
1201 /* Include global link_info */
1202 $ldap= $this->config->get_ldap_link();
1204 plugin::adapt_from_template($dn, $skip);
1205 $template= $this->attrs['uid'][0];
1207 /* Adapt group membership */
1208 $ldap->cd($this->config->current['BASE']);
1209 $ldap->search("(&(objectClass=posixGroup)(memberUid=".$this->attrs["uid"][0]."))", array("description", "cn"));
1211 while ($this->attrs= $ldap->fetch()){
1212 if (!isset($this->attrs["description"][0])){
1213 $entry= $this->attrs["cn"][0];
1214 } else {
1215 $entry= $this->attrs["cn"][0]." [".$this->attrs["description"][0]."]";
1216 }
1217 $this->groupMembership[$ldap->getDN()]= $entry;
1218 }
1220 /* Fix primary group settings */
1221 $ldap->cd($this->config->current['BASE']);
1222 $ldap->search("(&(objectClass=posixGroup)(cn=$template)(gidNumber=".$this->gidNumber."))", array("cn"));
1223 if ($ldap->count() != 1){
1224 $this->primaryGroup= $this->gidNumber;
1225 }
1227 $ldap->cd($this->config->current['BASE']);
1228 $ldap->search("(&(objectClass=gosaUserTemplate)(uid=".$template.")(accessTo=*))", array("cn","accessTo"));
1229 while($attr = $ldap->fetch()){
1230 $tmp = $attr['accessTo'];
1231 unset ($tmp['count']);
1232 $this->accessTo = $tmp;
1233 }
1235 /* Adjust shadow checkboxes */
1236 foreach (array("shadowMin", "shadowMax", "shadowWarning", "shadowInactive") as $val){
1237 if ($this->$val != 0){
1238 $oval= "activate_".$val;
1239 $this->$oval= "1";
1240 }
1241 }
1243 /* FIXME: NEED review of this section */
1244 /* Need to check shadowExpire separately */
1246 /*
1247 * If shadowExpire is not enabled in the template, it's a UNIX timestamp - so don't convert it to seconds.
1248 * The check is a hack - if difference between timestamp generated above and here is max 1 day.
1249 */
1250 if(abs($this->shadowExpire - time())>86400) {
1251 $this->shadowExpire= $this->convertToSeconds($this->shadowExpire);
1252 }
1254 /* Only enable checkbox, if shadowExpire is in the future */
1255 if($this->shadowExpire > time()) {
1256 $this->activate_shadowExpire= "1";
1257 }
1258 }
1260 function convertToSeconds($val)
1261 {
1262 if ($val != 0){
1263 $val*= 60 * 60 * 24;
1264 } else {
1265 $date= getdate();
1266 $val= floor($date[0] / (60*60*24)) * 60 * 60 * 24;
1267 }
1268 return($val);
1269 }
1272 function get_used_uid_numbers()
1273 {
1274 $ids= array();
1275 $ldap= $this->config->get_ldap_link();
1277 $ldap->cd ($this->config->current['BASE']);
1278 $ldap->search ("(&(objectClass=posixAccount)(uidNumber=*))", array("uidNumber"));
1280 /* Get list of ids */
1281 while ($attrs= $ldap->fetch()){
1282 $ids[$attrs['uidNumber'][0]] = $attrs['dn'];
1283 }
1284 return($ids);
1285 }
1288 function get_next_id($attrib, $dn)
1289 {
1290 $ids= array();
1291 $ldap= $this->config->get_ldap_link();
1293 $ldap->cd ($this->config->current['BASE']);
1294 if (preg_match('/gidNumber/i', $attrib)){
1295 $oc= "posixGroup";
1296 } else {
1297 $oc= "posixAccount";
1298 }
1299 $ldap->search ("(&(objectClass=$oc)($attrib=*))", array("$attrib"));
1301 /* Get list of ids */
1302 while ($attrs= $ldap->fetch()){
1303 $ids[]= (int)$attrs["$attrib"][0];
1304 }
1306 /* Add the nobody id */
1307 $ids[]= 65534;
1309 /* get the ranges */
1310 $tmp = array('0'=> 1000);
1311 if (preg_match('/posixAccount/', $oc) && $this->config->get_cfg_value("uidNumberBase") != ""){
1312 $tmp= split('-',$this->config->get_cfg_value("uidNumberBase"));
1313 } elseif($this->config->get_cfg_value("gidNumberBase") != ""){
1314 $tmp= split('-',$this->config->get_cfg_value("gidNumberBase"));
1315 }
1317 /* Set hwm to max if not set - for backward compatibility */
1318 $lwm= $tmp[0];
1319 if (isset($tmp[1])){
1320 $hwm= $tmp[1];
1321 } else {
1322 $hwm= pow(2,32);
1323 }
1325 /* Find out next free id near to UID_BASE */
1326 if ($this->config->get_cfg_value("baseIdHook") == ""){
1327 $base= $lwm;
1328 } else {
1329 /* Call base hook */
1330 $base= get_base_from_hook($dn, $attrib);
1331 }
1332 for ($id= $base; $id++; $id < pow(2,32)){
1333 if (!in_array($id, $ids)){
1334 return ($id);
1335 }
1336 }
1338 /* Should not happen */
1339 if ($id == $hwm){
1340 msg_dialog::display(_("Error"), _("Cannot allocate a free ID!"), ERROR_DIALOG);
1341 exit;
1342 }
1344 }
1346 function reload()
1347 {
1348 /* Set base for all searches */
1349 $base = session::get('CurrentMainBase');
1350 $base = $base;
1351 $ldap = $this->config->get_ldap_link();
1352 $attrs = array("cn", "description", "gidNumber");
1353 $Flags = GL_SIZELIMIT;
1355 /* Get groups */
1356 if ($this->GroupUserRegex == '*'){
1357 $filter = "(&(objectClass=posixGroup)(cn=".$this->GroupRegex."))";
1358 } else {
1359 $filter= "(&(objectClass=posixGroup)(cn=".$this->GroupRegex.")(memberUid=".$this->GroupUserRegex."))";
1360 }
1361 if($this->SubSearch){
1362 $Flags |= GL_SUBSEARCH;
1363 }else{
1364 $base = get_groups_ou().$base;
1365 }
1367 $res= get_list($filter, "groups", $base,$attrs, $Flags);
1369 /* check sizelimit */
1370 if (preg_match("/size limit/i", $ldap->get_error())){
1371 session::set('limit_exceeded',TRUE);
1372 }
1374 /* Create a list of users */
1375 $this->grouplist = array();
1376 foreach ($res as $value){
1377 $this->grouplist[$value['gidNumber'][0]]= $value;
1378 }
1380 $tmp=array();
1381 foreach($this->grouplist as $tkey => $val ){
1382 $tmp[strtolower($val['cn'][0]).$val['cn'][0]]=$val;
1383 }
1385 /* Sort index */
1386 ksort($tmp);
1388 /* Recreate index array[dn]=cn[description]*/
1389 $this->grouplist=array();
1390 foreach($tmp as $val){
1391 if(isset($val['description'])){
1392 $this->grouplist[$val['dn']]=$val['cn'][0]." [".$val['description'][0]."]";
1393 }else{
1394 $this->grouplist[$val['dn']]=$val['cn'][0];
1395 }
1396 }
1398 reset ($this->grouplist);
1399 }
1402 /* Get posts from copy & paste dialog */
1403 function saveCopyDialog()
1404 {
1405 if(isset($_POST['homeDirectory'])){
1406 $this->homeDirectory = $_POST['homeDirectory'];
1407 if (isset ($_POST['force_ids'])){
1408 $data= 1;
1409 $this->gidNumber = $_POST['gidNumber'];
1410 $this->uidNumber = $_POST['uidNumber'];
1411 } else {
1412 $data= 0;
1413 }
1414 if ($this->force_ids != $data){
1415 $this->is_modified= TRUE;
1416 }
1417 $this->force_ids= $data;
1418 }
1419 }
1422 /* Create the posix dialog part for copy & paste */
1423 function getCopyDialog()
1424 {
1425 /* Skip dialog creation if this is not a valid account*/
1426 if(!$this->is_account) return("");
1427 if ($this->force_ids == 1){
1428 $force_ids = "checked";
1429 if (session::get('js')){
1430 $forceMode = "";
1431 }
1432 } else {
1433 if (session::get('js')){
1434 if($this->acl != "#none#")
1435 $forceMode ="disabled";
1436 }
1437 $force_ids = "";
1438 }
1440 $sta = "";
1442 /* Open group add dialog */
1443 if(isset($_POST['edit_groupmembership'])){
1444 $this->group_dialog = TRUE;
1445 $sta = "SubDialog";
1446 }
1448 /* If the group-add dialog is closed, call execute
1449 to ensure that the membership is updatd */
1450 if(isset($_POST['add_groups_finish']) || isset($_POST['add_groups_cancel'])){
1451 $this->execute();
1452 $this->group_dialog =FALSE;
1453 }
1455 if($this->group_dialog){
1456 $str = $this->execute(true);
1457 $ret = array();
1458 $ret['string'] = $str;
1459 $ret['status'] = $sta;
1460 return($ret);
1461 }
1463 /* If a group member should be deleted, simply call execute */
1464 if(isset($_POST['delete_groupmembership'])){
1465 $this->execute();
1466 }
1468 /* Assigned informations to smarty */
1469 $smarty = get_smarty();
1470 $smarty->assign("homeDirectory",$this->homeDirectory);
1471 $smarty->assign("uidNumber",$this->uidNumber);
1472 $smarty->assign("gidNumber",$this->gidNumber);
1473 $smarty->assign("forceMode",$forceMode);
1474 $smarty->assign("force_ids",$force_ids);
1475 if (!count($this->groupMembership)){
1476 $smarty->assign("groupMembership", array(" "));
1477 } else {
1478 $smarty->assign("groupMembership", $this->groupMembership);
1479 }
1481 /* Display wars message if there are more than 16 group members */
1482 if (count($this->groupMembership) > 16){
1483 $smarty->assign("groups", "too_many_for_nfs");
1484 } else {
1485 $smarty->assign("groups", "");
1486 }
1487 $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE,dirname(__FILE__)));
1489 $ret = array();
1490 $ret['string'] = $str;
1491 $ret['status'] = $sta;
1492 return($ret);
1493 }
1496 function PrepareForCopyPaste($source)
1497 {
1498 plugin::PrepareForCopyPaste($source);
1500 /* Avoid using the same gid/uid number as source user
1501 empty numbers to enforce new ones. */
1502 # $this->savedUidNumber = $this->get_next_id("uidNumber", $this->dn);
1503 $this->savedUidNumber = "";
1504 # $this->savedGidNumber = $this->get_next_id("gidNumber", $this->dn);
1505 $this->savedGidNumber = "";
1506 }
1509 function multiple_execute()
1510 {
1511 return($this->execute());
1512 }
1515 static function plInfo()
1516 {
1517 return (array(
1518 "plDescription" => _("POSIX account"),
1519 "plSelfModify" => TRUE,
1520 "plDepends" => array("user"),
1521 "plPriority" => 2,
1522 "plSection" => array("personal" => _("My account")),
1523 "plCategory" => array("users"),
1524 "plOptions" => array(),
1526 "plProvidedAcls" => array(
1528 "homeDirectory" => _("Home directory"),
1529 "loginShell" => _("Shell"),
1530 "uidNumber" => _("User ID"),
1531 "gidNumber" => _("Group ID"),
1533 "mustchangepassword"=> _("Force password change on login"),
1534 "shadowMin" => _("Shadow min"),
1535 "shadowMax" => _("Shadow max"),
1536 "shadowWarning" => _("Shadow warning"),
1537 "shadowInactive" => _("Shadow inactive"),
1538 "shadowExpire" => _("Shadow expire"),
1539 "trustModel" => _("System trust model")))
1540 );
1541 }
1544 /* Return selected values for multiple edit */
1545 function get_multi_edit_values()
1546 {
1547 $ret = plugin::get_multi_edit_values();
1548 $ret['groupMembership'] = $this->groupMembership;
1549 $ret['groupMembership_some']= $this->groupMembership_some;
1551 if(in_array("primaryGroup",$this->multi_boxes)){
1552 $ret['primaryGroup'] = $this->primaryGroup;
1553 }
1554 if(in_array("trustmode",$this->multi_boxes)){
1555 $ret['trustModel'] = $this->trustModel;
1556 $ret['accessTo'] = $this->accessTo;
1557 }
1558 foreach(array("shadowWarning","shadowInactive","shadowMin","shadowMax", "shadowExpire") as $entry){
1559 $active = "activate_".$entry;
1560 if(in_array($active,$this->multi_boxes)){
1561 $ret[$entry] = $this->$entry;
1562 $ret[$active] = $this->$active;
1563 }
1564 }
1565 if(in_array("mustchangepassword",$this->multi_boxes)){
1566 $ret['mustchangepassword'] = $this->mustchangepassword;
1567 }
1568 return($ret);
1569 }
1572 /* Save posts for multiple edit
1573 */
1574 function multiple_save_object()
1575 {
1576 if(isset($_POST['posix_mulitple_edit'])){
1578 /* Backup expire value */
1579 $expire_tmp = $this->shadowExpire;
1581 /* Update all values */
1582 plugin::multiple_save_object();
1584 /* Get selected checkboxes */
1585 foreach(array("primaryGroup","trustmode","mustchangepassword","activate_shadowWarning","activate_shadowInactive","activate_shadowMin", "activate_shadowMax","activate_shadowExpire") as $val){
1586 if(isset($_POST["use_".$val])){
1587 $this->multi_boxes[] = $val;
1588 }
1589 }
1591 /* Update special values, checkboxes for posixShadow */
1592 foreach(array("shadowMin","shadowMax","shadowExpire","shadowInactive","shadowWarning") as $var) {
1593 if($this->acl_is_writeable($var)){
1594 $activate_var = "activate_".$var;
1595 if(in_array($activate_var, $this->multi_boxes)){
1596 if(isset($_POST['activate_'.$var])){
1597 $this->$activate_var = true;
1598 $this->$var = $_POST[$var];
1599 }else{
1600 $this->$activate_var = false;
1601 $this->$var = 0;
1602 }
1603 }
1604 }
1605 }
1607 /* Restore shadow value, if the shadow attribute isn't used */
1608 if(!in_array("activate_shadowExpire",$this->multi_boxes)){
1609 $this->shadowExpire = $expire_tmp;
1610 }
1612 /* Force change password ? */
1613 if(isset($_POST['mustchangepassword'])){
1614 $this->mustchangepassword = TRUE;
1615 }else{
1616 $this->mustchangepassword = FALSE;
1617 }
1619 /* Trust mode - special handling */
1620 if($this->acl_is_writeable("trustModel")){
1621 if (isset($_POST['trustmode'])){
1622 $saved= $this->trustModel;
1623 if ($_POST['trustmode'] == "1"){
1624 $this->trustModel= "fullaccess";
1625 } elseif ($_POST['trustmode'] == "2"){
1626 $this->trustModel= "byhost";
1627 } else {
1628 $this->trustModel= "";
1629 }
1630 if ($this->trustModel != $saved){
1631 $this->is_modified= TRUE;
1632 }
1633 }
1634 }
1636 /* Save primary group settings */
1637 if($this->acl_is_writeable("primaryGroup") && isset($_POST['primaryGroup'])){
1638 $data= $_POST['primaryGroup'];
1639 if ($this->primaryGroup != $data){
1640 $this->is_modified= TRUE;
1641 }
1642 $this->primaryGroup= $_POST['primaryGroup'];
1643 }
1644 }
1645 }
1648 /* Initialize plugin with given atribute arrays
1649 */
1650 function init_multiple_support($attrs,$all)
1651 {
1652 plugin::init_multiple_support($attrs,$all);
1654 /* Some dummy values */
1655 $groups_some = array();
1656 $groups_all = array();
1657 $groups_uid = array();
1658 $uids = array();
1659 $first = TRUE;
1661 /* Get all groups used by currently edited users */
1662 $uid_filter="";
1663 for($i =0; $i < $this->multi_attrs_all['uid']['count'] ; $i ++){
1664 $uid = $this->multi_attrs_all['uid'][$i];
1665 $uids[] = $uid;
1666 $uid_filter.= "(memberUid=".$uid.")";
1667 }
1668 $uid_filter = "(&(objectClass=posixGroup)(|".$uid_filter."))";
1669 $ldap = $this->config->get_ldap_link();
1670 $ldap->cd($this->config->current['BASE']);
1671 $ldap->search($uid_filter,array("dn","cn","memberUid"));
1672 while($group = $ldap->fetch()){
1673 $groups_some[$group['dn']] = $group['cn'][0];
1674 for($i = 0 ; $i < $group['memberUid']['count'] ; $i++){
1675 $groups_uid[$group['dn']][] = $group['memberUid'][$i];
1676 }
1677 }
1679 /* Create an array, containing all used groups */
1680 $groups_all = $groups_some;
1681 foreach($groups_all as $id => $group){
1682 foreach($uids as $uid){
1683 if(!in_array($uid,$groups_uid[$id])){
1684 unset($groups_all[$id]);
1685 break;
1686 }
1687 }
1688 }
1690 /* Assign group array */
1691 $this->groupMembership = $groups_all;
1693 /* Create an array of all grouops used by all users */
1694 foreach( $groups_all as $dn => $cn){
1695 if(isset($groups_some[$dn])){
1696 unset($groups_some[$dn]);
1697 }
1698 }
1699 $this->groupMembership_some = $groups_some;
1700 $this->primaryGroup = $this->gidNumber;
1702 /* Is this account a trustAccount? */
1703 if (isset($this->multi_attrs['trustModel'])){
1704 $this->trustModel= $this->multi_attrs['trustModel'][0];
1705 $this->was_trust_account= TRUE;
1706 $this->multi_boxes[] = "trustmode";
1707 } else {
1708 $this->was_trust_account= FALSE;
1709 $this->trustModel= "";
1710 }
1712 /* Create access informations */
1713 $this->accessTo = array();
1714 if (isset($this->multi_attrs['accessTo'])){
1715 for ($i= 0; $i<$this->multi_attrs['accessTo']['count']; $i++){
1716 $tmp= $this->multi_attrs['accessTo'][$i];
1717 $this->accessTo[$tmp]= $tmp;
1718 }
1719 }
1721 /* Adjust shadow checkboxes */
1722 foreach (array("shadowMin", "shadowMax", "shadowWarning", "shadowInactive",
1723 "shadowExpire") as $val){
1724 if ($this->$val != 0){
1725 $oval= "activate_".$val;
1726 $this->$oval= "1";
1727 }
1728 }
1730 /* Convert to seconds */
1731 if(isset($this->multi_attrs['shadowExpire'])){
1732 $this->shadowExpire = $this->convertToSeconds($this->multi_attrs['shadowExpire'][0]);
1733 }else{
1734 $this->activate_shadowExpire = FALSE;
1735 }
1736 }
1739 function set_multi_edit_values($attrs)
1740 {
1741 $groups = array();
1743 /* Update groupMembership, keep optinal group */
1744 foreach($attrs['groupMembership_some'] as $dn => $cn){
1745 if(isset($this->groupMembership[$dn])){
1746 $groups[$dn] = $cn;
1747 }
1748 }
1749 /* Update groupMembership, add forced groups */
1750 foreach($attrs['groupMembership'] as $dn => $cn){
1751 $groups[$dn] = $cn;
1752 }
1753 plugin::set_multi_edit_values($attrs);
1754 $this->groupMembership = $groups;
1755 }
1756 }
1758 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1759 ?>