Code

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