2 Unix SMB/CIFS implementation.
3 Printing routines that bridge to spoolss
4 Copyright (C) Simo Sorce 2010
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.
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.
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/>.
22 #include "../librpc/gen_ndr/cli_spoolss.h"
24 void print_spool_terminate(struct connection_struct *conn,
25 struct print_file_data *print_file);
27 /***************************************************************************
28 * Open a Document over spoolss
29 ***************************************************************************/
31 #define DOCNAME_DEFAULT "Remote Downlevel Document"
32 #ifndef PRINT_SPOOL_PREFIX
33 #define PRINT_SPOOL_PREFIX "smbprn."
36 NTSTATUS print_spool_open(files_struct *fsp,
38 uint16_t current_vuid)
42 struct print_file_data *pf;
43 struct rpc_pipe_client *cli;
44 struct spoolss_DevmodeContainer devmode_ctr;
45 union spoolss_DocumentInfo info;
49 tmp_ctx = talloc_new(fsp);
51 return NT_STATUS_NO_MEMORY;
54 pf = talloc_zero(fsp, struct print_file_data);
56 status = NT_STATUS_NO_MEMORY;
59 pf->svcname = talloc_strdup(pf, lp_servicename(SNUM(fsp->conn)));
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);
66 status = NT_STATUS_NO_MEMORY;
70 const char *p = strrchr(fname, '/');
74 pf->docname = talloc_asprintf_append(pf->docname, " %s", p);
76 status = NT_STATUS_NO_MEMORY;
81 /* Ok, now we have to open an actual file.
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. */
93 pf->filename = talloc_asprintf(pf, "%s/%s.XXXXXX",
94 lp_pathname(SNUM(fsp->conn)),
97 status = NT_STATUS_NO_MEMORY;
101 fd = mkstemp(pf->filename);
103 if (errno == EACCES) {
104 /* Common setup error, force a report. */
105 DEBUG(0, ("Insufficient permissions "
106 "to open spool file %s.\n",
109 /* Normal case, report at level 3 and above. */
110 DEBUG(3, ("can't open spool file %s,\n",
112 DEBUGADD(3, ("errno = %d (%s).\n",
113 errno, strerror(errno)));
115 status = map_nt_error_from_unix(errno);
119 /* now open a document over spoolss so that it does
120 * all printer verification, and eventually assigns
123 status = rpc_connect_spoolss_pipe(fsp->conn, &cli);
124 if (!NT_STATUS_IS_OK(status)) {
128 ZERO_STRUCT(devmode_ctr);
130 status = rpccli_spoolss_OpenPrinter(cli, pf, pf->svcname,
132 SEC_FLAG_MAXIMUM_ALLOWED,
134 if (!NT_STATUS_IS_OK(status)) {
137 if (!W_ERROR_IS_OK(werr)) {
138 status = werror_to_ntstatus(werr);
142 info.info1 = talloc(tmp_ctx, struct spoolss_DocumentInfo1);
144 status = NT_STATUS_NO_MEMORY;
147 info.info1->document_name = pf->docname;
148 info.info1->output_file = pf->filename;
149 info.info1->datatype = "RAW";
151 status = rpccli_spoolss_StartDocPrinter(cli, tmp_ctx, &pf->handle,
152 1, info, &pf->jobid, &werr);
153 if (!NT_STATUS_IS_OK(status)) {
156 if (!W_ERROR_IS_OK(werr)) {
157 status = werror_to_ntstatus(werr);
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;
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)) {
176 if (sys_fstat(fd, &fsp->fsp_name->st, false) != 0) {
177 status = map_nt_error_from_unix(errno);
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;
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;
195 fsp->print_file = pf;
197 status = NT_STATUS_OK;
199 if (!NT_STATUS_IS_OK(status)) {
202 unlink(fsp->print_file->filename);
204 /* We need to delete the job from spoolss too */
206 print_spool_terminate(fsp->conn, pf);
209 talloc_free(tmp_ctx);
213 int print_spool_write(files_struct *fsp, const char *data, uint32_t size,
214 SMB_OFF_T offset, uint32_t *written)
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 */
226 if (sys_fstat(fsp->fh->fd, &st, false) != 0) {
228 DEBUG(3, ("printfile_offset: sys_fstat failed on %s (%s)\n",
229 fsp_str_dbg(fsp), strerror(ret)));
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) {
240 /* When print files go beyond 4GB, the 32-bit offset sent in
241 * old SMBwrite calls is relative to the current 4GB chunk
243 * Discovered by Sebastian Kloska <oncaphillis@snafu.de>.
245 if (offset < 0xffffffff00000000LL) {
246 offset = (st.st_ex_size & 0xffffffff00000000LL) + offset;
249 n = write_data_at_offset(fsp->fh->fd, data, size, offset);
252 print_spool_terminate(fsp->conn, fsp->print_file);
261 void print_spool_end(files_struct *fsp, enum file_close_type close_type)
263 struct rpc_pipe_client *cli;
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",
275 switch (close_type) {
278 /* this also automatically calls spoolss_EndDocPrinter */
279 status = rpccli_spoolss_ClosePrinter(cli, fsp->print_file,
280 &fsp->print_file->handle,
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)));
289 print_spool_terminate(fsp->conn, fsp->print_file);
295 void print_spool_terminate(struct connection_struct *conn,
296 struct print_file_data *print_file)
298 struct rpc_pipe_client *cli;
302 rap_jobid_delete(print_file->svcname, print_file->jobid);
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",
312 status = rpccli_spoolss_SetJob(cli, print_file,
315 NULL, SPOOLSS_JOB_CONTROL_DELETE,
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)));
323 status = rpccli_spoolss_ClosePrinter(cli, print_file,
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)));