[SCM] Samba Shared Repository - branch master updated

Stefan Metzmacher metze at samba.org
Fri Jan 8 06:56:04 MST 2010


The branch, master has been updated
       via  0588f34... s4-kdc: Migrate tcp connections to tsocket.
       via  42c34cd... s4:kdc: use LIBSAMBA_TSOCKET
       via  d97562b... s4:kdc: the ->process function returns "bool"
       via  bbaec01... libcli/util: add tstream_read_pdu_blob_send/recv
      from  1bc9530... s3-time: fix build warnings after we moved to shared time functions.

http://gitweb.samba.org/?p=samba.git;a=shortlog;h=master


- Log -----------------------------------------------------------------
commit 0588f34467d8e9b56de1beabe776babde52a1a55
Author: Andreas Schneider <asn at redhat.com>
Date:   Thu Jan 7 12:23:33 2010 +0100

    s4-kdc: Migrate tcp connections to tsocket.
    
    Signed-off-by: Stefan Metzmacher <metze at samba.org>

commit 42c34cdafa3323cc6f298a3668eb03becc90aa84
Author: Stefan Metzmacher <metze at samba.org>
Date:   Wed Nov 4 19:27:20 2009 +0100

    s4:kdc: use LIBSAMBA_TSOCKET
    
    metze

commit d97562b382a1e770310f1417992dff417a585e16
Author: Stefan Metzmacher <metze at samba.org>
Date:   Fri Jan 8 11:45:59 2010 +0100

    s4:kdc: the ->process function returns "bool"
    
    metze

commit bbaec01b3720fafada13a7143d698f8cd7c0af37
Author: Stefan Metzmacher <metze at samba.org>
Date:   Thu Nov 5 09:55:12 2009 +0100

    libcli/util: add tstream_read_pdu_blob_send/recv
    
    This will take the some full_request callback function
    as the Samba4 packet code.
    
    metze

-----------------------------------------------------------------------

Summary of changes:
 libcli/util/tstream.c    |  167 ++++++++++++++++++++++++++
 libcli/util/tstream.h    |   79 ++++++++++++
 source4/kdc/config.mk    |    2 +-
 source4/kdc/kdc.c        |  295 +++++++++++++++++++++++++++++++---------------
 source4/libcli/config.mk |    5 +
 5 files changed, 449 insertions(+), 99 deletions(-)
 create mode 100644 libcli/util/tstream.c
 create mode 100644 libcli/util/tstream.h


Changeset truncated at 500 lines:

