Code

6a66a4f6cc7e9f39e6c7904906a8460a25759fa1
[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;
63   /* attribute list for save action */
64   var $attributes= array("homeDirectory", "loginShell", "uidNumber", "gidNumber", "gecos",
65       "shadowMin", "shadowMax", "shadowWarning", "shadowInactive", "shadowLastChange",
66       "shadowExpire", "gosaDefaultPrinter", "gosaDefaultLanguage", "uid");
67   var $objectclasses= array("posixAccount", "shadowAccount");
70   /* constructor, if 'dn' is set, the node loads the given
71      'dn' from LDAP */
72   function posixAccount ($config, $dn= NULL)
73   {
74     /* Configuration is fine, allways */
75     $this->config= $config;
77     /* Load bases attributes */
78     plugin::plugin($config, $dn);
80     $ldap= $this->config->get_ldap_link();
82     if ($dn != NULL){
84       /* Correct is_account. shadowAccount is not required. */
85       if (isset($this->attrs['objectClass']) &&
86           in_array ('posixAccount', $this->attrs['objectClass'])){
88         $this->is_account= TRUE;
89       }
91       /* Is this account a trustAccount? */
92       if ($this->is_account && isset($this->attrs['trustModel'])){
93         $this->trustModel= $this->attrs['trustModel'][0];
94         $this->was_trust_account= TRUE;
95       } else {
96         $this->was_trust_account= FALSE;
97         $this->trustModel= "";
98       }
99       if ($this->is_account && isset($this->attrs['accessTo'])){
100         for ($i= 0; $i<$this->attrs['accessTo']['count']; $i++){
101           $tmp= $this->attrs['accessTo'][$i];
102           $this->accessTo[$tmp]= $tmp;
103         }
104       }
105       $this->initially_was_account= $this->is_account;
107       /* Fill group */
108       $this->primaryGroup= $this->gidNumber;
110       /* Generate status text */
111       $current= date("U");
112       if (($current >= $this->shadowExpire) && $this->shadowExpire){
113         $this->status= "expired";
114         if (($this->shadowExpire - $current) < $this->shadowInactive){
115           $this->status.= ", grace time active";
116         }
117       } elseif (($this->shadowLastChange + $this->shadowMin) >= $current){
118         $this->status= "active, password not changable";
119       } elseif (($this->shadowLastChange + $this->shadowMax) >= $current){
120         $this->status= "active, password expired";
121       } else {
122         $this->status= "active";
123       }
125       /* Get group membership */
126       $ldap->cd($this->config->current['BASE']);
127       $ldap->search("(&(objectClass=posixGroup)(memberUid=".$this->uid."))", array("cn", "description"));
129       while ($this->attrs= $ldap->fetch()){
130         if (!isset($this->attrs["description"][0])){
131           $entry= $this->attrs["cn"][0];
132         } else {
133           $dsc= preg_replace ('/^Group of user/', _("Group of user"), $this->attrs["description"][0]);
134           $entry= $this->attrs["cn"][0]." [$dsc]";
135         }
136         $this->groupMembership[$ldap->getDN()]= $entry;
137       }
138       asort($this->groupMembership);
139       reset($this->groupMembership);
140       $this->savedGroupMembership= $this->groupMembership;
141       $this->savedUidNumber= $this->uidNumber;
142       $this->savedGidNumber= $this->gidNumber;
143     }
145     /* Adjust shadow checkboxes */
146     foreach (array("shadowMin", "shadowMax", "shadowWarning", "shadowInactive",
147           "shadowExpire") as $val){
149       if ($this->$val != 0){
150         $oval= "use_".$val;
151         $this->$oval= "1";
152       }
153     }
155     /* Convert to seconds */
156     if ($this->shadowExpire != 0){
157       $this->shadowExpire*= 60 * 60 * 24;
158     } else {
159       $date= getdate();
160       $this->shadowExpire= floor($date[0] / (60*60*24)) * 60 * 60 * 24;
161     }
163     /* Generate shell list from /etc/gosa/shells */
164     if (file_exists('/etc/gosa/shells')){
165       $shells = file ('/etc/gosa/shells');
166       foreach ($shells as $line){
167         if (!preg_match ("/^#/", $line)){
168           $this->loginShellList[]= trim($line);
169         }
170       }
171     } else {
172       if ($this->loginShell == ""){
173         $this->loginShellList[]= _("unconfigured");
174       }
175     }
177     /* Insert possibly missing loginShell */
178     if ($this->loginShell != "" && !in_array($this->loginShell, $this->loginShellList)){
179       $this->loginShellList[]= $this->loginShell;
180     }
182     /* Generate printer list */
183     if (isset($this->config->data['SERVERS']['CUPS'])){
184       $this->printerList= get_printer_list ($this->config->data['SERVERS']['CUPS']);
185       asort($this->printerList);
186     }
188     /* Generate group list */
189     $ldap->cd($this->config->current['BASE']);
190     $ldap->search("(objectClass=posixGroup)", array("cn", "gidNumber"));
191     $this->secondaryGroups[]= "- "._("automatic")." -";
192     while ($attrs= $ldap->fetch()){
193       $this->secondaryGroups[$attrs['gidNumber'][0]]= $attrs['cn'][0];
194     }
195     asort ($this->secondaryGroups);
197     /* Get global filter config */
198     if (!is_global("sysfilter")){
199       $ui= get_userinfo();
200       $base= get_base_from_people($ui->dn);
201       $sysfilter= array( "depselect"       => $base,
202           "regex"           => "*");
203       register_global("sysfilter", $sysfilter);
204     }
205   }
208   /* execute generates the html output for this node */
209   function execute()
210   {
211     /* Do we need to flip is_account state? */
212     if (isset($_POST['modify_state'])){
213       $this->is_account= !$this->is_account;
214     }
216     /* Do we represent a valid posixAccount? */
217     if (!$this->is_account && $this->parent == NULL ){
218       $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\">&nbsp;<b>".
219         _("This account has no unix extensions.")."</b>";
220       $display.= back_to_main();
221       return ($display);
222     }
224     $display= "";
226     /* Show tab dialog headers */
227     if ($this->parent != NULL){
228       if ($this->is_account){
229         if (isset($this->parent->by_object['sambaAccount'])){
230           $obj= $this->parent->by_object['sambaAccount'];
231         }
232         if (isset($obj) && $obj->is_account == TRUE &&
233              ((isset($this->parent->by_object['sambaAccount']))&&($this->parent->by_object['sambaAccount']->is_account))
234                         ||(isset($this->parent->by_object['environment'] ))&&($this->parent->by_object['environment'] ->is_account)){
236           /* Samba3 dependency on posix accounts are enabled
237              in the moment, because I need to rely on unique
238              uidNumbers. There'll be a better solution later
239              on. */
240           $display= $this->show_header(_("Remove posix account"),
241               _("This account has unix features enabled. To disable them, you'll need to remove the samba / environment account first."), TRUE);
242         } else {
243           $display= $this->show_header(_("Remove posix account"),
244               _("This account has posix features enabled. You can disable them by clicking below."));
245         }
246       } else {
247         $display= $this->show_header(_("Create posix account"),
248             _("This account has posix features disabled. You can enable them by clicking below."));
249         return($display);
250       }
251     }
253     /* Trigger group edit? */
254     if (isset($_POST['edit_groupmembership'])){
255       $this->group_dialog= TRUE;
256       $this->dialog= TRUE;
257     }
259     /* Cancel group edit? */
260     if (isset($_POST['add_groups_cancel']) ||
261         isset($_POST['add_groups_finish'])){
262       $this->group_dialog= FALSE;
263       $this->dialog= FALSE;
264     }
266     /* Add selected groups */
267     if (isset($_POST['add_groups_finish']) && isset($_POST['groups']) &&
268         count($_POST['groups'])){
270       if (chkacl ($this->acl, "memberUid") == ""){
271         $this->addGroup ($_POST['groups']);
272         $this->is_modified= TRUE;
273       }
274     }
276     /* Delete selected groups */
277     if (isset($_POST['delete_groupmembership']) && 
278         isset($_POST['group_list']) && count($_POST['group_list'])){
280       if (chkacl ($this->acl, "memberUid") == ""){
281         $this->delGroup ($_POST['group_list']);
282         $this->is_modified= TRUE;
283       }
284     }
286     /* Add user workstation? */
287     if (isset($_POST["add_ws"])){
288       $this->show_ws_dialog= TRUE;
289       $this->dialog= TRUE;
290     }
292     /* Add user workstation? */
293     if (isset($_POST["add_ws_finish"]) && isset($_POST['wslist'])){
294       foreach($_POST['wslist'] as $ws){
295         $this->accessTo[$ws]= $ws;
296       }
297       ksort($this->accessTo);
298       $this->is_modified= TRUE;
299     }
301     /* Remove user workstations? */
302     if (isset($_POST["delete_ws"]) && isset($_POST['workstation_list'])){
303       foreach($_POST['workstation_list'] as $name){
304         unset ($this->accessTo[$name]);
305       }
306       $this->is_modified= TRUE;
307     }
309     /* Add user workstation finished? */
310     if (isset($_POST["add_ws_finish"]) || isset($_POST["add_ws_cancel"])){
311       $this->show_ws_dialog= FALSE;
312       $this->dialog= FALSE;
313     }
315     /* Templates now! */
316     $smarty= get_smarty();
318     /* Show ws dialog */
319     if ($this->show_ws_dialog){
320       /* Save data */
321       $sysfilter= get_global("sysfilter");
322       foreach( array("depselect", "regex") as $type){
323         if (isset($_POST[$type])){
324           $sysfilter[$type]= $_POST[$type];
325         }
326       }
327       if (isset($_GET['search'])){
328         $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
329         if ($s == "**"){
330           $s= "*";
331         }
332         $sysfilter['regex']= $s;
333       }
334       register_global("sysfilter", $sysfilter);
336       /* Get workstation list */
337       $exclude= "";
338       foreach($this->accessTo as $ws){
339         $exclude.= "(cn=$ws)";
340       }
341       if ($exclude != ""){
342         $exclude= "(!(|$exclude))";
343       }
344       $acl= array($this->config->current['BASE'] => ":all");
345       $regex= $sysfilter['regex'];
346       $filter= "(&(|(objectClass=goServer)(objectClass=gotoWorkstation)(objectClass=gotoTerminal))$exclude(cn=*)(cn=$regex))";
347       $res= get_list($acl, "$filter", TRUE, $sysfilter['depselect'], array("cn"), TRUE);
348       $wslist= array();
349       foreach ($res as $attrs){
350         $wslist[]= preg_replace('/\$/', '', $attrs['cn'][0]);
351       }
352       asort($wslist);
353       $smarty->assign("search_image", get_template_path('images/search.png'));
354       $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
355       $smarty->assign("tree_image", get_template_path('images/tree.png'));
356       $smarty->assign("deplist", $this->config->idepartments);
357       $smarty->assign("alphabet", generate_alphabet());
358       foreach( array("depselect", "regex") as $type){
359         $smarty->assign("$type", $sysfilter[$type]);
360       }
361       $smarty->assign("hint", print_sizelimit_warning());
362       $smarty->assign("wslist", $wslist);
363       $smarty->assign("apply", apply_filter());
364       $display= $smarty->fetch (get_template_path('trust_machines.tpl', TRUE, dirname(__FILE__)));
365       return ($display);
366     }
368     /* Manage group add dialog */
369     if ($this->group_dialog){
370       $gd= new groupManagement($this->config, get_userinfo());
372       /* Save data */
373       $groupfilter= get_global("groupfilter");
374       foreach( array("depselect", "guser", "regex") as $type){
375         if (isset($_POST[$type])){
376           $groupfilter[$type]= $_POST[$type];
377         }
378       }
379       if (isset($_POST['depselect'])){
380         foreach( array("primarygroups", "sambagroups", "mailgroups", "appgroups",
381               "functionalgroups") as $type){
383           if (isset($_POST[$type])) {
384             $groupfilter[$type]= "checked";
385           } else {
386             $groupfilter[$type]= "";
387           }
388         }
389       }
390       if (isset($_GET['search'])){
391         $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
392         if ($s == "**"){
393           $s= "*";
394         }
395         $groupfilter['regex']= $s;
396       }
397       register_global("groupfilter", $groupfilter);
399       /* Calculate actual groups */
400       $gd->reload2();
401       $glist= array();
402       foreach ($gd->grouplist as $key => $value){
403         if (!isset($this->groupMembership[$key])){
404           $glist[$key]= $value;
405         }
406       }
408       $smarty->assign("groups", $glist);
409       $smarty->assign("search_image", get_template_path('images/search.png'));
410       $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
411       $smarty->assign("tree_image", get_template_path('images/tree.png'));
412       $smarty->assign("deplist", $this->config->idepartments);
413       $smarty->assign("alphabet", generate_alphabet());
414       foreach( array("depselect", "guser", "regex", "primarygroups", "mailgroups",
415             "appgroups", "sambagroups", "functionalgroups") as $type){
416         $smarty->assign("$type", $groupfilter[$type]);
417       }
418       $smarty->assign("hint", print_sizelimit_warning());
420       $smarty->assign("apply", apply_filter());
421       $display.= $smarty->fetch (get_template_path('posix_groups.tpl', TRUE, dirname(__FILE__)));
422       return ($display);
423     }
425     /* Show main page */
426     $smarty= get_smarty();
428     /* Depending on pwmode, currently hardcoded because there are no other methods */
429     if ( 1 == 1 ){
430       $smarty->assign("pwmode", dirname(__FILE__)."/posix_shadow");
431       $shadowMinACL= chkacl($this->acl, "shadowMin");
432       $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."\">"));
433       $shadowMaxACL= chkacl($this->acl, "shadowMax");
434       $smarty->assign("shadowmaxs", sprintf(_("Password must be changed after %s days"), "<input name=\"shadowMax\" size=3 maxlength=4 $shadowMaxACL value=\"".$this->shadowMax."\">"));
435       $shadowInactiveACL= chkacl($this->acl, "shadowInactive");
436       $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."\">"));
437       $shadowWarningACL= chkacl($this->acl, "shadowWarning");
438       $smarty->assign("shadowwarnings", sprintf(_("Warn user %s days before password expiery"), "<input name=\"shadowWarning\" size=3 maxlength=4 $shadowWarningACL value=\"".$this->shadowWarning."\">"));
439       foreach( array("must_change_password", "use_shadowMin", "use_shadowMax",
440             "use_shadowExpire", "use_shadowInactive",
441             "use_shadowWarning") as $val){
442         if ($this->$val == 1){
443           $smarty->assign("$val", "checked");
444         } else {
445           $smarty->assign("$val", "");
446         }
447         $smarty->assign("$val"."ACL", chkacl($this->acl, $val));
448       }
449     }
451     /* Fill calendar */
452     $date= getdate($this->shadowExpire);
454     $days= array();
455     for($d= 1; $d<32; $d++){
456       $days[$d]= $d;
457     }
458     $years= array();
459     for($y= $date['year']-10; $y<$date['year']+10; $y++){
460       $years[]= $y;
461     }
462     $months= array(_("January"), _("February"), _("March"), _("April"),
463         _("May"), _("June"), _("July"), _("August"), _("September"),
464         _("October"), _("November"), _("December"));
465     $smarty->assign("day", $date["mday"]);
466     $smarty->assign("days", $days);
467     $smarty->assign("months", $months);
468     $smarty->assign("month", $date["mon"]-1);
469     $smarty->assign("years", $years);
470     $smarty->assign("year", $date["year"]);
472     /* Fill arrays */
473     $smarty->assign("shells", $this->loginShellList);
474     $smarty->assign("secondaryGroups", $this->secondaryGroups);
475     $smarty->assign("primaryGroup", $this->primaryGroup);
476     if (!count($this->groupMembership)){
477       $smarty->assign("groupMembership", array("&nbsp;"));
478     } else {
479       $smarty->assign("groupMembership", $this->groupMembership);
480     }
481     if (count($this->groupMembership) > 16){
482       $smarty->assign("groups", "too_many_for_nfs");
483     } else {
484       $smarty->assign("groups", "");
485     }
486     $smarty->assign("printerList", $this->printerList);
487     $smarty->assign("languages", $this->config->data['MAIN']['LANGUAGES']);
489     /* Checkboxes */
490     if ($this->force_ids == 1){
491       $smarty->assign("force_ids", "checked");
492       if ($_SESSION['js']){
493         $smarty->assign("forceMode", "");
494       }
495     } else {
496       if ($_SESSION['js']){
497         $smarty->assign("forceMode", "disabled");
498       }
499       $smarty->assign("force_ids", "");
500     }
501     $smarty->assign("force_idsACL", chkacl($this->acl, "force_ids"));
503     /* Load attributes and acl's */
504     foreach($this->attributes as $val){
505       if(($_SESSION["js"])&&(($val=="uidNumber")||($val=="gidNumber")))
506         {
507           $smarty->assign("$val"."ACL","");
508           $smarty->assign("$val", $this->$val);
509           continue;
510         }
511       $smarty->assign("$val", $this->$val);
512       $smarty->assign("$val"."ACL", chkacl($this->acl,$val));
513     }
514     $smarty->assign("groupMembershipACL", chkacl($this->acl, "groupMembership"));
515     $smarty->assign("status", $this->status);
517     /* Work on trust modes */
518     $smarty->assign("trustmodeACL", chkacl($this->acl, "trustmode"));
519     if ($this->trustModel == "fullaccess"){
520       $trustmode= 1;
521       // pervent double disable tag in html code, this will disturb our clean w3c html
522     
523     if(chkacl($this->acl, "trustmode")==""){
524           $smarty->assign("trusthide", "disabled");
525       }else{
526           $smarty->assign("trusthide", "");
527       }
529     } elseif ($this->trustModel == "byhost"){
530       $trustmode= 2;
531       $smarty->assign("trusthide", "");
532     } else {
533       // pervent double disable tag in html code, this will disturb our clean w3c html
534       if(chkacl($this->acl, "trustmode")==""){
535           $smarty->assign("trusthide", "disabled");
536       }else{
537           $smarty->assign("trusthide", "");
538       }
539       $trustmode= 0;
540     }
541     $smarty->assign("trustmode", $trustmode);
542     $smarty->assign("trustmodes", array( 0 => _("disabled"), 1 => _("full access"),
543           2 => _("allow access to these hosts")));
547     if((count($this->accessTo))==0)
548       $smarty->assign("emptyArrAccess",true);
549     else
550       $smarty->assign("emptyArrAccess",false);
551     
554     $smarty->assign("workstations", $this->accessTo);
556     $smarty->assign("apply", apply_filter());
557     $display.= $smarty->fetch (get_template_path('generic.tpl', TRUE, dirname(__FILE__)));
558     return($display);
559   }
562   /* remove object from parent */
563   function remove_from_parent()
564   {
565     /* Cancel if there's nothing to do here */
566     if (!$this->initially_was_account){
567       return;
568     }
569     
570     /* include global link_info */
571     $ldap= $this->config->get_ldap_link();
573     /* Remove and write to LDAP */
574     plugin::remove_from_parent();
576     /* Zero out array */
577     $this->attrs['gosaHostACL']= array();
579     /* Keep uid, because we need it for authentification! */
580     unset($this->attrs['uid']);
582     @DEBUG (DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__,
583         $this->attributes, "Save");
584     $ldap->cd($this->dn);
585     $ldap->modify($this->attrs);
586     show_ldap_error($ldap->get_error());
588     /* Delete group only if cn is uid and there are no other
589        members inside */
590     $ldap->cd ($this->config->current['BASE']);
591     $ldap->search ("(&(objectClass=posixGroup)(gidNumber=".$this->gidNumber."))", array("cn", "memberUid"));
592     if ($ldap->count() != 0){
593       $attrs= $ldap->fetch();
594       if ($attrs['cn'][0] == $this->uid &&
595           !isset($this->attrs['memberUid'])){
597         $ldap->rmDir($ldap->getDN());
598       }
599     }
601     /* Optionally execute a command after we're done */
602     $this->handle_post_events("remove");
603   }
606   function save_object()
607   {
608     if (isset($_POST['posixTab'])){
609       /* Save values to object */
610       plugin::save_object();
612       /* Save force GID attribute */
613       if (chkacl ($this->acl, "force_ids") == ""){
614         if (isset ($_POST['force_ids'])){
615           $data= 1;
616         } else {
617           $data= 0;
618         }
619         if ($this->force_ids != $data){
620           $this->is_modified= TRUE;
621         }
622         $this->force_ids= $data;
624         $data= $_POST['primaryGroup'];
625         if ($this->primaryGroup != $data){
626           $this->is_modified= TRUE;
627         }
628         $this->primaryGroup= $_POST['primaryGroup'];
629       }
631       /* Save pwmode dependent attributes, curently hardcoded because there're
632          no alternatives */
633       if (1 == 1){
634         foreach( array("must_change_password", "use_shadowMin", "use_shadowMax",
635               "use_shadowExpire", "use_shadowInactive",
636               "use_shadowWarning") as $val){
637           if (chkacl($this->acl, "$val") == ""){
638             if (isset ($_POST[$val])){
639               $data= 1;
640             } else {
641               $data= 0;
642             }
643             if ($data != $this->$val){
644               $this->is_modified= TRUE;
645             }
646             $this->$val= $data;
647           }
648         }
649       }
651       /* Trust mode - special handling */
652       if (isset($_POST['trustmode'])){
653         $saved= $this->trustModel;
654         if ($_POST['trustmode'] == "1"){
655           $this->trustModel= "fullaccess";
656         } elseif ($_POST['trustmode'] == "2"){
657           $this->trustModel= "byhost";
658         } else {
659           $this->trustModel= "";
660         }
661         if ($this->trustModel != $saved){
662           $this->is_modified= TRUE;
663         }
664       }
665     }
666   }
669   /* Save data to LDAP, depending on is_account we save or delete */
670   function save()
671   {
672         
673     /* include global link_info */
674     $ldap= $this->config->get_ldap_link();
676     /* Adapt shadow values */
677     if (!$this->use_shadowExpire){
678       $this->shadowExpire= "0";
679     } else {
680       /* Transform seconds to days here */
681       $this->shadowExpire= (int)($this->shadowExpire / (60 * 60 * 24)) + 1;
682     }
683     if (!$this->use_shadowMax){
684       $this->shadowMax= "0";
685     }
686     if ($this->must_change_password){
687       $this->shadowLastChange= (int)(date("U") / 86400) - $this->shadowMax - 1;
688     } else {
689       $this->shadowLastChange= (int)(date("U") / 86400);
690     }
691     if (!$this->use_shadowWarning){
692       $this->shadowWarning= "0";
693     }
695     /* Check what to do with ID's */
696     if ($this->force_ids == 0){
698       /* Use id's that are already set */
699       if ($this->savedUidNumber != ""){
700         $this->uidNumber= $this->savedUidNumber;
701         $this->gidNumber= $this->savedGidNumber;
702       } else {
704         /* Calculate new id's. We need to place a lock before calling get_next_id
705            to get real unique values. */
706         $wait= 10;
707         while (get_lock("uidnumber") != ""){
708           sleep (1);
710           /* Oups - timed out */
711           if ($wait-- == 0){
712             print_red (_("Failed: overriding lock"));
713             break;
714           }
715         }
717         add_lock ("uidnumber", "gosa");
718         $this->uidNumber= $this->get_next_id("uidNumber");
719         if ($this->savedGidNumber != ""){
720           $this->gidNumber= $this->savedGidNumber;
721         } else {
722           $this->gidNumber= $this->get_next_id("gidNumber");
723         }
724       }
726       if ($this->primaryGroup != 0){
727         $this->gidNumber= $this->primaryGroup;
728       }
729     }
731     if ($this->use_shadowMin != "1" ) {
732       $this->shadowMin = "";
733     }
735     if (($this->use_shadowMax != "1") && ($this->must_change_password != "1")) {
736       $this->shadowMax = "";
737     }
739     if ($this->use_shadowWarning != "1" ) {
740       $this->shadowWarning = "";
741     }
743     if ($this->use_shadowInactive != "1" ) {
744       $this->shadowInactive = "";
745     }
747     if ($this->use_shadowExpire != "1" ) {
748       $this->shadowExpire = "";
749     }
751     /* Fill gecos */
752     if (isset($this->parent) && $this->parent != NULL){
753       $this->gecos= rewrite($this->parent->by_object['user']->cn);
754           if (!preg_match('/[a-z0-9 -]/i', $this->gecos)){
755                   $this->gecos= "";
756           }
757     }
759     /* Call parents save to prepare $this->attrs */
760     plugin::save();
762     /* Trust accounts */
763     $objectclasses= array();
764     foreach ($this->attrs['objectClass'] as $key => $class){
765       if (preg_match('/trustAccount/i', $class)){
766         continue;
767       }
768       $objectclasses[]= $this->attrs['objectClass'][$key];
769     }
770     $this->attrs['objectClass']= $objectclasses;
771     if ($this->trustModel != ""){
772       $this->attrs['objectClass'][]= "trustAccount";
773       $this->attrs['trustModel']= $this->trustModel;
774       $this->attrs['accessTo']= array();
775       if ($this->trustModel == "byhost"){
776         foreach ($this->accessTo as $host){
777           $this->attrs['accessTo'][]= $host;
778         }
779       }
780     } else {
781       if ($this->was_trust_account){
782         $this->attrs['accessTo']= array();
783         $this->attrs['trustModel']= array();
784       }
785     }
787     if(empty($this->attrs['gosaDefaultPrinter'])){
788       $thid->attrs['gosaDefaultPrinter']=array();
789     }
791         foreach(array("shadowMin","shadowMax","shadowWarning","shadowInactive") as $attr){
792                 $this->attrs[$attr] = (int) $this->attrs[$attr];
793         }
795     /* Save data to LDAP */
796     $ldap->cd($this->dn);
797     $ldap->modify($this->attrs);
798     show_ldap_error($ldap->get_error());
800     /* Remove lock needed for unique id generation */
801     del_lock ("uidnumber");
804     /* Posix accounts have group interrelationship, take care about these here. */
805     if ($this->force_ids == 0 && $this->primaryGroup == 0){
806       $ldap->cd($this->config->current['BASE']);
807       $ldap->search("(&(objectClass=posixGroup)(gidNumber=".$this->gidNumber."))", array("cn"));
809       /* Create group if it doesn't exist */
810       if ($ldap->count() == 0){
811         $groupdn= preg_replace ('/^'.$this->config->current['DNMODE'].'=[^,]+,'.get_people_ou().'/i', 'cn='.$this->uid.','.get_groups_ou(), $this->dn);
813         $g= new group($this->config, $groupdn);
814         $g->cn= $this->uid;
815         $g->force_gid= 1;
816         $g->gidNumber= $this->gidNumber;
817         $g->description= "Group of user ".$this->givenName." ".$this->sn;
818         $g->save ();
819       }
820     }
822     /* Take care about groupMembership values: add to groups */
823     foreach ($this->groupMembership as $key => $value){
824       $g= new group($this->config, $key);
825       $g->addUser ($this->uid);
826       $g->save();
828       /* May need to save the mail part, too */
829       if ($g->has_mailAccount){
830         $m= new mailgroup($this->config, $key);
831         $m->save();
832       }
833     }
835     /* Remove from groups not listed in groupMembership */
836     foreach ($this->savedGroupMembership as $key => $value){
837       if (!array_key_exists ($key, $this->groupMembership)){
838         $g= new group($this->config, $key);
839         $g->removeUser ($this->uid);
840         $g->save();
842         /* May need to save the mail part, too */
843         if ($g->has_mailAccount){
844           $m= new mailgroup($this->config, $key);
845           $m->save();
846         }
847       }
848     }
850     /* Optionally execute a command after we're done */
851     if ($this->initially_was_account == $this->is_account){
852       if ($this->is_modified){
853         $this->handle_post_events("mofify");
854       }
855     } else {
856       $this->handle_post_events("add");
857     }
858   }
860   /* Check formular input */
861   function check()
862   {
863     /* Include global link_info */
864     $ldap= $this->config->get_ldap_link();
866     /* Reset message array */
867     $message= array();
869     /* must: homeDirectory */
870     if ($this->homeDirectory == ""){
871       $message[]= _("The required field 'Home directory' is not set.");
872     }
873     if (!is_path($this->homeDirectory)){
874       $message[]= _("Please enter a valid path in 'Home directory' field.");
875     }
877     /* Check ID's if they are forced by user */
878     if ($this->force_ids == "1"){
880       /* Valid uid/gid? */
881       if (!is_id($this->uidNumber)){
882         $message[]= _("Value specified as 'UID' is not valid.");
883       } else {
884         if ($this->uidNumber < $this->config->current['MINID']){
885           $message[]= _("Value specified as 'UID' is too small.");
886         }
887       }
888       if (!is_id($this->gidNumber)){
889         $message[]= _("Value specified as 'GID' is not valid.");
890       } else {
891         if ($this->gidNumber < $this->config->current['MINID']){
892           $message[]= _("Value specified as 'GID' is too small.");
893         }
894       }
895     }
897     /* Check shadow settings, well I like spaghetties... */
898     if ($this->use_shadowMin){
899       if (!is_id($this->shadowMin)){
900         $message[]= _("Value specified as 'shadowMin' is not valid.");
901       }
902     }
903     if ($this->use_shadowMax){
904       if (!is_id($this->shadowMax)){
905         $message[]= _("Value specified as 'shadowMax' is not valid.");
906       }
907     }
908     if ($this->use_shadowWarning){
909       if (!is_id($this->shadowWarning)){
910         $message[]= _("Value specified as 'shadowWarning' is not valid.");
911       }
912       if (!$this->use_shadowMax){
913         $message[]= _("'shadowWarning' without 'shadowMax' makes no sense.");
914       }
915       if ($this->shadowWarning > $this->shadowMax){
916         $message[]= _("Value specified as 'shadowWarning' should be smaller than 'shadowMax'.");
917       }
918       if ($this->use_shadowMin && $this->shadowWarning < $this->shadowMin){
919         $message[]= _("Value specified as 'shadowWarning' should be greater than 'shadowMin'.");
920       }
921     }
922     if ($this->use_shadowInactive){
923       if (!is_id($this->shadowInactive)){
924         $message[]= _("Value specified as 'shadowInactive' is not valid.");
925       }
926       if (!$this->use_shadowMax){
927         $message[]= _("'shadowInactive' without 'shadowMax' makes no sense.");
928       }
929     }
930     if ($this->use_shadowMin && $this->use_shadowMax){
931       if ($this->shadowMin > $this->shadowMax){
932         $message[]= _("Value specified as 'shadowMin' should be smaller than 'shadowMax'.");
933       }
934     }
936   //  if(empty($this->gosaDefaultPrinter)){
937   //    $message[]= _("You need to specify a valid default printer.");
938   //  }
940     return ($message);
941   }
943   function addGroup ($groups)
944   {
945     /* include global link_info */
946     $ldap= $this->config->get_ldap_link();
948     /* Walk through groups and add the descriptive entry if not exists */
949     foreach ($groups as $value){
950       if (!array_key_exists($value, $this->groupMembership)){
951         $ldap->cat($value);
952         $attrs= $ldap->fetch();
953         error_reporting (0);
954         if (!isset($attrs['description'][0])){
955           $entry= $attrs["cn"][0];
956         } else {
957           $dsc= preg_replace ('/^Group of user/', _("Group of user"), $attrs["description"][0]);
958           $entry= $attrs["cn"][0]." [$dsc]";
959         }
960         error_reporting (E_ALL);
961         $this->groupMembership[$ldap->getDN()]= $entry;
962       }
963     }
965     /* Sort groups */
966     asort ($this->groupMembership);
967     reset ($this->groupMembership);
968   }
971   /* Del posix user from some groups */
972   function delGroup ($groups)
973   {
974     $dest= array();
976     foreach ($this->groupMembership as $key => $value){
977       if (!in_array($key, $groups)){
978         $dest[$key]= $value;
979       }
980     }
981     $this->groupMembership= $dest;
982   }
984   /* Adapt from template, using 'dn' */
985   function adapt_from_template($dn)
986   {
987     /* Include global link_info */
988     $ldap= $this->config->get_ldap_link();
990     plugin::adapt_from_template($dn);
991     $template= $this->attrs['uid'][0];
993     /* Adapt group membership */
994     $ldap->cd($this->config->current['BASE']);
995     $ldap->search("(&(objectClass=posixGroup)(memberUid=".$this->attrs["uid"][0]."))", array("description", "cn"));
997     while ($this->attrs= $ldap->fetch()){
998       if (!isset($this->attrs["description"][0])){
999         $entry= $this->attrs["cn"][0];
1000       } else {
1001         $entry= $this->attrs["cn"][0]." [".$this->attrs["description"][0]."]";
1002       }
1003       $this->groupMembership[$ldap->getDN()]= $entry;
1004     }
1006     /* Fix primary group settings */
1007     $ldap->cd($this->config->current['BASE']);
1008     $ldap->search("(&(objectClass=posixGroup)(cn=$template)(gidNumber=".$this->gidNumber."))", array("cn"));
1009     if ($ldap->count() != 1){
1010       $this->primaryGroup= $this->gidNumber;
1011     }
1012   }
1014   function get_next_id($attrib)
1015   {
1016     $ids= array();
1017     $ldap= $this->config->get_ldap_link();
1019     $ldap->cd ($this->config->current['BASE']);
1020     $ldap->search ("($attrib=*)", array("$attrib"));
1022     /* Get list of ids */
1023     while ($attrs= $ldap->fetch()){
1024       $ids[]= (int)$attrs["$attrib"][0];
1025     }
1027     /* Find out next free id near to UID_BASE */
1028     for ($id= $this->config->current['UIDBASE']; $id++; $id<65000){
1029       if (!in_array($id, $ids)){
1030         return ($id);
1031       }
1032     }
1034     /* Should not happen */
1035     if ($id == 65000){
1036       print_red(_("Too many users, can't allocate a free ID!"));
1037       exit;
1038     }
1040   }
1046 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1052 ?>