├── .gitignore ├── Makefile ├── auditpipe.cpp ├── paudit.cpp ├── pwait.cpp ├── README.md ├── commands.cpp ├── auditon.cpp └── LICENSE /.gitignore: -------------------------------------------------------------------------------- 1 | /*.dSYM 2 | /auditon 3 | /auditpipe 4 | /commands 5 | /paudit 6 | /pwait 7 | 8 | # Prerequisites 9 | *.d 10 | 11 | # Compiled Object files 12 | *.slo 13 | *.lo 14 | *.o 15 | *.obj 16 | 17 | # Precompiled Headers 18 | *.gch 19 | *.pch 20 | 21 | # Compiled Dynamic libraries 22 | *.so 23 | *.dylib 24 | *.dll 25 | 26 | # Fortran module files 27 | *.mod 28 | *.smod 29 | 30 | # Compiled Static libraries 31 | *.lai 32 | *.la 33 | *.a 34 | *.lib 35 | 36 | # Executables 37 | *.exe 38 | *.out 39 | *.app 40 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | CXXFLAGS := -Wall -std=c++14 -O2 -mmacos-version-min=10.10 -lbsm 2 | 3 | all: auditon auditpipe commands paudit pwait 4 | 5 | clean: 6 | rm -rf auditon auditpipe commands paudit pwait ./*.dSYM 7 | 8 | auditon: auditon.cpp 9 | xcrun -sdk macosx clang++ $(CXXFLAGS) -o $@ auditon.cpp 10 | 11 | auditpipe: auditpipe.cpp 12 | xcrun -sdk macosx clang++ $(CXXFLAGS) -o $@ auditpipe.cpp 13 | 14 | commands: commands.cpp 15 | xcrun -sdk macosx clang++ $(CXXFLAGS) -o $@ commands.cpp 16 | 17 | paudit: paudit.cpp 18 | xcrun -sdk macosx clang++ $(CXXFLAGS) -o $@ paudit.cpp 19 | 20 | pwait: pwait.cpp 21 | xcrun -sdk macosx clang++ $(CXXFLAGS) -o $@ pwait.cpp 22 | -------------------------------------------------------------------------------- /auditpipe.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | static auto config_failure() { 12 | perror("error: could not configure /dev/auditpipe"); 13 | return EXIT_FAILURE; 14 | } 15 | 16 | static bool keep_running = true; 17 | static void stop_running(int _signal) { keep_running = false; } 18 | 19 | #define break_or_fail(MESSAGE) \ 20 | if (errno == EINTR) { \ 21 | break; \ 22 | } \ 23 | perror(MESSAGE); \ 24 | return EXIT_FAILURE; 25 | 26 | int main(int argc, char **argv) { 27 | if (argc != 2 || strcmp(argv[1], "-h") == 0) { 28 | fprintf(stderr, 29 | "usage:\n" 30 | "\t%s | praudit\n" 31 | "\t%s > /path/to/log\n", 32 | argv[0], argv[0]); 33 | return EXIT_FAILURE; 34 | } 35 | 36 | const auto event_classes = argv[1]; 37 | au_mask_t masks; 38 | if (getauditflagsbin(event_classes, &masks)) { 39 | perror("error: unknown event class"); 40 | return EXIT_FAILURE; 41 | } 42 | 43 | if (geteuid() != 0) { 44 | // Re-exec with sudo. 45 | const char *cmd[] = {"sudo", argv[0], argv[1], nullptr}; 46 | execvp("sudo", (char **)cmd); 47 | } 48 | 49 | if (isatty(STDOUT_FILENO)) { 50 | fprintf(stderr, "error: cannot print to stdout, try piping to praudit\n"); 51 | return EXIT_FAILURE; 52 | } 53 | 54 | auto pipe = open("/dev/auditpipe", O_RDONLY); 55 | if (not pipe) { 56 | perror("error: could not open /dev/auditpipe"); 57 | return EXIT_FAILURE; 58 | } 59 | 60 | // 61 | // See man auditpipe for details on these auditpipe ioctls. 62 | 63 | int mode = AUDITPIPE_PRESELECT_MODE_LOCAL; 64 | if (ioctl(pipe, AUDITPIPE_SET_PRESELECT_MODE, &mode)) { 65 | return config_failure(); 66 | } 67 | 68 | // Increase the event queue to the largest maximum size. 69 | u_int max_qlimit; 70 | if (ioctl(pipe, AUDITPIPE_GET_QLIMIT_MAX, &max_qlimit) || 71 | ioctl(pipe, AUDITPIPE_SET_QLIMIT, &max_qlimit)) { 72 | return config_failure(); 73 | } 74 | 75 | if (ioctl(pipe, AUDITPIPE_SET_PRESELECT_FLAGS, &masks)) { 76 | return config_failure(); 77 | } 78 | 79 | u_int max_audit_record_size; 80 | if (ioctl(pipe, AUDITPIPE_GET_MAXAUDITDATA, &max_audit_record_size)) { 81 | return config_failure(); 82 | } 83 | 84 | struct sigaction act; 85 | act.sa_handler = stop_running; 86 | sigaction(SIGINT, &act, nullptr); 87 | 88 | auto buffer_size = max_audit_record_size * max_qlimit; 89 | auto buffer = new char[buffer_size]; 90 | 91 | while (keep_running) { 92 | auto read_size = read(pipe, buffer, buffer_size); 93 | if (read_size == -1) { 94 | break_or_fail("error: failed to read from /dev/auditpipe"); 95 | } 96 | 97 | auto write_size = write(STDOUT_FILENO, buffer, read_size); 98 | if (write_size == -1) { 99 | break_or_fail("error: failed to write to stdout"); 100 | } 101 | 102 | if (write_size != read_size) { 103 | fprintf(stderr, "error: incomplete write to stdout"); 104 | return EXIT_FAILURE; 105 | } 106 | } 107 | 108 | delete[] buffer; 109 | 110 | u_int64_t drop_count; 111 | if (ioctl(pipe, AUDITPIPE_GET_DROPS, &drop_count) == 0) { 112 | if (drop_count > 0) { 113 | // Use \n prefix because the interrupt has printed a bare "^C". 114 | fprintf(stderr, "\nwarning: %llu dropped audit events\n", drop_count); 115 | } 116 | } 117 | 118 | return EXIT_SUCCESS; 119 | } 120 | -------------------------------------------------------------------------------- /paudit.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | static pid_t 13 | _childPid(const std::unordered_multimap &tokens) { 14 | // For posix_spawn, the subject pid is the parent process. The child pid comes 15 | // from one of the arg token named "child PID". 16 | auto range = tokens.equal_range(AUT_ARG32); 17 | for (auto it = range.first; it != range.second; ++it) { 18 | auto &arg = it->second.tt.arg32; 19 | if (arg.len > 0 && strcmp(arg.text, "child PID") == 0) { 20 | return arg.val; 21 | } 22 | } 23 | 24 | return 0; 25 | } 26 | 27 | static std::vector 28 | _eventPids(const std::unordered_multimap &tokens) { 29 | std::vector pids; 30 | 31 | { 32 | auto range = tokens.equal_range(AUT_PROCESS32); 33 | for (auto it = range.first; it != range.second; ++it) { 34 | pids.push_back(it->second.tt.proc32.pid); 35 | } 36 | } 37 | 38 | { 39 | auto range = tokens.equal_range(AUT_SUBJECT32); 40 | for (auto it = range.first; it != range.second; ++it) { 41 | pids.push_back(it->second.tt.subj32.pid); 42 | } 43 | } 44 | 45 | return pids; 46 | } 47 | 48 | static char *_pidExecPath(pid_t pid) { 49 | int argmax_mib[] = {CTL_KERN, KERN_ARGMAX}; 50 | int argmax = ARG_MAX; 51 | auto argmax_size = sizeof(size_t); 52 | if (sysctl(argmax_mib, 2, &argmax, &argmax_size, NULL, 0) != 0) { 53 | return nullptr; 54 | } 55 | 56 | char *procargs = (char *)malloc(argmax); 57 | 58 | int procargs_mib[] = {CTL_KERN, KERN_PROCARGS2, pid}; 59 | size_t procargs_size = argmax; 60 | if (sysctl(procargs_mib, 3, procargs, &procargs_size, NULL, 0) != 0) { 61 | // Can happen if the pid is already gone. 62 | return nullptr; 63 | } 64 | 65 | // Skip past argc count. 66 | procargs += sizeof(int); 67 | auto *exec_path = procargs; 68 | 69 | return exec_path; 70 | } 71 | 72 | int main(int argc, char **argv) { 73 | std::unordered_set watchedCommands{argv + 1, argv + argc}; 74 | std::unordered_set watchedPids{}; 75 | std::unordered_set ignoredPids{}; 76 | 77 | std::unordered_multimap tokens; 78 | auto input = stdin; 79 | while (feof(input) == 0) { 80 | u_char *buffer = nullptr; 81 | auto record_size = au_read_rec(input, &buffer); 82 | if (record_size == 0) { 83 | break; 84 | } 85 | 86 | auto cursor = buffer; 87 | auto remaining = record_size; 88 | tokens.clear(); 89 | while (remaining > 0) { 90 | tokenstr_t token; 91 | au_fetch_tok(&token, cursor, remaining); 92 | tokens.emplace(token.id, token); 93 | remaining -= token.len; 94 | cursor += token.len; 95 | } 96 | 97 | bool watched = false; 98 | for (auto pid : _eventPids(tokens)) { 99 | if (watchedPids.find(pid) != watchedPids.end()) { 100 | watched = true; 101 | } 102 | else if (ignoredPids.find(pid) == ignoredPids.end()) { 103 | auto exec_path = _pidExecPath(pid); 104 | if (not exec_path) { 105 | // No exec_path can mean that the process no longer exists. 106 | // TODO: Lookup existing pids at start, so that they're already known. 107 | continue; 108 | } 109 | 110 | auto command = basename(exec_path); 111 | if (watchedCommands.find(command) != watchedCommands.end()) { 112 | watched = true; 113 | watchedPids.insert(pid); 114 | } else { 115 | ignoredPids.insert(pid); 116 | } 117 | } 118 | } 119 | 120 | auto range = tokens.equal_range(AUT_EXEC_ARGS); 121 | for (auto it = range.first; it != range.second; ++it) { 122 | auto &exec_args = it->second.tt.execarg; 123 | auto command = basename(exec_args.text[0]); 124 | if (watchedCommands.find(command) != watchedCommands.end()) { 125 | watched = true; 126 | auto pid = _childPid(tokens); 127 | if (pid != 0) { 128 | watchedPids.insert(_childPid(tokens)); 129 | } 130 | } 131 | } 132 | 133 | if (watched) { 134 | write(STDOUT_FILENO, buffer, record_size); 135 | } 136 | 137 | free(buffer); 138 | } 139 | 140 | return 0; 141 | } 142 | -------------------------------------------------------------------------------- /pwait.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | using file_unique_ptr = std::unique_ptr; 13 | 14 | static file_unique_ptr file_open(const char *path, const char *mode) { 15 | return {fopen(path, mode), &fclose}; 16 | } 17 | 18 | static file_unique_ptr auditpipe() { 19 | auto pipe = file_open("/dev/auditpipe", "r"); 20 | file_unique_ptr null_file{nullptr, &fclose}; 21 | if (not pipe) { 22 | return null_file; 23 | } 24 | 25 | // 26 | // Setup the `ex` event class. This is a hypothetical optimization. 27 | // 28 | // By default, the `pc` class includes the two events we want, `execve` and 29 | // `posix_spawn`, while the `ex` event class includes only `execve`. However, 30 | // the `pc` event class includes many other event's we're not interested in, 31 | // while the `ex` event class has very few events. 32 | { 33 | auto event_num = getauevnonam("AUE_POSIX_SPAWN"); 34 | if (not event_num) { 35 | return null_file; 36 | } 37 | 38 | au_evclass_map_t evc_map{}; 39 | evc_map.ec_number = *event_num; 40 | if (audit_get_class(&evc_map, sizeof(evc_map))) { 41 | return null_file; 42 | } 43 | 44 | auto class_ent = getauclassnam("ex"); 45 | if (not class_ent) { 46 | return null_file; 47 | } 48 | 49 | auto current_mask = evc_map.ec_class; 50 | evc_map.ec_class |= class_ent->ac_class; 51 | if (evc_map.ec_class != current_mask) { 52 | if (audit_set_class(&evc_map, sizeof(evc_map))) { 53 | return null_file; 54 | } 55 | } 56 | } 57 | 58 | // 59 | // See man auditpipe for details on these auditpipe ioctls. 60 | { 61 | auto fd = fileno(pipe.get()); 62 | int mode = AUDITPIPE_PRESELECT_MODE_LOCAL; 63 | if (ioctl(fd, AUDITPIPE_SET_PRESELECT_MODE, &mode)) { 64 | return null_file; 65 | } 66 | 67 | // Increase the event queue to the largest maximum size. 68 | u_int max_qlimit; 69 | if (ioctl(fd, AUDITPIPE_GET_QLIMIT_MAX, &max_qlimit) || 70 | ioctl(fd, AUDITPIPE_SET_QLIMIT, &max_qlimit)) { 71 | return null_file; 72 | } 73 | 74 | au_mask_t masks; 75 | if (getauditflagsbin((char *)"+ex", &masks)) { 76 | return null_file; 77 | } 78 | 79 | if (ioctl(fd, AUDITPIPE_SET_PRESELECT_FLAGS, &masks)) { 80 | return null_file; 81 | } 82 | } 83 | 84 | return pipe; 85 | } 86 | 87 | int main(int argc, char **argv) { 88 | if (argc <= 1) { 89 | fprintf(stderr, "usage: %s [...]\n", argv[0]); 90 | return EXIT_FAILURE; 91 | } 92 | 93 | if (geteuid() != 0) { 94 | // Re-exec with sudo. 95 | const char *cmd[argc + 2]; 96 | cmd[0] = "sudo"; 97 | for (int i = 0; i < argc; ++i) { 98 | cmd[i + 1] = argv[i]; 99 | } 100 | cmd[argc + 1] = nullptr; 101 | execvp("sudo", (char **)cmd); 102 | } 103 | 104 | const std::unordered_set waitCommands{argv + 1, argv + argc}; 105 | 106 | file_unique_ptr input = auditpipe(); 107 | if (not input) { 108 | perror("error"); 109 | return EXIT_FAILURE; 110 | } 111 | 112 | while (feof(input.get()) == 0) { 113 | // Read an audit event, which is a buffer of tokens. 114 | u_char *buffer = nullptr; 115 | const auto record_size = au_read_rec(input.get(), &buffer); 116 | if (record_size == 0) { 117 | // End of input. 118 | break; 119 | } 120 | 121 | // Scan through the buffer token by token. 122 | auto cursor = buffer; 123 | auto remaining = record_size; 124 | while (remaining > 0) { 125 | tokenstr_t token; 126 | au_fetch_tok(&token, cursor, remaining); 127 | remaining -= token.len; 128 | cursor += token.len; 129 | 130 | if (token.id == AUT_EXEC_ARGS) { 131 | auto &exec_args = token.tt.execarg; 132 | auto command = basename(exec_args.text[0]); 133 | if (waitCommands.find(command) != waitCommands.end()) { 134 | // Could return the pid here. 135 | return EXIT_SUCCESS; 136 | } else { 137 | // This audit event is not a match, stop scanning these tokens. 138 | break; 139 | } 140 | } 141 | } 142 | 143 | free(buffer); 144 | } 145 | 146 | return EXIT_SUCCESS; 147 | } 148 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # knox 2 | 3 | Tools to repurpose system auditing for non-security uses. Allows observability and introspection into processes and their lifecycle, granular file access, causes or sources of errors, info about networking use, and more. Based on macOS system auditing API ["Basic Security Module" (aka BSM)](https://en.wikipedia.org/wiki/OpenBSM). 4 | 5 | ## Introduction 6 | 7 | macOS has a system library called `libbsm`, which is a lesser known API for security auditing and monitoring. This same data is potentially useful for much more. Like what exactly? Well, that's why this repo exists -- to explore the possibilities and to provide tools, libraries, and documentation for working with `libbsm`. 8 | 9 | Some interesting uses for `libbsm` are: observing and reacting to process lifecycle, visualizing file access across the life of a process, visibility of internal process errors, etc. This kind of data has been available via `dtrace`, using `libbsm` is still interesting because it has different programming ergonomics, and has different tradeoffs. 10 | 11 | ## Install 12 | 13 | ```sh 14 | brew install --HEAD kastiglione/formulae/knox 15 | ``` 16 | 17 | ## Tools 18 | 19 | ### `auditpipe` 20 | 21 | Opens and configures the audit event firehose: `/dev/auditpipe`. The `auditpipe` command takes a set of event classes, and writes all matching events to `stdout`. A quick example to consider: 22 | 23 | ```sh 24 | auditpipe pc,fc | praudit -lx 25 | ``` 26 | 27 | Using `praudit` (ships with macOS), this prints process events ("pc" event class) and file creation events ("fc" event class). See [Event Classes](#event-classes) for a list. The process event class includes syscalls such as `fork`, `execve`, `posix_spawn`, `kill`, `exit`, and more. The syscalls and their associated event classes are listed in `/etc/security/audit_event`. 28 | 29 | As described in `man audit_control`, event classes can be formatted as "comma-delimited list". Additionally, event classes can be prefixed with `+` to show only successful events, or `-` to show only failed events. See "Audit Flags" in `man audit_control` for full details. 30 | 31 | The complete list of event classes can be found in `/etc/security/audit_classes`. See [Event Classes](#event-classes) for an overview: 32 | 33 | #### Examples 34 | 35 | ##### Print successful process events: 36 | 37 | ```sh 38 | auditpipe +pc | praudit -lx 39 | ``` 40 | 41 | ##### Print failed file reads and writes, and filters to the given path prefix: 42 | 43 | ```sh 44 | auditpipe -fr,-fw | praudit -lx | grep /Users/me 45 | ``` 46 | 47 | ### `commands` 48 | 49 | If you ever need to see which commands are being run by other processes, this is the tool to do that. Prints the command lines for all processes. The `commands` tool reads log files, for example those in `/var/audit`, or if no log file is provided `commands` shows live commands via `/dev/auditpipe`. 50 | 51 | #### Examples 52 | 53 | ```sh 54 | commands 55 | commands /var/audit/current 56 | ``` 57 | 58 | ### `auditon` 59 | 60 | The `auditon` command is a command line interface to the `auditon(2)` API. It's useful for some advanced use cases (TODO: document these). See the source and man page for details. 61 | 62 | ## Audit Log 63 | 64 | `/dev/auditpipe` is useful for live observing events. Additionally, BSM can also be configured to log events to `/var/audit`, and this is useful to look back in time for events matching some criteria. To configure the audit logs, see `man audit_control` and edit `/etc/security/audit_control`. Note that some settings take effect on login, so logout/login can be required to have settings take effect. Other settings, such as file size limits, can be applied by running `sudo audit -s`. 65 | 66 | ## Documentation 67 | 68 | The majority of documentation is in the BSM headers (`bsm/libbsm.h`) and manual pages (see below). Most of the Audit man pages are available only when Xcode is installed. The [xnu source](https://opensource.apple.com/tarballs/xnu/) can also be hepful. 69 | 70 | * `man auditpipe` 71 | * `man audit.log` 72 | * `man audit_class` 73 | * `man audit_control` 74 | * `man praudit` 75 | * `man auditon` 76 | * `man auditreduce` 77 | 78 | `grep` can find more `man` pages: 79 | 80 | ``` 81 | cd $(xcrun --show-sdk-path)/usr/share/man 82 | grep -rl '\bau_' . 83 | ``` 84 | 85 | #### Limitiations 86 | 87 | * The audit token for exec args is limited to a max of 128 arguments 88 | * During high load, `/dev/auditpipe` can drop events if its queue is full 89 | * `/dev/auditpipe` provides events for the current user, not `root` 90 | 91 | ## Event Classes 92 | 93 | | Name | Description | 94 | | --- | --- | 95 | | fr | file read | 96 | | fw | file write | 97 | | fa | file attribute access | 98 | | fm | file attribute modify | 99 | | fc | file create | 100 | | fd | file delete | 101 | | cl | file close | 102 | | pc | process | 103 | | nt | network | 104 | | ip | ipc | 105 | | ad | administrative | 106 | | lo | login_logout | 107 | | aa | authentication and authorization | 108 | | ap | application | 109 | | io | ioctl | 110 | | ex | exec | 111 | | ot | miscellaneous | 112 | | all | all flags set | 113 | 114 | ## Permissions 115 | 116 | Root permissinos are required to access `/dev/auditpipe` and the logs in `/var/audit`. To avoid needing a password to use these, there are two options: 117 | 118 | 1. Make the binaries setuid 119 | 2. Add config in `/etc/sudoers` 120 | 121 | #### setuid 122 | 123 | To make `auditpipe` setuid, run: 124 | 125 | ```sh 126 | sudo chown root auditpipe 127 | sudo chmod +s auditpipe 128 | ``` 129 | 130 | #### sudo 131 | 132 | Where necessary, `knox` commands will re-exec with `sudo`, prompting the user to enter their password, if necessary. 133 | 134 | To make `sudo auditpipe` require no password, run `sudo visudo` and then add: 135 | 136 | ``` 137 | yourusername ALL = NOPASSWD: /usr/local/bin/auditpipe * 138 | ``` 139 | 140 | ## Why "knox" 141 | 142 | The Audit API types and functions are prefixed with "au", and Au is the chemical symbol for gold. One place to find gold is at Fort Knox. But really, all of this provides a line of reasoning to pay some small homage to my super awesome grandma, whose maiden name was Knox. 143 | -------------------------------------------------------------------------------- /commands.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | static void shellAppend(std::string &string, char* arg) { 9 | if (strchr(arg, ' ')) { 10 | string.push_back('"'); 11 | string.append(arg); 12 | string.push_back('"'); 13 | } else { 14 | string.append(arg); 15 | } 16 | } 17 | 18 | static std::string shellJoin(char **strings, int count) { 19 | if (count <= 0) { 20 | return ""; 21 | } 22 | 23 | std::string result{}; 24 | shellAppend(result, strings[0]); 25 | for (auto i = 1; i < count; ++i) { 26 | result.push_back(' '); 27 | shellAppend(result, strings[i]); 28 | } 29 | return result; 30 | } 31 | 32 | static std::string shellJoin(const std::string &path, char **strings, int count) { 33 | if (path.empty()) { 34 | return shellJoin(strings, count); 35 | } 36 | 37 | auto args = shellJoin(&strings[1], count - 1); 38 | return path + ' ' + args; 39 | } 40 | 41 | using unique_file_ptr = std::unique_ptr; 42 | 43 | static unique_file_ptr file_open(const char *path, const char *mode) { 44 | return {fopen(path, mode), &fclose}; 45 | } 46 | 47 | static unique_file_ptr auditpipe() { 48 | auto pipe = file_open("/dev/auditpipe", "r"); 49 | unique_file_ptr null_file{nullptr, &fclose}; 50 | if (not pipe) { 51 | return null_file; 52 | } 53 | 54 | // 55 | // Setup the `ex` event class. This is a hypothetical optimization. 56 | // 57 | // By default, the `pc` class includes the two events we want, `execve` and 58 | // `posix_spawn`, while the `ex` event class includes only `execve`. However, 59 | // the `pc` event class includes many other event's we're not interested in, 60 | // while the `ex` event class has very few events. 61 | { 62 | #pragma clang diagnostic push 63 | #pragma clang diagnostic ignored "-Wdeprecated-declarations" 64 | auto event_num = getauevnonam("AUE_POSIX_SPAWN"); 65 | if (not event_num) { 66 | return null_file; 67 | } 68 | 69 | au_evclass_map_t evc_map{}; 70 | evc_map.ec_number = *event_num; 71 | if (audit_get_class(&evc_map, sizeof(evc_map))) { 72 | return null_file; 73 | } 74 | 75 | auto class_ent = getauclassnam("ex"); 76 | if (not class_ent) { 77 | return null_file; 78 | } 79 | 80 | auto current_mask = evc_map.ec_class; 81 | evc_map.ec_class |= class_ent->ac_class; 82 | if (evc_map.ec_class != current_mask) { 83 | if (audit_set_class(&evc_map, sizeof(evc_map))) { 84 | return null_file; 85 | } 86 | } 87 | } 88 | 89 | // 90 | // See man auditpipe for details on these auditpipe ioctls. 91 | { 92 | auto fd = fileno(pipe.get()); 93 | int mode = AUDITPIPE_PRESELECT_MODE_LOCAL; 94 | if (ioctl(fd, AUDITPIPE_SET_PRESELECT_MODE, &mode)) { 95 | return null_file; 96 | } 97 | 98 | // Increase the event queue to the largest maximum size. 99 | u_int max_qlimit; 100 | if (ioctl(fd, AUDITPIPE_GET_QLIMIT_MAX, &max_qlimit) || 101 | ioctl(fd, AUDITPIPE_SET_QLIMIT, &max_qlimit)) { 102 | return null_file; 103 | } 104 | 105 | au_mask_t masks; 106 | if (getauditflagsbin((char *)"+ex", &masks)) { 107 | return null_file; 108 | } 109 | #pragma clang diagnostic pop 110 | 111 | if (ioctl(fd, AUDITPIPE_SET_PRESELECT_FLAGS, &masks)) { 112 | return null_file; 113 | } 114 | } 115 | 116 | return pipe; 117 | } 118 | 119 | int main(int argc, char **argv) { 120 | if (argc == 1 && not isatty(STDIN_FILENO)) { 121 | std::cerr << "usage: " << argv[0] << " []" << std::endl; 122 | return EXIT_FAILURE; 123 | } 124 | 125 | if (geteuid() != 0) { 126 | std::cout << "Re-running as root" << std::endl; 127 | // TODO: This doesn't need to be in the uncommon case of reading from audit 128 | // log files owned by the user. 129 | const char *cmd[argc + 2]; 130 | cmd[0] = "sudo"; 131 | for (int i = 0; i < argc; ++i) { 132 | cmd[i + 1] = argv[i]; 133 | } 134 | cmd[argc + 1] = nullptr; 135 | execvp("sudo", (char **)cmd); 136 | } 137 | 138 | unique_file_ptr input = argc > 1 ? file_open(argv[1], "r") : auditpipe(); 139 | if (not input) { 140 | perror("error"); 141 | return EXIT_FAILURE; 142 | } 143 | 144 | while (feof(input.get()) == 0) { 145 | // Read an audit event, which is a buffer of tokens. 146 | u_char *buffer = nullptr; 147 | const auto record_size = au_read_rec(input.get(), &buffer); 148 | if (record_size == 0) { 149 | // End of input. 150 | break; 151 | } 152 | 153 | au_execenv_t exec_env{}; 154 | au_path_t path{}; 155 | au_execarg_t exec_args{}; 156 | 157 | // Scan through the buffer token by token. 158 | auto ptr = buffer; 159 | tokenstr_t tok; 160 | for (auto left = record_size; left > 0; left -= tok.len, ptr += tok.len) { 161 | au_fetch_tok(&tok, ptr, left); 162 | switch (tok.id) { 163 | case AUT_EXEC_ENV: 164 | exec_env = tok.tt.execenv; 165 | break; 166 | case AUT_PATH: 167 | path = tok.tt.path; 168 | break; 169 | case AUT_EXEC_ARGS: 170 | // Expects the last path to be the final resolved path. 171 | // 172 | // Audit events can contain more than one `au_path_t` token, and they 173 | // appear to show the command path being resolved step by step. From 174 | // relative to absolute, and from symlink to real path. 175 | exec_args = tok.tt.execarg; 176 | break; 177 | } 178 | } 179 | 180 | // If the audit tokens had exec args, print them (and optionally env too). 181 | if (exec_args.count > 0) { 182 | if (exec_env.count > 0) { 183 | auto env = shellJoin(exec_env.text, exec_env.count); 184 | std::cout << env << " "; 185 | } 186 | std::string full_path{path.path, path.len}; 187 | auto args = shellJoin(full_path, exec_args.text, exec_args.count); 188 | std::cout << args << std::endl; 189 | } 190 | 191 | free(buffer); 192 | } 193 | 194 | return EXIT_SUCCESS; 195 | } 196 | -------------------------------------------------------------------------------- /auditon.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include 6 | #include 7 | 8 | enum class Command { 9 | GETPOLICY, 10 | SETPOLICY, 11 | UNSETPOLICY, 12 | GETMASK, 13 | SETMASK, 14 | UNSETMASK, 15 | GETCLASS, 16 | SETCLASS, 17 | UNSETCLASS 18 | }; 19 | 20 | static auto usage() { 21 | fprintf(stderr, "usage: auditon getpolicy\n" 22 | " auditon setpolicy \n" 23 | " auditon unsetpolicy \n" 24 | " auditon getmask \n" 25 | " auditon setmask \n" 26 | " auditon unsetmask \n" 27 | " auditon getclass \n" 28 | " auditon setclass \n" 29 | " auditon unsetclass \n"); 30 | return EXIT_FAILURE; 31 | } 32 | 33 | int main(int argc, char **argv) { 34 | std::unordered_map commands{ 35 | {"getpolicy", Command::GETPOLICY}, {"setpolicy", Command::SETPOLICY}, 36 | {"unsetpolicy", Command::UNSETPOLICY}, {"getmask", Command::GETMASK}, 37 | {"setmask", Command::SETMASK}, {"unsetmask", Command::UNSETMASK}, 38 | {"getclass", Command::GETCLASS}, {"setclass", Command::SETCLASS}, 39 | {"unsetclass", Command::UNSETCLASS}, 40 | }; 41 | 42 | if (argc <= 1) { 43 | return usage(); 44 | } 45 | 46 | if (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--help") == 0) { 47 | return usage(); 48 | } 49 | 50 | const auto command = commands.find(argv[1]); 51 | if (command == commands.end()) { 52 | fprintf(stderr, "error: unknown command\n"); 53 | return usage(); 54 | } 55 | 56 | if (geteuid() != 0) { 57 | // Re-exec with sudo. 58 | char *cmd[argc + 2]; 59 | cmd[0] = "sudo"; 60 | for (int i = 0; i < argc; ++i) { 61 | cmd[i + 1] = argv[i]; 62 | } 63 | cmd[argc + 1] = nullptr; 64 | execvp("sudo", cmd); 65 | } 66 | 67 | switch (command->second) { 68 | case Command::GETPOLICY: { 69 | int policy; 70 | if (audit_get_policy(&policy)) { 71 | perror("error"); 72 | return EXIT_FAILURE; 73 | } 74 | 75 | // Based on known policy names, 128 should always be enough. 76 | char buf[128]; 77 | auto size = au_poltostr(policy, 128, buf); 78 | if (size < 0) { 79 | perror("error"); 80 | return EXIT_FAILURE; 81 | } 82 | printf("%.*s\n", (int)size, buf); 83 | break; 84 | } 85 | case Command::SETPOLICY: { 86 | if (argc != 3) { 87 | fprintf(stderr, "usage: auditon setpolicy \n"); 88 | return EXIT_FAILURE; 89 | } 90 | 91 | int current_policy; 92 | if (audit_get_policy(¤t_policy)) { 93 | perror("error"); 94 | return EXIT_FAILURE; 95 | } 96 | 97 | int new_policy; 98 | if (au_strtopol(argv[2], &new_policy)) { 99 | perror("error"); 100 | return EXIT_FAILURE; 101 | } 102 | 103 | int policy = current_policy | new_policy; 104 | if (audit_set_policy(&policy)) { 105 | perror("error"); 106 | return EXIT_FAILURE; 107 | } 108 | break; 109 | } 110 | case Command::UNSETPOLICY: { 111 | if (argc != 3) { 112 | fprintf(stderr, "usage: auditon unsetpolicy \n"); 113 | return EXIT_FAILURE; 114 | } 115 | 116 | int current_policy; 117 | if (audit_get_policy(¤t_policy)) { 118 | perror("error"); 119 | return EXIT_FAILURE; 120 | } 121 | 122 | int new_policy; 123 | if (au_strtopol(argv[2], &new_policy)) { 124 | perror("error"); 125 | return EXIT_FAILURE; 126 | } 127 | 128 | int policy = current_policy & ~new_policy; 129 | if (audit_set_policy(&policy)) { 130 | perror("error"); 131 | return EXIT_FAILURE; 132 | } 133 | break; 134 | } 135 | case Command::GETMASK: { 136 | if (argc != 3) { 137 | fprintf(stderr, "usage: auditon getmask \n"); 138 | return EXIT_FAILURE; 139 | } 140 | 141 | auditpinfo_t api; 142 | api.ap_pid = atoi(argv[2]); 143 | if (audit_get_pinfo(&api, sizeof(api))) { 144 | perror("error"); 145 | return EXIT_FAILURE; 146 | } 147 | 148 | // For buffer sizing, see "BUGS" section of `man au_mask`. 149 | // TODO: I don't think the docs are right. 150 | const size_t bufsize = 128; 151 | char buf[bufsize]; 152 | if (getauditflagschar(buf, &api.ap_mask, 0)) { 153 | perror("error"); 154 | return EXIT_FAILURE; 155 | } 156 | printf("%s\n", buf); 157 | break; 158 | } 159 | case Command::SETMASK: { 160 | if (argc != 4) { 161 | fprintf(stderr, "usage: auditon setmask \n"); 162 | return EXIT_FAILURE; 163 | } 164 | 165 | auditpinfo_t api; 166 | api.ap_pid = atoi(argv[2]); 167 | if (audit_get_pinfo(&api, sizeof(api))) { 168 | perror("error"); 169 | return EXIT_FAILURE; 170 | } 171 | 172 | auto new_mask = api.ap_mask; 173 | if (getauditflagsbin(argv[3], &new_mask)) { 174 | perror("error"); 175 | return EXIT_FAILURE; 176 | } 177 | 178 | api.ap_mask.am_success |= new_mask.am_success; 179 | api.ap_mask.am_failure |= new_mask.am_failure; 180 | if (audit_set_pmask(&api, sizeof(api))) { 181 | perror("error"); 182 | return EXIT_FAILURE; 183 | } 184 | break; 185 | } 186 | case Command::UNSETMASK: { 187 | if (argc != 4) { 188 | fprintf(stderr, "usage: auditon unsetmask \n"); 189 | return EXIT_FAILURE; 190 | } 191 | 192 | auditpinfo_t api; 193 | api.ap_pid = atoi(argv[2]); 194 | if (audit_get_pinfo(&api, sizeof(api))) { 195 | perror("error"); 196 | return EXIT_FAILURE; 197 | } 198 | 199 | auto new_mask = api.ap_mask; 200 | if (getauditflagsbin(argv[3], &new_mask)) { 201 | perror("error"); 202 | return EXIT_FAILURE; 203 | } 204 | 205 | api.ap_mask.am_success &= ~new_mask.am_success; 206 | api.ap_mask.am_failure &= ~new_mask.am_failure; 207 | if (audit_set_pmask(&api, sizeof(api))) { 208 | perror("error"); 209 | return EXIT_FAILURE; 210 | } 211 | break; 212 | } 213 | case Command::GETCLASS: { 214 | if (argc != 3) { 215 | fprintf(stderr, "usage: auditon getclass \n"); 216 | return EXIT_FAILURE; 217 | } 218 | 219 | au_evclass_map_t evc_map; 220 | const auto event_name = argv[2]; 221 | auto event_num = getauevnonam(event_name); 222 | if (not event_num) { 223 | perror("error"); 224 | return EXIT_FAILURE; 225 | } 226 | 227 | evc_map.ec_number = *event_num; 228 | if (audit_get_class(&evc_map, sizeof(evc_map))) { 229 | perror("error"); 230 | return EXIT_FAILURE; 231 | } 232 | 233 | // This function starts an iteration over all class_ent records. 234 | setauclass(); 235 | bool match = false; 236 | for (auto class_ent = getauclassent(); class_ent != nullptr; 237 | class_ent = getauclassent()) { 238 | auto mask = class_ent->ac_class; 239 | // Only check singular masks, for example not the "all" mask. 240 | if (__builtin_popcount(mask) != 1) { 241 | continue; 242 | } 243 | 244 | if (evc_map.ec_class & mask) { 245 | match = true; 246 | printf("%s,", class_ent->ac_name); 247 | } 248 | } 249 | endauclass(); 250 | 251 | if (not match) { 252 | fprintf(stderr, "error: %s does not belong to any classes\n", event_name); 253 | return EXIT_FAILURE; 254 | } 255 | 256 | // This "\b " overwrites the trailing ',' 257 | printf("\b \n"); 258 | break; 259 | } 260 | case Command::SETCLASS: { 261 | if (argc != 4) { 262 | fprintf(stderr, "usage: auditon setclass \n"); 263 | return EXIT_FAILURE; 264 | } 265 | 266 | au_evclass_map_t evc_map; 267 | auto event_num = getauevnonam(argv[2]); 268 | if (not event_num) { 269 | perror("error"); 270 | return EXIT_FAILURE; 271 | } 272 | 273 | evc_map.ec_number = *event_num; 274 | if (audit_get_class(&evc_map, sizeof(evc_map))) { 275 | perror("error"); 276 | return EXIT_FAILURE; 277 | } 278 | 279 | const auto class_name = argv[3]; 280 | auto class_ent = getauclassnam(class_name); 281 | if (not class_ent) { 282 | perror("error"); 283 | return EXIT_FAILURE; 284 | } 285 | 286 | evc_map.ec_class |= class_ent->ac_class; 287 | if (audit_set_class(&evc_map, sizeof(evc_map))) { 288 | perror("error"); 289 | return EXIT_FAILURE; 290 | } 291 | break; 292 | } 293 | case Command::UNSETCLASS: { 294 | if (argc != 4) { 295 | fprintf(stderr, 296 | "usage: auditon unsetclass \n"); 297 | return EXIT_FAILURE; 298 | } 299 | 300 | au_evclass_map_t evc_map; 301 | auto event_num = getauevnonam(argv[2]); 302 | if (not event_num) { 303 | perror("error"); 304 | return EXIT_FAILURE; 305 | } 306 | 307 | evc_map.ec_number = *event_num; 308 | if (audit_get_class(&evc_map, sizeof(evc_map))) { 309 | perror("error"); 310 | return EXIT_FAILURE; 311 | } 312 | 313 | const auto class_name = argv[3]; 314 | auto class_ent = getauclassnam(class_name); 315 | if (not class_ent) { 316 | perror("error"); 317 | return EXIT_FAILURE; 318 | } 319 | 320 | evc_map.ec_class &= ~class_ent->ac_class; 321 | if (audit_set_class(&evc_map, sizeof(evc_map))) { 322 | perror("error"); 323 | return EXIT_FAILURE; 324 | } 325 | break; 326 | } 327 | } 328 | 329 | return EXIT_SUCCESS; 330 | } 331 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | --------------------------------------------------------------------------------