Debian Codegraph @

src/lib/files/symlinks.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 <unistd.h>
23 #include <sys/stat.h>
24
25 #include "common/common.h"
26 #include "lib/constants.h"
27 #include "lib/util/util.h"
28 #include "lib/files/files.h"
29
30 errno_t
31 authselect_symlinks_write()
32 {
33 struct authselect_symlink symlinks[] = {SYMLINK_FILES};
34 mode_t oldmask;
35 errno_t ret;
36 int i;
37
38 oldmask = umask(~AUTHSELECT_FILE_MODE & ALLPERMS);
39
40 for (i = 0; symlinks[i].name != NULL; i++) {
41 INFO("Creating symbolic link [%s] to [%s]",
42 symlinks[i].name, symlinks[i].dest);
43
44 ret = unlink(symlinks[i].name);
45 if (ret != 0 && errno != ENOENT) {
46 ret = errno;
47 ERROR("Unable to overwrite file [%s] [%d]: %s",
48 symlinks[i].name, ret, strerror(ret));
49 goto done;
50 }
51
52 ret = symlink(symlinks[i].dest, symlinks[i].name);
53 if (ret != 0) {
54 ret = errno;
55 ERROR("Unable to create symbolic link [%s] [%d]: %s",
56 symlinks[i].name, ret, strerror(ret));
57 goto done;
58 }
59 }
60
61 ret = EOK;
62
63 done:
64 umask(oldmask);
65
66 return ret;
67 }
68
69 bool
70 authselect_symlinks_validate()
71 {
72 struct authselect_symlink symlinks[] = {SYMLINK_FILES};
73 bool result = true;
74 bool is_valid;
75 errno_t ret;
76 int i;
77
78 for (i = 0; symlinks[i].name != NULL; i++) {
79 INFO("Validating link [%s]", symlinks[i].name);
80
81 ret = file_links_to(symlinks[i].name, symlinks[i].dest, &is_valid);
82 if (ret != EOK) {
83 ERROR("Unable to validate link [%s] [%d]: %s",
84 symlinks[i].name, ret, strerror(ret));
85 result = false;
86 continue;
87 }
88
89 if (!is_valid) {
90 ERROR("[%s] was not created by authselect!", symlinks[i].name);
91 result = false;
92 }
93 }
94
95 return result;
96 }
97
98 bool
99 authselect_symlinks_validate_missing()
100 {
101 struct authselect_symlink symlinks[] = {SYMLINK_FILES};
102 bool result = true;
103 bool valid;
104 errno_t ret;
105 int i;
106
107 for (i = 0; symlinks[i].name != NULL; i++) {
108 ret = file_exists(symlinks[i].name);
109 if (ret == ENOENT) {
110 continue;
111 } else if (ret != EOK) {
112 ERROR("Error while trying to access file [%s] [%d]: %s",
113 symlinks[i].name, ret, strerror(ret));
114 result = false;
115 continue;
116 }
117
118 ret = file_does_not_link_to(symlinks[i].name, symlinks[i].dest, true,
119 &valid);
120 if (ret != EOK) {
121 ERROR("Unable to check file [%s] [%d]: %s",
122 symlinks[i].name, ret, strerror(ret));
123 result = false;
124 continue;
125 }
126
127 if (!valid) {
128 ERROR("Symbolic link [%s] to [%s] still exists!",
129 symlinks[i].name, symlinks[i].dest);
130 result = false;
131 continue;
132 }
133 }
134
135 return result;
136 }
137
138 bool
139 authselect_symlinks_location_available()
140 {
141 struct authselect_symlink symlinks[] = {SYMLINK_FILES};
142 bool result = true;
143 errno_t ret;
144 int i;
145
146 for (i = 0; symlinks[i].name != NULL; i++) {
147 ret = file_exists(symlinks[i].name);
148 if (ret == EOK) {
149 ERROR("File [%s] exists but it needs to be overwritten!",
150 symlinks[i].name);
151 result = false;
152 } else if (ret != ENOENT) {
153 ERROR("Error while trying to access file [%s] [%d]: %s",
154 symlinks[i].name, ret, strerror(ret));
155 result = false;
156 }
157 }
158
159 return result;
160 }
161
162 errno_t
163 authselect_symlinks_uninstall()
164 {
165 struct selinux_safe_copy table[] = {
166 {PATH_SYSTEM, PATH_SYMLINK_SYSTEM, false, false},
167 {PATH_PASSWORD, PATH_SYMLINK_PASSWORD, false, false},
168 {PATH_FINGERPRINT, PATH_SYMLINK_FINGERPRINT, false, false},
169 {PATH_SMARTCARD, PATH_SYMLINK_SMARTCARD, false, false},
170 {PATH_SWITCHABLE, PATH_SYMLINK_SWITCHABLE, false, false},
171 {PATH_POSTLOGIN, PATH_SYMLINK_POSTLOGIN, false, false},
172 {PATH_NSSWITCH, PATH_SYMLINK_NSSWITCH, false, false},
173 {PATH_DCONF_DB, PATH_SYMLINK_DCONF_DB, false, false},
174 {PATH_DCONF_LOCK, PATH_SYMLINK_DCONF_LOCK, false, false},
175 {NULL, NULL, false, false}
176 };
177 errno_t ret;
178 bool result;
179 int i;
180
181 for (i = 0; table[i].source != NULL; i++) {
182 /* Check if the symlink is still valid. */
183 ret = file_does_not_link_to(table[i].destination, table[i].source,
184 false, &result);
185 if (ret != EOK) {
186 return ret;
187 }
188
189 if (result) {
190 /* This path is already not part of authselect. Let's skip it. */
191 INFO("Skipping [%s] because it is not an authselect file",
192 table[i].destination);
193 table[i].destination = NULL;
194 }
195 }
196
197 return selinux_copy_files_safely(table, AUTHSELECT_DIR_MODE, false);
198 }