s3:idmap_ad: add support for ADS_AUTH_SASL_{STARTTLS,LDAPS}
[Samba.git] / source3 / printing / printspoolss.c
blob94404f7682ab66147774ffe01fb3e07659fd2a48
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;
247 fsp->fsp_flags.is_fsa = true;
249 fsp->print_file = pf;
251 status = NT_STATUS_OK;
252 done:
253 if (!NT_STATUS_IS_OK(status)) {
254 if (fd != -1) {
255 close(fd);
256 if (fsp->print_file) {
257 unlink(fsp->print_file->filename);
260 /* We need to delete the job from spoolss too */
261 if (pf && pf->jobid) {
262 print_spool_terminate(fsp->conn, pf);
265 talloc_free(tmp_ctx);
266 return status;
269 int print_spool_write(files_struct *fsp,
270 const char *data, uint32_t size,
271 off_t offset, uint32_t *written)
273 SMB_STRUCT_STAT st;
274 ssize_t n;
275 int ret;
277 *written = 0;
279 /* first of all stat file to find out if it is still there.
280 * spoolss may have deleted it to signal someone has killed
281 * the job through it's interface */
283 if (sys_fstat(fsp_get_io_fd(fsp), &st, false) != 0) {
284 ret = errno;
285 DEBUG(3, ("printfile_offset: sys_fstat failed on %s (%s)\n",
286 fsp_str_dbg(fsp), strerror(ret)));
287 return ret;
290 /* check if the file is unlinked, this will signal spoolss has
291 * killed it, just return an error and close the file */
292 if (st.st_ex_nlink == 0) {
293 close(fsp_get_io_fd(fsp));
294 return EBADF;
297 /* When print files go beyond 4GB, the 32-bit offset sent in
298 * old SMBwrite calls is relative to the current 4GB chunk
299 * we're writing to.
300 * Discovered by Sebastian Kloska <oncaphillis@snafu.de>.
302 if (offset < 0xffffffff00000000LL) {
303 offset = (st.st_ex_size & 0xffffffff00000000LL) + offset;
306 n = write_data_at_offset(fsp_get_io_fd(fsp), data, size, offset);
307 if (n == -1) {
308 ret = errno;
309 print_spool_terminate(fsp->conn, fsp->print_file);
310 } else {
311 *written = n;
312 ret = 0;
315 return ret;
318 void print_spool_end(files_struct *fsp, enum file_close_type close_type)
320 NTSTATUS status;
321 WERROR werr;
322 struct dcerpc_binding_handle *b = NULL;
324 if (fsp->fsp_flags.delete_on_close) {
325 int ret;
328 * Job was requested to be cancelled by setting
329 * delete on close so truncate the job file.
330 * print_job_end() which is called from
331 * _spoolss_EndDocPrinter() will take
332 * care of deleting it for us.
334 ret = ftruncate(fsp_get_io_fd(fsp), 0);
335 if (ret == -1) {
336 DBG_ERR("ftruncate failed: %s\n", strerror(errno));
340 b = fsp->conn->spoolss_pipe->binding_handle;
342 switch (close_type) {
343 case NORMAL_CLOSE:
344 case SHUTDOWN_CLOSE:
345 /* this also automatically calls spoolss_EndDocPrinter */
346 status = dcerpc_spoolss_ClosePrinter(b, fsp->print_file,
347 &fsp->print_file->handle,
348 &werr);
349 if (!NT_STATUS_IS_OK(status) ||
350 !NT_STATUS_IS_OK(status = werror_to_ntstatus(werr))) {
351 DEBUG(3, ("Failed to close printer %s [%s]\n",
352 fsp->print_file->svcname, nt_errstr(status)));
354 break;
355 case ERROR_CLOSE:
356 print_spool_terminate(fsp->conn, fsp->print_file);
357 break;
362 void print_spool_terminate(struct connection_struct *conn,
363 struct print_file_data *print_file)
365 NTSTATUS status;
366 WERROR werr;
367 struct dcerpc_binding_handle *b = NULL;
369 rap_jobid_delete(print_file->svcname, print_file->jobid);
371 status = rpc_pipe_open_interface(conn,
372 &ndr_table_spoolss,
373 conn->session_info,
374 conn->sconn->remote_address,
375 conn->sconn->local_address,
376 conn->sconn->msg_ctx,
377 &conn->spoolss_pipe);
378 if (!NT_STATUS_IS_OK(status)) {
379 DEBUG(0, ("print_spool_terminate: "
380 "Failed to get spoolss pipe [%s]\n",
381 nt_errstr(status)));
382 return;
384 b = conn->spoolss_pipe->binding_handle;
386 status = dcerpc_spoolss_SetJob(b, print_file,
387 &print_file->handle,
388 print_file->jobid,
389 NULL, SPOOLSS_JOB_CONTROL_DELETE,
390 &werr);
391 if (!NT_STATUS_IS_OK(status) ||
392 !NT_STATUS_IS_OK(status = werror_to_ntstatus(werr))) {
393 DEBUG(3, ("Failed to delete job %d [%s]\n",
394 print_file->jobid, nt_errstr(status)));
395 return;
397 status = dcerpc_spoolss_ClosePrinter(b, print_file,
398 &print_file->handle,
399 &werr);
400 if (!NT_STATUS_IS_OK(status) ||
401 !NT_STATUS_IS_OK(status = werror_to_ntstatus(werr))) {
402 DEBUG(3, ("Failed to close printer %s [%s]\n",
403 print_file->svcname, nt_errstr(status)));
404 return;