Code

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