Code

95bb1386857fd4204248cc2f2f4bc34f90e78b18
[gosa.git] / gosa-core / plugins / admin / sudo / class_sudoOptions.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  */
23 class sudoOption extends plugin
24 {
25   /* Group attributes */
26   var $sudoOption = array();
28   var $attributes    = array("sudoOption");
29   var $is_account = TRUE;
30   
31   var $options = array();
33   function sudoOption(&$config, $dn= NULL)
34   {
35     plugin::plugin ($config, $dn);
37     /****
38       Create a list of known options
39      ****/
40     $options = array();
41     $option['long_otp_prompt']= array('NAME' =>'long_otp_prompt' ,   'TYPE' => 'BOOLEAN' ,   'DEFAULT' => '');
42     $option['ignore_dot']=  array('NAME' =>'ignore_dot' ,  'TYPE' => 'BOOLEAN' ,   'DEFAULT' => '');
43     $option['mail_always']= array('NAME' =>'mail_always' ,   'TYPE' => 'BOOLEAN' ,   'DEFAULT' => '');
44     $option['mail_badpass']=  array('NAME' =>'mail_badpass' ,  'TYPE' => 'BOOLEAN' ,   'DEFAULT' => '');
45     $option['mail_no_user']=  array('NAME' =>'mail_no_user' ,  'TYPE' => 'BOOLEAN' ,   'DEFAULT' => '');
46     $option['mail_no_host']=  array('NAME' =>'mail_no_host' ,  'TYPE' => 'BOOLEAN' ,   'DEFAULT' => '');
47     $option['mail_no_perms']= array('NAME' =>'mail_no_perms' ,   'TYPE' => 'BOOLEAN' ,   'DEFAULT' => '');
48     $option['tty_tickets']= array('NAME' =>'tty_tickets' ,   'TYPE' => 'BOOLEAN' ,   'DEFAULT' => '');
49     $option['authenticate']=  array('NAME' =>'authenticate' ,  'TYPE' => 'BOOLEAN' ,   'DEFAULT' => '');
50     $option['root_sudo']= array('NAME' =>'root_sudo' ,   'TYPE' => 'BOOLEAN' ,   'DEFAULT' => '');
51     $option['log_host']=  array('NAME' =>'log_host' ,  'TYPE' => 'BOOLEAN' ,   'DEFAULT' => '');
52     $option['log_year']=  array('NAME' =>'log_year' ,  'TYPE' => 'BOOLEAN' ,   'DEFAULT' => '');
53     $option['shell_noargs']=  array('NAME' =>'shell_noargs' ,  'TYPE' => 'BOOLEAN' ,   'DEFAULT' => '');
54     $option['set_home']=  array('NAME' =>'set_home' ,  'TYPE' => 'BOOLEAN' ,   'DEFAULT' => '');
55     $option['always_set_home']= array('NAME' =>'always_set_home' ,   'TYPE' => 'BOOLEAN' ,   'DEFAULT' => '');
56     $option['path_info']= array('NAME' =>'path_info' ,   'TYPE' => 'BOOLEAN' ,   'DEFAULT' => '');
57     $option['preserve_groups']= array('NAME' =>'preserve_groups' ,   'TYPE' => 'BOOLEAN' ,   'DEFAULT' => '');
58     $option['fqdn']=  array('NAME' =>'fqdn' ,  'TYPE' => 'BOOLEAN' ,   'DEFAULT' => '');
59     $option['insults']= array('NAME' =>'insults' ,   'TYPE' => 'BOOLEAN' ,   'DEFAULT' => '');
60     $option['requiretty']=  array('NAME' =>'requiretty' ,  'TYPE' => 'BOOLEAN' ,   'DEFAULT' => '');
61     $option['env_editor']=  array('NAME' =>'env_editor' ,  'TYPE' => 'BOOLEAN' ,   'DEFAULT' => '');
62     $option['rootpw']=  array('NAME' =>'rootpw' ,  'TYPE' => 'BOOLEAN' ,   'DEFAULT' => '');
63     $option['runaspw']= array('NAME' =>'runaspw' ,   'TYPE' => 'BOOLEAN' ,   'DEFAULT' => '');
64     $option['targetpw']=  array('NAME' =>'targetpw' ,  'TYPE' => 'BOOLEAN' ,   'DEFAULT' => '');
65     $option['set_logname']= array('NAME' =>'set_logname' ,   'TYPE' => 'BOOLEAN' ,   'DEFAULT' => '');
66     $option['stay_setuid']= array('NAME' =>'stay_setuid' ,   'TYPE' => 'BOOLEAN' ,   'DEFAULT' => '');
67     $option['env_reset']= array('NAME' =>'env_reset' ,   'TYPE' => 'BOOLEAN' ,   'DEFAULT' => '');
68     $option['use_loginclass']=  array('NAME' =>'use_loginclass' ,  'TYPE' => 'BOOLEAN' ,   'DEFAULT' => '');
69     $option['noexec']=  array('NAME' =>'noexec' ,  'TYPE' => 'BOOLEAN' ,   'DEFAULT' => '');
70     $option['ignore_local_sudoers']=  array('NAME' =>'ignore_local_sudoers' ,  'TYPE' => 'BOOLEAN' ,   'DEFAULT' => '');
71     $option['passwd_tries']=  array('NAME' =>'passwd_tries' ,  'TYPE' => 'INTEGER' ,   'DEFAULT' => '');
72     $option['loglinelen']=  array('NAME' =>'loglinelen' ,  'TYPE' => 'BOOL_INTEGER' ,  'DEFAULT' => '');
73     $option['timestamp_timeout']= array('NAME' =>'timestamp_timeout' ,   'TYPE' => 'BOOL_INTEGER' ,  'DEFAULT' => '');
74     $option['passwd_timeout']=  array('NAME' =>'passwd_timeout' ,  'TYPE' => 'BOOL_INTEGER' ,  'DEFAULT' => '');
75     $option['umask']= array('NAME' =>'umask' ,   'TYPE' => 'BOOL_INTEGER' ,  'DEFAULT' => '');
76     $option['mailsub']= array('NAME' =>'mailsub' ,   'TYPE' => 'STRING' ,   'DEFAULT' => '');
77     $option['badpass_message']= array('NAME' =>'badpass_message' ,   'TYPE' => 'STRING' ,   'DEFAULT' => '');
78     $option['timestampdir']=  array('NAME' =>'timestampdir' ,  'TYPE' => 'STRING' ,   'DEFAULT' => '');
79     $option['timestampowner']=  array('NAME' =>'timestampowner' ,  'TYPE' => 'STRING' ,   'DEFAULT' => '');
80     $option['passprompt']=  array('NAME' =>'passprompt' ,  'TYPE' => 'STRING' ,   'DEFAULT' => '');
81     $option['runas_default']= array('NAME' =>'runas_default' ,   'TYPE' => 'STRING' ,   'DEFAULT' => '');
82     $option['syslog_goodpri']=  array('NAME' =>'syslog_goodpri' ,  'TYPE' => 'STRING' ,   'DEFAULT' => '');
83     $option['syslog_badpri']= array('NAME' =>'syslog_badpri' ,   'TYPE' => 'STRING' ,   'DEFAULT' => '');
84     $option['editor']=  array('NAME' =>'editor' ,  'TYPE' => 'STRING' ,   'DEFAULT' => '');
85     $option['noexec_file']= array('NAME' =>'noexec_file' ,   'TYPE' => 'STRING' ,   'DEFAULT' => '');
86     $option['lecture']= array('NAME' =>'lecture' ,   'TYPE' => 'STRING_BOOL' ,   'DEFAULT' => '');
87     $option['lecture_file']=  array('NAME' =>'lecture_file' ,  'TYPE' => 'STRING_BOOL' ,   'DEFAULT' => '');
88     $option['logfile']= array('NAME' =>'logfile' ,   'TYPE' => 'STRING_BOOL' ,   'DEFAULT' => '');
89     $option['syslog']=  array('NAME' =>'syslog' ,  'TYPE' => 'STRING_BOOL' ,   'DEFAULT' => '');
90     $option['mailerpath']=  array('NAME' =>'mailerpath' ,  'TYPE' => 'STRING_BOOL' ,   'DEFAULT' => '');
91     $option['mailerflags']= array('NAME' =>'mailerflags' ,   'TYPE' => 'STRING_BOOL' ,   'DEFAULT' => '');
92     $option['mailto']=  array('NAME' =>'mailto' ,  'TYPE' => 'STRING_BOOL' ,   'DEFAULT' => '');
93     $option['exempt_group']=  array('NAME' =>'exempt_group' ,  'TYPE' => 'STRING_BOOL' ,   'DEFAULT' => '');
94     $option['verifypw']=  array('NAME' =>'verifypw' ,  'TYPE' => 'STRING_BOOL' ,   'DEFAULT' => '');
95     $option['listpw']=  array('NAME' =>'listpw' ,  'TYPE' => 'STRING_BOOL' ,   'DEFAULT' => '');
96     $option['env_check']= array('NAME' =>'env_check' ,   'TYPE' => 'LISTS' ,   'DEFAULT' => '');
97     $option['env_delete']=  array('NAME' =>'env_delete' ,  'TYPE' => 'LISTS' ,   'DEFAULT' => '');
98     $option['env_keep']=  array('NAME' =>'env_keep' ,  'TYPE' => 'LISTS' ,   'DEFAULT' => '');
99     ksort($option);
100     $this->options = $option;
102     /****
103       Parse given sudoOption attributes 
104      ****/
105     $this->sudoOption = array();
106     if(isset($this->attrs['sudoOption'])){
107       for($i = 0 ; $i < $this->attrs['sudoOption']['count']; $i++){
109         /****
110           Detect attribute name/value/negation
111          ****/
112         $opt = $this->attrs['sudoOption'][$i];
114         /* Get negation */ 
115         $negation = FALSE;
116         if(preg_match("/^!/",$opt)){
117           $negation = TRUE; 
118           $opt = preg_replace("/^!/","",$opt);
119         }
121         /* Get value / name*/
122         $value    = "";
123         if(preg_match("/=/",$opt)){
124           $value  = preg_replace("/^[^=]*+=/","",$opt);
125           $opt    = preg_replace("/=.*$/","",$opt);
126         }
128         /* Check if the given value is part of our options list.
129            If it is not, add it as type STRING and display a warning.  
130         */
131         if(!isset($this->options[$opt])){
132           $this->options[$opt]=array('NAME'=>$opt,'TYPE'=>'STRING','DEFAULT' => '');
133           msg_dialog::display(_("Unknown option"),
134               sprintf(_("The sudo option '%s' is unkown to GOsa, it is now temporarily added as type string."),
135                 $opt),WARNING_DIALOG);
136         }
138         /* Create internal sudoOption object */
139         $option = array();
140         $option['NAME']   = $opt;
141         $option['VALUE']  = $value;
142         $option['NEGATE'] = $negation;
144         /*  Special handling for mixed flag types. 
145             Some attributes like (BOOL_INTEGER) can be TRUE/FALSE and INTEGER.
146             This means, if the value is empty it is BOOL and $negation defines its boolean value.
147          */
148         if(in_array($this->options[$opt]['TYPE'],array("BOOL_INTEGER","STRING_BOOL"))){
149           if(empty($value)){
150             $option['NEGATE'] = FALSE;
151             if($negation){
152               $option['VALUE'] = "FALSE";
153             }else{
154               $option['VALUE'] = "TRUE";
155             }
156           }
157         }
159         if($this->options[$opt]['TYPE'] == "LISTS"){
160            $this->sudoOption[$opt][] = $option;
161         }
163         /* Special handling for BOOLEAN values */
164         if(in_array($this->options[$opt]['TYPE'],array("BOOLEAN"))){
165           $option['NEGATE'] = FALSE;
166           if($negation){
167             $option['VALUE'] = "FALSE";
168           }else{
169             $option['VALUE'] = "TRUE";
170           }
171         }
173         /* Append values */
174         if(!isset($this->sudoOption[$opt])){
175           $this->sudoOption[$opt] = $option;
176         }
177       }
178     }
179   }
181   function execute()
182   {
183     /* Call parent execute */
184     plugin::execute();
186     /*****
187       Handle Posts 
188      *****/
189     foreach($_POST as $name => $value){
191       if(preg_match("/^negListOption_/",$name)){
192         $opt = preg_replace("/^negListOption_/","",$name);
193         $opt = preg_replace("/_[^_]*$/","",$opt);
194         $id  = preg_replace("/^.*_([0-9])*$/","\\1",$opt);
195         $opt = preg_replace("/_[0-9]*$/","",$opt);
197         if(isset($this->sudoOption[$opt][$id])){
198           $this->sudoOption[$opt][$id]['NEGATE'] = !$this->sudoOption[$opt][$id]['NEGATE']; 
199         }
200         break;
201       }
203       if(preg_match("/^negOption_/",$name)){
204         $opt = preg_replace("/^negOption_/","",$name);
205         $opt = preg_replace("/_[^_]*$/","",$opt);
206         if(isset($this->sudoOption[$opt])){
207           $val = $this->sudoOption[$opt]["VALUE"];
209           /*****
210             Negate STRING_BOOL && BOOL_INTEGER
211            *****/
212           if(in_array($this->options[$opt]['TYPE'],array('STRING_BOOL','BOOL_INTEGER'))){
213             if(in_array($val, array("TRUE","FALSE"))){
214               if($val == "TRUE"){
215                 $this->sudoOption[$opt]["VALUE"] = "FALSE";
216               }else{
217                 $this->sudoOption[$opt]["VALUE"] = "TRUE";
218               }
219             }else{
220                $this->sudoOption[$opt]['NEGATE'] = !$this->sudoOption[$opt]['NEGATE']; 
221             }
222           }
224           /*****
225             Negate STRING / INTEGER
226            *****/
227           if(in_array($this->options[$opt]['TYPE'],array('STRING','INTEGER'))){
228             $this->sudoOption[$opt]['NEGATE'] = !$this->sudoOption[$opt]['NEGATE']; 
229           }
231           /*****
232             Negate BOOLEAN
233            *****/
234           if(in_array($this->options[$opt]['TYPE'],array('BOOLEAN'))){
235             if($val == "TRUE"){
236               $this->sudoOption[$opt]["VALUE"] = "FALSE";
237             }else{
238               $this->sudoOption[$opt]["VALUE"] = "TRUE";
239             }
240           }
241         }
242         break;
243       }
245       /*****
246         Remove options
247        *****/
248       if(preg_match("/^delOption_/",$name)){
249         $opt = preg_replace("/^delOption_/","",$name);
250         $opt = preg_replace("/_[^_]*$/","",$opt);
251         if(isset($this->sudoOption[$opt])){
252           unset($this->sudoOption[$opt]);
253         }
254         break;
255       }
257       /*****
258         Remove LISTS options
259        *****/
260       if(preg_match("/^delListOption/",$name)){
261         $opt = preg_replace("/^delListOption_/","",$name);
262         $opt = preg_replace("/_[^_]*$/","",$opt);
263         $id  = preg_replace("/^.*_([0-9])*$/","\\1",$opt);
264         $opt = preg_replace("/_[0-9]*$/","",$opt);
266         if(isset($this->sudoOption[$opt][$id])){
267           unset($this->sudoOption[$opt][$id]);
268         }
269         if(!count($this->sudoOption[$opt])){
270           unset($this->sudoOption[$opt]);
271         }
272         break;
273       }
274     }
276  
277     $smarty = get_smarty();
278     $smarty->assign("map",  array("STRING" => _("string"), "BOOLEAN" => _("bool"),
279       "INTEGER" => _("integer") , "BOOL_INTEGER" => _("integer")."-"._("bool") ,
280       "STRING_BOOL" => _("string")."-"._("bool"),"LISTS" => _("lists")));
281     $smarty->assign("sudoOption",$this->sudoOption);
282     $smarty->assign("options",$this->options);
283     return($smarty->fetch(get_template_path('options.tpl', TRUE)));
284   }
286   function remove_from_parent()
287   {
288   }
290   /* Save data to object */
291   function save_object()
292   {
293     plugin::save_object();
295     if(isset($_POST['add_option']) && isset($_POST['option'])){
296       $opt = get_post("option");
298       /* Append attribute only once, lists are handled below */
299       if(isset($this->options[$opt]) && !isset($this->sudoOption[$opt])){
300         $type = $this->options[$opt]['TYPE'];
301         $val  = $this->options[$opt]['DEFAULT'];
302         $option = array("NAME" => $opt, "VALUE" => $val , "NEGATE" => FALSE);
304         if($type == "LISTS"){
305           $this->sudoOption[$opt][] = $option;
306         }else{
307           $this->sudoOption[$opt] = $option;
308         }
309       }elseif(isset($this->options[$opt]) && isset($this->sudoOption[$opt]) && $this->options[$opt]['TYPE'] == "LISTS"){
310         $type = $this->options[$opt]['TYPE'];
311         $val  = $this->options[$opt]['DEFAULT'];
312         $option = array("NAME" => $opt, "VALUE" => $val , "NEGATE" => FALSE);
313         $this->sudoOption[$opt][] = $option;
314       }
315     }
317     foreach($this->sudoOption as $name => $opt){
319       /****
320         Get posted value for BOOLEAN
321        ****/
322       if(in_array($this->options[$name]['TYPE'],array("BOOLEAN"))){
323         if(isset($_POST['option_value__'.$name])){
324           $this->sudoOption[$name]["VALUE"] = get_post('option_value__'.$name);
325         }
326       }
328       /****
329         Get posted value for STRING / INTEGER
330        ****/
331       if(in_array($this->options[$name]['TYPE'],array("STRING","INTEGER"))){
332         if(isset($_POST['option_value__'.$name])){
333           $this->sudoOption[$name]["VALUE"] = get_post('option_value__'.$name);
334         }
335       }
337       /****
338         Get posted value for STRING_BOOL / BOOL_INTEGER
339        ****/
340       if(in_array($this->options[$name]['TYPE'],array("BOOL_INTEGER","STRING_BOOL"))){
341         if(isset($_POST['option_selection__'.$name])){
342           $sel = get_post('option_selection__'.$name);
343           $val = "";
344           if(isset($_POST['option_value__'.$name])){
345             $val = get_post('option_value__'.$name);
346           }
348           if($sel == "FALSE" || $sel == "TRUE"){
349             $this->sudoOption[$name]['VALUE'] = $sel;
350             $this->sudoOption[$name]['NEGATE'] = FALSE;
351           }else{
352             $this->sudoOption[$name]['VALUE'] = $val;
353           }
354         }
355       }
357       /****
358         Get posted value for LISTS
359        ****/
360       if(in_array($this->options[$name]['TYPE'],array("LISTS"))){
361         foreach($this->sudoOption[$name] as $entry_key => $entry){
362           if(isset($_POST['list_value__'.$name.'_'.$entry_key])){
363             $val = get_post('list_value__'.$name.'_'.$entry_key);
364             $this->sudoOption[$name][$entry_key]["VALUE"] = $val;
365           }
366         } 
367       }
368     }
369   }
371   /* Save to LDAP */
372   function save()
373   {
374     plugin::save(); 
376     $this->attrs['sudoOption'] = array();
377     foreach($this->sudoOption as $name => $opt){
379       $type   = $this->options[$name]['TYPE'];
381       if($type=="LISTS"){
382         foreach($this->sudoOption[$name] as $entry_key => $entry){
383           $val =  $entry["VALUE"];
384           if(empty($val)){
385             $option = $name;
386           }else{
387             $option = $name."=".$val;
388           }
389           if($entry['NEGATE']){
390             $option = "!".$option;
391           }
392           $this->attrs['sudoOption'][] = $option;
393         }
394         continue;
395       }
397       $neg    = $opt['NEGATE'];
398       $value  = $opt["VALUE"];
399       $option = "";
401       /****
402         Save BOOLEAN
403        ****/
404       if(in_array($type,array("BOOLEAN"))){ 
405         $option = $name;
406         if($value == "FALSE"){
407           $option = "!".$option;
408         }
409       }
411       /****
412         Save STRING / INTEGER
413        ****/
414       if(in_array($type,array("STRING","INTEGER"))){ 
415         $option = $name."=".$value;
416         if($neg){
417           $option = "!".$option;
418         }
419       }
421       /****
422         Save STRING_BOOL / BOOL_INTEGER
423        ****/
424       if(in_array($type,array("STRING_BOOL","BOOL_INTEGER"))){
425         if($value == "FALSE"){
426           $option = "!".$name;
427         }elseif($value == "TRUE"){
428           $option = $name;
429         }else{
430           $option = $name."=".$value;
431           if($neg){
432             $option = "!".$option;
433           }
434         }
435       }
437       $this->attrs['sudoOption'][] = $option;
438     }
439     $this->cleanup();
440     $ldap = $this->config->get_ldap_link();
441     $ldap->cd($this->dn);
442     $ldap->modify($this->attrs);;
443   }
445   function check()
446   {
447     $message = plugin::check();
448     return ($message);
449   }
451 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
452 ?>