static GSList *
get_connections (NMSystemConfigInterface *config)
{
	SCPluginIfcfg *plugin = SC_PLUGIN_IFCFG (config);
	SCPluginIfcfgPrivate *priv = SC_PLUGIN_IFCFG_GET_PRIVATE (plugin);
	GSList *list = NULL;
	GHashTableIter iter;
	NMIfcfgConnection *connection;

	if (!priv->initialized) {
		if (nm_config_get_monitor_connection_files (nm_config_get ()))
			setup_ifcfg_monitoring (plugin);
		read_connections (plugin);
		priv->initialized = TRUE;
	}

	g_hash_table_iter_init (&iter, priv->connections);
	while (g_hash_table_iter_next (&iter, NULL, (gpointer) &connection)) {
		if (   !nm_ifcfg_connection_get_unmanaged_spec (connection)
		    && !nm_ifcfg_connection_get_unrecognized_spec (connection))
			list = g_slist_prepend (list, connection);
	}

	return list;
}
Exemplo n.º 2
0
void
nm_main_config_reload (int signal)
{
	nm_log_info (LOGD_CORE, "reload configuration (signal %s)...", strsignal (signal));
	/* The signal handler thread is only installed after
	 * creating NMConfig instance, and on shut down we
	 * no longer run the mainloop (to reach this point).
	 *
	 * Hence, a NMConfig singleton instance must always be
	 * available. */
	nm_config_reload (nm_config_get (), signal);
}
Exemplo n.º 3
0
static void
nm_audit_manager_init (NMAuditManager *self)
{
#if HAVE_LIBAUDIT
	NMAuditManagerPrivate *priv = NM_AUDIT_MANAGER_GET_PRIVATE (self);

	priv->config = g_object_ref (nm_config_get ());
	g_signal_connect (G_OBJECT (priv->config),
	                  NM_CONFIG_SIGNAL_CONFIG_CHANGED,
	                  G_CALLBACK (config_changed_cb),
	                  self);
	priv->auditd_fd = -1;

	init_auditd (self);
#endif
}
Exemplo n.º 4
0
NMConnectivity *
nm_connectivity_new (void)
{
	NMConnectivity *self;
	NMConfig *config;
	const char *check_response;

	config = nm_config_get ();
	check_response = nm_config_get_connectivity_response (config);

	self = g_object_new (NM_TYPE_CONNECTIVITY,
	                     NM_CONNECTIVITY_URI, nm_config_get_connectivity_uri (config),
	                     NM_CONNECTIVITY_INTERVAL, nm_config_get_connectivity_interval (config),
	                     NM_CONNECTIVITY_RESPONSE, check_response ? check_response : DEFAULT_RESPONSE,
	                     NULL);
	g_return_val_if_fail (self != NULL, NULL);
	update_state (self, NM_CONNECTIVITY_NONE);

	return self;
}
Exemplo n.º 5
0
static void
nm_dhcp_manager_init (NMDhcpManager *self)
{
	NMDhcpManagerPrivate *priv = NM_DHCP_MANAGER_GET_PRIVATE (self);
	NMConfig *config = nm_config_get ();
	const char *client;
	GError *error = NULL;
	GSList *iter;

	for (iter = client_descs; iter; iter = iter->next) {
		ClientDesc *desc = iter->data;

		nm_log_dbg (LOGD_DHCP, "Registered DHCP client '%s' (%s)",
		            desc->name, g_type_name (desc->gtype));
	}

	/* Client-specific setup */
	client = nm_config_get_dhcp_client (config);
	if (nm_config_get_configure_and_quit (config)) {
		if (g_strcmp0 (client, "internal") != 0)
			nm_log_warn (LOGD_DHCP, "Using internal DHCP client since configure-and-quit is set.");
		client = "internal";
	}

	priv->client_type = get_client_type (client, &error);
	if (priv->client_type == G_TYPE_INVALID) {
		nm_log_warn (LOGD_DHCP, "No usable DHCP client found (%s)! DHCP configurations will fail.",
		             error->message);
	} else {
		nm_log_dbg (LOGD_DHCP, "Using DHCP client '%s'", find_client_desc (NULL, priv->client_type)->name);

	}
	g_clear_error (&error);

	priv->clients = g_hash_table_new_full (g_direct_hash, g_direct_equal,
	                                       NULL,
	                                       (GDestroyNotify) g_object_unref);
}
Exemplo n.º 6
0
GObject *
nm_settings_keyfile_plugin_new (void)
{
	static SCPluginKeyfile *singleton = NULL;
	SCPluginKeyfilePrivate *priv;
	char *value;

	if (!singleton) {
		singleton = SC_PLUGIN_KEYFILE (g_object_new (SC_TYPE_PLUGIN_KEYFILE, NULL));
		priv = SC_PLUGIN_KEYFILE_GET_PRIVATE (singleton);

		priv->config = g_object_ref (nm_config_get ());
		value = nm_config_data_get_value (nm_config_get_data (priv->config),
		                                  "keyfile", "hostname", NULL);
		if (value) {
			nm_log_warn (LOGD_SETTINGS, "keyfile: 'hostname' option is deprecated and has no effect");
			g_free (value);
		}
	} else
		g_object_ref (singleton);

	return G_OBJECT (singleton);
}
Exemplo n.º 7
0
static void
setup_monitoring (NMSystemConfigInterface *config)
{
	SCPluginKeyfilePrivate *priv = SC_PLUGIN_KEYFILE_GET_PRIVATE (config);
	GFile *file;
	GFileMonitor *monitor;

	if (nm_config_get_monitor_connection_files (nm_config_get ())) {
		file = g_file_new_for_path (KEYFILE_DIR);
		monitor = g_file_monitor_directory (file, G_FILE_MONITOR_NONE, NULL, NULL);
		g_object_unref (file);

		if (monitor) {
			priv->monitor_id = g_signal_connect (monitor, "changed", G_CALLBACK (dir_changed), config);
			priv->monitor = monitor;
		}
	}

	g_signal_connect (G_OBJECT (priv->config),
	                  NM_CONFIG_SIGNAL_CONFIG_CHANGED,
	                  G_CALLBACK (config_changed_cb),
	                  config);
}
Exemplo n.º 8
0
void
get_dhcp_hostname_and_client_id (char **hostname, char **client_id)
{
	const char *dhcp_client;
	const gchar *dhcpcd_conf = SYSCONFDIR "/dhcpcd.conf";
	const gchar *dhclient_conf = SYSCONFDIR "/dhcp/dhclient.conf";
	gchar *line = NULL, *tmp = NULL, *contents = NULL, *tmp1;
	gchar **all_lines;
	guint line_num, i;
	gboolean use_dhclient = FALSE;

	*hostname = NULL;
	*client_id = NULL;
	dhcp_client = nm_config_get_dhcp_client (nm_config_get ());
	if (dhcp_client) {
		if (!strcmp (dhcp_client, "dhclient")) {
			g_file_get_contents (dhclient_conf, &contents, NULL,
					     NULL);
			use_dhclient = TRUE;
		} else if (!strcmp (dhcp_client, "dhcpcd"))
			g_file_get_contents (dhcpcd_conf, &contents, NULL,
					     NULL);
	} else {
		if (g_file_test (dhclient_conf, G_FILE_TEST_IS_REGULAR)) {
			g_file_get_contents (dhclient_conf, &contents, NULL,
					     NULL);
			use_dhclient = TRUE;
		}
		else if (g_file_test (dhcpcd_conf, G_FILE_TEST_IS_REGULAR))
			g_file_get_contents (dhcpcd_conf, &contents, NULL,
					     NULL);
	}
	if (!contents)
		return;
	all_lines = g_strsplit (contents, "\n", 0);
	line_num = g_strv_length (all_lines);
	for (i = 0; i < line_num; i++) {
		line = all_lines[i];
		g_strstrip (line);
		if (line[0] == '#' || line[0] == '\0')
			continue;
		if (!use_dhclient) {
			// dhcpcd.conf
			if ((tmp = _has_prefix (line, "hostname"))) {
				if (tmp[0] != '\0') {
					g_free (*hostname);
					*hostname = g_strdup (tmp);
				} else
					nm_log_info (LOGD_SETTINGS, "dhcpcd hostname not defined, ignoring");
			} else if ((tmp = _has_prefix (line, "clientid"))) {
				if (tmp[0] != '\0') {
					g_free (*client_id);
					*client_id = g_strdup (tmp);
				} else
					nm_log_info (LOGD_SETTINGS, "dhcpcd clientid not defined, ignoring");
			}
		} else {
			// dhclient.conf
			if ((tmp1 = _has_prefix (line, "send"))) {
				if ((tmp = _has_prefix (tmp1, "host-name"))) {
					strip_string (tmp, ';');
					strip_string (tmp, '"');
					if (tmp[0] != '\0') {
						g_free (*hostname);
						*hostname = g_strdup (tmp);
					} else
						nm_log_info (LOGD_SETTINGS, "dhclient hostname not defined, ignoring");
				} else if ((tmp = _has_prefix (tmp1, "dhcp-client-identifier"))) {
					strip_string (tmp, ';');
					if (tmp[0] != '\0') {
						g_free (*client_id);
						*client_id = g_strdup (tmp);
					} else
						nm_log_info (LOGD_SETTINGS, "dhclient clientid not defined, ignoring");
				}
			}
		}
	}
	g_strfreev (all_lines);
	g_free (contents);
}
Exemplo n.º 9
0
static void
SCPluginIfupdown_init (NMSystemConfigInterface *config)
{
	SCPluginIfupdown *self = SC_PLUGIN_IFUPDOWN (config);
	SCPluginIfupdownPrivate *priv = SC_PLUGIN_IFUPDOWN_GET_PRIVATE (self);
	GHashTable *auto_ifaces;
	if_block *block = NULL;
	NMInotifyHelper *inotify_helper;
	char *value;
	GError *error = NULL;
	GList *keys, *iter;
	GHashTableIter con_iter;
	const char *block_name;
	NMIfupdownConnection *connection;
	const char *subsys[2] = { "net", NULL };

	auto_ifaces = g_hash_table_new (g_str_hash, g_str_equal);

	if(!priv->connections)
		priv->connections = g_hash_table_new (g_str_hash, g_str_equal);

	if(!priv->kernel_ifaces)
		priv->kernel_ifaces = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref);

	if(!priv->eni_ifaces)
		priv->eni_ifaces = g_hash_table_new (g_str_hash, g_str_equal);

	PLUGIN_PRINT("SCPlugin-Ifupdown", "init!");

	priv->client = g_udev_client_new (subsys);
	if (!priv->client) {
		PLUGIN_WARN ("SCPlugin-Ifupdown", "    error initializing libgudev");
	} else
		g_signal_connect (priv->client, "uevent", G_CALLBACK (handle_uevent), self);

	priv->unmanage_well_known = IFUPDOWN_UNMANAGE_WELL_KNOWN_DEFAULT;
 
	inotify_helper = nm_inotify_helper_get ();
	priv->inotify_event_id = g_signal_connect (inotify_helper,
	                                           "event",
	                                           G_CALLBACK (update_system_hostname),
	                                           config);

	priv->inotify_system_hostname_wd =
		nm_inotify_helper_add_watch (inotify_helper, IFUPDOWN_SYSTEM_HOSTNAME_FILE);

	update_system_hostname (inotify_helper, NULL, NULL, config);

	/* Read in all the interfaces */
	ifparser_init (ENI_INTERFACES_FILE, 0);
	block = ifparser_getfirst ();
	while (block) {
		if(!strcmp ("auto", block->type) || !strcmp ("allow-hotplug", block->type))
			g_hash_table_insert (auto_ifaces, block->name, GUINT_TO_POINTER (1));
		else if (!strcmp ("iface", block->type)) {
			NMIfupdownConnection *exported;

			/* Bridge configuration */
			if(!strncmp ("br", block->name, 2)) {
				/* Try to find bridge ports */
				const char *ports = ifparser_getkey (block, "bridge-ports");
				if (ports) {
					int i;
					int state = 0;
					char **port_ifaces;

					PLUGIN_PRINT("SCPlugin-Ifupdown", "found bridge ports %s for %s", ports, block->name);

					port_ifaces = g_strsplit_set (ports, " \t", -1);
					for (i = 0; i < g_strv_length (port_ifaces); i++) {
						char *token = port_ifaces[i];
						/* Skip crazy stuff like regex or all */
						if (!strcmp ("all", token)) {
							continue;
						}
						/* Small SM to skip everything inside regex */
						if (!strcmp ("regex", token)) {
							state++;
							continue;
						}
						if (!strcmp ("noregex", token)) {
							state--;
							continue;
						}
						if (state == 0 && strlen (token) > 0) {
							PLUGIN_PRINT("SCPlugin-Ifupdown", "adding bridge port %s to eni_ifaces", token);
							g_hash_table_insert (priv->eni_ifaces, g_strdup (token), "known");
						}
					}
					g_strfreev (port_ifaces);
				}
				goto next;
			}

			/* Skip loopback configuration */
			if(!strcmp ("lo", block->name)) {
				goto next;
			}

			/* Remove any connection for this block that was previously found */
			exported = g_hash_table_lookup (priv->connections, block->name);
			if (exported) {
				PLUGIN_PRINT("SCPlugin-Ifupdown", "deleting %s from connections", block->name);
				nm_settings_connection_delete (NM_SETTINGS_CONNECTION (exported), ignore_cb, NULL);
				g_hash_table_remove (priv->connections, block->name);
			}

			/* add the new connection */
			exported = nm_ifupdown_connection_new (block);
			if (exported) {
				PLUGIN_PRINT("SCPlugin-Ifupdown", "adding %s to connections", block->name);
				g_hash_table_insert (priv->connections, block->name, exported);
			}
			PLUGIN_PRINT("SCPlugin-Ifupdown", "adding iface %s to eni_ifaces", block->name);
			g_hash_table_insert (priv->eni_ifaces, block->name, "known");
		} else if (!strcmp ("mapping", block->type)) {
			g_hash_table_insert (priv->eni_ifaces, block->name, "known");
			PLUGIN_PRINT("SCPlugin-Ifupdown", "adding mapping %s to eni_ifaces", block->name);
		}
	next:
		block = block->next;
	}

	/* Make 'auto' interfaces autoconnect=TRUE */
	g_hash_table_iter_init (&con_iter, priv->connections);
	while (g_hash_table_iter_next (&con_iter, (gpointer) &block_name, (gpointer) &connection)) {
		NMSettingConnection *setting;

		if (g_hash_table_lookup (auto_ifaces, block_name)) {
			setting = nm_connection_get_setting_connection (NM_CONNECTION (connection));
			g_object_set (setting, NM_SETTING_CONNECTION_AUTOCONNECT, TRUE, NULL);
			PLUGIN_PRINT("SCPlugin-Ifupdown", "autoconnect");
		}
	}
	g_hash_table_destroy (auto_ifaces);

	/* Check the config file to find out whether to manage interfaces */
	value = nm_config_get_value (nm_config_get (),
	                             IFUPDOWN_KEY_FILE_GROUP, IFUPDOWN_KEY_FILE_KEY_MANAGED,
	                             &error);
	if (error) {
		nm_log_info (LOGD_SETTINGS, "loading system config file (%s) caused error: %s",
		             nm_config_get_path (nm_config_get ()),
		             error->message);
	} else {
		gboolean manage_well_known;
		error = NULL;

		manage_well_known = !g_strcmp0 (value, "true") || !g_strcmp0 (value, "1");
		priv->unmanage_well_known = !manage_well_known;
		g_free (value);
	}
	PLUGIN_PRINT ("SCPluginIfupdown", "management mode: %s", priv->unmanage_well_known ? "unmanaged" : "managed");

	/* Add well-known interfaces */
	keys = g_udev_client_query_by_subsystem (priv->client, "net");
	for (iter = keys; iter; iter = g_list_next (iter)) {
		udev_device_added (self, G_UDEV_DEVICE (iter->data));
		g_object_unref (G_UDEV_DEVICE (iter->data));
	}
	g_list_free (keys);

	/* Now if we're running in managed mode, let NM know there are new connections */
	if (!priv->unmanage_well_known) {
		GList *con_list = g_hash_table_get_values (priv->connections);
		GList *cl_iter;

		for (cl_iter = con_list; cl_iter; cl_iter = g_list_next (cl_iter)) {
			g_signal_emit_by_name (self,
			                       NM_SYSTEM_CONFIG_INTERFACE_CONNECTION_ADDED,
			                       NM_SETTINGS_CONNECTION (cl_iter->data));
		}
		g_list_free (con_list);
	}

	PLUGIN_PRINT("SCPlugin-Ifupdown", "end _init.");
}