s3: Fix some nonempty blank lines
[kai/samba.git] / source3 / libsmb / libsmb_printjob.c
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 "libsmbclient.h"
27 #include "libsmb_internal.h"
28
29
30 /*
31  * Open a print file to be written to by other calls
32  */
33
34 SMBCFILE *
35 SMBC_open_print_job_ctx(SMBCCTX *context,
36                         const char *fname)
37 {
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();
44
45         if (!context || !context->internal->initialized) {
46                 errno = EINVAL;
47                 TALLOC_FREE(frame);
48                 return NULL;
49         }
50
51         if (!fname) {
52                 errno = EINVAL;
53                 TALLOC_FREE(frame);
54                 return NULL;
55         }
56
57         DEBUG(4, ("SMBC_open_print_job_ctx(%s)\n", fname));
58
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;
72         }
73
74         /* What if the path is empty, or the file exists? */
75
76         TALLOC_FREE(frame);
77         return smbc_getFunctionOpen(context)(context, fname, O_WRONLY, 666);
78 }
79
80 /*
81  * Routine to print a file on a remote server ...
82  *
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.
85  */
86
87 int
88 SMBC_print_file_ctx(SMBCCTX *c_file,
89                     const char *fname,
90                     SMBCCTX *c_print,
91                     const char *printq)
92 {
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();
100
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;
106         }
107
108         if (!fname && !printq) {
109                 errno = EINVAL;
110                 TALLOC_FREE(frame);
111                 return -1;
112         }
113
114         /* Try to open the file for reading ... */
115
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 */
121         }
122
123         /* Now, try to open the printer file for writing */
124
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;
132         }
133
134         while ((bytes = smbc_getFunctionRead(c_file)(c_file, fid1,
135                                                      buf, sizeof(buf))) > 0) {
136                 tot_bytes += bytes;
137
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;
144                 }
145         }
146
147         saverr = errno;
148
149         smbc_getFunctionClose(c_file)(c_file, fid1);
150         smbc_getFunctionClose(c_print)(c_print, fid2);
151
152         if (bytes < 0) {
153                 errno = saverr;
154                 TALLOC_FREE(frame);
155                 return -1;
156         }
157
158         TALLOC_FREE(frame);
159         return tot_bytes;
160 }
161
162 /*
163  * Routine to list print jobs on a printer share ...
164  */
165
166 int
167 SMBC_list_print_jobs_ctx(SMBCCTX *context,
168                          const char *fname,
169                          smbc_list_print_job_fn fn)
170 {
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();
179
180         if (!context || !context->internal->initialized) {
181                 errno = EINVAL;
182                 TALLOC_FREE(frame);
183                 return -1;
184         }
185
186         if (!fname) {
187                 errno = EINVAL;
188                 TALLOC_FREE(frame);
189                 return -1;
190         }
191
192         DEBUG(4, ("smbc_list_print_jobs(%s)\n", fname));
193
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;
207         }
208
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;
215                 }
216         }
217
218         srv = SMBC_server(frame, context, True,
219                           server, share, &workgroup, &user, &password);
220
221         if (!srv) {
222                 TALLOC_FREE(frame);
223                 return -1;  /* errno set by SMBC_server */
224         }
225
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;
231         }
232
233         TALLOC_FREE(frame);
234         return 0;
235 }
236
237 /*
238  * Delete a print job from a remote printer share
239  */
240
241 int
242 SMBC_unlink_print_job_ctx(SMBCCTX *context,
243                           const char *fname,
244                           int id)
245 {
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();
255
256         if (!context || !context->internal->initialized) {
257                 errno = EINVAL;
258                 TALLOC_FREE(frame);
259                 return -1;
260         }
261
262         if (!fname) {
263                 errno = EINVAL;
264                 TALLOC_FREE(frame);
265                 return -1;
266         }
267
268         DEBUG(4, ("smbc_unlink_print_job(%s)\n", fname));
269
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;
283         }
284
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;
291                 }
292         }
293
294         srv = SMBC_server(frame, context, True,
295                           server, share, &workgroup, &user, &password);
296
297         if (!srv) {
298                 TALLOC_FREE(frame);
299                 return -1;  /* errno set by SMBC_server */
300         }
301
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;
309         }
310
311         TALLOC_FREE(frame);
312         return 0;
313 }
314