Handle hostnames with upper-case letters
[webmin.git] / syslog-ng / save_source.cgi
1 #!/usr/local/bin/perl
2 # Create, update or delete a log source
3
4 require './syslog-ng-lib.pl';
5 &ReadParse();
6 &error_setup($text{'source_err'});
7
8 # Get the old source
9 $conf = &get_config();
10 if (!$in{'new'}) {
11         @sources = &find("source", $conf);
12         ($source) = grep { $_->{'value'} eq $in{'old'} } @sources;
13         $source || &error($text{'source_egone'});
14         $old = $source;
15         }
16 else {
17         $source = { 'name' => 'source',
18                   'type' => 1,
19                   'members' => [ ] };
20         }
21
22 &lock_file($config{'syslogng_conf'});
23 if ($in{'delete'}) {
24         # Just delete it!
25         &check_dependencies('source', $in{'old'}) &&
26             &error(&text('sdelete_eused', $in{'old'}));
27         &save_directive($conf, undef, $source, undef, 0);
28         }
29 else {
30         # Validate inputs, and update object
31         $in{'name'} =~ /^[a-z0-9_]+$/i || &error($text{'source_ename'});
32         if ($in{'new'} || $in{'old'} ne $in{'name'}) {
33                 ($clash) = grep { $_->{'value'} eq $in{'name'} } @sources;
34                 $clash && &error($text{'source_eclash'});
35                 }
36         $source->{'values'} = [ $in{'name'} ];
37
38         # Clear out all existing values
39         $source->{'members'} = [ ];
40
41         if ($in{'internal'}) {
42                 # Save internal option
43                 $internal = { 'name' => 'internal',
44                               'type' => 0,
45                               'values' => [ ] };
46                 &save_directive($conf, $source, undef, $internal, 1);
47                 }
48
49         foreach $t ("unix-stream", "unix-dgram") {
50                 # Save Unix socket file option
51                 next if (!$in{$t});
52                 $in{$t.'_name'} =~ /^\/\S/ ||
53                         &error($text{'source_eunix_name'});
54                 $unix = { 'name' => $t,
55                           'type' => 0,
56                           'values' => [ $in{$t.'_name'} ] };
57                 &save_directive($conf, $source, undef, $unix, 1);
58
59                 # Save owner
60                 if (!$in{$t.'_owner_def'}) {
61                         defined(getpwnam($in{$t.'_owner'})) ||
62                                 &error($text{'source_eowner'});
63                         &save_directive($conf, $unix, "owner",
64                                         $in{$t.'_owner'}, 1);
65                         }
66                 if (!$in{$t.'_group_def'}) {
67                         defined(getgrnam($in{$t.'_group'})) ||
68                                 &error($text{'source_egroup'});
69                         &save_directive($conf, $unix, "group",
70                                         $in{$t.'_group'}, 1);
71                         }
72
73                 # Save permissions
74                 if (!$in{$t.'_perm_def'}) {
75                         $in{$t.'_perm'} =~ /^[0-7]+$/ ||
76                                 &error($text{'source_eperm'});
77                         &save_directive($conf, $unix, "perm",
78                                         $in{$t.'_perm'}, 1);
79                         }
80
81                 if ($t eq "unix-stream") {
82                         # Save keep-alive option
83                         if ($in{$t.'_keep'}) {
84                                 &save_directive($conf, $unix, "keep-alive",
85                                                 $in{$t.'_keep'}, 1);
86                                 }
87
88                         # Save max connections option
89                         if (!$in{$t.'_max_def'}) {
90                                 $in{$t.'_max'} =~ /^\d+$/ ||
91                                         &error($text{'source_emax'});
92                                 &save_directive($conf, $unix, "max-connections",
93                                                 $in{$t.'_max'}, 1);
94                                 }
95                         }
96                 }
97
98         foreach $t ('tcp', 'udp') {
99                 # Save network socket file option
100                 next if (!$in{$t});
101                 $net = { 'name' => $t,
102                          'type' => 0,
103                          'values' => [ ] };
104                 &save_directive($conf, $source, undef, $net, 1);
105
106                 # Save local IP and port
107                 if (!$in{$t.'_ip_def'}) {
108                         &check_ipaddress($in{$t.'_ip'}) ||
109                                 &error($text{'source_eip'});
110                         &save_directive($conf, $net, "ip",
111                                         $in{$t.'_ip'}, 1);
112                         }
113                 if (!$in{$t.'_port_def'}) {
114                         $in{$t.'_port'} =~ /^\d+$/ ||
115                                 &error($text{'source_eport'});
116                         &save_directive($conf, $net, "port",
117                                         $in{$t.'_port'}, 1);
118                         }
119
120                 # Save TCP-specific options and max connections
121                 if ($t eq "tcp") {
122                         if ($in{$t.'_keep'}) {
123                                 &save_directive($conf, $net, "keep-alive",
124                                                 $in{$t.'_keep'}, 1);
125                                 }
126                         if ($in{$t.'_tkeep'}) {
127                                 &save_directive($conf, $net, "tcp-keep-alive",
128                                                 $in{$t.'_tkeep'}, 1);
129                                 }
130                         }
131                 if (!$in{$t.'_max_def'}) {
132                         $in{$t.'_max'} =~ /^\d+$/ ||
133                                 &error($text{'source_emax'});
134                         &save_directive($conf, $net, "max-connections",
135                                         $in{$t.'_max'}, 1);
136                         }
137                 }
138
139         # Save kernel file option
140         if ($in{'file'}) {
141                 $in{'file_name'} =~ /^\/\S/ ||
142                         &error($text{'source_efile_name'});
143                 $file = { 'name' => 'file',
144                           'type' => 0,
145                           'values' => [ $in{'file_name'} ] };
146                 &save_directive($conf, $source, undef, $file, 1);
147
148                 # Save log prefix
149                 if (!$in{'file_prefix_def'}) {
150                         $in{'file_prefix'} =~ /\S/ ||
151                             &error($text{'source_eprefix'});
152                         &save_directive($conf, $file, "log_prefix",
153                                         $in{'file_prefix'}, 1);
154                         }
155                 }
156
157         # Save named pipe option
158         if ($in{'pipe'}) {
159                 $in{'pipe_name'} =~ /^\/\S/ ||
160                         &error($text{'source_epipe_name'});
161                 $pipe = { 'name' => 'pipe',
162                           'type' => 0,
163                           'values' => [ $in{'pipe_name'} ] };
164                 &save_directive($conf, $source, undef, $pipe, 1);
165
166                 # Save log prefix and pad size
167                 if (!$in{'pipe_prefix_def'}) {
168                         $in{'pipe_prefix'} =~ /\S/ ||
169                             &error($text{'source_eprefix'});
170                         &save_directive($conf, $pipe, "log_prefix",
171                                         $in{'pipe_prefix'}, 1);
172                         }
173                 if (!$in{'pipe_pad_def'}) {
174                         $in{'pipe_pad'} =~ /^\d+$/ ||
175                             &error($text{'source_epad'});
176                         &save_directive($conf, $pipe, "pad_size",
177                                         $in{'pipe_pad'}, 1);
178                         }
179                 }
180                 
181         # Save Solaris streams option
182         if ($in{'sun-streams'}) {
183                 $in{'sun_streams_name'} =~ /^\/\S/ ||
184                         &error($text{'source_esun_streams_name'});
185                 $sun_streams = { 'name' => 'sun-streams',
186                                  'type' => 0,
187                                  'values' => [ $in{'sun_streams_name'} ] };
188                 &save_directive($conf, $source, undef, $sun_streams, 1);
189
190                 # Save door file
191                 $in{'sun_streams_door'} =~ /\S/ ||
192                     &error($text{'source_edoor'});
193                 &save_directive($conf, $sun_streams, "door",
194                                 $in{'sun_streams_door'}, 1);
195                 }
196
197
198         # Actually update the object
199         &save_directive($conf, undef, $old, $source, 0);
200
201         # Update dependent log targets
202         if (!$in{'new'}) {
203                 &rename_dependencies('source', $in{'old'}, $in{'name'});
204                 }
205         }
206
207 &unlock_file($config{'syslogng_conf'});
208 &webmin_log($in{'delete'} ? 'delete' : $in{'new'} ? 'create' : 'modify',
209             'source', $in{'old'} || $in{'name'});
210 &redirect("list_sources.cgi");
211