Beispiel #1
0
/**
 * nmt_editor_new:
 * @connection: the #NMConnection to edit
 *
 * Creates a new #NmtEditor to edit @connection.
 *
 * Returns: a new #NmtEditor
 */
NmtNewtForm *
nmt_editor_new (NMConnection *connection)
{
	NMEditorConnectionTypeData *type_data;

	type_data = nm_editor_utils_get_connection_type_data (connection);
	if (!type_data) {
		NMSettingConnection *s_con;

		s_con = nm_connection_get_setting_connection (connection);
		if (s_con) {
			nmt_newt_message_dialog (_("Could not create editor for connection '%s' of type '%s'."),
			                         nm_connection_get_id (connection),
			                         nm_setting_connection_get_connection_type (s_con));
		} else {
			nmt_newt_message_dialog (_("Could not create editor for invalid connection '%s'."),
			                         nm_connection_get_id (connection));
		}

		return NULL;
	}

	return g_object_new (NMT_TYPE_EDITOR,
	                     "connection", connection,
	                     "type-data", type_data,
	                     "title", _("Edit connection"),
	                     "fullscreen-vertical", TRUE,
	                     NULL);
}
static GSList *
append_nmt_devices_for_vpns (GSList *nmt_devices,
                             GSList *connections)
{
	NmtConnectDevice *nmtdev;
	GSList *iter;
	NMConnection *conn;
	NmtConnectConnection *nmtconn;

	nmtdev = g_slice_new0 (NmtConnectDevice);
	nmtdev->name = g_strdup (_("VPN"));
	nmtdev->sort_order = 100;

	for (iter = connections; iter; iter = iter->next) {
		conn = iter->data;
		if (!nm_connection_is_type (conn, NM_SETTING_VPN_SETTING_NAME))
			continue;

		nmtconn = g_slice_new0 (NmtConnectConnection);
		nmtconn->name = nm_connection_get_id (conn);
		nmtconn->conn = g_object_ref (conn);

		nmtdev->conns = g_slist_insert_sorted (nmtdev->conns, nmtconn, sort_connections);
	}

	if (nmtdev->conns)
		nmt_devices = g_slist_prepend (nmt_devices, nmtdev);
	else
		nmt_connect_device_free (nmtdev);

	return nmt_devices;
}
static GSList *
append_nmt_devices_for_vpns (GSList          *nmt_devices,
                             const GPtrArray *connections)
{
	NmtConnectDevice *nmtdev;
	int i;
	NMConnection *conn;
	NmtConnectConnection *nmtconn;

	nmtdev = g_slice_new0 (NmtConnectDevice);
	nmtdev->name = g_strdup (_("VPN"));
	nmtdev->sort_order = 100;

	for (i = 0; i < connections->len; i++) {
		conn = connections->pdata[i];
		if (!nm_connection_is_type (conn, NM_SETTING_VPN_SETTING_NAME))
			continue;

		nmtconn = g_slice_new0 (NmtConnectConnection);
		nmtconn->name = nm_connection_get_id (conn);
		nmtconn->conn = g_object_ref (conn);

		nmtdev->conns = g_slist_insert_sorted (nmtdev->conns, nmtconn, sort_connections);
	}

	if (nmtdev->conns)
		nmt_devices = g_slist_prepend (nmt_devices, nmtdev);
	else
		nmt_connect_device_free (nmtdev);

	return nmt_devices;
}
Beispiel #4
0
static void
read_connections (SettingsPluginIbft *self)
{
	SettingsPluginIbftPrivate *priv = SETTINGS_PLUGIN_IBFT_GET_PRIVATE (self);
	GSList *blocks = NULL, *iter;
	GError *error = NULL;
	NMIbftConnection *connection;

	if (!read_ibft_blocks ("/sbin/iscsiadm", &blocks, &error)) {
		nm_log_dbg (LOGD_SETTINGS, "ibft: failed to read iscsiadm records: %s", error->message);
		g_error_free (error);
		return;
	}

	for (iter = blocks; iter; iter = iter->next) {
		connection = nm_ibft_connection_new (iter->data, &error);
		if (connection) {
			nm_log_info (LOGD_SETTINGS, "ibft: read connection '%s'",
			             nm_connection_get_id (NM_CONNECTION (connection)));
			g_hash_table_insert (priv->connections,
			                     g_strdup (nm_connection_get_uuid (NM_CONNECTION (connection))),
			                     connection);
		} else {
			nm_log_warn (LOGD_SETTINGS, "ibft: failed to read iscsiadm record: %s", error->message);
			g_clear_error (&error);
		}
	}

	g_slist_free_full (blocks, (GDestroyNotify) g_ptr_array_unref);
}
static GHashTable *
_create_keyring_add_attr_list (NMConnection *connection,
                               const char *setting_name,
                               const char *setting_key,
                               char **out_display_name)
{
	const char *connection_id, *connection_uuid;

	g_return_val_if_fail (connection != NULL, NULL);
	g_return_val_if_fail (setting_name != NULL, NULL);
	g_return_val_if_fail (setting_key != NULL, NULL);

	connection_uuid = nm_connection_get_uuid (connection);
	g_assert (connection_uuid);
	connection_id = nm_connection_get_id (connection);
	g_assert (connection_id);

	if (out_display_name) {
		*out_display_name = g_strdup_printf ("Network secret for %s/%s/%s",
		                                     connection_id,
		                                     setting_name,
		                                     setting_key);
	}

	return secret_attributes_build (&network_manager_secret_schema,
	                                KEYRING_UUID_TAG, connection_uuid,
	                                KEYRING_SN_TAG, setting_name,
	                                KEYRING_SK_TAG, setting_key,
	                                NULL);
}
char *
nm_device_ethernet_utils_get_default_wired_name (const GSList *connections)
{
	const GSList *iter;
	char *cname = NULL;
	int i = 0;

	/* Find the next available unique connection name */
	while (!cname && (i++ < 10000)) {
		char *temp;
		gboolean found = FALSE;

		temp = g_strdup_printf (_("Wired connection %d"), i);
		for (iter = connections; iter; iter = iter->next) {
			if (g_strcmp0 (nm_connection_get_id (NM_CONNECTION (iter->data)), temp) == 0) {
				found = TRUE;
				g_free (temp);
				break;
			}
		}

		if (found == FALSE)
			cname = temp;
	}

	return cname;
}
NMDevice *
nm_device_bridge_new_for_connection (NMConnection *connection)
{
	const char *iface;

	g_return_val_if_fail (connection != NULL, NULL);

	iface = nm_connection_get_virtual_iface_name (connection);
	g_return_val_if_fail (iface != NULL, NULL);

	if (   !nm_platform_bridge_add (iface)
	    && nm_platform_get_error () != NM_PLATFORM_ERROR_EXISTS) {
		nm_log_warn (LOGD_DEVICE | LOGD_BRIDGE, "(%s): failed to create bridge master interface for '%s': %s",
		             iface, nm_connection_get_id (connection),
		             nm_platform_get_error_msg ());
		return NULL;
	}

	return (NMDevice *) g_object_new (NM_TYPE_DEVICE_BRIDGE,
	                                  NM_DEVICE_IFACE, iface,
	                                  NM_DEVICE_DRIVER, "bridge",
	                                  NM_DEVICE_TYPE_DESC, "Bridge",
	                                  NM_DEVICE_DEVICE_TYPE, NM_DEVICE_TYPE_BRIDGE,
	                                  NM_DEVICE_IS_MASTER, TRUE,
	                                  NULL);
}
static NMDevice *
create_virtual_device_for_connection (NMDeviceFactory *factory,
                                      NMConnection *connection,
                                      NMDevice *parent,
                                      GError **error)
{
	const char *iface;

	if (!nm_connection_is_type (connection, NM_SETTING_BOND_SETTING_NAME))
		return NULL;

	iface = nm_connection_get_interface_name (connection);
	g_return_val_if_fail (iface != NULL, NULL);

	if (   !nm_platform_bond_add (iface)
		&& nm_platform_get_error () != NM_PLATFORM_ERROR_EXISTS) {
		nm_log_warn (LOGD_DEVICE | LOGD_BOND, "(%s): failed to create bonding master interface for '%s': %s",
			         iface, nm_connection_get_id (connection),
			         nm_platform_get_error_msg ());
		return NULL;
	}

	return (NMDevice *) g_object_new (NM_TYPE_DEVICE_BOND,
		                              NM_DEVICE_IFACE, iface,
		                              NM_DEVICE_DRIVER, "bonding",
		                              NM_DEVICE_TYPE_DESC, "Bond",
		                              NM_DEVICE_DEVICE_TYPE, NM_DEVICE_TYPE_BOND,
		                              NM_DEVICE_IS_MASTER, TRUE,
		                              NULL);
}
static char *
get_new_connection_name (const GSList *existing,
                         const char *preferred,
                         const char *fallback_prefix)
{
	GSList *names = NULL;
	const GSList *iter;
	char *cname = NULL;
	int i = 0;
	gboolean preferred_found = FALSE;

	g_assert (fallback_prefix);

	for (iter = existing; iter; iter = g_slist_next (iter)) {
		NMConnection *candidate = NM_CONNECTION (iter->data);
		const char *id;

		id = nm_connection_get_id (candidate);
		g_assert (id);
		names = g_slist_append (names, (gpointer) id);

		if (preferred && !preferred_found && (strcmp (preferred, id) == 0))
			preferred_found = TRUE;
	}

	/* Return the preferred name if it was unique */
	if (preferred && !preferred_found) {
		g_slist_free (names);
		return g_strdup (preferred);
	}

	/* Otherwise find the next available unique connection name using the given
	 * connection name template.
	 */
	while (!cname && (i++ < 10000)) {
		char *temp;
		gboolean found = FALSE;

		/* Translators: the first %s is a prefix for the connection id, such
		 * as "Wired Connection" or "VPN Connection". The %d is a number
		 * that is combined with the first argument to create a unique
		 * connection id. */
		temp = g_strdup_printf (C_("connection id fallback", "%s %d"),
		                        fallback_prefix, i);
		for (iter = names; iter; iter = g_slist_next (iter)) {
			if (!strcmp (iter->data, temp)) {
				found = TRUE;
				break;
			}
		}
		if (!found)
			cname = temp;
		else
			g_free (temp);
	}

	g_slist_free (names);
	return cname;
}
static void
add_connections_for_aps (NmtConnectDevice *nmtdev,
                         const GPtrArray  *connections)
{
	NmtConnectConnection *nmtconn;
	NMConnection *conn;
	NMAccessPoint *ap;
	const GPtrArray *aps;
	GHashTable *seen_ssids;
	GBytes *ssid;
	char *ap_hash;
	int i, c;

	aps = nm_device_wifi_get_access_points (NM_DEVICE_WIFI (nmtdev->device));
	if (!aps->len)
		return;

	seen_ssids = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);

	for (i = 0; i < aps->len; i++) {
		ap = aps->pdata[i];

		if (!nm_access_point_get_ssid (ap))
			continue;

		ap_hash = hash_ap (ap);
		if (g_hash_table_contains (seen_ssids, ap_hash)) {
			g_free (ap_hash);
			continue;
		}
		g_hash_table_add (seen_ssids, ap_hash);

		nmtconn = g_slice_new0 (NmtConnectConnection);
		nmtconn->device = nmtdev->device;
		nmtconn->ap = g_object_ref (ap);
		ssid = nm_access_point_get_ssid (ap);
		if (ssid)
			nmtconn->ssid = nm_utils_ssid_to_utf8 (g_bytes_get_data (ssid, NULL),
			                                       g_bytes_get_size (ssid));

		for (c = 0; c < connections->len; c++) {
			conn = connections->pdata[c];
			if (   nm_device_connection_valid (nmtdev->device, conn)
			    && nm_access_point_connection_valid (ap, conn)) {
				nmtconn->name = nm_connection_get_id (conn);
				nmtconn->conn = g_object_ref (conn);
				break;
			}
		}

		if (!nmtconn->name)
			nmtconn->name = nmtconn->ssid ? nmtconn->ssid : "<unknown>";

		nmtdev->conns = g_slist_prepend (nmtdev->conns, nmtconn);
	}

	g_hash_table_unref (seen_ssids);
}
static void
add_connections_for_aps (NmtConnectDevice *nmtdev,
                         GSList           *connections)
{
	NmtConnectConnection *nmtconn;
	NMConnection *conn;
	NMAccessPoint *ap;
	const GPtrArray *aps;
	GHashTable *seen_ssids;
	char *ap_hash;
	GSList *iter;
	int i;

	aps = nm_device_wifi_get_access_points (NM_DEVICE_WIFI (nmtdev->device));
	if (!aps)
		return;

	seen_ssids = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);

	for (i = 0; i < aps->len; i++) {
		ap = aps->pdata[i];

		if (!nm_access_point_get_ssid (ap))
			continue;

		ap_hash = hash_ap (ap);
		if (g_hash_table_contains (seen_ssids, ap_hash)) {
			g_free (ap_hash);
			continue;
		}
		g_hash_table_add (seen_ssids, ap_hash);

		nmtconn = g_slice_new0 (NmtConnectConnection);
		nmtconn->device = nmtdev->device;
		nmtconn->ap = g_object_ref (ap);
		nmtconn->ssid = nm_utils_ssid_to_utf8 (nm_access_point_get_ssid (ap));

		for (iter = connections; iter; iter = iter->next) {
			conn = iter->data;
			if (   nm_device_connection_valid (nmtdev->device, conn)
			    && nm_access_point_connection_valid (ap, conn)) {
				nmtconn->name = nm_connection_get_id (conn);
				nmtconn->conn = g_object_ref (conn);
				break;
			}
		}

		if (!iter)
			nmtconn->name = nmtconn->ssid;

		nmtdev->conns = g_slist_prepend (nmtdev->conns, nmtconn);
	}

	g_hash_table_unref (seen_ssids);
}
static gint
id_sort_func (GtkTreeModel *model, GtkTreeIter *a, GtkTreeIter *b, gpointer user_data)
{
	NMConnection *conn_a, *conn_b;
	gint ret;

	gtk_tree_model_get (model, a, COL_CONNECTION, &conn_a, -1);
	gtk_tree_model_get (model, b, COL_CONNECTION, &conn_b, -1);

	if (!conn_a || !conn_b) {
		g_assert (!conn_a && !conn_b);
		return sort_connection_types (model, a, b, user_data);
	}

	ret = strcmp (nm_connection_get_id (conn_a), nm_connection_get_id (conn_b));
	g_object_unref (conn_a);
	g_object_unref (conn_b);

	return ret;
}
const char *
nm_active_connection_get_settings_connection_id (NMActiveConnection *self)
{
	NMSettingsConnection *con;

	g_return_val_if_fail (NM_IS_ACTIVE_CONNECTION (self), NULL);

	con = NM_ACTIVE_CONNECTION_GET_PRIVATE (self)->settings_connection;
	return con
	       ? nm_connection_get_id (NM_CONNECTION (con))
	       : NULL;
}
static char *
get_new_connection_name (const GSList *existing,
                         const char *format,
                         const char *preferred)
{
	GSList *names = NULL;
	const GSList *iter;
	char *cname = NULL;
	int i = 0;
	gboolean preferred_found = FALSE;

	for (iter = existing; iter; iter = g_slist_next (iter)) {
		NMConnection *candidate = NM_CONNECTION (iter->data);
		const char *id;

		id = nm_connection_get_id (candidate);
		g_assert (id);
		names = g_slist_append (names, (gpointer) id);

		if (preferred && !preferred_found && (strcmp (preferred, id) == 0))
			preferred_found = TRUE;
	}

	/* Return the preferred name if it was unique */
	if (preferred && !preferred_found) {
		g_slist_free (names);
		return g_strdup (preferred);
	}

	/* Otherwise find the next available unique connection name using the given
	 * connection name template.
	 */
	while (!cname && (i++ < 10000)) {
		char *temp;
		gboolean found = FALSE;

		temp = g_strdup_printf (format, i);
		for (iter = names; iter; iter = g_slist_next (iter)) {
			if (!strcmp (iter->data, temp)) {
				found = TRUE;
				break;
			}
		}
		if (!found)
			cname = temp;
		else
			g_free (temp);
	}

	g_slist_free (names);
	return cname;
}
Beispiel #15
0
static gboolean
create_and_realize (NMDevice *device,
                    NMConnection *connection,
                    NMDevice *parent,
                    NMPlatformLink *out_plink,
                    GError **error)
{
	NMDeviceVlanPrivate *priv = NM_DEVICE_VLAN_GET_PRIVATE (device);
	const char *iface = nm_device_get_iface (device);
	NMSettingVlan *s_vlan;
	int parent_ifindex, vlan_id;
	NMPlatformError plerr;

	g_assert (out_plink);

	s_vlan = nm_connection_get_setting_vlan (connection);
	g_assert (s_vlan);

	if (!nm_device_supports_vlans (parent)) {
		g_set_error (error, NM_DEVICE_ERROR, NM_DEVICE_ERROR_FAILED,
		             "no support for VLANs on interface %s of type %s",
		             nm_device_get_iface (parent),
		             nm_device_get_type_desc (parent));
		return FALSE;
	}

	parent_ifindex = nm_device_get_ifindex (parent);
	g_warn_if_fail (parent_ifindex > 0);

	vlan_id = nm_setting_vlan_get_id (s_vlan);

	plerr = nm_platform_vlan_add (NM_PLATFORM_GET,
	                              iface,
	                              parent_ifindex,
	                              vlan_id,
	                              nm_setting_vlan_get_flags (s_vlan),
	                              out_plink);
	if (plerr != NM_PLATFORM_ERROR_SUCCESS && plerr != NM_PLATFORM_ERROR_EXISTS) {
		g_set_error (error, NM_DEVICE_ERROR, NM_DEVICE_ERROR_CREATION_FAILED,
		             "Failed to create VLAN interface '%s' for '%s': %s",
		             iface,
		             nm_connection_get_id (connection),
		             nm_platform_error_to_string (plerr));
		return FALSE;
	}

	g_warn_if_fail (priv->parent == NULL);
	nm_device_vlan_set_parent (NM_DEVICE_VLAN (device), parent);
	priv->vlan_id = vlan_id;

	return TRUE;
}
Beispiel #16
0
static NMIfcfgConnection *
_internal_new_connection (SCPluginIfcfg *self,
                          const char *path,
                          NMConnection *source,
                          GError **error)
{
    SCPluginIfcfgPrivate *priv = SC_PLUGIN_IFCFG_GET_PRIVATE (self);
    NMIfcfgConnection *connection;
    const char *cid;
    GError *local = NULL;
    gboolean ignore_error = FALSE;

    if (!source) {
        PLUGIN_PRINT (IFCFG_PLUGIN_NAME, "parsing %s ... ", path);
    }

    connection = nm_ifcfg_connection_new (path, source, &local, &ignore_error);
    if (!connection) {
        if (!ignore_error) {
            PLUGIN_PRINT (IFCFG_PLUGIN_NAME, "    error: %s",
                          (local && local->message) ? local->message : "(unknown)");
        }
        g_propagate_error (error, local);
        return NULL;
    }

    cid = nm_connection_get_id (NM_CONNECTION (connection));
    g_assert (cid);

    g_hash_table_insert (priv->connections,
                         (gpointer) nm_ifcfg_connection_get_path (connection),
                         connection);
    PLUGIN_PRINT (IFCFG_PLUGIN_NAME, "    read connection '%s'", cid);

    if (nm_ifcfg_connection_get_unmanaged_spec (connection)) {
        PLUGIN_PRINT (IFCFG_PLUGIN_NAME, "Ignoring connection '%s' and its "
                      "device due to NM_CONTROLLED/BRIDGE/VLAN.", cid);
    } else {
        /* Wait for the connection to become unmanaged once it knows the
         * hardware IDs of its device, if/when the device gets plugged in.
         */
        g_signal_connect (G_OBJECT (connection), "notify::" NM_IFCFG_CONNECTION_UNMANAGED,
                          G_CALLBACK (connection_unmanaged_changed), self);
    }

    /* watch changes of ifcfg hardlinks */
    g_signal_connect (G_OBJECT (connection), "ifcfg-changed",
                      G_CALLBACK (connection_ifcfg_changed), self);

    return connection;
}
static gboolean
create_and_realize (NMDevice *device,
                    NMConnection *connection,
                    NMDevice *parent,
                    const NMPlatformLink **out_plink,
                    GError **error)
{
	const char *iface = nm_device_get_iface (device);
	NMPlatformError plerr;
	NMSettingMacvlan *s_macvlan;
	NMPlatformLnkMacvlan lnk = { };
	int parent_ifindex;

	s_macvlan = nm_connection_get_setting_macvlan (connection);
	g_assert (s_macvlan);

	if (!parent) {
		g_set_error (error, NM_DEVICE_ERROR, NM_DEVICE_ERROR_FAILED,
		             "MACVLAN devices can not be created without a parent interface");
		return FALSE;
	}

	parent_ifindex = nm_device_get_ifindex (parent);
	g_warn_if_fail (parent_ifindex > 0);

	lnk.mode = setting_mode_to_platform (nm_setting_macvlan_get_mode (s_macvlan));
	if (!lnk.mode) {
		g_set_error (error, NM_DEVICE_ERROR, NM_DEVICE_ERROR_FAILED,
		             "unsupported MACVLAN mode %u in connection %s",
		             nm_setting_macvlan_get_mode (s_macvlan),
		             nm_connection_get_uuid (connection));
		return FALSE;
	}
	lnk.no_promisc = !nm_setting_macvlan_get_promiscuous (s_macvlan);
	lnk.tap = nm_setting_macvlan_get_tap (s_macvlan);

	plerr = nm_platform_link_macvlan_add (NM_PLATFORM_GET, iface, parent_ifindex, &lnk, out_plink);
	if (plerr != NM_PLATFORM_ERROR_SUCCESS) {
		g_set_error (error, NM_DEVICE_ERROR, NM_DEVICE_ERROR_CREATION_FAILED,
		             "Failed to create %s interface '%s' for '%s': %s",
		             lnk.tap ? "macvtap" : "macvlan",
		             iface,
		             nm_connection_get_id (connection),
		             nm_platform_error_to_string (plerr));
		return FALSE;
	}

	return TRUE;
}
Beispiel #18
0
/*
 * nmc_find_connection:
 * @connections: array of NMConnections to search in
 * @filter_type: "id", "uuid", "path" or %NULL
 * @filter_val: connection to find (connection name, UUID or path)
 * @start: where to start in @list. The location is updated so that the function
 *   can be called multiple times (for connections with the same name).
 *
 * Find a connection in @list according to @filter_val. @filter_type determines
 * what property is used for comparison. When @filter_type is NULL, compare
 * @filter_val against all types. Otherwise, only compare against the specified
 * type. If 'path' filter type is specified, comparison against numeric index
 * (in addition to the whole path) is allowed.
 *
 * Returns: found connection, or %NULL
 */