diff --git a/libcli/util/tstream.c b/libcli/util/tstream.c
new file mode 100644
index 0000000..f6c92f3
--- /dev/null
+++ b/libcli/util/tstream.c
@@ -0,0 +1,167 @@
+/*
+ *  Unix SMB/CIFS implementation.
+ *
+ *  Copyright (C) Stefan Metzmacher 2009
+ *
+ *  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 <http://www.gnu.org/licenses/>.
+ */
+
+#include "includes.h"
+#include <tevent.h>
+#include "system/filesys.h"
+#include "../lib/tsocket/tsocket.h"
+#include "../libcli/util/tstream.h"
+#include "../lib/util/tevent_ntstatus.h"
+
+struct tstream_read_pdu_blob_state {
+	/* this structs are owned by the caller */
+	struct {
+		struct tevent_context *ev;
+		struct tstream_context *stream;
+		NTSTATUS (*full_fn)(void *private_data,
+				    DATA_BLOB blob,
+				    size_t *packet_size);
+		void *full_private;
+	} caller;
+
+	DATA_BLOB pdu_blob;
+	struct iovec tmp_vector;
+};
+
+static void tstream_read_pdu_blob_done(struct tevent_req *subreq);
+
+struct tevent_req *tstream_read_pdu_blob_send(TALLOC_CTX *mem_ctx,
+				struct tevent_context *ev,
+				struct tstream_context *stream,
+				size_t initial_read_size,
+				NTSTATUS (*full_fn)(void *private_data,
+						    DATA_BLOB blob,
+						    size_t *packet_size),
+				void *full_private)
+{
+	struct tevent_req *req;
+	struct tstream_read_pdu_blob_state *state;
+	struct tevent_req *subreq;
+	uint8_t *buf;
+
+	req = tevent_req_create(mem_ctx, &state,
+				struct tstream_read_pdu_blob_state);
+	if (!req) {
+		return NULL;
+	}
+
+	state->caller.ev		= ev;
+	state->caller.stream		= stream;
+	state->caller.full_fn		= full_fn;
+	state->caller.full_private	= full_private;
+
+	if (initial_read_size == 0) {
+		tevent_req_error(req, EINVAL);
+		return tevent_req_post(req, ev);
+	}
+
+	buf = talloc_array(state, uint8_t, initial_read_size);
+	if (tevent_req_nomem(buf, req)) {
+		return tevent_req_post(req, ev);
+	}
+	state->pdu_blob.data = buf;
+	state->pdu_blob.length = initial_read_size;
+
+	state->tmp_vector.iov_base = buf;
+	state->tmp_vector.iov_len = initial_read_size;
+
+	subreq = tstream_readv_send(state, ev, stream, &state->tmp_vector, 1);
+	if (tevent_req_nomem(subreq, req)) {
+		return tevent_req_post(req, ev);
+	}
+	tevent_req_set_callback(subreq, tstream_read_pdu_blob_done, req);
+
+	return req;
+}
+
+static void tstream_read_pdu_blob_done(struct tevent_req *subreq)
+{
+	struct tevent_req *req =
+		tevent_req_callback_data(subreq,
+		struct tevent_req);
+	struct tstream_read_pdu_blob_state *state =
+		tevent_req_data(req,
+		struct tstream_read_pdu_blob_state);
+	ssize_t ret;
+	int sys_errno;
+	size_t pdu_size;
+	NTSTATUS status;
+	uint8_t *buf;
+
+	ret = tstream_readv_recv(subreq, &sys_errno);
+	TALLOC_FREE(subreq);
+	if (ret == -1) {
+		status = map_nt_error_from_unix(sys_errno);
+		tevent_req_nterror(req, status);
+		return;
+	}
+
+	status = state->caller.full_fn(state->caller.full_private,
+				       state->pdu_blob, &pdu_size);
+	if (NT_STATUS_IS_OK(status)) {
+		tevent_req_done(req);
+		return;
+	} else if (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) {
+		/* more to get */
+	} else if (!NT_STATUS_IS_OK(status)) {
+		tevent_req_nterror(req, status);
+		return;
+	}
+
+	buf = talloc_realloc(state, state->pdu_blob.data, uint8_t, pdu_size);
+	if (tevent_req_nomem(buf, req)) {
+		return;
+	}
+	state->pdu_blob.data = buf;
+	state->pdu_blob.length = pdu_size;
+
+	state->tmp_vector.iov_base = buf + state->tmp_vector.iov_len;
+	state->tmp_vector.iov_len = pdu_size - state->tmp_vector.iov_len;
+
+	subreq = tstream_readv_send(state,
+				    state->caller.ev,
+				    state->caller.stream,
+				    &state->tmp_vector,
+				    1);
+	if (tevent_req_nomem(subreq, req)) {
+		return;
+	}
+	tevent_req_set_callback(subreq, tstream_read_pdu_blob_done, req);
+}
+
+NTSTATUS tstream_read_pdu_blob_recv(struct tevent_req *req,
+				    TALLOC_CTX *mem_ctx,
+				    DATA_BLOB *pdu_blob)
+{
+	struct tstream_read_pdu_blob_state *state = tevent_req_data(req,
+					struct tstream_read_pdu_blob_state);
+	NTSTATUS status;
+
+	if (tevent_req_is_nterror(req, &status)) {
+		tevent_req_received(req);
+		return status;
+	}
+
+	*pdu_blob = state->pdu_blob;
+	talloc_steal(mem_ctx, pdu_blob->data);
+
+	tevent_req_received(req);
+	return NT_STATUS_OK;
+}
+
diff --git a/libcli/util/tstream.h b/libcli/util/tstream.h
new file mode 100644
index 0000000..a945287
--- /dev/null
+++ b/libcli/util/tstream.h
@@ -0,0 +1,79 @@
+/*
+ *  Unix SMB/CIFS implementation.
+ *
+ *  Copyright (C) Stefan Metzmacher 2009
+ *
+ *  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 <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef _LIBCLI_UTIL_TSTREAM_H_
+#define _LIBCLI_UTIL_TSTREAM_H_
+
+/**
+ * @brief A helper function to read a full PDU from a stream
+ *
+ * This function is designed for simple PDUs and as compat layer
+ * for the Samba4 packet interface.
+ *
+ * tstream_readv_pdu_send() is a more powerful interface,
+ * which is part of the main (non samba specific) tsocket code.
+ *
+ * @param[in] mem_ctx		The memory context for the result.
+ *
+ * @param[in] ev		The event context the operation should work on.
+ *
+ * @param[in] stream		The stream to read data from.
+ *
+ * @param[in] inital_read_size	The initial byte count that is needed to workout
+ *				the full pdu size.
+ *
+ * @param[in] full_fn		The callback function that will report the size
+ *				of the full pdu.
+ *
+ * @param[in] full_private	The private data for the callback function.
+ *
+ * @return			The async request handle. NULL on fatal error.
+ *
+ * @see tstream_read_pdu_blob_recv()
+ * @see tstream_readv_pdu_send()
+ * @see tstream_readv_pdu_queue_send()
+ *
+ */
+struct tevent_req *tstream_read_pdu_blob_send(TALLOC_CTX *mem_ctx,
+				struct tevent_context *ev,
+				struct tstream_context *stream,
+				size_t inital_read_size,
+				NTSTATUS (*full_fn)(void *private_data,
+						    DATA_BLOB blob,
+						    size_t *packet_size),
+				void *full_private);
+/**
+ * @brief Receive the result of the tstream_read_pdu_blob_send() call.
+ *
+ * @param[in] req	The tevent request from tstream_read_pdu_blob_send().
+ *
+ * @param[in] mem_ctx	The memory context for returned pdu DATA_BLOB.
+ *
+ * @param[in] pdu_blob	The DATA_BLOB with the full pdu.
+ *
+ * @return		The NTSTATUS result, NT_STATUS_OK on success
+ *			and others on failure.
+ *
+ * @see tstream_read_pdu_blob_send()
+ */
+NTSTATUS tstream_read_pdu_blob_recv(struct tevent_req *req,
+				    TALLOC_CTX *mem_ctx,
+				    DATA_BLOB *pdu_blob);
+
+#endif /* _LIBCLI_UTIL_TSTREAM_H_ */
diff --git a/source4/kdc/config.mk b/source4/kdc/config.mk
index a9d0158..3ae5fe5 100644
--- a/source4/kdc/config.mk
+++ b/source4/kdc/config.mk
@@ -7,7 +7,7 @@ INIT_FUNCTION = server_service_kdc_init
 SUBSYSTEM = service
 PRIVATE_DEPENDENCIES = \
 		HEIMDAL_KDC HDB_SAMBA4 PAC_GLUE LIBSAMBA-HOSTCONFIG \
