Handle hostnames with upper-case letters
[webmin.git] / syslog-ng / save_destination.cgi
1 #!/usr/local/bin/perl
2 # Create, update or delete a destination
3
4 require './syslog-ng-lib.pl';
5 &ReadParse();
6 &error_setup($text{'destination_err'});
7
8 # Get the old destination
9 $conf = &get_config();
10 if (!$in{'new'}) {
11         @dests = &find("destination", $conf);
12         ($dest) = grep { $_->{'value'} eq $in{'old'} } @dests;
13         $dest || &error($text{'destination_egone'});
14         $old = $dest;
15         }
16 else {
17         $dest = { 'name' => 'destination',
18                   'type' => 1,
19                   'members' => [ ] };
20         }
21
22 &lock_file($config{'syslogng_conf'});
23 if ($in{'delete'}) {
24         # Just delete it!
25         &check_dependencies('destination', $in{'old'}) &&
26             &error(&text('ddelete_eused', $in{'old'}));
27         &save_directive($conf, undef, $dest, undef, 0);
28         }
29 else {
30         # Validate inputs, and update object
31         $in{'name'} =~ /^[a-z0-9_]+$/i || &error($text{'destination_ename'});
32         if ($in{'new'} || $in{'old'} ne $in{'name'}) {
33                 ($clash) = grep { $_->{'value'} eq $in{'name'} } @dests;
34                 $clash && &error($text{'destination_eclash'});
35                 }
36         $dest->{'values'} = [ $in{'name'} ];
37
38         # Clear out all existing values
39         $dest->{'members'} = [ ];
40
41         # Save type-specific values
42         if ($in{'type'} == 0) {
43                 # Writing to a file
44                 $in{'file_name'} =~ /^\/\S/ ||
45                         &error($text{'destination_efile_name'});
46                 $file = { 'name' => 'file',
47                           'type' => 0,
48                           'values' => [ $in{'file_name'} ] };
49                 &save_directive($conf, $dest, undef, $file, 1);
50
51                 # Save owner
52                 if (!$in{'file_owner_def'}) {
53                         defined(getpwnam($in{'file_owner'})) ||
54                                 &error($text{'destination_eowner'});
55                         &save_directive($conf, $file, "owner",
56                                         $in{'file_owner'}, 1);
57                         }
58                 if (!$in{'file_group_def'}) {
59                         defined(getgrnam($in{'file_group'})) ||
60                                 &error($text{'destination_egroup'});
61                         &save_directive($conf, $file, "group",
62                                         $in{'file_group'}, 1);
63                         }
64
65                 # Save permissions
66                 if (!$in{'file_perm_def'}) {
67                         $in{'file_perm'} =~ /^[0-7]+$/ ||
68                                 &error($text{'destination_eperm'});
69                         &save_directive($conf, $file, "perm",
70                                         $in{'file_perm'}, 1);
71                         }
72
73                 # Save create dirs option
74                 if ($in{'file_create_dirs'}) {
75                         &save_directive($conf, $file, "create_dirs",
76                                         $in{'file_create_dirs'}, 1);
77                         }
78                 if (!$in{'file_dir_perm_def'}) {
79                         $in{'file_dir_perm'} =~ /^[0-7]+$/ ||
80                                 &error($text{'destination_edir_perm'});
81                         &save_directive($conf, $file, "dir_perm",
82                                         $in{'file_dir_perm'}, 1);
83                         }
84
85                 # Save sync options
86                 if ($in{'file_fsync'}) {
87                         &save_directive($conf, $file, "fsync",
88                                         $in{'file_fsync'}, 1);
89                         }
90                 if (!$in{'file_sync_freq_def'}) {
91                         $in{'file_sync_freq'} =~ /^\d+$/ ||
92                                 &error($text{'destination_esync_freq'});
93                         &save_directive($conf, $file, "sync_freq",
94                                         $in{'file_sync_freq'}, 1);
95                         }
96                 }
97
98         elsif ($in{'type'} == 1) {
99                 # Sending to users
100                 $in{'usertty_user_def'} || $in{'usertty_user'} ||
101                         &error($text{'destination_euser'});
102                 $usertty = { 'name' => 'usertty',
103                           'type' => 0,
104                           'values' => [ $in{'usertty_user_def'} ? '*' :
105                                           $in{'usertty_user'} ] };
106                 &save_directive($conf, $dest, undef, $usertty, 1);
107                 }
108
109         elsif ($in{'type'} == 2) {
110                 # Feeding to a program
111                 $in{'program_prog'} =~ /^\S/ ||
112                         &error($text{'destination_eprog'});
113                 $program = { 'name' => 'program',
114                              'type' => 0,
115                              'values' => [ $in{'program_prog'} ] };
116                 &save_directive($conf, $dest, undef, $program, 1);
117                 }
118
119         elsif ($in{'type'} == 3) {
120                 # Writing to a pipe file
121                 $in{'pipe_name'} =~ /^\S/ ||
122                         &error($text{'destination_epipe'});
123                 $pipe = { 'name' => 'pipe',
124                              'type' => 0,
125                              'values' => [ $in{'pipe_name'} ] };
126                 &save_directive($conf, $dest, undef, $pipe, 1);
127                 }
128
129         elsif ($in{'type'} == 4) {
130                 # Writing to a TCP or UDP socket
131                 $net = { 'name' => $in{'net_proto'},
132                          'type' => 0,
133                          'values' => [ $in{'net_host'} ] };
134                 &to_ipaddress($in{'net_host'}) ||
135                     &to_ip6address($in{'net_host'}) ||
136                         &error($text{'destination_enet_host'});
137                 &save_directive($conf, $dest, undef, $net, 1);
138
139                 # Save other network dest options
140                 if (!$in{'net_port_def'}) {
141                         $in{'net_port'} =~ /^\d+$/ ||
142                                 &error($text{'destination_enet_port'});
143                         &save_directive($conf, $net, "port",
144                                         $in{'net_port'}, 1);
145                         }
146                 if (!$in{'net_localip_def'}) {
147                         &check_ipaddress($in{'net_localip'}) ||
148                                 &error($text{'destination_enet_localip'});
149                         &save_directive($conf, $net, "localip",
150                                         $in{'net_localip'}, 1);
151                         }
152                 if (!$in{'net_localport_def'}) {
153                         $in{'net_localport'} =~ /^\d+$/ ||
154                                 &error($text{'destination_enet_localport'});
155                         &save_directive($conf, $net, "localport",
156                                         $in{'net_localport'}, 1);
157                         }
158                 }
159
160         elsif ($in{'type'} == 6) {
161                 $unix = { 'name' => $in{'unix_type'},
162                          'type' => 0,
163                          'values' => [ $in{'unix_name'} ] };
164                 $in{'unix_name'} || &error($text{'destination_eunix'});
165                 &save_directive($conf, $dest, undef, $unix, 1);
166                 }
167
168         # Actually update the object
169         &save_directive($conf, undef, $old, $dest, 0);
170
171         # Update dependent log targets
172         if (!$in{'new'}) {
173                   &rename_dependencies('destination', $in{'old'}, $in{'name'});
174                   }
175         }
176
177 &unlock_file($config{'syslogng_conf'});
178 &webmin_log($in{'delete'} ? 'delete' : $in{'new'} ? 'create' : 'modify',
179             'destination', $in{'old'} || $in{'name'});
180 &redirect("list_destinations.cgi");
181