NMConnection *
nmc_find_connection (const GPtrArray *connections,
                     const char *filter_type,
                     const char *filter_val,
                     int *start)
{
	NMConnection *connection;
	NMConnection *found = NULL;
	int i;
	const char *id;
	const char *uuid;
	const char *path, *path_num;

	for (i = start ? *start : 0; i < connections->len; i++) {
		connection = NM_CONNECTION (connections->pdata[i]);

		id = nm_connection_get_id (connection);
		uuid = nm_connection_get_uuid (connection);
		path = nm_connection_get_path (connection);
		path_num = path ? strrchr (path, '/') + 1 : NULL;

		/* When filter_type is NULL, compare connection ID (filter_val)
		 * against all types. Otherwise, only compare against the specific
		 * type. If 'path' filter type is specified, comparison against
		 * numeric index (in addition to the whole path) is allowed.
		 */
		if (   (   (!filter_type || strcmp (filter_type, "id")  == 0)
		        && strcmp (filter_val, id) == 0)
		    || (   (!filter_type || strcmp (filter_type, "uuid") == 0)
		        && strcmp (filter_val, uuid) == 0)
		    || (   (!filter_type || strcmp (filter_type, "path") == 0)
		        && (g_strcmp0 (filter_val, path) == 0 || (filter_type && g_strcmp0 (filter_val, path_num) == 0)))) {
			if (!start)
				return connection;
			if (found) {
				*start = i;
				return found;
			}
			found = connection;
		}
	}

	if (start)
		*start = 0;
	return found;
}
NMDevice *
nm_device_vlan_new_for_connection (NMConnection *connection, NMDevice *parent)
{
	NMDevice *device;
	NMSettingVlan *s_vlan;
	char *iface;

	g_return_val_if_fail (connection != NULL, NULL);
	g_return_val_if_fail (NM_IS_DEVICE (parent), NULL);

	s_vlan = nm_connection_get_setting_vlan (connection);
	g_return_val_if_fail (s_vlan != NULL, NULL);

	iface = g_strdup (nm_connection_get_interface_name (connection));
	if (!iface) {
		iface = nm_utils_new_vlan_name (nm_device_get_ip_iface (parent),
		                                nm_setting_vlan_get_id (s_vlan));
	}

	if (   !nm_platform_vlan_add (iface,
	                              nm_device_get_ifindex (parent),
	                              nm_setting_vlan_get_id (s_vlan),
	                              nm_setting_vlan_get_flags (s_vlan))
	    && nm_platform_get_error () != NM_PLATFORM_ERROR_EXISTS) {
		nm_log_warn (LOGD_DEVICE | LOGD_VLAN, "(%s) failed to add VLAN interface for '%s'",
		             iface, nm_connection_get_id (connection));
		g_free (iface);
		return NULL;
	}

	device = (NMDevice *) g_object_new (NM_TYPE_DEVICE_VLAN,
	                                    NM_DEVICE_IFACE, iface,
	                                    NM_DEVICE_VLAN_PARENT, parent,
	                                    NM_DEVICE_DRIVER, "8021q",
	                                    NM_DEVICE_TYPE_DESC, "VLAN",
	                                    NM_DEVICE_DEVICE_TYPE, NM_DEVICE_TYPE_VLAN,
	                                    NULL);
	g_free (iface);
	if (NM_DEVICE_VLAN_GET_PRIVATE (device)->invalid) {
		g_object_unref (device);
		device = NULL;
	}

	return device;
}
static GSList *
append_nmt_devices_for_virtual_devices (GSList          *nmt_devices,
                                        const GPtrArray *connections)
{
	NmtConnectDevice *nmtdev = NULL;
	int i;
	GHashTable *devices_by_name;
	char *name;
	NMConnection *conn;
	NmtConnectConnection *nmtconn;
	int sort_order;

	devices_by_name = g_hash_table_new (g_str_hash, g_str_equal);

	for (i = 0; i < connections->len; i++) {
		conn = connections->pdata[i];
		sort_order = get_sort_order_for_connection (conn);
		if (sort_order == -1)
			continue;

		name = nm_connection_get_virtual_device_description (conn);
		if (name)
			nmtdev = g_hash_table_lookup (devices_by_name, name);
		if (nmtdev)
			g_free (name);
		else {
			nmtdev = g_slice_new0 (NmtConnectDevice);
			nmtdev->name = name ? name : g_strdup ("Unknown");
			nmtdev->sort_order = sort_order;

			g_hash_table_insert (devices_by_name, nmtdev->name, nmtdev);
			nmt_devices = g_slist_prepend (nmt_devices, nmtdev);
		}

		nmtconn = g_slice_new0 (NmtConnectConnection);
		nmtconn->name = nm_connection_get_id (conn);
		nmtconn->conn = g_object_ref (conn);

		nmtdev->conns = g_slist_insert_sorted (nmtdev->conns, nmtconn, sort_connections);
	}

	g_hash_table_destroy (devices_by_name);
	return nmt_devices;
}
static gboolean
create_and_realize (NMDevice *device,
                    NMConnection *connection,
                    NMDevice *parent,
                    const NMPlatformLink **out_plink,
                    GError **error)
{
	NMSettingBridge *s_bridge;
	const char *iface = nm_device_get_iface (device);
	const char *hwaddr;
	guint8 mac_address[NM_UTILS_HWADDR_LEN_MAX];
	NMPlatformError plerr;

	g_assert (iface);

	s_bridge = nm_connection_get_setting_bridge (connection);
	g_assert (s_bridge);
	hwaddr = nm_setting_bridge_get_mac_address (s_bridge);
	if (hwaddr) {
		if (!nm_utils_hwaddr_aton (hwaddr, mac_address, ETH_ALEN)) {
			g_set_error (error, NM_DEVICE_ERROR, NM_DEVICE_ERROR_FAILED,
			             "Invalid hardware address '%s'",
			             hwaddr);
			return FALSE;
		}
	}

	plerr = nm_platform_link_bridge_add (NM_PLATFORM_GET,
	                                     iface,
	                                     hwaddr ? mac_address : NULL,
	                                     hwaddr ? ETH_ALEN : 0,
	                                     out_plink);
	if (plerr != NM_PLATFORM_ERROR_SUCCESS) {
		g_set_error (error, NM_DEVICE_ERROR, NM_DEVICE_ERROR_CREATION_FAILED,
		             "Failed to create bridge interface '%s' for '%s': %s",
		             iface,
		             nm_connection_get_id (connection),
		             nm_platform_error_to_string (plerr));
		return FALSE;
	}

	return TRUE;
}
Beispiel #22
0
void
nmt_remove_connection (NMRemoteConnection *connection)
{
	const GPtrArray *all_conns;
	GSList *slaves, *iter;
	int i;
	NMRemoteConnection *slave;
	NMSettingConnection *s_con;
	const char *uuid, *iface, *master;
	int choice;

	choice = nmt_newt_choice_dialog (_("Cancel"),
	                                 _("Delete"),
	                                 _("Are you sure you want to delete the connection '%s'?"),
	                                 nm_connection_get_id (NM_CONNECTION (connection)));
	if (choice == 1)
		return;

	g_object_ref (connection);
	remove_one_connection (connection);

	uuid = nm_connection_get_uuid (NM_CONNECTION (connection));
	iface = nm_connection_get_interface_name (NM_CONNECTION (connection));

	all_conns = nm_client_get_connections (nm_client);
	slaves = NULL;
	for (i = 0; i < all_conns->len; i++) {
		slave = all_conns->pdata[i];
		s_con = nm_connection_get_setting_connection (NM_CONNECTION (slave));
		master = nm_setting_connection_get_master (s_con);
		if (master) {
			if (!g_strcmp0 (master, uuid) || !g_strcmp0 (master, iface))
				slaves = g_slist_prepend (slaves, g_object_ref (slave));
		}
	}

	for (iter = slaves; iter; iter = iter->next)
		remove_one_connection (iter->data);
	g_slist_free_full (slaves, g_object_unref);

	g_object_unref (connection);
}
Beispiel #23
0
static void
read_connections (NMSystemConfigInterface *config)
{
	SCPluginKeyfile *self = SC_PLUGIN_KEYFILE (config);
	GDir *dir;
	GError *error = NULL;
	const char *item;

	dir = g_dir_open (KEYFILE_DIR, 0, &error);
	if (!dir) {
		PLUGIN_WARN (KEYFILE_PLUGIN_NAME, "Cannot read directory '%s': (%d) %s",
		             KEYFILE_DIR,
		             error ? error->code : -1,
		             error && error->message ? error->message : "(unknown)");
		g_clear_error (&error);
		return;
	}

	while ((item = g_dir_read_name (dir))) {
		NMSettingsConnection *connection;
		char *full_path;

		if (nm_keyfile_plugin_utils_should_ignore_file (item))
			continue;

		full_path = g_build_filename (KEYFILE_DIR, item, NULL);
		PLUGIN_PRINT (KEYFILE_PLUGIN_NAME, "parsing %s ... ", item);

		connection = _internal_new_connection (self, full_path, NULL, &error);
		if (connection) {
			PLUGIN_PRINT (KEYFILE_PLUGIN_NAME, "    read connection '%s'",
			              nm_connection_get_id (NM_CONNECTION (connection)));
		} else {
			PLUGIN_PRINT (KEYFILE_PLUGIN_NAME, "    error: %s",
				          (error && error->message) ? error->message : "(unknown)");
		}
		g_clear_error (&error);
		g_free (full_path);
	}
	g_dir_close (dir);
}
Beispiel #24
0
/* Read each file in our config directory and try to create a new
 * NMExamplePlugin for it.
 */
