From b798e72e5edda97ebb31e60634c38c7e9db70692 Mon Sep 17 00:00:00 2001
From: "Heiko Schlittermann (HS12-RIPE)" <hs@schlittermann.de>
Date: Thu, 25 Feb 2021 17:15:11 +0100
Subject: [PATCH] Add preventive endpwent() prior calling setpwent()

---
 src/responder/nss/nss_cmd.c | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)

diff --git a/src/responder/nss/nss_cmd.c b/src/responder/nss/nss_cmd.c
index 844776c5f..27307a66c 100644
--- a/src/responder/nss/nss_cmd.c
+++ b/src/responder/nss/nss_cmd.c
@@ -996,10 +996,19 @@ static errno_t nss_cmd_getpwuid_ex(struct cli_ctx *cli_ctx)
                         SSS_MC_PASSWD, nss_protocol_fill_pwent);
 }
 
+static errno_t nss_cmd_endpwent(struct cli_ctx *cli_ctx)
+{
+    struct nss_state_ctx *state_ctx;
+
+    state_ctx = talloc_get_type(cli_ctx->state_ctx, struct nss_state_ctx);
+
+    return nss_endent(cli_ctx, &state_ctx->pwent);
+}
+
 static errno_t nss_cmd_setpwent(struct cli_ctx *cli_ctx)
 {
     struct nss_ctx *nss_ctx;
-
+    nss_cmd_endpwent(cli_ctx);
     nss_ctx = talloc_get_type(cli_ctx->rctx->pvt_ctx, struct nss_ctx);
 
     return nss_setent(cli_ctx, CACHE_REQ_ENUM_USERS, nss_ctx->pwent);
@@ -1018,15 +1027,6 @@ static errno_t nss_cmd_getpwent(struct cli_ctx *cli_ctx)
                       nss_ctx->pwent);
 }
 
-static errno_t nss_cmd_endpwent(struct cli_ctx *cli_ctx)
-{
-    struct nss_state_ctx *state_ctx;
-
-    state_ctx = talloc_get_type(cli_ctx->state_ctx, struct nss_state_ctx);
-
-    return nss_endent(cli_ctx, &state_ctx->pwent);
-}
-
 static errno_t nss_cmd_getgrnam(struct cli_ctx *cli_ctx)
 {
     return nss_getby_name(cli_ctx, false, CACHE_REQ_GROUP_BY_NAME, NULL,
-- 
2.20.1

