source: trunk/server/source3/printing/printspoolss.c@ 1037

Last change on this file since 1037 was 1037, checked in by Silvan Scherrer, 8 years ago

samba server: prevent extra LF in case of printing

File size: 9.7 KB
Line 
1/*
2 Unix SMB/CIFS implementation.
3 Printing routines that bridge to spoolss
4 Copyright (C) Simo Sorce 2010
5
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.
10
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.
15
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/>.
18*/
19
20#include "includes.h"
21#include "printing.h"
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"
27#ifdef __OS2__
28#include <sys/fcntl.h>
29#endif
30void print_spool_terminate(struct connection_struct *conn,
31 struct print_file_data *print_file);
32
33/***************************************************************************
34 * Open a Document over spoolss
35 ***************************************************************************/
36
37#define DOCNAME_DEFAULT "Remote Downlevel Document"
38#ifndef PRINT_SPOOL_PREFIX
39#define PRINT_SPOOL_PREFIX "smbprn."
40#endif
41
42NTSTATUS print_spool_open(files_struct *fsp,
43 const char *fname,
44 uint16_t current_vuid)
45{
46 NTSTATUS status;
47 TALLOC_CTX *tmp_ctx;
48 struct print_file_data *pf;
49 struct dcerpc_binding_handle *b = NULL;
50 struct spoolss_DevmodeContainer devmode_ctr;
51 union spoolss_DocumentInfo info;
52 int fd = -1;
53 WERROR werr;
54
55 tmp_ctx = talloc_new(fsp);
56 if (!tmp_ctx) {
57 return NT_STATUS_NO_MEMORY;
58 }
59
60 pf = talloc_zero(fsp, struct print_file_data);
61 if (!pf) {
62 status = NT_STATUS_NO_MEMORY;
63 goto done;
64 }
65 pf->svcname = talloc_strdup(pf, lp_servicename(SNUM(fsp->conn)));
66
67 /* the document name is derived from the file name.
68 * "Remote Downlevel Document" is added in front to
69 * mimic what windows does in this case */
70 pf->docname = talloc_strdup(pf, DOCNAME_DEFAULT);
71 if (!pf->docname) {
72 status = NT_STATUS_NO_MEMORY;
73 goto done;
74 }
75 if (fname) {
76 const char *p = strrchr(fname, '/');
77 if (!p) {
78 p = fname;
79 }
80 pf->docname = talloc_asprintf_append(pf->docname, " %s", p);
81 if (!pf->docname) {
82 status = NT_STATUS_NO_MEMORY;
83 goto done;
84 }
85 }
86
87 /*
88 * Ok, now we have to open an actual file.
89 * Here is the reason:
90 * We want to write the spool job to this file in
91 * smbd for scalability reason (and also because
92 * apparently window printer drivers can seek when
93 * spooling to a file).
94 * So we first create a file, and then we pass it
95 * to spoolss in output_file so it can monitor and
96 * take over once we call EndDocPrinter().
97 * Of course we will not start writing until
98 * StartDocPrinter() actually gives the ok.
99 * smbd spooler files do not include a print jobid
100 * path component, as the jobid is only known after
101 * calling StartDocPrinter().
102 */
103
104 pf->filename = talloc_asprintf(pf, "%s/%sXXXXXX",
105 lp_pathname(SNUM(fsp->conn)),
106 PRINT_SPOOL_PREFIX);
107 if (!pf->filename) {
108 status = NT_STATUS_NO_MEMORY;
109 goto done;
110 }
111 errno = 0;
112 fd = mkstemp(pf->filename);
113#ifdef __OS2__
114 if (fd >= 0)
115 setmode(fd, O_BINARY);
116#endif
117 if (fd == -1) {
118 if (errno == EACCES) {
119 /* Common setup error, force a report. */
120 DEBUG(0, ("Insufficient permissions "
121 "to open spool file %s.\n",
122 pf->filename));
123 } else {
124 /* Normal case, report at level 3 and above. */
125 DEBUG(3, ("can't open spool file %s,\n",
126 pf->filename));
127 DEBUGADD(3, ("errno = %d (%s).\n",
128 errno, strerror(errno)));
129 }
130 status = map_nt_error_from_unix(errno);
131 goto done;
132 }
133
134 /* now open a document over spoolss so that it does
135 * all printer verification, and eventually assigns
136 * a job id */
137
138 status = rpc_pipe_open_interface(fsp->conn,
139 &ndr_table_spoolss.syntax_id,
140 fsp->conn->session_info,
141 &fsp->conn->sconn->client_id,
142 fsp->conn->sconn->msg_ctx,
143 &fsp->conn->spoolss_pipe);
144 if (!NT_STATUS_IS_OK(status)) {
145 goto done;
146 }
147 b = fsp->conn->spoolss_pipe->binding_handle;
148
149 ZERO_STRUCT(devmode_ctr);
150
151 status = dcerpc_spoolss_OpenPrinter(b, pf, pf->svcname,
152 "RAW", devmode_ctr,
153 PRINTER_ACCESS_USE,
154 &pf->handle, &werr);
155 if (!NT_STATUS_IS_OK(status)) {
156 goto done;
157 }
158 if (!W_ERROR_IS_OK(werr)) {
159 status = werror_to_ntstatus(werr);
160 goto done;
161 }
162
163 info.info1 = talloc(tmp_ctx, struct spoolss_DocumentInfo1);
164 if (!info.info1) {
165 status = NT_STATUS_NO_MEMORY;
166 goto done;
167 }
168 info.info1->document_name = pf->docname;
169 info.info1->output_file = pf->filename;
170 info.info1->datatype = "RAW";
171
172 status = dcerpc_spoolss_StartDocPrinter(b, tmp_ctx, &pf->handle,
173 1, info, &pf->jobid, &werr);
174 if (!NT_STATUS_IS_OK(status)) {
175 goto done;
176 }
177 if (!W_ERROR_IS_OK(werr)) {
178 status = werror_to_ntstatus(werr);
179 goto done;
180 }
181
182 /* Convert to RAP id. */
183 pf->rap_jobid = pjobid_to_rap(pf->svcname, pf->jobid);
184 if (pf->rap_jobid == 0) {
185 /* No errno around here */
186 status = NT_STATUS_ACCESS_DENIED;
187 goto done;
188 }
189
190 /* setup a full fsp */
191 status = create_synthetic_smb_fname(fsp, pf->filename, NULL,
192 NULL, &fsp->fsp_name);
193 if (!NT_STATUS_IS_OK(status)) {
194 goto done;
195 }
196
197 if (sys_fstat(fd, &fsp->fsp_name->st, false) != 0) {
198 status = map_nt_error_from_unix(errno);
199 goto done;
200 }
201
202 fsp->file_id = vfs_file_id_from_sbuf(fsp->conn, &fsp->fsp_name->st);
203 fsp->mode = fsp->fsp_name->st.st_ex_mode;
204 fsp->fh->fd = fd;
205
206 fsp->vuid = current_vuid;
207 fsp->can_lock = false;
208 fsp->can_read = false;
209 fsp->access_mask = FILE_GENERIC_WRITE;
210 fsp->can_write = true;
211 fsp->modified = false;
212 fsp->oplock_type = NO_OPLOCK;
213 fsp->sent_oplock_break = NO_BREAK_SENT;
214 fsp->is_directory = false;
215
216 fsp->print_file = pf;
217
218 status = NT_STATUS_OK;
219done:
220 if (!NT_STATUS_IS_OK(status)) {
221 if (fd != -1) {
222 close(fd);
223 if (fsp->print_file) {
224 unlink(fsp->print_file->filename);
225 }
226 }
227 /* We need to delete the job from spoolss too */
228 if (pf->jobid) {
229 print_spool_terminate(fsp->conn, pf);
230 }
231 }
232 talloc_free(tmp_ctx);
233 return status;
234}
235
236int print_spool_write(files_struct *fsp,
237 const char *data, uint32_t size,
238 SMB_OFF_T offset, uint32_t *written)
239{
240 SMB_STRUCT_STAT st;
241 ssize_t n;
242 int ret;
243
244 *written = 0;
245
246 /* first of all stat file to find out if it is still there.
247 * spoolss may have deleted it to signal someone has killed
248 * the job through it's interface */
249
250 if (sys_fstat(fsp->fh->fd, &st, false) != 0) {
251 ret = errno;
252 DEBUG(3, ("printfile_offset: sys_fstat failed on %s (%s)\n",
253 fsp_str_dbg(fsp), strerror(ret)));
254 return ret;
255 }
256
257 /* check if the file is unlinked, this will signal spoolss has
258 * killed it, just return an error and close the file */
259 if (st.st_ex_nlink == 0) {
260 close(fsp->fh->fd);
261 return EBADF;
262 }
263
264 /* When print files go beyond 4GB, the 32-bit offset sent in
265 * old SMBwrite calls is relative to the current 4GB chunk
266 * we're writing to.
267 * Discovered by Sebastian Kloska <oncaphillis@snafu.de>.
268 */
269 if (offset < 0xffffffff00000000LL) {
270 offset = (st.st_ex_size & 0xffffffff00000000LL) + offset;
271 }
272
273 n = write_data_at_offset(fsp->fh->fd, data, size, offset);
274 if (n == -1) {
275 ret = errno;
276 print_spool_terminate(fsp->conn, fsp->print_file);
277 } else {
278 *written = n;
279 ret = 0;
280 }
281
282 return ret;
283}
284
285void print_spool_end(files_struct *fsp, enum file_close_type close_type)
286{
287 NTSTATUS status;
288 WERROR werr;
289 struct dcerpc_binding_handle *b = NULL;
290
291 b = fsp->conn->spoolss_pipe->binding_handle;
292
293 switch (close_type) {
294 case NORMAL_CLOSE:
295 case SHUTDOWN_CLOSE:
296 /* this also automatically calls spoolss_EndDocPrinter */
297 status = dcerpc_spoolss_ClosePrinter(b, fsp->print_file,
298 &fsp->print_file->handle,
299 &werr);
300 if (!NT_STATUS_IS_OK(status) ||
301 !NT_STATUS_IS_OK(status = werror_to_ntstatus(werr))) {
302 DEBUG(3, ("Failed to close printer %s [%s]\n",
303 fsp->print_file->svcname, nt_errstr(status)));
304 }
305 break;
306 case ERROR_CLOSE:
307 print_spool_terminate(fsp->conn, fsp->print_file);
308 break;
309 }
310}
311
312
313void print_spool_terminate(struct connection_struct *conn,
314 struct print_file_data *print_file)
315{
316 NTSTATUS status;
317 WERROR werr;
318 struct dcerpc_binding_handle *b = NULL;
319
320 rap_jobid_delete(print_file->svcname, print_file->jobid);
321
322 status = rpc_pipe_open_interface(conn,
323 &ndr_table_spoolss.syntax_id,
324 conn->session_info,
325 &conn->sconn->client_id,
326 conn->sconn->msg_ctx,
327 &conn->spoolss_pipe);
328 if (!NT_STATUS_IS_OK(status)) {
329 DEBUG(0, ("print_spool_terminate: "
330 "Failed to get spoolss pipe [%s]\n",
331 nt_errstr(status)));
332 return;
333 }
334 b = conn->spoolss_pipe->binding_handle;
335
336 status = dcerpc_spoolss_SetJob(b, print_file,
337 &print_file->handle,
338 print_file->jobid,
339 NULL, SPOOLSS_JOB_CONTROL_DELETE,
340 &werr);
341 if (!NT_STATUS_IS_OK(status) ||
342 !NT_STATUS_IS_OK(status = werror_to_ntstatus(werr))) {
343 DEBUG(3, ("Failed to delete job %d [%s]\n",
344 print_file->jobid, nt_errstr(status)));
345 return;
346 }
347 status = dcerpc_spoolss_ClosePrinter(b, print_file,
348 &print_file->handle,
349 &werr);
350 if (!NT_STATUS_IS_OK(status) ||
351 !NT_STATUS_IS_OK(status = werror_to_ntstatus(werr))) {
352 DEBUG(3, ("Failed to close printer %s [%s]\n",
353 print_file->svcname, nt_errstr(status)));
354 return;
355 }
356}
Note: See TracBrowser for help on using the repository browser.