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 $trustSelect= 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 $this->ui = get_userinfo();
257 }
260 /* execute generates the html output for this node */
261 function execute($isCopyPaste = false)
262 {
263 /* Call parent execute */
264 plugin::execute();
265 $display= "";
267 /* Log view */
268 if($this->is_account && !$this->view_logged){
269 $this->view_logged = TRUE;
270 new log("view","users/".get_class($this),$this->dn);
271 }
273 if($this->multiple_support_active){
274 $this->is_account = TRUE;
275 }
277 if(!$isCopyPaste && ! $this->multiple_support_active){
279 /* Do we need to flip is_account state? */
280 if(isset($_POST['modify_state'])){
281 if($this->is_account && $this->acl_is_removeable()){
282 $this->is_account= FALSE;
283 }elseif(!$this->is_account && $this->acl_is_createable()){
284 $this->is_account= TRUE;
285 }
286 }
288 /* Do we represent a valid posixAccount? */
289 if (!$this->is_account && $this->parent === NULL ){
290 $display= "<img alt=\"\" src=\"images/small-error.png\" align=\"middle\"> <b>".
291 msgPool::noValidExtension(_("POSIX"))."</b>";
292 $display.= back_to_main();
293 return ($display);
294 }
297 /* Show tab dialog headers */
298 if ($this->parent !== NULL){
299 if ($this->is_account){
300 if (isset($this->parent->by_object['sambaAccount'])){
301 $obj= $this->parent->by_object['sambaAccount'];
302 }
303 if (isset($obj) && $obj->is_account == TRUE &&
304 ((isset($this->parent->by_object['sambaAccount']))&&($this->parent->by_object['sambaAccount']->is_account))
305 ||(isset($this->parent->by_object['environment'] ))&&($this->parent->by_object['environment'] ->is_account)){
307 /* Samba3 dependency on posix accounts are enabled
308 in the moment, because I need to rely on unique
309 uidNumbers. There'll be a better solution later
310 on. */
311 $display= $this->show_disable_header(msgPool::removeFeaturesButton(_("POSIX")), msgPool::featuresEnabled(_("POSIX"), array(_("Samba"), _("Environment"))), TRUE);
312 } else {
313 $display= $this->show_disable_header(msgPool::removeFeaturesButton(_("POSIX")), msgPool::featuresEnabled(_("POSIX")));
314 }
315 } else {
316 $display= $this->show_enable_header(msgPool::addFeaturesButton(_("POSIX")), msgPool::featuresDisabled(_("POSIX")));
317 return($display);
318 }
319 }
320 }
322 // Display dialog to allow selection of groups
323 if (isset($_POST['edit_groupmembership'])){
324 $this->groupSelect = new groupSelect($this->config,get_userinfo());
325 $this->dialog= TRUE;
326 }
328 // Allow to select trusted machines from a list
329 if (isset($_POST["add_ws"])){
330 $this->trustSelect= new trustSelect($this->config,get_userinfo());
331 $this->dialog= TRUE;
332 }
334 // Cancel trust and group dialog
335 if (isset($_POST['add_groups_cancel']) || isset($_POST['add_ws_cancel'])){
336 $this->groupSelect= NULL;
337 $this->trustSelect= NULL;
338 $this->dialog= FALSE;
339 }
341 // Add groups selected in groupSelect dialog to ours.
342 if (isset($_POST['add_groups_finish']) && $this->groupSelect){
343 $groups = $this->groupSelect->detectPostActions();
344 if(isset($groups['targets'])){
345 $this->addGroup ($groups['targets']);
346 $this->is_modified= TRUE;
347 }
348 $this->groupSelect= NULL;
349 $this->dialog= FALSE;
350 }
352 // Remove groups from currently selected groups.
353 if (isset($_POST['delete_groupmembership']) &&
354 isset($_POST['group_list']) && count($_POST['group_list'])){
356 $this->delGroup ($_POST['group_list']);
357 }
359 // Add selected machines to trusted ones.
360 if (isset($_POST["add_ws_finish"]) && $this->trustSelect){
361 $trusts = $this->trustSelect->detectPostActions();
362 if(isset($trusts['targets'])){
364 $headpage = $this->trustSelect->getHeadpage();
365 foreach($trusts['targets'] as $id){
366 $attrs = $headpage->getEntry($id);
367 $this->accessTo[$attrs['cn'][0]]= $attrs['cn'][0];
368 }
369 ksort($this->accessTo);
370 $this->is_modified= TRUE;
371 }
372 $this->trustSelect= NULL;
373 $this->dialog= FALSE;
374 }
376 // Remove machine from trusted ones.
377 if (isset($_POST["delete_ws"]) && isset($_POST['workstation_list'])){
378 foreach($_POST['workstation_list'] as $name){
379 unset ($this->accessTo[$name]);
380 }
381 $this->is_modified= TRUE;
382 }
386 /* Templates now! */
387 $smarty= get_smarty();
388 $smarty->assign("usePrototype", "true");
390 /* Show ws dialog */
391 if ($this->trustSelect){
393 // Build up blocklist
394 session::set('filterBlacklist', array('cn' => array_values($this->accessTo)));
395 return($this->trustSelect->execute());
396 }
398 /* Manage group add dialog */
399 if ($this->groupSelect){
401 // Build up blocklist
402 session::set('filterBlacklist', array('dn' => array_keys($this->groupMembership)));
403 return($this->groupSelect->execute());
404 }
406 // Handle ssh dialog?
407 if ($this->ssh instanceOf sshPublicKey && preg_match('/[rw]/', $this->getacl("sshPublicKey"))) {
408 $smarty->assign("usePrototype", "false");
409 if ($result= $this->ssh->execute()) {
410 $this->dialog= true;
411 return $result;
412 }
413 $this->dialog= false;
414 }
417 /* Show main page */
418 $smarty= get_smarty();
419 $smarty->assign("usePrototype", "true");
421 $smarty->assign("sshPublicKeyACL", $this->getacl("sshPublicKey"));
423 /* Depending on pwmode, currently hardcoded because there are no other methods */
424 if ( 1 == 1 ){
425 $smarty->assign("pwmode", dirname(__FILE__)."/posix_shadow");
427 $shadowMinACL = $this->getacl("shadowMin");
428 $smarty->assign("shadowmins", sprintf(_("Password can't be changed up to %s days after last change"),
429 "<input type='text' name=\"shadowMin\" size=3 maxlength=4 value=\"".$this->shadowMin."\">"));
431 $shadowMaxACL = $this->getacl("shadowMax");
432 $smarty->assign("shadowmaxs", sprintf(_("Password must be changed after %s days"),
433 "<input type='text' name=\"shadowMax\" size=3 maxlength=4 value=\"".$this->shadowMax."\">"));
435 $shadowInactiveACL= $this->getacl("shadowInactive");
436 $smarty->assign("shadowinactives", sprintf(_("Disable account after %s days of inactivity after password expiry"),
437 "<input type='text' name=\"shadowInactive\" size=3 maxlength=4 value=\"".$this->shadowInactive."\">"));
439 $shadowWarningACL = $this->getacl("shadowWarning");
440 $smarty->assign("shadowwarnings", sprintf(_("Warn user %s days before password expiry"),
441 "<input type='text' name=\"shadowWarning\" size=3 maxlength=4 value=\"".$this->shadowWarning."\">"));
443 foreach( array("activate_shadowMin", "activate_shadowMax",
444 "activate_shadowExpire", "activate_shadowInactive","activate_shadowWarning") as $val){
445 if ($this->$val == 1){
446 $smarty->assign("$val", "checked");
447 } else {
448 $smarty->assign("$val", "");
449 }
450 $smarty->assign("$val"."ACL", $this->getacl($val));
451 }
453 $smarty->assign("mustchangepasswordACL", $this->getacl("mustchangepassword"));
454 }
456 // Set last system login
457 $smarty->assign("gotoLastSystemLogin",$this->gotoLastSystemLogin);
459 /* Fill arrays */
460 $smarty->assign("shells", $this->loginShellList);
461 $smarty->assign("secondaryGroups", $this->secondaryGroups);
462 $smarty->assign("primaryGroup", $this->primaryGroup);
463 if(!$this->multiple_support_active){
464 if (!count($this->groupMembership)){
465 $smarty->assign("groupMembership", array(" "));
466 } else {
467 $smarty->assign("groupMembership", $this->groupMembership);
468 }
469 }else{
470 $smarty->assign("groupMembership", $this->groupMembership);
471 $smarty->assign("groupMembership_some", $this->groupMembership_some);
472 }
473 if (count($this->groupMembership) > 16){
474 $smarty->assign("groups", "too_many_for_nfs");
475 } else {
476 $smarty->assign("groups", "");
477 }
479 /* Avoid "Undefined index: forceMode" */
480 $smarty->assign("forceMode", "");
482 /* Checkboxes */
483 if ($this->force_ids == 1){
484 $smarty->assign("force_ids", "checked");
485 if (session::get('js')){
486 $smarty->assign("forceMode", "");
487 }
488 } else {
489 if (session::get('js')){
490 $smarty->assign("forceMode", "disabled");
491 }
492 $smarty->assign("force_ids", "");
493 }
495 /* Create onClick="" action string for the "Force UID/GID" option
496 */
497 $onClickIDS ="";
498 if(preg_match("/w/",$this->getacl("uidNumber"))){
499 $onClickIDS .= "changeState('uidNumber');";
500 }
501 if(preg_match("/w/",$this->getacl("gidNumber"))){
502 $onClickIDS .= "changeState('gidNumber');";
503 }
504 $smarty->assign("onClickIDS", $onClickIDS);
505 $smarty->assign("force_idsACL", $this->getacl("uidNumber").$this->getacl("gidNumber"));
507 foreach(array("primaryGroup","trustmode","activate_shadowWarning","activate_shadowInactive","activate_shadowMin","activate_shadowMax","activate_shadowExpire","mustchangepassword") as $val){
508 if(in_array($val,$this->multi_boxes)){
509 $smarty->assign("use_".$val,TRUE);
510 }else{
511 $smarty->assign("use_".$val,FALSE);
512 }
513 }
516 /* Load attributes and acl's */
517 foreach($this->attributes as $val){
518 if(in_array($val,$this->multi_boxes)){
519 $smarty->assign("use_".$val,TRUE);
520 }else{
521 $smarty->assign("use_".$val,FALSE);
522 }
524 if((session::get("js"))&&(($val=="uidNumber")||($val=="gidNumber")))
525 {
526 $smarty->assign("$val"."ACL",$this->getacl($val));
527 $smarty->assign("$val", $this->$val);
528 continue;
529 }
530 $smarty->assign("$val", $this->$val);
531 $smarty->assign("$val"."ACL", $this->getacl($val));
532 }
534 if($this->read_only){
535 $smarty->assign("groupMembershipACL","r");
536 }else{
537 $smarty->assign("groupMembershipACL","rw");
538 }
539 $smarty->assign("status", $this->status);
541 /* Work on trust modes */
542 $smarty->assign("trusthide", " disabled ");
543 $smarty->assign("trustmodeACL", $this->getacl("trustModel"));
544 if ($this->trustModel == "fullaccess"){
545 $trustmode= 1;
546 // pervent double disable tag in html code, this will disturb our clean w3c html
547 $smarty->assign("trustmode", $this->getacl("trustModel"));
549 } elseif ($this->trustModel == "byhost"){
550 $trustmode= 2;
551 $smarty->assign("trusthide", "");
552 } else {
553 // pervent double disable tag in html code, this will disturb our clean w3c html
554 $smarty->assign("trustmode", $this->getacl("trustModel"));
555 $trustmode= 0;
556 }
557 $smarty->assign("trustmode", $trustmode);
558 $smarty->assign("trustmodes", array( 0 => _("disabled"), 1 => _("full access"),
559 2 => _("allow access to these hosts")));
563 if((count($this->accessTo))==0)
564 $smarty->assign("emptyArrAccess",true);
565 else
566 $smarty->assign("emptyArrAccess",false);
568 if($this->mustchangepassword){
569 $smarty->assign("mustchangepassword", " checked ");
570 } else {
571 $smarty->assign("mustchangepassword", "");
572 }
574 $smarty->assign("workstations", $this->accessTo);
576 // Add SSH button if available
577 $smarty->assign("sshPublicKey", $this->ssh?1:0);
579 $smarty->assign("apply", apply_filter());
580 $smarty->assign("multiple_support" , $this->multiple_support_active);
581 $display.= $smarty->fetch (get_template_path('generic.tpl', TRUE, dirname(__FILE__)));
582 return($display);
583 }
586 /* remove object from parent */
587 function remove_from_parent()
588 {
589 /* Cancel if there's nothing to do here */
590 if ((!$this->initially_was_account) || (!$this->acl_is_removeable())){
591 return;
592 }
595 /* Remove and write to LDAP */
596 plugin::remove_from_parent();
598 /* Zero out array */
599 $this->attrs['gosaHostACL']= array();
601 /* Keep uid, because we need it for authentification! */
602 unset($this->attrs['uid']);
603 unset($this->attrs['trustModel']);
605 @DEBUG (DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__,
606 /* include global link_info */
607 $this->attributes, "Save");
608 $ldap= $this->config->get_ldap_link();
609 $ldap->cd($this->dn);
610 $this->cleanup();
611 $ldap->modify ($this->attrs);
613 new log("remove","users/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
615 if (!$ldap->success()){
616 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_DEL, get_class()));
617 }
619 /* Delete group only if cn is uid and there are no other
620 members inside */
621 $ldap->cd ($this->config->current['BASE']);
622 $ldap->search ("(&(objectClass=posixGroup)(gidNumber=".$this->gidNumber."))", array("cn", "memberUid"));
623 if ($ldap->count() != 0){
624 $attrs= $ldap->fetch();
625 if ($attrs['cn'][0] == $this->uid &&
626 !isset($this->attrs['memberUid'])){
628 $ldap->rmDir($ldap->getDN());
629 }
630 }
632 /* Optionally execute a command after we're done */
633 $this->handle_post_events("remove",array("uid" => $this->uid));
634 }
637 function save_object()
638 {
639 if (isset($_POST['posixTab'])){
640 /* Save values to object */
641 plugin::save_object();
644 /* Save force GID checkbox */
645 if($this->acl_is_writeable("gidNumber") || $this->acl_is_writeable("uidNumber")){
646 if (isset ($_POST['force_ids'])){
647 $data= 1;
648 } else {
649 $data= 0;
650 }
651 if ($this->force_ids != $data){
652 $this->is_modified= TRUE;
653 }
654 $this->force_ids= $data;
655 }
657 /*Save primary group settings */
658 if($this->acl_is_writeable("primaryGroup") && isset($_POST['primaryGroup'])){
659 $data= $_POST['primaryGroup'];
660 if ($this->primaryGroup != $data){
661 $this->is_modified= TRUE;
662 }
663 $this->primaryGroup= $_POST['primaryGroup'];
664 }
666 /* Get seelcted shadow checkboxes */
667 foreach(array("shadowMin","shadowMax","shadowExpire","shadowInactive","shadowWarning") as $var) {
668 if($this->acl_is_writeable($var)){
669 $activate_var = "activate_".$var;
670 if(isset($_POST['activate_'.$var])){
671 $this->$activate_var = true;
672 $this->$var = $_POST[$var];
673 }else{
674 $this->$activate_var = false;
675 if ($var != "shadowExpire") {
676 $this->$var = 0;
677 }
678 }
679 }
680 }
682 /* Force change password ? */
683 if(isset($_POST['mustchangepassword'])){
684 $this->mustchangepassword = TRUE;
685 }else{
686 $this->mustchangepassword = FALSE;
687 }
689 /* Trust mode - special handling */
690 if($this->acl_is_writeable("trustModel")){
691 if (isset($_POST['trustmode'])){
692 $saved= $this->trustModel;
693 if ($_POST['trustmode'] == "1"){
694 $this->trustModel= "fullaccess";
695 } elseif ($_POST['trustmode'] == "2"){
696 $this->trustModel= "byhost";
697 } else {
698 $this->trustModel= "";
699 }
700 if ($this->trustModel != $saved){
701 $this->is_modified= TRUE;
702 }
703 }
704 }
705 }
707 /* Get regex from alphabet */
708 if(isset($_GET['search'])){
709 $this->GroupRegex = $_GET['search']."*";
710 }
712 /* Check checkboxes and regexes */
713 if(isset($_POST["PosixGroupDialogPosted"])){
715 if(isset($_POST['SubSearch']) && ($_POST['SubSearch'])){
716 $this->SubSearch = true;
717 }else{
718 $this->SubSearch = false;
719 }
720 if(isset($_POST['guser'])){
721 $this->GroupUserRegex = $_POST['guser'];
722 }
723 if(isset($_POST['regex'])){
724 $this->GroupRegex = $_POST['regex'];
725 }
726 }
727 $this->GroupRegex = preg_replace("/\*\**/","*",$this->GroupRegex);
728 $this->GroupUserRegex = preg_replace("/\*\**/","*",$this->GroupUserRegex);
729 }
732 /* Save data to LDAP, depending on is_account we save or delete */
733 function save()
734 {
736 /* Adapt shadow values */
737 if (!$this->activate_shadowExpire){
738 $this->shadowExpire= "0";
739 } else {
740 /* Transform date to days since the beginning */
741 list($day, $month, $year)= explode('.', $this->shadowExpire, 3);
742 $this->shadowExpire= (int)(mktime(0, 0, 0, $month, $day, $year)/ (60 * 60 * 24)) ;
743 }
744 if (!$this->activate_shadowMax){
745 $this->shadowMax= "0";
746 }
747 if ($this->mustchangepassword){
748 $this->shadowLastChange= (int)(date("U") / 86400) - $this->shadowMax - 1;
749 } else {
750 $this->shadowLastChange= (int)(date("U") / 86400);
751 }
752 if (!$this->activate_shadowWarning){
753 $this->shadowWarning= "0";
754 }
756 /* Check what to do with ID's
757 Nothing forced, so we may have to generate our own IDs, if not done already.
758 */
759 if ($this->force_ids == 0){
761 /* Handle uidNumber.
762 * - use existing number if possible
763 * - if not, try to create a new uniqe one.
764 * */
765 if ($this->savedUidNumber != ""){
766 $this->uidNumber= $this->savedUidNumber;
767 } else {
769 /* Calculate new id's. We need to place a lock before calling get_next_id
770 to get real unique values.
771 */
772 $wait= 10;
773 while (get_lock("uidnumber") != ""){
774 sleep (1);
776 /* Oups - timed out */
777 if ($wait-- == 0){
778 msg_dialog::display(_("Warning"), _("Timeout while waiting for lock. Ignoring lock!"), WARNING_DIALOG);
779 break;
780 }
781 }
782 add_lock ("uidnumber", "gosa");
783 $this->uidNumber= get_next_id("uidNumber", $this->dn);
784 }
785 }
788 /* Handle gidNumber
789 * - If we do not have a primary group selected (automatic), we will check if there
790 * is already a group with the same name and use this as primary.
791 * - .. if we couldn't find a group with the same name, we will create a new one,
792 * using the users uid as cn and a generated uniqe gidNumber.
793 * */
794 if ($this->primaryGroup == 0 || $this->force_ids){
796 /* Search for existing group */
797 $ldap = $this->config->get_ldap_link();
798 $ldap->cd($this->config->current['BASE']);
800 /* Are we forced to use a special gidNumber? */
801 if($this->force_ids){
802 $ldap->search("(&(objectClass=posixGroup)(gidNumber=".$this->gidNumber."))", array("cn","gidNumber"));
803 }else{
804 $ldap->search("(&(objectClass=posixGroup)(gidNumber=*)(cn=".$this->uid."))", array("cn","gidNumber"));
805 }
807 /* No primary group found, create a new one */
808 if ($ldap->count() == 0){
810 $groupcn = $this->uid;
811 $pri_attr = $this->config->get_cfg_value("accountPrimaryAttribute");
812 $groupdn= preg_replace ('/^'.preg_quote($pri_attr,'/').'=[^,]+,'.preg_quote(get_people_ou(),'/').'/i',
813 'cn='.$groupcn.','.get_groups_ou(), $this->dn);
815 /* Request a new and uniqe gidNumber, if required */
816 if(!$this->force_ids){
817 $this->gidNumber= get_next_id("gidNumber", $this->dn);
818 }
820 /* If forced gidNumber could not be found, then check if the given group name already exists
821 we do not want to modify the gidNumber of an existing group.
822 */
823 $cnt= 0;
824 while($ldap->dn_exists($groupdn) && ($cnt < 100)){
825 $cnt ++;
826 $groupcn = $this->uid."_".$cnt;
827 $groupdn= preg_replace ('/^'.preg_quote($pri_attr,'/').'=[^,]+,'.preg_quote(get_people_ou(),'/').'/i',
828 'cn='.$groupcn.','.get_groups_ou(), $this->dn);
829 }
831 /* Create new primary group and enforce the new gidNumber */
832 $g= new group($this->config, $groupdn);
833 $g->cn= $groupcn;
834 $g->force_gid= 1;
835 $g->gidNumber= $this->gidNumber;
836 $g->description= _("Group of user")." ".$this->givenName." ".$this->sn;
837 $g->save ();
839 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
840 sprintf("Primary group '%s' created, using gidNumber '%s'.",$groupcn,$this->gidNumber),"");
841 }else{
842 $attrs = $ldap->fetch();
843 $this->gidNumber = $attrs['gidNumber'][0];
844 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
845 "Found and used: <i>".$attrs['dn']."</i>",
846 sprintf("Primary group '%s' exists, gidNumber is '%s'.",$this->uid,$this->gidNumber));
847 }
848 }else{
850 /* Primary group was selected by user
851 */
852 $this->gidNumber = $this->primaryGroup;
853 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
854 sprintf("Primary group '%s' for user '%s' manually selected.",$this->gidNumber,$this->uid),"");
855 }
857 if ($this->activate_shadowMin != "1" ) {
858 $this->shadowMin = "";
859 }
861 if (($this->activate_shadowMax != "1") && ($this->mustchangepassword != "1")) {
862 $this->shadowMax = "";
863 }
865 if ($this->activate_shadowWarning != "1" ) {
866 $this->shadowWarning = "";
867 }
869 if ($this->activate_shadowInactive != "1" ) {
870 $this->shadowInactive = "";
871 }
873 if ($this->activate_shadowExpire != "1" ) {
874 $this->shadowExpire = "";
875 }
877 /* Fill gecos */
878 if (isset($this->parent) && $this->parent !== NULL){
879 $this->gecos= rewrite($this->parent->by_object['user']->cn);
880 if (!preg_match('/^[a-z0-9 -]+$/i', $this->gecos)){
881 $this->gecos= "";
882 }
883 }
885 foreach(array("shadowMin","shadowMax","shadowWarning","shadowInactive","shadowExpire") as $attr){
886 $this->$attr = (int) $this->$attr;
887 }
888 /* Call parents save to prepare $this->attrs */
889 plugin::save();
891 /* Trust accounts */
892 $objectclasses= array();
893 foreach ($this->attrs['objectClass'] as $key => $class){
894 if (preg_match('/trustAccount/i', $class)){
895 continue;
896 }
897 $objectclasses[]= $this->attrs['objectClass'][$key];
898 }
899 $this->attrs['objectClass']= $objectclasses;
900 if ($this->trustModel != ""){
901 $this->attrs['objectClass'][]= "trustAccount";
902 $this->attrs['trustModel']= $this->trustModel;
903 $this->attrs['accessTo']= array();
904 if ($this->trustModel == "byhost"){
905 foreach ($this->accessTo as $host){
906 $this->attrs['accessTo'][]= $host;
907 }
908 }
909 } else {
910 if ($this->was_trust_account){
911 $this->attrs['accessTo']= array();
912 $this->attrs['trustModel']= array();
913 }
914 }
916 if(empty($this->attrs['gosaDefaultPrinter'])){
917 $thid->attrs['gosaDefaultPrinter']=array();
918 }
921 /* include global link_info */
922 $this->cleanup();
924 /* This is just a test, we have had duplicated ids
925 in the past when copy & paste was used.
926 Normaly this should not happen.
927 */
928 if(isset($this->attrs['uidNumber']) && !$this->force_ids){
929 $used = $this->get_used_uid_numbers();
930 if(isset($used[$this->attrs['uidNumber']]) && $used[$this->attrs['uidNumber']] != $this->dn){
931 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);
932 }
933 }
935 $ldap= $this->config->get_ldap_link();
936 $ldap->cd($this->dn);
937 unset($this->attrs['uid']);
938 $ldap->modify ($this->attrs);
940 /* Log last action */
941 if($this->initially_was_account){
942 new log("modify","users/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
943 }else{
944 new log("create","users/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
945 }
947 if (!$ldap->success()){
948 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_MOD, get_class()));
949 }
951 /* Remove lock needed for unique id generation */
952 del_lock ("uidnumber");
954 // Save ssh stuff if needed
955 if ($this->ssh) {
956 $this->ssh->setDN($this->dn);
957 $this->ssh->save();
958 }
960 /* Take care about groupMembership values: add to groups */
961 foreach ($this->groupMembership as $key => $value){
962 if (!isset($this->savedGroupMembership[$key])){
963 $g= new grouptabs($this->config,$this->config->data['TABS']['GROUPTABS'], $key,"groups");
964 $g->set_acl_base($key);
965 $g->by_object['group']->addUser($this->uid);
966 $g->save();
967 }
968 }
970 /* Remove groups not listed in groupMembership */
971 foreach ($this->savedGroupMembership as $key => $value){
972 if (!isset($this->groupMembership[$key])){
973 $g= new grouptabs($this->config,$this->config->data['TABS']['GROUPTABS'], $key,"groups");
974 $g->set_acl_base($key);
975 $g->by_object['group']->removeUser ($this->uid);
976 $g->save();
977 }
978 }
980 /* Optionally execute a command after we're done */
981 if ($this->initially_was_account == $this->is_account){
982 if ($this->is_modified){
983 $this->handle_post_events("modify",array("uid" => $this->uid));
984 }
985 } else {
986 $this->handle_post_events("add" ,array("uid"=> $this->uid));
987 }
988 }
991 /* Check formular input */
992 function check()
993 {
994 /* Include global link_info */
995 $ldap= $this->config->get_ldap_link();
997 /* Append groups as memberGroup: to check hook
998 */
999 $tmp_attributes = $this->attributes;
1000 $this->attributes[] = "memberGroup";
1001 $this->memberGroup = array();
1002 foreach($this->groupMembership as $dn => $name){
1003 $this->memberGroup[] = $name;
1004 }
1006 /* Call common method to give check the hook */
1007 $message= plugin::check();
1008 $this->attributes = $tmp_attributes;
1010 /* must: homeDirectory */
1011 if ($this->homeDirectory == ""){
1012 $message[]= msgPool::required(_("Home directory"));
1013 }
1014 if (!tests::is_path($this->homeDirectory)){
1015 $message[]= msgPool::invalid(_("Home directory"), "", "", "/home/yourname" );
1016 }
1018 /* Check ID's if they are forced by user */
1019 if ($this->force_ids == "1"){
1021 /* Valid uid/gid? */
1022 if (!tests::is_id($this->uidNumber)){
1023 $message[]= msgPool::invalid(_("UID"), $this->uidNumber, "/[0-9]/");
1024 } else {
1025 if ($this->uidNumber < $this->config->get_cfg_value("minId")){
1026 $message[]= msgPool::toosmall(_("UID"), $this->config->get_cfg_value("minId"));
1027 }
1028 }
1029 if (!tests::is_id($this->gidNumber)){
1030 $message[]= msgPool::invalid(_("GID"), $this->gidNumber, "/[0-9]/");
1031 } else {
1032 if ($this->gidNumber < $this->config->get_cfg_value("minId")){
1033 $message[]= msgPool::toosmall(_("GID"), $this->config->get_cfg_value("minId"));
1034 }
1035 }
1036 }
1038 /* Check dates */
1039 if ($this->activate_shadowExpire && ($this->shadowExpire == "" || !tests::is_date($this->shadowExpire))){
1040 $message[]= msgPool::invalid("shadowExpire", $this->shadowExpire);
1041 }
1043 /* Check shadow settings, well I like spaghetties... */
1044 if ($this->activate_shadowMin){
1045 if (!tests::is_id($this->shadowMin)){
1046 $message[]= msgPool::invalid(_("shadowMin"), $this->shadowMin, "/[0-9]/");
1047 }
1048 }
1049 if ($this->activate_shadowMax){
1050 if (!tests::is_id($this->shadowMax)){
1051 $message[]= msgPool::invalid(_("shadowMax"), $this->shadowMax, "/[0-9]/");
1052 }
1053 }
1054 if ($this->activate_shadowWarning){
1055 if (!tests::is_id($this->shadowWarning)){
1056 $message[]= msgPool::invalid(_("shadowWarning"), $this->shadowWarning, "/[0-9]/");
1057 }
1058 if (!$this->activate_shadowMax){
1059 $message[]= msgPool::depends("shadowWarning", "shadowMax");
1060 }
1061 if ($this->shadowWarning > $this->shadowMax){
1062 $message[]= msgPool::toobig("shadowWarning", "shadowMax");
1063 }
1064 if ($this->activate_shadowMin && $this->shadowWarning < $this->shadowMin){
1065 $message[]= msgPool::toosmall("shadowWarning", "shadowMin");
1066 }
1067 }
1068 if ($this->activate_shadowInactive){
1069 if (!tests::is_id($this->shadowInactive)){
1070 $message[]= msgPool::invalid(_("shadowInactive"), $this->shadowInactive, "/[0-9]/");
1071 }
1072 if (!$this->activate_shadowMax){
1073 $message[]= msgPool::depends("shadowInactive", "shadowMax");
1074 }
1075 }
1076 if ($this->activate_shadowMin && $this->activate_shadowMax){
1077 if ($this->shadowMin > $this->shadowMax){
1078 $message[]= msgPool::toobig("shadowMin", "shadowMax");
1079 }
1080 }
1082 return ($message);
1083 }
1086 function multiple_check()
1087 {
1088 $message = plugin::multiple_check();
1089 if ($this->homeDirectory == "" && in_array("homeDirectory",$this->multi_boxes)){
1090 $message[]= msgPool::required(_("Home directory"));
1091 }
1092 if (!tests::is_path($this->homeDirectory) && in_array("homeDirectory",$this->multi_boxes)){
1093 $message[]= msgPool::invalid(_("Home directory"), "", "", "/home/yourname" );
1094 }
1096 /* Check shadow settings, well I like spaghetties... */
1097 if ($this->activate_shadowMin && in_array("activate_shadowMin",$this->multi_boxes)){
1098 if (!tests::is_id($this->shadowMin)){
1099 $message[]= msgPool::invalid(_("shadowMin"), $this->shadowMin, "/[0-9]/");
1100 }
1101 }
1102 if ($this->activate_shadowMax && in_array("activate_shadowMax",$this->multi_boxes)){
1103 if (!tests::is_id($this->shadowMax)){
1104 $message[]= msgPool::invalid(_("shadowMax"), $this->shadowMax, "/[0-9]/");
1105 }
1106 }
1107 if ($this->activate_shadowWarning && in_array("activate_shadowWarning",$this->multi_boxes)){
1108 if (!tests::is_id($this->shadowWarning)){
1109 $message[]= msgPool::invalid(_("shadowWarning"), $this->shadowWarning, "/[0-9]/");
1110 }
1111 if (!$this->activate_shadowMax && in_array("activate_shadowMax",$this->multi_boxes)){
1112 $message[]= msgPool::depends("shadowWarning", "shadowMax");
1113 }
1114 if ($this->shadowWarning > $this->shadowMax && in_array("activate_shadowWarning",$this->multi_boxes)){
1115 $message[]= msgPool::toobig("shadowWarning", "shadowMax");
1116 }
1117 if ($this->activate_shadowMin && $this->shadowWarning < $this->shadowMin && in_array("activate_shadowMin",$this->multi_boxes)){
1118 $message[]= msgPool::tosmall("shadowWarning", "shadowMin");
1119 }
1120 }
1121 if ($this->activate_shadowInactive && in_array("activate_shadowInactive",$this->multi_boxes)){
1122 if (!tests::is_id($this->shadowInactive)){
1123 $message[]= msgPool::invalid(_("shadowInactive"), $this->shadowInactive, "/[0-9]/");
1124 }
1125 if (!$this->activate_shadowMax && in_array("activate_shadowMax",$this->multi_boxes)){
1126 $message[]= msgPool::depends("shadowInactive", "shadowMax");
1127 }
1128 }
1129 if ($this->activate_shadowMin && $this->activate_shadowMax && in_array("activate_shadowMin",$this->multi_boxes)){
1130 if ($this->shadowMin > $this->shadowMax){
1131 $message[]= msgPool::toobig("shadowMin", "shadowMax");
1132 }
1133 }
1135 return($message);
1136 }
1139 function addGroup ($groups)
1140 {
1141 /* include global link_info */
1142 $ldap= $this->config->get_ldap_link();
1144 /* Walk through groups and add the descriptive entry if not exists */
1145 foreach ($groups as $value){
1147 if (!array_key_exists($value, $this->groupMembership)){
1148 $ldap->cat($value, array('cn', 'description', 'dn'));
1149 $attrs= $ldap->fetch();
1150 error_reporting (0);
1151 if (!isset($attrs['description'][0])){
1152 $entry= $attrs["cn"][0];
1153 } else {
1154 $dsc= preg_replace ('/^Group of user/', _("Group of user"), $attrs["description"][0]);
1155 $entry= $attrs["cn"][0]." [$dsc]";
1156 }
1157 error_reporting (E_ALL | E_STRICT);
1159 if(obj_is_writable($attrs['dn'],"groups/group","memberUid")){
1160 $this->groupMembership[$attrs['dn']]= $entry;
1161 if($this->multiple_support_active && isset($this->groupMembership_some[$attrs['dn']])){
1162 unset($this->groupMembership_some[$attrs['dn']]);
1163 }
1164 }
1165 }
1166 }
1168 /* Sort groups */
1169 asort ($this->groupMembership);
1170 reset ($this->groupMembership);
1171 }
1174 /* Del posix user from some groups */
1175 function delGroup ($groups)
1176 {
1177 $dest= array();
1178 foreach($groups as $dn_to_del){
1179 if(isset($this->groupMembership[$dn_to_del]) && obj_is_writable($dn_to_del,"groups/group","memberUid")){
1180 unset($this->groupMembership[$dn_to_del]);
1181 }
1182 if($this->multiple_support_active){
1183 if(isset($this->groupMembership_some[$dn_to_del]) && obj_is_writable($dn_to_del,"groups/group","memberUid")){
1184 unset($this->groupMembership_some[$dn_to_del]);
1185 }
1186 }
1187 }
1188 }
1191 /* Adapt from template, using 'dn' */
1192 function adapt_from_template($dn, $skip= array())
1193 {
1194 /* Include global link_info */
1195 $ldap= $this->config->get_ldap_link();
1197 plugin::adapt_from_template($dn, $skip);
1198 $template= $this->attrs['uid'][0];
1200 /* Adapt group membership */
1201 $ldap->cd($this->config->current['BASE']);
1202 $ldap->search("(&(objectClass=posixGroup)(memberUid=".$this->attrs["uid"][0]."))", array("description", "cn"));
1204 while ($this->attrs= $ldap->fetch()){
1205 if (!isset($this->attrs["description"][0])){
1206 $entry= $this->attrs["cn"][0];
1207 } else {
1208 $entry= $this->attrs["cn"][0]." [".$this->attrs["description"][0]."]";
1209 }
1210 $this->groupMembership[$ldap->getDN()]= $entry;
1211 }
1213 /* Fix primary group settings */
1214 $ldap->cd($this->config->current['BASE']);
1215 $ldap->search("(&(objectClass=posixGroup)(cn=$template)(gidNumber=".$this->gidNumber."))", array("cn"));
1216 if ($ldap->count() != 1){
1217 $this->primaryGroup= $this->gidNumber;
1218 }
1220 $ldap->cd($this->config->current['BASE']);
1221 $ldap->search("(&(objectClass=gosaUserTemplate)(uid=".$template.")(accessTo=*))", array("cn","accessTo"));
1222 while($attr = $ldap->fetch()){
1223 $tmp = $attr['accessTo'];
1224 unset ($tmp['count']);
1225 $this->accessTo = $tmp;
1226 }
1228 /* Adjust shadow checkboxes */
1229 foreach (array("shadowMin", "shadowMax", "shadowWarning", "shadowInactive") as $val){
1230 if ($this->$val != 0){
1231 $oval= "activate_".$val;
1232 $this->$oval= "1";
1233 }
1234 }
1236 /* FIXME: NEED review of this section */
1237 /* Need to check shadowExpire separately */
1239 /*
1240 * If shadowExpire is not enabled in the template, it's a UNIX timestamp - so don't convert it to seconds.
1241 * The check is a hack - if difference between timestamp generated above and here is max 1 day.
1242 */
1243 if(abs($this->shadowExpire - time())>86400) {
1244 $this->shadowExpire= $this->convertToSeconds($this->shadowExpire);
1245 }
1247 /* Only enable checkbox, if shadowExpire is in the future */
1248 if($this->shadowExpire > time()) {
1249 $this->activate_shadowExpire= "1";
1250 }
1251 }
1253 function convertToSeconds($val)
1254 {
1255 if ($val != 0){
1256 $val*= 60 * 60 * 24;
1257 } else {
1258 $date= getdate();
1259 $val= floor($date[0] / (60*60*24)) * 60 * 60 * 24;
1260 }
1261 return($val);
1262 }
1265 function get_used_uid_numbers()
1266 {
1267 $ids= array();
1268 $ldap= $this->config->get_ldap_link();
1270 $ldap->cd ($this->config->current['BASE']);
1271 $ldap->search ("(&(objectClass=posixAccount)(uidNumber=*))", array("uidNumber"));
1273 /* Get list of ids */
1274 while ($attrs= $ldap->fetch()){
1275 $ids[$attrs['uidNumber'][0]] = $attrs['dn'];
1276 }
1277 return($ids);
1278 }
1281 /* Get posts from copy & paste dialog */
1282 function saveCopyDialog()
1283 {
1284 if(isset($_POST['homeDirectory'])){
1285 $this->homeDirectory = $_POST['homeDirectory'];
1286 if (isset ($_POST['force_ids'])){
1287 $data= 1;
1288 $this->gidNumber = $_POST['gidNumber'];
1289 $this->uidNumber = $_POST['uidNumber'];
1290 } else {
1291 $data= 0;
1292 }
1293 if ($this->force_ids != $data){
1294 $this->is_modified= TRUE;
1295 }
1296 $this->force_ids= $data;
1297 $data= $_POST['primaryGroup'];
1298 if ($this->primaryGroup != $data){
1299 $this->is_modified= TRUE;
1300 }
1301 $this->primaryGroup= $_POST['primaryGroup'];
1302 }
1303 }
1306 /* Create the posix dialog part for copy & paste */
1307 function getCopyDialog()
1308 {
1309 /* Skip dialog creation if this is not a valid account*/
1310 if(!$this->is_account) return("");
1311 if ($this->force_ids == 1){
1312 $force_ids = "checked";
1313 if (session::get('js')){
1314 $forceMode = "";
1315 }
1316 } else {
1317 if (session::get('js')){
1318 if($this->acl != "#none#")
1319 $forceMode ="disabled";
1320 }
1321 $force_ids = "";
1322 }
1324 $sta = "";
1326 /* Open group add dialog */
1327 if(isset($_POST['edit_groupmembership'])){
1328 $this->groupSelect = new groupSelect($this->config,get_userinfo());
1329 $sta = "SubDialog";
1330 }
1332 /* If the group-add dialog is closed, call execute
1333 to ensure that the membership is updatd */
1334 if(isset($_POST['add_groups_finish']) || isset($_POST['add_groups_cancel'])){
1335 $this->execute();
1336 $this->groupSelect =NULL;
1337 }
1339 if($this->groupSelect){
1340 $str = $this->execute(true);
1341 $ret = array();
1342 $ret['string'] = $str;
1343 $ret['status'] = $sta;
1344 return($ret);
1345 }
1347 /* If a group member should be deleted, simply call execute */
1348 if(isset($_POST['delete_groupmembership'])){
1349 $this->execute();
1350 }
1352 /* Assigned informations to smarty */
1353 $smarty = get_smarty();
1354 $smarty->assign("homeDirectory",$this->homeDirectory);
1355 $smarty->assign("secondaryGroups",$this->secondaryGroups);
1356 $smarty->assign("primaryGroup",$this->primaryGroup);
1358 $smarty->assign("uidNumber",$this->uidNumber);
1359 $smarty->assign("gidNumber",$this->gidNumber);
1360 $smarty->assign("forceMode",$forceMode);
1361 $smarty->assign("force_ids",$force_ids);
1362 if (!count($this->groupMembership)){
1363 $smarty->assign("groupMembership", array(" "));
1364 } else {
1365 $smarty->assign("groupMembership", $this->groupMembership);
1366 }
1368 /* Display wars message if there are more than 16 group members */
1369 if (count($this->groupMembership) > 16){
1370 $smarty->assign("groups", "too_many_for_nfs");
1371 } else {
1372 $smarty->assign("groups", "");
1373 }
1374 $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE,dirname(__FILE__)));
1376 $ret = array();
1377 $ret['string'] = $str;
1378 $ret['status'] = $sta;
1379 return($ret);
1380 }
1383 function PrepareForCopyPaste($source)
1384 {
1385 plugin::PrepareForCopyPaste($source);
1387 /* Avoid using the same gid/uid number as source user
1388 empty numbers to enforce new ones. */
1389 $this->savedUidNumber = "";
1390 $this->savedGidNumber = "";
1392 /* Get group membership */
1393 $ldap = $this->config->get_ldap_link();
1394 $ldap->cd($this->config->current['BASE']);
1395 $ldap->search("(&(objectClass=posixGroup)(memberUid=".$source['uid'][0]."))", array("cn", "description"));
1397 while ($attrs= $ldap->fetch()){
1398 if (!isset($attrs["description"][0])){
1399 $entry= $attrs["cn"][0];
1400 } else {
1401 $entry= $attrs["cn"][0]." [".$attrs["description"][0]."]";
1402 }
1403 $this->groupMembership[$ldap->getDN()]= $entry;
1404 }
1405 asort($this->groupMembership);
1406 reset($this->groupMembership);
1408 /* Fill group */
1409 if(isset($source['gidNumber'][0])){
1410 $this->primaryGroup= $source['gidNumber'][0];
1411 }
1413 }
1416 function multiple_execute()
1417 {
1418 return($this->execute());
1419 }
1422 static function plInfo()
1423 {
1424 return (array(
1425 "plDescription" => _("POSIX account"),
1426 "plSelfModify" => TRUE,
1427 "plDepends" => array("user"),
1428 "plPriority" => 2,
1429 "plSection" => array("personal" => _("My account")),
1430 "plCategory" => array("users"),
1431 "plOptions" => array(),
1433 "plProvidedAcls" => array(
1435 "homeDirectory" => _("Home directory"),
1436 "loginShell" => _("Shell"),
1437 "uidNumber" => _("User ID"),
1438 "gidNumber" => _("Group ID"),
1440 "mustchangepassword"=> _("Force password change on login"),
1441 "shadowMin" => _("Shadow min"),
1442 "shadowMax" => _("Shadow max"),
1443 "shadowWarning" => _("Shadow warning"),
1444 "shadowInactive" => _("Shadow inactive"),
1445 "shadowExpire" => _("Shadow expire"),
1446 "sshPublickey" => _("Public SSH key"),
1447 "trustModel" => _("System trust model")))
1448 );
1449 }
1452 /* Return selected values for multiple edit */
1453 function get_multi_edit_values()
1454 {
1455 $ret = plugin::get_multi_edit_values();
1456 $ret['groupMembership'] = $this->groupMembership;
1457 $ret['groupMembership_some']= $this->groupMembership_some;
1459 if(in_array("primaryGroup",$this->multi_boxes)){
1460 $ret['primaryGroup'] = $this->primaryGroup;
1461 }
1462 if(in_array("trustmode",$this->multi_boxes)){
1463 $ret['trustModel'] = $this->trustModel;
1464 $ret['accessTo'] = $this->accessTo;
1465 }
1466 foreach(array("shadowWarning","shadowInactive","shadowMin","shadowMax", "shadowExpire") as $entry){
1467 $active = "activate_".$entry;
1468 if(in_array($active,$this->multi_boxes)){
1469 $ret[$entry] = $this->$entry;
1470 $ret[$active] = $this->$active;
1471 }
1472 }
1473 if(in_array("mustchangepassword",$this->multi_boxes)){
1474 $ret['mustchangepassword'] = $this->mustchangepassword;
1475 }
1476 return($ret);
1477 }
1480 /* Save posts for multiple edit
1481 */
1482 function multiple_save_object()
1483 {
1484 if(isset($_POST['posix_mulitple_edit'])){
1486 /* Backup expire value */
1487 $expire_tmp = $this->shadowExpire;
1489 /* Update all values */
1490 plugin::multiple_save_object();
1492 /* Get selected checkboxes */
1493 foreach(array("primaryGroup","trustmode","mustchangepassword","activate_shadowWarning","activate_shadowInactive","activate_shadowMin", "activate_shadowMax","activate_shadowExpire") as $val){
1494 if(isset($_POST["use_".$val])){
1495 $this->multi_boxes[] = $val;
1496 }
1497 }
1499 /* Update special values, checkboxes for posixShadow */
1500 foreach(array("shadowMin","shadowMax","shadowExpire","shadowInactive","shadowWarning") as $var) {
1501 if($this->acl_is_writeable($var)){
1502 $activate_var = "activate_".$var;
1503 if(in_array($activate_var, $this->multi_boxes)){
1504 if(isset($_POST['activate_'.$var])){
1505 $this->$activate_var = true;
1506 $this->$var = $_POST[$var];
1507 }else{
1508 $this->$activate_var = false;
1509 $this->$var = 0;
1510 }
1511 }
1512 }
1513 }
1515 /* Restore shadow value, if the shadow attribute isn't used */
1516 if(!in_array("activate_shadowExpire",$this->multi_boxes)){
1517 $this->shadowExpire = $expire_tmp;
1518 }
1520 /* Force change password ? */
1521 if(isset($_POST['mustchangepassword'])){
1522 $this->mustchangepassword = TRUE;
1523 }else{
1524 $this->mustchangepassword = FALSE;
1525 }
1527 /* Trust mode - special handling */
1528 if($this->acl_is_writeable("trustModel")){
1529 if (isset($_POST['trustmode'])){
1530 $saved= $this->trustModel;
1531 if ($_POST['trustmode'] == "1"){
1532 $this->trustModel= "fullaccess";
1533 } elseif ($_POST['trustmode'] == "2"){
1534 $this->trustModel= "byhost";
1535 } else {
1536 $this->trustModel= "";
1537 }
1538 if ($this->trustModel != $saved){
1539 $this->is_modified= TRUE;
1540 }
1541 }
1542 }
1544 /* Save primary group settings */
1545 if($this->acl_is_writeable("primaryGroup") && isset($_POST['primaryGroup'])){
1546 $data= $_POST['primaryGroup'];
1547 if ($this->primaryGroup != $data){
1548 $this->is_modified= TRUE;
1549 }
1550 $this->primaryGroup= $_POST['primaryGroup'];
1551 }
1552 }
1553 }
1556 /* Initialize plugin with given atribute arrays
1557 */
1558 function init_multiple_support($attrs,$all)
1559 {
1560 plugin::init_multiple_support($attrs,$all);
1562 /* Some dummy values */
1563 $groups_some = array();
1564 $groups_all = array();
1565 $groups_uid = array();
1566 $uids = array();
1567 $first = TRUE;
1569 /* Get all groups used by currently edited users */
1570 $uid_filter="";
1571 for($i =0; $i < $this->multi_attrs_all['uid']['count'] ; $i ++){
1572 $uid = $this->multi_attrs_all['uid'][$i];
1573 $uids[] = $uid;
1574 $uid_filter.= "(memberUid=".$uid.")";
1575 }
1576 $uid_filter = "(&(objectClass=posixGroup)(|".$uid_filter."))";
1577 $ldap = $this->config->get_ldap_link();
1578 $ldap->cd($this->config->current['BASE']);
1579 $ldap->search($uid_filter,array("dn","cn","memberUid"));
1580 while($group = $ldap->fetch()){
1581 $groups_some[$group['dn']] = $group['cn'][0];
1582 for($i = 0 ; $i < $group['memberUid']['count'] ; $i++){
1583 $groups_uid[$group['dn']][] = $group['memberUid'][$i];
1584 }
1585 }
1587 /* Create an array, containing all used groups */
1588 $groups_all = $groups_some;
1589 foreach($groups_all as $id => $group){
1590 foreach($uids as $uid){
1591 if(!in_array($uid,$groups_uid[$id])){
1592 unset($groups_all[$id]);
1593 break;
1594 }
1595 }
1596 }
1598 /* Assign group array */
1599 $this->groupMembership = $groups_all;
1601 /* Create an array of all grouops used by all users */
1602 foreach( $groups_all as $dn => $cn){
1603 if(isset($groups_some[$dn])){
1604 unset($groups_some[$dn]);
1605 }
1606 }
1607 $this->groupMembership_some = $groups_some;
1608 $this->primaryGroup = $this->gidNumber;
1610 /* Is this account a trustAccount? */
1611 if (isset($this->multi_attrs['trustModel'])){
1612 $this->trustModel= $this->multi_attrs['trustModel'][0];
1613 $this->was_trust_account= TRUE;
1614 $this->multi_boxes[] = "trustmode";
1615 } else {
1616 $this->was_trust_account= FALSE;
1617 $this->trustModel= "";
1618 }
1620 /* Create access informations */
1621 $this->accessTo = array();
1622 if (isset($this->multi_attrs['accessTo'])){
1623 for ($i= 0; $i<$this->multi_attrs['accessTo']['count']; $i++){
1624 $tmp= $this->multi_attrs['accessTo'][$i];
1625 $this->accessTo[$tmp]= $tmp;
1626 }
1627 }
1629 /* Adjust shadow checkboxes */
1630 foreach (array("shadowMin", "shadowMax", "shadowWarning", "shadowInactive",
1631 "shadowExpire") as $val){
1632 if ($this->$val != 0){
1633 $oval= "activate_".$val;
1634 $this->$oval= "1";
1635 }
1636 }
1638 /* Convert to seconds */
1639 if(isset($this->multi_attrs['shadowExpire'])){
1640 $this->shadowExpire = $this->convertToSeconds($this->multi_attrs['shadowExpire'][0]);
1641 }else{
1642 $this->activate_shadowExpire = FALSE;
1643 }
1644 }
1647 function set_multi_edit_values($attrs)
1648 {
1649 $groups = array();
1651 /* Update groupMembership, keep optinal group */
1652 foreach($attrs['groupMembership_some'] as $dn => $cn){
1653 if(isset($this->groupMembership[$dn])){
1654 $groups[$dn] = $cn;
1655 }
1656 }
1657 /* Update groupMembership, add forced groups */
1658 foreach($attrs['groupMembership'] as $dn => $cn){
1659 $groups[$dn] = $cn;
1660 }
1661 plugin::set_multi_edit_values($attrs);
1662 $this->groupMembership = $groups;
1663 }
1664 }
1666 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1667 ?>