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