| 1 | /* |
| 2 | Authors: |
| 3 | Pavel Březina <[email protected]> |
| 4 | |
| 5 | Copyright (C) 2024 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 <errno.h> |
| 22 | #include <string.h> |
| 23 | #include <stdlib.h> |
| 24 | |
| 25 | #include "common/common.h" |
| 26 | #include "lib/constants.h" |
| 27 | #include "lib/util/util.h" |
| 28 | #include "lib/util/config.h" |
| 29 | |
| 30 | static char * |
| 31 | config_read_profile_id(char **config) |
| 32 | { |
| 33 | return strdup(config[0]); |
| 34 | } |
| 35 | |
| 36 | static char ** |
| 37 | config_read_features(char **config) |
| 38 | { |
| 39 | char **features; |
| 40 | int i; |
| 41 | |
| 42 | features = string_array_create(0); |
| 43 | if (features == NULL) { |
| 44 | return NULL; |
| 45 | } |
| 46 | |
| 47 | /* Skip profile name. */ |
| 48 | for (i = 1; config[i] != NULL; i++) { |
| 49 | features = string_array_add_value(features, config[i], true); |
| 50 | if (features == NULL) { |
| 51 | return NULL; |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | return features; |
| 56 | } |
| 57 | |
| 58 | errno_t |
| 59 | config_read(const char *path, |
| 60 | char **_profile_id, |
| 61 | char ***_features) |
| 62 | { |
| 63 | char *profile_id; |
| 64 | char **features; |
| 65 | char *content; |
| 66 | char **lines; |
| 67 | errno_t ret; |
| 68 | |
| 69 | ret = textfile_read(path, AUTHSELECT_FILE_SIZE_LIMIT, &content); |
| 70 | if (ret != EOK) { |
| 71 | return ret; |
| 72 | } |
| 73 | |
| 74 | lines = string_explode(content, '\n', STRING_EXPLODE_ALL); |
| 75 | free(content); |
| 76 | if (lines == NULL) { |
| 77 | return ENOMEM; |
| 78 | } |
| 79 | |
| 80 | if (lines[0] == NULL) { |
| 81 | ret = ENOENT; |
| 82 | goto done; |
| 83 | } |
| 84 | |
| 85 | profile_id = config_read_profile_id(lines); |
| 86 | if (profile_id == NULL) { |
| 87 | ret = ENOMEM; |
| 88 | goto done; |
| 89 | } |
| 90 | |
| 91 | if (_features != NULL) { |
| 92 | features = config_read_features(lines); |
| 93 | if (features == NULL) { |
| 94 | free(profile_id); |
| 95 | ret = ENOMEM; |
| 96 | goto done; |
| 97 | } |
| 98 | |
| 99 | *_features = features; |
| 100 | } |
| 101 | |
| 102 | *_profile_id = profile_id; |
| 103 | |
| 104 | ret = EOK; |
| 105 | |
| 106 | done: |
| 107 | string_array_free(lines); |
| 108 | |
| 109 | return ret; |
| 110 | } |