| 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 <stdio.h> |
| 23 | #include <regex.h> |
| 24 | #include <string.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <unistd.h> |
| 27 | |
| 28 | #include "common/common.h" |
| 29 | #include "lib/constants.h" |
| 30 | #include "lib/util/util.h" |
| 31 | #include "lib/files/files.h" |
| 32 | |
| 33 | #define RE_NSS "^\\s*([^[:space:]:]+):.*$" |
| 34 | #define RE_NSS_MATCHES 2 |
| 35 | |
| 36 | struct authselect_system_paths { |
| 37 | const char *path; |
| 38 | char **content; |
| 39 | }; |
| 40 | |
| 41 | struct authselect_system_templates { |
| 42 | const char *template; |
| 43 | char **generated; |
| 44 | }; |
| 45 | |
| 46 | errno_t |
| 47 | authselect_system_read_templates(const char *dirname, |
| 48 | int dirfd, |
| 49 | struct authselect_files **_templates) |
| 50 | { |
| 51 | struct authselect_files *templates; |
| 52 | errno_t ret; |
| 53 | int i; |
| 54 | |
| 55 | templates = malloc_zero(struct authselect_files); |
| 56 | if (templates == NULL) { |
| 57 | return ENOMEM; |
| 58 | } |
| 59 | |
| 60 | struct authselect_system_paths paths[] = { |
| 61 | {FILE_SYSTEM, &templates->systemauth}, |
| 62 | {FILE_PASSWORD, &templates->passwordauth}, |
| 63 | {FILE_SMARTCARD, &templates->smartcardauth}, |
| 64 | {FILE_SWITCHABLE, &templates->switchableauth}, |
| 65 | {FILE_FINGERPRINT, &templates->fingerprintauth}, |
| 66 | {FILE_POSTLOGIN, &templates->postlogin}, |
| 67 | {FILE_NSSWITCH, &templates->nsswitch}, |
| 68 | {FILE_DCONF_DB, &templates->dconfdb}, |
| 69 | {FILE_DCONF_LOCK, &templates->dconflock}, |
| 70 | {NULL, NULL}, |
| 71 | }; |
| 72 | |
| 73 | for (i = 0; paths[i].path != NULL; i++) { |
| 74 | INFO("Reading file [%s/%s]", dirname, paths[i].path); |
| 75 | |
| 76 | ret = textfile_read_dirfd(dirfd, dirname, paths[i].path, |
| 77 | AUTHSELECT_FILE_SIZE_LIMIT, |
| 78 | paths[i].content); |
| 79 | if (ret == ENOENT) { |
| 80 | *paths[i].content = NULL; |
| 81 | } else if (ret != EOK) { |
| 82 | ERROR("Unable to read file [%s/%s] [%d]: %s", |
| 83 | dirname, paths[i].path, ret, strerror(ret)); |
| 84 | authselect_files_free(templates); |
| 85 | return ret; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | *_templates = templates; |
| 90 | |
| 91 | return EOK; |
| 92 | } |
| 93 | |
| 94 | errno_t |
| 95 | authselect_system_generate(const char **features, |
| 96 | struct authselect_files *templates, |
| 97 | struct authselect_files **_files) |
| 98 | { |
| 99 | struct authselect_files *files; |
| 100 | errno_t ret; |
| 101 | int i; |
| 102 | |
| 103 | if (templates == NULL) { |
| 104 | return EINVAL; |
| 105 | } |
| 106 | |
| 107 | files = malloc_zero(struct authselect_files); |
| 108 | if (files == NULL) { |
| 109 | return ENOMEM; |
| 110 | } |
| 111 | |
| 112 | struct authselect_system_templates tpls[] = { |
| 113 | {templates->systemauth, &files->systemauth}, |
| 114 | {templates->passwordauth, &files->passwordauth}, |
| 115 | {templates->smartcardauth, &files->smartcardauth}, |
| 116 | {templates->switchableauth, &files->switchableauth}, |
| 117 | {templates->fingerprintauth, &files->fingerprintauth}, |
| 118 | {templates->postlogin, &files->postlogin}, |
| 119 | {templates->dconfdb, &files->dconfdb}, |
| 120 | {templates->dconflock, &files->dconflock}, |
| 121 | {NULL, NULL}, |
| 122 | }; |
| 123 | |
| 124 | /* Template may be NULL so we must compare against destination. */ |
| 125 | for (i = 0; tpls[i].generated != NULL; i++) { |
| 126 | if (tpls[i].template == NULL) { |
| 127 | *tpls[i].generated = NULL; |
| 128 | continue; |
| 129 | } |
| 130 | |
| 131 | *tpls[i].generated = template_generate(tpls[i].template, features); |
| 132 | if (tpls[i].generated == NULL) { |
| 133 | ret = ENOMEM; |
| 134 | goto done; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | /* nsswitch.conf is special as it can be merged with user-editable file */ |
| 139 | ret = authselect_nsswitch_generate(templates->nsswitch, features, |
| 140 | &files->nsswitch); |
| 141 | if (ret != EOK) { |
| 142 | goto done; |
| 143 | } |
| 144 | |
| 145 | *_files = files; |
| 146 | |
| 147 | ret = EOK; |
| 148 | |
| 149 | done: |
| 150 | if (ret != EOK) { |
| 151 | ERROR("Unable to generate files [%d]: %s", ret, strerror(ret)); |
| 152 | authselect_files_free(files); |
| 153 | } |
| 154 | |
| 155 | return ret; |
| 156 | } |
| 157 | |
| 158 | static errno_t |
| 159 | authselect_system_write_temp(const char *path, |
| 160 | const char *content, |
| 161 | char **_tmp_file) |
| 162 | { |
| 163 | errno_t ret; |
| 164 | |
| 165 | INFO("Writing temporary file for [%s]", path); |
| 166 | ret = template_write_temporary(path, content, AUTHSELECT_FILE_MODE, _tmp_file); |
| 167 | if (ret != EOK) { |
| 168 | ERROR("Unable to write temporary file [%s] [%d]: %s", |
| 169 | path, ret, strerror(ret)); |
| 170 | return ret; |
| 171 | } |
| 172 | |
| 173 | INFO("Temporary file is named [%s]", *_tmp_file); |
| 174 | |
| 175 | return EOK; |
| 176 | } |
| 177 | |
| 178 | static errno_t |
| 179 | authselect_system_rename_temp(char **tmp_path, |
| 180 | const char *final_path) |
| 181 | { |
| 182 | errno_t ret; |
| 183 | |
| 184 | INFO("Renaming [%s] to [%s]", *tmp_path, final_path); |
| 185 | |
| 186 | ret = rename(*tmp_path, final_path); |
| 187 | if (ret != 0) { |
| 188 | ret = errno; |
| 189 | ERROR("Unable to rename [%s] to [%s] [%d]: %s", |
| 190 | *tmp_path, final_path, ret, strerror(ret)); |
| 191 | } |
| 192 | |
| 193 | free(*tmp_path); |
| 194 | *tmp_path = NULL; |
| 195 | |
| 196 | return ret; |
| 197 | } |
| 198 | |
| 199 | errno_t |
| 200 | authselect_system_write(const char **features, |
| 201 | struct authselect_files *templates) |
| 202 | { |
| 203 | struct authselect_files *files; |
| 204 | errno_t ret; |
| 205 | int i; |
| 206 | |
| 207 | ret = authselect_system_generate(features, templates, &files); |
| 208 | if (ret != EOK) { |
| 209 | return ret; |
| 210 | } |
| 211 | |
| 212 | struct authselect_generated generated[] = GENERATED_FILES(files); |
| 213 | char *tmp_files[sizeof(generated)/sizeof(struct authselect_generated)] = {NULL}; |
| 214 | |
| 215 | /* First, write content into temporary files, so we can safely fail |
| 216 | * on error. */ |
| 217 | for (i = 0; generated[i].path != NULL; i++) { |
| 218 | ret = authselect_system_write_temp(generated[i].path, |
| 219 | generated[i].content, |
| 220 | &tmp_files[i]); |
| 221 | if (ret != EOK) { |
| 222 | goto done; |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | /* Now rename the files. |
| 227 | * |
| 228 | * We now know that the system is writable, so rename call shall not |
| 229 | * fail and it will overwrite any existing file. The only reason it |
| 230 | * can fail is EIO which we can not do anything about and we can not |
| 231 | * even recover from it. |
| 232 | */ |
| 233 | for (i = 0; generated[i].path != NULL; i++) { |
| 234 | ret = authselect_system_rename_temp(&tmp_files[i], generated[i].path); |
| 235 | if (ret != EOK) { |
| 236 | goto done; |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | ret = EOK; |
| 241 | |
| 242 | done: |
| 243 | if (ret != EOK) { |
| 244 | for (i = 0; generated[i].path != NULL; i++) { |
| 245 | if (tmp_files[i] != NULL) { |
| 246 | unlink(tmp_files[i]); |
| 247 | free(tmp_files[i]); |
| 248 | tmp_files[i] = NULL; |
| 249 | } |
| 250 | } |
| 251 | } |
| 252 | authselect_files_free(files); |
| 253 | |
| 254 | return ret; |
| 255 | } |
| 256 | |
| 257 | static bool |
| 258 | authselect_system_validate_file(const char *path) |
| 259 | { |
| 260 | errno_t ret; |
| 261 | bool bret; |
| 262 | |
| 263 | INFO("Validating file [%s]", path); |
| 264 | |
| 265 | ret = file_is_regular(path, AUTHSELECT_UID, AUTHSELECT_GID, |
| 266 | S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH, &bret); |
| 267 | if (ret != EOK) { |
| 268 | ERROR("Unable to check file mode of [%s] [%d]: %s", |
| 269 | path, ret, strerror(ret)); |
| 270 | return false; |
| 271 | } |
| 272 | |
| 273 | return bret; |
| 274 | } |
| 275 | |
| 276 | bool |
| 277 | authselect_system_validate(struct authselect_files *files) |
| 278 | { |
| 279 | struct authselect_generated generated[] = GENERATED_FILES(files); |
| 280 | bool result = true; |
| 281 | bool bret; |
| 282 | int i; |
| 283 | |
| 284 | for (i = 0; generated[i].path != NULL; i++) { |
| 285 | bret = authselect_system_validate_file(generated[i].path); |
| 286 | result &= bret; |
| 287 | if (!bret) { |
| 288 | WARN("File [%s] was modified outside authselect!", |
| 289 | generated[i].path); |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | return result; |
| 294 | } |
| 295 | |
| 296 | bool |
| 297 | authselect_system_validate_missing() |
| 298 | { |
| 299 | struct authselect_generated generated[] = GENERATED_FILES_PATHS; |
| 300 | bool result = true; |
| 301 | errno_t ret; |
| 302 | int i; |
| 303 | |
| 304 | for (i = 0; generated[i].path != NULL; i++) { |
| 305 | ret = file_exists(generated[i].path); |
| 306 | if (ret == EOK) { |
| 307 | ERROR("File [%s] is still present", generated[i].path); |
| 308 | result = false; |
| 309 | continue; |
| 310 | } else if (ret != ENOENT) { |
| 311 | ERROR("Error while trying to access file [%s] [%d]: %s", |
| 312 | generated[i].path, ret, strerror(ret)); |
| 313 | result = false; |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | return result; |
| 318 | } |
| 319 | |
| 320 | errno_t |
| 321 | authselect_files_uninstall() |
| 322 | { |
| 323 | struct authselect_generated generated[] = GENERATED_FILES_PATHS; |
| 324 | errno_t ret; |
| 325 | int iret; |
| 326 | int i; |
| 327 | |
| 328 | errno = 0; |
| 329 | iret = unlink(PATH_CONFIG_FILE); |
| 330 | if (iret != 0 && errno != ENOENT) { |
| 331 | ret = errno; |
| 332 | ERROR("Unable to delete [%s] [%d]: %s", PATH_CONFIG_FILE, |
| 333 | ret, strerror(ret)); |
| 334 | return ret; |
| 335 | } |
| 336 | |
| 337 | for (i = 0; generated[i].path != NULL; i++) { |
| 338 | /* We can ignore errors here. */ |
| 339 | iret = unlink(generated[i].path); |
| 340 | if (iret != 0 && errno != ENOENT) { |
| 341 | ret = errno; |
| 342 | WARN("Unable to delete [%s] [%d]: %s", generated[i].path, |
| 343 | ret, strerror(ret)); |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | return EOK; |
| 348 | } |