Description: upstream: ssh session: clean closing
 Close sessions in a civilized manner when no dotdir
 is present (Close: #802212); meant to be submitted
 to the upstream maintainer.
Origin: debian
Forwarded: dormant-upstream
Author: Jerome Benoit <calculus@rezozer.net>
Last-Update: 2022-01-09

--- a/pam_ssh.c
+++ b/pam_ssh.c
@@ -1158,6 +1158,7 @@
 	const struct passwd *pwent;	/* user's passwd entry */
 	struct stat sb;			/* to check st_nlink */
 	const char *user;               /* username */
+	char *dotdir;		        /* .ssh dir name */
 
 	pam_ssh_log(LOG_DEBUG, "close session");
 
@@ -1177,6 +1178,21 @@
 		return retval;
 	}
 
+	/* handle the per-user configuration directory and check its existence */
+
+	if (asprintf(&dotdir, "%s/%s", pwent->pw_dir, SSH_DIR) == -1) {
+		pam_ssh_log(LOG_CRIT, "out of memory");
+		openpam_restore_cred(pamh);
+		return PAM_SERVICE_ERR;
+	}
+	if ((access(dotdir,F_OK)) == -1) {
+		pam_ssh_log(LOG_DEBUG, "inexistent configuration directory");
+		free(dotdir);
+		openpam_restore_cred(pamh);
+		return PAM_SUCCESS;
+	}
+	free(dotdir);
+
 	if (pam_get_data(pamh, "ssh_agent_env_session",
 	    (const void **)(void *)&env_file) == PAM_SUCCESS && env_file)
 		unlink(env_file);
