Code

Removed print_a
[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 Posts */ 
394       if(isset($_POST['depselect'])){
395         if(isset($_POST['regex'])){
396           $this->GroupRegex = $_POST['regex'];
397         }  
398         if(isset($_POST['guser'])){
399           $this->GroupUserRegex = $_POST['guser'];
400         }  
401       }
403       /* Get global filter config */
404       $this->reload();
406       /* remove already assigned groups */
407       $glist= array();
408       foreach ($this->grouplist as $key => $value){
409         if (!isset($this->groupMembership[$key])){
410           $glist[$key]= $value;
411         }
412       }
414       if($this->SubSearch){
415         $smarty->assign("SubSearchCHK"," checked ");
416       }else{
417         $smarty->assign("SubSearchCHK","");
418       }
420       $smarty->assign("regex",$this->GroupRegex);
421       $smarty->assign("guser",$this->GroupUserRegex);
422       $smarty->assign("groups", $glist);
423       $smarty->assign("search_image", get_template_path('images/search.png'));
424       $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
425       $smarty->assign("tree_image", get_template_path('images/tree.png'));
426       $smarty->assign("deplist", $this->config->idepartments);
427       $smarty->assign("alphabet", generate_alphabet());
428       $smarty->assign("depselect",$_SESSION['CurrentMainBase']);
429       $smarty->assign("hint", print_sizelimit_warning());
431       $smarty->assign("apply", apply_filter());
432       $display.= $smarty->fetch (get_template_path('posix_groups.tpl', TRUE, dirname(__FILE__)));
433       return ($display);
434     }
436     /* Show main page */
437     $smarty= get_smarty();
439     /* Depending on pwmode, currently hardcoded because there are no other methods */
440     if ( 1 == 1 ){
441       $smarty->assign("pwmode", dirname(__FILE__)."/posix_shadow");
442       $shadowMinACL= chkacl($this->acl, "shadowMin");
443       $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."\">"));
444       $shadowMaxACL= chkacl($this->acl, "shadowMax");
445       $smarty->assign("shadowmaxs", sprintf(_("Password must be changed after %s days"), "<input name=\"shadowMax\" size=3 maxlength=4 $shadowMaxACL value=\"".$this->shadowMax."\">"));
446       $shadowInactiveACL= chkacl($this->acl, "shadowInactive");
447       $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."\">"));
448       $shadowWarningACL= chkacl($this->acl, "shadowWarning");
449       $smarty->assign("shadowwarnings", sprintf(_("Warn user %s days before password expiery"), "<input name=\"shadowWarning\" size=3 maxlength=4 $shadowWarningACL value=\"".$this->shadowWarning."\">"));
450       foreach( array("must_change_password", "use_shadowMin", "use_shadowMax",
451             "use_shadowExpire", "use_shadowInactive",
452             "use_shadowWarning") as $val){
453         if ($this->$val == 1){
454           $smarty->assign("$val", "checked");
455         } else {
456           $smarty->assign("$val", "");
457         }
458         $smarty->assign("$val"."ACL", chkacl($this->acl, $val));
459       }
460     }
462     /* Fill calendar */
463     $date= getdate($this->shadowExpire);
465     $days= array();
466     for($d= 1; $d<32; $d++){
467       $days[$d]= $d;
468     }
469     $years= array();
470     for($y= $date['year']-10; $y<$date['year']+10; $y++){
471       $years[]= $y;
472     }
473     $months= array(_("January"), _("February"), _("March"), _("April"),
474         _("May"), _("June"), _("July"), _("August"), _("September"),
475         _("October"), _("November"), _("December"));
476     $smarty->assign("day", $date["mday"]);
477     $smarty->assign("days", $days);
478     $smarty->assign("months", $months);
479     $smarty->assign("month", $date["mon"]-1);
480     $smarty->assign("years", $years);
481     $smarty->assign("year", $date["year"]);
483     /* Fill arrays */
484     $smarty->assign("shells", $this->loginShellList);
485     $smarty->assign("secondaryGroups", $this->secondaryGroups);
486     $smarty->assign("primaryGroup", $this->primaryGroup);
487    if (!count($this->groupMembership)){
488       $smarty->assign("groupMembership", array("&nbsp;"));
489     } else {
490       $smarty->assign("groupMembership", $this->groupMembership);
491     }
492     if (count($this->groupMembership) > 16){
493       $smarty->assign("groups", "too_many_for_nfs");
494     } else {
495       $smarty->assign("groups", "");
496     }
497     $smarty->assign("printerList", $this->printerList);
498     $smarty->assign("languages", $this->config->data['MAIN']['LANGUAGES']);
500         /* Avoid "Undefined index: forceMode" */
501     $smarty->assign("forceMode", "");
503     /* Checkboxes */
504     if ($this->force_ids == 1){
505       $smarty->assign("force_ids", "checked");
506       if ($_SESSION['js']){
507         $smarty->assign("forceMode", "");
508       }
509     } else {
510       if ($_SESSION['js']){
511                 if($this->acl != "#none#")
512         $smarty->assign("forceMode", "disabled");
513       }
514       $smarty->assign("force_ids", "");
515     }
516     $smarty->assign("force_idsACL", chkacl($this->acl, "force_ids"));
518     /* Load attributes and acl's */
519     foreach($this->attributes as $val){
520       if((chkacl($this->acl,$val)=="")&&(($_SESSION["js"])&&(($val=="uidNumber")||($val=="gidNumber"))))
521         {
522           $smarty->assign("$val"."ACL","");
523           $smarty->assign("$val", $this->$val);
524           continue;
525         }
526       $smarty->assign("$val", $this->$val);
527       $smarty->assign("$val"."ACL", chkacl($this->acl,$val));
528     }
529     $smarty->assign("groupMembershipACL", chkacl($this->acl, "groupMembership"));
530     $smarty->assign("status", $this->status);
532     /* Work on trust modes */
533     $smarty->assign("trustmodeACL", chkacl($this->acl, "trustmode"));
534     if ($this->trustModel == "fullaccess"){
535       $trustmode= 1;
536       // pervent double disable tag in html code, this will disturb our clean w3c html
537     
538     if(chkacl($this->acl, "trustmode")==""){
539           $smarty->assign("trusthide", "disabled");
540       }else{
541           $smarty->assign("trusthide", "");
542       }
544     } elseif ($this->trustModel == "byhost"){
545       $trustmode= 2;
546       $smarty->assign("trusthide", "");
547     } else {
548       // pervent double disable tag in html code, this will disturb our clean w3c html
549       if(chkacl($this->acl, "trustmode")==""){
550           $smarty->assign("trusthide", "disabled");
551       }else{
552           $smarty->assign("trusthide", "");
553       }
554       $trustmode= 0;
555     }
556     $smarty->assign("trustmode", $trustmode);
557     $smarty->assign("trustmodes", array( 0 => _("disabled"), 1 => _("full access"),
558           2 => _("allow access to these hosts")));
562     if((count($this->accessTo))==0)
563       $smarty->assign("emptyArrAccess",true);
564     else
565       $smarty->assign("emptyArrAccess",false);
566     
569     $smarty->assign("workstations", $this->accessTo);
571     $smarty->assign("apply", apply_filter());
572     $display.= $smarty->fetch (get_template_path('generic.tpl', TRUE, dirname(__FILE__)));
573     return($display);
574   }
577   /* remove object from parent */
578   function remove_from_parent()
579   {
580     /* Cancel if there's nothing to do here */
581     if (!$this->initially_was_account){
582       return;
583     }
585     /* include global link_info */
586     $ldap= $this->config->get_ldap_link();
587     
588         /* Remove and write to LDAP */
589     plugin::remove_from_parent();
591     /* Zero out array */
592     $this->attrs['gosaHostACL']= array();
594     /* Keep uid, because we need it for authentification! */
595     unset($this->attrs['uid']);
596     unset($this->attrs['trustModel']);
598     @DEBUG (DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__,
599         $this->attributes, "Save");
600     $ldap->cd($this->dn);
601     $this->cleanup();
602     $ldap->modify ($this->attrs); 
604     show_ldap_error($ldap->get_error(), sprintf(_("Removing of user/posix account with dn '%s' failed."),$this->dn));
606     /* Delete group only if cn is uid and there are no other
607        members inside */
608     $ldap->cd ($this->config->current['BASE']);
609     $ldap->search ("(&(objectClass=posixGroup)(gidNumber=".$this->gidNumber."))", array("cn", "memberUid"));
610     if ($ldap->count() != 0){
611       $attrs= $ldap->fetch();
612       if ($attrs['cn'][0] == $this->uid &&
613           !isset($this->attrs['memberUid'])){
615         $ldap->rmDir($ldap->getDN());
616       }
617     }
619     /* Optionally execute a command after we're done */
620     $this->handle_post_events("remove");
621   }
624   function save_object()
625   {
626     if (isset($_POST['posixTab'])){
627       /* Save values to object */
628       plugin::save_object();
630       /* Save force GID attribute */
631       if (chkacl ($this->acl, "force_ids") == ""){
632         if (isset ($_POST['force_ids'])){
633           $data= 1;
634         } else {
635           $data= 0;
636         }
637         if ($this->force_ids != $data){
638           $this->is_modified= TRUE;
639         }
640         $this->force_ids= $data;
642         $data= $_POST['primaryGroup'];
643         if ($this->primaryGroup != $data){
644           $this->is_modified= TRUE;
645         }
646         $this->primaryGroup= $_POST['primaryGroup'];
647       }
649       /* Save pwmode dependent attributes, curently hardcoded because there're
650          no alternatives */
651       if (1 == 1){
652         foreach( array("must_change_password", "use_shadowMin", "use_shadowMax",
653               "use_shadowExpire", "use_shadowInactive",
654               "use_shadowWarning") as $val){
655           if (chkacl($this->acl, "$val") == ""){
656             if (isset ($_POST[$val])){
657               $data= 1;
658             } else {
659               $data= 0;
660             }
661             if ($data != $this->$val){
662               $this->is_modified= TRUE;
663             }
664             $this->$val= $data;
665           }
666         }
667       }
669       /* Trust mode - special handling */
670       if (isset($_POST['trustmode'])){
671         $saved= $this->trustModel;
672         if ($_POST['trustmode'] == "1"){
673           $this->trustModel= "fullaccess";
674         } elseif ($_POST['trustmode'] == "2"){
675           $this->trustModel= "byhost";
676         } else {
677           $this->trustModel= "";
678         }
679         if ($this->trustModel != $saved){
680           $this->is_modified= TRUE;
681         }
682       }
683     }
684     if(isset($_POST["PosixGroupDialogPosted"])){
685       if(isset($_POST['SubSearch']) && ($_POST['SubSearch'])){
686         $this->SubSearch = true;
687       }else{
688         $this->SubSearch = false;
689       }
690     }
691   }
694   /* Save data to LDAP, depending on is_account we save or delete */
695   function save()
696   {
697         
698     /* include global link_info */
699     $ldap= $this->config->get_ldap_link();
701     /* Adapt shadow values */
702     if (!$this->use_shadowExpire){
703       $this->shadowExpire= "0";
704     } else {
705       /* Transform seconds to days here */
706       $this->shadowExpire= (int)($this->shadowExpire / (60 * 60 * 24)) ;
707     }
708     if (!$this->use_shadowMax){
709       $this->shadowMax= "0";
710     }
711     if ($this->must_change_password){
712       $this->shadowLastChange= (int)(date("U") / 86400) - $this->shadowMax - 1;
713     } else {
714       $this->shadowLastChange= (int)(date("U") / 86400);
715     }
716     if (!$this->use_shadowWarning){
717       $this->shadowWarning= "0";
718     }
720     /* Check what to do with ID's */
721     if ($this->force_ids == 0){
723       /* Use id's that are already set */
724       if ($this->savedUidNumber != ""){
725         $this->uidNumber= $this->savedUidNumber;
726         $this->gidNumber= $this->savedGidNumber;
727       } else {
729         /* Calculate new id's. We need to place a lock before calling get_next_id
730            to get real unique values. */
731         $wait= 10;
732         while (get_lock("uidnumber") != ""){
733           sleep (1);
735           /* Oups - timed out */
736           if ($wait-- == 0){
737             print_red (_("Failed: overriding lock"));
738             break;
739           }
740         }
742         add_lock ("uidnumber", "gosa");
743         $this->uidNumber= $this->get_next_id("uidNumber");
744         if ($this->savedGidNumber != ""){
745           $this->gidNumber= $this->savedGidNumber;
746         } else {
747           $this->gidNumber= $this->get_next_id("gidNumber");
748         }
749       }
751       if ($this->primaryGroup != 0){
752         $this->gidNumber= $this->primaryGroup;
753       }
754     }
756     if ($this->use_shadowMin != "1" ) {
757       $this->shadowMin = "";
758     }
760     if (($this->use_shadowMax != "1") && ($this->must_change_password != "1")) {
761       $this->shadowMax = "";
762     }
764     if ($this->use_shadowWarning != "1" ) {
765       $this->shadowWarning = "";
766     }
768     if ($this->use_shadowInactive != "1" ) {
769       $this->shadowInactive = "";
770     }
772     if ($this->use_shadowExpire != "1" ) {
773       $this->shadowExpire = "";
774     }
776     /* Fill gecos */
777     if (isset($this->parent) && $this->parent != NULL){
778       $this->gecos= rewrite($this->parent->by_object['user']->cn);
779       if (!preg_match('/[a-z0-9 -]/i', $this->gecos)){
780         $this->gecos= "";
781       }
782     }
784         foreach(array("shadowMin","shadowMax","shadowWarning","shadowInactive","shadowExpire") as $attr){
785                 $this->$attr = (int) $this->$attr;
786         }
787     /* Call parents save to prepare $this->attrs */
788     plugin::save();
790     /* Trust accounts */
791     $objectclasses= array();
792     foreach ($this->attrs['objectClass'] as $key => $class){
793       if (preg_match('/trustAccount/i', $class)){
794         continue;
795       }
796       $objectclasses[]= $this->attrs['objectClass'][$key];
797     }
798     $this->attrs['objectClass']= $objectclasses;
799     if ($this->trustModel != ""){
800       $this->attrs['objectClass'][]= "trustAccount";
801       $this->attrs['trustModel']= $this->trustModel;
802       $this->attrs['accessTo']= array();
803       if ($this->trustModel == "byhost"){
804         foreach ($this->accessTo as $host){
805           $this->attrs['accessTo'][]= $host;
806         }
807       }
808     } else {
809       if ($this->was_trust_account){
810         $this->attrs['accessTo']= array();
811         $this->attrs['trustModel']= array();
812       }
813     }
815     if(empty($this->attrs['gosaDefaultPrinter'])){
816       $thid->attrs['gosaDefaultPrinter']=array();
817     }
820     /* Save data to LDAP */
821     $ldap->cd($this->dn);
822     $this->cleanup();
823     unset($this->attrs['uid']);
824     $ldap->modify ($this->attrs); 
826     show_ldap_error($ldap->get_error(), sprintf(_("Saving of user/posix account with dn '%s' failed."),$this->dn));
828     /* Remove lock needed for unique id generation */
829     del_lock ("uidnumber");
832     /* Posix accounts have group interrelationship, take care about these here. */
833     if ($this->force_ids == 0 && $this->primaryGroup == 0){
834       $ldap->cd($this->config->current['BASE']);
835       $ldap->search("(&(objectClass=posixGroup)(gidNumber=".$this->gidNumber."))", array("cn"));
837       /* Create group if it doesn't exist */
838       if ($ldap->count() == 0){
839         $groupdn= preg_replace ('/^'.$this->config->current['DNMODE'].'=[^,]+,'.get_people_ou().'/i', 'cn='.$this->uid.','.get_groups_ou(), $this->dn);
841         $g= new group($this->config, $groupdn);
842         $g->cn= $this->uid;
843         $g->force_gid= 1;
844         $g->gidNumber= $this->gidNumber;
845         $g->description= "Group of user ".$this->givenName." ".$this->sn;
846         $g->save ();
847       }
848     }
849  
850     /* Take care about groupMembership values: add to groups */
851     foreach ($this->groupMembership as $key => $value){
852       $g= new grouptabs($this->config,$this->config->data['TABS']['GROUPTABS'], $key);
853       $g->by_object['group']->addUser($this->uid);
854       $g->save();
855     }
856     
857     /* Remove from groups not listed in groupMembership */
858     foreach ($this->savedGroupMembership as $key => $value){
859       if (!isset($this->groupMembership[$key])){
860         $g= new grouptabs($this->config,$this->config->data['TABS']['GROUPTABS'], $key);
861         $g->by_object['group']->removeUser ($this->uid);
862         $g->save();
863       }
864     }
866     /* Optionally execute a command after we're done */
867     if ($this->initially_was_account == $this->is_account){
868       if ($this->is_modified){
869         $this->handle_post_events("mofify");
870       }
871     } else {
872       $this->handle_post_events("add");
873     }
874   }
876   /* Check formular input */
877   function check()
878   {
879     /* Include global link_info */
880     $ldap= $this->config->get_ldap_link();
882     /* Call common method to give check the hook */
883     $message= plugin::check();
885     /* must: homeDirectory */
886     if ($this->homeDirectory == ""){
887       $message[]= _("The required field 'Home directory' is not set.");
888     }
889     if (!is_path($this->homeDirectory)){
890       $message[]= _("Please enter a valid path in 'Home directory' field.");
891     }
893     /* Check ID's if they are forced by user */
894     if ($this->force_ids == "1"){
896       /* Valid uid/gid? */
897       if (!is_id($this->uidNumber)){
898         $message[]= _("Value specified as 'UID' is not valid.");
899       } else {
900         if ($this->uidNumber < $this->config->current['MINID']){
901           $message[]= _("Value specified as 'UID' is too small.");
902         }
903       }
904       if (!is_id($this->gidNumber)){
905         $message[]= _("Value specified as 'GID' is not valid.");
906       } else {
907         if ($this->gidNumber < $this->config->current['MINID']){
908           $message[]= _("Value specified as 'GID' is too small.");
909         }
910       }
911     }
913     /* Check shadow settings, well I like spaghetties... */
914     if ($this->use_shadowMin){
915       if (!is_id($this->shadowMin)){
916         $message[]= _("Value specified as 'shadowMin' is not valid.");
917       }
918     }
919     if ($this->use_shadowMax){
920       if (!is_id($this->shadowMax)){
921         $message[]= _("Value specified as 'shadowMax' is not valid.");
922       }
923     }
924     if ($this->use_shadowWarning){
925       if (!is_id($this->shadowWarning)){
926         $message[]= _("Value specified as 'shadowWarning' is not valid.");
927       }
928       if (!$this->use_shadowMax){
929         $message[]= _("'shadowWarning' without 'shadowMax' makes no sense.");
930       }
931       if ($this->shadowWarning > $this->shadowMax){
932         $message[]= _("Value specified as 'shadowWarning' should be smaller than 'shadowMax'.");
933       }
934       if ($this->use_shadowMin && $this->shadowWarning < $this->shadowMin){
935         $message[]= _("Value specified as 'shadowWarning' should be greater than 'shadowMin'.");
936       }
937     }
938     if ($this->use_shadowInactive){
939       if (!is_id($this->shadowInactive)){
940         $message[]= _("Value specified as 'shadowInactive' is not valid.");
941       }
942       if (!$this->use_shadowMax){
943         $message[]= _("'shadowInactive' without 'shadowMax' makes no sense.");
944       }
945     }
946     if ($this->use_shadowMin && $this->use_shadowMax){
947       if ($this->shadowMin > $this->shadowMax){
948         $message[]= _("Value specified as 'shadowMin' should be smaller than 'shadowMax'.");
949       }
950     }
952   //  if(empty($this->gosaDefaultPrinter)){
953   //    $message[]= _("You need to specify a valid default printer.");
954   //  }
956     return ($message);
957   }
959   function addGroup ($groups)
960   {
961     /* include global link_info */
962     $ldap= $this->config->get_ldap_link();
964     /* Walk through groups and add the descriptive entry if not exists */
965     foreach ($groups as $value){
966       if (!array_key_exists($value, $this->groupMembership)){
967         $ldap->cat($value, array('cn', 'description', 'dn'));
968         $attrs= $ldap->fetch();
969         error_reporting (0);
970         if (!isset($attrs['description'][0])){
971           $entry= $attrs["cn"][0];
972         } else {
973           $dsc= preg_replace ('/^Group of user/', _("Group of user"), $attrs["description"][0]);
974           $entry= $attrs["cn"][0]." [$dsc]";
975         }
976         error_reporting (E_ALL);
977         $this->groupMembership[$attrs['dn']]= $entry;
978       }
979     }
981     /* Sort groups */
982     asort ($this->groupMembership);
983     reset ($this->groupMembership);
984   }
987   /* Del posix user from some groups */
988   function delGroup ($groups)
989   {
990     $dest= array();
992     foreach ($this->groupMembership as $key => $value){
993       if (!in_array($key, $groups)){
994         $dest[$key]= $value;
995       }
996     }
997     $this->groupMembership= $dest;
998   }
1000   /* Adapt from template, using 'dn' */
1001   function adapt_from_template($dn)
1002   {
1003     /* Include global link_info */
1004     $ldap= $this->config->get_ldap_link();
1006     plugin::adapt_from_template($dn);
1007     $template= $this->attrs['uid'][0];
1009     /* Adapt group membership */
1010     $ldap->cd($this->config->current['BASE']);
1011     $ldap->search("(&(objectClass=posixGroup)(memberUid=".$this->attrs["uid"][0]."))", array("description", "cn"));
1013     while ($this->attrs= $ldap->fetch()){
1014       if (!isset($this->attrs["description"][0])){
1015         $entry= $this->attrs["cn"][0];
1016       } else {
1017         $entry= $this->attrs["cn"][0]." [".$this->attrs["description"][0]."]";
1018       }
1019       $this->groupMembership[$ldap->getDN()]= $entry;
1020     }
1022     /* Fix primary group settings */
1023     $ldap->cd($this->config->current['BASE']);
1024     $ldap->search("(&(objectClass=posixGroup)(cn=$template)(gidNumber=".$this->gidNumber."))", array("cn"));
1025     if ($ldap->count() != 1){
1026       $this->primaryGroup= $this->gidNumber;
1027     }
1029         $ldap->cd($this->config->current['BASE']);
1030     $ldap->search("(&(objectClass=gosaUserTemplate)(uid=".$template."))", array("cn","accessTo"));
1031         while($attr = $ldap->fetch()){
1032                 $tmp = $attr['accessTo'];
1033                 unset ($tmp['count']);
1034                 $this->accessTo = $tmp; 
1035         }
1036         
1037     /* Adjust shadow checkboxes */
1038     foreach (array("shadowMin", "shadowMax", "shadowWarning", "shadowInactive",
1039           "shadowExpire") as $val){
1041       if ($this->$val != 0){
1042         $oval= "use_".$val;
1043         $this->$oval= "1";
1044       }
1045     }
1046   }
1048   function get_next_id($attrib)
1049   {
1050     $ids= array();
1051     $ldap= $this->config->get_ldap_link();
1053     $ldap->cd ($this->config->current['BASE']);
1054     if (preg_match('/gidNumber/i', $attrib)){
1055       $oc= "posixGroup";
1056     } else {
1057       $oc= "posixAccount";
1058     }
1059     $ldap->search ("(&(objectClass=$oc)($attrib=*))", array("$attrib"));
1061     /* Get list of ids */
1062     while ($attrs= $ldap->fetch()){
1063       $ids[]= (int)$attrs["$attrib"][0];
1064     }
1066     /* Find out next free id near to UID_BASE */
1067     for ($id= $this->config->current['UIDBASE']; $id++; $id<65000){
1068       if (!in_array($id, $ids)){
1069         return ($id);
1070       }
1071     }
1073     /* Should not happen */
1074     if ($id == 65000){
1075       print_red(_("Too many users, can't allocate a free ID!"));
1076       exit;
1077     }
1079   }
1081   function reload()
1082   {
1083     /* Set base for all searches */
1084     $base     = $_SESSION['CurrentMainBase'];
1085     $base     = $base;
1086     $ldap     = $this->config->get_ldap_link();    
1087     $attrs    =  array("cn", "description", "gidNumber");
1088     $Flags    = GL_SIZELIMIT;
1090     /* Get groups */
1091     if ($this->GroupUserRegex == '*'){
1092       $filter = "(objectClass=posixGroup)";
1093     } else {
1094       $filter= "(&(objectClass=posixGroup)(cn=".$this->GroupRegex.")(memberUid=".$this->GroupUserRegex."))";
1095     }
1096     if($this->SubSearch){
1097       $Flags |= GL_SUBSEARCH;
1098     }else{
1099       $base = get_groups_ou().$base;
1100     }
1103     $res= get_list($filter, $this->ui->subtreeACL, $base,$attrs, $Flags);
1105     /* check sizelimit */
1106     if (preg_match("/size limit/i", $ldap->error)){
1107       $_SESSION['limit_exceeded']= TRUE;
1108     }
1110     /* Create a list of users */
1111     $this->grouplist = array();
1112     foreach ($res as $value){
1113         $this->grouplist[$value['gidNumber'][0]]= $value;
1114     }
1116     $tmp=array();
1117     foreach($this->grouplist as $tkey => $val ){
1118       $tmp[strtolower($val['cn'][0]).$val['cn'][0]]=$val;
1119     }
1121     /* Sort index */
1122     ksort($tmp);
1124     /* Recreate index array[dn]=cn[description]*/
1125     $this->grouplist=array();
1126     foreach($tmp as $val){
1127       if(isset($val['description'])){
1128         $this->grouplist[$val['dn']]=$val['cn'][0]."&nbsp;[".$val['description'][0]."]";
1129       }else{
1130         $this->grouplist[$val['dn']]=$val['cn'][0];
1131       }
1132     }
1133     reset ($this->grouplist);
1134   }
1137   /* Create the posix dialog part for copy & paste */
1138   function getCopyDialog()
1139   {
1140     /* Skip dialog creation if this is not a valid account*/
1141     if(!$this->is_account) return("");
1142     if ($this->force_ids == 1){
1143       $force_ids = "checked";
1144       if ($_SESSION['js']){
1145         $forceMode = "";
1146       }
1147     } else {
1148       if ($_SESSION['js']){
1149         if($this->acl != "#none#")
1150           $forceMode ="disabled";
1151       }
1152       $force_ids = "";
1153     }
1154    
1155     $sta = "";
1156  
1157     /* Open group add dialog */
1158     if(isset($_POST['edit_groupmembership'])){
1159       $this->group_dialog = TRUE;
1160       $sta = "SubDialog";
1161     }
1163     /* If the group-add dialog is closed, call execute 
1164         to ensure that the membership is updatd */
1165     if(isset($_POST['add_groups_finish']) || isset($_POST['add_groups_cancel'])){
1166       $this->execute();
1167       $this->group_dialog =FALSE;
1168     }
1170     if($this->group_dialog){
1171       $str = $this->execute(true);
1172       $ret = array();
1173       $ret['string'] = $str;
1174       $ret['status'] = $sta;
1175       return($ret);
1176     }
1178     /* If a group member should be deleted, simply call execute */
1179     if(isset($_POST['delete_groupmembership'])){
1180       $this->execute();
1181     }
1183     /* Assigned informations to smarty */
1184     $smarty = get_smarty();
1185     $smarty->assign("homeDirectory",$this->homeDirectory);
1186     $smarty->assign("uidNumber",$this->uidNumber);
1187     $smarty->assign("gidNumber",$this->gidNumber);
1188     $smarty->assign("forceMode",$forceMode);
1189     $smarty->assign("force_ids",$force_ids);
1190     if (!count($this->groupMembership)){
1191       $smarty->assign("groupMembership", array("&nbsp;"));
1192     } else {
1193       $smarty->assign("groupMembership", $this->groupMembership);
1194     }
1196     /* Display wars message if there are more than 16 group members */
1197     if (count($this->groupMembership) > 16){
1198       $smarty->assign("groups", "too_many_for_nfs");
1199     } else {
1200       $smarty->assign("groups", "");
1201     }
1202     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE,dirname(__FILE__)));
1204     $ret = array();
1205     $ret['string'] = $str;
1206     $ret['status'] = $sta;
1207     return($ret);
1208   }
1212 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1213 ?>