s3-printing: fix cups pcap reload with no printers
[Samba.git] / source3 / printing / print_cups.c
blob3031a88e5d110c905d9d53c974f56dc022c8fc5c
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"
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(int signum)
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, gotalarm_sig);
94 alarm(timeout);
97 #ifdef HAVE_HTTPCONNECTENCRYPT
98 http = httpConnectEncrypt(server, port, lp_cups_encrypt());
99 #else
100 http = httpConnect(server, port);
101 #endif
104 CatchSignal(SIGALRM, SIG_IGN);
105 alarm(0);
107 if (http == NULL) {
108 DEBUG(0,("Unable to connect to CUPS server %s:%d - %s\n",
109 server, port, strerror(errno)));
112 return http;
115 static void send_pcap_info(const char *name, const char *info, void *pd)
117 int fd = *(int *)pd;
118 size_t namelen = name ? strlen(name)+1 : 0;
119 size_t infolen = info ? strlen(info)+1 : 0;
121 DEBUG(11,("send_pcap_info: writing namelen %u\n", (unsigned int)namelen));
122 if (sys_write(fd, &namelen, sizeof(namelen)) != sizeof(namelen)) {
123 DEBUG(10,("send_pcap_info: namelen write failed %s\n",
124 strerror(errno)));
125 return;
127 DEBUG(11,("send_pcap_info: writing infolen %u\n", (unsigned int)infolen));
128 if (sys_write(fd, &infolen, sizeof(infolen)) != sizeof(infolen)) {
129 DEBUG(10,("send_pcap_info: infolen write failed %s\n",
130 strerror(errno)));
131 return;
133 if (namelen) {
134 DEBUG(11,("send_pcap_info: writing name %s\n", name));
135 if (sys_write(fd, name, namelen) != namelen) {
136 DEBUG(10,("send_pcap_info: name write failed %s\n",
137 strerror(errno)));
138 return;
141 if (infolen) {
142 DEBUG(11,("send_pcap_info: writing info %s\n", info));
143 if (sys_write(fd, info, infolen) != infolen) {
144 DEBUG(10,("send_pcap_info: info write failed %s\n",
145 strerror(errno)));
146 return;
151 static bool cups_cache_reload_async(int fd)
153 TALLOC_CTX *frame = talloc_stackframe();
154 struct pcap_cache *tmp_pcap_cache = NULL;
155 http_t *http = NULL; /* HTTP connection to server */
156 ipp_t *request = NULL, /* IPP Request */
157 *response = NULL; /* IPP Response */
158 ipp_attribute_t *attr; /* Current attribute */
159 cups_lang_t *language = NULL; /* Default language */
160 char *name, /* printer-name attribute */
161 *info; /* printer-info attribute */
162 static const char *requested[] =/* Requested attributes */
164 "printer-name",
165 "printer-info"
167 bool ret = False;
168 size_t size;
170 DEBUG(5, ("reloading cups printcap cache\n"));
173 * Make sure we don't ask for passwords...
176 cupsSetPasswordCB(cups_passwd_cb);
179 * Try to connect to the server...
182 if ((http = cups_connect(frame)) == NULL) {
183 goto out;
187 * Build a CUPS_GET_PRINTERS request, which requires the following
188 * attributes:
190 * attributes-charset
191 * attributes-natural-language
192 * requested-attributes
195 request = ippNew();
197 request->request.op.operation_id = CUPS_GET_PRINTERS;
198 request->request.op.request_id = 1;
200 language = cupsLangDefault();
202 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
203 "attributes-charset", NULL, "utf-8");
205 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
206 "attributes-natural-language", NULL, language->language);
208 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
209 "requested-attributes",
210 (sizeof(requested) / sizeof(requested[0])),
211 NULL, requested);
214 * Do the request and get back a response...
217 if ((response = cupsDoRequest(http, request, "/")) == NULL) {
218 DEBUG(0,("Unable to get printer list - %s\n",
219 ippErrorString(cupsLastError())));
220 goto out;
223 for (attr = response->attrs; attr != NULL;) {
225 * Skip leading attributes until we hit a printer...
228 while (attr != NULL && attr->group_tag != IPP_TAG_PRINTER)
229 attr = attr->next;
231 if (attr == NULL)
232 break;
235 * Pull the needed attributes from this printer...
238 name = NULL;
239 info = NULL;
241 while (attr != NULL && attr->group_tag == IPP_TAG_PRINTER) {
242 if (strcmp(attr->name, "printer-name") == 0 &&
243 attr->value_tag == IPP_TAG_NAME) {
244 if (!pull_utf8_talloc(frame,
245 &name,
246 attr->values[0].string.text,
247 &size)) {
248 goto out;
252 if (strcmp(attr->name, "printer-info") == 0 &&
253 attr->value_tag == IPP_TAG_TEXT) {
254 if (!pull_utf8_talloc(frame,
255 &info,
256 attr->values[0].string.text,
257 &size)) {
258 goto out;
262 attr = attr->next;
266 * See if we have everything needed...
269 if (name == NULL)
270 break;
272 if (!pcap_cache_add_specific(&tmp_pcap_cache, name, info)) {
273 goto out;
277 ippDelete(response);
278 response = NULL;
281 * Build a CUPS_GET_CLASSES request, which requires the following
282 * attributes:
284 * attributes-charset
285 * attributes-natural-language
286 * requested-attributes
289 request = ippNew();
291 request->request.op.operation_id = CUPS_GET_CLASSES;
292 request->request.op.request_id = 1;
294 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
295 "attributes-charset", NULL, "utf-8");
297 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
298 "attributes-natural-language", NULL, language->language);
300 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
301 "requested-attributes",
302 (sizeof(requested) / sizeof(requested[0])),
303 NULL, requested);
306 * Do the request and get back a response...
309 if ((response = cupsDoRequest(http, request, "/")) == NULL) {
310 DEBUG(0,("Unable to get printer list - %s\n",
311 ippErrorString(cupsLastError())));
312 goto out;
315 for (attr = response->attrs; attr != NULL;) {
317 * Skip leading attributes until we hit a printer...
320 while (attr != NULL && attr->group_tag != IPP_TAG_PRINTER)
321 attr = attr->next;
323 if (attr == NULL)
324 break;
327 * Pull the needed attributes from this printer...
330 name = NULL;
331 info = NULL;
333 while (attr != NULL && attr->group_tag == IPP_TAG_PRINTER) {
334 if (strcmp(attr->name, "printer-name") == 0 &&
335 attr->value_tag == IPP_TAG_NAME) {
336 if (!pull_utf8_talloc(frame,
337 &name,
338 attr->values[0].string.text,
339 &size)) {
340 goto out;
344 if (strcmp(attr->name, "printer-info") == 0 &&
345 attr->value_tag == IPP_TAG_TEXT) {
346 if (!pull_utf8_talloc(frame,
347 &info,
348 attr->values[0].string.text,
349 &size)) {
350 goto out;
354 attr = attr->next;
358 * See if we have everything needed...
361 if (name == NULL)
362 break;
364 if (!pcap_cache_add_specific(&tmp_pcap_cache, name, info)) {
365 goto out;
369 ret = True;
371 out:
372 if (response)
373 ippDelete(response);
375 if (language)
376 cupsLangFree(language);
378 if (http)
379 httpClose(http);
381 /* Send all the entries up the pipe. */
382 if (tmp_pcap_cache) {
383 pcap_printer_fn_specific(tmp_pcap_cache,
384 send_pcap_info,
385 (void *)&fd);
387 pcap_cache_destroy_specific(&tmp_pcap_cache);
389 TALLOC_FREE(frame);
390 return ret;
393 static struct fd_event *cache_fd_event;
395 static bool cups_pcap_load_async(struct tevent_context *ev,
396 struct messaging_context *msg_ctx,
397 int *pfd)
399 int fds[2];
400 pid_t pid;
401 NTSTATUS status;
403 *pfd = -1;
405 if (cache_fd_event) {
406 DEBUG(3,("cups_pcap_load_async: already waiting for "
407 "a refresh event\n" ));
408 return false;
411 DEBUG(5,("cups_pcap_load_async: asynchronously loading cups printers\n"));
413 if (pipe(fds) == -1) {
414 return false;
417 pid = sys_fork();
418 if (pid == (pid_t)-1) {
419 DEBUG(10,("cups_pcap_load_async: fork failed %s\n",
420 strerror(errno) ));
421 close(fds[0]);
422 close(fds[1]);
423 return false;
426 if (pid) {
427 DEBUG(10,("cups_pcap_load_async: child pid = %u\n",
428 (unsigned int)pid ));
429 /* Parent. */
430 close(fds[1]);
431 *pfd = fds[0];
432 return true;
435 /* Child. */
437 close_all_print_db();
439 status = reinit_after_fork(msg_ctx, ev, procid_self(), true);
440 if (!NT_STATUS_IS_OK(status)) {
441 DEBUG(0,("cups_pcap_load_async: reinit_after_fork() failed\n"));
442 smb_panic("cups_pcap_load_async: reinit_after_fork() failed");
445 close(fds[0]);
446 cups_cache_reload_async(fds[1]);
447 close(fds[1]);
448 _exit(0);
451 struct cups_async_cb_args {
452 int pipe_fd;
453 struct event_context *event_ctx;
454 struct messaging_context *msg_ctx;
455 void (*post_cache_fill_fn)(struct event_context *,
456 struct messaging_context *);
459 static void cups_async_callback(struct event_context *event_ctx,
460 struct fd_event *event,
461 uint16 flags,
462 void *p)
464 TALLOC_CTX *frame = talloc_stackframe();
465 struct cups_async_cb_args *cb_args = (struct cups_async_cb_args *)p;
466 int fd = cb_args->pipe_fd;
467 struct pcap_cache *tmp_pcap_cache = NULL;
468 bool ret_ok = true;
470 DEBUG(5,("cups_async_callback: callback received for printer data. "
471 "fd = %d\n", fd));
473 while (1) {
474 char *name = NULL, *info = NULL;
475 size_t namelen = 0, infolen = 0;
476 ssize_t ret = -1;
478 ret = sys_read(fd, &namelen, sizeof(namelen));
479 if (ret == 0) {
480 /* EOF */
481 break;
483 if (ret != sizeof(namelen)) {
484 DEBUG(10,("cups_async_callback: namelen read failed %d %s\n",
485 errno, strerror(errno)));
486 ret_ok = false;
487 break;
490 DEBUG(11,("cups_async_callback: read namelen %u\n",
491 (unsigned int)namelen));
493 ret = sys_read(fd, &infolen, sizeof(infolen));
494 if (ret == 0) {
495 /* EOF */
496 break;
498 if (ret != sizeof(infolen)) {
499 DEBUG(10,("cups_async_callback: infolen read failed %s\n",
500 strerror(errno)));
501 ret_ok = false;
502 break;
505 DEBUG(11,("cups_async_callback: read infolen %u\n",
506 (unsigned int)infolen));
508 if (namelen) {
509 name = TALLOC_ARRAY(frame, char, namelen);
510 if (!name) {
511 ret_ok = false;
512 break;
514 ret = sys_read(fd, name, namelen);
515 if (ret == 0) {
516 /* EOF */
517 break;
519 if (ret != namelen) {
520 DEBUG(10,("cups_async_callback: name read failed %s\n",
521 strerror(errno)));
522 ret_ok = false;
523 break;
525 DEBUG(11,("cups_async_callback: read name %s\n",
526 name));
527 } else {
528 name = NULL;
530 if (infolen) {
531 info = TALLOC_ARRAY(frame, char, infolen);
532 if (!info) {
533 ret_ok = false;
534 break;
536 ret = sys_read(fd, info, infolen);
537 if (ret == 0) {
538 /* EOF */
539 break;
541 if (ret != infolen) {
542 DEBUG(10,("cups_async_callback: info read failed %s\n",
543 strerror(errno)));
544 ret_ok = false;
545 break;
547 DEBUG(11,("cups_async_callback: read info %s\n",
548 info));
549 } else {
550 info = NULL;
553 /* Add to our local pcap cache. */
554 ret_ok = pcap_cache_add_specific(&tmp_pcap_cache, name, info);
555 TALLOC_FREE(name);
556 TALLOC_FREE(info);
557 if (!ret_ok) {
558 DEBUG(0, ("failed to add to tmp pcap cache\n"));
559 break;
563 TALLOC_FREE(frame);
564 if (!ret_ok) {
565 DEBUG(0, ("failed to read a new printer list\n"));
566 pcap_cache_destroy_specific(&tmp_pcap_cache);
567 } else {
569 * replace the system-wide pcap cache with a (possibly empty)
570 * new one.
571 * FIXME The child process does not currently propagate cups
572 * errors back up to the parent, therefore we cannot
573 * differentiate between an empty printer list and a failure.
575 ret_ok = pcap_cache_replace(tmp_pcap_cache);
576 if (!ret_ok) {
577 DEBUG(0, ("failed to replace pcap cache\n"));
578 } else if (cb_args->post_cache_fill_fn != NULL) {
579 /* Caller requested post cache fill callback */
580 cb_args->post_cache_fill_fn(cb_args->event_ctx,
581 cb_args->msg_ctx);
584 close(fd);
585 TALLOC_FREE(cb_args);
586 TALLOC_FREE(cache_fd_event);
589 bool cups_cache_reload(struct tevent_context *ev,
590 struct messaging_context *msg_ctx,
591 void (*post_cache_fill_fn)(struct tevent_context *,
592 struct messaging_context *))
594 struct cups_async_cb_args *cb_args;
595 int *p_pipe_fd;
597 cb_args = TALLOC_P(NULL, struct cups_async_cb_args);
598 if (cb_args == NULL) {
599 return false;
602 cb_args->post_cache_fill_fn = post_cache_fill_fn;
603 cb_args->event_ctx = ev;
604 cb_args->msg_ctx = msg_ctx;
605 p_pipe_fd = &cb_args->pipe_fd;
606 *p_pipe_fd = -1;
608 /* Set up an async refresh. */
609 if (!cups_pcap_load_async(ev, msg_ctx, p_pipe_fd)) {
610 talloc_free(cb_args);
611 return false;
614 DEBUG(10,("cups_cache_reload: async read on fd %d\n",
615 *p_pipe_fd ));
617 /* Trigger an event when the pipe can be read. */
618 cache_fd_event = event_add_fd(ev,
619 NULL, *p_pipe_fd,
620 EVENT_FD_READ,
621 cups_async_callback,
622 (void *)cb_args);
623 if (!cache_fd_event) {
624 close(*p_pipe_fd);
625 TALLOC_FREE(cb_args);
626 return false;
629 return true;
633 * 'cups_job_delete()' - Delete a job.
636 static int cups_job_delete(const char *sharename, const char *lprm_command, struct printjob *pjob)
638 TALLOC_CTX *frame = talloc_stackframe();
639 int ret = 1; /* Return value */
640 http_t *http = NULL; /* HTTP connection to server */
641 ipp_t *request = NULL, /* IPP Request */
642 *response = NULL; /* IPP Response */
643 cups_lang_t *language = NULL; /* Default language */
644 char *user = NULL;
645 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
646 size_t size;
648 DEBUG(5,("cups_job_delete(%s, %p (%d))\n", sharename, pjob, pjob->sysjob));
651 * Make sure we don't ask for passwords...
654 cupsSetPasswordCB(cups_passwd_cb);
657 * Try to connect to the server...
660 if ((http = cups_connect(frame)) == NULL) {
661 goto out;
665 * Build an IPP_CANCEL_JOB request, which requires the following
666 * attributes:
668 * attributes-charset
669 * attributes-natural-language
670 * job-uri
671 * requesting-user-name
674 request = ippNew();
676 request->request.op.operation_id = IPP_CANCEL_JOB;
677 request->request.op.request_id = 1;
679 language = cupsLangDefault();
681 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
682 "attributes-charset", NULL, "utf-8");
684 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
685 "attributes-natural-language", NULL, language->language);
687 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/jobs/%d", pjob->sysjob);
689 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri", NULL, uri);
691 if (!push_utf8_talloc(frame, &user, pjob->user, &size)) {
692 goto out;
695 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
696 NULL, user);
699 * Do the request and get back a response...
702 if ((response = cupsDoRequest(http, request, "/jobs")) != NULL) {
703 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
704 DEBUG(0,("Unable to cancel job %d - %s\n", pjob->sysjob,
705 ippErrorString(cupsLastError())));
706 } else {
707 ret = 0;
709 } else {
710 DEBUG(0,("Unable to cancel job %d - %s\n", pjob->sysjob,
711 ippErrorString(cupsLastError())));
714 out:
715 if (response)
716 ippDelete(response);
718 if (language)
719 cupsLangFree(language);
721 if (http)
722 httpClose(http);
724 TALLOC_FREE(frame);
725 return ret;
730 * 'cups_job_pause()' - Pause a job.
733 static int cups_job_pause(int snum, struct printjob *pjob)
735 TALLOC_CTX *frame = talloc_stackframe();
736 int ret = 1; /* Return value */
737 http_t *http = NULL; /* HTTP connection to server */
738 ipp_t *request = NULL, /* IPP Request */
739 *response = NULL; /* IPP Response */
740 cups_lang_t *language = NULL; /* Default language */
741 char *user = NULL;
742 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
743 size_t size;
745 DEBUG(5,("cups_job_pause(%d, %p (%d))\n", snum, pjob, pjob->sysjob));
748 * Make sure we don't ask for passwords...
751 cupsSetPasswordCB(cups_passwd_cb);
754 * Try to connect to the server...
757 if ((http = cups_connect(frame)) == NULL) {
758 goto out;
762 * Build an IPP_HOLD_JOB request, which requires the following
763 * attributes:
765 * attributes-charset
766 * attributes-natural-language
767 * job-uri
768 * requesting-user-name
771 request = ippNew();
773 request->request.op.operation_id = IPP_HOLD_JOB;
774 request->request.op.request_id = 1;
776 language = cupsLangDefault();
778 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
779 "attributes-charset", NULL, "utf-8");
781 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
782 "attributes-natural-language", NULL, language->language);
784 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/jobs/%d", pjob->sysjob);
786 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri", NULL, uri);
788 if (!push_utf8_talloc(frame, &user, pjob->user, &size)) {
789 goto out;
791 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
792 NULL, user);
795 * Do the request and get back a response...
798 if ((response = cupsDoRequest(http, request, "/jobs")) != NULL) {
799 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
800 DEBUG(0,("Unable to hold job %d - %s\n", pjob->sysjob,
801 ippErrorString(cupsLastError())));
802 } else {
803 ret = 0;
805 } else {
806 DEBUG(0,("Unable to hold job %d - %s\n", pjob->sysjob,
807 ippErrorString(cupsLastError())));
810 out:
811 if (response)
812 ippDelete(response);
814 if (language)
815 cupsLangFree(language);
817 if (http)
818 httpClose(http);
820 TALLOC_FREE(frame);
821 return ret;
826 * 'cups_job_resume()' - Resume a paused job.
829 static int cups_job_resume(int snum, struct printjob *pjob)
831 TALLOC_CTX *frame = talloc_stackframe();
832 int ret = 1; /* Return value */
833 http_t *http = NULL; /* HTTP connection to server */
834 ipp_t *request = NULL, /* IPP Request */
835 *response = NULL; /* IPP Response */
836 cups_lang_t *language = NULL; /* Default language */
837 char *user = NULL;
838 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
839 size_t size;
841 DEBUG(5,("cups_job_resume(%d, %p (%d))\n", snum, pjob, pjob->sysjob));
844 * Make sure we don't ask for passwords...
847 cupsSetPasswordCB(cups_passwd_cb);
850 * Try to connect to the server...
853 if ((http = cups_connect(frame)) == NULL) {
854 goto out;
858 * Build an IPP_RELEASE_JOB request, which requires the following
859 * attributes:
861 * attributes-charset
862 * attributes-natural-language
863 * job-uri
864 * requesting-user-name
867 request = ippNew();
869 request->request.op.operation_id = IPP_RELEASE_JOB;
870 request->request.op.request_id = 1;
872 language = cupsLangDefault();
874 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
875 "attributes-charset", NULL, "utf-8");
877 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
878 "attributes-natural-language", NULL, language->language);
880 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/jobs/%d", pjob->sysjob);
882 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri", NULL, uri);
884 if (!push_utf8_talloc(frame, &user, pjob->user, &size)) {
885 goto out;
887 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
888 NULL, user);
891 * Do the request and get back a response...
894 if ((response = cupsDoRequest(http, request, "/jobs")) != NULL) {
895 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
896 DEBUG(0,("Unable to release job %d - %s\n", pjob->sysjob,
897 ippErrorString(cupsLastError())));
898 } else {
899 ret = 0;
901 } else {
902 DEBUG(0,("Unable to release job %d - %s\n", pjob->sysjob,
903 ippErrorString(cupsLastError())));
906 out:
907 if (response)
908 ippDelete(response);
910 if (language)
911 cupsLangFree(language);
913 if (http)
914 httpClose(http);
916 TALLOC_FREE(frame);
917 return ret;
922 * 'cups_job_submit()' - Submit a job for printing.
925 static int cups_job_submit(int snum, struct printjob *pjob)
927 TALLOC_CTX *frame = talloc_stackframe();
928 int ret = 1; /* Return value */
929 http_t *http = NULL; /* HTTP connection to server */
930 ipp_t *request = NULL, /* IPP Request */
931 *response = NULL; /* IPP Response */
932 ipp_attribute_t *attr_job_id = NULL; /* IPP Attribute "job-id" */
933 cups_lang_t *language = NULL; /* Default language */
934 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
935 char *new_jobname = NULL;
936 int num_options = 0;
937 cups_option_t *options = NULL;
938 char *printername = NULL;
939 char *user = NULL;
940 char *jobname = NULL;
941 char *cupsoptions = NULL;
942 char *filename = NULL;
943 size_t size;
944 uint32_t jobid = (uint32_t)-1;
946 DEBUG(5,("cups_job_submit(%d, %p)\n", snum, pjob));
949 * Make sure we don't ask for passwords...
952 cupsSetPasswordCB(cups_passwd_cb);
955 * Try to connect to the server...
958 if ((http = cups_connect(frame)) == NULL) {
959 goto out;
963 * Build an IPP_PRINT_JOB request, which requires the following
964 * attributes:
966 * attributes-charset
967 * attributes-natural-language
968 * printer-uri
969 * requesting-user-name
970 * [document-data]
973 request = ippNew();
975 request->request.op.operation_id = IPP_PRINT_JOB;
976 request->request.op.request_id = 1;
978 language = cupsLangDefault();
980 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
981 "attributes-charset", NULL, "utf-8");
983 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
984 "attributes-natural-language", NULL, language->language);
986 if (!push_utf8_talloc(frame, &printername, lp_printername(snum),
987 &size)) {
988 goto out;
990 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/printers/%s",
991 printername);
993 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
994 "printer-uri", NULL, uri);
996 if (!push_utf8_talloc(frame, &user, pjob->user, &size)) {
997 goto out;
999 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
1000 NULL, user);
1002 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
1003 "job-originating-host-name", NULL,
1004 pjob->clientmachine);
1006 /* Get the jobid from the filename. */
1007 jobid = print_parse_jobid(pjob->filename);
1008 if (jobid == (uint32_t)-1) {
1009 DEBUG(0,("cups_job_submit: failed to parse jobid from name %s\n",
1010 pjob->filename ));
1011 jobid = 0;
1014 if (!push_utf8_talloc(frame, &jobname, pjob->jobname, &size)) {
1015 goto out;
1017 new_jobname = talloc_asprintf(frame,
1018 "%s%.8u %s", PRINT_SPOOL_PREFIX,
1019 (unsigned int)jobid,
1020 jobname);
1021 if (new_jobname == NULL) {
1022 goto out;
1025 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "job-name", NULL,
1026 new_jobname);
1029 * add any options defined in smb.conf
1032 if (!push_utf8_talloc(frame, &cupsoptions, lp_cups_options(snum), &size)) {
1033 goto out;
1035 num_options = 0;
1036 options = NULL;
1037 num_options = cupsParseOptions(cupsoptions, num_options, &options);
1039 if ( num_options )
1040 cupsEncodeOptions(request, num_options, options);
1043 * Do the request and get back a response...
1046 slprintf(uri, sizeof(uri) - 1, "/printers/%s", printername);
1048 if (!push_utf8_talloc(frame, &filename, pjob->filename, &size)) {
1049 goto out;
1051 if ((response = cupsDoFileRequest(http, request, uri, pjob->filename)) != NULL) {
1052 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
1053 DEBUG(0,("Unable to print file to %s - %s\n",
1054 lp_printername(snum),
1055 ippErrorString(cupsLastError())));
1056 } else {
1057 ret = 0;
1058 attr_job_id = ippFindAttribute(response, "job-id", IPP_TAG_INTEGER);
1059 if(attr_job_id) {
1060 pjob->sysjob = attr_job_id->values[0].integer;
1061 DEBUG(5,("cups_job_submit: job-id %d\n", pjob->sysjob));
1062 } else {
1063 DEBUG(0,("Missing job-id attribute in IPP response"));
1066 } else {
1067 DEBUG(0,("Unable to print file to `%s' - %s\n",
1068 lp_printername(snum),
1069 ippErrorString(cupsLastError())));
1072 if ( ret == 0 )
1073 unlink(pjob->filename);
1074 /* else print_job_end will do it for us */
1076 out:
1077 if (response)
1078 ippDelete(response);
1080 if (language)
1081 cupsLangFree(language);
1083 if (http)
1084 httpClose(http);
1086 TALLOC_FREE(frame);
1088 return ret;
1092 * 'cups_queue_get()' - Get all the jobs in the print queue.
1095 static int cups_queue_get(const char *sharename,
1096 enum printing_types printing_type,
1097 char *lpq_command,
1098 print_queue_struct **q,
1099 print_status_struct *status)
1101 TALLOC_CTX *frame = talloc_stackframe();
1102 char *printername = NULL;
1103 http_t *http = NULL; /* HTTP connection to server */
1104 ipp_t *request = NULL, /* IPP Request */
1105 *response = NULL; /* IPP Response */
1106 ipp_attribute_t *attr = NULL; /* Current attribute */
1107 cups_lang_t *language = NULL; /* Default language */
1108 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
1109 int qcount = 0, /* Number of active queue entries */
1110 qalloc = 0; /* Number of queue entries allocated */
1111 print_queue_struct *queue = NULL, /* Queue entries */
1112 *temp; /* Temporary pointer for queue */
1113 char *user_name = NULL, /* job-originating-user-name attribute */
1114 *job_name = NULL; /* job-name attribute */
1115 int job_id; /* job-id attribute */
1116 int job_k_octets; /* job-k-octets attribute */
1117 time_t job_time; /* time-at-creation attribute */
1118 ipp_jstate_t job_status; /* job-status attribute */
1119 int job_priority; /* job-priority attribute */
1120 size_t size;
1121 static const char *jattrs[] = /* Requested job attributes */
1123 "job-id",
1124 "job-k-octets",
1125 "job-name",
1126 "job-originating-user-name",
1127 "job-priority",
1128 "job-state",
1129 "time-at-creation",
1131 static const char *pattrs[] = /* Requested printer attributes */
1133 "printer-state",
1134 "printer-state-message"
1137 *q = NULL;
1139 /* HACK ALERT!!! The problem with support the 'printer name'
1140 option is that we key the tdb off the sharename. So we will
1141 overload the lpq_command string to pass in the printername
1142 (which is basically what we do for non-cups printers ... using
1143 the lpq_command to get the queue listing). */
1145 if (!push_utf8_talloc(frame, &printername, lpq_command, &size)) {
1146 goto out;
1148 DEBUG(5,("cups_queue_get(%s, %p, %p)\n", lpq_command, q, status));
1151 * Make sure we don't ask for passwords...
1154 cupsSetPasswordCB(cups_passwd_cb);
1157 * Try to connect to the server...
1160 if ((http = cups_connect(frame)) == NULL) {
1161 goto out;
1165 * Generate the printer URI...
1168 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/printers/%s", printername);
1171 * Build an IPP_GET_JOBS request, which requires the following
1172 * attributes:
1174 * attributes-charset
1175 * attributes-natural-language
1176 * requested-attributes
1177 * printer-uri
1180 request = ippNew();
1182 request->request.op.operation_id = IPP_GET_JOBS;
1183 request->request.op.request_id = 1;
1185 language = cupsLangDefault();
1187 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
1188 "attributes-charset", NULL, "utf-8");
1190 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
1191 "attributes-natural-language", NULL, language->language);
1193 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
1194 "requested-attributes",
1195 (sizeof(jattrs) / sizeof(jattrs[0])),
1196 NULL, jattrs);
1198 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
1199 "printer-uri", NULL, uri);
1202 * Do the request and get back a response...
1205 if ((response = cupsDoRequest(http, request, "/")) == NULL) {
1206 DEBUG(0,("Unable to get jobs for %s - %s\n", uri,
1207 ippErrorString(cupsLastError())));
1208 goto out;
1211 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
1212 DEBUG(0,("Unable to get jobs for %s - %s\n", uri,
1213 ippErrorString(response->request.status.status_code)));
1214 goto out;
1218 * Process the jobs...
1221 qcount = 0;
1222 qalloc = 0;
1223 queue = NULL;
1225 for (attr = response->attrs; attr != NULL; attr = attr->next) {
1227 * Skip leading attributes until we hit a job...
1230 while (attr != NULL && attr->group_tag != IPP_TAG_JOB)
1231 attr = attr->next;
1233 if (attr == NULL)
1234 break;
1237 * Allocate memory as needed...
1239 if (qcount >= qalloc) {
1240 qalloc += 16;
1242 queue = SMB_REALLOC_ARRAY(queue, print_queue_struct, qalloc);
1244 if (queue == NULL) {
1245 DEBUG(0,("cups_queue_get: Not enough memory!"));
1246 qcount = 0;
1247 goto out;
1251 temp = queue + qcount;
1252 memset(temp, 0, sizeof(print_queue_struct));
1255 * Pull the needed attributes from this job...
1258 job_id = 0;
1259 job_priority = 50;
1260 job_status = IPP_JOB_PENDING;
1261 job_time = 0;
1262 job_k_octets = 0;
1263 user_name = NULL;
1264 job_name = NULL;
1266 while (attr != NULL && attr->group_tag == IPP_TAG_JOB) {
1267 if (attr->name == NULL) {
1268 attr = attr->next;
1269 break;
1272 if (strcmp(attr->name, "job-id") == 0 &&
1273 attr->value_tag == IPP_TAG_INTEGER)
1274 job_id = attr->values[0].integer;
1276 if (strcmp(attr->name, "job-k-octets") == 0 &&
1277 attr->value_tag == IPP_TAG_INTEGER)
1278 job_k_octets = attr->values[0].integer;
1280 if (strcmp(attr->name, "job-priority") == 0 &&
1281 attr->value_tag == IPP_TAG_INTEGER)
1282 job_priority = attr->values[0].integer;
1284 if (strcmp(attr->name, "job-state") == 0 &&
1285 attr->value_tag == IPP_TAG_ENUM)
1286 job_status = (ipp_jstate_t)(attr->values[0].integer);
1288 if (strcmp(attr->name, "time-at-creation") == 0 &&
1289 attr->value_tag == IPP_TAG_INTEGER)
1290 job_time = attr->values[0].integer;
1292 if (strcmp(attr->name, "job-name") == 0 &&
1293 attr->value_tag == IPP_TAG_NAME) {
1294 if (!pull_utf8_talloc(frame,
1295 &job_name,
1296 attr->values[0].string.text,
1297 &size)) {
1298 goto out;
1302 if (strcmp(attr->name, "job-originating-user-name") == 0 &&
1303 attr->value_tag == IPP_TAG_NAME) {
1304 if (!pull_utf8_talloc(frame,
1305 &user_name,
1306 attr->values[0].string.text,
1307 &size)) {
1308 goto out;
1312 attr = attr->next;
1316 * See if we have everything needed...
1319 if (user_name == NULL || job_name == NULL || job_id == 0) {
1320 if (attr == NULL)
1321 break;
1322 else
1323 continue;
1326 temp->job = job_id;
1327 temp->size = job_k_octets * 1024;
1328 temp->status = job_status == IPP_JOB_PENDING ? LPQ_QUEUED :
1329 job_status == IPP_JOB_STOPPED ? LPQ_PAUSED :
1330 job_status == IPP_JOB_HELD ? LPQ_PAUSED :
1331 LPQ_PRINTING;
1332 temp->priority = job_priority;
1333 temp->time = job_time;
1334 strlcpy(temp->fs_user, user_name, sizeof(temp->fs_user));
1335 strlcpy(temp->fs_file, job_name, sizeof(temp->fs_file));
1337 qcount ++;
1339 if (attr == NULL)
1340 break;
1343 ippDelete(response);
1344 response = NULL;
1347 * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the
1348 * following attributes:
1350 * attributes-charset
1351 * attributes-natural-language
1352 * requested-attributes
1353 * printer-uri
1356 request = ippNew();
1358 request->request.op.operation_id = IPP_GET_PRINTER_ATTRIBUTES;
1359 request->request.op.request_id = 1;
1361 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
1362 "attributes-charset", NULL, "utf-8");
1364 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
1365 "attributes-natural-language", NULL, language->language);
1367 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
1368 "requested-attributes",
1369 (sizeof(pattrs) / sizeof(pattrs[0])),
1370 NULL, pattrs);
1372 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
1373 "printer-uri", NULL, uri);
1376 * Do the request and get back a response...
1379 if ((response = cupsDoRequest(http, request, "/")) == NULL) {
1380 DEBUG(0,("Unable to get printer status for %s - %s\n", printername,
1381 ippErrorString(cupsLastError())));
1382 goto out;
1385 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
1386 DEBUG(0,("Unable to get printer status for %s - %s\n", printername,
1387 ippErrorString(response->request.status.status_code)));
1388 goto out;
1392 * Get the current printer status and convert it to the SAMBA values.
1395 if ((attr = ippFindAttribute(response, "printer-state", IPP_TAG_ENUM)) != NULL) {
1396 if (attr->values[0].integer == IPP_PRINTER_STOPPED)
1397 status->status = LPSTAT_STOPPED;
1398 else
1399 status->status = LPSTAT_OK;
1402 if ((attr = ippFindAttribute(response, "printer-state-message",
1403 IPP_TAG_TEXT)) != NULL) {
1404 char *msg = NULL;
1405 if (!pull_utf8_talloc(frame, &msg,
1406 attr->values[0].string.text,
1407 &size)) {
1408 SAFE_FREE(queue);
1409 qcount = 0;
1410 goto out;
1412 fstrcpy(status->message, msg);
1415 out:
1418 * Return the job queue...
1421 *q = queue;
1423 if (response)
1424 ippDelete(response);
1426 if (language)
1427 cupsLangFree(language);
1429 if (http)
1430 httpClose(http);
1432 TALLOC_FREE(frame);
1433 return qcount;
1438 * 'cups_queue_pause()' - Pause a print queue.
1441 static int cups_queue_pause(int snum)
1443 TALLOC_CTX *frame = talloc_stackframe();
1444 int ret = 1; /* Return value */
1445 http_t *http = NULL; /* HTTP connection to server */
1446 ipp_t *request = NULL, /* IPP Request */
1447 *response = NULL; /* IPP Response */
1448 cups_lang_t *language = NULL; /* Default language */
1449 char *printername = NULL;
1450 char *username = NULL;
1451 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
1452 size_t size;
1454 DEBUG(5,("cups_queue_pause(%d)\n", snum));
1457 * Make sure we don't ask for passwords...
1460 cupsSetPasswordCB(cups_passwd_cb);
1463 * Try to connect to the server...
1466 if ((http = cups_connect(frame)) == NULL) {
1467 goto out;
1471 * Build an IPP_PAUSE_PRINTER request, which requires the following
1472 * attributes:
1474 * attributes-charset
1475 * attributes-natural-language
1476 * printer-uri
1477 * requesting-user-name
1480 request = ippNew();
1482 request->request.op.operation_id = IPP_PAUSE_PRINTER;
1483 request->request.op.request_id = 1;
1485 language = cupsLangDefault();
1487 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
1488 "attributes-charset", NULL, "utf-8");
1490 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
1491 "attributes-natural-language", NULL, language->language);
1493 if (!push_utf8_talloc(frame, &printername, lp_printername(snum),
1494 &size)) {
1495 goto out;
1497 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/printers/%s",
1498 printername);
1500 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, uri);
1502 if (!push_utf8_talloc(frame, &username, current_user_info.unix_name, &size)) {
1503 goto out;
1505 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
1506 NULL, username);
1509 * Do the request and get back a response...
1512 if ((response = cupsDoRequest(http, request, "/admin/")) != NULL) {
1513 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
1514 DEBUG(0,("Unable to pause printer %s - %s\n",
1515 lp_printername(snum),
1516 ippErrorString(cupsLastError())));
1517 } else {
1518 ret = 0;
1520 } else {
1521 DEBUG(0,("Unable to pause printer %s - %s\n",
1522 lp_printername(snum),
1523 ippErrorString(cupsLastError())));
1526 out:
1527 if (response)
1528 ippDelete(response);
1530 if (language)
1531 cupsLangFree(language);
1533 if (http)
1534 httpClose(http);
1536 TALLOC_FREE(frame);
1537 return ret;
1542 * 'cups_queue_resume()' - Restart a print queue.
1545 static int cups_queue_resume(int snum)
1547 TALLOC_CTX *frame = talloc_stackframe();
1548 int ret = 1; /* Return value */
1549 http_t *http = NULL; /* HTTP connection to server */
1550 ipp_t *request = NULL, /* IPP Request */
1551 *response = NULL; /* IPP Response */
1552 cups_lang_t *language = NULL; /* Default language */
1553 char *printername = NULL;
1554 char *username = NULL;
1555 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
1556 size_t size;
1558 DEBUG(5,("cups_queue_resume(%d)\n", snum));
1561 * Make sure we don't ask for passwords...
1564 cupsSetPasswordCB(cups_passwd_cb);
1567 * Try to connect to the server...
1570 if ((http = cups_connect(frame)) == NULL) {
1571 goto out;
1575 * Build an IPP_RESUME_PRINTER request, which requires the following
1576 * attributes:
1578 * attributes-charset
1579 * attributes-natural-language
1580 * printer-uri
1581 * requesting-user-name
1584 request = ippNew();
1586 request->request.op.operation_id = IPP_RESUME_PRINTER;
1587 request->request.op.request_id = 1;
1589 language = cupsLangDefault();
1591 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
1592 "attributes-charset", NULL, "utf-8");
1594 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
1595 "attributes-natural-language", NULL, language->language);
1597 if (!push_utf8_talloc(frame, &printername, lp_printername(snum),
1598 &size)) {
1599 goto out;
1601 slprintf(uri, sizeof(uri) - 1, "ipp://localhost/printers/%s",
1602 printername);
1604 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, uri);
1606 if (!push_utf8_talloc(frame, &username, current_user_info.unix_name, &size)) {
1607 goto out;
1609 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
1610 NULL, username);
1613 * Do the request and get back a response...
1616 if ((response = cupsDoRequest(http, request, "/admin/")) != NULL) {
1617 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
1618 DEBUG(0,("Unable to resume printer %s - %s\n",
1619 lp_printername(snum),
1620 ippErrorString(cupsLastError())));
1621 } else {
1622 ret = 0;
1624 } else {
1625 DEBUG(0,("Unable to resume printer %s - %s\n",
1626 lp_printername(snum),
1627 ippErrorString(cupsLastError())));
1630 out:
1631 if (response)
1632 ippDelete(response);
1634 if (language)
1635 cupsLangFree(language);
1637 if (http)
1638 httpClose(http);
1640 TALLOC_FREE(frame);
1641 return ret;
1644 /*******************************************************************
1645 * CUPS printing interface definitions...
1646 ******************************************************************/
1648 struct printif cups_printif =
1650 PRINT_CUPS,
1651 cups_queue_get,
1652 cups_queue_pause,
1653 cups_queue_resume,
1654 cups_job_delete,
1655 cups_job_pause,
1656 cups_job_resume,
1657 cups_job_submit,
1660 bool cups_pull_comment_location(TALLOC_CTX *mem_ctx,
1661 const char *printername,
1662 char **comment,
1663 char **location)
1665 TALLOC_CTX *frame = talloc_stackframe();
1666 http_t *http = NULL; /* HTTP connection to server */
1667 ipp_t *request = NULL, /* IPP Request */
1668 *response = NULL; /* IPP Response */
1669 ipp_attribute_t *attr; /* Current attribute */
1670 cups_lang_t *language = NULL; /* Default language */
1671 char uri[HTTP_MAX_URI];
1672 char *server = NULL;
1673 char *sharename = NULL;
1674 char *name = NULL;
1675 static const char *requested[] =/* Requested attributes */
1677 "printer-name",
1678 "printer-info",
1679 "printer-location"
1681 bool ret = False;
1682 size_t size;
1684 DEBUG(5, ("pulling %s location\n", printername));
1687 * Make sure we don't ask for passwords...
1690 cupsSetPasswordCB(cups_passwd_cb);
1693 * Try to connect to the server...
1696 if ((http = cups_connect(frame)) == NULL) {
1697 goto out;
1700 request = ippNew();
1702 request->request.op.operation_id = IPP_GET_PRINTER_ATTRIBUTES;
1703 request->request.op.request_id = 1;
1705 language = cupsLangDefault();
1707 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
1708 "attributes-charset", NULL, "utf-8");
1710 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
1711 "attributes-natural-language", NULL, language->language);
1713 if (lp_cups_server() != NULL && strlen(lp_cups_server()) > 0) {
1714 if (!push_utf8_talloc(frame, &server, lp_cups_server(), &size)) {
1715 goto out;
1717 } else {
1718 server = talloc_strdup(frame,cupsServer());
1720 if (server) {
1721 goto out;
1723 if (!push_utf8_talloc(frame, &sharename, printername, &size)) {
1724 goto out;
1726 slprintf(uri, sizeof(uri) - 1, "ipp://%s/printers/%s",
1727 server, sharename);
1729 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
1730 "printer-uri", NULL, uri);
1732 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
1733 "requested-attributes",
1734 (sizeof(requested) / sizeof(requested[0])),
1735 NULL, requested);
1738 * Do the request and get back a response...
1741 if ((response = cupsDoRequest(http, request, "/")) == NULL) {
1742 DEBUG(0,("Unable to get printer attributes - %s\n",
1743 ippErrorString(cupsLastError())));
1744 goto out;
1747 for (attr = response->attrs; attr != NULL;) {
1749 * Skip leading attributes until we hit a printer...
1752 while (attr != NULL && attr->group_tag != IPP_TAG_PRINTER)
1753 attr = attr->next;
1755 if (attr == NULL)
1756 break;
1759 * Pull the needed attributes from this printer...
1762 while ( attr && (attr->group_tag == IPP_TAG_PRINTER) ) {
1763 if (strcmp(attr->name, "printer-name") == 0 &&
1764 attr->value_tag == IPP_TAG_NAME) {
1765 if (!pull_utf8_talloc(frame,
1766 &name,
1767 attr->values[0].string.text,
1768 &size)) {
1769 goto out;
1773 /* Grab the comment if we don't have one */
1774 if ( (strcmp(attr->name, "printer-info") == 0)
1775 && (attr->value_tag == IPP_TAG_TEXT))
1777 if (!pull_utf8_talloc(mem_ctx,
1778 comment,
1779 attr->values[0].string.text,
1780 &size)) {
1781 goto out;
1783 DEBUG(5,("cups_pull_comment_location: Using cups comment: %s\n",
1784 *comment));
1787 /* Grab the location if we don't have one */
1788 if ( (strcmp(attr->name, "printer-location") == 0)
1789 && (attr->value_tag == IPP_TAG_TEXT))
1791 if (!pull_utf8_talloc(mem_ctx,
1792 location,
1793 attr->values[0].string.text,
1794 &size)) {
1795 goto out;
1797 DEBUG(5,("cups_pull_comment_location: Using cups location: %s\n",
1798 *location));
1801 attr = attr->next;
1805 * We have everything needed...
1808 if (name != NULL)
1809 break;
1812 ret = True;
1814 out:
1815 if (response)
1816 ippDelete(response);
1818 if (request) {
1819 ippDelete(request);
1822 if (language)
1823 cupsLangFree(language);
1825 if (http)
1826 httpClose(http);
1828 TALLOC_FREE(frame);
1829 return ret;
1832 #else
1833 /* this keeps fussy compilers happy */
1834 void print_cups_dummy(void);
1835 void print_cups_dummy(void) {}
1836 #endif /* HAVE_CUPS */