selftest: Run waf build --dup-symbol-check under $PYTHON
[Samba.git] / source3 / printing / printspoolss.c
blob9d565de120ad44ecdc37f0051017da6a1b928d07
1 /*
2 Unix SMB/CIFS implementation.
3 Printing routines that bridge to spoolss
4 Copyright (C) Simo Sorce 2010
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "includes.h"
21 #include "system/filesys.h"
22 #include "printing.h"
23 #include "rpc_client/rpc_client.h"
24 #include "../librpc/gen_ndr/ndr_spoolss_c.h"
25 #include "rpc_server/rpc_ncacn_np.h"
26 #include "smbd/globals.h"
27 #include "../libcli/security/security.h"
29 struct print_file_data {
30 char *svcname;
31 char *docname;
32 char *filename;
33 struct policy_handle handle;
34 uint32_t jobid;
35 uint16_t rap_jobid;
38 uint16_t print_spool_rap_jobid(struct print_file_data *print_file)
40 if (print_file == NULL) {
41 return 0;
44 return print_file->rap_jobid;
47 void print_spool_terminate(struct connection_struct *conn,
48 struct print_file_data *print_file);
50 /***************************************************************************
51 * Open a Document over spoolss
52 ***************************************************************************/
54 #define DOCNAME_DEFAULT "Remote Downlevel Document"
56 NTSTATUS print_spool_open(files_struct *fsp,
57 const char *fname,
58 uint64_t current_vuid)
60 NTSTATUS status;
61 TALLOC_CTX *tmp_ctx;
62 struct print_file_data *pf;
63 struct dcerpc_binding_handle *b = NULL;
64 struct spoolss_DevmodeContainer devmode_ctr;
65 struct spoolss_DocumentInfoCtr info_ctr;
66 struct spoolss_DocumentInfo1 *info1;
67 int fd = -1;
68 WERROR werr;
69 mode_t mask;
71 tmp_ctx = talloc_new(fsp);
72 if (!tmp_ctx) {
73 return NT_STATUS_NO_MEMORY;
76 pf = talloc_zero(fsp, struct print_file_data);
77 if (!pf) {
78 status = NT_STATUS_NO_MEMORY;
79 goto done;
81 pf->svcname = lp_servicename(pf, SNUM(fsp->conn));
83 /* the document name is derived from the file name.
84 * "Remote Downlevel Document" is added in front to
85 * mimic what windows does in this case */
86 pf->docname = talloc_strdup(pf, DOCNAME_DEFAULT);
87 if (!pf->docname) {
88 status = NT_STATUS_NO_MEMORY;
89 goto done;
91 if (fname) {
92 const char *p = strrchr(fname, '/');
93 if (!p) {
94 p = fname;
96 pf->docname = talloc_asprintf_append(pf->docname, " %s", p);
97 if (!pf->docname) {
98 status = NT_STATUS_NO_MEMORY;
99 goto done;
104 * Ok, now we have to open an actual file.
105 * Here is the reason:
106 * We want to write the spool job to this file in
107 * smbd for scalability reason (and also because
108 * apparently window printer drivers can seek when
109 * spooling to a file).
110 * So we first create a file, and then we pass it
111 * to spoolss in output_file so it can monitor and
112 * take over once we call EndDocPrinter().
113 * Of course we will not start writing until
114 * StartDocPrinter() actually gives the ok.
115 * smbd spooler files do not include a print jobid
116 * path component, as the jobid is only known after
117 * calling StartDocPrinter().
120 pf->filename = talloc_asprintf(pf, "%s/%sXXXXXX",
121 lp_path(talloc_tos(),
122 SNUM(fsp->conn)),
123 PRINT_SPOOL_PREFIX);
124 if (!pf->filename) {
125 status = NT_STATUS_NO_MEMORY;
126 goto done;
128 errno = 0;
129 mask = umask(S_IRWXO | S_IRWXG);
130 fd = mkstemp(pf->filename);
131 umask(mask);
132 if (fd == -1) {
133 if (errno == EACCES) {
134 /* Common setup error, force a report. */
135 DEBUG(0, ("Insufficient permissions "
136 "to open spool file %s.\n",
137 pf->filename));
138 } else {
139 /* Normal case, report at level 3 and above. */
140 DEBUG(3, ("can't open spool file %s,\n",
141 pf->filename));
142 DEBUGADD(3, ("errno = %d (%s).\n",
143 errno, strerror(errno)));
145 status = map_nt_error_from_unix(errno);
146 goto done;
149 /* now open a document over spoolss so that it does
150 * all printer verification, and eventually assigns
151 * a job id */
153 status = rpc_pipe_open_interface(fsp->conn,
154 &ndr_table_spoolss,
155 fsp->conn->session_info,
156 fsp->conn->sconn->remote_address,
157 fsp->conn->sconn->local_address,
158 fsp->conn->sconn->msg_ctx,
159 &fsp->conn->spoolss_pipe);
160 if (!NT_STATUS_IS_OK(status)) {
161 goto done;
163 b = fsp->conn->spoolss_pipe->binding_handle;
165 ZERO_STRUCT(devmode_ctr);
167 status = dcerpc_spoolss_OpenPrinter(b, pf, pf->svcname,
168 "RAW", devmode_ctr,
169 PRINTER_ACCESS_USE,
170 &pf->handle, &werr);
171 if (!NT_STATUS_IS_OK(status)) {
172 goto done;
174 if (!W_ERROR_IS_OK(werr)) {
175 status = werror_to_ntstatus(werr);
176 goto done;
179 info1 = talloc(tmp_ctx, struct spoolss_DocumentInfo1);
180 if (info1 == NULL) {
181 status = NT_STATUS_NO_MEMORY;
182 goto done;
184 info1->document_name = pf->docname;
185 info1->output_file = pf->filename;
186 info1->datatype = "RAW";
188 info_ctr.level = 1;
189 info_ctr.info.info1 = info1;
191 status = dcerpc_spoolss_StartDocPrinter(b, tmp_ctx,
192 &pf->handle,
193 &info_ctr,
194 &pf->jobid,
195 &werr);
196 if (!NT_STATUS_IS_OK(status)) {
197 goto done;
199 if (!W_ERROR_IS_OK(werr)) {
200 status = werror_to_ntstatus(werr);
201 goto done;
204 /* Convert to RAP id. */
205 pf->rap_jobid = pjobid_to_rap(pf->svcname, pf->jobid);
206 if (pf->rap_jobid == 0) {
207 /* No errno around here */
208 status = NT_STATUS_ACCESS_DENIED;
209 goto done;
212 /* setup a full fsp */
213 fsp->fsp_name = synthetic_smb_fname(fsp, pf->filename, NULL, NULL, 0);
214 if (fsp->fsp_name == NULL) {
215 status = NT_STATUS_NO_MEMORY;
216 goto done;
219 if (sys_fstat(fd, &fsp->fsp_name->st, false) != 0) {
220 status = map_nt_error_from_unix(errno);
221 goto done;
224 fsp->file_id = vfs_file_id_from_sbuf(fsp->conn, &fsp->fsp_name->st);
225 fsp->fh->fd = fd;
227 fsp->vuid = current_vuid;
228 fsp->can_lock = false;
229 fsp->can_read = false;
230 fsp->access_mask = FILE_GENERIC_WRITE;
231 fsp->can_write = true;
232 fsp->modified = false;
233 fsp->oplock_type = NO_OPLOCK;
234 fsp->sent_oplock_break = NO_BREAK_SENT;
235 fsp->is_directory = false;
237 fsp->print_file = pf;
239 status = NT_STATUS_OK;
240 done:
241 if (!NT_STATUS_IS_OK(status)) {
242 if (fd != -1) {
243 close(fd);
244 if (fsp->print_file) {
245 unlink(fsp->print_file->filename);
248 /* We need to delete the job from spoolss too */
249 if (pf && pf->jobid) {
250 print_spool_terminate(fsp->conn, pf);
253 talloc_free(tmp_ctx);
254 return status;
257 int print_spool_write(files_struct *fsp,
258 const char *data, uint32_t size,
259 off_t offset, uint32_t *written)
261 SMB_STRUCT_STAT st;
262 ssize_t n;
263 int ret;
265 *written = 0;
267 /* first of all stat file to find out if it is still there.
268 * spoolss may have deleted it to signal someone has killed
269 * the job through it's interface */
271 if (sys_fstat(fsp->fh->fd, &st, false) != 0) {
272 ret = errno;
273 DEBUG(3, ("printfile_offset: sys_fstat failed on %s (%s)\n",
274 fsp_str_dbg(fsp), strerror(ret)));
275 return ret;
278 /* check if the file is unlinked, this will signal spoolss has
279 * killed it, just return an error and close the file */
280 if (st.st_ex_nlink == 0) {
281 close(fsp->fh->fd);
282 return EBADF;
285 /* When print files go beyond 4GB, the 32-bit offset sent in
286 * old SMBwrite calls is relative to the current 4GB chunk
287 * we're writing to.
288 * Discovered by Sebastian Kloska <oncaphillis@snafu.de>.
290 if (offset < 0xffffffff00000000LL) {
291 offset = (st.st_ex_size & 0xffffffff00000000LL) + offset;
294 n = write_data_at_offset(fsp->fh->fd, data, size, offset);
295 if (n == -1) {
296 ret = errno;
297 print_spool_terminate(fsp->conn, fsp->print_file);
298 } else {
299 *written = n;
300 ret = 0;
303 return ret;
306 void print_spool_end(files_struct *fsp, enum file_close_type close_type)
308 NTSTATUS status;
309 WERROR werr;
310 struct dcerpc_binding_handle *b = NULL;
312 if (fsp->fh->private_options &
313 NTCREATEX_OPTIONS_PRIVATE_DELETE_ON_CLOSE) {
314 int ret;
317 * Job was requested to be cancelled by setting
318 * delete on close so truncate the job file.
319 * print_job_end() which is called from
320 * _spoolss_EndDocPrinter() will take
321 * care of deleting it for us.
323 ret = ftruncate(fsp->fh->fd, 0);
324 if (ret == -1) {
325 DBG_ERR("ftruncate failed: %s\n", strerror(errno));
329 b = fsp->conn->spoolss_pipe->binding_handle;
331 switch (close_type) {
332 case NORMAL_CLOSE:
333 case SHUTDOWN_CLOSE:
334 /* this also automatically calls spoolss_EndDocPrinter */
335 status = dcerpc_spoolss_ClosePrinter(b, fsp->print_file,
336 &fsp->print_file->handle,
337 &werr);
338 if (!NT_STATUS_IS_OK(status) ||
339 !NT_STATUS_IS_OK(status = werror_to_ntstatus(werr))) {
340 DEBUG(3, ("Failed to close printer %s [%s]\n",
341 fsp->print_file->svcname, nt_errstr(status)));
343 break;
344 case ERROR_CLOSE:
345 print_spool_terminate(fsp->conn, fsp->print_file);
346 break;
351 void print_spool_terminate(struct connection_struct *conn,
352 struct print_file_data *print_file)
354 NTSTATUS status;
355 WERROR werr;
356 struct dcerpc_binding_handle *b = NULL;
358 rap_jobid_delete(print_file->svcname, print_file->jobid);
360 status = rpc_pipe_open_interface(conn,
361 &ndr_table_spoolss,
362 conn->session_info,
363 conn->sconn->remote_address,
364 conn->sconn->local_address,
365 conn->sconn->msg_ctx,
366 &conn->spoolss_pipe);
367 if (!NT_STATUS_IS_OK(status)) {
368 DEBUG(0, ("print_spool_terminate: "
369 "Failed to get spoolss pipe [%s]\n",
370 nt_errstr(status)));
371 return;
373 b = conn->spoolss_pipe->binding_handle;
375 status = dcerpc_spoolss_SetJob(b, print_file,
376 &print_file->handle,
377 print_file->jobid,
378 NULL, SPOOLSS_JOB_CONTROL_DELETE,
379 &werr);
380 if (!NT_STATUS_IS_OK(status) ||
381 !NT_STATUS_IS_OK(status = werror_to_ntstatus(werr))) {
382 DEBUG(3, ("Failed to delete job %d [%s]\n",
383 print_file->jobid, nt_errstr(status)));
384 return;
386 status = dcerpc_spoolss_ClosePrinter(b, print_file,
387 &print_file->handle,
388 &werr);
389 if (!NT_STATUS_IS_OK(status) ||
390 !NT_STATUS_IS_OK(status = werror_to_ntstatus(werr))) {
391 DEBUG(3, ("Failed to close printer %s [%s]\n",
392 print_file->svcname, nt_errstr(status)));
393 return;