コード例 #1
0
ファイル: nm-conf.c プロジェクト: prusso/ubiquity
/* Getting configurations for NM relies on netcfrg global variables. */
void nm_get_configuration(struct netcfg_interface *niface, struct nm_config_info *nmconf)
{
    /* Decide if wireless configuration is needed. */
    if (!is_wireless_iface(niface->name)) {
        nm_get_wired_config(niface, nmconf);
    }
#ifdef WIRELESS
    else {
        nm_get_wireless_config(niface, nmconf);
    }
#endif
    if (nmconf->ipv4.method == MANUAL || nmconf->ipv6.method == MANUAL) {
        /* Manual address family configuration should be bound to a MAC
         * address. Hence record this fact globally for the connection. */
        nmconf->connection.manual = 1;
    }
    else {
        nmconf->connection.manual = 0;
    }
}
コード例 #2
0
ファイル: write_interface.c プロジェクト: GalliumOS/ubiquity
/* The main function for writing things to INTERFACES_FILE (aka
 * /etc/network/interfaces).
 *
 * In principle, this function is very simple: just examine the interface
 * we've been passed, and call out to the relevant private helper function. 
 * In practice...
 *
 * Takes the interface struct to write out.  If you pass NULL, the file gets
 * deleted and a helpful comment header gets written.
 *
 * Returns a true/false boolean representing "did everything go OK"; if 0 is
 * returned, the interfaces file will not have been modified, and errno will
 * contain the details.
 */
