xref: /reactos/win32ss/printing/base/spoolsv/ports.c (revision 8a978a17)
1 /*
2  * PROJECT:     ReactOS Print Spooler Service
3  * LICENSE:     GPL-2.0+ (https://spdx.org/licenses/GPL-2.0+)
4  * PURPOSE:     Functions related to Ports
5  * COPYRIGHT:   Copyright 2015-2018 Colin Finck (colin@reactos.org)
6  */
7 
8 #include "precomp.h"
9 #include <marshalling/ports.h>
10 
11 DWORD
12 _RpcAddPort(WINSPOOL_HANDLE pName, ULONG_PTR hWnd, WCHAR* pMonitorName)
13 {
14     DWORD dwErrorCode;
15 
16     FIXME("AddPort(%S, %p, %s)\n", pName, hWnd, debugstr_w(pMonitorName));
17 
18     dwErrorCode = RpcImpersonateClient(NULL);
19     if (dwErrorCode != ERROR_SUCCESS)
20     {
21         ERR("RpcImpersonateClient failed with error %lu!\n", dwErrorCode);
22         return dwErrorCode;
23     }
24 
25     if (!AddPortW( pName, (HWND)hWnd, pMonitorName ))
26         dwErrorCode = GetLastError();
27 
28     RpcRevertToSelf();
29     return dwErrorCode;
30 }
31 
32 DWORD
33 _RpcAddPortEx(WINSPOOL_HANDLE pName, WINSPOOL_PORT_CONTAINER* pPortContainer, WINSPOOL_PORT_VAR_CONTAINER* pPortVarContainer, WCHAR* pMonitorName)
34 {
35     DWORD dwErrorCode, Level = pPortContainer->Level;
36     WINSPOOL_PORT_INFO_FF PortInfoFF;
37     PBYTE lpBuffer;
38 
39     FIXME("AddPortEx(%S, %lu, %s)\n", pName, Level, debugstr_w(pMonitorName));
40 
41     switch (Level)
42     {
43         case 1:
44            lpBuffer = (PBYTE)pPortContainer->PortInfo.pPortInfo1;
45            break;
46 
47         case 0xFFFFFFFF:
48            PortInfoFF.pPortName = pPortContainer->PortInfo.pPortInfoFF->pPortName;
49            PortInfoFF.cbMonitorData = pPortVarContainer->cbMonitorData;
50            PortInfoFF.pMonitorData =  pPortVarContainer->pMonitorData;
51            lpBuffer = (PBYTE)&PortInfoFF;
52            break;
53 
54         default:
55            ERR("Level = %d, unsupported!\n", Level);
56            return ERROR_INVALID_LEVEL;
57     }
58 
59     dwErrorCode = RpcImpersonateClient(NULL);
60     if (dwErrorCode != ERROR_SUCCESS)
61     {
62         ERR("RpcImpersonateClient failed with error %lu!\n", dwErrorCode);
63         return dwErrorCode;
64     }
65 
66     if (!AddPortExW(pName, Level, lpBuffer, pMonitorName ))
67         dwErrorCode = GetLastError();
68 
69     RpcRevertToSelf();
70     return dwErrorCode;
71 }
72 
73 DWORD
74 _RpcConfigurePort(WINSPOOL_HANDLE pName, ULONG_PTR hWnd, WCHAR* pPortName)
75 {
76     DWORD dwErrorCode;
77 
78     dwErrorCode = RpcImpersonateClient(NULL);
79     if (dwErrorCode != ERROR_SUCCESS)
80     {
81         ERR("RpcImpersonateClient failed with error %lu!\n", dwErrorCode);
82         return dwErrorCode;
83     }
84 
85     if (!ConfigurePortW( pName, (HWND)hWnd, pPortName ))
86         dwErrorCode = GetLastError();
87 
88     RpcRevertToSelf();
89     return dwErrorCode;
90 }
91 
92 DWORD
93 _RpcDeletePort(WINSPOOL_HANDLE pName, ULONG_PTR hWnd, WCHAR* pPortName)
94 {
95     DWORD dwErrorCode;
96 
97     dwErrorCode = RpcImpersonateClient(NULL);
98     if (dwErrorCode != ERROR_SUCCESS)
99     {
100         ERR("RpcImpersonateClient failed with error %lu!\n", dwErrorCode);
101         return dwErrorCode;
102     }
103 
104     if (!DeletePortW( pName, (HWND)hWnd, pPortName ))
105         dwErrorCode = GetLastError();
106 
107     RpcRevertToSelf();
108     return dwErrorCode;
109 }
110 
111 DWORD
112 _RpcEnumPorts(WINSPOOL_HANDLE pName, DWORD Level, BYTE* pPort, DWORD cbBuf, DWORD* pcbNeeded, DWORD* pcReturned)
113 {
114     DWORD dwErrorCode;
115     PBYTE pPortAligned;
116 
117     dwErrorCode = RpcImpersonateClient(NULL);
118     if (dwErrorCode != ERROR_SUCCESS)
119     {
120         ERR("RpcImpersonateClient failed with error %lu!\n", dwErrorCode);
121         return dwErrorCode;
122     }
123 
124     pPortAligned = AlignRpcPtr(pPort, &cbBuf);
125 
126     if (EnumPortsW(pName, Level, pPortAligned, cbBuf, pcbNeeded, pcReturned))
127     {
128         // Replace absolute pointer addresses in the output by relative offsets.
129         ASSERT(Level >= 1 && Level <= 2);
130         MarshallDownStructuresArray(pPortAligned, *pcReturned, pPortInfoMarshalling[Level]->pInfo, pPortInfoMarshalling[Level]->cbStructureSize, TRUE);
131     }
132     else
133     {
134         dwErrorCode = GetLastError();
135     }
136 
137     RpcRevertToSelf();
138     UndoAlignRpcPtr(pPort, pPortAligned, cbBuf, pcbNeeded);
139 
140     return dwErrorCode;
141 }
142 
143 DWORD
144 _RpcSetPort(WINSPOOL_HANDLE pName, WCHAR* pPortName, WINSPOOL_PORT_CONTAINER* pPortContainer)
145 {
146     DWORD dwErrorCode;
147 
148     dwErrorCode = RpcImpersonateClient(NULL);
149     if (dwErrorCode != ERROR_SUCCESS)
150     {
151         ERR("RpcImpersonateClient failed with error %lu!\n", dwErrorCode);
152         return dwErrorCode;
153     }
154 
155     if (!SetPortW(pName, pPortName, pPortContainer->Level, (PBYTE)pPortContainer->PortInfo.pPortInfo3))
156         dwErrorCode = GetLastError();
157 
158     RpcRevertToSelf();
159     return dwErrorCode;
160 }
161