s3: Fix an uninitialized variable
[Samba/gebeck_regimport.git] / source3 / printing / printspoolss.c
blobe97b397cb0634efc8820c505c1f98d9331d92850
1 /*
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/>.
20 #include "includes.h"
21 #include "printing.h"
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."
34 #endif
36 NTSTATUS print_spool_open(files_struct *fsp,
37 const char *fname,
38 uint16_t current_vuid)
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;
49 tmp_ctx = talloc_new(fsp);
50 if (!tmp_ctx) {
51 return NT_STATUS_NO_MEMORY;
54 pf = talloc_zero(fsp, struct print_file_data);
55 if (!pf) {
56 status = NT_STATUS_NO_MEMORY;
57 goto done;
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);
65 if (!pf->docname) {
66 status = NT_STATUS_NO_MEMORY;
67 goto done;
69 if (fname) {
70 const char *p = strrchr(fname, '/');
71 if (!p) {
72 p = fname;
74 pf->docname = talloc_asprintf_append(pf->docname, " %s", p);
75 if (!pf->docname) {
76 status = NT_STATUS_NO_MEMORY;
77 goto done;
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. */
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;
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)));
115 status = map_nt_error_from_unix(errno);
116 goto done;
119 /* now open a document over spoolss so that it does
120 * all printer verification, and eventually assigns
121 * a job id */
123 status = rpc_connect_spoolss_pipe(fsp->conn, &cli);
124 if (!NT_STATUS_IS_OK(status)) {
125 goto done;
128 ZERO_STRUCT(devmode_ctr);
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;
137 if (!W_ERROR_IS_OK(werr)) {
138 status = werror_to_ntstatus(werr);
139 goto done;
142 info.info1 = talloc(tmp_ctx, struct spoolss_DocumentInfo1);
143 if (!info.info1) {
144 status = NT_STATUS_NO_MEMORY;
145 goto done;
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)) {
154 goto done;
156 if (!W_ERROR_IS_OK(werr)) {
157 status = werror_to_ntstatus(werr);
158 goto done;
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;
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;
176 if (sys_fstat(fd, &fsp->fsp_name->st, false) != 0) {
177 status = map_nt_error_from_unix(errno);
178 goto done;
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;
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;
198 done:
199 if (!NT_STATUS_IS_OK(status)) {
200 if (fd != -1) {
201 close(fd);
202 unlink(fsp->print_file->filename);
204 /* We need to delete the job from spoolss too */
205 if (pf->jobid) {
206 print_spool_terminate(fsp->conn, pf);
209 talloc_free(tmp_ctx);
210 return status;
213 int print_spool_write(files_struct *fsp,
214 const char *data, uint32_t size,
215 SMB_OFF_T offset, uint32_t *written)
217 SMB_STRUCT_STAT st;
218 ssize_t n;
219 int ret;
221 *written = 0;
223 /* first of all stat file to find out if it is still there.
224 * spoolss may have deleted it to signal someone has killed
225 * the job through it's interface */
227 if (sys_fstat(fsp->fh->fd, &st, false) != 0) {
228 ret = errno;
229 DEBUG(3, ("printfile_offset: sys_fstat failed on %s (%s)\n",
230 fsp_str_dbg(fsp), strerror(ret)));
231 return ret;
234 /* check if the file is unlinked, this will signal spoolss has
235 * killed it, just return an error and close the file */
236 if (st.st_ex_nlink == 0) {
237 close(fsp->fh->fd);
238 return EBADF;
241 /* When print files go beyond 4GB, the 32-bit offset sent in
242 * old SMBwrite calls is relative to the current 4GB chunk
243 * we're writing to.
244 * Discovered by Sebastian Kloska <oncaphillis@snafu.de>.
246 if (offset < 0xffffffff00000000LL) {
247 offset = (st.st_ex_size & 0xffffffff00000000LL) + offset;
250 n = write_data_at_offset(fsp->fh->fd, data, size, offset);
251 if (n == -1) {
252 ret = errno;
253 print_spool_terminate(fsp->conn, fsp->print_file);
254 } else {
255 *written = n;
256 ret = 0;
259 return ret;
262 void print_spool_end(files_struct *fsp, enum file_close_type close_type)
264 struct rpc_pipe_client *cli;
265 NTSTATUS status;
266 WERROR werr;
268 status = rpc_connect_spoolss_pipe(fsp->conn, &cli);
269 if (!NT_STATUS_IS_OK(status)) {
270 DEBUG(0, ("print_spool_end: "
271 "Failed to get spoolss pipe [%s]\n",
272 nt_errstr(status)));
273 return;
276 switch (close_type) {
277 case NORMAL_CLOSE:
278 case SHUTDOWN_CLOSE:
279 /* this also automatically calls spoolss_EndDocPrinter */
280 status = rpccli_spoolss_ClosePrinter(cli, fsp->print_file,
281 &fsp->print_file->handle,
282 &werr);
283 if (!NT_STATUS_IS_OK(status) ||
284 !NT_STATUS_IS_OK(status = werror_to_ntstatus(werr))) {
285 DEBUG(3, ("Failed to close printer %s [%s]\n",
286 fsp->print_file->svcname, nt_errstr(status)));
288 break;
289 case ERROR_CLOSE:
290 print_spool_terminate(fsp->conn, fsp->print_file);
291 break;
296 void print_spool_terminate(struct connection_struct *conn,
297 struct print_file_data *print_file)
299 struct rpc_pipe_client *cli;
300 NTSTATUS status;
301 WERROR werr;
303 rap_jobid_delete(print_file->svcname, print_file->jobid);
305 status = rpc_connect_spoolss_pipe(conn, &cli);
306 if (!NT_STATUS_IS_OK(status)) {
307 DEBUG(0, ("print_spool_terminate: "
308 "Failed to get spoolss pipe [%s]\n",
309 nt_errstr(status)));
310 return;
313 status = rpccli_spoolss_SetJob(cli, print_file,
314 &print_file->handle,
315 print_file->jobid,
316 NULL, SPOOLSS_JOB_CONTROL_DELETE,
317 &werr);
318 if (!NT_STATUS_IS_OK(status) ||
319 !NT_STATUS_IS_OK(status = werror_to_ntstatus(werr))) {
320 DEBUG(3, ("Failed to delete job %d [%s]\n",
321 print_file->jobid, nt_errstr(status)));
322 return;
324 status = rpccli_spoolss_ClosePrinter(cli, print_file,
325 &print_file->handle,
326 &werr);
327 if (!NT_STATUS_IS_OK(status) ||
328 !NT_STATUS_IS_OK(status = werror_to_ntstatus(werr))) {
329 DEBUG(3, ("Failed to close printer %s [%s]\n",
330 print_file->svcname, nt_errstr(status)));
331 return;