[Pcsclite-git-commit] [PCSC] 01/01: Add support of remove and/or customize PC/SC reader names

Ludovic Rousseau rousseau at moszumanska.debian.org
Mon Dec 21 21:23:26 UTC 2015


This is an automated email from the git hooks/post-receive script.

rousseau pushed a commit to branch master
in repository PCSC.

commit 5ad0431b9fe0c2a661e81ac8cc3ed52c436934e2
Author: Ludovic Rousseau <ludovic.rousseau at free.fr>
Date:   Mon Dec 21 22:21:04 2015 +0100

    Add support of remove and/or customize PC/SC reader names
    
    To enable these two features you need to configure pcsc-lite with
    --enable-filter.
    
    Ignore some readers
    ===================
    If the environment variable PCSCLITE_FILTER_IGNORE_READER_NAMES is
    defined then it contains a list of patterns separated by the character
    ":".
    If a pattern is found in a PC/SC reader name then this reader is ignored
    and will not be reported by SCardListReaders() or any other PC/SC calls.
    
    Extend reader names
    ===================
    To differentiate the PC/SC reader names one idea is to use the host name
    of the system. If the IT department is doing correctly his job every
    laptop should have a different host name.
    
    If the environment variable PCSCLITE_FILTER_EXTEND_READER_NAMES is
    defined then it contains a string that will be added at the end of the
    PC/SC reader names.
    The computer host name is available in the variable $HOSTNAME. If you
    want to have a space character between the PC/SC reader name and host
    name you define PCSCLITE_FILTER_EXTEND_READER_NAMES as:
    " $HOSTNAME".
    
    See also
    http://ludovicrousseau.blogspot.fr/2015/12/remove-andor-customize-pcsc-reader-names.html
---
 configure.ac        | 12 +++++++++++
 src/readerfactory.c | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++---
 2 files changed, 71 insertions(+), 3 deletions(-)

diff --git a/configure.ac b/configure.ac
index 91f7787..6eba37d 100644
--- a/configure.ac
+++ b/configure.ac
@@ -370,6 +370,17 @@ AS_HELP_STRING([--enable-confdir=DIR],[directory containing reader configuration
 	[confdir="${enableval}"],
 	[confdir="${sysconfdir}/reader.conf.d"])
 
+# --enable-filter
+AC_ARG_ENABLE(filter,
+  AS_HELP_STRING([--enable-filter],[allow to filter readers using
+				  PCSCLITE_FILTER_IGNORE_READER_NAMES and PCSCLITE_FILTER_EXTEND_READER_NAMES]),
+  [ use_filter="${enableval}" ], [ use_filter="no" ])
+
+if test x$use_filter = xyes; then
+  AC_DEFINE(FILTER_NAMES, 1, [Filter reader names])
+  PCSCLITE_FEATURES="${PCSCLITE_FEATURES} filter"
+fi
+
 # Setup dist stuff
 AC_SUBST(usbdropdir)
 AC_SUBST(ipcdir)
@@ -420,6 +431,7 @@ use serial:             ${use_serial}
 use usb:                ${use_usb}
 systemd unit directory: ${with_systemdsystemunitdir}
 serial config dir.:     ${confdir_exp}
+filter:                 ${use_filter}
 
 PCSCLITE_FEATURES:      ${PCSCLITE_FEATURES}
 
diff --git a/src/readerfactory.c b/src/readerfactory.c
index 235a75c..14cd42f 100644
--- a/src/readerfactory.c
+++ b/src/readerfactory.c
@@ -168,6 +168,45 @@ LONG RFAddReader(const char *readerNameLong, int port, const char *library,
 	if ((readerNameLong == NULL) || (library == NULL) || (device == NULL))
 		return SCARD_E_INVALID_VALUE;
 
+#ifdef FILTER_NAMES
+	const char *ro_filter = getenv("PCSCLITE_FILTER_IGNORE_READER_NAMES");
+	if (ro_filter)
+	{
+		char *filter, *next;
+
+		/* get a RW copy of the env string */
+		filter = alloca(strlen(ro_filter)+1);
+		strcpy(filter, ro_filter);
+
+		while (filter)
+		{
+			/* ':' is the separator */
+			next = strchr(filter, ':');
+			if (next)
+			{
+				/* NUL terminate the current pattern */
+				*next = '\0';
+			}
+
+			/* if filter is non empty and found in the reader name */
+			if (*filter && strstr(readerNameLong, filter))
+			{
+				Log3(PCSC_LOG_ERROR,
+					"Reader name \"%s\" contains \"%s\": ignored",
+					readerNameLong, filter);
+				return SCARD_E_READER_UNAVAILABLE;
+			}
+
+			if (next)
+				/* next pattern */
+				filter = next+1;
+			else
+				/* end */
+				filter = NULL;
+		}
+	}
+#endif
+
 	/* allocate memory that is automatically freed */
 	readerName = alloca(strlen(readerNameLong)+1);
 	strcpy(readerName, readerNameLong);
@@ -535,10 +574,20 @@ LONG RFRemoveReader(const char *readerName, int port)
 {
 	char lpcStripReader[MAX_READERNAME];
 	int i;
+#ifdef FILTER_NAMES
+	const char *extend;
+#endif
+	int extend_size = 0;
 
 	if (readerName == NULL)
 		return SCARD_E_INVALID_VALUE;
 
+#ifdef FILTER_NAMES
+	extend = getenv("PCSCLITE_FILTER_EXTEND_READER_NAMES");
+	if (extend)
+		extend_size = strlen(extend);
+#endif
+
 	for (i = 0; i < PCSCLITE_MAX_READERS_CONTEXTS; i++)
 	{
 		if (sReadersContexts[i]->vHandle != 0)
@@ -546,7 +595,7 @@ LONG RFRemoveReader(const char *readerName, int port)
 			strncpy(lpcStripReader,
 				sReadersContexts[i]->readerState->readerName,
 				sizeof(lpcStripReader));
-			lpcStripReader[strlen(lpcStripReader) - 6] = 0;
+			lpcStripReader[strlen(lpcStripReader) - 6 - extend_size] = 0;
 
 			/* Compare only the significant part of the reader name */
 			if ((strncmp(readerName, lpcStripReader, MAX_READERNAME - sizeof(" 00 00")) == 0)
@@ -648,6 +697,7 @@ LONG RFSetReaderName(READER_CONTEXT * rContext, const char *readerName,
 	int supportedChannels = 0;
 	int usedDigits[PCSCLITE_MAX_READERS_CONTEXTS];
 	int i;
+	const char *extend = "";
 
 	/* Clear the list */
 	for (i = 0; i < PCSCLITE_MAX_READERS_CONTEXTS; i++)
@@ -738,9 +788,15 @@ LONG RFSetReaderName(READER_CONTEXT * rContext, const char *readerName,
 		}
 	}
 
+#ifdef FILTER_NAMES
+	extend = getenv("PCSCLITE_FILTER_EXTEND_READER_NAMES");
+	if (NULL == extend)
+		extend = "";
+#endif
+
 	snprintf(rContext->readerState->readerName,
-		sizeof(rContext->readerState->readerName), "%s %02X 00",
-		readerName, i);
+		sizeof(rContext->readerState->readerName), "%s%s %02X 00",
+		readerName, extend, i);
 
 	/* Set the slot in 0xDDDDCCCC */
 	rContext->slot = i << 16;

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pcsclite/PCSC.git



More information about the Pcsclite-cvs-commit mailing list