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 "rpc_client/rpc_client.h"
23 #include "../librpc/gen_ndr/ndr_spoolss_c.h"
24 #include "rpc_server/rpc_ncacn_np.h"
25 #include "smbd/globals.h"
26 #include "../libcli/security/security.h"
28 struct print_file_data
{
32 struct policy_handle handle
;
37 uint16_t print_spool_rap_jobid(struct print_file_data
*print_file
)
39 if (print_file
== NULL
) {
43 return print_file
->rap_jobid
;
46 void print_spool_terminate(struct connection_struct
*conn
,
47 struct print_file_data
*print_file
);
49 /***************************************************************************
50 * Open a Document over spoolss
51 ***************************************************************************/
53 #define DOCNAME_DEFAULT "Remote Downlevel Document"
54 #ifndef PRINT_SPOOL_PREFIX
55 #define PRINT_SPOOL_PREFIX "smbprn."
58 NTSTATUS
print_spool_open(files_struct
*fsp
,
60 uint64_t current_vuid
)
64 struct print_file_data
*pf
;
65 struct dcerpc_binding_handle
*b
= NULL
;
66 struct spoolss_DevmodeContainer devmode_ctr
;
67 union spoolss_DocumentInfo info
;
71 tmp_ctx
= talloc_new(fsp
);
73 return NT_STATUS_NO_MEMORY
;
76 pf
= talloc_zero(fsp
, struct print_file_data
);
78 status
= NT_STATUS_NO_MEMORY
;
81 pf
->svcname
= talloc_strdup(pf
, lp_servicename(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
);
88 status
= NT_STATUS_NO_MEMORY
;
92 const char *p
= strrchr(fname
, '/');
96 pf
->docname
= talloc_asprintf_append(pf
->docname
, " %s", p
);
98 status
= NT_STATUS_NO_MEMORY
;
103 /* Ok, now we have to open an actual file.
104 * Here is the reason:
105 * We want to write the spool job to this file in
106 * smbd for scalability reason (and also because
107 * apparently window printer drivers can seek when
108 * spooling to a file).
109 * So we first create a file, and then we pass it
110 * to spoolss in output_file so it can monitor and
111 * take over once we call EndDocPrinter().
112 * Of course we will not start writing until
113 * StartDocPrinter() actually gives the ok. */
115 pf
->filename
= talloc_asprintf(pf
, "%s/%s.XXXXXX",
116 lp_pathname(SNUM(fsp
->conn
)),
119 status
= NT_STATUS_NO_MEMORY
;
123 fd
= mkstemp(pf
->filename
);
125 if (errno
== EACCES
) {
126 /* Common setup error, force a report. */
127 DEBUG(0, ("Insufficient permissions "
128 "to open spool file %s.\n",
131 /* Normal case, report at level 3 and above. */
132 DEBUG(3, ("can't open spool file %s,\n",
134 DEBUGADD(3, ("errno = %d (%s).\n",
135 errno
, strerror(errno
)));
137 status
= map_nt_error_from_unix(errno
);
141 /* now open a document over spoolss so that it does
142 * all printer verification, and eventually assigns
145 status
= rpc_pipe_open_interface(fsp
->conn
,
146 &ndr_table_spoolss
.syntax_id
,
147 fsp
->conn
->session_info
,
148 fsp
->conn
->sconn
->remote_address
,
149 fsp
->conn
->sconn
->msg_ctx
,
150 &fsp
->conn
->spoolss_pipe
);
151 if (!NT_STATUS_IS_OK(status
)) {
154 b
= fsp
->conn
->spoolss_pipe
->binding_handle
;
156 ZERO_STRUCT(devmode_ctr
);
158 status
= dcerpc_spoolss_OpenPrinter(b
, pf
, pf
->svcname
,
160 SEC_FLAG_MAXIMUM_ALLOWED
,
162 if (!NT_STATUS_IS_OK(status
)) {
165 if (!W_ERROR_IS_OK(werr
)) {
166 status
= werror_to_ntstatus(werr
);
170 info
.info1
= talloc(tmp_ctx
, struct spoolss_DocumentInfo1
);
172 status
= NT_STATUS_NO_MEMORY
;
175 info
.info1
->document_name
= pf
->docname
;
176 info
.info1
->output_file
= pf
->filename
;
177 info
.info1
->datatype
= "RAW";
179 status
= dcerpc_spoolss_StartDocPrinter(b
, tmp_ctx
, &pf
->handle
,
180 1, info
, &pf
->jobid
, &werr
);
181 if (!NT_STATUS_IS_OK(status
)) {
184 if (!W_ERROR_IS_OK(werr
)) {
185 status
= werror_to_ntstatus(werr
);
189 /* Convert to RAP id. */
190 pf
->rap_jobid
= pjobid_to_rap(pf
->svcname
, pf
->jobid
);
191 if (pf
->rap_jobid
== 0) {
192 /* No errno around here */
193 status
= NT_STATUS_ACCESS_DENIED
;
197 /* setup a full fsp */
198 status
= create_synthetic_smb_fname(fsp
, pf
->filename
, NULL
,
199 NULL
, &fsp
->fsp_name
);
200 if (!NT_STATUS_IS_OK(status
)) {
204 if (sys_fstat(fd
, &fsp
->fsp_name
->st
, false) != 0) {
205 status
= map_nt_error_from_unix(errno
);
209 fsp
->file_id
= vfs_file_id_from_sbuf(fsp
->conn
, &fsp
->fsp_name
->st
);
212 fsp
->vuid
= current_vuid
;
213 fsp
->can_lock
= false;
214 fsp
->can_read
= false;
215 fsp
->access_mask
= FILE_GENERIC_WRITE
;
216 fsp
->can_write
= true;
217 fsp
->modified
= false;
218 fsp
->oplock_type
= NO_OPLOCK
;
219 fsp
->sent_oplock_break
= NO_BREAK_SENT
;
220 fsp
->is_directory
= false;
222 fsp
->print_file
= pf
;
224 status
= NT_STATUS_OK
;
226 if (!NT_STATUS_IS_OK(status
)) {
229 if (fsp
->print_file
) {
230 unlink(fsp
->print_file
->filename
);
233 /* We need to delete the job from spoolss too */
235 print_spool_terminate(fsp
->conn
, pf
);
238 talloc_free(tmp_ctx
);
242 int print_spool_write(files_struct
*fsp
,
243 const char *data
, uint32_t size
,
244 off_t offset
, uint32_t *written
)
252 /* first of all stat file to find out if it is still there.
253 * spoolss may have deleted it to signal someone has killed
254 * the job through it's interface */
256 if (sys_fstat(fsp
->fh
->fd
, &st
, false) != 0) {
258 DEBUG(3, ("printfile_offset: sys_fstat failed on %s (%s)\n",
259 fsp_str_dbg(fsp
), strerror(ret
)));
263 /* check if the file is unlinked, this will signal spoolss has
264 * killed it, just return an error and close the file */
265 if (st
.st_ex_nlink
== 0) {
270 /* When print files go beyond 4GB, the 32-bit offset sent in
271 * old SMBwrite calls is relative to the current 4GB chunk
273 * Discovered by Sebastian Kloska <oncaphillis@snafu.de>.
275 if (offset
< 0xffffffff00000000LL
) {
276 offset
= (st
.st_ex_size
& 0xffffffff00000000LL
) + offset
;
279 n
= write_data_at_offset(fsp
->fh
->fd
, data
, size
, offset
);
282 print_spool_terminate(fsp
->conn
, fsp
->print_file
);
291 void print_spool_end(files_struct
*fsp
, enum file_close_type close_type
)
295 struct dcerpc_binding_handle
*b
= NULL
;
297 b
= fsp
->conn
->spoolss_pipe
->binding_handle
;
299 switch (close_type
) {
302 /* this also automatically calls spoolss_EndDocPrinter */
303 status
= dcerpc_spoolss_ClosePrinter(b
, fsp
->print_file
,
304 &fsp
->print_file
->handle
,
306 if (!NT_STATUS_IS_OK(status
) ||
307 !NT_STATUS_IS_OK(status
= werror_to_ntstatus(werr
))) {
308 DEBUG(3, ("Failed to close printer %s [%s]\n",
309 fsp
->print_file
->svcname
, nt_errstr(status
)));
313 print_spool_terminate(fsp
->conn
, fsp
->print_file
);
319 void print_spool_terminate(struct connection_struct
*conn
,
320 struct print_file_data
*print_file
)
324 struct dcerpc_binding_handle
*b
= NULL
;
326 rap_jobid_delete(print_file
->svcname
, print_file
->jobid
);
328 status
= rpc_pipe_open_interface(conn
,
329 &ndr_table_spoolss
.syntax_id
,
331 conn
->sconn
->remote_address
,
332 conn
->sconn
->msg_ctx
,
333 &conn
->spoolss_pipe
);
334 if (!NT_STATUS_IS_OK(status
)) {
335 DEBUG(0, ("print_spool_terminate: "
336 "Failed to get spoolss pipe [%s]\n",
340 b
= conn
->spoolss_pipe
->binding_handle
;
342 status
= dcerpc_spoolss_SetJob(b
, print_file
,
345 NULL
, SPOOLSS_JOB_CONTROL_DELETE
,
347 if (!NT_STATUS_IS_OK(status
) ||
348 !NT_STATUS_IS_OK(status
= werror_to_ntstatus(werr
))) {
349 DEBUG(3, ("Failed to delete job %d [%s]\n",
350 print_file
->jobid
, nt_errstr(status
)));
353 status
= dcerpc_spoolss_ClosePrinter(b
, print_file
,
356 if (!NT_STATUS_IS_OK(status
) ||
357 !NT_STATUS_IS_OK(status
= werror_to_ntstatus(werr
))) {
358 DEBUG(3, ("Failed to close printer %s [%s]\n",
359 print_file
->svcname
, nt_errstr(status
)));