/
config.c
431 lines (320 loc) · 10.3 KB
/
config.c
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
#include <glib.h>
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define CONFIG_FREE_STRING_VAR(__var) \
do \
{ \
if ((__var) != NULL) \
{ \
g_free((gchar *)(__var)); \
(__var) = NULL; \
} \
} \
while (0)
#define CONFIG_CHECK_STRING_VAR(__var, __error, __failOnError) \
do \
{ \
if ((__failOnError) == TRUE && (__var) == NULL) \
{ \
fprintf(stderr, "Jolicloud-DisplayManager: No configuration entry found for " # __error "\n"); \
goto onError; \
} \
} \
while (0)
#define CONFIG_GET_STRING_VALUE(__group, __key, __var) \
do \
{ \
char* localTmp; \
GError* localError = NULL; \
localTmp = g_key_file_get_string(keyFile, (__group), (__key), &localError); \
if (localTmp != NULL) \
{ \
if (!(*localTmp)) \
{ \
g_free((gchar *)localTmp); \
localTmp = NULL; \
} \
if ((__var) != NULL) \
g_free((gchar *)(__var)); \
(__var) = (char *)localTmp; \
} \
else \
{ \
g_error_free(localError); \
} \
} \
while (0)
#define CONFIG_GET_BOOLEAN_VALUE(__group, __key, __var) \
do \
{ \
char* localTmp = NULL; \
GError* localError = NULL; \
localTmp = g_key_file_get_string(keyFile, (__group), (__key), &localError); \
if (localTmp != NULL) \
{ \
if (!strcasecmp(localTmp, "true")) \
(__var) = TRUE; \
else \
(__var) = FALSE; \
g_free((gchar *)localTmp); \
} \
else \
{ \
g_error_free(localError); \
} \
} \
while (0)
/* group: internals
*/
static char* g_configLockFilePath = NULL; /* lockfile */
static char* g_configLogFilePath = NULL; /* logfile */
/* group: xserver
*/
static char* g_configXAuthPath = NULL; /* xauth */
static char* g_configXAuthFilePath = NULL; /* xauthfile */
static char* g_configSessregPath = NULL; /* sessreg */
static char* g_configXServerPath = NULL; /* xserver */
static char* g_configXServerArgs = NULL; /* xserverargs */
/* group: theme
*/
static char* g_configThemeUrl = NULL; /* url */
/* group: user
*/
static char* g_configUserPath = NULL; /* path */
static char* g_configUserLoginCmd = NULL; /* logincmd */
/* group: guestmode
*/
static gboolean g_configGuestmodeEnabled = FALSE; /* enabled */
static char* g_configGuestmodeLogin = NULL; /* login */
static char* g_configGuestmodeGroup = NULL; /* group */
static char* g_configGuestmodeLoginCmd = NULL; /* logincmd */
/* group: autologin
*/
static gboolean g_configAutologinEnabled = FALSE; /* enabled */
static char* g_configAutologinLogin = NULL; /* login */
static gboolean _config_parse_file(const char* filePath);
static gboolean _config_is_filename_valid(const char* filename);
gboolean config_init(void)
{
return TRUE;
}
void config_cleanup(void)
{
CONFIG_FREE_STRING_VAR(g_configLockFilePath);
CONFIG_FREE_STRING_VAR(g_configLogFilePath);
CONFIG_FREE_STRING_VAR(g_configXAuthPath);
CONFIG_FREE_STRING_VAR(g_configXAuthFilePath);
CONFIG_FREE_STRING_VAR(g_configSessregPath);
CONFIG_FREE_STRING_VAR(g_configXServerPath);
CONFIG_FREE_STRING_VAR(g_configXServerArgs);
CONFIG_FREE_STRING_VAR(g_configThemeUrl);
CONFIG_FREE_STRING_VAR(g_configUserPath);
CONFIG_FREE_STRING_VAR(g_configUserLoginCmd);
CONFIG_FREE_STRING_VAR(g_configGuestmodeLogin);
CONFIG_FREE_STRING_VAR(g_configGuestmodeGroup);
CONFIG_FREE_STRING_VAR(g_configGuestmodeLoginCmd);
CONFIG_FREE_STRING_VAR(g_configAutologinLogin);
}
gboolean config_load(const char* configPath)
{
GDir* dir = NULL;
GError* error = NULL;
const gchar* configFile = NULL;
dir = g_dir_open(configPath, 0, &error);
if (dir == NULL)
{
fprintf(stderr, "Jolicloud-DisplayManager: Unable to open configuration path '%s'. Error '%s'\n",
configPath, error->message);
goto onError;
}
/* according to glib's documentation: DO NOT FREE RETURN VALUE OF g_dir_read_name
*/
while ((configFile = g_dir_read_name(dir)) != NULL)
{
gchar* filePath = NULL;
if (_config_is_filename_valid(configFile) == FALSE)
continue;
filePath = g_build_filename(configPath, configFile, NULL);
if (filePath == NULL)
{
fprintf(stderr, "Jolicloud-DisplayManager: Unable to construct configuration file path\n");
goto onError;
}
if (g_file_test(filePath, G_FILE_TEST_IS_REGULAR) == TRUE
&& _config_parse_file(filePath) == FALSE)
{
g_free(filePath);
goto onError;
}
g_free(filePath);
}
CONFIG_CHECK_STRING_VAR(g_configLockFilePath, "lockfile", TRUE);
CONFIG_CHECK_STRING_VAR(g_configLogFilePath, "logfile", TRUE);
CONFIG_CHECK_STRING_VAR(g_configXAuthPath, "xauth", TRUE);
CONFIG_CHECK_STRING_VAR(g_configXAuthFilePath, "xauthfile", TRUE);
CONFIG_CHECK_STRING_VAR(g_configSessregPath, "sessreg", TRUE);
CONFIG_CHECK_STRING_VAR(g_configXServerPath, "xserver", TRUE);
CONFIG_CHECK_STRING_VAR(g_configXServerArgs, "xserverargs", TRUE);
CONFIG_CHECK_STRING_VAR(g_configThemeUrl, "theme", TRUE);
CONFIG_CHECK_STRING_VAR(g_configUserPath, "path", TRUE);
CONFIG_CHECK_STRING_VAR(g_configUserLoginCmd, "logincmd", TRUE);
CONFIG_CHECK_STRING_VAR(g_configGuestmodeLogin, "[guestmode] login", FALSE);
CONFIG_CHECK_STRING_VAR(g_configGuestmodeGroup, "[guestmode] group", FALSE);
CONFIG_CHECK_STRING_VAR(g_configGuestmodeLoginCmd, "[guestmode] logincmd", FALSE);
CONFIG_CHECK_STRING_VAR(g_configAutologinLogin, "[autologin] login", FALSE);
g_dir_close(dir);
return TRUE;
onError:
if (error != NULL)
g_error_free(error);
if (dir != NULL)
g_dir_close(dir);
CONFIG_FREE_STRING_VAR(g_configLockFilePath);
CONFIG_FREE_STRING_VAR(g_configLogFilePath);
CONFIG_FREE_STRING_VAR(g_configXAuthPath);
CONFIG_FREE_STRING_VAR(g_configXAuthFilePath);
CONFIG_FREE_STRING_VAR(g_configSessregPath);
CONFIG_FREE_STRING_VAR(g_configXServerPath);
CONFIG_FREE_STRING_VAR(g_configXServerArgs);
CONFIG_FREE_STRING_VAR(g_configThemeUrl);
CONFIG_FREE_STRING_VAR(g_configUserPath);
CONFIG_FREE_STRING_VAR(g_configUserLoginCmd);
CONFIG_FREE_STRING_VAR(g_configGuestmodeLogin);
CONFIG_FREE_STRING_VAR(g_configGuestmodeGroup);
CONFIG_FREE_STRING_VAR(g_configGuestmodeLoginCmd);
CONFIG_FREE_STRING_VAR(g_configAutologinLogin);
return FALSE;
}
const char* config_lockfile_path_get(void)
{
return g_configLockFilePath;
}
const char* config_logfile_path_get(void)
{
return g_configLogFilePath;
}
const char* config_xauth_path_get(void)
{
return g_configXAuthPath;
}
const char* config_xauthfile_path_get(void)
{
return g_configXAuthFilePath;
}
const char* config_sessreg_path_get(void)
{
return g_configSessregPath;
}
const char* config_xserver_path_get(void)
{
return g_configXServerPath;
}
const char* config_xserverargs_get(void)
{
return g_configXServerArgs;
}
const char* config_theme_url_get(void)
{
return g_configThemeUrl;
}
const char* config_user_path_get(void)
{
return g_configUserPath;
}
const char* config_user_logincmd_get(void)
{
return g_configUserLoginCmd;
}
gboolean config_guestmode_enabled(void)
{
return g_configGuestmodeEnabled;
}
const char* config_guestmode_login_get(void)
{
return g_configGuestmodeLogin;
}
const char* config_guestmode_group_get(void)
{
return g_configGuestmodeGroup;
}
const char* config_guestmode_logincmd_get(void)
{
return g_configGuestmodeLoginCmd;
}
gboolean config_autologin_enabled(void)
{
return g_configAutologinEnabled;
}
const char* config_autologin_login_get(void)
{
return g_configAutologinLogin;
}
/* privates
*/
static gboolean _config_parse_file(const char* filePath)
{
GKeyFile* keyFile = NULL;
GError* error = NULL;
keyFile = g_key_file_new();
if (keyFile == NULL)
{
fprintf(stderr, "Jolicloud-DisplayManager: Unable to initialize parsing of configuration file '%s'\n",
filePath);
return FALSE;
}
if (g_key_file_load_from_file(keyFile, filePath, G_KEY_FILE_NONE, &error) == FALSE)
{
fprintf(stderr, "Jolicloud-DisplayManager: Failed to load configuration file '%s'. Error '%s'\n",
filePath, error->message);
goto onError;
}
/* we don't care about error because we may load more than one file,
each one having some part of the configuration.
Errors will be handled in the parent function.
*/
CONFIG_GET_STRING_VALUE("internals", "lockfile", g_configLockFilePath);
CONFIG_GET_STRING_VALUE("internals", "logfile", g_configLogFilePath);
CONFIG_GET_STRING_VALUE("xserver", "xauth", g_configXAuthPath);
CONFIG_GET_STRING_VALUE("xserver", "xauthfile", g_configXAuthFilePath);
CONFIG_GET_STRING_VALUE("xserver", "sessreg", g_configSessregPath);
CONFIG_GET_STRING_VALUE("xserver", "xserver", g_configXServerPath);
CONFIG_GET_STRING_VALUE("xserver", "xserverargs", g_configXServerArgs);
CONFIG_GET_STRING_VALUE("theme", "url", g_configThemeUrl);
CONFIG_GET_STRING_VALUE("user", "path", g_configUserPath);
CONFIG_GET_STRING_VALUE("user", "logincmd", g_configUserLoginCmd);
CONFIG_GET_BOOLEAN_VALUE("guestmode", "enabled", g_configGuestmodeEnabled);
CONFIG_GET_STRING_VALUE("guestmode", "login", g_configGuestmodeLogin);
CONFIG_GET_STRING_VALUE("guestmode", "group", g_configGuestmodeGroup);
CONFIG_GET_STRING_VALUE("guestmode", "logincmd", g_configGuestmodeLoginCmd);
CONFIG_GET_BOOLEAN_VALUE("autologin", "enabled", g_configAutologinEnabled);
CONFIG_GET_STRING_VALUE("autologin", "login", g_configAutologinLogin);
g_key_file_free(keyFile);
return TRUE;
onError:
if (error != NULL)
g_error_free(error);
if (keyFile != NULL)
g_key_file_free(keyFile);
return FALSE;
}
static gboolean endswith(const char* string, const char* token)
{
const size_t stringLen = strlen(string);
const size_t tokenLen = strlen(token);
if (stringLen == 0 || token == 0 || stringLen < tokenLen)
return FALSE;
if (!strcmp(string + (stringLen - tokenLen), token))
return TRUE;
return FALSE;
}
static gboolean _config_is_filename_valid(const char* filename)
{
if (!filename || !(*filename) || *filename == '.')
return FALSE;
if (endswith(filename, "~") == TRUE
|| endswith(filename, ".oem") == TRUE)
return FALSE;
return TRUE;
}