Code

a798d2ea94e5674f2154e31f1e4742b1fb07e3c6
[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         $this->sudoOption[$opt][] = $option;
177       }
178     }
179   }
182   /*! \brief  Create HTML output for this plugin 
183       @return String  HTML output for this plugin.
184     */
185   function execute()
186   {
187     /* Call parent execute */
188     plugin::execute();
190     /*****
191       Handle Posts 
192      *****/
193     foreach($_POST as $name => $value){
195       if(preg_match("/^negOption_/",$name)){
197         $opt = preg_replace("/^negOption_/","",$name);
198         $opt = preg_replace("/_[^_]*$/","",$opt);
199         $id  = preg_replace("/^.*_([0-9])*$/","\\1",$opt);
200         $opt = preg_replace("/_[0-9]*$/","",$opt);
202         if(isset($this->sudoOption[$opt][$id])){
203           $val = $this->sudoOption[$opt][$id]["VALUE"];
205           /*****
206             Negate STRING_BOOL && BOOL_INTEGER
207            *****/
208           if(in_array($this->options[$opt]['TYPE'],array('STRING_BOOL','BOOL_INTEGER'))){
209             if(in_array($val, array("TRUE","FALSE"))){
210               if($val == "TRUE"){
211                 $this->sudoOption[$opt][$id]["VALUE"] = "FALSE";
212               }else{
213                 $this->sudoOption[$opt][$id]["VALUE"] = "TRUE";
214               }
215             }else{
216                $this->sudoOption[$opt][$id]['NEGATE'] = !$this->sudoOption[$opt][$id]['NEGATE']; 
217             }
218           }
220           /*****
221             Negate STRING / INTEGER
222            *****/
223           if(in_array($this->options[$opt]['TYPE'],array('STRING','INTEGER','LISTS'))){
224             $this->sudoOption[$opt][$id]['NEGATE'] = !$this->sudoOption[$opt][$id]['NEGATE']; 
225           }
227           /*****
228             Negate BOOLEAN
229            *****/
230           if(in_array($this->options[$opt]['TYPE'],array('BOOLEAN'))){
231             if($val == "TRUE"){
232               $this->sudoOption[$opt][$id]["VALUE"] = "FALSE";
233             }else{
234               $this->sudoOption[$opt][$id]["VALUE"] = "TRUE";
235             }
236           }
237         }
238         break;
239       }
241       /*****
242         Remove options
243        *****/
244       if(preg_match("/^delOption/",$name)){
245         $opt = preg_replace("/^delOption_/","",$name);
246         $opt = preg_replace("/_[^_]*$/","",$opt);
247         $id  = preg_replace("/^.*_([0-9])*$/","\\1",$opt);
248         $opt = preg_replace("/_[0-9]*$/","",$opt);
250         if(isset($this->sudoOption[$opt][$id])){
251           unset($this->sudoOption[$opt][$id]);
252         }
253         if(!count($this->sudoOption[$opt])){
254           unset($this->sudoOption[$opt]);
255         }
256         break;
257       }
258     }
260  
261     $smarty = get_smarty();
262     $smarty->assign("map",  array("STRING" => _("string"), "BOOLEAN" => _("bool"),
263       "INTEGER" => _("integer") , "BOOL_INTEGER" => _("integer")."-"._("bool") ,
264       "STRING_BOOL" => _("string")."-"._("bool"),"LISTS" => _("list")));
265     $smarty->assign("sudoOption",$this->sudoOption);
266     $smarty->assign("options",$this->options);
267     return($smarty->fetch(get_template_path('options.tpl', TRUE)));
268   }
271   /*! \brief  Removes this plugin 
272    */
273   function remove_from_parent()
274   {
275   }
278   /*! \brief  Saves all relevant HTML post values for this plugin 
279    */
280   function save_object()
281   {
282     plugin::save_object();
284     if(isset($_POST['add_option']) && isset($_POST['option'])){
285       $opt = get_post("option");
287       /* Append attribute only once, lists are handled below */
288       if(isset($this->options[$opt])){
289         $type = $this->options[$opt]['TYPE'];
290         $val  = $this->options[$opt]['DEFAULT'];
291         $option = array("NAME" => $opt, "VALUE" => $val , "NEGATE" => FALSE);
292         $this->sudoOption[$opt][] = $option;
293       }
294     }
296     foreach($this->sudoOption as $name => $opts){
297       foreach($opts as $id => $opt){
299         /****
300           Get posted value for BOOLEAN
301          ****/
302         if(in_array($this->options[$name]['TYPE'],array("BOOLEAN"))){
303           if(isset($_POST['option_value__'.$name.'_'.$id])){
304             $this->sudoOption[$name][$id]["VALUE"] = get_post('option_value__'.$name.'_'.$id);
305           }
306         }
308         /****
309           Get posted value for STRING / INTEGER
310          ****/
311         if(in_array($this->options[$name]['TYPE'],array("STRING","INTEGER"))){
312           if(isset($_POST['option_value__'.$name.'_'.$id])){
313             $this->sudoOption[$name][$id]["VALUE"] = get_post('option_value__'.$name.'_'.$id);
314           }
315         }
317         /****
318           Get posted value for STRING_BOOL / BOOL_INTEGER
319          ****/
320         if(in_array($this->options[$name]['TYPE'],array("BOOL_INTEGER","STRING_BOOL"))){
321           if(isset($_POST['option_selection__'.$name.'_'.$id])){
322             $sel = get_post('option_selection__'.$name.'_'.$id);
323             $val = "";
324             if(isset($_POST['option_value__'.$name.'_'.$id])){
325               $val = get_post('option_value__'.$name.'_'.$id);
326             }
328             if($sel == "FALSE" || $sel == "TRUE"){
329               $this->sudoOption[$name][$id]['VALUE'] = $sel;
330               $this->sudoOption[$name][$id]['NEGATE'] = FALSE;
331             }else{
332               $this->sudoOption[$name][$id]['VALUE'] = $val;
333             }
334           }
335         }
337         /****
338           Get posted value for LISTS
339          ****/
340         if(in_array($this->options[$name]['TYPE'],array("LISTS"))){
341           foreach($this->sudoOption[$name] as $entry_key => $entry){
342             if(isset($_POST['list_value__'.$name.'_'.$entry_key])){
343               $val = get_post('list_value__'.$name.'_'.$entry_key);
344               $this->sudoOption[$name][$entry_key]["VALUE"] = $val;
345             }
346           } 
347         }
348       }
349     }
350   }
353   /*! \brief  Save changes to ldap 
354    */
355   function save()
356   {
357     plugin::save(); 
359     $this->attrs['sudoOption'] = array();
360     foreach($this->sudoOption as $name => $opts){
361       foreach($opts as $id => $opt){
363         $type   = $this->options[$name]['TYPE'];
364         $neg    = $opt['NEGATE'];
365         $value  = $opt['VALUE'];
366         $option = "";
368         /****
369           Save LISTS 
370          ****/
371         if($type=="LISTS"){
372           if($value == ""){
373             $option = $name;
374           }else{
375             $option = $name."=".$value;
376           }
377           if($neg){
378             $option = "!".$option;
379           }
380         }
382         /****
383           Save BOOLEAN
384          ****/
385         if(in_array($type,array("BOOLEAN"))){ 
386           $option = $name;
387           if($value == "FALSE"){
388             $option = "!".$option;
389           }
390         }
392         /****
393           Save STRING / INTEGER
394          ****/
395         if(in_array($type,array("STRING","INTEGER"))){ 
396           if($value != ""){
397             $option = $name."=".$value;
398           }else{
399             $option = $name; 
400           }
401           if($neg){
402             $option = "!".$option;
403           }
404         }
406         /****
407           Save STRING_BOOL / BOOL_INTEGER
408          ****/
409         if(in_array($type,array("STRING_BOOL","BOOL_INTEGER"))){
410           if($value == "FALSE"){
411             $option = "!".$name;
412           }elseif($value == "TRUE"){
413             $option = $name;
414           }else{
415             if($value != ""){
416               $option = $name."=".$value;
417             }else{
418               $option = $name; 
419             }
420             if($neg){
421               $option = "!".$option;
422             }
423           }
424         }
426         $this->attrs['sudoOption'][] = $option;
427       }
428     }
429     $this->cleanup();
430     $ldap = $this->config->get_ldap_link();
431     $ldap->cd($this->dn);
432     $ldap->modify($this->attrs);;
433   }
435   
436   /*! \brief  Checks input validity
437    */
438   function check()
439   {
440     $message = plugin::check();
441     return ($message);
442   }
444 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
445 ?>