| 1 | /* |
| 2 | Authors: |
| 3 | Pavel Březina <[email protected]> |
| 4 | |
| 5 | Copyright (C) 2021 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 "config.h" |
| 22 | |
| 23 | #include <errno.h> |
| 24 | #include <regex.h> |
| 25 | #include <string.h> |
| 26 | #include <stdlib.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 | errno_t |
| 37 | authselect_nsswitch_find_maps(char *content, |
| 38 | char ***_maps) |
| 39 | { |
| 40 | char *match_string; |
| 41 | regmatch_t m[RE_NSS_MATCHES]; |
| 42 | regex_t regex; |
| 43 | errno_t ret; |
| 44 | char **maps; |
| 45 | int reret; |
| 46 | |
| 47 | maps = string_array_create(10); |
| 48 | if (maps == NULL) { |
| 49 | return ENOMEM; |
| 50 | } |
| 51 | |
| 52 | reret = regcomp(®ex, RE_NSS, REG_EXTENDED | REG_NEWLINE); |
| 53 | if (reret != REG_NOERROR) { |
| 54 | ERROR("Unable to compile regular expression: regex error %d", reret); |
| 55 | ret = EFAULT; |
| 56 | goto done; |
| 57 | } |
| 58 | |
| 59 | match_string = content; |
| 60 | while ((reret = regexec(®ex, match_string, 2, m, 0)) == REG_NOERROR) { |
| 61 | maps = string_array_add_value_safe(maps, match_string + m[1].rm_so, |
| 62 | m[1].rm_eo - m[1].rm_so, true); |
| 63 | if (maps == NULL) { |
| 64 | ret = ENOMEM; |
| 65 | goto done; |
| 66 | } |
| 67 | |
| 68 | match_string += m[0].rm_eo; |
| 69 | } |
| 70 | |
| 71 | if (reret != REG_NOMATCH) { |
| 72 | ERROR("Unable to search string: regex error %d", reret); |
| 73 | ret = EFAULT; |
| 74 | goto done; |
| 75 | } |
| 76 | |
| 77 | *_maps = maps; |
| 78 | |
| 79 | ret = EOK; |
| 80 | |
| 81 | done: |
| 82 | regfree(®ex); |
| 83 | if (ret != EOK) { |
| 84 | string_array_free(maps); |
| 85 | } |
| 86 | |
| 87 | return ret; |
| 88 | } |
| 89 | |
| 90 | errno_t |
| 91 | authselect_nsswitch_generate(const char *template, |
| 92 | const char **features, |
| 93 | char **_content) |
| 94 | { |
| 95 | char *content; |
| 96 | |
| 97 | content = template_generate(template, features); |
| 98 | if (content == NULL) { |
| 99 | return ENOMEM; |
| 100 | } |
| 101 | |
| 102 | *_content = content; |
| 103 | |
| 104 | return EOK; |
| 105 | } |