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 /****
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 /* Special chars are escaped, remove escape char now.
135 \\ => \
136 \: => :
137 \, => ,
138 \= => =
139 */
140 $value = preg_replace(array("/\\\\\\\\/","/\\\\,/","/\\\\:/","/\\\\=/"),
141 array("\\",",",":","="),$value);
143 /* Check if the given value is part of our options list.
144 If it is not, add it as type STRING and display a warning.
145 */
146 if(!isset($this->options[$opt])){
147 $this->options[$opt]=array('NAME'=>$opt,'TYPE'=>'STRING','DEFAULT' => '');
148 msg_dialog::display(_("Unknown option"),
149 sprintf(_("The sudo option '%s' is invalid!"),
150 $opt),WARNING_DIALOG);
151 }
153 /* Create internal sudoOption object */
154 $option = array();
155 $option['NAME'] = $opt;
156 $option['VALUE'] = $value;
157 $option['NEGATE'] = $negation;
159 /* Special handling for mixed flag types.
160 Some attributes like (BOOL_INTEGER) can be TRUE/FALSE and INTEGER.
161 This means, if the value is empty it is BOOL and $negation defines its boolean value.
162 */
163 if(in_array($this->options[$opt]['TYPE'],array("BOOL_INTEGER","STRING_BOOL"))){
164 if(empty($value)){
165 $option['NEGATE'] = FALSE;
166 if($negation){
167 $option['VALUE'] = "FALSE";
168 }else{
169 $option['VALUE'] = "TRUE";
170 }
171 }
172 }
174 /* Special handling for BOOLEAN values */
175 if(in_array($this->options[$opt]['TYPE'],array("BOOLEAN"))){
176 $option['NEGATE'] = FALSE;
177 if($negation){
178 $option['VALUE'] = "FALSE";
179 }else{
180 $option['VALUE'] = "TRUE";
181 }
182 }
184 /* Append values */
185 $this->sudoOption[$opt][] = $option;
186 }
187 }
188 }
191 /*! \brief Create HTML output for this plugin
192 @return String HTML output for this plugin.
193 */
194 function execute()
195 {
196 /* Call parent execute */
197 plugin::execute();
199 /*****
200 Handle Posts
201 *****/
202 foreach($_POST as $name => $value){
204 if(preg_match("/^negOption_/",$name)){
206 $opt = preg_replace("/^negOption_/","",$name);
207 $opt = preg_replace("/_[^_]*$/","",$opt);
208 $id = preg_replace("/^.*_([0-9])*$/","\\1",$opt);
209 $opt = preg_replace("/_[0-9]*$/","",$opt);
211 if(isset($this->sudoOption[$opt][$id])){
212 $val = $this->sudoOption[$opt][$id]["VALUE"];
214 /*****
215 Negate STRING_BOOL && BOOL_INTEGER
216 *****/
217 if(in_array($this->options[$opt]['TYPE'],array('STRING_BOOL','BOOL_INTEGER'))){
218 if(in_array($val, array("TRUE","FALSE"))){
219 if($val == "TRUE"){
220 $this->sudoOption[$opt][$id]["VALUE"] = "FALSE";
221 }else{
222 $this->sudoOption[$opt][$id]["VALUE"] = "TRUE";
223 }
224 }else{
225 $this->sudoOption[$opt][$id]['NEGATE'] = !$this->sudoOption[$opt][$id]['NEGATE'];
226 }
227 }
229 /*****
230 Negate STRING / INTEGER
231 *****/
232 if(in_array($this->options[$opt]['TYPE'],array('STRING','INTEGER','LISTS'))){
233 $this->sudoOption[$opt][$id]['NEGATE'] = !$this->sudoOption[$opt][$id]['NEGATE'];
234 }
236 /*****
237 Negate BOOLEAN
238 *****/
239 if(in_array($this->options[$opt]['TYPE'],array('BOOLEAN'))){
240 if($val == "TRUE"){
241 $this->sudoOption[$opt][$id]["VALUE"] = "FALSE";
242 }else{
243 $this->sudoOption[$opt][$id]["VALUE"] = "TRUE";
244 }
245 }
246 }
247 break;
248 }
250 /*****
251 Remove options
252 *****/
253 if(preg_match("/^delOption/",$name)){
254 $opt = preg_replace("/^delOption_/","",$name);
255 $opt = preg_replace("/_[^_]*$/","",$opt);
256 $id = preg_replace("/^.*_([0-9])*$/","\\1",$opt);
257 $opt = preg_replace("/_[0-9]*$/","",$opt);
259 if(isset($this->sudoOption[$opt][$id])){
260 unset($this->sudoOption[$opt][$id]);
261 }
262 if(!count($this->sudoOption[$opt])){
263 unset($this->sudoOption[$opt]);
264 }
265 break;
266 }
267 }
270 $smarty = get_smarty();
271 $smarty->assign("map", array("STRING" => _("string"), "BOOLEAN" => _("bool"),
272 "INTEGER" => _("integer") , "BOOL_INTEGER" => _("integer")."-"._("bool") ,
273 "STRING_BOOL" => _("string")."-"._("bool"),"LISTS" => _("list")));
274 $smarty->assign("sudoOption",$this->prepare_for_html($this->sudoOption));
275 $smarty->assign("options",$this->options);
276 return($smarty->fetch(get_template_path('options.tpl', TRUE)));
277 }
280 /*! \brief Prepare options array to be used in HTML.
281 @param Array The options array ($this->sudoOption)
282 @return Array HTML ready sudoOption. Can now be used in smarty templates
283 */
284 function prepare_for_html($a_options)
285 {
286 foreach($a_options as $name => $options){
287 foreach($options as $key => $option){
288 $a_options[$name][$key]['VALUE'] = htmlentities($option['VALUE']);
289 }
290 }
291 return($a_options);
292 }
295 /*! \brief Removes this plugin
296 */
297 function remove_from_parent()
298 {
299 }
302 /*! \brief Saves all relevant HTML post values for this plugin
303 */
304 function save_object()
305 {
306 plugin::save_object();
308 if(isset($_POST['add_option']) && isset($_POST['option'])){
309 $opt = get_post("option");
311 /* Append attribute only once, lists are handled below */
312 if(isset($this->options[$opt])){
313 $type = $this->options[$opt]['TYPE'];
314 $val = $this->options[$opt]['DEFAULT'];
315 $option = array("NAME" => $opt, "VALUE" => $val , "NEGATE" => FALSE);
316 $this->sudoOption[$opt][] = $option;
317 }
318 }
320 foreach($this->sudoOption as $name => $opts){
321 foreach($opts as $id => $opt){
323 /****
324 Get posted value for BOOLEAN
325 ****/
326 if(in_array($this->options[$name]['TYPE'],array("BOOLEAN"))){
327 if(isset($_POST['option_value__'.$name.'_'.$id])){
328 $this->sudoOption[$name][$id]["VALUE"] = get_post('option_value__'.$name.'_'.$id);
329 }
330 }
332 /****
333 Get posted value for STRING / INTEGER
334 ****/
335 if(in_array($this->options[$name]['TYPE'],array("STRING","INTEGER"))){
336 if(isset($_POST['option_value__'.$name.'_'.$id])){
337 $this->sudoOption[$name][$id]["VALUE"] = get_post('option_value__'.$name.'_'.$id);
338 }
339 }
341 /****
342 Get posted value for STRING_BOOL / BOOL_INTEGER
343 ****/
344 if(in_array($this->options[$name]['TYPE'],array("BOOL_INTEGER","STRING_BOOL"))){
345 if(isset($_POST['option_selection__'.$name.'_'.$id])){
346 $sel = get_post('option_selection__'.$name.'_'.$id);
347 $val = "";
348 if(isset($_POST['option_value__'.$name.'_'.$id])){
349 $val = get_post('option_value__'.$name.'_'.$id);
350 }
352 if($sel == "FALSE" || $sel == "TRUE"){
353 $this->sudoOption[$name][$id]['VALUE'] = $sel;
354 $this->sudoOption[$name][$id]['NEGATE'] = FALSE;
355 }else{
356 $this->sudoOption[$name][$id]['VALUE'] = $val;
357 }
358 }
359 }
361 /****
362 Get posted value for LISTS
363 ****/
364 if(in_array($this->options[$name]['TYPE'],array("LISTS"))){
365 foreach($this->sudoOption[$name] as $entry_key => $entry){
366 if(isset($_POST['list_value__'.$name.'_'.$entry_key])){
367 $val = get_post('list_value__'.$name.'_'.$entry_key);
368 $this->sudoOption[$name][$entry_key]["VALUE"] = $val;
369 }
370 }
371 }
372 }
373 }
374 }
377 /*! \brief Save changes to ldap
378 */
379 function save()
380 {
381 plugin::save();
383 $this->attrs['sudoOption'] = array();
384 foreach($this->sudoOption as $name => $opts){
385 foreach($opts as $id => $opt){
387 $type = $this->options[$name]['TYPE'];
388 $neg = $opt['NEGATE'];
389 $value = $opt['VALUE'];
390 $option = "";
392 /* Escape special chars */
393 $value = addcslashes($value,"\\:,:=");
395 /****
396 Save LISTS
397 ****/
398 if($type=="LISTS"){
399 if($value == ""){
400 $option = $name;
401 }else{
402 $option = $name."=".$value;
403 }
404 if($neg){
405 $option = "!".$option;
406 }
407 }
409 /****
410 Save BOOLEAN
411 ****/
412 if(in_array($type,array("BOOLEAN"))){
413 $option = $name;
414 if($value == "FALSE"){
415 $option = "!".$option;
416 }
417 }
419 /****
420 Save STRING / INTEGER
421 ****/
422 if(in_array($type,array("STRING","INTEGER"))){
423 if($value != ""){
424 $option = $name."=".$value;
425 }else{
426 $option = $name;
427 }
428 if($neg){
429 $option = "!".$option;
430 }
431 }
433 /****
434 Save STRING_BOOL / BOOL_INTEGER
435 ****/
436 if(in_array($type,array("STRING_BOOL","BOOL_INTEGER"))){
437 if($value == "FALSE"){
438 $option = "!".$name;
439 }elseif($value == "TRUE"){
440 $option = $name;
441 }else{
442 if($value != ""){
443 $option = $name."=".$value;
444 }else{
445 $option = $name;
446 }
447 if($neg){
448 $option = "!".$option;
449 }
450 }
451 }
453 $this->attrs['sudoOption'][] = $option;
454 }
455 }
456 $this->cleanup();
457 $ldap = $this->config->get_ldap_link();
458 $ldap->cd($this->dn);
459 $ldap->modify($this->attrs);;
460 }
463 /*! \brief Checks input validity
464 */
465 function check()
466 {
467 $message = plugin::check();
469 foreach($this->sudoOption as $name => $options){
470 foreach($options as $id => $option){
471 switch($this->options[$name]['TYPE']){
473 /* Check for a valid integer value */
474 case 'INTEGER' :
475 {
476 if(!preg_match("/^[0-9]*$/",$option['VALUE'])){
477 $message[] = msgPool::invalid($name,$option['VALUE'],"/[0-9]/");
478 }
479 } break;
481 }
482 }
483 }
485 return ($message);
486 }
487 }
488 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
489 ?>