Debian Codegraph @

src/lib/profiles/read.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 <errno.h>
22 #include <fcntl.h>
23 #include <string.h>
24 #include <stdlib.h>
25 #include <unistd.h>
26
27 #include "common/common.h"cxx . . $ `<file>/src/common/common.h`/
28 #include "lib/constants.h"cxx . . $ `<file>/src/lib/constants.h`/
29 #include "lib/profiles/profiles.h"cxx . . $ `<file>/src/lib/profiles/profiles.h`/
30 #include "lib/files/files.h"cxx . . $ `<file>/src/lib/files/files.h`/
31 #include "lib/util/util.h"cxx . . $ `<file>/src/lib/util/util.h`/
32
33 static const char **
34 authselect_profile_locationscxx . . $ authselect_profile_locations(4e43848a9479fba2).(const char *id,
35 enum authselect_profile_typecxx . . $ authselect_profile_type# type)
36 {
37 static const char *type_default[] = {DIR_DEFAULT_PROFILEScxx . . $ `src/lib/paths.h:70:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!};
38 static const char *type_vendor[] = {DIR_VENDOR_PROFILEScxx . . $ `src/lib/paths.h:71:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!};
39 static const char *type_custom[] = {DIR_CUSTOM_PROFILEScxx . . $ `src/lib/paths.h:72:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!};
40 static const char *type_any[] = {
41 DIR_VENDOR_PROFILEScxx . . $ `src/lib/paths.h:71:9`!,
42 DIR_DEFAULT_PROFILEScxx . . $ `src/lib/paths.h:70:9`!,
43 NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!
44 };
45
46 switch (type) {
47 case AUTHSELECT_PROFILE_DEFAULTcxx . . $ AUTHSELECT_PROFILE_DEFAULT.:
48 return type_default;
49 case AUTHSELECT_PROFILE_VENDORcxx . . $ AUTHSELECT_PROFILE_VENDOR.:
50 return type_vendor;
51 case AUTHSELECT_PROFILE_CUSTOMcxx . . $ AUTHSELECT_PROFILE_CUSTOM.:
52 return type_custom;
53 case AUTHSELECT_PROFILE_ANYcxx . . $ AUTHSELECT_PROFILE_ANY.:
54 if (authselect_profile_is_customcxx . . $ authselect_profile_is_custom(c5f7da30cbc0f4e9).(id)) {
55 return type_custom;
56 }
57
58 return type_any;
59 }
60
61 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
62 }
63
64 static errno_tcxx . . $ errno_t#
65 authselect_profile_open_locationcxx . . $ authselect_profile_open_location(7436bf38bddd805d).(const char *path,
66 int *_dirfd)
67 {
68 int dirfd;
69 errno_tcxx . . $ errno_t# ret;
70
71 dirfd = opencxx . . $ open(e156e3aa1e5c4ad1).(path, O_DIRECTORYcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:140:10`! | O_RDONLYcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:43:9`!);
72 if (dirfd == -1) {
73 ret = errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!;
74 /* To silence static analyzers that assumes that errno can be 0 here. */
75 if (ret == EOKcxx . . $ `src/common/errno_t.h:34:9`!) {
76 ret = EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!;
77 }
78
79 if (ret == ENOENTcxx . . $ `<external>/8987895939074593727/errno-base.h:6:9`!) {
80 return ENOENTcxx . . $ `<external>/8987895939074593727/errno-base.h:6:9`!;
81 }
82
83 ERRORcxx . . $ `src/common/common.h:71:9`!("Unable to open directory [%s] [%d]: %s",
84 path, ret, strerror(ret));
85 return ret;
86 }
87
88 *_dirfd = dirfd;
89
90 return EOKcxx . . $ `src/common/errno_t.h:34:9`!;
91 }
92
93 static errno_tcxx . . $ errno_t#
94 authselect_profile_opencxx . . $ authselect_profile_open(f5c3b45c724d5b9a).(const char *id,
95 enum authselect_profile_typecxx . . $ authselect_profile_type# type,
96 char **_location,
97 int *_dirfd)
98 {
99 const char **locations;
100 const char *name;
101 char *location;
102 errno_tcxx . . $ errno_t# ret;
103 int dirfd;
104 int i;
105
106 INFOcxx . . $ `src/common/common.h:63:9`!("Looking up profile [%s]", id);
107
108 locations = authselect_profile_locationscxx . . $ authselect_profile_locations(4e43848a9479fba2).(id, type);
109 if (locations == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) {
110 ERRORcxx . . $ `src/common/common.h:71:9`!("Locations array is NULL");
111 return EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!;
112 }
113
114 name = authselect_profile_parse_customcxx . . $ authselect_profile_parse_custom(fb58d2e09a2a1ce6).(id);
115 name = name == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`! ? id : name;
116
117 for (i = 0; locations[i] != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; i++) {
118 location = formatcxx . . $ format(a849077ff1b8d812).("%s/%s", locations[i], name);
119 if (location == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) {
120 return ENOMEMcxx . . $ `<external>/8987895939074593727/errno-base.h:16:9`!;
121 }
122
123 ret = authselect_profile_open_locationcxx . . $ authselect_profile_open_location(7436bf38bddd805d).(location, &dirfd);
124 if (ret == ENOENTcxx . . $ `<external>/8987895939074593727/errno-base.h:6:9`!) {
125 freecxx . . $ free(5d22bdacc48458e8).(location);
126 continue;
127 } else if (ret != EOKcxx . . $ `src/common/errno_t.h:34:9`!) {
128 freecxx . . $ free(5d22bdacc48458e8).(location);
129 return ret;
130 }
131
132 if (strcmpcxx . . $ strcmp(5ae29d726ae57f88).(locations[i], DIR_CUSTOM_PROFILEScxx . . $ `src/lib/paths.h:72:9`!) == 0) {
133 INFOcxx . . $ `src/common/common.h:63:9`!("Profile [%s] is a custom profile", id);
134 } else if (strcmpcxx . . $ strcmp(5ae29d726ae57f88).(locations[i], DIR_VENDOR_PROFILEScxx . . $ `src/lib/paths.h:71:9`!) == 0) {
135 INFOcxx . . $ `src/common/common.h:63:9`!("Profile [%s] is a vendor profile", id);
136 } else {
137 INFOcxx . . $ `src/common/common.h:63:9`!("Profile [%s] is a default profile", id);
138 }
139
140 INFOcxx . . $ `src/common/common.h:63:9`!("Profile [%s] found at [%s]", id, location);
141
142 *_location = location;
143 *_dirfd = dirfd;
144
145 return EOKcxx . . $ `src/common/errno_t.h:34:9`!;
146 }
147
148 INFOcxx . . $ `src/common/common.h:63:9`!("Profile [%s] was not found", id);
149
150 return ENOENTcxx . . $ `<external>/8987895939074593727/errno-base.h:6:9`!;
151 }
152
153 static errno_tcxx . . $ errno_t#
154 authselect_profile_read_metacxx . . $ authselect_profile_read_meta(819f5aabcc6a9973).(const char *location,
155 int dirfd,
156 const char *filename,
157 boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! mandatory,
158 char **_content)
159 {
160 errno_tcxx . . $ errno_t# ret;
161
162 INFOcxx . . $ `src/common/common.h:63:9`!("Reading file [%s/%s]", location, filename);
163
164 ret = textfile_read_dirfdcxx . . $ textfile_read_dirfd(4ff8903a8e11e2fc).(dirfd, location, filename,
165 AUTHSELECT_FILE_SIZE_LIMITcxx . . $ `src/lib/constants.h:31:9`!, _content);
166 if (ret != EOKcxx . . $ `src/common/errno_t.h:34:9`!) {
167 if (mandatory) {
168 ERRORcxx . . $ `src/common/common.h:71:9`!("Unable to read file [%s/%s] [%d]: %s",
169 location, filename, ret, strerror(ret));
170 return ret;
171 }
172
173 WARNcxx . . $ `src/common/common.h:67:9`!("Unable to read file [%s/%s] [%d]: %s",
174 location, filename, ret, strerror(ret));
175
176 *_content = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
177 return EOKcxx . . $ `src/common/errno_t.h:34:9`!;
178 }
179
180 return EOKcxx . . $ `src/common/errno_t.h:34:9`!;
181 }
182
183 static errno_tcxx . . $ errno_t#
184 authselect_profile_read_readmecxx . . $ authselect_profile_read_readme(c4c60c29bcbf93b2).(const char *location,
185 int dirfd,
186 char **_name,
187 char **_description)
188 {
189 size_t lineend;
190 char *trimmed = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
191 char *readme = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
192 char *name = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
193 errno_tcxx . . $ errno_t# ret;
194
195 ret = authselect_profile_read_metacxx . . $ authselect_profile_read_meta(819f5aabcc6a9973).(location, dirfd, FILE_READMEcxx . . $ `src/lib/paths.h:35:9`!, truecxx . . $ `<external>/13015445228558014418/stdbool.h:37:9`!,
196 &readme);
197 if (ret != EOKcxx . . $ `src/common/errno_t.h:34:9`!) {
198 return ret;
199 }
200
201 lineend = strcspncxx . . $ strcspn(335c1dc86f5c867f).(readme, "\r\n");
202 if (lineend <= 0) {
203 ERRORcxx . . $ `src/common/common.h:71:9`!("Profile [%s] does not contain a name in [%s]!",
204 location, FILE_READMEcxx . . $ `src/lib/paths.h:35:9`!);
205 ret = EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!;
206 goto done;
207 }
208
209 name = strndupcxx . . $ strndup(eabdfdef77619b3d).(readme, lineend);
210 if (name == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) {
211 ret = ENOMEMcxx . . $ `<external>/8987895939074593727/errno-base.h:16:9`!;
212 goto done;
213 }
214
215 trimmed = string_trimcxx . . $ string_trim(a8b8e32347182212).(name);
216 if (trimmed == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) {
217 ret = ENOMEMcxx . . $ `<external>/8987895939074593727/errno-base.h:16:9`!;
218 goto done;
219 }
220
221 if (string_is_emptycxx . . $ string_is_empty(c5f7da30cbc0f4e9).(trimmed)) {
222 ERRORcxx . . $ `src/common/common.h:71:9`!("Profile [%s] does not contain a name in [%s]!",
223 location, FILE_READMEcxx . . $ `src/lib/paths.h:35:9`!);
224 ret = EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!;
225 goto done;
226 }
227
228 *_description = readme;
229 *_name = trimmed;
230
231 ret = EOKcxx . . $ `src/common/errno_t.h:34:9`!;
232
233 done:
234 if (name != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) {
235 freecxx . . $ free(5d22bdacc48458e8).(name);
236 }
237
238 if (ret != EOKcxx . . $ `src/common/errno_t.h:34:9`!) {
239 freecxx . . $ free(5d22bdacc48458e8).(readme);
240
241 if (trimmed != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) {
242 freecxx . . $ free(5d22bdacc48458e8).(trimmed);
243 }
244 }
245
246 return ret;
247 }
248
249 static struct authselect_profilecxx . . $ authselect_profile# *
250 authselect_profile_initcxx . . $ authselect_profile_init(af32e96d4f1b7093).(const char *id)
251 {
252 struct authselect_profilecxx . . $ authselect_profile# *profile;
253
254 profile = malloc_zerocxx . . $ `src/common/common.h:38:9`!(struct authselect_profile);
255 if (profile == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) {
256 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
257 }
258
259 profile->idcxx . . $ authselect_profile#id. = strdupcxx . . $ strdup(a8b8e32347182212).(id);
260 if (profile->idcxx . . $ authselect_profile#id. == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) {
261 freecxx . . $ free(5d22bdacc48458e8).(profile);
262 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
263 }
264
265 return profile;
266 }
267
268 errno_tcxx . . $ errno_t#
269 authselect_profile_readcxx . . $ authselect_profile_read(73a0aa0767d26394).(const char *profile_id,
270 enum authselect_profile_typecxx . . $ authselect_profile_type# type,
271 struct authselect_profilecxx . . $ authselect_profile# **_profile)
272 {
273 struct authselect_profilecxx . . $ authselect_profile# *profile = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
274 char *location;
275 int dirfd;
276 errno_tcxx . . $ errno_t# ret;
277
278 ret = authselect_profile_opencxx . . $ authselect_profile_open(f5c3b45c724d5b9a).(profile_id, type, &location, &dirfd);
279 if (ret != EOKcxx . . $ `src/common/errno_t.h:34:9`!) {
280 return ret;
281 }
282
283 profile = authselect_profile_initcxx . . $ authselect_profile_init(af32e96d4f1b7093).(profile_id);
284 if (profile == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) {
285 ret = ENOMEMcxx . . $ `<external>/8987895939074593727/errno-base.h:16:9`!;
286 goto done;
287 }
288
289 profile->pathcxx . . $ authselect_profile#path. = location;
290
291 ret = authselect_profile_read_readmecxx . . $ authselect_profile_read_readme(c4c60c29bcbf93b2).(location, dirfd, &profile->namecxx . . $ authselect_profile#name.,
292 &profile->descriptioncxx . . $ authselect_profile#description.);
293 if (ret != EOKcxx . . $ `src/common/errno_t.h:34:9`!) {
294 goto done;
295 }
296
297 ret = authselect_profile_read_metacxx . . $ authselect_profile_read_meta(819f5aabcc6a9973).(location, dirfd, FILE_REQUIREMENTcxx . . $ `src/lib/paths.h:36:9`!,
298 falsecxx . . $ `<external>/13015445228558014418/stdbool.h:38:9`!, &profile->requirementscxx . . $ authselect_profile#requirements.);
299 if (ret != EOKcxx . . $ `src/common/errno_t.h:34:9`!) {
300 goto done;
301 }
302
303 ret = authselect_system_read_templatescxx . . $ authselect_system_read_templates(427a9196ceaf3f94).(location, dirfd, &profile->filescxx . . $ authselect_profile#files.);
304 if (ret != EOKcxx . . $ `src/common/errno_t.h:34:9`!) {
305 goto done;
306 }
307
308 *_profile = profile;
309
310 ret = EOKcxx . . $ `src/common/errno_t.h:34:9`!;
311
312 done:
313 closecxx . . $ close(7864480464b09eea).(dirfd);
314
315 if (ret != EOKcxx . . $ `src/common/errno_t.h:34:9`!) {
316 ERRORcxx . . $ `src/common/common.h:71:9`!("Unable to find profile [%s] [%d]: %s",
317 profile_id, ret, strerror(ret));
318 authselect_profile_freecxx . . $ authselect_profile_free(56572ca77072da15).(profile);
319 }
320
321 return ret;
322 }