ndr: Another try to support the build on non-IPv6 systems
[Samba.git] / source3 / printing / printspoolss.c
blob8426b84fec501a4ef5a839a7d579e4e0f5f4692e
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 "../librpc/gen_ndr/cli_spoolss.h"
23 #include "rpc_server/rpc_ncacn_np.h"
24 #include "smbd/globals.h"
25 #include "../libcli/security/security.h"
27 void print_spool_terminate(struct connection_struct *conn,
28 struct print_file_data *print_file);
30 /***************************************************************************
31 * Open a Document over spoolss
32 ***************************************************************************/
34 #define DOCNAME_DEFAULT "Remote Downlevel Document"
35 #ifndef PRINT_SPOOL_PREFIX
36 #define PRINT_SPOOL_PREFIX "smbprn."
37 #endif
39 NTSTATUS print_spool_open(files_struct *fsp,
40 const char *fname,
41 uint16_t current_vuid)
43 NTSTATUS status;
44 TALLOC_CTX *tmp_ctx;
45 struct print_file_data *pf;
46 struct rpc_pipe_client *cli;
47 struct spoolss_DevmodeContainer devmode_ctr;
48 union spoolss_DocumentInfo info;
49 int fd = -1;
50 WERROR werr;
52 tmp_ctx = talloc_new(fsp);
53 if (!tmp_ctx) {
54 return NT_STATUS_NO_MEMORY;
57 pf = talloc_zero(fsp, struct print_file_data);
58 if (!pf) {
59 status = NT_STATUS_NO_MEMORY;
60 goto done;
62 pf->svcname = talloc_strdup(pf, lp_servicename(SNUM(fsp->conn)));
64 /* the document name is derived from the file name.
65 * "Remote Downlevel Document" is added in front to
66 * mimic what windows does in this case */
67 pf->docname = talloc_strdup(pf, DOCNAME_DEFAULT);
68 if (!pf->docname) {
69 status = NT_STATUS_NO_MEMORY;
70 goto done;
72 if (fname) {
73 const char *p = strrchr(fname, '/');
74 if (!p) {
75 p = fname;
77 pf->docname = talloc_asprintf_append(pf->docname, " %s", p);
78 if (!pf->docname) {
79 status = NT_STATUS_NO_MEMORY;
80 goto done;
84 /* Ok, now we have to open an actual file.
85 * Here is the reason:
86 * We want to write the spool job to this file in
87 * smbd for scalability reason (and also because
88 * apparently window printer drivers can seek when
89 * spooling to a file).
90 * So we first create a file, and then we pass it
91 * to spoolss in output_file so it can monitor and
92 * take over once we call EndDocPrinter().
93 * Of course we will not start writing until
94 * StartDocPrinter() actually gives the ok. */
96 pf->filename = talloc_asprintf(pf, "%s/%s.XXXXXX",
97 lp_pathname(SNUM(fsp->conn)),
98 PRINT_SPOOL_PREFIX);
99 if (!pf->filename) {
100 status = NT_STATUS_NO_MEMORY;
101 goto done;
103 errno = 0;
104 fd = mkstemp(pf->filename);
105 if (fd == -1) {
106 if (errno == EACCES) {
107 /* Common setup error, force a report. */
108 DEBUG(0, ("Insufficient permissions "
109 "to open spool file %s.\n",
110 pf->filename));
111 } else {
112 /* Normal case, report at level 3 and above. */
113 DEBUG(3, ("can't open spool file %s,\n",
114 pf->filename));
115 DEBUGADD(3, ("errno = %d (%s).\n",
116 errno, strerror(errno)));
118 status = map_nt_error_from_unix(errno);
119 goto done;
122 /* now open a document over spoolss so that it does
123 * all printer verification, and eventually assigns
124 * a job id */
126 status = rpc_pipe_open_interface(fsp->conn,
127 &ndr_table_spoolss.syntax_id,
128 fsp->conn->server_info,
129 &fsp->conn->sconn->client_id,
130 fsp->conn->sconn->msg_ctx,
131 &fsp->conn->spoolss_pipe);
132 if (!NT_STATUS_IS_OK(status)) {
133 goto done;
135 cli = fsp->conn->spoolss_pipe;
137 ZERO_STRUCT(devmode_ctr);
139 status = rpccli_spoolss_OpenPrinter(cli, pf, pf->svcname,
140 "RAW", devmode_ctr,
141 SEC_FLAG_MAXIMUM_ALLOWED,
142 &pf->handle, &werr);
143 if (!NT_STATUS_IS_OK(status)) {
144 goto done;
146 if (!W_ERROR_IS_OK(werr)) {
147 status = werror_to_ntstatus(werr);
148 goto done;
151 info.info1 = talloc(tmp_ctx, struct spoolss_DocumentInfo1);
152 if (!info.info1) {
153 status = NT_STATUS_NO_MEMORY;
154 goto done;
156 info.info1->document_name = pf->docname;
157 info.info1->output_file = pf->filename;
158 info.info1->datatype = "RAW";
160 status = rpccli_spoolss_StartDocPrinter(cli, tmp_ctx, &pf->handle,
161 1, info, &pf->jobid, &werr);
162 if (!NT_STATUS_IS_OK(status)) {
163 goto done;
165 if (!W_ERROR_IS_OK(werr)) {
166 status = werror_to_ntstatus(werr);
167 goto done;
170 /* Convert to RAP id. */
171 pf->rap_jobid = pjobid_to_rap(pf->svcname, pf->jobid);
172 if (pf->rap_jobid == 0) {
173 /* No errno around here */
174 status = NT_STATUS_ACCESS_DENIED;
175 goto done;
178 /* setup a full fsp */
179 status = create_synthetic_smb_fname(fsp, pf->filename, NULL,
180 NULL, &fsp->fsp_name);
181 if (!NT_STATUS_IS_OK(status)) {
182 goto done;
185 if (sys_fstat(fd, &fsp->fsp_name->st, false) != 0) {
186 status = map_nt_error_from_unix(errno);
187 goto done;
190 fsp->file_id = vfs_file_id_from_sbuf(fsp->conn, &fsp->fsp_name->st);
191 fsp->mode = fsp->fsp_name->st.st_ex_mode;
192 fsp->fh->fd = fd;
194 fsp->vuid = current_vuid;
195 fsp->can_lock = false;
196 fsp->can_read = false;
197 fsp->access_mask = FILE_GENERIC_WRITE;
198 fsp->can_write = true;
199 fsp->modified = false;
200 fsp->oplock_type = NO_OPLOCK;
201 fsp->sent_oplock_break = NO_BREAK_SENT;
202 fsp->is_directory = false;
204 fsp->print_file = pf;
206 status = NT_STATUS_OK;
207 done:
208 if (!NT_STATUS_IS_OK(status)) {
209 if (fd != -1) {
210 close(fd);
211 unlink(fsp->print_file->filename);
213 /* We need to delete the job from spoolss too */
214 if (pf->jobid) {
215 print_spool_terminate(fsp->conn, pf);
218 talloc_free(tmp_ctx);
219 return status;
222 int print_spool_write(files_struct *fsp,
223 const char *data, uint32_t size,
224 SMB_OFF_T offset, uint32_t *written)
226 SMB_STRUCT_STAT st;
227 ssize_t n;
228 int ret;
230 *written = 0;
232 /* first of all stat file to find out if it is still there.
233 * spoolss may have deleted it to signal someone has killed
234 * the job through it's interface */
236 if (sys_fstat(fsp->fh->fd, &st, false) != 0) {
237 ret = errno;
238 DEBUG(3, ("printfile_offset: sys_fstat failed on %s (%s)\n",
239 fsp_str_dbg(fsp), strerror(ret)));
240 return ret;
243 /* check if the file is unlinked, this will signal spoolss has
244 * killed it, just return an error and close the file */
245 if (st.st_ex_nlink == 0) {
246 close(fsp->fh->fd);
247 return EBADF;
250 /* When print files go beyond 4GB, the 32-bit offset sent in
251 * old SMBwrite calls is relative to the current 4GB chunk
252 * we're writing to.
253 * Discovered by Sebastian Kloska <oncaphillis@snafu.de>.
255 if (offset < 0xffffffff00000000LL) {
256 offset = (st.st_ex_size & 0xffffffff00000000LL) + offset;
259 n = write_data_at_offset(fsp->fh->fd, data, size, offset);
260 if (n == -1) {
261 ret = errno;
262 print_spool_terminate(fsp->conn, fsp->print_file);
263 } else {
264 *written = n;
265 ret = 0;
268 return ret;
271 void print_spool_end(files_struct *fsp, enum file_close_type close_type)
273 struct rpc_pipe_client *cli;
274 NTSTATUS status;
275 WERROR werr;
277 status = rpc_pipe_open_interface(fsp->conn,
278 &ndr_table_spoolss.syntax_id,
279 fsp->conn->server_info,
280 &fsp->conn->sconn->client_id,
281 fsp->conn->sconn->msg_ctx,
282 &fsp->conn->spoolss_pipe);
283 if (!NT_STATUS_IS_OK(status)) {
284 DEBUG(0, ("print_spool_end: "
285 "Failed to get spoolss pipe [%s]\n",
286 nt_errstr(status)));
287 return;
289 cli = fsp->conn->spoolss_pipe;
291 switch (close_type) {
292 case NORMAL_CLOSE:
293 case SHUTDOWN_CLOSE:
294 /* this also automatically calls spoolss_EndDocPrinter */
295 status = rpccli_spoolss_ClosePrinter(cli, fsp->print_file,
296 &fsp->print_file->handle,
297 &werr);
298 if (!NT_STATUS_IS_OK(status) ||
299 !NT_STATUS_IS_OK(status = werror_to_ntstatus(werr))) {
300 DEBUG(3, ("Failed to close printer %s [%s]\n",
301 fsp->print_file->svcname, nt_errstr(status)));
303 break;
304 case ERROR_CLOSE:
305 print_spool_terminate(fsp->conn, fsp->print_file);
306 break;
311 void print_spool_terminate(struct connection_struct *conn,
312 struct print_file_data *print_file)
314 struct rpc_pipe_client *cli;
315 NTSTATUS status;
316 WERROR werr;
318 rap_jobid_delete(print_file->svcname, print_file->jobid);
320 status = rpc_pipe_open_interface(conn,
321 &ndr_table_spoolss.syntax_id,
322 conn->server_info,
323 &conn->sconn->client_id,
324 conn->sconn->msg_ctx,
325 &conn->spoolss_pipe);
326 if (!NT_STATUS_IS_OK(status)) {
327 DEBUG(0, ("print_spool_terminate: "
328 "Failed to get spoolss pipe [%s]\n",
329 nt_errstr(status)));
330 return;
332 cli = conn->spoolss_pipe;
334 status = rpccli_spoolss_SetJob(cli, print_file,
335 &print_file->handle,
336 print_file->jobid,
337 NULL, SPOOLSS_JOB_CONTROL_DELETE,
338 &werr);
339 if (!NT_STATUS_IS_OK(status) ||
340 !NT_STATUS_IS_OK(status = werror_to_ntstatus(werr))) {
341 DEBUG(3, ("Failed to delete job %d [%s]\n",
342 print_file->jobid, nt_errstr(status)));
343 return;
345 status = rpccli_spoolss_ClosePrinter(cli, print_file,
346 &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 print_file->svcname, nt_errstr(status)));
352 return;