Handle hostnames with upper-case letters
[webmin.git] / vgetty / save_options.cgi
1 #!/usr/local/bin/perl
2 # save_options.cgi
3 # Save voicemail server options
4
5 require './vgetty-lib.pl';
6 &ReadParse();
7 &error_setup($text{'options_err'});
8 @conf = &get_config();
9
10 # Validate inputs
11 $in{'rings'} =~ /^\d+$/ || &error($text{'options_erings'});
12 $in{'rings'} >= 2 || &error($text{'options_erings2'});
13 $ans = &parse_answer_mode("ans");
14 $in{'maxlen'} =~ /^\d+$/ || &error($text{'options_emaxlen'});
15 $in{'minlen'} =~ /^\d+$/ || &error($text{'options_eminlen'});
16 $in{'thresh'} =~ /^\d+$/ || &error($text{'options_ethresh'});
17 if (!$in{'rgain_def'}) {
18         $in{'rgain'} =~ /^\d+$/ || &error($text{'options_ergain'});
19         $in{'rgain'} >= 0 && $in{'rgain'} <= 100 ||
20                 &error($text{'options_ergain2'});
21         }
22 if (!$in{'tgain_def'}) {
23         $in{'tgain'} =~ /^\d+$/ || &error($text{'options_etgain'});
24         $in{'tgain'} >= 0 && $in{'tgain'} <= 100 ||
25                 &error($text{'options_etgain2'});
26         }
27 defined(getpwnam($in{'owner'})) || &error($text{'options_eowner'});
28 defined(getgrnam($in{'group'})) || &error($text{'options_egroup'});
29 $in{'mode'} =~ /^0[0-7]{3}$/ || &error($text{'options_emode'});
30 if ($in{'prog_mode'} == 1) {
31         $in{'email'} =~ /^\S+$/ || &error($text{'options_eemail'});
32         }
33 elsif ($in{'prog_mode'} == 2) {
34         $in{'prog'} =~ /^(\S+)/ && &has_command($1) ||
35                 &error($text{'options_eprog'});
36         }
37
38 # Write to config files
39 &lock_file($config{'vgetty_config'});
40 $rings = &find_value("rings", \@conf);
41 if ($in{'rings_port'}) {
42         local $tf = $rings =~ /^\// ? $rings : "/etc/rings";
43         &open_lock_tempfile(TF, ">$tf");
44         &print_tempfile(TF, $in{'rings'},"\n");
45         &close_tempfile(TF);
46         &save_directive(\@conf, "rings", $tf);
47         }
48 else {
49         if ($rings =~ /^\//) {
50                 &lock_file($rings);
51                 unlink($rings);
52                 &unlock_file($rings);
53                 }
54         &save_directive(\@conf, "rings", $in{'rings'});
55         }
56 $ans = &find_value("answer_mode", \@conf);
57 $mode = &parse_answer_mode("ans");
58 if ($in{'ans_port'}) {
59         local $tf = $ans =~ /^\// ? $ans : "/etc/answer";
60         &open_lock_tempfile(TF, ">$tf");
61         &print_tempfile(TF, "$mode\n");
62         &close_tempfile(TF);
63         &save_directive(\@conf, "answer_mode", $tf);
64         }
65 else {
66         if ($ans =~ /^\//) {
67                 &lock_file($ans);
68                 unlink($ans)
69                 &unlock_file($ans);
70                 }
71         &save_directive(\@conf, "answer_mode", $mode);
72         }
73
74 &save_directive(\@conf, "rec_max_len", $in{'maxlen'});
75 &save_directive(\@conf, "rec_min_len", $in{'minlen'});
76 &save_directive(\@conf, "rec_remove_silence", $in{'silence'} ? "true" :"false");
77 &save_directive(\@conf, "rec_silence_threshold", $in{'thresh'});
78 &save_directive(\@conf, "receive_gain", $in{'rgain_def'} ? -1 : $in{'rgain'});
79 &save_directive(\@conf, "transmit_gain", $in{'tgain_def'} ? -1 : $in{'tgain'});
80 &save_directive(\@conf, "rec_always_keep", $in{'keep'} ? "true" : "false");
81 &save_directive(\@conf, "do_message_light", $in{'light'} ? "true" : "false");
82 &save_directive(\@conf, "phone_owner", $in{'owner'});
83 &save_directive(\@conf, "phone_group", $in{'group'});
84 &save_directive(\@conf, "phone_mode", $in{'mode'});
85 if ($in{'prog_mode'} == 1) {
86         # Need to create the email wrapper script
87         local $script = "$module_config_directory/email.pl";
88         $perl_path = &get_perl_path();
89         &lock_file($script);
90         &open_tempfile(SCRIPT, ">$script");
91         &print_tempfile(SCRIPT, <<EOF
92 #!/bin/sh -- # -*- perl -*-
93 eval 'exec $perl_path -S \$0 \${1+"\$\@"}'
94         if \$running_under_some_shell;
95 open(CONF, "$config_directory/miniserv.conf");
96 while(<CONF>) {
97         \$root = \$1 if (/^root=(.*)/);
98         }
99 close(CONF);
100 \$ENV{'WEBMIN_CONFIG'} = "$ENV{'WEBMIN_CONFIG'}";
101 \$ENV{'WEBMIN_VAR'} = "$ENV{'WEBMIN_VAR'}";
102 chdir("\$root/$module_name");
103 exec("\$root/$module_name/email.pl", \$ARGV[0]);
104 EOF
105         );
106         &close_tempfile(CMD);
107         chmod(0755, $script);
108         &unlock_file($script);
109         &save_directive(\@conf, "message_program", $script);
110         $config{'email_to'} = $in{'email'};
111         &save_module_config();
112         }
113 elsif ($in{'prog_mode'} == 2) {
114         &save_directive(\@conf, "message_program", $in{'prog'});
115         }
116 else {
117         &save_directive(\@conf, "message_program", "");
118         }
119 &flush_file_lines();
120 &unlock_file($config{'vgetty_config'});
121 &webmin_log("options");
122 &redirect("");
123