s3-printing: use printcap IDL for IPC
[Samba.git] / source3 / printing / print_cups.c
blob5cea044293b55ac7af8f7fd2dea4925ac2da5228
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 "librpc/gen_ndr/ndr_printcap.h"
29 #ifdef HAVE_CUPS
30 #include <cups/cups.h>
31 #include <cups/language.h>
33 static SIG_ATOMIC_T gotalarm;
35 /***************************************************************
36 Signal function to tell us we timed out.
37 ****************************************************************/
39 static void gotalarm_sig(void)
41 gotalarm = 1;
44 extern userdom_struct current_user_info;
47 * 'cups_passwd_cb()' - The CUPS password callback...
50 static const char * /* O - Password or NULL */
51 cups_passwd_cb(const char *prompt) /* I - Prompt */
54 * Always return NULL to indicate that no password is available...
57 return (NULL);
60 static http_t *cups_connect(TALLOC_CTX *frame)
62 http_t *http = NULL;
63 char *server = NULL, *p = NULL;
64 int port;
65 int timeout = lp_cups_connection_timeout();
66 size_t size;
68 if (lp_cups_server() != NULL && strlen(lp_cups_server()) > 0) {
69 if (!push_utf8_talloc(frame, &server, lp_cups_server(), &size)) {
70 return NULL;
72 } else {
73 server = talloc_strdup(frame,cupsServer());
75 if (!server) {
76 return NULL;
79 p = strchr(server, ':');
80 if (p) {
81 port = atoi(p+1);
82 *p = '\0';
83 } else {
84 port = ippPort();
87 DEBUG(10, ("connecting to cups server %s:%d\n",
88 server, port));
90 gotalarm = 0;
92 if (timeout) {
93 CatchSignal(SIGALRM, SIGNAL_CAST gotalarm_sig);
94 alarm(timeout);
97 http = httpConnect(server, port);
99 CatchSignal(SIGALRM, SIGNAL_CAST SIG_IGN);
100 alarm(0);
102 if (http == NULL) {
103 DEBUG(0,("Unable to connect to CUPS server %s:%d - %s\n",
104 server, port, strerror(errno)));
107 return http;
110 static bool send_pcap_blob(DATA_BLOB *pcap_blob, int fd)
112 size_t ret;
114 ret = sys_write(fd, &pcap_blob->length, sizeof(pcap_blob->length));
115 if (ret != sizeof(pcap_blob->length)) {
116 return false;
119 ret = sys_write(fd, pcap_blob->data, pcap_blob->length);
120 if (ret != pcap_blob->length) {
121 return false;
124 DEBUG(10, ("successfully sent blob of len %ld\n", (int64_t)ret));
125 return true;
128 static bool recv_pcap_blob(TALLOC_CTX *mem_ctx, int fd, DATA_BLOB *pcap_blob)
130 size_t blob_len;
131 size_t ret;
133 ret = sys_read(fd, &blob_len, sizeof(blob_len));
134 if (ret != sizeof(blob_len)) {
135 return false;
138 *pcap_blob = data_blob_talloc_named(mem_ctx, NULL, blob_len,
139 "cups pcap");
140 if (pcap_blob->length != blob_len) {
141 return false;
143 ret = sys_read(fd, pcap_blob->data, blob_len);
144 if (ret != blob_len) {
145 talloc_free(pcap_blob->data);
146 return false;
149 DEBUG(10, ("successfully recvd blob of len %ld\n", (int64_t)ret));
150 return true;
153 static bool cups_cache_reload_async(int fd)
155 TALLOC_CTX *frame = talloc_stackframe();
156 struct pcap_data pcap_data;
157 struct pcap_printer *printer;
158 http_t *http = NULL; /* HTTP connection to server */
159 ipp_t *request = NULL, /* IPP Request */
160 *response = NULL; /* IPP Response */
161 ipp_attribute_t *attr; /* Current attribute */
162 cups_lang_t *language = NULL; /* Default language */
163 char *name, /* printer-name attribute */
164 *info; /* printer-info attribute */
165 static const char *requested[] =/* Requested attributes */
167 "printer-name",
168 "printer-info"
170 bool ret = False;
171 size_t size;
172 enum ndr_err_code ndr_ret;
173 DATA_BLOB pcap_blob;
175 ZERO_STRUCT(pcap_data);
176 pcap_data.status = NT_STATUS_UNSUCCESSFUL;
178 DEBUG(5, ("reloading cups printcap cache\n"));
181 * Make sure we don't ask for passwords...
184 cupsSetPasswordCB(cups_passwd_cb);
187 * Try to connect to the server...
190 if ((http = cups_connect(frame)) == NULL) {
191 goto out;
195 * Build a CUPS_GET_PRINTERS request, which requires the following
196 * attributes:
198 * attributes-charset
199 * attributes-natural-language
200 * requested-attributes
203 request = ippNew();
205 request->request.op.operation_id = CUPS_GET_PRINTERS;
206 request->request.op.request_id = 1;
208 language = cupsLangDefault();
210 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
211 "attributes-charset", NULL, "utf-8");
213 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
214 "attributes-natural-language", NULL, language->language);
216 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
217 "requested-attributes",
218 (sizeof(requested) / sizeof(requested[0])),
219 NULL, requested);
222 * Do the request and get back a response...
225 if ((response = cupsDoRequest(http, request, "/")) == NULL) {
226 DEBUG(0,("Unable to get printer list - %s\n",
227 ippErrorString(cupsLastError())));
228 goto out;
231 for (attr = response->attrs; attr != NULL;) {
233 * Skip leading attributes until we hit a printer...
236 while (attr != NULL && attr->group_tag != IPP_TAG_PRINTER)
237 attr = attr->next;
239 if (attr == NULL)
240 break;
243 * Pull the needed attributes from this printer...
246 name = NULL;
247 info = NULL;
249 while (attr != NULL && attr->group_tag == IPP_TAG_PRINTER) {
250 if (strcmp(attr->name, "printer-name") == 0 &&
251 attr->value_tag == IPP_TAG_NAME) {
252 if (!pull_utf8_talloc(frame,
253 &name,
254 attr->values[0].string.text,
255 &size)) {
256 goto out;
260 if (strcmp(attr->name, "printer-info") == 0 &&
261 attr->value_tag == IPP_TAG_TEXT) {
262 if (!pull_utf8_talloc(frame,
263 &info,
264 attr->values[0].string.text,
265 &size)) {
266 goto out;
270 attr = attr->next;
274 * See if we have everything needed...
277 if (name == NULL)
278 break;
280 if (pcap_data.count == 0) {
281 printer = talloc_array(frame, struct pcap_printer, 1);
282 } else {
283 printer = talloc_realloc(frame, pcap_data.printers,
284 struct pcap_printer,
285 pcap_data.count + 1);
287 if (printer == NULL) {
288 goto out;
290 pcap_data.printers = printer;
291 pcap_data.printers[pcap_data.count].name = name;
292 pcap_data.printers[pcap_data.count].info = info;
293 pcap_data.count++;
296 ippDelete(response);
297 response = NULL;
300 * Build a CUPS_GET_CLASSES request, which requires the following
301 * attributes:
303 * attributes-charset
304 * attributes-natural-language
305 * requested-attributes
308 request = ippNew();
310 request->request.op.operation_id = CUPS_GET_CLASSES;
311 request->request.op.request_id = 1;
313 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
314 "attributes-charset", NULL, "utf-8");
316 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
317 "attributes-natural-language", NULL, language->language);
319 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
320 "requested-attributes",
321 (sizeof(requested) / sizeof(requested[0])),
322 NULL, requested);
325 * Do the request and get back a response...
328 if ((response = cupsDoRequest(http, request, "/")) == NULL) {
329 DEBUG(0,("Unable to get printer list - %s\n",
330 ippErrorString(cupsLastError())));
331 goto out;
334 for (attr = response->attrs; attr != NULL;) {
336 * Skip leading attributes until we hit a printer...
339 while (attr != NULL && attr->group_tag != IPP_TAG_PRINTER)
340 attr = attr->next;
342 if (attr == NULL)
343 break;
346 * Pull the needed attributes from this printer...
349 name = NULL;
350 info = NULL;
352 while (attr != NULL && attr->group_tag == IPP_TAG_PRINTER) {
353 if (strcmp(attr->name, "printer-name") == 0 &&
354 attr->value_tag == IPP_TAG_NAME) {
355 if (!pull_utf8_talloc(frame,
356 &name,
357 attr->values[0].string.text,
358 &size)) {
359 goto out;
363 if (strcmp(attr->name, "printer-info") == 0 &&
364 attr->value_tag == IPP_TAG_TEXT) {
365 if (!pull_utf8_talloc(frame,
366 &info,
367 attr->values[0].string.text,
368 &size)) {
369 goto out;
373 attr = attr->next;
377 * See if we have everything needed...
380 if (name == NULL)
381 break;
383 if (pcap_data.count == 0) {
384 printer = talloc_array(frame, struct pcap_printer, 1);
385 } else {
386 printer = talloc_realloc(frame, pcap_data.printers,
387 struct pcap_printer,
388 pcap_data.count + 1);
390 if (printer == NULL) {
391 goto out;
393 pcap_data.printers = printer;
394 pcap_data.printers[pcap_data.count].name = name;
395 pcap_data.printers[pcap_data.count].info = info;
396 pcap_data.count++;
399 ret = true;
400 pcap_data.status = NT_STATUS_OK;
401 out:
402 if (response)
403 ippDelete(response);
405 if (language)
406 cupsLangFree(language);
408 if (http)
409 httpClose(http);
411 /* Send all the entries up the pipe. */
412 ndr_ret = ndr_push_struct_blob(&pcap_blob, frame, NULL, &pcap_data,
413 (ndr_push_flags_fn_t)ndr_push_pcap_data);
414 if (ndr_ret == NDR_ERR_SUCCESS) {
415 ret = send_pcap_blob(&pcap_blob, fd);
416 } else {
417 ret = false;
420 TALLOC_FREE(frame);
421 return ret;
424 static struct pcap_cache *local_pcap_copy;
425 struct fd_event *cache_fd_event;
427 static bool cups_pcap_load_async(int *pfd)
429 int fds[2];
430 pid_t pid;
432 *pfd = -1;
434 if (cache_fd_event) {
435 DEBUG(3,("cups_pcap_load_async: already waiting for "
436 "a refresh event\n" ));
437 return false;
440 DEBUG(5,("cups_pcap_load_async: asynchronously loading cups printers\n"));
442 if (pipe(fds) == -1) {
443 return false;
446 pid = sys_fork();
447 if (pid == (pid_t)-1) {
448 DEBUG(10,("cups_pcap_load_async: fork failed %s\n",
449 strerror(errno) ));
450 close(fds[0]);
451 close(fds[1]);
452 return false;
455 if (pid) {
456 DEBUG(10,("cups_pcap_load_async: child pid = %u\n",
457 (unsigned int)pid ));
458 /* Parent. */
459 close(fds[1]);
460 *pfd = fds[0];
461 return true;
464 /* Child. */
466 close_all_print_db();
468 if (!NT_STATUS_IS_OK(reinit_after_fork(smbd_messaging_context(),
469 smbd_event_context(), true))) {
470 DEBUG(0,("cups_pcap_load_async: reinit_after_fork() failed\n"));
471 smb_panic("cups_pcap_load_async: reinit_after_fork() failed");
474 close(fds[0]);
475 cups_cache_reload_async(fds[1]);
476 close(fds[1]);
477 _exit(0);
480 struct cups_async_cb_args {
481 int pipe_fd;
482 void (*post_cache_fill_fn)(void);
485 static void cups_async_callback(struct event_context *event_ctx,
486 struct fd_event *event,
487 uint16 flags,
488 void *p)
490 TALLOC_CTX *frame = talloc_stackframe();
491 struct cups_async_cb_args *cb_args = (struct cups_async_cb_args *)p;
492 struct pcap_cache *tmp_pcap_cache = NULL;
493 bool ret_ok;
494 struct pcap_data pcap_data;
495 DATA_BLOB pcap_blob;
496 enum ndr_err_code ndr_ret;
497 int i;
499 DEBUG(5,("cups_async_callback: callback received for printer data. "
500 "fd = %d\n", cb_args->pipe_fd));
502 ret_ok = recv_pcap_blob(frame, cb_args->pipe_fd, &pcap_blob);
503 if (!ret_ok) {
504 DEBUG(0,("failed to recv pcap blob\n"));
505 goto err_out;
508 ndr_ret = ndr_pull_struct_blob(&pcap_blob, frame, NULL, &pcap_data,
509 (ndr_pull_flags_fn_t)ndr_pull_pcap_data);
510 if (ndr_ret != NDR_ERR_SUCCESS) {
511 goto err_out;
514 if (!NT_STATUS_IS_OK(pcap_data.status)) {
515 DEBUG(0,("failed to retrieve printer list: %s\n",
516 nt_errstr(pcap_data.status)));
517 goto err_out;
520 for (i = 0; i < pcap_data.count; i++) {
521 ret_ok = pcap_cache_add_specific(&tmp_pcap_cache,
522 pcap_data.printers[i].name,
523 pcap_data.printers[i].info);
524 if (!ret_ok) {
525 DEBUG(0, ("failed to add to tmp pcap cache\n"));
526 break;
530 if (!ret_ok) {
531 DEBUG(0,("failed to read a new printer list\n"));
532 pcap_cache_destroy_specific(&tmp_pcap_cache);
533 } else {
534 /* We got a possibly empty namelist, replace our local cache. */
535 pcap_cache_destroy_specific(&local_pcap_copy);
536 local_pcap_copy = tmp_pcap_cache;
538 /* And the systemwide pcap cache. */
539 pcap_cache_replace(local_pcap_copy);
541 /* Caller may have requested post cache fill callback */
542 if (cb_args->post_cache_fill_fn) {
543 cb_args->post_cache_fill_fn();
546 err_out:
547 TALLOC_FREE(frame);
548 close(cb_args->pipe_fd);
549 TALLOC_FREE(cb_args);
550 TALLOC_FREE(cache_fd_event);
553 bool cups_cache_reload(void (*post_cache_fill_fn)(void))
555 struct cups_async_cb_args *cb_args;
556 int *p_pipe_fd;
558 cb_args = TALLOC_P(NULL, struct cups_async_cb_args);
559 if (!cb_args) {
560 return false;
562 cb_args->post_cache_fill_fn = post_cache_fill_fn;
563 p_pipe_fd = &cb_args->pipe_fd;
564 *p_pipe_fd = -1;
566 /* Set up an async refresh. */
567 if (!cups_pcap_load_async(p_pipe_fd)) {
568 talloc_free(cb_args);
569 return false;
571 if (!local_pcap_copy) {
572 /* We have no local cache, wait directly for
573 * async refresh to complete.
575 DEBUG(10,("cups_cache_reload: sync read on fd %d\n",
576 *p_pipe_fd ));
578 cups_async_callback(smbd_event_context(),
579 NULL,
580 EVENT_FD_READ,
581 (void *)cb_args);
582 if (!local_pcap_copy) {
583 return false;
585 } else {
586 /* Replace the system cache with our
587 * local copy. */
588 pcap_cache_replace(local_pcap_copy);
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(smbd_event_context(),
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 const char *clientname = NULL; /* hostname of client for job-originating-host 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 uint32_t jobid = (uint32_t)-1;
922 char addr[INET6_ADDRSTRLEN];
924 DEBUG(5,("cups_job_submit(%d, %p)\n", snum, pjob));
927 * Make sure we don't ask for passwords...
930 cupsSetPasswordCB(cups_passwd_cb);
933 * Try to connect to the server...
936 if ((http = cups_connect(frame)) == NULL) {
937 goto out;
941 * Build an IPP_PRINT_JOB request, which requires the following
942 * attributes:
944 * attributes-charset
945 * attributes-natural-language
946 * printer-uri
947 * requesting-user-name
948 * [document-data]
951 request = ippNew();
953 request->request.op.operation_id = IPP_PRINT_JOB;
954 request->request.op.request_id = 1;
956 language = cupsLangDefault();
958 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
959 "attributes-charset", NULL, "utf-8");
961 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
962 "attributes-natural-language", NULL, language->language);
964 if (!push_utf8_talloc(frame, &printername, PRINTERNAME(snum), &size)) {
965 goto out;
967 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/printers/%s",
968 printername);
970 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
971 "printer-uri", NULL, uri);
973 if (!push_utf8_talloc(frame, &user, pjob->user, &size)) {
974 goto out;
976 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
977 NULL, user);
979 clientname = client_name(get_client_fd());
980 if (strcmp(clientname, "UNKNOWN") == 0) {
981 clientname = client_addr(get_client_fd(),addr,sizeof(addr));
984 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
985 "job-originating-host-name", NULL,
986 clientname);
988 /* Get the jobid from the filename. */
989 jobid = print_parse_jobid(pjob->filename);
990 if (jobid == (uint32_t)-1) {
991 DEBUG(0,("cups_job_submit: failed to parse jobid from name %s\n",
992 pjob->filename ));
993 jobid = 0;
996 if (!push_utf8_talloc(frame, &jobname, pjob->jobname, &size)) {
997 goto out;
999 new_jobname = talloc_asprintf(frame,
1000 "%s%.8u %s", PRINT_SPOOL_PREFIX,
1001 (unsigned int)jobid,
1002 jobname);
1003 if (new_jobname == NULL) {
1004 goto out;
1007 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "job-name", NULL,
1008 new_jobname);
1011 * add any options defined in smb.conf
1014 if (!push_utf8_talloc(frame, &cupsoptions, lp_cups_options(snum), &size)) {
1015 goto out;
1017 num_options = 0;
1018 options = NULL;
1019 num_options = cupsParseOptions(cupsoptions, num_options, &options);
1021 if ( num_options )
1022 cupsEncodeOptions(request, num_options, options);
1025 * Do the request and get back a response...
1028 slprintf(uri, sizeof(uri) - 1, "/printers/%s", printername);
1030 if (!push_utf8_talloc(frame, &filename, pjob->filename, &size)) {
1031 goto out;
1033 if ((response = cupsDoFileRequest(http, request, uri, pjob->filename)) != NULL) {
1034 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
1035 DEBUG(0,("Unable to print file to %s - %s\n", PRINTERNAME(snum),
1036 ippErrorString(cupsLastError())));
1037 } else {
1038 ret = 0;
1039 attr_job_id = ippFindAttribute(response, "job-id", IPP_TAG_INTEGER);
1040 if(attr_job_id) {
1041 pjob->sysjob = attr_job_id->values[0].integer;
1042 DEBUG(5,("cups_job_submit: job-id %d\n", pjob->sysjob));
1043 } else {
1044 DEBUG(0,("Missing job-id attribute in IPP response"));
1047 } else {
1048 DEBUG(0,("Unable to print file to `%s' - %s\n", PRINTERNAME(snum),
1049 ippErrorString(cupsLastError())));
1052 if ( ret == 0 )
1053 unlink(pjob->filename);
1054 /* else print_job_end will do it for us */
1056 out:
1057 if (response)
1058 ippDelete(response);
1060 if (language)
1061 cupsLangFree(language);
1063 if (http)
1064 httpClose(http);
1066 TALLOC_FREE(frame);
1068 return ret;
1072 * 'cups_queue_get()' - Get all the jobs in the print queue.
1075 static int cups_queue_get(const char *sharename,
1076 enum printing_types printing_type,
1077 char *lpq_command,
1078 print_queue_struct **q,
1079 print_status_struct *status)
1081 TALLOC_CTX *frame = talloc_stackframe();
1082 char *printername = NULL;
1083 http_t *http = NULL; /* HTTP connection to server */
1084 ipp_t *request = NULL, /* IPP Request */
1085 *response = NULL; /* IPP Response */
1086 ipp_attribute_t *attr = NULL; /* Current attribute */
1087 cups_lang_t *language = NULL; /* Default language */
1088 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
1089 int qcount = 0, /* Number of active queue entries */
1090 qalloc = 0; /* Number of queue entries allocated */
1091 print_queue_struct *queue = NULL, /* Queue entries */
1092 *temp; /* Temporary pointer for queue */
1093 char *user_name = NULL, /* job-originating-user-name attribute */
1094 *job_name = NULL; /* job-name attribute */
1095 int job_id; /* job-id attribute */
1096 int job_k_octets; /* job-k-octets attribute */
1097 time_t job_time; /* time-at-creation attribute */
1098 ipp_jstate_t job_status; /* job-status attribute */
1099 int job_priority; /* job-priority attribute */
1100 size_t size;
1101 static const char *jattrs[] = /* Requested job attributes */
1103 "job-id",
1104 "job-k-octets",
1105 "job-name",
1106 "job-originating-user-name",
1107 "job-priority",
1108 "job-state",
1109 "time-at-creation",
1111 static const char *pattrs[] = /* Requested printer attributes */
1113 "printer-state",
1114 "printer-state-message"
1117 *q = NULL;
1119 /* HACK ALERT!!! The problem with support the 'printer name'
1120 option is that we key the tdb off the sharename. So we will
1121 overload the lpq_command string to pass in the printername
1122 (which is basically what we do for non-cups printers ... using
1123 the lpq_command to get the queue listing). */
1125 if (!push_utf8_talloc(frame, &printername, lpq_command, &size)) {
1126 goto out;
1128 DEBUG(5,("cups_queue_get(%s, %p, %p)\n", lpq_command, q, status));
1131 * Make sure we don't ask for passwords...
1134 cupsSetPasswordCB(cups_passwd_cb);
1137 * Try to connect to the server...
1140 if ((http = cups_connect(frame)) == NULL) {
1141 goto out;
1145 * Generate the printer URI...
1148 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/printers/%s", printername);
1151 * Build an IPP_GET_JOBS request, which requires the following
1152 * attributes:
1154 * attributes-charset
1155 * attributes-natural-language
1156 * requested-attributes
1157 * printer-uri
1160 request = ippNew();
1162 request->request.op.operation_id = IPP_GET_JOBS;
1163 request->request.op.request_id = 1;
1165 language = cupsLangDefault();
1167 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
1168 "attributes-charset", NULL, "utf-8");
1170 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
1171 "attributes-natural-language", NULL, language->language);
1173 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
1174 "requested-attributes",
1175 (sizeof(jattrs) / sizeof(jattrs[0])),
1176 NULL, jattrs);
1178 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
1179 "printer-uri", NULL, uri);
1182 * Do the request and get back a response...
1185 if ((response = cupsDoRequest(http, request, "/")) == NULL) {
1186 DEBUG(0,("Unable to get jobs for %s - %s\n", uri,
1187 ippErrorString(cupsLastError())));
1188 goto out;
1191 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
1192 DEBUG(0,("Unable to get jobs for %s - %s\n", uri,
1193 ippErrorString(response->request.status.status_code)));
1194 goto out;
1198 * Process the jobs...
1201 qcount = 0;
1202 qalloc = 0;
1203 queue = NULL;
1205 for (attr = response->attrs; attr != NULL; attr = attr->next) {
1207 * Skip leading attributes until we hit a job...
1210 while (attr != NULL && attr->group_tag != IPP_TAG_JOB)
1211 attr = attr->next;
1213 if (attr == NULL)
1214 break;
1217 * Allocate memory as needed...
1219 if (qcount >= qalloc) {
1220 qalloc += 16;
1222 queue = SMB_REALLOC_ARRAY(queue, print_queue_struct, qalloc);
1224 if (queue == NULL) {
1225 DEBUG(0,("cups_queue_get: Not enough memory!"));
1226 qcount = 0;
1227 goto out;
1231 temp = queue + qcount;
1232 memset(temp, 0, sizeof(print_queue_struct));
1235 * Pull the needed attributes from this job...
1238 job_id = 0;
1239 job_priority = 50;
1240 job_status = IPP_JOB_PENDING;
1241 job_time = 0;
1242 job_k_octets = 0;
1243 user_name = NULL;
1244 job_name = NULL;
1246 while (attr != NULL && attr->group_tag == IPP_TAG_JOB) {
1247 if (attr->name == NULL) {
1248 attr = attr->next;
1249 break;
1252 if (strcmp(attr->name, "job-id") == 0 &&
1253 attr->value_tag == IPP_TAG_INTEGER)
1254 job_id = attr->values[0].integer;
1256 if (strcmp(attr->name, "job-k-octets") == 0 &&
1257 attr->value_tag == IPP_TAG_INTEGER)
1258 job_k_octets = attr->values[0].integer;
1260 if (strcmp(attr->name, "job-priority") == 0 &&
1261 attr->value_tag == IPP_TAG_INTEGER)
1262 job_priority = attr->values[0].integer;
1264 if (strcmp(attr->name, "job-state") == 0 &&
1265 attr->value_tag == IPP_TAG_ENUM)
1266 job_status = (ipp_jstate_t)(attr->values[0].integer);
1268 if (strcmp(attr->name, "time-at-creation") == 0 &&
1269 attr->value_tag == IPP_TAG_INTEGER)
1270 job_time = attr->values[0].integer;
1272 if (strcmp(attr->name, "job-name") == 0 &&
1273 attr->value_tag == IPP_TAG_NAME) {
1274 if (!pull_utf8_talloc(frame,
1275 &job_name,
1276 attr->values[0].string.text,
1277 &size)) {
1278 goto out;
1282 if (strcmp(attr->name, "job-originating-user-name") == 0 &&
1283 attr->value_tag == IPP_TAG_NAME) {
1284 if (!pull_utf8_talloc(frame,
1285 &user_name,
1286 attr->values[0].string.text,
1287 &size)) {
1288 goto out;
1292 attr = attr->next;
1296 * See if we have everything needed...
1299 if (user_name == NULL || job_name == NULL || job_id == 0) {
1300 if (attr == NULL)
1301 break;
1302 else
1303 continue;
1306 temp->job = job_id;
1307 temp->size = job_k_octets * 1024;
1308 temp->status = job_status == IPP_JOB_PENDING ? LPQ_QUEUED :
1309 job_status == IPP_JOB_STOPPED ? LPQ_PAUSED :
1310 job_status == IPP_JOB_HELD ? LPQ_PAUSED :
1311 LPQ_PRINTING;
1312 temp->priority = job_priority;
1313 temp->time = job_time;
1314 strlcpy(temp->fs_user, user_name, sizeof(temp->fs_user));
1315 strlcpy(temp->fs_file, job_name, sizeof(temp->fs_file));
1317 qcount ++;
1319 if (attr == NULL)
1320 break;
1323 ippDelete(response);
1324 response = NULL;
1327 * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the
1328 * following attributes:
1330 * attributes-charset
1331 * attributes-natural-language
1332 * requested-attributes
1333 * printer-uri
1336 request = ippNew();
1338 request->request.op.operation_id = IPP_GET_PRINTER_ATTRIBUTES;
1339 request->request.op.request_id = 1;
1341 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
1342 "attributes-charset", NULL, "utf-8");
1344 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
1345 "attributes-natural-language", NULL, language->language);
1347 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
1348 "requested-attributes",
1349 (sizeof(pattrs) / sizeof(pattrs[0])),
1350 NULL, pattrs);
1352 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
1353 "printer-uri", NULL, uri);
1356 * Do the request and get back a response...
1359 if ((response = cupsDoRequest(http, request, "/")) == NULL) {
1360 DEBUG(0,("Unable to get printer status for %s - %s\n", printername,
1361 ippErrorString(cupsLastError())));
1362 *q = queue;
1363 goto out;
1366 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
1367 DEBUG(0,("Unable to get printer status for %s - %s\n", printername,
1368 ippErrorString(response->request.status.status_code)));
1369 *q = queue;
1370 goto out;
1374 * Get the current printer status and convert it to the SAMBA values.
1377 if ((attr = ippFindAttribute(response, "printer-state", IPP_TAG_ENUM)) != NULL) {
1378 if (attr->values[0].integer == IPP_PRINTER_STOPPED)
1379 status->status = LPSTAT_STOPPED;
1380 else
1381 status->status = LPSTAT_OK;
1384 if ((attr = ippFindAttribute(response, "printer-state-message",
1385 IPP_TAG_TEXT)) != NULL) {
1386 char *msg = NULL;
1387 if (!pull_utf8_talloc(frame, &msg,
1388 attr->values[0].string.text,
1389 &size)) {
1390 SAFE_FREE(queue);
1391 qcount = 0;
1392 goto out;
1394 fstrcpy(status->message, msg);
1398 * Return the job queue...
1401 *q = queue;
1403 out:
1404 if (response)
1405 ippDelete(response);
1407 if (language)
1408 cupsLangFree(language);
1410 if (http)
1411 httpClose(http);
1413 TALLOC_FREE(frame);
1414 return qcount;
1419 * 'cups_queue_pause()' - Pause a print queue.
1422 static int cups_queue_pause(int snum)
1424 TALLOC_CTX *frame = talloc_stackframe();
1425 int ret = 1; /* Return value */
1426 http_t *http = NULL; /* HTTP connection to server */
1427 ipp_t *request = NULL, /* IPP Request */
1428 *response = NULL; /* IPP Response */
1429 cups_lang_t *language = NULL; /* Default language */
1430 char *printername = NULL;
1431 char *username = NULL;
1432 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
1433 size_t size;
1435 DEBUG(5,("cups_queue_pause(%d)\n", snum));
1438 * Make sure we don't ask for passwords...
1441 cupsSetPasswordCB(cups_passwd_cb);
1444 * Try to connect to the server...
1447 if ((http = cups_connect(frame)) == NULL) {
1448 goto out;
1452 * Build an IPP_PAUSE_PRINTER request, which requires the following
1453 * attributes:
1455 * attributes-charset
1456 * attributes-natural-language
1457 * printer-uri
1458 * requesting-user-name
1461 request = ippNew();
1463 request->request.op.operation_id = IPP_PAUSE_PRINTER;
1464 request->request.op.request_id = 1;
1466 language = cupsLangDefault();
1468 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
1469 "attributes-charset", NULL, "utf-8");
1471 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
1472 "attributes-natural-language", NULL, language->language);
1474 if (!push_utf8_talloc(frame, &printername, PRINTERNAME(snum), &size)) {
1475 goto out;
1477 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/printers/%s",
1478 printername);
1480 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, uri);
1482 if (!push_utf8_talloc(frame, &username, current_user_info.unix_name, &size)) {
1483 goto out;
1485 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
1486 NULL, username);
1489 * Do the request and get back a response...
1492 if ((response = cupsDoRequest(http, request, "/admin/")) != NULL) {
1493 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
1494 DEBUG(0,("Unable to pause printer %s - %s\n", PRINTERNAME(snum),
1495 ippErrorString(cupsLastError())));
1496 } else {
1497 ret = 0;
1499 } else {
1500 DEBUG(0,("Unable to pause printer %s - %s\n", PRINTERNAME(snum),
1501 ippErrorString(cupsLastError())));
1504 out:
1505 if (response)
1506 ippDelete(response);
1508 if (language)
1509 cupsLangFree(language);
1511 if (http)
1512 httpClose(http);
1514 TALLOC_FREE(frame);
1515 return ret;
1520 * 'cups_queue_resume()' - Restart a print queue.
1523 static int cups_queue_resume(int snum)
1525 TALLOC_CTX *frame = talloc_stackframe();
1526 int ret = 1; /* Return value */
1527 http_t *http = NULL; /* HTTP connection to server */
1528 ipp_t *request = NULL, /* IPP Request */
1529 *response = NULL; /* IPP Response */
1530 cups_lang_t *language = NULL; /* Default language */
1531 char *printername = NULL;
1532 char *username = NULL;
1533 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
1534 size_t size;
1536 DEBUG(5,("cups_queue_resume(%d)\n", snum));
1539 * Make sure we don't ask for passwords...
1542 cupsSetPasswordCB(cups_passwd_cb);
1545 * Try to connect to the server...
1548 if ((http = cups_connect(frame)) == NULL) {
1549 goto out;
1553 * Build an IPP_RESUME_PRINTER request, which requires the following
1554 * attributes:
1556 * attributes-charset
1557 * attributes-natural-language
1558 * printer-uri
1559 * requesting-user-name
1562 request = ippNew();
1564 request->request.op.operation_id = IPP_RESUME_PRINTER;
1565 request->request.op.request_id = 1;
1567 language = cupsLangDefault();
1569 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
1570 "attributes-charset", NULL, "utf-8");
1572 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
1573 "attributes-natural-language", NULL, language->language);
1575 if (!push_utf8_talloc(frame, &printername, PRINTERNAME(snum), &size)) {
1576 goto out;
1578 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/printers/%s",
1579 printername);
1581 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, uri);
1583 if (!push_utf8_talloc(frame, &username, current_user_info.unix_name, &size)) {
1584 goto out;
1586 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
1587 NULL, username);
1590 * Do the request and get back a response...
1593 if ((response = cupsDoRequest(http, request, "/admin/")) != NULL) {
1594 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
1595 DEBUG(0,("Unable to resume printer %s - %s\n", PRINTERNAME(snum),
1596 ippErrorString(cupsLastError())));
1597 } else {
1598 ret = 0;
1600 } else {
1601 DEBUG(0,("Unable to resume printer %s - %s\n", PRINTERNAME(snum),
1602 ippErrorString(cupsLastError())));
1605 out:
1606 if (response)
1607 ippDelete(response);
1609 if (language)
1610 cupsLangFree(language);
1612 if (http)
1613 httpClose(http);
1615 TALLOC_FREE(frame);
1616 return ret;
1619 /*******************************************************************
1620 * CUPS printing interface definitions...
1621 ******************************************************************/
1623 struct printif cups_printif =
1625 PRINT_CUPS,
1626 cups_queue_get,
1627 cups_queue_pause,
1628 cups_queue_resume,
1629 cups_job_delete,
1630 cups_job_pause,
1631 cups_job_resume,
1632 cups_job_submit,
1635 bool cups_pull_comment_location(NT_PRINTER_INFO_LEVEL_2 *printer)
1637 TALLOC_CTX *frame = talloc_stackframe();
1638 http_t *http = NULL; /* HTTP connection to server */
1639 ipp_t *request = NULL, /* IPP Request */
1640 *response = NULL; /* IPP Response */
1641 ipp_attribute_t *attr; /* Current attribute */
1642 cups_lang_t *language = NULL; /* Default language */
1643 char uri[HTTP_MAX_URI];
1644 char *server = NULL;
1645 char *sharename = NULL;
1646 char *name = NULL;
1647 static const char *requested[] =/* Requested attributes */
1649 "printer-name",
1650 "printer-info",
1651 "printer-location"
1653 bool ret = False;
1654 size_t size;
1656 DEBUG(5, ("pulling %s location\n", printer->sharename));
1659 * Make sure we don't ask for passwords...
1662 cupsSetPasswordCB(cups_passwd_cb);
1665 * Try to connect to the server...
1668 if ((http = cups_connect(frame)) == NULL) {
1669 goto out;
1672 request = ippNew();
1674 request->request.op.operation_id = IPP_GET_PRINTER_ATTRIBUTES;
1675 request->request.op.request_id = 1;
1677 language = cupsLangDefault();
1679 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
1680 "attributes-charset", NULL, "utf-8");
1682 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
1683 "attributes-natural-language", NULL, language->language);
1685 if (lp_cups_server() != NULL && strlen(lp_cups_server()) > 0) {
1686 if (!push_utf8_talloc(frame, &server, lp_cups_server(), &size)) {
1687 goto out;
1689 } else {
1690 server = talloc_strdup(frame,cupsServer());
1692 if (server) {
1693 goto out;
1695 if (!push_utf8_talloc(frame, &sharename, printer->sharename, &size)) {
1696 goto out;
1698 slprintf(uri, sizeof(uri) - 1, "ipp://%s/printers/%s",
1699 server, sharename);
1701 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
1702 "printer-uri", NULL, uri);
1704 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
1705 "requested-attributes",
1706 (sizeof(requested) / sizeof(requested[0])),
1707 NULL, requested);
1710 * Do the request and get back a response...
1713 if ((response = cupsDoRequest(http, request, "/")) == NULL) {
1714 DEBUG(0,("Unable to get printer attributes - %s\n",
1715 ippErrorString(cupsLastError())));
1716 goto out;
1719 for (attr = response->attrs; attr != NULL;) {
1721 * Skip leading attributes until we hit a printer...
1724 while (attr != NULL && attr->group_tag != IPP_TAG_PRINTER)
1725 attr = attr->next;
1727 if (attr == NULL)
1728 break;
1731 * Pull the needed attributes from this printer...
1734 while ( attr && (attr->group_tag == IPP_TAG_PRINTER) ) {
1735 if (strcmp(attr->name, "printer-name") == 0 &&
1736 attr->value_tag == IPP_TAG_NAME) {
1737 if (!pull_utf8_talloc(frame,
1738 &name,
1739 attr->values[0].string.text,
1740 &size)) {
1741 goto out;
1745 /* Grab the comment if we don't have one */
1746 if ( (strcmp(attr->name, "printer-info") == 0)
1747 && (attr->value_tag == IPP_TAG_TEXT)
1748 && !strlen(printer->comment) )
1750 char *comment = NULL;
1751 if (!pull_utf8_talloc(frame,
1752 &comment,
1753 attr->values[0].string.text,
1754 &size)) {
1755 goto out;
1757 DEBUG(5,("cups_pull_comment_location: Using cups comment: %s\n",
1758 comment));
1759 strlcpy(printer->comment,
1760 comment,
1761 sizeof(printer->comment));
1764 /* Grab the location if we don't have one */
1765 if ( (strcmp(attr->name, "printer-location") == 0)
1766 && (attr->value_tag == IPP_TAG_TEXT)
1767 && !strlen(printer->location) )
1769 char *location = NULL;
1770 if (!pull_utf8_talloc(frame,
1771 &location,
1772 attr->values[0].string.text,
1773 &size)) {
1774 goto out;
1776 DEBUG(5,("cups_pull_comment_location: Using cups location: %s\n",
1777 location));
1778 strlcpy(printer->location,
1779 location,
1780 sizeof(printer->location));
1783 attr = attr->next;
1787 * We have everything needed...
1790 if (name != NULL)
1791 break;
1794 ret = True;
1796 out:
1797 if (response)
1798 ippDelete(response);
1800 if (request) {
1801 ippDelete(request);
1804 if (language)
1805 cupsLangFree(language);
1807 if (http)
1808 httpClose(http);
1810 TALLOC_FREE(frame);
1811 return ret;
1814 #else
1815 /* this keeps fussy compilers happy */
1816 void print_cups_dummy(void);
1817 void print_cups_dummy(void) {}
1818 #endif /* HAVE_CUPS */