Code

getShareServerList fix
[gosa.git] / plugins / personal / posix / class_posixAccount.inc
1 <?php
2 /*!
3   \brief   posixAccount plugin
4   \author  Cajus Pollmeier <pollmeier@gonicus.de>
5   \version 2.00
6   \date    24.07.2003
8   This class provides the functionality to read and write all attributes
9   relevant for posixAccounts and shadowAccounts from/to the LDAP. It
10   does syntax checking and displays the formulars required.
11  */
13 class posixAccount extends plugin
14 {
15   /* Definitions */
16   var $plHeadline= "UNIX";
17   var $plDescription= "This does something";
19   /* CLI vars */
20   var $cli_summary= "Manage users posix account";
21   var $cli_description= "Some longer text\nfor help";
22   var $cli_parameters= array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
24   /* Plugin specific values */
25   var $homeDirectory= "";
26   var $loginShell= "/bin/bash";
27   var $uidNumber= "";
28   var $gidNumber= "";
29   var $gecos= "";
30   var $shadowMin= "0";
31   var $shadowMax= "0";
32   var $shadowWarning= "0";
33   var $shadowLastChange= "0";
34   var $shadowInactive= "0";
35   var $shadowExpire= "0";
36   var $gosaDefaultPrinter= "";
37   var $gosaDefaultLanguage= "";
38   var $accessTo= array();
39   var $trustModel= "";
41   var $glist=array();
42   var $status= "";
43   var $loginShellList= array();
44   var $groupMembership= array();
45   var $savedGroupMembership= array();
46   var $savedUidNumber= "";
47   var $savedGidNumber= "";
48   var $use_shadowMin= "0";
49   var $use_shadowMax= "0";
50   var $use_shadowWarning= "0";
51   var $use_shadowInactive= "0";
52   var $use_shadowExpire= "0";
53   var $mustchangepassword= "0";
54   var $force_ids= 0;
55   var $printerList= array();
56   var $group_dialog= FALSE;
57   var $show_ws_dialog= FALSE;
58   var $secondaryGroups= array();
59   var $primaryGroup= 0;
60   var $was_trust_account= FALSE;
62   var $grouplist  = array();
63   var $ui         = array();
65   var $GroupRegex       = "*";
66   var $GroupUserRegex   = "*";
67   var $SubSearch        = false;
69   /* attribute list for save action */
70   var $CopyPasteVars  = array("grouplist","groupMembership","use_shadowMin","use_shadowMax",
71       "use_shadowWarning","use_shadowInactive","use_shadowExpire","mustchangepassword",
72       "force_ids","printerList","grouplist","savedGidNumber","savedUidNumber","savedGroupMembership");
74   var $attributes     = array("homeDirectory", "loginShell", "uidNumber", "gidNumber", "gecos",
75       "shadowMin", "shadowMax", "shadowWarning", "shadowInactive", "shadowLastChange",
76       "shadowExpire", "gosaDefaultPrinter", "gosaDefaultLanguage", "uid","accessTo","trustModel");
78   var $objectclasses= array("posixAccount", "shadowAccount");
81   /* constructor, if 'dn' is set, the node loads the given
82      'dn' from LDAP */
83   function posixAccount ($config, $dn= NULL)
84   {
85     /* Configuration is fine, allways */
86     $this->config= $config;
88     /* Load bases attributes */
89     plugin::plugin($config, $dn);
91     $ldap= $this->config->get_ldap_link();
93     if ($dn != NULL){
95       /* Correct is_account. shadowAccount is not required. */
96       if (isset($this->attrs['objectClass']) &&
97           in_array ('posixAccount', $this->attrs['objectClass'])){
99         $this->is_account= TRUE;
100       }
102       /* Is this account a trustAccount? */
103       if ($this->is_account && isset($this->attrs['trustModel'])){
104         $this->trustModel= $this->attrs['trustModel'][0];
105         $this->was_trust_account= TRUE;
106       } else {
107         $this->was_trust_account= FALSE;
108         $this->trustModel= "";
109       }
111       $this->accessTo = array(); 
112       if ($this->is_account && isset($this->attrs['accessTo'])){
113         for ($i= 0; $i<$this->attrs['accessTo']['count']; $i++){
114           $tmp= $this->attrs['accessTo'][$i];
115           $this->accessTo[$tmp]= $tmp;
116         }
117       }
118       $this->initially_was_account= $this->is_account;
120       /* Fill group */
121       $this->primaryGroup= $this->gidNumber;
123       /* Generate status text */
124       $current= date("U");
126       $current= floor($current / 60 /60 / 24);
128       if (($current >= $this->shadowExpire) && $this->shadowExpire){
129         $this->status= _("expired");
130         if (($current - $this->shadowExpire) < $this->shadowInactive){
131           $this->status.= ", "._("grace time active");
132         }
133       } elseif (($this->shadowLastChange + $this->shadowMin) >= $current){
134         $this->status= _("active, password not changable");
135       } elseif (($this->shadowLastChange + $this->shadowMax) >= $current){
136         $this->status= _("active, password expired");
137       } else {
138         $this->status= _("active");
139       }
141       /* Get group membership */
142       $ldap->cd($this->config->current['BASE']);
143       $ldap->search("(&(objectClass=posixGroup)(memberUid=".$this->uid."))", array("cn", "description"));
145       while ($this->attrs= $ldap->fetch()){
146         if (!isset($this->attrs["description"][0])){
147           $entry= $this->attrs["cn"][0];
148         } else {
149           $dsc= preg_replace ('/^Group of user/', _("Group of user"), $this->attrs["description"][0]);
150           $entry= $this->attrs["cn"][0]." [$dsc]";
151         }
152         $this->groupMembership[$ldap->getDN()]= $entry;
153       }
154       asort($this->groupMembership);
155       reset($this->groupMembership);
156       $this->savedGroupMembership= $this->groupMembership;
157       $this->savedUidNumber= $this->uidNumber;
158       $this->savedGidNumber= $this->gidNumber;
159     }
161     /* Adjust shadow checkboxes */
162     foreach (array("shadowMin", "shadowMax", "shadowWarning", "shadowInactive",
163           "shadowExpire") as $val){
165       if ($this->$val != 0){
166         $oval= "use_".$val;
167         $this->$oval= "1";
168       }
169     }
171     /* Convert to seconds */
172     if ($this->shadowExpire != 0){
173       $this->shadowExpire*= 60 * 60 * 24;
174     } else {
175       $date= getdate();
176       $this->shadowExpire= floor($date[0] / (60*60*24)) * 60 * 60 * 24;
177     }
179     /* Generate shell list from /etc/gosa/shells */
180     if (file_exists('/etc/gosa/shells')){
181       $shells = file ('/etc/gosa/shells');
182       foreach ($shells as $line){
183         if (!preg_match ("/^#/", $line)){
184           $this->loginShellList[]= trim($line);
185         }
186       }
187     } else {
188       if ($this->loginShell == ""){
189         $this->loginShellList[]= _("unconfigured");
190       }
191     }
193     /* Insert possibly missing loginShell */
194     if ($this->loginShell != "" && !in_array($this->loginShell, $this->loginShellList)){
195       $this->loginShellList[]= $this->loginShell;
196     }
198     /* Generate printer list */
199     if (isset($this->config->data['SERVERS']['CUPS'])){
200       $this->printerList= get_printer_list ($this->config->data['SERVERS']['CUPS']);
201       asort($this->printerList);
202     }
204     /* Generate group list */
205     $this->ui = get_userinfo(); 
206     $this->secondaryGroups[]= "- "._("automatic")." -";
207     $ldap->cd($this->config->current['BASE']);
208     $ldap->search("(objectClass=posixGroup)", array("cn", "gidNumber"));
209     while($attrs = $ldap->fetch()){
210       $this->secondaryGroups[$attrs['gidNumber'][0]]= $attrs['cn'][0];
211     }
212     asort ($this->secondaryGroups);
214     /* Get global filter config */
215     if (!is_global("sysfilter")){
216       $ui= get_userinfo();
217       $base= get_base_from_people($ui->dn);
218       $sysfilter= array( "depselect"       => $base,
219           "regex"           => "*");
220       register_global("sysfilter", $sysfilter);
221     }
222     $this->ui = get_userinfo();
223   }
226   /* execute generates the html output for this node */
227   function execute($isCopyPaste = false)
228   {
229     /* Call parent execute */
230     plugin::execute();
231     $display= "";
233     /* Department has changed? */
234     if(isset($_POST['depselect'])){
235       $_SESSION['CurrentMainBase']= validate($_POST['depselect']);
236     }
238     if(!$isCopyPaste){
240       /* Do we need to flip is_account state? */
241       if(isset($_POST['modify_state'])){
242         if($this->is_account && $this->acl_is_removeable()){
243           $this->is_account= FALSE;
244         }elseif(!$this->is_account && $this->acl_is_createable()){
245           $this->is_account= TRUE;
246         }
247       }
249       /* Do we represent a valid posixAccount? */
250       if (!$this->is_account && $this->parent == NULL ){
251         $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\">&nbsp;<b>".
252           _("This account has no unix extensions.")."</b>";
253         $display.= back_to_main();
254         return ($display);
255       }
258       /* Show tab dialog headers */
259       if ($this->parent != NULL){
260         if ($this->is_account){
261           if (isset($this->parent->by_object['sambaAccount'])){
262             $obj= $this->parent->by_object['sambaAccount'];
263           }
264           if (isset($obj) && $obj->is_account == TRUE &&
265               ((isset($this->parent->by_object['sambaAccount']))&&($this->parent->by_object['sambaAccount']->is_account))
266               ||(isset($this->parent->by_object['environment'] ))&&($this->parent->by_object['environment'] ->is_account)){
268             /* Samba3 dependency on posix accounts are enabled
269                in the moment, because I need to rely on unique
270                uidNumbers. There'll be a better solution later
271                on. */
272             $display= $this->show_disable_header(_("Remove posix account"),
273                 _("This account has unix features enabled. To disable them, you'll need to remove the samba / environment account first."), TRUE);
274           } else {
275             $display= $this->show_disable_header(_("Remove posix account"),
276                 _("This account has posix features enabled. You can disable them by clicking below."));
277           }
278         } else {
279           $display= $this->show_enable_header(_("Create posix account"),
280               _("This account has posix features disabled. You can enable them by clicking below."));
281           return($display);
282         }
283       }
284     }
285     /* Trigger group edit? */
286     if (isset($_POST['edit_groupmembership'])){
287       $this->group_dialog= TRUE;
288       $this->dialog= TRUE;
289     }
291     /* Cancel group edit? */
292     if (isset($_POST['add_groups_cancel']) ||
293         isset($_POST['add_groups_finish'])){
294       $this->group_dialog= FALSE;
295       $this->dialog= FALSE;
296     }
298     /* Add selected groups */
299     if (isset($_POST['add_groups_finish']) && isset($_POST['groups']) &&
300         count($_POST['groups'])){
302       $this->addGroup ($_POST['groups']);
303     }
305     /* Delete selected groups */
306     if (isset($_POST['delete_groupmembership']) && 
307         isset($_POST['group_list']) && count($_POST['group_list'])){
309       $this->delGroup ($_POST['group_list']);
310     }
312     /* Add user workstation? */
313     if (isset($_POST["add_ws"])){
314       $this->show_ws_dialog= TRUE;
315       $this->dialog= TRUE;
316     }
318     /* Add user workstation? */
319     if (isset($_POST["add_ws_finish"]) && isset($_POST['wslist'])){
320       foreach($_POST['wslist'] as $ws){
321         $this->accessTo[$ws]= $ws;
322       }
323       ksort($this->accessTo);
324       $this->is_modified= TRUE;
325     }
327     /* Remove user workstations? */
328     if (isset($_POST["delete_ws"]) && isset($_POST['workstation_list'])){
329       foreach($_POST['workstation_list'] as $name){
330         unset ($this->accessTo[$name]);
331       }
332       $this->is_modified= TRUE;
333     }
335     /* Add user workstation finished? */
336     if (isset($_POST["add_ws_finish"]) || isset($_POST["add_ws_cancel"])){
337       $this->show_ws_dialog= FALSE;
338       $this->dialog= FALSE;
339     }
341     /* Templates now! */
342     $smarty= get_smarty();
344     /* Show ws dialog */
345     if ($this->show_ws_dialog){
346       /* Save data */
347       $sysfilter= get_global("sysfilter");
348       foreach( array("depselect", "regex") as $type){
349         if (isset($_POST[$type])){
350           $sysfilter[$type]= $_POST[$type];
351         }
352       }
353       if (isset($_GET['search'])){
354         $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
355         if ($s == "**"){
356           $s= "*";
357         }
358         $sysfilter['regex']= $s;
359       }
360       register_global("sysfilter", $sysfilter);
362       /* Get workstation list */
363       $exclude= "";
364       foreach($this->accessTo as $ws){
365         $exclude.= "(cn=$ws)";
366       }
367       if ($exclude != ""){
368         $exclude= "(!(|$exclude))";
369       }
370       $regex= $sysfilter['regex'];
371       $filter= "(&(|(objectClass=goServer)(objectClass=gotoWorkstation)(objectClass=gotoTerminal))$exclude(cn=*)(cn=$regex))";
372       $res= get_list($filter, "groups", $sysfilter['depselect'], array("cn"), GL_SUBSEARCH | GL_SIZELIMIT);
373       $wslist= array();
374       foreach ($res as $attrs){
375         $wslist[]= preg_replace('/\$/', '', $attrs['cn'][0]);
376       }
377       asort($wslist);
378       $smarty->assign("search_image", get_template_path('images/search.png'));
379       $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
380       $smarty->assign("tree_image", get_template_path('images/tree.png'));
381       $smarty->assign("deplist", $this->config->idepartments);
382       $smarty->assign("alphabet", generate_alphabet());
383       foreach( array("depselect", "regex") as $type){
384         $smarty->assign("$type", $sysfilter[$type]);
385       }
386       $smarty->assign("hint", print_sizelimit_warning());
387       $smarty->assign("wslist", $wslist);
388       $smarty->assign("apply", apply_filter());
389       $display= $smarty->fetch (get_template_path('trust_machines.tpl', TRUE, dirname(__FILE__)));
390       return ($display);
391     }
393     /* Manage group add dialog */
394     if ($this->group_dialog){
396       /* Get global filter config */
397       $this->reload();
399       /* remove already assigned groups */
400       $glist= array();
401       foreach ($this->grouplist as $key => $value){
402         if (!isset($this->groupMembership[$key]) && obj_is_writable($key,"groups/group","memberUid")){
403           $glist[$key]= $value;
404         }
405       }
407       if($this->SubSearch){
408         $smarty->assign("SubSearchCHK"," checked ");
409       }else{
410         $smarty->assign("SubSearchCHK","");
411       }
413       $smarty->assign("regex",$this->GroupRegex);
414       $smarty->assign("guser",$this->GroupUserRegex);
415       $smarty->assign("groups", $glist);
416       $smarty->assign("search_image", get_template_path('images/search.png'));
417       $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
418       $smarty->assign("tree_image", get_template_path('images/tree.png'));
419       $smarty->assign("deplist", $this->config->idepartments);
420       $smarty->assign("alphabet", generate_alphabet());
421       $smarty->assign("depselect",$_SESSION['CurrentMainBase']);
422       $smarty->assign("hint", print_sizelimit_warning());
424       $smarty->assign("apply", apply_filter());
425       $display.= $smarty->fetch (get_template_path('posix_groups.tpl', TRUE, dirname(__FILE__)));
426       return ($display);
427     }
429     /* Show main page */
430     $smarty= get_smarty();
432     /* In 'MyAccount' mode, we must remove write acls if we are not in editing mode. */ 
433     $SkipWrite = (!isset($this->parent) || !$this->parent) && !isset($_SESSION['edit']);
435     /* Depending on pwmode, currently hardcoded because there are no other methods */
436     if ( 1 == 1 ){
437       $smarty->assign("pwmode", dirname(__FILE__)."/posix_shadow");
439       $shadowMinACL     =  $this->getacl("shadowMin",$SkipWrite);
440       $smarty->assign("shadowmins", sprintf(_("Password can't be changed up to %s days after last change"), 
441                                               "<input name=\"shadowMin\" size=3 maxlength=4 value=\"".$this->shadowMin."\">"));
443       $shadowMaxACL     =  $this->getacl("shadowMax",$SkipWrite);
444       $smarty->assign("shadowmaxs", sprintf(_("Password must be changed after %s days"), 
445                                               "<input name=\"shadowMax\" size=3 maxlength=4 value=\"".$this->shadowMax."\">"));
447       $shadowInactiveACL=  $this->getacl("shadowInactive",$SkipWrite);
448       $smarty->assign("shadowinactives", sprintf(_("Disable account after %s days of inactivity after password expiery"), 
449                                               "<input name=\"shadowInactive\" size=3 maxlength=4 value=\"".$this->shadowInactive."\">"));
451       $shadowWarningACL =  $this->getacl("shadowWarning",$SkipWrite);
452       $smarty->assign("shadowwarnings", sprintf(_("Warn user %s days before password expiery"), 
453                                               "<input name=\"shadowWarning\" size=3 maxlength=4 value=\"".$this->shadowWarning."\">"));
455       foreach( array("use_shadowMin", "use_shadowMax",
456                      "use_shadowExpire", "use_shadowInactive","use_shadowWarning") as $val){
457         if ($this->$val == 1){
458           $smarty->assign("$val", "checked");
459         } else {
460           $smarty->assign("$val", "");
461         }
462         $smarty->assign("$val"."ACL", $this->getacl($val,$SkipWrite));
463       }
465       if($this->mustchangepassword){
466         $smarty->assign("mustchangepassword", "checked");
467       } else {
468         $smarty->assign("mustchangepassword", "");
469       }
470       $smarty->assign("mustchangepasswordACL", $this->getacl("mustchangepassword",$SkipWrite));
471     }
473     /* Fill calendar */
474     $date= getdate($this->shadowExpire);
476     $days= array();
477     for($d= 1; $d<32; $d++){
478       $days[$d]= $d;
479     }
480     $years= array();
481     for($y= $date['year']-10; $y<$date['year']+10; $y++){
482       $years[]= $y;
483     }
484     $months= array(_("January"), _("February"), _("March"), _("April"),
485         _("May"), _("June"), _("July"), _("August"), _("September"),
486         _("October"), _("November"), _("December"));
487     $smarty->assign("day", $date["mday"]);
488     $smarty->assign("days", $days);
489     $smarty->assign("months", $months);
490     $smarty->assign("month", $date["mon"]-1);
491     $smarty->assign("years", $years);
492     $smarty->assign("year", $date["year"]);
494     /* Fill arrays */
495     $smarty->assign("shells", $this->loginShellList);
496     $smarty->assign("secondaryGroups", $this->secondaryGroups);
497     $smarty->assign("primaryGroup", $this->primaryGroup);
498     if (!count($this->groupMembership)){
499       $smarty->assign("groupMembership", array("&nbsp;"));
500     } else {
501       $smarty->assign("groupMembership", $this->groupMembership);
502     }
503     if (count($this->groupMembership) > 16){
504       $smarty->assign("groups", "too_many_for_nfs");
505     } else {
506       $smarty->assign("groups", "");
507     }
508     $smarty->assign("printerList", $this->printerList);
509     $smarty->assign("languages", $this->config->data['MAIN']['LANGUAGES']);
511     /* Avoid "Undefined index: forceMode" */
512     $smarty->assign("forceMode", "");
514     /* Checkboxes */
515     if ($this->force_ids == 1){
516       $smarty->assign("force_ids", "checked");
517       if ($_SESSION['js']){
518         $smarty->assign("forceMode", "");
519       }
520     } else {
521       if ($_SESSION['js']){
522 #FIXME: whats up here. remove acl?
523         if($this->acl != "#none#")
524           $smarty->assign("forceMode", "disabled");
525       }
526       $smarty->assign("force_ids", "");
527     }
529     
531     $smarty->assign("force_idsACL", $this->getacl("uidNumber",$SkipWrite).$this->getacl("gidNumber",$SkipWrite));
533     /* Load attributes and acl's */
534     foreach($this->attributes as $val){
535       if(($_SESSION["js"])&&(($val=="uidNumber")||($val=="gidNumber")))
536       {
537         $smarty->assign("$val"."ACL",$this->getacl($val,$SkipWrite));
538         $smarty->assign("$val", $this->$val);
539         continue;
540       }
541       $smarty->assign("$val", $this->$val);
542       $smarty->assign("$val"."ACL", $this->getacl($val,$SkipWrite));
543     }
544     if($SkipWrite){
545       $smarty->assign("groupMembershipACL","r");
546     }else{
547       $smarty->assign("groupMembershipACL","rw");
548     }
549     $smarty->assign("status", $this->status);
551     /* Work on trust modes */
552     $smarty->assign("trusthide", " disabled ");
553     $smarty->assign("trustmodeACL",  $this->getacl("trustModel",$SkipWrite));
554     if ($this->trustModel == "fullaccess"){
555       $trustmode= 1;
556       // pervent double disable tag in html code, this will disturb our clean w3c html
557       $smarty->assign("trustmode",  $this->getacl("trustModel",$SkipWrite));
559     } elseif ($this->trustModel == "byhost"){
560       $trustmode= 2;
561       $smarty->assign("trusthide", "");
562     } else {
563       // pervent double disable tag in html code, this will disturb our clean w3c html
564       $smarty->assign("trustmode",  $this->getacl("trustModel",$SkipWrite));
565       $trustmode= 0;
566     }
567     $smarty->assign("trustmode", $trustmode);
568     $smarty->assign("trustmodes", array( 0 => _("disabled"), 1 => _("full access"),
569           2 => _("allow access to these hosts")));
573     if((count($this->accessTo))==0)
574       $smarty->assign("emptyArrAccess",true);
575     else
576       $smarty->assign("emptyArrAccess",false);
580     $smarty->assign("workstations", $this->accessTo);
582     $smarty->assign("apply", apply_filter());
583     $display.= $smarty->fetch (get_template_path('generic.tpl', TRUE, dirname(__FILE__)));
584     return($display);
585   }
588   /* remove object from parent */
589   function remove_from_parent()
590   {
591     /* Cancel if there's nothing to do here */
592     if ((!$this->initially_was_account) || (!$this->acl_is_removeable())){
593       return;
594     }
596     /* include global link_info */
597     $ldap= $this->config->get_ldap_link();
599     /* Remove and write to LDAP */
600     plugin::remove_from_parent();
602     /* Zero out array */
603     $this->attrs['gosaHostACL']= array();
605     /* Keep uid, because we need it for authentification! */
606     unset($this->attrs['uid']);
607     unset($this->attrs['trustModel']);
609     @DEBUG (DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__,
610         $this->attributes, "Save");
611     $ldap->cd($this->dn);
612     $this->cleanup();
613     $ldap->modify ($this->attrs); 
615     show_ldap_error($ldap->get_error(), sprintf(_("Removing of user/posix account with dn '%s' failed."),$this->dn));
617     /* Delete group only if cn is uid and there are no other
618        members inside */
619     $ldap->cd ($this->config->current['BASE']);
620     $ldap->search ("(&(objectClass=posixGroup)(gidNumber=".$this->gidNumber."))", array("cn", "memberUid"));
621     if ($ldap->count() != 0){
622       $attrs= $ldap->fetch();
623       if ($attrs['cn'][0] == $this->uid &&
624           !isset($this->attrs['memberUid'])){
626         $ldap->rmDir($ldap->getDN());
627       }
628     }
630     /* Optionally execute a command after we're done */
631     $this->handle_post_events("remove");
632   }
635   function save_object()
636   {
637     if (isset($_POST['posixTab'])){
638       /* Save values to object */
639       plugin::save_object();
642       /* Save force GID checkbox */
643       if($this->acl_is_writeable("gidNumber") || $this->acl_is_writeable("uidNumber")){
644         if (isset ($_POST['force_ids'])){
645           $data= 1;
646         } else {
647           $data= 0;
648         }
649         if ($this->force_ids != $data){
650           $this->is_modified= TRUE;
651         }
652         $this->force_ids= $data;
653       }
655       /*Save primary group settings */
656       if($this->acl_is_writeable("primaryGroup") && isset($_POST['primaryGroup'])){
657         $data= $_POST['primaryGroup'];
658         if ($this->primaryGroup != $data){
659           $this->is_modified= TRUE;
660         }
661         $this->primaryGroup= $_POST['primaryGroup'];
662       }
664       foreach(array("shadowMin","shadowMax","shadowExpire","shadowInactive","shadowWarning","mustchangepassword") as $var) {
665         if($this->acl_is_writeable($var)){
666           $use_var = "use_".$var;
667           if(isset($_POST['use_'.$var])){
668             $this->$use_var  = true;
669             $this->$var      = $_POST[$var];
670           }else{
671             $this->$use_var  = false;
672             $this->$var      = 0;
673           }
674         }
675       }
677       /* Trust mode - special handling */
678       if($this->acl_is_writeable("trustModel")){
679         if (isset($_POST['trustmode'])){
680           $saved= $this->trustModel;
681           if ($_POST['trustmode'] == "1"){
682             $this->trustModel= "fullaccess";
683           } elseif ($_POST['trustmode'] == "2"){
684             $this->trustModel= "byhost";
685           } else {
686             $this->trustModel= "";
687           }
688           if ($this->trustModel != $saved){
689             $this->is_modified= TRUE;
690           }
691         }
692       }
693     }
695     /* Get regex from alphabet */
696     if(isset($_GET['search'])){
697       $this->GroupRegex = $_GET['search']."*";
698     }
700     /* Check checkboxes and regexes */
701     if(isset($_POST["PosixGroupDialogPosted"])){
703       if(isset($_POST['SubSearch']) && ($_POST['SubSearch'])){
704         $this->SubSearch = true;
705       }else{
706         $this->SubSearch = false;
707       }
708       if(isset($_POST['guser'])){
709         $this->GroupUserRegex = $_POST['guser'];
710       }
711       if(isset($_POST['regex'])){
712         $this->GroupRegex = $_POST['regex'];
713       }
714     }
715     $this->GroupRegex = preg_replace("/\*\**/","*",$this->GroupRegex);
716     $this->GroupUserRegex = preg_replace("/\*\**/","*",$this->GroupUserRegex);
717   }
720   /* Save data to LDAP, depending on is_account we save or delete */
721   function save()
722   {
724     /* include global link_info */
725     $ldap= $this->config->get_ldap_link();
727     /* Adapt shadow values */
728     if (!$this->use_shadowExpire){
729       $this->shadowExpire= "0";
730     } else {
731       /* Transform seconds to days here */
732       $this->shadowExpire= (int)($this->shadowExpire / (60 * 60 * 24)) ;
733     }
734     if (!$this->use_shadowMax){
735       $this->shadowMax= "0";
736     }
737     if ($this->mustchangepassword){
738       $this->shadowLastChange= (int)(date("U") / 86400) - $this->shadowMax - 1;
739     } else {
740       $this->shadowLastChange= (int)(date("U") / 86400);
741     }
742     if (!$this->use_shadowWarning){
743       $this->shadowWarning= "0";
744     }
746     /* Check what to do with ID's */
747     if ($this->force_ids == 0){
749       /* Use id's that are already set */
750       if ($this->savedUidNumber != ""){
751         $this->uidNumber= $this->savedUidNumber;
752         $this->gidNumber= $this->savedGidNumber;
753       } else {
755         /* Calculate new id's. We need to place a lock before calling get_next_id
756            to get real unique values. */
757         $wait= 10;
758         while (get_lock("uidnumber") != ""){
759           sleep (1);
761           /* Oups - timed out */
762           if ($wait-- == 0){
763             print_red (_("Failed: overriding lock"));
764             break;
765           }
766         }
768         add_lock ("uidnumber", "gosa");
769         $this->uidNumber= $this->get_next_id("uidNumber");
770         if ($this->savedGidNumber != ""){
771           $this->gidNumber= $this->savedGidNumber;
772         } else {
773           $this->gidNumber= $this->get_next_id("gidNumber");
774         }
775       }
777       if ($this->primaryGroup != 0){
778         $this->gidNumber= $this->primaryGroup;
779       }
780     }
782     if ($this->use_shadowMin != "1" ) {
783       $this->shadowMin = "";
784     }
786     if (($this->use_shadowMax != "1") && ($this->mustchangepassword != "1")) {
787       $this->shadowMax = "";
788     }
790     if ($this->use_shadowWarning != "1" ) {
791       $this->shadowWarning = "";
792     }
794     if ($this->use_shadowInactive != "1" ) {
795       $this->shadowInactive = "";
796     }
798     if ($this->use_shadowExpire != "1" ) {
799       $this->shadowExpire = "";
800     }
802     /* Fill gecos */
803     if (isset($this->parent) && $this->parent != NULL){
804       $this->gecos= rewrite($this->parent->by_object['user']->cn);
805       if (!preg_match('/[a-z0-9 -]/i', $this->gecos)){
806         $this->gecos= "";
807       }
808     }
810     foreach(array("shadowMin","shadowMax","shadowWarning","shadowInactive","shadowExpire") as $attr){
811       $this->$attr = (int) $this->$attr;
812     }
813     /* Call parents save to prepare $this->attrs */
814     plugin::save();
816     /* Trust accounts */
817     $objectclasses= array();
818     foreach ($this->attrs['objectClass'] as $key => $class){
819       if (preg_match('/trustAccount/i', $class)){
820         continue;
821       }
822       $objectclasses[]= $this->attrs['objectClass'][$key];
823     }
824     $this->attrs['objectClass']= $objectclasses;
825     if ($this->trustModel != ""){
826       $this->attrs['objectClass'][]= "trustAccount";
827       $this->attrs['trustModel']= $this->trustModel;
828       $this->attrs['accessTo']= array();
829       if ($this->trustModel == "byhost"){
830         foreach ($this->accessTo as $host){
831           $this->attrs['accessTo'][]= $host;
832         }
833       }
834     } else {
835       if ($this->was_trust_account){
836         $this->attrs['accessTo']= array();
837         $this->attrs['trustModel']= array();
838       }
839     }
841     if(empty($this->attrs['gosaDefaultPrinter'])){
842       $thid->attrs['gosaDefaultPrinter']=array();
843     }
846     /* Save data to LDAP */
847     $ldap->cd($this->dn);
848     $this->cleanup();
849     unset($this->attrs['uid']);
850     $ldap->modify ($this->attrs); 
852     show_ldap_error($ldap->get_error(), sprintf(_("Saving of user/posix account with dn '%s' failed."),$this->dn));
854     /* Remove lock needed for unique id generation */
855     del_lock ("uidnumber");
858     /* Posix accounts have group interrelationship, take care about these here. */
859     if ($this->force_ids == 0 && $this->primaryGroup == 0){
860       $ldap->cd($this->config->current['BASE']);
861       $ldap->search("(&(objectClass=posixGroup)(gidNumber=".$this->gidNumber."))", array("cn"));
863       /* Create group if it doesn't exist */
864       if ($ldap->count() == 0){
865         $groupdn= preg_replace ('/^'.$this->config->current['DNMODE'].'=[^,]+,'.get_people_ou().'/i', 'cn='.$this->uid.','.get_groups_ou(), $this->dn);
867         $g= new group($this->config, $groupdn);
868         $g->cn= $this->uid;
869         $g->force_gid= 1;
870         $g->gidNumber= $this->gidNumber;
871         $g->description= "Group of user ".$this->givenName." ".$this->sn;
872         $g->save ();
873       }
874     }
876     /* Take care about groupMembership values: add to groups */
877     foreach ($this->groupMembership as $key => $value){
878       $g= new grouptabs($this->config,$this->config->data['TABS']['GROUPTABS'], $key);
879       $g->by_object['group']->addUser($this->uid);
880       $g->save();
881     }
883     /* Remove from groups not listed in groupMembership */
884     foreach ($this->savedGroupMembership as $key => $value){
885       if (!isset($this->groupMembership[$key])){
886         $g= new grouptabs($this->config,$this->config->data['TABS']['GROUPTABS'], $key);
887         $g->by_object['group']->removeUser ($this->uid);
888         $g->save();
889       }
890     }
892     /* Optionally execute a command after we're done */
893     if ($this->initially_was_account == $this->is_account){
894       if ($this->is_modified){
895         $this->handle_post_events("mofify");
896       }
897     } else {
898       $this->handle_post_events("add");
899     }
900   }
902   /* Check formular input */
903   function check()
904   {
905     /* Include global link_info */
906     $ldap= $this->config->get_ldap_link();
908     /* Call common method to give check the hook */
909     $message= plugin::check();
911     /* must: homeDirectory */
912     if ($this->homeDirectory == ""){
913       $message[]= _("The required field 'Home directory' is not set.");
914     }
915     if (!is_path($this->homeDirectory)){
916       $message[]= _("Please enter a valid path in 'Home directory' field.");
917     }
919     /* Check ID's if they are forced by user */
920     if ($this->force_ids == "1"){
922       /* Valid uid/gid? */
923       if (!is_id($this->uidNumber)){
924         $message[]= _("Value specified as 'UID' is not valid.");
925       } else {
926         if ($this->uidNumber < $this->config->current['MINID']){
927           $message[]= _("Value specified as 'UID' is too small.");
928         }
929       }
930       if (!is_id($this->gidNumber)){
931         $message[]= _("Value specified as 'GID' is not valid.");
932       } else {
933         if ($this->gidNumber < $this->config->current['MINID']){
934           $message[]= _("Value specified as 'GID' is too small.");
935         }
936       }
937     }
939     /* Check shadow settings, well I like spaghetties... */
940     if ($this->use_shadowMin){
941       if (!is_id($this->shadowMin)){
942         $message[]= _("Value specified as 'shadowMin' is not valid.");
943       }
944     }
945     if ($this->use_shadowMax){
946       if (!is_id($this->shadowMax)){
947         $message[]= _("Value specified as 'shadowMax' is not valid.");
948       }
949     }
950     if ($this->use_shadowWarning){
951       if (!is_id($this->shadowWarning)){
952         $message[]= _("Value specified as 'shadowWarning' is not valid.");
953       }
954       if (!$this->use_shadowMax){
955         $message[]= _("'shadowWarning' without 'shadowMax' makes no sense.");
956       }
957       if ($this->shadowWarning > $this->shadowMax){
958         $message[]= _("Value specified as 'shadowWarning' should be smaller than 'shadowMax'.");
959       }
960       if ($this->use_shadowMin && $this->shadowWarning < $this->shadowMin){
961         $message[]= _("Value specified as 'shadowWarning' should be greater than 'shadowMin'.");
962       }
963     }
964     if ($this->use_shadowInactive){
965       if (!is_id($this->shadowInactive)){
966         $message[]= _("Value specified as 'shadowInactive' is not valid.");
967       }
968       if (!$this->use_shadowMax){
969         $message[]= _("'shadowInactive' without 'shadowMax' makes no sense.");
970       }
971     }
972     if ($this->use_shadowMin && $this->use_shadowMax){
973       if ($this->shadowMin > $this->shadowMax){
974         $message[]= _("Value specified as 'shadowMin' should be smaller than 'shadowMax'.");
975       }
976     }
978     //  if(empty($this->gosaDefaultPrinter)){
979     //    $message[]= _("You need to specify a valid default printer.");
980     //  }
982     return ($message);
983   }
985   function addGroup ($groups)
986   {
987     /* include global link_info */
988     $ldap= $this->config->get_ldap_link();
990     /* Walk through groups and add the descriptive entry if not exists */
991     foreach ($groups as $value){
992       if (!array_key_exists($value, $this->groupMembership)){
993         $ldap->cat($value, array('cn', 'description', 'dn'));
994         $attrs= $ldap->fetch();
995         error_reporting (0);
996         if (!isset($attrs['description'][0])){
997           $entry= $attrs["cn"][0];
998         } else {
999           $dsc= preg_replace ('/^Group of user/', _("Group of user"), $attrs["description"][0]);
1000           $entry= $attrs["cn"][0]." [$dsc]";
1001         }
1002         error_reporting (E_ALL);
1004         if(obj_is_writable($attrs['dn'],"groups/group","memberUid")){
1005           $this->groupMembership[$attrs['dn']]= $entry;
1006         }
1007       }
1008     }
1010     /* Sort groups */
1011     asort ($this->groupMembership);
1012     reset ($this->groupMembership);
1013   }
1016   /* Del posix user from some groups */
1017   function delGroup ($groups)
1018   {
1019     $dest= array();
1021     foreach ($this->groupMembership as $key => $value){
1022       if ((!in_array($key, $groups)) || (obj_is_writable($attrs['dn'],"groups/group","memberUid"))){
1023         $dest[$key]= $value;
1024       }
1025     }
1026     $this->groupMembership= $dest;
1027   }
1029   /* Adapt from template, using 'dn' */
1030   function adapt_from_template($dn)
1031   {
1032     /* Include global link_info */
1033     $ldap= $this->config->get_ldap_link();
1035     plugin::adapt_from_template($dn);
1036     $template= $this->attrs['uid'][0];
1038     /* Adapt group membership */
1039     $ldap->cd($this->config->current['BASE']);
1040     $ldap->search("(&(objectClass=posixGroup)(memberUid=".$this->attrs["uid"][0]."))", array("description", "cn"));
1042     while ($this->attrs= $ldap->fetch()){
1043       if (!isset($this->attrs["description"][0])){
1044         $entry= $this->attrs["cn"][0];
1045       } else {
1046         $entry= $this->attrs["cn"][0]." [".$this->attrs["description"][0]."]";
1047       }
1048       $this->groupMembership[$ldap->getDN()]= $entry;
1049     }
1051     /* Fix primary group settings */
1052     $ldap->cd($this->config->current['BASE']);
1053     $ldap->search("(&(objectClass=posixGroup)(cn=$template)(gidNumber=".$this->gidNumber."))", array("cn"));
1054     if ($ldap->count() != 1){
1055       $this->primaryGroup= $this->gidNumber;
1056     }
1058     $ldap->cd($this->config->current['BASE']);
1059     $ldap->search("(&(objectClass=gosaUserTemplate)(uid=".$template."))", array("cn","accessTo"));
1060     while($attr = $ldap->fetch()){
1061       $tmp = $attr['accessTo'];
1062       unset ($tmp['count']);
1063       $this->accessTo = $tmp;   
1064     }
1066     /* Adjust shadow checkboxes */
1067     foreach (array("shadowMin", "shadowMax", "shadowWarning", "shadowInactive",
1068           "shadowExpire") as $val){
1070       if ($this->$val != 0){
1071         $oval= "use_".$val;
1072         $this->$oval= "1";
1073       }
1074     }
1075   }
1077   function get_next_id($attrib)
1078   {
1079     $ids= array();
1080     $ldap= $this->config->get_ldap_link();
1082     $ldap->cd ($this->config->current['BASE']);
1083     if (preg_match('/gidNumber/i', $attrib)){
1084       $oc= "posixGroup";
1085     } else {
1086       $oc= "posixAccount";
1087     }
1088     $ldap->search ("(&(objectClass=$oc)($attrib=*))", array("$attrib"));
1090     /* Get list of ids */
1091     while ($attrs= $ldap->fetch()){
1092       $ids[]= (int)$attrs["$attrib"][0];
1093     }
1095     /* Find out next free id near to UID_BASE */
1096     for ($id= $this->config->current['UIDBASE']; $id++; $id<65000){
1097       if (!in_array($id, $ids)){
1098         return ($id);
1099       }
1100     }
1102     /* Should not happen */
1103     if ($id == 65000){
1104       print_red(_("Too many users, can't allocate a free ID!"));
1105       exit;
1106     }
1108   }
1110   function reload()
1111   {
1112     /* Set base for all searches */
1113     $base     = $_SESSION['CurrentMainBase'];
1114     $base     = $base;
1115     $ldap     = $this->config->get_ldap_link();    
1116     $attrs    =  array("cn", "description", "gidNumber");
1117     $Flags    = GL_SIZELIMIT;
1119     /* Get groups */
1120     if ($this->GroupUserRegex == '*'){
1121       $filter = "(&(objectClass=posixGroup)(cn=".$this->GroupRegex."))";
1122     } else {
1123       $filter= "(&(objectClass=posixGroup)(cn=".$this->GroupRegex.")(memberUid=".$this->GroupUserRegex."))";
1124     }
1125     if($this->SubSearch){
1126       $Flags |= GL_SUBSEARCH;
1127     }else{
1128       $base = get_groups_ou().$base;
1129     }
1131     $res= get_list($filter, "groups", $base,$attrs, $Flags);
1132   
1133     /* check sizelimit */
1134     if (preg_match("/size limit/i", $ldap->error)){
1135       $_SESSION['limit_exceeded']= TRUE;
1136     }
1138     /* Create a list of users */
1139     $this->grouplist = array();
1140     foreach ($res as $value){
1141       $this->grouplist[$value['gidNumber'][0]]= $value;
1142     }
1144     $tmp=array();
1145     foreach($this->grouplist as $tkey => $val ){
1146       $tmp[strtolower($val['cn'][0]).$val['cn'][0]]=$val;
1147     }
1149     /* Sort index */
1150     ksort($tmp);
1152     /* Recreate index array[dn]=cn[description]*/
1153     $this->grouplist=array();
1154     foreach($tmp as $val){
1155       if(isset($val['description'])){
1156         $this->grouplist[$val['dn']]=$val['cn'][0]."&nbsp;[".$val['description'][0]."]";
1157       }else{
1158         $this->grouplist[$val['dn']]=$val['cn'][0];
1159       }
1160     }
1162     reset ($this->grouplist);
1163   }
1166   /* Create the posix dialog part for copy & paste */
1167   function getCopyDialog()
1168   {
1169     /* Skip dialog creation if this is not a valid account*/
1170     if(!$this->is_account) return("");
1171     if ($this->force_ids == 1){
1172       $force_ids = "checked";
1173       if ($_SESSION['js']){
1174         $forceMode = "";
1175       }
1176     } else {
1177       if ($_SESSION['js']){
1178         if($this->acl != "#none#")
1179           $forceMode ="disabled";
1180       }
1181       $force_ids = "";
1182     }
1184     $sta = "";
1186     /* Open group add dialog */
1187     if(isset($_POST['edit_groupmembership'])){
1188       $this->group_dialog = TRUE;
1189       $sta = "SubDialog";
1190     }
1192     /* If the group-add dialog is closed, call execute 
1193        to ensure that the membership is updatd */
1194     if(isset($_POST['add_groups_finish']) || isset($_POST['add_groups_cancel'])){
1195       $this->execute();
1196       $this->group_dialog =FALSE;
1197     }
1199     if($this->group_dialog){
1200       $str = $this->execute(true);
1201       $ret = array();
1202       $ret['string'] = $str;
1203       $ret['status'] = $sta;
1204       return($ret);
1205     }
1207     /* If a group member should be deleted, simply call execute */
1208     if(isset($_POST['delete_groupmembership'])){
1209       $this->execute();
1210     }
1212     /* Assigned informations to smarty */
1213     $smarty = get_smarty();
1214     $smarty->assign("homeDirectory",$this->homeDirectory);
1215     $smarty->assign("uidNumber",$this->uidNumber);
1216     $smarty->assign("gidNumber",$this->gidNumber);
1217     $smarty->assign("forceMode",$forceMode);
1218     $smarty->assign("force_ids",$force_ids);
1219     if (!count($this->groupMembership)){
1220       $smarty->assign("groupMembership", array("&nbsp;"));
1221     } else {
1222       $smarty->assign("groupMembership", $this->groupMembership);
1223     }
1225     /* Display wars message if there are more than 16 group members */
1226     if (count($this->groupMembership) > 16){
1227       $smarty->assign("groups", "too_many_for_nfs");
1228     } else {
1229       $smarty->assign("groups", "");
1230     }
1231     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE,dirname(__FILE__)));
1233     $ret = array();
1234     $ret['string'] = $str;
1235     $ret['status'] = $sta;
1236     return($ret);
1237   }
1240   function plInfo()
1241   {
1242     return (array(
1243           "plDescription"     => _("POSIX account"),
1244           "plSelfModify"      => TRUE,
1245           "plDepends"         => array("user"),
1246           "plPriority"        => 1,
1247           "plSection"         => "personal", 
1248           "plCategory"        => array("users"),
1249           "plOptions"         => array(),
1251           "plProvidedAcls"  => array(
1253             "homeDirectory"       =>  _("Home directory"), 
1254             "loginShell"          =>  _("Shell"),
1255             "uidNumber"           =>  _("User ID"),
1256             "gidNumber"           =>  _("Group ID"),
1258             "mustchangepassword"=>  _("Force password change on login"),
1259             "shadowMin"           =>  _("Shadow min"),
1260             "shadowMax"           =>  _("Shadow max"),
1261             "shadowWarning"       =>  _("Shadow warning"),
1262             "shadowInactive"      =>  _("Shadow inactive"),
1263             "shadowExpire"        =>  _("Shadow expire"),
1264             "trustModel"          =>  _("System trust model")))
1265             );
1266   }
1269 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1270 ?>