Code

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