Code

d163ee0fe17c8c732f7269f8d3c0372710bff6f2
[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         /* Call parent execute */
212         plugin::execute();
213     /* Do we need to flip is_account state? */
214     if (isset($_POST['modify_state'])){
215       $this->is_account= !$this->is_account;
216     }
218     /* Do we represent a valid posixAccount? */
219     if (!$this->is_account && $this->parent == NULL ){
220       $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\">&nbsp;<b>".
221         _("This account has no unix extensions.")."</b>";
222       $display.= back_to_main();
223       return ($display);
224     }
226     $display= "";
228     /* Show tab dialog headers */
229     if ($this->parent != NULL){
230       if ($this->is_account){
231         if (isset($this->parent->by_object['sambaAccount'])){
232           $obj= $this->parent->by_object['sambaAccount'];
233         }
234         if (isset($obj) && $obj->is_account == TRUE &&
235              ((isset($this->parent->by_object['sambaAccount']))&&($this->parent->by_object['sambaAccount']->is_account))
236                         ||(isset($this->parent->by_object['environment'] ))&&($this->parent->by_object['environment'] ->is_account)){
238           /* Samba3 dependency on posix accounts are enabled
239              in the moment, because I need to rely on unique
240              uidNumbers. There'll be a better solution later
241              on. */
242           $display= $this->show_header(_("Remove posix account"),
243               _("This account has unix features enabled. To disable them, you'll need to remove the samba / environment account first."), TRUE);
244         } else {
245           $display= $this->show_header(_("Remove posix account"),
246               _("This account has posix features enabled. You can disable them by clicking below."));
247         }
248       } else {
249         $display= $this->show_header(_("Create posix account"),
250             _("This account has posix features disabled. You can enable them by clicking below."));
251         return($display);
252       }
253     }
255     /* Trigger group edit? */
256     if (isset($_POST['edit_groupmembership'])){
257       $this->group_dialog= TRUE;
258       $this->dialog= TRUE;
259     }
261     /* Cancel group edit? */
262     if (isset($_POST['add_groups_cancel']) ||
263         isset($_POST['add_groups_finish'])){
264       $this->group_dialog= FALSE;
265       $this->dialog= FALSE;
266     }
268     /* Add selected groups */
269     if (isset($_POST['add_groups_finish']) && isset($_POST['groups']) &&
270         count($_POST['groups'])){
272       if (chkacl ($this->acl, "memberUid") == ""){
273         $this->addGroup ($_POST['groups']);
274         $this->is_modified= TRUE;
275       }
276     }
278     /* Delete selected groups */
279     if (isset($_POST['delete_groupmembership']) && 
280         isset($_POST['group_list']) && count($_POST['group_list'])){
282       if (chkacl ($this->acl, "memberUid") == ""){
283         $this->delGroup ($_POST['group_list']);
284         $this->is_modified= TRUE;
285       }
286     }
288     /* Add user workstation? */
289     if (isset($_POST["add_ws"])){
290       $this->show_ws_dialog= TRUE;
291       $this->dialog= TRUE;
292     }
294     /* Add user workstation? */
295     if (isset($_POST["add_ws_finish"]) && isset($_POST['wslist'])){
296       foreach($_POST['wslist'] as $ws){
297         $this->accessTo[$ws]= $ws;
298       }
299       ksort($this->accessTo);
300       $this->is_modified= TRUE;
301     }
303     /* Remove user workstations? */
304     if (isset($_POST["delete_ws"]) && isset($_POST['workstation_list'])){
305       foreach($_POST['workstation_list'] as $name){
306         unset ($this->accessTo[$name]);
307       }
308       $this->is_modified= TRUE;
309     }
311     /* Add user workstation finished? */
312     if (isset($_POST["add_ws_finish"]) || isset($_POST["add_ws_cancel"])){
313       $this->show_ws_dialog= FALSE;
314       $this->dialog= FALSE;
315     }
317     /* Templates now! */
318     $smarty= get_smarty();
320     /* Show ws dialog */
321     if ($this->show_ws_dialog){
322       /* Save data */
323       $sysfilter= get_global("sysfilter");
324       foreach( array("depselect", "regex") as $type){
325         if (isset($_POST[$type])){
326           $sysfilter[$type]= $_POST[$type];
327         }
328       }
329       if (isset($_GET['search'])){
330         $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
331         if ($s == "**"){
332           $s= "*";
333         }
334         $sysfilter['regex']= $s;
335       }
336       register_global("sysfilter", $sysfilter);
338       /* Get workstation list */
339       $exclude= "";
340       foreach($this->accessTo as $ws){
341         $exclude.= "(cn=$ws)";
342       }
343       if ($exclude != ""){
344         $exclude= "(!(|$exclude))";
345       }
346       $acl= array($this->config->current['BASE'] => ":all");
347       $regex= $sysfilter['regex'];
348       $filter= "(&(|(objectClass=goServer)(objectClass=gotoWorkstation)(objectClass=gotoTerminal))$exclude(cn=*)(cn=$regex))";
349       $res= get_list($acl, "$filter", TRUE, $sysfilter['depselect'], array("cn"), TRUE);
350       $wslist= array();
351       foreach ($res as $attrs){
352         $wslist[]= preg_replace('/\$/', '', $attrs['cn'][0]);
353       }
354       asort($wslist);
355       $smarty->assign("search_image", get_template_path('images/search.png'));
356       $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
357       $smarty->assign("tree_image", get_template_path('images/tree.png'));
358       $smarty->assign("deplist", $this->config->idepartments);
359       $smarty->assign("alphabet", generate_alphabet());
360       foreach( array("depselect", "regex") as $type){
361         $smarty->assign("$type", $sysfilter[$type]);
362       }
363       $smarty->assign("hint", print_sizelimit_warning());
364       $smarty->assign("wslist", $wslist);
365       $smarty->assign("apply", apply_filter());
366       $display= $smarty->fetch (get_template_path('trust_machines.tpl', TRUE, dirname(__FILE__)));
367       return ($display);
368     }
370     /* Manage group add dialog */
371     if ($this->group_dialog){
372       $gd= new groupManagement($this->config, get_userinfo());
374       /* Save data */
375       $groupfilter= get_global("groupfilter");
376       foreach( array("depselect", "guser", "regex") as $type){
377         if (isset($_POST[$type])){
378           $groupfilter[$type]= $_POST[$type];
379         }
380       }
381       if (isset($_POST['depselect'])){
382         foreach( array("primarygroups", "sambagroups", "mailgroups", "appgroups",
383               "functionalgroups") as $type){
385           if (isset($_POST[$type])) {
386             $groupfilter[$type]= "checked";
387           } else {
388             $groupfilter[$type]= "";
389           }
390         }
391       }
392       if (isset($_GET['search'])){
393         $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
394         if ($s == "**"){
395           $s= "*";
396         }
397         $groupfilter['regex']= $s;
398       }
399       register_global("groupfilter", $groupfilter);
401       /* Calculate actual groups */
402       $gd->reload(true);
403       $glist= array();
404       foreach ($gd->grouplist as $key => $value){
405         if (!isset($this->groupMembership[$key])){
406           $glist[$key]= $value;
407         }
408       }
410       $smarty->assign("groups", $glist);
411       $smarty->assign("search_image", get_template_path('images/search.png'));
412       $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
413       $smarty->assign("tree_image", get_template_path('images/tree.png'));
414       $smarty->assign("deplist", $this->config->idepartments);
415       $smarty->assign("alphabet", generate_alphabet());
416       foreach( array("depselect", "guser", "regex", "primarygroups", "mailgroups",
417             "appgroups", "sambagroups", "functionalgroups") as $type){
418         $smarty->assign("$type", $groupfilter[$type]);
419       }
420       $smarty->assign("hint", print_sizelimit_warning());
422       $smarty->assign("apply", apply_filter());
423       $display.= $smarty->fetch (get_template_path('posix_groups.tpl', TRUE, dirname(__FILE__)));
424       return ($display);
425     }
427     /* Show main page */
428     $smarty= get_smarty();
430     /* Depending on pwmode, currently hardcoded because there are no other methods */
431     if ( 1 == 1 ){
432       $smarty->assign("pwmode", dirname(__FILE__)."/posix_shadow");
433       $shadowMinACL= chkacl($this->acl, "shadowMin");
434       $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."\">"));
435       $shadowMaxACL= chkacl($this->acl, "shadowMax");
436       $smarty->assign("shadowmaxs", sprintf(_("Password must be changed after %s days"), "<input name=\"shadowMax\" size=3 maxlength=4 $shadowMaxACL value=\"".$this->shadowMax."\">"));
437       $shadowInactiveACL= chkacl($this->acl, "shadowInactive");
438       $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."\">"));
439       $shadowWarningACL= chkacl($this->acl, "shadowWarning");
440       $smarty->assign("shadowwarnings", sprintf(_("Warn user %s days before password expiery"), "<input name=\"shadowWarning\" size=3 maxlength=4 $shadowWarningACL value=\"".$this->shadowWarning."\">"));
441       foreach( array("must_change_password", "use_shadowMin", "use_shadowMax",
442             "use_shadowExpire", "use_shadowInactive",
443             "use_shadowWarning") as $val){
444         if ($this->$val == 1){
445           $smarty->assign("$val", "checked");
446         } else {
447           $smarty->assign("$val", "");
448         }
449         $smarty->assign("$val"."ACL", chkacl($this->acl, $val));
450       }
451     }
453     /* Fill calendar */
454     $date= getdate($this->shadowExpire);
456     $days= array();
457     for($d= 1; $d<32; $d++){
458       $days[$d]= $d;
459     }
460     $years= array();
461     for($y= $date['year']-10; $y<$date['year']+10; $y++){
462       $years[]= $y;
463     }
464     $months= array(_("January"), _("February"), _("March"), _("April"),
465         _("May"), _("June"), _("July"), _("August"), _("September"),
466         _("October"), _("November"), _("December"));
467     $smarty->assign("day", $date["mday"]);
468     $smarty->assign("days", $days);
469     $smarty->assign("months", $months);
470     $smarty->assign("month", $date["mon"]-1);
471     $smarty->assign("years", $years);
472     $smarty->assign("year", $date["year"]);
474     /* Fill arrays */
475     $smarty->assign("shells", $this->loginShellList);
476     $smarty->assign("secondaryGroups", $this->secondaryGroups);
477     $smarty->assign("primaryGroup", $this->primaryGroup);
478     if (!count($this->groupMembership)){
479       $smarty->assign("groupMembership", array("&nbsp;"));
480     } else {
481       $smarty->assign("groupMembership", $this->groupMembership);
482     }
483     if (count($this->groupMembership) > 16){
484       $smarty->assign("groups", "too_many_for_nfs");
485     } else {
486       $smarty->assign("groups", "");
487     }
488     $smarty->assign("printerList", $this->printerList);
489     $smarty->assign("languages", $this->config->data['MAIN']['LANGUAGES']);
491         /* Avoid "Undefined index: forceMode" */
492     $smarty->assign("forceMode", "");
494     /* Checkboxes */
495     if ($this->force_ids == 1){
496       $smarty->assign("force_ids", "checked");
497       if ($_SESSION['js']){
498         $smarty->assign("forceMode", "");
499       }
500     } else {
501       if ($_SESSION['js']){
502         $smarty->assign("forceMode", "disabled");
503       }
504       $smarty->assign("force_ids", "");
505     }
506     $smarty->assign("force_idsACL", chkacl($this->acl, "force_ids"));
508     /* Load attributes and acl's */
509     foreach($this->attributes as $val){
510       if(($_SESSION["js"])&&(($val=="uidNumber")||($val=="gidNumber")))
511         {
512           $smarty->assign("$val"."ACL","");
513           $smarty->assign("$val", $this->$val);
514           continue;
515         }
516       $smarty->assign("$val", $this->$val);
517       $smarty->assign("$val"."ACL", chkacl($this->acl,$val));
518     }
519     $smarty->assign("groupMembershipACL", chkacl($this->acl, "groupMembership"));
520     $smarty->assign("status", $this->status);
522     /* Work on trust modes */
523     $smarty->assign("trustmodeACL", chkacl($this->acl, "trustmode"));
524     if ($this->trustModel == "fullaccess"){
525       $trustmode= 1;
526       // pervent double disable tag in html code, this will disturb our clean w3c html
527     
528     if(chkacl($this->acl, "trustmode")==""){
529           $smarty->assign("trusthide", "disabled");
530       }else{
531           $smarty->assign("trusthide", "");
532       }
534     } elseif ($this->trustModel == "byhost"){
535       $trustmode= 2;
536       $smarty->assign("trusthide", "");
537     } else {
538       // pervent double disable tag in html code, this will disturb our clean w3c html
539       if(chkacl($this->acl, "trustmode")==""){
540           $smarty->assign("trusthide", "disabled");
541       }else{
542           $smarty->assign("trusthide", "");
543       }
544       $trustmode= 0;
545     }
546     $smarty->assign("trustmode", $trustmode);
547     $smarty->assign("trustmodes", array( 0 => _("disabled"), 1 => _("full access"),
548           2 => _("allow access to these hosts")));
552     if((count($this->accessTo))==0)
553       $smarty->assign("emptyArrAccess",true);
554     else
555       $smarty->assign("emptyArrAccess",false);
556     
559     $smarty->assign("workstations", $this->accessTo);
561     $smarty->assign("apply", apply_filter());
562     $display.= $smarty->fetch (get_template_path('generic.tpl', TRUE, dirname(__FILE__)));
563     return($display);
564   }
567   /* remove object from parent */
568   function remove_from_parent()
569   {
570     /* Cancel if there's nothing to do here */
571     if (!$this->initially_was_account){
572       return;
573     }
574     
575     /* include global link_info */
576     $ldap= $this->config->get_ldap_link();
578     /* Remove and write to LDAP */
579     plugin::remove_from_parent();
581     /* Zero out array */
582     $this->attrs['gosaHostACL']= array();
584     /* Keep uid, because we need it for authentification! */
585     unset($this->attrs['uid']);
587     @DEBUG (DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__,
588         $this->attributes, "Save");
589     $ldap->cd($this->dn);
590     $ldap->modify($this->attrs);
591     show_ldap_error($ldap->get_error());
593     /* Delete group only if cn is uid and there are no other
594        members inside */
595     $ldap->cd ($this->config->current['BASE']);
596     $ldap->search ("(&(objectClass=posixGroup)(gidNumber=".$this->gidNumber."))", array("cn", "memberUid"));
597     if ($ldap->count() != 0){
598       $attrs= $ldap->fetch();
599       if ($attrs['cn'][0] == $this->uid &&
600           !isset($this->attrs['memberUid'])){
602         $ldap->rmDir($ldap->getDN());
603       }
604     }
606     /* Optionally execute a command after we're done */
607     $this->handle_post_events("remove");
608   }
611   function save_object()
612   {
613     if (isset($_POST['posixTab'])){
614       /* Save values to object */
615       plugin::save_object();
617       /* Save force GID attribute */
618       if (chkacl ($this->acl, "force_ids") == ""){
619         if (isset ($_POST['force_ids'])){
620           $data= 1;
621         } else {
622           $data= 0;
623         }
624         if ($this->force_ids != $data){
625           $this->is_modified= TRUE;
626         }
627         $this->force_ids= $data;
629         $data= $_POST['primaryGroup'];
630         if ($this->primaryGroup != $data){
631           $this->is_modified= TRUE;
632         }
633         $this->primaryGroup= $_POST['primaryGroup'];
634       }
636       /* Save pwmode dependent attributes, curently hardcoded because there're
637          no alternatives */
638       if (1 == 1){
639         foreach( array("must_change_password", "use_shadowMin", "use_shadowMax",
640               "use_shadowExpire", "use_shadowInactive",
641               "use_shadowWarning") as $val){
642           if (chkacl($this->acl, "$val") == ""){
643             if (isset ($_POST[$val])){
644               $data= 1;
645             } else {
646               $data= 0;
647             }
648             if ($data != $this->$val){
649               $this->is_modified= TRUE;
650             }
651             $this->$val= $data;
652           }
653         }
654       }
656       /* Trust mode - special handling */
657       if (isset($_POST['trustmode'])){
658         $saved= $this->trustModel;
659         if ($_POST['trustmode'] == "1"){
660           $this->trustModel= "fullaccess";
661         } elseif ($_POST['trustmode'] == "2"){
662           $this->trustModel= "byhost";
663         } else {
664           $this->trustModel= "";
665         }
666         if ($this->trustModel != $saved){
667           $this->is_modified= TRUE;
668         }
669       }
670     }
671   }
674   /* Save data to LDAP, depending on is_account we save or delete */
675   function save()
676   {
677         
678     /* include global link_info */
679     $ldap= $this->config->get_ldap_link();
681     /* Adapt shadow values */
682     if (!$this->use_shadowExpire){
683       $this->shadowExpire= "0";
684     } else {
685       /* Transform seconds to days here */
686       $this->shadowExpire= (int)($this->shadowExpire / (60 * 60 * 24)) + 1;
687     }
688     if (!$this->use_shadowMax){
689       $this->shadowMax= "0";
690     }
691     if ($this->must_change_password){
692       $this->shadowLastChange= (int)(date("U") / 86400) - $this->shadowMax - 1;
693     } else {
694       $this->shadowLastChange= (int)(date("U") / 86400);
695     }
696     if (!$this->use_shadowWarning){
697       $this->shadowWarning= "0";
698     }
700     /* Check what to do with ID's */
701     if ($this->force_ids == 0){
703       /* Use id's that are already set */
704       if ($this->savedUidNumber != ""){
705         $this->uidNumber= $this->savedUidNumber;
706         $this->gidNumber= $this->savedGidNumber;
707       } else {
709         /* Calculate new id's. We need to place a lock before calling get_next_id
710            to get real unique values. */
711         $wait= 10;
712         while (get_lock("uidnumber") != ""){
713           sleep (1);
715           /* Oups - timed out */
716           if ($wait-- == 0){
717             print_red (_("Failed: overriding lock"));
718             break;
719           }
720         }
722         add_lock ("uidnumber", "gosa");
723         $this->uidNumber= $this->get_next_id("uidNumber");
724         if ($this->savedGidNumber != ""){
725           $this->gidNumber= $this->savedGidNumber;
726         } else {
727           $this->gidNumber= $this->get_next_id("gidNumber");
728         }
729       }
731       if ($this->primaryGroup != 0){
732         $this->gidNumber= $this->primaryGroup;
733       }
734     }
736     if ($this->use_shadowMin != "1" ) {
737       $this->shadowMin = "";
738     }
740     if (($this->use_shadowMax != "1") && ($this->must_change_password != "1")) {
741       $this->shadowMax = "";
742     }
744     if ($this->use_shadowWarning != "1" ) {
745       $this->shadowWarning = "";
746     }
748     if ($this->use_shadowInactive != "1" ) {
749       $this->shadowInactive = "";
750     }
752     if ($this->use_shadowExpire != "1" ) {
753       $this->shadowExpire = "";
754     }
756     /* Fill gecos */
757     if (isset($this->parent) && $this->parent != NULL){
758       $this->gecos= rewrite($this->parent->by_object['user']->cn);
759           if (!preg_match('/[a-z0-9 -]/i', $this->gecos)){
760                   $this->gecos= "";
761           }
762     }
764     /* Call parents save to prepare $this->attrs */
765     plugin::save();
767     /* Trust accounts */
768     $objectclasses= array();
769     foreach ($this->attrs['objectClass'] as $key => $class){
770       if (preg_match('/trustAccount/i', $class)){
771         continue;
772       }
773       $objectclasses[]= $this->attrs['objectClass'][$key];
774     }
775     $this->attrs['objectClass']= $objectclasses;
776     if ($this->trustModel != ""){
777       $this->attrs['objectClass'][]= "trustAccount";
778       $this->attrs['trustModel']= $this->trustModel;
779       $this->attrs['accessTo']= array();
780       if ($this->trustModel == "byhost"){
781         foreach ($this->accessTo as $host){
782           $this->attrs['accessTo'][]= $host;
783         }
784       }
785     } else {
786       if ($this->was_trust_account){
787         $this->attrs['accessTo']= array();
788         $this->attrs['trustModel']= array();
789       }
790     }
792     if(empty($this->attrs['gosaDefaultPrinter'])){
793       $thid->attrs['gosaDefaultPrinter']=array();
794     }
796         foreach(array("shadowMin","shadowMax","shadowWarning","shadowInactive") as $attr){
797                 $this->attrs[$attr] = (int) $this->attrs[$attr];
798         }
800     /* Save data to LDAP */
801     $ldap->cd($this->dn);
802     $ldap->modify($this->attrs);
803     show_ldap_error($ldap->get_error());
805     /* Remove lock needed for unique id generation */
806     del_lock ("uidnumber");
809     /* Posix accounts have group interrelationship, take care about these here. */
810     if ($this->force_ids == 0 && $this->primaryGroup == 0){
811       $ldap->cd($this->config->current['BASE']);
812       $ldap->search("(&(objectClass=posixGroup)(gidNumber=".$this->gidNumber."))", array("cn"));
814       /* Create group if it doesn't exist */
815       if ($ldap->count() == 0){
816         $groupdn= preg_replace ('/^'.$this->config->current['DNMODE'].'=[^,]+,'.get_people_ou().'/i', 'cn='.$this->uid.','.get_groups_ou(), $this->dn);
818         $g= new group($this->config, $groupdn);
819         $g->cn= $this->uid;
820         $g->force_gid= 1;
821         $g->gidNumber= $this->gidNumber;
822         $g->description= "Group of user ".$this->givenName." ".$this->sn;
823         $g->save ();
824       }
825     }
827     /* Take care about groupMembership values: add to groups */
828     foreach ($this->groupMembership as $key => $value){
829       $g= new group($this->config, $key);
830       $g->addUser ($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     }
840     /* Remove from groups not listed in groupMembership */
841     foreach ($this->savedGroupMembership as $key => $value){
842       if (!array_key_exists ($key, $this->groupMembership)){
843         $g= new group($this->config, $key);
844         $g->removeUser ($this->uid);
845         $g->save();
847         /* May need to save the mail part, too */
848         if ($g->has_mailAccount){
849           $m= new mailgroup($this->config, $key);
850           $m->save();
851         }
852       }
853     }
855     /* Optionally execute a command after we're done */
856     if ($this->initially_was_account == $this->is_account){
857       if ($this->is_modified){
858         $this->handle_post_events("mofify");
859       }
860     } else {
861       $this->handle_post_events("add");
862     }
863   }
865   /* Check formular input */
866   function check()
867   {
868     /* Include global link_info */
869     $ldap= $this->config->get_ldap_link();
871     /* Reset message array */
872     $message= array();
874     /* must: homeDirectory */
875     if ($this->homeDirectory == ""){
876       $message[]= _("The required field 'Home directory' is not set.");
877     }
878     if (!is_path($this->homeDirectory)){
879       $message[]= _("Please enter a valid path in 'Home directory' field.");
880     }
882     /* Check ID's if they are forced by user */
883     if ($this->force_ids == "1"){
885       /* Valid uid/gid? */
886       if (!is_id($this->uidNumber)){
887         $message[]= _("Value specified as 'UID' is not valid.");
888       } else {
889         if ($this->uidNumber < $this->config->current['MINID']){
890           $message[]= _("Value specified as 'UID' is too small.");
891         }
892       }
893       if (!is_id($this->gidNumber)){
894         $message[]= _("Value specified as 'GID' is not valid.");
895       } else {
896         if ($this->gidNumber < $this->config->current['MINID']){
897           $message[]= _("Value specified as 'GID' is too small.");
898         }
899       }
900     }
902     /* Check shadow settings, well I like spaghetties... */
903     if ($this->use_shadowMin){
904       if (!is_id($this->shadowMin)){
905         $message[]= _("Value specified as 'shadowMin' is not valid.");
906       }
907     }
908     if ($this->use_shadowMax){
909       if (!is_id($this->shadowMax)){
910         $message[]= _("Value specified as 'shadowMax' is not valid.");
911       }
912     }
913     if ($this->use_shadowWarning){
914       if (!is_id($this->shadowWarning)){
915         $message[]= _("Value specified as 'shadowWarning' is not valid.");
916       }
917       if (!$this->use_shadowMax){
918         $message[]= _("'shadowWarning' without 'shadowMax' makes no sense.");
919       }
920       if ($this->shadowWarning > $this->shadowMax){
921         $message[]= _("Value specified as 'shadowWarning' should be smaller than 'shadowMax'.");
922       }
923       if ($this->use_shadowMin && $this->shadowWarning < $this->shadowMin){
924         $message[]= _("Value specified as 'shadowWarning' should be greater than 'shadowMin'.");
925       }
926     }
927     if ($this->use_shadowInactive){
928       if (!is_id($this->shadowInactive)){
929         $message[]= _("Value specified as 'shadowInactive' is not valid.");
930       }
931       if (!$this->use_shadowMax){
932         $message[]= _("'shadowInactive' without 'shadowMax' makes no sense.");
933       }
934     }
935     if ($this->use_shadowMin && $this->use_shadowMax){
936       if ($this->shadowMin > $this->shadowMax){
937         $message[]= _("Value specified as 'shadowMin' should be smaller than 'shadowMax'.");
938       }
939     }
941   //  if(empty($this->gosaDefaultPrinter)){
942   //    $message[]= _("You need to specify a valid default printer.");
943   //  }
945     return ($message);
946   }
948   function addGroup ($groups)
949   {
950     /* include global link_info */
951     $ldap= $this->config->get_ldap_link();
953     /* Walk through groups and add the descriptive entry if not exists */
954     foreach ($groups as $value){
955       if (!array_key_exists($value, $this->groupMembership)){
956         $ldap->cat($value);
957         $attrs= $ldap->fetch();
958         error_reporting (0);
959         if (!isset($attrs['description'][0])){
960           $entry= $attrs["cn"][0];
961         } else {
962           $dsc= preg_replace ('/^Group of user/', _("Group of user"), $attrs["description"][0]);
963           $entry= $attrs["cn"][0]." [$dsc]";
964         }
965         error_reporting (E_ALL);
966         $this->groupMembership[$ldap->getDN()]= $entry;
967       }
968     }
970     /* Sort groups */
971     asort ($this->groupMembership);
972     reset ($this->groupMembership);
973   }
976   /* Del posix user from some groups */
977   function delGroup ($groups)
978   {
979     $dest= array();
981     foreach ($this->groupMembership as $key => $value){
982       if (!in_array($key, $groups)){
983         $dest[$key]= $value;
984       }
985     }
986     $this->groupMembership= $dest;
987   }
989   /* Adapt from template, using 'dn' */
990   function adapt_from_template($dn)
991   {
992     /* Include global link_info */
993     $ldap= $this->config->get_ldap_link();
995     plugin::adapt_from_template($dn);
996     $template= $this->attrs['uid'][0];
998     /* Adapt group membership */
999     $ldap->cd($this->config->current['BASE']);
1000     $ldap->search("(&(objectClass=posixGroup)(memberUid=".$this->attrs["uid"][0]."))", array("description", "cn"));
1002     while ($this->attrs= $ldap->fetch()){
1003       if (!isset($this->attrs["description"][0])){
1004         $entry= $this->attrs["cn"][0];
1005       } else {
1006         $entry= $this->attrs["cn"][0]." [".$this->attrs["description"][0]."]";
1007       }
1008       $this->groupMembership[$ldap->getDN()]= $entry;
1009     }
1011     /* Fix primary group settings */
1012     $ldap->cd($this->config->current['BASE']);
1013     $ldap->search("(&(objectClass=posixGroup)(cn=$template)(gidNumber=".$this->gidNumber."))", array("cn"));
1014     if ($ldap->count() != 1){
1015       $this->primaryGroup= $this->gidNumber;
1016     }
1017   }
1019   function get_next_id($attrib)
1020   {
1021     $ids= array();
1022     $ldap= $this->config->get_ldap_link();
1024     $ldap->cd ($this->config->current['BASE']);
1025     $ldap->search ("($attrib=*)", array("$attrib"));
1027     /* Get list of ids */
1028     while ($attrs= $ldap->fetch()){
1029       $ids[]= (int)$attrs["$attrib"][0];
1030     }
1032     /* Find out next free id near to UID_BASE */
1033     for ($id= $this->config->current['UIDBASE']; $id++; $id<65000){
1034       if (!in_array($id, $ids)){
1035         return ($id);
1036       }
1037     }
1039     /* Should not happen */
1040     if ($id == 65000){
1041       print_red(_("Too many users, can't allocate a free ID!"));
1042       exit;
1043     }
1045   }
1051 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1057 ?>