Debian Codegraph @

src/lib/util/template.c

raw symbols SCIP
1 /*
2 Authors:
3 Pavel Březina <[email protected]>
4
5 Copyright (C) 2018 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 <regex.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <unistd.h>
26 #include <sys/stat.h>
27
28 #include "common/common.h"cxx . . $ `<file>/src/common/common.h`/
29 #include "lib/util/template.h"cxx . . $ `<file>/src/lib/util/template.h`/
30 #include "lib/util/textfile.h"cxx . . $ `<file>/src/lib/util/textfile.h`/
31 #include "lib/util/selinux.h"cxx . . $ `<file>/src/lib/util/selinux.h`/
32 #include "lib/util/string.h"cxx . . $ `<file>/src/lib/util/string.h`/
33 #include "lib/util/string_array.h"cxx . . $ `<file>/src/lib/util/string_array.h`/
34 #include "lib/util/evaluator.h"cxx . . $ `<file>/src/lib/util/evaluator.h`/
35
36 #define RE_MATCHEScxx . . $ `src/lib/util/template.c:36:9`! 12
37 #define RE_VALUEcxx . . $ `src/lib/util/template.c:37:9`! "([^{}|]{0,})"
38 #define RE_FEATUREcxx . . $ `src/lib/util/template.c:38:9`! "\"([^{}\"|]{1,})\""
39 #define RE_EXPRESSIONcxx . . $ `src/lib/util/template.c:39:9`! "([^{}|:]{1,})"
40 #define OP_RE_LINEcxx . . $ `src/lib/util/template.c:40:9`! "(continue if|stop if|include if|exclude if) " RE_EXPRESSIONcxx . . $ `src/lib/util/template.c:39:9`!
41 #define OP_RE_IMPLYcxx . . $ `src/lib/util/template.c:41:9`! "(imply) " RE_FEATUREcxx . . $ `src/lib/util/template.c:38:9`! " if " RE_EXPRESSIONcxx . . $ `src/lib/util/template.c:39:9`!
42 #define OP_RE_IFcxx . . $ `src/lib/util/template.c:42:9`! "(if) " RE_EXPRESSIONcxx . . $ `src/lib/util/template.c:39:9`! ":" RE_VALUEcxx . . $ `src/lib/util/template.c:37:9`! "(\\|" RE_VALUEcxx . . $ `src/lib/util/template.c:37:9`! "){0,1}"
43 #define OP_REcxx . . $ `src/lib/util/template.c:43:9`! "\\{(" OP_RE_LINEcxx . . $ `src/lib/util/template.c:40:9`! "|" OP_RE_IFcxx . . $ `src/lib/util/template.c:42:9`! "|" OP_RE_IMPLYcxx . . $ `src/lib/util/template.c:41:9`! ")\\}"
44
45 enum template_operatorcxx . . $ template_operator# {
46 OP_CONTINUEcxx . . $ OP_CONTINUE.,
47 OP_STOPcxx . . $ OP_STOP.,
48 OP_INCLUDEcxx . . $ OP_INCLUDE.,
49 OP_EXCLUDEcxx . . $ OP_EXCLUDE.,
50 OP_IMPLYcxx . . $ OP_IMPLY.,
51 OP_IFcxx . . $ OP_IF.,
52
53 OP_SENTINELcxx . . $ OP_SENTINEL.
54 };
55
56 void
57 template_debug_print_matchescxx . . $ template_debug_print_matches(4f6813092bf15eb5).(const char *str,
58 regmatch_tcxx . . $ regmatch_t# *matches,
59 int count)
60 {
61 #ifdef DEBUG_TEMPLATE_REGEX
62 regmatch_t *match;
63 int i;
64
65 printf("\n");
66 for (i = 0; i < count; i++) {
67 match = &matches[i];
68 if (match->rm_so == -1) {
69 continue;
70 }
71
72 printf("Match %d: %.*s\n", i, match->rm_eo - match->rm_so,
73 str + match->rm_so);
74 }
75 #else
76 return;
77 #endif
78 }
79
80 static enum template_operatorcxx . . $ template_operator#
81 template_match_get_operatorcxx . . $ template_match_get_operator(56e288ae03af3222).(const char *match_string,
82 regmatch_tcxx . . $ regmatch_t# *m)
83 {
84 size_t len;
85 int i;
86 structcxx . . $ template_match_get_operator(56e288ae03af3222).$anonymous_type_0# {
87 const char *strcxx . . $ template_match_get_operator(56e288ae03af3222).$anonymous_type_0#str.;
88 enum template_operatorcxx . . $ template_operator# opcxx . . $ template_match_get_operator(56e288ae03af3222).$anonymous_type_0#op.;
89 } operators[] = {
90 {"continue if", OP_CONTINUEcxx . . $ OP_CONTINUE.},
91 {"stop if", OP_STOPcxx . . $ OP_STOP.},
92 {"include if", OP_INCLUDEcxx . . $ OP_INCLUDE.},
93 {"exclude if", OP_EXCLUDEcxx . . $ OP_EXCLUDE.},
94 {"imply", OP_IMPLYcxx . . $ OP_IMPLY.},
95 {"if", OP_IFcxx . . $ OP_IF.},
96 {NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!, OP_SENTINELcxx . . $ OP_SENTINEL.}
97 };
98
99 for (i = 0; operators[i].strcxx . . $ template_match_get_operator(56e288ae03af3222).$anonymous_type_0#str. != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; i++) {
100 len = strlencxx . . $ strlen(751346f8406fd082).(operators[i].strcxx . . $ template_match_get_operator(56e288ae03af3222).$anonymous_type_0#str.);
101 if (strncmpcxx . . $ strncmp(fd278af2f295fa2e).(match_string + m->rm_socxx . . $ $anonymous_type_2410a2af4c2f900a_1#rm_so., operators[i].strcxx . . $ template_match_get_operator(56e288ae03af3222).$anonymous_type_0#str., len) == 0) {
102 return operators[i].opcxx . . $ template_match_get_operator(56e288ae03af3222).$anonymous_type_0#op.;
103 }
104 }
105
106 return OP_SENTINELcxx . . $ OP_SENTINEL.;
107 }
108
109 static char *
110 template_match_get_stringcxx . . $ template_match_get_string(333b265331441973).(const char *match_string,
111 regmatch_tcxx . . $ regmatch_t# *match)
112 {
113 if (match->rm_socxx . . $ $anonymous_type_2410a2af4c2f900a_1#rm_so. == -1) {
114 return strdupcxx . . $ strdup(a8b8e32347182212).("");
115 }
116
117 return strndupcxx . . $ strndup(eabdfdef77619b3d).(match_string + match->rm_socxx . . $ $anonymous_type_2410a2af4c2f900a_1#rm_so., match->rm_eocxx . . $ $anonymous_type_2410a2af4c2f900a_1#rm_eo. - match->rm_socxx . . $ $anonymous_type_2410a2af4c2f900a_1#rm_so.);
118 }
119
120 static errno_tcxx . . $ errno_t#
121 template_match_get_expressioncxx . . $ template_match_get_expression(d6076720e28bd98c).(const char *match_string,
122 enum template_operatorcxx . . $ template_operator# op,
123 regmatch_tcxx . . $ regmatch_t# *matches,
124 char **_expression)
125 {
126 regmatch_tcxx . . $ regmatch_t# *match = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
127 char *expression;
128
129 switch (op) {
130 case OP_CONTINUEcxx . . $ OP_CONTINUE.:
131 case OP_STOPcxx . . $ OP_STOP.:
132 case OP_INCLUDEcxx . . $ OP_INCLUDE.:
133 case OP_EXCLUDEcxx . . $ OP_EXCLUDE.:
134 match = &matches[3];
135 break;
136 case OP_IMPLYcxx . . $ OP_IMPLY.:
137 match = &matches[11];
138 break;
139 case OP_IFcxx . . $ OP_IF.:
140 match = &matches[5];
141 break;
142 case OP_SENTINELcxx . . $ OP_SENTINEL.:
143 ERRORcxx . . $ `src/common/common.h:71:9`!("Invalid operator!");
144 return EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!;
145 }
146
147 expression = template_match_get_stringcxx . . $ template_match_get_string(333b265331441973).(match_string, match);
148 if (expression == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) {
149 return ENOMEMcxx . . $ `<external>/8987895939074593727/errno-base.h:16:9`!;
150 }
151
152 *_expression = expression;
153
154 return EOKcxx . . $ `src/common/errno_t.h:34:9`!;
155 }
156
157 static errno_tcxx . . $ errno_t#
158 template_match_get_valuescxx . . $ template_match_get_values(8194cd16e3693118).(const char *match_string,
159 enum template_operatorcxx . . $ template_operator# op,
160 regmatch_tcxx . . $ regmatch_t# *matches,
161 char **_if_true,
162 char **_if_false,
163 char **_value)
164 {
165 char *if_true;
166 char *if_false;
167 char *value;
168
169 switch (op) {
170 case OP_CONTINUEcxx . . $ OP_CONTINUE.:
171 case OP_STOPcxx . . $ OP_STOP.:
172 case OP_INCLUDEcxx . . $ OP_INCLUDE.:
173 case OP_EXCLUDEcxx . . $ OP_EXCLUDE.:
174 *_if_true = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
175 *_if_false = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
176 *_value = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
177 break;
178 case OP_IMPLYcxx . . $ OP_IMPLY.:
179 value = template_match_get_stringcxx . . $ template_match_get_string(333b265331441973).(match_string, &matches[10]);
180 if (value == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) {
181 return ENOMEMcxx . . $ `<external>/8987895939074593727/errno-base.h:16:9`!;
182 }
183
184 *_if_true = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
185 *_if_false = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
186 *_value = value;
187 return EOKcxx . . $ `src/common/errno_t.h:34:9`!;
188 case OP_IFcxx . . $ OP_IF.:
189 if_true = template_match_get_stringcxx . . $ template_match_get_string(333b265331441973).(match_string, &matches[6]);
190 if (if_true == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) {
191 return ENOMEMcxx . . $ `<external>/8987895939074593727/errno-base.h:16:9`!;
192 }
193
194 if_false = template_match_get_stringcxx . . $ template_match_get_string(333b265331441973).(match_string, &matches[8]);
195 if (if_false == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) {
196 freecxx . . $ free(5d22bdacc48458e8).(if_true);
197 return ENOMEMcxx . . $ `<external>/8987895939074593727/errno-base.h:16:9`!;
198 }
199
200 *_if_true = if_true;
201 *_if_false = if_false;
202 *_value = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
203 break;
204 case OP_SENTINELcxx . . $ OP_SENTINEL.:
205 ERRORcxx . . $ `src/common/common.h:71:9`!("Invalid operator!");
206 return EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!;
207 }
208
209 return EOKcxx . . $ `src/common/errno_t.h:34:9`!;
210 }
211
212 static errno_tcxx . . $ errno_t#
213 template_match_replacecxx . . $ template_match_replace(8df99cd671000811).(char ***features,
214 char *beginning,
215 char *match_string,
216 regmatch_tcxx . . $ regmatch_t# *match,
217 enum template_operatorcxx . . $ template_operator# op,
218 const char *expression,
219 const char *if_true,
220 const char *if_false,
221 const char *value)
222 {
223 const char *replacement;
224 boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! enabled;
225 int ret;
226
227 ret = evaluatecxx . . $ evaluate(46899b4d75686a).(expression, (const char **)*features, &enabled);
228 if (ret != EOKcxx . . $ `src/common/errno_t.h:34:9`!) {
229 return ret;
230 }
231
232 switch (op) {
233 case OP_CONTINUEcxx . . $ OP_CONTINUE.:
234 if (enabled) {
235 string_remove_linecxx . . $ string_remove_line(dc09b413e6ab05e).(beginning, match_string, match->rm_socxx . . $ $anonymous_type_2410a2af4c2f900a_1#rm_so.);
236 break;
237 }
238
239 string_remove_remaindercxx . . $ string_remove_remainder(a14cc78460091691).(match_string, match->rm_socxx . . $ $anonymous_type_2410a2af4c2f900a_1#rm_so.);
240 break;
241 case OP_STOPcxx . . $ OP_STOP.:
242 if (!enabled) {
243 string_remove_linecxx . . $ string_remove_line(dc09b413e6ab05e).(beginning, match_string, match->rm_socxx . . $ $anonymous_type_2410a2af4c2f900a_1#rm_so.);
244 break;
245 }
246
247 string_remove_remaindercxx . . $ string_remove_remainder(a14cc78460091691).(match_string, match->rm_socxx . . $ $anonymous_type_2410a2af4c2f900a_1#rm_so.);
248 break;
249 case OP_INCLUDEcxx . . $ OP_INCLUDE.:
250 if (enabled) {
251 string_remove_rangecxx . . $ string_remove_range(223f2ae9eb7061ff).(match_string, match->rm_socxx . . $ $anonymous_type_2410a2af4c2f900a_1#rm_so., match->rm_eocxx . . $ $anonymous_type_2410a2af4c2f900a_1#rm_eo.);
252 break;
253 }
254
255 string_remove_linecxx . . $ string_remove_line(dc09b413e6ab05e).(beginning, match_string, match->rm_socxx . . $ $anonymous_type_2410a2af4c2f900a_1#rm_so.);
256 break;
257 case OP_EXCLUDEcxx . . $ OP_EXCLUDE.:
258 if (!enabled) {
259 string_remove_rangecxx . . $ string_remove_range(223f2ae9eb7061ff).(match_string, match->rm_socxx . . $ $anonymous_type_2410a2af4c2f900a_1#rm_so., match->rm_eocxx . . $ $anonymous_type_2410a2af4c2f900a_1#rm_eo.);
260 break;
261 }
262
263 string_remove_linecxx . . $ string_remove_line(dc09b413e6ab05e).(beginning, match_string, match->rm_socxx . . $ $anonymous_type_2410a2af4c2f900a_1#rm_so.);
264 break;
265 case OP_IMPLYcxx . . $ OP_IMPLY.:
266 if (enabled) {
267 *features = string_array_add_valuecxx . . $ string_array_add_value(db56f6d53b8c0a98).(*features, value, truecxx . . $ `<external>/13015445228558014418/stdbool.h:37:9`!);
268 if (*features == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) {
269 return ENOMEMcxx . . $ `<external>/8987895939074593727/errno-base.h:16:9`!;
270 }
271 }
272
273 string_remove_linecxx . . $ string_remove_line(dc09b413e6ab05e).(beginning, match_string, match->rm_socxx . . $ $anonymous_type_2410a2af4c2f900a_1#rm_so.);
274 break;
275 case OP_IFcxx . . $ OP_IF.:
276 replacement = enabled ? if_true : if_false;
277 string_replace_positioncxx . . $ string_replace_position(d10b6c01f1aebf31).(match_string, match->rm_socxx . . $ $anonymous_type_2410a2af4c2f900a_1#rm_so., match->rm_eocxx . . $ $anonymous_type_2410a2af4c2f900a_1#rm_eo.,
278 replacement);
279 break;
280 case OP_SENTINELcxx . . $ OP_SENTINEL.:
281 ERRORcxx . . $ `src/common/common.h:71:9`!("Invalid operator!");
282 return EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!;
283 }
284
285 return EOKcxx . . $ `src/common/errno_t.h:34:9`!;
286 }
287
288 /**
289 * Supported operators:
290 *
291 * {continue if "with-smartcard"}
292 * {stop if "with-smartcard"}
293 * {exclude if "with-smartcard"}
294 * {include if "with-smartcard"}
295 * {if "with-smartcard":true|false}
296 * {if "with-smartcard":true}
297 * {if not "with-smartcard":true}
298 * {if not "with-smartcard":true|false}
299 *
300 * Match groups for template regular expression are as follows:
301 *
302 * Match 0: {continue if "with-smartcard"}
303 * Match 1: continue if "with-smartcard"
304 * Match 2: continue if
305 * Match 3: "with-smartcard"
306 *
307 * Match 0: {stop if "with-smartcard"}
308 * Match 1: stop if "with-smartcard"
309 * Match 2: stop if
310 * Match 3: "with-smartcard"
311 *
312 * Match 0: {exclude if "with-smartcard"}
313 * Match 1: exclude if "with-smartcard"
314 * Match 2: exclude if
315 * Match 3: "with-smartcard"
316 *
317 * Match 0: {include if "with-smartcard"}
318 * Match 1: include if "with-smartcard"
319 * Match 2: include if
320 * Match 3: "with-smartcard"
321 *
322 * Match 0: {if "with-smartcard":true|false}
323 * Match 1: if "with-smartcard":true|false
324 * Match 4: if
325 * Match 5: "with-smartcard"
326 * Match 6: true
327 * Match 7: |false
328 * Match 8: false
329 *
330 * Match 0: {if "with-smartcard":true}
331 * Match 1: if "with-smartcard":true
332 * Match 4: if
333 * Match 5: "with-smartcard"
334 * Match 6: true
335 *
336 * Match 0: {if not "with-smartcard":true}
337 * Match 1: if not "with-smartcard":true
338 * Match 4: if
339 * Match 5: not "with-smartcard"
340 * Match 6: true
341 *
342 * Match 0: {if not "with-smartcard":true|false}
343 * Match 1: if not "with-smartcard":true|false
344 * Match 4: if
345 * Match 5: not "with-smartcard"
346 * Match 6: true
347 * Match 7: |false
348 * Match 8: false
349 *
350 * Match 0: {imply "with-smartcard" if "with-smartcard-required"}
351 * Match 1: imply "with-smartcard" if "with-smartcard-required"
352 * Match 9: imply
353 * Match 10: with-smartcard
354 * Match 11: "with-smartcard-required"
355 *
356 */
357 static errno_tcxx . . $ errno_t#
358 template_process_matchescxx . . $ template_process_matches(805e4a2c2acfda41).(const char *match_string,
359 regmatch_tcxx . . $ regmatch_t# *m,
360 enum template_operatorcxx . . $ template_operator# *_op,
361 char **_expression,
362 char **_if_true,
363 char **_if_false,
364 char **_value)
365 {
366 enum template_operatorcxx . . $ template_operator# op;
367 char *if_false;
368 char *if_true;
369 char *expression;
370 char *value;
371 errno_tcxx . . $ errno_t# ret;
372
373 template_debug_print_matchescxx . . $ template_debug_print_matches(4f6813092bf15eb5).(match_string, m, RE_MATCHEScxx . . $ `src/lib/util/template.c:36:9`!);
374
375 op = template_match_get_operatorcxx . . $ template_match_get_operator(56e288ae03af3222).(match_string, &m[1]);
376 if (op == OP_SENTINELcxx . . $ OP_SENTINEL.) {
377 return EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!;
378 }
379
380 ret = template_match_get_expressioncxx . . $ template_match_get_expression(d6076720e28bd98c).(match_string, op, m, &expression);
381 if (ret != EOKcxx . . $ `src/common/errno_t.h:34:9`!) {
382 return ret;
383 }
384
385 ret = template_match_get_valuescxx . . $ template_match_get_values(8194cd16e3693118).(match_string, op, m,
386 &if_true, &if_false, &value);
387 if (ret != EOKcxx . . $ `src/common/errno_t.h:34:9`!) {
388 freecxx . . $ free(5d22bdacc48458e8).(expression);
389 return ret;
390 }
391
392 if (_op != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) {
393 *_op = op;
394 }
395
396 if (_expression != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) {
397 *_expression = expression;
398 } else {
399 freecxx . . $ free(5d22bdacc48458e8).(expression);
400 }
401
402 if (_if_true != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) {
403 *_if_true = if_true;
404 } else {
405 freecxx . . $ free(5d22bdacc48458e8).(if_true);
406 }
407
408 if (_if_false != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) {
409 *_if_false = if_false;
410 } else {
411 freecxx . . $ free(5d22bdacc48458e8).(if_false);
412 }
413
414 if (_value != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) {
415 *_value = value;
416 } else {
417 freecxx . . $ free(5d22bdacc48458e8).(value);
418 }
419
420 return EOKcxx . . $ `src/common/errno_t.h:34:9`!;
421 }
422
423 static errno_tcxx . . $ errno_t#
424 template_process_operatorscxx . . $ template_process_operators(e7ab124e6eb95f0).(const char **features,
425 char *content)
426 {
427
428 regex_tcxx . . $ regex_t# regex;
429 char *match_string;
430 size_t orig_len;
431 regmatch_tcxx . . $ regmatch_t# m[RE_MATCHEScxx . . $ `src/lib/util/template.c:36:9`!];
432 enum template_operatorcxx . . $ template_operator# op;
433 char *if_false = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
434 char *if_true = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
435 char *expression = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
436 char *value = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
437 char **features_copy;
438 errno_tcxx . . $ errno_t# ret;
439 int reret;
440
441 features_copy = string_array_copycxx . . $ string_array_copy(55ba949d88614acb).((char**)features, truecxx . . $ `<external>/13015445228558014418/stdbool.h:37:9`!);
442 if (features_copy == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) {
443 return ENOMEMcxx . . $ `<external>/8987895939074593727/errno-base.h:16:9`!;
444 }
445
446 orig_len = strlencxx . . $ strlen(751346f8406fd082).(content);
447
448 reret = regcompcxx . . $ regcomp(aa05013d3db24b3e).(&regex, OP_REcxx . . $ `src/lib/util/template.c:43:9`!, REG_EXTENDEDcxx . . $ `<external>/18190116149591951179/regex.h:310:9`! | REG_NEWLINEcxx . . $ `<external>/18190116149591951179/regex.h:319:9`!);
449 if (reret != REG_NOERRORcxx . . $ `<external>/18190116149591951179/regex.h:376:9`!) {
450 ERRORcxx . . $ `src/common/common.h:71:9`!("Unable to compile regular expression: regex error %d", reret);
451 string_array_freecxx . . $ string_array_free(3ed91490e421178f).(features_copy);
452 return EFAULTcxx . . $ `<external>/8987895939074593727/errno-base.h:18:9`!;
453 }
454
455 match_string = content;
456 while ((reret = regexeccxx . . $ regexec(7b724a18a85d26ac).(&regex, match_string, RE_MATCHEScxx . . $ `src/lib/util/template.c:36:9`!, m, 0)) == REG_NOERRORcxx . . $ `<external>/18190116149591951179/regex.h:376:9`!) {
457 ret = template_process_matchescxx . . $ template_process_matches(805e4a2c2acfda41).(match_string, m, &op, &expression,
458 &if_true, &if_false, &value);
459 if (ret != EOKcxx . . $ `src/common/errno_t.h:34:9`!) {
460 ERRORcxx . . $ `src/common/common.h:71:9`!("Unable to process match [%d]: %s", ret, strerror(ret));
461 goto done;
462 }
463
464 ret = template_match_replacecxx . . $ template_match_replace(8df99cd671000811).(&features_copy, content, match_string,
465 &m[0], op, expression,
466 if_true, if_false, value);
467
468 if (expression != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) {
469 freecxx . . $ free(5d22bdacc48458e8).(expression);
470 }
471
472 if (if_true != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) {
473 freecxx . . $ free(5d22bdacc48458e8).(if_true);
474 }
475
476 if (if_false != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) {
477 freecxx . . $ free(5d22bdacc48458e8).(if_false);
478 }
479
480 if (value != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) {
481 freecxx . . $ free(5d22bdacc48458e8).(value);
482 }
483
484 if (ret != EOKcxx . . $ `src/common/errno_t.h:34:9`!) {
485 ERRORcxx . . $ `src/common/common.h:71:9`!("Unable to process operator [%d]: %s", ret, strerror(ret));
486 goto done;
487 }
488
489 /* Since the whole line could have been removed, we have to find first
490 * non-zero position. */
491 match_string += m[0].rm_eocxx . . $ $anonymous_type_2410a2af4c2f900a_1#rm_eo.;
492 while (*match_string == '\0' && match_string - content < orig_len) {
493 match_string++;
494 }
495 }
496
497 string_replace_shakecxx . . $ string_replace_shake(a14cc78460091691).(content, orig_len);
498
499 if (reret != REG_NOMATCHcxx . . $ `<external>/18190116149591951179/regex.h:377:9`!) {
500 ERRORcxx . . $ `src/common/common.h:71:9`!("Unable to search string: regex error %d", reret);
501 ret = EFAULTcxx . . $ `<external>/8987895939074593727/errno-base.h:18:9`!;
502 goto done;
503 }
504
505 ret = EOKcxx . . $ `src/common/errno_t.h:34:9`!;
506
507 done:
508 string_array_freecxx . . $ string_array_free(3ed91490e421178f).(features_copy);
509 regfreecxx . . $ regfree(b1814a8fd6af504b).(&regex);
510 return ret;
511 }
512
513 char *
514 template_generatecxx . . $ template_generate(e6b3452e8157deef).(const char *template,
515 const char **features)
516 {
517 char **lines;
518 char *output;
519 errno_tcxx . . $ errno_t# ret;
520
521 if (template == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) {
522 return strdupcxx . . $ strdup(a8b8e32347182212).("");
523 }
524
525 output = strdupcxx . . $ strdup(a8b8e32347182212).(template);
526 if (output == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) {
527 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
528 }
529
530 ret = template_process_operatorscxx . . $ template_process_operators(e7ab124e6eb95f0).(features, output);
531 if (ret != EOKcxx . . $ `src/common/errno_t.h:34:9`!) {
532 ERRORcxx . . $ `src/common/common.h:71:9`!("Unable to generate template [%d]: %s", ret, strerror(ret));
533 freecxx . . $ free(5d22bdacc48458e8).(output);
534 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
535 }
536
537 /* Trim lines */
538 lines = string_explodecxx . . $ string_explode(4a6f5072951f8444).(output, '\n', STRING_EXPLODE_TRIM_RIGHTcxx . . $ `src/lib/util/string.h:30:9`!);
539 freecxx . . $ free(5d22bdacc48458e8).(output);
540 if (lines == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) {
541 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
542 }
543
544 output = string_implodecxx . . $ string_implode(427dff5c35d3d96b).((const char **)lines, '\n');
545 string_array_freecxx . . $ string_array_free(3ed91490e421178f).(lines);
546
547 return output;
548 }
549
550 static char *
551 template_generate_preamblecxx . . $ template_generate_preamble(1a2b1742f4ee1b69).()
552 {
553 const char *preamble =
554 "# Generated by authselect\n"
555 "# Do not modify this file manually, use authselect instead. Any user changes will be overwritten.\n"
556 "# You can stop authselect from managing your configuration by calling 'authselect opt-out'.\n"
557 "# See authselect(8) for more details.\n\n";
558
559 return strdupcxx . . $ strdup(a8b8e32347182212).(preamble);
560 }
561
562 errno_tcxx . . $ errno_t#
563 template_list_features_from_expressioncxx . . $ template_list_features_from_expression(89de029539fc85c2).(const char *expression, char ***features)
564 {
565 regmatch_tcxx . . $ regmatch_t# m[RE_MATCHEScxx . . $ `src/lib/util/template.c:36:9`!];
566 const char *match_string;
567 regex_tcxx . . $ regex_t# regex;
568 errno_tcxx . . $ errno_t# ret;
569 int reret;
570
571 reret = regcompcxx . . $ regcomp(aa05013d3db24b3e).(&regex, "[^\"]{0,}" RE_FEATUREcxx . . $ `src/lib/util/template.c:38:9`!, REG_EXTENDEDcxx . . $ `<external>/18190116149591951179/regex.h:310:9`! | REG_NEWLINEcxx . . $ `<external>/18190116149591951179/regex.h:319:9`!);
572 if (reret != REG_NOERRORcxx . . $ `<external>/18190116149591951179/regex.h:376:9`!) {
573 ERRORcxx . . $ `src/common/common.h:71:9`!("Unable to compile regular expression: regex error %d", reret);
574 return EFAULTcxx . . $ `<external>/8987895939074593727/errno-base.h:18:9`!;
575 }
576
577 match_string = expression;
578 while ((reret = regexeccxx . . $ regexec(7b724a18a85d26ac).(&regex, match_string, RE_MATCHEScxx . . $ `src/lib/util/template.c:36:9`!, m, 0)) == REG_NOERRORcxx . . $ `<external>/18190116149591951179/regex.h:376:9`!) {
579 if (reret != EOKcxx . . $ `src/common/errno_t.h:34:9`!) {
580 ERRORcxx . . $ `src/common/common.h:71:9`!("Unable to find new match: regex error %d", reret);
581 ret = EFAULTcxx . . $ `<external>/8987895939074593727/errno-base.h:18:9`!;
582 goto done;
583 }
584 *features = string_array_add_value_safecxx . . $ string_array_add_value_safe(dfc2d168ddb15878).(*features,
585 &match_string[m[1].rm_socxx . . $ $anonymous_type_2410a2af4c2f900a_1#rm_so.],
586 m[1].rm_eocxx . . $ $anonymous_type_2410a2af4c2f900a_1#rm_eo. - m[1].rm_socxx . . $ $anonymous_type_2410a2af4c2f900a_1#rm_so.,
587 truecxx . . $ `<external>/13015445228558014418/stdbool.h:37:9`!);
588 if (*features == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) {
589 ret = ENOMEMcxx . . $ `<external>/8987895939074593727/errno-base.h:16:9`!;
590 goto done;
591 }
592 match_string += m[0].rm_eocxx . . $ $anonymous_type_2410a2af4c2f900a_1#rm_eo.;
593 }
594
595 ret = EOKcxx . . $ `src/common/errno_t.h:34:9`!;
596
597 done:
598 regfreecxx . . $ regfree(b1814a8fd6af504b).(&regex);
599 return ret;
600 }
601
602 char **
603 template_list_featurescxx . . $ template_list_features(9945f7050fb66ac).(const char *template)
604 {
605 regmatch_tcxx . . $ regmatch_t# m[RE_MATCHEScxx . . $ `src/lib/util/template.c:36:9`!];
606 const char *match_string;
607 char **features;
608 char *expression;
609 regex_tcxx . . $ regex_t# regex;
610 errno_tcxx . . $ errno_t# ret;
611 int reret;
612
613 features = string_array_createcxx . . $ string_array_create(4d3fbc76ec7ad0ba).(10);
614 if (features == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) {
615 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
616 }
617
618 if (template == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) {
619 return features;
620 }
621
622 reret = regcompcxx . . $ regcomp(aa05013d3db24b3e).(&regex, OP_REcxx . . $ `src/lib/util/template.c:43:9`!, REG_EXTENDEDcxx . . $ `<external>/18190116149591951179/regex.h:310:9`! | REG_NEWLINEcxx . . $ `<external>/18190116149591951179/regex.h:319:9`!);
623 if (reret != REG_NOERRORcxx . . $ `<external>/18190116149591951179/regex.h:376:9`!) {
624 ERRORcxx . . $ `src/common/common.h:71:9`!("Unable to compile regular expression: regex error %d", reret);
625 ret = EFAULTcxx . . $ `<external>/8987895939074593727/errno-base.h:18:9`!;
626 goto done;
627 }
628
629 match_string = template;
630 while ((reret = regexeccxx . . $ regexec(7b724a18a85d26ac).(&regex, match_string, RE_MATCHEScxx . . $ `src/lib/util/template.c:36:9`!, m, 0)) == REG_NOERRORcxx . . $ `<external>/18190116149591951179/regex.h:376:9`!) {
631 ret = template_process_matchescxx . . $ template_process_matches(805e4a2c2acfda41).(match_string, m, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!, &expression,
632 NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!);
633 if (ret != EOKcxx . . $ `src/common/errno_t.h:34:9`!) {
634 ERRORcxx . . $ `src/common/common.h:71:9`!("Unable to process match [%d]: %s", ret, strerror(ret));
635 goto done;
636 }
637
638 if (expression) {
639 ret = template_list_features_from_expressioncxx . . $ template_list_features_from_expression(89de029539fc85c2).(expression, &features);
640 freecxx . . $ free(5d22bdacc48458e8).(expression);
641 if (ret != EOKcxx . . $ `src/common/errno_t.h:34:9`!) {
642 goto done;
643 }
644 }
645
646 match_string += m[0].rm_eocxx . . $ $anonymous_type_2410a2af4c2f900a_1#rm_eo.;
647 }
648
649 if (reret != REG_NOMATCHcxx . . $ `<external>/18190116149591951179/regex.h:377:9`!) {
650 ERRORcxx . . $ `src/common/common.h:71:9`!("Unable to search string: regex error %d", reret);
651 ret = EFAULTcxx . . $ `<external>/8987895939074593727/errno-base.h:18:9`!;
652 goto done;
653 }
654
655 ret = EOKcxx . . $ `src/common/errno_t.h:34:9`!;
656
657 done:
658 if (ret != EOKcxx . . $ `src/common/errno_t.h:34:9`!) {
659 string_array_freecxx . . $ string_array_free(3ed91490e421178f).(features);
660 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
661 }
662
663 regfreecxx . . $ regfree(b1814a8fd6af504b).(&regex);
664 return features;
665 }
666
667 errno_tcxx . . $ errno_t#
668 template_writecxx . . $ template_write(54fb1e1f0004e0cf).(const char *filepath,
669 const char *content,
670 mode_tcxx . . $ mode_t# mode)
671 {
672 char *preamble;
673 char *output;
674 errno_tcxx . . $ errno_t# ret;
675
676 preamble = template_generate_preamblecxx . . $ template_generate_preamble(1a2b1742f4ee1b69).();
677 if (content == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) {
678 output = preamble;
679 } else {
680 output = formatcxx . . $ format(a849077ff1b8d812).("%s%s", preamble, content);
681 freecxx . . $ free(5d22bdacc48458e8).(preamble);
682 }
683
684 if (output == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) {
685 return ENOMEMcxx . . $ `<external>/8987895939074593727/errno-base.h:16:9`!;
686 }
687
688 ret = textfile_writecxx . . $ textfile_write(54fb1e1f0004e0cf).(filepath, output, mode);
689 freecxx . . $ free(5d22bdacc48458e8).(output);
690
691 return ret;
692 }
693
694 errno_tcxx . . $ errno_t#
695 template_write_temporarycxx . . $ template_write_temporary(5071347881743dfd).(const char *filepath,
696 const char *content,
697 mode_tcxx . . $ mode_t# mode,
698 char **_tmpfile)
699 {
700 char *tmpfile;
701 errno_tcxx . . $ errno_t# ret;
702
703 ret = selinux_mkstemp_forcxx . . $ selinux_mkstemp_for(d26d230207164ac8).(filepath, mode, &tmpfile);
704 if (ret != EOKcxx . . $ `src/common/errno_t.h:34:9`!) {
705 ERRORcxx . . $ `src/common/common.h:71:9`!("Unable to create temporary file for [%s] [%d]: %s",
706 filepath, ret, strerror(ret));
707 return ret;
708 }
709
710 ret = template_writecxx . . $ template_write(54fb1e1f0004e0cf).(tmpfile, content, mode);
711 if (ret != EOKcxx . . $ `src/common/errno_t.h:34:9`!) {
712 freecxx . . $ free(5d22bdacc48458e8).(tmpfile);
713 return ret;
714 }
715
716 *_tmpfile = tmpfile;
717
718 return EOKcxx . . $ `src/common/errno_t.h:34:9`!;
719 }
720
721 boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`!
722 template_validate_written_contentcxx . . $ template_validate_written_content(b7a7aa4a824b7595).(const char *file_content,
723 const char *expected)
724 {
725 char *processed_expected = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
726 char *processed_content = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
727 char **lines_expected = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
728 char **lines_content = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
729 boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! result = falsecxx . . $ `<external>/13015445228558014418/stdbool.h:38:9`!;
730
731 /* We ignore changes in comments, empty lines and surrounding spaces since
732 * they do not affect the resulting configuration. */
733
734 lines_content = string_explodecxx . . $ string_explode(4a6f5072951f8444).(file_content, '\n', STRING_EXPLODE_ALLcxx . . $ `src/lib/util/string.h:33:9`!);
735 if (lines_content == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) {
736 goto done;
737 }
738
739 processed_content = string_implodecxx . . $ string_implode(427dff5c35d3d96b).((const char **)lines_content, '\n');
740 if (processed_content == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) {
741 goto done;
742 }
743
744 lines_expected = string_explodecxx . . $ string_explode(4a6f5072951f8444).(expected, '\n', STRING_EXPLODE_ALLcxx . . $ `src/lib/util/string.h:33:9`!);
745 if (lines_expected == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) {
746 goto done;
747 }
748
749 processed_expected = string_implodecxx . . $ string_implode(427dff5c35d3d96b).((const char **)lines_expected, '\n');
750 if (processed_expected == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) {
751 goto done;
752 }
753
754 result = strcmpcxx . . $ strcmp(5ae29d726ae57f88).(processed_content, processed_expected) == 0;
755
756 done:
757 string_array_freecxx . . $ string_array_free(3ed91490e421178f).(lines_expected);
758 string_array_freecxx . . $ string_array_free(3ed91490e421178f).(lines_content);
759 freecxx . . $ free(5d22bdacc48458e8).(processed_expected);
760 freecxx . . $ free(5d22bdacc48458e8).(processed_content);
761
762 return result;
763 }