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