static void add_default_serial_setting (NMConnection *connection) { NMSettingSerial *s_serial; s_serial = NM_SETTING_SERIAL (nm_setting_serial_new ()); g_object_set (s_serial, NM_SETTING_SERIAL_BAUD, 115200, NM_SETTING_SERIAL_BITS, 8, NM_SETTING_SERIAL_PARITY, 'n', NM_SETTING_SERIAL_STOPBITS, 1, NULL); nm_connection_add_setting (connection, NM_SETTING (s_serial)); }
static NMConnection * dun_new_gsm (NMAMobileWizardAccessMethod *method) { NMConnection *connection; NMSetting *setting; char *uuid, *id; connection = nm_connection_new (); setting = nm_setting_gsm_new (); g_object_set (setting, NM_SETTING_GSM_NUMBER, "*99#", NM_SETTING_GSM_USERNAME, method->username, NM_SETTING_GSM_PASSWORD, method->password, NM_SETTING_GSM_PASSWORD_FLAGS, NM_SETTING_SECRET_FLAG_AGENT_OWNED, NM_SETTING_GSM_APN, method->gsm_apn, NULL); nm_connection_add_setting (connection, setting); /* Serial setting */ setting = nm_setting_serial_new (); g_object_set (setting, NM_SETTING_SERIAL_BAUD, 115200, NM_SETTING_SERIAL_BITS, 8, NM_SETTING_SERIAL_PARITY, 'n', NM_SETTING_SERIAL_STOPBITS, 1, NULL); nm_connection_add_setting (connection, setting); nm_connection_add_setting (connection, nm_setting_ppp_new ()); setting = nm_setting_connection_new (); id = utils_create_mobile_connection_id (method->provider_name, method->plan_name); uuid = nm_utils_uuid_generate (); g_object_set (setting, NM_SETTING_CONNECTION_ID, id, NM_SETTING_CONNECTION_TYPE, NM_SETTING_BLUETOOTH_SETTING_NAME, NM_SETTING_CONNECTION_AUTOCONNECT, FALSE, NM_SETTING_CONNECTION_UUID, uuid, NULL); g_free (uuid); g_free (id); nm_setting_connection_add_permission ((NMSettingConnection *) setting, "user", g_get_user_name (), NULL); nm_connection_add_setting (connection, setting); return connection; }
static NMConnection * dun_new_cdma (NMAMobileWizardAccessMethod *method) { NMConnection *connection; NMSetting *setting; char *uuid, *id; connection = nm_connection_new (); setting = nm_setting_cdma_new (); g_object_set (setting, NM_SETTING_CDMA_NUMBER, "#777", NM_SETTING_CDMA_USERNAME, method->username, NM_SETTING_CDMA_PASSWORD, method->password, NULL); nm_connection_add_setting (connection, setting); /* Serial setting */ setting = nm_setting_serial_new (); g_object_set (setting, NM_SETTING_SERIAL_BAUD, 115200, NM_SETTING_SERIAL_BITS, 8, NM_SETTING_SERIAL_PARITY, 'n', NM_SETTING_SERIAL_STOPBITS, 1, NULL); nm_connection_add_setting (connection, setting); nm_connection_add_setting (connection, nm_setting_ppp_new ()); setting = nm_setting_connection_new (); id = utils_create_mobile_connection_id (method->provider_name, method->plan_name); uuid = nm_utils_uuid_generate (); g_object_set (setting, NM_SETTING_CONNECTION_ID, id, NM_SETTING_CONNECTION_TYPE, NM_SETTING_BLUETOOTH_SETTING_NAME, NM_SETTING_CONNECTION_AUTOCONNECT, FALSE, NM_SETTING_CONNECTION_UUID, uuid, NULL); g_free (uuid); g_free (id); nm_connection_add_setting (connection, setting); return connection; }
static void mobile_wizard_done (NMAMobileWizard *wizard, gboolean cancelled, NMAMobileWizardAccessMethod *method, gpointer user_data) { AutoWizardInfo *info = user_data; NMConnection *connection = NULL; if (!cancelled && method) { NMSetting *setting; char *uuid, *id; const char *setting_name; if (method->devtype != info->requested_capability) { g_warning ("Unexpected device type"); cancelled = TRUE; goto done; } connection = nm_connection_new (); if (method->devtype == NM_DEVICE_MODEM_CAPABILITY_CDMA_EVDO) { setting_name = NM_SETTING_CDMA_SETTING_NAME; setting = nm_setting_cdma_new (); g_object_set (setting, NM_SETTING_CDMA_NUMBER, "#777", NM_SETTING_CDMA_USERNAME, method->username, NM_SETTING_CDMA_PASSWORD, method->password, NM_SETTING_CDMA_PASSWORD_FLAGS, NM_SETTING_SECRET_FLAG_AGENT_OWNED, NULL); nm_connection_add_setting (connection, setting); } else if (method->devtype == NM_DEVICE_MODEM_CAPABILITY_GSM_UMTS) { setting_name = NM_SETTING_GSM_SETTING_NAME; setting = nm_setting_gsm_new (); g_object_set (setting, NM_SETTING_GSM_NUMBER, "*99#", NM_SETTING_GSM_USERNAME, method->username, NM_SETTING_GSM_PASSWORD, method->password, NM_SETTING_GSM_APN, method->gsm_apn, NM_SETTING_GSM_PASSWORD_FLAGS, NM_SETTING_SECRET_FLAG_AGENT_OWNED, NULL); nm_connection_add_setting (connection, setting); } else g_assert_not_reached (); /* Serial setting */ setting = nm_setting_serial_new (); g_object_set (setting, NM_SETTING_SERIAL_BAUD, 115200, NM_SETTING_SERIAL_BITS, 8, NM_SETTING_SERIAL_PARITY, 'n', NM_SETTING_SERIAL_STOPBITS, 1, NULL); nm_connection_add_setting (connection, setting); nm_connection_add_setting (connection, nm_setting_ppp_new ()); setting = nm_setting_connection_new (); id = utils_create_mobile_connection_id (method->provider_name, method->plan_name); uuid = nm_utils_uuid_generate (); g_object_set (setting, NM_SETTING_CONNECTION_ID, id, NM_SETTING_CONNECTION_TYPE, setting_name, NM_SETTING_CONNECTION_AUTOCONNECT, FALSE, NM_SETTING_CONNECTION_UUID, uuid, NULL); g_free (uuid); g_free (id); nm_setting_connection_add_permission ((NMSettingConnection *) setting, "user", g_get_user_name (), NULL); nm_connection_add_setting (connection, setting); } done: (*(info->callback)) (connection, TRUE, cancelled, info->callback_data); if (wizard) nma_mobile_wizard_destroy (wizard); g_free (info); }
static int add_connection (DBusGProxy *proxy, char *con_name, char *apn, char *pin, char *username, char *password, int ntype, char *number, char *auth, char *comp, char *aut, char *netid, char *enc, int ecoint, int ecofail, char *uuid, int sbits, char sparity, int stbits, int sbaud) { NMConnection *connection; NMSettingConnection *s_con; NMSettingIP4Config *s_ip4; NMSettingGsm *s_gsm; NMSettingPPP *s_ppp; NMSettingSerial *s_serial; char *new_con_path = NULL; GHashTable *hash; GError *error = NULL; int autoconnect = 1; int uuid_generated = 0; if ((aut != NULL) && (aut[0] != 't')) autoconnect = 0; connection = (NMConnection *)nm_connection_new (); if (connection == NULL){ printf("Unable to allocate new connection... Sorry.\n"); return NMC_RESULT_ERROR_CON_ADD; } s_con = (NMSettingConnection *) nm_setting_connection_new (); if (s_con == NULL){ printf("Failed to allocate new %s setting... Sorry.\n",NM_SETTING_CONNECTION_SETTING_NAME); return NMC_RESULT_ERROR_CON_ADD; } nm_connection_add_setting (connection, NM_SETTING (s_con)); if (uuid == NULL){ uuid = nm_utils_uuid_generate (); uuid_generated = 1; } /*global settings*/ g_object_set (s_con, NM_SETTING_CONNECTION_ID, con_name, NM_SETTING_CONNECTION_UUID, uuid, NM_SETTING_CONNECTION_AUTOCONNECT, (autoconnect == 1) ? TRUE : FALSE, NM_SETTING_CONNECTION_TYPE, NM_SETTING_GSM_SETTING_NAME, NULL); if(uuid_generated == 1) g_free (uuid); /* GSM setting */ s_gsm = (NMSettingGsm *) nm_setting_gsm_new (); if (s_gsm == NULL){ printf("Failed to allocate new %s setting...Sorry.\n",NM_SETTING_GSM_SETTING_NAME); return NMC_RESULT_ERROR_CON_ADD; } nm_connection_add_setting (connection, NM_SETTING (s_gsm)); /*Network type Network preference to force the device to only use specific network technologies. The permitted values are: -1: any, 0: 3G only, 1: GPRS/EDGE only, 2: prefer 3G, and 3: prefer 2G, 4: prefer 4G (LTE), 5: 4G (LTE) only. Note that not all devices allow network preference control. */ g_object_set (s_gsm, NM_SETTING_GSM_NUMBER, (number == NULL) ? "*99#" : number, NM_SETTING_GSM_APN, apn, NM_SETTING_GSM_USERNAME, username, NM_SETTING_GSM_PASSWORD, password, NM_SETTING_GSM_PIN, pin, NM_SETTING_GSM_NETWORK_TYPE, ntype, NM_SETTING_GSM_NETWORK_ID, netid, NULL); /* Serial setting */ s_serial = (NMSettingSerial *) nm_setting_serial_new (); if (s_serial == NULL){ printf("Failed to allocate new %s setting...Sorry.\n",NM_SETTING_SERIAL_SETTING_NAME); return NMC_RESULT_ERROR_CON_ADD; } nm_connection_add_setting (connection, NM_SETTING (s_serial)); g_object_set (s_serial, NM_SETTING_SERIAL_BAUD, sbaud, NM_SETTING_SERIAL_BITS, sbits, NM_SETTING_SERIAL_PARITY, sparity, NM_SETTING_SERIAL_STOPBITS, stbits, NULL); /* IP4 setting */ s_ip4 = (NMSettingIP4Config *) nm_setting_ip4_config_new (); if (s_ip4 == NULL){ printf("Failed to allocate new %s setting... Sorry.\n",NM_SETTING_IP4_CONFIG_SETTING_NAME); return NMC_RESULT_ERROR_CON_ADD; } nm_connection_add_setting (connection, NM_SETTING (s_ip4)); g_object_set (s_ip4, NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, NULL); /* PPP setting */ s_ppp = (NMSettingPPP *) nm_setting_ppp_new (); if (s_ppp == NULL){ printf("Failed to allocate new %s setting... Sorry.\n", NM_SETTING_PPP_SETTING_NAME); return NMC_RESULT_ERROR_CON_ADD; } g_object_set(s_ppp, NM_SETTING_PPP_REFUSE_EAP, (auth[0] == 't') ? FALSE : TRUE, NM_SETTING_PPP_REFUSE_PAP, (auth[1] == 't') ? FALSE : TRUE, NM_SETTING_PPP_REFUSE_CHAP, (auth[2] == 't') ? FALSE : TRUE, NM_SETTING_PPP_REFUSE_MSCHAP, (auth[3] == 't') ? FALSE : TRUE, NM_SETTING_PPP_REFUSE_MSCHAPV2, (auth[4] == 't') ? FALSE : TRUE, NM_SETTING_PPP_NOBSDCOMP, (comp[0] == 't') ? FALSE : TRUE, NM_SETTING_PPP_NODEFLATE, (comp[1] == 't') ? FALSE : TRUE, NM_SETTING_PPP_NO_VJ_COMP,(comp[2] == 't') ? FALSE : TRUE, //tcp header compression NM_SETTING_PPP_REQUIRE_MPPE, (strcmp(enc, "ff")==0) ? FALSE : TRUE, NM_SETTING_PPP_MPPE_STATEFUL, (enc[1] == 't') ? TRUE : FALSE, NM_SETTING_PPP_REQUIRE_MPPE_128, (enc[0] == 't') ? TRUE : FALSE, NM_SETTING_PPP_LCP_ECHO_FAILURE, (ecofail > 0) ? ecofail : 0, NM_SETTING_PPP_LCP_ECHO_INTERVAL, (ecoint > 0) ? ecoint: 0, NULL); nm_connection_add_setting (connection, NM_SETTING (s_ppp)); hash = nm_connection_to_hash (connection, NM_SETTING_HASH_FLAG_ALL); /* Call AddConnection with the hash as argument */ if (!dbus_g_proxy_call (proxy, "AddConnection", &error, DBUS_TYPE_G_MAP_OF_MAP_OF_VARIANT, hash, G_TYPE_INVALID, DBUS_TYPE_G_OBJECT_PATH, &new_con_path, G_TYPE_INVALID)) { g_print ("Error adding connection: %s %s\n", dbus_g_error_get_name (error), error->message); g_clear_error (&error); g_hash_table_destroy (hash); g_object_unref (connection); return NMC_RESULT_ERROR_CON_ADD; } else { g_print ("\n\tConnection added successfully at: %s \n\tUse: nmcli con list id %s - to see connection detailed info \n\tcon delete id %s - to delete connection\n", new_con_path, con_name, con_name); g_print ("\tTo bring connection up use: nmcli con up id %s.\n",con_name); g_print ("\tTo see connection status use: nmcli con status id %s.\n\n",con_name); g_free (new_con_path); } g_hash_table_destroy (hash); g_object_unref (connection); return 0; }
static void gsm_mobile_wizard_done (NMAMobileWizard *wizard, gboolean canceled, NMAMobileWizardAccessMethod *method, gpointer user_data) { NMConnection *connection = NULL; if (!canceled && method) { NMSetting *setting; char *uuid, *id; if (method->devtype != NM_DEVICE_MODEM_CAPABILITY_GSM_UMTS) { g_warning ("Unexpected device type (not GSM)."); canceled = TRUE; goto done; } connection = nm_connection_new (); setting = nm_setting_gsm_new (); g_object_set (setting, NM_SETTING_GSM_NUMBER, "*99#", NM_SETTING_GSM_USERNAME, method->username, NM_SETTING_GSM_PASSWORD, method->password, NM_SETTING_GSM_APN, method->gsm_apn, NULL); nm_connection_add_setting (connection, setting); /* Serial setting */ setting = nm_setting_serial_new (); g_object_set (setting, NM_SETTING_SERIAL_BAUD, 115200, NM_SETTING_SERIAL_BITS, 8, NM_SETTING_SERIAL_PARITY, 'n', NM_SETTING_SERIAL_STOPBITS, 1, NULL); nm_connection_add_setting (connection, setting); nm_connection_add_setting (connection, nm_setting_ppp_new ()); setting = nm_setting_connection_new (); if (method->plan_name) id = g_strdup_printf ("%s %s", method->provider_name, method->plan_name); else id = g_strdup_printf ("%s connection", method->provider_name); uuid = nm_utils_uuid_generate (); g_object_set (setting, NM_SETTING_CONNECTION_ID, id, NM_SETTING_CONNECTION_TYPE, NM_SETTING_GSM_SETTING_NAME, NM_SETTING_CONNECTION_AUTOCONNECT, FALSE, NM_SETTING_CONNECTION_UUID, uuid, NULL); g_free (uuid); g_free (id); nm_connection_add_setting (connection, setting); } done: connect_3g (connection, canceled, user_data); nma_mobile_wizard_destroy (wizard); }
NMConnection * connection_from_file (const char *filename, GError **error) { GKeyFile *key_file; struct stat statbuf; gboolean bad_owner, bad_permissions; NMConnection *connection = NULL; NMSettingConnection *s_con; NMSettingBluetooth *s_bt; NMSetting *setting; gchar **groups; gsize length; int i; gboolean vpn_secrets = FALSE; const char *ctype, *tmp; GError *verify_error = NULL; if (stat (filename, &statbuf) != 0 || !S_ISREG (statbuf.st_mode)) { g_set_error_literal (error, KEYFILE_PLUGIN_ERROR, 0, "File did not exist or was not a regular file"); return NULL; } bad_owner = getuid () != statbuf.st_uid; bad_permissions = statbuf.st_mode & 0077; if (bad_owner || bad_permissions) { g_set_error (error, KEYFILE_PLUGIN_ERROR, 0, "File permissions (%o) or owner (%d) were insecure", statbuf.st_mode, statbuf.st_uid); return NULL; } key_file = g_key_file_new (); if (!g_key_file_load_from_file (key_file, filename, G_KEY_FILE_NONE, error)) goto out; connection = nm_connection_new (); groups = g_key_file_get_groups (key_file, &length); for (i = 0; i < length; i++) { /* Only read out secrets when needed */ if (!strcmp (groups[i], VPN_SECRETS_GROUP)) { vpn_secrets = TRUE; continue; } setting = read_setting (key_file, groups[i]); if (setting) nm_connection_add_setting (connection, setting); } /* Make sure that we have the base device type setting even if * the keyfile didn't include it, which can happen when the base * device type setting is all default values (like ethernet). */ s_con = (NMSettingConnection *) nm_connection_get_setting (connection, NM_TYPE_SETTING_CONNECTION); if (s_con) { ctype = nm_setting_connection_get_connection_type (s_con); setting = nm_connection_get_setting_by_name (connection, ctype); if (ctype) { gboolean add_serial = FALSE; NMSetting *new_setting = NULL; if (!setting && !strcmp (ctype, NM_SETTING_WIRED_SETTING_NAME)) new_setting = nm_setting_wired_new (); else if (!strcmp (ctype, NM_SETTING_BLUETOOTH_SETTING_NAME)) { s_bt = (NMSettingBluetooth *) nm_connection_get_setting (connection, NM_TYPE_SETTING_BLUETOOTH); if (s_bt) { tmp = nm_setting_bluetooth_get_connection_type (s_bt); if (tmp && !strcmp (tmp, NM_SETTING_BLUETOOTH_TYPE_DUN)) add_serial = TRUE; } } else if (!strcmp (ctype, NM_SETTING_GSM_SETTING_NAME)) add_serial = TRUE; else if (!strcmp (ctype, NM_SETTING_CDMA_SETTING_NAME)) add_serial = TRUE; /* Bluetooth DUN, GSM, and CDMA connections require a serial setting */ if (add_serial && !nm_connection_get_setting (connection, NM_TYPE_SETTING_SERIAL)) new_setting = nm_setting_serial_new (); if (new_setting) nm_connection_add_setting (connection, new_setting); } } /* Serial connections require a PPP setting too */ if (nm_connection_get_setting (connection, NM_TYPE_SETTING_SERIAL)) { if (!nm_connection_get_setting (connection, NM_TYPE_SETTING_PPP)) nm_connection_add_setting (connection, nm_setting_ppp_new ()); } /* Handle vpn secrets after the 'vpn' setting was read */ if (vpn_secrets) { NMSettingVPN *s_vpn; s_vpn = (NMSettingVPN *) nm_connection_get_setting (connection, NM_TYPE_SETTING_VPN); if (s_vpn) read_vpn_secrets (key_file, s_vpn); } g_strfreev (groups); /* Verify the connection */ if (!nm_connection_verify (connection, &verify_error)) { g_set_error (error, KEYFILE_PLUGIN_ERROR, 0, "invalid or missing connection property '%s'", (verify_error && verify_error->message) ? verify_error->message : "(unknown)"); g_clear_error (&verify_error); g_object_unref (connection); connection = NULL; } out: g_key_file_free (key_file); return connection; }