s3-printing: use printcap IDL for IPC
[Samba.git] / source3 / printing / print_cups.c
blob35e3b30dd8ee8fd747adbdb003e836fd4b12fb6e
1 /*
2 * Support code for the Common UNIX Printing System ("CUPS")
4 * Copyright 1999-2003 by Michael R Sweet.
5 * Copyright 2008 Jeremy Allison.
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.
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.
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/>.
22 * JRA. Converted to utf8 pull/push.
25 #include "includes.h"
26 #include "printing.h"
27 #include "printing/pcap.h"
28 #include "librpc/gen_ndr/ndr_printcap.h"
30 #ifdef HAVE_CUPS
31 #include <cups/cups.h>
32 #include <cups/language.h>
34 static SIG_ATOMIC_T gotalarm;
36 /***************************************************************
37 Signal function to tell us we timed out.
38 ****************************************************************/
40 static void gotalarm_sig(int signum)
42 gotalarm = 1;
45 extern userdom_struct current_user_info;
48 * 'cups_passwd_cb()' - The CUPS password callback...
51 static const char * /* O - Password or NULL */
52 cups_passwd_cb(const char *prompt) /* I - Prompt */
55 * Always return NULL to indicate that no password is available...
58 return (NULL);
61 static http_t *cups_connect(TALLOC_CTX *frame)
63 http_t *http = NULL;
64 char *server = NULL, *p = NULL;
65 int port;
66 int timeout = lp_cups_connection_timeout();
67 size_t size;
69 if (lp_cups_server() != NULL && strlen(lp_cups_server()) > 0) {
70 if (!push_utf8_talloc(frame, &server, lp_cups_server(), &size)) {
71 return NULL;
73 } else {
74 server = talloc_strdup(frame,cupsServer());
76 if (!server) {
77 return NULL;
80 p = strchr(server, ':');
81 if (p) {
82 port = atoi(p+1);
83 *p = '\0';
84 } else {
85 port = ippPort();
88 DEBUG(10, ("connecting to cups server %s:%d\n",
89 server, port));
91 gotalarm = 0;
93 if (timeout) {
94 CatchSignal(SIGALRM, gotalarm_sig);
95 alarm(timeout);
98 #ifdef HAVE_HTTPCONNECTENCRYPT
99 http = httpConnectEncrypt(server, port, lp_cups_encrypt());
100 #else
101 http = httpConnect(server, port);
102 #endif
105 CatchSignal(SIGALRM, SIG_IGN);
106 alarm(0);
108 if (http == NULL) {
109 DEBUG(0,("Unable to connect to CUPS server %s:%d - %s\n",
110 server, port, strerror(errno)));
113 return http;
116 static bool send_pcap_blob(DATA_BLOB *pcap_blob, int fd)
118 size_t ret;
120 ret = sys_write(fd, &pcap_blob->length, sizeof(pcap_blob->length));
121 if (ret != sizeof(pcap_blob->length)) {
122 return false;
125 ret = sys_write(fd, pcap_blob->data, pcap_blob->length);
126 if (ret != pcap_blob->length) {
127 return false;
130 DEBUG(10, ("successfully sent blob of len %ld\n", (int64_t)ret));
131 return true;
134 static bool recv_pcap_blob(TALLOC_CTX *mem_ctx, int fd, DATA_BLOB *pcap_blob)
136 size_t blob_len;
137 size_t ret;
139 ret = sys_read(fd, &blob_len, sizeof(blob_len));
140 if (ret != sizeof(blob_len)) {
141 return false;
144 *pcap_blob = data_blob_talloc_named(mem_ctx, NULL, blob_len,
145 "cups pcap");
146 if (pcap_blob->length != blob_len) {
147 return false;
149 ret = sys_read(fd, pcap_blob->data, blob_len);
150 if (ret != blob_len) {
151 talloc_free(pcap_blob->data);
152 return false;
155 DEBUG(10, ("successfully recvd blob of len %ld\n", (int64_t)ret));
156 return true;
159 static bool cups_cache_reload_async(int fd)
161 TALLOC_CTX *frame = talloc_stackframe();
162 struct pcap_data pcap_data;
163 struct pcap_printer *printer;
164 http_t *http = NULL; /* HTTP connection to server */
165 ipp_t *request = NULL, /* IPP Request */
166 *response = NULL; /* IPP Response */
167 ipp_attribute_t *attr; /* Current attribute */
168 cups_lang_t *language = NULL; /* Default language */
169 char *name, /* printer-name attribute */
170 *info; /* printer-info attribute */
171 static const char *requested[] =/* Requested attributes */
173 "printer-name",
174 "printer-info"
176 bool ret = False;
177 size_t size;
178 enum ndr_err_code ndr_ret;
179 DATA_BLOB pcap_blob;
181 ZERO_STRUCT(pcap_data);
182 pcap_data.status = NT_STATUS_UNSUCCESSFUL;
184 DEBUG(5, ("reloading cups printcap cache\n"));
187 * Make sure we don't ask for passwords...
190 cupsSetPasswordCB(cups_passwd_cb);
193 * Try to connect to the server...
196 if ((http = cups_connect(frame)) == NULL) {
197 goto out;
201 * Build a CUPS_GET_PRINTERS request, which requires the following
202 * attributes:
204 * attributes-charset
205 * attributes-natural-language
206 * requested-attributes
209 request = ippNew();
211 request->request.op.operation_id = CUPS_GET_PRINTERS;
212 request->request.op.request_id = 1;
214 language = cupsLangDefault();
216 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
217 "attributes-charset", NULL, "utf-8");
219 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
220 "attributes-natural-language", NULL, language->language);
222 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
223 "requested-attributes",
224 (sizeof(requested) / sizeof(requested[0])),
225 NULL, requested);
228 * Do the request and get back a response...
231 if ((response = cupsDoRequest(http, request, "/")) == NULL) {
232 DEBUG(0,("Unable to get printer list - %s\n",
233 ippErrorString(cupsLastError())));
234 goto out;
237 for (attr = response->attrs; attr != NULL;) {
239 * Skip leading attributes until we hit a printer...
242 while (attr != NULL && attr->group_tag != IPP_TAG_PRINTER)
243 attr = attr->next;
245 if (attr == NULL)
246 break;
249 * Pull the needed attributes from this printer...
252 name = NULL;
253 info = NULL;
255 while (attr != NULL && attr->group_tag == IPP_TAG_PRINTER) {
256 if (strcmp(attr->name, "printer-name") == 0 &&
257 attr->value_tag == IPP_TAG_NAME) {
258 if (!pull_utf8_talloc(frame,
259 &name,
260 attr->values[0].string.text,
261 &size)) {
262 goto out;
266 if (strcmp(attr->name, "printer-info") == 0 &&
267 attr->value_tag == IPP_TAG_TEXT) {
268 if (!pull_utf8_talloc(frame,
269 &info,
270 attr->values[0].string.text,
271 &size)) {
272 goto out;
276 attr = attr->next;
280 * See if we have everything needed...
283 if (name == NULL)
284 break;
286 if (pcap_data.count == 0) {
287 printer = talloc_array(frame, struct pcap_printer, 1);
288 } else {
289 printer = talloc_realloc(frame, pcap_data.printers,
290 struct pcap_printer,
291 pcap_data.count + 1);
293 if (printer == NULL) {
294 goto out;
296 pcap_data.printers = printer;
297 pcap_data.printers[pcap_data.count].name = name;
298 pcap_data.printers[pcap_data.count].info = info;
299 pcap_data.count++;
302 ippDelete(response);
303 response = NULL;
306 * Build a CUPS_GET_CLASSES request, which requires the following
307 * attributes:
309 * attributes-charset
310 * attributes-natural-language
311 * requested-attributes
314 request = ippNew();
316 request->request.op.operation_id = CUPS_GET_CLASSES;
317 request->request.op.request_id = 1;
319 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
320 "attributes-charset", NULL, "utf-8");
322 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
323 "attributes-natural-language", NULL, language->language);
325 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
326 "requested-attributes",
327 (sizeof(requested) / sizeof(requested[0])),
328 NULL, requested);
331 * Do the request and get back a response...
334 if ((response = cupsDoRequest(http, request, "/")) == NULL) {
335 DEBUG(0,("Unable to get printer list - %s\n",
336 ippErrorString(cupsLastError())));
337 goto out;
340 for (attr = response->attrs; attr != NULL;) {
342 * Skip leading attributes until we hit a printer...
345 while (attr != NULL && attr->group_tag != IPP_TAG_PRINTER)
346 attr = attr->next;
348 if (attr == NULL)
349 break;
352 * Pull the needed attributes from this printer...
355 name = NULL;
356 info = NULL;
358 while (attr != NULL && attr->group_tag == IPP_TAG_PRINTER) {
359 if (strcmp(attr->name, "printer-name") == 0 &&
360 attr->value_tag == IPP_TAG_NAME) {
361 if (!pull_utf8_talloc(frame,
362 &name,
363 attr->values[0].string.text,
364 &size)) {
365 goto out;
369 if (strcmp(attr->name, "printer-info") == 0 &&
370 attr->value_tag == IPP_TAG_TEXT) {
371 if (!pull_utf8_talloc(frame,
372 &info,
373 attr->values[0].string.text,
374 &size)) {
375 goto out;
379 attr = attr->next;
383 * See if we have everything needed...
386 if (name == NULL)
387 break;
389 if (pcap_data.count == 0) {
390 printer = talloc_array(frame, struct pcap_printer, 1);
391 } else {
392 printer = talloc_realloc(frame, pcap_data.printers,
393 struct pcap_printer,
394 pcap_data.count + 1);
396 if (printer == NULL) {
397 goto out;
399 pcap_data.printers = printer;
400 pcap_data.printers[pcap_data.count].name = name;
401 pcap_data.printers[pcap_data.count].info = info;
402 pcap_data.count++;
405 ret = true;
406 pcap_data.status = NT_STATUS_OK;
407 out:
408 if (response)
409 ippDelete(response);
411 if (language)
412 cupsLangFree(language);
414 if (http)
415 httpClose(http);
417 /* Send all the entries up the pipe. */
418 ndr_ret = ndr_push_struct_blob(&pcap_blob, frame, &pcap_data,
419 (ndr_push_flags_fn_t)ndr_push_pcap_data);
420 if (ndr_ret == NDR_ERR_SUCCESS) {
421 ret = send_pcap_blob(&pcap_blob, fd);
422 } else {
423 ret = false;
426 TALLOC_FREE(frame);
427 return ret;
430 static struct fd_event *cache_fd_event;
432 static bool cups_pcap_load_async(struct tevent_context *ev,
433 struct messaging_context *msg_ctx,
434 int *pfd)
436 int fds[2];
437 pid_t pid;
438 NTSTATUS status;
440 *pfd = -1;
442 if (cache_fd_event) {
443 DEBUG(3,("cups_pcap_load_async: already waiting for "
444 "a refresh event\n" ));
445 return false;
448 DEBUG(5,("cups_pcap_load_async: asynchronously loading cups printers\n"));
450 if (pipe(fds) == -1) {
451 return false;
454 pid = sys_fork();
455 if (pid == (pid_t)-1) {
456 DEBUG(10,("cups_pcap_load_async: fork failed %s\n",
457 strerror(errno) ));
458 close(fds[0]);
459 close(fds[1]);
460 return false;
463 if (pid) {
464 DEBUG(10,("cups_pcap_load_async: child pid = %u\n",
465 (unsigned int)pid ));
466 /* Parent. */
467 close(fds[1]);
468 *pfd = fds[0];
469 return true;
472 /* Child. */
474 close_all_print_db();
476 status = reinit_after_fork(msg_ctx, ev, procid_self(), true);
477 if (!NT_STATUS_IS_OK(status)) {
478 DEBUG(0,("cups_pcap_load_async: reinit_after_fork() failed\n"));
479 smb_panic("cups_pcap_load_async: reinit_after_fork() failed");
482 close(fds[0]);
483 cups_cache_reload_async(fds[1]);
484 close(fds[1]);
485 _exit(0);
488 struct cups_async_cb_args {
489 int pipe_fd;
490 struct event_context *event_ctx;
491 struct messaging_context *msg_ctx;
492 void (*post_cache_fill_fn)(struct event_context *,
493 struct messaging_context *);
496 static void cups_async_callback(struct event_context *event_ctx,
497 struct fd_event *event,
498 uint16 flags,
499 void *p)
501 TALLOC_CTX *frame = talloc_stackframe();
502 struct cups_async_cb_args *cb_args = (struct cups_async_cb_args *)p;
503 struct pcap_cache *tmp_pcap_cache = NULL;
504 bool ret_ok;
505 struct pcap_data pcap_data;
506 DATA_BLOB pcap_blob;
507 enum ndr_err_code ndr_ret;
508 int i;
510 DEBUG(5,("cups_async_callback: callback received for printer data. "
511 "fd = %d\n", cb_args->pipe_fd));
513 ret_ok = recv_pcap_blob(frame, cb_args->pipe_fd, &pcap_blob);
514 if (!ret_ok) {
515 DEBUG(0,("failed to recv pcap blob\n"));
516 goto err_out;
519 ndr_ret = ndr_pull_struct_blob(&pcap_blob, frame, &pcap_data,
520 (ndr_pull_flags_fn_t)ndr_pull_pcap_data);
521 if (ndr_ret != NDR_ERR_SUCCESS) {
522 goto err_out;
525 if (!NT_STATUS_IS_OK(pcap_data.status)) {
526 DEBUG(0,("failed to retrieve printer list: %s\n",
527 nt_errstr(pcap_data.status)));
528 goto err_out;
531 for (i = 0; i < pcap_data.count; i++) {
532 ret_ok = pcap_cache_add_specific(&tmp_pcap_cache,
533 pcap_data.printers[i].name,
534 pcap_data.printers[i].info);
535 if (!ret_ok) {
536 DEBUG(0, ("failed to add to tmp pcap cache\n"));
537 break;
541 if (!ret_ok) {
542 DEBUG(0, ("failed to read a new printer list\n"));
543 pcap_cache_destroy_specific(&tmp_pcap_cache);
544 } else {
546 * replace the system-wide pcap cache with a (possibly empty)
547 * new one.
549 ret_ok = pcap_cache_replace(tmp_pcap_cache);
550 if (!ret_ok) {
551 DEBUG(0, ("failed to replace pcap cache\n"));
552 } else if (cb_args->post_cache_fill_fn != NULL) {
553 /* Caller requested post cache fill callback */
554 cb_args->post_cache_fill_fn(cb_args->event_ctx,
555 cb_args->msg_ctx);
558 err_out:
559 TALLOC_FREE(frame);
560 close(cb_args->pipe_fd);
561 TALLOC_FREE(cb_args);
562 TALLOC_FREE(cache_fd_event);
565 bool cups_cache_reload(struct tevent_context *ev,
566 struct messaging_context *msg_ctx,
567 void (*post_cache_fill_fn)(struct tevent_context *,
568 struct messaging_context *))
570 struct cups_async_cb_args *cb_args;
571 int *p_pipe_fd;
573 cb_args = TALLOC_P(NULL, struct cups_async_cb_args);
574 if (cb_args == NULL) {
575 return false;
578 cb_args->post_cache_fill_fn = post_cache_fill_fn;
579 cb_args->event_ctx = ev;
580 cb_args->msg_ctx = msg_ctx;
581 p_pipe_fd = &cb_args->pipe_fd;
582 *p_pipe_fd = -1;
584 /* Set up an async refresh. */
585 if (!cups_pcap_load_async(ev, msg_ctx, p_pipe_fd)) {
586 talloc_free(cb_args);
587 return false;
590 DEBUG(10,("cups_cache_reload: async read on fd %d\n",
591 *p_pipe_fd ));
593 /* Trigger an event when the pipe can be read. */
594 cache_fd_event = event_add_fd(ev,
595 NULL, *p_pipe_fd,
596 EVENT_FD_READ,
597 cups_async_callback,
598 (void *)cb_args);
599 if (!cache_fd_event) {
600 close(*p_pipe_fd);
601 TALLOC_FREE(cb_args);
602 return false;
605 return true;
609 * 'cups_job_delete()' - Delete a job.
612 static int cups_job_delete(const char *sharename, const char *lprm_command, struct printjob *pjob)
614 TALLOC_CTX *frame = talloc_stackframe();
615 int ret = 1; /* Return value */
616 http_t *http = NULL; /* HTTP connection to server */
617 ipp_t *request = NULL, /* IPP Request */
618 *response = NULL; /* IPP Response */
619 cups_lang_t *language = NULL; /* Default language */
620 char *user = NULL;
621 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
622 size_t size;
624 DEBUG(5,("cups_job_delete(%s, %p (%d))\n", sharename, pjob, pjob->sysjob));
627 * Make sure we don't ask for passwords...
630 cupsSetPasswordCB(cups_passwd_cb);
633 * Try to connect to the server...
636 if ((http = cups_connect(frame)) == NULL) {
637 goto out;
641 * Build an IPP_CANCEL_JOB request, which requires the following
642 * attributes:
644 * attributes-charset
645 * attributes-natural-language
646 * job-uri
647 * requesting-user-name
650 request = ippNew();
652 request->request.op.operation_id = IPP_CANCEL_JOB;
653 request->request.op.request_id = 1;
655 language = cupsLangDefault();
657 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
658 "attributes-charset", NULL, "utf-8");
660 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
661 "attributes-natural-language", NULL, language->language);
663 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/jobs/%d", pjob->sysjob);
665 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri", NULL, uri);
667 if (!push_utf8_talloc(frame, &user, pjob->user, &size)) {
668 goto out;
671 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
672 NULL, user);
675 * Do the request and get back a response...
678 if ((response = cupsDoRequest(http, request, "/jobs")) != NULL) {
679 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
680 DEBUG(0,("Unable to cancel job %d - %s\n", pjob->sysjob,
681 ippErrorString(cupsLastError())));
682 } else {
683 ret = 0;
685 } else {
686 DEBUG(0,("Unable to cancel job %d - %s\n", pjob->sysjob,
687 ippErrorString(cupsLastError())));
690 out:
691 if (response)
692 ippDelete(response);
694 if (language)
695 cupsLangFree(language);
697 if (http)
698 httpClose(http);
700 TALLOC_FREE(frame);
701 return ret;
706 * 'cups_job_pause()' - Pause a job.
709 static int cups_job_pause(int snum, struct printjob *pjob)
711 TALLOC_CTX *frame = talloc_stackframe();
712 int ret = 1; /* Return value */
713 http_t *http = NULL; /* HTTP connection to server */
714 ipp_t *request = NULL, /* IPP Request */
715 *response = NULL; /* IPP Response */
716 cups_lang_t *language = NULL; /* Default language */
717 char *user = NULL;
718 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
719 size_t size;
721 DEBUG(5,("cups_job_pause(%d, %p (%d))\n", snum, pjob, pjob->sysjob));
724 * Make sure we don't ask for passwords...
727 cupsSetPasswordCB(cups_passwd_cb);
730 * Try to connect to the server...
733 if ((http = cups_connect(frame)) == NULL) {
734 goto out;
738 * Build an IPP_HOLD_JOB request, which requires the following
739 * attributes:
741 * attributes-charset
742 * attributes-natural-language
743 * job-uri
744 * requesting-user-name
747 request = ippNew();
749 request->request.op.operation_id = IPP_HOLD_JOB;
750 request->request.op.request_id = 1;
752 language = cupsLangDefault();
754 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
755 "attributes-charset", NULL, "utf-8");
757 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
758 "attributes-natural-language", NULL, language->language);
760 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/jobs/%d", pjob->sysjob);
762 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri", NULL, uri);
764 if (!push_utf8_talloc(frame, &user, pjob->user, &size)) {
765 goto out;
767 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
768 NULL, user);
771 * Do the request and get back a response...
774 if ((response = cupsDoRequest(http, request, "/jobs")) != NULL) {
775 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
776 DEBUG(0,("Unable to hold job %d - %s\n", pjob->sysjob,
777 ippErrorString(cupsLastError())));
778 } else {
779 ret = 0;
781 } else {
782 DEBUG(0,("Unable to hold job %d - %s\n", pjob->sysjob,
783 ippErrorString(cupsLastError())));
786 out:
787 if (response)
788 ippDelete(response);
790 if (language)
791 cupsLangFree(language);
793 if (http)
794 httpClose(http);
796 TALLOC_FREE(frame);
797 return ret;
802 * 'cups_job_resume()' - Resume a paused job.
805 static int cups_job_resume(int snum, struct printjob *pjob)
807 TALLOC_CTX *frame = talloc_stackframe();
808 int ret = 1; /* Return value */
809 http_t *http = NULL; /* HTTP connection to server */
810 ipp_t *request = NULL, /* IPP Request */
811 *response = NULL; /* IPP Response */
812 cups_lang_t *language = NULL; /* Default language */
813 char *user = NULL;
814 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
815 size_t size;
817 DEBUG(5,("cups_job_resume(%d, %p (%d))\n", snum, pjob, pjob->sysjob));
820 * Make sure we don't ask for passwords...
823 cupsSetPasswordCB(cups_passwd_cb);
826 * Try to connect to the server...
829 if ((http = cups_connect(frame)) == NULL) {
830 goto out;
834 * Build an IPP_RELEASE_JOB request, which requires the following
835 * attributes:
837 * attributes-charset
838 * attributes-natural-language
839 * job-uri
840 * requesting-user-name
843 request = ippNew();
845 request->request.op.operation_id = IPP_RELEASE_JOB;
846 request->request.op.request_id = 1;
848 language = cupsLangDefault();
850 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
851 "attributes-charset", NULL, "utf-8");
853 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
854 "attributes-natural-language", NULL, language->language);
856 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/jobs/%d", pjob->sysjob);
858 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri", NULL, uri);
860 if (!push_utf8_talloc(frame, &user, pjob->user, &size)) {
861 goto out;
863 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
864 NULL, user);
867 * Do the request and get back a response...
870 if ((response = cupsDoRequest(http, request, "/jobs")) != NULL) {
871 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
872 DEBUG(0,("Unable to release job %d - %s\n", pjob->sysjob,
873 ippErrorString(cupsLastError())));
874 } else {
875 ret = 0;
877 } else {
878 DEBUG(0,("Unable to release job %d - %s\n", pjob->sysjob,
879 ippErrorString(cupsLastError())));
882 out:
883 if (response)
884 ippDelete(response);
886 if (language)
887 cupsLangFree(language);
889 if (http)
890 httpClose(http);
892 TALLOC_FREE(frame);
893 return ret;
898 * 'cups_job_submit()' - Submit a job for printing.
901 static int cups_job_submit(int snum, struct printjob *pjob)
903 TALLOC_CTX *frame = talloc_stackframe();
904 int ret = 1; /* Return value */
905 http_t *http = NULL; /* HTTP connection to server */
906 ipp_t *request = NULL, /* IPP Request */
907 *response = NULL; /* IPP Response */
908 ipp_attribute_t *attr_job_id = NULL; /* IPP Attribute "job-id" */
909 cups_lang_t *language = NULL; /* Default language */
910 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
911 char *new_jobname = NULL;
912 int num_options = 0;
913 cups_option_t *options = NULL;
914 char *printername = NULL;
915 char *user = NULL;
916 char *jobname = NULL;
917 char *cupsoptions = NULL;
918 char *filename = NULL;
919 size_t size;
920 uint32_t jobid = (uint32_t)-1;
922 DEBUG(5,("cups_job_submit(%d, %p)\n", snum, pjob));
925 * Make sure we don't ask for passwords...
928 cupsSetPasswordCB(cups_passwd_cb);
931 * Try to connect to the server...
934 if ((http = cups_connect(frame)) == NULL) {
935 goto out;
939 * Build an IPP_PRINT_JOB request, which requires the following
940 * attributes:
942 * attributes-charset
943 * attributes-natural-language
944 * printer-uri
945 * requesting-user-name
946 * [document-data]
949 request = ippNew();
951 request->request.op.operation_id = IPP_PRINT_JOB;
952 request->request.op.request_id = 1;
954 language = cupsLangDefault();
956 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
957 "attributes-charset", NULL, "utf-8");
959 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
960 "attributes-natural-language", NULL, language->language);
962 if (!push_utf8_talloc(frame, &printername, lp_printername(snum),
963 &size)) {
964 goto out;
966 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/printers/%s",
967 printername);
969 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
970 "printer-uri", NULL, uri);
972 if (!push_utf8_talloc(frame, &user, pjob->user, &size)) {
973 goto out;
975 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
976 NULL, user);
978 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
979 "job-originating-host-name", NULL,
980 pjob->clientmachine);
982 /* Get the jobid from the filename. */
983 jobid = print_parse_jobid(pjob->filename);
984 if (jobid == (uint32_t)-1) {
985 DEBUG(0,("cups_job_submit: failed to parse jobid from name %s\n",
986 pjob->filename ));
987 jobid = 0;
990 if (!push_utf8_talloc(frame, &jobname, pjob->jobname, &size)) {
991 goto out;
993 new_jobname = talloc_asprintf(frame,
994 "%s%.8u %s", PRINT_SPOOL_PREFIX,
995 (unsigned int)jobid,
996 jobname);
997 if (new_jobname == NULL) {
998 goto out;
1001 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "job-name", NULL,
1002 new_jobname);
1005 * add any options defined in smb.conf
1008 if (!push_utf8_talloc(frame, &cupsoptions, lp_cups_options(snum), &size)) {
1009 goto out;
1011 num_options = 0;
1012 options = NULL;
1013 num_options = cupsParseOptions(cupsoptions, num_options, &options);
1015 if ( num_options )
1016 cupsEncodeOptions(request, num_options, options);
1019 * Do the request and get back a response...
1022 slprintf(uri, sizeof(uri) - 1, "/printers/%s", printername);
1024 if (!push_utf8_talloc(frame, &filename, pjob->filename, &size)) {
1025 goto out;
1027 if ((response = cupsDoFileRequest(http, request, uri, pjob->filename)) != NULL) {
1028 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
1029 DEBUG(0,("Unable to print file to %s - %s\n",
1030 lp_printername(snum),
1031 ippErrorString(cupsLastError())));
1032 } else {
1033 ret = 0;
1034 attr_job_id = ippFindAttribute(response, "job-id", IPP_TAG_INTEGER);
1035 if(attr_job_id) {
1036 pjob->sysjob = attr_job_id->values[0].integer;
1037 DEBUG(5,("cups_job_submit: job-id %d\n", pjob->sysjob));
1038 } else {
1039 DEBUG(0,("Missing job-id attribute in IPP response"));
1042 } else {
1043 DEBUG(0,("Unable to print file to `%s' - %s\n",
1044 lp_printername(snum),
1045 ippErrorString(cupsLastError())));
1048 if ( ret == 0 )
1049 unlink(pjob->filename);
1050 /* else print_job_end will do it for us */
1052 out:
1053 if (response)
1054 ippDelete(response);
1056 if (language)
1057 cupsLangFree(language);
1059 if (http)
1060 httpClose(http);
1062 TALLOC_FREE(frame);
1064 return ret;
1068 * 'cups_queue_get()' - Get all the jobs in the print queue.
1071 static int cups_queue_get(const char *sharename,
1072 enum printing_types printing_type,
1073 char *lpq_command,
1074 print_queue_struct **q,
1075 print_status_struct *status)
1077 TALLOC_CTX *frame = talloc_stackframe();
1078 char *printername = NULL;
1079 http_t *http = NULL; /* HTTP connection to server */
1080 ipp_t *request = NULL, /* IPP Request */
1081 *response = NULL; /* IPP Response */
1082 ipp_attribute_t *attr = NULL; /* Current attribute */
1083 cups_lang_t *language = NULL; /* Default language */
1084 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
1085 int qcount = 0, /* Number of active queue entries */
1086 qalloc = 0; /* Number of queue entries allocated */
1087 print_queue_struct *queue = NULL, /* Queue entries */
1088 *temp; /* Temporary pointer for queue */
1089 char *user_name = NULL, /* job-originating-user-name attribute */
1090 *job_name = NULL; /* job-name attribute */
1091 int job_id; /* job-id attribute */
1092 int job_k_octets; /* job-k-octets attribute */
1093 time_t job_time; /* time-at-creation attribute */
1094 ipp_jstate_t job_status; /* job-status attribute */
1095 int job_priority; /* job-priority attribute */
1096 size_t size;
1097 static const char *jattrs[] = /* Requested job attributes */
1099 "job-id",
1100 "job-k-octets",
1101 "job-name",
1102 "job-originating-user-name",
1103 "job-priority",
1104 "job-state",
1105 "time-at-creation",
1107 static const char *pattrs[] = /* Requested printer attributes */
1109 "printer-state",
1110 "printer-state-message"
1113 *q = NULL;
1115 /* HACK ALERT!!! The problem with support the 'printer name'
1116 option is that we key the tdb off the sharename. So we will
1117 overload the lpq_command string to pass in the printername
1118 (which is basically what we do for non-cups printers ... using
1119 the lpq_command to get the queue listing). */
1121 if (!push_utf8_talloc(frame, &printername, lpq_command, &size)) {
1122 goto out;
1124 DEBUG(5,("cups_queue_get(%s, %p, %p)\n", lpq_command, q, status));
1127 * Make sure we don't ask for passwords...
1130 cupsSetPasswordCB(cups_passwd_cb);
1133 * Try to connect to the server...
1136 if ((http = cups_connect(frame)) == NULL) {
1137 goto out;
1141 * Generate the printer URI...
1144 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/printers/%s", printername);
1147 * Build an IPP_GET_JOBS request, which requires the following
1148 * attributes:
1150 * attributes-charset
1151 * attributes-natural-language
1152 * requested-attributes
1153 * printer-uri
1156 request = ippNew();
1158 request->request.op.operation_id = IPP_GET_JOBS;
1159 request->request.op.request_id = 1;
1161 language = cupsLangDefault();
1163 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
1164 "attributes-charset", NULL, "utf-8");
1166 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
1167 "attributes-natural-language", NULL, language->language);
1169 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
1170 "requested-attributes",
1171 (sizeof(jattrs) / sizeof(jattrs[0])),
1172 NULL, jattrs);
1174 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
1175 "printer-uri", NULL, uri);
1178 * Do the request and get back a response...
1181 if ((response = cupsDoRequest(http, request, "/")) == NULL) {
1182 DEBUG(0,("Unable to get jobs for %s - %s\n", uri,
1183 ippErrorString(cupsLastError())));
1184 goto out;
1187 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
1188 DEBUG(0,("Unable to get jobs for %s - %s\n", uri,
1189 ippErrorString(response->request.status.status_code)));
1190 goto out;
1194 * Process the jobs...
1197 qcount = 0;
1198 qalloc = 0;
1199 queue = NULL;
1201 for (attr = response->attrs; attr != NULL; attr = attr->next) {
1203 * Skip leading attributes until we hit a job...
1206 while (attr != NULL && attr->group_tag != IPP_TAG_JOB)
1207 attr = attr->next;
1209 if (attr == NULL)
1210 break;
1213 * Allocate memory as needed...
1215 if (qcount >= qalloc) {
1216 qalloc += 16;
1218 queue = SMB_REALLOC_ARRAY(queue, print_queue_struct, qalloc);
1220 if (queue == NULL) {
1221 DEBUG(0,("cups_queue_get: Not enough memory!"));
1222 qcount = 0;
1223 goto out;
1227 temp = queue + qcount;
1228 memset(temp, 0, sizeof(print_queue_struct));
1231 * Pull the needed attributes from this job...
1234 job_id = 0;
1235 job_priority = 50;
1236 job_status = IPP_JOB_PENDING;
1237 job_time = 0;
1238 job_k_octets = 0;
1239 user_name = NULL;
1240 job_name = NULL;
1242 while (attr != NULL && attr->group_tag == IPP_TAG_JOB) {
1243 if (attr->name == NULL) {
1244 attr = attr->next;
1245 break;
1248 if (strcmp(attr->name, "job-id") == 0 &&
1249 attr->value_tag == IPP_TAG_INTEGER)
1250 job_id = attr->values[0].integer;
1252 if (strcmp(attr->name, "job-k-octets") == 0 &&
1253 attr->value_tag == IPP_TAG_INTEGER)
1254 job_k_octets = attr->values[0].integer;
1256 if (strcmp(attr->name, "job-priority") == 0 &&
1257 attr->value_tag == IPP_TAG_INTEGER)
1258 job_priority = attr->values[0].integer;
1260 if (strcmp(attr->name, "job-state") == 0 &&
1261 attr->value_tag == IPP_TAG_ENUM)
1262 job_status = (ipp_jstate_t)(attr->values[0].integer);
1264 if (strcmp(attr->name, "time-at-creation") == 0 &&
1265 attr->value_tag == IPP_TAG_INTEGER)
1266 job_time = attr->values[0].integer;
1268 if (strcmp(attr->name, "job-name") == 0 &&
1269 attr->value_tag == IPP_TAG_NAME) {
1270 if (!pull_utf8_talloc(frame,
1271 &job_name,
1272 attr->values[0].string.text,
1273 &size)) {
1274 goto out;
1278 if (strcmp(attr->name, "job-originating-user-name") == 0 &&
1279 attr->value_tag == IPP_TAG_NAME) {
1280 if (!pull_utf8_talloc(frame,
1281 &user_name,
1282 attr->values[0].string.text,
1283 &size)) {
1284 goto out;
1288 attr = attr->next;
1292 * See if we have everything needed...
1295 if (user_name == NULL || job_name == NULL || job_id == 0) {
1296 if (attr == NULL)
1297 break;
1298 else
1299 continue;
1302 temp->job = job_id;
1303 temp->size = job_k_octets * 1024;
1304 temp->status = job_status == IPP_JOB_PENDING ? LPQ_QUEUED :
1305 job_status == IPP_JOB_STOPPED ? LPQ_PAUSED :
1306 job_status == IPP_JOB_HELD ? LPQ_PAUSED :
1307 LPQ_PRINTING;
1308 temp->priority = job_priority;
1309 temp->time = job_time;
1310 strlcpy(temp->fs_user, user_name, sizeof(temp->fs_user));
1311 strlcpy(temp->fs_file, job_name, sizeof(temp->fs_file));
1313 qcount ++;
1315 if (attr == NULL)
1316 break;
1319 ippDelete(response);
1320 response = NULL;
1323 * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the
1324 * following attributes:
1326 * attributes-charset
1327 * attributes-natural-language
1328 * requested-attributes
1329 * printer-uri
1332 request = ippNew();
1334 request->request.op.operation_id = IPP_GET_PRINTER_ATTRIBUTES;
1335 request->request.op.request_id = 1;
1337 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
1338 "attributes-charset", NULL, "utf-8");
1340 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
1341 "attributes-natural-language", NULL, language->language);
1343 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
1344 "requested-attributes",
1345 (sizeof(pattrs) / sizeof(pattrs[0])),
1346 NULL, pattrs);
1348 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
1349 "printer-uri", NULL, uri);
1352 * Do the request and get back a response...
1355 if ((response = cupsDoRequest(http, request, "/")) == NULL) {
1356 DEBUG(0,("Unable to get printer status for %s - %s\n", printername,
1357 ippErrorString(cupsLastError())));
1358 goto out;
1361 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
1362 DEBUG(0,("Unable to get printer status for %s - %s\n", printername,
1363 ippErrorString(response->request.status.status_code)));
1364 goto out;
1368 * Get the current printer status and convert it to the SAMBA values.
1371 if ((attr = ippFindAttribute(response, "printer-state", IPP_TAG_ENUM)) != NULL) {
1372 if (attr->values[0].integer == IPP_PRINTER_STOPPED)
1373 status->status = LPSTAT_STOPPED;
1374 else
1375 status->status = LPSTAT_OK;
1378 if ((attr = ippFindAttribute(response, "printer-state-message",
1379 IPP_TAG_TEXT)) != NULL) {
1380 char *msg = NULL;
1381 if (!pull_utf8_talloc(frame, &msg,
1382 attr->values[0].string.text,
1383 &size)) {
1384 SAFE_FREE(queue);
1385 qcount = 0;
1386 goto out;
1388 fstrcpy(status->message, msg);
1391 out:
1394 * Return the job queue...
1397 *q = queue;
1399 if (response)
1400 ippDelete(response);
1402 if (language)
1403 cupsLangFree(language);
1405 if (http)
1406 httpClose(http);
1408 TALLOC_FREE(frame);
1409 return qcount;
1414 * 'cups_queue_pause()' - Pause a print queue.
1417 static int cups_queue_pause(int snum)
1419 TALLOC_CTX *frame = talloc_stackframe();
1420 int ret = 1; /* Return value */
1421 http_t *http = NULL; /* HTTP connection to server */
1422 ipp_t *request = NULL, /* IPP Request */
1423 *response = NULL; /* IPP Response */
1424 cups_lang_t *language = NULL; /* Default language */
1425 char *printername = NULL;
1426 char *username = NULL;
1427 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
1428 size_t size;
1430 DEBUG(5,("cups_queue_pause(%d)\n", snum));
1433 * Make sure we don't ask for passwords...
1436 cupsSetPasswordCB(cups_passwd_cb);
1439 * Try to connect to the server...
1442 if ((http = cups_connect(frame)) == NULL) {
1443 goto out;
1447 * Build an IPP_PAUSE_PRINTER request, which requires the following
1448 * attributes:
1450 * attributes-charset
1451 * attributes-natural-language
1452 * printer-uri
1453 * requesting-user-name
1456 request = ippNew();
1458 request->request.op.operation_id = IPP_PAUSE_PRINTER;
1459 request->request.op.request_id = 1;
1461 language = cupsLangDefault();
1463 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
1464 "attributes-charset", NULL, "utf-8");
1466 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
1467 "attributes-natural-language", NULL, language->language);
1469 if (!push_utf8_talloc(frame, &printername, lp_printername(snum),
1470 &size)) {
1471 goto out;
1473 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/printers/%s",
1474 printername);
1476 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, uri);
1478 if (!push_utf8_talloc(frame, &username, current_user_info.unix_name, &size)) {
1479 goto out;
1481 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
1482 NULL, username);
1485 * Do the request and get back a response...
1488 if ((response = cupsDoRequest(http, request, "/admin/")) != NULL) {
1489 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
1490 DEBUG(0,("Unable to pause printer %s - %s\n",
1491 lp_printername(snum),
1492 ippErrorString(cupsLastError())));
1493 } else {
1494 ret = 0;
1496 } else {
1497 DEBUG(0,("Unable to pause printer %s - %s\n",
1498 lp_printername(snum),
1499 ippErrorString(cupsLastError())));
1502 out:
1503 if (response)
1504 ippDelete(response);
1506 if (language)
1507 cupsLangFree(language);
1509 if (http)
1510 httpClose(http);
1512 TALLOC_FREE(frame);
1513 return ret;
1518 * 'cups_queue_resume()' - Restart a print queue.
1521 static int cups_queue_resume(int snum)
1523 TALLOC_CTX *frame = talloc_stackframe();
1524 int ret = 1; /* Return value */
1525 http_t *http = NULL; /* HTTP connection to server */
1526 ipp_t *request = NULL, /* IPP Request */
1527 *response = NULL; /* IPP Response */
1528 cups_lang_t *language = NULL; /* Default language */
1529 char *printername = NULL;
1530 char *username = NULL;
1531 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
1532 size_t size;
1534 DEBUG(5,("cups_queue_resume(%d)\n", snum));
1537 * Make sure we don't ask for passwords...
1540 cupsSetPasswordCB(cups_passwd_cb);
1543 * Try to connect to the server...
1546 if ((http = cups_connect(frame)) == NULL) {
1547 goto out;
1551 * Build an IPP_RESUME_PRINTER request, which requires the following
1552 * attributes:
1554 * attributes-charset
1555 * attributes-natural-language
1556 * printer-uri
1557 * requesting-user-name
1560 request = ippNew();
1562 request->request.op.operation_id = IPP_RESUME_PRINTER;
1563 request->request.op.request_id = 1;
1565 language = cupsLangDefault();
1567 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
1568 "attributes-charset", NULL, "utf-8");
1570 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
1571 "attributes-natural-language", NULL, language->language);
1573 if (!push_utf8_talloc(frame, &printername, lp_printername(snum),
1574 &size)) {
1575 goto out;
1577 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/printers/%s",
1578 printername);
1580 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, uri);
1582 if (!push_utf8_talloc(frame, &username, current_user_info.unix_name, &size)) {
1583 goto out;
1585 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
1586 NULL, username);
1589 * Do the request and get back a response...
1592 if ((response = cupsDoRequest(http, request, "/admin/")) != NULL) {
1593 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
1594 DEBUG(0,("Unable to resume printer %s - %s\n",
1595 lp_printername(snum),
1596 ippErrorString(cupsLastError())));
1597 } else {
1598 ret = 0;
1600 } else {
1601 DEBUG(0,("Unable to resume printer %s - %s\n",
1602 lp_printername(snum),
1603 ippErrorString(cupsLastError())));
1606 out:
1607 if (response)
1608 ippDelete(response);
1610 if (language)
1611 cupsLangFree(language);
1613 if (http)
1614 httpClose(http);
1616 TALLOC_FREE(frame);
1617 return ret;
1620 /*******************************************************************
1621 * CUPS printing interface definitions...
1622 ******************************************************************/
1624 struct printif cups_printif =
1626 PRINT_CUPS,
1627 cups_queue_get,
1628 cups_queue_pause,
1629 cups_queue_resume,
1630 cups_job_delete,
1631 cups_job_pause,
1632 cups_job_resume,
1633 cups_job_submit,
1636 bool cups_pull_comment_location(TALLOC_CTX *mem_ctx,
1637 const char *printername,
1638 char **comment,
1639 char **location)
1641 TALLOC_CTX *frame = talloc_stackframe();
1642 http_t *http = NULL; /* HTTP connection to server */
1643 ipp_t *request = NULL, /* IPP Request */
1644 *response = NULL; /* IPP Response */
1645 ipp_attribute_t *attr; /* Current attribute */
1646 cups_lang_t *language = NULL; /* Default language */
1647 char uri[HTTP_MAX_URI];
1648 char *server = NULL;
1649 char *sharename = NULL;
1650 char *name = NULL;
1651 static const char *requested[] =/* Requested attributes */
1653 "printer-name",
1654 "printer-info",
1655 "printer-location"
1657 bool ret = False;
1658 size_t size;
1660 DEBUG(5, ("pulling %s location\n", printername));
1663 * Make sure we don't ask for passwords...
1666 cupsSetPasswordCB(cups_passwd_cb);
1669 * Try to connect to the server...
1672 if ((http = cups_connect(frame)) == NULL) {
1673 goto out;
1676 request = ippNew();
1678 request->request.op.operation_id = IPP_GET_PRINTER_ATTRIBUTES;
1679 request->request.op.request_id = 1;
1681 language = cupsLangDefault();
1683 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
1684 "attributes-charset", NULL, "utf-8");
1686 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
1687 "attributes-natural-language", NULL, language->language);
1689 if (lp_cups_server() != NULL && strlen(lp_cups_server()) > 0) {
1690 if (!push_utf8_talloc(frame, &server, lp_cups_server(), &size)) {
1691 goto out;
1693 } else {
1694 server = talloc_strdup(frame,cupsServer());
1696 if (server) {
1697 goto out;
1699 if (!push_utf8_talloc(frame, &sharename, printername, &size)) {
1700 goto out;
1702 slprintf(uri, sizeof(uri) - 1, "ipp://%s/printers/%s",
1703 server, sharename);
1705 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
1706 "printer-uri", NULL, uri);
1708 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
1709 "requested-attributes",
1710 (sizeof(requested) / sizeof(requested[0])),
1711 NULL, requested);
1714 * Do the request and get back a response...
1717 if ((response = cupsDoRequest(http, request, "/")) == NULL) {
1718 DEBUG(0,("Unable to get printer attributes - %s\n",
1719 ippErrorString(cupsLastError())));
1720 goto out;
1723 for (attr = response->attrs; attr != NULL;) {
1725 * Skip leading attributes until we hit a printer...
1728 while (attr != NULL && attr->group_tag != IPP_TAG_PRINTER)
1729 attr = attr->next;
1731 if (attr == NULL)
1732 break;
1735 * Pull the needed attributes from this printer...
1738 while ( attr && (attr->group_tag == IPP_TAG_PRINTER) ) {
1739 if (strcmp(attr->name, "printer-name") == 0 &&
1740 attr->value_tag == IPP_TAG_NAME) {
1741 if (!pull_utf8_talloc(frame,
1742 &name,
1743 attr->values[0].string.text,
1744 &size)) {
1745 goto out;
1749 /* Grab the comment if we don't have one */
1750 if ( (strcmp(attr->name, "printer-info") == 0)
1751 && (attr->value_tag == IPP_TAG_TEXT))
1753 if (!pull_utf8_talloc(mem_ctx,
1754 comment,
1755 attr->values[0].string.text,
1756 &size)) {
1757 goto out;
1759 DEBUG(5,("cups_pull_comment_location: Using cups comment: %s\n",
1760 *comment));
1763 /* Grab the location if we don't have one */
1764 if ( (strcmp(attr->name, "printer-location") == 0)
1765 && (attr->value_tag == IPP_TAG_TEXT))
1767 if (!pull_utf8_talloc(mem_ctx,
1768 location,
1769 attr->values[0].string.text,
1770 &size)) {
1771 goto out;
1773 DEBUG(5,("cups_pull_comment_location: Using cups location: %s\n",
1774 *location));
1777 attr = attr->next;
1781 * We have everything needed...
1784 if (name != NULL)
1785 break;
1788 ret = True;
1790 out:
1791 if (response)
1792 ippDelete(response);
1794 if (request) {
1795 ippDelete(request);
1798 if (language)
1799 cupsLangFree(language);
1801 if (http)
1802 httpClose(http);
1804 TALLOC_FREE(frame);
1805 return ret;
1808 #else
1809 /* this keeps fussy compilers happy */
1810 void print_cups_dummy(void);
1811 void print_cups_dummy(void) {}
1812 #endif /* HAVE_CUPS */