Handle hostnames with upper-case letters
[webmin.git] / ldap-client / save_server.cgi
1 #!/usr/local/bin/perl
2 # Save the LDAP server to connect to
3
4 require './ldap-client-lib.pl';
5 &error_setup($text{'server_err'});
6 &ReadParse();
7
8 &lock_file($config{'auth_ldap'});
9 @secrets = split(/\t+/, $config{'secret'});
10 foreach $secret (@secrets) {
11         &lock_file($secret);
12         }
13 $conf = &get_config();
14 $uri = &find_svalue("uri", $conf);
15
16 # Validate and save inputs
17 if ($uri) {
18         # Save uri directive
19         for($i=0; defined($host = $in{'uhost_'.$i}); $i++) {
20                 next if (!$host);
21                 $port = $in{'uport_'.$i.'_def'} ? undef : $in{'uport_'.$i};
22                 $proto = $in{'uproto_'.$i};
23                 !defined($port) ||
24                     $port =~ /^\d+$/ && $port > 0 && $port < 65536 ||
25                     &error(&text('server_euport', $host));
26                 $uri = $proto."://".$host.($port ? ":$port" : "");
27                 $uri .= "/" if ($proto eq "ldap" || $proto eq "ldaps");
28                 push(@uris, $uri);
29                 }
30         @uris || &error($text{'server_euri'});
31         &save_directive($conf, "uri", join(" ", @uris));
32         }
33 else {
34         # Set host and port directives
35         @hosts = split(/\s+/, $in{'host'});
36         foreach $h (@hosts) {
37                 &to_ipaddress($h) || &to_ip6address($h) ||
38                         &error(&text('server_ehost', $h));
39                 }
40         @hosts || &error($text{'server_ehosts'});
41         &save_directive($conf, "host", join(" ", @hosts));
42
43         # Save server port
44         if ($in{'port_def'}) {
45                 &save_directive($conf, "port", undef);
46                 }
47         else {
48                 $in{'port'} =~ /^\d+$/ &&
49                     $in{'port'} > 0 && $in{'port'} < 65536 ||
50                         &error($text{'server_eport'});
51                 &save_directive($conf, "port", $in{'port'});
52                 }
53         }
54
55 # Save LDAP protocol version
56 &save_directive($conf, "ldap_version", $in{'version'} || undef);
57
58 # Save time limit
59 if ($in{'timelimit_def'}) {
60         &save_directive($conf, "bind_timelimit", undef);
61         }
62 else {
63         $in{'timelimit'} =~ /^\d+$/ || &error($text{'server_etimelimit'});
64         &save_directive($conf, "bind_timelimit", $in{'timelimit'});
65         }
66
67 # Save non-root login
68 if ($in{'binddn_def'}) {
69         &save_directive($conf, "binddn", undef);
70         }
71 else {
72         $in{'binddn'} =~ /\S/ || &error($text{'server_ebinddn'});
73         &save_directive($conf, "binddn", $in{'binddn'});
74         }
75
76 # Save non-root password
77 if ($in{'bindpw_def'}) {
78         &save_directive($conf, "bindpw", undef);
79         }
80 else {
81         $in{'bindpw'} =~ /\S/ || &error($text{'server_ebindpw'});
82         &save_directive($conf, "bindpw", $in{'bindpw'});
83         }
84
85 # Save root login
86 if ($in{'rootbinddn_def'}) {
87         &save_directive($conf, "rootbinddn", undef);
88         }
89 else {
90         $in{'rootbinddn'} =~ /\S/ || &error($text{'server_erootbinddn'});
91         &save_directive($conf, "rootbinddn", $in{'rootbinddn'});
92         }
93
94 # Save root password
95 if ($in{'rootbindpw_def'}) {
96         &save_rootbinddn_secret(undef);
97         }
98 else {
99         $in{'rootbindpw'} =~ /\S/ || &error($text{'server_erootbindpw'});
100         &save_rootbinddn_secret($in{'rootbindpw'});
101         }
102
103 # SSL mode
104 &save_directive($conf, "ssl", $in{'ssl'} || undef);
105
106 # Check server SSL cert
107 &save_directive($conf, "tls_checkpeer", $in{'peer'} || undef);
108
109 # CA cert file for server
110 if ($in{'cacert_def'}) {
111         &save_directive($conf, "tls_cacertfile", undef);
112         }
113 else {
114         $in{'cacert'} =~ /^\// && -r $in{'cacert'} && !-d $in{'cacert'} ||
115                 &error($text{'server_ecacert'});
116         &save_directive($conf, "tls_cacertfile", $in{'cacert'});
117         }
118
119 # Write out config
120 &flush_file_lines();
121 &unlock_file($config{'auth_ldap'});
122 foreach $secret (@secrets) {
123         &unlock_file($secret);
124         }
125
126 &webmin_log("server");
127 &redirect("");
128