If SELABEL_HANDLE is null, set PATH's label to the default to the local process. Otherwise use selabel_lookup to determine the default label, extract the type field and then modify the file system object. Note only the type field is updated, thus preserving MLS levels and user identity etc. of the PATH. Returns -1 on failure. errno will be set appropriately. */
| 175 | Returns -1 on failure. errno will be set appropriately. |
| 176 | */ |
| 177 | static int |
| 178 | restorecon_private (struct selabel_handle *selabel_handle, char const *path) |
| 179 | { |
| 180 | int rc = -1; |
| 181 | struct stat sb; |
| 182 | char *scon_raw = NULL; |
| 183 | char *tcon_raw = NULL; |
| 184 | context_t scontext = NULL, tcontext = NULL; |
| 185 | char const *contype; |
| 186 | char const *constr; |
| 187 | int fd; |
| 188 | |
| 189 | if (!selabel_handle) |
| 190 | { |
| 191 | if (getfscreatecon_raw (&tcon_raw) < 0) |
| 192 | return rc; |
| 193 | if (!tcon_raw) |
| 194 | { |
| 195 | errno = ENODATA; |
| 196 | return rc; |
| 197 | } |
| 198 | rc = lsetfilecon_raw (path, tcon_raw); |
| 199 | int err = errno; |
| 200 | freecon (tcon_raw); |
| 201 | errno = err; |
| 202 | return rc; |
| 203 | } |
| 204 | |
| 205 | fd = open (path, O_RDONLY | O_NOFOLLOW); |
| 206 | if (fd == -1 && (errno != ELOOP)) |
| 207 | goto quit; |
| 208 | |
| 209 | if (fd != -1) |
| 210 | { |
| 211 | if (fstat (fd, &sb) < 0) |
| 212 | goto quit; |
| 213 | } |
| 214 | else |
| 215 | { |
| 216 | if (lstat (path, &sb) < 0) |
| 217 | goto quit; |
| 218 | } |
| 219 | |
| 220 | if (selabel_lookup_raw (selabel_handle, &scon_raw, path, sb.st_mode) < 0) |
| 221 | { |
| 222 | /* "No such file or directory" is a confusing error, |
| 223 | when processing files, when in fact it was the |
| 224 | associated default context that was not found. |
| 225 | Therefore map the error to something more appropriate |
| 226 | to the context in which we're using selabel_lookup. */ |
| 227 | if (errno == ENOENT) |
| 228 | errno = ENODATA; |
| 229 | goto quit; |
| 230 | } |
| 231 | if (!(scontext = context_new (scon_raw))) |
| 232 | goto quit; |
| 233 | |
| 234 | if (fd != -1) |