You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

336 lines
11 KiB

  1. /*
  2. netcfg.c - Configure a network via DHCP or manual configuration
  3. for debian-installer
  4. Copyright (C) 2000-2002 David Kimdon <dwhedon@debian.org>
  5. and others (see debian/copyright)
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include <string.h>
  19. #include <stdlib.h>
  20. #include <stdio.h>
  21. #include <unistd.h>
  22. #include <sys/types.h>
  23. #include <cdebconf/debconfclient.h>
  24. #include <debian-installer.h>
  25. #ifdef WIRELESS
  26. #include <iwlib.h>
  27. #endif
  28. #include "netcfg.h"
  29. enum wpa_t wpa_supplicant_status;
  30. static method_t netcfg_method = DHCP;
  31. response_t netcfg_get_method(struct debconfclient *client)
  32. {
  33. int iret, ret;
  34. iret = debconf_input(client, "medium", "netcfg/use_dhcp");
  35. ret = debconf_go(client);
  36. if (ret == 30)
  37. return GO_BACK;
  38. debconf_get(client, "netcfg/use_dhcp");
  39. if (strcmp(client->value, "true") == 0)
  40. netcfg_method = DHCP;
  41. else
  42. netcfg_method = STATIC;
  43. if (iret == 30)
  44. return NOT_ASKED;
  45. return 0;
  46. }
  47. int main(int argc, char *argv[])
  48. {
  49. int num_interfaces = 0;
  50. enum { BACKUP,
  51. GET_INTERFACE,
  52. GET_HOSTNAME_ONLY,
  53. GET_METHOD,
  54. GET_DHCP,
  55. GET_STATIC,
  56. WCONFIG,
  57. WCONFIG_ESSID,
  58. WCONFIG_SECURITY_TYPE,
  59. WCONFIG_WEP,
  60. WCONFIG_WPA,
  61. START_WPA,
  62. QUIT } state = GET_INTERFACE;
  63. static struct debconfclient *client;
  64. static int requested_wireless_tools = 0;
  65. extern enum wpa_t wpa_supplicant_status;
  66. char **ifaces;
  67. char *defiface = NULL, *defwireless = NULL;
  68. response_t res;
  69. /* initialize libd-i */
  70. di_system_init("netcfg");
  71. di_info("Starting netcfg v.%s (built %s)", NETCFG_VERSION, NETCFG_BUILD_DATE);
  72. parse_args (argc, argv);
  73. reap_old_files ();
  74. open_sockets();
  75. /* initialize debconf */
  76. client = debconfclient_new();
  77. debconf_capb(client, "backup");
  78. /* Check to see if netcfg should be run at all */
  79. debconf_get(client, "netcfg/enable");
  80. if (!strcmp(client->value, "false")) {
  81. struct in_addr null_ipaddress;
  82. char *hostname = NULL;
  83. null_ipaddress.s_addr = 0;
  84. netcfg_get_hostname(client, "netcfg/get_hostname", &hostname, 0);
  85. netcfg_write_common(null_ipaddress, hostname, NULL);
  86. return 0;
  87. }
  88. /* always always always default back to DHCP, unless you've specified
  89. * disable_dhcp on the command line. */
  90. debconf_get(client, "netcfg/disable_dhcp");
  91. if (!strcmp(client->value, "true"))
  92. debconf_set(client, "netcfg/use_dhcp", "false");
  93. else
  94. debconf_set(client, "netcfg/use_dhcp", "true");
  95. for (;;) {
  96. switch(state) {
  97. case BACKUP:
  98. return 10;
  99. case GET_INTERFACE:
  100. /* If we have returned from outside of netcfg and want to
  101. * reconfigure networking, check to see if wpasupplicant is
  102. * running, and kill it if it is. If left running when
  103. * the interfaces are taken up and down, it appears to
  104. * leave it in an inconsistant state */
  105. kill_wpa_supplicant();
  106. /* Choose a default by looking for link */
  107. if (get_all_ifs(1, &ifaces) > 1) {
  108. while (*ifaces) {
  109. if (check_kill_switch(*ifaces)) {
  110. debconf_subst(client, "netcfg/kill_switch_enabled", "iface", *ifaces);
  111. debconf_input(client, "high", "netcfg/kill_switch_enabled");
  112. if (debconf_go(client) == 30) {
  113. state = BACKUP;
  114. break;
  115. }
  116. /* Is it still enabled? */
  117. if (check_kill_switch(*ifaces)) {
  118. ifaces++;
  119. continue;
  120. }
  121. }
  122. interface_up(*ifaces);
  123. if (netcfg_detect_link (client, *ifaces) == 1) /* CONNECTED */ {
  124. di_info("found link on interface %s, making it the default.", *ifaces);
  125. defiface = strdup(*ifaces);
  126. break;
  127. } else {
  128. #ifdef WIRELESS
  129. struct wireless_config wc;
  130. #endif /* WIRELESS */
  131. di_info("found no link on interface %s.", *ifaces);
  132. #ifdef WIRELESS
  133. if (iw_get_basic_config(wfd, *ifaces, &wc) == 0) {
  134. wc.essid[0] = '\0';
  135. wc.essid_on = 0;
  136. iw_set_basic_config(wfd, *ifaces, &wc);
  137. sleep(1);
  138. iw_get_basic_config(wfd, *ifaces, &wc);
  139. if (!empty_str(wc.essid)) {
  140. di_info("%s is associated with %s. Selecting as default", *ifaces, wc.essid);
  141. defiface = strdup(*ifaces);
  142. interface_down(*ifaces);
  143. break;
  144. } else {
  145. di_info("%s is not associated. Relegating to defwireless", *ifaces);
  146. if (defwireless != NULL)
  147. free (defwireless);
  148. defwireless = strdup(*ifaces);
  149. }
  150. }
  151. else
  152. di_info("%s is not a wireless interface. Continuing.", *ifaces);
  153. interface_down(*ifaces);
  154. #endif
  155. }
  156. interface_down(*ifaces);
  157. ifaces++;
  158. }
  159. }
  160. if (state == BACKUP)
  161. break;
  162. if (!defiface && defwireless)
  163. defiface = defwireless;
  164. if(netcfg_get_interface(client, &interface, &num_interfaces, defiface))
  165. state = BACKUP;
  166. else if (! interface || ! num_interfaces)
  167. state = GET_HOSTNAME_ONLY;
  168. else {
  169. if (is_wireless_iface (interface))
  170. state = WCONFIG;
  171. else
  172. state = GET_METHOD;
  173. }
  174. break;
  175. case GET_HOSTNAME_ONLY:
  176. if(netcfg_get_hostname(client, "netcfg/get_hostname", &hostname, 0))
  177. state = BACKUP;
  178. else {
  179. struct in_addr null_ipaddress;
  180. null_ipaddress.s_addr = 0;
  181. netcfg_write_common(null_ipaddress, hostname, NULL);
  182. state = QUIT;
  183. }
  184. break;
  185. case GET_METHOD:
  186. if ((res = netcfg_get_method(client)) == GO_BACK)
  187. state = (num_interfaces == 1) ? BACKUP : GET_INTERFACE;
  188. else {
  189. if (netcfg_method == DHCP)
  190. state = GET_DHCP;
  191. else
  192. state = GET_STATIC;
  193. }
  194. break;
  195. case GET_DHCP:
  196. switch (netcfg_activate_dhcp(client)) {
  197. case 0:
  198. state = QUIT;
  199. break;
  200. case 10:
  201. /*
  202. * It doesn't make sense to go back to GET_METHOD because
  203. * the user has already been asked whether he wants to
  204. * try an alternate method.
  205. */
  206. state = (num_interfaces == 1) ? BACKUP : GET_INTERFACE;
  207. break;
  208. case 15:
  209. state = GET_STATIC;
  210. break;
  211. default:
  212. return 1;
  213. }
  214. break;
  215. case GET_STATIC:
  216. {
  217. int ret;
  218. /* Misnomer - this should actually take care of activation */
  219. if ((ret = netcfg_get_static(client)) == 10)
  220. state = GET_INTERFACE;
  221. else if (ret)
  222. state = GET_METHOD;
  223. else
  224. state = QUIT;
  225. break;
  226. }
  227. case WCONFIG:
  228. if (requested_wireless_tools == 0) {
  229. di_exec_shell_log("apt-install wireless-tools");
  230. requested_wireless_tools = 1;
  231. }
  232. state = WCONFIG_ESSID;
  233. break;
  234. case WCONFIG_ESSID:
  235. if (netcfg_wireless_set_essid(client, interface, NULL) == GO_BACK)
  236. state = BACKUP;
  237. else {
  238. init_wpa_supplicant_support();
  239. if (wpa_supplicant_status == WPA_UNAVAIL)
  240. state = WCONFIG_WEP;
  241. else
  242. state = WCONFIG_SECURITY_TYPE;
  243. }
  244. break;
  245. case WCONFIG_SECURITY_TYPE:
  246. {
  247. int ret;
  248. ret = wireless_security_type(client, interface);
  249. if (ret == GO_BACK)
  250. state = WCONFIG_ESSID;
  251. else if (ret == REPLY_WPA)
  252. state = WCONFIG_WPA;
  253. else
  254. state = WCONFIG_WEP;
  255. break;
  256. }
  257. case WCONFIG_WEP:
  258. if (netcfg_wireless_set_wep(client, interface) == GO_BACK)
  259. if (wpa_supplicant_status == WPA_UNAVAIL)
  260. state = WCONFIG_ESSID;
  261. else
  262. state = WCONFIG_SECURITY_TYPE;
  263. else
  264. state = GET_METHOD;
  265. break;
  266. case WCONFIG_WPA:
  267. if (wpa_supplicant_status == WPA_OK) {
  268. di_exec_shell_log("apt-install wpasupplicant");
  269. wpa_supplicant_status = WPA_QUEUED;
  270. }
  271. if (netcfg_set_passphrase(client, interface) == GO_BACK)
  272. state = WCONFIG_SECURITY_TYPE;
  273. else
  274. state = START_WPA;
  275. break;
  276. case START_WPA:
  277. if (wpa_supplicant_start(client, interface, essid, passphrase) == GO_BACK)
  278. state = WCONFIG_ESSID;
  279. else
  280. state = GET_METHOD;
  281. break;
  282. case QUIT:
  283. netcfg_update_entropy();
  284. return 0;
  285. }
  286. }
  287. }