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
|
/*
Copyright (C) 2019 SUSE LLC
Author: Pascal Arlt <parlt@suse.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include "libeconf.h"
#include "defines.h"
#include "helpers.h"
#include "mergefiles.h"
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libgen.h>
// Insert the content of "etc_file.file_entry" into "fe" if there is no
// group specified
size_t insert_nogroup(struct file_entry **fe, econf_file *ef) {
size_t etc_start = 0;
if (ef) {
while (etc_start < ef->length &&
!strcmp(ef->file_entry[etc_start].group, KEY_FILE_NULL_VALUE)) {
(*fe)[etc_start] = cpy_file_entry(ef->file_entry[etc_start]);
etc_start++;
}
}
return etc_start;
}
// Merge contents from existing usr_file groups
// uf: usr_file, ef: etc_file
size_t merge_existing_groups(struct file_entry **fe, econf_file *uf, econf_file *ef,
const size_t etc_start) {
bool new_key;
size_t merge_length = etc_start, tmp = etc_start, added_keys = etc_start;
if (uf && ef) {
for (size_t i = 0; i <= uf->length; i++) {
// Check if the group has changed in the last iteration
if (i == uf->length ||
(i && strcmp(uf->file_entry[i].group, uf->file_entry[i - 1].group))) {
for (size_t j = etc_start; j < ef->length; j++) {
// Check for matching groups
if (!strcmp(uf->file_entry[i - 1].group, ef->file_entry[j].group)) {
new_key = true;
for (size_t k = merge_length; k < i + tmp; k++) {
// If an existing key is found in ef take the value from ef
if (!strcmp((*fe)[k].key, ef->file_entry[j].key)) {
free((*fe)[k].value);
(*fe)[k].value = ef->file_entry[j].value ? strdup(ef->file_entry[j].value) : strdup("");
new_key = false;
break;
}
}
// If a new key is found for an existing group append it to the group
if (new_key)
(*fe)[i + added_keys++] = cpy_file_entry(ef->file_entry[j]);
}
}
merge_length = i + added_keys;
// Temporary value to reduce amount of iterations in inner for loop
tmp = added_keys;
}
if (i != uf->length)
(*fe)[i + added_keys] = cpy_file_entry(uf->file_entry[i]);
}
}
return merge_length;
}
// Add entries from etc_file exclusive groups
size_t add_new_groups(struct file_entry **fe, econf_file *uf, econf_file *ef,
const size_t merge_length) {
size_t added_keys = merge_length;
bool new_key;
if (uf && ef) {
for (size_t i = 0; i < ef->length; i++) {
if (!strcmp(ef->file_entry[i].group, KEY_FILE_NULL_VALUE))
continue;
new_key = true;
for (size_t j = 0; j < uf->length; j++) {
if (!strcmp(uf->file_entry[j].group, ef->file_entry[i].group)) {
new_key = false;
break;
}
}
if (new_key)
(*fe)[added_keys++] = cpy_file_entry(ef->file_entry[i]);
}
*fe = realloc(*fe, added_keys * sizeof(struct file_entry));
}
return added_keys;
}
#if 0
// TODO: Make this function configureable with econf_set_opt()
// TODO: provide the initial array from the calling function (needs to be
// already big enough to hold this additional two entries)
char **get_default_dirs(const char *usr_conf_dir, const char *etc_conf_dir) {
size_t default_dir_number = 3, dir_num = 0;
char **default_dirs = malloc(default_dir_number * sizeof(char *));
if (default_dirs == NULL)
return NULL;
if (etc_conf_dir)
{
// Set config directory in /etc
default_dirs[dir_num++] = strdup(etc_conf_dir); /* XXX ENOMEM check */
}
if (usr_conf_dir)
{
// Set config directory in /usr
default_dirs[dir_num++] = strdup(usr_conf_dir); /* XXX ENOMEM check */
}
// TODO: Use secure_getenv() instead and check if the variable is actually set
// For security reasons, this code should only be enabled if the application
// sets a flag to do so (introduce econf_set_opt() for this.
// Using user supplied configs can lead to a security problem (e.g. su)
// If XDG_CONFIG_DIRS is set check it as well
if(getenv("XDG_CONFIG_DIRS")) {
default_dirs = realloc(default_dirs, ++default_dir_number * sizeof(char *));
default_dirs[dir_num++] = strdup(getenv("XDG_CONFIG_DIRS"));
}
// XDG config home
if (getenv("XDG_CONFIG_HOME")) {
default_dirs[dir_num++] = strdup(getenv("XDG_CONFIG_HOME"));
} else {
sprintf(default_dirs[dir_num++] = malloc(strlen("/home//.config") +
strlen(getenv("USERNAME")) + 1), "/home/%s/.config",
getenv("USERNAME"));
}
default_dirs[dir_num] = NULL;
return default_dirs;
}
#endif
// Check if the given directory exists. If so look for config files
// with the given suffix
static econf_err
check_conf_dir(econf_file ***key_files, size_t *size, const char *path,
const char *config_suffix, const char *delim, const char *comment)
{
struct dirent **de;
int num_dirs = scandir(path, &de, NULL, alphasort);
if(num_dirs > 0) {
for (int i = 0; i < num_dirs; i++) {
size_t lenstr = strlen(de[i]->d_name);
size_t lensuffix = strlen(config_suffix);
if (lensuffix < lenstr &&
strncmp(de[i]->d_name + lenstr - lensuffix, config_suffix, lensuffix) == 0) {
char *file_path = combine_strings(path, de[i]->d_name, '/');
econf_file *key_file;
econf_err error = econf_readFile(&key_file, file_path, delim, comment);
free(file_path);
if(!error && key_file) {
key_file->on_merge_delete = 1;
(*key_files)[(*size) - 1] = key_file;
*key_files = realloc(*key_files, ++(*size) * sizeof(econf_file *));
} else {
for (int k = i; k < num_dirs; k++)
free(de[k]);
free(de);
return error;
}
}
free(de[i]);
}
free(de);
}
return ECONF_SUCCESS;
}
econf_err traverse_conf_dirs(econf_file ***key_files,
const char *config_dirs[],
size_t *size, const char *path,
const char *config_suffix,
const char *delim, const char *comment) {
int i;
if (config_dirs == NULL)
return ECONF_NOFILE;
i = 0;
while (config_dirs[i] != NULL) {
char *fulldir, *cp;
if ((fulldir = malloc (strlen(path) + strlen (config_dirs[i]) + 1)) == NULL)
return ECONF_NOMEM;
cp = stpcpy (fulldir, path);
stpcpy (cp, config_dirs[i++]);
econf_err error = check_conf_dir(key_files, size,
fulldir, config_suffix, delim, comment);
free (fulldir);
if (error)
return error;
}
return ECONF_SUCCESS;
}
econf_err merge_econf_files(econf_file **key_files, econf_file **merged_files) {
if (*key_files == NULL || merged_files == NULL)
return ECONF_ERROR;
*merged_files = *key_files++;
if(*merged_files == NULL)
return ECONF_ERROR;
while(*key_files) {
econf_err error;
econf_file *tmp = *merged_files;
econf_file **double_key_files = key_files+1;
char * current_file = basename((*key_files)->path);
/* key_files are already sorted. If there is a file with the same name with
a higher priority, the current file will be ignored.
e.g. /usr/etc/shells.d/tcsh will not be merged if /etc/shells.d/tcsh exists.
*/
while (*double_key_files) {
char * compare_file = basename((*double_key_files)->path);
if (strcmp(current_file, ".") != 0 && strcmp(current_file, "..") &&
strcmp(current_file, compare_file) == 0) {
break;
}
double_key_files++;
}
if (*double_key_files == NULL) {
error = econf_mergeFiles(merged_files, *merged_files, *key_files);
if (error || *merged_files == NULL)
return error;
(*merged_files)->on_merge_delete = 1;
if(tmp->on_merge_delete) { econf_free(tmp); }
}
if((*key_files)->on_merge_delete) { econf_free(*key_files); }
key_files++;
}
return ECONF_SUCCESS;
}
|