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
465
466
467
468
|
/*
Copyright 2012 Alexandre Rostovtsev
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 St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <glib.h>
#include <glib/gstdio.h>
#include <gio/gio.h>
#include "shell-utils.h"
#include "config.h"
GRegex *indent_regex = NULL;
GRegex *comment_regex = NULL;
GRegex *separator_regex = NULL;
GRegex *var_equals_regex = NULL;
GRegex *single_quoted_regex = NULL;
GRegex *double_quoted_regex = NULL;
GRegex *unquoted_regex = NULL;
enum ShellEntryType {
SHELL_ENTRY_TYPE_INDENT,
SHELL_ENTRY_TYPE_COMMENT,
SHELL_ENTRY_TYPE_SEPARATOR,
SHELL_ENTRY_TYPE_ASSIGNMENT,
};
struct ShellEntry {
enum ShellEntryType type;
gchar *string;
gchar *variable; /* only relevant for assignments */
};
gchar *
shell_utils_source_var (const gchar *filename,
const gchar *variable,
GError **error)
{
gchar *argv[4] = { "sh", "-c", NULL, NULL };
gchar *quoted_filename = NULL;
gchar *output = NULL;
quoted_filename = g_shell_quote (filename);
argv[2] = g_strdup_printf (". %s; echo -n %s", quoted_filename, variable);
g_free (quoted_filename);
if (g_file_test (filename, G_FILE_TEST_IS_REGULAR))
if (!g_spawn_sync (NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL, &output, NULL, NULL, error)) {
g_prefix_error (error, "Unable to source '%s': ", filename);
}
g_free (argv[2]);
return output;
}
static void
shell_entry_free (struct ShellEntry *entry)
{
if (entry == NULL)
return;
if (entry->string != NULL)
g_free (entry->string);
if (entry->variable != NULL)
g_free (entry->variable);
g_free (entry);
}
void
shell_utils_trivial_free (ShellUtilsTrivial *trivial)
{
if (trivial == NULL)
return;
if (trivial->filename != NULL)
g_free (trivial->filename);
if (trivial->entry_list != NULL)
g_list_free_full (trivial->entry_list, (GDestroyNotify)shell_entry_free);
g_free (trivial);
}
ShellUtilsTrivial *
shell_utils_trivial_new (const gchar *filename,
GError **error)
{
gchar *filebuf = NULL;
ShellUtilsTrivial *ret = NULL;
GError *local_err;
gchar *s;
g_assert (filename != NULL);
ret = g_new0 (ShellUtilsTrivial, 1);
ret->filename = g_strdup (filename);
if (!g_file_get_contents (filename, &filebuf, NULL, &local_err)) {
if (local_err != NULL) {
/* Inability to parse or open is a failure; file not existing at all is *not* a failure */
if (local_err->code == G_FILE_ERROR_NOENT) {
g_error_free (local_err);
return ret;
}
g_propagate_prefixed_error (error, local_err, "Unable to read '%s':", filename);
}
shell_utils_trivial_free (ret);
return NULL;
}
gboolean want_separator = FALSE; /* Do we expect the next entry to be a separator or comment? */
s = filebuf;
while (*s != 0) {
#ifdef OPENRC_SETTINGSD_DEBUG
g_debug ("Scanning string: ``%s''", s);
#endif
gboolean matched = FALSE;
GMatchInfo *match_info = NULL;
struct ShellEntry *entry = NULL;
matched = g_regex_match (comment_regex, s, 0, &match_info);
if (matched) {
entry = g_new0 (struct ShellEntry, 1);
entry->type = SHELL_ENTRY_TYPE_COMMENT;
entry->string = g_match_info_fetch (match_info, 0);
ret->entry_list = g_list_prepend (ret->entry_list, entry);
s += strlen (entry->string);
#ifdef OPENRC_SETTINGSD_DEBUG
g_debug ("Scanned comment: ``%s''", entry->string);
#endif
g_match_info_free (match_info);
match_info = NULL;
want_separator = FALSE;
continue;
}
g_match_info_free (match_info);
match_info = NULL;
matched = g_regex_match (separator_regex, s, 0, &match_info);
if (matched) {
entry = g_new0 (struct ShellEntry, 1);
entry->type = SHELL_ENTRY_TYPE_SEPARATOR;
entry->string = g_match_info_fetch (match_info, 0);
ret->entry_list = g_list_prepend (ret->entry_list, entry);
s += strlen (entry->string);
#ifdef OPENRC_SETTINGSD_DEBUG
g_debug ("Scanned separator: ``%s''", entry->string);
#endif
g_match_info_free (match_info);
match_info = NULL;
want_separator = FALSE;
continue;
}
g_match_info_free (match_info);
match_info = NULL;
matched = g_regex_match (indent_regex, s, 0, &match_info);
if (matched) {
entry = g_new0 (struct ShellEntry, 1);
entry->type = SHELL_ENTRY_TYPE_INDENT;
entry->string = g_match_info_fetch (match_info, 0);
ret->entry_list = g_list_prepend (ret->entry_list, entry);
s += strlen (entry->string);
#ifdef OPENRC_SETTINGSD_DEBUG
g_debug ("Scanned indent: ``%s''", entry->string);
#endif
g_match_info_free (match_info);
match_info = NULL;
continue;
}
g_match_info_free (match_info);
match_info = NULL;
matched = g_regex_match (var_equals_regex, s, 0, &match_info);
if (matched) {
/* If we expect a separator and get an assignment instead, fail */
if (want_separator)
goto no_match;
entry = g_new0 (struct ShellEntry, 1);
entry->type = SHELL_ENTRY_TYPE_ASSIGNMENT;
entry->string = g_match_info_fetch (match_info, 0);
entry->variable = g_match_info_fetch (match_info, 1);
s += strlen (entry->string);
#ifdef OPENRC_SETTINGSD_DEBUG
g_debug ("Scanned variable: ``%s''", entry->string);
#endif
g_match_info_free (match_info);
match_info = NULL;
want_separator = TRUE;
while (*s != 0) {
#ifdef OPENRC_SETTINGSD_DEBUG
g_debug ("Scanning string for values: ``%s''", s);
#endif
gboolean matched2 = FALSE;
gchar *temp1 = NULL, *temp2 = NULL;
matched2 = g_regex_match (single_quoted_regex, s, 0, &match_info);
if (matched2)
goto append_value;
g_match_info_free (match_info);
match_info = NULL;
matched2 = g_regex_match (double_quoted_regex, s, 0, &match_info);
if (matched2)
goto append_value;
g_match_info_free (match_info);
match_info = NULL;
matched2 = g_regex_match (unquoted_regex, s, 0, &match_info);
if (matched2)
goto append_value;
g_match_info_free (match_info);
match_info = NULL;
break;
append_value:
temp1 = entry->string;
temp2 = g_match_info_fetch (match_info, 0);
entry->string = g_strconcat (temp1, temp2, NULL);
s += strlen (temp2);
#ifdef OPENRC_SETTINGSD_DEBUG
g_debug ("Scanned value: ``%s''", temp2);
#endif
g_free (temp1);
g_free (temp2);
g_match_info_free (match_info);
match_info = NULL;
}
ret->entry_list = g_list_prepend (ret->entry_list, entry);
continue;
}
no_match:
/* Nothing matches, parsing has failed! */
g_match_info_free (match_info);
match_info = NULL;
g_propagate_error (error,
g_error_new (G_FILE_ERROR, G_FILE_ERROR_FAILED,
"Unable to parse '%s'", filename));
shell_utils_trivial_free (ret);
return NULL;
}
ret->entry_list = g_list_reverse (ret->entry_list);
return ret;
}
gboolean
shell_utils_trivial_set_variable (ShellUtilsTrivial *trivial,
const gchar *variable,
const gchar *value,
gboolean add_if_unset)
{
GList *curr = NULL;
struct ShellEntry *found_entry = NULL;
gchar *quoted_value = NULL;
gboolean ret = FALSE;
g_assert (trivial != NULL);
g_assert (variable != NULL);
for (curr = trivial->entry_list; curr != NULL; curr = curr->next) {
struct ShellEntry *entry;
entry = (struct ShellEntry *)(curr->data);
if (entry->type == SHELL_ENTRY_TYPE_ASSIGNMENT && g_strcmp0 (variable, entry->variable) == 0)
found_entry = entry;
}
quoted_value = g_shell_quote (value);
if (found_entry != NULL) {
g_free (found_entry->string);
found_entry->string = g_strdup_printf ("%s=%s", variable, quoted_value);
ret = TRUE;
} else {
if (add_if_unset) {
found_entry = g_new0 (struct ShellEntry, 1);
found_entry->type = SHELL_ENTRY_TYPE_ASSIGNMENT;
found_entry->variable = g_strdup (variable);
found_entry->string = g_strdup_printf ("%s=%s", variable, quoted_value);
trivial->entry_list = g_list_append (trivial->entry_list, found_entry);
ret = TRUE;
}
}
g_free (quoted_value);
return ret;
}
gboolean
shell_utils_trivial_save (ShellUtilsTrivial *trivial,
GError **error)
{
GList *curr = NULL;
gchar *temp_filename = NULL;
gint fd;
gboolean retval = FALSE;
GStatBuf stat_buf;
gint mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH; /* 644 by default */
g_assert (trivial != NULL);
g_assert (trivial->filename != NULL);
/* If the file exists, preserve its mode */
if (!g_lstat (trivial->filename, &stat_buf))
mode = stat_buf.st_mode;
temp_filename = g_strdup_printf ("%s.XXXXXX", trivial->filename);
if ((fd = g_mkstemp_full (temp_filename, O_WRONLY, mode)) < 0) {
int errsv = errno;
GFileError file_err;
file_err = g_file_error_from_errno (errsv);
g_propagate_prefixed_error (error,
g_error_copy ((GError*)&file_err),
"Unable to write '%s': Unable to create temp file: ",
trivial->filename);
goto out;
}
for (curr = trivial->entry_list; curr != NULL; curr = curr->next) {
struct ShellEntry *entry;
ssize_t written;
entry = (struct ShellEntry *)(curr->data);
written = write (fd, entry->string, strlen (entry->string));
int errsv = errno;
if (written < strlen (entry->string)) {
if (written < 0) {
int errsv = errno;
GFileError file_err;
file_err = g_file_error_from_errno (errsv);
g_propagate_prefixed_error (error,
g_error_copy ((GError*)&file_err),
"Unable to write temp file '%s': ",
temp_filename);
} else {
g_propagate_error (error,
g_error_new (G_FILE_ERROR, G_FILE_ERROR_FAILED,
"Unable to write temp file '%s'", temp_filename));
}
close (fd);
goto out;
}
}
if (fsync (fd) || close (fd)) {
int errsv = errno;
GFileError file_err;
file_err = g_file_error_from_errno (errsv);
g_propagate_prefixed_error (error,
g_error_copy ((GError*)&file_err),
"Unable to sync or close temp file '%s': ",
temp_filename);
close (fd);
goto out;
}
if (g_rename (temp_filename, trivial->filename) < 0) {
int errsv = errno;
GFileError file_err;
file_err = g_file_error_from_errno (errsv);
g_propagate_prefixed_error (error,
g_error_copy ((GError*)&file_err),
"Unable to copy '%s' to '%s': ",
temp_filename, trivial->filename);
goto out;
}
retval = TRUE;
out:
g_free (temp_filename);
g_unlink (temp_filename);
return retval;
}
void
shell_utils_destroy (void)
{
if (indent_regex != NULL) {
g_regex_unref (indent_regex);
indent_regex = NULL;
}
if (comment_regex != NULL) {
g_regex_unref (comment_regex);
comment_regex = NULL;
}
if (separator_regex != NULL) {
g_regex_unref (separator_regex);
separator_regex = NULL;
}
if (var_equals_regex != NULL) {
g_regex_unref (var_equals_regex);
var_equals_regex = NULL;
}
if (single_quoted_regex != NULL) {
g_regex_unref (single_quoted_regex);
single_quoted_regex = NULL;
}
if (double_quoted_regex != NULL) {
g_regex_unref (double_quoted_regex);
double_quoted_regex = NULL;
}
if (unquoted_regex != NULL) {
g_regex_unref (unquoted_regex);
unquoted_regex = NULL;
}
}
void
shell_utils_init (void)
{
if (indent_regex == NULL) {
indent_regex = g_regex_new ("^[ \\t]+", G_REGEX_ANCHORED, 0, NULL);
g_assert (indent_regex != NULL);
}
if (comment_regex == NULL) {
comment_regex = g_regex_new ("^#[^\\n]*\\n", G_REGEX_ANCHORED|G_REGEX_MULTILINE, 0, NULL);
g_assert (comment_regex != NULL);
}
if (separator_regex == NULL) {
separator_regex = g_regex_new ("^[ \\t;\\n\\r]*[;\\n][ \\t;\\n\\r]*", G_REGEX_ANCHORED|G_REGEX_MULTILINE, 0, NULL);
g_assert (separator_regex != NULL);
}
if (var_equals_regex == NULL) {
var_equals_regex = g_regex_new ("^([a-zA-Z_][a-zA-Z0-9_]*)(?:(?:\\\\\\n)*)=(?:(?:\\\\\\n)*)", G_REGEX_ANCHORED|G_REGEX_MULTILINE, 0, NULL);
g_assert (var_equals_regex != NULL);
}
if (single_quoted_regex == NULL) {
single_quoted_regex = g_regex_new ("^'[^']*'", G_REGEX_ANCHORED|G_REGEX_MULTILINE, 0, NULL);
g_assert (single_quoted_regex != NULL);
}
/* We do not want to allow $(...) or `...` constructs in double-quoted
* strings because they might have side effects, but ${...} is OK */
if (double_quoted_regex == NULL) {
double_quoted_regex = g_regex_new ("^\"(?:[^\"`\\$]|\\\\[\"`\\$]|\\$\\{)*\"", G_REGEX_ANCHORED|G_REGEX_MULTILINE, 0, NULL);
g_assert (double_quoted_regex != NULL);
}
if (unquoted_regex == NULL) {
unquoted_regex = g_regex_new ("^(?:[^\\s\"'`\\$\\|&<>;]|\\\\[\\s\"'`\\$\\|&<>;]|\\$\\{)+", G_REGEX_ANCHORED|G_REGEX_MULTILINE, 0, NULL);
g_assert (unquoted_regex != NULL);
}
}
|