summaryrefslogtreecommitdiffstats
path: root/perl-install/Xconfig/xfreeX.pm
blob: 7d830ddee3e618d6c1c1505f24ad0f3cdf9468ff (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
package Xconfig::xfreeX; # $Id$

use diagnostics;
use strict;

use Xconfig::parse;
use common;
use log;


sub empty_config {
    my ($class) = @_;
    my $raw_X = Xconfig::parse::read_XF86Config_from_string(our $default_header);
    bless $raw_X, $class;
}

sub read {
    my ($class, $o_file) = @_;
    my $file = $o_file || ($::prefix || '') . (bless {}, $class)->config_file;
    my $raw_X = Xconfig::parse::read_XF86Config($file);
    bless $raw_X, $class;
}
sub write {
    my ($raw_X, $o_file) = @_;
    my $file = $o_file || ($::prefix || '') . $raw_X->config_file;
    rename $file, "$file.old";
    Xconfig::parse::write_XF86Config($raw_X, $file);
}
sub prepare_write {
    my ($raw_X) = @_;
    join('', Xconfig::parse::prepare_write_XF86Config($raw_X));
}


my @monitor_fields = qw(VendorName ModelName HorizSync VertRefresh);
sub get_monitors {
    my ($raw_X) = @_;
    my @raw_monitors = $raw_X->get_monitor_sections;
    map { raw_export_section($_, [ 'Identifier', @monitor_fields ]) } @raw_monitors;
}
sub set_monitors {
    my ($raw_X, @monitors) = @_;
    my @raw_monitors = $raw_X->new_monitor_sections(int @monitors);
    mapn { 
	my ($raw_monitor, $monitor) = @_;
	raw_import_section($raw_monitor, $monitor, \@monitor_fields);
    } \@raw_monitors, \@monitors;
}
sub get_monitor {
    my ($raw_X) = @_;
    my @l = $raw_X->get_monitors;
    if (!@l) {
	$raw_X->new_monitor_sections(1);
	@l = $raw_X->get_monitors;
    }
    $l[0]
}

my @keyboard_fields = qw(XkbLayout XkbModel XkbDisable XkbOptions XkbCompat);
sub get_keyboard {
    my ($raw_X) = @_;
    my $raw_kbd = $raw_X->get_keyboard_section;
    raw_export_section($raw_kbd, \@keyboard_fields);
}
sub set_keyboard {
    my ($raw_X, $kbd) = @_;
    my $raw_kbd = eval { $raw_X->get_keyboard_section } || $raw_X->new_keyboard_section;
    raw_import_section($raw_kbd, $kbd);
    $raw_X->set_Option('keyboard', $raw_kbd, keys %$kbd);
}

#- example: { Protocol => 'IMPS/2', Device => '/dev/psaux', Emulate3Buttons => undef, Emulate3Timeout => 50, ZAxisMapping => [ '4 5', '6 7' ] }
my @mouse_fields = qw(Protocol Device ZAxisMapping Emulate3Buttons Emulate3Timeout); #-);
sub get_mice {
    my ($raw_X) = @_;
    my @raw_mice = $raw_X->get_mouse_sections;
    map { raw_export_section($_, \@mouse_fields) } @raw_mice;
}
sub set_mice {
    my ($raw_X, @mice) = @_;
    my @raw_mice = $raw_X->new_mouse_sections(int @mice);
    mapn { 
	my ($raw_mouse, $mouse) = @_;
	raw_import_section($raw_mouse, $mouse);
	$raw_X->set_Option('mouse', $raw_mouse, keys %$mouse);
    } \@raw_mice, \@mice;
}

sub get_devices {
    my ($raw_X) = @_;
    my @raw_devices = $raw_X->get_device_sections;
    map {
	my $raw_device = $_;
	my $device = raw_export_section($raw_device, [ 'Identifier', $raw_X->get_device_section_fields ]);
	$device->{Options} = raw_export_section($raw_device, [ grep { (deref_array($raw_device->{$_}))[0]->{Option} } keys %$raw_device ]);
	$device;
    } @raw_devices;
}
sub set_devices {
    my ($raw_X, @devices) = @_;
    my @raw_devices = $raw_X->new_device_sections(int @devices);
    mapn { 
	my ($raw_device, $device) = @_;
	my %Options  = %{$device->{Options} || {}};
	add2hash(\%Options, $device->{'Options_' . $raw_X->name});
	raw_import_section($raw_device, $device, [ $raw_X->get_device_section_fields ]);
	raw_import_section($raw_device, \%Options);
	$_->{Option} = 1 foreach map { deref_array($raw_device->{$_}) } keys %Options;
	$raw_device->{''} = [ { post_comment => $device->{raw_LINES} } ] if $device->{raw_LINES};
    } \@raw_devices, \@devices;
}

sub get_device {
    my ($raw_X) = @_;
    first(get_devices($raw_X));
}

sub get_screens {
    my ($raw_X) = @_;
    $raw_X->get_Sections('Screen');
}
sub get_default_screen {
    my ($raw_X) = @_;
    my @l = get_screens($raw_X);
    (find { $_->{Identifier} && val($_->{Identifier}) eq 'screen1' || 
	      $_->{Driver} && val($_->{Driver}) =~ /svga|accel/ } @l) || $l[0];
}
sub set_screens {
    my ($raw_X, @screens) = @_;
    my @raw_screens = $raw_X->new_screen_sections(int @screens);
    mapn { 
	my ($raw_screen, $screen) = @_;
	raw_import_section($raw_screen, $screen);
    } \@raw_screens, \@screens;
}

sub get_modules {
    my ($raw_X) = @_;
    my $raw_Module = $raw_X->get_Section('Module') or return;
    my $Module = raw_export_section($raw_Module, ['Load']);
    @{$Module->{Load} || []};
}
sub add_load_module {
    my ($raw_X, $module) = @_;
    my $raw_Module = $raw_X->get_Section('Module') || $raw_X->new_module_section;

    my %load_modules_comment = (
	dbe => 'Double-Buffering Extension',
	v4l => 'Video for Linux',
	dri => 'direct rendering',
	glx => '3D layer',
	'glx-3.so' => '3D layer',
    );
    my $comment = $load_modules_comment{$module};
    push @{$raw_Module->{Load}}, { val => $module,
				   comment_on_line => $comment && " # $comment",
				 } if !member($module, $raw_X->get_modules);
}
sub remove_load_module {
    my ($raw_X, $module) = @_;
    my $raw_Module = $raw_X->get_Section('Module') or return;
    if (my @l = grep { $_->{val} ne $module } @{$raw_Module->{Load}}) {
	$raw_Module->{Load} = \@l;
    } else {
	$raw_X->remove_Section('Module');
    }
}
sub set_load_module {
    my ($raw_X, $module, $bool) = @_;
    $bool ? add_load_module($raw_X, $module) : remove_load_module($raw_X, $module);
}

sub get_resolution {
    my ($raw_X, $o_Screen) = @_;
    my $Screen = $o_Screen || $raw_X->get_default_screen or return {};

    my $depth = val($Screen->{DefaultColorDepth});
    my $Display = find { !$depth || val($_->{l}{Depth}) eq $depth } @{$Screen->{Display} || []} or return {};
    $Display->{l}{Virtual} && val($Display->{l}{Virtual}) =~ /(\d+)\s+(\d+)/ or
      val($Display->{l}{Modes}) =~ /(\d+)x(\d+)/ or return {};
    { X => $1, Y => $2, Depth => val($Display->{l}{Depth}) };
}

#-##############################################################################
#- common to xfree3 and xfree4
#-##############################################################################
sub default_ModeLine { our $default_ModeLine }


sub get_device_sections {
    my ($raw_X) = @_;
    $raw_X->get_Sections('Device');
}
sub new_device_sections {
    my ($raw_X, $nb_new) = @_;
    $raw_X->remove_Section('Device');
    map { $raw_X->add_Section('Device', { Identifier => { val => "device$_" } }) } (1 .. $nb_new);
}

sub get_monitor_sections {
    my ($raw_X) = @_;
    $raw_X->get_Sections('Monitor');
}
sub new_monitor_sections {
    my ($raw_X, $nb_new) = @_;
    my $ModeLine = ModeLine_from_string(qq(Section "Monitor"\n) . $raw_X->default_ModeLine . qq(EndSection\n));
    $raw_X->remove_Section('Monitor');
    map { $raw_X->add_Section('Monitor', { Identifier => { val => "monitor$_" }, ModeLine => $ModeLine }) } (1 .. $nb_new);
}

sub new_screen_sections {
    my ($raw_X, $nb_new) = @_;
    $raw_X->remove_Section('Screen');
    map { $raw_X->add_Section('Screen', {}) } (1 .. $nb_new);
}

sub new_module_section {
    my ($raw_X) = @_;
    return $raw_X->add_Section('Module', {});
}


#-##############################################################################
#- helpers
#-##############################################################################
sub raw_export_section {
    my ($section, $fields) = @_;

    my $export_name = sub {
	my ($name) = @_;
	my $h = $section->{$name} or return;

	my @l = map { if_(!$_->{commented}, $_->{val}) } deref_array($h) or return;    
	$name => (ref($h) eq 'ARRAY' ? \@l : $l[0]);
    };

    my %h = map { $export_name->($_) } @$fields;
    \%h;
}

sub raw_import_section {
    my ($section, $h, $o_fields) = @_;
    foreach ($o_fields ? grep { exists $h->{$_} } @$o_fields : keys %$h) {
	my @l = map { ref($_) eq 'HASH' ? $_ : { val => $_ } } deref_array($h->{$_});
	$section->{$_} = (ref($h->{$_}) eq 'ARRAY' ? \@l : $l[0]);
    }
}

sub add_Section {
    my ($raw_X, $Section, $h) = @_;
    my @suggested_ordering = qw(Files ServerFlags Module DRI Keyboard Pointer XInput InputDevice Monitor Device Screen ServerLayout);
    my %order = map_index { { lc($_) => $::i } } @suggested_ordering;
    my $e = { name => $Section, l => $h };
    my $added;
    @$raw_X = map { 
	if ($order{lc $_->{name}} > $order{lc $Section} && !$added) {
	    $added = 1;
	    ($e, $_);
	} else { $_ }
    } @$raw_X;
    push @$raw_X, $e if !$added;
    $h;
}
sub remove_Section {
    my ($raw_X, $Section, $o_when) = @_;
    @$raw_X = grep { $_->{name} ne $Section || $o_when && $o_when->($_->{l}) } @$raw_X;
    $raw_X;
}
sub get_Sections {
    my ($raw_X, $Section, $o_when) = @_;
    map { if_($_->{name} eq $Section && (!$o_when || $o_when->($_->{l})), $_->{l}) } @$raw_X;
}
sub get_Section {
    my ($raw_X, $Section, $o_when) = @_;
    my @l = get_Sections($raw_X, $Section, $o_when);
    @l > 1 and log::l("Xconfig: found more than one Section $Section");
    $l[0];
}
sub get_Section_by_Identifier {
    my ($raw_X, $Section, $Identifier) = @_;
    my @l = get_Sections($raw_X, $Section, sub { val($_[0]{Identifier}) eq $Identifier });
    @l > 1 and die "more than one Section $Section has Identifier $Identifier";
    $l[0];
}

sub val {
    my ($ref) = @_;
    $ref && $ref->{val};
}

sub ModeLine_from_string {
    my ($s) = @_;
    my $raw_X_for_ModeLine = Xconfig::parse::read_XF86Config_from_string($s);
    get_Section($raw_X_for_ModeLine, 'Monitor')->{ModeLine};
}



our @resolutions = ('640x480', '800x600', '1024x768', if_(arch() =~ /ppc/, '1152x768'), '1152x864', '1280x960', '1280x1024', '1400x1050', '1600x1200', '1920x1440', '2048x1536');

our $default_header = <<'END';
# File generated by XFdrake.

# **********************************************************************
# Refer to the XF86Config man page for details about the format of
# this file.
# **********************************************************************

Section "Files"
    # Multiple FontPath entries are allowed (they are concatenated together)
    # By default, Mandrake 6.0 and later now use a font server independent of
    # the X server to render fonts.
    FontPath "unix/:-1"
EndSection

Section "ServerFlags"
    #DontZap # disable <Crtl><Alt><BS> (server abort)
    #DontZoom # disable <Crtl><Alt><KP_+>/<KP_-> (resolution switching)
    AllowMouseOpenFail # allows the server to start up even if the mouse doesn't work
EndSection
END

our $default_ModeLine = arch() =~ /ppc/ ? <<'END_PPC' : <<'END';
    # Apple iMac modes
    ModeLine "1024x768"   78.525 1024 1049 1145 1312   768  769  772  800 +hsync +vsync
    ModeLine "800x600"    62.357  800  821  901 1040   600  601  604  632 +hsync +vsync
    ModeLine "640x480"    49.886  640  661  725  832   480  481  484  514 +hsync +vsync
    # Apple monitors tend to do 832x624
    ModeLine "832x624"    57      832  876  940 1152   624  625  628  667 -hsync -vsync
    # Apple PowerBook G3
    ModeLine "800x600"    100     800  816  824  840   600  616  624  640 -hsync -vsync
    # Apple TI Powerbook 
    ModeLine "1152x768"   78.741 1152 1173 1269 1440   768  769  772  800 +vsync +vsync
    # Pismo Firewire G3   
    ModeLine "1024x768"   65     1024 1032 1176 1344   768  771  777  806 -hsync -vsync
    # iBook2
    ModeLine "1024x768"   65     1024 1048 1184 1344   768  771  777  806 -hsync -vsync
    # 17" Apple Studio Display
    ModeLine "1024x768"   112.62 1024 1076 1248 1420 768 768 780 808 +hsync +vsync
    # HiRes Apple Studio Display
    ModeLine "1280x1024"  135    1280 1288 1392 1664  1024 1027 1030 1064
    # Another variation
    ModeLine "1280x1024"  134.989 1280 1317 1429 1688  1024 1025 1028 1066 +hsync +vsync
END_PPC
    # Sony Vaio C1(X,XS,VE,VN)?
    # 1024x480 @ 85.6 Hz, 48 kHz hsync
    ModeLine "1024x480"    65.00 1024 1032 1176 1344   480  488  494  563 -hsync -vsync
    
    # TV fullscreen mode or DVD fullscreen output.
    # 768x576 @ 79 Hz, 50 kHz hsync
    ModeLine "768x576"     50.00  768  832  846 1000   576  590  595  630
    # 768x576 @ 100 Hz, 61.6 kHz hsync
    ModeLine "768x576"     63.07  768  800  960 1024   576  578  590  616
END

1;

erver.s_addr == 0) { log_message("resolvconf needs a dns server"); return -1; } f = fopen(filename, "w"); if (!f) { log_perror(filename); return -1; } if (domain) fprintf(f, "search %s\n", domain); /* we can live without the domain search (user will have to enter fully-qualified names) */ fprintf(f, "nameserver %s\n", inet_ntoa(dns_server)); if (dns_server2.s_addr != 0) fprintf(f, "nameserver %s\n", inet_ntoa(dns_server2)); fclose(f); res_init(); /* reinit the resolver so DNS changes take affect */ return 0; } static int save_netinfo(struct interface_info * intf) { char * file_network = "/tmp/network"; char file_intf[500]; FILE * f; f = fopen(file_network, "w"); if (!f) { log_perror(file_network); return -1; } fprintf(f, "NETWORKING=yes\n"); fprintf(f, "FORWARD_IPV4=false\n"); if (hostname && !intf->boot_proto == BOOTPROTO_DHCP) fprintf(f, "HOSTNAME=%s\n", hostname); if (gateway.s_addr != 0) fprintf(f, "GATEWAY=%s\n", inet_ntoa(gateway)); fclose(f); strcpy(file_intf, "/tmp/ifcfg-"); strcat(file_intf, intf->device); f = fopen(file_intf, "w"); if (!f) { log_perror(file_intf); return -1; } fprintf(f, "DEVICE=%s\n", intf->device); if (intf->boot_proto == BOOTPROTO_DHCP) { fprintf(f, "BOOTPROTO=dhcp\n"); if (dhcp_hostname && !streq(dhcp_hostname, "")) fprintf(f, "DHCP_HOSTNAME=%s\n", dhcp_hostname); } else if (intf->boot_proto == BOOTPROTO_STATIC) { fprintf(f, "BOOTPROTO=static\n"); fprintf(f, "IPADDR=%s\n", inet_ntoa(intf->ip)); fprintf(f, "NETMASK=%s\n", inet_ntoa(intf->netmask)); fprintf(f, "NETWORK=%s\n", inet_ntoa(intf->network)); fprintf(f, "BROADCAST=%s\n", inet_ntoa(intf->broadcast)); if (domain) fprintf(f, "DOMAIN=%s\n", domain); if (dns_server.s_addr != 0) fprintf(f, "DNS1=%s\n", inet_ntoa(dns_server)); if (dns_server2.s_addr != 0) fprintf(f, "DNS2=%s\n", inet_ntoa(dns_server2)); } else if (intf->boot_proto == BOOTPROTO_ADSL_PPPOE) { fprintf(f, "BOOTPROTO=adsl_pppoe\n"); fprintf(f, "USER=%s\n", intf->user); fprintf(f, "PASS=%s\n", intf->pass); fprintf(f, "ACNAME=%s\n", intf->acname); } fclose(f); return 0; } char * guess_netmask(char * ip_addr) { struct in_addr addr; unsigned long int tmp; if (streq(ip_addr, "") || !inet_aton(ip_addr, &addr)) return ""; log_message("guessing netmask"); tmp = ntohl(addr.s_addr); if (((tmp & 0xFF000000) >> 24) <= 127) return "255.0.0.0"; else if (((tmp & 0xFF000000) >> 24) <= 191) return "255.255.0.0"; else return "255.255.255.0"; } char * guess_domain_from_hostname(char *hostname) { char *domain = strchr(strdup(hostname), '.'); if (!domain || domain[1] == '\0') { log_message("unable to guess domain from hostname: %s", hostname); return NULL; } return domain + 1; /* skip '.' */ } static void static_ip_callback(char ** strings) { struct in_addr addr; static int done = 0; if (done) return; if (streq(strings[0], "") || !inet_aton(strings[0], &addr)) return; done = 1; if (!strcmp(strings[1], "")) { char * ptr; strings[1] = strdup(strings[0]); ptr = strrchr(strings[1], '.'); if (ptr) *(ptr+1) = '\0'; } if (!strcmp(strings[2], "")) strings[2] = strdup(strings[1]); if (!strcmp(strings[3], "")) strings[3] = strdup(guess_netmask(strings[0])); } static enum return_type setup_network_interface(struct interface_info * intf) { enum return_type results; char * bootprotos[] = { "Static", "DHCP", "ADSL", NULL }; char * bootprotos_auto[] = { "static", "dhcp", "adsl" }; char * choice; results = ask_from_list_auto("Please select your network connection type.", bootprotos, &choice, "network", bootprotos_auto); if (results != RETURN_OK) return results; if (!strcmp(choice, "Static")) { char * questions[] = { "IP of this machine", "IP of DNS", "IP of default gateway", "Netmask", NULL }; char * questions_auto[] = { "ip", "dns", "gateway", "netmask" }; static char ** answers = NULL; struct in_addr addr; results = ask_from_entries_auto("Please enter the network information. (leave netmask blank for Internet standard)", questions, &answers, 16, questions_auto, static_ip_callback); if (results != RETURN_OK) return setup_network_interface(intf); if (streq(answers[0], "") || !inet_aton(answers[0], &addr)) { stg1_error_message("Invalid IP address."); return setup_network_interface(intf); } memcpy(&intf->ip, &addr, sizeof(addr)); if (!inet_aton(answers[1], &dns_server)) { log_message("invalid DNS"); dns_server.s_addr = 0; /* keep an understandable state */ } if (streq(answers[0], answers[1])) { log_message("IP and DNS are the same, guess you don't want a DNS, disabling it"); dns_server.s_addr = 0; /* keep an understandable state */ } if (!inet_aton(answers[2], &gateway)) { log_message("invalid gateway"); gateway.s_addr = 0; /* keep an understandable state */ } if ((streq(answers[3], "") && inet_aton(guess_netmask(answers[0]), &addr)) || inet_aton(answers[3], &addr)) memcpy(&intf->netmask, &addr, sizeof(addr)); else { stg1_error_message("Invalid netmask."); return setup_network_interface(intf); } *((uint32_t *) &intf->broadcast) = (*((uint32_t *) &intf->ip) & *((uint32_t *) &intf->netmask)) | ~(*((uint32_t *) &intf->netmask)); inet_aton("255.255.255.255", &addr); if (!memcmp(&addr, &intf->netmask, sizeof(addr))) { log_message("netmask is 255.255.255.255 -> point to point device"); intf->network = gateway; intf->is_ptp = 1; } else { *((uint32_t *) &intf->network) = *((uint32_t *) &intf->ip) & *((uint32_t *) &intf->netmask); intf->is_ptp = 0; } intf->boot_proto = BOOTPROTO_STATIC; if (configure_net_device(intf)) return RETURN_ERROR; } else if (streq(choice, "DHCP")) { results = perform_dhcp(intf); if (results == RETURN_BACK) return setup_network_interface(intf); if (results == RETURN_ERROR) return results; intf->boot_proto = BOOTPROTO_DHCP; if (configure_net_device(intf)) return RETURN_ERROR; } else if (streq(choice, "ADSL")) { results = perform_adsl(intf); if (results == RETURN_BACK) return setup_network_interface(intf); if (results == RETURN_ERROR) return results; } else return RETURN_ERROR; return add_default_route(); } static enum return_type configure_network(struct interface_info * intf) { char * dnshostname; if (hostname && domain) return RETURN_OK; dnshostname = mygethostbyaddr(inet_ntoa(intf->ip)); if (dnshostname) { if (intf->boot_proto == BOOTPROTO_STATIC) hostname = strdup(dnshostname); domain = guess_domain_from_hostname(dnshostname); if (domain) { log_message("got hostname and domain from dns entry, %s and %s", dnshostname, domain); return RETURN_OK; } } else log_message("reverse name lookup on self failed"); if (domain) return RETURN_OK; dnshostname = NULL; if (dns_server.s_addr != 0) { wait_message("Trying to resolve dns..."); dnshostname = mygethostbyaddr(inet_ntoa(dns_server)); remove_wait_message(); if (dnshostname) { log_message("got DNS fullname, %s", dnshostname); domain = guess_domain_from_hostname(dnshostname); } else log_message("reverse name lookup on DNS failed"); } else log_message("no DNS, unable to guess domain"); if (domain) { log_message("got domain from DNS fullname, %s", domain); } else { enum return_type results; char * questions[] = { "Host name", "Domain name", NULL }; char * questions_auto[] = { "hostname", "domain" }; static char ** answers = NULL; char * boulet; if (dhcp_hostname || dhcp_domain) { answers = (char **) malloc(sizeof(questions)); answers[0] = strdup(dhcp_hostname); answers[1] = strdup(dhcp_domain); } if (!dhcp_hostname || !dhcp_hostname) { results = ask_from_entries_auto("I could not guess hostname and domain name; please fill in this information. " "Valid answers are for example: `mybox' for hostname and `mynetwork.com' for " "domain name, for a machine called `mybox.mynetwork.com' on the Internet.", questions, &answers, 32, questions_auto, NULL); if (results != RETURN_OK) return results; } hostname = answers[0]; if ((boulet = strchr(hostname, '.')) != NULL) boulet[0] = '\0'; domain = answers[1]; } log_message("using hostname %s", hostname); log_message("using domain %s", domain); return RETURN_OK; } static enum return_type bringup_networking(struct interface_info * intf) { static struct interface_info loopback; enum return_type results; my_insmod("af_packet", ANY_DRIVER_TYPE, NULL, 1); do { results = configure_wireless(intf->device); } while (results == RETURN_ERROR); if (results == RETURN_BACK) return RETURN_BACK; do { results = setup_network_interface(intf); if (results != RETURN_OK) return results; write_resolvconf(); results = configure_network(intf); } while (results == RETURN_ERROR); if (results == RETURN_BACK) return bringup_networking(intf); write_resolvconf(); /* maybe we have now domain to write also */ if (loopback.is_up == 0) { int rc; strcpy(loopback.device, "lo"); loopback.is_ptp = 0; loopback.is_up = 0; loopback.ip.s_addr = htonl(0x7f000001); loopback.netmask.s_addr = htonl(0xff000000); loopback.broadcast.s_addr = htonl(0x7fffffff); loopback.network.s_addr = htonl(0x7f000000); rc = configure_net_device(&loopback); if (rc) return RETURN_ERROR; } return RETURN_OK; } static char * auto_select_up_intf(int detection_mode) { #define SIOCETHTOOL 0x8946 #define ETHTOOL_GLINK 0x0000000a /* Get link status (ethtool_value) */ struct ethtool_value { uint32_t cmd; uint32_t data; }; char ** interfaces, ** ptr; interfaces = get_net_devices(); int s; s = socket(AF_INET, SOCK_DGRAM, 0); if (s < 0) { return NULL; } ptr = interfaces; while (ptr && *ptr) { if (detection_mode != AUTO_DETECTION_WIRED || !wireless_is_aware(s, *interfaces)) { struct ifreq ifr; struct ethtool_value edata; strncpy(ifr.ifr_name, *ptr, IFNAMSIZ); edata.cmd = ETHTOOL_GLINK; ifr.ifr_data = (caddr_t)&edata; if (ioctl(s, SIOCETHTOOL, &ifr) == 0 && edata.data) { close(s); log_message("NETWORK: choosing interface %s (link beat detected)", *ptr); return *ptr; } } ptr++; } log_message("NETWORK: no interface has a link beat"); if (detection_mode == AUTO_DETECTION_WIRED) { ptr = interfaces; while (ptr && *ptr) { if (!wireless_is_aware(s, *interfaces)) { close(s); log_message("NETWORK: choosing interface %s (wired interface)", *ptr); return *ptr; } ptr++; } log_message("NETWORK: no interface is wired"); } close(s); return NULL; } static char * interface_select(void) { char ** interfaces, ** ptr; char * descriptions[50]; char * choice; int i, count = 0; enum return_type results; interfaces = get_net_devices(); ptr = interfaces; while (ptr && *ptr) { count++; ptr++; } if (count == 0) { stg1_error_message("No NET device found.\n" "Hint: if you're using a Laptop, note that PCMCIA Network adapters are now supported either with `pcmcia.img' or `network.img', please try both these bootdisks."); i = ask_insmod(NETWORK_DEVICES); if (i == RETURN_BACK) return NULL; return interface_select(); } if (count == 1) return *interfaces; /* this can't be done in ask_from_list_comments_auto because "auto" and "wired" are not in the interfaces list */ if (IS_AUTOMATIC) { enum auto_detection_type auto_detect = AUTO_DETECTION_NONE; if (streq(get_auto_value("interface"), "auto")) auto_detect = AUTO_DETECTION_ALL; else if (streq(get_auto_value("interface"), "wired")) auto_detect = AUTO_DETECTION_WIRED; if (auto_detect != AUTO_DETECTION_NONE) { choice = auto_select_up_intf(auto_detect); if (choice) return choice; } } i = 0; while (interfaces[i]) { descriptions[i] = get_net_intf_description(interfaces[i]); i++; } results = ask_from_list_comments_auto("Please choose the NET device to use for the installation.", interfaces, descriptions, &choice, "interface", interfaces); if (results != RETURN_OK) return NULL; return choice; } static enum return_type get_http_proxy(char **http_proxy_host, char **http_proxy_port) { char *questions[] = { "HTTP proxy host", "HTTP proxy port", NULL }; char *questions_auto[] = { "proxy_host", "proxy_port", NULL }; static char ** answers = NULL; enum return_type results; results = ask_from_entries_auto("Please enter HTTP proxy host and port if you need it, else leave them blank or cancel.", questions, &answers, 40, questions_auto, NULL); if (results == RETURN_OK) { *http_proxy_host = answers[0]; *http_proxy_port = answers[1]; } else { *http_proxy_host = NULL; *http_proxy_port = NULL; } return results; } static int url_split(const char *url, const char *protocol, char **host, char **path) { char *protocol_sep, *host_sep; protocol_sep = strstr(url, "://"); if (!protocol_sep) { log_message("NETWORK: no protocol in \"%s\"", url); return -1; } if (strncmp(protocol, url, protocol_sep - url)) return -1; url = protocol_sep + 3; host_sep = strchr(url, '/'); if (!host_sep || host_sep == url) { log_message("NETWORK: no hostname in \"%s\"", url); return -1; } *host = strndup(url, host_sep - url); *path = strdup(host_sep); return 0; } #define MIRRORLIST_MAX_ITEMS 500 typedef char *mirrorlist_t[2][MIRRORLIST_MAX_ITEMS+1]; static enum return_type get_mirrorlist(mirrorlist_t mirrorlist, int start, char *version, const char *protocol, char *http_proxy_host, char *http_proxy_port) { int fd, size, line_pos = 0; char path[1024]; char line[1024]; char type[100] = DISTRIB_TYPE; int mirror_idx = start; int use_http_proxy = http_proxy_host && http_proxy_port && !streq(http_proxy_host, "") && !streq(http_proxy_port, ""); lowercase(type); snprintf(path, sizeof(path), "%s/%s.%s.%s.list", MIRRORLIST_PATH, type, version, ARCH); fd = http_download_file(MIRRORLIST_HOST, path, &size, use_http_proxy ? "http" : NULL, http_proxy_host, http_proxy_port); if (fd < 0) { log_message("HTTP: unable to get mirrors list from %s (%s)", MIRRORLIST_HOST, path); return RETURN_ERROR; } while (read(fd, line + line_pos, 1) > 0) { if (line[line_pos] == '\n') { char *url; line[line_pos] = '\0'; line_pos = 0; /* skip medium if it looks like an updates one */ if (strstr(line, ",type=updates,")) continue; url = strstr(line, ",url="); if (!url) continue; url += 5; if (url_split(url, protocol, &mirrorlist[0][mirror_idx], &mirrorlist[1][mirror_idx]) < 0) continue; mirror_idx++; } else { line_pos++; } if (mirror_idx >= MIRRORLIST_MAX_ITEMS) break; } close(fd); mirrorlist[0][mirror_idx] = NULL; mirrorlist[1][mirror_idx] = NULL; return RETURN_OK; } static int choose_mirror_from_host_list(mirrorlist_t mirrorlist, char **selected_host, char **filepath) { enum return_type results; int mirror_idx = 0; do { results = ask_from_list_index("Please select a mirror from the list below.", mirrorlist[0], NULL, &mirror_idx); if (results == RETURN_BACK) { return RETURN_ERROR; } else if (results == RETURN_OK) { if (mirror_idx == 0) { /* enter the mirror manually */ return RETURN_OK; } *selected_host = strdup(mirrorlist[0][mirror_idx]); *filepath = strdup(mirrorlist[1][mirror_idx]); return RETURN_OK; } } while (results == RETURN_ERROR); return RETURN_ERROR; } static int choose_mirror_from_list(char *http_proxy_host, char *http_proxy_port, const char *protocol, char **selected_host, char **filepath) { enum return_type results; char *versions[] = { "Specify the mirror manually", DISTRIB_VERSION, "cooker", NULL }; char *version = DISTRIB_VERSION; do { results = ask_from_list("Please select a medium from the list below.", versions, &version); if (results == RETURN_BACK) { return RETURN_BACK; } else if (results == RETURN_OK) { if (!strcmp(version, versions[0])) { /* enter the mirror manually */ return RETURN_OK; } else { /* a medium has been selected */ mirrorlist_t mirrorlist; mirrorlist[0][0] = "Specify the mirror manually"; mirrorlist[1][0] = NULL; results = get_mirrorlist(mirrorlist, 1, version, protocol, http_proxy_host, http_proxy_port); if (results == RETURN_ERROR) return RETURN_ERROR; results = choose_mirror_from_host_list(mirrorlist, selected_host, filepath); } } } while (results == RETURN_ERROR); return results; } /* -=-=-- */ enum return_type intf_select_and_up() { static struct interface_info intf[20]; static int num_interfaces = 0; struct interface_info * sel_intf = NULL; int i; enum return_type results; char * iface = interface_select(); if (iface == NULL) return RETURN_BACK; for (i = 0; i < num_interfaces ; i++) if (!strcmp(intf[i].device, iface)) sel_intf = &(intf[i]); if (sel_intf == NULL) { sel_intf = &(intf[num_interfaces]); strcpy(sel_intf->device, iface); sel_intf->is_up = 0; num_interfaces++; } results = bringup_networking(sel_intf); if (results == RETURN_OK) save_netinfo(sel_intf); return results; } enum return_type nfs_prepare(void) { char * questions[] = { "NFS server name", DISTRIB_NAME " directory", NULL }; char * questions_auto[] = { "server", "directory", NULL }; static char ** answers = NULL; char * nfsmount_location; enum return_type results = intf_select_and_up(NULL, NULL); if (results != RETURN_OK) return results; do { results = ask_from_entries_auto("Please enter the name or IP address of your NFS server, " "and the directory containing the " DISTRIB_NAME " Distribution.", questions, &answers, 40, questions_auto, NULL); if (results != RETURN_OK || streq(answers[0], "")) { unset_automatic(); /* we are in a fallback mode */ return nfs_prepare(); } nfsmount_location = malloc(strlen(answers[0]) + strlen(answers[1]) + 2); strcpy(nfsmount_location, answers[0]); strcat(nfsmount_location, ":"); strcat(nfsmount_location, answers[1]); if (my_mount(nfsmount_location, MEDIA_LOCATION, "nfs", 0) == -1) { stg1_error_message("I can't mount the directory from the NFS server."); results = RETURN_BACK; continue; } free(nfsmount_location); nfsmount_location = NULL; results = try_with_directory(MEDIA_LOCATION, "nfs", "nfs-iso"); if (results != RETURN_OK) umount(MEDIA_LOCATION); if (results == RETURN_ERROR) return RETURN_ERROR; } while (results == RETURN_BACK); return RETURN_OK; } enum return_type ftp_prepare(void) { char * questions[] = { "FTP server", DISTRIB_NAME " directory", "Login", "Password", NULL }; char * questions_auto[] = { "server", "directory", "user", "pass", NULL }; static char ** answers = NULL; enum return_type results; struct utsname kernel_uname; char *http_proxy_host, *http_proxy_port; int use_http_proxy; if (!ramdisk_possible()) { stg1_error_message("FTP install needs more than %d Mbytes of memory (detected %d Mbytes). You may want to try an NFS install.", MEM_LIMIT_DRAKX, total_memory()); return RETURN_ERROR; } results = intf_select_and_up(); if (results != RETURN_OK) return results; get_http_proxy(&http_proxy_host, &http_proxy_port); use_http_proxy = http_proxy_host && http_proxy_port && !streq(http_proxy_host, "") && !streq(http_proxy_port, ""); uname(&kernel_uname); do { char location_full[500]; int ftp_serv_response = -1; int fd, size; char ftp_hostname[500]; if (!IS_AUTOMATIC) { if (answers == NULL) answers = (char **) malloc(sizeof(questions)); results = choose_mirror_from_list(http_proxy_host, http_proxy_port, "ftp", &answers[0], &answers[1]); if (results == RETURN_BACK) return ftp_prepare(); if (use_http_proxy) { results = ask_yes_no("Do you want to use this HTTP proxy for FTP connections too ?"); if (results == RETURN_BACK) return ftp_prepare(); use_http_proxy = results == RETURN_OK; } } results = ask_from_entries_auto("Please enter the name or IP address of the FTP server, " "the directory containing the " DISTRIB_NAME " Distribution, " "and the login/pass if necessary (leave login blank for anonymous). ", questions, &answers, 40, questions_auto, NULL); if (results != RETURN_OK || streq(answers[0], "")) { unset_automatic(); /* we are in a fallback mode */ return ftp_prepare(); } strcpy(location_full, answers[1][0] == '/' ? "" : "/"); strcat(location_full, answers[1]); if (use_http_proxy) { log_message("FTP: don't connect to %s directly, will use proxy", answers[0]); } else { log_message("FTP: trying to connect to %s", answers[0]); ftp_serv_response = ftp_open_connection(answers[0], answers[2], answers[3], ""); if (ftp_serv_response < 0) { log_message("FTP: error connect %d", ftp_serv_response); if (ftp_serv_response == FTPERR_BAD_HOSTNAME) stg1_error_message("Error: bad hostname."); else if (ftp_serv_response == FTPERR_FAILED_CONNECT) stg1_error_message("Error: failed to connect to remote host."); else stg1_error_message("Error: couldn't connect."); results = RETURN_BACK; continue; } } strcat(location_full, COMPRESSED_FILE_REL("/")); log_message("FTP: trying to retrieve %s", location_full); if (use_http_proxy) { if (strcmp(answers[2], "")) { strcpy(ftp_hostname, answers[2]); /* user name */ strcat(ftp_hostname, ":"); strcat(ftp_hostname, answers[3]); /* password */ strcat(ftp_hostname, "@"); } else { strcpy(ftp_hostname, ""); } strcat(ftp_hostname, answers[0]); fd = http_download_file(ftp_hostname, location_full, &size, "ftp", http_proxy_host, http_proxy_port); } else { fd = ftp_start_download(ftp_serv_response, location_full, &size); } if (fd < 0) { char *msg = str_ftp_error(fd); log_message("FTP: error get %d for remote file %s", fd, location_full); stg1_error_message("Error: %s.", msg ? msg : "couldn't retrieve Installation program"); results = RETURN_BACK; continue; } log_message("FTP: size of download %d bytes", size); results = load_compressed_fd(fd, size); if (results == RETURN_OK) { if (!use_http_proxy) ftp_end_data_command(ftp_serv_response); } else { unset_automatic(); /* we are in a fallback mode */ return results; } if (use_http_proxy) { add_to_env("METHOD", "http"); sprintf(location_full, "ftp://%s%s", ftp_hostname, answers[1]); add_to_env("URLPREFIX", location_full); add_to_env("PROXY", http_proxy_host); add_to_env("PROXYPORT", http_proxy_port); } else { add_to_env("METHOD", "ftp"); add_to_env("HOST", answers[0]); add_to_env("PREFIX", answers[1]); if (!streq(answers[2], "")) { add_to_env("LOGIN", answers[2]); add_to_env("PASSWORD", answers[3]); } } } while (results == RETURN_BACK); return RETURN_OK; } enum return_type http_prepare(void) { char * questions[] = { "HTTP server", DISTRIB_NAME " directory", NULL }; char * questions_auto[] = { "server", "directory", NULL }; static char ** answers = NULL; enum return_type results; char *http_proxy_host, *http_proxy_port; if (!ramdisk_possible()) { stg1_error_message("HTTP install needs more than %d Mbytes of memory (detected %d Mbytes). You may want to try an NFS install.", MEM_LIMIT_DRAKX, total_memory()); return RETURN_ERROR; } results = intf_select_and_up(); if (results != RETURN_OK) return results; get_http_proxy(&http_proxy_host, &http_proxy_port); do { char location_full[500]; int fd, size; int use_http_proxy; if (!IS_AUTOMATIC) { if (answers == NULL) answers = (char **) malloc(sizeof(questions)); results = choose_mirror_from_list(http_proxy_host, http_proxy_port, "http", &answers[0], &answers[1]); if (results == RETURN_BACK) return ftp_prepare(); } results = ask_from_entries_auto("Please enter the name or IP address of the HTTP server, " "and the directory containing the " DISTRIB_NAME " Distribution.", questions, &answers, 40, questions_auto, NULL); if (results != RETURN_OK || streq(answers[0], "")) { unset_automatic(); /* we are in a fallback mode */ return http_prepare(); } strcpy(location_full, answers[1][0] == '/' ? "" : "/"); strcat(location_full, answers[1]); strcat(location_full, COMPRESSED_FILE_REL("/")); log_message("HTTP: trying to retrieve %s from %s", location_full, answers[0]); use_http_proxy = http_proxy_host && http_proxy_port && !streq(http_proxy_host, "") && !streq(http_proxy_port, ""); fd = http_download_file(answers[0], location_full, &size, use_http_proxy ? "http" : NULL, http_proxy_host, http_proxy_port); if (fd < 0) { log_message("HTTP: error %d", fd); if (fd == FTPERR_FAILED_CONNECT) stg1_error_message("Error: couldn't connect to server."); else stg1_error_message("Error: couldn't get file (%s).", location_full); results = RETURN_BACK; continue; } log_message("HTTP: size of download %d bytes", size); if (load_compressed_fd(fd, size) != RETURN_OK) { unset_automatic(); /* we are in a fallback mode */ return RETURN_ERROR; } add_to_env("METHOD", "http"); sprintf(location_full, "http://%s%s%s", answers[0], answers[1][0] == '/' ? "" : "/", answers[1]); add_to_env("URLPREFIX", location_full); if (!streq(http_proxy_host, "")) add_to_env("PROXY", http_proxy_host); if (!streq(http_proxy_port, "")) add_to_env("PROXYPORT", http_proxy_port); } while (results == RETURN_BACK); return RETURN_OK; } #ifndef DISABLE_KA enum return_type ka_prepare(void) { enum return_type results; if (!ramdisk_possible()) { stg1_error_message("KA install needs more than %d Mbytes of memory (detected %d Mbytes).", MEM_LIMIT_DRAKX, total_memory()); return RETURN_ERROR; } results = intf_select_and_up(); if (results != RETURN_OK) return results; return perform_ka(); } #endif