s3-net: add command rpc registry import
[Samba.git] / source3 / libsmb / libsmb_printjob.c
blobea0cb37a144bb728533900124cd3eb15a8b1d6f0
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) {
46 errno = EINVAL;
47 TALLOC_FREE(frame);
48 return NULL;
51 if (!fname) {
52 errno = EINVAL;
53 TALLOC_FREE(frame);
54 return NULL;
57 DEBUG(4, ("SMBC_open_print_job_ctx(%s)\n", fname));
59 if (SMBC_parse_path(frame,
60 context,
61 fname,
62 NULL,
63 &server,
64 &share,
65 &path,
66 &user,
67 &password,
68 NULL)) {
69 errno = EINVAL;
70 TALLOC_FREE(frame);
71 return NULL;
74 /* What if the path is empty, or the file exists? */
76 TALLOC_FREE(frame);
77 return smbc_getFunctionOpen(context)(context, fname, O_WRONLY, 666);
81 * Routine to print a file on a remote server ...
83 * We open the file, which we assume to be on a remote server, and then
84 * copy it to a print file on the share specified by printq.
87 int
88 SMBC_print_file_ctx(SMBCCTX *c_file,
89 const char *fname,
90 SMBCCTX *c_print,
91 const char *printq)
93 SMBCFILE *fid1;
94 SMBCFILE *fid2;
95 int bytes;
96 int saverr;
97 int tot_bytes = 0;
98 char buf[4096];
99 TALLOC_CTX *frame = talloc_stackframe();
101 if (!c_file || !c_file->internal->initialized ||
102 !c_print || !c_print->internal->initialized) {
103 errno = EINVAL;
104 TALLOC_FREE(frame);
105 return -1;
108 if (!fname && !printq) {
109 errno = EINVAL;
110 TALLOC_FREE(frame);
111 return -1;
114 /* Try to open the file for reading ... */
116 if ((long)(fid1 = smbc_getFunctionOpen(c_file)(c_file, fname,
117 O_RDONLY, 0666)) < 0) {
118 DEBUG(3, ("Error, fname=%s, errno=%i\n", fname, errno));
119 TALLOC_FREE(frame);
120 return -1; /* smbc_open sets errno */
123 /* Now, try to open the printer file for writing */
125 if ((long)(fid2 = smbc_getFunctionOpenPrintJob(c_print)(c_print,
126 printq)) < 0) {
127 saverr = errno; /* Save errno */
128 smbc_getFunctionClose(c_file)(c_file, fid1);
129 errno = saverr;
130 TALLOC_FREE(frame);
131 return -1;
134 while ((bytes = smbc_getFunctionRead(c_file)(c_file, fid1,
135 buf, sizeof(buf))) > 0) {
136 tot_bytes += bytes;
138 if ((smbc_getFunctionWrite(c_print)(c_print, fid2,
139 buf, bytes)) < 0) {
140 saverr = errno;
141 smbc_getFunctionClose(c_file)(c_file, fid1);
142 smbc_getFunctionClose(c_print)(c_print, fid2);
143 errno = saverr;
147 saverr = errno;
149 smbc_getFunctionClose(c_file)(c_file, fid1);
150 smbc_getFunctionClose(c_print)(c_print, fid2);
152 if (bytes < 0) {
153 errno = saverr;
154 TALLOC_FREE(frame);
155 return -1;
158 TALLOC_FREE(frame);
159 return tot_bytes;
163 * Routine to list print jobs on a printer share ...
167 SMBC_list_print_jobs_ctx(SMBCCTX *context,
168 const char *fname,
169 smbc_list_print_job_fn fn)
171 SMBCSRV *srv = NULL;
172 char *server = NULL;
173 char *share = NULL;
174 char *user = NULL;
175 char *password = NULL;
176 char *workgroup = NULL;
177 char *path = NULL;
178 TALLOC_CTX *frame = talloc_stackframe();
180 if (!context || !context->internal->initialized) {
181 errno = EINVAL;
182 TALLOC_FREE(frame);
183 return -1;
186 if (!fname) {
187 errno = EINVAL;
188 TALLOC_FREE(frame);
189 return -1;
192 DEBUG(4, ("smbc_list_print_jobs(%s)\n", fname));
194 if (SMBC_parse_path(frame,
195 context,
196 fname,
197 &workgroup,
198 &server,
199 &share,
200 &path,
201 &user,
202 &password,
203 NULL)) {
204 errno = EINVAL;
205 TALLOC_FREE(frame);
206 return -1;
209 if (!user || user[0] == (char)0) {
210 user = talloc_strdup(frame, smbc_getUser(context));
211 if (!user) {
212 errno = ENOMEM;
213 TALLOC_FREE(frame);
214 return -1;
218 srv = SMBC_server(frame, context, True,
219 server, share, &workgroup, &user, &password);
221 if (!srv) {
222 TALLOC_FREE(frame);
223 return -1; /* errno set by SMBC_server */
226 if (cli_print_queue(srv->cli,
227 (void (*)(struct print_job_info *))fn) < 0) {
228 errno = SMBC_errno(context, srv->cli);
229 TALLOC_FREE(frame);
230 return -1;
233 TALLOC_FREE(frame);
234 return 0;
238 * Delete a print job from a remote printer share
242 SMBC_unlink_print_job_ctx(SMBCCTX *context,
243 const char *fname,
244 int id)
246 SMBCSRV *srv = NULL;
247 char *server = NULL;
248 char *share = NULL;
249 char *user = NULL;
250 char *password = NULL;
251 char *workgroup = NULL;
252 char *path = NULL;
253 int err;
254 TALLOC_CTX *frame = talloc_stackframe();
256 if (!context || !context->internal->initialized) {
257 errno = EINVAL;
258 TALLOC_FREE(frame);
259 return -1;
262 if (!fname) {
263 errno = EINVAL;
264 TALLOC_FREE(frame);
265 return -1;
268 DEBUG(4, ("smbc_unlink_print_job(%s)\n", fname));
270 if (SMBC_parse_path(frame,
271 context,
272 fname,
273 &workgroup,
274 &server,
275 &share,
276 &path,
277 &user,
278 &password,
279 NULL)) {
280 errno = EINVAL;
281 TALLOC_FREE(frame);
282 return -1;
285 if (!user || user[0] == (char)0) {
286 user = talloc_strdup(frame, smbc_getUser(context));
287 if (!user) {
288 errno = ENOMEM;
289 TALLOC_FREE(frame);
290 return -1;
294 srv = SMBC_server(frame, context, True,
295 server, share, &workgroup, &user, &password);
297 if (!srv) {
298 TALLOC_FREE(frame);
299 return -1; /* errno set by SMBC_server */
302 if ((err = cli_printjob_del(srv->cli, id)) != 0) {
303 if (err < 0)
304 errno = SMBC_errno(context, srv->cli);
305 else if (err == ERRnosuchprintjob)
306 errno = EINVAL;
307 TALLOC_FREE(frame);
308 return -1;
311 TALLOC_FREE(frame);
312 return 0;