Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 14 Jul 2003 05:17:48 -0700 (PDT)
From:      Dag-Erling Smorgrav <des@FreeBSD.org>
To:        Perforce Change Reviews <perforce@freebsd.org>
Subject:   PERFORCE change 34469 for review
Message-ID:  <200307141217.h6ECHmnu058044@repoman.freebsd.org>

next in thread | raw e-mail | index | archive | help
http://perforce.freebsd.org/chv.cgi?CH=34469

Change 34469 by des@des.at.des.thinksec.com on 2003/07/14 05:17:47

	Use read(2) on fileno(stdin) rather than fgets(3).  This make timeout
	handling considerably simpler, eliminating the need for setjmp(3) and
	evil global variables.
	
	Portions submitted by:	Dmitry V. Levin <ldv@altlinux.org>

Affected files ...

.. //depot/projects/openpam/configure.in#3 edit
.. //depot/projects/openpam/include/security/openpam.h#25 edit
.. //depot/projects/openpam/lib/openpam_ttyconv.c#22 edit

Differences ...

==== //depot/projects/openpam/configure.in#3 (text+ko) ====

@@ -1,4 +1,4 @@
-dnl $P4: //depot/projects/openpam/configure.in#2 $
+dnl $P4: //depot/projects/openpam/configure.in#3 $
 
 AC_PREREQ(2.53)
 AC_INIT([OpenPAM],[YYYYMMDD],[des@freebsd.org])
@@ -6,6 +6,7 @@
 AM_CONFIG_HEADER([config.h])
 
 AC_CANONICAL_SYSTEM
+AC_C_VOLATILE
 AC_DISABLE_STATIC
 AC_PROG_LIBTOOL
 AM_INIT_AUTOMAKE(AC_PACKAGE_NAME, AC_PACKAGE_VERSION)
@@ -63,9 +64,11 @@
 AM_CONDITIONAL(WITH_PAM_SU, test "x$with_pam_su" = "xyes")
 AM_CONDITIONAL(WITH_PAM_UNIX, test "x$with_pam_unix" = "xyes")
 
+AC_PROG_INSTALL
+
 AC_CHECK_HEADERS(crypt.h)
 
-AC_PROG_INSTALL
+AC_CHECK_FUNCS(fpurge)
 
 DL_LIBS=
 AC_CHECK_LIB(dl, dlopen, DL_LIBS=-ldl)

==== //depot/projects/openpam/include/security/openpam.h#25 (text+ko) ====

@@ -31,7 +31,7 @@
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  *
- * $P4: //depot/projects/openpam/include/security/openpam.h#24 $
+ * $P4: //depot/projects/openpam/include/security/openpam.h#25 $
  */
 
 #ifndef _SECURITY_OPENPAM_H_INCLUDED
@@ -178,6 +178,8 @@
 	struct pam_response **_resp,
 	void *_data);
 
+extern int openpam_ttyconv_timeout;
+
 /*
  * Null conversation function
  */

==== //depot/projects/openpam/lib/openpam_ttyconv.c#22 (text+ko) ====

@@ -31,12 +31,13 @@
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  *
- * $P4: //depot/projects/openpam/lib/openpam_ttyconv.c#21 $
+ * $P4: //depot/projects/openpam/lib/openpam_ttyconv.c#22 $
  */
 
 #include <sys/types.h>
 
 #include <ctype.h>
+#include <errno.h>
 #include <setjmp.h>
 #include <signal.h>
 #include <stdio.h>
@@ -50,14 +51,12 @@
 #include "openpam_impl.h"
 
 int openpam_ttyconv_timeout = 0;
-static jmp_buf jmpenv;
-static int timed_out;
 
 static void
 timeout(int sig)
 {
-	timed_out = 1;
-	longjmp(jmpenv, sig);
+
+	(void)sig;
 }
 
 static char *
@@ -67,8 +66,10 @@
 	struct sigaction action, saved_action;
 	sigset_t saved_sigset, sigset;
 	unsigned int saved_alarm;
+	int eof, error, fd, timed_out;
 	size_t len;
 	char *retval;
+	char ch;
 
 	sigemptyset(&sigset);
 	sigaddset(&sigset, SIGINT);
@@ -79,18 +80,40 @@
 	sigemptyset(&action.sa_mask);
 	sigaction(SIGALRM, &action, &saved_action);
 	fputs(msg, stdout);
+	fflush(stdout);
+#ifdef HAVE_FPURGE
+	fpurge(stdin);
+#endif
+	fd = fileno(stdin);
 	buf[0] = '\0';
 	timed_out = 0;
+	eof = error = timed_out = 0;
 	saved_alarm = alarm(openpam_ttyconv_timeout);
-	if (setjmp(jmpenv) == 0)
-		fgets(buf, sizeof buf, stdin);
-	else
-		fputs(" timeout!\n", stderr);
+	ch = '\0';
+	for (len = 0; ch != '\n' && !eof && !error; ++len) {
+		switch (read(fd, &ch, 1)) {
+		case 1:
+			if (len < PAM_MAX_RESP_SIZE - 1) {
+				buf[len + 1] = '\0';
+				buf[len] = ch;
+			}
+			break;
+		case 0:
+			eof = 1;
+			break;
+		default:
+			error = errno;
+			break;
+		}
+	}
 	alarm(0);
 	sigaction(SIGALRM, &saved_action, NULL);
 	sigprocmask(SIG_SETMASK, &saved_sigset, NULL);
 	alarm(saved_alarm);
-	if (timed_out || ferror(stdin) || feof(stdin)) {
+	if (error == EINTR)
+		fputs(" timeout!", stderr);
+	if (error || eof) {
+		fputs("\n", stderr);
 		memset(buf, 0, sizeof(buf));
 		return (NULL);
 	}



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200307141217.h6ECHmnu058044>