dsgetdcname: add site support.
[Samba.git] / source / libsmb / libsmb_printjob.c
blobc8d7ad039d2352c2fed465d852069a75f99f82b3
1 /*
2 Unix SMB/Netbios implementation.
3 SMB client library implementation
4 Copyright (C) Andrew Tridgell 1998
5 Copyright (C) Richard Sharpe 2000, 2002
6 Copyright (C) John Terpstra 2000
7 Copyright (C) Tom Jansen (Ninja ISD) 2002
8 Copyright (C) Derrell Lipman 2003-2008
9 Copyright (C) Jeremy Allison 2007, 2008
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 3 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include "includes.h"
26 #include "libsmbclient.h"
27 #include "libsmb_internal.h"
31 * Open a print file to be written to by other calls
34 SMBCFILE *
35 SMBC_open_print_job_ctx(SMBCCTX *context,
36 const char *fname)
38 char *server = NULL;
39 char *share = NULL;
40 char *user = NULL;
41 char *password = NULL;
42 char *path = NULL;
43 TALLOC_CTX *frame = talloc_stackframe();
45 if (!context || !context->internal->initialized) {
47 errno = EINVAL;
48 TALLOC_FREE(frame);
49 return NULL;
52 if (!fname) {
53 errno = EINVAL;
54 TALLOC_FREE(frame);
55 return NULL;
58 DEBUG(4, ("SMBC_open_print_job_ctx(%s)\n", fname));
60 if (SMBC_parse_path(frame,
61 context,
62 fname,
63 NULL,
64 &server,
65 &share,
66 &path,
67 &user,
68 &password,
69 NULL)) {
70 errno = EINVAL;
71 TALLOC_FREE(frame);
72 return NULL;
75 /* What if the path is empty, or the file exists? */
77 TALLOC_FREE(frame);
78 return smbc_getFunctionOpen(context)(context, fname, O_WRONLY, 666);
82 * Routine to print a file on a remote server ...
84 * We open the file, which we assume to be on a remote server, and then
85 * copy it to a print file on the share specified by printq.
88 int
89 SMBC_print_file_ctx(SMBCCTX *c_file,
90 const char *fname,
91 SMBCCTX *c_print,
92 const char *printq)
94 SMBCFILE *fid1;
95 SMBCFILE *fid2;
96 int bytes;
97 int saverr;
98 int tot_bytes = 0;
99 char buf[4096];
100 TALLOC_CTX *frame = talloc_stackframe();
102 if (!c_file || !c_file->internal->initialized ||
103 !c_print || !c_print->internal->initialized) {
105 errno = EINVAL;
106 TALLOC_FREE(frame);
107 return -1;
111 if (!fname && !printq) {
113 errno = EINVAL;
114 TALLOC_FREE(frame);
115 return -1;
119 /* Try to open the file for reading ... */
121 if ((long)(fid1 = smbc_getFunctionOpen(c_file)(c_file, fname,
122 O_RDONLY, 0666)) < 0) {
123 DEBUG(3, ("Error, fname=%s, errno=%i\n", fname, errno));
124 TALLOC_FREE(frame);
125 return -1; /* smbc_open sets errno */
128 /* Now, try to open the printer file for writing */
130 if ((long)(fid2 = smbc_getFunctionOpenPrintJob(c_print)(c_print,
131 printq)) < 0) {
133 saverr = errno; /* Save errno */
134 smbc_getFunctionClose(c_file)(c_file, fid1);
135 errno = saverr;
136 TALLOC_FREE(frame);
137 return -1;
141 while ((bytes = smbc_getFunctionRead(c_file)(c_file, fid1,
142 buf, sizeof(buf))) > 0) {
144 tot_bytes += bytes;
146 if ((smbc_getFunctionWrite(c_print)(c_print, fid2,
147 buf, bytes)) < 0) {
149 saverr = errno;
150 smbc_getFunctionClose(c_file)(c_file, fid1);
151 smbc_getFunctionClose(c_print)(c_print, fid2);
152 errno = saverr;
158 saverr = errno;
160 smbc_getFunctionClose(c_file)(c_file, fid1);
161 smbc_getFunctionClose(c_print)(c_print, fid2);
163 if (bytes < 0) {
165 errno = saverr;
166 TALLOC_FREE(frame);
167 return -1;
171 TALLOC_FREE(frame);
172 return tot_bytes;
177 * Routine to list print jobs on a printer share ...
181 SMBC_list_print_jobs_ctx(SMBCCTX *context,
182 const char *fname,
183 smbc_list_print_job_fn fn)
185 SMBCSRV *srv = NULL;
186 char *server = NULL;
187 char *share = NULL;
188 char *user = NULL;
189 char *password = NULL;
190 char *workgroup = NULL;
191 char *path = NULL;
192 TALLOC_CTX *frame = talloc_stackframe();
194 if (!context || !context->internal->initialized) {
196 errno = EINVAL;
197 TALLOC_FREE(frame);
198 return -1;
201 if (!fname) {
202 errno = EINVAL;
203 TALLOC_FREE(frame);
204 return -1;
207 DEBUG(4, ("smbc_list_print_jobs(%s)\n", fname));
209 if (SMBC_parse_path(frame,
210 context,
211 fname,
212 &workgroup,
213 &server,
214 &share,
215 &path,
216 &user,
217 &password,
218 NULL)) {
219 errno = EINVAL;
220 TALLOC_FREE(frame);
221 return -1;
224 if (!user || user[0] == (char)0) {
225 user = talloc_strdup(frame, smbc_getUser(context));
226 if (!user) {
227 errno = ENOMEM;
228 TALLOC_FREE(frame);
229 return -1;
233 srv = SMBC_server(frame, context, True,
234 server, share, &workgroup, &user, &password);
236 if (!srv) {
237 TALLOC_FREE(frame);
238 return -1; /* errno set by SMBC_server */
241 if (cli_print_queue(srv->cli,
242 (void (*)(struct print_job_info *))fn) < 0) {
243 errno = SMBC_errno(context, srv->cli);
244 TALLOC_FREE(frame);
245 return -1;
248 TALLOC_FREE(frame);
249 return 0;
254 * Delete a print job from a remote printer share
258 SMBC_unlink_print_job_ctx(SMBCCTX *context,
259 const char *fname,
260 int id)
262 SMBCSRV *srv = NULL;
263 char *server = NULL;
264 char *share = NULL;
265 char *user = NULL;
266 char *password = NULL;
267 char *workgroup = NULL;
268 char *path = NULL;
269 int err;
270 TALLOC_CTX *frame = talloc_stackframe();
272 if (!context || !context->internal->initialized) {
274 errno = EINVAL;
275 TALLOC_FREE(frame);
276 return -1;
279 if (!fname) {
280 errno = EINVAL;
281 TALLOC_FREE(frame);
282 return -1;
285 DEBUG(4, ("smbc_unlink_print_job(%s)\n", fname));
287 if (SMBC_parse_path(frame,
288 context,
289 fname,
290 &workgroup,
291 &server,
292 &share,
293 &path,
294 &user,
295 &password,
296 NULL)) {
297 errno = EINVAL;
298 TALLOC_FREE(frame);
299 return -1;
302 if (!user || user[0] == (char)0) {
303 user = talloc_strdup(frame, smbc_getUser(context));
304 if (!user) {
305 errno = ENOMEM;
306 TALLOC_FREE(frame);
307 return -1;
311 srv = SMBC_server(frame, context, True,
312 server, share, &workgroup, &user, &password);
314 if (!srv) {
316 TALLOC_FREE(frame);
317 return -1; /* errno set by SMBC_server */
321 if ((err = cli_printjob_del(srv->cli, id)) != 0) {
323 if (err < 0)
324 errno = SMBC_errno(context, srv->cli);
325 else if (err == ERRnosuchprintjob)
326 errno = EINVAL;
327 TALLOC_FREE(frame);
328 return -1;
332 TALLOC_FREE(frame);
333 return 0;