dovecot-2.2: auth: Code cleanup: Avoid using auth_stream_reply a...

dovecot at dovecot.org dovecot at dovecot.org
Wed Jan 30 19:08:08 EET 2013


details:   http://hg.dovecot.org/dovecot-2.2/rev/526aa986d534
changeset: 15682:526aa986d534
user:      Timo Sirainen <tss at iki.fi>
date:      Wed Jan 30 19:08:00 2013 +0200
description:
auth: Code cleanup: Avoid using auth_stream_reply as temporary strings.

diffstat:

 src/auth/auth-client-connection.c |    7 +-
 src/auth/auth-master-connection.c |   23 ++---
 src/auth/auth-master-connection.h |    3 +-
 src/auth/auth-request-handler.c   |  151 ++++++++++++++++---------------------
 src/auth/auth-request-handler.h   |    4 +-
 src/auth/auth-request.c           |   19 +---
 src/auth/auth-request.h           |    1 +
 7 files changed, 86 insertions(+), 122 deletions(-)

diffs (truncated from 540 to 300 lines):

diff -r 3fac9306be3e -r 526aa986d534 src/auth/auth-client-connection.c
--- a/src/auth/auth-client-connection.c	Wed Jan 30 18:40:49 2013 +0200
+++ b/src/auth/auth-client-connection.c	Wed Jan 30 19:08:00 2013 +0200
@@ -71,16 +71,15 @@
 	}
 }
 
-static void auth_callback(struct auth_stream_reply *reply,
+static void auth_callback(const char *reply,
 			  struct auth_client_connection *conn)
 {
 	if (reply == NULL) {
 		/* handler destroyed */
 		auth_client_connection_unref(&conn);
-		return;
+	} else {
+		auth_client_send(conn, reply);
 	}
-
-	auth_client_send(conn, auth_stream_reply_export(reply));
 }
 
 static bool
diff -r 3fac9306be3e -r 526aa986d534 src/auth/auth-master-connection.c
--- a/src/auth/auth-master-connection.c	Wed Jan 30 18:40:49 2013 +0200
+++ b/src/auth/auth-master-connection.c	Wed Jan 30 19:08:00 2013 +0200
@@ -69,22 +69,18 @@
 	return t_strarray_join((void *)args, "\t");
 }
 
-void auth_master_request_callback(struct auth_stream_reply *reply,
-				  void *context)
+void auth_master_request_callback(const char *reply, void *context)
 {
 	struct auth_master_connection *conn = context;
 	struct const_iovec iov[2];
-	const char *reply_str;
-
-	reply_str = auth_stream_reply_export(reply);
 
 	if (conn->auth->set->debug) {
 		i_debug("master userdb out: %s",
-			auth_master_reply_hide_passwords(conn, reply_str));
+			auth_master_reply_hide_passwords(conn, reply));
 	}
 
-	iov[0].iov_base = reply_str;
-	iov[0].iov_len = strlen(reply_str);
+	iov[0].iov_base = reply;
+	iov[0].iov_len = strlen(reply);
 	iov[1].iov_base = "\n";
 	iov[1].iov_len = 1;
 
@@ -250,7 +246,6 @@
 	      struct auth_request *auth_request)
 {
 	struct auth_master_connection *conn = auth_request->master;
-	struct auth_stream_reply *reply = auth_request->userdb_reply;
 	string_t *str;
 	const char *value;
 
@@ -267,7 +262,8 @@
 	case USERDB_RESULT_INTERNAL_FAILURE:
 		str_printfa(str, "FAIL\t%u", auth_request->id);
 		if (auth_request->userdb_lookup_failed) {
-			value = auth_stream_reply_find(reply, "reason");
+			value = auth_stream_reply_find(auth_request->userdb_reply,
+						       "reason");
 			if (value != NULL)
 				str_printfa(str, "\treason=%s", value);
 		}
@@ -279,7 +275,7 @@
 		str_printfa(str, "USER\t%u\t", auth_request->id);
 		str_append_tabescaped(str, auth_request->user);
 		str_append_c(str, '\t');
-		str_append(str, auth_stream_reply_export(reply));
+		auth_stream_reply_append(auth_request->userdb_reply, str);
 		break;
 	}
 
