Handle hostnames with upper-case letters
[webmin.git] / spam / save_db.cgi
1 #!/usr/local/bin/perl
2 # Save LDAP and SQL database options
3
4 require './spam-lib.pl';
5 &error_setup($text{'db_err'});
6 &ReadParse();
7 &set_config_file_in(\%in);
8 &can_use_check("db");
9 &execute_before("db");
10 &lock_spam_files();
11 $conf = &get_config();
12
13 # Parse backend DSN
14 if ($in{'mode'} == 0) {
15         # Files only
16         $dsn = undef;
17         }
18 elsif ($in{'mode'} == 1) {
19         # Database of some type
20         &to_ipaddress($in{'dbhost'}) || &error($text{'db_edbhost'});
21         $in{'dbdb'} =~ /^[a-z0-9\.\-\_]+$/ || &error($text{'db_edbdb'});
22         $in{'dbport_def'} || $in{'dbport'} =~ /^\d+$/ ||
23                 &error($text{'db_edbport'});
24         $dsn = join(":", "DBI", $in{'dbdriver'}, $in{'dbdb'}, $in{'dbhost'});
25         $dsn .= ":".$in{'dbport'} if (!$in{'dbport_def'});
26         }
27 elsif ($in{'mode'} == 3) {
28         # LDAP
29         &to_ipaddress($in{'ldaphost'}) || &to_ip6address($in{'ldaphost'}) ||
30                 &error($text{'db_eldaphost'});
31         $in{'ldapport_def'} || $in{'ldapport'} =~ /^\d+$/ ||
32                 &error($text{'db_eldapport'});
33         $in{'ldapdn'} =~ /^\S+$/ || &error($text{'db_eldapdn'});
34         $in{'ldapattr'} =~ /^\S+$/ || &error($text{'db_eldapattr'});
35         $in{'ldapuid'} =~ /^\S+$/ || &error($text{'db_eldapuid'});
36         $dsn = "ldap://".$in{'ldaphost'}.
37                ($in{'ldapport_def'} ? "" : ":".$in{'ldapport'})."/".
38                $in{'ldapdn'}."?".$in{'ldapattr'}."?".$in{'ldapscope'}."?".
39                $in{'ldapuid'}."=__USERNAME__";
40         }
41 else {
42         # Other DSN
43         $in{'dsn'} =~ /\S/ || &error($text{'db_edsn'});
44         $dsn = $in{'dsn'};
45         }
46 &save_directives($conf, "user_scores_dsn", [ $dsn ], 1);
47
48 # Parse username and password
49 &parse_opt($conf, "user_scores_sql_username", \&username_check);
50 &parse_opt($conf, "user_scores_sql_password");
51 &parse_opt($conf, "user_scores_ldap_username", \&username_check);
52 &parse_opt($conf, "user_scores_ldap_password");
53
54 &flush_file_lines();
55 &unlock_spam_files();
56 &execute_after("db");
57 &webmin_log("db");
58 &redirect($redirect_url);
59
60 sub username_check
61 {
62 return $_[0] =~ /^\S+$/ ? undef : $text{'db_eusername'};
63 }
64