Code

Prepared for multiple asterisk servers
[gosa.git] / plugins / personal / samba / class_sambaAccount.inc
1 <?php
2 /*
3    This code is part of GOsa (https://gosa.gonicus.de)
4    Copyright (C) 2004-2005 Cajus Pollmeier
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
21 class sambaAccount extends plugin
22 {
23   /* Definitions */
24   var $plHeadline= "Samba";
25   var $plDescription= "This does something";
27   /* CLI vars */
28   var $cli_summary= "Manage users samba account";
29   var $cli_description= "Some longer text\nfor help";
30   var $cli_parameters= array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
32   /* Switch for Samba version */
33   var $samba3= FALSE;
34   var $uidNumber= 65535;
35   var $gidNumber= 65535;
37   /* Samba 2 attributes */
38   var $pwdLastSet= "0";
39   var $logonTime= "0";
40   var $logoffTime= "2147483647";
41   var $kickoffTime= "2147483647";
42   var $pwdCanChange= "0";
43   var $pwdMustChange= "0";
44   var $password_expires= 0;
45   var $acctFlags= "[UX        ]";
46   var $smbHome= "";
47   var $homeDrive= "";
48   var $scriptPath= "";
49   var $profilePath= "";
50   var $rid= "";
51   var $primaryGroupID= "";
53   /* Samba 3 attributes */
54   var $SID= "";
55   var $ridBase= 0;
56   var $sambaSID= "";
57   var $sambaPwdLastSet= "0";
58   var $sambaLogonTime= "0";
59   var $sambaLogoffTime= "2147483647";
60   var $sambaKickoffTime= "2147483647";
61   var $sambaPwdCanChange= "";
62   var $sambaPwdMustChange= "0";
63   var $sambaAcctFlags= "[UX        ]";
64   var $sambaHomePath= "";
65   var $sambaHomeDrive= "";
66   var $sambaLogonScript= "";
67   var $sambaProfilePath= "";
68   var $sambaPrimaryGroupSID= "";
69   var $sambaDomainName= "";
70   var $sambaUserWorkstations= "";
71   var $sambaBadPasswordCount= "";
72   var $sambaBadPasswordTime= "";
73   var $sambaPasswordHistory= "";
74   var $sambaLogonHours= "";
75   var $orig_sambaDomainName= "";
76   var $sambaMungedDial= "";
77   var $mungedObject;
79   /* Helper */
80   var $show_ws_dialog= FALSE;
81   var $logon_time_set= 0;
82   var $logoff_time_set= 0;
83   var $kickoff_time_set= 0;
85   /* attribute list for save action */
86   var $ctxattributes= array();
87   var $attributes= array();
88   var $objectclasses= array();
89   
90   var $uid= "";
92   var $CopyPasteVars = array("kickoff_time_set","logoff_time_set","logon_time_set","mungedObject","orig_sambaDomainName");
94   function sambaAccount ($config, $dn= NULL)
95   {
96     /* Load attributes depending on the samba version */
97     $this->samba3= ($config->current['SAMBAVERSION'] == 3);
99     if ($this->samba3){
100       $this->attributes= array ("sambaSID", "sambaPwdLastSet", "sambaLogonTime",
101           "sambaLogoffTime", "sambaKickoffTime", "sambaPwdCanChange",
102           "sambaPwdMustChange", "sambaAcctFlags", "uid", "sambaMungedDial",
103           "sambaHomePath", "sambaHomeDrive", "sambaLogonScript",
104           "sambaProfilePath", "sambaPrimaryGroupSID", "sambaDomainName",
105           "sambaUserWorkstations", "sambaPasswordHistory",
106           "sambaLogonHours", "sambaBadPasswordTime",
107           "sambaBadPasswordCount");
108       $this->objectclasses= array ("sambaSamAccount");
109       $this->mungedObject= new sambaMungedDial;
110       $this->ctxattributes= $this->mungedObject->ctxattributes;
111     } else {
112       $this->attributes= array ("pwdLastSet", "logonTime", "logoffTime", "kickoffTime",
113           "pwdCanChange", "pwdMustChange", "acctFlags", "profilePath", "uid",
114           "smbHome", "homeDrive", "scriptPath", "rid", "primaryGroupID");
115       $this->objectclasses= array ("sambaAccount");
116     }
118     plugin::plugin ($config, $dn);
120     /* Setting uid to default */
121     if(isset($this->attrs['uid'][0])){
122       $this->uid = $this->attrs['uid'][0];
123     }
125     /* Get samba Domain in case of samba 3 */
126     if ($this->samba3 && $this->sambaSID != ""){
127       $this->SID= preg_replace ("/-[^-]+$/", "", $this->sambaSID);
128       $ldap= $this->config->get_ldap_link();
129       $ldap->cd($this->config->current['BASE']);
130       $ldap->search ("(&(objectClass=sambaDomain)(sambaSID=$this->SID))",array("sambaAlgorithmicRidBase","sambaDomainName"));
131       if ($ldap->count() != 0){
132         $attrs= $ldap->fetch();
133         $this->ridBase= $attrs['sambaAlgorithmicRidBase'][0];
134         if ($this->sambaDomainName == ""){
135           $this->sambaDomainName= $attrs['sambaDomainName'][0];
136         }
137       } else {
138         if ($this->sambaDomainName == ""){
139           $this->sambaDomainName= "DEFAULT";
140         }
141         $this->ridBase= $this->config->current['RIDBASE'];
142         $this->SID= $this->config->current['SID'];
143       }
145       /* Save in order to compare later on */
146       $this->orig_sambaDomainName= $this->sambaDomainName;
147     }
149     /* Fill mungedDial field */
150     if ($this->samba3 && isset($this->attrs['sambaMungedDial'])){
151       $this->mungedObject->load($this->sambaMungedDial);
152     }
154     /* Password expiery */
155     if(isset($this->attrs['sambaPwdMustChange']) &&
156         $this->attrs['sambaPwdMustChange'][0] != 0){
157       $this->password_expires= 1;
158     }
160     if(isset($this->attrs['sambaLogonTime']) && ! (
161         $this->attrs['sambaLogonTime'][0] == 0 ||
162         $this->attrs['sambaLogonTime'][0] == 2147483647
163       )){
164       $this->logon_time_set= 1;
165     }
166     if(isset($this->attrs['sambaLogoffTime']) && ! (
167         $this->attrs['sambaLogoffTime'][0] == 0 ||
168         $this->attrs['sambaLogoffTime'][0] == 2147483647
169       )){
170       $this->logoff_time_set= 1;
171     }
172     
173     /* Account expiery */
174     if(isset($this->attrs['sambaKickoffTime']) && ! (
175         $this->attrs['sambaKickoffTime'][0] == 0 ||
176         $this->attrs['sambaKickoffTime'][0] == 2147483647
177       )){
178       $this->kickoff_time_set= 1;
179     }
181     /* Get global filter config */
182     if (!is_global("sambafilter")){
183       $ui= get_userinfo();
184       $base= get_base_from_people($ui->dn);
185       $sambafilter= array( "depselect" => $base, "regex" => "*");
186       register_global("sambafilter", $sambafilter);
187     }
189     /* Save initial account state */
190     $this->initially_was_account= $this->is_account;
191   }
193   function execute()
194   {
195         /* Call parent execute */
196         plugin::execute();
198     /* Do we need to flip is_account state? */
199     if (isset($_POST['modify_state'])){
200       $this->is_account= !$this->is_account;
201     }
202     /* Do we represent a valid account? */
203     if (!$this->is_account && $this->parent == NULL){
204       $display= "<img alt=\"\"src=\"images/stop.png\" align=\"middle\">&nbsp;<b>".
205         _("This account has no samba extensions.")."</b>";
206       $display.= back_to_main();
207       return ($display);
208     }
210     /* Show tab dialog headers */
211     $display= "";
212     if ($this->parent != NULL){
213       if ($this->is_account){
214         $display= $this->show_disable_header(_("Remove samba account"),
215             _("This account has samba features enabled. You can disable them by clicking below."));
216       } else {
217         $obj= $this->parent->by_object['posixAccount'];
219         /* Samba3 dependency on posix accounts are enabled
220            in the moment, because I need to rely on unique
221            uidNumbers. There'll be a better solution later
222            on. */
223         if ($obj->is_account){
225           $display= $this->show_enable_header(_("Create samba account"),
226               _("This account has samba features disabled. You can enable them by clicking below."));
227         } else {
228           $display= $this->show_enable_header(_("Create samba account"),
229               _("This account has samba features disabled. Posix features are needed for samba accounts, enable them first."), TRUE);
230         }
231         return ($display);
232       }
233     }
235     $SkipWrite = (!isset($this->parent) || !$this->parent) && !isset($_SESSION['edit']);
237     /* Prepare templating */
238     $smarty= get_smarty();
240     $tmp = $this->plInfo();
241     foreach($tmp['plProvidedAcls'] as $var => $rest){
242       $smarty->assign($var."ACL",$this->getacl($var,$SkipWrite));
243     }
245     if ($this->sambaPwdMustChange=="0"){
246       $date= getdate();
247     } else {
248       $date= getdate($this->sambaPwdMustChange);
249     }
251     if ($this->sambaLogonTime=="2147483647" || $this->sambaLogonTime=="0"){
252       $sambaLogonTime_date= getdate();
253     } else {
254       $sambaLogonTime_date= getdate($this->sambaLogonTime);
255     }
256     
257     if ($this->sambaLogoffTime=="2147483647" || $this->sambaLogoffTime=="0"){
258       $sambaLogoffTime_date= getdate();
259     } else {
260       $sambaLogoffTime_date= getdate($this->sambaLogoffTime);
261     }
262     
263     if ($this->sambaKickoffTime=="2147483647" || $this->sambaKickoffTime=="0"){
264       $sambaKickoffTime_date= getdate();
265     } else {
266       $sambaKickoffTime_date= getdate($this->sambaKickoffTime);
267     }
269     /* Remove user workstations? */
270     if (isset($_POST["delete_ws"]) && isset($_POST['workstation_list'])){
272       if($this->acl_is_writeable("sambaUserWorkstations",$SkipWrite)){
274         $tmp= $this->sambaUserWorkstations;
275         foreach($_POST['workstation_list'] as $name){
276           $tmp= preg_replace("/$name/", '', $tmp);
277           $this->is_modified= TRUE;
278         }
279         $tmp= preg_replace('/,+/', ',', $tmp);
280         $this->sambaUserWorkstations= trim($tmp, ',');
281       }
282     }
284     /* Add user workstation? */
285     if (isset($_POST["add_ws"])){
286       if($this->acl_is_writeable("sambaUserWorkstations",$SkipWrite)){
287         $this->show_ws_dialog= TRUE;
288         $this->dialog= TRUE;
289       }
290     }
292     /* Add user workstation finished? */
293     if (isset($_POST["add_ws_finish"]) || isset($_POST["add_ws_cancel"])){
294       $this->show_ws_dialog= FALSE;
295       $this->dialog= FALSE;
296     }
298     /* Add user workstation? */
299     if (isset($_POST["add_ws_finish"]) && isset($_POST['wslist'])){
300       $tmp= $this->sambaUserWorkstations;
301       foreach($_POST['wslist'] as $ws){
302         $tmp.= ",$ws";
303       }
304       $tmp= preg_replace('/,+/', ',', $tmp);
305       $this->sambaUserWorkstations= trim($tmp, ',');
306       $this->is_modified= TRUE;
307     }
309     /* Show ws dialog */
310     if ($this->show_ws_dialog){
312       /* Save data */
313       $sambafilter= get_global("sambafilter");
314       foreach( array("depselect", "regex") as $type){
315         if (isset($_POST[$type])){
316           $sambafilter[$type]= $_POST[$type];
317         }
318       }
319       if (isset($_GET['search'])){
320         $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
321         if ($s == "**"){
322           $s= "*";
323         }
324         $sambafilter['regex']= $s;
325       }
326       register_global("sambafilter", $sambafilter);
328       /* Get workstation list */
329       $exclude= "";
330       foreach(split(',', $this->sambaUserWorkstations) as $ws){
331         $exclude.= "(cn=$ws$)";
332       }
333       if ($exclude != ""){
334         $exclude= "(!(|$exclude))";
335       }
336       $regex= $sambafilter['regex'];
337       $filter= "(&(objectClass=sambaSAMAccount)$exclude(uid=*$)(|(uid=$regex)(cn=$regex)))";
338       $res= get_list($filter, "winworkstation", $sambafilter['depselect'], array("uid"), GL_SUBSEARCH | GL_SIZELIMIT);
339         
340       $wslist= array();
341       foreach ($res as $attrs){
342         $wslist[]= preg_replace('/\$/', '', $attrs['uid'][0]);
343       }
344       asort($wslist);
346       $smarty->assign("search_image", get_template_path('images/search.png'));
347       $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
348       $smarty->assign("tree_image", get_template_path('images/tree.png'));
349       $smarty->assign("deplist", $this->config->idepartments);
350       $smarty->assign("alphabet", generate_alphabet());
351       foreach( array("depselect", "regex") as $type){
352         $smarty->assign("$type", $sambafilter[$type]);
353       }
354       $smarty->assign("hint", print_sizelimit_warning());
355       $smarty->assign("wslist", $wslist);
356       $smarty->assign("apply", apply_filter());
357       $display= $smarty->fetch (get_template_path('samba3_workstations.tpl', TRUE,
358                                 dirname(__FILE__)));
359       return ($display);
360     }
362     /* Fill calendar */
363     $days= array();
364     for($d= 1; $d<32; $d++){
365       $days[]= $d;
366     }
367     $years= array();
368     for($y= $date['year']-4; $y<$date['year']+4; $y++){
369       $years[]= $y;
370     }
371     $months= array(_("January"), _("February"), _("March"), _("April"),
372         _("May"), _("June"), _("July"), _("August"), _("September"),
373         _("October"), _("November"), _("December"));
374     $smarty->assign("day", $date["mday"]);
375     $smarty->assign("days", $days);
376     $smarty->assign("months", $months);
377     $smarty->assign("month", $date["mon"]-1);
378     $smarty->assign("years", $years);
379     $smarty->assign("year", $date["year"]);
380     
381     $sambaLogonTime_days= array();
382     for($d= 1; $d<32; $d++){
383       $sambaLogonTime_days[]= $d;
384     }
385     $sambaLogonTime_years= array();
386     for($y= $date['year']-4; $y<$date['year']+4; $y++){
387       $sambaLogonTime_years[]= $y;
388     }
389     $sambaLogonTime_months= array(_("January"), _("February"), _("March"), _("April"),
390         _("May"), _("June"), _("July"), _("August"), _("September"),
391         _("October"), _("November"), _("December"));
392     $smarty->assign("sambaLogonTime_day", $sambaLogonTime_date["mday"]);
393     $smarty->assign("sambaLogonTime_days", $sambaLogonTime_days);
394     $smarty->assign("sambaLogonTime_months", $sambaLogonTime_months);
395     $smarty->assign("sambaLogonTime_month", $sambaLogonTime_date["mon"]-1);
396     $smarty->assign("sambaLogonTime_years", $sambaLogonTime_years);
397     $smarty->assign("sambaLogonTime_year", $sambaLogonTime_date["year"]);
398     
399     $sambaLogoffTime_days= array();
400     for($d= 1; $d<32; $d++){
401       $sambaLogoffTime_days[]= $d;
402     }
403     $sambaLogoffTime_years= array();
404     for($y= $date['year']-4; $y<$date['year']+4; $y++){
405       $sambaLogoffTime_years[]= $y;
406     }
407     $sambaLogoffTime_months= array(_("January"), _("February"), _("March"), _("April"),
408         _("May"), _("June"), _("July"), _("August"), _("September"),
409         _("October"), _("November"), _("December"));
410     $smarty->assign("sambaLogoffTime_day", $sambaLogoffTime_date["mday"]);
411     $smarty->assign("sambaLogoffTime_days", $sambaLogoffTime_days);
412     $smarty->assign("sambaLogoffTime_months", $sambaLogoffTime_months);
413     $smarty->assign("sambaLogoffTime_month", $sambaLogoffTime_date["mon"]-1);
414     $smarty->assign("sambaLogoffTime_years", $sambaLogoffTime_years);
415     $smarty->assign("sambaLogoffTime_year", $sambaLogoffTime_date["year"]);
416     
417     $sambaKickoffTime_days= array();
418     for($d= 1; $d<32; $d++){
419       $sambaKickoffTime_days[]= $d;
420     }
421     $sambaKickoffTime_years= array();
422     for($y= $date['year']-4; $y<$date['year']+4; $y++){
423       $sambaKickoffTime_years[]= $y;
424     }
425     $sambaKickoffTime_months= array(_("January"), _("February"), _("March"), _("April"),
426         _("May"), _("June"), _("July"), _("August"), _("September"),
427         _("October"), _("November"), _("December"));
428     //$smarty->assign("sambaKickoffTime_day", $sambaKickoffTime_date["mday"]-1);
429     $smarty->assign("sambaKickoffTime_day", $sambaKickoffTime_date["mday"]); // hickert
430     $smarty->assign("sambaKickoffTime_days", $sambaKickoffTime_days);
431     $smarty->assign("sambaKickoffTime_months", $sambaKickoffTime_months);
432     $smarty->assign("sambaKickoffTime_month", $sambaKickoffTime_date["mon"]-1);
433     $smarty->assign("sambaKickoffTime_years", $sambaKickoffTime_years);
434     $smarty->assign("sambaKickoffTime_year", $sambaKickoffTime_date["year"]);
435      
436     /* Fill boxes */
437     if ($this->samba3){
438       $domains= array();
439       foreach($this->config->data['SERVERS']['SAMBA'] as $name => $content){
440         $domains[]= $name;
441       }
442       $smarty->assign("domains", $domains);
443     }
444     $letters= array();
445     for ($i= 68; $i<91; $i++){
446       $letters[]= chr($i).":";
447     }
448     $smarty->assign("drives", $letters);
450     /* Fill terminal server settings */
451     if ($this->samba3){
452       foreach ($this->ctxattributes as $attr){
453         /* Fill common attributes */
454         if (isset($this->mungedObject->ctx[$attr])){
455           $smarty->assign("$attr", $this->mungedObject->ctx[$attr]);
456           // Set field  to blank if value is 0
457           if(in_array($attr, array("CtxMaxConnectionTime", "CtxMaxDisconnectionTime", "CtxMaxIdleTime"))) {
458             if($this->mungedObject->ctx[$attr] == 0) {
459               $smarty->assign("$attr", "");
460             }
461           }
462         } else {
463           $smarty->assign("$attr", "");
464         }
465       }
467       /* Assign enum values for preset items */
468       $shadowModeVals= array( "0" => _("disabled"),
469           "1" => _("input on, notify on"),
470           "2" => _("input on, notify off"),
471           "3" => _("input off, notify on"),
472           "4" => _("input off, nofify off"));
474       $brokenConnModeVals= array(       "0" => _("disconnect"),
475           "1" => _("reset"));
477       $reConnModeVals= array( "0" => _("from any client"),
478           "1" => _("from previous client only"));
480       /* Fill preset items */
481       $smarty->assign("shadow", $shadowModeVals);
482       $smarty->assign("brokenconn", $brokenConnModeVals);
483       $smarty->assign("reconn", $reConnModeVals);
485       /* Fill preset items with values */
486       $smarty->assign("shadowmode", $this->mungedObject->getShadow());
487       $smarty->assign("brokenconnmode", $this->mungedObject->getBrokenConn());
488       $smarty->assign("reconnmode", $this->mungedObject->getReConn());
490       if($_SESSION['js']){
491         /* Set form elements to disabled/enable state */
492         $smarty->assign("tsloginstate", $this->mungedObject->getTsLogin()?"":"disabled");
494         $smarty->assign("inheritstate", "");
495         if($this->acl_is_writeable("AllowLoginOnTerminalServer",$SkipWrite)){
496           $smarty->assign("inheritstate", $this->mungedObject->getInheritMode()?"disabled":"");
497         }
498   
500   
501       }else{
502         $smarty->assign("tsloginstate", "");
503         $smarty->assign("inheritstate", "");
504       }      
506       /* Set checkboxes to checked or unchecked state */
507       $smarty->assign("tslogin", $this->mungedObject->getTsLogin()?"checked":"");
508       $smarty->assign("inherit", $this->mungedObject->getInheritMode()?"checked":"");
509       $smarty->assign("connectclientdrives",
510                       $this->mungedObject->getConnectClientDrives()?"checked":"");
511       $smarty->assign("connectclientprinters",
512                       $this->mungedObject->getConnectClientPrinters()?"checked":"");
513       $smarty->assign("defaultprinter",
514                       $this->mungedObject->getDefaultPrinter()?"checked":"");
515       $smarty->assign("CtxMaxConnectionTimeF",
516                       $this->mungedObject->getCtxMaxConnectionTimeF()?"checked":"");
517       $smarty->assign("CtxMaxDisconnectionTimeF",
518                       $this->mungedObject->getCtxMaxDisconnectionTimeF()?"checked":"");
519       $smarty->assign("CtxMaxIdleTimeF",
520                       $this->mungedObject->getCtxMaxIdleTimeF()?"checked":"");
522       /* Fill sambaUserWorkstations */
523       $ws= split(",", $this->sambaUserWorkstations);
524       sort($ws);
525       
526       /* Tidy checks for empty option, and smarty will produce one if array[0]="" */
527       if(($ws[0]=="")&&(count($ws)==1)) $ws=array();
530       $smarty->assign("workstations", $ws);
531     }
533     /* Variables */
534     foreach($this->attributes as $val){
535       $smarty->assign("$val", $this->$val);
536     }
538     /* 'sambaAcctFlags' checkboxes */
539     /* Check for 'lock-account'-flag: 'D' or 'L' */
540     if (is_integer(strpos($this->sambaAcctFlags, "D")) ||
541         is_integer(strpos($this->sambaAcctFlags, "L"))) {
542         $smarty->assign("flagsD", "checked");
543     } else {
544         $smarty->assign("flagsD", "");
545     }
546     
547     /* Check for no_password_required flag 'N' */
548     if (is_integer(strpos($this->sambaAcctFlags, "N"))) {
549         $smarty->assign("flagsN", "checked");
550     } else {
551         $smarty->assign("flagsN", "");
552     }
554     if($this->samba3){
555       if ($this->sambaPwdCanChange=="1"){
556         $smarty->assign("flagsP", "checked");
557       } else {
558         $smarty->assign("flagsP", "");
559       }
560     }else{
561       if ($this->pwdCanChange=="1"){
562         $smarty->assign("flagsP", "checked");
563       } else {
564         $smarty->assign("flagsP", "");
565       }
566     }
568     if ($this->password_expires=="1"){
569       $smarty->assign("flagsC", "checked");
570     } else {
571       $smarty->assign("flagsC", "");
572     }
573     if ($this->logon_time_set=="1"){
574       $smarty->assign("flagsT", "checked");
575     } else {
576       $smarty->assign("flagsT", "");
577     }
578     if ($this->logoff_time_set=="1"){
579       $smarty->assign("flagsO", "checked");
580     } else {
581       $smarty->assign("flagsO", "");
582     }
583     if ($this->kickoff_time_set=="1"){
584       $smarty->assign("flagsK", "checked");
585     } else {
586       $smarty->assign("flagsK", "");
587     }
588    
590     /* In case of javascript, disable some fields on demand */
591     if ($this->samba3){
592       foreach($this->mungedObject->getOnDemandFlags() as $key => $value) {
593         $smarty->assign("$key", "$value");
594       }
595     }
597     /* Show main page */
598     if ($this->samba3){
599       $display.= $smarty->fetch (get_template_path('samba3.tpl', TRUE, dirname(__FILE__)));
600     } else {
601       $display.= $smarty->fetch (get_template_path('samba2.tpl', TRUE, dirname(__FILE__)));
602     }
604     return ($display);
605   }
607   function remove_from_parent()
608   {
609     /* Cancel if there's nothing to do here */
610    if (!$this->initially_was_account){
611      return;
612    }
613     
614     /* include global link_info */
615     $ldap= $this->config->get_ldap_link();
617     plugin::remove_from_parent();
619     /* Keep uid attribute for gosaAccount */
620     unset($this->attrs['uid']);
621     unset($this->attrs['uidNumber']);
622     unset($this->attrs['gidNumber']);
623     @DEBUG (DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__,
624         $this->attributes, "Save");
625     $ldap->cd($this->dn);
626     $this->cleanup();
627     $ldap->modify ($this->attrs); 
629     show_ldap_error($ldap->get_error(), sprintf(_("Removing of user/samba account with dn '%s' failed."),$this->dn));
631     /* Optionally execute a command after we're done */
632     $this->handle_post_events("remove", array("uid" => $this->uid));
633   }
636   /* Check for input problems */
637   function check()
638   {
639     /* Call common method to give check the hook */
640     $message= plugin::check();
642     if ($this->samba3){
644       /* Strings */
645       foreach (array( "sambaHomePath" => _("Home directory"),
646             "sambaProfilePath" => _("Profile path")) as $key => $val){
647         if (!$this->mungedObject->is_samba_path($this->$key)){
648           $message[]= sprintf(_("The value specified as '%s' contains invalid characters!"), $val);
649         }
650       }
652       /* Numeric values */
653       foreach (array(   "CtxMaxConnectionTime" => _("Connection"),
654             "CtxMaxDisconnectionTime" => _("Disconnection"),
655             "CtxMaxIdleTime" => _("IDLE")) as $key => $val){
657         if (isset($this->mungedObject->ctx[$key]) && !is_id($this->mungedObject->ctx[$key]) && $val != 0){
658           $message[]= sprintf(_("The timeout property '%s' is checked and contains invalid or no characters!"), $val);
659         }
660       }
662       /* Too many workstations? Windows usrmgr only supports eight */
663       if (substr_count($this->sambaUserWorkstations, ",") >= 8){
664         $message[]= _("The windows user manager only allows eight clients. You've specified more than eight.");
665       }
666     }
668     return ($message);
669   }
672   /* Save data to object */
673   function save_object()
674   {
676     $SkipWrite = (!isset($this->parent) || !$this->parent) && !isset($_SESSION['edit']);
678     /* We only care if we are on the sambaTab... */
679     if (isset($_POST['sambaTab'])){
680       plugin::save_object();
682       /* Take care about access options */
683       if ($this->acl_is_writeable("sambaAcctFlagsL",$SkipWrite) || ($this->acl_is_writeable("sambaAcctFlagsN",$SkipWrite))){
684         if ($this->samba3){
685           $attrname= "sambaPwdCanChange";
686         } else {
687           $attrname= "pwdCanChange";
688         }
689         if (isset($_POST["allow_pwchange"]) && $_POST["allow_pwchange"] == 1){
690           $tmp= 1;
691         } else {
692           $tmp= 0;
693         }
694         if ($this->$attrname != $tmp){
695           $this->is_modified= TRUE;
696         }
697         $this->pwdCanChange= $tmp;
698         $this->sambaPwdCanChange= $tmp;
699       }
700       $tmp= "UX";
701       if (isset($_POST["no_password_required"])){
702         if ($_POST["no_password_required"] == 1){
703           $tmp.= "N";
704         }
705       }
706       if (isset($_POST["password_expires"])){
707         if ($_POST["password_expires"] == 1){
708           $this->password_expires= 1;
709         }
710       } else {
711         $this->password_expires= 0;
712       }
713       if (isset($_POST["temporary_disable"])){
714         if ($_POST["temporary_disable"] == 1){
715           if (is_integer(strpos($this->sambaAcctFlags, "L"))) {
716             $tmp.= "L";
717           } else {
718             $tmp.= "D";
719           }
720         }
721       }
722       if (isset($_POST["logon_time_set"])){
723         if ($_POST["logon_time_set"] == 1){
724           $this->logon_time_set= 1;
725         }
726       } else {
727         $this->logon_time_set= 0;
728       }
729       if (isset($_POST["logoff_time_set"])){
730         if ($_POST["logoff_time_set"] == 1){
731           $this->logoff_time_set= 1;
732         }
733       } else {
734         $this->logoff_time_set= 0;
735       }
736       if (isset($_POST["kickoff_time_set"])){
737         if ($_POST["kickoff_time_set"] == 1){
738           $this->kickoff_time_set= 1;
739         }
740       } else {
741         $this->kickoff_time_set= 0;
742       }
743       
744       $fill= "";
745       for ($i= strlen($tmp); $i<12; $i++){
746         $fill.= " ";
747       }
749       $tmp= "[$tmp$fill]";
751       /* Only save if acl's are set */
752       if ($this->acl_is_writeable("sambaAcctFlagsL",$SkipWrite) || ($this->acl_is_writeable("sambaAcctFlagsN",$SkipWrite))){
753         if ($this->samba3){
754           $attrname= "sambaAcctFlags";
755         } else {
756           $attrname= "acctFlags";
757         }
758         if ($this->$attrname != $tmp){
759           $this->is_modified= TRUE;
760         }
761         $this->$attrname= $tmp;
762       }
764       /* Save sambaDomain attribute */
765       if ($this->acl_is_writeable("sambaDomainName",$SkipWrite) && $this->samba3 && isset ($_POST['sambaDomainName'],$SkipWrite)){
766         $this->sambaDomainName= validate($_POST['sambaDomainName']);
767       }
769       /* Save CTX values */
770       if ($this->samba3){
772         /* Save obvious values */
773         foreach($this->ctxattributes as $val){
774           if (isset($_POST[$val]) && $this->acl_is_writeable("AllowLoginOnTerminalServer",$SkipWrite)){
775             if (get_magic_quotes_gpc()) {
776               $this->mungedObject->ctx[$val]= stripcslashes(validate($_POST[$val]));
777             } else {
778               $this->mungedObject->ctx[$val]= validate($_POST[$val]);
779             }
780           }
781         }
783         /* Save checkbox states. */
784         $this->mungedObject->setTsLogin(!isset($_POST['tslogin'])
785                         && $this->acl_is_writeable("AllowLoginOnTerminalServer",$SkipWrite));
786         // Need to do some index checking to avoid messages like "index ... not found"
787         if(isset($_POST['brokenconn'])) {
788           $this->mungedObject->setBrokenConn($_POST['brokenconn'] == '1'
789                         && $this->acl_is_writeable("AllowLoginOnTerminalServer",$SkipWrite));
790         }
791         if(isset($_POST['reconn'])) {
792           $this->mungedObject->setReConn($_POST['reconn'] == '1'
793                         && $this->acl_is_writeable("AllowLoginOnTerminalServer",$SkipWrite));
794         }
795         $this->mungedObject->setInheritMode(isset($_POST['inherit'])
796                         && $this->acl_is_writeable("AllowLoginOnTerminalServer",$SkipWrite));
797         $this->mungedObject->setCtxMaxConnectionTimeF(!isset($_POST['CtxMaxConnectionTimeF'])
798                         && $this->acl_is_writeable("AllowLoginOnTerminalServer",$SkipWrite));
799         $this->mungedObject->setCtxMaxDisconnectionTimeF(
800                         !isset($_POST['CtxMaxDisconnectionTimeF']) 
801                         && $this->acl_is_writeable("AllowLoginOnTerminalServer",$SkipWrite));
802         $this->mungedObject->setCtxMaxIdleTimeF(!isset($_POST['CtxMaxIdleTimeF'])
803                         && $this->acl_is_writeable("AllowLoginOnTerminalServer",$SkipWrite));
804         $this->mungedObject->setConnectClientDrives(isset($_POST['connectclientdrives'])
805                         && $this->acl_is_writeable("AllowLoginOnTerminalServer",$SkipWrite));
806         $this->mungedObject->setConnectClientPrinters(isset($_POST['connectclientprinters'])  
807                         && $this->acl_is_writeable("AllowLoginOnTerminalServer",$SkipWrite));
808         $this->mungedObject->setDefaultPrinter(isset($_POST['defaultprinter'])
809                         && $this->acl_is_writeable("AllowLoginOnTerminalServer",$SkipWrite));
811         /* Save combo boxes. Takes two values */
812         if(isset($_POST['reconn'])) {
813           $this->mungedObject->setShadow(isset($_POST['shadow']) && $this->acl_is_writeable("AllowLoginOnTerminalServer",$SkipWrite),$_POST['shadow']);
814         }
816         /* Check for changes */
817         if ($this->sambaMungedDial != $this->mungedObject->getMunged()){
818           $this->is_modified= TRUE;
819         }
820       }
821     }
822   }
825   /* Save to LDAP */
826   function save()
827   {
828     /* Load uid and gid of this 'dn' */
829     $ldap= $this->config->get_ldap_link();
830     $ldap->cat($this->dn, array('uidNumber', 'gidNumber'));
831     $tmp= $ldap->fetch();
832     $this->uidNumber= $tmp['uidNumber'][0];
833     $this->gidNumber= $tmp['gidNumber'][0];
835     plugin::save();
837     /* Remove objectClass for sambaIdmapEntry */
838     $tmp= array();
839     for ($i= 0; $i<count($this->attrs["objectClass"]); $i++){
840       if ($this->attrs['objectClass'][$i] != 'sambaIdmapEntry'){
841         $tmp[]= $this->attrs['objectClass'][$i];
842       }
843     }
844     $this->attrs['objectClass']= $tmp;
846     /* Generate rid / primaryGroupId */
847     if ($this->samba3){
848       if (!isset($this->config->data['SERVERS']['SAMBA'][$this->sambaDomainName]['SID'])){
849         print_red (_("Warning: This account has an undefined samba SID assigned. The problem can not be fixed by GOsa!"));
850       } else {
851         $this->SID= $this->config->data['SERVERS']['SAMBA'][$this->sambaDomainName]['SID'];
852         $this->ridBase= $this->config->data['SERVERS']['SAMBA'][$this->sambaDomainName]['RIDBASE'];
853       }
855       /* Need to generate a new uniqe uid/gid combination? */
856       if ($this->sambaSID == "" || $this->orig_sambaDomainName != $this->sambaDomainName){
857         $uidNumber= $this->uidNumber;
858         while(TRUE){
859           $sid= $this->SID."-".($uidNumber*2 + $this->ridBase);
860           $ldap->cd($this->config->current['BASE']);
861           $ldap->search("(sambaSID=$sid)", array("sambaSID"));
862           if ($ldap->count() == 0){
863             break;
864           }
865           $uidNumber++;
866         }
867         $this->attrs['sambaSID']= $sid;
869         /* Check for users primary group */
870         $ldap->cd($this->config->current['BASE']);
871         $ldap->search("(&(objectClass=posixGroup)(gidNumber=".$this->gidNumber."))", array("cn"));
872         if ($ldap->count() != 1){
873           print_red(_("Warning: Can't identify users primary group - no conversion to a samba group possible!"));
874         } else {
875           $attrs= $ldap->fetch();
876           $g= new group($this->config, $ldap->getDN());
877           if ($g->sambaSID == ""){
878             $g->sambaDomainName= $this->sambaDomainName;
879             $g->smbgroup= TRUE;
880             $g->save ();
881           }
882           $this->attrs['sambaPrimaryGroupSID']= $g->sambaSID;
883         }
884       }
886       if ($this->sambaHomeDrive == ""){
887         $this->attrs["sambaHomeDrive"]= array();
888       }
890       /* Generate munged dial value */
891       $this->attrs["sambaMungedDial"]= $this->mungedObject->getMunged();
893       /* User wants me to fake the idMappings? This is useful for
894          making winbind resolve the user names in a reasonable amount
895          of time in combination with larger databases. */
896       if (isset($this->config->current['SAMBAIDMAPPING']) &&
897           preg_match('/true/i', $this->config->current['SAMBAIDMAPPING'])){
898         $this->attrs['objectClass'][]= "sambaIdmapEntry";
899       }
902       /* Password expiery */
903       if ($this->password_expires == "1"){
904         $this->attrs['sambaPwdMustChange']= $this->sambaPwdMustChange;
905       } else {
906         $this->attrs['sambaPwdMustChange']= array();
907       }
908       /* Make sure not to save zero in sambaPwdLastset */
909       if ($this->sambaPwdLastSet != "0"){
910         $this->attrs['sambaPwdLastSet']= $this->sambaPwdLastSet;
911       } else {
912         $this->attrs['sambaPwdLastSet']= array();
913       }
914       /* Account expiery */
915       if ($this->logon_time_set == "1"){
916         $this->attrs['sambaLogonTime']= $this->sambaLogonTime;
917       } else {
918         $this->attrs['sambaLogonTime']= array();
919       }
920       if ($this->logoff_time_set == "1"){
921         $this->attrs['sambaLogoffTime']= $this->sambaLogoffTime;
922       } else {
923         $this->attrs['sambaLogoffTime']= array();
924       }
925       if ($this->kickoff_time_set == "1"){
926         # Add one day in unixtime format to be compatible with usrmgr
927         //$this->attrs['sambaKickoffTime']= $this->sambaKickoffTime + 86400; 
928         $this->attrs['sambaKickoffTime']= $this->sambaKickoffTime; //hickert 
929       } else {
930         $this->attrs['sambaKickoffTime']= array();
931       }
932     } else {
933     /* Not samba3 */
934       $this->attrs['rid']= $this->uidNumber*2 + 1000;
935       $this->attrs['primaryGroupID']= $this->gidNumber*2 +1001;
937       if ($this->homeDrive == ""){
938         $this->attrs["homeDrive"]= array();
939       }
941       /* Password expiery */
942       if ($this->password_expires == "1"){
943         $this->attrs['pwdMustChange']= $this->pwdMustChange;
944       } else {
945         $this->attrs['pwdMustChange']= 2147483647;
946       }
947       /* Make sure not to save zero in pwdLastset */
948       if ($this->pwdLastSet != "0"){
949         $this->attrs['pwdLastSet']= $this->pwdLastSet;
950       } else {
951         $this->attrs['pwdLastSet']= array();
952       }
953       /* Account expiery */
954       if ($this->logon_time_set == "1"){
955         $this->attrs['logonTime']= $this->logonTime;
956       } else {
957         $this->attrs['logonTime']= array();
958       }
959       if ($this->logoff_time_set == "1"){
960         $this->attrs['logoffTime']= $this->logoffTime;
961       } else {
962         $this->attrs['logoffTime']= array();
963       }
964       if ($this->kickoff_time_set == "1"){
965         # Add one day in unixtime format to be compatible with usrmgr
966         $this->attrs['kickoffTime']= $this->kickoffTime + 86400;
967       } else {
968         $this->attrs['kickoffTime']= array();
969       }
970     }
972     /* Write back to ldap */
973     $ldap->cd($this->dn);
974     $this->cleanup();
975     $ldap->modify ($this->attrs); 
977     show_ldap_error($ldap->get_error(), sprintf(_("Saving of user/samba account with dn '%s' failed."),$this->dn));
979     /* Optionally execute a command after we're done */
980     if ($this->initially_was_account == $this->is_account){
981       if ($this->is_modified){
982         $this->handle_post_events("modify", array("uid" => $this->uid));
983       }
984     } else {
985       $this->handle_post_events("add", array("uid" => $this->uid));
986     }
988   }
990   function adapt_from_template($dn)
991   {
992     plugin::adapt_from_template($dn);
993     $this->sambaSID= "";
994     $this->sambaPrimaryGroupSID= "";
996       /* Fill mungedDial field */
997     if ($this->samba3 && isset($this->attrs['sambaMungedDial'])){
998       $this->mungedObject->load($this->sambaMungedDial);
999     }
1001     /* Password expiery */
1002     if(isset($this->attrs['sambaPwdMustChange']) &&
1003         $this->attrs['sambaPwdMustChange'][0] != 0){
1004       $this->password_expires= 1;
1005     }
1007     if(isset($this->attrs['sambaLogonTime']) && ! (
1008         $this->attrs['sambaLogonTime'][0] == 0 ||
1009         $this->attrs['sambaLogonTime'][0] == 2147483647
1010       )){
1011       $this->logon_time_set= 1;
1012     }
1013     if(isset($this->attrs['sambaLogoffTime']) && ! (
1014         $this->attrs['sambaLogoffTime'][0] == 0 ||
1015         $this->attrs['sambaLogoffTime'][0] == 2147483647
1016       )){
1017       $this->logoff_time_set= 1;
1018     }
1020     /* Account expiery */
1021     if(isset($this->attrs['sambaKickoffTime']) && ! (
1022         $this->attrs['sambaKickoffTime'][0] == 0 ||
1023         $this->attrs['sambaKickoffTime'][0] == 2147483647
1024       )){
1025       $this->kickoff_time_set= 1;
1026     }
1028     /* Get global filter config */
1029     if (!is_global("sambafilter")){
1030       $ui= get_userinfo();
1031       $base= get_base_from_people($ui->dn);
1032       $sambafilter= array( "depselect" => $base, "regex" => "*");
1033       register_global("sambafilter", $sambafilter);
1034     }
1035   }
1037   
1038   function plInfo()
1039   {
1040     return (array(
1041           "plShortName"     => _("Samba"),
1042           "plDescription"   => _("Samba settings"),
1043           "plSelfModify"    => TRUE,
1044           "plDepends"       => array("user"),
1045           "plPriority"      => 5,
1046           "plSection"     => array("personal" => _("My account")),
1047           "plCategory"    => array("users" => array("description" => _("Samba account"),
1048                                                     "objectClass" => array("sambaSamAccount"))),
1049           "plOptions"       => array(),
1051           "plProvidedAcls"  => array(
1052             "AllowLoginOnTerminalServer"  => _("Allow login on terminal server"),
1053             "InheritClientConfig"         => _("Inherit client config"),
1055             "sambaPwdCanChange"     => _("Allow user to change password") ,
1056             "sambaAcctFlagsN"            => _("Login from windows client requires no password"),
1057             "sambaAcctFlagsL"            => _("Lock samba account"),
1060             "sambaKickoffTime"      => _("Account expires") ,
1061             "sambaPwdMustChange"    => _("Password expires") ,
1063             "sambaLogonTime"        => _("Limit Logon Time") ,
1064             "sambaLogoffTime"       => _("Limit Logoff Time") ,
1066             "sambaHomePath"         => _("Generic home directory") ,
1067             "sambaHomeDrive"        => _("Generic samba home drive") ,
1068             "sambaLogonScript"      => _("Generic script path") ,
1069             "sambaProfilePath"      => _("Generic profile path") ,
1070             "sambaDomainName"       => _("Domain") ,
1071             "sambaUserWorkstations" => _("Allow connection from")))
1072               );
1073   }    
1076 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1077 ?>