summaryrefslogtreecommitdiffstats
path: root/perl-install/network/network.pm
blob: 9586906351a3fa06008dc3364ea45bd943b9c8c2 (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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
package network::network; # $Id$wir

#-######################################################################################
#- misc imports
#-######################################################################################

use strict;

use Socket;
use common;
use detect_devices;
use run_program;
use any;
use vars qw(@ISA @EXPORT);
use log;

@ISA = qw(Exporter);
@EXPORT = qw(resolv configureNetworkIntf netmask dns is_ip masked_ip findIntf addDefaultRoute read_all_conf dnsServers guessHostname configureNetworkNet read_resolv_conf read_interface_conf add2hosts gateway configureNetwork2 write_conf sethostname down_it read_conf write_resolv_conf up_it);

#-######################################################################################
#- Functions
#-######################################################################################
sub read_conf {
    my ($file) = @_;
    +{ getVarsFromSh($file) };
}

sub read_resolv_conf_raw {
    my ($file) = @_;
    $file ||= "$::prefix/etc/resolv.conf";
    { nameserver => [ cat_($file) =~ /^\s*nameserver\s+(\S+)/mg ],
      search => [ if_(cat_($file) =~ /^\s*search\s+(.*)/m, split(' ', $1)) ] };
}

sub read_resolv_conf {
    my ($file) = @_;
    my $resolv_conf = read_resolv_conf_raw($file);
    +{
      (mapn { $_[0] => $_[1] } [ qw(dnsServer dnsServer2 dnsServer3) ], $resolv_conf->{nameserver}),
      (mapn { $_[0] => $_[1] } [ qw(DOMAINNAME DOMAINNAME2 DOMAINNAME3) ], $resolv_conf->{search}),
     };
}

sub read_interface_conf {
    my ($file) = @_;
    my %intf = getVarsFromSh($file);

    $intf{BOOTPROTO} ||= 'static';
    $intf{isPtp} = $intf{NETWORK} eq '255.255.255.255';
    $intf{isUp} = 1;
    \%intf;
}

sub read_dhcpd_conf {
    my ($file) = @_;
    $file ||= "$::prefix/etc/dhcpd.conf";
    { option_routers => [ cat_($file) =~ /^\s*option routers\s+(\S+);/mg ],
      subnet_mask => [ if_(cat_($file) =~ /^\s*option subnet-mask\s+(.*);/mg, split(' ', $1)) ],
      domain_name => [ if_(cat_($file) =~ /^\s*option domain-name\s+\"(.*)\";/mg, split(' ', $1)) ],
      domain_name_servers => [ if_(cat_($file) =~ /^\s*option domain-name-servers\s+(.*);/m, split(' ', $1)) ],
      dynamic_bootp => [ if_(cat_($file) =~ /^\s*range dynamic-bootp\s+\S+\.(\d+)\s+\S+\.(\d+)\s*;/m, split(' ', $1)) ],
      default_lease_time => [ if_(cat_($file) =~ /^\s*default-lease-time\s+(.*);/m, split(' ', $1)) ],
      max_lease_time => [ if_(cat_($file) =~ /^\s*max-lease-time\s+(.*);/m, split(' ', $1)) ] };
}

sub read_tmdns_conf {
    my ($file) = @_;
    local *F; open F, $file or die "cannot open file $file: $!";
    local $_;
    my %outf;

    while (<F>) {
	($outf{ZEROCONF_HOSTNAME}) = /^\s*hostname\s*=\s*(\w+)/ and return \%outf;
    }
    
    \%outf;
}

sub up_it {
    my ($prefix, $intfs) = @_;
    $_->{isUp} and return foreach values %$intfs;
    my $f = "/etc/resolv.conf"; symlink "$prefix/$f", $f;
    run_program::rooted($prefix, "/etc/rc.d/init.d/network", "start");
    $_->{isUp} = 1 foreach values %$intfs;
}

sub down_it {
    my ($prefix, $intfs) = @_;
    run_program::rooted($prefix, "/etc/rc.d/init.d/network", "stop");
    $_->{isUp} = 1 foreach values %$intfs;
}

sub write_conf {
    my ($file, $netc) = @_;

    if ($netc->{HOSTNAME} && $netc->{HOSTNAME} =~ /\.(.+)$/) {
	$netc->{DOMAINNAME} = $1;
    }
    $netc->{NETWORKING} = 'yes';

    setVarsInSh($file, $netc, qw(HOSTNAME NETWORKING GATEWAY GATEWAYDEV NISDOMAIN));
}

sub write_zeroconf {
    my ($file, $zhostname) = @_;
    eval { substInFile { s/^\s*(hostname)\s*=.*/$1 = $zhostname/ } $file };
}

sub write_resolv_conf {
    my ($file, $netc) = @_;

    my %new = (
        search => [ grep { $_ } uniq(@$netc{'DOMAINNAME', 'DOMAINNAME2', 'DOMAINNAME3'}) ],
        nameserver => [ grep { $_ } uniq(@$netc{'dnsServer', 'dnsServer2', 'dnsServer3'}) ],
    );

    my (%prev, @unknown);
    foreach (cat_($file)) {
	s/\s+$//;
	s/^[#\s]*//;

	if (my ($key, $val) = /^(search|nameserver)\s+(.*)$/) {
	    push @{$prev{$key}}, $val;
	} elsif (/^ppp temp entry$/) {
	} elsif (/\S/) {
	    push @unknown, $_;
	}
    }
    unlink $file;  #- workaround situation when /etc/resolv.conf is an absolute link to /etc/ppp/resolv.conf or whatever

    if (@{$new{search}} || @{$new{nameserver}}) {
	$prev{$_} = [ difference2($prev{$_} || [], $new{$_}) ] foreach keys %new;

	my @search = do {
	    my @new = if_(@{$new{search}}, "search " . join(' ', @{$new{search}}) . "\n");
	    my @old = if_(@{$prev{search}}, "# search " . join(' ', @{$prev{search}}) . "\n");
	    @new, @old;
	};
	my @nameserver = do {
	    my @new = map { "nameserver $_\n" } @{$new{nameserver}};
	    my @old = map { "# nameserver $_\n" } @{$prev{nameserver}};
	    @new, @old;
	};
	output_with_perm($file, 0644, @search, @nameserver, (map { "# $_\n" } @unknown), "\n# ppp temp entry\n");

	#-res_init();		# reinit the resolver so DNS changes take affect
	1;
    } else {
	log::explanations("neither domain name nor dns server are configured");
	0;
    }
}

sub write_interface_conf {
    my ($file, $intf, $_netc, $prefix) = @_;

    if ($intf->{HWADDR} && -e "$prefix/sbin/ip") {
	$intf->{HWADDR} = undef;
	if (my $s = `LC_ALL= LANG= $prefix/sbin/ip -o link show $intf->{DEVICE} 2>/dev/null`) {
	    if ($s =~ m|.*link/ether\s([0-9a-z:]+)\s|) {
		$intf->{HWADDR} = $1;
	    }
	}
    }
    my @ip = split '\.', $intf->{IPADDR};
    my @mask = split '\.', $intf->{NETMASK};

    add2hash($intf, {
		     BROADCAST => join('.', mapn { int($_[0]) | ((~int($_[1])) & 255) } \@ip, \@mask),
		     NETWORK   => join('.', mapn { int($_[0]) &        $_[1]          } \@ip, \@mask),
		     ONBOOT => bool2yesno(!member($intf->{DEVICE}, map { $_->{device} } detect_devices::pcmcia_probe())),
		    });

    $intf->{BOOTPROTO} =~ s/dhcp.*/dhcp/;

    setVarsInSh($file, $intf, qw(DEVICE BOOTPROTO IPADDR NETMASK NETWORK BROADCAST ONBOOT HWADDR MII_NOT_SUPPORTED), if_($intf->{wireless_eth}, qw(WIRELESS_MODE WIRELESS_ESSID WIRELESS_NWID WIRELESS_FREQ WIRELESS_SENS WIRELESS_RATE WIRELESS_ENC_KEY WIRELESS_RTS WIRELESS_FRAG WIRELESS_IWCONFIG WIRELESS_IWSPY WIRELESS_IWPRIV)), if_($intf->{BOOTPROTO} eq "dhcp", qw(DHCP_HOSTNAME NEEDHOSTNAME)));
    log::explanations("written $intf->{DEVICE} interface configuration in $file");
}

sub add2hosts {
    my ($file, $hostname, @ips) = @_;

    my %l = map { if_(/\s*(\S+)(.*)/, $1 => $2) }
            grep { !/\s+\Q$hostname\E\s*$/ } cat_($file);

    my $sub_hostname = $hostname =~ /(.*?)\./ ? " $1" : '';
    $l{$_} = "\t\t$hostname$sub_hostname" foreach grep { $_ } @ips;

    log::explanations("writing host information to $file");
    output($file, map { "$_$l{$_}\n" } keys %l);
}

# The interface/gateway needs to be configured before this will work!
sub guessHostname {
    my ($prefix, $netc, $intf) = @_;

    $intf->{isUp} && dnsServers($netc) or return 0;
    $netc->{HOSTNAME} && $netc->{DOMAINNAME} and return 1;

    write_resolv_conf("$prefix/etc/resolv.conf", $netc);

    my $name = gethostbyaddr(Socket::inet_aton($intf->{IPADDR}), Socket::AF_INET()) or log::explanations("reverse name lookup failed"), return 0;

    log::explanations("reverse name lookup worked");

    add2hash($netc, { HOSTNAME => $name });
    1;
}

sub addDefaultRoute {
    my ($netc) = @_;
    c::addDefaultRoute($netc->{GATEWAY}) if $netc->{GATEWAY};
}

sub sethostname {
    my ($netc) = @_;
    my $text;
    syscall_("sethostname", $netc->{HOSTNAME}, length $netc->{HOSTNAME}) ? ($text="set sethostname to $netc->{HOSTNAME}") : ($text="sethostname failed: $!");
    log::explanations($text);
}

sub resolv($) {
    my ($name) = @_;
    is_ip($name) and return $name;
    my $a = join(".", unpack "C4", (gethostbyname $name)[4]);
    #-log::explanations("resolved $name in $a");
    $a;
}

sub dnsServers {
    my ($netc) = @_;
    my %used_dns; @used_dns{$netc->{dnsServer}, $netc->{dnsServer2}, $netc->{dnsServer3}} = (1, 2, 3);
    sort { $used_dns{$a} <=> $used_dns{$b} } grep { $_ } keys %used_dns;
}

sub findIntf {
    my ($intf, $device) = @_;
    $intf->{$device}{DEVICE} = $device;
    $intf->{$device};
}

my $ip_regexp = qr/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/;

sub is_ip {
    my ($ip) = @_;
    my @fields = $ip =~ $ip_regexp or return;
    every { 0 <= $_ && $_ <= 255 } @fields or return;
    @fields;
}

sub ip_compare {
    my ($ip1, $ip2) = @_;
    my (@ip1_fields) = $ip1 =~ $ip_regexp;
    my (@ip2_fields) = $ip2 =~ $ip_regexp;
    
    every { $ip1_fields[$_] eq $ip2_fields[$_] } (0 .. 3);
}

sub is_ip_forbidden {
    my ($ip) = @_;
    my @forbidden = ('127.0.0.1', '255.255.255.255');
    
    any { ip_compare($ip, $_) } @forbidden;
}

sub is_domain_name {
    my ($name) = @_;
    my @fields = split /\./, $name;
    $name !~ /\.$/ && @fields > 0 && @fields == grep { /^[[:alnum:]](?:[\-[:alnum:]]{0,61}[[:alnum:]])?$/ } @fields;
}

sub netmask {
    my ($ip) = @_;
    return "255.255.255.0" unless is_ip($ip);
    $ip =~ $ip_regexp or warn "IP_regexp failed\n" and return "255.255.255.0";
    if ($1 >= 1 && $1 < 127) {
	"255.0.0.0";    #-1.0.0.0 to 127.0.0.0
    } elsif ($1  >= 128 && $1 <= 191) {
	"255.255.0.0";  #-128.0.0.0 to 191.255.0.0
    } elsif ($1 >= 192 && $1 <= 223) {
	"255.255.255.0";
    } else {
	"255.255.255.255"; #-experimental classes
    }
}

sub masked_ip {
    my ($ip) = @_;
    my @ip = is_ip($ip) or return '';
    my @mask = netmask($ip) =~ $ip_regexp;
    for (my $i = 0; $i < @ip; $i++) {
	$ip[$i] &= int $mask[$i];
    }
    join(".", @ip);
}

sub dns {
    my ($ip) = @_;
    my @masked = masked_ip($ip) =~ $ip_regexp;
    $masked[3]  = 2;
    join(".", @masked);

}

sub gateway {
    my ($ip) = @_;
    my @masked = masked_ip($ip) =~ $ip_regexp;
    $masked[3]  = 1;
    join(".", @masked);

}

sub configureNetworkIntf {
    my ($netc, $in, $intf, $net_device, $skip, $module) = @_;
    my $text;
    my @wireless_modules = qw(aironet_cs aironet4500_cs hermes airo orinoco_cs orinoco airo_cs netwave_cs ray_cs wavelan_cs wvlan_cs airport);
    my $flag = 0;
    foreach (@wireless_modules) {
	$module =~ /$_/ and $flag = 1;
    }
    if ($flag) {
	$intf->{wireless_eth} = 1;
	$netc->{wireless_eth} = 1;
	$intf->{WIRELESS_MODE} = "Managed";
	$intf->{WIRELESS_ESSID} = "any";
    }
    if ($net_device eq $intf->{DEVICE}) {
	$skip and return 1;
	$text = N("WARNING: this device has been previously configured to connect to the Internet.
Simply accept to keep this device configured.
Modifying the fields below will override this configuration.");
    }
    else {
	$text = N("Please enter the IP configuration for this machine.
Each item should be entered as an IP address in dotted-decimal
notation (for example, 1.2.3.4).");
    }
    my $auto_ip = $intf->{BOOTPROTO} !~ /static/;
    my $onboot = $intf->{ONBOOT} ? $intf->{ONBOOT} =~ /yes/ : bool2yesno(!member($intf->{DEVICE}, map { $_->{device} } detect_devices::pcmcia_probe()));
    my $needhostname = $intf->{NEEDHOSTNAME} !~ /no/; 
    my $hotplug = $::isStandalone && !$intf->{MII_NOT_SUPPORTED} || 1;
    my $track_network_id = $::isStandalone && $intf->{HWADDR} || detect_devices::isLaptop();
    delete $intf->{NETWORK};
    delete $intf->{BROADCAST};
    my @fields = qw(IPADDR NETMASK);

    $in->ask_from(N("Configuring network device %s", $intf->{DEVICE}),
  	          (N("Configuring network device %s", $intf->{DEVICE}) . ($module ? N(" (driver %s)", $module) : '') . "\n\n") .
	          $text,
	         [ { label => N("Automatic IP"), val => \$auto_ip, type => "bool", text => N("(bootp/dhcp/zeroconf)") },
                { label => N("IP address"), val => \$intf->{IPADDR}, disabled => sub { $auto_ip } },
	           { label => N("Netmask"), val => \$intf->{NETMASK}, disabled => sub { $auto_ip } },
		   
                { label => N("DHCP host name"), val => \$intf->{DHCP_HOSTNAME}, disabled => sub { ! ($auto_ip && $needhostname) }, advanced => 1 },
                { text => N("Track network card id (useful for laptops)"), val => \$track_network_id, type => "bool", advanced => 1 },
                { text => N("Network Hotplugging"), val => \$hotplug, type => "bool", advanced => 1 },
                { text => N("Assign host name from DHCP address"), val => \$needhostname, type => "bool", disabled => sub { ! $auto_ip }, advanced => 1 },
                { text => N("Start at boot"), val => \$onboot, type => "bool", advanced => 1 },
		   if_($intf->{wireless_eth},
	           { label => "WIRELESS_MODE", val => \$intf->{WIRELESS_MODE}, list => [ "Ad-hoc", "Managed", "Master", "Repeater", "Secondary", "Auto" ] },
	           { label => "WIRELESS_ESSID", val => \$intf->{WIRELESS_ESSID} },
	           { label => "WIRELESS_NWID", val => \$intf->{WIRELESS_NWID} },
	           { label => "WIRELESS_FREQ", val => \$intf->{WIRELESS_FREQ} },
	           { label => "WIRELESS_SENS", val => \$intf->{WIRELESS_SENS} },
	           { label => "WIRELESS_RATE", val => \$intf->{WIRELESS_RATE} },
	           { label => "WIRELESS_ENC_KEY", val => \$intf->{WIRELESS_ENC_KEY} },
	           { label => "WIRELESS_RTS", val => \$intf->{WIRELESS_RTS} },
	           { label => "WIRELESS_FRAG", val => \$intf->{WIRELESS_FRAG} },
	           { label => "WIRELESS_IWCONFIG", val => \$intf->{WIRELESS_IWCONFIG} },
	           { label => "WIRELESS_IWSPY", val => \$intf->{WIRELESS_IWSPY} },
	           { label => "WIRELESS_IWPRIV", val => \$intf->{WIRELESS_IWPRIV} }
	           ),
	         ],
	         complete => sub {
		     
		     $intf->{BOOTPROTO} = $auto_ip ? join('', if_($auto_ip, "dhcp")) : "static";
		     $netc->{DHCP} = $auto_ip;
		     return 0 if $auto_ip;

		     if (my @bad = map_index { if_(!is_ip($intf->{$_}), $::i) } @fields) {
			 $in->ask_warn('', N("IP address should be in format 1.2.3.4"));
			 return 1, $bad[0];
		     }

		     if (is_ip_forbidden($intf->{IPADDR})) {
			 $in->ask_warn('', N("Warning : IP address %s is usually reserved !", $intf->{IPADDR}));
		     }
		     
		     return 0 if !$intf->{WIRELESS_FREQ};
		     if ($intf->{WIRELESS_FREQ} !~ /[0-9.]*[kGM]/) {
			 $in->ask_warn('', N("Freq should have the suffix k, M or G (for example, \"2.46G\" for 2.46 GHz frequency), or add enough '0' (zeroes)."));
			 return 1,6;
		     }
		     if ($intf->{WIRELESS_RATE} !~ /[0-9.]*[kGM]/) {
			 $in->ask_warn('', N("Rate should have the suffix k, M or G (for example, \"11M\" for 11M), or add enough '0' (zeroes)."));
			 return 1,8;
		     }
		 },
	         focus_out => sub {
	         	 $intf->{NETMASK} ||= netmask($intf->{IPADDR}) unless $_[0]
	         }
    	    ) or return;
    $intf->{ONBOOT} = bool2yesno($onboot);
    $intf->{NEEDHOSTNAME} = bool2yesno($needhostname);
    $intf->{MII_NOT_SUPPORTED} = bool2yesno(!$hotplug);
    $intf->{HWADDR} = $track_network_id or delete $intf->{HWADDR};
    1;
}

sub configureNetworkNet {
    my ($in, $netc, $intf, @devices) = @_;

    $netc->{dnsServer} ||= dns($intf->{IPADDR});
    my $gateway_ex = gateway($intf->{IPADDR});
#-    $netc->{GATEWAY}   ||= gateway($intf->{IPADDR});

    $in->ask_from(N("Configuring network"),
N("Please enter your host name.
Your host name should be a fully-qualified host name,
such as ``mybox.mylab.myco.com''.
You may also enter the IP address of the gateway if you have one.") . N("

Enter a Zeroconf host name without any dot if you don't
want to use the default host name."),
			       [ { label => N("Host name"), val => \$netc->{HOSTNAME} },
                                 { label => N("Zeroconf Host name"), val => \$netc->{ZEROCONF_HOSTNAME} },
				 { label => N("DNS server"), val => \$netc->{dnsServer} },
				 { label => N("Gateway (e.g. %s)", $gateway_ex), val => \$netc->{GATEWAY} },
				    if_(@devices > 1,
				 { label => N("Gateway device"), val => \$netc->{GATEWAYDEV}, list => \@devices },
				    ),
			       ],
		               complete => sub {
				   if ($netc->{dnsServer} && !is_ip($netc->{dnsServer})) {
				       $in->ask_warn('', N("DNS server address should be in format 1.2.3.4"));
				       return 1;
				   }
				   if ($netc->{GATEWAY} && !is_ip($netc->{GATEWAY})) {
				       $in->ask_warn('', N("Gateway address should be in format 1.2.3.4"));
				       return 1;
				   }
				   if ($netc->{ZEROCONF_HOSTNAME} && $netc->{ZEROCONF_HOSTNAME} =~ /\./) {
				       $in->ask_warn('', N("Zeroconf host name must not contain a ."));
				       return 1;
				   }
				   0;
			       }
			      );
}

sub miscellaneous_choose {
    my ($in, $u) = @_;

    $in->ask_from('',
       N("Proxies configuration"),
       [ { label => N("HTTP proxy"), val => \$u->{http_proxy} },
         { label => N("FTP proxy"),  val => \$u->{ftp_proxy} },
       ],
       complete => sub {
	   $u->{http_proxy} =~ m,^($|http://), or $in->ask_warn('', N("Proxy should be http://...")), return 1,0;
	   $u->{ftp_proxy} =~ m,^($|ftp://|http://), or $in->ask_warn('', N("URL should begin with 'ftp:' or 'http:'")), return 1,1;
	   0;
       }
    ) or return;
    1;
}

sub proxy_configure {
    my ($u) = @_;
    setExportedVarsInSh("$::prefix/etc/profile.d/proxy.sh",  $u, qw(http_proxy ftp_proxy));
    setExportedVarsInCsh("$::prefix/etc/profile.d/proxy.csh", $u, qw(http_proxy ftp_proxy));
}

sub read_all_conf {
    my ($prefix, $netc, $intf, $o_netcnx) = @_;
    $netc ||= {}; $intf ||= {};
    my $netcnx = $o_netcnx || {};
    add2hash($netc, read_conf("$prefix/etc/sysconfig/network")) if -r "$prefix/etc/sysconfig/network";
    add2hash($netc, read_resolv_conf());
    add2hash($netc, read_tmdns_conf("$prefix/etc/tmdns.conf")) if -r "$prefix/etc/tmdns.conf";
    foreach (all("$prefix/etc/sysconfig/network-scripts")) {
	if (/ifcfg-(\w+)/ && $1 ne 'lo') {
	    my $intf = findIntf($intf, $1);
	    add2hash($intf, { getVarsFromSh("$prefix/etc/sysconfig/network-scripts/$_") });
	}
    }
    $netcnx->{type} or probe_netcnx_type($prefix, $netc, $intf, $netcnx);
}

sub probe_netcnx_type {
    my ($prefix, $_netc, $intf, $netcnx) = @_;
    #- try to probe $netcnx->{type} which is used almost everywhere.
    unless ($netcnx->{type}) {
	#- ugly hack to determine network type (avoid saying not configured in summary).
	-e "$prefix/etc/ppp/peers/adsl" and $netcnx->{type} ||= 'adsl'; # enough ?
	-e "$prefix/etc/ppp/ioptions1B" || -e "$prefix/etc/ppp/ioptions2B" and $netcnx->{type} ||= 'isdn'; # enough ?
	$intf->{ppp0} and $netcnx->{type} ||= 'modem';
	$intf->{eth0} and $netcnx->{type} ||= 'lan';
    }
}

sub easy_dhcp {
    my ($netc, $intf) = @_;

    return if text2bool($netc->{NETWORKING});

    require modules;
    require network::ethernet;
    modules::load_category('network/main|gigabit|usb');
    my @all_cards = network::ethernet::conf_network_card_backend();

    #- only for a single network card
    (any { $_->[0] eq 'eth0' } @all_cards) && (every { $_->[0] ne 'eth1' } @all_cards) or return;

    log::explanations("easy_dhcp: found eth0");

    network::ethernet::conf_network_card_backend($netc, $intf, 'dhcp', 'eth0');

    put_in_hash($netc, { 
			NETWORKING => "yes",
			DHCP => "yes",
		       });
    1;
}

#- configureNetwork2 : configure the network interfaces.
#- input
#-  $prefix
#-  $netc
#-  $intf
#- $netc input
#-  NETWORKING : networking flag : string : "yes" by default
#-  FORWARD_IPV4 : forward IP flag : string : "false" by default
#-  HOSTNAME : hostname : string : "localhost.localdomain" by default
#-  DOMAINNAME : domainname : string : $netc->{HOSTNAME} =~ /\.(.*)/ by default
#-  DOMAINNAME2 : well it's another domainname : have to look further why we used 2
#-  The following are facultatives
#-  DHCP_HOSTNAME : If you have a dhcp and want to set the hostname
#-  GATEWAY : gateway
#-  GATEWAYDEV : gateway interface
#-  NISDOMAIN : nis domain
#-  $netc->{dnsServer} : dns server 1
#-  $netc->{dnsServer2} : dns server 2
#-  $netc->{dnsServer3} : dns server 3 : note that we uses the dns1 for the LAN, and the 2 others for the internet conx
#- $intf input: for each $device (for example ethx)
#-  $intf->{$device}{IPADDR} : IP address
#-  $intf->{$device}{NETMASK} : netmask
#-  $intf->{$device}{DEVICE} : DEVICE = $device
#-  $intf->{$device}{BOOTPROTO} : boot prototype : "bootp" or "dhcp" or "pump" or ...
sub configureNetwork2 {
    my ($in, $prefix, $netc, $intf) = @_;
    my $etc = "$prefix/etc";

    $netc->{wireless_eth} and $in->do_pkgs->install(qw(wireless-tools));
    write_conf("$etc/sysconfig/network", $netc);
    write_resolv_conf("$etc/resolv.conf", $netc) if ! $netc->{DHCP};
    write_interface_conf("$etc/sysconfig/network-scripts/ifcfg-$_->{DEVICE}", $_, $netc, $prefix) foreach grep { $_->{DEVICE} ne 'ppp0' } values %$intf;
    add2hosts("$etc/hosts", $netc->{HOSTNAME}, map { $_->{IPADDR} } values %$intf) if $netc->{HOSTNAME} && !$netc->{DHCP};
    add2hosts("$etc/hosts", "localhost", "127.0.0.1");

    any { $_->{BOOTPROTO} eq "dhcp" } values %$intf and $in->do_pkgs->install($netc->{dhcp_client} || 'dhcp-client');
    $in->do_pkgs->install(qw(tmdns)) if !$in->do_pkgs->is_installed('bind');
    $in->do_pkgs->install(qw(zcip));
    $netc->{ZEROCONF_HOSTNAME} and write_zeroconf("$etc/tmdns.conf", $netc->{ZEROCONF_HOSTNAME});      
    any { $_->{BOOTPROTO} =~ /^(pump|bootp)$/ } values %$intf and $in->do_pkgs->install('pump');

    proxy_configure($::o->{miscellaneous});
}

1;
n class="hl opt">=~ m!([^/]+)/([^/]+)$!) { $workgroup = ""; $smbserver = $1; $smbshare = $2; } else { die "The \"smb://\" URI must at least contain the server name and the share name!\n"; } if (network::is_ip($smbserver)) { $smbserverip = $smbserver; $smbserver = ""; } } return if !$in->ask_from(_("SMB (Windows 9x/NT) Printer Options"), _("To print to a SMB printer, you need to provide the SMB host name (Note! It may be different from its TCP/IP hostname!) and possibly the IP address of the print server, as well as the share name for the printer you wish to access and any applicable user name, password, and workgroup information."), [ { label => _("SMB server host"), val => \$smbserver }, { label => _("SMB server IP"), val => \$smbserverip }, { label => _("Share name"), val => \$smbshare }, { label => _("User name"), val => \$smbuser }, { label => _("Password"), val => \$smbpassword, hidden => 1 }, { label => _("Workgroup"), val => \$workgroup }, ], complete => sub { unless ((network::is_ip($smbserverip)) || ($smbserverip eq "")) { $in->ask_warn('', _("IP address should be in format 1.2.3.4")); return (1,1); } unless (($smbserver ne "") || ($smbserverip ne "")) { $in->ask_warn('', _("Either the server name or the server's IP must be given!")); return (1,0); } unless ($smbshare ne "") { $in->ask_warn('', _("Samba share name missing!")); return (1,2); } return 0; } ); #- make the DeviceURI from, try to probe for available variable to #- build a suitable URI. $printer->{currentqueue}{'connect'} = join '', ("smb://", ($smbuser && ($smbuser . ($smbpassword && ":$smbpassword") . "@")), ($workgroup && ("$workgroup/")), ($smbserver || $smbserverip), "/$smbshare"); if ((!$::testing) && (!printer::files_exist((qw(/usr/bin/smbclient))))) { $in->do_pkgs->install('samba-client'); } $printer->{SPOOLER} eq 'cups' and printer::restart_queue($printer); 1; } sub setup_ncp { my ($printer, $in) = @_; $in->set_help('setupNCP') if $::isInstall; my ($uri, $ncpuser, $ncppassword, $ncpserver, $ncpqueue); my $queue = $printer->{OLD_QUEUE}; if (($printer->{configured}{$queue}) && ($printer->{currentqueue}{'connect'} =~ m/^ncp:/)) { $uri = $printer->{currentqueue}{'connect'}; my $parameters = $uri =~ m!^\s*ncp://(.*)$!; # Get the user's login and password from the URI if ($parameters =~ m!([^@]*)@([^@]+)!) { my $login = $1; $parameters = $2; if ($login =~ m!([^:]*):([^:]*)!) { $ncpuser = $1; $ncppassword = $2; } else { $ncpuser = $login; $ncppassword = ""; } } else { $ncpuser = ""; $ncppassword = ""; } # Get the workgroup, server, and share name if ($parameters =~ m!([^/]+)/([^/]+)$!) { $ncpserver = $1; $ncpqueue = $2; } else { die "The \"ncp://\" URI must at least contain the server name and the share name!\n"; } } return if !$in->ask_from(_("NetWare Printer Options"), _("To print on a NetWare printer, you need to provide the NetWare print server name (Note! it may be different from its TCP/IP hostname!) as well as the print queue name for the printer you wish to access and any applicable user name and password."), [ { label => _("Printer Server"), val => \$ncpserver }, { label => _("Print Queue Name"), val => \$ncpqueue }, { label => _("User name"), val => \$ncpuser }, { label => _("Password"), val => \$ncppassword, hidden => 1 } ], complete => sub { unless ($ncpserver ne "") { $in->ask_warn('', _("NCP server name missing!")); return (1,0); } unless ($ncpqueue ne "") { $in->ask_warn('', _("NCP queue name missing!")); return (1,1); } return 0; } ); # Generate the Foomatic URI $printer->{currentqueue}{'connect'} = join '', ("ncp://", ($ncpuser && ($ncpuser . ($ncppassword && ":$ncppassword") . "@")), "$ncpserver/$ncpqueue"); if ((!$::testing) && (!printer::files_exist((qw(/usr/bin/nprint))))) { $in->do_pkgs->install('ncpfs'); } 1; } sub setup_socket { my ($printer, $in) = @_; $in->set_help('setupSocket') if $::isInstall; my ($hostname, $port, $uri, $remotehost,$remoteport); my $queue = $printer->{OLD_QUEUE}; if (($printer->{configured}{$queue}) && ($printer->{currentqueue}{'connect'} =~ m/^socket:/)) { $uri = $printer->{currentqueue}{'connect'}; ($remotehost, $remoteport) = $uri =~ m!^\s*socket://([^/:]+):([0-9]+)/?\s*$!; } else { $remotehost = ""; $remoteport = "9100"; } return if !$in->ask_from(_("Socket Printer Options"), _("To print to a socket printer, you need to provide the host name of the printer and optionally the port number. On HP JetDirect servers the port number is usually 9100, on other servers it can vary. See the manual of your hardware."), [ { label => _("Printer host name"), val => \$remotehost }, { label => _("Port"), val => \$remoteport } ], complete => sub { unless ($remotehost ne "") { $in->ask_warn('', _("Printer host name missing!")); return (1,0); } unless ($remoteport =~ /^[0-9]+$/) { $in->ask_warn('', _("The port number should be an integer!")); return (1,1); } return 0; } ); #- make the Foomatic URI $printer->{currentqueue}{'connect'} = join '', ("socket://$remotehost", $remoteport ? (":$remoteport") : ()); #- LPD and LPRng need netcat ('nc') to access to socket printers if ((($printer->{SPOOLER} eq 'lpd') || ($printer->{SPOOLER} eq 'lprng'))&& (!$::testing) && (!printer::files_exist((qw(/usr/bin/nc))))) { $in->do_pkgs->install('nc'); } 1; } sub setup_uri { my ($printer, $in) = @_; $in->set_help('setupURI') if $::isInstall; return if !$in->ask_from(_("Printer Device URI"), _("You can specify directly the URI to access the printer. The URI must fulfill either the CUPS or the Foomatic specifications. Note that not all URI types are supported by all the spoolers."), [ { label => _("Printer Device URI"), val => \$printer->{currentqueue}{'connect'}, list => [ $printer->{currentqueue}{'connect'}, "file:/", "http://", "ipp://", "lpd://", "smb://", "ncp://", "socket://", "postpipe:\"\"", ], not_edit => 0 }, ], complete => sub { unless ($printer->{currentqueue}{'connect'} =~ /[^:]+:.+/) { $in->ask_warn('', _("A valid URI must be entered!")); return (1,0); } return 0; } ); # If the chosen protocol needs additional software, install it. # LPD does not support filtered queues to a remote LPD server by itself # It needs an additional program as "rlpr" if (($printer->{currentqueue}{'connect'} =~ /^lpd:/) && ($printer->{SPOOLER} eq 'lpd') && (!$::testing) && (!printer::files_exist((qw(/usr/bin/rlpr))))) { $in->do_pkgs->install('rlpr'); } if (($printer->{currentqueue}{'connect'} =~ /^smb:/) && (!$::testing) && (!printer::files_exist((qw(/usr/bin/smbclient))))) { $in->do_pkgs->install('samba-client'); } if (($printer->{currentqueue}{'connect'} =~ /^ncp:/) && (!$::testing) && (!printer::files_exist((qw(/usr/bin/nprint))))) { $in->do_pkgs->install('ncpfs'); } #- LPD and LPRng need netcat ('nc') to access to socket printers if (($printer->{currentqueue}{'connect'} =~ /^socket:/) && (($printer->{SPOOLER} eq 'lpd') || ($printer->{SPOOLER} eq 'lprng')) && (!$::testing) && (!printer::files_exist((qw(/usr/bin/nc))))) { $in->do_pkgs->install('nc'); } 1; } sub setup_postpipe { my ($printer, $in) = @_; $in->set_help('setupPostpipe') if $::isInstall; my $uri; my $commandline; my $queue = $printer->{OLD_QUEUE}; if (($printer->{configured}{$queue}) && ($printer->{currentqueue}{'connect'} =~ m/^postpipe:/)) { $uri = $printer->{currentqueue}{'connect'}; $uri =~ m!^\s*postpipe:\"(.*)\"$!; $commandline = $1; } else { $commandline = ""; } return if !$in->ask_from(_("Pipe into command"), _("Here you can specify any arbitrary command line into which the job should be piped instead of being sent directly to a printer."), [ { label => _("Command line"), val => \$commandline }, ], complete => sub { unless ($commandline ne "") { $in->ask_warn('', _("A command line must be entered!")); return (1,0); } return 0; } ); #- make the Foomatic URI $printer->{currentqueue}{'connect'} = "postpipe:$commandline"; 1; } sub choose_printer_name { my ($printer, $in) = @_; # Name, description, location $in->set_help('setupPrinterName') if $::isInstall; my $default = $printer->{currentqueue}{'queue'}; $in->ask_from_ ( { title => _("Enter Printer Name and Comments"), #cancel => !$printer->{configured}{$queue} ? '' : _("Remove queue"), callbacks => { complete => sub { unless ($printer->{currentqueue}{'queue'} =~ /^\w+$/) { $in->ask_warn('', _("Name of printer should contain only letters, numbers and the underscore")); return (1,0); } if (($printer->{configured}{$printer->{currentqueue}{'queue'}}) && ($printer->{currentqueue}{'queue'} ne $default) && (!$in->ask_yesorno('', _("The printer \"%s\" already exists,\ndo you really want to overwrite its configuration?", $printer->{currentqueue}{'queue'}), 0))) { return (1,0); # Let the user correct the name } return 0; }, }, messages => _("Every printer needs a name (for example lp). The Description and Location fields do not need to be filled in. They are comments for the users.") }, [ { label => _("Name of printer"), val => \$printer->{currentqueue}{'queue'} }, { label => _("Description"), val => \$printer->{currentqueue}{'desc'} }, { label => _("Location"), val => \$printer->{currentqueue}{'loc'} }, ]) or return 0; $printer->{QUEUE} = $printer->{currentqueue}{'queue'}; 1; } sub get_db_entry { my ($printer, $in) = @_; #- Read the printer driver database if necessary if ((keys %printer::thedb) == 0) { my $w = $in->wait_message('', _("Reading printer database ...")); printer::read_printer_db($printer->{SPOOLER}); } my $w = $in->wait_message('', _("Preparing printer database ...")); my $queue = $printer->{OLD_QUEUE}; if ($printer->{configured}{$queue}) { # The queue was already configured if ($printer->{configured}{$queue}{'queuedata'}{'foomatic'}) { # The queue was configured with Foomatic my $driverstr; if ($printer->{configured}{$queue}{'driver'} eq "Postscript") { $driverstr = "PostScript"; } else { $driverstr = "GhostScript + $printer->{configured}{$queue}{'driver'}"; } my $make = uc($printer->{configured}{$queue}{'queuedata'}{'make'}); my $model = $printer->{configured}{$queue}{'queuedata'}{'model'}; if ($::expert) { $printer->{DBENTRY} = "$make|$model|$driverstr"; # database key contains the "(recommended)" for the # recommended driver, so add it if necessary if (!($printer::thedb{$printer->{DBENTRY}}{printer})) { $printer->{DBENTRY} .= " (recommended)"; } } else { $printer->{DBENTRY} = "$make|$model"; } $printer->{OLD_CHOICE} = $printer->{DBENTRY}; } elsif (($printer->{SPOOLER} eq "cups") && ($::expert) && ($printer->{configured}{$queue}{'queuedata'}{'ppd'})) { # Do we have a native CUPS driver or a PostScript PPD file? $printer->{DBENTRY} = printer::get_descr_from_ppd($printer) || $printer->{DBENTRY}; $printer->{OLD_CHOICE} = $printer->{DBENTRY}; } else { # Point the list cursor at least to manufacturer and model of the # printer $printer->{DBENTRY} = ""; my $make = uc($printer->{configured}{$queue}{'queuedata'}{'make'}); my $model = $printer->{configured}{$queue}{'queuedata'}{'model'}; my $key; for $key (keys %printer::thedb) { if ((($::expert) && ($key =~ /^$make\|$model\|.*\(recommended\)$/)) || ((!$::expert) && ($key =~ /^$make\|$model$/))) { $printer->{DBENTRY} = $key; } } if ($printer->{DBENTRY} eq "") { # Exact match of make and model did not work, try to clean # ups the model name $model =~ s/PS//; $model =~ s/PostScript//; $model =~ s/Series//; for $key (keys %printer::thedb) { if ((($::expert) && ($key =~ /^$make\|$model\|.*\(recommended\)$/)) || ((!$::expert) && ($key =~ /^$make\|$model$/))) { $printer->{DBENTRY} = $key; } } } if (($printer->{DBENTRY} eq "") && 0) { # Exact match with cleaned-up model did not work, try a best match $printer->{DBENTRY} = bestMatchSentence("$make|$model", keys %printer::thedb); # If the manufacturer was not guessed correctly, discard the # guess. $printer->{DBENTRY} =~ /^([\|])|/; if (lc($1) ne lc($make)) {$printer->{DBENTRY} = ""}; } # Set the OLD_CHOICE to a non-existing value $printer->{OLD_CHOICE} = "XXX"; } } else { if (($::expert) && ($printer->{DBENTRY} !~ (recommended))) { my ($make, $model) = $printer->{DBENTRY} =~ /^([^\|]+)\|([^\|]+)\|/; for my $key (keys %printer::thedb) { if ($key =~ /^$make\|$model\|.*\(recommended\)$/) { $printer->{DBENTRY} = $key; } } } $printer->{OLD_CHOICE} = $printer->{DBENTRY}; } } sub choose_model { my ($printer, $in) = @_; $in->set_help('chooseModel') if $::isInstall; #- Read the printer driver database if necessary if ((keys %printer::thedb) == 0) { my $w = $in->wait_message('', _("Reading printer database ...")); printer::read_printer_db($printer->{SPOOLER}); } if (!$printer::thedb{$printer->{DBENTRY}}) { $printer->{DBENTRY} = _("Raw printer (No driver)"); } # Choose the printer/driver from the list return ($printer->{DBENTRY} = $in->ask_from_treelist(_("Printer model selection"), _("Which printer model do you have?"), '|', [ keys %printer::thedb ], $printer->{DBENTRY})); } sub get_printer_info { my ($printer, $in) = @_; #- Read the printer driver database if necessary #if ((keys %printer::thedb) == 0) { # my $w = $in->wait_message('', _("Reading printer database ...")); # printer::read_printer_db($printer->{SPOOLER}); #} my $queue = $printer->{OLD_QUEUE}; my $oldchoice = $printer->{OLD_CHOICE}; my $newdriver = 0; if ((!$printer->{configured}{$queue}) || # New queue or (($oldchoice) && ($printer->{DBENTRY}) && # make/model/driver changed (($oldchoice ne $printer->{DBENTRY}) || ($printer->{currentqueue}{'driver'} ne $printer::thedb{$printer->{DBENTRY}}{'driver'})))) { delete($printer->{currentqueue}{printer}); delete($printer->{currentqueue}{ppd}); $printer->{currentqueue}{foomatic} = 0; # Read info from printer database foreach (qw(printer ppd driver make model)) { #- copy some parameter, shorter that way... $printer->{currentqueue}{$_} = $printer::thedb{$printer->{DBENTRY}}{$_}; } $newdriver = 1; } # Use the "printer" and not the "foomatic" field to identify a Foomatic # queue because in a new queue "foomatic" is not set yet. if (($printer->{currentqueue}{'printer'}) || # We have a Foomatic queue ($printer->{currentqueue}{'ppd'})) { # We have a CUPS+PPD queue if ($printer->{currentqueue}{'printer'}) { # Foomatic queue? # In case of a new queue "foomatic" was not set yet $printer->{currentqueue}{'foomatic'} = 1; # Now get the options for this printer/driver combo if (($printer->{configured}{$queue}) && ($printer->{configured}{$queue}{'queuedata'}{'foomatic'})) { # The queue was already configured with Foomatic ... if (!$newdriver) { # ... and the user didn't change the printer/driver $printer->{ARGS} = $printer->{configured}{$queue}{'args'}; } else { # ... and the user has chosen another printer/driver $printer->{ARGS} = printer::read_foomatic_options($printer); } } else { # The queue was not configured with Foomatic before # Set some special options $printer->{SPECIAL_OPTIONS} = ''; # Default page size depending on the country/language # (US/Canada -> Letter, Others -> A4) my $pagesize; if ($printer->{PAPERSIZE}) { $printer->{SPECIAL_OPTIONS} .= " -o PageSize=$printer->{PAPERSIZE}"; } elsif (($pagesize = $ENV{'LC_PAPER'}) || ($pagesize = $ENV{'LANG'}) || ($pagesize = $ENV{'LC_ALL'})) { if (($pagesize eq 'en') || ($pagesize eq 'en_US')) { $pagesize = "Letter"; } else { $pagesize = "A4"; } $printer->{SPECIAL_OPTIONS} .= " -o PageSize=$pagesize"; } # oki4w driver -> OKI winprinter which needs the # oki4daemon to work if ($printer->{currentqueue}{'driver'} eq 'oki4w') { if ($printer->{currentqueue}{'connect'} ne 'file:/dev/lp0') { $in->ask_warn(_("OKI winprinter configuration"), _("You are configuring an OKI laser winprinter. These printers\nuse a very special communication protocol and therefore they work only when connected to the first parallel port. When your printer is connected to another port or to a print server box please connect the printer to the first parallel port before you print a test page. Otherwise the printer will not work. Your connection type setting will be ignored by the driver.")); } $printer->{currentqueue}{'connect'} = 'file:/dev/null'; # Start the oki4daemon printer::start_service_on_boot('oki4daemon'); printer::start_service('oki4daemon'); # Set permissions if ($printer->{SPOOLER} eq 'cups') { printer::set_permissions('/dev/oki4drv', '660', 'lp', 'sys'); } elsif ($printer->{SPOOLER} eq 'pdq') { printer::set_permissions('/dev/oki4drv', '666'); } else { printer::set_permissions('/dev/oki4drv', '660', 'lp', 'lp'); } } elsif ($printer->{currentqueue}{'driver'} eq 'lexmarkinkjet') { # Set "Port" option if ($printer->{currentqueue}{'connect'} eq 'file:/dev/lp0') { $printer->{SPECIAL_OPTIONS} .= " -o Port=ParPort1"; } elsif ($printer->{currentqueue}{'connect'} eq 'file:/dev/lp1') { $printer->{SPECIAL_OPTIONS} .= " -o Port=ParPort2"; } elsif ($printer->{currentqueue}{'connect'} eq 'file:/dev/lp2') { $printer->{SPECIAL_OPTIONS} .= " -o Port=ParPort3"; } elsif ($printer->{currentqueue}{'connect'} eq 'file:/dev/usb/lp0') { $printer->{SPECIAL_OPTIONS} .= " -o Port=USB1"; } elsif ($printer->{currentqueue}{'connect'} eq 'file:/dev/usb/lp1') { $printer->{SPECIAL_OPTIONS} .= " -o Port=USB2"; } elsif ($printer->{currentqueue}{'connect'} eq 'file:/dev/usb/lp2') { $printer->{SPECIAL_OPTIONS} .= " -o Port=USB3"; } else { $in->ask_warn(_("Lexmark inkjet configuration"), _("The inkjet printer drivers provided by Lexmark only support local printers, no printers on remote machines or print server boxes. Please connect your printer to a local port or configure it on the machine where it is connected to.")); return 0; } # Set device permissions $printer->{currentqueue}{'connect'} =~ /^\s*file:(\S*)\s*$/; if ($printer->{SPOOLER} eq 'cups') { printer::set_permissions($1, '660', 'lp', 'sys'); } elsif ($printer->{SPOOLER} eq 'pdq') { printer::set_permissions($1, '666'); } else { printer::set_permissions($1, '660', 'lp', 'lp'); } # This is needed to have the device not blocked by the # spooler backend. $printer->{currentqueue}{'connect'} = 'file:/dev/null'; #install packages my $drivertype = $printer->{currentqueue}{'model'}; if ($drivertype eq 'Z22') {$drivertype = 'Z32';} if ($drivertype eq 'Z23') {$drivertype = 'Z33';} $drivertype = lc($drivertype); if (!printer::files_exist("/usr/local/lexmark/$drivertype/$drivertype")) { eval { $in->do_pkgs->install("lexmark-drivers-$drivertype") }; } if (!printer::files_exist("/usr/local/lexmark/$drivertype/$drivertype")) { # Driver installation failed, probably we do not have # the commercial CDs $in->ask_warn(_("Lexmark inkjet configuration"), _("To be able to print with your Lexmark inkjet and this configuration, you need the inkjet printer drivers provided by Lexmark (http://www.lexmark.com/). Go to the US site and click on the \"Drivers\" button. Then choose your model and afterwards \"Linux\" as operating system. The drivers come as RPM packages or shell scripts with interactive graphical installation. You do not need to do this configuration by the graphical frontends. Cancel directly after the license agreement. Then print printhead alignment pages with \"lexmarkmaintain\" and adjust the head alignment settings with this program.")); } } $printer->{ARGS} = printer::read_foomatic_options($printer); delete($printer->{SPECIAL_OPTIONS}); } } elsif ($printer->{currentqueue}{'ppd'}) { # CUPS+PPD queue? # If we had a Foomatic queue before, unmark the flag and initialize # the "printer" and "driver" fields $printer->{currentqueue}{'foomatic'} = 0; $printer->{currentqueue}{'printer'} = undef; $printer->{currentqueue}{'driver'} = "CUPS/PPD"; # Now get the options from this PPD file if ($printer->{configured}{$queue}) { # The queue was already configured if ((!$printer->{DBENTRY}) || (!$oldchoice) || ($printer->{DBENTRY} eq $oldchoice)) { # ... and the user didn't change the printer/driver $printer->{ARGS} = printer::read_cups_options($queue); } else { # ... and the user has chosen another printer/driver $printer->{ARGS} = printer::read_cups_options("/usr/share/cups/model/$printer->{currentqueue}{ppd}"); } } else { # The queue was not configured before $printer->{ARGS} = printer::read_cups_options("/usr/share/cups/model/$printer->{currentqueue}{ppd}"); } } } 1; } sub setup_options { my ($printer, $in) = @_; $in->set_help('setupOptions') if $::isInstall; if (($printer->{currentqueue}{'printer'}) || # We have a Foomatic queue ($printer->{currentqueue}{'ppd'})) { # We have a CUPS+PPD queue # Set up the widgets for the option dialog my @widgets; my @userinputs; my @choicelists; my @shortchoicelists; my $i; for ($i = 0; $i <= $#{$printer->{ARGS}}; $i++) { my $optshortdefault = $printer->{ARGS}[$i]{'default'}; if ($printer->{ARGS}[$i]{'type'} eq 'enum') { # enumerated option push(@choicelists, []); push(@shortchoicelists, []); my $choice; for $choice (@{$printer->{ARGS}[$i]{'vals'}}) { push(@{$choicelists[$i]}, $choice->{'comment'}); push(@{$shortchoicelists[$i]}, $choice->{'value'}); if ($choice->{'value'} eq $optshortdefault) { push(@userinputs, $choice->{'comment'}); } } push(@widgets, { label => $printer->{ARGS}[$i]{'comment'}, val => \$userinputs[$i], not_edit => 1, list => \@{$choicelists[$i]} }); } elsif ($printer->{ARGS}[$i]{'type'} eq 'bool') { # boolean option push(@choicelists, [$printer->{ARGS}[$i]{'name'}, $printer->{ARGS}[$i]{'name_false'}]); push(@shortchoicelists, []); push(@userinputs, $choicelists[$i][1-$optshortdefault]); push(@widgets, { label => $printer->{ARGS}[$i]{'comment'}, val => \$userinputs[$i], not_edit => 1, list => \@{$choicelists[$i]} }); } else { # numerical option push(@choicelists, []); push(@shortchoicelists, []); push(@userinputs, $optshortdefault); push(@widgets, { label => $printer->{ARGS}[$i]{'comment'} . " ($printer->{ARGS}[$i]{'min'} ... " . "$printer->{ARGS}[$i]{'max'})", #type => 'range', #min => $printer->{ARGS}[$i]{'min'}, #max => $printer->{ARGS}[$i]{'max'}, val => \$userinputs[$i] } ); } } # Show the options dialog. The call-back function does a # range check of the numerical options. my $windowtitle = "$printer->{currentqueue}{'make'} $printer->{currentqueue}{'model'}"; if ($::expert) { my $driver = undef; if ($driver = $printer->{currentqueue}{driver}) { if ($printer->{currentqueue}{foomatic}) { if ($driver eq 'Postscript') { $driver = "PostScript"; } else { $driver = "GhostScript + $driver"; } } elsif ($printer->{currentqueue}{ppd}) { if ($printer->{DBENTRY}) { $printer->{DBENTRY} =~ /^[^\|]*\|[^\|]*\|(.*)$/; $driver = $1; } else { $driver = printer::get_descr_from_ppd($printer); if ($driver =~ /^[^\|]*\|[^\|]*$/) { # No driver info $driver = "CUPS/PPD"; } else { $driver =~ /^[^\|]*\|[^\|]*\|(.*)$/; $driver = $1; } } } } if ($driver) { $windowtitle .= ", $driver"; } } return 0 if !$in->ask_from ($windowtitle, _("Printer default settings You should make sure that the page size and the ink type (if available) are set correctly. Note that with a very high printout quality printing can get substantially slower."), \@widgets, complete => sub { my $i; for ($i = 0; $i <= $#{$printer->{ARGS}}; $i++) { if (($printer->{ARGS}[$i]{'type'} eq 'int') || ($printer->{ARGS}[$i]{'type'} eq 'float')) { unless (($printer->{ARGS}[$i]{'type'} ne 'int') || ($userinputs[$i] =~ /^[\-\+]?[0-9]+$/)) { $in->ask_warn('', _("Option %s must be an integer number!", $printer->{ARGS}[$i]{'comment'})); return (1, $i); }