Code

Updated sudo checks
[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   protected $sudoOption = array();
31   public $attributes    = array("sudoOption");
32   private $options = array();
33   public $ignore_account = TRUE;
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' => 'FALSE');
48     $option['ignore_dot']=  array('NAME' =>'ignore_dot' ,  'TYPE' => 'BOOLEAN' ,   'DEFAULT' => 'TRUE');
49     $option['mail_always']= array('NAME' =>'mail_always' ,   'TYPE' => 'BOOLEAN' ,   'DEFAULT' => 'FALSE');
50     $option['mail_badpass']=  array('NAME' =>'mail_badpass' ,  'TYPE' => 'BOOLEAN' ,   'DEFAULT' => 'FALSE');
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' => 'FALSE');
53     $option['mail_no_perms']= array('NAME' =>'mail_no_perms' ,   'TYPE' => 'BOOLEAN' ,   'DEFAULT' => 'FALSE');
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' => 'FALSE');
58     $option['log_year']=  array('NAME' =>'log_year' ,  'TYPE' => 'BOOLEAN' ,   'DEFAULT' => 'FALSE');
59     $option['shell_noargs']=  array('NAME' =>'shell_noargs' ,  'TYPE' => 'BOOLEAN' ,   'DEFAULT' => 'FALSE');
60     $option['set_home']=  array('NAME' =>'set_home' ,  'TYPE' => 'BOOLEAN' ,   'DEFAULT' => 'FALSE');
61     $option['always_set_home']= array('NAME' =>'always_set_home' ,   'TYPE' => 'BOOLEAN' ,   'DEFAULT' => 'FALSE');
62     $option['path_info']= array('NAME' =>'path_info' ,   'TYPE' => 'BOOLEAN' ,   'DEFAULT' => 'FALSE');
63     $option['preserve_groups']= array('NAME' =>'preserve_groups' ,   'TYPE' => 'BOOLEAN' ,   'DEFAULT' => 'FALSE');
64     $option['fqdn']=  array('NAME' =>'fqdn' ,  'TYPE' => 'BOOLEAN' ,   'DEFAULT' => 'FALSE');
65     $option['insults']= array('NAME' =>'insults' ,   'TYPE' => 'BOOLEAN' ,   'DEFAULT' => 'FALSE');
66     $option['requiretty']=  array('NAME' =>'requiretty' ,  'TYPE' => 'BOOLEAN' ,   'DEFAULT' => 'FALSE');
67     $option['env_editor']=  array('NAME' =>'env_editor' ,  'TYPE' => 'BOOLEAN' ,   'DEFAULT' => 'TRUE');
68     $option['rootpw']=  array('NAME' =>'rootpw' ,  'TYPE' => 'BOOLEAN' ,   'DEFAULT' => 'FALSE');
69     $option['runaspw']= array('NAME' =>'runaspw' ,   'TYPE' => 'BOOLEAN' ,   'DEFAULT' => 'FALSE');
70     $option['targetpw']=  array('NAME' =>'targetpw' ,  'TYPE' => 'BOOLEAN' ,   'DEFAULT' => 'FALSE');
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' => 'FALSE');
75     $option['noexec']=  array('NAME' =>'noexec' ,  'TYPE' => 'BOOLEAN' ,   'DEFAULT' => 'FALSE');
76     $option['ignore_local_sudoers']=  array('NAME' =>'ignore_local_sudoers' ,  'TYPE' => 'BOOLEAN' ,   'DEFAULT' => 'FALSE');
77     $option['passwd_tries']=  array('NAME' =>'passwd_tries' ,  'TYPE' => 'INTEGER' ,   'DEFAULT' => 3);
78     $option['loglinelen']=  array('NAME' =>'loglinelen' ,  'TYPE' => 'BOOL_INTEGER' ,  'DEFAULT' => 80);
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' => 15);
81     $option['umask']= array('NAME' =>'umask' ,   'TYPE' => 'BOOL_INTEGER' ,  'DEFAULT' => "0022");
82     $option['mailsub']= array('NAME' =>'mailsub' ,   'TYPE' => 'STRING' ,   'DEFAULT' => '*** SECURITY information for %h ***');
83     $option['badpass_message']= array('NAME' =>'badpass_message' ,   'TYPE' => 'STRING' ,   'DEFAULT' => 'Sorry, try again');
84     $option['timestampdir']=  array('NAME' =>'timestampdir' ,  'TYPE' => 'STRING' ,   'DEFAULT' => '/var/run/sudo');
85     $option['timestampowner']=  array('NAME' =>'timestampowner' ,  'TYPE' => 'STRING' ,   'DEFAULT' => 'root');
86     $option['passprompt']=  array('NAME' =>'passprompt' ,  'TYPE' => 'STRING' ,   'DEFAULT' => '[sudo] password for %p: ');
87     $option['runas_default']= array('NAME' =>'runas_default' ,   'TYPE' => 'STRING' ,   'DEFAULT' => 'root');
88     $option['syslog_goodpri']=  array('NAME' =>'syslog_goodpri' ,  'TYPE' => 'STRING' ,   'DEFAULT' => 'notice');
89     $option['syslog_badpri']= array('NAME' =>'syslog_badpri' ,   'TYPE' => 'STRING' ,   'DEFAULT' => 'alert');
90     $option['editor']=  array('NAME' =>'editor' ,  'TYPE' => 'STRING' ,   'DEFAULT' => '/usr/bin/vi');
91     $option['noexec_file']= array('NAME' =>'noexec_file' ,   'TYPE' => 'STRING' ,   'DEFAULT' => '/usr/lib/sudo/sudo_noexec.so');
92     $option['lecture']= array('NAME' =>'lecture' ,   'TYPE' => 'STRING_BOOL' ,   'DEFAULT' => 'once');
93     $option['lecture_file']=  array('NAME' =>'lecture_file' ,  'TYPE' => 'STRING_BOOL' ,   'DEFAULT' => '');
94     $option['logfile']= array('NAME' =>'logfile' ,   'TYPE' => 'STRING_BOOL' ,   'DEFAULT' => 'syslog');
95     $option['syslog']=  array('NAME' =>'syslog' ,  'TYPE' => 'STRING_BOOL' ,   'DEFAULT' => 'authpriv');
96     $option['mailerpath']=  array('NAME' =>'mailerpath' ,  'TYPE' => 'STRING_BOOL' ,   'DEFAULT' => '');
97     $option['mailerflags']= array('NAME' =>'mailerflags' ,   'TYPE' => 'STRING_BOOL' ,   'DEFAULT' => '-t');
98     $option['mailto']=  array('NAME' =>'mailto' ,  'TYPE' => 'STRING_BOOL' ,   'DEFAULT' => 'root');
99     $option['exempt_group']=  array('NAME' =>'exempt_group' ,  'TYPE' => 'STRING_BOOL' ,   'DEFAULT' => 'root');
100     $option['verifypw']=  array('NAME' =>'verifypw' ,  'TYPE' => 'STRING_BOOL' ,   'DEFAULT' => 'all');
101     $option['listpw']=  array('NAME' =>'listpw' ,  'TYPE' => 'STRING_BOOL' ,   'DEFAULT' => 'any');
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 = $this->unescape_command($value);
149         /* Check if the given value is part of our options list.
150            If it is not, add it as type STRING and display a warning.  
151         */
152         if(!isset($this->options[$opt])){
153           $this->options[$opt]=array('NAME'=>$opt,'TYPE'=>'STRING','DEFAULT' => '');
154           msg_dialog::display(_("Unknown option"),
155               sprintf(_("The sudo option '%s' is invalid!"),
156                 $opt),WARNING_DIALOG);
157         }
159         /* Create internal sudoOption object */
160         $option = array();
161         $option['NAME']   = $opt;
162         $option['VALUE']  = $value;
163         $option['NEGATE'] = $negation;
165         /*  Special handling for mixed flag types. 
166             Some attributes like (BOOL_INTEGER) can be TRUE/FALSE and INTEGER.
167             This means, if the value is empty it is BOOL and $negation defines its boolean value.
168          */
169         if(in_array($this->options[$opt]['TYPE'],array("BOOL_INTEGER","STRING_BOOL"))){
170           if(empty($value)){
171             $option['NEGATE'] = FALSE;
172             if($negation){
173               $option['VALUE'] = "FALSE";
174             }else{
175               $option['VALUE'] = "TRUE";
176             }
177           }
178         }
180         /* Special handling for BOOLEAN values */
181         if(in_array($this->options[$opt]['TYPE'],array("BOOLEAN"))){
182           $option['NEGATE'] = FALSE;
183           if($negation){
184             $option['VALUE'] = "FALSE";
185           }else{
186             $option['VALUE'] = "TRUE";
187           }
188         }
190         /* Append values */
191         $this->sudoOption[$opt][] = $option;
192       }
193     }
194   }
198   /*! \brief  Create HTML output for this plugin 
199       @return String  HTML output for this plugin.
200     */
201   function execute()
202   {
203     /* Call parent execute */
204     plugin::execute();
206     /*****
207       Handle Posts 
208      *****/
209     foreach($_POST as $name => $value){
211       if(preg_match("/^negOption_/",$name)){
213         $opt = preg_replace("/^negOption_/","",$name);
214         $opt = preg_replace("/_[^_]*$/","",$opt);
215         $id  = preg_replace("/^.*_([0-9])*$/","\\1",$opt);
216         $opt = preg_replace("/_[0-9]*$/","",$opt);
218         if(isset($this->sudoOption[$opt][$id])){
219           $val = $this->sudoOption[$opt][$id]["VALUE"];
221           /*****
222             Negate STRING_BOOL && BOOL_INTEGER
223            *****/
224           if(in_array($this->options[$opt]['TYPE'],array('STRING_BOOL','BOOL_INTEGER'))){
225             if(in_array($val, array("TRUE","FALSE"))){
226               if($val == "TRUE"){
227                 $this->sudoOption[$opt][$id]["VALUE"] = "FALSE";
228               }else{
229                 $this->sudoOption[$opt][$id]["VALUE"] = "TRUE";
230               }
231             }else{
232                $this->sudoOption[$opt][$id]['NEGATE'] = !$this->sudoOption[$opt][$id]['NEGATE']; 
233             }
234           }
236           /*****
237             Negate STRING / INTEGER
238            *****/
239           if(in_array($this->options[$opt]['TYPE'],array('STRING','INTEGER','LISTS'))){
240             $this->sudoOption[$opt][$id]['NEGATE'] = !$this->sudoOption[$opt][$id]['NEGATE']; 
241           }
243           /*****
244             Negate BOOLEAN
245            *****/
246           if(in_array($this->options[$opt]['TYPE'],array('BOOLEAN'))){
247             if($val == "TRUE"){
248               $this->sudoOption[$opt][$id]["VALUE"] = "FALSE";
249             }else{
250               $this->sudoOption[$opt][$id]["VALUE"] = "TRUE";
251             }
252           }
253         }
254         break;
255       }
257       /*****
258         Remove options
259        *****/
260       if(preg_match("/^delOption/",$name)){
261         $opt = preg_replace("/^delOption_/","",$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" => _("list")));
281     $smarty->assign("sudoOption",$this->prepare_for_html($this->sudoOption));
282     $smarty->assign("options",$this->options);
283     return($smarty->fetch(get_template_path('options.tpl', TRUE)));
284   }
287   /*! \brief  Prepare options array to be used in HTML.
288       @param  Array   The options array ($this->sudoOption) 
289       @return Array   HTML ready sudoOption. Can now be used in smarty templates
290    */
291   function prepare_for_html($a_options)
292   {
293     foreach($a_options as $name => $options){
294       foreach($options as $key => $option){
295         $a_options[$name][$key]['VALUE'] = htmlentities($option['VALUE']);
296       }
297     }
298     return($a_options);
299   }
302   /*! \brief  Removes this plugin 
303    */
304   function remove_from_parent()
305   {
306   }
309   /*! \brief  Saves all relevant HTML post values for this plugin 
310    */
311   function save_object()
312   {
313     plugin::save_object();
315     if(isset($_POST['add_option']) && isset($_POST['option'])){
316       $opt = get_post("option");
318       /* Append attribute only once, lists are handled below */
319       if(isset($this->options[$opt])){
320         $type = $this->options[$opt]['TYPE'];
321         $val  = $this->options[$opt]['DEFAULT'];
322         $option = array("NAME" => $opt, "VALUE" => $val , "NEGATE" => FALSE);
323         $this->sudoOption[$opt][] = $option;
324       }
325     }
327     foreach($this->sudoOption as $name => $opts){
328       foreach($opts as $id => $opt){
330         /****
331           Get posted value for BOOLEAN
332          ****/
333         if(in_array($this->options[$name]['TYPE'],array("BOOLEAN"))){
334           if(isset($_POST['option_value__'.$name.'_'.$id])){
335             $this->sudoOption[$name][$id]["VALUE"] = get_post('option_value__'.$name.'_'.$id);
336           }
337         }
339         /****
340           Get posted value for STRING / INTEGER
341          ****/
342         if(in_array($this->options[$name]['TYPE'],array("STRING","INTEGER"))){
343           if(isset($_POST['option_value__'.$name.'_'.$id])){
344             $this->sudoOption[$name][$id]["VALUE"] = get_post('option_value__'.$name.'_'.$id);
345           }
346         }
348         /****
349           Get posted value for STRING_BOOL / BOOL_INTEGER
350          ****/
351         if(in_array($this->options[$name]['TYPE'],array("BOOL_INTEGER","STRING_BOOL"))){
352           if(isset($_POST['option_selection__'.$name.'_'.$id])){
353             $sel = get_post('option_selection__'.$name.'_'.$id);
354             $val = "";
355             if(isset($_POST['option_value__'.$name.'_'.$id])){
356               $val = get_post('option_value__'.$name.'_'.$id);
357             }
359             if($sel == "FALSE" || $sel == "TRUE"){
360               $this->sudoOption[$name][$id]['VALUE'] = $sel;
361               $this->sudoOption[$name][$id]['NEGATE'] = FALSE;
362             }else{
363               $this->sudoOption[$name][$id]['VALUE'] = $val;
364             }
365           }
366         }
368         /****
369           Get posted value for LISTS
370          ****/
371         if(in_array($this->options[$name]['TYPE'],array("LISTS"))){
372           foreach($this->sudoOption[$name] as $entry_key => $entry){
373             if(isset($_POST['list_value__'.$name.'_'.$entry_key])){
374               $val = get_post('list_value__'.$name.'_'.$entry_key);
375               $this->sudoOption[$name][$entry_key]["VALUE"] = $val;
376             }
377           } 
378         }
379       }
380     }
381   }
384   /*! \brief  Save changes to ldap 
385    */
386   function save()
387   {
388     plugin::save(); 
390     $this->attrs['sudoOption'] = array();
391     foreach($this->sudoOption as $name => $opts){
392       foreach($opts as $id => $opt){
394         $type   = $this->options[$name]['TYPE'];
395         $neg    = $opt['NEGATE'];
396         $value  = $opt['VALUE'];
397         $option = "";
399         /* Escape special chars */
400         $value = $this->escape_command($value);
402         /****
403           Save LISTS 
404          ****/
405         if($type=="LISTS"){
406           if($value == ""){
407             $option = $name;
408           }else{
409             $option = $name."=".$value;
410           }
411           if($neg){
412             $option = "!".$option;
413           }
414         }
416         /****
417           Save BOOLEAN
418          ****/
419         if(in_array($type,array("BOOLEAN"))){ 
420           $option = $name;
421           if($value == "FALSE"){
422             $option = "!".$option;
423           }
424         }
426         /****
427           Save STRING / INTEGER
428          ****/
429         if(in_array($type,array("STRING","INTEGER"))){ 
430           if($value != ""){
431             $option = $name."=".$value;
432           }else{
433             $option = $name; 
434           }
435           if($neg){
436             $option = "!".$option;
437           }
438         }
440         /****
441           Save STRING_BOOL / BOOL_INTEGER
442          ****/
443         if(in_array($type,array("STRING_BOOL","BOOL_INTEGER"))){
444           if($value == "FALSE"){
445             $option = "!".$name;
446           }elseif($value == "TRUE"){
447             $option = $name;
448           }else{
449             if($value != ""){
450               $option = $name."=".$value;
451             }else{
452               $option = $name; 
453             }
454             if($neg){
455               $option = "!".$option;
456             }
457           }
458         }
460         $this->attrs['sudoOption'][] = $option;
461       }
462     }
463     $this->cleanup();
464     $ldap = $this->config->get_ldap_link();
465     $ldap->cd($this->dn);
466     $ldap->modify($this->attrs);;
467   }
470   /*! \brief  Checks input validity
471    */
472   function check()
473   {
474     $message = plugin::check();
476     foreach($this->sudoOption as $name => $options){
477       foreach($options as $id => $option){
478         switch($this->options[$name]['TYPE']){
480           /* Check for a valid integer value */
481           case 'INTEGER' : 
482             {
483               if(!preg_match("/^[0-9]*$/",$option['VALUE'])){
484                 $message[] = msgPool::invalid($name,$option['VALUE'],"/[0-9]/");
485               }
486             } break;
487         }
488       }
489     }
490     return ($message);
491   }
494   /*! \brief  This function will be called if an object gets copied.
495               This function adapts attributes from the source object.
496       @param  Array The source object.
497    */
498   function PrepareForCopyPaste($source)
499   {
500     plugin::PrepareForCopyPaste($source);
501     if(isset($source['sudoOption'])){
502       $this->attrs['sudoOption'] = $source['sudoOption'];
503       $this->load_options();
504     }
505   }
508   /*!  \brief   Escape special chars in function parameters.
509        @param   String the string to that must be escaped.
510    */
511   private function escape_command($str)
512   {
513     /* Check if given value is a command (/[a-z]/ ..)
514      */
515     if(preg_match("/^\//",$str)){
516       $cmd = preg_replace("/^([^ ]*).*$/","\\1",$str);
517       $val = preg_replace("/^[^ ]*(.*)$/","\\1",$str);
518       $str = $cmd.addcslashes($val,":.,\\");
519     }
520     return($str);
521   }
524   /*!  \brief   Unescape special chars in function parameters.
525        @param   String the string to that must be unescaped.
526    */
527   private function unescape_command($str)
528   {
529     /* Check if given value is a command (/[a-z]/ ..)
530      */
531     if(preg_match("/^\//",$str)){
532       $cmd = preg_replace("/^([^ ]*).*$/","\\1",$str);
533       $val = preg_replace("/^[^ ]*(.*)$/","\\1",$str);
534       $val = preg_replace(array("/\\\\\\\\/","/\\\\,/","/\\\\:/","/\\\\=/"),
535                               array("\\",",",":","="),$val);
536       $str = $cmd.$val;
537     }
538     return($str);
539   }
541 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
542 ?>