Handle hostnames with upper-case letters
[webmin.git] / openslp / slp-lib.pl
1 # An OpenSLP webmin module
2 # by Monty Charlton <monty@caldera.com>,
3 #
4 # Copyright (c) 2000 Caldera Systems
5 #
6 # Permission to use, copy, modify, and distribute this software and its
7 # documentation under the terms of the GNU General Public License is hereby 
8 # granted. No representations are made about the suitability of this software 
9 # for any purpose. It is provided "as is" without express or implied warranty.
10 # See the GNU General Public License for more details.
11 #
12
13 BEGIN { push(@INC, ".."); };
14 use WebminCore;
15 &init_config();
16
17 #$config_file = "./config-$gconfig{'os_type'}";
18 $config_file = "$module_config_directory/config";
19 $config = &parse_config_file;
20
21 # get_snda_config()
22 sub get_snda_config
23 {
24 local $snda;
25 flock SLP, 1 || &error("$text->{'error_flock_on'} $config->{'slpd_conf'}: $!\n");
26 open(SLP, $config->{'slpd_conf'}) || &error("$text->{'error_open'} $config->{'slpd_conf'}: $!\n");
27 while(<SLP>) {
28         s/\r|\n//g;
29         if (/^(\s|#|;)*net.slp.useScopes\s*=\s*(.*)/) {
30                 push(@{$snda->{'useScopes'}}, split(/\s*,\s*/, $2));
31                 $snda->{'useScopesDisabled'}++ if ($1 =~ /;|#/);
32                 }
33         elsif (/^(\s|#|;)*net.slp.DAAddresses\s*=\s*(.*)/) {
34                 push(@{$snda->{'DAAddresses'}}, split(/\s*,\s*/, $2));
35                 $snda->{'DAAddressesDisabled'}++ if ($1 =~ /;|#/);
36                 }
37         }
38 close(SLP);
39 flock SLP, 8 || &error("$text->{'error_flock_off'} $config->{'slpd_conf'}: $!\n");
40 return $snda;
41 }
42
43 # get_netcfg_config()
44 sub get_netcfg_config
45 {
46 local $netcfg;
47 flock SLP, 1 || &error("$text->{'error_flock_on'} $config->{'slpd_conf'}: $!\n");
48 open(SLP, $config->{'slpd_conf'}) || &error("$text->{'error_open'} $config->{'slpd_conf'}: $!\n");
49 while(<SLP>) {
50         s/\r|\n//g;
51         if (/^(\s|#|;)*net.slp.isBroadcastOnly\s*=\s*(\S+)/) {
52                 $netcfg->{'isBroadcastOnly'} = $2;
53                 $netcfg->{'isBroadcastOnlyDisabled'}++ if ($1 =~ /;|#/);
54                 }
55         elsif (/^(\s|#|;)*net.slp.passiveDADetection\s*=\s*(\S+)/) {
56                 $netcfg->{'passiveDADetection'} = $2;
57                 $netcfg->{'passiveDADetectionDisabled'}++ if ($1 =~ /;|#/);
58                 }
59         elsif (/^(\s|#|;)*net.slp.activeDADetection\s*=\s*(\S+)/) {
60                 $netcfg->{'activeDADetection'} = $2;
61                 $netcfg->{'activeDADetectionDisabled'}++ if ($1 =~ /;|#/);
62                 }
63         elsif (/^(\s|#|;)*net.slp.DAActiveDiscoveryInterval\s*=\s*(\S+)/) {
64                 $netcfg->{'DAActiveDiscoveryInterval'} = $2;
65                 $netcfg->{'DAActiveDiscoveryIntervalDisabled'}++ if ($1 =~ /;|#/);
66                 }
67         elsif (/^(\s|#|;)*net.slp.multicastTTL\s*=\s*(\S+)/) {
68                 $netcfg->{'multicastTTL'} = $2;
69                 $netcfg->{'multicastTTLDisabled'}++ if ($1 =~ /;|#/);
70                 }
71         elsif (/^(\s|#|;)*net.slp.DADiscoveryMaximumWait\s*=\s*(\S+)/) {
72                 $netcfg->{'DADiscoveryMaximumWait'} = $2;
73                 $netcfg->{'DADiscoveryMaximumWaitDisabled'}++ if ($1 =~ /;|#/);
74                 }
75         elsif (/^(\s|#|;)*net.slp.DADiscoveryTimeouts\s*=\s*(.*)/) {
76                 push(@{$netcfg->{'DADiscoveryTimeouts'}}, split(/\s*,\s*/, $2));
77                 $netcfg->{'DADiscoveryTimeoutsDisabled'}++ if ($1 =~ /;|#/);
78                 }
79         elsif (/^(\s|#|;)*net.slp.HintsFile\s*=\s*(\S+)/) {
80                 $netcfg->{'HintsFile'} = $2;
81                 $netcfg->{'HintsFileDisabled'}++ if ($1 =~ /;|#/);
82                 }
83         elsif (/^(\s|#|;)*net.slp.multicastMaximumWait\s*=\s*(\S+)/) {
84                 $netcfg->{'multicastMaximumWait'} = $2;
85                 $netcfg->{'multicastMaximumWaitDisabled'}++ if ($1 =~ /;|#/);
86                 }
87         elsif (/^(\s|#|;)*net.slp.multicastTimeouts\s*=\s*(.*)/) {
88                 push(@{$netcfg->{'multicastTimeouts'}}, split(/\s*,\s*/, $2));
89                 $netcfg->{'multicastTimeoutsDisabled'}++ if ($1 =~ /;|#/);
90                 }
91         elsif (/^(\s|#|;)*net.slp.unicastMaximumWait\s*=\s*(\S+)/) {
92                 $netcfg->{'unicastMaximumWait'} = $2;
93                 $netcfg->{'unicastMaximumWaitDisabled'}++ if ($1 =~ /;|#/);
94                 }
95         elsif (/^(\s|#|;)*net.slp.randomWaitBound\s*=\s*(\S+)/) {
96                 $netcfg->{'randomWaitBound'} = $2;
97                 $netcfg->{'randomWaitBoundDisabled'}++ if ($1 =~ /;|#/);
98                 }
99         elsif (/^(\s|#|;)*net.slp.MTU\s*=\s*(\S+)/) {
100                 $netcfg->{'MTU'} = $2;
101                 $netcfg->{'MTUDisabled'}++ if ($1 =~ /;|#/);
102                 }
103         elsif (/^(\s|#|;)*net\.slp\.interfaces\s*=\s*(.*)/) {
104                 push(@{$netcfg->{'interfaces'}}, split(/\s*,\s*/, $2));
105                 $netcfg->{'interfacesDisabled'}++ if ($1 =~ /;|#/);
106                 }
107         }
108 close(SLP);
109 flock SLP, 8 || &error("$text->{'error_flock_off'} $config->{'slpd_conf'}: $!\n");
110 return $netcfg;
111
112
113 # get_dacfg_config()
114 sub get_dacfg_config
115 {
116 local $dacfg;
117 flock SLP, 1 || &error("$text->{'error_flock_on'} $config->{'slpd_conf'}: $!\n");
118 open(SLP, $config->{'slpd_conf'}) || &error("$text->{'error_open'} $config->{'slpd_conf'}: $!\n");
119 while(<SLP>) {
120         s/\r|\n//g;
121         if (/^(\s|#|;)*net.slp.isDA\s*=\s*(\S+)/) {
122                 $dacfg->{'isDA'} = $2;
123                 $dacfg->{'isDADisabled'}++ if ($1 =~ /;|#/);
124                 }
125         }
126 close(SLP);
127 flock SLP, 8 || &error("$text->{'error_flock_off'} $config->{'slpd_conf'}: $!\n");
128 return $dacfg;
129 }
130
131 # get_log_config()
132 sub get_log_config
133 {
134 local $log;
135 flock SLP, 1 || &error("$text->{'error_flock_on'} $config->{'slpd_conf'}: $!\n");
136 open(SLP, $config->{'slpd_conf'}) || &error("$text->{'error_open'} $config->{'slpd_conf'}: $!\n");
137 while(<SLP>) {
138         s/\r|\n//g;
139         if (/^(\s|#|;)*net.slp.traceDATraffic\s*=\s*(\S+)/) {
140                 $dacfg->{'traceDATraffic'} = $2;
141                 $dacfg->{'traceDATrafficDisabled'}++ if ($1 =~ /;|#/);
142                 }
143         elsif (/^(\s|#|;)*net.slp.traceMsg\s*=\s*(\S+)/) {
144                 $dacfg->{'traceMsg'} = $2;
145                 $dacfg->{'traceMsgDisabled'}++ if ($1 =~ /;|#/);
146                 }
147         elsif (/^(\s|#|;)*net.slp.traceDrop\s*=\s*(\S+)/) {
148                 $dacfg->{'traceDrop'} = $2;
149                 $dacfg->{'traceDropDisabled'}++ if ($1 =~ /;|#/);
150                 }
151         elsif (/^(\s|#|;)*net.slp.traceReg\s*=\s*(\S+)/) {
152                 $dacfg->{'traceReg'} = $2;
153                 $dacfg->{'traceRegDisabled'}++ if ($1 =~ /;|#/);
154                 }
155
156         }
157 close(SLP);
158 flock SLP, 8 || &error("$text->{'error_flock_off'} $config->{'slpd_conf'}: $!\n");
159 return $dacfg;
160 }
161
162 # enable_list_line(&list, &line)
163 sub enable_list_line
164 {
165 flock SLP, 1 || &error("$text->{'error_flock_on'} $config->{'slpd_conf'}: $!\n");
166 open(SLP, $config->{'slpd_conf'}) || &error("$text->{'error_open'} $config->{'slpd_conf'}: $!\n");
167 local @slp = <SLP>;
168 close(SLP);
169 flock SLP, 8 || &error("$text->{'error_flock_off'} $config->{'slpd_conf'}: $!\n");
170 flock SLP, 2 || &error("$text->{'error_flock_on'} $config->{'slpd_conf'}: $!\n");
171 &open_tempfile(SLP, ">$config->{'slpd_conf'}") || &error("$text->{'error_open'} $config->{'slpd_conf'}: $!\n");
172 local $line = pop;
173 local $list = join ",", @_;
174 foreach(@slp) {
175         if (/^(\s|#|;)*net.slp.$line\s*=\s*\S+/ && $list) {
176                 &print_tempfile(SLP, "net.slp.$line=$list\n");
177                 }
178         else {
179                 &print_tempfile(SLP, $_);
180                 }
181         }
182 &close_tempfile(SLP);
183 flock SLP, 8 || &error("$text->{'error_flock_off'} $config->{'slpd_conf'}: $!\n");
184 }
185
186 # enable_single_val_line(&val, &line)
187 sub enable_single_val_line
188 {
189 open(SLP, $config->{slpd_conf}) || &error("$text->{'error_open'} $config->{'slpd_conf'}: $!\n");
190 flock SLP, 1 || &error("$text->{'error_flock_on'} $config->{'slpd_conf'}: $!\n");
191 local @slp = <SLP>;
192 close(SLP);
193 flock SLP, 8 || &error("$text->{'error_flock_off'} $config->{'slpd_conf'}: $!\n");
194 flock SLP, 2 || &error("$text->{'error_flock_on'} $config->{'slpd_conf'}: $!\n");
195 &open_tempfile(SLP, ">$config->{'slpd_conf'}") || &error("$text->{'error_open'} $config->{'slpd_conf'}: $!\n");
196 local $line = pop;
197 foreach(@slp) {
198         if (/^(\s|#|;)*net.slp.$line\s*=\s*\S+\s*$/) {
199                 &print_tempfile(SLP, "net.slp.$line=@_[0]\n");
200                 }
201         else {
202                 &print_tempfile(SLP, $_);
203                 }
204         }
205 &close_tempfile(SLP);
206 flock SLP, 8 || &error("$text->{'error_flock_off'} $config->{'slpd_conf'}: $!\n");
207 }
208
209 # disable_line(line)
210 sub disable_line
211 {
212 flock SLP, 1 || &error("$text->{'error_flock_on'} $config->{'slpd_conf'}: $!\n");
213 open(SLP, $config->{'slpd_conf'}) || &error("$text->{'error_open'} $config->{'slpd_conf'}: $!\n");
214 local @slp = <SLP>;
215 close(SLP);
216 flock SLP, 8 || &error("$text->{'error_flock_off'} $config->{'slpd_conf'}: $!\n");
217 flock SLP, 2 || &error("$text->{'error_flock_on'} $config->{'slpd_conf'}: $!\n");
218 &open_tempfile(SLP, ">$config->{'slpd_conf'}") || &error("$text->{'error_open'} $config->{'slpd_conf'}: $!\n");
219 foreach(@slp) {
220         if (/^(\s|#|;)*net.slp.@_[0]\s*=\s*(\S+)/) {
221                 &print_tempfile(SLP, ";net.slp.@_[0]=$2\n");
222                 }
223         else {
224                 &print_tempfile(SLP, $_);
225                 }
226         }
227 &close_tempfile(SLP);
228 flock SLP, 8 || &error("$text->{'error_flock_off'} $config->{'slpd_conf'}: $!\n");
229 }
230
231 # parse_config_file
232 sub parse_config_file
233
234 local %dummy;
235 flock FH, 1 || &error("$text->{'error_flock_on'} $config_file: $!\n");
236 open(FH, $config_file) || &error("$text->{'error_open'} $config_file: $!\n");
237 while (<FH>) {
238   $dummy{$1} = $2 if (/(\S+)=(.+)/)
239 }
240 return \%dummy;
241 close(FH);
242 flock FH, 8 || &error("$text->{'error_flock_off'} $config_file: $!\n");
243 }
244
245 # restart
246 sub restart
247 {
248 local $pid;
249 flock PID, 1 || &error("$text->{'error_flock_on'} $config->{'slpd_pid'}: $!\n");
250 open(PID, $config->{'slpd_pid'}) || &start_slpd;
251 while (<PID>) {
252   $pid=$1, last if (/(\d+)/)
253 }
254 close(PID);
255 flock PID, 8 || &error("$text->{'error_flock_off'} $config->{'slpd_pid'}: $!\n");
256 if ($pid =~ /\d+/) {
257   kill('HUP', $pid) || &error("$text->{'error_hup'}: $config->{'slpd_pid'}\n");
258 } else {
259   &start_slpd;
260 }
261 }
262
263 # is slpd running?
264 sub slpd_is_running
265 {
266 local $pid;
267 flock PID, 1 || &error("$text->{'error_flock_on'} $config->{'slpd_pid'}: $!\n");
268 open(PID, $config->{'slpd_pid'}) || return 0;
269 while (<PID>) {
270   $pid=$1, last if (/(\d+)/)
271 }
272 close(PID);
273 flock PID, 8 || &error("$text->{'error_flock_off'} $config->{'slpd_pid'}: $!\n");
274 if ($pid =~ /\d+/) {
275   # the pid file has a number in it but 
276   # we need to check if slpd is _actually_ running
277   if (kill 0 => $pid) {
278     return $pid;
279   } else {
280     &stop_slpd;
281     return 0;
282   }
283 } else {
284   return 0;
285 }
286 }
287
288 sub start_slpd
289 {
290 &error_setup($text->{'start_err'});
291 local $temp = &transname();
292 local $rv = &system_logged("($config->{'start_cmd'}) >$temp 2>&1");
293 local $out = `cat $temp`;
294 unlink($temp);
295 sleep(2);
296 &webmin_log("start");
297 }
298
299 sub stop_slpd
300 {
301 $out = &backquote_logged("$config->{'stop_cmd'} 2>&1");
302 &error_setup($text->{'stop_err'});
303 if ($?) {
304   &error("<pre>$?\n$out</pre>");
305 }
306 &webmin_log("stop");
307 }
308
309 1;
310