FreeRDP
printer_main.c
1 
24 #include <freerdp/config.h>
25 
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 
30 #include <winpr/crt.h>
31 #include <winpr/assert.h>
32 #include <winpr/string.h>
33 #include <winpr/synch.h>
34 #include <winpr/thread.h>
35 #include <winpr/stream.h>
36 #include <winpr/interlocked.h>
37 #include <winpr/path.h>
38 
39 #include <freerdp/channels/rdpdr.h>
40 #include <freerdp/crypto/crypto.h>
41 #include <freerdp/freerdp.h>
42 
43 #include "../printer.h"
44 
45 #include <freerdp/client/printer.h>
46 
47 #include <freerdp/channels/log.h>
48 
49 #define TAG CHANNELS_TAG("printer.client")
50 
51 typedef struct
52 {
53  DEVICE device;
54 
55  rdpPrinter* printer;
56 
57  WINPR_PSLIST_HEADER pIrpList;
58 
59  HANDLE event;
60  HANDLE stopEvent;
61 
62  HANDLE thread;
63  rdpContext* rdpcontext;
64  char port[64];
65  BOOL async;
66 } PRINTER_DEVICE;
67 
68 typedef enum
69 {
70  PRN_CONF_PORT = 0,
71  PRN_CONF_PNP = 1,
72  PRN_CONF_DRIVER = 2,
73  PRN_CONF_DATA = 3
74 } prn_conf_t;
75 
76 static const char* filemap[] = { "PortDosName", "PnPName", "DriverName",
77  "CachedPrinterConfigData" };
78 
79 static char* get_printer_config_path(const rdpSettings* settings, const WCHAR* name, size_t length)
80 {
81  const char* path = freerdp_settings_get_string(settings, FreeRDP_ConfigPath);
82  char* dir = GetCombinedPath(path, "printers");
83  char* bname = crypto_base64_encode((const BYTE*)name, length);
84  char* config = GetCombinedPath(dir, bname);
85 
86  if (config && !winpr_PathFileExists(config))
87  {
88  if (!winpr_PathMakePath(config, NULL))
89  {
90  free(config);
91  config = NULL;
92  }
93  }
94 
95  free(dir);
96  free(bname);
97  return config;
98 }
99 
100 static BOOL printer_write_setting(const char* path, prn_conf_t type, const void* data,
101  size_t length)
102 {
103  DWORD written = 0;
104  BOOL rc = FALSE;
105  HANDLE file = NULL;
106  char* base64 = NULL;
107  const char* name = filemap[type];
108  char* abs = GetCombinedPath(path, name);
109 
110  if (!abs || (length > INT32_MAX))
111  {
112  free(abs);
113  return FALSE;
114  }
115 
116  file = CreateFileA(abs, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
117  free(abs);
118 
119  if (file == INVALID_HANDLE_VALUE)
120  return FALSE;
121 
122  if (length > 0)
123  {
124  base64 = crypto_base64_encode(data, length);
125 
126  if (!base64)
127  goto fail;
128 
129  /* base64 char represents 6bit -> 4*(n/3) is the length which is
130  * always smaller than 2*n */
131  const size_t b64len = strnlen(base64, 2 * length);
132  rc = WriteFile(file, base64, (UINT32)b64len, &written, NULL);
133 
134  if (b64len != written)
135  rc = FALSE;
136  }
137  else
138  rc = TRUE;
139 
140 fail:
141  (void)CloseHandle(file);
142  free(base64);
143  return rc;
144 }
145 
146 static BOOL printer_config_valid(const char* path)
147 {
148  if (!path)
149  return FALSE;
150 
151  if (!winpr_PathFileExists(path))
152  return FALSE;
153 
154  return TRUE;
155 }
156 
157 static BOOL printer_read_setting(const char* path, prn_conf_t type, void** data, UINT32* length)
158 {
159  DWORD lowSize = 0;
160  DWORD highSize = 0;
161  DWORD read = 0;
162  BOOL rc = FALSE;
163  HANDLE file = NULL;
164  char* fdata = NULL;
165  const char* name = filemap[type];
166  char* abs = GetCombinedPath(path, name);
167 
168  if (!abs)
169  return FALSE;
170 
171  file = CreateFileA(abs, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
172  free(abs);
173 
174  if (file == INVALID_HANDLE_VALUE)
175  return FALSE;
176 
177  lowSize = GetFileSize(file, &highSize);
178 
179  if ((lowSize == INVALID_FILE_SIZE) || (highSize != 0))
180  goto fail;
181 
182  if (lowSize != 0)
183  {
184  fdata = malloc(lowSize);
185 
186  if (!fdata)
187  goto fail;
188 
189  rc = ReadFile(file, fdata, lowSize, &read, NULL);
190 
191  if (lowSize != read)
192  rc = FALSE;
193  }
194 
195 fail:
196  (void)CloseHandle(file);
197 
198  if (rc && (lowSize <= INT_MAX))
199  {
200  size_t blen = 0;
201  crypto_base64_decode(fdata, lowSize, (BYTE**)data, &blen);
202 
203  if (*data && (blen > 0))
204  *length = (UINT32)blen;
205  else
206  {
207  rc = FALSE;
208  *length = 0;
209  }
210  }
211  else
212  {
213  *length = 0;
214  *data = NULL;
215  }
216 
217  free(fdata);
218  return rc;
219 }
220 
221 static BOOL printer_save_to_config(const rdpSettings* settings, const char* PortDosName,
222  size_t PortDosNameLen, const WCHAR* PnPName, size_t PnPNameLen,
223  const WCHAR* DriverName, size_t DriverNameLen,
224  const WCHAR* PrinterName, size_t PrintNameLen,
225  const BYTE* CachedPrinterConfigData, size_t CacheFieldsLen)
226 {
227  BOOL rc = FALSE;
228  char* path = get_printer_config_path(settings, PrinterName, PrintNameLen);
229 
230  if (!path)
231  goto fail;
232 
233  if (!printer_write_setting(path, PRN_CONF_PORT, PortDosName, PortDosNameLen))
234  goto fail;
235 
236  if (!printer_write_setting(path, PRN_CONF_PNP, PnPName, PnPNameLen))
237  goto fail;
238 
239  if (!printer_write_setting(path, PRN_CONF_DRIVER, DriverName, DriverNameLen))
240  goto fail;
241 
242  if (!printer_write_setting(path, PRN_CONF_DATA, CachedPrinterConfigData, CacheFieldsLen))
243  goto fail;
244 
245 fail:
246  free(path);
247  return rc;
248 }
249 
250 static BOOL printer_update_to_config(const rdpSettings* settings, const WCHAR* name, size_t length,
251  const BYTE* data, size_t datalen)
252 {
253  BOOL rc = FALSE;
254  char* path = get_printer_config_path(settings, name, length);
255  rc = printer_write_setting(path, PRN_CONF_DATA, data, datalen);
256  free(path);
257  return rc;
258 }
259 
260 static BOOL printer_remove_config(const rdpSettings* settings, const WCHAR* name, size_t length)
261 {
262  BOOL rc = FALSE;
263  char* path = get_printer_config_path(settings, name, length);
264 
265  if (!printer_config_valid(path))
266  goto fail;
267 
268  rc = winpr_RemoveDirectory(path);
269 fail:
270  free(path);
271  return rc;
272 }
273 
274 static BOOL printer_move_config(const rdpSettings* settings, const WCHAR* oldName, size_t oldLength,
275  const WCHAR* newName, size_t newLength)
276 {
277  BOOL rc = FALSE;
278  char* oldPath = get_printer_config_path(settings, oldName, oldLength);
279  char* newPath = get_printer_config_path(settings, newName, newLength);
280 
281  if (printer_config_valid(oldPath))
282  rc = winpr_MoveFile(oldPath, newPath);
283 
284  free(oldPath);
285  free(newPath);
286  return rc;
287 }
288 
289 static BOOL printer_load_from_config(const rdpSettings* settings, rdpPrinter* printer,
290  PRINTER_DEVICE* printer_dev)
291 {
292  BOOL res = FALSE;
293  WCHAR* wname = NULL;
294  size_t wlen = 0;
295  char* path = NULL;
296  UINT32 flags = 0;
297  void* DriverName = NULL;
298  UINT32 DriverNameLen = 0;
299  void* PnPName = NULL;
300  UINT32 PnPNameLen = 0;
301  void* CachedPrinterConfigData = NULL;
302  UINT32 CachedFieldsLen = 0;
303  UINT32 PrinterNameLen = 0;
304 
305  if (!settings || !printer || !printer->name)
306  return FALSE;
307 
308  wname = ConvertUtf8ToWCharAlloc(printer->name, &wlen);
309 
310  if (!wname)
311  goto fail;
312 
313  wlen++;
314  path = get_printer_config_path(settings, wname, wlen * sizeof(WCHAR));
315  {
316  const size_t plen = wlen * sizeof(WCHAR);
317  if (plen > UINT32_MAX)
318  goto fail;
319  PrinterNameLen = (UINT32)plen;
320  }
321 
322  if (!path)
323  goto fail;
324 
325  if (printer->is_default)
326  flags |= RDPDR_PRINTER_ANNOUNCE_FLAG_DEFAULTPRINTER;
327 
328  if (!printer_read_setting(path, PRN_CONF_PNP, &PnPName, &PnPNameLen))
329  {
330  }
331 
332  if (!printer_read_setting(path, PRN_CONF_DRIVER, &DriverName, &DriverNameLen))
333  {
334  size_t len = 0;
335  DriverName = ConvertUtf8ToWCharAlloc(printer->driver, &len);
336  if (!DriverName)
337  goto fail;
338  const size_t dlen = (len + 1) * sizeof(WCHAR);
339  if (dlen > UINT32_MAX)
340  goto fail;
341  DriverNameLen = (UINT32)dlen;
342  }
343 
344  if (!printer_read_setting(path, PRN_CONF_DATA, &CachedPrinterConfigData, &CachedFieldsLen))
345  {
346  }
347 
348  Stream_SetPosition(printer_dev->device.data, 0);
349 
350  if (!Stream_EnsureRemainingCapacity(printer_dev->device.data, 24))
351  goto fail;
352 
353  Stream_Write_UINT32(printer_dev->device.data, flags);
354  Stream_Write_UINT32(printer_dev->device.data, 0); /* CodePage, reserved */
355  Stream_Write_UINT32(printer_dev->device.data, PnPNameLen); /* PnPNameLen */
356  Stream_Write_UINT32(printer_dev->device.data, DriverNameLen);
357  Stream_Write_UINT32(printer_dev->device.data, PrinterNameLen);
358  Stream_Write_UINT32(printer_dev->device.data, CachedFieldsLen);
359 
360  if (!Stream_EnsureRemainingCapacity(printer_dev->device.data, PnPNameLen))
361  goto fail;
362 
363  if (PnPNameLen > 0)
364  Stream_Write(printer_dev->device.data, PnPName, PnPNameLen);
365 
366  if (!Stream_EnsureRemainingCapacity(printer_dev->device.data, DriverNameLen))
367  goto fail;
368 
369  Stream_Write(printer_dev->device.data, DriverName, DriverNameLen);
370 
371  if (!Stream_EnsureRemainingCapacity(printer_dev->device.data, PrinterNameLen))
372  goto fail;
373 
374  union
375  {
376  char c[2];
377  WCHAR w;
378  } backslash;
379  backslash.c[0] = '\\';
380  backslash.c[1] = '\0';
381 
382  for (WCHAR* wptr = wname; (wptr = _wcschr(wptr, backslash.w));)
383  *wptr = L'_';
384  Stream_Write(printer_dev->device.data, wname, PrinterNameLen);
385 
386  if (!Stream_EnsureRemainingCapacity(printer_dev->device.data, CachedFieldsLen))
387  goto fail;
388 
389  Stream_Write(printer_dev->device.data, CachedPrinterConfigData, CachedFieldsLen);
390  res = TRUE;
391 fail:
392  free(path);
393  free(wname);
394  free(PnPName);
395  free(DriverName);
396  free(CachedPrinterConfigData);
397  return res;
398 }
399 
400 static BOOL printer_save_default_config(const rdpSettings* settings, rdpPrinter* printer)
401 {
402  BOOL res = FALSE;
403  WCHAR* wname = NULL;
404  WCHAR* driver = NULL;
405  size_t wlen = 0;
406  size_t dlen = 0;
407  char* path = NULL;
408 
409  if (!settings || !printer || !printer->name || !printer->driver)
410  return FALSE;
411 
412  wname = ConvertUtf8ToWCharAlloc(printer->name, NULL);
413 
414  if (!wname)
415  goto fail;
416 
417  driver = ConvertUtf8ToWCharAlloc(printer->driver, NULL);
418 
419  if (!driver)
420  goto fail;
421 
422  wlen = _wcslen(wname) + 1;
423  dlen = _wcslen(driver) + 1;
424  path = get_printer_config_path(settings, wname, wlen * sizeof(WCHAR));
425 
426  if (!path)
427  goto fail;
428 
429  if (dlen > 1)
430  {
431  if (!printer_write_setting(path, PRN_CONF_DRIVER, driver, dlen * sizeof(WCHAR)))
432  goto fail;
433  }
434 
435  res = TRUE;
436 fail:
437  free(path);
438  free(wname);
439  free(driver);
440  return res;
441 }
442 
448 static UINT printer_process_irp_create(PRINTER_DEVICE* printer_dev, IRP* irp)
449 {
450  rdpPrintJob* printjob = NULL;
451 
452  WINPR_ASSERT(printer_dev);
453  WINPR_ASSERT(irp);
454 
455  if (printer_dev->printer)
456  {
457  WINPR_ASSERT(printer_dev->printer->CreatePrintJob);
458  printjob =
459  printer_dev->printer->CreatePrintJob(printer_dev->printer, irp->devman->id_sequence++);
460  }
461 
462  if (printjob)
463  {
464  Stream_Write_UINT32(irp->output, printjob->id); /* FileId */
465  }
466  else
467  {
468  Stream_Write_UINT32(irp->output, 0); /* FileId */
469  irp->IoStatus = STATUS_PRINT_QUEUE_FULL;
470  }
471 
472  return irp->Complete(irp);
473 }
474 
480 static UINT printer_process_irp_close(PRINTER_DEVICE* printer_dev, IRP* irp)
481 {
482  rdpPrintJob* printjob = NULL;
483 
484  WINPR_ASSERT(printer_dev);
485  WINPR_ASSERT(irp);
486 
487  if (printer_dev->printer)
488  {
489  WINPR_ASSERT(printer_dev->printer->FindPrintJob);
490  printjob = printer_dev->printer->FindPrintJob(printer_dev->printer, irp->FileId);
491  }
492 
493  if (!printjob)
494  {
495  irp->IoStatus = STATUS_UNSUCCESSFUL;
496  }
497  else
498  {
499  printjob->Close(printjob);
500  }
501 
502  Stream_Zero(irp->output, 4); /* Padding(4) */
503  return irp->Complete(irp);
504 }
505 
511 static UINT printer_process_irp_write(PRINTER_DEVICE* printer_dev, IRP* irp)
512 {
513  UINT32 Length = 0;
514  UINT64 Offset = 0;
515  rdpPrintJob* printjob = NULL;
516  UINT error = CHANNEL_RC_OK;
517 
518  WINPR_ASSERT(printer_dev);
519  WINPR_ASSERT(irp);
520 
521  if (!Stream_CheckAndLogRequiredLength(TAG, irp->input, 32))
522  return ERROR_INVALID_DATA;
523  Stream_Read_UINT32(irp->input, Length);
524  Stream_Read_UINT64(irp->input, Offset);
525  (void)Offset; /* [MS-RDPEPC] 2.2.2.9 Server Printer Write Request (DR_PRN_WRITE_REQ)
526  * reserved for future use, ignore */
527  Stream_Seek(irp->input, 20); /* Padding */
528  const void* ptr = Stream_ConstPointer(irp->input);
529  if (!Stream_SafeSeek(irp->input, Length))
530  return ERROR_INVALID_DATA;
531  if (printer_dev->printer)
532  {
533  WINPR_ASSERT(printer_dev->printer->FindPrintJob);
534  printjob = printer_dev->printer->FindPrintJob(printer_dev->printer, irp->FileId);
535  }
536 
537  if (!printjob)
538  {
539  irp->IoStatus = STATUS_UNSUCCESSFUL;
540  Length = 0;
541  }
542  else
543  {
544  error = printjob->Write(printjob, ptr, Length);
545  }
546 
547  if (error)
548  {
549  WLog_ERR(TAG, "printjob->Write failed with error %" PRIu32 "!", error);
550  return error;
551  }
552 
553  Stream_Write_UINT32(irp->output, Length);
554  Stream_Write_UINT8(irp->output, 0); /* Padding */
555 
556  WINPR_ASSERT(irp->Complete);
557  return irp->Complete(irp);
558 }
559 
565 static UINT printer_process_irp_device_control(PRINTER_DEVICE* printer_dev, IRP* irp)
566 {
567  WINPR_ASSERT(printer_dev);
568  WINPR_ASSERT(irp);
569 
570  Stream_Write_UINT32(irp->output, 0); /* OutputBufferLength */
571 
572  WINPR_ASSERT(irp->Complete);
573  return irp->Complete(irp);
574 }
575 
581 static UINT printer_process_irp(PRINTER_DEVICE* printer_dev, IRP* irp)
582 {
583  UINT error = 0;
584 
585  WINPR_ASSERT(printer_dev);
586  WINPR_ASSERT(irp);
587 
588  switch (irp->MajorFunction)
589  {
590  case IRP_MJ_CREATE:
591  if ((error = printer_process_irp_create(printer_dev, irp)))
592  {
593  WLog_ERR(TAG, "printer_process_irp_create failed with error %" PRIu32 "!", error);
594  return error;
595  }
596 
597  break;
598 
599  case IRP_MJ_CLOSE:
600  if ((error = printer_process_irp_close(printer_dev, irp)))
601  {
602  WLog_ERR(TAG, "printer_process_irp_close failed with error %" PRIu32 "!", error);
603  return error;
604  }
605 
606  break;
607 
608  case IRP_MJ_WRITE:
609  if ((error = printer_process_irp_write(printer_dev, irp)))
610  {
611  WLog_ERR(TAG, "printer_process_irp_write failed with error %" PRIu32 "!", error);
612  return error;
613  }
614 
615  break;
616 
617  case IRP_MJ_DEVICE_CONTROL:
618  if ((error = printer_process_irp_device_control(printer_dev, irp)))
619  {
620  WLog_ERR(TAG, "printer_process_irp_device_control failed with error %" PRIu32 "!",
621  error);
622  return error;
623  }
624 
625  break;
626 
627  default:
628  irp->IoStatus = STATUS_NOT_SUPPORTED;
629  WINPR_ASSERT(irp->Complete);
630  return irp->Complete(irp);
631  }
632 
633  return CHANNEL_RC_OK;
634 }
635 
636 static DWORD WINAPI printer_thread_func(LPVOID arg)
637 {
638  IRP* irp = NULL;
639  PRINTER_DEVICE* printer_dev = (PRINTER_DEVICE*)arg;
640  UINT error = CHANNEL_RC_OK;
641 
642  WINPR_ASSERT(printer_dev);
643 
644  while (1)
645  {
646  HANDLE obj[] = { printer_dev->event, printer_dev->stopEvent };
647  DWORD rc = WaitForMultipleObjects(ARRAYSIZE(obj), obj, FALSE, INFINITE);
648 
649  if (rc == WAIT_FAILED)
650  {
651  error = GetLastError();
652  WLog_ERR(TAG, "WaitForMultipleObjects failed with error %" PRIu32 "!", error);
653  break;
654  }
655 
656  if (rc == WAIT_OBJECT_0 + 1)
657  break;
658  else if (rc != WAIT_OBJECT_0)
659  continue;
660 
661  (void)ResetEvent(printer_dev->event);
662  irp = (IRP*)InterlockedPopEntrySList(printer_dev->pIrpList);
663 
664  if (irp == NULL)
665  {
666  WLog_ERR(TAG, "InterlockedPopEntrySList failed!");
667  error = ERROR_INTERNAL_ERROR;
668  break;
669  }
670 
671  if ((error = printer_process_irp(printer_dev, irp)))
672  {
673  WLog_ERR(TAG, "printer_process_irp failed with error %" PRIu32 "!", error);
674  break;
675  }
676  }
677 
678  if (error && printer_dev->rdpcontext)
679  setChannelError(printer_dev->rdpcontext, error, "printer_thread_func reported an error");
680 
681  ExitThread(error);
682  return error;
683 }
684 
690 static UINT printer_irp_request(DEVICE* device, IRP* irp)
691 {
692  PRINTER_DEVICE* printer_dev = (PRINTER_DEVICE*)device;
693 
694  WINPR_ASSERT(printer_dev);
695  WINPR_ASSERT(irp);
696 
697  if (printer_dev->async)
698  {
699  InterlockedPushEntrySList(printer_dev->pIrpList, &(irp->ItemEntry));
700  (void)SetEvent(printer_dev->event);
701  }
702  else
703  {
704  UINT error = printer_process_irp(printer_dev, irp);
705  if (error)
706  {
707  WLog_ERR(TAG, "printer_process_irp failed with error %" PRIu32 "!", error);
708  return error;
709  }
710  }
711 
712  return CHANNEL_RC_OK;
713 }
714 
715 static UINT printer_custom_component(DEVICE* device, UINT16 component, UINT16 packetId, wStream* s)
716 {
717  UINT32 eventID = 0;
718  PRINTER_DEVICE* printer_dev = (PRINTER_DEVICE*)device;
719 
720  WINPR_ASSERT(printer_dev);
721  WINPR_ASSERT(printer_dev->rdpcontext);
722 
723  const rdpSettings* settings = printer_dev->rdpcontext->settings;
724  WINPR_ASSERT(settings);
725 
726  if (component != RDPDR_CTYP_PRN)
727  return ERROR_INVALID_DATA;
728 
729  if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
730  return ERROR_INVALID_DATA;
731 
732  Stream_Read_UINT32(s, eventID);
733 
734  switch (packetId)
735  {
736  case PAKID_PRN_CACHE_DATA:
737  switch (eventID)
738  {
739  case RDPDR_ADD_PRINTER_EVENT:
740  {
741  char PortDosName[8];
742  UINT32 PnPNameLen = 0;
743  UINT32 DriverNameLen = 0;
744  UINT32 PrintNameLen = 0;
745  UINT32 CacheFieldsLen = 0;
746  const WCHAR* PnPName = NULL;
747  const WCHAR* DriverName = NULL;
748  const WCHAR* PrinterName = NULL;
749  const BYTE* CachedPrinterConfigData = NULL;
750 
751  if (!Stream_CheckAndLogRequiredLength(TAG, s, 24))
752  return ERROR_INVALID_DATA;
753 
754  Stream_Read(s, PortDosName, sizeof(PortDosName));
755  Stream_Read_UINT32(s, PnPNameLen);
756  Stream_Read_UINT32(s, DriverNameLen);
757  Stream_Read_UINT32(s, PrintNameLen);
758  Stream_Read_UINT32(s, CacheFieldsLen);
759 
760  if (!Stream_CheckAndLogRequiredLength(TAG, s, PnPNameLen))
761  return ERROR_INVALID_DATA;
762 
763  PnPName = Stream_ConstPointer(s);
764  Stream_Seek(s, PnPNameLen);
765 
766  if (!Stream_CheckAndLogRequiredLength(TAG, s, DriverNameLen))
767  return ERROR_INVALID_DATA;
768 
769  DriverName = Stream_ConstPointer(s);
770  Stream_Seek(s, DriverNameLen);
771 
772  if (!Stream_CheckAndLogRequiredLength(TAG, s, PrintNameLen))
773  return ERROR_INVALID_DATA;
774 
775  PrinterName = Stream_ConstPointer(s);
776  Stream_Seek(s, PrintNameLen);
777 
778  if (!Stream_CheckAndLogRequiredLength(TAG, s, CacheFieldsLen))
779  return ERROR_INVALID_DATA;
780 
781  CachedPrinterConfigData = Stream_ConstPointer(s);
782  Stream_Seek(s, CacheFieldsLen);
783 
784  if (!printer_save_to_config(settings, PortDosName, sizeof(PortDosName), PnPName,
785  PnPNameLen, DriverName, DriverNameLen, PrinterName,
786  PrintNameLen, CachedPrinterConfigData,
787  CacheFieldsLen))
788  return ERROR_INTERNAL_ERROR;
789  }
790  break;
791 
792  case RDPDR_UPDATE_PRINTER_EVENT:
793  {
794  UINT32 PrinterNameLen = 0;
795  UINT32 ConfigDataLen = 0;
796  const WCHAR* PrinterName = NULL;
797  const BYTE* ConfigData = NULL;
798 
799  if (!Stream_CheckAndLogRequiredLength(TAG, s, 8))
800  return ERROR_INVALID_DATA;
801 
802  Stream_Read_UINT32(s, PrinterNameLen);
803  Stream_Read_UINT32(s, ConfigDataLen);
804 
805  if (!Stream_CheckAndLogRequiredLength(TAG, s, PrinterNameLen))
806  return ERROR_INVALID_DATA;
807 
808  PrinterName = Stream_ConstPointer(s);
809  Stream_Seek(s, PrinterNameLen);
810 
811  if (!Stream_CheckAndLogRequiredLength(TAG, s, ConfigDataLen))
812  return ERROR_INVALID_DATA;
813 
814  ConfigData = Stream_ConstPointer(s);
815  Stream_Seek(s, ConfigDataLen);
816 
817  if (!printer_update_to_config(settings, PrinterName, PrinterNameLen, ConfigData,
818  ConfigDataLen))
819  return ERROR_INTERNAL_ERROR;
820  }
821  break;
822 
823  case RDPDR_DELETE_PRINTER_EVENT:
824  {
825  UINT32 PrinterNameLen = 0;
826  const WCHAR* PrinterName = NULL;
827 
828  if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
829  return ERROR_INVALID_DATA;
830 
831  Stream_Read_UINT32(s, PrinterNameLen);
832 
833  if (!Stream_CheckAndLogRequiredLength(TAG, s, PrinterNameLen))
834  return ERROR_INVALID_DATA;
835 
836  PrinterName = Stream_ConstPointer(s);
837  Stream_Seek(s, PrinterNameLen);
838  printer_remove_config(settings, PrinterName, PrinterNameLen);
839  }
840  break;
841 
842  case RDPDR_RENAME_PRINTER_EVENT:
843  {
844  UINT32 OldPrinterNameLen = 0;
845  UINT32 NewPrinterNameLen = 0;
846  const WCHAR* OldPrinterName = NULL;
847  const WCHAR* NewPrinterName = NULL;
848 
849  if (!Stream_CheckAndLogRequiredLength(TAG, s, 8))
850  return ERROR_INVALID_DATA;
851 
852  Stream_Read_UINT32(s, OldPrinterNameLen);
853  Stream_Read_UINT32(s, NewPrinterNameLen);
854 
855  if (!Stream_CheckAndLogRequiredLength(TAG, s, OldPrinterNameLen))
856  return ERROR_INVALID_DATA;
857 
858  OldPrinterName = Stream_ConstPointer(s);
859  Stream_Seek(s, OldPrinterNameLen);
860 
861  if (!Stream_CheckAndLogRequiredLength(TAG, s, NewPrinterNameLen))
862  return ERROR_INVALID_DATA;
863 
864  NewPrinterName = Stream_ConstPointer(s);
865  Stream_Seek(s, NewPrinterNameLen);
866 
867  if (!printer_move_config(settings, OldPrinterName, OldPrinterNameLen,
868  NewPrinterName, NewPrinterNameLen))
869  return ERROR_INTERNAL_ERROR;
870  }
871  break;
872 
873  default:
874  WLog_ERR(TAG, "Unknown cache data eventID: 0x%08" PRIX32 "", eventID);
875  return ERROR_INVALID_DATA;
876  }
877 
878  break;
879 
880  case PAKID_PRN_USING_XPS:
881  {
882  UINT32 flags = 0;
883 
884  if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
885  return ERROR_INVALID_DATA;
886 
887  Stream_Read_UINT32(s, flags);
888  WLog_ERR(TAG,
889  "Ignoring unhandled message PAKID_PRN_USING_XPS [printerID=%08" PRIx32
890  ", flags=%08" PRIx32 "]",
891  eventID, flags);
892  }
893  break;
894 
895  default:
896  WLog_ERR(TAG, "Unknown printing component packetID: 0x%04" PRIX16 "", packetId);
897  return ERROR_INVALID_DATA;
898  }
899 
900  return CHANNEL_RC_OK;
901 }
902 
908 static UINT printer_free(DEVICE* device)
909 {
910  IRP* irp = NULL;
911  PRINTER_DEVICE* printer_dev = (PRINTER_DEVICE*)device;
912  UINT error = 0;
913 
914  WINPR_ASSERT(printer_dev);
915 
916  if (printer_dev->async)
917  {
918  (void)SetEvent(printer_dev->stopEvent);
919 
920  if (WaitForSingleObject(printer_dev->thread, INFINITE) == WAIT_FAILED)
921  {
922  error = GetLastError();
923  WLog_ERR(TAG, "WaitForSingleObject failed with error %" PRIu32 "", error);
924 
925  /* The analyzer is confused by this premature return value.
926  * Since this case can not be handled gracefully silence the
927  * analyzer here. */
928 #ifndef __clang_analyzer__
929  return error;
930 #endif
931  }
932 
933  while ((irp = (IRP*)InterlockedPopEntrySList(printer_dev->pIrpList)) != NULL)
934  {
935  WINPR_ASSERT(irp->Discard);
936  irp->Discard(irp);
937  }
938 
939  (void)CloseHandle(printer_dev->thread);
940  (void)CloseHandle(printer_dev->stopEvent);
941  (void)CloseHandle(printer_dev->event);
942  winpr_aligned_free(printer_dev->pIrpList);
943  }
944 
945  if (printer_dev->printer)
946  {
947  WINPR_ASSERT(printer_dev->printer->ReleaseRef);
948  printer_dev->printer->ReleaseRef(printer_dev->printer);
949  }
950 
951  Stream_Free(printer_dev->device.data, TRUE);
952  free(printer_dev);
953  return CHANNEL_RC_OK;
954 }
955 
961 static UINT printer_register(PDEVICE_SERVICE_ENTRY_POINTS pEntryPoints, rdpPrinter* printer)
962 {
963  PRINTER_DEVICE* printer_dev = NULL;
964  UINT error = ERROR_INTERNAL_ERROR;
965 
966  WINPR_ASSERT(pEntryPoints);
967  WINPR_ASSERT(printer);
968 
969  printer_dev = (PRINTER_DEVICE*)calloc(1, sizeof(PRINTER_DEVICE));
970 
971  if (!printer_dev)
972  {
973  WLog_ERR(TAG, "calloc failed!");
974  return CHANNEL_RC_NO_MEMORY;
975  }
976 
977  printer_dev->device.data = Stream_New(NULL, 1024);
978 
979  if (!printer_dev->device.data)
980  goto error_out;
981 
982  (void)sprintf_s(printer_dev->port, sizeof(printer_dev->port), "PRN%" PRIuz, printer->id);
983  printer_dev->device.type = RDPDR_DTYP_PRINT;
984  printer_dev->device.name = printer_dev->port;
985  printer_dev->device.IRPRequest = printer_irp_request;
986  printer_dev->device.CustomComponentRequest = printer_custom_component;
987  printer_dev->device.Free = printer_free;
988  printer_dev->rdpcontext = pEntryPoints->rdpcontext;
989  printer_dev->printer = printer;
990 
991  if (!freerdp_settings_get_bool(pEntryPoints->rdpcontext->settings,
992  FreeRDP_SynchronousStaticChannels))
993  printer_dev->async = TRUE;
994 
995  if (!printer_load_from_config(pEntryPoints->rdpcontext->settings, printer, printer_dev))
996  goto error_out;
997 
998  if (printer_dev->async)
999  {
1000  printer_dev->pIrpList = (WINPR_PSLIST_HEADER)winpr_aligned_malloc(
1001  sizeof(WINPR_SLIST_HEADER), MEMORY_ALLOCATION_ALIGNMENT);
1002 
1003  if (!printer_dev->pIrpList)
1004  {
1005  WLog_ERR(TAG, "_aligned_malloc failed!");
1006  error = CHANNEL_RC_NO_MEMORY;
1007  goto error_out;
1008  }
1009 
1010  InitializeSListHead(printer_dev->pIrpList);
1011 
1012  printer_dev->event = CreateEvent(NULL, TRUE, FALSE, NULL);
1013  if (!printer_dev->event)
1014  {
1015  WLog_ERR(TAG, "CreateEvent failed!");
1016  error = ERROR_INTERNAL_ERROR;
1017  goto error_out;
1018  }
1019 
1020  printer_dev->stopEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
1021  if (!printer_dev->stopEvent)
1022  {
1023  WLog_ERR(TAG, "CreateEvent failed!");
1024  error = ERROR_INTERNAL_ERROR;
1025  goto error_out;
1026  }
1027  }
1028 
1029  error = pEntryPoints->RegisterDevice(pEntryPoints->devman, &printer_dev->device);
1030  if (error)
1031  {
1032  WLog_ERR(TAG, "RegisterDevice failed with error %" PRIu32 "!", error);
1033  goto error_out;
1034  }
1035 
1036  if (printer_dev->async)
1037  {
1038  printer_dev->thread =
1039  CreateThread(NULL, 0, printer_thread_func, (void*)printer_dev, 0, NULL);
1040  if (!printer_dev->thread)
1041  {
1042  WLog_ERR(TAG, "CreateThread failed!");
1043  error = ERROR_INTERNAL_ERROR;
1044  goto error_out;
1045  }
1046  }
1047 
1048  WINPR_ASSERT(printer->AddRef);
1049  printer->AddRef(printer);
1050  return CHANNEL_RC_OK;
1051 error_out:
1052  printer_free(&printer_dev->device);
1053  return error;
1054 }
1055 
1056 static rdpPrinterDriver* printer_load_backend(const char* backend)
1057 {
1058  typedef UINT(VCAPITYPE * backend_load_t)(rdpPrinterDriver**);
1059  PVIRTUALCHANNELENTRY entry = freerdp_load_channel_addin_entry("printer", backend, NULL, 0);
1060  backend_load_t func = WINPR_FUNC_PTR_CAST(entry, backend_load_t);
1061  if (!func)
1062  return NULL;
1063 
1064  rdpPrinterDriver* printer = NULL;
1065  const UINT rc = func(&printer);
1066  if (rc != CHANNEL_RC_OK)
1067  return NULL;
1068 
1069  return printer;
1070 }
1071 
1077 FREERDP_ENTRY_POINT(
1078  UINT VCAPITYPE printer_DeviceServiceEntry(PDEVICE_SERVICE_ENTRY_POINTS pEntryPoints))
1079 {
1080  char* name = NULL;
1081  char* driver_name = NULL;
1082  BOOL default_backend = TRUE;
1083  RDPDR_PRINTER* device = NULL;
1084  rdpPrinterDriver* driver = NULL;
1085  UINT error = CHANNEL_RC_OK;
1086 
1087  if (!pEntryPoints || !pEntryPoints->device)
1088  return ERROR_INVALID_PARAMETER;
1089 
1090  device = (RDPDR_PRINTER*)pEntryPoints->device;
1091  name = device->device.Name;
1092  driver_name = _strdup(device->DriverName);
1093 
1094  /* Secondary argument is one of the following:
1095  *
1096  * <driver_name> ... name of a printer driver
1097  * <driver_name>:<backend_name> ... name of a printer driver and local printer backend to use
1098  */
1099  if (driver_name)
1100  {
1101  char* sep = strstr(driver_name, ":");
1102  if (sep)
1103  {
1104  const char* backend = sep + 1;
1105  *sep = '\0';
1106  driver = printer_load_backend(backend);
1107  default_backend = FALSE;
1108  }
1109  }
1110 
1111  if (!driver && default_backend)
1112  {
1113  const char* backend =
1114 #if defined(WITH_CUPS)
1115  "cups"
1116 #elif defined(_WIN32)
1117  "win"
1118 #else
1119  ""
1120 #endif
1121  ;
1122 
1123  driver = printer_load_backend(backend);
1124  }
1125 
1126  if (!driver)
1127  {
1128  WLog_ERR(TAG, "Could not get a printer driver!");
1129  error = CHANNEL_RC_INITIALIZATION_ERROR;
1130  goto fail;
1131  }
1132 
1133  if (name && name[0])
1134  {
1135  WINPR_ASSERT(driver->GetPrinter);
1136  rdpPrinter* printer = driver->GetPrinter(driver, name, driver_name, device->IsDefault);
1137 
1138  if (!printer)
1139  {
1140  WLog_ERR(TAG, "Could not get printer %s!", name);
1141  error = CHANNEL_RC_INITIALIZATION_ERROR;
1142  goto fail;
1143  }
1144 
1145  WINPR_ASSERT(printer->ReleaseRef);
1146  if (!printer_save_default_config(pEntryPoints->rdpcontext->settings, printer))
1147  {
1148  error = CHANNEL_RC_INITIALIZATION_ERROR;
1149  printer->ReleaseRef(printer);
1150  goto fail;
1151  }
1152 
1153  error = printer_register(pEntryPoints, printer);
1154  printer->ReleaseRef(printer);
1155  if (error)
1156  {
1157  WLog_ERR(TAG, "printer_register failed with error %" PRIu32 "!", error);
1158  goto fail;
1159  }
1160  }
1161  else
1162  {
1163  WINPR_ASSERT(driver->EnumPrinters);
1164  rdpPrinter** printers = driver->EnumPrinters(driver);
1165  if (printers)
1166  {
1167  for (rdpPrinter** current = printers; *current; ++current)
1168  {
1169  error = printer_register(pEntryPoints, *current);
1170  if (error)
1171  {
1172  WLog_ERR(TAG, "printer_register failed with error %" PRIu32 "!", error);
1173  break;
1174  }
1175  }
1176  }
1177  else
1178  {
1179  WLog_ERR(TAG, "Failed to enumerate printers!");
1180  error = CHANNEL_RC_INITIALIZATION_ERROR;
1181  }
1182 
1183  WINPR_ASSERT(driver->ReleaseEnumPrinters);
1184  driver->ReleaseEnumPrinters(printers);
1185  }
1186 
1187 fail:
1188  free(driver_name);
1189  if (driver)
1190  {
1191  WINPR_ASSERT(driver->ReleaseRef);
1192  driver->ReleaseRef(driver);
1193  }
1194 
1195  return error;
1196 }
FREERDP_API BOOL freerdp_settings_get_bool(const rdpSettings *settings, FreeRDP_Settings_Keys_Bool id)
Returns a boolean settings value.
FREERDP_API const char * freerdp_settings_get_string(const rdpSettings *settings, FreeRDP_Settings_Keys_String id)
Returns a immutable string settings value.