-		LIBTSOCKET
+		LIBTSOCKET LIBSAMBA_TSOCKET
 # End SUBSYSTEM KDC
 #######################
 
diff --git a/source4/kdc/kdc.c b/source4/kdc/kdc.c
index 93f1c7d..19042dc 100644
--- a/source4/kdc/kdc.c
+++ b/source4/kdc/kdc.c
@@ -29,6 +29,7 @@
 #include "lib/events/events.h"
 #include "lib/socket/socket.h"
 #include "lib/tsocket/tsocket.h"
+#include "libcli/util/tstream.h"
 #include "system/network.h"
 #include "../lib/util/dlinklist.h"
 #include "lib/messaging/irpc.h"
@@ -73,7 +74,9 @@ struct kdc_tcp_connection {
 	/* the kdc_server the connection belongs to */
 	struct kdc_socket *kdc_socket;
 
-	struct packet_context *packet;
+	struct tstream_context *tstream;
+
+	struct tevent_queue *send_queue;
 };
 
 static void kdc_tcp_terminate_connection(struct kdc_tcp_connection *kdcconn, const char *reason)
@@ -81,83 +84,20 @@ static void kdc_tcp_terminate_connection(struct kdc_tcp_connection *kdcconn, con
 	stream_terminate_connection(kdcconn->conn, reason);
 }
 
