]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/securebits-util.c
b5f6418a6c10882ec3d8ac64955b1446dc141c51
[thirdparty/systemd.git] / src / basic / securebits-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
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>
22
23 #include "alloc-util.h"
24 #include "extract-word.h"
25 #include "securebits.h"
26 #include "securebits-util.h"
27 #include "string-util.h"
28
29 int secure_bits_to_string_alloc(int i, char **s) {
30 _cleanup_free_ char *str = NULL;
31 size_t len;
32 int r;
33
34 assert(s);
35
36 r = asprintf(&str, "%s%s%s%s%s%s",
37 (i & (1 << SECURE_KEEP_CAPS)) ? "keep-caps " : "",
38 (i & (1 << SECURE_KEEP_CAPS_LOCKED)) ? "keep-caps-locked " : "",
39 (i & (1 << SECURE_NO_SETUID_FIXUP)) ? "no-setuid-fixup " : "",
40 (i & (1 << SECURE_NO_SETUID_FIXUP_LOCKED)) ? "no-setuid-fixup-locked " : "",
41 (i & (1 << SECURE_NOROOT)) ? "noroot " : "",
42 (i & (1 << SECURE_NOROOT_LOCKED)) ? "noroot-locked " : "");
43 if (r < 0)
44 return -ENOMEM;
45
46 len = strlen(str);
47 if (len != 0)
48 str[len - 1] = '\0';
49
50 *s = str;
51 str = NULL;
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 }