Debian Codegraph @

src/lib/authselect_backup.c

raw symbols SCIP
1 /*
2 Authors:
3 Pavel Březina <[email protected]>
4
5 Copyright (C) 2019 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 <time.h>
22 #include <errno.h>
23 #include <string.h>
24 #include <stdlib.h>
25
26 #include "authselect.h"
27 #include "common/common.h"
28 #include "lib/constants.h"
29 #include "lib/util/util.h"
30 #include "lib/files/files.h"
31 #include "lib/profiles/profiles.h"
32
33 static errno_t
34 authselect_backup_create_named(const char *name,
35 char **_path)
36 {
37 char *path;
38 errno_t ret;
39
40 path = format("%s/%s", AUTHSELECT_BACKUP_DIR, name);
41 if (path == NULL) {
42 return ENOMEM;
43 }
44
45 ret = file_make_path(path, AUTHSELECT_DIR_MODE);
46 if (ret != EOK) {
47 free(path);
48 ERROR("Unable to create backup directory [%s/%s] [%d]: %s",
49 AUTHSELECT_BACKUP_DIR, name, ret, strerror(ret));
50 return ret;
51 }
52
53 *_path = path;
54
55 return EOK;
56 }
57
58 static errno_t
59 authselect_backup_create_anonymous(char **_path)
60 {
61 struct tm *gmtime_ret;
62 struct tm tm;
63 char date[255];
64 char *path;
65 time_t now;
66 size_t n;
67 errno_t ret;
68
69 ret = file_make_path(AUTHSELECT_BACKUP_DIR, AUTHSELECT_DIR_MODE);
70 if (ret != EOK) {
71 ERROR("Unable to create backup directory [%s] [%d]: %s",
72 AUTHSELECT_BACKUP_DIR, ret, strerror(ret));
73 return ret;
74 }
75
76 now = time(NULL);
77 gmtime_ret = gmtime_r(&now, &tm);
78 if (gmtime_ret == NULL) {
79 return EINVAL;
80 }
81
82 n = strftime(date, sizeof(date), "%Y-%m-%d-%H-%M-%S", &tm);
83 if (n == 0) {
84 return EINVAL;
85 }
86
87 path = format("%s/%s.XXXXXX", AUTHSELECT_BACKUP_DIR, date);
88 if (path == NULL) {
89 return ENOMEM;
90 }
91
92 INFO("Creating temporary directory at [%s]", path);
93 if (mkdtemp(path) == NULL) {
94 ret = errno;
95 free(path);
96 return ret;
97 }
98
99 *_path = path;
100
101 return EOK;
102 }
103
104 static errno_t
105 authselect_backup_create(const char *name, char **_path)
106 {
107 if (name != NULL && name[0] != '\0') {
108 return authselect_backup_create_named(name, _path);
109 }
110
111 return authselect_backup_create_anonymous(_path);
112 }
113
114 static errno_t
115 authselect_backup_system_configuration(const char *path)
116 {
117 struct authselect_symlink files[] = {SYMLINK_FILES};
118 const char *filename;
119 errno_t ret;
120 int i;
121
122 for (i = 0; files[i].name != NULL; i++) {
123 filename = file_get_basename(files[i].dest);
124 if (filename == NULL) {
125 ERROR("There is no filename in [%s]", files[i].dest);
126 return EINVAL;
127 }
128
129 INFO("Copying [%s] to [%s/%s]", files[i].name, path, filename);
130 ret = selinux_file_copy(files[i].name, path, filename,
131 AUTHSELECT_DIR_MODE);
132 if (ret == ENOENT) {
133 WARN("File [%s] does not exist", files[i].name);
134 } else if (ret != EOK) {
135 ERROR("Unable to copy [%s] to [%s/%s] [%d]: %s", files[i].name,
136 path, filename, ret, strerror(ret));
137 return ret;
138 }
139 }
140
141 return EOK;
142 }
143
144 static errno_t
145 authselect_backup_authselect_configuration(const char *path)
146 {
147 errno_t ret;
148
149 ret = selinux_file_copy(PATH_CONFIG_FILE, path, FILE_CONFIG,
150 AUTHSELECT_DIR_MODE);
151 if (ret != EOK) {
152 ERROR("Unable to copy [%s] to [%s/%s] [%d]: %s", PATH_CONFIG_FILE,
153 path, FILE_CONFIG, ret, strerror(ret));
154 return ret;
155 }
156
157 return authselect_backup_system_configuration(path);
158 }
159
160 _PUBLIC_ int
161 authselect_backup(const char *name, char **_path)
162 {
163 char *path = NULL;
164 bool is_valid;
165 errno_t ret;
166
167 ret = authselect_backup_create(name, &path);
168 if (ret != EOK) {
169 goto done;
170 }
171
172 ret = authselect_validate_configuration(&is_valid);
173 if (ret == EOK && is_valid) {
174 /* Valid authselect configuration. */
175 INFO("Trying to backup authselect configuration to [%s]", path);
176 ret = authselect_backup_authselect_configuration(path);
177 goto done;
178 }
179
180 INFO("Trying to backup system configuration to [%s]", path);
181 ret = authselect_backup_system_configuration(path);
182
183 done:
184 if (ret == EOK) {
185 INFO("Backup was successfully created at [%s]", path);
186 *_path = path;
187 ret = EOK;
188 } else if (ret != EOK) {
189 ERROR("Unable to create backup [%d]: %s", ret, strerror(ret));
190 free(path);
191 }
192
193 return ret;
194 }
195
196 _PUBLIC_ char **
197 authselect_backup_list(void)
198 {
199 char **names;
200 errno_t ret;
201
202 ret = dir_list(AUTHSELECT_BACKUP_DIR,
203 DIR_LIST_DIRS | DIR_LIST_SORT_BY_CTIME,
204 &names, NULL);
205 if (ret == ENOENT) {
206 INFO(AUTHSELECT_BACKUP_DIR " does not exist.");
207 return string_array_create(0);
208 } else if (ret != EOK) {
209 ERROR("Unable to list directory [%s] [%d]: %s",
210 AUTHSELECT_BACKUP_DIR, ret, strerror(ret));
211 return NULL;
212 }
213
214 return names;
215 }
216
217 _PUBLIC_ int
218 authselect_backup_remove(const char *name)
219 {
220 char *path;
221 errno_t ret;
222
223 INFO("Removing backup [%s]", name);
224
225 path = format("%s/%s", AUTHSELECT_BACKUP_DIR, name);
226 if (path == NULL) {
227 return ENOMEM;
228 }
229
230 ret = dir_remove(path);
231 if (ret != EOK) {
232 ERROR("Unable to delete directory [%s] [%d]: %s",
233 path, ret, strerror(ret));
234 }
235
236 return ret;
237 }
238
239 static errno_t
240 authselect_restore_system_configuration(const char *path)
241 {
242 struct selinux_safe_copy table[] = {
243 {FILE_CONFIG, PATH_CONFIG_FILE, true, false},
244 {FILE_SYSTEM, PATH_SYMLINK_SYSTEM, false, false},
245 {FILE_PASSWORD, PATH_SYMLINK_PASSWORD, true, false},
246 {FILE_FINGERPRINT, PATH_SYMLINK_FINGERPRINT, true, false},
247 {FILE_SMARTCARD, PATH_SYMLINK_SMARTCARD, true, false},
248 {FILE_SWITCHABLE, PATH_SYMLINK_SWITCHABLE, true, false},
249 {FILE_POSTLOGIN, PATH_SYMLINK_POSTLOGIN, false, false},
250 {FILE_NSSWITCH, PATH_SYMLINK_NSSWITCH, true, false},
251 {FILE_DCONF_DB, PATH_SYMLINK_DCONF_DB, true, false},
252 {FILE_DCONF_LOCK, PATH_SYMLINK_DCONF_LOCK, true, false},
253 {NULL, NULL, false, false},
254 };
255 errno_t ret;
256 int i;
257
258 for (i = 0; table[i].source != NULL; i++) {
259 table[i].source = format("%s/%s", path, table[i].source);
260 if (table[i].source == NULL) {
261 ret = ENOMEM;
262 goto done;
263 }
264 }
265
266 ret = selinux_copy_files_safely(table, AUTHSELECT_DIR_MODE, true);
267
268 done:
269 /* In case of an error in formatting the source file, it will be NULL
270 * and the iteration will stop before we free the static data. */
271 for (i = 0; table[i].source != NULL; i++) {
272 free((char*)table[i].source);
273 }
274
275 return ret;
276 }
277
278 static errno_t
279 authselect_restore_authselect_configuration(const char *path)
280 {
281 struct selinux_safe_copy table[] = {
282 {FILE_CONFIG, PATH_CONFIG_FILE, false, false},
283 {FILE_SYSTEM, PATH_SYSTEM, false, false},
284 {FILE_PASSWORD, PATH_PASSWORD, false, false},
285 {FILE_FINGERPRINT, PATH_FINGERPRINT, false, false},
286 {FILE_SMARTCARD, PATH_SMARTCARD, false, false},
287 {FILE_SWITCHABLE, PATH_SWITCHABLE, false, true},
288 {FILE_POSTLOGIN, PATH_POSTLOGIN, false, false},
289 {FILE_NSSWITCH, PATH_NSSWITCH, false, false},
290 {FILE_DCONF_DB, PATH_DCONF_DB, false, false},
291 {FILE_DCONF_LOCK, PATH_DCONF_LOCK, false, false},
292 {NULL, NULL, false, false},
293 };
294 errno_t ret;
295 int i;
296
297 for (i = 0; table[i].source != NULL; i++) {
298 table[i].source = format("%s/%s", path, table[i].source);
299 if (table[i].source == NULL) {
300 ret = ENOMEM;
301 goto done;
302 }
303 }
304
305 ret = selinux_copy_files_safely(table, AUTHSELECT_DIR_MODE, true);
306 if (ret != EOK) {
307 ERROR("Unable to copy files [%d]: %s", ret, strerror(ret));
308 goto done;
309 }
310
311 ret = authselect_symlinks_write();
312 if (ret != EOK) {
313 ERROR("Unable to create symbolic links [%d]: %s", ret, strerror(ret));
314 goto done;
315 }
316
317 ret = authselect_profile_dconf_update();
318 if (ret == ENOENT) {
319 INFO("Dconf is not installed on your system");
320 } else if (ret != EOK) {
321 ERROR("Unable to update dconf database [%d]: %s", ret, strerror(ret));
322 goto done;
323 }
324
325 ret = EOK;
326
327 done:
328 /* In case of an error in formatting the source file, it will be NULL
329 * and the iteration will stop before we free the static data. */
330 for (i = 0; table[i].source != NULL; i++) {
331 free((char*)table[i].source);
332 }
333
334 return ret;
335 }
336
337 _PUBLIC_ int
338 authselect_backup_restore(const char *name)
339 {
340 char *confpath = NULL;
341 char *path = NULL;
342 errno_t ret;
343
344 INFO("Restoring configuration from backup [%s]", name);
345
346 path = format("%s/%s", AUTHSELECT_BACKUP_DIR, name);
347 if (path == NULL) {
348 ret = ENOMEM;
349 goto done;
350 }
351
352 confpath = format("%s/%s", path, FILE_CONFIG);
353 if (confpath == NULL) {
354 ret = ENOMEM;
355 goto done;
356 }
357
358 ret = file_exists(confpath);
359 if (ret == EOK) {
360 INFO("Backup [%s] contains authselect configuration", name);
361 ret = authselect_restore_authselect_configuration(path);
362 } else if (ret == ENOENT) {
363 INFO("Backup [%s] contains non-authselect configuration", name);
364 ret = authselect_restore_system_configuration(path);
365 }
366
367 done:
368 if (ret != EOK) {
369 ERROR("Unable to restore [%s] [%d]: %s", name, ret, strerror(ret));
370 }
371
372 free(confpath);
373 free(path);
374
375 return ret;
376 }