-/*
-  receive a full packet on a KDC connection
-*/
-static NTSTATUS kdc_tcp_recv(void *private_data, DATA_BLOB blob)
-{
-	struct kdc_tcp_connection *kdcconn = talloc_get_type(private_data,
-							     struct kdc_tcp_connection);
-	NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
-	TALLOC_CTX *tmp_ctx = talloc_new(kdcconn);
-	int ret;
-	DATA_BLOB input, reply;
-	talloc_steal(tmp_ctx, blob.data);
-
-	/* Call krb5 */
-	input = data_blob_const(blob.data + 4, blob.length - 4);
-
-	ret = kdcconn->kdc_socket->process(kdcconn->kdc_socket->kdc,
-					   tmp_ctx,
-					   &input,
-					   &reply,
-					   kdcconn->conn->remote_address,
-					   kdcconn->conn->local_address,
-					   0 /* Not datagram */);
-	if (!ret) {
-		talloc_free(tmp_ctx);
-		return NT_STATUS_INTERNAL_ERROR;
-	}
-
-	/* and now encode the reply */
-	blob = data_blob_talloc(kdcconn, NULL, reply.length + 4);
-	if (!blob.data) {
-		talloc_free(tmp_ctx);
-		return NT_STATUS_NO_MEMORY;
-	}
-
-	RSIVAL(blob.data, 0, reply.length);
-	memcpy(blob.data + 4, reply.data, reply.length);
-
-	status = packet_send(kdcconn->packet, blob);
-	if (!NT_STATUS_IS_OK(status)) {
-		talloc_free(tmp_ctx);
-		return status;
-	}
-
-	/* the call isn't needed any more */
-	talloc_free(tmp_ctx);
-	return NT_STATUS_OK;
-}
-
-/*
-  receive some data on a KDC connection
-*/
-static void kdc_tcp_recv_handler(struct stream_connection *conn, uint16_t flags)
+static void kdc_tcp_recv(struct stream_connection *conn, uint16_t flags)
 {
 	struct kdc_tcp_connection *kdcconn = talloc_get_type(conn->private_data,
 							     struct kdc_tcp_connection);
-	packet_recv(kdcconn->packet);
-}
-
-/*
-  called on a tcp recv error
-*/
-static void kdc_tcp_recv_error(void *private_data, NTSTATUS status)
-{
-	struct kdc_tcp_connection *kdcconn = talloc_get_type(private_data,
-					     struct kdc_tcp_connection);
-	kdc_tcp_terminate_connection(kdcconn, nt_errstr(status));
+	/* this should never be triggered! */
+	kdc_tcp_terminate_connection(kdcconn, "kdc_tcp_recv: called");
 }
 