int netcfg_write_interface(const struct netcfg_interface *interface)
{
	FILE *fd;
	int rv;
	struct stat stat_buf;
	
	if (!interface) {
		di_debug("No interface given; clearing " INTERFACES_FILE);
		rv = unlink(INTERFACES_FILE);
		if (rv < 0 && errno != ENOENT) {
			di_info("Error clearing %s: %s", INTERFACES_FILE, strerror(errno));
			return 0;
		}
	}
	
	fd = file_open(INTERFACES_FILE ".tmp", "w");
	if (!fd) {
		di_warning("Failed to open %s.tmp: %s", INTERFACES_FILE, strerror(errno));
		return 0;
	}

	/* All of this code is to handle the apparently simple task of
	 * copying the existing interfaces file to the tmpfile (if it exists)
	 * so we can add our new stuff to it.  Bloody longwinded way of doing
	 * it, I'm sure you'll agree.
	 */
	rv = stat(INTERFACES_FILE, &stat_buf);
	if (rv < 0 && errno != ENOENT) {
		di_warning("Failed to stat %s: %s", INTERFACES_FILE, strerror(errno));
		unlink(INTERFACES_FILE ".tmp");
		return 0;
	}
	if (rv == 0) {
		char *tmpbuf = malloc(stat_buf.st_size + 1);
		int origfd;
		origfd = open(INTERFACES_FILE, O_RDONLY);
		if (origfd < 0) {
			di_warning("Failed to open %s: %s", INTERFACES_FILE, strerror(errno));
			fclose(fd);
			unlink(INTERFACES_FILE ".tmp");
			free(tmpbuf);
			return 0;
		}
		rv = read(origfd, tmpbuf, stat_buf.st_size);
		if (rv < 0) {
			di_warning("Failed to read %s: %s", INTERFACES_FILE, strerror(errno));
			fclose(fd);
			unlink(INTERFACES_FILE ".tmp");
			free(tmpbuf);
			close(origfd);
			return 0;
		}
		if (rv != stat_buf.st_size) {
			di_warning("Short read on %s", INTERFACES_FILE);
			fclose(fd);
			unlink(INTERFACES_FILE ".tmp");
			free(tmpbuf);
			close(origfd);
			return 0;
		}
		rv = fwrite(tmpbuf, sizeof(char), stat_buf.st_size, fd);
		if (rv != (int)stat_buf.st_size) {
			di_warning("Short write on %s.tmp", INTERFACES_FILE);
			fclose(fd);
			unlink(INTERFACES_FILE ".tmp");
			free(tmpbuf);
			close(origfd);
			return 0;
		}
		free(tmpbuf);
		close(origfd);
	}
		
	
	/* Thank $DEITY all that's out of the way... now we can write a
	 * freaking interfaces file entry */
	rv = 1;

	if (!interface) {
		di_debug("Writing informative header");
		rv = nc_wi_header(fd);
	} else if (interface->loopback == 1) {
		di_debug("Writing loopback interface");
		rv = nc_wi_loopback(interface, fd);
	} else if (interface->dhcp == 1 || interface->slaac == 1) {
		if (interface->dhcp == 1) {
			di_debug("Writing DHCP stanza for %s", interface->name);
			rv = nc_wi_dhcp(interface, fd);
		}
		if (interface->slaac == 1) {
			di_debug("Writing SLAAC stanza for %s", interface->name);
			rv = nc_wi_slaac(interface, fd);
		}
	} else if (interface->address_family == AF_INET) {
		di_debug("Writing static IPv4 stanza for %s", interface->name);
		rv = nc_wi_static_ipv4(interface, fd);
	} else if (interface->address_family == AF_INET6) {
		di_debug("Writing static IPv6 stanza for %s", interface->name);
		rv = nc_wi_static_ipv6(interface, fd);
	}
	if (rv && interface && interface->parentif) {
		di_debug("Writing VLAN: %s", interface->name);
		rv = nc_wi_vlan(interface, fd);
	}
	if (rv && interface && is_wireless_iface(interface->name)) {
		di_debug("Writing wireless options for %s", interface->name);
		rv = nc_wi_wireless_options(interface, fd);
	}

	if (rv) {
		di_debug("Success!");
		rename(INTERFACES_FILE ".tmp", INTERFACES_FILE);
	}

	fclose(fd);
	unlink(INTERFACES_FILE ".tmp");
	return rv;
}
コード例 #3
0
ファイル: netcfg.c プロジェクト: vipullinux/DebianInstaller
int main(int argc, char *argv[])
{
    int num_interfaces = 0;
    enum { BACKUP,
           GET_INTERFACE,
           GET_HOSTNAME_ONLY,
           GET_METHOD,
           GET_DHCP,
           GET_STATIC,
           WCONFIG,
           WCONFIG_ESSID,
           WCONFIG_SECURITY_TYPE,
           WCONFIG_WEP,
           WCONFIG_WPA,
           START_WPA,
           QUIT } state = GET_INTERFACE;

    static struct debconfclient *client;
    static int requested_wireless_tools = 0;
    char **ifaces;
    char *defiface = NULL, *defwireless = NULL;
    response_t res;
    struct netcfg_interface interface;
#ifdef NM
    struct nm_config_info nmconf;
#endif

    /* initialize libd-i */
    di_system_init("netcfg");
    netcfg_interface_init(&interface);

    if (strcmp(basename(argv[0]), "ptom") != 0)
        di_info("Starting netcfg v.%s", NETCFG_VERSION);

    parse_args (argc, argv);
    reap_old_files ();
    open_sockets();

    /* initialize debconf */
    client = debconfclient_new();
    debconf_capb(client, "backup");

    /* Check to see if netcfg should be run at all */
    debconf_get(client, "netcfg/enable");
    if (!strcmp(client->value, "false")) {
        netcfg_get_hostname(client, "netcfg/get_hostname", hostname, 0);
        netcfg_write_common("", hostname, NULL);
        return 0;
    }

    /* always always always default back to autoconfig, unless you've specified
     * disable_autoconfig on the command line. */
    debconf_get(client, "netcfg/disable_autoconfig");

    if (!strcmp(client->value, "true"))
        debconf_set(client, "netcfg/use_autoconfig", "false");
    else
        debconf_set(client, "netcfg/use_autoconfig", "true");

    /* also support disable_dhcp for compatibility */
    debconf_get(client, "netcfg/disable_dhcp");

    if (!strcmp(client->value, "true"))
        debconf_set(client, "netcfg/use_autoconfig", "false");

    for (;;) {
        switch(state) {
        case BACKUP:
            return RETURN_TO_MAIN;
        case GET_INTERFACE:
            /* If we have returned from outside of netcfg and want to
             * reconfigure networking, check to see if wpasupplicant is
             * running, and kill it if it is. If left running when
             * the interfaces are taken up and down, it appears to
             * leave it in an inconsistant state */
            kill_wpa_supplicant();

            /* Choose a default by looking for link */
            if (get_all_ifs(1, &ifaces) > 1) {
                while (*ifaces) {
                    struct netcfg_interface link_interface;

                    if (check_kill_switch(*ifaces)) {
                        debconf_subst(client, "netcfg/kill_switch_enabled", "iface", *ifaces);
                        debconf_input(client, "high", "netcfg/kill_switch_enabled");
                        if (debconf_go(client) == CMD_GOBACK) {
                            state = BACKUP;
                            break;
                        }
                        /* Is it still enabled? */
                        if (check_kill_switch(*ifaces)) {
                            ifaces++;
                            continue;
                        }
                    }

                    interface_up(*ifaces);

                    netcfg_interface_init(&link_interface);
                    link_interface.name = strdup(*ifaces);
                    if (netcfg_detect_link (client, &link_interface) == 1) /* CONNECTED */ {
                        /* CONNECTED */
                        di_info("found link on interface %s, making it the default.", *ifaces);
                        defiface = strdup(*ifaces);
                        free(link_interface.name);
                        break;
                    } else {
#ifdef WIRELESS
                        struct wireless_config wc;
#endif /* WIRELESS */
                        di_info("found no link on interface %s.", *ifaces);
#ifdef WIRELESS
                        if (iw_get_basic_config(wfd, *ifaces, &wc) == 0) {
                            wc.essid[0] = '\0';
                            wc.essid_on = 0;

                            iw_set_basic_config(wfd, *ifaces, &wc);

                            sleep(1);

                            iw_get_basic_config(wfd, *ifaces, &wc);

                            if (!empty_str(wc.essid)) {
                                di_info("%s is associated with %s. Selecting as default", *ifaces, wc.essid);
                                defiface = strdup(*ifaces);
                                interface_down(*ifaces);
                                break;
                            } else {
                                di_info("%s is not associated. Relegating to defwireless", *ifaces);
                                if (defwireless != NULL)
                                    free (defwireless);
                                defwireless = strdup(*ifaces);
                            }
                        }
                        else
                            di_info("%s is not a wireless interface. Continuing.", *ifaces);

                        interface_down(*ifaces);
#endif
                    }

                    free(link_interface.name);
                    interface_down(*ifaces);

                    ifaces++;
                }
            }

            if (state == BACKUP)
                break;

            if (!defiface && defwireless)
                defiface = defwireless;

            if(netcfg_get_interface(client, &(interface.name), &num_interfaces, defiface))
                state = BACKUP;
            else if (! interface.name || ! num_interfaces)
                state = GET_HOSTNAME_ONLY;
            else {
                if (is_wireless_iface (interface.name))
                    state = WCONFIG;
                else
                    state = GET_METHOD;
            }
            break;
        case GET_HOSTNAME_ONLY:
            if(netcfg_get_hostname(client, "netcfg/get_hostname", hostname, 0))
                state = BACKUP;
            else {
                netcfg_write_common("", hostname, NULL);
                state = QUIT;
            }
            break;
        case GET_METHOD:
            if ((res = netcfg_get_method(client)) == GO_BACK)
                state = (num_interfaces == 1) ? BACKUP : GET_INTERFACE;
            else {
                if (netcfg_method == DHCP)
                    state = GET_DHCP;
                else
                    state = GET_STATIC;
            }
            break;

        case GET_DHCP:
            switch (netcfg_activate_dhcp(client, &interface)) {
            case 0:
                state = QUIT;
                break;
            case RETURN_TO_MAIN:
                /*
                 * It doesn't make sense to go back to GET_METHOD because
                 * the user has already been asked whether they want to
                 * try an alternate method.
                 */
                state = (num_interfaces == 1) ? BACKUP : GET_INTERFACE;
                break;
            case CONFIGURE_MANUALLY:
                state = GET_STATIC;
                break;
            default:
                return 1;
            }
            break;

        case GET_STATIC:
            {
                int ret;
                /* Misnomer - this should actually take care of activation */
                if ((ret = netcfg_get_static(client, &interface)) == RETURN_TO_MAIN)
                    state = GET_INTERFACE;
                else if (ret)
                    state = GET_METHOD;
                else
                    state = QUIT;
                break;
            }

        case WCONFIG:
            if (requested_wireless_tools == 0) {
                di_exec_shell_log("apt-install iw wireless-tools");
                requested_wireless_tools = 1;
            }
            state = WCONFIG_ESSID;
            break;

        case WCONFIG_ESSID:
            if (netcfg_wireless_set_essid(client, &interface) == GO_BACK)
                state = BACKUP;
            else {
                init_wpa_supplicant_support(&interface);
                if (interface.wpa_supplicant_status == WPA_UNAVAIL)
                    state = WCONFIG_WEP;
                else
                    state = WCONFIG_SECURITY_TYPE;
            }
            break;

        case WCONFIG_SECURITY_TYPE:
            {
                int ret;
                ret = wireless_security_type(client, interface.name);
                if (ret == GO_BACK)
                    state = WCONFIG_ESSID;
                else if (ret == REPLY_WPA) {
                    state = WCONFIG_WPA;
                    interface.wifi_security = REPLY_WPA;
                }
                else {
                    state = WCONFIG_WEP;
                    interface.wifi_security = REPLY_WEP;
                }
                break;
            }

        case WCONFIG_WEP:
            if (netcfg_wireless_set_wep(client, &interface) == GO_BACK) 
                if (interface.wpa_supplicant_status == WPA_UNAVAIL)
                    state = WCONFIG_ESSID;
                else
                    state = WCONFIG_SECURITY_TYPE;
            else
                state = GET_METHOD;
            break;

        case WCONFIG_WPA:
            if (interface.wpa_supplicant_status == WPA_OK) {
                di_exec_shell_log("apt-install wpasupplicant");
                interface.wpa_supplicant_status = WPA_QUEUED;
            }

            if (netcfg_set_passphrase(client, &interface) == GO_BACK)
                state = WCONFIG_SECURITY_TYPE;
            else
                state = START_WPA;
            break;

        case START_WPA:
            if (wpa_supplicant_start(client, &interface) == GO_BACK)
                state = WCONFIG_ESSID;
            else
                state = GET_METHOD;
            break;

        case QUIT:
#ifdef NM
            if (num_interfaces > 0) {
                nm_get_configuration(&interface, &nmconf);
                nm_write_configuration(nmconf);
            }
#endif

            netcfg_update_entropy();
            return 0;
        }
    }
}
コード例 #4
0
ファイル: netcfg-static.c プロジェクト: 27Paolo/ubiquity
int main(int argc, char** argv)
{
    int num_interfaces = 0;
    static struct debconfclient *client;
    static int requested_wireless_tools = 0;
    struct netcfg_interface interface;

    enum { BACKUP, GET_INTERFACE, GET_HOSTNAME_ONLY, GET_STATIC, WCONFIG, WCONFIG_ESSID, WCONFIG_WEP, QUIT} state = GET_INTERFACE;

    /* initialize libd-i */
    di_system_init("netcfg-static");
    if (strcmp(basename(argv[0]), "ptom") != 0)
	di_info("Starting netcfg v.%s (built %s)", NETCFG_VERSION, NETCFG_BUILD_DATE);

    parse_args(argc, argv);
    reap_old_files();
    open_sockets();

    /* initialize debconf */
    client = debconfclient_new();
    debconf_capb(client, "backup");

    while (1) {
        switch(state) {
        case BACKUP:
            return 10;
        case GET_INTERFACE:
            if (netcfg_get_interface(client, &(interface.name), &num_interfaces, NULL))
                state = BACKUP;
            else if (! interface.name || ! num_interfaces)
                state = GET_HOSTNAME_ONLY;
            else {
                if (is_wireless_iface(interface.name))
                    state = WCONFIG;
                else
                    state = GET_STATIC;
            }
            break;
        case GET_HOSTNAME_ONLY:
            if(netcfg_get_hostname(client, "netcfg/get_hostname", hostname, 0))
                state = BACKUP;
            else {
                netcfg_write_common("", hostname, NULL);
                return 0;
            }
            break;
        case GET_STATIC:
            if (netcfg_get_static(client, &interface))
                state = (num_interfaces == 1) ? BACKUP : GET_INTERFACE;
            else
                state = QUIT;
            break;

        case WCONFIG:
            if (requested_wireless_tools == 0) {
                requested_wireless_tools = 1;
                di_exec_shell("apt-install wireless-tools");
            }
            state = WCONFIG_ESSID;
            break;

        case WCONFIG_ESSID:
            if (netcfg_wireless_set_essid (client, &interface, NULL))
                state = BACKUP;
            else
                state = WCONFIG_WEP;
            break;

        case WCONFIG_WEP:
            if (netcfg_wireless_set_wep (client, &interface))
                state = WCONFIG_ESSID;
            else
                state = GET_STATIC;
            break;

        case QUIT:
            netcfg_update_entropy();
            return 0;
        }
    }

    return 0;
}