| 1 | /* |
| 2 | Authors: |
| 3 | Pavel Březina <[email protected]> |
| 4 | |
| 5 | Copyright (C) 2018 Red Hat |
| 6 | |
| 7 | This program is free software; you can redistribute it and/or modify |
| 8 | it under the terms of the GNU General Public License as published by |
| 9 | the Free Software Foundation; either version 3 of the License, or |
| 10 | (at your option) any later version. |
| 11 | |
| 12 | This program is distributed in the hope that it will be useful, |
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | GNU General Public License for more details. |
| 16 | |
| 17 | You should have received a copy of the GNU General Public License |
| 18 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | */ |
| 20 | |
| 21 | #include <time.h> |
| 22 | #include <regex.h> |
| 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <unistd.h> |
| 26 | #include <sys/stat.h> |
| 27 | |
| 28 | #include "common/common.h" |
| 29 | #include "lib/util/template.h" |
| 30 | #include "lib/util/textfile.h" |
| 31 | #include "lib/util/selinux.h" |
| 32 | #include "lib/util/string.h" |
| 33 | #include "lib/util/string_array.h" |
| 34 | #include "lib/util/evaluator.h" |
| 35 | |
| 36 | #define RE_MATCHES 12 |
| 37 | #define RE_VALUE "([^{}|]{0,})" |
| 38 | #define RE_FEATURE "\"([^{}\"|]{1,})\"" |
| 39 | #define RE_EXPRESSION "([^{}|:]{1,})" |
| 40 | #define OP_RE_LINE "(continue if|stop if|include if|exclude if) " RE_EXPRESSION |
| 41 | #define OP_RE_IMPLY "(imply) " RE_FEATURE " if " RE_EXPRESSION |
| 42 | #define OP_RE_IF "(if) " RE_EXPRESSION ":" RE_VALUE "(\\|" RE_VALUE "){0,1}" |
| 43 | #define OP_RE "\\{(" OP_RE_LINE "|" OP_RE_IF "|" OP_RE_IMPLY ")\\}" |
| 44 | |
| 45 | enum template_operator { |
| 46 | OP_CONTINUE, |
| 47 | OP_STOP, |
| 48 | OP_INCLUDE, |
| 49 | OP_EXCLUDE, |
| 50 | OP_IMPLY, |
| 51 | OP_IF, |
| 52 | |
| 53 | OP_SENTINEL |
| 54 | }; |
| 55 | |
| 56 | void |
| 57 | template_debug_print_matches(const char *str, |
| 58 | regmatch_t *matches, |
| 59 | int count) |
| 60 | { |
| 61 | #ifdef DEBUG_TEMPLATE_REGEX |
| 62 | regmatch_t *match; |
| 63 | int i; |
| 64 | |
| 65 | printf("\n"); |
| 66 | for (i = 0; i < count; i++) { |
| 67 | match = &matches[i]; |
| 68 | if (match->rm_so == -1) { |
| 69 | continue; |
| 70 | } |
| 71 | |
| 72 | printf("Match %d: %.*s\n", i, match->rm_eo - match->rm_so, |
| 73 | str + match->rm_so); |
| 74 | } |
| 75 | #else |
| 76 | return; |
| 77 | #endif |
| 78 | } |
| 79 | |
| 80 | static enum template_operator |
| 81 | template_match_get_operator(const char *match_string, |
| 82 | regmatch_t *m) |
| 83 | { |
| 84 | size_t len; |
| 85 | int i; |
| 86 | struct { |
| 87 | const char *str; |
| 88 | enum template_operator op; |
| 89 | } operators[] = { |
| 90 | {"continue if", OP_CONTINUE}, |
| 91 | {"stop if", OP_STOP}, |
| 92 | {"include if", OP_INCLUDE}, |
| 93 | {"exclude if", OP_EXCLUDE}, |
| 94 | {"imply", OP_IMPLY}, |
| 95 | {"if", OP_IF}, |
| 96 | {NULL, OP_SENTINEL} |
| 97 | }; |
| 98 | |
| 99 | for (i = 0; operators[i].str != NULL; i++) { |
| 100 | len = strlen(operators[i].str); |
| 101 | if (strncmp(match_string + m->rm_so, operators[i].str, len) == 0) { |
| 102 | return operators[i].op; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | return OP_SENTINEL; |
| 107 | } |
| 108 | |
| 109 | static char * |
| 110 | template_match_get_string(const char *match_string, |
| 111 | regmatch_t *match) |
| 112 | { |
| 113 | if (match->rm_so == -1) { |
| 114 | return strdup(""); |
| 115 | } |
| 116 | |
| 117 | return strndup(match_string + match->rm_so, match->rm_eo - match->rm_so); |
| 118 | } |
| 119 | |
| 120 | static errno_t |
| 121 | template_match_get_expression(const char *match_string, |
| 122 | enum template_operator op, |
| 123 | regmatch_t *matches, |
| 124 | char **_expression) |
| 125 | { |
| 126 | regmatch_t *match = NULL; |
| 127 | char *expression; |
| 128 | |
| 129 | switch (op) { |
| 130 | case OP_CONTINUE: |
| 131 | case OP_STOP: |
| 132 | case OP_INCLUDE: |
| 133 | case OP_EXCLUDE: |
| 134 | match = &matches[3]; |
| 135 | break; |
| 136 | case OP_IMPLY: |
| 137 | match = &matches[11]; |
| 138 | break; |
| 139 | case OP_IF: |
| 140 | match = &matches[5]; |
| 141 | break; |
| 142 | case OP_SENTINEL: |
| 143 | ERROR("Invalid operator!"); |
| 144 | return EINVAL; |
| 145 | } |
| 146 | |
| 147 | expression = template_match_get_string(match_string, match); |
| 148 | if (expression == NULL) { |
| 149 | return ENOMEM; |
| 150 | } |
| 151 | |
| 152 | *_expression = expression; |
| 153 | |
| 154 | return EOK; |
| 155 | } |
| 156 | |
| 157 | static errno_t |
| 158 | template_match_get_values(const char *match_string, |
| 159 | enum template_operator op, |
| 160 | regmatch_t *matches, |
| 161 | char **_if_true, |
| 162 | char **_if_false, |
| 163 | char **_value) |
| 164 | { |
| 165 | char *if_true; |
| 166 | char *if_false; |
| 167 | char *value; |
| 168 | |
| 169 | switch (op) { |
| 170 | case OP_CONTINUE: |
| 171 | case OP_STOP: |
| 172 | case OP_INCLUDE: |
| 173 | case OP_EXCLUDE: |
| 174 | *_if_true = NULL; |
| 175 | *_if_false = NULL; |
| 176 | *_value = NULL; |
| 177 | break; |
| 178 | case OP_IMPLY: |
| 179 | value = template_match_get_string(match_string, &matches[10]); |
| 180 | if (value == NULL) { |
| 181 | return ENOMEM; |
| 182 | } |
| 183 | |
| 184 | *_if_true = NULL; |
| 185 | *_if_false = NULL; |
| 186 | *_value = value; |
| 187 | return EOK; |
| 188 | case OP_IF: |
| 189 | if_true = template_match_get_string(match_string, &matches[6]); |
| 190 | if (if_true == NULL) { |
| 191 | return ENOMEM; |
| 192 | } |
| 193 | |
| 194 | if_false = template_match_get_string(match_string, &matches[8]); |
| 195 | if (if_false == NULL) { |
| 196 | free(if_true); |
| 197 | return ENOMEM; |
| 198 | } |
| 199 | |
| 200 | *_if_true = if_true; |
| 201 | *_if_false = if_false; |
| 202 | *_value = NULL; |
| 203 | break; |
| 204 | case OP_SENTINEL: |
| 205 | ERROR("Invalid operator!"); |
| 206 | return EINVAL; |
| 207 | } |
| 208 | |
| 209 | return EOK; |
| 210 | } |
| 211 | |
| 212 | static errno_t |
| 213 | template_match_replace(char ***features, |
| 214 | char *beginning, |
| 215 | char *match_string, |
| 216 | regmatch_t *match, |
| 217 | enum template_operator op, |
| 218 | const char *expression, |
| 219 | const char *if_true, |
| 220 | const char *if_false, |
| 221 | const char *value) |
| 222 | { |
| 223 | const char *replacement; |
| 224 | bool enabled; |
| 225 | int ret; |
| 226 | |
| 227 | ret = evaluate(expression, (const char **)*features, &enabled); |
| 228 | if (ret != EOK) { |
| 229 | return ret; |
| 230 | } |
| 231 | |
| 232 | switch (op) { |
| 233 | case OP_CONTINUE: |
| 234 | if (enabled) { |
| 235 | string_remove_line(beginning, match_string, match->rm_so); |
| 236 | break; |
| 237 | } |
| 238 | |
| 239 | string_remove_remainder(match_string, match->rm_so); |
| 240 | break; |
| 241 | case OP_STOP: |
| 242 | if (!enabled) { |
| 243 | string_remove_line(beginning, match_string, match->rm_so); |
| 244 | break; |
| 245 | } |
| 246 | |
| 247 | string_remove_remainder(match_string, match->rm_so); |
| 248 | break; |
| 249 | case OP_INCLUDE: |
| 250 | if (enabled) { |
| 251 | string_remove_range(match_string, match->rm_so, match->rm_eo); |
| 252 | break; |
| 253 | } |
| 254 | |
| 255 | string_remove_line(beginning, match_string, match->rm_so); |
| 256 | break; |
| 257 | case OP_EXCLUDE: |
| 258 | if (!enabled) { |
| 259 | string_remove_range(match_string, match->rm_so, match->rm_eo); |
| 260 | break; |
| 261 | } |
| 262 | |
| 263 | string_remove_line(beginning, match_string, match->rm_so); |
| 264 | break; |
| 265 | case OP_IMPLY: |
| 266 | if (enabled) { |
| 267 | *features = string_array_add_value(*features, value, true); |
| 268 | if (*features == NULL) { |
| 269 | return ENOMEM; |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | string_remove_line(beginning, match_string, match->rm_so); |
| 274 | break; |
| 275 | case OP_IF: |
| 276 | replacement = enabled ? if_true : if_false; |
| 277 | string_replace_position(match_string, match->rm_so, match->rm_eo, |
| 278 | replacement); |
| 279 | break; |
| 280 | case OP_SENTINEL: |
| 281 | ERROR("Invalid operator!"); |
| 282 | return EINVAL; |
| 283 | } |
| 284 | |
| 285 | return EOK; |
| 286 | } |
| 287 | |
| 288 | /** |
| 289 | * Supported operators: |
| 290 | * |
| 291 | * {continue if "with-smartcard"} |
| 292 | * {stop if "with-smartcard"} |
| 293 | * {exclude if "with-smartcard"} |
| 294 | * {include if "with-smartcard"} |
| 295 | * {if "with-smartcard":true|false} |
| 296 | * {if "with-smartcard":true} |
| 297 | * {if not "with-smartcard":true} |
| 298 | * {if not "with-smartcard":true|false} |
| 299 | * |
| 300 | * Match groups for template regular expression are as follows: |
| 301 | * |
| 302 | * Match 0: {continue if "with-smartcard"} |
| 303 | * Match 1: continue if "with-smartcard" |
| 304 | * Match 2: continue if |
| 305 | * Match 3: "with-smartcard" |
| 306 | * |
| 307 | * Match 0: {stop if "with-smartcard"} |
| 308 | * Match 1: stop if "with-smartcard" |
| 309 | * Match 2: stop if |
| 310 | * Match 3: "with-smartcard" |
| 311 | * |
| 312 | * Match 0: {exclude if "with-smartcard"} |
| 313 | * Match 1: exclude if "with-smartcard" |
| 314 | * Match 2: exclude if |
| 315 | * Match 3: "with-smartcard" |
| 316 | * |
| 317 | * Match 0: {include if "with-smartcard"} |
| 318 | * Match 1: include if "with-smartcard" |
| 319 | * Match 2: include if |
| 320 | * Match 3: "with-smartcard" |
| 321 | * |
| 322 | * Match 0: {if "with-smartcard":true|false} |
| 323 | * Match 1: if "with-smartcard":true|false |
| 324 | * Match 4: if |
| 325 | * Match 5: "with-smartcard" |
| 326 | * Match 6: true |
| 327 | * Match 7: |false |
| 328 | * Match 8: false |
| 329 | * |
| 330 | * Match 0: {if "with-smartcard":true} |
| 331 | * Match 1: if "with-smartcard":true |
| 332 | * Match 4: if |
| 333 | * Match 5: "with-smartcard" |
| 334 | * Match 6: true |
| 335 | * |
| 336 | * Match 0: {if not "with-smartcard":true} |
| 337 | * Match 1: if not "with-smartcard":true |
| 338 | * Match 4: if |
| 339 | * Match 5: not "with-smartcard" |
| 340 | * Match 6: true |
| 341 | * |
| 342 | * Match 0: {if not "with-smartcard":true|false} |
| 343 | * Match 1: if not "with-smartcard":true|false |
| 344 | * Match 4: if |
| 345 | * Match 5: not "with-smartcard" |
| 346 | * Match 6: true |
| 347 | * Match 7: |false |
| 348 | * Match 8: false |
| 349 | * |
| 350 | * Match 0: {imply "with-smartcard" if "with-smartcard-required"} |
| 351 | * Match 1: imply "with-smartcard" if "with-smartcard-required" |
| 352 | * Match 9: imply |
| 353 | * Match 10: with-smartcard |
| 354 | * Match 11: "with-smartcard-required" |
| 355 | * |
| 356 | */ |
| 357 | static errno_t |
| 358 | template_process_matches(const char *match_string, |
| 359 | regmatch_t *m, |
| 360 | enum template_operator *_op, |
| 361 | char **_expression, |
| 362 | char **_if_true, |
| 363 | char **_if_false, |
| 364 | char **_value) |
| 365 | { |
| 366 | enum template_operator op; |
| 367 | char *if_false; |
| 368 | char *if_true; |
| 369 | char *expression; |
| 370 | char *value; |
| 371 | errno_t ret; |
| 372 | |
| 373 | template_debug_print_matches(match_string, m, RE_MATCHES); |
| 374 | |
| 375 | op = template_match_get_operator(match_string, &m[1]); |
| 376 | if (op == OP_SENTINEL) { |
| 377 | return EINVAL; |
| 378 | } |
| 379 | |
| 380 | ret = template_match_get_expression(match_string, op, m, &expression); |
| 381 | if (ret != EOK) { |
| 382 | return ret; |
| 383 | } |
| 384 | |
| 385 | ret = template_match_get_values(match_string, op, m, |
| 386 | &if_true, &if_false, &value); |
| 387 | if (ret != EOK) { |
| 388 | free(expression); |
| 389 | return ret; |
| 390 | } |
| 391 | |
| 392 | if (_op != NULL) { |
| 393 | *_op = op; |
| 394 | } |
| 395 | |
| 396 | if (_expression != NULL) { |
| 397 | *_expression = expression; |
| 398 | } else { |
| 399 | free(expression); |
| 400 | } |
| 401 | |
| 402 | if (_if_true != NULL) { |
| 403 | *_if_true = if_true; |
| 404 | } else { |
| 405 | free(if_true); |
| 406 | } |
| 407 | |
| 408 | if (_if_false != NULL) { |
| 409 | *_if_false = if_false; |
| 410 | } else { |
| 411 | free(if_false); |
| 412 | } |
| 413 | |
| 414 | if (_value != NULL) { |
| 415 | *_value = value; |
| 416 | } else { |
| 417 | free(value); |
| 418 | } |
| 419 | |
| 420 | return EOK; |
| 421 | } |
| 422 | |
| 423 | static errno_t |
| 424 | template_process_operators(const char **features, |
| 425 | char *content) |
| 426 | { |
| 427 | |
| 428 | regex_t regex; |
| 429 | char *match_string; |
| 430 | size_t orig_len; |
| 431 | regmatch_t m[RE_MATCHES]; |
| 432 | enum template_operator op; |
| 433 | char *if_false = NULL; |
| 434 | char *if_true = NULL; |
| 435 | char *expression = NULL; |
| 436 | char *value = NULL; |
| 437 | char **features_copy; |
| 438 | errno_t ret; |
| 439 | int reret; |
| 440 | |
| 441 | features_copy = string_array_copy((char**)features, true); |
| 442 | if (features_copy == NULL) { |
| 443 | return ENOMEM; |
| 444 | } |
| 445 | |
| 446 | orig_len = strlen(content); |
| 447 | |
| 448 | reret = regcomp(®ex, OP_RE, REG_EXTENDED | REG_NEWLINE); |
| 449 | if (reret != REG_NOERROR) { |
| 450 | ERROR("Unable to compile regular expression: regex error %d", reret); |
| 451 | string_array_free(features_copy); |
| 452 | return EFAULT; |
| 453 | } |
| 454 | |
| 455 | match_string = content; |
| 456 | while ((reret = regexec(®ex, match_string, RE_MATCHES, m, 0)) == REG_NOERROR) { |
| 457 | ret = template_process_matches(match_string, m, &op, &expression, |
| 458 | &if_true, &if_false, &value); |
| 459 | if (ret != EOK) { |
| 460 | ERROR("Unable to process match [%d]: %s", ret, strerror(ret)); |
| 461 | goto done; |
| 462 | } |
| 463 | |
| 464 | ret = template_match_replace(&features_copy, content, match_string, |
| 465 | &m[0], op, expression, |
| 466 | if_true, if_false, value); |
| 467 | |
| 468 | if (expression != NULL) { |
| 469 | free(expression); |
| 470 | } |
| 471 | |
| 472 | if (if_true != NULL) { |
| 473 | free(if_true); |
| 474 | } |
| 475 | |
| 476 | if (if_false != NULL) { |
| 477 | free(if_false); |
| 478 | } |
| 479 | |
| 480 | if (value != NULL) { |
| 481 | free(value); |
| 482 | } |
| 483 | |
| 484 | if (ret != EOK) { |
| 485 | ERROR("Unable to process operator [%d]: %s", ret, strerror(ret)); |
| 486 | goto done; |
| 487 | } |
| 488 | |
| 489 | /* Since the whole line could have been removed, we have to find first |
| 490 | * non-zero position. */ |
| 491 | match_string += m[0].rm_eo; |
| 492 | while (*match_string == '\0' && match_string - content < orig_len) { |
| 493 | match_string++; |
| 494 | } |
| 495 | } |
| 496 | |
| 497 | string_replace_shake(content, orig_len); |
| 498 | |
| 499 | if (reret != REG_NOMATCH) { |
| 500 | ERROR("Unable to search string: regex error %d", reret); |
| 501 | ret = EFAULT; |
| 502 | goto done; |
| 503 | } |
| 504 | |
| 505 | ret = EOK; |
| 506 | |
| 507 | done: |
| 508 | string_array_free(features_copy); |
| 509 | regfree(®ex); |
| 510 | return ret; |
| 511 | } |
| 512 | |
| 513 | char * |
| 514 | template_generate(const char *template, |
| 515 | const char **features) |
| 516 | { |
| 517 | char **lines; |
| 518 | char *output; |
| 519 | errno_t ret; |
| 520 | |
| 521 | if (template == NULL) { |
| 522 | return strdup(""); |
| 523 | } |
| 524 | |
| 525 | output = strdup(template); |
| 526 | if (output == NULL) { |
| 527 | return NULL; |
| 528 | } |
| 529 | |
| 530 | ret = template_process_operators(features, output); |
| 531 | if (ret != EOK) { |
| 532 | ERROR("Unable to generate template [%d]: %s", ret, strerror(ret)); |
| 533 | free(output); |
| 534 | return NULL; |
| 535 | } |
| 536 | |
| 537 | /* Trim lines */ |
| 538 | lines = string_explode(output, '\n', STRING_EXPLODE_TRIM_RIGHT); |
| 539 | free(output); |
| 540 | if (lines == NULL) { |
| 541 | return NULL; |
| 542 | } |
| 543 | |
| 544 | output = string_implode((const char **)lines, '\n'); |
| 545 | string_array_free(lines); |
| 546 | |
| 547 | return output; |
| 548 | } |
| 549 | |
| 550 | static char * |
| 551 | template_generate_preamble() |
| 552 | { |
| 553 | const char *preamble = |
| 554 | "# Generated by authselect\n" |
| 555 | "# Do not modify this file manually, use authselect instead. Any user changes will be overwritten.\n" |
| 556 | "# You can stop authselect from managing your configuration by calling 'authselect opt-out'.\n" |
| 557 | "# See authselect(8) for more details.\n\n"; |
| 558 | |
| 559 | return strdup(preamble); |
| 560 | } |
| 561 | |
| 562 | errno_t |
| 563 | template_list_features_from_expression(const char *expression, char ***features) |
| 564 | { |
| 565 | regmatch_t m[RE_MATCHES]; |
| 566 | const char *match_string; |
| 567 | regex_t regex; |
| 568 | errno_t ret; |
| 569 | int reret; |
| 570 | |
| 571 | reret = regcomp(®ex, "[^\"]{0,}" RE_FEATURE, REG_EXTENDED | REG_NEWLINE); |
| 572 | if (reret != REG_NOERROR) { |
| 573 | ERROR("Unable to compile regular expression: regex error %d", reret); |
| 574 | return EFAULT; |
| 575 | } |
| 576 | |
| 577 | match_string = expression; |
| 578 | while ((reret = regexec(®ex, match_string, RE_MATCHES, m, 0)) == REG_NOERROR) { |
| 579 | if (reret != EOK) { |
| 580 | ERROR("Unable to find new match: regex error %d", reret); |
| 581 | ret = EFAULT; |
| 582 | goto done; |
| 583 | } |
| 584 | *features = string_array_add_value_safe(*features, |
| 585 | &match_string[m[1].rm_so], |
| 586 | m[1].rm_eo - m[1].rm_so, |
| 587 | true); |
| 588 | if (*features == NULL) { |
| 589 | ret = ENOMEM; |
| 590 | goto done; |
| 591 | } |
| 592 | match_string += m[0].rm_eo; |
| 593 | } |
| 594 | |
| 595 | ret = EOK; |
| 596 | |
| 597 | done: |
| 598 | regfree(®ex); |
| 599 | return ret; |
| 600 | } |
| 601 | |
| 602 | char ** |
| 603 | template_list_features(const char *template) |
| 604 | { |
| 605 | regmatch_t m[RE_MATCHES]; |
| 606 | const char *match_string; |
| 607 | char **features; |
| 608 | char *expression; |
| 609 | regex_t regex; |
| 610 | errno_t ret; |
| 611 | int reret; |
| 612 | |
| 613 | features = string_array_create(10); |
| 614 | if (features == NULL) { |
| 615 | return NULL; |
| 616 | } |
| 617 | |
| 618 | if (template == NULL) { |
| 619 | return features; |
| 620 | } |
| 621 | |
| 622 | reret = regcomp(®ex, OP_RE, REG_EXTENDED | REG_NEWLINE); |
| 623 | if (reret != REG_NOERROR) { |
| 624 | ERROR("Unable to compile regular expression: regex error %d", reret); |
| 625 | ret = EFAULT; |
| 626 | goto done; |
| 627 | } |
| 628 | |
| 629 | match_string = template; |
| 630 | while ((reret = regexec(®ex, match_string, RE_MATCHES, m, 0)) == REG_NOERROR) { |
| 631 | ret = template_process_matches(match_string, m, NULL, &expression, |
| 632 | NULL, NULL, NULL); |
| 633 | if (ret != EOK) { |
| 634 | ERROR("Unable to process match [%d]: %s", ret, strerror(ret)); |
| 635 | goto done; |
| 636 | } |
| 637 | |
| 638 | if (expression) { |
| 639 | ret = template_list_features_from_expression(expression, &features); |
| 640 | free(expression); |
| 641 | if (ret != EOK) { |
| 642 | goto done; |
| 643 | } |
| 644 | } |
| 645 | |
| 646 | match_string += m[0].rm_eo; |
| 647 | } |
| 648 | |
| 649 | if (reret != REG_NOMATCH) { |
| 650 | ERROR("Unable to search string: regex error %d", reret); |
| 651 | ret = EFAULT; |
| 652 | goto done; |
| 653 | } |
| 654 | |
| 655 | ret = EOK; |
| 656 | |
| 657 | done: |
| 658 | if (ret != EOK) { |
| 659 | string_array_free(features); |
| 660 | return NULL; |
| 661 | } |
| 662 | |
| 663 | regfree(®ex); |
| 664 | return features; |
| 665 | } |
| 666 | |
| 667 | errno_t |
| 668 | template_write(const char *filepath, |
| 669 | const char *content, |
| 670 | mode_t mode) |
| 671 | { |
| 672 | char *preamble; |
| 673 | char *output; |
| 674 | errno_t ret; |
| 675 | |
| 676 | preamble = template_generate_preamble(); |
| 677 | if (content == NULL) { |
| 678 | output = preamble; |
| 679 | } else { |
| 680 | output = format("%s%s", preamble, content); |
| 681 | free(preamble); |
| 682 | } |
| 683 | |
| 684 | if (output == NULL) { |
| 685 | return ENOMEM; |
| 686 | } |
| 687 | |
| 688 | ret = textfile_write(filepath, output, mode); |
| 689 | free(output); |
| 690 | |
| 691 | return ret; |
| 692 | } |
| 693 | |
| 694 | errno_t |
| 695 | template_write_temporary(const char *filepath, |
| 696 | const char *content, |
| 697 | mode_t mode, |
| 698 | char **_tmpfile) |
| 699 | { |
| 700 | char *tmpfile; |
| 701 | errno_t ret; |
| 702 | |
| 703 | ret = selinux_mkstemp_for(filepath, mode, &tmpfile); |
| 704 | if (ret != EOK) { |
| 705 | ERROR("Unable to create temporary file for [%s] [%d]: %s", |
| 706 | filepath, ret, strerror(ret)); |
| 707 | return ret; |
| 708 | } |
| 709 | |
| 710 | ret = template_write(tmpfile, content, mode); |
| 711 | if (ret != EOK) { |
| 712 | free(tmpfile); |
| 713 | return ret; |
| 714 | } |
| 715 | |
| 716 | *_tmpfile = tmpfile; |
| 717 | |
| 718 | return EOK; |
| 719 | } |
| 720 | |
| 721 | bool |
| 722 | template_validate_written_content(const char *file_content, |
| 723 | const char *expected) |
| 724 | { |
| 725 | char *processed_expected = NULL; |
| 726 | char *processed_content = NULL; |
| 727 | char **lines_expected = NULL; |
| 728 | char **lines_content = NULL; |
| 729 | bool result = false; |
| 730 | |
| 731 | /* We ignore changes in comments, empty lines and surrounding spaces since |
| 732 | * they do not affect the resulting configuration. */ |
| 733 | |
| 734 | lines_content = string_explode(file_content, '\n', STRING_EXPLODE_ALL); |
| 735 | if (lines_content == NULL) { |
| 736 | goto done; |
| 737 | } |
| 738 | |
| 739 | processed_content = string_implode((const char **)lines_content, '\n'); |
| 740 | if (processed_content == NULL) { |
| 741 | goto done; |
| 742 | } |
| 743 | |
| 744 | lines_expected = string_explode(expected, '\n', STRING_EXPLODE_ALL); |
| 745 | if (lines_expected == NULL) { |
| 746 | goto done; |
| 747 | } |
| 748 | |
| 749 | processed_expected = string_implode((const char **)lines_expected, '\n'); |
| 750 | if (processed_expected == NULL) { |
| 751 | goto done; |
| 752 | } |
| 753 | |
| 754 | result = strcmp(processed_content, processed_expected) == 0; |
| 755 | |
| 756 | done: |
| 757 | string_array_free(lines_expected); |
| 758 | string_array_free(lines_content); |
| 759 | free(processed_expected); |
| 760 | free(processed_content); |
| 761 | |
| 762 | return result; |
| 763 | } |