Handle hostnames with upper-case letters
[webmin.git] / sshd / save_host.cgi
1 #!/usr/local/bin/perl
2 # save_host.cgi
3 # Create, update or delete a client host
4
5 require (-r 'sshd-lib.pl' ? './sshd-lib.pl' : './ssh-lib.pl');
6 &ReadParse();
7 &lock_file($config{'client_config'});
8 $hconf = &get_client_config();
9 &error_setup($text{'host_err'});
10
11 # Get version and type
12 if (&get_product_name() eq 'usermin') {
13         $version_type = &get_ssh_type();
14         $version_number = &get_ssh_version();
15         }
16 else {
17         $version_type = $version{'type'};
18         $version_number = $version{'number'};
19         }
20
21 if ($in{'delete'}) {
22         # Just delete the host
23         $host = $hconf->[$in{'idx'}];
24         &delete_host($host);
25         }
26 else {
27         # Saving or creating a host
28         $host = $hconf->[$in{'idx'}] if (!$in{'new'});
29         if ($in{'name_def'}) {
30                 $host->{'values'} = [ '*' ];
31                 }
32         else {
33                 $in{'name'} =~ /^\S+$/ || &error($text{'host_ename'});
34                 $host->{'values'} = [ $in{'name'} ];
35                 }
36         if ($in{'new'}) {
37                 # Create empty host structure
38                 &create_host($host);
39                 }
40         else {
41                 &modify_host($host);
42                 }
43         $conf = $host->{'members'};
44
45         # Validate and store host options
46         if ($in{'user_def'}) {
47                 &save_directive("User", $conf);
48                 }
49         else {
50                 $in{'user'} =~ /^\S+$/ || &error($text{'host_euser'});
51                 &save_directive("User", $conf, $in{'user'});
52                 }
53
54         &save_directive("KeepAlive", $conf,
55                 $in{'keep'} == 2 ? undef : $in{'keep'} ? 'yes' : 'no');
56
57         if ($in{'hostname_def'}) {
58                 &save_directive("HostName", $conf);
59                 }
60         else {
61                 &to_ipaddress($in{'hostname'}) ||
62                     &to_ip6address($in{'hostname'}) ||
63                         &error($text{'host_ehostname'});
64                 &save_directive("HostName", $conf, $in{'hostname'});
65                 }
66
67         &save_directive("BatchMode", $conf,
68                 $in{'batch'} == 2 ? undef : $in{'batch'} ? 'yes' : 'no');
69
70         if ($in{'port_def'}) {
71                 &save_directive("Port", $conf);
72                 }
73         else {
74                 $in{'port'} =~ /^\d+$/ || &error($text{'host_eport'});
75                 &save_directive("Port", $conf, $in{'port'});
76                 }
77
78         if ($version_type ne 'ssh' || $version_number < 3) {
79                 &save_directive("Compression", $conf,
80                         $in{'comp'} == 2 ? undef : $in{'comp'} ? 'yes' : 'no');
81                 }
82
83         if ($in{'escape_def'} == 1) {
84                 &save_directive("EscapeChar", $conf);
85                 }
86         elsif ($in{'escape_def'} == 2) {
87                 &save_directive("EscapeChar", $conf, "none");
88                 }
89         else {
90                 $in{'escape'} =~ /^\S$/ || $in{'escape'} =~ /^\^\S$/ ||
91                          &error($text{'host_eescape'});
92                 &save_directive("EscapeChar", $conf, $in{'escape'});
93                 }
94
95         
96         if ($version_type ne 'ssh' || $version_number < 3) {
97                 if ($in{'clevel_def'}) {
98                         &save_directive("CompressionLevel", $conf);
99                         }
100                 else {
101                         &save_directive("CompressionLevel", $conf,
102                                         $in{'clevel'});
103                         }
104
105                 if ($in{'attempts_def'}) {
106                         &save_directive("ConnectionAttempts", $conf);
107                         }
108                 else {
109                         $in{'attempts'} =~ /^\d+$/ ||
110                                 &error($text{'host_eattempts'});
111                         &save_directive("ConnectionAttempts", $conf,
112                                         $in{'attempts'});
113                         }
114
115                 &save_directive("UsePrivilegedPort", $conf,
116                         $in{'priv'} == 2 ? undef : $in{'priv'} ? 'yes' : 'no');
117
118                 &save_directive("FallBackToRsh", $conf,
119                         $in{'rsh'} == 2 ? undef : $in{'rsh'} ? 'yes' : 'no');
120
121                 &save_directive("UseRsh", $conf,
122                     $in{'usersh'} == 2 ? undef : $in{'usersh'} ? 'yes' : 'no');
123                 }
124
125         &save_directive("ForwardAgent", $conf,
126                 $in{'agent'} == 2 ? undef : $in{'agent'} ? 'yes' : 'no');
127
128         &save_directive("ForwardX11", $conf,
129                 $in{'x11'} == 2 ? undef : $in{'x11'} ? 'yes' : 'no');
130
131         &save_directive("StrictHostKeyChecking", $conf,
132                 $in{'strict'} == 2 ? undef : $in{'strict'} == 1 ? 'yes' :
133                 $in{'strict'} == 0 ? 'no' : 'ask');
134
135         if ($version_type eq 'openssh') {
136                 &save_directive("CheckHostIP", $conf,
137                   $in{'checkip'} == 2 ? undef : $in{'checkip'} ? 'yes' : 'no');
138
139                 &save_directive("Protocol", $conf, $in{'prots'} || undef);
140                 }
141
142         for($i=0; defined($in{"llport_$i"}); $i++) {
143                 next if (!$in{"llport_$i"} && !$in{"lrhost_$i"} &&
144                          !$in{"lrport_$i"});
145                 $in{"llport_$i"} =~ /^\d+$/ || &error($text{'host_elport'});
146                 $in{"lrhost_$i"} =~ /^\S+$/ || &error($text{'host_erhost'});
147                 $in{"lrport_$i"} =~ /^\d+$/ || &error($text{'host_erport'});
148                 push(@lforward, sprintf("%d %s:%d", $in{"llport_$i"},
149                                         $in{"lrhost_$i"}, $in{"lrport_$i"}));
150                 }
151         &save_directive("LocalForward", $conf, @lforward);
152
153         for($i=0; defined($in{"rrport_$i"}); $i++) {
154                 next if (!$in{"rrport_$i"} && !$in{"rlhost_$i"} &&
155                          !$in{"rlport_$i"});
156                 $in{"rrport_$i"} =~ /^\d+$/ || &error($text{'host_erport'});
157                 $in{"rlhost_$i"} =~ /^\S+$/ || &error($text{'host_elhost'});
158                 $in{"rlport_$i"} =~ /^\d+$/ || &error($text{'host_elport'});
159                 push(@rforward, sprintf("%d %s:%d", $in{"rrport_$i"},
160                                         $in{"rlhost_$i"}, $in{"rlport_$i"}));
161                 }
162         &save_directive("RemoteForward", $conf, @rforward);
163         }
164
165 &flush_file_lines();
166 &unlock_file($config{'client_config'});
167 if (&get_product_name() ne 'usermin') {
168         &webmin_log($in{'new'} ? "create" : $in{'delete'} ? "delete" : "update",
169                     "host", $host->{'values'}->[0]);
170         }
171 &redirect("list_hosts.cgi");
172