Code

Made mail plugin w3c conform
[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();
90   var $CopyPasteVars = array("kickoff_time_set","logoff_time_set","logon_time_set","mungedObject","orig_sambaDomainName");
92   function sambaAccount ($config, $dn= NULL)
93   {
94     /* Load attributes depending on the samba version */
95     $this->samba3= ($config->current['SAMBAVERSION'] == 3);
97     if ($this->samba3){
98       $this->attributes= array ("sambaSID", "sambaPwdLastSet", "sambaLogonTime",
99           "sambaLogoffTime", "sambaKickoffTime", "sambaPwdCanChange",
100           "sambaPwdMustChange", "sambaAcctFlags", "uid", "sambaMungedDial",
101           "sambaHomePath", "sambaHomeDrive", "sambaLogonScript",
102           "sambaProfilePath", "sambaPrimaryGroupSID", "sambaDomainName",
103           "sambaUserWorkstations", "sambaPasswordHistory",
104           "sambaLogonHours", "sambaBadPasswordTime",
105           "sambaBadPasswordCount");
106       $this->objectclasses= array ("sambaSamAccount");
107       $this->mungedObject= new sambaMungedDial;
108       $this->ctxattributes= $this->mungedObject->ctxattributes;
109     } else {
110       $this->attributes= array ("pwdLastSet", "logonTime", "logoffTime", "kickoffTime",
111           "pwdCanChange", "pwdMustChange", "acctFlags", "profilePath", "uid",
112           "smbHome", "homeDrive", "scriptPath", "rid", "primaryGroupID");
113       $this->objectclasses= array ("sambaAccount");
114     }
116     plugin::plugin ($config, $dn);
118     /* Get samba Domain in case of samba 3 */
119     if ($this->samba3 && $this->sambaSID != ""){
120       $this->SID= preg_replace ("/-[^-]+$/", "", $this->sambaSID);
121       $ldap= $this->config->get_ldap_link();
122       $ldap->cd($this->config->current['BASE']);
123       $ldap->search ("(&(objectClass=sambaDomain)(sambaSID=$this->SID))",array("sambaAlgorithmicRidBase","sambaDomainName"));
124       if ($ldap->count() != 0){
125         $attrs= $ldap->fetch();
126         $this->ridBase= $attrs['sambaAlgorithmicRidBase'][0];
127         if ($this->sambaDomainName == ""){
128           $this->sambaDomainName= $attrs['sambaDomainName'][0];
129         }
130       } else {
131         if ($this->sambaDomainName == ""){
132           $this->sambaDomainName= "DEFAULT";
133         }
134         $this->ridBase= $this->config->current['RIDBASE'];
135         $this->SID= $this->config->current['SID'];
136       }
138       /* Save in order to compare later on */
139       $this->orig_sambaDomainName= $this->sambaDomainName;
140     }
142     /* Fill mungedDial field */
143     if ($this->samba3 && isset($this->attrs['sambaMungedDial'])){
144       $this->mungedObject->load($this->sambaMungedDial);
145     }
147     /* Password expiery */
148     if(isset($this->attrs['sambaPwdMustChange']) &&
149         $this->attrs['sambaPwdMustChange'][0] != 0){
150       $this->password_expires= 1;
151     }
153     if(isset($this->attrs['sambaLogonTime']) && ! (
154         $this->attrs['sambaLogonTime'][0] == 0 ||
155         $this->attrs['sambaLogonTime'][0] == 2147483647
156       )){
157       $this->logon_time_set= 1;
158     }
159     if(isset($this->attrs['sambaLogoffTime']) && ! (
160         $this->attrs['sambaLogoffTime'][0] == 0 ||
161         $this->attrs['sambaLogoffTime'][0] == 2147483647
162       )){
163       $this->logoff_time_set= 1;
164     }
165     
166     /* Account expiery */
167     if(isset($this->attrs['sambaKickoffTime']) && ! (
168         $this->attrs['sambaKickoffTime'][0] == 0 ||
169         $this->attrs['sambaKickoffTime'][0] == 2147483647
170       )){
171       $this->kickoff_time_set= 1;
172     }
174     /* Get global filter config */
175     if (!is_global("sambafilter")){
176       $ui= get_userinfo();
177       $base= get_base_from_people($ui->dn);
178       $sambafilter= array( "depselect" => $base, "regex" => "*");
179       register_global("sambafilter", $sambafilter);
180     }
182     /* Save initial account state */
183     $this->initially_was_account= $this->is_account;
184   }
186   function execute()
187   {
188         /* Call parent execute */
189         plugin::execute();
191     /* Do we need to flip is_account state? */
192     if (isset($_POST['modify_state'])){
193       $this->is_account= !$this->is_account;
194     }
195     /* Do we represent a valid account? */
196     if (!$this->is_account && $this->parent == NULL){
197       $display= "<img alt=\"\"src=\"images/stop.png\" align=\"middle\">&nbsp;<b>".
198         _("This account has no samba extensions.")."</b>";
199       $display.= back_to_main();
200       return ($display);
201     }
203     /* Show tab dialog headers */
204     $display= "";
205     if ($this->parent != NULL){
206       if ($this->is_account){
207         $display= $this->show_disable_header(_("Remove samba account"),
208             _("This account has samba features enabled. You can disable them by clicking below."));
209       } else {
210         $obj= $this->parent->by_object['posixAccount'];
212         /* Samba3 dependency on posix accounts are enabled
213            in the moment, because I need to rely on unique
214            uidNumbers. There'll be a better solution later
215            on. */
216         if ($obj->is_account){
218           $display= $this->show_enable_header(_("Create samba account"),
219               _("This account has samba features disabled. You can enable them by clicking below."));
220         } else {
221           $display= $this->show_enable_header(_("Create samba account"),
222               _("This account has samba features disabled. Posix features are needed for samba accounts, enable them first."), TRUE);
223         }
224         return ($display);
225       }
226     }
228     /* Prepare templating */
229     $smarty= get_smarty();
231     $tmp = $this->plInfo();
232     foreach($tmp['plProvidedAcls'] as $var => $rest){
233       $smarty->assign($var."ACL",$this->getacl($var));
234     }
236     if ($this->sambaPwdMustChange=="0"){
237       $date= getdate();
238     } else {
239       $date= getdate($this->sambaPwdMustChange);
240     }
242     if ($this->sambaLogonTime=="2147483647" || $this->sambaLogonTime=="0"){
243       $sambaLogonTime_date= getdate();
244     } else {
245       $sambaLogonTime_date= getdate($this->sambaLogonTime);
246     }
247     
248     if ($this->sambaLogoffTime=="2147483647" || $this->sambaLogoffTime=="0"){
249       $sambaLogoffTime_date= getdate();
250     } else {
251       $sambaLogoffTime_date= getdate($this->sambaLogoffTime);
252     }
253     
254     if ($this->sambaKickoffTime=="2147483647" || $this->sambaKickoffTime=="0"){
255       $sambaKickoffTime_date= getdate();
256     } else {
257       $sambaKickoffTime_date= getdate($this->sambaKickoffTime);
258     }
260     /* Remove user workstations? */
261     if (isset($_POST["delete_ws"]) && isset($_POST['workstation_list'])){
263       if($this->acl_is_writeable("sambaUserWorkstations")){
265         $tmp= $this->sambaUserWorkstations;
266         foreach($_POST['workstation_list'] as $name){
267           $tmp= preg_replace("/$name/", '', $tmp);
268           $this->is_modified= TRUE;
269         }
270         $tmp= preg_replace('/,+/', ',', $tmp);
271         $this->sambaUserWorkstations= trim($tmp, ',');
272       }
273     }
275     /* Add user workstation? */
276     if (isset($_POST["add_ws"])){
277       if($this->acl_is_writeable("sambaUserWorkstations")){
278         $this->show_ws_dialog= TRUE;
279         $this->dialog= TRUE;
280       }
281     }
283     /* Add user workstation finished? */
284     if (isset($_POST["add_ws_finish"]) || isset($_POST["add_ws_cancel"])){
285       $this->show_ws_dialog= FALSE;
286       $this->dialog= FALSE;
287     }
289     /* Add user workstation? */
290     if (isset($_POST["add_ws_finish"]) && isset($_POST['wslist'])){
291       $tmp= $this->sambaUserWorkstations;
292       foreach($_POST['wslist'] as $ws){
293         $tmp.= ",$ws";
294       }
295       $tmp= preg_replace('/,+/', ',', $tmp);
296       $this->sambaUserWorkstations= trim($tmp, ',');
297       $this->is_modified= TRUE;
298     }
300     /* Show ws dialog */
301     if ($this->show_ws_dialog){
303       /* Save data */
304       $sambafilter= get_global("sambafilter");
305       foreach( array("depselect", "regex") as $type){
306         if (isset($_POST[$type])){
307           $sambafilter[$type]= $_POST[$type];
308         }
309       }
310       if (isset($_GET['search'])){
311         $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
312         if ($s == "**"){
313           $s= "*";
314         }
315         $sambafilter['regex']= $s;
316       }
317       register_global("sambafilter", $sambafilter);
319       /* Get workstation list */
320       $exclude= "";
321       foreach(split(',', $this->sambaUserWorkstations) as $ws){
322         $exclude.= "(cn=$ws$)";
323       }
324       if ($exclude != ""){
325         $exclude= "(!(|$exclude))";
326       }
327       $regex= $sambafilter['regex'];
328       $filter= "(&(objectClass=sambaSAMAccount)$exclude(uid=*$)(|(uid=$regex)(cn=$regex)))";
329       $res= get_list($filter, "workstation", $sambafilter['depselect'], array("uid"), GL_SUBSEARCH | GL_SIZELIMIT);
330       $wslist= array();
331       foreach ($res as $attrs){
332         $wslist[]= preg_replace('/\$/', '', $attrs['uid'][0]);
333       }
334       asort($wslist);
336       $smarty->assign("search_image", get_template_path('images/search.png'));
337       $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
338       $smarty->assign("tree_image", get_template_path('images/tree.png'));
339       $smarty->assign("deplist", $this->config->idepartments);
340       $smarty->assign("alphabet", generate_alphabet());
341       foreach( array("depselect", "regex") as $type){
342         $smarty->assign("$type", $sambafilter[$type]);
343       }
344       $smarty->assign("hint", print_sizelimit_warning());
345       $smarty->assign("wslist", $wslist);
346       $smarty->assign("apply", apply_filter());
347       $display= $smarty->fetch (get_template_path('samba3_workstations.tpl', TRUE,
348                                 dirname(__FILE__)));
349       return ($display);
350     }
352     /* Fill calendar */
353     $days= array();
354     for($d= 1; $d<32; $d++){
355       $days[]= $d;
356     }
357     $years= array();
358     for($y= $date['year']-4; $y<$date['year']+4; $y++){
359       $years[]= $y;
360     }
361     $months= array(_("January"), _("February"), _("March"), _("April"),
362         _("May"), _("June"), _("July"), _("August"), _("September"),
363         _("October"), _("November"), _("December"));
364     $smarty->assign("day", $date["mday"]);
365     $smarty->assign("days", $days);
366     $smarty->assign("months", $months);
367     $smarty->assign("month", $date["mon"]-1);
368     $smarty->assign("years", $years);
369     $smarty->assign("year", $date["year"]);
370     
371     $sambaLogonTime_days= array();
372     for($d= 1; $d<32; $d++){
373       $sambaLogonTime_days[]= $d;
374     }
375     $sambaLogonTime_years= array();
376     for($y= $date['year']-4; $y<$date['year']+4; $y++){
377       $sambaLogonTime_years[]= $y;
378     }
379     $sambaLogonTime_months= array(_("January"), _("February"), _("March"), _("April"),
380         _("May"), _("June"), _("July"), _("August"), _("September"),
381         _("October"), _("November"), _("December"));
382     $smarty->assign("sambaLogonTime_day", $sambaLogonTime_date["mday"]);
383     $smarty->assign("sambaLogonTime_days", $sambaLogonTime_days);
384     $smarty->assign("sambaLogonTime_months", $sambaLogonTime_months);
385     $smarty->assign("sambaLogonTime_month", $sambaLogonTime_date["mon"]-1);
386     $smarty->assign("sambaLogonTime_years", $sambaLogonTime_years);
387     $smarty->assign("sambaLogonTime_year", $sambaLogonTime_date["year"]);
388     
389     $sambaLogoffTime_days= array();
390     for($d= 1; $d<32; $d++){
391       $sambaLogoffTime_days[]= $d;
392     }
393     $sambaLogoffTime_years= array();
394     for($y= $date['year']-4; $y<$date['year']+4; $y++){
395       $sambaLogoffTime_years[]= $y;
396     }
397     $sambaLogoffTime_months= array(_("January"), _("February"), _("March"), _("April"),
398         _("May"), _("June"), _("July"), _("August"), _("September"),
399         _("October"), _("November"), _("December"));
400     $smarty->assign("sambaLogoffTime_day", $sambaLogoffTime_date["mday"]);
401     $smarty->assign("sambaLogoffTime_days", $sambaLogoffTime_days);
402     $smarty->assign("sambaLogoffTime_months", $sambaLogoffTime_months);
403     $smarty->assign("sambaLogoffTime_month", $sambaLogoffTime_date["mon"]-1);
404     $smarty->assign("sambaLogoffTime_years", $sambaLogoffTime_years);
405     $smarty->assign("sambaLogoffTime_year", $sambaLogoffTime_date["year"]);
406     
407     $sambaKickoffTime_days= array();
408     for($d= 1; $d<32; $d++){
409       $sambaKickoffTime_days[]= $d;
410     }
411     $sambaKickoffTime_years= array();
412     for($y= $date['year']-4; $y<$date['year']+4; $y++){
413       $sambaKickoffTime_years[]= $y;
414     }
415     $sambaKickoffTime_months= array(_("January"), _("February"), _("March"), _("April"),
416         _("May"), _("June"), _("July"), _("August"), _("September"),
417         _("October"), _("November"), _("December"));
418     //$smarty->assign("sambaKickoffTime_day", $sambaKickoffTime_date["mday"]-1);
419     $smarty->assign("sambaKickoffTime_day", $sambaKickoffTime_date["mday"]); // hickert
420     $smarty->assign("sambaKickoffTime_days", $sambaKickoffTime_days);
421     $smarty->assign("sambaKickoffTime_months", $sambaKickoffTime_months);
422     $smarty->assign("sambaKickoffTime_month", $sambaKickoffTime_date["mon"]-1);
423     $smarty->assign("sambaKickoffTime_years", $sambaKickoffTime_years);
424     $smarty->assign("sambaKickoffTime_year", $sambaKickoffTime_date["year"]);
425      
426     /* Fill boxes */
427     if ($this->samba3){
428       $domains= array();
429       foreach($this->config->data['SERVERS']['SAMBA'] as $name => $content){
430         $domains[]= $name;
431       }
432       $smarty->assign("domains", $domains);
433     }
434     $letters= array();
435     for ($i= 68; $i<91; $i++){
436       $letters[]= chr($i).":";
437     }
438     $smarty->assign("drives", $letters);
440     /* Fill terminal server settings */
441     if ($this->samba3){
442       foreach ($this->ctxattributes as $attr){
443         /* Fill common attributes */
444         if (isset($this->mungedObject->ctx[$attr])){
445           $smarty->assign("$attr", $this->mungedObject->ctx[$attr]);
446           // Set field  to blank if value is 0
447           if(in_array($attr, array("CtxMaxConnectionTime", "CtxMaxDisconnectionTime", "CtxMaxIdleTime"))) {
448             if($this->mungedObject->ctx[$attr] == 0) {
449               $smarty->assign("$attr", "");
450             }
451           }
452         } else {
453           $smarty->assign("$attr", "");
454         }
455       }
457       /* Assign enum values for preset items */
458       $shadowModeVals= array( "0" => _("disabled"),
459           "1" => _("input on, notify on"),
460           "2" => _("input on, notify off"),
461           "3" => _("input off, notify on"),
462           "4" => _("input off, nofify off"));
464       $brokenConnModeVals= array(       "0" => _("disconnect"),
465           "1" => _("reset"));
467       $reConnModeVals= array( "0" => _("from any client"),
468           "1" => _("from previous client only"));
470       /* Fill preset items */
471       $smarty->assign("shadow", $shadowModeVals);
472       $smarty->assign("brokenconn", $brokenConnModeVals);
473       $smarty->assign("reconn", $reConnModeVals);
475       /* Fill preset items with values */
476       $smarty->assign("shadowmode", $this->mungedObject->getShadow());
477       $smarty->assign("brokenconnmode", $this->mungedObject->getBrokenConn());
478       $smarty->assign("reconnmode", $this->mungedObject->getReConn());
480       if($_SESSION['js']){
481         /* Set form elements to disabled/enable state */
482         $smarty->assign("tsloginstate", $this->mungedObject->getTsLogin()?"":"disabled");
483         $smarty->assign("inheritstate", $this->mungedObject->getInheritMode()?"disabled":"");
484       }else{
485         $smarty->assign("tsloginstate", "");
486         $smarty->assign("inheritstate", "");
487       }      
489       /* Set checkboxes to checked or unchecked state */
490       $smarty->assign("tslogin", $this->mungedObject->getTsLogin()?"checked":"");
491       $smarty->assign("inherit", $this->mungedObject->getInheritMode()?"checked":"");
492       $smarty->assign("connectclientdrives",
493                       $this->mungedObject->getConnectClientDrives()?"checked":"");
494       $smarty->assign("connectclientprinters",
495                       $this->mungedObject->getConnectClientPrinters()?"checked":"");
496       $smarty->assign("defaultprinter",
497                       $this->mungedObject->getDefaultPrinter()?"checked":"");
498       $smarty->assign("CtxMaxConnectionTimeF",
499                       $this->mungedObject->getCtxMaxConnectionTimeF()?"checked":"");
500       $smarty->assign("CtxMaxDisconnectionTimeF",
501                       $this->mungedObject->getCtxMaxDisconnectionTimeF()?"checked":"");
502       $smarty->assign("CtxMaxIdleTimeF",
503                       $this->mungedObject->getCtxMaxIdleTimeF()?"checked":"");
505       /* Fill sambaUserWorkstations */
506       $ws= split(",", $this->sambaUserWorkstations);
507       sort($ws);
508       
509       /* Tidy checks for empty option, and smarty will produce one if array[0]="" */
510       if(($ws[0]=="")&&(count($ws)==1)) $ws=array();
513       $smarty->assign("workstations", $ws);
514     }
516     /* Variables */
517     foreach($this->attributes as $val){
518       $smarty->assign("$val", $this->$val);
519     }
521     /* 'sambaAcctFlags' checkboxes */
522     /* Check for 'lock-account'-flag: 'D' or 'L' */
523     if (is_integer(strpos($this->sambaAcctFlags, "D")) ||
524         is_integer(strpos($this->sambaAcctFlags, "L"))) {
525         $smarty->assign("flagsD", "checked");
526     } else {
527         $smarty->assign("flagsD", "");
528     }
529     
530     /* Check for no_password_required flag 'N' */
531     if (is_integer(strpos($this->sambaAcctFlags, "N"))) {
532         $smarty->assign("flagsN", "checked");
533     } else {
534         $smarty->assign("flagsN", "");
535     }
537     /* 'normal' Checkboxes */
538     if ($this->pwdCanChange=="1"){
539       $smarty->assign("flagsP", "checked");
540     } else {
541       $smarty->assign("flagsP", "");
542     }
543     if ($this->password_expires=="1"){
544       $smarty->assign("flagsC", "checked");
545     } else {
546       $smarty->assign("flagsC", "");
547     }
548     if ($this->logon_time_set=="1"){
549       $smarty->assign("flagsT", "checked");
550     } else {
551       $smarty->assign("flagsT", "");
552     }
553     if ($this->logoff_time_set=="1"){
554       $smarty->assign("flagsO", "checked");
555     } else {
556       $smarty->assign("flagsO", "");
557     }
558     if ($this->kickoff_time_set=="1"){
559       $smarty->assign("flagsK", "checked");
560     } else {
561       $smarty->assign("flagsK", "");
562     }
563    
565     /* In case of javascript, disable some fields on demand */
566     if ($this->samba3){
567       foreach($this->mungedObject->getOnDemandFlags() as $key => $value) {
568         $smarty->assign("$key", "$value");
569       }
570     }
572     /* Show main page */
573     if ($this->samba3){
574       $display.= $smarty->fetch (get_template_path('samba3.tpl', TRUE, dirname(__FILE__)));
575     } else {
576       $display.= $smarty->fetch (get_template_path('samba2.tpl', TRUE, dirname(__FILE__)));
577     }
579     return ($display);
580   }
582   function remove_from_parent()
583   {
584     /* Cancel if there's nothing to do here */
585    if (!$this->initially_was_account){
586      return;
587    }
588     
589     /* include global link_info */
590     $ldap= $this->config->get_ldap_link();
592     plugin::remove_from_parent();
594     /* Keep uid attribute for gosaAccount */
595     unset($this->attrs['uid']);
596     unset($this->attrs['uidNumber']);
597     unset($this->attrs['gidNumber']);
598     @DEBUG (DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__,
599         $this->attributes, "Save");
600     $ldap->cd($this->dn);
601     $this->cleanup();
602     $ldap->modify ($this->attrs); 
604     show_ldap_error($ldap->get_error(), sprintf(_("Removing of user/samba account with dn '%s' failed."),$this->dn));
606     /* Optionally execute a command after we're done */
607     $this->handle_post_events("remove");
608   }
611   /* Check for input problems */
612   function check()
613   {
614     /* Call common method to give check the hook */
615     $message= plugin::check();
617     if ($this->samba3){
619       /* Strings */
620       foreach (array( "sambaHomePath" => _("Home directory"),
621             "sambaProfilePath" => _("Profile path")) as $key => $val){
622         if (!$this->mungedObject->is_samba_path($this->$key)){
623           $message[]= sprintf(_("The value specified as '%s' contains invalid characters!"), $val);
624         }
625       }
627       /* Numeric values */
628       foreach (array(   "CtxMaxConnectionTime" => _("Connection"),
629             "CtxMaxDisconnectionTime" => _("Disconnection"),
630             "CtxMaxIdleTime" => _("IDLE")) as $key => $val){
632         if (isset($this->mungedObject->ctx[$key]) && !is_id($this->mungedObject->ctx[$key]) && $val != 0){
633           $message[]= sprintf(_("The timeout property '%s' is checked and contains invalid or no characters!"), $val);
634         }
635       }
637       /* Too many workstations? Windows usrmgr only supports eight */
638       if (substr_count($this->sambaUserWorkstations, ",") >= 8){
639         $message[]= _("The windows user manager only allows eight clients. You've specified more than eight.");
640       }
641     }
643     return ($message);
644   }
647   /* Save data to object */
648   function save_object()
649   {
652     /* We only care if we are on the sambaTab... */
653     if (isset($_POST['sambaTab'])){
654       plugin::save_object();
656       /* Take care about access options */
657       if ($this->acl_is_writeable("sambaAcctFlagsL") || ($this->acl_is_writeable("sambaAcctFlagsN"))){
658         if ($this->samba3){
659           $attrname= "sambaPwdCanChange";
660         } else {
661           $attrname= "pwdCanChange";
662         }
663         if (isset($_POST["allow_pwchange"]) && $_POST["allow_pwchange"] == 1){
664           $tmp= 1;
665         } else {
666           $tmp= 0;
667         }
668         if ($this->$attrname != $tmp){
669           $this->is_modified= TRUE;
670         }
671         $this->pwdCanChange= $tmp;
672         $this->sambaPwdCanChange= $tmp;
673       }
674       $tmp= "UX";
675       if (isset($_POST["no_password_required"])){
676         if ($_POST["no_password_required"] == 1){
677           $tmp.= "N";
678         }
679       }
680       if (isset($_POST["password_expires"])){
681         if ($_POST["password_expires"] == 1){
682           $this->password_expires= 1;
683         }
684       } else {
685         $this->password_expires= 0;
686       }
687       if (isset($_POST["temporary_disable"])){
688         if ($_POST["temporary_disable"] == 1){
689           if (is_integer(strpos($this->sambaAcctFlags, "L"))) {
690             $tmp.= "L";
691           } else {
692             $tmp.= "D";
693           }
694         }
695       }
696       if (isset($_POST["logon_time_set"])){
697         if ($_POST["logon_time_set"] == 1){
698           $this->logon_time_set= 1;
699         }
700       } else {
701         $this->logon_time_set= 0;
702       }
703       if (isset($_POST["logoff_time_set"])){
704         if ($_POST["logoff_time_set"] == 1){
705           $this->logoff_time_set= 1;
706         }
707       } else {
708         $this->logoff_time_set= 0;
709       }
710       if (isset($_POST["kickoff_time_set"])){
711         if ($_POST["kickoff_time_set"] == 1){
712           $this->kickoff_time_set= 1;
713         }
714       } else {
715         $this->kickoff_time_set= 0;
716       }
717       
718       $fill= "";
719       for ($i= strlen($tmp); $i<12; $i++){
720         $fill.= " ";
721       }
723       $tmp= "[$tmp$fill]";
725       /* Only save if acl's are set */
726       if ($this->acl_is_writeable("sambaAcctFlagsL") || ($this->acl_is_writeable("sambaAcctFlagsN"))){
727         if ($this->samba3){
728           $attrname= "sambaAcctFlags";
729         } else {
730           $attrname= "acctFlags";
731         }
732         if ($this->$attrname != $tmp){
733           $this->is_modified= TRUE;
734         }
735         $this->$attrname= $tmp;
736       }
738       /* Save sambaDomain attribute */
739       if ($this->acl_is_writeable("sambaDomainName") && $this->samba3 && isset ($_POST['sambaDomainName'])){
740         $this->sambaDomainName= validate($_POST['sambaDomainName']);
741       }
743       /* Save CTX values */
744       if ($this->samba3){
746         /* Save obvious values */
747         foreach($this->ctxattributes as $val){
748           if (isset($_POST[$val]) && $this->acl_is_writeable("AllowLoginOnTerminalServer")){
749             if (get_magic_quotes_gpc()) {
750               $this->mungedObject->ctx[$val]= stripcslashes(validate($_POST[$val]));
751             } else {
752               $this->mungedObject->ctx[$val]= validate($_POST[$val]);
753             }
754           }
755         }
757         /* Save checkbox states. */
758         $this->mungedObject->setTsLogin(!isset($_POST['tslogin'])
759                         && $this->acl_is_writeable("AllowLoginOnTerminalServer"));
760         // Need to do some index checking to avoid messages like "index ... not found"
761         if(isset($_POST['brokenconn'])) {
762           $this->mungedObject->setBrokenConn($_POST['brokenconn'] == '1'
763                         && $this->acl_is_writeable("AllowLoginOnTerminalServer"));
764         }
765         if(isset($_POST['reconn'])) {
766           $this->mungedObject->setReConn($_POST['reconn'] == '1'
767                         && $this->acl_is_writeable("AllowLoginOnTerminalServer"));
768         }
769         $this->mungedObject->setInheritMode(isset($_POST['inherit'])
770                         && $this->acl_is_writeable("AllowLoginOnTerminalServer"));
771         $this->mungedObject->setCtxMaxConnectionTimeF(!isset($_POST['CtxMaxConnectionTimeF'])
772                         && $this->acl_is_writeable("AllowLoginOnTerminalServer"));
773         $this->mungedObject->setCtxMaxDisconnectionTimeF(
774                         !isset($_POST['CtxMaxDisconnectionTimeF']) 
775                         && $this->acl_is_writeable("AllowLoginOnTerminalServer"));
776         $this->mungedObject->setCtxMaxIdleTimeF(!isset($_POST['CtxMaxIdleTimeF'])
777                         && $this->acl_is_writeable("AllowLoginOnTerminalServer"));
778         $this->mungedObject->setConnectClientDrives(isset($_POST['connectclientdrives'])
779                         && $this->acl_is_writeable("AllowLoginOnTerminalServer"));
780         $this->mungedObject->setConnectClientPrinters(isset($_POST['connectclientprinters'])  
781                         && $this->acl_is_writeable("AllowLoginOnTerminalServer"));
782         $this->mungedObject->setDefaultPrinter(isset($_POST['defaultprinter'])
783                         && $this->acl_is_writeable("AllowLoginOnTerminalServer"));
785         /* Save combo boxes. Takes two values */
786         if(isset($_POST['reconn'])) {
787           $this->mungedObject->setShadow(isset($_POST['shadow']) && $this->acl_is_writeable("AllowLoginOnTerminalServer"),$_POST['shadow']);
788         }
790         /* Check for changes */
791         if ($this->sambaMungedDial != $this->mungedObject->getMunged()){
792           $this->is_modified= TRUE;
793         }
794       }
795     }
796   }
799   /* Save to LDAP */
800   function save()
801   {
802     /* Load uid and gid of this 'dn' */
803     $ldap= $this->config->get_ldap_link();
804     $ldap->cat($this->dn, array('uidNumber', 'gidNumber'));
805     $tmp= $ldap->fetch();
806     $this->uidNumber= $tmp['uidNumber'][0];
807     $this->gidNumber= $tmp['gidNumber'][0];
809     plugin::save();
811     /* Remove objectClass for sambaIdmapEntry */
812     $tmp= array();
813     for ($i= 0; $i<count($this->attrs["objectClass"]); $i++){
814       if ($this->attrs['objectClass'][$i] != 'sambaIdmapEntry'){
815         $tmp[]= $this->attrs['objectClass'][$i];
816       }
817     }
818     $this->attrs['objectClass']= $tmp;
820     /* Generate rid / primaryGroupId */
821     if ($this->samba3){
822       if (!isset($this->config->data['SERVERS']['SAMBA'][$this->sambaDomainName]['SID'])){
823         print_red (_("Warning: This account has an undefined samba SID assigned. The problem can not be fixed by GOsa!"));
824       } else {
825         $this->SID= $this->config->data['SERVERS']['SAMBA'][$this->sambaDomainName]['SID'];
826         $this->ridBase= $this->config->data['SERVERS']['SAMBA'][$this->sambaDomainName]['RIDBASE'];
827       }
829       /* Need to generate a new uniqe uid/gid combination? */
830       if ($this->sambaSID == "" || $this->orig_sambaDomainName != $this->sambaDomainName){
831         $uidNumber= $this->uidNumber;
832         while(TRUE){
833           $sid= $this->SID."-".($uidNumber*2 + $this->ridBase);
834           $ldap->cd($this->config->current['BASE']);
835           $ldap->search("(sambaSID=$sid)", array("sambaSID"));
836           if ($ldap->count() == 0){
837             break;
838           }
839           $uidNumber++;
840         }
841         $this->attrs['sambaSID']= $sid;
843         /* Check for users primary group */
844         $ldap->cd($this->config->current['BASE']);
845         $ldap->search("(&(objectClass=posixGroup)(gidNumber=".$this->gidNumber."))", array("cn"));
846         if ($ldap->count() != 1){
847           print_red(_("Warning: Can't identify users primary group - no conversion to a samba group possible!"));
848         } else {
849           $attrs= $ldap->fetch();
850           $g= new group($this->config, $ldap->getDN());
851           if ($g->sambaSID == ""){
852             $g->sambaDomainName= $this->sambaDomainName;
853             $g->smbgroup= TRUE;
854             $g->save ();
855           }
856           $this->attrs['sambaPrimaryGroupSID']= $g->sambaSID;
857         }
858       }
860       if ($this->sambaHomeDrive == ""){
861         $this->attrs["sambaHomeDrive"]= array();
862       }
864       /* Generate munged dial value */
865       $this->attrs["sambaMungedDial"]= $this->mungedObject->getMunged();
867       /* User wants me to fake the idMappings? This is useful for
868          making winbind resolve the user names in a reasonable amount
869          of time in combination with larger databases. */
870       if (isset($this->config->current['SAMBAIDMAPPING']) &&
871           preg_match('/true/i', $this->config->current['SAMBAIDMAPPING'])){
872         $this->attrs['objectClass'][]= "sambaIdmapEntry";
873       }
876       /* Password expiery */
877       if ($this->password_expires == "1"){
878         $this->attrs['sambaPwdMustChange']= $this->sambaPwdMustChange;
879       } else {
880         $this->attrs['sambaPwdMustChange']= array();
881       }
882       /* Make sure not to save zero in sambaPwdLastset */
883       if ($this->sambaPwdLastSet != "0"){
884         $this->attrs['sambaPwdLastSet']= $this->sambaPwdLastSet;
885       } else {
886         $this->attrs['sambaPwdLastSet']= array();
887       }
888       /* Account expiery */
889       if ($this->logon_time_set == "1"){
890         $this->attrs['sambaLogonTime']= $this->sambaLogonTime;
891       } else {
892         $this->attrs['sambaLogonTime']= array();
893       }
894       if ($this->logoff_time_set == "1"){
895         $this->attrs['sambaLogoffTime']= $this->sambaLogoffTime;
896       } else {
897         $this->attrs['sambaLogoffTime']= array();
898       }
899       if ($this->kickoff_time_set == "1"){
900         # Add one day in unixtime format to be compatible with usrmgr
901         //$this->attrs['sambaKickoffTime']= $this->sambaKickoffTime + 86400; 
902         $this->attrs['sambaKickoffTime']= $this->sambaKickoffTime; //hickert 
903       } else {
904         $this->attrs['sambaKickoffTime']= array();
905       }
906     } else {
907     /* Not samba3 */
908       $this->attrs['rid']= $this->uidNumber*2 + 1000;
909       $this->attrs['primaryGroupID']= $this->gidNumber*2 +1001;
911       if ($this->homeDrive == ""){
912         $this->attrs["homeDrive"]= array();
913       }
915       /* Password expiery */
916       if ($this->password_expires == "1"){
917         $this->attrs['pwdMustChange']= $this->pwdMustChange;
918       } else {
919         $this->attrs['pwdMustChange']= 2147483647;
920       }
921       /* Make sure not to save zero in pwdLastset */
922       if ($this->pwdLastSet != "0"){
923         $this->attrs['pwdLastSet']= $this->pwdLastSet;
924       } else {
925         $this->attrs['pwdLastSet']= array();
926       }
927       /* Account expiery */
928       if ($this->logon_time_set == "1"){
929         $this->attrs['logonTime']= $this->logonTime;
930       } else {
931         $this->attrs['logonTime']= array();
932       }
933       if ($this->logoff_time_set == "1"){
934         $this->attrs['logoffTime']= $this->logoffTime;
935       } else {
936         $this->attrs['logoffTime']= array();
937       }
938       if ($this->kickoff_time_set == "1"){
939         # Add one day in unixtime format to be compatible with usrmgr
940         $this->attrs['kickoffTime']= $this->kickoffTime + 86400;
941       } else {
942         $this->attrs['kickoffTime']= array();
943       }
944     }
946     /* Write back to ldap */
947     $ldap->cd($this->dn);
948     $this->cleanup();
949     $ldap->modify ($this->attrs); 
951     show_ldap_error($ldap->get_error(), sprintf(_("Saving of user/samba account with dn '%s' failed."),$this->dn));
953     /* Optionally execute a command after we're done */
954     if ($this->initially_was_account == $this->is_account){
955       if ($this->is_modified){
956         $this->handle_post_events("modify");
957       }
958     } else {
959       $this->handle_post_events("add");
960     }
962   }
964   function adapt_from_template($dn)
965   {
966     plugin::adapt_from_template($dn);
967     $this->sambaSID= "";
968     $this->sambaPrimaryGroupSID= "";
970       /* Fill mungedDial field */
971     if ($this->samba3 && isset($this->attrs['sambaMungedDial'])){
972       $this->mungedObject->load($this->sambaMungedDial);
973     }
975     /* Password expiery */
976     if(isset($this->attrs['sambaPwdMustChange']) &&
977         $this->attrs['sambaPwdMustChange'][0] != 0){
978       $this->password_expires= 1;
979     }
981     if(isset($this->attrs['sambaLogonTime']) && ! (
982         $this->attrs['sambaLogonTime'][0] == 0 ||
983         $this->attrs['sambaLogonTime'][0] == 2147483647
984       )){
985       $this->logon_time_set= 1;
986     }
987     if(isset($this->attrs['sambaLogoffTime']) && ! (
988         $this->attrs['sambaLogoffTime'][0] == 0 ||
989         $this->attrs['sambaLogoffTime'][0] == 2147483647
990       )){
991       $this->logoff_time_set= 1;
992     }
994     /* Account expiery */
995     if(isset($this->attrs['sambaKickoffTime']) && ! (
996         $this->attrs['sambaKickoffTime'][0] == 0 ||
997         $this->attrs['sambaKickoffTime'][0] == 2147483647
998       )){
999       $this->kickoff_time_set= 1;
1000     }
1002     /* Get global filter config */
1003     if (!is_global("sambafilter")){
1004       $ui= get_userinfo();
1005       $base= get_base_from_people($ui->dn);
1006       $sambafilter= array( "depselect" => $base, "regex" => "*");
1007       register_global("sambafilter", $sambafilter);
1008     }
1009   }
1011   
1012   function plInfo()
1013   {
1014     return (array(
1015           "plShortName"     => _("Samba"),
1016           "plDescription"   => _("Samba settings"),
1017           "plSelfModify"    => TRUE,
1018           "plDepends"       => array("user"),
1019           "plPriority"      => 5,                                 // Position in tabs
1020           "plSection"       => "personal",                        // This belongs to personal
1021           "plCategory"      => array("users"),                    // Add to following categories
1022           "plOptions"       => array(),
1024           "plProvidedAcls"  => array(
1025             "AllowLoginOnTerminalServer"  => _("Allow login on terminal server"),
1026             "InheritClientConfig"         => _("Inherit client config"),
1028             "sambaPwdCanChange"     => _("Allow user to change password") ,
1029             "sambaAcctFlagsN"            => _("Login from windows client requires no password"),
1030             "sambaAcctFlagsL"            => _("Lock samba account"),
1033             "sambaKickoffTime"      => _("Account expires") ,
1034             "sambaPwdMustChange"    => _("Password expires") ,
1036             "sambaLogonTime"        => _("Limit Logon Time") ,
1037             "sambaLogoffTime"       => _("Limit Logoff Time") ,
1039             "sambaHomePath"         => _("Generic home directory") ,
1040             "sambaHomeDrive"        => _("Generic samba home drive") ,
1041             "sambaLogonScript"      => _("Generic script path") ,
1042             "sambaProfilePath"      => _("Generic profile path") ,
1043             "sambaDomainName"       => _("Domain") ,
1044             "sambaUserWorkstations" => _("Allow connection from")))
1045               );
1046   }    
1049 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1050 ?>