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