From 8e2d624a588552f5d06f21fe37281615f3ec6296 Mon Sep 17 00:00:00 2001 From: Jelmer Vernooij Date: Mon, 3 Sep 2007 13:13:25 +0000 Subject: r24937: Merge tests spoolss RPC callbacks. (This used to be commit 9b256a0ca232ea6e89771bf73a1adf877273a752) --- source4/rpc_server/config.mk | 5 +- source4/rpc_server/dcerpc_server.c | 97 +----- source4/rpc_server/dcerpc_sock.c | 371 ---------------------- source4/rpc_server/service_rpc.c | 456 ++++++++++++++++++++++++++++ source4/rpc_server/spoolss/dcesrv_spoolss.c | 75 ++++- 5 files changed, 541 insertions(+), 463 deletions(-) delete mode 100644 source4/rpc_server/dcerpc_sock.c create mode 100644 source4/rpc_server/service_rpc.c (limited to 'source4/rpc_server') diff --git a/source4/rpc_server/config.mk b/source4/rpc_server/config.mk index 0de09b572e..7a39e56060 100644 --- a/source4/rpc_server/config.mk +++ b/source4/rpc_server/config.mk @@ -157,7 +157,8 @@ OBJ_FILES = \ PRIVATE_DEPENDENCIES = \ DCERPC_COMMON \ NDR_SPOOLSS \ - ntptr + ntptr \ + RPC_NDR_SPOOLSS # End MODULE dcerpc_spoolss ################################################ @@ -182,7 +183,6 @@ PUBLIC_HEADERS = dcerpc_server.h PUBLIC_PROTO_HEADER = dcerpc_server_proto.h OBJ_FILES = \ dcerpc_server.o \ - dcerpc_sock.o \ dcesrv_auth.o \ dcesrv_mgmt.o \ handles.o @@ -196,5 +196,6 @@ PRIVATE_DEPENDENCIES = \ [MODULE::DCESRV] INIT_FUNCTION = server_service_rpc_init +OBJ_FILES = service_rpc.o SUBSYSTEM = service PRIVATE_DEPENDENCIES = dcerpc_server diff --git a/source4/rpc_server/dcerpc_server.c b/source4/rpc_server/dcerpc_server.c index 35b37b3af6..466d35c373 100644 --- a/source4/rpc_server/dcerpc_server.c +++ b/source4/rpc_server/dcerpc_server.c @@ -39,11 +39,11 @@ extern const struct dcesrv_interface dcesrv_mgmt_interface; /* see if two endpoints match */ -static BOOL endpoints_match(const struct dcerpc_binding *ep1, +static bool endpoints_match(const struct dcerpc_binding *ep1, const struct dcerpc_binding *ep2) { if (ep1->transport != ep2->transport) { - return False; + return false; } if (!ep1->endpoint || !ep2->endpoint) { @@ -51,9 +51,9 @@ static BOOL endpoints_match(const struct dcerpc_binding *ep1, } if (strcasecmp(ep1->endpoint, ep2->endpoint) != 0) - return False; + return false; - return True; + return true; } /* @@ -1246,7 +1246,7 @@ _PUBLIC_ NTSTATUS dcesrv_output(struct dcesrv_connection *dce_conn, return status; } -static NTSTATUS dcesrv_init_context(TALLOC_CTX *mem_ctx, const char **endpoint_servers, struct dcesrv_context **_dce_ctx) +_PUBLIC_ NTSTATUS dcesrv_init_context(TALLOC_CTX *mem_ctx, const char **endpoint_servers, struct dcesrv_context **_dce_ctx) { NTSTATUS status; struct dcesrv_context *dce_ctx; @@ -1282,21 +1282,6 @@ static NTSTATUS dcesrv_init_context(TALLOC_CTX *mem_ctx, const char **endpoint_s return NT_STATUS_OK; } -/* - initialise the dcerpc server context for ncacn_np based services -*/ -_PUBLIC_ NTSTATUS dcesrv_init_ipc_context(TALLOC_CTX *mem_ctx, struct dcesrv_context **_dce_ctx) -{ - NTSTATUS status; - struct dcesrv_context *dce_ctx; - - status = dcesrv_init_context(mem_ctx, lp_dcerpc_endpoint_servers(), &dce_ctx); - NT_STATUS_NOT_OK_RETURN(status); - - *_dce_ctx = dce_ctx; - return NT_STATUS_OK; -} - /* the list of currently registered DCERPC endpoint servers. */ static struct ep_server { @@ -1379,78 +1364,18 @@ const struct dcesrv_critical_sizes *dcerpc_module_version(void) } /* - open the dcerpc server sockets + initialise the dcerpc server context for ncacn_np based services */ -static void dcesrv_task_init(struct task_server *task) +_PUBLIC_ NTSTATUS dcesrv_init_ipc_context(TALLOC_CTX *mem_ctx, struct dcesrv_context **_dce_ctx) { NTSTATUS status; struct dcesrv_context *dce_ctx; - struct dcesrv_endpoint *e; - task_server_set_title(task, "task[dcesrv]"); - - status = dcesrv_init_context(task->event_ctx, - lp_dcerpc_endpoint_servers(), - &dce_ctx); - if (!NT_STATUS_IS_OK(status)) goto failed; - - /* Make sure the directory for NCALRPC exists */ - if (!directory_exist(lp_ncalrpc_dir())) { - mkdir(lp_ncalrpc_dir(), 0755); - } - - for (e=dce_ctx->endpoint_list;e;e=e->next) { - switch (e->ep_description->transport) { - case NCACN_UNIX_STREAM: - status = dcesrv_add_ep_unix(dce_ctx, e, task->event_ctx, task->model_ops); - if (!NT_STATUS_IS_OK(status)) goto failed; - break; - - case NCALRPC: - status = dcesrv_add_ep_ncalrpc(dce_ctx, e, task->event_ctx, task->model_ops); - if (!NT_STATUS_IS_OK(status)) goto failed; - break; - - case NCACN_IP_TCP: - status = dcesrv_add_ep_tcp(dce_ctx, e, task->event_ctx, task->model_ops); - if (!NT_STATUS_IS_OK(status)) goto failed; - break; - - case NCACN_NP: - status = dcesrv_add_ep_np(dce_ctx, e, task->event_ctx, task->model_ops); - if (!NT_STATUS_IS_OK(status)) goto failed; - break; - - default: - status = NT_STATUS_NOT_SUPPORTED; - if (!NT_STATUS_IS_OK(status)) goto failed; - } - } - - return; -failed: - task_server_terminate(task, "Failed to startup dcerpc server task"); -} + status = dcesrv_init_context(mem_ctx, lp_dcerpc_endpoint_servers(), &dce_ctx); + NT_STATUS_NOT_OK_RETURN(status); -/* - called on startup of the smb server service It's job is to start - listening on all configured sockets -*/ -static NTSTATUS dcesrv_init(struct event_context *event_context, - const struct model_ops *model_ops) -{ - return task_server_startup(event_context, model_ops, dcesrv_task_init); + *_dce_ctx = dce_ctx; + return NT_STATUS_OK; } -NTSTATUS server_service_rpc_init(void) -{ - init_module_fn static_init[] = STATIC_dcerpc_server_MODULES; - init_module_fn *shared_init = load_samba_modules(NULL, "dcerpc_server"); - - run_init_functions(static_init); - run_init_functions(shared_init); - talloc_free(shared_init); - - return register_server_service("rpc", dcesrv_init); -} diff --git a/source4/rpc_server/dcerpc_sock.c b/source4/rpc_server/dcerpc_sock.c deleted file mode 100644 index 834758356b..0000000000 --- a/source4/rpc_server/dcerpc_sock.c +++ /dev/null @@ -1,371 +0,0 @@ -/* - Unix SMB/CIFS implementation. - - server side dcerpc using various kinds of sockets (tcp, unix domain) - - Copyright (C) Andrew Tridgell 2003 - Copyright (C) Stefan (metze) Metzmacher 2004-2005 - Copyright (C) Jelmer Vernooij 2004 - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . -*/ - -#include "includes.h" -#include "lib/socket/socket.h" -#include "lib/events/events.h" -#include "rpc_server/dcerpc_server.h" -#include "smbd/service_stream.h" -#include "smbd/service.h" -#include "lib/messaging/irpc.h" -#include "system/network.h" -#include "lib/socket/netif.h" -#include "auth/auth.h" - -struct dcesrv_socket_context { - const struct dcesrv_endpoint *endpoint; - struct dcesrv_context *dcesrv_ctx; -}; - -/* - write_fn callback for dcesrv_output() -*/ -static NTSTATUS dcerpc_write_fn(void *private_data, DATA_BLOB *out, size_t *nwritten) -{ - NTSTATUS status; - struct socket_context *sock = talloc_get_type(private_data, struct socket_context); - size_t sendlen; - - status = socket_send(sock, out, &sendlen); - NT_STATUS_IS_ERR_RETURN(status); - - *nwritten = sendlen; - return status; -} - -static void dcesrv_terminate_connection(struct dcesrv_connection *dce_conn, const char *reason) -{ - struct stream_connection *srv_conn; - srv_conn = talloc_get_type(dce_conn->transport.private_data, - struct stream_connection); - - stream_terminate_connection(srv_conn, reason); -} - -static void dcesrv_sock_report_output_data(struct dcesrv_connection *dcesrv_conn) -{ - struct stream_connection *srv_conn; - srv_conn = talloc_get_type(dcesrv_conn->transport.private_data, - struct stream_connection); - - if (srv_conn && srv_conn->event.fde) { - EVENT_FD_WRITEABLE(srv_conn->event.fde); - } -} - -static struct socket_address *dcesrv_sock_get_my_addr(struct dcesrv_connection *dcesrv_conn, TALLOC_CTX *mem_ctx) -{ - struct stream_connection *srv_conn; - srv_conn = talloc_get_type(dcesrv_conn->transport.private_data, - struct stream_connection); - - return socket_get_my_addr(srv_conn->socket, mem_ctx); -} - -static struct socket_address *dcesrv_sock_get_peer_addr(struct dcesrv_connection *dcesrv_conn, TALLOC_CTX *mem_ctx) -{ - struct stream_connection *srv_conn; - srv_conn = talloc_get_type(dcesrv_conn->transport.private_data, - struct stream_connection); - - return socket_get_peer_addr(srv_conn->socket, mem_ctx); -} - -static void dcesrv_sock_accept(struct stream_connection *srv_conn) -{ - NTSTATUS status; - struct dcesrv_socket_context *dcesrv_sock = - talloc_get_type(srv_conn->private, struct dcesrv_socket_context); - struct dcesrv_connection *dcesrv_conn = NULL; - struct auth_session_info *session_info = NULL; - - status = auth_anonymous_session_info(srv_conn, &session_info); - if (!NT_STATUS_IS_OK(status)) { - DEBUG(0,("dcesrv_sock_accept: auth_anonymous_session_info failed: %s\n", - nt_errstr(status))); - stream_terminate_connection(srv_conn, nt_errstr(status)); - return; - } - - status = dcesrv_endpoint_connect(dcesrv_sock->dcesrv_ctx, - srv_conn, - dcesrv_sock->endpoint, - session_info, - srv_conn->event.ctx, - srv_conn->msg_ctx, - srv_conn->server_id, - DCESRV_CALL_STATE_FLAG_MAY_ASYNC, - &dcesrv_conn); - if (!NT_STATUS_IS_OK(status)) { - DEBUG(0,("dcesrv_sock_accept: dcesrv_endpoint_connect failed: %s\n", - nt_errstr(status))); - stream_terminate_connection(srv_conn, nt_errstr(status)); - return; - } - - dcesrv_conn->transport.private_data = srv_conn; - dcesrv_conn->transport.report_output_data = dcesrv_sock_report_output_data; - dcesrv_conn->transport.get_my_addr = dcesrv_sock_get_my_addr; - dcesrv_conn->transport.get_peer_addr = dcesrv_sock_get_peer_addr; - - srv_conn->private = dcesrv_conn; - - irpc_add_name(srv_conn->msg_ctx, "rpc_server"); - - return; -} - -static void dcesrv_sock_recv(struct stream_connection *conn, uint16_t flags) -{ - NTSTATUS status; - struct dcesrv_connection *dce_conn = talloc_get_type(conn->private, struct dcesrv_connection); - DATA_BLOB tmp_blob; - size_t nread; - - if (dce_conn->processing) { - EVENT_FD_NOT_READABLE(conn->event.fde); - return; - } - - tmp_blob = data_blob_talloc(conn->socket, NULL, 0x1000); - if (tmp_blob.data == NULL) { - dcesrv_terminate_connection(dce_conn, "out of memory"); - return; - } - - status = socket_recv(conn->socket, tmp_blob.data, tmp_blob.length, &nread); - if (NT_STATUS_IS_ERR(status)) { - dcesrv_terminate_connection(dce_conn, nt_errstr(status)); - return; - } - if (nread == 0) { - talloc_free(tmp_blob.data); - return; - } - - tmp_blob.length = nread; - - dce_conn->processing = True; - status = dcesrv_input(dce_conn, &tmp_blob); - dce_conn->processing = False; - talloc_free(tmp_blob.data); - - EVENT_FD_READABLE(conn->event.fde); - - if (!NT_STATUS_IS_OK(status)) { - dcesrv_terminate_connection(dce_conn, nt_errstr(status)); - return; - } - - if (dce_conn->call_list && dce_conn->call_list->replies) { - EVENT_FD_WRITEABLE(conn->event.fde); - } -} - -static void dcesrv_sock_send(struct stream_connection *conn, uint16_t flags) -{ - struct dcesrv_connection *dce_conn = talloc_get_type(conn->private, struct dcesrv_connection); - NTSTATUS status; - - status = dcesrv_output(dce_conn, conn->socket, dcerpc_write_fn); - if (NT_STATUS_IS_ERR(status)) { - dcesrv_terminate_connection(dce_conn, "eof on socket"); - return; - } - - if (!dce_conn->call_list || !dce_conn->call_list->replies) { - EVENT_FD_NOT_WRITEABLE(conn->event.fde); - } -} - - -static const struct stream_server_ops dcesrv_stream_ops = { - .name = "rpc", - .accept_connection = dcesrv_sock_accept, - .recv_handler = dcesrv_sock_recv, - .send_handler = dcesrv_sock_send, -}; - - - -NTSTATUS dcesrv_add_ep_unix(struct dcesrv_context *dce_ctx, struct dcesrv_endpoint *e, - struct event_context *event_ctx, const struct model_ops *model_ops) -{ - struct dcesrv_socket_context *dcesrv_sock; - uint16_t port = 1; - NTSTATUS status; - - dcesrv_sock = talloc(event_ctx, struct dcesrv_socket_context); - NT_STATUS_HAVE_NO_MEMORY(dcesrv_sock); - - /* remember the endpoint of this socket */ - dcesrv_sock->endpoint = e; - dcesrv_sock->dcesrv_ctx = talloc_reference(dcesrv_sock, dce_ctx); - - status = stream_setup_socket(event_ctx, model_ops, &dcesrv_stream_ops, - "unix", e->ep_description->endpoint, &port, - dcesrv_sock); - if (!NT_STATUS_IS_OK(status)) { - DEBUG(0,("service_setup_stream_socket(path=%s) failed - %s\n", - e->ep_description->endpoint, nt_errstr(status))); - } - - return status; -} - -NTSTATUS dcesrv_add_ep_ncalrpc(struct dcesrv_context *dce_ctx, struct dcesrv_endpoint *e, - struct event_context *event_ctx, const struct model_ops *model_ops) -{ - struct dcesrv_socket_context *dcesrv_sock; - uint16_t port = 1; - char *full_path; - NTSTATUS status; - - if (!e->ep_description->endpoint) { - /* No identifier specified: use DEFAULT. - * DO NOT hardcode this value anywhere else. Rather, specify - * no endpoint and let the epmapper worry about it. */ - e->ep_description->endpoint = talloc_strdup(dce_ctx, "DEFAULT"); - } - - full_path = talloc_asprintf(dce_ctx, "%s/%s", lp_ncalrpc_dir(), e->ep_description->endpoint); - - dcesrv_sock = talloc(event_ctx, struct dcesrv_socket_context); - NT_STATUS_HAVE_NO_MEMORY(dcesrv_sock); - - /* remember the endpoint of this socket */ - dcesrv_sock->endpoint = e; - dcesrv_sock->dcesrv_ctx = talloc_reference(dcesrv_sock, dce_ctx); - - status = stream_setup_socket(event_ctx, model_ops, &dcesrv_stream_ops, - "unix", full_path, &port, dcesrv_sock); - if (!NT_STATUS_IS_OK(status)) { - DEBUG(0,("service_setup_stream_socket(identifier=%s,path=%s) failed - %s\n", - e->ep_description->endpoint, full_path, nt_errstr(status))); - } - return status; -} - - -/* - add a socket address to the list of events, one event per dcerpc endpoint -*/ -static NTSTATUS add_socket_rpc_pipe_iface(struct dcesrv_context *dce_ctx, struct dcesrv_endpoint *e, - struct event_context *event_ctx, const struct model_ops *model_ops) -{ - struct dcesrv_socket_context *dcesrv_sock; - NTSTATUS status; - - if (e->ep_description->endpoint == NULL) { - DEBUG(0, ("Endpoint mandatory for named pipes\n")); - return NT_STATUS_INVALID_PARAMETER; - } - - dcesrv_sock = talloc(event_ctx, struct dcesrv_socket_context); - NT_STATUS_HAVE_NO_MEMORY(dcesrv_sock); - - /* remember the endpoint of this socket */ - dcesrv_sock->endpoint = e; - dcesrv_sock->dcesrv_ctx = talloc_reference(dcesrv_sock, dce_ctx); - - status = NT_STATUS_OK; -#if 0 - - status = stream_setup_smb_pipe(event_ctx, model_ops, &dcesrv_stream_ops, - e->ep_description->endpoint, dcesrv_sock); - if (!NT_STATUS_IS_OK(status)) { - DEBUG(0,("service_setup_stream_socket(path=%s) failed - %s\n", - e->ep_description->endpoint, nt_errstr(status))); - } -#endif - return status; -} - -NTSTATUS dcesrv_add_ep_np(struct dcesrv_context *dce_ctx, struct dcesrv_endpoint *e, - struct event_context *event_ctx, const struct model_ops *model_ops) -{ - NTSTATUS status; - - status = add_socket_rpc_pipe_iface(dce_ctx, e, event_ctx, model_ops); - NT_STATUS_NOT_OK_RETURN(status); - - return status; -} - -/* - add a socket address to the list of events, one event per dcerpc endpoint -*/ -static NTSTATUS add_socket_rpc_tcp_iface(struct dcesrv_context *dce_ctx, struct dcesrv_endpoint *e, - struct event_context *event_ctx, const struct model_ops *model_ops, - const char *address) -{ - struct dcesrv_socket_context *dcesrv_sock; - uint16_t port = 0; - NTSTATUS status; - - if (e->ep_description->endpoint) { - port = atoi(e->ep_description->endpoint); - } - - dcesrv_sock = talloc(event_ctx, struct dcesrv_socket_context); - NT_STATUS_HAVE_NO_MEMORY(dcesrv_sock); - - /* remember the endpoint of this socket */ - dcesrv_sock->endpoint = e; - dcesrv_sock->dcesrv_ctx = talloc_reference(dcesrv_sock, dce_ctx); - - status = stream_setup_socket(event_ctx, model_ops, &dcesrv_stream_ops, - "ipv4", address, &port, dcesrv_sock); - if (!NT_STATUS_IS_OK(status)) { - DEBUG(0,("service_setup_stream_socket(address=%s,port=%u) failed - %s\n", - address, port, nt_errstr(status))); - } - - if (e->ep_description->endpoint == NULL) { - e->ep_description->endpoint = talloc_asprintf(dce_ctx, "%d", port); - } - - return status; -} - -NTSTATUS dcesrv_add_ep_tcp(struct dcesrv_context *dce_ctx, struct dcesrv_endpoint *e, - struct event_context *event_ctx, const struct model_ops *model_ops) -{ - NTSTATUS status; - - /* Add TCP/IP sockets */ - if (lp_interfaces() && lp_bind_interfaces_only()) { - int num_interfaces = iface_count(); - int i; - for(i = 0; i < num_interfaces; i++) { - const char *address = iface_n_ip(i); - status = add_socket_rpc_tcp_iface(dce_ctx, e, event_ctx, model_ops, address); - NT_STATUS_NOT_OK_RETURN(status); - } - } else { - status = add_socket_rpc_tcp_iface(dce_ctx, e, event_ctx, model_ops, lp_socket_address()); - NT_STATUS_NOT_OK_RETURN(status); - } - - return NT_STATUS_OK; -} diff --git a/source4/rpc_server/service_rpc.c b/source4/rpc_server/service_rpc.c new file mode 100644 index 0000000000..6d70dd0bae --- /dev/null +++ b/source4/rpc_server/service_rpc.c @@ -0,0 +1,456 @@ +/* + Unix SMB/CIFS implementation. + + smbd-specific dcerpc server code + + Copyright (C) Andrew Tridgell 2003-2005 + Copyright (C) Stefan (metze) Metzmacher 2004-2005 + Copyright (C) Jelmer Vernooij 2004,2007 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#include "includes.h" +#include "librpc/gen_ndr/ndr_dcerpc.h" +#include "auth/auth.h" +#include "auth/gensec/gensec.h" +#include "lib/util/dlinklist.h" +#include "rpc_server/dcerpc_server.h" +#include "lib/events/events.h" +#include "smbd/service_task.h" +#include "smbd/service_stream.h" +#include "smbd/service.h" +#include "system/filesys.h" +#include "libcli/security/security.h" +#include "lib/socket/socket.h" +#include "lib/messaging/irpc.h" +#include "system/network.h" +#include "lib/socket/netif.h" +#include "build.h" + +struct dcesrv_socket_context { + const struct dcesrv_endpoint *endpoint; + struct dcesrv_context *dcesrv_ctx; +}; + +/* + write_fn callback for dcesrv_output() +*/ +static NTSTATUS dcerpc_write_fn(void *private_data, DATA_BLOB *out, size_t *nwritten) +{ + NTSTATUS status; + struct socket_context *sock = talloc_get_type(private_data, struct socket_context); + size_t sendlen; + + status = socket_send(sock, out, &sendlen); + NT_STATUS_IS_ERR_RETURN(status); + + *nwritten = sendlen; + return status; +} + +static void dcesrv_terminate_connection(struct dcesrv_connection *dce_conn, const char *reason) +{ + struct stream_connection *srv_conn; + srv_conn = talloc_get_type(dce_conn->transport.private_data, + struct stream_connection); + + stream_terminate_connection(srv_conn, reason); +} + +static void dcesrv_sock_report_output_data(struct dcesrv_connection *dcesrv_conn) +{ + struct stream_connection *srv_conn; + srv_conn = talloc_get_type(dcesrv_conn->transport.private_data, + struct stream_connection); + + if (srv_conn && srv_conn->event.fde) { + EVENT_FD_WRITEABLE(srv_conn->event.fde); + } +} + +static struct socket_address *dcesrv_sock_get_my_addr(struct dcesrv_connection *dcesrv_conn, TALLOC_CTX *mem_ctx) +{ + struct stream_connection *srv_conn; + srv_conn = talloc_get_type(dcesrv_conn->transport.private_data, + struct stream_connection); + + return socket_get_my_addr(srv_conn->socket, mem_ctx); +} + +static struct socket_address *dcesrv_sock_get_peer_addr(struct dcesrv_connection *dcesrv_conn, TALLOC_CTX *mem_ctx) +{ + struct stream_connection *srv_conn; + srv_conn = talloc_get_type(dcesrv_conn->transport.private_data, + struct stream_connection); + + return socket_get_peer_addr(srv_conn->socket, mem_ctx); +} + +static void dcesrv_sock_accept(struct stream_connection *srv_conn) +{ + NTSTATUS status; + struct dcesrv_socket_context *dcesrv_sock = + talloc_get_type(srv_conn->private, struct dcesrv_socket_context); + struct dcesrv_connection *dcesrv_conn = NULL; + struct auth_session_info *session_info = NULL; + + status = auth_anonymous_session_info(srv_conn, &session_info); + if (!NT_STATUS_IS_OK(status)) { + DEBUG(0,("dcesrv_sock_accept: auth_anonymous_session_info failed: %s\n", + nt_errstr(status))); + stream_terminate_connection(srv_conn, nt_errstr(status)); + return; + } + + status = dcesrv_endpoint_connect(dcesrv_sock->dcesrv_ctx, + srv_conn, + dcesrv_sock->endpoint, + session_info, + srv_conn->event.ctx, + srv_conn->msg_ctx, + srv_conn->server_id, + DCESRV_CALL_STATE_FLAG_MAY_ASYNC, + &dcesrv_conn); + if (!NT_STATUS_IS_OK(status)) { + DEBUG(0,("dcesrv_sock_accept: dcesrv_endpoint_connect failed: %s\n", + nt_errstr(status))); + stream_terminate_connection(srv_conn, nt_errstr(status)); + return; + } + + dcesrv_conn->transport.private_data = srv_conn; + dcesrv_conn->transport.report_output_data = dcesrv_sock_report_output_data; + dcesrv_conn->transport.get_my_addr = dcesrv_sock_get_my_addr; + dcesrv_conn->transport.get_peer_addr = dcesrv_sock_get_peer_addr; + + srv_conn->private = dcesrv_conn; + + irpc_add_name(srv_conn->msg_ctx, "rpc_server"); + + return; +} + +static void dcesrv_sock_recv(struct stream_connection *conn, uint16_t flags) +{ + NTSTATUS status; + struct dcesrv_connection *dce_conn = talloc_get_type(conn->private, struct dcesrv_connection); + DATA_BLOB tmp_blob; + size_t nread; + + if (dce_conn->processing) { + EVENT_FD_NOT_READABLE(conn->event.fde); + return; + } + + tmp_blob = data_blob_talloc(conn->socket, NULL, 0x1000); + if (tmp_blob.data == NULL) { + dcesrv_terminate_connection(dce_conn, "out of memory"); + return; + } + + status = socket_recv(conn->socket, tmp_blob.data, tmp_blob.length, &nread); + if (NT_STATUS_IS_ERR(status)) { + dcesrv_terminate_connection(dce_conn, nt_errstr(status)); + return; + } + if (nread == 0) { + talloc_free(tmp_blob.data); + return; + } + + tmp_blob.length = nread; + + dce_conn->processing = True; + status = dcesrv_input(dce_conn, &tmp_blob); + dce_conn->processing = False; + talloc_free(tmp_blob.data); + + EVENT_FD_READABLE(conn->event.fde); + + if (!NT_STATUS_IS_OK(status)) { + dcesrv_terminate_connection(dce_conn, nt_errstr(status)); + return; + } + + if (dce_conn->call_list && dce_conn->call_list->replies) { + EVENT_FD_WRITEABLE(conn->event.fde); + } +} + +static void dcesrv_sock_send(struct stream_connection *conn, uint16_t flags) +{ + struct dcesrv_connection *dce_conn = talloc_get_type(conn->private, struct dcesrv_connection); + NTSTATUS status; + + status = dcesrv_output(dce_conn, conn->socket, dcerpc_write_fn); + if (NT_STATUS_IS_ERR(status)) { + dcesrv_terminate_connection(dce_conn, "eof on socket"); + return; + } + + if (!dce_conn->call_list || !dce_conn->call_list->replies) { + EVENT_FD_NOT_WRITEABLE(conn->event.fde); + } +} + + +static const struct stream_server_ops dcesrv_stream_ops = { + .name = "rpc", + .accept_connection = dcesrv_sock_accept, + .recv_handler = dcesrv_sock_recv, + .send_handler = dcesrv_sock_send, +}; + + + +NTSTATUS dcesrv_add_ep_unix(struct dcesrv_context *dce_ctx, struct dcesrv_endpoint *e, + struct event_context *event_ctx, const struct model_ops *model_ops) +{ + struct dcesrv_socket_context *dcesrv_sock; + uint16_t port = 1; + NTSTATUS status; + + dcesrv_sock = talloc(event_ctx, struct dcesrv_socket_context); + NT_STATUS_HAVE_NO_MEMORY(dcesrv_sock); + + /* remember the endpoint of this socket */ + dcesrv_sock->endpoint = e; + dcesrv_sock->dcesrv_ctx = talloc_reference(dcesrv_sock, dce_ctx); + + status = stream_setup_socket(event_ctx, model_ops, &dcesrv_stream_ops, + "unix", e->ep_description->endpoint, &port, + dcesrv_sock); + if (!NT_STATUS_IS_OK(status)) { + DEBUG(0,("service_setup_stream_socket(path=%s) failed - %s\n", + e->ep_description->endpoint, nt_errstr(status))); + } + + return status; +} + +NTSTATUS dcesrv_add_ep_ncalrpc(struct dcesrv_context *dce_ctx, struct dcesrv_endpoint *e, + struct event_context *event_ctx, const struct model_ops *model_ops) +{ + struct dcesrv_socket_context *dcesrv_sock; + uint16_t port = 1; + char *full_path; + NTSTATUS status; + + if (!e->ep_description->endpoint) { + /* No identifier specified: use DEFAULT. + * DO NOT hardcode this value anywhere else. Rather, specify + * no endpoint and let the epmapper worry about it. */ + e->ep_description->endpoint = talloc_strdup(dce_ctx, "DEFAULT"); + } + + full_path = talloc_asprintf(dce_ctx, "%s/%s", lp_ncalrpc_dir(), e->ep_description->endpoint); + + dcesrv_sock = talloc(event_ctx, struct dcesrv_socket_context); + NT_STATUS_HAVE_NO_MEMORY(dcesrv_sock); + + /* remember the endpoint of this socket */ + dcesrv_sock->endpoint = e; + dcesrv_sock->dcesrv_ctx = talloc_reference(dcesrv_sock, dce_ctx); + + status = stream_setup_socket(event_ctx, model_ops, &dcesrv_stream_ops, + "unix", full_path, &port, dcesrv_sock); + if (!NT_STATUS_IS_OK(status)) { + DEBUG(0,("service_setup_stream_socket(identifier=%s,path=%s) failed - %s\n", + e->ep_description->endpoint, full_path, nt_errstr(status))); + } + return status; +} + + +/* + add a socket address to the list of events, one event per dcerpc endpoint +*/ +static NTSTATUS add_socket_rpc_pipe_iface(struct dcesrv_context *dce_ctx, struct dcesrv_endpoint *e, + struct event_context *event_ctx, const struct model_ops *model_ops) +{ + struct dcesrv_socket_context *dcesrv_sock; + NTSTATUS status; + + if (e->ep_description->endpoint == NULL) { + DEBUG(0, ("Endpoint mandatory for named pipes\n")); + return NT_STATUS_INVALID_PARAMETER; + } + + dcesrv_sock = talloc(event_ctx, struct dcesrv_socket_context); + NT_STATUS_HAVE_NO_MEMORY(dcesrv_sock); + + /* remember the endpoint of this socket */ + dcesrv_sock->endpoint = e; + dcesrv_sock->dcesrv_ctx = talloc_reference(dcesrv_sock, dce_ctx); + + status = NT_STATUS_OK; +#if 0 + + status = stream_setup_smb_pipe(event_ctx, model_ops, &dcesrv_stream_ops, + e->ep_description->endpoint, dcesrv_sock); + if (!NT_STATUS_IS_OK(status)) { + DEBUG(0,("service_setup_stream_socket(path=%s) failed - %s\n", + e->ep_description->endpoint, nt_errstr(status))); + } +#endif + return status; +} + +NTSTATUS dcesrv_add_ep_np(struct dcesrv_context *dce_ctx, struct dcesrv_endpoint *e, + struct event_context *event_ctx, const struct model_ops *model_ops) +{ + NTSTATUS status; + + status = add_socket_rpc_pipe_iface(dce_ctx, e, event_ctx, model_ops); + NT_STATUS_NOT_OK_RETURN(status); + + return status; +} + +/* + add a socket address to the list of events, one event per dcerpc endpoint +*/ +static NTSTATUS add_socket_rpc_tcp_iface(struct dcesrv_context *dce_ctx, struct dcesrv_endpoint *e, + struct event_context *event_ctx, const struct model_ops *model_ops, + const char *address) +{ + struct dcesrv_socket_context *dcesrv_sock; + uint16_t port = 0; + NTSTATUS status; + + if (e->ep_description->endpoint) { + port = atoi(e->ep_description->endpoint); + } + + dcesrv_sock = talloc(event_ctx, struct dcesrv_socket_context); + NT_STATUS_HAVE_NO_MEMORY(dcesrv_sock); + + /* remember the endpoint of this socket */ + dcesrv_sock->endpoint = e; + dcesrv_sock->dcesrv_ctx = talloc_reference(dcesrv_sock, dce_ctx); + + status = stream_setup_socket(event_ctx, model_ops, &dcesrv_stream_ops, + "ipv4", address, &port, dcesrv_sock); + if (!NT_STATUS_IS_OK(status)) { + DEBUG(0,("service_setup_stream_socket(address=%s,port=%u) failed - %s\n", + address, port, nt_errstr(status))); + } + + if (e->ep_description->endpoint == NULL) { + e->ep_description->endpoint = talloc_asprintf(dce_ctx, "%d", port); + } + + return status; +} + +NTSTATUS dcesrv_add_ep_tcp(struct dcesrv_context *dce_ctx, struct dcesrv_endpoint *e, + struct event_context *event_ctx, const struct model_ops *model_ops) +{ + NTSTATUS status; + + /* Add TCP/IP sockets */ + if (lp_interfaces() && lp_bind_interfaces_only()) { + int num_interfaces = iface_count(); + int i; + for(i = 0; i < num_interfaces; i++) { + const char *address = iface_n_ip(i); + status = add_socket_rpc_tcp_iface(dce_ctx, e, event_ctx, model_ops, address); + NT_STATUS_NOT_OK_RETURN(status); + } + } else { + status = add_socket_rpc_tcp_iface(dce_ctx, e, event_ctx, model_ops, lp_socket_address()); + NT_STATUS_NOT_OK_RETURN(status); + } + + return NT_STATUS_OK; +} + + +NTSTATUS dcesrv_add_ep(struct dcesrv_context *dce_ctx, struct dcesrv_endpoint *e, + struct event_context *event_ctx, const struct model_ops *model_ops) +{ + switch (e->ep_description->transport) { + case NCACN_UNIX_STREAM: + return dcesrv_add_ep_unix(dce_ctx, e, event_ctx, model_ops); + + case NCALRPC: + return dcesrv_add_ep_ncalrpc(dce_ctx, e, event_ctx, model_ops); + + case NCACN_IP_TCP: + return dcesrv_add_ep_tcp(dce_ctx, e, event_ctx, model_ops); + + case NCACN_NP: + return dcesrv_add_ep_np(dce_ctx, e, event_ctx, model_ops); + + default: + return NT_STATUS_NOT_SUPPORTED; + } +} + +/* + open the dcerpc server sockets +*/ +static void dcesrv_task_init(struct task_server *task) +{ + NTSTATUS status; + struct dcesrv_context *dce_ctx; + struct dcesrv_endpoint *e; + + task_server_set_title(task, "task[dcesrv]"); + + status = dcesrv_init_context(task->event_ctx, + lp_dcerpc_endpoint_servers(), + &dce_ctx); + if (!NT_STATUS_IS_OK(status)) goto failed; + + /* Make sure the directory for NCALRPC exists */ + if (!directory_exist(lp_ncalrpc_dir())) { + mkdir(lp_ncalrpc_dir(), 0755); + } + + for (e=dce_ctx->endpoint_list;e;e=e->next) { + status = dcesrv_add_ep(dce_ctx, e, task->event_ctx, task->model_ops); + if (!NT_STATUS_IS_OK(status)) goto failed; + } + + return; +failed: + task_server_terminate(task, "Failed to startup dcerpc server task"); +} + +/* + called on startup of the smb server service It's job is to start + listening on all configured sockets +*/ +static NTSTATUS dcesrv_init(struct event_context *event_context, + const struct model_ops *model_ops) +{ + return task_server_startup(event_context, model_ops, dcesrv_task_init); +} + +NTSTATUS server_service_rpc_init(void) +{ + init_module_fn static_init[] = STATIC_dcerpc_server_MODULES; + init_module_fn *shared_init = load_samba_modules(NULL, "dcerpc_server"); + + run_init_functions(static_init); + run_init_functions(shared_init); + + talloc_free(shared_init); + + return register_server_service("rpc", dcesrv_init); +} + + diff --git a/source4/rpc_server/spoolss/dcesrv_spoolss.c b/source4/rpc_server/spoolss/dcesrv_spoolss.c index 2dca178245..cd39bcda3d 100644 --- a/source4/rpc_server/spoolss/dcesrv_spoolss.c +++ b/source4/rpc_server/spoolss/dcesrv_spoolss.c @@ -27,6 +27,12 @@ #include "ntptr/ntptr.h" #include "lib/socket/socket.h" #include "smbd/service_stream.h" +#include "librpc/gen_ndr/ndr_spoolss_c.h" +#include "auth/credentials/credentials.h" + +enum spoolss_handle { + SPOOLSS_NOTIFY +}; #define SPOOLSS_BUFFER_UNION(fn,info,level) \ ((info)?ndr_size_##fn(info, level, 0):0) @@ -1036,7 +1042,16 @@ static WERROR dcesrv_spoolss_RouterFindFirstPrinterChangeNotificationOld(struct static WERROR dcesrv_spoolss_ReplyOpenPrinter(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, struct spoolss_ReplyOpenPrinter *r) { - DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR); + struct dcesrv_handle *handle; + + handle = dcesrv_handle_new(dce_call->context, SPOOLSS_NOTIFY); + W_ERROR_HAVE_NO_MEMORY(handle); + + /* For now, just return a handle */ + + *r->out.handle = handle->wire_handle; + + return WERR_OK; } @@ -1056,9 +1071,16 @@ static WERROR dcesrv_spoolss_RouterReplyPrinter(struct dcesrv_call_state *dce_ca static WERROR dcesrv_spoolss_ReplyClosePrinter(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, struct spoolss_ReplyClosePrinter *r) { - DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR); -} + struct dcesrv_handle *handle; + + DCESRV_PULL_HANDLE_WERR(handle, r->in.handle, SPOOLSS_NOTIFY); + talloc_free(handle); + + ZERO_STRUCTP(r->out.handle); + + return WERR_OK; +} /* spoolss_AddPortEx @@ -1106,11 +1128,56 @@ static WERROR dcesrv_spoolss_ResetPrinterEx(struct dcesrv_call_state *dce_call, static WERROR dcesrv_spoolss_RemoteFindFirstPrinterChangeNotifyEx(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, struct spoolss_RemoteFindFirstPrinterChangeNotifyEx *r) { + struct dcerpc_pipe *p; + struct dcerpc_binding *binding; + NTSTATUS status; + struct spoolss_ReplyOpenPrinter rop; + struct cli_credentials *creds; + struct policy_handle notify_handle; + + DEBUG(2, ("Received RFFPCNex from %s\n", r->in.str)); + /* - * TODO: for now just return ok, + * TODO: for now just open a connection to the client and drop it again * to keep the w2k3 PrintServer * happy to allow to open the Add Printer GUI + * and the torture suite passing */ + + binding = talloc_zero(mem_ctx, struct dcerpc_binding); + + binding->transport = NCACN_NP; + if (strncmp(r->in.str, "\\\\", 2)) + return WERR_INVALID_COMPUTERNAME; + binding->host = r->in.str+2; + + creds = cli_credentials_init_anon(mem_ctx); /* FIXME: Use machine credentials instead ? */ + + status = dcerpc_pipe_connect_b(mem_ctx, &p, binding, &ndr_table_spoolss, + creds, NULL); + + if (NT_STATUS_IS_ERR(status)) { + DEBUG(0, ("unable to call back to %s\n", r->in.str)); + return WERR_SERVER_UNAVAILABLE; + } + + ZERO_STRUCT(rop); + rop.in.server_name = lp_netbios_name(); + W_ERROR_HAVE_NO_MEMORY(rop.in.server_name); + rop.in.printer_local = 0; + rop.in.type = REG_NONE; + rop.in.unknown1 = 0; + rop.in.unknown2 = 0; + rop.out.handle = ¬ify_handle; + + status = dcerpc_spoolss_ReplyOpenPrinter(p, mem_ctx, &rop); + if (NT_STATUS_IS_ERR(status)) { + DEBUG(0, ("unable to open remote printer %s\n", r->in.str)); + return WERR_SERVER_UNAVAILABLE; + } + + talloc_free(p); + return WERR_OK; } -- cgit