2011-08-17 17:57:09 +04:00
|
|
|
/**
|
2012-10-09 05:00:07 +04:00
|
|
|
* FreeRDP: A Remote Desktop Protocol Implementation
|
2011-08-17 17:57:09 +04:00
|
|
|
* Print Virtual Channel
|
|
|
|
*
|
|
|
|
* Copyright 2010-2011 Vic Lee
|
2015-06-08 18:51:01 +03:00
|
|
|
* Copyright 2015 Thincast Technologies GmbH
|
|
|
|
* Copyright 2015 DI (FH) Martin Haimberger <martin.haimberger@thincast.com>
|
2016-02-26 17:21:19 +03:00
|
|
|
* Copyright 2016 Armin Novak <armin.novak@gmail.com>
|
2016-07-06 22:12:40 +03:00
|
|
|
* Copyright 2016 David PHAM-VAN <d.phamvan@inuvika.com>
|
2011-08-17 17:57:09 +04:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2012-08-15 01:09:01 +04:00
|
|
|
#ifdef HAVE_CONFIG_H
|
2011-08-17 17:57:09 +04:00
|
|
|
#include "config.h"
|
2012-08-15 01:09:01 +04:00
|
|
|
#endif
|
|
|
|
|
2011-08-17 17:57:09 +04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2012-08-15 01:09:01 +04:00
|
|
|
|
2012-10-03 05:52:27 +04:00
|
|
|
#include <winpr/crt.h>
|
2015-07-03 14:26:15 +03:00
|
|
|
#include <winpr/string.h>
|
2012-10-03 05:52:27 +04:00
|
|
|
#include <winpr/synch.h>
|
|
|
|
#include <winpr/thread.h>
|
2014-06-11 23:09:28 +04:00
|
|
|
#include <winpr/stream.h>
|
2012-10-03 05:52:27 +04:00
|
|
|
#include <winpr/interlocked.h>
|
2018-12-12 16:10:46 +03:00
|
|
|
#include <winpr/path.h>
|
2012-10-03 05:52:27 +04:00
|
|
|
|
2012-10-09 05:00:07 +04:00
|
|
|
#include <freerdp/channels/rdpdr.h>
|
2018-12-12 16:10:46 +03:00
|
|
|
#include <freerdp/crypto/crypto.h>
|
2011-08-17 17:57:09 +04:00
|
|
|
|
2016-02-03 12:26:59 +03:00
|
|
|
#include "../printer.h"
|
|
|
|
|
2019-09-04 18:17:47 +03:00
|
|
|
#include <freerdp/client/printer.h>
|
2012-10-02 11:10:21 +04:00
|
|
|
|
2015-06-08 18:51:01 +03:00
|
|
|
#include <freerdp/channels/log.h>
|
|
|
|
|
|
|
|
#define TAG CHANNELS_TAG("printer.client")
|
|
|
|
|
2011-08-17 17:57:09 +04:00
|
|
|
typedef struct _PRINTER_DEVICE PRINTER_DEVICE;
|
|
|
|
struct _PRINTER_DEVICE
|
|
|
|
{
|
|
|
|
DEVICE device;
|
|
|
|
|
|
|
|
rdpPrinter* printer;
|
|
|
|
|
2016-08-10 11:46:01 +03:00
|
|
|
WINPR_PSLIST_HEADER pIrpList;
|
2013-03-22 01:40:04 +04:00
|
|
|
|
|
|
|
HANDLE event;
|
|
|
|
HANDLE stopEvent;
|
|
|
|
|
|
|
|
HANDLE thread;
|
2015-07-15 10:50:35 +03:00
|
|
|
rdpContext* rdpcontext;
|
2018-09-18 15:45:25 +03:00
|
|
|
char port[64];
|
2011-08-17 17:57:09 +04:00
|
|
|
};
|
|
|
|
|
2018-12-12 16:10:46 +03:00
|
|
|
typedef enum
|
|
|
|
{
|
|
|
|
PRN_CONF_PORT = 0,
|
|
|
|
PRN_CONF_PNP = 1,
|
|
|
|
PRN_CONF_DRIVER = 2,
|
|
|
|
PRN_CONF_DATA = 3
|
|
|
|
}
|
|
|
|
prn_conf_t;
|
|
|
|
|
|
|
|
static const char* filemap[] =
|
|
|
|
{
|
|
|
|
"PortDosName",
|
|
|
|
"PnPName",
|
|
|
|
"DriverName",
|
|
|
|
"CachedPrinterConfigData"
|
|
|
|
};
|
|
|
|
|
|
|
|
static char* get_printer_config_path(const rdpSettings* settings, const WCHAR* name, size_t length)
|
|
|
|
{
|
|
|
|
char* dir = GetCombinedPath(settings->ConfigPath, "printers");
|
|
|
|
char* bname = crypto_base64_encode((const BYTE*) name, (int)length);
|
|
|
|
char* config = GetCombinedPath(dir, bname);
|
|
|
|
|
|
|
|
if (config && !PathFileExistsA(config))
|
|
|
|
{
|
|
|
|
if (!PathMakePathA(config, NULL))
|
|
|
|
{
|
|
|
|
free(config);
|
|
|
|
config = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
free(dir);
|
|
|
|
free(bname);
|
|
|
|
return config;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL printer_write_setting(const char* path, prn_conf_t type, const void* data,
|
|
|
|
size_t length)
|
|
|
|
{
|
|
|
|
DWORD written = 0;
|
|
|
|
BOOL rc = FALSE;
|
|
|
|
HANDLE file;
|
|
|
|
size_t b64len;
|
|
|
|
char* base64 = NULL;
|
|
|
|
const char* name = filemap[type];
|
|
|
|
char* abs = GetCombinedPath(path, name);
|
|
|
|
|
|
|
|
if (!abs)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
file = CreateFileA(abs, GENERIC_WRITE, 0,
|
|
|
|
NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
|
|
|
|
free(abs);
|
|
|
|
|
|
|
|
if (file == INVALID_HANDLE_VALUE)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (length > 0)
|
|
|
|
{
|
|
|
|
base64 = crypto_base64_encode(data, length);
|
|
|
|
|
|
|
|
if (!base64)
|
|
|
|
goto fail;
|
|
|
|
|
2019-10-29 12:18:09 +03:00
|
|
|
/* base64 char represents 6bit -> 4*(n/3) is the length which is
|
|
|
|
* always smaller than 2*n */
|
|
|
|
b64len = strnlen(base64, 2 * length);
|
2018-12-12 16:10:46 +03:00
|
|
|
rc = WriteFile(file, base64, b64len, &written, NULL);
|
|
|
|
|
|
|
|
if (b64len != written)
|
|
|
|
rc = FALSE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
rc = TRUE;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
CloseHandle(file);
|
|
|
|
free(base64);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL printer_config_valid(const char* path)
|
|
|
|
{
|
|
|
|
if (!path)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (!PathFileExistsA(path))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2019-02-21 13:42:06 +03:00
|
|
|
static BOOL printer_read_setting(const char* path, prn_conf_t type, void** data, UINT32* length)
|
2018-12-12 16:10:46 +03:00
|
|
|
{
|
|
|
|
DWORD lowSize, highSize;
|
|
|
|
DWORD read = 0;
|
|
|
|
BOOL rc = FALSE;
|
|
|
|
HANDLE file;
|
2019-02-21 13:42:06 +03:00
|
|
|
char* fdata = NULL;
|
2018-12-12 16:10:46 +03:00
|
|
|
const char* name = filemap[type];
|
|
|
|
char* abs = GetCombinedPath(path, name);
|
|
|
|
|
|
|
|
if (!abs)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
file = CreateFileA(abs, GENERIC_READ, 0,
|
|
|
|
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
|
|
|
|
free(abs);
|
|
|
|
|
|
|
|
if (file == INVALID_HANDLE_VALUE)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
lowSize = GetFileSize(file, &highSize);
|
|
|
|
|
|
|
|
if ((lowSize == INVALID_FILE_SIZE) || (highSize != 0))
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
if (lowSize != 0)
|
|
|
|
{
|
|
|
|
fdata = malloc(lowSize);
|
|
|
|
|
|
|
|
if (!fdata)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
rc = ReadFile(file, fdata, lowSize, &read, NULL);
|
|
|
|
|
|
|
|
if (lowSize != read)
|
|
|
|
rc = FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
fail:
|
|
|
|
CloseHandle(file);
|
|
|
|
|
2019-02-21 13:42:06 +03:00
|
|
|
if (rc && (lowSize <= INT_MAX))
|
2018-12-12 16:10:46 +03:00
|
|
|
{
|
|
|
|
int blen = 0;
|
2019-02-21 13:42:06 +03:00
|
|
|
crypto_base64_decode(fdata, (int)lowSize, (BYTE**)data, &blen);
|
2018-12-12 16:10:46 +03:00
|
|
|
|
2019-02-21 13:42:06 +03:00
|
|
|
if (*data && (blen > 0))
|
|
|
|
*length = (UINT32)blen;
|
2018-12-12 16:10:46 +03:00
|
|
|
else
|
|
|
|
{
|
|
|
|
rc = FALSE;
|
|
|
|
*length = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*length = 0;
|
|
|
|
*data = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(fdata);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL printer_save_to_config(const rdpSettings* settings,
|
|
|
|
const char* PortDosName, size_t PortDosNameLen,
|
|
|
|
const WCHAR* PnPName, size_t PnPNameLen,
|
|
|
|
const WCHAR* DriverName, size_t DriverNameLen,
|
|
|
|
const WCHAR* PrinterName, size_t PrintNameLen,
|
|
|
|
const BYTE* CachedPrinterConfigData, size_t CacheFieldsLen)
|
|
|
|
{
|
|
|
|
BOOL rc = FALSE;
|
|
|
|
char* path = get_printer_config_path(settings, PrinterName, PrintNameLen);
|
|
|
|
|
|
|
|
if (!path)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
if (!printer_write_setting(path, PRN_CONF_PORT, PortDosName, PortDosNameLen))
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
if (!printer_write_setting(path, PRN_CONF_PNP, PnPName, PnPNameLen))
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
if (!printer_write_setting(path, PRN_CONF_DRIVER, DriverName, DriverNameLen))
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
if (!printer_write_setting(path, PRN_CONF_DATA, CachedPrinterConfigData, CacheFieldsLen))
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
free(path);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL printer_update_to_config(const rdpSettings* settings, const WCHAR* name, size_t length,
|
|
|
|
const BYTE* data, size_t datalen)
|
|
|
|
{
|
|
|
|
BOOL rc = FALSE;
|
|
|
|
char* path = get_printer_config_path(settings, name, length);
|
|
|
|
rc = printer_write_setting(path, PRN_CONF_DATA, data, datalen);
|
|
|
|
free(path);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL printer_remove_config(const rdpSettings* settings, const WCHAR* name, size_t length)
|
|
|
|
{
|
|
|
|
BOOL rc = FALSE;
|
|
|
|
char* path = get_printer_config_path(settings, name, length);
|
|
|
|
|
|
|
|
if (!printer_config_valid(path))
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
rc = RemoveDirectoryA(path);
|
|
|
|
fail:
|
|
|
|
free(path);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL printer_move_config(const rdpSettings* settings, const WCHAR* oldName, size_t oldLength,
|
|
|
|
const WCHAR* newName, size_t newLength)
|
|
|
|
{
|
|
|
|
BOOL rc = FALSE;
|
|
|
|
char* oldPath = get_printer_config_path(settings, oldName, oldLength);
|
|
|
|
char* newPath = get_printer_config_path(settings, newName, newLength);
|
|
|
|
|
|
|
|
if (printer_config_valid(oldPath))
|
|
|
|
rc = MoveFileA(oldPath, newPath);
|
|
|
|
|
|
|
|
free(oldPath);
|
|
|
|
free(newPath);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL printer_load_from_config(const rdpSettings* settings, rdpPrinter* printer,
|
|
|
|
PRINTER_DEVICE* printer_dev)
|
|
|
|
{
|
|
|
|
BOOL res = FALSE;
|
|
|
|
WCHAR* wname = NULL;
|
|
|
|
size_t wlen;
|
|
|
|
char* path = NULL;
|
|
|
|
int rc;
|
|
|
|
UINT32 flags = 0;
|
2019-02-21 13:42:06 +03:00
|
|
|
void* DriverName = NULL;
|
|
|
|
UINT32 DriverNameLen = 0;
|
|
|
|
void* PnPName = NULL;
|
|
|
|
UINT32 PnPNameLen = 0;
|
|
|
|
void* CachedPrinterConfigData = NULL;
|
|
|
|
UINT32 CachedFieldsLen = 0;
|
|
|
|
UINT32 PrinterNameLen = 0;
|
2018-12-12 16:10:46 +03:00
|
|
|
|
|
|
|
if (!settings || !printer)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
rc = ConvertToUnicode(CP_UTF8, 0, printer->name, -1, &wname, 0);
|
|
|
|
|
|
|
|
if (rc <= 0)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
wlen = _wcslen(wname) + 1;
|
|
|
|
path = get_printer_config_path(settings, wname, wlen * sizeof(WCHAR));
|
|
|
|
PrinterNameLen = (wlen + 1) * sizeof(WCHAR);
|
|
|
|
|
|
|
|
if (!path)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
if (printer->is_default)
|
|
|
|
flags |= RDPDR_PRINTER_ANNOUNCE_FLAG_DEFAULTPRINTER;
|
|
|
|
|
|
|
|
if (!printer_read_setting(path, PRN_CONF_PNP, &PnPName, &PnPNameLen))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!printer_read_setting(path, PRN_CONF_DRIVER, &DriverName, &DriverNameLen))
|
|
|
|
{
|
2019-02-21 13:42:06 +03:00
|
|
|
DriverNameLen = ConvertToUnicode(CP_UTF8, 0, printer->driver, -1, (LPWSTR*)&DriverName,
|
2018-12-12 16:10:46 +03:00
|
|
|
0) * 2 + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!printer_read_setting(path, PRN_CONF_DATA, &CachedPrinterConfigData, &CachedFieldsLen))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Stream_SetPosition(printer_dev->device.data, 0);
|
|
|
|
|
|
|
|
if (!Stream_EnsureRemainingCapacity(printer_dev->device.data, 24))
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
Stream_Write_UINT32(printer_dev->device.data, flags);
|
|
|
|
Stream_Write_UINT32(printer_dev->device.data, 0); /* CodePage, reserved */
|
|
|
|
Stream_Write_UINT32(printer_dev->device.data, PnPNameLen); /* PnPNameLen */
|
|
|
|
Stream_Write_UINT32(printer_dev->device.data, DriverNameLen);
|
|
|
|
Stream_Write_UINT32(printer_dev->device.data, PrinterNameLen);
|
|
|
|
Stream_Write_UINT32(printer_dev->device.data, CachedFieldsLen);
|
|
|
|
|
|
|
|
if (!Stream_EnsureRemainingCapacity(printer_dev->device.data, PnPNameLen))
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
if (PnPNameLen > 0)
|
|
|
|
Stream_Write(printer_dev->device.data, PnPName, PnPNameLen);
|
|
|
|
|
|
|
|
if (!Stream_EnsureRemainingCapacity(printer_dev->device.data, DriverNameLen))
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
Stream_Write(printer_dev->device.data, DriverName, DriverNameLen);
|
|
|
|
|
|
|
|
if (!Stream_EnsureRemainingCapacity(printer_dev->device.data, PrinterNameLen))
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
Stream_Write(printer_dev->device.data, wname, PrinterNameLen);
|
|
|
|
|
|
|
|
if (!Stream_EnsureRemainingCapacity(printer_dev->device.data, CachedFieldsLen))
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
Stream_Write(printer_dev->device.data, CachedPrinterConfigData, CachedFieldsLen);
|
|
|
|
res = TRUE;
|
|
|
|
fail:
|
|
|
|
free(path);
|
|
|
|
free(wname);
|
|
|
|
free(PnPName);
|
|
|
|
free(DriverName);
|
|
|
|
free(CachedPrinterConfigData);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL printer_save_default_config(const rdpSettings* settings, rdpPrinter* printer)
|
|
|
|
{
|
|
|
|
BOOL res = FALSE;
|
|
|
|
WCHAR* wname = NULL;
|
|
|
|
WCHAR* driver = NULL;
|
|
|
|
size_t wlen, dlen;
|
|
|
|
char* path = NULL;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
if (!settings || !printer)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
rc = ConvertToUnicode(CP_UTF8, 0, printer->name, -1, &wname, 0);
|
|
|
|
|
|
|
|
if (rc <= 0)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
rc = ConvertToUnicode(CP_UTF8, 0, printer->driver, -1, &driver, 0);
|
|
|
|
|
|
|
|
if (rc <= 0)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
wlen = _wcslen(wname) + 1;
|
|
|
|
dlen = _wcslen(driver) + 1;
|
|
|
|
path = get_printer_config_path(settings, wname, wlen * sizeof(WCHAR));
|
|
|
|
|
|
|
|
if (!path)
|
|
|
|
goto fail;
|
|
|
|
|
2018-12-12 16:42:50 +03:00
|
|
|
if (dlen > 1)
|
|
|
|
{
|
|
|
|
if (!printer_write_setting(path, PRN_CONF_DRIVER, driver, dlen * sizeof(WCHAR)))
|
|
|
|
goto fail;
|
|
|
|
}
|
2018-12-12 16:10:46 +03:00
|
|
|
|
|
|
|
res = TRUE;
|
|
|
|
fail:
|
|
|
|
free(path);
|
|
|
|
free(wname);
|
|
|
|
free(driver);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2015-08-27 15:25:09 +03:00
|
|
|
/**
|
|
|
|
* Function description
|
|
|
|
*
|
|
|
|
* @return 0 on success, otherwise a Win32 error code
|
|
|
|
*/
|
|
|
|
static UINT printer_process_irp_create(PRINTER_DEVICE* printer_dev, IRP* irp)
|
2011-08-17 17:57:09 +04:00
|
|
|
{
|
|
|
|
rdpPrintJob* printjob = NULL;
|
|
|
|
|
2013-03-22 01:40:04 +04:00
|
|
|
if (printer_dev->printer)
|
2016-10-04 10:00:00 +03:00
|
|
|
printjob = printer_dev->printer->CreatePrintJob(printer_dev->printer,
|
|
|
|
irp->devman->id_sequence++);
|
2011-08-17 17:57:09 +04:00
|
|
|
|
2013-03-22 01:40:04 +04:00
|
|
|
if (printjob)
|
2011-08-17 17:57:09 +04:00
|
|
|
{
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Write_UINT32(irp->output, printjob->id); /* FileId */
|
2011-08-17 17:57:09 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Write_UINT32(irp->output, 0); /* FileId */
|
2011-08-17 17:57:09 +04:00
|
|
|
irp->IoStatus = STATUS_PRINT_QUEUE_FULL;
|
|
|
|
}
|
|
|
|
|
2015-06-08 18:51:01 +03:00
|
|
|
return irp->Complete(irp);
|
2011-08-17 17:57:09 +04:00
|
|
|
}
|
|
|
|
|
2015-08-27 15:25:09 +03:00
|
|
|
/**
|
|
|
|
* Function description
|
|
|
|
*
|
|
|
|
* @return 0 on success, otherwise a Win32 error code
|
|
|
|
*/
|
|
|
|
static UINT printer_process_irp_close(PRINTER_DEVICE* printer_dev, IRP* irp)
|
2011-08-17 17:57:09 +04:00
|
|
|
{
|
|
|
|
rdpPrintJob* printjob = NULL;
|
|
|
|
|
2014-06-11 23:09:28 +04:00
|
|
|
if (printer_dev->printer)
|
2016-10-04 10:00:00 +03:00
|
|
|
printjob = printer_dev->printer->FindPrintJob(printer_dev->printer,
|
|
|
|
irp->FileId);
|
2011-08-17 17:57:09 +04:00
|
|
|
|
2013-03-22 01:40:04 +04:00
|
|
|
if (!printjob)
|
2011-08-17 17:57:09 +04:00
|
|
|
{
|
|
|
|
irp->IoStatus = STATUS_UNSUCCESSFUL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
printjob->Close(printjob);
|
|
|
|
}
|
|
|
|
|
2013-05-09 00:27:21 +04:00
|
|
|
Stream_Zero(irp->output, 4); /* Padding(4) */
|
2015-06-08 18:51:01 +03:00
|
|
|
return irp->Complete(irp);
|
2011-08-17 17:57:09 +04:00
|
|
|
}
|
|
|
|
|
2015-08-27 15:25:09 +03:00
|
|
|
/**
|
|
|
|
* Function description
|
|
|
|
*
|
|
|
|
* @return 0 on success, otherwise a Win32 error code
|
|
|
|
*/
|
|
|
|
static UINT printer_process_irp_write(PRINTER_DEVICE* printer_dev, IRP* irp)
|
2011-08-17 17:57:09 +04:00
|
|
|
{
|
2012-10-09 11:26:39 +04:00
|
|
|
UINT32 Length;
|
|
|
|
UINT64 Offset;
|
2012-10-03 05:52:27 +04:00
|
|
|
rdpPrintJob* printjob = NULL;
|
2015-08-27 15:25:09 +03:00
|
|
|
UINT error = CHANNEL_RC_OK;
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Read_UINT32(irp->input, Length);
|
|
|
|
Stream_Read_UINT64(irp->input, Offset);
|
2013-04-30 06:35:15 +04:00
|
|
|
Stream_Seek(irp->input, 20); /* Padding */
|
2011-08-17 17:57:09 +04:00
|
|
|
|
2013-03-22 01:40:04 +04:00
|
|
|
if (printer_dev->printer)
|
2016-10-04 10:00:00 +03:00
|
|
|
printjob = printer_dev->printer->FindPrintJob(printer_dev->printer,
|
|
|
|
irp->FileId);
|
2011-08-17 17:57:09 +04:00
|
|
|
|
2013-03-22 01:40:04 +04:00
|
|
|
if (!printjob)
|
2011-08-17 17:57:09 +04:00
|
|
|
{
|
|
|
|
irp->IoStatus = STATUS_UNSUCCESSFUL;
|
|
|
|
Length = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-06-08 18:51:01 +03:00
|
|
|
error = printjob->Write(printjob, Stream_Pointer(irp->input), Length);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (error)
|
|
|
|
{
|
2016-12-14 00:47:08 +03:00
|
|
|
WLog_ERR(TAG, "printjob->Write failed with error %"PRIu32"!", error);
|
2015-06-08 18:51:01 +03:00
|
|
|
return error;
|
2011-08-17 17:57:09 +04:00
|
|
|
}
|
|
|
|
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Write_UINT32(irp->output, Length);
|
|
|
|
Stream_Write_UINT8(irp->output, 0); /* Padding */
|
2015-06-08 18:51:01 +03:00
|
|
|
return irp->Complete(irp);
|
2011-08-17 17:57:09 +04:00
|
|
|
}
|
|
|
|
|
2015-08-27 15:25:09 +03:00
|
|
|
/**
|
|
|
|
* Function description
|
|
|
|
*
|
|
|
|
* @return 0 on success, otherwise a Win32 error code
|
|
|
|
*/
|
2016-10-04 10:00:00 +03:00
|
|
|
static UINT printer_process_irp_device_control(PRINTER_DEVICE* printer_dev,
|
|
|
|
IRP* irp)
|
2013-05-20 23:50:22 +04:00
|
|
|
{
|
|
|
|
Stream_Write_UINT32(irp->output, 0); /* OutputBufferLength */
|
2015-06-08 18:51:01 +03:00
|
|
|
return irp->Complete(irp);
|
2013-05-20 23:50:22 +04:00
|
|
|
}
|
|
|
|
|
2015-08-27 15:25:09 +03:00
|
|
|
/**
|
|
|
|
* Function description
|
|
|
|
*
|
|
|
|
* @return 0 on success, otherwise a Win32 error code
|
|
|
|
*/
|
|
|
|
static UINT printer_process_irp(PRINTER_DEVICE* printer_dev, IRP* irp)
|
2011-08-17 17:57:09 +04:00
|
|
|
{
|
2015-08-27 15:25:09 +03:00
|
|
|
UINT error;
|
2016-10-04 10:00:00 +03:00
|
|
|
|
2011-08-17 17:57:09 +04:00
|
|
|
switch (irp->MajorFunction)
|
|
|
|
{
|
|
|
|
case IRP_MJ_CREATE:
|
2015-06-08 18:51:01 +03:00
|
|
|
if ((error = printer_process_irp_create(printer_dev, irp)))
|
|
|
|
{
|
2016-12-14 00:47:08 +03:00
|
|
|
WLog_ERR(TAG, "printer_process_irp_create failed with error %"PRIu32"!", error);
|
2015-06-08 18:51:01 +03:00
|
|
|
return error;
|
|
|
|
}
|
2016-10-04 10:00:00 +03:00
|
|
|
|
2011-08-17 17:57:09 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case IRP_MJ_CLOSE:
|
2015-06-08 18:51:01 +03:00
|
|
|
if ((error = printer_process_irp_close(printer_dev, irp)))
|
|
|
|
{
|
2016-12-14 00:47:08 +03:00
|
|
|
WLog_ERR(TAG, "printer_process_irp_close failed with error %"PRIu32"!", error);
|
2015-06-08 18:51:01 +03:00
|
|
|
return error;
|
|
|
|
}
|
2016-10-04 10:00:00 +03:00
|
|
|
|
2011-08-17 17:57:09 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case IRP_MJ_WRITE:
|
2015-06-08 18:51:01 +03:00
|
|
|
if ((error = printer_process_irp_write(printer_dev, irp)))
|
|
|
|
{
|
2016-12-14 00:47:08 +03:00
|
|
|
WLog_ERR(TAG, "printer_process_irp_write failed with error %"PRIu32"!", error);
|
2015-06-08 18:51:01 +03:00
|
|
|
return error;
|
|
|
|
}
|
2016-10-04 10:00:00 +03:00
|
|
|
|
2011-08-17 17:57:09 +04:00
|
|
|
break;
|
|
|
|
|
2013-05-20 23:50:22 +04:00
|
|
|
case IRP_MJ_DEVICE_CONTROL:
|
2015-06-08 18:51:01 +03:00
|
|
|
if ((error = printer_process_irp_device_control(printer_dev, irp)))
|
|
|
|
{
|
2016-12-14 00:47:08 +03:00
|
|
|
WLog_ERR(TAG, "printer_process_irp_device_control failed with error %"PRIu32"!",
|
2016-10-04 10:00:00 +03:00
|
|
|
error);
|
2015-06-08 18:51:01 +03:00
|
|
|
return error;
|
|
|
|
}
|
2016-10-04 10:00:00 +03:00
|
|
|
|
2013-05-20 23:50:22 +04:00
|
|
|
break;
|
|
|
|
|
2011-08-17 17:57:09 +04:00
|
|
|
default:
|
|
|
|
irp->IoStatus = STATUS_NOT_SUPPORTED;
|
2015-06-08 18:51:01 +03:00
|
|
|
return irp->Complete(irp);
|
2011-08-17 17:57:09 +04:00
|
|
|
break;
|
|
|
|
}
|
2016-10-04 10:00:00 +03:00
|
|
|
|
2015-06-08 18:51:01 +03:00
|
|
|
return CHANNEL_RC_OK;
|
2011-08-17 17:57:09 +04:00
|
|
|
}
|
|
|
|
|
2018-03-07 14:03:10 +03:00
|
|
|
static DWORD WINAPI printer_thread_func(LPVOID arg)
|
2011-08-17 17:57:09 +04:00
|
|
|
{
|
|
|
|
IRP* irp;
|
2013-03-22 01:40:04 +04:00
|
|
|
PRINTER_DEVICE* printer_dev = (PRINTER_DEVICE*) arg;
|
2013-08-08 16:46:21 +04:00
|
|
|
HANDLE obj[] = {printer_dev->event, printer_dev->stopEvent};
|
2015-08-27 15:25:09 +03:00
|
|
|
UINT error = CHANNEL_RC_OK;
|
2016-10-04 10:00:00 +03:00
|
|
|
|
2011-08-17 17:57:09 +04:00
|
|
|
while (1)
|
|
|
|
{
|
2013-08-08 16:46:21 +04:00
|
|
|
DWORD rc = WaitForMultipleObjects(2, obj, FALSE, INFINITE);
|
2016-10-04 10:00:00 +03:00
|
|
|
|
|
|
|
if (rc == WAIT_FAILED)
|
|
|
|
{
|
|
|
|
error = GetLastError();
|
2016-12-14 00:47:08 +03:00
|
|
|
WLog_ERR(TAG, "WaitForMultipleObjects failed with error %"PRIu32"!", error);
|
2016-10-04 10:00:00 +03:00
|
|
|
break;
|
|
|
|
}
|
2013-03-22 01:40:04 +04:00
|
|
|
|
2013-08-08 16:46:21 +04:00
|
|
|
if (rc == WAIT_OBJECT_0 + 1)
|
2011-08-17 17:57:09 +04:00
|
|
|
break;
|
2016-10-04 10:00:00 +03:00
|
|
|
else if (rc != WAIT_OBJECT_0)
|
2013-08-08 16:46:21 +04:00
|
|
|
continue;
|
2011-08-17 17:57:09 +04:00
|
|
|
|
2013-03-22 01:40:04 +04:00
|
|
|
ResetEvent(printer_dev->event);
|
2012-10-03 05:52:27 +04:00
|
|
|
irp = (IRP*) InterlockedPopEntrySList(printer_dev->pIrpList);
|
2011-08-17 17:57:09 +04:00
|
|
|
|
|
|
|
if (irp == NULL)
|
2015-06-08 18:51:01 +03:00
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "InterlockedPopEntrySList failed!");
|
|
|
|
error = ERROR_INTERNAL_ERROR;
|
2011-08-17 17:57:09 +04:00
|
|
|
break;
|
2015-06-08 18:51:01 +03:00
|
|
|
}
|
2011-08-17 17:57:09 +04:00
|
|
|
|
2015-06-08 18:51:01 +03:00
|
|
|
if ((error = printer_process_irp(printer_dev, irp)))
|
|
|
|
{
|
2016-12-14 00:47:08 +03:00
|
|
|
WLog_ERR(TAG, "printer_process_irp failed with error %"PRIu32"!", error);
|
2015-06-08 18:51:01 +03:00
|
|
|
break;
|
|
|
|
}
|
2011-08-17 17:57:09 +04:00
|
|
|
}
|
2016-10-04 10:00:00 +03:00
|
|
|
|
2015-07-15 10:50:35 +03:00
|
|
|
if (error && printer_dev->rdpcontext)
|
2016-10-04 10:00:00 +03:00
|
|
|
setChannelError(printer_dev->rdpcontext, error,
|
|
|
|
"printer_thread_func reported an error");
|
2011-08-17 17:57:09 +04:00
|
|
|
|
2018-03-07 14:03:10 +03:00
|
|
|
ExitThread(error);
|
|
|
|
return error;
|
2011-08-17 17:57:09 +04:00
|
|
|
}
|
|
|
|
|
2015-08-27 15:25:09 +03:00
|
|
|
/**
|
|
|
|
* Function description
|
|
|
|
*
|
|
|
|
* @return 0 on success, otherwise a Win32 error code
|
|
|
|
*/
|
|
|
|
static UINT printer_irp_request(DEVICE* device, IRP* irp)
|
2011-08-17 17:57:09 +04:00
|
|
|
{
|
2012-10-03 05:52:27 +04:00
|
|
|
PRINTER_DEVICE* printer_dev = (PRINTER_DEVICE*) device;
|
|
|
|
InterlockedPushEntrySList(printer_dev->pIrpList, &(irp->ItemEntry));
|
2013-03-22 01:40:04 +04:00
|
|
|
SetEvent(printer_dev->event);
|
2015-06-08 18:51:01 +03:00
|
|
|
return CHANNEL_RC_OK;
|
2011-08-17 17:57:09 +04:00
|
|
|
}
|
|
|
|
|
2018-12-12 12:06:41 +03:00
|
|
|
static UINT printer_custom_component(DEVICE* device, UINT16 component, UINT16 packetId, wStream* s)
|
|
|
|
{
|
2018-12-12 16:10:46 +03:00
|
|
|
UINT32 eventID;
|
|
|
|
PRINTER_DEVICE* printer_dev = (PRINTER_DEVICE*) device;
|
|
|
|
const rdpSettings* settings = printer_dev->rdpcontext->settings;
|
|
|
|
|
|
|
|
if (component != RDPDR_CTYP_PRN)
|
|
|
|
return ERROR_INVALID_DATA;
|
|
|
|
|
|
|
|
if (Stream_GetRemainingLength(s) < 4)
|
|
|
|
return ERROR_INVALID_DATA;
|
|
|
|
|
|
|
|
Stream_Read_UINT32(s, eventID);
|
|
|
|
|
|
|
|
switch (packetId)
|
2018-12-12 12:06:41 +03:00
|
|
|
{
|
2018-12-12 16:10:46 +03:00
|
|
|
case PAKID_PRN_CACHE_DATA:
|
|
|
|
switch (eventID)
|
2018-12-12 12:06:41 +03:00
|
|
|
{
|
2018-12-12 16:10:46 +03:00
|
|
|
case RDPDR_ADD_PRINTER_EVENT:
|
|
|
|
{
|
|
|
|
char PortDosName[8];
|
|
|
|
UINT32 PnPNameLen, DriverNameLen, PrintNameLen, CacheFieldsLen;
|
|
|
|
const WCHAR* PnPName, *DriverName, *PrinterName;
|
|
|
|
const BYTE* CachedPrinterConfigData;
|
|
|
|
|
|
|
|
if (Stream_GetRemainingLength(s) < 24)
|
|
|
|
return ERROR_INVALID_DATA;
|
|
|
|
|
|
|
|
Stream_Read(s, PortDosName, sizeof(PortDosName));
|
|
|
|
Stream_Read_UINT32(s, PnPNameLen);
|
|
|
|
Stream_Read_UINT32(s, DriverNameLen);
|
|
|
|
Stream_Read_UINT32(s, PrintNameLen);
|
|
|
|
Stream_Read_UINT32(s, CacheFieldsLen);
|
|
|
|
|
|
|
|
if (Stream_GetRemainingLength(s) < PnPNameLen)
|
|
|
|
return ERROR_INVALID_DATA;
|
|
|
|
|
|
|
|
PnPName = (const WCHAR*)Stream_Pointer(s);
|
|
|
|
Stream_Seek(s, PnPNameLen);
|
|
|
|
|
|
|
|
if (Stream_GetRemainingLength(s) < DriverNameLen)
|
|
|
|
return ERROR_INVALID_DATA;
|
|
|
|
|
|
|
|
DriverName = (const WCHAR*)Stream_Pointer(s);
|
|
|
|
Stream_Seek(s, DriverNameLen);
|
|
|
|
|
|
|
|
if (Stream_GetRemainingLength(s) < PrintNameLen)
|
|
|
|
return ERROR_INVALID_DATA;
|
|
|
|
|
|
|
|
PrinterName = (const WCHAR*)Stream_Pointer(s);
|
|
|
|
Stream_Seek(s, PrintNameLen);
|
|
|
|
|
|
|
|
if (Stream_GetRemainingLength(s) < CacheFieldsLen)
|
|
|
|
return ERROR_INVALID_DATA;
|
|
|
|
|
|
|
|
CachedPrinterConfigData = Stream_Pointer(s);
|
|
|
|
Stream_Seek(s, CacheFieldsLen);
|
|
|
|
|
|
|
|
if (!printer_save_to_config(settings,
|
|
|
|
PortDosName, sizeof(PortDosName),
|
|
|
|
PnPName, PnPNameLen,
|
|
|
|
DriverName, DriverNameLen,
|
|
|
|
PrinterName, PrintNameLen,
|
|
|
|
CachedPrinterConfigData, CacheFieldsLen))
|
|
|
|
return ERROR_INTERNAL_ERROR;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case RDPDR_UPDATE_PRINTER_EVENT:
|
|
|
|
{
|
|
|
|
UINT32 PrinterNameLen, ConfigDataLen;
|
|
|
|
const WCHAR* PrinterName;
|
|
|
|
const BYTE* ConfigData;
|
|
|
|
|
|
|
|
if (Stream_GetRemainingLength(s) < 8)
|
|
|
|
return ERROR_INVALID_DATA;
|
|
|
|
|
|
|
|
Stream_Read_UINT32(s, PrinterNameLen);
|
|
|
|
Stream_Read_UINT32(s, ConfigDataLen);
|
|
|
|
|
|
|
|
if (Stream_GetRemainingLength(s) < PrinterNameLen)
|
|
|
|
return ERROR_INVALID_DATA;
|
|
|
|
|
|
|
|
PrinterName = (const WCHAR*)Stream_Pointer(s);
|
|
|
|
Stream_Seek(s, PrinterNameLen);
|
|
|
|
|
|
|
|
if (Stream_GetRemainingLength(s) < ConfigDataLen)
|
|
|
|
return ERROR_INVALID_DATA;
|
|
|
|
|
|
|
|
ConfigData = Stream_Pointer(s);
|
|
|
|
Stream_Seek(s, ConfigDataLen);
|
|
|
|
|
|
|
|
if (!printer_update_to_config(settings, PrinterName, PrinterNameLen, ConfigData, ConfigDataLen))
|
|
|
|
return ERROR_INTERNAL_ERROR;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case RDPDR_DELETE_PRINTER_EVENT:
|
2018-12-12 12:06:41 +03:00
|
|
|
{
|
2018-12-12 16:10:46 +03:00
|
|
|
UINT32 PrinterNameLen;
|
|
|
|
const WCHAR* PrinterName;
|
2018-12-12 12:06:41 +03:00
|
|
|
|
|
|
|
if (Stream_GetRemainingLength(s) < 4)
|
|
|
|
return ERROR_INVALID_DATA;
|
|
|
|
|
2018-12-12 16:10:46 +03:00
|
|
|
Stream_Read_UINT32(s, PrinterNameLen);
|
|
|
|
|
|
|
|
if (Stream_GetRemainingLength(s) < PrinterNameLen)
|
|
|
|
return ERROR_INVALID_DATA;
|
|
|
|
|
|
|
|
PrinterName = (const WCHAR*)Stream_Pointer(s);
|
|
|
|
Stream_Seek(s, PrinterNameLen);
|
|
|
|
printer_remove_config(settings, PrinterName, PrinterNameLen);
|
2018-12-12 12:06:41 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2018-12-12 16:10:46 +03:00
|
|
|
case RDPDR_RENAME_PRINTER_EVENT:
|
|
|
|
{
|
|
|
|
UINT32 OldPrinterNameLen, NewPrinterNameLen;
|
|
|
|
const WCHAR* OldPrinterName;
|
|
|
|
const WCHAR* NewPrinterName;
|
|
|
|
|
|
|
|
if (Stream_GetRemainingLength(s) < 8)
|
|
|
|
return ERROR_INVALID_DATA;
|
|
|
|
|
|
|
|
Stream_Read_UINT32(s, OldPrinterNameLen);
|
|
|
|
Stream_Read_UINT32(s, NewPrinterNameLen);
|
|
|
|
|
|
|
|
if (Stream_GetRemainingLength(s) < OldPrinterNameLen)
|
|
|
|
return ERROR_INVALID_DATA;
|
|
|
|
|
|
|
|
OldPrinterName = (const WCHAR*)Stream_Pointer(s);
|
|
|
|
Stream_Seek(s, OldPrinterNameLen);
|
|
|
|
|
|
|
|
if (Stream_GetRemainingLength(s) < NewPrinterNameLen)
|
|
|
|
return ERROR_INVALID_DATA;
|
|
|
|
|
|
|
|
NewPrinterName = (const WCHAR*)Stream_Pointer(s);
|
|
|
|
Stream_Seek(s, NewPrinterNameLen);
|
|
|
|
|
|
|
|
if (!printer_move_config(settings, OldPrinterName, OldPrinterNameLen, NewPrinterName,
|
|
|
|
NewPrinterNameLen))
|
|
|
|
return ERROR_INTERNAL_ERROR;
|
|
|
|
}
|
2018-12-12 12:06:41 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2019-01-29 16:21:58 +03:00
|
|
|
WLog_ERR(TAG, "Unknown cache data eventID: 0x%08"PRIX32"", eventID);
|
2018-12-12 12:06:41 +03:00
|
|
|
return ERROR_INVALID_DATA;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
2018-12-12 16:10:46 +03:00
|
|
|
case PAKID_PRN_USING_XPS:
|
|
|
|
{
|
|
|
|
UINT32 flags;
|
|
|
|
|
|
|
|
if (Stream_GetRemainingLength(s) < 4)
|
|
|
|
return ERROR_INVALID_DATA;
|
|
|
|
|
|
|
|
Stream_Read_UINT32(s, flags);
|
|
|
|
WLog_ERR(TAG,
|
|
|
|
"Ignoring unhandled message PAKID_PRN_USING_XPS [printerID=%08"PRIx32", flags=%08"PRIx32"]",
|
|
|
|
eventID, flags);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2018-12-12 12:06:41 +03:00
|
|
|
default:
|
2018-12-12 16:10:46 +03:00
|
|
|
WLog_ERR(TAG, "Unknown printing component packetID: 0x%04"PRIX16"", packetId);
|
2018-12-12 12:06:41 +03:00
|
|
|
return ERROR_INVALID_DATA;
|
|
|
|
}
|
|
|
|
|
|
|
|
return CHANNEL_RC_OK;
|
|
|
|
}
|
|
|
|
|
2015-08-27 15:25:09 +03:00
|
|
|
/**
|
|
|
|
* Function description
|
|
|
|
*
|
|
|
|
* @return 0 on success, otherwise a Win32 error code
|
|
|
|
*/
|
|
|
|
static UINT printer_free(DEVICE* device)
|
2011-08-17 17:57:09 +04:00
|
|
|
{
|
|
|
|
IRP* irp;
|
2012-10-03 05:52:27 +04:00
|
|
|
PRINTER_DEVICE* printer_dev = (PRINTER_DEVICE*) device;
|
2016-10-04 10:00:00 +03:00
|
|
|
UINT error;
|
2013-03-22 01:40:04 +04:00
|
|
|
SetEvent(printer_dev->stopEvent);
|
2016-10-04 10:00:00 +03:00
|
|
|
|
2015-07-30 16:49:21 +03:00
|
|
|
if (WaitForSingleObject(printer_dev->thread, INFINITE) == WAIT_FAILED)
|
2016-10-04 10:00:00 +03:00
|
|
|
{
|
|
|
|
error = GetLastError();
|
2016-12-14 00:47:08 +03:00
|
|
|
WLog_ERR(TAG, "WaitForSingleObject failed with error %"PRIu32"", error);
|
2019-01-29 19:16:46 +03:00
|
|
|
|
|
|
|
/* The analyzer is confused by this premature return value.
|
|
|
|
* Since this case can not be handled gracefully silence the
|
|
|
|
* analyzer here. */
|
|
|
|
#ifndef __clang_analyzer__
|
2016-10-04 10:00:00 +03:00
|
|
|
return error;
|
2019-01-29 19:16:46 +03:00
|
|
|
#endif
|
2016-10-04 10:00:00 +03:00
|
|
|
}
|
2012-10-03 05:52:27 +04:00
|
|
|
|
|
|
|
while ((irp = (IRP*) InterlockedPopEntrySList(printer_dev->pIrpList)) != NULL)
|
2011-08-17 17:57:09 +04:00
|
|
|
irp->Discard(irp);
|
2012-10-03 05:52:27 +04:00
|
|
|
|
2013-09-05 15:38:56 +04:00
|
|
|
CloseHandle(printer_dev->thread);
|
|
|
|
CloseHandle(printer_dev->stopEvent);
|
|
|
|
CloseHandle(printer_dev->event);
|
2012-10-03 05:52:27 +04:00
|
|
|
_aligned_free(printer_dev->pIrpList);
|
2011-08-17 17:57:09 +04:00
|
|
|
|
|
|
|
if (printer_dev->printer)
|
2019-09-04 18:17:47 +03:00
|
|
|
printer_dev->printer->ReleaseRef(printer_dev->printer);
|
2011-08-17 17:57:09 +04:00
|
|
|
|
2016-07-06 22:12:40 +03:00
|
|
|
Stream_Free(printer_dev->device.data, TRUE);
|
2012-10-09 07:21:26 +04:00
|
|
|
free(printer_dev);
|
2016-10-04 10:00:00 +03:00
|
|
|
return CHANNEL_RC_OK;
|
2011-08-17 17:57:09 +04:00
|
|
|
}
|
|
|
|
|
2015-08-27 15:25:09 +03:00
|
|
|
/**
|
|
|
|
* Function description
|
|
|
|
*
|
|
|
|
* @return 0 on success, otherwise a Win32 error code
|
|
|
|
*/
|
2019-09-04 18:17:47 +03:00
|
|
|
static UINT printer_register(PDEVICE_SERVICE_ENTRY_POINTS pEntryPoints,
|
2016-10-04 10:00:00 +03:00
|
|
|
rdpPrinter* printer)
|
2011-08-17 17:57:09 +04:00
|
|
|
{
|
2012-09-24 02:41:07 +04:00
|
|
|
PRINTER_DEVICE* printer_dev;
|
2019-01-29 18:53:30 +03:00
|
|
|
UINT error = ERROR_INTERNAL_ERROR;
|
2015-06-08 18:51:01 +03:00
|
|
|
printer_dev = (PRINTER_DEVICE*) calloc(1, sizeof(PRINTER_DEVICE));
|
2016-10-04 10:00:00 +03:00
|
|
|
|
2015-06-08 18:51:01 +03:00
|
|
|
if (!printer_dev)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "calloc failed!");
|
|
|
|
return CHANNEL_RC_NO_MEMORY;
|
|
|
|
}
|
2011-08-17 17:57:09 +04:00
|
|
|
|
2018-12-12 16:10:46 +03:00
|
|
|
printer_dev->device.data = Stream_New(NULL, 1024);
|
|
|
|
|
|
|
|
if (!printer_dev->device.data)
|
|
|
|
goto error_out;
|
|
|
|
|
2019-10-02 17:01:36 +03:00
|
|
|
sprintf_s(printer_dev->port, sizeof(printer_dev->port), "PRN%"PRIdz, printer->id);
|
2011-08-17 17:57:09 +04:00
|
|
|
printer_dev->device.type = RDPDR_DTYP_PRINT;
|
2018-09-18 15:45:25 +03:00
|
|
|
printer_dev->device.name = printer_dev->port;
|
2011-08-17 17:57:09 +04:00
|
|
|
printer_dev->device.IRPRequest = printer_irp_request;
|
2018-12-12 12:06:41 +03:00
|
|
|
printer_dev->device.CustomComponentRequest = printer_custom_component;
|
2011-08-17 17:57:09 +04:00
|
|
|
printer_dev->device.Free = printer_free;
|
2015-07-15 10:50:35 +03:00
|
|
|
printer_dev->rdpcontext = pEntryPoints->rdpcontext;
|
2011-08-17 17:57:09 +04:00
|
|
|
printer_dev->printer = printer;
|
2016-10-04 10:00:00 +03:00
|
|
|
printer_dev->pIrpList = (WINPR_PSLIST_HEADER) _aligned_malloc(sizeof(
|
|
|
|
WINPR_SLIST_HEADER), MEMORY_ALLOCATION_ALIGNMENT);
|
2011-08-17 17:57:09 +04:00
|
|
|
|
2015-06-08 18:51:01 +03:00
|
|
|
if (!printer_dev->pIrpList)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "_aligned_malloc failed!");
|
|
|
|
error = CHANNEL_RC_NO_MEMORY;
|
|
|
|
goto error_out;
|
|
|
|
}
|
2016-10-04 10:00:00 +03:00
|
|
|
|
2018-12-12 16:10:46 +03:00
|
|
|
if (!printer_load_from_config(pEntryPoints->rdpcontext->settings, printer, printer_dev))
|
|
|
|
goto error_out;
|
|
|
|
|
2012-10-03 05:52:27 +04:00
|
|
|
InitializeSListHead(printer_dev->pIrpList);
|
|
|
|
|
2015-06-08 18:51:01 +03:00
|
|
|
if (!(printer_dev->event = CreateEvent(NULL, TRUE, FALSE, NULL)))
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "CreateEvent failed!");
|
|
|
|
error = ERROR_INTERNAL_ERROR;
|
|
|
|
goto error_out;
|
|
|
|
}
|
2016-10-04 10:00:00 +03:00
|
|
|
|
2015-06-08 18:51:01 +03:00
|
|
|
if (!(printer_dev->stopEvent = CreateEvent(NULL, TRUE, FALSE, NULL)))
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "CreateEvent failed!");
|
|
|
|
error = ERROR_INTERNAL_ERROR;
|
|
|
|
goto error_out;
|
|
|
|
}
|
2011-08-17 17:57:09 +04:00
|
|
|
|
2016-10-04 10:00:00 +03:00
|
|
|
if ((error = pEntryPoints->RegisterDevice(pEntryPoints->devman,
|
|
|
|
(DEVICE*) printer_dev)))
|
2015-06-08 18:51:01 +03:00
|
|
|
{
|
2016-12-14 00:47:08 +03:00
|
|
|
WLog_ERR(TAG, "RegisterDevice failed with error %"PRIu32"!", error);
|
2015-06-08 18:51:01 +03:00
|
|
|
goto error_out;
|
|
|
|
}
|
|
|
|
|
2018-09-18 15:45:25 +03:00
|
|
|
if (!(printer_dev->thread = CreateThread(NULL, 0, printer_thread_func, (void*) printer_dev, 0,
|
|
|
|
NULL)))
|
2015-06-08 18:51:01 +03:00
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "CreateThread failed!");
|
|
|
|
error = ERROR_INTERNAL_ERROR;
|
|
|
|
goto error_out;
|
|
|
|
}
|
2011-08-17 17:57:09 +04:00
|
|
|
|
2019-09-04 18:17:47 +03:00
|
|
|
printer->AddRef(printer);
|
2015-06-08 18:51:01 +03:00
|
|
|
return CHANNEL_RC_OK;
|
|
|
|
error_out:
|
2018-09-18 15:45:25 +03:00
|
|
|
printer_free(&printer_dev->device);
|
2015-06-08 18:51:01 +03:00
|
|
|
return error;
|
2011-08-17 17:57:09 +04:00
|
|
|
}
|
|
|
|
|
2019-09-04 18:17:47 +03:00
|
|
|
static rdpPrinterDriver* printer_load_backend(const char* backend)
|
|
|
|
{
|
|
|
|
typedef rdpPrinterDriver* (*backend_load_t)(void);
|
|
|
|
|
|
|
|
backend_load_t entry = (backend_load_t)freerdp_load_channel_addin_entry("printer", backend, NULL, 0);
|
|
|
|
if (!entry)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return entry();
|
|
|
|
}
|
2012-10-14 10:38:58 +04:00
|
|
|
|
2015-08-27 15:25:09 +03:00
|
|
|
/**
|
|
|
|
* Function description
|
|
|
|
*
|
|
|
|
* @return 0 on success, otherwise a Win32 error code
|
|
|
|
*/
|
2019-09-04 18:17:47 +03:00
|
|
|
UINT
|
|
|
|
#ifdef BUILTIN_CHANNELS
|
|
|
|
printer_DeviceServiceEntry
|
|
|
|
#else
|
|
|
|
FREERDP_API DeviceServiceEntry
|
|
|
|
#endif
|
|
|
|
(PDEVICE_SERVICE_ENTRY_POINTS pEntryPoints)
|
2011-08-17 17:57:09 +04:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
char* name;
|
|
|
|
char* driver_name;
|
2019-09-22 17:22:41 +03:00
|
|
|
BOOL default_backend = TRUE;
|
2019-09-04 18:17:47 +03:00
|
|
|
RDPDR_PRINTER* device = NULL;
|
2012-10-03 05:52:27 +04:00
|
|
|
rdpPrinterDriver* driver = NULL;
|
2019-09-22 17:22:41 +03:00
|
|
|
UINT error = CHANNEL_RC_OK;
|
2019-09-04 18:17:47 +03:00
|
|
|
|
|
|
|
if (!pEntryPoints || !pEntryPoints->device)
|
|
|
|
return ERROR_INVALID_PARAMETER;
|
|
|
|
|
|
|
|
device = (RDPDR_PRINTER*) pEntryPoints->device;
|
|
|
|
name = device->Name;
|
|
|
|
driver_name = device->DriverName;
|
|
|
|
|
|
|
|
/* Secondary argument is one of the following:
|
|
|
|
*
|
|
|
|
* <driver_name> ... name of a printer driver
|
|
|
|
* <driver_name>:<backend_name> ... name of a printer driver and local printer backend to use
|
|
|
|
*/
|
|
|
|
if (driver_name)
|
|
|
|
{
|
|
|
|
char* sep = strstr(driver_name, ":");
|
|
|
|
if (sep)
|
|
|
|
{
|
|
|
|
const char* backend = sep + 1;
|
|
|
|
*sep = '\0';
|
|
|
|
driver = printer_load_backend(backend);
|
2019-09-22 17:22:41 +03:00
|
|
|
default_backend = FALSE;
|
2019-09-04 18:17:47 +03:00
|
|
|
}
|
|
|
|
}
|
2019-09-22 17:22:41 +03:00
|
|
|
|
|
|
|
if (!driver && default_backend)
|
|
|
|
{
|
|
|
|
const char* backend =
|
|
|
|
#if defined(WITH_CUPS)
|
|
|
|
"cups"
|
|
|
|
#elif defined(_WIN32)
|
|
|
|
"win"
|
|
|
|
#else
|
|
|
|
""
|
|
|
|
#endif
|
|
|
|
;
|
|
|
|
|
|
|
|
driver = printer_load_backend(backend);
|
|
|
|
}
|
2012-10-03 05:52:27 +04:00
|
|
|
|
2014-06-11 23:09:28 +04:00
|
|
|
if (!driver)
|
2011-08-17 17:57:09 +04:00
|
|
|
{
|
2015-06-08 18:51:01 +03:00
|
|
|
WLog_ERR(TAG, "Could not get a printer driver!");
|
|
|
|
return CHANNEL_RC_INITIALIZATION_ERROR;
|
2011-08-17 17:57:09 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (name && name[0])
|
|
|
|
{
|
2019-09-04 18:17:47 +03:00
|
|
|
rdpPrinter* printer = driver->GetPrinter(driver, name, driver_name);
|
2012-10-03 05:52:27 +04:00
|
|
|
|
2013-03-22 01:40:04 +04:00
|
|
|
if (!printer)
|
2015-06-08 18:51:01 +03:00
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "Could not get printer %s!", name);
|
2019-09-04 18:17:47 +03:00
|
|
|
error = CHANNEL_RC_INITIALIZATION_ERROR;
|
|
|
|
goto fail;
|
2015-06-08 18:51:01 +03:00
|
|
|
}
|
2012-10-03 05:52:27 +04:00
|
|
|
|
2018-12-12 16:10:46 +03:00
|
|
|
if (!printer_save_default_config(pEntryPoints->rdpcontext->settings, printer))
|
2019-09-04 18:17:47 +03:00
|
|
|
{
|
|
|
|
error = CHANNEL_RC_INITIALIZATION_ERROR;
|
|
|
|
printer->ReleaseRef(printer);
|
|
|
|
goto fail;
|
|
|
|
}
|
2018-12-12 16:10:46 +03:00
|
|
|
|
2015-06-08 18:51:01 +03:00
|
|
|
if ((error = printer_register(pEntryPoints, printer)))
|
|
|
|
{
|
2016-12-14 00:47:08 +03:00
|
|
|
WLog_ERR(TAG, "printer_register failed with error %"PRIu32"!", error);
|
2019-09-04 18:17:47 +03:00
|
|
|
printer->ReleaseRef(printer);
|
|
|
|
goto fail;
|
2015-06-08 18:51:01 +03:00
|
|
|
}
|
2011-08-17 17:57:09 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-09-04 18:17:47 +03:00
|
|
|
rdpPrinter** printers = driver->EnumPrinters(driver);
|
|
|
|
rdpPrinter** current = printers;
|
2012-10-03 05:52:27 +04:00
|
|
|
|
2019-09-04 18:17:47 +03:00
|
|
|
for (i = 0; current[i]; i++)
|
2011-08-17 17:57:09 +04:00
|
|
|
{
|
2019-09-04 18:17:47 +03:00
|
|
|
rdpPrinter* printer = current[i];
|
2016-10-04 10:00:00 +03:00
|
|
|
|
2015-06-08 18:51:01 +03:00
|
|
|
if ((error = printer_register(pEntryPoints, printer)))
|
|
|
|
{
|
2016-12-14 00:47:08 +03:00
|
|
|
WLog_ERR(TAG, "printer_register failed with error %"PRIu32"!", error);
|
2019-09-04 18:17:47 +03:00
|
|
|
break;
|
2015-06-08 18:51:01 +03:00
|
|
|
}
|
2011-08-17 17:57:09 +04:00
|
|
|
}
|
2012-10-03 05:52:27 +04:00
|
|
|
|
2019-09-04 18:17:47 +03:00
|
|
|
driver->ReleaseEnumPrinters(printers);
|
2011-08-17 17:57:09 +04:00
|
|
|
}
|
|
|
|
|
2019-09-04 18:17:47 +03:00
|
|
|
fail:
|
|
|
|
driver->ReleaseRef(driver);
|
|
|
|
|
|
|
|
return error;
|
2011-08-17 17:57:09 +04:00
|
|
|
}
|