Code

Some fixes, with posix tab
[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     /* include global link_info */
673     $ldap= $this->config->get_ldap_link();
675     /* Adapt shadow values */
676     if (!$this->use_shadowExpire){
677       $this->shadowExpire= "0";
678     } else {
679       /* Transform seconds to days here */
680       $this->shadowExpire= (int)($this->shadowExpire / (60 * 60 * 24)) + 1;
681     }
682     if (!$this->use_shadowMax){
683       $this->shadowMax= "0";
684     }
685     if ($this->must_change_password){
686       $this->shadowLastChange= (int)(date("U") / 86400) - $this->shadowMax - 1;
687     } else {
688       $this->shadowLastChange= (int)(date("U") / 86400);
689     }
690     if (!$this->use_shadowWarning){
691       $this->shadowWarning= "0";
692     }
694     /* Check what to do with ID's */
695     if ($this->force_ids == 0){
697       /* Use id's that are already set */
698       if ($this->savedUidNumber != ""){
699         $this->uidNumber= $this->savedUidNumber;
700         $this->gidNumber= $this->savedGidNumber;
701       } else {
703         /* Calculate new id's. We need to place a lock before calling get_next_id
704            to get real unique values. */
705         $wait= 10;
706         while (get_lock("uidnumber") != ""){
707           sleep (1);
709           /* Oups - timed out */
710           if ($wait-- == 0){
711             print_red (_("Failed: overriding lock"));
712             break;
713           }
714         }
716         add_lock ("uidnumber", "gosa");
717         $this->uidNumber= $this->get_next_id("uidNumber");
718         if ($this->savedGidNumber != ""){
719           $this->gidNumber= $this->savedGidNumber;
720         } else {
721           $this->gidNumber= $this->get_next_id("gidNumber");
722         }
723       }
725       if ($this->primaryGroup != 0){
726         $this->gidNumber= $this->primaryGroup;
727       }
728     }
730     if ($this->use_shadowMin != "1" ) {
731       $this->shadowMin = "";
732     }
734     if (($this->use_shadowMax != "1") && ($this->must_change_password != "1")) {
735       $this->shadowMax = "";
736     }
738     if ($this->use_shadowWarning != "1" ) {
739       $this->shadowWarning = "";
740     }
742     if ($this->use_shadowInactive != "1" ) {
743       $this->shadowInactive = "";
744     }
746     if ($this->use_shadowExpire != "1" ) {
747       $this->shadowExpire = "";
748     }
750     /* Fill gecos */
751     if (isset($this->parent) && $this->parent != NULL){
752       $this->gecos= rewrite($this->parent->by_object['user']->cn);
753           if (!preg_match('/[a-z0-9 -]/i', $this->gecos)){
754                   $this->gecos= "";
755           }
756     }
758     /* Call parents save to prepare $this->attrs */
759     plugin::save();
761     /* Trust accounts */
762     $objectclasses= array();
763     foreach ($this->attrs['objectClass'] as $key => $class){
764       if (preg_match('/trustAccount/i', $class)){
765         continue;
766       }
767       $objectclasses[]= $this->attrs['objectClass'][$key];
768     }
769     $this->attrs['objectClass']= $objectclasses;
770     if ($this->trustModel != ""){
771       $this->attrs['objectClass'][]= "trustAccount";
772       $this->attrs['trustModel']= $this->trustModel;
773       $this->attrs['accessTo']= array();
774       if ($this->trustModel == "byhost"){
775         foreach ($this->accessTo as $host){
776           $this->attrs['accessTo'][]= $host;
777         }
778       }
779     } else {
780       if ($this->was_trust_account){
781         $this->attrs['accessTo']= array();
782         $this->attrs['trustModel']= array();
783       }
784     }
786     if(empty($this->attrs['gosaDefaultPrinter'])){
787       $thid->attrs['gosaDefaultPrinter']=array();
788     }
790     /* Save data to LDAP */
791     $ldap->cd($this->dn);
792     $ldap->modify($this->attrs);
793     show_ldap_error($ldap->get_error());
795     /* Remove lock needed for unique id generation */
796     del_lock ("uidnumber");
799     /* Posix accounts have group interrelationship, take care about these here. */
800     if ($this->force_ids == 0 && $this->primaryGroup == 0){
801       $ldap->cd($this->config->current['BASE']);
802       $ldap->search("(&(objectClass=posixGroup)(gidNumber=".$this->gidNumber."))", array("cn"));
804       /* Create group if it doesn't exist */
805       if ($ldap->count() == 0){
806         $groupdn= preg_replace ('/^'.$this->config->current['DNMODE'].'=[^,]+,'.get_people_ou().'/i', 'cn='.$this->uid.','.get_groups_ou(), $this->dn);
808         $g= new group($this->config, $groupdn);
809         $g->cn= $this->uid;
810         $g->force_gid= 1;
811         $g->gidNumber= $this->gidNumber;
812         $g->description= "Group of user ".$this->givenName." ".$this->sn;
813         $g->save ();
814       }
815     }
817     /* Take care about groupMembership values: add to groups */
818     foreach ($this->groupMembership as $key => $value){
819       $g= new group($this->config, $key);
820       $g->addUser ($this->uid);
821       $g->save();
823       /* May need to save the mail part, too */
824       if ($g->has_mailAccount){
825         $m= new mailgroup($this->config, $key);
826         $m->save();
827       }
828     }
830     /* Remove from groups not listed in groupMembership */
831     foreach ($this->savedGroupMembership as $key => $value){
832       if (!array_key_exists ($key, $this->groupMembership)){
833         $g= new group($this->config, $key);
834         $g->removeUser ($this->uid);
835         $g->save();
837         /* May need to save the mail part, too */
838         if ($g->has_mailAccount){
839           $m= new mailgroup($this->config, $key);
840           $m->save();
841         }
842       }
843     }
845     /* Optionally execute a command after we're done */
846     if ($this->initially_was_account == $this->is_account){
847       if ($this->is_modified){
848         $this->handle_post_events("mofify");
849       }
850     } else {
851       $this->handle_post_events("add");
852     }
853   }
855   /* Check formular input */
856   function check()
857   {
858     /* Include global link_info */
859     $ldap= $this->config->get_ldap_link();
861     /* Reset message array */
862     $message= array();
864     /* must: homeDirectory */
865     if ($this->homeDirectory == ""){
866       $message[]= _("The required field 'Home directory' is not set.");
867     }
868     if (!is_path($this->homeDirectory)){
869       $message[]= _("Please enter a valid path in 'Home directory' field.");
870     }
872     /* Check ID's if they are forced by user */
873     if ($this->force_ids == "1"){
875       /* Valid uid/gid? */
876       if (!is_id($this->uidNumber)){
877         $message[]= _("Value specified as 'UID' is not valid.");
878       } else {
879         if ($this->uidNumber < $this->config->current['MINID']){
880           $message[]= _("Value specified as 'UID' is too small.");
881         }
882       }
883       if (!is_id($this->gidNumber)){
884         $message[]= _("Value specified as 'GID' is not valid.");
885       } else {
886         if ($this->gidNumber < $this->config->current['MINID']){
887           $message[]= _("Value specified as 'GID' is too small.");
888         }
889       }
890     }
892     /* Check shadow settings, well I like spaghetties... */
893     if ($this->use_shadowMin){
894       if (!is_id($this->shadowMin)){
895         $message[]= _("Value specified as 'shadowMin' is not valid.");
896       }
897     }
898     if ($this->use_shadowMax){
899       if (!is_id($this->shadowMax)){
900         $message[]= _("Value specified as 'shadowMax' is not valid.");
901       }
902     }
903     if ($this->use_shadowWarning){
904       if (!is_id($this->shadowWarning)){
905         $message[]= _("Value specified as 'shadowWarning' is not valid.");
906       }
907       if (!$this->use_shadowMax){
908         $message[]= _("'shadowWarning' without 'shadowMax' makes no sense.");
909       }
910       if ($this->shadowWarning > $this->shadowMax){
911         $message[]= _("Value specified as 'shadowWarning' should be smaller than 'shadowMax'.");
912       }
913       if ($this->use_shadowMin && $this->shadowWarning < $this->shadowMin){
914         $message[]= _("Value specified as 'shadowWarning' should be greater than 'shadowMin'.");
915       }
916     }
917     if ($this->use_shadowInactive){
918       if (!is_id($this->shadowInactive)){
919         $message[]= _("Value specified as 'shadowInactive' is not valid.");
920       }
921       if (!$this->use_shadowMax){
922         $message[]= _("'shadowInactive' without 'shadowMax' makes no sense.");
923       }
924     }
925     if ($this->use_shadowMin && $this->use_shadowMax){
926       if ($this->shadowMin > $this->shadowMax){
927         $message[]= _("Value specified as 'shadowMin' should be smaller than 'shadowMax'.");
928       }
929     }
931   //  if(empty($this->gosaDefaultPrinter)){
932   //    $message[]= _("You need to specify a valid default printer.");
933   //  }
935     return ($message);
936   }
938   function addGroup ($groups)
939   {
940     /* include global link_info */
941     $ldap= $this->config->get_ldap_link();
943     /* Walk through groups and add the descriptive entry if not exists */
944     foreach ($groups as $value){
945       if (!array_key_exists($value, $this->groupMembership)){
946         $ldap->cat($value);
947         $attrs= $ldap->fetch();
948         error_reporting (0);
949         if (!isset($attrs['description'][0])){
950           $entry= $attrs["cn"][0];
951         } else {
952           $dsc= preg_replace ('/^Group of user/', _("Group of user"), $attrs["description"][0]);
953           $entry= $attrs["cn"][0]." [$dsc]";
954         }
955         error_reporting (E_ALL);
956         $this->groupMembership[$ldap->getDN()]= $entry;
957       }
958     }
960     /* Sort groups */
961     asort ($this->groupMembership);
962     reset ($this->groupMembership);
963   }
966   /* Del posix user from some groups */
967   function delGroup ($groups)
968   {
969     $dest= array();
971     foreach ($this->groupMembership as $key => $value){
972       if (!in_array($key, $groups)){
973         $dest[$key]= $value;
974       }
975     }
976     $this->groupMembership= $dest;
977   }
979   /* Adapt from template, using 'dn' */
980   function adapt_from_template($dn)
981   {
982     /* Include global link_info */
983     $ldap= $this->config->get_ldap_link();
985     plugin::adapt_from_template($dn);
986     $template= $this->attrs['uid'][0];
988     /* Adapt group membership */
989     $ldap->cd($this->config->current['BASE']);
990     $ldap->search("(&(objectClass=posixGroup)(memberUid=".$this->attrs["uid"][0]."))", array("description", "cn"));
992     while ($this->attrs= $ldap->fetch()){
993       if (!isset($this->attrs["description"][0])){
994         $entry= $this->attrs["cn"][0];
995       } else {
996         $entry= $this->attrs["cn"][0]." [".$this->attrs["description"][0]."]";
997       }
998       $this->groupMembership[$ldap->getDN()]= $entry;
999     }
1001     /* Fix primary group settings */
1002     $ldap->cd($this->config->current['BASE']);
1003     $ldap->search("(&(objectClass=posixGroup)(cn=$template)(gidNumber=".$this->gidNumber."))", array("cn"));
1004     if ($ldap->count() != 1){
1005       $this->primaryGroup= $this->gidNumber;
1006     }
1007   }
1009   function get_next_id($attrib)
1010   {
1011     $ids= array();
1012     $ldap= $this->config->get_ldap_link();
1014     $ldap->cd ($this->config->current['BASE']);
1015     $ldap->search ("($attrib=*)", array("$attrib"));
1017     /* Get list of ids */
1018     while ($attrs= $ldap->fetch()){
1019       $ids[]= (int)$attrs["$attrib"][0];
1020     }
1022     /* Find out next free id near to UID_BASE */
1023     for ($id= $this->config->current['UIDBASE']; $id++; $id<65000){
1024       if (!in_array($id, $ids)){
1025         return ($id);
1026       }
1027     }
1029     /* Should not happen */
1030     if ($id == 65000){
1031       print_red(_("Too many users, can't allocate a free ID!"));
1032       exit;
1033     }
1035   }
1041 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1047 ?>