Debian Codegraph @

src/lib/files/system.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 <stdio.h>
23 #include <regex.h>
24 #include <string.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27
28 #include "common/common.h"cxx . . $ `<file>/src/common/common.h`/
29 #include "lib/constants.h"cxx . . $ `<file>/src/lib/constants.h`/
30 #include "lib/util/util.h"cxx . . $ `<file>/src/lib/util/util.h`/
31 #include "lib/files/files.h"cxx . . $ `<file>/src/lib/files/files.h`/
32
33 #define RE_NSScxx . . $ `src/lib/files/system.c:33:9`! "^\\s*([^[:space:]:]+):.*$"
34 #define RE_NSS_MATCHEScxx . . $ `src/lib/files/system.c:34:9`! 2
35
36 struct authselect_system_pathscxx . . $ authselect_system_paths# {
37 const char *pathcxx . . $ authselect_system_paths#path.;
38 char **contentcxx . . $ authselect_system_paths#content.;
39 };
40
41 struct authselect_system_templatescxx . . $ authselect_system_templates# {
42 const char *templatecxx . . $ authselect_system_templates#template.;
43 char **generatedcxx . . $ authselect_system_templates#generated.;
44 };
45
46 errno_tcxx . . $ errno_t#
47 authselect_system_read_templatescxx . . $ authselect_system_read_templates(427a9196ceaf3f94).(const char *dirname,
48 int dirfd,
49 struct authselect_filescxx . . $ authselect_files# **_templates)
50 {
51 struct authselect_filescxx . . $ authselect_files# *templates;
52 errno_tcxx . . $ errno_t# ret;
53 int i;
54
55 templates = malloc_zerocxx . . $ `src/common/common.h:38:9`!(struct authselect_files);
56 if (templates == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) {
57 return ENOMEMcxx . . $ `<external>/8987895939074593727/errno-base.h:16:9`!;
58 }
59
60 struct authselect_system_pathscxx . . $ authselect_system_paths# paths[] = {
61 {FILE_SYSTEMcxx . . $ `src/lib/paths.h:37:9`!, &templates->systemauthcxx . . $ authselect_files#systemauth.},
62 {FILE_PASSWORDcxx . . $ `src/lib/paths.h:38:9`!, &templates->passwordauthcxx . . $ authselect_files#passwordauth.},
63 {FILE_SMARTCARDcxx . . $ `src/lib/paths.h:40:9`!, &templates->smartcardauthcxx . . $ authselect_files#smartcardauth.},
64 {FILE_SWITCHABLEcxx . . $ `src/lib/paths.h:41:9`!, &templates->switchableauthcxx . . $ authselect_files#switchableauth.},
65 {FILE_FINGERPRINTcxx . . $ `src/lib/paths.h:39:9`!, &templates->fingerprintauthcxx . . $ authselect_files#fingerprintauth.},
66 {FILE_POSTLOGINcxx . . $ `src/lib/paths.h:42:9`!, &templates->postlogincxx . . $ authselect_files#postlogin.},
67 {FILE_NSSWITCHcxx . . $ `src/lib/paths.h:43:9`!, &templates->nsswitchcxx . . $ authselect_files#nsswitch.},
68 {FILE_DCONF_DBcxx . . $ `src/lib/paths.h:44:9`!, &templates->dconfdbcxx . . $ authselect_files#dconfdb.},
69 {FILE_DCONF_LOCKcxx . . $ `src/lib/paths.h:45:9`!, &templates->dconflockcxx . . $ authselect_files#dconflock.},
70 {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`!},
71 };
72
73 for (i = 0; paths[i].pathcxx . . $ authselect_system_paths#path. != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; i++) {
74 INFOcxx . . $ `src/common/common.h:63:9`!("Reading file [%s/%s]", dirname, paths[i].path);
75
76 ret = textfile_read_dirfdcxx . . $ textfile_read_dirfd(4ff8903a8e11e2fc).(dirfd, dirname, paths[i].pathcxx . . $ authselect_system_paths#path.,
77 AUTHSELECT_FILE_SIZE_LIMITcxx . . $ `src/lib/constants.h:31:9`!,
78 paths[i].contentcxx . . $ authselect_system_paths#content.);
79 if (ret == ENOENTcxx . . $ `<external>/8987895939074593727/errno-base.h:6:9`!) {
80 *paths[i].contentcxx . . $ authselect_system_paths#content. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
81 } else if (ret != EOKcxx . . $ `src/common/errno_t.h:34:9`!) {
82 ERRORcxx . . $ `src/common/common.h:71:9`!("Unable to read file [%s/%s] [%d]: %s",
83 dirname, paths[i].path, ret, strerror(ret));
84 authselect_files_freecxx . . $ authselect_files_free(e1e1e20e9d8c2ec3).(templates);
85 return ret;
86 }
87 }
88
89 *_templates = templates;
90
91 return EOKcxx . . $ `src/common/errno_t.h:34:9`!;
92 }
93
94 errno_tcxx . . $ errno_t#
95 authselect_system_generatecxx . . $ authselect_system_generate(5b14afb014176dea).(const char **features,
96 struct authselect_filescxx . . $ authselect_files# *templates,
97 struct authselect_filescxx . . $ authselect_files# **_files)
98 {
99 struct authselect_filescxx . . $ authselect_files# *files;
100 errno_tcxx . . $ errno_t# ret;
101 int i;
102
103 if (templates == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) {
104 return EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!;
105 }
106
107 files = malloc_zerocxx . . $ `src/common/common.h:38:9`!(struct authselect_files);
108 if (files == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) {
109 return ENOMEMcxx . . $ `<external>/8987895939074593727/errno-base.h:16:9`!;
110 }
111
112 struct authselect_system_templatescxx . . $ authselect_system_templates# tpls[] = {
113 {templates->systemauthcxx . . $ authselect_files#systemauth., &files->systemauthcxx . . $ authselect_files#systemauth.},
114 {templates->passwordauthcxx . . $ authselect_files#passwordauth., &files->passwordauthcxx . . $ authselect_files#passwordauth.},
115 {templates->smartcardauthcxx . . $ authselect_files#smartcardauth., &files->smartcardauthcxx . . $ authselect_files#smartcardauth.},
116 {templates->switchableauthcxx . . $ authselect_files#switchableauth., &files->switchableauthcxx . . $ authselect_files#switchableauth.},
117 {templates->fingerprintauthcxx . . $ authselect_files#fingerprintauth., &files->fingerprintauthcxx . . $ authselect_files#fingerprintauth.},
118 {templates->postlogincxx . . $ authselect_files#postlogin., &files->postlogincxx . . $ authselect_files#postlogin.},
119 {templates->dconfdbcxx . . $ authselect_files#dconfdb., &files->dconfdbcxx . . $ authselect_files#dconfdb.},
120 {templates->dconflockcxx . . $ authselect_files#dconflock., &files->dconflockcxx . . $ authselect_files#dconflock.},
121 {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`!},
122 };
123
124 /* Template may be NULL so we must compare against destination. */
125 for (i = 0; tpls[i].generatedcxx . . $ authselect_system_templates#generated. != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; i++) {
126 if (tpls[i].templatecxx . . $ authselect_system_templates#template. == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) {
127 *tpls[i].generatedcxx . . $ authselect_system_templates#generated. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
128 continue;
129 }
130
131 *tpls[i].generatedcxx . . $ authselect_system_templates#generated. = template_generatecxx . . $ template_generate(e6b3452e8157deef).(tpls[i].templatecxx . . $ authselect_system_templates#template., features);
132 if (tpls[i].generatedcxx . . $ authselect_system_templates#generated. == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) {
133 ret = ENOMEMcxx . . $ `<external>/8987895939074593727/errno-base.h:16:9`!;
134 goto done;
135 }
136 }
137
138 /* nsswitch.conf is special as it can be merged with user-editable file */
139 ret = authselect_nsswitch_generatecxx . . $ authselect_nsswitch_generate(bd92be699d7b007a).(templates->nsswitchcxx . . $ authselect_files#nsswitch., features,
140 &files->nsswitchcxx . . $ authselect_files#nsswitch.);
141 if (ret != EOKcxx . . $ `src/common/errno_t.h:34:9`!) {
142 goto done;
143 }
144
145 *_files = files;
146
147 ret = EOKcxx . . $ `src/common/errno_t.h:34:9`!;
148
149 done:
150 if (ret != EOKcxx . . $ `src/common/errno_t.h:34:9`!) {
151 ERRORcxx . . $ `src/common/common.h:71:9`!("Unable to generate files [%d]: %s", ret, strerror(ret));
152 authselect_files_freecxx . . $ authselect_files_free(e1e1e20e9d8c2ec3).(files);
153 }
154
155 return ret;
156 }
157
158 static errno_tcxx . . $ errno_t#
159 authselect_system_write_tempcxx . . $ authselect_system_write_temp(f0dc5b3d4b8a75).(const char *path,
160 const char *content,
161 char **_tmp_file)
162 {
163 errno_tcxx . . $ errno_t# ret;
164
165 INFOcxx . . $ `src/common/common.h:63:9`!("Writing temporary file for [%s]", path);
166 ret = template_write_temporarycxx . . $ template_write_temporary(5071347881743dfd).(path, content, AUTHSELECT_FILE_MODEcxx . . $ `src/lib/constants.h:30:9`!, _tmp_file);
167 if (ret != EOKcxx . . $ `src/common/errno_t.h:34:9`!) {
168 ERRORcxx . . $ `src/common/common.h:71:9`!("Unable to write temporary file [%s] [%d]: %s",
169 path, ret, strerror(ret));
170 return ret;
171 }
172
173 INFOcxx . . $ `src/common/common.h:63:9`!("Temporary file is named [%s]", *_tmp_file);
174
175 return EOKcxx . . $ `src/common/errno_t.h:34:9`!;
176 }
177
178 static errno_tcxx . . $ errno_t#
179 authselect_system_rename_tempcxx . . $ authselect_system_rename_temp(cc1a8f44225c7fe0).(char **tmp_path,
180 const char *final_path)
181 {
182 errno_tcxx . . $ errno_t# ret;
183
184 INFOcxx . . $ `src/common/common.h:63:9`!("Renaming [%s] to [%s]", *tmp_path, final_path);
185
186 ret = renamecxx . . $ rename(5ae29d726ae57f88).(*tmp_path, final_path);
187 if (ret != 0) {
188 ret = errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!;
189 ERRORcxx . . $ `src/common/common.h:71:9`!("Unable to rename [%s] to [%s] [%d]: %s",
190 *tmp_path, final_path, ret, strerror(ret));
191 }
192
193 freecxx . . $ free(5d22bdacc48458e8).(*tmp_path);
194 *tmp_path = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
195
196 return ret;
197 }
198
199 errno_tcxx . . $ errno_t#
200 authselect_system_writecxx . . $ authselect_system_write(36f2feb53451c8f1).(const char **features,
201 struct authselect_filescxx . . $ authselect_files# *templates)
202 {
203 struct authselect_filescxx . . $ authselect_files# *files;
204 errno_tcxx . . $ errno_t# ret;
205 int i;
206
207 ret = authselect_system_generatecxx . . $ authselect_system_generate(5b14afb014176dea).(features, templates, &files);
208 if (ret != EOKcxx . . $ `src/common/errno_t.h:34:9`!) {
209 return ret;
210 }
211
212 struct authselect_generatedcxx . . $ authselect_generated# generated[] = GENERATED_FILEScxx . . $ `src/lib/paths.h:85:9`!(files);
213 char *tmp_files[sizeof(generated)/sizeof(struct authselect_generatedcxx . . $ authselect_generated#)] = {NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!};
214
215 /* First, write content into temporary files, so we can safely fail
216 * on error. */
217 for (i = 0; generated[i].pathcxx . . $ authselect_generated#path. != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; i++) {
218 ret = authselect_system_write_tempcxx . . $ authselect_system_write_temp(f0dc5b3d4b8a75).(generated[i].pathcxx . . $ authselect_generated#path.,
219 generated[i].contentcxx . . $ authselect_generated#content.,
220 &tmp_files[i]);
221 if (ret != EOKcxx . . $ `src/common/errno_t.h:34:9`!) {
222 goto done;
223 }
224 }
225
226 /* Now rename the files.
227 *
228 * We now know that the system is writable, so rename call shall not
229 * fail and it will overwrite any existing file. The only reason it
230 * can fail is EIO which we can not do anything about and we can not
231 * even recover from it.
232 */
233 for (i = 0; generated[i].pathcxx . . $ authselect_generated#path. != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; i++) {
234 ret = authselect_system_rename_tempcxx . . $ authselect_system_rename_temp(cc1a8f44225c7fe0).(&tmp_files[i], generated[i].pathcxx . . $ authselect_generated#path.);
235 if (ret != EOKcxx . . $ `src/common/errno_t.h:34:9`!) {
236 goto done;
237 }
238 }
239
240 ret = EOKcxx . . $ `src/common/errno_t.h:34:9`!;
241
242 done:
243 if (ret != EOKcxx . . $ `src/common/errno_t.h:34:9`!) {
244 for (i = 0; generated[i].pathcxx . . $ authselect_generated#path. != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; i++) {
245 if (tmp_files[i] != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) {
246 unlinkcxx . . $ unlink(5c4306d8553b3696).(tmp_files[i]);
247 freecxx . . $ free(5d22bdacc48458e8).(tmp_files[i]);
248 tmp_files[i] = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
249 }
250 }
251 }
252 authselect_files_freecxx . . $ authselect_files_free(e1e1e20e9d8c2ec3).(files);
253
254 return ret;
255 }
256
257 static boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`!
258 authselect_system_validate_filecxx . . $ authselect_system_validate_file(c5f7da30cbc0f4e9).(const char *path)
259 {
260 errno_tcxx . . $ errno_t# ret;
261 boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! bret;
262
263 INFOcxx . . $ `src/common/common.h:63:9`!("Validating file [%s]", path);
264
265 ret = file_is_regularcxx . . $ file_is_regular(4567836c9036030d).(path, AUTHSELECT_UIDcxx . . $ `src/lib/paths.h:31:9`!, AUTHSELECT_GIDcxx . . $ `src/lib/paths.h:32:9`!,
266 S_IRUSRcxx . . $ `<external>/14710646456994434043/fcntl.h:105:10`! | S_IWUSRcxx . . $ `<external>/14710646456994434043/fcntl.h:106:10`! | S_IRGRPcxx . . $ `<external>/14710646456994434043/fcntl.h:111:10`! | S_IROTHcxx . . $ `<external>/14710646456994434043/fcntl.h:117:10`!, &bret);
267 if (ret != EOKcxx . . $ `src/common/errno_t.h:34:9`!) {
268 ERRORcxx . . $ `src/common/common.h:71:9`!("Unable to check file mode of [%s] [%d]: %s",
269 path, ret, strerror(ret));
270 return falsecxx . . $ `<external>/13015445228558014418/stdbool.h:38:9`!;
271 }
272
273 return bret;
274 }
275
276 boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`!
277 authselect_system_validatecxx . . $ authselect_system_validate(a73b0e1def25ad2a).(struct authselect_filescxx . . $ authselect_files# *files)
278 {
279 struct authselect_generatedcxx . . $ authselect_generated# generated[] = GENERATED_FILEScxx . . $ `src/lib/paths.h:85:9`!(files);
280 boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! result = truecxx . . $ `<external>/13015445228558014418/stdbool.h:37:9`!;
281 boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! bret;
282 int i;
283
284 for (i = 0; generated[i].pathcxx . . $ authselect_generated#path. != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; i++) {
285 bret = authselect_system_validate_filecxx . . $ authselect_system_validate_file(c5f7da30cbc0f4e9).(generated[i].pathcxx . . $ authselect_generated#path.);
286 result &= bret;
287 if (!bret) {
288 WARNcxx . . $ `src/common/common.h:67:9`!("File [%s] was modified outside authselect!",
289 generated[i].path);
290 }
291 }
292
293 return result;
294 }
295
296 boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`!
297 authselect_system_validate_missingcxx . . $ authselect_system_validate_missing(c9857a73d68e59ba).()
298 {
299 struct authselect_generatedcxx . . $ authselect_generated# generated[] = GENERATED_FILES_PATHScxx . . $ `src/lib/paths.h:99:9`!;
300 boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! result = truecxx . . $ `<external>/13015445228558014418/stdbool.h:37:9`!;
301 errno_tcxx . . $ errno_t# ret;
302 int i;
303
304 for (i = 0; generated[i].pathcxx . . $ authselect_generated#path. != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; i++) {
305 ret = file_existscxx . . $ file_exists(5c4306d8553b3696).(generated[i].pathcxx . . $ authselect_generated#path.);
306 if (ret == EOKcxx . . $ `src/common/errno_t.h:34:9`!) {
307 ERRORcxx . . $ `src/common/common.h:71:9`!("File [%s] is still present", generated[i].path);
308 result = falsecxx . . $ `<external>/13015445228558014418/stdbool.h:38:9`!;
309 continue;
310 } else if (ret != ENOENTcxx . . $ `<external>/8987895939074593727/errno-base.h:6:9`!) {
311 ERRORcxx . . $ `src/common/common.h:71:9`!("Error while trying to access file [%s] [%d]: %s",
312 generated[i].path, ret, strerror(ret));
313 result = falsecxx . . $ `<external>/13015445228558014418/stdbool.h:38:9`!;
314 }
315 }
316
317 return result;
318 }
319
320 errno_tcxx . . $ errno_t#
321 authselect_files_uninstallcxx . . $ authselect_files_uninstall(b126dc7c1de90089).()
322 {
323 struct authselect_generatedcxx . . $ authselect_generated# generated[] = GENERATED_FILES_PATHScxx . . $ `src/lib/paths.h:99:9`!;
324 errno_tcxx . . $ errno_t# ret;
325 int iret;
326 int i;
327
328 errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = 0;
329 iret = unlinkcxx . . $ unlink(5c4306d8553b3696).(PATH_CONFIG_FILEcxx . . $ `src/lib/paths.h:28:9`!);
330 if (iret != 0 && errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! != ENOENTcxx . . $ `<external>/8987895939074593727/errno-base.h:6:9`!) {
331 ret = errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!;
332 ERRORcxx . . $ `src/common/common.h:71:9`!("Unable to delete [%s] [%d]: %s", PATH_CONFIG_FILEcxx . . $ `src/lib/paths.h:28:9`!,
333 ret, strerror(ret));
334 return ret;
335 }
336
337 for (i = 0; generated[i].pathcxx . . $ authselect_generated#path. != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; i++) {
338 /* We can ignore errors here. */
339 iret = unlinkcxx . . $ unlink(5c4306d8553b3696).(generated[i].pathcxx . . $ authselect_generated#path.);
340 if (iret != 0 && errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! != ENOENTcxx . . $ `<external>/8987895939074593727/errno-base.h:6:9`!) {
341 ret = errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!;
342 WARNcxx . . $ `src/common/common.h:67:9`!("Unable to delete [%s] [%d]: %s", generated[i].path,
343 ret, strerror(ret));
344 }
345 }
346
347 return EOKcxx . . $ `src/common/errno_t.h:34:9`!;
348 }