Code

Sudo update
[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' => '');
48     $option['ignore_dot']=  array('NAME' =>'ignore_dot' ,  'TYPE' => 'BOOLEAN' ,   'DEFAULT' => '');
49     $option['mail_always']= array('NAME' =>'mail_always' ,   'TYPE' => 'BOOLEAN' ,   'DEFAULT' => '');
50     $option['mail_badpass']=  array('NAME' =>'mail_badpass' ,  'TYPE' => 'BOOLEAN' ,   'DEFAULT' => '');
51     $option['mail_no_user']=  array('NAME' =>'mail_no_user' ,  'TYPE' => 'BOOLEAN' ,   'DEFAULT' => '');
52     $option['mail_no_host']=  array('NAME' =>'mail_no_host' ,  'TYPE' => 'BOOLEAN' ,   'DEFAULT' => '');
53     $option['mail_no_perms']= array('NAME' =>'mail_no_perms' ,   'TYPE' => 'BOOLEAN' ,   'DEFAULT' => '');
54     $option['tty_tickets']= array('NAME' =>'tty_tickets' ,   'TYPE' => 'BOOLEAN' ,   'DEFAULT' => '');
55     $option['authenticate']=  array('NAME' =>'authenticate' ,  'TYPE' => 'BOOLEAN' ,   'DEFAULT' => '');
56     $option['root_sudo']= array('NAME' =>'root_sudo' ,   'TYPE' => 'BOOLEAN' ,   'DEFAULT' => '');
57     $option['log_host']=  array('NAME' =>'log_host' ,  'TYPE' => 'BOOLEAN' ,   'DEFAULT' => '');
58     $option['log_year']=  array('NAME' =>'log_year' ,  'TYPE' => 'BOOLEAN' ,   'DEFAULT' => '');
59     $option['shell_noargs']=  array('NAME' =>'shell_noargs' ,  'TYPE' => 'BOOLEAN' ,   'DEFAULT' => '');
60     $option['set_home']=  array('NAME' =>'set_home' ,  'TYPE' => 'BOOLEAN' ,   'DEFAULT' => '');
61     $option['always_set_home']= array('NAME' =>'always_set_home' ,   'TYPE' => 'BOOLEAN' ,   'DEFAULT' => '');
62     $option['path_info']= array('NAME' =>'path_info' ,   'TYPE' => 'BOOLEAN' ,   'DEFAULT' => '');
63     $option['preserve_groups']= array('NAME' =>'preserve_groups' ,   'TYPE' => 'BOOLEAN' ,   'DEFAULT' => '');
64     $option['fqdn']=  array('NAME' =>'fqdn' ,  'TYPE' => 'BOOLEAN' ,   'DEFAULT' => '');
65     $option['insults']= array('NAME' =>'insults' ,   'TYPE' => 'BOOLEAN' ,   'DEFAULT' => '');
66     $option['requiretty']=  array('NAME' =>'requiretty' ,  'TYPE' => 'BOOLEAN' ,   'DEFAULT' => '');
67     $option['env_editor']=  array('NAME' =>'env_editor' ,  'TYPE' => 'BOOLEAN' ,   'DEFAULT' => '');
68     $option['rootpw']=  array('NAME' =>'rootpw' ,  'TYPE' => 'BOOLEAN' ,   'DEFAULT' => '');
69     $option['runaspw']= array('NAME' =>'runaspw' ,   'TYPE' => 'BOOLEAN' ,   'DEFAULT' => '');
70     $option['targetpw']=  array('NAME' =>'targetpw' ,  'TYPE' => 'BOOLEAN' ,   'DEFAULT' => '');
71     $option['set_logname']= array('NAME' =>'set_logname' ,   'TYPE' => 'BOOLEAN' ,   'DEFAULT' => '');
72     $option['stay_setuid']= array('NAME' =>'stay_setuid' ,   'TYPE' => 'BOOLEAN' ,   'DEFAULT' => '');
73     $option['env_reset']= array('NAME' =>'env_reset' ,   'TYPE' => 'BOOLEAN' ,   'DEFAULT' => '');
74     $option['use_loginclass']=  array('NAME' =>'use_loginclass' ,  'TYPE' => 'BOOLEAN' ,   'DEFAULT' => '');
75     $option['noexec']=  array('NAME' =>'noexec' ,  'TYPE' => 'BOOLEAN' ,   'DEFAULT' => '');
76     $option['ignore_local_sudoers']=  array('NAME' =>'ignore_local_sudoers' ,  'TYPE' => 'BOOLEAN' ,   'DEFAULT' => '');
77     $option['passwd_tries']=  array('NAME' =>'passwd_tries' ,  'TYPE' => 'INTEGER' ,   'DEFAULT' => '');
78     $option['loglinelen']=  array('NAME' =>'loglinelen' ,  'TYPE' => 'BOOL_INTEGER' ,  'DEFAULT' => '');
79     $option['timestamp_timeout']= array('NAME' =>'timestamp_timeout' ,   'TYPE' => 'BOOL_INTEGER' ,  'DEFAULT' => '');
80     $option['passwd_timeout']=  array('NAME' =>'passwd_timeout' ,  'TYPE' => 'BOOL_INTEGER' ,  'DEFAULT' => '');
81     $option['umask']= array('NAME' =>'umask' ,   'TYPE' => 'BOOL_INTEGER' ,  'DEFAULT' => '');
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     /****
109       Parse given sudoOption attributes 
110      ****/
111     $this->sudoOption = array();
112     if(isset($this->attrs['sudoOption'])){
113       for($i = 0 ; $i < $this->attrs['sudoOption']['count']; $i++){
115         /****
116           Detect attribute name/value/negation
117          ****/
118         $opt = $this->attrs['sudoOption'][$i];
120         /* Get negation */ 
121         $negation = FALSE;
122         if(preg_match("/^!/",$opt)){
123           $negation = TRUE; 
124           $opt = preg_replace("/^!/","",$opt);
125         }
127         /* Get value / name*/
128         $value    = "";
129         if(preg_match("/=/",$opt)){
130           $value  = preg_replace("/^[^=]*+=/","",$opt);
131           $opt    = preg_replace("/=.*$/","",$opt);
132         }
134         /* Check if the given value is part of our options list.
135            If it is not, add it as type STRING and display a warning.  
136         */
137         if(!isset($this->options[$opt])){
138           $this->options[$opt]=array('NAME'=>$opt,'TYPE'=>'STRING','DEFAULT' => '');
139           msg_dialog::display(_("Unknown option"),
140               sprintf(_("The sudo option '%s' is invalid!"),
141                 $opt),WARNING_DIALOG);
142         }
144         /* Create internal sudoOption object */
145         $option = array();
146         $option['NAME']   = $opt;
147         $option['VALUE']  = $value;
148         $option['NEGATE'] = $negation;
150         /*  Special handling for mixed flag types. 
151             Some attributes like (BOOL_INTEGER) can be TRUE/FALSE and INTEGER.
152             This means, if the value is empty it is BOOL and $negation defines its boolean value.
153          */
154         if(in_array($this->options[$opt]['TYPE'],array("BOOL_INTEGER","STRING_BOOL"))){
155           if(empty($value)){
156             $option['NEGATE'] = FALSE;
157             if($negation){
158               $option['VALUE'] = "FALSE";
159             }else{
160               $option['VALUE'] = "TRUE";
161             }
162           }
163         }
165         /* Special handling for BOOLEAN values */
166         if(in_array($this->options[$opt]['TYPE'],array("BOOLEAN"))){
167           $option['NEGATE'] = FALSE;
168           if($negation){
169             $option['VALUE'] = "FALSE";
170           }else{
171             $option['VALUE'] = "TRUE";
172           }
173         }
175         /* Append values */
176         if(!isset($this->sudoOption[$opt])){
177           $this->sudoOption[$opt][] = $option;
178         }
179       }
180     }
181   }
184   /*! \brief  Create HTML output for this plugin 
185       @return String  HTML output for this plugin.
186     */
187   function execute()
188   {
189     /* Call parent execute */
190     plugin::execute();
192     /*****
193       Handle Posts 
194      *****/
195     foreach($_POST as $name => $value){
197       if(preg_match("/^negOption_/",$name)){
199         $opt = preg_replace("/^negOption_/","",$name);
200         $opt = preg_replace("/_[^_]*$/","",$opt);
201         $id  = preg_replace("/^.*_([0-9])*$/","\\1",$opt);
202         $opt = preg_replace("/_[0-9]*$/","",$opt);
204         if(isset($this->sudoOption[$opt][$id])){
205           $val = $this->sudoOption[$opt][$id]["VALUE"];
207           /*****
208             Negate STRING_BOOL && BOOL_INTEGER
209            *****/
210           if(in_array($this->options[$opt]['TYPE'],array('STRING_BOOL','BOOL_INTEGER'))){
211             if(in_array($val, array("TRUE","FALSE"))){
212               if($val == "TRUE"){
213                 $this->sudoOption[$opt][$id]["VALUE"] = "FALSE";
214               }else{
215                 $this->sudoOption[$opt][$id]["VALUE"] = "TRUE";
216               }
217             }else{
218                $this->sudoOption[$opt][$id]['NEGATE'] = !$this->sudoOption[$opt][$id]['NEGATE']; 
219             }
220           }
222           /*****
223             Negate STRING / INTEGER
224            *****/
225           if(in_array($this->options[$opt]['TYPE'],array('STRING','INTEGER','LISTS'))){
226             $this->sudoOption[$opt][$id]['NEGATE'] = !$this->sudoOption[$opt][$id]['NEGATE']; 
227           }
229           /*****
230             Negate BOOLEAN
231            *****/
232           if(in_array($this->options[$opt]['TYPE'],array('BOOLEAN'))){
233             if($val == "TRUE"){
234               $this->sudoOption[$opt][$id]["VALUE"] = "FALSE";
235             }else{
236               $this->sudoOption[$opt][$id]["VALUE"] = "TRUE";
237             }
238           }
239         }
240         break;
241       }
243       /*****
244         Remove options
245        *****/
246       if(preg_match("/^delOption/",$name)){
247         $opt = preg_replace("/^delOption_/","",$name);
248         $opt = preg_replace("/_[^_]*$/","",$opt);
249         $id  = preg_replace("/^.*_([0-9])*$/","\\1",$opt);
250         $opt = preg_replace("/_[0-9]*$/","",$opt);
252         if(isset($this->sudoOption[$opt][$id])){
253           unset($this->sudoOption[$opt][$id]);
254         }
255         if(!count($this->sudoOption[$opt])){
256           unset($this->sudoOption[$opt]);
257         }
258         break;
259       }
260     }
262  
263     $smarty = get_smarty();
264     $smarty->assign("map",  array("STRING" => _("string"), "BOOLEAN" => _("bool"),
265       "INTEGER" => _("integer") , "BOOL_INTEGER" => _("integer")."-"._("bool") ,
266       "STRING_BOOL" => _("string")."-"._("bool"),"LISTS" => _("list")));
267     $smarty->assign("sudoOption",$this->sudoOption);
268     $smarty->assign("options",$this->options);
269     return($smarty->fetch(get_template_path('options.tpl', TRUE)));
270   }
273   /*! \brief  Removes this plugin 
274    */
275   function remove_from_parent()
276   {
277   }
280   /*! \brief  Saves all relevant HTML post values for this plugin 
281    */
282   function save_object()
283   {
284     plugin::save_object();
286     if(isset($_POST['add_option']) && isset($_POST['option'])){
287       $opt = get_post("option");
289       /* Append attribute only once, lists are handled below */
290       if(isset($this->options[$opt])){
291         $type = $this->options[$opt]['TYPE'];
292         $val  = $this->options[$opt]['DEFAULT'];
293         $option = array("NAME" => $opt, "VALUE" => $val , "NEGATE" => FALSE);
294         $this->sudoOption[$opt][] = $option;
295       }
296     }
298     foreach($this->sudoOption as $name => $opts){
299       foreach($opts as $id => $opt){
301         /****
302           Get posted value for BOOLEAN
303          ****/
304         if(in_array($this->options[$name]['TYPE'],array("BOOLEAN"))){
305           if(isset($_POST['option_value__'.$name.'_'.$id])){
306             $this->sudoOption[$name][$id]["VALUE"] = get_post('option_value__'.$name.'_'.$id);
307           }
308         }
310         /****
311           Get posted value for STRING / INTEGER
312          ****/
313         if(in_array($this->options[$name]['TYPE'],array("STRING","INTEGER"))){
314           if(isset($_POST['option_value__'.$name.'_'.$id])){
315             $this->sudoOption[$name][$id]["VALUE"] = get_post('option_value__'.$name.'_'.$id);
316           }
317         }
319         /****
320           Get posted value for STRING_BOOL / BOOL_INTEGER
321          ****/
322         if(in_array($this->options[$name]['TYPE'],array("BOOL_INTEGER","STRING_BOOL"))){
323           if(isset($_POST['option_selection__'.$name.'_'.$id])){
324             $sel = get_post('option_selection__'.$name.'_'.$id);
325             $val = "";
326             if(isset($_POST['option_value__'.$name.'_'.$id])){
327               $val = get_post('option_value__'.$name.'_'.$id);
328             }
330             if($sel == "FALSE" || $sel == "TRUE"){
331               $this->sudoOption[$name][$id]['VALUE'] = $sel;
332               $this->sudoOption[$name][$id]['NEGATE'] = FALSE;
333             }else{
334               $this->sudoOption[$name][$id]['VALUE'] = $val;
335             }
336           }
337         }
339         /****
340           Get posted value for LISTS
341          ****/
342         if(in_array($this->options[$name]['TYPE'],array("LISTS"))){
343           foreach($this->sudoOption[$name] as $entry_key => $entry){
344             if(isset($_POST['list_value__'.$name.'_'.$entry_key])){
345               $val = get_post('list_value__'.$name.'_'.$entry_key);
346               $this->sudoOption[$name][$entry_key]["VALUE"] = $val;
347             }
348           } 
349         }
350       }
351     }
352   }
355   /*! \brief  Save changes to ldap 
356    */
357   function save()
358   {
359     plugin::save(); 
361     $this->attrs['sudoOption'] = array();
362     foreach($this->sudoOption as $name => $opts){
363       foreach($opts as $id => $opt){
365         $type   = $this->options[$name]['TYPE'];
366         $neg    = $opt['NEGATE'];
367         $value  = $opt['VALUE'];
368         $option = "";
370         /****
371           Save LISTS 
372          ****/
373         if($type=="LISTS"){
374           if(empty($value)){
375             $option = $name;
376           }else{
377             $option = $name."=".$value;
378           }
379           if($neg){
380             $option = "!".$option;
381           }
382         }
384         /****
385           Save BOOLEAN
386          ****/
387         if(in_array($type,array("BOOLEAN"))){ 
388           $option = $name;
389           if($value == "FALSE"){
390             $option = "!".$option;
391           }
392         }
394         /****
395           Save STRING / INTEGER
396          ****/
397         if(in_array($type,array("STRING","INTEGER"))){ 
398           if(!empty($value)){
399             $option = $name."=".$value;
400           }else{
401             $option = $name; 
402           }
403           if($neg){
404             $option = "!".$option;
405           }
406         }
408         /****
409           Save STRING_BOOL / BOOL_INTEGER
410          ****/
411         if(in_array($type,array("STRING_BOOL","BOOL_INTEGER"))){
412           if($value == "FALSE"){
413             $option = "!".$name;
414           }elseif($value == "TRUE"){
415             $option = $name;
416           }else{
417             if(!empty($value)){
418               $option = $name."=".$value;
419             }else{
420               $option = $name; 
421             }
422             if($neg){
423               $option = "!".$option;
424             }
425           }
426         }
428         $this->attrs['sudoOption'][] = $option;
429       }
430     }
431     $this->cleanup();
432     $ldap = $this->config->get_ldap_link();
433     $ldap->cd($this->dn);
434     $ldap->modify($this->attrs);;
435   }
437   
438   /*! \brief  Checks input validity
439    */
440   function check()
441   {
442     $message = plugin::check();
443     return ($message);
444   }
446 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
447 ?>