xref: /reactos/win32ss/printing/base/spoolsv/jobs.c (revision 40462c92)
1 /*
2  * PROJECT:     ReactOS Print Spooler Service
3  * LICENSE:     GPL-2.0+ (https://spdx.org/licenses/GPL-2.0+)
4  * PURPOSE:     Functions for managing print jobs
5  * COPYRIGHT:   Copyright 2015-2018 Colin Finck (colin@reactos.org)
6  */
7 
8 #include "precomp.h"
9 #include <marshalling/jobs.h>
10 
11 DWORD
12 _RpcAddJob(WINSPOOL_PRINTER_HANDLE hPrinter, DWORD Level, BYTE* pAddJob, DWORD cbBuf, DWORD* pcbNeeded)
13 {
14     DWORD dwErrorCode;
15     PBYTE pAddJobAligned;
16 
17     dwErrorCode = RpcImpersonateClient(NULL);
18     if (dwErrorCode != ERROR_SUCCESS)
19     {
20         ERR("RpcImpersonateClient failed with error %lu!\n", dwErrorCode);
21         return dwErrorCode;
22     }
23 
24     pAddJobAligned = AlignRpcPtr(pAddJob, &cbBuf);
25 
26     if (AddJobW(hPrinter, Level, pAddJobAligned, cbBuf, pcbNeeded))
27     {
28         // Replace absolute pointer addresses in the output by relative offsets.
29         MarshallDownStructure(pAddJobAligned, AddJobInfo1Marshalling.pInfo, AddJobInfo1Marshalling.cbStructureSize, TRUE);
30     }
31     else
32     {
33         dwErrorCode = GetLastError();
34     }
35 
36     RpcRevertToSelf();
37     UndoAlignRpcPtr(pAddJob, pAddJobAligned, cbBuf, pcbNeeded);
38 
39     return dwErrorCode;
40 }
41 
42 DWORD
43 _RpcEnumJobs(WINSPOOL_PRINTER_HANDLE hPrinter, DWORD FirstJob, DWORD NoJobs, DWORD Level, BYTE* pJob, DWORD cbBuf, DWORD* pcbNeeded, DWORD* pcReturned)
44 {
45     DWORD dwErrorCode;
46     PBYTE pJobAligned;
47 
48     dwErrorCode = RpcImpersonateClient(NULL);
49     if (dwErrorCode != ERROR_SUCCESS)
50     {
51         ERR("RpcImpersonateClient failed with error %lu!\n", dwErrorCode);
52         return dwErrorCode;
53     }
54 
55     pJobAligned = AlignRpcPtr(pJob, &cbBuf);
56 
57     if (EnumJobsW(hPrinter, FirstJob, NoJobs, Level, pJobAligned, cbBuf, pcbNeeded, pcReturned))
58     {
59         // Replace absolute pointer addresses in the output by relative offsets for JOB_INFO_1W and JOB_INFO_2W.
60         if (Level <= 2)
61         {
62             ASSERT(Level >= 1);
63             MarshallDownStructuresArray(pJobAligned, *pcReturned, pJobInfoMarshalling[Level]->pInfo, pJobInfoMarshalling[Level]->cbStructureSize, TRUE);
64         }
65     }
66     else
67     {
68         dwErrorCode = GetLastError();
69     }
70 
71     RpcRevertToSelf();
72     UndoAlignRpcPtr(pJob, pJobAligned, cbBuf, pcbNeeded);
73 
74     return dwErrorCode;
75 }
76 
77 DWORD
78 _RpcGetJob(WINSPOOL_PRINTER_HANDLE hPrinter, DWORD JobId, DWORD Level, BYTE* pJob, DWORD cbBuf, DWORD* pcbNeeded)
79 {
80     DWORD dwErrorCode;
81     PBYTE pJobAligned;
82 
83     dwErrorCode = RpcImpersonateClient(NULL);
84     if (dwErrorCode != ERROR_SUCCESS)
85     {
86         ERR("RpcImpersonateClient failed with error %lu!\n", dwErrorCode);
87         return dwErrorCode;
88     }
89 
90     pJobAligned = AlignRpcPtr(pJob, &cbBuf);
91 
92     if (GetJobW(hPrinter, JobId, Level, pJobAligned, cbBuf, pcbNeeded))
93     {
94         // Replace absolute pointer addresses in the output by relative offsets.
95         ASSERT(Level >= 1 && Level <= 2);
96         MarshallDownStructure(pJobAligned, pJobInfoMarshalling[Level]->pInfo, pJobInfoMarshalling[Level]->cbStructureSize, TRUE);
97     }
98     else
99     {
100         dwErrorCode = GetLastError();
101     }
102 
103     RpcRevertToSelf();
104     UndoAlignRpcPtr(pJob, pJobAligned, cbBuf, pcbNeeded);
105 
106     return dwErrorCode;
107 }
108 
109 DWORD
110 _RpcScheduleJob(WINSPOOL_PRINTER_HANDLE hPrinter, DWORD JobId)
111 {
112     DWORD dwErrorCode;
113 
114     dwErrorCode = RpcImpersonateClient(NULL);
115     if (dwErrorCode != ERROR_SUCCESS)
116     {
117         ERR("RpcImpersonateClient failed with error %lu!\n", dwErrorCode);
118         return dwErrorCode;
119     }
120 
121     if (!ScheduleJob(hPrinter, JobId))
122         dwErrorCode = GetLastError();
123 
124     RpcRevertToSelf();
125     return dwErrorCode;
126 }
127 
128 DWORD
129 _RpcSetJob(WINSPOOL_PRINTER_HANDLE hPrinter, DWORD JobId, WINSPOOL_JOB_CONTAINER* pJobContainer, DWORD Command)
130 {
131     DWORD dwErrorCode;
132 
133     dwErrorCode = RpcImpersonateClient(NULL);
134     if (dwErrorCode != ERROR_SUCCESS)
135     {
136         ERR("RpcImpersonateClient failed with error %lu!\n", dwErrorCode);
137         return dwErrorCode;
138     }
139 
140     // pJobContainer->JobInfo is a union of pointers, so we can just convert any element to our BYTE pointer.
141     if (!SetJobW(hPrinter, JobId, pJobContainer->Level, (PBYTE)pJobContainer->JobInfo.Level1, Command))
142         dwErrorCode = GetLastError();
143 
144     RpcRevertToSelf();
145     return dwErrorCode;
146 }
147