Code

Closes #288 Made conferences language selectable
[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   /* Plugin specific values */
20   var $homeDirectory= "";
21   var $loginShell= "/bin/bash";
22   var $uidNumber= "";
23   var $gidNumber= "";
24   var $gecos= "";
25   var $shadowMin= "0";
26   var $shadowMax= "0";
27   var $shadowWarning= "0";
28   var $shadowLastChange= "0";
29   var $shadowInactive= "0";
30   var $shadowExpire= "0";
31   var $gosaDefaultPrinter= "";
32   var $accessTo= array();
33   var $trustModel= "";
35   var $glist=array();
36   var $status= "";
37   var $loginShellList= array();
38   var $groupMembership= array();
39   var $savedGroupMembership= array();
40   var $savedUidNumber= "";
41   var $savedGidNumber= "";
42   var $use_shadowMin= "0";
43   var $use_shadowMax= "0";
44   var $use_shadowWarning= "0";
45   var $use_shadowInactive= "0";
46   var $use_shadowExpire= "0";
47   var $must_change_password= "0";
48   var $force_ids= 0;
49   var $group_dialog= FALSE;
50   var $show_ws_dialog= FALSE;
51   var $secondaryGroups= array();
52   var $primaryGroup= 0;
53   var $was_trust_account= FALSE;
55   var $grouplist  = array();
56   var $ui         = array();
58   var $GroupRegex       = "*";
59   var $GroupUserRegex   = "*";
60   var $SubSearch        = false;
61   var $memberGroup      = array();
63   /* attribute list for save action */
64   var $CopyPasteVars  = array("grouplist","groupMembership","use_shadowMin","use_shadowMax","use_shadowWarning","use_shadowInactive","use_shadowExpire","must_change_password","grouplist","savedGidNumber","savedUidNumber");
65   var $attributes     = array("homeDirectory", "loginShell", "uidNumber", "gidNumber", "gecos",
66       "shadowMin", "shadowMax", "shadowWarning", "shadowInactive", "shadowLastChange",
67       "shadowExpire", "gosaDefaultPrinter", "uid","accessTo","trustModel");
68   var $objectclasses= array("posixAccount", "shadowAccount");
70   var $uid ="";
72   /* constructor, if 'dn' is set, the node loads the given
73      'dn' from LDAP */
74   function posixAccount ($config, $dn= NULL, $parent= NULL)
75   {
76     /* Configuration is fine, allways */
77     $this->config= $config;
79     /* Load bases attributes */
80     plugin::plugin($config, $dn, $parent);
82     /* set user id */    
83     if(isset($this->attrs['uid'])){
84       $this->uid = $this->attrs['uid'][0];
85     }
87     $ldap= $this->config->get_ldap_link();
89     if ($dn != NULL){
91       /* Correct is_account. shadowAccount is not required. */
92       if (isset($this->attrs['objectClass']) &&
93           in_array ('posixAccount', $this->attrs['objectClass'])){
95         $this->is_account= TRUE;
96       }
98       /* Is this account a trustAccount? */
99       if ($this->is_account && isset($this->attrs['trustModel'])){
100         $this->trustModel= $this->attrs['trustModel'][0];
101         $this->was_trust_account= TRUE;
102       } else {
103         $this->was_trust_account= FALSE;
104         $this->trustModel= "";
105       }
106          
107           $this->accessTo = array(); 
108       if ($this->is_account && isset($this->attrs['accessTo'])){
109         for ($i= 0; $i<$this->attrs['accessTo']['count']; $i++){
110           $tmp= $this->attrs['accessTo'][$i];
111           $this->accessTo[$tmp]= $tmp;
112         }
113       }
114       $this->initially_was_account= $this->is_account;
116       /* Fill group */
117       $this->primaryGroup= $this->gidNumber;
119       /* Generate status text */
120       $current= date("U");
122       $current= floor($current / 60 /60 / 24);
124       if (($current >= $this->shadowExpire) && $this->shadowExpire){
125         $this->status= _("expired");
126         if (($current - $this->shadowExpire) < $this->shadowInactive){
127           $this->status.= ", "._("grace time active");
128         }
129       } elseif (($this->shadowLastChange + $this->shadowMin) >= $current){
130         $this->status= _("active, password not changable");
131       } elseif (($this->shadowLastChange + $this->shadowMax) >= $current){
132         $this->status= _("active, password expired");
133       } else {
134         $this->status= _("active");
135       }
137       /* Get group membership */
138       $ldap->cd($this->config->current['BASE']);
139       $ldap->search("(&(objectClass=posixGroup)(memberUid=".$this->uid."))", array("cn", "description"));
141       while ($this->attrs= $ldap->fetch()){
142         if (!isset($this->attrs["description"][0])){
143           $entry= $this->attrs["cn"][0];
144         } else {
145           $dsc= preg_replace ('/^Group of user/', _("Group of user"), $this->attrs["description"][0]);
146           $entry= $this->attrs["cn"][0]." [$dsc]";
147         }
148         $this->groupMembership[$ldap->getDN()]= $entry;
149       }
150       asort($this->groupMembership);
151       reset($this->groupMembership);
152       $this->savedGroupMembership= $this->groupMembership;
153       $this->savedUidNumber= $this->uidNumber;
154       $this->savedGidNumber= $this->gidNumber;
155     }
157     /* Adjust shadow checkboxes */
158     foreach (array("shadowMin", "shadowMax", "shadowWarning", "shadowInactive",
159           "shadowExpire") as $val){
161       if ($this->$val != 0){
162         $oval= "use_".$val;
163         $this->$oval= "1";
164       }
165     }
167     /* Convert to seconds */
168     $this->shadowExpire= $this->convertToSeconds($this->shadowExpire);
170     /* Generate shell list from CONFIG_DIR./shells */
171     if (file_exists(CONFIG_DIR.'/shells')){
172       $shells = file (CONFIG_DIR.'/shells');
173       foreach ($shells as $line){
174         if (!preg_match ("/^#/", $line)){
175           $this->loginShellList[]= trim($line);
176         }
177       }
178     } else {
179       if ($this->loginShell == ""){
180         $this->loginShellList[]= _("unconfigured");
181       }
182     }
184     /* Insert possibly missing loginShell */
185     if ($this->loginShell != "" && !in_array($this->loginShell, $this->loginShellList)){
186       $this->loginShellList[]= $this->loginShell;
187     }
189     /* Set tag attribute if we've tagging activated */
190     $filter= "(objectClass=posixGroup)";
191     $ui= get_userinfo();
192     if ($ui->gosaUnitTag != "" && isset($config->current['STRICT_UNITS']) &&
193         preg_match('/TRUE/i', $config->current['STRICT_UNITS'])){
194       $filter= "(&(objectClass=posixGroup)(gosaUnitTag=".$ui->gosaUnitTag."))";
195     }
197     /* Generate group list */
198     $ldap->cd($this->config->current['BASE']);
199     $ldap->search("$filter", array("cn", "gidNumber"));
200     $this->secondaryGroups[]= "- "._("automatic")." -";
201     while ($attrs= $ldap->fetch()){
202       $this->secondaryGroups[$attrs['gidNumber'][0]]= $attrs['cn'][0];
203     }
204     asort ($this->secondaryGroups);
206     /* Get global filter config */
207     if (!is_global("sysfilter")){
208       $ui= get_userinfo();
209       $base= get_base_from_people($ui->dn);
210       $sysfilter= array( "depselect"       => $base,
211           "regex"           => "*");
212       register_global("sysfilter", $sysfilter);
213     }
214     $this->ui = get_userinfo();
215   }
218   /* execute generates the html output for this node */
219   function execute($isCopyPaste = false)
220   {
221         /* Call parent execute */
222         plugin::execute();
223   $display= "";
225   /* Department has changed? */
226   if(isset($_POST['depselect'])){
227     $_SESSION['CurrentMainBase']= validate($_POST['depselect']);
228   }
230   if(!$isCopyPaste){
232     $required = (isset($this->parent->by_object['sambaAccount']) && $this->parent->by_object['sambaAccount']->is_account) || 
233                 (isset($this->parent->by_object['environment'] ) && $this->parent->by_object['environment'] ->is_account);
235     /* Do we need to flip is_account state? */
236     if (isset($_POST['modify_state'])){
237       if($this->is_account && $this->acl == "#all#" && !$required ){
238         $this->is_account= !$this->is_account;
239       }elseif(!$this->is_account && chkacl($this->acl,"create") == ""){
240         $this->is_account= !$this->is_account;
241       }
242     }
244     /* Do we represent a valid posixAccount? */
245     if (!$this->is_account && $this->parent == NULL ){
246       $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\">&nbsp;<b>".
247         _("This account has no unix extensions.")."</b>";
248       $display.= back_to_main();
249       return ($display);
250     }
253     /* Show tab dialog headers */
254     if ($this->parent != NULL){
255       if ($this->is_account){
256         if (isset($this->parent->by_object['sambaAccount'])){
257           $obj= $this->parent->by_object['sambaAccount'];
258         }
259         if (isset($obj) && $obj->is_account == TRUE &&
260             ((isset($this->parent->by_object['sambaAccount']))&&($this->parent->by_object['sambaAccount']->is_account))
261             ||(isset($this->parent->by_object['environment'] ))&&($this->parent->by_object['environment'] ->is_account)){
263           /* Samba3 dependency on posix accounts are enabled
264              in the moment, because I need to rely on unique
265              uidNumbers. There'll be a better solution later
266              on. */
267           $display= $this->show_header(_("Remove posix account"),
268               _("This account has unix features enabled. To disable them, you'll need to remove the samba / environment account first."), TRUE);
269         } else {
270           $display= $this->show_header(_("Remove posix account"),
271               _("This account has posix features enabled. You can disable them by clicking below."));
272         }
273       } else {
274         $display= $this->show_header(_("Create posix account"),
275             _("This account has posix features disabled. You can enable them by clicking below."));
276         return($display);
277       }
278     }
279   }
280   /* Trigger group edit? */
281   if (isset($_POST['edit_groupmembership'])){
282     $this->group_dialog= TRUE;
283     $this->dialog= TRUE;
284   }
286   /* Cancel group edit? */
287   if (isset($_POST['add_groups_cancel']) ||
288       isset($_POST['add_groups_finish'])){
289     $this->group_dialog= FALSE;
290     $this->dialog= FALSE;
291   }
293   /* Add selected groups */
294   if (isset($_POST['add_groups_finish']) && isset($_POST['groups']) &&
295       count($_POST['groups'])){
297       if (chkacl ($this->acl, "memberUid") == ""){
298         $this->addGroup ($_POST['groups']);
299         $this->is_modified= TRUE;
300       }
301     }
303     /* Delete selected groups */
304     if (isset($_POST['delete_groupmembership']) && 
305         isset($_POST['group_list']) && count($_POST['group_list'])){
307       if (chkacl ($this->acl, "memberUid") == ""){
308         $this->delGroup ($_POST['group_list']);
309         $this->is_modified= TRUE;
310       }
311     }
313     /* Add user workstation? */
314     if (isset($_POST["add_ws"]) && chkacl($this->acl,"allowedHosts") == ""){
315       $this->show_ws_dialog= TRUE;
316       $this->dialog= TRUE;
317     }
319     /* Add user workstation? */
320     if (isset($_POST["add_ws_finish"]) && isset($_POST['wslist'])){
321       foreach($_POST['wslist'] as $ws){
322         $this->accessTo[$ws]= $ws;
323       }
324       ksort($this->accessTo);
325       $this->is_modified= TRUE;
326     }
328     /* Remove user workstations? */
329     if (isset($_POST["delete_ws"]) && isset($_POST['workstation_list']) && chkacl($this->acl,"allowedHosts") == ""){
330       foreach($_POST['workstation_list'] as $name){
331         unset ($this->accessTo[$name]);
332       }
333       $this->is_modified= TRUE;
334     }
336     /* Add user workstation finished? */
337     if (isset($_POST["add_ws_finish"]) || isset($_POST["add_ws_cancel"])){
338       $this->show_ws_dialog= FALSE;
339       $this->dialog= FALSE;
340     }
342     /* Templates now! */
343     $smarty= get_smarty();
345     /* Show ws dialog */
346     if ($this->show_ws_dialog){
347       /* Save data */
348       $sysfilter= get_global("sysfilter");
349       foreach( array("depselect", "regex") as $type){
350         if (isset($_POST[$type])){
351           $sysfilter[$type]= $_POST[$type];
352         }
353       }
354       if (isset($_GET['search'])){
355         $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
356         if ($s == "**"){
357           $s= "*";
358         }
359         $sysfilter['regex']= $s;
360       }
361       register_global("sysfilter", $sysfilter);
363       /* Get workstation list */
364       $exclude= "";
365       foreach($this->accessTo as $ws){
366         $exclude.= "(cn=$ws)";
367       }
368       if ($exclude != ""){
369         $exclude= "(!(|$exclude))";
370       }
371       $acl= array($this->config->current['BASE'] => ":all");
372       $regex= $sysfilter['regex'];
373       $filter= "(&(|(objectClass=goServer)(objectClass=gotoWorkstation)(objectClass=gotoTerminal))$exclude(cn=*)(cn=$regex))";
374       $res= get_list($filter, $acl, $sysfilter['depselect'], array("cn"), GL_SUBSEARCH | GL_SIZELIMIT);
375       $wslist= array();
376       foreach ($res as $attrs){
377         $wslist[]= preg_replace('/\$/', '', $attrs['cn'][0]);
378       }
379       asort($wslist);
380       $smarty->assign("search_image", get_template_path('images/search.png'));
381       $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
382       $smarty->assign("tree_image", get_template_path('images/tree.png'));
383       $smarty->assign("deplist", $this->config->idepartments);
384       $smarty->assign("alphabet", generate_alphabet());
385       foreach( array("depselect", "regex") as $type){
386         $smarty->assign("$type", $sysfilter[$type]);
387       }
388       $smarty->assign("hint", print_sizelimit_warning());
389       $smarty->assign("wslist", $wslist);
390       $smarty->assign("apply", apply_filter());
391       $display= $smarty->fetch (get_template_path('trust_machines.tpl', TRUE, dirname(__FILE__)));
392       return ($display);
393     }
395     /* Manage group add dialog */
396     if ($this->group_dialog){
398       /* Get global filter config */
399       $this->reload();
401       /* remove already assigned groups */
402       $glist= array();
403       foreach ($this->grouplist as $key => $value){
404         if (!isset($this->groupMembership[$key])){
405           $glist[$key]= $value;
406         }
407       }
409       if($this->SubSearch){
410         $smarty->assign("SubSearchCHK"," checked ");
411       }else{
412         $smarty->assign("SubSearchCHK","");
413       }
415       $smarty->assign("regex",$this->GroupRegex);
416       $smarty->assign("guser",$this->GroupUserRegex);
417       $smarty->assign("groups", $glist);
418       $smarty->assign("search_image", get_template_path('images/search.png'));
419       $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
420       $smarty->assign("tree_image", get_template_path('images/tree.png'));
421       $smarty->assign("deplist", $this->config->idepartments);
422       $smarty->assign("alphabet", generate_alphabet());
423       $smarty->assign("depselect",$_SESSION['CurrentMainBase']);
424       $smarty->assign("hint", print_sizelimit_warning());
426       $smarty->assign("apply", apply_filter());
427       $display.= $smarty->fetch (get_template_path('posix_groups.tpl', TRUE, dirname(__FILE__)));
428       return ($display);
429     }
431     /* Show main page */
432     $smarty= get_smarty();
434     /* Depending on pwmode, currently hardcoded because there are no other methods */
435     if ( 1 == 1 ){
436       $smarty->assign("pwmode", dirname(__FILE__)."/posix_shadow");
437       $shadowMinACL= chkacl($this->acl, "shadowMin");
438       $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."\">"));
439       $shadowMaxACL= chkacl($this->acl, "shadowMax");
440       $smarty->assign("shadowmaxs", sprintf(_("Password must be changed after %s days"), "<input name=\"shadowMax\" size=3 maxlength=4 $shadowMaxACL value=\"".$this->shadowMax."\">"));
441       $shadowInactiveACL= chkacl($this->acl, "shadowInactive");
442       $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."\">"));
443       $shadowWarningACL= chkacl($this->acl, "shadowWarning");
444       $smarty->assign("shadowwarnings", sprintf(_("Warn user %s days before password expiery"), "<input name=\"shadowWarning\" size=3 maxlength=4 $shadowWarningACL value=\"".$this->shadowWarning."\">"));
445       foreach( array("must_change_password", "use_shadowMin", "use_shadowMax",
446             "use_shadowExpire", "use_shadowInactive",
447             "use_shadowWarning") as $val){
448         if ($this->$val == 1){
449           $smarty->assign("$val", "checked");
450         } else {
451           $smarty->assign("$val", "");
452         }
453         $smarty->assign("$val"."ACL", chkacl($this->acl, $val));
454       }
455     }
457     /* Fill calendar */
458    
459     /* If this $this->shadowExpire is empty 
460         use current date as base for calculating selectbox values.
461        (This attribute is empty if this is a new user )*/ 
462     if(empty($this->shadowExpire)){
463       $date= getdate(time());
464     }else{
465       $date= getdate($this->shadowExpire);
466     }
468     $days= array();
469     for($d= 1; $d<32; $d++){
470       $days[$d]= $d;
471     }
472     $years= array();
473     for($y= $date['year']-10; $y<$date['year']+10; $y++){
474       $years[]= $y;
475     }
476     $months= array(_("January"), _("February"), _("March"), _("April"),
477         _("May"), _("June"), _("July"), _("August"), _("September"),
478         _("October"), _("November"), _("December"));
479     $smarty->assign("day", $date["mday"]);
480     $smarty->assign("days", $days);
481     $smarty->assign("months", $months);
482     $smarty->assign("month", $date["mon"]-1);
483     $smarty->assign("years", $years);
484     $smarty->assign("year", $date["year"]);
486     /* Fill arrays */
487     $smarty->assign("shells", $this->loginShellList);
488     $smarty->assign("secondaryGroups", $this->secondaryGroups);
489     $smarty->assign("primaryGroup", $this->primaryGroup);
490    if (!count($this->groupMembership)){
491       $smarty->assign("groupMembership", array("&nbsp;"));
492     } else {
493       $smarty->assign("groupMembership", $this->groupMembership);
494     }
495     if (count($this->groupMembership) > 16){
496       $smarty->assign("groups", "too_many_for_nfs");
497     } else {
498       $smarty->assign("groups", "");
499     }
501         /* Avoid "Undefined index: forceMode" */
502     $smarty->assign("forceMode", "");
504     /* Checkboxes */
505     if ($this->force_ids == 1){
506       $smarty->assign("force_ids", "checked");
507       if ($_SESSION['js']){
508         $smarty->assign("forceMode", "");
509       }
510     } else {
511       if ($_SESSION['js']){
512                 if($this->acl != "#none#")
513         $smarty->assign("forceMode", "disabled");
514       }
515       $smarty->assign("force_ids", "");
516     }
517     $smarty->assign("force_idsACL", chkacl($this->acl, "force_ids"));
519     /* Load attributes and acl's */
520     foreach($this->attributes as $val){
521       if((chkacl($this->acl,$val)=="")&&(($_SESSION["js"])&&(($val=="uidNumber")||($val=="gidNumber"))))
522         {
523           $smarty->assign("$val"."ACL","");
524           $smarty->assign("$val", $this->$val);
525           continue;
526         }
527       $smarty->assign("$val", $this->$val);
528       $smarty->assign("$val"."ACL", chkacl($this->acl,$val));
529     }
530     $smarty->assign("groupMembershipACL", chkacl($this->acl, "groupMembership"));
531     $smarty->assign("status", $this->status);
533     /* Work on trust modes */
534     $smarty->assign("trustmodeACL", chkacl($this->acl, "trustmode"));
535     if ($this->trustModel == "fullaccess"){
536       $trustmode= 1;
537       // pervent double disable tag in html code, this will disturb our clean w3c html
538     
539     if(chkacl($this->acl, "trustmode")==""){
540           $smarty->assign("trusthide", "disabled");
541       }else{
542           $smarty->assign("trusthide", "");
543       }
545     } elseif ($this->trustModel == "byhost"){
546       $trustmode= 2;
547       $smarty->assign("trusthide", "");
548     } else {
549       // pervent double disable tag in html code, this will disturb our clean w3c html
550       if(chkacl($this->acl, "trustmode")==""){
551           $smarty->assign("trusthide", "disabled");
552       }else{
553           $smarty->assign("trusthide", "");
554       }
555       $trustmode= 0;
556     }
557     $smarty->assign("trustmode", $trustmode);
558     $smarty->assign("trustmodes", array( 0 => _("disabled"), 1 => _("full access"),
559           2 => _("allow access to these hosts")));
563     if((count($this->accessTo))==0)
564       $smarty->assign("emptyArrAccess",true);
565     else
566       $smarty->assign("emptyArrAccess",false);
567     
570     $smarty->assign("workstations", $this->accessTo);
572     $smarty->assign("apply", apply_filter());
573     $display.= $smarty->fetch (get_template_path('generic.tpl', TRUE, dirname(__FILE__)));
574     return($display);
575   }
578   /* remove object from parent */
579   function remove_from_parent()
580   {
581     /* Cancel if there's nothing to do here */
582     if (!$this->initially_was_account){
583       return;
584     }
586     /* include global link_info */
587     $ldap= $this->config->get_ldap_link();
588     
589         /* Remove and write to LDAP */
590     plugin::remove_from_parent();
592     /* Zero out array */
593     $this->attrs['gosaHostACL']= array();
595     /* Keep uid, because we need it for authentification! */
596     unset($this->attrs['uid']);
597     unset($this->attrs['trustModel']);
599     @DEBUG (DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__,
600         $this->attributes, "Save");
601     $ldap->cd($this->dn);
602     $this->cleanup();
603     $ldap->modify ($this->attrs); 
605     show_ldap_error($ldap->get_error(), _("Removing UNIX account failed"));
607     /* Delete group only if cn is uid and there are no other
608        members inside */
609     $ldap->cd ($this->config->current['BASE']);
610     $ldap->search ("(&(objectClass=posixGroup)(gidNumber=".$this->gidNumber."))", array("cn", "memberUid"));
611     if ($ldap->count() != 0){
612       $attrs= $ldap->fetch();
613       if ($attrs['cn'][0] == $this->uid &&
614           !isset($this->attrs['memberUid'])){
616         $ldap->rmDir($ldap->getDN());
617       }
618     }
620     /* Optionally execute a command after we're done */
621     $this->handle_post_events("remove", array("uid" => $this->uid));
622   }
625   function save_object()
626   {
627     if (isset($_POST['posixTab'])){
628       /* Save values to object */
629       plugin::save_object();
631       /* Save force GID attribute */
632       if (chkacl ($this->acl, "force_ids") == ""){
633         if (isset ($_POST['force_ids'])){
634           $data= 1;
635         } else {
636           $data= 0;
637         }
638         if ($this->force_ids != $data){
639           $this->is_modified= TRUE;
640         }
641         $this->force_ids= $data;
643         $data= $_POST['primaryGroup'];
644         if ($this->primaryGroup != $data){
645           $this->is_modified= TRUE;
646         }
647         $this->primaryGroup= $_POST['primaryGroup'];
648         if ($this->primaryGroup != 0){
649           $this->gidNumber= $this->primaryGroup;
650         }
651       }
653       /* Save pwmode dependent attributes, curently hardcoded because there're
654          no alternatives */
655       if (1 == 1){
656         foreach( array("must_change_password", "use_shadowMin", "use_shadowMax",
657               "use_shadowExpire", "use_shadowInactive",
658               "use_shadowWarning") as $val){
659           if (chkacl($this->acl, "$val") == ""){
660             if (isset ($_POST[$val])){
661               $data= 1;
662             } else {
663               $data= 0;
664             }
665             if ($data != $this->$val){
666               $this->is_modified= TRUE;
667             }
668             $this->$val= $data;
669           }
670         }
671       }
673       /* Trust mode - special handling */
674       if (isset($_POST['trustmode']) && chkacl($this->acl,"allowedHosts") == ""){
675         $saved= $this->trustModel;
676         if ($_POST['trustmode'] == "1"){
677           $this->trustModel= "fullaccess";
678         } elseif ($_POST['trustmode'] == "2"){
679           $this->trustModel= "byhost";
680         } else {
681           $this->trustModel= "";
682         }
683         if ($this->trustModel != $saved){
684           $this->is_modified= TRUE;
685         }
686       }
687     }
689     /* Get regex from alphabet */
690     if(isset($_GET['search'])){
691       $this->GroupRegex = $_GET['search']."*";
692     }
694     /* Check checkboxes and regexes */
695     if(isset($_POST["PosixGroupDialogPosted"])){
696       if(isset($_POST['SubSearch']) && ($_POST['SubSearch'])){
697         $this->SubSearch = true;
698       }else{
699         $this->SubSearch = false;
700       }
701     
702       if(isset($_POST['guser'])){
703         $this->GroupUserRegex = $_POST['guser'];
704       }
705       if(isset($_POST['regex'])){
706         $this->GroupRegex = $_POST['regex'];
707       }
708     }
709     $this->GroupRegex = preg_replace("/\*\**/","*",$this->GroupRegex);
710     $this->GroupUserRegex = preg_replace("/\*\**/","*",$this->GroupUserRegex);
711   }
714   /* Save data to LDAP, depending on is_account we save or delete */
715   function save()
716   {
717         
718     /* include global link_info */
719     $ldap= $this->config->get_ldap_link();
721     /* Adapt shadow values */
722     if (!$this->use_shadowExpire){
723       $this->shadowExpire= "0";
724     } else {
725       /* Transform seconds to days here */
726       $this->shadowExpire= (int)($this->shadowExpire / (60 * 60 * 24)) ;
727     }
728     if (!$this->use_shadowMax){
729       $this->shadowMax= "0";
730     }
731     if ($this->must_change_password){
732       $this->shadowLastChange= (int)(date("U") / 86400) - $this->shadowMax - 1;
733     } else {
734       $this->shadowLastChange= (int)(date("U") / 86400);
735     }
736     if (!$this->use_shadowWarning){
737       $this->shadowWarning= "0";
738     }
740     /* Check what to do with ID's */
741     if ($this->force_ids == 0){
743       /* Use id's that are already set */
744       if ($this->savedUidNumber != ""){
745         $this->uidNumber= $this->savedUidNumber;
746         $this->gidNumber= $this->savedGidNumber;
747       } else {
749         /* Calculate new id's. We need to place a lock before calling get_next_id
750            to get real unique values. */
751         $wait= 10;
752         while (get_lock("uidnumber") != ""){
753           sleep (1);
755           /* Oups - timed out */
756           if ($wait-- == 0){
757             print_red (_("Failed: overriding lock"));
758             break;
759           }
760         }
762         add_lock ("uidnumber", "gosa");
763         $this->uidNumber= $this->get_next_id("uidNumber", $this->dn);
764         if ($this->savedGidNumber != ""){
765           $this->gidNumber= $this->savedGidNumber;
766         } else {
767           $this->gidNumber= $this->get_next_id("gidNumber", $this->dn);
768         }
769       }
771       if ($this->primaryGroup != 0){
772         $this->gidNumber= $this->primaryGroup;
773       }
774     }
776     if ($this->use_shadowMin != "1" ) {
777       $this->shadowMin = "";
778     }
780     if (($this->use_shadowMax != "1") && ($this->must_change_password != "1")) {
781       $this->shadowMax = "";
782     }
784     if ($this->use_shadowWarning != "1" ) {
785       $this->shadowWarning = "";
786     }
788     if ($this->use_shadowInactive != "1" ) {
789       $this->shadowInactive = "";
790     }
792     if ($this->use_shadowExpire != "1" ) {
793       $this->shadowExpire = "";
794     }
796     /* Fill gecos */
797     if (isset($this->parent) && $this->parent != NULL){
798       $this->gecos= rewrite($this->parent->by_object['user']->cn);
799       if (!preg_match('/^[a-z0-9 -]+$/i', $this->gecos)){
800         $this->gecos= "";
801       }
802     }
804         foreach(array("shadowMin","shadowMax","shadowWarning","shadowInactive","shadowExpire") as $attr){
805                 $this->$attr = (int) $this->$attr;
806         }
807     /* Call parents save to prepare $this->attrs */
808     plugin::save();
810     /* Trust accounts */
811     $objectclasses= array();
812     foreach ($this->attrs['objectClass'] as $key => $class){
813       if (preg_match('/trustAccount/i', $class)){
814         continue;
815       }
816       $objectclasses[]= $this->attrs['objectClass'][$key];
817     }
818     $this->attrs['objectClass']= $objectclasses;
819     if ($this->trustModel != ""){
820       $this->attrs['objectClass'][]= "trustAccount";
821       $this->attrs['trustModel']= $this->trustModel;
822       $this->attrs['accessTo']= array();
823       if ($this->trustModel == "byhost"){
824         foreach ($this->accessTo as $host){
825           $this->attrs['accessTo'][]= $host;
826         }
827       }
828     } else {
829       if ($this->was_trust_account){
830         $this->attrs['accessTo']= array();
831         $this->attrs['trustModel']= array();
832       }
833     }
835     if(empty($this->attrs['gosaDefaultPrinter'])){
836       $thid->attrs['gosaDefaultPrinter']=array();
837     }
840     /* Save data to LDAP */
841     $ldap->cd($this->dn);
842     $this->cleanup();
843     unset($this->attrs['uid']);
844     $ldap->modify ($this->attrs); 
846     show_ldap_error($ldap->get_error(), _("Saving UNIX account failed"));
848     /* Remove lock needed for unique id generation */
849     del_lock ("uidnumber");
852     /* Posix accounts have group interrelationship, 
853         take care about these here if this is a new user without forced gidNumber. */
854     if ($this->force_ids == 0 && $this->primaryGroup == 0 && !$this->initially_was_account){
855       $ldap->cd($this->config->current['BASE']);
856       $ldap->search("(&(objectClass=posixGroup)(gidNumber=".$this->gidNumber."))", array("cn"));
858       /* Create group if it doesn't exist */
859       if ($ldap->count() == 0){
860         $groupdn= preg_replace ('/^'.$this->config->current['DNMODE'].'=[^,]+,'.get_people_ou().'/i', 'cn='.$this->uid.','.get_groups_ou(), $this->dn);
861         $g= new group($this->config, $groupdn);
862         $g->cn= $this->uid;
863         $g->force_gid= 1;
864         $g->gidNumber= $this->gidNumber;
865         $g->description= "Group of user ".$this->givenName." ".$this->sn;
866         $g->save ();
867       }
868     }
869  
870     /* Take care about groupMembership values: add to groups */
871     foreach ($this->groupMembership as $key => $value){
872       if (!isset($this->savedGroupMembership[$key])){
873         $g= new grouptabs($this->config,$this->config->data['TABS']['GROUPTABS'], $key, false);
874         $g->by_object['group']->addUser($this->uid);
875         $g->save();
876       }
877     }
878     
879     /* Remove from groups not listed in groupMembership */
880     foreach ($this->savedGroupMembership as $key => $value){
881       if (!isset($this->groupMembership[$key])){
882         $g= new grouptabs($this->config,$this->config->data['TABS']['GROUPTABS'], $key);
883         $g->by_object['group']->removeUser ($this->uid);
884         $g->save();
885       }
886     }
888     /* Optionally execute a command after we're done */
889     if ($this->initially_was_account == $this->is_account){
890       if ($this->is_modified){
891         $this->handle_post_events("modify",array("uid" => $this->uid));
892       }
893     } else {
894       $this->handle_post_events("add",array("uid" , $this->uid));
895     }
896   }
898   /* Check formular input */
899   function check()
900   {
901     /* Include global link_info */
902     $ldap= $this->config->get_ldap_link();
904     /* Append groups as memberGroup: to check hook 
905      */
906     $tmp_attributes  = $this->attributes;    
907     $this->attributes[] = "memberGroup";
908     $this->memberGroup = array();
909     foreach($this->groupMembership as $dn => $name){
910       $this->memberGroup[] = $name;
911     }
913     /* Call common method to give check the hook */
914     $message= plugin::check();
915     $this->attributes = $tmp_attributes;
917     /* must: homeDirectory */
918     if ($this->homeDirectory == ""){
919       $message[]= _("The required field 'Home directory' is not set.");
920     }
921     if (!is_path($this->homeDirectory)){
922       $message[]= _("Please enter a valid path in 'Home directory' field.");
923     }
925     /* Check ID's if they are forced by user */
926     if ($this->force_ids == "1"){
928       /* Valid uid/gid? */
929       if (!is_id($this->uidNumber)){
930         $message[]= _("Value specified as 'UID' is not valid.");
931       } else {
932         if ($this->uidNumber < $this->config->current['MINID']){
933           $message[]= _("Value specified as 'UID' is too small.");
934         }
935       }
936       if (!is_id($this->gidNumber)){
937         $message[]= _("Value specified as 'GID' is not valid.");
938       } else {
939         if ($this->gidNumber < $this->config->current['MINID']){
940           $message[]= _("Value specified as 'GID' is too small.");
941         }
942       }
943     }
945     /* Check shadow settings, well I like spaghetties... */
946     if ($this->use_shadowMin){
947       if (!is_id($this->shadowMin)){
948         $message[]= _("Value specified as 'shadowMin' is not valid.");
949       }
950     }
951     if ($this->use_shadowMax){
952       if (!is_id($this->shadowMax)){
953         $message[]= _("Value specified as 'shadowMax' is not valid.");
954       }
955     }
956     if ($this->use_shadowWarning){
957       if (!is_id($this->shadowWarning)){
958         $message[]= _("Value specified as 'shadowWarning' is not valid.");
959       }
960       if (!$this->use_shadowMax){
961         $message[]= _("'shadowWarning' without 'shadowMax' makes no sense.");
962       }
963       if ($this->shadowWarning > $this->shadowMax){
964         $message[]= _("Value specified as 'shadowWarning' should be smaller than 'shadowMax'.");
965       }
966       if ($this->use_shadowMin && $this->shadowWarning < $this->shadowMin){
967         $message[]= _("Value specified as 'shadowWarning' should be greater than 'shadowMin'.");
968       }
969     }
970     if ($this->use_shadowInactive){
971       if (!is_id($this->shadowInactive)){
972         $message[]= _("Value specified as 'shadowInactive' is not valid.");
973       }
974       if (!$this->use_shadowMax){
975         $message[]= _("'shadowInactive' without 'shadowMax' makes no sense.");
976       }
977     }
978     if ($this->use_shadowMin && $this->use_shadowMax){
979       if ($this->shadowMin > $this->shadowMax){
980         $message[]= _("Value specified as 'shadowMin' should be smaller than 'shadowMax'.");
981       }
982     }
984   //  if(empty($this->gosaDefaultPrinter)){
985   //    $message[]= _("You need to specify a valid default printer.");
986   //  }
988     return ($message);
989   }
991   function addGroup ($groups)
992   {
993     /* include global link_info */
994     $ldap= $this->config->get_ldap_link();
996     /* Walk through groups and add the descriptive entry if not exists */
997     foreach ($groups as $value){
998       if (!array_key_exists($value, $this->groupMembership)){
999         $ldap->cat($value, array('cn', 'description', 'dn'));
1000         $attrs= $ldap->fetch();
1001         error_reporting (0);
1002         if (!isset($attrs['description'][0])){
1003           $entry= $attrs["cn"][0];
1004         } else {
1005           $dsc= preg_replace ('/^Group of user/', _("Group of user"), $attrs["description"][0]);
1006           $entry= $attrs["cn"][0]." [$dsc]";
1007         }
1008         error_reporting (E_ALL);
1009         $this->groupMembership[$attrs['dn']]= $entry;
1010       }
1011     }
1013     /* Sort groups */
1014     asort ($this->groupMembership);
1015     reset ($this->groupMembership);
1016   }
1019   /* Del posix user from some groups */
1020   function delGroup ($groups)
1021   {
1022     $dest= array();
1024     foreach ($this->groupMembership as $key => $value){
1025       if (!in_array($key, $groups)){
1026         $dest[$key]= $value;
1027       }
1028     }
1029     $this->groupMembership= $dest;
1030   }
1032   /* Adapt from template, using 'dn' */
1033   function adapt_from_template($dn)
1034   {
1035     /* Include global link_info */
1036     $ldap= $this->config->get_ldap_link();
1038     plugin::adapt_from_template($dn);
1039     $template= $this->attrs['uid'][0];
1041     /* Adapt group membership */
1042     $ldap->cd($this->config->current['BASE']);
1043     $ldap->search("(&(objectClass=posixGroup)(memberUid=".$this->attrs["uid"][0]."))", array("description", "cn"));
1045     while ($this->attrs= $ldap->fetch()){
1046       if (!isset($this->attrs["description"][0])){
1047         $entry= $this->attrs["cn"][0];
1048       } else {
1049         $entry= $this->attrs["cn"][0]." [".$this->attrs["description"][0]."]";
1050       }
1051       $this->groupMembership[$ldap->getDN()]= $entry;
1052     }
1054     /* Fix primary group settings */
1055     $ldap->cd($this->config->current['BASE']);
1056     $ldap->search("(&(objectClass=posixGroup)(cn=$template)(gidNumber=".$this->gidNumber."))", array("cn"));
1057     if ($ldap->count() != 1){
1058       $this->primaryGroup= $this->gidNumber;
1059     }
1061     $ldap->cd($this->config->current['BASE']);
1062     $ldap->search("(&(objectClass=gosaUserTemplate)(uid=".$template.")(accessTo=*))", array("cn","accessTo"));
1064     while($attr = $ldap->fetch()){
1065       $tmp = $attr['accessTo'];
1066       unset ($tmp['count']);
1067       $this->accessTo = $tmp;   
1068     }
1070     /* Adjust shadow checkboxes */
1071     foreach (array("shadowMin", "shadowMax", "shadowWarning", "shadowInactive") as $val){
1072       if ($this->$val != 0){
1073         $oval= "use_".$val;
1074         $this->$oval= "1";
1075       }
1076     }
1078     /* 
1079      * If shadowExpire is not enabled in the template, it's a UNIX timestamp - so don't convert it to seconds.
1080      * The check is a hack - if difference between timestamp generated above and here is max 1 day.
1081      */
1082     if(abs($this->shadowExpire - time())>86400) {
1083       $this->shadowExpire= $this->convertToSeconds($this->shadowExpire);
1084     }
1085     
1086     /* Only enable checkbox, if shadowExpire is in the future */
1087     if($this->shadowExpire > time()) {
1088       $this->use_shadowExpire= "1";
1089     }
1090   }
1092   function get_next_id($attrib, $dn)
1093   {
1094     $ids= array();
1095     $ldap= $this->config->get_ldap_link();
1097     $ldap->cd ($this->config->current['BASE']);
1098     if (preg_match('/gidNumber/i', $attrib)){
1099       $oc= "posixGroup";
1100     } else {
1101       $oc= "posixAccount";
1102     }
1103     $ldap->search ("(&(objectClass=$oc)($attrib=*))", array("$attrib"));
1105     /* Get list of ids */
1106     while ($attrs= $ldap->fetch()){
1107       $ids[]= (int)$attrs["$attrib"][0];
1108     }
1110     /* Add the nobody id */
1111     $ids[]= 65534;
1113     /* Find out next free id near to UID_BASE */
1114     if (!isset($this->config->current['BASE_HOOK'])){
1115       $base= $this->config->current['UIDBASE'];
1116     } else {
1117       /* Call base hook */
1118       $base= get_base_from_hook($dn, $attrib);
1119     }
1120     for ($id= $base; $id++; $id < pow(2,32)){
1121       if (!in_array($id, $ids)){
1122         return ($id);
1123       }
1124     }
1126     /* Check if current id reaches the maximum of 32 bit */
1127     if ($id >= pow(2,32)){
1128       echo _("Too many users, can't allocate a free ID!");
1129       exit;
1130     }
1131   }
1133  function reload()
1134   {
1135     /* Set base for all searches */
1136     $base     = $_SESSION['CurrentMainBase'];
1137     $ldap     = $this->config->get_ldap_link();    
1138     $attrs    =  array("cn", "description", "gidNumber");
1139     $Flags    = GL_SIZELIMIT;
1141     /* Get groups */
1142     if ($this->GroupUserRegex == '*'){
1143       $filter = "(&(objectClass=posixGroup)(cn=".$this->GroupRegex."))";
1144     } else {
1145       $filter= "(&(objectClass=posixGroup)(cn=".$this->GroupRegex.")(memberUid=".$this->GroupUserRegex."))";
1146     }
1147     if($this->SubSearch){
1148       $Flags |= GL_SUBSEARCH;
1149     }else{
1150       $base = get_groups_ou().$base;
1151     }
1154     $res= get_list($filter, $this->ui->subtreeACL, $base,$attrs, $Flags);
1156     /* check sizelimit */
1157     if (preg_match("/size limit/i", $ldap->error)){
1158       $_SESSION['limit_exceeded']= TRUE;
1159     }
1161     /* Create a list of users */
1162     $this->grouplist = array();
1163     foreach ($res as $value){
1164         $this->grouplist[$value['gidNumber'][0]]= $value;
1165     }
1167     $tmp=array();
1168     foreach($this->grouplist as $tkey => $val ){
1169       $tmp[strtolower($val['cn'][0]).$val['cn'][0]]=$val;
1170     }
1172     /* Sort index */
1173     ksort($tmp);
1175     /* Recreate index array[dn]=cn[description]*/
1176     $this->grouplist=array();
1177     foreach($tmp as $val){
1178       if(isset($val['description'])){
1179         $this->grouplist[$val['dn']]=$val['cn'][0]."&nbsp;[".$val['description'][0]."]";
1180       }else{
1181         $this->grouplist[$val['dn']]=$val['cn'][0];
1182       }
1183     }
1184     reset ($this->grouplist);
1185   }
1187  
1188   /* Get posts from copy & paste dialog */ 
1189   function saveCopyDialog()
1190   {
1191     if(isset($_POST['homeDirectory'])){
1192       $this->homeDirectory = $_POST['homeDirectory'];
1193       if (isset ($_POST['force_ids'])){
1194         $data= 1;
1195         $this->gidNumber = $_POST['gidNumber'];
1196         $this->uidNumber = $_POST['uidNumber'];
1197       } else {
1198         $data= 0;
1199       }
1200       if ($this->force_ids != $data){
1201         $this->is_modified= TRUE;
1202       }
1203       $this->force_ids= $data;
1204     }
1205   }
1208   /* Create the posix dialog part for copy & paste */
1209   function getCopyDialog()
1210   {
1211     /* Skip dialog creation if this is not a valid account */
1212     if(!$this->is_account) return("");
1213     if ($this->force_ids == 1){
1214       $force_ids = "checked";
1215       if ($_SESSION['js']){
1216         $forceMode = "";
1217       }
1218     } else {
1219       if ($_SESSION['js']){
1220         if($this->acl != "#none#")
1221           $forceMode ="disabled";
1222       }
1223       $force_ids = "";
1224     }
1225    
1226     $sta = "";
1227  
1228     /* Open group add dialog */
1229     if(isset($_POST['edit_groupmembership'])){
1230       $this->group_dialog = TRUE;
1231       $sta = "SubDialog";
1232     }
1234     /* If the group-add dialog is closed, call execute 
1235         to ensure that the membership is updatd */
1236     if(isset($_POST['add_groups_finish']) || isset($_POST['add_groups_cancel'])){
1237       $this->execute();
1238       $this->group_dialog =FALSE;
1239     }
1241     if($this->group_dialog){
1242       $str = $this->execute(true);
1243       $ret = array();
1244       $ret['string'] = $str;
1245       $ret['status'] = $sta;
1246       return($ret);
1247     }
1249     /* If a group member should be deleted, simply call execute */
1250     if(isset($_POST['delete_groupmembership'])){
1251       $this->execute();
1252     }
1254     /* Assigned informations to smarty */
1255     $smarty = get_smarty();
1256     $smarty->assign("homeDirectory",$this->homeDirectory);
1257     $smarty->assign("uidNumber",$this->uidNumber);
1258     $smarty->assign("gidNumber",$this->gidNumber);
1259     $smarty->assign("forceMode",$forceMode);
1260     $smarty->assign("force_ids",$force_ids);
1261     if (!count($this->groupMembership)){
1262       $smarty->assign("groupMembership", array("&nbsp;"));
1263     } else {
1264       $smarty->assign("groupMembership", $this->groupMembership);
1265     }
1267     /* Display wars message if there are more than 16 group members */
1268     if (count($this->groupMembership) > 16){
1269       $smarty->assign("groups", "too_many_for_nfs");
1270     } else {
1271       $smarty->assign("groups", "");
1272     }
1273     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE,dirname(__FILE__)));
1275     $ret = array();
1276     $ret['string'] = $str;
1277     $ret['status'] = $sta;
1278     return($ret);
1279   }
1281   function PrepareForCopyPaste($source)
1282   {
1283     plugin::PrepareForCopyPaste($source);
1284     
1285     /* Avoid using the same gid/uid number as source user */
1286     $this->savedUidNumber = $this->get_next_id("gidNumber", $this->dn);
1287     $this->savedGidNumber = $this->get_next_id("uidNumber", $this->dn);
1288   }
1290   function convertToSeconds($val)
1291   {
1292     if ($val != 0){
1293       $val*= 60 * 60 * 24;
1294     } else {
1295       $date= getdate();
1296       $val= floor($date[0] / (60*60*24)) * 60 * 60 * 24;
1297     }
1298     return($val);
1299   }
1303 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1304 ?>