| 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 <errno.h> |
| 22 | #include <fcntl.h> |
| 23 | #include <string.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <unistd.h> |
| 26 | |
| 27 | #include "common/common.h" |
| 28 | #include "lib/constants.h" |
| 29 | #include "lib/profiles/profiles.h" |
| 30 | #include "lib/files/files.h" |
| 31 | #include "lib/util/util.h" |
| 32 | |
| 33 | static const char ** |
| 34 | authselect_profile_locations(const char *id, |
| 35 | enum authselect_profile_type type) |
| 36 | { |
| 37 | static const char *type_default[] = {DIR_DEFAULT_PROFILES, NULL}; |
| 38 | static const char *type_vendor[] = {DIR_VENDOR_PROFILES, NULL}; |
| 39 | static const char *type_custom[] = {DIR_CUSTOM_PROFILES, NULL}; |
| 40 | static const char *type_any[] = { |
| 41 | DIR_VENDOR_PROFILES, |
| 42 | DIR_DEFAULT_PROFILES, |
| 43 | NULL |
| 44 | }; |
| 45 | |
| 46 | switch (type) { |
| 47 | case AUTHSELECT_PROFILE_DEFAULT: |
| 48 | return type_default; |
| 49 | case AUTHSELECT_PROFILE_VENDOR: |
| 50 | return type_vendor; |
| 51 | case AUTHSELECT_PROFILE_CUSTOM: |
| 52 | return type_custom; |
| 53 | case AUTHSELECT_PROFILE_ANY: |
| 54 | if (authselect_profile_is_custom(id)) { |
| 55 | return type_custom; |
| 56 | } |
| 57 | |
| 58 | return type_any; |
| 59 | } |
| 60 | |
| 61 | return NULL; |
| 62 | } |
| 63 | |
| 64 | static errno_t |
| 65 | authselect_profile_open_location(const char *path, |
| 66 | int *_dirfd) |
| 67 | { |
| 68 | int dirfd; |
| 69 | errno_t ret; |
| 70 | |
| 71 | dirfd = open(path, O_DIRECTORY | O_RDONLY); |
| 72 | if (dirfd == -1) { |
| 73 | ret = errno; |
| 74 | /* To silence static analyzers that assumes that errno can be 0 here. */ |
| 75 | if (ret == EOK) { |
| 76 | ret = EINVAL; |
| 77 | } |
| 78 | |
| 79 | if (ret == ENOENT) { |
| 80 | return ENOENT; |
| 81 | } |
| 82 | |
| 83 | ERROR("Unable to open directory [%s] [%d]: %s", |
| 84 | path, ret, strerror(ret)); |
| 85 | return ret; |
| 86 | } |
| 87 | |
| 88 | *_dirfd = dirfd; |
| 89 | |
| 90 | return EOK; |
| 91 | } |
| 92 | |
| 93 | static errno_t |
| 94 | authselect_profile_open(const char *id, |
| 95 | enum authselect_profile_type type, |
| 96 | char **_location, |
| 97 | int *_dirfd) |
| 98 | { |
| 99 | const char **locations; |
| 100 | const char *name; |
| 101 | char *location; |
| 102 | errno_t ret; |
| 103 | int dirfd; |
| 104 | int i; |
| 105 | |
| 106 | INFO("Looking up profile [%s]", id); |
| 107 | |
| 108 | locations = authselect_profile_locations(id, type); |
| 109 | if (locations == NULL) { |
| 110 | ERROR("Locations array is NULL"); |
| 111 | return EINVAL; |
| 112 | } |
| 113 | |
| 114 | name = authselect_profile_parse_custom(id); |
| 115 | name = name == NULL ? id : name; |
| 116 | |
| 117 | for (i = 0; locations[i] != NULL; i++) { |
| 118 | location = format("%s/%s", locations[i], name); |
| 119 | if (location == NULL) { |
| 120 | return ENOMEM; |
| 121 | } |
| 122 | |
| 123 | ret = authselect_profile_open_location(location, &dirfd); |
| 124 | if (ret == ENOENT) { |
| 125 | free(location); |
| 126 | continue; |
| 127 | } else if (ret != EOK) { |
| 128 | free(location); |
| 129 | return ret; |
| 130 | } |
| 131 | |
| 132 | if (strcmp(locations[i], DIR_CUSTOM_PROFILES) == 0) { |
| 133 | INFO("Profile [%s] is a custom profile", id); |
| 134 | } else if (strcmp(locations[i], DIR_VENDOR_PROFILES) == 0) { |
| 135 | INFO("Profile [%s] is a vendor profile", id); |
| 136 | } else { |
| 137 | INFO("Profile [%s] is a default profile", id); |
| 138 | } |
| 139 | |
| 140 | INFO("Profile [%s] found at [%s]", id, location); |
| 141 | |
| 142 | *_location = location; |
| 143 | *_dirfd = dirfd; |
| 144 | |
| 145 | return EOK; |
| 146 | } |
| 147 | |
| 148 | INFO("Profile [%s] was not found", id); |
| 149 | |
| 150 | return ENOENT; |
| 151 | } |
| 152 | |
| 153 | static errno_t |
| 154 | authselect_profile_read_meta(const char *location, |
| 155 | int dirfd, |
| 156 | const char *filename, |
| 157 | bool mandatory, |
| 158 | char **_content) |
| 159 | { |
| 160 | errno_t ret; |
| 161 | |
| 162 | INFO("Reading file [%s/%s]", location, filename); |
| 163 | |
| 164 | ret = textfile_read_dirfd(dirfd, location, filename, |
| 165 | AUTHSELECT_FILE_SIZE_LIMIT, _content); |
| 166 | if (ret != EOK) { |
| 167 | if (mandatory) { |
| 168 | ERROR("Unable to read file [%s/%s] [%d]: %s", |
| 169 | location, filename, ret, strerror(ret)); |
| 170 | return ret; |
| 171 | } |
| 172 | |
| 173 | WARN("Unable to read file [%s/%s] [%d]: %s", |
| 174 | location, filename, ret, strerror(ret)); |
| 175 | |
| 176 | *_content = NULL; |
| 177 | return EOK; |
| 178 | } |
| 179 | |
| 180 | return EOK; |
| 181 | } |
| 182 | |
| 183 | static errno_t |
| 184 | authselect_profile_read_readme(const char *location, |
| 185 | int dirfd, |
| 186 | char **_name, |
| 187 | char **_description) |
| 188 | { |
| 189 | size_t lineend; |
| 190 | char *trimmed = NULL; |
| 191 | char *readme = NULL; |
| 192 | char *name = NULL; |
| 193 | errno_t ret; |
| 194 | |
| 195 | ret = authselect_profile_read_meta(location, dirfd, FILE_README, true, |
| 196 | &readme); |
| 197 | if (ret != EOK) { |
| 198 | return ret; |
| 199 | } |
| 200 | |
| 201 | lineend = strcspn(readme, "\r\n"); |
| 202 | if (lineend <= 0) { |
| 203 | ERROR("Profile [%s] does not contain a name in [%s]!", |
| 204 | location, FILE_README); |
| 205 | ret = EINVAL; |
| 206 | goto done; |
| 207 | } |
| 208 | |
| 209 | name = strndup(readme, lineend); |
| 210 | if (name == NULL) { |
| 211 | ret = ENOMEM; |
| 212 | goto done; |
| 213 | } |
| 214 | |
| 215 | trimmed = string_trim(name); |
| 216 | if (trimmed == NULL) { |
| 217 | ret = ENOMEM; |
| 218 | goto done; |
| 219 | } |
| 220 | |
| 221 | if (string_is_empty(trimmed)) { |
| 222 | ERROR("Profile [%s] does not contain a name in [%s]!", |
| 223 | location, FILE_README); |
| 224 | ret = EINVAL; |
| 225 | goto done; |
| 226 | } |
| 227 | |
| 228 | *_description = readme; |
| 229 | *_name = trimmed; |
| 230 | |
| 231 | ret = EOK; |
| 232 | |
| 233 | done: |
| 234 | if (name != NULL) { |
| 235 | free(name); |
| 236 | } |
| 237 | |
| 238 | if (ret != EOK) { |
| 239 | free(readme); |
| 240 | |
| 241 | if (trimmed != NULL) { |
| 242 | free(trimmed); |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | return ret; |
| 247 | } |
| 248 | |
| 249 | static struct authselect_profile * |
| 250 | authselect_profile_init(const char *id) |
| 251 | { |
| 252 | struct authselect_profile *profile; |
| 253 | |
| 254 | profile = malloc_zero(struct authselect_profile); |
| 255 | if (profile == NULL) { |
| 256 | return NULL; |
| 257 | } |
| 258 | |
| 259 | profile->id = strdup(id); |
| 260 | if (profile->id == NULL) { |
| 261 | free(profile); |
| 262 | return NULL; |
| 263 | } |
| 264 | |
| 265 | return profile; |
| 266 | } |
| 267 | |
| 268 | errno_t |
| 269 | authselect_profile_read(const char *profile_id, |
| 270 | enum authselect_profile_type type, |
| 271 | struct authselect_profile **_profile) |
| 272 | { |
| 273 | struct authselect_profile *profile = NULL; |
| 274 | char *location; |
| 275 | int dirfd; |
| 276 | errno_t ret; |
| 277 | |
| 278 | ret = authselect_profile_open(profile_id, type, &location, &dirfd); |
| 279 | if (ret != EOK) { |
| 280 | return ret; |
| 281 | } |
| 282 | |
| 283 | profile = authselect_profile_init(profile_id); |
| 284 | if (profile == NULL) { |
| 285 | ret = ENOMEM; |
| 286 | goto done; |
| 287 | } |
| 288 | |
| 289 | profile->path = location; |
| 290 | |
| 291 | ret = authselect_profile_read_readme(location, dirfd, &profile->name, |
| 292 | &profile->description); |
| 293 | if (ret != EOK) { |
| 294 | goto done; |
| 295 | } |
| 296 | |
| 297 | ret = authselect_profile_read_meta(location, dirfd, FILE_REQUIREMENT, |
| 298 | false, &profile->requirements); |
| 299 | if (ret != EOK) { |
| 300 | goto done; |
| 301 | } |
| 302 | |
| 303 | ret = authselect_system_read_templates(location, dirfd, &profile->files); |
| 304 | if (ret != EOK) { |
| 305 | goto done; |
| 306 | } |
| 307 | |
| 308 | *_profile = profile; |
| 309 | |
| 310 | ret = EOK; |
| 311 | |
| 312 | done: |
| 313 | close(dirfd); |
| 314 | |
| 315 | if (ret != EOK) { |
| 316 | ERROR("Unable to find profile [%s] [%d]: %s", |
| 317 | profile_id, ret, strerror(ret)); |
| 318 | authselect_profile_free(profile); |
| 319 | } |
| 320 | |
| 321 | return ret; |
| 322 | } |