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= "";
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 $gotoLastSystemLogin= "";
71 var $groupSelect= FALSE;
72 var $trustDialog= FALSE;
73 var $secondaryGroups= array();
74 var $primaryGroup= 0;
75 var $was_trust_account= FALSE;
76 var $memberGroup= array();
77 var $grouplist= array();
78 var $ui= array();
79 var $ssh= null;
80 var $sshAcl= "";
82 var $GroupRegex= "*";
83 var $GroupUserRegex= "*";
84 var $SubSearch= false;
86 var $view_logged= false;
88 /* attribute list for save action */
89 var $CopyPasteVars =
90 array("grouplist","groupMembership","activate_shadowMin",
91 "activate_shadowMax","activate_shadowWarning","activate_shadowInactive","activate_shadowExpire",
92 "must_change_password","printerList","grouplist","savedGidNumber","savedUidNumber");
94 var $attributes = array("homeDirectory", "loginShell", "uidNumber", "gidNumber", "gecos",
95 "shadowMin", "shadowMax", "shadowWarning", "shadowInactive", "shadowLastChange",
96 "shadowExpire", "gosaDefaultPrinter", "uid","accessTo","trustModel", "gotoLastSystemLogin");
98 var $objectclasses= array("posixAccount", "shadowAccount");
100 var $uid= "";
101 var $multiple_support = TRUE;
102 var $groupMembership_some = array();
104 /* constructor, if 'dn' is set, the node loads the given
105 'dn' from LDAP */
106 function posixAccount (&$config, $dn= NULL)
107 {
108 global $class_mapping;
110 /* Configuration is fine, allways */
111 $this->config= $config;
113 /* Load bases attributes */
114 plugin::plugin($config, $dn);
116 /* If gotoLastSystemLogin is available read it from ldap and create a readable
117 date time string, fallback to sambaLogonTime if available.
118 */
119 if(isset($this->attrs['gotoLastSystemLogin'][0]) && preg_match("/^[0-9]*$/",$this->attrs['gotoLastSystemLogin'][0])){
120 $this->gotoLastSystemLogin = date("d.m.Y H:i:s", strtotime($this->attrs['gotoLastSystemLogin'][0]));
121 } else if(isset($this->attrs['sambaLogonTime'][0]) && preg_match("/^[0-9]*$/",$this->attrs['sambaLogonTime'][0])){
122 $this->gotoLastSystemLogin = date("d.m.Y H:i:s", $this->attrs['sambaLogonTime'][0]);
123 }
125 /* Setting uid to default */
126 if(isset($this->attrs['uid'][0])){
127 $this->uid = $this->attrs['uid'][0];
128 }
130 $ldap= $this->config->get_ldap_link();
132 if ($dn !== NULL){
134 /* Correct is_account. shadowAccount is not required. */
135 if (isset($this->attrs['objectClass']) &&
136 in_array ('posixAccount', $this->attrs['objectClass'])){
138 $this->is_account= TRUE;
139 }
141 /* Is this account a trustAccount? */
142 if ($this->is_account && isset($this->attrs['trustModel'])){
143 $this->trustModel= $this->attrs['trustModel'][0];
144 $this->was_trust_account= TRUE;
145 } else {
146 $this->was_trust_account= FALSE;
147 $this->trustModel= "";
148 }
150 $this->accessTo = array();
151 if ($this->is_account && isset($this->attrs['accessTo'])){
152 for ($i= 0; $i<$this->attrs['accessTo']['count']; $i++){
153 $tmp= $this->attrs['accessTo'][$i];
154 $this->accessTo[$tmp]= $tmp;
155 }
156 }
157 $this->initially_was_account= $this->is_account;
159 /* Fill group */
160 $this->primaryGroup= $this->gidNumber;
162 /* Generate status text */
163 $current= date("U");
165 $current= floor($current / 60 /60 / 24);
167 if (($current >= $this->shadowExpire) && $this->shadowExpire){
168 $this->status= _("expired");
169 if (($current - $this->shadowExpire) < $this->shadowInactive){
170 $this->status.= ", "._("grace time active");
171 }
172 } elseif (($this->shadowLastChange + $this->shadowMin) >= $current){
173 $this->status= _("active").", "._("password not changeable");
174 } elseif (($this->shadowLastChange + $this->shadowMax) >= $current){
175 $this->status= _("active").", "._("password expired");
176 } else {
177 $this->status= _("active");
178 }
180 /* Get group membership */
181 $ldap->cd($this->config->current['BASE']);
182 $ldap->search("(&(objectClass=posixGroup)(memberUid=".$this->uid."))", array("cn", "description"));
184 while ($attrs= $ldap->fetch()){
185 if (!isset($attrs["description"][0])){
186 $entry= $attrs["cn"][0];
187 } else {
188 $entry= $attrs["cn"][0]." [".$attrs["description"][0]."]";
189 }
190 $this->groupMembership[$ldap->getDN()]= $entry;
191 }
192 asort($this->groupMembership);
193 reset($this->groupMembership);
194 $this->savedGroupMembership= $this->groupMembership;
195 $this->savedUidNumber= $this->uidNumber;
196 $this->savedGidNumber= $this->gidNumber;
198 // Instanciate SSH object if available
199 if (isset($class_mapping["sshPublicKey"])){
200 if (empty($this->acl_base)){
201 $this->acl_base= $config->current['BASE'];
202 }
204 $this->sshAcl= $this->getacl("sshPublicKey");
205 $this->ssh= new sshPublicKey($this->config, $this->dn, $this->sshAcl);
206 }
207 }
209 /* Adjust shadow checkboxes */
210 foreach (array("shadowMin", "shadowMax", "shadowWarning", "shadowInactive",
211 "shadowExpire") as $val){
213 if ($this->$val != 0){
214 $oval= "activate_".$val;
215 $this->$oval= "1";
216 }
217 }
219 /* Convert shadowExpire for usage */
220 if ($this->shadowExpire == 0){
221 $this->shadowExpire= "";
222 } else {
223 $this->shadowExpire= date('d.m.Y', $this->shadowExpire * 60 * 60 * 24);
224 }
227 /* Generate shell list from CONFIG_DIR./shells */
228 if (file_exists(CONFIG_DIR.'/shells')){
229 $shells = file (CONFIG_DIR.'/shells');
230 foreach ($shells as $line){
231 if (!preg_match ("/^#/", $line)){
232 $this->loginShellList[]= trim($line);
233 }
234 }
235 } else {
236 if ($this->loginShell == ""){
237 $this->loginShellList[]= _("unconfigured");
238 }
239 }
241 /* Insert possibly missing loginShell */
242 if ($this->loginShell != "" && !in_array($this->loginShell, $this->loginShellList)){
243 $this->loginShellList[]= $this->loginShell;
244 }
246 /* Generate group list */
247 $this->ui = get_userinfo();
248 $this->secondaryGroups[]= "- "._("automatic")." -";
249 $ldap->cd($this->config->current['BASE']);
250 $ldap->search("(objectClass=posixGroup)", array("cn", "gidNumber"));
251 while($attrs = $ldap->fetch()){
252 $this->secondaryGroups[$attrs['gidNumber'][0]]= $attrs['cn'][0];
253 }
254 asort ($this->secondaryGroups);
256 /* Get global filter config */
257 if (!session::is_set("sysfilter")){
258 $ui= get_userinfo();
259 $base= get_base_from_people($ui->dn);
260 $sysfilter= array( "depselect" => $base,
261 "regex" => "*");
262 session::set("sysfilter", $sysfilter);
263 }
264 $this->ui = get_userinfo();
265 }
268 /* execute generates the html output for this node */
269 function execute($isCopyPaste = false)
270 {
271 /* Call parent execute */
272 plugin::execute();
273 $display= "";
275 /* Log view */
276 if($this->is_account && !$this->view_logged){
277 $this->view_logged = TRUE;
278 new log("view","users/".get_class($this),$this->dn);
279 }
281 /* Department has changed? */
282 if(isset($_POST['depselect'])){
283 session::set('CurrentMainBase',validate($_POST['depselect']));
284 }
286 if($this->multiple_support_active){
287 $this->is_account = TRUE;
288 }
290 if(!$isCopyPaste && ! $this->multiple_support_active){
292 /* Do we need to flip is_account state? */
293 if(isset($_POST['modify_state'])){
294 if($this->is_account && $this->acl_is_removeable()){
295 $this->is_account= FALSE;
296 }elseif(!$this->is_account && $this->acl_is_createable()){
297 $this->is_account= TRUE;
298 }
299 }
301 /* Do we represent a valid posixAccount? */
302 if (!$this->is_account && $this->parent === NULL ){
303 $display= "<img alt=\"\" src=\"images/small-error.png\" align=\"middle\"> <b>".
304 msgPool::noValidExtension(_("POSIX"))."</b>";
305 $display.= back_to_main();
306 return ($display);
307 }
310 /* Show tab dialog headers */
311 if ($this->parent !== NULL){
312 if ($this->is_account){
313 if (isset($this->parent->by_object['sambaAccount'])){
314 $obj= $this->parent->by_object['sambaAccount'];
315 }
316 if (isset($obj) && $obj->is_account == TRUE &&
317 ((isset($this->parent->by_object['sambaAccount']))&&($this->parent->by_object['sambaAccount']->is_account))
318 ||(isset($this->parent->by_object['environment'] ))&&($this->parent->by_object['environment'] ->is_account)){
320 /* Samba3 dependency on posix accounts are enabled
321 in the moment, because I need to rely on unique
322 uidNumbers. There'll be a better solution later
323 on. */
324 $display= $this->show_disable_header(msgPool::removeFeaturesButton(_("POSIX")), msgPool::featuresEnabled(_("POSIX"), array(_("Samba"), _("Environment"))), TRUE);
325 } else {
326 $display= $this->show_disable_header(msgPool::removeFeaturesButton(_("POSIX")), msgPool::featuresEnabled(_("POSIX")));
327 }
328 } else {
329 $display= $this->show_enable_header(msgPool::addFeaturesButton(_("POSIX")), msgPool::featuresDisabled(_("POSIX")));
330 return($display);
331 }
332 }
333 }
335 /* Trigger group edit? */
336 if (isset($_POST['edit_groupmembership'])){
337 $this->groupSelect = new groupSelect($this->config,get_userinfo());
338 $this->dialog= TRUE;
339 }
341 /* Cancel group edit? */
342 if (isset($_POST['add_groups_cancel'])){
343 $this->groupSelect= NULL;
344 $this->dialog= FALSE;
345 }
347 /* Add selected groups */
348 if (isset($_POST['add_groups_finish']) && $this->groupSelect){
349 $groups = $this->groupSelect->detectPostActions();
350 if(isset($groups['targets'])){
351 $this->addGroup ($groups['targets']);
352 }
353 $this->groupSelect= NULL;
354 $this->dialog= FALSE;
355 }
357 /* Delete selected groups */
358 if (isset($_POST['delete_groupmembership']) &&
359 isset($_POST['group_list']) && count($_POST['group_list'])){
361 $this->delGroup ($_POST['group_list']);
362 }
364 /* Add user workstation? */
365 if (isset($_POST["add_ws"])){
366 $this->trustDialog= new trustSelect($this->config,get_userinfo());
367 $this->dialog= TRUE;
368 }
370 /* Add user workstation? */
371 if (isset($_POST["add_ws_finish"]) && isset($_POST['wslist'])){
372 foreach($_POST['wslist'] as $ws){
373 $this->accessTo[$ws]= $ws;
374 }
375 ksort($this->accessTo);
376 $this->is_modified= TRUE;
377 }
379 /* Remove user workstations? */
380 if (isset($_POST["delete_ws"]) && isset($_POST['workstation_list'])){
381 foreach($_POST['workstation_list'] as $name){
382 unset ($this->accessTo[$name]);
383 }
384 $this->is_modified= TRUE;
385 }
387 /* Add user workstation finished? */
388 if (isset($_POST["add_ws_finish"]) || isset($_POST["add_ws_cancel"])){
389 $this->trustDialog= NULL;
390 $this->dialog= FALSE;
391 }
393 /* Templates now! */
394 $smarty= get_smarty();
395 $smarty->assign("usePrototype", "true");
397 /* Show ws dialog */
398 if ($this->trustDialog){
399 return($this->trustDialog->execute());
400 }
402 /* Manage group add dialog */
403 if ($this->groupSelect){
404 return($this->groupSelect->execute());
405 }
407 // Handle ssh dialog?
408 if ($this->ssh instanceOf sshPublicKey && preg_match('/[rw]/', $this->getacl("sshPublicKey"))) {
409 $smarty->assign("usePrototype", "false");
410 if ($result= $this->ssh->execute()) {
411 $this->dialog= true;
412 return $result;
413 }
414 $this->dialog= false;
415 }
418 /* Show main page */
419 $smarty= get_smarty();
420 $smarty->assign("usePrototype", "true");
422 /* In 'MyAccount' mode, we must remove write acls if we are not in editing mode. */
423 $SkipWrite = (!isset($this->parent) || !$this->parent) && !session::is_set('edit');
425 $smarty->assign("sshPublicKeyACL", $this->getacl("sshPublicKey", $SkipWrite));
427 /* Depending on pwmode, currently hardcoded because there are no other methods */
428 if ( 1 == 1 ){
429 $smarty->assign("pwmode", dirname(__FILE__)."/posix_shadow");
431 $shadowMinACL = $this->getacl("shadowMin",$SkipWrite);
432 $smarty->assign("shadowmins", sprintf(_("Password can't be changed up to %s days after last change"),
433 "<input name=\"shadowMin\" size=3 maxlength=4 value=\"".$this->shadowMin."\">"));
435 $shadowMaxACL = $this->getacl("shadowMax",$SkipWrite);
436 $smarty->assign("shadowmaxs", sprintf(_("Password must be changed after %s days"),
437 "<input name=\"shadowMax\" size=3 maxlength=4 value=\"".$this->shadowMax."\">"));
439 $shadowInactiveACL= $this->getacl("shadowInactive",$SkipWrite);
440 $smarty->assign("shadowinactives", sprintf(_("Disable account after %s days of inactivity after password expiry"),
441 "<input name=\"shadowInactive\" size=3 maxlength=4 value=\"".$this->shadowInactive."\">"));
443 $shadowWarningACL = $this->getacl("shadowWarning",$SkipWrite);
444 $smarty->assign("shadowwarnings", sprintf(_("Warn user %s days before password expiry"),
445 "<input name=\"shadowWarning\" size=3 maxlength=4 value=\"".$this->shadowWarning."\">"));
447 foreach( array("activate_shadowMin", "activate_shadowMax",
448 "activate_shadowExpire", "activate_shadowInactive","activate_shadowWarning") as $val){
449 if ($this->$val == 1){
450 $smarty->assign("$val", "checked");
451 } else {
452 $smarty->assign("$val", "");
453 }
454 $smarty->assign("$val"."ACL", $this->getacl($val,$SkipWrite));
455 }
457 $smarty->assign("mustchangepasswordACL", $this->getacl("mustchangepassword",$SkipWrite));
458 }
460 // Set last system login
461 $smarty->assign("gotoLastSystemLogin",$this->gotoLastSystemLogin);
463 /* Fill arrays */
464 $smarty->assign("shells", $this->loginShellList);
465 $smarty->assign("secondaryGroups", $this->secondaryGroups);
466 $smarty->assign("primaryGroup", $this->primaryGroup);
467 if(!$this->multiple_support_active){
468 if (!count($this->groupMembership)){
469 $smarty->assign("groupMembership", array(" "));
470 } else {
471 $smarty->assign("groupMembership", $this->groupMembership);
472 }
473 }else{
474 $smarty->assign("groupMembership", $this->groupMembership);
475 $smarty->assign("groupMembership_some", $this->groupMembership_some);
476 }
477 if (count($this->groupMembership) > 16){
478 $smarty->assign("groups", "too_many_for_nfs");
479 } else {
480 $smarty->assign("groups", "");
481 }
483 /* Avoid "Undefined index: forceMode" */
484 $smarty->assign("forceMode", "");
486 /* Checkboxes */
487 if ($this->force_ids == 1){
488 $smarty->assign("force_ids", "checked");
489 if (session::get('js')){
490 $smarty->assign("forceMode", "");
491 }
492 } else {
493 if (session::get('js')){
494 $smarty->assign("forceMode", "disabled");
495 }
496 $smarty->assign("force_ids", "");
497 }
499 /* Create onClick="" action string for the "Force UID/GID" option
500 */
501 $onClickIDS ="";
502 if(preg_match("/w/",$this->getacl("uidNumber",$SkipWrite))){
503 $onClickIDS .= "changeState('uidNumber');";
504 }
505 if(preg_match("/w/",$this->getacl("gidNumber",$SkipWrite))){
506 $onClickIDS .= "changeState('gidNumber');";
507 }
508 $smarty->assign("onClickIDS", $onClickIDS);
509 $smarty->assign("force_idsACL", $this->getacl("uidNumber",$SkipWrite).$this->getacl("gidNumber",$SkipWrite));
511 foreach(array("primaryGroup","trustmode","activate_shadowWarning","activate_shadowInactive","activate_shadowMin","activate_shadowMax","activate_shadowExpire","mustchangepassword") as $val){
512 if(in_array($val,$this->multi_boxes)){
513 $smarty->assign("use_".$val,TRUE);
514 }else{
515 $smarty->assign("use_".$val,FALSE);
516 }
517 }
520 /* Load attributes and acl's */
521 foreach($this->attributes as $val){
522 if(in_array($val,$this->multi_boxes)){
523 $smarty->assign("use_".$val,TRUE);
524 }else{
525 $smarty->assign("use_".$val,FALSE);
526 }
528 if((session::get("js"))&&(($val=="uidNumber")||($val=="gidNumber")))
529 {
530 $smarty->assign("$val"."ACL",$this->getacl($val,$SkipWrite));
531 $smarty->assign("$val", $this->$val);
532 continue;
533 }
534 $smarty->assign("$val", $this->$val);
535 $smarty->assign("$val"."ACL", $this->getacl($val,$SkipWrite));
536 }
537 if($SkipWrite){
538 $smarty->assign("groupMembershipACL","r");
539 }else{
540 $smarty->assign("groupMembershipACL","rw");
541 }
542 $smarty->assign("status", $this->status);
544 /* Work on trust modes */
545 $smarty->assign("trusthide", " disabled ");
546 $smarty->assign("trustmodeACL", $this->getacl("trustModel",$SkipWrite));
547 if ($this->trustModel == "fullaccess"){
548 $trustmode= 1;
549 // pervent double disable tag in html code, this will disturb our clean w3c html
550 $smarty->assign("trustmode", $this->getacl("trustModel",$SkipWrite));
552 } elseif ($this->trustModel == "byhost"){
553 $trustmode= 2;
554 $smarty->assign("trusthide", "");
555 } else {
556 // pervent double disable tag in html code, this will disturb our clean w3c html
557 $smarty->assign("trustmode", $this->getacl("trustModel",$SkipWrite));
558 $trustmode= 0;
559 }
560 $smarty->assign("trustmode", $trustmode);
561 $smarty->assign("trustmodes", array( 0 => _("disabled"), 1 => _("full access"),
562 2 => _("allow access to these hosts")));
566 if((count($this->accessTo))==0)
567 $smarty->assign("emptyArrAccess",true);
568 else
569 $smarty->assign("emptyArrAccess",false);
571 if($this->mustchangepassword){
572 $smarty->assign("mustchangepassword", " checked ");
573 } else {
574 $smarty->assign("mustchangepassword", "");
575 }
577 $smarty->assign("workstations", $this->accessTo);
579 // Add SSH button if available
580 $smarty->assign("sshPublicKey", $this->ssh?1:0);
582 $smarty->assign("apply", apply_filter());
583 $smarty->assign("multiple_support" , $this->multiple_support_active);
584 $display.= $smarty->fetch (get_template_path('generic.tpl', TRUE, dirname(__FILE__)));
585 return($display);
586 }
589 /* remove object from parent */
590 function remove_from_parent()
591 {
592 /* Cancel if there's nothing to do here */
593 if ((!$this->initially_was_account) || (!$this->acl_is_removeable())){
594 return;
595 }
598 /* Remove and write to LDAP */
599 plugin::remove_from_parent();
601 /* Zero out array */
602 $this->attrs['gosaHostACL']= array();
604 /* Keep uid, because we need it for authentification! */
605 unset($this->attrs['uid']);
606 unset($this->attrs['trustModel']);
608 @DEBUG (DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__,
609 /* include global link_info */
610 $this->attributes, "Save");
611 $ldap= $this->config->get_ldap_link();
612 $ldap->cd($this->dn);
613 $this->cleanup();
614 $ldap->modify ($this->attrs);
616 new log("remove","users/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
618 if (!$ldap->success()){
619 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_DEL, get_class()));
620 }
622 /* Delete group only if cn is uid and there are no other
623 members inside */
624 $ldap->cd ($this->config->current['BASE']);
625 $ldap->search ("(&(objectClass=posixGroup)(gidNumber=".$this->gidNumber."))", array("cn", "memberUid"));
626 if ($ldap->count() != 0){
627 $attrs= $ldap->fetch();
628 if ($attrs['cn'][0] == $this->uid &&
629 !isset($this->attrs['memberUid'])){
631 $ldap->rmDir($ldap->getDN());
632 }
633 }
635 /* Optionally execute a command after we're done */
636 $this->handle_post_events("remove",array("uid" => $this->uid));
637 }
640 function save_object()
641 {
642 if (isset($_POST['posixTab'])){
643 /* Save values to object */
644 plugin::save_object();
647 /* Save force GID checkbox */
648 if($this->acl_is_writeable("gidNumber") || $this->acl_is_writeable("uidNumber")){
649 if (isset ($_POST['force_ids'])){
650 $data= 1;
651 } else {
652 $data= 0;
653 }
654 if ($this->force_ids != $data){
655 $this->is_modified= TRUE;
656 }
657 $this->force_ids= $data;
658 }
660 /*Save primary group settings */
661 if($this->acl_is_writeable("primaryGroup") && isset($_POST['primaryGroup'])){
662 $data= $_POST['primaryGroup'];
663 if ($this->primaryGroup != $data){
664 $this->is_modified= TRUE;
665 }
666 $this->primaryGroup= $_POST['primaryGroup'];
667 }
669 /* Get seelcted shadow checkboxes */
670 foreach(array("shadowMin","shadowMax","shadowExpire","shadowInactive","shadowWarning") as $var) {
671 if($this->acl_is_writeable($var)){
672 $activate_var = "activate_".$var;
673 if(isset($_POST['activate_'.$var])){
674 $this->$activate_var = true;
675 $this->$var = $_POST[$var];
676 }else{
677 $this->$activate_var = false;
678 if ($var != "shadowExpire") {
679 $this->$var = 0;
680 }
681 }
682 }
683 }
685 /* Force change password ? */
686 if(isset($_POST['mustchangepassword'])){
687 $this->mustchangepassword = TRUE;
688 }else{
689 $this->mustchangepassword = FALSE;
690 }
692 /* Trust mode - special handling */
693 if($this->acl_is_writeable("trustModel")){
694 if (isset($_POST['trustmode'])){
695 $saved= $this->trustModel;
696 if ($_POST['trustmode'] == "1"){
697 $this->trustModel= "fullaccess";
698 } elseif ($_POST['trustmode'] == "2"){
699 $this->trustModel= "byhost";
700 } else {
701 $this->trustModel= "";
702 }
703 if ($this->trustModel != $saved){
704 $this->is_modified= TRUE;
705 }
706 }
707 }
708 }
710 /* Get regex from alphabet */
711 if(isset($_GET['search'])){
712 $this->GroupRegex = $_GET['search']."*";
713 }
715 /* Check checkboxes and regexes */
716 if(isset($_POST["PosixGroupDialogPosted"])){
718 if(isset($_POST['SubSearch']) && ($_POST['SubSearch'])){
719 $this->SubSearch = true;
720 }else{
721 $this->SubSearch = false;
722 }
723 if(isset($_POST['guser'])){
724 $this->GroupUserRegex = $_POST['guser'];
725 }
726 if(isset($_POST['regex'])){
727 $this->GroupRegex = $_POST['regex'];
728 }
729 }
730 $this->GroupRegex = preg_replace("/\*\**/","*",$this->GroupRegex);
731 $this->GroupUserRegex = preg_replace("/\*\**/","*",$this->GroupUserRegex);
732 }
735 /* Save data to LDAP, depending on is_account we save or delete */
736 function save()
737 {
739 /* Adapt shadow values */
740 if (!$this->activate_shadowExpire){
741 $this->shadowExpire= "0";
742 } else {
743 /* Transform date to days since the beginning */
744 list($day, $month, $year)= explode('.', $this->shadowExpire, 3);
745 $this->shadowExpire= (int)(mktime(0, 0, 0, $month, $day, $year)/ (60 * 60 * 24)) ;
746 }
747 if (!$this->activate_shadowMax){
748 $this->shadowMax= "0";
749 }
750 if ($this->mustchangepassword){
751 $this->shadowLastChange= (int)(date("U") / 86400) - $this->shadowMax - 1;
752 } else {
753 $this->shadowLastChange= (int)(date("U") / 86400);
754 }
755 if (!$this->activate_shadowWarning){
756 $this->shadowWarning= "0";
757 }
759 /* Check what to do with ID's
760 Nothing forced, so we may have to generate our own IDs, if not done already.
761 */
762 if ($this->force_ids == 0){
764 /* Handle uidNumber.
765 * - use existing number if possible
766 * - if not, try to create a new uniqe one.
767 * */
768 if ($this->savedUidNumber != ""){
769 $this->uidNumber= $this->savedUidNumber;
770 } else {
772 /* Calculate new id's. We need to place a lock before calling get_next_id
773 to get real unique values.
774 */
775 $wait= 10;
776 while (get_lock("uidnumber") != ""){
777 sleep (1);
779 /* Oups - timed out */
780 if ($wait-- == 0){
781 msg_dialog::display(_("Warning"), _("Timeout while waiting for lock. Ignoring lock!"), WARNING_DIALOG);
782 break;
783 }
784 }
785 add_lock ("uidnumber", "gosa");
786 $this->uidNumber= get_next_id("uidNumber", $this->dn);
787 }
788 }
791 /* Handle gidNumber
792 * - If we do not have a primary group selected (automatic), we will check if there
793 * is already a group with the same name and use this as primary.
794 * - .. if we couldn't find a group with the same name, we will create a new one,
795 * using the users uid as cn and a generated uniqe gidNumber.
796 * */
797 if ($this->primaryGroup == 0 || $this->force_ids){
799 /* Search for existing group */
800 $ldap = $this->config->get_ldap_link();
801 $ldap->cd($this->config->current['BASE']);
803 /* Are we forced to use a special gidNumber? */
804 if($this->force_ids){
805 $ldap->search("(&(objectClass=posixGroup)(gidNumber=".$this->gidNumber."))", array("cn","gidNumber"));
806 }else{
807 $ldap->search("(&(objectClass=posixGroup)(gidNumber=*)(cn=".$this->uid."))", array("cn","gidNumber"));
808 }
810 /* No primary group found, create a new one */
811 if ($ldap->count() == 0){
813 $groupcn = $this->uid;
814 $pri_attr = $this->config->get_cfg_value("accountPrimaryAttribute");
815 $groupdn= preg_replace ('/^'.preg_quote($pri_attr,'/').'=[^,]+,'.preg_quote(get_people_ou(),'/').'/i',
816 'cn='.$groupcn.','.get_groups_ou(), $this->dn);
818 /* Request a new and uniqe gidNumber, if required */
819 if(!$this->force_ids){
820 $this->gidNumber= get_next_id("gidNumber", $this->dn);
821 }
823 /* If forced gidNumber could not be found, then check if the given group name already exists
824 we do not want to modify the gidNumber of an existing group.
825 */
826 $cnt= 0;
827 while($ldap->dn_exists($groupdn) && ($cnt < 100)){
828 $cnt ++;
829 $groupcn = $this->uid."_".$cnt;
830 $groupdn= preg_replace ('/^'.preg_quote($pri_attr,'/').'=[^,]+,'.preg_quote(get_people_ou(),'/').'/i',
831 'cn='.$groupcn.','.get_groups_ou(), $this->dn);
832 }
834 /* Create new primary group and enforce the new gidNumber */
835 $g= new group($this->config, $groupdn);
836 $g->cn= $groupcn;
837 $g->force_gid= 1;
838 $g->gidNumber= $this->gidNumber;
839 $g->description= _("Group of user")." ".$this->givenName." ".$this->sn;
840 $g->save ();
842 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
843 sprintf("Primary group '%s' created, using gidNumber '%s'.",$groupcn,$this->gidNumber),"");
844 }else{
845 $attrs = $ldap->fetch();
846 $this->gidNumber = $attrs['gidNumber'][0];
847 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
848 "Found and used: <i>".$attrs['dn']."</i>",
849 sprintf("Primary group '%s' exists, gidNumber is '%s'.",$this->uid,$this->gidNumber));
850 }
851 }else{
853 /* Primary group was selected by user
854 */
855 $this->gidNumber = $this->primaryGroup;
856 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
857 sprintf("Primary group '%s' for user '%s' manually selected.",$this->gidNumber,$this->uid),"");
858 }
860 if ($this->activate_shadowMin != "1" ) {
861 $this->shadowMin = "";
862 }
864 if (($this->activate_shadowMax != "1") && ($this->mustchangepassword != "1")) {
865 $this->shadowMax = "";
866 }
868 if ($this->activate_shadowWarning != "1" ) {
869 $this->shadowWarning = "";
870 }
872 if ($this->activate_shadowInactive != "1" ) {
873 $this->shadowInactive = "";
874 }
876 if ($this->activate_shadowExpire != "1" ) {
877 $this->shadowExpire = "";
878 }
880 /* Fill gecos */
881 if (isset($this->parent) && $this->parent !== NULL){
882 $this->gecos= rewrite($this->parent->by_object['user']->cn);
883 if (!preg_match('/^[a-z0-9 -]+$/i', $this->gecos)){
884 $this->gecos= "";
885 }
886 }
888 foreach(array("shadowMin","shadowMax","shadowWarning","shadowInactive","shadowExpire") as $attr){
889 $this->$attr = (int) $this->$attr;
890 }
891 /* Call parents save to prepare $this->attrs */
892 plugin::save();
894 /* Trust accounts */
895 $objectclasses= array();
896 foreach ($this->attrs['objectClass'] as $key => $class){
897 if (preg_match('/trustAccount/i', $class)){
898 continue;
899 }
900 $objectclasses[]= $this->attrs['objectClass'][$key];
901 }
902 $this->attrs['objectClass']= $objectclasses;
903 if ($this->trustModel != ""){
904 $this->attrs['objectClass'][]= "trustAccount";
905 $this->attrs['trustModel']= $this->trustModel;
906 $this->attrs['accessTo']= array();
907 if ($this->trustModel == "byhost"){
908 foreach ($this->accessTo as $host){
909 $this->attrs['accessTo'][]= $host;
910 }
911 }
912 } else {
913 if ($this->was_trust_account){
914 $this->attrs['accessTo']= array();
915 $this->attrs['trustModel']= array();
916 }
917 }
919 if(empty($this->attrs['gosaDefaultPrinter'])){
920 $thid->attrs['gosaDefaultPrinter']=array();
921 }
924 /* include global link_info */
925 $this->cleanup();
927 /* This is just a test, we have had duplicated ids
928 in the past when copy & paste was used.
929 Normaly this should not happen.
930 */
931 if(isset($this->attrs['uidNumber']) && !$this->force_ids){
932 $used = $this->get_used_uid_numbers();
933 if(isset($used[$this->attrs['uidNumber']]) && $used[$this->attrs['uidNumber']] != $this->dn){
934 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);
935 }
936 }
938 $ldap= $this->config->get_ldap_link();
939 $ldap->cd($this->dn);
940 unset($this->attrs['uid']);
941 $ldap->modify ($this->attrs);
943 /* Log last action */
944 if($this->initially_was_account){
945 new log("modify","users/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
946 }else{
947 new log("create","users/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
948 }
950 if (!$ldap->success()){
951 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_MOD, get_class()));
952 }
954 /* Remove lock needed for unique id generation */
955 del_lock ("uidnumber");
957 // Save ssh stuff if needed
958 if ($this->ssh) {
959 $this->ssh->setDN($this->dn);
960 $this->ssh->save();
961 }
963 /* Take care about groupMembership values: add to groups */
964 foreach ($this->groupMembership as $key => $value){
965 if (!isset($this->savedGroupMembership[$key])){
966 $g= new grouptabs($this->config,$this->config->data['TABS']['GROUPTABS'], $key,"groups");
967 $g->set_acl_base($key);
968 $g->by_object['group']->addUser($this->uid);
969 $g->save();
970 }
971 }
973 /* Remove groups not listed in groupMembership */
974 foreach ($this->savedGroupMembership as $key => $value){
975 if (!isset($this->groupMembership[$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']->removeUser ($this->uid);
979 $g->save();
980 }
981 }
983 /* Optionally execute a command after we're done */
984 if ($this->initially_was_account == $this->is_account){
985 if ($this->is_modified){
986 $this->handle_post_events("modify",array("uid" => $this->uid));
987 }
988 } else {
989 $this->handle_post_events("add" ,array("uid"=> $this->uid));
990 }
991 }
994 /* Check formular input */
995 function check()
996 {
997 /* Include global link_info */
998 $ldap= $this->config->get_ldap_link();
1000 /* Append groups as memberGroup: to check hook
1001 */
1002 $tmp_attributes = $this->attributes;
1003 $this->attributes[] = "memberGroup";
1004 $this->memberGroup = array();
1005 foreach($this->groupMembership as $dn => $name){
1006 $this->memberGroup[] = $name;
1007 }
1009 /* Call common method to give check the hook */
1010 $message= plugin::check();
1011 $this->attributes = $tmp_attributes;
1013 /* must: homeDirectory */
1014 if ($this->homeDirectory == ""){
1015 $message[]= msgPool::required(_("Home directory"));
1016 }
1017 if (!tests::is_path($this->homeDirectory)){
1018 $message[]= msgPool::invalid(_("Home directory"), "", "", "/home/yourname" );
1019 }
1021 /* Check ID's if they are forced by user */
1022 if ($this->force_ids == "1"){
1024 /* Valid uid/gid? */
1025 if (!tests::is_id($this->uidNumber)){
1026 $message[]= msgPool::invalid(_("UID"), $this->uidNumber, "/[0-9]/");
1027 } else {
1028 if ($this->uidNumber < $this->config->get_cfg_value("minId")){
1029 $message[]= msgPool::toosmall(_("UID"), $this->config->get_cfg_value("minId"));
1030 }
1031 }
1032 if (!tests::is_id($this->gidNumber)){
1033 $message[]= msgPool::invalid(_("GID"), $this->gidNumber, "/[0-9]/");
1034 } else {
1035 if ($this->gidNumber < $this->config->get_cfg_value("minId")){
1036 $message[]= msgPool::toosmall(_("GID"), $this->config->get_cfg_value("minId"));
1037 }
1038 }
1039 }
1041 /* Check dates */
1042 if ($this->activate_shadowExpire && ($this->shadowExpire == "" || !tests::is_date($this->shadowExpire))){
1043 $message[]= msgPool::invalid("shadowExpire", $this->shadowExpire);
1044 }
1046 /* Check shadow settings, well I like spaghetties... */
1047 if ($this->activate_shadowMin){
1048 if (!tests::is_id($this->shadowMin)){
1049 $message[]= msgPool::invalid(_("shadowMin"), $this->shadowMin, "/[0-9]/");
1050 }
1051 }
1052 if ($this->activate_shadowMax){
1053 if (!tests::is_id($this->shadowMax)){
1054 $message[]= msgPool::invalid(_("shadowMax"), $this->shadowMax, "/[0-9]/");
1055 }
1056 }
1057 if ($this->activate_shadowWarning){
1058 if (!tests::is_id($this->shadowWarning)){
1059 $message[]= msgPool::invalid(_("shadowWarning"), $this->shadowWarning, "/[0-9]/");
1060 }
1061 if (!$this->activate_shadowMax){
1062 $message[]= msgPool::depends("shadowWarning", "shadowMax");
1063 }
1064 if ($this->shadowWarning > $this->shadowMax){
1065 $message[]= msgPool::toobig("shadowWarning", "shadowMax");
1066 }
1067 if ($this->activate_shadowMin && $this->shadowWarning < $this->shadowMin){
1068 $message[]= msgPool::toosmall("shadowWarning", "shadowMin");
1069 }
1070 }
1071 if ($this->activate_shadowInactive){
1072 if (!tests::is_id($this->shadowInactive)){
1073 $message[]= msgPool::invalid(_("shadowInactive"), $this->shadowInactive, "/[0-9]/");
1074 }
1075 if (!$this->activate_shadowMax){
1076 $message[]= msgPool::depends("shadowInactive", "shadowMax");
1077 }
1078 }
1079 if ($this->activate_shadowMin && $this->activate_shadowMax){
1080 if ($this->shadowMin > $this->shadowMax){
1081 $message[]= msgPool::toobig("shadowMin", "shadowMax");
1082 }
1083 }
1085 return ($message);
1086 }
1089 function multiple_check()
1090 {
1091 $message = plugin::multiple_check();
1092 if ($this->homeDirectory == "" && in_array("homeDirectory",$this->multi_boxes)){
1093 $message[]= msgPool::required(_("Home directory"));
1094 }
1095 if (!tests::is_path($this->homeDirectory) && in_array("homeDirectory",$this->multi_boxes)){
1096 $message[]= msgPool::invalid(_("Home directory"), "", "", "/home/yourname" );
1097 }
1099 /* Check shadow settings, well I like spaghetties... */
1100 if ($this->activate_shadowMin && in_array("activate_shadowMin",$this->multi_boxes)){
1101 if (!tests::is_id($this->shadowMin)){
1102 $message[]= msgPool::invalid(_("shadowMin"), $this->shadowMin, "/[0-9]/");
1103 }
1104 }
1105 if ($this->activate_shadowMax && in_array("activate_shadowMax",$this->multi_boxes)){
1106 if (!tests::is_id($this->shadowMax)){
1107 $message[]= msgPool::invalid(_("shadowMax"), $this->shadowMax, "/[0-9]/");
1108 }
1109 }
1110 if ($this->activate_shadowWarning && in_array("activate_shadowWarning",$this->multi_boxes)){
1111 if (!tests::is_id($this->shadowWarning)){
1112 $message[]= msgPool::invalid(_("shadowWarning"), $this->shadowWarning, "/[0-9]/");
1113 }
1114 if (!$this->activate_shadowMax && in_array("activate_shadowMax",$this->multi_boxes)){
1115 $message[]= msgPool::depends("shadowWarning", "shadowMax");
1116 }
1117 if ($this->shadowWarning > $this->shadowMax && in_array("activate_shadowWarning",$this->multi_boxes)){
1118 $message[]= msgPool::toobig("shadowWarning", "shadowMax");
1119 }
1120 if ($this->activate_shadowMin && $this->shadowWarning < $this->shadowMin && in_array("activate_shadowMin",$this->multi_boxes)){
1121 $message[]= msgPool::tosmall("shadowWarning", "shadowMin");
1122 }
1123 }
1124 if ($this->activate_shadowInactive && in_array("activate_shadowInactive",$this->multi_boxes)){
1125 if (!tests::is_id($this->shadowInactive)){
1126 $message[]= msgPool::invalid(_("shadowInactive"), $this->shadowInactive, "/[0-9]/");
1127 }
1128 if (!$this->activate_shadowMax && in_array("activate_shadowMax",$this->multi_boxes)){
1129 $message[]= msgPool::depends("shadowInactive", "shadowMax");
1130 }
1131 }
1132 if ($this->activate_shadowMin && $this->activate_shadowMax && in_array("activate_shadowMin",$this->multi_boxes)){
1133 if ($this->shadowMin > $this->shadowMax){
1134 $message[]= msgPool::toobig("shadowMin", "shadowMax");
1135 }
1136 }
1138 return($message);
1139 }
1142 function addGroup ($groups)
1143 {
1144 /* include global link_info */
1145 $ldap= $this->config->get_ldap_link();
1147 /* Walk through groups and add the descriptive entry if not exists */
1148 foreach ($groups as $value){
1150 if (!array_key_exists($value, $this->groupMembership)){
1151 $ldap->cat($value, array('cn', 'description', 'dn'));
1152 $attrs= $ldap->fetch();
1153 error_reporting (0);
1154 if (!isset($attrs['description'][0])){
1155 $entry= $attrs["cn"][0];
1156 } else {
1157 $dsc= preg_replace ('/^Group of user/', _("Group of user"), $attrs["description"][0]);
1158 $entry= $attrs["cn"][0]." [$dsc]";
1159 }
1160 error_reporting (E_ALL | E_STRICT);
1162 if(obj_is_writable($attrs['dn'],"groups/group","memberUid")){
1163 $this->groupMembership[$attrs['dn']]= $entry;
1164 if($this->multiple_support_active && isset($this->groupMembership_some[$attrs['dn']])){
1165 unset($this->groupMembership_some[$attrs['dn']]);
1166 }
1167 }
1168 }
1169 }
1171 /* Sort groups */
1172 asort ($this->groupMembership);
1173 reset ($this->groupMembership);
1174 }
1177 /* Del posix user from some groups */
1178 function delGroup ($groups)
1179 {
1180 $dest= array();
1181 foreach($groups as $dn_to_del){
1182 if(isset($this->groupMembership[$dn_to_del]) && obj_is_writable($dn_to_del,"groups/group","memberUid")){
1183 unset($this->groupMembership[$dn_to_del]);
1184 }
1185 if($this->multiple_support_active){
1186 if(isset($this->groupMembership_some[$dn_to_del]) && obj_is_writable($dn_to_del,"groups/group","memberUid")){
1187 unset($this->groupMembership_some[$dn_to_del]);
1188 }
1189 }
1190 }
1191 }
1194 /* Adapt from template, using 'dn' */
1195 function adapt_from_template($dn, $skip= array())
1196 {
1197 /* Include global link_info */
1198 $ldap= $this->config->get_ldap_link();
1200 plugin::adapt_from_template($dn, $skip);
1201 $template= $this->attrs['uid'][0];
1203 /* Adapt group membership */
1204 $ldap->cd($this->config->current['BASE']);
1205 $ldap->search("(&(objectClass=posixGroup)(memberUid=".$this->attrs["uid"][0]."))", array("description", "cn"));
1207 while ($this->attrs= $ldap->fetch()){
1208 if (!isset($this->attrs["description"][0])){
1209 $entry= $this->attrs["cn"][0];
1210 } else {
1211 $entry= $this->attrs["cn"][0]." [".$this->attrs["description"][0]."]";
1212 }
1213 $this->groupMembership[$ldap->getDN()]= $entry;
1214 }
1216 /* Fix primary group settings */
1217 $ldap->cd($this->config->current['BASE']);
1218 $ldap->search("(&(objectClass=posixGroup)(cn=$template)(gidNumber=".$this->gidNumber."))", array("cn"));
1219 if ($ldap->count() != 1){
1220 $this->primaryGroup= $this->gidNumber;
1221 }
1223 $ldap->cd($this->config->current['BASE']);
1224 $ldap->search("(&(objectClass=gosaUserTemplate)(uid=".$template.")(accessTo=*))", array("cn","accessTo"));
1225 while($attr = $ldap->fetch()){
1226 $tmp = $attr['accessTo'];
1227 unset ($tmp['count']);
1228 $this->accessTo = $tmp;
1229 }
1231 /* Adjust shadow checkboxes */
1232 foreach (array("shadowMin", "shadowMax", "shadowWarning", "shadowInactive") as $val){
1233 if ($this->$val != 0){
1234 $oval= "activate_".$val;
1235 $this->$oval= "1";
1236 }
1237 }
1239 /* FIXME: NEED review of this section */
1240 /* Need to check shadowExpire separately */
1242 /*
1243 * If shadowExpire is not enabled in the template, it's a UNIX timestamp - so don't convert it to seconds.
1244 * The check is a hack - if difference between timestamp generated above and here is max 1 day.
1245 */
1246 if(abs($this->shadowExpire - time())>86400) {
1247 $this->shadowExpire= $this->convertToSeconds($this->shadowExpire);
1248 }
1250 /* Only enable checkbox, if shadowExpire is in the future */
1251 if($this->shadowExpire > time()) {
1252 $this->activate_shadowExpire= "1";
1253 }
1254 }
1256 function convertToSeconds($val)
1257 {
1258 if ($val != 0){
1259 $val*= 60 * 60 * 24;
1260 } else {
1261 $date= getdate();
1262 $val= floor($date[0] / (60*60*24)) * 60 * 60 * 24;
1263 }
1264 return($val);
1265 }
1268 function get_used_uid_numbers()
1269 {
1270 $ids= array();
1271 $ldap= $this->config->get_ldap_link();
1273 $ldap->cd ($this->config->current['BASE']);
1274 $ldap->search ("(&(objectClass=posixAccount)(uidNumber=*))", array("uidNumber"));
1276 /* Get list of ids */
1277 while ($attrs= $ldap->fetch()){
1278 $ids[$attrs['uidNumber'][0]] = $attrs['dn'];
1279 }
1280 return($ids);
1281 }
1284 function reload()
1285 {
1286 /* Set base for all searches */
1287 $base = session::get('CurrentMainBase');
1288 $base = $base;
1289 $ldap = $this->config->get_ldap_link();
1290 $attrs = array("cn", "description", "gidNumber");
1291 $Flags = GL_SIZELIMIT;
1293 /* Get groups */
1294 if ($this->GroupUserRegex == '*'){
1295 $filter = "(&(objectClass=posixGroup)(cn=".$this->GroupRegex."))";
1296 } else {
1297 $filter= "(&(objectClass=posixGroup)(cn=".$this->GroupRegex.")(memberUid=".$this->GroupUserRegex."))";
1298 }
1299 if($this->SubSearch){
1300 $Flags |= GL_SUBSEARCH;
1301 }else{
1302 $base = get_groups_ou().$base;
1303 }
1305 $res= get_list($filter, "groups", $base,$attrs, $Flags);
1307 /* check sizelimit */
1308 if (preg_match("/size limit/i", $ldap->get_error())){
1309 session::set('limit_exceeded',TRUE);
1310 }
1312 /* Create a list of users */
1313 $this->grouplist = array();
1314 foreach ($res as $value){
1315 $this->grouplist[$value['gidNumber'][0]]= $value;
1316 }
1318 $tmp=array();
1319 foreach($this->grouplist as $tkey => $val ){
1320 $tmp[strtolower($val['cn'][0]).$val['cn'][0]]=$val;
1321 }
1323 /* Sort index */
1324 ksort($tmp);
1326 /* Recreate index array[dn]=cn[description]*/
1327 $this->grouplist=array();
1328 foreach($tmp as $val){
1329 if(isset($val['description'])){
1330 $this->grouplist[$val['dn']]=$val['cn'][0]." [".$val['description'][0]."]";
1331 }else{
1332 $this->grouplist[$val['dn']]=$val['cn'][0];
1333 }
1334 }
1336 reset ($this->grouplist);
1337 }
1340 /* Get posts from copy & paste dialog */
1341 function saveCopyDialog()
1342 {
1343 if(isset($_POST['homeDirectory'])){
1344 $this->homeDirectory = $_POST['homeDirectory'];
1345 if (isset ($_POST['force_ids'])){
1346 $data= 1;
1347 $this->gidNumber = $_POST['gidNumber'];
1348 $this->uidNumber = $_POST['uidNumber'];
1349 } else {
1350 $data= 0;
1351 }
1352 if ($this->force_ids != $data){
1353 $this->is_modified= TRUE;
1354 }
1355 $this->force_ids= $data;
1356 $data= $_POST['primaryGroup'];
1357 if ($this->primaryGroup != $data){
1358 $this->is_modified= TRUE;
1359 }
1360 $this->primaryGroup= $_POST['primaryGroup'];
1361 }
1362 }
1365 /* Create the posix dialog part for copy & paste */
1366 function getCopyDialog()
1367 {
1368 /* Skip dialog creation if this is not a valid account*/
1369 if(!$this->is_account) return("");
1370 if ($this->force_ids == 1){
1371 $force_ids = "checked";
1372 if (session::get('js')){
1373 $forceMode = "";
1374 }
1375 } else {
1376 if (session::get('js')){
1377 if($this->acl != "#none#")
1378 $forceMode ="disabled";
1379 }
1380 $force_ids = "";
1381 }
1383 $sta = "";
1385 /* Open group add dialog */
1386 if(isset($_POST['edit_groupmembership'])){
1387 $this->groupSelect = new groupSelect($this->config,get_userinfo());
1388 $sta = "SubDialog";
1389 }
1391 /* If the group-add dialog is closed, call execute
1392 to ensure that the membership is updatd */
1393 if(isset($_POST['add_groups_finish']) || isset($_POST['add_groups_cancel'])){
1394 $this->execute();
1395 $this->groupSelect =NULL;
1396 }
1398 if($this->groupSelect){
1399 $str = $this->execute(true);
1400 $ret = array();
1401 $ret['string'] = $str;
1402 $ret['status'] = $sta;
1403 return($ret);
1404 }
1406 /* If a group member should be deleted, simply call execute */
1407 if(isset($_POST['delete_groupmembership'])){
1408 $this->execute();
1409 }
1411 /* Assigned informations to smarty */
1412 $smarty = get_smarty();
1413 $smarty->assign("homeDirectory",$this->homeDirectory);
1414 $smarty->assign("secondaryGroups",$this->secondaryGroups);
1415 $smarty->assign("primaryGroup",$this->primaryGroup);
1417 $smarty->assign("uidNumber",$this->uidNumber);
1418 $smarty->assign("gidNumber",$this->gidNumber);
1419 $smarty->assign("forceMode",$forceMode);
1420 $smarty->assign("force_ids",$force_ids);
1421 if (!count($this->groupMembership)){
1422 $smarty->assign("groupMembership", array(" "));
1423 } else {
1424 $smarty->assign("groupMembership", $this->groupMembership);
1425 }
1427 /* Display wars message if there are more than 16 group members */
1428 if (count($this->groupMembership) > 16){
1429 $smarty->assign("groups", "too_many_for_nfs");
1430 } else {
1431 $smarty->assign("groups", "");
1432 }
1433 $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE,dirname(__FILE__)));
1435 $ret = array();
1436 $ret['string'] = $str;
1437 $ret['status'] = $sta;
1438 return($ret);
1439 }
1442 function PrepareForCopyPaste($source)
1443 {
1444 plugin::PrepareForCopyPaste($source);
1446 /* Avoid using the same gid/uid number as source user
1447 empty numbers to enforce new ones. */
1448 $this->savedUidNumber = "";
1449 $this->savedGidNumber = "";
1451 /* Get group membership */
1452 $ldap = $this->config->get_ldap_link();
1453 $ldap->cd($this->config->current['BASE']);
1454 $ldap->search("(&(objectClass=posixGroup)(memberUid=".$source['uid'][0]."))", array("cn", "description"));
1456 while ($attrs= $ldap->fetch()){
1457 if (!isset($attrs["description"][0])){
1458 $entry= $attrs["cn"][0];
1459 } else {
1460 $entry= $attrs["cn"][0]." [".$attrs["description"][0]."]";
1461 }
1462 $this->groupMembership[$ldap->getDN()]= $entry;
1463 }
1464 asort($this->groupMembership);
1465 reset($this->groupMembership);
1467 /* Fill group */
1468 if(isset($source['gidNumber'][0])){
1469 $this->primaryGroup= $source['gidNumber'][0];
1470 }
1472 }
1475 function multiple_execute()
1476 {
1477 return($this->execute());
1478 }
1481 static function plInfo()
1482 {
1483 return (array(
1484 "plDescription" => _("POSIX account"),
1485 "plSelfModify" => TRUE,
1486 "plDepends" => array("user"),
1487 "plPriority" => 2,
1488 "plSection" => array("personal" => _("My account")),
1489 "plCategory" => array("users"),
1490 "plOptions" => array(),
1492 "plProvidedAcls" => array(
1494 "homeDirectory" => _("Home directory"),
1495 "loginShell" => _("Shell"),
1496 "uidNumber" => _("User ID"),
1497 "gidNumber" => _("Group ID"),
1499 "mustchangepassword"=> _("Force password change on login"),
1500 "shadowMin" => _("Shadow min"),
1501 "shadowMax" => _("Shadow max"),
1502 "shadowWarning" => _("Shadow warning"),
1503 "shadowInactive" => _("Shadow inactive"),
1504 "shadowExpire" => _("Shadow expire"),
1505 "sshPublickey" => _("Public SSH key"),
1506 "trustModel" => _("System trust model")))
1507 );
1508 }
1511 /* Return selected values for multiple edit */
1512 function get_multi_edit_values()
1513 {
1514 $ret = plugin::get_multi_edit_values();
1515 $ret['groupMembership'] = $this->groupMembership;
1516 $ret['groupMembership_some']= $this->groupMembership_some;
1518 if(in_array("primaryGroup",$this->multi_boxes)){
1519 $ret['primaryGroup'] = $this->primaryGroup;
1520 }
1521 if(in_array("trustmode",$this->multi_boxes)){
1522 $ret['trustModel'] = $this->trustModel;
1523 $ret['accessTo'] = $this->accessTo;
1524 }
1525 foreach(array("shadowWarning","shadowInactive","shadowMin","shadowMax", "shadowExpire") as $entry){
1526 $active = "activate_".$entry;
1527 if(in_array($active,$this->multi_boxes)){
1528 $ret[$entry] = $this->$entry;
1529 $ret[$active] = $this->$active;
1530 }
1531 }
1532 if(in_array("mustchangepassword",$this->multi_boxes)){
1533 $ret['mustchangepassword'] = $this->mustchangepassword;
1534 }
1535 return($ret);
1536 }
1539 /* Save posts for multiple edit
1540 */
1541 function multiple_save_object()
1542 {
1543 if(isset($_POST['posix_mulitple_edit'])){
1545 /* Backup expire value */
1546 $expire_tmp = $this->shadowExpire;
1548 /* Update all values */
1549 plugin::multiple_save_object();
1551 /* Get selected checkboxes */
1552 foreach(array("primaryGroup","trustmode","mustchangepassword","activate_shadowWarning","activate_shadowInactive","activate_shadowMin", "activate_shadowMax","activate_shadowExpire") as $val){
1553 if(isset($_POST["use_".$val])){
1554 $this->multi_boxes[] = $val;
1555 }
1556 }
1558 /* Update special values, checkboxes for posixShadow */
1559 foreach(array("shadowMin","shadowMax","shadowExpire","shadowInactive","shadowWarning") as $var) {
1560 if($this->acl_is_writeable($var)){
1561 $activate_var = "activate_".$var;
1562 if(in_array($activate_var, $this->multi_boxes)){
1563 if(isset($_POST['activate_'.$var])){
1564 $this->$activate_var = true;
1565 $this->$var = $_POST[$var];
1566 }else{
1567 $this->$activate_var = false;
1568 $this->$var = 0;
1569 }
1570 }
1571 }
1572 }
1574 /* Restore shadow value, if the shadow attribute isn't used */
1575 if(!in_array("activate_shadowExpire",$this->multi_boxes)){
1576 $this->shadowExpire = $expire_tmp;
1577 }
1579 /* Force change password ? */
1580 if(isset($_POST['mustchangepassword'])){
1581 $this->mustchangepassword = TRUE;
1582 }else{
1583 $this->mustchangepassword = FALSE;
1584 }
1586 /* Trust mode - special handling */
1587 if($this->acl_is_writeable("trustModel")){
1588 if (isset($_POST['trustmode'])){
1589 $saved= $this->trustModel;
1590 if ($_POST['trustmode'] == "1"){
1591 $this->trustModel= "fullaccess";
1592 } elseif ($_POST['trustmode'] == "2"){
1593 $this->trustModel= "byhost";
1594 } else {
1595 $this->trustModel= "";
1596 }
1597 if ($this->trustModel != $saved){
1598 $this->is_modified= TRUE;
1599 }
1600 }
1601 }
1603 /* Save primary group settings */
1604 if($this->acl_is_writeable("primaryGroup") && isset($_POST['primaryGroup'])){
1605 $data= $_POST['primaryGroup'];
1606 if ($this->primaryGroup != $data){
1607 $this->is_modified= TRUE;
1608 }
1609 $this->primaryGroup= $_POST['primaryGroup'];
1610 }
1611 }
1612 }
1615 /* Initialize plugin with given atribute arrays
1616 */
1617 function init_multiple_support($attrs,$all)
1618 {
1619 plugin::init_multiple_support($attrs,$all);
1621 /* Some dummy values */
1622 $groups_some = array();
1623 $groups_all = array();
1624 $groups_uid = array();
1625 $uids = array();
1626 $first = TRUE;
1628 /* Get all groups used by currently edited users */
1629 $uid_filter="";
1630 for($i =0; $i < $this->multi_attrs_all['uid']['count'] ; $i ++){
1631 $uid = $this->multi_attrs_all['uid'][$i];
1632 $uids[] = $uid;
1633 $uid_filter.= "(memberUid=".$uid.")";
1634 }
1635 $uid_filter = "(&(objectClass=posixGroup)(|".$uid_filter."))";
1636 $ldap = $this->config->get_ldap_link();
1637 $ldap->cd($this->config->current['BASE']);
1638 $ldap->search($uid_filter,array("dn","cn","memberUid"));
1639 while($group = $ldap->fetch()){
1640 $groups_some[$group['dn']] = $group['cn'][0];
1641 for($i = 0 ; $i < $group['memberUid']['count'] ; $i++){
1642 $groups_uid[$group['dn']][] = $group['memberUid'][$i];
1643 }
1644 }
1646 /* Create an array, containing all used groups */
1647 $groups_all = $groups_some;
1648 foreach($groups_all as $id => $group){
1649 foreach($uids as $uid){
1650 if(!in_array($uid,$groups_uid[$id])){
1651 unset($groups_all[$id]);
1652 break;
1653 }
1654 }
1655 }
1657 /* Assign group array */
1658 $this->groupMembership = $groups_all;
1660 /* Create an array of all grouops used by all users */
1661 foreach( $groups_all as $dn => $cn){
1662 if(isset($groups_some[$dn])){
1663 unset($groups_some[$dn]);
1664 }
1665 }
1666 $this->groupMembership_some = $groups_some;
1667 $this->primaryGroup = $this->gidNumber;
1669 /* Is this account a trustAccount? */
1670 if (isset($this->multi_attrs['trustModel'])){
1671 $this->trustModel= $this->multi_attrs['trustModel'][0];
1672 $this->was_trust_account= TRUE;
1673 $this->multi_boxes[] = "trustmode";
1674 } else {
1675 $this->was_trust_account= FALSE;
1676 $this->trustModel= "";
1677 }
1679 /* Create access informations */
1680 $this->accessTo = array();
1681 if (isset($this->multi_attrs['accessTo'])){
1682 for ($i= 0; $i<$this->multi_attrs['accessTo']['count']; $i++){
1683 $tmp= $this->multi_attrs['accessTo'][$i];
1684 $this->accessTo[$tmp]= $tmp;
1685 }
1686 }
1688 /* Adjust shadow checkboxes */
1689 foreach (array("shadowMin", "shadowMax", "shadowWarning", "shadowInactive",
1690 "shadowExpire") as $val){
1691 if ($this->$val != 0){
1692 $oval= "activate_".$val;
1693 $this->$oval= "1";
1694 }
1695 }
1697 /* Convert to seconds */
1698 if(isset($this->multi_attrs['shadowExpire'])){
1699 $this->shadowExpire = $this->convertToSeconds($this->multi_attrs['shadowExpire'][0]);
1700 }else{
1701 $this->activate_shadowExpire = FALSE;
1702 }
1703 }
1706 function set_multi_edit_values($attrs)
1707 {
1708 $groups = array();
1710 /* Update groupMembership, keep optinal group */
1711 foreach($attrs['groupMembership_some'] as $dn => $cn){
1712 if(isset($this->groupMembership[$dn])){
1713 $groups[$dn] = $cn;
1714 }
1715 }
1716 /* Update groupMembership, add forced groups */
1717 foreach($attrs['groupMembership'] as $dn => $cn){
1718 $groups[$dn] = $cn;
1719 }
1720 plugin::set_multi_edit_values($attrs);
1721 $this->groupMembership = $groups;
1722 }
1723 }
1725 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1726 ?>