@@ -320,7 +316,6 @@
 				 enum passdb_result result)
 {
 	struct auth_master_connection *conn = auth_request->master;
-	struct auth_stream_reply *reply = auth_request->extra_fields;
 	string_t *str;
 
 	str = t_str_new(128);
@@ -328,9 +323,9 @@
 	case PASSDB_RESULT_OK:
 		str_printfa(str, "PASS\t%u\tuser=", auth_request->id);
 		str_append_tabescaped(str, auth_request->user);
-		if (reply != NULL) {
+		if (!auth_stream_is_empty(auth_request->extra_fields)) {
 			str_append_c(str, '\t');
-			str_append(str, auth_stream_reply_export(reply));
+			auth_stream_reply_append(auth_request->extra_fields, str);
 		}
 		break;
 	case PASSDB_RESULT_USER_UNKNOWN:
diff -r 3fac9306be3e -r 526aa986d534 src/auth/auth-master-connection.h
--- a/src/auth/auth-master-connection.h	Wed Jan 30 18:40:49 2013 +0200
+++ b/src/auth/auth-master-connection.h	Wed Jan 30 19:08:00 2013 +0200
@@ -34,8 +34,7 @@
 void auth_master_connection_ref(struct auth_master_connection *conn);
 void auth_master_connection_unref(struct auth_master_connection **conn);
 
-void auth_master_request_callback(struct auth_stream_reply *reply,
-				  void *context);
+void auth_master_request_callback(const char *reply, void *context);
 
 void auth_master_connections_destroy_all(void);
 
diff -r 3fac9306be3e -r 526aa986d534 src/auth/auth-request-handler.c
--- a/src/auth/auth-request-handler.c	Wed Jan 30 18:40:49 2013 +0200
+++ b/src/auth/auth-request-handler.c	Wed Jan 30 19:08:00 2013 +0200
@@ -8,6 +8,7 @@
 #include "hash.h"
 #include "net.h"
 #include "str.h"
+#include "strescape.h"
 #include "str-sanitize.h"
 #include "master-interface.h"
 #include "auth-penalty.h"
@@ -153,38 +154,44 @@
 	auth_request_unref(&request);
 }
 
-static void get_client_extra_fields(struct auth_request *request,
-				    struct auth_stream_reply *reply)
+static void
+auth_str_add_keyvalue(string_t *dest, const char *key, const char *value)
 {
-	const char *extra_fields;
+	str_append_c(dest, '\t');
+	str_append(dest, key);
+	str_append_c(dest, '=');
+	str_append_tabescaped(dest, value);
+}
 
+static void
+auth_str_append_extra_fields(struct auth_request *request, string_t *dest)
+{
 	if (auth_stream_is_empty(request->extra_fields))
 		return;
 
-	extra_fields = auth_stream_reply_export(request->extra_fields);
-	auth_stream_reply_import(reply, extra_fields);
+	str_append_c(dest, '\t');
+	auth_stream_reply_append(request->extra_fields, dest);
 
 	if (request->proxy && !request->auth_only) {
 		/* we're proxying */
-		if (!auth_stream_reply_exists(reply, "pass") &&
+		if (!auth_stream_reply_exists(request->extra_fields, "pass") &&
 		    request->mech_password != NULL) {
 			/* send back the password that was sent by user
 			   (not the password in passdb). */
-			auth_stream_reply_add(reply, "pass",
+			auth_str_add_keyvalue(dest, "pass",
 					      request->mech_password);
 		}
 		if (request->master_user != NULL &&
-		    auth_stream_reply_find(reply, "master") == NULL) {
+		    !auth_stream_reply_exists(request->extra_fields, "master")) {
 			/* the master username needs to be forwarded */
-			auth_stream_reply_add(reply, "master",
+			auth_str_add_keyvalue(dest, "master",
 					      request->master_user);
 		}
 	}
 }
 
 static void
-auth_request_handle_failure(struct auth_request *request,
-			    struct auth_stream_reply *reply)
+auth_request_handle_failure(struct auth_request *request, const char *reply)
 {
         struct auth_request_handler *handler = request->handler;
 
@@ -228,52 +235,47 @@
 auth_request_handler_reply_success_finish(struct auth_request *request)
 {
         struct auth_request_handler *handler = request->handler;
-	struct auth_stream_reply *reply;
-
-	reply = auth_stream_reply_init(pool_datastack_create());
+	string_t *str = t_str_new(128);
 
 	if (request->last_penalty != 0 && auth_penalty != NULL) {
 		/* reset penalty */
 		auth_penalty_update(auth_penalty, request, 0);
 	}
 
-	auth_stream_reply_add(reply, "OK", NULL);
-	auth_stream_reply_add(reply, NULL, dec2str(request->id));
-	auth_stream_reply_add(reply, "user", request->user);
-	get_client_extra_fields(request, reply);
+	str_printfa(str, "OK\t%u\tuser=", request->id);
+	str_append_tabescaped(str, request->user);
+	auth_str_append_extra_fields(request, str);
 	if (request->no_login || handler->master_callback == NULL) {
 		/* this request doesn't have to wait for master
 		   process to pick it up. delete it */
 		auth_request_handler_remove(handler, request);
 	}
-	handler->callback(reply, handler->context);
+	handler->callback(str_c(str), handler->context);
 }
 
 static void
 auth_request_handler_reply_failure_finish(struct auth_request *request)
 {
-	struct auth_stream_reply *reply;
+	string_t *str = t_str_new(128);
 
-	reply = auth_stream_reply_init(pool_datastack_create());
-	auth_stream_reply_add(reply, "FAIL", NULL);
-	auth_stream_reply_add(reply, NULL, dec2str(request->id));
+	str_printfa(str, "FAIL\t%u", request->id);
 	if (request->user != NULL)
-		auth_stream_reply_add(reply, "user", request->user);
+		auth_str_add_keyvalue(str, "user", request->user);
 	else if (request->original_username != NULL) {
-		auth_stream_reply_add(reply, "user",
+		auth_str_add_keyvalue(str, "user", 
 				      request->original_username);
 	}
 
 	if (request->internal_failure)
-		auth_stream_reply_add(reply, "temp", NULL);
+		str_append(str, "\ttemp");
 	else if (request->master_user != NULL) {
 		/* authentication succeeded, but we can't log in
 		   as the wanted user */
-		auth_stream_reply_add(reply, "authz", NULL);
+		str_append(str, "\tauthz");
 	}
 	if (request->no_failure_delay)
-		auth_stream_reply_add(reply, "nodelay", NULL);
-	get_client_extra_fields(request, reply);
+		str_append(str, "\tnodelay");
+	auth_str_append_extra_fields(request, str);
 
 	switch (request->passdb_result) {
 	case PASSDB_RESULT_INTERNAL_FAILURE:
@@ -283,14 +285,14 @@
 	case PASSDB_RESULT_OK:
 		break;
 	case PASSDB_RESULT_USER_DISABLED:
-		auth_stream_reply_add(reply, "user_disabled", NULL);
+		str_append(str, "\tuser_disabled");
 		break;
 	case PASSDB_RESULT_PASS_EXPIRED:
-		auth_stream_reply_add(reply, "pass_expired", NULL);
+		str_append(str, "\tpass_expired");
 		break;
 	}
 
-	auth_request_handle_failure(request, reply);
+	auth_request_handle_failure(request, str_c(str));
 }
 
 static void
@@ -310,7 +312,6 @@
 				const void *auth_reply, size_t reply_size)
 {
         struct auth_request_handler *handler = request->handler;
-	struct auth_stream_reply *reply;
 	string_t *str;
 	int ret;
 
@@ -326,16 +327,12 @@
 
 	switch (result) {
 	case AUTH_CLIENT_RESULT_CONTINUE:
-		reply = auth_stream_reply_init(pool_datastack_create());
-		auth_stream_reply_add(reply, "CONT", NULL);
-		auth_stream_reply_add(reply, NULL, dec2str(request->id));
-
-		str = t_str_new(MAX_BASE64_ENCODED_SIZE(reply_size));
+		str = t_str_new(16 + MAX_BASE64_ENCODED_SIZE(reply_size));
+		str_printfa(str, "CONT\t%u\t", request->id);
 		base64_encode(auth_reply, reply_size, str);
-		auth_stream_reply_add(reply, NULL, str_c(str));
 
 		request->accept_input = TRUE;
-		handler->callback(reply, handler->context);
+		handler->callback(str_c(str), handler->context);
 		break;
 	case AUTH_CLIENT_RESULT_SUCCESS:
 		if (reply_size > 0) {
@@ -373,16 +370,14 @@
 					   struct auth_request *request,
 					   const char *reason)
 {
-	struct auth_stream_reply *reply;
+	string_t *str = t_str_new(128);
 
 	auth_request_log_info(request, request->mech->mech_name, "%s", reason);


More information about the dovecot-cvs mailing list