/* Platform calls the library makes on Windows: the filesystem * capabilities the store layer uses, the limits of a builder's job, and * the daemon's named pipe with the accounts its clients are named by. * The cabal c-sources entry is unconditional and the C99 strict job * lints every C file under cbits on Linux, so every other platform * compiles a constant 0 stub of the case-sensitivity call; the Haskell * side picks the platform's answer at compile time and never calls it. * The access-control, job, pipe and account calls are declared and * defined on Windows alone, the only platform that calls them. */#ifndef NN_WINFS_H#define NN_WINFS_H#include <wchar.h>/* Make an EMPTY directory's namespace case-sensitive (the NTFS * per-directory flag, the same mechanism WSL uses to host Linux trees). * Returns 1 on success, 0 when refused or unsupported - a non-NTFS * volume, policy, a non-empty directory, or a non-Windows build - and * the caller falls back to the case-hack. The check guards external * input (NAR trees choose which directories need it), so the failure * path is a live branch, never an assert. */int nn_dir_set_case_sensitive(const wchar_t *path);#ifdef _WIN32/* The SID of the user this process runs as, its token's user, in the * string form ConvertSidToStringSidW writes ("S-1-5-21-..."), written to * *sid for the caller to release with LocalFree. Returns 0, or the * Win32 error code of the call that failed, with *failed naming that * call (a static string) and *sid NULL. */unsigned long nn_token_user_sid(wchar_t **sid, const char **failed);/* Create the directory at path with the security descriptor the SDDL * string describes, in the one CreateDirectoryW call that makes it, so * the directory never exists with the access control list its parent * would pass down. Returns 0, or the Win32 error code (a DWORD) of the * step that failed: ERROR_ALREADY_EXISTS when anything already has the * name, a file as well as a directory. */unsigned long nn_dir_create_sddl(const wchar_t *path, const wchar_t *sddl);/* The owner and DACL of the entry at path itself, a link read as itself * and not followed, as the self-relative security descriptor * GetSecurityInfo returns, written to *descriptor for the caller to * release with LocalFree, with its length in bytes in *length. Returns * 0, or the Win32 error code of the call that failed, with *failed naming * that call (a static string) and *descriptor NULL: ERROR_FILE_NOT_FOUND * or ERROR_PATH_NOT_FOUND from CreateFileW when nothing has the name. */unsigned long nn_entry_security(const wchar_t *path, void **descriptor, unsigned long *length, const char **failed);/* Whether this process's token is granted the access rights given * (winnt.h's FILE_LIST_DIRECTORY, FILE_ADD_FILE and the like, ORed) by the * owner and DACL of the directory at path, a link followed. The rights * are checked against the descriptor and the directory is not opened for * them, since an open made for backup intent passes whatever the DACL says * for a token whose backup or restore privilege is enabled, and a write * does not. Returns 0 and sets *granted to 1 or 0, or returns the Win32 * error code of the call that failed, named in *failed (ERROR_FILE_NOT_FOUND * or ERROR_PATH_NOT_FOUND where nothing has the name). */unsigned long nn_dir_access(const wchar_t *path, unsigned long access, int *granted, const char **failed);/* The SID of the local group the name names on this machine, in the * string form ConvertSidToStringSidW writes, written to *sid for the * caller to release with LocalFree; *sid is NULL where the machine has no * local group of that name, none at all or an account of another kind. * Returns 0, or the Win32 error code of the call that failed, with * *failed naming that call (a static string) and *sid NULL. */unsigned long nn_local_group_sid(const wchar_t *name, wchar_t **sid, const char **failed);/* The self-relative security descriptor the SDDL string describes, * written to *descriptor for the caller to release with LocalFree. * Returns 0, or the Win32 error code. */unsigned long nn_descriptor_from_sddl(const wchar_t *sddl, void **descriptor);/* Give the entry at path itself, a link not followed, the owner and the * DACL, protected, of the security descriptor given. Returns 0, or the * Win32 error code of the call that failed, with *failed naming that * call (a static string). */unsigned long nn_entry_set_owner_dacl(const wchar_t *path, void *descriptor, const char **failed);/* Add JOB_OBJECT_LIMIT_DIE_ON_UNHANDLED_EXCEPTION to the limits of the * job whose handle is given, keeping the others, so that a process of * the job that meets an unhandled exception ends with the exception's * code as its exit code, and Windows Error Reporting is not invoked. * Returns 0, or the Win32 error code of the call that failed, with * *failed naming that call (a static string). */unsigned long nn_job_die_on_unhandled_exception(void *job, const char **failed);/* The daemon's named pipe. Every handle is opened for overlapped I/O, so * one connection can be read and written at once from two threads, and * every call that waits also wakes when the event stop is set, which * cancels what it waited on: the caller sets stop to interrupt it. Each * returns 0, or the Win32 error code of the call that failed with *failed * naming that call (a static string): ERROR_OPERATION_ABORTED when stop * was set first. *//* Create an instance of the pipe at name (\\.\pipe\NAME): duplex, in byte * mode, refusing clients on other machines, with the security descriptor * the SDDL string describes. With first nonzero it is created with * FILE_FLAG_FIRST_PIPE_INSTANCE, so it fails with ERROR_ACCESS_DENIED * where the pipe already exists. *pipe gets the instance's handle. */unsigned long nn_pipe_create(const wchar_t *name, int first, const wchar_t *sddl, void **pipe, const char **failed);/* Wait until a client connects to the instance. A client that connected * and closed its end before the wait is dropped and waited past. */unsigned long nn_pipe_accept(void *pipe, void *stop, const char **failed);/* Read up to size bytes into buffer, waiting until some arrive; *got gets * how many. ERROR_BROKEN_PIPE once the other end has closed and all it * wrote has been read. */unsigned long nn_pipe_read(void *pipe, void *buffer, unsigned long size, void *stop, unsigned long *got, const char **failed);/* Write all size bytes of buffer, waiting until the pipe takes them. */unsigned long nn_pipe_write(void *pipe, const void *buffer, unsigned long size, void *stop, const char **failed);/* Who the client connected to the instance is, read from its token by * impersonating it, which the pipe allows as soon as the client has * connected, before it has sent anything: *process gets its process ID; * *restricted is nonzero for a restricted token or one whose user is held * for deny only; and *sids gets the token's user and then each group the * token holds enabled and not for deny only, each SID in the string form * ConvertSidToStringSidW writes and ended by a NUL, *chars wide characters * in all, for the caller to release with LocalFree. The impersonation and * its end are this one call, so the calling thread runs nothing else as * the client. */unsigned long nn_pipe_client(void *pipe, unsigned long *process, int *restricted, wchar_t **sids, unsigned long *chars, const char **failed);/* Open a client's end of the pipe at name, asking for the access given, * and letting the daemon identify the client but not act as it. While * every instance is busy it waits for one, for as long as the daemon's * instances say and up to a fixed number of times, and gives * ERROR_PIPE_BUSY if none frees. */unsigned long nn_pipe_open(const wchar_t *name, unsigned long access, void **pipe, const char **failed);/* The SID of the account that owns the pipe the handle is open to, in the * string form ConvertSidToStringSidW writes, written to *sid for the * caller to release with LocalFree; *sid is NULL where the pipe has no * owner. Reading it takes READ_CONTROL on the handle, which the access * nn_pipe_open is given includes. */unsigned long nn_pipe_owner(void *pipe, wchar_t **sid, const char **failed);/* The integrity level of the pipe the handle is open to, its mandatory * label's RID (0x2000 for Medium, 0x1000 for Low), written to *level: that * of its label where it has one, and Medium where it has none, as Windows * reads an object with no label. A pipe a process running at Low makes * has the Low label, and one a process running at Medium or above makes * has none, so the level says whether a process below Medium made it. * Reading it takes READ_CONTROL on the handle. */unsigned long nn_pipe_integrity(void *pipe, unsigned long *level, const char **failed);/* The SID a daemon running with the token makes its pipe's owner, in the * string form ConvertSidToStringSidW writes, written to *sid for the * caller to release with LocalFree: BUILTIN\Administrators where the token * holds that group enabled, not for deny only, and with the right to own * objects as it, which is how an elevated administrator's token holds it, * and the token's user otherwise. A process without the restore * privilege can give an object it makes no other owner than its token's * user or a group its token holds that way. The token is the handle of * one opened for TOKEN_QUERY, or NULL for this process's. */unsigned long nn_token_pipe_owner(void *token, wchar_t **sid, const char **failed);/* Whether the pipe at path (\\.\pipe\NAME, NAME given apart as name) * exists, read from the listing of the machine's pipes, which connects to * no instance and does not wait: *present gets 1 or 0. The name is * compared as Windows compares a pipe's, ignoring case. */unsigned long nn_pipe_present(const wchar_t *path, const wchar_t *name, int *present, const char **failed);/* Which kind of account nn_account_sid is to find: a user, among which * the well-known accounts that run processes (SYSTEM, a service's virtual * account), or a group, local, global or well-known. */enum { NN_ACCOUNT_USER = 0, NN_ACCOUNT_GROUP = 1 };/* The SID an account's name names, as LookupAccountNameW finds one, in the * string form ConvertSidToStringSidW writes, to *sid for the caller to * release with LocalFree. A name with no backslash and no @ is an account * of this machine alone, which a domain's accounts of the same name never * answer, so a domain does not decide who a bare name is; NAME@DOMAIN and * DOMAIN\NAME name the domain. *sid is NULL where no account of the kind * has the name: ERROR_NONE_MAPPED, a name too long for any account, and an * account of another kind. Any other failure is returned as one, so a * lookup that could not be made is not taken for an answer. */unsigned long nn_account_sid(const wchar_t *name, int kind, wchar_t **sid, const char **failed);/* Every group the account whose SID is given in string form belongs to, as * Windows computes them for a logon of the account (local groups, domain * groups they nest in, and the well-known groups of every logon), from the * account itself and so whatever token a client presents: each SID in the * string form ConvertSidToStringSidW writes and ended by a NUL, *chars * wide characters in all, written to *sids for the caller to release with * LocalFree. Returns the Win32 error code of the call that failed where * Windows cannot say, as for an account it does not know. */unsigned long nn_account_groups(const wchar_t *sid, wchar_t **sids, unsigned long *chars, const char **failed);/* The name of the account whose SID is given in string form, DOMAIN\NAME * or NAME where the account has no domain, to *name for the caller to * release with LocalFree; *name is NULL where no account has the SID. */unsigned long nn_account_name(const wchar_t *sid, wchar_t **name, const char **failed);#endif#endif