aboutsummaryrefslogtreecommitdiffstats
path: root/tests/test_framework/phpbb_test_case_helpers.php
blob: a29b6e1955b1345e5f699b3283e60289d2f8c892 (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
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/

class phpbb_test_case_helpers
{
	protected $expectedTriggerError = false;

	protected $test_case;

	public function __construct($test_case)
	{
		$this->test_case = $test_case;
	}

	/**
	* This should only be called once before the tests are run.
	* This is used to copy the fixtures to the phpBB install
	*/
	public function copy_ext_fixtures($fixtures_dir, $fixtures)
	{
		global $phpbb_root_path;

		if (file_exists($phpbb_root_path . 'ext/'))
		{
			// First, move any extensions setup on the board to a temp directory
			$this->copy_dir($phpbb_root_path . 'ext/', $phpbb_root_path . 'store/temp_ext/');

			// Then empty the ext/ directory on the board (for accurate test cases)
			$this->empty_dir($phpbb_root_path . 'ext/');
		}

		// Copy our ext/ files from the test case to the board
		foreach ($fixtures as $fixture)
		{
			$this->copy_dir($fixtures_dir . $fixture, $phpbb_root_path . 'ext/' . $fixture);
		}
	}

	/**
	* This should only be called once after the tests are run.
	* This is used to remove the fixtures from the phpBB install
	*/
	public function restore_original_ext_dir()
	{
		global $phpbb_root_path;

		// Remove all of the files we copied from test ext -> board ext
		$this->empty_dir($phpbb_root_path . 'ext/');

		// Copy back the board installed extensions from the temp directory
		if (file_exists($phpbb_root_path . 'store/temp_ext/'))
		{
			$this->copy_dir($phpbb_root_path . 'store/temp_ext/', $phpbb_root_path . 'ext/');

			// Remove all of the files we copied from board ext -> temp_ext
			$this->empty_dir($phpbb_root_path . 'store/temp_ext/');
		}

		if (file_exists($phpbb_root_path . 'store/temp_ext/'))
		{
			$this->empty_dir($phpbb_root_path . 'store/temp_ext/');
		}
	}

	public function setExpectedTriggerError($errno, $message = '')
	{
		$exceptionName = '';
		switch ($errno)
		{
			case E_NOTICE:
			case E_STRICT:
				PHPUnit_Framework_Error_Notice::$enabled = true;
				$exceptionName = 'PHPUnit_Framework_Error_Notice';
			break;

			case E_WARNING:
				PHPUnit_Framework_Error_Warning::$enabled = true;
				$exceptionName = 'PHPUnit_Framework_Error_Warning';
			break;

			default:
				$exceptionName = 'PHPUnit_Framework_Error';
			break;
		}
		$this->expectedTriggerError = true;
		$this->test_case->setExpectedException($exceptionName, (string) $message, $errno);
	}

	public function makedirs($path)
	{
		// PHP bug #55124 (fixed in 5.4.0)
		$path = str_replace('/./', '/', $path);

		mkdir($path, 0777, true);
	}

	static public function get_test_config()
	{
		$config = array();


		if (extension_loaded('sqlite3'))
		{
			$config = array_merge($config, array(
				'dbms'		=> 'phpbb\db\driver\sqlite3',
				'dbhost'	=> dirname(__FILE__) . '/../phpbb_unit_tests.sqlite3', // filename
				'dbport'	=> '',
				'dbname'	=> '',
				'dbuser'	=> '',
				'dbpasswd'	=> '',
			));
		}
		else if (extension_loaded('sqlite'))
		{
			$config = array_merge($config, array(
				'dbms'		=> 'phpbb\db\driver\sqlite',
				'dbhost'	=> dirname(__FILE__) . '/../phpbb_unit_tests.sqlite2', // filename
				'dbport'	=> '',
				'dbname'	=> '',
				'dbuser'	=> '',
				'dbpasswd'	=> '',
			));
		}

		if (isset($_SERVER['PHPBB_TEST_CONFIG']))
		{
			// Could be an absolute path
			$test_config = $_SERVER['PHPBB_TEST_CONFIG'];
		}
		else
		{
			$test_config = dirname(__FILE__) . '/../test_config.php';
		}

		if (file_exists($test_config))
		{
			include($test_config);

			if (!function_exists('phpbb_convert_30_dbms_to_31'))
			{
				require_once dirname(__FILE__) . '/../../phpBB/includes/functions.php';
			}

			$config = array_merge($config, array(
				'dbms'		=> phpbb_convert_30_dbms_to_31($dbms),
				'dbhost'	=> $dbhost,
				'dbport'	=> $dbport,
				'dbname'	=> $dbname,
				'dbuser'	=> $dbuser,
				'dbpasswd'	=> $dbpasswd,
				'custom_dsn'	=> isset($custom_dsn) ? $custom_dsn : '',
			));

			if (isset($phpbb_functional_url))
			{
				$config['phpbb_functional_url'] = $phpbb_functional_url;
			}

			if (isset($phpbb_redis_host))
			{
				$config['redis_host'] = $phpbb_redis_host;
			}
			if (isset($phpbb_redis_port))
			{
				$config['redis_port'] = $phpbb_redis_port;
			}

			if (isset($fulltext_sphinx_id))
			{
				$config['fulltext_sphinx_id'] = $fulltext_sphinx_id;
			}
		}

		if (isset($_SERVER['PHPBB_TEST_DBMS']))
		{
			if (!function_exists('phpbb_convert_30_dbms_to_31'))
			{
				require_once dirname(__FILE__) . '/../../phpBB/includes/functions.php';
			}

			$config = array_merge($config, array(
				'dbms'		=> isset($_SERVER['PHPBB_TEST_DBMS']) ? phpbb_convert_30_dbms_to_31($_SERVER['PHPBB_TEST_DBMS']) : '',
				'dbhost'	=> isset($_SERVER['PHPBB_TEST_DBHOST']) ? $_SERVER['PHPBB_TEST_DBHOST'] : '',
				'dbport'	=> isset($_SERVER['PHPBB_TEST_DBPORT']) ? $_SERVER['PHPBB_TEST_DBPORT'] : '',
				'dbname'	=> isset($_SERVER['PHPBB_TEST_DBNAME']) ? $_SERVER['PHPBB_TEST_DBNAME'] : '',
				'dbuser'	=> isset($_SERVER['PHPBB_TEST_DBUSER']) ? $_SERVER['PHPBB_TEST_DBUSER'] : '',
				'dbpasswd'	=> isset($_SERVER['PHPBB_TEST_DBPASSWD']) ? $_SERVER['PHPBB_TEST_DBPASSWD'] : '',
				'custom_dsn'	=> isset($_SERVER['PHPBB_TEST_CUSTOM_DSN']) ? $_SERVER['PHPBB_TEST_CUSTOM_DSN'] : '',
			));
		}

		if (isset($_SERVER['PHPBB_FUNCTIONAL_URL']))
		{
			$config = array_merge($config, array(
				'phpbb_functional_url'	=> isset($_SERVER['PHPBB_FUNCTIONAL_URL']) ? $_SERVER['PHPBB_FUNCTIONAL_URL'] : '',
			));
		}

		if (isset($_SERVER['PHPBB_TEST_REDIS_HOST']))
		{
			$config['redis_host'] = $_SERVER['PHPBB_TEST_REDIS_HOST'];
		}

		if (isset($_SERVER['PHPBB_TEST_REDIS_PORT']))
		{
			$config['redis_port'] = $_SERVER['PHPBB_TEST_REDIS_PORT'];
		}

		return $config;
	}

	/**
	* Recursive directory copying function
	*
	* @param string $source
	* @param string $dest
	* @return array list of files copied
	*/
	public function copy_dir($source, $dest)
	{
		$source = (substr($source, -1) == '/') ? $source : $source . '/';
		$dest = (substr($dest, -1) == '/') ? $dest : $dest . '/';

		$copied_files = array();

		if (!is_dir($dest))
		{
			$this->makedirs($dest);
		}

		$files = scandir($source);
		foreach ($files as $file)
		{
			if ($file == '.' || $file == '..')
			{
				continue;
			}

			if (is_dir($source . $file))
			{
				$created_dir = false;
				if (!is_dir($dest . $file))
				{
					$created_dir = true;
					$this->makedirs($dest . $file);
				}

				$copied_files = array_merge($copied_files, self::copy_dir($source . $file, $dest . $file));

				if ($created_dir)
				{
					$copied_files[] = $dest . $file;
				}
			}
			else
			{
				if (!file_exists($dest . $file))
				{
					copy($source . $file, $dest . $file);

					$copied_files[] = $dest . $file;
				}
			}
		}

		return $copied_files;
	}

	/**
	* Empty directory (remove any subdirectories/files below)
	*
	* @param array $file_list
	*/
	public function empty_dir($path)
	{
		$path = (substr($path, -1) == '/') ? $path : $path . '/';

		$files = scandir($path);
		foreach ($files as $file)
		{
			if ($file == '.' || $file == '..')
			{
				continue;
			}

			if (is_dir($path . $file))
			{
				$this->empty_dir($path . $file);

				rmdir($path . $file);
			}
			else
			{
				unlink($path . $file);
			}
		}
	}
}
/span> memcpy(&ia, &intf->broadcast, sizeof(intf->broadcast)); strcpy(bc, inet_ntoa(ia)); memcpy(&ia, &intf->network, sizeof(intf->network)); strcpy(nw, inet_ntoa(ia)); log_message("configuring device %s ip: %s nm: %s nw: %s bc: %s", intf->device, ip, nm, nw, bc); if (IS_TESTING) return 0; s = socket(AF_INET, SOCK_DGRAM, 0); if (s < 0) { log_perror("socket"); error_message_net(); return 1; } strcpy(req.ifr_name, intf->device); if (intf->is_up == 1) { log_message("interface already up, downing before reconfigure"); req.ifr_flags = 0; if (ioctl(s, SIOCSIFFLAGS, &req)) { close(s); log_perror("SIOCSIFFLAGS (downing)"); error_message_net(); return 1; } } /* sets IP address */ addr.sin_port = 0; memcpy(&addr.sin_addr, &intf->ip, sizeof(intf->ip)); memcpy(&req.ifr_addr, &addr, sizeof(addr)); if (ioctl(s, SIOCSIFADDR, &req)) { close(s); log_perror("SIOCSIFADDR"); error_message_net(); return 1; } /* sets broadcast */ memcpy(&addr.sin_addr, &intf->broadcast, sizeof(intf->broadcast)); memcpy(&req.ifr_broadaddr, &addr, sizeof(addr)); if (ioctl(s, SIOCSIFBRDADDR, &req)) { close(s); log_perror("SIOCSIFBRDADDR"); error_message_net(); return 1; } /* sets netmask */ memcpy(&addr.sin_addr, &intf->netmask, sizeof(intf->netmask)); memcpy(&req.ifr_netmask, &addr, sizeof(addr)); if (ioctl(s, SIOCSIFNETMASK, &req)) { close(s); log_perror("SIOCSIFNETMASK"); error_message_net(); return 1; } if (intf->is_ptp) req.ifr_flags = IFF_UP | IFF_RUNNING | IFF_POINTOPOINT | IFF_NOARP; else req.ifr_flags = IFF_UP | IFF_RUNNING | IFF_BROADCAST; /* brings up networking! */ if (ioctl(s, SIOCSIFFLAGS, &req)) { close(s); log_perror("SIOCSIFFLAGS (upping)"); error_message_net(); return 1; } memset(&route, 0, sizeof(route)); route.rt_dev = intf->device; route.rt_flags = RTF_UP; memcpy(&addr.sin_addr, &intf->network, sizeof(intf->network)); memcpy(&route.rt_dst, &addr, sizeof(addr)); memcpy(&addr.sin_addr, &intf->netmask, sizeof(intf->netmask)); memcpy(&route.rt_genmask, &addr, sizeof(addr)); /* adds route */ if (ioctl(s, SIOCADDRT, &route)) { close(s); log_perror("SIOCADDRT"); error_message_net(); return 1; } close(s); intf->is_up = 1; if (intf->boot_proto != BOOTPROTO_DHCP && !streq(intf->device, "lo")) { /* I need to sleep a bit in order for kernel to finish init of the network device; if not, first sendto() for gethostbyaddr will get an EINVAL. */ wait_message("Bringing up networking..."); sleep(2); remove_wait_message(); } return 0; } /* host network informations */ char * hostname = NULL; char * domain = NULL; struct in_addr gateway = { 0 }; struct in_addr dns_server = { 0 }; struct in_addr dns_server2 = { 0 }; static int add_default_route(void) { int s; struct rtentry route; struct sockaddr_in addr; if (IS_TESTING) return 0; if (gateway.s_addr == 0) { log_message("no gateway provided, can't add default route"); return 0; } s = socket(AF_INET, SOCK_DGRAM, 0); if (s < 0) { close(s); log_perror("socket"); error_message_net(); return 1; } memset(&route, 0, sizeof(route)); addr.sin_family = AF_INET; addr.sin_port = 0; addr.sin_addr = gateway; memcpy(&route.rt_gateway, &addr, sizeof(addr)); addr.sin_addr.s_addr = INADDR_ANY; memcpy(&route.rt_dst, &addr, sizeof(addr)); memcpy(&route.rt_genmask, &addr, sizeof(addr)); route.rt_flags = RTF_UP | RTF_GATEWAY; route.rt_metric = 0; if (ioctl(s, SIOCADDRT, &route)) { close(s); log_perror("SIOCADDRT"); error_message_net(); return 1; } close(s); return 0; } static int write_resolvconf(void) { char * filename = "/etc/resolv.conf"; FILE * f; if (dns_server.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 (domain) fprintf(f, "DOMAINNAME=%s\n", domain); if (dhcp_hostname && !streq(dhcp_hostname, "")) fprintf(f, "DHCP_HOSTNAME=%s\n", dhcp_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"); 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)); } 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); } if (!streq(intf->http_proxy_host, "") && !streq(intf->http_proxy_port, "")) { fprintf(f, "PROXY=%s\n", intf->http_proxy_host); fprintf(f, "PROXYPORT=%s\n", intf->http_proxy_port); } 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"; } 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 choose the desired IP attribution.", 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 = strchr(strdup(dnshostname), '.') + 1; log_message("got hostname and domain from dns entry, %s and %s", dnshostname, domain); return RETURN_OK; } log_message("reverse name lookup on self failed"); if (domain) return RETURN_OK; if (dns_server.s_addr != 0) { wait_message("Trying to resolve dns..."); dnshostname = mygethostbyaddr(inet_ntoa(dns_server)); remove_wait_message(); } if (dnshostname) { domain = strchr(strdup(dnshostname), '.') + 1; 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; log_message("reverse name lookup on DNS failed"); 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]; } return RETURN_OK; } static enum return_type bringup_networking(struct interface_info * intf) { static struct interface_info loopback; enum return_type results = RETURN_ERROR; my_insmod("af_packet", ANY_DRIVER_TYPE, NULL, 1); while (results != RETURN_OK) { results = setup_network_interface(intf); if (results != RETURN_OK) return results; write_resolvconf(); results = configure_network(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 * 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; 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 intf_get_http_proxy(struct interface_info * intf) { 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) { intf->http_proxy_host = strdup(answers[0]); intf->http_proxy_port = strdup(answers[1]); } return results; } static int mirrorlist_entry_split(const char *entry, char *mirror[4]) /* mirror = { medium, protocol, host, path } */ { char *medium_sep, *protocol_sep, *host_sep, *path_sep; medium_sep = strchr(entry, ':'); if (!medium_sep || medium_sep == entry) { log_message("NETWORK: no medium in \"%s\"", entry); return -1; } mirror[0] = strndup(entry, medium_sep - entry); entry = medium_sep + 1; protocol_sep = strstr(entry, "://"); if (!protocol_sep || protocol_sep == entry) { log_message("NETWORK: no protocol in \"%s\"", entry); return -1; } mirror[1] = strndup(entry, protocol_sep - entry); entry = protocol_sep + 3; host_sep = strchr(entry, '/'); if (!host_sep || host_sep == entry) { log_message("NETWORK: no hostname in \"%s\"", entry); return -1; } mirror[2] = strndup(entry, host_sep - entry); entry = host_sep; path_sep = strstr(entry, "/Mandrake/RPMS"); if (!path_sep || path_sep == entry) { log_message("NETWORK: this path isn't valid : \"%s\"", entry); return -1; } mirror[3] = strndup(entry, path_sep - entry); return 0; } static int choose_mirror_from_host_list(char *mirrorlist[][4], const char *protocol, char *medium, char **selected_host, char **filepath) { enum return_type results; char *hostlist[MIRRORLIST_MAX_ITEMS+1] = { "Specify the mirror manually", "-----" }; int hostlist_index = 2, mirrorlist_index; /* select hosts matching medium and protocol */ for (mirrorlist_index = 0; mirrorlist[mirrorlist_index][0]; mirrorlist_index++) { if (!strcmp(mirrorlist[mirrorlist_index][0], medium) && !strcmp(mirrorlist[mirrorlist_index][1], protocol)) { hostlist[hostlist_index] = mirrorlist[mirrorlist_index][2]; hostlist_index++; if (hostlist_index == MIRRORLIST_MAX_ITEMS) break; } } hostlist[hostlist_index] = NULL; do { results = ask_from_list("Please select a mirror from the list below.", hostlist, selected_host); if (results == RETURN_BACK) { return RETURN_ERROR; } else if (results == RETURN_OK) { if (!strcmp(*selected_host, hostlist[0])) { /* enter the mirror manually */ return RETURN_OK; } else if (!strcmp(*selected_host, hostlist[1])) { /* the separator has been selected */ results = RETURN_ERROR; continue; } } /* select the path according to medium, protocol and host */ for (mirrorlist_index = 0; mirrorlist[mirrorlist_index][0]; mirrorlist_index++) { if (!strcmp(mirrorlist[mirrorlist_index][0], medium) && !strcmp(mirrorlist[mirrorlist_index][1], protocol) && !strcmp(mirrorlist[mirrorlist_index][2], *selected_host)) { *filepath = mirrorlist[mirrorlist_index][3]; return RETURN_OK; } } stg1_info_message("Unable to find the path for this mirror, please select another one"); results = RETURN_ERROR; } 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 *mirrorlist[MIRRORLIST_MAX_ITEMS+1][4]; int mirrorlist_number = 0; char *medialist[MIRRORLIST_MAX_MEDIA+1] = { "Specify the mirror manually", "-----" }; int media_number = 2; char *selected_medium; int fd, size, line_pos = 0; char line[500]; int use_http_proxy = !streq(http_proxy_host, "") && !streq(http_proxy_port, ""); fd = http_download_file(MIRRORLIST_HOST, MIRRORLIST_PATH, &size, use_http_proxy ? "http" : NULL, http_proxy_host, http_proxy_port); if (fd < 0) { log_message("HTTP: unable to get mirrors list"); return RETURN_ERROR; } while (read(fd, line + line_pos, 1) > 0) { if (line[line_pos] == '\n') { line[line_pos] = '\0'; line_pos = 0; /* skip medium if it looks like an updates one */ if (strstr(line, "updates")) continue; if (mirrorlist_entry_split(line, mirrorlist[mirrorlist_number]) < 0) continue; /* add medium in media list if different from previous one */ if (media_number == 2 || strcmp(mirrorlist[mirrorlist_number][0], medialist[media_number-1])) { medialist[media_number] = mirrorlist[mirrorlist_number][0]; media_number++; } mirrorlist_number++; } else { line_pos++; } if (mirrorlist_number >= MIRRORLIST_MAX_ITEMS || media_number >= MIRRORLIST_MAX_MEDIA) break; } close(fd); mirrorlist[mirrorlist_number][0] = NULL; medialist[media_number] = NULL; do { results = ask_from_list("Please select a medium from the list below.", medialist, &selected_medium); if (results == RETURN_BACK) { return RETURN_ERROR; } else if (results == RETURN_OK) { if (!strcmp(selected_medium, medialist[0])) { /* enter the mirror manually */ return RETURN_OK; } else if (!strcmp(selected_medium, medialist[1])) { /* the separator has been selected */ results = RETURN_ERROR; continue; } else { /* a medium has been selected */ results = choose_mirror_from_host_list(mirrorlist, protocol, selected_medium, selected_host, filepath); } } } while (results == RETURN_ERROR); return results; } /* -=-=-- */ static enum return_type intf_select_and_up(char **http_proxy_host, char **http_proxy_port) { 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++; } sel_intf->http_proxy_host = ""; sel_intf->http_proxy_port = ""; results = bringup_networking(sel_intf); if (results == RETURN_OK) { if (http_proxy_host && http_proxy_port) { results = intf_get_http_proxy(sel_intf); if (results != RETURN_OK) return results; *http_proxy_host = sel_intf->http_proxy_host; *http_proxy_port = sel_intf->http_proxy_port; } 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 * nfs_own_mount = SLASH_LOCATION "/tmp/nfsimage"; 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_param(MODE_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, nfs_own_mount, "nfs", 0) == -1) { stg1_error_message("I can't mount the directory from the NFS server."); results = RETURN_BACK; continue; } results = try_with_directory(nfs_own_mount, "nfs", "nfs-iso"); if (results == RETURN_BACK) umount(nfs_own_mount); } 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; char modules_cz[500]; struct utsname kernel_uname; char *http_proxy_host = "", *http_proxy_port = ""; 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_RAMDISK, total_memory()); return RETURN_ERROR; } results = intf_select_and_up(&http_proxy_host, &http_proxy_port); if (results != RETURN_OK) return results; uname(&kernel_uname); do { char location_full[500]; int ftp_serv_response = -1; int fd, size; int use_http_proxy; 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_OK) { unset_param(MODE_AUTOMATIC); /* we are in a fallback mode */ return ftp_prepare(); } } 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_param(MODE_AUTOMATIC); /* we are in a fallback mode */ return ftp_prepare(); } use_http_proxy = !streq(http_proxy_host, "") && !streq(http_proxy_port, ""); 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; } strcpy(modules_cz, answers[1]);