1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
|
/* vi: set et sw=4 ts=4 cino=t0,(0: */
/* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of gsignond
*
* Copyright (C) 2012 Intel Corporation.
*
* Contact: Jussi Laako <jussi.laako@linux.intel.com>
* Amarnath Valluri <amarnath.valluri@linux.intel.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA
*/
#include <stdlib.h>
#include <unistd.h>
#include <glib/gstdio.h>
#include "config.h"
#include "gsignond/gsignond-config.h"
#include "gsignond/gsignond-config-general.h"
#include "gsignond/gsignond-config-db.h"
#include "gsignond/gsignond-config-dbus.h"
#include "gsignond/gsignond-log.h"
#include "gsignond/gsignond-dictionary.h"
/**
* SECTION:gsignond-config
* @short_description: gSSO configuration information
* @include: gsignond/gsignond-config.h
*
* #GSignondConfig holds configuration information as a set of keys and values
* (integer or strings). The key names are defined in
* <link linkend="gsignond-General-configuration">general config keys</link>,
* <link linkend="gsignond-Database-configuration">database config keys</link>, and
* <link linkend="gsignond-DBus-configuration">DBus config keys</link>.
*
* The configuration is discovered from these sources, in decreasing order of
* priority:
* - environment variables, if gSSO has been compiled with --enable-debug switch.
* See the specific keys documentation for the variable names.
* - gSSO configuration file. See below for where the file is searched for.
* - default values. See the documentation for specific keys for those.
*
* <refsect1><title>Where the configuration file is searched for</title></refsect1>
*
* If gSSO has been compiled with --enable-debug, then these locations are used,
* in decreasing order of priority:
* - GSIGNOND_CONFIG environment variable
* - g_get_user_config_dir() + "gsignond/gsignond.conf"
* - each of g_get_system_config_dirs() + "gsignond/gsignond.conf"
*
* Otherwise, the config file location is determined at compilation time as
* $(sysconfdir) + "gsignond/gsignond.conf"
*
* <refsect1><title>Example configuration file</title></refsect1>
*
* See example configuration file here:
* <ulink url="http://code.google.com/p/accounts-sso/source/browse/gsignond.conf?repo=gsignond">
* http://code.google.com/p/accounts-sso/source/browse/gsignond.conf?repo=gsignond</ulink>
*/
/**
* GSignondConfig:
*
* Opaque structure for the object.
*/
/**
* GSignondConfigClass:
*
* Opaque structure for the class.
*/
#define GSIGNOND_DB_METADATA_DEFAULT_DB_FILENAME "metadata.db"
#define GSIGNOND_DB_SECRET_DEFAULT_DB_FILENAME "secret.db"
struct _GSignondConfigPrivate
{
gchar *config_file_path;
GSignondDictionary *config_table;
};
#define GSIGNOND_CONFIG_PRIV(obj) G_TYPE_INSTANCE_GET_PRIVATE ((obj), GSIGNOND_TYPE_CONFIG, GSignondConfigPrivate)
G_DEFINE_TYPE (GSignondConfig, gsignond_config, G_TYPE_OBJECT);
static void
_set_storage_path (GSignondConfig *self, const gchar *value)
{
gchar *storage_path = g_build_filename (value,
"gsignond.general",
NULL);
gsignond_config_set_string (self,
GSIGNOND_CONFIG_GENERAL_STORAGE_PATH,
storage_path);
g_free (storage_path);
}
static gboolean
_load_config (GSignondConfig *self)
{
gchar *def_config;
GError *err = NULL;
gchar **groups = NULL;
gsize n_groups = 0;
int i,j;
GKeyFile *settings = g_key_file_new ();
# ifdef ENABLE_DEBUG
const gchar * const *sysconfdirs;
if (!self->priv->config_file_path) {
def_config = g_strdup (g_getenv ("GSIGNOND_CONFIG"));
if (!def_config)
def_config = g_build_filename (g_get_user_config_dir(),
"gsignond/gsignond.conf",
NULL);
if (g_access (def_config, R_OK) == 0) {
self->priv->config_file_path = def_config;
} else {
g_free (def_config);
sysconfdirs = g_get_system_config_dirs ();
while (*sysconfdirs != NULL) {
def_config = g_build_filename (*sysconfdirs,
"gsignond/gsignond.conf",
NULL);
if (g_access (def_config, R_OK) == 0) {
self->priv->config_file_path = def_config;
break;
}
g_free (def_config);
sysconfdirs++;
}
}
}
# else /* ENABLE_DEBUG */
# ifndef GSIGNOND_SYSCONF_DIR
# error "System configuration directory not defined!"
# endif
def_config = g_build_filename (GSIGNOND_SYSCONF_DIR,
"gsignond/gsignond.conf",
NULL);
if (g_access (def_config, R_OK) == 0) {
self->priv->config_file_path = def_config;
} else {
g_free (def_config);
}
# endif /* ENABLE_DEBUG */
if (self->priv->config_file_path) {
DBG ("Loading SSO config from %s", self->priv->config_file_path);
if (!g_key_file_load_from_file (settings,
self->priv->config_file_path,
G_KEY_FILE_NONE, &err)) {
WARN ("error reading config file at '%s': %s",
self->priv->config_file_path, err->message);
g_error_free (err);
g_key_file_free (settings);
return FALSE;
}
}
groups = g_key_file_get_groups (settings, &n_groups);
for (i = 0; i < n_groups; i++) {
GError *err = NULL;
gsize n_keys =0;
gchar **keys = g_key_file_get_keys (settings,
groups[i],
&n_keys,
&err);
if (err) {
WARN ("fail to read group '%s': %s", groups[i], err->message);
g_error_free (err);
continue;
}
for (j = 0; j < n_keys; j++) {
gchar *key = g_strdup_printf ("%s/%s", groups[i], keys[j]);
gchar *value = g_key_file_get_value (settings,
groups[i],
keys[j],
&err);
if (err) {
WARN ("fail to read key '%s/%s': %s", groups[i], keys[j], err->message);
g_error_free (err);
continue;
}
INFO ("found config : '%s/%s' - '%s'", groups[i], keys[j], value);
/* construct a full storage path for wipe safety */
if (g_strcmp0 (key, GSIGNOND_CONFIG_GENERAL_STORAGE_PATH) == 0)
_set_storage_path (self, value);
else
gsignond_config_set_string (self, key, value);
g_free (key);
g_free (value);
}
g_strfreev (keys);
}
g_strfreev (groups);
g_key_file_free (settings);
return TRUE;
}
#ifdef ENABLE_DEBUG
static void
_load_environment (GSignondConfig *self)
{
const gchar *e_val = 0;
guint timeout = 0;
e_val = g_getenv ("SSO_DAEMON_TIMEOUT");
if (e_val && (timeout = atoi(e_val)))
gsignond_config_set_string (self,
GSIGNOND_CONFIG_DBUS_DAEMON_TIMEOUT,
e_val);
e_val = g_getenv ("SSO_IDENTITY_TIMEOUT");
if (e_val && (timeout = atoi(e_val)))
gsignond_config_set_string (self,
GSIGNOND_CONFIG_DBUS_IDENTITY_TIMEOUT,
e_val);
e_val = g_getenv ("SSO_AUTH_SESSION_TIMEOUT");
if (e_val && (timeout = atoi(e_val)))
gsignond_config_set_string (self,
GSIGNOND_CONFIG_DBUS_AUTH_SESSION_TIMEOUT,
e_val);
e_val = g_getenv ("SSO_PLUGIN_TIMEOUT");
if (e_val && (timeout = atoi(e_val)))
gsignond_config_set_string (self,
GSIGNOND_CONFIG_PLUGIN_TIMEOUT,
e_val);
e_val = g_getenv ("SSO_PLUGINS_DIR");
if (e_val)
gsignond_config_set_string (self,
GSIGNOND_CONFIG_GENERAL_PLUGINS_DIR,
e_val);
e_val = g_getenv ("SSO_EXTENSIONS_DIR");
if (e_val)
gsignond_config_set_string (self,
GSIGNOND_CONFIG_GENERAL_EXTENSIONS_DIR,
e_val);
e_val = g_getenv ("SSO_BIN_DIR");
if (e_val)
gsignond_config_set_string (self,
GSIGNOND_CONFIG_GENERAL_BIN_DIR,
e_val);
e_val = g_getenv ("SSO_EXTENSION");
if (e_val)
gsignond_config_set_string (self,
GSIGNOND_CONFIG_GENERAL_EXTENSION,
e_val);
e_val = g_getenv ("SSO_STORAGE_PATH");
if (e_val)
_set_storage_path (self, e_val);
}
#endif /* ENABLE_DEBUG */
/**
* gsignond_config_get_integer:
* @self: an instance of #GSignondConfig
* @key: the key name
*
* Get an integer configuration value.
*
* Returns: the value corresponding to the key as an integer. If the key does not
* exist or cannot be converted to the integer, 0 is returned.
*/
gint
gsignond_config_get_integer (GSignondConfig *self, const gchar *key)
{
const gchar *str_value = gsignond_config_get_string (self, key);
return (gint) (str_value ? atoi (str_value) : 0);
}
/**
* gsignond_config_set_integer:
* @self: an instance of #GSignondConfig
* @key: the key name
* @value: the value
*
* Sets the configuration value to the provided integer.
*/
void
gsignond_config_set_integer (GSignondConfig *self, const gchar *key,
gint value)
{
gchar *s_value = 0;
g_return_if_fail (self && GSIGNOND_IS_CONFIG (self));
s_value = g_strdup_printf ("%d", value);
if (!s_value) return;
gsignond_config_set_string (self, (gpointer) key, s_value);
g_free (s_value);
}
/**
* gsignond_config_get_string:
* @self: an instance of #GSignondConfig
* @key: the key name
*
* Get a string configuration value.
*
* Returns: (transfer none): the value corresponding to the key as string. If the key does not
* exist, NULL is returned.
*/
const gchar *
gsignond_config_get_string (GSignondConfig *self, const gchar *key)
{
g_return_val_if_fail (self && GSIGNOND_IS_CONFIG (self), NULL);
GVariant* value = gsignond_dictionary_get (self->priv->config_table,
(gpointer) key);
if (!value) return NULL;
return g_variant_get_string (value, NULL);
}
/**
* gsignond_config_set_string:
* @self: an instance of #GSignondConfig
* @key: the key name
* @value: (transfer none): the value
*
* Sets the configuration value to the provided string.
*/
void
gsignond_config_set_string (GSignondConfig *self, const gchar *key,
const gchar *value)
{
g_return_if_fail (self && GSIGNOND_IS_CONFIG (self));
gsignond_dictionary_set (self->priv->config_table,
(gpointer) key,
g_variant_new_string (value));
}
static void
gsignond_config_dispose (GObject *object)
{
GSignondConfig *self = 0;
g_return_if_fail (object && GSIGNOND_IS_CONFIG (object));
self = GSIGNOND_CONFIG (object);
if (self->priv->config_table) {
gsignond_dictionary_unref (self->priv->config_table);
self->priv->config_table = NULL;
}
G_OBJECT_CLASS (gsignond_config_parent_class)->dispose (object);
}
static void
gsignond_config_finalize (GObject *object)
{
GSignondConfig *self = 0;
g_return_if_fail (object && GSIGNOND_IS_CONFIG (object));
self = GSIGNOND_CONFIG (object);
if (self->priv->config_file_path) {
g_free (self->priv->config_file_path);
self->priv->config_file_path = NULL;
}
G_OBJECT_CLASS (gsignond_config_parent_class)->finalize (object);
}
static void
gsignond_config_init (GSignondConfig *self)
{
self->priv = GSIGNOND_CONFIG_PRIV (self);
self->priv->config_file_path = NULL;
self->priv->config_table = gsignond_dictionary_new();
gsignond_config_set_string (self,
GSIGNOND_CONFIG_GENERAL_PLUGINS_DIR,
(GSIGNOND_PLUGINS_DIR));
gsignond_config_set_string (self,
GSIGNOND_CONFIG_GENERAL_EXTENSIONS_DIR,
(GSIGNOND_EXTENSIONS_DIR));
gsignond_config_set_string (self,
(GSIGNOND_CONFIG_GENERAL_BIN_DIR),
(GSIGNOND_BIN_DIR));
gsignond_config_set_string (self,
GSIGNOND_CONFIG_GENERAL_STORAGE_PATH,
"/var/db");
gsignond_config_set_string (self,
GSIGNOND_CONFIG_DB_SECRET_DB_FILENAME,
GSIGNOND_DB_SECRET_DEFAULT_DB_FILENAME);
gsignond_config_set_string (self,
GSIGNOND_CONFIG_DB_METADATA_DB_FILENAME,
GSIGNOND_DB_METADATA_DEFAULT_DB_FILENAME);
if (!_load_config (self))
WARN ("load configuration failed, using default settings");
# ifdef ENABLE_DEBUG
_load_environment (self);
# endif
}
static void
gsignond_config_class_init (GSignondConfigClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
g_type_class_add_private (object_class, sizeof (GSignondConfigPrivate));
object_class->dispose = gsignond_config_dispose;
object_class->finalize = gsignond_config_finalize;
}
/**
* gsignond_config_new:
*
* Create a #GSignondConfig object.
*
* Returns: an instance of #GSignondConfig. gSSO extensions should not use this
* as they're already provided with a config object when they're created.
*/
GSignondConfig *
gsignond_config_new ()
{
return GSIGNOND_CONFIG (g_object_new (GSIGNOND_TYPE_CONFIG, NULL));
}
|