Code

W3c fixes
[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 $status= "";
42   var $loginShellList= array();
43   var $groupMembership= array();
44   var $savedGroupMembership= array();
45   var $savedUidNumber= "";
46   var $savedGidNumber= "";
47   var $use_shadowMin= "0";
48   var $use_shadowMax= "0";
49   var $use_shadowWarning= "0";
50   var $use_shadowInactive= "0";
51   var $use_shadowExpire= "0";
52   var $must_change_password= "0";
53   var $force_ids= 0;
54   var $printerList= array();
55   var $group_dialog= FALSE;
56   var $show_ws_dialog= FALSE;
57   var $secondaryGroups= array();
58   var $primaryGroup= 0;
59   var $was_trust_account= FALSE;
62   /* attribute list for save action */
63   var $attributes= array("homeDirectory", "loginShell", "uidNumber", "gidNumber", "gecos",
64       "shadowMin", "shadowMax", "shadowWarning", "shadowInactive", "shadowLastChange",
65       "shadowExpire", "gosaDefaultPrinter", "gosaDefaultLanguage", "uid");
66   var $objectclasses= array("posixAccount", "shadowAccount");
69   /* constructor, if 'dn' is set, the node loads the given
70      'dn' from LDAP */
71   function posixAccount ($config, $dn= NULL)
72   {
73     /* Configuration is fine, allways */
74     $this->config= $config;
76     /* Load bases attributes */
77     plugin::plugin($config, $dn);
79     $ldap= $this->config->get_ldap_link();
81     if ($dn != NULL){
83       /* Correct is_account. shadowAccount is not required. */
84       if (isset($this->attrs['objectClass']) &&
85           in_array ('posixAccount', $this->attrs['objectClass'])){
87         $this->is_account= TRUE;
88       }
90       /* Is this account a trustAccount? */
91       if ($this->is_account && isset($this->attrs['trustModel'])){
92         $this->trustModel= $this->attrs['trustModel'][0];
93         $this->was_trust_account= TRUE;
94       } else {
95         $this->was_trust_account= FALSE;
96         $this->trustModel= "";
97       }
98       if ($this->is_account && isset($this->attrs['accessTo'])){
99         for ($i= 0; $i<$this->attrs['accessTo']['count']; $i++){
100           $tmp= $this->attrs['accessTo'][$i];
101           $this->accessTo[$tmp]= $tmp;
102         }
103       }
104       $this->initially_was_account= $this->is_account;
106       /* Fill group */
107       $this->primaryGroup= $this->gidNumber;
109       /* Generate status text */
110       $current= date("U");
111       if (($current >= $this->shadowExpire) && $this->shadowExpire){
112         $this->status= "expired";
113         if (($this->shadowExpire - $current) < $this->shadowInactive){
114           $this->status.= ", grace time active";
115         }
116       } elseif (($this->shadowLastChange + $this->shadowMin) >= $current){
117         $this->status= "active, password not changable";
118       } elseif (($this->shadowLastChange + $this->shadowMax) >= $current){
119         $this->status= "active, password expired";
120       } else {
121         $this->status= "active";
122       }
124       /* Get group membership */
125       $ldap->cd($this->config->current['BASE']);
126       $ldap->search("(&(objectClass=posixGroup)(memberUid=".$this->uid."))", array("cn", "description"));
128       while ($this->attrs= $ldap->fetch()){
129         if (!isset($this->attrs["description"][0])){
130           $entry= $this->attrs["cn"][0];
131         } else {
132           $dsc= preg_replace ('/^Group of user/', _("Group of user"), $this->attrs["description"][0]);
133           $entry= $this->attrs["cn"][0]." [$dsc]";
134         }
135         $this->groupMembership[$ldap->getDN()]= $entry;
136       }
137       asort($this->groupMembership);
138       reset($this->groupMembership);
139       $this->savedGroupMembership= $this->groupMembership;
140       $this->savedUidNumber= $this->uidNumber;
141       $this->savedGidNumber= $this->gidNumber;
142     }
144     /* Adjust shadow checkboxes */
145     foreach (array("shadowMin", "shadowMax", "shadowWarning", "shadowInactive",
146           "shadowExpire") as $val){
148       if ($this->$val != 0){
149         $oval= "use_".$val;
150         $this->$oval= "1";
151       }
152     }
154     /* Convert to seconds */
155     if ($this->shadowExpire != 0){
156       $this->shadowExpire*= 60 * 60 * 24;
157     } else {
158       $date= getdate();
159       $this->shadowExpire= floor($date[0] / (60*60*24)) * 60 * 60 * 24;
160     }
162     /* Generate shell list from /etc/gosa/shells */
163     if (file_exists('/etc/gosa/shells')){
164       $shells = file ('/etc/gosa/shells');
165       foreach ($shells as $line){
166         if (!preg_match ("/^#/", $line)){
167           $this->loginShellList[]= trim($line);
168         }
169       }
170     } else {
171       if ($this->loginShell == ""){
172         $this->loginShellList[]= _("unconfigured");
173       }
174     }
176     /* Insert possibly missing loginShell */
177     if ($this->loginShell != "" && !in_array($this->loginShell, $this->loginShellList)){
178       $this->loginShellList[]= $this->loginShell;
179     }
181     /* Generate printer list */
182     if (isset($this->config->data['SERVERS']['CUPS'])){
183       $this->printerList= get_printer_list ($this->config->data['SERVERS']['CUPS']);
184       asort($this->printerList);
185     }
187     /* Generate group list */
188     $ldap->cd($this->config->current['BASE']);
189     $ldap->search("(objectClass=posixGroup)", array("cn", "gidNumber"));
190     $this->secondaryGroups[]= "- "._("automatic")." -";
191     while ($attrs= $ldap->fetch()){
192       $this->secondaryGroups[$attrs['gidNumber'][0]]= $attrs['cn'][0];
193     }
194     asort ($this->secondaryGroups);
196     /* Get global filter config */
197     if (!is_global("sysfilter")){
198       $ui= get_userinfo();
199       $base= get_base_from_people($ui->dn);
200       $sysfilter= array( "depselect"       => $base,
201           "regex"           => "*");
202       register_global("sysfilter", $sysfilter);
203     }
204   }
207   /* execute generates the html output for this node */
208   function execute()
209   {
210     /* Do we need to flip is_account state? */
211     if (isset($_POST['modify_state'])){
212       $this->is_account= !$this->is_account;
213     }
215     /* Do we represent a valid posixAccount? */
216     if (!$this->is_account && $this->parent == NULL ){
217       $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\">&nbsp;<b>".
218         _("This account has no unix extensions.")."</b>";
219       $display.= back_to_main();
220       return ($display);
221     }
223     $display= "";
225     /* Show tab dialog headers */
226     if ($this->parent != NULL){
227       if ($this->is_account){
228         if (isset($this->parent->by_object['sambaAccount'])){
229           $obj= $this->parent->by_object['sambaAccount'];
230         }
231         if (isset($obj) && $obj->is_account == TRUE &&
232             isset($this->parent->by_object['sambaAccount'])){
234           /* Samba3 dependency on posix accounts are enabled
235              in the moment, because I need to rely on unique
236              uidNumbers. There'll be a better solution later
237              on. */
238           $display= $this->show_header(_("Remove posix account"),
239               _("This account has unix features enabled. To disable them, you'll need to remove the samba account first."), TRUE);
240         } else {
241           $display= $this->show_header(_("Remove posix account"),
242               _("This account has posix features enabled. You can disable them by clicking below."));
243         }
244       } else {
245         $display= $this->show_header(_("Create posix account"),
246             _("This account has posix features disabled. You can enable them by clicking below."));
247         return($display);
248       }
249     }
251     /* Trigger group edit? */
252     if (isset($_POST['edit_groupmembership'])){
253       $this->group_dialog= TRUE;
254       $this->dialog= TRUE;
255     }
257     /* Cancel group edit? */
258     if (isset($_POST['add_groups_cancel']) ||
259         isset($_POST['add_groups_finish'])){
260       $this->group_dialog= FALSE;
261       $this->dialog= FALSE;
262     }
264     /* Add selected groups */
265     if (isset($_POST['add_groups_finish']) && isset($_POST['groups']) &&
266         count($_POST['groups'])){
268       if (chkacl ($this->acl, "memberUid") == ""){
269         $this->addGroup ($_POST['groups']);
270         $this->is_modified= TRUE;
271       }
272     }
274     /* Delete selected groups */
275     if (isset($_POST['delete_groupmembership']) && 
276         isset($_POST['group_list']) && count($_POST['group_list'])){
278       if (chkacl ($this->acl, "memberUid") == ""){
279         $this->delGroup ($_POST['group_list']);
280         $this->is_modified= TRUE;
281       }
282     }
284     /* Add user workstation? */
285     if (isset($_POST["add_ws"])){
286       $this->show_ws_dialog= TRUE;
287       $this->dialog= TRUE;
288     }
290     /* Add user workstation? */
291     if (isset($_POST["add_ws_finish"]) && isset($_POST['wslist'])){
292       foreach($_POST['wslist'] as $ws){
293         $this->accessTo[$ws]= $ws;
294       }
295       ksort($this->accessTo);
296       $this->is_modified= TRUE;
297     }
299     /* Remove user workstations? */
300     if (isset($_POST["delete_ws"]) && isset($_POST['workstation_list'])){
301       foreach($_POST['workstation_list'] as $name){
302         unset ($this->accessTo[$name]);
303       }
304       $this->is_modified= TRUE;
305     }
307     /* Add user workstation finished? */
308     if (isset($_POST["add_ws_finish"]) || isset($_POST["add_ws_cancel"])){
309       $this->show_ws_dialog= FALSE;
310       $this->dialog= FALSE;
311     }
313     /* Templates now! */
314     $smarty= get_smarty();
316     /* Show ws dialog */
317     if ($this->show_ws_dialog){
318       /* Save data */
319       $sysfilter= get_global("sysfilter");
320       foreach( array("depselect", "regex") as $type){
321         if (isset($_POST[$type])){
322           $sysfilter[$type]= $_POST[$type];
323         }
324       }
325       if (isset($_GET['search'])){
326         $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
327         if ($s == "**"){
328           $s= "*";
329         }
330         $sysfilter['regex']= $s;
331       }
332       register_global("sysfilter", $sysfilter);
334       /* Get workstation list */
335       $exclude= "";
336       foreach($this->accessTo as $ws){
337         $exclude.= "(cn=$ws)";
338       }
339       if ($exclude != ""){
340         $exclude= "(!(|$exclude))";
341       }
342       $acl= array($this->config->current['BASE'] => ":all");
343       $regex= $sysfilter['regex'];
344       $filter= "(&(|(objectClass=goServer)(objectClass=gotoWorkstation)(objectClass=gotoTerminal))$exclude(cn=*)(cn=$regex))";
345       $res= get_list($acl, "$filter", TRUE, $sysfilter['depselect'], array("cn"), TRUE);
346       $wslist= array();
347       foreach ($res as $attrs){
348         $wslist[]= preg_replace('/\$/', '', $attrs['cn'][0]);
349       }
350       asort($wslist);
351       $smarty->assign("search_image", get_template_path('images/search.png'));
352       $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
353       $smarty->assign("tree_image", get_template_path('images/tree.png'));
354       $smarty->assign("deplist", $this->config->idepartments);
355       $smarty->assign("alphabet", generate_alphabet());
356       foreach( array("depselect", "regex") as $type){
357         $smarty->assign("$type", $sysfilter[$type]);
358       }
359       $smarty->assign("hint", print_sizelimit_warning());
360       $smarty->assign("wslist", $wslist);
361       $smarty->assign("apply", apply_filter());
362       $display= $smarty->fetch (get_template_path('trust_machines.tpl', TRUE, dirname(__FILE__)));
363       return ($display);
364     }
366     /* Manage group add dialog */
367     if ($this->group_dialog){
368       $gd= new groupManagement($this->config, get_userinfo());
370       /* Save data */
371       $groupfilter= get_global("groupfilter");
372       foreach( array("depselect", "guser", "regex") as $type){
373         if (isset($_POST[$type])){
374           $groupfilter[$type]= $_POST[$type];
375         }
376       }
377       if (isset($_POST['depselect'])){
378         foreach( array("primarygroups", "sambagroups", "mailgroups", "appgroups",
379               "functionalgroups") as $type){
381           if (isset($_POST[$type])) {
382             $groupfilter[$type]= "checked";
383           } else {
384             $groupfilter[$type]= "";
385           }
386         }
387       }
388       if (isset($_GET['search'])){
389         $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
390         if ($s == "**"){
391           $s= "*";
392         }
393         $groupfilter['regex']= $s;
394       }
395       register_global("groupfilter", $groupfilter);
397       /* Calculate actual groups */
398       $gd->reload();
399       $glist= array();
400       foreach ($gd->grouplist as $key => $value){
401         if (!isset($this->groupMembership[$key])){
402           $glist[$key]= $value;
403         }
404       }
406       /* Show dialog */
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     /* Call parents save to prepare $this->attrs */
750     plugin::save();
752     /* Fill gecos */
753     if (isset($this->parent) && $this->parent != NULL){
754       $this->gecos= $this->parent->by_object['user']->cn;
755     }
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     /* Save data to LDAP */
783     $ldap->cd($this->dn);
784     $ldap->modify($this->attrs);
785     show_ldap_error($ldap->get_error());
787     /* Remove lock needed for unique id generation */
788     del_lock ("uidnumber");
791     /* Posix accounts have group interrelationship, take care about these here. */
792     if ($this->force_ids == 0 && $this->primaryGroup == 0){
793       $ldap->cd($this->config->current['BASE']);
794       $ldap->search("(&(objectClass=posixGroup)(gidNumber=".$this->gidNumber."))", array("cn"));
796       /* Create group if it doesn't exist */
797       if ($ldap->count() == 0){
798         $groupdn= preg_replace ('/^'.$this->config->current['DNMODE'].'=[^,]+,'.get_people_ou().'/i', 'cn='.$this->uid.','.get_groups_ou(), $this->dn);
800         $g= new group($this->config, $groupdn);
801         $g->cn= $this->uid;
802         $g->force_gid= 1;
803         $g->gidNumber= $this->gidNumber;
804         $g->description= "Group of user ".$this->givenName." ".$this->sn;
805         $g->save ();
806       }
807     }
809     /* Take care about groupMembership values: add to groups */
810     foreach ($this->groupMembership as $key => $value){
811       $g= new group($this->config, $key);
812       $g->addUser ($this->uid);
813       $g->save();
815       /* May need to save the mail part, too */
816       if ($g->has_mailAccount){
817         $m= new mailgroup($this->config, $key);
818         $m->save();
819       }
820     }
822     /* Remove from groups not listed in groupMembership */
823     foreach ($this->savedGroupMembership as $key => $value){
824       if (!array_key_exists ($key, $this->groupMembership)){
825         $g= new group($this->config, $key);
826         $g->removeUser ($this->uid);
827         $g->save();
829         /* May need to save the mail part, too */
830         if ($g->has_mailAccount){
831           $m= new mailgroup($this->config, $key);
832           $m->save();
833         }
834       }
835     }
837     /* Optionally execute a command after we're done */
838     if ($this->initially_was_account == $this->is_account){
839       if ($this->is_modified){
840         $this->handle_post_events("mofify");
841       }
842     } else {
843       $this->handle_post_events("add");
844     }
845   }
847   /* Check formular input */
848   function check()
849   {
850     /* Include global link_info */
851     $ldap= $this->config->get_ldap_link();
853     /* Reset message array */
854     $message= array();
856     /* must: homeDirectory */
857     if ($this->homeDirectory == ""){
858       $message[]= _("The required field 'Home directory' is not set.");
859     }
860     if (!is_path($this->homeDirectory)){
861       $message[]= _("Please enter a valid path in 'Home directory' field.");
862     }
864     /* Check ID's if they are forced by user */
865     if ($this->force_ids == "1"){
867       /* Valid uid/gid? */
868       if (!is_id($this->uidNumber)){
869         $message[]= _("Value specified as 'UID' is not valid.");
870       } else {
871         if ($this->uidNumber < $this->config->current['MINID']){
872           $message[]= _("Value specified as 'UID' is too small.");
873         }
874       }
875       if (!is_id($this->gidNumber)){
876         $message[]= _("Value specified as 'GID' is not valid.");
877       } else {
878         if ($this->gidNumber < $this->config->current['MINID']){
879           $message[]= _("Value specified as 'GID' is too small.");
880         }
881       }
882     }
884     /* Check shadow settings, well I like spaghetties... */
885     if ($this->use_shadowMin){
886       if (!is_id($this->shadowMin)){
887         $message[]= _("Value specified as 'shadowMin' is not valid.");
888       }
889     }
890     if ($this->use_shadowMax){
891       if (!is_id($this->shadowMax)){
892         $message[]= _("Value specified as 'shadowMax' is not valid.");
893       }
894     }
895     if ($this->use_shadowWarning){
896       if (!is_id($this->shadowWarning)){
897         $message[]= _("Value specified as 'shadowWarning' is not valid.");
898       }
899       if (!$this->use_shadowMax){
900         $message[]= _("'shadowWarning' without 'shadowMax' makes no sense.");
901       }
902       if ($this->shadowWarning > $this->shadowMax){
903         $message[]= _("Value specified as 'shadowWarning' should be smaller than 'shadowMax'.");
904       }
905       if ($this->use_shadowMin && $this->shadowWarning < $this->shadowMin){
906         $message[]= _("Value specified as 'shadowWarning' should be greater than 'shadowMin'.");
907       }
908     }
909     if ($this->use_shadowInactive){
910       if (!is_id($this->shadowInactive)){
911         $message[]= _("Value specified as 'shadowInactive' is not valid.");
912       }
913       if (!$this->use_shadowMax){
914         $message[]= _("'shadowInactive' without 'shadowMax' makes no sense.");
915       }
916     }
917     if ($this->use_shadowMin && $this->use_shadowMax){
918       if ($this->shadowMin > $this->shadowMax){
919         $message[]= _("Value specified as 'shadowMin' should be smaller than 'shadowMax'.");
920       }
921     }
923     return ($message);
924   }
927   /* Add posix user to some groups */
928   function addGroup ($groups)
929   {
930     /* include global link_info */
931     $ldap= $this->config->get_ldap_link();
933     /* Walk through groups and add the descriptive entry if not exists */
934     foreach ($groups as $value){
935       if (!array_key_exists($value, $this->groupMembership)){
936         $ldap->cat($value);
937         $attrs= $ldap->fetch();
938         error_reporting (0);
939         if (!isset($attrs['description'][0])){
940           $entry= $attrs["cn"][0];
941         } else {
942           $dsc= preg_replace ('/^Group of user/', _("Group of user"), $attrs["description"][0]);
943           $entry= $attrs["cn"][0]." [$dsc]";
944         }
945         error_reporting (E_ALL);
946         $this->groupMembership[$ldap->getDN()]= $entry;
947       }
948     }
950     /* Sort groups */
951     asort ($this->groupMembership);
952     reset ($this->groupMembership);
953   }
956   /* Del posix user from some groups */
957   function delGroup ($groups)
958   {
959     $dest= array();
961     foreach ($this->groupMembership as $key => $value){
962       if (!in_array($key, $groups)){
963         $dest[$key]= $value;
964       }
965     }
966     $this->groupMembership= $dest;
967   }
969   /* Adapt from template, using 'dn' */
970   function adapt_from_template($dn)
971   {
972     /* Include global link_info */
973     $ldap= $this->config->get_ldap_link();
975     plugin::adapt_from_template($dn);
976     $template= $this->attrs['uid'][0];
978     /* Adapt group membership */
979     $ldap->cd($this->config->current['BASE']);
980     $ldap->search("(&(objectClass=posixGroup)(memberUid=".$this->attrs["uid"][0]."))", array("description", "cn"));
982     while ($this->attrs= $ldap->fetch()){
983       if (!isset($this->attrs["description"][0])){
984         $entry= $this->attrs["cn"][0];
985       } else {
986         $entry= $this->attrs["cn"][0]." [".$this->attrs["description"][0]."]";
987       }
988       $this->groupMembership[$ldap->getDN()]= $entry;
989     }
991     /* Fix primary group settings */
992     $ldap->cd($this->config->current['BASE']);
993     $ldap->search("(&(objectClass=posixGroup)(cn=$template)(gidNumber=".$this->gidNumber."))", array("cn"));
994     if ($ldap->count() != 1){
995       $this->primaryGroup= $this->gidNumber;
996     }
997   }
999   function get_next_id($attrib)
1000   {
1001     $ids= array();
1002     $ldap= $this->config->get_ldap_link();
1004     $ldap->cd ($this->config->current['BASE']);
1005     $ldap->search ("($attrib=*)", array("$attrib"));
1007     /* Get list of ids */
1008     while ($attrs= $ldap->fetch()){
1009       $ids[]= (int)$attrs["$attrib"][0];
1010     }
1012     /* Find out next free id near to UID_BASE */
1013     for ($id= $this->config->current['UIDBASE']; $id++; $id<65000){
1014       if (!in_array($id, $ids)){
1015         return ($id);
1016       }
1017     }
1019     /* Should not happen */
1020     if ($id == 65000){
1021       print_red(_("Too many users, can't allocate a free ID!"));
1022       exit;
1023     }
1025   }
1029 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1030 ?>