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