Code

12d537833164c42d3b1734b84183fabd00a47078
[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'])){
235           /* Samba3 dependency on posix accounts are enabled
236              in the moment, because I need to rely on unique
237              uidNumbers. There'll be a better solution later
238              on. */
239           $display= $this->show_header(_("Remove posix account"),
240               _("This account has unix features enabled. To disable them, you'll need to remove the samba account first."), TRUE);
241         } else {
242           $display= $this->show_header(_("Remove posix account"),
243               _("This account has posix features enabled. You can disable them by clicking below."));
244         }
245       } else {
246         $display= $this->show_header(_("Create posix account"),
247             _("This account has posix features disabled. You can enable them by clicking below."));
248         return($display);
249       }
250     }
252     /* Trigger group edit? */
253     if (isset($_POST['edit_groupmembership'])){
254       $this->group_dialog= TRUE;
255       $this->dialog= TRUE;
256     }
258     /* Cancel group edit? */
259     if (isset($_POST['add_groups_cancel']) ||
260         isset($_POST['add_groups_finish'])){
261       $this->group_dialog= FALSE;
262       $this->dialog= FALSE;
263     }
265     /* Add selected groups */
266     if (isset($_POST['add_groups_finish']) && isset($_POST['groups']) &&
267         count($_POST['groups'])){
269       if (chkacl ($this->acl, "memberUid") == ""){
270         $this->addGroup ($_POST['groups']);
271         $this->is_modified= TRUE;
272       }
273     }
275     /* Delete selected groups */
276     if (isset($_POST['delete_groupmembership']) && 
277         isset($_POST['group_list']) && count($_POST['group_list'])){
279       if (chkacl ($this->acl, "memberUid") == ""){
280         $this->delGroup ($_POST['group_list']);
281         $this->is_modified= TRUE;
282       }
283     }
285     /* Add user workstation? */
286     if (isset($_POST["add_ws"])){
287       $this->show_ws_dialog= TRUE;
288       $this->dialog= TRUE;
289     }
291     /* Add user workstation? */
292     if (isset($_POST["add_ws_finish"]) && isset($_POST['wslist'])){
293       foreach($_POST['wslist'] as $ws){
294         $this->accessTo[$ws]= $ws;
295       }
296       ksort($this->accessTo);
297       $this->is_modified= TRUE;
298     }
300     /* Remove user workstations? */
301     if (isset($_POST["delete_ws"]) && isset($_POST['workstation_list'])){
302       foreach($_POST['workstation_list'] as $name){
303         unset ($this->accessTo[$name]);
304       }
305       $this->is_modified= TRUE;
306     }
308     /* Add user workstation finished? */
309     if (isset($_POST["add_ws_finish"]) || isset($_POST["add_ws_cancel"])){
310       $this->show_ws_dialog= FALSE;
311       $this->dialog= FALSE;
312     }
314     /* Templates now! */
315     $smarty= get_smarty();
317     /* Show ws dialog */
318     if ($this->show_ws_dialog){
319       /* Save data */
320       $sysfilter= get_global("sysfilter");
321       foreach( array("depselect", "regex") as $type){
322         if (isset($_POST[$type])){
323           $sysfilter[$type]= $_POST[$type];
324         }
325       }
326       if (isset($_GET['search'])){
327         $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
328         if ($s == "**"){
329           $s= "*";
330         }
331         $sysfilter['regex']= $s;
332       }
333       register_global("sysfilter", $sysfilter);
335       /* Get workstation list */
336       $exclude= "";
337       foreach($this->accessTo as $ws){
338         $exclude.= "(cn=$ws)";
339       }
340       if ($exclude != ""){
341         $exclude= "(!(|$exclude))";
342       }
343       $acl= array($this->config->current['BASE'] => ":all");
344       $regex= $sysfilter['regex'];
345       $filter= "(&(|(objectClass=goServer)(objectClass=gotoWorkstation)(objectClass=gotoTerminal))$exclude(cn=*)(cn=$regex))";
346       $res= get_list($acl, "$filter", TRUE, $sysfilter['depselect'], array("cn"), TRUE);
347       $wslist= array();
348       foreach ($res as $attrs){
349         $wslist[]= preg_replace('/\$/', '', $attrs['cn'][0]);
350       }
351       asort($wslist);
352       $smarty->assign("search_image", get_template_path('images/search.png'));
353       $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
354       $smarty->assign("tree_image", get_template_path('images/tree.png'));
355       $smarty->assign("deplist", $this->config->idepartments);
356       $smarty->assign("alphabet", generate_alphabet());
357       foreach( array("depselect", "regex") as $type){
358         $smarty->assign("$type", $sysfilter[$type]);
359       }
360       $smarty->assign("hint", print_sizelimit_warning());
361       $smarty->assign("wslist", $wslist);
362       $smarty->assign("apply", apply_filter());
363       $display= $smarty->fetch (get_template_path('trust_machines.tpl', TRUE, dirname(__FILE__)));
364       return ($display);
365     }
367     /* Manage group add dialog */
368     if ($this->group_dialog){
369       $gd= new groupManagement($this->config, get_userinfo());
371       /* Save data */
372       $groupfilter= get_global("groupfilter");
373       foreach( array("depselect", "guser", "regex") as $type){
374         if (isset($_POST[$type])){
375           $groupfilter[$type]= $_POST[$type];
376         }
377       }
378       if (isset($_POST['depselect'])){
379         foreach( array("primarygroups", "sambagroups", "mailgroups", "appgroups",
380               "functionalgroups") as $type){
382           if (isset($_POST[$type])) {
383             $groupfilter[$type]= "checked";
384           } else {
385             $groupfilter[$type]= "";
386           }
387         }
388       }
389       if (isset($_GET['search'])){
390         $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
391         if ($s == "**"){
392           $s= "*";
393         }
394         $groupfilter['regex']= $s;
395       }
396       register_global("groupfilter", $groupfilter);
398       /* Calculate actual groups */
399       $gd->reload2();
400       $glist= array();
401       foreach ($gd->grouplist as $key => $value){
402         if (!isset($this->groupMembership[$key])){
403           $glist[$key]= $value;
404         }
405       }
407       $smarty->assign("groups", $glist);
408       $smarty->assign("search_image", get_template_path('images/search.png'));
409       $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
410       $smarty->assign("tree_image", get_template_path('images/tree.png'));
411       $smarty->assign("deplist", $this->config->idepartments);
412       $smarty->assign("alphabet", generate_alphabet());
413       foreach( array("depselect", "guser", "regex", "primarygroups", "mailgroups",
414             "appgroups", "sambagroups", "functionalgroups") as $type){
415         $smarty->assign("$type", $groupfilter[$type]);
416       }
417       $smarty->assign("hint", print_sizelimit_warning());
419       $smarty->assign("apply", apply_filter());
420       $display.= $smarty->fetch (get_template_path('posix_groups.tpl', TRUE, dirname(__FILE__)));
421       return ($display);
422     }
424     /* Show main page */
425     $smarty= get_smarty();
427     /* Depending on pwmode, currently hardcoded because there are no other methods */
428     if ( 1 == 1 ){
429       $smarty->assign("pwmode", dirname(__FILE__)."/posix_shadow");
430       $shadowMinACL= chkacl($this->acl, "shadowMin");
431       $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."\">"));
432       $shadowMaxACL= chkacl($this->acl, "shadowMax");
433       $smarty->assign("shadowmaxs", sprintf(_("Password must be changed after %s days"), "<input name=\"shadowMax\" size=3 maxlength=4 $shadowMaxACL value=\"".$this->shadowMax."\">"));
434       $shadowInactiveACL= chkacl($this->acl, "shadowInactive");
435       $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."\">"));
436       $shadowWarningACL= chkacl($this->acl, "shadowWarning");
437       $smarty->assign("shadowwarnings", sprintf(_("Warn user %s days before password expiery"), "<input name=\"shadowWarning\" size=3 maxlength=4 $shadowWarningACL value=\"".$this->shadowWarning."\">"));
438       foreach( array("must_change_password", "use_shadowMin", "use_shadowMax",
439             "use_shadowExpire", "use_shadowInactive",
440             "use_shadowWarning") as $val){
441         if ($this->$val == 1){
442           $smarty->assign("$val", "checked");
443         } else {
444           $smarty->assign("$val", "");
445         }
446         $smarty->assign("$val"."ACL", chkacl($this->acl, $val));
447       }
448     }
450     /* Fill calendar */
451     $date= getdate($this->shadowExpire);
453     $days= array();
454     for($d= 1; $d<32; $d++){
455       $days[$d]= $d;
456     }
457     $years= array();
458     for($y= $date['year']-10; $y<$date['year']+10; $y++){
459       $years[]= $y;
460     }
461     $months= array(_("January"), _("February"), _("March"), _("April"),
462         _("May"), _("June"), _("July"), _("August"), _("September"),
463         _("October"), _("November"), _("December"));
464     $smarty->assign("day", $date["mday"]);
465     $smarty->assign("days", $days);
466     $smarty->assign("months", $months);
467     $smarty->assign("month", $date["mon"]-1);
468     $smarty->assign("years", $years);
469     $smarty->assign("year", $date["year"]);
471     /* Fill arrays */
472     $smarty->assign("shells", $this->loginShellList);
473     $smarty->assign("secondaryGroups", $this->secondaryGroups);
474     $smarty->assign("primaryGroup", $this->primaryGroup);
475     if (!count($this->groupMembership)){
476       $smarty->assign("groupMembership", array("&nbsp;"));
477     } else {
478       $smarty->assign("groupMembership", $this->groupMembership);
479     }
480     if (count($this->groupMembership) > 16){
481       $smarty->assign("groups", "too_many_for_nfs");
482     } else {
483       $smarty->assign("groups", "");
484     }
485     $smarty->assign("printerList", $this->printerList);
486     $smarty->assign("languages", $this->config->data['MAIN']['LANGUAGES']);
488     /* Checkboxes */
489     if ($this->force_ids == 1){
490       $smarty->assign("force_ids", "checked");
491       if ($_SESSION['js']){
492         $smarty->assign("forceMode", "");
493       }
494     } else {
495       if ($_SESSION['js']){
496         $smarty->assign("forceMode", "disabled");
497       }
498       $smarty->assign("force_ids", "");
499     }
500     $smarty->assign("force_idsACL", chkacl($this->acl, "force_ids"));
502     /* Load attributes and acl's */
503     foreach($this->attributes as $val){
504       if(($_SESSION["js"])&&(($val=="uidNumber")||($val=="gidNumber")))
505         {
506           $smarty->assign("$val"."ACL","");
507           $smarty->assign("$val", $this->$val);
508           continue;
509         }
510       $smarty->assign("$val", $this->$val);
511       $smarty->assign("$val"."ACL", chkacl($this->acl,$val));
512     }
513     $smarty->assign("groupMembershipACL", chkacl($this->acl, "groupMembership"));
514     $smarty->assign("status", $this->status);
516     /* Work on trust modes */
517     $smarty->assign("trustmodeACL", chkacl($this->acl, "trustmode"));
518     if ($this->trustModel == "fullaccess"){
519       $trustmode= 1;
520       // pervent double disable tag in html code, this will disturb our clean w3c html
521     
522     if(chkacl($this->acl, "trustmode")==""){
523           $smarty->assign("trusthide", "disabled");
524       }else{
525           $smarty->assign("trusthide", "");
526       }
528     } elseif ($this->trustModel == "byhost"){
529       $trustmode= 2;
530       $smarty->assign("trusthide", "");
531     } else {
532       // pervent double disable tag in html code, this will disturb our clean w3c html
533       if(chkacl($this->acl, "trustmode")==""){
534           $smarty->assign("trusthide", "disabled");
535       }else{
536           $smarty->assign("trusthide", "");
537       }
538       $trustmode= 0;
539     }
540     $smarty->assign("trustmode", $trustmode);
541     $smarty->assign("trustmodes", array( 0 => _("disabled"), 1 => _("full access"),
542           2 => _("allow access to these hosts")));
546     if((count($this->accessTo))==0)
547       $smarty->assign("emptyArrAccess",true);
548     else
549       $smarty->assign("emptyArrAccess",false);
550     
553     $smarty->assign("workstations", $this->accessTo);
555     $smarty->assign("apply", apply_filter());
556     $display.= $smarty->fetch (get_template_path('generic.tpl', TRUE, dirname(__FILE__)));
557     return($display);
558   }
561   /* remove object from parent */
562   function remove_from_parent()
563   {
564     /* Cancel if there's nothing to do here */
565     if (!$this->initially_was_account){
566       return;
567     }
568     
569     /* include global link_info */
570     $ldap= $this->config->get_ldap_link();
572     /* Remove and write to LDAP */
573     plugin::remove_from_parent();
575     /* Zero out array */
576     $this->attrs['gosaHostACL']= array();
578     /* Keep uid, because we need it for authentification! */
579     unset($this->attrs['uid']);
581     @DEBUG (DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__,
582         $this->attributes, "Save");
583     $ldap->cd($this->dn);
584     $ldap->modify($this->attrs);
585     show_ldap_error($ldap->get_error());
587     /* Delete group only if cn is uid and there are no other
588        members inside */
589     $ldap->cd ($this->config->current['BASE']);
590     $ldap->search ("(&(objectClass=posixGroup)(gidNumber=".$this->gidNumber."))", array("cn", "memberUid"));
591     if ($ldap->count() != 0){
592       $attrs= $ldap->fetch();
593       if ($attrs['cn'][0] == $this->uid &&
594           !isset($this->attrs['memberUid'])){
596         $ldap->rmDir($ldap->getDN());
597       }
598     }
600     /* Optionally execute a command after we're done */
601     $this->handle_post_events("remove");
602   }
605   function save_object()
606   {
607     if (isset($_POST['posixTab'])){
608       /* Save values to object */
609       plugin::save_object();
611       /* Save force GID attribute */
612       if (chkacl ($this->acl, "force_ids") == ""){
613         if (isset ($_POST['force_ids'])){
614           $data= 1;
615         } else {
616           $data= 0;
617         }
618         if ($this->force_ids != $data){
619           $this->is_modified= TRUE;
620         }
621         $this->force_ids= $data;
623         $data= $_POST['primaryGroup'];
624         if ($this->primaryGroup != $data){
625           $this->is_modified= TRUE;
626         }
627         $this->primaryGroup= $_POST['primaryGroup'];
628       }
630       /* Save pwmode dependent attributes, curently hardcoded because there're
631          no alternatives */
632       if (1 == 1){
633         foreach( array("must_change_password", "use_shadowMin", "use_shadowMax",
634               "use_shadowExpire", "use_shadowInactive",
635               "use_shadowWarning") as $val){
636           if (chkacl($this->acl, "$val") == ""){
637             if (isset ($_POST[$val])){
638               $data= 1;
639             } else {
640               $data= 0;
641             }
642             if ($data != $this->$val){
643               $this->is_modified= TRUE;
644             }
645             $this->$val= $data;
646           }
647         }
648       }
650       /* Trust mode - special handling */
651       if (isset($_POST['trustmode'])){
652         $saved= $this->trustModel;
653         if ($_POST['trustmode'] == "1"){
654           $this->trustModel= "fullaccess";
655         } elseif ($_POST['trustmode'] == "2"){
656           $this->trustModel= "byhost";
657         } else {
658           $this->trustModel= "";
659         }
660         if ($this->trustModel != $saved){
661           $this->is_modified= TRUE;
662         }
663       }
664     }
665   }
668   /* Save data to LDAP, depending on is_account we save or delete */
669   function save()
670   {
671     /* include global link_info */
672     $ldap= $this->config->get_ldap_link();
674     /* Adapt shadow values */
675     if (!$this->use_shadowExpire){
676       $this->shadowExpire= "0";
677     } else {
678       /* Transform seconds to days here */
679       $this->shadowExpire= (int)($this->shadowExpire / (60 * 60 * 24)) + 1;
680     }
681     if (!$this->use_shadowMax){
682       $this->shadowMax= "0";
683     }
684     if ($this->must_change_password){
685       $this->shadowLastChange= (int)(date("U") / 86400) - $this->shadowMax - 1;
686     } else {
687       $this->shadowLastChange= (int)(date("U") / 86400);
688     }
689     if (!$this->use_shadowWarning){
690       $this->shadowWarning= "0";
691     }
693     /* Check what to do with ID's */
694     if ($this->force_ids == 0){
696       /* Use id's that are already set */
697       if ($this->savedUidNumber != ""){
698         $this->uidNumber= $this->savedUidNumber;
699         $this->gidNumber= $this->savedGidNumber;
700       } else {
702         /* Calculate new id's. We need to place a lock before calling get_next_id
703            to get real unique values. */
704         $wait= 10;
705         while (get_lock("uidnumber") != ""){
706           sleep (1);
708           /* Oups - timed out */
709           if ($wait-- == 0){
710             print_red (_("Failed: overriding lock"));
711             break;
712           }
713         }
715         add_lock ("uidnumber", "gosa");
716         $this->uidNumber= $this->get_next_id("uidNumber");
717         if ($this->savedGidNumber != ""){
718           $this->gidNumber= $this->savedGidNumber;
719         } else {
720           $this->gidNumber= $this->get_next_id("gidNumber");
721         }
722       }
724       if ($this->primaryGroup != 0){
725         $this->gidNumber= $this->primaryGroup;
726       }
727     }
729     if ($this->use_shadowMin != "1" ) {
730       $this->shadowMin = "";
731     }
733     if (($this->use_shadowMax != "1") && ($this->must_change_password != "1")) {
734       $this->shadowMax = "";
735     }
737     if ($this->use_shadowWarning != "1" ) {
738       $this->shadowWarning = "";
739     }
741     if ($this->use_shadowInactive != "1" ) {
742       $this->shadowInactive = "";
743     }
745     if ($this->use_shadowExpire != "1" ) {
746       $this->shadowExpire = "";
747     }
749     /* Fill gecos */
750     if (isset($this->parent) && $this->parent != NULL){
751       $this->gecos= $this->parent->by_object['user']->cn;
752     }
754     /* Call parents save to prepare $this->attrs */
755     plugin::save();
757     /* Trust accounts */
758     $objectclasses= array();
759     foreach ($this->attrs['objectClass'] as $key => $class){
760       if (preg_match('/trustAccount/i', $class)){
761         continue;
762       }
763       $objectclasses[]= $this->attrs['objectClass'][$key];
764     }
765     $this->attrs['objectClass']= $objectclasses;
766     if ($this->trustModel != ""){
767       $this->attrs['objectClass'][]= "trustAccount";
768       $this->attrs['trustModel']= $this->trustModel;
769       $this->attrs['accessTo']= array();
770       if ($this->trustModel == "byhost"){
771         foreach ($this->accessTo as $host){
772           $this->attrs['accessTo'][]= $host;
773         }
774       }
775     } else {
776       if ($this->was_trust_account){
777         $this->attrs['accessTo']= array();
778         $this->attrs['trustModel']= array();
779       }
780     }
782     if(empty($this->attrs['gosaDefaultPrinter'])){
783       $thid->attrs['gosaDefaultPrinter']=array();
784     }
786     /* Save data to LDAP */
787     $ldap->cd($this->dn);
788     $ldap->modify($this->attrs);
789     show_ldap_error($ldap->get_error());
791     /* Remove lock needed for unique id generation */
792     del_lock ("uidnumber");
795     /* Posix accounts have group interrelationship, take care about these here. */
796     if ($this->force_ids == 0 && $this->primaryGroup == 0){
797       $ldap->cd($this->config->current['BASE']);
798       $ldap->search("(&(objectClass=posixGroup)(gidNumber=".$this->gidNumber."))", array("cn"));
800       /* Create group if it doesn't exist */
801       if ($ldap->count() == 0){
802         $groupdn= preg_replace ('/^'.$this->config->current['DNMODE'].'=[^,]+,'.get_people_ou().'/i', 'cn='.$this->uid.','.get_groups_ou(), $this->dn);
804         $g= new group($this->config, $groupdn);
805         $g->cn= $this->uid;
806         $g->force_gid= 1;
807         $g->gidNumber= $this->gidNumber;
808         $g->description= "Group of user ".$this->givenName." ".$this->sn;
809         $g->save ();
810       }
811     }
813     /* Take care about groupMembership values: add to groups */
814     foreach ($this->groupMembership as $key => $value){
815       $g= new group($this->config, $key);
816       $g->addUser ($this->uid);
817       $g->save();
819       /* May need to save the mail part, too */
820       if ($g->has_mailAccount){
821         $m= new mailgroup($this->config, $key);
822         $m->save();
823       }
824     }
826     /* Remove from groups not listed in groupMembership */
827     foreach ($this->savedGroupMembership as $key => $value){
828       if (!array_key_exists ($key, $this->groupMembership)){
829         $g= new group($this->config, $key);
830         $g->removeUser ($this->uid);
831         $g->save();
833         /* May need to save the mail part, too */
834         if ($g->has_mailAccount){
835           $m= new mailgroup($this->config, $key);
836           $m->save();
837         }
838       }
839     }
841     /* Optionally execute a command after we're done */
842     if ($this->initially_was_account == $this->is_account){
843       if ($this->is_modified){
844         $this->handle_post_events("mofify");
845       }
846     } else {
847       $this->handle_post_events("add");
848     }
849   }
851   /* Check formular input */
852   function check()
853   {
854     /* Include global link_info */
855     $ldap= $this->config->get_ldap_link();
857     /* Reset message array */
858     $message= array();
860     /* must: homeDirectory */
861     if ($this->homeDirectory == ""){
862       $message[]= _("The required field 'Home directory' is not set.");
863     }
864     if (!is_path($this->homeDirectory)){
865       $message[]= _("Please enter a valid path in 'Home directory' field.");
866     }
868     /* Check ID's if they are forced by user */
869     if ($this->force_ids == "1"){
871       /* Valid uid/gid? */
872       if (!is_id($this->uidNumber)){
873         $message[]= _("Value specified as 'UID' is not valid.");
874       } else {
875         if ($this->uidNumber < $this->config->current['MINID']){
876           $message[]= _("Value specified as 'UID' is too small.");
877         }
878       }
879       if (!is_id($this->gidNumber)){
880         $message[]= _("Value specified as 'GID' is not valid.");
881       } else {
882         if ($this->gidNumber < $this->config->current['MINID']){
883           $message[]= _("Value specified as 'GID' is too small.");
884         }
885       }
886     }
888     /* Check shadow settings, well I like spaghetties... */
889     if ($this->use_shadowMin){
890       if (!is_id($this->shadowMin)){
891         $message[]= _("Value specified as 'shadowMin' is not valid.");
892       }
893     }
894     if ($this->use_shadowMax){
895       if (!is_id($this->shadowMax)){
896         $message[]= _("Value specified as 'shadowMax' is not valid.");
897       }
898     }
899     if ($this->use_shadowWarning){
900       if (!is_id($this->shadowWarning)){
901         $message[]= _("Value specified as 'shadowWarning' is not valid.");
902       }
903       if (!$this->use_shadowMax){
904         $message[]= _("'shadowWarning' without 'shadowMax' makes no sense.");
905       }
906       if ($this->shadowWarning > $this->shadowMax){
907         $message[]= _("Value specified as 'shadowWarning' should be smaller than 'shadowMax'.");
908       }
909       if ($this->use_shadowMin && $this->shadowWarning < $this->shadowMin){
910         $message[]= _("Value specified as 'shadowWarning' should be greater than 'shadowMin'.");
911       }
912     }
913     if ($this->use_shadowInactive){
914       if (!is_id($this->shadowInactive)){
915         $message[]= _("Value specified as 'shadowInactive' is not valid.");
916       }
917       if (!$this->use_shadowMax){
918         $message[]= _("'shadowInactive' without 'shadowMax' makes no sense.");
919       }
920     }
921     if ($this->use_shadowMin && $this->use_shadowMax){
922       if ($this->shadowMin > $this->shadowMax){
923         $message[]= _("Value specified as 'shadowMin' should be smaller than 'shadowMax'.");
924       }
925     }
927   //  if(empty($this->gosaDefaultPrinter)){
928   //    $message[]= _("You need to specify a valid default printer.");
929   //  }
931     return ($message);
932   }
935   /* Add posix user to some groups */
936   function addGroup2 ($groups){
937     print "Haleluja".$groups;
938     print_a($this->groupMembership);
939     $this->groupMembership[$this->glist[$groups]['dn']]= $this->glist[$groups]['cn'][0];
940   }
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 ?>