source: trunk/server/source3/printing/print_cups.c

Last change on this file was 751, checked in by Silvan Scherrer, 13 years ago

Samba Server: updated trunk to 3.6.9

File size: 40.2 KB
Line 
1/*
2 * Support code for the Common UNIX Printing System ("CUPS")
3 *
4 * Copyright 1999-2003 by Michael R Sweet.
5 * Copyright 2008 Jeremy Allison.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, see <http://www.gnu.org/licenses/>.
19 */
20
21/*
22 * JRA. Converted to utf8 pull/push.
23 */
24
25#include "includes.h"
26#include "printing.h"
27#include "printing/pcap.h"
28#include "librpc/gen_ndr/ndr_printcap.h"
29
30#ifdef HAVE_CUPS
31#include <cups/cups.h>
32#include <cups/language.h>
33
34#ifdef __OS2__
35#define pipe(A) os2_pipe(A)
36#endif
37
38#if (CUPS_VERSION_MAJOR > 1) || (CUPS_VERSION_MINOR > 5)
39#define HAVE_CUPS_1_6 1
40#endif
41
42#ifndef HAVE_CUPS_1_6
43#define ippGetGroupTag(attr) attr->group_tag
44#define ippGetName(attr) attr->name
45#define ippGetValueTag(attr) attr->value_tag
46#define ippGetStatusCode(ipp) ipp->request.status.status_code
47#define ippGetInteger(attr, element) attr->values[element].integer
48#define ippGetString(attr, element, language) attr->values[element].string.text
49
50static ipp_attribute_t *
51ippFirstAttribute(ipp_t *ipp)
52{
53 if (!ipp)
54 return (NULL);
55 return (ipp->current = ipp->attrs);
56}
57
58static ipp_attribute_t *
59ippNextAttribute(ipp_t *ipp)
60{
61 if (!ipp || !ipp->current)
62 return (NULL);
63 return (ipp->current = ipp->current->next);
64}
65
66static int ippSetOperation(ipp_t *ipp, ipp_op_t op)
67{
68 ipp->request.op.operation_id = op;
69 return (1);
70}
71
72static int ippSetRequestId(ipp_t *ipp, int request_id)
73{
74 ipp->request.any.request_id = request_id;
75 return (1);
76}
77#endif
78
79static SIG_ATOMIC_T gotalarm;
80
81/***************************************************************
82 Signal function to tell us we timed out.
83****************************************************************/
84
85static void gotalarm_sig(int signum)
86{
87 gotalarm = 1;
88}
89
90extern userdom_struct current_user_info;
91
92/*
93 * 'cups_passwd_cb()' - The CUPS password callback...
94 */
95
96static const char * /* O - Password or NULL */
97cups_passwd_cb(const char *prompt) /* I - Prompt */
98{
99 /*
100 * Always return NULL to indicate that no password is available...
101 */
102
103 return (NULL);
104}
105
106static http_t *cups_connect(TALLOC_CTX *frame)
107{
108 http_t *http = NULL;
109 char *server = NULL, *p = NULL;
110 int port;
111 int timeout = lp_cups_connection_timeout();
112 size_t size;
113
114 if (lp_cups_server() != NULL && strlen(lp_cups_server()) > 0) {
115 if (!push_utf8_talloc(frame, &server, lp_cups_server(), &size)) {
116 return NULL;
117 }
118 } else {
119 server = talloc_strdup(frame,cupsServer());
120 }
121 if (!server) {
122 return NULL;
123 }
124
125 p = strchr(server, ':');
126 if (p) {
127 port = atoi(p+1);
128 *p = '\0';
129 } else {
130 port = ippPort();
131 }
132
133 DEBUG(10, ("connecting to cups server %s:%d\n",
134 server, port));
135
136 gotalarm = 0;
137
138 if (timeout) {
139 CatchSignal(SIGALRM, gotalarm_sig);
140 alarm(timeout);
141 }
142
143#ifdef HAVE_HTTPCONNECTENCRYPT
144 http = httpConnectEncrypt(server, port, lp_cups_encrypt());
145#else
146 http = httpConnect(server, port);
147#endif
148
149
150 CatchSignal(SIGALRM, SIG_IGN);
151 alarm(0);
152
153 if (http == NULL) {
154 DEBUG(0,("Unable to connect to CUPS server %s:%d - %s\n",
155 server, port, strerror(errno)));
156 }
157
158 return http;
159}
160
161static bool send_pcap_blob(DATA_BLOB *pcap_blob, int fd)
162{
163 size_t ret;
164
165 ret = sys_write(fd, &pcap_blob->length, sizeof(pcap_blob->length));
166 if (ret != sizeof(pcap_blob->length)) {
167 return false;
168 }
169
170 ret = sys_write(fd, pcap_blob->data, pcap_blob->length);
171 if (ret != pcap_blob->length) {
172 return false;
173 }
174
175 DEBUG(10, ("successfully sent blob of len %d\n", (int)ret));
176 return true;
177}
178
179static bool recv_pcap_blob(TALLOC_CTX *mem_ctx, int fd, DATA_BLOB *pcap_blob)
180{
181 size_t blob_len;
182 size_t ret;
183
184 ret = sys_read(fd, &blob_len, sizeof(blob_len));
185 if (ret != sizeof(blob_len)) {
186 return false;
187 }
188
189 *pcap_blob = data_blob_talloc_named(mem_ctx, NULL, blob_len,
190 "cups pcap");
191 if (pcap_blob->length != blob_len) {
192 return false;
193 }
194 ret = sys_read(fd, pcap_blob->data, blob_len);
195 if (ret != blob_len) {
196 talloc_free(pcap_blob->data);
197 return false;
198 }
199
200 DEBUG(10, ("successfully recvd blob of len %d\n", (int)ret));
201 return true;
202}
203
204static bool process_cups_printers_response(TALLOC_CTX *mem_ctx,
205 ipp_t *response,
206 struct pcap_data *pcap_data)
207{
208 ipp_attribute_t *attr;
209 char *name;
210 char *info;
211 char *location = NULL;
212 struct pcap_printer *printer;
213 bool ret_ok = false;
214
215 for (attr = ippFirstAttribute(response); attr != NULL;) {
216 /*
217 * Skip leading attributes until we hit a printer...
218 */
219
220 while (attr != NULL && ippGetGroupTag(attr) != IPP_TAG_PRINTER)
221 attr = ippNextAttribute(response);
222
223 if (attr == NULL)
224 break;
225
226 /*
227 * Pull the needed attributes from this printer...
228 */
229
230 name = NULL;
231 info = NULL;
232
233 while (attr != NULL && ippGetGroupTag(attr) == IPP_TAG_PRINTER) {
234 size_t size;
235 if (strcmp(ippGetName(attr), "printer-name") == 0 &&
236 ippGetValueTag(attr) == IPP_TAG_NAME) {
237 if (!pull_utf8_talloc(mem_ctx,
238 &name,
239 ippGetString(attr, 0, NULL),
240 &size)) {
241 goto err_out;
242 }
243 }
244
245 if (strcmp(ippGetName(attr), "printer-info") == 0 &&
246 ippGetValueTag(attr) == IPP_TAG_TEXT) {
247 if (!pull_utf8_talloc(mem_ctx,
248 &info,
249 ippGetString(attr, 0, NULL),
250 &size)) {
251 goto err_out;
252 }
253 }
254
255 if (strcmp(ippGetName(attr), "printer-location") == 0 &&
256 ippGetValueTag(attr) == IPP_TAG_TEXT) {
257 if (!pull_utf8_talloc(mem_ctx,
258 &location,
259 ippGetString(attr, 0, NULL),
260 &size)) {
261 goto err_out;
262 }
263 }
264
265 attr = ippNextAttribute(response);
266 }
267
268 /*
269 * See if we have everything needed...
270 */
271
272 if (name == NULL)
273 break;
274
275 if (pcap_data->count == 0) {
276 printer = talloc_array(mem_ctx, struct pcap_printer, 1);
277 } else {
278 printer = talloc_realloc(mem_ctx, pcap_data->printers,
279 struct pcap_printer,
280 pcap_data->count + 1);
281 }
282 if (printer == NULL) {
283 goto err_out;
284 }
285 pcap_data->printers = printer;
286 pcap_data->printers[pcap_data->count].name = name;
287 pcap_data->printers[pcap_data->count].info = info;
288 pcap_data->printers[pcap_data->count].location = location;
289 pcap_data->count++;
290 }
291
292 ret_ok = true;
293err_out:
294 return ret_ok;
295}
296
297/*
298 * request printer list from cups, send result back to up parent via fd.
299 * returns true if the (possibly failed) result was successfuly sent to parent.
300 */
301static bool cups_cache_reload_async(int fd)
302{
303 TALLOC_CTX *frame = talloc_stackframe();
304 struct pcap_data pcap_data;
305 http_t *http = NULL; /* HTTP connection to server */
306 ipp_t *request = NULL, /* IPP Request */
307 *response = NULL; /* IPP Response */
308 cups_lang_t *language = NULL; /* Default language */
309 static const char *requested[] =/* Requested attributes */
310 {
311 "printer-name",
312 "printer-info",
313 "printer-location"
314 };
315 bool ret = False;
316 enum ndr_err_code ndr_ret;
317 DATA_BLOB pcap_blob;
318
319 ZERO_STRUCT(pcap_data);
320 pcap_data.status = NT_STATUS_UNSUCCESSFUL;
321
322 DEBUG(5, ("reloading cups printcap cache\n"));
323
324 /*
325 * Make sure we don't ask for passwords...
326 */
327
328 cupsSetPasswordCB(cups_passwd_cb);
329
330 if ((http = cups_connect(frame)) == NULL) {
331 goto out;
332 }
333
334 /*
335 * Build a CUPS_GET_PRINTERS request, which requires the following
336 * attributes:
337 *
338 * attributes-charset
339 * attributes-natural-language
340 * requested-attributes
341 */
342
343 request = ippNew();
344
345 ippSetOperation(request, CUPS_GET_PRINTERS);
346 ippSetRequestId(request, 1);
347
348 language = cupsLangDefault();
349
350 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
351 "attributes-charset", NULL, "utf-8");
352
353 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
354 "attributes-natural-language", NULL, language->language);
355
356 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
357 "requested-attributes",
358 (sizeof(requested) / sizeof(requested[0])),
359 NULL, requested);
360
361 if ((response = cupsDoRequest(http, request, "/")) == NULL) {
362 DEBUG(0,("Unable to get printer list - %s\n",
363 ippErrorString(cupsLastError())));
364 goto out;
365 }
366
367 ret = process_cups_printers_response(frame, response, &pcap_data);
368 if (!ret) {
369 DEBUG(0,("failed to process cups response\n"));
370 goto out;
371 }
372
373 ippDelete(response);
374 response = NULL;
375
376 /*
377 * Build a CUPS_GET_CLASSES request, which requires the following
378 * attributes:
379 *
380 * attributes-charset
381 * attributes-natural-language
382 * requested-attributes
383 */
384
385 request = ippNew();
386
387 ippSetOperation(request, CUPS_GET_CLASSES);
388 ippSetRequestId(request, 1);
389
390 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
391 "attributes-charset", NULL, "utf-8");
392
393 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
394 "attributes-natural-language", NULL, language->language);
395
396 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
397 "requested-attributes",
398 (sizeof(requested) / sizeof(requested[0])),
399 NULL, requested);
400
401 if ((response = cupsDoRequest(http, request, "/")) == NULL) {
402 DEBUG(0,("Unable to get printer list - %s\n",
403 ippErrorString(cupsLastError())));
404 goto out;
405 }
406
407 ret = process_cups_printers_response(frame, response, &pcap_data);
408 if (!ret) {
409 DEBUG(0,("failed to process cups response\n"));
410 goto out;
411 }
412
413 pcap_data.status = NT_STATUS_OK;
414 out:
415 if (response)
416 ippDelete(response);
417
418 if (language)
419 cupsLangFree(language);
420
421 if (http)
422 httpClose(http);
423
424 ret = false;
425 ndr_ret = ndr_push_struct_blob(&pcap_blob, frame, &pcap_data,
426 (ndr_push_flags_fn_t)ndr_push_pcap_data);
427 if (ndr_ret == NDR_ERR_SUCCESS) {
428 ret = send_pcap_blob(&pcap_blob, fd);
429 }
430
431 TALLOC_FREE(frame);
432 return ret;
433}
434
435static struct fd_event *cache_fd_event;
436
437static bool cups_pcap_load_async(struct tevent_context *ev,
438 struct messaging_context *msg_ctx,
439 int *pfd)
440{
441 int fds[2];
442 pid_t pid;
443 NTSTATUS status;
444
445 *pfd = -1;
446
447 if (cache_fd_event) {
448 DEBUG(3,("cups_pcap_load_async: already waiting for "
449 "a refresh event\n" ));
450 return false;
451 }
452
453 DEBUG(5,("cups_pcap_load_async: asynchronously loading cups printers\n"));
454
455 if (pipe(fds) == -1) {
456 return false;
457 }
458
459 pid = sys_fork();
460 if (pid == (pid_t)-1) {
461 DEBUG(10,("cups_pcap_load_async: fork failed %s\n",
462 strerror(errno) ));
463 close(fds[0]);
464 close(fds[1]);
465 return false;
466 }
467
468 if (pid) {
469 DEBUG(10,("cups_pcap_load_async: child pid = %u\n",
470 (unsigned int)pid ));
471 /* Parent. */
472 close(fds[1]);
473 *pfd = fds[0];
474 return true;
475 }
476
477 /* Child. */
478
479 close_all_print_db();
480
481 status = reinit_after_fork(msg_ctx, ev, procid_self(), true);
482 if (!NT_STATUS_IS_OK(status)) {
483 DEBUG(0,("cups_pcap_load_async: reinit_after_fork() failed\n"));
484 smb_panic("cups_pcap_load_async: reinit_after_fork() failed");
485 }
486
487 close(fds[0]);
488 cups_cache_reload_async(fds[1]);
489 close(fds[1]);
490 _exit(0);
491}
492
493struct cups_async_cb_args {
494 int pipe_fd;
495 struct event_context *event_ctx;
496 struct messaging_context *msg_ctx;
497 void (*post_cache_fill_fn)(struct event_context *,
498 struct messaging_context *);
499};
500
501static void cups_async_callback(struct event_context *event_ctx,
502 struct fd_event *event,
503 uint16 flags,
504 void *p)
505{
506 TALLOC_CTX *frame = talloc_stackframe();
507 struct cups_async_cb_args *cb_args = (struct cups_async_cb_args *)p;
508 struct pcap_cache *tmp_pcap_cache = NULL;
509 bool ret_ok;
510 struct pcap_data pcap_data;
511 DATA_BLOB pcap_blob;
512 enum ndr_err_code ndr_ret;
513 int i;
514
515 DEBUG(5,("cups_async_callback: callback received for printer data. "
516 "fd = %d\n", cb_args->pipe_fd));
517
518 ret_ok = recv_pcap_blob(frame, cb_args->pipe_fd, &pcap_blob);
519 if (!ret_ok) {
520 DEBUG(0,("failed to recv pcap blob\n"));
521 goto err_out;
522 }
523
524 ndr_ret = ndr_pull_struct_blob(&pcap_blob, frame, &pcap_data,
525 (ndr_pull_flags_fn_t)ndr_pull_pcap_data);
526 if (ndr_ret != NDR_ERR_SUCCESS) {
527 goto err_out;
528 }
529
530 if (!NT_STATUS_IS_OK(pcap_data.status)) {
531 DEBUG(0,("failed to retrieve printer list: %s\n",
532 nt_errstr(pcap_data.status)));
533 goto err_out;
534 }
535
536 for (i = 0; i < pcap_data.count; i++) {
537 ret_ok = pcap_cache_add_specific(&tmp_pcap_cache,
538 pcap_data.printers[i].name,
539 pcap_data.printers[i].info,
540 pcap_data.printers[i].location);
541 if (!ret_ok) {
542 DEBUG(0, ("failed to add to tmp pcap cache\n"));
543 goto err_out;
544 }
545 }
546
547 /* replace the system-wide pcap cache with a (possibly empty) new one */
548 ret_ok = pcap_cache_replace(tmp_pcap_cache);
549 if (!ret_ok) {
550 DEBUG(0, ("failed to replace pcap cache\n"));
551 } else if (cb_args->post_cache_fill_fn != NULL) {
552 /* Caller requested post cache fill callback */
553 cb_args->post_cache_fill_fn(cb_args->event_ctx,
554 cb_args->msg_ctx);
555 }
556err_out:
557 pcap_cache_destroy_specific(&tmp_pcap_cache);
558 TALLOC_FREE(frame);
559 close(cb_args->pipe_fd);
560 TALLOC_FREE(cb_args);
561 TALLOC_FREE(cache_fd_event);
562}
563
564bool cups_cache_reload(struct tevent_context *ev,
565 struct messaging_context *msg_ctx,
566 void (*post_cache_fill_fn)(struct tevent_context *,
567 struct messaging_context *))
568{
569 struct cups_async_cb_args *cb_args;
570 int *p_pipe_fd;
571
572 cb_args = TALLOC_P(NULL, struct cups_async_cb_args);
573 if (cb_args == NULL) {
574 return false;
575 }
576
577 cb_args->post_cache_fill_fn = post_cache_fill_fn;
578 cb_args->event_ctx = ev;
579 cb_args->msg_ctx = msg_ctx;
580 p_pipe_fd = &cb_args->pipe_fd;
581 *p_pipe_fd = -1;
582
583 /* Set up an async refresh. */
584 if (!cups_pcap_load_async(ev, msg_ctx, p_pipe_fd)) {
585 talloc_free(cb_args);
586 return false;
587 }
588
589 DEBUG(10,("cups_cache_reload: async read on fd %d\n",
590 *p_pipe_fd ));
591
592 /* Trigger an event when the pipe can be read. */
593 cache_fd_event = event_add_fd(ev,
594 NULL, *p_pipe_fd,
595 EVENT_FD_READ,
596 cups_async_callback,
597 (void *)cb_args);
598 if (!cache_fd_event) {
599 close(*p_pipe_fd);
600 TALLOC_FREE(cb_args);
601 return false;
602 }
603
604 return true;
605}
606
607/*
608 * 'cups_job_delete()' - Delete a job.
609 */
610
611static int cups_job_delete(const char *sharename, const char *lprm_command, struct printjob *pjob)
612{
613 TALLOC_CTX *frame = talloc_stackframe();
614 int ret = 1; /* Return value */
615 http_t *http = NULL; /* HTTP connection to server */
616 ipp_t *request = NULL, /* IPP Request */
617 *response = NULL; /* IPP Response */
618 cups_lang_t *language = NULL; /* Default language */
619 char *user = NULL;
620 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
621 size_t size;
622
623 DEBUG(5,("cups_job_delete(%s, %p (%d))\n", sharename, pjob, pjob->sysjob));
624
625 /*
626 * Make sure we don't ask for passwords...
627 */
628
629 cupsSetPasswordCB(cups_passwd_cb);
630
631 /*
632 * Try to connect to the server...
633 */
634
635 if ((http = cups_connect(frame)) == NULL) {
636 goto out;
637 }
638
639 /*
640 * Build an IPP_CANCEL_JOB request, which requires the following
641 * attributes:
642 *
643 * attributes-charset
644 * attributes-natural-language
645 * job-uri
646 * requesting-user-name
647 */
648
649 request = ippNew();
650
651 ippSetOperation(request, IPP_CANCEL_JOB);
652 ippSetRequestId(request, 1);
653
654 language = cupsLangDefault();
655
656 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
657 "attributes-charset", NULL, "utf-8");
658
659 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
660 "attributes-natural-language", NULL, language->language);
661
662 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/jobs/%d", pjob->sysjob);
663
664 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri", NULL, uri);
665
666 if (!push_utf8_talloc(frame, &user, pjob->user, &size)) {
667 goto out;
668 }
669
670 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
671 NULL, user);
672
673 /*
674 * Do the request and get back a response...
675 */
676
677 if ((response = cupsDoRequest(http, request, "/jobs")) != NULL) {
678 if (ippGetStatusCode(response) >= IPP_OK_CONFLICT) {
679 DEBUG(0,("Unable to cancel job %d - %s\n", pjob->sysjob,
680 ippErrorString(cupsLastError())));
681 } else {
682 ret = 0;
683 }
684 } else {
685 DEBUG(0,("Unable to cancel job %d - %s\n", pjob->sysjob,
686 ippErrorString(cupsLastError())));
687 }
688
689 out:
690 if (response)
691 ippDelete(response);
692
693 if (language)
694 cupsLangFree(language);
695
696 if (http)
697 httpClose(http);
698
699 TALLOC_FREE(frame);
700 return ret;
701}
702
703
704/*
705 * 'cups_job_pause()' - Pause a job.
706 */
707
708static int cups_job_pause(int snum, struct printjob *pjob)
709{
710 TALLOC_CTX *frame = talloc_stackframe();
711 int ret = 1; /* Return value */
712 http_t *http = NULL; /* HTTP connection to server */
713 ipp_t *request = NULL, /* IPP Request */
714 *response = NULL; /* IPP Response */
715 cups_lang_t *language = NULL; /* Default language */
716 char *user = NULL;
717 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
718 size_t size;
719
720 DEBUG(5,("cups_job_pause(%d, %p (%d))\n", snum, pjob, pjob->sysjob));
721
722 /*
723 * Make sure we don't ask for passwords...
724 */
725
726 cupsSetPasswordCB(cups_passwd_cb);
727
728 /*
729 * Try to connect to the server...
730 */
731
732 if ((http = cups_connect(frame)) == NULL) {
733 goto out;
734 }
735
736 /*
737 * Build an IPP_HOLD_JOB request, which requires the following
738 * attributes:
739 *
740 * attributes-charset
741 * attributes-natural-language
742 * job-uri
743 * requesting-user-name
744 */
745
746 request = ippNew();
747
748 ippSetOperation(request, IPP_HOLD_JOB);
749 ippSetRequestId(request, 1);
750
751 language = cupsLangDefault();
752
753 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
754 "attributes-charset", NULL, "utf-8");
755
756 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
757 "attributes-natural-language", NULL, language->language);
758
759 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/jobs/%d", pjob->sysjob);
760
761 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri", NULL, uri);
762
763 if (!push_utf8_talloc(frame, &user, pjob->user, &size)) {
764 goto out;
765 }
766 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
767 NULL, user);
768
769 /*
770 * Do the request and get back a response...
771 */
772
773 if ((response = cupsDoRequest(http, request, "/jobs")) != NULL) {
774 if (ippGetStatusCode(response) >= IPP_OK_CONFLICT) {
775 DEBUG(0,("Unable to hold job %d - %s\n", pjob->sysjob,
776 ippErrorString(cupsLastError())));
777 } else {
778 ret = 0;
779 }
780 } else {
781 DEBUG(0,("Unable to hold job %d - %s\n", pjob->sysjob,
782 ippErrorString(cupsLastError())));
783 }
784
785 out:
786 if (response)
787 ippDelete(response);
788
789 if (language)
790 cupsLangFree(language);
791
792 if (http)
793 httpClose(http);
794
795 TALLOC_FREE(frame);
796 return ret;
797}
798
799
800/*
801 * 'cups_job_resume()' - Resume a paused job.
802 */
803
804static int cups_job_resume(int snum, struct printjob *pjob)
805{
806 TALLOC_CTX *frame = talloc_stackframe();
807 int ret = 1; /* Return value */
808 http_t *http = NULL; /* HTTP connection to server */
809 ipp_t *request = NULL, /* IPP Request */
810 *response = NULL; /* IPP Response */
811 cups_lang_t *language = NULL; /* Default language */
812 char *user = NULL;
813 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
814 size_t size;
815
816 DEBUG(5,("cups_job_resume(%d, %p (%d))\n", snum, pjob, pjob->sysjob));
817
818 /*
819 * Make sure we don't ask for passwords...
820 */
821
822 cupsSetPasswordCB(cups_passwd_cb);
823
824 /*
825 * Try to connect to the server...
826 */
827
828 if ((http = cups_connect(frame)) == NULL) {
829 goto out;
830 }
831
832 /*
833 * Build an IPP_RELEASE_JOB request, which requires the following
834 * attributes:
835 *
836 * attributes-charset
837 * attributes-natural-language
838 * job-uri
839 * requesting-user-name
840 */
841
842 request = ippNew();
843
844 ippSetOperation(request, IPP_RELEASE_JOB);
845 ippSetRequestId(request, 1);
846
847 language = cupsLangDefault();
848
849 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
850 "attributes-charset", NULL, "utf-8");
851
852 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
853 "attributes-natural-language", NULL, language->language);
854
855 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/jobs/%d", pjob->sysjob);
856
857 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri", NULL, uri);
858
859 if (!push_utf8_talloc(frame, &user, pjob->user, &size)) {
860 goto out;
861 }
862 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
863 NULL, user);
864
865 /*
866 * Do the request and get back a response...
867 */
868
869 if ((response = cupsDoRequest(http, request, "/jobs")) != NULL) {
870 if (ippGetStatusCode(response) >= IPP_OK_CONFLICT) {
871 DEBUG(0,("Unable to release job %d - %s\n", pjob->sysjob,
872 ippErrorString(cupsLastError())));
873 } else {
874 ret = 0;
875 }
876 } else {
877 DEBUG(0,("Unable to release job %d - %s\n", pjob->sysjob,
878 ippErrorString(cupsLastError())));
879 }
880
881 out:
882 if (response)
883 ippDelete(response);
884
885 if (language)
886 cupsLangFree(language);
887
888 if (http)
889 httpClose(http);
890
891 TALLOC_FREE(frame);
892 return ret;
893}
894
895
896/*
897 * 'cups_job_submit()' - Submit a job for printing.
898 */
899
900static int cups_job_submit(int snum, struct printjob *pjob,
901 enum printing_types printing_type,
902 char *lpq_cmd)
903{
904 TALLOC_CTX *frame = talloc_stackframe();
905 int ret = 1; /* Return value */
906 http_t *http = NULL; /* HTTP connection to server */
907 ipp_t *request = NULL, /* IPP Request */
908 *response = NULL; /* IPP Response */
909 ipp_attribute_t *attr_job_id = NULL; /* IPP Attribute "job-id" */
910 cups_lang_t *language = NULL; /* Default language */
911 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
912 char *new_jobname = NULL;
913 int num_options = 0;
914 cups_option_t *options = NULL;
915 char *printername = NULL;
916 char *user = NULL;
917 char *jobname = NULL;
918 char *cupsoptions = NULL;
919 char *filename = NULL;
920 size_t size;
921
922 DEBUG(5,("cups_job_submit(%d, %p)\n", snum, pjob));
923
924 /*
925 * Make sure we don't ask for passwords...
926 */
927
928 cupsSetPasswordCB(cups_passwd_cb);
929
930 /*
931 * Try to connect to the server...
932 */
933
934 if ((http = cups_connect(frame)) == NULL) {
935 goto out;
936 }
937
938 /*
939 * Build an IPP_PRINT_JOB request, which requires the following
940 * attributes:
941 *
942 * attributes-charset
943 * attributes-natural-language
944 * printer-uri
945 * requesting-user-name
946 * [document-data]
947 */
948
949 request = ippNew();
950
951 ippSetOperation(request, IPP_PRINT_JOB);
952 ippSetRequestId(request, 1);
953
954 language = cupsLangDefault();
955
956 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
957 "attributes-charset", NULL, "utf-8");
958
959 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
960 "attributes-natural-language", NULL, language->language);
961
962 if (!push_utf8_talloc(frame, &printername, lp_printername(snum),
963 &size)) {
964 goto out;
965 }
966 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/printers/%s",
967 printername);
968
969 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
970 "printer-uri", NULL, uri);
971
972 if (!push_utf8_talloc(frame, &user, pjob->user, &size)) {
973 goto out;
974 }
975 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
976 NULL, user);
977
978 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
979 "job-originating-host-name", NULL,
980 pjob->clientmachine);
981
982 if (!push_utf8_talloc(frame, &jobname, pjob->jobname, &size)) {
983 goto out;
984 }
985 new_jobname = talloc_asprintf(frame,
986 "%s%.8u %s", PRINT_SPOOL_PREFIX,
987 pjob->jobid, jobname);
988 if (new_jobname == NULL) {
989 goto out;
990 }
991
992 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "job-name", NULL,
993 new_jobname);
994
995 /*
996 * add any options defined in smb.conf
997 */
998
999 if (!push_utf8_talloc(frame, &cupsoptions, lp_cups_options(snum), &size)) {
1000 goto out;
1001 }
1002 num_options = 0;
1003 options = NULL;
1004 num_options = cupsParseOptions(cupsoptions, num_options, &options);
1005
1006 if ( num_options )
1007 cupsEncodeOptions(request, num_options, options);
1008
1009 /*
1010 * Do the request and get back a response...
1011 */
1012
1013 slprintf(uri, sizeof(uri) - 1, "/printers/%s", printername);
1014
1015 if (!push_utf8_talloc(frame, &filename, pjob->filename, &size)) {
1016 goto out;
1017 }
1018 if ((response = cupsDoFileRequest(http, request, uri, pjob->filename)) != NULL) {
1019 if (ippGetStatusCode(response) >= IPP_OK_CONFLICT) {
1020 DEBUG(0,("Unable to print file to %s - %s\n",
1021 lp_printername(snum),
1022 ippErrorString(cupsLastError())));
1023 } else {
1024 ret = 0;
1025 attr_job_id = ippFindAttribute(response, "job-id", IPP_TAG_INTEGER);
1026 if(attr_job_id) {
1027 pjob->sysjob = ippGetInteger(attr_job_id, 0);
1028 DEBUG(5,("cups_job_submit: job-id %d\n", pjob->sysjob));
1029 } else {
1030 DEBUG(0,("Missing job-id attribute in IPP response"));
1031 }
1032 }
1033 } else {
1034 DEBUG(0,("Unable to print file to `%s' - %s\n",
1035 lp_printername(snum),
1036 ippErrorString(cupsLastError())));
1037 }
1038
1039 if ( ret == 0 )
1040 unlink(pjob->filename);
1041 /* else print_job_end will do it for us */
1042
1043 out:
1044 if (response)
1045 ippDelete(response);
1046
1047 if (language)
1048 cupsLangFree(language);
1049
1050 if (http)
1051 httpClose(http);
1052
1053 TALLOC_FREE(frame);
1054
1055 return ret;
1056}
1057
1058/*
1059 * 'cups_queue_get()' - Get all the jobs in the print queue.
1060 */
1061
1062static int cups_queue_get(const char *sharename,
1063 enum printing_types printing_type,
1064 char *lpq_command,
1065 print_queue_struct **q,
1066 print_status_struct *status)
1067{
1068 TALLOC_CTX *frame = talloc_stackframe();
1069 char *printername = NULL;
1070 http_t *http = NULL; /* HTTP connection to server */
1071 ipp_t *request = NULL, /* IPP Request */
1072 *response = NULL; /* IPP Response */
1073 ipp_attribute_t *attr = NULL; /* Current attribute */
1074 cups_lang_t *language = NULL; /* Default language */
1075 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
1076 int qcount = 0, /* Number of active queue entries */
1077 qalloc = 0; /* Number of queue entries allocated */
1078 print_queue_struct *queue = NULL, /* Queue entries */
1079 *temp; /* Temporary pointer for queue */
1080 char *user_name = NULL, /* job-originating-user-name attribute */
1081 *job_name = NULL; /* job-name attribute */
1082 int job_id; /* job-id attribute */
1083 int job_k_octets; /* job-k-octets attribute */
1084 time_t job_time; /* time-at-creation attribute */
1085 ipp_jstate_t job_status; /* job-status attribute */
1086 int job_priority; /* job-priority attribute */
1087 size_t size;
1088 static const char *jattrs[] = /* Requested job attributes */
1089 {
1090 "job-id",
1091 "job-k-octets",
1092 "job-name",
1093 "job-originating-user-name",
1094 "job-priority",
1095 "job-state",
1096 "time-at-creation",
1097 };
1098 static const char *pattrs[] = /* Requested printer attributes */
1099 {
1100 "printer-state",
1101 "printer-state-message"
1102 };
1103
1104 *q = NULL;
1105
1106 /* HACK ALERT!!! The problem with support the 'printer name'
1107 option is that we key the tdb off the sharename. So we will
1108 overload the lpq_command string to pass in the printername
1109 (which is basically what we do for non-cups printers ... using
1110 the lpq_command to get the queue listing). */
1111
1112 if (!push_utf8_talloc(frame, &printername, lpq_command, &size)) {
1113 goto out;
1114 }
1115 DEBUG(5,("cups_queue_get(%s, %p, %p)\n", lpq_command, q, status));
1116
1117 /*
1118 * Make sure we don't ask for passwords...
1119 */
1120
1121 cupsSetPasswordCB(cups_passwd_cb);
1122
1123 /*
1124 * Try to connect to the server...
1125 */
1126
1127 if ((http = cups_connect(frame)) == NULL) {
1128 goto out;
1129 }
1130
1131 /*
1132 * Generate the printer URI...
1133 */
1134
1135 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/printers/%s", printername);
1136
1137 /*
1138 * Build an IPP_GET_JOBS request, which requires the following
1139 * attributes:
1140 *
1141 * attributes-charset
1142 * attributes-natural-language
1143 * requested-attributes
1144 * printer-uri
1145 */
1146
1147 request = ippNew();
1148
1149 ippSetOperation(request, IPP_GET_JOBS);
1150 ippSetRequestId(request, 1);
1151
1152 language = cupsLangDefault();
1153
1154 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
1155 "attributes-charset", NULL, "utf-8");
1156
1157 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
1158 "attributes-natural-language", NULL, language->language);
1159
1160 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
1161 "requested-attributes",
1162 (sizeof(jattrs) / sizeof(jattrs[0])),
1163 NULL, jattrs);
1164
1165 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
1166 "printer-uri", NULL, uri);
1167
1168 /*
1169 * Do the request and get back a response...
1170 */
1171
1172 if ((response = cupsDoRequest(http, request, "/")) == NULL) {
1173 DEBUG(0,("Unable to get jobs for %s - %s\n", uri,
1174 ippErrorString(cupsLastError())));
1175 goto out;
1176 }
1177
1178 if (ippGetStatusCode(response) >= IPP_OK_CONFLICT) {
1179 DEBUG(0,("Unable to get jobs for %s - %s\n", uri,
1180 ippErrorString(ippGetStatusCode(response))));
1181 goto out;
1182 }
1183
1184 /*
1185 * Process the jobs...
1186 */
1187
1188 qcount = 0;
1189 qalloc = 0;
1190 queue = NULL;
1191
1192 for (attr = ippFirstAttribute(response); attr != NULL; attr = ippNextAttribute(response)) {
1193 /*
1194 * Skip leading attributes until we hit a job...
1195 */
1196
1197 while (attr != NULL && ippGetGroupTag(attr) != IPP_TAG_JOB)
1198 attr = ippNextAttribute(response);
1199
1200 if (attr == NULL)
1201 break;
1202
1203 /*
1204 * Allocate memory as needed...
1205 */
1206 if (qcount >= qalloc) {
1207 qalloc += 16;
1208
1209 queue = SMB_REALLOC_ARRAY(queue, print_queue_struct, qalloc);
1210
1211 if (queue == NULL) {
1212 DEBUG(0,("cups_queue_get: Not enough memory!"));
1213 qcount = 0;
1214 goto out;
1215 }
1216 }
1217
1218 temp = queue + qcount;
1219 memset(temp, 0, sizeof(print_queue_struct));
1220
1221 /*
1222 * Pull the needed attributes from this job...
1223 */
1224
1225 job_id = 0;
1226 job_priority = 50;
1227 job_status = IPP_JOB_PENDING;
1228 job_time = 0;
1229 job_k_octets = 0;
1230 user_name = NULL;
1231 job_name = NULL;
1232
1233 while (attr != NULL && ippGetGroupTag(attr) == IPP_TAG_JOB) {
1234 if (ippGetName(attr) == NULL) {
1235 attr = ippNextAttribute(response);
1236 break;
1237 }
1238
1239 if (strcmp(ippGetName(attr), "job-id") == 0 &&
1240 ippGetValueTag(attr) == IPP_TAG_INTEGER)
1241 job_id = ippGetInteger(attr, 0);
1242
1243 if (strcmp(ippGetName(attr), "job-k-octets") == 0 &&
1244 ippGetValueTag(attr) == IPP_TAG_INTEGER)
1245 job_k_octets = ippGetInteger(attr, 0);
1246
1247 if (strcmp(ippGetName(attr), "job-priority") == 0 &&
1248 ippGetValueTag(attr) == IPP_TAG_INTEGER)
1249 job_priority = ippGetInteger(attr, 0);
1250
1251 if (strcmp(ippGetName(attr), "job-state") == 0 &&
1252 ippGetValueTag(attr) == IPP_TAG_ENUM)
1253 job_status = (ipp_jstate_t)ippGetInteger(attr, 0);
1254
1255 if (strcmp(ippGetName(attr), "time-at-creation") == 0 &&
1256 ippGetValueTag(attr) == IPP_TAG_INTEGER)
1257 job_time = ippGetInteger(attr, 0);
1258
1259 if (strcmp(ippGetName(attr), "job-name") == 0 &&
1260 ippGetValueTag(attr) == IPP_TAG_NAME) {
1261 if (!pull_utf8_talloc(frame,
1262 &job_name,
1263 ippGetString(attr, 0, NULL),
1264 &size)) {
1265 goto out;
1266 }
1267 }
1268
1269 if (strcmp(ippGetName(attr), "job-originating-user-name") == 0 &&
1270 ippGetValueTag(attr) == IPP_TAG_NAME) {
1271 if (!pull_utf8_talloc(frame,
1272 &user_name,
1273 ippGetString(attr, 0, NULL),
1274 &size)) {
1275 goto out;
1276 }
1277 }
1278
1279 attr = ippNextAttribute(response);
1280 }
1281
1282 /*
1283 * See if we have everything needed...
1284 */
1285
1286 if (user_name == NULL || job_name == NULL || job_id == 0) {
1287 if (attr == NULL)
1288 break;
1289 else
1290 continue;
1291 }
1292
1293 temp->sysjob = job_id;
1294 temp->size = job_k_octets * 1024;
1295 temp->status = job_status == IPP_JOB_PENDING ? LPQ_QUEUED :
1296 job_status == IPP_JOB_STOPPED ? LPQ_PAUSED :
1297 job_status == IPP_JOB_HELD ? LPQ_PAUSED :
1298 LPQ_PRINTING;
1299 temp->priority = job_priority;
1300 temp->time = job_time;
1301 strlcpy(temp->fs_user, user_name, sizeof(temp->fs_user));
1302 strlcpy(temp->fs_file, job_name, sizeof(temp->fs_file));
1303
1304 qcount ++;
1305
1306 if (attr == NULL)
1307 break;
1308 }
1309
1310 ippDelete(response);
1311 response = NULL;
1312
1313 /*
1314 * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the
1315 * following attributes:
1316 *
1317 * attributes-charset
1318 * attributes-natural-language
1319 * requested-attributes
1320 * printer-uri
1321 */
1322
1323 request = ippNew();
1324
1325 ippSetOperation(request, IPP_GET_PRINTER_ATTRIBUTES);
1326 ippSetRequestId(request, 1);
1327
1328 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
1329 "attributes-charset", NULL, "utf-8");
1330
1331 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
1332 "attributes-natural-language", NULL, language->language);
1333
1334 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
1335 "requested-attributes",
1336 (sizeof(pattrs) / sizeof(pattrs[0])),
1337 NULL, pattrs);
1338
1339 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
1340 "printer-uri", NULL, uri);
1341
1342 /*
1343 * Do the request and get back a response...
1344 */
1345
1346 if ((response = cupsDoRequest(http, request, "/")) == NULL) {
1347 DEBUG(0,("Unable to get printer status for %s - %s\n", printername,
1348 ippErrorString(cupsLastError())));
1349 goto out;
1350 }
1351
1352 if (ippGetStatusCode(response) >= IPP_OK_CONFLICT) {
1353 DEBUG(0,("Unable to get printer status for %s - %s\n", printername,
1354 ippErrorString(ippGetStatusCode(response))));
1355 goto out;
1356 }
1357
1358 /*
1359 * Get the current printer status and convert it to the SAMBA values.
1360 */
1361
1362 if ((attr = ippFindAttribute(response, "printer-state", IPP_TAG_ENUM)) != NULL) {
1363 if (ippGetInteger(attr, 0) == IPP_PRINTER_STOPPED)
1364 status->status = LPSTAT_STOPPED;
1365 else
1366 status->status = LPSTAT_OK;
1367 }
1368
1369 if ((attr = ippFindAttribute(response, "printer-state-message",
1370 IPP_TAG_TEXT)) != NULL) {
1371 char *msg = NULL;
1372 if (!pull_utf8_talloc(frame, &msg,
1373 ippGetString(attr, 0, NULL),
1374 &size)) {
1375 SAFE_FREE(queue);
1376 qcount = 0;
1377 goto out;
1378 }
1379 fstrcpy(status->message, msg);
1380 }
1381
1382 out:
1383
1384 /*
1385 * Return the job queue...
1386 */
1387
1388 *q = queue;
1389
1390 if (response)
1391 ippDelete(response);
1392
1393 if (language)
1394 cupsLangFree(language);
1395
1396 if (http)
1397 httpClose(http);
1398
1399 TALLOC_FREE(frame);
1400 return qcount;
1401}
1402
1403
1404/*
1405 * 'cups_queue_pause()' - Pause a print queue.
1406 */
1407
1408static int cups_queue_pause(int snum)
1409{
1410 TALLOC_CTX *frame = talloc_stackframe();
1411 int ret = 1; /* Return value */
1412 http_t *http = NULL; /* HTTP connection to server */
1413 ipp_t *request = NULL, /* IPP Request */
1414 *response = NULL; /* IPP Response */
1415 cups_lang_t *language = NULL; /* Default language */
1416 char *printername = NULL;
1417 char *username = NULL;
1418 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
1419 size_t size;
1420
1421 DEBUG(5,("cups_queue_pause(%d)\n", snum));
1422
1423 /*
1424 * Make sure we don't ask for passwords...
1425 */
1426
1427 cupsSetPasswordCB(cups_passwd_cb);
1428
1429 /*
1430 * Try to connect to the server...
1431 */
1432
1433 if ((http = cups_connect(frame)) == NULL) {
1434 goto out;
1435 }
1436
1437 /*
1438 * Build an IPP_PAUSE_PRINTER request, which requires the following
1439 * attributes:
1440 *
1441 * attributes-charset
1442 * attributes-natural-language
1443 * printer-uri
1444 * requesting-user-name
1445 */
1446
1447 request = ippNew();
1448
1449 ippSetOperation(request, IPP_PAUSE_PRINTER);
1450 ippSetRequestId(request, 1);
1451
1452 language = cupsLangDefault();
1453
1454 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
1455 "attributes-charset", NULL, "utf-8");
1456
1457 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
1458 "attributes-natural-language", NULL, language->language);
1459
1460 if (!push_utf8_talloc(frame, &printername, lp_printername(snum),
1461 &size)) {
1462 goto out;
1463 }
1464 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/printers/%s",
1465 printername);
1466
1467 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, uri);
1468
1469 if (!push_utf8_talloc(frame, &username, current_user_info.unix_name, &size)) {
1470 goto out;
1471 }
1472 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
1473 NULL, username);
1474
1475 /*
1476 * Do the request and get back a response...
1477 */
1478
1479 if ((response = cupsDoRequest(http, request, "/admin/")) != NULL) {
1480 if (ippGetStatusCode(response) >= IPP_OK_CONFLICT) {
1481 DEBUG(0,("Unable to pause printer %s - %s\n",
1482 lp_printername(snum),
1483 ippErrorString(cupsLastError())));
1484 } else {
1485 ret = 0;
1486 }
1487 } else {
1488 DEBUG(0,("Unable to pause printer %s - %s\n",
1489 lp_printername(snum),
1490 ippErrorString(cupsLastError())));
1491 }
1492
1493 out:
1494 if (response)
1495 ippDelete(response);
1496
1497 if (language)
1498 cupsLangFree(language);
1499
1500 if (http)
1501 httpClose(http);
1502
1503 TALLOC_FREE(frame);
1504 return ret;
1505}
1506
1507
1508/*
1509 * 'cups_queue_resume()' - Restart a print queue.
1510 */
1511
1512static int cups_queue_resume(int snum)
1513{
1514 TALLOC_CTX *frame = talloc_stackframe();
1515 int ret = 1; /* Return value */
1516 http_t *http = NULL; /* HTTP connection to server */
1517 ipp_t *request = NULL, /* IPP Request */
1518 *response = NULL; /* IPP Response */
1519 cups_lang_t *language = NULL; /* Default language */
1520 char *printername = NULL;
1521 char *username = NULL;
1522 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
1523 size_t size;
1524
1525 DEBUG(5,("cups_queue_resume(%d)\n", snum));
1526
1527 /*
1528 * Make sure we don't ask for passwords...
1529 */
1530
1531 cupsSetPasswordCB(cups_passwd_cb);
1532
1533 /*
1534 * Try to connect to the server...
1535 */
1536
1537 if ((http = cups_connect(frame)) == NULL) {
1538 goto out;
1539 }
1540
1541 /*
1542 * Build an IPP_RESUME_PRINTER request, which requires the following
1543 * attributes:
1544 *
1545 * attributes-charset
1546 * attributes-natural-language
1547 * printer-uri
1548 * requesting-user-name
1549 */
1550
1551 request = ippNew();
1552
1553 ippSetOperation(request, IPP_RESUME_PRINTER);
1554 ippSetRequestId(request, 1);
1555
1556 language = cupsLangDefault();
1557
1558 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
1559 "attributes-charset", NULL, "utf-8");
1560
1561 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
1562 "attributes-natural-language", NULL, language->language);
1563
1564 if (!push_utf8_talloc(frame, &printername, lp_printername(snum),
1565 &size)) {
1566 goto out;
1567 }
1568 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/printers/%s",
1569 printername);
1570
1571 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, uri);
1572
1573 if (!push_utf8_talloc(frame, &username, current_user_info.unix_name, &size)) {
1574 goto out;
1575 }
1576 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
1577 NULL, username);
1578
1579 /*
1580 * Do the request and get back a response...
1581 */
1582
1583 if ((response = cupsDoRequest(http, request, "/admin/")) != NULL) {
1584 if (ippGetStatusCode(response) >= IPP_OK_CONFLICT) {
1585 DEBUG(0,("Unable to resume printer %s - %s\n",
1586 lp_printername(snum),
1587 ippErrorString(cupsLastError())));
1588 } else {
1589 ret = 0;
1590 }
1591 } else {
1592 DEBUG(0,("Unable to resume printer %s - %s\n",
1593 lp_printername(snum),
1594 ippErrorString(cupsLastError())));
1595 }
1596
1597 out:
1598 if (response)
1599 ippDelete(response);
1600
1601 if (language)
1602 cupsLangFree(language);
1603
1604 if (http)
1605 httpClose(http);
1606
1607 TALLOC_FREE(frame);
1608 return ret;
1609}
1610
1611/*******************************************************************
1612 * CUPS printing interface definitions...
1613 ******************************************************************/
1614
1615struct printif cups_printif =
1616{
1617 PRINT_CUPS,
1618 cups_queue_get,
1619 cups_queue_pause,
1620 cups_queue_resume,
1621 cups_job_delete,
1622 cups_job_pause,
1623 cups_job_resume,
1624 cups_job_submit,
1625};
1626
1627#else
1628 /* this keeps fussy compilers happy */
1629 void print_cups_dummy(void);
1630 void print_cups_dummy(void) {}
1631#endif /* HAVE_CUPS */
Note: See TracBrowser for help on using the repository browser.