changeset 22790:b0c2b646547f

auth-worker: Add auth_worker_auth_request_new Replaces worker_auth_request_new, moves in check for username and service. Simplifies code.
author Aki Tuomi <aki.tuomi@dovecot.fi>
date Mon, 08 Jan 2018 14:52:10 +0200
parents 48a918c22fe5
children a24438dfacf3
files src/auth/auth-worker-client.c src/auth/auth-worker-client.h
diffstat 2 files changed, 22 insertions(+), 27 deletions(-) [+]
line wrap: on
line diff
--- a/src/auth/auth-worker-client.c	Tue Jan 02 12:33:50 2018 +0200
+++ b/src/auth/auth-worker-client.c	Mon Jan 08 14:52:10 2018 +0200
@@ -76,9 +76,8 @@
 	}
 }
 
-static struct auth_request *
-worker_auth_request_new(struct auth_worker_client *client, unsigned int id,
-			const char *const *args)
+bool auth_worker_auth_request_new(struct auth_worker_client *client, unsigned int id,
+				  const char *const *args, struct auth_request **request_r)
 {
 	struct auth_request *auth_request;
 	const char *key, *value;
@@ -98,6 +97,11 @@
 			(void)auth_request_import(auth_request, key, value);
 		}
 	}
+	if (auth_request->user == NULL || auth_request->service == NULL) {
+		auth_request_unref(&auth_request);
+		return FALSE;
+	}
+
 	/* reset changed-fields, so we'll export only the ones that were
 	   changed by this lookup. */
 	auth_fields_snapshot(auth_request->extra_fields);
@@ -105,7 +109,9 @@
 		auth_fields_snapshot(auth_request->userdb_reply);
 
 	auth_request_init(auth_request);
-	return auth_request;
+	*request_r = auth_request;
+
+	return TRUE;
 }
 
 static void auth_worker_send_reply(struct auth_worker_client *client,
@@ -202,16 +208,13 @@
 	}
 	password = args[1];
 
-	auth_request = worker_auth_request_new(client, id, args + 2);
+	if (!auth_worker_auth_request_new(client, id, args + 2, &auth_request)) {
+		i_error("BUG: Auth worker server sent us invalid PASSV");
+		return FALSE;
+	}
 	auth_request->mech_password =
 		p_strdup(auth_request->pool, password);
 
-	if (auth_request->user == NULL || auth_request->service == NULL) {
-		i_error("BUG: PASSV had missing parameters");
-		auth_request_unref(&auth_request);
-		return FALSE;
-	}
-
 	passdb = auth_request->passdb;
 	while (passdb != NULL && passdb->passdb->id != passdb_id)
 		passdb = passdb->next;
@@ -291,14 +294,11 @@
 	}
 	scheme = args[1];
 
-	auth_request = worker_auth_request_new(client, id, args + 2);
-	auth_request->credentials_scheme = p_strdup(auth_request->pool, scheme);
-
-	if (auth_request->user == NULL || auth_request->service == NULL) {
+	if (!auth_worker_auth_request_new(client, id, args + 2, &auth_request)) {
 		i_error("BUG: PASSL had missing parameters");
-		auth_request_unref(&auth_request);
 		return FALSE;
 	}
+	auth_request->credentials_scheme = p_strdup(auth_request->pool, scheme);
 
 	while (auth_request->passdb->passdb->id != passdb_id) {
 		auth_request->passdb = auth_request->passdb->next;
@@ -352,10 +352,8 @@
 	}
 	creds = args[1];
 
-	auth_request = worker_auth_request_new(client, id, args + 2);
-	if (auth_request->user == NULL || auth_request->service == NULL) {
+	if (!auth_worker_auth_request_new(client, id, args + 2, &auth_request)) {
 		i_error("BUG: SETCRED had missing parameters");
-		auth_request_unref(&auth_request);
 		return FALSE;
 	}
 
@@ -437,10 +435,8 @@
 		return FALSE;
 	}
 
-	auth_request = worker_auth_request_new(client, id, args + 1);
-	if (auth_request->user == NULL || auth_request->service == NULL) {
+	if (!auth_worker_auth_request_new(client, id, args + 1, &auth_request)) {
 		i_error("BUG: USER had missing parameters");
-		auth_request_unref(&auth_request);
 		return FALSE;
 	}
 
@@ -593,15 +589,12 @@
 
 	ctx = i_new(struct auth_worker_list_context, 1);
 	ctx->client = client;
-	ctx->auth_request = worker_auth_request_new(client, id, args + 1);
-	ctx->auth_request->userdb = userdb;
-	if (ctx->auth_request->user == NULL ||
-	    ctx->auth_request->service == NULL) {
+	if (!auth_worker_auth_request_new(client, id, args + 1, &ctx->auth_request)) {
 		i_error("BUG: LIST had missing parameters");
-		auth_request_unref(&ctx->auth_request);
 		i_free(ctx);
 		return FALSE;
 	}
+	ctx->auth_request->userdb = userdb;
 
 	io_remove(&ctx->client->io);
 	if (ctx->client->to_idle != NULL)
--- a/src/auth/auth-worker-client.h	Tue Jan 02 12:33:50 2018 +0200
+++ b/src/auth/auth-worker-client.h	Mon Jan 08 14:52:10 2018 +0200
@@ -8,6 +8,8 @@
 extern struct auth_worker_client *auth_worker_client;
 
 struct auth_worker_client *auth_worker_client_create(struct auth *auth, int fd);
+bool auth_worker_auth_request_new(struct auth_worker_client *client, unsigned int id,
+				  const char *const *args, struct auth_request **request_r);
 void auth_worker_client_destroy(struct auth_worker_client **client);
 void auth_worker_client_unref(struct auth_worker_client **client);