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
|
---|
10 |
|
---|
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.
|
---|
15 |
|
---|
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.
|
---|
20 |
|
---|
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/>.
|
---|
23 | */
|
---|
24 |
|
---|
25 | #include "includes.h"
|
---|
26 | #include "libsmb/libsmb.h"
|
---|
27 | #include "libsmbclient.h"
|
---|
28 | #include "libsmb_internal.h"
|
---|
29 |
|
---|
30 |
|
---|
31 | /*
|
---|
32 | * Open a print file to be written to by other calls
|
---|
33 | */
|
---|
34 |
|
---|
35 | SMBCFILE *
|
---|
36 | SMBC_open_print_job_ctx(SMBCCTX *context,
|
---|
37 | const char *fname)
|
---|
38 | {
|
---|
39 | char *server = NULL;
|
---|
40 | char *share = NULL;
|
---|
41 | char *user = NULL;
|
---|
42 | char *password = NULL;
|
---|
43 | char *path = NULL;
|
---|
44 | uint16_t port = 0;
|
---|
45 | TALLOC_CTX *frame = talloc_stackframe();
|
---|
46 |
|
---|
47 | if (!context || !context->internal->initialized) {
|
---|
48 | errno = EINVAL;
|
---|
49 | TALLOC_FREE(frame);
|
---|
50 | return NULL;
|
---|
51 | }
|
---|
52 |
|
---|
53 | if (!fname) {
|
---|
54 | errno = EINVAL;
|
---|
55 | TALLOC_FREE(frame);
|
---|
56 | return NULL;
|
---|
57 | }
|
---|
58 |
|
---|
59 | DEBUG(4, ("SMBC_open_print_job_ctx(%s)\n", fname));
|
---|
60 |
|
---|
61 | if (SMBC_parse_path(frame,
|
---|
62 | context,
|
---|
63 | fname,
|
---|
64 | NULL,
|
---|
65 | &server,
|
---|
66 | &port,
|
---|
67 | &share,
|
---|
68 | &path,
|
---|
69 | &user,
|
---|
70 | &password,
|
---|
71 | NULL)) {
|
---|
72 | errno = EINVAL;
|
---|
73 | TALLOC_FREE(frame);
|
---|
74 | return NULL;
|
---|
75 | }
|
---|
76 |
|
---|
77 | /* What if the path is empty, or the file exists? */
|
---|
78 |
|
---|
79 | TALLOC_FREE(frame);
|
---|
80 | return smbc_getFunctionOpen(context)(context, fname, O_WRONLY, 666);
|
---|
81 | }
|
---|
82 |
|
---|
83 | /*
|
---|
84 | * Routine to print a file on a remote server ...
|
---|
85 | *
|
---|
86 | * We open the file, which we assume to be on a remote server, and then
|
---|
87 | * copy it to a print file on the share specified by printq.
|
---|
88 | */
|
---|
89 |
|
---|
90 | int
|
---|
91 | SMBC_print_file_ctx(SMBCCTX *c_file,
|
---|
92 | const char *fname,
|
---|
93 | SMBCCTX *c_print,
|
---|
94 | const char *printq)
|
---|
95 | {
|
---|
96 | SMBCFILE *fid1;
|
---|
97 | SMBCFILE *fid2;
|
---|
98 | smbc_open_fn f_open1;
|
---|
99 | smbc_open_print_job_fn f_open_pj2;
|
---|
100 | int bytes;
|
---|
101 | int saverr;
|
---|
102 | int tot_bytes = 0;
|
---|
103 | char buf[4096];
|
---|
104 | TALLOC_CTX *frame = talloc_stackframe();
|
---|
105 |
|
---|
106 | if (!c_file || !c_file->internal->initialized ||
|
---|
107 | !c_print || !c_print->internal->initialized) {
|
---|
108 | errno = EINVAL;
|
---|
109 | TALLOC_FREE(frame);
|
---|
110 | return -1;
|
---|
111 | }
|
---|
112 |
|
---|
113 | if (!fname && !printq) {
|
---|
114 | errno = EINVAL;
|
---|
115 | TALLOC_FREE(frame);
|
---|
116 | return -1;
|
---|
117 | }
|
---|
118 |
|
---|
119 | /* Try to open the file for reading ... */
|
---|
120 | f_open1 = smbc_getFunctionOpen(c_file);
|
---|
121 | if (f_open1 == NULL) {
|
---|
122 | errno = EINVAL;
|
---|
123 | TALLOC_FREE(frame);
|
---|
124 | return -1;
|
---|
125 | }
|
---|
126 |
|
---|
127 | fid1 = f_open1(c_file, fname, O_RDONLY, 0666);
|
---|
128 | if (fid1 == NULL) {
|
---|
129 | DEBUG(3, ("Error, fname=%s, errno=%i\n", fname, errno));
|
---|
130 | TALLOC_FREE(frame);
|
---|
131 | return -1; /* smbc_open sets errno */
|
---|
132 | }
|
---|
133 |
|
---|
134 | /* Now, try to open the printer file for writing */
|
---|
135 | f_open_pj2 = smbc_getFunctionOpenPrintJob(c_print);
|
---|
136 | if (f_open_pj2 == NULL) {
|
---|
137 | errno = EINVAL;
|
---|
138 | TALLOC_FREE(frame);
|
---|
139 | return -1;
|
---|
140 | }
|
---|
141 |
|
---|
142 | fid2 = f_open_pj2(c_print, printq);
|
---|
143 | if (fid2 == NULL) {
|
---|
144 | saverr = errno; /* Save errno */
|
---|
145 | smbc_getFunctionClose(c_file)(c_file, fid1);
|
---|
146 | errno = saverr;
|
---|
147 | TALLOC_FREE(frame);
|
---|
148 | return -1;
|
---|
149 | }
|
---|
150 |
|
---|
151 | while ((bytes = smbc_getFunctionRead(c_file)(c_file, fid1,
|
---|
152 | buf, sizeof(buf))) > 0) {
|
---|
153 | tot_bytes += bytes;
|
---|
154 |
|
---|
155 | if ((smbc_getFunctionWrite(c_print)(c_print, fid2,
|
---|
156 | buf, bytes)) < 0) {
|
---|
157 | saverr = errno;
|
---|
158 | smbc_getFunctionClose(c_file)(c_file, fid1);
|
---|
159 | smbc_getFunctionClose(c_print)(c_print, fid2);
|
---|
160 | errno = saverr;
|
---|
161 | }
|
---|
162 | }
|
---|
163 |
|
---|
164 | saverr = errno;
|
---|
165 |
|
---|
166 | smbc_getFunctionClose(c_file)(c_file, fid1);
|
---|
167 | smbc_getFunctionClose(c_print)(c_print, fid2);
|
---|
168 |
|
---|
169 | if (bytes < 0) {
|
---|
170 | errno = saverr;
|
---|
171 | TALLOC_FREE(frame);
|
---|
172 | return -1;
|
---|
173 | }
|
---|
174 |
|
---|
175 | TALLOC_FREE(frame);
|
---|
176 | return tot_bytes;
|
---|
177 | }
|
---|
178 |
|
---|
179 | /*
|
---|
180 | * Routine to list print jobs on a printer share ...
|
---|
181 | */
|
---|
182 |
|
---|
183 | int
|
---|
184 | SMBC_list_print_jobs_ctx(SMBCCTX *context,
|
---|
185 | const char *fname,
|
---|
186 | smbc_list_print_job_fn fn)
|
---|
187 | {
|
---|
188 | SMBCSRV *srv = NULL;
|
---|
189 | char *server = NULL;
|
---|
190 | char *share = NULL;
|
---|
191 | char *user = NULL;
|
---|
192 | char *password = NULL;
|
---|
193 | char *workgroup = NULL;
|
---|
194 | char *path = NULL;
|
---|
195 | uint16_t port = 0;
|
---|
196 | TALLOC_CTX *frame = talloc_stackframe();
|
---|
197 |
|
---|
198 | if (!context || !context->internal->initialized) {
|
---|
199 | errno = EINVAL;
|
---|
200 | TALLOC_FREE(frame);
|
---|
201 | return -1;
|
---|
202 | }
|
---|
203 |
|
---|
204 | if (!fname) {
|
---|
205 | errno = EINVAL;
|
---|
206 | TALLOC_FREE(frame);
|
---|
207 | return -1;
|
---|
208 | }
|
---|
209 |
|
---|
210 | DEBUG(4, ("smbc_list_print_jobs(%s)\n", fname));
|
---|
211 |
|
---|
212 | if (SMBC_parse_path(frame,
|
---|
213 | context,
|
---|
214 | fname,
|
---|
215 | &workgroup,
|
---|
216 | &server,
|
---|
217 | &port,
|
---|
218 | &share,
|
---|
219 | &path,
|
---|
220 | &user,
|
---|
221 | &password,
|
---|
222 | NULL)) {
|
---|
223 | errno = EINVAL;
|
---|
224 | TALLOC_FREE(frame);
|
---|
225 | return -1;
|
---|
226 | }
|
---|
227 |
|
---|
228 | if (!user || user[0] == (char)0) {
|
---|
229 | user = talloc_strdup(frame, smbc_getUser(context));
|
---|
230 | if (!user) {
|
---|
231 | errno = ENOMEM;
|
---|
232 | TALLOC_FREE(frame);
|
---|
233 | return -1;
|
---|
234 | }
|
---|
235 | }
|
---|
236 |
|
---|
237 | srv = SMBC_server(frame, context, True,
|
---|
238 | server, port, share, &workgroup, &user, &password);
|
---|
239 |
|
---|
240 | if (!srv) {
|
---|
241 | TALLOC_FREE(frame);
|
---|
242 | return -1; /* errno set by SMBC_server */
|
---|
243 | }
|
---|
244 |
|
---|
245 | if (cli_print_queue(srv->cli,
|
---|
246 | (void (*)(struct print_job_info *))fn) < 0) {
|
---|
247 | errno = SMBC_errno(context, srv->cli);
|
---|
248 | TALLOC_FREE(frame);
|
---|
249 | return -1;
|
---|
250 | }
|
---|
251 |
|
---|
252 | TALLOC_FREE(frame);
|
---|
253 | return 0;
|
---|
254 | }
|
---|
255 |
|
---|
256 | /*
|
---|
257 | * Delete a print job from a remote printer share
|
---|
258 | */
|
---|
259 |
|
---|
260 | int
|
---|
261 | SMBC_unlink_print_job_ctx(SMBCCTX *context,
|
---|
262 | const char *fname,
|
---|
263 | int id)
|
---|
264 | {
|
---|
265 | SMBCSRV *srv = NULL;
|
---|
266 | char *server = NULL;
|
---|
267 | char *share = NULL;
|
---|
268 | char *user = NULL;
|
---|
269 | char *password = NULL;
|
---|
270 | char *workgroup = NULL;
|
---|
271 | char *path = NULL;
|
---|
272 | int err;
|
---|
273 | uint16_t port = 0;
|
---|
274 | TALLOC_CTX *frame = talloc_stackframe();
|
---|
275 |
|
---|
276 | if (!context || !context->internal->initialized) {
|
---|
277 | errno = EINVAL;
|
---|
278 | TALLOC_FREE(frame);
|
---|
279 | return -1;
|
---|
280 | }
|
---|
281 |
|
---|
282 | if (!fname) {
|
---|
283 | errno = EINVAL;
|
---|
284 | TALLOC_FREE(frame);
|
---|
285 | return -1;
|
---|
286 | }
|
---|
287 |
|
---|
288 | DEBUG(4, ("smbc_unlink_print_job(%s)\n", fname));
|
---|
289 |
|
---|
290 | if (SMBC_parse_path(frame,
|
---|
291 | context,
|
---|
292 | fname,
|
---|
293 | &workgroup,
|
---|
294 | &server,
|
---|
295 | &port,
|
---|
296 | &share,
|
---|
297 | &path,
|
---|
298 | &user,
|
---|
299 | &password,
|
---|
300 | NULL)) {
|
---|
301 | errno = EINVAL;
|
---|
302 | TALLOC_FREE(frame);
|
---|
303 | return -1;
|
---|
304 | }
|
---|
305 |
|
---|
306 | if (!user || user[0] == (char)0) {
|
---|
307 | user = talloc_strdup(frame, smbc_getUser(context));
|
---|
308 | if (!user) {
|
---|
309 | errno = ENOMEM;
|
---|
310 | TALLOC_FREE(frame);
|
---|
311 | return -1;
|
---|
312 | }
|
---|
313 | }
|
---|
314 |
|
---|
315 | srv = SMBC_server(frame, context, True,
|
---|
316 | server, port, share, &workgroup, &user, &password);
|
---|
317 |
|
---|
318 | if (!srv) {
|
---|
319 | TALLOC_FREE(frame);
|
---|
320 | return -1; /* errno set by SMBC_server */
|
---|
321 | }
|
---|
322 |
|
---|
323 | if ((err = cli_printjob_del(srv->cli, id)) != 0) {
|
---|
324 | if (err < 0)
|
---|
325 | errno = SMBC_errno(context, srv->cli);
|
---|
326 | else if (err == ERRnosuchprintjob)
|
---|
327 | errno = EINVAL;
|
---|
328 | TALLOC_FREE(frame);
|
---|
329 | return -1;
|
---|
330 | }
|
---|
331 |
|
---|
332 | TALLOC_FREE(frame);
|
---|
333 | return 0;
|
---|
334 | }
|
---|
335 |
|
---|