Debian Codegraph @

src/analyze/analyze-image-policy.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "alloc-util.h"
4 #include "analyze.h"
5 #include "analyze-image-policy.h"
6 #include "ansi-color.h"
7 #include "format-table.h"
8 #include "image-policy.h"
9 #include "string-util.h"
10
11 static int table_add_designator_line(Table *table, PartitionDesignator d, PartitionPolicyFlags f) {
12 _cleanup_free_ char *q = NULL, *t = NULL;
13 const char *color;
14 int r;
15
16 assert(table);
17 assert(f >= 0);
18
19 if (partition_policy_flags_to_string(f & _PARTITION_POLICY_USE_MASK, /* simplify= */ true, &q) < 0)
20 return log_oom();
21
22 if (partition_policy_flags_to_string(f & _PARTITION_POLICY_FSTYPE_MASK, /* simplify= */ true, &t) < 0)
23 return log_oom();
24
25 color = (f & _PARTITION_POLICY_USE_MASK) == PARTITION_POLICY_IGNORE ? ansi_grey() :
26 ((f & (PARTITION_POLICY_UNPROTECTED|PARTITION_POLICY_ENCRYPTED|PARTITION_POLICY_ENCRYPTEDWITHINTEGRITY|PARTITION_POLICY_VERITY|PARTITION_POLICY_SIGNED|PARTITION_POLICY_ABSENT)) ==
27 (PARTITION_POLICY_UNPROTECTED|PARTITION_POLICY_ENCRYPTED|PARTITION_POLICY_ENCRYPTEDWITHINTEGRITY|PARTITION_POLICY_VERITY|PARTITION_POLICY_SIGNED|PARTITION_POLICY_ABSENT)) ? ansi_highlight_yellow() :
28 (f & _PARTITION_POLICY_USE_MASK) == PARTITION_POLICY_ABSENT ? ansi_highlight_red() :
29 !(f & PARTITION_POLICY_UNPROTECTED) ? ansi_highlight_green() : NULL;
30
31 if (d < 0)
32 r = table_add_many(table,
33 TABLE_STRING, "default",
34 TABLE_SET_COLOR, ansi_highlight_green(),
35 TABLE_STRING, q,
36 TABLE_SET_COLOR, color);
37 else
38 r = table_add_many(table,
39 TABLE_STRING, partition_designator_to_string(d),
40 TABLE_SET_COLOR, ansi_normal(),
41 TABLE_STRING, q,
42 TABLE_SET_COLOR, color);
43 if (r < 0)
44 return table_log_add_error(r);
45
46 switch (f & _PARTITION_POLICY_READ_ONLY_MASK) {
47
48 case PARTITION_POLICY_READ_ONLY_ON:
49 r = table_add_many(table, TABLE_BOOLEAN, true);
50 break;
51
52 case PARTITION_POLICY_READ_ONLY_OFF:
53 r = table_add_many(table, TABLE_BOOLEAN, false);
54 break;
55
56 default:
57 r = table_add_many(table, TABLE_EMPTY);
58 }
59 if (r < 0)
60 return table_log_add_error(r);
61
62 switch (f & _PARTITION_POLICY_GROWFS_MASK) {
63
64 case PARTITION_POLICY_GROWFS_ON:
65 r = table_add_many(table, TABLE_BOOLEAN, true);
66 break;
67
68 case PARTITION_POLICY_GROWFS_OFF:
69 r = table_add_many(table, TABLE_BOOLEAN, false);
70 break;
71
72 default:
73 r = table_add_many(table, TABLE_EMPTY);
74 }
75 if (r < 0)
76 return table_log_add_error(r);
77
78 r = table_add_many(table,
79 TABLE_STRING, isempty(t) ? "-" : t);
80 if (r < 0)
81 return table_log_add_error(r);
82
83 return 0;
84 }
85
86 int verb_image_policy(int argc, char *argv[], uintptr_t _data, void *userdata) {
87 int r;
88
89 for (int i = 1; i < argc; i++) {
90 _cleanup_(table_unrefp) Table *table = NULL;
91 _cleanup_(image_policy_freep) ImagePolicy *pbuf = NULL;
92 _cleanup_free_ char *as_string = NULL, *as_string_simplified = NULL;
93 const ImagePolicy *p;
94
95 /* NB: The magic '@' strings are not officially documented for now, since we might change
96 * around defaults (and in particular where precisely to reuse policy). We should document
97 * them once the dust has settled a bit. For now it's just useful for debugging and
98 * introspect our own defaults without guaranteeing API safety. */
99 if (streq(argv[i], "@sysext"))
100 p = &image_policy_sysext;
101 else if (streq(argv[i], "@sysext-strict"))
102 p = &image_policy_sysext_strict;
103 else if (streq(argv[i], "@confext"))
104 p = &image_policy_confext;
105 else if (streq(argv[i], "@confext-strict"))
106 p = &image_policy_confext_strict;
107 else if (streq(argv[i], "@container"))
108 p = &image_policy_container;
109 else if (streq(argv[i], "@service"))
110 p = &image_policy_service;
111 else if (streq(argv[i], "@host"))
112 p = &image_policy_host;
113 else {
114 r = image_policy_from_string(argv[i], /* graceful= */ false, &pbuf);
115 if (r < 0)
116 return log_error_errno(r, "Failed to parse image policy '%s': %m", argv[i]);
117
118 p = pbuf;
119 }
120
121 r = image_policy_to_string(p, /* simplify= */ false, &as_string);
122 if (r < 0)
123 return log_error_errno(r, "Failed to format policy '%s' as string: %m", argv[i]);
124
125 r = image_policy_to_string(p, /* simplify= */ true, &as_string_simplified);
126 if (r < 0)
127 return log_error_errno(r, "Failed to format policy '%s' as string: %m", argv[i]);
128
129 pager_open(arg_pager_flags);
130
131 if (streq(as_string, as_string_simplified))
132 printf("Analyzing policy: %s%s%s\n", ansi_highlight_magenta_underline(), as_string, ansi_normal());
133 else
134 printf("Analyzing policy: %s%s%s\n"
135 " Long form: %s%s%s\n",
136 ansi_highlight(), as_string_simplified, ansi_normal(),
137 ansi_grey(), as_string, ansi_normal());
138
139 table = table_new("partition", "mode", "read-only", "growfs", "fstype");
140 if (!table)
141 return log_oom();
142
143 table_set_ersatz_string(table, TABLE_ERSATZ_DASH);
144
145 for (PartitionDesignator d = 0; d < _PARTITION_DESIGNATOR_MAX; d++) {
146 PartitionPolicyFlags f = image_policy_get_exhaustively(p, d);
147 assert(f >= 0);
148
149 r = table_add_designator_line(table, d, f);
150 if (r < 0)
151 return r;
152 }
153
154 r = table_add_designator_line(table, _PARTITION_DESIGNATOR_INVALID, image_policy_default(p));
155 if (r < 0)
156 return r;
157
158 putc('\n', stdout);
159
160 r = table_print_or_warn(table);
161 if (r < 0)
162 return r;
163 }
164
165 return EXIT_SUCCESS;
166 }