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