ms-nfs41-client/daemon/upcall.c

214 lines
7 KiB
C
Raw Normal View History

/* Copyright (c) 2010, 2011
* The Regents of the University of Michigan
* All Rights Reserved
*
* Olga Kornievskaia <aglo@umich.edu>
* Casey Bodley <cbodley@umich.edu>
*
* Permission is granted to use, copy and redistribute this software
* for noncommercial education and research purposes, so long as no
* fee is charged, and so long as the name of the University of Michigan
* is not used in any advertising or publicity pertaining to the use
* or distribution of this software without specific, written prior
* authorization. Permission to modify or otherwise create derivative
* works of this software is not granted.
*
* This software is provided as is, without representation or warranty
* of any kind either express or implied, including without limitation
* the implied warranties of merchantability, fitness for a particular
* purpose, or noninfringement. The Regents of the University of
* Michigan shall not be liable for any damages, including special,
* indirect, incidental, or consequential damages, with respect to any
* claim arising out of or in connection with the use of the software,
* even if it has been or is hereafter advised of the possibility of
* such damages.
*/
#include <Windows.h>
#include <stdio.h>
#include "upcall.h"
#include "daemon_debug.h"
#include "util.h"
extern const nfs41_upcall_op nfs41_op_mount;
extern const nfs41_upcall_op nfs41_op_unmount;
extern const nfs41_upcall_op nfs41_op_open;
extern const nfs41_upcall_op nfs41_op_close;
extern const nfs41_upcall_op nfs41_op_read;
extern const nfs41_upcall_op nfs41_op_write;
extern const nfs41_upcall_op nfs41_op_lock;
extern const nfs41_upcall_op nfs41_op_unlock;
extern const nfs41_upcall_op nfs41_op_readdir;
extern const nfs41_upcall_op nfs41_op_getattr;
extern const nfs41_upcall_op nfs41_op_setattr;
2011-09-22 14:56:11 -04:00
extern const nfs41_upcall_op nfs41_op_getexattr;
extern const nfs41_upcall_op nfs41_op_setexattr;
extern const nfs41_upcall_op nfs41_op_symlink;
extern const nfs41_upcall_op nfs41_op_volume;
first stab at handling security irp Basic handling of owner and group security query (no dacl). Added new upcall for NFS41_ACL_QUERY (driver and daemon code). Daemon, upon getting NFS41_ACL_QUERY first places a getattr that has owner, group attribute request. We currently don't cache them!!! Then, we parse nfs4name format (ie user@domain or group@domain) into user and domain. We currently ignore domain part!!! Then, we assume that whatever we are mapping is "known" locally (ie LookupAccountName() api which retrieves a SID for a given name). Mapping from name to SID can only be done in the userland. We then copy the bytes via the upcall pipe to the kernel. If the received user or group cant be mapped via LookupAccoundName(), we create a well known null SID as the reply. Kernel creates a security descriptor in the absolute-format and adds owner and group sids to it. Important: RtlSetOwner/Group functions only work with absolute-format security descriptor, however the reply to the user needs to be in the self-relative format. The way security query works is that it passes us a buffer to be filled with the security context. However the user doesn't know how big the buffer should be so, the user is allowed to pass a null buffer and have the kernel return how much memory is needed. This leads to 2 security queries => 2 NFS41_ACL_QUERY upcalls => 2 getattr rpcs... It should be improved. TODO: - need to add caching of owner/group attributes for a file? - need to add calls to LDAP for more general mapping? - need to cache reply of the ACL if supplied length is 0?
2011-03-15 16:31:52 -04:00
extern const nfs41_upcall_op nfs41_op_getacl;
extern const nfs41_upcall_op nfs41_op_setacl;
static const nfs41_upcall_op *g_upcall_op_table[] = {
&nfs41_op_mount,
&nfs41_op_unmount,
&nfs41_op_open,
&nfs41_op_close,
&nfs41_op_read,
&nfs41_op_write,
&nfs41_op_lock,
&nfs41_op_unlock,
&nfs41_op_readdir,
&nfs41_op_getattr,
&nfs41_op_setattr,
2011-09-22 14:56:11 -04:00
&nfs41_op_getexattr,
&nfs41_op_setexattr,
&nfs41_op_symlink,
&nfs41_op_volume,
first stab at handling security irp Basic handling of owner and group security query (no dacl). Added new upcall for NFS41_ACL_QUERY (driver and daemon code). Daemon, upon getting NFS41_ACL_QUERY first places a getattr that has owner, group attribute request. We currently don't cache them!!! Then, we parse nfs4name format (ie user@domain or group@domain) into user and domain. We currently ignore domain part!!! Then, we assume that whatever we are mapping is "known" locally (ie LookupAccountName() api which retrieves a SID for a given name). Mapping from name to SID can only be done in the userland. We then copy the bytes via the upcall pipe to the kernel. If the received user or group cant be mapped via LookupAccoundName(), we create a well known null SID as the reply. Kernel creates a security descriptor in the absolute-format and adds owner and group sids to it. Important: RtlSetOwner/Group functions only work with absolute-format security descriptor, however the reply to the user needs to be in the self-relative format. The way security query works is that it passes us a buffer to be filled with the security context. However the user doesn't know how big the buffer should be so, the user is allowed to pass a null buffer and have the kernel return how much memory is needed. This leads to 2 security queries => 2 NFS41_ACL_QUERY upcalls => 2 getattr rpcs... It should be improved. TODO: - need to add caching of owner/group attributes for a file? - need to add calls to LDAP for more general mapping? - need to cache reply of the ACL if supplied length is 0?
2011-03-15 16:31:52 -04:00
&nfs41_op_getacl,
&nfs41_op_setacl,
NULL,
NULL
};
static const uint32_t g_upcall_op_table_size = ARRAYSIZE(g_upcall_op_table);
int upcall_parse(
IN unsigned char *buffer,
IN uint32_t length,
OUT nfs41_upcall *upcall)
{
int status;
const nfs41_upcall_op *op;
DWORD version;
ZeroMemory(upcall, sizeof(nfs41_upcall));
if (!length) {
eprintf("empty upcall\n");
upcall->status = status = 102;
goto out;
}
dprintf(2, "received %d bytes upcall data: processing upcall\n", length);
2010-10-12 09:58:23 -04:00
print_hexbuf(4, (unsigned char *)"upcall buffer: ", buffer, length);
/* parse common elements */
status = safe_read(&buffer, &length, &version, sizeof(uint32_t));
if (status) goto out;
status = safe_read(&buffer, &length, &upcall->xid, sizeof(uint64_t));
if (status) goto out;
status = safe_read(&buffer, &length, &upcall->opcode, sizeof(uint32_t));
if (status) goto out;
status = safe_read(&buffer, &length, &upcall->root_ref, sizeof(HANDLE));
if (status) goto out;
status = safe_read(&buffer, &length, &upcall->state_ref, sizeof(HANDLE));
if (status) goto out;
dprintf(2, "version=%d xid=%d opcode=%s session=0x%x open_state=0x%x\n",
version, upcall->xid, opcode2string(upcall->opcode), upcall->root_ref,
upcall->state_ref);
if (version != NFS41D_VERSION) {
eprintf("received version %d expecting version %d\n", version, NFS41D_VERSION);
upcall->status = status = NFSD_VERSION_MISMATCH;
goto out;
}
if (upcall->opcode >= g_upcall_op_table_size) {
status = ERROR_NOT_SUPPORTED;
eprintf("unrecognized upcall opcode %d!\n", upcall->opcode);
goto out;
}
if (upcall->root_ref != INVALID_HANDLE_VALUE)
nfs41_root_ref(upcall->root_ref);
if (upcall->state_ref != INVALID_HANDLE_VALUE)
nfs41_open_state_ref(upcall->state_ref);
/* parse the operation's arguments */
op = g_upcall_op_table[upcall->opcode];
if (op && op->parse) {
status = op->parse(buffer, length, upcall);
if (status) {
eprintf("parsing of upcall '%s' failed with %d.\n",
opcode2string(upcall->opcode), status);
goto out;
}
}
out:
return status;
}
int upcall_handle(
IN nfs41_upcall *upcall)
{
int status = NO_ERROR;
const nfs41_upcall_op *op;
op = g_upcall_op_table[upcall->opcode];
if (op == NULL || op->handle == NULL) {
status = ERROR_NOT_SUPPORTED;
eprintf("upcall '%s' missing handle function!\n",
opcode2string(upcall->opcode));
goto out;
}
upcall->status = op->handle(upcall);
out:
return status;
}
2011-08-26 13:54:54 -04:00
#pragma warning (disable : 4706) /* assignment within conditional expression */
2010-11-02 14:40:52 -04:00
void upcall_marshall(
IN nfs41_upcall *upcall,
OUT unsigned char *buffer,
IN uint32_t length,
OUT uint32_t *length_out)
{
const nfs41_upcall_op *op;
unsigned char *orig_buf = buffer;
const uint32_t total = length, orig_len = length;
/* marshall common elements */
write_downcall:
length = orig_len;
buffer = orig_buf;
safe_write(&buffer, &length, &upcall->xid, sizeof(upcall->xid));
safe_write(&buffer, &length, &upcall->opcode, sizeof(upcall->opcode));
safe_write(&buffer, &length, &upcall->status, sizeof(upcall->status));
safe_write(&buffer, &length, &upcall->last_error, sizeof(upcall->last_error));
if (upcall->status)
goto out;
/* marshall the operation's results */
op = g_upcall_op_table[upcall->opcode];
if (op && op->marshall) {
2011-08-24 17:07:45 -04:00
if ((upcall->status = op->marshall(buffer, &length, upcall)))
goto write_downcall;
}
out:
*length_out = total - length;
}
void upcall_cancel(
IN nfs41_upcall *upcall)
{
const nfs41_upcall_op *op = g_upcall_op_table[upcall->opcode];
if (op && op->cancel)
op->cancel(upcall);
}
void upcall_cleanup(
IN nfs41_upcall *upcall)
{
const nfs41_upcall_op *op = g_upcall_op_table[upcall->opcode];
if (op && op->cleanup && upcall->status != NFSD_VERSION_MISMATCH)
op->cleanup(upcall);
2010-10-27 16:18:12 -04:00
if (upcall->state_ref && upcall->state_ref != INVALID_HANDLE_VALUE) {
2010-10-27 16:18:12 -04:00
nfs41_open_state_deref(upcall->state_ref);
upcall->state_ref = NULL;
}
if (upcall->root_ref && upcall->root_ref != INVALID_HANDLE_VALUE) {
nfs41_root_deref(upcall->root_ref);
upcall->root_ref = NULL;
}
}