1 <?php
2 /*
3 * This code is part of GOsa (http://www.gosa-project.org)
4 * Copyright (C) 2003-2008 GONICUS GmbH
5 *
6 * ID: $$Id$$
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
23 /*!
24 \brief posixAccount plugin
25 \author Cajus Pollmeier <pollmeier@gonicus.de>
26 \version 2.00
27 \date 24.07.2003
29 This class provides the functionality to read and write all attributes
30 relevant for posixAccounts and shadowAccounts from/to the LDAP. It
31 does syntax checking and displays the formulars required.
32 */
34 class posixAccount extends plugin
35 {
36 /* Definitions */
37 var $plHeadline= "UNIX";
38 var $plDescription= "Edit users POSIX settings";
40 /* Plugin specific values */
41 var $homeDirectory= "";
42 var $loginShell= "/bin/bash";
43 var $uidNumber= "";
44 var $gidNumber= "";
45 var $gecos= "";
46 var $shadowMin= "0";
47 var $shadowMax= "0";
48 var $shadowWarning= "0";
49 var $shadowLastChange= "0";
50 var $shadowInactive= "0";
51 var $shadowExpire= "0";
52 var $gosaDefaultPrinter= "";
53 var $accessTo= array();
54 var $trustModel= "";
56 var $glist=array();
57 var $status= "";
58 var $loginShellList= array();
59 var $groupMembership= array();
60 var $savedGroupMembership= array();
61 var $savedUidNumber= "";
62 var $savedGidNumber= "";
63 var $activate_shadowMin= "0";
64 var $activate_shadowMax= "0";
65 var $activate_shadowWarning= "0";
66 var $activate_shadowInactive= "0";
67 var $activate_shadowExpire= "0";
68 var $mustchangepassword= "0";
69 var $force_ids= 0;
70 var $gotoLastSystemLogin= "";
71 var $group_dialog= FALSE;
72 var $show_ws_dialog= 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 $this->sshAcl= $this->getacl("sshPublicKey");
201 $this->ssh= new sshPublicKey($this->config, $this->dn, $this->sshAcl);
202 }
203 }
205 /* Adjust shadow checkboxes */
206 foreach (array("shadowMin", "shadowMax", "shadowWarning", "shadowInactive",
207 "shadowExpire") as $val){
209 if ($this->$val != 0){
210 $oval= "activate_".$val;
211 $this->$oval= "1";
212 }
213 }
215 /* Convert to seconds */
216 $this->shadowExpire= $this->convertToSeconds($this->shadowExpire);
218 /* Generate shell list from CONFIG_DIR./shells */
219 if (file_exists(CONFIG_DIR.'/shells')){
220 $shells = file (CONFIG_DIR.'/shells');
221 foreach ($shells as $line){
222 if (!preg_match ("/^#/", $line)){
223 $this->loginShellList[]= trim($line);
224 }
225 }
226 } else {
227 if ($this->loginShell == ""){
228 $this->loginShellList[]= _("unconfigured");
229 }
230 }
232 /* Insert possibly missing loginShell */
233 if ($this->loginShell != "" && !in_array($this->loginShell, $this->loginShellList)){
234 $this->loginShellList[]= $this->loginShell;
235 }
237 /* Generate group list */
238 $this->ui = get_userinfo();
239 $this->secondaryGroups[]= "- "._("automatic")." -";
240 $ldap->cd($this->config->current['BASE']);
241 $ldap->search("(objectClass=posixGroup)", array("cn", "gidNumber"));
242 while($attrs = $ldap->fetch()){
243 $this->secondaryGroups[$attrs['gidNumber'][0]]= $attrs['cn'][0];
244 }
245 asort ($this->secondaryGroups);
247 /* Get global filter config */
248 if (!session::is_set("sysfilter")){
249 $ui= get_userinfo();
250 $base= get_base_from_people($ui->dn);
251 $sysfilter= array( "depselect" => $base,
252 "regex" => "*");
253 session::set("sysfilter", $sysfilter);
254 }
255 $this->ui = get_userinfo();
256 }
259 /* execute generates the html output for this node */
260 function execute($isCopyPaste = false)
261 {
262 /* Call parent execute */
263 plugin::execute();
264 $display= "";
266 /* Log view */
267 if($this->is_account && !$this->view_logged){
268 $this->view_logged = TRUE;
269 new log("view","users/".get_class($this),$this->dn);
270 }
272 /* Department has changed? */
273 if(isset($_POST['depselect'])){
274 session::set('CurrentMainBase',validate($_POST['depselect']));
275 }
277 if($this->multiple_support_active){
278 $this->is_account = TRUE;
279 }
281 if(!$isCopyPaste && ! $this->multiple_support_active){
283 /* Do we need to flip is_account state? */
284 if(isset($_POST['modify_state'])){
285 if($this->is_account && $this->acl_is_removeable()){
286 $this->is_account= FALSE;
287 }elseif(!$this->is_account && $this->acl_is_createable()){
288 $this->is_account= TRUE;
289 }
290 }
292 /* Do we represent a valid posixAccount? */
293 if (!$this->is_account && $this->parent === NULL ){
294 $display= "<img alt=\"\" src=\"images/small-error.png\" align=\"middle\"> <b>".
295 msgPool::noValidExtension(_("POSIX"))."</b>";
296 $display.= back_to_main();
297 return ($display);
298 }
301 /* Show tab dialog headers */
302 if ($this->parent !== NULL){
303 if ($this->is_account){
304 if (isset($this->parent->by_object['sambaAccount'])){
305 $obj= $this->parent->by_object['sambaAccount'];
306 }
307 if (isset($obj) && $obj->is_account == TRUE &&
308 ((isset($this->parent->by_object['sambaAccount']))&&($this->parent->by_object['sambaAccount']->is_account))
309 ||(isset($this->parent->by_object['environment'] ))&&($this->parent->by_object['environment'] ->is_account)){
311 /* Samba3 dependency on posix accounts are enabled
312 in the moment, because I need to rely on unique
313 uidNumbers. There'll be a better solution later
314 on. */
315 $display= $this->show_disable_header(msgPool::removeFeaturesButton(_("POSIX")), msgPool::featuresEnabled(_("POSIX"), array(_("Samba"), _("Environment"))), TRUE);
316 } else {
317 $display= $this->show_disable_header(msgPool::removeFeaturesButton(_("POSIX")), msgPool::featuresEnabled(_("POSIX")));
318 }
319 } else {
320 $display= $this->show_enable_header(msgPool::addFeaturesButton(_("POSIX")), msgPool::featuresDisabled(_("POSIX")));
321 return($display);
322 }
323 }
324 }
326 /* Trigger group edit? */
327 if (isset($_POST['edit_groupmembership'])){
328 $this->group_dialog= TRUE;
329 $this->dialog= TRUE;
330 }
332 /* Cancel group edit? */
333 if (isset($_POST['add_groups_cancel']) ||
334 isset($_POST['add_groups_finish'])){
335 $this->group_dialog= FALSE;
336 $this->dialog= FALSE;
337 }
339 /* Add selected groups */
340 if (isset($_POST['add_groups_finish']) && isset($_POST['groups']) &&
341 count($_POST['groups'])){
343 $this->addGroup ($_POST['groups']);
344 }
346 /* Delete selected groups */
347 if (isset($_POST['delete_groupmembership']) &&
348 isset($_POST['group_list']) && count($_POST['group_list'])){
350 $this->delGroup ($_POST['group_list']);
351 }
353 /* Add user workstation? */
354 if (isset($_POST["add_ws"])){
355 $this->show_ws_dialog= TRUE;
356 $this->dialog= TRUE;
357 }
359 /* Add user workstation? */
360 if (isset($_POST["add_ws_finish"]) && isset($_POST['wslist'])){
361 foreach($_POST['wslist'] as $ws){
362 $this->accessTo[$ws]= $ws;
363 }
364 ksort($this->accessTo);
365 $this->is_modified= TRUE;
366 }
368 /* Remove user workstations? */
369 if (isset($_POST["delete_ws"]) && isset($_POST['workstation_list'])){
370 foreach($_POST['workstation_list'] as $name){
371 unset ($this->accessTo[$name]);
372 }
373 $this->is_modified= TRUE;
374 }
376 /* Add user workstation finished? */
377 if (isset($_POST["add_ws_finish"]) || isset($_POST["add_ws_cancel"])){
378 $this->show_ws_dialog= FALSE;
379 $this->dialog= FALSE;
380 }
382 /* Templates now! */
383 $smarty= get_smarty();
385 /* Show ws dialog */
386 if ($this->show_ws_dialog){
387 /* Save data */
388 $sysfilter= session::get("sysfilter");
389 foreach( array("depselect", "regex") as $type){
390 if (isset($_POST[$type])){
391 $sysfilter[$type]= $_POST[$type];
392 }
393 }
394 if (isset($_GET['search'])){
395 $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
396 if ($s == "**"){
397 $s= "*";
398 }
399 $sysfilter['regex']= $s;
400 }
401 session::set("sysfilter", $sysfilter);
403 /* Get workstation list */
404 $exclude= "";
405 foreach($this->accessTo as $ws){
406 $exclude.= "(cn=$ws)";
407 }
408 if ($exclude != ""){
409 $exclude= "(!(|$exclude))";
410 }
411 $regex= $sysfilter['regex'];
413 $acls = array();
414 if(class_available("servgeneric")) $acls[] = "server";
415 if(class_available("workgeneric")) $acls[] = "worstation";
416 if(class_available("termgeneric")) $acls[] = "terminal";
418 $filter= "(&(|(objectClass=goServer)(objectClass=gotoWorkstation)(objectClass=gotoTerminal))$exclude(cn=*)(cn=$regex))";
420 $deps_a = array(get_ou("serverRDN"),
421 get_ou("terminalRDN"),
422 get_ou("workstationRDN"));
424 $res= get_sub_list($filter, $acls, $deps_a, get_ou("systemRDN").$sysfilter['depselect'], array("cn"), GL_SUBSEARCH | GL_SIZELIMIT);
425 $wslist= array();
426 foreach ($res as $attrs){
427 $wslist[]= preg_replace('/\$/', '', $attrs['cn'][0]);
428 }
429 asort($wslist);
430 $smarty->assign("search_image", get_template_path('images/lists/search.png'));
431 $smarty->assign("launchimage", get_template_path('images/lists/action.png'));
432 $smarty->assign("tree_image", get_template_path('images/lists/search-subtree.png'));
433 $smarty->assign("deplist", $this->config->idepartments);
434 $smarty->assign("alphabet", generate_alphabet());
435 foreach( array("depselect", "regex") as $type){
436 $smarty->assign("$type", $sysfilter[$type]);
437 }
438 $smarty->assign("hint", print_sizelimit_warning());
439 $smarty->assign("wslist", $wslist);
440 $smarty->assign("apply", apply_filter());
441 $display= $smarty->fetch (get_template_path('trust_machines.tpl', TRUE, dirname(__FILE__)));
442 return ($display);
443 }
445 /* Manage group add dialog */
446 if ($this->group_dialog){
448 /* Get global filter config */
449 $this->reload();
451 /* remove already assigned groups */
452 $glist= array();
453 foreach ($this->grouplist as $key => $value){
454 if (!isset($this->groupMembership[$key]) && obj_is_writable($key,"groups/group","memberUid")){
455 $glist[$key]= $value;
456 }
457 }
459 if($this->SubSearch){
460 $smarty->assign("SubSearchCHK"," checked ");
461 }else{
462 $smarty->assign("SubSearchCHK","");
463 }
465 $smarty->assign("regex",$this->GroupRegex);
466 $smarty->assign("guser",$this->GroupUserRegex);
467 $smarty->assign("groups", $glist);
468 $smarty->assign("search_image", get_template_path('images/lists/search.png'));
469 $smarty->assign("launchimage", get_template_path('images/lists/action.png'));
470 $smarty->assign("tree_image", get_template_path('images/lists/search-subtree.png'));
471 $smarty->assign("deplist", $this->config->idepartments);
472 $smarty->assign("alphabet", generate_alphabet());
473 $smarty->assign("depselect", session::get('CurrentMainBase'));
474 $smarty->assign("hint", print_sizelimit_warning());
475 $smarty->assign("apply", apply_filter());
477 $display.= $smarty->fetch (get_template_path('posix_groups.tpl', TRUE, dirname(__FILE__)));
478 return ($display);
479 }
481 // Handle ssh dialog?
482 if ($this->ssh && preg_match('/[rw]/', $this->getacl("sshPublicKey"))) {
483 if ($result= $this->ssh->execute()) {
484 $this->dialog= true;
485 return $result;
486 }
487 $this->dialog= false;
488 }
491 /* Show main page */
492 $smarty= get_smarty();
494 /* In 'MyAccount' mode, we must remove write acls if we are not in editing mode. */
495 $SkipWrite = (!isset($this->parent) || !$this->parent) && !session::is_set('edit');
497 $smarty->assign("sshPublicKeyACL", $this->getacl("sshPublicKey"));
499 /* Depending on pwmode, currently hardcoded because there are no other methods */
500 if ( 1 == 1 ){
501 $smarty->assign("pwmode", dirname(__FILE__)."/posix_shadow");
503 $shadowMinACL = $this->getacl("shadowMin",$SkipWrite);
504 $smarty->assign("shadowmins", sprintf(_("Password can't be changed up to %s days after last change"),
505 "<input name=\"shadowMin\" size=3 maxlength=4 value=\"".$this->shadowMin."\">"));
507 $shadowMaxACL = $this->getacl("shadowMax",$SkipWrite);
508 $smarty->assign("shadowmaxs", sprintf(_("Password must be changed after %s days"),
509 "<input name=\"shadowMax\" size=3 maxlength=4 value=\"".$this->shadowMax."\">"));
511 $shadowInactiveACL= $this->getacl("shadowInactive",$SkipWrite);
512 $smarty->assign("shadowinactives", sprintf(_("Disable account after %s days of inactivity after password expiry"),
513 "<input name=\"shadowInactive\" size=3 maxlength=4 value=\"".$this->shadowInactive."\">"));
515 $shadowWarningACL = $this->getacl("shadowWarning",$SkipWrite);
516 $smarty->assign("shadowwarnings", sprintf(_("Warn user %s days before password expiry"),
517 "<input name=\"shadowWarning\" size=3 maxlength=4 value=\"".$this->shadowWarning."\">"));
519 foreach( array("activate_shadowMin", "activate_shadowMax",
520 "activate_shadowExpire", "activate_shadowInactive","activate_shadowWarning") as $val){
521 if ($this->$val == 1){
522 $smarty->assign("$val", "checked");
523 } else {
524 $smarty->assign("$val", "");
525 }
526 $smarty->assign("$val"."ACL", $this->getacl($val,$SkipWrite));
527 }
529 $smarty->assign("mustchangepasswordACL", $this->getacl("mustchangepassword",$SkipWrite));
530 }
532 // Set last system login
533 $smarty->assign("gotoLastSystemLogin",$this->gotoLastSystemLogin);
535 /* Fill calendar */
536 /* If this $this->shadowExpire is empty
537 use current date as base for calculating selectbox values.
538 (This attribute is empty if this is a new user )*/
539 if(empty($this->shadowExpire)){
540 $date= getdate(time());
541 }else{
542 $date= getdate($this->shadowExpire);
543 }
545 $days= array();
546 for($d= 1; $d<32; $d++){
547 $days[$d]= $d;
548 }
549 $years= array();
550 for($y= $date['year']-10; $y<$date['year']+10; $y++){
551 $years[]= $y;
552 }
553 $months= msgPool::months();
554 $smarty->assign("day", $date["mday"]);
555 $smarty->assign("days", $days);
556 $smarty->assign("months", $months);
557 $smarty->assign("month", $date["mon"]-1);
558 $smarty->assign("years", $years);
559 $smarty->assign("year", $date["year"]);
561 /* Fill arrays */
562 $smarty->assign("shells", $this->loginShellList);
563 $smarty->assign("secondaryGroups", $this->secondaryGroups);
564 $smarty->assign("primaryGroup", $this->primaryGroup);
565 if(!$this->multiple_support_active){
566 if (!count($this->groupMembership)){
567 $smarty->assign("groupMembership", array(" "));
568 } else {
569 $smarty->assign("groupMembership", $this->groupMembership);
570 }
571 }else{
572 $smarty->assign("groupMembership", $this->groupMembership);
573 $smarty->assign("groupMembership_some", $this->groupMembership_some);
574 }
575 if (count($this->groupMembership) > 16){
576 $smarty->assign("groups", "too_many_for_nfs");
577 } else {
578 $smarty->assign("groups", "");
579 }
581 /* Avoid "Undefined index: forceMode" */
582 $smarty->assign("forceMode", "");
584 /* Checkboxes */
585 if ($this->force_ids == 1){
586 $smarty->assign("force_ids", "checked");
587 if (session::get('js')){
588 $smarty->assign("forceMode", "");
589 }
590 } else {
591 if (session::get('js')){
592 $smarty->assign("forceMode", "disabled");
593 }
594 $smarty->assign("force_ids", "");
595 }
597 /* Create onClick="" action string for the "Force UID/GID" option
598 */
599 $onClickIDS ="";
600 if(preg_match("/w/",$this->getacl("uidNumber",$SkipWrite))){
601 $onClickIDS .= "changeState('uidNumber');";
602 }
603 if(preg_match("/w/",$this->getacl("gidNumber",$SkipWrite))){
604 $onClickIDS .= "changeState('gidNumber');";
605 }
606 $smarty->assign("onClickIDS", $onClickIDS);
607 $smarty->assign("force_idsACL", $this->getacl("uidNumber",$SkipWrite).$this->getacl("gidNumber",$SkipWrite));
609 foreach(array("primaryGroup","trustmode","activate_shadowWarning","activate_shadowInactive","activate_shadowMin","activate_shadowMax","activate_shadowExpire","mustchangepassword") as $val){
610 if(in_array($val,$this->multi_boxes)){
611 $smarty->assign("use_".$val,TRUE);
612 }else{
613 $smarty->assign("use_".$val,FALSE);
614 }
615 }
618 /* Load attributes and acl's */
619 foreach($this->attributes as $val){
620 if(in_array($val,$this->multi_boxes)){
621 $smarty->assign("use_".$val,TRUE);
622 }else{
623 $smarty->assign("use_".$val,FALSE);
624 }
626 if((session::get("js"))&&(($val=="uidNumber")||($val=="gidNumber")))
627 {
628 $smarty->assign("$val"."ACL",$this->getacl($val,$SkipWrite));
629 $smarty->assign("$val", $this->$val);
630 continue;
631 }
632 $smarty->assign("$val", $this->$val);
633 $smarty->assign("$val"."ACL", $this->getacl($val,$SkipWrite));
634 }
635 if($SkipWrite){
636 $smarty->assign("groupMembershipACL","r");
637 }else{
638 $smarty->assign("groupMembershipACL","rw");
639 }
640 $smarty->assign("status", $this->status);
642 /* Work on trust modes */
643 $smarty->assign("trusthide", " disabled ");
644 $smarty->assign("trustmodeACL", $this->getacl("trustModel",$SkipWrite));
645 if ($this->trustModel == "fullaccess"){
646 $trustmode= 1;
647 // pervent double disable tag in html code, this will disturb our clean w3c html
648 $smarty->assign("trustmode", $this->getacl("trustModel",$SkipWrite));
650 } elseif ($this->trustModel == "byhost"){
651 $trustmode= 2;
652 $smarty->assign("trusthide", "");
653 } else {
654 // pervent double disable tag in html code, this will disturb our clean w3c html
655 $smarty->assign("trustmode", $this->getacl("trustModel",$SkipWrite));
656 $trustmode= 0;
657 }
658 $smarty->assign("trustmode", $trustmode);
659 $smarty->assign("trustmodes", array( 0 => _("disabled"), 1 => _("full access"),
660 2 => _("allow access to these hosts")));
664 if((count($this->accessTo))==0)
665 $smarty->assign("emptyArrAccess",true);
666 else
667 $smarty->assign("emptyArrAccess",false);
669 if($this->mustchangepassword){
670 $smarty->assign("mustchangepassword", " checked ");
671 } else {
672 $smarty->assign("mustchangepassword", "");
673 }
675 $smarty->assign("workstations", $this->accessTo);
677 // Add SSH button if available
678 if ($this->ssh) {
679 $smarty->assign("sshPublicKey", 1);
680 }
682 $smarty->assign("apply", apply_filter());
683 $smarty->assign("multiple_support" , $this->multiple_support_active);
684 $display.= $smarty->fetch (get_template_path('generic.tpl', TRUE, dirname(__FILE__)));
685 return($display);
686 }
689 /* remove object from parent */
690 function remove_from_parent()
691 {
692 /* Cancel if there's nothing to do here */
693 if ((!$this->initially_was_account) || (!$this->acl_is_removeable())){
694 return;
695 }
698 /* Remove and write to LDAP */
699 plugin::remove_from_parent();
701 /* Zero out array */
702 $this->attrs['gosaHostACL']= array();
704 /* Keep uid, because we need it for authentification! */
705 unset($this->attrs['uid']);
706 unset($this->attrs['trustModel']);
708 @DEBUG (DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__,
709 /* include global link_info */
710 $this->attributes, "Save");
711 $ldap= $this->config->get_ldap_link();
712 $ldap->cd($this->dn);
713 $this->cleanup();
714 $ldap->modify ($this->attrs);
716 new log("remove","users/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
718 if (!$ldap->success()){
719 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_DEL, get_class()));
720 }
722 /* Delete group only if cn is uid and there are no other
723 members inside */
724 $ldap->cd ($this->config->current['BASE']);
725 $ldap->search ("(&(objectClass=posixGroup)(gidNumber=".$this->gidNumber."))", array("cn", "memberUid"));
726 if ($ldap->count() != 0){
727 $attrs= $ldap->fetch();
728 if ($attrs['cn'][0] == $this->uid &&
729 !isset($this->attrs['memberUid'])){
731 $ldap->rmDir($ldap->getDN());
732 }
733 }
735 /* Optionally execute a command after we're done */
736 $this->handle_post_events("remove",array("uid" => $this->uid));
737 }
740 function save_object()
741 {
742 if (isset($_POST['posixTab'])){
743 /* Save values to object */
744 plugin::save_object();
747 /* Save force GID checkbox */
748 if($this->acl_is_writeable("gidNumber") || $this->acl_is_writeable("uidNumber")){
749 if (isset ($_POST['force_ids'])){
750 $data= 1;
751 } else {
752 $data= 0;
753 }
754 if ($this->force_ids != $data){
755 $this->is_modified= TRUE;
756 }
757 $this->force_ids= $data;
758 }
760 /*Save primary group settings */
761 if($this->acl_is_writeable("primaryGroup") && isset($_POST['primaryGroup'])){
762 $data= $_POST['primaryGroup'];
763 if ($this->primaryGroup != $data){
764 $this->is_modified= TRUE;
765 }
766 $this->primaryGroup= $_POST['primaryGroup'];
767 }
769 /* Get seelcted shadow checkboxes */
770 foreach(array("shadowMin","shadowMax","shadowExpire","shadowInactive","shadowWarning") as $var) {
771 if($this->acl_is_writeable($var)){
772 $activate_var = "activate_".$var;
773 if(isset($_POST['activate_'.$var])){
774 $this->$activate_var = true;
775 $this->$var = $_POST[$var];
776 }else{
777 $this->$activate_var = false;
778 $this->$var = 0;
779 }
780 }
781 }
783 /* Force change password ? */
784 if(isset($_POST['mustchangepassword'])){
785 $this->mustchangepassword = TRUE;
786 }else{
787 $this->mustchangepassword = FALSE;
788 }
790 /* Trust mode - special handling */
791 if($this->acl_is_writeable("trustModel")){
792 if (isset($_POST['trustmode'])){
793 $saved= $this->trustModel;
794 if ($_POST['trustmode'] == "1"){
795 $this->trustModel= "fullaccess";
796 } elseif ($_POST['trustmode'] == "2"){
797 $this->trustModel= "byhost";
798 } else {
799 $this->trustModel= "";
800 }
801 if ($this->trustModel != $saved){
802 $this->is_modified= TRUE;
803 }
804 }
805 }
806 }
808 /* Get regex from alphabet */
809 if(isset($_GET['search'])){
810 $this->GroupRegex = $_GET['search']."*";
811 }
813 /* Check checkboxes and regexes */
814 if(isset($_POST["PosixGroupDialogPosted"])){
816 if(isset($_POST['SubSearch']) && ($_POST['SubSearch'])){
817 $this->SubSearch = true;
818 }else{
819 $this->SubSearch = false;
820 }
821 if(isset($_POST['guser'])){
822 $this->GroupUserRegex = $_POST['guser'];
823 }
824 if(isset($_POST['regex'])){
825 $this->GroupRegex = $_POST['regex'];
826 }
827 }
828 $this->GroupRegex = preg_replace("/\*\**/","*",$this->GroupRegex);
829 $this->GroupUserRegex = preg_replace("/\*\**/","*",$this->GroupUserRegex);
830 }
833 /* Save data to LDAP, depending on is_account we save or delete */
834 function save()
835 {
837 /* Adapt shadow values */
838 if (!$this->activate_shadowExpire){
839 $this->shadowExpire= "0";
840 } else {
841 /* Transform seconds to days here */
842 $this->shadowExpire= (int)($this->shadowExpire / (60 * 60 * 24)) ;
843 }
844 if (!$this->activate_shadowMax){
845 $this->shadowMax= "0";
846 }
847 if ($this->mustchangepassword){
848 $this->shadowLastChange= (int)(date("U") / 86400) - $this->shadowMax - 1;
849 } else {
850 $this->shadowLastChange= (int)(date("U") / 86400);
851 }
852 if (!$this->activate_shadowWarning){
853 $this->shadowWarning= "0";
854 }
856 /* Check what to do with ID's
857 Nothing forced, so we may have to generate our own IDs, if not done already.
858 */
859 if ($this->force_ids == 0){
861 /* Handle uidNumber.
862 * - use existing number if possible
863 * - if not, try to create a new uniqe one.
864 * */
865 if ($this->savedUidNumber != ""){
866 $this->uidNumber= $this->savedUidNumber;
867 } else {
869 /* Calculate new id's. We need to place a lock before calling get_next_id
870 to get real unique values.
871 */
872 $wait= 10;
873 while (get_lock("uidnumber") != ""){
874 sleep (1);
876 /* Oups - timed out */
877 if ($wait-- == 0){
878 msg_dialog::display(_("Warning"), _("Timeout while waiting for lock. Ignoring lock!"), WARNING_DIALOG);
879 break;
880 }
881 }
882 add_lock ("uidnumber", "gosa");
883 $this->uidNumber= $this->get_next_id("uidNumber", $this->dn);
884 }
885 }
888 /* Handle gidNumber
889 * - If we do not have a primary group selected (automatic), we will check if there
890 * is already a group with the same name and use this as primary.
891 * - .. if we couldn't find a group with the same name, we will create a new one,
892 * using the users uid as cn and a generated uniqe gidNumber.
893 * */
894 if ($this->primaryGroup == 0 || $this->force_ids){
896 /* Search for existing group */
897 $ldap = $this->config->get_ldap_link();
898 $ldap->cd($this->config->current['BASE']);
900 /* Are we forced to use a special gidNumber? */
901 if($this->force_ids){
902 $ldap->search("(&(objectClass=posixGroup)(gidNumber=".$this->gidNumber."))", array("cn","gidNumber"));
903 }else{
904 $ldap->search("(&(objectClass=posixGroup)(gidNumber=*)(cn=".$this->uid."))", array("cn","gidNumber"));
905 }
907 /* No primary group found, create a new one */
908 if ($ldap->count() == 0){
910 $groupcn = $this->uid;
911 $groupdn= preg_replace ('/^'.$this->config->get_cfg_value("accountPrimaryAttribute").'=[^,]+,'.get_people_ou().'/i',
912 'cn='.$groupcn.','.get_groups_ou(), $this->dn);
914 /* Request a new and uniqe gidNumber, if required */
915 if(!$this->force_ids){
916 $this->gidNumber= $this->get_next_id("gidNumber", $this->dn);
917 }else{
919 /* If forced gidNumber could not be found, then check if the given group name already exists
920 we do not want to modify the gidNumber of an existing group.
921 */
922 $cnt= 0;
923 while($ldap->dn_exists($groupdn)){
924 $cnt ++;
925 $groupcn = $this->uid."_".$cnt;
926 $groupdn= preg_replace ('/^'.$this->config->get_cfg_value("accountPrimaryAttribute").'=[^,]+,'.get_people_ou().'/i',
927 'cn='.$groupcn.','.get_groups_ou(), $this->dn);
928 }
929 }
931 /* Create new primary group and enforce the new gidNumber */
932 $g= new group($this->config, $groupdn);
933 $g->cn= $groupcn;
934 $g->force_gid= 1;
935 $g->gidNumber= $this->gidNumber;
936 $g->description= _("Group of user")." ".$this->givenName." ".$this->sn;
937 $g->save ();
939 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
940 sprintf("Primary group '%s' created, using gidNumber '%s'.",$groupcn,$this->gidNumber),"");
941 }else{
942 $attrs = $ldap->fetch();
943 $this->gidNumber = $attrs['gidNumber'][0];
944 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
945 "Found and used: <i>".$attrs['dn']."</i>",
946 sprintf("Primary group '%s' exists, gidNumber is '%s'.",$this->uid,$this->gidNumber));
947 }
948 }else{
950 /* Primary group was selected by user
951 */
952 $this->gidNumber = $this->primaryGroup;
953 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
954 sprintf("Primary group '%s' for user '%s' manually selected.",$this->gidNumber,$this->uid),"");
955 }
957 if ($this->activate_shadowMin != "1" ) {
958 $this->shadowMin = "";
959 }
961 if (($this->activate_shadowMax != "1") && ($this->mustchangepassword != "1")) {
962 $this->shadowMax = "";
963 }
965 if ($this->activate_shadowWarning != "1" ) {
966 $this->shadowWarning = "";
967 }
969 if ($this->activate_shadowInactive != "1" ) {
970 $this->shadowInactive = "";
971 }
973 if ($this->activate_shadowExpire != "1" ) {
974 $this->shadowExpire = "";
975 }
977 /* Fill gecos */
978 if (isset($this->parent) && $this->parent !== NULL){
979 $this->gecos= rewrite($this->parent->by_object['user']->cn);
980 if (!preg_match('/^[a-z0-9 -]+$/i', $this->gecos)){
981 $this->gecos= "";
982 }
983 }
985 foreach(array("shadowMin","shadowMax","shadowWarning","shadowInactive","shadowExpire") as $attr){
986 $this->$attr = (int) $this->$attr;
987 }
988 /* Call parents save to prepare $this->attrs */
989 plugin::save();
991 /* Trust accounts */
992 $objectclasses= array();
993 foreach ($this->attrs['objectClass'] as $key => $class){
994 if (preg_match('/trustAccount/i', $class)){
995 continue;
996 }
997 $objectclasses[]= $this->attrs['objectClass'][$key];
998 }
999 $this->attrs['objectClass']= $objectclasses;
1000 if ($this->trustModel != ""){
1001 $this->attrs['objectClass'][]= "trustAccount";
1002 $this->attrs['trustModel']= $this->trustModel;
1003 $this->attrs['accessTo']= array();
1004 if ($this->trustModel == "byhost"){
1005 foreach ($this->accessTo as $host){
1006 $this->attrs['accessTo'][]= $host;
1007 }
1008 }
1009 } else {
1010 if ($this->was_trust_account){
1011 $this->attrs['accessTo']= array();
1012 $this->attrs['trustModel']= array();
1013 }
1014 }
1016 if(empty($this->attrs['gosaDefaultPrinter'])){
1017 $thid->attrs['gosaDefaultPrinter']=array();
1018 }
1021 /* include global link_info */
1022 $this->cleanup();
1024 /* This is just a test, we have had duplicated ids
1025 in the past when copy & paste was used.
1026 Normaly this should not happen.
1027 */
1028 if(isset($this->attrs['uidNumber']) && !$this->force_ids){
1029 $used = $this->get_used_uid_numbers();
1030 if(isset($used[$this->attrs['uidNumber']]) && $used[$this->attrs['uidNumber']] != $this->dn){
1031 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);
1032 }
1033 }
1035 $ldap= $this->config->get_ldap_link();
1036 $ldap->cd($this->dn);
1037 unset($this->attrs['uid']);
1038 $ldap->modify ($this->attrs);
1040 /* Log last action */
1041 if($this->initially_was_account){
1042 new log("modify","users/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
1043 }else{
1044 new log("create","users/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
1045 }
1047 if (!$ldap->success()){
1048 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_MOD, get_class()));
1049 }
1051 /* Remove lock needed for unique id generation */
1052 del_lock ("uidnumber");
1054 // Save ssh stuff if needed
1055 if ($this->ssh) {
1056 $this->ssh->setDN($this->dn);
1057 $this->ssh->save();
1058 }
1060 /* Take care about groupMembership values: add to groups */
1061 foreach ($this->groupMembership as $key => $value){
1062 if (!isset($this->savedGroupMembership[$key])){
1063 $g= new grouptabs($this->config,$this->config->data['TABS']['GROUPTABS'], $key,"groups");
1064 $g->set_acl_base($key);
1065 $g->by_object['group']->addUser($this->uid);
1066 $g->save();
1067 }
1068 }
1070 /* Remove groups not listed in groupMembership */
1071 foreach ($this->savedGroupMembership as $key => $value){
1072 if (!isset($this->groupMembership[$key])){
1073 $g= new grouptabs($this->config,$this->config->data['TABS']['GROUPTABS'], $key,"groups");
1074 $g->set_acl_base($key);
1075 $g->by_object['group']->removeUser ($this->uid);
1076 $g->save();
1077 }
1078 }
1080 /* Optionally execute a command after we're done */
1081 if ($this->initially_was_account == $this->is_account){
1082 if ($this->is_modified){
1083 $this->handle_post_events("modify",array("uid" => $this->uid));
1084 }
1085 } else {
1086 $this->handle_post_events("add" ,array("uid"=> $this->uid));
1087 }
1088 }
1091 /* Check formular input */
1092 function check()
1093 {
1094 /* Include global link_info */
1095 $ldap= $this->config->get_ldap_link();
1097 /* Append groups as memberGroup: to check hook
1098 */
1099 $tmp_attributes = $this->attributes;
1100 $this->attributes[] = "memberGroup";
1101 $this->memberGroup = array();
1102 foreach($this->groupMembership as $dn => $name){
1103 $this->memberGroup[] = $name;
1104 }
1106 /* Call common method to give check the hook */
1107 $message= plugin::check();
1108 $this->attributes = $tmp_attributes;
1110 /* must: homeDirectory */
1111 if ($this->homeDirectory == ""){
1112 $message[]= msgPool::required(_("Home directory"));
1113 }
1114 if (!tests::is_path($this->homeDirectory)){
1115 $message[]= msgPool::invalid(_("Home directory"), "", "", "/home/yourname" );
1116 }
1118 /* Check ID's if they are forced by user */
1119 if ($this->force_ids == "1"){
1121 /* Valid uid/gid? */
1122 if (!tests::is_id($this->uidNumber)){
1123 $message[]= msgPool::invalid(_("UID"), $this->uidNumber, "/[0-9]/");
1124 } else {
1125 if ($this->uidNumber < $this->config->get_cfg_value("minId")){
1126 $message[]= msgPool::toosmall(_("UID"), $this->config->get_cfg_value("minId"));
1127 }
1128 }
1129 if (!tests::is_id($this->gidNumber)){
1130 $message[]= msgPool::invalid(_("GID"), $this->gidNumber, "/[0-9]/");
1131 } else {
1132 if ($this->gidNumber < $this->config->get_cfg_value("minId")){
1133 $message[]= msgPool::toosmall(_("GID"), $this->config->get_cfg_value("minId"));
1134 }
1135 }
1136 }
1138 /* Check shadow settings, well I like spaghetties... */
1139 if ($this->activate_shadowMin){
1140 if (!tests::is_id($this->shadowMin)){
1141 $message[]= msgPool::invalid(_("shadowMin"), $this->shadowMin, "/[0-9]/");
1142 }
1143 }
1144 if ($this->activate_shadowMax){
1145 if (!tests::is_id($this->shadowMax)){
1146 $message[]= msgPool::invalid(_("shadowMax"), $this->shadowMax, "/[0-9]/");
1147 }
1148 }
1149 if ($this->activate_shadowWarning){
1150 if (!tests::is_id($this->shadowWarning)){
1151 $message[]= msgPool::invalid(_("shadowWarning"), $this->shadowWarning, "/[0-9]/");
1152 }
1153 if (!$this->activate_shadowMax){
1154 $message[]= msgPool::depends("shadowWarning", "shadowMax");
1155 }
1156 if ($this->shadowWarning > $this->shadowMax){
1157 $message[]= msgPool::toobig("shadowWarning", "shadowMax");
1158 }
1159 if ($this->activate_shadowMin && $this->shadowWarning < $this->shadowMin){
1160 $message[]= msgPool::toosmall("shadowWarning", "shadowMin");
1161 }
1162 }
1163 if ($this->activate_shadowInactive){
1164 if (!tests::is_id($this->shadowInactive)){
1165 $message[]= msgPool::invalid(_("shadowInactive"), $this->shadowInactive, "/[0-9]/");
1166 }
1167 if (!$this->activate_shadowMax){
1168 $message[]= msgPool::depends("shadowInactive", "shadowMax");
1169 }
1170 }
1171 if ($this->activate_shadowMin && $this->activate_shadowMax){
1172 if ($this->shadowMin > $this->shadowMax){
1173 $message[]= msgPool::toobig("shadowMin", "shadowMax");
1174 }
1175 }
1177 return ($message);
1178 }
1181 function multiple_check()
1182 {
1183 $message = plugin::multiple_check();
1184 if ($this->homeDirectory == "" && in_array("homeDirectory",$this->multi_boxes)){
1185 $message[]= msgPool::required(_("Home directory"));
1186 }
1187 if (!tests::is_path($this->homeDirectory) && in_array("homeDirectory",$this->multi_boxes)){
1188 $message[]= msgPool::invalid(_("Home directory"), "", "", "/home/yourname" );
1189 }
1191 /* Check shadow settings, well I like spaghetties... */
1192 if ($this->activate_shadowMin && in_array("activate_shadowMin",$this->multi_boxes)){
1193 if (!tests::is_id($this->shadowMin)){
1194 $message[]= msgPool::invalid(_("shadowMin"), $this->shadowMin, "/[0-9]/");
1195 }
1196 }
1197 if ($this->activate_shadowMax && in_array("activate_shadowMax",$this->multi_boxes)){
1198 if (!tests::is_id($this->shadowMax)){
1199 $message[]= msgPool::invalid(_("shadowMax"), $this->shadowMax, "/[0-9]/");
1200 }
1201 }
1202 if ($this->activate_shadowWarning && in_array("activate_shadowWarning",$this->multi_boxes)){
1203 if (!tests::is_id($this->shadowWarning)){
1204 $message[]= msgPool::invalid(_("shadowWarning"), $this->shadowWarning, "/[0-9]/");
1205 }
1206 if (!$this->activate_shadowMax && in_array("activate_shadowMax",$this->multi_boxes)){
1207 $message[]= msgPool::depends("shadowWarning", "shadowMax");
1208 }
1209 if ($this->shadowWarning > $this->shadowMax && in_array("activate_shadowWarning",$this->multi_boxes)){
1210 $message[]= msgPool::toobig("shadowWarning", "shadowMax");
1211 }
1212 if ($this->activate_shadowMin && $this->shadowWarning < $this->shadowMin && in_array("activate_shadowMin",$this->multi_boxes)){
1213 $message[]= msgPool::tosmall("shadowWarning", "shadowMin");
1214 }
1215 }
1216 if ($this->activate_shadowInactive && in_array("activate_shadowInactive",$this->multi_boxes)){
1217 if (!tests::is_id($this->shadowInactive)){
1218 $message[]= msgPool::invalid(_("shadowInactive"), $this->shadowInactive, "/[0-9]/");
1219 }
1220 if (!$this->activate_shadowMax && in_array("activate_shadowMax",$this->multi_boxes)){
1221 $message[]= msgPool::depends("shadowInactive", "shadowMax");
1222 }
1223 }
1224 if ($this->activate_shadowMin && $this->activate_shadowMax && in_array("activate_shadowMin",$this->multi_boxes)){
1225 if ($this->shadowMin > $this->shadowMax){
1226 $message[]= msgPool::toobig("shadowMin", "shadowMax");
1227 }
1228 }
1230 return($message);
1231 }
1234 function addGroup ($groups)
1235 {
1236 /* include global link_info */
1237 $ldap= $this->config->get_ldap_link();
1239 /* Walk through groups and add the descriptive entry if not exists */
1240 foreach ($groups as $value){
1242 if (!array_key_exists($value, $this->groupMembership)){
1243 $ldap->cat($value, array('cn', 'description', 'dn'));
1244 $attrs= $ldap->fetch();
1245 error_reporting (0);
1246 if (!isset($attrs['description'][0])){
1247 $entry= $attrs["cn"][0];
1248 } else {
1249 $dsc= preg_replace ('/^Group of user/', _("Group of user"), $attrs["description"][0]);
1250 $entry= $attrs["cn"][0]." [$dsc]";
1251 }
1252 error_reporting (E_ALL | E_STRICT);
1254 if(obj_is_writable($attrs['dn'],"groups/group","memberUid")){
1255 $this->groupMembership[$attrs['dn']]= $entry;
1256 if($this->multiple_support_active && isset($this->groupMembership_some[$attrs['dn']])){
1257 unset($this->groupMembership_some[$attrs['dn']]);
1258 }
1259 }
1260 }
1261 }
1263 /* Sort groups */
1264 asort ($this->groupMembership);
1265 reset ($this->groupMembership);
1266 }
1269 /* Del posix user from some groups */
1270 function delGroup ($groups)
1271 {
1272 $dest= array();
1273 foreach($groups as $dn_to_del){
1274 if(isset($this->groupMembership[$dn_to_del]) && obj_is_writable($dn_to_del,"groups/group","memberUid")){
1275 unset($this->groupMembership[$dn_to_del]);
1276 }
1277 if($this->multiple_support_active){
1278 if(isset($this->groupMembership_some[$dn_to_del]) && obj_is_writable($dn_to_del,"groups/group","memberUid")){
1279 unset($this->groupMembership_some[$dn_to_del]);
1280 }
1281 }
1282 }
1283 }
1286 /* Adapt from template, using 'dn' */
1287 function adapt_from_template($dn, $skip= array())
1288 {
1289 /* Include global link_info */
1290 $ldap= $this->config->get_ldap_link();
1292 plugin::adapt_from_template($dn, $skip);
1293 $template= $this->attrs['uid'][0];
1295 /* Adapt group membership */
1296 $ldap->cd($this->config->current['BASE']);
1297 $ldap->search("(&(objectClass=posixGroup)(memberUid=".$this->attrs["uid"][0]."))", array("description", "cn"));
1299 while ($this->attrs= $ldap->fetch()){
1300 if (!isset($this->attrs["description"][0])){
1301 $entry= $this->attrs["cn"][0];
1302 } else {
1303 $entry= $this->attrs["cn"][0]." [".$this->attrs["description"][0]."]";
1304 }
1305 $this->groupMembership[$ldap->getDN()]= $entry;
1306 }
1308 /* Fix primary group settings */
1309 $ldap->cd($this->config->current['BASE']);
1310 $ldap->search("(&(objectClass=posixGroup)(cn=$template)(gidNumber=".$this->gidNumber."))", array("cn"));
1311 if ($ldap->count() != 1){
1312 $this->primaryGroup= $this->gidNumber;
1313 }
1315 $ldap->cd($this->config->current['BASE']);
1316 $ldap->search("(&(objectClass=gosaUserTemplate)(uid=".$template.")(accessTo=*))", array("cn","accessTo"));
1317 while($attr = $ldap->fetch()){
1318 $tmp = $attr['accessTo'];
1319 unset ($tmp['count']);
1320 $this->accessTo = $tmp;
1321 }
1323 /* Adjust shadow checkboxes */
1324 foreach (array("shadowMin", "shadowMax", "shadowWarning", "shadowInactive") as $val){
1325 if ($this->$val != 0){
1326 $oval= "activate_".$val;
1327 $this->$oval= "1";
1328 }
1329 }
1331 /* FIXME: NEED review of this section */
1332 /* Need to check shadowExpire separately */
1334 /*
1335 * If shadowExpire is not enabled in the template, it's a UNIX timestamp - so don't convert it to seconds.
1336 * The check is a hack - if difference between timestamp generated above and here is max 1 day.
1337 */
1338 if(abs($this->shadowExpire - time())>86400) {
1339 $this->shadowExpire= $this->convertToSeconds($this->shadowExpire);
1340 }
1342 /* Only enable checkbox, if shadowExpire is in the future */
1343 if($this->shadowExpire > time()) {
1344 $this->activate_shadowExpire= "1";
1345 }
1346 }
1348 function convertToSeconds($val)
1349 {
1350 if ($val != 0){
1351 $val*= 60 * 60 * 24;
1352 } else {
1353 $date= getdate();
1354 $val= floor($date[0] / (60*60*24)) * 60 * 60 * 24;
1355 }
1356 return($val);
1357 }
1360 function get_used_uid_numbers()
1361 {
1362 $ids= array();
1363 $ldap= $this->config->get_ldap_link();
1365 $ldap->cd ($this->config->current['BASE']);
1366 $ldap->search ("(&(objectClass=posixAccount)(uidNumber=*))", array("uidNumber"));
1368 /* Get list of ids */
1369 while ($attrs= $ldap->fetch()){
1370 $ids[$attrs['uidNumber'][0]] = $attrs['dn'];
1371 }
1372 return($ids);
1373 }
1376 function get_next_id($attrib, $dn)
1377 {
1378 $ids= array();
1379 $ldap= $this->config->get_ldap_link();
1381 $ldap->cd ($this->config->current['BASE']);
1382 if (preg_match('/gidNumber/i', $attrib)){
1383 $oc= "posixGroup";
1384 } else {
1385 $oc= "posixAccount";
1386 }
1387 $ldap->search ("(&(objectClass=$oc)($attrib=*))", array("$attrib"));
1389 /* Get list of ids */
1390 while ($attrs= $ldap->fetch()){
1391 $ids[]= (int)$attrs["$attrib"][0];
1392 }
1394 /* Add the nobody id */
1395 $ids[]= 65534;
1397 /* get the ranges */
1398 $tmp = array('0'=> 1000);
1399 if (preg_match('/posixAccount/', $oc) && $this->config->get_cfg_value("uidNumberBase") != ""){
1400 $tmp= split('-',$this->config->get_cfg_value("uidNumberBase"));
1401 } elseif($this->config->get_cfg_value("gidNumberBase") != ""){
1402 $tmp= split('-',$this->config->get_cfg_value("gidNumberBase"));
1403 }
1405 /* Set hwm to max if not set - for backward compatibility */
1406 $lwm= $tmp[0];
1407 if (isset($tmp[1])){
1408 $hwm= $tmp[1];
1409 } else {
1410 $hwm= pow(2,32);
1411 }
1413 /* Find out next free id near to UID_BASE */
1414 if ($this->config->get_cfg_value("baseIdHook") == ""){
1415 $base= $lwm;
1416 } else {
1417 /* Call base hook */
1418 $base= get_base_from_hook($dn, $attrib);
1419 }
1420 for ($id= $base; $id++; $id < pow(2,32)){
1421 if (!in_array($id, $ids)){
1422 return ($id);
1423 }
1424 }
1426 /* Should not happen */
1427 if ($id == $hwm){
1428 msg_dialog::display(_("Error"), _("Cannot allocate a free ID!"), ERROR_DIALOG);
1429 exit;
1430 }
1432 }
1434 function reload()
1435 {
1436 /* Set base for all searches */
1437 $base = session::get('CurrentMainBase');
1438 $base = $base;
1439 $ldap = $this->config->get_ldap_link();
1440 $attrs = array("cn", "description", "gidNumber");
1441 $Flags = GL_SIZELIMIT;
1443 /* Get groups */
1444 if ($this->GroupUserRegex == '*'){
1445 $filter = "(&(objectClass=posixGroup)(cn=".$this->GroupRegex."))";
1446 } else {
1447 $filter= "(&(objectClass=posixGroup)(cn=".$this->GroupRegex.")(memberUid=".$this->GroupUserRegex."))";
1448 }
1449 if($this->SubSearch){
1450 $Flags |= GL_SUBSEARCH;
1451 }else{
1452 $base = get_groups_ou().$base;
1453 }
1455 $res= get_list($filter, "groups", $base,$attrs, $Flags);
1457 /* check sizelimit */
1458 if (preg_match("/size limit/i", $ldap->get_error())){
1459 session::set('limit_exceeded',TRUE);
1460 }
1462 /* Create a list of users */
1463 $this->grouplist = array();
1464 foreach ($res as $value){
1465 $this->grouplist[$value['gidNumber'][0]]= $value;
1466 }
1468 $tmp=array();
1469 foreach($this->grouplist as $tkey => $val ){
1470 $tmp[strtolower($val['cn'][0]).$val['cn'][0]]=$val;
1471 }
1473 /* Sort index */
1474 ksort($tmp);
1476 /* Recreate index array[dn]=cn[description]*/
1477 $this->grouplist=array();
1478 foreach($tmp as $val){
1479 if(isset($val['description'])){
1480 $this->grouplist[$val['dn']]=$val['cn'][0]." [".$val['description'][0]."]";
1481 }else{
1482 $this->grouplist[$val['dn']]=$val['cn'][0];
1483 }
1484 }
1486 reset ($this->grouplist);
1487 }
1490 /* Get posts from copy & paste dialog */
1491 function saveCopyDialog()
1492 {
1493 if(isset($_POST['homeDirectory'])){
1494 $this->homeDirectory = $_POST['homeDirectory'];
1495 if (isset ($_POST['force_ids'])){
1496 $data= 1;
1497 $this->gidNumber = $_POST['gidNumber'];
1498 $this->uidNumber = $_POST['uidNumber'];
1499 } else {
1500 $data= 0;
1501 }
1502 if ($this->force_ids != $data){
1503 $this->is_modified= TRUE;
1504 }
1505 $this->force_ids= $data;
1506 $data= $_POST['primaryGroup'];
1507 if ($this->primaryGroup != $data){
1508 $this->is_modified= TRUE;
1509 }
1510 $this->primaryGroup= $_POST['primaryGroup'];
1511 }
1512 }
1515 /* Create the posix dialog part for copy & paste */
1516 function getCopyDialog()
1517 {
1518 /* Skip dialog creation if this is not a valid account*/
1519 if(!$this->is_account) return("");
1520 if ($this->force_ids == 1){
1521 $force_ids = "checked";
1522 if (session::get('js')){
1523 $forceMode = "";
1524 }
1525 } else {
1526 if (session::get('js')){
1527 if($this->acl != "#none#")
1528 $forceMode ="disabled";
1529 }
1530 $force_ids = "";
1531 }
1533 $sta = "";
1535 /* Open group add dialog */
1536 if(isset($_POST['edit_groupmembership'])){
1537 $this->group_dialog = TRUE;
1538 $sta = "SubDialog";
1539 }
1541 /* If the group-add dialog is closed, call execute
1542 to ensure that the membership is updatd */
1543 if(isset($_POST['add_groups_finish']) || isset($_POST['add_groups_cancel'])){
1544 $this->execute();
1545 $this->group_dialog =FALSE;
1546 }
1548 if($this->group_dialog){
1549 $str = $this->execute(true);
1550 $ret = array();
1551 $ret['string'] = $str;
1552 $ret['status'] = $sta;
1553 return($ret);
1554 }
1556 /* If a group member should be deleted, simply call execute */
1557 if(isset($_POST['delete_groupmembership'])){
1558 $this->execute();
1559 }
1561 /* Assigned informations to smarty */
1562 $smarty = get_smarty();
1563 $smarty->assign("homeDirectory",$this->homeDirectory);
1564 $smarty->assign("secondaryGroups",$this->secondaryGroups);
1565 $smarty->assign("primaryGroup",$this->primaryGroup);
1567 $smarty->assign("uidNumber",$this->uidNumber);
1568 $smarty->assign("gidNumber",$this->gidNumber);
1569 $smarty->assign("forceMode",$forceMode);
1570 $smarty->assign("force_ids",$force_ids);
1571 if (!count($this->groupMembership)){
1572 $smarty->assign("groupMembership", array(" "));
1573 } else {
1574 $smarty->assign("groupMembership", $this->groupMembership);
1575 }
1577 /* Display wars message if there are more than 16 group members */
1578 if (count($this->groupMembership) > 16){
1579 $smarty->assign("groups", "too_many_for_nfs");
1580 } else {
1581 $smarty->assign("groups", "");
1582 }
1583 $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE,dirname(__FILE__)));
1585 $ret = array();
1586 $ret['string'] = $str;
1587 $ret['status'] = $sta;
1588 return($ret);
1589 }
1592 function PrepareForCopyPaste($source)
1593 {
1594 plugin::PrepareForCopyPaste($source);
1596 /* Avoid using the same gid/uid number as source user
1597 empty numbers to enforce new ones. */
1598 # $this->savedUidNumber = $this->get_next_id("uidNumber", $this->dn);
1599 $this->savedUidNumber = "";
1600 # $this->savedGidNumber = $this->get_next_id("gidNumber", $this->dn);
1601 $this->savedGidNumber = "";
1603 /* Fill group */
1604 if(isset($source['gidNumber'][0])){
1605 $this->primaryGroup= $source['gidNumber'][0];
1606 }
1608 }
1611 function multiple_execute()
1612 {
1613 return($this->execute());
1614 }
1617 static function plInfo()
1618 {
1619 return (array(
1620 "plDescription" => _("POSIX account"),
1621 "plSelfModify" => TRUE,
1622 "plDepends" => array("user"),
1623 "plPriority" => 2,
1624 "plSection" => array("personal" => _("My account")),
1625 "plCategory" => array("users"),
1626 "plOptions" => array(),
1628 "plProvidedAcls" => array(
1630 "homeDirectory" => _("Home directory"),
1631 "loginShell" => _("Shell"),
1632 "uidNumber" => _("User ID"),
1633 "gidNumber" => _("Group ID"),
1635 "mustchangepassword"=> _("Force password change on login"),
1636 "shadowMin" => _("Shadow min"),
1637 "shadowMax" => _("Shadow max"),
1638 "shadowWarning" => _("Shadow warning"),
1639 "shadowInactive" => _("Shadow inactive"),
1640 "shadowExpire" => _("Shadow expire"),
1641 "sshPublickey" => _("Public SSH key"),
1642 "trustModel" => _("System trust model")))
1643 );
1644 }
1647 /* Return selected values for multiple edit */
1648 function get_multi_edit_values()
1649 {
1650 $ret = plugin::get_multi_edit_values();
1651 $ret['groupMembership'] = $this->groupMembership;
1652 $ret['groupMembership_some']= $this->groupMembership_some;
1654 if(in_array("primaryGroup",$this->multi_boxes)){
1655 $ret['primaryGroup'] = $this->primaryGroup;
1656 }
1657 if(in_array("trustmode",$this->multi_boxes)){
1658 $ret['trustModel'] = $this->trustModel;
1659 $ret['accessTo'] = $this->accessTo;
1660 }
1661 foreach(array("shadowWarning","shadowInactive","shadowMin","shadowMax", "shadowExpire") as $entry){
1662 $active = "activate_".$entry;
1663 if(in_array($active,$this->multi_boxes)){
1664 $ret[$entry] = $this->$entry;
1665 $ret[$active] = $this->$active;
1666 }
1667 }
1668 if(in_array("mustchangepassword",$this->multi_boxes)){
1669 $ret['mustchangepassword'] = $this->mustchangepassword;
1670 }
1671 return($ret);
1672 }
1675 /* Save posts for multiple edit
1676 */
1677 function multiple_save_object()
1678 {
1679 if(isset($_POST['posix_mulitple_edit'])){
1681 /* Backup expire value */
1682 $expire_tmp = $this->shadowExpire;
1684 /* Update all values */
1685 plugin::multiple_save_object();
1687 /* Get selected checkboxes */
1688 foreach(array("primaryGroup","trustmode","mustchangepassword","activate_shadowWarning","activate_shadowInactive","activate_shadowMin", "activate_shadowMax","activate_shadowExpire") as $val){
1689 if(isset($_POST["use_".$val])){
1690 $this->multi_boxes[] = $val;
1691 }
1692 }
1694 /* Update special values, checkboxes for posixShadow */
1695 foreach(array("shadowMin","shadowMax","shadowExpire","shadowInactive","shadowWarning") as $var) {
1696 if($this->acl_is_writeable($var)){
1697 $activate_var = "activate_".$var;
1698 if(in_array($activate_var, $this->multi_boxes)){
1699 if(isset($_POST['activate_'.$var])){
1700 $this->$activate_var = true;
1701 $this->$var = $_POST[$var];
1702 }else{
1703 $this->$activate_var = false;
1704 $this->$var = 0;
1705 }
1706 }
1707 }
1708 }
1710 /* Restore shadow value, if the shadow attribute isn't used */
1711 if(!in_array("activate_shadowExpire",$this->multi_boxes)){
1712 $this->shadowExpire = $expire_tmp;
1713 }
1715 /* Force change password ? */
1716 if(isset($_POST['mustchangepassword'])){
1717 $this->mustchangepassword = TRUE;
1718 }else{
1719 $this->mustchangepassword = FALSE;
1720 }
1722 /* Trust mode - special handling */
1723 if($this->acl_is_writeable("trustModel")){
1724 if (isset($_POST['trustmode'])){
1725 $saved= $this->trustModel;
1726 if ($_POST['trustmode'] == "1"){
1727 $this->trustModel= "fullaccess";
1728 } elseif ($_POST['trustmode'] == "2"){
1729 $this->trustModel= "byhost";
1730 } else {
1731 $this->trustModel= "";
1732 }
1733 if ($this->trustModel != $saved){
1734 $this->is_modified= TRUE;
1735 }
1736 }
1737 }
1739 /* Save primary group settings */
1740 if($this->acl_is_writeable("primaryGroup") && isset($_POST['primaryGroup'])){
1741 $data= $_POST['primaryGroup'];
1742 if ($this->primaryGroup != $data){
1743 $this->is_modified= TRUE;
1744 }
1745 $this->primaryGroup= $_POST['primaryGroup'];
1746 }
1747 }
1748 }
1751 /* Initialize plugin with given atribute arrays
1752 */
1753 function init_multiple_support($attrs,$all)
1754 {
1755 plugin::init_multiple_support($attrs,$all);
1757 /* Some dummy values */
1758 $groups_some = array();
1759 $groups_all = array();
1760 $groups_uid = array();
1761 $uids = array();
1762 $first = TRUE;
1764 /* Get all groups used by currently edited users */
1765 $uid_filter="";
1766 for($i =0; $i < $this->multi_attrs_all['uid']['count'] ; $i ++){
1767 $uid = $this->multi_attrs_all['uid'][$i];
1768 $uids[] = $uid;
1769 $uid_filter.= "(memberUid=".$uid.")";
1770 }
1771 $uid_filter = "(&(objectClass=posixGroup)(|".$uid_filter."))";
1772 $ldap = $this->config->get_ldap_link();
1773 $ldap->cd($this->config->current['BASE']);
1774 $ldap->search($uid_filter,array("dn","cn","memberUid"));
1775 while($group = $ldap->fetch()){
1776 $groups_some[$group['dn']] = $group['cn'][0];
1777 for($i = 0 ; $i < $group['memberUid']['count'] ; $i++){
1778 $groups_uid[$group['dn']][] = $group['memberUid'][$i];
1779 }
1780 }
1782 /* Create an array, containing all used groups */
1783 $groups_all = $groups_some;
1784 foreach($groups_all as $id => $group){
1785 foreach($uids as $uid){
1786 if(!in_array($uid,$groups_uid[$id])){
1787 unset($groups_all[$id]);
1788 break;
1789 }
1790 }
1791 }
1793 /* Assign group array */
1794 $this->groupMembership = $groups_all;
1796 /* Create an array of all grouops used by all users */
1797 foreach( $groups_all as $dn => $cn){
1798 if(isset($groups_some[$dn])){
1799 unset($groups_some[$dn]);
1800 }
1801 }
1802 $this->groupMembership_some = $groups_some;
1803 $this->primaryGroup = $this->gidNumber;
1805 /* Is this account a trustAccount? */
1806 if (isset($this->multi_attrs['trustModel'])){
1807 $this->trustModel= $this->multi_attrs['trustModel'][0];
1808 $this->was_trust_account= TRUE;
1809 $this->multi_boxes[] = "trustmode";
1810 } else {
1811 $this->was_trust_account= FALSE;
1812 $this->trustModel= "";
1813 }
1815 /* Create access informations */
1816 $this->accessTo = array();
1817 if (isset($this->multi_attrs['accessTo'])){
1818 for ($i= 0; $i<$this->multi_attrs['accessTo']['count']; $i++){
1819 $tmp= $this->multi_attrs['accessTo'][$i];
1820 $this->accessTo[$tmp]= $tmp;
1821 }
1822 }
1824 /* Adjust shadow checkboxes */
1825 foreach (array("shadowMin", "shadowMax", "shadowWarning", "shadowInactive",
1826 "shadowExpire") as $val){
1827 if ($this->$val != 0){
1828 $oval= "activate_".$val;
1829 $this->$oval= "1";
1830 }
1831 }
1833 /* Convert to seconds */
1834 if(isset($this->multi_attrs['shadowExpire'])){
1835 $this->shadowExpire = $this->convertToSeconds($this->multi_attrs['shadowExpire'][0]);
1836 }else{
1837 $this->activate_shadowExpire = FALSE;
1838 }
1839 }
1842 function set_multi_edit_values($attrs)
1843 {
1844 $groups = array();
1846 /* Update groupMembership, keep optinal group */
1847 foreach($attrs['groupMembership_some'] as $dn => $cn){
1848 if(isset($this->groupMembership[$dn])){
1849 $groups[$dn] = $cn;
1850 }
1851 }
1852 /* Update groupMembership, add forced groups */
1853 foreach($attrs['groupMembership'] as $dn => $cn){
1854 $groups[$dn] = $cn;
1855 }
1856 plugin::set_multi_edit_values($attrs);
1857 $this->groupMembership = $groups;
1858 }
1859 }
1861 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1862 ?>