]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
07d46372 YW |
2 | /*** |
3 | This file is part of systemd. | |
4 | ||
5 | Copyright 2017 Yu Watanabe | |
6 | ||
7 | systemd is free software; you can redistribute it and/or modify it | |
8 | under the terms of the GNU Lesser General Public License as published by | |
9 | the Free Software Foundation; either version 2.1 of the License, or | |
10 | (at your option) any later version. | |
11 | ||
12 | systemd is distributed in the hope that it will be useful, but | |
13 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 | Lesser General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU Lesser General Public License | |
18 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
19 | ***/ | |
20 | ||
21 | #include <errno.h> | |
dccca82b | 22 | #include <stdio.h> |
07d46372 YW |
23 | |
24 | #include "alloc-util.h" | |
25 | #include "extract-word.h" | |
26 | #include "securebits.h" | |
27 | #include "securebits-util.h" | |
28 | #include "string-util.h" | |
29 | ||
30 | int secure_bits_to_string_alloc(int i, char **s) { | |
31 | _cleanup_free_ char *str = NULL; | |
32 | size_t len; | |
33 | int r; | |
34 | ||
35 | assert(s); | |
36 | ||
37 | r = asprintf(&str, "%s%s%s%s%s%s", | |
38 | (i & (1 << SECURE_KEEP_CAPS)) ? "keep-caps " : "", | |
39 | (i & (1 << SECURE_KEEP_CAPS_LOCKED)) ? "keep-caps-locked " : "", | |
40 | (i & (1 << SECURE_NO_SETUID_FIXUP)) ? "no-setuid-fixup " : "", | |
41 | (i & (1 << SECURE_NO_SETUID_FIXUP_LOCKED)) ? "no-setuid-fixup-locked " : "", | |
42 | (i & (1 << SECURE_NOROOT)) ? "noroot " : "", | |
43 | (i & (1 << SECURE_NOROOT_LOCKED)) ? "noroot-locked " : ""); | |
44 | if (r < 0) | |
45 | return -ENOMEM; | |
46 | ||
47 | len = strlen(str); | |
48 | if (len != 0) | |
49 | str[len - 1] = '\0'; | |
50 | ||
ae2a15bc | 51 | *s = TAKE_PTR(str); |
07d46372 YW |
52 | |
53 | return 0; | |
54 | } | |
55 | ||
56 | int secure_bits_from_string(const char *s) { | |
57 | int secure_bits = 0; | |
58 | const char *p; | |
59 | int r; | |
60 | ||
61 | for (p = s;;) { | |
62 | _cleanup_free_ char *word = NULL; | |
63 | ||
64 | r = extract_first_word(&p, &word, NULL, EXTRACT_QUOTES); | |
65 | if (r == -ENOMEM) | |
66 | return r; | |
67 | if (r <= 0) | |
68 | break; | |
69 | ||
70 | if (streq(word, "keep-caps")) | |
71 | secure_bits |= 1 << SECURE_KEEP_CAPS; | |
72 | else if (streq(word, "keep-caps-locked")) | |
73 | secure_bits |= 1 << SECURE_KEEP_CAPS_LOCKED; | |
74 | else if (streq(word, "no-setuid-fixup")) | |
75 | secure_bits |= 1 << SECURE_NO_SETUID_FIXUP; | |
76 | else if (streq(word, "no-setuid-fixup-locked")) | |
77 | secure_bits |= 1 << SECURE_NO_SETUID_FIXUP_LOCKED; | |
78 | else if (streq(word, "noroot")) | |
79 | secure_bits |= 1 << SECURE_NOROOT; | |
80 | else if (streq(word, "noroot-locked")) | |
81 | secure_bits |= 1 << SECURE_NOROOT_LOCKED; | |
82 | } | |
83 | ||
84 | return secure_bits; | |
85 | } |