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/>.
22 #include "../librpc/gen_ndr/cli_spoolss.h"
23 #include "rpc_server/rpc_ncacn_np.h"
24 #include "smbd/globals.h"
25 #include "../libcli/security/security.h"
27 void print_spool_terminate(struct connection_struct
*conn
,
28 struct print_file_data
*print_file
);
30 /***************************************************************************
31 * Open a Document over spoolss
32 ***************************************************************************/
34 #define DOCNAME_DEFAULT "Remote Downlevel Document"
35 #ifndef PRINT_SPOOL_PREFIX
36 #define PRINT_SPOOL_PREFIX "smbprn."
39 NTSTATUS
print_spool_open(files_struct
*fsp
,
41 uint16_t current_vuid
)
45 struct print_file_data
*pf
;
46 struct rpc_pipe_client
*cli
;
47 struct spoolss_DevmodeContainer devmode_ctr
;
48 union spoolss_DocumentInfo info
;
52 tmp_ctx
= talloc_new(fsp
);
54 return NT_STATUS_NO_MEMORY
;
57 pf
= talloc_zero(fsp
, struct print_file_data
);
59 status
= NT_STATUS_NO_MEMORY
;
62 pf
->svcname
= talloc_strdup(pf
, lp_servicename(SNUM(fsp
->conn
)));
64 /* the document name is derived from the file name.
65 * "Remote Downlevel Document" is added in front to
66 * mimic what windows does in this case */
67 pf
->docname
= talloc_strdup(pf
, DOCNAME_DEFAULT
);
69 status
= NT_STATUS_NO_MEMORY
;
73 const char *p
= strrchr(fname
, '/');
77 pf
->docname
= talloc_asprintf_append(pf
->docname
, " %s", p
);
79 status
= NT_STATUS_NO_MEMORY
;
84 /* Ok, now we have to open an actual file.
86 * We want to write the spool job to this file in
87 * smbd for scalability reason (and also because
88 * apparently window printer drivers can seek when
89 * spooling to a file).
90 * So we first create a file, and then we pass it
91 * to spoolss in output_file so it can monitor and
92 * take over once we call EndDocPrinter().
93 * Of course we will not start writing until
94 * StartDocPrinter() actually gives the ok. */
96 pf
->filename
= talloc_asprintf(pf
, "%s/%s.XXXXXX",
97 lp_pathname(SNUM(fsp
->conn
)),
100 status
= NT_STATUS_NO_MEMORY
;
104 fd
= mkstemp(pf
->filename
);
106 if (errno
== EACCES
) {
107 /* Common setup error, force a report. */
108 DEBUG(0, ("Insufficient permissions "
109 "to open spool file %s.\n",
112 /* Normal case, report at level 3 and above. */
113 DEBUG(3, ("can't open spool file %s,\n",
115 DEBUGADD(3, ("errno = %d (%s).\n",
116 errno
, strerror(errno
)));
118 status
= map_nt_error_from_unix(errno
);
122 /* now open a document over spoolss so that it does
123 * all printer verification, and eventually assigns
126 status
= rpc_pipe_open_interface(fsp
->conn
,
127 &ndr_table_spoolss
.syntax_id
,
128 fsp
->conn
->server_info
,
129 &fsp
->conn
->sconn
->client_id
,
130 fsp
->conn
->sconn
->msg_ctx
,
131 &fsp
->conn
->spoolss_pipe
);
132 if (!NT_STATUS_IS_OK(status
)) {
135 cli
= fsp
->conn
->spoolss_pipe
;
137 ZERO_STRUCT(devmode_ctr
);
139 status
= rpccli_spoolss_OpenPrinter(cli
, pf
, pf
->svcname
,
141 SEC_FLAG_MAXIMUM_ALLOWED
,
143 if (!NT_STATUS_IS_OK(status
)) {
146 if (!W_ERROR_IS_OK(werr
)) {
147 status
= werror_to_ntstatus(werr
);
151 info
.info1
= talloc(tmp_ctx
, struct spoolss_DocumentInfo1
);
153 status
= NT_STATUS_NO_MEMORY
;
156 info
.info1
->document_name
= pf
->docname
;
157 info
.info1
->output_file
= pf
->filename
;
158 info
.info1
->datatype
= "RAW";
160 status
= rpccli_spoolss_StartDocPrinter(cli
, tmp_ctx
, &pf
->handle
,
161 1, info
, &pf
->jobid
, &werr
);
162 if (!NT_STATUS_IS_OK(status
)) {
165 if (!W_ERROR_IS_OK(werr
)) {
166 status
= werror_to_ntstatus(werr
);
170 /* Convert to RAP id. */
171 pf
->rap_jobid
= pjobid_to_rap(pf
->svcname
, pf
->jobid
);
172 if (pf
->rap_jobid
== 0) {
173 /* No errno around here */
174 status
= NT_STATUS_ACCESS_DENIED
;
178 /* setup a full fsp */
179 status
= create_synthetic_smb_fname(fsp
, pf
->filename
, NULL
,
180 NULL
, &fsp
->fsp_name
);
181 if (!NT_STATUS_IS_OK(status
)) {
185 if (sys_fstat(fd
, &fsp
->fsp_name
->st
, false) != 0) {
186 status
= map_nt_error_from_unix(errno
);
190 fsp
->file_id
= vfs_file_id_from_sbuf(fsp
->conn
, &fsp
->fsp_name
->st
);
191 fsp
->mode
= fsp
->fsp_name
->st
.st_ex_mode
;
194 fsp
->vuid
= current_vuid
;
195 fsp
->can_lock
= false;
196 fsp
->can_read
= false;
197 fsp
->access_mask
= FILE_GENERIC_WRITE
;
198 fsp
->can_write
= true;
199 fsp
->modified
= false;
200 fsp
->oplock_type
= NO_OPLOCK
;
201 fsp
->sent_oplock_break
= NO_BREAK_SENT
;
202 fsp
->is_directory
= false;
204 fsp
->print_file
= pf
;
206 status
= NT_STATUS_OK
;
208 if (!NT_STATUS_IS_OK(status
)) {
211 unlink(fsp
->print_file
->filename
);
213 /* We need to delete the job from spoolss too */
215 print_spool_terminate(fsp
->conn
, pf
);
218 talloc_free(tmp_ctx
);
222 int print_spool_write(files_struct
*fsp
,
223 const char *data
, uint32_t size
,
224 SMB_OFF_T offset
, uint32_t *written
)
232 /* first of all stat file to find out if it is still there.
233 * spoolss may have deleted it to signal someone has killed
234 * the job through it's interface */
236 if (sys_fstat(fsp
->fh
->fd
, &st
, false) != 0) {
238 DEBUG(3, ("printfile_offset: sys_fstat failed on %s (%s)\n",
239 fsp_str_dbg(fsp
), strerror(ret
)));
243 /* check if the file is unlinked, this will signal spoolss has
244 * killed it, just return an error and close the file */
245 if (st
.st_ex_nlink
== 0) {
250 /* When print files go beyond 4GB, the 32-bit offset sent in
251 * old SMBwrite calls is relative to the current 4GB chunk
253 * Discovered by Sebastian Kloska <oncaphillis@snafu.de>.
255 if (offset
< 0xffffffff00000000LL
) {
256 offset
= (st
.st_ex_size
& 0xffffffff00000000LL
) + offset
;
259 n
= write_data_at_offset(fsp
->fh
->fd
, data
, size
, offset
);
262 print_spool_terminate(fsp
->conn
, fsp
->print_file
);
271 void print_spool_end(files_struct
*fsp
, enum file_close_type close_type
)
273 struct rpc_pipe_client
*cli
;
277 status
= rpc_pipe_open_interface(fsp
->conn
,
278 &ndr_table_spoolss
.syntax_id
,
279 fsp
->conn
->server_info
,
280 &fsp
->conn
->sconn
->client_id
,
281 fsp
->conn
->sconn
->msg_ctx
,
282 &fsp
->conn
->spoolss_pipe
);
283 if (!NT_STATUS_IS_OK(status
)) {
284 DEBUG(0, ("print_spool_end: "
285 "Failed to get spoolss pipe [%s]\n",
289 cli
= fsp
->conn
->spoolss_pipe
;
291 switch (close_type
) {
294 /* this also automatically calls spoolss_EndDocPrinter */
295 status
= rpccli_spoolss_ClosePrinter(cli
, fsp
->print_file
,
296 &fsp
->print_file
->handle
,
298 if (!NT_STATUS_IS_OK(status
) ||
299 !NT_STATUS_IS_OK(status
= werror_to_ntstatus(werr
))) {
300 DEBUG(3, ("Failed to close printer %s [%s]\n",
301 fsp
->print_file
->svcname
, nt_errstr(status
)));
305 print_spool_terminate(fsp
->conn
, fsp
->print_file
);
311 void print_spool_terminate(struct connection_struct
*conn
,
312 struct print_file_data
*print_file
)
314 struct rpc_pipe_client
*cli
;
318 rap_jobid_delete(print_file
->svcname
, print_file
->jobid
);
320 status
= rpc_pipe_open_interface(conn
,
321 &ndr_table_spoolss
.syntax_id
,
323 &conn
->sconn
->client_id
,
324 conn
->sconn
->msg_ctx
,
325 &conn
->spoolss_pipe
);
326 if (!NT_STATUS_IS_OK(status
)) {
327 DEBUG(0, ("print_spool_terminate: "
328 "Failed to get spoolss pipe [%s]\n",
332 cli
= conn
->spoolss_pipe
;
334 status
= rpccli_spoolss_SetJob(cli
, print_file
,
337 NULL
, SPOOLSS_JOB_CONTROL_DELETE
,
339 if (!NT_STATUS_IS_OK(status
) ||
340 !NT_STATUS_IS_OK(status
= werror_to_ntstatus(werr
))) {
341 DEBUG(3, ("Failed to delete job %d [%s]\n",
342 print_file
->jobid
, nt_errstr(status
)));
345 status
= rpccli_spoolss_ClosePrinter(cli
, print_file
,
348 if (!NT_STATUS_IS_OK(status
) ||
349 !NT_STATUS_IS_OK(status
= werror_to_ntstatus(werr
))) {
350 DEBUG(3, ("Failed to close printer %s [%s]\n",
351 print_file
->svcname
, nt_errstr(status
)));