1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
|
#!/usr/bin/env perl
#-*- Mode: perl; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
# Network configurator. Designed to be architecture and distribution independent.
#
# Copyright (C) 2000-2001 Ximian, Inc.
#
# Authors: Hans Petter Jansson <hpj@ximian.com>
# Michael Vogt <mvo@debian.org> (Debian Support)
# Arturo Espinosa <arturo@ximian.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Library General Public License as published
# by the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Library General Public License for more details.
#
# You should have received a copy of the GNU Library General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
# Best viewed with 100 columns of width.
# Configuration files affected:
#
# /etc/resolv.conf
# /etc/host.conf
# /etc/hosts
# /etc/sysconfig/network
# /etc/rc.config
# /etc/smb.conf
# Running programs affected:
#
# smbd
# nmbd
# ifconfig: check current interfaces and activate/deactivate.
BEGIN {
$SCRIPTSDIR = "___scriptsdir___";
if ($SCRIPTSDIR =~ /^___scriptsdir__[_]/)
{
$SCRIPTSDIR = ".";
$DOTIN = ".in";
}
require "$SCRIPTSDIR/general.pl$DOTIN";
require "$SCRIPTSDIR/platform.pl$DOTIN";
require "$SCRIPTSDIR/util.pl$DOTIN";
require "$SCRIPTSDIR/file.pl$DOTIN";
require "$SCRIPTSDIR/xml.pl$DOTIN";
require "$SCRIPTSDIR/network.pl$DOTIN";
}
# Debug stuff
#$xst_prefix = "/tmp/y";
#open STDERR, ">/tmp/err";
# --- Tool information --- #
$name = "network";
$version = "___version___";
@platforms = ("redhat-5.2", "redhat-6.0", "redhat-6.1", "redhat-6.2", "redhat-7.0", "redhat-7.1",
"mandrake-7.1", "mandrake-7.2",
"debian-2.2", "debian-woody",
"suse-7.0", "turbolinux-7.0");
$description =<<"end_of_description;";
Configures all network parameters and interfaces.
end_of_description;
$progress_max = 10;
# --- XML parsing ---
# Scan XML from standard input to an internal tree.
sub xml_parse
{
my ($tree, %hash);
# Scan XML to tree.
$tree = &xst_xml_scan;
# Walk the tree recursively and extract configuration parameters.
# This is the top level - find and enter the "network" tag.
while (@$tree)
{
if ($$tree[0] eq "network") { &xml_parse_network ($$tree[1], \%hash); }
shift @$tree;
shift @$tree;
}
return(\%hash);
}
# <network>...</network>
sub push_unique
{
my ($arr, $val) = @_;
my $i;
foreach $i (@$arr)
{
return if $i eq $val;
}
push @$arr, $val;
}
sub xml_parse_network
{
my $tree = $_[0];
my $hash = $_[1];
my (@searchdomain, @nameserver, @order, %statichost, %interface, %dialing);
shift @$tree; # Skip attributes.
while (@$tree)
{
if ($$tree[0] eq "auto") { $$hash{"auto"} = &xst_xml_get_pcdata ($$tree[1]); }
elsif ($$tree[0] eq "hostname") { $$hash{"hostname"} = &xst_xml_get_pcdata ($$tree[1]); }
elsif ($$tree[0] eq "gateway") { $$hash{"gateway"} = &xst_xml_get_pcdata ($$tree[1]); }
elsif ($$tree[0] eq "gatewaydev") { $$hash{"gatewaydev"} = &xst_xml_get_pcdata ($$tree[1]); }
elsif ($$tree[0] eq "domain") { $$hash{"domain"} = &xst_xml_get_pcdata ($$tree[1]); }
elsif ($$tree[0] eq "nameserver") { &push_unique (\@nameserver, &xst_xml_get_pcdata ($$tree[1])); }
elsif ($$tree[0] eq "searchdomain") { &push_unique (\@searchdomain, &xst_xml_get_pcdata ($$tree[1])); }
elsif ($$tree[0] eq "order") { push (@order, &xst_xml_get_pcdata ($$tree[1])); }
elsif ($$tree[0] eq "hostmatch") { $$hash{"hostmatch"} = &xst_xml_get_pcdata ($$tree[1]); }
elsif ($$tree[0] eq "statichost") { &xml_parse_statichost ($$tree[1], \%statichost); }
elsif ($$tree[0] eq "workgroup") { $$hash{"workgroup"} = &xst_xml_get_pcdata ($$tree[1]); }
elsif ($$tree[0] eq "description") { $$hash{"description"} = &xst_xml_get_pcdata ($$tree[1]); }
elsif ($$tree[0] eq "winsserver") { $$hash{"winsserver"} = &xst_xml_get_pcdata ($$tree[1]); }
elsif ($$tree[0] eq "winsuse") { $$hash{"winsuse"} = &xst_xml_get_pcdata ($$tree[1]); }
elsif ($$tree[0] eq "smbuse") { $$hash{"smbuse"} = &xst_xml_get_pcdata ($$tree[1]); }
elsif ($$tree[0] eq "interface") { &xst_network_xml_parse_interface ($$tree[1], \%interface); }
elsif ($$tree[0] eq "dialing") { &xml_parse_dialing ($$tree[1], \%dialing); }
shift @$tree;
shift @$tree;
}
$$hash{"order"} = \@order unless $#order < 0;
$$hash{"searchdomain"} = \@searchdomain unless $#searchdomain < 0;
$$hash{"nameserver"} = \@nameserver unless $#nameserver < 0;
$$hash{"statichost"} = \%statichost unless scalar keys %statichost == 0;
$$hash{"interface"} = \%interface unless scalar keys %interface == 0;
$$hash{"dialing"} = \%dialing unless scalar keys %dialing == 0;
}
# <network><statichost>...</statichost></network>
sub xml_parse_statichost
{
my $tree = $_[0];
my $statichost = $_[1];
my $ip;
my @alias;
shift @$tree;
while (@$tree)
{
if ($$tree[0] eq "ip") { $ip = &xst_xml_get_pcdata($$tree[1]); }
elsif ($$tree[0] eq "alias") { push(@alias, &xst_xml_get_pcdata($$tree[1])); }
shift @$tree;
shift @$tree;
}
if ($ip =~ /[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/)
{
$$statichost{$ip} = \@alias;
}
}
sub xml_parse_dialing
{
my $tree = $_[0];
my $dialing = $_[1];
my %hash;
my $name;
shift @$tree;
while (@$tree)
{
$hash{$$tree[0]} = &xst_xml_get_pcdata ($$tree[1]);
shift @$tree;
shift @$tree;
}
$name = $hash{"name"};
$$dialing{$name} = \%hash;
}
# --- XML printing --- #
sub xml_print_statichost
{
my ($h) = $_[0];
my ($statichost, $i, $j, $val);
&xst_xml_print_vspace ();
foreach $i (keys %{$$h{"statichost"}})
{
$statichost = $ {$$h{"statichost"}}{$i};
&xst_xml_print_line ("<statichost>\n");
&xst_xml_enter ();
$val = &xst_xml_quote ($i);
&xst_xml_print_line ("<ip>$val</ip>\n");
foreach $j (@$statichost)
{
$val = &xst_xml_quote ($j);
&xst_xml_print_line ("<alias>$val</alias>\n");
}
&xst_xml_leave ();
&xst_xml_print_line ("</statichost>\n");
}
}
sub xml_print
{
my ($h) = @_;
my @scalar_keys =
qw(auto hostname gateway gatewaydev gwdevunsup domain
hostmatch workgroup description winsserver winsuse smbuse
smartdhcpcd smbinstalled dialinstalled);
my @array_keys =
qw(nameserver searchdomain order);
&xst_xml_print_begin ();
# Hostname, domain, search domains, nameservers.
&xst_xml_print_scalars ($h, @scalar_keys);
&xst_xml_print_arrays ($h, @array_keys);
&xml_print_statichost ($h);
&xst_xml_print_hash_hash ($$h{"interface"}, "interface");
&xst_xml_print_hash_hash ($$h{"dialing"}, "dialing");
&xst_xml_print_end ();
}
# Top-level actions.
sub get
{
my $hash;
# network interface stuff
$hash = &xst_network_conf_get ();
&xst_network_ensure_loopback ($hash);
&xst_report_end ();
&xml_print ($hash);
}
sub set
{
my $hash;
$hash = &xml_parse ();
&xst_network_conf_set ($hash);
&xst_report_end ();
}
# --- Filter config: XML in, XML out --- #
sub filter
{
my $hash;
$hash = &xml_parse ();
&xst_report_end ();
&xml_print ($hash);
}
sub enable_iface
{
my ($tool, $iface, $enabled) = @_;
my (%dist_attrib, $iface_set);
my %hash = ("file" => $iface);
%dist_attrib = &xst_network_get_interface_replace_table ();
$iface_set = $dist_attrib{"iface_set"};
&$iface_set (\%hash, undef, $enabled, 1);
# Don't forget to do xst_end when the reports are over!
&xst_report_end ();
# XML output would come here, but this directive returns no XML.
}
# --- Main --- #
# get, set and filter are special cases that don't need more parameters than a ref to their function.
# Read general.pl.in:xst_run_directive to know about the format of this hash.
$directives = {
"get" => [ \&get, [], "" ],
"set" => [ \&set, [], "" ],
"filter" => [ \&filter, [], "" ],
"enable_iface" => [ \&enable_iface, [ "interface", "enabled" ],
"Immediatly enable or disable a given interface. " .
"interface is the file tag value, enabled is 1 or 0." ]
};
$tool = &xst_init ($name, $version, $description, $directives, @ARGV);
&xst_platform_ensure_supported ($tool, @platforms);
&xst_run ($tool);
|