static GValue *
addr_to_gvalue (const char *str)
{
	struct in_addr	temp_addr;

	/* Empty */
	if (!str || strlen (str) < 1)
		return NULL;

	if (inet_pton (AF_INET, str, &temp_addr) <= 0)
		return NULL;

	return uint_to_gvalue (temp_addr.s_addr);
}
Exemplo n.º 2
0
static void
test_update_secrets_wifi (void)
{
	NMConnection *connection;
	NMSettingConnection *s_con;
	NMSettingWireless *s_wifi;
	NMSettingWirelessSecurity *s_wsec;
	unsigned char tmpssid[] = { 0x31, 0x33, 0x33, 0x37 };
	const char *wepkey = "11111111111111111111111111";
	GHashTable *secrets;
	GError *error = NULL;
	char *uuid;
	GByteArray *ssid;
	gboolean success;

	connection = nm_connection_new ();
	g_assert (connection);

	/* Connection setting */
	s_con = (NMSettingConnection *) nm_setting_connection_new ();
	g_assert (s_con);

	uuid = nm_utils_uuid_generate ();
	g_object_set (s_con,
	              NM_SETTING_CONNECTION_ID, "Test Wireless",
	              NM_SETTING_CONNECTION_UUID, uuid,
	              NM_SETTING_CONNECTION_AUTOCONNECT, FALSE,
	              NM_SETTING_CONNECTION_TYPE, NM_SETTING_WIRELESS_SETTING_NAME,
	              NULL);
	g_free (uuid);
	nm_connection_add_setting (connection, NM_SETTING (s_con));

	/* Wireless setting */
	s_wifi = (NMSettingWireless *) nm_setting_wireless_new ();
	g_assert (s_wifi);

	ssid = g_byte_array_sized_new (sizeof (tmpssid));
	g_byte_array_append (ssid, &tmpssid[0], sizeof (tmpssid));
	g_object_set (s_wifi,
	              NM_SETTING_WIRELESS_SSID, ssid,
	              NM_SETTING_WIRELESS_SEC, NM_SETTING_WIRELESS_SECURITY_SETTING_NAME,
	              NULL);
	g_byte_array_free (ssid, TRUE);
	nm_connection_add_setting (connection, NM_SETTING (s_wifi));

	/* Wifi security */
	s_wsec = (NMSettingWirelessSecurity *) nm_setting_wireless_security_new ();
	g_assert (s_wsec);

	g_object_set (G_OBJECT (s_wsec),
	              NM_SETTING_WIRELESS_SECURITY_KEY_MGMT, "none",
	              NULL);
	nm_connection_add_setting (connection, NM_SETTING (s_wsec));

	/* Build up the secrets hash */
	secrets = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, value_destroy);
	g_hash_table_insert (secrets, NM_SETTING_WIRELESS_SECURITY_WEP_KEY0, string_to_gvalue (wepkey));
	g_hash_table_insert (secrets, NM_SETTING_WIRELESS_SECURITY_WEP_KEY_TYPE, uint_to_gvalue (NM_WEP_KEY_TYPE_KEY));

	success = nm_connection_update_secrets (connection,
	                                        NM_SETTING_WIRELESS_SECURITY_SETTING_NAME,
	                                        secrets,
	                                        &error);
	if (!success) {
		/* Print the warning message before we assert success */
		g_assert (error);
		g_warning ("Error updating connection secrets: %s", error->message);
		g_clear_error (&error);
	}
	g_assert (success);
}
static gboolean
nm_openssh_send_ip4_config (sshtun_handle_t handle)
{
	DBusGConnection *connection;
	GError *err = NULL;
	GHashTable *config;
	GValue *val;
	struct in_addr temp_addr;
	const char *tmp;

	connection = dbus_g_bus_get (DBUS_BUS_SYSTEM, &err);
	if (!connection)
		return FALSE;

	config = g_hash_table_new (g_str_hash, g_str_equal);

	/* Gateway */
	val = addr_to_gvalue (sshtun_get_param (handle, SSHTUN_PARAM_GW_ADDR));
	if (val)
		g_hash_table_insert (config, NM_VPN_PLUGIN_IP4_CONFIG_GATEWAY, val);
	else {
		helper_failed (connection, "VPN Gateway");
		dbus_g_connection_unref (connection);
		return FALSE;
	}

	/* Tunnel device */
	val = str_to_gvalue (sshtun_get_param (handle, SSHTUN_PARAM_TUN_DEV),
						 FALSE);
	if (val)
		g_hash_table_insert (config, NM_VPN_PLUGIN_IP4_CONFIG_TUNDEV, val);
	else {
		helper_failed (connection, "Tunnel Device");
		dbus_g_connection_unref (connection);
		return FALSE;
	}

	/* IP address */
	val = addr_to_gvalue (sshtun_get_param (handle, SSHTUN_PARAM_ADDR));
	if (val)
		g_hash_table_insert (config, NM_VPN_PLUGIN_IP4_CONFIG_ADDRESS, val);
	else {
		helper_failed (connection, "IP4 Address");
		dbus_g_connection_unref (connection);
		return FALSE;
	}

	/* PTP address */
	val = addr_to_gvalue (sshtun_get_param (handle, SSHTUN_PARAM_PEER_ADDR));
	if (val)
		g_hash_table_insert (config, NM_VPN_PLUGIN_IP4_CONFIG_PTP, val);

	/* Netmask */
	tmp = sshtun_get_param (handle, SSHTUN_PARAM_NETMASK);
	if (tmp && inet_pton (AF_INET, tmp, &temp_addr) > 0) {
		val = uint_to_gvalue (nm_utils_ip4_netmask_to_prefix (temp_addr.s_addr));
		g_hash_table_insert (config, NM_VPN_PLUGIN_IP4_CONFIG_PREFIX, val);
	}

	/* MTU */
	tmp = sshtun_get_param (handle, SSHTUN_PARAM_MTU);
	if (tmp && strlen (tmp)) {
		long int mtu;

		errno = 0;
		mtu = strtol (tmp, NULL, 10);
		if (errno || mtu < 0 || mtu > 20000) {
			nm_warning ("Ignoring invalid tunnel MTU '%s'", tmp);
		} else {
			val = uint_to_gvalue ((guint32) mtu);
			g_hash_table_insert (config, NM_VPN_PLUGIN_IP4_CONFIG_MTU, val);
		}
	}

	send_ip4_config (connection, config);
	dbus_g_connection_unref (connection);

	return TRUE;
}
static void
nm_ip_up (void *data, int arg)
{
	guint32 pppd_made_up_address = htonl (0x0a404040 + ifunit);
	ipcp_options opts = ipcp_gotoptions[0];
	ipcp_options peer_opts = ipcp_hisoptions[0];
	GHashTable *hash;
	GArray *array;
	GValue *val;
	struct sockaddr_in addr;

	g_return_if_fail (DBUS_IS_G_PROXY (proxy));

	g_message ("nm-sstp-ppp-plugin: (%s): ip-up event", __func__);

	if (!opts.ouraddr) {
		g_warning ("nm-sstp-ppp-plugin: (%s): didn't receive an internal IP from pppd!", __func__);
		return;
	}

	hash = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, value_destroy);

	/* Request the address of the server sstpc connected to */
	if (0 == nm_sstp_getaddr(&addr))
	{
		/* This will eliminate the need to have nm-sstp-service
		 * insert a new entry for "gateway" as we have already set it.
		 */
		g_hash_table_insert (hash, NM_VPN_PLUGIN_IP4_CONFIG_EXT_GATEWAY,
							 uint_to_gvalue (addr.sin_addr.s_addr));
	}

	g_hash_table_insert (hash, NM_VPN_PLUGIN_IP4_CONFIG_TUNDEV, 
					 str_to_gvalue (ifname));

	/* Prefer the peer options remote address first, _unless_ pppd made the
	 * address up, at which point prefer the local options remote address,
	 * and if that's not right, use the made-up address as a last resort.
	 */
	if (peer_opts.hisaddr && (peer_opts.hisaddr != pppd_made_up_address)) {
		g_hash_table_insert (hash, NM_VPN_PLUGIN_IP4_CONFIG_PTP,
		                     uint_to_gvalue (peer_opts.hisaddr));
	} else if (opts.hisaddr) {
		g_hash_table_insert (hash, NM_VPN_PLUGIN_IP4_CONFIG_PTP,
		                     uint_to_gvalue (opts.hisaddr));
	} else if (peer_opts.hisaddr == pppd_made_up_address) {
		/* As a last resort, use the made-up address */
		g_hash_table_insert (hash, NM_VPN_PLUGIN_IP4_CONFIG_PTP,
		                     uint_to_gvalue (peer_opts.hisaddr));
	}

	g_hash_table_insert (hash, NM_VPN_PLUGIN_IP4_CONFIG_ADDRESS, 
					 uint_to_gvalue (opts.ouraddr));

	g_hash_table_insert (hash, NM_VPN_PLUGIN_IP4_CONFIG_PREFIX, uint_to_gvalue (32));

	if (opts.dnsaddr[0] || opts.dnsaddr[1]) {
		array = g_array_new (FALSE, FALSE, sizeof (guint32));

		if (opts.dnsaddr[0])
			g_array_append_val (array, opts.dnsaddr[0]);
		if (opts.dnsaddr[1])
			g_array_append_val (array, opts.dnsaddr[1]);

		val = g_slice_new0 (GValue);
		g_value_init (val, DBUS_TYPE_G_UINT_ARRAY);
		g_value_set_boxed (val, array);

		g_hash_table_insert (hash, NM_VPN_PLUGIN_IP4_CONFIG_DNS, val);
	}

	/* Default MTU to 1400, which is also what Windows XP/Vista use */
	g_hash_table_insert (hash, NM_VPN_PLUGIN_IP4_CONFIG_MTU, uint_to_gvalue (1400));

	g_message ("nm-sstp-ppp-plugin: (%s): sending Ip4Config to NetworkManager-sstp...", __func__);

	dbus_g_proxy_call_no_reply (proxy, "SetIp4Config",
	                            DBUS_TYPE_G_MAP_OF_VARIANT, hash, G_TYPE_INVALID,
	                            G_TYPE_INVALID);

	g_hash_table_destroy (hash);
}