Description: handle session with no controlling tty
 Let the PAM session handle situations where there is no controlling tty
 by using the PID to construct the sessoin file name.
Origin: debian
Author: Jens Peter Secher <jps@debian.org>
Author: Ansgar Burchardt
Bug-Debian: http://bugs.debian.org/541449
Last-Update: 2013-07-05

--- a/pam_ssh.c
+++ b/pam_ssh.c
@@ -984,24 +984,28 @@
 		return retval;
 	}
 
-	/* tty_raw could be NULL in which case we shouldn't bother
-     with the per-session file */
-	if (!tty_raw) {
-		pam_ssh_log(LOG_DEBUG, "session has no tty");
-		free(dotdir);
-		return PAM_SUCCESS;
-	}
-
-	/* set tty_nodir to the tty with / replaced by _ */
+ 	/* if there is no controlling tty, then use the process id */
 
-	if (!(tty_nodir = strdup(tty_raw))) {
-		pam_ssh_log(LOG_CRIT, "out of memory");
-		free(dotdir);
-		openpam_restore_cred(pamh);
-		return PAM_SERVICE_ERR;
+	if (tty_raw == NULL) {
+		pam_ssh_log(LOG_DEBUG, "no controlling tty");
+		if (asprintf(&tty_nodir, "pid%ld", (long) getpid()) == -1) {
+			pam_ssh_log(LOG_CRIT, "out of memory");
+			free(dotdir);
+			openpam_restore_cred(pamh);
+			return PAM_SERVICE_ERR;
+		}
 	}
-	for (cp = tty_nodir; (cp = strchr(cp, '/')); )
-		*cp = '_';
+	/* else set tty_nodir to the tty with / replaced by _ */
+	else {
+		if (!(tty_nodir = strdup(tty_raw))) {
+			pam_ssh_log(LOG_CRIT, "out of memory");
+			free(dotdir);
+			openpam_restore_cred(pamh);
+			return PAM_SERVICE_ERR;
+		}
+		for (cp = tty_nodir; (cp = strchr(cp, '/')); )
+			*cp = '_';
+ 	}
 
 	if (asprintf(&per_session, "%s/agent-%s-%s", dotdir, hname, tty_nodir) == -1) {
 		pam_ssh_log(LOG_CRIT, "out of memory");
