2011-09-22 10:16:07 -05:00
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/* NetworkManager -- Network link manager
*
* This program is free software ; you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation ; either version 2 of the License , or
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License along
* with this program ; if not , write to the Free Software Foundation , Inc . ,
* 51 Franklin Street , Fifth Floor , Boston , MA 02110 - 1301 USA .
*
* Copyright ( C ) 2011 Red Hat , Inc .
2013-01-15 18:48:51 +01:00
* Copyright ( C ) 2013 Thomas Bechtold < thomasbechtold @ jpberlin . de >
2011-09-22 10:16:07 -05:00
*/
# include <config.h>
# include <string.h>
# include <stdio.h>
# include "nm-config.h"
2013-03-11 12:23:57 -04:00
# include "nm-logging.h"
2013-03-12 13:14:54 -04:00
# include "nm-utils.h"
2013-04-03 12:56:13 -04:00
# include "nm-glib-compat.h"
2014-07-17 16:24:17 -04:00
# include "nm-device.h"
2011-09-22 10:16:07 -05:00
2013-03-14 14:34:36 -04:00
# include <gio/gio.h>
2013-03-11 12:06:38 -04:00
# include <glib/gi18n.h>
2013-03-12 13:14:54 -04:00
# define NM_DEFAULT_SYSTEM_CONF_FILE NMCONFDIR " / NetworkManager.conf"
2013-03-14 14:34:36 -04:00
# define NM_DEFAULT_SYSTEM_CONF_DIR NMCONFDIR " / conf.d"
2013-03-12 13:14:54 -04:00
# define NM_OLD_SYSTEM_CONF_FILE NMCONFDIR " / nm-system-settings.conf"
# define NM_NO_AUTO_DEFAULT_STATE_FILE NMSTATEDIR " / no-auto-default.state"
2011-09-22 10:16:07 -05:00
2013-01-15 18:48:51 +01:00
typedef struct {
2013-03-14 14:34:36 -04:00
char * nm_conf_path ;
char * config_dir ;
2013-06-14 16:05:04 -04:00
char * config_description ;
2013-03-12 13:14:54 -04:00
char * no_auto_default_file ;
GKeyFile * keyfile ;
2011-09-22 10:16:07 -05:00
char * * plugins ;
2013-05-23 19:05:40 -03:00
gboolean monitor_connection_files ;
2014-08-14 13:34:57 +02:00
gboolean auth_polkit ;
2011-09-22 10:16:07 -05:00
char * dhcp_client ;
2013-03-26 10:03:06 -04:00
char * dns_mode ;
2013-03-19 10:24:35 -04:00
2011-09-22 10:16:07 -05:00
char * log_level ;
char * log_domains ;
2013-03-19 10:24:35 -04:00
2014-03-06 22:04:44 +01:00
char * debug ;
2011-10-21 21:21:30 +02:00
char * connectivity_uri ;
2013-01-31 17:32:51 -06:00
gint connectivity_interval ;
2011-10-21 21:21:30 +02:00
char * connectivity_response ;
2013-03-19 10:24:35 -04:00
2013-03-12 13:14:54 -04:00
char * * no_auto_default ;
2013-03-19 10:24:35 -04:00
char * * ignore_carrier ;
2013-01-15 18:48:51 +01:00
} NMConfigPrivate ;
static NMConfig * singleton = NULL ;
G_DEFINE_TYPE ( NMConfig , nm_config , G_TYPE_OBJECT )
# define NM_CONFIG_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_CONFIG, NMConfigPrivate))
2011-09-22 10:16:07 -05:00
/************************************************************************/
2014-08-17 14:06:41 +02:00
static gboolean
_parse_bool_str ( const char * str , gboolean * out_value )
{
gboolean value ;
gsize len ;
char * s = NULL ;
g_return_val_if_fail ( str , FALSE ) ;
while ( g_ascii_isspace ( * str ) )
str + + ;
if ( ! * str )
return FALSE ;
len = strlen ( str ) ;
if ( g_ascii_isspace ( str [ len - 1 ] ) ) {
str = s = g_strdup ( str ) ;
g_strchomp ( s ) ;
}
if ( ! g_ascii_strcasecmp ( str , " true " ) | | ! g_ascii_strcasecmp ( str , " yes " ) | | ! g_ascii_strcasecmp ( str , " on " ) | | ! g_ascii_strcasecmp ( str , " 1 " ) )
value = TRUE ;
else if ( ! g_ascii_strcasecmp ( str , " false " ) | | ! g_ascii_strcasecmp ( str , " no " ) | | ! g_ascii_strcasecmp ( str , " off " ) | | ! g_ascii_strcasecmp ( str , " 0 " ) )
value = FALSE ;
else {
g_free ( s ) ;
return FALSE ;
}
if ( out_value )
* out_value = value ;
g_free ( s ) ;
return TRUE ;
}
/************************************************************************/
2011-09-22 10:16:07 -05:00
const char *
nm_config_get_path ( NMConfig * config )
{
g_return_val_if_fail ( config ! = NULL , NULL ) ;
2013-03-14 14:34:36 -04:00
return NM_CONFIG_GET_PRIVATE ( config ) - > nm_conf_path ;
2011-09-22 10:16:07 -05:00
}
2013-06-14 16:05:04 -04:00
const char *
nm_config_get_description ( NMConfig * config )
{
g_return_val_if_fail ( config ! = NULL , NULL ) ;
return NM_CONFIG_GET_PRIVATE ( config ) - > config_description ;
}
2011-09-22 10:16:07 -05:00
const char * *
nm_config_get_plugins ( NMConfig * config )
{
g_return_val_if_fail ( config ! = NULL , NULL ) ;
2013-01-15 18:48:51 +01:00
return ( const char * * ) NM_CONFIG_GET_PRIVATE ( config ) - > plugins ;
2011-09-22 10:16:07 -05:00
}
2013-05-23 19:05:40 -03:00
gboolean
nm_config_get_monitor_connection_files ( NMConfig * config )
{
g_return_val_if_fail ( config ! = NULL , FALSE ) ;
return NM_CONFIG_GET_PRIVATE ( config ) - > monitor_connection_files ;
}
2014-08-14 13:34:57 +02:00
gboolean
nm_config_get_auth_polkit ( NMConfig * config )
{
g_return_val_if_fail ( NM_IS_CONFIG ( config ) , NM_CONFIG_DEFAULT_AUTH_POLKIT ) ;
return NM_CONFIG_GET_PRIVATE ( config ) - > auth_polkit ;
}
2011-09-22 10:16:07 -05:00
const char *
nm_config_get_dhcp_client ( NMConfig * config )
{
g_return_val_if_fail ( config ! = NULL , NULL ) ;
2013-03-12 09:32:47 +01:00
return NM_CONFIG_GET_PRIVATE ( config ) - > dhcp_client ;
2011-09-22 10:16:07 -05:00
}
2013-03-26 10:03:06 -04:00
const char *
nm_config_get_dns_mode ( NMConfig * config )
2011-09-22 10:16:07 -05:00
{
g_return_val_if_fail ( config ! = NULL , NULL ) ;
2013-03-26 10:03:06 -04:00
return NM_CONFIG_GET_PRIVATE ( config ) - > dns_mode ;
2011-09-22 10:16:07 -05:00
}
const char *
nm_config_get_log_level ( NMConfig * config )
{
g_return_val_if_fail ( config ! = NULL , NULL ) ;
2013-03-12 09:32:47 +01:00
return NM_CONFIG_GET_PRIVATE ( config ) - > log_level ;
2011-09-22 10:16:07 -05:00
}
const char *
nm_config_get_log_domains ( NMConfig * config )
{
g_return_val_if_fail ( config ! = NULL , NULL ) ;
2013-01-15 18:48:51 +01:00
return NM_CONFIG_GET_PRIVATE ( config ) - > log_domains ;
2011-09-22 10:16:07 -05:00
}
2014-03-06 22:04:44 +01:00
const char *
nm_config_get_debug ( NMConfig * config )
{
g_return_val_if_fail ( config ! = NULL , NULL ) ;
return NM_CONFIG_GET_PRIVATE ( config ) - > debug ;
}
2011-10-21 21:21:30 +02:00
const char *
nm_config_get_connectivity_uri ( NMConfig * config )
{
g_return_val_if_fail ( config ! = NULL , NULL ) ;
2013-01-15 18:48:51 +01:00
return NM_CONFIG_GET_PRIVATE ( config ) - > connectivity_uri ;
2011-10-21 21:21:30 +02:00
}
2014-07-29 20:04:42 +02:00
guint
2011-10-21 21:21:30 +02:00
nm_config_get_connectivity_interval ( NMConfig * config )
{
2012-07-09 16:42:01 +02:00
g_return_val_if_fail ( config ! = NULL , 0 ) ;
2011-10-21 21:21:30 +02:00
2013-01-31 17:32:51 -06:00
/* We store interval as signed internally to track whether it's
* set or not , but report as unsigned to callers .
*/
2013-10-31 14:13:33 +01:00
return MAX ( NM_CONFIG_GET_PRIVATE ( config ) - > connectivity_interval , 0 ) ;
2011-10-21 21:21:30 +02:00
}
const char *
nm_config_get_connectivity_response ( NMConfig * config )
{
g_return_val_if_fail ( config ! = NULL , NULL ) ;
2013-01-15 18:48:51 +01:00
return NM_CONFIG_GET_PRIVATE ( config ) - > connectivity_response ;
2011-10-21 21:21:30 +02:00
}
2013-03-12 13:14:54 -04:00
char *
nm_config_get_value ( NMConfig * config , const char * group , const char * key , GError * * error )
{
NMConfigPrivate * priv = NM_CONFIG_GET_PRIVATE ( config ) ;
return g_key_file_get_string ( priv - > keyfile , group , key , error ) ;
}
2013-03-19 10:24:35 -04:00
gboolean
2014-07-17 16:24:17 -04:00
nm_config_get_ignore_carrier ( NMConfig * config , NMDevice * device )
2013-03-19 10:24:35 -04:00
{
NMConfigPrivate * priv = NM_CONFIG_GET_PRIVATE ( config ) ;
2014-07-17 16:24:17 -04:00
GSList * specs = NULL ;
int i ;
gboolean match ;
2013-03-19 10:24:35 -04:00
2014-07-23 13:00:51 -04:00
if ( ! priv - > ignore_carrier )
return FALSE ;
2014-07-17 16:24:17 -04:00
for ( i = 0 ; priv - > ignore_carrier [ i ] ; i + + )
specs = g_slist_prepend ( specs , priv - > ignore_carrier [ i ] ) ;
match = nm_device_spec_match_list ( device , specs ) ;
g_slist_free ( specs ) ;
return match ;
2013-03-19 10:24:35 -04:00
}
2013-03-12 13:14:54 -04:00
/************************************************************************/
static void
merge_no_auto_default_state ( NMConfig * config )
{
NMConfigPrivate * priv = NM_CONFIG_GET_PRIVATE ( config ) ;
GPtrArray * updated ;
char * * list ;
int i , j ;
char * data ;
/* If the config already matches everything, we don't need to do anything else. */
if ( priv - > no_auto_default & & ! g_strcmp0 ( priv - > no_auto_default [ 0 ] , " * " ) )
return ;
updated = g_ptr_array_new ( ) ;
if ( priv - > no_auto_default ) {
for ( i = 0 ; priv - > no_auto_default [ i ] ; i + + )
g_ptr_array_add ( updated , priv - > no_auto_default [ i ] ) ;
g_free ( priv - > no_auto_default ) ;
}
if ( g_file_get_contents ( priv - > no_auto_default_file , & data , NULL , NULL ) ) {
list = g_strsplit ( data , " \n " , - 1 ) ;
for ( i = 0 ; list [ i ] ; i + + ) {
if ( ! * list [ i ] )
continue ;
for ( j = 0 ; j < updated - > len ; j + + ) {
if ( ! strcmp ( list [ i ] , updated - > pdata [ j ] ) )
break ;
}
if ( j = = updated - > len )
g_ptr_array_add ( updated , list [ i ] ) ;
}
g_free ( list ) ;
g_free ( data ) ;
}
g_ptr_array_add ( updated , NULL ) ;
priv - > no_auto_default = ( char * * ) g_ptr_array_free ( updated , FALSE ) ;
}
gboolean
2014-07-17 16:24:17 -04:00
nm_config_get_ethernet_can_auto_default ( NMConfig * config , NMDevice * device )
2013-03-12 13:14:54 -04:00
{
NMConfigPrivate * priv = NM_CONFIG_GET_PRIVATE ( config ) ;
2014-07-17 16:24:17 -04:00
GSList * specs = NULL ;
int i ;
gboolean match ;
for ( i = 0 ; priv - > no_auto_default [ i ] ; i + + )
specs = g_slist_prepend ( specs , priv - > no_auto_default [ i ] ) ;
match = nm_device_spec_match_list ( device , specs ) ;
2013-03-12 13:14:54 -04:00
2014-07-17 16:24:17 -04:00
g_slist_free ( specs ) ;
return ! match ;
2013-03-12 13:14:54 -04:00
}
void
2014-07-17 16:24:17 -04:00
nm_config_set_ethernet_no_auto_default ( NMConfig * config , NMDevice * device )
2013-03-12 13:14:54 -04:00
{
NMConfigPrivate * priv = NM_CONFIG_GET_PRIVATE ( config ) ;
2014-06-21 12:44:56 -04:00
char * current ;
2013-03-12 13:14:54 -04:00
GString * updated ;
GError * error = NULL ;
if ( ! nm_config_get_ethernet_can_auto_default ( config , device ) )
return ;
updated = g_string_new ( NULL ) ;
if ( g_file_get_contents ( priv - > no_auto_default_file , & current , NULL , NULL ) ) {
g_string_append ( updated , current ) ;
g_free ( current ) ;
if ( updated - > str [ updated - > len - 1 ] ! = ' \n ' )
g_string_append_c ( updated , ' \n ' ) ;
}
2014-06-21 12:44:56 -04:00
g_string_append ( updated , nm_device_get_hw_address ( device ) ) ;
2013-03-12 13:14:54 -04:00
g_string_append_c ( updated , ' \n ' ) ;
if ( ! g_file_set_contents ( priv - > no_auto_default_file , updated - > str , updated - > len , & error ) ) {
nm_log_warn ( LOGD_SETTINGS , " Could not update no-auto-default.state file: %s " ,
error - > message ) ;
g_error_free ( error ) ;
}
g_string_free ( updated , TRUE ) ;
merge_no_auto_default_state ( config ) ;
}
2013-03-11 12:06:38 -04:00
/************************************************************************/
static char * cli_config_path ;
2013-03-14 14:34:36 -04:00
static char * cli_config_dir ;
2013-03-12 13:14:54 -04:00
static char * cli_no_auto_default_file ;
2013-03-11 12:06:38 -04:00
static char * cli_plugins ;
static char * cli_connectivity_uri ;
static int cli_connectivity_interval = - 1 ;
static char * cli_connectivity_response ;
static GOptionEntry config_options [ ] = {
{ " config " , 0 , 0 , G_OPTION_ARG_FILENAME , & cli_config_path , N_ ( " Config file location " ) , N_ ( " /path/to/config.file " ) } ,
2013-03-14 14:34:36 -04:00
{ " config-dir " , 0 , 0 , G_OPTION_ARG_FILENAME , & cli_config_dir , N_ ( " Config directory location " ) , N_ ( " /path/to/config/dir " ) } ,
2013-03-12 13:14:54 -04:00
{ " no-auto-default " , 0 , G_OPTION_FLAG_HIDDEN , G_OPTION_ARG_FILENAME , & cli_no_auto_default_file , " no-auto-default.state location " , NULL } ,
2013-03-11 12:06:38 -04:00
{ " plugins " , 0 , 0 , G_OPTION_ARG_STRING , & cli_plugins , N_ ( " List of plugins separated by ',' " ) , N_ ( " plugin1,plugin2 " ) } ,
2013-03-14 09:23:21 -04:00
/* These three are hidden for now, and should eventually just go away. */
{ " connectivity-uri " , 0 , G_OPTION_FLAG_HIDDEN , G_OPTION_ARG_STRING , & cli_connectivity_uri , N_ ( " An http(s) address for checking internet connectivity " ) , " http://example.com " } ,
{ " connectivity-interval " , 0 , G_OPTION_FLAG_HIDDEN , G_OPTION_ARG_INT , & cli_connectivity_interval , N_ ( " The interval between connectivity checks (in seconds) " ) , " 60 " } ,
{ " connectivity-response " , 0 , G_OPTION_FLAG_HIDDEN , G_OPTION_ARG_STRING , & cli_connectivity_response , N_ ( " The expected start of the response " ) , N_ ( " Bingo! " ) } ,
2013-03-11 12:06:38 -04:00
{ NULL }
} ;
GOptionEntry *
nm_config_get_options ( void )
{
return config_options ;
}
2011-10-21 21:21:30 +02:00
2011-09-22 10:16:07 -05:00
/************************************************************************/
static gboolean
2013-02-01 09:20:17 -06:00
read_config ( NMConfig * config , const char * path , GError * * error )
2011-09-22 10:16:07 -05:00
{
2013-01-15 18:48:51 +01:00
NMConfigPrivate * priv = NM_CONFIG_GET_PRIVATE ( config ) ;
2011-09-22 10:16:07 -05:00
GKeyFile * kf ;
2013-03-14 14:34:36 -04:00
char * * groups , * * keys ;
gsize ngroups , nkeys ;
int g , k ;
2011-09-22 10:16:07 -05:00
if ( g_file_test ( path , G_FILE_TEST_EXISTS ) = = FALSE ) {
2011-10-01 00:22:09 +02:00
g_set_error ( error , G_KEY_FILE_ERROR , G_KEY_FILE_ERROR_NOT_FOUND , " file %s not found " , path ) ;
2011-09-22 10:16:07 -05:00
return FALSE ;
}
2013-06-14 16:05:04 -04:00
nm_log_dbg ( LOGD_SETTINGS , " Reading config file '%s' " , path ) ;
2011-09-22 10:16:07 -05:00
kf = g_key_file_new ( ) ;
g_key_file_set_list_separator ( kf , ' , ' ) ;
2013-03-14 14:34:36 -04:00
if ( ! g_key_file_load_from_file ( kf , path , G_KEY_FILE_NONE , error ) ) {
g_key_file_free ( kf ) ;
return FALSE ;
}
2011-09-22 10:16:07 -05:00
2013-03-14 14:34:36 -04:00
/* Override the current settings with the new ones */
groups = g_key_file_get_groups ( kf , & ngroups ) ;
for ( g = 0 ; groups [ g ] ; g + + ) {
keys = g_key_file_get_keys ( kf , groups [ g ] , & nkeys , NULL ) ;
if ( ! keys )
continue ;
for ( k = 0 ; keys [ k ] ; k + + ) {
int len = strlen ( keys [ k ] ) ;
if ( keys [ k ] [ len - 1 ] = = ' + ' ) {
char * base_key = g_strndup ( keys [ k ] , len - 1 ) ;
const char * old_val = g_key_file_get_value ( priv - > keyfile , groups [ g ] , base_key , NULL ) ;
const char * new_val = g_key_file_get_value ( kf , groups [ g ] , keys [ k ] , NULL ) ;
if ( old_val & & * old_val ) {
char * combined = g_strconcat ( old_val , " , " , new_val , NULL ) ;
g_key_file_set_value ( priv - > keyfile , groups [ g ] , base_key , combined ) ;
g_free ( combined ) ;
} else
g_key_file_set_value ( priv - > keyfile , groups [ g ] , base_key , new_val ) ;
g_free ( base_key ) ;
continue ;
}
2011-09-22 10:16:07 -05:00
2013-03-14 14:34:36 -04:00
g_key_file_set_value ( priv - > keyfile , groups [ g ] , keys [ k ] ,
g_key_file_get_value ( kf , groups [ g ] , keys [ k ] , NULL ) ) ;
}
}
g_key_file_free ( kf ) ;
2011-09-22 10:16:07 -05:00
2013-03-14 14:34:36 -04:00
return TRUE ;
}
2011-09-22 10:16:07 -05:00
2013-03-14 14:34:36 -04:00
static gboolean
find_base_config ( NMConfig * config , GError * * error )
{
NMConfigPrivate * priv = NM_CONFIG_GET_PRIVATE ( config ) ;
GError * my_error = NULL ;
2011-10-21 21:21:30 +02:00
2013-03-14 14:34:36 -04:00
/* Try a user-specified config file first */
if ( cli_config_path ) {
/* Bad user-specific config file path is a hard error */
if ( read_config ( config , cli_config_path , error ) ) {
priv - > nm_conf_path = g_strdup ( cli_config_path ) ;
return TRUE ;
} else
return FALSE ;
}
2011-10-21 21:21:30 +02:00
2013-03-14 14:34:36 -04:00
/* Even though we prefer NetworkManager.conf, we need to check the
* old nm - system - settings . conf first to preserve compat with older
* setups . In package managed systems dropping a NetworkManager . conf
* onto the system would make NM use it instead of nm - system - settings . conf ,
* changing behavior during an upgrade . We don ' t want that .
*/
2011-10-21 21:21:30 +02:00
2013-03-14 14:34:36 -04:00
/* Try deprecated nm-system-settings.conf first */
if ( read_config ( config , NM_OLD_SYSTEM_CONF_FILE , & my_error ) ) {
priv - > nm_conf_path = g_strdup ( NM_OLD_SYSTEM_CONF_FILE ) ;
return TRUE ;
}
2011-10-21 21:21:30 +02:00
2013-03-14 14:34:36 -04:00
if ( ! g_error_matches ( my_error , G_KEY_FILE_ERROR , G_KEY_FILE_ERROR_NOT_FOUND ) ) {
2014-07-09 14:41:07 +02:00
nm_log_warn ( LOGD_CORE , " Old default config file %s invalid: %s \n " ,
NM_OLD_SYSTEM_CONF_FILE ,
my_error - > message ) ;
2013-03-14 14:34:36 -04:00
}
g_clear_error ( & my_error ) ;
2013-03-12 13:14:54 -04:00
2013-03-14 14:34:36 -04:00
/* Try the standard config file location next */
if ( read_config ( config , NM_DEFAULT_SYSTEM_CONF_FILE , & my_error ) ) {
priv - > nm_conf_path = g_strdup ( NM_DEFAULT_SYSTEM_CONF_FILE ) ;
return TRUE ;
}
2011-09-22 10:16:07 -05:00
2013-03-14 14:34:36 -04:00
if ( ! g_error_matches ( my_error , G_KEY_FILE_ERROR , G_KEY_FILE_ERROR_NOT_FOUND ) ) {
2014-07-09 14:41:07 +02:00
nm_log_warn ( LOGD_CORE , " Default config file %s invalid: %s \n " ,
NM_DEFAULT_SYSTEM_CONF_FILE ,
my_error - > message ) ;
2013-03-14 14:34:36 -04:00
g_propagate_error ( error , my_error ) ;
return FALSE ;
}
2014-07-09 14:37:54 +02:00
g_clear_error ( & my_error ) ;
2013-03-14 14:34:36 -04:00
/* If for some reason no config file exists, use the default
* config file path .
*/
priv - > nm_conf_path = g_strdup ( NM_DEFAULT_SYSTEM_CONF_FILE ) ;
2014-07-09 14:41:07 +02:00
nm_log_info ( LOGD_CORE , " No config file found or given; using %s \n " ,
NM_DEFAULT_SYSTEM_CONF_FILE ) ;
2013-03-14 14:34:36 -04:00
return TRUE ;
2011-09-22 10:16:07 -05:00
}
2013-01-31 16:16:56 -06:00
/************************************************************************/
NMConfig *
nm_config_get ( void )
{
g_assert ( singleton ) ;
return singleton ;
}
2013-03-14 14:34:36 -04:00
static int
sort_asciibetically ( gconstpointer a , gconstpointer b )
{
const char * s1 = * ( const char * * ) a ;
const char * s2 = * ( const char * * ) b ;
return strcmp ( s1 , s2 ) ;
}
2013-01-15 18:48:51 +01:00
/* call this function only once! */
2011-09-22 10:16:07 -05:00
NMConfig *
2013-03-11 12:06:38 -04:00
nm_config_new ( GError * * error )
2011-09-22 10:16:07 -05:00
{
2013-01-15 18:48:51 +01:00
NMConfigPrivate * priv = NULL ;
2013-03-14 14:34:36 -04:00
GFile * dir ;
GFileEnumerator * direnum ;
GFileInfo * info ;
GPtrArray * confs ;
const char * name ;
2013-05-23 19:05:40 -03:00
char * value ;
2013-03-14 14:34:36 -04:00
int i ;
2013-06-14 16:05:04 -04:00
GString * config_description ;
2011-09-22 10:16:07 -05:00
2013-01-15 18:48:51 +01:00
g_assert ( ! singleton ) ;
singleton = NM_CONFIG ( g_object_new ( NM_TYPE_CONFIG , NULL ) ) ;
priv = NM_CONFIG_GET_PRIVATE ( singleton ) ;
2011-09-22 10:16:07 -05:00
2013-03-14 14:34:36 -04:00
/* First read the base config file */
if ( ! find_base_config ( singleton , error ) ) {
2013-01-15 18:48:51 +01:00
g_object_unref ( singleton ) ;
2013-03-14 14:34:36 -04:00
singleton = NULL ;
2013-02-01 09:20:17 -06:00
return NULL ;
2011-09-22 10:16:07 -05:00
}
2013-03-14 14:34:36 -04:00
/* Now read the overrides in the config dir */
if ( cli_config_dir )
priv - > config_dir = g_strdup ( cli_config_dir ) ;
else
priv - > config_dir = g_strdup ( NM_DEFAULT_SYSTEM_CONF_DIR ) ;
confs = g_ptr_array_new_with_free_func ( g_free ) ;
2013-06-14 16:05:04 -04:00
config_description = g_string_new ( priv - > nm_conf_path ) ;
2013-03-14 14:34:36 -04:00
dir = g_file_new_for_path ( priv - > config_dir ) ;
direnum = g_file_enumerate_children ( dir , G_FILE_ATTRIBUTE_STANDARD_NAME , 0 , NULL , NULL ) ;
if ( direnum ) {
while ( ( info = g_file_enumerator_next_file ( direnum , NULL , NULL ) ) ) {
name = g_file_info_get_name ( info ) ;
2013-06-14 16:05:04 -04:00
if ( g_str_has_suffix ( name , " .conf " ) ) {
2013-03-14 14:34:36 -04:00
g_ptr_array_add ( confs , g_build_filename ( priv - > config_dir , name , NULL ) ) ;
2013-06-14 16:05:04 -04:00
if ( confs - > len = = 1 )
g_string_append ( config_description , " and conf.d: " ) ;
else
g_string_append ( config_description , " , " ) ;
g_string_append ( config_description , name ) ;
}
2013-03-14 14:34:36 -04:00
g_object_unref ( info ) ;
}
g_object_unref ( direnum ) ;
2012-01-13 16:50:51 -06:00
}
2013-03-14 14:34:36 -04:00
g_object_unref ( dir ) ;
2012-01-13 16:50:51 -06:00
2013-03-14 14:34:36 -04:00
g_ptr_array_sort ( confs , sort_asciibetically ) ;
2013-06-14 16:05:04 -04:00
priv - > config_description = g_string_free ( config_description , FALSE ) ;
2013-03-14 14:34:36 -04:00
for ( i = 0 ; i < confs - > len ; i + + ) {
if ( ! read_config ( singleton , confs - > pdata [ i ] , error ) ) {
g_object_unref ( singleton ) ;
singleton = NULL ;
break ;
}
}
g_ptr_array_unref ( confs ) ;
if ( ! singleton )
return FALSE ;
2013-03-12 13:14:54 -04:00
2013-03-14 14:34:36 -04:00
/* Handle no-auto-default key and state file */
priv - > no_auto_default = g_key_file_get_string_list ( priv - > keyfile , " main " , " no-auto-default " , NULL , NULL ) ;
2013-03-12 13:14:54 -04:00
if ( cli_no_auto_default_file )
priv - > no_auto_default_file = g_strdup ( cli_no_auto_default_file ) ;
else
priv - > no_auto_default_file = g_strdup ( NM_NO_AUTO_DEFAULT_STATE_FILE ) ;
merge_no_auto_default_state ( singleton ) ;
2013-03-14 14:34:36 -04:00
/* Now let command-line options override the config files, and fill in priv. */
if ( cli_plugins & & cli_plugins [ 0 ] )
g_key_file_set_value ( priv - > keyfile , " main " , " plugins " , cli_plugins ) ;
priv - > plugins = g_key_file_get_string_list ( priv - > keyfile , " main " , " plugins " , NULL , NULL ) ;
2014-10-16 12:01:35 +02:00
if ( ! priv - > plugins & & STRLEN ( CONFIG_PLUGINS_DEFAULT ) > 0 )
priv - > plugins = g_strsplit ( CONFIG_PLUGINS_DEFAULT , " , " , - 1 ) ;
2013-03-14 14:34:36 -04:00
2013-05-23 19:05:40 -03:00
value = g_key_file_get_value ( priv - > keyfile , " main " , " monitor-connection-files " , NULL ) ;
2014-08-17 14:06:41 +02:00
priv - > monitor_connection_files = FALSE ;
2013-05-23 19:05:40 -03:00
if ( value ) {
2014-08-17 14:06:41 +02:00
if ( ! _parse_bool_str ( value , & priv - > monitor_connection_files ) )
2014-07-09 14:41:07 +02:00
nm_log_warn ( LOGD_CORE , " Unrecognized value for main.monitor-connection-files: %s. Assuming 'false' " , value ) ;
2013-05-23 19:05:40 -03:00
g_free ( value ) ;
2014-08-17 14:06:41 +02:00
}
2013-05-23 19:05:40 -03:00
2014-08-14 13:34:57 +02:00
value = g_key_file_get_value ( priv - > keyfile , " main " , " auth-polkit " , NULL ) ;
priv - > auth_polkit = NM_CONFIG_DEFAULT_AUTH_POLKIT ;
if ( value ) {
if ( ! _parse_bool_str ( value , & priv - > auth_polkit ) ) {
nm_log_warn ( LOGD_CORE , " Unrecognized value for main.auth-polkit: %s. Assuming '%s' " , value ,
NM_CONFIG_DEFAULT_AUTH_POLKIT ? " true " : " false " ) ;
}
g_free ( value ) ;
}
2013-03-14 14:34:36 -04:00
priv - > dhcp_client = g_key_file_get_value ( priv - > keyfile , " main " , " dhcp " , NULL ) ;
2013-03-26 10:03:06 -04:00
priv - > dns_mode = g_key_file_get_value ( priv - > keyfile , " main " , " dns " , NULL ) ;
2013-03-14 14:34:36 -04:00
priv - > log_level = g_key_file_get_value ( priv - > keyfile , " logging " , " level " , NULL ) ;
priv - > log_domains = g_key_file_get_value ( priv - > keyfile , " logging " , " domains " , NULL ) ;
2014-03-06 22:04:44 +01:00
priv - > debug = g_key_file_get_value ( priv - > keyfile , " main " , " debug " , NULL ) ;
2013-03-14 14:34:36 -04:00
if ( cli_connectivity_uri & & cli_connectivity_uri [ 0 ] )
g_key_file_set_value ( priv - > keyfile , " connectivity " , " uri " , cli_connectivity_uri ) ;
priv - > connectivity_uri = g_key_file_get_value ( priv - > keyfile , " connectivity " , " uri " , NULL ) ;
if ( cli_connectivity_interval > = 0 )
g_key_file_set_integer ( priv - > keyfile , " connectivity " , " interval " , cli_connectivity_interval ) ;
priv - > connectivity_interval = g_key_file_get_integer ( priv - > keyfile , " connectivity " , " interval " , NULL ) ;
if ( cli_connectivity_response & & cli_connectivity_response [ 0 ] )
g_key_file_set_value ( priv - > keyfile , " connectivity " , " response " , cli_connectivity_response ) ;
priv - > connectivity_response = g_key_file_get_value ( priv - > keyfile , " connectivity " , " response " , NULL ) ;
2013-03-19 10:24:35 -04:00
priv - > ignore_carrier = g_key_file_get_string_list ( priv - > keyfile , " main " , " ignore-carrier " , NULL , NULL ) ;
2013-01-15 18:48:51 +01:00
return singleton ;
2011-09-22 10:16:07 -05:00
}
2013-01-31 16:16:56 -06:00
static void
nm_config_init ( NMConfig * config )
{
2013-03-14 14:34:36 -04:00
NMConfigPrivate * priv = NM_CONFIG_GET_PRIVATE ( config ) ;
2014-08-14 13:34:57 +02:00
priv - > auth_polkit = NM_CONFIG_DEFAULT_AUTH_POLKIT ;
2013-03-14 14:34:36 -04:00
priv - > keyfile = g_key_file_new ( ) ;
g_key_file_set_list_separator ( priv - > keyfile , ' , ' ) ;
priv - > connectivity_interval = - 1 ;
2013-01-31 16:16:56 -06:00
}
static void
2013-03-12 14:15:06 -04:00
finalize ( GObject * gobject )
2013-01-31 16:16:56 -06:00
{
2013-03-12 14:15:06 -04:00
NMConfigPrivate * priv = NM_CONFIG_GET_PRIVATE ( gobject ) ;
2013-01-31 16:16:56 -06:00
2013-03-14 14:34:36 -04:00
g_free ( priv - > nm_conf_path ) ;
g_free ( priv - > config_dir ) ;
2013-06-14 16:05:04 -04:00
g_free ( priv - > config_description ) ;
2013-03-14 14:34:36 -04:00
g_free ( priv - > no_auto_default_file ) ;
2013-03-12 13:14:54 -04:00
g_clear_pointer ( & priv - > keyfile , g_key_file_unref ) ;
2013-03-12 14:15:06 -04:00
g_strfreev ( priv - > plugins ) ;
g_free ( priv - > dhcp_client ) ;
2013-03-26 10:03:06 -04:00
g_free ( priv - > dns_mode ) ;
2013-03-12 14:15:06 -04:00
g_free ( priv - > log_level ) ;
g_free ( priv - > log_domains ) ;
2014-03-06 22:04:44 +01:00
g_free ( priv - > debug ) ;
2013-03-12 14:15:06 -04:00
g_free ( priv - > connectivity_uri ) ;
g_free ( priv - > connectivity_response ) ;
2013-03-14 14:34:36 -04:00
g_strfreev ( priv - > no_auto_default ) ;
2013-03-19 10:24:35 -04:00
g_strfreev ( priv - > ignore_carrier ) ;
2013-01-31 16:16:56 -06:00
singleton = NULL ;
2013-03-11 12:06:38 -04:00
g_clear_pointer ( & cli_config_path , g_free ) ;
2013-03-14 14:34:36 -04:00
g_clear_pointer ( & cli_config_dir , g_free ) ;
g_clear_pointer ( & cli_no_auto_default_file , g_free ) ;
2013-03-11 12:06:38 -04:00
g_clear_pointer ( & cli_plugins , g_free ) ;
g_clear_pointer ( & cli_connectivity_uri , g_free ) ;
g_clear_pointer ( & cli_connectivity_response , g_free ) ;
2013-01-31 16:16:56 -06:00
G_OBJECT_CLASS ( nm_config_parent_class ) - > finalize ( gobject ) ;
}
static void
nm_config_class_init ( NMConfigClass * config_class )
{
GObjectClass * object_class = G_OBJECT_CLASS ( config_class ) ;
g_type_class_add_private ( config_class , sizeof ( NMConfigPrivate ) ) ;
object_class - > finalize = finalize ;
}