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/smbd.h"
26 #include "smbd/globals.h"
27 #include "../libcli/security/security.h"
29 void print_spool_terminate(struct connection_struct *conn,
30 struct print_file_data *print_file);
32 /***************************************************************************
33 * Open a Document over spoolss
34 ***************************************************************************/
36 #define DOCNAME_DEFAULT "Remote Downlevel Document"
37 #ifndef PRINT_SPOOL_PREFIX
38 #define PRINT_SPOOL_PREFIX "smbprn."
41 NTSTATUS print_spool_open(files_struct *fsp,
43 uint16_t current_vuid)
47 struct print_file_data *pf;
48 struct dcerpc_binding_handle *b = NULL;
49 struct spoolss_DevmodeContainer devmode_ctr;
50 union spoolss_DocumentInfo info;
54 tmp_ctx = talloc_new(fsp);
56 return NT_STATUS_NO_MEMORY;
59 pf = talloc_zero(fsp, struct print_file_data);
61 status = NT_STATUS_NO_MEMORY;
64 pf->svcname = talloc_strdup(pf, lp_servicename(SNUM(fsp->conn)));
66 /* the document name is derived from the file name.
67 * "Remote Downlevel Document" is added in front to
68 * mimic what windows does in this case */
69 pf->docname = talloc_strdup(pf, DOCNAME_DEFAULT);
71 status = NT_STATUS_NO_MEMORY;
75 const char *p = strrchr(fname, '/');
79 pf->docname = talloc_asprintf_append(pf->docname, " %s", p);
81 status = NT_STATUS_NO_MEMORY;
86 /* Ok, now we have to open an actual file.
88 * We want to write the spool job to this file in
89 * smbd for scalability reason (and also because
90 * apparently window printer drivers can seek when
91 * spooling to a file).
92 * So we first create a file, and then we pass it
93 * to spoolss in output_file so it can monitor and
94 * take over once we call EndDocPrinter().
95 * Of course we will not start writing until
96 * StartDocPrinter() actually gives the ok. */
98 pf->filename = talloc_asprintf(pf, "%s/%s.XXXXXX",
99 lp_pathname(SNUM(fsp->conn)),
102 status = NT_STATUS_NO_MEMORY;
106 fd = mkstemp(pf->filename);
108 if (errno == EACCES) {
109 /* Common setup error, force a report. */
110 DEBUG(0, ("Insufficient permissions "
111 "to open spool file %s.\n",
114 /* Normal case, report at level 3 and above. */
115 DEBUG(3, ("can't open spool file %s,\n",
117 DEBUGADD(3, ("errno = %d (%s).\n",
118 errno, strerror(errno)));
120 status = map_nt_error_from_unix(errno);
124 /* now open a document over spoolss so that it does
125 * all printer verification, and eventually assigns
128 status = rpc_pipe_open_interface(fsp->conn,
129 &ndr_table_spoolss.syntax_id,
130 fsp->conn->session_info,
131 &fsp->conn->sconn->client_id,
132 fsp->conn->sconn->msg_ctx,
133 &fsp->conn->spoolss_pipe);
134 if (!NT_STATUS_IS_OK(status)) {
137 b = fsp->conn->spoolss_pipe->binding_handle;
139 ZERO_STRUCT(devmode_ctr);
141 status = dcerpc_spoolss_OpenPrinter(b, pf, pf->svcname,
143 SEC_FLAG_MAXIMUM_ALLOWED,
145 if (!NT_STATUS_IS_OK(status)) {
148 if (!W_ERROR_IS_OK(werr)) {
149 status = werror_to_ntstatus(werr);
153 info.info1 = talloc(tmp_ctx, struct spoolss_DocumentInfo1);
155 status = NT_STATUS_NO_MEMORY;
158 info.info1->document_name = pf->docname;
159 info.info1->output_file = pf->filename;
160 info.info1->datatype = "RAW";
162 status = dcerpc_spoolss_StartDocPrinter(b, tmp_ctx, &pf->handle,
163 1, info, &pf->jobid, &werr);
164 if (!NT_STATUS_IS_OK(status)) {
167 if (!W_ERROR_IS_OK(werr)) {
168 status = werror_to_ntstatus(werr);
172 /* Convert to RAP id. */
173 pf->rap_jobid = pjobid_to_rap(pf->svcname, pf->jobid);
174 if (pf->rap_jobid == 0) {
175 /* No errno around here */
176 status = NT_STATUS_ACCESS_DENIED;
180 /* setup a full fsp */
181 status = create_synthetic_smb_fname(fsp, pf->filename, NULL,
182 NULL, &fsp->fsp_name);
183 if (!NT_STATUS_IS_OK(status)) {
187 if (sys_fstat(fd, &fsp->fsp_name->st, false) != 0) {
188 status = map_nt_error_from_unix(errno);
192 fsp->file_id = vfs_file_id_from_sbuf(fsp->conn, &fsp->fsp_name->st);
193 fsp->mode = fsp->fsp_name->st.st_ex_mode;
196 fsp->vuid = current_vuid;
197 fsp->can_lock = false;
198 fsp->can_read = false;
199 fsp->access_mask = FILE_GENERIC_WRITE;
200 fsp->can_write = true;
201 fsp->modified = false;
202 fsp->oplock_type = NO_OPLOCK;
203 fsp->sent_oplock_break = NO_BREAK_SENT;
204 fsp->is_directory = false;
206 fsp->print_file = pf;
208 status = NT_STATUS_OK;
210 if (!NT_STATUS_IS_OK(status)) {
213 if (fsp->print_file) {
214 unlink(fsp->print_file->filename);
217 /* We need to delete the job from spoolss too */
219 print_spool_terminate(fsp->conn, pf);
222 talloc_free(tmp_ctx);
226 int print_spool_write(files_struct *fsp,
227 const char *data, uint32_t size,
228 SMB_OFF_T offset, uint32_t *written)
236 /* first of all stat file to find out if it is still there.
237 * spoolss may have deleted it to signal someone has killed
238 * the job through it's interface */
240 if (sys_fstat(fsp->fh->fd, &st, false) != 0) {
242 DEBUG(3, ("printfile_offset: sys_fstat failed on %s (%s)\n",
243 fsp_str_dbg(fsp), strerror(ret)));
247 /* check if the file is unlinked, this will signal spoolss has
248 * killed it, just return an error and close the file */
249 if (st.st_ex_nlink == 0) {
254 /* When print files go beyond 4GB, the 32-bit offset sent in
255 * old SMBwrite calls is relative to the current 4GB chunk
257 * Discovered by Sebastian Kloska <oncaphillis@snafu.de>.
259 if (offset < 0xffffffff00000000LL) {
260 offset = (st.st_ex_size & 0xffffffff00000000LL) + offset;
263 n = write_data_at_offset(fsp->fh->fd, data, size, offset);
266 print_spool_terminate(fsp->conn, fsp->print_file);
275 void print_spool_end(files_struct *fsp, enum file_close_type close_type)
279 struct dcerpc_binding_handle *b = NULL;
281 b = fsp->conn->spoolss_pipe->binding_handle;
283 switch (close_type) {
286 /* this also automatically calls spoolss_EndDocPrinter */
287 status = dcerpc_spoolss_ClosePrinter(b, fsp->print_file,
288 &fsp->print_file->handle,
290 if (!NT_STATUS_IS_OK(status) ||
291 !NT_STATUS_IS_OK(status = werror_to_ntstatus(werr))) {
292 DEBUG(3, ("Failed to close printer %s [%s]\n",
293 fsp->print_file->svcname, nt_errstr(status)));
297 print_spool_terminate(fsp->conn, fsp->print_file);
303 void print_spool_terminate(struct connection_struct *conn,
304 struct print_file_data *print_file)
308 struct dcerpc_binding_handle *b = NULL;
310 rap_jobid_delete(print_file->svcname, print_file->jobid);
312 status = rpc_pipe_open_interface(conn,
313 &ndr_table_spoolss.syntax_id,
315 &conn->sconn->client_id,
316 conn->sconn->msg_ctx,
317 &conn->spoolss_pipe);
318 if (!NT_STATUS_IS_OK(status)) {
319 DEBUG(0, ("print_spool_terminate: "
320 "Failed to get spoolss pipe [%s]\n",
324 b = conn->spoolss_pipe->binding_handle;
326 status = dcerpc_spoolss_SetJob(b, print_file,
329 NULL, SPOOLSS_JOB_CONTROL_DELETE,
331 if (!NT_STATUS_IS_OK(status) ||
332 !NT_STATUS_IS_OK(status = werror_to_ntstatus(werr))) {
333 DEBUG(3, ("Failed to delete job %d [%s]\n",
334 print_file->jobid, nt_errstr(status)));
337 status = dcerpc_spoolss_ClosePrinter(b, print_file,
340 if (!NT_STATUS_IS_OK(status) ||
341 !NT_STATUS_IS_OK(status = werror_to_ntstatus(werr))) {
342 DEBUG(3, ("Failed to close printer %s [%s]\n",
343 print_file->svcname, nt_errstr(status)));