Debian Codegraph @

src/lib/authselect_profile.c

raw symbols SCIP
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 <string.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24
25 #include "authselect.h"
26 #include "common/common.h"
27 #include "lib/constants.h"
28 #include "lib/util/util.h"
29 #include "lib/profiles/profiles.h"
30
31 _PUBLIC_ int
32 authselect_profile(const char *profile_id,
33 struct authselect_profile **_profile)
34 {
35 return authselect_profile_read(profile_id, AUTHSELECT_PROFILE_ANY,
36 _profile);
37 }
38
39 _PUBLIC_ const char *
40 authselect_profile_id(const struct authselect_profile *profile)
41 {
42 if (profile == NULL) {
43 return NULL;
44 }
45
46 return profile->id;
47 }
48
49 _PUBLIC_ const char *
50 authselect_profile_name(const struct authselect_profile *profile)
51 {
52 if (profile == NULL) {
53 return NULL;
54 }
55
56 return profile->name;
57 }
58
59 _PUBLIC_ const char *
60 authselect_profile_path(const struct authselect_profile *profile)
61 {
62 if (profile == NULL) {
63 return NULL;
64 }
65
66 return profile->path;
67 }
68
69 _PUBLIC_ const char *
70 authselect_profile_description(const struct authselect_profile *profile)
71 {
72 if (profile == NULL) {
73 return NULL;
74 }
75
76 return profile->description;
77 }
78
79 _PUBLIC_ char *
80 authselect_profile_requirements(const struct authselect_profile *profile,
81 const char **features)
82 {
83 if (profile == NULL) {
84 return NULL;
85 }
86
87 return template_generate(profile->requirements, features);
88 }
89
90 _PUBLIC_ char **
91 authselect_profile_nsswitch_maps(const struct authselect_profile *profile,
92 const char **features)
93 {
94 char *template;
95 char **maps;
96 errno_t ret;
97
98 if (profile == NULL) {
99 return NULL;
100 }
101
102 template = template_generate(profile->files->nsswitch, features);
103 if (template == NULL) {
104 ERROR("Unable to generate nsswitch.conf");
105 return NULL;
106 }
107
108 ret = authselect_nsswitch_find_maps(template, &maps);
109 free(template);
110 if (ret != EOK) {
111 ERROR("Unable to find nsswitch maps [%d]: %s", ret, strerror(ret));
112 return NULL;
113 }
114
115 return maps;
116 }
117
118 _PUBLIC_ char **
119 authselect_profile_features(const struct authselect_profile *profile)
120 {
121 char **features;
122 char **array;
123 errno_t ret;
124 int i;
125
126 if (profile == NULL) {
127 return NULL;
128 }
129
130 features = string_array_create(10);
131 if (features == NULL) {
132 ERROR("Unable to create array (out of memory)");
133 return NULL;
134 }
135
136 struct authselect_generated files[] = PROFILE_FILES(profile->files);
137
138 for (i = 0; files[i].path != NULL; i++) {
139 array = template_list_features(files[i].content);
140 if (array == NULL) {
141 ERROR("Unable to obtain feature list (out of memory)");
142 ret = ENOMEM;
143 goto done;
144 }
145
146 features = string_array_concat(features, array, true);
147 string_array_free(array);
148
149 if (features == NULL) {
150 ERROR("Unable to obtain feature list (out of memory)");
151 ret = ENOMEM;
152 goto done;
153 }
154 }
155
156 string_array_sort(features);
157
158 ret = EOK;
159
160 done:
161 if (ret != EOK) {
162 string_array_free(features);
163 return NULL;
164 }
165
166 return features;
167 }
168
169 _PUBLIC_ void
170 authselect_profile_free(struct authselect_profile *profile)
171 {
172 if (profile == NULL) {
173 return;
174 }
175
176 if (profile->id != NULL) {
177 free(profile->id);
178 }
179
180 if (profile->path != NULL) {
181 free(profile->path);
182 }
183
184 if (profile->name != NULL) {
185 free(profile->name);
186 }
187
188 if (profile->description != NULL) {
189 free(profile->description);
190 }
191
192 if (profile->requirements != NULL) {
193 free(profile->requirements);
194 }
195
196 authselect_files_free(profile->files);
197
198 memset(profile, 0, sizeof(struct authselect_profile));
199
200 free(profile);
201
202 return;
203 }
204
205 static char **
206 authselect_profile_files_get(const char *profile_path)
207 {
208 const char *files[] = {FILES_ALL, NULL};
209 char **paths;
210 char *path;
211 errno_t ret;
212 int i;
213
214 paths = string_array_create(sizeof(files) / sizeof(const char *));
215 if (paths == NULL) {
216 return NULL;
217 }
218
219 for (i = 0; files[i] != NULL; i++) {
220 path = format("%s/%s", profile_path, files[i]);
221 if (path == NULL) {
222 ret = ENOMEM;
223 goto done;
224 }
225
226 paths = string_array_add_value(paths, path, true);
227 free(path);
228 if (paths == NULL) {
229 ret = ENOMEM;
230 goto done;
231 }
232 }
233
234 ret = EOK;
235
236 done:
237 if (ret != EOK) {
238 string_array_free(paths);
239 return NULL;
240 }
241
242 return paths;
243 }
244
245 static errno_t
246 authselect_profile_symlinks_add(const char *profile_path,
247 char ***symlinks,
248 const char **from)
249 {
250 char *target;
251 int i;
252
253 if (from == NULL) {
254 return EOK;
255 }
256
257 for (i = 0; from[i] != NULL; i++) {
258 target = format("%s/%s", profile_path, from[i]);
259 if (target == NULL) {
260 return ENOMEM;
261 }
262
263 *symlinks = string_array_add_value(*symlinks, target, true);
264 free(target);
265 if (*symlinks == NULL) {
266 return ENOMEM;
267 }
268 }
269
270 return EOK;
271 }
272
273 static char **
274 authselect_profile_symlinks_get(const char *profile_path,
275 uint32_t flags,
276 const char **symlinks)
277 {
278 const char *sym_meta[] = {FILES_META, NULL};
279 const char *sym_nsswitch[] = {FILES_NSSWITCH, NULL};
280 const char *sym_pam[] = {FILES_PAM, NULL};
281 const char *sym_dconf[] = {FILES_DCONF, NULL};
282 char **targets;
283 errno_t ret;
284
285 targets = string_array_create(10);
286 if (targets == NULL) {
287 ret = ENOMEM;
288 goto done;
289 }
290
291 if (flags & AUTHSELECT_SYMLINK_META) {
292 ret = authselect_profile_symlinks_add(profile_path, &targets,
293 sym_meta);
294 if (ret != EOK) {
295 goto done;
296 }
297 }
298
299 if (flags & AUTHSELECT_SYMLINK_NSSWITCH) {
300 ret = authselect_profile_symlinks_add(profile_path, &targets,
301 sym_nsswitch);
302 if (ret != EOK) {
303 goto done;
304 }
305 }
306
307 if (flags & AUTHSELECT_SYMLINK_PAM) {
308 ret = authselect_profile_symlinks_add(profile_path, &targets,
309 sym_pam);
310 if (ret != EOK) {
311 goto done;
312 }
313 }
314
315 if (flags & AUTHSELECT_SYMLINK_DCONF) {
316 ret = authselect_profile_symlinks_add(profile_path, &targets,
317 sym_dconf);
318 if (ret != EOK) {
319 goto done;
320 }
321 }
322
323 ret = authselect_profile_symlinks_add(profile_path, &targets,
324 symlinks);
325 if (ret != EOK) {
326 goto done;
327 }
328
329 done:
330 if (ret != EOK) {
331 string_array_free(targets);
332 return NULL;
333 }
334
335 return targets;
336 }
337
338 static errno_t
339 authselect_profile_create_empty(const char *path, char **filepaths)
340 {
341 errno_t ret;
342 int i;
343
344 INFO("Creating empty profile at [%s]", path);
345
346 ret = file_make_path(path, AUTHSELECT_DIR_MODE);
347 if (ret != EOK) {
348 ERROR("Unable to make path [%s] [%d]: %s", path, ret, strerror(ret));
349 return ret;
350 }
351
352 for (i = 0; filepaths[i] != NULL; i++) {
353 ret = textfile_write(filepaths[i], "", AUTHSELECT_FILE_MODE);
354 if (ret != EOK) {
355 ERROR("Unable to write to [%s] [%d]: %s",
356 filepaths[i], ret, strerror(ret));
357 return ret;
358 }
359 }
360
361 return EOK;
362 }
363
364 static errno_t
365 authselect_profile_create_from_source(const char *filename,
366 const char *destination,
367 const char *content,
368 char **symlinks)
369 {
370 const char *symname;
371 errno_t ret;
372 int i;
373
374 for (i = 0; symlinks[i] != NULL; i++) {
375 symname = file_get_basename(symlinks[i]);
376 if (symname == NULL) {
377 ERROR("There is no filename in [%s]", symlinks[i]);
378 return EINVAL;
379 }
380
381 if (strcmp(filename, symname) == 0) {
382 /* Create symlink instead of copying the file - if it exists. */
383 ret = file_exists(symlinks[i]);
384 if (ret == ENOENT) {
385 INFO("Omitting [%s] since it does not exist in base profile",
386 symname);
387 return EOK;
388 } else if (ret != EOK) {
389 ERROR("Unable to check presence of [%s] [%d]: %s",
390 symlinks[i], ret, strerror(ret));
391 return ret;
392 }
393
394 ret = symlink(symlinks[i], destination);
395 if (ret != 0) {
396 ret = errno;
397 ERROR("Unable to create symbolic link [%s] to [%s] [%d]: %s",
398 destination, symlinks[i], ret, strerror(ret));
399 return ret;
400 }
401
402 return EOK;
403 }
404 }
405
406 ret = textfile_write(destination, content, AUTHSELECT_FILE_MODE);
407 if (ret != EOK) {
408 ERROR("Unable to write to [%s] [%d]: %s",
409 destination, ret, strerror(ret));
410 return ret;
411 }
412
413 return EOK;
414 }
415
416 static errno_t
417 authselect_profile_create_from(const char *path,
418 char **filepaths,
419 const char *base_id,
420 enum authselect_profile_type base_type,
421 uint32_t symlink_flags,
422 const char **symlinks)
423 {
424 struct authselect_profile *base;
425 char **symlink_targets;
426 const char *filename;
427 errno_t ret;
428 int i, j;
429
430 INFO("Creating new profile from \"%s\" at [%s]", base_id, path);
431
432 ret = authselect_profile_read(base_id, base_type, &base);
433 if (ret != EOK) {
434 ERROR("Unable to read base profile [%s] [%d]: %s",
435 base_id, ret, strerror(ret));
436 return ret;
437 }
438
439 symlink_targets = authselect_profile_symlinks_get(base->path, symlink_flags,
440 symlinks);
441 if (symlink_targets == NULL) {
442 ERROR("Unable to resolve symbolic links names");
443 ret = ENOMEM;
444 goto done;
445 }
446
447 ret = file_make_path(path, AUTHSELECT_DIR_MODE);
448 if (ret != EOK) {
449 ERROR("Unable to make path [%s] [%d]: %s", path, ret, strerror(ret));
450 goto done;
451 }
452
453 struct authselect_generated profile_files[] = PROFILE_FILES(base->files);
454 for (i = 0; filepaths[i] != NULL; i++) {
455 filename = file_get_basename(filepaths[i]);
456 if (filename == NULL) {
457 ERROR("There is no filename in [%s]", filepaths[i]);
458 ret = EINVAL;
459 goto done;
460 }
461
462 if (strcmp(filename, FILE_README) == 0) {
463 ret = authselect_profile_create_from_source(filename,
464 filepaths[i], base->description,
465 symlink_targets);
466 if (ret != EOK) {
467 ERROR("Unable to create [%s] [%d]: %s",
468 filepaths[i], ret, strerror(ret));
469 goto done;
470 }
471
472 continue;
473 }
474
475 if (strcmp(filename, FILE_REQUIREMENT) == 0) {
476 ret = authselect_profile_create_from_source(filename,
477 filepaths[i], base->requirements,
478 symlink_targets);
479 if (ret != EOK) {
480 ERROR("Unable to create [%s] [%d]: %s",
481 filepaths[i], ret, strerror(ret));
482 goto done;
483 }
484
485 continue;
486 }
487
488 for (j = 0; profile_files[j].path != NULL; j++) {
489 if (strcmp(filename, profile_files[j].path) == 0) {
490 ret = authselect_profile_create_from_source(filename,
491 filepaths[i], profile_files[j].content,
492 symlink_targets);
493 if (ret != EOK) {
494 ERROR("Unable to create [%s] [%d]: %s",
495 filepaths[i], ret, strerror(ret));
496 goto done;
497 }
498
499 break;
500 }
501
502 WARN("Unknown file name [%s]", filepaths[i]);
503 }
504 }
505
506 ret = EOK;
507
508 done:
509 authselect_profile_free(base);
510
511 return ret;
512 }
513
514 _PUBLIC_ int
515 authselect_profile_create(const char *name,
516 enum authselect_profile_type type,
517 const char *base_id,
518 enum authselect_profile_type base_type,
519 uint32_t symlink_flags,
520 const char **symlinks,
521 char **_path)
522 {
523 char **filepaths = NULL;
524 char *path = NULL;
525 errno_t ret;
526 int i;
527
528 if (string_is_empty(name)) {
529 ERROR("Name can not be empty");
530 return EINVAL;
531 }
532
533 switch (type) {
534 case AUTHSELECT_PROFILE_VENDOR:
535 path = format("%s/%s", DIR_VENDOR_PROFILES, name);
536 break;
537 case AUTHSELECT_PROFILE_CUSTOM:
538 path = format("%s/%s", DIR_CUSTOM_PROFILES, name);
539 break;
540 case AUTHSELECT_PROFILE_DEFAULT:
541 ERROR("Default profile can not be created");
542 return EINVAL;
543 case AUTHSELECT_PROFILE_ANY:
544 ERROR("Value AUTHSELECT_PROFILE_ANY is invalid in this context");
545 return EINVAL;
546 }
547
548 if (path == NULL) {
549 ERROR("Unable to create profile path: out of memory");
550 return ENOMEM;
551 }
552
553 ret = file_exists(path);
554 if (ret == EOK) {
555 ERROR("Profile \"%s\" already exists at [%s]", name, path);
556 ret = EEXIST;
557 goto done;
558 } else if (ret != ENOENT) {
559 ERROR("Unable to access [%s] [%d]: %s", path, ret, strerror(ret));
560 goto done;
561 }
562
563 filepaths = authselect_profile_files_get(path);
564 if (filepaths == NULL) {
565 ERROR("Unable to create file name: out of memory");
566 ret = ENOMEM;
567 goto done;
568 }
569
570 if (base_id == NULL) {
571 ret = authselect_profile_create_empty(path, filepaths);
572 if (ret != EOK) {
573 ERROR("Unable to create empty profile [%d]: %s",
574 ret, strerror(ret));
575 goto done;
576 }
577 } else {
578 ret = authselect_profile_create_from(path, filepaths, base_id,
579 base_type, symlink_flags,
580 symlinks);
581 if (ret != EOK) {
582 ERROR("Unable to create profile [%d]: %s",
583 ret, strerror(ret));
584 goto done;
585 }
586 }
587
588 if (_path != NULL) {
589 *_path = path;
590 } else {
591 free(path);
592 }
593
594 ret = EOK;
595
596 done:
597 if (ret != EOK) {
598 for (i = 0; filepaths != NULL && filepaths[i] != NULL; i++) {
599 unlink(filepaths[i]);
600 }
601
602 rmdir(path);
603 free(path);
604 }
605
606 string_array_free(filepaths);
607
608 return ret;
609 }