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"
26 void print_spool_terminate(struct connection_struct *conn,
27 struct print_file_data *print_file);
29 /***************************************************************************
30 * Open a Document over spoolss
31 ***************************************************************************/
33 #define DOCNAME_DEFAULT "Remote Downlevel Document"
34 #ifndef PRINT_SPOOL_PREFIX
35 #define PRINT_SPOOL_PREFIX "smbprn."
38 NTSTATUS print_spool_open(files_struct *fsp,
40 uint16_t current_vuid)
44 struct print_file_data *pf;
45 struct rpc_pipe_client *cli;
46 struct spoolss_DevmodeContainer devmode_ctr;
47 union spoolss_DocumentInfo info;
51 tmp_ctx = talloc_new(fsp);
53 return NT_STATUS_NO_MEMORY;
56 pf = talloc_zero(fsp, struct print_file_data);
58 status = NT_STATUS_NO_MEMORY;
61 pf->svcname = talloc_strdup(pf, lp_servicename(SNUM(fsp->conn)));
63 /* the document name is derived from the file name.
64 * "Remote Downlevel Document" is added in front to
65 * mimic what windows does in this case */
66 pf->docname = talloc_strdup(pf, DOCNAME_DEFAULT);
68 status = NT_STATUS_NO_MEMORY;
72 const char *p = strrchr(fname, '/');
76 pf->docname = talloc_asprintf_append(pf->docname, " %s", p);
78 status = NT_STATUS_NO_MEMORY;
83 /* Ok, now we have to open an actual file.
85 * We want to write the spool job to this file in
86 * smbd for scalability reason (and also because
87 * apparently window printer drivers can seek when
88 * spooling to a file).
89 * So we first create a file, and then we pass it
90 * to spoolss in output_file so it can monitor and
91 * take over once we call EndDocPrinter().
92 * Of course we will not start writing until
93 * StartDocPrinter() actually gives the ok. */
95 pf->filename = talloc_asprintf(pf, "%s/%s.XXXXXX",
96 lp_pathname(SNUM(fsp->conn)),
99 status = NT_STATUS_NO_MEMORY;
103 fd = mkstemp(pf->filename);
105 if (errno == EACCES) {
106 /* Common setup error, force a report. */
107 DEBUG(0, ("Insufficient permissions "
108 "to open spool file %s.\n",
111 /* Normal case, report at level 3 and above. */
112 DEBUG(3, ("can't open spool file %s,\n",
114 DEBUGADD(3, ("errno = %d (%s).\n",
115 errno, strerror(errno)));
117 status = map_nt_error_from_unix(errno);
121 /* now open a document over spoolss so that it does
122 * all printer verification, and eventually assigns
125 status = rpc_pipe_open_interface(fsp->conn,
126 &ndr_table_spoolss.syntax_id,
127 fsp->conn->server_info,
128 &fsp->conn->sconn->client_id,
129 fsp->conn->sconn->msg_ctx,
130 &fsp->conn->spoolss_pipe);
131 if (!NT_STATUS_IS_OK(status)) {
134 cli = fsp->conn->spoolss_pipe;
136 ZERO_STRUCT(devmode_ctr);
138 status = rpccli_spoolss_OpenPrinter(cli, pf, pf->svcname,
140 SEC_FLAG_MAXIMUM_ALLOWED,
142 if (!NT_STATUS_IS_OK(status)) {
145 if (!W_ERROR_IS_OK(werr)) {
146 status = werror_to_ntstatus(werr);
150 info.info1 = talloc(tmp_ctx, struct spoolss_DocumentInfo1);
152 status = NT_STATUS_NO_MEMORY;
155 info.info1->document_name = pf->docname;
156 info.info1->output_file = pf->filename;
157 info.info1->datatype = "RAW";
159 status = rpccli_spoolss_StartDocPrinter(cli, tmp_ctx, &pf->handle,
160 1, info, &pf->jobid, &werr);
161 if (!NT_STATUS_IS_OK(status)) {
164 if (!W_ERROR_IS_OK(werr)) {
165 status = werror_to_ntstatus(werr);
169 /* Convert to RAP id. */
170 pf->rap_jobid = pjobid_to_rap(pf->svcname, pf->jobid);
171 if (pf->rap_jobid == 0) {
172 /* No errno around here */
173 status = NT_STATUS_ACCESS_DENIED;
177 /* setup a full fsp */
178 status = create_synthetic_smb_fname(fsp, pf->filename, NULL,
179 NULL, &fsp->fsp_name);
180 if (!NT_STATUS_IS_OK(status)) {
184 if (sys_fstat(fd, &fsp->fsp_name->st, false) != 0) {
185 status = map_nt_error_from_unix(errno);
189 fsp->file_id = vfs_file_id_from_sbuf(fsp->conn, &fsp->fsp_name->st);
190 fsp->mode = fsp->fsp_name->st.st_ex_mode;
193 fsp->vuid = current_vuid;
194 fsp->can_lock = false;
195 fsp->can_read = false;
196 fsp->access_mask = FILE_GENERIC_WRITE;
197 fsp->can_write = true;
198 fsp->modified = false;
199 fsp->oplock_type = NO_OPLOCK;
200 fsp->sent_oplock_break = NO_BREAK_SENT;
201 fsp->is_directory = false;
203 fsp->print_file = pf;
205 status = NT_STATUS_OK;
207 if (!NT_STATUS_IS_OK(status)) {
210 unlink(fsp->print_file->filename);
212 /* We need to delete the job from spoolss too */
214 print_spool_terminate(fsp->conn, pf);
217 talloc_free(tmp_ctx);
221 int print_spool_write(files_struct *fsp,
222 const char *data, uint32_t size,
223 SMB_OFF_T offset, uint32_t *written)
231 /* first of all stat file to find out if it is still there.
232 * spoolss may have deleted it to signal someone has killed
233 * the job through it's interface */
235 if (sys_fstat(fsp->fh->fd, &st, false) != 0) {
237 DEBUG(3, ("printfile_offset: sys_fstat failed on %s (%s)\n",
238 fsp_str_dbg(fsp), strerror(ret)));
242 /* check if the file is unlinked, this will signal spoolss has
243 * killed it, just return an error and close the file */
244 if (st.st_ex_nlink == 0) {
249 /* When print files go beyond 4GB, the 32-bit offset sent in
250 * old SMBwrite calls is relative to the current 4GB chunk
252 * Discovered by Sebastian Kloska <oncaphillis@snafu.de>.
254 if (offset < 0xffffffff00000000LL) {
255 offset = (st.st_ex_size & 0xffffffff00000000LL) + offset;
258 n = write_data_at_offset(fsp->fh->fd, data, size, offset);
261 print_spool_terminate(fsp->conn, fsp->print_file);
270 void print_spool_end(files_struct *fsp, enum file_close_type close_type)
272 struct rpc_pipe_client *cli;
276 status = rpc_pipe_open_interface(fsp->conn,
277 &ndr_table_spoolss.syntax_id,
278 fsp->conn->server_info,
279 &fsp->conn->sconn->client_id,
280 fsp->conn->sconn->msg_ctx,
281 &fsp->conn->spoolss_pipe);
282 if (!NT_STATUS_IS_OK(status)) {
283 DEBUG(0, ("print_spool_end: "
284 "Failed to get spoolss pipe [%s]\n",
288 cli = fsp->conn->spoolss_pipe;
290 switch (close_type) {
293 /* this also automatically calls spoolss_EndDocPrinter */
294 status = rpccli_spoolss_ClosePrinter(cli, fsp->print_file,
295 &fsp->print_file->handle,
297 if (!NT_STATUS_IS_OK(status) ||
298 !NT_STATUS_IS_OK(status = werror_to_ntstatus(werr))) {
299 DEBUG(3, ("Failed to close printer %s [%s]\n",
300 fsp->print_file->svcname, nt_errstr(status)));
304 print_spool_terminate(fsp->conn, fsp->print_file);
310 void print_spool_terminate(struct connection_struct *conn,
311 struct print_file_data *print_file)
313 struct rpc_pipe_client *cli;
317 rap_jobid_delete(print_file->svcname, print_file->jobid);
319 status = rpc_pipe_open_interface(conn,
320 &ndr_table_spoolss.syntax_id,
322 &conn->sconn->client_id,
323 conn->sconn->msg_ctx,
324 &conn->spoolss_pipe);
325 if (!NT_STATUS_IS_OK(status)) {
326 DEBUG(0, ("print_spool_terminate: "
327 "Failed to get spoolss pipe [%s]\n",
331 cli = conn->spoolss_pipe;
333 status = rpccli_spoolss_SetJob(cli, print_file,
336 NULL, SPOOLSS_JOB_CONTROL_DELETE,
338 if (!NT_STATUS_IS_OK(status) ||
339 !NT_STATUS_IS_OK(status = werror_to_ntstatus(werr))) {
340 DEBUG(3, ("Failed to delete job %d [%s]\n",
341 print_file->jobid, nt_errstr(status)));
344 status = rpccli_spoolss_ClosePrinter(cli, print_file,
347 if (!NT_STATUS_IS_OK(status) ||
348 !NT_STATUS_IS_OK(status = werror_to_ntstatus(werr))) {
349 DEBUG(3, ("Failed to close printer %s [%s]\n",
350 print_file->svcname, nt_errstr(status)));