Code

Updated plinfo
[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");
77   /* constructor, if 'dn' is set, the node loads the given
78      'dn' from LDAP */
79   function posixAccount ($config, $dn= NULL)
80   {
81     /* Configuration is fine, allways */
82     $this->config= $config;
84     /* Load bases attributes */
85     plugin::plugin($config, $dn);
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     if ($this->shadowExpire != 0){
169       $this->shadowExpire*= 60 * 60 * 24;
170     } else {
171       $date= getdate();
172       $this->shadowExpire= floor($date[0] / (60*60*24)) * 60 * 60 * 24;
173     }
175     /* Generate shell list from /etc/gosa/shells */
176     if (file_exists('/etc/gosa/shells')){
177       $shells = file ('/etc/gosa/shells');
178       foreach ($shells as $line){
179         if (!preg_match ("/^#/", $line)){
180           $this->loginShellList[]= trim($line);
181         }
182       }
183     } else {
184       if ($this->loginShell == ""){
185         $this->loginShellList[]= _("unconfigured");
186       }
187     }
189     /* Insert possibly missing loginShell */
190     if ($this->loginShell != "" && !in_array($this->loginShell, $this->loginShellList)){
191       $this->loginShellList[]= $this->loginShell;
192     }
194     /* Generate printer list */
195     if (isset($this->config->data['SERVERS']['CUPS'])){
196       $this->printerList= get_printer_list ($this->config->data['SERVERS']['CUPS']);
197       asort($this->printerList);
198     }
200     /* Generate group list */
201     $ldap->cd($this->config->current['BASE']);
202     $ldap->search("(objectClass=posixGroup)", array("cn", "gidNumber"));
203     $this->secondaryGroups[]= "- "._("automatic")." -";
204     while ($attrs= $ldap->fetch()){
205       $this->secondaryGroups[$attrs['gidNumber'][0]]= $attrs['cn'][0];
206     }
207     asort ($this->secondaryGroups);
209     /* Get global filter config */
210     if (!is_global("sysfilter")){
211       $ui= get_userinfo();
212       $base= get_base_from_people($ui->dn);
213       $sysfilter= array( "depselect"       => $base,
214           "regex"           => "*");
215       register_global("sysfilter", $sysfilter);
216     }
217     $this->ui = get_userinfo();
218   }
221   /* execute generates the html output for this node */
222   function execute($isCopyPaste = false)
223   {
224         /* Call parent execute */
225         plugin::execute();
226   $display= "";
228   /* Department has changed? */
229   if(isset($_POST['depselect'])){
230     $_SESSION['CurrentMainBase']= validate($_POST['depselect']);
231   }
233   if(!$isCopyPaste){
234     /* Do we need to flip is_account state? */
235     if (isset($_POST['modify_state'])){
236       $this->is_account= !$this->is_account;
237     }
239     /* Do we represent a valid posixAccount? */
240     if (!$this->is_account && $this->parent == NULL ){
241       $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\">&nbsp;<b>".
242         _("This account has no unix extensions.")."</b>";
243       $display.= back_to_main();
244       return ($display);
245     }
248     /* Show tab dialog headers */
249     if ($this->parent != NULL){
250       if ($this->is_account){
251         if (isset($this->parent->by_object['sambaAccount'])){
252           $obj= $this->parent->by_object['sambaAccount'];
253         }
254         if (isset($obj) && $obj->is_account == TRUE &&
255             ((isset($this->parent->by_object['sambaAccount']))&&($this->parent->by_object['sambaAccount']->is_account))
256             ||(isset($this->parent->by_object['environment'] ))&&($this->parent->by_object['environment'] ->is_account)){
258           /* Samba3 dependency on posix accounts are enabled
259              in the moment, because I need to rely on unique
260              uidNumbers. There'll be a better solution later
261              on. */
262           $display= $this->show_header(_("Remove posix account"),
263               _("This account has unix features enabled. To disable them, you'll need to remove the samba / environment account first."), TRUE);
264         } else {
265           $display= $this->show_header(_("Remove posix account"),
266               _("This account has posix features enabled. You can disable them by clicking below."));
267         }
268       } else {
269         $display= $this->show_header(_("Create posix account"),
270             _("This account has posix features disabled. You can enable them by clicking below."));
271         return($display);
272       }
273     }
274   }
275   /* Trigger group edit? */
276   if (isset($_POST['edit_groupmembership'])){
277     $this->group_dialog= TRUE;
278     $this->dialog= TRUE;
279   }
281   /* Cancel group edit? */
282   if (isset($_POST['add_groups_cancel']) ||
283       isset($_POST['add_groups_finish'])){
284     $this->group_dialog= FALSE;
285     $this->dialog= FALSE;
286   }
288   /* Add selected groups */
289   if (isset($_POST['add_groups_finish']) && isset($_POST['groups']) &&
290       count($_POST['groups'])){
292       if (chkacl ($this->acl, "memberUid") == ""){
293         $this->addGroup ($_POST['groups']);
294         $this->is_modified= TRUE;
295       }
296     }
298     /* Delete selected groups */
299     if (isset($_POST['delete_groupmembership']) && 
300         isset($_POST['group_list']) && count($_POST['group_list'])){
302       if (chkacl ($this->acl, "memberUid") == ""){
303         $this->delGroup ($_POST['group_list']);
304         $this->is_modified= TRUE;
305       }
306     }
308     /* Add user workstation? */
309     if (isset($_POST["add_ws"])){
310       $this->show_ws_dialog= TRUE;
311       $this->dialog= TRUE;
312     }
314     /* Add user workstation? */
315     if (isset($_POST["add_ws_finish"]) && isset($_POST['wslist'])){
316       foreach($_POST['wslist'] as $ws){
317         $this->accessTo[$ws]= $ws;
318       }
319       ksort($this->accessTo);
320       $this->is_modified= TRUE;
321     }
323     /* Remove user workstations? */
324     if (isset($_POST["delete_ws"]) && isset($_POST['workstation_list'])){
325       foreach($_POST['workstation_list'] as $name){
326         unset ($this->accessTo[$name]);
327       }
328       $this->is_modified= TRUE;
329     }
331     /* Add user workstation finished? */
332     if (isset($_POST["add_ws_finish"]) || isset($_POST["add_ws_cancel"])){
333       $this->show_ws_dialog= FALSE;
334       $this->dialog= FALSE;
335     }
337     /* Templates now! */
338     $smarty= get_smarty();
340     /* Show ws dialog */
341     if ($this->show_ws_dialog){
342       /* Save data */
343       $sysfilter= get_global("sysfilter");
344       foreach( array("depselect", "regex") as $type){
345         if (isset($_POST[$type])){
346           $sysfilter[$type]= $_POST[$type];
347         }
348       }
349       if (isset($_GET['search'])){
350         $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
351         if ($s == "**"){
352           $s= "*";
353         }
354         $sysfilter['regex']= $s;
355       }
356       register_global("sysfilter", $sysfilter);
358       /* Get workstation list */
359       $exclude= "";
360       foreach($this->accessTo as $ws){
361         $exclude.= "(cn=$ws)";
362       }
363       if ($exclude != ""){
364         $exclude= "(!(|$exclude))";
365       }
366       $acl= array($this->config->current['BASE'] => ":all");
367       $regex= $sysfilter['regex'];
368       $filter= "(&(|(objectClass=goServer)(objectClass=gotoWorkstation)(objectClass=gotoTerminal))$exclude(cn=*)(cn=$regex))";
369       $res= get_list($filter, $acl, $sysfilter['depselect'], array("cn"), GL_SUBSEARCH | GL_SIZELIMIT);
370       $wslist= array();
371       foreach ($res as $attrs){
372         $wslist[]= preg_replace('/\$/', '', $attrs['cn'][0]);
373       }
374       asort($wslist);
375       $smarty->assign("search_image", get_template_path('images/search.png'));
376       $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
377       $smarty->assign("tree_image", get_template_path('images/tree.png'));
378       $smarty->assign("deplist", $this->config->idepartments);
379       $smarty->assign("alphabet", generate_alphabet());
380       foreach( array("depselect", "regex") as $type){
381         $smarty->assign("$type", $sysfilter[$type]);
382       }
383       $smarty->assign("hint", print_sizelimit_warning());
384       $smarty->assign("wslist", $wslist);
385       $smarty->assign("apply", apply_filter());
386       $display= $smarty->fetch (get_template_path('trust_machines.tpl', TRUE, dirname(__FILE__)));
387       return ($display);
388     }
390     /* Manage group add dialog */
391     if ($this->group_dialog){
393       /* Get global filter config */
394       $this->reload();
396       /* remove already assigned groups */
397       $glist= array();
398       foreach ($this->grouplist as $key => $value){
399         if (!isset($this->groupMembership[$key])){
400           $glist[$key]= $value;
401         }
402       }
404       if($this->SubSearch){
405         $smarty->assign("SubSearchCHK"," checked ");
406       }else{
407         $smarty->assign("SubSearchCHK","");
408       }
410       $smarty->assign("regex",$this->GroupRegex);
411       $smarty->assign("guser",$this->GroupUserRegex);
412       $smarty->assign("groups", $glist);
413       $smarty->assign("search_image", get_template_path('images/search.png'));
414       $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
415       $smarty->assign("tree_image", get_template_path('images/tree.png'));
416       $smarty->assign("deplist", $this->config->idepartments);
417       $smarty->assign("alphabet", generate_alphabet());
418       $smarty->assign("depselect",$_SESSION['CurrentMainBase']);
419       $smarty->assign("hint", print_sizelimit_warning());
421       $smarty->assign("apply", apply_filter());
422       $display.= $smarty->fetch (get_template_path('posix_groups.tpl', TRUE, dirname(__FILE__)));
423       return ($display);
424     }
426     /* Show main page */
427     $smarty= get_smarty();
429     /* Depending on pwmode, currently hardcoded because there are no other methods */
430     if ( 1 == 1 ){
431       $smarty->assign("pwmode", dirname(__FILE__)."/posix_shadow");
432       $shadowMinACL= chkacl($this->acl, "shadowMin");
433       $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."\">"));
434       $shadowMaxACL= chkacl($this->acl, "shadowMax");
435       $smarty->assign("shadowmaxs", sprintf(_("Password must be changed after %s days"), "<input name=\"shadowMax\" size=3 maxlength=4 $shadowMaxACL value=\"".$this->shadowMax."\">"));
436       $shadowInactiveACL= chkacl($this->acl, "shadowInactive");
437       $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."\">"));
438       $shadowWarningACL= chkacl($this->acl, "shadowWarning");
439       $smarty->assign("shadowwarnings", sprintf(_("Warn user %s days before password expiery"), "<input name=\"shadowWarning\" size=3 maxlength=4 $shadowWarningACL value=\"".$this->shadowWarning."\">"));
440       foreach( array("must_change_password", "use_shadowMin", "use_shadowMax",
441             "use_shadowExpire", "use_shadowInactive",
442             "use_shadowWarning") as $val){
443         if ($this->$val == 1){
444           $smarty->assign("$val", "checked");
445         } else {
446           $smarty->assign("$val", "");
447         }
448         $smarty->assign("$val"."ACL", chkacl($this->acl, $val));
449       }
450     }
452     /* Fill calendar */
453     $date= getdate($this->shadowExpire);
455     $days= array();
456     for($d= 1; $d<32; $d++){
457       $days[$d]= $d;
458     }
459     $years= array();
460     for($y= $date['year']-10; $y<$date['year']+10; $y++){
461       $years[]= $y;
462     }
463     $months= array(_("January"), _("February"), _("March"), _("April"),
464         _("May"), _("June"), _("July"), _("August"), _("September"),
465         _("October"), _("November"), _("December"));
466     $smarty->assign("day", $date["mday"]);
467     $smarty->assign("days", $days);
468     $smarty->assign("months", $months);
469     $smarty->assign("month", $date["mon"]-1);
470     $smarty->assign("years", $years);
471     $smarty->assign("year", $date["year"]);
473     /* Fill arrays */
474     $smarty->assign("shells", $this->loginShellList);
475     $smarty->assign("secondaryGroups", $this->secondaryGroups);
476     $smarty->assign("primaryGroup", $this->primaryGroup);
477    if (!count($this->groupMembership)){
478       $smarty->assign("groupMembership", array("&nbsp;"));
479     } else {
480       $smarty->assign("groupMembership", $this->groupMembership);
481     }
482     if (count($this->groupMembership) > 16){
483       $smarty->assign("groups", "too_many_for_nfs");
484     } else {
485       $smarty->assign("groups", "");
486     }
487     $smarty->assign("printerList", $this->printerList);
488     $smarty->assign("languages", $this->config->data['MAIN']['LANGUAGES']);
490         /* Avoid "Undefined index: forceMode" */
491     $smarty->assign("forceMode", "");
493     /* Checkboxes */
494     if ($this->force_ids == 1){
495       $smarty->assign("force_ids", "checked");
496       if ($_SESSION['js']){
497         $smarty->assign("forceMode", "");
498       }
499     } else {
500       if ($_SESSION['js']){
501                 if($this->acl != "#none#")
502         $smarty->assign("forceMode", "disabled");
503       }
504       $smarty->assign("force_ids", "");
505     }
506     $smarty->assign("force_idsACL", chkacl($this->acl, "force_ids"));
508     /* Load attributes and acl's */
509     foreach($this->attributes as $val){
510       if((chkacl($this->acl,$val)=="")&&(($_SESSION["js"])&&(($val=="uidNumber")||($val=="gidNumber"))))
511         {
512           $smarty->assign("$val"."ACL","");
513           $smarty->assign("$val", $this->$val);
514           continue;
515         }
516       $smarty->assign("$val", $this->$val);
517       $smarty->assign("$val"."ACL", chkacl($this->acl,$val));
518     }
519     $smarty->assign("groupMembershipACL", chkacl($this->acl, "groupMembership"));
520     $smarty->assign("status", $this->status);
522     /* Work on trust modes */
523     $smarty->assign("trustmodeACL", chkacl($this->acl, "trustmode"));
524     if ($this->trustModel == "fullaccess"){
525       $trustmode= 1;
526       // pervent double disable tag in html code, this will disturb our clean w3c html
527     
528     if(chkacl($this->acl, "trustmode")==""){
529           $smarty->assign("trusthide", "disabled");
530       }else{
531           $smarty->assign("trusthide", "");
532       }
534     } elseif ($this->trustModel == "byhost"){
535       $trustmode= 2;
536       $smarty->assign("trusthide", "");
537     } else {
538       // pervent double disable tag in html code, this will disturb our clean w3c html
539       if(chkacl($this->acl, "trustmode")==""){
540           $smarty->assign("trusthide", "disabled");
541       }else{
542           $smarty->assign("trusthide", "");
543       }
544       $trustmode= 0;
545     }
546     $smarty->assign("trustmode", $trustmode);
547     $smarty->assign("trustmodes", array( 0 => _("disabled"), 1 => _("full access"),
548           2 => _("allow access to these hosts")));
552     if((count($this->accessTo))==0)
553       $smarty->assign("emptyArrAccess",true);
554     else
555       $smarty->assign("emptyArrAccess",false);
556     
559     $smarty->assign("workstations", $this->accessTo);
561     $smarty->assign("apply", apply_filter());
562     $display.= $smarty->fetch (get_template_path('generic.tpl', TRUE, dirname(__FILE__)));
563     return($display);
564   }
567   /* remove object from parent */
568   function remove_from_parent()
569   {
570     /* Cancel if there's nothing to do here */
571     if (!$this->initially_was_account){
572       return;
573     }
575     /* include global link_info */
576     $ldap= $this->config->get_ldap_link();
577     
578         /* Remove and write to LDAP */
579     plugin::remove_from_parent();
581     /* Zero out array */
582     $this->attrs['gosaHostACL']= array();
584     /* Keep uid, because we need it for authentification! */
585     unset($this->attrs['uid']);
586     unset($this->attrs['trustModel']);
588     @DEBUG (DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__,
589         $this->attributes, "Save");
590     $ldap->cd($this->dn);
591     $this->cleanup();
592     $ldap->modify ($this->attrs); 
594     show_ldap_error($ldap->get_error(), sprintf(_("Removing of user/posix account with dn '%s' failed."),$this->dn));
596     /* Delete group only if cn is uid and there are no other
597        members inside */
598     $ldap->cd ($this->config->current['BASE']);
599     $ldap->search ("(&(objectClass=posixGroup)(gidNumber=".$this->gidNumber."))", array("cn", "memberUid"));
600     if ($ldap->count() != 0){
601       $attrs= $ldap->fetch();
602       if ($attrs['cn'][0] == $this->uid &&
603           !isset($this->attrs['memberUid'])){
605         $ldap->rmDir($ldap->getDN());
606       }
607     }
609     /* Optionally execute a command after we're done */
610     $this->handle_post_events("remove");
611   }
614   function save_object()
615   {
616     if (isset($_POST['posixTab'])){
617       /* Save values to object */
618       plugin::save_object();
620       /* Save force GID attribute */
621       if (chkacl ($this->acl, "force_ids") == ""){
622         if (isset ($_POST['force_ids'])){
623           $data= 1;
624         } else {
625           $data= 0;
626         }
627         if ($this->force_ids != $data){
628           $this->is_modified= TRUE;
629         }
630         $this->force_ids= $data;
632         $data= $_POST['primaryGroup'];
633         if ($this->primaryGroup != $data){
634           $this->is_modified= TRUE;
635         }
636         $this->primaryGroup= $_POST['primaryGroup'];
637       }
639       /* Save pwmode dependent attributes, curently hardcoded because there're
640          no alternatives */
641       if (1 == 1){
642         foreach( array("must_change_password", "use_shadowMin", "use_shadowMax",
643               "use_shadowExpire", "use_shadowInactive",
644               "use_shadowWarning") as $val){
645           if (chkacl($this->acl, "$val") == ""){
646             if (isset ($_POST[$val])){
647               $data= 1;
648             } else {
649               $data= 0;
650             }
651             if ($data != $this->$val){
652               $this->is_modified= TRUE;
653             }
654             $this->$val= $data;
655           }
656         }
657       }
659       /* Trust mode - special handling */
660       if (isset($_POST['trustmode'])){
661         $saved= $this->trustModel;
662         if ($_POST['trustmode'] == "1"){
663           $this->trustModel= "fullaccess";
664         } elseif ($_POST['trustmode'] == "2"){
665           $this->trustModel= "byhost";
666         } else {
667           $this->trustModel= "";
668         }
669         if ($this->trustModel != $saved){
670           $this->is_modified= TRUE;
671         }
672       }
673     }
674   
675     /* Get regex from alphabet */
676     if(isset($_GET['search'])){
677       $this->GroupRegex = $_GET['search']."*";
678     }
680     /* Check checkboxes and regexes */
681     if(isset($_POST["PosixGroupDialogPosted"])){
683       if(isset($_POST['SubSearch']) && ($_POST['SubSearch'])){
684         $this->SubSearch = true;
685       }else{
686         $this->SubSearch = false;
687       }
688       if(isset($_POST['guser'])){
689         $this->GroupUserRegex = $_POST['guser'];
690       }
691       if(isset($_POST['regex'])){
692         $this->GroupRegex = $_POST['regex'];
693       }
694     }
695     $this->GroupRegex = preg_replace("/\*\**/","*",$this->GroupRegex);
696     $this->GroupUserRegex = preg_replace("/\*\**/","*",$this->GroupUserRegex);
697   }
700   /* Save data to LDAP, depending on is_account we save or delete */
701   function save()
702   {
703         
704     /* include global link_info */
705     $ldap= $this->config->get_ldap_link();
707     /* Adapt shadow values */
708     if (!$this->use_shadowExpire){
709       $this->shadowExpire= "0";
710     } else {
711       /* Transform seconds to days here */
712       $this->shadowExpire= (int)($this->shadowExpire / (60 * 60 * 24)) ;
713     }
714     if (!$this->use_shadowMax){
715       $this->shadowMax= "0";
716     }
717     if ($this->must_change_password){
718       $this->shadowLastChange= (int)(date("U") / 86400) - $this->shadowMax - 1;
719     } else {
720       $this->shadowLastChange= (int)(date("U") / 86400);
721     }
722     if (!$this->use_shadowWarning){
723       $this->shadowWarning= "0";
724     }
726     /* Check what to do with ID's */
727     if ($this->force_ids == 0){
729       /* Use id's that are already set */
730       if ($this->savedUidNumber != ""){
731         $this->uidNumber= $this->savedUidNumber;
732         $this->gidNumber= $this->savedGidNumber;
733       } else {
735         /* Calculate new id's. We need to place a lock before calling get_next_id
736            to get real unique values. */
737         $wait= 10;
738         while (get_lock("uidnumber") != ""){
739           sleep (1);
741           /* Oups - timed out */
742           if ($wait-- == 0){
743             print_red (_("Failed: overriding lock"));
744             break;
745           }
746         }
748         add_lock ("uidnumber", "gosa");
749         $this->uidNumber= $this->get_next_id("uidNumber");
750         if ($this->savedGidNumber != ""){
751           $this->gidNumber= $this->savedGidNumber;
752         } else {
753           $this->gidNumber= $this->get_next_id("gidNumber");
754         }
755       }
757       if ($this->primaryGroup != 0){
758         $this->gidNumber= $this->primaryGroup;
759       }
760     }
762     if ($this->use_shadowMin != "1" ) {
763       $this->shadowMin = "";
764     }
766     if (($this->use_shadowMax != "1") && ($this->must_change_password != "1")) {
767       $this->shadowMax = "";
768     }
770     if ($this->use_shadowWarning != "1" ) {
771       $this->shadowWarning = "";
772     }
774     if ($this->use_shadowInactive != "1" ) {
775       $this->shadowInactive = "";
776     }
778     if ($this->use_shadowExpire != "1" ) {
779       $this->shadowExpire = "";
780     }
782     /* Fill gecos */
783     if (isset($this->parent) && $this->parent != NULL){
784       $this->gecos= rewrite($this->parent->by_object['user']->cn);
785       if (!preg_match('/[a-z0-9 -]/i', $this->gecos)){
786         $this->gecos= "";
787       }
788     }
790         foreach(array("shadowMin","shadowMax","shadowWarning","shadowInactive","shadowExpire") as $attr){
791                 $this->$attr = (int) $this->$attr;
792         }
793     /* Call parents save to prepare $this->attrs */
794     plugin::save();
796     /* Trust accounts */
797     $objectclasses= array();
798     foreach ($this->attrs['objectClass'] as $key => $class){
799       if (preg_match('/trustAccount/i', $class)){
800         continue;
801       }
802       $objectclasses[]= $this->attrs['objectClass'][$key];
803     }
804     $this->attrs['objectClass']= $objectclasses;
805     if ($this->trustModel != ""){
806       $this->attrs['objectClass'][]= "trustAccount";
807       $this->attrs['trustModel']= $this->trustModel;
808       $this->attrs['accessTo']= array();
809       if ($this->trustModel == "byhost"){
810         foreach ($this->accessTo as $host){
811           $this->attrs['accessTo'][]= $host;
812         }
813       }
814     } else {
815       if ($this->was_trust_account){
816         $this->attrs['accessTo']= array();
817         $this->attrs['trustModel']= array();
818       }
819     }
821     if(empty($this->attrs['gosaDefaultPrinter'])){
822       $thid->attrs['gosaDefaultPrinter']=array();
823     }
826     /* Save data to LDAP */
827     $ldap->cd($this->dn);
828     $this->cleanup();
829     unset($this->attrs['uid']);
830     $ldap->modify ($this->attrs); 
832     show_ldap_error($ldap->get_error(), sprintf(_("Saving of user/posix account with dn '%s' failed."),$this->dn));
834     /* Remove lock needed for unique id generation */
835     del_lock ("uidnumber");
838     /* Posix accounts have group interrelationship, take care about these here. */
839     if ($this->force_ids == 0 && $this->primaryGroup == 0){
840       $ldap->cd($this->config->current['BASE']);
841       $ldap->search("(&(objectClass=posixGroup)(gidNumber=".$this->gidNumber."))", array("cn"));
843       /* Create group if it doesn't exist */
844       if ($ldap->count() == 0){
845         $groupdn= preg_replace ('/^'.$this->config->current['DNMODE'].'=[^,]+,'.get_people_ou().'/i', 'cn='.$this->uid.','.get_groups_ou(), $this->dn);
847         $g= new group($this->config, $groupdn);
848         $g->cn= $this->uid;
849         $g->force_gid= 1;
850         $g->gidNumber= $this->gidNumber;
851         $g->description= "Group of user ".$this->givenName." ".$this->sn;
852         $g->save ();
853       }
854     }
855  
856     /* Take care about groupMembership values: add to groups */
857     foreach ($this->groupMembership as $key => $value){
858       $g= new grouptabs($this->config,$this->config->data['TABS']['GROUPTABS'], $key);
859       $g->by_object['group']->addUser($this->uid);
860       $g->save();
861     }
862     
863     /* Remove from groups not listed in groupMembership */
864     foreach ($this->savedGroupMembership as $key => $value){
865       if (!isset($this->groupMembership[$key])){
866         $g= new grouptabs($this->config,$this->config->data['TABS']['GROUPTABS'], $key);
867         $g->by_object['group']->removeUser ($this->uid);
868         $g->save();
869       }
870     }
872     /* Optionally execute a command after we're done */
873     if ($this->initially_was_account == $this->is_account){
874       if ($this->is_modified){
875         $this->handle_post_events("mofify");
876       }
877     } else {
878       $this->handle_post_events("add");
879     }
880   }
882   /* Check formular input */
883   function check()
884   {
885     /* Include global link_info */
886     $ldap= $this->config->get_ldap_link();
888     /* Call common method to give check the hook */
889     $message= plugin::check();
891     /* must: homeDirectory */
892     if ($this->homeDirectory == ""){
893       $message[]= _("The required field 'Home directory' is not set.");
894     }
895     if (!is_path($this->homeDirectory)){
896       $message[]= _("Please enter a valid path in 'Home directory' field.");
897     }
899     /* Check ID's if they are forced by user */
900     if ($this->force_ids == "1"){
902       /* Valid uid/gid? */
903       if (!is_id($this->uidNumber)){
904         $message[]= _("Value specified as 'UID' is not valid.");
905       } else {
906         if ($this->uidNumber < $this->config->current['MINID']){
907           $message[]= _("Value specified as 'UID' is too small.");
908         }
909       }
910       if (!is_id($this->gidNumber)){
911         $message[]= _("Value specified as 'GID' is not valid.");
912       } else {
913         if ($this->gidNumber < $this->config->current['MINID']){
914           $message[]= _("Value specified as 'GID' is too small.");
915         }
916       }
917     }
919     /* Check shadow settings, well I like spaghetties... */
920     if ($this->use_shadowMin){
921       if (!is_id($this->shadowMin)){
922         $message[]= _("Value specified as 'shadowMin' is not valid.");
923       }
924     }
925     if ($this->use_shadowMax){
926       if (!is_id($this->shadowMax)){
927         $message[]= _("Value specified as 'shadowMax' is not valid.");
928       }
929     }
930     if ($this->use_shadowWarning){
931       if (!is_id($this->shadowWarning)){
932         $message[]= _("Value specified as 'shadowWarning' is not valid.");
933       }
934       if (!$this->use_shadowMax){
935         $message[]= _("'shadowWarning' without 'shadowMax' makes no sense.");
936       }
937       if ($this->shadowWarning > $this->shadowMax){
938         $message[]= _("Value specified as 'shadowWarning' should be smaller than 'shadowMax'.");
939       }
940       if ($this->use_shadowMin && $this->shadowWarning < $this->shadowMin){
941         $message[]= _("Value specified as 'shadowWarning' should be greater than 'shadowMin'.");
942       }
943     }
944     if ($this->use_shadowInactive){
945       if (!is_id($this->shadowInactive)){
946         $message[]= _("Value specified as 'shadowInactive' is not valid.");
947       }
948       if (!$this->use_shadowMax){
949         $message[]= _("'shadowInactive' without 'shadowMax' makes no sense.");
950       }
951     }
952     if ($this->use_shadowMin && $this->use_shadowMax){
953       if ($this->shadowMin > $this->shadowMax){
954         $message[]= _("Value specified as 'shadowMin' should be smaller than 'shadowMax'.");
955       }
956     }
958   //  if(empty($this->gosaDefaultPrinter)){
959   //    $message[]= _("You need to specify a valid default printer.");
960   //  }
962     return ($message);
963   }
965   function addGroup ($groups)
966   {
967     /* include global link_info */
968     $ldap= $this->config->get_ldap_link();
970     /* Walk through groups and add the descriptive entry if not exists */
971     foreach ($groups as $value){
972       if (!array_key_exists($value, $this->groupMembership)){
973         $ldap->cat($value, array('cn', 'description', 'dn'));
974         $attrs= $ldap->fetch();
975         error_reporting (0);
976         if (!isset($attrs['description'][0])){
977           $entry= $attrs["cn"][0];
978         } else {
979           $dsc= preg_replace ('/^Group of user/', _("Group of user"), $attrs["description"][0]);
980           $entry= $attrs["cn"][0]." [$dsc]";
981         }
982         error_reporting (E_ALL);
983         $this->groupMembership[$attrs['dn']]= $entry;
984       }
985     }
987     /* Sort groups */
988     asort ($this->groupMembership);
989     reset ($this->groupMembership);
990   }
993   /* Del posix user from some groups */
994   function delGroup ($groups)
995   {
996     $dest= array();
998     foreach ($this->groupMembership as $key => $value){
999       if (!in_array($key, $groups)){
1000         $dest[$key]= $value;
1001       }
1002     }
1003     $this->groupMembership= $dest;
1004   }
1006   /* Adapt from template, using 'dn' */
1007   function adapt_from_template($dn)
1008   {
1009     /* Include global link_info */
1010     $ldap= $this->config->get_ldap_link();
1012     plugin::adapt_from_template($dn);
1013     $template= $this->attrs['uid'][0];
1015     /* Adapt group membership */
1016     $ldap->cd($this->config->current['BASE']);
1017     $ldap->search("(&(objectClass=posixGroup)(memberUid=".$this->attrs["uid"][0]."))", array("description", "cn"));
1019     while ($this->attrs= $ldap->fetch()){
1020       if (!isset($this->attrs["description"][0])){
1021         $entry= $this->attrs["cn"][0];
1022       } else {
1023         $entry= $this->attrs["cn"][0]." [".$this->attrs["description"][0]."]";
1024       }
1025       $this->groupMembership[$ldap->getDN()]= $entry;
1026     }
1028     /* Fix primary group settings */
1029     $ldap->cd($this->config->current['BASE']);
1030     $ldap->search("(&(objectClass=posixGroup)(cn=$template)(gidNumber=".$this->gidNumber."))", array("cn"));
1031     if ($ldap->count() != 1){
1032       $this->primaryGroup= $this->gidNumber;
1033     }
1035         $ldap->cd($this->config->current['BASE']);
1036     $ldap->search("(&(objectClass=gosaUserTemplate)(uid=".$template."))", array("cn","accessTo"));
1037         while($attr = $ldap->fetch()){
1038                 $tmp = $attr['accessTo'];
1039                 unset ($tmp['count']);
1040                 $this->accessTo = $tmp; 
1041         }
1042         
1043     /* Adjust shadow checkboxes */
1044     foreach (array("shadowMin", "shadowMax", "shadowWarning", "shadowInactive",
1045           "shadowExpire") as $val){
1047       if ($this->$val != 0){
1048         $oval= "use_".$val;
1049         $this->$oval= "1";
1050       }
1051     }
1052   }
1054   function get_next_id($attrib)
1055   {
1056     $ids= array();
1057     $ldap= $this->config->get_ldap_link();
1059     $ldap->cd ($this->config->current['BASE']);
1060     if (preg_match('/gidNumber/i', $attrib)){
1061       $oc= "posixGroup";
1062     } else {
1063       $oc= "posixAccount";
1064     }
1065     $ldap->search ("(&(objectClass=$oc)($attrib=*))", array("$attrib"));
1067     /* Get list of ids */
1068     while ($attrs= $ldap->fetch()){
1069       $ids[]= (int)$attrs["$attrib"][0];
1070     }
1072     /* Find out next free id near to UID_BASE */
1073     for ($id= $this->config->current['UIDBASE']; $id++; $id<65000){
1074       if (!in_array($id, $ids)){
1075         return ($id);
1076       }
1077     }
1079     /* Should not happen */
1080     if ($id == 65000){
1081       print_red(_("Too many users, can't allocate a free ID!"));
1082       exit;
1083     }
1085   }
1087   function reload()
1088   {
1089     /* Set base for all searches */
1090     $base     = $_SESSION['CurrentMainBase'];
1091     $base     = $base;
1092     $ldap     = $this->config->get_ldap_link();    
1093     $attrs    =  array("cn", "description", "gidNumber");
1094     $Flags    = GL_SIZELIMIT;
1096     /* Get groups */
1097     if ($this->GroupUserRegex == '*'){
1098       $filter = "(&(objectClass=posixGroup)(cn=".$this->GroupRegex."))";
1099     } else {
1100       $filter= "(&(objectClass=posixGroup)(cn=".$this->GroupRegex.")(memberUid=".$this->GroupUserRegex."))";
1101     }
1102     if($this->SubSearch){
1103       $Flags |= GL_SUBSEARCH;
1104     }else{
1105       $base = get_groups_ou().$base;
1106     }
1109     $res= get_list($filter, $this->ui->subtreeACL, $base,$attrs, $Flags);
1111     /* check sizelimit */
1112     if (preg_match("/size limit/i", $ldap->error)){
1113       $_SESSION['limit_exceeded']= TRUE;
1114     }
1116     /* Create a list of users */
1117     $this->grouplist = array();
1118     foreach ($res as $value){
1119         $this->grouplist[$value['gidNumber'][0]]= $value;
1120     }
1122     $tmp=array();
1123     foreach($this->grouplist as $tkey => $val ){
1124       $tmp[strtolower($val['cn'][0]).$val['cn'][0]]=$val;
1125     }
1127     /* Sort index */
1128     ksort($tmp);
1130     /* Recreate index array[dn]=cn[description]*/
1131     $this->grouplist=array();
1132     foreach($tmp as $val){
1133       if(isset($val['description'])){
1134         $this->grouplist[$val['dn']]=$val['cn'][0]."&nbsp;[".$val['description'][0]."]";
1135       }else{
1136         $this->grouplist[$val['dn']]=$val['cn'][0];
1137       }
1138     }
1139     reset ($this->grouplist);
1140   }
1143   /* Create the posix dialog part for copy & paste */
1144   function getCopyDialog()
1145   {
1146     /* Skip dialog creation if this is not a valid account*/
1147     if(!$this->is_account) return("");
1148     if ($this->force_ids == 1){
1149       $force_ids = "checked";
1150       if ($_SESSION['js']){
1151         $forceMode = "";
1152       }
1153     } else {
1154       if ($_SESSION['js']){
1155         if($this->acl != "#none#")
1156           $forceMode ="disabled";
1157       }
1158       $force_ids = "";
1159     }
1160    
1161     $sta = "";
1162  
1163     /* Open group add dialog */
1164     if(isset($_POST['edit_groupmembership'])){
1165       $this->group_dialog = TRUE;
1166       $sta = "SubDialog";
1167     }
1169     /* If the group-add dialog is closed, call execute 
1170         to ensure that the membership is updatd */
1171     if(isset($_POST['add_groups_finish']) || isset($_POST['add_groups_cancel'])){
1172       $this->execute();
1173       $this->group_dialog =FALSE;
1174     }
1176     if($this->group_dialog){
1177       $str = $this->execute(true);
1178       $ret = array();
1179       $ret['string'] = $str;
1180       $ret['status'] = $sta;
1181       return($ret);
1182     }
1184     /* If a group member should be deleted, simply call execute */
1185     if(isset($_POST['delete_groupmembership'])){
1186       $this->execute();
1187     }
1189     /* Assigned informations to smarty */
1190     $smarty = get_smarty();
1191     $smarty->assign("homeDirectory",$this->homeDirectory);
1192     $smarty->assign("uidNumber",$this->uidNumber);
1193     $smarty->assign("gidNumber",$this->gidNumber);
1194     $smarty->assign("forceMode",$forceMode);
1195     $smarty->assign("force_ids",$force_ids);
1196     if (!count($this->groupMembership)){
1197       $smarty->assign("groupMembership", array("&nbsp;"));
1198     } else {
1199       $smarty->assign("groupMembership", $this->groupMembership);
1200     }
1202     /* Display wars message if there are more than 16 group members */
1203     if (count($this->groupMembership) > 16){
1204       $smarty->assign("groups", "too_many_for_nfs");
1205     } else {
1206       $smarty->assign("groups", "");
1207     }
1208     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE,dirname(__FILE__)));
1210     $ret = array();
1211     $ret['string'] = $str;
1212     $ret['status'] = $sta;
1213     return($ret);
1214   }
1217   function plInfo()
1218   {
1219     return (array(
1220           "plDescription"     => _("POSIX account"),
1221           "plSelfModify"      => TRUE,
1222           "plDepends"         => array("user"),
1223           "plPriority"        => 1,
1224           "plSection"         => "personal", 
1225           "plCategory"        => array("users"),
1226           "plOptions"         => array(),
1228           "plProvidedAcls"  => array(
1229             "homeDirectory"       =>  _("Home directory"), 
1230             "uidNumber"           =>  _("User ID"),
1231             "gidNumber"           =>  _("Group ID"),
1232             "homeDirectory"       =>  _("Home directory"),
1233             "primaryGroup"        =>  _("Primary group"),
1235             "loginShell"          =>  _("Shell"),
1236             "shadowMin"           =>  _("Shadow min"),
1237             "shadowMax"           =>  _("Shadow max"),
1238             "shadowWarning"       =>  _("Shadow warning"),
1239             "shadowInactive"      =>  _("Shadow inactive"),
1240             "shadowLastChange"    =>  _("Shadow last change"),
1242             "shadowExpire"        =>  _("Shadow expire"),
1243             "accessTo"            =>  _("System trust"),
1244             "trustModel"          =>  _("System trust model")))
1245            ) ;
1246   }
1249 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1250 ?>