Debian Codegraph @

src/lib/files/config.c

raw symbols SCIP
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 <fcntl.h>
22 #include <errno.h>
23 #include <string.h>
24 #include <stdlib.h>
25
26 #include "common/common.h"
27 #include "lib/constants.h"
28 #include "lib/util/util.h"
29 #include "lib/util/config.h"
30 #include "lib/files/files.h"
31
32 errno_t
33 authselect_config_read(char **_profile_id,
34 char ***_features)
35 {
36 return config_read(PATH_CONFIG_FILE, _profile_id, _features);
37 }
38
39 errno_t
40 authselect_config_write(const char *profile_id,
41 const char **features)
42 {
43 char *implode;
44 char *output;
45 errno_t ret;
46
47 implode = string_implode(features, '\n');
48 if (implode == NULL) {
49 return ENOMEM;
50 }
51
52 output = format("%s\n%s\n", profile_id, implode);
53 free(implode);
54 if (output == NULL) {
55 return ENOMEM;
56 }
57
58 ret = textfile_write(PATH_CONFIG_FILE, output, AUTHSELECT_FILE_MODE);
59 free(output);
60
61 return ret;
62 }
63
64 bool
65 authselect_config_locations_writable()
66 {
67 struct authselect_symlink files[] = {
68 {PATH_CONFIG_FILE, NULL, false},
69 SYMLINK_FILES
70 };
71 bool result = true;
72 char *dirpath;
73 errno_t ret;
74 int i;
75
76 INFO("Checking if all required directories are writable.");
77
78 for (i = 0; files[i].name != NULL; i++) {
79 dirpath = file_get_parent_directory(files[i].name);
80 if (dirpath == NULL) {
81 ERROR("Unable to get path to %s parent directory!", files[i].name);
82 result = false;
83 continue;
84 }
85
86 ret = file_check_access(dirpath, W_OK | X_OK);
87 if (ret == ENOENT && files[i].create_path) {
88 INFO("Creating path [%s]", dirpath);
89 ret = file_make_path(dirpath, AUTHSELECT_DIR_MODE);
90 if (ret != EOK) {
91 result = false;
92 ERROR("Unable to create path [%s] [%d]: %s",
93 dirpath, ret, strerror(ret));
94 }
95 } else if (ret == ENOENT) {
96 result = false;
97 ERROR("Directory [%s] does not exist, please create it!", dirpath);
98 } else if (ret != EOK) {
99 result = false;
100 ERROR("Unable to access directory [%s] in [WX] mode!", dirpath);
101 }
102
103 free(dirpath);
104 }
105
106 return result;
107 }
108
109 bool
110 authselect_config_validate_authselect(const char *profile_id,
111 const char **features)
112 {
113 struct authselect_files *files;
114 bool result = true;
115 errno_t ret;
116
117 ret = authselect_files(profile_id, features, &files);
118 if (ret != EOK) {
119 ERROR("Unable to load profile [%s] [%d]: %s",
120 profile_id, ret, strerror(ret));
121 return false;
122 }
123
124 /* Check that generated files exist and have proper content. */
125 result &= authselect_system_validate(files);
126
127 /* Check that symlinks exist and point to generated files. */
128 result &= authselect_symlinks_validate();
129
130 authselect_files_free(files);
131
132 return result;
133 }
134
135 bool
136 authselect_config_validate_user()
137 {
138 bool result = true;
139
140 result &= authselect_system_validate_missing();
141 result &= authselect_symlinks_validate_missing();
142
143 return result;
144 }
145
146 bool
147 authselect_config_validate_missing()
148 {
149 struct authselect_generated generated[] = GENERATED_FILES_PATHS;
150 struct authselect_symlink symlinks[] = {SYMLINK_FILES};
151 errno_t ret;
152 int i;
153
154 ret = file_exists(PATH_CONFIG_FILE);
155 if (ret != ENOENT) {
156 return false;
157 }
158
159 for (i = 0; generated[i].path != NULL; i++) {
160 ret = file_exists(generated[i].path);
161 if (ret != ENOENT) {
162 return false;
163 }
164 }
165
166 for (i = 0; symlinks[i].name != NULL; i++) {
167 ret = file_exists(symlinks[i].name);
168 if (ret != ENOENT) {
169 return false;
170 }
171 }
172
173 return true;
174 }