| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
#include "config.h" |
| 22 |
#include <errno.h> |
| 23 |
#include <sys/types.h> |
| 24 |
#include <sys/stat.h> |
| 25 |
#include <unistd.h> |
| 26 |
#include <fcntl.h> |
| 27 |
#include <string.h> |
| 28 |
#include <stdlib.h> |
| 29 |
#include <iconv.h> |
| 30 |
#include <dirent.h> |
| 31 |
#include "trace.h" |
| 32 |
#include "ini_defines.h" |
| 33 |
#include "ini_configobj.h" |
| 34 |
#include "ini_config_priv.h" |
| 35 |
#include "path_utils.h" |
| 36 |
|
| 37 |
#define ICONV_BUFFER 5000 |
| 38 |
|
| 39 |
#define BOM4_SIZE 4 |
| 40 |
#define BOM3_SIZE 3 |
| 41 |
#define BOM2_SIZE 2 |
| 42 |
|
| 43 |
static const char *encodings[] = { "UTF-32BE", |
| 44 |
"UTF-32LE", |
| 45 |
"UTF-16BE", |
| 46 |
"UTF-16LE", |
| 47 |
"UTF-8", |
| 48 |
"UTF-8" }; |
| 49 |
|
| 50 |
|
| 51 |
void ini_config_file_close(struct ini_cfgfile *file_ctx) |
| 52 |
{ |
| 53 |
TRACE_FLOW_ENTRY(); |
| 54 |
|
| 55 |
if(file_ctx) { |
| 56 |
if(file_ctx->file) { |
| 57 |
fclose(file_ctx->file); |
| 58 |
file_ctx->file = NULL; |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
TRACE_FLOW_EXIT(); |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
void ini_config_file_destroy(struct ini_cfgfile *file_ctx) |
| 67 |
{ |
| 68 |
TRACE_FLOW_ENTRY(); |
| 69 |
|
| 70 |
if(file_ctx) { |
| 71 |
free(file_ctx->filename); |
| 72 |
simplebuffer_free(file_ctx->file_data); |
| 73 |
if(file_ctx->file) fclose(file_ctx->file); |
| 74 |
free(file_ctx); |
| 75 |
} |
| 76 |
|
| 77 |
TRACE_FLOW_EXIT(); |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
static size_t how_much_to_read(size_t left, size_t increment) |
| 82 |
{ |
| 83 |
if(left > increment) return increment; |
| 84 |
else return left; |
| 85 |
} |
| 86 |
|
| 87 |
static enum index_utf_t check_bom(enum index_utf_t ind, |
| 88 |
unsigned char *buffer, |
| 89 |
size_t len, |
| 90 |
size_t *bom_shift) |
| 91 |
{ |
| 92 |
TRACE_FLOW_ENTRY(); |
| 93 |
|
| 94 |
if (len >= BOM4_SIZE) { |
| 95 |
if ((buffer[0] == 0x00) && |
| 96 |
(buffer[1] == 0x00) && |
| 97 |
(buffer[2] == 0xFE) && |
| 98 |
(buffer[3] == 0xFF)) { |
| 99 |
TRACE_FLOW_RETURN(INDEX_UTF32BE); |
| 100 |
*bom_shift = BOM4_SIZE; |
| 101 |
return INDEX_UTF32BE; |
| 102 |
} |
| 103 |
else if ((buffer[0] == 0xFF) && |
| 104 |
(buffer[1] == 0xFE) && |
| 105 |
(buffer[2] == 0x00) && |
| 106 |
(buffer[3] == 0x00)) { |
| 107 |
TRACE_FLOW_RETURN(INDEX_UTF32LE); |
| 108 |
*bom_shift = BOM4_SIZE; |
| 109 |
return INDEX_UTF32LE; |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
if (len >= BOM3_SIZE) { |
| 114 |
if ((buffer[0] == 0xEF) && |
| 115 |
(buffer[1] == 0xBB) && |
| 116 |
(buffer[2] == 0xBF)) { |
| 117 |
TRACE_FLOW_RETURN(INDEX_UTF8); |
| 118 |
*bom_shift = BOM3_SIZE; |
| 119 |
return INDEX_UTF8; |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
if (len >= BOM2_SIZE) { |
| 124 |
if ((buffer[0] == 0xFE) && |
| 125 |
(buffer[1] == 0xFF)) { |
| 126 |
TRACE_FLOW_RETURN(INDEX_UTF16BE); |
| 127 |
*bom_shift = BOM2_SIZE; |
| 128 |
return INDEX_UTF16BE; |
| 129 |
} |
| 130 |
else if ((buffer[0] == 0xFF) && |
| 131 |
(buffer[1] == 0xFE)) { |
| 132 |
TRACE_FLOW_RETURN(INDEX_UTF16LE); |
| 133 |
*bom_shift = BOM2_SIZE; |
| 134 |
return INDEX_UTF16LE; |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
TRACE_FLOW_RETURN(ind); |
| 139 |
return ind; |
| 140 |
} |
| 141 |
|
| 142 |
static int read_chunk(FILE *file, size_t left, size_t increment, |
| 143 |
char *position, size_t *read_num) |
| 144 |
{ |
| 145 |
int error = EOK; |
| 146 |
size_t to_read = 0; |
| 147 |
size_t read_cnt = 0; |
| 148 |
|
| 149 |
TRACE_FLOW_ENTRY(); |
| 150 |
|
| 151 |
to_read = how_much_to_read(left, increment); |
| 152 |
|
| 153 |
TRACE_INFO_NUMBER("About to read", to_read); |
| 154 |
|
| 155 |
read_cnt = fread(position, to_read, 1, file); |
| 156 |
|
| 157 |
TRACE_INFO_NUMBER("Read", read_cnt * to_read); |
| 158 |
|
| 159 |
if (read_cnt == 0) { |
| 160 |
error = ferror(file); |
| 161 |
if (error) { |
| 162 |
TRACE_ERROR_NUMBER("Failed to read data from file", error); |
| 163 |
return error; |
| 164 |
} |
| 165 |
error = feof(file); |
| 166 |
if(error) { |
| 167 |
TRACE_FLOW_EXIT(); |
| 168 |
return EOK; |
| 169 |
} |
| 170 |
TRACE_ERROR_NUMBER("Failed to read data from file", EIO); |
| 171 |
return EIO; |
| 172 |
} |
| 173 |
|
| 174 |
*read_num = to_read; |
| 175 |
|
| 176 |
TRACE_FLOW_EXIT(); |
| 177 |
return error; |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
static int initialize_conv(unsigned char *read_buf, |
| 194 |
size_t read_cnt, |
| 195 |
int *initialized, |
| 196 |
size_t *bom_shift, |
| 197 |
enum index_utf_t *in_ind, |
| 198 |
iconv_t *conv) |
| 199 |
{ |
| 200 |
int error = EOK; |
| 201 |
enum index_utf_t ind = INDEX_UTF8NOBOM; |
| 202 |
|
| 203 |
TRACE_FLOW_ENTRY(); |
| 204 |
|
| 205 |
if (*initialized == 0) { |
| 206 |
|
| 207 |
TRACE_INFO_STRING("Reading first time.","Checking BOM"); |
| 208 |
|
| 209 |
ind = check_bom(ind, |
| 210 |
(unsigned char *)read_buf, |
| 211 |
read_cnt, |
| 212 |
bom_shift); |
| 213 |
|
| 214 |
TRACE_INFO_STRING("Converting to", encodings[INDEX_UTF8NOBOM]); |
| 215 |
TRACE_INFO_STRING("Converting from", encodings[ind]); |
| 216 |
|
| 217 |
errno = 0; |
| 218 |
*conv = iconv_open(encodings[INDEX_UTF8], encodings[ind]); |
| 219 |
if (*conv == (iconv_t) -1) { |
| 220 |
error = errno; |
| 221 |
TRACE_ERROR_NUMBER("Failed to create converter", error); |
| 222 |
return error; |
| 223 |
} |
| 224 |
|
| 225 |
*initialized = 1; |
| 226 |
*in_ind = ind; |
| 227 |
} |
| 228 |
else *bom_shift = 0; |
| 229 |
|
| 230 |
TRACE_FLOW_EXIT(); |
| 231 |
return error; |
| 232 |
} |
| 233 |
|
| 234 |
|
| 235 |
static int common_file_convert(FILE *file, |
| 236 |
struct ini_cfgfile *file_ctx, |
| 237 |
uint32_t size) |
| 238 |
{ |
| 239 |
int error = EOK; |
| 240 |
size_t read_cnt = 0; |
| 241 |
size_t total_read = 0; |
| 242 |
size_t in_buffer = 0; |
| 243 |
iconv_t conv = (iconv_t)-1; |
| 244 |
size_t conv_res = 0; |
| 245 |
char read_buf[ICONV_BUFFER+1]; |
| 246 |
char result_buf[ICONV_BUFFER]; |
| 247 |
char *src, *dest; |
| 248 |
size_t to_convert = 0; |
| 249 |
size_t room_left = 0; |
| 250 |
size_t bom_shift = 0; |
| 251 |
int initialized = 0; |
| 252 |
enum index_utf_t ind = INDEX_UTF8NOBOM; |
| 253 |
|
| 254 |
TRACE_FLOW_ENTRY(); |
| 255 |
|
| 256 |
do { |
| 257 |
|
| 258 |
error = read_chunk(file, |
| 259 |
size - total_read, |
| 260 |
ICONV_BUFFER - in_buffer, |
| 261 |
read_buf + in_buffer, |
| 262 |
&read_cnt); |
| 263 |
|
| 264 |
if (error) { |
| 265 |
if (conv != (iconv_t) -1) iconv_close(conv); |
| 266 |
TRACE_ERROR_NUMBER("Failed to read chunk", error); |
| 267 |
return error; |
| 268 |
} |
| 269 |
|
| 270 |
|
| 271 |
src = read_buf; |
| 272 |
to_convert = read_cnt + in_buffer; |
| 273 |
in_buffer = 0; |
| 274 |
|
| 275 |
|
| 276 |
error = initialize_conv((unsigned char *)read_buf, |
| 277 |
read_cnt, |
| 278 |
&initialized, |
| 279 |
&bom_shift, |
| 280 |
&ind, |
| 281 |
&conv); |
| 282 |
if (error) { |
| 283 |
TRACE_ERROR_NUMBER("Failed to initialize", |
| 284 |
error); |
| 285 |
return error; |
| 286 |
} |
| 287 |
|
| 288 |
src += bom_shift; |
| 289 |
to_convert -= bom_shift; |
| 290 |
total_read += read_cnt; |
| 291 |
file_ctx->bom = ind; |
| 292 |
TRACE_INFO_NUMBER("Total read", total_read); |
| 293 |
|
| 294 |
do { |
| 295 |
|
| 296 |
dest = result_buf; |
| 297 |
room_left = ICONV_BUFFER; |
| 298 |
|
| 299 |
TRACE_INFO_NUMBER("To convert", to_convert); |
| 300 |
TRACE_INFO_NUMBER("Room left", room_left); |
| 301 |
TRACE_INFO_NUMBER("Total read", total_read); |
| 302 |
|
| 303 |
errno = 0; |
| 304 |
conv_res = iconv(conv, &src, &to_convert, &dest, &room_left); |
| 305 |
if (conv_res == (size_t) -1) { |
| 306 |
error = errno; |
| 307 |
switch(error) { |
| 308 |
case EILSEQ: |
| 309 |
TRACE_ERROR_NUMBER("Invalid multibyte encoding", error); |
| 310 |
iconv_close(conv); |
| 311 |
return error; |
| 312 |
case EINVAL: |
| 313 |
|
| 314 |
TRACE_INFO_NUMBER("Incomplete sequence len", |
| 315 |
src - read_buf); |
| 316 |
TRACE_INFO_NUMBER("File size.", size); |
| 317 |
if (total_read == size) { |
| 318 |
|
| 319 |
TRACE_ERROR_NUMBER("Incomplete sequence", error); |
| 320 |
iconv_close(conv); |
| 321 |
return error; |
| 322 |
} |
| 323 |
memmove(read_buf, src, to_convert); |
| 324 |
in_buffer = to_convert; |
| 325 |
break; |
| 326 |
|
| 327 |
case E2BIG: |
| 328 |
TRACE_INFO_STRING("No room in the output buffer.", ""); |
| 329 |
error = simplebuffer_add_raw(file_ctx->file_data, |
| 330 |
result_buf, |
| 331 |
ICONV_BUFFER - room_left, |
| 332 |
ICONV_BUFFER); |
| 333 |
if (error) { |
| 334 |
TRACE_ERROR_NUMBER("Failed to store converted bytes", |
| 335 |
error); |
| 336 |
iconv_close(conv); |
| 337 |
return error; |
| 338 |
} |
| 339 |
continue; |
| 340 |
default: |
| 341 |
TRACE_ERROR_NUMBER("Unexpected internal error", |
| 342 |
error); |
| 343 |
iconv_close(conv); |
| 344 |
return ENOTSUP; |
| 345 |
} |
| 346 |
} |
| 347 |
|
| 348 |
error = simplebuffer_add_raw(file_ctx->file_data, |
| 349 |
result_buf, |
| 350 |
ICONV_BUFFER - room_left, |
| 351 |
ICONV_BUFFER); |
| 352 |
if (error) { |
| 353 |
TRACE_ERROR_NUMBER("Failed to store converted bytes", |
| 354 |
error); |
| 355 |
iconv_close(conv); |
| 356 |
return error; |
| 357 |
} |
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
break; |
| 363 |
} |
| 364 |
while (1); |
| 365 |
} |
| 366 |
while (total_read < size); |
| 367 |
|
| 368 |
iconv_close(conv); |
| 369 |
|
| 370 |
|
| 371 |
TRACE_INFO_STRING("File data", |
| 372 |
(char *)simplebuffer_get_vbuf(file_ctx->file_data)); |
| 373 |
TRACE_INFO_NUMBER("File len", |
| 374 |
simplebuffer_get_len(file_ctx->file_data)); |
| 375 |
TRACE_INFO_NUMBER("Size", size); |
| 376 |
errno = 0; |
| 377 |
file_ctx->file = fmemopen(simplebuffer_get_vbuf(file_ctx->file_data), |
| 378 |
simplebuffer_get_len(file_ctx->file_data), |
| 379 |
"r"); |
| 380 |
if (!(file_ctx->file)) { |
| 381 |
error = errno; |
| 382 |
TRACE_ERROR_NUMBER("Failed to open file", error); |
| 383 |
return error; |
| 384 |
} |
| 385 |
|
| 386 |
TRACE_FLOW_EXIT(); |
| 387 |
return EOK; |
| 388 |
} |
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
static int common_file_init(struct ini_cfgfile *file_ctx, |
| 393 |
void *data_buf, |
| 394 |
uint32_t data_len) |
| 395 |
{ |
| 396 |
int error = EOK; |
| 397 |
FILE *file = NULL; |
| 398 |
int stat_ret = 0; |
| 399 |
uint32_t size = 0; |
| 400 |
void *internal_data = NULL; |
| 401 |
uint32_t internal_len = 0; |
| 402 |
unsigned char alt_buffer[2] = {0, 0}; |
| 403 |
uint32_t alt_buffer_len = 1; |
| 404 |
|
| 405 |
TRACE_FLOW_ENTRY(); |
| 406 |
|
| 407 |
if (data_buf) { |
| 408 |
|
| 409 |
if(data_len) { |
| 410 |
internal_data = data_buf; |
| 411 |
internal_len = data_len; |
| 412 |
} |
| 413 |
else { |
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
internal_data = alt_buffer; |
| 418 |
internal_len = alt_buffer_len; |
| 419 |
} |
| 420 |
|
| 421 |
TRACE_INFO_NUMBER("Inside file_init len", internal_len); |
| 422 |
TRACE_INFO_STRING("Inside file_init data:", (char *)internal_data); |
| 423 |
|
| 424 |
file = fmemopen(internal_data, internal_len, "r"); |
| 425 |
if (!file) { |
| 426 |
error = errno; |
| 427 |
TRACE_ERROR_NUMBER("Failed to memmap file", error); |
| 428 |
return error; |
| 429 |
} |
| 430 |
size = internal_len; |
| 431 |
} |
| 432 |
else { |
| 433 |
|
| 434 |
TRACE_INFO_STRING("File", file_ctx->filename); |
| 435 |
|
| 436 |
|
| 437 |
errno = 0; |
| 438 |
file = fopen(file_ctx->filename, "r"); |
| 439 |
if (!file) { |
| 440 |
error = errno; |
| 441 |
TRACE_ERROR_NUMBER("Failed to open file", error); |
| 442 |
return error; |
| 443 |
} |
| 444 |
|
| 445 |
|
| 446 |
errno = 0; |
| 447 |
stat_ret = fstat(fileno(file), &(file_ctx->file_stats)); |
| 448 |
if (stat_ret == -1) { |
| 449 |
error = errno; |
| 450 |
fclose(file); |
| 451 |
TRACE_ERROR_NUMBER("Failed to get file stats", error); |
| 452 |
return error; |
| 453 |
} |
| 454 |
size = file_ctx->file_stats.st_size; |
| 455 |
} |
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
if (size) { |
| 462 |
error = common_file_convert(file, file_ctx, size); |
| 463 |
if (error) { |
| 464 |
TRACE_ERROR_NUMBER("Failed to convert file", |
| 465 |
error); |
| 466 |
fclose(file); |
| 467 |
return error; |
| 468 |
} |
| 469 |
} |
| 470 |
else { |
| 471 |
|
| 472 |
TRACE_INFO_STRING("File is 0 length",""); |
| 473 |
errno = 0; |
| 474 |
|
| 475 |
file_ctx->file = fdopen(fileno(file), "r"); |
| 476 |
if (!(file_ctx->file)) { |
| 477 |
error = errno; |
| 478 |
fclose(file); |
| 479 |
TRACE_ERROR_NUMBER("Failed to fdopen file", error); |
| 480 |
return error; |
| 481 |
} |
| 482 |
} |
| 483 |
|
| 484 |
fclose(file); |
| 485 |
|
| 486 |
|
| 487 |
if (file_ctx->metadata_flags & INI_META_STATS) { |
| 488 |
file_ctx->stats_read = 1; |
| 489 |
} |
| 490 |
else { |
| 491 |
memset(&(file_ctx->file_stats), 0, sizeof(struct stat)); |
| 492 |
file_ctx->stats_read = 0; |
| 493 |
} |
| 494 |
|
| 495 |
TRACE_FLOW_EXIT(); |
| 496 |
return EOK; |
| 497 |
} |
| 498 |
|
| 499 |
|
| 500 |
int ini_config_file_open(const char *filename, |
| 501 |
uint32_t metadata_flags, |
| 502 |
struct ini_cfgfile **file_ctx) |
| 503 |
{ |
| 504 |
int error = EOK; |
| 505 |
struct ini_cfgfile *new_ctx = NULL; |
| 506 |
|
| 507 |
TRACE_FLOW_ENTRY(); |
| 508 |
|
| 509 |
if ((!filename) || (!file_ctx)) { |
| 510 |
TRACE_ERROR_NUMBER("Invalid parameter.", EINVAL); |
| 511 |
return EINVAL; |
| 512 |
} |
| 513 |
|
| 514 |
|
| 515 |
new_ctx = malloc(sizeof(struct ini_cfgfile)); |
| 516 |
if (!new_ctx) { |
| 517 |
TRACE_ERROR_NUMBER("Failed to allocate file ctx.", ENOMEM); |
| 518 |
return ENOMEM; |
| 519 |
} |
| 520 |
|
| 521 |
new_ctx->filename = NULL; |
| 522 |
new_ctx->file = NULL; |
| 523 |
new_ctx->file_data = NULL; |
| 524 |
new_ctx->bom = INDEX_UTF8NOBOM; |
| 525 |
|
| 526 |
error = simplebuffer_alloc(&(new_ctx->file_data)); |
| 527 |
if (error) { |
| 528 |
TRACE_ERROR_NUMBER("Failed to allocate buffer ctx.", error); |
| 529 |
ini_config_file_destroy(new_ctx); |
| 530 |
return error; |
| 531 |
|
| 532 |
} |
| 533 |
|
| 534 |
|
| 535 |
new_ctx->metadata_flags = metadata_flags; |
| 536 |
|
| 537 |
|
| 538 |
new_ctx->filename = malloc(PATH_MAX + 1); |
| 539 |
if (!(new_ctx->filename)) { |
| 540 |
ini_config_file_destroy(new_ctx); |
| 541 |
TRACE_ERROR_NUMBER("Failed to allocate memory for file path.", ENOMEM); |
| 542 |
return ENOMEM; |
| 543 |
} |
| 544 |
|
| 545 |
|
| 546 |
error = make_normalized_absolute_path(new_ctx->filename, |
| 547 |
PATH_MAX, |
| 548 |
filename); |
| 549 |
if(error) { |
| 550 |
TRACE_ERROR_NUMBER("Failed to resolve path", error); |
| 551 |
ini_config_file_destroy(new_ctx); |
| 552 |
return error; |
| 553 |
} |
| 554 |
|
| 555 |
|
| 556 |
error = common_file_init(new_ctx, NULL, 0); |
| 557 |
if(error) { |
| 558 |
TRACE_ERROR_NUMBER("Failed to do common init", error); |
| 559 |
ini_config_file_destroy(new_ctx); |
| 560 |
return error; |
| 561 |
} |
| 562 |
|
| 563 |
*file_ctx = new_ctx; |
| 564 |
TRACE_FLOW_EXIT(); |
| 565 |
return error; |
| 566 |
} |
| 567 |
|
| 568 |
|
| 569 |
int ini_config_file_from_mem(void *data_buf, |
| 570 |
uint32_t data_len, |
| 571 |
struct ini_cfgfile **file_ctx) |
| 572 |
{ |
| 573 |
int error = EOK; |
| 574 |
struct ini_cfgfile *new_ctx = NULL; |
| 575 |
|
| 576 |
TRACE_FLOW_ENTRY(); |
| 577 |
|
| 578 |
if ((!data_buf) || (!file_ctx)) { |
| 579 |
TRACE_ERROR_NUMBER("Invalid parameter.", EINVAL); |
| 580 |
return EINVAL; |
| 581 |
} |
| 582 |
|
| 583 |
|
| 584 |
new_ctx = malloc(sizeof(struct ini_cfgfile)); |
| 585 |
if (!new_ctx) { |
| 586 |
TRACE_ERROR_NUMBER("Failed to allocate file ctx.", ENOMEM); |
| 587 |
return ENOMEM; |
| 588 |
} |
| 589 |
|
| 590 |
new_ctx->filename = NULL; |
| 591 |
new_ctx->file = NULL; |
| 592 |
new_ctx->file_data = NULL; |
| 593 |
new_ctx->metadata_flags = 0; |
| 594 |
new_ctx->bom = INDEX_UTF8NOBOM; |
| 595 |
|
| 596 |
error = simplebuffer_alloc(&(new_ctx->file_data)); |
| 597 |
if (error) { |
| 598 |
TRACE_ERROR_NUMBER("Failed to allocate buffer ctx.", error); |
| 599 |
ini_config_file_destroy(new_ctx); |
| 600 |
return error; |
| 601 |
} |
| 602 |
|
| 603 |
|
| 604 |
new_ctx->filename = strdup(""); |
| 605 |
if (!(new_ctx->filename)) { |
| 606 |
ini_config_file_destroy(new_ctx); |
| 607 |
TRACE_ERROR_NUMBER("Failed to put empty string into filename.", ENOMEM); |
| 608 |
return ENOMEM; |
| 609 |
} |
| 610 |
|
| 611 |
|
| 612 |
error = common_file_init(new_ctx, data_buf, data_len); |
| 613 |
if(error) { |
| 614 |
TRACE_ERROR_NUMBER("Failed to do common init", error); |
| 615 |
ini_config_file_destroy(new_ctx); |
| 616 |
return error; |
| 617 |
} |
| 618 |
|
| 619 |
*file_ctx = new_ctx; |
| 620 |
TRACE_FLOW_EXIT(); |
| 621 |
return error; |
| 622 |
} |
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
int ini_config_file_reopen(struct ini_cfgfile *file_ctx_in, |
| 628 |
struct ini_cfgfile **file_ctx_out) |
| 629 |
{ |
| 630 |
int error = EOK; |
| 631 |
struct ini_cfgfile *new_ctx = NULL; |
| 632 |
|
| 633 |
TRACE_FLOW_ENTRY(); |
| 634 |
|
| 635 |
if ((!file_ctx_in) || (!file_ctx_out)) { |
| 636 |
TRACE_ERROR_NUMBER("Invalid parameter.", EINVAL); |
| 637 |
return EINVAL; |
| 638 |
} |
| 639 |
|
| 640 |
|
| 641 |
new_ctx = malloc(sizeof(struct ini_cfgfile)); |
| 642 |
if (!new_ctx) { |
| 643 |
TRACE_ERROR_NUMBER("Failed to allocate file ctx.", ENOMEM); |
| 644 |
return ENOMEM; |
| 645 |
} |
| 646 |
|
| 647 |
new_ctx->file = NULL; |
| 648 |
new_ctx->file_data = NULL; |
| 649 |
new_ctx->filename = NULL; |
| 650 |
|
| 651 |
error = simplebuffer_alloc(&(new_ctx->file_data)); |
| 652 |
if (error) { |
| 653 |
TRACE_ERROR_NUMBER("Failed to allocate buffer ctx.", error); |
| 654 |
ini_config_file_destroy(new_ctx); |
| 655 |
return error; |
| 656 |
|
| 657 |
} |
| 658 |
|
| 659 |
|
| 660 |
new_ctx->metadata_flags = file_ctx_in->metadata_flags; |
| 661 |
|
| 662 |
|
| 663 |
errno = 0; |
| 664 |
new_ctx->filename = strndup(file_ctx_in->filename, PATH_MAX); |
| 665 |
if (!(new_ctx->filename)) { |
| 666 |
error = errno; |
| 667 |
ini_config_file_destroy(new_ctx); |
| 668 |
TRACE_ERROR_NUMBER("Failed to allocate memory for file path.", error); |
| 669 |
return error; |
| 670 |
} |
| 671 |
|
| 672 |
new_ctx->bom = file_ctx_in->bom; |
| 673 |
|
| 674 |
|
| 675 |
error = common_file_init(new_ctx, NULL, 0); |
| 676 |
if(error) { |
| 677 |
TRACE_ERROR_NUMBER("Failed to do common init", error); |
| 678 |
ini_config_file_destroy(new_ctx); |
| 679 |
return error; |
| 680 |
} |
| 681 |
|
| 682 |
*file_ctx_out = new_ctx; |
| 683 |
TRACE_FLOW_EXIT(); |
| 684 |
return error; |
| 685 |
} |
| 686 |
|
| 687 |
|
| 688 |
static int create_file_name(const char *dir, |
| 689 |
const char *tpl, |
| 690 |
unsigned count, |
| 691 |
char **filename) |
| 692 |
{ |
| 693 |
char *resolved = NULL; |
| 694 |
char *full_name = NULL; |
| 695 |
int ret = 0; |
| 696 |
const char *dir_to_use; |
| 697 |
char dirbuf[PATH_MAX * 2 + 1]; |
| 698 |
|
| 699 |
TRACE_FLOW_ENTRY(); |
| 700 |
|
| 701 |
|
| 702 |
ret = asprintf(&resolved, tpl, count); |
| 703 |
if (ret == -1) { |
| 704 |
TRACE_ERROR_NUMBER("First asprintf falied.", ENOMEM); |
| 705 |
return ENOMEM; |
| 706 |
} |
| 707 |
|
| 708 |
|
| 709 |
if (dir) dir_to_use = dir; |
| 710 |
else { |
| 711 |
memset(dirbuf, 0 , PATH_MAX * 2 + 1); |
| 712 |
dir_to_use = getcwd(dirbuf, PATH_MAX * 2); |
| 713 |
} |
| 714 |
|
| 715 |
ret = asprintf(&full_name, "%s/%s", dir_to_use, resolved); |
| 716 |
free(resolved); |
| 717 |
if (ret == -1) { |
| 718 |
TRACE_ERROR_NUMBER("Second asprintf falied.", ENOMEM); |
| 719 |
return ENOMEM; |
| 720 |
} |
| 721 |
|
| 722 |
*filename = full_name; |
| 723 |
|
| 724 |
TRACE_FLOW_EXIT(); |
| 725 |
return EOK; |
| 726 |
} |
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
static int determine_permissions(struct ini_cfgfile *file_ctx, |
| 731 |
struct access_check *overwrite, |
| 732 |
uid_t *uid_ptr, |
| 733 |
gid_t *gid_ptr, |
| 734 |
mode_t *mode_ptr) |
| 735 |
{ |
| 736 |
int error = EOK; |
| 737 |
uid_t uid = 0; |
| 738 |
gid_t gid = 0; |
| 739 |
mode_t mode = 0; |
| 740 |
struct stat stats; |
| 741 |
int ret = 0; |
| 742 |
|
| 743 |
TRACE_FLOW_ENTRY(); |
| 744 |
|
| 745 |
|
| 746 |
if (file_ctx->stats_read) { |
| 747 |
uid = file_ctx->file_stats.st_uid; |
| 748 |
gid = file_ctx->file_stats.st_gid; |
| 749 |
mode = file_ctx->file_stats.st_mode; |
| 750 |
} |
| 751 |
else if (*(file_ctx->filename) != '\0') { |
| 752 |
|
| 753 |
memset(&stats, 0, sizeof(struct stat)); |
| 754 |
ret = stat(file_ctx->filename, &stats); |
| 755 |
if (ret == -1) { |
| 756 |
error = errno; |
| 757 |
TRACE_ERROR_NUMBER("Stat falied.", error); |
| 758 |
return error; |
| 759 |
} |
| 760 |
uid = stats.st_uid; |
| 761 |
gid = stats.st_gid; |
| 762 |
mode = stats.st_mode; |
| 763 |
} |
| 764 |
else { |
| 765 |
|
| 766 |
uid = geteuid(); |
| 767 |
gid = getegid(); |
| 768 |
|
| 769 |
mode = S_IRUSR | S_IWUSR; |
| 770 |
} |
| 771 |
|
| 772 |
|
| 773 |
if (overwrite) { |
| 774 |
|
| 775 |
overwrite->flags &= INI_ACCESS_CHECK_MODE | |
| 776 |
INI_ACCESS_CHECK_GID | |
| 777 |
INI_ACCESS_CHECK_UID; |
| 778 |
|
| 779 |
if (overwrite->flags == 0) { |
| 780 |
TRACE_ERROR_NUMBER("Invalid parameter.", EINVAL); |
| 781 |
return EINVAL; |
| 782 |
} |
| 783 |
|
| 784 |
|
| 785 |
if (overwrite->flags & INI_ACCESS_CHECK_MODE) { |
| 786 |
mode = overwrite->mode; |
| 787 |
} |
| 788 |
|
| 789 |
|
| 790 |
if (overwrite->flags & INI_ACCESS_CHECK_UID) { |
| 791 |
uid = overwrite->uid; |
| 792 |
} |
| 793 |
|
| 794 |
|
| 795 |
if (overwrite->flags & INI_ACCESS_CHECK_GID) { |
| 796 |
gid = overwrite->gid; |
| 797 |
} |
| 798 |
} |
| 799 |
|
| 800 |
*uid_ptr = uid; |
| 801 |
*gid_ptr = gid; |
| 802 |
*mode_ptr = mode; |
| 803 |
|
| 804 |
TRACE_FLOW_EXIT(); |
| 805 |
return EOK; |
| 806 |
} |
| 807 |
|
| 808 |
|
| 809 |
static int open_new_file(const char *filename, |
| 810 |
uid_t uid, |
| 811 |
gid_t gid, |
| 812 |
mode_t mode, |
| 813 |
int check, |
| 814 |
int *fd_ptr) |
| 815 |
{ |
| 816 |
int error = EOK; |
| 817 |
int ret = 0; |
| 818 |
int fd; |
| 819 |
|
| 820 |
TRACE_FLOW_ENTRY(); |
| 821 |
|
| 822 |
if (check) { |
| 823 |
errno = 0; |
| 824 |
fd = open(filename, O_RDONLY); |
| 825 |
if (fd != -1) { |
| 826 |
close(fd); |
| 827 |
TRACE_ERROR_NUMBER("File already exists.", error); |
| 828 |
return EEXIST; |
| 829 |
} |
| 830 |
else { |
| 831 |
error = errno; |
| 832 |
if (error == EACCES) { |
| 833 |
TRACE_ERROR_NUMBER("Failed to open file.", error); |
| 834 |
return error; |
| 835 |
} |
| 836 |
} |
| 837 |
} |
| 838 |
|
| 839 |
|
| 840 |
errno = 0; |
| 841 |
fd = creat(filename, mode); |
| 842 |
if (fd == -1) { |
| 843 |
error = errno; |
| 844 |
TRACE_ERROR_NUMBER("Failed to create file.", error); |
| 845 |
return error; |
| 846 |
} |
| 847 |
|
| 848 |
errno = 0; |
| 849 |
ret = fchmod(fd, mode); |
| 850 |
if (ret == -1) { |
| 851 |
error = errno; |
| 852 |
close(fd); |
| 853 |
TRACE_ERROR_NUMBER("Failed to chmod file.", error); |
| 854 |
return error; |
| 855 |
} |
| 856 |
|
| 857 |
errno = 0; |
| 858 |
ret = fchown(fd, uid, gid); |
| 859 |
if (ret == -1) { |
| 860 |
error = errno; |
| 861 |
close(fd); |
| 862 |
TRACE_ERROR_NUMBER("Failed to chown file.", error); |
| 863 |
return error; |
| 864 |
} |
| 865 |
|
| 866 |
*fd_ptr = fd; |
| 867 |
|
| 868 |
TRACE_FLOW_EXIT(); |
| 869 |
return EOK; |
| 870 |
|
| 871 |
} |
| 872 |
|
| 873 |
|
| 874 |
static int do_encoding(struct ini_cfgfile *file_ctx, |
| 875 |
struct simplebuffer *sb) |
| 876 |
{ |
| 877 |
int error = EOK; |
| 878 |
iconv_t encoder; |
| 879 |
char *src, *dest; |
| 880 |
size_t to_convert = 0; |
| 881 |
size_t room_left = 0; |
| 882 |
char result_buf[ICONV_BUFFER]; |
| 883 |
size_t conv_res = 0; |
| 884 |
|
| 885 |
TRACE_FLOW_ENTRY(); |
| 886 |
|
| 887 |
encoder = iconv_open(encodings[file_ctx->bom], encodings[INDEX_UTF8NOBOM]); |
| 888 |
if (encoder == (iconv_t) -1) { |
| 889 |
error = errno; |
| 890 |
TRACE_ERROR_NUMBER("Failed to create converter", error); |
| 891 |
return error; |
| 892 |
} |
| 893 |
|
| 894 |
src = (char *)simplebuffer_get_vbuf(file_ctx->file_data); |
| 895 |
to_convert = (size_t)simplebuffer_get_len(file_ctx->file_data); |
| 896 |
|
| 897 |
do { |
| 898 |
|
| 899 |
|
| 900 |
|
| 901 |
dest = result_buf; |
| 902 |
room_left = ICONV_BUFFER; |
| 903 |
|
| 904 |
errno = 0; |
| 905 |
conv_res = iconv(encoder, &src, &to_convert, &dest, &room_left); |
| 906 |
if (conv_res == (size_t) -1) { |
| 907 |
error = errno; |
| 908 |
switch(error) { |
| 909 |
case EILSEQ: |
| 910 |
TRACE_ERROR_NUMBER("Invalid multibyte encoding", error); |
| 911 |
iconv_close(encoder); |
| 912 |
return error; |
| 913 |
case EINVAL: |
| 914 |
TRACE_ERROR_NUMBER("Incomplete sequence", error); |
| 915 |
iconv_close(encoder); |
| 916 |
return error; |
| 917 |
case E2BIG: |
| 918 |
TRACE_INFO_STRING("No room in the output buffer.", ""); |
| 919 |
error = simplebuffer_add_raw(sb, |
| 920 |
result_buf, |
| 921 |
ICONV_BUFFER - room_left, |
| 922 |
ICONV_BUFFER); |
| 923 |
if (error) { |
| 924 |
TRACE_ERROR_NUMBER("Failed to store converted bytes", |
| 925 |
error); |
| 926 |
iconv_close(encoder); |
| 927 |
return error; |
| 928 |
} |
| 929 |
continue; |
| 930 |
default: |
| 931 |
TRACE_ERROR_NUMBER("Unexpected internal error", |
| 932 |
error); |
| 933 |
iconv_close(encoder); |
| 934 |
return ENOTSUP; |
| 935 |
} |
| 936 |
} |
| 937 |
|
| 938 |
|
| 939 |
error = simplebuffer_add_raw(sb, |
| 940 |
result_buf, |
| 941 |
ICONV_BUFFER - room_left, |
| 942 |
ICONV_BUFFER); |
| 943 |
if (error) { |
| 944 |
TRACE_ERROR_NUMBER("Failed to store converted bytes", |
| 945 |
error); |
| 946 |
iconv_close(encoder); |
| 947 |
return error; |
| 948 |
} |
| 949 |
|
| 950 |
|
| 951 |
|
| 952 |
|
| 953 |
break; |
| 954 |
|
| 955 |
} |
| 956 |
while(1); |
| 957 |
|
| 958 |
iconv_close(encoder); |
| 959 |
TRACE_FLOW_EXIT(); |
| 960 |
return EOK; |
| 961 |
} |
| 962 |
|
| 963 |
|
| 964 |
static int write_bom(int fd, |
| 965 |
enum index_utf_t bom) |
| 966 |
{ |
| 967 |
unsigned char buffer[4]; |
| 968 |
size_t size = 0; |
| 969 |
ssize_t ret; |
| 970 |
int error = EOK; |
| 971 |
|
| 972 |
TRACE_FLOW_ENTRY(); |
| 973 |
|
| 974 |
switch (bom) { |
| 975 |
|
| 976 |
case INDEX_UTF32BE: |
| 977 |
buffer[0] = 0x00; |
| 978 |
buffer[1] = 0x00; |
| 979 |
buffer[2] = 0xFE; |
| 980 |
buffer[3] = 0xFF; |
| 981 |
size = BOM4_SIZE; |
| 982 |
break; |
| 983 |
|
| 984 |
case INDEX_UTF32LE: |
| 985 |
buffer[0] = 0xFF; |
| 986 |
buffer[1] = 0xFE; |
| 987 |
buffer[2] = 0x00; |
| 988 |
buffer[3] = 0x00; |
| 989 |
size = BOM4_SIZE; |
| 990 |
break; |
| 991 |
|
| 992 |
case INDEX_UTF8: |
| 993 |
buffer[0] = 0xEF; |
| 994 |
buffer[1] = 0xBB; |
| 995 |
buffer[2] = 0xBF; |
| 996 |
size = BOM3_SIZE; |
| 997 |
break; |
| 998 |
|
| 999 |
case INDEX_UTF16BE: |
| 1000 |
buffer[0] = 0xFE; |
| 1001 |
buffer[1] = 0xFF; |
| 1002 |
size = BOM2_SIZE; |
| 1003 |
break; |
| 1004 |
|
| 1005 |
case INDEX_UTF16LE: |
| 1006 |
buffer[0] = 0xFF; |
| 1007 |
buffer[1] = 0xFE; |
| 1008 |
size = BOM2_SIZE; |
| 1009 |
break; |
| 1010 |
|
| 1011 |
default: |
| 1012 |
|
| 1013 |
|
| 1014 |
TRACE_ERROR_NUMBER("Invalid bom type", bom); |
| 1015 |
break; |
| 1016 |
} |
| 1017 |
|
| 1018 |
ret = write(fd, buffer, size); |
| 1019 |
if (ret == -1) { |
| 1020 |
error = errno; |
| 1021 |
TRACE_ERROR_NUMBER("Failed to write bom bytes.", error); |
| 1022 |
return error; |
| 1023 |
} |
| 1024 |
|
| 1025 |
TRACE_FLOW_EXIT(); |
| 1026 |
return EOK; |
| 1027 |
} |
| 1028 |
|
| 1029 |
|
| 1030 |
static int write_to_file(struct ini_cfgfile *file_ctx, |
| 1031 |
const char *filename, |
| 1032 |
struct access_check *overwrite, |
| 1033 |
int check) |
| 1034 |
{ |
| 1035 |
int error = EOK; |
| 1036 |
uid_t uid = 0; |
| 1037 |
gid_t gid = 0; |
| 1038 |
mode_t mode = 0; |
| 1039 |
int fd = -1; |
| 1040 |
uint32_t left = 0; |
| 1041 |
struct simplebuffer *sb = NULL; |
| 1042 |
struct simplebuffer *sb_ptr = NULL; |
| 1043 |
|
| 1044 |
TRACE_FLOW_ENTRY(); |
| 1045 |
|
| 1046 |
|
| 1047 |
error = determine_permissions(file_ctx, |
| 1048 |
overwrite, |
| 1049 |
&uid, |
| 1050 |
&gid, |
| 1051 |
&mode); |
| 1052 |
if (error) { |
| 1053 |
TRACE_ERROR_NUMBER("Failed to determine permissions.", error); |
| 1054 |
return error; |
| 1055 |
} |
| 1056 |
|
| 1057 |
|
| 1058 |
error = open_new_file(filename, |
| 1059 |
uid, |
| 1060 |
gid, |
| 1061 |
mode, |
| 1062 |
check, |
| 1063 |
&fd); |
| 1064 |
if (error) { |
| 1065 |
TRACE_ERROR_NUMBER("Failed to open new file.", error); |
| 1066 |
return error; |
| 1067 |
} |
| 1068 |
|
| 1069 |
|
| 1070 |
if (file_ctx->bom != INDEX_UTF8NOBOM) { |
| 1071 |
|
| 1072 |
if (file_ctx->bom != INDEX_UTF8) { |
| 1073 |
|
| 1074 |
error = simplebuffer_alloc(&sb); |
| 1075 |
if (error) { |
| 1076 |
TRACE_ERROR_NUMBER("Failed to allocate buffer for conversion", |
| 1077 |
error); |
| 1078 |
close(fd); |
| 1079 |
return error; |
| 1080 |
} |
| 1081 |
|
| 1082 |
|
| 1083 |
error = do_encoding(file_ctx, sb); |
| 1084 |
if (error) { |
| 1085 |
TRACE_ERROR_NUMBER("Failed to re-encode", error); |
| 1086 |
simplebuffer_free(sb); |
| 1087 |
close(fd); |
| 1088 |
return error; |
| 1089 |
} |
| 1090 |
sb_ptr = sb; |
| 1091 |
|
| 1092 |
} |
| 1093 |
else sb_ptr = file_ctx->file_data; |
| 1094 |
|
| 1095 |
|
| 1096 |
error = write_bom(fd, file_ctx->bom); |
| 1097 |
if (error) { |
| 1098 |
TRACE_ERROR_NUMBER("Failed to save bom", error); |
| 1099 |
simplebuffer_free(sb); |
| 1100 |
close(fd); |
| 1101 |
return error; |
| 1102 |
} |
| 1103 |
|
| 1104 |
} |
| 1105 |
else sb_ptr = file_ctx->file_data; |
| 1106 |
|
| 1107 |
left = simplebuffer_get_len(sb_ptr); |
| 1108 |
do { |
| 1109 |
error = simplebuffer_write(fd, sb_ptr, &left); |
| 1110 |
if (error) { |
| 1111 |
TRACE_ERROR_NUMBER("Failed to write data", error); |
| 1112 |
simplebuffer_free(sb); |
| 1113 |
close(fd); |
| 1114 |
return error; |
| 1115 |
} |
| 1116 |
} |
| 1117 |
while (left > 0); |
| 1118 |
|
| 1119 |
simplebuffer_free(sb); |
| 1120 |
close(fd); |
| 1121 |
|
| 1122 |
TRACE_FLOW_EXIT(); |
| 1123 |
return error; |
| 1124 |
} |
| 1125 |
|
| 1126 |
|
| 1127 |
|
| 1128 |
|
| 1129 |
|
| 1130 |
static int check_template(const char *tpl) |
| 1131 |
{ |
| 1132 |
char *ptr; |
| 1133 |
char *ptr_pcnt = NULL; |
| 1134 |
|
| 1135 |
TRACE_FLOW_ENTRY(); |
| 1136 |
|
| 1137 |
|
| 1138 |
ptr = (char *)(intptr_t)tpl; |
| 1139 |
|
| 1140 |
for (;;) { |
| 1141 |
|
| 1142 |
ptr = strchr(ptr, '%'); |
| 1143 |
if (ptr == NULL) { |
| 1144 |
TRACE_ERROR_NUMBER("No '%%d' found in format", EINVAL); |
| 1145 |
return EINVAL; |
| 1146 |
} |
| 1147 |
else { |
| 1148 |
if (*(ptr + 1) == 'd') { |
| 1149 |
ptr_pcnt = ptr + 2; |
| 1150 |
|
| 1151 |
for (;;) { |
| 1152 |
ptr_pcnt = strchr(ptr_pcnt, '%'); |
| 1153 |
if (ptr_pcnt) { |
| 1154 |
ptr_pcnt++; |
| 1155 |
if (*ptr_pcnt != '%') { |
| 1156 |
TRACE_ERROR_NUMBER("Single '%%' " |
| 1157 |
"symbol after '%%d'.", EINVAL); |
| 1158 |
return EINVAL; |
| 1159 |
} |
| 1160 |
ptr_pcnt++; |
| 1161 |
} |
| 1162 |
else break; |
| 1163 |
} |
| 1164 |
break; |
| 1165 |
} |
| 1166 |
|
| 1167 |
else if (*(ptr + 1) == '%') { |
| 1168 |
ptr += 2; |
| 1169 |
continue; |
| 1170 |
} |
| 1171 |
else { |
| 1172 |
TRACE_ERROR_NUMBER("Single '%%' " |
| 1173 |
"symbol before '%%d'.", EINVAL); |
| 1174 |
return EINVAL; |
| 1175 |
} |
| 1176 |
} |
| 1177 |
} |
| 1178 |
TRACE_FLOW_EXIT(); |
| 1179 |
return EOK; |
| 1180 |
} |
| 1181 |
|
| 1182 |
|
| 1183 |
int ini_config_file_backup(struct ini_cfgfile *file_ctx, |
| 1184 |
const char *backup_dir, |
| 1185 |
const char *backup_tpl, |
| 1186 |
struct access_check *backup_access, |
| 1187 |
unsigned max_num) |
| 1188 |
{ |
| 1189 |
int error = EOK; |
| 1190 |
DIR *ddir = NULL; |
| 1191 |
char *filename = NULL; |
| 1192 |
unsigned i; |
| 1193 |
|
| 1194 |
TRACE_FLOW_ENTRY(); |
| 1195 |
|
| 1196 |
if (file_ctx == NULL) { |
| 1197 |
TRACE_ERROR_NUMBER("Invalid parameter.", EINVAL); |
| 1198 |
return EINVAL; |
| 1199 |
} |
| 1200 |
|
| 1201 |
if (backup_tpl == NULL) { |
| 1202 |
TRACE_ERROR_NUMBER("Name template is required.", EINVAL); |
| 1203 |
return EINVAL; |
| 1204 |
} |
| 1205 |
|
| 1206 |
|
| 1207 |
error = check_template(backup_tpl); |
| 1208 |
if (error) { |
| 1209 |
TRACE_ERROR_NUMBER("Name template is invalid.", error); |
| 1210 |
return error; |
| 1211 |
} |
| 1212 |
|
| 1213 |
if (backup_dir) { |
| 1214 |
|
| 1215 |
errno = 0; |
| 1216 |
ddir = opendir(backup_dir); |
| 1217 |
if (!ddir) { |
| 1218 |
error = errno; |
| 1219 |
TRACE_ERROR_NUMBER("Something is wrong with the directory.", error); |
| 1220 |
return error; |
| 1221 |
} |
| 1222 |
} |
| 1223 |
|
| 1224 |
for (i = 1; i <= max_num; i++) { |
| 1225 |
|
| 1226 |
error = create_file_name(backup_dir, backup_tpl, i, &filename); |
| 1227 |
if (error) { |
| 1228 |
TRACE_ERROR_NUMBER("Failed to create path.", error); |
| 1229 |
if (ddir) closedir(ddir); |
| 1230 |
return error; |
| 1231 |
} |
| 1232 |
|
| 1233 |
error = write_to_file(file_ctx, filename, backup_access, 1); |
| 1234 |
free(filename); |
| 1235 |
if (error) { |
| 1236 |
if ((error == EEXIST) || (error == EACCES)) { |
| 1237 |
|
| 1238 |
|
| 1239 |
|
| 1240 |
TRACE_INFO_STRING("File existis.", "Retrying."); |
| 1241 |
continue; |
| 1242 |
} |
| 1243 |
TRACE_ERROR_NUMBER("Failed to write file.", error); |
| 1244 |
if (ddir) closedir(ddir); |
| 1245 |
return error; |
| 1246 |
} |
| 1247 |
break; |
| 1248 |
} |
| 1249 |
|
| 1250 |
if (ddir) closedir(ddir); |
| 1251 |
TRACE_FLOW_EXIT(); |
| 1252 |
return error; |
| 1253 |
} |
| 1254 |
|
| 1255 |
|
| 1256 |
int ini_config_change_access(struct ini_cfgfile *file_ctx, |
| 1257 |
struct access_check *new_access) |
| 1258 |
{ |
| 1259 |
int error = EOK; |
| 1260 |
uid_t uid = 0; |
| 1261 |
gid_t gid = 0; |
| 1262 |
mode_t mode = 0; |
| 1263 |
int ret; |
| 1264 |
|
| 1265 |
TRACE_FLOW_ENTRY(); |
| 1266 |
|
| 1267 |
|
| 1268 |
if (*(file_ctx->filename) == '\0') { |
| 1269 |
TRACE_ERROR_NUMBER("Invalid file context.", EINVAL); |
| 1270 |
return EINVAL; |
| 1271 |
} |
| 1272 |
|
| 1273 |
if (!(new_access)) { |
| 1274 |
TRACE_ERROR_NUMBER("Access structure is required.", EINVAL); |
| 1275 |
return EINVAL; |
| 1276 |
} |
| 1277 |
|
| 1278 |
|
| 1279 |
error = determine_permissions(file_ctx, |
| 1280 |
new_access, |
| 1281 |
&uid, |
| 1282 |
&gid, |
| 1283 |
&mode); |
| 1284 |
if (error) { |
| 1285 |
TRACE_ERROR_NUMBER("Failed to determine permissions.", error); |
| 1286 |
return error; |
| 1287 |
} |
| 1288 |
|
| 1289 |
errno = 0; |
| 1290 |
ret = chmod(file_ctx->filename, mode); |
| 1291 |
if (ret == -1) { |
| 1292 |
error = errno; |
| 1293 |
TRACE_ERROR_NUMBER("Failed to chmod file.", error); |
| 1294 |
return error; |
| 1295 |
} |
| 1296 |
|
| 1297 |
errno = 0; |
| 1298 |
ret = chown(file_ctx->filename, uid, gid); |
| 1299 |
if (ret == -1) { |
| 1300 |
error = errno; |
| 1301 |
TRACE_ERROR_NUMBER("Failed to chown file.", error); |
| 1302 |
return error; |
| 1303 |
} |
| 1304 |
|
| 1305 |
if (file_ctx->metadata_flags & INI_META_STATS) { |
| 1306 |
file_ctx->stats_read = 1; |
| 1307 |
ret = stat(file_ctx->filename, &(file_ctx->file_stats)); |
| 1308 |
if (ret == -1) { |
| 1309 |
error = errno; |
| 1310 |
TRACE_ERROR_NUMBER("Failed to get file stats", error); |
| 1311 |
return error; |
| 1312 |
} |
| 1313 |
} |
| 1314 |
else { |
| 1315 |
memset(&(file_ctx->file_stats), 0, sizeof(struct stat)); |
| 1316 |
file_ctx->stats_read = 0; |
| 1317 |
} |
| 1318 |
|
| 1319 |
TRACE_FLOW_EXIT(); |
| 1320 |
return error; |
| 1321 |
} |
| 1322 |
|
| 1323 |
|
| 1324 |
int ini_config_save(struct ini_cfgfile *file_ctx, |
| 1325 |
struct access_check *new_access, |
| 1326 |
struct ini_cfgobj *ini_config) |
| 1327 |
{ |
| 1328 |
int error = EOK; |
| 1329 |
|
| 1330 |
TRACE_FLOW_ENTRY(); |
| 1331 |
|
| 1332 |
error = ini_config_save_as(file_ctx, |
| 1333 |
NULL, |
| 1334 |
new_access, |
| 1335 |
ini_config); |
| 1336 |
|
| 1337 |
TRACE_FLOW_EXIT(); |
| 1338 |
return error; |
| 1339 |
} |
| 1340 |
|
| 1341 |
|
| 1342 |
int ini_config_save_as(struct ini_cfgfile *file_ctx, |
| 1343 |
const char *filename, |
| 1344 |
struct access_check *new_access, |
| 1345 |
struct ini_cfgobj *ini_config) |
| 1346 |
{ |
| 1347 |
int error = EOK; |
| 1348 |
struct simplebuffer *sbobj = NULL; |
| 1349 |
|
| 1350 |
TRACE_FLOW_ENTRY(); |
| 1351 |
|
| 1352 |
if (*(file_ctx->filename) == '\0') { |
| 1353 |
TRACE_ERROR_NUMBER("Attempt to use wrong file context", EINVAL); |
| 1354 |
return EINVAL; |
| 1355 |
} |
| 1356 |
|
| 1357 |
error = simplebuffer_alloc(&sbobj); |
| 1358 |
if (error) { |
| 1359 |
TRACE_ERROR_NUMBER("Failed to allocate buffer.", error); |
| 1360 |
return error; |
| 1361 |
} |
| 1362 |
|
| 1363 |
error = ini_config_serialize(ini_config, sbobj); |
| 1364 |
if (error) { |
| 1365 |
TRACE_ERROR_NUMBER("Failed to serialize.", error); |
| 1366 |
simplebuffer_free(sbobj); |
| 1367 |
return error; |
| 1368 |
} |
| 1369 |
|
| 1370 |
|
| 1371 |
ini_config_file_close(file_ctx); |
| 1372 |
|
| 1373 |
|
| 1374 |
simplebuffer_free(file_ctx->file_data); |
| 1375 |
file_ctx->file_data = sbobj; |
| 1376 |
sbobj = NULL; |
| 1377 |
|
| 1378 |
if (filename) { |
| 1379 |
|
| 1380 |
free(file_ctx->filename); |
| 1381 |
file_ctx->filename = NULL; |
| 1382 |
|
| 1383 |
|
| 1384 |
file_ctx->filename = malloc(PATH_MAX + 1); |
| 1385 |
if (!(file_ctx->filename)) { |
| 1386 |
TRACE_ERROR_NUMBER("Failed to allocate memory for file path.", |
| 1387 |
ENOMEM); |
| 1388 |
return ENOMEM; |
| 1389 |
} |
| 1390 |
|
| 1391 |
|
| 1392 |
error = make_normalized_absolute_path(file_ctx->filename, |
| 1393 |
PATH_MAX, |
| 1394 |
filename); |
| 1395 |
if(error) { |
| 1396 |
TRACE_ERROR_NUMBER("Failed to resolve path", error); |
| 1397 |
return error; |
| 1398 |
} |
| 1399 |
} |
| 1400 |
|
| 1401 |
|
| 1402 |
error = write_to_file(file_ctx, file_ctx->filename, new_access, 0); |
| 1403 |
if (error) { |
| 1404 |
TRACE_ERROR_NUMBER("Failed to write file.", error); |
| 1405 |
return error; |
| 1406 |
} |
| 1407 |
|
| 1408 |
|
| 1409 |
simplebuffer_free(file_ctx->file_data); |
| 1410 |
file_ctx->file_data = NULL; |
| 1411 |
|
| 1412 |
error = simplebuffer_alloc(&sbobj); |
| 1413 |
if (error) { |
| 1414 |
TRACE_ERROR_NUMBER("Failed to allocate buffer.", error); |
| 1415 |
return error; |
| 1416 |
} |
| 1417 |
|
| 1418 |
file_ctx->file_data = sbobj; |
| 1419 |
|
| 1420 |
|
| 1421 |
error = common_file_init(file_ctx, NULL, 0); |
| 1422 |
if(error) { |
| 1423 |
TRACE_ERROR_NUMBER("Failed to do common init", error); |
| 1424 |
return error; |
| 1425 |
} |
| 1426 |
|
| 1427 |
TRACE_FLOW_EXIT(); |
| 1428 |
return EOK; |
| 1429 |
} |
| 1430 |
|
| 1431 |
|
| 1432 |
enum index_utf_t ini_config_get_bom(struct ini_cfgfile *file_ctx) |
| 1433 |
{ |
| 1434 |
enum index_utf_t ret; |
| 1435 |
TRACE_FLOW_ENTRY(); |
| 1436 |
|
| 1437 |
ret = file_ctx->bom; |
| 1438 |
|
| 1439 |
TRACE_FLOW_EXIT(); |
| 1440 |
return ret; |
| 1441 |
} |
| 1442 |
|
| 1443 |
|
| 1444 |
|
| 1445 |
int ini_config_set_bom(struct ini_cfgfile *file_ctx, enum index_utf_t bom) |
| 1446 |
{ |
| 1447 |
TRACE_FLOW_ENTRY(); |
| 1448 |
|
| 1449 |
if (file_ctx == NULL) { |
| 1450 |
TRACE_ERROR_NUMBER("Invalid parameter.", EINVAL); |
| 1451 |
return EINVAL; |
| 1452 |
} |
| 1453 |
|
| 1454 |
file_ctx->bom = bom; |
| 1455 |
|
| 1456 |
TRACE_FLOW_EXIT(); |
| 1457 |
return EOK; |
| 1458 |
} |
| 1459 |
|
| 1460 |
|
| 1461 |
|
| 1462 |
const char *ini_config_get_filename(struct ini_cfgfile *file_ctx) |
| 1463 |
{ |
| 1464 |
const char *ret; |
| 1465 |
TRACE_FLOW_ENTRY(); |
| 1466 |
|
| 1467 |
ret = file_ctx->filename; |
| 1468 |
|
| 1469 |
TRACE_FLOW_EXIT(); |
| 1470 |
return ret; |
| 1471 |
} |
| 1472 |
|
| 1473 |
|
| 1474 |
const struct stat *ini_config_get_stat(struct ini_cfgfile *file_ctx) |
| 1475 |
{ |
| 1476 |
const struct stat *ret; |
| 1477 |
TRACE_FLOW_ENTRY(); |
| 1478 |
|
| 1479 |
if (file_ctx->stats_read) ret = &(file_ctx->file_stats); |
| 1480 |
else ret = NULL; |
| 1481 |
|
| 1482 |
TRACE_FLOW_EXIT(); |
| 1483 |
return ret; |
| 1484 |
} |
| 1485 |
|
| 1486 |
|
| 1487 |
int access_check_int(struct stat *file_stats, |
| 1488 |
uint32_t flags, |
| 1489 |
uid_t uid, |
| 1490 |
gid_t gid, |
| 1491 |
mode_t mode, |
| 1492 |
mode_t mask) |
| 1493 |
{ |
| 1494 |
mode_t st_mode; |
| 1495 |
|
| 1496 |
TRACE_FLOW_ENTRY(); |
| 1497 |
|
| 1498 |
flags &= INI_ACCESS_CHECK_MODE | |
| 1499 |
INI_ACCESS_CHECK_GID | |
| 1500 |
INI_ACCESS_CHECK_UID; |
| 1501 |
|
| 1502 |
if (flags == 0) { |
| 1503 |
TRACE_ERROR_NUMBER("Invalid parameter.", EINVAL); |
| 1504 |
return EINVAL; |
| 1505 |
} |
| 1506 |
|
| 1507 |
|
| 1508 |
if (flags & INI_ACCESS_CHECK_MODE) { |
| 1509 |
|
| 1510 |
TRACE_INFO_NUMBER("File mode as saved.", |
| 1511 |
file_stats->st_mode); |
| 1512 |
|
| 1513 |
st_mode = file_stats->st_mode; |
| 1514 |
st_mode &= S_IRWXU | S_IRWXG | S_IRWXO; |
| 1515 |
TRACE_INFO_NUMBER("File mode adjusted.", st_mode); |
| 1516 |
|
| 1517 |
TRACE_INFO_NUMBER("Mode as provided.", mode); |
| 1518 |
mode &= S_IRWXU | S_IRWXG | S_IRWXO; |
| 1519 |
TRACE_INFO_NUMBER("Mode adjusted.", mode); |
| 1520 |
|
| 1521 |
|
| 1522 |
if (mask == 0) mask = S_IRWXU | S_IRWXG | S_IRWXO; |
| 1523 |
else mask &= S_IRWXU | S_IRWXG | S_IRWXO; |
| 1524 |
|
| 1525 |
if ((mode & mask) != (st_mode & mask)) { |
| 1526 |
TRACE_INFO_NUMBER("File mode:", (mode & mask)); |
| 1527 |
TRACE_INFO_NUMBER("Mode adjusted.", |
| 1528 |
(st_mode & mask)); |
| 1529 |
TRACE_ERROR_NUMBER("Access denied.", EACCES); |
| 1530 |
return EACCES; |
| 1531 |
} |
| 1532 |
} |
| 1533 |
|
| 1534 |
|
| 1535 |
if (flags & INI_ACCESS_CHECK_UID) { |
| 1536 |
if (file_stats->st_uid != uid) { |
| 1537 |
TRACE_ERROR_NUMBER("UID:", file_stats->st_uid); |
| 1538 |
TRACE_ERROR_NUMBER("UID passed in.", uid); |
| 1539 |
TRACE_ERROR_NUMBER("Access denied.", EACCES); |
| 1540 |
return EACCES; |
| 1541 |
} |
| 1542 |
} |
| 1543 |
|
| 1544 |
|
| 1545 |
if (flags & INI_ACCESS_CHECK_GID) { |
| 1546 |
if (file_stats->st_gid != gid) { |
| 1547 |
TRACE_ERROR_NUMBER("GID:", file_stats->st_gid); |
| 1548 |
TRACE_ERROR_NUMBER("GID passed in.", gid); |
| 1549 |
TRACE_ERROR_NUMBER("Access denied.", EACCES); |
| 1550 |
return EACCES; |
| 1551 |
} |
| 1552 |
} |
| 1553 |
|
| 1554 |
TRACE_FLOW_EXIT(); |
| 1555 |
return EOK; |
| 1556 |
|
| 1557 |
} |
| 1558 |
|
| 1559 |
|
| 1560 |
int ini_config_access_check(struct ini_cfgfile *file_ctx, |
| 1561 |
uint32_t flags, |
| 1562 |
uid_t uid, |
| 1563 |
gid_t gid, |
| 1564 |
mode_t mode, |
| 1565 |
mode_t mask) |
| 1566 |
{ |
| 1567 |
int error = EOK; |
| 1568 |
|
| 1569 |
TRACE_FLOW_ENTRY(); |
| 1570 |
|
| 1571 |
if (file_ctx == NULL) { |
| 1572 |
TRACE_ERROR_NUMBER("Invalid parameter.", EINVAL); |
| 1573 |
return EINVAL; |
| 1574 |
} |
| 1575 |
|
| 1576 |
if (file_ctx->stats_read == 0) { |
| 1577 |
TRACE_ERROR_NUMBER("Stats were not collected.", EINVAL); |
| 1578 |
return EINVAL; |
| 1579 |
} |
| 1580 |
|
| 1581 |
error = access_check_int(&(file_ctx->file_stats), |
| 1582 |
flags, |
| 1583 |
uid, |
| 1584 |
gid, |
| 1585 |
mode, |
| 1586 |
mask); |
| 1587 |
|
| 1588 |
TRACE_FLOW_EXIT(); |
| 1589 |
return error; |
| 1590 |
|
| 1591 |
} |
| 1592 |
|
| 1593 |
|
| 1594 |
|
| 1595 |
|
| 1596 |
|
| 1597 |
|
| 1598 |
int ini_config_changed(struct ini_cfgfile *file_ctx1, |
| 1599 |
struct ini_cfgfile *file_ctx2, |
| 1600 |
int *changed) |
| 1601 |
{ |
| 1602 |
TRACE_FLOW_ENTRY(); |
| 1603 |
|
| 1604 |
if ((file_ctx1 == NULL) || |
| 1605 |
(file_ctx2 == NULL) || |
| 1606 |
(changed == NULL)) { |
| 1607 |
TRACE_ERROR_NUMBER("Invalid parameter.", EINVAL); |
| 1608 |
return EINVAL; |
| 1609 |
} |
| 1610 |
|
| 1611 |
if ((file_ctx1->stats_read == 0) || |
| 1612 |
(file_ctx2->stats_read == 0)) { |
| 1613 |
TRACE_ERROR_NUMBER("Stats were not collected.", EINVAL); |
| 1614 |
return EINVAL; |
| 1615 |
} |
| 1616 |
|
| 1617 |
*changed = 0; |
| 1618 |
|
| 1619 |
|
| 1620 |
|
| 1621 |
|
| 1622 |
|
| 1623 |
|
| 1624 |
|
| 1625 |
|
| 1626 |
if((file_ctx1->file_stats.st_mtime != |
| 1627 |
file_ctx2->file_stats.st_mtime) || |
| 1628 |
(file_ctx1->file_stats.st_dev != |
| 1629 |
file_ctx2->file_stats.st_dev) || |
| 1630 |
(file_ctx1->file_stats.st_ino != |
| 1631 |
file_ctx2->file_stats.st_ino)) { |
| 1632 |
TRACE_INFO_STRING("File changed!", ""); |
| 1633 |
*changed = 1; |
| 1634 |
} |
| 1635 |
|
| 1636 |
TRACE_FLOW_EXIT(); |
| 1637 |
return EOK; |
| 1638 |
} |
| 1639 |
|
| 1640 |
|
| 1641 |
void ini_config_file_print(struct ini_cfgfile *file_ctx) |
| 1642 |
{ |
| 1643 |
TRACE_FLOW_ENTRY(); |
| 1644 |
if (file_ctx == NULL) { |
| 1645 |
printf("No file object\n."); |
| 1646 |
} |
| 1647 |
else { |
| 1648 |
printf("File name: %s\n", |
| 1649 |
(file_ctx->filename) ? file_ctx->filename : "NULL"); |
| 1650 |
printf("File is %s\n", (file_ctx->file) ? "open" : "closed"); |
| 1651 |
printf("File BOM %d\n", file_ctx->bom); |
| 1652 |
printf("Metadata flags %u\n", file_ctx->metadata_flags); |
| 1653 |
printf("Stats flag st_dev %li\n", file_ctx->file_stats.st_dev); |
| 1654 |
printf("Stats flag st_ino %li\n", file_ctx->file_stats.st_ino); |
| 1655 |
printf("Stats flag st_mode %u\n", file_ctx->file_stats.st_mode); |
| 1656 |
printf("Stats flag st_nlink %li\n", file_ctx->file_stats.st_nlink); |
| 1657 |
printf("Stats flag st_uid %u\n", file_ctx->file_stats.st_uid); |
| 1658 |
printf("Stats flag st_gid %u\n", file_ctx->file_stats.st_gid); |
| 1659 |
printf("Stats flag st_rdev %li\n", file_ctx->file_stats.st_rdev); |
| 1660 |
printf("Stats flag st_size %lu\n", file_ctx->file_stats.st_size); |
| 1661 |
printf("Stats flag st_blocks %li\n", file_ctx->file_stats.st_blocks); |
| 1662 |
printf("Stats flag st_atime %ld\n", file_ctx->file_stats.st_atime); |
| 1663 |
printf("Stats flag st_mtime %ld\n", file_ctx->file_stats.st_mtime); |
| 1664 |
printf("Stats flag st_ctime %ld\n", file_ctx->file_stats.st_ctime); |
| 1665 |
} |
| 1666 |
TRACE_FLOW_EXIT(); |
| 1667 |
} |