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/>.
21 #include "system/filesys.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"
28 #include "smbd/fd_handle.h"
29 #include "source3/printing/rap_jobid.h"
31 struct print_file_data
{
35 struct policy_handle handle
;
40 uint16_t print_spool_rap_jobid(struct print_file_data
*print_file
)
42 if (print_file
== NULL
) {
46 return print_file
->rap_jobid
;
49 void print_spool_terminate(struct connection_struct
*conn
,
50 struct print_file_data
*print_file
);
52 /***************************************************************************
53 * Open a Document over spoolss
54 ***************************************************************************/
56 #define DOCNAME_DEFAULT "Remote Downlevel Document"
58 NTSTATUS
print_spool_open(files_struct
*fsp
,
60 uint64_t current_vuid
)
62 const struct loadparm_substitution
*lp_sub
=
63 loadparm_s3_global_substitution();
66 struct print_file_data
*pf
;
67 struct dcerpc_binding_handle
*b
= NULL
;
68 struct spoolss_DevmodeContainer devmode_ctr
;
69 struct spoolss_DocumentInfoCtr info_ctr
;
70 struct spoolss_DocumentInfo1
*info1
;
75 tmp_ctx
= talloc_new(fsp
);
77 return NT_STATUS_NO_MEMORY
;
80 pf
= talloc_zero(fsp
, struct print_file_data
);
82 status
= NT_STATUS_NO_MEMORY
;
85 pf
->svcname
= lp_servicename(pf
, lp_sub
, SNUM(fsp
->conn
));
87 /* the document name is derived from the file name.
88 * "Remote Downlevel Document" is added in front to
89 * mimic what windows does in this case */
90 pf
->docname
= talloc_strdup(pf
, DOCNAME_DEFAULT
);
92 status
= NT_STATUS_NO_MEMORY
;
96 const char *p
= strrchr(fname
, '/');
100 pf
->docname
= talloc_asprintf_append(pf
->docname
, " %s", p
);
102 status
= NT_STATUS_NO_MEMORY
;
108 * Ok, now we have to open an actual file.
109 * Here is the reason:
110 * We want to write the spool job to this file in
111 * smbd for scalability reason (and also because
112 * apparently window printer drivers can seek when
113 * spooling to a file).
114 * So we first create a file, and then we pass it
115 * to spoolss in output_file so it can monitor and
116 * take over once we call EndDocPrinter().
117 * Of course we will not start writing until
118 * StartDocPrinter() actually gives the ok.
119 * smbd spooler files do not include a print jobid
120 * path component, as the jobid is only known after
121 * calling StartDocPrinter().
124 pf
->filename
= talloc_asprintf(pf
, "%s/%sXXXXXX",
125 lp_path(talloc_tos(),
130 status
= NT_STATUS_NO_MEMORY
;
134 mask
= umask(S_IRWXO
| S_IRWXG
);
135 fd
= mkstemp(pf
->filename
);
138 if (errno
== EACCES
) {
139 /* Common setup error, force a report. */
140 DEBUG(0, ("Insufficient permissions "
141 "to open spool file %s.\n",
144 /* Normal case, report at level 3 and above. */
145 DEBUG(3, ("can't open spool file %s,\n",
147 DEBUGADD(3, ("errno = %d (%s).\n",
148 errno
, strerror(errno
)));
150 status
= map_nt_error_from_unix(errno
);
154 /* now open a document over spoolss so that it does
155 * all printer verification, and eventually assigns
158 status
= rpc_pipe_open_interface(fsp
->conn
,
160 fsp
->conn
->session_info
,
161 fsp
->conn
->sconn
->remote_address
,
162 fsp
->conn
->sconn
->local_address
,
163 fsp
->conn
->sconn
->msg_ctx
,
164 &fsp
->conn
->spoolss_pipe
);
165 if (!NT_STATUS_IS_OK(status
)) {
168 b
= fsp
->conn
->spoolss_pipe
->binding_handle
;
170 ZERO_STRUCT(devmode_ctr
);
172 status
= dcerpc_spoolss_OpenPrinter(b
, pf
, pf
->svcname
,
176 if (!NT_STATUS_IS_OK(status
)) {
179 if (!W_ERROR_IS_OK(werr
)) {
180 status
= werror_to_ntstatus(werr
);
184 info1
= talloc(tmp_ctx
, struct spoolss_DocumentInfo1
);
186 status
= NT_STATUS_NO_MEMORY
;
189 info1
->document_name
= pf
->docname
;
190 info1
->output_file
= pf
->filename
;
191 info1
->datatype
= "RAW";
194 info_ctr
.info
.info1
= info1
;
196 status
= dcerpc_spoolss_StartDocPrinter(b
, tmp_ctx
,
201 if (!NT_STATUS_IS_OK(status
)) {
204 if (!W_ERROR_IS_OK(werr
)) {
205 status
= werror_to_ntstatus(werr
);
209 /* Convert to RAP id. */
210 pf
->rap_jobid
= pjobid_to_rap(pf
->svcname
, pf
->jobid
);
211 if (pf
->rap_jobid
== 0) {
212 /* No errno around here */
213 status
= NT_STATUS_ACCESS_DENIED
;
217 /* setup a full fsp */
218 fsp
->fsp_name
= synthetic_smb_fname(fsp
,
224 if (fsp
->fsp_name
== NULL
) {
225 status
= NT_STATUS_NO_MEMORY
;
229 if (sys_fstat(fd
, &fsp
->fsp_name
->st
, false) != 0) {
230 status
= map_nt_error_from_unix(errno
);
234 fsp
->file_id
= vfs_file_id_from_sbuf(fsp
->conn
, &fsp
->fsp_name
->st
);
237 fsp
->vuid
= current_vuid
;
238 fsp
->fsp_flags
.can_lock
= false;
239 fsp
->fsp_flags
.can_read
= false;
240 fsp
->access_mask
= FILE_GENERIC_WRITE
;
241 fsp
->fsp_flags
.can_write
= true;
242 fsp
->fsp_flags
.modified
= false;
243 fsp
->oplock_type
= NO_OPLOCK
;
244 fsp
->sent_oplock_break
= NO_BREAK_SENT
;
245 fsp
->fsp_flags
.is_directory
= false;
246 fsp
->fsp_flags
.delete_on_close
= false;
247 fsp
->fsp_flags
.is_fsa
= true;
249 fsp
->print_file
= pf
;
251 status
= NT_STATUS_OK
;
253 if (!NT_STATUS_IS_OK(status
)) {
256 if (fsp
->print_file
) {
257 unlink(fsp
->print_file
->filename
);
260 /* We need to delete the job from spoolss too */
261 if (pf
&& pf
->jobid
) {
262 print_spool_terminate(fsp
->conn
, pf
);
265 talloc_free(tmp_ctx
);
269 int print_spool_write(files_struct
*fsp
,
270 const char *data
, uint32_t size
,
271 off_t offset
, uint32_t *written
)
279 /* first of all stat file to find out if it is still there.
280 * spoolss may have deleted it to signal someone has killed
281 * the job through it's interface */
283 if (sys_fstat(fsp_get_io_fd(fsp
), &st
, false) != 0) {
285 DEBUG(3, ("printfile_offset: sys_fstat failed on %s (%s)\n",
286 fsp_str_dbg(fsp
), strerror(ret
)));
290 /* check if the file is unlinked, this will signal spoolss has
291 * killed it, just return an error and close the file */
292 if (st
.st_ex_nlink
== 0) {
293 close(fsp_get_io_fd(fsp
));
297 /* When print files go beyond 4GB, the 32-bit offset sent in
298 * old SMBwrite calls is relative to the current 4GB chunk
300 * Discovered by Sebastian Kloska <oncaphillis@snafu.de>.
302 if (offset
< 0xffffffff00000000LL
) {
303 offset
= (st
.st_ex_size
& 0xffffffff00000000LL
) + offset
;
306 n
= write_data_at_offset(fsp_get_io_fd(fsp
), data
, size
, offset
);
309 print_spool_terminate(fsp
->conn
, fsp
->print_file
);
318 void print_spool_end(files_struct
*fsp
, enum file_close_type close_type
)
322 struct dcerpc_binding_handle
*b
= NULL
;
324 if (fsp
->fsp_flags
.delete_on_close
) {
328 * Job was requested to be cancelled by setting
329 * delete on close so truncate the job file.
330 * print_job_end() which is called from
331 * _spoolss_EndDocPrinter() will take
332 * care of deleting it for us.
334 ret
= ftruncate(fsp_get_io_fd(fsp
), 0);
336 DBG_ERR("ftruncate failed: %s\n", strerror(errno
));
340 b
= fsp
->conn
->spoolss_pipe
->binding_handle
;
342 switch (close_type
) {
345 /* this also automatically calls spoolss_EndDocPrinter */
346 status
= dcerpc_spoolss_ClosePrinter(b
, fsp
->print_file
,
347 &fsp
->print_file
->handle
,
349 if (!NT_STATUS_IS_OK(status
) ||
350 !NT_STATUS_IS_OK(status
= werror_to_ntstatus(werr
))) {
351 DEBUG(3, ("Failed to close printer %s [%s]\n",
352 fsp
->print_file
->svcname
, nt_errstr(status
)));
356 print_spool_terminate(fsp
->conn
, fsp
->print_file
);
362 void print_spool_terminate(struct connection_struct
*conn
,
363 struct print_file_data
*print_file
)
367 struct dcerpc_binding_handle
*b
= NULL
;
369 rap_jobid_delete(print_file
->svcname
, print_file
->jobid
);
371 status
= rpc_pipe_open_interface(conn
,
374 conn
->sconn
->remote_address
,
375 conn
->sconn
->local_address
,
376 conn
->sconn
->msg_ctx
,
377 &conn
->spoolss_pipe
);
378 if (!NT_STATUS_IS_OK(status
)) {
379 DEBUG(0, ("print_spool_terminate: "
380 "Failed to get spoolss pipe [%s]\n",
384 b
= conn
->spoolss_pipe
->binding_handle
;
386 status
= dcerpc_spoolss_SetJob(b
, print_file
,
389 NULL
, SPOOLSS_JOB_CONTROL_DELETE
,
391 if (!NT_STATUS_IS_OK(status
) ||
392 !NT_STATUS_IS_OK(status
= werror_to_ntstatus(werr
))) {
393 DEBUG(3, ("Failed to delete job %d [%s]\n",
394 print_file
->jobid
, nt_errstr(status
)));
397 status
= dcerpc_spoolss_ClosePrinter(b
, print_file
,
400 if (!NT_STATUS_IS_OK(status
) ||
401 !NT_STATUS_IS_OK(status
= werror_to_ntstatus(werr
))) {
402 DEBUG(3, ("Failed to close printer %s [%s]\n",
403 print_file
->svcname
, nt_errstr(status
)));