| 1 | /* |
| 2 | Authors: |
| 3 | Pavel Březina <[email protected]> |
| 4 | |
| 5 | Copyright (C) 2017 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 <stddef.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <stdbool.h> |
| 24 | |
| 25 | #include "authselect.h" |
| 26 | |
| 27 | #include "lib/constants.h" |
| 28 | #include "lib/util/util.h" |
| 29 | #include "lib/files/files.h" |
| 30 | #include "lib/profiles/profiles.h" |
| 31 | |
| 32 | static bool |
| 33 | authselect_check_features(const struct authselect_profile *profile, |
| 34 | const char **features) |
| 35 | { |
| 36 | const char *similar; |
| 37 | bool result = true; |
| 38 | char **supported; |
| 39 | int i; |
| 40 | |
| 41 | if (features == NULL) { |
| 42 | return true; |
| 43 | } |
| 44 | |
| 45 | supported = authselect_profile_features(profile); |
| 46 | if (supported == NULL) { |
| 47 | ERROR("Unable to obtain supported features"); |
| 48 | return false; |
| 49 | } |
| 50 | |
| 51 | for (i = 0; features[i] != NULL; i++) { |
| 52 | if (string_array_has_value(supported, features[i])) { |
| 53 | continue; |
| 54 | } |
| 55 | |
| 56 | result = false; |
| 57 | similar = string_array_find_similar(features[i], supported, 5); |
| 58 | if (similar != NULL) { |
| 59 | ERROR("Unknown profile feature [%s], did you mean [%s]?", |
| 60 | features[i], similar); |
| 61 | } else { |
| 62 | ERROR("Unknown profile feature [%s]", features[i]); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | string_array_free(supported); |
| 67 | |
| 68 | return result; |
| 69 | } |
| 70 | |
| 71 | _PUBLIC_ void |
| 72 | authselect_set_debug_fn(authselect_debug_fn fn, void *pvt) |
| 73 | { |
| 74 | set_debug_fn(fn, pvt); |
| 75 | } |
| 76 | |
| 77 | _PUBLIC_ int |
| 78 | authselect_activate(const char *profile_id, |
| 79 | const char **features, |
| 80 | bool force_overwrite) |
| 81 | { |
| 82 | struct authselect_profile *profile; |
| 83 | bool is_valid; |
| 84 | errno_t ret; |
| 85 | |
| 86 | INFO("Trying to activate profile [%s]", profile_id); |
| 87 | |
| 88 | ret = authselect_profile(profile_id, &profile); |
| 89 | if (ret != EOK) { |
| 90 | ERROR("Unable to find profile [%s] [%d]: %s", |
| 91 | profile_id, ret, strerror(ret)); |
| 92 | return ret; |
| 93 | } |
| 94 | |
| 95 | if (!authselect_check_features(profile, features)) { |
| 96 | ret = EINVAL; |
| 97 | goto done; |
| 98 | } |
| 99 | |
| 100 | if (force_overwrite) { |
| 101 | INFO("Enforcing activation!"); |
| 102 | ret = authselect_profile_activate(profile, features); |
| 103 | goto done; |
| 104 | } |
| 105 | |
| 106 | /* Require force if authselect.conf is missing or invalid but otherwise |
| 107 | * ignore user changes. */ |
| 108 | ret = authselect_validate_configuration(&is_valid); |
| 109 | if (ret != EOK) { |
| 110 | ERROR("%s is missing or unreadable, system was not properly configured " |
| 111 | "by authselect.", PATH_CONFIG_FILE); |
| 112 | ERROR("Refusing to activate profile unless overwrite is requested."); |
| 113 | ret = EEXIST; |
| 114 | goto done; |
| 115 | } |
| 116 | |
| 117 | if (!is_valid) { |
| 118 | ERROR("Changes to the authselect configuration were detected. " |
| 119 | "These changes will be overwritten. Please call " |
| 120 | "'authselect opt-out' in order to keep them."); |
| 121 | } |
| 122 | |
| 123 | ret = authselect_profile_activate(profile, features); |
| 124 | |
| 125 | done: |
| 126 | if (ret != EOK && ret != EEXIST) { |
| 127 | ERROR("Unable to activate profile [%s] [%d]: %s", |
| 128 | profile_id, ret, strerror(ret)); |
| 129 | } |
| 130 | |
| 131 | authselect_profile_free(profile); |
| 132 | |
| 133 | return ret; |
| 134 | } |
| 135 | |
| 136 | _PUBLIC_ int |
| 137 | authselect_uninstall(void) |
| 138 | { |
| 139 | errno_t ret; |
| 140 | |
| 141 | INFO("Trying to uninstall authselect configuration"); |
| 142 | |
| 143 | ret = authselect_symlinks_uninstall(); |
| 144 | if (ret != EOK) { |
| 145 | ERROR("Unable to remove symlinks [%d]: %s", ret, strerror(ret)); |
| 146 | return ret; |
| 147 | } |
| 148 | |
| 149 | INFO("Symbolic links were successfully removed"); |
| 150 | |
| 151 | /* Remove files from /etc/authselect */ |
| 152 | ret = authselect_files_uninstall(); |
| 153 | if (ret != EOK) { |
| 154 | ERROR("Unable to remove authselect configuration [%d]: %s", |
| 155 | ret, strerror(ret)); |
| 156 | return ret; |
| 157 | } |
| 158 | |
| 159 | INFO("Authselect configuration was successfully removed"); |
| 160 | |
| 161 | return EOK; |
| 162 | } |
| 163 | |
| 164 | _PUBLIC_ int |
| 165 | authselect_apply_changes(bool upgrade) |
| 166 | { |
| 167 | struct authselect_profile *profile = NULL; |
| 168 | char **supported = NULL; |
| 169 | char *profile_id; |
| 170 | char **features; |
| 171 | char *checksum_orig = NULL; |
| 172 | char *checksum_copy = NULL; |
| 173 | errno_t ret; |
| 174 | int i; |
| 175 | |
| 176 | ret = authselect_current_configuration(&profile_id, &features); |
| 177 | if (ret != EOK) { |
| 178 | return ret; |
| 179 | } |
| 180 | |
| 181 | if (upgrade) { |
| 182 | ret = textfile_read(PATH_CHECKSUM_ORIG, AUTHSELECT_FILE_SIZE_LIMIT, |
| 183 | &checksum_orig); |
| 184 | if (ret != EOK) { |
| 185 | ERROR("Unable to read original checksum [%s] [%d]: %s", |
| 186 | PATH_CHECKSUM_ORIG, ret, strerror(ret)); |
| 187 | goto done; |
| 188 | } |
| 189 | |
| 190 | ret = textfile_read(PATH_CHECKSUM_COPY, AUTHSELECT_FILE_SIZE_LIMIT, |
| 191 | &checksum_copy); |
| 192 | if (ret != EOK && ret != ENOENT) { |
| 193 | ERROR("Unable to read copy checksum [%s] [%d]: %s", |
| 194 | PATH_CHECKSUM_COPY, ret, strerror(ret)); |
| 195 | goto done; |
| 196 | } else if (ret == EOK) { |
| 197 | if (strcmp(checksum_orig, checksum_copy) == 0) { |
| 198 | INFO("Installed profiles did not change. No action is needed."); |
| 199 | ret = EAGAIN; |
| 200 | goto done; |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | /* Copy checksum file does not exist or profiles did changed. Update. */ |
| 205 | } |
| 206 | |
| 207 | ret = authselect_profile(profile_id, &profile); |
| 208 | if (ret != EOK) { |
| 209 | ERROR("Unable to find profile [%s] [%d]: %s", |
| 210 | profile_id, ret, strerror(ret)); |
| 211 | goto done; |
| 212 | } |
| 213 | |
| 214 | supported = authselect_profile_features(profile); |
| 215 | if (supported == NULL) { |
| 216 | ERROR("Unable to obtain supported features"); |
| 217 | ret = ENOMEM; |
| 218 | goto done; |
| 219 | } |
| 220 | |
| 221 | for (i = 0; features[i] != NULL; i++) { |
| 222 | if (string_array_has_value(supported, features[i])) { |
| 223 | continue; |
| 224 | } |
| 225 | |
| 226 | WARN("Profile feature [%s] is no longer supported, removing it...", |
| 227 | features[i]); |
| 228 | |
| 229 | string_array_del_value(features, features[i]); |
| 230 | i--; |
| 231 | } |
| 232 | |
| 233 | ret = authselect_activate(profile_id, (const char **)features, false); |
| 234 | |
| 235 | done: |
| 236 | authselect_profile_free(profile); |
| 237 | string_array_free(supported); |
| 238 | string_array_free(features); |
| 239 | free(checksum_orig); |
| 240 | free(checksum_copy); |
| 241 | free(profile_id); |
| 242 | |
| 243 | return ret; |
| 244 | } |
| 245 | |
| 246 | _PUBLIC_ int |
| 247 | authselect_feature_enable(const char *feature) |
| 248 | { |
| 249 | char *profile_id; |
| 250 | char **features; |
| 251 | errno_t ret; |
| 252 | |
| 253 | ret = authselect_current_configuration(&profile_id, &features); |
| 254 | if (ret != EOK) { |
| 255 | return ret; |
| 256 | } |
| 257 | |
| 258 | features = string_array_add_value(features, feature, true); |
| 259 | if (features == NULL) { |
| 260 | ret = ENOMEM; |
| 261 | goto done; |
| 262 | } |
| 263 | |
| 264 | ret = authselect_activate(profile_id, (const char **)features, false); |
| 265 | |
| 266 | done: |
| 267 | string_array_free(features); |
| 268 | free(profile_id); |
| 269 | |
| 270 | return ret; |
| 271 | } |
| 272 | |
| 273 | _PUBLIC_ int |
| 274 | authselect_feature_disable(const char *feature) |
| 275 | { |
| 276 | char *profile_id; |
| 277 | char **features; |
| 278 | errno_t ret; |
| 279 | |
| 280 | ret = authselect_config_read(&profile_id, &features); |
| 281 | if (ret != EOK) { |
| 282 | return ret; |
| 283 | } |
| 284 | |
| 285 | string_array_del_value(features, feature); |
| 286 | |
| 287 | ret = authselect_activate(profile_id, (const char **)features, false); |
| 288 | |
| 289 | string_array_free(features); |
| 290 | free(profile_id); |
| 291 | |
| 292 | return ret; |
| 293 | } |
| 294 | |
| 295 | _PUBLIC_ int |
| 296 | authselect_feature_enabled(const char *feature) |
| 297 | { |
| 298 | char *profile_id; |
| 299 | char **features; |
| 300 | errno_t ret; |
| 301 | bool bret; |
| 302 | |
| 303 | ret = authselect_config_read(&profile_id, &features); |
| 304 | if (ret != EOK) { |
| 305 | return ret; |
| 306 | } |
| 307 | |
| 308 | bret = string_array_has_value(features, feature); |
| 309 | |
| 310 | string_array_free(features); |
| 311 | free(profile_id); |
| 312 | |
| 313 | ret = bret ? EOK : ENOENT; |
| 314 | |
| 315 | return ret; |
| 316 | } |
| 317 | |
| 318 | _PUBLIC_ int |
| 319 | authselect_validate_configuration(bool *_is_valid) |
| 320 | { |
| 321 | char *profile_id; |
| 322 | char **features; |
| 323 | errno_t ret; |
| 324 | |
| 325 | ret = authselect_config_read(&profile_id, &features); |
| 326 | if (ret == ENOENT) { |
| 327 | *_is_valid = authselect_config_validate_user(); |
| 328 | |
| 329 | if (*_is_valid && authselect_config_validate_missing()) { |
| 330 | return ENOENT; |
| 331 | } |
| 332 | |
| 333 | return EEXIST; |
| 334 | } if (ret != EOK) { |
| 335 | return ret; |
| 336 | } |
| 337 | |
| 338 | *_is_valid = authselect_config_validate_authselect(profile_id, |
| 339 | (const char **)features); |
| 340 | |
| 341 | free(profile_id); |
| 342 | string_array_free(features); |
| 343 | |
| 344 | return EOK; |
| 345 | } |
| 346 | |
| 347 | _PUBLIC_ int |
| 348 | authselect_current_configuration(char **_profile_id, |
| 349 | char ***_features) |
| 350 | { |
| 351 | return authselect_config_read(_profile_id, _features); |
| 352 | } |
| 353 | |
| 354 | _PUBLIC_ char ** |
| 355 | authselect_list() |
| 356 | { |
| 357 | char **profiles; |
| 358 | errno_t ret; |
| 359 | |
| 360 | ret = authselect_profile_list(&profiles); |
| 361 | if (ret != EOK) { |
| 362 | return NULL; |
| 363 | } |
| 364 | |
| 365 | return profiles; |
| 366 | } |
| 367 | |
| 368 | _PUBLIC_ void |
| 369 | authselect_array_free(char **array) |
| 370 | { |
| 371 | string_array_free(array); |
| 372 | } |