Code

7dfec52d2741c1a70368d7c973b1b80793634b34
[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;
62   var $grouplist  = array();
63   var $ui         = array();
65   /* attribute list for save action */
66   var $attributes= array("homeDirectory", "loginShell", "uidNumber", "gidNumber", "gecos",
67       "shadowMin", "shadowMax", "shadowWarning", "shadowInactive", "shadowLastChange",
68       "shadowExpire", "gosaDefaultPrinter", "gosaDefaultLanguage", "uid","accessTo","trustModel");
69   var $objectclasses= array("posixAccount", "shadowAccount");
72   /* constructor, if 'dn' is set, the node loads the given
73      'dn' from LDAP */
74   function posixAccount ($config, $dn= NULL)
75   {
76     /* Configuration is fine, allways */
77     $this->config= $config;
79     /* Load bases attributes */
80     plugin::plugin($config, $dn);
82     $ldap= $this->config->get_ldap_link();
84     if ($dn != NULL){
86       /* Correct is_account. shadowAccount is not required. */
87       if (isset($this->attrs['objectClass']) &&
88           in_array ('posixAccount', $this->attrs['objectClass'])){
90         $this->is_account= TRUE;
91       }
93       /* Is this account a trustAccount? */
94       if ($this->is_account && isset($this->attrs['trustModel'])){
95         $this->trustModel= $this->attrs['trustModel'][0];
96         $this->was_trust_account= TRUE;
97       } else {
98         $this->was_trust_account= FALSE;
99         $this->trustModel= "";
100       }
101          
102           $this->accessTo = array(); 
103       if ($this->is_account && isset($this->attrs['accessTo'])){
104         for ($i= 0; $i<$this->attrs['accessTo']['count']; $i++){
105           $tmp= $this->attrs['accessTo'][$i];
106           $this->accessTo[$tmp]= $tmp;
107         }
108       }
109       $this->initially_was_account= $this->is_account;
111       /* Fill group */
112       $this->primaryGroup= $this->gidNumber;
114       /* Generate status text */
115       $current= date("U");
116       if (($current >= $this->shadowExpire) && $this->shadowExpire){
117         $this->status= "expired";
118         if (($this->shadowExpire - $current) < $this->shadowInactive){
119           $this->status.= ", grace time active";
120         }
121       } elseif (($this->shadowLastChange + $this->shadowMin) >= $current){
122         $this->status= "active, password not changable";
123       } elseif (($this->shadowLastChange + $this->shadowMax) >= $current){
124         $this->status= "active, password expired";
125       } else {
126         $this->status= "active";
127       }
129       /* Get group membership */
130       $ldap->cd($this->config->current['BASE']);
131       $ldap->search("(&(objectClass=posixGroup)(memberUid=".$this->uid."))", array("cn", "description"));
133       while ($this->attrs= $ldap->fetch()){
134         if (!isset($this->attrs["description"][0])){
135           $entry= $this->attrs["cn"][0];
136         } else {
137           $dsc= preg_replace ('/^Group of user/', _("Group of user"), $this->attrs["description"][0]);
138           $entry= $this->attrs["cn"][0]." [$dsc]";
139         }
140         $this->groupMembership[$ldap->getDN()]= $entry;
141       }
142       asort($this->groupMembership);
143       reset($this->groupMembership);
144       $this->savedGroupMembership= $this->groupMembership;
145       $this->savedUidNumber= $this->uidNumber;
146       $this->savedGidNumber= $this->gidNumber;
147     }
149     /* Adjust shadow checkboxes */
150     foreach (array("shadowMin", "shadowMax", "shadowWarning", "shadowInactive",
151           "shadowExpire") as $val){
153       if ($this->$val != 0){
154         $oval= "use_".$val;
155         $this->$oval= "1";
156       }
157     }
159     /* Convert to seconds */
160     if ($this->shadowExpire != 0){
161       $this->shadowExpire*= 60 * 60 * 24;
162     } else {
163       $date= getdate();
164       $this->shadowExpire= floor($date[0] / (60*60*24)) * 60 * 60 * 24;
165     }
167     /* Generate shell list from /etc/gosa/shells */
168     if (file_exists('/etc/gosa/shells')){
169       $shells = file ('/etc/gosa/shells');
170       foreach ($shells as $line){
171         if (!preg_match ("/^#/", $line)){
172           $this->loginShellList[]= trim($line);
173         }
174       }
175     } else {
176       if ($this->loginShell == ""){
177         $this->loginShellList[]= _("unconfigured");
178       }
179     }
181     /* Insert possibly missing loginShell */
182     if ($this->loginShell != "" && !in_array($this->loginShell, $this->loginShellList)){
183       $this->loginShellList[]= $this->loginShell;
184     }
186     /* Generate printer list */
187     if (isset($this->config->data['SERVERS']['CUPS'])){
188       $this->printerList= get_printer_list ($this->config->data['SERVERS']['CUPS']);
189       asort($this->printerList);
190     }
192     /* Generate group list */
193     $ldap->cd($this->config->current['BASE']);
194     $ldap->search("(objectClass=posixGroup)", array("cn", "gidNumber"));
195     $this->secondaryGroups[]= "- "._("automatic")." -";
196     while ($attrs= $ldap->fetch()){
197       $this->secondaryGroups[$attrs['gidNumber'][0]]= $attrs['cn'][0];
198     }
199     asort ($this->secondaryGroups);
201     /* Get global filter config */
202     if (!is_global("sysfilter")){
203       $ui= get_userinfo();
204       $base= get_base_from_people($ui->dn);
205       $sysfilter= array( "depselect"       => $base,
206           "regex"           => "*");
207       register_global("sysfilter", $sysfilter);
208     }
209     $this->ui = get_userinfo();
210   }
213   /* execute generates the html output for this node */
214   function execute()
215   {
216         /* Call parent execute */
217         plugin::execute();
219     /* Do we need to flip is_account state? */
220     if (isset($_POST['modify_state'])){
221       $this->is_account= !$this->is_account;
222     }
224     /* Do we represent a valid posixAccount? */
225     if (!$this->is_account && $this->parent == NULL ){
226       $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\">&nbsp;<b>".
227         _("This account has no unix extensions.")."</b>";
228       $display.= back_to_main();
229       return ($display);
230     }
232     $display= "";
234     /* Show tab dialog headers */
235     if ($this->parent != NULL){
236       if ($this->is_account){
237         if (isset($this->parent->by_object['sambaAccount'])){
238           $obj= $this->parent->by_object['sambaAccount'];
239         }
240         if (isset($obj) && $obj->is_account == TRUE &&
241              ((isset($this->parent->by_object['sambaAccount']))&&($this->parent->by_object['sambaAccount']->is_account))
242                         ||(isset($this->parent->by_object['environment'] ))&&($this->parent->by_object['environment'] ->is_account)){
244           /* Samba3 dependency on posix accounts are enabled
245              in the moment, because I need to rely on unique
246              uidNumbers. There'll be a better solution later
247              on. */
248           $display= $this->show_header(_("Remove posix account"),
249               _("This account has unix features enabled. To disable them, you'll need to remove the samba / environment account first."), TRUE);
250         } else {
251           $display= $this->show_header(_("Remove posix account"),
252               _("This account has posix features enabled. You can disable them by clicking below."));
253         }
254       } else {
255         $display= $this->show_header(_("Create posix account"),
256             _("This account has posix features disabled. You can enable them by clicking below."));
257         return($display);
258       }
259     }
261     /* Trigger group edit? */
262     if (isset($_POST['edit_groupmembership'])){
263       $this->group_dialog= TRUE;
264       $this->dialog= TRUE;
265     }
267     /* Cancel group edit? */
268     if (isset($_POST['add_groups_cancel']) ||
269         isset($_POST['add_groups_finish'])){
270       $this->group_dialog= FALSE;
271       $this->dialog= FALSE;
272     }
274     /* Add selected groups */
275     if (isset($_POST['add_groups_finish']) && isset($_POST['groups']) &&
276         count($_POST['groups'])){
278       if (chkacl ($this->acl, "memberUid") == ""){
279         $this->addGroup ($_POST['groups']);
280         $this->is_modified= TRUE;
281       }
282     }
284     /* Delete selected groups */
285     if (isset($_POST['delete_groupmembership']) && 
286         isset($_POST['group_list']) && count($_POST['group_list'])){
288       if (chkacl ($this->acl, "memberUid") == ""){
289         $this->delGroup ($_POST['group_list']);
290         $this->is_modified= TRUE;
291       }
292     }
294     /* Add user workstation? */
295     if (isset($_POST["add_ws"])){
296       $this->show_ws_dialog= TRUE;
297       $this->dialog= TRUE;
298     }
300     /* Add user workstation? */
301     if (isset($_POST["add_ws_finish"]) && isset($_POST['wslist'])){
302       foreach($_POST['wslist'] as $ws){
303         $this->accessTo[$ws]= $ws;
304       }
305       ksort($this->accessTo);
306       $this->is_modified= TRUE;
307     }
309     /* Remove user workstations? */
310     if (isset($_POST["delete_ws"]) && isset($_POST['workstation_list'])){
311       foreach($_POST['workstation_list'] as $name){
312         unset ($this->accessTo[$name]);
313       }
314       $this->is_modified= TRUE;
315     }
317     /* Add user workstation finished? */
318     if (isset($_POST["add_ws_finish"]) || isset($_POST["add_ws_cancel"])){
319       $this->show_ws_dialog= FALSE;
320       $this->dialog= FALSE;
321     }
323     /* Templates now! */
324     $smarty= get_smarty();
326     /* Show ws dialog */
327     if ($this->show_ws_dialog){
328       /* Save data */
329       $sysfilter= get_global("sysfilter");
330       foreach( array("depselect", "regex") as $type){
331         if (isset($_POST[$type])){
332           $sysfilter[$type]= $_POST[$type];
333         }
334       }
335       if (isset($_GET['search'])){
336         $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
337         if ($s == "**"){
338           $s= "*";
339         }
340         $sysfilter['regex']= $s;
341       }
342       register_global("sysfilter", $sysfilter);
344       /* Get workstation list */
345       $exclude= "";
346       foreach($this->accessTo as $ws){
347         $exclude.= "(cn=$ws)";
348       }
349       if ($exclude != ""){
350         $exclude= "(!(|$exclude))";
351       }
352       $acl= array($this->config->current['BASE'] => ":all");
353       $regex= $sysfilter['regex'];
354       $filter= "(&(|(objectClass=goServer)(objectClass=gotoWorkstation)(objectClass=gotoTerminal))$exclude(cn=*)(cn=$regex))";
355       $res= get_list($acl, "$filter", TRUE, $sysfilter['depselect'], array("cn"), TRUE);
356       $wslist= array();
357       foreach ($res as $attrs){
358         $wslist[]= preg_replace('/\$/', '', $attrs['cn'][0]);
359       }
360       asort($wslist);
361       $smarty->assign("search_image", get_template_path('images/search.png'));
362       $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
363       $smarty->assign("tree_image", get_template_path('images/tree.png'));
364       $smarty->assign("deplist", $this->config->idepartments);
365       $smarty->assign("alphabet", generate_alphabet());
366       foreach( array("depselect", "regex") as $type){
367         $smarty->assign("$type", $sysfilter[$type]);
368       }
369       $smarty->assign("hint", print_sizelimit_warning());
370       $smarty->assign("wslist", $wslist);
371       $smarty->assign("apply", apply_filter());
372       $display= $smarty->fetch (get_template_path('trust_machines.tpl', TRUE, dirname(__FILE__)));
373       return ($display);
374     }
376     /* Manage group add dialog */
377     if ($this->group_dialog){
379       /* Get global filter config */
380       if (!is_global("groupfilter")){
381         $base= get_base_from_people($ui->dn);
382         $groupfilter= array("primarygroups" => "checked",
383             "mailgroups" => "checked",
384             "sambagroups" => "checked",
385             "appgroups" => "checked",
386             "functionalgroups" => "checked",
387             "guser" => "*",
388             "subsearch" => "",
389             "depselect" => $base,
390             "regex" => "*");
391         register_global("groupfilter", $groupfilter);
392       }
394       /* Save data */
395       $groupfilter= get_global("groupfilter");
396       foreach( array("depselect", "guser", "regex") as $type){
397         if (isset($_POST[$type])){
398           $groupfilter[$type]= $_POST[$type];
399         }
400       }
401       if (isset($_POST['depselect'])){
402         foreach( array("primarygroups", "sambagroups", "mailgroups", "appgroups",
403               "functionalgroups") as $type){
405           if (isset($_POST[$type])) {
406             $groupfilter[$type]= "checked";
407           } else {
408             $groupfilter[$type]= "";
409           }
410         }
411       }
412       if (isset($_GET['search'])){
413         $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
414         if ($s == "**"){
415           $s= "*";
416         }
417         $groupfilter['regex']= $s;
418       }
419       register_global("groupfilter", $groupfilter);
421       /* Calculate actual groups */
422                 
423       $this->reload();
424       $glist= array();
425       foreach ($this->grouplist as $key => $value){
426         if (!isset($this->groupMembership[$key])){
427           $glist[$key]= $value;
428         }
429       }
431       $smarty->assign("groups", $glist);
432       $smarty->assign("search_image", get_template_path('images/search.png'));
433       $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
434       $smarty->assign("tree_image", get_template_path('images/tree.png'));
435       $smarty->assign("deplist", $this->config->idepartments);
436       $smarty->assign("alphabet", generate_alphabet());
437       foreach( array("depselect", "guser", "regex", "primarygroups", "mailgroups",
438             "appgroups", "sambagroups", "functionalgroups") as $type){
439         $smarty->assign("$type", $groupfilter[$type]);
440       }
441       $smarty->assign("hint", print_sizelimit_warning());
443       $smarty->assign("apply", apply_filter());
444       $display.= $smarty->fetch (get_template_path('posix_groups.tpl', TRUE, dirname(__FILE__)));
445       return ($display);
446     }
448     /* Show main page */
449     $smarty= get_smarty();
451     /* Depending on pwmode, currently hardcoded because there are no other methods */
452     if ( 1 == 1 ){
453       $smarty->assign("pwmode", dirname(__FILE__)."/posix_shadow");
454       $shadowMinACL= chkacl($this->acl, "shadowMin");
455       $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."\">"));
456       $shadowMaxACL= chkacl($this->acl, "shadowMax");
457       $smarty->assign("shadowmaxs", sprintf(_("Password must be changed after %s days"), "<input name=\"shadowMax\" size=3 maxlength=4 $shadowMaxACL value=\"".$this->shadowMax."\">"));
458       $shadowInactiveACL= chkacl($this->acl, "shadowInactive");
459       $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."\">"));
460       $shadowWarningACL= chkacl($this->acl, "shadowWarning");
461       $smarty->assign("shadowwarnings", sprintf(_("Warn user %s days before password expiery"), "<input name=\"shadowWarning\" size=3 maxlength=4 $shadowWarningACL value=\"".$this->shadowWarning."\">"));
462       foreach( array("must_change_password", "use_shadowMin", "use_shadowMax",
463             "use_shadowExpire", "use_shadowInactive",
464             "use_shadowWarning") as $val){
465         if ($this->$val == 1){
466           $smarty->assign("$val", "checked");
467         } else {
468           $smarty->assign("$val", "");
469         }
470         $smarty->assign("$val"."ACL", chkacl($this->acl, $val));
471       }
472     }
474     /* Fill calendar */
475     $date= getdate($this->shadowExpire);
477     $days= array();
478     for($d= 1; $d<32; $d++){
479       $days[$d]= $d;
480     }
481     $years= array();
482     for($y= $date['year']-10; $y<$date['year']+10; $y++){
483       $years[]= $y;
484     }
485     $months= array(_("January"), _("February"), _("March"), _("April"),
486         _("May"), _("June"), _("July"), _("August"), _("September"),
487         _("October"), _("November"), _("December"));
488     $smarty->assign("day", $date["mday"]);
489     $smarty->assign("days", $days);
490     $smarty->assign("months", $months);
491     $smarty->assign("month", $date["mon"]-1);
492     $smarty->assign("years", $years);
493     $smarty->assign("year", $date["year"]);
495     /* Fill arrays */
496     $smarty->assign("shells", $this->loginShellList);
497     $smarty->assign("secondaryGroups", $this->secondaryGroups);
498     $smarty->assign("primaryGroup", $this->primaryGroup);
499     if (!count($this->groupMembership)){
500       $smarty->assign("groupMembership", array("&nbsp;"));
501     } else {
502       $smarty->assign("groupMembership", $this->groupMembership);
503     }
504     if (count($this->groupMembership) > 16){
505       $smarty->assign("groups", "too_many_for_nfs");
506     } else {
507       $smarty->assign("groups", "");
508     }
509     $smarty->assign("printerList", $this->printerList);
510     $smarty->assign("languages", $this->config->data['MAIN']['LANGUAGES']);
512         /* Avoid "Undefined index: forceMode" */
513     $smarty->assign("forceMode", "");
515     /* Checkboxes */
516     if ($this->force_ids == 1){
517       $smarty->assign("force_ids", "checked");
518       if ($_SESSION['js']){
519         $smarty->assign("forceMode", "");
520       }
521     } else {
522       if ($_SESSION['js']){
523                 if($this->acl != "#none#")
524         $smarty->assign("forceMode", "disabled");
525       }
526       $smarty->assign("force_ids", "");
527     }
528     $smarty->assign("force_idsACL", chkacl($this->acl, "force_ids"));
530     /* Load attributes and acl's */
531     foreach($this->attributes as $val){
532       if((chkacl($this->acl,$val)=="")&&(($_SESSION["js"])&&(($val=="uidNumber")||($val=="gidNumber"))))
533         {
534           $smarty->assign("$val"."ACL","");
535           $smarty->assign("$val", $this->$val);
536           continue;
537         }
538       $smarty->assign("$val", $this->$val);
539       $smarty->assign("$val"."ACL", chkacl($this->acl,$val));
540     }
541     $smarty->assign("groupMembershipACL", chkacl($this->acl, "groupMembership"));
542     $smarty->assign("status", $this->status);
544     /* Work on trust modes */
545     $smarty->assign("trustmodeACL", chkacl($this->acl, "trustmode"));
546     if ($this->trustModel == "fullaccess"){
547       $trustmode= 1;
548       // pervent double disable tag in html code, this will disturb our clean w3c html
549     
550     if(chkacl($this->acl, "trustmode")==""){
551           $smarty->assign("trusthide", "disabled");
552       }else{
553           $smarty->assign("trusthide", "");
554       }
556     } elseif ($this->trustModel == "byhost"){
557       $trustmode= 2;
558       $smarty->assign("trusthide", "");
559     } else {
560       // pervent double disable tag in html code, this will disturb our clean w3c html
561       if(chkacl($this->acl, "trustmode")==""){
562           $smarty->assign("trusthide", "disabled");
563       }else{
564           $smarty->assign("trusthide", "");
565       }
566       $trustmode= 0;
567     }
568     $smarty->assign("trustmode", $trustmode);
569     $smarty->assign("trustmodes", array( 0 => _("disabled"), 1 => _("full access"),
570           2 => _("allow access to these hosts")));
574     if((count($this->accessTo))==0)
575       $smarty->assign("emptyArrAccess",true);
576     else
577       $smarty->assign("emptyArrAccess",false);
578     
581     $smarty->assign("workstations", $this->accessTo);
583     $smarty->assign("apply", apply_filter());
584     $display.= $smarty->fetch (get_template_path('generic.tpl', TRUE, dirname(__FILE__)));
585     return($display);
586   }
589   /* remove object from parent */
590   function remove_from_parent()
591   {
592     /* Cancel if there's nothing to do here */
593     if (!$this->initially_was_account){
594       return;
595     }
597     /* include global link_info */
598     $ldap= $this->config->get_ldap_link();
599     
600         /* Remove and write to LDAP */
601     plugin::remove_from_parent();
603     /* Zero out array */
604     $this->attrs['gosaHostACL']= array();
606     /* Keep uid, because we need it for authentification! */
607     unset($this->attrs['uid']);
608     unset($this->attrs['trustModel']);
610     @DEBUG (DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__,
611         $this->attributes, "Save");
612     $ldap->cd($this->dn);
613     $this->cleanup();
614 $ldap->modify ($this->attrs); 
616     show_ldap_error($ldap->get_error());
618     /* Delete group only if cn is uid and there are no other
619        members inside */
620     $ldap->cd ($this->config->current['BASE']);
621     $ldap->search ("(&(objectClass=posixGroup)(gidNumber=".$this->gidNumber."))", array("cn", "memberUid"));
622     if ($ldap->count() != 0){
623       $attrs= $ldap->fetch();
624       if ($attrs['cn'][0] == $this->uid &&
625           !isset($this->attrs['memberUid'])){
627         $ldap->rmDir($ldap->getDN());
628       }
629     }
631     /* Optionally execute a command after we're done */
632     $this->handle_post_events("remove");
633   }
636   function save_object()
637   {
638     if (isset($_POST['posixTab'])){
639       /* Save values to object */
640       plugin::save_object();
642       /* Save force GID attribute */
643       if (chkacl ($this->acl, "force_ids") == ""){
644         if (isset ($_POST['force_ids'])){
645           $data= 1;
646         } else {
647           $data= 0;
648         }
649         if ($this->force_ids != $data){
650           $this->is_modified= TRUE;
651         }
652         $this->force_ids= $data;
654         $data= $_POST['primaryGroup'];
655         if ($this->primaryGroup != $data){
656           $this->is_modified= TRUE;
657         }
658         $this->primaryGroup= $_POST['primaryGroup'];
659       }
661       /* Save pwmode dependent attributes, curently hardcoded because there're
662          no alternatives */
663       if (1 == 1){
664         foreach( array("must_change_password", "use_shadowMin", "use_shadowMax",
665               "use_shadowExpire", "use_shadowInactive",
666               "use_shadowWarning") as $val){
667           if (chkacl($this->acl, "$val") == ""){
668             if (isset ($_POST[$val])){
669               $data= 1;
670             } else {
671               $data= 0;
672             }
673             if ($data != $this->$val){
674               $this->is_modified= TRUE;
675             }
676             $this->$val= $data;
677           }
678         }
679       }
681       /* Trust mode - special handling */
682       if (isset($_POST['trustmode'])){
683         $saved= $this->trustModel;
684         if ($_POST['trustmode'] == "1"){
685           $this->trustModel= "fullaccess";
686         } elseif ($_POST['trustmode'] == "2"){
687           $this->trustModel= "byhost";
688         } else {
689           $this->trustModel= "";
690         }
691         if ($this->trustModel != $saved){
692           $this->is_modified= TRUE;
693         }
694       }
695     }
696   }
699   /* Save data to LDAP, depending on is_account we save or delete */
700   function save()
701   {
702         
703     /* include global link_info */
704     $ldap= $this->config->get_ldap_link();
706     /* Adapt shadow values */
707     if (!$this->use_shadowExpire){
708       $this->shadowExpire= "0";
709     } else {
710       /* Transform seconds to days here */
711       $this->shadowExpire= (int)($this->shadowExpire / (60 * 60 * 24)) ;
712     }
713     if (!$this->use_shadowMax){
714       $this->shadowMax= "0";
715     }
716     if ($this->must_change_password){
717       $this->shadowLastChange= (int)(date("U") / 86400) - $this->shadowMax - 1;
718     } else {
719       $this->shadowLastChange= (int)(date("U") / 86400);
720     }
721     if (!$this->use_shadowWarning){
722       $this->shadowWarning= "0";
723     }
725     /* Check what to do with ID's */
726     if ($this->force_ids == 0){
728       /* Use id's that are already set */
729       if ($this->savedUidNumber != ""){
730         $this->uidNumber= $this->savedUidNumber;
731         $this->gidNumber= $this->savedGidNumber;
732       } else {
734         /* Calculate new id's. We need to place a lock before calling get_next_id
735            to get real unique values. */
736         $wait= 10;
737         while (get_lock("uidnumber") != ""){
738           sleep (1);
740           /* Oups - timed out */
741           if ($wait-- == 0){
742             print_red (_("Failed: overriding lock"));
743             break;
744           }
745         }
747         add_lock ("uidnumber", "gosa");
748         $this->uidNumber= $this->get_next_id("uidNumber");
749         if ($this->savedGidNumber != ""){
750           $this->gidNumber= $this->savedGidNumber;
751         } else {
752           $this->gidNumber= $this->get_next_id("gidNumber");
753         }
754       }
756       if ($this->primaryGroup != 0){
757         $this->gidNumber= $this->primaryGroup;
758       }
759     }
761     if ($this->use_shadowMin != "1" ) {
762       $this->shadowMin = "";
763     }
765     if (($this->use_shadowMax != "1") && ($this->must_change_password != "1")) {
766       $this->shadowMax = "";
767     }
769     if ($this->use_shadowWarning != "1" ) {
770       $this->shadowWarning = "";
771     }
773     if ($this->use_shadowInactive != "1" ) {
774       $this->shadowInactive = "";
775     }
777     if ($this->use_shadowExpire != "1" ) {
778       $this->shadowExpire = "";
779     }
781     /* Fill gecos */
782     if (isset($this->parent) && $this->parent != NULL){
783       $this->gecos= rewrite($this->parent->by_object['user']->cn);
784       if (!preg_match('/[a-z0-9 -]/i', $this->gecos)){
785         $this->gecos= "";
786       }
787     }
789         foreach(array("shadowMin","shadowMax","shadowWarning","shadowInactive","shadowExpire") as $attr){
790                 $this->$attr = (int) $this->$attr;
791         }
792     /* Call parents save to prepare $this->attrs */
793     plugin::save();
795     /* Trust accounts */
796     $objectclasses= array();
797     foreach ($this->attrs['objectClass'] as $key => $class){
798       if (preg_match('/trustAccount/i', $class)){
799         continue;
800       }
801       $objectclasses[]= $this->attrs['objectClass'][$key];
802     }
803     $this->attrs['objectClass']= $objectclasses;
804     if ($this->trustModel != ""){
805       $this->attrs['objectClass'][]= "trustAccount";
806       $this->attrs['trustModel']= $this->trustModel;
807       $this->attrs['accessTo']= array();
808       if ($this->trustModel == "byhost"){
809         foreach ($this->accessTo as $host){
810           $this->attrs['accessTo'][]= $host;
811         }
812       }
813     } else {
814       if ($this->was_trust_account){
815         $this->attrs['accessTo']= array();
816         $this->attrs['trustModel']= array();
817       }
818     }
820     if(empty($this->attrs['gosaDefaultPrinter'])){
821       $thid->attrs['gosaDefaultPrinter']=array();
822     }
825     /* Save data to LDAP */
826     $ldap->cd($this->dn);
827     $this->cleanup();
828     unset($this->attrs['uid']);
829     $ldap->modify ($this->attrs); 
831     show_ldap_error($ldap->get_error());
833     /* Remove lock needed for unique id generation */
834     del_lock ("uidnumber");
837     /* Posix accounts have group interrelationship, take care about these here. */
838     if ($this->force_ids == 0 && $this->primaryGroup == 0){
839       $ldap->cd($this->config->current['BASE']);
840       $ldap->search("(&(objectClass=posixGroup)(gidNumber=".$this->gidNumber."))", array("cn"));
842       /* Create group if it doesn't exist */
843       if ($ldap->count() == 0){
844         $groupdn= preg_replace ('/^'.$this->config->current['DNMODE'].'=[^,]+,'.get_people_ou().'/i', 'cn='.$this->uid.','.get_groups_ou(), $this->dn);
846         $g= new group($this->config, $groupdn);
847         $g->cn= $this->uid;
848         $g->force_gid= 1;
849         $g->gidNumber= $this->gidNumber;
850         $g->description= "Group of user ".$this->givenName." ".$this->sn;
851         $g->save ();
852       }
853     }
854  
855     /* Take care about groupMembership values: add to groups */
856     foreach ($this->groupMembership as $key => $value){
857       $g= new group($this->config, $key);
858       $g->addUser ($this->uid);
859       $g->save();
861       /* May need to save the mail part, too */
862       if ($g->has_mailAccount){
863         $g= new grouptabs($this->config,$this->config->data['TABS']['GROUPTABS'], $key);
864         $g->by_object['group']->removeUser ($this->uid);
865         $g->save();
866       }
867     }
868     
869     /* Remove from groups not listed in groupMembership */
870     foreach ($this->savedGroupMembership as $key => $value){
871       if (!isset($this->groupMembership[$key])){
872         
873         $g= new group($this->config, $key);
874         $g->removeUser ($this->uid);
875         $g->save();
877         /* May need to save the mail part, too */
878         if ($g->has_mailAccount){
879           $g= new grouptabs($this->config,$this->config->data['TABS']['GROUPTABS'], $key);
880           $g->by_object['group']->removeUser ($this->uid);
881           $g->save();
882         }
883       }
884     }
886     /* Optionally execute a command after we're done */
887     if ($this->initially_was_account == $this->is_account){
888       if ($this->is_modified){
889         $this->handle_post_events("mofify");
890       }
891     } else {
892       $this->handle_post_events("add");
893     }
894   }
896   /* Check formular input */
897   function check()
898   {
899     /* Include global link_info */
900     $ldap= $this->config->get_ldap_link();
902     /* Reset message array */
903     $message= array();
905     /* must: homeDirectory */
906     if ($this->homeDirectory == ""){
907       $message[]= _("The required field 'Home directory' is not set.");
908     }
909     if (!is_path($this->homeDirectory)){
910       $message[]= _("Please enter a valid path in 'Home directory' field.");
911     }
913     /* Check ID's if they are forced by user */
914     if ($this->force_ids == "1"){
916       /* Valid uid/gid? */
917       if (!is_id($this->uidNumber)){
918         $message[]= _("Value specified as 'UID' is not valid.");
919       } else {
920         if ($this->uidNumber < $this->config->current['MINID']){
921           $message[]= _("Value specified as 'UID' is too small.");
922         }
923       }
924       if (!is_id($this->gidNumber)){
925         $message[]= _("Value specified as 'GID' is not valid.");
926       } else {
927         if ($this->gidNumber < $this->config->current['MINID']){
928           $message[]= _("Value specified as 'GID' is too small.");
929         }
930       }
931     }
933     /* Check shadow settings, well I like spaghetties... */
934     if ($this->use_shadowMin){
935       if (!is_id($this->shadowMin)){
936         $message[]= _("Value specified as 'shadowMin' is not valid.");
937       }
938     }
939     if ($this->use_shadowMax){
940       if (!is_id($this->shadowMax)){
941         $message[]= _("Value specified as 'shadowMax' is not valid.");
942       }
943     }
944     if ($this->use_shadowWarning){
945       if (!is_id($this->shadowWarning)){
946         $message[]= _("Value specified as 'shadowWarning' is not valid.");
947       }
948       if (!$this->use_shadowMax){
949         $message[]= _("'shadowWarning' without 'shadowMax' makes no sense.");
950       }
951       if ($this->shadowWarning > $this->shadowMax){
952         $message[]= _("Value specified as 'shadowWarning' should be smaller than 'shadowMax'.");
953       }
954       if ($this->use_shadowMin && $this->shadowWarning < $this->shadowMin){
955         $message[]= _("Value specified as 'shadowWarning' should be greater than 'shadowMin'.");
956       }
957     }
958     if ($this->use_shadowInactive){
959       if (!is_id($this->shadowInactive)){
960         $message[]= _("Value specified as 'shadowInactive' is not valid.");
961       }
962       if (!$this->use_shadowMax){
963         $message[]= _("'shadowInactive' without 'shadowMax' makes no sense.");
964       }
965     }
966     if ($this->use_shadowMin && $this->use_shadowMax){
967       if ($this->shadowMin > $this->shadowMax){
968         $message[]= _("Value specified as 'shadowMin' should be smaller than 'shadowMax'.");
969       }
970     }
972   //  if(empty($this->gosaDefaultPrinter)){
973   //    $message[]= _("You need to specify a valid default printer.");
974   //  }
976     return ($message);
977   }
979   function addGroup ($groups)
980   {
981     /* include global link_info */
982     $ldap= $this->config->get_ldap_link();
984     /* Walk through groups and add the descriptive entry if not exists */
985     foreach ($groups as $value){
986       if (!array_key_exists($value, $this->groupMembership)){
987         $ldap->cat($value);
988         $attrs= $ldap->fetch();
989         error_reporting (0);
990         if (!isset($attrs['description'][0])){
991           $entry= $attrs["cn"][0];
992         } else {
993           $dsc= preg_replace ('/^Group of user/', _("Group of user"), $attrs["description"][0]);
994           $entry= $attrs["cn"][0]." [$dsc]";
995         }
996         error_reporting (E_ALL);
997         $this->groupMembership[$ldap->getDN()]= $entry;
998       }
999     }
1001     /* Sort groups */
1002     asort ($this->groupMembership);
1003     reset ($this->groupMembership);
1004   }
1007   /* Del posix user from some groups */
1008   function delGroup ($groups)
1009   {
1010     $dest= array();
1012     foreach ($this->groupMembership as $key => $value){
1013       if (!in_array($key, $groups)){
1014         $dest[$key]= $value;
1015       }
1016     }
1017     $this->groupMembership= $dest;
1018   }
1020   /* Adapt from template, using 'dn' */
1021   function adapt_from_template($dn)
1022   {
1023     /* Include global link_info */
1024     $ldap= $this->config->get_ldap_link();
1026     plugin::adapt_from_template($dn);
1027     $template= $this->attrs['uid'][0];
1029     /* Adapt group membership */
1030     $ldap->cd($this->config->current['BASE']);
1031     $ldap->search("(&(objectClass=posixGroup)(memberUid=".$this->attrs["uid"][0]."))", array("description", "cn"));
1033     while ($this->attrs= $ldap->fetch()){
1034       if (!isset($this->attrs["description"][0])){
1035         $entry= $this->attrs["cn"][0];
1036       } else {
1037         $entry= $this->attrs["cn"][0]." [".$this->attrs["description"][0]."]";
1038       }
1039       $this->groupMembership[$ldap->getDN()]= $entry;
1040     }
1042     /* Fix primary group settings */
1043     $ldap->cd($this->config->current['BASE']);
1044     $ldap->search("(&(objectClass=posixGroup)(cn=$template)(gidNumber=".$this->gidNumber."))", array("cn"));
1045     if ($ldap->count() != 1){
1046       $this->primaryGroup= $this->gidNumber;
1047     }
1049         $ldap->cd($this->config->current['BASE']);
1050     $ldap->search("(&(objectClass=gosaUserTemplate)(uid=".$template."))", array("cn","accessTo"));
1051         while($attr = $ldap->fetch()){
1052                 $tmp = $attr['accessTo'];
1053                 unset ($tmp['count']);
1054                 $this->accessTo = $tmp; 
1055         }
1056         
1057     /* Adjust shadow checkboxes */
1058     foreach (array("shadowMin", "shadowMax", "shadowWarning", "shadowInactive",
1059           "shadowExpire") as $val){
1061       if ($this->$val != 0){
1062         $oval= "use_".$val;
1063         $this->$oval= "1";
1064       }
1065     }
1066   }
1068   function get_next_id($attrib)
1069   {
1070     $ids= array();
1071     $ldap= $this->config->get_ldap_link();
1073     $ldap->cd ($this->config->current['BASE']);
1074     if (preg_match('/gidNumber/i', $attrib)){
1075       $oc= "posixGroup";
1076     } else {
1077       $oc= "posixAccount";
1078     }
1079     $ldap->search ("(&(objectClass=$oc)($attrib=*))", array("$attrib"));
1081     /* Get list of ids */
1082     while ($attrs= $ldap->fetch()){
1083       $ids[]= (int)$attrs["$attrib"][0];
1084     }
1086     /* Find out next free id near to UID_BASE */
1087     for ($id= $this->config->current['UIDBASE']; $id++; $id<65000){
1088       if (!in_array($id, $ids)){
1089         return ($id);
1090       }
1091     }
1093     /* Should not happen */
1094     if ($id == 65000){
1095       print_red(_("Too many users, can't allocate a free ID!"));
1096       exit;
1097     }
1099   }
1101  function reload()
1102   {
1104     /* Get config */
1105     $groupfilter= get_global('groupfilter');
1107     /* Set base for all searches */
1108     $base= $groupfilter['depselect'];
1110     /* Regex filter? */
1111     if ($groupfilter['regex'] != ""){
1112       $regex= $groupfilter['regex'];
1113     } else {
1114       $regex= "*";
1115     }
1117     $error = "";
1118     $ldap = $this->config->get_ldap_link();    
1120     $base= get_groups_ou().$base;
1121     $res= get_list($this->ui->subtreeACL, "(objectClass=posixGroup)", FALSE, $base, array("cn", "description", "gidNumber"), TRUE);
1122     if (preg_match("/size limit/i", $error)){
1123       $_SESSION['limit_exceeded']= TRUE;
1124     }
1126     $error = $ldap->error;
1127     $this->grouplist = array();
1128     foreach ($res as $value){
1129         $this->grouplist[$value['gidNumber'][0]]= $value;
1130     }
1132     $tmp=array();
1133     foreach($this->grouplist as $tkey => $val ){
1134       $tmp[strtolower($val['cn'][0]).$val['cn'][0]]=$val;
1135     }
1137     /* Sort index */
1138     ksort($tmp);
1140     /* Recreate index array[dn]=cn[description]*/
1141     $this->grouplist=array();
1142     foreach($tmp as $val){
1143       if(isset($val['description'])){
1144         $this->grouplist[$val['dn']]=$val['cn'][0]."&nbsp;[".$val['description'][0]."]";
1145       }else{
1146         $this->grouplist[$val['dn']]=$val['cn'][0];
1147       }
1148     }
1149     reset ($this->grouplist);
1150   }
1152   function getCopyDialog()
1153   {
1154     if(!$this->is_account) return("");
1155     if ($this->force_ids == 1){
1156       $force_ids = "checked";
1157       if ($_SESSION['js']){
1158         $forceMode = "";
1159       }
1160     } else {
1161       if ($_SESSION['js']){
1162         if($this->acl != "#none#")
1163           $forceMode ="disabled";
1164       }
1165       $force_ids = "";
1166     }
1168   /*
1169     if(isset($_POST['editGroupMembership'])){
1170       $this->group_dialog = TRUE;
1171     }
1173     if(isset($_POST['add_groups_finish']) || isset($_POST['add_groups_cancel'])){
1174       $this->execute();
1175       $this->group_dialog =FALSE;
1176     }
1178     if($this->group_dialog){
1179       return($this->execute());
1180     }
1181   */
1183     $smarty = get_smarty();
1184     $smarty->assign("homeDirectory",$this->homeDirectory);
1185     $smarty->assign("uidNumber",$this->uidNumber);
1186     $smarty->assign("gidNumber",$this->gidNumber);
1187     $smarty->assign("forceMode",$forceMode);
1188     $smarty->assign("force_ids",$force_ids);
1189     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE,dirname(__FILE__)));
1190     return($str);
1191   }
1195 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1196 ?>