| 1 |
/* |
| 2 |
* Copyright (C) 2015 Red Hat, Inc. |
| 3 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
*/ |
| 5 |
|
| 6 |
#include "config.h" |
| 7 |
|
| 8 |
#include "cockpitconf.h" |
| 9 |
#include "cockpitmemory.h" |
| 10 |
|
| 11 |
#include <ctype.h> |
| 12 |
#include <err.h> |
| 13 |
#include <errno.h> |
| 14 |
#include <libgen.h> |
| 15 |
#include <limits.h> |
| 16 |
#include <regex.h> |
| 17 |
#include <stdio.h> |
| 18 |
#include <stdlib.h> |
| 19 |
#include <string.h> |
| 20 |
|
| 21 |
/* #define DEBUG 1 */ |
| 22 |
#if DEBUG |
| 23 |
#define debug(fmt, ...) (fprintf (stderr, "cockpitconf: " fmt "\n", ##__VA_ARGS__)) |
| 24 |
#else |
| 25 |
#define debug(...) |
| 26 |
#endif |
| 27 |
|
| 28 |
|
| 29 |
typedef struct Entry { |
| 30 |
char *section; |
| 31 |
char *key; |
| 32 |
char *value; |
| 33 |
char *strv_value; /* copy of value with all strv_delimiters replaced with \0 */ |
| 34 |
char strv_delimiter; |
| 35 |
const char **strv_cache; /* value split by strv_delimiter; points into strv_value */ |
| 36 |
struct Entry *next; |
| 37 |
} Entry; |
| 38 |
|
| 39 |
static bool cockpit_conf_loaded = false; |
| 40 |
static Entry *cockpit_conf = NULL; |
| 41 |
|
| 42 |
const char *cockpit_config_file = "cockpit.conf"; |
| 43 |
|
| 44 |
/* |
| 45 |
* some helper functions for safe memory allocation |
| 46 |
*/ |
| 47 |
|
| 48 |
static void |
| 49 |
regcompx (regex_t *preg, const char *regex, int cflags) |
| 50 |
{ |
| 51 |
int ret = regcomp (preg, regex, cflags); |
| 52 |
if (ret != 0) |
| 53 |
{ |
| 54 |
char err[1024]; |
| 55 |
regerror (ret, preg, err, sizeof (err)); |
| 56 |
fprintf (stderr, "failed to compile regular expression: %s\n", err); |
| 57 |
abort (); |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
/* For optimization, this modifies string; the returned array has pointers into string |
| 62 |
* The array itself gets allocated and must be freed after use. */ |
| 63 |
static const char ** |
| 64 |
strsplit (char *string, char delimiter) |
| 65 |
{ |
| 66 |
const char ** parts = reallocarrayx (NULL, 2, sizeof (char*)); |
| 67 |
char *cur = string; |
| 68 |
bool done = false; |
| 69 |
unsigned len = 0; |
| 70 |
|
| 71 |
/* backwards compatible special case: a totally empty string gives [], while ":" splits into ["", ""] */ |
| 72 |
if (string && *string) |
| 73 |
{ |
| 74 |
while (!done) |
| 75 |
{ |
| 76 |
char *next_delim = strchr (cur, delimiter); |
| 77 |
|
| 78 |
if (next_delim) |
| 79 |
*next_delim = '\0'; |
| 80 |
else |
| 81 |
done = true; |
| 82 |
|
| 83 |
parts = reallocarrayx (parts, len + 2, sizeof (char*)); |
| 84 |
parts[len++] = cur; |
| 85 |
|
| 86 |
if (next_delim) |
| 87 |
cur = next_delim + 1; |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
parts[len] = NULL; |
| 92 |
return parts; |
| 93 |
} |
| 94 |
|
| 95 |
/* |
| 96 |
* internal logic/helpers |
| 97 |
*/ |
| 98 |
|
| 99 |
/* See https://developer.gnome.org/glib/stable/glib-Key-value-file-parser.html for the spec */ |
| 100 |
static bool |
| 101 |
load_key_file (const char *file_path) |
| 102 |
{ |
| 103 |
FILE *f = NULL; |
| 104 |
char *cur_section = NULL; |
| 105 |
regex_t re_section, re_keyval, re_ignore; |
| 106 |
char *line = NULL; |
| 107 |
bool ret = true; |
| 108 |
size_t line_size = 0; |
| 109 |
|
| 110 |
cockpit_conf_loaded = true; |
| 111 |
|
| 112 |
f = fopen (file_path, "r"); |
| 113 |
if (!f) |
| 114 |
{ |
| 115 |
if (errno != ENOENT) |
| 116 |
warnx ("couldn't load configuration file: %s: %m\n", file_path); |
| 117 |
return false; |
| 118 |
} |
| 119 |
|
| 120 |
regcompx (&re_section, "^[[:space:]]*\\[([^][[:cntrl:]]+)\\][[:space:]]*$", REG_EXTENDED|REG_NEWLINE); |
| 121 |
regcompx (&re_keyval, "^[[:space:]]*([[:alnum:]-]+)[[:space:]]*=[[:space:]]*(.*)$", REG_EXTENDED|REG_NEWLINE); |
| 122 |
regcompx (&re_ignore, "^[[:space:]]*(#.*)?$", REG_EXTENDED|REG_NOSUB); |
| 123 |
|
| 124 |
for (;;) |
| 125 |
{ |
| 126 |
/* getline returns with -1 and not setting errno on EOL */ |
| 127 |
errno = 0; |
| 128 |
if (getline (&line, &line_size, f) < 0) |
| 129 |
{ |
| 130 |
if (errno != 0) |
| 131 |
{ |
| 132 |
perror ("failed to read line from config file"); |
| 133 |
abort (); |
| 134 |
} |
| 135 |
else |
| 136 |
{ |
| 137 |
break; /* EOL */ |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
/* maximum number of () matches that we want to capture from the above REs, + 1 for the entire string (group 0) */ |
| 142 |
const int MAX_MATCH = 3; |
| 143 |
regmatch_t matches[MAX_MATCH]; |
| 144 |
|
| 145 |
if (regexec (&re_section, line, MAX_MATCH, matches, 0) == 0) |
| 146 |
{ |
| 147 |
free (cur_section); |
| 148 |
cur_section = strndupx (line + matches[1].rm_so, matches[1].rm_eo - matches[1].rm_so); |
| 149 |
} |
| 150 |
|
| 151 |
else if (regexec (&re_keyval, line, 3, matches, 0) == 0) |
| 152 |
{ |
| 153 |
Entry *e; |
| 154 |
|
| 155 |
if (!cur_section) |
| 156 |
{ |
| 157 |
warnx ("%s: key=val line not in any section: %s", file_path, line); |
| 158 |
ret = false; |
| 159 |
break; |
| 160 |
} |
| 161 |
|
| 162 |
e = mallocx (sizeof (Entry)); |
| 163 |
e->section = strdupx (cur_section); |
| 164 |
e->key = strndupx (line + matches[1].rm_so, matches[1].rm_eo - matches[1].rm_so); |
| 165 |
e->value = strndupx (line + matches[2].rm_so, matches[2].rm_eo - matches[2].rm_so); |
| 166 |
e->strv_value = NULL; |
| 167 |
e->strv_cache = NULL; |
| 168 |
/* prepend new Entry to cockpit_conf; that way, later values win over earlier ones in a forward search */ |
| 169 |
e->next = cockpit_conf; |
| 170 |
cockpit_conf = e; |
| 171 |
} |
| 172 |
|
| 173 |
else if (regexec (&re_ignore, line, 0, NULL, 0) == 0) |
| 174 |
{ |
| 175 |
/* comment or empty line */ |
| 176 |
} |
| 177 |
else |
| 178 |
{ |
| 179 |
warnx ("%s: invalid line: %s", file_path, line); |
| 180 |
ret = false; |
| 181 |
break; |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
free (line); |
| 186 |
regfree (&re_section); |
| 187 |
regfree (&re_keyval); |
| 188 |
regfree (&re_ignore); |
| 189 |
fclose (f); |
| 190 |
free (cur_section); |
| 191 |
|
| 192 |
if (ret) |
| 193 |
debug ("Loaded configuration from: %s\n", file_path); |
| 194 |
else |
| 195 |
cockpit_conf_cleanup (); |
| 196 |
|
| 197 |
return ret; |
| 198 |
} |
| 199 |
|
| 200 |
static Entry* |
| 201 |
cockpit_conf_lookup (const char *section, |
| 202 |
const char *field) |
| 203 |
{ |
| 204 |
Entry *e; |
| 205 |
|
| 206 |
if (section == NULL || field == NULL) |
| 207 |
return NULL; |
| 208 |
|
| 209 |
if (!cockpit_conf_loaded) |
| 210 |
cockpit_conf_init (); |
| 211 |
|
| 212 |
for (e = cockpit_conf; e; e = e->next) |
| 213 |
{ |
| 214 |
/* that cockpit.conf has traditionally been case insensitive for section and key names */ |
| 215 |
if (strcasecmp (e->section, section) == 0 && strcasecmp (e->key, field) == 0) |
| 216 |
break; |
| 217 |
} |
| 218 |
|
| 219 |
return e; |
| 220 |
} |
| 221 |
|
| 222 |
/* |
| 223 |
* external API |
| 224 |
*/ |
| 225 |
|
| 226 |
void |
| 227 |
cockpit_conf_init (void) |
| 228 |
{ |
| 229 |
if (!cockpit_config_file) |
| 230 |
{ |
| 231 |
debug ("No configuration to load"); |
| 232 |
return; |
| 233 |
} |
| 234 |
|
| 235 |
if (strchr (cockpit_config_file, '/')) |
| 236 |
{ |
| 237 |
load_key_file (cockpit_config_file); |
| 238 |
} |
| 239 |
else |
| 240 |
{ |
| 241 |
const char *const *dirs; |
| 242 |
|
| 243 |
for (dirs = cockpit_conf_get_dirs (); *dirs; ++dirs) |
| 244 |
{ |
| 245 |
char *file = NULL; |
| 246 |
asprintfx (&file, "%s/cockpit/%s", *dirs, cockpit_config_file); |
| 247 |
load_key_file (file); |
| 248 |
free (file); |
| 249 |
} |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
void |
| 254 |
cockpit_conf_cleanup (void) |
| 255 |
{ |
| 256 |
Entry *e, *enext = NULL; |
| 257 |
|
| 258 |
for (e = cockpit_conf; e; e = enext) |
| 259 |
{ |
| 260 |
free (e->section); |
| 261 |
free (e->key); |
| 262 |
free (e->value); |
| 263 |
free (e->strv_value); |
| 264 |
free (e->strv_cache); |
| 265 |
enext = e->next; |
| 266 |
free (e); |
| 267 |
} |
| 268 |
|
| 269 |
cockpit_conf = NULL; |
| 270 |
cockpit_conf_loaded = false; |
| 271 |
} |
| 272 |
|
| 273 |
const char * const * |
| 274 |
cockpit_conf_get_dirs (void) |
| 275 |
{ |
| 276 |
static const char * const * system_config_dirs = NULL; |
| 277 |
static bool initialized = false; |
| 278 |
|
| 279 |
if (!initialized) |
| 280 |
{ |
| 281 |
|
| 282 |
initialized = true; |
| 283 |
const char *env = getenv ("XDG_CONFIG_DIRS"); |
| 284 |
|
| 285 |
/* prepend PACKAGE_SYSCONF_DIR; we read all config files, so XDG_CONFIG_DIRS files override/win */ |
| 286 |
char *dirs_string; |
| 287 |
asprintfx (&dirs_string, "%s%s%s", PACKAGE_SYSCONF_DIR, |
| 288 |
(env && env[0]) ? ":" : "", |
| 289 |
(env && env[0]) ? env : ""); |
| 290 |
system_config_dirs = strsplit (dirs_string, ':'); |
| 291 |
} |
| 292 |
|
| 293 |
return system_config_dirs; |
| 294 |
} |
| 295 |
|
| 296 |
const char * |
| 297 |
cockpit_conf_string (const char *section, |
| 298 |
const char *field) |
| 299 |
{ |
| 300 |
const Entry *entry = cockpit_conf_lookup (section, field); |
| 301 |
return entry ? entry->value : NULL; |
| 302 |
} |
| 303 |
|
| 304 |
bool |
| 305 |
cockpit_conf_bool (const char *section, |
| 306 |
const char *field, |
| 307 |
bool defawlt) |
| 308 |
{ |
| 309 |
const char *value = cockpit_conf_string (section, field); |
| 310 |
if (value) |
| 311 |
return strcasecmp (value, "yes") == 0 || strcasecmp (value, "true") == 0 || strcmp (value, "1") == 0; |
| 312 |
return defawlt; |
| 313 |
} |
| 314 |
|
| 315 |
const char * const * |
| 316 |
cockpit_conf_strv (const char *section, |
| 317 |
const char *field, |
| 318 |
char delimiter) |
| 319 |
{ |
| 320 |
Entry *entry = cockpit_conf_lookup (section, field); |
| 321 |
|
| 322 |
if (!entry || !entry->value) |
| 323 |
return NULL; |
| 324 |
|
| 325 |
if (entry->strv_cache) |
| 326 |
{ |
| 327 |
if (delimiter != entry->strv_delimiter) |
| 328 |
errx (1, "cockpitconf: Looking up strv with different delimiters is not supported"); |
| 329 |
} |
| 330 |
else |
| 331 |
{ |
| 332 |
/* strip off trailing whitespace (leading whitespace is already stripped by regexp) */ |
| 333 |
entry->strv_value = strdupx (entry->value); |
| 334 |
for (char *c = entry->strv_value + strlen (entry->strv_value) - 1; |
| 335 |
c >= entry->strv_value && isspace ((unsigned char) *c); --c) |
| 336 |
*c = '\0'; |
| 337 |
entry->strv_cache = strsplit (entry->strv_value, delimiter); |
| 338 |
entry->strv_delimiter = delimiter; |
| 339 |
} |
| 340 |
|
| 341 |
return entry->strv_cache; |
| 342 |
} |
| 343 |
|
| 344 |
unsigned |
| 345 |
cockpit_conf_uint (const char *section, |
| 346 |
const char *field, |
| 347 |
unsigned default_value, |
| 348 |
unsigned max, |
| 349 |
unsigned min) |
| 350 |
{ |
| 351 |
unsigned val = default_value; |
| 352 |
long long conf_val; |
| 353 |
char *endptr = NULL; |
| 354 |
|
| 355 |
const char* conf = cockpit_conf_string (section, field); |
| 356 |
if (conf) |
| 357 |
{ |
| 358 |
errno = 0; |
| 359 |
conf_val = strtoll (conf, &endptr, 10); |
| 360 |
if ((conf_val == LLONG_MIN || conf_val == LLONG_MAX || conf_val == 0) && |
| 361 |
(errno == ERANGE || errno == EINVAL)) |
| 362 |
val = default_value; |
| 363 |
else if (endptr && endptr[0] != '\0') |
| 364 |
val = default_value; |
| 365 |
else if (conf_val > max) |
| 366 |
val = max; |
| 367 |
else if (conf_val < min) |
| 368 |
val = min; |
| 369 |
else |
| 370 |
val = (unsigned)conf_val; |
| 371 |
|
| 372 |
if (conf_val != val) |
| 373 |
warnx ("Invalid %s %s value '%s', setting to %u", section, field, conf, val); |
| 374 |
} |
| 375 |
|
| 376 |
return val; |
| 377 |
} |