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"
24 void print_spool_terminate(struct connection_struct
*conn
,
25 struct print_file_data
*print_file
);
27 /***************************************************************************
28 * Open a Document over spoolss
29 ***************************************************************************/
31 #define DOCNAME_DEFAULT "Remote Downlevel Document"
32 #ifndef PRINT_SPOOL_PREFIX
33 #define PRINT_SPOOL_PREFIX "smbprn."
36 NTSTATUS
print_spool_open(files_struct
*fsp
,
38 uint16_t current_vuid
)
42 struct print_file_data
*pf
;
43 struct rpc_pipe_client
*cli
;
44 struct spoolss_DevmodeContainer devmode_ctr
;
45 union spoolss_DocumentInfo info
;
49 tmp_ctx
= talloc_new(fsp
);
51 return NT_STATUS_NO_MEMORY
;
54 pf
= talloc_zero(fsp
, struct print_file_data
);
56 status
= NT_STATUS_NO_MEMORY
;
59 pf
->svcname
= talloc_strdup(pf
, lp_servicename(SNUM(fsp
->conn
)));
61 /* the document name is derived from the file name.
62 * "Remote Downlevel Document" is added in front to
63 * mimic what windows does in this case */
64 pf
->docname
= talloc_strdup(pf
, DOCNAME_DEFAULT
);
66 status
= NT_STATUS_NO_MEMORY
;
70 const char *p
= strrchr(fname
, '/');
74 pf
->docname
= talloc_asprintf_append(pf
->docname
, " %s", p
);
76 status
= NT_STATUS_NO_MEMORY
;
81 /* Ok, now we have to open an actual file.
83 * We want to write the spool job to this file in
84 * smbd for scalability reason (and also because
85 * apparently window printer drivers can seek when
86 * spooling to a file).
87 * So we first create a file, and then we pass it
88 * to spoolss in output_file so it can monitor and
89 * take over once we call EndDocPrinter().
90 * Of course we will not start writing until
91 * StartDocPrinter() actually gives the ok. */
93 pf
->filename
= talloc_asprintf(pf
, "%s/%s.XXXXXX",
94 lp_pathname(SNUM(fsp
->conn
)),
97 status
= NT_STATUS_NO_MEMORY
;
101 fd
= mkstemp(pf
->filename
);
103 if (errno
== EACCES
) {
104 /* Common setup error, force a report. */
105 DEBUG(0, ("Insufficient permissions "
106 "to open spool file %s.\n",
109 /* Normal case, report at level 3 and above. */
110 DEBUG(3, ("can't open spool file %s,\n",
112 DEBUGADD(3, ("errno = %d (%s).\n",
113 errno
, strerror(errno
)));
115 status
= map_nt_error_from_unix(errno
);
119 /* now open a document over spoolss so that it does
120 * all printer verification, and eventually assigns
123 status
= rpc_connect_spoolss_pipe(fsp
->conn
, &cli
);
124 if (!NT_STATUS_IS_OK(status
)) {
128 ZERO_STRUCT(devmode_ctr
);
130 status
= rpccli_spoolss_OpenPrinter(cli
, pf
, pf
->svcname
,
132 SEC_FLAG_MAXIMUM_ALLOWED
,
134 if (!NT_STATUS_IS_OK(status
)) {
137 if (!W_ERROR_IS_OK(werr
)) {
138 status
= werror_to_ntstatus(werr
);
142 info
.info1
= talloc(tmp_ctx
, struct spoolss_DocumentInfo1
);
144 status
= NT_STATUS_NO_MEMORY
;
147 info
.info1
->document_name
= pf
->docname
;
148 info
.info1
->output_file
= pf
->filename
;
149 info
.info1
->datatype
= "RAW";
151 status
= rpccli_spoolss_StartDocPrinter(cli
, tmp_ctx
, &pf
->handle
,
152 1, info
, &pf
->jobid
, &werr
);
153 if (!NT_STATUS_IS_OK(status
)) {
156 if (!W_ERROR_IS_OK(werr
)) {
157 status
= werror_to_ntstatus(werr
);
161 /* Convert to RAP id. */
162 pf
->rap_jobid
= pjobid_to_rap(pf
->svcname
, pf
->jobid
);
163 if (pf
->rap_jobid
== 0) {
164 /* No errno around here */
165 status
= NT_STATUS_ACCESS_DENIED
;
169 /* setup a full fsp */
170 status
= create_synthetic_smb_fname(fsp
, pf
->filename
, NULL
,
171 NULL
, &fsp
->fsp_name
);
172 if (!NT_STATUS_IS_OK(status
)) {
176 if (sys_fstat(fd
, &fsp
->fsp_name
->st
, false) != 0) {
177 status
= map_nt_error_from_unix(errno
);
181 fsp
->file_id
= vfs_file_id_from_sbuf(fsp
->conn
, &fsp
->fsp_name
->st
);
182 fsp
->mode
= fsp
->fsp_name
->st
.st_ex_mode
;
185 fsp
->vuid
= current_vuid
;
186 fsp
->can_lock
= false;
187 fsp
->can_read
= false;
188 fsp
->access_mask
= FILE_GENERIC_WRITE
;
189 fsp
->can_write
= true;
190 fsp
->modified
= false;
191 fsp
->oplock_type
= NO_OPLOCK
;
192 fsp
->sent_oplock_break
= NO_BREAK_SENT
;
193 fsp
->is_directory
= false;
195 fsp
->print_file
= pf
;
197 status
= NT_STATUS_OK
;
199 if (!NT_STATUS_IS_OK(status
)) {
202 unlink(fsp
->print_file
->filename
);
204 /* We need to delete the job from spoolss too */
206 print_spool_terminate(fsp
->conn
, pf
);
209 talloc_free(tmp_ctx
);
213 int print_spool_write(files_struct
*fsp
,
214 const char *data
, uint32_t size
,
215 SMB_OFF_T offset
, uint32_t *written
)
223 /* first of all stat file to find out if it is still there.
224 * spoolss may have deleted it to signal someone has killed
225 * the job through it's interface */
227 if (sys_fstat(fsp
->fh
->fd
, &st
, false) != 0) {
229 DEBUG(3, ("printfile_offset: sys_fstat failed on %s (%s)\n",
230 fsp_str_dbg(fsp
), strerror(ret
)));
234 /* check if the file is unlinked, this will signal spoolss has
235 * killed it, just return an error and close the file */
236 if (st
.st_ex_nlink
== 0) {
241 /* When print files go beyond 4GB, the 32-bit offset sent in
242 * old SMBwrite calls is relative to the current 4GB chunk
244 * Discovered by Sebastian Kloska <oncaphillis@snafu.de>.
246 if (offset
< 0xffffffff00000000LL
) {
247 offset
= (st
.st_ex_size
& 0xffffffff00000000LL
) + offset
;
250 n
= write_data_at_offset(fsp
->fh
->fd
, data
, size
, offset
);
253 print_spool_terminate(fsp
->conn
, fsp
->print_file
);
262 void print_spool_end(files_struct
*fsp
, enum file_close_type close_type
)
264 struct rpc_pipe_client
*cli
;
268 status
= rpc_connect_spoolss_pipe(fsp
->conn
, &cli
);
269 if (!NT_STATUS_IS_OK(status
)) {
270 DEBUG(0, ("print_spool_end: "
271 "Failed to get spoolss pipe [%s]\n",
276 switch (close_type
) {
279 /* this also automatically calls spoolss_EndDocPrinter */
280 status
= rpccli_spoolss_ClosePrinter(cli
, fsp
->print_file
,
281 &fsp
->print_file
->handle
,
283 if (!NT_STATUS_IS_OK(status
) ||
284 !NT_STATUS_IS_OK(status
= werror_to_ntstatus(werr
))) {
285 DEBUG(3, ("Failed to close printer %s [%s]\n",
286 fsp
->print_file
->svcname
, nt_errstr(status
)));
290 print_spool_terminate(fsp
->conn
, fsp
->print_file
);
296 void print_spool_terminate(struct connection_struct
*conn
,
297 struct print_file_data
*print_file
)
299 struct rpc_pipe_client
*cli
;
303 rap_jobid_delete(print_file
->svcname
, print_file
->jobid
);
305 status
= rpc_connect_spoolss_pipe(conn
, &cli
);
306 if (!NT_STATUS_IS_OK(status
)) {
307 DEBUG(0, ("print_spool_terminate: "
308 "Failed to get spoolss pipe [%s]\n",
313 status
= rpccli_spoolss_SetJob(cli
, print_file
,
316 NULL
, SPOOLSS_JOB_CONTROL_DELETE
,
318 if (!NT_STATUS_IS_OK(status
) ||
319 !NT_STATUS_IS_OK(status
= werror_to_ntstatus(werr
))) {
320 DEBUG(3, ("Failed to delete job %d [%s]\n",
321 print_file
->jobid
, nt_errstr(status
)));
324 status
= rpccli_spoolss_ClosePrinter(cli
, print_file
,
327 if (!NT_STATUS_IS_OK(status
) ||
328 !NT_STATUS_IS_OK(status
= werror_to_ntstatus(werr
))) {
329 DEBUG(3, ("Failed to close printer %s [%s]\n",
330 print_file
->svcname
, nt_errstr(status
)));