2 * ConnMan VPN daemon settings
4 * Copyright (C) 2012-2013 Intel Corporation. All rights reserved.
5 * Copyright (C) 2018-2019 Jolla Ltd. All rights reserved.
6 * Contact: jussi.laakkonen@jolla.com
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
27 #include <connman/log.h>
31 #define DEFAULT_INPUT_REQUEST_TIMEOUT 300 * 1000
32 #define PLUGIN_CONFIGDIR CONFIGDIR "/vpn-plugin"
33 #define VPN_GROUP "DACPrivileges"
36 unsigned int timeout_inputreq;
39 char **binary_supplementary_groups;
40 } connman_vpn_settings = {
41 .timeout_inputreq = DEFAULT_INPUT_REQUEST_TIMEOUT,
44 .binary_supplementary_groups = NULL,
47 struct vpn_plugin_data {
50 char **binary_supplementary_groups;
53 GHashTable *plugin_hash = NULL;
55 const char *vpn_settings_get_binary_user(struct vpn_plugin_data *data)
57 if (data && data->binary_user)
58 return data->binary_user;
60 return connman_vpn_settings.binary_user;
63 const char *vpn_settings_get_binary_group(struct vpn_plugin_data *data)
65 if (data && data->binary_group)
66 return data->binary_group;
68 return connman_vpn_settings.binary_group;
71 char **vpn_settings_get_binary_supplementary_groups(struct vpn_plugin_data *data)
73 if (data && data->binary_supplementary_groups)
74 return data->binary_supplementary_groups;
76 return connman_vpn_settings.binary_supplementary_groups;
79 unsigned int __vpn_settings_get_timeout_inputreq()
81 return connman_vpn_settings.timeout_inputreq;
84 static char *get_string(GKeyFile *config, const char *group, const char *key)
86 char *str = g_key_file_get_string(config, group, key, NULL);
87 return str ? g_strstrip(str) : NULL;
90 static char **get_string_list(GKeyFile *config, const char *group,
94 char **str = g_key_file_get_string_list(config, group, key, &len, NULL);
99 for (i = 0; i < len ; i++) {
100 str[i] = g_strstrip(str[i]);
107 static void parse_config(GKeyFile *config, const char *file)
109 const char *group = "General";
110 GError *error = NULL;
116 DBG("parsing %s", file);
118 timeout = g_key_file_get_integer(config, group,
119 "InputRequestTimeout", &error);
120 if (!error && timeout >= 0)
121 connman_vpn_settings.timeout_inputreq = timeout * 1000;
123 g_clear_error(&error);
125 connman_vpn_settings.binary_user = get_string(config, VPN_GROUP,
127 connman_vpn_settings.binary_group = get_string(config, VPN_GROUP,
129 connman_vpn_settings.binary_supplementary_groups = get_string_list(
131 "SupplementaryGroups");
134 struct vpn_plugin_data *vpn_settings_get_vpn_plugin_config(const char *name)
136 struct vpn_plugin_data *data = NULL;
139 data = g_hash_table_lookup(plugin_hash, name);
144 static void vpn_plugin_data_free(gpointer data)
146 struct vpn_plugin_data *plugin_data = (struct vpn_plugin_data*)data;
148 g_free(plugin_data->binary_user);
149 g_free(plugin_data->binary_group);
150 g_strfreev(plugin_data->binary_supplementary_groups);
155 int vpn_settings_parse_vpn_plugin_config(const char *name)
157 struct vpn_plugin_data *data;
159 gchar *ext = ".conf";
166 if (vpn_settings_get_vpn_plugin_config(name))
169 file = g_strconcat(PLUGIN_CONFIGDIR, "/", name, ext, NULL);
171 config = __vpn_settings_load_config(file);
175 DBG("Cannot load config %s for %s", file, name);
179 data = g_try_new0(struct vpn_plugin_data, 1);
181 data->binary_user = get_string(config, VPN_GROUP, "User");
182 data->binary_group = get_string(config, VPN_GROUP, "Group");
183 data->binary_supplementary_groups = get_string_list(config, VPN_GROUP,
184 "SupplementaryGroups");
186 DBG("Loaded settings for %s: %s - %s",
187 name, data->binary_user, data->binary_group);
190 plugin_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
191 g_free, vpn_plugin_data_free);
193 g_hash_table_replace(plugin_hash, g_strdup(name), data);
195 g_key_file_unref(config);
202 void vpn_settings_delete_vpn_plugin_config(const char *name)
204 if (plugin_hash && name)
205 g_hash_table_remove(plugin_hash, name);
208 GKeyFile *__vpn_settings_load_config(const char *file)
213 keyfile = g_key_file_new();
215 g_key_file_set_list_separator(keyfile, ',');
217 if (!g_key_file_load_from_file(keyfile, file, 0, &err)) {
218 if (err->code != G_FILE_ERROR_NOENT) {
219 connman_error("Parsing %s failed: %s", file,
224 g_key_file_unref(keyfile);
231 int __vpn_settings_init(const char *file)
235 config = __vpn_settings_load_config(file);
236 parse_config(config, file);
238 g_key_file_unref(config);
243 void __vpn_settings_cleanup()
245 g_free(connman_vpn_settings.binary_user);
246 g_free(connman_vpn_settings.binary_group);
247 g_strfreev(connman_vpn_settings.binary_supplementary_groups);
250 g_hash_table_destroy(plugin_hash);