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