Code

Updated sudo stuff
[gosa.git] / gosa-plugins / sudo / admin / sudo / class_sudoOption.inc
1 <?php
2 /*
3  * This code is part of GOsa (http://www.gosa-project.org)
4  * Copyright (C) 2003-2008 GONICUS GmbH
5  *
6  * ID: $$Id: class_sudo.inc 9975 2008-03-25 14:09:30Z hickert $$
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
24 /*! \brief  Sudo option class.
25             Allows setting flags/options for a sudo role.
26  */
27 class sudoOption extends plugin
28 {
29   /* Group attributes */
30   var $sudoOption = array();
31   var $attributes    = array("sudoOption");
32   var $is_account = TRUE;
33   var $options = array();
35   /*! \brief  Initializes this class
36         @param  Object $config  The GOsa configuration object.
37         @param  String $dn      The object dn.
38    */
39   function sudoOption(&$config, $dn= NULL)
40   {
41     plugin::plugin ($config, $dn);
43     /****
44       Create a list of known options
45      ****/
46     $options = array();
47     $option['long_otp_prompt']= array('NAME' =>'long_otp_prompt' ,   'TYPE' => 'BOOLEAN' ,   'DEFAULT' => 'TRUE');
48     $option['ignore_dot']=  array('NAME' =>'ignore_dot' ,  'TYPE' => 'BOOLEAN' ,   'DEFAULT' => 'TRUE');
49     $option['mail_always']= array('NAME' =>'mail_always' ,   'TYPE' => 'BOOLEAN' ,   'DEFAULT' => 'TRUE');
50     $option['mail_badpass']=  array('NAME' =>'mail_badpass' ,  'TYPE' => 'BOOLEAN' ,   'DEFAULT' => 'TRUE');
51     $option['mail_no_user']=  array('NAME' =>'mail_no_user' ,  'TYPE' => 'BOOLEAN' ,   'DEFAULT' => 'TRUE');
52     $option['mail_no_host']=  array('NAME' =>'mail_no_host' ,  'TYPE' => 'BOOLEAN' ,   'DEFAULT' => 'TRUE');
53     $option['mail_no_perms']= array('NAME' =>'mail_no_perms' ,   'TYPE' => 'BOOLEAN' ,   'DEFAULT' => 'TRUE');
54     $option['tty_tickets']= array('NAME' =>'tty_tickets' ,   'TYPE' => 'BOOLEAN' ,   'DEFAULT' => 'TRUE');
55     $option['authenticate']=  array('NAME' =>'authenticate' ,  'TYPE' => 'BOOLEAN' ,   'DEFAULT' => 'TRUE');
56     $option['root_sudo']= array('NAME' =>'root_sudo' ,   'TYPE' => 'BOOLEAN' ,   'DEFAULT' => 'TRUE');
57     $option['log_host']=  array('NAME' =>'log_host' ,  'TYPE' => 'BOOLEAN' ,   'DEFAULT' => 'TRUE');
58     $option['log_year']=  array('NAME' =>'log_year' ,  'TYPE' => 'BOOLEAN' ,   'DEFAULT' => 'TRUE');
59     $option['shell_noargs']=  array('NAME' =>'shell_noargs' ,  'TYPE' => 'BOOLEAN' ,   'DEFAULT' => 'TRUE');
60     $option['set_home']=  array('NAME' =>'set_home' ,  'TYPE' => 'BOOLEAN' ,   'DEFAULT' => 'TRUE');
61     $option['always_set_home']= array('NAME' =>'always_set_home' ,   'TYPE' => 'BOOLEAN' ,   'DEFAULT' => 'TRUE');
62     $option['path_info']= array('NAME' =>'path_info' ,   'TYPE' => 'BOOLEAN' ,   'DEFAULT' => 'TRUE');
63     $option['preserve_groups']= array('NAME' =>'preserve_groups' ,   'TYPE' => 'BOOLEAN' ,   'DEFAULT' => 'TRUE');
64     $option['fqdn']=  array('NAME' =>'fqdn' ,  'TYPE' => 'BOOLEAN' ,   'DEFAULT' => 'TRUE');
65     $option['insults']= array('NAME' =>'insults' ,   'TYPE' => 'BOOLEAN' ,   'DEFAULT' => 'TRUE');
66     $option['requiretty']=  array('NAME' =>'requiretty' ,  'TYPE' => 'BOOLEAN' ,   'DEFAULT' => 'TRUE');
67     $option['env_editor']=  array('NAME' =>'env_editor' ,  'TYPE' => 'BOOLEAN' ,   'DEFAULT' => 'TRUE');
68     $option['rootpw']=  array('NAME' =>'rootpw' ,  'TYPE' => 'BOOLEAN' ,   'DEFAULT' => 'TRUE');
69     $option['runaspw']= array('NAME' =>'runaspw' ,   'TYPE' => 'BOOLEAN' ,   'DEFAULT' => 'TRUE');
70     $option['targetpw']=  array('NAME' =>'targetpw' ,  'TYPE' => 'BOOLEAN' ,   'DEFAULT' => 'TRUE');
71     $option['set_logname']= array('NAME' =>'set_logname' ,   'TYPE' => 'BOOLEAN' ,   'DEFAULT' => 'TRUE');
72     $option['stay_setuid']= array('NAME' =>'stay_setuid' ,   'TYPE' => 'BOOLEAN' ,   'DEFAULT' => 'TRUE');
73     $option['env_reset']= array('NAME' =>'env_reset' ,   'TYPE' => 'BOOLEAN' ,   'DEFAULT' => 'TRUE');
74     $option['use_loginclass']=  array('NAME' =>'use_loginclass' ,  'TYPE' => 'BOOLEAN' ,   'DEFAULT' => 'TRUE');
75     $option['noexec']=  array('NAME' =>'noexec' ,  'TYPE' => 'BOOLEAN' ,   'DEFAULT' => 'TRUE');
76     $option['ignore_local_sudoers']=  array('NAME' =>'ignore_local_sudoers' ,  'TYPE' => 'BOOLEAN' ,   'DEFAULT' => 'TRUE');
77     $option['passwd_tries']=  array('NAME' =>'passwd_tries' ,  'TYPE' => 'INTEGER' ,   'DEFAULT' => 0);
78     $option['loglinelen']=  array('NAME' =>'loglinelen' ,  'TYPE' => 'BOOL_INTEGER' ,  'DEFAULT' => 0);
79     $option['timestamp_timeout']= array('NAME' =>'timestamp_timeout' ,   'TYPE' => 'BOOL_INTEGER' ,  'DEFAULT' => 0);
80     $option['passwd_timeout']=  array('NAME' =>'passwd_timeout' ,  'TYPE' => 'BOOL_INTEGER' ,  'DEFAULT' => 0);
81     $option['umask']= array('NAME' =>'umask' ,   'TYPE' => 'BOOL_INTEGER' ,  'DEFAULT' => 0);
82     $option['mailsub']= array('NAME' =>'mailsub' ,   'TYPE' => 'STRING' ,   'DEFAULT' => '');
83     $option['badpass_message']= array('NAME' =>'badpass_message' ,   'TYPE' => 'STRING' ,   'DEFAULT' => '');
84     $option['timestampdir']=  array('NAME' =>'timestampdir' ,  'TYPE' => 'STRING' ,   'DEFAULT' => '');
85     $option['timestampowner']=  array('NAME' =>'timestampowner' ,  'TYPE' => 'STRING' ,   'DEFAULT' => '');
86     $option['passprompt']=  array('NAME' =>'passprompt' ,  'TYPE' => 'STRING' ,   'DEFAULT' => '');
87     $option['runas_default']= array('NAME' =>'runas_default' ,   'TYPE' => 'STRING' ,   'DEFAULT' => '');
88     $option['syslog_goodpri']=  array('NAME' =>'syslog_goodpri' ,  'TYPE' => 'STRING' ,   'DEFAULT' => '');
89     $option['syslog_badpri']= array('NAME' =>'syslog_badpri' ,   'TYPE' => 'STRING' ,   'DEFAULT' => '');
90     $option['editor']=  array('NAME' =>'editor' ,  'TYPE' => 'STRING' ,   'DEFAULT' => '');
91     $option['noexec_file']= array('NAME' =>'noexec_file' ,   'TYPE' => 'STRING' ,   'DEFAULT' => '');
92     $option['lecture']= array('NAME' =>'lecture' ,   'TYPE' => 'STRING_BOOL' ,   'DEFAULT' => '');
93     $option['lecture_file']=  array('NAME' =>'lecture_file' ,  'TYPE' => 'STRING_BOOL' ,   'DEFAULT' => '');
94     $option['logfile']= array('NAME' =>'logfile' ,   'TYPE' => 'STRING_BOOL' ,   'DEFAULT' => '');
95     $option['syslog']=  array('NAME' =>'syslog' ,  'TYPE' => 'STRING_BOOL' ,   'DEFAULT' => '');
96     $option['mailerpath']=  array('NAME' =>'mailerpath' ,  'TYPE' => 'STRING_BOOL' ,   'DEFAULT' => '');
97     $option['mailerflags']= array('NAME' =>'mailerflags' ,   'TYPE' => 'STRING_BOOL' ,   'DEFAULT' => '');
98     $option['mailto']=  array('NAME' =>'mailto' ,  'TYPE' => 'STRING_BOOL' ,   'DEFAULT' => '');
99     $option['exempt_group']=  array('NAME' =>'exempt_group' ,  'TYPE' => 'STRING_BOOL' ,   'DEFAULT' => '');
100     $option['verifypw']=  array('NAME' =>'verifypw' ,  'TYPE' => 'STRING_BOOL' ,   'DEFAULT' => '');
101     $option['listpw']=  array('NAME' =>'listpw' ,  'TYPE' => 'STRING_BOOL' ,   'DEFAULT' => '');
102     $option['env_check']= array('NAME' =>'env_check' ,   'TYPE' => 'LISTS' ,   'DEFAULT' => '');
103     $option['env_delete']=  array('NAME' =>'env_delete' ,  'TYPE' => 'LISTS' ,   'DEFAULT' => '');
104     $option['env_keep']=  array('NAME' =>'env_keep' ,  'TYPE' => 'LISTS' ,   'DEFAULT' => '');
105     ksort($option);
106     $this->options = $option;
108     $this->load_options();
109   }
112   private function load_options()
113   {
115     /****
116       Parse given sudoOption attributes 
117      ****/
118     $this->sudoOption = array();
119     if(isset($this->attrs['sudoOption'])){
120       for($i = 0 ; $i < $this->attrs['sudoOption']['count']; $i++){
122         /****
123           Detect attribute name/value/negation
124          ****/
125         $opt = $this->attrs['sudoOption'][$i];
127         /* Get negation */ 
128         $negation = FALSE;
129         if(preg_match("/^!/",$opt)){
130           $negation = TRUE; 
131           $opt = preg_replace("/^!/","",$opt);
132         }
134         /* Get value / name*/
135         $value    = "";
136         if(preg_match("/=/",$opt)){
137           $value  = preg_replace("/^[^=]*+=/","",$opt);
138           $opt    = preg_replace("/=.*$/","",$opt);
139         }
141         /* Special chars are escaped, remove escape char now.
142             \\ => \
143             \: => :
144             \, => ,
145             \= => = 
146          */
147         $value = preg_replace(array("/\\\\\\\\/","/\\\\,/","/\\\\:/","/\\\\=/"),
148                               array("\\",",",":","="),$value);
150         /* Check if the given value is part of our options list.
151            If it is not, add it as type STRING and display a warning.  
152         */
153         if(!isset($this->options[$opt])){
154           $this->options[$opt]=array('NAME'=>$opt,'TYPE'=>'STRING','DEFAULT' => '');
155           msg_dialog::display(_("Unknown option"),
156               sprintf(_("The sudo option '%s' is invalid!"),
157                 $opt),WARNING_DIALOG);
158         }
160         /* Create internal sudoOption object */
161         $option = array();
162         $option['NAME']   = $opt;
163         $option['VALUE']  = $value;
164         $option['NEGATE'] = $negation;
166         /*  Special handling for mixed flag types. 
167             Some attributes like (BOOL_INTEGER) can be TRUE/FALSE and INTEGER.
168             This means, if the value is empty it is BOOL and $negation defines its boolean value.
169          */
170         if(in_array($this->options[$opt]['TYPE'],array("BOOL_INTEGER","STRING_BOOL"))){
171           if(empty($value)){
172             $option['NEGATE'] = FALSE;
173             if($negation){
174               $option['VALUE'] = "FALSE";
175             }else{
176               $option['VALUE'] = "TRUE";
177             }
178           }
179         }
181         /* Special handling for BOOLEAN values */
182         if(in_array($this->options[$opt]['TYPE'],array("BOOLEAN"))){
183           $option['NEGATE'] = FALSE;
184           if($negation){
185             $option['VALUE'] = "FALSE";
186           }else{
187             $option['VALUE'] = "TRUE";
188           }
189         }
191         /* Append values */
192         $this->sudoOption[$opt][] = $option;
193       }
194     }
195   }
199   /*! \brief  Create HTML output for this plugin 
200       @return String  HTML output for this plugin.
201     */
202   function execute()
203   {
204     /* Call parent execute */
205     plugin::execute();
207     /*****
208       Handle Posts 
209      *****/
210     foreach($_POST as $name => $value){
212       if(preg_match("/^negOption_/",$name)){
214         $opt = preg_replace("/^negOption_/","",$name);
215         $opt = preg_replace("/_[^_]*$/","",$opt);
216         $id  = preg_replace("/^.*_([0-9])*$/","\\1",$opt);
217         $opt = preg_replace("/_[0-9]*$/","",$opt);
219         if(isset($this->sudoOption[$opt][$id])){
220           $val = $this->sudoOption[$opt][$id]["VALUE"];
222           /*****
223             Negate STRING_BOOL && BOOL_INTEGER
224            *****/
225           if(in_array($this->options[$opt]['TYPE'],array('STRING_BOOL','BOOL_INTEGER'))){
226             if(in_array($val, array("TRUE","FALSE"))){
227               if($val == "TRUE"){
228                 $this->sudoOption[$opt][$id]["VALUE"] = "FALSE";
229               }else{
230                 $this->sudoOption[$opt][$id]["VALUE"] = "TRUE";
231               }
232             }else{
233                $this->sudoOption[$opt][$id]['NEGATE'] = !$this->sudoOption[$opt][$id]['NEGATE']; 
234             }
235           }
237           /*****
238             Negate STRING / INTEGER
239            *****/
240           if(in_array($this->options[$opt]['TYPE'],array('STRING','INTEGER','LISTS'))){
241             $this->sudoOption[$opt][$id]['NEGATE'] = !$this->sudoOption[$opt][$id]['NEGATE']; 
242           }
244           /*****
245             Negate BOOLEAN
246            *****/
247           if(in_array($this->options[$opt]['TYPE'],array('BOOLEAN'))){
248             if($val == "TRUE"){
249               $this->sudoOption[$opt][$id]["VALUE"] = "FALSE";
250             }else{
251               $this->sudoOption[$opt][$id]["VALUE"] = "TRUE";
252             }
253           }
254         }
255         break;
256       }
258       /*****
259         Remove options
260        *****/
261       if(preg_match("/^delOption/",$name)){
262         $opt = preg_replace("/^delOption_/","",$name);
263         $opt = preg_replace("/_[^_]*$/","",$opt);
264         $id  = preg_replace("/^.*_([0-9])*$/","\\1",$opt);
265         $opt = preg_replace("/_[0-9]*$/","",$opt);
267         if(isset($this->sudoOption[$opt][$id])){
268           unset($this->sudoOption[$opt][$id]);
269         }
270         if(!count($this->sudoOption[$opt])){
271           unset($this->sudoOption[$opt]);
272         }
273         break;
274       }
275     }
277  
278     $smarty = get_smarty();
279     $smarty->assign("map",  array("STRING" => _("string"), "BOOLEAN" => _("bool"),
280       "INTEGER" => _("integer") , "BOOL_INTEGER" => _("integer")."-"._("bool") ,
281       "STRING_BOOL" => _("string")."-"._("bool"),"LISTS" => _("list")));
282     $smarty->assign("sudoOption",$this->prepare_for_html($this->sudoOption));
283     $smarty->assign("options",$this->options);
284     return($smarty->fetch(get_template_path('options.tpl', TRUE)));
285   }
288   /*! \brief  Prepare options array to be used in HTML.
289       @param  Array   The options array ($this->sudoOption) 
290       @return Array   HTML ready sudoOption. Can now be used in smarty templates
291    */
292   function prepare_for_html($a_options)
293   {
294     foreach($a_options as $name => $options){
295       foreach($options as $key => $option){
296         $a_options[$name][$key]['VALUE'] = htmlentities($option['VALUE']);
297       }
298     }
299     return($a_options);
300   }
303   /*! \brief  Removes this plugin 
304    */
305   function remove_from_parent()
306   {
307   }
310   /*! \brief  Saves all relevant HTML post values for this plugin 
311    */
312   function save_object()
313   {
314     plugin::save_object();
316     if(isset($_POST['add_option']) && isset($_POST['option'])){
317       $opt = get_post("option");
319       /* Append attribute only once, lists are handled below */
320       if(isset($this->options[$opt])){
321         $type = $this->options[$opt]['TYPE'];
322         $val  = $this->options[$opt]['DEFAULT'];
323         $option = array("NAME" => $opt, "VALUE" => $val , "NEGATE" => FALSE);
324         $this->sudoOption[$opt][] = $option;
325       }
326     }
328     foreach($this->sudoOption as $name => $opts){
329       foreach($opts as $id => $opt){
331         /****
332           Get posted value for BOOLEAN
333          ****/
334         if(in_array($this->options[$name]['TYPE'],array("BOOLEAN"))){
335           if(isset($_POST['option_value__'.$name.'_'.$id])){
336             $this->sudoOption[$name][$id]["VALUE"] = get_post('option_value__'.$name.'_'.$id);
337           }
338         }
340         /****
341           Get posted value for STRING / INTEGER
342          ****/
343         if(in_array($this->options[$name]['TYPE'],array("STRING","INTEGER"))){
344           if(isset($_POST['option_value__'.$name.'_'.$id])){
345             $this->sudoOption[$name][$id]["VALUE"] = get_post('option_value__'.$name.'_'.$id);
346           }
347         }
349         /****
350           Get posted value for STRING_BOOL / BOOL_INTEGER
351          ****/
352         if(in_array($this->options[$name]['TYPE'],array("BOOL_INTEGER","STRING_BOOL"))){
353           if(isset($_POST['option_selection__'.$name.'_'.$id])){
354             $sel = get_post('option_selection__'.$name.'_'.$id);
355             $val = "";
356             if(isset($_POST['option_value__'.$name.'_'.$id])){
357               $val = get_post('option_value__'.$name.'_'.$id);
358             }
360             if($sel == "FALSE" || $sel == "TRUE"){
361               $this->sudoOption[$name][$id]['VALUE'] = $sel;
362               $this->sudoOption[$name][$id]['NEGATE'] = FALSE;
363             }else{
364               $this->sudoOption[$name][$id]['VALUE'] = $val;
365             }
366           }
367         }
369         /****
370           Get posted value for LISTS
371          ****/
372         if(in_array($this->options[$name]['TYPE'],array("LISTS"))){
373           foreach($this->sudoOption[$name] as $entry_key => $entry){
374             if(isset($_POST['list_value__'.$name.'_'.$entry_key])){
375               $val = get_post('list_value__'.$name.'_'.$entry_key);
376               $this->sudoOption[$name][$entry_key]["VALUE"] = $val;
377             }
378           } 
379         }
380       }
381     }
382   }
385   /*! \brief  Save changes to ldap 
386    */
387   function save()
388   {
389     plugin::save(); 
391     $this->attrs['sudoOption'] = array();
392     foreach($this->sudoOption as $name => $opts){
393       foreach($opts as $id => $opt){
395         $type   = $this->options[$name]['TYPE'];
396         $neg    = $opt['NEGATE'];
397         $value  = $opt['VALUE'];
398         $option = "";
400         /* Escape special chars */
401         $value = addcslashes($value,"\\:,:=");
403         /****
404           Save LISTS 
405          ****/
406         if($type=="LISTS"){
407           if($value == ""){
408             $option = $name;
409           }else{
410             $option = $name."=".$value;
411           }
412           if($neg){
413             $option = "!".$option;
414           }
415         }
417         /****
418           Save BOOLEAN
419          ****/
420         if(in_array($type,array("BOOLEAN"))){ 
421           $option = $name;
422           if($value == "FALSE"){
423             $option = "!".$option;
424           }
425         }
427         /****
428           Save STRING / INTEGER
429          ****/
430         if(in_array($type,array("STRING","INTEGER"))){ 
431           if($value != ""){
432             $option = $name."=".$value;
433           }else{
434             $option = $name; 
435           }
436           if($neg){
437             $option = "!".$option;
438           }
439         }
441         /****
442           Save STRING_BOOL / BOOL_INTEGER
443          ****/
444         if(in_array($type,array("STRING_BOOL","BOOL_INTEGER"))){
445           if($value == "FALSE"){
446             $option = "!".$name;
447           }elseif($value == "TRUE"){
448             $option = $name;
449           }else{
450             if($value != ""){
451               $option = $name."=".$value;
452             }else{
453               $option = $name; 
454             }
455             if($neg){
456               $option = "!".$option;
457             }
458           }
459         }
461         $this->attrs['sudoOption'][] = $option;
462       }
463     }
464     $this->cleanup();
465     $ldap = $this->config->get_ldap_link();
466     $ldap->cd($this->dn);
467     $ldap->modify($this->attrs);;
468   }
471   /*! \brief  Checks input validity
472    */
473   function check()
474   {
475     $message = plugin::check();
477     foreach($this->sudoOption as $name => $options){
478       foreach($options as $id => $option){
479         switch($this->options[$name]['TYPE']){
481           /* Check for a valid integer value */
482           case 'INTEGER' : 
483             {
484               if(!preg_match("/^[0-9]*$/",$option['VALUE'])){
485                 $message[] = msgPool::invalid($name,$option['VALUE'],"/[0-9]/");
486               }
487             } break;
488         }
489       }
490     }
491     return ($message);
492   }
495   /*! \brief  This function will be called if an object gets copied.
496               This function adapts attributes from the source object.
497       @param  Array The source object.
498    */
499   function PrepareForCopyPaste($source)
500   {
501     plugin::PrepareForCopyPaste($source);
502     if(isset($source['sudoOption'])){
503       $this->attrs['sudoOption'] = $source['sudoOption'];
504       $this->load_options();
505     }
506   }
508 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
509 ?>