Blame libfreerdp/common/addin.c

Packit 1fb8d4
/**
Packit 1fb8d4
 * FreeRDP: A Remote Desktop Protocol Implementation
Packit 1fb8d4
 * Addin Loader
Packit 1fb8d4
 *
Packit 1fb8d4
 * Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
Packit 1fb8d4
 *
Packit 1fb8d4
 * Licensed under the Apache License, Version 2.0 (the "License");
Packit 1fb8d4
 * you may not use this file except in compliance with the License.
Packit 1fb8d4
 * You may obtain a copy of the License at
Packit 1fb8d4
 *
Packit 1fb8d4
 *     http://www.apache.org/licenses/LICENSE-2.0
Packit 1fb8d4
 *
Packit 1fb8d4
 * Unless required by applicable law or agreed to in writing, software
Packit 1fb8d4
 * distributed under the License is distributed on an "AS IS" BASIS,
Packit 1fb8d4
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Packit 1fb8d4
 * See the License for the specific language governing permissions and
Packit 1fb8d4
 * limitations under the License.
Packit 1fb8d4
 */
Packit 1fb8d4
Packit 1fb8d4
#ifdef HAVE_CONFIG_H
Packit 1fb8d4
#include "config.h"
Packit 1fb8d4
#endif
Packit 1fb8d4
Packit 1fb8d4
#include <stdio.h>
Packit 1fb8d4
#include <stdlib.h>
Packit 1fb8d4
#include <string.h>
Packit 1fb8d4
Packit 1fb8d4
#include <winpr/crt.h>
Packit 1fb8d4
#include <winpr/path.h>
Packit 1fb8d4
#include <winpr/string.h>
Packit 1fb8d4
#include <winpr/library.h>
Packit 1fb8d4
Packit 1fb8d4
#include <freerdp/addin.h>
Packit 1fb8d4
#include <freerdp/build-config.h>
Packit 1fb8d4
Packit 1fb8d4
#include <freerdp/log.h>
Packit 1fb8d4
#define TAG FREERDP_TAG("addin")
Packit 1fb8d4
Packit 1fb8d4
static INLINE BOOL is_path_required(LPCSTR path, size_t len)
Packit 1fb8d4
{
Packit 1fb8d4
	if (!path || (len <= 1))
Packit 1fb8d4
		return FALSE;
Packit 1fb8d4
Packit 1fb8d4
	if (strcmp(path, ".") == 0)
Packit 1fb8d4
		return FALSE;
Packit 1fb8d4
Packit 1fb8d4
	return TRUE;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
LPSTR freerdp_get_library_install_path(void)
Packit 1fb8d4
{
Packit 1fb8d4
	LPSTR pszPath;
Packit 1fb8d4
	size_t cchPath;
Packit 1fb8d4
	size_t cchLibraryPath;
Packit 1fb8d4
	size_t cchInstallPrefix;
Packit 1fb8d4
	BOOL needLibPath, needInstallPath;
Packit 1fb8d4
	LPCSTR pszLibraryPath = FREERDP_LIBRARY_PATH;
Packit 1fb8d4
	LPCSTR pszInstallPrefix = FREERDP_INSTALL_PREFIX;
Packit 1fb8d4
	cchLibraryPath = strlen(pszLibraryPath) + 1;
Packit 1fb8d4
	cchInstallPrefix = strlen(pszInstallPrefix) + 1;
Packit 1fb8d4
	cchPath = cchInstallPrefix + cchLibraryPath;
Packit 1fb8d4
	needInstallPath = is_path_required(pszInstallPrefix, cchInstallPrefix);
Packit 1fb8d4
	needLibPath = is_path_required(pszLibraryPath, cchLibraryPath);
Packit 1fb8d4
Packit 1fb8d4
	if (!needInstallPath && !needLibPath)
Packit 1fb8d4
		return NULL;
Packit 1fb8d4
Packit Service 5a9772
	pszPath = (LPSTR)malloc(cchPath + 1);
Packit 1fb8d4
Packit 1fb8d4
	if (!pszPath)
Packit 1fb8d4
		return NULL;
Packit 1fb8d4
Packit 1fb8d4
	if (needInstallPath)
Packit 1fb8d4
	{
Packit 1fb8d4
		CopyMemory(pszPath, pszInstallPrefix, cchInstallPrefix);
Packit 1fb8d4
		pszPath[cchInstallPrefix] = '\0';
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	if (needLibPath)
Packit 1fb8d4
	{
Packit 1fb8d4
		if (FAILED(NativePathCchAppendA(pszPath, cchPath + 1, pszLibraryPath)))
Packit 1fb8d4
		{
Packit 1fb8d4
			free(pszPath);
Packit 1fb8d4
			return NULL;
Packit 1fb8d4
		}
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	return pszPath;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
LPSTR freerdp_get_dynamic_addin_install_path(void)
Packit 1fb8d4
{
Packit 1fb8d4
	LPSTR pszPath;
Packit 1fb8d4
	size_t cchPath;
Packit 1fb8d4
	size_t cchAddinPath;
Packit 1fb8d4
	size_t cchInstallPrefix;
Packit 1fb8d4
	BOOL needLibPath, needInstallPath;
Packit 1fb8d4
	LPCSTR pszAddinPath = FREERDP_ADDIN_PATH;
Packit 1fb8d4
	LPCSTR pszInstallPrefix = FREERDP_INSTALL_PREFIX;
Packit 1fb8d4
	cchAddinPath = strlen(pszAddinPath) + 1;
Packit 1fb8d4
	cchInstallPrefix = strlen(pszInstallPrefix) + 1;
Packit 1fb8d4
	cchPath = cchInstallPrefix + cchAddinPath;
Packit 1fb8d4
	needInstallPath = is_path_required(pszInstallPrefix, cchInstallPrefix);
Packit 1fb8d4
	needLibPath = is_path_required(pszAddinPath, cchAddinPath);
Packit 1fb8d4
Packit 1fb8d4
	if (!needInstallPath && !needLibPath)
Packit 1fb8d4
		return NULL;
Packit 1fb8d4
Packit Service 5a9772
	pszPath = (LPSTR)calloc(cchPath + 1, sizeof(CHAR));
Packit 1fb8d4
Packit 1fb8d4
	if (!pszPath)
Packit 1fb8d4
		return NULL;
Packit 1fb8d4
Packit 1fb8d4
	if (needInstallPath)
Packit 1fb8d4
	{
Packit 1fb8d4
		CopyMemory(pszPath, pszInstallPrefix, cchInstallPrefix);
Packit 1fb8d4
		pszPath[cchInstallPrefix] = '\0';
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	if (needLibPath)
Packit 1fb8d4
	{
Packit 1fb8d4
		if (FAILED(NativePathCchAppendA(pszPath, cchPath + 1, pszAddinPath)))
Packit 1fb8d4
		{
Packit 1fb8d4
			free(pszPath);
Packit 1fb8d4
			return NULL;
Packit 1fb8d4
		}
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	return pszPath;
Packit 1fb8d4
}
Packit 1fb8d4
Packit Service 5a9772
PVIRTUALCHANNELENTRY freerdp_load_dynamic_addin(LPCSTR pszFileName, LPCSTR pszPath,
Packit Service 5a9772
                                                LPCSTR pszEntryName)
Packit 1fb8d4
{
Packit 1fb8d4
	LPSTR pszAddinInstallPath = freerdp_get_dynamic_addin_install_path();
Packit 1fb8d4
	PVIRTUALCHANNELENTRY entry = NULL;
Packit 1fb8d4
	BOOL bHasExt = TRUE;
Packit 1fb8d4
	PCSTR pszExt;
Packit 1fb8d4
	size_t cchExt = 0;
Packit 1fb8d4
	HINSTANCE library = NULL;
Packit 1fb8d4
	size_t cchFileName;
Packit 1fb8d4
	size_t cchFilePath;
Packit 1fb8d4
	LPSTR pszAddinFile = NULL;
Packit 1fb8d4
	LPSTR pszFilePath = NULL;
Packit 1fb8d4
	LPSTR pszRelativeFilePath = NULL;
Packit 1fb8d4
	size_t cchAddinFile;
Packit 1fb8d4
	size_t cchAddinInstallPath;
Packit 1fb8d4
Packit 1fb8d4
	if (!pszFileName || !pszEntryName)
Packit 1fb8d4
		goto fail;
Packit 1fb8d4
Packit 1fb8d4
	cchFileName = strlen(pszFileName);
Packit 1fb8d4
Packit 1fb8d4
	/* Get file name with prefix and extension */
Packit 1fb8d4
	if (FAILED(PathCchFindExtensionA(pszFileName, cchFileName + 1, &pszExt)))
Packit 1fb8d4
	{
Packit 1fb8d4
		pszExt = PathGetSharedLibraryExtensionA(PATH_SHARED_LIB_EXT_WITH_DOT);
Packit 1fb8d4
		cchExt = strlen(pszExt);
Packit 1fb8d4
		bHasExt = FALSE;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	if (bHasExt)
Packit 1fb8d4
	{
Packit 1fb8d4
		pszAddinFile = _strdup(pszFileName);
Packit 1fb8d4
Packit 1fb8d4
		if (!pszAddinFile)
Packit 1fb8d4
			goto fail;
Packit 1fb8d4
	}
Packit 1fb8d4
	else
Packit 1fb8d4
	{
Packit 1fb8d4
		cchAddinFile = cchFileName + cchExt + 2 + sizeof(FREERDP_SHARED_LIBRARY_PREFIX);
Packit Service 5a9772
		pszAddinFile = (LPSTR)malloc(cchAddinFile + 1);
Packit 1fb8d4
Packit 1fb8d4
		if (!pszAddinFile)
Packit 1fb8d4
			goto fail;
Packit 1fb8d4
Packit Service 5a9772
		sprintf_s(pszAddinFile, cchAddinFile, FREERDP_SHARED_LIBRARY_PREFIX "%s%s", pszFileName,
Packit Service 5a9772
		          pszExt);
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	cchAddinFile = strlen(pszAddinFile);
Packit 1fb8d4
Packit 1fb8d4
	/* If a path is provided prefix the library name with it. */
Packit 1fb8d4
	if (pszPath)
Packit 1fb8d4
	{
Packit 1fb8d4
		size_t relPathLen = strlen(pszPath) + cchAddinFile + 1;
Packit 1fb8d4
		pszRelativeFilePath = calloc(relPathLen, sizeof(CHAR));
Packit 1fb8d4
Packit 1fb8d4
		if (!pszRelativeFilePath)
Packit 1fb8d4
			goto fail;
Packit 1fb8d4
Packit 1fb8d4
		sprintf_s(pszRelativeFilePath, relPathLen, "%s", pszPath);
Packit 1fb8d4
		NativePathCchAppendA(pszRelativeFilePath, relPathLen, pszAddinFile);
Packit 1fb8d4
	}
Packit 1fb8d4
	else
Packit 1fb8d4
		pszRelativeFilePath = _strdup(pszAddinFile);
Packit 1fb8d4
Packit 1fb8d4
	if (!pszRelativeFilePath)
Packit 1fb8d4
		goto fail;
Packit 1fb8d4
Packit 1fb8d4
	/* If a system prefix path is provided try these locations too. */
Packit 1fb8d4
	if (pszAddinInstallPath)
Packit 1fb8d4
	{
Packit 1fb8d4
		cchAddinInstallPath = strlen(pszAddinInstallPath);
Packit 1fb8d4
		cchFilePath = cchAddinInstallPath + cchFileName + 32;
Packit Service 5a9772
		pszFilePath = (LPSTR)malloc(cchFilePath + 1);
Packit 1fb8d4
Packit 1fb8d4
		if (!pszFilePath)
Packit 1fb8d4
			goto fail;
Packit 1fb8d4
Packit 1fb8d4
		CopyMemory(pszFilePath, pszAddinInstallPath, cchAddinInstallPath);
Packit 1fb8d4
		pszFilePath[cchAddinInstallPath] = '\0';
Packit Service 5a9772
		NativePathCchAppendA((LPSTR)pszFilePath, cchFilePath + 1, pszRelativeFilePath);
Packit 1fb8d4
	}
Packit 1fb8d4
	else
Packit 1fb8d4
		pszFilePath = _strdup(pszRelativeFilePath);
Packit 1fb8d4
Packit 1fb8d4
	library = LoadLibraryA(pszFilePath);
Packit 1fb8d4
Packit 1fb8d4
	if (!library)
Packit 1fb8d4
		goto fail;
Packit 1fb8d4
Packit 1fb8d4
	entry = (PVIRTUALCHANNELENTRY)GetProcAddress(library, pszEntryName);
Packit 1fb8d4
fail:
Packit 1fb8d4
	free(pszRelativeFilePath);
Packit 1fb8d4
	free(pszAddinFile);
Packit 1fb8d4
	free(pszFilePath);
Packit 1fb8d4
	free(pszAddinInstallPath);
Packit 1fb8d4
Packit 1fb8d4
	if (!entry && library)
Packit 1fb8d4
		FreeLibrary(library);
Packit 1fb8d4
Packit 1fb8d4
	return entry;
Packit 1fb8d4
}
Packit 1fb8d4
Packit Service 5a9772
PVIRTUALCHANNELENTRY freerdp_load_dynamic_channel_addin_entry(LPCSTR pszName, LPCSTR pszSubsystem,
Packit Service 5a9772
                                                              LPCSTR pszType, DWORD dwFlags)
Packit 1fb8d4
{
Packit 1fb8d4
	PVIRTUALCHANNELENTRY entry;
Packit 1fb8d4
	LPSTR pszFileName;
Packit 1fb8d4
	const size_t cchBaseFileName = sizeof(FREERDP_SHARED_LIBRARY_PREFIX) + 32;
Packit 1fb8d4
	LPCSTR pszExtension;
Packit 1fb8d4
	LPCSTR pszPrefix = FREERDP_SHARED_LIBRARY_PREFIX;
Packit Service 5a9772
	size_t nameLen = 0;
Packit Service 5a9772
	size_t subsystemLen = 0;
Packit Service 5a9772
	size_t typeLen = 0;
Packit Service 5a9772
	size_t extensionLen = 0;
Packit 1fb8d4
	pszExtension = PathGetSharedLibraryExtensionA(0);
Packit Service 5a9772
	if (pszName)
Packit Service 5a9772
		nameLen = strnlen(pszName, MAX_PATH);
Packit Service 5a9772
	if (pszSubsystem)
Packit Service 5a9772
		subsystemLen = strnlen(pszSubsystem, MAX_PATH);
Packit Service 5a9772
	if (pszType)
Packit Service 5a9772
		typeLen = strnlen(pszType, MAX_PATH);
Packit Service 5a9772
	if (pszExtension)
Packit Service 5a9772
		extensionLen = strnlen(pszExtension, MAX_PATH);
Packit 1fb8d4
Packit 1fb8d4
	if (pszName && pszSubsystem && pszType)
Packit 1fb8d4
	{
Packit Service 5a9772
		const size_t cchFileName =
Packit Service 5a9772
		    cchBaseFileName + nameLen + subsystemLen + typeLen + extensionLen;
Packit Service 5a9772
		pszFileName = (LPSTR)malloc(cchFileName);
Packit 1fb8d4
Packit 1fb8d4
		if (!pszFileName)
Packit 1fb8d4
			return NULL;
Packit 1fb8d4
Packit 1fb8d4
		sprintf_s(pszFileName, cchFileName, "%s%s-client-%s-%s.%s", pszPrefix, pszName,
Packit 1fb8d4
		          pszSubsystem, pszType, pszExtension);
Packit 1fb8d4
	}
Packit 1fb8d4
	else if (pszName && pszSubsystem)
Packit 1fb8d4
	{
Packit Service 5a9772
		const size_t cchFileName = cchBaseFileName + nameLen + subsystemLen + extensionLen;
Packit Service 5a9772
		pszFileName = (LPSTR)malloc(cchFileName);
Packit 1fb8d4
Packit 1fb8d4
		if (!pszFileName)
Packit 1fb8d4
			return NULL;
Packit 1fb8d4
Packit Service 5a9772
		sprintf_s(pszFileName, cchFileName, "%s%s-client-%s.%s", pszPrefix, pszName, pszSubsystem,
Packit Service 5a9772
		          pszExtension);
Packit 1fb8d4
	}
Packit 1fb8d4
	else if (pszName)
Packit 1fb8d4
	{
Packit Service 5a9772
		const size_t cchFileName = cchBaseFileName + nameLen + extensionLen;
Packit Service 5a9772
		pszFileName = (LPSTR)malloc(cchFileName);
Packit 1fb8d4
Packit 1fb8d4
		if (!pszFileName)
Packit 1fb8d4
			return NULL;
Packit 1fb8d4
Packit Service 5a9772
		sprintf_s(pszFileName, cchFileName, "%s%s-client.%s", pszPrefix, pszName, pszExtension);
Packit 1fb8d4
	}
Packit 1fb8d4
	else
Packit 1fb8d4
	{
Packit 1fb8d4
		return NULL;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	if (pszSubsystem)
Packit 1fb8d4
	{
Packit 1fb8d4
		LPSTR pszEntryName;
Packit 1fb8d4
		size_t cchEntryName;
Packit 1fb8d4
		/* subsystem add-in */
Packit Service 5a9772
		cchEntryName = 64 + nameLen;
Packit Service 5a9772
		pszEntryName = (LPSTR)malloc(cchEntryName + 1);
Packit 1fb8d4
Packit 1fb8d4
		if (!pszEntryName)
Packit 1fb8d4
		{
Packit 1fb8d4
			free(pszFileName);
Packit 1fb8d4
			return NULL;
Packit 1fb8d4
		}
Packit 1fb8d4
Packit Service 5a9772
		sprintf_s(pszEntryName, cchEntryName + 1, "freerdp_%s_client_subsystem_entry", pszName);
Packit 1fb8d4
		entry = freerdp_load_dynamic_addin(pszFileName, NULL, pszEntryName);
Packit 1fb8d4
		free(pszEntryName);
Packit 1fb8d4
		free(pszFileName);
Packit 1fb8d4
		return entry;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	/* channel add-in */
Packit 1fb8d4
Packit 1fb8d4
	if (dwFlags & FREERDP_ADDIN_CHANNEL_STATIC)
Packit 1fb8d4
	{
Packit 1fb8d4
		if (dwFlags & FREERDP_ADDIN_CHANNEL_ENTRYEX)
Packit 1fb8d4
			entry = freerdp_load_dynamic_addin(pszFileName, NULL, "VirtualChannelEntryEx");
Packit 1fb8d4
		else
Packit 1fb8d4
			entry = freerdp_load_dynamic_addin(pszFileName, NULL, "VirtualChannelEntry");
Packit 1fb8d4
	}
Packit 1fb8d4
	else if (dwFlags & FREERDP_ADDIN_CHANNEL_DYNAMIC)
Packit 1fb8d4
		entry = freerdp_load_dynamic_addin(pszFileName, NULL, "DVCPluginEntry");
Packit 1fb8d4
	else if (dwFlags & FREERDP_ADDIN_CHANNEL_DEVICE)
Packit 1fb8d4
		entry = freerdp_load_dynamic_addin(pszFileName, NULL, "DeviceServiceEntry");
Packit 1fb8d4
	else
Packit 1fb8d4
		entry = freerdp_load_dynamic_addin(pszFileName, NULL, pszType);
Packit 1fb8d4
Packit 1fb8d4
	free(pszFileName);
Packit 1fb8d4
	return entry;
Packit 1fb8d4
}
Packit 1fb8d4
Packit Service 5a9772
static FREERDP_LOAD_CHANNEL_ADDIN_ENTRY_FN freerdp_load_static_channel_addin_entry = NULL;
Packit 1fb8d4
Packit Service 5a9772
int freerdp_register_addin_provider(FREERDP_LOAD_CHANNEL_ADDIN_ENTRY_FN provider, DWORD dwFlags)
Packit 1fb8d4
{
Packit 1fb8d4
	freerdp_load_static_channel_addin_entry = provider;
Packit 1fb8d4
	return 0;
Packit 1fb8d4
}
Packit 1fb8d4
Packit Service 5a9772
PVIRTUALCHANNELENTRY freerdp_load_channel_addin_entry(LPCSTR pszName, LPCSTR pszSubsystem,
Packit Service 5a9772
                                                      LPCSTR pszType, DWORD dwFlags)
Packit 1fb8d4
{
Packit 1fb8d4
	PVIRTUALCHANNELENTRY entry = NULL;
Packit 1fb8d4
Packit 1fb8d4
	if (freerdp_load_static_channel_addin_entry)
Packit 1fb8d4
		entry = freerdp_load_static_channel_addin_entry(pszName, pszSubsystem, pszType, dwFlags);
Packit 1fb8d4
Packit 1fb8d4
	if (!entry)
Packit 1fb8d4
		entry = freerdp_load_dynamic_channel_addin_entry(pszName, pszSubsystem, pszType, dwFlags);
Packit 1fb8d4
Packit 1fb8d4
	if (!entry)
Packit 1fb8d4
		WLog_WARN(TAG, "Failed to load channel %s [%s]", pszName, pszSubsystem);
Packit 1fb8d4
Packit 1fb8d4
	return entry;
Packit 1fb8d4
}