summaryrefslogtreecommitdiff
path: root/network-conf.in
blob: 2e706c65162d0ae8f7d966e39e70e4e38a9d6087 (plain)
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
#!/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>
#          Grzegorz Golawski <grzegol@pld.org.pl> (PLD Support)
#
# 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
#$gst_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",
              "redhat-7.2", "redhat-8.0", "redhat-9",
              
              "mandrake-7.1", "mandrake-7.2", "mandrake-9.0", "mandrake-9.1", 
              
              "debian-2.2", "debian-woody", "debian-sarge",
              
              "suse-7.0", "turbolinux-7.0", "pld-1.0");

$description =<<"end_of_description;";
       Configures all network parameters and interfaces.
end_of_description;

$progress_max = 10;

$profile_file = "profiles.xml";


# --- XML parsing ---

# Scan XML from standard input to an internal tree.
sub xml_parse
{
  my ($tree, %hash, $elem);
  # Scan XML to tree.

  $tree = &gst_xml_scan ();

  # Walk the tree recursively and extract configuration parameters.
  # This is the top level - find and enter the "network" tag.

  while ($elem = shift @$tree)
  {
    if ($elem eq "network") { &xml_parse_network (shift @$tree, \%hash); }
    else { &gst_report ("xml_unexp_tag", $elem); 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, $hash) = @_;
  my ($elem);
  my (@searchdomain, @nameserver, @order, %statichost, %interface, %dialing);

  shift @$tree;  # Skip attributes.

  while ($elem = shift @$tree)
  {
    if    ($elem eq "auto")          { $$hash{"auto"} =        &gst_xml_get_pcdata (shift @$tree); }
    elsif ($elem eq "hostname")      { $$hash{"hostname"} =    &gst_xml_get_pcdata (shift @$tree); }
    elsif ($elem eq "gateway")       { $$hash{"gateway"} =     &gst_xml_get_pcdata (shift @$tree); }
    elsif ($elem eq "gatewaydev")    { $$hash{"gatewaydev"} =  &gst_xml_get_pcdata (shift @$tree); }
    elsif ($elem eq "domain")        { $$hash{"domain"} =      &gst_xml_get_pcdata (shift @$tree); }
    elsif ($elem eq "workgroup")     { $$hash{"workgroup"} =   &gst_xml_get_pcdata (shift @$tree); }
    elsif ($elem eq "smbdesc")       { $$hash{"smbdesc"} =     &gst_xml_get_pcdata (shift @$tree); }
    elsif ($elem eq "name")          { $$hash{"name"} =        &gst_xml_get_pcdata (shift @$tree); }
    elsif ($elem eq "description")   { $$hash{"description"} = &gst_xml_get_pcdata (shift @$tree); }
    elsif ($elem eq "winsserver")    { $$hash{"winsserver"} =  &gst_xml_get_pcdata (shift @$tree); }
    elsif ($elem eq "winsuse")       { $$hash{"winsuse"} =     &gst_xml_get_pcdata (shift @$tree); }
    elsif ($elem eq "smbuse")        { $$hash{"smbuse"} =      &gst_xml_get_pcdata (shift @$tree); }
    elsif ($elem eq "hostmatch")     { $$hash{"hostmatch"} =   &gst_xml_get_pcdata (shift @$tree); }
    elsif ($elem eq "nameserver")    { &push_unique (\@nameserver, &gst_xml_get_pcdata (shift @$tree)); }
    elsif ($elem eq "searchdomain")  { &push_unique (\@searchdomain, &gst_xml_get_pcdata (shift @$tree)); }
    elsif ($elem eq "order")         { push (@order, &gst_xml_get_pcdata (shift @$tree)); }
    elsif ($elem eq "statichost")    { &xml_parse_statichost (shift @$tree, \%statichost); }
    elsif ($elem eq "interface")     { &gst_network_xml_parse_interface (shift @$tree, \%interface); }
    elsif ($elem eq "dialing")       { &xml_parse_dialing (shift @$tree, \%dialing); }
    elsif ($elem eq "dialinstalled") { shift @$tree; }
    elsif ($elem eq "smbinstalled")  { shift @$tree; }
    elsif ($elem eq "smartdhcpcd")   { shift @$tree; }
    elsif ($elem eq "gwdevunsup")    { shift @$tree; }
    elsif ($elem eq "profiledb")     { &xml_parse_profiledb (shift @$tree, $hash); }
    else                             { &gst_report ("xml_unexp_tag", $elem); 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, $statichost) = @_;
  my ($ip, @alias, $elem);

  shift @$tree;

  while ($elem = shift @$tree)
  {
    if    ($elem eq "ip")    { $ip = &gst_xml_get_pcdata (shift @$tree); }
    elsif ($elem eq "alias") { push(@alias, &gst_xml_get_pcdata (shift @$tree)); }
    else                     { &gst_report ("xml_unexp_tag", $elem); shift @$tree; }
  }

  # common regexp for IPv4 and IPv6
  if ($ip =~ /([0-9]+[\.:])+/)
  {
    $$statichost{$ip} = \@alias;
  }
}

sub xml_parse_dialing
{
  my ($tree, $dialing) = @_;
  my (%hash, $name, $elem);

  shift @$tree;
  
  while ($elem = shift @$tree)
  {
    $hash{$elem} = &gst_xml_get_pcdata (shift @$tree);
  }

  $name = $hash{"name"};
  $$dialing{$name} = \%hash;
}

sub xml_parse_profile
{
  my ($tree, $hash) = @_;
  my (%profile);

  &xml_parse_network ($tree, \%profile);

  push @{$hash->{"profiledb"}{"profile"}}, \%profile;
}

sub xml_parse_profiledb
{
  my ($tree, $hash) = @_;

  shift @$tree; # Skip attributes.

  while (@$tree)
  {
    if ($$tree[0] eq "profile") { &xml_parse_profile ($$tree[1], $hash); }
    else
    {
		  &gst_report ("xml_unexp_tag", $$tree[0]);
    }

    shift @$tree;
    shift @$tree;
  }
}

# --- XML printing --- #

sub xml_print_statichost
{
  my ($h) = $_[0];
  my ($statichost, $i, $j, $val);
  
  &gst_xml_print_vspace ();
  foreach $i (keys %{$$h{"statichost"}})
  {
    $statichost = $ {$$h{"statichost"}}{$i};
    &gst_xml_print_line ("<statichost>\n");
    &gst_xml_enter ();
    $val = &gst_xml_quote ($i);
    &gst_xml_print_line ("<ip>$val</ip>\n");
    foreach $j (@$statichost)
    {
      $val = &gst_xml_quote ($j);
      &gst_xml_print_line ("<alias>$val</alias>\n");
    }
    &gst_xml_leave ();
    &gst_xml_print_line ("</statichost>\n");
  }
}

sub xml_print_profiledb
{
  my ($h) = @_;
  my @scalar_keys =
      qw(auto hostname gateway gatewaydev gwdevunsup domain 
         hostmatch workgroup description smbdesc winsserver winsuse smbuse
         smartdhcpcd smbinstalled dialinstalled name);
  my @array_keys =
      qw(nameserver searchdomain order);


  &gst_xml_print_vspace ();

  &gst_xml_print_line ("<profiledb>\n");
  &gst_xml_enter ();
  foreach $i (@{$$h{"profiledb"}{"profile"}})
  {
    gst_xml_print_line ("<profile>\n");
    gst_xml_enter ();

    #print profile contents
    &gst_xml_print_scalars ($i, @scalar_keys);
    &gst_xml_print_arrays ($i, @array_keys);
    &xml_print_statichost ($i);

    &gst_xml_print_hash_hash ($$i{"interface"}, "interface");
    &gst_xml_print_hash_hash ($$i{"dialing"}, "dialing");

    gst_xml_leave ();
    gst_xml_print_line ("</profile>\n");
  }
  gst_xml_leave ();
  gst_xml_print_line ("</profiledb>\n");
}

sub xml_print
{
  my ($h) = @_;
  my @scalar_keys =
      qw(auto hostname gateway gatewaydev gwdevunsup domain 
         hostmatch workgroup smbdesc winsserver winsuse smbuse
         smartdhcpcd smbinstalled dialinstalled);
  my @array_keys =
      qw(nameserver searchdomain order);

  &gst_xml_print_begin ();

  # Hostname, domain, search domains, nameservers.

  &gst_xml_print_scalars ($h, @scalar_keys);
  &gst_xml_print_arrays ($h, @array_keys);
  &xml_print_statichost ($h);

  &gst_xml_print_hash_hash ($$h{"interface"}, "interface");
  &gst_xml_print_hash_hash ($$h{"dialing"}, "dialing");
  &xml_print_profiledb ($h);

  &gst_xml_print_end ();
}


# Reading profiles
sub read_profiledb
{
  my ($hash) = @_;
  my ($path);
  my ($tree);

  $path = gst_file_get_data_path () . "/" . $main::tool->{"name"} . "/" . $profile_file;
  $tree = &gst_xml_scan ($path, $tool);
  if ($tree && scalar @$tree)
  {
    if ($$tree[0] eq 'profiledb')
    {
      xml_parse_profiledb ($$tree[1], $hash);
    }
    else
    {
      gst_report ('xml_unexp_tag', $$tree[0]);
    }
  }
}

# Writing profiles
sub write_profiledb
{
  my ($hash) = @_;
  my $profiledb = $hash->{'profiledb'};
  my $path = &gst_file_get_data_path () . "/" . $main::tool->{'name'} . "/" . $profile_file;

  if ($profiledb) {
    # Write our profiles.
    my $fh = &gst_file_open_write_from_names ($path);
    if ($fh)
    {
      local *STDOUT = $fh;
      &xml_print_profiledb ($hash);
      close ($fh);

      delete $hash->{'profiledb'};
    }
  }
  else
  {
    gst_file_remove ($path);
  }
}


# Top-level actions.
sub get
{
  my $hash;

  # network interface stuff
  $hash = &gst_network_conf_get ();
  &read_profiledb (\%$hash);
  &gst_network_ensure_loopback ($hash);

  &gst_report_end ();
  &xml_print ($hash);
}


sub set
{
  my $hash;
  
  $hash = &xml_parse ();
  &write_profiledb ($hash);
  &gst_network_conf_set ($hash);
  &gst_report_end ();
}


# --- Filter config: XML in, XML out --- #


sub filter
{
  my $hash;
  
  $hash = &xml_parse ();
  &gst_report_end ();
  &xml_print ($hash);
}


sub enable_iface
{
  my ($tool, $iface, $enabled) = @_;
  my (%dist_attrib, $iface_set);
  my %hash = ("file" => $iface);
  
  %dist_attrib = &gst_network_get_interface_replace_table ();
  $iface_set = $dist_attrib{"iface_set"};
  &$iface_set (\%hash, undef, $enabled, 1);
  # Don't forget to do gst_end when the reports are over!
  &gst_report_end ();
  # XML output would come here, but this directive returns no XML.
}


sub list_ifaces
{
  my ($tool) = @_;
  my ($ifaces, $iface, @arr);

  $ifaces = &gst_network_interfaces_get_info ();
  
  foreach $iface (keys %$ifaces)
  {
    push @arr, $$ifaces{$iface};
  }
  
  &gst_report_end ();
  &gst_xml_print_begin ("network-ifaces");
  &gst_xml_print_structure (\@arr, "interface");
  &gst_xml_print_end ("network-ifaces");
}

sub detect_modem
{
  my ($tool) = @_;
  my (%hash);

  $$hash {"device"} = &gst_network_autodetect_modem ();
  &gst_report_end ();
  
  &gst_xml_print_begin ("network-modem-device");
  &gst_xml_print_hash ($hash);
  &gst_xml_print_end ("network-modem-device");
}


# --- Main --- #

# get, set and filter are special cases that don't need more parameters than a ref to their function.
# Read general.pl.in:gst_run_directive to know about the format of this hash.

$directives = {
  "get"          => [ \&get,          [], "" ],
  "set"          => [ \&set,          [], "" ],
  "filter"       => [ \&filter,       [], "" ],
  "list_ifaces"  => [ \&list_ifaces,  [],
                      "List interfaces and active/inactive status." ],
  "enable_iface" => [ \&enable_iface, [ "interface", "enabled" ],
                      "Immediatly enable or disable a given interface. " .
                      "interface is the file tag value, enabled is 1 or 0." ],
  "detect_modem" => [ \&detect_modem, [], "Detects the modem device." ]
};

$tool = &gst_init ($name, $version, $description, $directives, @ARGV);
&gst_platform_ensure_supported ($tool, @platforms);
&gst_run ($tool);