dsdb-acl: introduce a 'msg' helper variable to acl_modify()
[Samba/gebeck_regimport.git] / source3 / printing / printspoolss.c
blob7a730a55bb0c407ffe94e364d27281cc656592f7
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 "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"
28 struct print_file_data {
29 char *svcname;
30 char *docname;
31 char *filename;
32 struct policy_handle handle;
33 uint32_t jobid;
34 uint16 rap_jobid;
37 uint16_t print_spool_rap_jobid(struct print_file_data *print_file)
39 if (print_file == NULL) {
40 return 0;
43 return print_file->rap_jobid;
46 void print_spool_terminate(struct connection_struct *conn,
47 struct print_file_data *print_file);
49 /***************************************************************************
50 * Open a Document over spoolss
51 ***************************************************************************/
53 #define DOCNAME_DEFAULT "Remote Downlevel Document"
54 #ifndef PRINT_SPOOL_PREFIX
55 #define PRINT_SPOOL_PREFIX "smbprn."
56 #endif
58 NTSTATUS print_spool_open(files_struct *fsp,
59 const char *fname,
60 uint64_t current_vuid)
62 NTSTATUS status;
63 TALLOC_CTX *tmp_ctx;
64 struct print_file_data *pf;
65 struct dcerpc_binding_handle *b = NULL;
66 struct spoolss_DevmodeContainer devmode_ctr;
67 union spoolss_DocumentInfo info;
68 int fd = -1;
69 WERROR werr;
71 tmp_ctx = talloc_new(fsp);
72 if (!tmp_ctx) {
73 return NT_STATUS_NO_MEMORY;
76 pf = talloc_zero(fsp, struct print_file_data);
77 if (!pf) {
78 status = NT_STATUS_NO_MEMORY;
79 goto done;
81 pf->svcname = lp_servicename(pf, SNUM(fsp->conn));
83 /* the document name is derived from the file name.
84 * "Remote Downlevel Document" is added in front to
85 * mimic what windows does in this case */
86 pf->docname = talloc_strdup(pf, DOCNAME_DEFAULT);
87 if (!pf->docname) {
88 status = NT_STATUS_NO_MEMORY;
89 goto done;
91 if (fname) {
92 const char *p = strrchr(fname, '/');
93 if (!p) {
94 p = fname;
96 pf->docname = talloc_asprintf_append(pf->docname, " %s", p);
97 if (!pf->docname) {
98 status = NT_STATUS_NO_MEMORY;
99 goto done;
104 * Ok, now we have to open an actual file.
105 * Here is the reason:
106 * We want to write the spool job to this file in
107 * smbd for scalability reason (and also because
108 * apparently window printer drivers can seek when
109 * spooling to a file).
110 * So we first create a file, and then we pass it
111 * to spoolss in output_file so it can monitor and
112 * take over once we call EndDocPrinter().
113 * Of course we will not start writing until
114 * StartDocPrinter() actually gives the ok.
115 * smbd spooler files do not include a print jobid
116 * path component, as the jobid is only known after
117 * calling StartDocPrinter().
120 pf->filename = talloc_asprintf(pf, "%s/%sXXXXXX",
121 lp_pathname(talloc_tos(),
122 SNUM(fsp->conn)),
123 PRINT_SPOOL_PREFIX);
124 if (!pf->filename) {
125 status = NT_STATUS_NO_MEMORY;
126 goto done;
128 errno = 0;
129 fd = mkstemp(pf->filename);
130 if (fd == -1) {
131 if (errno == EACCES) {
132 /* Common setup error, force a report. */
133 DEBUG(0, ("Insufficient permissions "
134 "to open spool file %s.\n",
135 pf->filename));
136 } else {
137 /* Normal case, report at level 3 and above. */
138 DEBUG(3, ("can't open spool file %s,\n",
139 pf->filename));
140 DEBUGADD(3, ("errno = %d (%s).\n",
141 errno, strerror(errno)));
143 status = map_nt_error_from_unix(errno);
144 goto done;
147 /* now open a document over spoolss so that it does
148 * all printer verification, and eventually assigns
149 * a job id */
151 status = rpc_pipe_open_interface(fsp->conn,
152 &ndr_table_spoolss.syntax_id,
153 fsp->conn->session_info,
154 fsp->conn->sconn->remote_address,
155 fsp->conn->sconn->msg_ctx,
156 &fsp->conn->spoolss_pipe);
157 if (!NT_STATUS_IS_OK(status)) {
158 goto done;
160 b = fsp->conn->spoolss_pipe->binding_handle;
162 ZERO_STRUCT(devmode_ctr);
164 status = dcerpc_spoolss_OpenPrinter(b, pf, pf->svcname,
165 "RAW", devmode_ctr,
166 PRINTER_ACCESS_USE,
167 &pf->handle, &werr);
168 if (!NT_STATUS_IS_OK(status)) {
169 goto done;
171 if (!W_ERROR_IS_OK(werr)) {
172 status = werror_to_ntstatus(werr);
173 goto done;
176 info.info1 = talloc(tmp_ctx, struct spoolss_DocumentInfo1);
177 if (!info.info1) {
178 status = NT_STATUS_NO_MEMORY;
179 goto done;
181 info.info1->document_name = pf->docname;
182 info.info1->output_file = pf->filename;
183 info.info1->datatype = "RAW";
185 status = dcerpc_spoolss_StartDocPrinter(b, tmp_ctx, &pf->handle,
186 1, info, &pf->jobid, &werr);
187 if (!NT_STATUS_IS_OK(status)) {
188 goto done;
190 if (!W_ERROR_IS_OK(werr)) {
191 status = werror_to_ntstatus(werr);
192 goto done;
195 /* Convert to RAP id. */
196 pf->rap_jobid = pjobid_to_rap(pf->svcname, pf->jobid);
197 if (pf->rap_jobid == 0) {
198 /* No errno around here */
199 status = NT_STATUS_ACCESS_DENIED;
200 goto done;
203 /* setup a full fsp */
204 status = create_synthetic_smb_fname(fsp, pf->filename, NULL,
205 NULL, &fsp->fsp_name);
206 if (!NT_STATUS_IS_OK(status)) {
207 goto done;
210 if (sys_fstat(fd, &fsp->fsp_name->st, false) != 0) {
211 status = map_nt_error_from_unix(errno);
212 goto done;
215 fsp->file_id = vfs_file_id_from_sbuf(fsp->conn, &fsp->fsp_name->st);
216 fsp->fh->fd = fd;
218 fsp->vuid = current_vuid;
219 fsp->can_lock = false;
220 fsp->can_read = false;
221 fsp->access_mask = FILE_GENERIC_WRITE;
222 fsp->can_write = true;
223 fsp->modified = false;
224 fsp->oplock_type = NO_OPLOCK;
225 fsp->sent_oplock_break = NO_BREAK_SENT;
226 fsp->is_directory = false;
228 fsp->print_file = pf;
230 status = NT_STATUS_OK;
231 done:
232 if (!NT_STATUS_IS_OK(status)) {
233 if (fd != -1) {
234 close(fd);
235 if (fsp->print_file) {
236 unlink(fsp->print_file->filename);
239 /* We need to delete the job from spoolss too */
240 if (pf->jobid) {
241 print_spool_terminate(fsp->conn, pf);
244 talloc_free(tmp_ctx);
245 return status;
248 int print_spool_write(files_struct *fsp,
249 const char *data, uint32_t size,
250 off_t offset, uint32_t *written)
252 SMB_STRUCT_STAT st;
253 ssize_t n;
254 int ret;
256 *written = 0;
258 /* first of all stat file to find out if it is still there.
259 * spoolss may have deleted it to signal someone has killed
260 * the job through it's interface */
262 if (sys_fstat(fsp->fh->fd, &st, false) != 0) {
263 ret = errno;
264 DEBUG(3, ("printfile_offset: sys_fstat failed on %s (%s)\n",
265 fsp_str_dbg(fsp), strerror(ret)));
266 return ret;
269 /* check if the file is unlinked, this will signal spoolss has
270 * killed it, just return an error and close the file */
271 if (st.st_ex_nlink == 0) {
272 close(fsp->fh->fd);
273 return EBADF;
276 /* When print files go beyond 4GB, the 32-bit offset sent in
277 * old SMBwrite calls is relative to the current 4GB chunk
278 * we're writing to.
279 * Discovered by Sebastian Kloska <oncaphillis@snafu.de>.
281 if (offset < 0xffffffff00000000LL) {
282 offset = (st.st_ex_size & 0xffffffff00000000LL) + offset;
285 n = write_data_at_offset(fsp->fh->fd, data, size, offset);
286 if (n == -1) {
287 ret = errno;
288 print_spool_terminate(fsp->conn, fsp->print_file);
289 } else {
290 *written = n;
291 ret = 0;
294 return ret;
297 void print_spool_end(files_struct *fsp, enum file_close_type close_type)
299 NTSTATUS status;
300 WERROR werr;
301 struct dcerpc_binding_handle *b = NULL;
303 b = fsp->conn->spoolss_pipe->binding_handle;
305 switch (close_type) {
306 case NORMAL_CLOSE:
307 case SHUTDOWN_CLOSE:
308 /* this also automatically calls spoolss_EndDocPrinter */
309 status = dcerpc_spoolss_ClosePrinter(b, fsp->print_file,
310 &fsp->print_file->handle,
311 &werr);
312 if (!NT_STATUS_IS_OK(status) ||
313 !NT_STATUS_IS_OK(status = werror_to_ntstatus(werr))) {
314 DEBUG(3, ("Failed to close printer %s [%s]\n",
315 fsp->print_file->svcname, nt_errstr(status)));
317 break;
318 case ERROR_CLOSE:
319 print_spool_terminate(fsp->conn, fsp->print_file);
320 break;
325 void print_spool_terminate(struct connection_struct *conn,
326 struct print_file_data *print_file)
328 NTSTATUS status;
329 WERROR werr;
330 struct dcerpc_binding_handle *b = NULL;
332 rap_jobid_delete(print_file->svcname, print_file->jobid);
334 status = rpc_pipe_open_interface(conn,
335 &ndr_table_spoolss.syntax_id,
336 conn->session_info,
337 conn->sconn->remote_address,
338 conn->sconn->msg_ctx,
339 &conn->spoolss_pipe);
340 if (!NT_STATUS_IS_OK(status)) {
341 DEBUG(0, ("print_spool_terminate: "
342 "Failed to get spoolss pipe [%s]\n",
343 nt_errstr(status)));
344 return;
346 b = conn->spoolss_pipe->binding_handle;
348 status = dcerpc_spoolss_SetJob(b, print_file,
349 &print_file->handle,
350 print_file->jobid,
351 NULL, SPOOLSS_JOB_CONTROL_DELETE,
352 &werr);
353 if (!NT_STATUS_IS_OK(status) ||
354 !NT_STATUS_IS_OK(status = werror_to_ntstatus(werr))) {
355 DEBUG(3, ("Failed to delete job %d [%s]\n",
356 print_file->jobid, nt_errstr(status)));
357 return;
359 status = dcerpc_spoolss_ClosePrinter(b, print_file,
360 &print_file->handle,
361 &werr);
362 if (!NT_STATUS_IS_OK(status) ||
363 !NT_STATUS_IS_OK(status = werror_to_ntstatus(werr))) {
364 DEBUG(3, ("Failed to close printer %s [%s]\n",
365 print_file->svcname, nt_errstr(status)));
366 return;