s3-utils/net_rpc_printer.c: print more info on write error
[Samba/gebeck_regimport.git] / source3 / printing / printspoolss.c
blob75e9cad28da763fc1cd20e9adf4ae259abbc6755
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 void print_spool_terminate(struct connection_struct *conn,
29 struct print_file_data *print_file);
31 /***************************************************************************
32 * Open a Document over spoolss
33 ***************************************************************************/
35 #define DOCNAME_DEFAULT "Remote Downlevel Document"
36 #ifndef PRINT_SPOOL_PREFIX
37 #define PRINT_SPOOL_PREFIX "smbprn."
38 #endif
40 NTSTATUS print_spool_open(files_struct *fsp,
41 const char *fname,
42 uint16_t current_vuid)
44 NTSTATUS status;
45 TALLOC_CTX *tmp_ctx;
46 struct print_file_data *pf;
47 struct dcerpc_binding_handle *b = NULL;
48 struct spoolss_DevmodeContainer devmode_ctr;
49 union spoolss_DocumentInfo info;
50 int fd = -1;
51 WERROR werr;
53 tmp_ctx = talloc_new(fsp);
54 if (!tmp_ctx) {
55 return NT_STATUS_NO_MEMORY;
58 pf = talloc_zero(fsp, struct print_file_data);
59 if (!pf) {
60 status = NT_STATUS_NO_MEMORY;
61 goto done;
63 pf->svcname = talloc_strdup(pf, lp_servicename(SNUM(fsp->conn)));
65 /* the document name is derived from the file name.
66 * "Remote Downlevel Document" is added in front to
67 * mimic what windows does in this case */
68 pf->docname = talloc_strdup(pf, DOCNAME_DEFAULT);
69 if (!pf->docname) {
70 status = NT_STATUS_NO_MEMORY;
71 goto done;
73 if (fname) {
74 const char *p = strrchr(fname, '/');
75 if (!p) {
76 p = fname;
78 pf->docname = talloc_asprintf_append(pf->docname, " %s", p);
79 if (!pf->docname) {
80 status = NT_STATUS_NO_MEMORY;
81 goto done;
85 /* Ok, now we have to open an actual file.
86 * Here is the reason:
87 * We want to write the spool job to this file in
88 * smbd for scalability reason (and also because
89 * apparently window printer drivers can seek when
90 * spooling to a file).
91 * So we first create a file, and then we pass it
92 * to spoolss in output_file so it can monitor and
93 * take over once we call EndDocPrinter().
94 * Of course we will not start writing until
95 * StartDocPrinter() actually gives the ok. */
97 pf->filename = talloc_asprintf(pf, "%s/%s.XXXXXX",
98 lp_pathname(SNUM(fsp->conn)),
99 PRINT_SPOOL_PREFIX);
100 if (!pf->filename) {
101 status = NT_STATUS_NO_MEMORY;
102 goto done;
104 errno = 0;
105 fd = mkstemp(pf->filename);
106 if (fd == -1) {
107 if (errno == EACCES) {
108 /* Common setup error, force a report. */
109 DEBUG(0, ("Insufficient permissions "
110 "to open spool file %s.\n",
111 pf->filename));
112 } else {
113 /* Normal case, report at level 3 and above. */
114 DEBUG(3, ("can't open spool file %s,\n",
115 pf->filename));
116 DEBUGADD(3, ("errno = %d (%s).\n",
117 errno, strerror(errno)));
119 status = map_nt_error_from_unix(errno);
120 goto done;
123 /* now open a document over spoolss so that it does
124 * all printer verification, and eventually assigns
125 * a job id */
127 status = rpc_pipe_open_interface(fsp->conn,
128 &ndr_table_spoolss.syntax_id,
129 fsp->conn->session_info,
130 fsp->conn->sconn->remote_address,
131 fsp->conn->sconn->msg_ctx,
132 &fsp->conn->spoolss_pipe);
133 if (!NT_STATUS_IS_OK(status)) {
134 goto done;
136 b = fsp->conn->spoolss_pipe->binding_handle;
138 ZERO_STRUCT(devmode_ctr);
140 status = dcerpc_spoolss_OpenPrinter(b, pf, pf->svcname,
141 "RAW", devmode_ctr,
142 SEC_FLAG_MAXIMUM_ALLOWED,
143 &pf->handle, &werr);
144 if (!NT_STATUS_IS_OK(status)) {
145 goto done;
147 if (!W_ERROR_IS_OK(werr)) {
148 status = werror_to_ntstatus(werr);
149 goto done;
152 info.info1 = talloc(tmp_ctx, struct spoolss_DocumentInfo1);
153 if (!info.info1) {
154 status = NT_STATUS_NO_MEMORY;
155 goto done;
157 info.info1->document_name = pf->docname;
158 info.info1->output_file = pf->filename;
159 info.info1->datatype = "RAW";
161 status = dcerpc_spoolss_StartDocPrinter(b, tmp_ctx, &pf->handle,
162 1, info, &pf->jobid, &werr);
163 if (!NT_STATUS_IS_OK(status)) {
164 goto done;
166 if (!W_ERROR_IS_OK(werr)) {
167 status = werror_to_ntstatus(werr);
168 goto done;
171 /* Convert to RAP id. */
172 pf->rap_jobid = pjobid_to_rap(pf->svcname, pf->jobid);
173 if (pf->rap_jobid == 0) {
174 /* No errno around here */
175 status = NT_STATUS_ACCESS_DENIED;
176 goto done;
179 /* setup a full fsp */
180 status = create_synthetic_smb_fname(fsp, pf->filename, NULL,
181 NULL, &fsp->fsp_name);
182 if (!NT_STATUS_IS_OK(status)) {
183 goto done;
186 if (sys_fstat(fd, &fsp->fsp_name->st, false) != 0) {
187 status = map_nt_error_from_unix(errno);
188 goto done;
191 fsp->file_id = vfs_file_id_from_sbuf(fsp->conn, &fsp->fsp_name->st);
192 fsp->mode = fsp->fsp_name->st.st_ex_mode;
193 fsp->fh->fd = fd;
195 fsp->vuid = current_vuid;
196 fsp->can_lock = false;
197 fsp->can_read = false;
198 fsp->access_mask = FILE_GENERIC_WRITE;
199 fsp->can_write = true;
200 fsp->modified = false;
201 fsp->oplock_type = NO_OPLOCK;
202 fsp->sent_oplock_break = NO_BREAK_SENT;
203 fsp->is_directory = false;
205 fsp->print_file = pf;
207 status = NT_STATUS_OK;
208 done:
209 if (!NT_STATUS_IS_OK(status)) {
210 if (fd != -1) {
211 close(fd);
212 if (fsp->print_file) {
213 unlink(fsp->print_file->filename);
216 /* We need to delete the job from spoolss too */
217 if (pf->jobid) {
218 print_spool_terminate(fsp->conn, pf);
221 talloc_free(tmp_ctx);
222 return status;
225 int print_spool_write(files_struct *fsp,
226 const char *data, uint32_t size,
227 SMB_OFF_T offset, uint32_t *written)
229 SMB_STRUCT_STAT st;
230 ssize_t n;
231 int ret;
233 *written = 0;
235 /* first of all stat file to find out if it is still there.
236 * spoolss may have deleted it to signal someone has killed
237 * the job through it's interface */
239 if (sys_fstat(fsp->fh->fd, &st, false) != 0) {
240 ret = errno;
241 DEBUG(3, ("printfile_offset: sys_fstat failed on %s (%s)\n",
242 fsp_str_dbg(fsp), strerror(ret)));
243 return ret;
246 /* check if the file is unlinked, this will signal spoolss has
247 * killed it, just return an error and close the file */
248 if (st.st_ex_nlink == 0) {
249 close(fsp->fh->fd);
250 return EBADF;
253 /* When print files go beyond 4GB, the 32-bit offset sent in
254 * old SMBwrite calls is relative to the current 4GB chunk
255 * we're writing to.
256 * Discovered by Sebastian Kloska <oncaphillis@snafu.de>.
258 if (offset < 0xffffffff00000000LL) {
259 offset = (st.st_ex_size & 0xffffffff00000000LL) + offset;
262 n = write_data_at_offset(fsp->fh->fd, data, size, offset);
263 if (n == -1) {
264 ret = errno;
265 print_spool_terminate(fsp->conn, fsp->print_file);
266 } else {
267 *written = n;
268 ret = 0;
271 return ret;
274 void print_spool_end(files_struct *fsp, enum file_close_type close_type)
276 NTSTATUS status;
277 WERROR werr;
278 struct dcerpc_binding_handle *b = NULL;
280 b = fsp->conn->spoolss_pipe->binding_handle;
282 switch (close_type) {
283 case NORMAL_CLOSE:
284 case SHUTDOWN_CLOSE:
285 /* this also automatically calls spoolss_EndDocPrinter */
286 status = dcerpc_spoolss_ClosePrinter(b, fsp->print_file,
287 &fsp->print_file->handle,
288 &werr);
289 if (!NT_STATUS_IS_OK(status) ||
290 !NT_STATUS_IS_OK(status = werror_to_ntstatus(werr))) {
291 DEBUG(3, ("Failed to close printer %s [%s]\n",
292 fsp->print_file->svcname, nt_errstr(status)));
294 break;
295 case ERROR_CLOSE:
296 print_spool_terminate(fsp->conn, fsp->print_file);
297 break;
302 void print_spool_terminate(struct connection_struct *conn,
303 struct print_file_data *print_file)
305 NTSTATUS status;
306 WERROR werr;
307 struct dcerpc_binding_handle *b = NULL;
309 rap_jobid_delete(print_file->svcname, print_file->jobid);
311 status = rpc_pipe_open_interface(conn,
312 &ndr_table_spoolss.syntax_id,
313 conn->session_info,
314 conn->sconn->remote_address,
315 conn->sconn->msg_ctx,
316 &conn->spoolss_pipe);
317 if (!NT_STATUS_IS_OK(status)) {
318 DEBUG(0, ("print_spool_terminate: "
319 "Failed to get spoolss pipe [%s]\n",
320 nt_errstr(status)));
321 return;
323 b = conn->spoolss_pipe->binding_handle;
325 status = dcerpc_spoolss_SetJob(b, print_file,
326 &print_file->handle,
327 print_file->jobid,
328 NULL, SPOOLSS_JOB_CONTROL_DELETE,
329 &werr);
330 if (!NT_STATUS_IS_OK(status) ||
331 !NT_STATUS_IS_OK(status = werror_to_ntstatus(werr))) {
332 DEBUG(3, ("Failed to delete job %d [%s]\n",
333 print_file->jobid, nt_errstr(status)));
334 return;
336 status = dcerpc_spoolss_ClosePrinter(b, print_file,
337 &print_file->handle,
338 &werr);
339 if (!NT_STATUS_IS_OK(status) ||
340 !NT_STATUS_IS_OK(status = werror_to_ntstatus(werr))) {
341 DEBUG(3, ("Failed to close printer %s [%s]\n",
342 print_file->svcname, nt_errstr(status)));
343 return;