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 Nothing forced, so we may have to generate our own IDs, if not done already.
817 */
818 if ($this->force_ids == 0){
820 /* Handle uidNumber.
821 * - use existing number if possible
822 * - if not, try to create a new uniqe one.
823 * */
824 if ($this->savedUidNumber != ""){
825 $this->uidNumber= $this->savedUidNumber;
826 } else {
828 /* Calculate new id's. We need to place a lock before calling get_next_id
829 to get real unique values.
830 */
831 $wait= 10;
832 while (get_lock("uidnumber") != ""){
833 sleep (1);
835 /* Oups - timed out */
836 if ($wait-- == 0){
837 msg_dialog::display(_("Warning"), _("Timeout while waiting for lock. Ignoring lock!"), WARNING_DIALOG);
838 break;
839 }
840 }
841 add_lock ("uidnumber", "gosa");
842 $this->uidNumber= $this->get_next_id("uidNumber", $this->dn);
843 }
844 }
847 /* Handle gidNumber
848 * - If we do not have a primary group selected (automatic), we will check if there
849 * is already a group with the same name and use this as primary.
850 * - .. if we couldn't find a group with the same name, we will create a new one,
851 * using the users uid as cn and a generated uniqe gidNumber.
852 * */
853 if ($this->primaryGroup == 0 || $this->force_ids){
855 /* Search for existing group */
856 $ldap = $this->config->get_ldap_link();
857 $ldap->cd($this->config->current['BASE']);
859 /* Are we forced to use a special gidNumber? */
860 if($this->force_ids){
861 $ldap->search("(&(objectClass=posixGroup)(gidNumber=".$this->gidNumber."))", array("cn","gidNumber"));
862 }else{
863 $ldap->search("(&(objectClass=posixGroup)(gidNumber=*)(cn=".$this->uid."))", array("cn","gidNumber"));
864 }
866 /* No primary group found, create a new one */
867 if ($ldap->count() == 0){
869 $groupcn = $this->uid;
870 $groupdn= preg_replace ('/^'.$this->config->get_cfg_value("accountPrimaryAttribute").'=[^,]+,'.get_people_ou().'/i',
871 'cn='.$groupcn.','.get_groups_ou(), $this->dn);
873 /* Request a new and uniqe gidNumber, if required */
874 if(!$this->force_ids){
875 $this->gidNumber= $this->get_next_id("gidNumber", $this->dn);
876 }else{
878 /* If forced gidNumber could not be found, then check if the given group name already exists
879 we do not want to modify the gidNumber of an existing group.
880 */
881 $cnt= 0;
882 while($ldap->dn_exists($groupdn)){
883 $cnt ++;
884 $groupcn = $this->uid."_".$cnt;
885 $groupdn= preg_replace ('/^'.$this->config->get_cfg_value("accountPrimaryAttribute").'=[^,]+,'.get_people_ou().'/i',
886 'cn='.$groupcn.','.get_groups_ou(), $this->dn);
887 }
888 }
890 /* Create new primary group and enforce the new gidNumber */
891 $g= new group($this->config, $groupdn);
892 $g->cn= $groupcn;
893 $g->force_gid= 1;
894 $g->gidNumber= $this->gidNumber;
895 $g->description= _("Group of user")." ".$this->givenName." ".$this->sn;
896 $g->save ();
898 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
899 sprintf("Primary group '%s' created, using gidNumber '%s'.",$groupcn,$this->gidNumber),"");
900 }else{
901 $attrs = $ldap->fetch();
902 $this->gidNumber = $attrs['gidNumber'][0];
903 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
904 "Found and used: <i>".$attrs['dn']."</i>",
905 sprintf("Primary group '%s' exists, gidNumber is '%s'.",$this->uid,$this->gidNumber));
906 }
907 }else{
909 /* Primary group was selected by user
910 */
911 $this->gidNumber = $this->primaryGroup;
912 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
913 sprintf("Primary group '%s' for user '%s' manually selected.",$this->gidNumber,$this->uid),"");
914 }
916 if ($this->activate_shadowMin != "1" ) {
917 $this->shadowMin = "";
918 }
920 if (($this->activate_shadowMax != "1") && ($this->mustchangepassword != "1")) {
921 $this->shadowMax = "";
922 }
924 if ($this->activate_shadowWarning != "1" ) {
925 $this->shadowWarning = "";
926 }
928 if ($this->activate_shadowInactive != "1" ) {
929 $this->shadowInactive = "";
930 }
932 if ($this->activate_shadowExpire != "1" ) {
933 $this->shadowExpire = "";
934 }
936 /* Fill gecos */
937 if (isset($this->parent) && $this->parent !== NULL){
938 $this->gecos= rewrite($this->parent->by_object['user']->cn);
939 if (!preg_match('/^[a-z0-9 -]+$/i', $this->gecos)){
940 $this->gecos= "";
941 }
942 }
944 foreach(array("shadowMin","shadowMax","shadowWarning","shadowInactive","shadowExpire") as $attr){
945 $this->$attr = (int) $this->$attr;
946 }
947 /* Call parents save to prepare $this->attrs */
948 plugin::save();
950 /* Trust accounts */
951 $objectclasses= array();
952 foreach ($this->attrs['objectClass'] as $key => $class){
953 if (preg_match('/trustAccount/i', $class)){
954 continue;
955 }
956 $objectclasses[]= $this->attrs['objectClass'][$key];
957 }
958 $this->attrs['objectClass']= $objectclasses;
959 if ($this->trustModel != ""){
960 $this->attrs['objectClass'][]= "trustAccount";
961 $this->attrs['trustModel']= $this->trustModel;
962 $this->attrs['accessTo']= array();
963 if ($this->trustModel == "byhost"){
964 foreach ($this->accessTo as $host){
965 $this->attrs['accessTo'][]= $host;
966 }
967 }
968 } else {
969 if ($this->was_trust_account){
970 $this->attrs['accessTo']= array();
971 $this->attrs['trustModel']= array();
972 }
973 }
975 if(empty($this->attrs['gosaDefaultPrinter'])){
976 $thid->attrs['gosaDefaultPrinter']=array();
977 }
980 /* include global link_info */
981 $this->cleanup();
983 /* This is just a test, we have had duplicated ids
984 in the past when copy & paste was used.
985 Normaly this should not happen.
986 */
987 if(isset($this->attrs['uidNumber']) && !$this->force_ids){
988 $used = $this->get_used_uid_numbers();
989 if(isset($used[$this->attrs['uidNumber']]) && $used[$this->attrs['uidNumber']] != $this->dn){
990 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);
991 }
992 }
994 $ldap= $this->config->get_ldap_link();
995 $ldap->cd($this->dn);
996 unset($this->attrs['uid']);
997 $ldap->modify ($this->attrs);
999 /* Log last action */
1000 if($this->initially_was_account){
1001 new log("modify","users/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
1002 }else{
1003 new log("create","users/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
1004 }
1006 if (!$ldap->success()){
1007 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_MOD, get_class()));
1008 }
1010 /* Remove lock needed for unique id generation */
1011 del_lock ("uidnumber");
1014 /* Take care about groupMembership values: add to groups */
1015 foreach ($this->groupMembership as $key => $value){
1016 if (!isset($this->savedGroupMembership[$key])){
1017 $g= new grouptabs($this->config,$this->config->data['TABS']['GROUPTABS'], $key,"groups");
1018 $g->set_acl_base($key);
1019 $g->by_object['group']->addUser($this->uid);
1020 $g->save();
1021 }
1022 }
1024 /* Remove groups not listed in groupMembership */
1025 foreach ($this->savedGroupMembership as $key => $value){
1026 if (!isset($this->groupMembership[$key])){
1027 $g= new grouptabs($this->config,$this->config->data['TABS']['GROUPTABS'], $key,"groups");
1028 $g->set_acl_base($key);
1029 $g->by_object['group']->removeUser ($this->uid);
1030 $g->save();
1031 }
1032 }
1034 /* Optionally execute a command after we're done */
1035 if ($this->initially_was_account == $this->is_account){
1036 if ($this->is_modified){
1037 $this->handle_post_events("modify",array("uid" => $this->uid));
1038 }
1039 } else {
1040 $this->handle_post_events("add" ,array("uid"=> $this->uid));
1041 }
1042 }
1045 /* Check formular input */
1046 function check()
1047 {
1048 /* Include global link_info */
1049 $ldap= $this->config->get_ldap_link();
1051 /* Append groups as memberGroup: to check hook
1052 */
1053 $tmp_attributes = $this->attributes;
1054 $this->attributes[] = "memberGroup";
1055 $this->memberGroup = array();
1056 foreach($this->groupMembership as $dn => $name){
1057 $this->memberGroup[] = $name;
1058 }
1060 /* Call common method to give check the hook */
1061 $message= plugin::check();
1062 $this->attributes = $tmp_attributes;
1064 /* must: homeDirectory */
1065 if ($this->homeDirectory == ""){
1066 $message[]= msgPool::required(_("Home directory"));
1067 }
1068 if (!tests::is_path($this->homeDirectory)){
1069 $message[]= msgPool::invalid(_("Home directory"), "", "", "/home/yourname" );
1070 }
1072 /* Check ID's if they are forced by user */
1073 if ($this->force_ids == "1"){
1075 /* Valid uid/gid? */
1076 if (!tests::is_id($this->uidNumber)){
1077 $message[]= msgPool::invalid(_("UID"), $this->uidNumber, "/[0-9]/");
1078 } else {
1079 if ($this->uidNumber < $this->config->get_cfg_value("minId")){
1080 $message[]= msgPool::toosmall(_("UID"), $this->config->get_cfg_value("minId"));
1081 }
1082 }
1083 if (!tests::is_id($this->gidNumber)){
1084 $message[]= msgPool::invalid(_("GID"), $this->gidNumber, "/[0-9]/");
1085 } else {
1086 if ($this->gidNumber < $this->config->get_cfg_value("minId")){
1087 $message[]= msgPool::toosmall(_("GID"), $this->config->get_cfg_value("minId"));
1088 }
1089 }
1090 }
1092 /* Check shadow settings, well I like spaghetties... */
1093 if ($this->activate_shadowMin){
1094 if (!tests::is_id($this->shadowMin)){
1095 $message[]= msgPool::invalid(_("shadowMin"), $this->shadowMin, "/[0-9]/");
1096 }
1097 }
1098 if ($this->activate_shadowMax){
1099 if (!tests::is_id($this->shadowMax)){
1100 $message[]= msgPool::invalid(_("shadowMax"), $this->shadowMax, "/[0-9]/");
1101 }
1102 }
1103 if ($this->activate_shadowWarning){
1104 if (!tests::is_id($this->shadowWarning)){
1105 $message[]= msgPool::invalid(_("shadowWarning"), $this->shadowWarning, "/[0-9]/");
1106 }
1107 if (!$this->activate_shadowMax){
1108 $message[]= msgPool::depends("shadowWarning", "shadowMax");
1109 }
1110 if ($this->shadowWarning > $this->shadowMax){
1111 $message[]= msgPool::toobig("shadowWarning", "shadowMax");
1112 }
1113 if ($this->activate_shadowMin && $this->shadowWarning < $this->shadowMin){
1114 $message[]= msgPool::toosmall("shadowWarning", "shadowMin");
1115 }
1116 }
1117 if ($this->activate_shadowInactive){
1118 if (!tests::is_id($this->shadowInactive)){
1119 $message[]= msgPool::invalid(_("shadowInactive"), $this->shadowInactive, "/[0-9]/");
1120 }
1121 if (!$this->activate_shadowMax){
1122 $message[]= msgPool::depends("shadowInactive", "shadowMax");
1123 }
1124 }
1125 if ($this->activate_shadowMin && $this->activate_shadowMax){
1126 if ($this->shadowMin > $this->shadowMax){
1127 $message[]= msgPool::toobig("shadowMin", "shadowMax");
1128 }
1129 }
1131 return ($message);
1132 }
1135 function multiple_check()
1136 {
1137 $message = plugin::multiple_check();
1138 if ($this->homeDirectory == "" && in_array("homeDirectory",$this->multi_boxes)){
1139 $message[]= msgPool::required(_("Home directory"));
1140 }
1141 if (!tests::is_path($this->homeDirectory) && in_array("homeDirectory",$this->multi_boxes)){
1142 $message[]= msgPool::invalid(_("Home directory"), "", "", "/home/yourname" );
1143 }
1145 /* Check shadow settings, well I like spaghetties... */
1146 if ($this->activate_shadowMin && in_array("activate_shadowMin",$this->multi_boxes)){
1147 if (!tests::is_id($this->shadowMin)){
1148 $message[]= msgPool::invalid(_("shadowMin"), $this->shadowMin, "/[0-9]/");
1149 }
1150 }
1151 if ($this->activate_shadowMax && in_array("activate_shadowMax",$this->multi_boxes)){
1152 if (!tests::is_id($this->shadowMax)){
1153 $message[]= msgPool::invalid(_("shadowMax"), $this->shadowMax, "/[0-9]/");
1154 }
1155 }
1156 if ($this->activate_shadowWarning && in_array("activate_shadowWarning",$this->multi_boxes)){
1157 if (!tests::is_id($this->shadowWarning)){
1158 $message[]= msgPool::invalid(_("shadowWarning"), $this->shadowWarning, "/[0-9]/");
1159 }
1160 if (!$this->activate_shadowMax && in_array("activate_shadowMax",$this->multi_boxes)){
1161 $message[]= msgPool::depends("shadowWarning", "shadowMax");
1162 }
1163 if ($this->shadowWarning > $this->shadowMax && in_array("activate_shadowWarning",$this->multi_boxes)){
1164 $message[]= msgPool::toobig("shadowWarning", "shadowMax");
1165 }
1166 if ($this->activate_shadowMin && $this->shadowWarning < $this->shadowMin && in_array("activate_shadowMin",$this->multi_boxes)){
1167 $message[]= msgPool::tosmall("shadowWarning", "shadowMin");
1168 }
1169 }
1170 if ($this->activate_shadowInactive && in_array("activate_shadowInactive",$this->multi_boxes)){
1171 if (!tests::is_id($this->shadowInactive)){
1172 $message[]= msgPool::invalid(_("shadowInactive"), $this->shadowInactive, "/[0-9]/");
1173 }
1174 if (!$this->activate_shadowMax && in_array("activate_shadowMax",$this->multi_boxes)){
1175 $message[]= msgPool::depends("shadowInactive", "shadowMax");
1176 }
1177 }
1178 if ($this->activate_shadowMin && $this->activate_shadowMax && in_array("activate_shadowMin",$this->multi_boxes)){
1179 if ($this->shadowMin > $this->shadowMax){
1180 $message[]= msgPool::toobig("shadowMin", "shadowMax");
1181 }
1182 }
1184 return($message);
1185 }
1188 function addGroup ($groups)
1189 {
1190 /* include global link_info */
1191 $ldap= $this->config->get_ldap_link();
1193 /* Walk through groups and add the descriptive entry if not exists */
1194 foreach ($groups as $value){
1196 if (!array_key_exists($value, $this->groupMembership)){
1197 $ldap->cat($value, array('cn', 'description', 'dn'));
1198 $attrs= $ldap->fetch();
1199 error_reporting (0);
1200 if (!isset($attrs['description'][0])){
1201 $entry= $attrs["cn"][0];
1202 } else {
1203 $dsc= preg_replace ('/^Group of user/', _("Group of user"), $attrs["description"][0]);
1204 $entry= $attrs["cn"][0]." [$dsc]";
1205 }
1206 error_reporting (E_ALL | E_STRICT);
1208 if(obj_is_writable($attrs['dn'],"groups/group","memberUid")){
1209 $this->groupMembership[$attrs['dn']]= $entry;
1210 if($this->multiple_support_active && isset($this->groupMembership_some[$attrs['dn']])){
1211 unset($this->groupMembership_some[$attrs['dn']]);
1212 }
1213 }
1214 }
1215 }
1217 /* Sort groups */
1218 asort ($this->groupMembership);
1219 reset ($this->groupMembership);
1220 }
1223 /* Del posix user from some groups */
1224 function delGroup ($groups)
1225 {
1226 $dest= array();
1227 foreach($groups as $dn_to_del){
1228 if(isset($this->groupMembership[$dn_to_del]) && obj_is_writable($dn_to_del,"groups/group","memberUid")){
1229 unset($this->groupMembership[$dn_to_del]);
1230 }
1231 if($this->multiple_support_active){
1232 if(isset($this->groupMembership_some[$dn_to_del]) && obj_is_writable($dn_to_del,"groups/group","memberUid")){
1233 unset($this->groupMembership_some[$dn_to_del]);
1234 }
1235 }
1236 }
1237 }
1240 /* Adapt from template, using 'dn' */
1241 function adapt_from_template($dn, $skip= array())
1242 {
1243 /* Include global link_info */
1244 $ldap= $this->config->get_ldap_link();
1246 plugin::adapt_from_template($dn, $skip);
1247 $template= $this->attrs['uid'][0];
1249 /* Adapt group membership */
1250 $ldap->cd($this->config->current['BASE']);
1251 $ldap->search("(&(objectClass=posixGroup)(memberUid=".$this->attrs["uid"][0]."))", array("description", "cn"));
1253 while ($this->attrs= $ldap->fetch()){
1254 if (!isset($this->attrs["description"][0])){
1255 $entry= $this->attrs["cn"][0];
1256 } else {
1257 $entry= $this->attrs["cn"][0]." [".$this->attrs["description"][0]."]";
1258 }
1259 $this->groupMembership[$ldap->getDN()]= $entry;
1260 }
1262 /* Fix primary group settings */
1263 $ldap->cd($this->config->current['BASE']);
1264 $ldap->search("(&(objectClass=posixGroup)(cn=$template)(gidNumber=".$this->gidNumber."))", array("cn"));
1265 if ($ldap->count() != 1){
1266 $this->primaryGroup= $this->gidNumber;
1267 }
1269 $ldap->cd($this->config->current['BASE']);
1270 $ldap->search("(&(objectClass=gosaUserTemplate)(uid=".$template.")(accessTo=*))", array("cn","accessTo"));
1271 while($attr = $ldap->fetch()){
1272 $tmp = $attr['accessTo'];
1273 unset ($tmp['count']);
1274 $this->accessTo = $tmp;
1275 }
1277 /* Adjust shadow checkboxes */
1278 foreach (array("shadowMin", "shadowMax", "shadowWarning", "shadowInactive") as $val){
1279 if ($this->$val != 0){
1280 $oval= "activate_".$val;
1281 $this->$oval= "1";
1282 }
1283 }
1285 /* FIXME: NEED review of this section */
1286 /* Need to check shadowExpire separately */
1288 /*
1289 * If shadowExpire is not enabled in the template, it's a UNIX timestamp - so don't convert it to seconds.
1290 * The check is a hack - if difference between timestamp generated above and here is max 1 day.
1291 */
1292 if(abs($this->shadowExpire - time())>86400) {
1293 $this->shadowExpire= $this->convertToSeconds($this->shadowExpire);
1294 }
1296 /* Only enable checkbox, if shadowExpire is in the future */
1297 if($this->shadowExpire > time()) {
1298 $this->activate_shadowExpire= "1";
1299 }
1300 }
1302 function convertToSeconds($val)
1303 {
1304 if ($val != 0){
1305 $val*= 60 * 60 * 24;
1306 } else {
1307 $date= getdate();
1308 $val= floor($date[0] / (60*60*24)) * 60 * 60 * 24;
1309 }
1310 return($val);
1311 }
1314 function get_used_uid_numbers()
1315 {
1316 $ids= array();
1317 $ldap= $this->config->get_ldap_link();
1319 $ldap->cd ($this->config->current['BASE']);
1320 $ldap->search ("(&(objectClass=posixAccount)(uidNumber=*))", array("uidNumber"));
1322 /* Get list of ids */
1323 while ($attrs= $ldap->fetch()){
1324 $ids[$attrs['uidNumber'][0]] = $attrs['dn'];
1325 }
1326 return($ids);
1327 }
1330 function get_next_id($attrib, $dn)
1331 {
1332 $ids= array();
1333 $ldap= $this->config->get_ldap_link();
1335 $ldap->cd ($this->config->current['BASE']);
1336 if (preg_match('/gidNumber/i', $attrib)){
1337 $oc= "posixGroup";
1338 } else {
1339 $oc= "posixAccount";
1340 }
1341 $ldap->search ("(&(objectClass=$oc)($attrib=*))", array("$attrib"));
1343 /* Get list of ids */
1344 while ($attrs= $ldap->fetch()){
1345 $ids[]= (int)$attrs["$attrib"][0];
1346 }
1348 /* Add the nobody id */
1349 $ids[]= 65534;
1351 /* get the ranges */
1352 $tmp = array('0'=> 1000);
1353 if (preg_match('/posixAccount/', $oc) && $this->config->get_cfg_value("uidNumberBase") != ""){
1354 $tmp= split('-',$this->config->get_cfg_value("uidNumberBase"));
1355 } elseif($this->config->get_cfg_value("gidNumberBase") != ""){
1356 $tmp= split('-',$this->config->get_cfg_value("gidNumberBase"));
1357 }
1359 /* Set hwm to max if not set - for backward compatibility */
1360 $lwm= $tmp[0];
1361 if (isset($tmp[1])){
1362 $hwm= $tmp[1];
1363 } else {
1364 $hwm= pow(2,32);
1365 }
1367 /* Find out next free id near to UID_BASE */
1368 if ($this->config->get_cfg_value("baseIdHook") == ""){
1369 $base= $lwm;
1370 } else {
1371 /* Call base hook */
1372 $base= get_base_from_hook($dn, $attrib);
1373 }
1374 for ($id= $base; $id++; $id < pow(2,32)){
1375 if (!in_array($id, $ids)){
1376 return ($id);
1377 }
1378 }
1380 /* Should not happen */
1381 if ($id == $hwm){
1382 msg_dialog::display(_("Error"), _("Cannot allocate a free ID!"), ERROR_DIALOG);
1383 exit;
1384 }
1386 }
1388 function reload()
1389 {
1390 /* Set base for all searches */
1391 $base = session::get('CurrentMainBase');
1392 $base = $base;
1393 $ldap = $this->config->get_ldap_link();
1394 $attrs = array("cn", "description", "gidNumber");
1395 $Flags = GL_SIZELIMIT;
1397 /* Get groups */
1398 if ($this->GroupUserRegex == '*'){
1399 $filter = "(&(objectClass=posixGroup)(cn=".$this->GroupRegex."))";
1400 } else {
1401 $filter= "(&(objectClass=posixGroup)(cn=".$this->GroupRegex.")(memberUid=".$this->GroupUserRegex."))";
1402 }
1403 if($this->SubSearch){
1404 $Flags |= GL_SUBSEARCH;
1405 }else{
1406 $base = get_groups_ou().$base;
1407 }
1409 $res= get_list($filter, "groups", $base,$attrs, $Flags);
1411 /* check sizelimit */
1412 if (preg_match("/size limit/i", $ldap->get_error())){
1413 session::set('limit_exceeded',TRUE);
1414 }
1416 /* Create a list of users */
1417 $this->grouplist = array();
1418 foreach ($res as $value){
1419 $this->grouplist[$value['gidNumber'][0]]= $value;
1420 }
1422 $tmp=array();
1423 foreach($this->grouplist as $tkey => $val ){
1424 $tmp[strtolower($val['cn'][0]).$val['cn'][0]]=$val;
1425 }
1427 /* Sort index */
1428 ksort($tmp);
1430 /* Recreate index array[dn]=cn[description]*/
1431 $this->grouplist=array();
1432 foreach($tmp as $val){
1433 if(isset($val['description'])){
1434 $this->grouplist[$val['dn']]=$val['cn'][0]." [".$val['description'][0]."]";
1435 }else{
1436 $this->grouplist[$val['dn']]=$val['cn'][0];
1437 }
1438 }
1440 reset ($this->grouplist);
1441 }
1444 /* Get posts from copy & paste dialog */
1445 function saveCopyDialog()
1446 {
1447 if(isset($_POST['homeDirectory'])){
1448 $this->homeDirectory = $_POST['homeDirectory'];
1449 if (isset ($_POST['force_ids'])){
1450 $data= 1;
1451 $this->gidNumber = $_POST['gidNumber'];
1452 $this->uidNumber = $_POST['uidNumber'];
1453 } else {
1454 $data= 0;
1455 }
1456 if ($this->force_ids != $data){
1457 $this->is_modified= TRUE;
1458 }
1459 $this->force_ids= $data;
1460 }
1461 }
1464 /* Create the posix dialog part for copy & paste */
1465 function getCopyDialog()
1466 {
1467 /* Skip dialog creation if this is not a valid account*/
1468 if(!$this->is_account) return("");
1469 if ($this->force_ids == 1){
1470 $force_ids = "checked";
1471 if (session::get('js')){
1472 $forceMode = "";
1473 }
1474 } else {
1475 if (session::get('js')){
1476 if($this->acl != "#none#")
1477 $forceMode ="disabled";
1478 }
1479 $force_ids = "";
1480 }
1482 $sta = "";
1484 /* Open group add dialog */
1485 if(isset($_POST['edit_groupmembership'])){
1486 $this->group_dialog = TRUE;
1487 $sta = "SubDialog";
1488 }
1490 /* If the group-add dialog is closed, call execute
1491 to ensure that the membership is updatd */
1492 if(isset($_POST['add_groups_finish']) || isset($_POST['add_groups_cancel'])){
1493 $this->execute();
1494 $this->group_dialog =FALSE;
1495 }
1497 if($this->group_dialog){
1498 $str = $this->execute(true);
1499 $ret = array();
1500 $ret['string'] = $str;
1501 $ret['status'] = $sta;
1502 return($ret);
1503 }
1505 /* If a group member should be deleted, simply call execute */
1506 if(isset($_POST['delete_groupmembership'])){
1507 $this->execute();
1508 }
1510 /* Assigned informations to smarty */
1511 $smarty = get_smarty();
1512 $smarty->assign("homeDirectory",$this->homeDirectory);
1513 $smarty->assign("uidNumber",$this->uidNumber);
1514 $smarty->assign("gidNumber",$this->gidNumber);
1515 $smarty->assign("forceMode",$forceMode);
1516 $smarty->assign("force_ids",$force_ids);
1517 if (!count($this->groupMembership)){
1518 $smarty->assign("groupMembership", array(" "));
1519 } else {
1520 $smarty->assign("groupMembership", $this->groupMembership);
1521 }
1523 /* Display wars message if there are more than 16 group members */
1524 if (count($this->groupMembership) > 16){
1525 $smarty->assign("groups", "too_many_for_nfs");
1526 } else {
1527 $smarty->assign("groups", "");
1528 }
1529 $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE,dirname(__FILE__)));
1531 $ret = array();
1532 $ret['string'] = $str;
1533 $ret['status'] = $sta;
1534 return($ret);
1535 }
1538 function PrepareForCopyPaste($source)
1539 {
1540 plugin::PrepareForCopyPaste($source);
1542 /* Avoid using the same gid/uid number as source user
1543 empty numbers to enforce new ones. */
1544 # $this->savedUidNumber = $this->get_next_id("uidNumber", $this->dn);
1545 $this->savedUidNumber = "";
1546 # $this->savedGidNumber = $this->get_next_id("gidNumber", $this->dn);
1547 $this->savedGidNumber = "";
1548 }
1551 function multiple_execute()
1552 {
1553 return($this->execute());
1554 }
1557 static function plInfo()
1558 {
1559 return (array(
1560 "plDescription" => _("POSIX account"),
1561 "plSelfModify" => TRUE,
1562 "plDepends" => array("user"),
1563 "plPriority" => 2,
1564 "plSection" => array("personal" => _("My account")),
1565 "plCategory" => array("users"),
1566 "plOptions" => array(),
1568 "plProvidedAcls" => array(
1570 "homeDirectory" => _("Home directory"),
1571 "loginShell" => _("Shell"),
1572 "uidNumber" => _("User ID"),
1573 "gidNumber" => _("Group ID"),
1575 "mustchangepassword"=> _("Force password change on login"),
1576 "shadowMin" => _("Shadow min"),
1577 "shadowMax" => _("Shadow max"),
1578 "shadowWarning" => _("Shadow warning"),
1579 "shadowInactive" => _("Shadow inactive"),
1580 "shadowExpire" => _("Shadow expire"),
1581 "trustModel" => _("System trust model")))
1582 );
1583 }
1586 /* Return selected values for multiple edit */
1587 function get_multi_edit_values()
1588 {
1589 $ret = plugin::get_multi_edit_values();
1590 $ret['groupMembership'] = $this->groupMembership;
1591 $ret['groupMembership_some']= $this->groupMembership_some;
1593 if(in_array("primaryGroup",$this->multi_boxes)){
1594 $ret['primaryGroup'] = $this->primaryGroup;
1595 }
1596 if(in_array("trustmode",$this->multi_boxes)){
1597 $ret['trustModel'] = $this->trustModel;
1598 $ret['accessTo'] = $this->accessTo;
1599 }
1600 foreach(array("shadowWarning","shadowInactive","shadowMin","shadowMax", "shadowExpire") as $entry){
1601 $active = "activate_".$entry;
1602 if(in_array($active,$this->multi_boxes)){
1603 $ret[$entry] = $this->$entry;
1604 $ret[$active] = $this->$active;
1605 }
1606 }
1607 if(in_array("mustchangepassword",$this->multi_boxes)){
1608 $ret['mustchangepassword'] = $this->mustchangepassword;
1609 }
1610 return($ret);
1611 }
1614 /* Save posts for multiple edit
1615 */
1616 function multiple_save_object()
1617 {
1618 if(isset($_POST['posix_mulitple_edit'])){
1620 /* Backup expire value */
1621 $expire_tmp = $this->shadowExpire;
1623 /* Update all values */
1624 plugin::multiple_save_object();
1626 /* Get selected checkboxes */
1627 foreach(array("primaryGroup","trustmode","mustchangepassword","activate_shadowWarning","activate_shadowInactive","activate_shadowMin", "activate_shadowMax","activate_shadowExpire") as $val){
1628 if(isset($_POST["use_".$val])){
1629 $this->multi_boxes[] = $val;
1630 }
1631 }
1633 /* Update special values, checkboxes for posixShadow */
1634 foreach(array("shadowMin","shadowMax","shadowExpire","shadowInactive","shadowWarning") as $var) {
1635 if($this->acl_is_writeable($var)){
1636 $activate_var = "activate_".$var;
1637 if(in_array($activate_var, $this->multi_boxes)){
1638 if(isset($_POST['activate_'.$var])){
1639 $this->$activate_var = true;
1640 $this->$var = $_POST[$var];
1641 }else{
1642 $this->$activate_var = false;
1643 $this->$var = 0;
1644 }
1645 }
1646 }
1647 }
1649 /* Restore shadow value, if the shadow attribute isn't used */
1650 if(!in_array("activate_shadowExpire",$this->multi_boxes)){
1651 $this->shadowExpire = $expire_tmp;
1652 }
1654 /* Force change password ? */
1655 if(isset($_POST['mustchangepassword'])){
1656 $this->mustchangepassword = TRUE;
1657 }else{
1658 $this->mustchangepassword = FALSE;
1659 }
1661 /* Trust mode - special handling */
1662 if($this->acl_is_writeable("trustModel")){
1663 if (isset($_POST['trustmode'])){
1664 $saved= $this->trustModel;
1665 if ($_POST['trustmode'] == "1"){
1666 $this->trustModel= "fullaccess";
1667 } elseif ($_POST['trustmode'] == "2"){
1668 $this->trustModel= "byhost";
1669 } else {
1670 $this->trustModel= "";
1671 }
1672 if ($this->trustModel != $saved){
1673 $this->is_modified= TRUE;
1674 }
1675 }
1676 }
1678 /* Save primary group settings */
1679 if($this->acl_is_writeable("primaryGroup") && isset($_POST['primaryGroup'])){
1680 $data= $_POST['primaryGroup'];
1681 if ($this->primaryGroup != $data){
1682 $this->is_modified= TRUE;
1683 }
1684 $this->primaryGroup= $_POST['primaryGroup'];
1685 }
1686 }
1687 }
1690 /* Initialize plugin with given atribute arrays
1691 */
1692 function init_multiple_support($attrs,$all)
1693 {
1694 plugin::init_multiple_support($attrs,$all);
1696 /* Some dummy values */
1697 $groups_some = array();
1698 $groups_all = array();
1699 $groups_uid = array();
1700 $uids = array();
1701 $first = TRUE;
1703 /* Get all groups used by currently edited users */
1704 $uid_filter="";
1705 for($i =0; $i < $this->multi_attrs_all['uid']['count'] ; $i ++){
1706 $uid = $this->multi_attrs_all['uid'][$i];
1707 $uids[] = $uid;
1708 $uid_filter.= "(memberUid=".$uid.")";
1709 }
1710 $uid_filter = "(&(objectClass=posixGroup)(|".$uid_filter."))";
1711 $ldap = $this->config->get_ldap_link();
1712 $ldap->cd($this->config->current['BASE']);
1713 $ldap->search($uid_filter,array("dn","cn","memberUid"));
1714 while($group = $ldap->fetch()){
1715 $groups_some[$group['dn']] = $group['cn'][0];
1716 for($i = 0 ; $i < $group['memberUid']['count'] ; $i++){
1717 $groups_uid[$group['dn']][] = $group['memberUid'][$i];
1718 }
1719 }
1721 /* Create an array, containing all used groups */
1722 $groups_all = $groups_some;
1723 foreach($groups_all as $id => $group){
1724 foreach($uids as $uid){
1725 if(!in_array($uid,$groups_uid[$id])){
1726 unset($groups_all[$id]);
1727 break;
1728 }
1729 }
1730 }
1732 /* Assign group array */
1733 $this->groupMembership = $groups_all;
1735 /* Create an array of all grouops used by all users */
1736 foreach( $groups_all as $dn => $cn){
1737 if(isset($groups_some[$dn])){
1738 unset($groups_some[$dn]);
1739 }
1740 }
1741 $this->groupMembership_some = $groups_some;
1742 $this->primaryGroup = $this->gidNumber;
1744 /* Is this account a trustAccount? */
1745 if (isset($this->multi_attrs['trustModel'])){
1746 $this->trustModel= $this->multi_attrs['trustModel'][0];
1747 $this->was_trust_account= TRUE;
1748 $this->multi_boxes[] = "trustmode";
1749 } else {
1750 $this->was_trust_account= FALSE;
1751 $this->trustModel= "";
1752 }
1754 /* Create access informations */
1755 $this->accessTo = array();
1756 if (isset($this->multi_attrs['accessTo'])){
1757 for ($i= 0; $i<$this->multi_attrs['accessTo']['count']; $i++){
1758 $tmp= $this->multi_attrs['accessTo'][$i];
1759 $this->accessTo[$tmp]= $tmp;
1760 }
1761 }
1763 /* Adjust shadow checkboxes */
1764 foreach (array("shadowMin", "shadowMax", "shadowWarning", "shadowInactive",
1765 "shadowExpire") as $val){
1766 if ($this->$val != 0){
1767 $oval= "activate_".$val;
1768 $this->$oval= "1";
1769 }
1770 }
1772 /* Convert to seconds */
1773 if(isset($this->multi_attrs['shadowExpire'])){
1774 $this->shadowExpire = $this->convertToSeconds($this->multi_attrs['shadowExpire'][0]);
1775 }else{
1776 $this->activate_shadowExpire = FALSE;
1777 }
1778 }
1781 function set_multi_edit_values($attrs)
1782 {
1783 $groups = array();
1785 /* Update groupMembership, keep optinal group */
1786 foreach($attrs['groupMembership_some'] as $dn => $cn){
1787 if(isset($this->groupMembership[$dn])){
1788 $groups[$dn] = $cn;
1789 }
1790 }
1791 /* Update groupMembership, add forced groups */
1792 foreach($attrs['groupMembership'] as $dn => $cn){
1793 $groups[$dn] = $cn;
1794 }
1795 plugin::set_multi_edit_values($attrs);
1796 $this->groupMembership = $groups;
1797 }
1798 }
1800 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1801 ?>