lib/util: add debug_set_forced_log_priority()
[Samba.git] / source3 / printing / printspoolss.c
blob31117a4b74389bcf0034231df722514dbbfa7a7b
1 /*
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/>.
20 #include "includes.h"
21 #include "system/filesys.h"
22 #include "printing.h"
23 #include "rpc_client/rpc_client.h"
24 #include "../librpc/gen_ndr/ndr_spoolss_c.h"
25 #include "rpc_server/rpc_ncacn_np.h"
26 #include "smbd/globals.h"
27 #include "../libcli/security/security.h"
28 #include "smbd/fd_handle.h"
29 #include "source3/printing/rap_jobid.h"
31 struct print_file_data {
32 char *svcname;
33 char *docname;
34 char *filename;
35 struct policy_handle handle;
36 uint32_t jobid;
37 uint16_t rap_jobid;
40 uint16_t print_spool_rap_jobid(struct print_file_data *print_file)
42 if (print_file == NULL) {
43 return 0;
46 return print_file->rap_jobid;
49 void print_spool_terminate(struct connection_struct *conn,
50 struct print_file_data *print_file);
52 /***************************************************************************
53 * Open a Document over spoolss
54 ***************************************************************************/
56 #define DOCNAME_DEFAULT "Remote Downlevel Document"
58 NTSTATUS print_spool_open(files_struct *fsp,
59 const char *fname,
60 uint64_t current_vuid)
62 const struct loadparm_substitution *lp_sub =
63 loadparm_s3_global_substitution();
64 NTSTATUS status;
65 TALLOC_CTX *tmp_ctx;
66 struct print_file_data *pf;
67 struct dcerpc_binding_handle *b = NULL;
68 struct spoolss_DevmodeContainer devmode_ctr;
69 struct spoolss_DocumentInfoCtr info_ctr;
70 struct spoolss_DocumentInfo1 *info1;
71 int fd = -1;
72 WERROR werr;
73 mode_t mask;
75 tmp_ctx = talloc_new(fsp);
76 if (!tmp_ctx) {
77 return NT_STATUS_NO_MEMORY;
80 pf = talloc_zero(fsp, struct print_file_data);
81 if (!pf) {
82 status = NT_STATUS_NO_MEMORY;
83 goto done;
85 pf->svcname = lp_servicename(pf, lp_sub, SNUM(fsp->conn));
87 /* the document name is derived from the file name.
88 * "Remote Downlevel Document" is added in front to
89 * mimic what windows does in this case */
90 pf->docname = talloc_strdup(pf, DOCNAME_DEFAULT);
91 if (!pf->docname) {
92 status = NT_STATUS_NO_MEMORY;
93 goto done;
95 if (fname) {
96 const char *p = strrchr(fname, '/');
97 if (!p) {
98 p = fname;
100 pf->docname = talloc_asprintf_append(pf->docname, " %s", p);
101 if (!pf->docname) {
102 status = NT_STATUS_NO_MEMORY;
103 goto done;
108 * Ok, now we have to open an actual file.
109 * Here is the reason:
110 * We want to write the spool job to this file in
111 * smbd for scalability reason (and also because
112 * apparently window printer drivers can seek when
113 * spooling to a file).
114 * So we first create a file, and then we pass it
115 * to spoolss in output_file so it can monitor and
116 * take over once we call EndDocPrinter().
117 * Of course we will not start writing until
118 * StartDocPrinter() actually gives the ok.
119 * smbd spooler files do not include a print jobid
120 * path component, as the jobid is only known after
121 * calling StartDocPrinter().
124 pf->filename = talloc_asprintf(pf, "%s/%sXXXXXX",
125 lp_path(talloc_tos(),
126 lp_sub,
127 SNUM(fsp->conn)),
128 PRINT_SPOOL_PREFIX);
129 if (!pf->filename) {
130 status = NT_STATUS_NO_MEMORY;
131 goto done;
133 errno = 0;
134 mask = umask(S_IRWXO | S_IRWXG);
135 fd = mkstemp(pf->filename);
136 umask(mask);
137 if (fd == -1) {
138 if (errno == EACCES) {
139 /* Common setup error, force a report. */
140 DEBUG(0, ("Insufficient permissions "
141 "to open spool file %s.\n",
142 pf->filename));
143 } else {
144 /* Normal case, report at level 3 and above. */
145 DEBUG(3, ("can't open spool file %s,\n",
146 pf->filename));
147 DEBUGADD(3, ("errno = %d (%s).\n",
148 errno, strerror(errno)));
150 status = map_nt_error_from_unix(errno);
151 goto done;
154 /* now open a document over spoolss so that it does
155 * all printer verification, and eventually assigns
156 * a job id */
158 status = rpc_pipe_open_interface(fsp->conn,
159 &ndr_table_spoolss,
160 fsp->conn->session_info,
161 fsp->conn->sconn->remote_address,
162 fsp->conn->sconn->local_address,
163 fsp->conn->sconn->msg_ctx,
164 &fsp->conn->spoolss_pipe);
165 if (!NT_STATUS_IS_OK(status)) {
166 goto done;
168 b = fsp->conn->spoolss_pipe->binding_handle;
170 ZERO_STRUCT(devmode_ctr);
172 status = dcerpc_spoolss_OpenPrinter(b, pf, pf->svcname,
173 "RAW", devmode_ctr,
174 PRINTER_ACCESS_USE,
175 &pf->handle, &werr);
176 if (!NT_STATUS_IS_OK(status)) {
177 goto done;
179 if (!W_ERROR_IS_OK(werr)) {
180 status = werror_to_ntstatus(werr);
181 goto done;
184 info1 = talloc(tmp_ctx, struct spoolss_DocumentInfo1);
185 if (info1 == NULL) {
186 status = NT_STATUS_NO_MEMORY;
187 goto done;
189 info1->document_name = pf->docname;
190 info1->output_file = pf->filename;
191 info1->datatype = "RAW";
193 info_ctr.level = 1;
194 info_ctr.info.info1 = info1;
196 status = dcerpc_spoolss_StartDocPrinter(b, tmp_ctx,
197 &pf->handle,
198 &info_ctr,
199 &pf->jobid,
200 &werr);
201 if (!NT_STATUS_IS_OK(status)) {
202 goto done;
204 if (!W_ERROR_IS_OK(werr)) {
205 status = werror_to_ntstatus(werr);
206 goto done;
209 /* Convert to RAP id. */
210 pf->rap_jobid = pjobid_to_rap(pf->svcname, pf->jobid);
211 if (pf->rap_jobid == 0) {
212 /* No errno around here */
213 status = NT_STATUS_ACCESS_DENIED;
214 goto done;
217 /* setup a full fsp */
218 fsp->fsp_name = synthetic_smb_fname(fsp,
219 pf->filename,
220 NULL,
221 NULL,
224 if (fsp->fsp_name == NULL) {
225 status = NT_STATUS_NO_MEMORY;
226 goto done;
229 if (sys_fstat(fd, &fsp->fsp_name->st, false) != 0) {
230 status = map_nt_error_from_unix(errno);
231 goto done;
234 fsp->file_id = vfs_file_id_from_sbuf(fsp->conn, &fsp->fsp_name->st);
235 fsp_set_fd(fsp, fd);
237 fsp->vuid = current_vuid;
238 fsp->fsp_flags.can_lock = false;
239 fsp->fsp_flags.can_read = false;
240 fsp->access_mask = FILE_GENERIC_WRITE;
241 fsp->fsp_flags.can_write = true;
242 fsp->fsp_flags.modified = false;
243 fsp->oplock_type = NO_OPLOCK;
244 fsp->sent_oplock_break = NO_BREAK_SENT;
245 fsp->fsp_flags.is_directory = false;
246 fsp->fsp_flags.delete_on_close = false;
248 fsp->print_file = pf;
250 status = NT_STATUS_OK;
251 done:
252 if (!NT_STATUS_IS_OK(status)) {
253 if (fd != -1) {
254 close(fd);
255 if (fsp->print_file) {
256 unlink(fsp->print_file->filename);
259 /* We need to delete the job from spoolss too */
260 if (pf && pf->jobid) {
261 print_spool_terminate(fsp->conn, pf);
264 talloc_free(tmp_ctx);
265 return status;
268 int print_spool_write(files_struct *fsp,
269 const char *data, uint32_t size,
270 off_t offset, uint32_t *written)
272 SMB_STRUCT_STAT st;
273 ssize_t n;
274 int ret;
276 *written = 0;
278 /* first of all stat file to find out if it is still there.
279 * spoolss may have deleted it to signal someone has killed
280 * the job through it's interface */
282 if (sys_fstat(fsp_get_io_fd(fsp), &st, false) != 0) {
283 ret = errno;
284 DEBUG(3, ("printfile_offset: sys_fstat failed on %s (%s)\n",
285 fsp_str_dbg(fsp), strerror(ret)));
286 return ret;
289 /* check if the file is unlinked, this will signal spoolss has
290 * killed it, just return an error and close the file */
291 if (st.st_ex_nlink == 0) {
292 close(fsp_get_io_fd(fsp));
293 return EBADF;
296 /* When print files go beyond 4GB, the 32-bit offset sent in
297 * old SMBwrite calls is relative to the current 4GB chunk
298 * we're writing to.
299 * Discovered by Sebastian Kloska <oncaphillis@snafu.de>.
301 if (offset < 0xffffffff00000000LL) {
302 offset = (st.st_ex_size & 0xffffffff00000000LL) + offset;
305 n = write_data_at_offset(fsp_get_io_fd(fsp), data, size, offset);
306 if (n == -1) {
307 ret = errno;
308 print_spool_terminate(fsp->conn, fsp->print_file);
309 } else {
310 *written = n;
311 ret = 0;
314 return ret;
317 void print_spool_end(files_struct *fsp, enum file_close_type close_type)
319 NTSTATUS status;
320 WERROR werr;
321 struct dcerpc_binding_handle *b = NULL;
323 if (fsp->fsp_flags.delete_on_close) {
324 int ret;
327 * Job was requested to be cancelled by setting
328 * delete on close so truncate the job file.
329 * print_job_end() which is called from
330 * _spoolss_EndDocPrinter() will take
331 * care of deleting it for us.
333 ret = ftruncate(fsp_get_io_fd(fsp), 0);
334 if (ret == -1) {
335 DBG_ERR("ftruncate failed: %s\n", strerror(errno));
339 b = fsp->conn->spoolss_pipe->binding_handle;
341 switch (close_type) {
342 case NORMAL_CLOSE:
343 case SHUTDOWN_CLOSE:
344 /* this also automatically calls spoolss_EndDocPrinter */
345 status = dcerpc_spoolss_ClosePrinter(b, fsp->print_file,
346 &fsp->print_file->handle,
347 &werr);
348 if (!NT_STATUS_IS_OK(status) ||
349 !NT_STATUS_IS_OK(status = werror_to_ntstatus(werr))) {
350 DEBUG(3, ("Failed to close printer %s [%s]\n",
351 fsp->print_file->svcname, nt_errstr(status)));
353 break;
354 case ERROR_CLOSE:
355 print_spool_terminate(fsp->conn, fsp->print_file);
356 break;
361 void print_spool_terminate(struct connection_struct *conn,
362 struct print_file_data *print_file)
364 NTSTATUS status;
365 WERROR werr;
366 struct dcerpc_binding_handle *b = NULL;
368 rap_jobid_delete(print_file->svcname, print_file->jobid);
370 status = rpc_pipe_open_interface(conn,
371 &ndr_table_spoolss,
372 conn->session_info,
373 conn->sconn->remote_address,
374 conn->sconn->local_address,
375 conn->sconn->msg_ctx,
376 &conn->spoolss_pipe);
377 if (!NT_STATUS_IS_OK(status)) {
378 DEBUG(0, ("print_spool_terminate: "
379 "Failed to get spoolss pipe [%s]\n",
380 nt_errstr(status)));
381 return;
383 b = conn->spoolss_pipe->binding_handle;
385 status = dcerpc_spoolss_SetJob(b, print_file,
386 &print_file->handle,
387 print_file->jobid,
388 NULL, SPOOLSS_JOB_CONTROL_DELETE,
389 &werr);
390 if (!NT_STATUS_IS_OK(status) ||
391 !NT_STATUS_IS_OK(status = werror_to_ntstatus(werr))) {
392 DEBUG(3, ("Failed to delete job %d [%s]\n",
393 print_file->jobid, nt_errstr(status)));
394 return;
396 status = dcerpc_spoolss_ClosePrinter(b, print_file,
397 &print_file->handle,
398 &werr);
399 if (!NT_STATUS_IS_OK(status) ||
400 !NT_STATUS_IS_OK(status = werror_to_ntstatus(werr))) {
401 DEBUG(3, ("Failed to close printer %s [%s]\n",
402 print_file->svcname, nt_errstr(status)));
403 return;