Handle hostnames with upper-case letters
[webmin.git] / sendmail / save_ports.cgi
1 #!/usr/local/bin/perl
2 # Update the list of TCP ports Sendmail uses
3
4 require './sendmail-lib.pl';
5 require './features-lib.pl';
6
7 &ReadParse();
8 &error_setup($text{'ports_err'});
9 $access{'ports'} || &error($text{'ports_ecannot'});
10
11 # Parse and validate inputs
12 @ports = ( );
13 if (!$in{'ports_def'}) {
14         for($i=0; defined($name=$in{"name_$i"}); $i++) {
15                 # Port name
16                 next if (!$name);
17                 $name =~ /^[a-z0-9\_]+$/i || &error(&text('ports_ename', $i+1));
18                 $done{$name}++ && &error(&text('ports_eclash', $i+1));
19                 @opts = ( "Name=$name" );
20
21                 # IP address
22                 if (!$in{"addr_${i}_def"}) {
23                         &check_ipaddress($in{"addr_$i"}) ||
24                            &check_ip6address($in{"addr_$i"}) ||
25                                 &error(&text('ports_eaddr', $i+1));
26                         push(@opts, "Address=".$in{"addr_$i"});
27                         }
28
29                 # Family
30                 if ($in{"family_${i}"}) {
31                         push(@opts, "Family=".$in{"family_${i}"});
32                         }
33
34                 # TCP port
35                 if (!$in{"port_${i}_def"}) {
36                         $in{"port_$i"} =~ /^\d+$/ && $in{"port_$i"} > 0 &&
37                             $in{"port_$i"} < 65536 ||
38                             getservbyname($in{"port_$i"}, "tcp") ||
39                                 &error(&text('ports_eport', $i+1));
40                         push(@opts, "Port=".$in{"port_$i"});
41                         }
42
43                 # Modifiers
44                 @mods = split(/\0/, $in{"mod_$i"});
45                 if (@mods) {
46                         push(@opts, "Modifiers=".join("", @mods));
47                         }
48
49                 # Other options
50                 push(@opts, split(/,/, $in{"other_$i"}));
51                 push(@ports, join(",", @opts));
52                 }
53         }
54
55 # Update sendmail.cf
56 &lock_file($config{'sendmail_cf'});
57 $conf = &get_sendmailcf();
58 @oldlist = map { $_->[0] } &find_options("DaemonPortOptions", $conf);
59 @newlist = map { { 'type' => 'O',
60                    'values' => [ " DaemonPortOptions=$_" ] } } @ports;
61 &save_directives($conf, \@oldlist, \@newlist);
62 &flush_file_lines($config{'sendmail_cf'});
63 &unlock_file($config{'sendmail_cf'});
64
65 # Update .mc file too, if we have one
66 if ($features_access) {
67         @features = &list_features();
68         if (@features) {
69                 &lock_file($config{'sendmail_mc'});
70                 @dpa = grep { $_->{'type'} == 0 &&
71                         $_->{'text'} =~ /^DAEMON_OPTIONS/ } @features;
72                 for($i=0; $i<@dpa || $i<@ports; $i++) {
73                         if ($dpa[$i] && $ports[$i]) {
74                                 # Modify
75                                 $dpa[$i]->{'text'} =
76                                         "DAEMON_OPTIONS(`$ports[$i]')";
77                                 &modify_feature($dpa[$i]);
78                                 }
79                         elsif ($dpa[$i] && !$ports[$i]) {
80                                 # No longer needed .. delete
81                                 &delete_feature($dpa[$i]);
82                                 }
83                         elsif (!$dpa[$i] && $ports[$i]) {
84                                 # Add new feature
85                                 $f = { 'type' => 0,
86                                    'text' => "DAEMON_OPTIONS(`$ports[$i]')" };
87                                 &create_feature($f);
88                                 }
89                         }
90                 &unlock_file($config{'sendmail_mc'});
91                 }
92         }
93
94 # Restart Sendmail
95 &restart_sendmail();
96 &webmin_log("ports");
97 &redirect("");
98