static void
read_connections (NMSystemConfigInterface *config)
{
	SCPluginExample *self = SC_PLUGIN_EXAMPLE (config);
	GDir *dir;
	GError *error = NULL;
	const char *item;

	dir = g_dir_open (EXAMPLE_DIR, 0, &error);
	if (!dir) {
		nm_log_warn (LOGD_SETTINGS, "Cannot read directory '%s': (%d) %s",
		             EXAMPLE_DIR,
		             error ? error->code : -1,
		             error && error->message ? error->message : "(unknown)");
		g_clear_error (&error);
		return;
	}

	while ((item = g_dir_read_name (dir))) {
		NMSettingsConnection *connection;
		char *full_path;

		/* XXX: Check file extension and ignore "~", ".tmp", ".bak", etc */

		full_path = g_build_filename (EXAMPLE_DIR, item, NULL);
		nm_log_info (LOGD_SETTINGS, "parsing %s ... ", item);

		connection = _internal_new_connection (self, full_path, NULL, &error);
		if (connection) {
			nm_log_info (LOGD_SETTINGS, "    read connection '%s'",
			             nm_connection_get_id (NM_CONNECTION (connection)));
		} else {
			nm_log_info (LOGD_SETTINGS, "    error: %s",
			             (error && error->message) ? error->message : "(unknown)");
		}
		g_clear_error (&error);
		g_free (full_path);
	}
	g_dir_close (dir);
}
static gboolean
create_and_realize (NMDevice *device,
                    NMConnection *connection,
                    NMDevice *parent,
                    const NMPlatformLink **out_plink,
                    GError **error)
{
	const char *iface = nm_device_get_iface (device);
	NMPlatformError plerr;

	plerr = nm_platform_link_team_add (NM_PLATFORM_GET, iface, out_plink);
	if (plerr != NM_PLATFORM_ERROR_SUCCESS) {
		g_set_error (error, NM_DEVICE_ERROR, NM_DEVICE_ERROR_CREATION_FAILED,
		             "Failed to create team master interface '%s' for '%s': %s",
		             iface,
		             nm_connection_get_id (connection),
		             nm_platform_error_to_string (plerr));
		return FALSE;
	}

	return TRUE;
}
char *
ce_page_get_next_available_name (const GPtrArray *connections, const char *format)
{
	GSList *names = NULL, *iter;
	char *cname = NULL;
	int i = 0;

	for (i = 0; i < connections->len; i++) {
		const char *id;

		id = nm_connection_get_id (connections->pdata[i]);
		g_assert (id);
		names = g_slist_append (names, (gpointer) id);
	}

	/* Find the next available unique connection name */
	for (i = 1; !cname && i < 10000; i++) {
		char *temp;
		gboolean found = FALSE;

		NM_PRAGMA_WARNING_DISABLE("-Wformat-nonliteral")
		temp = g_strdup_printf (format, i);
		NM_PRAGMA_WARNING_REENABLE
		for (iter = names; iter; iter = g_slist_next (iter)) {
			if (!strcmp (iter->data, temp)) {
				found = TRUE;
				break;
			}
		}
		if (!found)
			cname = temp;
		else
			g_free (temp);
	}

	g_slist_free (names);
	return cname;
}
static NMDevice *
create_virtual_device_for_connection (NMDeviceFactory *factory,
                                      NMConnection *connection,
                                      NMDevice *parent,
                                      GError **error)
{
	NMSettingInfiniband *s_infiniband;
	int p_key, parent_ifindex;
	const char *iface;

	if (!nm_connection_is_type (connection, NM_SETTING_INFINIBAND_SETTING_NAME))
		return NULL;

	g_return_val_if_fail (NM_IS_DEVICE_INFINIBAND (parent), NULL);

	s_infiniband = nm_connection_get_setting_infiniband (connection);

	iface = nm_setting_infiniband_get_virtual_interface_name (s_infiniband);
	g_return_val_if_fail (iface != NULL, NULL);

	parent_ifindex = nm_device_get_ifindex (parent);
	p_key = nm_setting_infiniband_get_p_key (s_infiniband);

	if (   !nm_platform_infiniband_partition_add (parent_ifindex, p_key)
	    && nm_platform_get_error () != NM_PLATFORM_ERROR_EXISTS) {
		nm_log_warn (LOGD_DEVICE | LOGD_INFINIBAND, "(%s): failed to add InfiniBand P_Key interface for '%s': %s",
		             iface, nm_connection_get_id (connection),
		             nm_platform_get_error_msg ());
		return NULL;
	}

	return (NMDevice *) g_object_new (NM_TYPE_DEVICE_INFINIBAND,
	                                  NM_DEVICE_IFACE, iface,
	                                  NM_DEVICE_DRIVER, nm_device_get_driver (parent),
	                                  NM_DEVICE_TYPE_DESC, "InfiniBand",
	                                  NM_DEVICE_DEVICE_TYPE, NM_DEVICE_TYPE_INFINIBAND,
	                                  NULL);
}
Beispiel #28
0
char *
nmc_unique_connection_name (const GPtrArray *connections, const char *try_name)
{
	NMConnection *connection;
	const char *name;
	char *new_name;
	unsigned int num = 1;
	int i = 0;

	new_name = g_strdup (try_name);
	while (i < connections->len) {
		connection = NM_CONNECTION (connections->pdata[i]);

		name = nm_connection_get_id (connection);
		if (g_strcmp0 (new_name, name) == 0) {
			g_free (new_name);
			new_name = g_strdup_printf ("%s-%d", try_name, num++);
			i = 0;
		} else
			i++;
	}
	return new_name;
}
Beispiel #29
0
static void
remove_one_connection (NMRemoteConnection *connection)
{
	ConnectionDeleteData data;
	GError *error = NULL;

	data.got_callback = data.got_signal = FALSE;
	nmt_sync_op_init (&data.op);

	data.connection = connection;
	g_signal_connect (nm_client, NM_CLIENT_CONNECTION_REMOVED,
	                  G_CALLBACK (connection_removed_signal), &data);
	nm_remote_connection_delete_async (connection, NULL, connection_deleted_callback, &data);

	if (!nmt_sync_op_wait_boolean (&data.op, &error)) {
		nmt_newt_message_dialog (_("Could not delete connection '%s': %s"),
		                         nm_connection_get_id (NM_CONNECTION (connection)),
		                         error->message);
		g_error_free (error);
	}

	g_signal_handlers_disconnect_by_func (nm_client, G_CALLBACK (connection_removed_signal), &data);
}
static void
dispose (GObject *object)
{
	NMBluezDevice *self = NM_BLUEZ_DEVICE (object);
	NMBluezDevicePrivate *priv = NM_BLUEZ_DEVICE_GET_PRIVATE (self);
	NMConnection *to_delete = NULL;

	if (priv->pan_connection) {
		/* Check whether we want to remove the created connection. If so, we take a reference
		 * and delete it at the end of dispose(). */
		if (   nm_settings_connection_get_unsaved (NM_SETTINGS_CONNECTION (priv->pan_connection))
		    && nm_connection_compare (priv->pan_connection, priv->pan_connection_original, NM_SETTING_COMPARE_FLAG_EXACT))
			to_delete = g_object_ref (priv->pan_connection);

		priv->pan_connection = NULL;
		g_clear_object (&priv->pan_connection_original);
	}

	g_signal_handlers_disconnect_by_func (priv->provider, cp_connection_added, self);
	g_signal_handlers_disconnect_by_func (priv->provider, cp_connection_removed, self);
	g_signal_handlers_disconnect_by_func (priv->provider, cp_connection_updated, self);

	g_slist_free_full (priv->connections, g_object_unref);
	priv->connections = NULL;

	g_clear_object (&priv->adapter5);
	g_clear_object (&priv->dbus_connection);

	G_OBJECT_CLASS (nm_bluez_device_parent_class)->dispose (object);

	if (to_delete) {
		nm_log_dbg (LOGD_BT, "bluez[%s] removing Bluetooth connection for NAP device: '%s' (%s)", priv->path,
		            nm_connection_get_id (to_delete), nm_connection_get_uuid (to_delete));
		nm_settings_connection_delete (NM_SETTINGS_CONNECTION (to_delete), NULL, NULL);
		g_object_unref (to_delete);
	}
}