| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
#pragma once |
| 3 |
|
| 4 |
#include "basic-forward.h" |
| 5 |
|
| 6 |
typedef enum ChaseFlags { |
| 7 |
CHASE_PREFIX_ROOT = 1 << 0, /* The specified path will be prefixed by the specified root before beginning the iteration */ |
| 8 |
CHASE_NONEXISTENT = 1 << 1, /* It's OK if the path doesn't actually exist. */ |
| 9 |
CHASE_NO_AUTOFS = 1 << 2, /* Return -EREMOTE if autofs mount point found */ |
| 10 |
CHASE_TRIGGER_AUTOFS = 1 << 3, /* Use open_tree() rather than openat() to trigger autofs mount */ |
| 11 |
CHASE_SAFE = 1 << 4, /* Return -EPERM if we ever traverse from unprivileged to privileged files or directories */ |
| 12 |
CHASE_TRAIL_SLASH = 1 << 5, /* Any trailing slash will be preserved */ |
| 13 |
CHASE_STEP = 1 << 6, /* Just execute a single step of the normalization */ |
| 14 |
CHASE_NOFOLLOW = 1 << 7, /* Do not follow the path's right-most component. With ret_fd, when the path's |
| 15 |
* right-most component refers to symlink, return O_PATH fd of the symlink. */ |
| 16 |
CHASE_WARN = 1 << 8, /* Emit an appropriate warning when an error is encountered. |
| 17 |
* Note: this may do an NSS lookup, hence this flag cannot be used in PID 1. */ |
| 18 |
CHASE_PROHIBIT_SYMLINKS = 1 << 9, /* Refuse all symlinks */ |
| 19 |
CHASE_PARENT = 1 << 10, /* Chase the parent directory of the given path. Note that the |
| 20 |
* full path is still stored in ret_path and only the returned |
| 21 |
* file descriptor will point to the parent directory. Note that |
| 22 |
* the result path is the root or '.', then the file descriptor |
| 23 |
* also points to the result path even if this flag is set. |
| 24 |
* When this specified, chase() will succeed with 1 even if the |
| 25 |
* file points to the last path component does not exist. */ |
| 26 |
CHASE_MKDIR_0755 = 1 << 11, /* Create any missing directories in the given path. */ |
| 27 |
CHASE_EXTRACT_FILENAME = 1 << 12, /* Only return the last component of the resolved path */ |
| 28 |
CHASE_MUST_BE_DIRECTORY = 1 << 13, /* Fail if returned inode fd is not a dir */ |
| 29 |
CHASE_MUST_BE_REGULAR = 1 << 14, /* Fail if returned inode fd is not a regular file */ |
| 30 |
CHASE_MUST_BE_SOCKET = 1 << 15, /* Fail if returned inode fd is not a socket */ |
| 31 |
} ChaseFlags; |
| 32 |
|
| 33 |
int statx_unsafe_transition(const struct statx *a, const struct statx *b); |
| 34 |
bool stat_unsafe_transition(const struct stat *a, const struct stat *b); |
| 35 |
|
| 36 |
/* How many iterations to execute before returning -ELOOP */ |
| 37 |
#define CHASE_MAX 128U |
| 38 |
|
| 39 |
int chase(const char *path_with_prefix, const char *root, ChaseFlags flags, char **ret_path, int *ret_fd); |
| 40 |
|
| 41 |
int chaseat_prefix_root(const char *path, const char *root, char **ret); |
| 42 |
int chase_extract_filename(const char *path, const char *root, char **ret); |
| 43 |
|
| 44 |
int chase_and_open(const char *path, const char *root, ChaseFlags chase_flags, int open_flags, char **ret_path); |
| 45 |
int chase_and_opendir(const char *path, const char *root, ChaseFlags chase_flags, char **ret_path, DIR **ret_dir); |
| 46 |
int chase_and_stat(const char *path, const char *root, ChaseFlags chase_flags, char **ret_path, struct stat *ret_stat); |
| 47 |
int chase_and_access(const char *path, const char *root, ChaseFlags chase_flags, int access_mode, char **ret_path); |
| 48 |
int chase_and_fopen_unlocked(const char *path, const char *root, ChaseFlags chase_flags, const char *open_flags, char **ret_path, FILE **ret_file); |
| 49 |
int chase_and_unlink(const char *path, const char *root, ChaseFlags chase_flags, int unlink_flags, char **ret_path); |
| 50 |
int chase_and_open_parent(const char *path, const char *root, ChaseFlags chase_flags, char **ret_filename); |
| 51 |
|
| 52 |
int chaseat(int root_fd, int dir_fd, const char *path, ChaseFlags flags, char **ret_path, int *ret_fd); |
| 53 |
|
| 54 |
int chase_and_openat(int root_fd, int dir_fd, const char *path, ChaseFlags chase_flags, int open_flags, char **ret_path); |
| 55 |
int chase_and_opendirat(int root_fd, int dir_fd, const char *path, ChaseFlags chase_flags, char **ret_path, DIR **ret_dir); |
| 56 |
int chase_and_statat(int root_fd, int dir_fd, const char *path, ChaseFlags chase_flags, char **ret_path, struct stat *ret_stat); |
| 57 |
int chase_and_accessat(int root_fd, int dir_fd, const char *path, ChaseFlags chase_flags, int access_mode, char **ret_path); |
| 58 |
int chase_and_fopenat_unlocked(int root_fd, int dir_fd, const char *path, ChaseFlags chase_flags, const char *open_flags, char **ret_path, FILE **ret_file); |
| 59 |
int chase_and_unlinkat(int root_fd, int dir_fd, const char *path, ChaseFlags chase_flags, int unlink_flags, char **ret_path); |
| 60 |
int chase_and_open_parent_at(int root_fd, int dir_fd, const char *path, ChaseFlags chase_flags, char **ret_filename); |