/* Write Network Manager config file. */ void nm_write_configuration(struct nm_config_info nmconf) { FILE *config_file; char buffer[NM_MAX_LEN_BUF]; /* Create the directory for the config file and clear any possible * previous files found there. */ sprintf(buffer, "mkdir -p %s", NM_CONFIG_FILE_PATH); di_exec_shell(buffer); /* If the directory exist mkdir will do nothing, so just remove every file * there. Rely on the fact that for now netcfg only does config for one * interface. */ sprintf(buffer, "rm %s/*", NM_CONFIG_FILE_PATH); di_exec_shell(buffer); /* Open file using its full path. */ sprintf(buffer, "%s/%s", NM_CONFIG_FILE_PATH, nmconf.connection.id); config_file = fopen(buffer, "w"); if (config_file == NULL) { di_info("Unable to open file for writing network-manager " "configuration. The connection id (%s) might not be " "set to a proper value.", nmconf.connection.id); return; } if (fchmod(fileno(config_file), 0600) != 0) { di_error("network-manager connection file cannot be protected " "from reading: %s", strerror(errno)); exit(1); } nm_write_connection(config_file, nmconf.connection); if (nmconf.connection.type == WIRED) { nm_write_wired_specific_options(config_file, &nmconf); } #ifdef WIRELESS else { nm_write_wireless_specific_options(config_file, &nmconf); if (nmconf.wireless.is_secured) { nm_write_wireless_security(config_file, nmconf.wireless_security); } } #endif nm_write_ipv4(config_file, nmconf.ipv4); nm_write_ipv6(config_file, nmconf.ipv6); fclose(config_file); nm_write_connection_type(nmconf); }
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; }