2012-08-15 01:09:01 +04:00
|
|
|
/**
|
2012-10-09 07:02:04 +04:00
|
|
|
* FreeRDP: A Remote Desktop Protocol Implementation
|
2012-08-15 01:09:01 +04:00
|
|
|
* Smartcard Device Service Virtual Channel
|
|
|
|
*
|
|
|
|
* Copyright 2011 O.S. Systems Software Ltda.
|
|
|
|
* Copyright 2011 Eduardo Fiss Beloni <beloni@ossystems.com.br>
|
|
|
|
* Copyright 2011 Anthony Tong <atong@trustedcs.com>
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
2011-10-15 19:30:10 +04:00
|
|
|
#include "config.h"
|
2012-08-15 01:09:01 +04:00
|
|
|
#endif
|
2011-10-15 19:30:10 +04:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <freerdp/utils/list.h>
|
|
|
|
#include <freerdp/utils/thread.h>
|
|
|
|
#include <freerdp/utils/svc_plugin.h>
|
2012-09-19 01:33:52 +04:00
|
|
|
#include <freerdp/utils/debug.h>
|
2012-10-09 05:00:07 +04:00
|
|
|
#include <freerdp/channels/rdpdr.h>
|
2011-10-15 19:30:10 +04:00
|
|
|
|
|
|
|
#include "scard_main.h"
|
|
|
|
|
2012-09-23 21:54:14 +04:00
|
|
|
static void scard_free(DEVICE* dev)
|
2011-10-15 19:30:10 +04:00
|
|
|
{
|
|
|
|
IRP* irp;
|
2012-08-02 02:05:45 +04:00
|
|
|
COMPLETIONIDINFO* CompletionIdInfo;
|
2012-09-23 21:54:14 +04:00
|
|
|
SCARD_DEVICE* scard = (SCARD_DEVICE*) dev;
|
2011-10-15 19:30:10 +04:00
|
|
|
|
|
|
|
freerdp_thread_stop(scard->thread);
|
|
|
|
freerdp_thread_free(scard->thread);
|
2012-10-03 05:52:27 +04:00
|
|
|
|
|
|
|
while ((irp = (IRP*) InterlockedPopEntrySList(scard->pIrpList)) != NULL)
|
2011-10-15 19:30:10 +04:00
|
|
|
irp->Discard(irp);
|
2012-09-23 21:54:14 +04:00
|
|
|
|
2012-10-03 05:52:27 +04:00
|
|
|
_aligned_free(scard->pIrpList);
|
2011-10-15 19:30:10 +04:00
|
|
|
|
2012-08-02 02:05:45 +04:00
|
|
|
/* Begin TS Client defect workaround. */
|
2012-09-23 21:54:14 +04:00
|
|
|
|
|
|
|
while ((CompletionIdInfo = (COMPLETIONIDINFO*) list_dequeue(scard->CompletionIds)) != NULL)
|
2012-10-09 07:21:26 +04:00
|
|
|
free(CompletionIdInfo);
|
2012-09-23 21:54:14 +04:00
|
|
|
|
2012-08-02 02:05:45 +04:00
|
|
|
list_free(scard->CompletionIds);
|
2012-09-23 21:54:14 +04:00
|
|
|
|
2012-08-02 02:05:45 +04:00
|
|
|
/* End TS Client defect workaround. */
|
|
|
|
|
2012-10-09 07:21:26 +04:00
|
|
|
free(dev);
|
2011-10-15 19:30:10 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-09-23 21:54:14 +04:00
|
|
|
static void scard_process_irp(SCARD_DEVICE* scard, IRP* irp)
|
2011-10-15 19:30:10 +04:00
|
|
|
{
|
|
|
|
switch (irp->MajorFunction)
|
|
|
|
{
|
|
|
|
case IRP_MJ_DEVICE_CONTROL:
|
|
|
|
scard_device_control(scard, irp);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
printf("MajorFunction 0x%X unexpected for smartcards.", irp->MajorFunction);
|
|
|
|
DEBUG_WARN("Smartcard MajorFunction 0x%X not supported.", irp->MajorFunction);
|
|
|
|
irp->IoStatus = STATUS_NOT_SUPPORTED;
|
|
|
|
irp->Complete(irp);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-23 21:54:14 +04:00
|
|
|
static void scard_process_irp_list(SCARD_DEVICE* scard)
|
2011-10-15 19:30:10 +04:00
|
|
|
{
|
2012-09-23 21:54:14 +04:00
|
|
|
IRP* irp;
|
2011-10-15 19:30:10 +04:00
|
|
|
|
|
|
|
while (!freerdp_thread_is_stopped(scard->thread))
|
|
|
|
{
|
2012-10-03 05:52:27 +04:00
|
|
|
irp = (IRP*) InterlockedPopEntrySList(scard->pIrpList);
|
2011-10-15 19:30:10 +04:00
|
|
|
|
|
|
|
if (irp == NULL)
|
|
|
|
break;
|
|
|
|
|
|
|
|
scard_process_irp(scard, irp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-23 21:54:14 +04:00
|
|
|
struct scard_irp_thread_args
|
|
|
|
{
|
2011-10-15 19:30:10 +04:00
|
|
|
SCARD_DEVICE* scard;
|
|
|
|
IRP* irp;
|
|
|
|
freerdp_thread* thread;
|
|
|
|
};
|
|
|
|
|
2012-09-23 21:54:14 +04:00
|
|
|
static void scard_process_irp_thread_func(struct scard_irp_thread_args* args)
|
2011-10-15 19:30:10 +04:00
|
|
|
{
|
|
|
|
scard_process_irp(args->scard, args->irp);
|
|
|
|
|
|
|
|
freerdp_thread_free(args->thread);
|
2012-10-09 07:21:26 +04:00
|
|
|
free(args);
|
2011-10-15 19:30:10 +04:00
|
|
|
}
|
|
|
|
|
2012-09-23 21:54:14 +04:00
|
|
|
static void* scard_thread_func(void* arg)
|
2011-10-15 19:30:10 +04:00
|
|
|
{
|
|
|
|
SCARD_DEVICE* scard = (SCARD_DEVICE*) arg;
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
freerdp_thread_wait(scard->thread);
|
|
|
|
|
|
|
|
if (freerdp_thread_is_stopped(scard->thread))
|
|
|
|
break;
|
|
|
|
|
|
|
|
freerdp_thread_reset(scard->thread);
|
|
|
|
scard_process_irp_list(scard);
|
|
|
|
}
|
|
|
|
|
|
|
|
freerdp_thread_quit(scard->thread);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-08-02 02:05:45 +04:00
|
|
|
/* Begin TS Client defect workaround. */
|
2012-10-09 11:26:39 +04:00
|
|
|
static COMPLETIONIDINFO* scard_mark_duplicate_id(SCARD_DEVICE* scard, UINT32 CompletionId)
|
2012-08-02 02:05:45 +04:00
|
|
|
{
|
2012-09-23 21:54:14 +04:00
|
|
|
/*
|
|
|
|
* Search from the beginning of the LIST for one outstanding "CompletionID"
|
|
|
|
* that matches the one passed in. If there is one, mark it as a duplicate
|
|
|
|
* if it is not already marked.
|
|
|
|
*/
|
2012-08-02 02:05:45 +04:00
|
|
|
LIST_ITEM* item;
|
|
|
|
COMPLETIONIDINFO* CompletionIdInfo;
|
|
|
|
|
|
|
|
for (item = scard->CompletionIds->head; item; item = item->next)
|
|
|
|
{
|
|
|
|
CompletionIdInfo = (COMPLETIONIDINFO*)item->data;
|
|
|
|
if (CompletionIdInfo->ID == CompletionId)
|
|
|
|
{
|
2012-10-09 10:31:28 +04:00
|
|
|
if (FALSE == CompletionIdInfo->duplicate)
|
2012-08-02 02:05:45 +04:00
|
|
|
{
|
2012-10-09 10:31:28 +04:00
|
|
|
CompletionIdInfo->duplicate = TRUE;
|
2012-08-02 02:05:45 +04:00
|
|
|
DEBUG_WARN("CompletionID number %u is now marked as a duplicate.", CompletionId);
|
|
|
|
}
|
|
|
|
return CompletionIdInfo;
|
|
|
|
}
|
|
|
|
}
|
2012-09-23 21:54:14 +04:00
|
|
|
|
2012-08-02 02:05:45 +04:00
|
|
|
return NULL; /* Either no items in the list or no match. */
|
|
|
|
}
|
|
|
|
|
2012-10-09 11:26:39 +04:00
|
|
|
static BOOL scard_check_for_duplicate_id(SCARD_DEVICE* scard, UINT32 CompletionId)
|
2012-08-02 02:05:45 +04:00
|
|
|
{
|
2012-09-23 21:54:14 +04:00
|
|
|
/*
|
|
|
|
* Search from the end of the LIST for one outstanding "CompletionID"
|
|
|
|
* that matches the one passed in. Remove it from the list and free the
|
|
|
|
* memory associated with it. Return whether or not it was marked
|
|
|
|
* as a duplicate.
|
|
|
|
*/
|
2012-08-02 02:05:45 +04:00
|
|
|
LIST_ITEM* item;
|
|
|
|
COMPLETIONIDINFO* CompletionIdInfo;
|
2012-10-09 10:38:39 +04:00
|
|
|
BOOL duplicate;
|
2012-08-02 02:05:45 +04:00
|
|
|
|
|
|
|
for (item = scard->CompletionIds->tail; item; item = item->prev)
|
|
|
|
{
|
|
|
|
CompletionIdInfo = (COMPLETIONIDINFO*)item->data;
|
|
|
|
if (CompletionIdInfo->ID == CompletionId)
|
|
|
|
{
|
|
|
|
duplicate = CompletionIdInfo->duplicate;
|
2012-10-09 10:31:28 +04:00
|
|
|
if (TRUE == duplicate)
|
2012-08-02 02:05:45 +04:00
|
|
|
{
|
|
|
|
DEBUG_WARN("CompletionID number %u was previously marked as a duplicate. The response to the command is removed.", CompletionId);
|
|
|
|
}
|
|
|
|
list_remove(scard->CompletionIds, CompletionIdInfo);
|
2012-10-09 07:21:26 +04:00
|
|
|
free(CompletionIdInfo);
|
2012-08-02 02:05:45 +04:00
|
|
|
return duplicate;
|
|
|
|
}
|
|
|
|
}
|
2012-09-23 21:54:14 +04:00
|
|
|
|
2012-08-02 02:05:45 +04:00
|
|
|
/* This function should only be called when there is
|
|
|
|
* at least one outstanding CompletionID item in the list.
|
|
|
|
*/
|
|
|
|
DEBUG_WARN("Error!!! No CompletionIDs (or no matching IDs) in the list!");
|
2012-09-23 21:54:14 +04:00
|
|
|
|
2012-10-09 10:31:28 +04:00
|
|
|
return FALSE;
|
2012-08-02 02:05:45 +04:00
|
|
|
}
|
|
|
|
|
2012-09-23 21:54:14 +04:00
|
|
|
static void scard_irp_complete(IRP* irp)
|
2012-08-02 02:05:45 +04:00
|
|
|
{
|
2012-09-23 21:54:14 +04:00
|
|
|
/* This function is (mostly) a copy of the statically-declared "irp_complete()"
|
|
|
|
* function except that this function adds extra operations for the
|
|
|
|
* smart card's handling of duplicate "CompletionID"s. This function needs
|
|
|
|
* to be in this file so that "scard_irp_request()" can reference it.
|
|
|
|
*/
|
2012-08-02 02:05:45 +04:00
|
|
|
int pos;
|
2012-10-09 10:38:39 +04:00
|
|
|
BOOL duplicate;
|
2012-08-02 02:05:45 +04:00
|
|
|
SCARD_DEVICE* scard = (SCARD_DEVICE*)irp->device;
|
|
|
|
|
|
|
|
DEBUG_SVC("DeviceId %d FileId %d CompletionId %d", irp->device->id, irp->FileId, irp->CompletionId);
|
|
|
|
|
|
|
|
pos = stream_get_pos(irp->output);
|
|
|
|
stream_set_pos(irp->output, 12);
|
2012-10-09 11:26:39 +04:00
|
|
|
stream_write_UINT32(irp->output, irp->IoStatus);
|
2012-08-02 02:05:45 +04:00
|
|
|
stream_set_pos(irp->output, pos);
|
|
|
|
|
|
|
|
/* Begin TS Client defect workaround. */
|
2012-09-19 01:33:52 +04:00
|
|
|
WaitForSingleObject(scard->CompletionIdsMutex, INFINITE);
|
2012-08-02 02:05:45 +04:00
|
|
|
/* Remove from the list the item identified by the CompletionID.
|
|
|
|
* The function returns whether or not it was a duplicate CompletionID.
|
|
|
|
*/
|
|
|
|
duplicate = scard_check_for_duplicate_id(scard, irp->CompletionId);
|
2012-09-19 01:33:52 +04:00
|
|
|
ReleaseMutex(scard->CompletionIdsMutex);
|
2012-08-02 02:05:45 +04:00
|
|
|
|
2012-10-09 10:31:28 +04:00
|
|
|
if (FALSE == duplicate)
|
2012-08-02 02:05:45 +04:00
|
|
|
{
|
|
|
|
svc_plugin_send(irp->devman->plugin, irp->output);
|
|
|
|
irp->output = NULL;
|
|
|
|
}
|
|
|
|
/* End TS Client defect workaround. */
|
|
|
|
|
|
|
|
/* irp_free(irp); The "irp_free()" function is statically-declared
|
|
|
|
* and so is not available to be called
|
|
|
|
* here. Instead, call it indirectly by calling
|
|
|
|
* the IRP's "Discard()" function,
|
|
|
|
* which has already been assigned
|
|
|
|
* to point to "irp_free()" in "irp_new()".
|
|
|
|
*/
|
|
|
|
irp->Discard(irp);
|
|
|
|
}
|
|
|
|
/* End TS Client defect workaround. */
|
|
|
|
|
2012-09-23 21:54:14 +04:00
|
|
|
static void scard_irp_request(DEVICE* device, IRP* irp)
|
2011-10-15 19:30:10 +04:00
|
|
|
{
|
2012-08-02 02:05:45 +04:00
|
|
|
COMPLETIONIDINFO* CompletionIdInfo;
|
2012-09-23 21:54:14 +04:00
|
|
|
SCARD_DEVICE* scard = (SCARD_DEVICE*) device;
|
2011-10-15 19:30:10 +04:00
|
|
|
|
2012-08-02 02:05:45 +04:00
|
|
|
/* Begin TS Client defect workaround. */
|
|
|
|
CompletionIdInfo= xnew(COMPLETIONIDINFO);
|
|
|
|
CompletionIdInfo->ID = irp->CompletionId;/* "duplicate" member is set
|
2012-10-09 10:31:28 +04:00
|
|
|
* to FALSE by "xnew()"
|
2012-08-02 02:05:45 +04:00
|
|
|
*/
|
2012-09-19 01:33:52 +04:00
|
|
|
WaitForSingleObject(scard->CompletionIdsMutex, INFINITE);
|
2012-08-02 02:05:45 +04:00
|
|
|
scard_mark_duplicate_id(scard, irp->CompletionId);
|
|
|
|
list_enqueue(scard->CompletionIds, CompletionIdInfo);
|
2012-09-19 01:33:52 +04:00
|
|
|
ReleaseMutex(scard->CompletionIdsMutex);
|
2012-08-02 02:05:45 +04:00
|
|
|
|
|
|
|
irp->Complete = scard_irp_complete; /* Overwrite the previous
|
|
|
|
* assignment made in
|
|
|
|
* "irp_new()".
|
|
|
|
*/
|
|
|
|
/* End TS Client defect workaround. */
|
|
|
|
|
2011-10-15 19:30:10 +04:00
|
|
|
if (irp->MajorFunction == IRP_MJ_DEVICE_CONTROL &&
|
|
|
|
scard_async_op(irp))
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* certain potentially long running operations
|
|
|
|
* get their own thread
|
|
|
|
* TODO: revise this mechanism.. maybe worker pool
|
|
|
|
*/
|
2012-10-09 07:21:26 +04:00
|
|
|
struct scard_irp_thread_args *args = malloc(sizeof(struct scard_irp_thread_args));
|
2011-10-15 19:30:10 +04:00
|
|
|
|
|
|
|
|
|
|
|
args->thread = freerdp_thread_new();
|
|
|
|
args->scard = scard;
|
|
|
|
args->irp = irp;
|
|
|
|
freerdp_thread_start(args->thread, scard_process_irp_thread_func, args);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-10-03 05:52:27 +04:00
|
|
|
InterlockedPushEntrySList(scard->pIrpList, &(irp->ItemEntry));
|
2011-10-15 19:30:10 +04:00
|
|
|
|
|
|
|
freerdp_thread_signal(scard->thread);
|
|
|
|
}
|
|
|
|
|
2012-10-14 10:38:58 +04:00
|
|
|
#ifdef STATIC_CHANNELS
|
|
|
|
#define DeviceServiceEntry smartcard_DeviceServiceEntry
|
|
|
|
#endif
|
|
|
|
|
2012-09-19 01:33:52 +04:00
|
|
|
int DeviceServiceEntry(PDEVICE_SERVICE_ENTRY_POINTS pEntryPoints)
|
2011-10-15 19:30:10 +04:00
|
|
|
{
|
|
|
|
char* name;
|
|
|
|
char* path;
|
|
|
|
int i, length;
|
2012-09-19 01:33:52 +04:00
|
|
|
SCARD_DEVICE* scard;
|
2011-10-15 19:30:10 +04:00
|
|
|
|
2012-09-19 01:33:52 +04:00
|
|
|
name = (char*) pEntryPoints->plugin_data->data[1];
|
|
|
|
path = (char*) pEntryPoints->plugin_data->data[2];
|
2011-10-15 19:30:10 +04:00
|
|
|
|
|
|
|
if (name)
|
|
|
|
{
|
|
|
|
/* TODO: check if server supports sc redirect (version 5.1) */
|
|
|
|
|
|
|
|
scard = xnew(SCARD_DEVICE);
|
|
|
|
|
|
|
|
scard->device.type = RDPDR_DTYP_SMARTCARD;
|
|
|
|
scard->device.name = "SCARD";
|
|
|
|
scard->device.IRPRequest = scard_irp_request;
|
|
|
|
scard->device.Free = scard_free;
|
|
|
|
|
|
|
|
length = strlen(scard->device.name);
|
|
|
|
scard->device.data = stream_new(length + 1);
|
|
|
|
|
|
|
|
for (i = 0; i <= length; i++)
|
2012-10-09 11:01:37 +04:00
|
|
|
stream_write_BYTE(scard->device.data, name[i] < 0 ? '_' : name[i]);
|
2011-10-15 19:30:10 +04:00
|
|
|
|
|
|
|
scard->path = path;
|
|
|
|
|
2012-10-03 05:52:27 +04:00
|
|
|
scard->pIrpList = (PSLIST_HEADER) _aligned_malloc(sizeof(SLIST_HEADER), MEMORY_ALLOCATION_ALIGNMENT);
|
|
|
|
InitializeSListHead(scard->pIrpList);
|
|
|
|
|
2011-10-15 19:30:10 +04:00
|
|
|
scard->thread = freerdp_thread_new();
|
|
|
|
|
2012-08-02 02:05:45 +04:00
|
|
|
scard->CompletionIds = list_new();
|
2012-09-19 01:33:52 +04:00
|
|
|
scard->CompletionIdsMutex = CreateMutex(NULL, FALSE, NULL);
|
2012-08-02 02:05:45 +04:00
|
|
|
|
2011-10-15 19:30:10 +04:00
|
|
|
pEntryPoints->RegisterDevice(pEntryPoints->devman, (DEVICE *)scard);
|
|
|
|
|
|
|
|
freerdp_thread_start(scard->thread, scard_thread_func, scard);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|