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 "smbd/globals.h"
25 void print_spool_terminate(struct connection_struct *conn,
26 struct print_file_data *print_file);
28 /***************************************************************************
29 * Open a Document over spoolss
30 ***************************************************************************/
32 #define DOCNAME_DEFAULT "Remote Downlevel Document"
33 #ifndef PRINT_SPOOL_PREFIX
34 #define PRINT_SPOOL_PREFIX "smbprn."
37 NTSTATUS print_spool_open(files_struct *fsp,
39 uint16_t current_vuid)
43 struct print_file_data *pf;
44 struct rpc_pipe_client *cli;
45 struct spoolss_DevmodeContainer devmode_ctr;
46 union spoolss_DocumentInfo info;
50 tmp_ctx = talloc_new(fsp);
52 return NT_STATUS_NO_MEMORY;
55 pf = talloc_zero(fsp, struct print_file_data);
57 status = NT_STATUS_NO_MEMORY;
60 pf->svcname = talloc_strdup(pf, lp_servicename(SNUM(fsp->conn)));
62 /* the document name is derived from the file name.
63 * "Remote Downlevel Document" is added in front to
64 * mimic what windows does in this case */
65 pf->docname = talloc_strdup(pf, DOCNAME_DEFAULT);
67 status = NT_STATUS_NO_MEMORY;
71 const char *p = strrchr(fname, '/');
75 pf->docname = talloc_asprintf_append(pf->docname, " %s", p);
77 status = NT_STATUS_NO_MEMORY;
82 /* Ok, now we have to open an actual file.
84 * We want to write the spool job to this file in
85 * smbd for scalability reason (and also because
86 * apparently window printer drivers can seek when
87 * spooling to a file).
88 * So we first create a file, and then we pass it
89 * to spoolss in output_file so it can monitor and
90 * take over once we call EndDocPrinter().
91 * Of course we will not start writing until
92 * StartDocPrinter() actually gives the ok. */
94 pf->filename = talloc_asprintf(pf, "%s/%s.XXXXXX",
95 lp_pathname(SNUM(fsp->conn)),
98 status = NT_STATUS_NO_MEMORY;
102 fd = mkstemp(pf->filename);
104 if (errno == EACCES) {
105 /* Common setup error, force a report. */
106 DEBUG(0, ("Insufficient permissions "
107 "to open spool file %s.\n",
110 /* Normal case, report at level 3 and above. */
111 DEBUG(3, ("can't open spool file %s,\n",
113 DEBUGADD(3, ("errno = %d (%s).\n",
114 errno, strerror(errno)));
116 status = map_nt_error_from_unix(errno);
120 /* now open a document over spoolss so that it does
121 * all printer verification, and eventually assigns
124 status = rpc_pipe_open_interface(fsp->conn,
125 &ndr_table_spoolss.syntax_id,
126 fsp->conn->server_info,
127 &fsp->conn->sconn->client_id,
128 fsp->conn->sconn->msg_ctx,
129 &fsp->conn->spoolss_pipe);
130 if (!NT_STATUS_IS_OK(status)) {
133 cli = fsp->conn->spoolss_pipe;
135 ZERO_STRUCT(devmode_ctr);
137 status = rpccli_spoolss_OpenPrinter(cli, pf, pf->svcname,
139 SEC_FLAG_MAXIMUM_ALLOWED,
141 if (!NT_STATUS_IS_OK(status)) {
144 if (!W_ERROR_IS_OK(werr)) {
145 status = werror_to_ntstatus(werr);
149 info.info1 = talloc(tmp_ctx, struct spoolss_DocumentInfo1);
151 status = NT_STATUS_NO_MEMORY;
154 info.info1->document_name = pf->docname;
155 info.info1->output_file = pf->filename;
156 info.info1->datatype = "RAW";
158 status = rpccli_spoolss_StartDocPrinter(cli, tmp_ctx, &pf->handle,
159 1, info, &pf->jobid, &werr);
160 if (!NT_STATUS_IS_OK(status)) {
163 if (!W_ERROR_IS_OK(werr)) {
164 status = werror_to_ntstatus(werr);
168 /* Convert to RAP id. */
169 pf->rap_jobid = pjobid_to_rap(pf->svcname, pf->jobid);
170 if (pf->rap_jobid == 0) {
171 /* No errno around here */
172 status = NT_STATUS_ACCESS_DENIED;
176 /* setup a full fsp */
177 status = create_synthetic_smb_fname(fsp, pf->filename, NULL,
178 NULL, &fsp->fsp_name);
179 if (!NT_STATUS_IS_OK(status)) {
183 if (sys_fstat(fd, &fsp->fsp_name->st, false) != 0) {
184 status = map_nt_error_from_unix(errno);
188 fsp->file_id = vfs_file_id_from_sbuf(fsp->conn, &fsp->fsp_name->st);
189 fsp->mode = fsp->fsp_name->st.st_ex_mode;
192 fsp->vuid = current_vuid;
193 fsp->can_lock = false;
194 fsp->can_read = false;
195 fsp->access_mask = FILE_GENERIC_WRITE;
196 fsp->can_write = true;
197 fsp->modified = false;
198 fsp->oplock_type = NO_OPLOCK;
199 fsp->sent_oplock_break = NO_BREAK_SENT;
200 fsp->is_directory = false;
202 fsp->print_file = pf;
204 status = NT_STATUS_OK;
206 if (!NT_STATUS_IS_OK(status)) {
209 unlink(fsp->print_file->filename);
211 /* We need to delete the job from spoolss too */
213 print_spool_terminate(fsp->conn, pf);
216 talloc_free(tmp_ctx);
220 int print_spool_write(files_struct *fsp,
221 const char *data, uint32_t size,
222 SMB_OFF_T offset, uint32_t *written)
230 /* first of all stat file to find out if it is still there.
231 * spoolss may have deleted it to signal someone has killed
232 * the job through it's interface */
234 if (sys_fstat(fsp->fh->fd, &st, false) != 0) {
236 DEBUG(3, ("printfile_offset: sys_fstat failed on %s (%s)\n",
237 fsp_str_dbg(fsp), strerror(ret)));
241 /* check if the file is unlinked, this will signal spoolss has
242 * killed it, just return an error and close the file */
243 if (st.st_ex_nlink == 0) {
248 /* When print files go beyond 4GB, the 32-bit offset sent in
249 * old SMBwrite calls is relative to the current 4GB chunk
251 * Discovered by Sebastian Kloska <oncaphillis@snafu.de>.
253 if (offset < 0xffffffff00000000LL) {
254 offset = (st.st_ex_size & 0xffffffff00000000LL) + offset;
257 n = write_data_at_offset(fsp->fh->fd, data, size, offset);
260 print_spool_terminate(fsp->conn, fsp->print_file);
269 void print_spool_end(files_struct *fsp, enum file_close_type close_type)
271 struct rpc_pipe_client *cli;
275 status = rpc_pipe_open_interface(fsp->conn,
276 &ndr_table_spoolss.syntax_id,
277 fsp->conn->server_info,
278 &fsp->conn->sconn->client_id,
279 fsp->conn->sconn->msg_ctx,
280 &fsp->conn->spoolss_pipe);
281 if (!NT_STATUS_IS_OK(status)) {
282 DEBUG(0, ("print_spool_end: "
283 "Failed to get spoolss pipe [%s]\n",
287 cli = fsp->conn->spoolss_pipe;
289 switch (close_type) {
292 /* this also automatically calls spoolss_EndDocPrinter */
293 status = rpccli_spoolss_ClosePrinter(cli, fsp->print_file,
294 &fsp->print_file->handle,
296 if (!NT_STATUS_IS_OK(status) ||
297 !NT_STATUS_IS_OK(status = werror_to_ntstatus(werr))) {
298 DEBUG(3, ("Failed to close printer %s [%s]\n",
299 fsp->print_file->svcname, nt_errstr(status)));
303 print_spool_terminate(fsp->conn, fsp->print_file);
309 void print_spool_terminate(struct connection_struct *conn,
310 struct print_file_data *print_file)
312 struct rpc_pipe_client *cli;
316 rap_jobid_delete(print_file->svcname, print_file->jobid);
318 status = rpc_pipe_open_interface(conn,
319 &ndr_table_spoolss.syntax_id,
321 &conn->sconn->client_id,
322 conn->sconn->msg_ctx,
323 &conn->spoolss_pipe);
324 if (!NT_STATUS_IS_OK(status)) {
325 DEBUG(0, ("print_spool_terminate: "
326 "Failed to get spoolss pipe [%s]\n",
330 cli = &conn->spoolss_pipe;
332 status = rpccli_spoolss_SetJob(cli, print_file,
335 NULL, SPOOLSS_JOB_CONTROL_DELETE,
337 if (!NT_STATUS_IS_OK(status) ||
338 !NT_STATUS_IS_OK(status = werror_to_ntstatus(werr))) {
339 DEBUG(3, ("Failed to delete job %d [%s]\n",
340 print_file->jobid, nt_errstr(status)));
343 status = rpccli_spoolss_ClosePrinter(cli, print_file,
346 if (!NT_STATUS_IS_OK(status) ||
347 !NT_STATUS_IS_OK(status = werror_to_ntstatus(werr))) {
348 DEBUG(3, ("Failed to close printer %s [%s]\n",
349 print_file->svcname, nt_errstr(status)));