-/*
-  called when we can write to a connection
-*/
 static void kdc_tcp_send(struct stream_connection *conn, uint16_t flags)
 {
 	struct kdc_tcp_connection *kdcconn = talloc_get_type(conn->private_data,
 							     struct kdc_tcp_connection);
-	packet_queue_run(kdcconn->packet);
+	/* this should never be triggered! */
+	kdc_tcp_terminate_connection(kdcconn, "kdc_tcp_send: called");
 }
 
 /**
@@ -214,42 +154,201 @@ static bool kdc_process(struct kdc_server *kdc,
 	return true;
 }
 
+struct kdc_tcp_call {
+	struct kdc_tcp_connection *kdc_conn;
+	DATA_BLOB in;
+	DATA_BLOB out;
+	uint8_t out_hdr[4];
+	struct iovec out_iov[2];
+};
+
+static void kdc_tcp_call_writev_done(struct tevent_req *subreq);
+
+static void kdc_tcp_call_loop(struct tevent_req *subreq)
+{
+	struct kdc_tcp_connection *kdc_conn = tevent_req_callback_data(subreq,
+				      struct kdc_tcp_connection);
+	struct kdc_tcp_call *call;
+	NTSTATUS status;
+	bool ok;
+
+	call = talloc(kdc_conn, struct kdc_tcp_call);
+	if (call == NULL) {
+		kdc_tcp_terminate_connection(kdc_conn, "kdc_tcp_call_loop: "
+				"no memory for kdc_tcp_call");
+		return;
+	}
+	call->kdc_conn = kdc_conn;
+
+	status = tstream_read_pdu_blob_recv(subreq,
+					    call,
+					    &call->in);
+	TALLOC_FREE(subreq);
+	if (!NT_STATUS_IS_OK(status)) {
+		const char *reason;
+
+		reason = talloc_asprintf(call, "kdc_tcp_call_loop: "
+					 "tstream_read_pdu_blob_recv() - %s",
+					 nt_errstr(status));
+		if (!reason) {
+			reason = nt_errstr(status);
+		}
+
+		kdc_tcp_terminate_connection(kdc_conn, reason);
+		return;
+	}
+
+	DEBUG(10,("Received krb5 TCP packet of length %lu from %s\n",
+		 (long) call->in.length,
+		 tsocket_address_string(kdc_conn->conn->remote_address, call)));
+
+	/* skip length header */
+	call->in.data +=4;
+	call->in.length -= 4;
+
+	/* Call krb5 */
+	ok = kdc_conn->kdc_socket->process(kdc_conn->kdc_socket->kdc,
+					   call,
+					   &call->in,
+					   &call->out,
+					   kdc_conn->conn->remote_address,
+					   kdc_conn->conn->local_address,
+					   0 /* Stream */);
+	if (!ok) {
+		kdc_tcp_terminate_connection(kdc_conn,
+				"kdc_tcp_call_loop: process function failed");
+		return;
+	}
+
+	/* First add the length of the out buffer */
+	RSIVAL(call->out_hdr, 0, call->out.length);
+	call->out_iov[0].iov_base = call->out_hdr;
+	call->out_iov[0].iov_len = 4;
+
+	call->out_iov[1].iov_base = call->out.data;
+	call->out_iov[1].iov_len = call->out.length;
+
+	subreq = tstream_writev_queue_send(call,
+					   kdc_conn->conn->event.ctx,
+					   kdc_conn->tstream,
+					   kdc_conn->send_queue,
+					   call->out_iov, 2);
+	if (subreq == NULL) {
+		kdc_tcp_terminate_connection(kdc_conn, "kdc_tcp_call_loop: "
+				"no memory for tstream_writev_queue_send");
+		return;
+	}
+	tevent_req_set_callback(subreq, kdc_tcp_call_writev_done, call);
+
+	/*
+	 * The krb5 tcp pdu's has the length as 4 byte (initial_read_size),
+	 * packet_full_request_u32 provides the pdu length then.
+	 */
+	subreq = tstream_read_pdu_blob_send(kdc_conn,
+					    kdc_conn->conn->event.ctx,
+					    kdc_conn->tstream,
+					    4, /* initial_read_size */
+					    packet_full_request_u32,
+					    kdc_conn);
+	if (subreq == NULL) {
+		kdc_tcp_terminate_connection(kdc_conn, "kdc_tcp_call_loop: "
+				"no memory for tstream_read_pdu_blob_send");
+		return;
+	}
+	tevent_req_set_callback(subreq, kdc_tcp_call_loop, kdc_conn);
+}
+
+static void kdc_tcp_call_writev_done(struct tevent_req *subreq)
+{
+	struct kdc_tcp_call *call = tevent_req_callback_data(subreq,
+			struct kdc_tcp_call);
+	int sys_errno;
+	int rc;
+
+	rc = tstream_writev_queue_recv(subreq, &sys_errno);
+	TALLOC_FREE(subreq);


-- 
Samba Shared Repository


More information about the samba-cvs mailing list