Code

Posix account expiration date was set to 1970 for new created accounts
[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 $must_change_password= "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","use_shadowWarning","use_shadowInactive","use_shadowExpire","must_change_password","force_ids","printerList","grouplist","savedGidNumber","savedUidNumber","savedGroupMembership");
71   var $attributes     = array("homeDirectory", "loginShell", "uidNumber", "gidNumber", "gecos",
72       "shadowMin", "shadowMax", "shadowWarning", "shadowInactive", "shadowLastChange",
73       "shadowExpire", "gosaDefaultPrinter", "gosaDefaultLanguage", "uid","accessTo","trustModel");
74   var $objectclasses= array("posixAccount", "shadowAccount");
76   var $uid ="";
78   /* constructor, if 'dn' is set, the node loads the given
79      'dn' from LDAP */
80   function posixAccount ($config, $dn= NULL, $parent= NULL)
81   {
82     /* Configuration is fine, allways */
83     $this->config= $config;
85     /* Load bases attributes */
86     plugin::plugin($config, $dn, $parent);
88     /* set user id */    
89     if(isset($this->attrs['uid'])){
90       $this->uid = $this->attrs['uid'][0];
91     }
93     $ldap= $this->config->get_ldap_link();
95     if ($dn != NULL){
97       /* Correct is_account. shadowAccount is not required. */
98       if (isset($this->attrs['objectClass']) &&
99           in_array ('posixAccount', $this->attrs['objectClass'])){
101         $this->is_account= TRUE;
102       }
104       /* Is this account a trustAccount? */
105       if ($this->is_account && isset($this->attrs['trustModel'])){
106         $this->trustModel= $this->attrs['trustModel'][0];
107         $this->was_trust_account= TRUE;
108       } else {
109         $this->was_trust_account= FALSE;
110         $this->trustModel= "";
111       }
112          
113           $this->accessTo = array(); 
114       if ($this->is_account && isset($this->attrs['accessTo'])){
115         for ($i= 0; $i<$this->attrs['accessTo']['count']; $i++){
116           $tmp= $this->attrs['accessTo'][$i];
117           $this->accessTo[$tmp]= $tmp;
118         }
119       }
120       $this->initially_was_account= $this->is_account;
122       /* Fill group */
123       $this->primaryGroup= $this->gidNumber;
125       /* Generate status text */
126       $current= date("U");
128       $current= floor($current / 60 /60 / 24);
130       if (($current >= $this->shadowExpire) && $this->shadowExpire){
131         $this->status= _("expired");
132         if (($current - $this->shadowExpire) < $this->shadowInactive){
133           $this->status.= ", "._("grace time active");
134         }
135       } elseif (($this->shadowLastChange + $this->shadowMin) >= $current){
136         $this->status= _("active, password not changable");
137       } elseif (($this->shadowLastChange + $this->shadowMax) >= $current){
138         $this->status= _("active, password expired");
139       } else {
140         $this->status= _("active");
141       }
143       /* Get group membership */
144       $ldap->cd($this->config->current['BASE']);
145       $ldap->search("(&(objectClass=posixGroup)(memberUid=".$this->uid."))", array("cn", "description"));
147       while ($this->attrs= $ldap->fetch()){
148         if (!isset($this->attrs["description"][0])){
149           $entry= $this->attrs["cn"][0];
150         } else {
151           $dsc= preg_replace ('/^Group of user/', _("Group of user"), $this->attrs["description"][0]);
152           $entry= $this->attrs["cn"][0]." [$dsc]";
153         }
154         $this->groupMembership[$ldap->getDN()]= $entry;
155       }
156       asort($this->groupMembership);
157       reset($this->groupMembership);
158       $this->savedGroupMembership= $this->groupMembership;
159       $this->savedUidNumber= $this->uidNumber;
160       $this->savedGidNumber= $this->gidNumber;
161     }
163     /* Adjust shadow checkboxes */
164     foreach (array("shadowMin", "shadowMax", "shadowWarning", "shadowInactive",
165           "shadowExpire") as $val){
167       if ($this->$val != 0){
168         $oval= "use_".$val;
169         $this->$oval= "1";
170       }
171     }
173     /* Convert to seconds */
174     $this->shadowExpire= $this->convertToSeconds($this->shadowExpire);
176     /* Generate shell list from /etc/gosa/shells */
177     if (file_exists('/etc/gosa/shells')){
178       $shells = file ('/etc/gosa/shells');
179       foreach ($shells as $line){
180         if (!preg_match ("/^#/", $line)){
181           $this->loginShellList[]= trim($line);
182         }
183       }
184     } else {
185       if ($this->loginShell == ""){
186         $this->loginShellList[]= _("unconfigured");
187       }
188     }
190     /* Insert possibly missing loginShell */
191     if ($this->loginShell != "" && !in_array($this->loginShell, $this->loginShellList)){
192       $this->loginShellList[]= $this->loginShell;
193     }
195     /* Generate printer list */
196     if (isset($this->config->data['SERVERS']['CUPS'])){
197       $this->printerList= get_printer_list ($this->config->data['SERVERS']['CUPS']);
198       asort($this->printerList);
199     }
201     /* Set tag attribute if we've tagging activated */
202     $filter= "(objectClass=posixGroup)";
203     $ui= get_userinfo();
204     if ($ui->gosaUnitTag != "" && isset($config->current['STRICT_UNITS']) &&
205         preg_match('/TRUE/i', $config->current['STRICT_UNITS'])){
206       $filter= "(&(objectClass=posixGroup)(gosaUnitTag=".$ui->gosaUnitTag."))";
207     }
209     /* Generate group list */
210     $ldap->cd($this->config->current['BASE']);
211     $ldap->search("$filter", array("cn", "gidNumber"));
212     $this->secondaryGroups[]= "- "._("automatic")." -";
213     while ($attrs= $ldap->fetch()){
214       $this->secondaryGroups[$attrs['gidNumber'][0]]= $attrs['cn'][0];
215     }
216     asort ($this->secondaryGroups);
218     /* Get global filter config */
219     if (!is_global("sysfilter")){
220       $ui= get_userinfo();
221       $base= get_base_from_people($ui->dn);
222       $sysfilter= array( "depselect"       => $base,
223           "regex"           => "*");
224       register_global("sysfilter", $sysfilter);
225     }
226     $this->ui = get_userinfo();
227   }
230   /* execute generates the html output for this node */
231   function execute($isCopyPaste = false)
232   {
233         /* Call parent execute */
234         plugin::execute();
235   $display= "";
237   /* Department has changed? */
238   if(isset($_POST['depselect'])){
239     $_SESSION['CurrentMainBase']= validate($_POST['depselect']);
240   }
242   if(!$isCopyPaste){
243     /* Do we need to flip is_account state? */
244     if (isset($_POST['modify_state'])){
245       $this->is_account= !$this->is_account;
246     }
248     /* Do we represent a valid posixAccount? */
249     if (!$this->is_account && $this->parent == NULL ){
250       $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\">&nbsp;<b>".
251         _("This account has no unix extensions.")."</b>";
252       $display.= back_to_main();
253       return ($display);
254     }
257     /* Show tab dialog headers */
258     if ($this->parent != NULL){
259       if ($this->is_account){
260         if (isset($this->parent->by_object['sambaAccount'])){
261           $obj= $this->parent->by_object['sambaAccount'];
262         }
263         if (isset($obj) && $obj->is_account == TRUE &&
264             ((isset($this->parent->by_object['sambaAccount']))&&($this->parent->by_object['sambaAccount']->is_account))
265             ||(isset($this->parent->by_object['environment'] ))&&($this->parent->by_object['environment'] ->is_account)){
267           /* Samba3 dependency on posix accounts are enabled
268              in the moment, because I need to rely on unique
269              uidNumbers. There'll be a better solution later
270              on. */
271           $display= $this->show_header(_("Remove posix account"),
272               _("This account has unix features enabled. To disable them, you'll need to remove the samba / environment account first."), TRUE);
273         } else {
274           $display= $this->show_header(_("Remove posix account"),
275               _("This account has posix features enabled. You can disable them by clicking below."));
276         }
277       } else {
278         $display= $this->show_header(_("Create posix account"),
279             _("This account has posix features disabled. You can enable them by clicking below."));
280         return($display);
281       }
282     }
283   }
284   /* Trigger group edit? */
285   if (isset($_POST['edit_groupmembership'])){
286     $this->group_dialog= TRUE;
287     $this->dialog= TRUE;
288   }
290   /* Cancel group edit? */
291   if (isset($_POST['add_groups_cancel']) ||
292       isset($_POST['add_groups_finish'])){
293     $this->group_dialog= FALSE;
294     $this->dialog= FALSE;
295   }
297   /* Add selected groups */
298   if (isset($_POST['add_groups_finish']) && isset($_POST['groups']) &&
299       count($_POST['groups'])){
301       if (chkacl ($this->acl, "memberUid") == ""){
302         $this->addGroup ($_POST['groups']);
303         $this->is_modified= TRUE;
304       }
305     }
307     /* Delete selected groups */
308     if (isset($_POST['delete_groupmembership']) && 
309         isset($_POST['group_list']) && count($_POST['group_list'])){
311       if (chkacl ($this->acl, "memberUid") == ""){
312         $this->delGroup ($_POST['group_list']);
313         $this->is_modified= TRUE;
314       }
315     }
317     /* Add user workstation? */
318     if (isset($_POST["add_ws"])){
319       $this->show_ws_dialog= TRUE;
320       $this->dialog= TRUE;
321     }
323     /* Add user workstation? */
324     if (isset($_POST["add_ws_finish"]) && isset($_POST['wslist'])){
325       foreach($_POST['wslist'] as $ws){
326         $this->accessTo[$ws]= $ws;
327       }
328       ksort($this->accessTo);
329       $this->is_modified= TRUE;
330     }
332     /* Remove user workstations? */
333     if (isset($_POST["delete_ws"]) && isset($_POST['workstation_list'])){
334       foreach($_POST['workstation_list'] as $name){
335         unset ($this->accessTo[$name]);
336       }
337       $this->is_modified= TRUE;
338     }
340     /* Add user workstation finished? */
341     if (isset($_POST["add_ws_finish"]) || isset($_POST["add_ws_cancel"])){
342       $this->show_ws_dialog= FALSE;
343       $this->dialog= FALSE;
344     }
346     /* Templates now! */
347     $smarty= get_smarty();
349     /* Show ws dialog */
350     if ($this->show_ws_dialog){
351       /* Save data */
352       $sysfilter= get_global("sysfilter");
353       foreach( array("depselect", "regex") as $type){
354         if (isset($_POST[$type])){
355           $sysfilter[$type]= $_POST[$type];
356         }
357       }
358       if (isset($_GET['search'])){
359         $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
360         if ($s == "**"){
361           $s= "*";
362         }
363         $sysfilter['regex']= $s;
364       }
365       register_global("sysfilter", $sysfilter);
367       /* Get workstation list */
368       $exclude= "";
369       foreach($this->accessTo as $ws){
370         $exclude.= "(cn=$ws)";
371       }
372       if ($exclude != ""){
373         $exclude= "(!(|$exclude))";
374       }
375       $acl= array($this->config->current['BASE'] => ":all");
376       $regex= $sysfilter['regex'];
377       $filter= "(&(|(objectClass=goServer)(objectClass=gotoWorkstation)(objectClass=gotoTerminal))$exclude(cn=*)(cn=$regex))";
378       $res= get_list($filter, $acl, $sysfilter['depselect'], array("cn"), GL_SUBSEARCH | GL_SIZELIMIT);
379       $wslist= array();
380       foreach ($res as $attrs){
381         $wslist[]= preg_replace('/\$/', '', $attrs['cn'][0]);
382       }
383       asort($wslist);
384       $smarty->assign("search_image", get_template_path('images/search.png'));
385       $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
386       $smarty->assign("tree_image", get_template_path('images/tree.png'));
387       $smarty->assign("deplist", $this->config->idepartments);
388       $smarty->assign("alphabet", generate_alphabet());
389       foreach( array("depselect", "regex") as $type){
390         $smarty->assign("$type", $sysfilter[$type]);
391       }
392       $smarty->assign("hint", print_sizelimit_warning());
393       $smarty->assign("wslist", $wslist);
394       $smarty->assign("apply", apply_filter());
395       $display= $smarty->fetch (get_template_path('trust_machines.tpl', TRUE, dirname(__FILE__)));
396       return ($display);
397     }
399     /* Manage group add dialog */
400     if ($this->group_dialog){
402       /* Get global filter config */
403       $this->reload();
405       /* remove already assigned groups */
406       $glist= array();
407       foreach ($this->grouplist as $key => $value){
408         if (!isset($this->groupMembership[$key])){
409           $glist[$key]= $value;
410         }
411       }
413       if($this->SubSearch){
414         $smarty->assign("SubSearchCHK"," checked ");
415       }else{
416         $smarty->assign("SubSearchCHK","");
417       }
419       $smarty->assign("regex",$this->GroupRegex);
420       $smarty->assign("guser",$this->GroupUserRegex);
421       $smarty->assign("groups", $glist);
422       $smarty->assign("search_image", get_template_path('images/search.png'));
423       $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
424       $smarty->assign("tree_image", get_template_path('images/tree.png'));
425       $smarty->assign("deplist", $this->config->idepartments);
426       $smarty->assign("alphabet", generate_alphabet());
427       $smarty->assign("depselect",$_SESSION['CurrentMainBase']);
428       $smarty->assign("hint", print_sizelimit_warning());
430       $smarty->assign("apply", apply_filter());
431       $display.= $smarty->fetch (get_template_path('posix_groups.tpl', TRUE, dirname(__FILE__)));
432       return ($display);
433     }
435     /* Show main page */
436     $smarty= get_smarty();
438     /* Depending on pwmode, currently hardcoded because there are no other methods */
439     if ( 1 == 1 ){
440       $smarty->assign("pwmode", dirname(__FILE__)."/posix_shadow");
441       $shadowMinACL= chkacl($this->acl, "shadowMin");
442       $smarty->assign("shadowmins", sprintf(_("Password can't be changed up to %s days after last change"), "<input name=\"shadowMin\" size=3 maxlength=4 $shadowMinACL value=\"".$this->shadowMin."\">"));
443       $shadowMaxACL= chkacl($this->acl, "shadowMax");
444       $smarty->assign("shadowmaxs", sprintf(_("Password must be changed after %s days"), "<input name=\"shadowMax\" size=3 maxlength=4 $shadowMaxACL value=\"".$this->shadowMax."\">"));
445       $shadowInactiveACL= chkacl($this->acl, "shadowInactive");
446       $smarty->assign("shadowinactives", sprintf(_("Disable account after %s days of inactivity after password expiery"), "<input name=\"shadowInactive\" size=3 maxlength=4 $shadowInactiveACL value=\"".$this->shadowInactive."\">"));
447       $shadowWarningACL= chkacl($this->acl, "shadowWarning");
448       $smarty->assign("shadowwarnings", sprintf(_("Warn user %s days before password expiery"), "<input name=\"shadowWarning\" size=3 maxlength=4 $shadowWarningACL value=\"".$this->shadowWarning."\">"));
449       foreach( array("must_change_password", "use_shadowMin", "use_shadowMax",
450             "use_shadowExpire", "use_shadowInactive",
451             "use_shadowWarning") as $val){
452         if ($this->$val == 1){
453           $smarty->assign("$val", "checked");
454         } else {
455           $smarty->assign("$val", "");
456         }
457         $smarty->assign("$val"."ACL", chkacl($this->acl, $val));
458       }
459     }
461     /* Fill calendar */
462    
463     /* If this $this->shadowExpire is empty 
464         use current date as base for calculating selectbox values.
465        (This attribute is empty if this is a new user )*/ 
466     if(empty($this->shadowExpire)){
467       $date= getdate(time());
468     }else{
469       $date= getdate($this->shadowExpire);
470     }
472     $days= array();
473     for($d= 1; $d<32; $d++){
474       $days[$d]= $d;
475     }
476     $years= array();
477     for($y= $date['year']-10; $y<$date['year']+10; $y++){
478       $years[]= $y;
479     }
480     $months= array(_("January"), _("February"), _("March"), _("April"),
481         _("May"), _("June"), _("July"), _("August"), _("September"),
482         _("October"), _("November"), _("December"));
483     $smarty->assign("day", $date["mday"]);
484     $smarty->assign("days", $days);
485     $smarty->assign("months", $months);
486     $smarty->assign("month", $date["mon"]-1);
487     $smarty->assign("years", $years);
488     $smarty->assign("year", $date["year"]);
490     /* Fill arrays */
491     $smarty->assign("shells", $this->loginShellList);
492     $smarty->assign("secondaryGroups", $this->secondaryGroups);
493     $smarty->assign("primaryGroup", $this->primaryGroup);
494    if (!count($this->groupMembership)){
495       $smarty->assign("groupMembership", array("&nbsp;"));
496     } else {
497       $smarty->assign("groupMembership", $this->groupMembership);
498     }
499     if (count($this->groupMembership) > 16){
500       $smarty->assign("groups", "too_many_for_nfs");
501     } else {
502       $smarty->assign("groups", "");
503     }
504     $smarty->assign("printerList", $this->printerList);
505     $smarty->assign("languages", $this->config->data['MAIN']['LANGUAGES']);
507         /* Avoid "Undefined index: forceMode" */
508     $smarty->assign("forceMode", "");
510     /* Checkboxes */
511     if ($this->force_ids == 1){
512       $smarty->assign("force_ids", "checked");
513       if ($_SESSION['js']){
514         $smarty->assign("forceMode", "");
515       }
516     } else {
517       if ($_SESSION['js']){
518                 if($this->acl != "#none#")
519         $smarty->assign("forceMode", "disabled");
520       }
521       $smarty->assign("force_ids", "");
522     }
523     $smarty->assign("force_idsACL", chkacl($this->acl, "force_ids"));
525     /* Load attributes and acl's */
526     foreach($this->attributes as $val){
527       if((chkacl($this->acl,$val)=="")&&(($_SESSION["js"])&&(($val=="uidNumber")||($val=="gidNumber"))))
528         {
529           $smarty->assign("$val"."ACL","");
530           $smarty->assign("$val", $this->$val);
531           continue;
532         }
533       $smarty->assign("$val", $this->$val);
534       $smarty->assign("$val"."ACL", chkacl($this->acl,$val));
535     }
536     $smarty->assign("groupMembershipACL", chkacl($this->acl, "groupMembership"));
537     $smarty->assign("status", $this->status);
539     /* Work on trust modes */
540     $smarty->assign("trustmodeACL", chkacl($this->acl, "trustmode"));
541     if ($this->trustModel == "fullaccess"){
542       $trustmode= 1;
543       // pervent double disable tag in html code, this will disturb our clean w3c html
544     
545     if(chkacl($this->acl, "trustmode")==""){
546           $smarty->assign("trusthide", "disabled");
547       }else{
548           $smarty->assign("trusthide", "");
549       }
551     } elseif ($this->trustModel == "byhost"){
552       $trustmode= 2;
553       $smarty->assign("trusthide", "");
554     } else {
555       // pervent double disable tag in html code, this will disturb our clean w3c html
556       if(chkacl($this->acl, "trustmode")==""){
557           $smarty->assign("trusthide", "disabled");
558       }else{
559           $smarty->assign("trusthide", "");
560       }
561       $trustmode= 0;
562     }
563     $smarty->assign("trustmode", $trustmode);
564     $smarty->assign("trustmodes", array( 0 => _("disabled"), 1 => _("full access"),
565           2 => _("allow access to these hosts")));
569     if((count($this->accessTo))==0)
570       $smarty->assign("emptyArrAccess",true);
571     else
572       $smarty->assign("emptyArrAccess",false);
573     
576     $smarty->assign("workstations", $this->accessTo);
578     $smarty->assign("apply", apply_filter());
579     $display.= $smarty->fetch (get_template_path('generic.tpl', TRUE, dirname(__FILE__)));
580     return($display);
581   }
584   /* remove object from parent */
585   function remove_from_parent()
586   {
587     /* Cancel if there's nothing to do here */
588     if (!$this->initially_was_account){
589       return;
590     }
592     /* include global link_info */
593     $ldap= $this->config->get_ldap_link();
594     
595         /* Remove and write to LDAP */
596     plugin::remove_from_parent();
598     /* Zero out array */
599     $this->attrs['gosaHostACL']= array();
601     /* Keep uid, because we need it for authentification! */
602     unset($this->attrs['uid']);
603     unset($this->attrs['trustModel']);
605     @DEBUG (DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__,
606         $this->attributes, "Save");
607     $ldap->cd($this->dn);
608     $this->cleanup();
609     $ldap->modify ($this->attrs); 
611     show_ldap_error($ldap->get_error(), _("Removing UNIX account failed"));
613     /* Delete group only if cn is uid and there are no other
614        members inside */
615     $ldap->cd ($this->config->current['BASE']);
616     $ldap->search ("(&(objectClass=posixGroup)(gidNumber=".$this->gidNumber."))", array("cn", "memberUid"));
617     if ($ldap->count() != 0){
618       $attrs= $ldap->fetch();
619       if ($attrs['cn'][0] == $this->uid &&
620           !isset($this->attrs['memberUid'])){
622         $ldap->rmDir($ldap->getDN());
623       }
624     }
626     /* Optionally execute a command after we're done */
627     $this->handle_post_events("remove", array("uid" => $this->uid));
628   }
631   function save_object()
632   {
633     if (isset($_POST['posixTab'])){
634       /* Save values to object */
635       plugin::save_object();
637       /* Save force GID attribute */
638       if (chkacl ($this->acl, "force_ids") == ""){
639         if (isset ($_POST['force_ids'])){
640           $data= 1;
641         } else {
642           $data= 0;
643         }
644         if ($this->force_ids != $data){
645           $this->is_modified= TRUE;
646         }
647         $this->force_ids= $data;
649         $data= $_POST['primaryGroup'];
650         if ($this->primaryGroup != $data){
651           $this->is_modified= TRUE;
652         }
653         $this->primaryGroup= $_POST['primaryGroup'];
654       }
656       /* Save pwmode dependent attributes, curently hardcoded because there're
657          no alternatives */
658       if (1 == 1){
659         foreach( array("must_change_password", "use_shadowMin", "use_shadowMax",
660               "use_shadowExpire", "use_shadowInactive",
661               "use_shadowWarning") as $val){
662           if (chkacl($this->acl, "$val") == ""){
663             if (isset ($_POST[$val])){
664               $data= 1;
665             } else {
666               $data= 0;
667             }
668             if ($data != $this->$val){
669               $this->is_modified= TRUE;
670             }
671             $this->$val= $data;
672           }
673         }
674       }
676       /* Trust mode - special handling */
677       if (isset($_POST['trustmode'])){
678         $saved= $this->trustModel;
679         if ($_POST['trustmode'] == "1"){
680           $this->trustModel= "fullaccess";
681         } elseif ($_POST['trustmode'] == "2"){
682           $this->trustModel= "byhost";
683         } else {
684           $this->trustModel= "";
685         }
686         if ($this->trustModel != $saved){
687           $this->is_modified= TRUE;
688         }
689       }
690     }
692     /* Get regex from alphabet */
693     if(isset($_GET['search'])){
694       $this->GroupRegex = $_GET['search']."*";
695     }
697     /* Check checkboxes and regexes */
698     if(isset($_POST["PosixGroupDialogPosted"])){
699       if(isset($_POST['SubSearch']) && ($_POST['SubSearch'])){
700         $this->SubSearch = true;
701       }else{
702         $this->SubSearch = false;
703       }
704     
705       if(isset($_POST['guser'])){
706         $this->GroupUserRegex = $_POST['guser'];
707       }
708       if(isset($_POST['regex'])){
709         $this->GroupRegex = $_POST['regex'];
710       }
711     }
712     $this->GroupRegex = preg_replace("/\*\**/","*",$this->GroupRegex);
713     $this->GroupUserRegex = preg_replace("/\*\**/","*",$this->GroupUserRegex);
714   }
717   /* Save data to LDAP, depending on is_account we save or delete */
718   function save()
719   {
720         
721     /* include global link_info */
722     $ldap= $this->config->get_ldap_link();
724     /* Adapt shadow values */
725     if (!$this->use_shadowExpire){
726       $this->shadowExpire= "0";
727     } else {
728       /* Transform seconds to days here */
729       $this->shadowExpire= (int)($this->shadowExpire / (60 * 60 * 24)) ;
730     }
731     if (!$this->use_shadowMax){
732       $this->shadowMax= "0";
733     }
734     if ($this->must_change_password){
735       $this->shadowLastChange= (int)(date("U") / 86400) - $this->shadowMax - 1;
736     } else {
737       $this->shadowLastChange= (int)(date("U") / 86400);
738     }
739     if (!$this->use_shadowWarning){
740       $this->shadowWarning= "0";
741     }
743     /* Check what to do with ID's */
744     if ($this->force_ids == 0){
746       /* Use id's that are already set */
747       if ($this->savedUidNumber != ""){
748         $this->uidNumber= $this->savedUidNumber;
749         $this->gidNumber= $this->savedGidNumber;
750       } else {
752         /* Calculate new id's. We need to place a lock before calling get_next_id
753            to get real unique values. */
754         $wait= 10;
755         while (get_lock("uidnumber") != ""){
756           sleep (1);
758           /* Oups - timed out */
759           if ($wait-- == 0){
760             print_red (_("Failed: overriding lock"));
761             break;
762           }
763         }
765         add_lock ("uidnumber", "gosa");
766         $this->uidNumber= $this->get_next_id("uidNumber");
767         if ($this->savedGidNumber != ""){
768           $this->gidNumber= $this->savedGidNumber;
769         } else {
770           $this->gidNumber= $this->get_next_id("gidNumber");
771         }
772       }
774       if ($this->primaryGroup != 0){
775         $this->gidNumber= $this->primaryGroup;
776       }
777     }
779     if ($this->use_shadowMin != "1" ) {
780       $this->shadowMin = "";
781     }
783     if (($this->use_shadowMax != "1") && ($this->must_change_password != "1")) {
784       $this->shadowMax = "";
785     }
787     if ($this->use_shadowWarning != "1" ) {
788       $this->shadowWarning = "";
789     }
791     if ($this->use_shadowInactive != "1" ) {
792       $this->shadowInactive = "";
793     }
795     if ($this->use_shadowExpire != "1" ) {
796       $this->shadowExpire = "";
797     }
799     /* Fill gecos */
800     if (isset($this->parent) && $this->parent != NULL){
801       $this->gecos= rewrite($this->parent->by_object['user']->cn);
802       if (!preg_match('/^[a-z0-9 -]+$/i', $this->gecos)){
803         $this->gecos= "";
804       }
805     }
807         foreach(array("shadowMin","shadowMax","shadowWarning","shadowInactive","shadowExpire") as $attr){
808                 $this->$attr = (int) $this->$attr;
809         }
810     /* Call parents save to prepare $this->attrs */
811     plugin::save();
813     /* Trust accounts */
814     $objectclasses= array();
815     foreach ($this->attrs['objectClass'] as $key => $class){
816       if (preg_match('/trustAccount/i', $class)){
817         continue;
818       }
819       $objectclasses[]= $this->attrs['objectClass'][$key];
820     }
821     $this->attrs['objectClass']= $objectclasses;
822     if ($this->trustModel != ""){
823       $this->attrs['objectClass'][]= "trustAccount";
824       $this->attrs['trustModel']= $this->trustModel;
825       $this->attrs['accessTo']= array();
826       if ($this->trustModel == "byhost"){
827         foreach ($this->accessTo as $host){
828           $this->attrs['accessTo'][]= $host;
829         }
830       }
831     } else {
832       if ($this->was_trust_account){
833         $this->attrs['accessTo']= array();
834         $this->attrs['trustModel']= array();
835       }
836     }
838     if(empty($this->attrs['gosaDefaultPrinter'])){
839       $thid->attrs['gosaDefaultPrinter']=array();
840     }
843     /* Save data to LDAP */
844     $ldap->cd($this->dn);
845     $this->cleanup();
846     unset($this->attrs['uid']);
847     $ldap->modify ($this->attrs); 
849     show_ldap_error($ldap->get_error(), _("Saving UNIX account failed"));
851     /* Remove lock needed for unique id generation */
852     del_lock ("uidnumber");
855     /* Posix accounts have group interrelationship, take care about these here. */
856     if ($this->force_ids == 0 && $this->primaryGroup == 0){
857       $ldap->cd($this->config->current['BASE']);
858       $ldap->search("(&(objectClass=posixGroup)(gidNumber=".$this->gidNumber."))", array("cn"));
860       /* Create group if it doesn't exist */
861       if ($ldap->count() == 0){
862         $groupdn= preg_replace ('/^'.$this->config->current['DNMODE'].'=[^,]+,'.get_people_ou().'/i', 'cn='.$this->uid.','.get_groups_ou(), $this->dn);
864         $g= new group($this->config, $groupdn);
865         $g->cn= $this->uid;
866         $g->force_gid= 1;
867         $g->gidNumber= $this->gidNumber;
868         $g->description= "Group of user ".$this->givenName." ".$this->sn;
869         $g->save ();
870       }
871     }
872  
873     /* Take care about groupMembership values: add to groups */
874     foreach ($this->groupMembership as $key => $value){
875       if (!isset($this->savedGroupMembership[$key])){
876         $g= new grouptabs($this->config,$this->config->data['TABS']['GROUPTABS'], $key, false);
877         $g->by_object['group']->addUser($this->uid);
878         $g->save();
879       }
880     }
881     
882     /* Remove from groups not listed in groupMembership */
883     foreach ($this->savedGroupMembership as $key => $value){
884       if (!isset($this->groupMembership[$key])){
885         $g= new grouptabs($this->config,$this->config->data['TABS']['GROUPTABS'], $key);
886         $g->by_object['group']->removeUser ($this->uid);
887         $g->save();
888       }
889     }
891     /* Optionally execute a command after we're done */
892     if ($this->initially_was_account == $this->is_account){
893       if ($this->is_modified){
894         $this->handle_post_events("modify",array("uid" => $this->uid));
895       }
896     } else {
897       $this->handle_post_events("add",array("uid" , $this->uid));
898     }
899   }
901   /* Check formular input */
902   function check()
903   {
904     /* Include global link_info */
905     $ldap= $this->config->get_ldap_link();
907     /* Call common method to give check the hook */
908     $message= plugin::check();
910     /* must: homeDirectory */
911     if ($this->homeDirectory == ""){
912       $message[]= _("The required field 'Home directory' is not set.");
913     }
914     if (!is_path($this->homeDirectory)){
915       $message[]= _("Please enter a valid path in 'Home directory' field.");
916     }
918     /* Check ID's if they are forced by user */
919     if ($this->force_ids == "1"){
921       /* Valid uid/gid? */
922       if (!is_id($this->uidNumber)){
923         $message[]= _("Value specified as 'UID' is not valid.");
924       } else {
925         if ($this->uidNumber < $this->config->current['MINID']){
926           $message[]= _("Value specified as 'UID' is too small.");
927         }
928       }
929       if (!is_id($this->gidNumber)){
930         $message[]= _("Value specified as 'GID' is not valid.");
931       } else {
932         if ($this->gidNumber < $this->config->current['MINID']){
933           $message[]= _("Value specified as 'GID' is too small.");
934         }
935       }
936     }
938     /* Check shadow settings, well I like spaghetties... */
939     if ($this->use_shadowMin){
940       if (!is_id($this->shadowMin)){
941         $message[]= _("Value specified as 'shadowMin' is not valid.");
942       }
943     }
944     if ($this->use_shadowMax){
945       if (!is_id($this->shadowMax)){
946         $message[]= _("Value specified as 'shadowMax' is not valid.");
947       }
948     }
949     if ($this->use_shadowWarning){
950       if (!is_id($this->shadowWarning)){
951         $message[]= _("Value specified as 'shadowWarning' is not valid.");
952       }
953       if (!$this->use_shadowMax){
954         $message[]= _("'shadowWarning' without 'shadowMax' makes no sense.");
955       }
956       if ($this->shadowWarning > $this->shadowMax){
957         $message[]= _("Value specified as 'shadowWarning' should be smaller than 'shadowMax'.");
958       }
959       if ($this->use_shadowMin && $this->shadowWarning < $this->shadowMin){
960         $message[]= _("Value specified as 'shadowWarning' should be greater than 'shadowMin'.");
961       }
962     }
963     if ($this->use_shadowInactive){
964       if (!is_id($this->shadowInactive)){
965         $message[]= _("Value specified as 'shadowInactive' is not valid.");
966       }
967       if (!$this->use_shadowMax){
968         $message[]= _("'shadowInactive' without 'shadowMax' makes no sense.");
969       }
970     }
971     if ($this->use_shadowMin && $this->use_shadowMax){
972       if ($this->shadowMin > $this->shadowMax){
973         $message[]= _("Value specified as 'shadowMin' should be smaller than 'shadowMax'.");
974       }
975     }
977   //  if(empty($this->gosaDefaultPrinter)){
978   //    $message[]= _("You need to specify a valid default printer.");
979   //  }
981     return ($message);
982   }
984   function addGroup ($groups)
985   {
986     /* include global link_info */
987     $ldap= $this->config->get_ldap_link();
989     /* Walk through groups and add the descriptive entry if not exists */
990     foreach ($groups as $value){
991       if (!array_key_exists($value, $this->groupMembership)){
992         $ldap->cat($value, array('cn', 'description', 'dn'));
993         $attrs= $ldap->fetch();
994         error_reporting (0);
995         if (!isset($attrs['description'][0])){
996           $entry= $attrs["cn"][0];
997         } else {
998           $dsc= preg_replace ('/^Group of user/', _("Group of user"), $attrs["description"][0]);
999           $entry= $attrs["cn"][0]." [$dsc]";
1000         }
1001         error_reporting (E_ALL);
1002         $this->groupMembership[$attrs['dn']]= $entry;
1003       }
1004     }
1006     /* Sort groups */
1007     asort ($this->groupMembership);
1008     reset ($this->groupMembership);
1009   }
1012   /* Del posix user from some groups */
1013   function delGroup ($groups)
1014   {
1015     $dest= array();
1017     foreach ($this->groupMembership as $key => $value){
1018       if (!in_array($key, $groups)){
1019         $dest[$key]= $value;
1020       }
1021     }
1022     $this->groupMembership= $dest;
1023   }
1025   /* Adapt from template, using 'dn' */
1026   function adapt_from_template($dn)
1027   {
1028     /* Include global link_info */
1029     $ldap= $this->config->get_ldap_link();
1031     plugin::adapt_from_template($dn);
1032     $template= $this->attrs['uid'][0];
1034     /* Adapt group membership */
1035     $ldap->cd($this->config->current['BASE']);
1036     $ldap->search("(&(objectClass=posixGroup)(memberUid=".$this->attrs["uid"][0]."))", array("description", "cn"));
1038     while ($this->attrs= $ldap->fetch()){
1039       if (!isset($this->attrs["description"][0])){
1040         $entry= $this->attrs["cn"][0];
1041       } else {
1042         $entry= $this->attrs["cn"][0]." [".$this->attrs["description"][0]."]";
1043       }
1044       $this->groupMembership[$ldap->getDN()]= $entry;
1045     }
1047     /* Fix primary group settings */
1048     $ldap->cd($this->config->current['BASE']);
1049     $ldap->search("(&(objectClass=posixGroup)(cn=$template)(gidNumber=".$this->gidNumber."))", array("cn"));
1050     if ($ldap->count() != 1){
1051       $this->primaryGroup= $this->gidNumber;
1052     }
1054     $ldap->cd($this->config->current['BASE']);
1055     $ldap->search("(&(objectClass=gosaUserTemplate)(uid=".$template.")(accessTo=*))", array("cn","accessTo"));
1057     while($attr = $ldap->fetch()){
1058       $tmp = $attr['accessTo'];
1059       unset ($tmp['count']);
1060       $this->accessTo = $tmp;   
1061     }
1063     /* Adjust shadow checkboxes */
1064     foreach (array("shadowMin", "shadowMax", "shadowWarning", "shadowInactive",
1065           "shadowExpire") as $val){
1067       if ($this->$val != 0){
1068         $oval= "use_".$val;
1069         $this->$oval= "1";
1070       }
1071     }
1073     $this->shadowExpire= $this->convertToSeconds($this->shadowExpire);
1074   }
1076   function get_next_id($attrib)
1077   {
1078     $ids= array();
1079     $ldap= $this->config->get_ldap_link();
1081     $ldap->cd ($this->config->current['BASE']);
1082     if (preg_match('/gidNumber/i', $attrib)){
1083       $oc= "posixGroup";
1084     } else {
1085       $oc= "posixAccount";
1086     }
1087     $ldap->search ("(&(objectClass=$oc)($attrib=*))", array("$attrib"));
1089     /* Get list of ids */
1090     while ($attrs= $ldap->fetch()){
1091       $ids[]= (int)$attrs["$attrib"][0];
1092     }
1094     /* Find out next free id near to UID_BASE */
1095     for ($id= $this->config->current['UIDBASE']; $id++; $id<65000){
1096       if (!in_array($id, $ids)){
1097         return ($id);
1098       }
1099     }
1101     /* Should not happen */
1102     if ($id == 65000){
1103       print_red(_("Too many users, can't allocate a free ID!"));
1104       exit;
1105     }
1107   }
1109  function reload()
1110   {
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     }
1132     $res= get_list($filter, $this->ui->subtreeACL, $base,$attrs, $Flags);
1134     /* check sizelimit */
1135     if (preg_match("/size limit/i", $ldap->error)){
1136       $_SESSION['limit_exceeded']= TRUE;
1137     }
1139     /* Create a list of users */
1140     $this->grouplist = array();
1141     foreach ($res as $value){
1142         $this->grouplist[$value['gidNumber'][0]]= $value;
1143     }
1145     $tmp=array();
1146     foreach($this->grouplist as $tkey => $val ){
1147       $tmp[strtolower($val['cn'][0]).$val['cn'][0]]=$val;
1148     }
1150     /* Sort index */
1151     ksort($tmp);
1153     /* Recreate index array[dn]=cn[description]*/
1154     $this->grouplist=array();
1155     foreach($tmp as $val){
1156       if(isset($val['description'])){
1157         $this->grouplist[$val['dn']]=$val['cn'][0]."&nbsp;[".$val['description'][0]."]";
1158       }else{
1159         $this->grouplist[$val['dn']]=$val['cn'][0];
1160       }
1161     }
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     }
1183    
1184     $sta = "";
1185  
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 convertToSeconds($val)
1241   {
1242     if ($val != 0){
1243       $val*= 60 * 60 * 24;
1244     } else {
1245       $date= getdate();
1246       $val= floor($date[0] / (60*60*24)) * 60 * 60 * 24;
1247     }
1248   }
1252 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1253 ?>